cpu.hh revision 9342
11689SN/A/*
28707Sandreas.hansson@arm.com * Copyright (c) 2011 ARM Limited
38707Sandreas.hansson@arm.com * All rights reserved
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68707Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78707Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88707Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98707Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108707Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118707Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128707Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138707Sandreas.hansson@arm.com *
141689SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
157897Shestness@cs.utexas.edu * Copyright (c) 2011 Regents of the University of California
161689SN/A * All rights reserved.
171689SN/A *
181689SN/A * Redistribution and use in source and binary forms, with or without
191689SN/A * modification, are permitted provided that the following conditions are
201689SN/A * met: redistributions of source code must retain the above copyright
211689SN/A * notice, this list of conditions and the following disclaimer;
221689SN/A * redistributions in binary form must reproduce the above copyright
231689SN/A * notice, this list of conditions and the following disclaimer in the
241689SN/A * documentation and/or other materials provided with the distribution;
251689SN/A * neither the name of the copyright holders nor the names of its
261689SN/A * contributors may be used to endorse or promote products derived from
271689SN/A * this software without specific prior written permission.
281689SN/A *
291689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
301689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
311689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
321689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
331689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
341689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
351689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
361689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
371689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
381689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
391689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
422756Sksewell@umich.edu *          Korey Sewell
437897Shestness@cs.utexas.edu *          Rick Strong
441689SN/A */
451689SN/A
462325SN/A#ifndef __CPU_O3_CPU_HH__
472325SN/A#define __CPU_O3_CPU_HH__
481060SN/A
491060SN/A#include <iostream>
501060SN/A#include <list>
512292SN/A#include <queue>
522292SN/A#include <set>
531681SN/A#include <vector>
541060SN/A
552980Sgblack@eecs.umich.edu#include "arch/types.hh"
561060SN/A#include "base/statistics.hh"
576658Snate@binkert.org#include "config/the_isa.hh"
581717SN/A#include "cpu/o3/comm.hh"
591717SN/A#include "cpu/o3/cpu_policy.hh"
602292SN/A#include "cpu/o3/scoreboard.hh"
612292SN/A#include "cpu/o3/thread_state.hh"
628229Snate@binkert.org#include "cpu/activity.hh"
638229Snate@binkert.org#include "cpu/base.hh"
648229Snate@binkert.org#include "cpu/simple_thread.hh"
658229Snate@binkert.org#include "cpu/timebuf.hh"
662817Sksewell@umich.edu//#include "cpu/o3/thread_context.hh"
678229Snate@binkert.org#include "params/DerivO3CPU.hh"
681060SN/A#include "sim/process.hh"
691060SN/A
702316SN/Atemplate <class>
712316SN/Aclass Checker;
722680Sktlim@umich.educlass ThreadContext;
732817Sksewell@umich.edutemplate <class>
742817Sksewell@umich.educlass O3ThreadContext;
752843Sktlim@umich.edu
762843Sktlim@umich.educlass Checkpoint;
772669Sktlim@umich.educlass MemObject;
781060SN/Aclass Process;
791060SN/A
808737Skoansin.tan@gmail.comstruct BaseCPUParams;
815529Snate@binkert.org
822733Sktlim@umich.educlass BaseO3CPU : public BaseCPU
831060SN/A{
841060SN/A    //Stuff that's pretty ISA independent will go here.
851060SN/A  public:
865529Snate@binkert.org    BaseO3CPU(BaseCPUParams *params);
872292SN/A
882292SN/A    void regStats();
891060SN/A};
901060SN/A
912348SN/A/**
922348SN/A * FullO3CPU class, has each of the stages (fetch through commit)
932348SN/A * within it, as well as all of the time buffers between stages.  The
942348SN/A * tick() function for the CPU is defined here.
952348SN/A */
961060SN/Atemplate <class Impl>
972733Sktlim@umich.educlass FullO3CPU : public BaseO3CPU
981060SN/A{
991060SN/A  public:
1002325SN/A    // Typedefs from the Impl here.
1011060SN/A    typedef typename Impl::CPUPol CPUPolicy;
1021061SN/A    typedef typename Impl::DynInstPtr DynInstPtr;
1034329Sktlim@umich.edu    typedef typename Impl::O3CPU O3CPU;
1041060SN/A
1055595Sgblack@eecs.umich.edu    typedef O3ThreadState<Impl> ImplState;
1062292SN/A    typedef O3ThreadState<Impl> Thread;
1072292SN/A
1082292SN/A    typedef typename std::list<DynInstPtr>::iterator ListIt;
1092292SN/A
1102817Sksewell@umich.edu    friend class O3ThreadContext<Impl>;
1112829Sksewell@umich.edu
1121060SN/A  public:
1131060SN/A    enum Status {
1141060SN/A        Running,
1151060SN/A        Idle,
1161060SN/A        Halted,
1172307SN/A        Blocked,
1182307SN/A        SwitchedOut
1191060SN/A    };
1201060SN/A
1216022Sgblack@eecs.umich.edu    TheISA::TLB * itb;
1226022Sgblack@eecs.umich.edu    TheISA::TLB * dtb;
1233781Sgblack@eecs.umich.edu
1242292SN/A    /** Overall CPU status. */
1251060SN/A    Status _status;
1261060SN/A
1272829Sksewell@umich.edu    /** Per-thread status in CPU, used for SMT.  */
1282829Sksewell@umich.edu    Status _threadStatus[Impl::MaxThreads];
1292829Sksewell@umich.edu
1301060SN/A  private:
1318707Sandreas.hansson@arm.com
1328707Sandreas.hansson@arm.com    /**
1338707Sandreas.hansson@arm.com     * IcachePort class for instruction fetch.
1348707Sandreas.hansson@arm.com     */
1358707Sandreas.hansson@arm.com    class IcachePort : public CpuPort
1368707Sandreas.hansson@arm.com    {
1378707Sandreas.hansson@arm.com      protected:
1388707Sandreas.hansson@arm.com        /** Pointer to fetch. */
1398707Sandreas.hansson@arm.com        DefaultFetch<Impl> *fetch;
1408707Sandreas.hansson@arm.com
1418707Sandreas.hansson@arm.com      public:
1428707Sandreas.hansson@arm.com        /** Default constructor. */
1438707Sandreas.hansson@arm.com        IcachePort(DefaultFetch<Impl> *_fetch, FullO3CPU<Impl>* _cpu)
1449095Sandreas.hansson@arm.com            : CpuPort(_cpu->name() + ".icache_port", _cpu), fetch(_fetch)
1458707Sandreas.hansson@arm.com        { }
1468707Sandreas.hansson@arm.com
1478707Sandreas.hansson@arm.com      protected:
1488707Sandreas.hansson@arm.com
1498707Sandreas.hansson@arm.com        /** Timing version of receive.  Handles setting fetch to the
1508707Sandreas.hansson@arm.com         * proper status to start fetching. */
1518975Sandreas.hansson@arm.com        virtual bool recvTimingResp(PacketPtr pkt);
1528975Sandreas.hansson@arm.com        virtual void recvTimingSnoopReq(PacketPtr pkt) { }
1538707Sandreas.hansson@arm.com
1548707Sandreas.hansson@arm.com        /** Handles doing a retry of a failed fetch. */
1558707Sandreas.hansson@arm.com        virtual void recvRetry();
1568707Sandreas.hansson@arm.com    };
1578707Sandreas.hansson@arm.com
1588707Sandreas.hansson@arm.com    /**
1598707Sandreas.hansson@arm.com     * DcachePort class for the load/store queue.
1608707Sandreas.hansson@arm.com     */
1618707Sandreas.hansson@arm.com    class DcachePort : public CpuPort
1628707Sandreas.hansson@arm.com    {
1638707Sandreas.hansson@arm.com      protected:
1648707Sandreas.hansson@arm.com
1658707Sandreas.hansson@arm.com        /** Pointer to LSQ. */
1668707Sandreas.hansson@arm.com        LSQ<Impl> *lsq;
1678707Sandreas.hansson@arm.com
1688707Sandreas.hansson@arm.com      public:
1698707Sandreas.hansson@arm.com        /** Default constructor. */
1708707Sandreas.hansson@arm.com        DcachePort(LSQ<Impl> *_lsq, FullO3CPU<Impl>* _cpu)
1719095Sandreas.hansson@arm.com            : CpuPort(_cpu->name() + ".dcache_port", _cpu), lsq(_lsq)
1728707Sandreas.hansson@arm.com        { }
1738707Sandreas.hansson@arm.com
1748707Sandreas.hansson@arm.com      protected:
1758707Sandreas.hansson@arm.com
1768707Sandreas.hansson@arm.com        /** Timing version of receive.  Handles writing back and
1778707Sandreas.hansson@arm.com         * completing the load or store that has returned from
1788707Sandreas.hansson@arm.com         * memory. */
1798975Sandreas.hansson@arm.com        virtual bool recvTimingResp(PacketPtr pkt);
1808975Sandreas.hansson@arm.com        virtual void recvTimingSnoopReq(PacketPtr pkt);
1818707Sandreas.hansson@arm.com
1828707Sandreas.hansson@arm.com        /** Handles doing a retry of the previous send. */
1838707Sandreas.hansson@arm.com        virtual void recvRetry();
1848707Sandreas.hansson@arm.com
1858707Sandreas.hansson@arm.com        /**
1868707Sandreas.hansson@arm.com         * As this CPU requires snooping to maintain the load store queue
1878707Sandreas.hansson@arm.com         * change the behaviour from the base CPU port.
1888707Sandreas.hansson@arm.com         *
1898711Sandreas.hansson@arm.com         * @return true since we have to snoop
1908707Sandreas.hansson@arm.com         */
1918922Swilliam.wang@arm.com        virtual bool isSnooping() const { return true; }
1928707Sandreas.hansson@arm.com    };
1938707Sandreas.hansson@arm.com
1941060SN/A    class TickEvent : public Event
1951060SN/A    {
1961060SN/A      private:
1972292SN/A        /** Pointer to the CPU. */
1981755SN/A        FullO3CPU<Impl> *cpu;
1991060SN/A
2001060SN/A      public:
2012292SN/A        /** Constructs a tick event. */
2021755SN/A        TickEvent(FullO3CPU<Impl> *c);
2032292SN/A
2042292SN/A        /** Processes a tick event, calling tick() on the CPU. */
2051060SN/A        void process();
2062292SN/A        /** Returns the description of the tick event. */
2075336Shines@cs.fsu.edu        const char *description() const;
2081060SN/A    };
2091060SN/A
2102292SN/A    /** The tick event used for scheduling CPU ticks. */
2111060SN/A    TickEvent tickEvent;
2121060SN/A
2132292SN/A    /** Schedule tick event, regardless of its current state. */
2149180Sandreas.hansson@arm.com    void scheduleTickEvent(Cycles delay)
2151060SN/A    {
2161060SN/A        if (tickEvent.squashed())
2179179Sandreas.hansson@arm.com            reschedule(tickEvent, clockEdge(delay));
2181060SN/A        else if (!tickEvent.scheduled())
2199179Sandreas.hansson@arm.com            schedule(tickEvent, clockEdge(delay));
2201060SN/A    }
2211060SN/A
2222292SN/A    /** Unschedule tick event, regardless of its current state. */
2231060SN/A    void unscheduleTickEvent()
2241060SN/A    {
2251060SN/A        if (tickEvent.scheduled())
2261060SN/A            tickEvent.squash();
2271060SN/A    }
2281060SN/A
2292829Sksewell@umich.edu    class ActivateThreadEvent : public Event
2302829Sksewell@umich.edu    {
2312829Sksewell@umich.edu      private:
2322829Sksewell@umich.edu        /** Number of Thread to Activate */
2336221Snate@binkert.org        ThreadID tid;
2342829Sksewell@umich.edu
2352829Sksewell@umich.edu        /** Pointer to the CPU. */
2362829Sksewell@umich.edu        FullO3CPU<Impl> *cpu;
2372829Sksewell@umich.edu
2382829Sksewell@umich.edu      public:
2392829Sksewell@umich.edu        /** Constructs the event. */
2402829Sksewell@umich.edu        ActivateThreadEvent();
2412829Sksewell@umich.edu
2422829Sksewell@umich.edu        /** Initialize Event */
2432829Sksewell@umich.edu        void init(int thread_num, FullO3CPU<Impl> *thread_cpu);
2442829Sksewell@umich.edu
2452829Sksewell@umich.edu        /** Processes the event, calling activateThread() on the CPU. */
2462829Sksewell@umich.edu        void process();
2472829Sksewell@umich.edu
2482829Sksewell@umich.edu        /** Returns the description of the event. */
2495336Shines@cs.fsu.edu        const char *description() const;
2502829Sksewell@umich.edu    };
2512829Sksewell@umich.edu
2522829Sksewell@umich.edu    /** Schedule thread to activate , regardless of its current state. */
2536221Snate@binkert.org    void
2549180Sandreas.hansson@arm.com    scheduleActivateThreadEvent(ThreadID tid, Cycles delay)
2552829Sksewell@umich.edu    {
2562829Sksewell@umich.edu        // Schedule thread to activate, regardless of its current state.
2572829Sksewell@umich.edu        if (activateThreadEvent[tid].squashed())
2585606Snate@binkert.org            reschedule(activateThreadEvent[tid],
2599179Sandreas.hansson@arm.com                       clockEdge(delay));
2608518Sgeoffrey.blake@arm.com        else if (!activateThreadEvent[tid].scheduled()) {
2619179Sandreas.hansson@arm.com            Tick when = clockEdge(delay);
2628518Sgeoffrey.blake@arm.com
2638518Sgeoffrey.blake@arm.com            // Check if the deallocateEvent is also scheduled, and make
2648518Sgeoffrey.blake@arm.com            // sure they do not happen at same time causing a sleep that
2658518Sgeoffrey.blake@arm.com            // is never woken from.
2668518Sgeoffrey.blake@arm.com            if (deallocateContextEvent[tid].scheduled() &&
2678518Sgeoffrey.blake@arm.com                deallocateContextEvent[tid].when() == when) {
2688518Sgeoffrey.blake@arm.com                when++;
2698518Sgeoffrey.blake@arm.com            }
2708518Sgeoffrey.blake@arm.com
2718518Sgeoffrey.blake@arm.com            schedule(activateThreadEvent[tid], when);
2728518Sgeoffrey.blake@arm.com        }
2732829Sksewell@umich.edu    }
2742829Sksewell@umich.edu
2752829Sksewell@umich.edu    /** Unschedule actiavte thread event, regardless of its current state. */
2766221Snate@binkert.org    void
2776221Snate@binkert.org    unscheduleActivateThreadEvent(ThreadID tid)
2782829Sksewell@umich.edu    {
2792829Sksewell@umich.edu        if (activateThreadEvent[tid].scheduled())
2802829Sksewell@umich.edu            activateThreadEvent[tid].squash();
2812829Sksewell@umich.edu    }
2822829Sksewell@umich.edu
2832829Sksewell@umich.edu    /** The tick event used for scheduling CPU ticks. */
2842829Sksewell@umich.edu    ActivateThreadEvent activateThreadEvent[Impl::MaxThreads];
2852829Sksewell@umich.edu
2862875Sksewell@umich.edu    class DeallocateContextEvent : public Event
2872875Sksewell@umich.edu    {
2882875Sksewell@umich.edu      private:
2893221Sktlim@umich.edu        /** Number of Thread to deactivate */
2906221Snate@binkert.org        ThreadID tid;
2912875Sksewell@umich.edu
2923221Sktlim@umich.edu        /** Should the thread be removed from the CPU? */
2933221Sktlim@umich.edu        bool remove;
2943221Sktlim@umich.edu
2952875Sksewell@umich.edu        /** Pointer to the CPU. */
2962875Sksewell@umich.edu        FullO3CPU<Impl> *cpu;
2972875Sksewell@umich.edu
2982875Sksewell@umich.edu      public:
2992875Sksewell@umich.edu        /** Constructs the event. */
3002875Sksewell@umich.edu        DeallocateContextEvent();
3012875Sksewell@umich.edu
3022875Sksewell@umich.edu        /** Initialize Event */
3032875Sksewell@umich.edu        void init(int thread_num, FullO3CPU<Impl> *thread_cpu);
3042875Sksewell@umich.edu
3052875Sksewell@umich.edu        /** Processes the event, calling activateThread() on the CPU. */
3062875Sksewell@umich.edu        void process();
3072875Sksewell@umich.edu
3083221Sktlim@umich.edu        /** Sets whether the thread should also be removed from the CPU. */
3093221Sktlim@umich.edu        void setRemove(bool _remove) { remove = _remove; }
3103221Sktlim@umich.edu
3112875Sksewell@umich.edu        /** Returns the description of the event. */
3125336Shines@cs.fsu.edu        const char *description() const;
3132875Sksewell@umich.edu    };
3142875Sksewell@umich.edu
3152875Sksewell@umich.edu    /** Schedule cpu to deallocate thread context.*/
3166221Snate@binkert.org    void
3179180Sandreas.hansson@arm.com    scheduleDeallocateContextEvent(ThreadID tid, bool remove, Cycles delay)
3182875Sksewell@umich.edu    {
3192875Sksewell@umich.edu        // Schedule thread to activate, regardless of its current state.
3202875Sksewell@umich.edu        if (deallocateContextEvent[tid].squashed())
3215606Snate@binkert.org            reschedule(deallocateContextEvent[tid],
3229179Sandreas.hansson@arm.com                       clockEdge(delay));
3232875Sksewell@umich.edu        else if (!deallocateContextEvent[tid].scheduled())
3245606Snate@binkert.org            schedule(deallocateContextEvent[tid],
3259179Sandreas.hansson@arm.com                     clockEdge(delay));
3262875Sksewell@umich.edu    }
3272875Sksewell@umich.edu
3282875Sksewell@umich.edu    /** Unschedule thread deallocation in CPU */
3296221Snate@binkert.org    void
3306221Snate@binkert.org    unscheduleDeallocateContextEvent(ThreadID tid)
3312875Sksewell@umich.edu    {
3322875Sksewell@umich.edu        if (deallocateContextEvent[tid].scheduled())
3332875Sksewell@umich.edu            deallocateContextEvent[tid].squash();
3342875Sksewell@umich.edu    }
3352875Sksewell@umich.edu
3362875Sksewell@umich.edu    /** The tick event used for scheduling CPU ticks. */
3372875Sksewell@umich.edu    DeallocateContextEvent deallocateContextEvent[Impl::MaxThreads];
3382875Sksewell@umich.edu
3391060SN/A  public:
3402292SN/A    /** Constructs a CPU with the given parameters. */
3415595Sgblack@eecs.umich.edu    FullO3CPU(DerivO3CPUParams *params);
3422292SN/A    /** Destructor. */
3431755SN/A    ~FullO3CPU();
3441060SN/A
3452292SN/A    /** Registers statistics. */
3465595Sgblack@eecs.umich.edu    void regStats();
3471684SN/A
3485358Sgblack@eecs.umich.edu    void demapPage(Addr vaddr, uint64_t asn)
3495358Sgblack@eecs.umich.edu    {
3505358Sgblack@eecs.umich.edu        this->itb->demapPage(vaddr, asn);
3515358Sgblack@eecs.umich.edu        this->dtb->demapPage(vaddr, asn);
3525358Sgblack@eecs.umich.edu    }
3535358Sgblack@eecs.umich.edu
3545358Sgblack@eecs.umich.edu    void demapInstPage(Addr vaddr, uint64_t asn)
3555358Sgblack@eecs.umich.edu    {
3565358Sgblack@eecs.umich.edu        this->itb->demapPage(vaddr, asn);
3575358Sgblack@eecs.umich.edu    }
3585358Sgblack@eecs.umich.edu
3595358Sgblack@eecs.umich.edu    void demapDataPage(Addr vaddr, uint64_t asn)
3605358Sgblack@eecs.umich.edu    {
3615358Sgblack@eecs.umich.edu        this->dtb->demapPage(vaddr, asn);
3625358Sgblack@eecs.umich.edu    }
3635358Sgblack@eecs.umich.edu
3642292SN/A    /** Ticks CPU, calling tick() on each stage, and checking the overall
3652292SN/A     *  activity to see if the CPU should deschedule itself.
3662292SN/A     */
3671684SN/A    void tick();
3681684SN/A
3692292SN/A    /** Initialize the CPU */
3701060SN/A    void init();
3711060SN/A
3722834Sksewell@umich.edu    /** Returns the Number of Active Threads in the CPU */
3732834Sksewell@umich.edu    int numActiveThreads()
3742834Sksewell@umich.edu    { return activeThreads.size(); }
3752834Sksewell@umich.edu
3762829Sksewell@umich.edu    /** Add Thread to Active Threads List */
3776221Snate@binkert.org    void activateThread(ThreadID tid);
3782875Sksewell@umich.edu
3792875Sksewell@umich.edu    /** Remove Thread from Active Threads List */
3806221Snate@binkert.org    void deactivateThread(ThreadID tid);
3812829Sksewell@umich.edu
3822292SN/A    /** Setup CPU to insert a thread's context */
3836221Snate@binkert.org    void insertThread(ThreadID tid);
3841060SN/A
3852292SN/A    /** Remove all of a thread's context from CPU */
3866221Snate@binkert.org    void removeThread(ThreadID tid);
3872292SN/A
3882292SN/A    /** Count the Total Instructions Committed in the CPU. */
3898834Satgutier@umich.edu    virtual Counter totalInsts() const;
3908834Satgutier@umich.edu
3918834Satgutier@umich.edu    /** Count the Total Ops (including micro ops) committed in the CPU. */
3928834Satgutier@umich.edu    virtual Counter totalOps() const;
3932292SN/A
3942292SN/A    /** Add Thread to Active Threads List. */
3959180Sandreas.hansson@arm.com    void activateContext(ThreadID tid, Cycles delay);
3962292SN/A
3972292SN/A    /** Remove Thread from Active Threads List */
3986221Snate@binkert.org    void suspendContext(ThreadID tid);
3992292SN/A
4002292SN/A    /** Remove Thread from Active Threads List &&
4013221Sktlim@umich.edu     *  Possibly Remove Thread Context from CPU.
4022292SN/A     */
4039180Sandreas.hansson@arm.com    bool scheduleDeallocateContext(ThreadID tid, bool remove,
4049180Sandreas.hansson@arm.com                                   Cycles delay = Cycles(1));
4052292SN/A
4062292SN/A    /** Remove Thread from Active Threads List &&
4072292SN/A     *  Remove Thread Context from CPU.
4082292SN/A     */
4096221Snate@binkert.org    void haltContext(ThreadID tid);
4102292SN/A
4112292SN/A    /** Activate a Thread When CPU Resources are Available. */
4126221Snate@binkert.org    void activateWhenReady(ThreadID tid);
4132292SN/A
4142292SN/A    /** Add or Remove a Thread Context in the CPU. */
4152292SN/A    void doContextSwitch();
4162292SN/A
4172292SN/A    /** Update The Order In Which We Process Threads. */
4182292SN/A    void updateThreadPriority();
4192292SN/A
4202864Sktlim@umich.edu    /** Serialize state. */
4212864Sktlim@umich.edu    virtual void serialize(std::ostream &os);
4222864Sktlim@umich.edu
4232864Sktlim@umich.edu    /** Unserialize from a checkpoint. */
4242864Sktlim@umich.edu    virtual void unserialize(Checkpoint *cp, const std::string &section);
4252864Sktlim@umich.edu
4262864Sktlim@umich.edu  public:
4275595Sgblack@eecs.umich.edu    /** Executes a syscall.
4285595Sgblack@eecs.umich.edu     * @todo: Determine if this needs to be virtual.
4292292SN/A     */
4306221Snate@binkert.org    void syscall(int64_t callnum, ThreadID tid);
4312292SN/A
4322843Sktlim@umich.edu    /** Starts draining the CPU's pipeline of all instructions in
4332843Sktlim@umich.edu     * order to stop all memory accesses. */
4349342SAndreas.Sandberg@arm.com    unsigned int drain(DrainManager *drain_manager);
4352843Sktlim@umich.edu
4362843Sktlim@umich.edu    /** Resumes execution after a drain. */
4379342SAndreas.Sandberg@arm.com    void drainResume();
4382292SN/A
4392348SN/A    /** Signals to this CPU that a stage has completed switching out. */
4402843Sktlim@umich.edu    void signalDrained();
4412843Sktlim@umich.edu
4422843Sktlim@umich.edu    /** Switches out this CPU. */
4432843Sktlim@umich.edu    virtual void switchOut();
4442316SN/A
4452348SN/A    /** Takes over from another CPU. */
4462843Sktlim@umich.edu    virtual void takeOverFrom(BaseCPU *oldCPU);
4471060SN/A
4481060SN/A    /** Get the current instruction sequence number, and increment it. */
4492316SN/A    InstSeqNum getAndIncrementInstSeq()
4502316SN/A    { return globalSeqNum++; }
4511060SN/A
4525595Sgblack@eecs.umich.edu    /** Traps to handle given fault. */
4537684Sgblack@eecs.umich.edu    void trap(Fault fault, ThreadID tid, StaticInstPtr inst);
4545595Sgblack@eecs.umich.edu
4555702Ssaidi@eecs.umich.edu    /** HW return from error interrupt. */
4566221Snate@binkert.org    Fault hwrei(ThreadID tid);
4575702Ssaidi@eecs.umich.edu
4586221Snate@binkert.org    bool simPalCheck(int palFunc, ThreadID tid);
4595702Ssaidi@eecs.umich.edu
4605595Sgblack@eecs.umich.edu    /** Returns the Fault for any valid interrupt. */
4615595Sgblack@eecs.umich.edu    Fault getInterrupts();
4625595Sgblack@eecs.umich.edu
4635595Sgblack@eecs.umich.edu    /** Processes any an interrupt fault. */
4645595Sgblack@eecs.umich.edu    void processInterrupts(Fault interrupt);
4655595Sgblack@eecs.umich.edu
4665595Sgblack@eecs.umich.edu    /** Halts the CPU. */
4675595Sgblack@eecs.umich.edu    void halt() { panic("Halt not implemented!\n"); }
4685595Sgblack@eecs.umich.edu
4691060SN/A    /** Check if this address is a valid instruction address. */
4701060SN/A    bool validInstAddr(Addr addr) { return true; }
4711060SN/A
4721060SN/A    /** Check if this address is a valid data address. */
4731060SN/A    bool validDataAddr(Addr addr) { return true; }
4741060SN/A
4752348SN/A    /** Register accessors.  Index refers to the physical register index. */
4765595Sgblack@eecs.umich.edu
4775595Sgblack@eecs.umich.edu    /** Reads a miscellaneous register. */
4786221Snate@binkert.org    TheISA::MiscReg readMiscRegNoEffect(int misc_reg, ThreadID tid);
4795595Sgblack@eecs.umich.edu
4805595Sgblack@eecs.umich.edu    /** Reads a misc. register, including any side effects the read
4815595Sgblack@eecs.umich.edu     * might have as defined by the architecture.
4825595Sgblack@eecs.umich.edu     */
4836221Snate@binkert.org    TheISA::MiscReg readMiscReg(int misc_reg, ThreadID tid);
4845595Sgblack@eecs.umich.edu
4855595Sgblack@eecs.umich.edu    /** Sets a miscellaneous register. */
4866221Snate@binkert.org    void setMiscRegNoEffect(int misc_reg, const TheISA::MiscReg &val,
4876221Snate@binkert.org            ThreadID tid);
4885595Sgblack@eecs.umich.edu
4895595Sgblack@eecs.umich.edu    /** Sets a misc. register, including any side effects the write
4905595Sgblack@eecs.umich.edu     * might have as defined by the architecture.
4915595Sgblack@eecs.umich.edu     */
4925595Sgblack@eecs.umich.edu    void setMiscReg(int misc_reg, const TheISA::MiscReg &val,
4936221Snate@binkert.org            ThreadID tid);
4945595Sgblack@eecs.umich.edu
4951060SN/A    uint64_t readIntReg(int reg_idx);
4961060SN/A
4973781Sgblack@eecs.umich.edu    TheISA::FloatReg readFloatReg(int reg_idx);
4981060SN/A
4993781Sgblack@eecs.umich.edu    TheISA::FloatRegBits readFloatRegBits(int reg_idx);
5002455SN/A
5011060SN/A    void setIntReg(int reg_idx, uint64_t val);
5021060SN/A
5033781Sgblack@eecs.umich.edu    void setFloatReg(int reg_idx, TheISA::FloatReg val);
5041060SN/A
5053781Sgblack@eecs.umich.edu    void setFloatRegBits(int reg_idx, TheISA::FloatRegBits val);
5062455SN/A
5076221Snate@binkert.org    uint64_t readArchIntReg(int reg_idx, ThreadID tid);
5081060SN/A
5096314Sgblack@eecs.umich.edu    float readArchFloatReg(int reg_idx, ThreadID tid);
5102292SN/A
5116221Snate@binkert.org    uint64_t readArchFloatRegInt(int reg_idx, ThreadID tid);
5122292SN/A
5132348SN/A    /** Architectural register accessors.  Looks up in the commit
5142348SN/A     * rename table to obtain the true physical index of the
5152348SN/A     * architected register first, then accesses that physical
5162348SN/A     * register.
5172348SN/A     */
5186221Snate@binkert.org    void setArchIntReg(int reg_idx, uint64_t val, ThreadID tid);
5192292SN/A
5206314Sgblack@eecs.umich.edu    void setArchFloatReg(int reg_idx, float val, ThreadID tid);
5212292SN/A
5226221Snate@binkert.org    void setArchFloatRegInt(int reg_idx, uint64_t val, ThreadID tid);
5232292SN/A
5247720Sgblack@eecs.umich.edu    /** Sets the commit PC state of a specific thread. */
5257720Sgblack@eecs.umich.edu    void pcState(const TheISA::PCState &newPCState, ThreadID tid);
5267720Sgblack@eecs.umich.edu
5277720Sgblack@eecs.umich.edu    /** Reads the commit PC state of a specific thread. */
5287720Sgblack@eecs.umich.edu    TheISA::PCState pcState(ThreadID tid);
5297720Sgblack@eecs.umich.edu
5302348SN/A    /** Reads the commit PC of a specific thread. */
5317720Sgblack@eecs.umich.edu    Addr instAddr(ThreadID tid);
5322292SN/A
5334636Sgblack@eecs.umich.edu    /** Reads the commit micro PC of a specific thread. */
5347720Sgblack@eecs.umich.edu    MicroPC microPC(ThreadID tid);
5354636Sgblack@eecs.umich.edu
5362348SN/A    /** Reads the next PC of a specific thread. */
5377720Sgblack@eecs.umich.edu    Addr nextInstAddr(ThreadID tid);
5382756Sksewell@umich.edu
5395595Sgblack@eecs.umich.edu    /** Initiates a squash of all in-flight instructions for a given
5405595Sgblack@eecs.umich.edu     * thread.  The source of the squash is an external update of
5415595Sgblack@eecs.umich.edu     * state through the TC.
5425595Sgblack@eecs.umich.edu     */
5436221Snate@binkert.org    void squashFromTC(ThreadID tid);
5445595Sgblack@eecs.umich.edu
5451060SN/A    /** Function to add instruction onto the head of the list of the
5461060SN/A     *  instructions.  Used when new instructions are fetched.
5471060SN/A     */
5482292SN/A    ListIt addInst(DynInstPtr &inst);
5491060SN/A
5501060SN/A    /** Function to tell the CPU that an instruction has completed. */
5518834Satgutier@umich.edu    void instDone(ThreadID tid, DynInstPtr &inst);
5521060SN/A
5532325SN/A    /** Remove an instruction from the front end of the list.  There's
5542325SN/A     *  no restriction on location of the instruction.
5551060SN/A     */
5561061SN/A    void removeFrontInst(DynInstPtr &inst);
5571060SN/A
5582935Sksewell@umich.edu    /** Remove all instructions that are not currently in the ROB.
5592935Sksewell@umich.edu     *  There's also an option to not squash delay slot instructions.*/
5606221Snate@binkert.org    void removeInstsNotInROB(ThreadID tid);
5611060SN/A
5621062SN/A    /** Remove all instructions younger than the given sequence number. */
5636221Snate@binkert.org    void removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid);
5642292SN/A
5652348SN/A    /** Removes the instruction pointed to by the iterator. */
5666221Snate@binkert.org    inline void squashInstIt(const ListIt &instIt, ThreadID tid);
5672292SN/A
5682348SN/A    /** Cleans up all instructions on the remove list. */
5692292SN/A    void cleanUpRemovedInsts();
5701062SN/A
5712348SN/A    /** Debug function to print all instructions on the list. */
5721060SN/A    void dumpInsts();
5731060SN/A
5741060SN/A  public:
5755737Scws3k@cs.virginia.edu#ifndef NDEBUG
5765737Scws3k@cs.virginia.edu    /** Count of total number of dynamic instructions in flight. */
5775737Scws3k@cs.virginia.edu    int instcount;
5785737Scws3k@cs.virginia.edu#endif
5795737Scws3k@cs.virginia.edu
5801060SN/A    /** List of all the instructions in flight. */
5812292SN/A    std::list<DynInstPtr> instList;
5821060SN/A
5832292SN/A    /** List of all the instructions that will be removed at the end of this
5842292SN/A     *  cycle.
5852292SN/A     */
5862292SN/A    std::queue<ListIt> removeList;
5872292SN/A
5882325SN/A#ifdef DEBUG
5892348SN/A    /** Debug structure to keep track of the sequence numbers still in
5902348SN/A     * flight.
5912348SN/A     */
5922292SN/A    std::set<InstSeqNum> snList;
5932325SN/A#endif
5942292SN/A
5952325SN/A    /** Records if instructions need to be removed this cycle due to
5962325SN/A     *  being retired or squashed.
5972292SN/A     */
5982292SN/A    bool removeInstsThisCycle;
5992292SN/A
6001060SN/A  protected:
6011060SN/A    /** The fetch stage. */
6021060SN/A    typename CPUPolicy::Fetch fetch;
6031060SN/A
6041060SN/A    /** The decode stage. */
6051060SN/A    typename CPUPolicy::Decode decode;
6061060SN/A
6071060SN/A    /** The dispatch stage. */
6081060SN/A    typename CPUPolicy::Rename rename;
6091060SN/A
6101060SN/A    /** The issue/execute/writeback stages. */
6111060SN/A    typename CPUPolicy::IEW iew;
6121060SN/A
6131060SN/A    /** The commit stage. */
6141060SN/A    typename CPUPolicy::Commit commit;
6151060SN/A
6161060SN/A    /** The register file. */
6171060SN/A    typename CPUPolicy::RegFile regFile;
6181060SN/A
6191060SN/A    /** The free list. */
6201060SN/A    typename CPUPolicy::FreeList freeList;
6211060SN/A
6221060SN/A    /** The rename map. */
6232292SN/A    typename CPUPolicy::RenameMap renameMap[Impl::MaxThreads];
6242292SN/A
6252292SN/A    /** The commit rename map. */
6262292SN/A    typename CPUPolicy::RenameMap commitRenameMap[Impl::MaxThreads];
6271060SN/A
6281060SN/A    /** The re-order buffer. */
6291060SN/A    typename CPUPolicy::ROB rob;
6301060SN/A
6312292SN/A    /** Active Threads List */
6326221Snate@binkert.org    std::list<ThreadID> activeThreads;
6332292SN/A
6342292SN/A    /** Integer Register Scoreboard */
6352292SN/A    Scoreboard scoreboard;
6362292SN/A
6376313Sgblack@eecs.umich.edu    TheISA::ISA isa[Impl::MaxThreads];
6386313Sgblack@eecs.umich.edu
6398707Sandreas.hansson@arm.com    /** Instruction port. Note that it has to appear after the fetch stage. */
6408707Sandreas.hansson@arm.com    IcachePort icachePort;
6418707Sandreas.hansson@arm.com
6428707Sandreas.hansson@arm.com    /** Data port. Note that it has to appear after the iew stages */
6438707Sandreas.hansson@arm.com    DcachePort dcachePort;
6448707Sandreas.hansson@arm.com
6451060SN/A  public:
6462292SN/A    /** Enum to give each stage a specific index, so when calling
6472292SN/A     *  activateStage() or deactivateStage(), they can specify which stage
6482292SN/A     *  is being activated/deactivated.
6492292SN/A     */
6502292SN/A    enum StageIdx {
6512292SN/A        FetchIdx,
6522292SN/A        DecodeIdx,
6532292SN/A        RenameIdx,
6542292SN/A        IEWIdx,
6552292SN/A        CommitIdx,
6562292SN/A        NumStages };
6572292SN/A
6581060SN/A    /** Typedefs from the Impl to get the structs that each of the
6591060SN/A     *  time buffers should use.
6601060SN/A     */
6611061SN/A    typedef typename CPUPolicy::TimeStruct TimeStruct;
6621060SN/A
6631061SN/A    typedef typename CPUPolicy::FetchStruct FetchStruct;
6641060SN/A
6651061SN/A    typedef typename CPUPolicy::DecodeStruct DecodeStruct;
6661060SN/A
6671061SN/A    typedef typename CPUPolicy::RenameStruct RenameStruct;
6681060SN/A
6691061SN/A    typedef typename CPUPolicy::IEWStruct IEWStruct;
6701060SN/A
6711060SN/A    /** The main time buffer to do backwards communication. */
6721060SN/A    TimeBuffer<TimeStruct> timeBuffer;
6731060SN/A
6741060SN/A    /** The fetch stage's instruction queue. */
6751060SN/A    TimeBuffer<FetchStruct> fetchQueue;
6761060SN/A
6771060SN/A    /** The decode stage's instruction queue. */
6781060SN/A    TimeBuffer<DecodeStruct> decodeQueue;
6791060SN/A
6801060SN/A    /** The rename stage's instruction queue. */
6811060SN/A    TimeBuffer<RenameStruct> renameQueue;
6821060SN/A
6831060SN/A    /** The IEW stage's instruction queue. */
6841060SN/A    TimeBuffer<IEWStruct> iewQueue;
6851060SN/A
6862348SN/A  private:
6872348SN/A    /** The activity recorder; used to tell if the CPU has any
6882348SN/A     * activity remaining or if it can go to idle and deschedule
6892348SN/A     * itself.
6902348SN/A     */
6912325SN/A    ActivityRecorder activityRec;
6921060SN/A
6932348SN/A  public:
6942348SN/A    /** Records that there was time buffer activity this cycle. */
6952325SN/A    void activityThisCycle() { activityRec.activity(); }
6962292SN/A
6972348SN/A    /** Changes a stage's status to active within the activity recorder. */
6982325SN/A    void activateStage(const StageIdx idx)
6992325SN/A    { activityRec.activateStage(idx); }
7002292SN/A
7012348SN/A    /** Changes a stage's status to inactive within the activity recorder. */
7022325SN/A    void deactivateStage(const StageIdx idx)
7032325SN/A    { activityRec.deactivateStage(idx); }
7042292SN/A
7052292SN/A    /** Wakes the CPU, rescheduling the CPU if it's not already active. */
7062292SN/A    void wakeCPU();
7072260SN/A
7085807Snate@binkert.org    virtual void wakeup();
7095807Snate@binkert.org
7102292SN/A    /** Gets a free thread id. Use if thread ids change across system. */
7116221Snate@binkert.org    ThreadID getFreeTid();
7122292SN/A
7132292SN/A  public:
7142680Sktlim@umich.edu    /** Returns a pointer to a thread context. */
7156221Snate@binkert.org    ThreadContext *
7166221Snate@binkert.org    tcBase(ThreadID tid)
7171681SN/A    {
7182680Sktlim@umich.edu        return thread[tid]->getTC();
7192190SN/A    }
7202190SN/A
7212292SN/A    /** The global sequence number counter. */
7223093Sksewell@umich.edu    InstSeqNum globalSeqNum;//[Impl::MaxThreads];
7231060SN/A
7242348SN/A    /** Pointer to the checker, which can dynamically verify
7252348SN/A     * instruction results at run time.  This can be set to NULL if it
7262348SN/A     * is not being used.
7272348SN/A     */
7288733Sgeoffrey.blake@arm.com    Checker<Impl> *checker;
7292316SN/A
7302292SN/A    /** Pointer to the system. */
7311060SN/A    System *system;
7321060SN/A
7339342SAndreas.Sandberg@arm.com    /** DrainManager to notify when draining has completed. */
7349342SAndreas.Sandberg@arm.com    DrainManager *drainManager;
7352843Sktlim@umich.edu
7362843Sktlim@umich.edu    /** Counter of how many stages have completed draining. */
7372843Sktlim@umich.edu    int drainCount;
7382316SN/A
7392348SN/A    /** Pointers to all of the threads in the CPU. */
7402292SN/A    std::vector<Thread *> thread;
7412260SN/A
7422292SN/A    /** Whether or not the CPU should defer its registration. */
7431060SN/A    bool deferRegistration;
7441060SN/A
7452292SN/A    /** Is there a context switch pending? */
7462292SN/A    bool contextSwitch;
7471060SN/A
7482292SN/A    /** Threads Scheduled to Enter CPU */
7492292SN/A    std::list<int> cpuWaitList;
7502292SN/A
7512292SN/A    /** The cycle that the CPU was last running, used for statistics. */
7529180Sandreas.hansson@arm.com    Cycles lastRunningCycle;
7532292SN/A
7542829Sksewell@umich.edu    /** The cycle that the CPU was last activated by a new thread*/
7552829Sksewell@umich.edu    Tick lastActivatedCycle;
7562829Sksewell@umich.edu
7572292SN/A    /** Mapping for system thread id to cpu id */
7586221Snate@binkert.org    std::map<ThreadID, unsigned> threadMap;
7592292SN/A
7602292SN/A    /** Available thread ids in the cpu*/
7616221Snate@binkert.org    std::vector<ThreadID> tids;
7622292SN/A
7635595Sgblack@eecs.umich.edu    /** CPU read function, forwards read to LSQ. */
7646974Stjones1@inf.ed.ac.uk    Fault read(RequestPtr &req, RequestPtr &sreqLow, RequestPtr &sreqHigh,
7657520Sgblack@eecs.umich.edu               uint8_t *data, int load_idx)
7665595Sgblack@eecs.umich.edu    {
7676974Stjones1@inf.ed.ac.uk        return this->iew.ldstQueue.read(req, sreqLow, sreqHigh,
7686974Stjones1@inf.ed.ac.uk                                        data, load_idx);
7695595Sgblack@eecs.umich.edu    }
7705595Sgblack@eecs.umich.edu
7715595Sgblack@eecs.umich.edu    /** CPU write function, forwards write to LSQ. */
7726974Stjones1@inf.ed.ac.uk    Fault write(RequestPtr &req, RequestPtr &sreqLow, RequestPtr &sreqHigh,
7737520Sgblack@eecs.umich.edu                uint8_t *data, int store_idx)
7745595Sgblack@eecs.umich.edu    {
7756974Stjones1@inf.ed.ac.uk        return this->iew.ldstQueue.write(req, sreqLow, sreqHigh,
7766974Stjones1@inf.ed.ac.uk                                         data, store_idx);
7775595Sgblack@eecs.umich.edu    }
7785595Sgblack@eecs.umich.edu
7798707Sandreas.hansson@arm.com    /** Used by the fetch unit to get a hold of the instruction port. */
7808850Sandreas.hansson@arm.com    virtual CpuPort &getInstPort() { return icachePort; }
7818707Sandreas.hansson@arm.com
7826974Stjones1@inf.ed.ac.uk    /** Get the dcache port (used to find block size for translations). */
7838850Sandreas.hansson@arm.com    virtual CpuPort &getDataPort() { return dcachePort; }
7846974Stjones1@inf.ed.ac.uk
7855595Sgblack@eecs.umich.edu    Addr lockAddr;
7865595Sgblack@eecs.umich.edu
7875595Sgblack@eecs.umich.edu    /** Temporary fix for the lock flag, works in the UP case. */
7885595Sgblack@eecs.umich.edu    bool lockFlag;
7895595Sgblack@eecs.umich.edu
7902292SN/A    /** Stat for total number of times the CPU is descheduled. */
7915999Snate@binkert.org    Stats::Scalar timesIdled;
7922292SN/A    /** Stat for total number of cycles the CPU spends descheduled. */
7935999Snate@binkert.org    Stats::Scalar idleCycles;
7948627SAli.Saidi@ARM.com    /** Stat for total number of cycles the CPU spends descheduled due to a
7958627SAli.Saidi@ARM.com     * quiesce operation or waiting for an interrupt. */
7968627SAli.Saidi@ARM.com    Stats::Scalar quiesceCycles;
7972292SN/A    /** Stat for the number of committed instructions per thread. */
7985999Snate@binkert.org    Stats::Vector committedInsts;
7998834Satgutier@umich.edu    /** Stat for the number of committed ops (including micro ops) per thread. */
8008834Satgutier@umich.edu    Stats::Vector committedOps;
8012292SN/A    /** Stat for the total number of committed instructions. */
8025999Snate@binkert.org    Stats::Scalar totalCommittedInsts;
8032292SN/A    /** Stat for the CPI per thread. */
8042292SN/A    Stats::Formula cpi;
8052292SN/A    /** Stat for the total CPI. */
8062292SN/A    Stats::Formula totalCpi;
8072292SN/A    /** Stat for the IPC per thread. */
8082292SN/A    Stats::Formula ipc;
8092292SN/A    /** Stat for the total IPC. */
8102292SN/A    Stats::Formula totalIpc;
8117897Shestness@cs.utexas.edu
8127897Shestness@cs.utexas.edu    //number of integer register file accesses
8137897Shestness@cs.utexas.edu    Stats::Scalar intRegfileReads;
8147897Shestness@cs.utexas.edu    Stats::Scalar intRegfileWrites;
8157897Shestness@cs.utexas.edu    //number of float register file accesses
8167897Shestness@cs.utexas.edu    Stats::Scalar fpRegfileReads;
8177897Shestness@cs.utexas.edu    Stats::Scalar fpRegfileWrites;
8187897Shestness@cs.utexas.edu    //number of misc
8197897Shestness@cs.utexas.edu    Stats::Scalar miscRegfileReads;
8207897Shestness@cs.utexas.edu    Stats::Scalar miscRegfileWrites;
8211060SN/A};
8221060SN/A
8232325SN/A#endif // __CPU_O3_CPU_HH__
824