fetch.hh revision 2756
11689SN/A/*
22329SN/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_FETCH_HH__
332292SN/A#define __CPU_O3_FETCH_HH__
341060SN/A
352669Sktlim@umich.edu#include "arch/utility.hh"
361461SN/A#include "base/statistics.hh"
371060SN/A#include "base/timebuf.hh"
381060SN/A#include "cpu/pc_event.hh"
392669Sktlim@umich.edu#include "mem/packet.hh"
402669Sktlim@umich.edu#include "mem/port.hh"
411461SN/A#include "sim/eventq.hh"
421060SN/A
432307SN/Aclass Sampler;
442307SN/A
451060SN/A/**
462329SN/A * DefaultFetch class handles both single threaded and SMT fetch. Its
472329SN/A * width is specified by the parameters; each cycle it tries to fetch
482329SN/A * that many instructions. It supports using a branch predictor to
492329SN/A * predict direction and targets.
502348SN/A * It supports the idling functionality of the CPU by indicating to
512329SN/A * the CPU when it is active and inactive.
521060SN/A */
531060SN/Atemplate <class Impl>
542292SN/Aclass DefaultFetch
551060SN/A{
561060SN/A  public:
571060SN/A    /** Typedefs from Impl. */
581061SN/A    typedef typename Impl::CPUPol CPUPol;
591060SN/A    typedef typename Impl::DynInst DynInst;
601061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
611060SN/A    typedef typename Impl::FullCPU FullCPU;
621060SN/A    typedef typename Impl::Params Params;
631060SN/A
642292SN/A    /** Typedefs from the CPU policy. */
651061SN/A    typedef typename CPUPol::BPredUnit BPredUnit;
661061SN/A    typedef typename CPUPol::FetchStruct FetchStruct;
671061SN/A    typedef typename CPUPol::TimeStruct TimeStruct;
681060SN/A
691060SN/A    /** Typedefs from ISA. */
702107SN/A    typedef TheISA::MachInst MachInst;
712292SN/A    typedef TheISA::ExtMachInst ExtMachInst;
722632Sstever@eecs.umich.edu
732698Sktlim@umich.edu    /** IcachePort class for DefaultFetch.  Handles doing the
742698Sktlim@umich.edu     * communication with the cache/memory.
752698Sktlim@umich.edu     */
762669Sktlim@umich.edu    class IcachePort : public Port
772669Sktlim@umich.edu    {
782669Sktlim@umich.edu      protected:
792698Sktlim@umich.edu        /** Pointer to fetch. */
802669Sktlim@umich.edu        DefaultFetch<Impl> *fetch;
812669Sktlim@umich.edu
822669Sktlim@umich.edu      public:
832698Sktlim@umich.edu        /** Default constructor. */
842669Sktlim@umich.edu        IcachePort(DefaultFetch<Impl> *_fetch)
852669Sktlim@umich.edu            : Port(_fetch->name() + "-iport"), fetch(_fetch)
862669Sktlim@umich.edu        { }
872669Sktlim@umich.edu
882669Sktlim@umich.edu      protected:
892698Sktlim@umich.edu        /** Atomic version of receive.  Panics. */
902669Sktlim@umich.edu        virtual Tick recvAtomic(PacketPtr pkt);
912669Sktlim@umich.edu
922698Sktlim@umich.edu        /** Functional version of receive.  Panics. */
932669Sktlim@umich.edu        virtual void recvFunctional(PacketPtr pkt);
942669Sktlim@umich.edu
952698Sktlim@umich.edu        /** Receives status change.  Other than range changing, panics. */
962669Sktlim@umich.edu        virtual void recvStatusChange(Status status);
972669Sktlim@umich.edu
982698Sktlim@umich.edu        /** Returns the address ranges of this device. */
992669Sktlim@umich.edu        virtual void getDeviceAddressRanges(AddrRangeList &resp,
1002669Sktlim@umich.edu                                            AddrRangeList &snoop)
1012669Sktlim@umich.edu        { resp.clear(); snoop.clear(); }
1022669Sktlim@umich.edu
1032698Sktlim@umich.edu        /** Timing version of receive.  Handles setting fetch to the
1042698Sktlim@umich.edu         * proper status to start fetching. */
1052669Sktlim@umich.edu        virtual bool recvTiming(PacketPtr pkt);
1062669Sktlim@umich.edu
1072698Sktlim@umich.edu        /** Handles doing a retry of a failed fetch. */
1082669Sktlim@umich.edu        virtual void recvRetry();
1092669Sktlim@umich.edu    };
1101060SN/A
1111060SN/A  public:
1122329SN/A    /** Overall fetch status. Used to determine if the CPU can
1132329SN/A     * deschedule itsef due to a lack of activity.
1142292SN/A     */
1152292SN/A    enum FetchStatus {
1162292SN/A        Active,
1172292SN/A        Inactive
1182292SN/A    };
1192292SN/A
1202292SN/A    /** Individual thread status. */
1212292SN/A    enum ThreadStatus {
1221060SN/A        Running,
1231060SN/A        Idle,
1241060SN/A        Squashing,
1251060SN/A        Blocked,
1262292SN/A        Fetching,
1272292SN/A        TrapPending,
1282292SN/A        QuiescePending,
1292307SN/A        SwitchOut,
1302669Sktlim@umich.edu        IcacheWaitResponse,
1312696Sktlim@umich.edu        IcacheWaitRetry,
1322669Sktlim@umich.edu        IcacheAccessComplete
1331060SN/A    };
1341060SN/A
1352292SN/A    /** Fetching Policy, Add new policies here.*/
1362292SN/A    enum FetchPriority {
1372292SN/A        SingleThread,
1382292SN/A        RoundRobin,
1392292SN/A        Branch,
1402292SN/A        IQ,
1412292SN/A        LSQ
1422292SN/A    };
1431060SN/A
1442292SN/A  private:
1452292SN/A    /** Fetch status. */
1462292SN/A    FetchStatus _status;
1472292SN/A
1482292SN/A    /** Per-thread status. */
1492292SN/A    ThreadStatus fetchStatus[Impl::MaxThreads];
1502292SN/A
1512292SN/A    /** Fetch policy. */
1522292SN/A    FetchPriority fetchPolicy;
1532292SN/A
1542292SN/A    /** List that has the threads organized by priority. */
1552292SN/A    std::list<unsigned> priorityList;
1561060SN/A
1571060SN/A  public:
1582292SN/A    /** DefaultFetch constructor. */
1592292SN/A    DefaultFetch(Params *params);
1601684SN/A
1612292SN/A    /** Returns the name of fetch. */
1622292SN/A    std::string name() const;
1631684SN/A
1642292SN/A    /** Registers statistics. */
1651062SN/A    void regStats();
1661062SN/A
1672292SN/A    /** Sets CPU pointer. */
1681060SN/A    void setCPU(FullCPU *cpu_ptr);
1691060SN/A
1702292SN/A    /** Sets the main backwards communication time buffer pointer. */
1711060SN/A    void setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer);
1721060SN/A
1732292SN/A    /** Sets pointer to list of active threads. */
1742292SN/A    void setActiveThreads(std::list<unsigned> *at_ptr);
1752292SN/A
1762292SN/A    /** Sets pointer to time buffer used to communicate to the next stage. */
1771060SN/A    void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
1781060SN/A
1792292SN/A    /** Initialize stage. */
1802292SN/A    void initStage();
1812292SN/A
1822292SN/A    /** Processes cache completion event. */
1832669Sktlim@umich.edu    void processCacheCompletion(PacketPtr pkt);
1842292SN/A
1852348SN/A    /** Begins the switch out of the fetch stage. */
1862307SN/A    void switchOut();
1872307SN/A
1882348SN/A    /** Completes the switch out of the fetch stage. */
1892316SN/A    void doSwitchOut();
1902316SN/A
1912348SN/A    /** Takes over from another CPU's thread. */
1922307SN/A    void takeOverFrom();
1932307SN/A
1942348SN/A    /** Checks if the fetch stage is switched out. */
1952307SN/A    bool isSwitchedOut() { return switchedOut; }
1962307SN/A
1972348SN/A    /** Tells fetch to wake up from a quiesce instruction. */
1982292SN/A    void wakeFromQuiesce();
1991060SN/A
2001061SN/A  private:
2012329SN/A    /** Changes the status of this stage to active, and indicates this
2022329SN/A     * to the CPU.
2032292SN/A     */
2042292SN/A    inline void switchToActive();
2052292SN/A
2062329SN/A    /** Changes the status of this stage to inactive, and indicates
2072329SN/A     * this to the CPU.
2082292SN/A     */
2092292SN/A    inline void switchToInactive();
2102292SN/A
2111061SN/A    /**
2121061SN/A     * Looks up in the branch predictor to see if the next PC should be
2131061SN/A     * either next PC+=MachInst or a branch target.
2141763SN/A     * @param next_PC Next PC variable passed in by reference.  It is
2151061SN/A     * expected to be set to the current PC; it will be updated with what
2161061SN/A     * the next PC will be.
2171061SN/A     * @return Whether or not a branch was predicted as taken.
2181061SN/A     */
2191062SN/A    bool lookupAndUpdateNextPC(DynInstPtr &inst, Addr &next_PC);
2201062SN/A
2211062SN/A    /**
2221062SN/A     * Fetches the cache line that contains fetch_PC.  Returns any
2231062SN/A     * fault that happened.  Puts the data into the class variable
2241062SN/A     * cacheData.
2251763SN/A     * @param fetch_PC The PC address that is being fetched from.
2262292SN/A     * @param ret_fault The fault reference that will be set to the result of
2272292SN/A     * the icache access.
2282292SN/A     * @param tid Thread id.
2291062SN/A     * @return Any fault that occured.
2301062SN/A     */
2312292SN/A    bool fetchCacheLine(Addr fetch_PC, Fault &ret_fault, unsigned tid);
2321062SN/A
2332292SN/A    /** Squashes a specific thread and resets the PC. */
2342292SN/A    inline void doSquash(const Addr &new_PC, unsigned tid);
2351684SN/A
2362292SN/A    /** Squashes a specific thread and resets the PC. Also tells the CPU to
2372292SN/A     * remove any instructions between fetch and decode that should be sqaushed.
2382292SN/A     */
2392292SN/A    void squashFromDecode(const Addr &new_PC, const InstSeqNum &seq_num,
2402292SN/A                          unsigned tid);
2412292SN/A
2422292SN/A    /** Checks if a thread is stalled. */
2432292SN/A    bool checkStall(unsigned tid) const;
2442292SN/A
2452292SN/A    /** Updates overall fetch stage status; to be called at the end of each
2462292SN/A     * cycle. */
2472292SN/A    FetchStatus updateFetchStatus();
2481684SN/A
2491684SN/A  public:
2502292SN/A    /** Squashes a specific thread and resets the PC. Also tells the CPU to
2512292SN/A     * remove any instructions that are not in the ROB. The source of this
2522292SN/A     * squash should be the commit stage.
2532292SN/A     */
2542292SN/A    void squash(const Addr &new_PC, unsigned tid);
2551684SN/A
2562292SN/A    /** Ticks the fetch stage, processing all inputs signals and fetching
2572292SN/A     * as many instructions as possible.
2582292SN/A     */
2591684SN/A    void tick();
2601684SN/A
2612292SN/A    /** Checks all input signals and updates the status as necessary.
2622292SN/A     *  @return: Returns if the status has changed due to input signals.
2632292SN/A     */
2642292SN/A    bool checkSignalsAndUpdate(unsigned tid);
2651684SN/A
2662292SN/A    /** Does the actual fetching of instructions and passing them on to the
2672292SN/A     * next stage.
2682292SN/A     * @param status_change fetch() sets this variable if there was a status
2692292SN/A     * change (ie switching to IcacheMissStall).
2702292SN/A     */
2712292SN/A    void fetch(bool &status_change);
2722292SN/A
2732292SN/A    /** Align a PC to the start of an I-cache block. */
2741062SN/A    Addr icacheBlockAlignPC(Addr addr)
2751062SN/A    {
2762107SN/A        addr = TheISA::realPCToFetchPC(addr);
2771062SN/A        return (addr & ~(cacheBlkMask));
2781062SN/A    }
2791061SN/A
2801060SN/A  private:
2812698Sktlim@umich.edu    /** Handles retrying the fetch access. */
2822696Sktlim@umich.edu    void recvRetry();
2832696Sktlim@umich.edu
2842292SN/A    /** Returns the appropriate thread to fetch, given the fetch policy. */
2852292SN/A    int getFetchingThread(FetchPriority &fetch_priority);
2862292SN/A
2872292SN/A    /** Returns the appropriate thread to fetch using a round robin policy. */
2882292SN/A    int roundRobin();
2892292SN/A
2902292SN/A    /** Returns the appropriate thread to fetch using the IQ count policy. */
2912292SN/A    int iqCount();
2922292SN/A
2932292SN/A    /** Returns the appropriate thread to fetch using the LSQ count policy. */
2942292SN/A    int lsqCount();
2952292SN/A
2962292SN/A    /** Returns the appropriate thread to fetch using the branch count policy. */
2972292SN/A    int branchCount();
2982292SN/A
2992292SN/A  private:
3001060SN/A    /** Pointer to the FullCPU. */
3011060SN/A    FullCPU *cpu;
3021060SN/A
3031060SN/A    /** Time buffer interface. */
3041060SN/A    TimeBuffer<TimeStruct> *timeBuffer;
3051060SN/A
3061060SN/A    /** Wire to get decode's information from backwards time buffer. */
3071060SN/A    typename TimeBuffer<TimeStruct>::wire fromDecode;
3081060SN/A
3091060SN/A    /** Wire to get rename's information from backwards time buffer. */
3101060SN/A    typename TimeBuffer<TimeStruct>::wire fromRename;
3111060SN/A
3121060SN/A    /** Wire to get iew's information from backwards time buffer. */
3131060SN/A    typename TimeBuffer<TimeStruct>::wire fromIEW;
3141060SN/A
3151060SN/A    /** Wire to get commit's information from backwards time buffer. */
3161060SN/A    typename TimeBuffer<TimeStruct>::wire fromCommit;
3171060SN/A
3181060SN/A    /** Internal fetch instruction queue. */
3191060SN/A    TimeBuffer<FetchStruct> *fetchQueue;
3201060SN/A
3211060SN/A    //Might be annoying how this name is different than the queue.
3221060SN/A    /** Wire used to write any information heading to decode. */
3231060SN/A    typename TimeBuffer<FetchStruct>::wire toDecode;
3241060SN/A
3252669Sktlim@umich.edu    MemObject *mem;
3262669Sktlim@umich.edu
3271060SN/A    /** Icache interface. */
3282669Sktlim@umich.edu    IcachePort *icachePort;
3291060SN/A
3301061SN/A    /** BPredUnit. */
3311061SN/A    BPredUnit branchPred;
3321061SN/A
3332348SN/A    /** Per-thread fetch PC. */
3342292SN/A    Addr PC[Impl::MaxThreads];
3352292SN/A
3362348SN/A    /** Per-thread next PC. */
3372292SN/A    Addr nextPC[Impl::MaxThreads];
3382292SN/A
3392756Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
3402756Sksewell@umich.edu    /** Per-thread next Next PC.
3412756Sksewell@umich.edu     *  This is not a real register but is used for
3422756Sksewell@umich.edu     *  architectures that use a branch-delay slot.
3432756Sksewell@umich.edu     *  (such as MIPS or Sparc)
3442756Sksewell@umich.edu     */
3452756Sksewell@umich.edu    Addr nextNPC[Impl::MaxThreads];
3462756Sksewell@umich.edu#endif
3472756Sksewell@umich.edu
3482678Sktlim@umich.edu    /** Memory request used to access cache. */
3492678Sktlim@umich.edu    RequestPtr memReq[Impl::MaxThreads];
3502292SN/A
3512292SN/A    /** Variable that tracks if fetch has written to the time buffer this
3522292SN/A     * cycle. Used to tell CPU if there is activity this cycle.
3532292SN/A     */
3542292SN/A    bool wroteToTimeBuffer;
3552292SN/A
3562292SN/A    /** Tracks how many instructions has been fetched this cycle. */
3572292SN/A    int numInst;
3582292SN/A
3592292SN/A    /** Source of possible stalls. */
3602292SN/A    struct Stalls {
3612292SN/A        bool decode;
3622292SN/A        bool rename;
3632292SN/A        bool iew;
3642292SN/A        bool commit;
3652292SN/A    };
3662292SN/A
3672292SN/A    /** Tracks which stages are telling fetch to stall. */
3682292SN/A    Stalls stalls[Impl::MaxThreads];
3691060SN/A
3701060SN/A    /** Decode to fetch delay, in ticks. */
3711060SN/A    unsigned decodeToFetchDelay;
3721060SN/A
3731060SN/A    /** Rename to fetch delay, in ticks. */
3741060SN/A    unsigned renameToFetchDelay;
3751060SN/A
3761060SN/A    /** IEW to fetch delay, in ticks. */
3771060SN/A    unsigned iewToFetchDelay;
3781060SN/A
3791060SN/A    /** Commit to fetch delay, in ticks. */
3801060SN/A    unsigned commitToFetchDelay;
3811060SN/A
3821060SN/A    /** The width of fetch in instructions. */
3831060SN/A    unsigned fetchWidth;
3841060SN/A
3852696Sktlim@umich.edu    /** Is the cache blocked?  If so no threads can access it. */
3862696Sktlim@umich.edu    bool cacheBlocked;
3872696Sktlim@umich.edu
3882696Sktlim@umich.edu    /** The packet that is waiting to be retried. */
3892696Sktlim@umich.edu    PacketPtr retryPkt;
3902696Sktlim@umich.edu
3912696Sktlim@umich.edu    /** The thread that is waiting on the cache to tell fetch to retry. */
3922696Sktlim@umich.edu    int retryTid;
3932696Sktlim@umich.edu
3941060SN/A    /** Cache block size. */
3951062SN/A    int cacheBlkSize;
3961060SN/A
3971060SN/A    /** Mask to get a cache block's address. */
3981062SN/A    Addr cacheBlkMask;
3991060SN/A
4001062SN/A    /** The cache line being fetched. */
4012292SN/A    uint8_t *cacheData[Impl::MaxThreads];
4021060SN/A
4031060SN/A    /** Size of instructions. */
4041060SN/A    int instSize;
4051060SN/A
4061060SN/A    /** Icache stall statistics. */
4072292SN/A    Counter lastIcacheStall[Impl::MaxThreads];
4081062SN/A
4092292SN/A    /** List of Active Threads */
4102292SN/A    std::list<unsigned> *activeThreads;
4112292SN/A
4122292SN/A    /** Number of threads. */
4132292SN/A    unsigned numThreads;
4142292SN/A
4152292SN/A    /** Number of threads that are actively fetching. */
4162292SN/A    unsigned numFetchingThreads;
4172292SN/A
4182292SN/A    /** Thread ID being fetched. */
4192292SN/A    int threadFetched;
4202292SN/A
4212348SN/A    /** Checks if there is an interrupt pending.  If there is, fetch
4222348SN/A     * must stop once it is not fetching PAL instructions.
4232348SN/A     */
4242292SN/A    bool interruptPending;
4252292SN/A
4262348SN/A    /** Records if fetch is switched out. */
4272307SN/A    bool switchedOut;
4282307SN/A
4292292SN/A    // @todo: Consider making these vectors and tracking on a per thread basis.
4302292SN/A    /** Stat for total number of cycles stalled due to an icache miss. */
4311062SN/A    Stats::Scalar<> icacheStallCycles;
4322292SN/A    /** Stat for total number of fetched instructions. */
4331062SN/A    Stats::Scalar<> fetchedInsts;
4342727Sktlim@umich.edu    /** Total number of fetched branches. */
4352301SN/A    Stats::Scalar<> fetchedBranches;
4362292SN/A    /** Stat for total number of predicted branches. */
4371062SN/A    Stats::Scalar<> predictedBranches;
4382292SN/A    /** Stat for total number of cycles spent fetching. */
4391062SN/A    Stats::Scalar<> fetchCycles;
4402292SN/A    /** Stat for total number of cycles spent squashing. */
4411062SN/A    Stats::Scalar<> fetchSquashCycles;
4422292SN/A    /** Stat for total number of cycles spent blocked due to other stages in
4432292SN/A     * the pipeline.
4442292SN/A     */
4452292SN/A    Stats::Scalar<> fetchIdleCycles;
4462348SN/A    /** Total number of cycles spent blocked. */
4471062SN/A    Stats::Scalar<> fetchBlockedCycles;
4482348SN/A    /** Total number of cycles spent in any other state. */
4492307SN/A    Stats::Scalar<> fetchMiscStallCycles;
4502292SN/A    /** Stat for total number of fetched cache lines. */
4511062SN/A    Stats::Scalar<> fetchedCacheLines;
4522348SN/A    /** Total number of outstanding icache accesses that were dropped
4532348SN/A     * due to a squash.
4542348SN/A     */
4552301SN/A    Stats::Scalar<> fetchIcacheSquashes;
4562292SN/A    /** Distribution of number of instructions fetched each cycle. */
4572292SN/A    Stats::Distribution<> fetchNisnDist;
4582348SN/A    /** Rate of how often fetch was idle. */
4592292SN/A    Stats::Formula idleRate;
4602348SN/A    /** Number of branch fetches per cycle. */
4612292SN/A    Stats::Formula branchRate;
4622348SN/A    /** Number of instruction fetched per cycle. */
4632292SN/A    Stats::Formula fetchRate;
4641060SN/A};
4651060SN/A
4662292SN/A#endif //__CPU_O3_FETCH_HH__
467