dram_ctrl.hh revision 11677:beaf1afe2f83
1/*
2 * Copyright (c) 2012-2016 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2013 Amin Farmahini-Farahani
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Andreas Hansson
41 *          Ani Udipi
42 *          Neha Agarwal
43 *          Omar Naji
44 *          Matthias Jung
45 */
46
47/**
48 * @file
49 * DRAMCtrl declaration
50 */
51
52#ifndef __MEM_DRAM_CTRL_HH__
53#define __MEM_DRAM_CTRL_HH__
54
55#include <deque>
56#include <string>
57#include <unordered_set>
58
59#include "base/callback.hh"
60#include "base/statistics.hh"
61#include "enums/AddrMap.hh"
62#include "enums/MemSched.hh"
63#include "enums/PageManage.hh"
64#include "mem/abstract_mem.hh"
65#include "mem/qport.hh"
66#include "params/DRAMCtrl.hh"
67#include "sim/eventq.hh"
68#include "mem/drampower.hh"
69
70/**
71 * The DRAM controller is a single-channel memory controller capturing
72 * the most important timing constraints associated with a
73 * contemporary DRAM. For multi-channel memory systems, the controller
74 * is combined with a crossbar model, with the channel address
75 * interleaving taking part in the crossbar.
76 *
77 * As a basic design principle, this controller
78 * model is not cycle callable, but instead uses events to: 1) decide
79 * when new decisions can be made, 2) when resources become available,
80 * 3) when things are to be considered done, and 4) when to send
81 * things back. Through these simple principles, the model delivers
82 * high performance, and lots of flexibility, allowing users to
83 * evaluate the system impact of a wide range of memory technologies,
84 * such as DDR3/4, LPDDR2/3/4, WideIO1/2, HBM and HMC.
85 *
86 * For more details, please see Hansson et al, "Simulating DRAM
87 * controllers for future system architecture exploration",
88 * Proc. ISPASS, 2014. If you use this model as part of your research
89 * please cite the paper.
90 */
91class DRAMCtrl : public AbstractMemory
92{
93
94  private:
95
96    // For now, make use of a queued slave port to avoid dealing with
97    // flow control for the responses being sent back
98    class MemoryPort : public QueuedSlavePort
99    {
100
101        RespPacketQueue queue;
102        DRAMCtrl& memory;
103
104      public:
105
106        MemoryPort(const std::string& name, DRAMCtrl& _memory);
107
108      protected:
109
110        Tick recvAtomic(PacketPtr pkt);
111
112        void recvFunctional(PacketPtr pkt);
113
114        bool recvTimingReq(PacketPtr);
115
116        virtual AddrRangeList getAddrRanges() const;
117
118    };
119
120    /**
121     * Our incoming port, for a multi-ported controller add a crossbar
122     * in front of it
123     */
124    MemoryPort port;
125
126    /**
127     * Remeber if the memory system is in timing mode
128     */
129    bool isTimingMode;
130
131    /**
132     * Remember if we have to retry a request when available.
133     */
134    bool retryRdReq;
135    bool retryWrReq;
136
137    /**
138     * Bus state used to control the read/write switching and drive
139     * the scheduling of the next request.
140     */
141    enum BusState {
142        READ = 0,
143        READ_TO_WRITE,
144        WRITE,
145        WRITE_TO_READ
146    };
147
148    BusState busState;
149
150    /**
151     * Simple structure to hold the values needed to keep track of
152     * commands for DRAMPower
153     */
154    struct Command {
155       Data::MemCommand::cmds type;
156       uint8_t bank;
157       Tick timeStamp;
158
159       constexpr Command(Data::MemCommand::cmds _type, uint8_t _bank,
160                         Tick time_stamp)
161            : type(_type), bank(_bank), timeStamp(time_stamp)
162        { }
163    };
164
165    /**
166     * A basic class to track the bank state, i.e. what row is
167     * currently open (if any), when is the bank free to accept a new
168     * column (read/write) command, when can it be precharged, and
169     * when can it be activated.
170     *
171     * The bank also keeps track of how many bytes have been accessed
172     * in the open row since it was opened.
173     */
174    class Bank
175    {
176
177      public:
178
179        static const uint32_t NO_ROW = -1;
180
181        uint32_t openRow;
182        uint8_t bank;
183        uint8_t bankgr;
184
185        Tick colAllowedAt;
186        Tick preAllowedAt;
187        Tick actAllowedAt;
188
189        uint32_t rowAccesses;
190        uint32_t bytesAccessed;
191
192        Bank() :
193            openRow(NO_ROW), bank(0), bankgr(0),
194            colAllowedAt(0), preAllowedAt(0), actAllowedAt(0),
195            rowAccesses(0), bytesAccessed(0)
196        { }
197    };
198
199
200    /**
201     * Rank class includes a vector of banks. Refresh and Power state
202     * machines are defined per rank. Events required to change the
203     * state of the refresh and power state machine are scheduled per
204     * rank. This class allows the implementation of rank-wise refresh
205     * and rank-wise power-down.
206     */
207    class Rank : public EventManager
208    {
209
210      private:
211
212        /**
213         * The power state captures the different operational states of
214         * the DRAM and interacts with the bus read/write state machine,
215         * and the refresh state machine. In the idle state all banks are
216         * precharged. From there we either go to an auto refresh (as
217         * determined by the refresh state machine), or to a precharge
218         * power down mode. From idle the memory can also go to the active
219         * state (with one or more banks active), and in turn from there
220         * to active power down. At the moment we do not capture the deep
221         * power down and self-refresh state.
222         */
223        enum PowerState {
224            PWR_IDLE = 0,
225            PWR_REF,
226            PWR_PRE_PDN,
227            PWR_ACT,
228            PWR_ACT_PDN
229        };
230
231        /**
232         * The refresh state is used to control the progress of the
233         * refresh scheduling. When normal operation is in progress the
234         * refresh state is idle. From there, it progresses to the refresh
235         * drain state once tREFI has passed. The refresh drain state
236         * captures the DRAM row active state, as it will stay there until
237         * all ongoing accesses complete. Thereafter all banks are
238         * precharged, and lastly, the DRAM is refreshed.
239         */
240        enum RefreshState {
241            REF_IDLE = 0,
242            REF_DRAIN,
243            REF_PRE,
244            REF_RUN
245        };
246
247        /**
248         * A reference to the parent DRAMCtrl instance
249         */
250        DRAMCtrl& memory;
251
252        /**
253         * Since we are taking decisions out of order, we need to keep
254         * track of what power transition is happening at what time, such
255         * that we can go back in time and change history. For example, if
256         * we precharge all banks and schedule going to the idle state, we
257         * might at a later point decide to activate a bank before the
258         * transition to idle would have taken place.
259         */
260        PowerState pwrStateTrans;
261
262        /**
263         * Current power state.
264         */
265        PowerState pwrState;
266
267        /**
268         * Track when we transitioned to the current power state
269         */
270        Tick pwrStateTick;
271
272        /**
273         * current refresh state
274         */
275        RefreshState refreshState;
276
277        /**
278         * Keep track of when a refresh is due.
279         */
280        Tick refreshDueAt;
281
282        /*
283         * Command energies
284         */
285        Stats::Scalar actEnergy;
286        Stats::Scalar preEnergy;
287        Stats::Scalar readEnergy;
288        Stats::Scalar writeEnergy;
289        Stats::Scalar refreshEnergy;
290
291        /*
292         * Active Background Energy
293         */
294        Stats::Scalar actBackEnergy;
295
296        /*
297         * Precharge Background Energy
298         */
299        Stats::Scalar preBackEnergy;
300
301        Stats::Scalar totalEnergy;
302        Stats::Scalar averagePower;
303
304        /**
305         * Track time spent in each power state.
306         */
307        Stats::Vector pwrStateTime;
308
309        /**
310         * Function to update Power Stats
311         */
312        void updatePowerStats();
313
314        /**
315         * Schedule a power state transition in the future, and
316         * potentially override an already scheduled transition.
317         *
318         * @param pwr_state Power state to transition to
319         * @param tick Tick when transition should take place
320         */
321        void schedulePowerEvent(PowerState pwr_state, Tick tick);
322
323      public:
324
325        /**
326         * Current Rank index
327         */
328        uint8_t rank;
329
330        /**
331         * One DRAMPower instance per rank
332         */
333        DRAMPower power;
334
335        /**
336         * List of comamnds issued, to be sent to DRAMPpower at refresh
337         * and stats dump.  Keep commands here since commands to different
338         * banks are added out of order.  Will only pass commands up to
339         * curTick() to DRAMPower after sorting.
340         */
341        std::vector<Command> cmdList;
342
343        /**
344         * Vector of Banks. Each rank is made of several devices which in
345         * term are made from several banks.
346         */
347        std::vector<Bank> banks;
348
349        /**
350         *  To track number of banks which are currently active for
351         *  this rank.
352         */
353        unsigned int numBanksActive;
354
355        /** List to keep track of activate ticks */
356        std::deque<Tick> actTicks;
357
358        Rank(DRAMCtrl& _memory, const DRAMCtrlParams* _p);
359
360        const std::string name() const
361        {
362            return csprintf("%s_%d", memory.name(), rank);
363        }
364
365        /**
366         * Kick off accounting for power and refresh states and
367         * schedule initial refresh.
368         *
369         * @param ref_tick Tick for first refresh
370         */
371        void startup(Tick ref_tick);
372
373        /**
374         * Stop the refresh events.
375         */
376        void suspend();
377
378        /**
379         * Check if the current rank is available for scheduling.
380         *
381         * @param Return true if the rank is idle from a refresh point of view
382         */
383        bool isAvailable() const { return refreshState == REF_IDLE; }
384
385        /**
386         * Check if the current rank has all banks closed and is not
387         * in a low power state
388         *
389         * @param Return true if the rank is idle from a bank
390         *        and power point of view
391         */
392        bool inPwrIdleState() const { return pwrState == PWR_IDLE; }
393
394        /**
395         * Let the rank check if it was waiting for requests to drain
396         * to allow it to transition states.
397         */
398        void checkDrainDone();
399
400        /**
401         * Push command out of cmdList queue that are scheduled at
402         * or before curTick() to DRAMPower library
403         * All commands before curTick are guaranteed to be complete
404         * and can safely be flushed.
405         */
406        void flushCmdList();
407
408        /*
409         * Function to register Stats
410         */
411        void regStats();
412
413        /**
414         * Computes stats just prior to dump event
415         */
416        void computeStats();
417
418        void processActivateEvent();
419        EventWrapper<Rank, &Rank::processActivateEvent>
420        activateEvent;
421
422        void processPrechargeEvent();
423        EventWrapper<Rank, &Rank::processPrechargeEvent>
424        prechargeEvent;
425
426        void processRefreshEvent();
427        EventWrapper<Rank, &Rank::processRefreshEvent>
428        refreshEvent;
429
430        void processPowerEvent();
431        EventWrapper<Rank, &Rank::processPowerEvent>
432        powerEvent;
433
434    };
435
436    // define the process to compute stats on simulation exit
437    // defined per rank as the per rank stats are based on state
438    // transition and periodically updated, requiring re-sync at
439    // exit.
440    class RankDumpCallback : public Callback
441    {
442        Rank *ranks;
443      public:
444        RankDumpCallback(Rank *r) : ranks(r) {}
445        virtual void process() { ranks->computeStats(); };
446    };
447
448    /**
449     * A burst helper helps organize and manage a packet that is larger than
450     * the DRAM burst size. A system packet that is larger than the burst size
451     * is split into multiple DRAM packets and all those DRAM packets point to
452     * a single burst helper such that we know when the whole packet is served.
453     */
454    class BurstHelper {
455
456      public:
457
458        /** Number of DRAM bursts requred for a system packet **/
459        const unsigned int burstCount;
460
461        /** Number of DRAM bursts serviced so far for a system packet **/
462        unsigned int burstsServiced;
463
464        BurstHelper(unsigned int _burstCount)
465            : burstCount(_burstCount), burstsServiced(0)
466        { }
467    };
468
469    /**
470     * A DRAM packet stores packets along with the timestamp of when
471     * the packet entered the queue, and also the decoded address.
472     */
473    class DRAMPacket {
474
475      public:
476
477        /** When did request enter the controller */
478        const Tick entryTime;
479
480        /** When will request leave the controller */
481        Tick readyTime;
482
483        /** This comes from the outside world */
484        const PacketPtr pkt;
485
486        const bool isRead;
487
488        /** Will be populated by address decoder */
489        const uint8_t rank;
490        const uint8_t bank;
491        const uint32_t row;
492
493        /**
494         * Bank id is calculated considering banks in all the ranks
495         * eg: 2 ranks each with 8 banks, then bankId = 0 --> rank0, bank0 and
496         * bankId = 8 --> rank1, bank0
497         */
498        const uint16_t bankId;
499
500        /**
501         * The starting address of the DRAM packet.
502         * This address could be unaligned to burst size boundaries. The
503         * reason is to keep the address offset so we can accurately check
504         * incoming read packets with packets in the write queue.
505         */
506        Addr addr;
507
508        /**
509         * The size of this dram packet in bytes
510         * It is always equal or smaller than DRAM burst size
511         */
512        unsigned int size;
513
514        /**
515         * A pointer to the BurstHelper if this DRAMPacket is a split packet
516         * If not a split packet (common case), this is set to NULL
517         */
518        BurstHelper* burstHelper;
519        Bank& bankRef;
520        Rank& rankRef;
521
522        DRAMPacket(PacketPtr _pkt, bool is_read, uint8_t _rank, uint8_t _bank,
523                   uint32_t _row, uint16_t bank_id, Addr _addr,
524                   unsigned int _size, Bank& bank_ref, Rank& rank_ref)
525            : entryTime(curTick()), readyTime(curTick()),
526              pkt(_pkt), isRead(is_read), rank(_rank), bank(_bank), row(_row),
527              bankId(bank_id), addr(_addr), size(_size), burstHelper(NULL),
528              bankRef(bank_ref), rankRef(rank_ref)
529        { }
530
531    };
532
533    /**
534     * Bunch of things requires to setup "events" in gem5
535     * When event "respondEvent" occurs for example, the method
536     * processRespondEvent is called; no parameters are allowed
537     * in these methods
538     */
539    void processNextReqEvent();
540    EventWrapper<DRAMCtrl,&DRAMCtrl::processNextReqEvent> nextReqEvent;
541
542    void processRespondEvent();
543    EventWrapper<DRAMCtrl, &DRAMCtrl::processRespondEvent> respondEvent;
544
545    /**
546     * Check if the read queue has room for more entries
547     *
548     * @param pktCount The number of entries needed in the read queue
549     * @return true if read queue is full, false otherwise
550     */
551    bool readQueueFull(unsigned int pktCount) const;
552
553    /**
554     * Check if the write queue has room for more entries
555     *
556     * @param pktCount The number of entries needed in the write queue
557     * @return true if write queue is full, false otherwise
558     */
559    bool writeQueueFull(unsigned int pktCount) const;
560
561    /**
562     * When a new read comes in, first check if the write q has a
563     * pending request to the same address.\ If not, decode the
564     * address to populate rank/bank/row, create one or mutliple
565     * "dram_pkt", and push them to the back of the read queue.\
566     * If this is the only
567     * read request in the system, schedule an event to start
568     * servicing it.
569     *
570     * @param pkt The request packet from the outside world
571     * @param pktCount The number of DRAM bursts the pkt
572     * translate to. If pkt size is larger then one full burst,
573     * then pktCount is greater than one.
574     */
575    void addToReadQueue(PacketPtr pkt, unsigned int pktCount);
576
577    /**
578     * Decode the incoming pkt, create a dram_pkt and push to the
579     * back of the write queue. \If the write q length is more than
580     * the threshold specified by the user, ie the queue is beginning
581     * to get full, stop reads, and start draining writes.
582     *
583     * @param pkt The request packet from the outside world
584     * @param pktCount The number of DRAM bursts the pkt
585     * translate to. If pkt size is larger then one full burst,
586     * then pktCount is greater than one.
587     */
588    void addToWriteQueue(PacketPtr pkt, unsigned int pktCount);
589
590    /**
591     * Actually do the DRAM access - figure out the latency it
592     * will take to service the req based on bank state, channel state etc
593     * and then update those states to account for this request.\ Based
594     * on this, update the packet's "readyTime" and move it to the
595     * response q from where it will eventually go back to the outside
596     * world.
597     *
598     * @param pkt The DRAM packet created from the outside world pkt
599     */
600    void doDRAMAccess(DRAMPacket* dram_pkt);
601
602    /**
603     * When a packet reaches its "readyTime" in the response Q,
604     * use the "access()" method in AbstractMemory to actually
605     * create the response packet, and send it back to the outside
606     * world requestor.
607     *
608     * @param pkt The packet from the outside world
609     * @param static_latency Static latency to add before sending the packet
610     */
611    void accessAndRespond(PacketPtr pkt, Tick static_latency);
612
613    /**
614     * Address decoder to figure out physical mapping onto ranks,
615     * banks, and rows. This function is called multiple times on the same
616     * system packet if the pakcet is larger than burst of the memory. The
617     * dramPktAddr is used for the offset within the packet.
618     *
619     * @param pkt The packet from the outside world
620     * @param dramPktAddr The starting address of the DRAM packet
621     * @param size The size of the DRAM packet in bytes
622     * @param isRead Is the request for a read or a write to DRAM
623     * @return A DRAMPacket pointer with the decoded information
624     */
625    DRAMPacket* decodeAddr(PacketPtr pkt, Addr dramPktAddr, unsigned int size,
626                           bool isRead);
627
628    /**
629     * The memory schduler/arbiter - picks which request needs to
630     * go next, based on the specified policy such as FCFS or FR-FCFS
631     * and moves it to the head of the queue.
632     * Prioritizes accesses to the same rank as previous burst unless
633     * controller is switching command type.
634     *
635     * @param queue Queued requests to consider
636     * @param extra_col_delay Any extra delay due to a read/write switch
637     * @return true if a packet is scheduled to a rank which is available else
638     * false
639     */
640    bool chooseNext(std::deque<DRAMPacket*>& queue, Tick extra_col_delay);
641
642    /**
643     * For FR-FCFS policy reorder the read/write queue depending on row buffer
644     * hits and earliest bursts available in DRAM
645     *
646     * @param queue Queued requests to consider
647     * @param extra_col_delay Any extra delay due to a read/write switch
648     * @return true if a packet is scheduled to a rank which is available else
649     * false
650     */
651    bool reorderQueue(std::deque<DRAMPacket*>& queue, Tick extra_col_delay);
652
653    /**
654     * Find which are the earliest banks ready to issue an activate
655     * for the enqueued requests. Assumes maximum of 64 banks per DIMM
656     * Also checks if the bank is already prepped.
657     *
658     * @param queue Queued requests to consider
659     * @param time of seamless burst command
660     * @return One-hot encoded mask of bank indices
661     * @return boolean indicating burst can issue seamlessly, with no gaps
662     */
663    std::pair<uint64_t, bool> minBankPrep(const std::deque<DRAMPacket*>& queue,
664                                          Tick min_col_at) const;
665
666    /**
667     * Keep track of when row activations happen, in order to enforce
668     * the maximum number of activations in the activation window. The
669     * method updates the time that the banks become available based
670     * on the current limits.
671     *
672     * @param rank_ref Reference to the rank
673     * @param bank_ref Reference to the bank
674     * @param act_tick Time when the activation takes place
675     * @param row Index of the row
676     */
677    void activateBank(Rank& rank_ref, Bank& bank_ref, Tick act_tick,
678                      uint32_t row);
679
680    /**
681     * Precharge a given bank and also update when the precharge is
682     * done. This will also deal with any stats related to the
683     * accesses to the open page.
684     *
685     * @param rank_ref The rank to precharge
686     * @param bank_ref The bank to precharge
687     * @param pre_at Time when the precharge takes place
688     * @param trace Is this an auto precharge then do not add to trace
689     */
690    void prechargeBank(Rank& rank_ref, Bank& bank_ref,
691                       Tick pre_at, bool trace = true);
692
693    /**
694     * Used for debugging to observe the contents of the queues.
695     */
696    void printQs() const;
697
698    /**
699     * Burst-align an address.
700     *
701     * @param addr The potentially unaligned address
702     *
703     * @return An address aligned to a DRAM burst
704     */
705    Addr burstAlign(Addr addr) const { return (addr & ~(Addr(burstSize - 1))); }
706
707    /**
708     * The controller's main read and write queues
709     */
710    std::deque<DRAMPacket*> readQueue;
711    std::deque<DRAMPacket*> writeQueue;
712
713    /**
714     * To avoid iterating over the write queue to check for
715     * overlapping transactions, maintain a set of burst addresses
716     * that are currently queued. Since we merge writes to the same
717     * location we never have more than one address to the same burst
718     * address.
719     */
720    std::unordered_set<Addr> isInWriteQueue;
721
722    /**
723     * Response queue where read packets wait after we're done working
724     * with them, but it's not time to send the response yet. The
725     * responses are stored seperately mostly to keep the code clean
726     * and help with events scheduling. For all logical purposes such
727     * as sizing the read queue, this and the main read queue need to
728     * be added together.
729     */
730    std::deque<DRAMPacket*> respQueue;
731
732    /**
733     * Vector of ranks
734     */
735    std::vector<Rank*> ranks;
736
737    /**
738     * The following are basic design parameters of the memory
739     * controller, and are initialized based on parameter values.
740     * The rowsPerBank is determined based on the capacity, number of
741     * ranks and banks, the burst size, and the row buffer size.
742     */
743    const uint32_t deviceSize;
744    const uint32_t deviceBusWidth;
745    const uint32_t burstLength;
746    const uint32_t deviceRowBufferSize;
747    const uint32_t devicesPerRank;
748    const uint32_t burstSize;
749    const uint32_t rowBufferSize;
750    const uint32_t columnsPerRowBuffer;
751    const uint32_t columnsPerStripe;
752    const uint32_t ranksPerChannel;
753    const uint32_t bankGroupsPerRank;
754    const bool bankGroupArch;
755    const uint32_t banksPerRank;
756    const uint32_t channels;
757    uint32_t rowsPerBank;
758    const uint32_t readBufferSize;
759    const uint32_t writeBufferSize;
760    const uint32_t writeHighThreshold;
761    const uint32_t writeLowThreshold;
762    const uint32_t minWritesPerSwitch;
763    uint32_t writesThisTime;
764    uint32_t readsThisTime;
765
766    /**
767     * Basic memory timing parameters initialized based on parameter
768     * values.
769     */
770    const Tick M5_CLASS_VAR_USED tCK;
771    const Tick tWTR;
772    const Tick tRTW;
773    const Tick tCS;
774    const Tick tBURST;
775    const Tick tCCD_L;
776    const Tick tRCD;
777    const Tick tCL;
778    const Tick tRP;
779    const Tick tRAS;
780    const Tick tWR;
781    const Tick tRTP;
782    const Tick tRFC;
783    const Tick tREFI;
784    const Tick tRRD;
785    const Tick tRRD_L;
786    const Tick tXAW;
787    const Tick tXP;
788    const Tick tXS;
789    const uint32_t activationLimit;
790
791    /**
792     * Memory controller configuration initialized based on parameter
793     * values.
794     */
795    Enums::MemSched memSchedPolicy;
796    Enums::AddrMap addrMapping;
797    Enums::PageManage pageMgmt;
798
799    /**
800     * Max column accesses (read and write) per row, before forefully
801     * closing it.
802     */
803    const uint32_t maxAccessesPerRow;
804
805    /**
806     * Pipeline latency of the controller frontend. The frontend
807     * contribution is added to writes (that complete when they are in
808     * the write buffer) and reads that are serviced the write buffer.
809     */
810    const Tick frontendLatency;
811
812    /**
813     * Pipeline latency of the backend and PHY. Along with the
814     * frontend contribution, this latency is added to reads serviced
815     * by the DRAM.
816     */
817    const Tick backendLatency;
818
819    /**
820     * Till when has the main data bus been spoken for already?
821     */
822    Tick busBusyUntil;
823
824    Tick prevArrival;
825
826    /**
827     * The soonest you have to start thinking about the next request
828     * is the longest access time that can occur before
829     * busBusyUntil. Assuming you need to precharge, open a new row,
830     * and access, it is tRP + tRCD + tCL.
831     */
832    Tick nextReqTime;
833
834    // All statistics that the model needs to capture
835    Stats::Scalar readReqs;
836    Stats::Scalar writeReqs;
837    Stats::Scalar readBursts;
838    Stats::Scalar writeBursts;
839    Stats::Scalar bytesReadDRAM;
840    Stats::Scalar bytesReadWrQ;
841    Stats::Scalar bytesWritten;
842    Stats::Scalar bytesReadSys;
843    Stats::Scalar bytesWrittenSys;
844    Stats::Scalar servicedByWrQ;
845    Stats::Scalar mergedWrBursts;
846    Stats::Scalar neitherReadNorWrite;
847    Stats::Vector perBankRdBursts;
848    Stats::Vector perBankWrBursts;
849    Stats::Scalar numRdRetry;
850    Stats::Scalar numWrRetry;
851    Stats::Scalar totGap;
852    Stats::Vector readPktSize;
853    Stats::Vector writePktSize;
854    Stats::Vector rdQLenPdf;
855    Stats::Vector wrQLenPdf;
856    Stats::Histogram bytesPerActivate;
857    Stats::Histogram rdPerTurnAround;
858    Stats::Histogram wrPerTurnAround;
859
860    // Latencies summed over all requests
861    Stats::Scalar totQLat;
862    Stats::Scalar totMemAccLat;
863    Stats::Scalar totBusLat;
864
865    // Average latencies per request
866    Stats::Formula avgQLat;
867    Stats::Formula avgBusLat;
868    Stats::Formula avgMemAccLat;
869
870    // Average bandwidth
871    Stats::Formula avgRdBW;
872    Stats::Formula avgWrBW;
873    Stats::Formula avgRdBWSys;
874    Stats::Formula avgWrBWSys;
875    Stats::Formula peakBW;
876    Stats::Formula busUtil;
877    Stats::Formula busUtilRead;
878    Stats::Formula busUtilWrite;
879
880    // Average queue lengths
881    Stats::Average avgRdQLen;
882    Stats::Average avgWrQLen;
883
884    // Row hit count and rate
885    Stats::Scalar readRowHits;
886    Stats::Scalar writeRowHits;
887    Stats::Formula readRowHitRate;
888    Stats::Formula writeRowHitRate;
889    Stats::Formula avgGap;
890
891    // DRAM Power Calculation
892    Stats::Formula pageHitRate;
893
894    // Holds the value of the rank of burst issued
895    uint8_t activeRank;
896
897    // timestamp offset
898    uint64_t timeStampOffset;
899
900    /**
901     * Upstream caches need this packet until true is returned, so
902     * hold it for deletion until a subsequent call
903     */
904    std::unique_ptr<Packet> pendingDelete;
905
906    /**
907     * This function increments the energy when called. If stats are
908     * dumped periodically, note accumulated energy values will
909     * appear in the stats (even if the stats are reset). This is a
910     * result of the energy values coming from DRAMPower, and there
911     * is currently no support for resetting the state.
912     *
913     * @param rank Currrent rank
914     */
915    void updatePowerStats(Rank& rank_ref);
916
917    /**
918     * Function for sorting Command structures based on timeStamp
919     *
920     * @param a Memory Command
921     * @param next Memory Command
922     * @return true if timeStamp of Command 1 < timeStamp of Command 2
923     */
924    static bool sortTime(const Command& cmd, const Command& cmd_next) {
925        return cmd.timeStamp < cmd_next.timeStamp;
926    };
927
928  public:
929
930    void regStats() override;
931
932    DRAMCtrl(const DRAMCtrlParams* p);
933
934    DrainState drain() override;
935
936    virtual BaseSlavePort& getSlavePort(const std::string& if_name,
937                                        PortID idx = InvalidPortID) override;
938
939    virtual void init() override;
940    virtual void startup() override;
941    virtual void drainResume() override;
942
943    /**
944     * Return true once refresh is complete for all ranks and there are no
945     * additional commands enqueued.  (only evaluated when draining)
946     * This will ensure that all banks are closed, power state is IDLE, and
947     * power stats have been updated
948     *
949     * @return true if all ranks have refreshed, with no commands enqueued
950     *
951     */
952    bool allRanksDrained() const;
953
954  protected:
955
956    Tick recvAtomic(PacketPtr pkt);
957    void recvFunctional(PacketPtr pkt);
958    bool recvTimingReq(PacketPtr pkt);
959
960};
961
962#endif //__MEM_DRAM_CTRL_HH__
963