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