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