fetch.hh revision 9480:d059f8a95a42
1/* 2 * Copyright (c) 2010-2012 ARM Limited 3 * All rights reserved 4 * 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. 13 * 14 * Copyright (c) 2004-2006 The Regents of The University of Michigan 15 * All rights reserved. 16 * 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. 27 * 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. 39 * 40 * Authors: Kevin Lim 41 * Korey Sewell 42 */ 43 44#ifndef __CPU_O3_FETCH_HH__ 45#define __CPU_O3_FETCH_HH__ 46 47#include "arch/decoder.hh" 48#include "arch/utility.hh" 49#include "base/statistics.hh" 50#include "config/the_isa.hh" 51#include "cpu/pc_event.hh" 52#include "cpu/pred/bpred_unit.hh" 53#include "cpu/timebuf.hh" 54#include "cpu/translation.hh" 55#include "mem/packet.hh" 56#include "mem/port.hh" 57#include "sim/eventq.hh" 58 59struct DerivO3CPUParams; 60 61/** 62 * DefaultFetch class handles both single threaded and SMT fetch. Its 63 * width is specified by the parameters; each cycle it tries to fetch 64 * that many instructions. It supports using a branch predictor to 65 * predict direction and targets. 66 * It supports the idling functionality of the CPU by indicating to 67 * the CPU when it is active and inactive. 68 */ 69template <class Impl> 70class DefaultFetch 71{ 72 public: 73 /** Typedefs from Impl. */ 74 typedef typename Impl::CPUPol CPUPol; 75 typedef typename Impl::DynInst DynInst; 76 typedef typename Impl::DynInstPtr DynInstPtr; 77 typedef typename Impl::O3CPU O3CPU; 78 79 /** Typedefs from the CPU policy. */ 80 typedef typename CPUPol::FetchStruct FetchStruct; 81 typedef typename CPUPol::TimeStruct TimeStruct; 82 83 /** Typedefs from ISA. */ 84 typedef TheISA::MachInst MachInst; 85 typedef TheISA::ExtMachInst ExtMachInst; 86 87 class FetchTranslation : public BaseTLB::Translation 88 { 89 protected: 90 DefaultFetch<Impl> *fetch; 91 92 public: 93 FetchTranslation(DefaultFetch<Impl> *_fetch) 94 : fetch(_fetch) 95 {} 96 97 void 98 markDelayed() 99 {} 100 101 void 102 finish(Fault fault, RequestPtr req, ThreadContext *tc, 103 BaseTLB::Mode mode) 104 { 105 assert(mode == BaseTLB::Execute); 106 fetch->finishTranslation(fault, req); 107 delete this; 108 } 109 }; 110 111 private: 112 /* Event to delay delivery of a fetch translation result in case of 113 * a fault and the nop to carry the fault cannot be generated 114 * immediately */ 115 class FinishTranslationEvent : public Event 116 { 117 private: 118 DefaultFetch<Impl> *fetch; 119 Fault fault; 120 RequestPtr req; 121 122 public: 123 FinishTranslationEvent(DefaultFetch<Impl> *_fetch) 124 : fetch(_fetch) 125 {} 126 127 void setFault(Fault _fault) 128 { 129 fault = _fault; 130 } 131 132 void setReq(RequestPtr _req) 133 { 134 req = _req; 135 } 136 137 /** Process the delayed finish translation */ 138 void process() 139 { 140 assert(fetch->numInst < fetch->fetchWidth); 141 fetch->finishTranslation(fault, req); 142 } 143 144 const char *description() const 145 { 146 return "FullO3CPU FetchFinishTranslation"; 147 } 148 }; 149 150 public: 151 /** Overall fetch status. Used to determine if the CPU can 152 * deschedule itsef due to a lack of activity. 153 */ 154 enum FetchStatus { 155 Active, 156 Inactive 157 }; 158 159 /** Individual thread status. */ 160 enum ThreadStatus { 161 Running, 162 Idle, 163 Squashing, 164 Blocked, 165 Fetching, 166 TrapPending, 167 QuiescePending, 168 ItlbWait, 169 IcacheWaitResponse, 170 IcacheWaitRetry, 171 IcacheAccessComplete, 172 NoGoodAddr 173 }; 174 175 /** Fetching Policy, Add new policies here.*/ 176 enum FetchPriority { 177 SingleThread, 178 RoundRobin, 179 Branch, 180 IQ, 181 LSQ 182 }; 183 184 private: 185 /** Fetch status. */ 186 FetchStatus _status; 187 188 /** Per-thread status. */ 189 ThreadStatus fetchStatus[Impl::MaxThreads]; 190 191 /** Fetch policy. */ 192 FetchPriority fetchPolicy; 193 194 /** List that has the threads organized by priority. */ 195 std::list<ThreadID> priorityList; 196 197 public: 198 /** DefaultFetch constructor. */ 199 DefaultFetch(O3CPU *_cpu, DerivO3CPUParams *params); 200 201 /** Returns the name of fetch. */ 202 std::string name() const; 203 204 /** Registers statistics. */ 205 void regStats(); 206 207 /** Sets the main backwards communication time buffer pointer. */ 208 void setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer); 209 210 /** Sets pointer to list of active threads. */ 211 void setActiveThreads(std::list<ThreadID> *at_ptr); 212 213 /** Sets pointer to time buffer used to communicate to the next stage. */ 214 void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr); 215 216 /** Initialize stage. */ 217 void startupStage(); 218 219 /** Tells the fetch stage that the Icache is set. */ 220 void setIcache(); 221 222 /** Handles retrying the fetch access. */ 223 void recvRetry(); 224 225 /** Processes cache completion event. */ 226 void processCacheCompletion(PacketPtr pkt); 227 228 /** Resume after a drain. */ 229 void drainResume(); 230 231 /** Perform sanity checks after a drain. */ 232 void drainSanityCheck() const; 233 234 /** Has the stage drained? */ 235 bool isDrained() const; 236 237 /** Takes over from another CPU's thread. */ 238 void takeOverFrom(); 239 240 /** 241 * Stall the fetch stage after reaching a safe drain point. 242 * 243 * The CPU uses this method to stop fetching instructions from a 244 * thread that has been drained. The drain stall is different from 245 * all other stalls in that it is signaled instantly from the 246 * commit stage (without the normal communication delay) when it 247 * has reached a safe point to drain from. 248 */ 249 void drainStall(ThreadID tid); 250 251 /** Tells fetch to wake up from a quiesce instruction. */ 252 void wakeFromQuiesce(); 253 254 private: 255 /** Reset this pipeline stage */ 256 void resetStage(); 257 258 /** Changes the status of this stage to active, and indicates this 259 * to the CPU. 260 */ 261 inline void switchToActive(); 262 263 /** Changes the status of this stage to inactive, and indicates 264 * this to the CPU. 265 */ 266 inline void switchToInactive(); 267 268 /** 269 * Looks up in the branch predictor to see if the next PC should be 270 * either next PC+=MachInst or a branch target. 271 * @param next_PC Next PC variable passed in by reference. It is 272 * expected to be set to the current PC; it will be updated with what 273 * the next PC will be. 274 * @param next_NPC Used for ISAs which use delay slots. 275 * @return Whether or not a branch was predicted as taken. 276 */ 277 bool lookupAndUpdateNextPC(DynInstPtr &inst, TheISA::PCState &pc); 278 279 /** 280 * Fetches the cache line that contains fetch_PC. Returns any 281 * fault that happened. Puts the data into the class variable 282 * cacheData. 283 * @param vaddr The memory address that is being fetched from. 284 * @param ret_fault The fault reference that will be set to the result of 285 * the icache access. 286 * @param tid Thread id. 287 * @param pc The actual PC of the current instruction. 288 * @return Any fault that occured. 289 */ 290 bool fetchCacheLine(Addr vaddr, ThreadID tid, Addr pc); 291 void finishTranslation(Fault fault, RequestPtr mem_req); 292 293 294 /** Check if an interrupt is pending and that we need to handle 295 */ 296 bool 297 checkInterrupt(Addr pc) 298 { 299 return (interruptPending && (THE_ISA != ALPHA_ISA || !(pc & 0x3))); 300 } 301 302 /** Squashes a specific thread and resets the PC. */ 303 inline void doSquash(const TheISA::PCState &newPC, 304 const DynInstPtr squashInst, ThreadID tid); 305 306 /** Squashes a specific thread and resets the PC. Also tells the CPU to 307 * remove any instructions between fetch and decode that should be sqaushed. 308 */ 309 void squashFromDecode(const TheISA::PCState &newPC, 310 const DynInstPtr squashInst, 311 const InstSeqNum seq_num, ThreadID tid); 312 313 /** Checks if a thread is stalled. */ 314 bool checkStall(ThreadID tid) const; 315 316 /** Updates overall fetch stage status; to be called at the end of each 317 * cycle. */ 318 FetchStatus updateFetchStatus(); 319 320 public: 321 /** Squashes a specific thread and resets the PC. Also tells the CPU to 322 * remove any instructions that are not in the ROB. The source of this 323 * squash should be the commit stage. 324 */ 325 void squash(const TheISA::PCState &newPC, const InstSeqNum seq_num, 326 DynInstPtr squashInst, ThreadID tid); 327 328 /** Ticks the fetch stage, processing all inputs signals and fetching 329 * as many instructions as possible. 330 */ 331 void tick(); 332 333 /** Checks all input signals and updates the status as necessary. 334 * @return: Returns if the status has changed due to input signals. 335 */ 336 bool checkSignalsAndUpdate(ThreadID tid); 337 338 /** Does the actual fetching of instructions and passing them on to the 339 * next stage. 340 * @param status_change fetch() sets this variable if there was a status 341 * change (ie switching to IcacheMissStall). 342 */ 343 void fetch(bool &status_change); 344 345 /** Align a PC to the start of an I-cache block. */ 346 Addr icacheBlockAlignPC(Addr addr) 347 { 348 return (addr & ~(cacheBlkMask)); 349 } 350 351 /** The decoder. */ 352 TheISA::Decoder *decoder[Impl::MaxThreads]; 353 354 private: 355 DynInstPtr buildInst(ThreadID tid, StaticInstPtr staticInst, 356 StaticInstPtr curMacroop, TheISA::PCState thisPC, 357 TheISA::PCState nextPC, bool trace); 358 359 /** Returns the appropriate thread to fetch, given the fetch policy. */ 360 ThreadID getFetchingThread(FetchPriority &fetch_priority); 361 362 /** Returns the appropriate thread to fetch using a round robin policy. */ 363 ThreadID roundRobin(); 364 365 /** Returns the appropriate thread to fetch using the IQ count policy. */ 366 ThreadID iqCount(); 367 368 /** Returns the appropriate thread to fetch using the LSQ count policy. */ 369 ThreadID lsqCount(); 370 371 /** Returns the appropriate thread to fetch using the branch count 372 * policy. */ 373 ThreadID branchCount(); 374 375 /** Pipeline the next I-cache access to the current one. */ 376 void pipelineIcacheAccesses(ThreadID tid); 377 378 /** Profile the reasons of fetch stall. */ 379 void profileStall(ThreadID tid); 380 381 private: 382 /** Pointer to the O3CPU. */ 383 O3CPU *cpu; 384 385 /** Time buffer interface. */ 386 TimeBuffer<TimeStruct> *timeBuffer; 387 388 /** Wire to get decode's information from backwards time buffer. */ 389 typename TimeBuffer<TimeStruct>::wire fromDecode; 390 391 /** Wire to get rename's information from backwards time buffer. */ 392 typename TimeBuffer<TimeStruct>::wire fromRename; 393 394 /** Wire to get iew's information from backwards time buffer. */ 395 typename TimeBuffer<TimeStruct>::wire fromIEW; 396 397 /** Wire to get commit's information from backwards time buffer. */ 398 typename TimeBuffer<TimeStruct>::wire fromCommit; 399 400 /** Internal fetch instruction queue. */ 401 TimeBuffer<FetchStruct> *fetchQueue; 402 403 //Might be annoying how this name is different than the queue. 404 /** Wire used to write any information heading to decode. */ 405 typename TimeBuffer<FetchStruct>::wire toDecode; 406 407 /** BPredUnit. */ 408 BPredUnit *branchPred; 409 410 TheISA::PCState pc[Impl::MaxThreads]; 411 412 Addr fetchOffset[Impl::MaxThreads]; 413 414 StaticInstPtr macroop[Impl::MaxThreads]; 415 416 /** Can the fetch stage redirect from an interrupt on this instruction? */ 417 bool delayedCommit[Impl::MaxThreads]; 418 419 /** Memory request used to access cache. */ 420 RequestPtr memReq[Impl::MaxThreads]; 421 422 /** Variable that tracks if fetch has written to the time buffer this 423 * cycle. Used to tell CPU if there is activity this cycle. 424 */ 425 bool wroteToTimeBuffer; 426 427 /** Tracks how many instructions has been fetched this cycle. */ 428 int numInst; 429 430 /** Source of possible stalls. */ 431 struct Stalls { 432 bool decode; 433 bool rename; 434 bool iew; 435 bool commit; 436 bool drain; 437 }; 438 439 /** Tracks which stages are telling fetch to stall. */ 440 Stalls stalls[Impl::MaxThreads]; 441 442 /** Decode to fetch delay. */ 443 Cycles decodeToFetchDelay; 444 445 /** Rename to fetch delay. */ 446 Cycles renameToFetchDelay; 447 448 /** IEW to fetch delay. */ 449 Cycles iewToFetchDelay; 450 451 /** Commit to fetch delay. */ 452 Cycles commitToFetchDelay; 453 454 /** The width of fetch in instructions. */ 455 unsigned fetchWidth; 456 457 /** Is the cache blocked? If so no threads can access it. */ 458 bool cacheBlocked; 459 460 /** The packet that is waiting to be retried. */ 461 PacketPtr retryPkt; 462 463 /** The thread that is waiting on the cache to tell fetch to retry. */ 464 ThreadID retryTid; 465 466 /** Cache block size. */ 467 int cacheBlkSize; 468 469 /** Mask to get a cache block's address. */ 470 Addr cacheBlkMask; 471 472 /** The cache line being fetched. */ 473 uint8_t *cacheData[Impl::MaxThreads]; 474 475 /** The PC of the cacheline that has been loaded. */ 476 Addr cacheDataPC[Impl::MaxThreads]; 477 478 /** Whether or not the cache data is valid. */ 479 bool cacheDataValid[Impl::MaxThreads]; 480 481 /** Size of instructions. */ 482 int instSize; 483 484 /** Icache stall statistics. */ 485 Counter lastIcacheStall[Impl::MaxThreads]; 486 487 /** List of Active Threads */ 488 std::list<ThreadID> *activeThreads; 489 490 /** Number of threads. */ 491 ThreadID numThreads; 492 493 /** Number of threads that are actively fetching. */ 494 ThreadID numFetchingThreads; 495 496 /** Thread ID being fetched. */ 497 ThreadID threadFetched; 498 499 /** Checks if there is an interrupt pending. If there is, fetch 500 * must stop once it is not fetching PAL instructions. 501 */ 502 bool interruptPending; 503 504 /** Set to true if a pipelined I-cache request should be issued. */ 505 bool issuePipelinedIfetch[Impl::MaxThreads]; 506 507 /** Event used to delay fault generation of translation faults */ 508 FinishTranslationEvent finishTranslationEvent; 509 510 // @todo: Consider making these vectors and tracking on a per thread basis. 511 /** Stat for total number of cycles stalled due to an icache miss. */ 512 Stats::Scalar icacheStallCycles; 513 /** Stat for total number of fetched instructions. */ 514 Stats::Scalar fetchedInsts; 515 /** Total number of fetched branches. */ 516 Stats::Scalar fetchedBranches; 517 /** Stat for total number of predicted branches. */ 518 Stats::Scalar predictedBranches; 519 /** Stat for total number of cycles spent fetching. */ 520 Stats::Scalar fetchCycles; 521 /** Stat for total number of cycles spent squashing. */ 522 Stats::Scalar fetchSquashCycles; 523 /** Stat for total number of cycles spent waiting for translation */ 524 Stats::Scalar fetchTlbCycles; 525 /** Stat for total number of cycles spent blocked due to other stages in 526 * the pipeline. 527 */ 528 Stats::Scalar fetchIdleCycles; 529 /** Total number of cycles spent blocked. */ 530 Stats::Scalar fetchBlockedCycles; 531 /** Total number of cycles spent in any other state. */ 532 Stats::Scalar fetchMiscStallCycles; 533 /** Total number of cycles spent in waiting for drains. */ 534 Stats::Scalar fetchPendingDrainCycles; 535 /** Total number of stall cycles caused by no active threads to run. */ 536 Stats::Scalar fetchNoActiveThreadStallCycles; 537 /** Total number of stall cycles caused by pending traps. */ 538 Stats::Scalar fetchPendingTrapStallCycles; 539 /** Total number of stall cycles caused by pending quiesce instructions. */ 540 Stats::Scalar fetchPendingQuiesceStallCycles; 541 /** Total number of stall cycles caused by I-cache wait retrys. */ 542 Stats::Scalar fetchIcacheWaitRetryStallCycles; 543 /** Stat for total number of fetched cache lines. */ 544 Stats::Scalar fetchedCacheLines; 545 /** Total number of outstanding icache accesses that were dropped 546 * due to a squash. 547 */ 548 Stats::Scalar fetchIcacheSquashes; 549 /** Total number of outstanding tlb accesses that were dropped 550 * due to a squash. 551 */ 552 Stats::Scalar fetchTlbSquashes; 553 /** Distribution of number of instructions fetched each cycle. */ 554 Stats::Distribution fetchNisnDist; 555 /** Rate of how often fetch was idle. */ 556 Stats::Formula idleRate; 557 /** Number of branch fetches per cycle. */ 558 Stats::Formula branchRate; 559 /** Number of instruction fetched per cycle. */ 560 Stats::Formula fetchRate; 561}; 562 563#endif //__CPU_O3_FETCH_HH__ 564