2 * Copyright (c) 2011-2015 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,2015 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" },
74 /* ReadReq - Read issued by a non-caching agent such as a CPU or
75 * device, with no restrictions on alignment. */
76 { SET3(IsRead
, IsRequest
, NeedsResponse
), ReadResp
, "ReadReq" },
78 { SET3(IsRead
, IsResponse
, HasData
), InvalidCmd
, "ReadResp" },
79 /* ReadRespWithInvalidate */
80 { SET4(IsRead
, IsResponse
, HasData
, IsInvalidate
),
81 InvalidCmd
, "ReadRespWithInvalidate" },
83 { SET5(IsWrite
, NeedsExclusive
, IsRequest
, NeedsResponse
, HasData
),
84 WriteResp
, "WriteReq" },
86 { SET3(IsWrite
, NeedsExclusive
, IsResponse
), InvalidCmd
, "WriteResp" },
88 { SET4(IsWrite
, NeedsExclusive
, IsRequest
, HasData
),
89 InvalidCmd
, "Writeback" },
91 { SET2(IsWrite
, IsRequest
), InvalidCmd
, "CleanEvict" },
93 { SET4(IsRead
, IsRequest
, IsSWPrefetch
, NeedsResponse
),
94 SoftPFResp
, "SoftPFReq" },
96 { SET4(IsRead
, IsRequest
, IsHWPrefetch
, NeedsResponse
),
97 HardPFResp
, "HardPFReq" },
99 { SET4(IsRead
, IsResponse
, IsSWPrefetch
, HasData
),
100 InvalidCmd
, "SoftPFResp" },
102 { SET4(IsRead
, IsResponse
, IsHWPrefetch
, HasData
),
103 InvalidCmd
, "HardPFResp" },
105 { SET5(IsWrite
, NeedsExclusive
, IsRequest
, NeedsResponse
, HasData
),
106 WriteResp
, "WriteLineReq" },
108 { SET5(IsInvalidate
, NeedsExclusive
, IsUpgrade
, IsRequest
, NeedsResponse
),
109 UpgradeResp
, "UpgradeReq" },
110 /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */
111 { SET6(IsInvalidate
, NeedsExclusive
, IsUpgrade
, IsLlsc
,
112 IsRequest
, NeedsResponse
),
113 UpgradeResp
, "SCUpgradeReq" },
115 { SET3(NeedsExclusive
, IsUpgrade
, IsResponse
),
116 InvalidCmd
, "UpgradeResp" },
117 /* SCUpgradeFailReq: generates UpgradeFailResp but still gets the data */
118 { SET6(IsRead
, NeedsExclusive
, IsInvalidate
,
119 IsLlsc
, IsRequest
, NeedsResponse
),
120 UpgradeFailResp
, "SCUpgradeFailReq" },
121 /* UpgradeFailResp - Behaves like a ReadExReq, but notifies an SC
122 * that it has failed, acquires line as Dirty*/
123 { SET4(IsRead
, NeedsExclusive
, IsResponse
, HasData
),
124 InvalidCmd
, "UpgradeFailResp" },
125 /* ReadExReq - Read issues by a cache, always cache-line aligned,
126 * and the response is guaranteed to be writeable (exclusive or
128 { SET5(IsRead
, NeedsExclusive
, IsInvalidate
, IsRequest
, NeedsResponse
),
129 ReadExResp
, "ReadExReq" },
130 /* ReadExResp - Response matching a read exclusive, as we check
131 * the need for exclusive also on responses */
132 { SET4(IsRead
, NeedsExclusive
, IsResponse
, HasData
),
133 InvalidCmd
, "ReadExResp" },
134 /* ReadCleanReq - Read issued by a cache, always cache-line
135 * aligned, and the response is guaranteed to not contain dirty data
136 * (exclusive or shared).*/
137 { SET3(IsRead
, IsRequest
, NeedsResponse
), ReadResp
, "ReadCleanReq" },
138 /* ReadSharedReq - Read issued by a cache, always cache-line
139 * aligned, response is shared, possibly exclusive, owned or even
141 { SET3(IsRead
, IsRequest
, NeedsResponse
), ReadResp
, "ReadSharedReq" },
142 /* LoadLockedReq: note that we use plain ReadResp as response, so that
143 * we can also use ReadRespWithInvalidate when needed */
144 { SET4(IsRead
, IsLlsc
, IsRequest
, NeedsResponse
),
145 ReadResp
, "LoadLockedReq" },
147 { SET6(IsWrite
, NeedsExclusive
, IsLlsc
,
148 IsRequest
, NeedsResponse
, HasData
),
149 StoreCondResp
, "StoreCondReq" },
150 /* StoreCondFailReq: generates failing StoreCondResp */
151 { SET6(IsWrite
, NeedsExclusive
, IsLlsc
,
152 IsRequest
, NeedsResponse
, HasData
),
153 StoreCondResp
, "StoreCondFailReq" },
155 { SET4(IsWrite
, NeedsExclusive
, IsLlsc
, IsResponse
),
156 InvalidCmd
, "StoreCondResp" },
157 /* SwapReq -- for Swap ldstub type operations */
158 { SET6(IsRead
, IsWrite
, NeedsExclusive
, IsRequest
, HasData
, NeedsResponse
),
159 SwapResp
, "SwapReq" },
160 /* SwapResp -- for Swap ldstub type operations */
161 { SET5(IsRead
, IsWrite
, NeedsExclusive
, IsResponse
, HasData
),
162 InvalidCmd
, "SwapResp" },
163 /* IntReq -- for interrupts */
164 { SET4(IsWrite
, IsRequest
, NeedsResponse
, HasData
),
165 MessageResp
, "MessageReq" },
166 /* IntResp -- for interrupts */
167 { SET2(IsWrite
, IsResponse
), InvalidCmd
, "MessageResp" },
168 /* ReleaseReq -- for release synchronization */
169 { SET2(IsRequest
, NeedsResponse
), ReleaseResp
, "ReleaseReq" },
170 /* ReleaseResp -- for release synchronization */
171 { SET1(IsResponse
), InvalidCmd
, "ReleaseResp" },
172 /* AcquireReq -- for release synchronization */
173 { SET2(IsRequest
, NeedsResponse
), AcquireResp
, "AcquireReq" },
174 /* AcquireResp -- for release synchronization */
175 { SET2(IsResponse
, NeedsResponse
), InvalidCmd
, "AcquireResp" },
176 /* InvalidDestError -- packet dest field invalid */
177 { SET2(IsResponse
, IsError
), InvalidCmd
, "InvalidDestError" },
178 /* BadAddressError -- memory address invalid */
179 { SET2(IsResponse
, IsError
), InvalidCmd
, "BadAddressError" },
180 /* FunctionalReadError */
181 { SET3(IsRead
, IsResponse
, IsError
), InvalidCmd
, "FunctionalReadError" },
182 /* FunctionalWriteError */
183 { SET3(IsWrite
, IsResponse
, IsError
), InvalidCmd
, "FunctionalWriteError" },
185 { SET2(IsRequest
, IsPrint
), InvalidCmd
, "PrintReq" },
187 { SET3(IsRequest
, IsFlush
, NeedsExclusive
), InvalidCmd
, "FlushReq" },
188 /* Invalidation Request */
189 { SET4(IsInvalidate
, IsRequest
, NeedsExclusive
, NeedsResponse
),
190 InvalidateResp
, "InvalidateReq" },
191 /* Invalidation Response */
192 { SET3(IsInvalidate
, IsResponse
, NeedsExclusive
),
193 InvalidCmd
, "InvalidateResp" }
197 Packet::checkFunctional(Printable
*obj
, Addr addr
, bool is_secure
, int size
,
200 Addr func_start
= getAddr();
201 Addr func_end
= getAddr() + getSize() - 1;
202 Addr val_start
= addr
;
203 Addr val_end
= val_start
+ size
- 1;
205 if (is_secure
!= _isSecure
|| func_start
> val_end
||
206 val_start
> func_end
) {
211 // check print first since it doesn't require data
214 safe_cast
<PrintReqState
*>(senderState
)->printObj(obj
);
218 // we allow the caller to pass NULL to signify the other packet
224 // offset of functional request into supplied value (could be
225 // negative if partial overlap)
226 int offset
= func_start
- val_start
;
229 if (func_start
>= val_start
&& func_end
<= val_end
) {
230 memcpy(getPtr
<uint8_t>(), _data
+ offset
, getSize());
231 if (bytesValid
.empty())
232 bytesValid
.resize(getSize(), true);
233 // complete overlap, and as the current packet is a read
237 // Offsets and sizes to copy in case of partial overlap
242 // calculate offsets and copy sizes for the two byte arrays
243 if (val_start
< func_start
&& val_end
<= func_end
) {
244 // the one we are checking against starts before and
245 // ends before or the same
246 val_offset
= func_start
- val_start
;
248 overlap_size
= val_end
- func_start
;
249 } else if (val_start
>= func_start
&& val_end
> func_end
) {
250 // the one we are checking against starts after or the
251 // same, and ends after
253 func_offset
= val_start
- func_start
;
254 overlap_size
= func_end
- val_start
;
255 } else if (val_start
>= func_start
&& val_end
<= func_end
) {
256 // the one we are checking against is completely
257 // subsumed in the current packet, possibly starting
258 // and ending at the same address
260 func_offset
= val_start
- func_start
;
262 } else if (val_start
< func_start
&& val_end
> func_end
) {
263 // the current packet is completely subsumed in the
264 // one we are checking against
265 val_offset
= func_start
- val_start
;
267 overlap_size
= func_end
- func_start
;
269 panic("Missed a case for checkFunctional with "
270 " %s 0x%x size %d, against 0x%x size %d\n",
271 cmdString(), getAddr(), getSize(), addr
, size
);
274 // copy partial data into the packet's data array
275 uint8_t *dest
= getPtr
<uint8_t>() + func_offset
;
276 uint8_t *src
= _data
+ val_offset
;
277 memcpy(dest
, src
, overlap_size
);
279 // initialise the tracking of valid bytes if we have not
281 if (bytesValid
.empty())
282 bytesValid
.resize(getSize(), false);
284 // track if we are done filling the functional access
285 bool all_bytes_valid
= true;
289 // check up to func_offset
290 for (; all_bytes_valid
&& i
< func_offset
; ++i
)
291 all_bytes_valid
&= bytesValid
[i
];
293 // update the valid bytes
294 for (i
= func_offset
; i
< func_offset
+ overlap_size
; ++i
)
295 bytesValid
[i
] = true;
297 // check the bit after the update we just made
298 for (; all_bytes_valid
&& i
< getSize(); ++i
)
299 all_bytes_valid
&= bytesValid
[i
];
301 return all_bytes_valid
;
303 } else if (isWrite()) {
305 memcpy(_data
+ offset
, getConstPtr
<uint8_t>(),
306 (min(func_end
, val_end
) - func_start
) + 1);
308 // val_start > func_start
309 memcpy(_data
, getConstPtr
<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::pushSenderState(Packet::SenderState
*sender_state
)
323 assert(sender_state
!= NULL
);
324 sender_state
->predecessor
= senderState
;
325 senderState
= sender_state
;
328 Packet::SenderState
*
329 Packet::popSenderState()
331 assert(senderState
!= NULL
);
332 SenderState
*sender_state
= senderState
;
333 senderState
= sender_state
->predecessor
;
334 sender_state
->predecessor
= NULL
;
339 Packet::print(ostream
&o
, const int verbosity
, const string
&prefix
) const
341 ccprintf(o
, "%s[%x:%x] %s\n", prefix
,
342 getAddr(), getAddr() + getSize() - 1, cmdString());
346 Packet::print() const {
352 Packet::PrintReqState::PrintReqState(ostream
&_os
, int _verbosity
)
353 : curPrefixPtr(new string("")), os(_os
), verbosity(_verbosity
)
355 labelStack
.push_back(LabelStackEntry("", curPrefixPtr
));
358 Packet::PrintReqState::~PrintReqState()
360 labelStack
.pop_back();
361 assert(labelStack
.empty());
365 Packet::PrintReqState::
366 LabelStackEntry::LabelStackEntry(const string
&_label
, string
*_prefix
)
367 : label(_label
), prefix(_prefix
), labelPrinted(false)
372 Packet::PrintReqState::pushLabel(const string
&lbl
, const string
&prefix
)
374 labelStack
.push_back(LabelStackEntry(lbl
, curPrefixPtr
));
375 curPrefixPtr
= new string(*curPrefixPtr
);
376 *curPrefixPtr
+= prefix
;
380 Packet::PrintReqState::popLabel()
383 curPrefixPtr
= labelStack
.back().prefix
;
384 labelStack
.pop_back();
385 assert(!labelStack
.empty());
389 Packet::PrintReqState::printLabels()
391 if (!labelStack
.back().labelPrinted
) {
392 LabelStack::iterator i
= labelStack
.begin();
393 LabelStack::iterator end
= labelStack
.end();
395 if (!i
->labelPrinted
) {
396 ccprintf(os
, "%s%s\n", *(i
->prefix
), i
->label
);
397 i
->labelPrinted
= true;
406 Packet::PrintReqState::printObj(Printable
*obj
)
409 obj
->print(os
, verbosity
, curPrefix());