ruby: cleaning up RubyQueue and RubyNetwork dprintfs
[gem5.git] / src / mem / packet.cc
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 <iostream>
40 #include <cstring>
41 #include "base/cprintf.hh"
42 #include "base/misc.hh"
43 #include "base/trace.hh"
44 #include "mem/packet.hh"
45
46 using namespace std;
47
48 // The one downside to bitsets is that static initializers can get ugly.
49 #define SET1(a1) (1 << (a1))
50 #define SET2(a1, a2) (SET1(a1) | SET1(a2))
51 #define SET3(a1, a2, a3) (SET2(a1, a2) | SET1(a3))
52 #define SET4(a1, a2, a3, a4) (SET3(a1, a2, a3) | SET1(a4))
53 #define SET5(a1, a2, a3, a4, a5) (SET4(a1, a2, a3, a4) | SET1(a5))
54 #define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
55
56 const MemCmd::CommandInfo
57 MemCmd::commandInfo[] =
58 {
59 /* InvalidCmd */
60 { 0, InvalidCmd, "InvalidCmd" },
61 /* ReadReq */
62 { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
63 /* ReadResp */
64 { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
65 /* ReadRespWithInvalidate */
66 { SET4(IsRead, IsResponse, HasData, IsInvalidate),
67 InvalidCmd, "ReadRespWithInvalidate" },
68 /* WriteReq */
69 { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
70 WriteResp, "WriteReq" },
71 /* WriteResp */
72 { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
73 /* Writeback */
74 { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
75 InvalidCmd, "Writeback" },
76 /* SoftPFReq */
77 { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
78 SoftPFResp, "SoftPFReq" },
79 /* HardPFReq */
80 { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
81 HardPFResp, "HardPFReq" },
82 /* SoftPFResp */
83 { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
84 InvalidCmd, "SoftPFResp" },
85 /* HardPFResp */
86 { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
87 InvalidCmd, "HardPFResp" },
88 /* WriteInvalidateReq */
89 { SET6(IsWrite, NeedsExclusive, IsInvalidate,
90 IsRequest, HasData, NeedsResponse),
91 WriteInvalidateResp, "WriteInvalidateReq" },
92 /* WriteInvalidateResp */
93 { SET3(IsWrite, NeedsExclusive, IsResponse),
94 InvalidCmd, "WriteInvalidateResp" },
95 /* UpgradeReq */
96 { SET5(IsInvalidate, NeedsExclusive, IsUpgrade, IsRequest, NeedsResponse),
97 UpgradeResp, "UpgradeReq" },
98 /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */
99 { SET6(IsInvalidate, NeedsExclusive, IsUpgrade, IsLlsc,
100 IsRequest, NeedsResponse),
101 UpgradeResp, "SCUpgradeReq" },
102 /* UpgradeResp */
103 { SET3(NeedsExclusive, IsUpgrade, IsResponse),
104 InvalidCmd, "UpgradeResp" },
105 /* SCUpgradeFailReq: generates UpgradeFailResp ASAP */
106 { SET5(IsInvalidate, NeedsExclusive, IsLlsc,
107 IsRequest, NeedsResponse),
108 UpgradeFailResp, "SCUpgradeFailReq" },
109 /* UpgradeFailResp */
110 { SET2(NeedsExclusive, IsResponse),
111 InvalidCmd, "UpgradeFailResp" },
112 /* ReadExReq */
113 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
114 ReadExResp, "ReadExReq" },
115 /* ReadExResp */
116 { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
117 InvalidCmd, "ReadExResp" },
118 /* LoadLockedReq: note that we use plain ReadResp as response, so that
119 * we can also use ReadRespWithInvalidate when needed */
120 { SET4(IsRead, IsLlsc, IsRequest, NeedsResponse),
121 ReadResp, "LoadLockedReq" },
122 /* StoreCondReq */
123 { SET6(IsWrite, NeedsExclusive, IsLlsc,
124 IsRequest, NeedsResponse, HasData),
125 StoreCondResp, "StoreCondReq" },
126 /* StoreCondFailReq: generates failing StoreCondResp ASAP */
127 { SET6(IsWrite, NeedsExclusive, IsLlsc,
128 IsRequest, NeedsResponse, HasData),
129 StoreCondResp, "StoreCondFailReq" },
130 /* StoreCondResp */
131 { SET4(IsWrite, NeedsExclusive, IsLlsc, IsResponse),
132 InvalidCmd, "StoreCondResp" },
133 /* SwapReq -- for Swap ldstub type operations */
134 { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
135 SwapResp, "SwapReq" },
136 /* SwapResp -- for Swap ldstub type operations */
137 { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
138 InvalidCmd, "SwapResp" },
139 /* IntReq -- for interrupts */
140 { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
141 MessageResp, "MessageReq" },
142 /* IntResp -- for interrupts */
143 { SET2(IsWrite, IsResponse), InvalidCmd, "MessageResp" },
144 /* NetworkNackError -- nacked at network layer (not by protocol) */
145 { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
146 /* InvalidDestError -- packet dest field invalid */
147 { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
148 /* BadAddressError -- memory address invalid */
149 { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
150 /* PrintReq */
151 { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
152 };
153
154 bool
155 Packet::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
156 {
157 Addr func_start = getAddr();
158 Addr func_end = getAddr() + getSize() - 1;
159 Addr val_start = addr;
160 Addr val_end = val_start + size - 1;
161
162 if (func_start > val_end || val_start > func_end) {
163 // no intersection
164 return false;
165 }
166
167 // check print first since it doesn't require data
168 if (isPrint()) {
169 dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
170 return false;
171 }
172
173 // if there's no data, there's no need to look further
174 if (!data) {
175 return false;
176 }
177
178 // offset of functional request into supplied value (could be
179 // negative if partial overlap)
180 int offset = func_start - val_start;
181
182 if (isRead()) {
183 if (func_start >= val_start && func_end <= val_end) {
184 allocate();
185 memcpy(getPtr<uint8_t>(), data + offset, getSize());
186 return true;
187 } else {
188 // In this case the timing packet only partially satisfies
189 // the request, so we would need more information to make
190 // this work. Like bytes valid in the packet or
191 // something, so the request could continue and get this
192 // bit of possibly newer data along with the older data
193 // not written to yet.
194 panic("Memory value only partially satisfies the functional "
195 "request. Now what?");
196 }
197 } else if (isWrite()) {
198 if (offset >= 0) {
199 memcpy(data + offset, getPtr<uint8_t>(),
200 (min(func_end, val_end) - func_start) + 1);
201 } else {
202 // val_start > func_start
203 memcpy(data, getPtr<uint8_t>() - offset,
204 (min(func_end, val_end) - val_start) + 1);
205 }
206 } else {
207 panic("Don't know how to handle command %s\n", cmdString());
208 }
209
210 // keep going with request by default
211 return false;
212 }
213
214 void
215 Packet::print(ostream &o, const int verbosity, const string &prefix) const
216 {
217 ccprintf(o, "%s[%x:%x] %s\n", prefix,
218 getAddr(), getAddr() + getSize() - 1, cmdString());
219 }
220
221 Packet::PrintReqState::PrintReqState(ostream &_os, int _verbosity)
222 : curPrefixPtr(new string("")), os(_os), verbosity(_verbosity)
223 {
224 labelStack.push_back(LabelStackEntry("", curPrefixPtr));
225 }
226
227 Packet::PrintReqState::~PrintReqState()
228 {
229 labelStack.pop_back();
230 assert(labelStack.empty());
231 delete curPrefixPtr;
232 }
233
234 Packet::PrintReqState::
235 LabelStackEntry::LabelStackEntry(const string &_label, string *_prefix)
236 : label(_label), prefix(_prefix), labelPrinted(false)
237 {
238 }
239
240 void
241 Packet::PrintReqState::pushLabel(const string &lbl, const string &prefix)
242 {
243 labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
244 curPrefixPtr = new string(*curPrefixPtr);
245 *curPrefixPtr += prefix;
246 }
247
248 void
249 Packet::PrintReqState::popLabel()
250 {
251 delete curPrefixPtr;
252 curPrefixPtr = labelStack.back().prefix;
253 labelStack.pop_back();
254 assert(!labelStack.empty());
255 }
256
257 void
258 Packet::PrintReqState::printLabels()
259 {
260 if (!labelStack.back().labelPrinted) {
261 LabelStack::iterator i = labelStack.begin();
262 LabelStack::iterator end = labelStack.end();
263 while (i != end) {
264 if (!i->labelPrinted) {
265 ccprintf(os, "%s%s\n", *(i->prefix), i->label);
266 i->labelPrinted = true;
267 }
268 i++;
269 }
270 }
271 }
272
273
274 void
275 Packet::PrintReqState::printObj(Printable *obj)
276 {
277 printLabels();
278 obj->print(os, verbosity, curPrefix());
279 }