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