cpu.hh revision 2905:62879b0282eb
12SN/A/*
21762SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
292665Ssaidi@eecs.umich.edu *          Korey Sewell
302SN/A */
312SN/A
322SN/A#ifndef __CPU_O3_CPU_HH__
332SN/A#define __CPU_O3_CPU_HH__
342SN/A
352SN/A#include <iostream>
362SN/A#include <list>
371492SN/A#include <queue>
381858SN/A#include <set>
391717SN/A#include <vector>
402680Sktlim@umich.edu
4156SN/A#include "arch/isa_traits.hh"
421492SN/A#include "base/statistics.hh"
431696SN/A#include "base/timebuf.hh"
442190SN/A#include "config/full_system.hh"
452SN/A#include "cpu/activity.hh"
462SN/A#include "cpu/base.hh"
472SN/A#include "cpu/simple_thread.hh"
482SN/A#include "cpu/o3/comm.hh"
492SN/A#include "cpu/o3/cpu_policy.hh"
502SN/A#include "cpu/o3/scoreboard.hh"
512SN/A#include "cpu/o3/thread_state.hh"
522SN/A//#include "cpu/o3/thread_context.hh"
532SN/A#include "sim/process.hh"
542SN/A
552SN/Atemplate <class>
562SN/Aclass Checker;
572SN/Aclass ThreadContext;
582SN/Atemplate <class>
592SN/Aclass O3ThreadContext;
602SN/A
612SN/Aclass Checkpoint;
622SN/Aclass MemObject;
632SN/Aclass Process;
642SN/A
652SN/Aclass BaseO3CPU : public BaseCPU
662SN/A{
672SN/A    //Stuff that's pretty ISA independent will go here.
682SN/A  public:
692SN/A    typedef BaseCPU::Params Params;
702SN/A
712SN/A    BaseO3CPU(Params *params);
722SN/A
732SN/A    void regStats();
742SN/A
752SN/A    /** Sets this CPU's ID. */
762SN/A    void setCpuId(int id) { cpu_id = id; }
772SN/A
782SN/A    /** Reads this CPU's ID. */
792SN/A    int readCpuId() { return cpu_id; }
802SN/A
812SN/A  protected:
822SN/A    int cpu_id;
832SN/A};
842680Sktlim@umich.edu
852SN/A/**
862680Sktlim@umich.edu * FullO3CPU class, has each of the stages (fetch through commit)
872SN/A * within it, as well as all of the time buffers between stages.  The
882SN/A * tick() function for the CPU is defined here.
892SN/A */
902SN/Atemplate <class Impl>
912SN/Aclass FullO3CPU : public BaseO3CPU
922SN/A{
932SN/A  public:
942680Sktlim@umich.edu    typedef TheISA::FloatReg FloatReg;
952SN/A    typedef TheISA::FloatRegBits FloatRegBits;
962SN/A
972SN/A    // Typedefs from the Impl here.
982SN/A    typedef typename Impl::CPUPol CPUPolicy;
992SN/A    typedef typename Impl::Params Params;
1002680Sktlim@umich.edu    typedef typename Impl::DynInstPtr DynInstPtr;
1012SN/A
1022SN/A    typedef O3ThreadState<Impl> Thread;
1032SN/A
1042SN/A    typedef typename std::list<DynInstPtr>::iterator ListIt;
1052SN/A
1062SN/A    friend class O3ThreadContext<Impl>;
1072SN/A
1082SN/A  public:
1092SN/A    enum Status {
1102SN/A        Running,
1112SN/A        Idle,
1122SN/A        Halted,
1132SN/A        Blocked,
1142SN/A        SwitchedOut
1152SN/A    };
1162SN/A
1172SN/A    /** Overall CPU status. */
1182SN/A    Status _status;
1192SN/A
1202SN/A    /** Per-thread status in CPU, used for SMT.  */
1212SN/A    Status _threadStatus[Impl::MaxThreads];
1222SN/A
1232SN/A  private:
1241885SN/A    class TickEvent : public Event
1251885SN/A    {
1261885SN/A      private:
1272SN/A        /** Pointer to the CPU. */
1282SN/A        FullO3CPU<Impl> *cpu;
1292SN/A
1302SN/A      public:
1312680Sktlim@umich.edu        /** Constructs a tick event. */
1322SN/A        TickEvent(FullO3CPU<Impl> *c);
1332680Sktlim@umich.edu
1341646SN/A        /** Processes a tick event, calling tick() on the CPU. */
1352SN/A        void process();
1362SN/A        /** Returns the description of the tick event. */
1372SN/A        const char *description();
1382SN/A    };
1392SN/A
1401858SN/A    /** The tick event used for scheduling CPU ticks. */
1412SN/A    TickEvent tickEvent;
1422SN/A
1432130SN/A    /** Schedule tick event, regardless of its current state. */
1442SN/A    void scheduleTickEvent(int delay)
1451885SN/A    {
1462SN/A        if (tickEvent.squashed())
1472SN/A            tickEvent.reschedule(curTick + cycles(delay));
1482SN/A        else if (!tickEvent.scheduled())
1492SN/A            tickEvent.schedule(curTick + cycles(delay));
1502130SN/A    }
1512SN/A
1522SN/A    /** Unschedule tick event, regardless of its current state. */
1532SN/A    void unscheduleTickEvent()
1542SN/A    {
1552SN/A        if (tickEvent.scheduled())
1562SN/A            tickEvent.squash();
1572SN/A    }
1582SN/A
159    class ActivateThreadEvent : public Event
160    {
161      private:
162        /** Number of Thread to Activate */
163        int tid;
164
165        /** Pointer to the CPU. */
166        FullO3CPU<Impl> *cpu;
167
168      public:
169        /** Constructs the event. */
170        ActivateThreadEvent();
171
172        /** Initialize Event */
173        void init(int thread_num, FullO3CPU<Impl> *thread_cpu);
174
175        /** Processes the event, calling activateThread() on the CPU. */
176        void process();
177
178        /** Returns the description of the event. */
179        const char *description();
180    };
181
182    /** Schedule thread to activate , regardless of its current state. */
183    void scheduleActivateThreadEvent(int tid, int delay)
184    {
185        // Schedule thread to activate, regardless of its current state.
186        if (activateThreadEvent[tid].squashed())
187            activateThreadEvent[tid].reschedule(curTick + cycles(delay));
188        else if (!activateThreadEvent[tid].scheduled())
189            activateThreadEvent[tid].schedule(curTick + cycles(delay));
190    }
191
192    /** Unschedule actiavte thread event, regardless of its current state. */
193    void unscheduleActivateThreadEvent(int tid)
194    {
195        if (activateThreadEvent[tid].scheduled())
196            activateThreadEvent[tid].squash();
197    }
198
199    /** The tick event used for scheduling CPU ticks. */
200    ActivateThreadEvent activateThreadEvent[Impl::MaxThreads];
201
202    class DeallocateContextEvent : public Event
203    {
204      private:
205        /** Number of Thread to Activate */
206        int tid;
207
208        /** Pointer to the CPU. */
209        FullO3CPU<Impl> *cpu;
210
211      public:
212        /** Constructs the event. */
213        DeallocateContextEvent();
214
215        /** Initialize Event */
216        void init(int thread_num, FullO3CPU<Impl> *thread_cpu);
217
218        /** Processes the event, calling activateThread() on the CPU. */
219        void process();
220
221        /** Returns the description of the event. */
222        const char *description();
223    };
224
225    /** Schedule cpu to deallocate thread context.*/
226    void scheduleDeallocateContextEvent(int tid, int delay)
227    {
228        // Schedule thread to activate, regardless of its current state.
229        if (deallocateContextEvent[tid].squashed())
230            deallocateContextEvent[tid].reschedule(curTick + cycles(delay));
231        else if (!deallocateContextEvent[tid].scheduled())
232            deallocateContextEvent[tid].schedule(curTick + cycles(delay));
233    }
234
235    /** Unschedule thread deallocation in CPU */
236    void unscheduleDeallocateContextEvent(int tid)
237    {
238        if (deallocateContextEvent[tid].scheduled())
239            deallocateContextEvent[tid].squash();
240    }
241
242    /** The tick event used for scheduling CPU ticks. */
243    DeallocateContextEvent deallocateContextEvent[Impl::MaxThreads];
244
245  public:
246    /** Constructs a CPU with the given parameters. */
247    FullO3CPU(Params *params);
248    /** Destructor. */
249    ~FullO3CPU();
250
251    /** Registers statistics. */
252    void fullCPURegStats();
253
254    /** Returns a specific port. */
255    Port *getPort(const std::string &if_name, int idx);
256
257    /** Ticks CPU, calling tick() on each stage, and checking the overall
258     *  activity to see if the CPU should deschedule itself.
259     */
260    void tick();
261
262    /** Initialize the CPU */
263    void init();
264
265    /** Returns the Number of Active Threads in the CPU */
266    int numActiveThreads()
267    { return activeThreads.size(); }
268
269    /** Add Thread to Active Threads List */
270    void activateThread(unsigned tid);
271
272    /** Remove Thread from Active Threads List */
273    void deactivateThread(unsigned tid);
274
275    /** Setup CPU to insert a thread's context */
276    void insertThread(unsigned tid);
277
278    /** Remove all of a thread's context from CPU */
279    void removeThread(unsigned tid);
280
281    /** Count the Total Instructions Committed in the CPU. */
282    virtual Counter totalInstructions() const
283    {
284        Counter total(0);
285
286        for (int i=0; i < thread.size(); i++)
287            total += thread[i]->numInst;
288
289        return total;
290    }
291
292    /** Add Thread to Active Threads List. */
293    void activateContext(int tid, int delay);
294
295    /** Remove Thread from Active Threads List */
296    void suspendContext(int tid);
297
298    /** Remove Thread from Active Threads List &&
299     *  Remove Thread Context from CPU.
300     */
301    void deallocateContext(int tid, int delay = 1);
302
303    /** Remove Thread from Active Threads List &&
304     *  Remove Thread Context from CPU.
305     */
306    void haltContext(int tid);
307
308    /** Activate a Thread When CPU Resources are Available. */
309    void activateWhenReady(int tid);
310
311    /** Add or Remove a Thread Context in the CPU. */
312    void doContextSwitch();
313
314    /** Update The Order In Which We Process Threads. */
315    void updateThreadPriority();
316
317    /** Serialize state. */
318    virtual void serialize(std::ostream &os);
319
320    /** Unserialize from a checkpoint. */
321    virtual void unserialize(Checkpoint *cp, const std::string &section);
322
323  public:
324    /** Executes a syscall on this cycle.
325     *  ---------------------------------------
326     *  Note: this is a virtual function. CPU-Specific
327     *  functionality defined in derived classes
328     */
329    virtual void syscall(int tid) { panic("Unimplemented!"); }
330
331    /** Starts draining the CPU's pipeline of all instructions in
332     * order to stop all memory accesses. */
333    virtual unsigned int drain(Event *drain_event);
334
335    /** Resumes execution after a drain. */
336    virtual void resume();
337
338    /** Signals to this CPU that a stage has completed switching out. */
339    void signalDrained();
340
341    /** Switches out this CPU. */
342    virtual void switchOut();
343
344    /** Takes over from another CPU. */
345    virtual void takeOverFrom(BaseCPU *oldCPU);
346
347    /** Get the current instruction sequence number, and increment it. */
348    InstSeqNum getAndIncrementInstSeq()
349    { return globalSeqNum++; }
350
351#if FULL_SYSTEM
352    /** Check if this address is a valid instruction address. */
353    bool validInstAddr(Addr addr) { return true; }
354
355    /** Check if this address is a valid data address. */
356    bool validDataAddr(Addr addr) { return true; }
357
358    /** Get instruction asid. */
359    int getInstAsid(unsigned tid)
360    { return regFile.miscRegs[tid].getInstAsid(); }
361
362    /** Get data asid. */
363    int getDataAsid(unsigned tid)
364    { return regFile.miscRegs[tid].getDataAsid(); }
365#else
366    /** Get instruction asid. */
367    int getInstAsid(unsigned tid)
368    { return thread[tid]->getInstAsid(); }
369
370    /** Get data asid. */
371    int getDataAsid(unsigned tid)
372    { return thread[tid]->getDataAsid(); }
373
374#endif
375
376    /** Register accessors.  Index refers to the physical register index. */
377    uint64_t readIntReg(int reg_idx);
378
379    FloatReg readFloatReg(int reg_idx);
380
381    FloatReg readFloatReg(int reg_idx, int width);
382
383    FloatRegBits readFloatRegBits(int reg_idx);
384
385    FloatRegBits readFloatRegBits(int reg_idx, int width);
386
387    void setIntReg(int reg_idx, uint64_t val);
388
389    void setFloatReg(int reg_idx, FloatReg val);
390
391    void setFloatReg(int reg_idx, FloatReg val, int width);
392
393    void setFloatRegBits(int reg_idx, FloatRegBits val);
394
395    void setFloatRegBits(int reg_idx, FloatRegBits val, int width);
396
397    uint64_t readArchIntReg(int reg_idx, unsigned tid);
398
399    float readArchFloatRegSingle(int reg_idx, unsigned tid);
400
401    double readArchFloatRegDouble(int reg_idx, unsigned tid);
402
403    uint64_t readArchFloatRegInt(int reg_idx, unsigned tid);
404
405    /** Architectural register accessors.  Looks up in the commit
406     * rename table to obtain the true physical index of the
407     * architected register first, then accesses that physical
408     * register.
409     */
410    void setArchIntReg(int reg_idx, uint64_t val, unsigned tid);
411
412    void setArchFloatRegSingle(int reg_idx, float val, unsigned tid);
413
414    void setArchFloatRegDouble(int reg_idx, double val, unsigned tid);
415
416    void setArchFloatRegInt(int reg_idx, uint64_t val, unsigned tid);
417
418    /** Reads the commit PC of a specific thread. */
419    uint64_t readPC(unsigned tid);
420
421    /** Sets the commit PC of a specific thread. */
422    void setPC(Addr new_PC, unsigned tid);
423
424    /** Reads the next PC of a specific thread. */
425    uint64_t readNextPC(unsigned tid);
426
427    /** Sets the next PC of a specific thread. */
428    void setNextPC(uint64_t val, unsigned tid);
429
430    /** Reads the next NPC of a specific thread. */
431    uint64_t readNextNPC(unsigned tid);
432
433    /** Sets the next NPC of a specific thread. */
434    void setNextNPC(uint64_t val, unsigned tid);
435
436    /** Function to add instruction onto the head of the list of the
437     *  instructions.  Used when new instructions are fetched.
438     */
439    ListIt addInst(DynInstPtr &inst);
440
441    /** Function to tell the CPU that an instruction has completed. */
442    void instDone(unsigned tid);
443
444    /** Add Instructions to the CPU Remove List*/
445    void addToRemoveList(DynInstPtr &inst);
446
447    /** Remove an instruction from the front end of the list.  There's
448     *  no restriction on location of the instruction.
449     */
450    void removeFrontInst(DynInstPtr &inst);
451
452    /** Remove all instructions that are not currently in the ROB. */
453    void removeInstsNotInROB(unsigned tid);
454
455    /** Remove all instructions younger than the given sequence number. */
456    void removeInstsUntil(const InstSeqNum &seq_num,unsigned tid);
457
458    /** Removes the instruction pointed to by the iterator. */
459    inline void squashInstIt(const ListIt &instIt, const unsigned &tid);
460
461    /** Cleans up all instructions on the remove list. */
462    void cleanUpRemovedInsts();
463
464    /** Debug function to print all instructions on the list. */
465    void dumpInsts();
466
467  public:
468    /** List of all the instructions in flight. */
469    std::list<DynInstPtr> instList;
470
471    /** List of all the instructions that will be removed at the end of this
472     *  cycle.
473     */
474    std::queue<ListIt> removeList;
475
476#ifdef DEBUG
477    /** Debug structure to keep track of the sequence numbers still in
478     * flight.
479     */
480    std::set<InstSeqNum> snList;
481#endif
482
483    /** Records if instructions need to be removed this cycle due to
484     *  being retired or squashed.
485     */
486    bool removeInstsThisCycle;
487
488  protected:
489    /** The fetch stage. */
490    typename CPUPolicy::Fetch fetch;
491
492    /** The decode stage. */
493    typename CPUPolicy::Decode decode;
494
495    /** The dispatch stage. */
496    typename CPUPolicy::Rename rename;
497
498    /** The issue/execute/writeback stages. */
499    typename CPUPolicy::IEW iew;
500
501    /** The commit stage. */
502    typename CPUPolicy::Commit commit;
503
504    /** The register file. */
505    typename CPUPolicy::RegFile regFile;
506
507    /** The free list. */
508    typename CPUPolicy::FreeList freeList;
509
510    /** The rename map. */
511    typename CPUPolicy::RenameMap renameMap[Impl::MaxThreads];
512
513    /** The commit rename map. */
514    typename CPUPolicy::RenameMap commitRenameMap[Impl::MaxThreads];
515
516    /** The re-order buffer. */
517    typename CPUPolicy::ROB rob;
518
519    /** Active Threads List */
520    std::list<unsigned> activeThreads;
521
522    /** Integer Register Scoreboard */
523    Scoreboard scoreboard;
524
525  public:
526    /** Enum to give each stage a specific index, so when calling
527     *  activateStage() or deactivateStage(), they can specify which stage
528     *  is being activated/deactivated.
529     */
530    enum StageIdx {
531        FetchIdx,
532        DecodeIdx,
533        RenameIdx,
534        IEWIdx,
535        CommitIdx,
536        NumStages };
537
538    /** Typedefs from the Impl to get the structs that each of the
539     *  time buffers should use.
540     */
541    typedef typename CPUPolicy::TimeStruct TimeStruct;
542
543    typedef typename CPUPolicy::FetchStruct FetchStruct;
544
545    typedef typename CPUPolicy::DecodeStruct DecodeStruct;
546
547    typedef typename CPUPolicy::RenameStruct RenameStruct;
548
549    typedef typename CPUPolicy::IEWStruct IEWStruct;
550
551    /** The main time buffer to do backwards communication. */
552    TimeBuffer<TimeStruct> timeBuffer;
553
554    /** The fetch stage's instruction queue. */
555    TimeBuffer<FetchStruct> fetchQueue;
556
557    /** The decode stage's instruction queue. */
558    TimeBuffer<DecodeStruct> decodeQueue;
559
560    /** The rename stage's instruction queue. */
561    TimeBuffer<RenameStruct> renameQueue;
562
563    /** The IEW stage's instruction queue. */
564    TimeBuffer<IEWStruct> iewQueue;
565
566  private:
567    /** The activity recorder; used to tell if the CPU has any
568     * activity remaining or if it can go to idle and deschedule
569     * itself.
570     */
571    ActivityRecorder activityRec;
572
573  public:
574    /** Records that there was time buffer activity this cycle. */
575    void activityThisCycle() { activityRec.activity(); }
576
577    /** Changes a stage's status to active within the activity recorder. */
578    void activateStage(const StageIdx idx)
579    { activityRec.activateStage(idx); }
580
581    /** Changes a stage's status to inactive within the activity recorder. */
582    void deactivateStage(const StageIdx idx)
583    { activityRec.deactivateStage(idx); }
584
585    /** Wakes the CPU, rescheduling the CPU if it's not already active. */
586    void wakeCPU();
587
588    /** Gets a free thread id. Use if thread ids change across system. */
589    int getFreeTid();
590
591  public:
592    /** Returns a pointer to a thread context. */
593    ThreadContext *tcBase(unsigned tid)
594    {
595        return thread[tid]->getTC();
596    }
597
598    /** The global sequence number counter. */
599    InstSeqNum globalSeqNum;
600
601    /** Pointer to the checker, which can dynamically verify
602     * instruction results at run time.  This can be set to NULL if it
603     * is not being used.
604     */
605    Checker<DynInstPtr> *checker;
606
607#if FULL_SYSTEM
608    /** Pointer to the system. */
609    System *system;
610
611    /** Pointer to physical memory. */
612    PhysicalMemory *physmem;
613#endif
614
615    /** Pointer to memory. */
616    MemObject *mem;
617
618    /** Event to call process() on once draining has completed. */
619    Event *drainEvent;
620
621    /** Counter of how many stages have completed draining. */
622    int drainCount;
623
624    /** Pointers to all of the threads in the CPU. */
625    std::vector<Thread *> thread;
626
627    /** Pointer to the icache interface. */
628    MemInterface *icacheInterface;
629    /** Pointer to the dcache interface. */
630    MemInterface *dcacheInterface;
631
632    /** Whether or not the CPU should defer its registration. */
633    bool deferRegistration;
634
635    /** Is there a context switch pending? */
636    bool contextSwitch;
637
638    /** Threads Scheduled to Enter CPU */
639    std::list<int> cpuWaitList;
640
641    /** The cycle that the CPU was last running, used for statistics. */
642    Tick lastRunningCycle;
643
644    /** The cycle that the CPU was last activated by a new thread*/
645    Tick lastActivatedCycle;
646
647    /** Number of Threads CPU can process */
648    unsigned numThreads;
649
650    /** Mapping for system thread id to cpu id */
651    std::map<unsigned,unsigned> threadMap;
652
653    /** Available thread ids in the cpu*/
654    std::vector<unsigned> tids;
655
656    /** Stat for total number of times the CPU is descheduled. */
657    Stats::Scalar<> timesIdled;
658    /** Stat for total number of cycles the CPU spends descheduled. */
659    Stats::Scalar<> idleCycles;
660    /** Stat for the number of committed instructions per thread. */
661    Stats::Vector<> committedInsts;
662    /** Stat for the total number of committed instructions. */
663    Stats::Scalar<> totalCommittedInsts;
664    /** Stat for the CPI per thread. */
665    Stats::Formula cpi;
666    /** Stat for the total CPI. */
667    Stats::Formula totalCpi;
668    /** Stat for the IPC per thread. */
669    Stats::Formula ipc;
670    /** Stat for the total IPC. */
671    Stats::Formula totalIpc;
672};
673
674#endif // __CPU_O3_CPU_HH__
675