lsq_unit.hh revision 9440
12292SN/A/*
22329SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
32292SN/A * All rights reserved.
42292SN/A *
52292SN/A * Redistribution and use in source and binary forms, with or without
62292SN/A * modification, are permitted provided that the following conditions are
72292SN/A * met: redistributions of source code must retain the above copyright
82292SN/A * notice, this list of conditions and the following disclaimer;
92292SN/A * redistributions in binary form must reproduce the above copyright
102292SN/A * notice, this list of conditions and the following disclaimer in the
112292SN/A * documentation and/or other materials provided with the distribution;
122292SN/A * neither the name of the copyright holders nor the names of its
132292SN/A * contributors may be used to endorse or promote products derived from
142292SN/A * this software without specific prior written permission.
152292SN/A *
162292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182292SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192292SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202292SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212292SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222292SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232292SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242292SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252292SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262292SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272689Sktlim@umich.edu *
282689Sktlim@umich.edu * Authors: Kevin Lim
292689Sktlim@umich.edu *          Korey Sewell
302292SN/A */
312292SN/A
322292SN/A#ifndef __CPU_O3_LSQ_UNIT_HH__
332292SN/A#define __CPU_O3_LSQ_UNIT_HH__
342292SN/A
352329SN/A#include <algorithm>
362292SN/A#include <cstring>
372292SN/A#include <map>
382292SN/A#include <queue>
392329SN/A
403326Sktlim@umich.edu#include "arch/generic/debugfaults.hh"
412292SN/A#include "arch/isa_traits.hh"
422292SN/A#include "arch/locked_mem.hh"
432292SN/A#include "arch/mmapped_ipr.hh"
443348Sbinkertn@umich.edu#include "base/hashmap.hh"
452669Sktlim@umich.edu#include "config/the_isa.hh"
462292SN/A#include "cpu/inst_seq.hh"
472292SN/A#include "cpu/timebuf.hh"
482329SN/A#include "debug/LSQUnit.hh"
492329SN/A#include "mem/packet.hh"
502329SN/A#include "mem/port.hh"
512329SN/A#include "sim/fault_fwd.hh"
522329SN/A
532329SN/Astruct DerivO3CPUParams;
542329SN/A
552329SN/A/**
562329SN/A * Class that implements the actual LQ and SQ for each specific
572329SN/A * thread.  Both are circular queues; load entries are freed upon
582292SN/A * committing, while store entries are freed once they writeback. The
592292SN/A * LSQUnit tracks if there are memory ordering violations, and also
602292SN/A * detects partial load to store forwarding cases (a store only has
612292SN/A * part of a load's data) that requires the load to wait until the
622292SN/A * store writes back. In the former case it holds onto the instruction
632292SN/A * until the dependence unit looks at it, and in the latter it stalls
642292SN/A * the LSQ until the store writes back. At that point the load is
652733Sktlim@umich.edu * replayed.
662292SN/A */
672292SN/Atemplate <class Impl>
682907Sktlim@umich.educlass LSQUnit {
692292SN/A  public:
702292SN/A    typedef typename Impl::O3CPU O3CPU;
712292SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
722292SN/A    typedef typename Impl::CPUPol::IEW IEW;
732292SN/A    typedef typename Impl::CPUPol::LSQ LSQ;
742292SN/A    typedef typename Impl::CPUPol::IssueStruct IssueStruct;
752292SN/A
764329Sktlim@umich.edu  public:
774329Sktlim@umich.edu    /** Constructs an LSQ unit. init() must be called prior to use. */
782292SN/A    LSQUnit();
792292SN/A
802292SN/A    /** Initializes the LSQ unit with the specified number of entries. */
812292SN/A    void init(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params,
822727Sktlim@umich.edu            LSQ *lsq_ptr, unsigned maxLQEntries, unsigned maxSQEntries,
832727Sktlim@umich.edu            unsigned id);
842727Sktlim@umich.edu
852907Sktlim@umich.edu    /** Returns the name of the LSQ unit. */
864329Sktlim@umich.edu    std::string name() const;
872907Sktlim@umich.edu
882348SN/A    /** Registers statistics. */
892307SN/A    void regStats();
902307SN/A
912348SN/A    /** Sets the pointer to the dcache port. */
922307SN/A    void setDcachePort(MasterPort *dcache_port);
932307SN/A
942348SN/A    /** Switches out LSQ unit. */
952307SN/A    void switchOut();
962307SN/A
972292SN/A    /** Takes over from another CPU's thread. */
982292SN/A    void takeOverFrom();
992292SN/A
1002292SN/A    /** Returns if the LSQ is switched out. */
1012292SN/A    bool isSwitchedOut() { return switchedOut; }
1022292SN/A
1032292SN/A    /** Ticks the LSQ unit, which in this case only resets the number of
1042292SN/A     * used cache ports.
1052292SN/A     * @todo: Move the number of used ports up to the LSQ level so it can
1062292SN/A     * be shared by all LSQ units.
1072292SN/A     */
1082292SN/A    void tick() { usedPorts = 0; }
1092292SN/A
1102292SN/A    /** Inserts an instruction. */
1112292SN/A    void insert(DynInstPtr &inst);
1122292SN/A    /** Inserts a load instruction. */
1132292SN/A    void insertLoad(DynInstPtr &load_inst);
1142329SN/A    /** Inserts a store instruction. */
1152292SN/A    void insertStore(DynInstPtr &store_inst);
1162292SN/A
1172292SN/A    /** Check for ordering violations in the LSQ. For a store squash if we
1182292SN/A     * ever find a conflicting load. For a load, only squash if we
1192292SN/A     * an external snoop invalidate has been seen for that load address
1202292SN/A     * @param load_idx index to start checking at
1212292SN/A     * @param inst the instruction to check
1222292SN/A     */
1232292SN/A    Fault checkViolations(int load_idx, DynInstPtr &inst);
1242292SN/A
1252292SN/A    /** Check if an incoming invalidate hits in the lsq on a load
1262292SN/A     * that might have issued out of order wrt another load beacuse
1272292SN/A     * of the intermediate invalidate.
1282292SN/A     */
1292790Sktlim@umich.edu    void checkSnoop(PacketPtr pkt);
1302790Sktlim@umich.edu
1312669Sktlim@umich.edu    /** Executes a load instruction. */
1322669Sktlim@umich.edu    Fault executeLoad(DynInstPtr &inst);
1332292SN/A
1342292SN/A    Fault executeLoad(int lq_idx) { panic("Not implemented"); return NoFault; }
1352292SN/A    /** Executes a store instruction. */
1362292SN/A    Fault executeStore(DynInstPtr &inst);
1372292SN/A
1382292SN/A    /** Commits the head load. */
1392292SN/A    void commitLoad();
1402292SN/A    /** Commits loads older than a specific sequence number. */
1412292SN/A    void commitLoads(InstSeqNum &youngest_inst);
1422292SN/A
1432292SN/A    /** Commits stores older than a specific sequence number. */
1442292SN/A    void commitStores(InstSeqNum &youngest_inst);
1452292SN/A
1462292SN/A    /** Writes back stores. */
1472292SN/A    void writebackStores();
1482292SN/A
1492292SN/A    /** Completes the data access that has been returned from the
1502292SN/A     * memory system. */
1512292SN/A    void completeDataAccess(PacketPtr pkt);
1522292SN/A
1532292SN/A    /** Clears all the entries in the LQ. */
1542292SN/A    void clearLQ();
1552292SN/A
1562329SN/A    /** Clears all the entries in the SQ. */
1572292SN/A    void clearSQ();
1582292SN/A
1592292SN/A    /** Resizes the LQ to a given size. */
1602348SN/A    void resizeLQ(unsigned size);
1612292SN/A
1622292SN/A    /** Resizes the SQ to a given size. */
1632292SN/A    void resizeSQ(unsigned size);
1642348SN/A
1652292SN/A    /** Squashes all instructions younger than a specific sequence number. */
1662292SN/A    void squash(const InstSeqNum &squashed_num);
1672292SN/A
1682348SN/A    /** Returns if there is a memory ordering violation. Value is reset upon
1692292SN/A     * call to getMemDepViolator().
1702292SN/A     */
1712292SN/A    bool violation() { return memDepViolator; }
1722292SN/A
1732292SN/A    /** Returns the memory ordering violator. */
1742292SN/A    DynInstPtr getMemDepViolator();
1752292SN/A
1762292SN/A    /** Returns if a load became blocked due to the memory system. */
1772292SN/A    bool loadBlocked()
1782292SN/A    { return isLoadBlocked; }
1792292SN/A
1802292SN/A    /** Clears the signal that a load became blocked. */
1812292SN/A    void clearLoadBlocked()
1822292SN/A    { isLoadBlocked = false; }
1832292SN/A
1842292SN/A    /** Returns if the blocked load was handled. */
1852292SN/A    bool isLoadBlockedHandled()
1862292SN/A    { return loadBlockedHandled; }
1872292SN/A
1882292SN/A    /** Records the blocked load as being handled. */
1892292SN/A    void setLoadBlockedHandled()
1902292SN/A    { loadBlockedHandled = true; }
1912292SN/A
1922292SN/A    /** Returns the number of free entries (min of free LQ and SQ entries). */
1932292SN/A    unsigned numFreeEntries();
1942292SN/A
1952292SN/A    /** Returns the number of loads in the LQ. */
1962292SN/A    int numLoads() { return loads; }
1972292SN/A
1982292SN/A    /** Returns the number of stores in the SQ. */
1992292SN/A    int numStores() { return stores; }
2002292SN/A
2012292SN/A    /** Returns if either the LQ or SQ is full. */
2022292SN/A    bool isFull() { return lqFull() || sqFull(); }
2032292SN/A
2042678Sktlim@umich.edu    /** Returns if the LQ is full. */
2052678Sktlim@umich.edu    bool lqFull() { return loads >= (LQEntries - 1); }
2062292SN/A
2072907Sktlim@umich.edu    /** Returns if the SQ is full. */
2082907Sktlim@umich.edu    bool sqFull() { return stores >= (SQEntries - 1); }
2092907Sktlim@umich.edu
2102292SN/A    /** Returns the number of instructions in the LSQ. */
2112698Sktlim@umich.edu    unsigned getCount() { return loads + stores; }
2122678Sktlim@umich.edu
2132678Sktlim@umich.edu    /** Returns if there are any stores to writeback. */
2142698Sktlim@umich.edu    bool hasStoresToWB() { return storesToWB; }
2153349Sbinkertn@umich.edu
2162693Sktlim@umich.edu    /** Returns the number of stores to writeback. */
2172292SN/A    int numStoresToWB() { return storesToWB; }
2182292SN/A
2192292SN/A    /** Returns if the LSQ unit will writeback on this cycle. */
2202292SN/A    bool willWB() { return storeQueue[storeWBIdx].canWB &&
2212292SN/A                        !storeQueue[storeWBIdx].completed &&
2222292SN/A                        !isStoreBlocked; }
2232292SN/A
2242292SN/A    /** Handles doing the retry. */
2252292SN/A    void recvRetry();
2262292SN/A
2272292SN/A  private:
2282292SN/A    /** Writes back the instruction, sending it to IEW. */
2292329SN/A    void writeback(DynInstPtr &inst, PacketPtr pkt);
2302329SN/A
2312329SN/A    /** Writes back a store that couldn't be completed the previous cycle. */
2322329SN/A    void writebackPendingStore();
2332292SN/A
2342292SN/A    /** Handles completing the send of a store to memory. */
2352733Sktlim@umich.edu    void storePostSend(PacketPtr pkt);
2362292SN/A
2372292SN/A    /** Completes the store at the specified index. */
2382292SN/A    void completeStore(int store_idx);
2392292SN/A
2402907Sktlim@umich.edu    /** Attempts to send a store to the cache. */
2412907Sktlim@umich.edu    bool sendStore(PacketPtr data_pkt);
2422669Sktlim@umich.edu
2432907Sktlim@umich.edu    /** Increments the given store index (circular queue). */
2442907Sktlim@umich.edu    inline void incrStIdx(int &store_idx) const;
2452292SN/A    /** Decrements the given store index (circular queue). */
2462698Sktlim@umich.edu    inline void decrStIdx(int &store_idx) const;
2472678Sktlim@umich.edu    /** Increments the given load index (circular queue). */
2482678Sktlim@umich.edu    inline void incrLdIdx(int &load_idx) const;
2492678Sktlim@umich.edu    /** Decrements the given load index (circular queue). */
2502698Sktlim@umich.edu    inline void decrLdIdx(int &load_idx) const;
2512678Sktlim@umich.edu
2522678Sktlim@umich.edu  public:
2532678Sktlim@umich.edu    /** Debugging function to dump instructions in the LSQ. */
2542678Sktlim@umich.edu    void dumpInsts() const;
2552698Sktlim@umich.edu
2562678Sktlim@umich.edu  private:
2572698Sktlim@umich.edu    /** Pointer to the CPU. */
2582678Sktlim@umich.edu    O3CPU *cpu;
2592698Sktlim@umich.edu
2602678Sktlim@umich.edu    /** Pointer to the IEW stage. */
2612698Sktlim@umich.edu    IEW *iewStage;
2622678Sktlim@umich.edu
2632678Sktlim@umich.edu    /** Pointer to the LSQ. */
2642678Sktlim@umich.edu    LSQ *lsq;
2652698Sktlim@umich.edu
2662678Sktlim@umich.edu    /** Pointer to the dcache port.  Used only for sending. */
2672678Sktlim@umich.edu    MasterPort *dcachePort;
2682678Sktlim@umich.edu
2692678Sktlim@umich.edu    /** Derived class to hold any sender state the LSQ needs. */
2702678Sktlim@umich.edu    class LSQSenderState : public Packet::SenderState
2712678Sktlim@umich.edu    {
2722678Sktlim@umich.edu      public:
2732678Sktlim@umich.edu        /** Default constructor. */
2742678Sktlim@umich.edu        LSQSenderState()
2752678Sktlim@umich.edu            : mainPkt(NULL), pendingPacket(NULL), outstanding(1),
2762678Sktlim@umich.edu              noWB(false), isSplit(false), pktToSend(false)
2772678Sktlim@umich.edu          { }
2782698Sktlim@umich.edu
2792678Sktlim@umich.edu        /** Instruction who initiated the access to memory. */
2802678Sktlim@umich.edu        DynInstPtr inst;
2812698Sktlim@umich.edu        /** The main packet from a split load, used during writeback. */
2822678Sktlim@umich.edu        PacketPtr mainPkt;
2832678Sktlim@umich.edu        /** A second packet from a split store that needs sending. */
2842678Sktlim@umich.edu        PacketPtr pendingPacket;
2852678Sktlim@umich.edu        /** The LQ/SQ index of the instruction. */
2862678Sktlim@umich.edu        uint8_t idx;
2872678Sktlim@umich.edu        /** Number of outstanding packets to complete. */
2882292SN/A        uint8_t outstanding;
2892292SN/A        /** Whether or not it is a load. */
2902292SN/A        bool isLoad;
2912292SN/A        /** Whether or not the instruction will need to writeback. */
2922292SN/A        bool noWB;
2932292SN/A        /** Whether or not this access is split in two. */
2942292SN/A        bool isSplit;
2952292SN/A        /** Whether or not there is a packet that needs sending. */
2962292SN/A        bool pktToSend;
2972292SN/A
2982292SN/A        /** Completes a packet and returns whether the access is finished. */
2992292SN/A        inline bool complete() { return --outstanding == 0; }
3002292SN/A    };
3012292SN/A
3022292SN/A    /** Writeback event, specifically for when stores forward data to loads. */
3032292SN/A    class WritebackEvent : public Event {
3042669Sktlim@umich.edu      public:
3052669Sktlim@umich.edu        /** Constructs a writeback event. */
3062292SN/A        WritebackEvent(DynInstPtr &_inst, PacketPtr pkt, LSQUnit *lsq_ptr);
3072292SN/A
3082292SN/A        /** Processes the writeback event. */
3092292SN/A        void process();
3102292SN/A
3112292SN/A        /** Returns the description of this event. */
3122292SN/A        const char *description() const;
3132292SN/A
3142292SN/A      private:
3152292SN/A        /** Instruction whose results are being written back. */
3162292SN/A        DynInstPtr inst;
3172329SN/A
3182292SN/A        /** The packet that would have been sent to memory. */
3192292SN/A        PacketPtr pkt;
3202292SN/A
3212292SN/A        /** The pointer to the LSQ unit that issued the store. */
3222292SN/A        LSQUnit<Impl> *lsqPtr;
3232292SN/A    };
3242292SN/A
3252292SN/A  public:
3262292SN/A    struct SQEntry {
3272292SN/A        /** Constructs an empty store queue entry. */
3282329SN/A        SQEntry()
3292329SN/A            : inst(NULL), req(NULL), size(0),
3302329SN/A              canWB(0), committed(0), completed(0)
3312292SN/A        {
3322329SN/A            std::memset(data, 0, sizeof(data));
3332329SN/A        }
3342329SN/A
3352292SN/A        ~SQEntry()
3362292SN/A        {
3372292SN/A            inst = NULL;
3382292SN/A        }
3392329SN/A
3402292SN/A        /** Constructs a store queue entry for a given instruction. */
3412292SN/A        SQEntry(DynInstPtr &_inst)
3422292SN/A            : inst(_inst), req(NULL), sreqLow(NULL), sreqHigh(NULL), size(0),
3432292SN/A              isSplit(0), canWB(0), committed(0), completed(0)
3442292SN/A        {
3452292SN/A            std::memset(data, 0, sizeof(data));
3462292SN/A        }
3472292SN/A        /** The store data. */
3482292SN/A        char data[16];
3492292SN/A        /** The store instruction. */
3502292SN/A        DynInstPtr inst;
3512329SN/A        /** The request for the store. */
3522329SN/A        RequestPtr req;
3532292SN/A        /** The split requests for the store. */
3542292SN/A        RequestPtr sreqLow;
3552292SN/A        RequestPtr sreqHigh;
3562292SN/A        /** The size of the store. */
3572292SN/A        uint8_t size;
3582292SN/A        /** Whether or not the store is split into two requests. */
3592292SN/A        bool isSplit;
3602292SN/A        /** Whether or not the store can writeback. */
3612292SN/A        bool canWB;
3622292SN/A        /** Whether or not the store is committed. */
3632292SN/A        bool committed;
3642292SN/A        /** Whether or not the store is completed. */
3652348SN/A        bool completed;
3662307SN/A    };
3672307SN/A
3682292SN/A  private:
3692292SN/A    /** The LSQUnit thread id. */
3702292SN/A    ThreadID lsqID;
3712292SN/A
3722292SN/A    /** The store queue. */
3732292SN/A    std::vector<SQEntry> storeQueue;
3742292SN/A
3752292SN/A    /** The load queue. */
3762292SN/A    std::vector<DynInstPtr> loadQueue;
3772292SN/A
3782292SN/A    /** The number of LQ entries, plus a sentinel entry (circular queue).
3792292SN/A     *  @todo: Consider having var that records the true number of LQ entries.
3802292SN/A     */
3812292SN/A    unsigned LQEntries;
3822698Sktlim@umich.edu    /** The number of SQ entries, plus a sentinel entry (circular queue).
3832698Sktlim@umich.edu     *  @todo: Consider having var that records the true number of SQ entries.
3842693Sktlim@umich.edu     */
3852698Sktlim@umich.edu    unsigned SQEntries;
3862678Sktlim@umich.edu
3872678Sktlim@umich.edu    /** The number of places to shift addresses in the LSQ before checking
3882329SN/A     * for dependency violations
3892292SN/A     */
3902292SN/A    unsigned depCheckShift;
3912348SN/A
3922292SN/A    /** Should loads be checked for dependency issues */
3932292SN/A    bool checkLoads;
3942348SN/A
3952292SN/A    /** The number of load instructions in the LQ. */
3962292SN/A    int loads;
3972292SN/A    /** The number of store instructions in the SQ. */
3982292SN/A    int stores;
3992292SN/A    /** The number of store instructions in the SQ waiting to writeback. */
4002292SN/A    int storesToWB;
4012292SN/A
4022292SN/A    /** The index of the head instruction in the LQ. */
4032727Sktlim@umich.edu    int loadHead;
4042727Sktlim@umich.edu    /** The index of the tail instruction in the LQ. */
4052307SN/A    int loadTail;
4063126Sktlim@umich.edu
4073126Sktlim@umich.edu    /** The index of the head instruction in the SQ. */
4083126Sktlim@umich.edu    int storeHead;
4093126Sktlim@umich.edu    /** The index of the first instruction that may be ready to be
4103126Sktlim@umich.edu     * written back, and has not yet been written back.
4113126Sktlim@umich.edu     */
4123126Sktlim@umich.edu    int storeWBIdx;
4133126Sktlim@umich.edu    /** The index of the tail instruction in the SQ. */
4143126Sktlim@umich.edu    int storeTail;
4153126Sktlim@umich.edu
4163126Sktlim@umich.edu    /// @todo Consider moving to a more advanced model with write vs read ports
4173126Sktlim@umich.edu    /** The number of cache ports available each cycle. */
4183126Sktlim@umich.edu    int cachePorts;
4192727Sktlim@umich.edu
4202727Sktlim@umich.edu    /** The number of used cache ports in this cycle. */
4212727Sktlim@umich.edu    int usedPorts;
4222727Sktlim@umich.edu
4232727Sktlim@umich.edu    /** Is the LSQ switched out. */
4242727Sktlim@umich.edu    bool switchedOut;
4252727Sktlim@umich.edu
4262727Sktlim@umich.edu    //list<InstSeqNum> mshrSeqNums;
4272727Sktlim@umich.edu
4282727Sktlim@umich.edu    /** Address Mask for a cache block (e.g. ~(cache_block_size-1)) */
4292727Sktlim@umich.edu    Addr cacheBlockMask;
4302727Sktlim@umich.edu
4312727Sktlim@umich.edu    /** Wire to read information from the issue stage time queue. */
4322727Sktlim@umich.edu    typename TimeBuffer<IssueStruct>::wire fromIssue;
4332727Sktlim@umich.edu
4342292SN/A    /** Whether or not the LSQ is stalled. */
4352292SN/A    bool stalled;
4362292SN/A    /** The store that causes the stall due to partial store to load
4372669Sktlim@umich.edu     * forwarding.
4382292SN/A     */
4392292SN/A    InstSeqNum stallingStoreIsn;
4402292SN/A    /** The index of the above store. */
4412669Sktlim@umich.edu    int stallingLoadIdx;
4422292SN/A
4432292SN/A    /** The packet that needs to be retried. */
4442292SN/A    PacketPtr retryPkt;
4452292SN/A
4462292SN/A    /** Whehter or not a store is blocked due to the memory system. */
4472292SN/A    bool isStoreBlocked;
4482292SN/A
4492292SN/A    /** Whether or not a load is blocked due to the memory system. */
4502292SN/A    bool isLoadBlocked;
4512292SN/A
4522292SN/A    /** Has the blocked load been handled. */
4532292SN/A    bool loadBlockedHandled;
4542292SN/A
4552292SN/A    /** Whether or not a store is in flight. */
4562292SN/A    bool storeInFlight;
4572292SN/A
4582292SN/A    /** The sequence number of the blocked load. */
4592292SN/A    InstSeqNum blockedLoadSeqNum;
4602292SN/A
4612292SN/A    /** The oldest load that caused a memory ordering violation. */
4622292SN/A    DynInstPtr memDepViolator;
4632292SN/A
4642292SN/A    /** Whether or not there is a packet that couldn't be sent because of
4652292SN/A     * a lack of cache ports. */
4662292SN/A    bool hasPendingPkt;
4672292SN/A
4682292SN/A    /** The packet that is pending free cache ports. */
4692292SN/A    PacketPtr pendingPkt;
4702292SN/A
4712292SN/A    /** Flag for memory model. */
4722292SN/A    bool needsTSO;
4732292SN/A
4742292SN/A    // Will also need how many read/write ports the Dcache has.  Or keep track
4752292SN/A    // of that in stage that is one level up, and only call executeLoad/Store
4762669Sktlim@umich.edu    // the appropriate number of times.
4772292SN/A    /** Total number of loads forwaded from LSQ stores. */
4782669Sktlim@umich.edu    Stats::Scalar lsqForwLoads;
4792292SN/A
4802669Sktlim@umich.edu    /** Total number of loads ignored due to invalid addresses. */
4812669Sktlim@umich.edu    Stats::Scalar invAddrLoads;
4822669Sktlim@umich.edu
4832292SN/A    /** Total number of squashed loads. */
4842292SN/A    Stats::Scalar lsqSquashedLoads;
4852292SN/A
4862292SN/A    /** Total number of responses from the memory system that are
4872292SN/A     * ignored due to the instruction already being squashed. */
4883172Sstever@eecs.umich.edu    Stats::Scalar lsqIgnoredResponses;
4892731Sktlim@umich.edu
4902669Sktlim@umich.edu    /** Tota number of memory ordering violations. */
4912727Sktlim@umich.edu    Stats::Scalar lsqMemOrderViolation;
4924032Sktlim@umich.edu
4934032Sktlim@umich.edu    /** Total number of squashed stores. */
4944032Sktlim@umich.edu    Stats::Scalar lsqSquashedStores;
4954032Sktlim@umich.edu
4964032Sktlim@umich.edu    /** Total number of software prefetches ignored due to invalid addresses. */
4972292SN/A    Stats::Scalar invAddrSwpfs;
4982292SN/A
4992292SN/A    /** Ready loads blocked due to partial store-forwarding. */
5002292SN/A    Stats::Scalar lsqBlockedLoads;
5012669Sktlim@umich.edu
5022292SN/A    /** Number of loads that were rescheduled. */
5032292SN/A    Stats::Scalar lsqRescheduledLoads;
5042292SN/A
5052292SN/A    /** Number of times the LSQ is blocked due to the cache. */
5062292SN/A    Stats::Scalar lsqCacheBlocked;
5072669Sktlim@umich.edu
5082292SN/A  public:
5093172Sstever@eecs.umich.edu    /** Executes the load at the given index. */
5103326Sktlim@umich.edu    Fault read(Request *req, Request *sreqLow, Request *sreqHigh,
5113326Sktlim@umich.edu               uint8_t *data, int load_idx);
5123326Sktlim@umich.edu
5133326Sktlim@umich.edu    /** Executes the store at the given index. */
5143326Sktlim@umich.edu    Fault write(Request *req, Request *sreqLow, Request *sreqHigh,
5153326Sktlim@umich.edu                uint8_t *data, int store_idx);
5162292SN/A
5172292SN/A    /** Returns the index of the head load instruction. */
5182292SN/A    int getLoadHead() { return loadHead; }
5192292SN/A    /** Returns the sequence number of the head load instruction. */
5202292SN/A    InstSeqNum getLoadHeadSeqNum()
5212292SN/A    {
5222292SN/A        if (loadQueue[loadHead]) {
5232292SN/A            return loadQueue[loadHead]->seqNum;
5242292SN/A        } else {
5252292SN/A            return 0;
5262292SN/A        }
5272292SN/A
5282292SN/A    }
5292292SN/A
5302292SN/A    /** Returns the index of the head store instruction. */
5312292SN/A    int getStoreHead() { return storeHead; }
5322292SN/A    /** Returns the sequence number of the head store instruction. */
5332292SN/A    InstSeqNum getStoreHeadSeqNum()
5344032Sktlim@umich.edu    {
5354032Sktlim@umich.edu        if (storeQueue[storeHead].inst) {
5364032Sktlim@umich.edu            return storeQueue[storeHead].inst->seqNum;
5374032Sktlim@umich.edu        } else {
5382292SN/A            return 0;
5392292SN/A        }
5402292SN/A
5412292SN/A    }
5422669Sktlim@umich.edu
5432292SN/A    /** Returns whether or not the LSQ unit is stalled. */
5442669Sktlim@umich.edu    bool isStalled()  { return stalled; }
5452669Sktlim@umich.edu};
5462292SN/A
5472669Sktlim@umich.edutemplate <class Impl>
5482292SN/AFault
5492292SN/ALSQUnit<Impl>::read(Request *req, Request *sreqLow, Request *sreqHigh,
5502669Sktlim@umich.edu                    uint8_t *data, int load_idx)
5512669Sktlim@umich.edu{
5522292SN/A    DynInstPtr load_inst = loadQueue[load_idx];
5532292SN/A
5544032Sktlim@umich.edu    assert(load_inst);
5552329SN/A
5562669Sktlim@umich.edu    assert(!load_inst->isExecuted());
5572329SN/A
5582292SN/A    // Make sure this isn't an uncacheable access
5592292SN/A    // A bit of a hackish way to get uncached accesses to work only if they're
5602292SN/A    // at the head of the LSQ and are ready to commit (at the head of the ROB
5612292SN/A    // too).
5622292SN/A    if (req->isUncacheable() &&
5633803Sgblack@eecs.umich.edu        (load_idx != loadHead || !load_inst->isAtCommit())) {
5643803Sgblack@eecs.umich.edu        iewStage->rescheduleMemInst(load_inst);
5653803Sgblack@eecs.umich.edu        ++lsqRescheduledLoads;
5663803Sgblack@eecs.umich.edu        DPRINTF(LSQUnit, "Uncachable load [sn:%lli] PC %s\n",
5673803Sgblack@eecs.umich.edu                load_inst->seqNum, load_inst->pcState());
5683803Sgblack@eecs.umich.edu
5692669Sktlim@umich.edu        // Must delete request now that it wasn't handed off to
5702669Sktlim@umich.edu        // memory.  This is quite ugly.  @todo: Figure out the proper
5712292SN/A        // place to really handle request deletes.
5722669Sktlim@umich.edu        delete req;
5732292SN/A        if (TheISA::HasUnalignedMemAcc && sreqLow) {
5742292SN/A            delete sreqLow;
5752292SN/A            delete sreqHigh;
5762693Sktlim@umich.edu        }
5772678Sktlim@umich.edu        return new GenericISA::M5PanicFault(
5784022Sstever@eecs.umich.edu                "Uncachable load [sn:%llx] PC %s\n",
5794022Sstever@eecs.umich.edu                load_inst->seqNum, load_inst->pcState());
5802678Sktlim@umich.edu    }
5812678Sktlim@umich.edu
5822678Sktlim@umich.edu    // Check the SQ for any previous stores that might lead to forwarding
5832292SN/A    int store_idx = load_inst->sqIdx;
5842292SN/A
5852292SN/A    int store_size = 0;
5862292SN/A
5872292SN/A    DPRINTF(LSQUnit, "Read called, load idx: %i, store idx: %i, "
5882678Sktlim@umich.edu            "storeHead: %i addr: %#x%s\n",
5892727Sktlim@umich.edu            load_idx, store_idx, storeHead, req->getPaddr(),
5902292SN/A            sreqLow ? " split" : "");
5912292SN/A
5922292SN/A    if (req->isLLSC()) {
5932292SN/A        assert(!sreqLow);
5942292SN/A        // Disable recording the result temporarily.  Writing to misc
5952292SN/A        // regs normally updates the result, but this is not the
5962292SN/A        // desired behavior when handling store conditionals.
5972292SN/A        load_inst->recordResult(false);
5982292SN/A        TheISA::handleLockedRead(load_inst.get(), req);
5992292SN/A        load_inst->recordResult(true);
6004032Sktlim@umich.edu    }
6012292SN/A
6022292SN/A    if (req->isMmappedIpr()) {
6032292SN/A        assert(!load_inst->memData);
6042292SN/A        load_inst->memData = new uint8_t[64];
6052292SN/A
6062292SN/A        ThreadContext *thread = cpu->tcBase(lsqID);
6072292SN/A        Cycles delay(0);
6082669Sktlim@umich.edu        PacketPtr data_pkt = new Packet(req, MemCmd::ReadReq);
6092292SN/A
6102292SN/A        if (!TheISA::HasUnalignedMemAcc || !sreqLow) {
6112292SN/A            data_pkt->dataStatic(load_inst->memData);
6122292SN/A            delay = TheISA::handleIprRead(thread, data_pkt);
6132292SN/A        } else {
6142292SN/A            assert(sreqLow->isMmappedIpr() && sreqHigh->isMmappedIpr());
6152292SN/A            PacketPtr fst_data_pkt = new Packet(sreqLow, MemCmd::ReadReq);
6162292SN/A            PacketPtr snd_data_pkt = new Packet(sreqHigh, MemCmd::ReadReq);
6172669Sktlim@umich.edu
6182927Sktlim@umich.edu            fst_data_pkt->dataStatic(load_inst->memData);
6194032Sktlim@umich.edu            snd_data_pkt->dataStatic(load_inst->memData + sreqLow->getSize());
6202727Sktlim@umich.edu
6212292SN/A            delay = TheISA::handleIprRead(thread, fst_data_pkt);
6222292SN/A            Cycles delay2 = TheISA::handleIprRead(thread, snd_data_pkt);
6232292SN/A            if (delay2 > delay)
6242292SN/A                delay = delay2;
6252292SN/A
6262669Sktlim@umich.edu            delete sreqLow;
6272292SN/A            delete sreqHigh;
6284032Sktlim@umich.edu            delete fst_data_pkt;
6294032Sktlim@umich.edu            delete snd_data_pkt;
6304032Sktlim@umich.edu        }
6314032Sktlim@umich.edu        WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt, this);
6324032Sktlim@umich.edu        cpu->schedule(wb, cpu->clockEdge(delay));
6332292SN/A        return NoFault;
6342292SN/A    }
6352292SN/A
6362292SN/A    while (store_idx != -1) {
6372292SN/A        // End once we've reached the top of the LSQ
6382907Sktlim@umich.edu        if (store_idx == storeWBIdx) {
6392669Sktlim@umich.edu            break;
6402292SN/A        }
6412669Sktlim@umich.edu
6422669Sktlim@umich.edu        // Move the index to one younger
6432292SN/A        if (--store_idx < 0)
6442292SN/A            store_idx += SQEntries;
6452292SN/A
6462907Sktlim@umich.edu        assert(storeQueue[store_idx].inst);
6472907Sktlim@umich.edu
6483228Sktlim@umich.edu        store_size = storeQueue[store_idx].size;
6494022Sstever@eecs.umich.edu
6503228Sktlim@umich.edu        if (store_size == 0)
6513228Sktlim@umich.edu            continue;
6523228Sktlim@umich.edu        else if (storeQueue[store_idx].inst->uncacheable())
6533228Sktlim@umich.edu            continue;
6543228Sktlim@umich.edu
6553228Sktlim@umich.edu        assert(storeQueue[store_idx].inst->effAddrValid());
6563228Sktlim@umich.edu
6573228Sktlim@umich.edu        // Check if the store data is within the lower and upper bounds of
6582907Sktlim@umich.edu        // addresses that the request needs.
6593228Sktlim@umich.edu        bool store_has_lower_limit =
6603228Sktlim@umich.edu            req->getVaddr() >= storeQueue[store_idx].inst->effAddr;
6613228Sktlim@umich.edu        bool store_has_upper_limit =
6623228Sktlim@umich.edu            (req->getVaddr() + req->getSize()) <=
6633228Sktlim@umich.edu            (storeQueue[store_idx].inst->effAddr + store_size);
6644032Sktlim@umich.edu        bool lower_load_has_store_part =
6653228Sktlim@umich.edu            req->getVaddr() < (storeQueue[store_idx].inst->effAddr +
6663228Sktlim@umich.edu                           store_size);
6674032Sktlim@umich.edu        bool upper_load_has_store_part =
6684032Sktlim@umich.edu            (req->getVaddr() + req->getSize()) >
6693228Sktlim@umich.edu            storeQueue[store_idx].inst->effAddr;
6703221Sktlim@umich.edu
6713221Sktlim@umich.edu        // If the store's data has all of the data needed, we can forward.
6723221Sktlim@umich.edu        if ((store_has_lower_limit && store_has_upper_limit)) {
6732907Sktlim@umich.edu            // Get shift amount for offset into the store's data.
6742907Sktlim@umich.edu            int shift_amt = req->getVaddr() - storeQueue[store_idx].inst->effAddr;
6752907Sktlim@umich.edu
6762907Sktlim@umich.edu            memcpy(data, storeQueue[store_idx].data + shift_amt,
6772907Sktlim@umich.edu                   req->getSize());
6782907Sktlim@umich.edu
6792907Sktlim@umich.edu            assert(!load_inst->memData);
6802907Sktlim@umich.edu            load_inst->memData = new uint8_t[64];
6812907Sktlim@umich.edu
6824032Sktlim@umich.edu            memcpy(load_inst->memData,
6834032Sktlim@umich.edu                    storeQueue[store_idx].data + shift_amt, req->getSize());
6844032Sktlim@umich.edu
6852727Sktlim@umich.edu            DPRINTF(LSQUnit, "Forwarding from store idx %i to load to "
6863014Srdreslin@umich.edu                    "addr %#x, data %#x\n",
6873014Srdreslin@umich.edu                    store_idx, req->getVaddr(), data);
6882669Sktlim@umich.edu
6892669Sktlim@umich.edu            PacketPtr data_pkt = new Packet(req, MemCmd::ReadReq);
6902669Sktlim@umich.edu            data_pkt->dataStatic(load_inst->memData);
6912292SN/A
6922669Sktlim@umich.edu            WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt, this);
6932669Sktlim@umich.edu
6942669Sktlim@umich.edu            // We'll say this has a 1 cycle load-store forwarding latency
6952669Sktlim@umich.edu            // for now.
6962669Sktlim@umich.edu            // @todo: Need to make this a parameter.
6972669Sktlim@umich.edu            cpu->schedule(wb, curTick());
6982669Sktlim@umich.edu
6992669Sktlim@umich.edu            // Don't need to do anything special for split loads.
7002292SN/A            if (TheISA::HasUnalignedMemAcc && sreqLow) {
7012292SN/A                delete sreqLow;
7022669Sktlim@umich.edu                delete sreqHigh;
7032292SN/A            }
7042292SN/A
7052292SN/A            ++lsqForwLoads;
7062292SN/A            return NoFault;
7072292SN/A        } else if ((store_has_lower_limit && lower_load_has_store_part) ||
7082669Sktlim@umich.edu                   (store_has_upper_limit && upper_load_has_store_part) ||
7092292SN/A                   (lower_load_has_store_part && upper_load_has_store_part)) {
7102292SN/A            // This is the partial store-load forwarding case where a store
7112292SN/A            // has only part of the load's data.
7122292SN/A
7132292SN/A            // If it's already been written back, then don't worry about
7142669Sktlim@umich.edu            // stalling on it.
7152292SN/A            if (storeQueue[store_idx].completed) {
7162329SN/A                panic("Should not check one of these");
7172292SN/A                continue;
7182292SN/A            }
7192292SN/A
7202329SN/A            // Must stall load and force it to retry, so long as it's the oldest
7212292SN/A            // load that needs to do so.
7222292SN/A            if (!stalled ||
7232292SN/A                (stalled &&
7242292SN/A                 load_inst->seqNum <
7252292SN/A                 loadQueue[stallingLoadIdx]->seqNum)) {
7262292SN/A                stalled = true;
727                stallingStoreIsn = storeQueue[store_idx].inst->seqNum;
728                stallingLoadIdx = load_idx;
729            }
730
731            // Tell IQ/mem dep unit that this instruction will need to be
732            // rescheduled eventually
733            iewStage->rescheduleMemInst(load_inst);
734            iewStage->decrWb(load_inst->seqNum);
735            load_inst->clearIssued();
736            ++lsqRescheduledLoads;
737
738            // Do not generate a writeback event as this instruction is not
739            // complete.
740            DPRINTF(LSQUnit, "Load-store forwarding mis-match. "
741                    "Store idx %i to load addr %#x\n",
742                    store_idx, req->getVaddr());
743
744            // Must delete request now that it wasn't handed off to
745            // memory.  This is quite ugly.  @todo: Figure out the
746            // proper place to really handle request deletes.
747            delete req;
748            if (TheISA::HasUnalignedMemAcc && sreqLow) {
749                delete sreqLow;
750                delete sreqHigh;
751            }
752
753            return NoFault;
754        }
755    }
756
757    // If there's no forwarding case, then go access memory
758    DPRINTF(LSQUnit, "Doing memory access for inst [sn:%lli] PC %s\n",
759            load_inst->seqNum, load_inst->pcState());
760
761    assert(!load_inst->memData);
762    load_inst->memData = new uint8_t[64];
763
764    ++usedPorts;
765
766    // if we the cache is not blocked, do cache access
767    bool completedFirst = false;
768    if (!lsq->cacheBlocked()) {
769        MemCmd command =
770            req->isLLSC() ? MemCmd::LoadLockedReq : MemCmd::ReadReq;
771        PacketPtr data_pkt = new Packet(req, command);
772        PacketPtr fst_data_pkt = NULL;
773        PacketPtr snd_data_pkt = NULL;
774
775        data_pkt->dataStatic(load_inst->memData);
776
777        LSQSenderState *state = new LSQSenderState;
778        state->isLoad = true;
779        state->idx = load_idx;
780        state->inst = load_inst;
781        data_pkt->senderState = state;
782
783        if (!TheISA::HasUnalignedMemAcc || !sreqLow) {
784
785            // Point the first packet at the main data packet.
786            fst_data_pkt = data_pkt;
787        } else {
788
789            // Create the split packets.
790            fst_data_pkt = new Packet(sreqLow, command);
791            snd_data_pkt = new Packet(sreqHigh, command);
792
793            fst_data_pkt->dataStatic(load_inst->memData);
794            snd_data_pkt->dataStatic(load_inst->memData + sreqLow->getSize());
795
796            fst_data_pkt->senderState = state;
797            snd_data_pkt->senderState = state;
798
799            state->isSplit = true;
800            state->outstanding = 2;
801            state->mainPkt = data_pkt;
802        }
803
804        if (!dcachePort->sendTimingReq(fst_data_pkt)) {
805            // Delete state and data packet because a load retry
806            // initiates a pipeline restart; it does not retry.
807            delete state;
808            delete data_pkt->req;
809            delete data_pkt;
810            if (TheISA::HasUnalignedMemAcc && sreqLow) {
811                delete fst_data_pkt->req;
812                delete fst_data_pkt;
813                delete snd_data_pkt->req;
814                delete snd_data_pkt;
815                sreqLow = NULL;
816                sreqHigh = NULL;
817            }
818
819            req = NULL;
820
821            // If the access didn't succeed, tell the LSQ by setting
822            // the retry thread id.
823            lsq->setRetryTid(lsqID);
824        } else if (TheISA::HasUnalignedMemAcc && sreqLow) {
825            completedFirst = true;
826
827            // The first packet was sent without problems, so send this one
828            // too. If there is a problem with this packet then the whole
829            // load will be squashed, so indicate this to the state object.
830            // The first packet will return in completeDataAccess and be
831            // handled there.
832            ++usedPorts;
833            if (!dcachePort->sendTimingReq(snd_data_pkt)) {
834
835                // The main packet will be deleted in completeDataAccess.
836                delete snd_data_pkt->req;
837                delete snd_data_pkt;
838
839                state->complete();
840
841                req = NULL;
842                sreqHigh = NULL;
843
844                lsq->setRetryTid(lsqID);
845            }
846        }
847    }
848
849    // If the cache was blocked, or has become blocked due to the access,
850    // handle it.
851    if (lsq->cacheBlocked()) {
852        if (req)
853            delete req;
854        if (TheISA::HasUnalignedMemAcc && sreqLow && !completedFirst) {
855            delete sreqLow;
856            delete sreqHigh;
857        }
858
859        ++lsqCacheBlocked;
860
861        // If the first part of a split access succeeds, then let the LSQ
862        // handle the decrWb when completeDataAccess is called upon return
863        // of the requested first part of data
864        if (!completedFirst)
865            iewStage->decrWb(load_inst->seqNum);
866
867        // There's an older load that's already going to squash.
868        if (isLoadBlocked && blockedLoadSeqNum < load_inst->seqNum)
869            return NoFault;
870
871        // Record that the load was blocked due to memory.  This
872        // load will squash all instructions after it, be
873        // refetched, and re-executed.
874        isLoadBlocked = true;
875        loadBlockedHandled = false;
876        blockedLoadSeqNum = load_inst->seqNum;
877        // No fault occurred, even though the interface is blocked.
878        return NoFault;
879    }
880
881    return NoFault;
882}
883
884template <class Impl>
885Fault
886LSQUnit<Impl>::write(Request *req, Request *sreqLow, Request *sreqHigh,
887                     uint8_t *data, int store_idx)
888{
889    assert(storeQueue[store_idx].inst);
890
891    DPRINTF(LSQUnit, "Doing write to store idx %i, addr %#x data %#x"
892            " | storeHead:%i [sn:%i]\n",
893            store_idx, req->getPaddr(), data, storeHead,
894            storeQueue[store_idx].inst->seqNum);
895
896    storeQueue[store_idx].req = req;
897    storeQueue[store_idx].sreqLow = sreqLow;
898    storeQueue[store_idx].sreqHigh = sreqHigh;
899    unsigned size = req->getSize();
900    storeQueue[store_idx].size = size;
901    assert(size <= sizeof(storeQueue[store_idx].data));
902
903    // Split stores can only occur in ISAs with unaligned memory accesses.  If
904    // a store request has been split, sreqLow and sreqHigh will be non-null.
905    if (TheISA::HasUnalignedMemAcc && sreqLow) {
906        storeQueue[store_idx].isSplit = true;
907    }
908
909    memcpy(storeQueue[store_idx].data, data, size);
910
911    // This function only writes the data to the store queue, so no fault
912    // can happen here.
913    return NoFault;
914}
915
916#endif // __CPU_O3_LSQ_UNIT_HH__
917