packet.hh revision 4628:17b3ce796176
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#include <bitset> 44 45#include "base/compiler.hh" 46#include "base/misc.hh" 47#include "mem/request.hh" 48#include "sim/host.hh" 49#include "sim/core.hh" 50 51 52struct Packet; 53typedef Packet *PacketPtr; 54typedef uint8_t* PacketDataPtr; 55typedef std::list<PacketPtr> PacketList; 56 57class MemCmd 58{ 59 public: 60 61 /** List of all commands associated with a packet. */ 62 enum Command 63 { 64 InvalidCmd, 65 ReadReq, 66 ReadResp, 67 WriteReq, 68 WriteResp, 69 Writeback, 70 SoftPFReq, 71 HardPFReq, 72 SoftPFResp, 73 HardPFResp, 74 WriteInvalidateReq, 75 WriteInvalidateResp, 76 UpgradeReq, 77 UpgradeResp, 78 ReadExReq, 79 ReadExResp, 80 LoadLockedReq, 81 LoadLockedResp, 82 StoreCondReq, 83 StoreCondResp, 84 SwapReq, 85 SwapResp, 86 NUM_MEM_CMDS 87 }; 88 89 private: 90 /** List of command attributes. */ 91 enum Attribute 92 { 93 IsRead, //!< Data flows from responder to requester 94 IsWrite, //!< Data flows from requester to responder 95 IsPrefetch, //!< Not a demand access 96 IsInvalidate, 97 NeedsExclusive, //!< Requires exclusive copy to complete in-cache 98 IsRequest, //!< Issued by requester 99 IsResponse, //!< Issue by responder 100 NeedsResponse, //!< Requester needs response from target 101 IsSWPrefetch, 102 IsHWPrefetch, 103 IsLocked, //!< Alpha/MIPS LL or SC access 104 HasData, //!< There is an associated payload 105 NUM_COMMAND_ATTRIBUTES 106 }; 107 108 /** Structure that defines attributes and other data associated 109 * with a Command. */ 110 struct CommandInfo { 111 /** Set of attribute flags. */ 112 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes; 113 /** Corresponding response for requests; InvalidCmd if no 114 * response is applicable. */ 115 const Command response; 116 /** String representation (for printing) */ 117 const std::string str; 118 }; 119 120 /** Array to map Command enum to associated info. */ 121 static const CommandInfo commandInfo[]; 122 123 private: 124 125 Command cmd; 126 127 bool testCmdAttrib(MemCmd::Attribute attrib) const { 128 return commandInfo[cmd].attributes[attrib] != 0; 129 } 130 131 public: 132 133 bool isRead() const { return testCmdAttrib(IsRead); } 134 bool isWrite() const { return testCmdAttrib(IsWrite); } 135 bool isRequest() const { return testCmdAttrib(IsRequest); } 136 bool isResponse() const { return testCmdAttrib(IsResponse); } 137 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); } 138 bool needsResponse() const { return testCmdAttrib(NeedsResponse); } 139 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); } 140 bool hasData() const { return testCmdAttrib(HasData); } 141 bool isReadWrite() const { return isRead() && isWrite(); } 142 bool isLocked() const { return testCmdAttrib(IsLocked); } 143 144 const Command responseCommand() const { 145 return commandInfo[cmd].response; 146 } 147 148 /** Return the string to a cmd given by idx. */ 149 const std::string &toString() const { 150 return commandInfo[cmd].str; 151 } 152 153 int toInt() const { return (int)cmd; } 154 155 MemCmd(Command _cmd) 156 : cmd(_cmd) 157 { } 158 159 MemCmd(int _cmd) 160 : cmd((Command)_cmd) 161 { } 162 163 MemCmd() 164 : cmd(InvalidCmd) 165 { } 166 167 bool operator==(MemCmd c2) { return (cmd == c2.cmd); } 168 bool operator!=(MemCmd c2) { return (cmd != c2.cmd); } 169 170 friend class Packet; 171}; 172 173/** 174 * A Packet is used to encapsulate a transfer between two objects in 175 * the memory system (e.g., the L1 and L2 cache). (In contrast, a 176 * single Request travels all the way from the requester to the 177 * ultimate destination and back, possibly being conveyed by several 178 * different Packets along the way.) 179 */ 180class Packet 181{ 182 public: 183 184 typedef MemCmd::Command Command; 185 186 private: 187 /** A pointer to the data being transfered. It can be differnt 188 * sizes at each level of the heirarchy so it belongs in the 189 * packet, not request. This may or may not be populated when a 190 * responder recieves the packet. If not populated it memory 191 * should be allocated. 192 */ 193 PacketDataPtr data; 194 195 /** Is the data pointer set to a value that shouldn't be freed 196 * when the packet is destroyed? */ 197 bool staticData; 198 /** The data pointer points to a value that should be freed when 199 * the packet is destroyed. */ 200 bool dynamicData; 201 /** the data pointer points to an array (thus delete [] ) needs to 202 * be called on it rather than simply delete.*/ 203 bool arrayData; 204 205 /** The address of the request. This address could be virtual or 206 * physical, depending on the system configuration. */ 207 Addr addr; 208 209 /** The size of the request or transfer. */ 210 int size; 211 212 /** Device address (e.g., bus ID) of the source of the 213 * transaction. The source is not responsible for setting this 214 * field; it is set implicitly by the interconnect when the 215 * packet is first sent. */ 216 short src; 217 218 /** Device address (e.g., bus ID) of the destination of the 219 * transaction. The special value Broadcast indicates that the 220 * packet should be routed based on its address. This field is 221 * initialized in the constructor and is thus always valid 222 * (unlike * addr, size, and src). */ 223 short dest; 224 225 /** Are the 'addr' and 'size' fields valid? */ 226 bool addrSizeValid; 227 /** Is the 'src' field valid? */ 228 bool srcValid; 229 230 enum SnoopFlag { 231 MemInhibit, 232 Shared, 233 NUM_SNOOP_FLAGS 234 }; 235 236 /** Coherence snoopFlags for snooping */ 237 std::bitset<NUM_SNOOP_FLAGS> snoopFlags; 238 239 public: 240 241 /** Used to calculate latencies for each packet.*/ 242 Tick time; 243 244 /** The time at which the packet will be fully transmitted */ 245 Tick finishTime; 246 247 /** The time at which the first chunk of the packet will be transmitted */ 248 Tick firstWordTime; 249 250 /** The special destination address indicating that the packet 251 * should be routed based on its address. */ 252 static const short Broadcast = -1; 253 254 /** A pointer to the original request. */ 255 RequestPtr req; 256 257 /** A virtual base opaque structure used to hold coherence-related 258 * state. A specific subclass would be derived from this to 259 * carry state specific to a particular coherence protocol. */ 260 class CoherenceState { 261 public: 262 virtual ~CoherenceState() {} 263 }; 264 265 /** This packet's coherence state. Caches should use 266 * dynamic_cast<> to cast to the state appropriate for the 267 * system's coherence protocol. */ 268 CoherenceState *coherence; 269 270 /** A virtual base opaque structure used to hold state associated 271 * with the packet but specific to the sending device (e.g., an 272 * MSHR). A pointer to this state is returned in the packet's 273 * response so that the sender can quickly look up the state 274 * needed to process it. A specific subclass would be derived 275 * from this to carry state specific to a particular sending 276 * device. */ 277 class SenderState { 278 public: 279 virtual ~SenderState() {} 280 }; 281 282 /** This packet's sender state. Devices should use dynamic_cast<> 283 * to cast to the state appropriate to the sender. */ 284 SenderState *senderState; 285 286 public: 287 288 /** The command field of the packet. */ 289 MemCmd cmd; 290 291 /** Return the string name of the cmd field (for debugging and 292 * tracing). */ 293 const std::string &cmdString() const { return cmd.toString(); } 294 295 /** Return the index of this command. */ 296 inline int cmdToIndex() const { return cmd.toInt(); } 297 298 public: 299 300 bool isRead() const { return cmd.isRead(); } 301 bool isWrite() const { return cmd.isWrite(); } 302 bool isRequest() const { return cmd.isRequest(); } 303 bool isResponse() const { return cmd.isResponse(); } 304 bool needsExclusive() const { return cmd.needsExclusive(); } 305 bool needsResponse() const { return cmd.needsResponse(); } 306 bool isInvalidate() const { return cmd.isInvalidate(); } 307 bool hasData() const { return cmd.hasData(); } 308 bool isReadWrite() const { return cmd.isReadWrite(); } 309 bool isLocked() const { return cmd.isLocked(); } 310 311 void assertMemInhibit() { snoopFlags[MemInhibit] = true; } 312 void assertShared() { snoopFlags[Shared] = true; } 313 bool memInhibitAsserted() { return snoopFlags[MemInhibit]; } 314 bool sharedAsserted() { return snoopFlags[Shared]; } 315 316 bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN } 317 318 /** Possible results of a packet's request. */ 319 enum Result 320 { 321 Success, 322 BadAddress, 323 Nacked, 324 Unknown 325 }; 326 327 /** The result of this packet's request. */ 328 Result result; 329 330 /** Accessor function that returns the source index of the packet. */ 331 short getSrc() const { assert(srcValid); return src; } 332 void setSrc(short _src) { src = _src; srcValid = true; } 333 /** Reset source field, e.g. to retransmit packet on different bus. */ 334 void clearSrc() { srcValid = false; } 335 336 /** Accessor function that returns the destination index of 337 the packet. */ 338 short getDest() const { return dest; } 339 void setDest(short _dest) { dest = _dest; } 340 341 Addr getAddr() const { assert(addrSizeValid); return addr; } 342 int getSize() const { assert(addrSizeValid); return size; } 343 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); } 344 345 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; } 346 void cmdOverride(MemCmd newCmd) { cmd = newCmd; } 347 348 /** Constructor. Note that a Request object must be constructed 349 * first, but the Requests's physical address and size fields 350 * need not be valid. The command and destination addresses 351 * must be supplied. */ 352 Packet(Request *_req, MemCmd _cmd, short _dest) 353 : data(NULL), staticData(false), dynamicData(false), arrayData(false), 354 addr(_req->paddr), size(_req->size), dest(_dest), 355 addrSizeValid(_req->validPaddr), srcValid(false), 356 snoopFlags(0), 357 time(curTick), 358 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd), 359 result(Unknown) 360 { 361 } 362 363 /** Alternate constructor if you are trying to create a packet with 364 * a request that is for a whole block, not the address from the req. 365 * this allows for overriding the size/addr of the req.*/ 366 Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize) 367 : data(NULL), staticData(false), dynamicData(false), arrayData(false), 368 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest), 369 addrSizeValid(_req->validPaddr), srcValid(false), 370 snoopFlags(0), 371 time(curTick), 372 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd), 373 result(Unknown) 374 { 375 } 376 377 /** Alternate constructor for copying a packet. Copy all fields 378 * *except* set data allocation as static... even if the original 379 * packet's data was dynamic, we don't want to free it when the 380 * new packet is deallocated. Note that if original packet used 381 * dynamic data, user must guarantee that the new packet's 382 * lifetime is less than that of the original packet. */ 383 Packet(Packet *origPkt) 384 : data(NULL), staticData(false), dynamicData(false), arrayData(false), 385 addr(origPkt->addr), size(origPkt->size), 386 dest(origPkt->dest), 387 addrSizeValid(origPkt->addrSizeValid), srcValid(origPkt->srcValid), 388 snoopFlags(origPkt->snoopFlags), 389 time(curTick), 390 req(origPkt->req), coherence(origPkt->coherence), 391 senderState(origPkt->senderState), cmd(origPkt->cmd), 392 result(origPkt->result) 393 { 394 } 395 396 /** Destructor. */ 397 ~Packet() 398 { if (staticData || dynamicData) deleteData(); } 399 400 /** Reinitialize packet address and size from the associated 401 * Request object, and reset other fields that may have been 402 * modified by a previous transaction. Typically called when a 403 * statically allocated Request/Packet pair is reused for 404 * multiple transactions. */ 405 void reinitFromRequest() { 406 assert(req->validPaddr); 407 snoopFlags = 0; 408 addr = req->paddr; 409 size = req->size; 410 time = req->time; 411 addrSizeValid = true; 412 result = Unknown; 413 if (dynamicData) { 414 deleteData(); 415 dynamicData = false; 416 arrayData = false; 417 } 418 } 419 420 /** 421 * Take a request packet and modify it in place to be suitable for 422 * returning as a response to that request. The source and 423 * destination fields are *not* modified, as is appropriate for 424 * atomic accesses. 425 */ 426 void makeAtomicResponse() 427 { 428 assert(needsResponse()); 429 assert(isRequest()); 430 assert(result == Unknown); 431 cmd = cmd.responseCommand(); 432 result = Success; 433 } 434 435 /** 436 * Perform the additional work required for timing responses above 437 * and beyond atomic responses; i.e., change the destination to 438 * point back to the requester and clear the source field. 439 */ 440 void convertAtomicToTimingResponse() 441 { 442 dest = src; 443 srcValid = false; 444 } 445 446 /** 447 * Take a request packet and modify it in place to be suitable for 448 * returning as a response to a timing request. 449 */ 450 void makeTimingResponse() 451 { 452 makeAtomicResponse(); 453 convertAtomicToTimingResponse(); 454 } 455 456 /** 457 * Take a request packet that has been returned as NACKED and 458 * modify it so that it can be sent out again. Only packets that 459 * need a response can be NACKED, so verify that that is true. 460 */ 461 void 462 reinitNacked() 463 { 464 assert(needsResponse() && result == Nacked); 465 dest = Broadcast; 466 result = Unknown; 467 } 468 469 470 /** 471 * Set the data pointer to the following value that should not be 472 * freed. 473 */ 474 template <typename T> 475 void 476 dataStatic(T *p) 477 { 478 if(dynamicData) 479 dynamicData = false; 480 data = (PacketDataPtr)p; 481 staticData = true; 482 } 483 484 /** 485 * Set the data pointer to a value that should have delete [] 486 * called on it. 487 */ 488 template <typename T> 489 void 490 dataDynamicArray(T *p) 491 { 492 assert(!staticData && !dynamicData); 493 data = (PacketDataPtr)p; 494 dynamicData = true; 495 arrayData = true; 496 } 497 498 /** 499 * set the data pointer to a value that should have delete called 500 * on it. 501 */ 502 template <typename T> 503 void 504 dataDynamic(T *p) 505 { 506 assert(!staticData && !dynamicData); 507 data = (PacketDataPtr)p; 508 dynamicData = true; 509 arrayData = false; 510 } 511 512 /** get a pointer to the data ptr. */ 513 template <typename T> 514 T* 515 getPtr() 516 { 517 assert(staticData || dynamicData); 518 return (T*)data; 519 } 520 521 /** return the value of what is pointed to in the packet. */ 522 template <typename T> 523 T get(); 524 525 /** set the value in the data pointer to v. */ 526 template <typename T> 527 void set(T v); 528 529 /** 530 * Copy data into the packet from the provided pointer. 531 */ 532 void setData(uint8_t *p) 533 { 534 std::memcpy(getPtr<uint8_t>(), p, getSize()); 535 } 536 537 /** 538 * Copy data into the packet from the provided block pointer, 539 * which is aligned to the given block size. 540 */ 541 void setDataFromBlock(uint8_t *blk_data, int blkSize) 542 { 543 setData(blk_data + getOffset(blkSize)); 544 } 545 546 /** 547 * Copy data from the packet to the provided block pointer, which 548 * is aligned to the given block size. 549 */ 550 void writeData(uint8_t *p) 551 { 552 std::memcpy(p, getPtr<uint8_t>(), getSize()); 553 } 554 555 /** 556 * Copy data from the packet to the memory at the provided pointer. 557 */ 558 void writeDataToBlock(uint8_t *blk_data, int blkSize) 559 { 560 writeData(blk_data + getOffset(blkSize)); 561 } 562 563 /** 564 * delete the data pointed to in the data pointer. Ok to call to 565 * matter how data was allocted. 566 */ 567 void deleteData(); 568 569 /** If there isn't data in the packet, allocate some. */ 570 void allocate(); 571 572 /** Do the packet modify the same addresses. */ 573 bool intersect(PacketPtr p); 574 575 /** 576 * Check a functional request against a memory value represented 577 * by a base/size pair and an associated data array. If the 578 * functional request is a read, it may be satisfied by the memory 579 * value. If the functional request is a write, it may update the 580 * memory value. 581 */ 582 bool checkFunctional(Addr base, int size, uint8_t *data); 583 584 /** 585 * Check a functional request against a memory value stored in 586 * another packet (i.e. an in-transit request or response). 587 */ 588 bool checkFunctional(PacketPtr otherPkt) { 589 return (otherPkt->hasData() && 590 checkFunctional(otherPkt->getAddr(), otherPkt->getSize(), 591 otherPkt->getPtr<uint8_t>())); 592 } 593}; 594 595 596 597/** Temporary for backwards compatibility. 598 */ 599inline 600bool fixPacket(PacketPtr func, PacketPtr timing) { 601 return !func->checkFunctional(timing); 602} 603 604/** This function is a wrapper for the fixPacket field that toggles 605 * the hasData bit it is used when a response is waiting in the 606 * caches, but hasn't been marked as a response yet (so the fixPacket 607 * needs to get the correct value for the hasData) 608 */ 609bool fixDelayedResponsePacket(PacketPtr func, PacketPtr timing); 610 611std::ostream & operator<<(std::ostream &o, const Packet &p); 612 613#endif //__MEM_PACKET_HH 614