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/misc.hh"
47 #include "mem/request.hh"
48 #include "sim/host.hh"
49 #include "sim/core.hh"
53 typedef Packet *PacketPtr;
54 typedef uint8_t* PacketDataPtr;
55 typedef std::list<PacketPtr> PacketList;
58 #define NACKED_LINE (1 << 0)
59 #define SATISFIED (1 << 1)
60 #define SHARED_LINE (1 << 2)
61 #define CACHE_LINE_FILL (1 << 3)
62 #define COMPRESSED (1 << 4)
63 #define NO_ALLOCATE (1 << 5)
64 #define SNOOP_COMMIT (1 << 6)
71 /** List of all commands associated with a packet. */
97 /** List of command attributes. */
112 NUM_COMMAND_ATTRIBUTES
115 /** Structure that defines attributes and other data associated
118 /** Set of attribute flags. */
119 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
120 /** Corresponding response for requests; InvalidCmd if no
121 * response is applicable. */
122 const Command response;
123 /** String representation (for printing) */
124 const std::string str;
127 /** Array to map Command enum to associated info. */
128 static const CommandInfo commandInfo[];
134 bool testCmdAttrib(MemCmd::Attribute attrib) const {
135 return commandInfo[cmd].attributes[attrib] != 0;
140 bool isRead() const { return testCmdAttrib(IsRead); }
141 bool isWrite() const { return testCmdAttrib(IsWrite); }
142 bool isRequest() const { return testCmdAttrib(IsRequest); }
143 bool isResponse() const { return testCmdAttrib(IsResponse); }
144 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
145 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
146 bool hasData() const { return testCmdAttrib(HasData); }
147 bool isReadWrite() const { return testCmdAttrib(IsReadWrite); }
149 const Command responseCommand() const {
150 return commandInfo[cmd].response;
153 /** Return the string to a cmd given by idx. */
154 const std::string &toString() const {
155 return commandInfo[cmd].str;
158 int toInt() const { return (int)cmd; }
172 bool operator==(MemCmd c2) { return (cmd == c2.cmd); }
173 bool operator!=(MemCmd c2) { return (cmd != c2.cmd); }
179 * A Packet is used to encapsulate a transfer between two objects in
180 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
181 * single Request travels all the way from the requester to the
182 * ultimate destination and back, possibly being conveyed by several
183 * different Packets along the way.)
189 typedef MemCmd::Command Command;
191 /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
195 /** A pointer to the data being transfered. It can be differnt
196 * sizes at each level of the heirarchy so it belongs in the
197 * packet, not request. This may or may not be populated when a
198 * responder recieves the packet. If not populated it memory
199 * should be allocated.
203 /** Is the data pointer set to a value that shouldn't be freed
204 * when the packet is destroyed? */
206 /** The data pointer points to a value that should be freed when
207 * the packet is destroyed. */
209 /** the data pointer points to an array (thus delete [] ) needs to
210 * be called on it rather than simply delete.*/
213 /** The address of the request. This address could be virtual or
214 * physical, depending on the system configuration. */
217 /** The size of the request or transfer. */
220 /** Device address (e.g., bus ID) of the source of the
221 * transaction. The source is not responsible for setting this
222 * field; it is set implicitly by the interconnect when the
223 * packet is first sent. */
226 /** Device address (e.g., bus ID) of the destination of the
227 * transaction. The special value Broadcast indicates that the
228 * packet should be routed based on its address. This field is
229 * initialized in the constructor and is thus always valid
230 * (unlike * addr, size, and src). */
233 /** Are the 'addr' and 'size' fields valid? */
235 /** Is the 'src' field valid? */
241 /** Used to calculate latencies for each packet.*/
244 /** The time at which the packet will be fully transmitted */
247 /** The time at which the first chunk of the packet will be transmitted */
250 /** The special destination address indicating that the packet
251 * should be routed based on its address. */
252 static const short Broadcast = -1;
254 /** A pointer to the original request. */
257 /** A virtual base opaque structure used to hold coherence-related
258 * state. A specific subclass would be derived from this to
259 * carry state specific to a particular coherence protocol. */
260 class CoherenceState {
262 virtual ~CoherenceState() {}
265 /** This packet's coherence state. Caches should use
266 * dynamic_cast<> to cast to the state appropriate for the
267 * system's coherence protocol. */
268 CoherenceState *coherence;
270 /** A virtual base opaque structure used to hold state associated
271 * with the packet but specific to the sending device (e.g., an
272 * MSHR). A pointer to this state is returned in the packet's
273 * response so that the sender can quickly look up the state
274 * needed to process it. A specific subclass would be derived
275 * from this to carry state specific to a particular sending
279 virtual ~SenderState() {}
282 /** This packet's sender state. Devices should use dynamic_cast<>
283 * to cast to the state appropriate to the sender. */
284 SenderState *senderState;
288 /** The command field of the packet. */
291 /** Return the string name of the cmd field (for debugging and
293 const std::string &cmdString() const { return cmd.toString(); }
295 /** Return the index of this command. */
296 inline int cmdToIndex() const { return cmd.toInt(); }
300 bool isRead() const { return cmd.isRead(); }
301 bool isWrite() const { return cmd.isWrite(); }
302 bool isRequest() const { return cmd.isRequest(); }
303 bool isResponse() const { return cmd.isResponse(); }
304 bool needsResponse() const { return cmd.needsResponse(); }
305 bool isInvalidate() const { return cmd.isInvalidate(); }
306 bool hasData() const { return cmd.hasData(); }
307 bool isReadWrite() const { return cmd.isReadWrite(); }
309 bool isCacheFill() const { return (flags & CACHE_LINE_FILL) != 0; }
310 bool isNoAllocate() const { return (flags & NO_ALLOCATE) != 0; }
311 bool isCompressed() const { return (flags & COMPRESSED) != 0; }
313 bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
315 /** Possible results of a packet's request. */
324 /** The result of this packet's request. */
327 /** Accessor function that returns the source index of the packet. */
328 short getSrc() const { assert(srcValid); return src; }
329 void setSrc(short _src) { src = _src; srcValid = true; }
331 /** Accessor function that returns the destination index of
333 short getDest() const { return dest; }
334 void setDest(short _dest) { dest = _dest; }
336 Addr getAddr() const { assert(addrSizeValid); return addr; }
337 int getSize() const { assert(addrSizeValid); return size; }
338 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
340 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
341 void cmdOverride(MemCmd newCmd) { cmd = newCmd; }
343 /** Constructor. Note that a Request object must be constructed
344 * first, but the Requests's physical address and size fields
345 * need not be valid. The command and destination addresses
346 * must be supplied. */
347 Packet(Request *_req, MemCmd _cmd, short _dest)
348 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
349 addr(_req->paddr), size(_req->size), dest(_dest),
350 addrSizeValid(_req->validPaddr),
352 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
359 /** Alternate constructor if you are trying to create a packet with
360 * a request that is for a whole block, not the address from the req.
361 * this allows for overriding the size/addr of the req.*/
362 Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
363 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
364 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
366 addrSizeValid(_req->validPaddr), srcValid(false),
367 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
376 { if (staticData || dynamicData) deleteData(); }
378 /** Reinitialize packet address and size from the associated
379 * Request object, and reset other fields that may have been
380 * modified by a previous transaction. Typically called when a
381 * statically allocated Request/Packet pair is reused for
382 * multiple transactions. */
383 void reinitFromRequest() {
384 assert(req->validPaddr);
389 addrSizeValid = true;
398 /** Take a request packet and modify it in place to be suitable
399 * for returning as a response to that request. Used for timing
400 * accesses only. For atomic and functional accesses, the
401 * request packet is always implicitly passed back *without*
402 * modifying the destination fields, so this function
403 * should not be called. */
404 void makeTimingResponse() {
405 assert(needsResponse());
407 cmd = cmd.responseCommand();
413 * Take a request packet and modify it in place to be suitable for
414 * returning as a response to that request.
416 void makeAtomicResponse()
418 assert(needsResponse());
420 cmd = cmd.responseCommand();
424 * Take a request packet that has been returned as NACKED and
425 * modify it so that it can be sent out again. Only packets that
426 * need a response can be NACKED, so verify that that is true.
431 assert(needsResponse() && result == Nacked);
438 * Set the data pointer to the following value that should not be
441 template <typename T>
447 data = (PacketDataPtr)p;
452 * Set the data pointer to a value that should have delete []
455 template <typename T>
457 dataDynamicArray(T *p)
459 assert(!staticData && !dynamicData);
460 data = (PacketDataPtr)p;
466 * set the data pointer to a value that should have delete called
469 template <typename T>
473 assert(!staticData && !dynamicData);
474 data = (PacketDataPtr)p;
479 /** get a pointer to the data ptr. */
480 template <typename T>
484 assert(staticData || dynamicData);
488 /** return the value of what is pointed to in the packet. */
489 template <typename T>
492 /** set the value in the data pointer to v. */
493 template <typename T>
497 * delete the data pointed to in the data pointer. Ok to call to
498 * matter how data was allocted.
502 /** If there isn't data in the packet, allocate some. */
505 /** Do the packet modify the same addresses. */
506 bool intersect(PacketPtr p);
509 /** This function given a functional packet and a timing packet either satisfies
510 * the timing packet, or updates the timing packet to reflect the updated state
511 * in the timing packet. It returns if the functional packet should continue to
512 * traverse the memory hierarchy or not.
514 bool fixPacket(PacketPtr func, PacketPtr timing);
516 /** This function is a wrapper for the fixPacket field that toggles the hasData bit
517 * it is used when a response is waiting in the caches, but hasn't been marked as a
518 * response yet (so the fixPacket needs to get the correct value for the hasData)
520 bool fixDelayedResponsePacket(PacketPtr func, PacketPtr timing);
522 std::ostream & operator<<(std::ostream &o, const Packet &p);
524 #endif //__MEM_PACKET_HH