2 * Copyright (c) 2006 The Regents of The University of Michigan
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.
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.
31 #include "config/use_checker.hh"
33 #include "arch/isa_traits.hh"
34 #include "base/str.hh"
35 #include "cpu/ozone/lw_lsq.hh"
36 #include "cpu/checker/cpu.hh"
39 OzoneLWLSQ<Impl>::WritebackEvent::WritebackEvent(DynInstPtr &_inst, PacketPtr _pkt,
41 : Event(&mainEventQueue), inst(_inst), pkt(_pkt), lsqPtr(lsq_ptr)
43 this->setFlags(Event::AutoDelete);
48 OzoneLWLSQ<Impl>::WritebackEvent::process()
50 if (!lsqPtr->isSwitchedOut()) {
51 lsqPtr->writeback(inst, pkt);
58 OzoneLWLSQ<Impl>::WritebackEvent::description()
60 return "Store writeback event";
65 OzoneLWLSQ<Impl>::DcachePort::recvAtomic(PacketPtr pkt)
67 panic("O3CPU model does not work with atomic mode!");
73 OzoneLWLSQ<Impl>::DcachePort::recvFunctional(PacketPtr pkt)
75 panic("O3CPU doesn't expect recvFunctional callback!");
80 OzoneLWLSQ<Impl>::DcachePort::recvStatusChange(Status status)
82 if (status == RangeChange)
85 panic("O3CPU doesn't expect recvStatusChange callback!");
90 OzoneLWLSQ<Impl>::DcachePort::recvTiming(PacketPtr pkt)
92 lsq->completeDataAccess(pkt);
98 OzoneLWLSQ<Impl>::DcachePort::recvRetry()
105 OzoneLWLSQ<Impl>::completeDataAccess(PacketPtr pkt)
107 LSQSenderState *state = dynamic_cast<LSQSenderState *>(pkt->senderState);
108 DynInstPtr inst = state->inst;
109 DPRINTF(IEW, "Writeback event [sn:%lli]\n", inst->seqNum);
110 DPRINTF(Activity, "Activity: Writeback event [sn:%lli]\n", inst->seqNum);
112 //iewStage->ldstQueue.removeMSHR(inst->threadNumber,inst->seqNum);
114 if (isSwitchedOut() || inst->isSquashed()) {
120 writeback(inst, pkt);
123 if (inst->isStore()) {
124 completeStore(state->idx);
132 template <class Impl>
133 OzoneLWLSQ<Impl>::OzoneLWLSQ()
134 : switchedOut(false), dcachePort(this), loads(0), stores(0),
135 storesToWB(0), stalled(false), isStoreBlocked(false),
136 isLoadBlocked(false), loadBlockedHandled(false)
142 OzoneLWLSQ<Impl>::init(Params *params, unsigned maxLQEntries,
143 unsigned maxSQEntries, unsigned id)
145 DPRINTF(OzoneLSQ, "Creating OzoneLWLSQ%i object.\n",id);
149 LQEntries = maxLQEntries;
150 SQEntries = maxSQEntries;
152 for (int i = 0; i < LQEntries * 2; i++) {
160 cachePorts = params->cachePorts;
162 loadFaultInst = storeFaultInst = memDepViolator = NULL;
164 blockedLoadSeqNum = 0;
169 OzoneLWLSQ<Impl>::name() const
176 OzoneLWLSQ<Impl>::setCPU(OzoneCPU *cpu_ptr)
179 dcachePort.setName(this->name() + "-dport");
183 cpu->checker->setDcachePort(&dcachePort);
190 OzoneLWLSQ<Impl>::clearLQ()
197 OzoneLWLSQ<Impl>::clearSQ()
204 OzoneLWLSQ<Impl>::setPageTable(PageTable *pt_ptr)
206 DPRINTF(OzoneLSQ, "Setting the page table pointer.\n");
212 OzoneLWLSQ<Impl>::resizeLQ(unsigned size)
214 assert( size >= LQEntries);
216 if (size > LQEntries) {
217 while (size > loadQueue.size()) {
219 loadQueue.push_back(dummy);
230 OzoneLWLSQ<Impl>::resizeSQ(unsigned size)
232 if (size > SQEntries) {
233 while (size > storeQueue.size()) {
235 storeQueue.push_back(dummy);
243 template <class Impl>
245 OzoneLWLSQ<Impl>::insert(DynInstPtr &inst)
247 // Make sure we really have a memory reference.
248 assert(inst->isMemRef());
250 // Make sure it's one of the two classes of memory references.
251 assert(inst->isLoad() || inst->isStore());
253 if (inst->isLoad()) {
260 template <class Impl>
262 OzoneLWLSQ<Impl>::insertLoad(DynInstPtr &load_inst)
264 assert(loads < LQEntries * 2);
265 assert(!LQIndices.empty());
266 int load_index = LQIndices.front();
269 DPRINTF(OzoneLSQ, "Inserting load PC %#x, idx:%i [sn:%lli]\n",
270 load_inst->readPC(), load_index, load_inst->seqNum);
272 load_inst->lqIdx = load_index;
274 loadQueue.push_front(load_inst);
275 LQItHash[load_index] = loadQueue.begin();
280 template <class Impl>
282 OzoneLWLSQ<Impl>::insertStore(DynInstPtr &store_inst)
284 // Make sure it is not full before inserting an instruction.
285 assert(stores - storesToWB < SQEntries);
287 assert(!SQIndices.empty());
288 int store_index = SQIndices.front();
291 DPRINTF(OzoneLSQ, "Inserting store PC %#x, idx:%i [sn:%lli]\n",
292 store_inst->readPC(), store_index, store_inst->seqNum);
294 store_inst->sqIdx = store_index;
295 SQEntry entry(store_inst);
296 if (loadQueue.empty()) {
297 entry.lqIt = loadQueue.end();
299 entry.lqIt = loadQueue.begin();
301 storeQueue.push_front(entry);
303 SQItHash[store_index] = storeQueue.begin();
308 template <class Impl>
309 typename Impl::DynInstPtr
310 OzoneLWLSQ<Impl>::getMemDepViolator()
312 DynInstPtr temp = memDepViolator;
314 memDepViolator = NULL;
319 template <class Impl>
321 OzoneLWLSQ<Impl>::numFreeEntries()
323 unsigned free_lq_entries = LQEntries - loads;
324 unsigned free_sq_entries = SQEntries - stores;
326 // Both the LQ and SQ entries have an extra dummy entry to differentiate
327 // empty/full conditions. Subtract 1 from the free entries.
328 if (free_lq_entries < free_sq_entries) {
329 return free_lq_entries - 1;
331 return free_sq_entries - 1;
335 template <class Impl>
337 OzoneLWLSQ<Impl>::numLoadsReady()
340 LQIt lq_it = loadQueue.begin();
341 LQIt end_it = loadQueue.end();
343 while (lq_it != end_it) {
344 if ((*lq_it)->readyToIssue()) {
352 template <class Impl>
354 OzoneLWLSQ<Impl>::executeLoad(DynInstPtr &inst)
356 // Execute a specific load.
357 Fault load_fault = NoFault;
359 DPRINTF(OzoneLSQ, "Executing load PC %#x, [sn:%lli]\n",
360 inst->readPC(),inst->seqNum);
362 // Make sure it's really in the list.
363 // Normally it should always be in the list. However,
364 /* due to a syscall it may not be the list.
368 if (i == loadTail && !find(inst)) {
369 assert(0 && "Load not in the queue!");
370 } else if (loadQueue[i] == inst) {
375 if (i >= LQEntries) {
381 load_fault = inst->initiateAcc();
383 // Might want to make sure that I'm not overwriting a previously faulting
384 // instruction that hasn't been checked yet.
385 // Actually probably want the oldest faulting load
386 if (load_fault != NoFault) {
387 DPRINTF(OzoneLSQ, "Load [sn:%lli] has a fault\n", inst->seqNum);
388 // Maybe just set it as can commit here, although that might cause
389 // some other problems with sending traps to the ROB too quickly.
390 be->instToCommit(inst);
391 // iewStage->activityThisCycle();
397 template <class Impl>
399 OzoneLWLSQ<Impl>::executeStore(DynInstPtr &store_inst)
401 // Make sure that a store exists.
404 int store_idx = store_inst->sqIdx;
405 SQHashIt sq_hash_it = SQItHash.find(store_idx);
406 assert(sq_hash_it != SQItHash.end());
407 DPRINTF(OzoneLSQ, "Executing store PC %#x [sn:%lli]\n",
408 store_inst->readPC(), store_inst->seqNum);
410 SQIt sq_it = (*sq_hash_it).second;
412 Fault store_fault = store_inst->initiateAcc();
414 // Store size should now be available. Use it to get proper offset for
416 int size = (*sq_it).size;
419 DPRINTF(OzoneLSQ,"Fault on Store PC %#x, [sn:%lli],Size = 0\n",
420 store_inst->readPC(),store_inst->seqNum);
425 assert(store_fault == NoFault);
427 if (!storeFaultInst) {
428 if (store_fault != NoFault) {
429 panic("Fault in a store instruction!");
430 storeFaultInst = store_inst;
431 } else if (store_inst->isStoreConditional()) {
432 // Store conditionals need to set themselves as able to
433 // writeback if we haven't had a fault by here.
434 (*sq_it).canWB = true;
437 DPRINTF(OzoneLSQ, "Nonspeculative store! storesToWB:%i\n",
442 LQIt lq_it = --(loadQueue.end());
444 if (!memDepViolator) {
445 while (lq_it != loadQueue.end()) {
446 if ((*lq_it)->seqNum < store_inst->seqNum) {
450 // Actually should only check loads that have actually executed
451 // Might be safe because effAddr is set to InvalAddr when the
452 // dyn inst is created.
454 // Must actually check all addrs in the proper size range
455 // Which is more correct than needs to be. What if for now we just
456 // assume all loads are quad-word loads, and do the addr based
458 // @todo: Fix this, magic number being used here
459 if (((*lq_it)->effAddr >> 8) ==
460 (store_inst->effAddr >> 8)) {
461 // A load incorrectly passed this store. Squash and refetch.
462 // For now return a fault to show that it was unsuccessful.
463 memDepViolator = (*lq_it);
465 return TheISA::genMachineCheckFault();
471 // If we've reached this point, there was no violation.
472 memDepViolator = NULL;
478 template <class Impl>
480 OzoneLWLSQ<Impl>::commitLoad()
482 assert(!loadQueue.empty());
484 DPRINTF(OzoneLSQ, "[sn:%lli] Committing head load instruction, PC %#x\n",
485 loadQueue.back()->seqNum, loadQueue.back()->readPC());
487 LQIndices.push(loadQueue.back()->lqIdx);
488 LQItHash.erase(loadQueue.back()->lqIdx);
490 loadQueue.pop_back();
495 template <class Impl>
497 OzoneLWLSQ<Impl>::commitLoads(InstSeqNum &youngest_inst)
499 assert(loads == 0 || !loadQueue.empty());
502 loadQueue.back()->seqNum <= youngest_inst) {
507 template <class Impl>
509 OzoneLWLSQ<Impl>::commitStores(InstSeqNum &youngest_inst)
511 assert(stores == 0 || !storeQueue.empty());
513 SQIt sq_it = --(storeQueue.end());
514 while (!storeQueue.empty() && sq_it != storeQueue.end()) {
515 assert((*sq_it).inst);
516 if (!(*sq_it).canWB) {
517 if ((*sq_it).inst->seqNum > youngest_inst) {
522 DPRINTF(OzoneLSQ, "Marking store as able to write back, PC "
523 "%#x [sn:%lli], storesToWB:%i\n",
524 (*sq_it).inst->readPC(),
525 (*sq_it).inst->seqNum,
528 (*sq_it).canWB = true;
535 template <class Impl>
537 OzoneLWLSQ<Impl>::writebackStores()
539 SQIt sq_it = --(storeQueue.end());
540 while (storesToWB > 0 &&
541 sq_it != storeQueue.end() &&
544 usedPorts < cachePorts) {
546 if (isStoreBlocked) {
547 DPRINTF(OzoneLSQ, "Unable to write back any more stores, cache"
552 DynInstPtr inst = (*sq_it).inst;
554 if ((*sq_it).size == 0 && !(*sq_it).completed) {
556 completeStore(inst->sqIdx);
561 if (inst->isDataPrefetch() || (*sq_it).committed) {
568 assert((*sq_it).req);
569 assert(!(*sq_it).committed);
571 Request *req = (*sq_it).req;
572 (*sq_it).committed = true;
574 assert(!inst->memData);
575 inst->memData = new uint8_t[64];
576 memcpy(inst->memData, (uint8_t *)&(*sq_it).data,
579 PacketPtr data_pkt = new Packet(req, Packet::WriteReq, Packet::Broadcast);
580 data_pkt->dataStatic(inst->memData);
582 LSQSenderState *state = new LSQSenderState;
583 state->isLoad = false;
584 state->idx = inst->sqIdx;
586 data_pkt->senderState = state;
588 DPRINTF(OzoneLSQ, "D-Cache: Writing back store PC:%#x "
589 "to Addr:%#x, data:%#x [sn:%lli]\n",
590 (*sq_it).inst->readPC(),
591 req->getPaddr(), *(inst->memData),
594 // @todo: Remove this SC hack once the memory system handles it.
595 if (req->getFlags() & LOCKED) {
596 if (req->getFlags() & UNCACHEABLE) {
603 // Hack: Instantly complete this store.
604 completeDataAccess(data_pkt);
610 // Non-store conditionals do not need a writeback.
614 if (!dcachePort.sendTiming(data_pkt)) {
615 // Need to handle becoming blocked on a store.
616 isStoreBlocked = true;
617 assert(retryPkt == NULL);
620 storePostSend(data_pkt, inst);
624 DPRINTF(OzoneLSQ, "D-Cache: Writing back store idx:%i PC:%#x "
625 "to Addr:%#x, data:%#x [sn:%lli]\n",
626 inst->sqIdx,inst->readPC(),
627 req->paddr, *(req->data),
630 if (dcacheInterface) {
631 assert(!req->completionEvent);
632 StoreCompletionEvent *store_event = new
633 StoreCompletionEvent(inst, be, NULL, this);
634 req->completionEvent = store_event;
636 MemAccessResult result = dcacheInterface->access(req);
639 inst->seqNum == stallingStoreIsn) {
640 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
642 stallingStoreIsn, (*stallingLoad)->seqNum);
644 stallingStoreIsn = 0;
645 be->replayMemInst((*stallingLoad));
648 if (result != MA_HIT && dcacheInterface->doEvents()) {
649 store_event->miss = true;
650 typename BackEnd::LdWritebackEvent *wb = NULL;
651 if (req->flags & LOCKED) {
652 wb = new typename BackEnd::LdWritebackEvent(inst,
654 store_event->wbEvent = wb;
657 DPRINTF(OzoneLSQ,"D-Cache Write Miss!\n");
659 // DPRINTF(Activity, "Active st accessing mem miss [sn:%lli]\n",
662 be->addDcacheMiss(inst);
664 lastDcacheStall = curTick;
666 _status = DcacheMissStall;
668 // Increment stat here or something
672 DPRINTF(OzoneLSQ,"D-Cache: Write Hit on idx:%i !\n",
675 // DPRINTF(Activity, "Active st accessing mem hit [sn:%lli]\n",
678 if (req->flags & LOCKED) {
679 // Stx_C does not generate a system port
680 // transaction in the 21264, but that might be
681 // hard to accomplish in this model.
683 typename BackEnd::LdWritebackEvent *wb =
684 new typename BackEnd::LdWritebackEvent(inst,
686 store_event->wbEvent = wb;
691 panic("Must HAVE DCACHE!!!!!\n");
696 // Not sure this should set it to 0.
699 assert(stores >= 0 && storesToWB >= 0);
702 template <class Impl>
704 OzoneLWLSQ<Impl>::squash(const InstSeqNum &squashed_num)
706 DPRINTF(OzoneLSQ, "Squashing until [sn:%lli]!"
707 "(Loads:%i Stores:%i)\n",squashed_num,loads,stores);
710 LQIt lq_it = loadQueue.begin();
712 while (loads != 0 && (*lq_it)->seqNum > squashed_num) {
713 assert(!loadQueue.empty());
714 // Clear the smart pointer to make sure it is decremented.
715 DPRINTF(OzoneLSQ,"Load Instruction PC %#x squashed, "
720 if (isStalled() && lq_it == stallingLoad) {
722 stallingStoreIsn = 0;
729 LQHashIt lq_hash_it = LQItHash.find((*lq_it)->lqIdx);
730 assert(lq_hash_it != LQItHash.end());
731 LQItHash.erase(lq_hash_it);
732 LQIndices.push((*lq_it)->lqIdx);
733 loadQueue.erase(lq_it++);
737 if (squashed_num < blockedLoadSeqNum) {
738 isLoadBlocked = false;
739 loadBlockedHandled = false;
740 blockedLoadSeqNum = 0;
744 SQIt sq_it = storeQueue.begin();
746 while (stores != 0 && (*sq_it).inst->seqNum > squashed_num) {
747 assert(!storeQueue.empty());
749 if ((*sq_it).canWB) {
753 // Clear the smart pointer to make sure it is decremented.
754 DPRINTF(OzoneLSQ,"Store Instruction PC %#x idx:%i squashed [sn:%lli]\n",
755 (*sq_it).inst->readPC(), (*sq_it).inst->sqIdx,
756 (*sq_it).inst->seqNum);
758 // I don't think this can happen. It should have been cleared by the
761 (*sq_it).inst->seqNum == stallingStoreIsn) {
762 panic("Is stalled should have been cleared by stalling load!\n");
764 stallingStoreIsn = 0;
767 SQHashIt sq_hash_it = SQItHash.find((*sq_it).inst->sqIdx);
768 assert(sq_hash_it != SQItHash.end());
769 SQItHash.erase(sq_hash_it);
770 SQIndices.push((*sq_it).inst->sqIdx);
771 (*sq_it).inst = NULL;
775 storeQueue.erase(sq_it++);
779 template <class Impl>
781 OzoneLWLSQ<Impl>::dumpInsts()
783 cprintf("Load store queue: Dumping instructions.\n");
784 cprintf("Load queue size: %i\n", loads);
785 cprintf("Load queue: ");
787 LQIt lq_it = --(loadQueue.end());
789 while (lq_it != loadQueue.end() && (*lq_it)) {
790 cprintf("[sn:%lli] %#x ", (*lq_it)->seqNum,
796 cprintf("\nStore queue size: %i\n", stores);
797 cprintf("Store queue: ");
799 SQIt sq_it = --(storeQueue.end());
801 while (sq_it != storeQueue.end() && (*sq_it).inst) {
802 cprintf("[sn:%lli]\nPC:%#x\nSize:%i\nCommitted:%i\nCompleted:%i\ncanWB:%i\n",
803 (*sq_it).inst->seqNum,
804 (*sq_it).inst->readPC(),
816 template <class Impl>
818 OzoneLWLSQ<Impl>::storePostSend(Packet *pkt, DynInstPtr &inst)
821 inst->seqNum == stallingStoreIsn) {
822 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
824 stallingStoreIsn, (*stallingLoad)->seqNum);
826 stallingStoreIsn = 0;
827 be->replayMemInst((*stallingLoad));
830 if (!inst->isStoreConditional()) {
831 // The store is basically completed at this time. This
832 // only works so long as the checker doesn't try to
833 // verify the value in memory for stores.
834 inst->setCompleted();
837 cpu->checker->verify(inst);
842 if (pkt->result != Packet::Success) {
843 DPRINTF(OzoneLSQ,"D-Cache Write Miss!\n");
845 DPRINTF(Activity, "Active st accessing mem miss [sn:%lli]\n",
848 //mshrSeqNums.push_back(storeQueue[storeWBIdx].inst->seqNum);
850 //DPRINTF(OzoneLWLSQ, "Added MSHR. count = %i\n",mshrSeqNums.size());
852 // @todo: Increment stat here.
854 DPRINTF(OzoneLSQ,"D-Cache: Write Hit!\n");
856 DPRINTF(Activity, "Active st accessing mem hit [sn:%lli]\n",
861 template <class Impl>
863 OzoneLWLSQ<Impl>::writeback(DynInstPtr &inst, PacketPtr pkt)
865 // Squashed instructions do not need to complete their access.
866 if (inst->isSquashed()) {
867 assert(!inst->isStore());
871 if (!inst->isExecuted()) {
874 // Complete access to copy data to proper place.
875 inst->completeAcc(pkt);
878 // Need to insert instruction into queue to commit
879 be->instToCommit(inst);
882 template <class Impl>
884 OzoneLWLSQ<Impl>::completeStore(int store_idx)
886 SQHashIt sq_hash_it = SQItHash.find(store_idx);
887 assert(sq_hash_it != SQItHash.end());
888 SQIt sq_it = (*sq_hash_it).second;
890 assert((*sq_it).inst);
891 (*sq_it).completed = true;
892 DynInstPtr inst = (*sq_it).inst;
897 inst->seqNum == stallingStoreIsn) {
898 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
900 stallingStoreIsn, (*stallingLoad)->seqNum);
902 stallingStoreIsn = 0;
903 be->replayMemInst((*stallingLoad));
906 DPRINTF(OzoneLSQ, "Completing store idx:%i [sn:%lli], storesToWB:%i\n",
907 inst->sqIdx, inst->seqNum, storesToWB);
909 assert(!storeQueue.empty());
910 SQItHash.erase(sq_hash_it);
911 SQIndices.push(inst->sqIdx);
912 storeQueue.erase(sq_it);
915 inst->setCompleted();
918 cpu->checker->verify(inst);
923 template <class Impl>
925 OzoneLWLSQ<Impl>::recvRetry()
927 panic("Unimplemented!");
930 template <class Impl>
932 OzoneLWLSQ<Impl>::switchOut()
934 assert(storesToWB == 0);
937 // Clear the queue to free up resources
940 loads = stores = storesToWB = 0;
943 template <class Impl>
945 OzoneLWLSQ<Impl>::takeOverFrom(ThreadContext *old_tc)
947 // Clear out any old state. May be redundant if this is the first time
948 // the CPU is being used.
950 isLoadBlocked = false;
951 loadBlockedHandled = false;
954 // Could do simple checks here to see if indices are on twice
955 while (!LQIndices.empty())
957 while (!SQIndices.empty())
960 for (int i = 0; i < LQEntries * 2; i++) {
967 loadFaultInst = storeFaultInst = memDepViolator = NULL;
969 blockedLoadSeqNum = 0;