packet.cc revision 4622:f681e10844f3
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
44// The one downside to bitsets is that static initializers can get ugly.
45#define SET1(a1)                     (1 << (a1))
46#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
47#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
48#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
49#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
50#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
51
52const MemCmd::CommandInfo
53MemCmd::commandInfo[] =
54{
55    /* InvalidCmd */
56    { 0, InvalidCmd, "InvalidCmd" },
57    /* ReadReq */
58    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
59    /* ReadResp */
60    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
61    /* WriteReq */
62    { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
63            WriteResp, "WriteReq" },
64    /* WriteResp */
65    { SET2(IsWrite, IsResponse), InvalidCmd, "WriteResp" },
66    /* Writeback */
67    { SET4(IsWrite, IsRequest, HasData, NeedsResponse),
68            WritebackAck, "Writeback" },
69    /* WritebackAck */
70    { SET2(IsWrite, IsResponse), InvalidCmd, "WritebackAck" },
71    /* SoftPFReq */
72    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
73            SoftPFResp, "SoftPFReq" },
74    /* HardPFReq */
75    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
76            HardPFResp, "HardPFReq" },
77    /* SoftPFResp */
78    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
79            InvalidCmd, "SoftPFResp" },
80    /* HardPFResp */
81    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
82            InvalidCmd, "HardPFResp" },
83    /* InvalidateReq */
84    { SET2(IsInvalidate, IsRequest), InvalidCmd, "InvalidateReq" },
85    /* WriteInvalidateReq */
86    { SET5(IsWrite, IsInvalidate, IsRequest, HasData, NeedsResponse),
87            WriteInvalidateResp, "WriteInvalidateReq" },
88    /* WriteInvalidateResp */
89    { SET3(IsWrite, IsInvalidate, IsResponse),
90            InvalidCmd, "WriteInvalidateResp" },
91    /* UpgradeReq */
92    { SET3(IsInvalidate, IsRequest, IsUpgrade), InvalidCmd, "UpgradeReq" },
93    /* ReadExReq */
94    { SET4(IsRead, IsInvalidate, IsRequest, NeedsResponse),
95            ReadExResp, "ReadExReq" },
96    /* ReadExResp */
97    { SET4(IsRead, IsInvalidate, IsResponse, HasData),
98            InvalidCmd, "ReadExResp" },
99    /* SwapReq -- for Swap ldstub type operations */
100    { SET4(IsReadWrite, IsRequest, HasData, NeedsResponse),
101        SwapResp, "SwapReq" },
102    /* SwapResp -- for Swap ldstub type operations */
103    { SET3(IsReadWrite, IsResponse, HasData),
104        InvalidCmd, "SwapResp" }
105};
106
107
108/** delete the data pointed to in the data pointer. Ok to call to matter how
109 * data was allocted. */
110void
111Packet::deleteData()
112{
113    assert(staticData || dynamicData);
114    if (staticData)
115        return;
116
117    if (arrayData)
118        delete [] data;
119    else
120        delete data;
121}
122
123/** If there isn't data in the packet, allocate some. */
124void
125Packet::allocate()
126{
127    if (data)
128        return;
129    assert(!staticData);
130    dynamicData = true;
131    arrayData = true;
132    data = new uint8_t[getSize()];
133}
134
135/** Do the packet modify the same addresses. */
136bool
137Packet::intersect(PacketPtr p)
138{
139    Addr s1 = getAddr();
140    Addr e1 = getAddr() + getSize() - 1;
141    Addr s2 = p->getAddr();
142    Addr e2 = p->getAddr() + p->getSize() - 1;
143
144    return !(s1 > e2 || e1 < s2);
145}
146
147bool
148fixDelayedResponsePacket(PacketPtr func, PacketPtr timing)
149{
150    bool result;
151
152    if (timing->isRead() || timing->isWrite()) {
153        // Ugly hack to deal with the fact that we queue the requests
154        // and don't convert them to responses until we issue them on
155        // the bus.  I tried to avoid this by converting packets to
156        // responses right away, but this breaks during snoops where a
157        // responder may do the conversion before other caches have
158        // done the snoop.  Would work if we copied the packet instead
159        // of just hanging on to a pointer.
160        MemCmd oldCmd = timing->cmd;
161        timing->cmd = timing->cmd.responseCommand();
162        result = fixPacket(func, timing);
163        timing->cmd = oldCmd;
164    }
165    else {
166        //Don't toggle if it isn't a read/write response
167        result = fixPacket(func, timing);
168    }
169
170    return result;
171}
172
173bool
174fixPacket(PacketPtr func, PacketPtr timing)
175{
176    Addr funcStart      = func->getAddr();
177    Addr funcEnd        = func->getAddr() + func->getSize() - 1;
178    Addr timingStart    = timing->getAddr();
179    Addr timingEnd      = timing->getAddr() + timing->getSize() - 1;
180
181    assert(!(funcStart > timingEnd || timingStart > funcEnd));
182
183    // this packet can't solve our problem, continue on
184    if (!timing->hasData())
185        return true;
186
187    if (func->isRead()) {
188        if (funcStart >= timingStart && funcEnd <= timingEnd) {
189            func->allocate();
190            std::memcpy(func->getPtr<uint8_t>(), timing->getPtr<uint8_t>() +
191                    funcStart - timingStart, func->getSize());
192            func->result = Packet::Success;
193            func->flags |= SATISFIED;
194            return false;
195        } else {
196            // In this case the timing packet only partially satisfies
197            // the request, so we would need more information to make
198            // this work.  Like bytes valid in the packet or
199            // something, so the request could continue and get this
200            // bit of possibly newer data along with the older data
201            // not written to yet.
202            panic("Timing packet only partially satisfies the functional"
203                    "request. Now what?");
204        }
205    } else if (func->isWrite()) {
206        if (funcStart >= timingStart) {
207            std::memcpy(timing->getPtr<uint8_t>() + (funcStart - timingStart),
208                   func->getPtr<uint8_t>(),
209                   (std::min(funcEnd, timingEnd) - funcStart) + 1);
210        } else { // timingStart > funcStart
211            std::memcpy(timing->getPtr<uint8_t>(),
212                   func->getPtr<uint8_t>() + (timingStart - funcStart),
213                   (std::min(funcEnd, timingEnd) - timingStart) + 1);
214        }
215        // we always want to keep going with a write
216        return true;
217    } else
218        panic("Don't know how to handle command type %#x\n",
219                func->cmdToIndex());
220
221}
222
223
224std::ostream &
225operator<<(std::ostream &o, const Packet &p)
226{
227
228    o << "[0x";
229    o.setf(std::ios_base::hex, std::ios_base::showbase);
230    o <<  p.getAddr();
231    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
232    o <<  ":";
233    o.setf(std::ios_base::hex, std::ios_base::showbase);
234    o <<  p.getAddr() + p.getSize() - 1 << "] ";
235    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
236
237    if (p.result == Packet::Success)
238        o << "Successful ";
239    if (p.result == Packet::BadAddress)
240        o << "BadAddress ";
241    if (p.result == Packet::Nacked)
242        o << "Nacked ";
243    if (p.result == Packet::Unknown)
244        o << "Inflight ";
245
246    if (p.isRead())
247        o << "Read ";
248    if (p.isWrite())
249        o << "Write ";
250    if (p.isReadWrite())
251        o << "Read/Write ";
252    if (p.isInvalidate())
253        o << "Invalidate ";
254    if (p.isRequest())
255        o << "Request ";
256    if (p.isResponse())
257        o << "Response ";
258    if (p.hasData())
259        o << "w/Data ";
260
261    o << std::endl;
262    return o;
263}
264
265