timing.hh revision 11608
13560SN/A/*
23560SN/A * Copyright (c) 2012-2013,2015 ARM Limited
33560SN/A * All rights reserved
43560SN/A *
53560SN/A * The license below extends only to copyright in the software and shall
63560SN/A * not be construed as granting a license to any other intellectual
73560SN/A * property including but not limited to intellectual property relating
83560SN/A * to a hardware implementation of the functionality of the software
93560SN/A * licensed hereunder.  You may use the software subject to the license
103560SN/A * terms below provided that you ensure that this notice is replicated
113560SN/A * unmodified and in its entirety in all distributions of the software,
123560SN/A * modified or unmodified, in source code or in binary form.
133560SN/A *
143560SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
153560SN/A * All rights reserved.
163560SN/A *
173560SN/A * Redistribution and use in source and binary forms, with or without
183560SN/A * modification, are permitted provided that the following conditions are
193560SN/A * met: redistributions of source code must retain the above copyright
203560SN/A * notice, this list of conditions and the following disclaimer;
213560SN/A * redistributions in binary form must reproduce the above copyright
223560SN/A * notice, this list of conditions and the following disclaimer in the
233560SN/A * documentation and/or other materials provided with the distribution;
243560SN/A * neither the name of the copyright holders nor the names of its
253560SN/A * contributors may be used to endorse or promote products derived from
263560SN/A * this software without specific prior written permission.
273560SN/A *
283560SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
293560SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
303560SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
313560SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
323560SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
333560SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
343560SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
353560SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
363560SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
373560SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
383560SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393560SN/A *
403560SN/A * Authors: Steve Reinhardt
413565Sgblack@eecs.umich.edu */
423560SN/A
433560SN/A#ifndef __CPU_SIMPLE_TIMING_HH__
443560SN/A#define __CPU_SIMPLE_TIMING_HH__
453560SN/A
463560SN/A#include "cpu/simple/base.hh"
473560SN/A#include "cpu/simple/exec_context.hh"
483560SN/A#include "cpu/translation.hh"
493560SN/A#include "params/TimingSimpleCPU.hh"
503560SN/A
513560SN/Aclass TimingSimpleCPU : public BaseSimpleCPU
523560SN/A{
533560SN/A  public:
543560SN/A
553560SN/A    TimingSimpleCPU(TimingSimpleCPUParams * params);
563560SN/A    virtual ~TimingSimpleCPU();
573560SN/A
583560SN/A    void init() override;
593560SN/A
603560SN/A  private:
613560SN/A
623560SN/A    /*
633560SN/A     * If an access needs to be broken into fragments, currently at most two,
643560SN/A     * the the following two classes are used as the sender state of the
655999Snate@binkert.org     * packets so the CPU can keep track of everything. In the main packet
665999Snate@binkert.org     * sender state, there's an array with a spot for each fragment. If a
673560SN/A     * fragment has already been accepted by the CPU, aka isn't waiting for
685999Snate@binkert.org     * a retry, it's pointer is NULL. After each fragment has successfully
695999Snate@binkert.org     * been processed, the "outstanding" counter is decremented. Once the
703560SN/A     * count is zero, the entire larger access is complete.
715999Snate@binkert.org     */
723560SN/A    class SplitMainSenderState : public Packet::SenderState
735999Snate@binkert.org    {
743560SN/A      public:
753560SN/A        int outstanding;
763560SN/A        PacketPtr fragments[2];
773560SN/A
783560SN/A        int
793560SN/A        getPendingFragment()
803560SN/A        {
813560SN/A            if (fragments[0]) {
823560SN/A                return 0;
833560SN/A            } else if (fragments[1]) {
843560SN/A                return 1;
853560SN/A            } else {
863560SN/A                return -1;
873560SN/A            }
883560SN/A        }
8911168Sandreas.hansson@arm.com    };
9011168Sandreas.hansson@arm.com
913560SN/A    class SplitFragmentSenderState : public Packet::SenderState
923560SN/A    {
935568Snate@binkert.org      public:
945568Snate@binkert.org        SplitFragmentSenderState(PacketPtr _bigPkt, int _index) :
953560SN/A            bigPkt(_bigPkt), index(_index)
963560SN/A        {}
97        PacketPtr bigPkt;
98        int index;
99
100        void
101        clearFromParent()
102        {
103            SplitMainSenderState * main_send_state =
104                dynamic_cast<SplitMainSenderState *>(bigPkt->senderState);
105            main_send_state->fragments[index] = NULL;
106        }
107    };
108
109    class FetchTranslation : public BaseTLB::Translation
110    {
111      protected:
112        TimingSimpleCPU *cpu;
113
114      public:
115        FetchTranslation(TimingSimpleCPU *_cpu)
116            : cpu(_cpu)
117        {}
118
119        void
120        markDelayed()
121        {
122            assert(cpu->_status == BaseSimpleCPU::Running);
123            cpu->_status = ITBWaitResponse;
124        }
125
126        void
127        finish(const Fault &fault, RequestPtr req, ThreadContext *tc,
128               BaseTLB::Mode mode)
129        {
130            cpu->sendFetch(fault, req, tc);
131        }
132    };
133    FetchTranslation fetchTranslation;
134
135    void threadSnoop(PacketPtr pkt, ThreadID sender);
136    void sendData(RequestPtr req, uint8_t *data, uint64_t *res, bool read);
137    void sendSplitData(RequestPtr req1, RequestPtr req2, RequestPtr req,
138                       uint8_t *data, bool read);
139
140    void translationFault(const Fault &fault);
141
142    PacketPtr buildPacket(RequestPtr req, bool read);
143    void buildSplitPacket(PacketPtr &pkt1, PacketPtr &pkt2,
144            RequestPtr req1, RequestPtr req2, RequestPtr req,
145            uint8_t *data, bool read);
146
147    bool handleReadPacket(PacketPtr pkt);
148    // This function always implicitly uses dcache_pkt.
149    bool handleWritePacket();
150
151    /**
152     * A TimingCPUPort overrides the default behaviour of the
153     * recvTiming and recvRetry and implements events for the
154     * scheduling of handling of incoming packets in the following
155     * cycle.
156     */
157    class TimingCPUPort : public MasterPort
158    {
159      public:
160
161        TimingCPUPort(const std::string& _name, TimingSimpleCPU* _cpu)
162            : MasterPort(_name, _cpu), cpu(_cpu), retryRespEvent(this)
163        { }
164
165      protected:
166
167        TimingSimpleCPU* cpu;
168
169        struct TickEvent : public Event
170        {
171            PacketPtr pkt;
172            TimingSimpleCPU *cpu;
173
174            TickEvent(TimingSimpleCPU *_cpu) : pkt(NULL), cpu(_cpu) {}
175            const char *description() const { return "Timing CPU tick"; }
176            void schedule(PacketPtr _pkt, Tick t);
177        };
178
179        EventWrapper<MasterPort, &MasterPort::sendRetryResp> retryRespEvent;
180    };
181
182    class IcachePort : public TimingCPUPort
183    {
184      public:
185
186        IcachePort(TimingSimpleCPU *_cpu)
187            : TimingCPUPort(_cpu->name() + ".icache_port", _cpu),
188              tickEvent(_cpu)
189        { }
190
191      protected:
192
193        virtual bool recvTimingResp(PacketPtr pkt);
194
195        virtual void recvReqRetry();
196
197        struct ITickEvent : public TickEvent
198        {
199
200            ITickEvent(TimingSimpleCPU *_cpu)
201                : TickEvent(_cpu) {}
202            void process();
203            const char *description() const { return "Timing CPU icache tick"; }
204        };
205
206        ITickEvent tickEvent;
207
208    };
209
210    class DcachePort : public TimingCPUPort
211    {
212      public:
213
214        DcachePort(TimingSimpleCPU *_cpu)
215            : TimingCPUPort(_cpu->name() + ".dcache_port", _cpu),
216              tickEvent(_cpu)
217        {
218           cacheBlockMask = ~(cpu->cacheLineSize() - 1);
219        }
220
221        Addr cacheBlockMask;
222      protected:
223
224        /** Snoop a coherence request, we need to check if this causes
225         * a wakeup event on a cpu that is monitoring an address
226         */
227        virtual void recvTimingSnoopReq(PacketPtr pkt);
228        virtual void recvFunctionalSnoop(PacketPtr pkt);
229
230        virtual bool recvTimingResp(PacketPtr pkt);
231
232        virtual void recvReqRetry();
233
234        virtual bool isSnooping() const {
235            return true;
236        }
237
238        struct DTickEvent : public TickEvent
239        {
240            DTickEvent(TimingSimpleCPU *_cpu)
241                : TickEvent(_cpu) {}
242            void process();
243            const char *description() const { return "Timing CPU dcache tick"; }
244        };
245
246        DTickEvent tickEvent;
247
248    };
249
250    void updateCycleCounts();
251
252    IcachePort icachePort;
253    DcachePort dcachePort;
254
255    PacketPtr ifetch_pkt;
256    PacketPtr dcache_pkt;
257
258    Cycles previousCycle;
259
260  protected:
261
262     /** Return a reference to the data port. */
263    MasterPort &getDataPort() override { return dcachePort; }
264
265    /** Return a reference to the instruction port. */
266    MasterPort &getInstPort() override { return icachePort; }
267
268  public:
269
270    DrainState drain() override;
271    void drainResume() override;
272
273    void switchOut() override;
274    void takeOverFrom(BaseCPU *oldCPU) override;
275
276    void verifyMemoryMode() const override;
277
278    void activateContext(ThreadID thread_num) override;
279    void suspendContext(ThreadID thread_num) override;
280
281    Fault readMem(Addr addr, uint8_t *data, unsigned size,
282                  Request::Flags flags) override;
283
284    Fault initiateMemRead(Addr addr, unsigned size,
285                          Request::Flags flags) override;
286
287    Fault writeMem(uint8_t *data, unsigned size,
288                   Addr addr, Request::Flags flags, uint64_t *res) override;
289
290    void fetch();
291    void sendFetch(const Fault &fault, RequestPtr req, ThreadContext *tc);
292    void completeIfetch(PacketPtr );
293    void completeDataAccess(PacketPtr pkt);
294    void advanceInst(const Fault &fault);
295
296    /** This function is used by the page table walker to determine if it could
297     * translate the a pending request or if the underlying request has been
298     * squashed. This always returns false for the simple timing CPU as it never
299     * executes any instructions speculatively.
300     * @ return Is the current instruction squashed?
301     */
302    bool isSquashed() const { return false; }
303
304    /**
305     * Print state of address in memory system via PrintReq (for
306     * debugging).
307     */
308    void printAddr(Addr a);
309
310    /**
311     * Finish a DTB translation.
312     * @param state The DTB translation state.
313     */
314    void finishTranslation(WholeTranslationState *state);
315
316  private:
317
318    typedef EventWrapper<TimingSimpleCPU, &TimingSimpleCPU::fetch> FetchEvent;
319    FetchEvent fetchEvent;
320
321    struct IprEvent : Event {
322        Packet *pkt;
323        TimingSimpleCPU *cpu;
324        IprEvent(Packet *_pkt, TimingSimpleCPU *_cpu, Tick t);
325        virtual void process();
326        virtual const char *description() const;
327    };
328
329    /**
330     * Check if a system is in a drained state.
331     *
332     * We need to drain if:
333     * <ul>
334     * <li>We are in the middle of a microcode sequence as some CPUs
335     *     (e.g., HW accelerated CPUs) can't be started in the middle
336     *     of a gem5 microcode sequence.
337     *
338     * <li>Stay at PC is true.
339     *
340     * <li>A fetch event is scheduled. Normally this would never be the
341     *     case with microPC() == 0, but right after a context is
342     *     activated it can happen.
343     * </ul>
344     */
345    bool isDrained() {
346        SimpleExecContext& t_info = *threadInfo[curThread];
347        SimpleThread* thread = t_info.thread;
348
349        return thread->microPC() == 0 && !t_info.stayAtPC &&
350               !fetchEvent.scheduled();
351    }
352
353    /**
354     * Try to complete a drain request.
355     *
356     * @returns true if the CPU is drained, false otherwise.
357     */
358    bool tryCompleteDrain();
359};
360
361#endif // __CPU_SIMPLE_TIMING_HH__
362