lsq_unit.hh revision 13831
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"
548506Sgblack@eecs.umich.edu#include "arch/isa_traits.hh"
553326Sktlim@umich.edu#include "arch/locked_mem.hh"
568481Sgblack@eecs.umich.edu#include "arch/mmapped_ipr.hh"
576658Snate@binkert.org#include "config/the_isa.hh"
582292SN/A#include "cpu/inst_seq.hh"
598230Snate@binkert.org#include "cpu/timebuf.hh"
608232Snate@binkert.org#include "debug/LSQUnit.hh"
613348Sbinkertn@umich.edu#include "mem/packet.hh"
622669Sktlim@umich.edu#include "mem/port.hh"
632292SN/A
648737Skoansin.tan@gmail.comstruct DerivO3CPUParams;
6513590Srekai.gonzalezalberquilla@arm.com#include "base/circular_queue.hh"
665529Snate@binkert.org
672292SN/A/**
682329SN/A * Class that implements the actual LQ and SQ for each specific
692329SN/A * thread.  Both are circular queues; load entries are freed upon
702329SN/A * committing, while store entries are freed once they writeback. The
712329SN/A * LSQUnit tracks if there are memory ordering violations, and also
722329SN/A * detects partial load to store forwarding cases (a store only has
732329SN/A * part of a load's data) that requires the load to wait until the
742329SN/A * store writes back. In the former case it holds onto the instruction
752329SN/A * until the dependence unit looks at it, and in the latter it stalls
762329SN/A * the LSQ until the store writes back. At that point the load is
772329SN/A * replayed.
782292SN/A */
792292SN/Atemplate <class Impl>
8013590Srekai.gonzalezalberquilla@arm.comclass LSQUnit
8113590Srekai.gonzalezalberquilla@arm.com{
822292SN/A  public:
832733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
842292SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
852292SN/A    typedef typename Impl::CPUPol::IEW IEW;
862907Sktlim@umich.edu    typedef typename Impl::CPUPol::LSQ LSQ;
872292SN/A    typedef typename Impl::CPUPol::IssueStruct IssueStruct;
882292SN/A
8913590Srekai.gonzalezalberquilla@arm.com    using LSQSenderState = typename LSQ::LSQSenderState;
9013590Srekai.gonzalezalberquilla@arm.com    using LSQRequest = typename Impl::CPUPol::LSQ::LSQRequest;
9113590Srekai.gonzalezalberquilla@arm.com  private:
9213590Srekai.gonzalezalberquilla@arm.com    class LSQEntry
9313590Srekai.gonzalezalberquilla@arm.com    {
9413590Srekai.gonzalezalberquilla@arm.com      private:
9513590Srekai.gonzalezalberquilla@arm.com        /** The instruction. */
9613590Srekai.gonzalezalberquilla@arm.com        DynInstPtr inst;
9713590Srekai.gonzalezalberquilla@arm.com        /** The request. */
9813590Srekai.gonzalezalberquilla@arm.com        LSQRequest* req;
9913590Srekai.gonzalezalberquilla@arm.com        /** The size of the operation. */
10013590Srekai.gonzalezalberquilla@arm.com        uint8_t _size;
10113590Srekai.gonzalezalberquilla@arm.com        /** Valid entry. */
10213590Srekai.gonzalezalberquilla@arm.com        bool _valid;
10313590Srekai.gonzalezalberquilla@arm.com      public:
10413590Srekai.gonzalezalberquilla@arm.com        /** Constructs an empty store queue entry. */
10513590Srekai.gonzalezalberquilla@arm.com        LSQEntry()
10613590Srekai.gonzalezalberquilla@arm.com            : inst(nullptr), req(nullptr), _size(0), _valid(false)
10713590Srekai.gonzalezalberquilla@arm.com        {
10813590Srekai.gonzalezalberquilla@arm.com        }
10913590Srekai.gonzalezalberquilla@arm.com
11013590Srekai.gonzalezalberquilla@arm.com        ~LSQEntry()
11113590Srekai.gonzalezalberquilla@arm.com        {
11213590Srekai.gonzalezalberquilla@arm.com            inst = nullptr;
11313590Srekai.gonzalezalberquilla@arm.com            if (req != nullptr) {
11413590Srekai.gonzalezalberquilla@arm.com                req->freeLSQEntry();
11513590Srekai.gonzalezalberquilla@arm.com                req = nullptr;
11613590Srekai.gonzalezalberquilla@arm.com            }
11713590Srekai.gonzalezalberquilla@arm.com        }
11813590Srekai.gonzalezalberquilla@arm.com
11913590Srekai.gonzalezalberquilla@arm.com        void
12013590Srekai.gonzalezalberquilla@arm.com        clear()
12113590Srekai.gonzalezalberquilla@arm.com        {
12213590Srekai.gonzalezalberquilla@arm.com            inst = nullptr;
12313590Srekai.gonzalezalberquilla@arm.com            if (req != nullptr) {
12413590Srekai.gonzalezalberquilla@arm.com                req->freeLSQEntry();
12513590Srekai.gonzalezalberquilla@arm.com            }
12613590Srekai.gonzalezalberquilla@arm.com            req = nullptr;
12713590Srekai.gonzalezalberquilla@arm.com            _valid = false;
12813590Srekai.gonzalezalberquilla@arm.com            _size = 0;
12913590Srekai.gonzalezalberquilla@arm.com        }
13013590Srekai.gonzalezalberquilla@arm.com
13113590Srekai.gonzalezalberquilla@arm.com        void
13213590Srekai.gonzalezalberquilla@arm.com        set(const DynInstPtr& inst)
13313590Srekai.gonzalezalberquilla@arm.com        {
13413590Srekai.gonzalezalberquilla@arm.com            assert(!_valid);
13513590Srekai.gonzalezalberquilla@arm.com            this->inst = inst;
13613590Srekai.gonzalezalberquilla@arm.com            _valid = true;
13713590Srekai.gonzalezalberquilla@arm.com            _size = 0;
13813590Srekai.gonzalezalberquilla@arm.com        }
13913590Srekai.gonzalezalberquilla@arm.com        LSQRequest* request() { return req; }
14013590Srekai.gonzalezalberquilla@arm.com        void setRequest(LSQRequest* r) { req = r; }
14113590Srekai.gonzalezalberquilla@arm.com        bool hasRequest() { return req != nullptr; }
14213590Srekai.gonzalezalberquilla@arm.com        /** Member accessors. */
14313590Srekai.gonzalezalberquilla@arm.com        /** @{ */
14413590Srekai.gonzalezalberquilla@arm.com        bool valid() const { return _valid; }
14513590Srekai.gonzalezalberquilla@arm.com        uint8_t& size() { return _size; }
14613590Srekai.gonzalezalberquilla@arm.com        const uint8_t& size() const { return _size; }
14713590Srekai.gonzalezalberquilla@arm.com        const DynInstPtr& instruction() const { return inst; }
14813590Srekai.gonzalezalberquilla@arm.com        /** @} */
14913590Srekai.gonzalezalberquilla@arm.com    };
15013590Srekai.gonzalezalberquilla@arm.com
15113590Srekai.gonzalezalberquilla@arm.com    class SQEntry : public LSQEntry
15213590Srekai.gonzalezalberquilla@arm.com    {
15313590Srekai.gonzalezalberquilla@arm.com      private:
15413590Srekai.gonzalezalberquilla@arm.com        /** The store data. */
15513590Srekai.gonzalezalberquilla@arm.com        char _data[64];  // TODO: 64 should become a parameter
15613590Srekai.gonzalezalberquilla@arm.com        /** Whether or not the store can writeback. */
15713590Srekai.gonzalezalberquilla@arm.com        bool _canWB;
15813590Srekai.gonzalezalberquilla@arm.com        /** Whether or not the store is committed. */
15913590Srekai.gonzalezalberquilla@arm.com        bool _committed;
16013590Srekai.gonzalezalberquilla@arm.com        /** Whether or not the store is completed. */
16113590Srekai.gonzalezalberquilla@arm.com        bool _completed;
16213590Srekai.gonzalezalberquilla@arm.com        /** Does this request write all zeros and thus doesn't
16313590Srekai.gonzalezalberquilla@arm.com         * have any data attached to it. Used for cache block zero
16413590Srekai.gonzalezalberquilla@arm.com         * style instructs (ARM DC ZVA; ALPHA WH64)
16513590Srekai.gonzalezalberquilla@arm.com         */
16613590Srekai.gonzalezalberquilla@arm.com        bool _isAllZeros;
16713590Srekai.gonzalezalberquilla@arm.com      public:
16813590Srekai.gonzalezalberquilla@arm.com        static constexpr size_t DataSize = sizeof(_data);
16913590Srekai.gonzalezalberquilla@arm.com        /** Constructs an empty store queue entry. */
17013590Srekai.gonzalezalberquilla@arm.com        SQEntry()
17113590Srekai.gonzalezalberquilla@arm.com            : _canWB(false), _committed(false), _completed(false),
17213590Srekai.gonzalezalberquilla@arm.com              _isAllZeros(false)
17313590Srekai.gonzalezalberquilla@arm.com        {
17413590Srekai.gonzalezalberquilla@arm.com            std::memset(_data, 0, DataSize);
17513590Srekai.gonzalezalberquilla@arm.com        }
17613590Srekai.gonzalezalberquilla@arm.com
17713590Srekai.gonzalezalberquilla@arm.com        ~SQEntry()
17813590Srekai.gonzalezalberquilla@arm.com        {
17913590Srekai.gonzalezalberquilla@arm.com        }
18013590Srekai.gonzalezalberquilla@arm.com
18113590Srekai.gonzalezalberquilla@arm.com        void
18213590Srekai.gonzalezalberquilla@arm.com        set(const DynInstPtr& inst)
18313590Srekai.gonzalezalberquilla@arm.com        {
18413590Srekai.gonzalezalberquilla@arm.com            LSQEntry::set(inst);
18513590Srekai.gonzalezalberquilla@arm.com        }
18613590Srekai.gonzalezalberquilla@arm.com
18713590Srekai.gonzalezalberquilla@arm.com        void
18813590Srekai.gonzalezalberquilla@arm.com        clear()
18913590Srekai.gonzalezalberquilla@arm.com        {
19013590Srekai.gonzalezalberquilla@arm.com            LSQEntry::clear();
19113590Srekai.gonzalezalberquilla@arm.com            _canWB = _completed = _committed = _isAllZeros = false;
19213590Srekai.gonzalezalberquilla@arm.com        }
19313590Srekai.gonzalezalberquilla@arm.com        /** Member accessors. */
19413590Srekai.gonzalezalberquilla@arm.com        /** @{ */
19513590Srekai.gonzalezalberquilla@arm.com        bool& canWB() { return _canWB; }
19613590Srekai.gonzalezalberquilla@arm.com        const bool& canWB() const { return _canWB; }
19713590Srekai.gonzalezalberquilla@arm.com        bool& completed() { return _completed; }
19813590Srekai.gonzalezalberquilla@arm.com        const bool& completed() const { return _completed; }
19913590Srekai.gonzalezalberquilla@arm.com        bool& committed() { return _committed; }
20013590Srekai.gonzalezalberquilla@arm.com        const bool& committed() const { return _committed; }
20113590Srekai.gonzalezalberquilla@arm.com        bool& isAllZeros() { return _isAllZeros; }
20213590Srekai.gonzalezalberquilla@arm.com        const bool& isAllZeros() const { return _isAllZeros; }
20313590Srekai.gonzalezalberquilla@arm.com        char* data() { return _data; }
20413590Srekai.gonzalezalberquilla@arm.com        const char* data() const { return _data; }
20513590Srekai.gonzalezalberquilla@arm.com        /** @} */
20613590Srekai.gonzalezalberquilla@arm.com    };
20713590Srekai.gonzalezalberquilla@arm.com    using LQEntry = LSQEntry;
20813590Srekai.gonzalezalberquilla@arm.com
20913590Srekai.gonzalezalberquilla@arm.com  public:
21013590Srekai.gonzalezalberquilla@arm.com    using LoadQueue = CircularQueue<LQEntry>;
21113590Srekai.gonzalezalberquilla@arm.com    using StoreQueue = CircularQueue<SQEntry>;
21213590Srekai.gonzalezalberquilla@arm.com
2132292SN/A  public:
2142292SN/A    /** Constructs an LSQ unit. init() must be called prior to use. */
21513472Srekai.gonzalezalberquilla@arm.com    LSQUnit(uint32_t lqEntries, uint32_t sqEntries);
21613472Srekai.gonzalezalberquilla@arm.com
21713472Srekai.gonzalezalberquilla@arm.com    /** We cannot copy LSQUnit because it has stats for which copy
21813472Srekai.gonzalezalberquilla@arm.com     * contructor is deleted explicitly. However, STL vector requires
21913472Srekai.gonzalezalberquilla@arm.com     * a valid copy constructor for the base type at compile time.
22013472Srekai.gonzalezalberquilla@arm.com     */
22113472Srekai.gonzalezalberquilla@arm.com    LSQUnit(const LSQUnit &l) { panic("LSQUnit is not copy-able"); }
2222292SN/A
2232292SN/A    /** Initializes the LSQ unit with the specified number of entries. */
2245529Snate@binkert.org    void init(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params,
22513472Srekai.gonzalezalberquilla@arm.com            LSQ *lsq_ptr, unsigned id);
2262292SN/A
2272292SN/A    /** Returns the name of the LSQ unit. */
2282292SN/A    std::string name() const;
2292292SN/A
2302727Sktlim@umich.edu    /** Registers statistics. */
2312727Sktlim@umich.edu    void regStats();
2322727Sktlim@umich.edu
2332907Sktlim@umich.edu    /** Sets the pointer to the dcache port. */
2348922Swilliam.wang@arm.com    void setDcachePort(MasterPort *dcache_port);
2352907Sktlim@umich.edu
2369444SAndreas.Sandberg@ARM.com    /** Perform sanity checks after a drain. */
2379444SAndreas.Sandberg@ARM.com    void drainSanityCheck() const;
2382307SN/A
2392348SN/A    /** Takes over from another CPU's thread. */
2402307SN/A    void takeOverFrom();
2412307SN/A
2422292SN/A    /** Inserts an instruction. */
24313429Srekai.gonzalezalberquilla@arm.com    void insert(const DynInstPtr &inst);
2442292SN/A    /** Inserts a load instruction. */
24513429Srekai.gonzalezalberquilla@arm.com    void insertLoad(const DynInstPtr &load_inst);
2462292SN/A    /** Inserts a store instruction. */
24713429Srekai.gonzalezalberquilla@arm.com    void insertStore(const DynInstPtr &store_inst);
2482292SN/A
2498545Ssaidi@eecs.umich.edu    /** Check for ordering violations in the LSQ. For a store squash if we
2508545Ssaidi@eecs.umich.edu     * ever find a conflicting load. For a load, only squash if we
2518545Ssaidi@eecs.umich.edu     * an external snoop invalidate has been seen for that load address
2528199SAli.Saidi@ARM.com     * @param load_idx index to start checking at
2538199SAli.Saidi@ARM.com     * @param inst the instruction to check
2548199SAli.Saidi@ARM.com     */
25513590Srekai.gonzalezalberquilla@arm.com    Fault checkViolations(typename LoadQueue::iterator& loadIt,
25613590Srekai.gonzalezalberquilla@arm.com            const DynInstPtr& inst);
2578199SAli.Saidi@ARM.com
2588545Ssaidi@eecs.umich.edu    /** Check if an incoming invalidate hits in the lsq on a load
2598545Ssaidi@eecs.umich.edu     * that might have issued out of order wrt another load beacuse
2608545Ssaidi@eecs.umich.edu     * of the intermediate invalidate.
2618545Ssaidi@eecs.umich.edu     */
2628545Ssaidi@eecs.umich.edu    void checkSnoop(PacketPtr pkt);
2638545Ssaidi@eecs.umich.edu
2642292SN/A    /** Executes a load instruction. */
26513429Srekai.gonzalezalberquilla@arm.com    Fault executeLoad(const DynInstPtr &inst);
2662292SN/A
2672329SN/A    Fault executeLoad(int lq_idx) { panic("Not implemented"); return NoFault; }
2682292SN/A    /** Executes a store instruction. */
26913429Srekai.gonzalezalberquilla@arm.com    Fault executeStore(const DynInstPtr &inst);
2702292SN/A
2712292SN/A    /** Commits the head load. */
2722292SN/A    void commitLoad();
2732292SN/A    /** Commits loads older than a specific sequence number. */
2742292SN/A    void commitLoads(InstSeqNum &youngest_inst);
2752292SN/A
2762292SN/A    /** Commits stores older than a specific sequence number. */
2772292SN/A    void commitStores(InstSeqNum &youngest_inst);
2782292SN/A
2792292SN/A    /** Writes back stores. */
2802292SN/A    void writebackStores();
2812292SN/A
2822790Sktlim@umich.edu    /** Completes the data access that has been returned from the
2832790Sktlim@umich.edu     * memory system. */
2842669Sktlim@umich.edu    void completeDataAccess(PacketPtr pkt);
2852669Sktlim@umich.edu
2862292SN/A    /** Squashes all instructions younger than a specific sequence number. */
2872292SN/A    void squash(const InstSeqNum &squashed_num);
2882292SN/A
2892292SN/A    /** Returns if there is a memory ordering violation. Value is reset upon
2902292SN/A     * call to getMemDepViolator().
2912292SN/A     */
2922292SN/A    bool violation() { return memDepViolator; }
2932292SN/A
2942292SN/A    /** Returns the memory ordering violator. */
2952292SN/A    DynInstPtr getMemDepViolator();
2962292SN/A
29710239Sbinhpham@cs.rutgers.edu    /** Returns the number of free LQ entries. */
29810239Sbinhpham@cs.rutgers.edu    unsigned numFreeLoadEntries();
29910239Sbinhpham@cs.rutgers.edu
30010239Sbinhpham@cs.rutgers.edu    /** Returns the number of free SQ entries. */
30110239Sbinhpham@cs.rutgers.edu    unsigned numFreeStoreEntries();
3022292SN/A
3032292SN/A    /** Returns the number of loads in the LQ. */
3042292SN/A    int numLoads() { return loads; }
3052292SN/A
3062292SN/A    /** Returns the number of stores in the SQ. */
3072292SN/A    int numStores() { return stores; }
3082292SN/A
3092292SN/A    /** Returns if either the LQ or SQ is full. */
3102292SN/A    bool isFull() { return lqFull() || sqFull(); }
3112292SN/A
3129444SAndreas.Sandberg@ARM.com    /** Returns if both the LQ and SQ are empty. */
3139444SAndreas.Sandberg@ARM.com    bool isEmpty() const { return lqEmpty() && sqEmpty(); }
3149444SAndreas.Sandberg@ARM.com
3152292SN/A    /** Returns if the LQ is full. */
31613590Srekai.gonzalezalberquilla@arm.com    bool lqFull() { return loadQueue.full(); }
3172292SN/A
3182292SN/A    /** Returns if the SQ is full. */
31913590Srekai.gonzalezalberquilla@arm.com    bool sqFull() { return storeQueue.full(); }
3202292SN/A
3219444SAndreas.Sandberg@ARM.com    /** Returns if the LQ is empty. */
3229444SAndreas.Sandberg@ARM.com    bool lqEmpty() const { return loads == 0; }
3239444SAndreas.Sandberg@ARM.com
3249444SAndreas.Sandberg@ARM.com    /** Returns if the SQ is empty. */
3259444SAndreas.Sandberg@ARM.com    bool sqEmpty() const { return stores == 0; }
3269444SAndreas.Sandberg@ARM.com
3272292SN/A    /** Returns the number of instructions in the LSQ. */
3282292SN/A    unsigned getCount() { return loads + stores; }
3292292SN/A
3302292SN/A    /** Returns if there are any stores to writeback. */
3312292SN/A    bool hasStoresToWB() { return storesToWB; }
3322292SN/A
3332292SN/A    /** Returns the number of stores to writeback. */
3342292SN/A    int numStoresToWB() { return storesToWB; }
3352292SN/A
3362292SN/A    /** Returns if the LSQ unit will writeback on this cycle. */
33713590Srekai.gonzalezalberquilla@arm.com    bool
33813590Srekai.gonzalezalberquilla@arm.com    willWB()
33913590Srekai.gonzalezalberquilla@arm.com    {
34013590Srekai.gonzalezalberquilla@arm.com        return storeWBIt.dereferenceable() &&
34113590Srekai.gonzalezalberquilla@arm.com                        storeWBIt->valid() &&
34213590Srekai.gonzalezalberquilla@arm.com                        storeWBIt->canWB() &&
34313590Srekai.gonzalezalberquilla@arm.com                        !storeWBIt->completed() &&
34413590Srekai.gonzalezalberquilla@arm.com                        !isStoreBlocked;
34513590Srekai.gonzalezalberquilla@arm.com    }
3462292SN/A
3472907Sktlim@umich.edu    /** Handles doing the retry. */
3482907Sktlim@umich.edu    void recvRetry();
3492907Sktlim@umich.edu
35013590Srekai.gonzalezalberquilla@arm.com    unsigned int cacheLineSize();
3512292SN/A  private:
3529444SAndreas.Sandberg@ARM.com    /** Reset the LSQ state */
3539444SAndreas.Sandberg@ARM.com    void resetState();
3549444SAndreas.Sandberg@ARM.com
3552698Sktlim@umich.edu    /** Writes back the instruction, sending it to IEW. */
35613429Srekai.gonzalezalberquilla@arm.com    void writeback(const DynInstPtr &inst, PacketPtr pkt);
3572678Sktlim@umich.edu
35813590Srekai.gonzalezalberquilla@arm.com    /** Try to finish a previously blocked write back attempt */
35913590Srekai.gonzalezalberquilla@arm.com    void writebackBlockedStore();
36013590Srekai.gonzalezalberquilla@arm.com
36113590Srekai.gonzalezalberquilla@arm.com    /** Completes the store at the specified index. */
36213590Srekai.gonzalezalberquilla@arm.com    void completeStore(typename StoreQueue::iterator store_idx);
3636974Stjones1@inf.ed.ac.uk
3642698Sktlim@umich.edu    /** Handles completing the send of a store to memory. */
36513590Srekai.gonzalezalberquilla@arm.com    void storePostSend();
3662292SN/A
3672329SN/A  public:
36813590Srekai.gonzalezalberquilla@arm.com    /** Attempts to send a packet to the cache.
36913590Srekai.gonzalezalberquilla@arm.com     * Check if there are ports available. Return true if
37013590Srekai.gonzalezalberquilla@arm.com     * there are, false if there are not.
37113590Srekai.gonzalezalberquilla@arm.com     */
37213590Srekai.gonzalezalberquilla@arm.com    bool trySendPacket(bool isLoad, PacketPtr data_pkt);
37313590Srekai.gonzalezalberquilla@arm.com
37413590Srekai.gonzalezalberquilla@arm.com
3752329SN/A    /** Debugging function to dump instructions in the LSQ. */
3769440SAndreas.Sandberg@ARM.com    void dumpInsts() const;
3772329SN/A
37813590Srekai.gonzalezalberquilla@arm.com    /** Schedule event for the cpu. */
37913590Srekai.gonzalezalberquilla@arm.com    void schedule(Event& ev, Tick when) { cpu->schedule(ev, when); }
38013590Srekai.gonzalezalberquilla@arm.com
38113590Srekai.gonzalezalberquilla@arm.com    BaseTLB* dTLB() { return cpu->dtb; }
38213590Srekai.gonzalezalberquilla@arm.com
3832292SN/A  private:
3842292SN/A    /** Pointer to the CPU. */
3852733Sktlim@umich.edu    O3CPU *cpu;
3862292SN/A
3872292SN/A    /** Pointer to the IEW stage. */
3882292SN/A    IEW *iewStage;
3892292SN/A
3902907Sktlim@umich.edu    /** Pointer to the LSQ. */
3912907Sktlim@umich.edu    LSQ *lsq;
3922669Sktlim@umich.edu
3932907Sktlim@umich.edu    /** Pointer to the dcache port.  Used only for sending. */
3948922Swilliam.wang@arm.com    MasterPort *dcachePort;
3952292SN/A
39613590Srekai.gonzalezalberquilla@arm.com    /** Particularisation of the LSQSenderState to the LQ. */
39713590Srekai.gonzalezalberquilla@arm.com    class LQSenderState : public LSQSenderState
3982678Sktlim@umich.edu    {
39913590Srekai.gonzalezalberquilla@arm.com        using LSQSenderState::alive;
4002678Sktlim@umich.edu      public:
40113590Srekai.gonzalezalberquilla@arm.com        LQSenderState(typename LoadQueue::iterator idx_)
40213590Srekai.gonzalezalberquilla@arm.com            : LSQSenderState(idx_->request(), true), idx(idx_) { }
4032678Sktlim@umich.edu
40413590Srekai.gonzalezalberquilla@arm.com        /** The LQ index of the instruction. */
40513590Srekai.gonzalezalberquilla@arm.com        typename LoadQueue::iterator idx;
40613590Srekai.gonzalezalberquilla@arm.com        //virtual LSQRequest* request() { return idx->request(); }
40713590Srekai.gonzalezalberquilla@arm.com        virtual void
40813590Srekai.gonzalezalberquilla@arm.com        complete()
40913590Srekai.gonzalezalberquilla@arm.com        {
41013590Srekai.gonzalezalberquilla@arm.com            //if (alive())
41113590Srekai.gonzalezalberquilla@arm.com            //  idx->request()->senderState(nullptr);
41213590Srekai.gonzalezalberquilla@arm.com        }
41313590Srekai.gonzalezalberquilla@arm.com    };
4146974Stjones1@inf.ed.ac.uk
41513590Srekai.gonzalezalberquilla@arm.com    /** Particularisation of the LSQSenderState to the SQ. */
41613590Srekai.gonzalezalberquilla@arm.com    class SQSenderState : public LSQSenderState
41713590Srekai.gonzalezalberquilla@arm.com    {
41813590Srekai.gonzalezalberquilla@arm.com        using LSQSenderState::alive;
41913590Srekai.gonzalezalberquilla@arm.com      public:
42013590Srekai.gonzalezalberquilla@arm.com        SQSenderState(typename StoreQueue::iterator idx_)
42113590Srekai.gonzalezalberquilla@arm.com            : LSQSenderState(idx_->request(), false), idx(idx_) { }
42213590Srekai.gonzalezalberquilla@arm.com        /** The SQ index of the instruction. */
42313590Srekai.gonzalezalberquilla@arm.com        typename StoreQueue::iterator idx;
42413590Srekai.gonzalezalberquilla@arm.com        //virtual LSQRequest* request() { return idx->request(); }
42513590Srekai.gonzalezalberquilla@arm.com        virtual void
42613590Srekai.gonzalezalberquilla@arm.com        complete()
42713590Srekai.gonzalezalberquilla@arm.com        {
42813590Srekai.gonzalezalberquilla@arm.com            //if (alive())
42913590Srekai.gonzalezalberquilla@arm.com            //   idx->request()->senderState(nullptr);
43013590Srekai.gonzalezalberquilla@arm.com        }
4312678Sktlim@umich.edu    };
4322678Sktlim@umich.edu
4332698Sktlim@umich.edu    /** Writeback event, specifically for when stores forward data to loads. */
43413590Srekai.gonzalezalberquilla@arm.com    class WritebackEvent : public Event
43513590Srekai.gonzalezalberquilla@arm.com    {
4362678Sktlim@umich.edu      public:
4372678Sktlim@umich.edu        /** Constructs a writeback event. */
43813429Srekai.gonzalezalberquilla@arm.com        WritebackEvent(const DynInstPtr &_inst, PacketPtr pkt,
43913429Srekai.gonzalezalberquilla@arm.com                LSQUnit *lsq_ptr);
4402678Sktlim@umich.edu
4412678Sktlim@umich.edu        /** Processes the writeback event. */
4422678Sktlim@umich.edu        void process();
4432678Sktlim@umich.edu
4442678Sktlim@umich.edu        /** Returns the description of this event. */
4455336Shines@cs.fsu.edu        const char *description() const;
4462678Sktlim@umich.edu
4472678Sktlim@umich.edu      private:
4482698Sktlim@umich.edu        /** Instruction whose results are being written back. */
4492678Sktlim@umich.edu        DynInstPtr inst;
4502678Sktlim@umich.edu
4512698Sktlim@umich.edu        /** The packet that would have been sent to memory. */
4522678Sktlim@umich.edu        PacketPtr pkt;
4532678Sktlim@umich.edu
4542678Sktlim@umich.edu        /** The pointer to the LSQ unit that issued the store. */
4552678Sktlim@umich.edu        LSQUnit<Impl> *lsqPtr;
4562678Sktlim@umich.edu    };
4572678Sktlim@umich.edu
4582292SN/A  public:
45913590Srekai.gonzalezalberquilla@arm.com    /**
46013590Srekai.gonzalezalberquilla@arm.com     * Handles writing back and completing the load or store that has
46113590Srekai.gonzalezalberquilla@arm.com     * returned from memory.
46213590Srekai.gonzalezalberquilla@arm.com     *
46313590Srekai.gonzalezalberquilla@arm.com     * @param pkt Response packet from the memory sub-system
46413590Srekai.gonzalezalberquilla@arm.com     */
46513590Srekai.gonzalezalberquilla@arm.com    bool recvTimingResp(PacketPtr pkt);
4662329SN/A
4672292SN/A  private:
4682292SN/A    /** The LSQUnit thread id. */
4696221Snate@binkert.org    ThreadID lsqID;
47013590Srekai.gonzalezalberquilla@arm.com  public:
4712292SN/A    /** The store queue. */
47213590Srekai.gonzalezalberquilla@arm.com    CircularQueue<SQEntry> storeQueue;
4732292SN/A
4742292SN/A    /** The load queue. */
47513590Srekai.gonzalezalberquilla@arm.com    LoadQueue loadQueue;
4762292SN/A
47713590Srekai.gonzalezalberquilla@arm.com  private:
4788199SAli.Saidi@ARM.com    /** The number of places to shift addresses in the LSQ before checking
4798199SAli.Saidi@ARM.com     * for dependency violations
4808199SAli.Saidi@ARM.com     */
4818199SAli.Saidi@ARM.com    unsigned depCheckShift;
4828199SAli.Saidi@ARM.com
4838199SAli.Saidi@ARM.com    /** Should loads be checked for dependency issues */
4848199SAli.Saidi@ARM.com    bool checkLoads;
4858199SAli.Saidi@ARM.com
4862292SN/A    /** The number of load instructions in the LQ. */
4872292SN/A    int loads;
4882329SN/A    /** The number of store instructions in the SQ. */
4892292SN/A    int stores;
4902292SN/A    /** The number of store instructions in the SQ waiting to writeback. */
4912292SN/A    int storesToWB;
4922292SN/A
4932329SN/A    /** The index of the first instruction that may be ready to be
4942329SN/A     * written back, and has not yet been written back.
4952292SN/A     */
49613590Srekai.gonzalezalberquilla@arm.com    typename StoreQueue::iterator storeWBIt;
4972292SN/A
4988545Ssaidi@eecs.umich.edu    /** Address Mask for a cache block (e.g. ~(cache_block_size-1)) */
4998545Ssaidi@eecs.umich.edu    Addr cacheBlockMask;
5008545Ssaidi@eecs.umich.edu
5012292SN/A    /** Wire to read information from the issue stage time queue. */
5022292SN/A    typename TimeBuffer<IssueStruct>::wire fromIssue;
5032292SN/A
5042292SN/A    /** Whether or not the LSQ is stalled. */
5052292SN/A    bool stalled;
5062292SN/A    /** The store that causes the stall due to partial store to load
5072292SN/A     * forwarding.
5082292SN/A     */
5092292SN/A    InstSeqNum stallingStoreIsn;
5102292SN/A    /** The index of the above store. */
5112292SN/A    int stallingLoadIdx;
5122292SN/A
5132698Sktlim@umich.edu    /** The packet that needs to be retried. */
5142698Sktlim@umich.edu    PacketPtr retryPkt;
5152693Sktlim@umich.edu
5162698Sktlim@umich.edu    /** Whehter or not a store is blocked due to the memory system. */
5172678Sktlim@umich.edu    bool isStoreBlocked;
5182678Sktlim@umich.edu
5198727Snilay@cs.wisc.edu    /** Whether or not a store is in flight. */
5208727Snilay@cs.wisc.edu    bool storeInFlight;
5218727Snilay@cs.wisc.edu
5222292SN/A    /** The oldest load that caused a memory ordering violation. */
5232292SN/A    DynInstPtr memDepViolator;
5242292SN/A
5256974Stjones1@inf.ed.ac.uk    /** Whether or not there is a packet that couldn't be sent because of
5266974Stjones1@inf.ed.ac.uk     * a lack of cache ports. */
52713590Srekai.gonzalezalberquilla@arm.com    bool hasPendingRequest;
5286974Stjones1@inf.ed.ac.uk
5296974Stjones1@inf.ed.ac.uk    /** The packet that is pending free cache ports. */
53013590Srekai.gonzalezalberquilla@arm.com    LSQRequest* pendingRequest;
5316974Stjones1@inf.ed.ac.uk
5328727Snilay@cs.wisc.edu    /** Flag for memory model. */
5338727Snilay@cs.wisc.edu    bool needsTSO;
5348727Snilay@cs.wisc.edu
5352292SN/A    // Will also need how many read/write ports the Dcache has.  Or keep track
5362292SN/A    // of that in stage that is one level up, and only call executeLoad/Store
5372292SN/A    // the appropriate number of times.
5382727Sktlim@umich.edu    /** Total number of loads forwaded from LSQ stores. */
5395999Snate@binkert.org    Stats::Scalar lsqForwLoads;
5402307SN/A
5413126Sktlim@umich.edu    /** Total number of loads ignored due to invalid addresses. */
5425999Snate@binkert.org    Stats::Scalar invAddrLoads;
5433126Sktlim@umich.edu
5443126Sktlim@umich.edu    /** Total number of squashed loads. */
5455999Snate@binkert.org    Stats::Scalar lsqSquashedLoads;
5463126Sktlim@umich.edu
5473126Sktlim@umich.edu    /** Total number of responses from the memory system that are
5483126Sktlim@umich.edu     * ignored due to the instruction already being squashed. */
5495999Snate@binkert.org    Stats::Scalar lsqIgnoredResponses;
5503126Sktlim@umich.edu
5513126Sktlim@umich.edu    /** Tota number of memory ordering violations. */
5525999Snate@binkert.org    Stats::Scalar lsqMemOrderViolation;
5533126Sktlim@umich.edu
5542727Sktlim@umich.edu    /** Total number of squashed stores. */
5555999Snate@binkert.org    Stats::Scalar lsqSquashedStores;
5562727Sktlim@umich.edu
5572727Sktlim@umich.edu    /** Total number of software prefetches ignored due to invalid addresses. */
5585999Snate@binkert.org    Stats::Scalar invAddrSwpfs;
5592727Sktlim@umich.edu
5602727Sktlim@umich.edu    /** Ready loads blocked due to partial store-forwarding. */
5615999Snate@binkert.org    Stats::Scalar lsqBlockedLoads;
5622727Sktlim@umich.edu
5632727Sktlim@umich.edu    /** Number of loads that were rescheduled. */
5645999Snate@binkert.org    Stats::Scalar lsqRescheduledLoads;
5652727Sktlim@umich.edu
5662727Sktlim@umich.edu    /** Number of times the LSQ is blocked due to the cache. */
5675999Snate@binkert.org    Stats::Scalar lsqCacheBlocked;
5682727Sktlim@umich.edu
5692292SN/A  public:
5702292SN/A    /** Executes the load at the given index. */
57113590Srekai.gonzalezalberquilla@arm.com    Fault read(LSQRequest *req, int load_idx);
5722292SN/A
5732292SN/A    /** Executes the store at the given index. */
57413590Srekai.gonzalezalberquilla@arm.com    Fault write(LSQRequest *req, uint8_t *data, int store_idx);
5752292SN/A
5762292SN/A    /** Returns the index of the head load instruction. */
57713590Srekai.gonzalezalberquilla@arm.com    int getLoadHead() { return loadQueue.head(); }
57813590Srekai.gonzalezalberquilla@arm.com
5792292SN/A    /** Returns the sequence number of the head load instruction. */
58013590Srekai.gonzalezalberquilla@arm.com    InstSeqNum
58113590Srekai.gonzalezalberquilla@arm.com    getLoadHeadSeqNum()
5822292SN/A    {
58313590Srekai.gonzalezalberquilla@arm.com        return loadQueue.front().valid()
58413590Srekai.gonzalezalberquilla@arm.com            ? loadQueue.front().instruction()->seqNum
58513590Srekai.gonzalezalberquilla@arm.com            : 0;
5862292SN/A    }
5872292SN/A
5882292SN/A    /** Returns the index of the head store instruction. */
58913590Srekai.gonzalezalberquilla@arm.com    int getStoreHead() { return storeQueue.head(); }
5902292SN/A    /** Returns the sequence number of the head store instruction. */
59113590Srekai.gonzalezalberquilla@arm.com    InstSeqNum
59213590Srekai.gonzalezalberquilla@arm.com    getStoreHeadSeqNum()
5932292SN/A    {
59413590Srekai.gonzalezalberquilla@arm.com        return storeQueue.front().valid()
59513590Srekai.gonzalezalberquilla@arm.com            ? storeQueue.front().instruction()->seqNum
59613590Srekai.gonzalezalberquilla@arm.com            : 0;
5972292SN/A    }
5982292SN/A
5992292SN/A    /** Returns whether or not the LSQ unit is stalled. */
6002292SN/A    bool isStalled()  { return stalled; }
60113590Srekai.gonzalezalberquilla@arm.com  public:
60213590Srekai.gonzalezalberquilla@arm.com    typedef typename CircularQueue<LQEntry>::iterator LQIterator;
60313590Srekai.gonzalezalberquilla@arm.com    typedef typename CircularQueue<SQEntry>::iterator SQIterator;
60413590Srekai.gonzalezalberquilla@arm.com    typedef CircularQueue<LQEntry> LQueue;
60513590Srekai.gonzalezalberquilla@arm.com    typedef CircularQueue<SQEntry> SQueue;
6062292SN/A};
6072292SN/A
6082292SN/Atemplate <class Impl>
6092292SN/AFault
61013590Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::read(LSQRequest *req, int load_idx)
6112292SN/A{
61213590Srekai.gonzalezalberquilla@arm.com    LQEntry& load_req = loadQueue[load_idx];
61313590Srekai.gonzalezalberquilla@arm.com    const DynInstPtr& load_inst = load_req.instruction();
6142292SN/A
61513590Srekai.gonzalezalberquilla@arm.com    load_req.setRequest(req);
6162669Sktlim@umich.edu    assert(load_inst);
6172669Sktlim@umich.edu
6182669Sktlim@umich.edu    assert(!load_inst->isExecuted());
6192292SN/A
62010824SAndreas.Sandberg@ARM.com    // Make sure this isn't a strictly ordered load
62110824SAndreas.Sandberg@ARM.com    // A bit of a hackish way to get strictly ordered accesses to work
62210824SAndreas.Sandberg@ARM.com    // only if they're at the head of the LSQ and are ready to commit
62310824SAndreas.Sandberg@ARM.com    // (at the head of the ROB too).
62413590Srekai.gonzalezalberquilla@arm.com
62513590Srekai.gonzalezalberquilla@arm.com    if (req->mainRequest()->isStrictlyOrdered() &&
62613590Srekai.gonzalezalberquilla@arm.com        (load_idx != loadQueue.head() || !load_inst->isAtCommit())) {
62713590Srekai.gonzalezalberquilla@arm.com        // Tell IQ/mem dep unit that this instruction will need to be
62813590Srekai.gonzalezalberquilla@arm.com        // rescheduled eventually
6292669Sktlim@umich.edu        iewStage->rescheduleMemInst(load_inst);
63013590Srekai.gonzalezalberquilla@arm.com        load_inst->clearIssued();
63113590Srekai.gonzalezalberquilla@arm.com        load_inst->effAddrValid(false);
6322727Sktlim@umich.edu        ++lsqRescheduledLoads;
63310824SAndreas.Sandberg@ARM.com        DPRINTF(LSQUnit, "Strictly ordered load [sn:%lli] PC %s\n",
6347720Sgblack@eecs.umich.edu                load_inst->seqNum, load_inst->pcState());
6354032Sktlim@umich.edu
63613590Srekai.gonzalezalberquilla@arm.com        // Must delete request now that it wasn't handed off to
63713590Srekai.gonzalezalberquilla@arm.com        // memory.  This is quite ugly.  @todo: Figure out the proper
63813590Srekai.gonzalezalberquilla@arm.com        // place to really handle request deletes.
63913590Srekai.gonzalezalberquilla@arm.com        load_req.setRequest(nullptr);
64013590Srekai.gonzalezalberquilla@arm.com        req->discard();
64110474Sandreas.hansson@arm.com        return std::make_shared<GenericISA::M5PanicFault>(
64210824SAndreas.Sandberg@ARM.com            "Strictly ordered load [sn:%llx] PC %s\n",
64310474Sandreas.hansson@arm.com            load_inst->seqNum, load_inst->pcState());
6442292SN/A    }
6452292SN/A
6462292SN/A    DPRINTF(LSQUnit, "Read called, load idx: %i, store idx: %i, "
6476974Stjones1@inf.ed.ac.uk            "storeHead: %i addr: %#x%s\n",
64813590Srekai.gonzalezalberquilla@arm.com            load_idx - 1, load_inst->sqIt._idx, storeQueue.head() - 1,
64913590Srekai.gonzalezalberquilla@arm.com            req->mainRequest()->getPaddr(), req->isSplit() ? " split" : "");
6502292SN/A
65113590Srekai.gonzalezalberquilla@arm.com    if (req->mainRequest()->isLLSC()) {
6523326Sktlim@umich.edu        // Disable recording the result temporarily.  Writing to misc
6533326Sktlim@umich.edu        // regs normally updates the result, but this is not the
6543326Sktlim@umich.edu        // desired behavior when handling store conditionals.
6559046SAli.Saidi@ARM.com        load_inst->recordResult(false);
65613590Srekai.gonzalezalberquilla@arm.com        TheISA::handleLockedRead(load_inst.get(), req->mainRequest());
6579046SAli.Saidi@ARM.com        load_inst->recordResult(true);
6582292SN/A    }
6592292SN/A
66013590Srekai.gonzalezalberquilla@arm.com    if (req->mainRequest()->isMmappedIpr()) {
6618481Sgblack@eecs.umich.edu        assert(!load_inst->memData);
6628481Sgblack@eecs.umich.edu        load_inst->memData = new uint8_t[64];
6638481Sgblack@eecs.umich.edu
6648481Sgblack@eecs.umich.edu        ThreadContext *thread = cpu->tcBase(lsqID);
66513590Srekai.gonzalezalberquilla@arm.com        PacketPtr main_pkt = new Packet(req->mainRequest(), MemCmd::ReadReq);
6668481Sgblack@eecs.umich.edu
66713590Srekai.gonzalezalberquilla@arm.com        Cycles delay = req->handleIprRead(thread, main_pkt);
6688481Sgblack@eecs.umich.edu
66913590Srekai.gonzalezalberquilla@arm.com        WritebackEvent *wb = new WritebackEvent(load_inst, main_pkt, this);
6709179Sandreas.hansson@arm.com        cpu->schedule(wb, cpu->clockEdge(delay));
6718481Sgblack@eecs.umich.edu        return NoFault;
6728481Sgblack@eecs.umich.edu    }
6738481Sgblack@eecs.umich.edu
67413590Srekai.gonzalezalberquilla@arm.com    // Check the SQ for any previous stores that might lead to forwarding
67513590Srekai.gonzalezalberquilla@arm.com    auto store_it = load_inst->sqIt;
67613590Srekai.gonzalezalberquilla@arm.com    assert (store_it >= storeWBIt);
67713590Srekai.gonzalezalberquilla@arm.com    // End once we've reached the top of the LSQ
67813590Srekai.gonzalezalberquilla@arm.com    while (store_it != storeWBIt) {
67913590Srekai.gonzalezalberquilla@arm.com        // Move the index to one younger
68013590Srekai.gonzalezalberquilla@arm.com        store_it--;
68113590Srekai.gonzalezalberquilla@arm.com        assert(store_it->valid());
68213590Srekai.gonzalezalberquilla@arm.com        assert(store_it->instruction()->seqNum < load_inst->seqNum);
68313590Srekai.gonzalezalberquilla@arm.com        int store_size = store_it->size();
6842292SN/A
68513590Srekai.gonzalezalberquilla@arm.com        // Cache maintenance instructions go down via the store
68613590Srekai.gonzalezalberquilla@arm.com        // path but they carry no data and they shouldn't be
68713590Srekai.gonzalezalberquilla@arm.com        // considered for forwarding
68813590Srekai.gonzalezalberquilla@arm.com        if (store_size != 0 && !store_it->instruction()->strictlyOrdered() &&
68913590Srekai.gonzalezalberquilla@arm.com            !(store_it->request()->mainRequest() &&
69013590Srekai.gonzalezalberquilla@arm.com              store_it->request()->mainRequest()->isCacheMaintenance())) {
69113590Srekai.gonzalezalberquilla@arm.com            assert(store_it->instruction()->effAddrValid());
6922292SN/A
69313590Srekai.gonzalezalberquilla@arm.com            // Check if the store data is within the lower and upper bounds of
69413590Srekai.gonzalezalberquilla@arm.com            // addresses that the request needs.
69513590Srekai.gonzalezalberquilla@arm.com            auto req_s = req->mainRequest()->getVaddr();
69613590Srekai.gonzalezalberquilla@arm.com            auto req_e = req_s + req->mainRequest()->getSize();
69713590Srekai.gonzalezalberquilla@arm.com            auto st_s = store_it->instruction()->effAddr;
69813590Srekai.gonzalezalberquilla@arm.com            auto st_e = st_s + store_size;
6992292SN/A
70013590Srekai.gonzalezalberquilla@arm.com            bool store_has_lower_limit = req_s >= st_s;
70113590Srekai.gonzalezalberquilla@arm.com            bool store_has_upper_limit = req_e <= st_e;
70213590Srekai.gonzalezalberquilla@arm.com            bool lower_load_has_store_part = req_s < st_e;
70313590Srekai.gonzalezalberquilla@arm.com            bool upper_load_has_store_part = req_e > st_s;
7042292SN/A
70513652Sqtt2@cornell.edu            // If the store entry is not atomic (atomic does not have valid
70613652Sqtt2@cornell.edu            // data), the store has all of the data needed, and
70713652Sqtt2@cornell.edu            // the load is not LLSC, then
70813652Sqtt2@cornell.edu            // we can forward data from the store to the load
70913652Sqtt2@cornell.edu            if (!store_it->instruction()->isAtomic() &&
71013652Sqtt2@cornell.edu                store_has_lower_limit && store_has_upper_limit &&
71113590Srekai.gonzalezalberquilla@arm.com                !req->mainRequest()->isLLSC()) {
7124032Sktlim@umich.edu
71313590Srekai.gonzalezalberquilla@arm.com                // Get shift amount for offset into the store's data.
71413590Srekai.gonzalezalberquilla@arm.com                int shift_amt = req->mainRequest()->getVaddr() -
71513590Srekai.gonzalezalberquilla@arm.com                    store_it->instruction()->effAddr;
7162292SN/A
71713590Srekai.gonzalezalberquilla@arm.com                // Allocate memory if this is the first time a load is issued.
71813590Srekai.gonzalezalberquilla@arm.com                if (!load_inst->memData) {
71913590Srekai.gonzalezalberquilla@arm.com                    load_inst->memData =
72013590Srekai.gonzalezalberquilla@arm.com                        new uint8_t[req->mainRequest()->getSize()];
72113590Srekai.gonzalezalberquilla@arm.com                }
72213590Srekai.gonzalezalberquilla@arm.com                if (store_it->isAllZeros())
72313590Srekai.gonzalezalberquilla@arm.com                    memset(load_inst->memData, 0,
72413590Srekai.gonzalezalberquilla@arm.com                            req->mainRequest()->getSize());
72513590Srekai.gonzalezalberquilla@arm.com                else
72613590Srekai.gonzalezalberquilla@arm.com                    memcpy(load_inst->memData,
72713590Srekai.gonzalezalberquilla@arm.com                        store_it->data() + shift_amt,
72813590Srekai.gonzalezalberquilla@arm.com                        req->mainRequest()->getSize());
7292292SN/A
73013590Srekai.gonzalezalberquilla@arm.com                DPRINTF(LSQUnit, "Forwarding from store idx %i to load to "
73113590Srekai.gonzalezalberquilla@arm.com                        "addr %#x\n", store_it._idx,
73213590Srekai.gonzalezalberquilla@arm.com                        req->mainRequest()->getVaddr());
7332292SN/A
73413590Srekai.gonzalezalberquilla@arm.com                PacketPtr data_pkt = new Packet(req->mainRequest(),
73513590Srekai.gonzalezalberquilla@arm.com                        MemCmd::ReadReq);
73613590Srekai.gonzalezalberquilla@arm.com                data_pkt->dataStatic(load_inst->memData);
7372292SN/A
73813590Srekai.gonzalezalberquilla@arm.com                if (req->isAnyOutstandingRequest()) {
73913590Srekai.gonzalezalberquilla@arm.com                    assert(req->_numOutstandingPackets > 0);
74013590Srekai.gonzalezalberquilla@arm.com                    // There are memory requests packets in flight already.
74113590Srekai.gonzalezalberquilla@arm.com                    // This may happen if the store was not complete the
74213590Srekai.gonzalezalberquilla@arm.com                    // first time this load got executed. Signal the senderSate
74313590Srekai.gonzalezalberquilla@arm.com                    // that response packets should be discarded.
74413590Srekai.gonzalezalberquilla@arm.com                    req->discardSenderState();
74513590Srekai.gonzalezalberquilla@arm.com                }
7462678Sktlim@umich.edu
74713590Srekai.gonzalezalberquilla@arm.com                WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt,
74813590Srekai.gonzalezalberquilla@arm.com                        this);
7492678Sktlim@umich.edu
75013590Srekai.gonzalezalberquilla@arm.com                // We'll say this has a 1 cycle load-store forwarding latency
75113590Srekai.gonzalezalberquilla@arm.com                // for now.
75213590Srekai.gonzalezalberquilla@arm.com                // @todo: Need to make this a parameter.
75313590Srekai.gonzalezalberquilla@arm.com                cpu->schedule(wb, curTick());
7542292SN/A
75513590Srekai.gonzalezalberquilla@arm.com                // Don't need to do anything special for split loads.
75613590Srekai.gonzalezalberquilla@arm.com                ++lsqForwLoads;
7572678Sktlim@umich.edu
75813590Srekai.gonzalezalberquilla@arm.com                return NoFault;
75913590Srekai.gonzalezalberquilla@arm.com            } else if (
76013652Sqtt2@cornell.edu                // This is the partial store-load forwarding case where a store
76113652Sqtt2@cornell.edu                // has only part of the load's data and the load isn't LLSC
76213590Srekai.gonzalezalberquilla@arm.com                (!req->mainRequest()->isLLSC() &&
76312022Sar4jc@virginia.edu                 ((store_has_lower_limit && lower_load_has_store_part) ||
76412022Sar4jc@virginia.edu                  (store_has_upper_limit && upper_load_has_store_part) ||
76512022Sar4jc@virginia.edu                  (lower_load_has_store_part && upper_load_has_store_part))) ||
76613652Sqtt2@cornell.edu                // The load is LLSC, and the store has all or part of the
76713652Sqtt2@cornell.edu                // load's data
76813590Srekai.gonzalezalberquilla@arm.com                (req->mainRequest()->isLLSC() &&
76912022Sar4jc@virginia.edu                 ((store_has_lower_limit || upper_load_has_store_part) &&
77013652Sqtt2@cornell.edu                  (store_has_upper_limit || lower_load_has_store_part))) ||
77113652Sqtt2@cornell.edu                // The store entry is atomic and has all or part of the load's
77213652Sqtt2@cornell.edu                // data
77313652Sqtt2@cornell.edu                (store_it->instruction()->isAtomic() &&
77413652Sqtt2@cornell.edu                 ((store_has_lower_limit || upper_load_has_store_part) &&
77512022Sar4jc@virginia.edu                  (store_has_upper_limit || lower_load_has_store_part)))) {
7762292SN/A
77713590Srekai.gonzalezalberquilla@arm.com                // If it's already been written back, then don't worry about
77813590Srekai.gonzalezalberquilla@arm.com                // stalling on it.
77913590Srekai.gonzalezalberquilla@arm.com                if (store_it->completed()) {
78013590Srekai.gonzalezalberquilla@arm.com                    panic("Should not check one of these");
78113590Srekai.gonzalezalberquilla@arm.com                    continue;
78213590Srekai.gonzalezalberquilla@arm.com                }
78313590Srekai.gonzalezalberquilla@arm.com
78413590Srekai.gonzalezalberquilla@arm.com                // Must stall load and force it to retry, so long as it's the
78513590Srekai.gonzalezalberquilla@arm.com                // oldest load that needs to do so.
78613590Srekai.gonzalezalberquilla@arm.com                if (!stalled ||
78713590Srekai.gonzalezalberquilla@arm.com                    (stalled &&
78813590Srekai.gonzalezalberquilla@arm.com                     load_inst->seqNum <
78913590Srekai.gonzalezalberquilla@arm.com                     loadQueue[stallingLoadIdx].instruction()->seqNum)) {
79013590Srekai.gonzalezalberquilla@arm.com                    stalled = true;
79113590Srekai.gonzalezalberquilla@arm.com                    stallingStoreIsn = store_it->instruction()->seqNum;
79213590Srekai.gonzalezalberquilla@arm.com                    stallingLoadIdx = load_idx;
79313590Srekai.gonzalezalberquilla@arm.com                }
79413590Srekai.gonzalezalberquilla@arm.com
79513590Srekai.gonzalezalberquilla@arm.com                // Tell IQ/mem dep unit that this instruction will need to be
79613590Srekai.gonzalezalberquilla@arm.com                // rescheduled eventually
79713590Srekai.gonzalezalberquilla@arm.com                iewStage->rescheduleMemInst(load_inst);
79813590Srekai.gonzalezalberquilla@arm.com                load_inst->clearIssued();
79913590Srekai.gonzalezalberquilla@arm.com                load_inst->effAddrValid(false);
80013590Srekai.gonzalezalberquilla@arm.com                ++lsqRescheduledLoads;
80113590Srekai.gonzalezalberquilla@arm.com
80213590Srekai.gonzalezalberquilla@arm.com                // Do not generate a writeback event as this instruction is not
80313590Srekai.gonzalezalberquilla@arm.com                // complete.
80413590Srekai.gonzalezalberquilla@arm.com                DPRINTF(LSQUnit, "Load-store forwarding mis-match. "
80513590Srekai.gonzalezalberquilla@arm.com                        "Store idx %i to load addr %#x\n",
80613590Srekai.gonzalezalberquilla@arm.com                        store_it._idx, req->mainRequest()->getVaddr());
80713590Srekai.gonzalezalberquilla@arm.com
80813590Srekai.gonzalezalberquilla@arm.com                // Must discard the request.
80913590Srekai.gonzalezalberquilla@arm.com                req->discard();
81013590Srekai.gonzalezalberquilla@arm.com                load_req.setRequest(nullptr);
81113590Srekai.gonzalezalberquilla@arm.com                return NoFault;
8122292SN/A            }
8132292SN/A        }
8142292SN/A    }
8152292SN/A
8162292SN/A    // If there's no forwarding case, then go access memory
8177720Sgblack@eecs.umich.edu    DPRINTF(LSQUnit, "Doing memory access for inst [sn:%lli] PC %s\n",
8187720Sgblack@eecs.umich.edu            load_inst->seqNum, load_inst->pcState());
8192292SN/A
82010333Smitch.hayenga@arm.com    // Allocate memory if this is the first time a load is issued.
82110333Smitch.hayenga@arm.com    if (!load_inst->memData) {
82213590Srekai.gonzalezalberquilla@arm.com        load_inst->memData = new uint8_t[req->mainRequest()->getSize()];
82310333Smitch.hayenga@arm.com    }
8246974Stjones1@inf.ed.ac.uk
82511780Sarthur.perais@inria.fr    // For now, load throughput is constrained by the number of
82611780Sarthur.perais@inria.fr    // load FUs only, and loads do not consume a cache port (only
82711780Sarthur.perais@inria.fr    // stores do).
82811780Sarthur.perais@inria.fr    // @todo We should account for cache port contention
82911780Sarthur.perais@inria.fr    // and arbitrate between loads and stores.
8306974Stjones1@inf.ed.ac.uk
83113590Srekai.gonzalezalberquilla@arm.com    // if we the cache is not blocked, do cache access
83213590Srekai.gonzalezalberquilla@arm.com    if (req->senderState() == nullptr) {
83313590Srekai.gonzalezalberquilla@arm.com        LQSenderState *state = new LQSenderState(
83413590Srekai.gonzalezalberquilla@arm.com                loadQueue.getIterator(load_idx));
83513590Srekai.gonzalezalberquilla@arm.com        state->isLoad = true;
83613590Srekai.gonzalezalberquilla@arm.com        state->inst = load_inst;
83713590Srekai.gonzalezalberquilla@arm.com        state->isSplit = req->isSplit();
83813590Srekai.gonzalezalberquilla@arm.com        req->senderState(state);
8392907Sktlim@umich.edu    }
84013590Srekai.gonzalezalberquilla@arm.com    req->buildPackets();
84113590Srekai.gonzalezalberquilla@arm.com    req->sendPacketToCache();
84213590Srekai.gonzalezalberquilla@arm.com    if (!req->isSent())
84310333Smitch.hayenga@arm.com        iewStage->blockMemInst(load_inst);
8442292SN/A
8452669Sktlim@umich.edu    return NoFault;
8462292SN/A}
8472292SN/A
8482292SN/Atemplate <class Impl>
8492292SN/AFault
85013590Srekai.gonzalezalberquilla@arm.comLSQUnit<Impl>::write(LSQRequest *req, uint8_t *data, int store_idx)
8512292SN/A{
85213590Srekai.gonzalezalberquilla@arm.com    assert(storeQueue[store_idx].valid());
8532292SN/A
85413590Srekai.gonzalezalberquilla@arm.com    DPRINTF(LSQUnit, "Doing write to store idx %i, addr %#x | storeHead:%i "
85513831SAndrea.Mondelli@ucf.edu            "[sn:%llu]\n",
85613590Srekai.gonzalezalberquilla@arm.com            store_idx - 1, req->request()->getPaddr(), storeQueue.head() - 1,
85713590Srekai.gonzalezalberquilla@arm.com            storeQueue[store_idx].instruction()->seqNum);
8582329SN/A
85913590Srekai.gonzalezalberquilla@arm.com    storeQueue[store_idx].setRequest(req);
86013590Srekai.gonzalezalberquilla@arm.com    unsigned size = req->_size;
86113590Srekai.gonzalezalberquilla@arm.com    storeQueue[store_idx].size() = size;
86213590Srekai.gonzalezalberquilla@arm.com    bool store_no_data =
86313590Srekai.gonzalezalberquilla@arm.com        req->mainRequest()->getFlags() & Request::STORE_NO_DATA;
86413590Srekai.gonzalezalberquilla@arm.com    storeQueue[store_idx].isAllZeros() = store_no_data;
86513590Srekai.gonzalezalberquilla@arm.com    assert(size <= SQEntry::DataSize || store_no_data);
8667509Stjones1@inf.ed.ac.uk
86713652Sqtt2@cornell.edu    // copy data into the storeQueue only if the store request has valid data
86813590Srekai.gonzalezalberquilla@arm.com    if (!(req->request()->getFlags() & Request::CACHE_BLOCK_ZERO) &&
86913652Sqtt2@cornell.edu        !req->request()->isCacheMaintenance() &&
87013652Sqtt2@cornell.edu        !req->request()->isAtomic())
87113590Srekai.gonzalezalberquilla@arm.com        memcpy(storeQueue[store_idx].data(), data, size);
8722329SN/A
8732292SN/A    // This function only writes the data to the store queue, so no fault
8742292SN/A    // can happen here.
8752292SN/A    return NoFault;
8762292SN/A}
8772292SN/A
8782292SN/A#endif // __CPU_O3_LSQ_UNIT_HH__
879