fetch.hh revision 2696
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 291689SN/A */ 301689SN/A 312292SN/A#ifndef __CPU_O3_FETCH_HH__ 322292SN/A#define __CPU_O3_FETCH_HH__ 331060SN/A 342669Sktlim@umich.edu#include "arch/utility.hh" 351461SN/A#include "base/statistics.hh" 361060SN/A#include "base/timebuf.hh" 371060SN/A#include "cpu/pc_event.hh" 382669Sktlim@umich.edu#include "mem/packet.hh" 392669Sktlim@umich.edu#include "mem/port.hh" 401461SN/A#include "sim/eventq.hh" 411060SN/A 422307SN/Aclass Sampler; 432307SN/A 441060SN/A/** 452329SN/A * DefaultFetch class handles both single threaded and SMT fetch. Its 462329SN/A * width is specified by the parameters; each cycle it tries to fetch 472329SN/A * that many instructions. It supports using a branch predictor to 482329SN/A * predict direction and targets. 492348SN/A * It supports the idling functionality of the CPU by indicating to 502329SN/A * the CPU when it is active and inactive. 511060SN/A */ 521060SN/Atemplate <class Impl> 532292SN/Aclass DefaultFetch 541060SN/A{ 551060SN/A public: 561060SN/A /** Typedefs from Impl. */ 571061SN/A typedef typename Impl::CPUPol CPUPol; 581060SN/A typedef typename Impl::DynInst DynInst; 591061SN/A typedef typename Impl::DynInstPtr DynInstPtr; 601060SN/A typedef typename Impl::FullCPU FullCPU; 611060SN/A typedef typename Impl::Params Params; 621060SN/A 632292SN/A /** Typedefs from the CPU policy. */ 641061SN/A typedef typename CPUPol::BPredUnit BPredUnit; 651061SN/A typedef typename CPUPol::FetchStruct FetchStruct; 661061SN/A typedef typename CPUPol::TimeStruct TimeStruct; 671060SN/A 681060SN/A /** Typedefs from ISA. */ 692107SN/A typedef TheISA::MachInst MachInst; 702292SN/A typedef TheISA::ExtMachInst ExtMachInst; 712632Sstever@eecs.umich.edu 722669Sktlim@umich.edu class IcachePort : public Port 732669Sktlim@umich.edu { 742669Sktlim@umich.edu protected: 752669Sktlim@umich.edu DefaultFetch<Impl> *fetch; 762669Sktlim@umich.edu 772669Sktlim@umich.edu public: 782669Sktlim@umich.edu IcachePort(DefaultFetch<Impl> *_fetch) 792669Sktlim@umich.edu : Port(_fetch->name() + "-iport"), fetch(_fetch) 802669Sktlim@umich.edu { } 812669Sktlim@umich.edu 822669Sktlim@umich.edu protected: 832669Sktlim@umich.edu virtual Tick recvAtomic(PacketPtr pkt); 842669Sktlim@umich.edu 852669Sktlim@umich.edu virtual void recvFunctional(PacketPtr pkt); 862669Sktlim@umich.edu 872669Sktlim@umich.edu virtual void recvStatusChange(Status status); 882669Sktlim@umich.edu 892669Sktlim@umich.edu virtual void getDeviceAddressRanges(AddrRangeList &resp, 902669Sktlim@umich.edu AddrRangeList &snoop) 912669Sktlim@umich.edu { resp.clear(); snoop.clear(); } 922669Sktlim@umich.edu 932669Sktlim@umich.edu virtual bool recvTiming(PacketPtr pkt); 942669Sktlim@umich.edu 952669Sktlim@umich.edu virtual void recvRetry(); 962669Sktlim@umich.edu }; 971060SN/A 981060SN/A public: 992329SN/A /** Overall fetch status. Used to determine if the CPU can 1002329SN/A * deschedule itsef due to a lack of activity. 1012292SN/A */ 1022292SN/A enum FetchStatus { 1032292SN/A Active, 1042292SN/A Inactive 1052292SN/A }; 1062292SN/A 1072292SN/A /** Individual thread status. */ 1082292SN/A enum ThreadStatus { 1091060SN/A Running, 1101060SN/A Idle, 1111060SN/A Squashing, 1121060SN/A Blocked, 1132292SN/A Fetching, 1142292SN/A TrapPending, 1152292SN/A QuiescePending, 1162307SN/A SwitchOut, 1172669Sktlim@umich.edu IcacheWaitResponse, 1182696Sktlim@umich.edu IcacheWaitRetry, 1192669Sktlim@umich.edu IcacheAccessComplete 1201060SN/A }; 1211060SN/A 1222292SN/A /** Fetching Policy, Add new policies here.*/ 1232292SN/A enum FetchPriority { 1242292SN/A SingleThread, 1252292SN/A RoundRobin, 1262292SN/A Branch, 1272292SN/A IQ, 1282292SN/A LSQ 1292292SN/A }; 1301060SN/A 1312292SN/A private: 1322292SN/A /** Fetch status. */ 1332292SN/A FetchStatus _status; 1342292SN/A 1352292SN/A /** Per-thread status. */ 1362292SN/A ThreadStatus fetchStatus[Impl::MaxThreads]; 1372292SN/A 1382292SN/A /** Fetch policy. */ 1392292SN/A FetchPriority fetchPolicy; 1402292SN/A 1412292SN/A /** List that has the threads organized by priority. */ 1422292SN/A std::list<unsigned> priorityList; 1431060SN/A 1441060SN/A public: 1452292SN/A /** DefaultFetch constructor. */ 1462292SN/A DefaultFetch(Params *params); 1471684SN/A 1482292SN/A /** Returns the name of fetch. */ 1492292SN/A std::string name() const; 1501684SN/A 1512292SN/A /** Registers statistics. */ 1521062SN/A void regStats(); 1531062SN/A 1542292SN/A /** Sets CPU pointer. */ 1551060SN/A void setCPU(FullCPU *cpu_ptr); 1561060SN/A 1572292SN/A /** Sets the main backwards communication time buffer pointer. */ 1581060SN/A void setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer); 1591060SN/A 1602292SN/A /** Sets pointer to list of active threads. */ 1612292SN/A void setActiveThreads(std::list<unsigned> *at_ptr); 1622292SN/A 1632292SN/A /** Sets pointer to time buffer used to communicate to the next stage. */ 1641060SN/A void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr); 1651060SN/A 1662292SN/A /** Sets pointer to page table. */ 1672292SN/A// void setPageTable(PageTable *pt_ptr); 1682292SN/A 1692292SN/A /** Initialize stage. */ 1702292SN/A void initStage(); 1712292SN/A 1722292SN/A /** Processes cache completion event. */ 1732669Sktlim@umich.edu void processCacheCompletion(PacketPtr pkt); 1742292SN/A 1752348SN/A /** Begins the switch out of the fetch stage. */ 1762307SN/A void switchOut(); 1772307SN/A 1782348SN/A /** Completes the switch out of the fetch stage. */ 1792316SN/A void doSwitchOut(); 1802316SN/A 1812348SN/A /** Takes over from another CPU's thread. */ 1822307SN/A void takeOverFrom(); 1832307SN/A 1842348SN/A /** Checks if the fetch stage is switched out. */ 1852307SN/A bool isSwitchedOut() { return switchedOut; } 1862307SN/A 1872348SN/A /** Tells fetch to wake up from a quiesce instruction. */ 1882292SN/A void wakeFromQuiesce(); 1891060SN/A 1901061SN/A private: 1912329SN/A /** Changes the status of this stage to active, and indicates this 1922329SN/A * to the CPU. 1932292SN/A */ 1942292SN/A inline void switchToActive(); 1952292SN/A 1962329SN/A /** Changes the status of this stage to inactive, and indicates 1972329SN/A * this to the CPU. 1982292SN/A */ 1992292SN/A inline void switchToInactive(); 2002292SN/A 2011061SN/A /** 2021061SN/A * Looks up in the branch predictor to see if the next PC should be 2031061SN/A * either next PC+=MachInst or a branch target. 2041763SN/A * @param next_PC Next PC variable passed in by reference. It is 2051061SN/A * expected to be set to the current PC; it will be updated with what 2061061SN/A * the next PC will be. 2071061SN/A * @return Whether or not a branch was predicted as taken. 2081061SN/A */ 2091062SN/A bool lookupAndUpdateNextPC(DynInstPtr &inst, Addr &next_PC); 2101062SN/A 2111062SN/A /** 2121062SN/A * Fetches the cache line that contains fetch_PC. Returns any 2131062SN/A * fault that happened. Puts the data into the class variable 2141062SN/A * cacheData. 2151763SN/A * @param fetch_PC The PC address that is being fetched from. 2162292SN/A * @param ret_fault The fault reference that will be set to the result of 2172292SN/A * the icache access. 2182292SN/A * @param tid Thread id. 2191062SN/A * @return Any fault that occured. 2201062SN/A */ 2212292SN/A bool fetchCacheLine(Addr fetch_PC, Fault &ret_fault, unsigned tid); 2221062SN/A 2232292SN/A /** Squashes a specific thread and resets the PC. */ 2242292SN/A inline void doSquash(const Addr &new_PC, unsigned tid); 2251684SN/A 2262292SN/A /** Squashes a specific thread and resets the PC. Also tells the CPU to 2272292SN/A * remove any instructions between fetch and decode that should be sqaushed. 2282292SN/A */ 2292292SN/A void squashFromDecode(const Addr &new_PC, const InstSeqNum &seq_num, 2302292SN/A unsigned tid); 2312292SN/A 2322292SN/A /** Checks if a thread is stalled. */ 2332292SN/A bool checkStall(unsigned tid) const; 2342292SN/A 2352292SN/A /** Updates overall fetch stage status; to be called at the end of each 2362292SN/A * cycle. */ 2372292SN/A FetchStatus updateFetchStatus(); 2381684SN/A 2391684SN/A public: 2402292SN/A /** Squashes a specific thread and resets the PC. Also tells the CPU to 2412292SN/A * remove any instructions that are not in the ROB. The source of this 2422292SN/A * squash should be the commit stage. 2432292SN/A */ 2442292SN/A void squash(const Addr &new_PC, unsigned tid); 2451684SN/A 2462292SN/A /** Ticks the fetch stage, processing all inputs signals and fetching 2472292SN/A * as many instructions as possible. 2482292SN/A */ 2491684SN/A void tick(); 2501684SN/A 2512292SN/A /** Checks all input signals and updates the status as necessary. 2522292SN/A * @return: Returns if the status has changed due to input signals. 2532292SN/A */ 2542292SN/A bool checkSignalsAndUpdate(unsigned tid); 2551684SN/A 2562292SN/A /** Does the actual fetching of instructions and passing them on to the 2572292SN/A * next stage. 2582292SN/A * @param status_change fetch() sets this variable if there was a status 2592292SN/A * change (ie switching to IcacheMissStall). 2602292SN/A */ 2612292SN/A void fetch(bool &status_change); 2622292SN/A 2632292SN/A /** Align a PC to the start of an I-cache block. */ 2641062SN/A Addr icacheBlockAlignPC(Addr addr) 2651062SN/A { 2662107SN/A addr = TheISA::realPCToFetchPC(addr); 2671062SN/A return (addr & ~(cacheBlkMask)); 2681062SN/A } 2691061SN/A 2701060SN/A private: 2712696Sktlim@umich.edu void recvRetry(); 2722696Sktlim@umich.edu 2732292SN/A /** Returns the appropriate thread to fetch, given the fetch policy. */ 2742292SN/A int getFetchingThread(FetchPriority &fetch_priority); 2752292SN/A 2762292SN/A /** Returns the appropriate thread to fetch using a round robin policy. */ 2772292SN/A int roundRobin(); 2782292SN/A 2792292SN/A /** Returns the appropriate thread to fetch using the IQ count policy. */ 2802292SN/A int iqCount(); 2812292SN/A 2822292SN/A /** Returns the appropriate thread to fetch using the LSQ count policy. */ 2832292SN/A int lsqCount(); 2842292SN/A 2852292SN/A /** Returns the appropriate thread to fetch using the branch count policy. */ 2862292SN/A int branchCount(); 2872292SN/A 2882292SN/A private: 2891060SN/A /** Pointer to the FullCPU. */ 2901060SN/A FullCPU *cpu; 2911060SN/A 2921060SN/A /** Time buffer interface. */ 2931060SN/A TimeBuffer<TimeStruct> *timeBuffer; 2941060SN/A 2951060SN/A /** Wire to get decode's information from backwards time buffer. */ 2961060SN/A typename TimeBuffer<TimeStruct>::wire fromDecode; 2971060SN/A 2981060SN/A /** Wire to get rename's information from backwards time buffer. */ 2991060SN/A typename TimeBuffer<TimeStruct>::wire fromRename; 3001060SN/A 3011060SN/A /** Wire to get iew's information from backwards time buffer. */ 3021060SN/A typename TimeBuffer<TimeStruct>::wire fromIEW; 3031060SN/A 3041060SN/A /** Wire to get commit's information from backwards time buffer. */ 3051060SN/A typename TimeBuffer<TimeStruct>::wire fromCommit; 3061060SN/A 3071060SN/A /** Internal fetch instruction queue. */ 3081060SN/A TimeBuffer<FetchStruct> *fetchQueue; 3091060SN/A 3101060SN/A //Might be annoying how this name is different than the queue. 3111060SN/A /** Wire used to write any information heading to decode. */ 3121060SN/A typename TimeBuffer<FetchStruct>::wire toDecode; 3131060SN/A 3142669Sktlim@umich.edu MemObject *mem; 3152669Sktlim@umich.edu 3161060SN/A /** Icache interface. */ 3172669Sktlim@umich.edu IcachePort *icachePort; 3181060SN/A 3191061SN/A /** BPredUnit. */ 3201061SN/A BPredUnit branchPred; 3211061SN/A 3222348SN/A /** Per-thread fetch PC. */ 3232292SN/A Addr PC[Impl::MaxThreads]; 3242292SN/A 3252348SN/A /** Per-thread next PC. */ 3262292SN/A Addr nextPC[Impl::MaxThreads]; 3272292SN/A 3282678Sktlim@umich.edu /** Memory request used to access cache. */ 3292678Sktlim@umich.edu RequestPtr memReq[Impl::MaxThreads]; 3302292SN/A 3312292SN/A /** Variable that tracks if fetch has written to the time buffer this 3322292SN/A * cycle. Used to tell CPU if there is activity this cycle. 3332292SN/A */ 3342292SN/A bool wroteToTimeBuffer; 3352292SN/A 3362292SN/A /** Tracks how many instructions has been fetched this cycle. */ 3372292SN/A int numInst; 3382292SN/A 3392292SN/A /** Source of possible stalls. */ 3402292SN/A struct Stalls { 3412292SN/A bool decode; 3422292SN/A bool rename; 3432292SN/A bool iew; 3442292SN/A bool commit; 3452292SN/A }; 3462292SN/A 3472292SN/A /** Tracks which stages are telling fetch to stall. */ 3482292SN/A Stalls stalls[Impl::MaxThreads]; 3491060SN/A 3501060SN/A /** Decode to fetch delay, in ticks. */ 3511060SN/A unsigned decodeToFetchDelay; 3521060SN/A 3531060SN/A /** Rename to fetch delay, in ticks. */ 3541060SN/A unsigned renameToFetchDelay; 3551060SN/A 3561060SN/A /** IEW to fetch delay, in ticks. */ 3571060SN/A unsigned iewToFetchDelay; 3581060SN/A 3591060SN/A /** Commit to fetch delay, in ticks. */ 3601060SN/A unsigned commitToFetchDelay; 3611060SN/A 3621060SN/A /** The width of fetch in instructions. */ 3631060SN/A unsigned fetchWidth; 3641060SN/A 3652696Sktlim@umich.edu /** Is the cache blocked? If so no threads can access it. */ 3662696Sktlim@umich.edu bool cacheBlocked; 3672696Sktlim@umich.edu 3682696Sktlim@umich.edu /** The packet that is waiting to be retried. */ 3692696Sktlim@umich.edu PacketPtr retryPkt; 3702696Sktlim@umich.edu 3712696Sktlim@umich.edu /** The thread that is waiting on the cache to tell fetch to retry. */ 3722696Sktlim@umich.edu int retryTid; 3732696Sktlim@umich.edu 3741060SN/A /** Cache block size. */ 3751062SN/A int cacheBlkSize; 3761060SN/A 3771060SN/A /** Mask to get a cache block's address. */ 3781062SN/A Addr cacheBlkMask; 3791060SN/A 3801062SN/A /** The cache line being fetched. */ 3812292SN/A uint8_t *cacheData[Impl::MaxThreads]; 3821060SN/A 3831060SN/A /** Size of instructions. */ 3841060SN/A int instSize; 3851060SN/A 3861060SN/A /** Icache stall statistics. */ 3872292SN/A Counter lastIcacheStall[Impl::MaxThreads]; 3881062SN/A 3892292SN/A /** List of Active Threads */ 3902292SN/A std::list<unsigned> *activeThreads; 3912292SN/A 3922292SN/A /** Number of threads. */ 3932292SN/A unsigned numThreads; 3942292SN/A 3952292SN/A /** Number of threads that are actively fetching. */ 3962292SN/A unsigned numFetchingThreads; 3972292SN/A 3982292SN/A /** Thread ID being fetched. */ 3992292SN/A int threadFetched; 4002292SN/A 4012348SN/A /** Checks if there is an interrupt pending. If there is, fetch 4022348SN/A * must stop once it is not fetching PAL instructions. 4032348SN/A */ 4042292SN/A bool interruptPending; 4052292SN/A 4062348SN/A /** Records if fetch is switched out. */ 4072307SN/A bool switchedOut; 4082307SN/A 4092292SN/A#if !FULL_SYSTEM 4102292SN/A /** Page table pointer. */ 4112292SN/A// PageTable *pTable; 4122292SN/A#endif 4132292SN/A 4142292SN/A // @todo: Consider making these vectors and tracking on a per thread basis. 4152292SN/A /** Stat for total number of cycles stalled due to an icache miss. */ 4161062SN/A Stats::Scalar<> icacheStallCycles; 4172292SN/A /** Stat for total number of fetched instructions. */ 4181062SN/A Stats::Scalar<> fetchedInsts; 4192301SN/A Stats::Scalar<> fetchedBranches; 4202292SN/A /** Stat for total number of predicted branches. */ 4211062SN/A Stats::Scalar<> predictedBranches; 4222292SN/A /** Stat for total number of cycles spent fetching. */ 4231062SN/A Stats::Scalar<> fetchCycles; 4242292SN/A /** Stat for total number of cycles spent squashing. */ 4251062SN/A Stats::Scalar<> fetchSquashCycles; 4262292SN/A /** Stat for total number of cycles spent blocked due to other stages in 4272292SN/A * the pipeline. 4282292SN/A */ 4292292SN/A Stats::Scalar<> fetchIdleCycles; 4302348SN/A /** Total number of cycles spent blocked. */ 4311062SN/A Stats::Scalar<> fetchBlockedCycles; 4322348SN/A /** Total number of cycles spent in any other state. */ 4332307SN/A Stats::Scalar<> fetchMiscStallCycles; 4342292SN/A /** Stat for total number of fetched cache lines. */ 4351062SN/A Stats::Scalar<> fetchedCacheLines; 4362348SN/A /** Total number of outstanding icache accesses that were dropped 4372348SN/A * due to a squash. 4382348SN/A */ 4392301SN/A Stats::Scalar<> fetchIcacheSquashes; 4402292SN/A /** Distribution of number of instructions fetched each cycle. */ 4412292SN/A Stats::Distribution<> fetchNisnDist; 4422348SN/A /** Rate of how often fetch was idle. */ 4432292SN/A Stats::Formula idleRate; 4442348SN/A /** Number of branch fetches per cycle. */ 4452292SN/A Stats::Formula branchRate; 4462348SN/A /** Number of instruction fetched per cycle. */ 4472292SN/A Stats::Formula fetchRate; 4481060SN/A}; 4491060SN/A 4502292SN/A#endif //__CPU_O3_FETCH_HH__ 451