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