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), loads(0), stores(0), storesToWB(0), stalled(false),
135 isStoreBlocked(false), isLoadBlocked(false), loadBlockedHandled(false)
141 OzoneLWLSQ<Impl>::init(Params *params, unsigned maxLQEntries,
142 unsigned maxSQEntries, unsigned id)
144 DPRINTF(OzoneLSQ, "Creating OzoneLWLSQ%i object.\n",id);
148 LQEntries = maxLQEntries;
149 SQEntries = maxSQEntries;
151 for (int i = 0; i < LQEntries * 2; i++) {
159 cachePorts = params->cachePorts;
161 loadFaultInst = storeFaultInst = memDepViolator = NULL;
163 blockedLoadSeqNum = 0;
168 OzoneLWLSQ<Impl>::name() const
175 OzoneLWLSQ<Impl>::setCPU(OzoneCPU *cpu_ptr)
178 dcachePort = new DcachePort(cpu, this);
180 Port *mem_dport = mem->getPort("");
181 dcachePort->setPeer(mem_dport);
182 mem_dport->setPeer(dcachePort);
186 cpu->checker->setDcachePort(dcachePort);
193 OzoneLWLSQ<Impl>::clearLQ()
200 OzoneLWLSQ<Impl>::clearSQ()
207 OzoneLWLSQ<Impl>::setPageTable(PageTable *pt_ptr)
209 DPRINTF(OzoneLSQ, "Setting the page table pointer.\n");
215 OzoneLWLSQ<Impl>::resizeLQ(unsigned size)
217 assert( size >= LQEntries);
219 if (size > LQEntries) {
220 while (size > loadQueue.size()) {
222 loadQueue.push_back(dummy);
233 OzoneLWLSQ<Impl>::resizeSQ(unsigned size)
235 if (size > SQEntries) {
236 while (size > storeQueue.size()) {
238 storeQueue.push_back(dummy);
246 template <class Impl>
248 OzoneLWLSQ<Impl>::insert(DynInstPtr &inst)
250 // Make sure we really have a memory reference.
251 assert(inst->isMemRef());
253 // Make sure it's one of the two classes of memory references.
254 assert(inst->isLoad() || inst->isStore());
256 if (inst->isLoad()) {
263 template <class Impl>
265 OzoneLWLSQ<Impl>::insertLoad(DynInstPtr &load_inst)
267 assert(loads < LQEntries * 2);
268 assert(!LQIndices.empty());
269 int load_index = LQIndices.front();
272 DPRINTF(OzoneLSQ, "Inserting load PC %#x, idx:%i [sn:%lli]\n",
273 load_inst->readPC(), load_index, load_inst->seqNum);
275 load_inst->lqIdx = load_index;
277 loadQueue.push_front(load_inst);
278 LQItHash[load_index] = loadQueue.begin();
283 template <class Impl>
285 OzoneLWLSQ<Impl>::insertStore(DynInstPtr &store_inst)
287 // Make sure it is not full before inserting an instruction.
288 assert(stores - storesToWB < SQEntries);
290 assert(!SQIndices.empty());
291 int store_index = SQIndices.front();
294 DPRINTF(OzoneLSQ, "Inserting store PC %#x, idx:%i [sn:%lli]\n",
295 store_inst->readPC(), store_index, store_inst->seqNum);
297 store_inst->sqIdx = store_index;
298 SQEntry entry(store_inst);
299 if (loadQueue.empty()) {
300 entry.lqIt = loadQueue.end();
302 entry.lqIt = loadQueue.begin();
304 storeQueue.push_front(entry);
306 SQItHash[store_index] = storeQueue.begin();
311 template <class Impl>
312 typename Impl::DynInstPtr
313 OzoneLWLSQ<Impl>::getMemDepViolator()
315 DynInstPtr temp = memDepViolator;
317 memDepViolator = NULL;
322 template <class Impl>
324 OzoneLWLSQ<Impl>::numFreeEntries()
326 unsigned free_lq_entries = LQEntries - loads;
327 unsigned free_sq_entries = SQEntries - stores;
329 // Both the LQ and SQ entries have an extra dummy entry to differentiate
330 // empty/full conditions. Subtract 1 from the free entries.
331 if (free_lq_entries < free_sq_entries) {
332 return free_lq_entries - 1;
334 return free_sq_entries - 1;
338 template <class Impl>
340 OzoneLWLSQ<Impl>::numLoadsReady()
343 LQIt lq_it = loadQueue.begin();
344 LQIt end_it = loadQueue.end();
346 while (lq_it != end_it) {
347 if ((*lq_it)->readyToIssue()) {
355 template <class Impl>
357 OzoneLWLSQ<Impl>::executeLoad(DynInstPtr &inst)
359 // Execute a specific load.
360 Fault load_fault = NoFault;
362 DPRINTF(OzoneLSQ, "Executing load PC %#x, [sn:%lli]\n",
363 inst->readPC(),inst->seqNum);
365 // Make sure it's really in the list.
366 // Normally it should always be in the list. However,
367 /* due to a syscall it may not be the list.
371 if (i == loadTail && !find(inst)) {
372 assert(0 && "Load not in the queue!");
373 } else if (loadQueue[i] == inst) {
378 if (i >= LQEntries) {
384 load_fault = inst->initiateAcc();
386 // Might want to make sure that I'm not overwriting a previously faulting
387 // instruction that hasn't been checked yet.
388 // Actually probably want the oldest faulting load
389 if (load_fault != NoFault) {
390 DPRINTF(OzoneLSQ, "Load [sn:%lli] has a fault\n", inst->seqNum);
391 // Maybe just set it as can commit here, although that might cause
392 // some other problems with sending traps to the ROB too quickly.
393 be->instToCommit(inst);
394 // iewStage->activityThisCycle();
400 template <class Impl>
402 OzoneLWLSQ<Impl>::executeStore(DynInstPtr &store_inst)
404 // Make sure that a store exists.
407 int store_idx = store_inst->sqIdx;
408 SQHashIt sq_hash_it = SQItHash.find(store_idx);
409 assert(sq_hash_it != SQItHash.end());
410 DPRINTF(OzoneLSQ, "Executing store PC %#x [sn:%lli]\n",
411 store_inst->readPC(), store_inst->seqNum);
413 SQIt sq_it = (*sq_hash_it).second;
415 Fault store_fault = store_inst->initiateAcc();
417 // Store size should now be available. Use it to get proper offset for
419 int size = (*sq_it).size;
422 DPRINTF(OzoneLSQ,"Fault on Store PC %#x, [sn:%lli],Size = 0\n",
423 store_inst->readPC(),store_inst->seqNum);
428 assert(store_fault == NoFault);
430 if (!storeFaultInst) {
431 if (store_fault != NoFault) {
432 panic("Fault in a store instruction!");
433 storeFaultInst = store_inst;
434 } else if (store_inst->isStoreConditional()) {
435 // Store conditionals need to set themselves as able to
436 // writeback if we haven't had a fault by here.
437 (*sq_it).canWB = true;
440 DPRINTF(OzoneLSQ, "Nonspeculative store! storesToWB:%i\n",
445 LQIt lq_it = --(loadQueue.end());
447 if (!memDepViolator) {
448 while (lq_it != loadQueue.end()) {
449 if ((*lq_it)->seqNum < store_inst->seqNum) {
453 // Actually should only check loads that have actually executed
454 // Might be safe because effAddr is set to InvalAddr when the
455 // dyn inst is created.
457 // Must actually check all addrs in the proper size range
458 // Which is more correct than needs to be. What if for now we just
459 // assume all loads are quad-word loads, and do the addr based
461 // @todo: Fix this, magic number being used here
462 if (((*lq_it)->effAddr >> 8) ==
463 (store_inst->effAddr >> 8)) {
464 // A load incorrectly passed this store. Squash and refetch.
465 // For now return a fault to show that it was unsuccessful.
466 memDepViolator = (*lq_it);
468 return TheISA::genMachineCheckFault();
474 // If we've reached this point, there was no violation.
475 memDepViolator = NULL;
481 template <class Impl>
483 OzoneLWLSQ<Impl>::commitLoad()
485 assert(!loadQueue.empty());
487 DPRINTF(OzoneLSQ, "[sn:%lli] Committing head load instruction, PC %#x\n",
488 loadQueue.back()->seqNum, loadQueue.back()->readPC());
490 LQIndices.push(loadQueue.back()->lqIdx);
491 LQItHash.erase(loadQueue.back()->lqIdx);
493 loadQueue.pop_back();
498 template <class Impl>
500 OzoneLWLSQ<Impl>::commitLoads(InstSeqNum &youngest_inst)
502 assert(loads == 0 || !loadQueue.empty());
505 loadQueue.back()->seqNum <= youngest_inst) {
510 template <class Impl>
512 OzoneLWLSQ<Impl>::commitStores(InstSeqNum &youngest_inst)
514 assert(stores == 0 || !storeQueue.empty());
516 SQIt sq_it = --(storeQueue.end());
517 while (!storeQueue.empty() && sq_it != storeQueue.end()) {
518 assert((*sq_it).inst);
519 if (!(*sq_it).canWB) {
520 if ((*sq_it).inst->seqNum > youngest_inst) {
525 DPRINTF(OzoneLSQ, "Marking store as able to write back, PC "
526 "%#x [sn:%lli], storesToWB:%i\n",
527 (*sq_it).inst->readPC(),
528 (*sq_it).inst->seqNum,
531 (*sq_it).canWB = true;
538 template <class Impl>
540 OzoneLWLSQ<Impl>::writebackStores()
542 SQIt sq_it = --(storeQueue.end());
543 while (storesToWB > 0 &&
544 sq_it != storeQueue.end() &&
547 usedPorts < cachePorts) {
549 if (isStoreBlocked) {
550 DPRINTF(OzoneLSQ, "Unable to write back any more stores, cache"
555 DynInstPtr inst = (*sq_it).inst;
557 if ((*sq_it).size == 0 && !(*sq_it).completed) {
559 completeStore(inst->sqIdx);
564 if (inst->isDataPrefetch() || (*sq_it).committed) {
571 assert((*sq_it).req);
572 assert(!(*sq_it).committed);
574 Request *req = (*sq_it).req;
575 (*sq_it).committed = true;
577 assert(!inst->memData);
578 inst->memData = new uint8_t[64];
579 memcpy(inst->memData, (uint8_t *)&(*sq_it).data,
582 PacketPtr data_pkt = new Packet(req, Packet::WriteReq, Packet::Broadcast);
583 data_pkt->dataStatic(inst->memData);
585 LSQSenderState *state = new LSQSenderState;
586 state->isLoad = false;
587 state->idx = inst->sqIdx;
589 data_pkt->senderState = state;
591 DPRINTF(OzoneLSQ, "D-Cache: Writing back store PC:%#x "
592 "to Addr:%#x, data:%#x [sn:%lli]\n",
593 (*sq_it).inst->readPC(),
594 req->getPaddr(), *(inst->memData),
597 // @todo: Remove this SC hack once the memory system handles it.
598 if (req->getFlags() & LOCKED) {
599 if (req->getFlags() & UNCACHEABLE) {
606 // Hack: Instantly complete this store.
607 completeDataAccess(data_pkt);
613 // Non-store conditionals do not need a writeback.
617 if (!dcachePort->sendTiming(data_pkt)) {
618 // Need to handle becoming blocked on a store.
619 isStoreBlocked = true;
620 assert(retryPkt == NULL);
623 storePostSend(data_pkt, inst);
627 DPRINTF(OzoneLSQ, "D-Cache: Writing back store idx:%i PC:%#x "
628 "to Addr:%#x, data:%#x [sn:%lli]\n",
629 inst->sqIdx,inst->readPC(),
630 req->paddr, *(req->data),
633 if (dcacheInterface) {
634 assert(!req->completionEvent);
635 StoreCompletionEvent *store_event = new
636 StoreCompletionEvent(inst, be, NULL, this);
637 req->completionEvent = store_event;
639 MemAccessResult result = dcacheInterface->access(req);
642 inst->seqNum == stallingStoreIsn) {
643 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
645 stallingStoreIsn, (*stallingLoad)->seqNum);
647 stallingStoreIsn = 0;
648 be->replayMemInst((*stallingLoad));
651 if (result != MA_HIT && dcacheInterface->doEvents()) {
652 store_event->miss = true;
653 typename BackEnd::LdWritebackEvent *wb = NULL;
654 if (req->flags & LOCKED) {
655 wb = new typename BackEnd::LdWritebackEvent(inst,
657 store_event->wbEvent = wb;
660 DPRINTF(OzoneLSQ,"D-Cache Write Miss!\n");
662 // DPRINTF(Activity, "Active st accessing mem miss [sn:%lli]\n",
665 be->addDcacheMiss(inst);
667 lastDcacheStall = curTick;
669 _status = DcacheMissStall;
671 // Increment stat here or something
675 DPRINTF(OzoneLSQ,"D-Cache: Write Hit on idx:%i !\n",
678 // DPRINTF(Activity, "Active st accessing mem hit [sn:%lli]\n",
681 if (req->flags & LOCKED) {
682 // Stx_C does not generate a system port
683 // transaction in the 21264, but that might be
684 // hard to accomplish in this model.
686 typename BackEnd::LdWritebackEvent *wb =
687 new typename BackEnd::LdWritebackEvent(inst,
689 store_event->wbEvent = wb;
694 panic("Must HAVE DCACHE!!!!!\n");
699 // Not sure this should set it to 0.
702 assert(stores >= 0 && storesToWB >= 0);
705 template <class Impl>
707 OzoneLWLSQ<Impl>::squash(const InstSeqNum &squashed_num)
709 DPRINTF(OzoneLSQ, "Squashing until [sn:%lli]!"
710 "(Loads:%i Stores:%i)\n",squashed_num,loads,stores);
713 LQIt lq_it = loadQueue.begin();
715 while (loads != 0 && (*lq_it)->seqNum > squashed_num) {
716 assert(!loadQueue.empty());
717 // Clear the smart pointer to make sure it is decremented.
718 DPRINTF(OzoneLSQ,"Load Instruction PC %#x squashed, "
723 if (isStalled() && lq_it == stallingLoad) {
725 stallingStoreIsn = 0;
732 LQHashIt lq_hash_it = LQItHash.find((*lq_it)->lqIdx);
733 assert(lq_hash_it != LQItHash.end());
734 LQItHash.erase(lq_hash_it);
735 LQIndices.push((*lq_it)->lqIdx);
736 loadQueue.erase(lq_it++);
740 if (squashed_num < blockedLoadSeqNum) {
741 isLoadBlocked = false;
742 loadBlockedHandled = false;
743 blockedLoadSeqNum = 0;
747 SQIt sq_it = storeQueue.begin();
749 while (stores != 0 && (*sq_it).inst->seqNum > squashed_num) {
750 assert(!storeQueue.empty());
752 if ((*sq_it).canWB) {
756 // Clear the smart pointer to make sure it is decremented.
757 DPRINTF(OzoneLSQ,"Store Instruction PC %#x idx:%i squashed [sn:%lli]\n",
758 (*sq_it).inst->readPC(), (*sq_it).inst->sqIdx,
759 (*sq_it).inst->seqNum);
761 // I don't think this can happen. It should have been cleared by the
764 (*sq_it).inst->seqNum == stallingStoreIsn) {
765 panic("Is stalled should have been cleared by stalling load!\n");
767 stallingStoreIsn = 0;
770 SQHashIt sq_hash_it = SQItHash.find((*sq_it).inst->sqIdx);
771 assert(sq_hash_it != SQItHash.end());
772 SQItHash.erase(sq_hash_it);
773 SQIndices.push((*sq_it).inst->sqIdx);
774 (*sq_it).inst = NULL;
778 storeQueue.erase(sq_it++);
782 template <class Impl>
784 OzoneLWLSQ<Impl>::dumpInsts()
786 cprintf("Load store queue: Dumping instructions.\n");
787 cprintf("Load queue size: %i\n", loads);
788 cprintf("Load queue: ");
790 LQIt lq_it = --(loadQueue.end());
792 while (lq_it != loadQueue.end() && (*lq_it)) {
793 cprintf("[sn:%lli] %#x ", (*lq_it)->seqNum,
799 cprintf("\nStore queue size: %i\n", stores);
800 cprintf("Store queue: ");
802 SQIt sq_it = --(storeQueue.end());
804 while (sq_it != storeQueue.end() && (*sq_it).inst) {
805 cprintf("[sn:%lli]\nPC:%#x\nSize:%i\nCommitted:%i\nCompleted:%i\ncanWB:%i\n",
806 (*sq_it).inst->seqNum,
807 (*sq_it).inst->readPC(),
819 template <class Impl>
821 OzoneLWLSQ<Impl>::storePostSend(Packet *pkt, DynInstPtr &inst)
824 inst->seqNum == stallingStoreIsn) {
825 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
827 stallingStoreIsn, (*stallingLoad)->seqNum);
829 stallingStoreIsn = 0;
830 be->replayMemInst((*stallingLoad));
833 if (!inst->isStoreConditional()) {
834 // The store is basically completed at this time. This
835 // only works so long as the checker doesn't try to
836 // verify the value in memory for stores.
837 inst->setCompleted();
840 cpu->checker->verify(inst);
845 if (pkt->result != Packet::Success) {
846 DPRINTF(OzoneLSQ,"D-Cache Write Miss!\n");
848 DPRINTF(Activity, "Active st accessing mem miss [sn:%lli]\n",
851 //mshrSeqNums.push_back(storeQueue[storeWBIdx].inst->seqNum);
853 //DPRINTF(OzoneLWLSQ, "Added MSHR. count = %i\n",mshrSeqNums.size());
855 // @todo: Increment stat here.
857 DPRINTF(OzoneLSQ,"D-Cache: Write Hit!\n");
859 DPRINTF(Activity, "Active st accessing mem hit [sn:%lli]\n",
864 template <class Impl>
866 OzoneLWLSQ<Impl>::writeback(DynInstPtr &inst, PacketPtr pkt)
868 // Squashed instructions do not need to complete their access.
869 if (inst->isSquashed()) {
870 assert(!inst->isStore());
874 if (!inst->isExecuted()) {
877 // Complete access to copy data to proper place.
878 inst->completeAcc(pkt);
881 // Need to insert instruction into queue to commit
882 be->instToCommit(inst);
885 template <class Impl>
887 OzoneLWLSQ<Impl>::completeStore(int store_idx)
889 SQHashIt sq_hash_it = SQItHash.find(store_idx);
890 assert(sq_hash_it != SQItHash.end());
891 SQIt sq_it = (*sq_hash_it).second;
893 assert((*sq_it).inst);
894 (*sq_it).completed = true;
895 DynInstPtr inst = (*sq_it).inst;
900 inst->seqNum == stallingStoreIsn) {
901 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
903 stallingStoreIsn, (*stallingLoad)->seqNum);
905 stallingStoreIsn = 0;
906 be->replayMemInst((*stallingLoad));
909 DPRINTF(OzoneLSQ, "Completing store idx:%i [sn:%lli], storesToWB:%i\n",
910 inst->sqIdx, inst->seqNum, storesToWB);
912 assert(!storeQueue.empty());
913 SQItHash.erase(sq_hash_it);
914 SQIndices.push(inst->sqIdx);
915 storeQueue.erase(sq_it);
918 inst->setCompleted();
921 cpu->checker->verify(inst);
926 template <class Impl>
928 OzoneLWLSQ<Impl>::recvRetry()
930 panic("Unimplemented!");
933 template <class Impl>
935 OzoneLWLSQ<Impl>::switchOut()
937 assert(storesToWB == 0);
940 // Clear the queue to free up resources
943 loads = stores = storesToWB = 0;
946 template <class Impl>
948 OzoneLWLSQ<Impl>::takeOverFrom(ThreadContext *old_tc)
950 // Clear out any old state. May be redundant if this is the first time
951 // the CPU is being used.
953 isLoadBlocked = false;
954 loadBlockedHandled = false;
957 // Could do simple checks here to see if indices are on twice
958 while (!LQIndices.empty())
960 while (!SQIndices.empty())
963 for (int i = 0; i < LQEntries * 2; i++) {
970 loadFaultInst = storeFaultInst = memDepViolator = NULL;
972 blockedLoadSeqNum = 0;