lsq.hh revision 5714:76abee886def
14123Sbinkertn@umich.edu/*
24123Sbinkertn@umich.edu * Copyright (c) 2004-2006 The Regents of The University of Michigan
39983Sstever@gmail.com * All rights reserved.
49983Sstever@gmail.com *
54123Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64123Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74123Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84123Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94123Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104123Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114123Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124123Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134123Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144123Sbinkertn@umich.edu * this software without specific prior written permission.
154123Sbinkertn@umich.edu *
164123Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174123Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184123Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194123Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204123Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214123Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224123Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234123Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244123Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254123Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264123Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274123Sbinkertn@umich.edu *
284123Sbinkertn@umich.edu * Authors: Korey Sewell
294123Sbinkertn@umich.edu */
304123Sbinkertn@umich.edu
314123Sbinkertn@umich.edu#ifndef __CPU_O3_LSQ_HH__
324123Sbinkertn@umich.edu#define __CPU_O3_LSQ_HH__
334123Sbinkertn@umich.edu
3411793Sbrandon.potter@amd.com#include <map>
3511793Sbrandon.potter@amd.com#include <queue>
369983Sstever@gmail.com
379983Sstever@gmail.com#include "config/full_system.hh"
389983Sstever@gmail.com#include "cpu/inst_seq.hh"
394123Sbinkertn@umich.edu#include "cpu/o3/lsq_unit.hh"
404123Sbinkertn@umich.edu#include "mem/port.hh"
416216Snate@binkert.org#include "sim/sim_object.hh"
424123Sbinkertn@umich.edu
439356Snilay@cs.wisc.educlass DerivO3CPUParams;
444123Sbinkertn@umich.edu
454123Sbinkertn@umich.edutemplate <class Impl>
466216Snate@binkert.orgclass LSQ {
474123Sbinkertn@umich.edu  public:
489983Sstever@gmail.com    typedef typename Impl::O3CPU O3CPU;
499983Sstever@gmail.com    typedef typename Impl::DynInstPtr DynInstPtr;
509983Sstever@gmail.com    typedef typename Impl::CPUPol::IEW IEW;
519983Sstever@gmail.com    typedef typename Impl::CPUPol::LSQUnit LSQUnit;
529983Sstever@gmail.com
539983Sstever@gmail.com    /** SMT policy. */
549983Sstever@gmail.com    enum LSQPolicy {
559983Sstever@gmail.com        Dynamic,
569983Sstever@gmail.com        Partitioned,
579983Sstever@gmail.com        Threshold
589983Sstever@gmail.com    };
599983Sstever@gmail.com
609983Sstever@gmail.com    /** Constructs an LSQ with the given parameters. */
619983Sstever@gmail.com    LSQ(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params);
629983Sstever@gmail.com
639983Sstever@gmail.com    /** Returns the name of the LSQ. */
649983Sstever@gmail.com    std::string name() const;
659983Sstever@gmail.com
669983Sstever@gmail.com    /** Registers statistics of each LSQ unit. */
679983Sstever@gmail.com    void regStats();
689983Sstever@gmail.com
699983Sstever@gmail.com    /** Returns dcache port.
709983Sstever@gmail.com     *  @todo: Dcache port needs to be moved up to this level for SMT
719983Sstever@gmail.com     *  to work.  For now it just returns the port from one of the
729983Sstever@gmail.com     *  threads.
739983Sstever@gmail.com     */
749983Sstever@gmail.com    Port *getDcachePort() { return &dcachePort; }
7510762SCurtis.Dunham@arm.com
7610756SCurtis.Dunham@arm.com    /** Sets the pointer to the list of active threads. */
774123Sbinkertn@umich.edu    void setActiveThreads(std::list<unsigned> *at_ptr);
784123Sbinkertn@umich.edu    /** Switches out the LSQ. */
794123Sbinkertn@umich.edu    void switchOut();
804123Sbinkertn@umich.edu    /** Takes over execution from another CPU's thread. */
814123Sbinkertn@umich.edu    void takeOverFrom();
829983Sstever@gmail.com
834123Sbinkertn@umich.edu    /** Number of entries needed for the given amount of threads.*/
844123Sbinkertn@umich.edu    int entryAmount(int num_threads);
859983Sstever@gmail.com    void removeEntries(unsigned tid);
869983Sstever@gmail.com    /** Reset the max entries for each thread. */
879983Sstever@gmail.com    void resetEntries();
889983Sstever@gmail.com    /** Resize the max entries for a thread. */
899983Sstever@gmail.com    void resizeEntries(unsigned size, unsigned tid);
909983Sstever@gmail.com
919983Sstever@gmail.com    /** Ticks the LSQ. */
929983Sstever@gmail.com    void tick();
939983Sstever@gmail.com    /** Ticks a specific LSQ Unit. */
949983Sstever@gmail.com    void tick(unsigned tid)
959983Sstever@gmail.com    { thread[tid].tick(); }
969983Sstever@gmail.com
979983Sstever@gmail.com    /** Inserts a load into the LSQ. */
989983Sstever@gmail.com    void insertLoad(DynInstPtr &load_inst);
999983Sstever@gmail.com    /** Inserts a store into the LSQ. */
1009983Sstever@gmail.com    void insertStore(DynInstPtr &store_inst);
1019983Sstever@gmail.com
10210762SCurtis.Dunham@arm.com    /** Executes a load. */
10310762SCurtis.Dunham@arm.com    Fault executeLoad(DynInstPtr &inst);
10410762SCurtis.Dunham@arm.com
1059983Sstever@gmail.com    /** Executes a store. */
1069983Sstever@gmail.com    Fault executeStore(DynInstPtr &inst);
1077823Ssteve.reinhardt@amd.com
1084123Sbinkertn@umich.edu    /**
1099174Satgutier@umich.edu     * Commits loads up until the given sequence number for a specific thread.
1109174Satgutier@umich.edu     */
1119174Satgutier@umich.edu    void commitLoads(InstSeqNum &youngest_inst, unsigned tid)
1124123Sbinkertn@umich.edu    { thread[tid].commitLoads(youngest_inst); }
1134123Sbinkertn@umich.edu
11410762SCurtis.Dunham@arm.com    /**
1159983Sstever@gmail.com     * Commits stores up until the given sequence number for a specific thread.
1169983Sstever@gmail.com     */
1179983Sstever@gmail.com    void commitStores(InstSeqNum &youngest_inst, unsigned tid)
1189983Sstever@gmail.com    { thread[tid].commitStores(youngest_inst); }
1199983Sstever@gmail.com
1209983Sstever@gmail.com    /**
1219983Sstever@gmail.com     * Attempts to write back stores until all cache ports are used or the
12210101Sandreas@sandberg.pp.se     * interface becomes blocked.
1239983Sstever@gmail.com     */
1249983Sstever@gmail.com    void writebackStores();
1259983Sstever@gmail.com    /** Same as above, but only for one thread. */
1269983Sstever@gmail.com    void writebackStores(unsigned tid);
1279983Sstever@gmail.com
1289983Sstever@gmail.com    /**
1299983Sstever@gmail.com     * Squash instructions from a thread until the specified sequence number.
1309983Sstever@gmail.com     */
1319983Sstever@gmail.com    void squash(const InstSeqNum &squashed_num, unsigned tid)
1329983Sstever@gmail.com    { thread[tid].squash(squashed_num); }
1339983Sstever@gmail.com
1349983Sstever@gmail.com    /** Returns whether or not there was a memory ordering violation. */
1359983Sstever@gmail.com    bool violation();
1369983Sstever@gmail.com    /**
1379983Sstever@gmail.com     * Returns whether or not there was a memory ordering violation for a
1389983Sstever@gmail.com     * specific thread.
1399983Sstever@gmail.com     */
1409983Sstever@gmail.com    bool violation(unsigned tid)
1419983Sstever@gmail.com    { return thread[tid].violation(); }
1429983Sstever@gmail.com
1439983Sstever@gmail.com    /** Returns if a load is blocked due to the memory system for a specific
1449983Sstever@gmail.com     *  thread.
1459983Sstever@gmail.com     */
1469983Sstever@gmail.com    bool loadBlocked(unsigned tid)
1479983Sstever@gmail.com    { return thread[tid].loadBlocked(); }
1489983Sstever@gmail.com
1499983Sstever@gmail.com    bool isLoadBlockedHandled(unsigned tid)
1509983Sstever@gmail.com    { return thread[tid].isLoadBlockedHandled(); }
1519983Sstever@gmail.com
1529983Sstever@gmail.com    void setLoadBlockedHandled(unsigned tid)
1539983Sstever@gmail.com    { thread[tid].setLoadBlockedHandled(); }
1549983Sstever@gmail.com
1559983Sstever@gmail.com    /** Gets the instruction that caused the memory ordering violation. */
1569983Sstever@gmail.com    DynInstPtr getMemDepViolator(unsigned tid)
1579983Sstever@gmail.com    { return thread[tid].getMemDepViolator(); }
1589983Sstever@gmail.com
1599983Sstever@gmail.com    /** Returns the head index of the load queue for a specific thread. */
1609983Sstever@gmail.com    int getLoadHead(unsigned tid)
1619983Sstever@gmail.com    { return thread[tid].getLoadHead(); }
1629983Sstever@gmail.com
1639983Sstever@gmail.com    /** Returns the sequence number of the head of the load queue. */
1649983Sstever@gmail.com    InstSeqNum getLoadHeadSeqNum(unsigned tid)
1659983Sstever@gmail.com    {
1669983Sstever@gmail.com        return thread[tid].getLoadHeadSeqNum();
1679983Sstever@gmail.com    }
1689983Sstever@gmail.com
1699983Sstever@gmail.com    /** Returns the head index of the store queue. */
1709983Sstever@gmail.com    int getStoreHead(unsigned tid)
1719983Sstever@gmail.com    { return thread[tid].getStoreHead(); }
1729983Sstever@gmail.com
1739983Sstever@gmail.com    /** Returns the sequence number of the head of the store queue. */
1749983Sstever@gmail.com    InstSeqNum getStoreHeadSeqNum(unsigned tid)
1759983Sstever@gmail.com    {
1769983Sstever@gmail.com        return thread[tid].getStoreHeadSeqNum();
1779983Sstever@gmail.com    }
1789983Sstever@gmail.com
1799983Sstever@gmail.com    /** Returns the number of instructions in all of the queues. */
1809983Sstever@gmail.com    int getCount();
1819983Sstever@gmail.com    /** Returns the number of instructions in the queues of one thread. */
1829983Sstever@gmail.com    int getCount(unsigned tid)
1839983Sstever@gmail.com    { return thread[tid].getCount(); }
1849983Sstever@gmail.com
1854123Sbinkertn@umich.edu    /** Returns the total number of loads in the load queue. */
1864123Sbinkertn@umich.edu    int numLoads();
1874123Sbinkertn@umich.edu    /** Returns the total number of loads for a single thread. */
1884123Sbinkertn@umich.edu    int numLoads(unsigned tid)
1899983Sstever@gmail.com    { return thread[tid].numLoads(); }
1909983Sstever@gmail.com
1914123Sbinkertn@umich.edu    /** Returns the total number of stores in the store queue. */
1924123Sbinkertn@umich.edu    int numStores();
1939983Sstever@gmail.com    /** Returns the total number of stores for a single thread. */
19410153Sandreas@sandberg.pp.se    int numStores(unsigned tid)
19510153Sandreas@sandberg.pp.se    { return thread[tid].numStores(); }
19610153Sandreas@sandberg.pp.se
1974123Sbinkertn@umich.edu    /** Returns the total number of loads that are ready. */
1987822Ssteve.reinhardt@amd.com    int numLoadsReady();
1994123Sbinkertn@umich.edu    /** Returns the number of loads that are ready for a single thread. */
2004123Sbinkertn@umich.edu    int numLoadsReady(unsigned tid)
2014123Sbinkertn@umich.edu    { return thread[tid].numLoadsReady(); }
2024123Sbinkertn@umich.edu
20310670SCurtis.Dunham@arm.com    /** Returns the number of free entries. */
20410670SCurtis.Dunham@arm.com    unsigned numFreeEntries();
20510670SCurtis.Dunham@arm.com    /** Returns the number of free entries for a specific thread. */
20610670SCurtis.Dunham@arm.com    unsigned numFreeEntries(unsigned tid);
20710670SCurtis.Dunham@arm.com
2084123Sbinkertn@umich.edu    /** Returns if the LSQ is full (either LQ or SQ is full). */
2094123Sbinkertn@umich.edu    bool isFull();
2104123Sbinkertn@umich.edu    /**
2114123Sbinkertn@umich.edu     * Returns if the LSQ is full for a specific thread (either LQ or SQ is
2124123Sbinkertn@umich.edu     * full).
2134123Sbinkertn@umich.edu     */
2144123Sbinkertn@umich.edu    bool isFull(unsigned tid);
2154123Sbinkertn@umich.edu
2164123Sbinkertn@umich.edu    /** Returns if any of the LQs are full. */
2174123Sbinkertn@umich.edu    bool lqFull();
21810670SCurtis.Dunham@arm.com    /** Returns if the LQ of a given thread is full. */
21910670SCurtis.Dunham@arm.com    bool lqFull(unsigned tid);
22010670SCurtis.Dunham@arm.com
22110670SCurtis.Dunham@arm.com    /** Returns if any of the SQs are full. */
22210670SCurtis.Dunham@arm.com    bool sqFull();
2234123Sbinkertn@umich.edu    /** Returns if the SQ of a given thread is full. */
2244123Sbinkertn@umich.edu    bool sqFull(unsigned tid);
2254123Sbinkertn@umich.edu
2264123Sbinkertn@umich.edu    /**
227     * Returns if the LSQ is stalled due to a memory operation that must be
228     * replayed.
229     */
230    bool isStalled();
231    /**
232     * Returns if the LSQ of a specific thread is stalled due to a memory
233     * operation that must be replayed.
234     */
235    bool isStalled(unsigned tid);
236
237    /** Returns whether or not there are any stores to write back to memory. */
238    bool hasStoresToWB();
239
240    /** Returns whether or not a specific thread has any stores to write back
241     * to memory.
242     */
243    bool hasStoresToWB(unsigned tid)
244    { return thread[tid].hasStoresToWB(); }
245
246    /** Returns the number of stores a specific thread has to write back. */
247    int  numStoresToWB(unsigned tid)
248    { return thread[tid].numStoresToWB(); }
249
250    /** Returns if the LSQ will write back to memory this cycle. */
251    bool willWB();
252    /** Returns if the LSQ of a specific thread will write back to memory this
253     * cycle.
254     */
255    bool willWB(unsigned tid)
256    { return thread[tid].willWB(); }
257
258    /** Returns if the cache is currently blocked. */
259    bool cacheBlocked()
260    { return retryTid != -1; }
261
262    /** Sets the retry thread id, indicating that one of the LSQUnits
263     * tried to access the cache but the cache was blocked. */
264    void setRetryTid(int tid)
265    { retryTid = tid; }
266
267    /** Debugging function to print out all instructions. */
268    void dumpInsts();
269    /** Debugging function to print out instructions from a specific thread. */
270    void dumpInsts(unsigned tid)
271    { thread[tid].dumpInsts(); }
272
273    /** Executes a read operation, using the load specified at the load index. */
274    template <class T>
275    Fault read(RequestPtr req, T &data, int load_idx);
276
277    /** Executes a store operation, using the store specified at the store
278     *   index.
279     */
280    template <class T>
281    Fault write(RequestPtr req, T &data, int store_idx);
282
283    /** The CPU pointer. */
284    O3CPU *cpu;
285
286    /** The IEW stage pointer. */
287    IEW *iewStage;
288
289    /** DcachePort class for this LSQ.  Handles doing the
290     * communication with the cache/memory.
291     */
292    class DcachePort : public Port
293    {
294      protected:
295        /** Pointer to LSQ. */
296        LSQ *lsq;
297
298      public:
299        /** Default constructor. */
300        DcachePort(LSQ *_lsq)
301            : Port(_lsq->name() + "-dport", _lsq->cpu), lsq(_lsq)
302        { }
303
304        bool snoopRangeSent;
305
306        virtual void setPeer(Port *port);
307
308      protected:
309        /** Atomic version of receive.  Panics. */
310        virtual Tick recvAtomic(PacketPtr pkt);
311
312        /** Functional version of receive.  Panics. */
313        virtual void recvFunctional(PacketPtr pkt);
314
315        /** Receives status change.  Other than range changing, panics. */
316        virtual void recvStatusChange(Status status);
317
318        /** Returns the address ranges of this device. */
319        virtual void getDeviceAddressRanges(AddrRangeList &resp,
320                                            bool &snoop)
321        { resp.clear(); snoop = true; }
322
323        /** Timing version of receive.  Handles writing back and
324         * completing the load or store that has returned from
325         * memory. */
326        virtual bool recvTiming(PacketPtr pkt);
327
328        /** Handles doing a retry of the previous send. */
329        virtual void recvRetry();
330    };
331
332    /** D-cache port. */
333    DcachePort dcachePort;
334
335#if FULL_SYSTEM
336    /** Tell the CPU to update the Phys and Virt ports. */
337    void updateMemPorts() { cpu->updateMemPorts(); }
338#endif
339
340  protected:
341    /** The LSQ policy for SMT mode. */
342    LSQPolicy lsqPolicy;
343
344    /** The LSQ units for individual threads. */
345    LSQUnit thread[Impl::MaxThreads];
346
347    /** List of Active Threads in System. */
348    std::list<unsigned> *activeThreads;
349
350    /** Total Size of LQ Entries. */
351    unsigned LQEntries;
352    /** Total Size of SQ Entries. */
353    unsigned SQEntries;
354
355    /** Max LQ Size - Used to Enforce Sharing Policies. */
356    unsigned maxLQEntries;
357
358    /** Max SQ Size - Used to Enforce Sharing Policies. */
359    unsigned maxSQEntries;
360
361    /** Number of Threads. */
362    unsigned numThreads;
363
364    /** The thread id of the LSQ Unit that is currently waiting for a
365     * retry. */
366    int retryTid;
367};
368
369template <class Impl>
370template <class T>
371Fault
372LSQ<Impl>::read(RequestPtr req, T &data, int load_idx)
373{
374    unsigned tid = req->threadId();
375
376    return thread[tid].read(req, data, load_idx);
377}
378
379template <class Impl>
380template <class T>
381Fault
382LSQ<Impl>::write(RequestPtr req, T &data, int store_idx)
383{
384    unsigned tid = req->threadId();
385
386    return thread[tid].write(req, data, store_idx);
387}
388
389#endif // __CPU_O3_LSQ_HH__
390