2 * Copyright (c) 2010-2012, 2014 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __CPU_O3_FETCH_HH__
42 #define __CPU_O3_FETCH_HH__
44 #include "arch/decoder.hh"
45 #include "arch/utility.hh"
46 #include "base/statistics.hh"
47 #include "config/the_isa.hh"
48 #include "cpu/pc_event.hh"
49 #include "cpu/pred/bpred_unit.hh"
50 #include "cpu/timebuf.hh"
51 #include "cpu/translation.hh"
52 #include "enums/FetchPolicy.hh"
53 #include "mem/packet.hh"
54 #include "mem/port.hh"
55 #include "sim/eventq.hh"
56 #include "sim/probe/probe.hh"
58 struct DerivO3CPUParams;
63 * DefaultFetch class handles both single threaded and SMT fetch. Its
64 * width is specified by the parameters; each cycle it tries to fetch
65 * that many instructions. It supports using a branch predictor to
66 * predict direction and targets.
67 * It supports the idling functionality of the CPU by indicating to
68 * the CPU when it is active and inactive.
74 /** Typedefs from Impl. */
75 typedef typename Impl::CPUPol CPUPol;
76 typedef typename Impl::DynInst DynInst;
77 typedef typename Impl::DynInstPtr DynInstPtr;
78 typedef typename Impl::O3CPU O3CPU;
80 /** Typedefs from the CPU policy. */
81 typedef typename CPUPol::FetchStruct FetchStruct;
82 typedef typename CPUPol::TimeStruct TimeStruct;
84 /** Typedefs from ISA. */
85 typedef TheISA::MachInst MachInst;
88 * IcachePort class for instruction fetch.
90 class IcachePort : public MasterPort
93 /** Pointer to fetch. */
94 DefaultFetch<Impl> *fetch;
97 /** Default constructor. */
98 IcachePort(DefaultFetch<Impl> *_fetch, FullO3CPU<Impl>* _cpu)
99 : MasterPort(_cpu->name() + ".icache_port", _cpu), fetch(_fetch)
104 /** Timing version of receive. Handles setting fetch to the
105 * proper status to start fetching. */
106 virtual bool recvTimingResp(PacketPtr pkt);
108 /** Handles doing a retry of a failed fetch. */
109 virtual void recvReqRetry();
112 class FetchTranslation : public BaseTLB::Translation
115 DefaultFetch<Impl> *fetch;
118 FetchTranslation(DefaultFetch<Impl> *_fetch)
127 finish(const Fault &fault, const RequestPtr &req, ThreadContext *tc,
130 assert(mode == BaseTLB::Execute);
131 fetch->finishTranslation(fault, req);
137 /* Event to delay delivery of a fetch translation result in case of
138 * a fault and the nop to carry the fault cannot be generated
140 class FinishTranslationEvent : public Event
143 DefaultFetch<Impl> *fetch;
148 FinishTranslationEvent(DefaultFetch<Impl> *_fetch)
149 : fetch(_fetch), req(nullptr)
152 void setFault(Fault _fault)
157 void setReq(const RequestPtr &_req)
162 /** Process the delayed finish translation */
165 assert(fetch->numInst < fetch->fetchWidth);
166 fetch->finishTranslation(fault, req);
169 const char *description() const
171 return "FullO3CPU FetchFinishTranslation";
176 /** Overall fetch status. Used to determine if the CPU can
177 * deschedule itsef due to a lack of activity.
184 /** Individual thread status. */
196 IcacheAccessComplete,
204 /** Per-thread status. */
205 ThreadStatus fetchStatus[Impl::MaxThreads];
208 FetchPolicy fetchPolicy;
210 /** List that has the threads organized by priority. */
211 std::list<ThreadID> priorityList;
214 ProbePointArg<DynInstPtr> *ppFetch;
215 /** To probe when a fetch request is successfully sent. */
216 ProbePointArg<RequestPtr> *ppFetchRequestSent;
219 /** DefaultFetch constructor. */
220 DefaultFetch(O3CPU *_cpu, DerivO3CPUParams *params);
222 /** Returns the name of fetch. */
223 std::string name() const;
225 /** Registers statistics. */
228 /** Registers probes. */
229 void regProbePoints();
231 /** Sets the main backwards communication time buffer pointer. */
232 void setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer);
234 /** Sets pointer to list of active threads. */
235 void setActiveThreads(std::list<ThreadID> *at_ptr);
237 /** Sets pointer to time buffer used to communicate to the next stage. */
238 void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
240 /** Initialize stage. */
243 /** Clear all thread-specific states*/
244 void clearStates(ThreadID tid);
246 /** Handles retrying the fetch access. */
249 /** Processes cache completion event. */
250 void processCacheCompletion(PacketPtr pkt);
252 /** Resume after a drain. */
255 /** Perform sanity checks after a drain. */
256 void drainSanityCheck() const;
258 /** Has the stage drained? */
259 bool isDrained() const;
261 /** Takes over from another CPU's thread. */
265 * Stall the fetch stage after reaching a safe drain point.
267 * The CPU uses this method to stop fetching instructions from a
268 * thread that has been drained. The drain stall is different from
269 * all other stalls in that it is signaled instantly from the
270 * commit stage (without the normal communication delay) when it
271 * has reached a safe point to drain from.
273 void drainStall(ThreadID tid);
275 /** Tells fetch to wake up from a quiesce instruction. */
276 void wakeFromQuiesce();
278 /** For priority-based fetch policies, need to keep update priorityList */
279 void deactivateThread(ThreadID tid);
281 /** Reset this pipeline stage */
284 /** Changes the status of this stage to active, and indicates this
287 inline void switchToActive();
289 /** Changes the status of this stage to inactive, and indicates
292 inline void switchToInactive();
295 * Looks up in the branch predictor to see if the next PC should be
296 * either next PC+=MachInst or a branch target.
297 * @param next_PC Next PC variable passed in by reference. It is
298 * expected to be set to the current PC; it will be updated with what
299 * the next PC will be.
300 * @param next_NPC Used for ISAs which use delay slots.
301 * @return Whether or not a branch was predicted as taken.
303 bool lookupAndUpdateNextPC(const DynInstPtr &inst, TheISA::PCState &pc);
306 * Fetches the cache line that contains the fetch PC. Returns any
307 * fault that happened. Puts the data into the class variable
308 * fetchBuffer, which may not hold the entire fetched cache line.
309 * @param vaddr The memory address that is being fetched from.
310 * @param ret_fault The fault reference that will be set to the result of
312 * @param tid Thread id.
313 * @param pc The actual PC of the current instruction.
314 * @return Any fault that occured.
316 bool fetchCacheLine(Addr vaddr, ThreadID tid, Addr pc);
317 void finishTranslation(const Fault &fault, const RequestPtr &mem_req);
320 /** Check if an interrupt is pending and that we need to handle
323 checkInterrupt(Addr pc)
325 return interruptPending;
328 /** Squashes a specific thread and resets the PC. */
329 inline void doSquash(const TheISA::PCState &newPC,
330 const DynInstPtr squashInst, ThreadID tid);
332 /** Squashes a specific thread and resets the PC. Also tells the CPU to
333 * remove any instructions between fetch and decode that should be sqaushed.
335 void squashFromDecode(const TheISA::PCState &newPC,
336 const DynInstPtr squashInst,
337 const InstSeqNum seq_num, ThreadID tid);
339 /** Checks if a thread is stalled. */
340 bool checkStall(ThreadID tid) const;
342 /** Updates overall fetch stage status; to be called at the end of each
344 FetchStatus updateFetchStatus();
347 /** Squashes a specific thread and resets the PC. Also tells the CPU to
348 * remove any instructions that are not in the ROB. The source of this
349 * squash should be the commit stage.
351 void squash(const TheISA::PCState &newPC, const InstSeqNum seq_num,
352 DynInstPtr squashInst, ThreadID tid);
354 /** Ticks the fetch stage, processing all inputs signals and fetching
355 * as many instructions as possible.
359 /** Checks all input signals and updates the status as necessary.
360 * @return: Returns if the status has changed due to input signals.
362 bool checkSignalsAndUpdate(ThreadID tid);
364 /** Does the actual fetching of instructions and passing them on to the
366 * @param status_change fetch() sets this variable if there was a status
367 * change (ie switching to IcacheMissStall).
369 void fetch(bool &status_change);
371 /** Align a PC to the start of a fetch buffer block. */
372 Addr fetchBufferAlignPC(Addr addr)
374 return (addr & ~(fetchBufferMask));
378 TheISA::Decoder *decoder[Impl::MaxThreads];
380 MasterPort &getInstPort() { return icachePort; }
383 DynInstPtr buildInst(ThreadID tid, StaticInstPtr staticInst,
384 StaticInstPtr curMacroop, TheISA::PCState thisPC,
385 TheISA::PCState nextPC, bool trace);
387 /** Returns the appropriate thread to fetch, given the fetch policy. */
388 ThreadID getFetchingThread();
390 /** Returns the appropriate thread to fetch using a round robin policy. */
391 ThreadID roundRobin();
393 /** Returns the appropriate thread to fetch using the IQ count policy. */
396 /** Returns the appropriate thread to fetch using the LSQ count policy. */
399 /** Returns the appropriate thread to fetch using the branch count
401 ThreadID branchCount();
403 /** Pipeline the next I-cache access to the current one. */
404 void pipelineIcacheAccesses(ThreadID tid);
406 /** Profile the reasons of fetch stall. */
407 void profileStall(ThreadID tid);
410 /** Pointer to the O3CPU. */
413 /** Time buffer interface. */
414 TimeBuffer<TimeStruct> *timeBuffer;
416 /** Wire to get decode's information from backwards time buffer. */
417 typename TimeBuffer<TimeStruct>::wire fromDecode;
419 /** Wire to get rename's information from backwards time buffer. */
420 typename TimeBuffer<TimeStruct>::wire fromRename;
422 /** Wire to get iew's information from backwards time buffer. */
423 typename TimeBuffer<TimeStruct>::wire fromIEW;
425 /** Wire to get commit's information from backwards time buffer. */
426 typename TimeBuffer<TimeStruct>::wire fromCommit;
428 //Might be annoying how this name is different than the queue.
429 /** Wire used to write any information heading to decode. */
430 typename TimeBuffer<FetchStruct>::wire toDecode;
433 BPredUnit *branchPred;
435 TheISA::PCState pc[Impl::MaxThreads];
437 Addr fetchOffset[Impl::MaxThreads];
439 StaticInstPtr macroop[Impl::MaxThreads];
441 /** Can the fetch stage redirect from an interrupt on this instruction? */
442 bool delayedCommit[Impl::MaxThreads];
444 /** Memory request used to access cache. */
445 RequestPtr memReq[Impl::MaxThreads];
447 /** Variable that tracks if fetch has written to the time buffer this
448 * cycle. Used to tell CPU if there is activity this cycle.
450 bool wroteToTimeBuffer;
452 /** Tracks how many instructions has been fetched this cycle. */
455 /** Source of possible stalls. */
461 /** Tracks which stages are telling fetch to stall. */
462 Stalls stalls[Impl::MaxThreads];
464 /** Decode to fetch delay. */
465 Cycles decodeToFetchDelay;
467 /** Rename to fetch delay. */
468 Cycles renameToFetchDelay;
470 /** IEW to fetch delay. */
471 Cycles iewToFetchDelay;
473 /** Commit to fetch delay. */
474 Cycles commitToFetchDelay;
476 /** The width of fetch in instructions. */
479 /** The width of decode in instructions. */
480 unsigned decodeWidth;
482 /** Is the cache blocked? If so no threads can access it. */
485 /** The packet that is waiting to be retried. */
488 /** The thread that is waiting on the cache to tell fetch to retry. */
491 /** Cache block size. */
492 unsigned int cacheBlkSize;
494 /** The size of the fetch buffer in bytes. The fetch buffer
495 * itself may be smaller than a cache line.
497 unsigned fetchBufferSize;
499 /** Mask to align a fetch address to a fetch buffer boundary. */
500 Addr fetchBufferMask;
502 /** The fetch data that is being fetched and buffered. */
503 uint8_t *fetchBuffer[Impl::MaxThreads];
505 /** The PC of the first instruction loaded into the fetch buffer. */
506 Addr fetchBufferPC[Impl::MaxThreads];
508 /** The size of the fetch queue in micro-ops */
509 unsigned fetchQueueSize;
511 /** Queue of fetched instructions. Per-thread to prevent HoL blocking. */
512 std::deque<DynInstPtr> fetchQueue[Impl::MaxThreads];
514 /** Whether or not the fetch buffer data is valid. */
515 bool fetchBufferValid[Impl::MaxThreads];
517 /** Size of instructions. */
520 /** Icache stall statistics. */
521 Counter lastIcacheStall[Impl::MaxThreads];
523 /** List of Active Threads */
524 std::list<ThreadID> *activeThreads;
526 /** Number of threads. */
529 /** Number of threads that are actively fetching. */
530 ThreadID numFetchingThreads;
532 /** Thread ID being fetched. */
533 ThreadID threadFetched;
535 /** Checks if there is an interrupt pending. If there is, fetch
536 * must stop once it is not fetching PAL instructions.
538 bool interruptPending;
540 /** Instruction port. Note that it has to appear after the fetch stage. */
541 IcachePort icachePort;
543 /** Set to true if a pipelined I-cache request should be issued. */
544 bool issuePipelinedIfetch[Impl::MaxThreads];
546 /** Event used to delay fault generation of translation faults */
547 FinishTranslationEvent finishTranslationEvent;
549 // @todo: Consider making these vectors and tracking on a per thread basis.
550 /** Stat for total number of cycles stalled due to an icache miss. */
551 Stats::Scalar icacheStallCycles;
552 /** Stat for total number of fetched instructions. */
553 Stats::Scalar fetchedInsts;
554 /** Total number of fetched branches. */
555 Stats::Scalar fetchedBranches;
556 /** Stat for total number of predicted branches. */
557 Stats::Scalar predictedBranches;
558 /** Stat for total number of cycles spent fetching. */
559 Stats::Scalar fetchCycles;
560 /** Stat for total number of cycles spent squashing. */
561 Stats::Scalar fetchSquashCycles;
562 /** Stat for total number of cycles spent waiting for translation */
563 Stats::Scalar fetchTlbCycles;
564 /** Stat for total number of cycles spent blocked due to other stages in
567 Stats::Scalar fetchIdleCycles;
568 /** Total number of cycles spent blocked. */
569 Stats::Scalar fetchBlockedCycles;
570 /** Total number of cycles spent in any other state. */
571 Stats::Scalar fetchMiscStallCycles;
572 /** Total number of cycles spent in waiting for drains. */
573 Stats::Scalar fetchPendingDrainCycles;
574 /** Total number of stall cycles caused by no active threads to run. */
575 Stats::Scalar fetchNoActiveThreadStallCycles;
576 /** Total number of stall cycles caused by pending traps. */
577 Stats::Scalar fetchPendingTrapStallCycles;
578 /** Total number of stall cycles caused by pending quiesce instructions. */
579 Stats::Scalar fetchPendingQuiesceStallCycles;
580 /** Total number of stall cycles caused by I-cache wait retrys. */
581 Stats::Scalar fetchIcacheWaitRetryStallCycles;
582 /** Stat for total number of fetched cache lines. */
583 Stats::Scalar fetchedCacheLines;
584 /** Total number of outstanding icache accesses that were dropped
587 Stats::Scalar fetchIcacheSquashes;
588 /** Total number of outstanding tlb accesses that were dropped
591 Stats::Scalar fetchTlbSquashes;
592 /** Distribution of number of instructions fetched each cycle. */
593 Stats::Distribution fetchNisnDist;
594 /** Rate of how often fetch was idle. */
595 Stats::Formula idleRate;
596 /** Number of branch fetches per cycle. */
597 Stats::Formula branchRate;
598 /** Number of instruction fetched per cycle. */
599 Stats::Formula fetchRate;
602 #endif //__CPU_O3_FETCH_HH__