packet.cc revision 3605:ed3c5b4e8bca
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: Ali Saidi 29 * Steve Reinhardt 30 */ 31 32/** 33 * @file 34 * Definition of the Packet Class, a packet is a transaction occuring 35 * between a single level of the memory heirarchy (ie L1->L2). 36 */ 37 38#include <iostream> 39 40#include "base/misc.hh" 41#include "base/trace.hh" 42#include "mem/packet.hh" 43 44static const std::string ReadReqString("ReadReq"); 45static const std::string WriteReqString("WriteReq"); 46static const std::string WriteReqNoAckString("WriteReqNoAck|Writeback"); 47static const std::string ReadRespString("ReadResp"); 48static const std::string WriteRespString("WriteResp"); 49static const std::string SoftPFReqString("SoftPFReq"); 50static const std::string SoftPFRespString("SoftPFResp"); 51static const std::string HardPFReqString("HardPFReq"); 52static const std::string HardPFRespString("HardPFResp"); 53static const std::string InvalidateReqString("InvalidateReq"); 54static const std::string WriteInvalidateReqString("WriteInvalidateReq"); 55static const std::string WriteInvalidateRespString("WriteInvalidateResp"); 56static const std::string UpgradeReqString("UpgradeReq"); 57static const std::string ReadExReqString("ReadExReq"); 58static const std::string ReadExRespString("ReadExResp"); 59static const std::string OtherCmdString("<other>"); 60 61const std::string & 62Packet::cmdString() const 63{ 64 switch (cmd) { 65 case ReadReq: return ReadReqString; 66 case WriteReq: return WriteReqString; 67 case WriteReqNoAck: return WriteReqNoAckString; 68 case ReadResp: return ReadRespString; 69 case WriteResp: return WriteRespString; 70 case SoftPFReq: return SoftPFReqString; 71 case SoftPFResp: return SoftPFRespString; 72 case HardPFReq: return HardPFReqString; 73 case HardPFResp: return HardPFRespString; 74 case InvalidateReq: return InvalidateReqString; 75 case WriteInvalidateReq:return WriteInvalidateReqString; 76 case WriteInvalidateResp:return WriteInvalidateRespString; 77 case UpgradeReq: return UpgradeReqString; 78 case ReadExReq: return ReadExReqString; 79 case ReadExResp: return ReadExRespString; 80 default: return OtherCmdString; 81 } 82} 83 84const std::string & 85Packet::cmdIdxToString(Packet::Command idx) 86{ 87 switch (idx) { 88 case ReadReq: return ReadReqString; 89 case WriteReq: return WriteReqString; 90 case WriteReqNoAck: return WriteReqNoAckString; 91 case ReadResp: return ReadRespString; 92 case WriteResp: return WriteRespString; 93 case SoftPFReq: return SoftPFReqString; 94 case SoftPFResp: return SoftPFRespString; 95 case HardPFReq: return HardPFReqString; 96 case HardPFResp: return HardPFRespString; 97 case InvalidateReq: return InvalidateReqString; 98 case WriteInvalidateReq:return WriteInvalidateReqString; 99 case WriteInvalidateResp:return WriteInvalidateRespString; 100 case UpgradeReq: return UpgradeReqString; 101 case ReadExReq: return ReadExReqString; 102 case ReadExResp: return ReadExRespString; 103 default: return OtherCmdString; 104 } 105} 106 107/** delete the data pointed to in the data pointer. Ok to call to matter how 108 * data was allocted. */ 109void 110Packet::deleteData() 111{ 112 assert(staticData || dynamicData); 113 if (staticData) 114 return; 115 116 if (arrayData) 117 delete [] data; 118 else 119 delete data; 120} 121 122/** If there isn't data in the packet, allocate some. */ 123void 124Packet::allocate() 125{ 126 if (data) 127 return; 128 assert(!staticData); 129 dynamicData = true; 130 arrayData = true; 131 data = new uint8_t[getSize()]; 132} 133 134/** Do the packet modify the same addresses. */ 135bool 136Packet::intersect(PacketPtr p) 137{ 138 Addr s1 = getAddr(); 139 Addr e1 = getAddr() + getSize() - 1; 140 Addr s2 = p->getAddr(); 141 Addr e2 = p->getAddr() + p->getSize() - 1; 142 143 return !(s1 > e2 || e1 < s2); 144} 145 146bool 147fixPacket(PacketPtr func, PacketPtr timing) 148{ 149 Addr funcStart = func->getAddr(); 150 Addr funcEnd = func->getAddr() + func->getSize() - 1; 151 Addr timingStart = timing->getAddr(); 152 Addr timingEnd = timing->getAddr() + timing->getSize() - 1; 153 154 assert(!(funcStart > timingEnd || timingStart > funcEnd)); 155 156 if (DTRACE(FunctionalAccess)) { 157 DebugOut() << func; 158 DebugOut() << timing; 159 } 160 161 // this packet can't solve our problem, continue on 162 if (!timing->hasData()) 163 return true; 164 165 if (func->isRead()) { 166 if (funcStart >= timingStart && funcEnd <= timingEnd) { 167 func->allocate(); 168 memcpy(func->getPtr<uint8_t>(), timing->getPtr<uint8_t>() + 169 funcStart - timingStart, func->getSize()); 170 func->result = Packet::Success; 171 func->flags |= SATISFIED; 172 return false; 173 } else { 174 // In this case the timing packet only partially satisfies the 175 // requset, so we would need more information to make this work. 176 // Like bytes valid in the packet or something, so the request could 177 // continue and get this bit of possibly newer data along with the 178 // older data not written to yet. 179 panic("Timing packet only partially satisfies the functional" 180 "request. Now what?"); 181 } 182 } else if (func->isWrite()) { 183 if (funcStart >= timingStart) { 184 memcpy(timing->getPtr<uint8_t>() + (funcStart - timingStart), 185 func->getPtr<uint8_t>(), 186 (std::min(funcEnd, timingEnd) - funcStart) + 1); 187 } else { // timingStart > funcStart 188 memcpy(timing->getPtr<uint8_t>(), 189 func->getPtr<uint8_t>() + (timingStart - funcStart), 190 (std::min(funcEnd, timingEnd) - timingStart) + 1); 191 } 192 // we always want to keep going with a write 193 return true; 194 } else 195 panic("Don't know how to handle command type %#x\n", 196 func->cmdToIndex()); 197 198} 199 200 201std::ostream & 202operator<<(std::ostream &o, const Packet &p) 203{ 204 205 o << "[0x"; 206 o.setf(std::ios_base::hex, std::ios_base::showbase); 207 o << p.getAddr(); 208 o.unsetf(std::ios_base::hex| std::ios_base::showbase); 209 o << ":"; 210 o.setf(std::ios_base::hex, std::ios_base::showbase); 211 o << p.getAddr() + p.getSize() - 1 << "] "; 212 o.unsetf(std::ios_base::hex| std::ios_base::showbase); 213 214 if (p.result == Packet::Success) 215 o << "Successful "; 216 if (p.result == Packet::BadAddress) 217 o << "BadAddress "; 218 if (p.result == Packet::Nacked) 219 o << "Nacked "; 220 if (p.result == Packet::Unknown) 221 o << "Inflight "; 222 223 if (p.isRead()) 224 o << "Read "; 225 if (p.isWrite()) 226 o << "Read "; 227 if (p.isInvalidate()) 228 o << "Read "; 229 if (p.isRequest()) 230 o << "Request "; 231 if (p.isResponse()) 232 o << "Response "; 233 if (p.hasData()) 234 o << "w/Data "; 235 236 o << std::endl; 237 return o; 238} 239 240