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__
44 #include "mem/request.hh"
45 #include "sim/host.hh"
46 #include "sim/root.hh"
49 typedef Packet *PacketPtr;
50 typedef uint8_t* PacketDataPtr;
51 typedef std::list<PacketPtr> PacketList;
54 #define NACKED_LINE (1 << 0)
55 #define SATISFIED (1 << 1)
56 #define SHARED_LINE (1 << 2)
57 #define CACHE_LINE_FILL (1 << 3)
58 #define COMPRESSED (1 << 4)
59 #define NO_ALLOCATE (1 << 5)
60 #define SNOOP_COMMIT (1 << 6)
62 //for now. @todo fix later
63 #define NUM_MEM_CMDS (1 << 11)
65 * A Packet is used to encapsulate a transfer between two objects in
66 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
67 * single Request travels all the way from the requester to the
68 * ultimate destination and back, possibly being conveyed by several
69 * different Packets along the way.)
74 /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
78 /** A pointer to the data being transfered. It can be differnt
79 * sizes at each level of the heirarchy so it belongs in the
80 * packet, not request. This may or may not be populated when a
81 * responder recieves the packet. If not populated it memory
82 * should be allocated.
86 /** Is the data pointer set to a value that shouldn't be freed
87 * when the packet is destroyed? */
89 /** The data pointer points to a value that should be freed when
90 * the packet is destroyed. */
92 /** the data pointer points to an array (thus delete [] ) needs to
93 * be called on it rather than simply delete.*/
96 /** The address of the request. This address could be virtual or
97 * physical, depending on the system configuration. */
100 /** The size of the request or transfer. */
103 /** Device address (e.g., bus ID) of the source of the
104 * transaction. The source is not responsible for setting this
105 * field; it is set implicitly by the interconnect when the
106 * packet is first sent. */
109 /** Device address (e.g., bus ID) of the destination of the
110 * transaction. The special value Broadcast indicates that the
111 * packet should be routed based on its address. This field is
112 * initialized in the constructor and is thus always valid
113 * (unlike * addr, size, and src). */
116 /** Are the 'addr' and 'size' fields valid? */
118 /** Is the 'src' field valid? */
124 /** Used to calculate latencies for each packet.*/
127 /** The time at which the packet will be fully transmitted */
130 /** The time at which the first chunk of the packet will be transmitted */
133 /** The special destination address indicating that the packet
134 * should be routed based on its address. */
135 static const short Broadcast = -1;
137 /** A pointer to the original request. */
140 /** A virtual base opaque structure used to hold coherence-related
141 * state. A specific subclass would be derived from this to
142 * carry state specific to a particular coherence protocol. */
143 class CoherenceState {
145 virtual ~CoherenceState() {}
148 /** This packet's coherence state. Caches should use
149 * dynamic_cast<> to cast to the state appropriate for the
150 * system's coherence protocol. */
151 CoherenceState *coherence;
153 /** A virtual base opaque structure used to hold state associated
154 * with the packet but specific to the sending device (e.g., an
155 * MSHR). A pointer to this state is returned in the packet's
156 * response so that the sender can quickly look up the state
157 * needed to process it. A specific subclass would be derived
158 * from this to carry state specific to a particular sending
162 virtual ~SenderState() {}
165 /** This packet's sender state. Devices should use dynamic_cast<>
166 * to cast to the state appropriate to the sender. */
167 SenderState *senderState;
170 /** List of command attributes. */
171 // If you add a new CommandAttribute, make sure to increase NUM_MEM_CMDS
173 enum CommandAttribute
178 IsInvalidate = 1 << 3,
181 NeedsResponse = 1 << 6,
182 IsSWPrefetch = 1 << 7,
183 IsHWPrefetch = 1 << 8,
189 /** List of all commands associated with a packet. */
193 ReadReq = IsRead | IsRequest | NeedsResponse,
194 WriteReq = IsWrite | IsRequest | NeedsResponse | HasData,
195 WriteReqNoAck = IsWrite | IsRequest | HasData,
196 ReadResp = IsRead | IsResponse | NeedsResponse | HasData,
197 WriteResp = IsWrite | IsResponse | NeedsResponse,
198 Writeback = IsWrite | IsRequest | HasData,
199 SoftPFReq = IsRead | IsRequest | IsSWPrefetch | NeedsResponse,
200 HardPFReq = IsRead | IsRequest | IsHWPrefetch | NeedsResponse,
201 SoftPFResp = IsRead | IsResponse | IsSWPrefetch
202 | NeedsResponse | HasData,
203 HardPFResp = IsRead | IsResponse | IsHWPrefetch
204 | NeedsResponse | HasData,
205 InvalidateReq = IsInvalidate | IsRequest,
206 WriteInvalidateReq = IsWrite | IsInvalidate | IsRequest
207 | HasData | NeedsResponse,
208 WriteInvalidateResp = IsWrite | IsInvalidate | IsRequest
209 | NeedsResponse | IsResponse,
210 UpgradeReq = IsInvalidate | IsRequest | IsUpgrade,
211 ReadExReq = IsRead | IsInvalidate | IsRequest | NeedsResponse,
212 ReadExResp = IsRead | IsInvalidate | IsResponse
213 | NeedsResponse | HasData
216 /** Return the string name of the cmd field (for debugging and
218 const std::string &cmdString() const;
220 /** Reutrn the string to a cmd given by idx. */
221 const std::string &cmdIdxToString(Command idx);
223 /** Return the index of this command. */
224 inline int cmdToIndex() const { return (int) cmd; }
226 /** The command field of the packet. */
229 bool isRead() const { return (cmd & IsRead) != 0; }
230 bool isWrite() const { return (cmd & IsWrite) != 0; }
231 bool isRequest() const { return (cmd & IsRequest) != 0; }
232 bool isResponse() const { return (cmd & IsResponse) != 0; }
233 bool needsResponse() const { return (cmd & NeedsResponse) != 0; }
234 bool isInvalidate() const { return (cmd & IsInvalidate) != 0; }
235 bool hasData() const { return (cmd & HasData) != 0; }
237 bool isCacheFill() const { return (flags & CACHE_LINE_FILL) != 0; }
238 bool isNoAllocate() const { return (flags & NO_ALLOCATE) != 0; }
239 bool isCompressed() const { return (flags & COMPRESSED) != 0; }
241 bool nic_pkt() { assert("Unimplemented\n" && 0); return false; }
243 /** Possible results of a packet's request. */
252 /** The result of this packet's request. */
255 /** Accessor function that returns the source index of the packet. */
256 short getSrc() const { assert(srcValid); return src; }
257 void setSrc(short _src) { src = _src; srcValid = true; }
259 /** Accessor function that returns the destination index of
261 short getDest() const { return dest; }
262 void setDest(short _dest) { dest = _dest; }
264 Addr getAddr() const { assert(addrSizeValid); return addr; }
265 int getSize() const { assert(addrSizeValid); return size; }
266 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
268 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
269 void cmdOverride(Command newCmd) { cmd = newCmd; }
271 /** Constructor. Note that a Request object must be constructed
272 * first, but the Requests's physical address and size fields
273 * need not be valid. The command and destination addresses
274 * must be supplied. */
275 Packet(Request *_req, Command _cmd, short _dest)
276 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
277 addr(_req->paddr), size(_req->size), dest(_dest),
278 addrSizeValid(_req->validPaddr),
280 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
287 /** Alternate constructor if you are trying to create a packet with
288 * a request that is for a whole block, not the address from the req.
289 * this allows for overriding the size/addr of the req.*/
290 Packet(Request *_req, Command _cmd, short _dest, int _blkSize)
291 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
292 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
294 addrSizeValid(_req->validPaddr), srcValid(false),
295 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
304 { if (staticData || dynamicData) deleteData(); }
306 /** Reinitialize packet address and size from the associated
307 * Request object, and reset other fields that may have been
308 * modified by a previous transaction. Typically called when a
309 * statically allocated Request/Packet pair is reused for
310 * multiple transactions. */
311 void reinitFromRequest() {
312 assert(req->validPaddr);
317 addrSizeValid = true;
326 /** Take a request packet and modify it in place to be suitable
327 * for returning as a response to that request. Used for timing
328 * accesses only. For atomic and functional accesses, the
329 * request packet is always implicitly passed back *without*
330 * modifying the destination fields, so this function
331 * should not be called. */
332 void makeTimingResponse() {
333 assert(needsResponse());
336 icmd &= ~(IsRequest);
355 * Take a request packet and modify it in place to be suitable for
356 * returning as a response to that request.
358 void makeAtomicResponse()
360 assert(needsResponse());
363 icmd &= ~(IsRequest);
373 * Take a request packet that has been returned as NACKED and
374 * modify it so that it can be sent out again. Only packets that
375 * need a response can be NACKED, so verify that that is true.
380 assert(needsResponse() && result == Nacked);
387 * Set the data pointer to the following value that should not be
390 template <typename T>
396 data = (PacketDataPtr)p;
401 * Set the data pointer to a value that should have delete []
404 template <typename T>
406 dataDynamicArray(T *p)
408 assert(!staticData && !dynamicData);
409 data = (PacketDataPtr)p;
415 * set the data pointer to a value that should have delete called
418 template <typename T>
422 assert(!staticData && !dynamicData);
423 data = (PacketDataPtr)p;
428 /** get a pointer to the data ptr. */
429 template <typename T>
433 assert(staticData || dynamicData);
437 /** return the value of what is pointed to in the packet. */
438 template <typename T>
441 /** set the value in the data pointer to v. */
442 template <typename T>
446 * delete the data pointed to in the data pointer. Ok to call to
447 * matter how data was allocted.
451 /** If there isn't data in the packet, allocate some. */
454 /** Do the packet modify the same addresses. */
455 bool intersect(PacketPtr p);
458 /** This function given a functional packet and a timing packet either satisfies
459 * the timing packet, or updates the timing packet to reflect the updated state
460 * in the timing packet. It returns if the functional packet should continue to
461 * traverse the memory hierarchy or not.
463 bool fixPacket(PacketPtr func, PacketPtr timing);
465 /** This function is a wrapper for the fixPacket field that toggles the hasData bit
466 * it is used when a response is waiting in the caches, but hasn't been marked as a
467 * response yet (so the fixPacket needs to get the correct value for the hasData)
469 bool fixDelayedResponsePacket(PacketPtr func, PacketPtr timing);
471 std::ostream & operator<<(std::ostream &o, const Packet &p);
473 #endif //__MEM_PACKET_HH