2 * Copyright (c) 2007 MIPS Technologies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Korey Sewell
32 #ifndef __CPU_INORDER_CPU_HH__
33 #define __CPU_INORDER_CPU_HH__
41 #include "arch/isa_traits.hh"
42 #include "arch/registers.hh"
43 #include "arch/types.hh"
44 #include "base/statistics.hh"
45 #include "base/types.hh"
46 #include "config/full_system.hh"
47 #include "config/the_isa.hh"
48 #include "cpu/inorder/inorder_dyn_inst.hh"
49 #include "cpu/inorder/pipeline_stage.hh"
50 #include "cpu/inorder/pipeline_traits.hh"
51 #include "cpu/inorder/reg_dep_map.hh"
52 #include "cpu/inorder/thread_state.hh"
53 #include "cpu/o3/dep_graph.hh"
54 #include "cpu/o3/rename_map.hh"
55 #include "cpu/activity.hh"
56 #include "cpu/base.hh"
57 #include "cpu/simple_thread.hh"
58 #include "cpu/timebuf.hh"
59 #include "mem/packet.hh"
60 #include "mem/port.hh"
61 #include "mem/request.hh"
62 #include "sim/eventq.hh"
63 #include "sim/process.hh"
71 class InOrderCPU : public BaseCPU
75 typedef ThePipeline::Params Params;
76 typedef InOrderThreadState Thread;
79 typedef TheISA::IntReg IntReg;
80 typedef TheISA::FloatReg FloatReg;
81 typedef TheISA::FloatRegBits FloatRegBits;
82 typedef TheISA::MiscReg MiscReg;
83 typedef TheISA::RegIndex RegIndex;
86 typedef ThePipeline::DynInstPtr DynInstPtr;
87 typedef std::list<DynInstPtr>::iterator ListIt;
90 typedef TimeBuffer<InterStageStruct> StageQueue;
92 friend class Resource;
95 /** Constructs a CPU with the given parameters. */
96 InOrderCPU(Params *params);
104 ThreadID asid[ThePipeline::MaxThreads];
106 /** Type of core that this is */
107 std::string coreType;
109 // Only need for SE MODE
116 ThreadModel threadModel;
118 int readCpuId() { return cpu_id; }
120 void setCpuId(int val) { cpu_id = val; }
133 /** Overall CPU status. */
136 /** Define TickEvent for the CPU */
137 class TickEvent : public Event
140 /** Pointer to the CPU. */
144 /** Constructs a tick event. */
145 TickEvent(InOrderCPU *c);
147 /** Processes a tick event, calling tick() on the CPU. */
150 /** Returns the description of the tick event. */
151 const char *description();
154 /** The tick event used for scheduling CPU ticks. */
157 /** Schedule tick event, regardless of its current state. */
158 void scheduleTickEvent(int delay)
160 assert(!tickEvent.scheduled() || tickEvent.squashed());
161 reschedule(&tickEvent, nextCycle(curTick() + ticks(delay)), true);
164 /** Unschedule tick event, regardless of its current state. */
165 void unscheduleTickEvent()
167 if (tickEvent.scheduled())
172 // List of Events That can be scheduled from
174 // NOTE(1): The Resource Pool also uses this event list
175 // to schedule events broadcast to all resources interfaces
176 // NOTE(2): CPU Events usually need to schedule a corresponding resource
180 ActivateNextReadyThread,
191 static std::string eventNames[NumCPUEvents];
193 /** Define CPU Event */
194 class CPUEvent : public Event
200 CPUEventType cpuEventType;
207 /** Constructs a CPU event. */
208 CPUEvent(InOrderCPU *_cpu, CPUEventType e_type, Fault fault,
209 ThreadID _tid, DynInstPtr inst, unsigned event_pri_offset);
211 /** Set Type of Event To Be Scheduled */
212 void setEvent(CPUEventType e_type, Fault _fault, ThreadID _tid,
216 cpuEventType = e_type;
222 /** Processes a CPU event. */
225 /** Returns the description of the CPU event. */
226 const char *description();
228 /** Schedule Event */
229 void scheduleEvent(int delay);
231 /** Unschedule This Event */
232 void unscheduleEvent();
235 /** Schedule a CPU Event */
236 void scheduleCpuEvent(CPUEventType cpu_event, Fault fault, ThreadID tid,
237 DynInstPtr inst, unsigned delay = 0,
238 unsigned event_pri_offset = 0);
241 /** Interface between the CPU and CPU resources. */
242 ResourcePool *resPool;
244 /** Instruction used to signify that there is no *real* instruction in
246 DynInstPtr dummyInst[ThePipeline::MaxThreads];
247 DynInstPtr dummyBufferInst;
248 DynInstPtr dummyReqInst;
250 /** Used by resources to signify a denied access to a resource. */
251 ResourceRequest *dummyReq[ThePipeline::MaxThreads];
253 /** Identifies the resource id that identifies a fetch
256 unsigned fetchPortIdx;
258 /** Identifies the resource id that identifies a ITB */
261 /** Identifies the resource id that identifies a data
264 unsigned dataPortIdx;
266 /** Identifies the resource id that identifies a DTB */
269 /** The Pipeline Stages for the CPU */
270 PipelineStage *pipelineStage[ThePipeline::NumStages];
272 /** Width (processing bandwidth) of each stage */
275 /** Program Counters */
276 TheISA::PCState pc[ThePipeline::MaxThreads];
278 /** The Register File for the CPU */
280 FloatReg f[ThePipeline::MaxThreads][TheISA::NumFloatRegs];
281 FloatRegBits i[ThePipeline::MaxThreads][TheISA::NumFloatRegs];
283 TheISA::IntReg intRegs[ThePipeline::MaxThreads][TheISA::NumIntRegs];
286 TheISA::ISA isa[ThePipeline::MaxThreads];
288 /** Dependency Tracker for Integer & Floating Point Regs */
289 RegDepMap archRegDepMap[ThePipeline::MaxThreads];
291 /** Global communication structure */
292 TimeBuffer<TimeStruct> timeBuffer;
294 /** Communication structure that sits in between pipeline stages */
295 StageQueue *stageQueue[ThePipeline::NumStages-1];
297 TheISA::TLB *getITBPtr();
298 TheISA::TLB *getDTBPtr();
300 /** Accessor Type for the SkedCache */
301 typedef uint32_t SkedID;
303 /** Cache of Instruction Schedule using the instruction's name as a key */
304 static m5::hash_map<SkedID, ThePipeline::RSkedPtr> skedCache;
306 typedef m5::hash_map<SkedID, ThePipeline::RSkedPtr>::iterator SkedCacheIt;
308 /** Initialized to last iterator in map, signifying a invalid entry
311 SkedCacheIt endOfSkedIt;
313 ThePipeline::RSkedPtr frontEndSked;
315 /** Add a new instruction schedule to the schedule cache */
316 void addToSkedCache(DynInstPtr inst, ThePipeline::RSkedPtr inst_sked)
318 SkedID sked_id = genSkedID(inst);
319 assert(skedCache.find(sked_id) == skedCache.end());
320 skedCache[sked_id] = inst_sked;
324 /** Find a instruction schedule */
325 ThePipeline::RSkedPtr lookupSked(DynInstPtr inst)
327 SkedID sked_id = genSkedID(inst);
328 SkedCacheIt lookup_it = skedCache.find(sked_id);
330 if (lookup_it != endOfSkedIt) {
331 return (*lookup_it).second;
337 static const uint8_t INST_OPCLASS = 26;
338 static const uint8_t INST_LOAD = 25;
339 static const uint8_t INST_STORE = 24;
340 static const uint8_t INST_CONTROL = 23;
341 static const uint8_t INST_NONSPEC = 22;
342 static const uint8_t INST_DEST_REGS = 18;
343 static const uint8_t INST_SRC_REGS = 14;
345 inline SkedID genSkedID(DynInstPtr inst)
348 id = (inst->opClass() << INST_OPCLASS) |
349 (inst->isLoad() << INST_LOAD) |
350 (inst->isStore() << INST_STORE) |
351 (inst->isControl() << INST_CONTROL) |
352 (inst->isNonSpeculative() << INST_NONSPEC) |
353 (inst->numDestRegs() << INST_DEST_REGS) |
354 (inst->numSrcRegs() << INST_SRC_REGS);
358 ThePipeline::RSkedPtr createFrontEndSked();
359 ThePipeline::RSkedPtr createBackEndSked(DynInstPtr inst);
361 class StageScheduler {
363 ThePipeline::RSkedPtr rsked;
365 int nextTaskPriority;
368 StageScheduler(ThePipeline::RSkedPtr _rsked, int stage_num)
369 : rsked(_rsked), stageNum(stage_num),
373 void needs(int unit, int request) {
374 rsked->push(new ScheduleEntry(
375 stageNum, nextTaskPriority++, unit, request
379 void needs(int unit, int request, int param) {
380 rsked->push(new ScheduleEntry(
381 stageNum, nextTaskPriority++, unit, request, param
388 /** Registers statistics. */
391 /** Ticks CPU, calling tick() on each stage, and checking the overall
392 * activity to see if the CPU should deschedule itself.
396 /** Initialize the CPU */
399 /** Get a Memory Port */
400 Port* getPort(const std::string &if_name, int idx = 0);
403 /** HW return from error interrupt. */
404 Fault hwrei(ThreadID tid);
406 bool simPalCheck(int palFunc, ThreadID tid);
408 /** Returns the Fault for any valid interrupt. */
409 Fault getInterrupts();
411 /** Processes any an interrupt fault. */
412 void processInterrupts(Fault interrupt);
414 /** Halts the CPU. */
415 void halt() { panic("Halt not implemented!\n"); }
417 /** Update the Virt and Phys ports of all ThreadContexts to
418 * reflect change in memory connections. */
419 void updateMemPorts();
421 /** Check if this address is a valid instruction address. */
422 bool validInstAddr(Addr addr) { return true; }
424 /** Check if this address is a valid data address. */
425 bool validDataAddr(Addr addr) { return true; }
428 /** trap() - sets up a trap event on the cpuTraps to handle given fault.
429 * trapCPU() - Traps to handle given fault
431 void trap(Fault fault, ThreadID tid, DynInstPtr inst, int delay = 0);
432 void trapCPU(Fault fault, ThreadID tid, DynInstPtr inst);
434 /** Add Thread to Active Threads List. */
435 void activateContext(ThreadID tid, int delay = 0);
436 void activateThread(ThreadID tid);
437 void activateThreadInPipeline(ThreadID tid);
439 /** Add Thread to Active Threads List. */
440 void activateNextReadyContext(int delay = 0);
441 void activateNextReadyThread();
443 /** Remove from Active Thread List */
444 void deactivateContext(ThreadID tid, int delay = 0);
445 void deactivateThread(ThreadID tid);
447 /** Suspend Thread, Remove from Active Threads List, Add to Suspend List */
448 void suspendContext(ThreadID tid, int delay = 0);
449 void suspendThread(ThreadID tid);
451 /** Halt Thread, Remove from Active Thread List, Place Thread on Halted
454 void haltContext(ThreadID tid, int delay = 0);
455 void haltThread(ThreadID tid);
457 /** squashFromMemStall() - sets up a squash event
458 * squashDueToMemStall() - squashes pipeline
459 * @note: maybe squashContext/squashThread would be better?
461 void squashFromMemStall(DynInstPtr inst, ThreadID tid, int delay = 0);
462 void squashDueToMemStall(int stage_num, InstSeqNum seq_num, ThreadID tid);
464 void removePipelineStalls(ThreadID tid);
465 void squashThreadInPipeline(ThreadID tid);
466 void squashBehindMemStall(int stage_num, InstSeqNum seq_num, ThreadID tid);
468 PipelineStage* getPipeStage(int stage_num);
473 hack_once("return a bogus context id");
477 /** Update The Order In Which We Process Threads. */
478 void updateThreadPriority();
480 /** Switches a Pipeline Stage to Active. (Unused currently) */
481 void switchToActive(int stage_idx)
482 { /*pipelineStage[stage_idx]->switchToActive();*/ }
484 /** Get the current instruction sequence number, and increment it. */
485 InstSeqNum getAndIncrementInstSeq(ThreadID tid)
486 { return globalSeqNum[tid]++; }
488 /** Get the current instruction sequence number, and increment it. */
489 InstSeqNum nextInstSeqNum(ThreadID tid)
490 { return globalSeqNum[tid]; }
492 /** Increment Instruction Sequence Number */
493 void incrInstSeqNum(ThreadID tid)
494 { globalSeqNum[tid]++; }
496 /** Set Instruction Sequence Number */
497 void setInstSeqNum(ThreadID tid, InstSeqNum seq_num)
499 globalSeqNum[tid] = seq_num;
502 /** Get & Update Next Event Number */
503 InstSeqNum getNextEventNum()
506 return cpuEventNum++;
512 /** Register file accessors */
513 uint64_t readIntReg(RegIndex reg_idx, ThreadID tid);
515 FloatReg readFloatReg(RegIndex reg_idx, ThreadID tid);
517 FloatRegBits readFloatRegBits(RegIndex reg_idx, ThreadID tid);
519 void setIntReg(RegIndex reg_idx, uint64_t val, ThreadID tid);
521 void setFloatReg(RegIndex reg_idx, FloatReg val, ThreadID tid);
523 void setFloatRegBits(RegIndex reg_idx, FloatRegBits val, ThreadID tid);
525 RegIndex flattenRegIdx(RegIndex reg_idx, ThreadID tid);
527 /** Reads a miscellaneous register. */
528 MiscReg readMiscRegNoEffect(int misc_reg, ThreadID tid = 0);
530 /** Reads a misc. register, including any side effects the read
531 * might have as defined by the architecture.
533 MiscReg readMiscReg(int misc_reg, ThreadID tid = 0);
535 /** Sets a miscellaneous register. */
536 void setMiscRegNoEffect(int misc_reg, const MiscReg &val,
539 /** Sets a misc. register, including any side effects the write
540 * might have as defined by the architecture.
542 void setMiscReg(int misc_reg, const MiscReg &val, ThreadID tid = 0);
544 /** Reads a int/fp/misc reg. from another thread depending on ISA-defined
547 uint64_t readRegOtherThread(unsigned misc_reg,
548 ThreadID tid = InvalidThreadID);
550 /** Sets a int/fp/misc reg. from another thread depending on an ISA-defined
553 void setRegOtherThread(unsigned misc_reg, const MiscReg &val,
556 /** Reads the commit PC of a specific thread. */
558 pcState(ThreadID tid)
563 /** Sets the commit PC of a specific thread. */
565 pcState(const TheISA::PCState &newPC, ThreadID tid)
570 Addr instAddr(ThreadID tid) { return pc[tid].instAddr(); }
571 Addr nextInstAddr(ThreadID tid) { return pc[tid].nextInstAddr(); }
572 MicroPC microPC(ThreadID tid) { return pc[tid].microPC(); }
574 /** Function to add instruction onto the head of the list of the
575 * instructions. Used when new instructions are fetched.
577 ListIt addInst(DynInstPtr &inst);
579 /** Function to tell the CPU that an instruction has completed. */
580 void instDone(DynInstPtr inst, ThreadID tid);
582 /** Add Instructions to the CPU Remove List*/
583 void addToRemoveList(DynInstPtr &inst);
585 /** Remove an instruction from CPU */
586 void removeInst(DynInstPtr &inst);
588 /** Remove all instructions younger than the given sequence number. */
589 void removeInstsUntil(const InstSeqNum &seq_num,ThreadID tid);
591 /** Removes the instruction pointed to by the iterator. */
592 inline void squashInstIt(const ListIt &instIt, ThreadID tid);
594 /** Cleans up all instructions on the instruction remove list. */
595 void cleanUpRemovedInsts();
597 /** Cleans up all events on the CPU event remove list. */
598 void cleanUpRemovedEvents();
600 /** Debug function to print all instructions on the list. */
603 /** Forwards an instruction read to the appropriate data
604 * resource (indexes into Resource Pool thru "dataPortIdx")
606 Fault read(DynInstPtr inst, Addr addr,
607 uint8_t *data, unsigned size, unsigned flags);
609 /** Forwards an instruction write. to the appropriate data
610 * resource (indexes into Resource Pool thru "dataPortIdx")
612 Fault write(DynInstPtr inst, uint8_t *data, unsigned size,
613 Addr addr, unsigned flags, uint64_t *write_res = NULL);
615 /** Executes a syscall.*/
616 void syscall(int64_t callnum, ThreadID tid);
619 /** Per-Thread List of all the instructions in flight. */
620 std::list<DynInstPtr> instList[ThePipeline::MaxThreads];
622 /** List of all the instructions that will be removed at the end of this
625 std::queue<ListIt> removeList;
627 /** List of all the cpu event requests that will be removed at the end of
630 std::queue<Event*> cpuEventRemoveList;
632 /** Records if instructions need to be removed this cycle due to
633 * being retired or squashed.
635 bool removeInstsThisCycle;
637 /** True if there is non-speculative Inst Active In Pipeline. Lets any
638 * execution unit know, NOT to execute while the instruction is active.
640 bool nonSpecInstActive[ThePipeline::MaxThreads];
642 /** Instruction Seq. Num of current non-speculative instruction. */
643 InstSeqNum nonSpecSeqNum[ThePipeline::MaxThreads];
645 /** Instruction Seq. Num of last instruction squashed in pipeline */
646 InstSeqNum squashSeqNum[ThePipeline::MaxThreads];
648 /** Last Cycle that the CPU squashed instruction end. */
649 Tick lastSquashCycle[ThePipeline::MaxThreads];
651 std::list<ThreadID> fetchPriorityList;
654 /** Active Threads List */
655 std::list<ThreadID> activeThreads;
657 /** Ready Threads List */
658 std::list<ThreadID> readyThreads;
660 /** Suspended Threads List */
661 std::list<ThreadID> suspendedThreads;
663 /** Halted Threads List */
664 std::list<ThreadID> haltedThreads;
666 /** Thread Status Functions */
667 bool isThreadActive(ThreadID tid);
668 bool isThreadReady(ThreadID tid);
669 bool isThreadSuspended(ThreadID tid);
672 /** The activity recorder; used to tell if the CPU has any
673 * activity remaining or if it can go to idle and deschedule
676 ActivityRecorder activityRec;
679 /** Number of Active Threads in the CPU */
680 ThreadID numActiveThreads() { return activeThreads.size(); }
682 /** Thread id of active thread
683 * Only used for SwitchOnCacheMiss model.
684 * Assumes only 1 thread active
686 ThreadID activeThreadId()
688 if (numActiveThreads() > 0)
689 return activeThreads.front();
691 return InvalidThreadID;
695 /** Records that there was time buffer activity this cycle. */
696 void activityThisCycle() { activityRec.activity(); }
698 /** Changes a stage's status to active within the activity recorder. */
699 void activateStage(const int idx)
700 { activityRec.activateStage(idx); }
702 /** Changes a stage's status to inactive within the activity recorder. */
703 void deactivateStage(const int idx)
704 { activityRec.deactivateStage(idx); }
706 /** Wakes the CPU, rescheduling the CPU if it's not already active. */
710 virtual void wakeup();
713 // LL/SC debug functionality
714 unsigned stCondFails;
716 unsigned readStCondFailures()
717 { return stCondFails; }
719 unsigned setStCondFailures(unsigned st_fails)
720 { return stCondFails = st_fails; }
722 /** Returns a pointer to a thread context. */
723 ThreadContext *tcBase(ThreadID tid = 0)
725 return thread[tid]->getTC();
728 /** Count the Total Instructions Committed in the CPU. */
729 virtual Counter totalInstructions() const
733 for (ThreadID tid = 0; tid < (ThreadID)thread.size(); tid++)
734 total += thread[tid]->numInst;
740 /** Pointer to the system. */
743 /** Pointer to physical memory. */
744 PhysicalMemory *physmem;
747 /** The global sequence number counter. */
748 InstSeqNum globalSeqNum[ThePipeline::MaxThreads];
751 /** The global event number counter. */
752 InstSeqNum cpuEventNum;
754 /** Number of resource requests active in CPU **/
755 unsigned resReqCount;
758 /** Counter of how many stages have completed switching out. */
761 /** Pointers to all of the threads in the CPU. */
762 std::vector<Thread *> thread;
764 /** Pointer to the icache interface. */
765 MemInterface *icacheInterface;
767 /** Pointer to the dcache interface. */
768 MemInterface *dcacheInterface;
770 /** Whether or not the CPU should defer its registration. */
771 bool deferRegistration;
773 /** Per-Stage Instruction Tracing */
776 /** The cycle that the CPU was last running, used for statistics. */
777 Tick lastRunningCycle;
779 void updateContextSwitchStats();
780 unsigned instsPerSwitch;
781 Stats::Average instsPerCtxtSwitch;
782 Stats::Scalar numCtxtSwitches;
784 /** Update Thread , used for statistic purposes*/
785 inline void tickThreadStats();
787 /** Per-Thread Tick */
788 Stats::Vector threadCycles;
791 Stats::Scalar smtCycles;
793 /** Stat for total number of times the CPU is descheduled. */
794 Stats::Scalar timesIdled;
796 /** Stat for total number of cycles the CPU spends descheduled or no
799 Stats::Scalar idleCycles;
801 /** Stat for total number of cycles the CPU is active. */
802 Stats::Scalar runCycles;
804 /** Percentage of cycles a stage was active */
805 Stats::Formula activity;
807 /** Instruction Mix Stats */
808 Stats::Scalar comLoads;
809 Stats::Scalar comStores;
810 Stats::Scalar comBranches;
811 Stats::Scalar comNops;
812 Stats::Scalar comNonSpec;
813 Stats::Scalar comInts;
814 Stats::Scalar comFloats;
816 /** Stat for the number of committed instructions per thread. */
817 Stats::Vector committedInsts;
819 /** Stat for the number of committed instructions per thread. */
820 Stats::Vector smtCommittedInsts;
822 /** Stat for the total number of committed instructions. */
823 Stats::Scalar totalCommittedInsts;
825 /** Stat for the CPI per thread. */
828 /** Stat for the SMT-CPI per thread. */
829 Stats::Formula smtCpi;
831 /** Stat for the total CPI. */
832 Stats::Formula totalCpi;
834 /** Stat for the IPC per thread. */
837 /** Stat for the total IPC. */
838 Stats::Formula smtIpc;
840 /** Stat for the total IPC. */
841 Stats::Formula totalIpc;
844 #endif // __CPU_O3_CPU_HH__