packet.cc revision 8229:78bf55f23338
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Ali Saidi
30 *          Steve Reinhardt
31 */
32
33/**
34 * @file
35 * Definition of the Packet Class, a packet is a transaction occuring
36 * between a single level of the memory heirarchy (ie L1->L2).
37 */
38
39#include <cstring>
40#include <iostream>
41
42#include "base/cprintf.hh"
43#include "base/misc.hh"
44#include "base/trace.hh"
45#include "mem/packet.hh"
46
47using namespace std;
48
49// The one downside to bitsets is that static initializers can get ugly.
50#define SET1(a1)                     (1 << (a1))
51#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
52#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
53#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
54#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
55#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
56
57const MemCmd::CommandInfo
58MemCmd::commandInfo[] =
59{
60    /* InvalidCmd */
61    { 0, InvalidCmd, "InvalidCmd" },
62    /* ReadReq */
63    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
64    /* ReadResp */
65    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
66    /* ReadRespWithInvalidate */
67    { SET4(IsRead, IsResponse, HasData, IsInvalidate),
68            InvalidCmd, "ReadRespWithInvalidate" },
69    /* WriteReq */
70    { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
71            WriteResp, "WriteReq" },
72    /* WriteResp */
73    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
74    /* Writeback */
75    { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
76            InvalidCmd, "Writeback" },
77    /* SoftPFReq */
78    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
79            SoftPFResp, "SoftPFReq" },
80    /* HardPFReq */
81    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
82            HardPFResp, "HardPFReq" },
83    /* SoftPFResp */
84    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
85            InvalidCmd, "SoftPFResp" },
86    /* HardPFResp */
87    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
88            InvalidCmd, "HardPFResp" },
89    /* WriteInvalidateReq */
90    { SET6(IsWrite, NeedsExclusive, IsInvalidate,
91           IsRequest, HasData, NeedsResponse),
92            WriteInvalidateResp, "WriteInvalidateReq" },
93    /* WriteInvalidateResp */
94    { SET3(IsWrite, NeedsExclusive, IsResponse),
95            InvalidCmd, "WriteInvalidateResp" },
96    /* UpgradeReq */
97    { SET5(IsInvalidate, NeedsExclusive, IsUpgrade, IsRequest, NeedsResponse),
98            UpgradeResp, "UpgradeReq" },
99    /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */
100    { SET6(IsInvalidate, NeedsExclusive, IsUpgrade, IsLlsc,
101           IsRequest, NeedsResponse),
102            UpgradeResp, "SCUpgradeReq" },
103    /* UpgradeResp */
104    { SET3(NeedsExclusive, IsUpgrade, IsResponse),
105            InvalidCmd, "UpgradeResp" },
106    /* SCUpgradeFailReq: generates UpgradeFailResp ASAP */
107    { SET5(IsInvalidate, NeedsExclusive, IsLlsc,
108           IsRequest, NeedsResponse),
109            UpgradeFailResp, "SCUpgradeFailReq" },
110    /* UpgradeFailResp */
111    { SET2(NeedsExclusive, IsResponse),
112            InvalidCmd, "UpgradeFailResp" },
113    /* ReadExReq */
114    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
115            ReadExResp, "ReadExReq" },
116    /* ReadExResp */
117    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
118            InvalidCmd, "ReadExResp" },
119    /* LoadLockedReq: note that we use plain ReadResp as response, so that
120     *                we can also use ReadRespWithInvalidate when needed */
121    { SET4(IsRead, IsLlsc, IsRequest, NeedsResponse),
122            ReadResp, "LoadLockedReq" },
123    /* StoreCondReq */
124    { SET6(IsWrite, NeedsExclusive, IsLlsc,
125           IsRequest, NeedsResponse, HasData),
126            StoreCondResp, "StoreCondReq" },
127    /* StoreCondFailReq: generates failing StoreCondResp ASAP */
128    { SET6(IsWrite, NeedsExclusive, IsLlsc,
129           IsRequest, NeedsResponse, HasData),
130            StoreCondResp, "StoreCondFailReq" },
131    /* StoreCondResp */
132    { SET4(IsWrite, NeedsExclusive, IsLlsc, IsResponse),
133            InvalidCmd, "StoreCondResp" },
134    /* SwapReq -- for Swap ldstub type operations */
135    { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
136        SwapResp, "SwapReq" },
137    /* SwapResp -- for Swap ldstub type operations */
138    { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
139            InvalidCmd, "SwapResp" },
140    /* IntReq -- for interrupts */
141    { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
142        MessageResp, "MessageReq" },
143    /* IntResp -- for interrupts */
144    { SET2(IsWrite, IsResponse), InvalidCmd, "MessageResp" },
145    /* NetworkNackError  -- nacked at network layer (not by protocol) */
146    { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
147    /* InvalidDestError  -- packet dest field invalid */
148    { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
149    /* BadAddressError   -- memory address invalid */
150    { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
151    /* PrintReq */
152    { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" },
153    /* Flush Request */
154    { SET3(IsRequest, IsFlush, NeedsExclusive), InvalidCmd, "FlushReq" }
155};
156
157bool
158Packet::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
159{
160    Addr func_start = getAddr();
161    Addr func_end   = getAddr() + getSize() - 1;
162    Addr val_start  = addr;
163    Addr val_end    = val_start + size - 1;
164
165    if (func_start > val_end || val_start > func_end) {
166        // no intersection
167        return false;
168    }
169
170    // check print first since it doesn't require data
171    if (isPrint()) {
172        dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
173        return false;
174    }
175
176    // if there's no data, there's no need to look further
177    if (!data) {
178        return false;
179    }
180
181    // offset of functional request into supplied value (could be
182    // negative if partial overlap)
183    int offset = func_start - val_start;
184
185    if (isRead()) {
186        if (func_start >= val_start && func_end <= val_end) {
187            allocate();
188            memcpy(getPtr<uint8_t>(), data + offset, getSize());
189            return true;
190        } else {
191            // In this case the timing packet only partially satisfies
192            // the request, so we would need more information to make
193            // this work.  Like bytes valid in the packet or
194            // something, so the request could continue and get this
195            // bit of possibly newer data along with the older data
196            // not written to yet.
197            panic("Memory value only partially satisfies the functional "
198                  "request. Now what?");
199        }
200    } else if (isWrite()) {
201        if (offset >= 0) {
202            memcpy(data + offset, getPtr<uint8_t>(),
203                   (min(func_end, val_end) - func_start) + 1);
204        } else {
205            // val_start > func_start
206            memcpy(data, getPtr<uint8_t>() - offset,
207                   (min(func_end, val_end) - val_start) + 1);
208        }
209    } else {
210        panic("Don't know how to handle command %s\n", cmdString());
211    }
212
213    // keep going with request by default
214    return false;
215}
216
217void
218Packet::print(ostream &o, const int verbosity, const string &prefix) const
219{
220    ccprintf(o, "%s[%x:%x] %s\n", prefix,
221             getAddr(), getAddr() + getSize() - 1, cmdString());
222}
223
224Packet::PrintReqState::PrintReqState(ostream &_os, int _verbosity)
225    : curPrefixPtr(new string("")), os(_os), verbosity(_verbosity)
226{
227    labelStack.push_back(LabelStackEntry("", curPrefixPtr));
228}
229
230Packet::PrintReqState::~PrintReqState()
231{
232    labelStack.pop_back();
233    assert(labelStack.empty());
234    delete curPrefixPtr;
235}
236
237Packet::PrintReqState::
238LabelStackEntry::LabelStackEntry(const string &_label, string *_prefix)
239    : label(_label), prefix(_prefix), labelPrinted(false)
240{
241}
242
243void
244Packet::PrintReqState::pushLabel(const string &lbl, const string &prefix)
245{
246    labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
247    curPrefixPtr = new string(*curPrefixPtr);
248    *curPrefixPtr += prefix;
249}
250
251void
252Packet::PrintReqState::popLabel()
253{
254    delete curPrefixPtr;
255    curPrefixPtr = labelStack.back().prefix;
256    labelStack.pop_back();
257    assert(!labelStack.empty());
258}
259
260void
261Packet::PrintReqState::printLabels()
262{
263    if (!labelStack.back().labelPrinted) {
264        LabelStack::iterator i = labelStack.begin();
265        LabelStack::iterator end = labelStack.end();
266        while (i != end) {
267            if (!i->labelPrinted) {
268                ccprintf(os, "%s%s\n", *(i->prefix), i->label);
269                i->labelPrinted = true;
270            }
271            i++;
272        }
273    }
274}
275
276
277void
278Packet::PrintReqState::printObj(Printable *obj)
279{
280    printLabels();
281    obj->print(os, verbosity, curPrefix());
282}
283