lsq_unit.hh revision 4326
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 <map>
372292SN/A#include <queue>
382292SN/A
392329SN/A#include "arch/faults.hh"
403326Sktlim@umich.edu#include "arch/locked_mem.hh"
412292SN/A#include "config/full_system.hh"
422292SN/A#include "base/hashmap.hh"
432292SN/A#include "cpu/inst_seq.hh"
443348Sbinkertn@umich.edu#include "mem/packet.hh"
452669Sktlim@umich.edu#include "mem/port.hh"
462292SN/A
472292SN/A/**
482329SN/A * Class that implements the actual LQ and SQ for each specific
492329SN/A * thread.  Both are circular queues; load entries are freed upon
502329SN/A * committing, while store entries are freed once they writeback. The
512329SN/A * LSQUnit tracks if there are memory ordering violations, and also
522329SN/A * detects partial load to store forwarding cases (a store only has
532329SN/A * part of a load's data) that requires the load to wait until the
542329SN/A * store writes back. In the former case it holds onto the instruction
552329SN/A * until the dependence unit looks at it, and in the latter it stalls
562329SN/A * the LSQ until the store writes back. At that point the load is
572329SN/A * replayed.
582292SN/A */
592292SN/Atemplate <class Impl>
602292SN/Aclass LSQUnit {
612292SN/A  protected:
622292SN/A    typedef TheISA::IntReg IntReg;
632292SN/A  public:
642292SN/A    typedef typename Impl::Params Params;
652733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
662292SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
672292SN/A    typedef typename Impl::CPUPol::IEW IEW;
682907Sktlim@umich.edu    typedef typename Impl::CPUPol::LSQ LSQ;
692292SN/A    typedef typename Impl::CPUPol::IssueStruct IssueStruct;
702292SN/A
712292SN/A  public:
722292SN/A    /** Constructs an LSQ unit. init() must be called prior to use. */
732292SN/A    LSQUnit();
742292SN/A
752292SN/A    /** Initializes the LSQ unit with the specified number of entries. */
762907Sktlim@umich.edu    void init(Params *params, LSQ *lsq_ptr, unsigned maxLQEntries,
772292SN/A              unsigned maxSQEntries, unsigned id);
782292SN/A
792292SN/A    /** Returns the name of the LSQ unit. */
802292SN/A    std::string name() const;
812292SN/A
822727Sktlim@umich.edu    /** Registers statistics. */
832727Sktlim@umich.edu    void regStats();
842727Sktlim@umich.edu
852292SN/A    /** Sets the CPU pointer. */
862733Sktlim@umich.edu    void setCPU(O3CPU *cpu_ptr);
872292SN/A
882292SN/A    /** Sets the IEW stage pointer. */
892292SN/A    void setIEW(IEW *iew_ptr)
902292SN/A    { iewStage = iew_ptr; }
912292SN/A
922907Sktlim@umich.edu    /** Sets the pointer to the dcache port. */
932907Sktlim@umich.edu    void setDcachePort(Port *dcache_port)
942907Sktlim@umich.edu    { dcachePort = dcache_port; }
952907Sktlim@umich.edu
962348SN/A    /** Switches out LSQ unit. */
972307SN/A    void switchOut();
982307SN/A
992348SN/A    /** Takes over from another CPU's thread. */
1002307SN/A    void takeOverFrom();
1012307SN/A
1022348SN/A    /** Returns if the LSQ is switched out. */
1032307SN/A    bool isSwitchedOut() { return switchedOut; }
1042307SN/A
1052292SN/A    /** Ticks the LSQ unit, which in this case only resets the number of
1062292SN/A     * used cache ports.
1072292SN/A     * @todo: Move the number of used ports up to the LSQ level so it can
1082292SN/A     * be shared by all LSQ units.
1092292SN/A     */
1102292SN/A    void tick() { usedPorts = 0; }
1112292SN/A
1122292SN/A    /** Inserts an instruction. */
1132292SN/A    void insert(DynInstPtr &inst);
1142292SN/A    /** Inserts a load instruction. */
1152292SN/A    void insertLoad(DynInstPtr &load_inst);
1162292SN/A    /** Inserts a store instruction. */
1172292SN/A    void insertStore(DynInstPtr &store_inst);
1182292SN/A
1192292SN/A    /** Executes a load instruction. */
1202292SN/A    Fault executeLoad(DynInstPtr &inst);
1212292SN/A
1222329SN/A    Fault executeLoad(int lq_idx) { panic("Not implemented"); return NoFault; }
1232292SN/A    /** Executes a store instruction. */
1242292SN/A    Fault executeStore(DynInstPtr &inst);
1252292SN/A
1262292SN/A    /** Commits the head load. */
1272292SN/A    void commitLoad();
1282292SN/A    /** Commits loads older than a specific sequence number. */
1292292SN/A    void commitLoads(InstSeqNum &youngest_inst);
1302292SN/A
1312292SN/A    /** Commits stores older than a specific sequence number. */
1322292SN/A    void commitStores(InstSeqNum &youngest_inst);
1332292SN/A
1342292SN/A    /** Writes back stores. */
1352292SN/A    void writebackStores();
1362292SN/A
1372790Sktlim@umich.edu    /** Completes the data access that has been returned from the
1382790Sktlim@umich.edu     * memory system. */
1392669Sktlim@umich.edu    void completeDataAccess(PacketPtr pkt);
1402669Sktlim@umich.edu
1412292SN/A    /** Clears all the entries in the LQ. */
1422292SN/A    void clearLQ();
1432292SN/A
1442292SN/A    /** Clears all the entries in the SQ. */
1452292SN/A    void clearSQ();
1462292SN/A
1472292SN/A    /** Resizes the LQ to a given size. */
1482292SN/A    void resizeLQ(unsigned size);
1492292SN/A
1502292SN/A    /** Resizes the SQ to a given size. */
1512292SN/A    void resizeSQ(unsigned size);
1522292SN/A
1532292SN/A    /** Squashes all instructions younger than a specific sequence number. */
1542292SN/A    void squash(const InstSeqNum &squashed_num);
1552292SN/A
1562292SN/A    /** Returns if there is a memory ordering violation. Value is reset upon
1572292SN/A     * call to getMemDepViolator().
1582292SN/A     */
1592292SN/A    bool violation() { return memDepViolator; }
1602292SN/A
1612292SN/A    /** Returns the memory ordering violator. */
1622292SN/A    DynInstPtr getMemDepViolator();
1632292SN/A
1642329SN/A    /** Returns if a load became blocked due to the memory system. */
1652292SN/A    bool loadBlocked()
1662292SN/A    { return isLoadBlocked; }
1672292SN/A
1682348SN/A    /** Clears the signal that a load became blocked. */
1692292SN/A    void clearLoadBlocked()
1702292SN/A    { isLoadBlocked = false; }
1712292SN/A
1722348SN/A    /** Returns if the blocked load was handled. */
1732292SN/A    bool isLoadBlockedHandled()
1742292SN/A    { return loadBlockedHandled; }
1752292SN/A
1762348SN/A    /** Records the blocked load as being handled. */
1772292SN/A    void setLoadBlockedHandled()
1782292SN/A    { loadBlockedHandled = true; }
1792292SN/A
1802292SN/A    /** Returns the number of free entries (min of free LQ and SQ entries). */
1812292SN/A    unsigned numFreeEntries();
1822292SN/A
1832292SN/A    /** Returns the number of loads ready to execute. */
1842292SN/A    int numLoadsReady();
1852292SN/A
1862292SN/A    /** Returns the number of loads in the LQ. */
1872292SN/A    int numLoads() { return loads; }
1882292SN/A
1892292SN/A    /** Returns the number of stores in the SQ. */
1902292SN/A    int numStores() { return stores; }
1912292SN/A
1922292SN/A    /** Returns if either the LQ or SQ is full. */
1932292SN/A    bool isFull() { return lqFull() || sqFull(); }
1942292SN/A
1952292SN/A    /** Returns if the LQ is full. */
1962292SN/A    bool lqFull() { return loads >= (LQEntries - 1); }
1972292SN/A
1982292SN/A    /** Returns if the SQ is full. */
1992292SN/A    bool sqFull() { return stores >= (SQEntries - 1); }
2002292SN/A
2012292SN/A    /** Returns the number of instructions in the LSQ. */
2022292SN/A    unsigned getCount() { return loads + stores; }
2032292SN/A
2042292SN/A    /** Returns if there are any stores to writeback. */
2052292SN/A    bool hasStoresToWB() { return storesToWB; }
2062292SN/A
2072292SN/A    /** Returns the number of stores to writeback. */
2082292SN/A    int numStoresToWB() { return storesToWB; }
2092292SN/A
2102292SN/A    /** Returns if the LSQ unit will writeback on this cycle. */
2112292SN/A    bool willWB() { return storeQueue[storeWBIdx].canWB &&
2122678Sktlim@umich.edu                        !storeQueue[storeWBIdx].completed &&
2132678Sktlim@umich.edu                        !isStoreBlocked; }
2142292SN/A
2152907Sktlim@umich.edu    /** Handles doing the retry. */
2162907Sktlim@umich.edu    void recvRetry();
2172907Sktlim@umich.edu
2182292SN/A  private:
2192698Sktlim@umich.edu    /** Writes back the instruction, sending it to IEW. */
2202678Sktlim@umich.edu    void writeback(DynInstPtr &inst, PacketPtr pkt);
2212678Sktlim@umich.edu
2222698Sktlim@umich.edu    /** Handles completing the send of a store to memory. */
2233349Sbinkertn@umich.edu    void storePostSend(PacketPtr pkt);
2242693Sktlim@umich.edu
2252292SN/A    /** Completes the store at the specified index. */
2262292SN/A    void completeStore(int store_idx);
2272292SN/A
2282292SN/A    /** Increments the given store index (circular queue). */
2292292SN/A    inline void incrStIdx(int &store_idx);
2302292SN/A    /** Decrements the given store index (circular queue). */
2312292SN/A    inline void decrStIdx(int &store_idx);
2322292SN/A    /** Increments the given load index (circular queue). */
2332292SN/A    inline void incrLdIdx(int &load_idx);
2342292SN/A    /** Decrements the given load index (circular queue). */
2352292SN/A    inline void decrLdIdx(int &load_idx);
2362292SN/A
2372329SN/A  public:
2382329SN/A    /** Debugging function to dump instructions in the LSQ. */
2392329SN/A    void dumpInsts();
2402329SN/A
2412292SN/A  private:
2422292SN/A    /** Pointer to the CPU. */
2432733Sktlim@umich.edu    O3CPU *cpu;
2442292SN/A
2452292SN/A    /** Pointer to the IEW stage. */
2462292SN/A    IEW *iewStage;
2472292SN/A
2482907Sktlim@umich.edu    /** Pointer to the LSQ. */
2492907Sktlim@umich.edu    LSQ *lsq;
2502669Sktlim@umich.edu
2512907Sktlim@umich.edu    /** Pointer to the dcache port.  Used only for sending. */
2522907Sktlim@umich.edu    Port *dcachePort;
2532292SN/A
2542698Sktlim@umich.edu    /** Derived class to hold any sender state the LSQ needs. */
2552678Sktlim@umich.edu    class LSQSenderState : public Packet::SenderState
2562678Sktlim@umich.edu    {
2572678Sktlim@umich.edu      public:
2582698Sktlim@umich.edu        /** Default constructor. */
2592678Sktlim@umich.edu        LSQSenderState()
2602678Sktlim@umich.edu            : noWB(false)
2612678Sktlim@umich.edu        { }
2622678Sktlim@umich.edu
2632698Sktlim@umich.edu        /** Instruction who initiated the access to memory. */
2642678Sktlim@umich.edu        DynInstPtr inst;
2652698Sktlim@umich.edu        /** Whether or not it is a load. */
2662678Sktlim@umich.edu        bool isLoad;
2672698Sktlim@umich.edu        /** The LQ/SQ index of the instruction. */
2682678Sktlim@umich.edu        int idx;
2692698Sktlim@umich.edu        /** Whether or not the instruction will need to writeback. */
2702678Sktlim@umich.edu        bool noWB;
2712678Sktlim@umich.edu    };
2722678Sktlim@umich.edu
2732698Sktlim@umich.edu    /** Writeback event, specifically for when stores forward data to loads. */
2742678Sktlim@umich.edu    class WritebackEvent : public Event {
2752678Sktlim@umich.edu      public:
2762678Sktlim@umich.edu        /** Constructs a writeback event. */
2772678Sktlim@umich.edu        WritebackEvent(DynInstPtr &_inst, PacketPtr pkt, LSQUnit *lsq_ptr);
2782678Sktlim@umich.edu
2792678Sktlim@umich.edu        /** Processes the writeback event. */
2802678Sktlim@umich.edu        void process();
2812678Sktlim@umich.edu
2822678Sktlim@umich.edu        /** Returns the description of this event. */
2832678Sktlim@umich.edu        const char *description();
2842678Sktlim@umich.edu
2852678Sktlim@umich.edu      private:
2862698Sktlim@umich.edu        /** Instruction whose results are being written back. */
2872678Sktlim@umich.edu        DynInstPtr inst;
2882678Sktlim@umich.edu
2892698Sktlim@umich.edu        /** The packet that would have been sent to memory. */
2902678Sktlim@umich.edu        PacketPtr pkt;
2912678Sktlim@umich.edu
2922678Sktlim@umich.edu        /** The pointer to the LSQ unit that issued the store. */
2932678Sktlim@umich.edu        LSQUnit<Impl> *lsqPtr;
2942678Sktlim@umich.edu    };
2952678Sktlim@umich.edu
2962292SN/A  public:
2972292SN/A    struct SQEntry {
2982292SN/A        /** Constructs an empty store queue entry. */
2992292SN/A        SQEntry()
3004326Sgblack@eecs.umich.edu            : inst(NULL), req(NULL), size(0),
3012292SN/A              canWB(0), committed(0), completed(0)
3024326Sgblack@eecs.umich.edu        {
3034326Sgblack@eecs.umich.edu            bzero(data, sizeof(data));
3044326Sgblack@eecs.umich.edu        }
3052292SN/A
3062292SN/A        /** Constructs a store queue entry for a given instruction. */
3072292SN/A        SQEntry(DynInstPtr &_inst)
3084326Sgblack@eecs.umich.edu            : inst(_inst), req(NULL), size(0),
3092292SN/A              canWB(0), committed(0), completed(0)
3104326Sgblack@eecs.umich.edu        {
3114326Sgblack@eecs.umich.edu            bzero(data, sizeof(data));
3124326Sgblack@eecs.umich.edu        }
3132292SN/A
3142292SN/A        /** The store instruction. */
3152292SN/A        DynInstPtr inst;
3162669Sktlim@umich.edu        /** The request for the store. */
3172669Sktlim@umich.edu        RequestPtr req;
3182292SN/A        /** The size of the store. */
3192292SN/A        int size;
3202292SN/A        /** The store data. */
3214326Sgblack@eecs.umich.edu        char data[sizeof(IntReg)];
3222292SN/A        /** Whether or not the store can writeback. */
3232292SN/A        bool canWB;
3242292SN/A        /** Whether or not the store is committed. */
3252292SN/A        bool committed;
3262292SN/A        /** Whether or not the store is completed. */
3272292SN/A        bool completed;
3282292SN/A    };
3292329SN/A
3302292SN/A  private:
3312292SN/A    /** The LSQUnit thread id. */
3322292SN/A    unsigned lsqID;
3332292SN/A
3342292SN/A    /** The store queue. */
3352292SN/A    std::vector<SQEntry> storeQueue;
3362292SN/A
3372292SN/A    /** The load queue. */
3382292SN/A    std::vector<DynInstPtr> loadQueue;
3392292SN/A
3402329SN/A    /** The number of LQ entries, plus a sentinel entry (circular queue).
3412329SN/A     *  @todo: Consider having var that records the true number of LQ entries.
3422329SN/A     */
3432292SN/A    unsigned LQEntries;
3442329SN/A    /** The number of SQ entries, plus a sentinel entry (circular queue).
3452329SN/A     *  @todo: Consider having var that records the true number of SQ entries.
3462329SN/A     */
3472292SN/A    unsigned SQEntries;
3482292SN/A
3492292SN/A    /** The number of load instructions in the LQ. */
3502292SN/A    int loads;
3512329SN/A    /** The number of store instructions in the SQ. */
3522292SN/A    int stores;
3532292SN/A    /** The number of store instructions in the SQ waiting to writeback. */
3542292SN/A    int storesToWB;
3552292SN/A
3562292SN/A    /** The index of the head instruction in the LQ. */
3572292SN/A    int loadHead;
3582292SN/A    /** The index of the tail instruction in the LQ. */
3592292SN/A    int loadTail;
3602292SN/A
3612292SN/A    /** The index of the head instruction in the SQ. */
3622292SN/A    int storeHead;
3632329SN/A    /** The index of the first instruction that may be ready to be
3642329SN/A     * written back, and has not yet been written back.
3652292SN/A     */
3662292SN/A    int storeWBIdx;
3672292SN/A    /** The index of the tail instruction in the SQ. */
3682292SN/A    int storeTail;
3692292SN/A
3702292SN/A    /// @todo Consider moving to a more advanced model with write vs read ports
3712292SN/A    /** The number of cache ports available each cycle. */
3722292SN/A    int cachePorts;
3732292SN/A
3742292SN/A    /** The number of used cache ports in this cycle. */
3752292SN/A    int usedPorts;
3762292SN/A
3772348SN/A    /** Is the LSQ switched out. */
3782307SN/A    bool switchedOut;
3792307SN/A
3802292SN/A    //list<InstSeqNum> mshrSeqNums;
3812292SN/A
3822292SN/A    /** Wire to read information from the issue stage time queue. */
3832292SN/A    typename TimeBuffer<IssueStruct>::wire fromIssue;
3842292SN/A
3852292SN/A    /** Whether or not the LSQ is stalled. */
3862292SN/A    bool stalled;
3872292SN/A    /** The store that causes the stall due to partial store to load
3882292SN/A     * forwarding.
3892292SN/A     */
3902292SN/A    InstSeqNum stallingStoreIsn;
3912292SN/A    /** The index of the above store. */
3922292SN/A    int stallingLoadIdx;
3932292SN/A
3942698Sktlim@umich.edu    /** The packet that needs to be retried. */
3952698Sktlim@umich.edu    PacketPtr retryPkt;
3962693Sktlim@umich.edu
3972698Sktlim@umich.edu    /** Whehter or not a store is blocked due to the memory system. */
3982678Sktlim@umich.edu    bool isStoreBlocked;
3992678Sktlim@umich.edu
4002329SN/A    /** Whether or not a load is blocked due to the memory system. */
4012292SN/A    bool isLoadBlocked;
4022292SN/A
4032348SN/A    /** Has the blocked load been handled. */
4042292SN/A    bool loadBlockedHandled;
4052292SN/A
4062348SN/A    /** The sequence number of the blocked load. */
4072292SN/A    InstSeqNum blockedLoadSeqNum;
4082292SN/A
4092292SN/A    /** The oldest load that caused a memory ordering violation. */
4102292SN/A    DynInstPtr memDepViolator;
4112292SN/A
4122292SN/A    // Will also need how many read/write ports the Dcache has.  Or keep track
4132292SN/A    // of that in stage that is one level up, and only call executeLoad/Store
4142292SN/A    // the appropriate number of times.
4152727Sktlim@umich.edu    /** Total number of loads forwaded from LSQ stores. */
4162727Sktlim@umich.edu    Stats::Scalar<> lsqForwLoads;
4172307SN/A
4183126Sktlim@umich.edu    /** Total number of loads ignored due to invalid addresses. */
4193126Sktlim@umich.edu    Stats::Scalar<> invAddrLoads;
4203126Sktlim@umich.edu
4213126Sktlim@umich.edu    /** Total number of squashed loads. */
4223126Sktlim@umich.edu    Stats::Scalar<> lsqSquashedLoads;
4233126Sktlim@umich.edu
4243126Sktlim@umich.edu    /** Total number of responses from the memory system that are
4253126Sktlim@umich.edu     * ignored due to the instruction already being squashed. */
4263126Sktlim@umich.edu    Stats::Scalar<> lsqIgnoredResponses;
4273126Sktlim@umich.edu
4283126Sktlim@umich.edu    /** Tota number of memory ordering violations. */
4293126Sktlim@umich.edu    Stats::Scalar<> lsqMemOrderViolation;
4303126Sktlim@umich.edu
4312727Sktlim@umich.edu    /** Total number of squashed stores. */
4322727Sktlim@umich.edu    Stats::Scalar<> lsqSquashedStores;
4332727Sktlim@umich.edu
4342727Sktlim@umich.edu    /** Total number of software prefetches ignored due to invalid addresses. */
4352727Sktlim@umich.edu    Stats::Scalar<> invAddrSwpfs;
4362727Sktlim@umich.edu
4372727Sktlim@umich.edu    /** Ready loads blocked due to partial store-forwarding. */
4382727Sktlim@umich.edu    Stats::Scalar<> lsqBlockedLoads;
4392727Sktlim@umich.edu
4402727Sktlim@umich.edu    /** Number of loads that were rescheduled. */
4412727Sktlim@umich.edu    Stats::Scalar<> lsqRescheduledLoads;
4422727Sktlim@umich.edu
4432727Sktlim@umich.edu    /** Number of times the LSQ is blocked due to the cache. */
4442727Sktlim@umich.edu    Stats::Scalar<> lsqCacheBlocked;
4452727Sktlim@umich.edu
4462292SN/A  public:
4472292SN/A    /** Executes the load at the given index. */
4482292SN/A    template <class T>
4492669Sktlim@umich.edu    Fault read(Request *req, T &data, int load_idx);
4502292SN/A
4512292SN/A    /** Executes the store at the given index. */
4522292SN/A    template <class T>
4532669Sktlim@umich.edu    Fault write(Request *req, T &data, int store_idx);
4542292SN/A
4552292SN/A    /** Returns the index of the head load instruction. */
4562292SN/A    int getLoadHead() { return loadHead; }
4572292SN/A    /** Returns the sequence number of the head load instruction. */
4582292SN/A    InstSeqNum getLoadHeadSeqNum()
4592292SN/A    {
4602292SN/A        if (loadQueue[loadHead]) {
4612292SN/A            return loadQueue[loadHead]->seqNum;
4622292SN/A        } else {
4632292SN/A            return 0;
4642292SN/A        }
4652292SN/A
4662292SN/A    }
4672292SN/A
4682292SN/A    /** Returns the index of the head store instruction. */
4692292SN/A    int getStoreHead() { return storeHead; }
4702292SN/A    /** Returns the sequence number of the head store instruction. */
4712292SN/A    InstSeqNum getStoreHeadSeqNum()
4722292SN/A    {
4732292SN/A        if (storeQueue[storeHead].inst) {
4742292SN/A            return storeQueue[storeHead].inst->seqNum;
4752292SN/A        } else {
4762292SN/A            return 0;
4772292SN/A        }
4782292SN/A
4792292SN/A    }
4802292SN/A
4812292SN/A    /** Returns whether or not the LSQ unit is stalled. */
4822292SN/A    bool isStalled()  { return stalled; }
4832292SN/A};
4842292SN/A
4852292SN/Atemplate <class Impl>
4862292SN/Atemplate <class T>
4872292SN/AFault
4882669Sktlim@umich.eduLSQUnit<Impl>::read(Request *req, T &data, int load_idx)
4892292SN/A{
4902669Sktlim@umich.edu    DynInstPtr load_inst = loadQueue[load_idx];
4912292SN/A
4922669Sktlim@umich.edu    assert(load_inst);
4932669Sktlim@umich.edu
4942669Sktlim@umich.edu    assert(!load_inst->isExecuted());
4952292SN/A
4962292SN/A    // Make sure this isn't an uncacheable access
4972292SN/A    // A bit of a hackish way to get uncached accesses to work only if they're
4982292SN/A    // at the head of the LSQ and are ready to commit (at the head of the ROB
4992292SN/A    // too).
5003172Sstever@eecs.umich.edu    if (req->isUncacheable() &&
5012731Sktlim@umich.edu        (load_idx != loadHead || !load_inst->isAtCommit())) {
5022669Sktlim@umich.edu        iewStage->rescheduleMemInst(load_inst);
5032727Sktlim@umich.edu        ++lsqRescheduledLoads;
5044032Sktlim@umich.edu
5054032Sktlim@umich.edu        // Must delete request now that it wasn't handed off to
5064032Sktlim@umich.edu        // memory.  This is quite ugly.  @todo: Figure out the proper
5074032Sktlim@umich.edu        // place to really handle request deletes.
5084032Sktlim@umich.edu        delete req;
5092292SN/A        return TheISA::genMachineCheckFault();
5102292SN/A    }
5112292SN/A
5122292SN/A    // Check the SQ for any previous stores that might lead to forwarding
5132669Sktlim@umich.edu    int store_idx = load_inst->sqIdx;
5142292SN/A
5152292SN/A    int store_size = 0;
5162292SN/A
5172292SN/A    DPRINTF(LSQUnit, "Read called, load idx: %i, store idx: %i, "
5182292SN/A            "storeHead: %i addr: %#x\n",
5192669Sktlim@umich.edu            load_idx, store_idx, storeHead, req->getPaddr());
5202292SN/A
5213172Sstever@eecs.umich.edu    if (req->isLocked()) {
5223326Sktlim@umich.edu        // Disable recording the result temporarily.  Writing to misc
5233326Sktlim@umich.edu        // regs normally updates the result, but this is not the
5243326Sktlim@umich.edu        // desired behavior when handling store conditionals.
5253326Sktlim@umich.edu        load_inst->recordResult = false;
5263326Sktlim@umich.edu        TheISA::handleLockedRead(load_inst.get(), req);
5273326Sktlim@umich.edu        load_inst->recordResult = true;
5282292SN/A    }
5292292SN/A
5302292SN/A    while (store_idx != -1) {
5312292SN/A        // End once we've reached the top of the LSQ
5322292SN/A        if (store_idx == storeWBIdx) {
5332292SN/A            break;
5342292SN/A        }
5352292SN/A
5362292SN/A        // Move the index to one younger
5372292SN/A        if (--store_idx < 0)
5382292SN/A            store_idx += SQEntries;
5392292SN/A
5402292SN/A        assert(storeQueue[store_idx].inst);
5412292SN/A
5422292SN/A        store_size = storeQueue[store_idx].size;
5432292SN/A
5442292SN/A        if (store_size == 0)
5452292SN/A            continue;
5464032Sktlim@umich.edu        else if (storeQueue[store_idx].inst->uncacheable())
5474032Sktlim@umich.edu            continue;
5484032Sktlim@umich.edu
5494032Sktlim@umich.edu        assert(storeQueue[store_idx].inst->effAddrValid);
5502292SN/A
5512292SN/A        // Check if the store data is within the lower and upper bounds of
5522292SN/A        // addresses that the request needs.
5532292SN/A        bool store_has_lower_limit =
5542669Sktlim@umich.edu            req->getVaddr() >= storeQueue[store_idx].inst->effAddr;
5552292SN/A        bool store_has_upper_limit =
5562669Sktlim@umich.edu            (req->getVaddr() + req->getSize()) <=
5572669Sktlim@umich.edu            (storeQueue[store_idx].inst->effAddr + store_size);
5582292SN/A        bool lower_load_has_store_part =
5592669Sktlim@umich.edu            req->getVaddr() < (storeQueue[store_idx].inst->effAddr +
5602292SN/A                           store_size);
5612292SN/A        bool upper_load_has_store_part =
5622669Sktlim@umich.edu            (req->getVaddr() + req->getSize()) >
5632669Sktlim@umich.edu            storeQueue[store_idx].inst->effAddr;
5642292SN/A
5652292SN/A        // If the store's data has all of the data needed, we can forward.
5664032Sktlim@umich.edu        if ((store_has_lower_limit && store_has_upper_limit)) {
5672329SN/A            // Get shift amount for offset into the store's data.
5682669Sktlim@umich.edu            int shift_amt = req->getVaddr() & (store_size - 1);
5692292SN/A
5704326Sgblack@eecs.umich.edu            memcpy(&data, storeQueue[store_idx].data + shift_amt, sizeof(T));
5713803Sgblack@eecs.umich.edu
5722669Sktlim@umich.edu            assert(!load_inst->memData);
5732669Sktlim@umich.edu            load_inst->memData = new uint8_t[64];
5742292SN/A
5754326Sgblack@eecs.umich.edu            memcpy(load_inst->memData,
5764326Sgblack@eecs.umich.edu                    storeQueue[store_idx].data + shift_amt, req->getSize());
5772292SN/A
5782292SN/A            DPRINTF(LSQUnit, "Forwarding from store idx %i to load to "
5792292SN/A                    "addr %#x, data %#x\n",
5802693Sktlim@umich.edu                    store_idx, req->getVaddr(), data);
5812678Sktlim@umich.edu
5824022Sstever@eecs.umich.edu            PacketPtr data_pkt = new Packet(req, MemCmd::ReadReq,
5834022Sstever@eecs.umich.edu                                            Packet::Broadcast);
5842678Sktlim@umich.edu            data_pkt->dataStatic(load_inst->memData);
5852678Sktlim@umich.edu
5862678Sktlim@umich.edu            WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt, this);
5872292SN/A
5882292SN/A            // We'll say this has a 1 cycle load-store forwarding latency
5892292SN/A            // for now.
5902292SN/A            // @todo: Need to make this a parameter.
5912292SN/A            wb->schedule(curTick);
5922678Sktlim@umich.edu
5932727Sktlim@umich.edu            ++lsqForwLoads;
5942292SN/A            return NoFault;
5952292SN/A        } else if ((store_has_lower_limit && lower_load_has_store_part) ||
5962292SN/A                   (store_has_upper_limit && upper_load_has_store_part) ||
5972292SN/A                   (lower_load_has_store_part && upper_load_has_store_part)) {
5982292SN/A            // This is the partial store-load forwarding case where a store
5992292SN/A            // has only part of the load's data.
6002292SN/A
6012292SN/A            // If it's already been written back, then don't worry about
6022292SN/A            // stalling on it.
6032292SN/A            if (storeQueue[store_idx].completed) {
6044032Sktlim@umich.edu                panic("Should not check one of these");
6052292SN/A                continue;
6062292SN/A            }
6072292SN/A
6082292SN/A            // Must stall load and force it to retry, so long as it's the oldest
6092292SN/A            // load that needs to do so.
6102292SN/A            if (!stalled ||
6112292SN/A                (stalled &&
6122669Sktlim@umich.edu                 load_inst->seqNum <
6132292SN/A                 loadQueue[stallingLoadIdx]->seqNum)) {
6142292SN/A                stalled = true;
6152292SN/A                stallingStoreIsn = storeQueue[store_idx].inst->seqNum;
6162292SN/A                stallingLoadIdx = load_idx;
6172292SN/A            }
6182292SN/A
6192292SN/A            // Tell IQ/mem dep unit that this instruction will need to be
6202292SN/A            // rescheduled eventually
6212669Sktlim@umich.edu            iewStage->rescheduleMemInst(load_inst);
6222927Sktlim@umich.edu            iewStage->decrWb(load_inst->seqNum);
6234032Sktlim@umich.edu            load_inst->clearIssued();
6242727Sktlim@umich.edu            ++lsqRescheduledLoads;
6252292SN/A
6262292SN/A            // Do not generate a writeback event as this instruction is not
6272292SN/A            // complete.
6282292SN/A            DPRINTF(LSQUnit, "Load-store forwarding mis-match. "
6292292SN/A                    "Store idx %i to load addr %#x\n",
6302669Sktlim@umich.edu                    store_idx, req->getVaddr());
6312292SN/A
6324032Sktlim@umich.edu            // Must delete request now that it wasn't handed off to
6334032Sktlim@umich.edu            // memory.  This is quite ugly.  @todo: Figure out the
6344032Sktlim@umich.edu            // proper place to really handle request deletes.
6354032Sktlim@umich.edu            delete req;
6364032Sktlim@umich.edu
6372292SN/A            return NoFault;
6382292SN/A        }
6392292SN/A    }
6402292SN/A
6412292SN/A    // If there's no forwarding case, then go access memory
6422907Sktlim@umich.edu    DPRINTF(LSQUnit, "Doing memory access for inst [sn:%lli] PC %#x\n",
6432669Sktlim@umich.edu            load_inst->seqNum, load_inst->readPC());
6442292SN/A
6452669Sktlim@umich.edu    assert(!load_inst->memData);
6462669Sktlim@umich.edu    load_inst->memData = new uint8_t[64];
6472292SN/A
6482292SN/A    ++usedPorts;
6492292SN/A
6502907Sktlim@umich.edu    // if we the cache is not blocked, do cache access
6512907Sktlim@umich.edu    if (!lsq->cacheBlocked()) {
6523228Sktlim@umich.edu        PacketPtr data_pkt =
6534022Sstever@eecs.umich.edu            new Packet(req, MemCmd::ReadReq, Packet::Broadcast);
6543228Sktlim@umich.edu        data_pkt->dataStatic(load_inst->memData);
6553228Sktlim@umich.edu
6563228Sktlim@umich.edu        LSQSenderState *state = new LSQSenderState;
6573228Sktlim@umich.edu        state->isLoad = true;
6583228Sktlim@umich.edu        state->idx = load_idx;
6593228Sktlim@umich.edu        state->inst = load_inst;
6603228Sktlim@umich.edu        data_pkt->senderState = state;
6613228Sktlim@umich.edu
6622907Sktlim@umich.edu        if (!dcachePort->sendTiming(data_pkt)) {
6633228Sktlim@umich.edu            Packet::Result result = data_pkt->result;
6643228Sktlim@umich.edu
6653228Sktlim@umich.edu            // Delete state and data packet because a load retry
6663228Sktlim@umich.edu            // initiates a pipeline restart; it does not retry.
6673228Sktlim@umich.edu            delete state;
6684032Sktlim@umich.edu            delete data_pkt->req;
6693228Sktlim@umich.edu            delete data_pkt;
6703228Sktlim@umich.edu
6714032Sktlim@umich.edu            req = NULL;
6724032Sktlim@umich.edu
6733228Sktlim@umich.edu            if (result == Packet::BadAddress) {
6743221Sktlim@umich.edu                return TheISA::genMachineCheckFault();
6753221Sktlim@umich.edu            }
6763221Sktlim@umich.edu
6772907Sktlim@umich.edu            // If the access didn't succeed, tell the LSQ by setting
6782907Sktlim@umich.edu            // the retry thread id.
6792907Sktlim@umich.edu            lsq->setRetryTid(lsqID);
6802907Sktlim@umich.edu        }
6812907Sktlim@umich.edu    }
6822907Sktlim@umich.edu
6832907Sktlim@umich.edu    // If the cache was blocked, or has become blocked due to the access,
6842907Sktlim@umich.edu    // handle it.
6852907Sktlim@umich.edu    if (lsq->cacheBlocked()) {
6864032Sktlim@umich.edu        if (req)
6874032Sktlim@umich.edu            delete req;
6884032Sktlim@umich.edu
6892727Sktlim@umich.edu        ++lsqCacheBlocked;
6903014Srdreslin@umich.edu
6913014Srdreslin@umich.edu        iewStage->decrWb(load_inst->seqNum);
6922669Sktlim@umich.edu        // There's an older load that's already going to squash.
6932669Sktlim@umich.edu        if (isLoadBlocked && blockedLoadSeqNum < load_inst->seqNum)
6942669Sktlim@umich.edu            return NoFault;
6952292SN/A
6962669Sktlim@umich.edu        // Record that the load was blocked due to memory.  This
6972669Sktlim@umich.edu        // load will squash all instructions after it, be
6982669Sktlim@umich.edu        // refetched, and re-executed.
6992669Sktlim@umich.edu        isLoadBlocked = true;
7002669Sktlim@umich.edu        loadBlockedHandled = false;
7012669Sktlim@umich.edu        blockedLoadSeqNum = load_inst->seqNum;
7022669Sktlim@umich.edu        // No fault occurred, even though the interface is blocked.
7032669Sktlim@umich.edu        return NoFault;
7042292SN/A    }
7052292SN/A
7062669Sktlim@umich.edu    return NoFault;
7072292SN/A}
7082292SN/A
7092292SN/Atemplate <class Impl>
7102292SN/Atemplate <class T>
7112292SN/AFault
7122669Sktlim@umich.eduLSQUnit<Impl>::write(Request *req, T &data, int store_idx)
7132292SN/A{
7142292SN/A    assert(storeQueue[store_idx].inst);
7152292SN/A
7162292SN/A    DPRINTF(LSQUnit, "Doing write to store idx %i, addr %#x data %#x"
7172292SN/A            " | storeHead:%i [sn:%i]\n",
7182669Sktlim@umich.edu            store_idx, req->getPaddr(), data, storeHead,
7192292SN/A            storeQueue[store_idx].inst->seqNum);
7202329SN/A
7212292SN/A    storeQueue[store_idx].req = req;
7222292SN/A    storeQueue[store_idx].size = sizeof(T);
7234326Sgblack@eecs.umich.edu    assert(sizeof(T) <= sizeof(storeQueue[store_idx].data));
7244326Sgblack@eecs.umich.edu
7254326Sgblack@eecs.umich.edu    T gData = htog(data);
7264326Sgblack@eecs.umich.edu    memcpy(storeQueue[store_idx].data, &gData, sizeof(T));
7272329SN/A
7282292SN/A    // This function only writes the data to the store queue, so no fault
7292292SN/A    // can happen here.
7302292SN/A    return NoFault;
7312292SN/A}
7322292SN/A
7332292SN/A#endif // __CPU_O3_LSQ_UNIT_HH__
734