commit.hh revision 2863
11689SN/A/*
22316SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
292756Sksewell@umich.edu *          Korey Sewell
301689SN/A */
311689SN/A
322292SN/A#ifndef __CPU_O3_COMMIT_HH__
332292SN/A#define __CPU_O3_COMMIT_HH__
341060SN/A
352292SN/A#include "arch/faults.hh"
361461SN/A#include "base/statistics.hh"
371060SN/A#include "base/timebuf.hh"
382292SN/A#include "cpu/exetrace.hh"
392329SN/A#include "cpu/inst_seq.hh"
401060SN/A
412292SN/Atemplate <class>
422292SN/Aclass O3ThreadState;
432292SN/A
442292SN/A/**
452316SN/A * DefaultCommit handles single threaded and SMT commit. Its width is
462316SN/A * specified by the parameters; each cycle it tries to commit that
472316SN/A * many instructions. The SMT policy decides which thread it tries to
482316SN/A * commit instructions from. Non- speculative instructions must reach
492316SN/A * the head of the ROB before they are ready to execute; once they
502316SN/A * reach the head, commit will broadcast the instruction's sequence
512316SN/A * number to the previous stages so that they can issue/ execute the
522316SN/A * instruction. Only one non-speculative instruction is handled per
532316SN/A * cycle. Commit is responsible for handling all back-end initiated
542316SN/A * redirects.  It receives the redirect, and then broadcasts it to all
552316SN/A * stages, indicating the sequence number they should squash until,
562316SN/A * and any necessary branch misprediction information as well. It
572316SN/A * priortizes redirects by instruction's age, only broadcasting a
582316SN/A * redirect if it corresponds to an instruction that should currently
592316SN/A * be in the ROB. This is done by tracking the sequence number of the
602316SN/A * youngest instruction in the ROB, which gets updated to any
612316SN/A * squashing instruction's sequence number, and only broadcasting a
622316SN/A * redirect if it corresponds to an older instruction. Commit also
632316SN/A * supports multiple cycle squashing, to model a ROB that can only
642329SN/A * remove a certain number of instructions per cycle.
652292SN/A */
661060SN/Atemplate<class Impl>
672292SN/Aclass DefaultCommit
681060SN/A{
691060SN/A  public:
701060SN/A    // Typedefs from the Impl.
712733Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
721061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
731060SN/A    typedef typename Impl::Params Params;
741061SN/A    typedef typename Impl::CPUPol CPUPol;
751060SN/A
762292SN/A    typedef typename CPUPol::RenameMap RenameMap;
771061SN/A    typedef typename CPUPol::ROB ROB;
781060SN/A
791061SN/A    typedef typename CPUPol::TimeStruct TimeStruct;
802292SN/A    typedef typename CPUPol::FetchStruct FetchStruct;
811061SN/A    typedef typename CPUPol::IEWStruct IEWStruct;
821061SN/A    typedef typename CPUPol::RenameStruct RenameStruct;
831060SN/A
842316SN/A    typedef typename CPUPol::Fetch Fetch;
852292SN/A    typedef typename CPUPol::IEW IEW;
862292SN/A
872292SN/A    typedef O3ThreadState<Impl> Thread;
882292SN/A
892348SN/A    /** Event class used to schedule a squash due to a trap (fault or
902348SN/A     * interrupt) to happen on a specific cycle.
912348SN/A     */
922292SN/A    class TrapEvent : public Event {
932292SN/A      private:
942292SN/A        DefaultCommit<Impl> *commit;
952292SN/A        unsigned tid;
962292SN/A
972292SN/A      public:
982292SN/A        TrapEvent(DefaultCommit<Impl> *_commit, unsigned _tid);
992292SN/A
1002292SN/A        void process();
1012292SN/A        const char *description();
1022292SN/A    };
1032292SN/A
1042292SN/A    /** Overall commit status. Used to determine if the CPU can deschedule
1052292SN/A     * itself due to a lack of activity.
1062292SN/A     */
1072292SN/A    enum CommitStatus{
1082292SN/A        Active,
1092292SN/A        Inactive
1102292SN/A    };
1112292SN/A
1122292SN/A    /** Individual thread status. */
1132292SN/A    enum ThreadStatus {
1141060SN/A        Running,
1151060SN/A        Idle,
1161060SN/A        ROBSquashing,
1172292SN/A        TrapPending,
1182292SN/A        FetchTrapPending
1192292SN/A    };
1202292SN/A
1212292SN/A    /** Commit policy for SMT mode. */
1222292SN/A    enum CommitPolicy {
1232292SN/A        Aggressive,
1242292SN/A        RoundRobin,
1252292SN/A        OldestReady
1261060SN/A    };
1271060SN/A
1281060SN/A  private:
1292292SN/A    /** Overall commit status. */
1302292SN/A    CommitStatus _status;
1312292SN/A    /** Next commit status, to be set at the end of the cycle. */
1322292SN/A    CommitStatus _nextStatus;
1332292SN/A    /** Per-thread status. */
1342292SN/A    ThreadStatus commitStatus[Impl::MaxThreads];
1352292SN/A    /** Commit policy used in SMT mode. */
1362292SN/A    CommitPolicy commitPolicy;
1371060SN/A
1381060SN/A  public:
1392292SN/A    /** Construct a DefaultCommit with the given parameters. */
1402292SN/A    DefaultCommit(Params *params);
1411060SN/A
1422292SN/A    /** Returns the name of the DefaultCommit. */
1432292SN/A    std::string name() const;
1442292SN/A
1452292SN/A    /** Registers statistics. */
1461062SN/A    void regStats();
1471062SN/A
1482292SN/A    /** Sets the CPU pointer. */
1492733Sktlim@umich.edu    void setCPU(O3CPU *cpu_ptr);
1501060SN/A
1512292SN/A    /** Sets the list of threads. */
1522292SN/A    void setThreads(std::vector<Thread *> &threads);
1532292SN/A
1542292SN/A    /** Sets the main time buffer pointer, used for backwards communication. */
1551060SN/A    void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
1561060SN/A
1572292SN/A    void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
1582292SN/A
1592292SN/A    /** Sets the pointer to the queue coming from rename. */
1601060SN/A    void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
1611060SN/A
1622292SN/A    /** Sets the pointer to the queue coming from IEW. */
1631060SN/A    void setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr);
1641060SN/A
1652316SN/A    void setFetchStage(Fetch *fetch_stage);
1662316SN/A
1672316SN/A    Fetch *fetchStage;
1682316SN/A
1692348SN/A    /** Sets the pointer to the IEW stage. */
1702292SN/A    void setIEWStage(IEW *iew_stage);
1712292SN/A
1722316SN/A    /** The pointer to the IEW stage. Used solely to ensure that
1732316SN/A     * various events (traps, interrupts, syscalls) do not occur until
1742316SN/A     * all stores have written back.
1752292SN/A     */
1762292SN/A    IEW *iewStage;
1772292SN/A
1782292SN/A    /** Sets pointer to list of active threads. */
1792292SN/A    void setActiveThreads(std::list<unsigned> *at_ptr);
1802292SN/A
1812292SN/A    /** Sets pointer to the commited state rename map. */
1822292SN/A    void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]);
1832292SN/A
1842292SN/A    /** Sets pointer to the ROB. */
1851060SN/A    void setROB(ROB *rob_ptr);
1861060SN/A
1872292SN/A    /** Initializes stage by sending back the number of free entries. */
1882292SN/A    void initStage();
1892292SN/A
1902843Sktlim@umich.edu    /** Initializes the draining of commit. */
1912863Sktlim@umich.edu    bool drain();
1922843Sktlim@umich.edu
1932843Sktlim@umich.edu    /** Resumes execution after draining. */
1942843Sktlim@umich.edu    void resume();
1952307SN/A
1962348SN/A    /** Completes the switch out of commit. */
1972843Sktlim@umich.edu    void switchOut();
1982316SN/A
1992348SN/A    /** Takes over from another CPU's thread. */
2002307SN/A    void takeOverFrom();
2012307SN/A
2022292SN/A    /** Ticks the commit stage, which tries to commit instructions. */
2031060SN/A    void tick();
2041060SN/A
2052292SN/A    /** Handles any squashes that are sent from IEW, and adds instructions
2062292SN/A     * to the ROB and tries to commit instructions.
2072292SN/A     */
2081060SN/A    void commit();
2091060SN/A
2102292SN/A    /** Returns the number of free ROB entries for a specific thread. */
2112292SN/A    unsigned numROBFreeEntries(unsigned tid);
2122292SN/A
2132348SN/A    /** Generates an event to schedule a squash due to a trap. */
2142348SN/A    void generateTrapEvent(unsigned tid);
2152348SN/A
2162348SN/A    /** Records that commit needs to initiate a squash due to an
2172680Sktlim@umich.edu     * external state update through the TC.
2182348SN/A     */
2192680Sktlim@umich.edu    void generateTCEvent(unsigned tid);
2202292SN/A
2211060SN/A  private:
2222292SN/A    /** Updates the overall status of commit with the nextStatus, and
2232348SN/A     * tell the CPU if commit is active/inactive.
2242348SN/A     */
2252292SN/A    void updateStatus();
2261060SN/A
2272292SN/A    /** Sets the next status based on threads' statuses, which becomes the
2282292SN/A     * current status at the end of the cycle.
2292292SN/A     */
2302292SN/A    void setNextStatus();
2312292SN/A
2322292SN/A    /** Checks if the ROB is completed with squashing. This is for the case
2332292SN/A     * where the ROB can take multiple cycles to complete squashing.
2342292SN/A     */
2352292SN/A    bool robDoneSquashing();
2362292SN/A
2372292SN/A    /** Returns if any of the threads have the number of ROB entries changed
2382292SN/A     * on this cycle. Used to determine if the number of free ROB entries needs
2392292SN/A     * to be sent back to previous stages.
2402292SN/A     */
2412292SN/A    bool changedROBEntries();
2422292SN/A
2432348SN/A    /** Squashes all in flight instructions. */
2442316SN/A    void squashAll(unsigned tid);
2452316SN/A
2462348SN/A    /** Handles squashing due to a trap. */
2472292SN/A    void squashFromTrap(unsigned tid);
2482292SN/A
2492680Sktlim@umich.edu    /** Handles squashing due to an TC write. */
2502680Sktlim@umich.edu    void squashFromTC(unsigned tid);
2512292SN/A
2522292SN/A    /** Commits as many instructions as possible. */
2531060SN/A    void commitInsts();
2541060SN/A
2552292SN/A    /** Tries to commit the head ROB instruction passed in.
2562292SN/A     * @param head_inst The instruction to be committed.
2572292SN/A     */
2581061SN/A    bool commitHead(DynInstPtr &head_inst, unsigned inst_num);
2591060SN/A
2602292SN/A    /** Gets instructions from rename and inserts them into the ROB. */
2611060SN/A    void getInsts();
2621060SN/A
2632292SN/A    /** Marks completed instructions using information sent from IEW. */
2641060SN/A    void markCompletedInsts();
2651060SN/A
2662292SN/A    /** Gets the thread to commit, based on the SMT policy. */
2672292SN/A    int getCommittingThread();
2682292SN/A
2692292SN/A    /** Returns the thread ID to use based on a round robin policy. */
2702292SN/A    int roundRobin();
2712292SN/A
2722292SN/A    /** Returns the thread ID to use based on an oldest instruction policy. */
2732292SN/A    int oldestReady();
2742292SN/A
2751684SN/A  public:
2762316SN/A    /** Returns the PC of the head instruction of the ROB.
2772316SN/A     * @todo: Probably remove this function as it returns only thread 0.
2782316SN/A     */
2792316SN/A    uint64_t readPC() { return PC[0]; }
2801684SN/A
2812348SN/A    /** Returns the PC of a specific thread. */
2822292SN/A    uint64_t readPC(unsigned tid) { return PC[tid]; }
2832292SN/A
2842348SN/A    /** Sets the PC of a specific thread. */
2852292SN/A    void setPC(uint64_t val, unsigned tid) { PC[tid] = val; }
2862292SN/A
2872756Sksewell@umich.edu    /** Reads the next PC of a specific thread. */
2882292SN/A    uint64_t readNextPC(unsigned tid) { return nextPC[tid]; }
2892292SN/A
2902348SN/A    /** Sets the next PC of a specific thread. */
2912292SN/A    void setNextPC(uint64_t val, unsigned tid) { nextPC[tid] = val; }
2921684SN/A
2932756Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
2942756Sksewell@umich.edu    /** Reads the next NPC of a specific thread. */
2952756Sksewell@umich.edu    uint64_t readNextPC(unsigned tid) { return nextNPC[tid]; }
2962756Sksewell@umich.edu
2972756Sksewell@umich.edu    /** Sets the next NPC of a specific thread. */
2982756Sksewell@umich.edu    void setNextPC(uint64_t val, unsigned tid) { nextNPC[tid] = val; }
2992756Sksewell@umich.edu#endif
3002756Sksewell@umich.edu
3011684SN/A  private:
3021060SN/A    /** Time buffer interface. */
3031060SN/A    TimeBuffer<TimeStruct> *timeBuffer;
3041060SN/A
3051060SN/A    /** Wire to write information heading to previous stages. */
3061060SN/A    typename TimeBuffer<TimeStruct>::wire toIEW;
3071060SN/A
3081060SN/A    /** Wire to read information from IEW (for ROB). */
3091060SN/A    typename TimeBuffer<TimeStruct>::wire robInfoFromIEW;
3101060SN/A
3112292SN/A    TimeBuffer<FetchStruct> *fetchQueue;
3122292SN/A
3132292SN/A    typename TimeBuffer<FetchStruct>::wire fromFetch;
3142292SN/A
3151060SN/A    /** IEW instruction queue interface. */
3161060SN/A    TimeBuffer<IEWStruct> *iewQueue;
3171060SN/A
3181060SN/A    /** Wire to read information from IEW queue. */
3191060SN/A    typename TimeBuffer<IEWStruct>::wire fromIEW;
3201060SN/A
3211060SN/A    /** Rename instruction queue interface, for ROB. */
3221060SN/A    TimeBuffer<RenameStruct> *renameQueue;
3231060SN/A
3241060SN/A    /** Wire to read information from rename queue. */
3251060SN/A    typename TimeBuffer<RenameStruct>::wire fromRename;
3261060SN/A
3272292SN/A  public:
3281060SN/A    /** ROB interface. */
3291060SN/A    ROB *rob;
3301060SN/A
3312292SN/A  private:
3322733Sktlim@umich.edu    /** Pointer to O3CPU. */
3332733Sktlim@umich.edu    O3CPU *cpu;
3341060SN/A
3352348SN/A    /** Vector of all of the threads. */
3362292SN/A    std::vector<Thread *> thread;
3371060SN/A
3382292SN/A    Fault fetchFault;
3392316SN/A
3402292SN/A    int fetchTrapWait;
3412316SN/A
3422292SN/A    /** Records that commit has written to the time buffer this cycle. Used for
3432292SN/A     * the CPU to determine if it can deschedule itself if there is no activity.
3442292SN/A     */
3452292SN/A    bool wroteToTimeBuffer;
3462292SN/A
3472292SN/A    /** Records if the number of ROB entries has changed this cycle. If it has,
3482292SN/A     * then the number of free entries must be re-broadcast.
3492292SN/A     */
3502292SN/A    bool changedROBNumEntries[Impl::MaxThreads];
3512292SN/A
3522292SN/A    /** A counter of how many threads are currently squashing. */
3532292SN/A    int squashCounter;
3542292SN/A
3552292SN/A    /** Records if a thread has to squash this cycle due to a trap. */
3562292SN/A    bool trapSquash[Impl::MaxThreads];
3572292SN/A
3582292SN/A    /** Records if a thread has to squash this cycle due to an XC write. */
3592680Sktlim@umich.edu    bool tcSquash[Impl::MaxThreads];
3602292SN/A
3612292SN/A    /** Priority List used for Commit Policy */
3622292SN/A    std::list<unsigned> priority_list;
3632292SN/A
3641060SN/A    /** IEW to Commit delay, in ticks. */
3651060SN/A    unsigned iewToCommitDelay;
3661060SN/A
3672292SN/A    /** Commit to IEW delay, in ticks. */
3682292SN/A    unsigned commitToIEWDelay;
3692292SN/A
3701060SN/A    /** Rename to ROB delay, in ticks. */
3711060SN/A    unsigned renameToROBDelay;
3721060SN/A
3732292SN/A    unsigned fetchToCommitDelay;
3742292SN/A
3751060SN/A    /** Rename width, in instructions.  Used so ROB knows how many
3761060SN/A     *  instructions to get from the rename instruction queue.
3771060SN/A     */
3781060SN/A    unsigned renameWidth;
3791060SN/A
3801060SN/A    /** Commit width, in instructions. */
3811060SN/A    unsigned commitWidth;
3821062SN/A
3832292SN/A    /** Number of Reorder Buffers */
3842292SN/A    unsigned numRobs;
3852292SN/A
3862292SN/A    /** Number of Active Threads */
3872292SN/A    unsigned numThreads;
3882292SN/A
3892843Sktlim@umich.edu    /** Is a drain pending. */
3902843Sktlim@umich.edu    bool drainPending;
3912348SN/A
3922348SN/A    /** Is commit switched out. */
3932307SN/A    bool switchedOut;
3942307SN/A
3952348SN/A    /** The latency to handle a trap.  Used when scheduling trap
3962348SN/A     * squash event.
3972348SN/A     */
3982292SN/A    Tick trapLatency;
3992292SN/A
4002292SN/A    Tick fetchTrapLatency;
4012316SN/A
4022292SN/A    Tick fetchFaultTick;
4032292SN/A
4042348SN/A    /** The commit PC of each thread.  Refers to the instruction that
4052348SN/A     * is currently being processed/committed.
4062348SN/A     */
4072292SN/A    Addr PC[Impl::MaxThreads];
4082292SN/A
4092348SN/A    /** The next PC of each thread. */
4102292SN/A    Addr nextPC[Impl::MaxThreads];
4112292SN/A
4122831Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
4132756Sksewell@umich.edu    /** The next NPC of each thread. */
4142756Sksewell@umich.edu    Addr nextNPC[Impl::MaxThreads];
4152831Sksewell@umich.edu#endif
4162756Sksewell@umich.edu
4172292SN/A    /** The sequence number of the youngest valid instruction in the ROB. */
4182292SN/A    InstSeqNum youngestSeqNum[Impl::MaxThreads];
4192292SN/A
4202292SN/A    /** Pointer to the list of active threads. */
4212292SN/A    std::list<unsigned> *activeThreads;
4222292SN/A
4232292SN/A    /** Rename map interface. */
4242292SN/A    RenameMap *renameMap[Impl::MaxThreads];
4252292SN/A
4262348SN/A    /** Updates commit stats based on this instruction. */
4272301SN/A    void updateComInstStats(DynInstPtr &inst);
4282301SN/A
4292292SN/A    /** Stat for the total number of committed instructions. */
4301062SN/A    Stats::Scalar<> commitCommittedInsts;
4312292SN/A    /** Stat for the total number of squashed instructions discarded by commit.
4322292SN/A     */
4331062SN/A    Stats::Scalar<> commitSquashedInsts;
4342292SN/A    /** Stat for the total number of times commit is told to squash.
4352292SN/A     * @todo: Actually increment this stat.
4362292SN/A     */
4371062SN/A    Stats::Scalar<> commitSquashEvents;
4382292SN/A    /** Stat for the total number of times commit has had to stall due to a non-
4392292SN/A     * speculative instruction reaching the head of the ROB.
4402292SN/A     */
4411062SN/A    Stats::Scalar<> commitNonSpecStalls;
4422292SN/A    /** Stat for the total number of branch mispredicts that caused a squash. */
4431062SN/A    Stats::Scalar<> branchMispredicts;
4442292SN/A    /** Distribution of the number of committed instructions each cycle. */
4452292SN/A    Stats::Distribution<> numCommittedDist;
4461062SN/A
4472316SN/A    /** Total number of instructions committed. */
4482316SN/A    Stats::Vector<> statComInst;
4492316SN/A    /** Total number of software prefetches committed. */
4502316SN/A    Stats::Vector<> statComSwp;
4512316SN/A    /** Stat for the total number of committed memory references. */
4522316SN/A    Stats::Vector<> statComRefs;
4532316SN/A    /** Stat for the total number of committed loads. */
4542316SN/A    Stats::Vector<> statComLoads;
4552316SN/A    /** Total number of committed memory barriers. */
4562316SN/A    Stats::Vector<> statComMembars;
4572316SN/A    /** Total number of committed branches. */
4582316SN/A    Stats::Vector<> statComBranches;
4592301SN/A
4602348SN/A    /** Number of cycles where the commit bandwidth limit is reached. */
4612316SN/A    Stats::Scalar<> commitEligibleSamples;
4622348SN/A    /** Number of instructions not committed due to bandwidth limits. */
4632316SN/A    Stats::Vector<> commitEligible;
4641060SN/A};
4651060SN/A
4662292SN/A#endif // __CPU_O3_COMMIT_HH__
467