2 * Copyright (c) 2011 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * Definition of the Packet Class, a packet is a transaction occuring
48 * between a single level of the memory heirarchy (ie L1->L2).
54 #include "base/cprintf.hh"
55 #include "base/misc.hh"
56 #include "base/trace.hh"
57 #include "mem/packet.hh"
61 // The one downside to bitsets is that static initializers can get ugly.
62 #define SET1(a1) (1 << (a1))
63 #define SET2(a1, a2) (SET1(a1) | SET1(a2))
64 #define SET3(a1, a2, a3) (SET2(a1, a2) | SET1(a3))
65 #define SET4(a1, a2, a3, a4) (SET3(a1, a2, a3) | SET1(a4))
66 #define SET5(a1, a2, a3, a4, a5) (SET4(a1, a2, a3, a4) | SET1(a5))
67 #define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
69 const MemCmd::CommandInfo
70 MemCmd::commandInfo
[] =
73 { 0, InvalidCmd
, "InvalidCmd" },
75 { SET3(IsRead
, IsRequest
, NeedsResponse
), ReadResp
, "ReadReq" },
77 { SET3(IsRead
, IsResponse
, HasData
), InvalidCmd
, "ReadResp" },
78 /* ReadRespWithInvalidate */
79 { SET4(IsRead
, IsResponse
, HasData
, IsInvalidate
),
80 InvalidCmd
, "ReadRespWithInvalidate" },
82 { SET5(IsWrite
, NeedsExclusive
, IsRequest
, NeedsResponse
, HasData
),
83 WriteResp
, "WriteReq" },
85 { SET3(IsWrite
, NeedsExclusive
, IsResponse
), InvalidCmd
, "WriteResp" },
87 { SET4(IsWrite
, NeedsExclusive
, IsRequest
, HasData
),
88 InvalidCmd
, "Writeback" },
90 { SET4(IsRead
, IsRequest
, IsSWPrefetch
, NeedsResponse
),
91 SoftPFResp
, "SoftPFReq" },
93 { SET4(IsRead
, IsRequest
, IsHWPrefetch
, NeedsResponse
),
94 HardPFResp
, "HardPFReq" },
96 { SET4(IsRead
, IsResponse
, IsSWPrefetch
, HasData
),
97 InvalidCmd
, "SoftPFResp" },
99 { SET4(IsRead
, IsResponse
, IsHWPrefetch
, HasData
),
100 InvalidCmd
, "HardPFResp" },
101 /* WriteInvalidateReq (currently unused, see packet.hh) */
102 { SET6(IsWrite
, NeedsExclusive
, IsInvalidate
,
103 IsRequest
, HasData
, NeedsResponse
),
104 WriteInvalidateResp
, "WriteInvalidateReq" },
105 /* WriteInvalidateResp (currently unused, see packet.hh) */
106 { SET3(IsWrite
, NeedsExclusive
, IsResponse
),
107 InvalidCmd
, "WriteInvalidateResp" },
109 { SET5(IsInvalidate
, NeedsExclusive
, IsUpgrade
, IsRequest
, NeedsResponse
),
110 UpgradeResp
, "UpgradeReq" },
111 /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */
112 { SET6(IsInvalidate
, NeedsExclusive
, IsUpgrade
, IsLlsc
,
113 IsRequest
, NeedsResponse
),
114 UpgradeResp
, "SCUpgradeReq" },
116 { SET3(NeedsExclusive
, IsUpgrade
, IsResponse
),
117 InvalidCmd
, "UpgradeResp" },
118 /* SCUpgradeFailReq: generates UpgradeFailResp ASAP */
119 { SET5(IsInvalidate
, NeedsExclusive
, IsLlsc
,
120 IsRequest
, NeedsResponse
),
121 UpgradeFailResp
, "SCUpgradeFailReq" },
122 /* UpgradeFailResp */
123 { SET2(NeedsExclusive
, IsResponse
),
124 InvalidCmd
, "UpgradeFailResp" },
126 { SET5(IsRead
, NeedsExclusive
, IsInvalidate
, IsRequest
, NeedsResponse
),
127 ReadExResp
, "ReadExReq" },
129 { SET4(IsRead
, NeedsExclusive
, IsResponse
, HasData
),
130 InvalidCmd
, "ReadExResp" },
131 /* LoadLockedReq: note that we use plain ReadResp as response, so that
132 * we can also use ReadRespWithInvalidate when needed */
133 { SET4(IsRead
, IsLlsc
, IsRequest
, NeedsResponse
),
134 ReadResp
, "LoadLockedReq" },
136 { SET6(IsWrite
, NeedsExclusive
, IsLlsc
,
137 IsRequest
, NeedsResponse
, HasData
),
138 StoreCondResp
, "StoreCondReq" },
139 /* StoreCondFailReq: generates failing StoreCondResp ASAP */
140 { SET6(IsWrite
, NeedsExclusive
, IsLlsc
,
141 IsRequest
, NeedsResponse
, HasData
),
142 StoreCondResp
, "StoreCondFailReq" },
144 { SET4(IsWrite
, NeedsExclusive
, IsLlsc
, IsResponse
),
145 InvalidCmd
, "StoreCondResp" },
146 /* SwapReq -- for Swap ldstub type operations */
147 { SET6(IsRead
, IsWrite
, NeedsExclusive
, IsRequest
, HasData
, NeedsResponse
),
148 SwapResp
, "SwapReq" },
149 /* SwapResp -- for Swap ldstub type operations */
150 { SET5(IsRead
, IsWrite
, NeedsExclusive
, IsResponse
, HasData
),
151 InvalidCmd
, "SwapResp" },
152 /* IntReq -- for interrupts */
153 { SET4(IsWrite
, IsRequest
, NeedsResponse
, HasData
),
154 MessageResp
, "MessageReq" },
155 /* IntResp -- for interrupts */
156 { SET2(IsWrite
, IsResponse
), InvalidCmd
, "MessageResp" },
157 /* NetworkNackError -- nacked at network layer (not by protocol) */
158 { SET2(IsResponse
, IsError
), InvalidCmd
, "NetworkNackError" },
159 /* InvalidDestError -- packet dest field invalid */
160 { SET2(IsResponse
, IsError
), InvalidCmd
, "InvalidDestError" },
161 /* BadAddressError -- memory address invalid */
162 { SET2(IsResponse
, IsError
), InvalidCmd
, "BadAddressError" },
163 /* FunctionalReadError */
164 { SET3(IsRead
, IsResponse
, IsError
), InvalidCmd
, "FunctionalReadError" },
165 /* FunctionalWriteError */
166 { SET3(IsWrite
, IsResponse
, IsError
), InvalidCmd
, "FunctionalWriteError" },
168 { SET2(IsRequest
, IsPrint
), InvalidCmd
, "PrintReq" },
170 { SET3(IsRequest
, IsFlush
, NeedsExclusive
), InvalidCmd
, "FlushReq" },
171 /* Invalidation Request */
172 { SET3(NeedsExclusive
, IsInvalidate
, IsRequest
),
173 InvalidCmd
, "InvalidationReq" },
177 Packet::checkFunctional(Printable
*obj
, Addr addr
, int size
, uint8_t *data
)
179 Addr func_start
= getAddr();
180 Addr func_end
= getAddr() + getSize() - 1;
181 Addr val_start
= addr
;
182 Addr val_end
= val_start
+ size
- 1;
184 if (func_start
> val_end
|| val_start
> func_end
) {
189 // check print first since it doesn't require data
191 dynamic_cast<PrintReqState
*>(senderState
)->printObj(obj
);
195 // if there's no data, there's no need to look further
200 // offset of functional request into supplied value (could be
201 // negative if partial overlap)
202 int offset
= func_start
- val_start
;
205 if (func_start
>= val_start
&& func_end
<= val_end
) {
207 memcpy(getPtr
<uint8_t>(), data
+ offset
, getSize());
210 // Offsets and sizes to copy in case of partial overlap
215 // calculate offsets and copy sizes for the two byte arrays
216 if (val_start
< func_start
&& val_end
<= func_end
) {
217 val_offset
= func_start
- val_start
;
219 overlap_size
= val_end
- func_start
;
220 } else if (val_start
>= func_start
&& val_end
> func_end
) {
222 func_offset
= val_start
- func_start
;
223 overlap_size
= func_end
- val_start
;
224 } else if (val_start
>= func_start
&& val_end
<= func_end
) {
226 func_offset
= val_start
- func_start
;
229 panic("BUG: Missed a case for a partial functional request");
232 // Figure out how much of the partial overlap should be copied
233 // into the packet and not overwrite previously found bytes.
234 if (bytesValidStart
== 0 && bytesValidEnd
== 0) {
235 // No bytes have been copied yet, just set indices
237 bytesValidStart
= func_offset
;
238 bytesValidEnd
= func_offset
+ overlap_size
;
240 // Some bytes have already been copied. Use bytesValid
241 // indices and offset values to figure out how much data
242 // to copy and where to copy it to.
244 // Indice overlap conditions to check
245 int a
= func_offset
- bytesValidStart
;
246 int b
= (func_offset
+ overlap_size
) - bytesValidEnd
;
247 int c
= func_offset
- bytesValidEnd
;
248 int d
= (func_offset
+ overlap_size
) - bytesValidStart
;
250 if (a
>= 0 && b
<= 0) {
251 // bytes already in pkt data array are superset of
252 // found bytes, will not copy any bytes
254 } else if (a
< 0 && d
>= 0 && b
<= 0) {
255 // found bytes will move bytesValidStart towards 0
256 overlap_size
= bytesValidStart
- func_offset
;
257 bytesValidStart
= func_offset
;
258 } else if (b
> 0 && c
<= 0 && a
>= 0) {
259 // found bytes will move bytesValidEnd
260 // towards end of pkt data array
262 (func_offset
+ overlap_size
) - bytesValidEnd
;
263 val_offset
+= bytesValidEnd
- func_offset
;
264 func_offset
= bytesValidEnd
;
265 bytesValidEnd
+= overlap_size
;
266 } else if (a
< 0 && b
> 0) {
267 // Found bytes are superset of copied range. Will move
268 // bytesValidStart towards 0 and bytesValidEnd towards
269 // end of pkt data array. Need to break copy into two
270 // pieces so as to not overwrite previously found data.
272 // copy the first half
273 uint8_t *dest
= getPtr
<uint8_t>() + func_offset
;
274 uint8_t *src
= data
+ val_offset
;
275 memcpy(dest
, src
, (bytesValidStart
- func_offset
));
277 // re-calc the offsets and indices to do the copy
278 // required for the second half
279 val_offset
+= (bytesValidEnd
- func_offset
);
280 bytesValidStart
= func_offset
;
282 (func_offset
+ overlap_size
) - bytesValidEnd
;
283 func_offset
= bytesValidEnd
;
284 bytesValidEnd
+= overlap_size
;
285 } else if ((c
> 0 && b
> 0)
286 || (a
< 0 && d
< 0)) {
287 // region to be copied is discontiguous! Not supported.
288 panic("BUG: Discontiguous bytes found"
289 "for functional copying!");
292 assert(bytesValidEnd
<= getSize());
294 // copy partial data into the packet's data array
295 uint8_t *dest
= getPtr
<uint8_t>() + func_offset
;
296 uint8_t *src
= data
+ val_offset
;
297 memcpy(dest
, src
, overlap_size
);
299 // check if we're done filling the functional access
300 bool done
= (bytesValidStart
== 0) && (bytesValidEnd
== getSize());
303 } else if (isWrite()) {
305 memcpy(data
+ offset
, getPtr
<uint8_t>(),
306 (min(func_end
, val_end
) - func_start
) + 1);
308 // val_start > func_start
309 memcpy(data
, getPtr
<uint8_t>() - offset
,
310 (min(func_end
, val_end
) - val_start
) + 1);
313 panic("Don't know how to handle command %s\n", cmdString());
316 // keep going with request by default
321 Packet::print(ostream
&o
, const int verbosity
, const string
&prefix
) const
323 ccprintf(o
, "%s[%x:%x] %s\n", prefix
,
324 getAddr(), getAddr() + getSize() - 1, cmdString());
327 Packet::PrintReqState::PrintReqState(ostream
&_os
, int _verbosity
)
328 : curPrefixPtr(new string("")), os(_os
), verbosity(_verbosity
)
330 labelStack
.push_back(LabelStackEntry("", curPrefixPtr
));
333 Packet::PrintReqState::~PrintReqState()
335 labelStack
.pop_back();
336 assert(labelStack
.empty());
340 Packet::PrintReqState::
341 LabelStackEntry::LabelStackEntry(const string
&_label
, string
*_prefix
)
342 : label(_label
), prefix(_prefix
), labelPrinted(false)
347 Packet::PrintReqState::pushLabel(const string
&lbl
, const string
&prefix
)
349 labelStack
.push_back(LabelStackEntry(lbl
, curPrefixPtr
));
350 curPrefixPtr
= new string(*curPrefixPtr
);
351 *curPrefixPtr
+= prefix
;
355 Packet::PrintReqState::popLabel()
358 curPrefixPtr
= labelStack
.back().prefix
;
359 labelStack
.pop_back();
360 assert(!labelStack
.empty());
364 Packet::PrintReqState::printLabels()
366 if (!labelStack
.back().labelPrinted
) {
367 LabelStack::iterator i
= labelStack
.begin();
368 LabelStack::iterator end
= labelStack
.end();
370 if (!i
->labelPrinted
) {
371 ccprintf(os
, "%s%s\n", *(i
->prefix
), i
->label
);
372 i
->labelPrinted
= true;
381 Packet::PrintReqState::printObj(Printable
*obj
)
384 obj
->print(os
, verbosity
, curPrefix());