More Changes, working towards cache.cc compiling. Headers cleaned up.
[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 "arch/isa_traits.hh"
43 #include "sim/root.hh"
44 #include <list>
45
46 struct Packet;
47 typedef Packet* PacketPtr;
48 typedef uint8_t* PacketDataPtr;
49 typedef std::list<PacketPtr> PacketList;
50
51 //Coherence Flags
52 #define NACKED_LINE 1 << 0
53 #define SATISFIED 1 << 1
54 #define SHARED_LINE 1 << 2
55
56 //For statistics we need max number of commands, hard code it at
57 //20 for now. @todo fix later
58 #define NUM_MEM_CMDS 1 << 9
59
60 /**
61 * A Packet is used to encapsulate a transfer between two objects in
62 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
63 * single Request travels all the way from the requester to the
64 * ultimate destination and back, possibly being conveyed by several
65 * different Packets along the way.)
66 */
67 class Packet
68 {
69 private:
70 /** A pointer to the data being transfered. It can be differnt
71 * sizes at each level of the heirarchy so it belongs in the
72 * packet, not request. This may or may not be populated when a
73 * responder recieves the packet. If not populated it memory
74 * should be allocated.
75 */
76 PacketDataPtr data;
77
78 /** Is the data pointer set to a value that shouldn't be freed
79 * when the packet is destroyed? */
80 bool staticData;
81 /** The data pointer points to a value that should be freed when
82 * the packet is destroyed. */
83 bool dynamicData;
84 /** the data pointer points to an array (thus delete [] ) needs to
85 * be called on it rather than simply delete.*/
86 bool arrayData;
87
88
89 /** The address of the request. This address could be virtual or
90 * physical, depending on the system configuration. */
91 Addr addr;
92
93 /** The size of the request or transfer. */
94 int size;
95
96 /** Device address (e.g., bus ID) of the source of the
97 * transaction. The source is not responsible for setting this
98 * field; it is set implicitly by the interconnect when the
99 * packet * is first sent. */
100 short src;
101
102 /** Device address (e.g., bus ID) of the destination of the
103 * transaction. The special value Broadcast indicates that the
104 * packet should be routed based on its address. This field is
105 * initialized in the constructor and is thus always valid
106 * (unlike * addr, size, and src). */
107 short dest;
108
109 /** Are the 'addr' and 'size' fields valid? */
110 bool addrSizeValid;
111 /** Is the 'src' field valid? */
112 bool srcValid;
113
114 public:
115
116 /** Used to calculate latencies for each packet.*/
117 Tick time;
118
119 /** The special destination address indicating that the packet
120 * should be routed based on its address. */
121 static const short Broadcast = -1;
122
123 /** A pointer to the original request. */
124 RequestPtr req;
125
126 /** A virtual base opaque structure used to hold coherence-related
127 * state. A specific subclass would be derived from this to
128 * carry state specific to a particular coherence protocol. */
129 class CoherenceState {
130 public:
131 virtual ~CoherenceState() {}
132 };
133
134 /** This packet's coherence state. Caches should use
135 * dynamic_cast<> to cast to the state appropriate for the
136 * system's coherence protocol. */
137 CoherenceState *coherence;
138
139 /** A virtual base opaque structure used to hold state associated
140 * with the packet but specific to the sending device (e.g., an
141 * MSHR). A pointer to this state is returned in the packet's
142 * response so that the sender can quickly look up the state
143 * needed to process it. A specific subclass would be derived
144 * from this to carry state specific to a particular sending
145 * device. */
146 class SenderState {
147 public:
148 virtual ~SenderState() {}
149 };
150
151 /** This packet's sender state. Devices should use dynamic_cast<>
152 * to cast to the state appropriate to the sender. */
153 SenderState *senderState;
154
155 private:
156 /** List of command attributes. */
157 enum CommandAttribute
158 {
159 IsRead = 1 << 0,
160 IsWrite = 1 << 1,
161 IsPrefetch = 1 << 2,
162 IsInvalidate = 1 << 3,
163 IsRequest = 1 << 4,
164 IsResponse = 1 << 5,
165 NeedsResponse = 1 << 6,
166 IsSWPrefetch = 1 << 7,
167 IsHWPrefetch = 1 << 8
168 };
169
170 public:
171 /** List of all commands associated with a packet. */
172 enum Command
173 {
174 ReadReq = IsRead | IsRequest | NeedsResponse,
175 WriteReq = IsWrite | IsRequest | NeedsResponse,
176 WriteReqNoAck = IsWrite | IsRequest,
177 ReadResp = IsRead | IsResponse,
178 WriteResp = IsWrite | IsResponse,
179 Writeback = IsWrite | IsRequest,
180 SoftPFReq = IsRead | IsRequest | IsSWPrefetch | NeedsResponse,
181 HardPFReq = IsRead | IsRequest | IsHWPrefetch | NeedsResponse,
182 SoftPFResp = IsRead | IsRequest | IsSWPrefetch | IsResponse,
183 HardPFResp = IsRead | IsRequest | IsHWPrefetch | IsResponse,
184 InvalidateReq = IsInvalidate | IsRequest,
185 WriteInvalidateReq = IsWrite | IsInvalidate | IsRequest,
186 UpgradeReq = IsInvalidate | NeedsResponse
187 };
188
189 /** Return the string name of the cmd field (for debugging and
190 * tracing). */
191 const std::string &cmdString() const;
192
193 /** Reutrn the string to a cmd given by idx. */
194 const std::string &cmdIdxToString(Command idx);
195
196 /** Return the index of this command. */
197 inline int cmdToIndex() const { return (int) cmd; }
198
199 /** The command field of the packet. */
200 Command cmd;
201
202 bool isRead() { return (cmd & IsRead) != 0; }
203 bool isWrite() { return (cmd & IsWrite) != 0; }
204 bool isRequest() { return (cmd & IsRequest) != 0; }
205 bool isResponse() { return (cmd & IsResponse) != 0; }
206 bool needsResponse() { return (cmd & NeedsResponse) != 0; }
207 bool isInvalidate() { return (cmd * IsInvalidate) != 0; }
208
209 bool isCacheFill() { assert("Unimplemented yet\n" && 0); }
210 bool isNoAllocate() { assert("Unimplemented yet\n" && 0); }
211
212 /** Possible results of a packet's request. */
213 enum Result
214 {
215 Success,
216 BadAddress,
217 Nacked,
218 Unknown
219 };
220
221 /** The result of this packet's request. */
222 Result result;
223
224 /** Accessor function that returns the source index of the packet. */
225 short getSrc() const { assert(srcValid); return src; }
226 void setSrc(short _src) { src = _src; srcValid = true; }
227
228 /** Accessor function that returns the destination index of
229 the packet. */
230 short getDest() const { return dest; }
231 void setDest(short _dest) { dest = _dest; }
232
233 Addr getAddr() const { assert(addrSizeValid); return addr; }
234 int getSize() const { assert(addrSizeValid); return size; }
235
236 /** Constructor. Note that a Request object must be constructed
237 * first, but the Requests's physical address and size fields
238 * need not be valid. The command and destination addresses
239 * must be supplied. */
240 Packet(Request *_req, Command _cmd, short _dest)
241 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
242 addr(_req->paddr), size(_req->size), dest(_dest),
243 addrSizeValid(_req->validPaddr),
244 srcValid(false),
245 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
246 result(Unknown)
247 {
248 }
249
250 /** Destructor. */
251 ~Packet()
252 { deleteData(); }
253
254 /** Reinitialize packet address and size from the associated
255 * Request object, and reset other fields that may have been
256 * modified by a previous transaction. Typically called when a
257 * statically allocated Request/Packet pair is reused for
258 * multiple transactions. */
259 void reinitFromRequest() {
260 assert(req->validPaddr);
261 addr = req->paddr;
262 size = req->size;
263 addrSizeValid = true;
264 result = Unknown;
265 if (dynamicData) {
266 deleteData();
267 dynamicData = false;
268 arrayData = false;
269 }
270 }
271
272 /** Take a request packet and modify it in place to be suitable
273 * for returning as a response to that request. Used for timing
274 * accesses only. For atomic and functional accesses, the
275 * request packet is always implicitly passed back *without*
276 * modifying the command or destination fields, so this function
277 * should not be called. */
278 void makeTimingResponse() {
279 assert(needsResponse());
280 int icmd = (int)cmd;
281 icmd &= ~(IsRequest | NeedsResponse);
282 icmd |= IsResponse;
283 cmd = (Command)icmd;
284 dest = src;
285 srcValid = false;
286 }
287
288 /** Take a request packet that has been returned as NACKED and modify it so
289 * that it can be sent out again. Only packets that need a response can be
290 * NACKED, so verify that that is true. */
291 void reinitNacked() {
292 assert(needsResponse() && result == Nacked);
293 dest = Broadcast;
294 result = Unknown;
295 }
296
297
298 /** Set the data pointer to the following value that should not be freed. */
299 template <typename T>
300 void dataStatic(T *p);
301
302 /** Set the data pointer to a value that should have delete [] called on it.
303 */
304 template <typename T>
305 void dataDynamicArray(T *p);
306
307 /** set the data pointer to a value that should have delete called on it. */
308 template <typename T>
309 void dataDynamic(T *p);
310
311 /** return the value of what is pointed to in the packet. */
312 template <typename T>
313 T get();
314
315 /** get a pointer to the data ptr. */
316 template <typename T>
317 T* getPtr();
318
319 /** set the value in the data pointer to v. */
320 template <typename T>
321 void set(T v);
322
323 /** delete the data pointed to in the data pointer. Ok to call to matter how
324 * data was allocted. */
325 void deleteData();
326
327 /** If there isn't data in the packet, allocate some. */
328 void allocate();
329
330 /** Do the packet modify the same addresses. */
331 bool intersect(Packet *p);
332 };
333
334 bool fixPacket(Packet *func, Packet *timing);
335 #endif //__MEM_PACKET_HH