426f1442126b509ca1048812b32f5f13fc2638eb
[gem5.git] / src / mem / packet.hh
1 /*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Ron Dreslinski
29 * Steve Reinhardt
30 * Ali Saidi
31 */
32
33 /**
34 * @file
35 * Declaration of the Packet class.
36 */
37
38 #ifndef __MEM_PACKET_HH__
39 #define __MEM_PACKET_HH__
40
41 #include "mem/request.hh"
42 #include "sim/host.hh"
43 #include "sim/root.hh"
44 #include <list>
45 #include <cassert>
46
47 struct Packet;
48 typedef Packet* PacketPtr;
49 typedef uint8_t* PacketDataPtr;
50 typedef std::list<PacketPtr> PacketList;
51
52 //Coherence Flags
53 #define NACKED_LINE 1 << 0
54 #define SATISFIED 1 << 1
55 #define SHARED_LINE 1 << 2
56 #define CACHE_LINE_FILL 1 << 3
57 #define COMPRESSED 1 << 4
58 #define NO_ALLOCATE 1 << 5
59 #define SNOOP_COMMIT 1 << 6
60
61 //for now. @todo fix later
62 #define NUM_MEM_CMDS 1 << 11
63 /**
64 * A Packet is used to encapsulate a transfer between two objects in
65 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
66 * single Request travels all the way from the requester to the
67 * ultimate destination and back, possibly being conveyed by several
68 * different Packets along the way.)
69 */
70 class Packet
71 {
72 public:
73 /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
74 uint64_t flags;
75
76 private:
77 /** A pointer to the data being transfered. It can be differnt
78 * sizes at each level of the heirarchy so it belongs in the
79 * packet, not request. This may or may not be populated when a
80 * responder recieves the packet. If not populated it memory
81 * should be allocated.
82 */
83 PacketDataPtr data;
84
85 /** Is the data pointer set to a value that shouldn't be freed
86 * when the packet is destroyed? */
87 bool staticData;
88 /** The data pointer points to a value that should be freed when
89 * the packet is destroyed. */
90 bool dynamicData;
91 /** the data pointer points to an array (thus delete [] ) needs to
92 * be called on it rather than simply delete.*/
93 bool arrayData;
94
95
96 /** The address of the request. This address could be virtual or
97 * physical, depending on the system configuration. */
98 Addr addr;
99
100 /** The size of the request or transfer. */
101 int size;
102
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. */
107 short src;
108
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). */
114 short dest;
115
116 /** Are the 'addr' and 'size' fields valid? */
117 bool addrSizeValid;
118 /** Is the 'src' field valid? */
119 bool srcValid;
120
121
122 public:
123
124 /** Used to calculate latencies for each packet.*/
125 Tick time;
126
127 /** The special destination address indicating that the packet
128 * should be routed based on its address. */
129 static const short Broadcast = -1;
130
131 /** A pointer to the original request. */
132 RequestPtr req;
133
134 /** A virtual base opaque structure used to hold coherence-related
135 * state. A specific subclass would be derived from this to
136 * carry state specific to a particular coherence protocol. */
137 class CoherenceState {
138 public:
139 virtual ~CoherenceState() {}
140 };
141
142 /** This packet's coherence state. Caches should use
143 * dynamic_cast<> to cast to the state appropriate for the
144 * system's coherence protocol. */
145 CoherenceState *coherence;
146
147 /** A virtual base opaque structure used to hold state associated
148 * with the packet but specific to the sending device (e.g., an
149 * MSHR). A pointer to this state is returned in the packet's
150 * response so that the sender can quickly look up the state
151 * needed to process it. A specific subclass would be derived
152 * from this to carry state specific to a particular sending
153 * device. */
154 class SenderState {
155 public:
156 virtual ~SenderState() {}
157 };
158
159 /** This packet's sender state. Devices should use dynamic_cast<>
160 * to cast to the state appropriate to the sender. */
161 SenderState *senderState;
162
163 private:
164 /** List of command attributes. */
165 // If you add a new CommandAttribute, make sure to increase NUM_MEM_CMDS
166 // as well.
167 enum CommandAttribute
168 {
169 IsRead = 1 << 0,
170 IsWrite = 1 << 1,
171 IsPrefetch = 1 << 2,
172 IsInvalidate = 1 << 3,
173 IsRequest = 1 << 4,
174 IsResponse = 1 << 5,
175 NeedsResponse = 1 << 6,
176 IsSWPrefetch = 1 << 7,
177 IsHWPrefetch = 1 << 8,
178 IsUpgrade = 1 << 9,
179 HasData = 1 << 10
180 };
181 //For statistics we need max number of commands, hard code it at
182 //20 for now. @todo fix later
183 #define NUM_MEM_CMDS 1 << 10
184
185 public:
186 /** List of all commands associated with a packet. */
187 enum Command
188 {
189 InvalidCmd = 0,
190 ReadReq = IsRead | IsRequest | NeedsResponse,
191 WriteReq = IsWrite | IsRequest | NeedsResponse | HasData,
192 WriteReqNoAck = IsWrite | IsRequest | HasData,
193 ReadResp = IsRead | IsResponse | NeedsResponse | HasData,
194 WriteResp = IsWrite | IsResponse | NeedsResponse,
195 Writeback = IsWrite | IsRequest | HasData,
196 SoftPFReq = IsRead | IsRequest | IsSWPrefetch | NeedsResponse,
197 HardPFReq = IsRead | IsRequest | IsHWPrefetch | NeedsResponse,
198 SoftPFResp = IsRead | IsResponse | IsSWPrefetch
199 | NeedsResponse | HasData,
200 HardPFResp = IsRead | IsResponse | IsHWPrefetch
201 | NeedsResponse | HasData,
202 InvalidateReq = IsInvalidate | IsRequest,
203 WriteInvalidateReq = IsWrite | IsInvalidate | IsRequest | HasData,
204 UpgradeReq = IsInvalidate | IsRequest | IsUpgrade,
205 ReadExReq = IsRead | IsInvalidate | IsRequest | NeedsResponse,
206 ReadExResp = IsRead | IsInvalidate | IsResponse
207 | NeedsResponse | HasData
208 };
209
210 /** Return the string name of the cmd field (for debugging and
211 * tracing). */
212 const std::string &cmdString() const;
213
214 /** Reutrn the string to a cmd given by idx. */
215 const std::string &cmdIdxToString(Command idx);
216
217 /** Return the index of this command. */
218 inline int cmdToIndex() const { return (int) cmd; }
219
220 /** The command field of the packet. */
221 Command cmd;
222
223 bool isRead() { return (cmd & IsRead) != 0; }
224 bool isWrite() { return (cmd & IsWrite) != 0; }
225 bool isRequest() { return (cmd & IsRequest) != 0; }
226 bool isResponse() { return (cmd & IsResponse) != 0; }
227 bool needsResponse() { return (cmd & NeedsResponse) != 0; }
228 bool isInvalidate() { return (cmd & IsInvalidate) != 0; }
229 bool hasData() { return (cmd & HasData) != 0; }
230
231 bool isCacheFill() { return (flags & CACHE_LINE_FILL) != 0; }
232 bool isNoAllocate() { return (flags & NO_ALLOCATE) != 0; }
233 bool isCompressed() { return (flags & COMPRESSED) != 0; }
234
235 bool nic_pkt() { assert("Unimplemented\n" && 0); return false; }
236
237 /** Possible results of a packet's request. */
238 enum Result
239 {
240 Success,
241 BadAddress,
242 Nacked,
243 Unknown
244 };
245
246 /** The result of this packet's request. */
247 Result result;
248
249 /** Accessor function that returns the source index of the packet. */
250 short getSrc() const { assert(srcValid); return src; }
251 void setSrc(short _src) { src = _src; srcValid = true; }
252
253 /** Accessor function that returns the destination index of
254 the packet. */
255 short getDest() const { return dest; }
256 void setDest(short _dest) { dest = _dest; }
257
258 Addr getAddr() const { assert(addrSizeValid); return addr; }
259 int getSize() const { assert(addrSizeValid); return size; }
260 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
261
262 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
263 void cmdOverride(Command newCmd) { cmd = newCmd; }
264
265 /** Constructor. Note that a Request object must be constructed
266 * first, but the Requests's physical address and size fields
267 * need not be valid. The command and destination addresses
268 * must be supplied. */
269 Packet(Request *_req, Command _cmd, short _dest)
270 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
271 addr(_req->paddr), size(_req->size), dest(_dest),
272 addrSizeValid(_req->validPaddr),
273 srcValid(false),
274 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
275 result(Unknown)
276 {
277 flags = 0;
278 time = curTick;
279 }
280
281 /** Alternate constructor if you are trying to create a packet with
282 * a request that is for a whole block, not the address from the req.
283 * this allows for overriding the size/addr of the req.*/
284 Packet(Request *_req, Command _cmd, short _dest, int _blkSize)
285 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
286 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
287 dest(_dest),
288 addrSizeValid(_req->validPaddr), srcValid(false),
289 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
290 result(Unknown)
291 {
292 flags = 0;
293 time = curTick;
294 }
295
296 /** Destructor. */
297 ~Packet()
298 { deleteData(); }
299
300 /** Reinitialize packet address and size from the associated
301 * Request object, and reset other fields that may have been
302 * modified by a previous transaction. Typically called when a
303 * statically allocated Request/Packet pair is reused for
304 * multiple transactions. */
305 void reinitFromRequest() {
306 assert(req->validPaddr);
307 addr = req->paddr;
308 size = req->size;
309 time = req->time;
310 addrSizeValid = true;
311 result = Unknown;
312 if (dynamicData) {
313 deleteData();
314 dynamicData = false;
315 arrayData = false;
316 }
317 }
318
319 /** Take a request packet and modify it in place to be suitable
320 * for returning as a response to that request. Used for timing
321 * accesses only. For atomic and functional accesses, the
322 * request packet is always implicitly passed back *without*
323 * modifying the destination fields, so this function
324 * should not be called. */
325 void makeTimingResponse() {
326 assert(needsResponse());
327 assert(isRequest());
328 int icmd = (int)cmd;
329 icmd &= ~(IsRequest);
330 icmd |= IsResponse;
331 cmd = (Command)icmd;
332 dest = src;
333 srcValid = false;
334 }
335
336 /** Take a request packet and modify it in place to be suitable
337 * for returning as a response to that request.
338 */
339 void makeAtomicResponse() {
340 assert(needsResponse());
341 assert(isRequest());
342 int icmd = (int)cmd;
343 icmd &= ~(IsRequest);
344 icmd |= IsResponse;
345 cmd = (Command)icmd;
346 }
347
348 /** Take a request packet that has been returned as NACKED and modify it so
349 * that it can be sent out again. Only packets that need a response can be
350 * NACKED, so verify that that is true. */
351 void reinitNacked() {
352 assert(needsResponse() && result == Nacked);
353 dest = Broadcast;
354 result = Unknown;
355 }
356
357
358 /** Set the data pointer to the following value that should not be freed. */
359 template <typename T>
360 void dataStatic(T *p);
361
362 /** Set the data pointer to a value that should have delete [] called on it.
363 */
364 template <typename T>
365 void dataDynamicArray(T *p);
366
367 /** set the data pointer to a value that should have delete called on it. */
368 template <typename T>
369 void dataDynamic(T *p);
370
371 /** return the value of what is pointed to in the packet. */
372 template <typename T>
373 T get();
374
375 /** get a pointer to the data ptr. */
376 template <typename T>
377 T* getPtr();
378
379 /** set the value in the data pointer to v. */
380 template <typename T>
381 void set(T v);
382
383 /** delete the data pointed to in the data pointer. Ok to call to matter how
384 * data was allocted. */
385 void deleteData();
386
387 /** If there isn't data in the packet, allocate some. */
388 void allocate();
389
390 /** Do the packet modify the same addresses. */
391 bool intersect(Packet *p);
392 };
393
394 bool fixPacket(Packet *func, Packet *timing);
395 #endif //__MEM_PACKET_HH