cache.cc (11352:4e195fb9ec4f) cache.cc (11375:f98df9231cdd)
1/*
1/*
2 * Copyright (c) 2010-2015 ARM Limited
2 * Copyright (c) 2010-2016 ARM Limited
3 * All rights reserved.
4 *
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.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
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.
28 *
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.
40 *
41 * Authors: Erik Hallnor
42 * Dave Greene
43 * Nathan Binkert
44 * Steve Reinhardt
45 * Ron Dreslinski
46 * Andreas Sandberg
47 */
48
49/**
50 * @file
51 * Cache definitions.
52 */
53
54#include "mem/cache/cache.hh"
55
56#include "base/misc.hh"
57#include "base/types.hh"
58#include "debug/Cache.hh"
59#include "debug/CachePort.hh"
60#include "debug/CacheTags.hh"
61#include "debug/CacheVerbose.hh"
62#include "mem/cache/blk.hh"
63#include "mem/cache/mshr.hh"
64#include "mem/cache/prefetch/base.hh"
65#include "sim/sim_exit.hh"
66
67Cache::Cache(const CacheParams *p)
68 : BaseCache(p, p->system->cacheLineSize()),
69 tags(p->tags),
70 prefetcher(p->prefetcher),
71 doFastWrites(true),
72 prefetchOnAccess(p->prefetch_on_access),
73 clusivity(p->clusivity),
74 writebackClean(p->writeback_clean),
75 tempBlockWriteback(nullptr),
76 writebackTempBlockAtomicEvent(this, false,
77 EventBase::Delayed_Writeback_Pri)
78{
79 tempBlock = new CacheBlk();
80 tempBlock->data = new uint8_t[blkSize];
81
82 cpuSidePort = new CpuSidePort(p->name + ".cpu_side", this,
83 "CpuSidePort");
84 memSidePort = new MemSidePort(p->name + ".mem_side", this,
85 "MemSidePort");
86
87 tags->setCache(this);
88 if (prefetcher)
89 prefetcher->setCache(this);
90}
91
92Cache::~Cache()
93{
94 delete [] tempBlock->data;
95 delete tempBlock;
96
97 delete cpuSidePort;
98 delete memSidePort;
99}
100
101void
102Cache::regStats()
103{
104 BaseCache::regStats();
105}
106
107void
108Cache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
109{
110 assert(pkt->isRequest());
111
112 uint64_t overwrite_val;
113 bool overwrite_mem;
114 uint64_t condition_val64;
115 uint32_t condition_val32;
116
117 int offset = tags->extractBlkOffset(pkt->getAddr());
118 uint8_t *blk_data = blk->data + offset;
119
120 assert(sizeof(uint64_t) >= pkt->getSize());
121
122 overwrite_mem = true;
123 // keep a copy of our possible write value, and copy what is at the
124 // memory address into the packet
125 pkt->writeData((uint8_t *)&overwrite_val);
126 pkt->setData(blk_data);
127
128 if (pkt->req->isCondSwap()) {
129 if (pkt->getSize() == sizeof(uint64_t)) {
130 condition_val64 = pkt->req->getExtraData();
131 overwrite_mem = !std::memcmp(&condition_val64, blk_data,
132 sizeof(uint64_t));
133 } else if (pkt->getSize() == sizeof(uint32_t)) {
134 condition_val32 = (uint32_t)pkt->req->getExtraData();
135 overwrite_mem = !std::memcmp(&condition_val32, blk_data,
136 sizeof(uint32_t));
137 } else
138 panic("Invalid size for conditional read/write\n");
139 }
140
141 if (overwrite_mem) {
142 std::memcpy(blk_data, &overwrite_val, pkt->getSize());
143 blk->status |= BlkDirty;
144 }
145}
146
147
148void
149Cache::satisfyCpuSideRequest(PacketPtr pkt, CacheBlk *blk,
150 bool deferred_response, bool pending_downgrade)
151{
152 assert(pkt->isRequest());
153
154 assert(blk && blk->isValid());
155 // Occasionally this is not true... if we are a lower-level cache
156 // satisfying a string of Read and ReadEx requests from
157 // upper-level caches, a Read will mark the block as shared but we
158 // can satisfy a following ReadEx anyway since we can rely on the
159 // Read requester(s) to have buffered the ReadEx snoop and to
160 // invalidate their blocks after receiving them.
161 // assert(!pkt->needsWritable() || blk->isWritable());
162 assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
163
164 // Check RMW operations first since both isRead() and
165 // isWrite() will be true for them
166 if (pkt->cmd == MemCmd::SwapReq) {
167 cmpAndSwap(blk, pkt);
168 } else if (pkt->isWrite()) {
169 // we have the block in a writable state and can go ahead,
170 // note that the line may be also be considered writable in
171 // downstream caches along the path to memory, but always
172 // Exclusive, and never Modified
173 assert(blk->isWritable());
174 // Write or WriteLine at the first cache with block in writable state
175 if (blk->checkWrite(pkt)) {
176 pkt->writeDataToBlock(blk->data, blkSize);
177 }
178 // Always mark the line as dirty (and thus transition to the
179 // Modified state) even if we are a failed StoreCond so we
180 // supply data to any snoops that have appended themselves to
181 // this cache before knowing the store will fail.
182 blk->status |= BlkDirty;
183 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d (write)\n",
184 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
185 } else if (pkt->isRead()) {
186 if (pkt->isLLSC()) {
187 blk->trackLoadLocked(pkt);
188 }
189
190 // all read responses have a data payload
191 assert(pkt->hasRespData());
192 pkt->setDataFromBlock(blk->data, blkSize);
193
194 // determine if this read is from a (coherent) cache, or not
195 // by looking at the command type; we could potentially add a
196 // packet attribute such as 'FromCache' to make this check a
197 // bit cleaner
198 if (pkt->cmd == MemCmd::ReadExReq ||
199 pkt->cmd == MemCmd::ReadSharedReq ||
200 pkt->cmd == MemCmd::ReadCleanReq ||
201 pkt->cmd == MemCmd::SCUpgradeFailReq) {
202 assert(pkt->getSize() == blkSize);
203 // special handling for coherent block requests from
204 // upper-level caches
205 if (pkt->needsWritable()) {
206 // sanity check
207 assert(pkt->cmd == MemCmd::ReadExReq ||
208 pkt->cmd == MemCmd::SCUpgradeFailReq);
209
210 // if we have a dirty copy, make sure the recipient
211 // keeps it marked dirty (in the modified state)
212 if (blk->isDirty()) {
213 pkt->setCacheResponding();
214 }
215 // on ReadExReq we give up our copy unconditionally,
216 // even if this cache is mostly inclusive, we may want
217 // to revisit this
218 invalidateBlock(blk);
219 } else if (blk->isWritable() && !pending_downgrade &&
220 !pkt->hasSharers() &&
221 pkt->cmd != MemCmd::ReadCleanReq) {
222 // we can give the requester a writable copy on a read
223 // request if:
224 // - we have a writable copy at this level (& below)
225 // - we don't have a pending snoop from below
226 // signaling another read request
227 // - no other cache above has a copy (otherwise it
228 // would have set hasSharers flag when
229 // snooping the packet)
230 // - the read has explicitly asked for a clean
231 // copy of the line
232 if (blk->isDirty()) {
233 // special considerations if we're owner:
234 if (!deferred_response) {
235 // respond with the line in Modified state
236 // (cacheResponding set, hasSharers not set)
237 pkt->setCacheResponding();
238
239 if (clusivity == Enums::mostly_excl) {
240 // if this cache is mostly exclusive with
241 // respect to the cache above, drop the
242 // block, no need to first unset the dirty
243 // bit
244 invalidateBlock(blk);
245 } else {
246 // if this cache is mostly inclusive, we
247 // keep the block in the Exclusive state,
248 // and pass it upwards as Modified
249 // (writable and dirty), hence we have
250 // multiple caches, all on the same path
251 // towards memory, all considering the
252 // same block writable, but only one
253 // considering it Modified
254
255 // we get away with multiple caches (on
256 // the same path to memory) considering
257 // the block writeable as we always enter
258 // the cache hierarchy through a cache,
259 // and first snoop upwards in all other
260 // branches
261 blk->status &= ~BlkDirty;
262 }
263 } else {
264 // if we're responding after our own miss,
265 // there's a window where the recipient didn't
266 // know it was getting ownership and may not
267 // have responded to snoops correctly, so we
268 // have to respond with a shared line
269 pkt->setHasSharers();
270 }
271 }
272 } else {
273 // otherwise only respond with a shared copy
274 pkt->setHasSharers();
275 }
276 }
277 } else {
278 // Upgrade or Invalidate
279 assert(pkt->isUpgrade() || pkt->isInvalidate());
280
281 // for invalidations we could be looking at the temp block
282 // (for upgrades we always allocate)
283 invalidateBlock(blk);
284 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d (invalidation)\n",
285 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
286 }
287}
288
3 * All rights reserved.
4 *
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.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
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.
28 *
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.
40 *
41 * Authors: Erik Hallnor
42 * Dave Greene
43 * Nathan Binkert
44 * Steve Reinhardt
45 * Ron Dreslinski
46 * Andreas Sandberg
47 */
48
49/**
50 * @file
51 * Cache definitions.
52 */
53
54#include "mem/cache/cache.hh"
55
56#include "base/misc.hh"
57#include "base/types.hh"
58#include "debug/Cache.hh"
59#include "debug/CachePort.hh"
60#include "debug/CacheTags.hh"
61#include "debug/CacheVerbose.hh"
62#include "mem/cache/blk.hh"
63#include "mem/cache/mshr.hh"
64#include "mem/cache/prefetch/base.hh"
65#include "sim/sim_exit.hh"
66
67Cache::Cache(const CacheParams *p)
68 : BaseCache(p, p->system->cacheLineSize()),
69 tags(p->tags),
70 prefetcher(p->prefetcher),
71 doFastWrites(true),
72 prefetchOnAccess(p->prefetch_on_access),
73 clusivity(p->clusivity),
74 writebackClean(p->writeback_clean),
75 tempBlockWriteback(nullptr),
76 writebackTempBlockAtomicEvent(this, false,
77 EventBase::Delayed_Writeback_Pri)
78{
79 tempBlock = new CacheBlk();
80 tempBlock->data = new uint8_t[blkSize];
81
82 cpuSidePort = new CpuSidePort(p->name + ".cpu_side", this,
83 "CpuSidePort");
84 memSidePort = new MemSidePort(p->name + ".mem_side", this,
85 "MemSidePort");
86
87 tags->setCache(this);
88 if (prefetcher)
89 prefetcher->setCache(this);
90}
91
92Cache::~Cache()
93{
94 delete [] tempBlock->data;
95 delete tempBlock;
96
97 delete cpuSidePort;
98 delete memSidePort;
99}
100
101void
102Cache::regStats()
103{
104 BaseCache::regStats();
105}
106
107void
108Cache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
109{
110 assert(pkt->isRequest());
111
112 uint64_t overwrite_val;
113 bool overwrite_mem;
114 uint64_t condition_val64;
115 uint32_t condition_val32;
116
117 int offset = tags->extractBlkOffset(pkt->getAddr());
118 uint8_t *blk_data = blk->data + offset;
119
120 assert(sizeof(uint64_t) >= pkt->getSize());
121
122 overwrite_mem = true;
123 // keep a copy of our possible write value, and copy what is at the
124 // memory address into the packet
125 pkt->writeData((uint8_t *)&overwrite_val);
126 pkt->setData(blk_data);
127
128 if (pkt->req->isCondSwap()) {
129 if (pkt->getSize() == sizeof(uint64_t)) {
130 condition_val64 = pkt->req->getExtraData();
131 overwrite_mem = !std::memcmp(&condition_val64, blk_data,
132 sizeof(uint64_t));
133 } else if (pkt->getSize() == sizeof(uint32_t)) {
134 condition_val32 = (uint32_t)pkt->req->getExtraData();
135 overwrite_mem = !std::memcmp(&condition_val32, blk_data,
136 sizeof(uint32_t));
137 } else
138 panic("Invalid size for conditional read/write\n");
139 }
140
141 if (overwrite_mem) {
142 std::memcpy(blk_data, &overwrite_val, pkt->getSize());
143 blk->status |= BlkDirty;
144 }
145}
146
147
148void
149Cache::satisfyCpuSideRequest(PacketPtr pkt, CacheBlk *blk,
150 bool deferred_response, bool pending_downgrade)
151{
152 assert(pkt->isRequest());
153
154 assert(blk && blk->isValid());
155 // Occasionally this is not true... if we are a lower-level cache
156 // satisfying a string of Read and ReadEx requests from
157 // upper-level caches, a Read will mark the block as shared but we
158 // can satisfy a following ReadEx anyway since we can rely on the
159 // Read requester(s) to have buffered the ReadEx snoop and to
160 // invalidate their blocks after receiving them.
161 // assert(!pkt->needsWritable() || blk->isWritable());
162 assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
163
164 // Check RMW operations first since both isRead() and
165 // isWrite() will be true for them
166 if (pkt->cmd == MemCmd::SwapReq) {
167 cmpAndSwap(blk, pkt);
168 } else if (pkt->isWrite()) {
169 // we have the block in a writable state and can go ahead,
170 // note that the line may be also be considered writable in
171 // downstream caches along the path to memory, but always
172 // Exclusive, and never Modified
173 assert(blk->isWritable());
174 // Write or WriteLine at the first cache with block in writable state
175 if (blk->checkWrite(pkt)) {
176 pkt->writeDataToBlock(blk->data, blkSize);
177 }
178 // Always mark the line as dirty (and thus transition to the
179 // Modified state) even if we are a failed StoreCond so we
180 // supply data to any snoops that have appended themselves to
181 // this cache before knowing the store will fail.
182 blk->status |= BlkDirty;
183 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d (write)\n",
184 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
185 } else if (pkt->isRead()) {
186 if (pkt->isLLSC()) {
187 blk->trackLoadLocked(pkt);
188 }
189
190 // all read responses have a data payload
191 assert(pkt->hasRespData());
192 pkt->setDataFromBlock(blk->data, blkSize);
193
194 // determine if this read is from a (coherent) cache, or not
195 // by looking at the command type; we could potentially add a
196 // packet attribute such as 'FromCache' to make this check a
197 // bit cleaner
198 if (pkt->cmd == MemCmd::ReadExReq ||
199 pkt->cmd == MemCmd::ReadSharedReq ||
200 pkt->cmd == MemCmd::ReadCleanReq ||
201 pkt->cmd == MemCmd::SCUpgradeFailReq) {
202 assert(pkt->getSize() == blkSize);
203 // special handling for coherent block requests from
204 // upper-level caches
205 if (pkt->needsWritable()) {
206 // sanity check
207 assert(pkt->cmd == MemCmd::ReadExReq ||
208 pkt->cmd == MemCmd::SCUpgradeFailReq);
209
210 // if we have a dirty copy, make sure the recipient
211 // keeps it marked dirty (in the modified state)
212 if (blk->isDirty()) {
213 pkt->setCacheResponding();
214 }
215 // on ReadExReq we give up our copy unconditionally,
216 // even if this cache is mostly inclusive, we may want
217 // to revisit this
218 invalidateBlock(blk);
219 } else if (blk->isWritable() && !pending_downgrade &&
220 !pkt->hasSharers() &&
221 pkt->cmd != MemCmd::ReadCleanReq) {
222 // we can give the requester a writable copy on a read
223 // request if:
224 // - we have a writable copy at this level (& below)
225 // - we don't have a pending snoop from below
226 // signaling another read request
227 // - no other cache above has a copy (otherwise it
228 // would have set hasSharers flag when
229 // snooping the packet)
230 // - the read has explicitly asked for a clean
231 // copy of the line
232 if (blk->isDirty()) {
233 // special considerations if we're owner:
234 if (!deferred_response) {
235 // respond with the line in Modified state
236 // (cacheResponding set, hasSharers not set)
237 pkt->setCacheResponding();
238
239 if (clusivity == Enums::mostly_excl) {
240 // if this cache is mostly exclusive with
241 // respect to the cache above, drop the
242 // block, no need to first unset the dirty
243 // bit
244 invalidateBlock(blk);
245 } else {
246 // if this cache is mostly inclusive, we
247 // keep the block in the Exclusive state,
248 // and pass it upwards as Modified
249 // (writable and dirty), hence we have
250 // multiple caches, all on the same path
251 // towards memory, all considering the
252 // same block writable, but only one
253 // considering it Modified
254
255 // we get away with multiple caches (on
256 // the same path to memory) considering
257 // the block writeable as we always enter
258 // the cache hierarchy through a cache,
259 // and first snoop upwards in all other
260 // branches
261 blk->status &= ~BlkDirty;
262 }
263 } else {
264 // if we're responding after our own miss,
265 // there's a window where the recipient didn't
266 // know it was getting ownership and may not
267 // have responded to snoops correctly, so we
268 // have to respond with a shared line
269 pkt->setHasSharers();
270 }
271 }
272 } else {
273 // otherwise only respond with a shared copy
274 pkt->setHasSharers();
275 }
276 }
277 } else {
278 // Upgrade or Invalidate
279 assert(pkt->isUpgrade() || pkt->isInvalidate());
280
281 // for invalidations we could be looking at the temp block
282 // (for upgrades we always allocate)
283 invalidateBlock(blk);
284 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d (invalidation)\n",
285 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
286 }
287}
288
289
290/////////////////////////////////////////////////////
291//
289/////////////////////////////////////////////////////
290//
292// MSHR helper functions
293//
294/////////////////////////////////////////////////////
295
296
297void
298Cache::markInService(MSHR *mshr, bool pending_modified_resp)
299{
300 markInServiceInternal(mshr, pending_modified_resp);
301}
302
303/////////////////////////////////////////////////////
304//
305// Access path: requests coming in from the CPU side
306//
307/////////////////////////////////////////////////////
308
309bool
310Cache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
311 PacketList &writebacks)
312{
313 // sanity check
314 assert(pkt->isRequest());
315
316 chatty_assert(!(isReadOnly && pkt->isWrite()),
317 "Should never see a write in a read-only cache %s\n",
318 name());
319
320 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
321 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
322
323 if (pkt->req->isUncacheable()) {
324 DPRINTF(Cache, "%s%s addr %#llx uncacheable\n", pkt->cmdString(),
325 pkt->req->isInstFetch() ? " (ifetch)" : "",
326 pkt->getAddr());
327
328 // flush and invalidate any existing block
329 CacheBlk *old_blk(tags->findBlock(pkt->getAddr(), pkt->isSecure()));
330 if (old_blk && old_blk->isValid()) {
331 if (old_blk->isDirty() || writebackClean)
332 writebacks.push_back(writebackBlk(old_blk));
333 else
334 writebacks.push_back(cleanEvictBlk(old_blk));
335 tags->invalidate(old_blk);
336 old_blk->invalidate();
337 }
338
339 blk = NULL;
340 // lookupLatency is the latency in case the request is uncacheable.
341 lat = lookupLatency;
342 return false;
343 }
344
345 ContextID id = pkt->req->hasContextId() ?
346 pkt->req->contextId() : InvalidContextID;
347 // Here lat is the value passed as parameter to accessBlock() function
348 // that can modify its value.
349 blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), lat, id);
350
351 DPRINTF(Cache, "%s%s addr %#llx size %d (%s) %s\n", pkt->cmdString(),
352 pkt->req->isInstFetch() ? " (ifetch)" : "",
353 pkt->getAddr(), pkt->getSize(), pkt->isSecure() ? "s" : "ns",
354 blk ? "hit " + blk->print() : "miss");
355
356
357 if (pkt->isEviction()) {
358 // We check for presence of block in above caches before issuing
359 // Writeback or CleanEvict to write buffer. Therefore the only
360 // possible cases can be of a CleanEvict packet coming from above
361 // encountering a Writeback generated in this cache peer cache and
362 // waiting in the write buffer. Cases of upper level peer caches
363 // generating CleanEvict and Writeback or simply CleanEvict and
364 // CleanEvict almost simultaneously will be caught by snoops sent out
365 // by crossbar.
291// Access path: requests coming in from the CPU side
292//
293/////////////////////////////////////////////////////
294
295bool
296Cache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
297 PacketList &writebacks)
298{
299 // sanity check
300 assert(pkt->isRequest());
301
302 chatty_assert(!(isReadOnly && pkt->isWrite()),
303 "Should never see a write in a read-only cache %s\n",
304 name());
305
306 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
307 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
308
309 if (pkt->req->isUncacheable()) {
310 DPRINTF(Cache, "%s%s addr %#llx uncacheable\n", pkt->cmdString(),
311 pkt->req->isInstFetch() ? " (ifetch)" : "",
312 pkt->getAddr());
313
314 // flush and invalidate any existing block
315 CacheBlk *old_blk(tags->findBlock(pkt->getAddr(), pkt->isSecure()));
316 if (old_blk && old_blk->isValid()) {
317 if (old_blk->isDirty() || writebackClean)
318 writebacks.push_back(writebackBlk(old_blk));
319 else
320 writebacks.push_back(cleanEvictBlk(old_blk));
321 tags->invalidate(old_blk);
322 old_blk->invalidate();
323 }
324
325 blk = NULL;
326 // lookupLatency is the latency in case the request is uncacheable.
327 lat = lookupLatency;
328 return false;
329 }
330
331 ContextID id = pkt->req->hasContextId() ?
332 pkt->req->contextId() : InvalidContextID;
333 // Here lat is the value passed as parameter to accessBlock() function
334 // that can modify its value.
335 blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), lat, id);
336
337 DPRINTF(Cache, "%s%s addr %#llx size %d (%s) %s\n", pkt->cmdString(),
338 pkt->req->isInstFetch() ? " (ifetch)" : "",
339 pkt->getAddr(), pkt->getSize(), pkt->isSecure() ? "s" : "ns",
340 blk ? "hit " + blk->print() : "miss");
341
342
343 if (pkt->isEviction()) {
344 // We check for presence of block in above caches before issuing
345 // Writeback or CleanEvict to write buffer. Therefore the only
346 // possible cases can be of a CleanEvict packet coming from above
347 // encountering a Writeback generated in this cache peer cache and
348 // waiting in the write buffer. Cases of upper level peer caches
349 // generating CleanEvict and Writeback or simply CleanEvict and
350 // CleanEvict almost simultaneously will be caught by snoops sent out
351 // by crossbar.
366 std::vector<MSHR *> outgoing;
367 if (writeBuffer.findMatches(pkt->getAddr(), pkt->isSecure(),
368 outgoing)) {
369 assert(outgoing.size() == 1);
370 MSHR *wb_entry = outgoing[0];
352 WriteQueueEntry *wb_entry = writeBuffer.findMatch(pkt->getAddr(),
353 pkt->isSecure());
354 if (wb_entry) {
371 assert(wb_entry->getNumTargets() == 1);
372 PacketPtr wbPkt = wb_entry->getTarget()->pkt;
373 assert(wbPkt->isWriteback());
374
375 if (pkt->isCleanEviction()) {
376 // The CleanEvict and WritebackClean snoops into other
377 // peer caches of the same level while traversing the
378 // crossbar. If a copy of the block is found, the
379 // packet is deleted in the crossbar. Hence, none of
380 // the other upper level caches connected to this
381 // cache have the block, so we can clear the
382 // BLOCK_CACHED flag in the Writeback if set and
383 // discard the CleanEvict by returning true.
384 wbPkt->clearBlockCached();
385 return true;
386 } else {
387 assert(pkt->cmd == MemCmd::WritebackDirty);
388 // Dirty writeback from above trumps our clean
389 // writeback... discard here
390 // Note: markInService will remove entry from writeback buffer.
355 assert(wb_entry->getNumTargets() == 1);
356 PacketPtr wbPkt = wb_entry->getTarget()->pkt;
357 assert(wbPkt->isWriteback());
358
359 if (pkt->isCleanEviction()) {
360 // The CleanEvict and WritebackClean snoops into other
361 // peer caches of the same level while traversing the
362 // crossbar. If a copy of the block is found, the
363 // packet is deleted in the crossbar. Hence, none of
364 // the other upper level caches connected to this
365 // cache have the block, so we can clear the
366 // BLOCK_CACHED flag in the Writeback if set and
367 // discard the CleanEvict by returning true.
368 wbPkt->clearBlockCached();
369 return true;
370 } else {
371 assert(pkt->cmd == MemCmd::WritebackDirty);
372 // Dirty writeback from above trumps our clean
373 // writeback... discard here
374 // Note: markInService will remove entry from writeback buffer.
391 markInService(wb_entry, false);
375 markInService(wb_entry);
392 delete wbPkt;
393 }
394 }
395 }
396
397 // Writeback handling is special case. We can write the block into
398 // the cache without having a writeable copy (or any copy at all).
399 if (pkt->isWriteback()) {
400 assert(blkSize == pkt->getSize());
401
402 // we could get a clean writeback while we are having
403 // outstanding accesses to a block, do the simple thing for
404 // now and drop the clean writeback so that we do not upset
405 // any ordering/decisions about ownership already taken
406 if (pkt->cmd == MemCmd::WritebackClean &&
407 mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
408 DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
409 "dropping\n", pkt->getAddr());
410 return true;
411 }
412
413 if (blk == NULL) {
414 // need to do a replacement
415 blk = allocateBlock(pkt->getAddr(), pkt->isSecure(), writebacks);
416 if (blk == NULL) {
417 // no replaceable block available: give up, fwd to next level.
418 incMissCount(pkt);
419 return false;
420 }
421 tags->insertBlock(pkt, blk);
422
423 blk->status = (BlkValid | BlkReadable);
424 if (pkt->isSecure()) {
425 blk->status |= BlkSecure;
426 }
427 }
428 // only mark the block dirty if we got a writeback command,
429 // and leave it as is for a clean writeback
430 if (pkt->cmd == MemCmd::WritebackDirty) {
431 blk->status |= BlkDirty;
432 }
433 // if the packet does not have sharers, it is passing
434 // writable, and we got the writeback in Modified or Exclusive
435 // state, if not we are in the Owned or Shared state
436 if (!pkt->hasSharers()) {
437 blk->status |= BlkWritable;
438 }
439 // nothing else to do; writeback doesn't expect response
440 assert(!pkt->needsResponse());
441 std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
442 DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
443 incHitCount(pkt);
444 return true;
445 } else if (pkt->cmd == MemCmd::CleanEvict) {
446 if (blk != NULL) {
447 // Found the block in the tags, need to stop CleanEvict from
448 // propagating further down the hierarchy. Returning true will
449 // treat the CleanEvict like a satisfied write request and delete
450 // it.
451 return true;
452 }
453 // We didn't find the block here, propagate the CleanEvict further
454 // down the memory hierarchy. Returning false will treat the CleanEvict
455 // like a Writeback which could not find a replaceable block so has to
456 // go to next level.
457 return false;
458 } else if ((blk != NULL) &&
459 (pkt->needsWritable() ? blk->isWritable() : blk->isReadable())) {
460 // OK to satisfy access
461 incHitCount(pkt);
462 satisfyCpuSideRequest(pkt, blk);
463 return true;
464 }
465
466 // Can't satisfy access normally... either no block (blk == NULL)
467 // or have block but need writable
468
469 incMissCount(pkt);
470
471 if (blk == NULL && pkt->isLLSC() && pkt->isWrite()) {
472 // complete miss on store conditional... just give up now
473 pkt->req->setExtraData(0);
474 return true;
475 }
476
477 return false;
478}
479
480void
481Cache::doWritebacks(PacketList& writebacks, Tick forward_time)
482{
483 while (!writebacks.empty()) {
484 PacketPtr wbPkt = writebacks.front();
485 // We use forwardLatency here because we are copying writebacks to
486 // write buffer. Call isCachedAbove for both Writebacks and
487 // CleanEvicts. If isCachedAbove returns true we set BLOCK_CACHED flag
488 // in Writebacks and discard CleanEvicts.
489 if (isCachedAbove(wbPkt)) {
490 if (wbPkt->cmd == MemCmd::CleanEvict) {
491 // Delete CleanEvict because cached copies exist above. The
492 // packet destructor will delete the request object because
493 // this is a non-snoop request packet which does not require a
494 // response.
495 delete wbPkt;
496 } else if (wbPkt->cmd == MemCmd::WritebackClean) {
497 // clean writeback, do not send since the block is
498 // still cached above
499 assert(writebackClean);
500 delete wbPkt;
501 } else {
502 assert(wbPkt->cmd == MemCmd::WritebackDirty);
503 // Set BLOCK_CACHED flag in Writeback and send below, so that
504 // the Writeback does not reset the bit corresponding to this
505 // address in the snoop filter below.
506 wbPkt->setBlockCached();
507 allocateWriteBuffer(wbPkt, forward_time);
508 }
509 } else {
510 // If the block is not cached above, send packet below. Both
511 // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
512 // reset the bit corresponding to this address in the snoop filter
513 // below.
514 allocateWriteBuffer(wbPkt, forward_time);
515 }
516 writebacks.pop_front();
517 }
518}
519
520void
521Cache::doWritebacksAtomic(PacketList& writebacks)
522{
523 while (!writebacks.empty()) {
524 PacketPtr wbPkt = writebacks.front();
525 // Call isCachedAbove for both Writebacks and CleanEvicts. If
526 // isCachedAbove returns true we set BLOCK_CACHED flag in Writebacks
527 // and discard CleanEvicts.
528 if (isCachedAbove(wbPkt, false)) {
529 if (wbPkt->cmd == MemCmd::WritebackDirty) {
530 // Set BLOCK_CACHED flag in Writeback and send below,
531 // so that the Writeback does not reset the bit
532 // corresponding to this address in the snoop filter
533 // below. We can discard CleanEvicts because cached
534 // copies exist above. Atomic mode isCachedAbove
535 // modifies packet to set BLOCK_CACHED flag
536 memSidePort->sendAtomic(wbPkt);
537 }
538 } else {
539 // If the block is not cached above, send packet below. Both
540 // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
541 // reset the bit corresponding to this address in the snoop filter
542 // below.
543 memSidePort->sendAtomic(wbPkt);
544 }
545 writebacks.pop_front();
546 // In case of CleanEvicts, the packet destructor will delete the
547 // request object because this is a non-snoop request packet which
548 // does not require a response.
549 delete wbPkt;
550 }
551}
552
553
554void
555Cache::recvTimingSnoopResp(PacketPtr pkt)
556{
557 DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
558 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
559
560 assert(pkt->isResponse());
561 assert(!system->bypassCaches());
562
563 // determine if the response is from a snoop request we created
564 // (in which case it should be in the outstandingSnoop), or if we
565 // merely forwarded someone else's snoop request
566 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
567 outstandingSnoop.end();
568
569 if (!forwardAsSnoop) {
570 // the packet came from this cache, so sink it here and do not
571 // forward it
572 assert(pkt->cmd == MemCmd::HardPFResp);
573
574 outstandingSnoop.erase(pkt->req);
575
576 DPRINTF(Cache, "Got prefetch response from above for addr "
577 "%#llx (%s)\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
578 recvTimingResp(pkt);
579 return;
580 }
581
582 // forwardLatency is set here because there is a response from an
583 // upper level cache.
584 // To pay the delay that occurs if the packet comes from the bus,
585 // we charge also headerDelay.
586 Tick snoop_resp_time = clockEdge(forwardLatency) + pkt->headerDelay;
587 // Reset the timing of the packet.
588 pkt->headerDelay = pkt->payloadDelay = 0;
589 memSidePort->schedTimingSnoopResp(pkt, snoop_resp_time);
590}
591
592void
593Cache::promoteWholeLineWrites(PacketPtr pkt)
594{
595 // Cache line clearing instructions
596 if (doFastWrites && (pkt->cmd == MemCmd::WriteReq) &&
597 (pkt->getSize() == blkSize) && (pkt->getOffset(blkSize) == 0)) {
598 pkt->cmd = MemCmd::WriteLineReq;
599 DPRINTF(Cache, "packet promoted from Write to WriteLineReq\n");
600 }
601}
602
603bool
604Cache::recvTimingReq(PacketPtr pkt)
605{
606 DPRINTF(CacheTags, "%s tags: %s\n", __func__, tags->print());
607
608 assert(pkt->isRequest());
609
610 // Just forward the packet if caches are disabled.
611 if (system->bypassCaches()) {
612 // @todo This should really enqueue the packet rather
613 bool M5_VAR_USED success = memSidePort->sendTimingReq(pkt);
614 assert(success);
615 return true;
616 }
617
618 promoteWholeLineWrites(pkt);
619
620 if (pkt->cacheResponding()) {
621 // a cache above us (but not where the packet came from) is
622 // responding to the request, in other words it has the line
623 // in Modified or Owned state
624 DPRINTF(Cache, "Cache above responding to %#llx (%s): "
625 "not responding\n",
626 pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
627
628 // if the packet needs the block to be writable, and the cache
629 // that has promised to respond (setting the cache responding
630 // flag) is not providing writable (it is in Owned rather than
631 // the Modified state), we know that there may be other Shared
632 // copies in the system; go out and invalidate them all
633 assert(pkt->needsWritable() && !pkt->responderHadWritable());
634
635 // an upstream cache that had the line in Owned state
636 // (dirty, but not writable), is responding and thus
637 // transferring the dirty line from one branch of the
638 // cache hierarchy to another
639
640 // send out an express snoop and invalidate all other
641 // copies (snooping a packet that needs writable is the
642 // same as an invalidation), thus turning the Owned line
643 // into a Modified line, note that we don't invalidate the
644 // block in the current cache or any other cache on the
645 // path to memory
646
647 // create a downstream express snoop with cleared packet
648 // flags, there is no need to allocate any data as the
649 // packet is merely used to co-ordinate state transitions
650 Packet *snoop_pkt = new Packet(pkt, true, false);
651
652 // also reset the bus time that the original packet has
653 // not yet paid for
654 snoop_pkt->headerDelay = snoop_pkt->payloadDelay = 0;
655
656 // make this an instantaneous express snoop, and let the
657 // other caches in the system know that the another cache
658 // is responding, because we have found the authorative
659 // copy (Modified or Owned) that will supply the right
660 // data
661 snoop_pkt->setExpressSnoop();
662 snoop_pkt->setCacheResponding();
663
664 // this express snoop travels towards the memory, and at
665 // every crossbar it is snooped upwards thus reaching
666 // every cache in the system
667 bool M5_VAR_USED success = memSidePort->sendTimingReq(snoop_pkt);
668 // express snoops always succeed
669 assert(success);
670
671 // main memory will delete the snoop packet
672
673 // queue for deletion, as opposed to immediate deletion, as
674 // the sending cache is still relying on the packet
675 pendingDelete.reset(pkt);
676
677 // no need to take any further action in this particular cache
678 // as an upstram cache has already committed to responding,
679 // and we have already sent out any express snoops in the
680 // section above to ensure all other copies in the system are
681 // invalidated
682 return true;
683 }
684
685 // anything that is merely forwarded pays for the forward latency and
686 // the delay provided by the crossbar
687 Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
688
689 // We use lookupLatency here because it is used to specify the latency
690 // to access.
691 Cycles lat = lookupLatency;
692 CacheBlk *blk = NULL;
693 bool satisfied = false;
694 {
695 PacketList writebacks;
696 // Note that lat is passed by reference here. The function
697 // access() calls accessBlock() which can modify lat value.
698 satisfied = access(pkt, blk, lat, writebacks);
699
700 // copy writebacks to write buffer here to ensure they logically
701 // proceed anything happening below
702 doWritebacks(writebacks, forward_time);
703 }
704
705 // Here we charge the headerDelay that takes into account the latencies
706 // of the bus, if the packet comes from it.
707 // The latency charged it is just lat that is the value of lookupLatency
708 // modified by access() function, or if not just lookupLatency.
709 // In case of a hit we are neglecting response latency.
710 // In case of a miss we are neglecting forward latency.
711 Tick request_time = clockEdge(lat) + pkt->headerDelay;
712 // Here we reset the timing of the packet.
713 pkt->headerDelay = pkt->payloadDelay = 0;
714
715 // track time of availability of next prefetch, if any
716 Tick next_pf_time = MaxTick;
717
718 bool needsResponse = pkt->needsResponse();
719
720 if (satisfied) {
721 // should never be satisfying an uncacheable access as we
722 // flush and invalidate any existing block as part of the
723 // lookup
724 assert(!pkt->req->isUncacheable());
725
726 // hit (for all other request types)
727
728 if (prefetcher && (prefetchOnAccess || (blk && blk->wasPrefetched()))) {
729 if (blk)
730 blk->status &= ~BlkHWPrefetched;
731
732 // Don't notify on SWPrefetch
733 if (!pkt->cmd.isSWPrefetch())
734 next_pf_time = prefetcher->notify(pkt);
735 }
736
737 if (needsResponse) {
738 pkt->makeTimingResponse();
739 // @todo: Make someone pay for this
740 pkt->headerDelay = pkt->payloadDelay = 0;
741
742 // In this case we are considering request_time that takes
743 // into account the delay of the xbar, if any, and just
744 // lat, neglecting responseLatency, modelling hit latency
745 // just as lookupLatency or or the value of lat overriden
746 // by access(), that calls accessBlock() function.
747 cpuSidePort->schedTimingResp(pkt, request_time, true);
748 } else {
749 DPRINTF(Cache, "%s satisfied %s addr %#llx, no response needed\n",
750 __func__, pkt->cmdString(), pkt->getAddr(),
751 pkt->getSize());
752
753 // queue the packet for deletion, as the sending cache is
754 // still relying on it; if the block is found in access(),
755 // CleanEvict and Writeback messages will be deleted
756 // here as well
757 pendingDelete.reset(pkt);
758 }
759 } else {
760 // miss
761
762 Addr blk_addr = blockAlign(pkt->getAddr());
763
764 // ignore any existing MSHR if we are dealing with an
765 // uncacheable request
766 MSHR *mshr = pkt->req->isUncacheable() ? nullptr :
767 mshrQueue.findMatch(blk_addr, pkt->isSecure());
768
769 // Software prefetch handling:
770 // To keep the core from waiting on data it won't look at
771 // anyway, send back a response with dummy data. Miss handling
772 // will continue asynchronously. Unfortunately, the core will
773 // insist upon freeing original Packet/Request, so we have to
774 // create a new pair with a different lifecycle. Note that this
775 // processing happens before any MSHR munging on the behalf of
776 // this request because this new Request will be the one stored
777 // into the MSHRs, not the original.
778 if (pkt->cmd.isSWPrefetch()) {
779 assert(needsResponse);
780 assert(pkt->req->hasPaddr());
781 assert(!pkt->req->isUncacheable());
782
783 // There's no reason to add a prefetch as an additional target
784 // to an existing MSHR. If an outstanding request is already
785 // in progress, there is nothing for the prefetch to do.
786 // If this is the case, we don't even create a request at all.
787 PacketPtr pf = nullptr;
788
789 if (!mshr) {
790 // copy the request and create a new SoftPFReq packet
791 RequestPtr req = new Request(pkt->req->getPaddr(),
792 pkt->req->getSize(),
793 pkt->req->getFlags(),
794 pkt->req->masterId());
795 pf = new Packet(req, pkt->cmd);
796 pf->allocate();
797 assert(pf->getAddr() == pkt->getAddr());
798 assert(pf->getSize() == pkt->getSize());
799 }
800
801 pkt->makeTimingResponse();
802
803 // request_time is used here, taking into account lat and the delay
804 // charged if the packet comes from the xbar.
805 cpuSidePort->schedTimingResp(pkt, request_time, true);
806
807 // If an outstanding request is in progress (we found an
808 // MSHR) this is set to null
809 pkt = pf;
810 }
811
812 if (mshr) {
813 /// MSHR hit
814 /// @note writebacks will be checked in getNextMSHR()
815 /// for any conflicting requests to the same block
816
817 //@todo remove hw_pf here
818
819 // Coalesce unless it was a software prefetch (see above).
820 if (pkt) {
821 assert(!pkt->isWriteback());
822 // CleanEvicts corresponding to blocks which have
823 // outstanding requests in MSHRs are simply sunk here
824 if (pkt->cmd == MemCmd::CleanEvict) {
825 pendingDelete.reset(pkt);
826 } else {
827 DPRINTF(Cache, "%s coalescing MSHR for %s addr %#llx size %d\n",
828 __func__, pkt->cmdString(), pkt->getAddr(),
829 pkt->getSize());
830
831 assert(pkt->req->masterId() < system->maxMasters());
832 mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
833 // We use forward_time here because it is the same
834 // considering new targets. We have multiple
835 // requests for the same address here. It
836 // specifies the latency to allocate an internal
837 // buffer and to schedule an event to the queued
838 // port and also takes into account the additional
839 // delay of the xbar.
840 mshr->allocateTarget(pkt, forward_time, order++,
841 allocOnFill(pkt->cmd));
842 if (mshr->getNumTargets() == numTarget) {
843 noTargetMSHR = mshr;
844 setBlocked(Blocked_NoTargets);
845 // need to be careful with this... if this mshr isn't
846 // ready yet (i.e. time > curTick()), we don't want to
847 // move it ahead of mshrs that are ready
848 // mshrQueue.moveToFront(mshr);
849 }
850 }
851 // We should call the prefetcher reguardless if the request is
852 // satisfied or not, reguardless if the request is in the MSHR or
853 // not. The request could be a ReadReq hit, but still not
854 // satisfied (potentially because of a prior write to the same
855 // cache line. So, even when not satisfied, tehre is an MSHR
856 // already allocated for this, we need to let the prefetcher know
857 // about the request
858 if (prefetcher) {
859 // Don't notify on SWPrefetch
860 if (!pkt->cmd.isSWPrefetch())
861 next_pf_time = prefetcher->notify(pkt);
862 }
863 }
864 } else {
865 // no MSHR
866 assert(pkt->req->masterId() < system->maxMasters());
867 if (pkt->req->isUncacheable()) {
868 mshr_uncacheable[pkt->cmdToIndex()][pkt->req->masterId()]++;
869 } else {
870 mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
871 }
872
873 if (pkt->isEviction() ||
874 (pkt->req->isUncacheable() && pkt->isWrite())) {
875 // We use forward_time here because there is an
876 // uncached memory write, forwarded to WriteBuffer.
877 allocateWriteBuffer(pkt, forward_time);
878 } else {
879 if (blk && blk->isValid()) {
880 // should have flushed and have no valid block
881 assert(!pkt->req->isUncacheable());
882
883 // If we have a write miss to a valid block, we
884 // need to mark the block non-readable. Otherwise
885 // if we allow reads while there's an outstanding
886 // write miss, the read could return stale data
887 // out of the cache block... a more aggressive
888 // system could detect the overlap (if any) and
889 // forward data out of the MSHRs, but we don't do
890 // that yet. Note that we do need to leave the
891 // block valid so that it stays in the cache, in
892 // case we get an upgrade response (and hence no
893 // new data) when the write miss completes.
894 // As long as CPUs do proper store/load forwarding
895 // internally, and have a sufficiently weak memory
896 // model, this is probably unnecessary, but at some
897 // point it must have seemed like we needed it...
898 assert(pkt->needsWritable());
899 assert(!blk->isWritable());
900 blk->status &= ~BlkReadable;
901 }
902 // Here we are using forward_time, modelling the latency of
903 // a miss (outbound) just as forwardLatency, neglecting the
904 // lookupLatency component.
905 allocateMissBuffer(pkt, forward_time);
906 }
907
908 if (prefetcher) {
909 // Don't notify on SWPrefetch
910 if (!pkt->cmd.isSWPrefetch())
911 next_pf_time = prefetcher->notify(pkt);
912 }
913 }
914 }
915
916 if (next_pf_time != MaxTick)
917 schedMemSideSendEvent(next_pf_time);
918
919 return true;
920}
921
922
923// See comment in cache.hh.
924PacketPtr
925Cache::getBusPacket(PacketPtr cpu_pkt, CacheBlk *blk,
926 bool needsWritable) const
927{
928 bool blkValid = blk && blk->isValid();
929
930 if (cpu_pkt->req->isUncacheable()) {
931 // note that at the point we see the uncacheable request we
932 // flush any block, but there could be an outstanding MSHR,
933 // and the cache could have filled again before we actually
934 // send out the forwarded uncacheable request (blk could thus
935 // be non-null)
936 return NULL;
937 }
938
939 if (!blkValid &&
940 (cpu_pkt->isUpgrade() ||
941 cpu_pkt->isEviction())) {
942 // Writebacks that weren't allocated in access() and upgrades
943 // from upper-level caches that missed completely just go
944 // through.
945 return NULL;
946 }
947
948 assert(cpu_pkt->needsResponse());
949
950 MemCmd cmd;
951 // @TODO make useUpgrades a parameter.
952 // Note that ownership protocols require upgrade, otherwise a
953 // write miss on a shared owned block will generate a ReadExcl,
954 // which will clobber the owned copy.
955 const bool useUpgrades = true;
956 if (blkValid && useUpgrades) {
957 // only reason to be here is that blk is read only and we need
958 // it to be writable
959 assert(needsWritable);
960 assert(!blk->isWritable());
961 cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
962 } else if (cpu_pkt->cmd == MemCmd::SCUpgradeFailReq ||
963 cpu_pkt->cmd == MemCmd::StoreCondFailReq) {
964 // Even though this SC will fail, we still need to send out the
965 // request and get the data to supply it to other snoopers in the case
966 // where the determination the StoreCond fails is delayed due to
967 // all caches not being on the same local bus.
968 cmd = MemCmd::SCUpgradeFailReq;
969 } else if (cpu_pkt->cmd == MemCmd::WriteLineReq ||
970 cpu_pkt->cmd == MemCmd::InvalidateReq) {
971 // forward as invalidate to all other caches, this gives us
972 // the line in Exclusive state, and invalidates all other
973 // copies
974 cmd = MemCmd::InvalidateReq;
975 } else {
976 // block is invalid
977 cmd = needsWritable ? MemCmd::ReadExReq :
978 (isReadOnly ? MemCmd::ReadCleanReq : MemCmd::ReadSharedReq);
979 }
980 PacketPtr pkt = new Packet(cpu_pkt->req, cmd, blkSize);
981
982 // if there are upstream caches that have already marked the
983 // packet as having sharers (not passing writable), pass that info
984 // downstream
985 if (cpu_pkt->hasSharers()) {
986 // note that cpu_pkt may have spent a considerable time in the
987 // MSHR queue and that the information could possibly be out
988 // of date, however, there is no harm in conservatively
989 // assuming the block has sharers
990 pkt->setHasSharers();
991 DPRINTF(Cache, "%s passing hasSharers from %s to %s addr %#llx "
992 "size %d\n",
993 __func__, cpu_pkt->cmdString(), pkt->cmdString(),
994 pkt->getAddr(), pkt->getSize());
995 }
996
997 // the packet should be block aligned
998 assert(pkt->getAddr() == blockAlign(pkt->getAddr()));
999
1000 pkt->allocate();
1001 DPRINTF(Cache, "%s created %s from %s for addr %#llx size %d\n",
1002 __func__, pkt->cmdString(), cpu_pkt->cmdString(), pkt->getAddr(),
1003 pkt->getSize());
1004 return pkt;
1005}
1006
1007
1008Tick
1009Cache::recvAtomic(PacketPtr pkt)
1010{
1011 // We are in atomic mode so we pay just for lookupLatency here.
1012 Cycles lat = lookupLatency;
1013
1014 // Forward the request if the system is in cache bypass mode.
1015 if (system->bypassCaches())
1016 return ticksToCycles(memSidePort->sendAtomic(pkt));
1017
1018 promoteWholeLineWrites(pkt);
1019
1020 // follow the same flow as in recvTimingReq, and check if a cache
1021 // above us is responding
1022 if (pkt->cacheResponding()) {
1023 DPRINTF(Cache, "Cache above responding to %#llx (%s): "
1024 "not responding\n",
1025 pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
1026
1027 // if a cache is responding, and it had the line in Owned
1028 // rather than Modified state, we need to invalidate any
1029 // copies that are not on the same path to memory
1030 assert(pkt->needsWritable() && !pkt->responderHadWritable());
1031 lat += ticksToCycles(memSidePort->sendAtomic(pkt));
1032
1033 return lat * clockPeriod();
1034 }
1035
1036 // should assert here that there are no outstanding MSHRs or
1037 // writebacks... that would mean that someone used an atomic
1038 // access in timing mode
1039
1040 CacheBlk *blk = NULL;
1041 PacketList writebacks;
1042 bool satisfied = access(pkt, blk, lat, writebacks);
1043
1044 // handle writebacks resulting from the access here to ensure they
1045 // logically proceed anything happening below
1046 doWritebacksAtomic(writebacks);
1047
1048 if (!satisfied) {
1049 // MISS
1050
1051 PacketPtr bus_pkt = getBusPacket(pkt, blk, pkt->needsWritable());
1052
1053 bool is_forward = (bus_pkt == NULL);
1054
1055 if (is_forward) {
1056 // just forwarding the same request to the next level
1057 // no local cache operation involved
1058 bus_pkt = pkt;
1059 }
1060
1061 DPRINTF(Cache, "Sending an atomic %s for %#llx (%s)\n",
1062 bus_pkt->cmdString(), bus_pkt->getAddr(),
1063 bus_pkt->isSecure() ? "s" : "ns");
1064
1065#if TRACING_ON
1066 CacheBlk::State old_state = blk ? blk->status : 0;
1067#endif
1068
1069 lat += ticksToCycles(memSidePort->sendAtomic(bus_pkt));
1070
1071 // We are now dealing with the response handling
1072 DPRINTF(Cache, "Receive response: %s for addr %#llx (%s) in state %i\n",
1073 bus_pkt->cmdString(), bus_pkt->getAddr(),
1074 bus_pkt->isSecure() ? "s" : "ns",
1075 old_state);
1076
1077 // If packet was a forward, the response (if any) is already
1078 // in place in the bus_pkt == pkt structure, so we don't need
1079 // to do anything. Otherwise, use the separate bus_pkt to
1080 // generate response to pkt and then delete it.
1081 if (!is_forward) {
1082 if (pkt->needsResponse()) {
1083 assert(bus_pkt->isResponse());
1084 if (bus_pkt->isError()) {
1085 pkt->makeAtomicResponse();
1086 pkt->copyError(bus_pkt);
1087 } else if (pkt->cmd == MemCmd::InvalidateReq) {
1088 if (blk) {
1089 // invalidate response to a cache that received
1090 // an invalidate request
1091 satisfyCpuSideRequest(pkt, blk);
1092 }
1093 } else if (pkt->cmd == MemCmd::WriteLineReq) {
1094 // note the use of pkt, not bus_pkt here.
1095
1096 // write-line request to the cache that promoted
1097 // the write to a whole line
1098 blk = handleFill(pkt, blk, writebacks,
1099 allocOnFill(pkt->cmd));
1100 satisfyCpuSideRequest(pkt, blk);
1101 } else if (bus_pkt->isRead() ||
1102 bus_pkt->cmd == MemCmd::UpgradeResp) {
1103 // we're updating cache state to allow us to
1104 // satisfy the upstream request from the cache
1105 blk = handleFill(bus_pkt, blk, writebacks,
1106 allocOnFill(pkt->cmd));
1107 satisfyCpuSideRequest(pkt, blk);
1108 } else {
1109 // we're satisfying the upstream request without
1110 // modifying cache state, e.g., a write-through
1111 pkt->makeAtomicResponse();
1112 }
1113 }
1114 delete bus_pkt;
1115 }
1116 }
1117
1118 // Note that we don't invoke the prefetcher at all in atomic mode.
1119 // It's not clear how to do it properly, particularly for
1120 // prefetchers that aggressively generate prefetch candidates and
1121 // rely on bandwidth contention to throttle them; these will tend
1122 // to pollute the cache in atomic mode since there is no bandwidth
1123 // contention. If we ever do want to enable prefetching in atomic
1124 // mode, though, this is the place to do it... see timingAccess()
1125 // for an example (though we'd want to issue the prefetch(es)
1126 // immediately rather than calling requestMemSideBus() as we do
1127 // there).
1128
1129 // do any writebacks resulting from the response handling
1130 doWritebacksAtomic(writebacks);
1131
1132 // if we used temp block, check to see if its valid and if so
1133 // clear it out, but only do so after the call to recvAtomic is
1134 // finished so that any downstream observers (such as a snoop
1135 // filter), first see the fill, and only then see the eviction
1136 if (blk == tempBlock && tempBlock->isValid()) {
1137 // the atomic CPU calls recvAtomic for fetch and load/store
1138 // sequentuially, and we may already have a tempBlock
1139 // writeback from the fetch that we have not yet sent
1140 if (tempBlockWriteback) {
1141 // if that is the case, write the prevoius one back, and
1142 // do not schedule any new event
1143 writebackTempBlockAtomic();
1144 } else {
1145 // the writeback/clean eviction happens after the call to
1146 // recvAtomic has finished (but before any successive
1147 // calls), so that the response handling from the fill is
1148 // allowed to happen first
1149 schedule(writebackTempBlockAtomicEvent, curTick());
1150 }
1151
1152 tempBlockWriteback = (blk->isDirty() || writebackClean) ?
1153 writebackBlk(blk) : cleanEvictBlk(blk);
1154 blk->invalidate();
1155 }
1156
1157 if (pkt->needsResponse()) {
1158 pkt->makeAtomicResponse();
1159 }
1160
1161 return lat * clockPeriod();
1162}
1163
1164
1165void
1166Cache::functionalAccess(PacketPtr pkt, bool fromCpuSide)
1167{
1168 if (system->bypassCaches()) {
1169 // Packets from the memory side are snoop request and
1170 // shouldn't happen in bypass mode.
1171 assert(fromCpuSide);
1172
1173 // The cache should be flushed if we are in cache bypass mode,
1174 // so we don't need to check if we need to update anything.
1175 memSidePort->sendFunctional(pkt);
1176 return;
1177 }
1178
1179 Addr blk_addr = blockAlign(pkt->getAddr());
1180 bool is_secure = pkt->isSecure();
1181 CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
1182 MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
1183
1184 pkt->pushLabel(name());
1185
1186 CacheBlkPrintWrapper cbpw(blk);
1187
1188 // Note that just because an L2/L3 has valid data doesn't mean an
1189 // L1 doesn't have a more up-to-date modified copy that still
1190 // needs to be found. As a result we always update the request if
1191 // we have it, but only declare it satisfied if we are the owner.
1192
1193 // see if we have data at all (owned or otherwise)
1194 bool have_data = blk && blk->isValid()
1195 && pkt->checkFunctional(&cbpw, blk_addr, is_secure, blkSize,
1196 blk->data);
1197
1198 // data we have is dirty if marked as such or if we have an
1199 // in-service MSHR that is pending a modified line
1200 bool have_dirty =
1201 have_data && (blk->isDirty() ||
1202 (mshr && mshr->inService && mshr->isPendingModified()));
1203
1204 bool done = have_dirty
1205 || cpuSidePort->checkFunctional(pkt)
1206 || mshrQueue.checkFunctional(pkt, blk_addr)
1207 || writeBuffer.checkFunctional(pkt, blk_addr)
1208 || memSidePort->checkFunctional(pkt);
1209
1210 DPRINTF(CacheVerbose, "functional %s %#llx (%s) %s%s%s\n",
1211 pkt->cmdString(), pkt->getAddr(), is_secure ? "s" : "ns",
1212 (blk && blk->isValid()) ? "valid " : "",
1213 have_data ? "data " : "", done ? "done " : "");
1214
1215 // We're leaving the cache, so pop cache->name() label
1216 pkt->popLabel();
1217
1218 if (done) {
1219 pkt->makeResponse();
1220 } else {
1221 // if it came as a request from the CPU side then make sure it
1222 // continues towards the memory side
1223 if (fromCpuSide) {
1224 memSidePort->sendFunctional(pkt);
1225 } else if (forwardSnoops && cpuSidePort->isSnooping()) {
1226 // if it came from the memory side, it must be a snoop request
1227 // and we should only forward it if we are forwarding snoops
1228 cpuSidePort->sendFunctionalSnoop(pkt);
1229 }
1230 }
1231}
1232
1233
1234/////////////////////////////////////////////////////
1235//
1236// Response handling: responses from the memory side
1237//
1238/////////////////////////////////////////////////////
1239
1240
1241void
376 delete wbPkt;
377 }
378 }
379 }
380
381 // Writeback handling is special case. We can write the block into
382 // the cache without having a writeable copy (or any copy at all).
383 if (pkt->isWriteback()) {
384 assert(blkSize == pkt->getSize());
385
386 // we could get a clean writeback while we are having
387 // outstanding accesses to a block, do the simple thing for
388 // now and drop the clean writeback so that we do not upset
389 // any ordering/decisions about ownership already taken
390 if (pkt->cmd == MemCmd::WritebackClean &&
391 mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
392 DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
393 "dropping\n", pkt->getAddr());
394 return true;
395 }
396
397 if (blk == NULL) {
398 // need to do a replacement
399 blk = allocateBlock(pkt->getAddr(), pkt->isSecure(), writebacks);
400 if (blk == NULL) {
401 // no replaceable block available: give up, fwd to next level.
402 incMissCount(pkt);
403 return false;
404 }
405 tags->insertBlock(pkt, blk);
406
407 blk->status = (BlkValid | BlkReadable);
408 if (pkt->isSecure()) {
409 blk->status |= BlkSecure;
410 }
411 }
412 // only mark the block dirty if we got a writeback command,
413 // and leave it as is for a clean writeback
414 if (pkt->cmd == MemCmd::WritebackDirty) {
415 blk->status |= BlkDirty;
416 }
417 // if the packet does not have sharers, it is passing
418 // writable, and we got the writeback in Modified or Exclusive
419 // state, if not we are in the Owned or Shared state
420 if (!pkt->hasSharers()) {
421 blk->status |= BlkWritable;
422 }
423 // nothing else to do; writeback doesn't expect response
424 assert(!pkt->needsResponse());
425 std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
426 DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
427 incHitCount(pkt);
428 return true;
429 } else if (pkt->cmd == MemCmd::CleanEvict) {
430 if (blk != NULL) {
431 // Found the block in the tags, need to stop CleanEvict from
432 // propagating further down the hierarchy. Returning true will
433 // treat the CleanEvict like a satisfied write request and delete
434 // it.
435 return true;
436 }
437 // We didn't find the block here, propagate the CleanEvict further
438 // down the memory hierarchy. Returning false will treat the CleanEvict
439 // like a Writeback which could not find a replaceable block so has to
440 // go to next level.
441 return false;
442 } else if ((blk != NULL) &&
443 (pkt->needsWritable() ? blk->isWritable() : blk->isReadable())) {
444 // OK to satisfy access
445 incHitCount(pkt);
446 satisfyCpuSideRequest(pkt, blk);
447 return true;
448 }
449
450 // Can't satisfy access normally... either no block (blk == NULL)
451 // or have block but need writable
452
453 incMissCount(pkt);
454
455 if (blk == NULL && pkt->isLLSC() && pkt->isWrite()) {
456 // complete miss on store conditional... just give up now
457 pkt->req->setExtraData(0);
458 return true;
459 }
460
461 return false;
462}
463
464void
465Cache::doWritebacks(PacketList& writebacks, Tick forward_time)
466{
467 while (!writebacks.empty()) {
468 PacketPtr wbPkt = writebacks.front();
469 // We use forwardLatency here because we are copying writebacks to
470 // write buffer. Call isCachedAbove for both Writebacks and
471 // CleanEvicts. If isCachedAbove returns true we set BLOCK_CACHED flag
472 // in Writebacks and discard CleanEvicts.
473 if (isCachedAbove(wbPkt)) {
474 if (wbPkt->cmd == MemCmd::CleanEvict) {
475 // Delete CleanEvict because cached copies exist above. The
476 // packet destructor will delete the request object because
477 // this is a non-snoop request packet which does not require a
478 // response.
479 delete wbPkt;
480 } else if (wbPkt->cmd == MemCmd::WritebackClean) {
481 // clean writeback, do not send since the block is
482 // still cached above
483 assert(writebackClean);
484 delete wbPkt;
485 } else {
486 assert(wbPkt->cmd == MemCmd::WritebackDirty);
487 // Set BLOCK_CACHED flag in Writeback and send below, so that
488 // the Writeback does not reset the bit corresponding to this
489 // address in the snoop filter below.
490 wbPkt->setBlockCached();
491 allocateWriteBuffer(wbPkt, forward_time);
492 }
493 } else {
494 // If the block is not cached above, send packet below. Both
495 // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
496 // reset the bit corresponding to this address in the snoop filter
497 // below.
498 allocateWriteBuffer(wbPkt, forward_time);
499 }
500 writebacks.pop_front();
501 }
502}
503
504void
505Cache::doWritebacksAtomic(PacketList& writebacks)
506{
507 while (!writebacks.empty()) {
508 PacketPtr wbPkt = writebacks.front();
509 // Call isCachedAbove for both Writebacks and CleanEvicts. If
510 // isCachedAbove returns true we set BLOCK_CACHED flag in Writebacks
511 // and discard CleanEvicts.
512 if (isCachedAbove(wbPkt, false)) {
513 if (wbPkt->cmd == MemCmd::WritebackDirty) {
514 // Set BLOCK_CACHED flag in Writeback and send below,
515 // so that the Writeback does not reset the bit
516 // corresponding to this address in the snoop filter
517 // below. We can discard CleanEvicts because cached
518 // copies exist above. Atomic mode isCachedAbove
519 // modifies packet to set BLOCK_CACHED flag
520 memSidePort->sendAtomic(wbPkt);
521 }
522 } else {
523 // If the block is not cached above, send packet below. Both
524 // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
525 // reset the bit corresponding to this address in the snoop filter
526 // below.
527 memSidePort->sendAtomic(wbPkt);
528 }
529 writebacks.pop_front();
530 // In case of CleanEvicts, the packet destructor will delete the
531 // request object because this is a non-snoop request packet which
532 // does not require a response.
533 delete wbPkt;
534 }
535}
536
537
538void
539Cache::recvTimingSnoopResp(PacketPtr pkt)
540{
541 DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
542 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
543
544 assert(pkt->isResponse());
545 assert(!system->bypassCaches());
546
547 // determine if the response is from a snoop request we created
548 // (in which case it should be in the outstandingSnoop), or if we
549 // merely forwarded someone else's snoop request
550 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
551 outstandingSnoop.end();
552
553 if (!forwardAsSnoop) {
554 // the packet came from this cache, so sink it here and do not
555 // forward it
556 assert(pkt->cmd == MemCmd::HardPFResp);
557
558 outstandingSnoop.erase(pkt->req);
559
560 DPRINTF(Cache, "Got prefetch response from above for addr "
561 "%#llx (%s)\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
562 recvTimingResp(pkt);
563 return;
564 }
565
566 // forwardLatency is set here because there is a response from an
567 // upper level cache.
568 // To pay the delay that occurs if the packet comes from the bus,
569 // we charge also headerDelay.
570 Tick snoop_resp_time = clockEdge(forwardLatency) + pkt->headerDelay;
571 // Reset the timing of the packet.
572 pkt->headerDelay = pkt->payloadDelay = 0;
573 memSidePort->schedTimingSnoopResp(pkt, snoop_resp_time);
574}
575
576void
577Cache::promoteWholeLineWrites(PacketPtr pkt)
578{
579 // Cache line clearing instructions
580 if (doFastWrites && (pkt->cmd == MemCmd::WriteReq) &&
581 (pkt->getSize() == blkSize) && (pkt->getOffset(blkSize) == 0)) {
582 pkt->cmd = MemCmd::WriteLineReq;
583 DPRINTF(Cache, "packet promoted from Write to WriteLineReq\n");
584 }
585}
586
587bool
588Cache::recvTimingReq(PacketPtr pkt)
589{
590 DPRINTF(CacheTags, "%s tags: %s\n", __func__, tags->print());
591
592 assert(pkt->isRequest());
593
594 // Just forward the packet if caches are disabled.
595 if (system->bypassCaches()) {
596 // @todo This should really enqueue the packet rather
597 bool M5_VAR_USED success = memSidePort->sendTimingReq(pkt);
598 assert(success);
599 return true;
600 }
601
602 promoteWholeLineWrites(pkt);
603
604 if (pkt->cacheResponding()) {
605 // a cache above us (but not where the packet came from) is
606 // responding to the request, in other words it has the line
607 // in Modified or Owned state
608 DPRINTF(Cache, "Cache above responding to %#llx (%s): "
609 "not responding\n",
610 pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
611
612 // if the packet needs the block to be writable, and the cache
613 // that has promised to respond (setting the cache responding
614 // flag) is not providing writable (it is in Owned rather than
615 // the Modified state), we know that there may be other Shared
616 // copies in the system; go out and invalidate them all
617 assert(pkt->needsWritable() && !pkt->responderHadWritable());
618
619 // an upstream cache that had the line in Owned state
620 // (dirty, but not writable), is responding and thus
621 // transferring the dirty line from one branch of the
622 // cache hierarchy to another
623
624 // send out an express snoop and invalidate all other
625 // copies (snooping a packet that needs writable is the
626 // same as an invalidation), thus turning the Owned line
627 // into a Modified line, note that we don't invalidate the
628 // block in the current cache or any other cache on the
629 // path to memory
630
631 // create a downstream express snoop with cleared packet
632 // flags, there is no need to allocate any data as the
633 // packet is merely used to co-ordinate state transitions
634 Packet *snoop_pkt = new Packet(pkt, true, false);
635
636 // also reset the bus time that the original packet has
637 // not yet paid for
638 snoop_pkt->headerDelay = snoop_pkt->payloadDelay = 0;
639
640 // make this an instantaneous express snoop, and let the
641 // other caches in the system know that the another cache
642 // is responding, because we have found the authorative
643 // copy (Modified or Owned) that will supply the right
644 // data
645 snoop_pkt->setExpressSnoop();
646 snoop_pkt->setCacheResponding();
647
648 // this express snoop travels towards the memory, and at
649 // every crossbar it is snooped upwards thus reaching
650 // every cache in the system
651 bool M5_VAR_USED success = memSidePort->sendTimingReq(snoop_pkt);
652 // express snoops always succeed
653 assert(success);
654
655 // main memory will delete the snoop packet
656
657 // queue for deletion, as opposed to immediate deletion, as
658 // the sending cache is still relying on the packet
659 pendingDelete.reset(pkt);
660
661 // no need to take any further action in this particular cache
662 // as an upstram cache has already committed to responding,
663 // and we have already sent out any express snoops in the
664 // section above to ensure all other copies in the system are
665 // invalidated
666 return true;
667 }
668
669 // anything that is merely forwarded pays for the forward latency and
670 // the delay provided by the crossbar
671 Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
672
673 // We use lookupLatency here because it is used to specify the latency
674 // to access.
675 Cycles lat = lookupLatency;
676 CacheBlk *blk = NULL;
677 bool satisfied = false;
678 {
679 PacketList writebacks;
680 // Note that lat is passed by reference here. The function
681 // access() calls accessBlock() which can modify lat value.
682 satisfied = access(pkt, blk, lat, writebacks);
683
684 // copy writebacks to write buffer here to ensure they logically
685 // proceed anything happening below
686 doWritebacks(writebacks, forward_time);
687 }
688
689 // Here we charge the headerDelay that takes into account the latencies
690 // of the bus, if the packet comes from it.
691 // The latency charged it is just lat that is the value of lookupLatency
692 // modified by access() function, or if not just lookupLatency.
693 // In case of a hit we are neglecting response latency.
694 // In case of a miss we are neglecting forward latency.
695 Tick request_time = clockEdge(lat) + pkt->headerDelay;
696 // Here we reset the timing of the packet.
697 pkt->headerDelay = pkt->payloadDelay = 0;
698
699 // track time of availability of next prefetch, if any
700 Tick next_pf_time = MaxTick;
701
702 bool needsResponse = pkt->needsResponse();
703
704 if (satisfied) {
705 // should never be satisfying an uncacheable access as we
706 // flush and invalidate any existing block as part of the
707 // lookup
708 assert(!pkt->req->isUncacheable());
709
710 // hit (for all other request types)
711
712 if (prefetcher && (prefetchOnAccess || (blk && blk->wasPrefetched()))) {
713 if (blk)
714 blk->status &= ~BlkHWPrefetched;
715
716 // Don't notify on SWPrefetch
717 if (!pkt->cmd.isSWPrefetch())
718 next_pf_time = prefetcher->notify(pkt);
719 }
720
721 if (needsResponse) {
722 pkt->makeTimingResponse();
723 // @todo: Make someone pay for this
724 pkt->headerDelay = pkt->payloadDelay = 0;
725
726 // In this case we are considering request_time that takes
727 // into account the delay of the xbar, if any, and just
728 // lat, neglecting responseLatency, modelling hit latency
729 // just as lookupLatency or or the value of lat overriden
730 // by access(), that calls accessBlock() function.
731 cpuSidePort->schedTimingResp(pkt, request_time, true);
732 } else {
733 DPRINTF(Cache, "%s satisfied %s addr %#llx, no response needed\n",
734 __func__, pkt->cmdString(), pkt->getAddr(),
735 pkt->getSize());
736
737 // queue the packet for deletion, as the sending cache is
738 // still relying on it; if the block is found in access(),
739 // CleanEvict and Writeback messages will be deleted
740 // here as well
741 pendingDelete.reset(pkt);
742 }
743 } else {
744 // miss
745
746 Addr blk_addr = blockAlign(pkt->getAddr());
747
748 // ignore any existing MSHR if we are dealing with an
749 // uncacheable request
750 MSHR *mshr = pkt->req->isUncacheable() ? nullptr :
751 mshrQueue.findMatch(blk_addr, pkt->isSecure());
752
753 // Software prefetch handling:
754 // To keep the core from waiting on data it won't look at
755 // anyway, send back a response with dummy data. Miss handling
756 // will continue asynchronously. Unfortunately, the core will
757 // insist upon freeing original Packet/Request, so we have to
758 // create a new pair with a different lifecycle. Note that this
759 // processing happens before any MSHR munging on the behalf of
760 // this request because this new Request will be the one stored
761 // into the MSHRs, not the original.
762 if (pkt->cmd.isSWPrefetch()) {
763 assert(needsResponse);
764 assert(pkt->req->hasPaddr());
765 assert(!pkt->req->isUncacheable());
766
767 // There's no reason to add a prefetch as an additional target
768 // to an existing MSHR. If an outstanding request is already
769 // in progress, there is nothing for the prefetch to do.
770 // If this is the case, we don't even create a request at all.
771 PacketPtr pf = nullptr;
772
773 if (!mshr) {
774 // copy the request and create a new SoftPFReq packet
775 RequestPtr req = new Request(pkt->req->getPaddr(),
776 pkt->req->getSize(),
777 pkt->req->getFlags(),
778 pkt->req->masterId());
779 pf = new Packet(req, pkt->cmd);
780 pf->allocate();
781 assert(pf->getAddr() == pkt->getAddr());
782 assert(pf->getSize() == pkt->getSize());
783 }
784
785 pkt->makeTimingResponse();
786
787 // request_time is used here, taking into account lat and the delay
788 // charged if the packet comes from the xbar.
789 cpuSidePort->schedTimingResp(pkt, request_time, true);
790
791 // If an outstanding request is in progress (we found an
792 // MSHR) this is set to null
793 pkt = pf;
794 }
795
796 if (mshr) {
797 /// MSHR hit
798 /// @note writebacks will be checked in getNextMSHR()
799 /// for any conflicting requests to the same block
800
801 //@todo remove hw_pf here
802
803 // Coalesce unless it was a software prefetch (see above).
804 if (pkt) {
805 assert(!pkt->isWriteback());
806 // CleanEvicts corresponding to blocks which have
807 // outstanding requests in MSHRs are simply sunk here
808 if (pkt->cmd == MemCmd::CleanEvict) {
809 pendingDelete.reset(pkt);
810 } else {
811 DPRINTF(Cache, "%s coalescing MSHR for %s addr %#llx size %d\n",
812 __func__, pkt->cmdString(), pkt->getAddr(),
813 pkt->getSize());
814
815 assert(pkt->req->masterId() < system->maxMasters());
816 mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
817 // We use forward_time here because it is the same
818 // considering new targets. We have multiple
819 // requests for the same address here. It
820 // specifies the latency to allocate an internal
821 // buffer and to schedule an event to the queued
822 // port and also takes into account the additional
823 // delay of the xbar.
824 mshr->allocateTarget(pkt, forward_time, order++,
825 allocOnFill(pkt->cmd));
826 if (mshr->getNumTargets() == numTarget) {
827 noTargetMSHR = mshr;
828 setBlocked(Blocked_NoTargets);
829 // need to be careful with this... if this mshr isn't
830 // ready yet (i.e. time > curTick()), we don't want to
831 // move it ahead of mshrs that are ready
832 // mshrQueue.moveToFront(mshr);
833 }
834 }
835 // We should call the prefetcher reguardless if the request is
836 // satisfied or not, reguardless if the request is in the MSHR or
837 // not. The request could be a ReadReq hit, but still not
838 // satisfied (potentially because of a prior write to the same
839 // cache line. So, even when not satisfied, tehre is an MSHR
840 // already allocated for this, we need to let the prefetcher know
841 // about the request
842 if (prefetcher) {
843 // Don't notify on SWPrefetch
844 if (!pkt->cmd.isSWPrefetch())
845 next_pf_time = prefetcher->notify(pkt);
846 }
847 }
848 } else {
849 // no MSHR
850 assert(pkt->req->masterId() < system->maxMasters());
851 if (pkt->req->isUncacheable()) {
852 mshr_uncacheable[pkt->cmdToIndex()][pkt->req->masterId()]++;
853 } else {
854 mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
855 }
856
857 if (pkt->isEviction() ||
858 (pkt->req->isUncacheable() && pkt->isWrite())) {
859 // We use forward_time here because there is an
860 // uncached memory write, forwarded to WriteBuffer.
861 allocateWriteBuffer(pkt, forward_time);
862 } else {
863 if (blk && blk->isValid()) {
864 // should have flushed and have no valid block
865 assert(!pkt->req->isUncacheable());
866
867 // If we have a write miss to a valid block, we
868 // need to mark the block non-readable. Otherwise
869 // if we allow reads while there's an outstanding
870 // write miss, the read could return stale data
871 // out of the cache block... a more aggressive
872 // system could detect the overlap (if any) and
873 // forward data out of the MSHRs, but we don't do
874 // that yet. Note that we do need to leave the
875 // block valid so that it stays in the cache, in
876 // case we get an upgrade response (and hence no
877 // new data) when the write miss completes.
878 // As long as CPUs do proper store/load forwarding
879 // internally, and have a sufficiently weak memory
880 // model, this is probably unnecessary, but at some
881 // point it must have seemed like we needed it...
882 assert(pkt->needsWritable());
883 assert(!blk->isWritable());
884 blk->status &= ~BlkReadable;
885 }
886 // Here we are using forward_time, modelling the latency of
887 // a miss (outbound) just as forwardLatency, neglecting the
888 // lookupLatency component.
889 allocateMissBuffer(pkt, forward_time);
890 }
891
892 if (prefetcher) {
893 // Don't notify on SWPrefetch
894 if (!pkt->cmd.isSWPrefetch())
895 next_pf_time = prefetcher->notify(pkt);
896 }
897 }
898 }
899
900 if (next_pf_time != MaxTick)
901 schedMemSideSendEvent(next_pf_time);
902
903 return true;
904}
905
906
907// See comment in cache.hh.
908PacketPtr
909Cache::getBusPacket(PacketPtr cpu_pkt, CacheBlk *blk,
910 bool needsWritable) const
911{
912 bool blkValid = blk && blk->isValid();
913
914 if (cpu_pkt->req->isUncacheable()) {
915 // note that at the point we see the uncacheable request we
916 // flush any block, but there could be an outstanding MSHR,
917 // and the cache could have filled again before we actually
918 // send out the forwarded uncacheable request (blk could thus
919 // be non-null)
920 return NULL;
921 }
922
923 if (!blkValid &&
924 (cpu_pkt->isUpgrade() ||
925 cpu_pkt->isEviction())) {
926 // Writebacks that weren't allocated in access() and upgrades
927 // from upper-level caches that missed completely just go
928 // through.
929 return NULL;
930 }
931
932 assert(cpu_pkt->needsResponse());
933
934 MemCmd cmd;
935 // @TODO make useUpgrades a parameter.
936 // Note that ownership protocols require upgrade, otherwise a
937 // write miss on a shared owned block will generate a ReadExcl,
938 // which will clobber the owned copy.
939 const bool useUpgrades = true;
940 if (blkValid && useUpgrades) {
941 // only reason to be here is that blk is read only and we need
942 // it to be writable
943 assert(needsWritable);
944 assert(!blk->isWritable());
945 cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
946 } else if (cpu_pkt->cmd == MemCmd::SCUpgradeFailReq ||
947 cpu_pkt->cmd == MemCmd::StoreCondFailReq) {
948 // Even though this SC will fail, we still need to send out the
949 // request and get the data to supply it to other snoopers in the case
950 // where the determination the StoreCond fails is delayed due to
951 // all caches not being on the same local bus.
952 cmd = MemCmd::SCUpgradeFailReq;
953 } else if (cpu_pkt->cmd == MemCmd::WriteLineReq ||
954 cpu_pkt->cmd == MemCmd::InvalidateReq) {
955 // forward as invalidate to all other caches, this gives us
956 // the line in Exclusive state, and invalidates all other
957 // copies
958 cmd = MemCmd::InvalidateReq;
959 } else {
960 // block is invalid
961 cmd = needsWritable ? MemCmd::ReadExReq :
962 (isReadOnly ? MemCmd::ReadCleanReq : MemCmd::ReadSharedReq);
963 }
964 PacketPtr pkt = new Packet(cpu_pkt->req, cmd, blkSize);
965
966 // if there are upstream caches that have already marked the
967 // packet as having sharers (not passing writable), pass that info
968 // downstream
969 if (cpu_pkt->hasSharers()) {
970 // note that cpu_pkt may have spent a considerable time in the
971 // MSHR queue and that the information could possibly be out
972 // of date, however, there is no harm in conservatively
973 // assuming the block has sharers
974 pkt->setHasSharers();
975 DPRINTF(Cache, "%s passing hasSharers from %s to %s addr %#llx "
976 "size %d\n",
977 __func__, cpu_pkt->cmdString(), pkt->cmdString(),
978 pkt->getAddr(), pkt->getSize());
979 }
980
981 // the packet should be block aligned
982 assert(pkt->getAddr() == blockAlign(pkt->getAddr()));
983
984 pkt->allocate();
985 DPRINTF(Cache, "%s created %s from %s for addr %#llx size %d\n",
986 __func__, pkt->cmdString(), cpu_pkt->cmdString(), pkt->getAddr(),
987 pkt->getSize());
988 return pkt;
989}
990
991
992Tick
993Cache::recvAtomic(PacketPtr pkt)
994{
995 // We are in atomic mode so we pay just for lookupLatency here.
996 Cycles lat = lookupLatency;
997
998 // Forward the request if the system is in cache bypass mode.
999 if (system->bypassCaches())
1000 return ticksToCycles(memSidePort->sendAtomic(pkt));
1001
1002 promoteWholeLineWrites(pkt);
1003
1004 // follow the same flow as in recvTimingReq, and check if a cache
1005 // above us is responding
1006 if (pkt->cacheResponding()) {
1007 DPRINTF(Cache, "Cache above responding to %#llx (%s): "
1008 "not responding\n",
1009 pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
1010
1011 // if a cache is responding, and it had the line in Owned
1012 // rather than Modified state, we need to invalidate any
1013 // copies that are not on the same path to memory
1014 assert(pkt->needsWritable() && !pkt->responderHadWritable());
1015 lat += ticksToCycles(memSidePort->sendAtomic(pkt));
1016
1017 return lat * clockPeriod();
1018 }
1019
1020 // should assert here that there are no outstanding MSHRs or
1021 // writebacks... that would mean that someone used an atomic
1022 // access in timing mode
1023
1024 CacheBlk *blk = NULL;
1025 PacketList writebacks;
1026 bool satisfied = access(pkt, blk, lat, writebacks);
1027
1028 // handle writebacks resulting from the access here to ensure they
1029 // logically proceed anything happening below
1030 doWritebacksAtomic(writebacks);
1031
1032 if (!satisfied) {
1033 // MISS
1034
1035 PacketPtr bus_pkt = getBusPacket(pkt, blk, pkt->needsWritable());
1036
1037 bool is_forward = (bus_pkt == NULL);
1038
1039 if (is_forward) {
1040 // just forwarding the same request to the next level
1041 // no local cache operation involved
1042 bus_pkt = pkt;
1043 }
1044
1045 DPRINTF(Cache, "Sending an atomic %s for %#llx (%s)\n",
1046 bus_pkt->cmdString(), bus_pkt->getAddr(),
1047 bus_pkt->isSecure() ? "s" : "ns");
1048
1049#if TRACING_ON
1050 CacheBlk::State old_state = blk ? blk->status : 0;
1051#endif
1052
1053 lat += ticksToCycles(memSidePort->sendAtomic(bus_pkt));
1054
1055 // We are now dealing with the response handling
1056 DPRINTF(Cache, "Receive response: %s for addr %#llx (%s) in state %i\n",
1057 bus_pkt->cmdString(), bus_pkt->getAddr(),
1058 bus_pkt->isSecure() ? "s" : "ns",
1059 old_state);
1060
1061 // If packet was a forward, the response (if any) is already
1062 // in place in the bus_pkt == pkt structure, so we don't need
1063 // to do anything. Otherwise, use the separate bus_pkt to
1064 // generate response to pkt and then delete it.
1065 if (!is_forward) {
1066 if (pkt->needsResponse()) {
1067 assert(bus_pkt->isResponse());
1068 if (bus_pkt->isError()) {
1069 pkt->makeAtomicResponse();
1070 pkt->copyError(bus_pkt);
1071 } else if (pkt->cmd == MemCmd::InvalidateReq) {
1072 if (blk) {
1073 // invalidate response to a cache that received
1074 // an invalidate request
1075 satisfyCpuSideRequest(pkt, blk);
1076 }
1077 } else if (pkt->cmd == MemCmd::WriteLineReq) {
1078 // note the use of pkt, not bus_pkt here.
1079
1080 // write-line request to the cache that promoted
1081 // the write to a whole line
1082 blk = handleFill(pkt, blk, writebacks,
1083 allocOnFill(pkt->cmd));
1084 satisfyCpuSideRequest(pkt, blk);
1085 } else if (bus_pkt->isRead() ||
1086 bus_pkt->cmd == MemCmd::UpgradeResp) {
1087 // we're updating cache state to allow us to
1088 // satisfy the upstream request from the cache
1089 blk = handleFill(bus_pkt, blk, writebacks,
1090 allocOnFill(pkt->cmd));
1091 satisfyCpuSideRequest(pkt, blk);
1092 } else {
1093 // we're satisfying the upstream request without
1094 // modifying cache state, e.g., a write-through
1095 pkt->makeAtomicResponse();
1096 }
1097 }
1098 delete bus_pkt;
1099 }
1100 }
1101
1102 // Note that we don't invoke the prefetcher at all in atomic mode.
1103 // It's not clear how to do it properly, particularly for
1104 // prefetchers that aggressively generate prefetch candidates and
1105 // rely on bandwidth contention to throttle them; these will tend
1106 // to pollute the cache in atomic mode since there is no bandwidth
1107 // contention. If we ever do want to enable prefetching in atomic
1108 // mode, though, this is the place to do it... see timingAccess()
1109 // for an example (though we'd want to issue the prefetch(es)
1110 // immediately rather than calling requestMemSideBus() as we do
1111 // there).
1112
1113 // do any writebacks resulting from the response handling
1114 doWritebacksAtomic(writebacks);
1115
1116 // if we used temp block, check to see if its valid and if so
1117 // clear it out, but only do so after the call to recvAtomic is
1118 // finished so that any downstream observers (such as a snoop
1119 // filter), first see the fill, and only then see the eviction
1120 if (blk == tempBlock && tempBlock->isValid()) {
1121 // the atomic CPU calls recvAtomic for fetch and load/store
1122 // sequentuially, and we may already have a tempBlock
1123 // writeback from the fetch that we have not yet sent
1124 if (tempBlockWriteback) {
1125 // if that is the case, write the prevoius one back, and
1126 // do not schedule any new event
1127 writebackTempBlockAtomic();
1128 } else {
1129 // the writeback/clean eviction happens after the call to
1130 // recvAtomic has finished (but before any successive
1131 // calls), so that the response handling from the fill is
1132 // allowed to happen first
1133 schedule(writebackTempBlockAtomicEvent, curTick());
1134 }
1135
1136 tempBlockWriteback = (blk->isDirty() || writebackClean) ?
1137 writebackBlk(blk) : cleanEvictBlk(blk);
1138 blk->invalidate();
1139 }
1140
1141 if (pkt->needsResponse()) {
1142 pkt->makeAtomicResponse();
1143 }
1144
1145 return lat * clockPeriod();
1146}
1147
1148
1149void
1150Cache::functionalAccess(PacketPtr pkt, bool fromCpuSide)
1151{
1152 if (system->bypassCaches()) {
1153 // Packets from the memory side are snoop request and
1154 // shouldn't happen in bypass mode.
1155 assert(fromCpuSide);
1156
1157 // The cache should be flushed if we are in cache bypass mode,
1158 // so we don't need to check if we need to update anything.
1159 memSidePort->sendFunctional(pkt);
1160 return;
1161 }
1162
1163 Addr blk_addr = blockAlign(pkt->getAddr());
1164 bool is_secure = pkt->isSecure();
1165 CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
1166 MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
1167
1168 pkt->pushLabel(name());
1169
1170 CacheBlkPrintWrapper cbpw(blk);
1171
1172 // Note that just because an L2/L3 has valid data doesn't mean an
1173 // L1 doesn't have a more up-to-date modified copy that still
1174 // needs to be found. As a result we always update the request if
1175 // we have it, but only declare it satisfied if we are the owner.
1176
1177 // see if we have data at all (owned or otherwise)
1178 bool have_data = blk && blk->isValid()
1179 && pkt->checkFunctional(&cbpw, blk_addr, is_secure, blkSize,
1180 blk->data);
1181
1182 // data we have is dirty if marked as such or if we have an
1183 // in-service MSHR that is pending a modified line
1184 bool have_dirty =
1185 have_data && (blk->isDirty() ||
1186 (mshr && mshr->inService && mshr->isPendingModified()));
1187
1188 bool done = have_dirty
1189 || cpuSidePort->checkFunctional(pkt)
1190 || mshrQueue.checkFunctional(pkt, blk_addr)
1191 || writeBuffer.checkFunctional(pkt, blk_addr)
1192 || memSidePort->checkFunctional(pkt);
1193
1194 DPRINTF(CacheVerbose, "functional %s %#llx (%s) %s%s%s\n",
1195 pkt->cmdString(), pkt->getAddr(), is_secure ? "s" : "ns",
1196 (blk && blk->isValid()) ? "valid " : "",
1197 have_data ? "data " : "", done ? "done " : "");
1198
1199 // We're leaving the cache, so pop cache->name() label
1200 pkt->popLabel();
1201
1202 if (done) {
1203 pkt->makeResponse();
1204 } else {
1205 // if it came as a request from the CPU side then make sure it
1206 // continues towards the memory side
1207 if (fromCpuSide) {
1208 memSidePort->sendFunctional(pkt);
1209 } else if (forwardSnoops && cpuSidePort->isSnooping()) {
1210 // if it came from the memory side, it must be a snoop request
1211 // and we should only forward it if we are forwarding snoops
1212 cpuSidePort->sendFunctionalSnoop(pkt);
1213 }
1214 }
1215}
1216
1217
1218/////////////////////////////////////////////////////
1219//
1220// Response handling: responses from the memory side
1221//
1222/////////////////////////////////////////////////////
1223
1224
1225void
1226Cache::handleUncacheableWriteResp(PacketPtr pkt)
1227{
1228 WriteQueueEntry *wq_entry =
1229 dynamic_cast<WriteQueueEntry*>(pkt->senderState);
1230 assert(wq_entry);
1231
1232 WriteQueueEntry::Target *target = wq_entry->getTarget();
1233 Packet *tgt_pkt = target->pkt;
1234
1235 // we send out invalidation reqs and get invalidation
1236 // responses for write-line requests
1237 assert(tgt_pkt->cmd != MemCmd::WriteLineReq);
1238
1239 int stats_cmd_idx = tgt_pkt->cmdToIndex();
1240 Tick miss_latency = curTick() - target->recvTime;
1241 assert(pkt->req->masterId() < system->maxMasters());
1242 mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
1243 miss_latency;
1244
1245 tgt_pkt->makeTimingResponse();
1246 // if this packet is an error copy that to the new packet
1247 if (pkt->isError())
1248 tgt_pkt->copyError(pkt);
1249 // Reset the bus additional time as it is now accounted for
1250 tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
1251 Tick completion_time = clockEdge(responseLatency) +
1252 pkt->headerDelay + pkt->payloadDelay;
1253
1254 cpuSidePort->schedTimingResp(tgt_pkt, completion_time, true);
1255
1256 wq_entry->popTarget();
1257 assert(!wq_entry->hasTargets());
1258
1259 bool wasFull = writeBuffer.isFull();
1260 writeBuffer.deallocate(wq_entry);
1261
1262 if (wasFull && !writeBuffer.isFull()) {
1263 clearBlocked(Blocked_NoWBBuffers);
1264 }
1265
1266 delete pkt;
1267}
1268
1269void
1242Cache::recvTimingResp(PacketPtr pkt)
1243{
1244 assert(pkt->isResponse());
1245
1246 // all header delay should be paid for by the crossbar, unless
1247 // this is a prefetch response from above
1248 panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
1249 "%s saw a non-zero packet delay\n", name());
1250
1270Cache::recvTimingResp(PacketPtr pkt)
1271{
1272 assert(pkt->isResponse());
1273
1274 // all header delay should be paid for by the crossbar, unless
1275 // this is a prefetch response from above
1276 panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
1277 "%s saw a non-zero packet delay\n", name());
1278
1251 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
1252 bool is_error = pkt->isError();
1253
1279 bool is_error = pkt->isError();
1280
1254 assert(mshr);
1255
1256 if (is_error) {
1257 DPRINTF(Cache, "Cache received packet with error for addr %#llx (%s), "
1258 "cmd: %s\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns",
1259 pkt->cmdString());
1260 }
1261
1262 DPRINTF(Cache, "Handling response %s for addr %#llx size %d (%s)\n",
1263 pkt->cmdString(), pkt->getAddr(), pkt->getSize(),
1264 pkt->isSecure() ? "s" : "ns");
1265
1281 if (is_error) {
1282 DPRINTF(Cache, "Cache received packet with error for addr %#llx (%s), "
1283 "cmd: %s\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns",
1284 pkt->cmdString());
1285 }
1286
1287 DPRINTF(Cache, "Handling response %s for addr %#llx size %d (%s)\n",
1288 pkt->cmdString(), pkt->getAddr(), pkt->getSize(),
1289 pkt->isSecure() ? "s" : "ns");
1290
1266 MSHRQueue *mq = mshr->queue;
1267 bool wasFull = mq->isFull();
1291 // if this is a write, we should be looking at an uncacheable
1292 // write
1293 if (pkt->isWrite()) {
1294 assert(pkt->req->isUncacheable());
1295 handleUncacheableWriteResp(pkt);
1296 return;
1297 }
1268
1298
1299 // we have dealt with any (uncacheable) writes above, from here on
1300 // we know we are dealing with an MSHR due to a miss or a prefetch
1301 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
1302 assert(mshr);
1303
1269 if (mshr == noTargetMSHR) {
1270 // we always clear at least one target
1271 clearBlocked(Blocked_NoTargets);
1272 noTargetMSHR = NULL;
1273 }
1274
1275 // Initial target is used just for stats
1276 MSHR::Target *initial_tgt = mshr->getTarget();
1277 int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
1278 Tick miss_latency = curTick() - initial_tgt->recvTime;
1304 if (mshr == noTargetMSHR) {
1305 // we always clear at least one target
1306 clearBlocked(Blocked_NoTargets);
1307 noTargetMSHR = NULL;
1308 }
1309
1310 // Initial target is used just for stats
1311 MSHR::Target *initial_tgt = mshr->getTarget();
1312 int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
1313 Tick miss_latency = curTick() - initial_tgt->recvTime;
1279 PacketList writebacks;
1280 // We need forward_time here because we have a call of
1281 // allocateWriteBuffer() that need this parameter to specify the
1282 // time to request the bus. In this case we use forward latency
1283 // because there is a writeback. We pay also here for headerDelay
1284 // that is charged of bus latencies if the packet comes from the
1285 // bus.
1286 Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
1287
1288 if (pkt->req->isUncacheable()) {
1289 assert(pkt->req->masterId() < system->maxMasters());
1290 mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
1291 miss_latency;
1292 } else {
1293 assert(pkt->req->masterId() < system->maxMasters());
1294 mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
1295 miss_latency;
1296 }
1297
1314
1315 if (pkt->req->isUncacheable()) {
1316 assert(pkt->req->masterId() < system->maxMasters());
1317 mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
1318 miss_latency;
1319 } else {
1320 assert(pkt->req->masterId() < system->maxMasters());
1321 mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
1322 miss_latency;
1323 }
1324
1325 bool wasFull = mshrQueue.isFull();
1326
1327 PacketList writebacks;
1328
1329 Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
1330
1298 // upgrade deferred targets if the response has no sharers, and is
1299 // thus passing writable
1300 if (!pkt->hasSharers()) {
1301 mshr->promoteWritable();
1302 }
1303
1304 bool is_fill = !mshr->isForward &&
1305 (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
1306
1307 CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
1308
1309 if (is_fill && !is_error) {
1310 DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
1311 pkt->getAddr());
1312
1313 blk = handleFill(pkt, blk, writebacks, mshr->allocOnFill);
1314 assert(blk != NULL);
1315 }
1316
1317 // allow invalidation responses originating from write-line
1318 // requests to be discarded
1319 bool is_invalidate = pkt->isInvalidate();
1320
1321 // First offset for critical word first calculations
1322 int initial_offset = initial_tgt->pkt->getOffset(blkSize);
1323
1324 while (mshr->hasTargets()) {
1325 MSHR::Target *target = mshr->getTarget();
1326 Packet *tgt_pkt = target->pkt;
1327
1328 switch (target->source) {
1329 case MSHR::Target::FromCPU:
1330 Tick completion_time;
1331 // Here we charge on completion_time the delay of the xbar if the
1332 // packet comes from it, charged on headerDelay.
1333 completion_time = pkt->headerDelay;
1334
1335 // Software prefetch handling for cache closest to core
1336 if (tgt_pkt->cmd.isSWPrefetch()) {
1337 // a software prefetch would have already been ack'd immediately
1338 // with dummy data so the core would be able to retire it.
1339 // this request completes right here, so we deallocate it.
1340 delete tgt_pkt->req;
1341 delete tgt_pkt;
1342 break; // skip response
1343 }
1344
1345 // unlike the other packet flows, where data is found in other
1346 // caches or memory and brought back, write-line requests always
1347 // have the data right away, so the above check for "is fill?"
1348 // cannot actually be determined until examining the stored MSHR
1349 // state. We "catch up" with that logic here, which is duplicated
1350 // from above.
1351 if (tgt_pkt->cmd == MemCmd::WriteLineReq) {
1352 assert(!is_error);
1353 // we got the block in a writable state, so promote
1354 // any deferred targets if possible
1355 mshr->promoteWritable();
1356 // NB: we use the original packet here and not the response!
1357 blk = handleFill(tgt_pkt, blk, writebacks, mshr->allocOnFill);
1358 assert(blk != NULL);
1359
1360 // treat as a fill, and discard the invalidation
1361 // response
1362 is_fill = true;
1363 is_invalidate = false;
1364 }
1365
1366 if (is_fill) {
1367 satisfyCpuSideRequest(tgt_pkt, blk,
1368 true, mshr->hasPostDowngrade());
1369
1370 // How many bytes past the first request is this one
1371 int transfer_offset =
1372 tgt_pkt->getOffset(blkSize) - initial_offset;
1373 if (transfer_offset < 0) {
1374 transfer_offset += blkSize;
1375 }
1376
1377 // If not critical word (offset) return payloadDelay.
1378 // responseLatency is the latency of the return path
1379 // from lower level caches/memory to an upper level cache or
1380 // the core.
1381 completion_time += clockEdge(responseLatency) +
1382 (transfer_offset ? pkt->payloadDelay : 0);
1383
1384 assert(!tgt_pkt->req->isUncacheable());
1385
1386 assert(tgt_pkt->req->masterId() < system->maxMasters());
1387 missLatency[tgt_pkt->cmdToIndex()][tgt_pkt->req->masterId()] +=
1388 completion_time - target->recvTime;
1389 } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
1390 // failed StoreCond upgrade
1391 assert(tgt_pkt->cmd == MemCmd::StoreCondReq ||
1392 tgt_pkt->cmd == MemCmd::StoreCondFailReq ||
1393 tgt_pkt->cmd == MemCmd::SCUpgradeFailReq);
1394 // responseLatency is the latency of the return path
1395 // from lower level caches/memory to an upper level cache or
1396 // the core.
1397 completion_time += clockEdge(responseLatency) +
1398 pkt->payloadDelay;
1399 tgt_pkt->req->setExtraData(0);
1400 } else {
1401 // not a cache fill, just forwarding response
1402 // responseLatency is the latency of the return path
1403 // from lower level cahces/memory to the core.
1404 completion_time += clockEdge(responseLatency) +
1405 pkt->payloadDelay;
1406 if (pkt->isRead() && !is_error) {
1407 // sanity check
1408 assert(pkt->getAddr() == tgt_pkt->getAddr());
1409 assert(pkt->getSize() >= tgt_pkt->getSize());
1410
1411 tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
1412 }
1413 }
1414 tgt_pkt->makeTimingResponse();
1415 // if this packet is an error copy that to the new packet
1416 if (is_error)
1417 tgt_pkt->copyError(pkt);
1418 if (tgt_pkt->cmd == MemCmd::ReadResp &&
1419 (is_invalidate || mshr->hasPostInvalidate())) {
1420 // If intermediate cache got ReadRespWithInvalidate,
1421 // propagate that. Response should not have
1422 // isInvalidate() set otherwise.
1423 tgt_pkt->cmd = MemCmd::ReadRespWithInvalidate;
1424 DPRINTF(Cache, "%s updated cmd to %s for addr %#llx\n",
1425 __func__, tgt_pkt->cmdString(), tgt_pkt->getAddr());
1426 }
1427 // Reset the bus additional time as it is now accounted for
1428 tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
1429 cpuSidePort->schedTimingResp(tgt_pkt, completion_time, true);
1430 break;
1431
1432 case MSHR::Target::FromPrefetcher:
1433 assert(tgt_pkt->cmd == MemCmd::HardPFReq);
1434 if (blk)
1435 blk->status |= BlkHWPrefetched;
1436 delete tgt_pkt->req;
1437 delete tgt_pkt;
1438 break;
1439
1440 case MSHR::Target::FromSnoop:
1441 // I don't believe that a snoop can be in an error state
1442 assert(!is_error);
1443 // response to snoop request
1444 DPRINTF(Cache, "processing deferred snoop...\n");
1445 assert(!(is_invalidate && !mshr->hasPostInvalidate()));
1446 handleSnoop(tgt_pkt, blk, true, true, mshr->hasPostInvalidate());
1447 break;
1448
1449 default:
1450 panic("Illegal target->source enum %d\n", target->source);
1451 }
1452
1453 mshr->popTarget();
1454 }
1455
1456 if (blk && blk->isValid()) {
1457 // an invalidate response stemming from a write line request
1458 // should not invalidate the block, so check if the
1459 // invalidation should be discarded
1460 if (is_invalidate || mshr->hasPostInvalidate()) {
1461 invalidateBlock(blk);
1462 } else if (mshr->hasPostDowngrade()) {
1463 blk->status &= ~BlkWritable;
1464 }
1465 }
1466
1467 if (mshr->promoteDeferredTargets()) {
1468 // avoid later read getting stale data while write miss is
1469 // outstanding.. see comment in timingAccess()
1470 if (blk) {
1471 blk->status &= ~BlkReadable;
1472 }
1331 // upgrade deferred targets if the response has no sharers, and is
1332 // thus passing writable
1333 if (!pkt->hasSharers()) {
1334 mshr->promoteWritable();
1335 }
1336
1337 bool is_fill = !mshr->isForward &&
1338 (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
1339
1340 CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
1341
1342 if (is_fill && !is_error) {
1343 DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
1344 pkt->getAddr());
1345
1346 blk = handleFill(pkt, blk, writebacks, mshr->allocOnFill);
1347 assert(blk != NULL);
1348 }
1349
1350 // allow invalidation responses originating from write-line
1351 // requests to be discarded
1352 bool is_invalidate = pkt->isInvalidate();
1353
1354 // First offset for critical word first calculations
1355 int initial_offset = initial_tgt->pkt->getOffset(blkSize);
1356
1357 while (mshr->hasTargets()) {
1358 MSHR::Target *target = mshr->getTarget();
1359 Packet *tgt_pkt = target->pkt;
1360
1361 switch (target->source) {
1362 case MSHR::Target::FromCPU:
1363 Tick completion_time;
1364 // Here we charge on completion_time the delay of the xbar if the
1365 // packet comes from it, charged on headerDelay.
1366 completion_time = pkt->headerDelay;
1367
1368 // Software prefetch handling for cache closest to core
1369 if (tgt_pkt->cmd.isSWPrefetch()) {
1370 // a software prefetch would have already been ack'd immediately
1371 // with dummy data so the core would be able to retire it.
1372 // this request completes right here, so we deallocate it.
1373 delete tgt_pkt->req;
1374 delete tgt_pkt;
1375 break; // skip response
1376 }
1377
1378 // unlike the other packet flows, where data is found in other
1379 // caches or memory and brought back, write-line requests always
1380 // have the data right away, so the above check for "is fill?"
1381 // cannot actually be determined until examining the stored MSHR
1382 // state. We "catch up" with that logic here, which is duplicated
1383 // from above.
1384 if (tgt_pkt->cmd == MemCmd::WriteLineReq) {
1385 assert(!is_error);
1386 // we got the block in a writable state, so promote
1387 // any deferred targets if possible
1388 mshr->promoteWritable();
1389 // NB: we use the original packet here and not the response!
1390 blk = handleFill(tgt_pkt, blk, writebacks, mshr->allocOnFill);
1391 assert(blk != NULL);
1392
1393 // treat as a fill, and discard the invalidation
1394 // response
1395 is_fill = true;
1396 is_invalidate = false;
1397 }
1398
1399 if (is_fill) {
1400 satisfyCpuSideRequest(tgt_pkt, blk,
1401 true, mshr->hasPostDowngrade());
1402
1403 // How many bytes past the first request is this one
1404 int transfer_offset =
1405 tgt_pkt->getOffset(blkSize) - initial_offset;
1406 if (transfer_offset < 0) {
1407 transfer_offset += blkSize;
1408 }
1409
1410 // If not critical word (offset) return payloadDelay.
1411 // responseLatency is the latency of the return path
1412 // from lower level caches/memory to an upper level cache or
1413 // the core.
1414 completion_time += clockEdge(responseLatency) +
1415 (transfer_offset ? pkt->payloadDelay : 0);
1416
1417 assert(!tgt_pkt->req->isUncacheable());
1418
1419 assert(tgt_pkt->req->masterId() < system->maxMasters());
1420 missLatency[tgt_pkt->cmdToIndex()][tgt_pkt->req->masterId()] +=
1421 completion_time - target->recvTime;
1422 } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
1423 // failed StoreCond upgrade
1424 assert(tgt_pkt->cmd == MemCmd::StoreCondReq ||
1425 tgt_pkt->cmd == MemCmd::StoreCondFailReq ||
1426 tgt_pkt->cmd == MemCmd::SCUpgradeFailReq);
1427 // responseLatency is the latency of the return path
1428 // from lower level caches/memory to an upper level cache or
1429 // the core.
1430 completion_time += clockEdge(responseLatency) +
1431 pkt->payloadDelay;
1432 tgt_pkt->req->setExtraData(0);
1433 } else {
1434 // not a cache fill, just forwarding response
1435 // responseLatency is the latency of the return path
1436 // from lower level cahces/memory to the core.
1437 completion_time += clockEdge(responseLatency) +
1438 pkt->payloadDelay;
1439 if (pkt->isRead() && !is_error) {
1440 // sanity check
1441 assert(pkt->getAddr() == tgt_pkt->getAddr());
1442 assert(pkt->getSize() >= tgt_pkt->getSize());
1443
1444 tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
1445 }
1446 }
1447 tgt_pkt->makeTimingResponse();
1448 // if this packet is an error copy that to the new packet
1449 if (is_error)
1450 tgt_pkt->copyError(pkt);
1451 if (tgt_pkt->cmd == MemCmd::ReadResp &&
1452 (is_invalidate || mshr->hasPostInvalidate())) {
1453 // If intermediate cache got ReadRespWithInvalidate,
1454 // propagate that. Response should not have
1455 // isInvalidate() set otherwise.
1456 tgt_pkt->cmd = MemCmd::ReadRespWithInvalidate;
1457 DPRINTF(Cache, "%s updated cmd to %s for addr %#llx\n",
1458 __func__, tgt_pkt->cmdString(), tgt_pkt->getAddr());
1459 }
1460 // Reset the bus additional time as it is now accounted for
1461 tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
1462 cpuSidePort->schedTimingResp(tgt_pkt, completion_time, true);
1463 break;
1464
1465 case MSHR::Target::FromPrefetcher:
1466 assert(tgt_pkt->cmd == MemCmd::HardPFReq);
1467 if (blk)
1468 blk->status |= BlkHWPrefetched;
1469 delete tgt_pkt->req;
1470 delete tgt_pkt;
1471 break;
1472
1473 case MSHR::Target::FromSnoop:
1474 // I don't believe that a snoop can be in an error state
1475 assert(!is_error);
1476 // response to snoop request
1477 DPRINTF(Cache, "processing deferred snoop...\n");
1478 assert(!(is_invalidate && !mshr->hasPostInvalidate()));
1479 handleSnoop(tgt_pkt, blk, true, true, mshr->hasPostInvalidate());
1480 break;
1481
1482 default:
1483 panic("Illegal target->source enum %d\n", target->source);
1484 }
1485
1486 mshr->popTarget();
1487 }
1488
1489 if (blk && blk->isValid()) {
1490 // an invalidate response stemming from a write line request
1491 // should not invalidate the block, so check if the
1492 // invalidation should be discarded
1493 if (is_invalidate || mshr->hasPostInvalidate()) {
1494 invalidateBlock(blk);
1495 } else if (mshr->hasPostDowngrade()) {
1496 blk->status &= ~BlkWritable;
1497 }
1498 }
1499
1500 if (mshr->promoteDeferredTargets()) {
1501 // avoid later read getting stale data while write miss is
1502 // outstanding.. see comment in timingAccess()
1503 if (blk) {
1504 blk->status &= ~BlkReadable;
1505 }
1473 mq = mshr->queue;
1474 mq->markPending(mshr);
1506 mshrQueue.markPending(mshr);
1475 schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
1476 } else {
1507 schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
1508 } else {
1477 mq->deallocate(mshr);
1478 if (wasFull && !mq->isFull()) {
1479 clearBlocked((BlockedCause)mq->index);
1509 mshrQueue.deallocate(mshr);
1510 if (wasFull && !mshrQueue.isFull()) {
1511 clearBlocked(Blocked_NoMSHRs);
1480 }
1481
1482 // Request the bus for a prefetch if this deallocation freed enough
1483 // MSHRs for a prefetch to take place
1512 }
1513
1514 // Request the bus for a prefetch if this deallocation freed enough
1515 // MSHRs for a prefetch to take place
1484 if (prefetcher && mq == &mshrQueue && mshrQueue.canPrefetch()) {
1516 if (prefetcher && mshrQueue.canPrefetch()) {
1485 Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
1486 clockEdge());
1487 if (next_pf_time != MaxTick)
1488 schedMemSideSendEvent(next_pf_time);
1489 }
1490 }
1491 // reset the xbar additional timinig as it is now accounted for
1492 pkt->headerDelay = pkt->payloadDelay = 0;
1493
1494 // copy writebacks to write buffer
1495 doWritebacks(writebacks, forward_time);
1496
1497 // if we used temp block, check to see if its valid and then clear it out
1498 if (blk == tempBlock && tempBlock->isValid()) {
1499 // We use forwardLatency here because we are copying
1500 // Writebacks/CleanEvicts to write buffer. It specifies the latency to
1501 // allocate an internal buffer and to schedule an event to the
1502 // queued port.
1503 if (blk->isDirty() || writebackClean) {
1504 PacketPtr wbPkt = writebackBlk(blk);
1505 allocateWriteBuffer(wbPkt, forward_time);
1506 // Set BLOCK_CACHED flag if cached above.
1507 if (isCachedAbove(wbPkt))
1508 wbPkt->setBlockCached();
1509 } else {
1510 PacketPtr wcPkt = cleanEvictBlk(blk);
1511 // Check to see if block is cached above. If not allocate
1512 // write buffer
1513 if (isCachedAbove(wcPkt))
1514 delete wcPkt;
1515 else
1516 allocateWriteBuffer(wcPkt, forward_time);
1517 }
1518 blk->invalidate();
1519 }
1520
1521 DPRINTF(CacheVerbose, "Leaving %s with %s for addr %#llx\n", __func__,
1522 pkt->cmdString(), pkt->getAddr());
1523 delete pkt;
1524}
1525
1526PacketPtr
1527Cache::writebackBlk(CacheBlk *blk)
1528{
1529 chatty_assert(!isReadOnly || writebackClean,
1530 "Writeback from read-only cache");
1531 assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
1532
1533 writebacks[Request::wbMasterId]++;
1534
1535 Request *req = new Request(tags->regenerateBlkAddr(blk->tag, blk->set),
1536 blkSize, 0, Request::wbMasterId);
1537 if (blk->isSecure())
1538 req->setFlags(Request::SECURE);
1539
1540 req->taskId(blk->task_id);
1541 blk->task_id= ContextSwitchTaskId::Unknown;
1542 blk->tickInserted = curTick();
1543
1544 PacketPtr pkt =
1545 new Packet(req, blk->isDirty() ?
1546 MemCmd::WritebackDirty : MemCmd::WritebackClean);
1547
1548 DPRINTF(Cache, "Create Writeback %#llx writable: %d, dirty: %d\n",
1549 pkt->getAddr(), blk->isWritable(), blk->isDirty());
1550
1551 if (blk->isWritable()) {
1552 // not asserting shared means we pass the block in modified
1553 // state, mark our own block non-writeable
1554 blk->status &= ~BlkWritable;
1555 } else {
1556 // we are in the Owned state, tell the receiver
1557 pkt->setHasSharers();
1558 }
1559
1560 // make sure the block is not marked dirty
1561 blk->status &= ~BlkDirty;
1562
1563 pkt->allocate();
1564 std::memcpy(pkt->getPtr<uint8_t>(), blk->data, blkSize);
1565
1566 return pkt;
1567}
1568
1569PacketPtr
1570Cache::cleanEvictBlk(CacheBlk *blk)
1571{
1572 assert(!writebackClean);
1573 assert(blk && blk->isValid() && !blk->isDirty());
1574 // Creating a zero sized write, a message to the snoop filter
1575 Request *req =
1576 new Request(tags->regenerateBlkAddr(blk->tag, blk->set), blkSize, 0,
1577 Request::wbMasterId);
1578 if (blk->isSecure())
1579 req->setFlags(Request::SECURE);
1580
1581 req->taskId(blk->task_id);
1582 blk->task_id = ContextSwitchTaskId::Unknown;
1583 blk->tickInserted = curTick();
1584
1585 PacketPtr pkt = new Packet(req, MemCmd::CleanEvict);
1586 pkt->allocate();
1587 DPRINTF(Cache, "%s%s %x Create CleanEvict\n", pkt->cmdString(),
1588 pkt->req->isInstFetch() ? " (ifetch)" : "",
1589 pkt->getAddr());
1590
1591 return pkt;
1592}
1593
1594void
1595Cache::memWriteback()
1596{
1597 CacheBlkVisitorWrapper visitor(*this, &Cache::writebackVisitor);
1598 tags->forEachBlk(visitor);
1599}
1600
1601void
1602Cache::memInvalidate()
1603{
1604 CacheBlkVisitorWrapper visitor(*this, &Cache::invalidateVisitor);
1605 tags->forEachBlk(visitor);
1606}
1607
1608bool
1609Cache::isDirty() const
1610{
1611 CacheBlkIsDirtyVisitor visitor;
1612 tags->forEachBlk(visitor);
1613
1614 return visitor.isDirty();
1615}
1616
1617bool
1618Cache::writebackVisitor(CacheBlk &blk)
1619{
1620 if (blk.isDirty()) {
1621 assert(blk.isValid());
1622
1623 Request request(tags->regenerateBlkAddr(blk.tag, blk.set),
1624 blkSize, 0, Request::funcMasterId);
1625 request.taskId(blk.task_id);
1626
1627 Packet packet(&request, MemCmd::WriteReq);
1628 packet.dataStatic(blk.data);
1629
1630 memSidePort->sendFunctional(&packet);
1631
1632 blk.status &= ~BlkDirty;
1633 }
1634
1635 return true;
1636}
1637
1638bool
1639Cache::invalidateVisitor(CacheBlk &blk)
1640{
1641
1642 if (blk.isDirty())
1643 warn_once("Invalidating dirty cache lines. Expect things to break.\n");
1644
1645 if (blk.isValid()) {
1646 assert(!blk.isDirty());
1647 tags->invalidate(&blk);
1648 blk.invalidate();
1649 }
1650
1651 return true;
1652}
1653
1654CacheBlk*
1655Cache::allocateBlock(Addr addr, bool is_secure, PacketList &writebacks)
1656{
1657 CacheBlk *blk = tags->findVictim(addr);
1658
1659 // It is valid to return NULL if there is no victim
1660 if (!blk)
1661 return nullptr;
1662
1663 if (blk->isValid()) {
1664 Addr repl_addr = tags->regenerateBlkAddr(blk->tag, blk->set);
1665 MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
1666 if (repl_mshr) {
1667 // must be an outstanding upgrade request
1668 // on a block we're about to replace...
1669 assert(!blk->isWritable() || blk->isDirty());
1670 assert(repl_mshr->needsWritable());
1671 // too hard to replace block with transient state
1672 // allocation failed, block not inserted
1673 return NULL;
1674 } else {
1675 DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx (%s): %s\n",
1676 repl_addr, blk->isSecure() ? "s" : "ns",
1677 addr, is_secure ? "s" : "ns",
1678 blk->isDirty() ? "writeback" : "clean");
1679
1680 // Will send up Writeback/CleanEvict snoops via isCachedAbove
1681 // when pushing this writeback list into the write buffer.
1682 if (blk->isDirty() || writebackClean) {
1683 // Save writeback packet for handling by caller
1684 writebacks.push_back(writebackBlk(blk));
1685 } else {
1686 writebacks.push_back(cleanEvictBlk(blk));
1687 }
1688 }
1689 }
1690
1691 return blk;
1692}
1693
1694void
1695Cache::invalidateBlock(CacheBlk *blk)
1696{
1697 if (blk != tempBlock)
1698 tags->invalidate(blk);
1699 blk->invalidate();
1700}
1701
1702// Note that the reason we return a list of writebacks rather than
1703// inserting them directly in the write buffer is that this function
1704// is called by both atomic and timing-mode accesses, and in atomic
1705// mode we don't mess with the write buffer (we just perform the
1706// writebacks atomically once the original request is complete).
1707CacheBlk*
1708Cache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
1709 bool allocate)
1710{
1711 assert(pkt->isResponse() || pkt->cmd == MemCmd::WriteLineReq);
1712 Addr addr = pkt->getAddr();
1713 bool is_secure = pkt->isSecure();
1714#if TRACING_ON
1715 CacheBlk::State old_state = blk ? blk->status : 0;
1716#endif
1717
1517 Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
1518 clockEdge());
1519 if (next_pf_time != MaxTick)
1520 schedMemSideSendEvent(next_pf_time);
1521 }
1522 }
1523 // reset the xbar additional timinig as it is now accounted for
1524 pkt->headerDelay = pkt->payloadDelay = 0;
1525
1526 // copy writebacks to write buffer
1527 doWritebacks(writebacks, forward_time);
1528
1529 // if we used temp block, check to see if its valid and then clear it out
1530 if (blk == tempBlock && tempBlock->isValid()) {
1531 // We use forwardLatency here because we are copying
1532 // Writebacks/CleanEvicts to write buffer. It specifies the latency to
1533 // allocate an internal buffer and to schedule an event to the
1534 // queued port.
1535 if (blk->isDirty() || writebackClean) {
1536 PacketPtr wbPkt = writebackBlk(blk);
1537 allocateWriteBuffer(wbPkt, forward_time);
1538 // Set BLOCK_CACHED flag if cached above.
1539 if (isCachedAbove(wbPkt))
1540 wbPkt->setBlockCached();
1541 } else {
1542 PacketPtr wcPkt = cleanEvictBlk(blk);
1543 // Check to see if block is cached above. If not allocate
1544 // write buffer
1545 if (isCachedAbove(wcPkt))
1546 delete wcPkt;
1547 else
1548 allocateWriteBuffer(wcPkt, forward_time);
1549 }
1550 blk->invalidate();
1551 }
1552
1553 DPRINTF(CacheVerbose, "Leaving %s with %s for addr %#llx\n", __func__,
1554 pkt->cmdString(), pkt->getAddr());
1555 delete pkt;
1556}
1557
1558PacketPtr
1559Cache::writebackBlk(CacheBlk *blk)
1560{
1561 chatty_assert(!isReadOnly || writebackClean,
1562 "Writeback from read-only cache");
1563 assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
1564
1565 writebacks[Request::wbMasterId]++;
1566
1567 Request *req = new Request(tags->regenerateBlkAddr(blk->tag, blk->set),
1568 blkSize, 0, Request::wbMasterId);
1569 if (blk->isSecure())
1570 req->setFlags(Request::SECURE);
1571
1572 req->taskId(blk->task_id);
1573 blk->task_id= ContextSwitchTaskId::Unknown;
1574 blk->tickInserted = curTick();
1575
1576 PacketPtr pkt =
1577 new Packet(req, blk->isDirty() ?
1578 MemCmd::WritebackDirty : MemCmd::WritebackClean);
1579
1580 DPRINTF(Cache, "Create Writeback %#llx writable: %d, dirty: %d\n",
1581 pkt->getAddr(), blk->isWritable(), blk->isDirty());
1582
1583 if (blk->isWritable()) {
1584 // not asserting shared means we pass the block in modified
1585 // state, mark our own block non-writeable
1586 blk->status &= ~BlkWritable;
1587 } else {
1588 // we are in the Owned state, tell the receiver
1589 pkt->setHasSharers();
1590 }
1591
1592 // make sure the block is not marked dirty
1593 blk->status &= ~BlkDirty;
1594
1595 pkt->allocate();
1596 std::memcpy(pkt->getPtr<uint8_t>(), blk->data, blkSize);
1597
1598 return pkt;
1599}
1600
1601PacketPtr
1602Cache::cleanEvictBlk(CacheBlk *blk)
1603{
1604 assert(!writebackClean);
1605 assert(blk && blk->isValid() && !blk->isDirty());
1606 // Creating a zero sized write, a message to the snoop filter
1607 Request *req =
1608 new Request(tags->regenerateBlkAddr(blk->tag, blk->set), blkSize, 0,
1609 Request::wbMasterId);
1610 if (blk->isSecure())
1611 req->setFlags(Request::SECURE);
1612
1613 req->taskId(blk->task_id);
1614 blk->task_id = ContextSwitchTaskId::Unknown;
1615 blk->tickInserted = curTick();
1616
1617 PacketPtr pkt = new Packet(req, MemCmd::CleanEvict);
1618 pkt->allocate();
1619 DPRINTF(Cache, "%s%s %x Create CleanEvict\n", pkt->cmdString(),
1620 pkt->req->isInstFetch() ? " (ifetch)" : "",
1621 pkt->getAddr());
1622
1623 return pkt;
1624}
1625
1626void
1627Cache::memWriteback()
1628{
1629 CacheBlkVisitorWrapper visitor(*this, &Cache::writebackVisitor);
1630 tags->forEachBlk(visitor);
1631}
1632
1633void
1634Cache::memInvalidate()
1635{
1636 CacheBlkVisitorWrapper visitor(*this, &Cache::invalidateVisitor);
1637 tags->forEachBlk(visitor);
1638}
1639
1640bool
1641Cache::isDirty() const
1642{
1643 CacheBlkIsDirtyVisitor visitor;
1644 tags->forEachBlk(visitor);
1645
1646 return visitor.isDirty();
1647}
1648
1649bool
1650Cache::writebackVisitor(CacheBlk &blk)
1651{
1652 if (blk.isDirty()) {
1653 assert(blk.isValid());
1654
1655 Request request(tags->regenerateBlkAddr(blk.tag, blk.set),
1656 blkSize, 0, Request::funcMasterId);
1657 request.taskId(blk.task_id);
1658
1659 Packet packet(&request, MemCmd::WriteReq);
1660 packet.dataStatic(blk.data);
1661
1662 memSidePort->sendFunctional(&packet);
1663
1664 blk.status &= ~BlkDirty;
1665 }
1666
1667 return true;
1668}
1669
1670bool
1671Cache::invalidateVisitor(CacheBlk &blk)
1672{
1673
1674 if (blk.isDirty())
1675 warn_once("Invalidating dirty cache lines. Expect things to break.\n");
1676
1677 if (blk.isValid()) {
1678 assert(!blk.isDirty());
1679 tags->invalidate(&blk);
1680 blk.invalidate();
1681 }
1682
1683 return true;
1684}
1685
1686CacheBlk*
1687Cache::allocateBlock(Addr addr, bool is_secure, PacketList &writebacks)
1688{
1689 CacheBlk *blk = tags->findVictim(addr);
1690
1691 // It is valid to return NULL if there is no victim
1692 if (!blk)
1693 return nullptr;
1694
1695 if (blk->isValid()) {
1696 Addr repl_addr = tags->regenerateBlkAddr(blk->tag, blk->set);
1697 MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
1698 if (repl_mshr) {
1699 // must be an outstanding upgrade request
1700 // on a block we're about to replace...
1701 assert(!blk->isWritable() || blk->isDirty());
1702 assert(repl_mshr->needsWritable());
1703 // too hard to replace block with transient state
1704 // allocation failed, block not inserted
1705 return NULL;
1706 } else {
1707 DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx (%s): %s\n",
1708 repl_addr, blk->isSecure() ? "s" : "ns",
1709 addr, is_secure ? "s" : "ns",
1710 blk->isDirty() ? "writeback" : "clean");
1711
1712 // Will send up Writeback/CleanEvict snoops via isCachedAbove
1713 // when pushing this writeback list into the write buffer.
1714 if (blk->isDirty() || writebackClean) {
1715 // Save writeback packet for handling by caller
1716 writebacks.push_back(writebackBlk(blk));
1717 } else {
1718 writebacks.push_back(cleanEvictBlk(blk));
1719 }
1720 }
1721 }
1722
1723 return blk;
1724}
1725
1726void
1727Cache::invalidateBlock(CacheBlk *blk)
1728{
1729 if (blk != tempBlock)
1730 tags->invalidate(blk);
1731 blk->invalidate();
1732}
1733
1734// Note that the reason we return a list of writebacks rather than
1735// inserting them directly in the write buffer is that this function
1736// is called by both atomic and timing-mode accesses, and in atomic
1737// mode we don't mess with the write buffer (we just perform the
1738// writebacks atomically once the original request is complete).
1739CacheBlk*
1740Cache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
1741 bool allocate)
1742{
1743 assert(pkt->isResponse() || pkt->cmd == MemCmd::WriteLineReq);
1744 Addr addr = pkt->getAddr();
1745 bool is_secure = pkt->isSecure();
1746#if TRACING_ON
1747 CacheBlk::State old_state = blk ? blk->status : 0;
1748#endif
1749
1718 // When handling a fill, discard any CleanEvicts for the
1719 // same address in write buffer.
1720 Addr M5_VAR_USED blk_addr = blockAlign(pkt->getAddr());
1721 std::vector<MSHR *> M5_VAR_USED wbs;
1722 assert (!writeBuffer.findMatches(blk_addr, is_secure, wbs));
1750 // When handling a fill, we should have no writes to this line.
1751 assert(addr == blockAlign(addr));
1752 assert(!writeBuffer.findMatch(addr, is_secure));
1723
1724 if (blk == NULL) {
1725 // better have read new data...
1726 assert(pkt->hasData());
1727
1728 // only read responses and write-line requests have data;
1729 // note that we don't write the data here for write-line - that
1730 // happens in the subsequent satisfyCpuSideRequest.
1731 assert(pkt->isRead() || pkt->cmd == MemCmd::WriteLineReq);
1732
1733 // need to do a replacement if allocating, otherwise we stick
1734 // with the temporary storage
1735 blk = allocate ? allocateBlock(addr, is_secure, writebacks) : NULL;
1736
1737 if (blk == NULL) {
1738 // No replaceable block or a mostly exclusive
1739 // cache... just use temporary storage to complete the
1740 // current request and then get rid of it
1741 assert(!tempBlock->isValid());
1742 blk = tempBlock;
1743 tempBlock->set = tags->extractSet(addr);
1744 tempBlock->tag = tags->extractTag(addr);
1745 // @todo: set security state as well...
1746 DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
1747 is_secure ? "s" : "ns");
1748 } else {
1749 tags->insertBlock(pkt, blk);
1750 }
1751
1752 // we should never be overwriting a valid block
1753 assert(!blk->isValid());
1754 } else {
1755 // existing block... probably an upgrade
1756 assert(blk->tag == tags->extractTag(addr));
1757 // either we're getting new data or the block should already be valid
1758 assert(pkt->hasData() || blk->isValid());
1759 // don't clear block status... if block is already dirty we
1760 // don't want to lose that
1761 }
1762
1763 if (is_secure)
1764 blk->status |= BlkSecure;
1765 blk->status |= BlkValid | BlkReadable;
1766
1767 // sanity check for whole-line writes, which should always be
1768 // marked as writable as part of the fill, and then later marked
1769 // dirty as part of satisfyCpuSideRequest
1770 if (pkt->cmd == MemCmd::WriteLineReq) {
1771 assert(!pkt->hasSharers());
1772 // at the moment other caches do not respond to the
1773 // invalidation requests corresponding to a whole-line write
1774 assert(!pkt->cacheResponding());
1775 }
1776
1777 // here we deal with setting the appropriate state of the line,
1778 // and we start by looking at the hasSharers flag, and ignore the
1779 // cacheResponding flag (normally signalling dirty data) if the
1780 // packet has sharers, thus the line is never allocated as Owned
1781 // (dirty but not writable), and always ends up being either
1782 // Shared, Exclusive or Modified, see Packet::setCacheResponding
1783 // for more details
1784 if (!pkt->hasSharers()) {
1785 // we could get a writable line from memory (rather than a
1786 // cache) even in a read-only cache, note that we set this bit
1787 // even for a read-only cache, possibly revisit this decision
1788 blk->status |= BlkWritable;
1789
1790 // check if we got this via cache-to-cache transfer (i.e., from a
1791 // cache that had the block in Modified or Owned state)
1792 if (pkt->cacheResponding()) {
1793 // we got the block in Modified state, and invalidated the
1794 // owners copy
1795 blk->status |= BlkDirty;
1796
1797 chatty_assert(!isReadOnly, "Should never see dirty snoop response "
1798 "in read-only cache %s\n", name());
1799 }
1800 }
1801
1802 DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
1803 addr, is_secure ? "s" : "ns", old_state, blk->print());
1804
1805 // if we got new data, copy it in (checking for a read response
1806 // and a response that has data is the same in the end)
1807 if (pkt->isRead()) {
1808 // sanity checks
1809 assert(pkt->hasData());
1810 assert(pkt->getSize() == blkSize);
1811
1812 std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
1813 }
1814 // We pay for fillLatency here.
1815 blk->whenReady = clockEdge() + fillLatency * clockPeriod() +
1816 pkt->payloadDelay;
1817
1818 return blk;
1819}
1820
1821
1822/////////////////////////////////////////////////////
1823//
1824// Snoop path: requests coming in from the memory side
1825//
1826/////////////////////////////////////////////////////
1827
1828void
1829Cache::doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
1830 bool already_copied, bool pending_inval)
1831{
1832 // sanity check
1833 assert(req_pkt->isRequest());
1834 assert(req_pkt->needsResponse());
1835
1836 DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
1837 req_pkt->cmdString(), req_pkt->getAddr(), req_pkt->getSize());
1838 // timing-mode snoop responses require a new packet, unless we
1839 // already made a copy...
1840 PacketPtr pkt = req_pkt;
1841 if (!already_copied)
1842 // do not clear flags, and allocate space for data if the
1843 // packet needs it (the only packets that carry data are read
1844 // responses)
1845 pkt = new Packet(req_pkt, false, req_pkt->isRead());
1846
1847 assert(req_pkt->req->isUncacheable() || req_pkt->isInvalidate() ||
1848 pkt->hasSharers());
1849 pkt->makeTimingResponse();
1850 if (pkt->isRead()) {
1851 pkt->setDataFromBlock(blk_data, blkSize);
1852 }
1853 if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
1854 // Assume we defer a response to a read from a far-away cache
1855 // A, then later defer a ReadExcl from a cache B on the same
1856 // bus as us. We'll assert cacheResponding in both cases, but
1857 // in the latter case cacheResponding will keep the
1858 // invalidation from reaching cache A. This special response
1859 // tells cache A that it gets the block to satisfy its read,
1860 // but must immediately invalidate it.
1861 pkt->cmd = MemCmd::ReadRespWithInvalidate;
1862 }
1863 // Here we consider forward_time, paying for just forward latency and
1864 // also charging the delay provided by the xbar.
1865 // forward_time is used as send_time in next allocateWriteBuffer().
1866 Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
1867 // Here we reset the timing of the packet.
1868 pkt->headerDelay = pkt->payloadDelay = 0;
1869 DPRINTF(CacheVerbose,
1870 "%s created response: %s addr %#llx size %d tick: %lu\n",
1871 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize(),
1872 forward_time);
1873 memSidePort->schedTimingSnoopResp(pkt, forward_time, true);
1874}
1875
1876uint32_t
1877Cache::handleSnoop(PacketPtr pkt, CacheBlk *blk, bool is_timing,
1878 bool is_deferred, bool pending_inval)
1879{
1880 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
1881 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
1882 // deferred snoops can only happen in timing mode
1883 assert(!(is_deferred && !is_timing));
1884 // pending_inval only makes sense on deferred snoops
1885 assert(!(pending_inval && !is_deferred));
1886 assert(pkt->isRequest());
1887
1888 // the packet may get modified if we or a forwarded snooper
1889 // responds in atomic mode, so remember a few things about the
1890 // original packet up front
1891 bool invalidate = pkt->isInvalidate();
1892 bool M5_VAR_USED needs_writable = pkt->needsWritable();
1893
1894 // at the moment we could get an uncacheable write which does not
1895 // have the invalidate flag, and we need a suitable way of dealing
1896 // with this case
1897 panic_if(invalidate && pkt->req->isUncacheable(),
1898 "%s got an invalidating uncacheable snoop request %s to %#llx",
1899 name(), pkt->cmdString(), pkt->getAddr());
1900
1901 uint32_t snoop_delay = 0;
1902
1903 if (forwardSnoops) {
1904 // first propagate snoop upward to see if anyone above us wants to
1905 // handle it. save & restore packet src since it will get
1906 // rewritten to be relative to cpu-side bus (if any)
1907 bool alreadyResponded = pkt->cacheResponding();
1908 if (is_timing) {
1909 // copy the packet so that we can clear any flags before
1910 // forwarding it upwards, we also allocate data (passing
1911 // the pointer along in case of static data), in case
1912 // there is a snoop hit in upper levels
1913 Packet snoopPkt(pkt, true, true);
1914 snoopPkt.setExpressSnoop();
1915 // the snoop packet does not need to wait any additional
1916 // time
1917 snoopPkt.headerDelay = snoopPkt.payloadDelay = 0;
1918 cpuSidePort->sendTimingSnoopReq(&snoopPkt);
1919
1920 // add the header delay (including crossbar and snoop
1921 // delays) of the upward snoop to the snoop delay for this
1922 // cache
1923 snoop_delay += snoopPkt.headerDelay;
1924
1925 if (snoopPkt.cacheResponding()) {
1926 // cache-to-cache response from some upper cache
1927 assert(!alreadyResponded);
1928 pkt->setCacheResponding();
1929 }
1930 // upstream cache has the block, or has an outstanding
1931 // MSHR, pass the flag on
1932 if (snoopPkt.hasSharers()) {
1933 pkt->setHasSharers();
1934 }
1935 // If this request is a prefetch or clean evict and an upper level
1936 // signals block present, make sure to propagate the block
1937 // presence to the requester.
1938 if (snoopPkt.isBlockCached()) {
1939 pkt->setBlockCached();
1940 }
1941 } else {
1942 cpuSidePort->sendAtomicSnoop(pkt);
1943 if (!alreadyResponded && pkt->cacheResponding()) {
1944 // cache-to-cache response from some upper cache:
1945 // forward response to original requester
1946 assert(pkt->isResponse());
1947 }
1948 }
1949 }
1950
1951 if (!blk || !blk->isValid()) {
1952 DPRINTF(CacheVerbose, "%s snoop miss for %s addr %#llx size %d\n",
1953 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
1954 return snoop_delay;
1955 } else {
1956 DPRINTF(Cache, "%s snoop hit for %s addr %#llx size %d, "
1957 "old state is %s\n", __func__, pkt->cmdString(),
1958 pkt->getAddr(), pkt->getSize(), blk->print());
1959 }
1960
1961 chatty_assert(!(isReadOnly && blk->isDirty()),
1962 "Should never have a dirty block in a read-only cache %s\n",
1963 name());
1964
1965 // We may end up modifying both the block state and the packet (if
1966 // we respond in atomic mode), so just figure out what to do now
1967 // and then do it later. If we find dirty data while snooping for
1968 // an invalidate, we don't need to send a response. The
1969 // invalidation itself is taken care of below.
1970 bool respond = blk->isDirty() && pkt->needsResponse() &&
1971 pkt->cmd != MemCmd::InvalidateReq;
1972 bool have_writable = blk->isWritable();
1973
1974 // Invalidate any prefetch's from below that would strip write permissions
1975 // MemCmd::HardPFReq is only observed by upstream caches. After missing
1976 // above and in it's own cache, a new MemCmd::ReadReq is created that
1977 // downstream caches observe.
1978 if (pkt->mustCheckAbove()) {
1979 DPRINTF(Cache, "Found addr %#llx in upper level cache for snoop %s from"
1980 " lower cache\n", pkt->getAddr(), pkt->cmdString());
1981 pkt->setBlockCached();
1982 return snoop_delay;
1983 }
1984
1985 if (pkt->isRead() && !invalidate) {
1986 // reading without requiring the line in a writable state
1987 assert(!needs_writable);
1988 pkt->setHasSharers();
1989
1990 // if the requesting packet is uncacheable, retain the line in
1991 // the current state, otherwhise unset the writable flag,
1992 // which means we go from Modified to Owned (and will respond
1993 // below), remain in Owned (and will respond below), from
1994 // Exclusive to Shared, or remain in Shared
1995 if (!pkt->req->isUncacheable())
1996 blk->status &= ~BlkWritable;
1997 }
1998
1999 if (respond) {
2000 // prevent anyone else from responding, cache as well as
2001 // memory, and also prevent any memory from even seeing the
2002 // request
2003 pkt->setCacheResponding();
2004 if (have_writable) {
2005 // inform the cache hierarchy that this cache had the line
2006 // in the Modified state so that we avoid unnecessary
2007 // invalidations (see Packet::setResponderHadWritable)
2008 pkt->setResponderHadWritable();
2009
2010 // in the case of an uncacheable request there is no point
2011 // in setting the responderHadWritable flag, but since the
2012 // recipient does not care there is no harm in doing so
2013 } else {
2014 // if the packet has needsWritable set we invalidate our
2015 // copy below and all other copies will be invalidates
2016 // through express snoops, and if needsWritable is not set
2017 // we already called setHasSharers above
2018 }
2019
2020 // if we are returning a writable and dirty (Modified) line,
2021 // we should be invalidating the line
2022 panic_if(!invalidate && !pkt->hasSharers(),
2023 "%s is passing a Modified line through %s to %#llx, "
2024 "but keeping the block",
2025 name(), pkt->cmdString(), pkt->getAddr());
2026
2027 if (is_timing) {
2028 doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
2029 } else {
2030 pkt->makeAtomicResponse();
2031 // packets such as upgrades do not actually have any data
2032 // payload
2033 if (pkt->hasData())
2034 pkt->setDataFromBlock(blk->data, blkSize);
2035 }
2036 }
2037
2038 if (!respond && is_timing && is_deferred) {
2039 // if it's a deferred timing snoop to which we are not
2040 // responding, then we've made a copy of both the request and
2041 // the packet, delete them here
2042 assert(pkt->needsResponse());
2043 delete pkt->req;
2044 delete pkt;
2045 }
2046
2047 // Do this last in case it deallocates block data or something
2048 // like that
2049 if (invalidate) {
2050 invalidateBlock(blk);
2051 }
2052
2053 DPRINTF(Cache, "new state is %s\n", blk->print());
2054
2055 return snoop_delay;
2056}
2057
2058
2059void
2060Cache::recvTimingSnoopReq(PacketPtr pkt)
2061{
2062 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
2063 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
2064
2065 // Snoops shouldn't happen when bypassing caches
2066 assert(!system->bypassCaches());
2067
2068 // no need to snoop requests that are not in range
2069 if (!inRange(pkt->getAddr())) {
2070 return;
2071 }
2072
2073 bool is_secure = pkt->isSecure();
2074 CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
2075
2076 Addr blk_addr = blockAlign(pkt->getAddr());
2077 MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
2078
2079 // Update the latency cost of the snoop so that the crossbar can
2080 // account for it. Do not overwrite what other neighbouring caches
2081 // have already done, rather take the maximum. The update is
2082 // tentative, for cases where we return before an upward snoop
2083 // happens below.
2084 pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay,
2085 lookupLatency * clockPeriod());
2086
2087 // Inform request(Prefetch, CleanEvict or Writeback) from below of
2088 // MSHR hit, set setBlockCached.
2089 if (mshr && pkt->mustCheckAbove()) {
2090 DPRINTF(Cache, "Setting block cached for %s from"
2091 "lower cache on mshr hit %#x\n",
2092 pkt->cmdString(), pkt->getAddr());
2093 pkt->setBlockCached();
2094 return;
2095 }
2096
2097 // Let the MSHR itself track the snoop and decide whether we want
2098 // to go ahead and do the regular cache snoop
2099 if (mshr && mshr->handleSnoop(pkt, order++)) {
2100 DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %#llx (%s)."
2101 "mshrs: %s\n", blk_addr, is_secure ? "s" : "ns",
2102 mshr->print());
2103
2104 if (mshr->getNumTargets() > numTarget)
2105 warn("allocating bonus target for snoop"); //handle later
2106 return;
2107 }
2108
2109 //We also need to check the writeback buffers and handle those
1753
1754 if (blk == NULL) {
1755 // better have read new data...
1756 assert(pkt->hasData());
1757
1758 // only read responses and write-line requests have data;
1759 // note that we don't write the data here for write-line - that
1760 // happens in the subsequent satisfyCpuSideRequest.
1761 assert(pkt->isRead() || pkt->cmd == MemCmd::WriteLineReq);
1762
1763 // need to do a replacement if allocating, otherwise we stick
1764 // with the temporary storage
1765 blk = allocate ? allocateBlock(addr, is_secure, writebacks) : NULL;
1766
1767 if (blk == NULL) {
1768 // No replaceable block or a mostly exclusive
1769 // cache... just use temporary storage to complete the
1770 // current request and then get rid of it
1771 assert(!tempBlock->isValid());
1772 blk = tempBlock;
1773 tempBlock->set = tags->extractSet(addr);
1774 tempBlock->tag = tags->extractTag(addr);
1775 // @todo: set security state as well...
1776 DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
1777 is_secure ? "s" : "ns");
1778 } else {
1779 tags->insertBlock(pkt, blk);
1780 }
1781
1782 // we should never be overwriting a valid block
1783 assert(!blk->isValid());
1784 } else {
1785 // existing block... probably an upgrade
1786 assert(blk->tag == tags->extractTag(addr));
1787 // either we're getting new data or the block should already be valid
1788 assert(pkt->hasData() || blk->isValid());
1789 // don't clear block status... if block is already dirty we
1790 // don't want to lose that
1791 }
1792
1793 if (is_secure)
1794 blk->status |= BlkSecure;
1795 blk->status |= BlkValid | BlkReadable;
1796
1797 // sanity check for whole-line writes, which should always be
1798 // marked as writable as part of the fill, and then later marked
1799 // dirty as part of satisfyCpuSideRequest
1800 if (pkt->cmd == MemCmd::WriteLineReq) {
1801 assert(!pkt->hasSharers());
1802 // at the moment other caches do not respond to the
1803 // invalidation requests corresponding to a whole-line write
1804 assert(!pkt->cacheResponding());
1805 }
1806
1807 // here we deal with setting the appropriate state of the line,
1808 // and we start by looking at the hasSharers flag, and ignore the
1809 // cacheResponding flag (normally signalling dirty data) if the
1810 // packet has sharers, thus the line is never allocated as Owned
1811 // (dirty but not writable), and always ends up being either
1812 // Shared, Exclusive or Modified, see Packet::setCacheResponding
1813 // for more details
1814 if (!pkt->hasSharers()) {
1815 // we could get a writable line from memory (rather than a
1816 // cache) even in a read-only cache, note that we set this bit
1817 // even for a read-only cache, possibly revisit this decision
1818 blk->status |= BlkWritable;
1819
1820 // check if we got this via cache-to-cache transfer (i.e., from a
1821 // cache that had the block in Modified or Owned state)
1822 if (pkt->cacheResponding()) {
1823 // we got the block in Modified state, and invalidated the
1824 // owners copy
1825 blk->status |= BlkDirty;
1826
1827 chatty_assert(!isReadOnly, "Should never see dirty snoop response "
1828 "in read-only cache %s\n", name());
1829 }
1830 }
1831
1832 DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
1833 addr, is_secure ? "s" : "ns", old_state, blk->print());
1834
1835 // if we got new data, copy it in (checking for a read response
1836 // and a response that has data is the same in the end)
1837 if (pkt->isRead()) {
1838 // sanity checks
1839 assert(pkt->hasData());
1840 assert(pkt->getSize() == blkSize);
1841
1842 std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
1843 }
1844 // We pay for fillLatency here.
1845 blk->whenReady = clockEdge() + fillLatency * clockPeriod() +
1846 pkt->payloadDelay;
1847
1848 return blk;
1849}
1850
1851
1852/////////////////////////////////////////////////////
1853//
1854// Snoop path: requests coming in from the memory side
1855//
1856/////////////////////////////////////////////////////
1857
1858void
1859Cache::doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
1860 bool already_copied, bool pending_inval)
1861{
1862 // sanity check
1863 assert(req_pkt->isRequest());
1864 assert(req_pkt->needsResponse());
1865
1866 DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
1867 req_pkt->cmdString(), req_pkt->getAddr(), req_pkt->getSize());
1868 // timing-mode snoop responses require a new packet, unless we
1869 // already made a copy...
1870 PacketPtr pkt = req_pkt;
1871 if (!already_copied)
1872 // do not clear flags, and allocate space for data if the
1873 // packet needs it (the only packets that carry data are read
1874 // responses)
1875 pkt = new Packet(req_pkt, false, req_pkt->isRead());
1876
1877 assert(req_pkt->req->isUncacheable() || req_pkt->isInvalidate() ||
1878 pkt->hasSharers());
1879 pkt->makeTimingResponse();
1880 if (pkt->isRead()) {
1881 pkt->setDataFromBlock(blk_data, blkSize);
1882 }
1883 if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
1884 // Assume we defer a response to a read from a far-away cache
1885 // A, then later defer a ReadExcl from a cache B on the same
1886 // bus as us. We'll assert cacheResponding in both cases, but
1887 // in the latter case cacheResponding will keep the
1888 // invalidation from reaching cache A. This special response
1889 // tells cache A that it gets the block to satisfy its read,
1890 // but must immediately invalidate it.
1891 pkt->cmd = MemCmd::ReadRespWithInvalidate;
1892 }
1893 // Here we consider forward_time, paying for just forward latency and
1894 // also charging the delay provided by the xbar.
1895 // forward_time is used as send_time in next allocateWriteBuffer().
1896 Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
1897 // Here we reset the timing of the packet.
1898 pkt->headerDelay = pkt->payloadDelay = 0;
1899 DPRINTF(CacheVerbose,
1900 "%s created response: %s addr %#llx size %d tick: %lu\n",
1901 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize(),
1902 forward_time);
1903 memSidePort->schedTimingSnoopResp(pkt, forward_time, true);
1904}
1905
1906uint32_t
1907Cache::handleSnoop(PacketPtr pkt, CacheBlk *blk, bool is_timing,
1908 bool is_deferred, bool pending_inval)
1909{
1910 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
1911 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
1912 // deferred snoops can only happen in timing mode
1913 assert(!(is_deferred && !is_timing));
1914 // pending_inval only makes sense on deferred snoops
1915 assert(!(pending_inval && !is_deferred));
1916 assert(pkt->isRequest());
1917
1918 // the packet may get modified if we or a forwarded snooper
1919 // responds in atomic mode, so remember a few things about the
1920 // original packet up front
1921 bool invalidate = pkt->isInvalidate();
1922 bool M5_VAR_USED needs_writable = pkt->needsWritable();
1923
1924 // at the moment we could get an uncacheable write which does not
1925 // have the invalidate flag, and we need a suitable way of dealing
1926 // with this case
1927 panic_if(invalidate && pkt->req->isUncacheable(),
1928 "%s got an invalidating uncacheable snoop request %s to %#llx",
1929 name(), pkt->cmdString(), pkt->getAddr());
1930
1931 uint32_t snoop_delay = 0;
1932
1933 if (forwardSnoops) {
1934 // first propagate snoop upward to see if anyone above us wants to
1935 // handle it. save & restore packet src since it will get
1936 // rewritten to be relative to cpu-side bus (if any)
1937 bool alreadyResponded = pkt->cacheResponding();
1938 if (is_timing) {
1939 // copy the packet so that we can clear any flags before
1940 // forwarding it upwards, we also allocate data (passing
1941 // the pointer along in case of static data), in case
1942 // there is a snoop hit in upper levels
1943 Packet snoopPkt(pkt, true, true);
1944 snoopPkt.setExpressSnoop();
1945 // the snoop packet does not need to wait any additional
1946 // time
1947 snoopPkt.headerDelay = snoopPkt.payloadDelay = 0;
1948 cpuSidePort->sendTimingSnoopReq(&snoopPkt);
1949
1950 // add the header delay (including crossbar and snoop
1951 // delays) of the upward snoop to the snoop delay for this
1952 // cache
1953 snoop_delay += snoopPkt.headerDelay;
1954
1955 if (snoopPkt.cacheResponding()) {
1956 // cache-to-cache response from some upper cache
1957 assert(!alreadyResponded);
1958 pkt->setCacheResponding();
1959 }
1960 // upstream cache has the block, or has an outstanding
1961 // MSHR, pass the flag on
1962 if (snoopPkt.hasSharers()) {
1963 pkt->setHasSharers();
1964 }
1965 // If this request is a prefetch or clean evict and an upper level
1966 // signals block present, make sure to propagate the block
1967 // presence to the requester.
1968 if (snoopPkt.isBlockCached()) {
1969 pkt->setBlockCached();
1970 }
1971 } else {
1972 cpuSidePort->sendAtomicSnoop(pkt);
1973 if (!alreadyResponded && pkt->cacheResponding()) {
1974 // cache-to-cache response from some upper cache:
1975 // forward response to original requester
1976 assert(pkt->isResponse());
1977 }
1978 }
1979 }
1980
1981 if (!blk || !blk->isValid()) {
1982 DPRINTF(CacheVerbose, "%s snoop miss for %s addr %#llx size %d\n",
1983 __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
1984 return snoop_delay;
1985 } else {
1986 DPRINTF(Cache, "%s snoop hit for %s addr %#llx size %d, "
1987 "old state is %s\n", __func__, pkt->cmdString(),
1988 pkt->getAddr(), pkt->getSize(), blk->print());
1989 }
1990
1991 chatty_assert(!(isReadOnly && blk->isDirty()),
1992 "Should never have a dirty block in a read-only cache %s\n",
1993 name());
1994
1995 // We may end up modifying both the block state and the packet (if
1996 // we respond in atomic mode), so just figure out what to do now
1997 // and then do it later. If we find dirty data while snooping for
1998 // an invalidate, we don't need to send a response. The
1999 // invalidation itself is taken care of below.
2000 bool respond = blk->isDirty() && pkt->needsResponse() &&
2001 pkt->cmd != MemCmd::InvalidateReq;
2002 bool have_writable = blk->isWritable();
2003
2004 // Invalidate any prefetch's from below that would strip write permissions
2005 // MemCmd::HardPFReq is only observed by upstream caches. After missing
2006 // above and in it's own cache, a new MemCmd::ReadReq is created that
2007 // downstream caches observe.
2008 if (pkt->mustCheckAbove()) {
2009 DPRINTF(Cache, "Found addr %#llx in upper level cache for snoop %s from"
2010 " lower cache\n", pkt->getAddr(), pkt->cmdString());
2011 pkt->setBlockCached();
2012 return snoop_delay;
2013 }
2014
2015 if (pkt->isRead() && !invalidate) {
2016 // reading without requiring the line in a writable state
2017 assert(!needs_writable);
2018 pkt->setHasSharers();
2019
2020 // if the requesting packet is uncacheable, retain the line in
2021 // the current state, otherwhise unset the writable flag,
2022 // which means we go from Modified to Owned (and will respond
2023 // below), remain in Owned (and will respond below), from
2024 // Exclusive to Shared, or remain in Shared
2025 if (!pkt->req->isUncacheable())
2026 blk->status &= ~BlkWritable;
2027 }
2028
2029 if (respond) {
2030 // prevent anyone else from responding, cache as well as
2031 // memory, and also prevent any memory from even seeing the
2032 // request
2033 pkt->setCacheResponding();
2034 if (have_writable) {
2035 // inform the cache hierarchy that this cache had the line
2036 // in the Modified state so that we avoid unnecessary
2037 // invalidations (see Packet::setResponderHadWritable)
2038 pkt->setResponderHadWritable();
2039
2040 // in the case of an uncacheable request there is no point
2041 // in setting the responderHadWritable flag, but since the
2042 // recipient does not care there is no harm in doing so
2043 } else {
2044 // if the packet has needsWritable set we invalidate our
2045 // copy below and all other copies will be invalidates
2046 // through express snoops, and if needsWritable is not set
2047 // we already called setHasSharers above
2048 }
2049
2050 // if we are returning a writable and dirty (Modified) line,
2051 // we should be invalidating the line
2052 panic_if(!invalidate && !pkt->hasSharers(),
2053 "%s is passing a Modified line through %s to %#llx, "
2054 "but keeping the block",
2055 name(), pkt->cmdString(), pkt->getAddr());
2056
2057 if (is_timing) {
2058 doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
2059 } else {
2060 pkt->makeAtomicResponse();
2061 // packets such as upgrades do not actually have any data
2062 // payload
2063 if (pkt->hasData())
2064 pkt->setDataFromBlock(blk->data, blkSize);
2065 }
2066 }
2067
2068 if (!respond && is_timing && is_deferred) {
2069 // if it's a deferred timing snoop to which we are not
2070 // responding, then we've made a copy of both the request and
2071 // the packet, delete them here
2072 assert(pkt->needsResponse());
2073 delete pkt->req;
2074 delete pkt;
2075 }
2076
2077 // Do this last in case it deallocates block data or something
2078 // like that
2079 if (invalidate) {
2080 invalidateBlock(blk);
2081 }
2082
2083 DPRINTF(Cache, "new state is %s\n", blk->print());
2084
2085 return snoop_delay;
2086}
2087
2088
2089void
2090Cache::recvTimingSnoopReq(PacketPtr pkt)
2091{
2092 DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
2093 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
2094
2095 // Snoops shouldn't happen when bypassing caches
2096 assert(!system->bypassCaches());
2097
2098 // no need to snoop requests that are not in range
2099 if (!inRange(pkt->getAddr())) {
2100 return;
2101 }
2102
2103 bool is_secure = pkt->isSecure();
2104 CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
2105
2106 Addr blk_addr = blockAlign(pkt->getAddr());
2107 MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
2108
2109 // Update the latency cost of the snoop so that the crossbar can
2110 // account for it. Do not overwrite what other neighbouring caches
2111 // have already done, rather take the maximum. The update is
2112 // tentative, for cases where we return before an upward snoop
2113 // happens below.
2114 pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay,
2115 lookupLatency * clockPeriod());
2116
2117 // Inform request(Prefetch, CleanEvict or Writeback) from below of
2118 // MSHR hit, set setBlockCached.
2119 if (mshr && pkt->mustCheckAbove()) {
2120 DPRINTF(Cache, "Setting block cached for %s from"
2121 "lower cache on mshr hit %#x\n",
2122 pkt->cmdString(), pkt->getAddr());
2123 pkt->setBlockCached();
2124 return;
2125 }
2126
2127 // Let the MSHR itself track the snoop and decide whether we want
2128 // to go ahead and do the regular cache snoop
2129 if (mshr && mshr->handleSnoop(pkt, order++)) {
2130 DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %#llx (%s)."
2131 "mshrs: %s\n", blk_addr, is_secure ? "s" : "ns",
2132 mshr->print());
2133
2134 if (mshr->getNumTargets() > numTarget)
2135 warn("allocating bonus target for snoop"); //handle later
2136 return;
2137 }
2138
2139 //We also need to check the writeback buffers and handle those
2110 std::vector<MSHR *> writebacks;
2111 if (writeBuffer.findMatches(blk_addr, is_secure, writebacks)) {
2140 WriteQueueEntry *wb_entry = writeBuffer.findMatch(blk_addr, is_secure);
2141 if (wb_entry) {
2112 DPRINTF(Cache, "Snoop hit in writeback to addr %#llx (%s)\n",
2113 pkt->getAddr(), is_secure ? "s" : "ns");
2142 DPRINTF(Cache, "Snoop hit in writeback to addr %#llx (%s)\n",
2143 pkt->getAddr(), is_secure ? "s" : "ns");
2114
2115 // Look through writebacks for any cachable writes.
2116 // We should only ever find a single match
2117 assert(writebacks.size() == 1);
2118 MSHR *wb_entry = writebacks[0];
2119 // Expect to see only Writebacks and/or CleanEvicts here, both of
2120 // which should not be generated for uncacheable data.
2121 assert(!wb_entry->isUncacheable());
2122 // There should only be a single request responsible for generating
2123 // Writebacks/CleanEvicts.
2124 assert(wb_entry->getNumTargets() == 1);
2125 PacketPtr wb_pkt = wb_entry->getTarget()->pkt;
2126 assert(wb_pkt->isEviction());
2127
2128 if (pkt->isEviction()) {
2129 // if the block is found in the write queue, set the BLOCK_CACHED
2130 // flag for Writeback/CleanEvict snoop. On return the snoop will
2131 // propagate the BLOCK_CACHED flag in Writeback packets and prevent
2132 // any CleanEvicts from travelling down the memory hierarchy.
2133 pkt->setBlockCached();
2134 DPRINTF(Cache, "Squashing %s from lower cache on writequeue hit"
2135 " %#x\n", pkt->cmdString(), pkt->getAddr());
2136 return;
2137 }
2138
2139 // conceptually writebacks are no different to other blocks in
2140 // this cache, so the behaviour is modelled after handleSnoop,
2141 // the difference being that instead of querying the block
2142 // state to determine if it is dirty and writable, we use the
2143 // command and fields of the writeback packet
2144 bool respond = wb_pkt->cmd == MemCmd::WritebackDirty &&
2145 pkt->needsResponse() && pkt->cmd != MemCmd::InvalidateReq;
2146 bool have_writable = !wb_pkt->hasSharers();
2147 bool invalidate = pkt->isInvalidate();
2148
2149 if (!pkt->req->isUncacheable() && pkt->isRead() && !invalidate) {
2150 assert(!pkt->needsWritable());
2151 pkt->setHasSharers();
2152 wb_pkt->setHasSharers();
2153 }
2154
2155 if (respond) {
2156 pkt->setCacheResponding();
2157
2158 if (have_writable) {
2159 pkt->setResponderHadWritable();
2160 }
2161
2162 doTimingSupplyResponse(pkt, wb_pkt->getConstPtr<uint8_t>(),
2163 false, false);
2164 }
2165
2166 if (invalidate) {
2167 // Invalidation trumps our writeback... discard here
2168 // Note: markInService will remove entry from writeback buffer.
2144 // Expect to see only Writebacks and/or CleanEvicts here, both of
2145 // which should not be generated for uncacheable data.
2146 assert(!wb_entry->isUncacheable());
2147 // There should only be a single request responsible for generating
2148 // Writebacks/CleanEvicts.
2149 assert(wb_entry->getNumTargets() == 1);
2150 PacketPtr wb_pkt = wb_entry->getTarget()->pkt;
2151 assert(wb_pkt->isEviction());
2152
2153 if (pkt->isEviction()) {
2154 // if the block is found in the write queue, set the BLOCK_CACHED
2155 // flag for Writeback/CleanEvict snoop. On return the snoop will
2156 // propagate the BLOCK_CACHED flag in Writeback packets and prevent
2157 // any CleanEvicts from travelling down the memory hierarchy.
2158 pkt->setBlockCached();
2159 DPRINTF(Cache, "Squashing %s from lower cache on writequeue hit"
2160 " %#x\n", pkt->cmdString(), pkt->getAddr());
2161 return;
2162 }
2163
2164 // conceptually writebacks are no different to other blocks in
2165 // this cache, so the behaviour is modelled after handleSnoop,
2166 // the difference being that instead of querying the block
2167 // state to determine if it is dirty and writable, we use the
2168 // command and fields of the writeback packet
2169 bool respond = wb_pkt->cmd == MemCmd::WritebackDirty &&
2170 pkt->needsResponse() && pkt->cmd != MemCmd::InvalidateReq;
2171 bool have_writable = !wb_pkt->hasSharers();
2172 bool invalidate = pkt->isInvalidate();
2173
2174 if (!pkt->req->isUncacheable() && pkt->isRead() && !invalidate) {
2175 assert(!pkt->needsWritable());
2176 pkt->setHasSharers();
2177 wb_pkt->setHasSharers();
2178 }
2179
2180 if (respond) {
2181 pkt->setCacheResponding();
2182
2183 if (have_writable) {
2184 pkt->setResponderHadWritable();
2185 }
2186
2187 doTimingSupplyResponse(pkt, wb_pkt->getConstPtr<uint8_t>(),
2188 false, false);
2189 }
2190
2191 if (invalidate) {
2192 // Invalidation trumps our writeback... discard here
2193 // Note: markInService will remove entry from writeback buffer.
2169 markInService(wb_entry, false);
2194 markInService(wb_entry);
2170 delete wb_pkt;
2171 }
2172 }
2173
2174 // If this was a shared writeback, there may still be
2175 // other shared copies above that require invalidation.
2176 // We could be more selective and return here if the
2177 // request is non-exclusive or if the writeback is
2178 // exclusive.
2179 uint32_t snoop_delay = handleSnoop(pkt, blk, true, false, false);
2180
2181 // Override what we did when we first saw the snoop, as we now
2182 // also have the cost of the upwards snoops to account for
2183 pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay, snoop_delay +
2184 lookupLatency * clockPeriod());
2185}
2186
2187bool
2188Cache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
2189{
2190 // Express snoop responses from master to slave, e.g., from L1 to L2
2191 cache->recvTimingSnoopResp(pkt);
2192 return true;
2193}
2194
2195Tick
2196Cache::recvAtomicSnoop(PacketPtr pkt)
2197{
2198 // Snoops shouldn't happen when bypassing caches
2199 assert(!system->bypassCaches());
2200
2201 // no need to snoop requests that are not in range.
2202 if (!inRange(pkt->getAddr())) {
2203 return 0;
2204 }
2205
2206 CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
2207 uint32_t snoop_delay = handleSnoop(pkt, blk, false, false, false);
2208 return snoop_delay + lookupLatency * clockPeriod();
2209}
2210
2211
2195 delete wb_pkt;
2196 }
2197 }
2198
2199 // If this was a shared writeback, there may still be
2200 // other shared copies above that require invalidation.
2201 // We could be more selective and return here if the
2202 // request is non-exclusive or if the writeback is
2203 // exclusive.
2204 uint32_t snoop_delay = handleSnoop(pkt, blk, true, false, false);
2205
2206 // Override what we did when we first saw the snoop, as we now
2207 // also have the cost of the upwards snoops to account for
2208 pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay, snoop_delay +
2209 lookupLatency * clockPeriod());
2210}
2211
2212bool
2213Cache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
2214{
2215 // Express snoop responses from master to slave, e.g., from L1 to L2
2216 cache->recvTimingSnoopResp(pkt);
2217 return true;
2218}
2219
2220Tick
2221Cache::recvAtomicSnoop(PacketPtr pkt)
2222{
2223 // Snoops shouldn't happen when bypassing caches
2224 assert(!system->bypassCaches());
2225
2226 // no need to snoop requests that are not in range.
2227 if (!inRange(pkt->getAddr())) {
2228 return 0;
2229 }
2230
2231 CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
2232 uint32_t snoop_delay = handleSnoop(pkt, blk, false, false, false);
2233 return snoop_delay + lookupLatency * clockPeriod();
2234}
2235
2236
2212MSHR *
2213Cache::getNextMSHR()
2237QueueEntry*
2238Cache::getNextQueueEntry()
2214{
2215 // Check both MSHR queue and write buffer for potential requests,
2216 // note that null does not mean there is no request, it could
2217 // simply be that it is not ready
2239{
2240 // Check both MSHR queue and write buffer for potential requests,
2241 // note that null does not mean there is no request, it could
2242 // simply be that it is not ready
2218 MSHR *miss_mshr = mshrQueue.getNextMSHR();
2219 MSHR *write_mshr = writeBuffer.getNextMSHR();
2243 MSHR *miss_mshr = mshrQueue.getNext();
2244 WriteQueueEntry *wq_entry = writeBuffer.getNext();
2220
2221 // If we got a write buffer request ready, first priority is a
2245
2246 // If we got a write buffer request ready, first priority is a
2222 // full write buffer, otherwhise we favour the miss requests
2223 if (write_mshr &&
2224 ((writeBuffer.isFull() && writeBuffer.inServiceEntries == 0) ||
2247 // full write buffer (but only if we have no uncacheable write
2248 // responses outstanding, possibly revisit this last part),
2249 // otherwhise we favour the miss requests
2250 if (wq_entry &&
2251 ((writeBuffer.isFull() && writeBuffer.numInService() == 0) ||
2225 !miss_mshr)) {
2226 // need to search MSHR queue for conflicting earlier miss.
2227 MSHR *conflict_mshr =
2252 !miss_mshr)) {
2253 // need to search MSHR queue for conflicting earlier miss.
2254 MSHR *conflict_mshr =
2228 mshrQueue.findPending(write_mshr->blkAddr,
2229 write_mshr->isSecure);
2255 mshrQueue.findPending(wq_entry->blkAddr,
2256 wq_entry->isSecure);
2230
2257
2231 if (conflict_mshr && conflict_mshr->order < write_mshr->order) {
2258 if (conflict_mshr && conflict_mshr->order < wq_entry->order) {
2232 // Service misses in order until conflict is cleared.
2233 return conflict_mshr;
2234
2235 // @todo Note that we ignore the ready time of the conflict here
2236 }
2237
2238 // No conflicts; issue write
2259 // Service misses in order until conflict is cleared.
2260 return conflict_mshr;
2261
2262 // @todo Note that we ignore the ready time of the conflict here
2263 }
2264
2265 // No conflicts; issue write
2239 return write_mshr;
2266 return wq_entry;
2240 } else if (miss_mshr) {
2241 // need to check for conflicting earlier writeback
2267 } else if (miss_mshr) {
2268 // need to check for conflicting earlier writeback
2242 MSHR *conflict_mshr =
2269 WriteQueueEntry *conflict_mshr =
2243 writeBuffer.findPending(miss_mshr->blkAddr,
2244 miss_mshr->isSecure);
2245 if (conflict_mshr) {
2246 // not sure why we don't check order here... it was in the
2247 // original code but commented out.
2248
2249 // The only way this happens is if we are
2250 // doing a write and we didn't have permissions
2251 // then subsequently saw a writeback (owned got evicted)
2252 // We need to make sure to perform the writeback first
2253 // To preserve the dirty data, then we can issue the write
2254
2270 writeBuffer.findPending(miss_mshr->blkAddr,
2271 miss_mshr->isSecure);
2272 if (conflict_mshr) {
2273 // not sure why we don't check order here... it was in the
2274 // original code but commented out.
2275
2276 // The only way this happens is if we are
2277 // doing a write and we didn't have permissions
2278 // then subsequently saw a writeback (owned got evicted)
2279 // We need to make sure to perform the writeback first
2280 // To preserve the dirty data, then we can issue the write
2281
2255 // should we return write_mshr here instead? I.e. do we
2282 // should we return wq_entry here instead? I.e. do we
2256 // have to flush writes in order? I don't think so... not
2257 // for Alpha anyway. Maybe for x86?
2258 return conflict_mshr;
2259
2260 // @todo Note that we ignore the ready time of the conflict here
2261 }
2262
2263 // No conflicts; issue read
2264 return miss_mshr;
2265 }
2266
2267 // fall through... no pending requests. Try a prefetch.
2283 // have to flush writes in order? I don't think so... not
2284 // for Alpha anyway. Maybe for x86?
2285 return conflict_mshr;
2286
2287 // @todo Note that we ignore the ready time of the conflict here
2288 }
2289
2290 // No conflicts; issue read
2291 return miss_mshr;
2292 }
2293
2294 // fall through... no pending requests. Try a prefetch.
2268 assert(!miss_mshr && !write_mshr);
2295 assert(!miss_mshr && !wq_entry);
2269 if (prefetcher && mshrQueue.canPrefetch()) {
2270 // If we have a miss queue slot, we can try a prefetch
2271 PacketPtr pkt = prefetcher->getPacket();
2272 if (pkt) {
2273 Addr pf_addr = blockAlign(pkt->getAddr());
2274 if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
2275 !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
2276 !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
2277 // Update statistic on number of prefetches issued
2278 // (hwpf_mshr_misses)
2279 assert(pkt->req->masterId() < system->maxMasters());
2280 mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
2281
2282 // allocate an MSHR and return it, note
2283 // that we send the packet straight away, so do not
2284 // schedule the send
2285 return allocateMissBuffer(pkt, curTick(), false);
2286 } else {
2287 // free the request and packet
2288 delete pkt->req;
2289 delete pkt;
2290 }
2291 }
2292 }
2293
2296 if (prefetcher && mshrQueue.canPrefetch()) {
2297 // If we have a miss queue slot, we can try a prefetch
2298 PacketPtr pkt = prefetcher->getPacket();
2299 if (pkt) {
2300 Addr pf_addr = blockAlign(pkt->getAddr());
2301 if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
2302 !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
2303 !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
2304 // Update statistic on number of prefetches issued
2305 // (hwpf_mshr_misses)
2306 assert(pkt->req->masterId() < system->maxMasters());
2307 mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
2308
2309 // allocate an MSHR and return it, note
2310 // that we send the packet straight away, so do not
2311 // schedule the send
2312 return allocateMissBuffer(pkt, curTick(), false);
2313 } else {
2314 // free the request and packet
2315 delete pkt->req;
2316 delete pkt;
2317 }
2318 }
2319 }
2320
2294 return NULL;
2321 return nullptr;
2295}
2296
2297bool
2298Cache::isCachedAbove(PacketPtr pkt, bool is_timing) const
2299{
2300 if (!forwardSnoops)
2301 return false;
2302 // Mirroring the flow of HardPFReqs, the cache sends CleanEvict and
2303 // Writeback snoops into upper level caches to check for copies of the
2304 // same block. Using the BLOCK_CACHED flag with the Writeback/CleanEvict
2305 // packet, the cache can inform the crossbar below of presence or absence
2306 // of the block.
2307 if (is_timing) {
2308 Packet snoop_pkt(pkt, true, false);
2309 snoop_pkt.setExpressSnoop();
2310 // Assert that packet is either Writeback or CleanEvict and not a
2311 // prefetch request because prefetch requests need an MSHR and may
2312 // generate a snoop response.
2313 assert(pkt->isEviction());
2314 snoop_pkt.senderState = NULL;
2315 cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
2316 // Writeback/CleanEvict snoops do not generate a snoop response.
2317 assert(!(snoop_pkt.cacheResponding()));
2318 return snoop_pkt.isBlockCached();
2319 } else {
2320 cpuSidePort->sendAtomicSnoop(pkt);
2321 return pkt->isBlockCached();
2322 }
2323}
2324
2322}
2323
2324bool
2325Cache::isCachedAbove(PacketPtr pkt, bool is_timing) const
2326{
2327 if (!forwardSnoops)
2328 return false;
2329 // Mirroring the flow of HardPFReqs, the cache sends CleanEvict and
2330 // Writeback snoops into upper level caches to check for copies of the
2331 // same block. Using the BLOCK_CACHED flag with the Writeback/CleanEvict
2332 // packet, the cache can inform the crossbar below of presence or absence
2333 // of the block.
2334 if (is_timing) {
2335 Packet snoop_pkt(pkt, true, false);
2336 snoop_pkt.setExpressSnoop();
2337 // Assert that packet is either Writeback or CleanEvict and not a
2338 // prefetch request because prefetch requests need an MSHR and may
2339 // generate a snoop response.
2340 assert(pkt->isEviction());
2341 snoop_pkt.senderState = NULL;
2342 cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
2343 // Writeback/CleanEvict snoops do not generate a snoop response.
2344 assert(!(snoop_pkt.cacheResponding()));
2345 return snoop_pkt.isBlockCached();
2346 } else {
2347 cpuSidePort->sendAtomicSnoop(pkt);
2348 return pkt->isBlockCached();
2349 }
2350}
2351
2325PacketPtr
2326Cache::getTimingPacket()
2352Tick
2353Cache::nextQueueReadyTime() const
2327{
2354{
2328 MSHR *mshr = getNextMSHR();
2355 Tick nextReady = std::min(mshrQueue.nextReadyTime(),
2356 writeBuffer.nextReadyTime());
2329
2357
2330 if (mshr == NULL) {
2331 return NULL;
2358 // Don't signal prefetch ready time if no MSHRs available
2359 // Will signal once enoguh MSHRs are deallocated
2360 if (prefetcher && mshrQueue.canPrefetch()) {
2361 nextReady = std::min(nextReady,
2362 prefetcher->nextPrefetchReadyTime());
2332 }
2333
2363 }
2364
2365 return nextReady;
2366}
2367
2368bool
2369Cache::sendMSHRQueuePacket(MSHR* mshr)
2370{
2371 assert(mshr);
2372
2334 // use request from 1st target
2335 PacketPtr tgt_pkt = mshr->getTarget()->pkt;
2373 // use request from 1st target
2374 PacketPtr tgt_pkt = mshr->getTarget()->pkt;
2336 PacketPtr pkt = NULL;
2337
2375
2338 DPRINTF(CachePort, "%s %s for addr %#llx size %d\n", __func__,
2339 tgt_pkt->cmdString(), tgt_pkt->getAddr(), tgt_pkt->getSize());
2376 DPRINTF(Cache, "%s MSHR %s for addr %#llx size %d\n", __func__,
2377 tgt_pkt->cmdString(), tgt_pkt->getAddr(),
2378 tgt_pkt->getSize());
2340
2341 CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
2342
2343 if (tgt_pkt->cmd == MemCmd::HardPFReq && forwardSnoops) {
2379
2380 CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
2381
2382 if (tgt_pkt->cmd == MemCmd::HardPFReq && forwardSnoops) {
2383 // we should never have hardware prefetches to allocated
2384 // blocks
2385 assert(blk == NULL);
2386
2344 // We need to check the caches above us to verify that
2345 // they don't have a copy of this block in the dirty state
2346 // at the moment. Without this check we could get a stale
2347 // copy from memory that might get used in place of the
2348 // dirty one.
2349 Packet snoop_pkt(tgt_pkt, true, false);
2350 snoop_pkt.setExpressSnoop();
2351 // We are sending this packet upwards, but if it hits we will
2352 // get a snoop response that we end up treating just like a
2353 // normal response, hence it needs the MSHR as its sender
2354 // state
2355 snoop_pkt.senderState = mshr;
2356 cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
2357
2358 // Check to see if the prefetch was squashed by an upper cache (to
2359 // prevent us from grabbing the line) or if a Check to see if a
2360 // writeback arrived between the time the prefetch was placed in
2361 // the MSHRs and when it was selected to be sent or if the
2362 // prefetch was squashed by an upper cache.
2363
2364 // It is important to check cacheResponding before
2365 // prefetchSquashed. If another cache has committed to
2366 // responding, it will be sending a dirty response which will
2367 // arrive at the MSHR allocated for this request. Checking the
2368 // prefetchSquash first may result in the MSHR being
2369 // prematurely deallocated.
2370 if (snoop_pkt.cacheResponding()) {
2371 auto M5_VAR_USED r = outstandingSnoop.insert(snoop_pkt.req);
2372 assert(r.second);
2373
2374 // if we are getting a snoop response with no sharers it
2375 // will be allocated as Modified
2376 bool pending_modified_resp = !snoop_pkt.hasSharers();
2377 markInService(mshr, pending_modified_resp);
2378
2379 DPRINTF(Cache, "Upward snoop of prefetch for addr"
2380 " %#x (%s) hit\n",
2381 tgt_pkt->getAddr(), tgt_pkt->isSecure()? "s": "ns");
2387 // We need to check the caches above us to verify that
2388 // they don't have a copy of this block in the dirty state
2389 // at the moment. Without this check we could get a stale
2390 // copy from memory that might get used in place of the
2391 // dirty one.
2392 Packet snoop_pkt(tgt_pkt, true, false);
2393 snoop_pkt.setExpressSnoop();
2394 // We are sending this packet upwards, but if it hits we will
2395 // get a snoop response that we end up treating just like a
2396 // normal response, hence it needs the MSHR as its sender
2397 // state
2398 snoop_pkt.senderState = mshr;
2399 cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
2400
2401 // Check to see if the prefetch was squashed by an upper cache (to
2402 // prevent us from grabbing the line) or if a Check to see if a
2403 // writeback arrived between the time the prefetch was placed in
2404 // the MSHRs and when it was selected to be sent or if the
2405 // prefetch was squashed by an upper cache.
2406
2407 // It is important to check cacheResponding before
2408 // prefetchSquashed. If another cache has committed to
2409 // responding, it will be sending a dirty response which will
2410 // arrive at the MSHR allocated for this request. Checking the
2411 // prefetchSquash first may result in the MSHR being
2412 // prematurely deallocated.
2413 if (snoop_pkt.cacheResponding()) {
2414 auto M5_VAR_USED r = outstandingSnoop.insert(snoop_pkt.req);
2415 assert(r.second);
2416
2417 // if we are getting a snoop response with no sharers it
2418 // will be allocated as Modified
2419 bool pending_modified_resp = !snoop_pkt.hasSharers();
2420 markInService(mshr, pending_modified_resp);
2421
2422 DPRINTF(Cache, "Upward snoop of prefetch for addr"
2423 " %#x (%s) hit\n",
2424 tgt_pkt->getAddr(), tgt_pkt->isSecure()? "s": "ns");
2382 return NULL;
2425 return false;
2383 }
2384
2426 }
2427
2385 if (snoop_pkt.isBlockCached() || blk != NULL) {
2428 if (snoop_pkt.isBlockCached()) {
2386 DPRINTF(Cache, "Block present, prefetch squashed by cache. "
2387 "Deallocating mshr target %#x.\n",
2388 mshr->blkAddr);
2429 DPRINTF(Cache, "Block present, prefetch squashed by cache. "
2430 "Deallocating mshr target %#x.\n",
2431 mshr->blkAddr);
2432
2389 // Deallocate the mshr target
2433 // Deallocate the mshr target
2390 if (mshr->queue->forceDeallocateTarget(mshr)) {
2434 if (mshrQueue.forceDeallocateTarget(mshr)) {
2391 // Clear block if this deallocation resulted freed an
2392 // mshr when all had previously been utilized
2435 // Clear block if this deallocation resulted freed an
2436 // mshr when all had previously been utilized
2393 clearBlocked((BlockedCause)(mshr->queue->index));
2437 clearBlocked(Blocked_NoMSHRs);
2394 }
2438 }
2395 return NULL;
2439 return false;
2396 }
2397 }
2398
2440 }
2441 }
2442
2399 if (mshr->isForwardNoResponse()) {
2400 // no response expected, just forward packet as it is
2401 assert(tags->findBlock(mshr->blkAddr, mshr->isSecure) == NULL);
2402 pkt = tgt_pkt;
2403 } else {
2404 pkt = getBusPacket(tgt_pkt, blk, mshr->needsWritable());
2443 // either a prefetch that is not present upstream, or a normal
2444 // MSHR request, proceed to get the packet to send downstream
2445 PacketPtr pkt = getBusPacket(tgt_pkt, blk, mshr->needsWritable());
2405
2446
2406 mshr->isForward = (pkt == NULL);
2447 mshr->isForward = (pkt == NULL);
2407
2448
2408 if (mshr->isForward) {
2409 // not a cache block request, but a response is expected
2410 // make copy of current packet to forward, keep current
2411 // copy for response handling
2412 pkt = new Packet(tgt_pkt, false, true);
2413 if (pkt->isWrite()) {
2414 pkt->setData(tgt_pkt->getConstPtr<uint8_t>());
2415 }
2416 }
2449 if (mshr->isForward) {
2450 // not a cache block request, but a response is expected
2451 // make copy of current packet to forward, keep current
2452 // copy for response handling
2453 pkt = new Packet(tgt_pkt, false, true);
2454 assert(!pkt->isWrite());
2417 }
2418
2455 }
2456
2419 assert(pkt != NULL);
2420 // play it safe and append (rather than set) the sender state, as
2421 // forwarded packets may already have existing state
2457 // play it safe and append (rather than set) the sender state,
2458 // as forwarded packets may already have existing state
2422 pkt->pushSenderState(mshr);
2459 pkt->pushSenderState(mshr);
2423 return pkt;
2424}
2425
2460
2461 if (!memSidePort->sendTimingReq(pkt)) {
2462 // we are awaiting a retry, but we
2463 // delete the packet and will be creating a new packet
2464 // when we get the opportunity
2465 delete pkt;
2426
2466
2427Tick
2428Cache::nextMSHRReadyTime() const
2467 // note that we have now masked any requestBus and
2468 // schedSendEvent (we will wait for a retry before
2469 // doing anything), and this is so even if we do not
2470 // care about this packet and might override it before
2471 // it gets retried
2472 return true;
2473 } else {
2474 // As part of the call to sendTimingReq the packet is
2475 // forwarded to all neighbouring caches (and any caches
2476 // above them) as a snoop. Thus at this point we know if
2477 // any of the neighbouring caches are responding, and if
2478 // so, we know it is dirty, and we can determine if it is
2479 // being passed as Modified, making our MSHR the ordering
2480 // point
2481 bool pending_modified_resp = !pkt->hasSharers() &&
2482 pkt->cacheResponding();
2483 markInService(mshr, pending_modified_resp);
2484 return false;
2485 }
2486}
2487
2488bool
2489Cache::sendWriteQueuePacket(WriteQueueEntry* wq_entry)
2429{
2490{
2430 Tick nextReady = std::min(mshrQueue.nextMSHRReadyTime(),
2431 writeBuffer.nextMSHRReadyTime());
2491 assert(wq_entry);
2432
2492
2433 // Don't signal prefetch ready time if no MSHRs available
2434 // Will signal once enoguh MSHRs are deallocated
2435 if (prefetcher && mshrQueue.canPrefetch()) {
2436 nextReady = std::min(nextReady,
2437 prefetcher->nextPrefetchReadyTime());
2493 // always a single target for write queue entries
2494 PacketPtr tgt_pkt = wq_entry->getTarget()->pkt;
2495
2496 DPRINTF(Cache, "%s write %s for addr %#llx size %d\n", __func__,
2497 tgt_pkt->cmdString(), tgt_pkt->getAddr(),
2498 tgt_pkt->getSize());
2499
2500 PacketPtr pkt = nullptr;
2501 bool delete_pkt = false;
2502
2503 if (tgt_pkt->isEviction()) {
2504 assert(!wq_entry->isUncacheable());
2505 // no response expected, just forward packet as it is
2506 pkt = tgt_pkt;
2507 } else {
2508 // the only thing we deal with besides eviction commands
2509 // are uncacheable writes
2510 assert(tgt_pkt->req->isUncacheable() && tgt_pkt->isWrite());
2511 // not a cache block request, but a response is expected
2512 // make copy of current packet to forward, keep current
2513 // copy for response handling
2514 pkt = new Packet(tgt_pkt, false, true);
2515 pkt->setData(tgt_pkt->getConstPtr<uint8_t>());
2516 delete_pkt = true;
2438 }
2439
2517 }
2518
2440 return nextReady;
2519 pkt->pushSenderState(wq_entry);
2520
2521 if (!memSidePort->sendTimingReq(pkt)) {
2522 if (delete_pkt) {
2523 // we are awaiting a retry, but we
2524 // delete the packet and will be creating a new packet
2525 // when we get the opportunity
2526 delete pkt;
2527 }
2528 // note that we have now masked any requestBus and
2529 // schedSendEvent (we will wait for a retry before
2530 // doing anything), and this is so even if we do not
2531 // care about this packet and might override it before
2532 // it gets retried
2533 return true;
2534 } else {
2535 markInService(wq_entry);
2536 return false;
2537 }
2441}
2442
2443void
2444Cache::serialize(CheckpointOut &cp) const
2445{
2446 bool dirty(isDirty());
2447
2448 if (dirty) {
2449 warn("*** The cache still contains dirty data. ***\n");
2450 warn(" Make sure to drain the system using the correct flags.\n");
2451 warn(" This checkpoint will not restore correctly and dirty data in "
2452 "the cache will be lost!\n");
2453 }
2454
2455 // Since we don't checkpoint the data in the cache, any dirty data
2456 // will be lost when restoring from a checkpoint of a system that
2457 // wasn't drained properly. Flag the checkpoint as invalid if the
2458 // cache contains dirty data.
2459 bool bad_checkpoint(dirty);
2460 SERIALIZE_SCALAR(bad_checkpoint);
2461}
2462
2463void
2464Cache::unserialize(CheckpointIn &cp)
2465{
2466 bool bad_checkpoint;
2467 UNSERIALIZE_SCALAR(bad_checkpoint);
2468 if (bad_checkpoint) {
2469 fatal("Restoring from checkpoints with dirty caches is not supported "
2470 "in the classic memory system. Please remove any caches or "
2471 " drain them properly before taking checkpoints.\n");
2472 }
2473}
2474
2475///////////////
2476//
2477// CpuSidePort
2478//
2479///////////////
2480
2481AddrRangeList
2482Cache::CpuSidePort::getAddrRanges() const
2483{
2484 return cache->getAddrRanges();
2485}
2486
2487bool
2488Cache::CpuSidePort::recvTimingReq(PacketPtr pkt)
2489{
2490 assert(!cache->system->bypassCaches());
2491
2492 bool success = false;
2493
2494 // always let express snoop packets through if even if blocked
2495 if (pkt->isExpressSnoop()) {
2496 // do not change the current retry state
2497 bool M5_VAR_USED bypass_success = cache->recvTimingReq(pkt);
2498 assert(bypass_success);
2499 return true;
2500 } else if (blocked || mustSendRetry) {
2501 // either already committed to send a retry, or blocked
2502 success = false;
2503 } else {
2504 // pass it on to the cache, and let the cache decide if we
2505 // have to retry or not
2506 success = cache->recvTimingReq(pkt);
2507 }
2508
2509 // remember if we have to retry
2510 mustSendRetry = !success;
2511 return success;
2512}
2513
2514Tick
2515Cache::CpuSidePort::recvAtomic(PacketPtr pkt)
2516{
2517 return cache->recvAtomic(pkt);
2518}
2519
2520void
2521Cache::CpuSidePort::recvFunctional(PacketPtr pkt)
2522{
2523 // functional request
2524 cache->functionalAccess(pkt, true);
2525}
2526
2527Cache::
2528CpuSidePort::CpuSidePort(const std::string &_name, Cache *_cache,
2529 const std::string &_label)
2530 : BaseCache::CacheSlavePort(_name, _cache, _label), cache(_cache)
2531{
2532}
2533
2534Cache*
2535CacheParams::create()
2536{
2537 assert(tags);
2538
2539 return new Cache(this);
2540}
2541///////////////
2542//
2543// MemSidePort
2544//
2545///////////////
2546
2547bool
2548Cache::MemSidePort::recvTimingResp(PacketPtr pkt)
2549{
2550 cache->recvTimingResp(pkt);
2551 return true;
2552}
2553
2554// Express snooping requests to memside port
2555void
2556Cache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
2557{
2558 // handle snooping requests
2559 cache->recvTimingSnoopReq(pkt);
2560}
2561
2562Tick
2563Cache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
2564{
2565 return cache->recvAtomicSnoop(pkt);
2566}
2567
2568void
2569Cache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
2570{
2571 // functional snoop (note that in contrast to atomic we don't have
2572 // a specific functionalSnoop method, as they have the same
2573 // behaviour regardless)
2574 cache->functionalAccess(pkt, false);
2575}
2576
2577void
2578Cache::CacheReqPacketQueue::sendDeferredPacket()
2579{
2580 // sanity check
2581 assert(!waitingOnRetry);
2582
2583 // there should never be any deferred request packets in the
2584 // queue, instead we resly on the cache to provide the packets
2585 // from the MSHR queue or write queue
2586 assert(deferredPacketReadyTime() == MaxTick);
2587
2588 // check for request packets (requests & writebacks)
2538}
2539
2540void
2541Cache::serialize(CheckpointOut &cp) const
2542{
2543 bool dirty(isDirty());
2544
2545 if (dirty) {
2546 warn("*** The cache still contains dirty data. ***\n");
2547 warn(" Make sure to drain the system using the correct flags.\n");
2548 warn(" This checkpoint will not restore correctly and dirty data in "
2549 "the cache will be lost!\n");
2550 }
2551
2552 // Since we don't checkpoint the data in the cache, any dirty data
2553 // will be lost when restoring from a checkpoint of a system that
2554 // wasn't drained properly. Flag the checkpoint as invalid if the
2555 // cache contains dirty data.
2556 bool bad_checkpoint(dirty);
2557 SERIALIZE_SCALAR(bad_checkpoint);
2558}
2559
2560void
2561Cache::unserialize(CheckpointIn &cp)
2562{
2563 bool bad_checkpoint;
2564 UNSERIALIZE_SCALAR(bad_checkpoint);
2565 if (bad_checkpoint) {
2566 fatal("Restoring from checkpoints with dirty caches is not supported "
2567 "in the classic memory system. Please remove any caches or "
2568 " drain them properly before taking checkpoints.\n");
2569 }
2570}
2571
2572///////////////
2573//
2574// CpuSidePort
2575//
2576///////////////
2577
2578AddrRangeList
2579Cache::CpuSidePort::getAddrRanges() const
2580{
2581 return cache->getAddrRanges();
2582}
2583
2584bool
2585Cache::CpuSidePort::recvTimingReq(PacketPtr pkt)
2586{
2587 assert(!cache->system->bypassCaches());
2588
2589 bool success = false;
2590
2591 // always let express snoop packets through if even if blocked
2592 if (pkt->isExpressSnoop()) {
2593 // do not change the current retry state
2594 bool M5_VAR_USED bypass_success = cache->recvTimingReq(pkt);
2595 assert(bypass_success);
2596 return true;
2597 } else if (blocked || mustSendRetry) {
2598 // either already committed to send a retry, or blocked
2599 success = false;
2600 } else {
2601 // pass it on to the cache, and let the cache decide if we
2602 // have to retry or not
2603 success = cache->recvTimingReq(pkt);
2604 }
2605
2606 // remember if we have to retry
2607 mustSendRetry = !success;
2608 return success;
2609}
2610
2611Tick
2612Cache::CpuSidePort::recvAtomic(PacketPtr pkt)
2613{
2614 return cache->recvAtomic(pkt);
2615}
2616
2617void
2618Cache::CpuSidePort::recvFunctional(PacketPtr pkt)
2619{
2620 // functional request
2621 cache->functionalAccess(pkt, true);
2622}
2623
2624Cache::
2625CpuSidePort::CpuSidePort(const std::string &_name, Cache *_cache,
2626 const std::string &_label)
2627 : BaseCache::CacheSlavePort(_name, _cache, _label), cache(_cache)
2628{
2629}
2630
2631Cache*
2632CacheParams::create()
2633{
2634 assert(tags);
2635
2636 return new Cache(this);
2637}
2638///////////////
2639//
2640// MemSidePort
2641//
2642///////////////
2643
2644bool
2645Cache::MemSidePort::recvTimingResp(PacketPtr pkt)
2646{
2647 cache->recvTimingResp(pkt);
2648 return true;
2649}
2650
2651// Express snooping requests to memside port
2652void
2653Cache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
2654{
2655 // handle snooping requests
2656 cache->recvTimingSnoopReq(pkt);
2657}
2658
2659Tick
2660Cache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
2661{
2662 return cache->recvAtomicSnoop(pkt);
2663}
2664
2665void
2666Cache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
2667{
2668 // functional snoop (note that in contrast to atomic we don't have
2669 // a specific functionalSnoop method, as they have the same
2670 // behaviour regardless)
2671 cache->functionalAccess(pkt, false);
2672}
2673
2674void
2675Cache::CacheReqPacketQueue::sendDeferredPacket()
2676{
2677 // sanity check
2678 assert(!waitingOnRetry);
2679
2680 // there should never be any deferred request packets in the
2681 // queue, instead we resly on the cache to provide the packets
2682 // from the MSHR queue or write queue
2683 assert(deferredPacketReadyTime() == MaxTick);
2684
2685 // check for request packets (requests & writebacks)
2589 PacketPtr pkt = cache.getTimingPacket();
2590 if (pkt == NULL) {
2686 QueueEntry* entry = cache.getNextQueueEntry();
2687
2688 if (!entry) {
2591 // can happen if e.g. we attempt a writeback and fail, but
2592 // before the retry, the writeback is eliminated because
2593 // we snoop another cache's ReadEx.
2594 } else {
2689 // can happen if e.g. we attempt a writeback and fail, but
2690 // before the retry, the writeback is eliminated because
2691 // we snoop another cache's ReadEx.
2692 } else {
2595 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
2596 // in most cases getTimingPacket allocates a new packet, and
2597 // we must delete it unless it is successfully sent
2598 bool delete_pkt = !mshr->isForwardNoResponse();
2599
2600 // let our snoop responses go first if there are responses to
2693 // let our snoop responses go first if there are responses to
2601 // the same addresses we are about to writeback, note that
2602 // this creates a dependency between requests and snoop
2603 // responses, but that should not be a problem since there is
2604 // a chain already and the key is that the snoop responses can
2605 // sink unconditionally
2606 if (snoopRespQueue.hasAddr(pkt->getAddr())) {
2607 DPRINTF(CachePort, "Waiting for snoop response to be sent\n");
2608 Tick when = snoopRespQueue.deferredPacketReadyTime();
2609 schedSendEvent(when);
2610
2611 if (delete_pkt)
2612 delete pkt;
2613
2694 // the same addresses
2695 if (checkConflictingSnoop(entry->blkAddr)) {
2614 return;
2615 }
2696 return;
2697 }
2616
2617
2618 waitingOnRetry = !masterPort.sendTimingReq(pkt);
2619
2620 if (waitingOnRetry) {
2621 DPRINTF(CachePort, "now waiting on a retry\n");
2622 if (delete_pkt) {
2623 // we are awaiting a retry, but we
2624 // delete the packet and will be creating a new packet
2625 // when we get the opportunity
2626 delete pkt;
2627 }
2628 // note that we have now masked any requestBus and
2629 // schedSendEvent (we will wait for a retry before
2630 // doing anything), and this is so even if we do not
2631 // care about this packet and might override it before
2632 // it gets retried
2633 } else {
2634 // As part of the call to sendTimingReq the packet is
2635 // forwarded to all neighbouring caches (and any caches
2636 // above them) as a snoop. Thus at this point we know if
2637 // any of the neighbouring caches are responding, and if
2638 // so, we know it is dirty, and we can determine if it is
2639 // being passed as Modified, making our MSHR the ordering
2640 // point
2641 bool pending_modified_resp = !pkt->hasSharers() &&
2642 pkt->cacheResponding();
2643
2644 cache.markInService(mshr, pending_modified_resp);
2645 }
2698 waitingOnRetry = entry->sendPacket(cache);
2646 }
2647
2648 // if we succeeded and are not waiting for a retry, schedule the
2699 }
2700
2701 // if we succeeded and are not waiting for a retry, schedule the
2649 // next send considering when the next MSHR is ready, note that
2702 // next send considering when the next queue is ready, note that
2650 // snoop responses have their own packet queue and thus schedule
2651 // their own events
2652 if (!waitingOnRetry) {
2703 // snoop responses have their own packet queue and thus schedule
2704 // their own events
2705 if (!waitingOnRetry) {
2653 schedSendEvent(cache.nextMSHRReadyTime());
2706 schedSendEvent(cache.nextQueueReadyTime());
2654 }
2655}
2656
2657Cache::
2658MemSidePort::MemSidePort(const std::string &_name, Cache *_cache,
2659 const std::string &_label)
2660 : BaseCache::CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
2661 _reqQueue(*_cache, *this, _snoopRespQueue, _label),
2662 _snoopRespQueue(*_cache, *this, _label), cache(_cache)
2663{
2664}
2707 }
2708}
2709
2710Cache::
2711MemSidePort::MemSidePort(const std::string &_name, Cache *_cache,
2712 const std::string &_label)
2713 : BaseCache::CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
2714 _reqQueue(*_cache, *this, _snoopRespQueue, _label),
2715 _snoopRespQueue(*_cache, *this, _label), cache(_cache)
2716{
2717}