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