3 * Copyright (c) 2010-2013 ARM Limited
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Copyright (c) 2004-2005 The Regents of The University of Michigan
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #ifndef __CPU_O3_LSQ_UNIT_IMPL_HH__
46 #define __CPU_O3_LSQ_UNIT_IMPL_HH__
48 #include "arch/generic/debugfaults.hh"
49 #include "arch/locked_mem.hh"
50 #include "base/str.hh"
51 #include "config/the_isa.hh"
52 #include "cpu/checker/cpu.hh"
53 #include "cpu/o3/lsq.hh"
54 #include "cpu/o3/lsq_unit.hh"
55 #include "debug/Activity.hh"
56 #include "debug/IEW.hh"
57 #include "debug/LSQUnit.hh"
58 #include "debug/O3PipeView.hh"
59 #include "mem/packet.hh"
60 #include "mem/request.hh"
63 LSQUnit<Impl>::WritebackEvent::WritebackEvent(DynInstPtr &_inst, PacketPtr _pkt,
65 : Event(Default_Pri, AutoDelete),
66 inst(_inst), pkt(_pkt), lsqPtr(lsq_ptr)
72 LSQUnit<Impl>::WritebackEvent::process()
74 assert(!lsqPtr->cpu->switchedOut());
76 lsqPtr->writeback(inst, pkt);
79 delete pkt->senderState;
87 LSQUnit<Impl>::WritebackEvent::description() const
89 return "Store writeback";
94 LSQUnit<Impl>::completeDataAccess(PacketPtr pkt)
96 LSQSenderState *state = dynamic_cast<LSQSenderState *>(pkt->senderState);
97 DynInstPtr inst = state->inst;
98 DPRINTF(IEW, "Writeback event [sn:%lli].\n", inst->seqNum);
99 DPRINTF(Activity, "Activity: Writeback event [sn:%lli].\n", inst->seqNum);
101 //iewStage->ldstQueue.removeMSHR(inst->threadNumber,inst->seqNum);
103 // If this is a split access, wait until all packets are received.
104 if (TheISA::HasUnalignedMemAcc && !state->complete()) {
110 assert(!cpu->switchedOut());
111 if (inst->isSquashed()) {
112 iewStage->decrWb(inst->seqNum);
115 if (!TheISA::HasUnalignedMemAcc || !state->isSplit ||
117 writeback(inst, pkt);
119 writeback(inst, state->mainPkt);
123 if (inst->isStore()) {
124 completeStore(state->idx);
128 if (TheISA::HasUnalignedMemAcc && state->isSplit && state->isLoad) {
129 delete state->mainPkt->req;
130 delete state->mainPkt;
133 pkt->req->setAccessLatency();
134 cpu->ppDataAccessComplete->notify(std::make_pair(inst, pkt));
141 template <class Impl>
142 LSQUnit<Impl>::LSQUnit()
143 : loads(0), stores(0), storesToWB(0), cacheBlockMask(0), stalled(false),
144 isStoreBlocked(false), isLoadBlocked(false),
145 loadBlockedHandled(false), storeInFlight(false), hasPendingPkt(false)
151 LSQUnit<Impl>::init(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params,
152 LSQ *lsq_ptr, unsigned maxLQEntries, unsigned maxSQEntries,
162 DPRINTF(LSQUnit, "Creating LSQUnit%i object.\n",id);
164 // Add 1 for the sentinel entry (they are circular queues).
165 LQEntries = maxLQEntries + 1;
166 SQEntries = maxSQEntries + 1;
168 //Due to uint8_t index in LSQSenderState
169 assert(LQEntries <= 256);
170 assert(SQEntries <= 256);
172 loadQueue.resize(LQEntries);
173 storeQueue.resize(SQEntries);
175 depCheckShift = params->LSQDepCheckShift;
176 checkLoads = params->LSQCheckLoads;
177 cachePorts = params->cachePorts;
178 needsTSO = params->needsTSO;
186 LSQUnit<Impl>::resetState()
188 loads = stores = storesToWB = 0;
190 loadHead = loadTail = 0;
192 storeHead = storeWBIdx = storeTail = 0;
197 memDepViolator = NULL;
199 blockedLoadSeqNum = 0;
202 isLoadBlocked = false;
203 loadBlockedHandled = false;
205 cacheBlockMask = ~(cpu->cacheLineSize() - 1);
210 LSQUnit<Impl>::name() const
212 if (Impl::MaxThreads == 1) {
213 return iewStage->name() + ".lsq";
215 return iewStage->name() + ".lsq.thread" + to_string(lsqID);
221 LSQUnit<Impl>::regStats()
224 .name(name() + ".forwLoads")
225 .desc("Number of loads that had data forwarded from stores");
228 .name(name() + ".invAddrLoads")
229 .desc("Number of loads ignored due to an invalid address");
232 .name(name() + ".squashedLoads")
233 .desc("Number of loads squashed");
236 .name(name() + ".ignoredResponses")
237 .desc("Number of memory responses ignored because the instruction is squashed");
240 .name(name() + ".memOrderViolation")
241 .desc("Number of memory ordering violations");
244 .name(name() + ".squashedStores")
245 .desc("Number of stores squashed");
248 .name(name() + ".invAddrSwpfs")
249 .desc("Number of software prefetches ignored due to an invalid address");
252 .name(name() + ".blockedLoads")
253 .desc("Number of blocked loads due to partial load-store forwarding");
256 .name(name() + ".rescheduledLoads")
257 .desc("Number of loads that were rescheduled");
260 .name(name() + ".cacheBlocked")
261 .desc("Number of times an access to memory failed due to the cache being blocked");
266 LSQUnit<Impl>::setDcachePort(MasterPort *dcache_port)
268 dcachePort = dcache_port;
273 LSQUnit<Impl>::clearLQ()
280 LSQUnit<Impl>::clearSQ()
287 LSQUnit<Impl>::drainSanityCheck() const
289 for (int i = 0; i < loadQueue.size(); ++i)
290 assert(!loadQueue[i]);
292 assert(storesToWB == 0);
298 LSQUnit<Impl>::takeOverFrom()
305 LSQUnit<Impl>::resizeLQ(unsigned size)
307 unsigned size_plus_sentinel = size + 1;
308 assert(size_plus_sentinel >= LQEntries);
310 if (size_plus_sentinel > LQEntries) {
311 while (size_plus_sentinel > loadQueue.size()) {
313 loadQueue.push_back(dummy);
317 LQEntries = size_plus_sentinel;
320 assert(LQEntries <= 256);
325 LSQUnit<Impl>::resizeSQ(unsigned size)
327 unsigned size_plus_sentinel = size + 1;
328 if (size_plus_sentinel > SQEntries) {
329 while (size_plus_sentinel > storeQueue.size()) {
331 storeQueue.push_back(dummy);
335 SQEntries = size_plus_sentinel;
338 assert(SQEntries <= 256);
341 template <class Impl>
343 LSQUnit<Impl>::insert(DynInstPtr &inst)
345 assert(inst->isMemRef());
347 assert(inst->isLoad() || inst->isStore());
349 if (inst->isLoad()) {
358 template <class Impl>
360 LSQUnit<Impl>::insertLoad(DynInstPtr &load_inst)
362 assert((loadTail + 1) % LQEntries != loadHead);
363 assert(loads < LQEntries);
365 DPRINTF(LSQUnit, "Inserting load PC %s, idx:%i [sn:%lli]\n",
366 load_inst->pcState(), loadTail, load_inst->seqNum);
368 load_inst->lqIdx = loadTail;
371 load_inst->sqIdx = -1;
373 load_inst->sqIdx = storeTail;
376 loadQueue[loadTail] = load_inst;
383 template <class Impl>
385 LSQUnit<Impl>::insertStore(DynInstPtr &store_inst)
387 // Make sure it is not full before inserting an instruction.
388 assert((storeTail + 1) % SQEntries != storeHead);
389 assert(stores < SQEntries);
391 DPRINTF(LSQUnit, "Inserting store PC %s, idx:%i [sn:%lli]\n",
392 store_inst->pcState(), storeTail, store_inst->seqNum);
394 store_inst->sqIdx = storeTail;
395 store_inst->lqIdx = loadTail;
397 storeQueue[storeTail] = SQEntry(store_inst);
399 incrStIdx(storeTail);
404 template <class Impl>
405 typename Impl::DynInstPtr
406 LSQUnit<Impl>::getMemDepViolator()
408 DynInstPtr temp = memDepViolator;
410 memDepViolator = NULL;
415 template <class Impl>
417 LSQUnit<Impl>::numFreeEntries()
419 unsigned free_lq_entries = LQEntries - loads;
420 unsigned free_sq_entries = SQEntries - stores;
422 // Both the LQ and SQ entries have an extra dummy entry to differentiate
423 // empty/full conditions. Subtract 1 from the free entries.
424 if (free_lq_entries < free_sq_entries) {
425 return free_lq_entries - 1;
427 return free_sq_entries - 1;
431 template <class Impl>
433 LSQUnit<Impl>::checkSnoop(PacketPtr pkt)
435 int load_idx = loadHead;
436 DPRINTF(LSQUnit, "Got snoop for address %#x\n", pkt->getAddr());
438 // Unlock the cpu-local monitor when the CPU sees a snoop to a locked
439 // address. The CPU can speculatively execute a LL operation after a pending
440 // SC operation in the pipeline and that can make the cache monitor the CPU
441 // is connected to valid while it really shouldn't be.
442 for (int x = 0; x < cpu->numContexts(); x++) {
443 ThreadContext *tc = cpu->getContext(x);
444 bool no_squash = cpu->thread[x]->noSquashFromTC;
445 cpu->thread[x]->noSquashFromTC = true;
446 TheISA::handleLockedSnoop(tc, pkt, cacheBlockMask);
447 cpu->thread[x]->noSquashFromTC = no_squash;
450 Addr invalidate_addr = pkt->getAddr() & cacheBlockMask;
452 DynInstPtr ld_inst = loadQueue[load_idx];
454 Addr load_addr = ld_inst->physEffAddr & cacheBlockMask;
455 // Check that this snoop didn't just invalidate our lock flag
456 if (ld_inst->effAddrValid() && load_addr == invalidate_addr &&
457 ld_inst->memReqFlags & Request::LLSC)
458 TheISA::handleLockedSnoopHit(ld_inst.get());
461 // If this is the only load in the LSQ we don't care
462 if (load_idx == loadTail)
467 bool force_squash = false;
469 while (load_idx != loadTail) {
470 DynInstPtr ld_inst = loadQueue[load_idx];
472 if (!ld_inst->effAddrValid() || ld_inst->uncacheable()) {
477 Addr load_addr = ld_inst->physEffAddr & cacheBlockMask;
478 DPRINTF(LSQUnit, "-- inst [sn:%lli] load_addr: %#x to pktAddr:%#x\n",
479 ld_inst->seqNum, load_addr, invalidate_addr);
481 if (load_addr == invalidate_addr || force_squash) {
483 // If we have a TSO system, as all loads must be ordered with
484 // all other loads, this load as well as *all* subsequent loads
485 // need to be squashed to prevent possible load reordering.
488 if (ld_inst->possibleLoadViolation() || force_squash) {
489 DPRINTF(LSQUnit, "Conflicting load at addr %#x [sn:%lli]\n",
490 pkt->getAddr(), ld_inst->seqNum);
492 // Mark the load for re-execution
493 ld_inst->fault = new ReExec;
495 DPRINTF(LSQUnit, "HitExternal Snoop for addr %#x [sn:%lli]\n",
496 pkt->getAddr(), ld_inst->seqNum);
498 // Make sure that we don't lose a snoop hitting a LOCKED
499 // address since the LOCK* flags don't get updated until
501 if (ld_inst->memReqFlags & Request::LLSC)
502 TheISA::handleLockedSnoopHit(ld_inst.get());
504 // If a older load checks this and it's true
505 // then we might have missed the snoop
506 // in which case we need to invalidate to be sure
507 ld_inst->hitExternalSnoop(true);
515 template <class Impl>
517 LSQUnit<Impl>::checkViolations(int load_idx, DynInstPtr &inst)
519 Addr inst_eff_addr1 = inst->effAddr >> depCheckShift;
520 Addr inst_eff_addr2 = (inst->effAddr + inst->effSize - 1) >> depCheckShift;
522 /** @todo in theory you only need to check an instruction that has executed
523 * however, there isn't a good way in the pipeline at the moment to check
524 * all instructions that will execute before the store writes back. Thus,
525 * like the implementation that came before it, we're overly conservative.
527 while (load_idx != loadTail) {
528 DynInstPtr ld_inst = loadQueue[load_idx];
529 if (!ld_inst->effAddrValid() || ld_inst->uncacheable()) {
534 Addr ld_eff_addr1 = ld_inst->effAddr >> depCheckShift;
536 (ld_inst->effAddr + ld_inst->effSize - 1) >> depCheckShift;
538 if (inst_eff_addr2 >= ld_eff_addr1 && inst_eff_addr1 <= ld_eff_addr2) {
539 if (inst->isLoad()) {
540 // If this load is to the same block as an external snoop
541 // invalidate that we've observed then the load needs to be
542 // squashed as it could have newer data
543 if (ld_inst->hitExternalSnoop()) {
544 if (!memDepViolator ||
545 ld_inst->seqNum < memDepViolator->seqNum) {
546 DPRINTF(LSQUnit, "Detected fault with inst [sn:%lli] "
547 "and [sn:%lli] at address %#x\n",
548 inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
549 memDepViolator = ld_inst;
551 ++lsqMemOrderViolation;
553 return new GenericISA::M5PanicFault(
554 "Detected fault with inst [sn:%lli] and "
555 "[sn:%lli] at address %#x\n",
556 inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
560 // Otherwise, mark the load has a possible load violation
561 // and if we see a snoop before it's commited, we need to squash
562 ld_inst->possibleLoadViolation(true);
563 DPRINTF(LSQUnit, "Found possible load violaiton at addr: %#x"
564 " between instructions [sn:%lli] and [sn:%lli]\n",
565 inst_eff_addr1, inst->seqNum, ld_inst->seqNum);
567 // A load/store incorrectly passed this store.
568 // Check if we already have a violator, or if it's newer
569 // squash and refetch.
570 if (memDepViolator && ld_inst->seqNum > memDepViolator->seqNum)
573 DPRINTF(LSQUnit, "Detected fault with inst [sn:%lli] and "
574 "[sn:%lli] at address %#x\n",
575 inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
576 memDepViolator = ld_inst;
578 ++lsqMemOrderViolation;
580 return new GenericISA::M5PanicFault("Detected fault with "
581 "inst [sn:%lli] and [sn:%lli] at address %#x\n",
582 inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
594 template <class Impl>
596 LSQUnit<Impl>::executeLoad(DynInstPtr &inst)
598 using namespace TheISA;
599 // Execute a specific load.
600 Fault load_fault = NoFault;
602 DPRINTF(LSQUnit, "Executing load PC %s, [sn:%lli]\n",
603 inst->pcState(), inst->seqNum);
605 assert(!inst->isSquashed());
607 load_fault = inst->initiateAcc();
609 if (inst->isTranslationDelayed() &&
610 load_fault == NoFault)
613 // If the instruction faulted or predicated false, then we need to send it
614 // along to commit without the instruction completing.
615 if (load_fault != NoFault || !inst->readPredicate()) {
616 // Send this instruction to commit, also make sure iew stage
617 // realizes there is activity.
618 // Mark it as executed unless it is an uncached load that
619 // needs to hit the head of commit.
620 if (!inst->readPredicate())
621 inst->forwardOldRegs();
622 DPRINTF(LSQUnit, "Load [sn:%lli] not executed from %s\n",
624 (load_fault != NoFault ? "fault" : "predication"));
625 if (!(inst->hasRequest() && inst->uncacheable()) ||
626 inst->isAtCommit()) {
629 iewStage->instToCommit(inst);
630 iewStage->activityThisCycle();
631 } else if (!loadBlocked()) {
632 assert(inst->effAddrValid());
633 int load_idx = inst->lqIdx;
637 return checkViolations(load_idx, inst);
643 template <class Impl>
645 LSQUnit<Impl>::executeStore(DynInstPtr &store_inst)
647 using namespace TheISA;
648 // Make sure that a store exists.
651 int store_idx = store_inst->sqIdx;
653 DPRINTF(LSQUnit, "Executing store PC %s [sn:%lli]\n",
654 store_inst->pcState(), store_inst->seqNum);
656 assert(!store_inst->isSquashed());
658 // Check the recently completed loads to see if any match this store's
659 // address. If so, then we have a memory ordering violation.
660 int load_idx = store_inst->lqIdx;
662 Fault store_fault = store_inst->initiateAcc();
664 if (store_inst->isTranslationDelayed() &&
665 store_fault == NoFault)
668 if (!store_inst->readPredicate())
669 store_inst->forwardOldRegs();
671 if (storeQueue[store_idx].size == 0) {
672 DPRINTF(LSQUnit,"Fault on Store PC %s, [sn:%lli], Size = 0\n",
673 store_inst->pcState(), store_inst->seqNum);
676 } else if (!store_inst->readPredicate()) {
677 DPRINTF(LSQUnit, "Store [sn:%lli] not executed from predication\n",
682 assert(store_fault == NoFault);
684 if (store_inst->isStoreConditional()) {
685 // Store conditionals need to set themselves as able to
686 // writeback if we haven't had a fault by here.
687 storeQueue[store_idx].canWB = true;
692 return checkViolations(load_idx, store_inst);
696 template <class Impl>
698 LSQUnit<Impl>::commitLoad()
700 assert(loadQueue[loadHead]);
702 DPRINTF(LSQUnit, "Committing head load instruction, PC %s\n",
703 loadQueue[loadHead]->pcState());
705 loadQueue[loadHead] = NULL;
712 template <class Impl>
714 LSQUnit<Impl>::commitLoads(InstSeqNum &youngest_inst)
716 assert(loads == 0 || loadQueue[loadHead]);
718 while (loads != 0 && loadQueue[loadHead]->seqNum <= youngest_inst) {
723 template <class Impl>
725 LSQUnit<Impl>::commitStores(InstSeqNum &youngest_inst)
727 assert(stores == 0 || storeQueue[storeHead].inst);
729 int store_idx = storeHead;
731 while (store_idx != storeTail) {
732 assert(storeQueue[store_idx].inst);
733 // Mark any stores that are now committed and have not yet
734 // been marked as able to write back.
735 if (!storeQueue[store_idx].canWB) {
736 if (storeQueue[store_idx].inst->seqNum > youngest_inst) {
739 DPRINTF(LSQUnit, "Marking store as able to write back, PC "
741 storeQueue[store_idx].inst->pcState(),
742 storeQueue[store_idx].inst->seqNum);
744 storeQueue[store_idx].canWB = true;
749 incrStIdx(store_idx);
753 template <class Impl>
755 LSQUnit<Impl>::writebackPendingStore()
758 assert(pendingPkt != NULL);
760 // If the cache is blocked, this will store the packet for retry.
761 if (sendStore(pendingPkt)) {
762 storePostSend(pendingPkt);
765 hasPendingPkt = false;
769 template <class Impl>
771 LSQUnit<Impl>::writebackStores()
773 // First writeback the second packet from any split store that didn't
774 // complete last cycle because there weren't enough cache ports available.
775 if (TheISA::HasUnalignedMemAcc) {
776 writebackPendingStore();
779 while (storesToWB > 0 &&
780 storeWBIdx != storeTail &&
781 storeQueue[storeWBIdx].inst &&
782 storeQueue[storeWBIdx].canWB &&
783 ((!needsTSO) || (!storeInFlight)) &&
784 usedPorts < cachePorts) {
786 if (isStoreBlocked || lsq->cacheBlocked()) {
787 DPRINTF(LSQUnit, "Unable to write back any more stores, cache"
792 // Store didn't write any data so no need to write it back to
794 if (storeQueue[storeWBIdx].size == 0) {
795 completeStore(storeWBIdx);
797 incrStIdx(storeWBIdx);
804 if (storeQueue[storeWBIdx].inst->isDataPrefetch()) {
805 incrStIdx(storeWBIdx);
810 assert(storeQueue[storeWBIdx].req);
811 assert(!storeQueue[storeWBIdx].committed);
813 if (TheISA::HasUnalignedMemAcc && storeQueue[storeWBIdx].isSplit) {
814 assert(storeQueue[storeWBIdx].sreqLow);
815 assert(storeQueue[storeWBIdx].sreqHigh);
818 DynInstPtr inst = storeQueue[storeWBIdx].inst;
820 Request *req = storeQueue[storeWBIdx].req;
821 RequestPtr sreqLow = storeQueue[storeWBIdx].sreqLow;
822 RequestPtr sreqHigh = storeQueue[storeWBIdx].sreqHigh;
824 storeQueue[storeWBIdx].committed = true;
826 assert(!inst->memData);
827 inst->memData = new uint8_t[req->getSize()];
829 if (storeQueue[storeWBIdx].isAllZeros)
830 memset(inst->memData, 0, req->getSize());
832 memcpy(inst->memData, storeQueue[storeWBIdx].data, req->getSize());
835 req->isSwap() ? MemCmd::SwapReq :
836 (req->isLLSC() ? MemCmd::StoreCondReq : MemCmd::WriteReq);
838 PacketPtr snd_data_pkt = NULL;
840 LSQSenderState *state = new LSQSenderState;
841 state->isLoad = false;
842 state->idx = storeWBIdx;
845 if (!TheISA::HasUnalignedMemAcc || !storeQueue[storeWBIdx].isSplit) {
847 // Build a single data packet if the store isn't split.
848 data_pkt = new Packet(req, command);
849 data_pkt->dataStatic(inst->memData);
850 data_pkt->senderState = state;
852 // Create two packets if the store is split in two.
853 data_pkt = new Packet(sreqLow, command);
854 snd_data_pkt = new Packet(sreqHigh, command);
856 data_pkt->dataStatic(inst->memData);
857 snd_data_pkt->dataStatic(inst->memData + sreqLow->getSize());
859 data_pkt->senderState = state;
860 snd_data_pkt->senderState = state;
862 state->isSplit = true;
863 state->outstanding = 2;
865 // Can delete the main request now.
870 DPRINTF(LSQUnit, "D-Cache: Writing back store idx:%i PC:%s "
871 "to Addr:%#x, data:%#x [sn:%lli]\n",
872 storeWBIdx, inst->pcState(),
873 req->getPaddr(), (int)*(inst->memData),
876 // @todo: Remove this SC hack once the memory system handles it.
877 if (inst->isStoreConditional()) {
878 assert(!storeQueue[storeWBIdx].isSplit);
879 // Disable recording the result temporarily. Writing to
880 // misc regs normally updates the result, but this is not
881 // the desired behavior when handling store conditionals.
882 inst->recordResult(false);
883 bool success = TheISA::handleLockedWrite(inst.get(), req, cacheBlockMask);
884 inst->recordResult(true);
887 // Instantly complete this store.
888 DPRINTF(LSQUnit, "Store conditional [sn:%lli] failed. "
889 "Instantly completing it.\n",
891 WritebackEvent *wb = new WritebackEvent(inst, data_pkt, this);
892 cpu->schedule(wb, curTick() + 1);
894 // Make sure to set the LLSC data for verification
895 // if checker is loaded
896 inst->reqToVerify->setExtraData(0);
897 inst->completeAcc(data_pkt);
899 completeStore(storeWBIdx);
900 incrStIdx(storeWBIdx);
904 // Non-store conditionals do not need a writeback.
909 TheISA::HasUnalignedMemAcc && storeQueue[storeWBIdx].isSplit;
911 ThreadContext *thread = cpu->tcBase(lsqID);
913 if (req->isMmappedIpr()) {
914 assert(!inst->isStoreConditional());
915 TheISA::handleIprWrite(thread, data_pkt);
918 assert(snd_data_pkt->req->isMmappedIpr());
919 TheISA::handleIprWrite(thread, snd_data_pkt);
926 completeStore(storeWBIdx);
927 incrStIdx(storeWBIdx);
928 } else if (!sendStore(data_pkt)) {
929 DPRINTF(IEW, "D-Cache became blocked when writing [sn:%lli], will"
933 // Need to store the second packet, if split.
935 state->pktToSend = true;
936 state->pendingPacket = snd_data_pkt;
940 // If split, try to send the second packet too
942 assert(snd_data_pkt);
944 // Ensure there are enough ports to use.
945 if (usedPorts < cachePorts) {
947 if (sendStore(snd_data_pkt)) {
948 storePostSend(snd_data_pkt);
950 DPRINTF(IEW, "D-Cache became blocked when writing"
951 " [sn:%lli] second packet, will retry later\n",
956 // Store the packet for when there's free ports.
957 assert(pendingPkt == NULL);
958 pendingPkt = snd_data_pkt;
959 hasPendingPkt = true;
963 // Not a split store.
964 storePostSend(data_pkt);
969 // Not sure this should set it to 0.
972 assert(stores >= 0 && storesToWB >= 0);
975 /*template <class Impl>
977 LSQUnit<Impl>::removeMSHR(InstSeqNum seqNum)
979 list<InstSeqNum>::iterator mshr_it = find(mshrSeqNums.begin(),
983 if (mshr_it != mshrSeqNums.end()) {
984 mshrSeqNums.erase(mshr_it);
985 DPRINTF(LSQUnit, "Removing MSHR. count = %i\n",mshrSeqNums.size());
989 template <class Impl>
991 LSQUnit<Impl>::squash(const InstSeqNum &squashed_num)
993 DPRINTF(LSQUnit, "Squashing until [sn:%lli]!"
994 "(Loads:%i Stores:%i)\n", squashed_num, loads, stores);
996 int load_idx = loadTail;
999 while (loads != 0 && loadQueue[load_idx]->seqNum > squashed_num) {
1000 DPRINTF(LSQUnit,"Load Instruction PC %s squashed, "
1002 loadQueue[load_idx]->pcState(),
1003 loadQueue[load_idx]->seqNum);
1005 if (isStalled() && load_idx == stallingLoadIdx) {
1007 stallingStoreIsn = 0;
1008 stallingLoadIdx = 0;
1011 // Clear the smart pointer to make sure it is decremented.
1012 loadQueue[load_idx]->setSquashed();
1013 loadQueue[load_idx] = NULL;
1017 loadTail = load_idx;
1019 decrLdIdx(load_idx);
1023 if (isLoadBlocked) {
1024 if (squashed_num < blockedLoadSeqNum) {
1025 isLoadBlocked = false;
1026 loadBlockedHandled = false;
1027 blockedLoadSeqNum = 0;
1031 if (memDepViolator && squashed_num < memDepViolator->seqNum) {
1032 memDepViolator = NULL;
1035 int store_idx = storeTail;
1036 decrStIdx(store_idx);
1038 while (stores != 0 &&
1039 storeQueue[store_idx].inst->seqNum > squashed_num) {
1040 // Instructions marked as can WB are already committed.
1041 if (storeQueue[store_idx].canWB) {
1045 DPRINTF(LSQUnit,"Store Instruction PC %s squashed, "
1046 "idx:%i [sn:%lli]\n",
1047 storeQueue[store_idx].inst->pcState(),
1048 store_idx, storeQueue[store_idx].inst->seqNum);
1050 // I don't think this can happen. It should have been cleared
1051 // by the stalling load.
1053 storeQueue[store_idx].inst->seqNum == stallingStoreIsn) {
1054 panic("Is stalled should have been cleared by stalling load!\n");
1056 stallingStoreIsn = 0;
1059 // Clear the smart pointer to make sure it is decremented.
1060 storeQueue[store_idx].inst->setSquashed();
1061 storeQueue[store_idx].inst = NULL;
1062 storeQueue[store_idx].canWB = 0;
1064 // Must delete request now that it wasn't handed off to
1065 // memory. This is quite ugly. @todo: Figure out the proper
1066 // place to really handle request deletes.
1067 delete storeQueue[store_idx].req;
1068 if (TheISA::HasUnalignedMemAcc && storeQueue[store_idx].isSplit) {
1069 delete storeQueue[store_idx].sreqLow;
1070 delete storeQueue[store_idx].sreqHigh;
1072 storeQueue[store_idx].sreqLow = NULL;
1073 storeQueue[store_idx].sreqHigh = NULL;
1076 storeQueue[store_idx].req = NULL;
1080 storeTail = store_idx;
1082 decrStIdx(store_idx);
1083 ++lsqSquashedStores;
1087 template <class Impl>
1089 LSQUnit<Impl>::storePostSend(PacketPtr pkt)
1092 storeQueue[storeWBIdx].inst->seqNum == stallingStoreIsn) {
1093 DPRINTF(LSQUnit, "Unstalling, stalling store [sn:%lli] "
1095 stallingStoreIsn, stallingLoadIdx);
1097 stallingStoreIsn = 0;
1098 iewStage->replayMemInst(loadQueue[stallingLoadIdx]);
1101 if (!storeQueue[storeWBIdx].inst->isStoreConditional()) {
1102 // The store is basically completed at this time. This
1103 // only works so long as the checker doesn't try to
1104 // verify the value in memory for stores.
1105 storeQueue[storeWBIdx].inst->setCompleted();
1108 cpu->checker->verify(storeQueue[storeWBIdx].inst);
1113 storeInFlight = true;
1116 incrStIdx(storeWBIdx);
1119 template <class Impl>
1121 LSQUnit<Impl>::writeback(DynInstPtr &inst, PacketPtr pkt)
1123 iewStage->wakeCPU();
1125 // Squashed instructions do not need to complete their access.
1126 if (inst->isSquashed()) {
1127 iewStage->decrWb(inst->seqNum);
1128 assert(!inst->isStore());
1129 ++lsqIgnoredResponses;
1133 if (!inst->isExecuted()) {
1134 inst->setExecuted();
1136 // Complete access to copy data to proper place.
1137 inst->completeAcc(pkt);
1140 // Need to insert instruction into queue to commit
1141 iewStage->instToCommit(inst);
1143 iewStage->activityThisCycle();
1145 // see if this load changed the PC
1146 iewStage->checkMisprediction(inst);
1149 template <class Impl>
1151 LSQUnit<Impl>::completeStore(int store_idx)
1153 assert(storeQueue[store_idx].inst);
1154 storeQueue[store_idx].completed = true;
1156 // A bit conservative because a store completion may not free up entries,
1157 // but hopefully avoids two store completions in one cycle from making
1158 // the CPU tick twice.
1160 cpu->activityThisCycle();
1162 if (store_idx == storeHead) {
1164 incrStIdx(storeHead);
1167 } while (storeQueue[storeHead].completed &&
1168 storeHead != storeTail);
1170 iewStage->updateLSQNextCycle = true;
1173 DPRINTF(LSQUnit, "Completing store [sn:%lli], idx:%i, store head "
1175 storeQueue[store_idx].inst->seqNum, store_idx, storeHead);
1178 if (DTRACE(O3PipeView)) {
1179 storeQueue[store_idx].inst->storeTick =
1180 curTick() - storeQueue[store_idx].inst->fetchTick;
1185 storeQueue[store_idx].inst->seqNum == stallingStoreIsn) {
1186 DPRINTF(LSQUnit, "Unstalling, stalling store [sn:%lli] "
1188 stallingStoreIsn, stallingLoadIdx);
1190 stallingStoreIsn = 0;
1191 iewStage->replayMemInst(loadQueue[stallingLoadIdx]);
1194 storeQueue[store_idx].inst->setCompleted();
1197 storeInFlight = false;
1200 // Tell the checker we've completed this instruction. Some stores
1201 // may get reported twice to the checker, but the checker can
1202 // handle that case.
1204 cpu->checker->verify(storeQueue[store_idx].inst);
1208 template <class Impl>
1210 LSQUnit<Impl>::sendStore(PacketPtr data_pkt)
1212 if (!dcachePort->sendTimingReq(data_pkt)) {
1213 // Need to handle becoming blocked on a store.
1214 isStoreBlocked = true;
1216 assert(retryPkt == NULL);
1217 retryPkt = data_pkt;
1218 lsq->setRetryTid(lsqID);
1224 template <class Impl>
1226 LSQUnit<Impl>::recvRetry()
1228 if (isStoreBlocked) {
1229 DPRINTF(LSQUnit, "Receiving retry: store blocked\n");
1230 assert(retryPkt != NULL);
1232 LSQSenderState *state =
1233 dynamic_cast<LSQSenderState *>(retryPkt->senderState);
1235 if (dcachePort->sendTimingReq(retryPkt)) {
1236 // Don't finish the store unless this is the last packet.
1237 if (!TheISA::HasUnalignedMemAcc || !state->pktToSend ||
1238 state->pendingPacket == retryPkt) {
1239 state->pktToSend = false;
1240 storePostSend(retryPkt);
1243 isStoreBlocked = false;
1244 lsq->setRetryTid(InvalidThreadID);
1246 // Send any outstanding packet.
1247 if (TheISA::HasUnalignedMemAcc && state->pktToSend) {
1248 assert(state->pendingPacket);
1249 if (sendStore(state->pendingPacket)) {
1250 storePostSend(state->pendingPacket);
1256 lsq->setRetryTid(lsqID);
1258 } else if (isLoadBlocked) {
1259 DPRINTF(LSQUnit, "Loads squash themselves and all younger insts, "
1260 "no need to resend packet.\n");
1262 DPRINTF(LSQUnit, "Retry received but LSQ is no longer blocked.\n");
1266 template <class Impl>
1268 LSQUnit<Impl>::incrStIdx(int &store_idx) const
1270 if (++store_idx >= SQEntries)
1274 template <class Impl>
1276 LSQUnit<Impl>::decrStIdx(int &store_idx) const
1278 if (--store_idx < 0)
1279 store_idx += SQEntries;
1282 template <class Impl>
1284 LSQUnit<Impl>::incrLdIdx(int &load_idx) const
1286 if (++load_idx >= LQEntries)
1290 template <class Impl>
1292 LSQUnit<Impl>::decrLdIdx(int &load_idx) const
1295 load_idx += LQEntries;
1298 template <class Impl>
1300 LSQUnit<Impl>::dumpInsts() const
1302 cprintf("Load store queue: Dumping instructions.\n");
1303 cprintf("Load queue size: %i\n", loads);
1304 cprintf("Load queue: ");
1306 int load_idx = loadHead;
1308 while (load_idx != loadTail && loadQueue[load_idx]) {
1309 const DynInstPtr &inst(loadQueue[load_idx]);
1310 cprintf("%s.[sn:%i] ", inst->pcState(), inst->seqNum);
1312 incrLdIdx(load_idx);
1316 cprintf("Store queue size: %i\n", stores);
1317 cprintf("Store queue: ");
1319 int store_idx = storeHead;
1321 while (store_idx != storeTail && storeQueue[store_idx].inst) {
1322 const DynInstPtr &inst(storeQueue[store_idx].inst);
1323 cprintf("%s.[sn:%i] ", inst->pcState(), inst->seqNum);
1325 incrStIdx(store_idx);
1331 #endif//__CPU_O3_LSQ_UNIT_IMPL_HH__