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