timing.hh revision 5894
12623SN/A/*
22623SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32623SN/A * All rights reserved.
42623SN/A *
52623SN/A * Redistribution and use in source and binary forms, with or without
62623SN/A * modification, are permitted provided that the following conditions are
72623SN/A * met: redistributions of source code must retain the above copyright
82623SN/A * notice, this list of conditions and the following disclaimer;
92623SN/A * redistributions in binary form must reproduce the above copyright
102623SN/A * notice, this list of conditions and the following disclaimer in the
112623SN/A * documentation and/or other materials provided with the distribution;
122623SN/A * neither the name of the copyright holders nor the names of its
132623SN/A * contributors may be used to endorse or promote products derived from
142623SN/A * this software without specific prior written permission.
152623SN/A *
162623SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292623SN/A */
302623SN/A
312623SN/A#ifndef __CPU_SIMPLE_TIMING_HH__
322623SN/A#define __CPU_SIMPLE_TIMING_HH__
332623SN/A
342623SN/A#include "cpu/simple/base.hh"
352623SN/A
365529Snate@binkert.org#include "params/TimingSimpleCPU.hh"
375529Snate@binkert.org
382623SN/Aclass TimingSimpleCPU : public BaseSimpleCPU
392623SN/A{
402623SN/A  public:
412623SN/A
425529Snate@binkert.org    TimingSimpleCPU(TimingSimpleCPUParams * params);
432623SN/A    virtual ~TimingSimpleCPU();
442623SN/A
452623SN/A    virtual void init();
462623SN/A
472623SN/A  public:
482839Sktlim@umich.edu    Event *drainEvent;
492798Sktlim@umich.edu
502623SN/A  private:
512623SN/A
525728Sgblack@eecs.umich.edu    /*
535728Sgblack@eecs.umich.edu     * If an access needs to be broken into fragments, currently at most two,
545728Sgblack@eecs.umich.edu     * the the following two classes are used as the sender state of the
555728Sgblack@eecs.umich.edu     * packets so the CPU can keep track of everything. In the main packet
565728Sgblack@eecs.umich.edu     * sender state, there's an array with a spot for each fragment. If a
575728Sgblack@eecs.umich.edu     * fragment has already been accepted by the CPU, aka isn't waiting for
585728Sgblack@eecs.umich.edu     * a retry, it's pointer is NULL. After each fragment has successfully
595728Sgblack@eecs.umich.edu     * been processed, the "outstanding" counter is decremented. Once the
605728Sgblack@eecs.umich.edu     * count is zero, the entire larger access is complete.
615728Sgblack@eecs.umich.edu     */
625728Sgblack@eecs.umich.edu    class SplitMainSenderState : public Packet::SenderState
635728Sgblack@eecs.umich.edu    {
645728Sgblack@eecs.umich.edu      public:
655728Sgblack@eecs.umich.edu        int outstanding;
665728Sgblack@eecs.umich.edu        PacketPtr fragments[2];
675728Sgblack@eecs.umich.edu
685728Sgblack@eecs.umich.edu        int
695728Sgblack@eecs.umich.edu        getPendingFragment()
705728Sgblack@eecs.umich.edu        {
715728Sgblack@eecs.umich.edu            if (fragments[0]) {
725728Sgblack@eecs.umich.edu                return 0;
735728Sgblack@eecs.umich.edu            } else if (fragments[1]) {
745728Sgblack@eecs.umich.edu                return 1;
755728Sgblack@eecs.umich.edu            } else {
765728Sgblack@eecs.umich.edu                return -1;
775728Sgblack@eecs.umich.edu            }
785728Sgblack@eecs.umich.edu        }
795728Sgblack@eecs.umich.edu    };
805728Sgblack@eecs.umich.edu
815728Sgblack@eecs.umich.edu    class SplitFragmentSenderState : public Packet::SenderState
825728Sgblack@eecs.umich.edu    {
835728Sgblack@eecs.umich.edu      public:
845728Sgblack@eecs.umich.edu        SplitFragmentSenderState(PacketPtr _bigPkt, int _index) :
855728Sgblack@eecs.umich.edu            bigPkt(_bigPkt), index(_index)
865728Sgblack@eecs.umich.edu        {}
875728Sgblack@eecs.umich.edu        PacketPtr bigPkt;
885728Sgblack@eecs.umich.edu        int index;
895728Sgblack@eecs.umich.edu
905728Sgblack@eecs.umich.edu        void
915728Sgblack@eecs.umich.edu        clearFromParent()
925728Sgblack@eecs.umich.edu        {
935728Sgblack@eecs.umich.edu            SplitMainSenderState * main_send_state =
945728Sgblack@eecs.umich.edu                dynamic_cast<SplitMainSenderState *>(bigPkt->senderState);
955728Sgblack@eecs.umich.edu            main_send_state->fragments[index] = NULL;
965728Sgblack@eecs.umich.edu        }
975728Sgblack@eecs.umich.edu    };
985728Sgblack@eecs.umich.edu
995894Sgblack@eecs.umich.edu    class FetchTranslation : public BaseTLB::Translation
1005894Sgblack@eecs.umich.edu    {
1015894Sgblack@eecs.umich.edu      protected:
1025894Sgblack@eecs.umich.edu        TimingSimpleCPU *cpu;
1035894Sgblack@eecs.umich.edu
1045894Sgblack@eecs.umich.edu      public:
1055894Sgblack@eecs.umich.edu        FetchTranslation(TimingSimpleCPU *_cpu) : cpu(_cpu)
1065894Sgblack@eecs.umich.edu        {}
1075894Sgblack@eecs.umich.edu
1085894Sgblack@eecs.umich.edu        void finish(Fault fault, RequestPtr req,
1095894Sgblack@eecs.umich.edu                ThreadContext *tc, bool write)
1105894Sgblack@eecs.umich.edu        {
1115894Sgblack@eecs.umich.edu            cpu->sendFetch(fault, req, tc);
1125894Sgblack@eecs.umich.edu        }
1135894Sgblack@eecs.umich.edu    };
1145894Sgblack@eecs.umich.edu    FetchTranslation fetchTranslation;
1155894Sgblack@eecs.umich.edu
1165894Sgblack@eecs.umich.edu    class DataTranslation : public BaseTLB::Translation
1175894Sgblack@eecs.umich.edu    {
1185894Sgblack@eecs.umich.edu      protected:
1195894Sgblack@eecs.umich.edu        TimingSimpleCPU *cpu;
1205894Sgblack@eecs.umich.edu        uint8_t *data;
1215894Sgblack@eecs.umich.edu        uint64_t *res;
1225894Sgblack@eecs.umich.edu        bool read;
1235894Sgblack@eecs.umich.edu
1245894Sgblack@eecs.umich.edu      public:
1255894Sgblack@eecs.umich.edu        DataTranslation(TimingSimpleCPU *_cpu,
1265894Sgblack@eecs.umich.edu                uint8_t *_data, uint64_t *_res, bool _read) :
1275894Sgblack@eecs.umich.edu            cpu(_cpu), data(_data), res(_res), read(_read)
1285894Sgblack@eecs.umich.edu        {}
1295894Sgblack@eecs.umich.edu
1305894Sgblack@eecs.umich.edu        void
1315894Sgblack@eecs.umich.edu        finish(Fault fault, RequestPtr req,
1325894Sgblack@eecs.umich.edu                ThreadContext *tc, bool write)
1335894Sgblack@eecs.umich.edu        {
1345894Sgblack@eecs.umich.edu            cpu->sendData(fault, req, data, res, read);
1355894Sgblack@eecs.umich.edu            delete this;
1365894Sgblack@eecs.umich.edu        }
1375894Sgblack@eecs.umich.edu    };
1385894Sgblack@eecs.umich.edu
1395894Sgblack@eecs.umich.edu    class SplitDataTranslation : public BaseTLB::Translation
1405894Sgblack@eecs.umich.edu    {
1415894Sgblack@eecs.umich.edu      public:
1425894Sgblack@eecs.umich.edu        struct WholeTranslationState
1435894Sgblack@eecs.umich.edu        {
1445894Sgblack@eecs.umich.edu          public:
1455894Sgblack@eecs.umich.edu            int outstanding;
1465894Sgblack@eecs.umich.edu            RequestPtr requests[2];
1475894Sgblack@eecs.umich.edu            RequestPtr mainReq;
1485894Sgblack@eecs.umich.edu            Fault faults[2];
1495894Sgblack@eecs.umich.edu            uint8_t *data;
1505894Sgblack@eecs.umich.edu            bool read;
1515894Sgblack@eecs.umich.edu
1525894Sgblack@eecs.umich.edu            WholeTranslationState(RequestPtr req1, RequestPtr req2,
1535894Sgblack@eecs.umich.edu                    RequestPtr main, uint8_t *_data, bool _read)
1545894Sgblack@eecs.umich.edu            {
1555894Sgblack@eecs.umich.edu                outstanding = 2;
1565894Sgblack@eecs.umich.edu                requests[0] = req1;
1575894Sgblack@eecs.umich.edu                requests[1] = req2;
1585894Sgblack@eecs.umich.edu                mainReq = main;
1595894Sgblack@eecs.umich.edu                faults[0] = faults[1] = NoFault;
1605894Sgblack@eecs.umich.edu                data = _data;
1615894Sgblack@eecs.umich.edu                read = _read;
1625894Sgblack@eecs.umich.edu            }
1635894Sgblack@eecs.umich.edu        };
1645894Sgblack@eecs.umich.edu
1655894Sgblack@eecs.umich.edu        TimingSimpleCPU *cpu;
1665894Sgblack@eecs.umich.edu        int index;
1675894Sgblack@eecs.umich.edu        WholeTranslationState *state;
1685894Sgblack@eecs.umich.edu
1695894Sgblack@eecs.umich.edu        SplitDataTranslation(TimingSimpleCPU *_cpu, int _index,
1705894Sgblack@eecs.umich.edu                WholeTranslationState *_state) :
1715894Sgblack@eecs.umich.edu            cpu(_cpu), index(_index), state(_state)
1725894Sgblack@eecs.umich.edu        {}
1735894Sgblack@eecs.umich.edu
1745894Sgblack@eecs.umich.edu        void
1755894Sgblack@eecs.umich.edu        finish(Fault fault, RequestPtr req,
1765894Sgblack@eecs.umich.edu                ThreadContext *tc, bool write)
1775894Sgblack@eecs.umich.edu        {
1785894Sgblack@eecs.umich.edu            assert(state);
1795894Sgblack@eecs.umich.edu            assert(state->outstanding);
1805894Sgblack@eecs.umich.edu            state->faults[index] = fault;
1815894Sgblack@eecs.umich.edu            if (--state->outstanding == 0) {
1825894Sgblack@eecs.umich.edu                cpu->sendSplitData(state->faults[0],
1835894Sgblack@eecs.umich.edu                                   state->faults[1],
1845894Sgblack@eecs.umich.edu                                   state->requests[0],
1855894Sgblack@eecs.umich.edu                                   state->requests[1],
1865894Sgblack@eecs.umich.edu                                   state->mainReq,
1875894Sgblack@eecs.umich.edu                                   state->data,
1885894Sgblack@eecs.umich.edu                                   state->read);
1895894Sgblack@eecs.umich.edu                delete state;
1905894Sgblack@eecs.umich.edu            }
1915894Sgblack@eecs.umich.edu            delete this;
1925894Sgblack@eecs.umich.edu        }
1935894Sgblack@eecs.umich.edu    };
1945894Sgblack@eecs.umich.edu
1955894Sgblack@eecs.umich.edu    void sendData(Fault fault, RequestPtr req,
1965894Sgblack@eecs.umich.edu            uint8_t *data, uint64_t *res, bool read);
1975894Sgblack@eecs.umich.edu    void sendSplitData(Fault fault1, Fault fault2,
1985894Sgblack@eecs.umich.edu            RequestPtr req1, RequestPtr req2, RequestPtr req,
1995894Sgblack@eecs.umich.edu            uint8_t *data, bool read);
2005894Sgblack@eecs.umich.edu
2015894Sgblack@eecs.umich.edu    void translationFault(Fault fault);
2025894Sgblack@eecs.umich.edu
2035894Sgblack@eecs.umich.edu    void buildPacket(PacketPtr &pkt, RequestPtr req, bool read);
2045894Sgblack@eecs.umich.edu    void buildSplitPacket(PacketPtr &pkt1, PacketPtr &pkt2,
2055894Sgblack@eecs.umich.edu            RequestPtr req1, RequestPtr req2, RequestPtr req,
2065894Sgblack@eecs.umich.edu            uint8_t *data, bool read);
2075744Sgblack@eecs.umich.edu
2085728Sgblack@eecs.umich.edu    bool handleReadPacket(PacketPtr pkt);
2095728Sgblack@eecs.umich.edu    // This function always implicitly uses dcache_pkt.
2105728Sgblack@eecs.umich.edu    bool handleWritePacket();
2115728Sgblack@eecs.umich.edu
2122623SN/A    class CpuPort : public Port
2132623SN/A    {
2142623SN/A      protected:
2152623SN/A        TimingSimpleCPU *cpu;
2162948Ssaidi@eecs.umich.edu        Tick lat;
2172623SN/A
2182623SN/A      public:
2192623SN/A
2202948Ssaidi@eecs.umich.edu        CpuPort(const std::string &_name, TimingSimpleCPU *_cpu, Tick _lat)
2213401Sktlim@umich.edu            : Port(_name, _cpu), cpu(_cpu), lat(_lat)
2222623SN/A        { }
2232623SN/A
2243647Srdreslin@umich.edu        bool snoopRangeSent;
2253647Srdreslin@umich.edu
2262623SN/A      protected:
2272623SN/A
2283349Sbinkertn@umich.edu        virtual Tick recvAtomic(PacketPtr pkt);
2292623SN/A
2303349Sbinkertn@umich.edu        virtual void recvFunctional(PacketPtr pkt);
2312623SN/A
2322623SN/A        virtual void recvStatusChange(Status status);
2332623SN/A
2342623SN/A        virtual void getDeviceAddressRanges(AddrRangeList &resp,
2354475Sstever@eecs.umich.edu                                            bool &snoop)
2364475Sstever@eecs.umich.edu        { resp.clear(); snoop = false; }
2372948Ssaidi@eecs.umich.edu
2382948Ssaidi@eecs.umich.edu        struct TickEvent : public Event
2392948Ssaidi@eecs.umich.edu        {
2403349Sbinkertn@umich.edu            PacketPtr pkt;
2412948Ssaidi@eecs.umich.edu            TimingSimpleCPU *cpu;
2422948Ssaidi@eecs.umich.edu
2435606Snate@binkert.org            TickEvent(TimingSimpleCPU *_cpu) : cpu(_cpu) {}
2445336Shines@cs.fsu.edu            const char *description() const { return "Timing CPU tick"; }
2453349Sbinkertn@umich.edu            void schedule(PacketPtr _pkt, Tick t);
2462948Ssaidi@eecs.umich.edu        };
2472948Ssaidi@eecs.umich.edu
2482623SN/A    };
2492623SN/A
2502623SN/A    class IcachePort : public CpuPort
2512623SN/A    {
2522623SN/A      public:
2532623SN/A
2542948Ssaidi@eecs.umich.edu        IcachePort(TimingSimpleCPU *_cpu, Tick _lat)
2552948Ssaidi@eecs.umich.edu            : CpuPort(_cpu->name() + "-iport", _cpu, _lat), tickEvent(_cpu)
2562623SN/A        { }
2572623SN/A
2582623SN/A      protected:
2592623SN/A
2603349Sbinkertn@umich.edu        virtual bool recvTiming(PacketPtr pkt);
2612623SN/A
2622657Ssaidi@eecs.umich.edu        virtual void recvRetry();
2632948Ssaidi@eecs.umich.edu
2642948Ssaidi@eecs.umich.edu        struct ITickEvent : public TickEvent
2652948Ssaidi@eecs.umich.edu        {
2662948Ssaidi@eecs.umich.edu
2672948Ssaidi@eecs.umich.edu            ITickEvent(TimingSimpleCPU *_cpu)
2682948Ssaidi@eecs.umich.edu                : TickEvent(_cpu) {}
2692948Ssaidi@eecs.umich.edu            void process();
2705336Shines@cs.fsu.edu            const char *description() const { return "Timing CPU icache tick"; }
2712948Ssaidi@eecs.umich.edu        };
2722948Ssaidi@eecs.umich.edu
2732948Ssaidi@eecs.umich.edu        ITickEvent tickEvent;
2742948Ssaidi@eecs.umich.edu
2752623SN/A    };
2762623SN/A
2772623SN/A    class DcachePort : public CpuPort
2782623SN/A    {
2792623SN/A      public:
2802623SN/A
2812948Ssaidi@eecs.umich.edu        DcachePort(TimingSimpleCPU *_cpu, Tick _lat)
2822948Ssaidi@eecs.umich.edu            : CpuPort(_cpu->name() + "-dport", _cpu, _lat), tickEvent(_cpu)
2832623SN/A        { }
2842623SN/A
2854192Sktlim@umich.edu        virtual void setPeer(Port *port);
2864192Sktlim@umich.edu
2872623SN/A      protected:
2882623SN/A
2893349Sbinkertn@umich.edu        virtual bool recvTiming(PacketPtr pkt);
2902623SN/A
2912657Ssaidi@eecs.umich.edu        virtual void recvRetry();
2922948Ssaidi@eecs.umich.edu
2932948Ssaidi@eecs.umich.edu        struct DTickEvent : public TickEvent
2942948Ssaidi@eecs.umich.edu        {
2952948Ssaidi@eecs.umich.edu            DTickEvent(TimingSimpleCPU *_cpu)
2962948Ssaidi@eecs.umich.edu                : TickEvent(_cpu) {}
2972948Ssaidi@eecs.umich.edu            void process();
2985336Shines@cs.fsu.edu            const char *description() const { return "Timing CPU dcache tick"; }
2992948Ssaidi@eecs.umich.edu        };
3002948Ssaidi@eecs.umich.edu
3012948Ssaidi@eecs.umich.edu        DTickEvent tickEvent;
3022948Ssaidi@eecs.umich.edu
3032623SN/A    };
3042623SN/A
3052623SN/A    IcachePort icachePort;
3062623SN/A    DcachePort dcachePort;
3072623SN/A
3083349Sbinkertn@umich.edu    PacketPtr ifetch_pkt;
3093349Sbinkertn@umich.edu    PacketPtr dcache_pkt;
3102623SN/A
3113222Sktlim@umich.edu    Tick previousTick;
3123170Sstever@eecs.umich.edu
3132623SN/A  public:
3142623SN/A
3152856Srdreslin@umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1);
3162856Srdreslin@umich.edu
3172623SN/A    virtual void serialize(std::ostream &os);
3182623SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
3192623SN/A
3202901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *drain_event);
3212798Sktlim@umich.edu    virtual void resume();
3222798Sktlim@umich.edu
3232798Sktlim@umich.edu    void switchOut();
3242623SN/A    void takeOverFrom(BaseCPU *oldCPU);
3252623SN/A
3262623SN/A    virtual void activateContext(int thread_num, int delay);
3272623SN/A    virtual void suspendContext(int thread_num);
3282623SN/A
3292623SN/A    template <class T>
3302623SN/A    Fault read(Addr addr, T &data, unsigned flags);
3312623SN/A
3322623SN/A    template <class T>
3332623SN/A    Fault write(T data, Addr addr, unsigned flags, uint64_t *res);
3342623SN/A
3352623SN/A    void fetch();
3365894Sgblack@eecs.umich.edu    void sendFetch(Fault fault, RequestPtr req, ThreadContext *tc);
3373349Sbinkertn@umich.edu    void completeIfetch(PacketPtr );
3385894Sgblack@eecs.umich.edu    void completeDataAccess(PacketPtr pkt);
3392644Sstever@eecs.umich.edu    void advanceInst(Fault fault);
3404471Sstever@eecs.umich.edu
3415315Sstever@gmail.com    /**
3425315Sstever@gmail.com     * Print state of address in memory system via PrintReq (for
3435315Sstever@gmail.com     * debugging).
3445315Sstever@gmail.com     */
3455315Sstever@gmail.com    void printAddr(Addr a);
3465315Sstever@gmail.com
3472798Sktlim@umich.edu  private:
3484471Sstever@eecs.umich.edu
3494471Sstever@eecs.umich.edu    typedef EventWrapper<TimingSimpleCPU, &TimingSimpleCPU::fetch> FetchEvent;
3505710Scws3k@cs.virginia.edu    FetchEvent fetchEvent;
3514471Sstever@eecs.umich.edu
3525103Ssaidi@eecs.umich.edu    struct IprEvent : Event {
3535103Ssaidi@eecs.umich.edu        Packet *pkt;
3545103Ssaidi@eecs.umich.edu        TimingSimpleCPU *cpu;
3555103Ssaidi@eecs.umich.edu        IprEvent(Packet *_pkt, TimingSimpleCPU *_cpu, Tick t);
3565103Ssaidi@eecs.umich.edu        virtual void process();
3575336Shines@cs.fsu.edu        virtual const char *description() const;
3585103Ssaidi@eecs.umich.edu    };
3595103Ssaidi@eecs.umich.edu
3602839Sktlim@umich.edu    void completeDrain();
3612623SN/A};
3622623SN/A
3632623SN/A#endif // __CPU_SIMPLE_TIMING_HH__
364