commit.hh revision 7855:c0be563517da
1/* 2 * Copyright (c) 2010 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_COMMIT_HH__ 45#define __CPU_O3_COMMIT_HH__ 46 47#include "base/statistics.hh" 48#include "cpu/timebuf.hh" 49#include "cpu/exetrace.hh" 50#include "cpu/inst_seq.hh" 51 52class DerivO3CPUParams; 53 54template <class> 55class O3ThreadState; 56 57/** 58 * DefaultCommit handles single threaded and SMT commit. Its width is 59 * specified by the parameters; each cycle it tries to commit that 60 * many instructions. The SMT policy decides which thread it tries to 61 * commit instructions from. Non- speculative instructions must reach 62 * the head of the ROB before they are ready to execute; once they 63 * reach the head, commit will broadcast the instruction's sequence 64 * number to the previous stages so that they can issue/ execute the 65 * instruction. Only one non-speculative instruction is handled per 66 * cycle. Commit is responsible for handling all back-end initiated 67 * redirects. It receives the redirect, and then broadcasts it to all 68 * stages, indicating the sequence number they should squash until, 69 * and any necessary branch misprediction information as well. It 70 * priortizes redirects by instruction's age, only broadcasting a 71 * redirect if it corresponds to an instruction that should currently 72 * be in the ROB. This is done by tracking the sequence number of the 73 * youngest instruction in the ROB, which gets updated to any 74 * squashing instruction's sequence number, and only broadcasting a 75 * redirect if it corresponds to an older instruction. Commit also 76 * supports multiple cycle squashing, to model a ROB that can only 77 * remove a certain number of instructions per cycle. 78 */ 79template<class Impl> 80class DefaultCommit 81{ 82 public: 83 // Typedefs from the Impl. 84 typedef typename Impl::O3CPU O3CPU; 85 typedef typename Impl::DynInstPtr DynInstPtr; 86 typedef typename Impl::CPUPol CPUPol; 87 88 typedef typename CPUPol::RenameMap RenameMap; 89 typedef typename CPUPol::ROB ROB; 90 91 typedef typename CPUPol::TimeStruct TimeStruct; 92 typedef typename CPUPol::FetchStruct FetchStruct; 93 typedef typename CPUPol::IEWStruct IEWStruct; 94 typedef typename CPUPol::RenameStruct RenameStruct; 95 96 typedef typename CPUPol::Fetch Fetch; 97 typedef typename CPUPol::IEW IEW; 98 99 typedef O3ThreadState<Impl> Thread; 100 101 /** Event class used to schedule a squash due to a trap (fault or 102 * interrupt) to happen on a specific cycle. 103 */ 104 class TrapEvent : public Event { 105 private: 106 DefaultCommit<Impl> *commit; 107 ThreadID tid; 108 109 public: 110 TrapEvent(DefaultCommit<Impl> *_commit, ThreadID _tid); 111 112 void process(); 113 const char *description() const; 114 }; 115 116 /** Overall commit status. Used to determine if the CPU can deschedule 117 * itself due to a lack of activity. 118 */ 119 enum CommitStatus{ 120 Active, 121 Inactive 122 }; 123 124 /** Individual thread status. */ 125 enum ThreadStatus { 126 Running, 127 Idle, 128 ROBSquashing, 129 TrapPending, 130 FetchTrapPending 131 }; 132 133 /** Commit policy for SMT mode. */ 134 enum CommitPolicy { 135 Aggressive, 136 RoundRobin, 137 OldestReady 138 }; 139 140 private: 141 /** Overall commit status. */ 142 CommitStatus _status; 143 /** Next commit status, to be set at the end of the cycle. */ 144 CommitStatus _nextStatus; 145 /** Per-thread status. */ 146 ThreadStatus commitStatus[Impl::MaxThreads]; 147 /** Commit policy used in SMT mode. */ 148 CommitPolicy commitPolicy; 149 150 public: 151 /** Construct a DefaultCommit with the given parameters. */ 152 DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params); 153 154 /** Returns the name of the DefaultCommit. */ 155 std::string name() const; 156 157 /** Registers statistics. */ 158 void regStats(); 159 160 /** Sets the list of threads. */ 161 void setThreads(std::vector<Thread *> &threads); 162 163 /** Sets the main time buffer pointer, used for backwards communication. */ 164 void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr); 165 166 void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr); 167 168 /** Sets the pointer to the queue coming from rename. */ 169 void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr); 170 171 /** Sets the pointer to the queue coming from IEW. */ 172 void setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr); 173 174 /** Sets the pointer to the IEW stage. */ 175 void setIEWStage(IEW *iew_stage); 176 177 /** Skid buffer between rename and commit. */ 178 std::queue<DynInstPtr> skidBuffer; 179 180 /** The pointer to the IEW stage. Used solely to ensure that 181 * various events (traps, interrupts, syscalls) do not occur until 182 * all stores have written back. 183 */ 184 IEW *iewStage; 185 186 /** Sets pointer to list of active threads. */ 187 void setActiveThreads(std::list<ThreadID> *at_ptr); 188 189 /** Sets pointer to the commited state rename map. */ 190 void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]); 191 192 /** Sets pointer to the ROB. */ 193 void setROB(ROB *rob_ptr); 194 195 /** Initializes stage by sending back the number of free entries. */ 196 void initStage(); 197 198 /** Initializes the draining of commit. */ 199 bool drain(); 200 201 /** Resumes execution after draining. */ 202 void resume(); 203 204 /** Completes the switch out of commit. */ 205 void switchOut(); 206 207 /** Takes over from another CPU's thread. */ 208 void takeOverFrom(); 209 210 /** Ticks the commit stage, which tries to commit instructions. */ 211 void tick(); 212 213 /** Handles any squashes that are sent from IEW, and adds instructions 214 * to the ROB and tries to commit instructions. 215 */ 216 void commit(); 217 218 /** Returns the number of free ROB entries for a specific thread. */ 219 size_t numROBFreeEntries(ThreadID tid); 220 221 /** Generates an event to schedule a squash due to a trap. */ 222 void generateTrapEvent(ThreadID tid); 223 224 /** Records that commit needs to initiate a squash due to an 225 * external state update through the TC. 226 */ 227 void generateTCEvent(ThreadID tid); 228 229 private: 230 /** Updates the overall status of commit with the nextStatus, and 231 * tell the CPU if commit is active/inactive. 232 */ 233 void updateStatus(); 234 235 /** Sets the next status based on threads' statuses, which becomes the 236 * current status at the end of the cycle. 237 */ 238 void setNextStatus(); 239 240 /** Checks if the ROB is completed with squashing. This is for the case 241 * where the ROB can take multiple cycles to complete squashing. 242 */ 243 bool robDoneSquashing(); 244 245 /** Returns if any of the threads have the number of ROB entries changed 246 * on this cycle. Used to determine if the number of free ROB entries needs 247 * to be sent back to previous stages. 248 */ 249 bool changedROBEntries(); 250 251 /** Squashes all in flight instructions. */ 252 void squashAll(ThreadID tid); 253 254 /** Handles squashing due to a trap. */ 255 void squashFromTrap(ThreadID tid); 256 257 /** Handles squashing due to an TC write. */ 258 void squashFromTC(ThreadID tid); 259 260 /** Handles squashing from instruction with SquashAfter set. 261 * This differs from the other squashes as it squashes following 262 * instructions instead of the current instruction and doesn't 263 * clean up various status bits about traps/tc writes pending. 264 */ 265 void squashAfter(ThreadID tid, uint64_t squash_after_seq_num); 266 267#if FULL_SYSTEM 268 /** Handles processing an interrupt. */ 269 void handleInterrupt(); 270 271 /** Get fetch redirecting so we can handle an interrupt */ 272 void propagateInterrupt(); 273#endif // FULL_SYSTEM 274 275 /** Commits as many instructions as possible. */ 276 void commitInsts(); 277 278 /** Tries to commit the head ROB instruction passed in. 279 * @param head_inst The instruction to be committed. 280 */ 281 bool commitHead(DynInstPtr &head_inst, unsigned inst_num); 282 283 /** Gets instructions from rename and inserts them into the ROB. */ 284 void getInsts(); 285 286 /** Insert all instructions from rename into skidBuffer */ 287 void skidInsert(); 288 289 /** Marks completed instructions using information sent from IEW. */ 290 void markCompletedInsts(); 291 292 /** Gets the thread to commit, based on the SMT policy. */ 293 ThreadID getCommittingThread(); 294 295 /** Returns the thread ID to use based on a round robin policy. */ 296 ThreadID roundRobin(); 297 298 /** Returns the thread ID to use based on an oldest instruction policy. */ 299 ThreadID oldestReady(); 300 301 public: 302 /** Reads the PC of a specific thread. */ 303 TheISA::PCState pcState(ThreadID tid) { return pc[tid]; } 304 305 /** Sets the PC of a specific thread. */ 306 void pcState(const TheISA::PCState &val, ThreadID tid) 307 { pc[tid] = val; } 308 309 /** Returns the PC of a specific thread. */ 310 Addr instAddr(ThreadID tid) { return pc[tid].instAddr(); } 311 312 /** Returns the next PC of a specific thread. */ 313 Addr nextInstAddr(ThreadID tid) { return pc[tid].nextInstAddr(); } 314 315 /** Reads the micro PC of a specific thread. */ 316 Addr microPC(ThreadID tid) { return pc[tid].microPC(); } 317 318 private: 319 /** Time buffer interface. */ 320 TimeBuffer<TimeStruct> *timeBuffer; 321 322 /** Wire to write information heading to previous stages. */ 323 typename TimeBuffer<TimeStruct>::wire toIEW; 324 325 /** Wire to read information from IEW (for ROB). */ 326 typename TimeBuffer<TimeStruct>::wire robInfoFromIEW; 327 328 TimeBuffer<FetchStruct> *fetchQueue; 329 330 typename TimeBuffer<FetchStruct>::wire fromFetch; 331 332 /** IEW instruction queue interface. */ 333 TimeBuffer<IEWStruct> *iewQueue; 334 335 /** Wire to read information from IEW queue. */ 336 typename TimeBuffer<IEWStruct>::wire fromIEW; 337 338 /** Rename instruction queue interface, for ROB. */ 339 TimeBuffer<RenameStruct> *renameQueue; 340 341 /** Wire to read information from rename queue. */ 342 typename TimeBuffer<RenameStruct>::wire fromRename; 343 344 public: 345 /** ROB interface. */ 346 ROB *rob; 347 348 private: 349 /** Pointer to O3CPU. */ 350 O3CPU *cpu; 351 352 /** Vector of all of the threads. */ 353 std::vector<Thread *> thread; 354 355 /** Records that commit has written to the time buffer this cycle. Used for 356 * the CPU to determine if it can deschedule itself if there is no activity. 357 */ 358 bool wroteToTimeBuffer; 359 360 /** Records if the number of ROB entries has changed this cycle. If it has, 361 * then the number of free entries must be re-broadcast. 362 */ 363 bool changedROBNumEntries[Impl::MaxThreads]; 364 365 /** A counter of how many threads are currently squashing. */ 366 ThreadID squashCounter; 367 368 /** Records if a thread has to squash this cycle due to a trap. */ 369 bool trapSquash[Impl::MaxThreads]; 370 371 /** Records if a thread has to squash this cycle due to an XC write. */ 372 bool tcSquash[Impl::MaxThreads]; 373 374 /** Priority List used for Commit Policy */ 375 std::list<ThreadID> priority_list; 376 377 /** IEW to Commit delay, in ticks. */ 378 unsigned iewToCommitDelay; 379 380 /** Commit to IEW delay, in ticks. */ 381 unsigned commitToIEWDelay; 382 383 /** Rename to ROB delay, in ticks. */ 384 unsigned renameToROBDelay; 385 386 unsigned fetchToCommitDelay; 387 388 /** Rename width, in instructions. Used so ROB knows how many 389 * instructions to get from the rename instruction queue. 390 */ 391 unsigned renameWidth; 392 393 /** Commit width, in instructions. */ 394 unsigned commitWidth; 395 396 /** Number of Reorder Buffers */ 397 unsigned numRobs; 398 399 /** Number of Active Threads */ 400 ThreadID numThreads; 401 402 /** Is a drain pending. */ 403 bool drainPending; 404 405 /** Is commit switched out. */ 406 bool switchedOut; 407 408 /** The latency to handle a trap. Used when scheduling trap 409 * squash event. 410 */ 411 Tick trapLatency; 412 413 /** The interrupt fault. */ 414 Fault interrupt; 415 416 /** The commit PC state of each thread. Refers to the instruction that 417 * is currently being processed/committed. 418 */ 419 TheISA::PCState pc[Impl::MaxThreads]; 420 421 /** The sequence number of the youngest valid instruction in the ROB. */ 422 InstSeqNum youngestSeqNum[Impl::MaxThreads]; 423 424 /** The sequence number of the last commited instruction. */ 425 InstSeqNum lastCommitedSeqNum[Impl::MaxThreads]; 426 427 /** Records if there is a trap currently in flight. */ 428 bool trapInFlight[Impl::MaxThreads]; 429 430 /** Records if there were any stores committed this cycle. */ 431 bool committedStores[Impl::MaxThreads]; 432 433 /** Records if commit should check if the ROB is truly empty (see 434 commit_impl.hh). */ 435 bool checkEmptyROB[Impl::MaxThreads]; 436 437 /** Pointer to the list of active threads. */ 438 std::list<ThreadID> *activeThreads; 439 440 /** Rename map interface. */ 441 RenameMap *renameMap[Impl::MaxThreads]; 442 443 /** Updates commit stats based on this instruction. */ 444 void updateComInstStats(DynInstPtr &inst); 445 446 /** Stat for the total number of committed instructions. */ 447 Stats::Scalar commitCommittedInsts; 448 /** Stat for the total number of squashed instructions discarded by commit. 449 */ 450 Stats::Scalar commitSquashedInsts; 451 /** Stat for the total number of times commit is told to squash. 452 * @todo: Actually increment this stat. 453 */ 454 Stats::Scalar commitSquashEvents; 455 /** Stat for the total number of times commit has had to stall due to a non- 456 * speculative instruction reaching the head of the ROB. 457 */ 458 Stats::Scalar commitNonSpecStalls; 459 /** Stat for the total number of branch mispredicts that caused a squash. */ 460 Stats::Scalar branchMispredicts; 461 /** Distribution of the number of committed instructions each cycle. */ 462 Stats::Distribution numCommittedDist; 463 464 /** Total number of instructions committed. */ 465 Stats::Vector statComInst; 466 /** Total number of software prefetches committed. */ 467 Stats::Vector statComSwp; 468 /** Stat for the total number of committed memory references. */ 469 Stats::Vector statComRefs; 470 /** Stat for the total number of committed loads. */ 471 Stats::Vector statComLoads; 472 /** Total number of committed memory barriers. */ 473 Stats::Vector statComMembars; 474 /** Total number of committed branches. */ 475 Stats::Vector statComBranches; 476 477 /** Number of cycles where the commit bandwidth limit is reached. */ 478 Stats::Scalar commitEligibleSamples; 479 /** Number of instructions not committed due to bandwidth limits. */ 480 Stats::Vector commitEligible; 481}; 482 483#endif // __CPU_O3_COMMIT_HH__ 484