packet.hh revision 3349
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 <cassert> 42#include <list> 43 44#include "mem/request.hh" 45#include "sim/host.hh" 46#include "sim/root.hh" 47 48struct Packet; 49typedef Packet *PacketPtr; 50typedef uint8_t* PacketDataPtr; 51typedef std::list<PacketPtr> PacketList; 52 53//Coherence Flags 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 61 62//for now. @todo fix later 63#define NUM_MEM_CMDS 1 << 11 64/** 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.) 70 */ 71class Packet 72{ 73 public: 74 /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */ 75 uint64_t flags; 76 77 private: 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. 83 */ 84 PacketDataPtr data; 85 86 /** Is the data pointer set to a value that shouldn't be freed 87 * when the packet is destroyed? */ 88 bool staticData; 89 /** The data pointer points to a value that should be freed when 90 * the packet is destroyed. */ 91 bool dynamicData; 92 /** the data pointer points to an array (thus delete [] ) needs to 93 * be called on it rather than simply delete.*/ 94 bool arrayData; 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 time at which the packet will be fully transmitted */ 128 Tick finishTime; 129 130 /** The time at which the first chunk of the packet will be transmitted */ 131 Tick firstWordTime; 132 133 /** The special destination address indicating that the packet 134 * should be routed based on its address. */ 135 static const short Broadcast = -1; 136 137 /** A pointer to the original request. */ 138 RequestPtr req; 139 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 { 144 public: 145 virtual ~CoherenceState() {} 146 }; 147 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; 152 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 159 * device. */ 160 class SenderState { 161 public: 162 virtual ~SenderState() {} 163 }; 164 165 /** This packet's sender state. Devices should use dynamic_cast<> 166 * to cast to the state appropriate to the sender. */ 167 SenderState *senderState; 168 169 private: 170 /** List of command attributes. */ 171 // If you add a new CommandAttribute, make sure to increase NUM_MEM_CMDS 172 // as well. 173 enum CommandAttribute 174 { 175 IsRead = 1 << 0, 176 IsWrite = 1 << 1, 177 IsPrefetch = 1 << 2, 178 IsInvalidate = 1 << 3, 179 IsRequest = 1 << 4, 180 IsResponse = 1 << 5, 181 NeedsResponse = 1 << 6, 182 IsSWPrefetch = 1 << 7, 183 IsHWPrefetch = 1 << 8, 184 IsUpgrade = 1 << 9, 185 HasData = 1 << 10 186 }; 187 188 public: 189 /** List of all commands associated with a packet. */ 190 enum Command 191 { 192 InvalidCmd = 0, 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 | NeedsResponse 209 | IsResponse, 210 UpgradeReq = IsInvalidate | IsRequest | IsUpgrade, 211 ReadExReq = IsRead | IsInvalidate | IsRequest | NeedsResponse, 212 ReadExResp = IsRead | IsInvalidate | IsResponse 213 | NeedsResponse | HasData 214 }; 215 216 /** Return the string name of the cmd field (for debugging and 217 * tracing). */ 218 const std::string &cmdString() const; 219 220 /** Reutrn the string to a cmd given by idx. */ 221 const std::string &cmdIdxToString(Command idx); 222 223 /** Return the index of this command. */ 224 inline int cmdToIndex() const { return (int) cmd; } 225 226 /** The command field of the packet. */ 227 Command cmd; 228 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; } 236 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; } 240 241 bool nic_pkt() { assert("Unimplemented\n" && 0); return false; } 242 243 /** Possible results of a packet's request. */ 244 enum Result 245 { 246 Success, 247 BadAddress, 248 Nacked, 249 Unknown 250 }; 251 252 /** The result of this packet's request. */ 253 Result result; 254 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; } 258 259 /** Accessor function that returns the destination index of 260 the packet. */ 261 short getDest() const { return dest; } 262 void setDest(short _dest) { dest = _dest; } 263 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); } 267 268 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; } 269 void cmdOverride(Command newCmd) { cmd = newCmd; } 270 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), 279 srcValid(false), 280 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd), 281 result(Unknown) 282 { 283 flags = 0; 284 time = curTick; 285 } 286 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), 293 dest(_dest), 294 addrSizeValid(_req->validPaddr), srcValid(false), 295 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd), 296 result(Unknown) 297 { 298 flags = 0; 299 time = curTick; 300 } 301 302 /** Destructor. */ 303 ~Packet() 304 { deleteData(); } 305 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); 313 addr = req->paddr; 314 size = req->size; 315 time = req->time; 316 addrSizeValid = true; 317 result = Unknown; 318 if (dynamicData) { 319 deleteData(); 320 dynamicData = false; 321 arrayData = false; 322 } 323 } 324 325 /** Take a request packet and modify it in place to be suitable 326 * for returning as a response to that request. Used for timing 327 * accesses only. For atomic and functional accesses, the 328 * request packet is always implicitly passed back *without* 329 * modifying the destination fields, so this function 330 * should not be called. */ 331 void makeTimingResponse() { 332 assert(needsResponse()); 333 assert(isRequest()); 334 int icmd = (int)cmd; 335 icmd &= ~(IsRequest); 336 icmd |= IsResponse; 337 if (isRead()) 338 icmd |= HasData; 339 if (isWrite()) 340 icmd &= ~HasData; 341 cmd = (Command)icmd; 342 dest = src; 343 srcValid = false; 344 } 345 346 /** 347 * Take a request packet and modify it in place to be suitable for 348 * returning as a response to that request. 349 */ 350 void makeAtomicResponse() 351 { 352 assert(needsResponse()); 353 assert(isRequest()); 354 int icmd = (int)cmd; 355 icmd &= ~(IsRequest); 356 icmd |= IsResponse; 357 if (isRead()) 358 icmd |= HasData; 359 if (isWrite()) 360 icmd &= ~HasData; 361 cmd = (Command)icmd; 362 } 363 364 /** 365 * Take a request packet that has been returned as NACKED and 366 * modify it so that it can be sent out again. Only packets that 367 * need a response can be NACKED, so verify that that is true. 368 */ 369 void 370 reinitNacked() 371 { 372 assert(needsResponse() && result == Nacked); 373 dest = Broadcast; 374 result = Unknown; 375 } 376 377 378 /** 379 * Set the data pointer to the following value that should not be 380 * freed. 381 */ 382 template <typename T> 383 void 384 dataStatic(T *p) 385 { 386 if(dynamicData) 387 dynamicData = false; 388 data = (PacketDataPtr)p; 389 staticData = true; 390 } 391 392 /** 393 * Set the data pointer to a value that should have delete [] 394 * called on it. 395 */ 396 template <typename T> 397 void 398 dataDynamicArray(T *p) 399 { 400 assert(!staticData && !dynamicData); 401 data = (PacketDataPtr)p; 402 dynamicData = true; 403 arrayData = true; 404 } 405 406 /** 407 * set the data pointer to a value that should have delete called 408 * on it. 409 */ 410 template <typename T> 411 void 412 dataDynamic(T *p) 413 { 414 assert(!staticData && !dynamicData); 415 data = (PacketDataPtr)p; 416 dynamicData = true; 417 arrayData = false; 418 } 419 420 /** get a pointer to the data ptr. */ 421 template <typename T> 422 T* 423 getPtr() 424 { 425 assert(staticData || dynamicData); 426 return (T*)data; 427 } 428 429 /** return the value of what is pointed to in the packet. */ 430 template <typename T> 431 T get(); 432 433 /** set the value in the data pointer to v. */ 434 template <typename T> 435 void set(T v); 436 437 /** 438 * delete the data pointed to in the data pointer. Ok to call to 439 * matter how data was allocted. 440 */ 441 void deleteData(); 442 443 /** If there isn't data in the packet, allocate some. */ 444 void allocate(); 445 446 /** Do the packet modify the same addresses. */ 447 bool intersect(PacketPtr p); 448}; 449 450 451/** This function given a functional packet and a timing packet either satisfies 452 * the timing packet, or updates the timing packet to reflect the updated state 453 * in the timing packet. It returns if the functional packet should continue to 454 * traverse the memory hierarchy or not. 455 */ 456bool fixPacket(PacketPtr func, PacketPtr timing); 457 458std::ostream & operator<<(std::ostream &o, const Packet &p); 459 460#endif //__MEM_PACKET_HH 461