lsq_unit.hh revision 14083
12292SN/A/*
213590Srekai.gonzalezalberquilla@arm.com * Copyright (c) 2012-2014,2017-2018 ARM Limited
39444SAndreas.Sandberg@ARM.com * All rights reserved
49444SAndreas.Sandberg@ARM.com *
59444SAndreas.Sandberg@ARM.com * The license below extends only to copyright in the software and shall
69444SAndreas.Sandberg@ARM.com * not be construed as granting a license to any other intellectual
79444SAndreas.Sandberg@ARM.com * property including but not limited to intellectual property relating
89444SAndreas.Sandberg@ARM.com * to a hardware implementation of the functionality of the software
99444SAndreas.Sandberg@ARM.com * licensed hereunder.  You may use the software subject to the license
109444SAndreas.Sandberg@ARM.com * terms below provided that you ensure that this notice is replicated
119444SAndreas.Sandberg@ARM.com * unmodified and in its entirety in all distributions of the software,
129444SAndreas.Sandberg@ARM.com * modified or unmodified, in source code or in binary form.
139444SAndreas.Sandberg@ARM.com *
142329SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
1510239Sbinhpham@cs.rutgers.edu * Copyright (c) 2013 Advanced Micro Devices, Inc.
162292SN/A * All rights reserved.
172292SN/A *
182292SN/A * Redistribution and use in source and binary forms, with or without
192292SN/A * modification, are permitted provided that the following conditions are
202292SN/A * met: redistributions of source code must retain the above copyright
212292SN/A * notice, this list of conditions and the following disclaimer;
222292SN/A * redistributions in binary form must reproduce the above copyright
232292SN/A * notice, this list of conditions and the following disclaimer in the
242292SN/A * documentation and/or other materials provided with the distribution;
252292SN/A * neither the name of the copyright holders nor the names of its
262292SN/A * contributors may be used to endorse or promote products derived from
272292SN/A * this software without specific prior written permission.
282292SN/A *
292292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312292SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322292SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
332292SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342292SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352292SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362292SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372292SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382292SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392292SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402689Sktlim@umich.edu *
412689Sktlim@umich.edu * Authors: Kevin Lim
422689Sktlim@umich.edu *          Korey Sewell
432292SN/A */
442292SN/A
452292SN/A#ifndef __CPU_O3_LSQ_UNIT_HH__
462292SN/A#define __CPU_O3_LSQ_UNIT_HH__
472292SN/A
482329SN/A#include <algorithm>
494395Ssaidi@eecs.umich.edu#include <cstring>
502292SN/A#include <map>
512292SN/A#include <queue>
522292SN/A
538591Sgblack@eecs.umich.edu#include "arch/generic/debugfaults.hh"
5414030Sgabor.dozsa@arm.com#include "arch/generic/vec_reg.hh"
558506Sgblack@eecs.umich.edu#include "arch/isa_traits.hh"
563326Sktlim@umich.edu#include "arch/locked_mem.hh"
578481Sgblack@eecs.umich.edu#include "arch/mmapped_ipr.hh"
586658Snate@binkert.org#include "config/the_isa.hh"
592292SN/A#include "cpu/inst_seq.hh"
608230Snate@binkert.org#include "cpu/timebuf.hh"
618232Snate@binkert.org#include "debug/LSQUnit.hh"
623348Sbinkertn@umich.edu#include "mem/packet.hh"
632669Sktlim@umich.edu#include "mem/port.hh"
642292SN/A
658737Skoansin.tan@gmail.comstruct DerivO3CPUParams;
6613590Srekai.gonzalezalberquilla@arm.com#include "base/circular_queue.hh"
675529Snate@binkert.org
682292SN/A/**
692329SN/A * Class that implements the actual LQ and SQ for each specific
702329SN/A * thread.  Both are circular queues; load entries are freed upon
712329SN/A * committing, while store entries are freed once they writeback. The
722329SN/A * LSQUnit tracks if there are memory ordering violations, and also
732329SN/A * detects partial load to store forwarding cases (a store only has
742329SN/A * part of a load's data) that requires the load to wait until the
752329SN/A * store writes back. In the former case it holds onto the instruction
762329SN/A * until the dependence unit looks at it, and in the latter it stalls
772329SN/A * the LSQ until the store writes back. At that point the load is
782329SN/A * replayed.
792292SN/A */
802292SN/Atemplate <class Impl>
8113590Srekai.gonzalezalberquilla@arm.comclass LSQUnit
8213590Srekai.gonzalezalberquilla@arm.com{
832292SN/A  public:
8414030Sgabor.dozsa@arm.com    static constexpr auto MaxDataBytes = MaxVecRegLenInBytes;
8514030Sgabor.dozsa@arm.com
862733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
872292SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
882292SN/A    typedef typename Impl::CPUPol::IEW IEW;
892907Sktlim@umich.edu    typedef typename Impl::CPUPol::LSQ LSQ;
902292SN/A    typedef typename Impl::CPUPol::IssueStruct IssueStruct;
912292SN/A
9213590Srekai.gonzalezalberquilla@arm.com    using LSQSenderState = typename LSQ::LSQSenderState;
9313590Srekai.gonzalezalberquilla@arm.com    using LSQRequest = typename Impl::CPUPol::LSQ::LSQRequest;
9413590Srekai.gonzalezalberquilla@arm.com  private:
9513590Srekai.gonzalezalberquilla@arm.com    class LSQEntry
9613590Srekai.gonzalezalberquilla@arm.com    {
9713590Srekai.gonzalezalberquilla@arm.com      private:
9813590Srekai.gonzalezalberquilla@arm.com        /** The instruction. */
9913590Srekai.gonzalezalberquilla@arm.com        DynInstPtr inst;
10013590Srekai.gonzalezalberquilla@arm.com        /** The request. */
10113590Srekai.gonzalezalberquilla@arm.com        LSQRequest* req;
10213590Srekai.gonzalezalberquilla@arm.com        /** The size of the operation. */
10314030Sgabor.dozsa@arm.com        uint32_t _size;
10413590Srekai.gonzalezalberquilla@arm.com        /** Valid entry. */
10513590Srekai.gonzalezalberquilla@arm.com        bool _valid;
10613590Srekai.gonzalezalberquilla@arm.com      public:
10713590Srekai.gonzalezalberquilla@arm.com        /** Constructs an empty store queue entry. */
10813590Srekai.gonzalezalberquilla@arm.com        LSQEntry()
10913590Srekai.gonzalezalberquilla@arm.com            : inst(nullptr), req(nullptr), _size(0), _valid(false)
11013590Srekai.gonzalezalberquilla@arm.com        {
11113590Srekai.gonzalezalberquilla@arm.com        }
11213590Srekai.gonzalezalberquilla@arm.com
11313590Srekai.gonzalezalberquilla@arm.com        ~LSQEntry()
11413590Srekai.gonzalezalberquilla@arm.com        {
11513590Srekai.gonzalezalberquilla@arm.com            inst = nullptr;
11613590Srekai.gonzalezalberquilla@arm.com            if (req != nullptr) {
11713590Srekai.gonzalezalberquilla@arm.com                req->freeLSQEntry();
11813590Srekai.gonzalezalberquilla@arm.com                req = nullptr;
11913590Srekai.gonzalezalberquilla@arm.com            }
12013590Srekai.gonzalezalberquilla@arm.com        }
12113590Srekai.gonzalezalberquilla@arm.com
12213590Srekai.gonzalezalberquilla@arm.com        void
12313590Srekai.gonzalezalberquilla@arm.com        clear()
12413590Srekai.gonzalezalberquilla@arm.com        {
12513590Srekai.gonzalezalberquilla@arm.com            inst = nullptr;
12613590Srekai.gonzalezalberquilla@arm.com            if (req != nullptr) {
12713590Srekai.gonzalezalberquilla@arm.com                req->freeLSQEntry();
12813590Srekai.gonzalezalberquilla@arm.com            }
12913590Srekai.gonzalezalberquilla@arm.com            req = nullptr;
13013590Srekai.gonzalezalberquilla@arm.com            _valid = false;
13113590Srekai.gonzalezalberquilla@arm.com            _size = 0;
13213590Srekai.gonzalezalberquilla@arm.com        }
13313590Srekai.gonzalezalberquilla@arm.com
13413590Srekai.gonzalezalberquilla@arm.com        void
13513590Srekai.gonzalezalberquilla@arm.com        set(const DynInstPtr& inst)
13613590Srekai.gonzalezalberquilla@arm.com        {
13713590Srekai.gonzalezalberquilla@arm.com            assert(!_valid);
13813590Srekai.gonzalezalberquilla@arm.com            this->inst = inst;
13913590Srekai.gonzalezalberquilla@arm.com            _valid = true;
14013590Srekai.gonzalezalberquilla@arm.com            _size = 0;
14113590Srekai.gonzalezalberquilla@arm.com        }
14213590Srekai.gonzalezalberquilla@arm.com        LSQRequest* request() { return req; }
14313590Srekai.gonzalezalberquilla@arm.com        void setRequest(LSQRequest* r) { req = r; }
14413590Srekai.gonzalezalberquilla@arm.com        bool hasRequest() { return req != nullptr; }
14513590Srekai.gonzalezalberquilla@arm.com        /** Member accessors. */
14613590Srekai.gonzalezalberquilla@arm.com        /** @{ */
14713590Srekai.gonzalezalberquilla@arm.com        bool valid() const { return _valid; }
14814030Sgabor.dozsa@arm.com        uint32_t& size() { return _size; }
14914030Sgabor.dozsa@arm.com        const uint32_t& size() const { return _size; }
15013590Srekai.gonzalezalberquilla@arm.com        const DynInstPtr& instruction() const { return inst; }
15113590Srekai.gonzalezalberquilla@arm.com        /** @} */
15213590Srekai.gonzalezalberquilla@arm.com    };
15313590Srekai.gonzalezalberquilla@arm.com
15413590Srekai.gonzalezalberquilla@arm.com    class SQEntry : public LSQEntry
15513590Srekai.gonzalezalberquilla@arm.com    {
15613590Srekai.gonzalezalberquilla@arm.com      private:
15713590Srekai.gonzalezalberquilla@arm.com        /** The store data. */
15814030Sgabor.dozsa@arm.com        char _data[MaxDataBytes];
15913590Srekai.gonzalezalberquilla@arm.com        /** Whether or not the store can writeback. */
16013590Srekai.gonzalezalberquilla@arm.com        bool _canWB;
16113590Srekai.gonzalezalberquilla@arm.com        /** Whether or not the store is committed. */
16213590Srekai.gonzalezalberquilla@arm.com        bool _committed;
16313590Srekai.gonzalezalberquilla@arm.com        /** Whether or not the store is completed. */
16413590Srekai.gonzalezalberquilla@arm.com        bool _completed;
16513590Srekai.gonzalezalberquilla@arm.com        /** Does this request write all zeros and thus doesn't
16613590Srekai.gonzalezalberquilla@arm.com         * have any data attached to it. Used for cache block zero
16713590Srekai.gonzalezalberquilla@arm.com         * style instructs (ARM DC ZVA; ALPHA WH64)
16813590Srekai.gonzalezalberquilla@arm.com         */
16913590Srekai.gonzalezalberquilla@arm.com        bool _isAllZeros;
17013590Srekai.gonzalezalberquilla@arm.com      public:
17113590Srekai.gonzalezalberquilla@arm.com        static constexpr size_t DataSize = sizeof(_data);
17213590Srekai.gonzalezalberquilla@arm.com        /** Constructs an empty store queue entry. */
17313590Srekai.gonzalezalberquilla@arm.com        SQEntry()
17413590Srekai.gonzalezalberquilla@arm.com            : _canWB(false), _committed(false), _completed(false),
17513590Srekai.gonzalezalberquilla@arm.com              _isAllZeros(false)
17613590Srekai.gonzalezalberquilla@arm.com        {
17713590Srekai.gonzalezalberquilla@arm.com            std::memset(_data, 0, DataSize);
17813590Srekai.gonzalezalberquilla@arm.com        }
17913590Srekai.gonzalezalberquilla@arm.com
18013590Srekai.gonzalezalberquilla@arm.com        ~SQEntry()
18113590Srekai.gonzalezalberquilla@arm.com        {
18213590Srekai.gonzalezalberquilla@arm.com        }
18313590Srekai.gonzalezalberquilla@arm.com
18413590Srekai.gonzalezalberquilla@arm.com        void
18513590Srekai.gonzalezalberquilla@arm.com        set(const DynInstPtr& inst)
18613590Srekai.gonzalezalberquilla@arm.com        {
18713590Srekai.gonzalezalberquilla@arm.com            LSQEntry::set(inst);
18813590Srekai.gonzalezalberquilla@arm.com        }
18913590Srekai.gonzalezalberquilla@arm.com
19013590Srekai.gonzalezalberquilla@arm.com        void
19113590Srekai.gonzalezalberquilla@arm.com        clear()
19213590Srekai.gonzalezalberquilla@arm.com        {
19313590Srekai.gonzalezalberquilla@arm.com            LSQEntry::clear();
19413590Srekai.gonzalezalberquilla@arm.com            _canWB = _completed = _committed = _isAllZeros = false;
19513590Srekai.gonzalezalberquilla@arm.com        }
19613590Srekai.gonzalezalberquilla@arm.com        /** Member accessors. */
19713590Srekai.gonzalezalberquilla@arm.com        /** @{ */
19813590Srekai.gonzalezalberquilla@arm.com        bool& canWB() { return _canWB; }
19913590Srekai.gonzalezalberquilla@arm.com        const bool& canWB() const { return _canWB; }
20013590Srekai.gonzalezalberquilla@arm.com        bool& completed() { return _completed; }
20113590Srekai.gonzalezalberquilla@arm.com        const bool& completed() const { return _completed; }
20213590Srekai.gonzalezalberquilla@arm.com        bool& committed() { return _committed; }
20313590Srekai.gonzalezalberquilla@arm.com        const bool& committed() const { return _committed; }
20413590Srekai.gonzalezalberquilla@arm.com        bool& isAllZeros() { return _isAllZeros; }
20513590Srekai.gonzalezalberquilla@arm.com        const bool& isAllZeros() const { return _isAllZeros; }
20613590Srekai.gonzalezalberquilla@arm.com        char* data() { return _data; }
20713590Srekai.gonzalezalberquilla@arm.com        const char* data() const { return _data; }
20813590Srekai.gonzalezalberquilla@arm.com        /** @} */
20913590Srekai.gonzalezalberquilla@arm.com    };
21013590Srekai.gonzalezalberquilla@arm.com    using LQEntry = LSQEntry;
21113590Srekai.gonzalezalberquilla@arm.com
21213590Srekai.gonzalezalberquilla@arm.com  public:
21313590Srekai.gonzalezalberquilla@arm.com    using LoadQueue = CircularQueue<LQEntry>;
21413590Srekai.gonzalezalberquilla@arm.com    using StoreQueue = CircularQueue<SQEntry>;
21513590Srekai.gonzalezalberquilla@arm.com
2162292SN/A  public:
2172292SN/A    /** Constructs an LSQ unit. init() must be called prior to use. */
21813472Srekai.gonzalezalberquilla@arm.com    LSQUnit(uint32_t lqEntries, uint32_t sqEntries);
21913472Srekai.gonzalezalberquilla@arm.com
22013472Srekai.gonzalezalberquilla@arm.com    /** We cannot copy LSQUnit because it has stats for which copy
22113472Srekai.gonzalezalberquilla@arm.com     * contructor is deleted explicitly. However, STL vector requires
22213472Srekai.gonzalezalberquilla@arm.com     * a valid copy constructor for the base type at compile time.
22313472Srekai.gonzalezalberquilla@arm.com     */
22413472Srekai.gonzalezalberquilla@arm.com    LSQUnit(const LSQUnit &l) { panic("LSQUnit is not copy-able"); }
2252292SN/A
2262292SN/A    /** Initializes the LSQ unit with the specified number of entries. */
2275529Snate@binkert.org    void init(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params,
22813472Srekai.gonzalezalberquilla@arm.com            LSQ *lsq_ptr, unsigned id);
2292292SN/A
2302292SN/A    /** Returns the name of the LSQ unit. */
2312292SN/A    std::string name() const;
2322292SN/A
2332727Sktlim@umich.edu    /** Registers statistics. */
2342727Sktlim@umich.edu    void regStats();
2352727Sktlim@umich.edu
2362907Sktlim@umich.edu    /** Sets the pointer to the dcache port. */
2378922Swilliam.wang@arm.com    void setDcachePort(MasterPort *dcache_port);
2382907Sktlim@umich.edu
2399444SAndreas.Sandberg@ARM.com    /** Perform sanity checks after a drain. */
2409444SAndreas.Sandberg@ARM.com    void drainSanityCheck() const;
2412307SN/A
2422348SN/A    /** Takes over from another CPU's thread. */
2432307SN/A    void takeOverFrom();
2442307SN/A
2452292SN/A    /** Inserts an instruction. */
24613429Srekai.gonzalezalberquilla@arm.com    void insert(const DynInstPtr &inst);
2472292SN/A    /** Inserts a load instruction. */
24813429Srekai.gonzalezalberquilla@arm.com    void insertLoad(const DynInstPtr &load_inst);
2492292SN/A    /** Inserts a store instruction. */
25013429Srekai.gonzalezalberquilla@arm.com    void insertStore(const DynInstPtr &store_inst);
2512292SN/A
2528545Ssaidi@eecs.umich.edu    /** Check for ordering violations in the LSQ. For a store squash if we
2538545Ssaidi@eecs.umich.edu     * ever find a conflicting load. For a load, only squash if we
2548545Ssaidi@eecs.umich.edu     * an external snoop invalidate has been seen for that load address
2558199SAli.Saidi@ARM.com     * @param load_idx index to start checking at
2568199SAli.Saidi@ARM.com     * @param inst the instruction to check
2578199SAli.Saidi@ARM.com     */
25813590Srekai.gonzalezalberquilla@arm.com    Fault checkViolations(typename LoadQueue::iterator& loadIt,
25913590Srekai.gonzalezalberquilla@arm.com            const DynInstPtr& inst);
2608199SAli.Saidi@ARM.com
2618545Ssaidi@eecs.umich.edu    /** Check if an incoming invalidate hits in the lsq on a load
2628545Ssaidi@eecs.umich.edu     * that might have issued out of order wrt another load beacuse
2638545Ssaidi@eecs.umich.edu     * of the intermediate invalidate.
2648545Ssaidi@eecs.umich.edu     */
2658545Ssaidi@eecs.umich.edu    void checkSnoop(PacketPtr pkt);
2668545Ssaidi@eecs.umich.edu
2672292SN/A    /** Executes a load instruction. */
26813429Srekai.gonzalezalberquilla@arm.com    Fault executeLoad(const DynInstPtr &inst);
2692292SN/A
2702329SN/A    Fault executeLoad(int lq_idx) { panic("Not implemented"); return NoFault; }
2712292SN/A    /** Executes a store instruction. */
27213429Srekai.gonzalezalberquilla@arm.com    Fault executeStore(const DynInstPtr &inst);
2732292SN/A
2742292SN/A    /** Commits the head load. */
2752292SN/A    void commitLoad();
2762292SN/A    /** Commits loads older than a specific sequence number. */
2772292SN/A    void commitLoads(InstSeqNum &youngest_inst);
2782292SN/A
2792292SN/A    /** Commits stores older than a specific sequence number. */
2802292SN/A    void commitStores(InstSeqNum &youngest_inst);
2812292SN/A
2822292SN/A    /** Writes back stores. */
2832292SN/A    void writebackStores();
2842292SN/A
2852790Sktlim@umich.edu    /** Completes the data access that has been returned from the
2862790Sktlim@umich.edu     * memory system. */
2872669Sktlim@umich.edu    void completeDataAccess(PacketPtr pkt);
2882669Sktlim@umich.edu
2892292SN/A    /** Squashes all instructions younger than a specific sequence number. */
2902292SN/A    void squash(const InstSeqNum &squashed_num);
2912292SN/A
2922292SN/A    /** Returns if there is a memory ordering violation. Value is reset upon
2932292SN/A     * call to getMemDepViolator().
2942292SN/A     */
2952292SN/A    bool violation() { return memDepViolator; }
2962292SN/A
2972292SN/A    /** Returns the memory ordering violator. */
2982292SN/A    DynInstPtr getMemDepViolator();
2992292SN/A
30010239Sbinhpham@cs.rutgers.edu    /** Returns the number of free LQ entries. */
30110239Sbinhpham@cs.rutgers.edu    unsigned numFreeLoadEntries();
30210239Sbinhpham@cs.rutgers.edu
30310239Sbinhpham@cs.rutgers.edu    /** Returns the number of free SQ entries. */
30410239Sbinhpham@cs.rutgers.edu    unsigned numFreeStoreEntries();
3052292SN/A
3062292SN/A    /** Returns the number of loads in the LQ. */
3072292SN/A    int numLoads() { return loads; }
3082292SN/A
3092292SN/A    /** Returns the number of stores in the SQ. */
3102292SN/A    int numStores() { return stores; }
3112292SN/A
3122292SN/A    /** Returns if either the LQ or SQ is full. */
3132292SN/A    bool isFull() { return lqFull() || sqFull(); }
3142292SN/A
3159444SAndreas.Sandberg@ARM.com    /** Returns if both the LQ and SQ are empty. */
3169444SAndreas.Sandberg@ARM.com    bool isEmpty() const { return lqEmpty() && sqEmpty(); }
3179444SAndreas.Sandberg@ARM.com
3182292SN/A    /** Returns if the LQ is full. */
31913590Srekai.gonzalezalberquilla@arm.com    bool lqFull() { return loadQueue.full(); }
3202292SN/A
3212292SN/A    /** Returns if the SQ is full. */
32213590Srekai.gonzalezalberquilla@arm.com    bool sqFull() { return storeQueue.full(); }
3232292SN/A
3249444SAndreas.Sandberg@ARM.com    /** Returns if the LQ is empty. */
3259444SAndreas.Sandberg@ARM.com    bool lqEmpty() const { return loads == 0; }
3269444SAndreas.Sandberg@ARM.com
3279444SAndreas.Sandberg@ARM.com    /** Returns if the SQ is empty. */
3289444SAndreas.Sandberg@ARM.com    bool sqEmpty() const { return stores == 0; }
3299444SAndreas.Sandberg@ARM.com
3302292SN/A    /** Returns the number of instructions in the LSQ. */
3312292SN/A    unsigned getCount() { return loads + stores; }
3322292SN/A
3332292SN/A    /** Returns if there are any stores to writeback. */
3342292SN/A    bool hasStoresToWB() { return storesToWB; }
3352292SN/A
3362292SN/A    /** Returns the number of stores to writeback. */
3372292SN/A    int numStoresToWB() { return storesToWB; }
3382292SN/A
3392292SN/A    /** Returns if the LSQ unit will writeback on this cycle. */
34013590Srekai.gonzalezalberquilla@arm.com    bool
34113590Srekai.gonzalezalberquilla@arm.com    willWB()
34213590Srekai.gonzalezalberquilla@arm.com    {
34313590Srekai.gonzalezalberquilla@arm.com        return storeWBIt.dereferenceable() &&
34413590Srekai.gonzalezalberquilla@arm.com                        storeWBIt->valid() &&
34513590Srekai.gonzalezalberquilla@arm.com                        storeWBIt->canWB() &&
34613590Srekai.gonzalezalberquilla@arm.com                        !storeWBIt->completed() &&
34713590Srekai.gonzalezalberquilla@arm.com                        !isStoreBlocked;
34813590Srekai.gonzalezalberquilla@arm.com    }
3492292SN/A
3502907Sktlim@umich.edu    /** Handles doing the retry. */
3512907Sktlim@umich.edu    void recvRetry();
3522907Sktlim@umich.edu
35313590Srekai.gonzalezalberquilla@arm.com    unsigned int cacheLineSize();
3542292SN/A  private:
3559444SAndreas.Sandberg@ARM.com    /** Reset the LSQ state */
3569444SAndreas.Sandberg@ARM.com    void resetState();
3579444SAndreas.Sandberg@ARM.com
3582698Sktlim@umich.edu    /** Writes back the instruction, sending it to IEW. */
35913429Srekai.gonzalezalberquilla@arm.com    void writeback(const DynInstPtr &inst, PacketPtr pkt);
3602678Sktlim@umich.edu
36113590Srekai.gonzalezalberquilla@arm.com    /** Try to finish a previously blocked write back attempt */
36213590Srekai.gonzalezalberquilla@arm.com    void writebackBlockedStore();
36313590Srekai.gonzalezalberquilla@arm.com
36413590Srekai.gonzalezalberquilla@arm.com    /** Completes the store at the specified index. */
36513590Srekai.gonzalezalberquilla@arm.com    void completeStore(typename StoreQueue::iterator store_idx);
3666974Stjones1@inf.ed.ac.uk
3672698Sktlim@umich.edu    /** Handles completing the send of a store to memory. */
36813590Srekai.gonzalezalberquilla@arm.com    void storePostSend();
3692292SN/A
3702329SN/A  public:
37113590Srekai.gonzalezalberquilla@arm.com    /** Attempts to send a packet to the cache.
37213590Srekai.gonzalezalberquilla@arm.com     * Check if there are ports available. Return true if
37313590Srekai.gonzalezalberquilla@arm.com     * there are, false if there are not.
37413590Srekai.gonzalezalberquilla@arm.com     */
37513590Srekai.gonzalezalberquilla@arm.com    bool trySendPacket(bool isLoad, PacketPtr data_pkt);
37613590Srekai.gonzalezalberquilla@arm.com
37713590Srekai.gonzalezalberquilla@arm.com
3782329SN/A    /** Debugging function to dump instructions in the LSQ. */
3799440SAndreas.Sandberg@ARM.com    void dumpInsts() const;
3802329SN/A
38113590Srekai.gonzalezalberquilla@arm.com    /** Schedule event for the cpu. */
38213590Srekai.gonzalezalberquilla@arm.com    void schedule(Event& ev, Tick when) { cpu->schedule(ev, when); }
38313590Srekai.gonzalezalberquilla@arm.com
38413590Srekai.gonzalezalberquilla@arm.com    BaseTLB* dTLB() { return cpu->dtb; }
38513590Srekai.gonzalezalberquilla@arm.com
3862292SN/A  private:
3872292SN/A    /** Pointer to the CPU. */
3882733Sktlim@umich.edu    O3CPU *cpu;
3892292SN/A
3902292SN/A    /** Pointer to the IEW stage. */
3912292SN/A    IEW *iewStage;
3922292SN/A
3932907Sktlim@umich.edu    /** Pointer to the LSQ. */
3942907Sktlim@umich.edu    LSQ *lsq;
3952669Sktlim@umich.edu
3962907Sktlim@umich.edu    /** Pointer to the dcache port.  Used only for sending. */
3978922Swilliam.wang@arm.com    MasterPort *dcachePort;
3982292SN/A
39913590Srekai.gonzalezalberquilla@arm.com    /** Particularisation of the LSQSenderState to the LQ. */
40013590Srekai.gonzalezalberquilla@arm.com    class LQSenderState : public LSQSenderState
4012678Sktlim@umich.edu    {
40213590Srekai.gonzalezalberquilla@arm.com        using LSQSenderState::alive;
4032678Sktlim@umich.edu      public:
40413590Srekai.gonzalezalberquilla@arm.com        LQSenderState(typename LoadQueue::iterator idx_)
40513590Srekai.gonzalezalberquilla@arm.com            : LSQSenderState(idx_->request(), true), idx(idx_) { }
4062678Sktlim@umich.edu
40713590Srekai.gonzalezalberquilla@arm.com        /** The LQ index of the instruction. */
40813590Srekai.gonzalezalberquilla@arm.com        typename LoadQueue::iterator idx;
40913590Srekai.gonzalezalberquilla@arm.com        //virtual LSQRequest* request() { return idx->request(); }
41013590Srekai.gonzalezalberquilla@arm.com        virtual void
41113590Srekai.gonzalezalberquilla@arm.com        complete()
41213590Srekai.gonzalezalberquilla@arm.com        {
41313590Srekai.gonzalezalberquilla@arm.com            //if (alive())
41413590Srekai.gonzalezalberquilla@arm.com            //  idx->request()->senderState(nullptr);
41513590Srekai.gonzalezalberquilla@arm.com        }
41613590Srekai.gonzalezalberquilla@arm.com    };
4176974Stjones1@inf.ed.ac.uk
41813590Srekai.gonzalezalberquilla@arm.com    /** Particularisation of the LSQSenderState to the SQ. */
41913590Srekai.gonzalezalberquilla@arm.com    class SQSenderState : public LSQSenderState
42013590Srekai.gonzalezalberquilla@arm.com    {
42113590Srekai.gonzalezalberquilla@arm.com        using LSQSenderState::alive;
42213590Srekai.gonzalezalberquilla@arm.com      public:
42313590Srekai.gonzalezalberquilla@arm.com        SQSenderState(typename StoreQueue::iterator idx_)
42413590Srekai.gonzalezalberquilla@arm.com            : LSQSenderState(idx_->request(), false), idx(idx_) { }
42513590Srekai.gonzalezalberquilla@arm.com        /** The SQ index of the instruction. */
42613590Srekai.gonzalezalberquilla@arm.com        typename StoreQueue::iterator idx;
42713590Srekai.gonzalezalberquilla@arm.com        //virtual LSQRequest* request() { return idx->request(); }
42813590Srekai.gonzalezalberquilla@arm.com        virtual void
42913590Srekai.gonzalezalberquilla@arm.com        complete()
43013590Srekai.gonzalezalberquilla@arm.com        {
43113590Srekai.gonzalezalberquilla@arm.com            //if (alive())
43213590Srekai.gonzalezalberquilla@arm.com            //   idx->request()->senderState(nullptr);
43313590Srekai.gonzalezalberquilla@arm.com        }
4342678Sktlim@umich.edu    };
4352678Sktlim@umich.edu
4362698Sktlim@umich.edu    /** Writeback event, specifically for when stores forward data to loads. */
43713590Srekai.gonzalezalberquilla@arm.com    class WritebackEvent : public Event
43813590Srekai.gonzalezalberquilla@arm.com    {
4392678Sktlim@umich.edu      public:
4402678Sktlim@umich.edu        /** Constructs a writeback event. */
44113429Srekai.gonzalezalberquilla@arm.com        WritebackEvent(const DynInstPtr &_inst, PacketPtr pkt,
44213429Srekai.gonzalezalberquilla@arm.com                LSQUnit *lsq_ptr);
4432678Sktlim@umich.edu
4442678Sktlim@umich.edu        /** Processes the writeback event. */
4452678Sktlim@umich.edu        void process();
4462678Sktlim@umich.edu
4472678Sktlim@umich.edu        /** Returns the description of this event. */
4485336Shines@cs.fsu.edu        const char *description() const;
4492678Sktlim@umich.edu
4502678Sktlim@umich.edu      private:
4512698Sktlim@umich.edu        /** Instruction whose results are being written back. */
4522678Sktlim@umich.edu        DynInstPtr inst;
4532678Sktlim@umich.edu
4542698Sktlim@umich.edu        /** The packet that would have been sent to memory. */
4552678Sktlim@umich.edu        PacketPtr pkt;
4562678Sktlim@umich.edu
4572678Sktlim@umich.edu        /** The pointer to the LSQ unit that issued the store. */
4582678Sktlim@umich.edu        LSQUnit<Impl> *lsqPtr;
4592678Sktlim@umich.edu    };
4602678Sktlim@umich.edu
4612292SN/A  public:
46213590Srekai.gonzalezalberquilla@arm.com    /**
46313590Srekai.gonzalezalberquilla@arm.com     * Handles writing back and completing the load or store that has
46413590Srekai.gonzalezalberquilla@arm.com     * returned from memory.
46513590Srekai.gonzalezalberquilla@arm.com     *
46613590Srekai.gonzalezalberquilla@arm.com     * @param pkt Response packet from the memory sub-system
46713590Srekai.gonzalezalberquilla@arm.com     */
46813590Srekai.gonzalezalberquilla@arm.com    bool recvTimingResp(PacketPtr pkt);
4692329SN/A
4702292SN/A  private:
4712292SN/A    /** The LSQUnit thread id. */
4726221Snate@binkert.org    ThreadID lsqID;
47313590Srekai.gonzalezalberquilla@arm.com  public:
4742292SN/A    /** The store queue. */
47513590Srekai.gonzalezalberquilla@arm.com    CircularQueue<SQEntry> storeQueue;
4762292SN/A
4772292SN/A    /** The load queue. */
47813590Srekai.gonzalezalberquilla@arm.com    LoadQueue loadQueue;
4792292SN/A
48013590Srekai.gonzalezalberquilla@arm.com  private:
4818199SAli.Saidi@ARM.com    /** The number of places to shift addresses in the LSQ before checking
4828199SAli.Saidi@ARM.com     * for dependency violations
4838199SAli.Saidi@ARM.com     */
4848199SAli.Saidi@ARM.com    unsigned depCheckShift;
4858199SAli.Saidi@ARM.com
4868199SAli.Saidi@ARM.com    /** Should loads be checked for dependency issues */
4878199SAli.Saidi@ARM.com    bool checkLoads;
4888199SAli.Saidi@ARM.com
4892292SN/A    /** The number of load instructions in the LQ. */
4902292SN/A    int loads;
4912329SN/A    /** The number of store instructions in the SQ. */
4922292SN/A    int stores;
4932292SN/A    /** The number of store instructions in the SQ waiting to writeback. */
4942292SN/A    int storesToWB;
4952292SN/A
4962329SN/A    /** The index of the first instruction that may be ready to be
4972329SN/A     * written back, and has not yet been written back.
4982292SN/A     */
49913590Srekai.gonzalezalberquilla@arm.com    typename StoreQueue::iterator storeWBIt;
5002292SN/A
5018545Ssaidi@eecs.umich.edu    /** Address Mask for a cache block (e.g. ~(cache_block_size-1)) */
5028545Ssaidi@eecs.umich.edu    Addr cacheBlockMask;
5038545Ssaidi@eecs.umich.edu
5042292SN/A    /** Wire to read information from the issue stage time queue. */
5052292SN/A    typename TimeBuffer<IssueStruct>::wire fromIssue;
5062292SN/A
5072292SN/A    /** Whether or not the LSQ is stalled. */
5082292SN/A    bool stalled;
5092292SN/A    /** The store that causes the stall due to partial store to load
5102292SN/A     * forwarding.
5112292SN/A     */
5122292SN/A    InstSeqNum stallingStoreIsn;
5132292SN/A    /** The index of the above store. */
5142292SN/A    int stallingLoadIdx;
5152292SN/A
5162698Sktlim@umich.edu    /** The packet that needs to be retried. */
5172698Sktlim@umich.edu    PacketPtr retryPkt;
5182693Sktlim@umich.edu
5192698Sktlim@umich.edu    /** Whehter or not a store is blocked due to the memory system. */
5202678Sktlim@umich.edu    bool isStoreBlocked;
5212678Sktlim@umich.edu
5228727Snilay@cs.wisc.edu    /** Whether or not a store is in flight. */
5238727Snilay@cs.wisc.edu    bool storeInFlight;
5248727Snilay@cs.wisc.edu
5252292SN/A    /** The oldest load that caused a memory ordering violation. */
5262292SN/A    DynInstPtr memDepViolator;
5272292SN/A
5286974Stjones1@inf.ed.ac.uk    /** Whether or not there is a packet that couldn't be sent because of
5296974Stjones1@inf.ed.ac.uk     * a lack of cache ports. */
53013590Srekai.gonzalezalberquilla@arm.com    bool hasPendingRequest;
5316974Stjones1@inf.ed.ac.uk
5326974Stjones1@inf.ed.ac.uk    /** The packet that is pending free cache ports. */
53313590Srekai.gonzalezalberquilla@arm.com    LSQRequest* pendingRequest;
5346974Stjones1@inf.ed.ac.uk
5358727Snilay@cs.wisc.edu    /** Flag for memory model. */
5368727Snilay@cs.wisc.edu    bool needsTSO;
5378727Snilay@cs.wisc.edu
5382292SN/A    // Will also need how many read/write ports the Dcache has.  Or keep track
5392292SN/A    // of that in stage that is one level up, and only call executeLoad/Store
5402292SN/A    // the appropriate number of times.
5412727Sktlim@umich.edu    /** Total number of loads forwaded from LSQ stores. */
5425999Snate@binkert.org    Stats::Scalar lsqForwLoads;
5432307SN/A
5443126Sktlim@umich.edu    /** Total number of loads ignored due to invalid addresses. */
5455999Snate@binkert.org    Stats::Scalar invAddrLoads;
5463126Sktlim@umich.edu
5473126Sktlim@umich.edu    /** Total number of squashed loads. */
5485999Snate@binkert.org    Stats::Scalar lsqSquashedLoads;
5493126Sktlim@umich.edu
5503126Sktlim@umich.edu    /** Total number of responses from the memory system that are
5513126Sktlim@umich.edu     * ignored due to the instruction already being squashed. */
5525999Snate@binkert.org    Stats::Scalar lsqIgnoredResponses;
5533126Sktlim@umich.edu
5543126Sktlim@umich.edu    /** Tota number of memory ordering violations. */
5555999Snate@binkert.org    Stats::Scalar lsqMemOrderViolation;
5563126Sktlim@umich.edu
5572727Sktlim@umich.edu    /** Total number of squashed stores. */
5585999Snate@binkert.org    Stats::Scalar lsqSquashedStores;
5592727Sktlim@umich.edu
5602727Sktlim@umich.edu    /** Total number of software prefetches ignored due to invalid addresses. */
5615999Snate@binkert.org    Stats::Scalar invAddrSwpfs;
5622727Sktlim@umich.edu
5632727Sktlim@umich.edu    /** Ready loads blocked due to partial store-forwarding. */
5645999Snate@binkert.org    Stats::Scalar lsqBlockedLoads;
5652727Sktlim@umich.edu
5662727Sktlim@umich.edu    /** Number of loads that were rescheduled. */
5675999Snate@binkert.org    Stats::Scalar lsqRescheduledLoads;
5682727Sktlim@umich.edu
5692727Sktlim@umich.edu    /** Number of times the LSQ is blocked due to the cache. */
5705999Snate@binkert.org    Stats::Scalar lsqCacheBlocked;
5712727Sktlim@umich.edu
5722292SN/A  public:
5732292SN/A    /** Executes the load at the given index. */
57413590Srekai.gonzalezalberquilla@arm.com    Fault read(LSQRequest *req, int load_idx);
5752292SN/A
5762292SN/A    /** Executes the store at the given index. */
57713590Srekai.gonzalezalberquilla@arm.com    Fault write(LSQRequest *req, uint8_t *data, int store_idx);
5782292SN/A
5792292SN/A    /** Returns the index of the head load instruction. */
58013590Srekai.gonzalezalberquilla@arm.com    int getLoadHead() { return loadQueue.head(); }
58113590Srekai.gonzalezalberquilla@arm.com
5822292SN/A    /** Returns the sequence number of the head load instruction. */
58313590Srekai.gonzalezalberquilla@arm.com    InstSeqNum
58413590Srekai.gonzalezalberquilla@arm.com    getLoadHeadSeqNum()
5852292SN/A    {
58613590Srekai.gonzalezalberquilla@arm.com        return loadQueue.front().valid()
58713590Srekai.gonzalezalberquilla@arm.com            ? loadQueue.front().instruction()->seqNum
58813590Srekai.gonzalezalberquilla@arm.com            : 0;
5892292SN/A    }
5902292SN/A
5912292SN/A    /** Returns the index of the head store instruction. */
59213590Srekai.gonzalezalberquilla@arm.com    int getStoreHead() { return storeQueue.head(); }
5932292SN/A    /** Returns the sequence number of the head store instruction. */
59413590Srekai.gonzalezalberquilla@arm.com    InstSeqNum
59513590Srekai.gonzalezalberquilla@arm.com    getStoreHeadSeqNum()
5962292SN/A    {
59713590Srekai.gonzalezalberquilla@arm.com        return storeQueue.front().valid()
59813590Srekai.gonzalezalberquilla@arm.com            ? storeQueue.front().instruction()->seqNum
59913590Srekai.gonzalezalberquilla@arm.com            : 0;
6002292SN/A    }
6012292SN/A
6022292SN/A    /** Returns whether or not the LSQ unit is stalled. */
6032292SN/A    bool isStalled()  { return stalled; }
60413590Srekai.gonzalezalberquilla@arm.com  public:
60513590Srekai.gonzalezalberquilla@arm.com    typedef typename CircularQueue<LQEntry>::iterator LQIterator;
60613590Srekai.gonzalezalberquilla@arm.com    typedef typename CircularQueue<SQEntry>::iterator SQIterator;
60713590Srekai.gonzalezalberquilla@arm.com    typedef CircularQueue<LQEntry> LQueue;
60813590Srekai.gonzalezalberquilla@arm.com    typedef CircularQueue<SQEntry> SQueue;
6092292SN/A};
6102292SN/A
6112292SN/Atemplate <class Impl>
6122292SN/AFault
61313590Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::read(LSQRequest *req, int load_idx)
6142292SN/A{
61513590Srekai.gonzalezalberquilla@arm.com    LQEntry& load_req = loadQueue[load_idx];
61613590Srekai.gonzalezalberquilla@arm.com    const DynInstPtr& load_inst = load_req.instruction();
6172292SN/A
61813590Srekai.gonzalezalberquilla@arm.com    load_req.setRequest(req);
6192669Sktlim@umich.edu    assert(load_inst);
6202669Sktlim@umich.edu
6212669Sktlim@umich.edu    assert(!load_inst->isExecuted());
6222292SN/A
62310824SAndreas.Sandberg@ARM.com    // Make sure this isn't a strictly ordered load
62410824SAndreas.Sandberg@ARM.com    // A bit of a hackish way to get strictly ordered accesses to work
62510824SAndreas.Sandberg@ARM.com    // only if they're at the head of the LSQ and are ready to commit
62610824SAndreas.Sandberg@ARM.com    // (at the head of the ROB too).
62713590Srekai.gonzalezalberquilla@arm.com
62813590Srekai.gonzalezalberquilla@arm.com    if (req->mainRequest()->isStrictlyOrdered() &&
62913590Srekai.gonzalezalberquilla@arm.com        (load_idx != loadQueue.head() || !load_inst->isAtCommit())) {
63013590Srekai.gonzalezalberquilla@arm.com        // Tell IQ/mem dep unit that this instruction will need to be
63113590Srekai.gonzalezalberquilla@arm.com        // rescheduled eventually
6322669Sktlim@umich.edu        iewStage->rescheduleMemInst(load_inst);
63313590Srekai.gonzalezalberquilla@arm.com        load_inst->clearIssued();
63413590Srekai.gonzalezalberquilla@arm.com        load_inst->effAddrValid(false);
6352727Sktlim@umich.edu        ++lsqRescheduledLoads;
63610824SAndreas.Sandberg@ARM.com        DPRINTF(LSQUnit, "Strictly ordered load [sn:%lli] PC %s\n",
6377720Sgblack@eecs.umich.edu                load_inst->seqNum, load_inst->pcState());
6384032Sktlim@umich.edu
63913590Srekai.gonzalezalberquilla@arm.com        // Must delete request now that it wasn't handed off to
64013590Srekai.gonzalezalberquilla@arm.com        // memory.  This is quite ugly.  @todo: Figure out the proper
64113590Srekai.gonzalezalberquilla@arm.com        // place to really handle request deletes.
64213590Srekai.gonzalezalberquilla@arm.com        load_req.setRequest(nullptr);
64313590Srekai.gonzalezalberquilla@arm.com        req->discard();
64410474Sandreas.hansson@arm.com        return std::make_shared<GenericISA::M5PanicFault>(
64510824SAndreas.Sandberg@ARM.com            "Strictly ordered load [sn:%llx] PC %s\n",
64610474Sandreas.hansson@arm.com            load_inst->seqNum, load_inst->pcState());
6472292SN/A    }
6482292SN/A
6492292SN/A    DPRINTF(LSQUnit, "Read called, load idx: %i, store idx: %i, "
6506974Stjones1@inf.ed.ac.uk            "storeHead: %i addr: %#x%s\n",
65113590Srekai.gonzalezalberquilla@arm.com            load_idx - 1, load_inst->sqIt._idx, storeQueue.head() - 1,
65213590Srekai.gonzalezalberquilla@arm.com            req->mainRequest()->getPaddr(), req->isSplit() ? " split" : "");
6532292SN/A
65413590Srekai.gonzalezalberquilla@arm.com    if (req->mainRequest()->isLLSC()) {
6553326Sktlim@umich.edu        // Disable recording the result temporarily.  Writing to misc
6563326Sktlim@umich.edu        // regs normally updates the result, but this is not the
6573326Sktlim@umich.edu        // desired behavior when handling store conditionals.
6589046SAli.Saidi@ARM.com        load_inst->recordResult(false);
65913590Srekai.gonzalezalberquilla@arm.com        TheISA::handleLockedRead(load_inst.get(), req->mainRequest());
6609046SAli.Saidi@ARM.com        load_inst->recordResult(true);
6612292SN/A    }
6622292SN/A
66313590Srekai.gonzalezalberquilla@arm.com    if (req->mainRequest()->isMmappedIpr()) {
6648481Sgblack@eecs.umich.edu        assert(!load_inst->memData);
66514030Sgabor.dozsa@arm.com        load_inst->memData = new uint8_t[MaxDataBytes];
6668481Sgblack@eecs.umich.edu
6678481Sgblack@eecs.umich.edu        ThreadContext *thread = cpu->tcBase(lsqID);
66813590Srekai.gonzalezalberquilla@arm.com        PacketPtr main_pkt = new Packet(req->mainRequest(), MemCmd::ReadReq);
6698481Sgblack@eecs.umich.edu
67014083SPouya.Fotouhi@amd.com        main_pkt->dataStatic(load_inst->memData);
67114083SPouya.Fotouhi@amd.com
67213590Srekai.gonzalezalberquilla@arm.com        Cycles delay = req->handleIprRead(thread, main_pkt);
6738481Sgblack@eecs.umich.edu
67413590Srekai.gonzalezalberquilla@arm.com        WritebackEvent *wb = new WritebackEvent(load_inst, main_pkt, this);
6759179Sandreas.hansson@arm.com        cpu->schedule(wb, cpu->clockEdge(delay));
6768481Sgblack@eecs.umich.edu        return NoFault;
6778481Sgblack@eecs.umich.edu    }
6788481Sgblack@eecs.umich.edu
67913590Srekai.gonzalezalberquilla@arm.com    // Check the SQ for any previous stores that might lead to forwarding
68013590Srekai.gonzalezalberquilla@arm.com    auto store_it = load_inst->sqIt;
68113590Srekai.gonzalezalberquilla@arm.com    assert (store_it >= storeWBIt);
68213590Srekai.gonzalezalberquilla@arm.com    // End once we've reached the top of the LSQ
68313590Srekai.gonzalezalberquilla@arm.com    while (store_it != storeWBIt) {
68413590Srekai.gonzalezalberquilla@arm.com        // Move the index to one younger
68513590Srekai.gonzalezalberquilla@arm.com        store_it--;
68613590Srekai.gonzalezalberquilla@arm.com        assert(store_it->valid());
68713590Srekai.gonzalezalberquilla@arm.com        assert(store_it->instruction()->seqNum < load_inst->seqNum);
68813590Srekai.gonzalezalberquilla@arm.com        int store_size = store_it->size();
6892292SN/A
69013590Srekai.gonzalezalberquilla@arm.com        // Cache maintenance instructions go down via the store
69113590Srekai.gonzalezalberquilla@arm.com        // path but they carry no data and they shouldn't be
69213590Srekai.gonzalezalberquilla@arm.com        // considered for forwarding
69313590Srekai.gonzalezalberquilla@arm.com        if (store_size != 0 && !store_it->instruction()->strictlyOrdered() &&
69413590Srekai.gonzalezalberquilla@arm.com            !(store_it->request()->mainRequest() &&
69513590Srekai.gonzalezalberquilla@arm.com              store_it->request()->mainRequest()->isCacheMaintenance())) {
69613590Srekai.gonzalezalberquilla@arm.com            assert(store_it->instruction()->effAddrValid());
6972292SN/A
69813590Srekai.gonzalezalberquilla@arm.com            // Check if the store data is within the lower and upper bounds of
69913590Srekai.gonzalezalberquilla@arm.com            // addresses that the request needs.
70013590Srekai.gonzalezalberquilla@arm.com            auto req_s = req->mainRequest()->getVaddr();
70113590Srekai.gonzalezalberquilla@arm.com            auto req_e = req_s + req->mainRequest()->getSize();
70213590Srekai.gonzalezalberquilla@arm.com            auto st_s = store_it->instruction()->effAddr;
70313590Srekai.gonzalezalberquilla@arm.com            auto st_e = st_s + store_size;
7042292SN/A
70513590Srekai.gonzalezalberquilla@arm.com            bool store_has_lower_limit = req_s >= st_s;
70613590Srekai.gonzalezalberquilla@arm.com            bool store_has_upper_limit = req_e <= st_e;
70713590Srekai.gonzalezalberquilla@arm.com            bool lower_load_has_store_part = req_s < st_e;
70813590Srekai.gonzalezalberquilla@arm.com            bool upper_load_has_store_part = req_e > st_s;
7092292SN/A
71013652Sqtt2@cornell.edu            // If the store entry is not atomic (atomic does not have valid
71113652Sqtt2@cornell.edu            // data), the store has all of the data needed, and
71213652Sqtt2@cornell.edu            // the load is not LLSC, then
71313652Sqtt2@cornell.edu            // we can forward data from the store to the load
71413652Sqtt2@cornell.edu            if (!store_it->instruction()->isAtomic() &&
71513652Sqtt2@cornell.edu                store_has_lower_limit && store_has_upper_limit &&
71613590Srekai.gonzalezalberquilla@arm.com                !req->mainRequest()->isLLSC()) {
7174032Sktlim@umich.edu
71813590Srekai.gonzalezalberquilla@arm.com                // Get shift amount for offset into the store's data.
71913590Srekai.gonzalezalberquilla@arm.com                int shift_amt = req->mainRequest()->getVaddr() -
72013590Srekai.gonzalezalberquilla@arm.com                    store_it->instruction()->effAddr;
7212292SN/A
72213590Srekai.gonzalezalberquilla@arm.com                // Allocate memory if this is the first time a load is issued.
72313590Srekai.gonzalezalberquilla@arm.com                if (!load_inst->memData) {
72413590Srekai.gonzalezalberquilla@arm.com                    load_inst->memData =
72513590Srekai.gonzalezalberquilla@arm.com                        new uint8_t[req->mainRequest()->getSize()];
72613590Srekai.gonzalezalberquilla@arm.com                }
72713590Srekai.gonzalezalberquilla@arm.com                if (store_it->isAllZeros())
72813590Srekai.gonzalezalberquilla@arm.com                    memset(load_inst->memData, 0,
72913590Srekai.gonzalezalberquilla@arm.com                            req->mainRequest()->getSize());
73013590Srekai.gonzalezalberquilla@arm.com                else
73113590Srekai.gonzalezalberquilla@arm.com                    memcpy(load_inst->memData,
73213590Srekai.gonzalezalberquilla@arm.com                        store_it->data() + shift_amt,
73313590Srekai.gonzalezalberquilla@arm.com                        req->mainRequest()->getSize());
7342292SN/A
73513590Srekai.gonzalezalberquilla@arm.com                DPRINTF(LSQUnit, "Forwarding from store idx %i to load to "
73613590Srekai.gonzalezalberquilla@arm.com                        "addr %#x\n", store_it._idx,
73713590Srekai.gonzalezalberquilla@arm.com                        req->mainRequest()->getVaddr());
7382292SN/A
73913590Srekai.gonzalezalberquilla@arm.com                PacketPtr data_pkt = new Packet(req->mainRequest(),
74013590Srekai.gonzalezalberquilla@arm.com                        MemCmd::ReadReq);
74113590Srekai.gonzalezalberquilla@arm.com                data_pkt->dataStatic(load_inst->memData);
7422292SN/A
74313590Srekai.gonzalezalberquilla@arm.com                if (req->isAnyOutstandingRequest()) {
74413590Srekai.gonzalezalberquilla@arm.com                    assert(req->_numOutstandingPackets > 0);
74513590Srekai.gonzalezalberquilla@arm.com                    // There are memory requests packets in flight already.
74613590Srekai.gonzalezalberquilla@arm.com                    // This may happen if the store was not complete the
74713590Srekai.gonzalezalberquilla@arm.com                    // first time this load got executed. Signal the senderSate
74813590Srekai.gonzalezalberquilla@arm.com                    // that response packets should be discarded.
74913590Srekai.gonzalezalberquilla@arm.com                    req->discardSenderState();
75013590Srekai.gonzalezalberquilla@arm.com                }
7512678Sktlim@umich.edu
75213590Srekai.gonzalezalberquilla@arm.com                WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt,
75313590Srekai.gonzalezalberquilla@arm.com                        this);
7542678Sktlim@umich.edu
75513590Srekai.gonzalezalberquilla@arm.com                // We'll say this has a 1 cycle load-store forwarding latency
75613590Srekai.gonzalezalberquilla@arm.com                // for now.
75713590Srekai.gonzalezalberquilla@arm.com                // @todo: Need to make this a parameter.
75813590Srekai.gonzalezalberquilla@arm.com                cpu->schedule(wb, curTick());
7592292SN/A
76013590Srekai.gonzalezalberquilla@arm.com                // Don't need to do anything special for split loads.
76113590Srekai.gonzalezalberquilla@arm.com                ++lsqForwLoads;
7622678Sktlim@umich.edu
76313590Srekai.gonzalezalberquilla@arm.com                return NoFault;
76413590Srekai.gonzalezalberquilla@arm.com            } else if (
76513652Sqtt2@cornell.edu                // This is the partial store-load forwarding case where a store
76613652Sqtt2@cornell.edu                // has only part of the load's data and the load isn't LLSC
76713590Srekai.gonzalezalberquilla@arm.com                (!req->mainRequest()->isLLSC() &&
76812022Sar4jc@virginia.edu                 ((store_has_lower_limit && lower_load_has_store_part) ||
76912022Sar4jc@virginia.edu                  (store_has_upper_limit && upper_load_has_store_part) ||
77012022Sar4jc@virginia.edu                  (lower_load_has_store_part && upper_load_has_store_part))) ||
77113652Sqtt2@cornell.edu                // The load is LLSC, and the store has all or part of the
77213652Sqtt2@cornell.edu                // load's data
77313590Srekai.gonzalezalberquilla@arm.com                (req->mainRequest()->isLLSC() &&
77412022Sar4jc@virginia.edu                 ((store_has_lower_limit || upper_load_has_store_part) &&
77513652Sqtt2@cornell.edu                  (store_has_upper_limit || lower_load_has_store_part))) ||
77613652Sqtt2@cornell.edu                // The store entry is atomic and has all or part of the load's
77713652Sqtt2@cornell.edu                // data
77813652Sqtt2@cornell.edu                (store_it->instruction()->isAtomic() &&
77913652Sqtt2@cornell.edu                 ((store_has_lower_limit || upper_load_has_store_part) &&
78012022Sar4jc@virginia.edu                  (store_has_upper_limit || lower_load_has_store_part)))) {
7812292SN/A
78213590Srekai.gonzalezalberquilla@arm.com                // If it's already been written back, then don't worry about
78313590Srekai.gonzalezalberquilla@arm.com                // stalling on it.
78413590Srekai.gonzalezalberquilla@arm.com                if (store_it->completed()) {
78513590Srekai.gonzalezalberquilla@arm.com                    panic("Should not check one of these");
78613590Srekai.gonzalezalberquilla@arm.com                    continue;
78713590Srekai.gonzalezalberquilla@arm.com                }
78813590Srekai.gonzalezalberquilla@arm.com
78913590Srekai.gonzalezalberquilla@arm.com                // Must stall load and force it to retry, so long as it's the
79013590Srekai.gonzalezalberquilla@arm.com                // oldest load that needs to do so.
79113590Srekai.gonzalezalberquilla@arm.com                if (!stalled ||
79213590Srekai.gonzalezalberquilla@arm.com                    (stalled &&
79313590Srekai.gonzalezalberquilla@arm.com                     load_inst->seqNum <
79413590Srekai.gonzalezalberquilla@arm.com                     loadQueue[stallingLoadIdx].instruction()->seqNum)) {
79513590Srekai.gonzalezalberquilla@arm.com                    stalled = true;
79613590Srekai.gonzalezalberquilla@arm.com                    stallingStoreIsn = store_it->instruction()->seqNum;
79713590Srekai.gonzalezalberquilla@arm.com                    stallingLoadIdx = load_idx;
79813590Srekai.gonzalezalberquilla@arm.com                }
79913590Srekai.gonzalezalberquilla@arm.com
80013590Srekai.gonzalezalberquilla@arm.com                // Tell IQ/mem dep unit that this instruction will need to be
80113590Srekai.gonzalezalberquilla@arm.com                // rescheduled eventually
80213590Srekai.gonzalezalberquilla@arm.com                iewStage->rescheduleMemInst(load_inst);
80313590Srekai.gonzalezalberquilla@arm.com                load_inst->clearIssued();
80413590Srekai.gonzalezalberquilla@arm.com                load_inst->effAddrValid(false);
80513590Srekai.gonzalezalberquilla@arm.com                ++lsqRescheduledLoads;
80613590Srekai.gonzalezalberquilla@arm.com
80713590Srekai.gonzalezalberquilla@arm.com                // Do not generate a writeback event as this instruction is not
80813590Srekai.gonzalezalberquilla@arm.com                // complete.
80913590Srekai.gonzalezalberquilla@arm.com                DPRINTF(LSQUnit, "Load-store forwarding mis-match. "
81013590Srekai.gonzalezalberquilla@arm.com                        "Store idx %i to load addr %#x\n",
81113590Srekai.gonzalezalberquilla@arm.com                        store_it._idx, req->mainRequest()->getVaddr());
81213590Srekai.gonzalezalberquilla@arm.com
81313590Srekai.gonzalezalberquilla@arm.com                // Must discard the request.
81413590Srekai.gonzalezalberquilla@arm.com                req->discard();
81513590Srekai.gonzalezalberquilla@arm.com                load_req.setRequest(nullptr);
81613590Srekai.gonzalezalberquilla@arm.com                return NoFault;
8172292SN/A            }
8182292SN/A        }
8192292SN/A    }
8202292SN/A
8212292SN/A    // If there's no forwarding case, then go access memory
8227720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Doing memory access for inst [sn:%lli] PC %s\n",
8237720Sgblack@eecs.umich.edu            load_inst->seqNum, load_inst->pcState());
8242292SN/A
82510333Smitch.hayenga@arm.com    // Allocate memory if this is the first time a load is issued.
82610333Smitch.hayenga@arm.com    if (!load_inst->memData) {
82713590Srekai.gonzalezalberquilla@arm.com        load_inst->memData = new uint8_t[req->mainRequest()->getSize()];
82810333Smitch.hayenga@arm.com    }
8296974Stjones1@inf.ed.ac.uk
83011780Sarthur.perais@inria.fr    // For now, load throughput is constrained by the number of
83111780Sarthur.perais@inria.fr    // load FUs only, and loads do not consume a cache port (only
83211780Sarthur.perais@inria.fr    // stores do).
83311780Sarthur.perais@inria.fr    // @todo We should account for cache port contention
83411780Sarthur.perais@inria.fr    // and arbitrate between loads and stores.
8356974Stjones1@inf.ed.ac.uk
83613590Srekai.gonzalezalberquilla@arm.com    // if we the cache is not blocked, do cache access
83713590Srekai.gonzalezalberquilla@arm.com    if (req->senderState() == nullptr) {
83813590Srekai.gonzalezalberquilla@arm.com        LQSenderState *state = new LQSenderState(
83913590Srekai.gonzalezalberquilla@arm.com                loadQueue.getIterator(load_idx));
84013590Srekai.gonzalezalberquilla@arm.com        state->isLoad = true;
84113590Srekai.gonzalezalberquilla@arm.com        state->inst = load_inst;
84213590Srekai.gonzalezalberquilla@arm.com        state->isSplit = req->isSplit();
84313590Srekai.gonzalezalberquilla@arm.com        req->senderState(state);
8442907Sktlim@umich.edu    }
84513590Srekai.gonzalezalberquilla@arm.com    req->buildPackets();
84613590Srekai.gonzalezalberquilla@arm.com    req->sendPacketToCache();
84713590Srekai.gonzalezalberquilla@arm.com    if (!req->isSent())
84810333Smitch.hayenga@arm.com        iewStage->blockMemInst(load_inst);
8492292SN/A
8502669Sktlim@umich.edu    return NoFault;
8512292SN/A}
8522292SN/A
8532292SN/Atemplate <class Impl>
8542292SN/AFault
85513590Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::write(LSQRequest *req, uint8_t *data, int store_idx)
8562292SN/A{
85713590Srekai.gonzalezalberquilla@arm.com    assert(storeQueue[store_idx].valid());
8582292SN/A
85913590Srekai.gonzalezalberquilla@arm.com    DPRINTF(LSQUnit, "Doing write to store idx %i, addr %#x | storeHead:%i "
86013831SAndrea.Mondelli@ucf.edu            "[sn:%llu]\n",
86113590Srekai.gonzalezalberquilla@arm.com            store_idx - 1, req->request()->getPaddr(), storeQueue.head() - 1,
86213590Srekai.gonzalezalberquilla@arm.com            storeQueue[store_idx].instruction()->seqNum);
8632329SN/A
86413590Srekai.gonzalezalberquilla@arm.com    storeQueue[store_idx].setRequest(req);
86513590Srekai.gonzalezalberquilla@arm.com    unsigned size = req->_size;
86613590Srekai.gonzalezalberquilla@arm.com    storeQueue[store_idx].size() = size;
86713590Srekai.gonzalezalberquilla@arm.com    bool store_no_data =
86813590Srekai.gonzalezalberquilla@arm.com        req->mainRequest()->getFlags() & Request::STORE_NO_DATA;
86913590Srekai.gonzalezalberquilla@arm.com    storeQueue[store_idx].isAllZeros() = store_no_data;
87013590Srekai.gonzalezalberquilla@arm.com    assert(size <= SQEntry::DataSize || store_no_data);
8717509Stjones1@inf.ed.ac.uk
87213652Sqtt2@cornell.edu    // copy data into the storeQueue only if the store request has valid data
87313590Srekai.gonzalezalberquilla@arm.com    if (!(req->request()->getFlags() & Request::CACHE_BLOCK_ZERO) &&
87413652Sqtt2@cornell.edu        !req->request()->isCacheMaintenance() &&
87513652Sqtt2@cornell.edu        !req->request()->isAtomic())
87613590Srekai.gonzalezalberquilla@arm.com        memcpy(storeQueue[store_idx].data(), data, size);
8772329SN/A
8782292SN/A    // This function only writes the data to the store queue, so no fault
8792292SN/A    // can happen here.
8802292SN/A    return NoFault;
8812292SN/A}
8822292SN/A
8832292SN/A#endif // __CPU_O3_LSQ_UNIT_HH__
884