2 * Copyright (c) 2004-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.
29 #include "arch/isa_traits.hh"
30 #include "base/str.hh"
31 #include "cpu/ozone/lw_lsq.hh"
32 #include "cpu/checker/cpu.hh"
35 OzoneLWLSQ<Impl>::StoreCompletionEvent::StoreCompletionEvent(DynInstPtr &_inst,
38 OzoneLWLSQ<Impl> *lsq_ptr)
39 : Event(&mainEventQueue),
46 this->setFlags(Event::AutoDelete);
51 OzoneLWLSQ<Impl>::StoreCompletionEvent::process()
53 DPRINTF(OzoneLSQ, "Cache miss complete for store [sn:%lli]\n",
56 //lsqPtr->removeMSHR(lsqPtr->storeQueue[storeIdx].inst->seqNum);
58 // lsqPtr->cpu->wakeCPU();
59 if (lsqPtr->isSwitchedOut()) {
71 lsqPtr->completeStore(inst->sqIdx);
73 be->removeDcacheMiss(inst);
78 OzoneLWLSQ<Impl>::StoreCompletionEvent::description()
80 return "LSQ store completion event";
84 OzoneLWLSQ<Impl>::OzoneLWLSQ()
85 : loads(0), stores(0), storesToWB(0), stalled(false), isLoadBlocked(false),
86 loadBlockedHandled(false)
92 OzoneLWLSQ<Impl>::init(Params *params, unsigned maxLQEntries,
93 unsigned maxSQEntries, unsigned id)
95 DPRINTF(OzoneLSQ, "Creating OzoneLWLSQ%i object.\n",id);
99 LQEntries = maxLQEntries;
100 SQEntries = maxSQEntries;
102 for (int i = 0; i < LQEntries * 2; i++) {
108 cachePorts = params->cachePorts;
110 dcacheInterface = params->dcacheInterface;
112 loadFaultInst = storeFaultInst = memDepViolator = NULL;
114 blockedLoadSeqNum = 0;
119 OzoneLWLSQ<Impl>::name() const
126 OzoneLWLSQ<Impl>::clearLQ()
133 OzoneLWLSQ<Impl>::clearSQ()
140 OzoneLWLSQ<Impl>::setPageTable(PageTable *pt_ptr)
142 DPRINTF(OzoneLSQ, "Setting the page table pointer.\n");
148 OzoneLWLSQ<Impl>::resizeLQ(unsigned size)
150 assert( size >= LQEntries);
152 if (size > LQEntries) {
153 while (size > loadQueue.size()) {
155 loadQueue.push_back(dummy);
166 OzoneLWLSQ<Impl>::resizeSQ(unsigned size)
168 if (size > SQEntries) {
169 while (size > storeQueue.size()) {
171 storeQueue.push_back(dummy);
179 template <class Impl>
181 OzoneLWLSQ<Impl>::insert(DynInstPtr &inst)
183 // Make sure we really have a memory reference.
184 assert(inst->isMemRef());
186 // Make sure it's one of the two classes of memory references.
187 assert(inst->isLoad() || inst->isStore());
189 if (inst->isLoad()) {
196 template <class Impl>
198 OzoneLWLSQ<Impl>::insertLoad(DynInstPtr &load_inst)
200 assert(loads < LQEntries * 2);
201 assert(!LQIndices.empty());
202 int load_index = LQIndices.front();
205 DPRINTF(OzoneLSQ, "Inserting load PC %#x, idx:%i [sn:%lli]\n",
206 load_inst->readPC(), load_index, load_inst->seqNum);
208 load_inst->lqIdx = load_index;
210 loadQueue.push_front(load_inst);
211 LQItHash[load_index] = loadQueue.begin();
216 template <class Impl>
218 OzoneLWLSQ<Impl>::insertStore(DynInstPtr &store_inst)
220 // Make sure it is not full before inserting an instruction.
221 assert(stores - storesToWB < SQEntries);
223 assert(!SQIndices.empty());
224 int store_index = SQIndices.front();
227 DPRINTF(OzoneLSQ, "Inserting store PC %#x, idx:%i [sn:%lli]\n",
228 store_inst->readPC(), store_index, store_inst->seqNum);
230 store_inst->sqIdx = store_index;
231 SQEntry entry(store_inst);
232 if (loadQueue.empty()) {
233 entry.lqIt = loadQueue.end();
235 entry.lqIt = loadQueue.begin();
237 storeQueue.push_front(entry);
239 SQItHash[store_index] = storeQueue.begin();
244 template <class Impl>
245 typename Impl::DynInstPtr
246 OzoneLWLSQ<Impl>::getMemDepViolator()
248 DynInstPtr temp = memDepViolator;
250 memDepViolator = NULL;
255 template <class Impl>
257 OzoneLWLSQ<Impl>::numFreeEntries()
259 unsigned free_lq_entries = LQEntries - loads;
260 unsigned free_sq_entries = SQEntries - stores;
262 // Both the LQ and SQ entries have an extra dummy entry to differentiate
263 // empty/full conditions. Subtract 1 from the free entries.
264 if (free_lq_entries < free_sq_entries) {
265 return free_lq_entries - 1;
267 return free_sq_entries - 1;
271 template <class Impl>
273 OzoneLWLSQ<Impl>::numLoadsReady()
276 LQIt lq_it = loadQueue.begin();
277 LQIt end_it = loadQueue.end();
279 while (lq_it != end_it) {
280 if ((*lq_it)->readyToIssue()) {
288 template <class Impl>
290 OzoneLWLSQ<Impl>::executeLoad(DynInstPtr &inst)
292 // Execute a specific load.
293 Fault load_fault = NoFault;
295 DPRINTF(OzoneLSQ, "Executing load PC %#x, [sn:%lli]\n",
296 inst->readPC(),inst->seqNum);
298 // Make sure it's really in the list.
299 // Normally it should always be in the list. However,
300 /* due to a syscall it may not be the list.
304 if (i == loadTail && !find(inst)) {
305 assert(0 && "Load not in the queue!");
306 } else if (loadQueue[i] == inst) {
311 if (i >= LQEntries) {
317 load_fault = inst->initiateAcc();
319 // Might want to make sure that I'm not overwriting a previously faulting
320 // instruction that hasn't been checked yet.
321 // Actually probably want the oldest faulting load
322 if (load_fault != NoFault) {
323 DPRINTF(OzoneLSQ, "Load [sn:%lli] has a fault\n", inst->seqNum);
324 // Maybe just set it as can commit here, although that might cause
325 // some other problems with sending traps to the ROB too quickly.
326 be->instToCommit(inst);
327 // iewStage->activityThisCycle();
333 template <class Impl>
335 OzoneLWLSQ<Impl>::executeStore(DynInstPtr &store_inst)
337 // Make sure that a store exists.
340 int store_idx = store_inst->sqIdx;
341 SQHashIt sq_hash_it = SQItHash.find(store_idx);
342 assert(sq_hash_it != SQItHash.end());
343 DPRINTF(OzoneLSQ, "Executing store PC %#x [sn:%lli]\n",
344 store_inst->readPC(), store_inst->seqNum);
346 SQIt sq_it = (*sq_hash_it).second;
348 Fault store_fault = store_inst->initiateAcc();
350 // Store size should now be available. Use it to get proper offset for
352 int size = (*sq_it).size;
355 DPRINTF(OzoneLSQ,"Fault on Store PC %#x, [sn:%lli],Size = 0\n",
356 store_inst->readPC(),store_inst->seqNum);
361 assert(store_fault == NoFault);
363 if (!storeFaultInst) {
364 if (store_fault != NoFault) {
365 panic("Fault in a store instruction!");
366 storeFaultInst = store_inst;
367 } else if (store_inst->isStoreConditional()) {
368 // Store conditionals need to set themselves as able to
369 // writeback if we haven't had a fault by here.
370 (*sq_it).canWB = true;
373 DPRINTF(OzoneLSQ, "Nonspeculative store! storesToWB:%i\n",
378 LQIt lq_it = --(loadQueue.end());
380 if (!memDepViolator) {
381 while (lq_it != loadQueue.end()) {
382 if ((*lq_it)->seqNum < store_inst->seqNum) {
386 // Actually should only check loads that have actually executed
387 // Might be safe because effAddr is set to InvalAddr when the
388 // dyn inst is created.
390 // Must actually check all addrs in the proper size range
391 // Which is more correct than needs to be. What if for now we just
392 // assume all loads are quad-word loads, and do the addr based
394 // @todo: Fix this, magic number being used here
395 if (((*lq_it)->effAddr >> 8) ==
396 (store_inst->effAddr >> 8)) {
397 // A load incorrectly passed this store. Squash and refetch.
398 // For now return a fault to show that it was unsuccessful.
399 memDepViolator = (*lq_it);
401 return TheISA::genMachineCheckFault();
407 // If we've reached this point, there was no violation.
408 memDepViolator = NULL;
414 template <class Impl>
416 OzoneLWLSQ<Impl>::commitLoad()
418 assert(!loadQueue.empty());
420 DPRINTF(OzoneLSQ, "[sn:%lli] Committing head load instruction, PC %#x\n",
421 loadQueue.back()->seqNum, loadQueue.back()->readPC());
423 LQIndices.push(loadQueue.back()->lqIdx);
424 LQItHash.erase(loadQueue.back()->lqIdx);
426 loadQueue.pop_back();
431 template <class Impl>
433 OzoneLWLSQ<Impl>::commitLoads(InstSeqNum &youngest_inst)
435 assert(loads == 0 || !loadQueue.empty());
438 loadQueue.back()->seqNum <= youngest_inst) {
443 template <class Impl>
445 OzoneLWLSQ<Impl>::commitStores(InstSeqNum &youngest_inst)
447 assert(stores == 0 || !storeQueue.empty());
449 SQIt sq_it = --(storeQueue.end());
450 while (!storeQueue.empty() && sq_it != storeQueue.end()) {
451 assert((*sq_it).inst);
452 if (!(*sq_it).canWB) {
453 if ((*sq_it).inst->seqNum > youngest_inst) {
458 DPRINTF(OzoneLSQ, "Marking store as able to write back, PC "
459 "%#x [sn:%lli], storesToWB:%i\n",
460 (*sq_it).inst->readPC(),
461 (*sq_it).inst->seqNum,
464 (*sq_it).canWB = true;
471 template <class Impl>
473 OzoneLWLSQ<Impl>::writebackStores()
475 SQIt sq_it = --(storeQueue.end());
476 while (storesToWB > 0 &&
477 sq_it != storeQueue.end() &&
480 usedPorts < cachePorts) {
482 DynInstPtr inst = (*sq_it).inst;
484 if ((*sq_it).size == 0 && !(*sq_it).completed) {
486 completeStore(inst->sqIdx);
491 if (inst->isDataPrefetch() || (*sq_it).committed) {
496 if (dcacheInterface && dcacheInterface->isBlocked()) {
497 DPRINTF(OzoneLSQ, "Unable to write back any more stores, cache"
504 assert((*sq_it).req);
505 assert(!(*sq_it).committed);
507 (*sq_it).committed = true;
509 MemReqPtr req = (*sq_it).req;
512 req->completionEvent = NULL;
515 switch((*sq_it).size) {
517 cpu->write(req, (uint8_t &)(*sq_it).data);
520 cpu->write(req, (uint16_t &)(*sq_it).data);
523 cpu->write(req, (uint32_t &)(*sq_it).data);
526 cpu->write(req, (uint64_t &)(*sq_it).data);
529 panic("Unexpected store size!\n");
531 if (!(req->flags & LOCKED)) {
532 (*sq_it).inst->setCompleted();
534 cpu->checker->tick((*sq_it).inst);
538 DPRINTF(OzoneLSQ, "D-Cache: Writing back store idx:%i PC:%#x "
539 "to Addr:%#x, data:%#x [sn:%lli]\n",
540 inst->sqIdx,inst->readPC(),
541 req->paddr, *(req->data),
544 if (dcacheInterface) {
545 assert(!req->completionEvent);
546 StoreCompletionEvent *store_event = new
547 StoreCompletionEvent(inst, be, NULL, this);
548 req->completionEvent = store_event;
550 MemAccessResult result = dcacheInterface->access(req);
553 inst->seqNum == stallingStoreIsn) {
554 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
556 stallingStoreIsn, (*stallingLoad)->seqNum);
558 stallingStoreIsn = 0;
559 be->replayMemInst((*stallingLoad));
562 if (result != MA_HIT && dcacheInterface->doEvents()) {
563 store_event->miss = true;
564 typename BackEnd::LdWritebackEvent *wb = NULL;
565 if (req->flags & LOCKED) {
566 wb = new typename BackEnd::LdWritebackEvent(inst,
568 store_event->wbEvent = wb;
571 DPRINTF(OzoneLSQ,"D-Cache Write Miss!\n");
573 // DPRINTF(Activity, "Active st accessing mem miss [sn:%lli]\n",
576 be->addDcacheMiss(inst);
578 lastDcacheStall = curTick;
580 _status = DcacheMissStall;
582 // Increment stat here or something
586 DPRINTF(OzoneLSQ,"D-Cache: Write Hit on idx:%i !\n",
589 // DPRINTF(Activity, "Active st accessing mem hit [sn:%lli]\n",
592 if (req->flags & LOCKED) {
593 // Stx_C does not generate a system port
594 // transaction in the 21264, but that might be
595 // hard to accomplish in this model.
597 typename BackEnd::LdWritebackEvent *wb =
598 new typename BackEnd::LdWritebackEvent(inst,
600 store_event->wbEvent = wb;
605 panic("Must HAVE DCACHE!!!!!\n");
609 // Not sure this should set it to 0.
612 assert(stores >= 0 && storesToWB >= 0);
615 template <class Impl>
617 OzoneLWLSQ<Impl>::squash(const InstSeqNum &squashed_num)
619 DPRINTF(OzoneLSQ, "Squashing until [sn:%lli]!"
620 "(Loads:%i Stores:%i)\n",squashed_num,loads,stores);
623 LQIt lq_it = loadQueue.begin();
625 while (loads != 0 && (*lq_it)->seqNum > squashed_num) {
626 assert(!loadQueue.empty());
627 // Clear the smart pointer to make sure it is decremented.
628 DPRINTF(OzoneLSQ,"Load Instruction PC %#x squashed, "
633 if (isStalled() && lq_it == stallingLoad) {
635 stallingStoreIsn = 0;
642 LQHashIt lq_hash_it = LQItHash.find((*lq_it)->lqIdx);
643 assert(lq_hash_it != LQItHash.end());
644 LQItHash.erase(lq_hash_it);
645 LQIndices.push((*lq_it)->lqIdx);
646 loadQueue.erase(lq_it++);
650 if (squashed_num < blockedLoadSeqNum) {
651 isLoadBlocked = false;
652 loadBlockedHandled = false;
653 blockedLoadSeqNum = 0;
657 SQIt sq_it = storeQueue.begin();
659 while (stores != 0 && (*sq_it).inst->seqNum > squashed_num) {
660 assert(!storeQueue.empty());
662 if ((*sq_it).canWB) {
666 // Clear the smart pointer to make sure it is decremented.
667 DPRINTF(OzoneLSQ,"Store Instruction PC %#x idx:%i squashed [sn:%lli]\n",
668 (*sq_it).inst->readPC(), (*sq_it).inst->sqIdx,
669 (*sq_it).inst->seqNum);
671 // I don't think this can happen. It should have been cleared by the
674 (*sq_it).inst->seqNum == stallingStoreIsn) {
675 panic("Is stalled should have been cleared by stalling load!\n");
677 stallingStoreIsn = 0;
680 SQHashIt sq_hash_it = SQItHash.find((*sq_it).inst->sqIdx);
681 assert(sq_hash_it != SQItHash.end());
682 SQItHash.erase(sq_hash_it);
683 SQIndices.push((*sq_it).inst->sqIdx);
684 (*sq_it).inst = NULL;
688 assert(!(*sq_it).req->completionEvent);
692 storeQueue.erase(sq_it++);
696 template <class Impl>
698 OzoneLWLSQ<Impl>::dumpInsts()
700 cprintf("Load store queue: Dumping instructions.\n");
701 cprintf("Load queue size: %i\n", loads);
702 cprintf("Load queue: ");
704 LQIt lq_it = --(loadQueue.end());
706 while (lq_it != loadQueue.end() && (*lq_it)) {
707 cprintf("[sn:%lli] %#x ", (*lq_it)->seqNum,
713 cprintf("\nStore queue size: %i\n", stores);
714 cprintf("Store queue: ");
716 SQIt sq_it = --(storeQueue.end());
718 while (sq_it != storeQueue.end() && (*sq_it).inst) {
719 cprintf("[sn:%lli]\nPC:%#x\nSize:%i\nCommitted:%i\nCompleted:%i\ncanWB:%i\n",
720 (*sq_it).inst->seqNum,
721 (*sq_it).inst->readPC(),
733 template <class Impl>
735 OzoneLWLSQ<Impl>::completeStore(int store_idx)
737 SQHashIt sq_hash_it = SQItHash.find(store_idx);
738 assert(sq_hash_it != SQItHash.end());
739 SQIt sq_it = (*sq_hash_it).second;
741 assert((*sq_it).inst);
742 (*sq_it).completed = true;
743 DynInstPtr inst = (*sq_it).inst;
748 inst->seqNum == stallingStoreIsn) {
749 DPRINTF(OzoneLSQ, "Unstalling, stalling store [sn:%lli] "
751 stallingStoreIsn, (*stallingLoad)->seqNum);
753 stallingStoreIsn = 0;
754 be->replayMemInst((*stallingLoad));
757 DPRINTF(OzoneLSQ, "Completing store idx:%i [sn:%lli], storesToWB:%i\n",
758 inst->sqIdx, inst->seqNum, storesToWB);
760 assert(!storeQueue.empty());
761 SQItHash.erase(sq_hash_it);
762 SQIndices.push(inst->sqIdx);
763 storeQueue.erase(sq_it);
766 inst->setCompleted();
768 cpu->checker->tick(inst);
772 template <class Impl>
774 OzoneLWLSQ<Impl>::switchOut()
776 assert(storesToWB == 0);
778 SQIt sq_it = --(storeQueue.end());
779 while (storesToWB > 0 &&
780 sq_it != storeQueue.end() &&
784 DynInstPtr inst = (*sq_it).inst;
786 if ((*sq_it).size == 0 && !(*sq_it).completed) {
791 // Store conditionals don't complete until *after* they have written
792 // back. If it's here and not yet sent to memory, then don't bother
793 // as it's not part of committed state.
794 if (inst->isDataPrefetch() || (*sq_it).committed) {
797 } else if ((*sq_it).req->flags & LOCKED) {
799 assert(!(*sq_it).canWB ||
800 ((*sq_it).canWB && (*sq_it).req->flags & LOCKED));
804 assert((*sq_it).req);
805 assert(!(*sq_it).committed);
807 MemReqPtr req = (*sq_it).req;
808 (*sq_it).committed = true;
811 req->completionEvent = NULL;
814 req->data = new uint8_t[64];
815 memcpy(req->data, (uint8_t *)&(*sq_it).data, req->size);
817 DPRINTF(OzoneLSQ, "Switching out : Writing back store idx:%i PC:%#x "
818 "to Addr:%#x, data:%#x directly to memory [sn:%lli]\n",
819 inst->sqIdx,inst->readPC(),
820 req->paddr, *(req->data),
823 switch((*sq_it).size) {
825 cpu->write(req, (uint8_t &)(*sq_it).data);
828 cpu->write(req, (uint16_t &)(*sq_it).data);
831 cpu->write(req, (uint32_t &)(*sq_it).data);
834 cpu->write(req, (uint64_t &)(*sq_it).data);
837 panic("Unexpected store size!\n");
841 // Clear the queue to free up resources
844 loads = stores = storesToWB = 0;
847 template <class Impl>
849 OzoneLWLSQ<Impl>::takeOverFrom(ThreadContext *old_tc)
851 // Clear out any old state. May be redundant if this is the first time
852 // the CPU is being used.
854 isLoadBlocked = false;
855 loadBlockedHandled = false;
858 // Could do simple checks here to see if indices are on twice
859 while (!LQIndices.empty())
861 while (!SQIndices.empty())
864 for (int i = 0; i < LQEntries * 2; i++) {
871 loadFaultInst = storeFaultInst = memDepViolator = NULL;
873 blockedLoadSeqNum = 0;