Always get the functional access from the highest level of cache first.
[gem5.git] / src / mem / cache / cache_impl.hh
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Erik Hallnor
29 * Dave Greene
30 * Nathan Binkert
31 */
32
33 /**
34 * @file
35 * Cache definitions.
36 */
37
38 #include <assert.h>
39 #include <math.h>
40
41 #include <cassert>
42 #include <iostream>
43 #include <string>
44
45 #include "sim/host.hh"
46 #include "base/misc.hh"
47 #include "cpu/smt.hh"
48
49 #include "mem/cache/cache.hh"
50 #include "mem/cache/cache_blk.hh"
51 #include "mem/cache/miss/mshr.hh"
52 #include "mem/cache/prefetch/prefetcher.hh"
53
54 #include "sim/sim_exit.hh" // for SimExitEvent
55
56 template<class TagStore, class Buffering, class Coherence>
57 bool
58 Cache<TagStore,Buffering,Coherence>::
59 doTimingAccess(Packet *pkt, CachePort *cachePort, bool isCpuSide)
60 {
61 if (isCpuSide)
62 {
63 if (pkt->isWrite() && (pkt->req->isLocked())) {
64 pkt->req->setScResult(1);
65 }
66 access(pkt);
67
68 }
69 else
70 {
71 if (pkt->isResponse())
72 handleResponse(pkt);
73 else {
74 //Check if we should do the snoop
75 if (pkt->flags & SNOOP_COMMIT)
76 snoop(pkt);
77 }
78 }
79 return true;
80 }
81
82 template<class TagStore, class Buffering, class Coherence>
83 Tick
84 Cache<TagStore,Buffering,Coherence>::
85 doAtomicAccess(Packet *pkt, bool isCpuSide)
86 {
87 if (isCpuSide)
88 {
89 //Temporary solution to LL/SC
90 if (pkt->isWrite() && (pkt->req->isLocked())) {
91 pkt->req->setScResult(1);
92 }
93
94 probe(pkt, true, NULL);
95 //TEMP ALWAYS SUCCES FOR NOW
96 pkt->result = Packet::Success;
97 }
98 else
99 {
100 if (pkt->isResponse())
101 handleResponse(pkt);
102 else
103 return snoopProbe(pkt);
104 }
105 //Fix this timing info
106 return hitLatency;
107 }
108
109 template<class TagStore, class Buffering, class Coherence>
110 void
111 Cache<TagStore,Buffering,Coherence>::
112 doFunctionalAccess(Packet *pkt, bool isCpuSide)
113 {
114 if (isCpuSide)
115 {
116 //TEMP USE CPU?THREAD 0 0
117 pkt->req->setThreadContext(0,0);
118
119 //Temporary solution to LL/SC
120 if (pkt->isWrite() && (pkt->req->isLocked())) {
121 assert("Can't handle LL/SC on functional path\n");
122 }
123
124 probe(pkt, false, memSidePort);
125 //TEMP ALWAYS SUCCESFUL FOR NOW
126 pkt->result = Packet::Success;
127 }
128 else
129 {
130 probe(pkt, false, cpuSidePort);
131 }
132 }
133
134 template<class TagStore, class Buffering, class Coherence>
135 void
136 Cache<TagStore,Buffering,Coherence>::
137 recvStatusChange(Port::Status status, bool isCpuSide)
138 {
139
140 }
141
142
143 template<class TagStore, class Buffering, class Coherence>
144 Cache<TagStore,Buffering,Coherence>::
145 Cache(const std::string &_name,
146 Cache<TagStore,Buffering,Coherence>::Params &params)
147 : BaseCache(_name, params.baseParams),
148 prefetchAccess(params.prefetchAccess),
149 tags(params.tags), missQueue(params.missQueue),
150 coherence(params.coherence), prefetcher(params.prefetcher),
151 hitLatency(params.hitLatency)
152 {
153 tags->setCache(this);
154 tags->setPrefetcher(prefetcher);
155 missQueue->setCache(this);
156 missQueue->setPrefetcher(prefetcher);
157 coherence->setCache(this);
158 prefetcher->setCache(this);
159 prefetcher->setTags(tags);
160 prefetcher->setBuffer(missQueue);
161 invalidateReq = new Request((Addr) NULL, blkSize, 0);
162 invalidatePkt = new Packet(invalidateReq, Packet::InvalidateReq, 0);
163 }
164
165 template<class TagStore, class Buffering, class Coherence>
166 void
167 Cache<TagStore,Buffering,Coherence>::regStats()
168 {
169 BaseCache::regStats();
170 tags->regStats(name());
171 missQueue->regStats(name());
172 coherence->regStats(name());
173 prefetcher->regStats(name());
174 }
175
176 template<class TagStore, class Buffering, class Coherence>
177 bool
178 Cache<TagStore,Buffering,Coherence>::access(PacketPtr &pkt)
179 {
180 //@todo Add back in MemDebug Calls
181 // MemDebug::cacheAccess(pkt);
182 BlkType *blk = NULL;
183 PacketList writebacks;
184 int size = blkSize;
185 int lat = hitLatency;
186 if (prefetchAccess) {
187 //We are determining prefetches on access stream, call prefetcher
188 prefetcher->handleMiss(pkt, curTick);
189 }
190 if (!pkt->req->isUncacheable()) {
191 blk = tags->handleAccess(pkt, lat, writebacks);
192 } else {
193 size = pkt->getSize();
194 }
195 // If this is a block size write/hint (WH64) allocate the block here
196 // if the coherence protocol allows it.
197 /** @todo make the fast write alloc (wh64) work with coherence. */
198 /** @todo Do we want to do fast writes for writebacks as well? */
199 if (!blk && pkt->getSize() >= blkSize && coherence->allowFastWrites() &&
200 (pkt->cmd == Packet::WriteReq
201 || pkt->cmd == Packet::WriteInvalidateReq) ) {
202 // not outstanding misses, can do this
203 MSHR* outstanding_miss = missQueue->findMSHR(pkt->getAddr());
204 if (pkt->cmd == Packet::WriteInvalidateReq || !outstanding_miss) {
205 if (outstanding_miss) {
206 warn("WriteInv doing a fastallocate"
207 "with an outstanding miss to the same address\n");
208 }
209 blk = tags->handleFill(NULL, pkt, BlkValid | BlkWritable,
210 writebacks);
211 ++fastWrites;
212 }
213 }
214 while (!writebacks.empty()) {
215 missQueue->doWriteback(writebacks.front());
216 writebacks.pop_front();
217 }
218 DPRINTF(Cache, "%s %x %s blk_addr: %x\n", pkt->cmdString(),
219 pkt->getAddr() & (((ULL(1))<<48)-1), (blk) ? "hit" : "miss",
220 pkt->getAddr() & ~((Addr)blkSize - 1));
221 if (blk) {
222 // Hit
223 hits[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
224 // clear dirty bit if write through
225 if (pkt->needsResponse())
226 respond(pkt, curTick+lat);
227 if (pkt->cmd == Packet::Writeback) {
228 //Signal that you can kill the pkt/req
229 pkt->flags |= SATISFIED;
230 }
231 return true;
232 }
233
234 // Miss
235 if (!pkt->req->isUncacheable()) {
236 misses[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
237 /** @todo Move miss count code into BaseCache */
238 if (missCount) {
239 --missCount;
240 if (missCount == 0)
241 exitSimLoop("A cache reached the maximum miss count");
242 }
243 }
244 missQueue->handleMiss(pkt, size, curTick + hitLatency);
245 // return MA_CACHE_MISS;
246 return true;
247 }
248
249
250 template<class TagStore, class Buffering, class Coherence>
251 Packet *
252 Cache<TagStore,Buffering,Coherence>::getPacket()
253 {
254 assert(missQueue->havePending());
255 Packet * pkt = missQueue->getPacket();
256 if (pkt) {
257 if (!pkt->req->isUncacheable()) {
258 if (pkt->cmd == Packet::HardPFReq)
259 misses[Packet::HardPFReq][0/*pkt->req->getThreadNum()*/]++;
260 BlkType *blk = tags->findBlock(pkt);
261 Packet::Command cmd = coherence->getBusCmd(pkt->cmd,
262 (blk)? blk->status : 0);
263 missQueue->setBusCmd(pkt, cmd);
264 }
265 }
266
267 assert(!doMasterRequest() || missQueue->havePending());
268 assert(!pkt || pkt->time <= curTick);
269 return pkt;
270 }
271
272 template<class TagStore, class Buffering, class Coherence>
273 void
274 Cache<TagStore,Buffering,Coherence>::sendResult(PacketPtr &pkt, MSHR* mshr,
275 bool success)
276 {
277 if (success && !(pkt && (pkt->flags & NACKED_LINE))) {
278 if (!mshr->pkt->needsResponse()
279 && !(mshr->pkt->cmd == Packet::UpgradeReq)
280 && (pkt && (pkt->flags & SATISFIED))) {
281 //Writeback, clean up the non copy version of the packet
282 delete pkt;
283 }
284 missQueue->markInService(mshr->pkt, mshr);
285 //Temp Hack for UPGRADES
286 if (mshr->pkt && mshr->pkt->cmd == Packet::UpgradeReq) {
287 assert(pkt); //Upgrades need to be fixed
288 pkt->flags &= ~CACHE_LINE_FILL;
289 BlkType *blk = tags->findBlock(pkt);
290 CacheBlk::State old_state = (blk) ? blk->status : 0;
291 CacheBlk::State new_state = coherence->getNewState(pkt,old_state);
292 if (old_state != new_state)
293 DPRINTF(Cache, "Block for blk addr %x moving from "
294 "state %i to %i\n",
295 pkt->getAddr() & (((ULL(1))<<48)-1),
296 old_state, new_state);
297 //Set the state on the upgrade
298 memcpy(pkt->getPtr<uint8_t>(), blk->data, blkSize);
299 PacketList writebacks;
300 tags->handleFill(blk, mshr, new_state, writebacks, pkt);
301 assert(writebacks.empty());
302 missQueue->handleResponse(pkt, curTick + hitLatency);
303 }
304 } else if (pkt && !pkt->req->isUncacheable()) {
305 pkt->flags &= ~NACKED_LINE;
306 pkt->flags &= ~SATISFIED;
307 pkt->flags &= ~SNOOP_COMMIT;
308
309 //Rmove copy from mshr
310 delete mshr->pkt;
311 mshr->pkt = pkt;
312
313 missQueue->restoreOrigCmd(pkt);
314 }
315 }
316
317 template<class TagStore, class Buffering, class Coherence>
318 void
319 Cache<TagStore,Buffering,Coherence>::handleResponse(Packet * &pkt)
320 {
321 BlkType *blk = NULL;
322 if (pkt->senderState) {
323 //Delete temp copy in MSHR, restore it.
324 delete ((MSHR*)pkt->senderState)->pkt;
325 ((MSHR*)pkt->senderState)->pkt = pkt;
326 if (pkt->result == Packet::Nacked) {
327 //pkt->reinitFromRequest();
328 warn("NACKs from devices not connected to the same bus "
329 "not implemented\n");
330 return;
331 }
332 if (pkt->result == Packet::BadAddress) {
333 //Make the response a Bad address and send it
334 }
335 // MemDebug::cacheResponse(pkt);
336 DPRINTF(Cache, "Handling reponse to %x, blk addr: %x\n",pkt->getAddr(),
337 pkt->getAddr() & (((ULL(1))<<48)-1));
338
339 if (pkt->isCacheFill() && !pkt->isNoAllocate()) {
340 blk = tags->findBlock(pkt);
341 CacheBlk::State old_state = (blk) ? blk->status : 0;
342 PacketList writebacks;
343 CacheBlk::State new_state = coherence->getNewState(pkt,old_state);
344 if (old_state != new_state)
345 DPRINTF(Cache, "Block for blk addr %x moving from "
346 "state %i to %i\n",
347 pkt->getAddr() & (((ULL(1))<<48)-1),
348 old_state, new_state);
349 blk = tags->handleFill(blk, (MSHR*)pkt->senderState,
350 new_state, writebacks, pkt);
351 while (!writebacks.empty()) {
352 missQueue->doWriteback(writebacks.front());
353 writebacks.pop_front();
354 }
355 }
356 missQueue->handleResponse(pkt, curTick + hitLatency);
357 }
358 }
359
360 template<class TagStore, class Buffering, class Coherence>
361 Packet *
362 Cache<TagStore,Buffering,Coherence>::getCoherencePacket()
363 {
364 return coherence->getPacket();
365 }
366
367 template<class TagStore, class Buffering, class Coherence>
368 void
369 Cache<TagStore,Buffering,Coherence>::sendCoherenceResult(Packet* &pkt,
370 MSHR *cshr,
371 bool success)
372 {
373 coherence->sendResult(pkt, cshr, success);
374 }
375
376
377 template<class TagStore, class Buffering, class Coherence>
378 void
379 Cache<TagStore,Buffering,Coherence>::snoop(Packet * &pkt)
380 {
381 if (pkt->req->isUncacheable()) {
382 //Can't get a hit on an uncacheable address
383 //Revisit this for multi level coherence
384 return;
385 }
386 Addr blk_addr = pkt->getAddr() & ~(Addr(blkSize-1));
387 BlkType *blk = tags->findBlock(pkt);
388 MSHR *mshr = missQueue->findMSHR(blk_addr);
389 if (coherence->hasProtocol()) { //@todo Move this into handle bus req
390 //If we find an mshr, and it is in service, we need to NACK or
391 //invalidate
392 if (mshr) {
393 if (mshr->inService) {
394 if ((mshr->pkt->isInvalidate() || !mshr->pkt->isCacheFill())
395 && (pkt->cmd != Packet::InvalidateReq
396 && pkt->cmd != Packet::WriteInvalidateReq)) {
397 //If the outstanding request was an invalidate
398 //(upgrade,readex,..) Then we need to ACK the request
399 //until we get the data Also NACK if the outstanding
400 //request is not a cachefill (writeback)
401 assert(!(pkt->flags & SATISFIED));
402 pkt->flags |= SATISFIED;
403 pkt->flags |= NACKED_LINE;
404 ///@todo NACK's from other levels
405 //warn("NACKs from devices not connected to the same bus "
406 //"not implemented\n");
407 //respondToSnoop(pkt, curTick + hitLatency);
408 return;
409 }
410 else {
411 //The supplier will be someone else, because we are
412 //waiting for the data. This should cause this cache to
413 //be forced to go to the shared state, not the exclusive
414 //even though the shared line won't be asserted. But for
415 //now we will just invlidate ourselves and allow the other
416 //cache to go into the exclusive state. @todo Make it so
417 //a read to a pending read doesn't invalidate. @todo Make
418 //it so that a read to a pending read can't be exclusive
419 //now.
420
421 //Set the address so find match works
422 //panic("Don't have invalidates yet\n");
423 invalidatePkt->addrOverride(pkt->getAddr());
424
425 //Append the invalidate on
426 missQueue->addTarget(mshr,invalidatePkt);
427 DPRINTF(Cache, "Appending Invalidate to blk_addr: %x\n",
428 pkt->getAddr() & (((ULL(1))<<48)-1));
429 return;
430 }
431 }
432 }
433 //We also need to check the writeback buffers and handle those
434 std::vector<MSHR *> writebacks;
435 if (missQueue->findWrites(blk_addr, writebacks)) {
436 DPRINTF(Cache, "Snoop hit in writeback to blk_addr: %x\n",
437 pkt->getAddr() & (((ULL(1))<<48)-1));
438
439 //Look through writebacks for any non-uncachable writes, use that
440 for (int i=0; i<writebacks.size(); i++) {
441 mshr = writebacks[i];
442
443 if (!mshr->pkt->req->isUncacheable()) {
444 if (pkt->isRead()) {
445 //Only Upgrades don't get here
446 //Supply the data
447 assert(!(pkt->flags & SATISFIED));
448 pkt->flags |= SATISFIED;
449
450 //If we are in an exclusive protocol, make it ask again
451 //to get write permissions (upgrade), signal shared
452 pkt->flags |= SHARED_LINE;
453
454 assert(pkt->isRead());
455 Addr offset = pkt->getAddr() & (blkSize - 1);
456 assert(offset < blkSize);
457 assert(pkt->getSize() <= blkSize);
458 assert(offset + pkt->getSize() <=blkSize);
459 memcpy(pkt->getPtr<uint8_t>(), mshr->pkt->getPtr<uint8_t>() + offset, pkt->getSize());
460
461 respondToSnoop(pkt, curTick + hitLatency);
462 }
463
464 if (pkt->isInvalidate()) {
465 //This must be an upgrade or other cache will take
466 //ownership
467 missQueue->markInService(mshr->pkt, mshr);
468 }
469 return;
470 }
471 }
472 }
473 }
474 CacheBlk::State new_state;
475 bool satisfy = coherence->handleBusRequest(pkt,blk,mshr, new_state);
476 if (satisfy) {
477 DPRINTF(Cache, "Cache snooped a %s request for addr %x and "
478 "now supplying data, new state is %i\n",
479 pkt->cmdString(), blk_addr, new_state);
480
481 tags->handleSnoop(blk, new_state, pkt);
482 respondToSnoop(pkt, curTick + hitLatency);
483 return;
484 }
485 if (blk)
486 DPRINTF(Cache, "Cache snooped a %s request for addr %x, "
487 "new state is %i\n", pkt->cmdString(), blk_addr, new_state);
488 tags->handleSnoop(blk, new_state);
489 }
490
491 template<class TagStore, class Buffering, class Coherence>
492 void
493 Cache<TagStore,Buffering,Coherence>::snoopResponse(Packet * &pkt)
494 {
495 //Need to handle the response, if NACKED
496 if (pkt->flags & NACKED_LINE) {
497 //Need to mark it as not in service, and retry for bus
498 assert(0); //Yeah, we saw a NACK come through
499
500 //For now this should never get called, we return false when we see a
501 //NACK instead, by doing this we allow the bus_blocked mechanism to
502 //handle the retry For now it retrys in just 2 cycles, need to figure
503 //out how to change that Eventually we will want to also have success
504 //come in as a parameter Need to make sure that we handle the
505 //functionality that happens on successufl return of the sendAddr
506 //function
507 }
508 }
509
510 template<class TagStore, class Buffering, class Coherence>
511 void
512 Cache<TagStore,Buffering,Coherence>::invalidateBlk(Addr addr)
513 {
514 tags->invalidateBlk(addr);
515 }
516
517
518 /**
519 * @todo Fix to not assume write allocate
520 */
521 template<class TagStore, class Buffering, class Coherence>
522 Tick
523 Cache<TagStore,Buffering,Coherence>::probe(Packet * &pkt, bool update,
524 CachePort* otherSidePort)
525 {
526 // MemDebug::cacheProbe(pkt);
527 if (!pkt->req->isUncacheable()) {
528 if (pkt->isInvalidate() && !pkt->isRead()
529 && !pkt->isWrite()) {
530 //Upgrade or Invalidate, satisfy it, don't forward
531 DPRINTF(Cache, "%s %x ? blk_addr: %x\n", pkt->cmdString(),
532 pkt->getAddr() & (((ULL(1))<<48)-1),
533 pkt->getAddr() & ~((Addr)blkSize - 1));
534 pkt->flags |= SATISFIED;
535 return 0;
536 }
537 }
538
539 if (!update && (pkt->isWrite() || (otherSidePort == cpuSidePort))) {
540 // Still need to change data in all locations.
541 otherSidePort->sendFunctional(pkt);
542 if (pkt->isRead() && pkt->result == Packet::Success)
543 return 0;
544 }
545
546 PacketList writebacks;
547 int lat;
548 BlkType *blk = tags->handleAccess(pkt, lat, writebacks, update);
549
550 DPRINTF(Cache, "%s %x %s blk_addr: %x\n", pkt->cmdString(),
551 pkt->getAddr() & (((ULL(1))<<48)-1), (blk) ? "hit" : "miss",
552 pkt->getAddr() & ~((Addr)blkSize - 1));
553
554
555 // Need to check for outstanding misses and writes
556 Addr blk_addr = pkt->getAddr() & ~(blkSize - 1);
557
558 // There can only be one matching outstanding miss.
559 MSHR* mshr = missQueue->findMSHR(blk_addr);
560
561 // There can be many matching outstanding writes.
562 std::vector<MSHR*> writes;
563 missQueue->findWrites(blk_addr, writes);
564
565 if (!update) {
566 // Check for data in MSHR and writebuffer.
567 if (mshr) {
568 warn("Found outstanding miss on an non-update probe");
569 MSHR::TargetList *targets = mshr->getTargetList();
570 MSHR::TargetList::iterator i = targets->begin();
571 MSHR::TargetList::iterator end = targets->end();
572 for (; i != end; ++i) {
573 Packet * target = *i;
574 // If the target contains data, and it overlaps the
575 // probed request, need to update data
576 if (target->isWrite() && target->intersect(pkt)) {
577 uint8_t* pkt_data;
578 uint8_t* write_data;
579 int data_size;
580 if (target->getAddr() < pkt->getAddr()) {
581 int offset = pkt->getAddr() - target->getAddr();
582 pkt_data = pkt->getPtr<uint8_t>();
583 write_data = target->getPtr<uint8_t>() + offset;
584 data_size = target->getSize() - offset;
585 assert(data_size > 0);
586 if (data_size > pkt->getSize())
587 data_size = pkt->getSize();
588 } else {
589 int offset = target->getAddr() - pkt->getAddr();
590 pkt_data = pkt->getPtr<uint8_t>() + offset;
591 write_data = target->getPtr<uint8_t>();
592 data_size = pkt->getSize() - offset;
593 assert(data_size > pkt->getSize());
594 if (data_size > target->getSize())
595 data_size = target->getSize();
596 }
597
598 if (pkt->isWrite()) {
599 memcpy(pkt_data, write_data, data_size);
600 } else {
601 memcpy(write_data, pkt_data, data_size);
602 }
603 }
604 }
605 }
606 for (int i = 0; i < writes.size(); ++i) {
607 Packet * write = writes[i]->pkt;
608 if (write->intersect(pkt)) {
609 warn("Found outstanding write on an non-update probe");
610 uint8_t* pkt_data;
611 uint8_t* write_data;
612 int data_size;
613 if (write->getAddr() < pkt->getAddr()) {
614 int offset = pkt->getAddr() - write->getAddr();
615 pkt_data = pkt->getPtr<uint8_t>();
616 write_data = write->getPtr<uint8_t>() + offset;
617 data_size = write->getSize() - offset;
618 assert(data_size > 0);
619 if (data_size > pkt->getSize())
620 data_size = pkt->getSize();
621 } else {
622 int offset = write->getAddr() - pkt->getAddr();
623 pkt_data = pkt->getPtr<uint8_t>() + offset;
624 write_data = write->getPtr<uint8_t>();
625 data_size = pkt->getSize() - offset;
626 assert(data_size > pkt->getSize());
627 if (data_size > write->getSize())
628 data_size = write->getSize();
629 }
630
631 if (pkt->isWrite()) {
632 memcpy(pkt_data, write_data, data_size);
633 } else {
634 memcpy(write_data, pkt_data, data_size);
635 }
636
637 }
638 }
639 } else if (!blk) {
640 // update the cache state and statistics
641 if (mshr || !writes.empty()){
642 // Can't handle it, return pktuest unsatisfied.
643 panic("Atomic access ran into outstanding MSHR's or WB's!");
644 }
645 if (!pkt->req->isUncacheable()) {
646 // Fetch the cache block to fill
647 BlkType *blk = tags->findBlock(pkt);
648 Packet::Command temp_cmd = coherence->getBusCmd(pkt->cmd,
649 (blk)? blk->status : 0);
650
651 Packet * busPkt = new Packet(pkt->req,temp_cmd, -1, blkSize);
652
653 busPkt->allocate();
654
655 busPkt->time = curTick;
656
657 DPRINTF(Cache, "Sending a atomic %s for %x blk_addr: %x\n",
658 busPkt->cmdString(),
659 busPkt->getAddr() & (((ULL(1))<<48)-1),
660 busPkt->getAddr() & ~((Addr)blkSize - 1));
661
662 lat = memSidePort->sendAtomic(busPkt);
663
664 //Be sure to flip the response to a request for coherence
665 if (busPkt->needsResponse()) {
666 busPkt->makeAtomicResponse();
667 }
668
669 /* if (!(busPkt->flags & SATISFIED)) {
670 // blocked at a higher level, just return
671 return 0;
672 }
673
674 */ misses[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
675
676 CacheBlk::State old_state = (blk) ? blk->status : 0;
677 CacheBlk::State new_state =
678 coherence->getNewState(busPkt, old_state);
679 DPRINTF(Cache,
680 "Receive response:%s for blk addr %x in state %i\n",
681 busPkt->cmdString(),
682 busPkt->getAddr() & (((ULL(1))<<48)-1), old_state);
683 if (old_state != new_state)
684 DPRINTF(Cache, "Block for blk addr %x moving from "
685 "state %i to %i\n",
686 busPkt->getAddr() & (((ULL(1))<<48)-1),
687 old_state, new_state);
688
689 tags->handleFill(blk, busPkt,
690 new_state,
691 writebacks, pkt);
692 //Free the packet
693 delete busPkt;
694
695 // Handle writebacks if needed
696 while (!writebacks.empty()){
697 Packet *wbPkt = writebacks.front();
698 memSidePort->sendAtomic(wbPkt);
699 writebacks.pop_front();
700 delete wbPkt;
701 }
702 return lat + hitLatency;
703 } else {
704 return memSidePort->sendAtomic(pkt);
705 }
706 } else {
707 // There was a cache hit.
708 // Handle writebacks if needed
709 while (!writebacks.empty()){
710 memSidePort->sendAtomic(writebacks.front());
711 writebacks.pop_front();
712 }
713
714 hits[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
715
716 return hitLatency;
717 }
718 fatal("Probe not handled.\n");
719 return 0;
720 }
721
722 template<class TagStore, class Buffering, class Coherence>
723 Tick
724 Cache<TagStore,Buffering,Coherence>::snoopProbe(PacketPtr &pkt)
725 {
726 Addr blk_addr = pkt->getAddr() & ~(Addr(blkSize-1));
727 BlkType *blk = tags->findBlock(pkt);
728 MSHR *mshr = missQueue->findMSHR(blk_addr);
729 CacheBlk::State new_state = 0;
730 bool satisfy = coherence->handleBusRequest(pkt,blk,mshr, new_state);
731 if (satisfy) {
732 DPRINTF(Cache, "Cache snooped a %s request for addr %x and "
733 "now supplying data, new state is %i\n",
734 pkt->cmdString(), blk_addr, new_state);
735
736 tags->handleSnoop(blk, new_state, pkt);
737 return hitLatency;
738 }
739 if (blk)
740 DPRINTF(Cache, "Cache snooped a %s request for addr %x, "
741 "new state is %i\n",
742 pkt->cmdString(), blk_addr, new_state);
743 tags->handleSnoop(blk, new_state);
744 return 0;
745 }
746