lsq_unit_impl.hh revision 13472
19814Sandreas.hansson@arm.com
22292SN/A/*
312216Snikos.nikoleris@arm.com * Copyright (c) 2010-2014, 2017 ARM Limited
410239Sbinhpham@cs.rutgers.edu * Copyright (c) 2013 Advanced Micro Devices, Inc.
57597Sminkyu.jeong@arm.com * All rights reserved
67597Sminkyu.jeong@arm.com *
77597Sminkyu.jeong@arm.com * The license below extends only to copyright in the software and shall
87597Sminkyu.jeong@arm.com * not be construed as granting a license to any other intellectual
97597Sminkyu.jeong@arm.com * property including but not limited to intellectual property relating
107597Sminkyu.jeong@arm.com * to a hardware implementation of the functionality of the software
117597Sminkyu.jeong@arm.com * licensed hereunder.  You may use the software subject to the license
127597Sminkyu.jeong@arm.com * terms below provided that you ensure that this notice is replicated
137597Sminkyu.jeong@arm.com * unmodified and in its entirety in all distributions of the software,
147597Sminkyu.jeong@arm.com * modified or unmodified, in source code or in binary form.
157597Sminkyu.jeong@arm.com *
162292SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
172292SN/A * All rights reserved.
182292SN/A *
192292SN/A * Redistribution and use in source and binary forms, with or without
202292SN/A * modification, are permitted provided that the following conditions are
212292SN/A * met: redistributions of source code must retain the above copyright
222292SN/A * notice, this list of conditions and the following disclaimer;
232292SN/A * redistributions in binary form must reproduce the above copyright
242292SN/A * notice, this list of conditions and the following disclaimer in the
252292SN/A * documentation and/or other materials provided with the distribution;
262292SN/A * neither the name of the copyright holders nor the names of its
272292SN/A * contributors may be used to endorse or promote products derived from
282292SN/A * this software without specific prior written permission.
292292SN/A *
302292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
312292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
322292SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
332292SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
342292SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
352292SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
362292SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
372292SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
382292SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
392292SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
402292SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
412689Sktlim@umich.edu *
422689Sktlim@umich.edu * Authors: Kevin Lim
432689Sktlim@umich.edu *          Korey Sewell
442292SN/A */
452292SN/A
469944Smatt.horsnell@ARM.com#ifndef __CPU_O3_LSQ_UNIT_IMPL_HH__
479944Smatt.horsnell@ARM.com#define __CPU_O3_LSQ_UNIT_IMPL_HH__
489944Smatt.horsnell@ARM.com
498591Sgblack@eecs.umich.edu#include "arch/generic/debugfaults.hh"
503326Sktlim@umich.edu#include "arch/locked_mem.hh"
518229Snate@binkert.org#include "base/str.hh"
526658Snate@binkert.org#include "config/the_isa.hh"
538887Sgeoffrey.blake@arm.com#include "cpu/checker/cpu.hh"
542907Sktlim@umich.edu#include "cpu/o3/lsq.hh"
552292SN/A#include "cpu/o3/lsq_unit.hh"
568232Snate@binkert.org#include "debug/Activity.hh"
578232Snate@binkert.org#include "debug/IEW.hh"
588232Snate@binkert.org#include "debug/LSQUnit.hh"
599527SMatt.Horsnell@arm.com#include "debug/O3PipeView.hh"
602722Sktlim@umich.edu#include "mem/packet.hh"
612669Sktlim@umich.edu#include "mem/request.hh"
622292SN/A
632669Sktlim@umich.edutemplate<class Impl>
6413429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::WritebackEvent::WritebackEvent(const DynInstPtr &_inst,
6513429Srekai.gonzalezalberquilla@arm.com        PacketPtr _pkt, LSQUnit *lsq_ptr)
668581Ssteve.reinhardt@amd.com    : Event(Default_Pri, AutoDelete),
678581Ssteve.reinhardt@amd.com      inst(_inst), pkt(_pkt), lsqPtr(lsq_ptr)
682292SN/A{
692292SN/A}
702292SN/A
712669Sktlim@umich.edutemplate<class Impl>
722292SN/Avoid
732678Sktlim@umich.eduLSQUnit<Impl>::WritebackEvent::process()
742292SN/A{
759444SAndreas.Sandberg@ARM.com    assert(!lsqPtr->cpu->switchedOut());
769444SAndreas.Sandberg@ARM.com
779444SAndreas.Sandberg@ARM.com    lsqPtr->writeback(inst, pkt);
784319Sktlim@umich.edu
794319Sktlim@umich.edu    if (pkt->senderState)
804319Sktlim@umich.edu        delete pkt->senderState;
814319Sktlim@umich.edu
822678Sktlim@umich.edu    delete pkt;
832678Sktlim@umich.edu}
842292SN/A
852678Sktlim@umich.edutemplate<class Impl>
862678Sktlim@umich.educonst char *
875336Shines@cs.fsu.eduLSQUnit<Impl>::WritebackEvent::description() const
882678Sktlim@umich.edu{
894873Sstever@eecs.umich.edu    return "Store writeback";
902678Sktlim@umich.edu}
912292SN/A
922678Sktlim@umich.edutemplate<class Impl>
932678Sktlim@umich.eduvoid
942678Sktlim@umich.eduLSQUnit<Impl>::completeDataAccess(PacketPtr pkt)
952678Sktlim@umich.edu{
962678Sktlim@umich.edu    LSQSenderState *state = dynamic_cast<LSQSenderState *>(pkt->senderState);
972678Sktlim@umich.edu    DynInstPtr inst = state->inst;
987852SMatt.Horsnell@arm.com    DPRINTF(IEW, "Writeback event [sn:%lli].\n", inst->seqNum);
997852SMatt.Horsnell@arm.com    DPRINTF(Activity, "Activity: Writeback event [sn:%lli].\n", inst->seqNum);
1002344SN/A
10110333Smitch.hayenga@arm.com    if (state->cacheBlocked) {
10210333Smitch.hayenga@arm.com        // This is the first half of a previous split load,
10310333Smitch.hayenga@arm.com        // where the 2nd half blocked, ignore this response
10410333Smitch.hayenga@arm.com        DPRINTF(IEW, "[sn:%lli]: Response from first half of earlier "
10510333Smitch.hayenga@arm.com                "blocked split load recieved. Ignoring.\n", inst->seqNum);
10610333Smitch.hayenga@arm.com        delete state;
10710333Smitch.hayenga@arm.com        return;
10810333Smitch.hayenga@arm.com    }
1092678Sktlim@umich.edu
1106974Stjones1@inf.ed.ac.uk    // If this is a split access, wait until all packets are received.
1116974Stjones1@inf.ed.ac.uk    if (TheISA::HasUnalignedMemAcc && !state->complete()) {
1126974Stjones1@inf.ed.ac.uk        return;
1136974Stjones1@inf.ed.ac.uk    }
1146974Stjones1@inf.ed.ac.uk
1159444SAndreas.Sandberg@ARM.com    assert(!cpu->switchedOut());
11610327Smitch.hayenga@arm.com    if (!inst->isSquashed()) {
1172678Sktlim@umich.edu        if (!state->noWB) {
11812216Snikos.nikoleris@arm.com            // Only loads and store conditionals perform the writeback
11912216Snikos.nikoleris@arm.com            // after receving the response from the memory
12012216Snikos.nikoleris@arm.com            assert(inst->isLoad() || inst->isStoreConditional());
1216974Stjones1@inf.ed.ac.uk            if (!TheISA::HasUnalignedMemAcc || !state->isSplit ||
1226974Stjones1@inf.ed.ac.uk                !state->isLoad) {
1236974Stjones1@inf.ed.ac.uk                writeback(inst, pkt);
1246974Stjones1@inf.ed.ac.uk            } else {
1256974Stjones1@inf.ed.ac.uk                writeback(inst, state->mainPkt);
1266974Stjones1@inf.ed.ac.uk            }
1272678Sktlim@umich.edu        }
1282678Sktlim@umich.edu
1292678Sktlim@umich.edu        if (inst->isStore()) {
1302678Sktlim@umich.edu            completeStore(state->idx);
1312678Sktlim@umich.edu        }
1322344SN/A    }
1332307SN/A
1346974Stjones1@inf.ed.ac.uk    if (TheISA::HasUnalignedMemAcc && state->isSplit && state->isLoad) {
1356974Stjones1@inf.ed.ac.uk        delete state->mainPkt;
1366974Stjones1@inf.ed.ac.uk    }
13710020Smatt.horsnell@ARM.com
13810020Smatt.horsnell@ARM.com    pkt->req->setAccessLatency();
13910023Smatt.horsnell@ARM.com    cpu->ppDataAccessComplete->notify(std::make_pair(inst, pkt));
14010023Smatt.horsnell@ARM.com
1412678Sktlim@umich.edu    delete state;
1422292SN/A}
1432292SN/A
1442292SN/Atemplate <class Impl>
14513472Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::LSQUnit(uint32_t lqEntries, uint32_t sqEntries)
14613472Srekai.gonzalezalberquilla@arm.com    : lsqID(-1), storeQueue(sqEntries+1), loadQueue(lqEntries+1),
14713472Srekai.gonzalezalberquilla@arm.com      LQEntries(lqEntries+1), SQEntries(lqEntries+1),
14813472Srekai.gonzalezalberquilla@arm.com      loads(0), stores(0), storesToWB(0), cacheBlockMask(0), stalled(false),
14911243Spau.cabre@metempsy.com      isStoreBlocked(false), storeInFlight(false), hasPendingPkt(false),
15011243Spau.cabre@metempsy.com      pendingPkt(nullptr)
1512292SN/A{
1522292SN/A}
1532292SN/A
1542292SN/Atemplate<class Impl>
1552292SN/Avoid
1565529Snate@binkert.orgLSQUnit<Impl>::init(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params,
15713472Srekai.gonzalezalberquilla@arm.com        LSQ *lsq_ptr, unsigned id)
1582292SN/A{
15913472Srekai.gonzalezalberquilla@arm.com    lsqID = id;
16013472Srekai.gonzalezalberquilla@arm.com
1614329Sktlim@umich.edu    cpu = cpu_ptr;
1624329Sktlim@umich.edu    iewStage = iew_ptr;
1634329Sktlim@umich.edu
1642907Sktlim@umich.edu    lsq = lsq_ptr;
1652907Sktlim@umich.edu
16613472Srekai.gonzalezalberquilla@arm.com    DPRINTF(LSQUnit, "Creating LSQUnit%i object.\n",lsqID);
1672292SN/A
1688199SAli.Saidi@ARM.com    depCheckShift = params->LSQDepCheckShift;
1698199SAli.Saidi@ARM.com    checkLoads = params->LSQCheckLoads;
17011780Sarthur.perais@inria.fr    cacheStorePorts = params->cacheStorePorts;
1719444SAndreas.Sandberg@ARM.com    needsTSO = params->needsTSO;
1729444SAndreas.Sandberg@ARM.com
1739444SAndreas.Sandberg@ARM.com    resetState();
1749444SAndreas.Sandberg@ARM.com}
1759444SAndreas.Sandberg@ARM.com
1769444SAndreas.Sandberg@ARM.com
1779444SAndreas.Sandberg@ARM.comtemplate<class Impl>
1789444SAndreas.Sandberg@ARM.comvoid
1799444SAndreas.Sandberg@ARM.comLSQUnit<Impl>::resetState()
1809444SAndreas.Sandberg@ARM.com{
1819444SAndreas.Sandberg@ARM.com    loads = stores = storesToWB = 0;
1828199SAli.Saidi@ARM.com
1832292SN/A    loadHead = loadTail = 0;
1842292SN/A
1852292SN/A    storeHead = storeWBIdx = storeTail = 0;
1862292SN/A
18711780Sarthur.perais@inria.fr    usedStorePorts = 0;
1882292SN/A
1893492Sktlim@umich.edu    retryPkt = NULL;
1902329SN/A    memDepViolator = NULL;
1912292SN/A
1929444SAndreas.Sandberg@ARM.com    stalled = false;
1939444SAndreas.Sandberg@ARM.com
1949814Sandreas.hansson@arm.com    cacheBlockMask = ~(cpu->cacheLineSize() - 1);
1952292SN/A}
1962292SN/A
1972292SN/Atemplate<class Impl>
1982292SN/Astd::string
1992292SN/ALSQUnit<Impl>::name() const
2002292SN/A{
2012292SN/A    if (Impl::MaxThreads == 1) {
2022292SN/A        return iewStage->name() + ".lsq";
2032292SN/A    } else {
20410386Sandreas.hansson@arm.com        return iewStage->name() + ".lsq.thread" + std::to_string(lsqID);
2052292SN/A    }
2062292SN/A}
2072292SN/A
2082292SN/Atemplate<class Impl>
2092292SN/Avoid
2102727Sktlim@umich.eduLSQUnit<Impl>::regStats()
2112727Sktlim@umich.edu{
2122727Sktlim@umich.edu    lsqForwLoads
2132727Sktlim@umich.edu        .name(name() + ".forwLoads")
2142727Sktlim@umich.edu        .desc("Number of loads that had data forwarded from stores");
2152727Sktlim@umich.edu
2162727Sktlim@umich.edu    invAddrLoads
2172727Sktlim@umich.edu        .name(name() + ".invAddrLoads")
2182727Sktlim@umich.edu        .desc("Number of loads ignored due to an invalid address");
2192727Sktlim@umich.edu
2202727Sktlim@umich.edu    lsqSquashedLoads
2212727Sktlim@umich.edu        .name(name() + ".squashedLoads")
2222727Sktlim@umich.edu        .desc("Number of loads squashed");
2232727Sktlim@umich.edu
2242727Sktlim@umich.edu    lsqIgnoredResponses
2252727Sktlim@umich.edu        .name(name() + ".ignoredResponses")
2262727Sktlim@umich.edu        .desc("Number of memory responses ignored because the instruction is squashed");
2272727Sktlim@umich.edu
2282361SN/A    lsqMemOrderViolation
2292361SN/A        .name(name() + ".memOrderViolation")
2302361SN/A        .desc("Number of memory ordering violations");
2312361SN/A
2322727Sktlim@umich.edu    lsqSquashedStores
2332727Sktlim@umich.edu        .name(name() + ".squashedStores")
2342727Sktlim@umich.edu        .desc("Number of stores squashed");
2352727Sktlim@umich.edu
2362727Sktlim@umich.edu    invAddrSwpfs
2372727Sktlim@umich.edu        .name(name() + ".invAddrSwpfs")
2382727Sktlim@umich.edu        .desc("Number of software prefetches ignored due to an invalid address");
2392727Sktlim@umich.edu
2402727Sktlim@umich.edu    lsqBlockedLoads
2412727Sktlim@umich.edu        .name(name() + ".blockedLoads")
2422727Sktlim@umich.edu        .desc("Number of blocked loads due to partial load-store forwarding");
2432727Sktlim@umich.edu
2442727Sktlim@umich.edu    lsqRescheduledLoads
2452727Sktlim@umich.edu        .name(name() + ".rescheduledLoads")
2462727Sktlim@umich.edu        .desc("Number of loads that were rescheduled");
2472727Sktlim@umich.edu
2482727Sktlim@umich.edu    lsqCacheBlocked
2492727Sktlim@umich.edu        .name(name() + ".cacheBlocked")
2502727Sktlim@umich.edu        .desc("Number of times an access to memory failed due to the cache being blocked");
2512727Sktlim@umich.edu}
2522727Sktlim@umich.edu
2532727Sktlim@umich.edutemplate<class Impl>
2542727Sktlim@umich.eduvoid
2558922Swilliam.wang@arm.comLSQUnit<Impl>::setDcachePort(MasterPort *dcache_port)
2564329Sktlim@umich.edu{
2574329Sktlim@umich.edu    dcachePort = dcache_port;
2584329Sktlim@umich.edu}
2594329Sktlim@umich.edu
2604329Sktlim@umich.edutemplate<class Impl>
2614329Sktlim@umich.eduvoid
2622292SN/ALSQUnit<Impl>::clearLQ()
2632292SN/A{
2642292SN/A    loadQueue.clear();
2652292SN/A}
2662292SN/A
2672292SN/Atemplate<class Impl>
2682292SN/Avoid
2692292SN/ALSQUnit<Impl>::clearSQ()
2702292SN/A{
2712292SN/A    storeQueue.clear();
2722292SN/A}
2732292SN/A
2742292SN/Atemplate<class Impl>
2752292SN/Avoid
2769444SAndreas.Sandberg@ARM.comLSQUnit<Impl>::drainSanityCheck() const
2772307SN/A{
2789444SAndreas.Sandberg@ARM.com    for (int i = 0; i < loadQueue.size(); ++i)
2792367SN/A        assert(!loadQueue[i]);
2802307SN/A
2812329SN/A    assert(storesToWB == 0);
2829444SAndreas.Sandberg@ARM.com    assert(!retryPkt);
2832307SN/A}
2842307SN/A
2852307SN/Atemplate<class Impl>
2862307SN/Avoid
2872307SN/ALSQUnit<Impl>::takeOverFrom()
2882307SN/A{
2899444SAndreas.Sandberg@ARM.com    resetState();
2902307SN/A}
2912307SN/A
2922307SN/Atemplate<class Impl>
2932307SN/Avoid
2942292SN/ALSQUnit<Impl>::resizeLQ(unsigned size)
2952292SN/A{
2962329SN/A    unsigned size_plus_sentinel = size + 1;
2972329SN/A    assert(size_plus_sentinel >= LQEntries);
2982292SN/A
2992329SN/A    if (size_plus_sentinel > LQEntries) {
3002329SN/A        while (size_plus_sentinel > loadQueue.size()) {
3012292SN/A            DynInstPtr dummy;
3022292SN/A            loadQueue.push_back(dummy);
3032292SN/A            LQEntries++;
3042292SN/A        }
3052292SN/A    } else {
3062329SN/A        LQEntries = size_plus_sentinel;
3072292SN/A    }
3082292SN/A
3099936SFaissal.Sleiman@arm.com    assert(LQEntries <= 256);
3102292SN/A}
3112292SN/A
3122292SN/Atemplate<class Impl>
3132292SN/Avoid
3142292SN/ALSQUnit<Impl>::resizeSQ(unsigned size)
3152292SN/A{
3162329SN/A    unsigned size_plus_sentinel = size + 1;
3172329SN/A    if (size_plus_sentinel > SQEntries) {
3182329SN/A        while (size_plus_sentinel > storeQueue.size()) {
3192292SN/A            SQEntry dummy;
3202292SN/A            storeQueue.push_back(dummy);
3212292SN/A            SQEntries++;
3222292SN/A        }
3232292SN/A    } else {
3242329SN/A        SQEntries = size_plus_sentinel;
3252292SN/A    }
3269936SFaissal.Sleiman@arm.com
3279936SFaissal.Sleiman@arm.com    assert(SQEntries <= 256);
3282292SN/A}
3292292SN/A
3302292SN/Atemplate <class Impl>
3312292SN/Avoid
33213429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::insert(const DynInstPtr &inst)
3332292SN/A{
3342292SN/A    assert(inst->isMemRef());
3352292SN/A
3362292SN/A    assert(inst->isLoad() || inst->isStore());
3372292SN/A
3382292SN/A    if (inst->isLoad()) {
3392292SN/A        insertLoad(inst);
3402292SN/A    } else {
3412292SN/A        insertStore(inst);
3422292SN/A    }
3432292SN/A
3442292SN/A    inst->setInLSQ();
3452292SN/A}
3462292SN/A
3472292SN/Atemplate <class Impl>
3482292SN/Avoid
34913429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::insertLoad(const DynInstPtr &load_inst)
3502292SN/A{
3512329SN/A    assert((loadTail + 1) % LQEntries != loadHead);
3522329SN/A    assert(loads < LQEntries);
3532292SN/A
3547720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Inserting load PC %s, idx:%i [sn:%lli]\n",
3557720Sgblack@eecs.umich.edu            load_inst->pcState(), loadTail, load_inst->seqNum);
3562292SN/A
3572292SN/A    load_inst->lqIdx = loadTail;
3582292SN/A
3592292SN/A    if (stores == 0) {
3602292SN/A        load_inst->sqIdx = -1;
3612292SN/A    } else {
3622292SN/A        load_inst->sqIdx = storeTail;
3632292SN/A    }
3642292SN/A
3652292SN/A    loadQueue[loadTail] = load_inst;
3662292SN/A
3672292SN/A    incrLdIdx(loadTail);
3682292SN/A
3692292SN/A    ++loads;
3702292SN/A}
3712292SN/A
3722292SN/Atemplate <class Impl>
3732292SN/Avoid
37413429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::insertStore(const DynInstPtr &store_inst)
3752292SN/A{
3762292SN/A    // Make sure it is not full before inserting an instruction.
3772292SN/A    assert((storeTail + 1) % SQEntries != storeHead);
3782292SN/A    assert(stores < SQEntries);
3792292SN/A
3807720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Inserting store PC %s, idx:%i [sn:%lli]\n",
3817720Sgblack@eecs.umich.edu            store_inst->pcState(), storeTail, store_inst->seqNum);
3822292SN/A
3832292SN/A    store_inst->sqIdx = storeTail;
3842292SN/A    store_inst->lqIdx = loadTail;
3852292SN/A
3862292SN/A    storeQueue[storeTail] = SQEntry(store_inst);
3872292SN/A
3882292SN/A    incrStIdx(storeTail);
3892292SN/A
3902292SN/A    ++stores;
3912292SN/A}
3922292SN/A
3932292SN/Atemplate <class Impl>
3942292SN/Atypename Impl::DynInstPtr
3952292SN/ALSQUnit<Impl>::getMemDepViolator()
3962292SN/A{
3972292SN/A    DynInstPtr temp = memDepViolator;
3982292SN/A
3992292SN/A    memDepViolator = NULL;
4002292SN/A
4012292SN/A    return temp;
4022292SN/A}
4032292SN/A
4042292SN/Atemplate <class Impl>
4052292SN/Aunsigned
40610239Sbinhpham@cs.rutgers.eduLSQUnit<Impl>::numFreeLoadEntries()
4072292SN/A{
40810239Sbinhpham@cs.rutgers.edu        //LQ has an extra dummy entry to differentiate
40910239Sbinhpham@cs.rutgers.edu        //empty/full conditions. Subtract 1 from the free entries.
41010239Sbinhpham@cs.rutgers.edu        DPRINTF(LSQUnit, "LQ size: %d, #loads occupied: %d\n", LQEntries, loads);
41110239Sbinhpham@cs.rutgers.edu        return LQEntries - loads - 1;
41210239Sbinhpham@cs.rutgers.edu}
4132292SN/A
41410239Sbinhpham@cs.rutgers.edutemplate <class Impl>
41510239Sbinhpham@cs.rutgers.eduunsigned
41610239Sbinhpham@cs.rutgers.eduLSQUnit<Impl>::numFreeStoreEntries()
41710239Sbinhpham@cs.rutgers.edu{
41810239Sbinhpham@cs.rutgers.edu        //SQ has an extra dummy entry to differentiate
41910239Sbinhpham@cs.rutgers.edu        //empty/full conditions. Subtract 1 from the free entries.
42010239Sbinhpham@cs.rutgers.edu        DPRINTF(LSQUnit, "SQ size: %d, #stores occupied: %d\n", SQEntries, stores);
42110239Sbinhpham@cs.rutgers.edu        return SQEntries - stores - 1;
42210239Sbinhpham@cs.rutgers.edu
42310239Sbinhpham@cs.rutgers.edu }
4242292SN/A
4252292SN/Atemplate <class Impl>
4268545Ssaidi@eecs.umich.eduvoid
4278545Ssaidi@eecs.umich.eduLSQUnit<Impl>::checkSnoop(PacketPtr pkt)
4288545Ssaidi@eecs.umich.edu{
42911357Sstephan.diestelhorst@arm.com    // Should only ever get invalidations in here
43011357Sstephan.diestelhorst@arm.com    assert(pkt->isInvalidate());
43111357Sstephan.diestelhorst@arm.com
4328545Ssaidi@eecs.umich.edu    int load_idx = loadHead;
43310030SAli.Saidi@ARM.com    DPRINTF(LSQUnit, "Got snoop for address %#x\n", pkt->getAddr());
4348545Ssaidi@eecs.umich.edu
43511356Skrinat01@arm.com    // Only Invalidate packet calls checkSnoop
43611356Skrinat01@arm.com    assert(pkt->isInvalidate());
43710030SAli.Saidi@ARM.com    for (int x = 0; x < cpu->numContexts(); x++) {
4389383SAli.Saidi@ARM.com        ThreadContext *tc = cpu->getContext(x);
4399383SAli.Saidi@ARM.com        bool no_squash = cpu->thread[x]->noSquashFromTC;
4409383SAli.Saidi@ARM.com        cpu->thread[x]->noSquashFromTC = true;
4419383SAli.Saidi@ARM.com        TheISA::handleLockedSnoop(tc, pkt, cacheBlockMask);
4429383SAli.Saidi@ARM.com        cpu->thread[x]->noSquashFromTC = no_squash;
4439383SAli.Saidi@ARM.com    }
4449383SAli.Saidi@ARM.com
44510030SAli.Saidi@ARM.com    Addr invalidate_addr = pkt->getAddr() & cacheBlockMask;
44610030SAli.Saidi@ARM.com
44710030SAli.Saidi@ARM.com    DynInstPtr ld_inst = loadQueue[load_idx];
44810030SAli.Saidi@ARM.com    if (ld_inst) {
44911097Songal@cs.wisc.edu        Addr load_addr_low = ld_inst->physEffAddrLow & cacheBlockMask;
45011097Songal@cs.wisc.edu        Addr load_addr_high = ld_inst->physEffAddrHigh & cacheBlockMask;
45111097Songal@cs.wisc.edu
45210030SAli.Saidi@ARM.com        // Check that this snoop didn't just invalidate our lock flag
45311097Songal@cs.wisc.edu        if (ld_inst->effAddrValid() && (load_addr_low == invalidate_addr
45411097Songal@cs.wisc.edu                                        || load_addr_high == invalidate_addr)
45511097Songal@cs.wisc.edu            && ld_inst->memReqFlags & Request::LLSC)
45610030SAli.Saidi@ARM.com            TheISA::handleLockedSnoopHit(ld_inst.get());
45710030SAli.Saidi@ARM.com    }
45810030SAli.Saidi@ARM.com
4598545Ssaidi@eecs.umich.edu    // If this is the only load in the LSQ we don't care
4608545Ssaidi@eecs.umich.edu    if (load_idx == loadTail)
4618545Ssaidi@eecs.umich.edu        return;
46210030SAli.Saidi@ARM.com
4638545Ssaidi@eecs.umich.edu    incrLdIdx(load_idx);
4648545Ssaidi@eecs.umich.edu
46510149Smarco.elver@ed.ac.uk    bool force_squash = false;
46610149Smarco.elver@ed.ac.uk
4678545Ssaidi@eecs.umich.edu    while (load_idx != loadTail) {
4688545Ssaidi@eecs.umich.edu        DynInstPtr ld_inst = loadQueue[load_idx];
4698545Ssaidi@eecs.umich.edu
47010824SAndreas.Sandberg@ARM.com        if (!ld_inst->effAddrValid() || ld_inst->strictlyOrdered()) {
4718545Ssaidi@eecs.umich.edu            incrLdIdx(load_idx);
4728545Ssaidi@eecs.umich.edu            continue;
4738545Ssaidi@eecs.umich.edu        }
4748545Ssaidi@eecs.umich.edu
47511097Songal@cs.wisc.edu        Addr load_addr_low = ld_inst->physEffAddrLow & cacheBlockMask;
47611097Songal@cs.wisc.edu        Addr load_addr_high = ld_inst->physEffAddrHigh & cacheBlockMask;
47711097Songal@cs.wisc.edu
4788545Ssaidi@eecs.umich.edu        DPRINTF(LSQUnit, "-- inst [sn:%lli] load_addr: %#x to pktAddr:%#x\n",
47911097Songal@cs.wisc.edu                    ld_inst->seqNum, load_addr_low, invalidate_addr);
4808545Ssaidi@eecs.umich.edu
48111097Songal@cs.wisc.edu        if ((load_addr_low == invalidate_addr
48211097Songal@cs.wisc.edu             || load_addr_high == invalidate_addr) || force_squash) {
48310149Smarco.elver@ed.ac.uk            if (needsTSO) {
48410149Smarco.elver@ed.ac.uk                // If we have a TSO system, as all loads must be ordered with
48510149Smarco.elver@ed.ac.uk                // all other loads, this load as well as *all* subsequent loads
48610149Smarco.elver@ed.ac.uk                // need to be squashed to prevent possible load reordering.
48710149Smarco.elver@ed.ac.uk                force_squash = true;
48810149Smarco.elver@ed.ac.uk            }
48910149Smarco.elver@ed.ac.uk            if (ld_inst->possibleLoadViolation() || force_squash) {
4908545Ssaidi@eecs.umich.edu                DPRINTF(LSQUnit, "Conflicting load at addr %#x [sn:%lli]\n",
49110030SAli.Saidi@ARM.com                        pkt->getAddr(), ld_inst->seqNum);
4928545Ssaidi@eecs.umich.edu
4938545Ssaidi@eecs.umich.edu                // Mark the load for re-execution
49410474Sandreas.hansson@arm.com                ld_inst->fault = std::make_shared<ReExec>();
4958545Ssaidi@eecs.umich.edu            } else {
49610030SAli.Saidi@ARM.com                DPRINTF(LSQUnit, "HitExternal Snoop for addr %#x [sn:%lli]\n",
49710030SAli.Saidi@ARM.com                        pkt->getAddr(), ld_inst->seqNum);
49810030SAli.Saidi@ARM.com
49910030SAli.Saidi@ARM.com                // Make sure that we don't lose a snoop hitting a LOCKED
50010030SAli.Saidi@ARM.com                // address since the LOCK* flags don't get updated until
50110030SAli.Saidi@ARM.com                // commit.
50210030SAli.Saidi@ARM.com                if (ld_inst->memReqFlags & Request::LLSC)
50310030SAli.Saidi@ARM.com                    TheISA::handleLockedSnoopHit(ld_inst.get());
50410030SAli.Saidi@ARM.com
5058545Ssaidi@eecs.umich.edu                // If a older load checks this and it's true
5068545Ssaidi@eecs.umich.edu                // then we might have missed the snoop
5078545Ssaidi@eecs.umich.edu                // in which case we need to invalidate to be sure
5089046SAli.Saidi@ARM.com                ld_inst->hitExternalSnoop(true);
5098545Ssaidi@eecs.umich.edu            }
5108545Ssaidi@eecs.umich.edu        }
5118545Ssaidi@eecs.umich.edu        incrLdIdx(load_idx);
5128545Ssaidi@eecs.umich.edu    }
5138545Ssaidi@eecs.umich.edu    return;
5148545Ssaidi@eecs.umich.edu}
5158545Ssaidi@eecs.umich.edu
5168545Ssaidi@eecs.umich.edutemplate <class Impl>
5172292SN/AFault
51813429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::checkViolations(int load_idx, const DynInstPtr &inst)
5198199SAli.Saidi@ARM.com{
5208199SAli.Saidi@ARM.com    Addr inst_eff_addr1 = inst->effAddr >> depCheckShift;
5218199SAli.Saidi@ARM.com    Addr inst_eff_addr2 = (inst->effAddr + inst->effSize - 1) >> depCheckShift;
5228199SAli.Saidi@ARM.com
5238199SAli.Saidi@ARM.com    /** @todo in theory you only need to check an instruction that has executed
5248199SAli.Saidi@ARM.com     * however, there isn't a good way in the pipeline at the moment to check
5258199SAli.Saidi@ARM.com     * all instructions that will execute before the store writes back. Thus,
5268199SAli.Saidi@ARM.com     * like the implementation that came before it, we're overly conservative.
5278199SAli.Saidi@ARM.com     */
5288199SAli.Saidi@ARM.com    while (load_idx != loadTail) {
5298199SAli.Saidi@ARM.com        DynInstPtr ld_inst = loadQueue[load_idx];
53010824SAndreas.Sandberg@ARM.com        if (!ld_inst->effAddrValid() || ld_inst->strictlyOrdered()) {
5318199SAli.Saidi@ARM.com            incrLdIdx(load_idx);
5328199SAli.Saidi@ARM.com            continue;
5338199SAli.Saidi@ARM.com        }
5348199SAli.Saidi@ARM.com
5358199SAli.Saidi@ARM.com        Addr ld_eff_addr1 = ld_inst->effAddr >> depCheckShift;
5368199SAli.Saidi@ARM.com        Addr ld_eff_addr2 =
5378199SAli.Saidi@ARM.com            (ld_inst->effAddr + ld_inst->effSize - 1) >> depCheckShift;
5388199SAli.Saidi@ARM.com
5398272SAli.Saidi@ARM.com        if (inst_eff_addr2 >= ld_eff_addr1 && inst_eff_addr1 <= ld_eff_addr2) {
5408545Ssaidi@eecs.umich.edu            if (inst->isLoad()) {
5418545Ssaidi@eecs.umich.edu                // If this load is to the same block as an external snoop
5428545Ssaidi@eecs.umich.edu                // invalidate that we've observed then the load needs to be
5438545Ssaidi@eecs.umich.edu                // squashed as it could have newer data
5449046SAli.Saidi@ARM.com                if (ld_inst->hitExternalSnoop()) {
5458545Ssaidi@eecs.umich.edu                    if (!memDepViolator ||
5468545Ssaidi@eecs.umich.edu                            ld_inst->seqNum < memDepViolator->seqNum) {
5478545Ssaidi@eecs.umich.edu                        DPRINTF(LSQUnit, "Detected fault with inst [sn:%lli] "
5488592Sgblack@eecs.umich.edu                                "and [sn:%lli] at address %#x\n",
5498592Sgblack@eecs.umich.edu                                inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
5508545Ssaidi@eecs.umich.edu                        memDepViolator = ld_inst;
5518199SAli.Saidi@ARM.com
5528545Ssaidi@eecs.umich.edu                        ++lsqMemOrderViolation;
5538199SAli.Saidi@ARM.com
55410474Sandreas.hansson@arm.com                        return std::make_shared<GenericISA::M5PanicFault>(
55510474Sandreas.hansson@arm.com                            "Detected fault with inst [sn:%lli] and "
55610474Sandreas.hansson@arm.com                            "[sn:%lli] at address %#x\n",
55710474Sandreas.hansson@arm.com                            inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
5588545Ssaidi@eecs.umich.edu                    }
5598545Ssaidi@eecs.umich.edu                }
5608199SAli.Saidi@ARM.com
5618545Ssaidi@eecs.umich.edu                // Otherwise, mark the load has a possible load violation
5628545Ssaidi@eecs.umich.edu                // and if we see a snoop before it's commited, we need to squash
5639046SAli.Saidi@ARM.com                ld_inst->possibleLoadViolation(true);
56410575SMarco.Elver@ARM.com                DPRINTF(LSQUnit, "Found possible load violation at addr: %#x"
5658545Ssaidi@eecs.umich.edu                        " between instructions [sn:%lli] and [sn:%lli]\n",
5668545Ssaidi@eecs.umich.edu                        inst_eff_addr1, inst->seqNum, ld_inst->seqNum);
5678545Ssaidi@eecs.umich.edu            } else {
5688545Ssaidi@eecs.umich.edu                // A load/store incorrectly passed this store.
5698545Ssaidi@eecs.umich.edu                // Check if we already have a violator, or if it's newer
5708545Ssaidi@eecs.umich.edu                // squash and refetch.
5718545Ssaidi@eecs.umich.edu                if (memDepViolator && ld_inst->seqNum > memDepViolator->seqNum)
5728545Ssaidi@eecs.umich.edu                    break;
5738545Ssaidi@eecs.umich.edu
5748592Sgblack@eecs.umich.edu                DPRINTF(LSQUnit, "Detected fault with inst [sn:%lli] and "
5758592Sgblack@eecs.umich.edu                        "[sn:%lli] at address %#x\n",
5768592Sgblack@eecs.umich.edu                        inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
5778545Ssaidi@eecs.umich.edu                memDepViolator = ld_inst;
5788545Ssaidi@eecs.umich.edu
5798545Ssaidi@eecs.umich.edu                ++lsqMemOrderViolation;
5808545Ssaidi@eecs.umich.edu
58110474Sandreas.hansson@arm.com                return std::make_shared<GenericISA::M5PanicFault>(
58210474Sandreas.hansson@arm.com                    "Detected fault with "
58310474Sandreas.hansson@arm.com                    "inst [sn:%lli] and [sn:%lli] at address %#x\n",
58410474Sandreas.hansson@arm.com                    inst->seqNum, ld_inst->seqNum, ld_eff_addr1);
5858545Ssaidi@eecs.umich.edu            }
5868199SAli.Saidi@ARM.com        }
5878199SAli.Saidi@ARM.com
5888199SAli.Saidi@ARM.com        incrLdIdx(load_idx);
5898199SAli.Saidi@ARM.com    }
5908199SAli.Saidi@ARM.com    return NoFault;
5918199SAli.Saidi@ARM.com}
5928199SAli.Saidi@ARM.com
5938199SAli.Saidi@ARM.com
5948199SAli.Saidi@ARM.com
5958199SAli.Saidi@ARM.com
5968199SAli.Saidi@ARM.comtemplate <class Impl>
5978199SAli.Saidi@ARM.comFault
59813429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::executeLoad(const DynInstPtr &inst)
5992292SN/A{
6004032Sktlim@umich.edu    using namespace TheISA;
6012292SN/A    // Execute a specific load.
6022292SN/A    Fault load_fault = NoFault;
6032292SN/A
6047720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Executing load PC %s, [sn:%lli]\n",
6057944SGiacomo.Gabrielli@arm.com            inst->pcState(), inst->seqNum);
6062292SN/A
6074032Sktlim@umich.edu    assert(!inst->isSquashed());
6084032Sktlim@umich.edu
6092669Sktlim@umich.edu    load_fault = inst->initiateAcc();
6102292SN/A
6117944SGiacomo.Gabrielli@arm.com    if (inst->isTranslationDelayed() &&
6127944SGiacomo.Gabrielli@arm.com        load_fault == NoFault)
6137944SGiacomo.Gabrielli@arm.com        return load_fault;
6147944SGiacomo.Gabrielli@arm.com
6157597Sminkyu.jeong@arm.com    // If the instruction faulted or predicated false, then we need to send it
6167597Sminkyu.jeong@arm.com    // along to commit without the instruction completing.
61710231Ssteve.reinhardt@amd.com    if (load_fault != NoFault || !inst->readPredicate()) {
6182329SN/A        // Send this instruction to commit, also make sure iew stage
61910824SAndreas.Sandberg@ARM.com        // realizes there is activity.  Mark it as executed unless it
62010824SAndreas.Sandberg@ARM.com        // is a strictly ordered load that needs to hit the head of
62110824SAndreas.Sandberg@ARM.com        // commit.
62210231Ssteve.reinhardt@amd.com        if (!inst->readPredicate())
6237848SAli.Saidi@ARM.com            inst->forwardOldRegs();
6247600Sminkyu.jeong@arm.com        DPRINTF(LSQUnit, "Load [sn:%lli] not executed from %s\n",
6257600Sminkyu.jeong@arm.com                inst->seqNum,
6267600Sminkyu.jeong@arm.com                (load_fault != NoFault ? "fault" : "predication"));
62710824SAndreas.Sandberg@ARM.com        if (!(inst->hasRequest() && inst->strictlyOrdered()) ||
6283731Sktlim@umich.edu            inst->isAtCommit()) {
6292367SN/A            inst->setExecuted();
6302367SN/A        }
6312292SN/A        iewStage->instToCommit(inst);
6322292SN/A        iewStage->activityThisCycle();
63310333Smitch.hayenga@arm.com    } else {
6349046SAli.Saidi@ARM.com        assert(inst->effAddrValid());
6354032Sktlim@umich.edu        int load_idx = inst->lqIdx;
6364032Sktlim@umich.edu        incrLdIdx(load_idx);
6374032Sktlim@umich.edu
6388199SAli.Saidi@ARM.com        if (checkLoads)
6398199SAli.Saidi@ARM.com            return checkViolations(load_idx, inst);
6402292SN/A    }
6412292SN/A
6422292SN/A    return load_fault;
6432292SN/A}
6442292SN/A
6452292SN/Atemplate <class Impl>
6462292SN/AFault
64713429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::executeStore(const DynInstPtr &store_inst)
6482292SN/A{
6492292SN/A    using namespace TheISA;
6502292SN/A    // Make sure that a store exists.
6512292SN/A    assert(stores != 0);
6522292SN/A
6532292SN/A    int store_idx = store_inst->sqIdx;
6542292SN/A
6557720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Executing store PC %s [sn:%lli]\n",
6567720Sgblack@eecs.umich.edu            store_inst->pcState(), store_inst->seqNum);
6572292SN/A
6584032Sktlim@umich.edu    assert(!store_inst->isSquashed());
6594032Sktlim@umich.edu
6602292SN/A    // Check the recently completed loads to see if any match this store's
6612292SN/A    // address.  If so, then we have a memory ordering violation.
6622292SN/A    int load_idx = store_inst->lqIdx;
6632292SN/A
6642292SN/A    Fault store_fault = store_inst->initiateAcc();
6652292SN/A
6667944SGiacomo.Gabrielli@arm.com    if (store_inst->isTranslationDelayed() &&
6677944SGiacomo.Gabrielli@arm.com        store_fault == NoFault)
6687944SGiacomo.Gabrielli@arm.com        return store_fault;
6697944SGiacomo.Gabrielli@arm.com
67012217Snikos.nikoleris@arm.com    if (!store_inst->readPredicate()) {
67112217Snikos.nikoleris@arm.com        DPRINTF(LSQUnit, "Store [sn:%lli] not executed from predication\n",
67212217Snikos.nikoleris@arm.com                store_inst->seqNum);
6737848SAli.Saidi@ARM.com        store_inst->forwardOldRegs();
67412217Snikos.nikoleris@arm.com        return store_fault;
67512217Snikos.nikoleris@arm.com    }
6767848SAli.Saidi@ARM.com
6772329SN/A    if (storeQueue[store_idx].size == 0) {
6787782Sminkyu.jeong@arm.com        DPRINTF(LSQUnit,"Fault on Store PC %s, [sn:%lli], Size = 0\n",
6797720Sgblack@eecs.umich.edu                store_inst->pcState(), store_inst->seqNum);
6802292SN/A
6812292SN/A        return store_fault;
6822292SN/A    }
6832292SN/A
6842292SN/A    assert(store_fault == NoFault);
6852292SN/A
6862336SN/A    if (store_inst->isStoreConditional()) {
6872336SN/A        // Store conditionals need to set themselves as able to
6882336SN/A        // writeback if we haven't had a fault by here.
6892329SN/A        storeQueue[store_idx].canWB = true;
6902292SN/A
6912329SN/A        ++storesToWB;
6922292SN/A    }
6932292SN/A
6948199SAli.Saidi@ARM.com    return checkViolations(load_idx, store_inst);
6952292SN/A
6962292SN/A}
6972292SN/A
6982292SN/Atemplate <class Impl>
6992292SN/Avoid
7002292SN/ALSQUnit<Impl>::commitLoad()
7012292SN/A{
7022292SN/A    assert(loadQueue[loadHead]);
7032292SN/A
7047720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Committing head load instruction, PC %s\n",
7057720Sgblack@eecs.umich.edu            loadQueue[loadHead]->pcState());
7062292SN/A
7072292SN/A    loadQueue[loadHead] = NULL;
7082292SN/A
7092292SN/A    incrLdIdx(loadHead);
7102292SN/A
7112292SN/A    --loads;
7122292SN/A}
7132292SN/A
7142292SN/Atemplate <class Impl>
7152292SN/Avoid
7162292SN/ALSQUnit<Impl>::commitLoads(InstSeqNum &youngest_inst)
7172292SN/A{
7182292SN/A    assert(loads == 0 || loadQueue[loadHead]);
7192292SN/A
7202292SN/A    while (loads != 0 && loadQueue[loadHead]->seqNum <= youngest_inst) {
7212292SN/A        commitLoad();
7222292SN/A    }
7232292SN/A}
7242292SN/A
7252292SN/Atemplate <class Impl>
7262292SN/Avoid
7272292SN/ALSQUnit<Impl>::commitStores(InstSeqNum &youngest_inst)
7282292SN/A{
7292292SN/A    assert(stores == 0 || storeQueue[storeHead].inst);
7302292SN/A
7312292SN/A    int store_idx = storeHead;
7322292SN/A
7332292SN/A    while (store_idx != storeTail) {
7342292SN/A        assert(storeQueue[store_idx].inst);
7352329SN/A        // Mark any stores that are now committed and have not yet
7362329SN/A        // been marked as able to write back.
7372292SN/A        if (!storeQueue[store_idx].canWB) {
7382292SN/A            if (storeQueue[store_idx].inst->seqNum > youngest_inst) {
7392292SN/A                break;
7402292SN/A            }
7412292SN/A            DPRINTF(LSQUnit, "Marking store as able to write back, PC "
7427720Sgblack@eecs.umich.edu                    "%s [sn:%lli]\n",
7437720Sgblack@eecs.umich.edu                    storeQueue[store_idx].inst->pcState(),
7442292SN/A                    storeQueue[store_idx].inst->seqNum);
7452292SN/A
7462292SN/A            storeQueue[store_idx].canWB = true;
7472292SN/A
7482292SN/A            ++storesToWB;
7492292SN/A        }
7502292SN/A
7512292SN/A        incrStIdx(store_idx);
7522292SN/A    }
7532292SN/A}
7542292SN/A
7552292SN/Atemplate <class Impl>
7562292SN/Avoid
7576974Stjones1@inf.ed.ac.ukLSQUnit<Impl>::writebackPendingStore()
7586974Stjones1@inf.ed.ac.uk{
7596974Stjones1@inf.ed.ac.uk    if (hasPendingPkt) {
7606974Stjones1@inf.ed.ac.uk        assert(pendingPkt != NULL);
7616974Stjones1@inf.ed.ac.uk
7626974Stjones1@inf.ed.ac.uk        // If the cache is blocked, this will store the packet for retry.
7636974Stjones1@inf.ed.ac.uk        if (sendStore(pendingPkt)) {
7646974Stjones1@inf.ed.ac.uk            storePostSend(pendingPkt);
7656974Stjones1@inf.ed.ac.uk        }
7666974Stjones1@inf.ed.ac.uk        pendingPkt = NULL;
7676974Stjones1@inf.ed.ac.uk        hasPendingPkt = false;
7686974Stjones1@inf.ed.ac.uk    }
7696974Stjones1@inf.ed.ac.uk}
7706974Stjones1@inf.ed.ac.uk
7716974Stjones1@inf.ed.ac.uktemplate <class Impl>
7726974Stjones1@inf.ed.ac.ukvoid
7732292SN/ALSQUnit<Impl>::writebackStores()
7742292SN/A{
7756974Stjones1@inf.ed.ac.uk    // First writeback the second packet from any split store that didn't
7766974Stjones1@inf.ed.ac.uk    // complete last cycle because there weren't enough cache ports available.
7776974Stjones1@inf.ed.ac.uk    if (TheISA::HasUnalignedMemAcc) {
7786974Stjones1@inf.ed.ac.uk        writebackPendingStore();
7796974Stjones1@inf.ed.ac.uk    }
7806974Stjones1@inf.ed.ac.uk
7812292SN/A    while (storesToWB > 0 &&
7822292SN/A           storeWBIdx != storeTail &&
7832292SN/A           storeQueue[storeWBIdx].inst &&
7842292SN/A           storeQueue[storeWBIdx].canWB &&
7858727Snilay@cs.wisc.edu           ((!needsTSO) || (!storeInFlight)) &&
78611780Sarthur.perais@inria.fr           usedStorePorts < cacheStorePorts) {
7872292SN/A
78810333Smitch.hayenga@arm.com        if (isStoreBlocked) {
7892678Sktlim@umich.edu            DPRINTF(LSQUnit, "Unable to write back any more stores, cache"
7902678Sktlim@umich.edu                    " is blocked!\n");
7912678Sktlim@umich.edu            break;
7922678Sktlim@umich.edu        }
7932678Sktlim@umich.edu
7942329SN/A        // Store didn't write any data so no need to write it back to
7952329SN/A        // memory.
7962292SN/A        if (storeQueue[storeWBIdx].size == 0) {
7972292SN/A            completeStore(storeWBIdx);
7982292SN/A
7992292SN/A            incrStIdx(storeWBIdx);
8002292SN/A
8012292SN/A            continue;
8022292SN/A        }
8032678Sktlim@umich.edu
80411780Sarthur.perais@inria.fr        ++usedStorePorts;
8052292SN/A
8062292SN/A        if (storeQueue[storeWBIdx].inst->isDataPrefetch()) {
8072292SN/A            incrStIdx(storeWBIdx);
8082292SN/A
8092292SN/A            continue;
8102292SN/A        }
8112292SN/A
8122292SN/A        assert(storeQueue[storeWBIdx].req);
8132292SN/A        assert(!storeQueue[storeWBIdx].committed);
8142292SN/A
8156974Stjones1@inf.ed.ac.uk        if (TheISA::HasUnalignedMemAcc && storeQueue[storeWBIdx].isSplit) {
8166974Stjones1@inf.ed.ac.uk            assert(storeQueue[storeWBIdx].sreqLow);
8176974Stjones1@inf.ed.ac.uk            assert(storeQueue[storeWBIdx].sreqHigh);
8186974Stjones1@inf.ed.ac.uk        }
8196974Stjones1@inf.ed.ac.uk
8202669Sktlim@umich.edu        DynInstPtr inst = storeQueue[storeWBIdx].inst;
8212669Sktlim@umich.edu
82212749Sgiacomo.travaglini@arm.com        RequestPtr &req = storeQueue[storeWBIdx].req;
82312749Sgiacomo.travaglini@arm.com        const RequestPtr &sreqLow = storeQueue[storeWBIdx].sreqLow;
82412749Sgiacomo.travaglini@arm.com        const RequestPtr &sreqHigh = storeQueue[storeWBIdx].sreqHigh;
8258481Sgblack@eecs.umich.edu
8262292SN/A        storeQueue[storeWBIdx].committed = true;
8272292SN/A
8282669Sktlim@umich.edu        assert(!inst->memData);
82910031SAli.Saidi@ARM.com        inst->memData = new uint8_t[req->getSize()];
8303772Sgblack@eecs.umich.edu
83110031SAli.Saidi@ARM.com        if (storeQueue[storeWBIdx].isAllZeros)
83210031SAli.Saidi@ARM.com            memset(inst->memData, 0, req->getSize());
83310031SAli.Saidi@ARM.com        else
83410031SAli.Saidi@ARM.com            memcpy(inst->memData, storeQueue[storeWBIdx].data, req->getSize());
8352669Sktlim@umich.edu
8366974Stjones1@inf.ed.ac.uk        PacketPtr data_pkt;
8376974Stjones1@inf.ed.ac.uk        PacketPtr snd_data_pkt = NULL;
8382292SN/A
8392678Sktlim@umich.edu        LSQSenderState *state = new LSQSenderState;
8402678Sktlim@umich.edu        state->isLoad = false;
8412678Sktlim@umich.edu        state->idx = storeWBIdx;
8422678Sktlim@umich.edu        state->inst = inst;
8436974Stjones1@inf.ed.ac.uk
8446974Stjones1@inf.ed.ac.uk        if (!TheISA::HasUnalignedMemAcc || !storeQueue[storeWBIdx].isSplit) {
8456974Stjones1@inf.ed.ac.uk
8466974Stjones1@inf.ed.ac.uk            // Build a single data packet if the store isn't split.
84710342SCurtis.Dunham@arm.com            data_pkt = Packet::createWrite(req);
8486974Stjones1@inf.ed.ac.uk            data_pkt->dataStatic(inst->memData);
8496974Stjones1@inf.ed.ac.uk            data_pkt->senderState = state;
8506974Stjones1@inf.ed.ac.uk        } else {
8516974Stjones1@inf.ed.ac.uk            // Create two packets if the store is split in two.
85210342SCurtis.Dunham@arm.com            data_pkt = Packet::createWrite(sreqLow);
85310342SCurtis.Dunham@arm.com            snd_data_pkt = Packet::createWrite(sreqHigh);
8546974Stjones1@inf.ed.ac.uk
8556974Stjones1@inf.ed.ac.uk            data_pkt->dataStatic(inst->memData);
8566974Stjones1@inf.ed.ac.uk            snd_data_pkt->dataStatic(inst->memData + sreqLow->getSize());
8576974Stjones1@inf.ed.ac.uk
8586974Stjones1@inf.ed.ac.uk            data_pkt->senderState = state;
8596974Stjones1@inf.ed.ac.uk            snd_data_pkt->senderState = state;
8606974Stjones1@inf.ed.ac.uk
8616974Stjones1@inf.ed.ac.uk            state->isSplit = true;
8626974Stjones1@inf.ed.ac.uk            state->outstanding = 2;
8636974Stjones1@inf.ed.ac.uk
8646974Stjones1@inf.ed.ac.uk            // Can delete the main request now.
8656974Stjones1@inf.ed.ac.uk            req = sreqLow;
8666974Stjones1@inf.ed.ac.uk        }
8672678Sktlim@umich.edu
8687720Sgblack@eecs.umich.edu        DPRINTF(LSQUnit, "D-Cache: Writing back store idx:%i PC:%s "
8692292SN/A                "to Addr:%#x, data:%#x [sn:%lli]\n",
8707720Sgblack@eecs.umich.edu                storeWBIdx, inst->pcState(),
8713797Sgblack@eecs.umich.edu                req->getPaddr(), (int)*(inst->memData),
8723221Sktlim@umich.edu                inst->seqNum);
8732292SN/A
8742693Sktlim@umich.edu        // @todo: Remove this SC hack once the memory system handles it.
8754350Sgblack@eecs.umich.edu        if (inst->isStoreConditional()) {
8766974Stjones1@inf.ed.ac.uk            assert(!storeQueue[storeWBIdx].isSplit);
8773326Sktlim@umich.edu            // Disable recording the result temporarily.  Writing to
8783326Sktlim@umich.edu            // misc regs normally updates the result, but this is not
8793326Sktlim@umich.edu            // the desired behavior when handling store conditionals.
8809046SAli.Saidi@ARM.com            inst->recordResult(false);
88110030SAli.Saidi@ARM.com            bool success = TheISA::handleLockedWrite(inst.get(), req, cacheBlockMask);
8829046SAli.Saidi@ARM.com            inst->recordResult(true);
8833326Sktlim@umich.edu
8843326Sktlim@umich.edu            if (!success) {
8853326Sktlim@umich.edu                // Instantly complete this store.
8863326Sktlim@umich.edu                DPRINTF(LSQUnit, "Store conditional [sn:%lli] failed.  "
8873326Sktlim@umich.edu                        "Instantly completing it.\n",
8883326Sktlim@umich.edu                        inst->seqNum);
8893326Sktlim@umich.edu                WritebackEvent *wb = new WritebackEvent(inst, data_pkt, this);
8907823Ssteve.reinhardt@amd.com                cpu->schedule(wb, curTick() + 1);
8913326Sktlim@umich.edu                completeStore(storeWBIdx);
8923326Sktlim@umich.edu                incrStIdx(storeWBIdx);
8933326Sktlim@umich.edu                continue;
8942693Sktlim@umich.edu            }
8952693Sktlim@umich.edu        } else {
8962693Sktlim@umich.edu            // Non-store conditionals do not need a writeback.
8972693Sktlim@umich.edu            state->noWB = true;
8982693Sktlim@umich.edu        }
8992693Sktlim@umich.edu
9008481Sgblack@eecs.umich.edu        bool split =
9018481Sgblack@eecs.umich.edu            TheISA::HasUnalignedMemAcc && storeQueue[storeWBIdx].isSplit;
9028481Sgblack@eecs.umich.edu
9038481Sgblack@eecs.umich.edu        ThreadContext *thread = cpu->tcBase(lsqID);
9048481Sgblack@eecs.umich.edu
9058481Sgblack@eecs.umich.edu        if (req->isMmappedIpr()) {
9068481Sgblack@eecs.umich.edu            assert(!inst->isStoreConditional());
9078481Sgblack@eecs.umich.edu            TheISA::handleIprWrite(thread, data_pkt);
9088481Sgblack@eecs.umich.edu            delete data_pkt;
9098481Sgblack@eecs.umich.edu            if (split) {
9108481Sgblack@eecs.umich.edu                assert(snd_data_pkt->req->isMmappedIpr());
9118481Sgblack@eecs.umich.edu                TheISA::handleIprWrite(thread, snd_data_pkt);
9128481Sgblack@eecs.umich.edu                delete snd_data_pkt;
9138481Sgblack@eecs.umich.edu            }
9148481Sgblack@eecs.umich.edu            delete state;
9158481Sgblack@eecs.umich.edu            completeStore(storeWBIdx);
9168481Sgblack@eecs.umich.edu            incrStIdx(storeWBIdx);
9178481Sgblack@eecs.umich.edu        } else if (!sendStore(data_pkt)) {
9184032Sktlim@umich.edu            DPRINTF(IEW, "D-Cache became blocked when writing [sn:%lli], will"
9193221Sktlim@umich.edu                    "retry later\n",
9203221Sktlim@umich.edu                    inst->seqNum);
9216974Stjones1@inf.ed.ac.uk
9226974Stjones1@inf.ed.ac.uk            // Need to store the second packet, if split.
9238481Sgblack@eecs.umich.edu            if (split) {
9246974Stjones1@inf.ed.ac.uk                state->pktToSend = true;
9256974Stjones1@inf.ed.ac.uk                state->pendingPacket = snd_data_pkt;
9266974Stjones1@inf.ed.ac.uk            }
9272669Sktlim@umich.edu        } else {
9286974Stjones1@inf.ed.ac.uk
9296974Stjones1@inf.ed.ac.uk            // If split, try to send the second packet too
9308481Sgblack@eecs.umich.edu            if (split) {
9316974Stjones1@inf.ed.ac.uk                assert(snd_data_pkt);
9326974Stjones1@inf.ed.ac.uk
9336974Stjones1@inf.ed.ac.uk                // Ensure there are enough ports to use.
93411780Sarthur.perais@inria.fr                if (usedStorePorts < cacheStorePorts) {
93511780Sarthur.perais@inria.fr                    ++usedStorePorts;
9366974Stjones1@inf.ed.ac.uk                    if (sendStore(snd_data_pkt)) {
9376974Stjones1@inf.ed.ac.uk                        storePostSend(snd_data_pkt);
9386974Stjones1@inf.ed.ac.uk                    } else {
9396974Stjones1@inf.ed.ac.uk                        DPRINTF(IEW, "D-Cache became blocked when writing"
9406974Stjones1@inf.ed.ac.uk                                " [sn:%lli] second packet, will retry later\n",
9416974Stjones1@inf.ed.ac.uk                                inst->seqNum);
9426974Stjones1@inf.ed.ac.uk                    }
9436974Stjones1@inf.ed.ac.uk                } else {
9446974Stjones1@inf.ed.ac.uk
9456974Stjones1@inf.ed.ac.uk                    // Store the packet for when there's free ports.
9466974Stjones1@inf.ed.ac.uk                    assert(pendingPkt == NULL);
9476974Stjones1@inf.ed.ac.uk                    pendingPkt = snd_data_pkt;
9486974Stjones1@inf.ed.ac.uk                    hasPendingPkt = true;
9496974Stjones1@inf.ed.ac.uk                }
9506974Stjones1@inf.ed.ac.uk            } else {
9516974Stjones1@inf.ed.ac.uk
9526974Stjones1@inf.ed.ac.uk                // Not a split store.
9536974Stjones1@inf.ed.ac.uk                storePostSend(data_pkt);
9546974Stjones1@inf.ed.ac.uk            }
9552292SN/A        }
9562292SN/A    }
9572292SN/A
9582292SN/A    // Not sure this should set it to 0.
95911780Sarthur.perais@inria.fr    usedStorePorts = 0;
9602292SN/A
9612292SN/A    assert(stores >= 0 && storesToWB >= 0);
9622292SN/A}
9632292SN/A
9642292SN/A/*template <class Impl>
9652292SN/Avoid
9662292SN/ALSQUnit<Impl>::removeMSHR(InstSeqNum seqNum)
9672292SN/A{
9682292SN/A    list<InstSeqNum>::iterator mshr_it = find(mshrSeqNums.begin(),
9692292SN/A                                              mshrSeqNums.end(),
9702292SN/A                                              seqNum);
9712292SN/A
9722292SN/A    if (mshr_it != mshrSeqNums.end()) {
9732292SN/A        mshrSeqNums.erase(mshr_it);
9742292SN/A        DPRINTF(LSQUnit, "Removing MSHR. count = %i\n",mshrSeqNums.size());
9752292SN/A    }
9762292SN/A}*/
9772292SN/A
9782292SN/Atemplate <class Impl>
9792292SN/Avoid
9802292SN/ALSQUnit<Impl>::squash(const InstSeqNum &squashed_num)
9812292SN/A{
9822292SN/A    DPRINTF(LSQUnit, "Squashing until [sn:%lli]!"
9832329SN/A            "(Loads:%i Stores:%i)\n", squashed_num, loads, stores);
9842292SN/A
9852292SN/A    int load_idx = loadTail;
9862292SN/A    decrLdIdx(load_idx);
9872292SN/A
9882292SN/A    while (loads != 0 && loadQueue[load_idx]->seqNum > squashed_num) {
9897720Sgblack@eecs.umich.edu        DPRINTF(LSQUnit,"Load Instruction PC %s squashed, "
9902292SN/A                "[sn:%lli]\n",
9917720Sgblack@eecs.umich.edu                loadQueue[load_idx]->pcState(),
9922292SN/A                loadQueue[load_idx]->seqNum);
9932292SN/A
9942292SN/A        if (isStalled() && load_idx == stallingLoadIdx) {
9952292SN/A            stalled = false;
9962292SN/A            stallingStoreIsn = 0;
9972292SN/A            stallingLoadIdx = 0;
9982292SN/A        }
9992292SN/A
10002329SN/A        // Clear the smart pointer to make sure it is decremented.
10012731Sktlim@umich.edu        loadQueue[load_idx]->setSquashed();
10022292SN/A        loadQueue[load_idx] = NULL;
10032292SN/A        --loads;
10042292SN/A
10052292SN/A        // Inefficient!
10062292SN/A        loadTail = load_idx;
10072292SN/A
10082292SN/A        decrLdIdx(load_idx);
10092727Sktlim@umich.edu        ++lsqSquashedLoads;
10102292SN/A    }
10112292SN/A
10124032Sktlim@umich.edu    if (memDepViolator && squashed_num < memDepViolator->seqNum) {
10134032Sktlim@umich.edu        memDepViolator = NULL;
10144032Sktlim@umich.edu    }
10154032Sktlim@umich.edu
10162292SN/A    int store_idx = storeTail;
10172292SN/A    decrStIdx(store_idx);
10182292SN/A
10192292SN/A    while (stores != 0 &&
10202292SN/A           storeQueue[store_idx].inst->seqNum > squashed_num) {
10212329SN/A        // Instructions marked as can WB are already committed.
10222292SN/A        if (storeQueue[store_idx].canWB) {
10232292SN/A            break;
10242292SN/A        }
10252292SN/A
10267720Sgblack@eecs.umich.edu        DPRINTF(LSQUnit,"Store Instruction PC %s squashed, "
10272292SN/A                "idx:%i [sn:%lli]\n",
10287720Sgblack@eecs.umich.edu                storeQueue[store_idx].inst->pcState(),
10292292SN/A                store_idx, storeQueue[store_idx].inst->seqNum);
10302292SN/A
10312329SN/A        // I don't think this can happen.  It should have been cleared
10322329SN/A        // by the stalling load.
10332292SN/A        if (isStalled() &&
10342292SN/A            storeQueue[store_idx].inst->seqNum == stallingStoreIsn) {
10352292SN/A            panic("Is stalled should have been cleared by stalling load!\n");
10362292SN/A            stalled = false;
10372292SN/A            stallingStoreIsn = 0;
10382292SN/A        }
10392292SN/A
10402329SN/A        // Clear the smart pointer to make sure it is decremented.
10412731Sktlim@umich.edu        storeQueue[store_idx].inst->setSquashed();
10422292SN/A        storeQueue[store_idx].inst = NULL;
10432292SN/A        storeQueue[store_idx].canWB = 0;
10442292SN/A
10454032Sktlim@umich.edu        // Must delete request now that it wasn't handed off to
10464032Sktlim@umich.edu        // memory.  This is quite ugly.  @todo: Figure out the proper
10474032Sktlim@umich.edu        // place to really handle request deletes.
104812749Sgiacomo.travaglini@arm.com        storeQueue[store_idx].req.reset();
10496974Stjones1@inf.ed.ac.uk        if (TheISA::HasUnalignedMemAcc && storeQueue[store_idx].isSplit) {
105012749Sgiacomo.travaglini@arm.com            storeQueue[store_idx].sreqLow.reset();
105112749Sgiacomo.travaglini@arm.com            storeQueue[store_idx].sreqHigh.reset();
10526974Stjones1@inf.ed.ac.uk        }
10534032Sktlim@umich.edu
10542292SN/A        --stores;
10552292SN/A
10562292SN/A        // Inefficient!
10572292SN/A        storeTail = store_idx;
10582292SN/A
10592292SN/A        decrStIdx(store_idx);
10602727Sktlim@umich.edu        ++lsqSquashedStores;
10612292SN/A    }
10622292SN/A}
10632292SN/A
10642292SN/Atemplate <class Impl>
10652292SN/Avoid
10663349Sbinkertn@umich.eduLSQUnit<Impl>::storePostSend(PacketPtr pkt)
10672693Sktlim@umich.edu{
10682693Sktlim@umich.edu    if (isStalled() &&
10692693Sktlim@umich.edu        storeQueue[storeWBIdx].inst->seqNum == stallingStoreIsn) {
10702693Sktlim@umich.edu        DPRINTF(LSQUnit, "Unstalling, stalling store [sn:%lli] "
10712693Sktlim@umich.edu                "load idx:%i\n",
10722693Sktlim@umich.edu                stallingStoreIsn, stallingLoadIdx);
10732693Sktlim@umich.edu        stalled = false;
10742693Sktlim@umich.edu        stallingStoreIsn = 0;
10752693Sktlim@umich.edu        iewStage->replayMemInst(loadQueue[stallingLoadIdx]);
10762693Sktlim@umich.edu    }
10772693Sktlim@umich.edu
10782693Sktlim@umich.edu    if (!storeQueue[storeWBIdx].inst->isStoreConditional()) {
10792693Sktlim@umich.edu        // The store is basically completed at this time. This
10802693Sktlim@umich.edu        // only works so long as the checker doesn't try to
10812693Sktlim@umich.edu        // verify the value in memory for stores.
10822693Sktlim@umich.edu        storeQueue[storeWBIdx].inst->setCompleted();
10838887Sgeoffrey.blake@arm.com
10842693Sktlim@umich.edu        if (cpu->checker) {
10852732Sktlim@umich.edu            cpu->checker->verify(storeQueue[storeWBIdx].inst);
10862693Sktlim@umich.edu        }
10872693Sktlim@umich.edu    }
10882693Sktlim@umich.edu
10898727Snilay@cs.wisc.edu    if (needsTSO) {
10908727Snilay@cs.wisc.edu        storeInFlight = true;
10918727Snilay@cs.wisc.edu    }
10928727Snilay@cs.wisc.edu
10932693Sktlim@umich.edu    incrStIdx(storeWBIdx);
10942693Sktlim@umich.edu}
10952693Sktlim@umich.edu
10962693Sktlim@umich.edutemplate <class Impl>
10972693Sktlim@umich.eduvoid
109813429Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::writeback(const DynInstPtr &inst, PacketPtr pkt)
10992678Sktlim@umich.edu{
11002678Sktlim@umich.edu    iewStage->wakeCPU();
11012678Sktlim@umich.edu
11022678Sktlim@umich.edu    // Squashed instructions do not need to complete their access.
11032678Sktlim@umich.edu    if (inst->isSquashed()) {
11042678Sktlim@umich.edu        assert(!inst->isStore());
11052727Sktlim@umich.edu        ++lsqIgnoredResponses;
11062678Sktlim@umich.edu        return;
11072678Sktlim@umich.edu    }
11082678Sktlim@umich.edu
11092678Sktlim@umich.edu    if (!inst->isExecuted()) {
11102678Sktlim@umich.edu        inst->setExecuted();
11112678Sktlim@umich.edu
111210575SMarco.Elver@ARM.com        if (inst->fault == NoFault) {
111310575SMarco.Elver@ARM.com            // Complete access to copy data to proper place.
111410575SMarco.Elver@ARM.com            inst->completeAcc(pkt);
111510575SMarco.Elver@ARM.com        } else {
111610575SMarco.Elver@ARM.com            // If the instruction has an outstanding fault, we cannot complete
111710575SMarco.Elver@ARM.com            // the access as this discards the current fault.
111810575SMarco.Elver@ARM.com
111910575SMarco.Elver@ARM.com            // If we have an outstanding fault, the fault should only be of
112010575SMarco.Elver@ARM.com            // type ReExec.
112110575SMarco.Elver@ARM.com            assert(dynamic_cast<ReExec*>(inst->fault.get()) != nullptr);
112210575SMarco.Elver@ARM.com
112310575SMarco.Elver@ARM.com            DPRINTF(LSQUnit, "Not completing instruction [sn:%lli] access "
112410575SMarco.Elver@ARM.com                    "due to pending fault.\n", inst->seqNum);
112510575SMarco.Elver@ARM.com        }
11262678Sktlim@umich.edu    }
11272678Sktlim@umich.edu
11282678Sktlim@umich.edu    // Need to insert instruction into queue to commit
11292678Sktlim@umich.edu    iewStage->instToCommit(inst);
11302678Sktlim@umich.edu
11312678Sktlim@umich.edu    iewStage->activityThisCycle();
11327598Sminkyu.jeong@arm.com
11337598Sminkyu.jeong@arm.com    // see if this load changed the PC
11347598Sminkyu.jeong@arm.com    iewStage->checkMisprediction(inst);
11352678Sktlim@umich.edu}
11362678Sktlim@umich.edu
11372678Sktlim@umich.edutemplate <class Impl>
11382678Sktlim@umich.eduvoid
11392292SN/ALSQUnit<Impl>::completeStore(int store_idx)
11402292SN/A{
11412292SN/A    assert(storeQueue[store_idx].inst);
11422292SN/A    storeQueue[store_idx].completed = true;
11432292SN/A    --storesToWB;
11442292SN/A    // A bit conservative because a store completion may not free up entries,
11452292SN/A    // but hopefully avoids two store completions in one cycle from making
11462292SN/A    // the CPU tick twice.
11473126Sktlim@umich.edu    cpu->wakeCPU();
11482292SN/A    cpu->activityThisCycle();
11492292SN/A
11502292SN/A    if (store_idx == storeHead) {
11512292SN/A        do {
11522292SN/A            incrStIdx(storeHead);
11532292SN/A
11542292SN/A            --stores;
11552292SN/A        } while (storeQueue[storeHead].completed &&
11562292SN/A                 storeHead != storeTail);
11572292SN/A
11582292SN/A        iewStage->updateLSQNextCycle = true;
11592292SN/A    }
11602292SN/A
11612329SN/A    DPRINTF(LSQUnit, "Completing store [sn:%lli], idx:%i, store head "
11622329SN/A            "idx:%i\n",
11632329SN/A            storeQueue[store_idx].inst->seqNum, store_idx, storeHead);
11642292SN/A
11659527SMatt.Horsnell@arm.com#if TRACING_ON
11669527SMatt.Horsnell@arm.com    if (DTRACE(O3PipeView)) {
11679527SMatt.Horsnell@arm.com        storeQueue[store_idx].inst->storeTick =
11689527SMatt.Horsnell@arm.com            curTick() - storeQueue[store_idx].inst->fetchTick;
11699527SMatt.Horsnell@arm.com    }
11709527SMatt.Horsnell@arm.com#endif
11719527SMatt.Horsnell@arm.com
11722292SN/A    if (isStalled() &&
11732292SN/A        storeQueue[store_idx].inst->seqNum == stallingStoreIsn) {
11742292SN/A        DPRINTF(LSQUnit, "Unstalling, stalling store [sn:%lli] "
11752292SN/A                "load idx:%i\n",
11762292SN/A                stallingStoreIsn, stallingLoadIdx);
11772292SN/A        stalled = false;
11782292SN/A        stallingStoreIsn = 0;
11792292SN/A        iewStage->replayMemInst(loadQueue[stallingLoadIdx]);
11802292SN/A    }
11812316SN/A
11822316SN/A    storeQueue[store_idx].inst->setCompleted();
11832329SN/A
11848727Snilay@cs.wisc.edu    if (needsTSO) {
11858727Snilay@cs.wisc.edu        storeInFlight = false;
11868727Snilay@cs.wisc.edu    }
11878727Snilay@cs.wisc.edu
11882329SN/A    // Tell the checker we've completed this instruction.  Some stores
11892329SN/A    // may get reported twice to the checker, but the checker can
11902329SN/A    // handle that case.
119112216Snikos.nikoleris@arm.com
119212216Snikos.nikoleris@arm.com    // Store conditionals cannot be sent to the checker yet, they have
119312216Snikos.nikoleris@arm.com    // to update the misc registers first which should take place
119412216Snikos.nikoleris@arm.com    // when they commit
119512216Snikos.nikoleris@arm.com    if (cpu->checker && !storeQueue[store_idx].inst->isStoreConditional()) {
11962732Sktlim@umich.edu        cpu->checker->verify(storeQueue[store_idx].inst);
11972316SN/A    }
11982292SN/A}
11992292SN/A
12002292SN/Atemplate <class Impl>
12016974Stjones1@inf.ed.ac.ukbool
12026974Stjones1@inf.ed.ac.ukLSQUnit<Impl>::sendStore(PacketPtr data_pkt)
12036974Stjones1@inf.ed.ac.uk{
12048975Sandreas.hansson@arm.com    if (!dcachePort->sendTimingReq(data_pkt)) {
12056974Stjones1@inf.ed.ac.uk        // Need to handle becoming blocked on a store.
12066974Stjones1@inf.ed.ac.uk        isStoreBlocked = true;
12076974Stjones1@inf.ed.ac.uk        ++lsqCacheBlocked;
12086974Stjones1@inf.ed.ac.uk        assert(retryPkt == NULL);
12096974Stjones1@inf.ed.ac.uk        retryPkt = data_pkt;
12106974Stjones1@inf.ed.ac.uk        return false;
12116974Stjones1@inf.ed.ac.uk    }
12126974Stjones1@inf.ed.ac.uk    return true;
12136974Stjones1@inf.ed.ac.uk}
12146974Stjones1@inf.ed.ac.uk
12156974Stjones1@inf.ed.ac.uktemplate <class Impl>
12162693Sktlim@umich.eduvoid
12172693Sktlim@umich.eduLSQUnit<Impl>::recvRetry()
12182693Sktlim@umich.edu{
12192698Sktlim@umich.edu    if (isStoreBlocked) {
12204985Sktlim@umich.edu        DPRINTF(LSQUnit, "Receiving retry: store blocked\n");
12212698Sktlim@umich.edu        assert(retryPkt != NULL);
12222693Sktlim@umich.edu
12238587Snilay@cs.wisc.edu        LSQSenderState *state =
12248587Snilay@cs.wisc.edu            dynamic_cast<LSQSenderState *>(retryPkt->senderState);
12258587Snilay@cs.wisc.edu
12268975Sandreas.hansson@arm.com        if (dcachePort->sendTimingReq(retryPkt)) {
12276974Stjones1@inf.ed.ac.uk            // Don't finish the store unless this is the last packet.
12288133SAli.Saidi@ARM.com            if (!TheISA::HasUnalignedMemAcc || !state->pktToSend ||
12298133SAli.Saidi@ARM.com                    state->pendingPacket == retryPkt) {
12308133SAli.Saidi@ARM.com                state->pktToSend = false;
12316974Stjones1@inf.ed.ac.uk                storePostSend(retryPkt);
12326974Stjones1@inf.ed.ac.uk            }
12332699Sktlim@umich.edu            retryPkt = NULL;
12342693Sktlim@umich.edu            isStoreBlocked = false;
12356974Stjones1@inf.ed.ac.uk
12366974Stjones1@inf.ed.ac.uk            // Send any outstanding packet.
12376974Stjones1@inf.ed.ac.uk            if (TheISA::HasUnalignedMemAcc && state->pktToSend) {
12386974Stjones1@inf.ed.ac.uk                assert(state->pendingPacket);
12396974Stjones1@inf.ed.ac.uk                if (sendStore(state->pendingPacket)) {
12406974Stjones1@inf.ed.ac.uk                    storePostSend(state->pendingPacket);
12416974Stjones1@inf.ed.ac.uk                }
12426974Stjones1@inf.ed.ac.uk            }
12432693Sktlim@umich.edu        } else {
12442693Sktlim@umich.edu            // Still blocked!
12452727Sktlim@umich.edu            ++lsqCacheBlocked;
12462693Sktlim@umich.edu        }
12472693Sktlim@umich.edu    }
12482693Sktlim@umich.edu}
12492693Sktlim@umich.edu
12502693Sktlim@umich.edutemplate <class Impl>
12512292SN/Ainline void
12529440SAndreas.Sandberg@ARM.comLSQUnit<Impl>::incrStIdx(int &store_idx) const
12532292SN/A{
12542292SN/A    if (++store_idx >= SQEntries)
12552292SN/A        store_idx = 0;
12562292SN/A}
12572292SN/A
12582292SN/Atemplate <class Impl>
12592292SN/Ainline void
12609440SAndreas.Sandberg@ARM.comLSQUnit<Impl>::decrStIdx(int &store_idx) const
12612292SN/A{
12622292SN/A    if (--store_idx < 0)
12632292SN/A        store_idx += SQEntries;
12642292SN/A}
12652292SN/A
12662292SN/Atemplate <class Impl>
12672292SN/Ainline void
12689440SAndreas.Sandberg@ARM.comLSQUnit<Impl>::incrLdIdx(int &load_idx) const
12692292SN/A{
12702292SN/A    if (++load_idx >= LQEntries)
12712292SN/A        load_idx = 0;
12722292SN/A}
12732292SN/A
12742292SN/Atemplate <class Impl>
12752292SN/Ainline void
12769440SAndreas.Sandberg@ARM.comLSQUnit<Impl>::decrLdIdx(int &load_idx) const
12772292SN/A{
12782292SN/A    if (--load_idx < 0)
12792292SN/A        load_idx += LQEntries;
12802292SN/A}
12812329SN/A
12822329SN/Atemplate <class Impl>
12832329SN/Avoid
12849440SAndreas.Sandberg@ARM.comLSQUnit<Impl>::dumpInsts() const
12852329SN/A{
12862329SN/A    cprintf("Load store queue: Dumping instructions.\n");
12872329SN/A    cprintf("Load queue size: %i\n", loads);
12882329SN/A    cprintf("Load queue: ");
12892329SN/A
12902329SN/A    int load_idx = loadHead;
12912329SN/A
12922329SN/A    while (load_idx != loadTail && loadQueue[load_idx]) {
12939440SAndreas.Sandberg@ARM.com        const DynInstPtr &inst(loadQueue[load_idx]);
12949440SAndreas.Sandberg@ARM.com        cprintf("%s.[sn:%i] ", inst->pcState(), inst->seqNum);
12952329SN/A
12962329SN/A        incrLdIdx(load_idx);
12972329SN/A    }
12989440SAndreas.Sandberg@ARM.com    cprintf("\n");
12992329SN/A
13002329SN/A    cprintf("Store queue size: %i\n", stores);
13012329SN/A    cprintf("Store queue: ");
13022329SN/A
13032329SN/A    int store_idx = storeHead;
13042329SN/A
13052329SN/A    while (store_idx != storeTail && storeQueue[store_idx].inst) {
13069440SAndreas.Sandberg@ARM.com        const DynInstPtr &inst(storeQueue[store_idx].inst);
13079440SAndreas.Sandberg@ARM.com        cprintf("%s.[sn:%i] ", inst->pcState(), inst->seqNum);
13082329SN/A
13092329SN/A        incrStIdx(store_idx);
13102329SN/A    }
13112329SN/A
13122329SN/A    cprintf("\n");
13132329SN/A}
13149944Smatt.horsnell@ARM.com
13159944Smatt.horsnell@ARM.com#endif//__CPU_O3_LSQ_UNIT_IMPL_HH__
1316