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