packet.cc revision 4021:bdae6aac5c43
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 <cstring>
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
147fixDelayedResponsePacket(PacketPtr func, PacketPtr timing)
148{
149    bool result;
150
151    if (timing->isRead() || timing->isWrite()) {
152        timing->toggleData();
153        result = fixPacket(func, timing);
154        timing->toggleData();
155    }
156    else {
157        //Don't toggle if it isn't a read/write response
158        result = fixPacket(func, timing);
159    }
160
161    return result;
162}
163
164bool
165fixPacket(PacketPtr func, PacketPtr timing)
166{
167    Addr funcStart      = func->getAddr();
168    Addr funcEnd        = func->getAddr() + func->getSize() - 1;
169    Addr timingStart    = timing->getAddr();
170    Addr timingEnd      = timing->getAddr() + timing->getSize() - 1;
171
172    assert(!(funcStart > timingEnd || timingStart > funcEnd));
173
174    // this packet can't solve our problem, continue on
175    if (!timing->hasData())
176        return true;
177
178    if (func->isRead()) {
179        if (funcStart >= timingStart && funcEnd <= timingEnd) {
180            func->allocate();
181            std::memcpy(func->getPtr<uint8_t>(), timing->getPtr<uint8_t>() +
182                    funcStart - timingStart, func->getSize());
183            func->result = Packet::Success;
184            func->flags |= SATISFIED;
185            return false;
186        } else {
187            // In this case the timing packet only partially satisfies the
188            // requset, so we would need more information to make this work.
189            // Like bytes valid in the packet or something, so the request could
190            // continue and get this bit of possibly newer data along with the
191            // older data not written to yet.
192            panic("Timing packet only partially satisfies the functional"
193                    "request. Now what?");
194        }
195    } else if (func->isWrite()) {
196        if (funcStart >= timingStart) {
197            std::memcpy(timing->getPtr<uint8_t>() + (funcStart - timingStart),
198                   func->getPtr<uint8_t>(),
199                   (std::min(funcEnd, timingEnd) - funcStart) + 1);
200        } else { // timingStart > funcStart
201            std::memcpy(timing->getPtr<uint8_t>(),
202                   func->getPtr<uint8_t>() + (timingStart - funcStart),
203                   (std::min(funcEnd, timingEnd) - timingStart) + 1);
204        }
205        // we always want to keep going with a write
206        return true;
207    } else
208        panic("Don't know how to handle command type %#x\n",
209                func->cmdToIndex());
210
211}
212
213
214std::ostream &
215operator<<(std::ostream &o, const Packet &p)
216{
217
218    o << "[0x";
219    o.setf(std::ios_base::hex, std::ios_base::showbase);
220    o <<  p.getAddr();
221    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
222    o <<  ":";
223    o.setf(std::ios_base::hex, std::ios_base::showbase);
224    o <<  p.getAddr() + p.getSize() - 1 << "] ";
225    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
226
227    if (p.result == Packet::Success)
228        o << "Successful ";
229    if (p.result == Packet::BadAddress)
230        o << "BadAddress ";
231    if (p.result == Packet::Nacked)
232        o << "Nacked ";
233    if (p.result == Packet::Unknown)
234        o << "Inflight ";
235
236    if (p.isRead())
237        o << "Read ";
238    if (p.isWrite())
239        o << "Read ";
240    if (p.isInvalidate())
241        o << "Read ";
242    if (p.isRequest())
243        o << "Request ";
244    if (p.isResponse())
245        o << "Response ";
246    if (p.hasData())
247        o << "w/Data ";
248
249    o << std::endl;
250    return o;
251}
252
253