packet.cc revision 3400:469db0566924
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            return false;
172        } else {
173            // In this case the timing packet only partially satisfies the
174            // requset, so we would need more information to make this work.
175            // Like bytes valid in the packet or something, so the request could
176            // continue and get this bit of possibly newer data along with the
177            // older data not written to yet.
178            panic("Timing packet only partially satisfies the functional"
179                    "request. Now what?");
180        }
181    } else if (func->isWrite()) {
182        if (funcStart >= timingStart) {
183            memcpy(timing->getPtr<uint8_t>() + (funcStart - timingStart),
184                   func->getPtr<uint8_t>(),
185                   std::min(funcEnd, timingEnd) - funcStart);
186        } else { // timingStart > funcStart
187            memcpy(timing->getPtr<uint8_t>(),
188                   func->getPtr<uint8_t>() + (timingStart - funcStart),
189                   std::min(funcEnd, timingEnd) - timingStart);
190        }
191        // we always want to keep going with a write
192        return true;
193    } else
194        panic("Don't know how to handle command type %#x\n",
195                func->cmdToIndex());
196
197}
198
199
200std::ostream &
201operator<<(std::ostream &o, const Packet &p)
202{
203
204    o << "[0x";
205    o.setf(std::ios_base::hex, std::ios_base::showbase);
206    o <<  p.getAddr();
207    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
208    o <<  ":";
209    o.setf(std::ios_base::hex, std::ios_base::showbase);
210    o <<  p.getAddr() + p.getSize() - 1 << "] ";
211    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
212
213    if (p.result == Packet::Success)
214        o << "Successful ";
215    if (p.result == Packet::BadAddress)
216        o << "BadAddress ";
217    if (p.result == Packet::Nacked)
218        o << "Nacked ";
219    if (p.result == Packet::Unknown)
220        o << "Inflight ";
221
222    if (p.isRead())
223        o << "Read ";
224    if (p.isWrite())
225        o << "Read ";
226    if (p.isInvalidate())
227        o << "Read ";
228    if (p.isRequest())
229        o << "Request ";
230    if (p.isResponse())
231        o << "Response ";
232    if (p.hasData())
233        o << "w/Data ";
234
235    o << std::endl;
236    return o;
237}
238
239