2 * Copyright (c) 2012-2015 ARM Limited
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.
14 * Copyright (c) 2013 Amin Farmahini-Farahani
15 * All rights reserved.
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.
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.
40 * Authors: Andreas Hansson
48 * DRAMCtrl declaration
51 #ifndef __MEM_DRAM_CTRL_HH__
52 #define __MEM_DRAM_CTRL_HH__
56 #include <unordered_set>
58 #include "base/statistics.hh"
59 #include "enums/AddrMap.hh"
60 #include "enums/MemSched.hh"
61 #include "enums/PageManage.hh"
62 #include "mem/abstract_mem.hh"
63 #include "mem/qport.hh"
64 #include "params/DRAMCtrl.hh"
65 #include "sim/eventq.hh"
66 #include "mem/drampower.hh"
69 * The DRAM controller is a single-channel memory controller capturing
70 * the most important timing constraints associated with a
71 * contemporary DRAM. For multi-channel memory systems, the controller
72 * is combined with a crossbar model, with the channel address
73 * interleaving taking part in the crossbar.
75 * As a basic design principle, this controller
76 * model is not cycle callable, but instead uses events to: 1) decide
77 * when new decisions can be made, 2) when resources become available,
78 * 3) when things are to be considered done, and 4) when to send
79 * things back. Through these simple principles, the model delivers
80 * high performance, and lots of flexibility, allowing users to
81 * evaluate the system impact of a wide range of memory technologies,
82 * such as DDR3/4, LPDDR2/3/4, WideIO1/2, HBM and HMC.
84 * For more details, please see Hansson et al, "Simulating DRAM
85 * controllers for future system architecture exploration",
86 * Proc. ISPASS, 2014. If you use this model as part of your research
87 * please cite the paper.
89 class DRAMCtrl : public AbstractMemory
94 // For now, make use of a queued slave port to avoid dealing with
95 // flow control for the responses being sent back
96 class MemoryPort : public QueuedSlavePort
99 RespPacketQueue queue;
104 MemoryPort(const std::string& name, DRAMCtrl& _memory);
108 Tick recvAtomic(PacketPtr pkt);
110 void recvFunctional(PacketPtr pkt);
112 bool recvTimingReq(PacketPtr);
114 virtual AddrRangeList getAddrRanges() const;
119 * Our incoming port, for a multi-ported controller add a crossbar
125 * Remeber if the memory system is in timing mode
130 * Remember if we have to retry a request when available.
136 * Bus state used to control the read/write switching and drive
137 * the scheduling of the next request.
149 * A basic class to track the bank state, i.e. what row is
150 * currently open (if any), when is the bank free to accept a new
151 * column (read/write) command, when can it be precharged, and
152 * when can it be activated.
154 * The bank also keeps track of how many bytes have been accessed
155 * in the open row since it was opened.
162 static const uint32_t NO_ROW = -1;
172 uint32_t rowAccesses;
173 uint32_t bytesAccessed;
176 openRow(NO_ROW), bank(0), bankgr(0),
177 colAllowedAt(0), preAllowedAt(0), actAllowedAt(0),
178 rowAccesses(0), bytesAccessed(0)
184 * Rank class includes a vector of banks. Refresh and Power state
185 * machines are defined per rank. Events required to change the
186 * state of the refresh and power state machine are scheduled per
187 * rank. This class allows the implementation of rank-wise refresh
188 * and rank-wise power-down.
190 class Rank : public EventManager
196 * The power state captures the different operational states of
197 * the DRAM and interacts with the bus read/write state machine,
198 * and the refresh state machine. In the idle state all banks are
199 * precharged. From there we either go to an auto refresh (as
200 * determined by the refresh state machine), or to a precharge
201 * power down mode. From idle the memory can also go to the active
202 * state (with one or more banks active), and in turn from there
203 * to active power down. At the moment we do not capture the deep
204 * power down and self-refresh state.
215 * The refresh state is used to control the progress of the
216 * refresh scheduling. When normal operation is in progress the
217 * refresh state is idle. From there, it progresses to the refresh
218 * drain state once tREFI has passed. The refresh drain state
219 * captures the DRAM row active state, as it will stay there until
220 * all ongoing accesses complete. Thereafter all banks are
221 * precharged, and lastly, the DRAM is refreshed.
231 * A reference to the parent DRAMCtrl instance
236 * Since we are taking decisions out of order, we need to keep
237 * track of what power transition is happening at what time, such
238 * that we can go back in time and change history. For example, if
239 * we precharge all banks and schedule going to the idle state, we
240 * might at a later point decide to activate a bank before the
241 * transition to idle would have taken place.
243 PowerState pwrStateTrans;
246 * Current power state.
251 * Track when we transitioned to the current power state
256 * current refresh state
258 RefreshState refreshState;
261 * Keep track of when a refresh is due.
268 Stats::Scalar actEnergy;
269 Stats::Scalar preEnergy;
270 Stats::Scalar readEnergy;
271 Stats::Scalar writeEnergy;
272 Stats::Scalar refreshEnergy;
275 * Active Background Energy
277 Stats::Scalar actBackEnergy;
280 * Precharge Background Energy
282 Stats::Scalar preBackEnergy;
284 Stats::Scalar totalEnergy;
285 Stats::Scalar averagePower;
288 * Track time spent in each power state.
290 Stats::Vector pwrStateTime;
293 * Function to update Power Stats
295 void updatePowerStats();
298 * Schedule a power state transition in the future, and
299 * potentially override an already scheduled transition.
301 * @param pwr_state Power state to transition to
302 * @param tick Tick when transition should take place
304 void schedulePowerEvent(PowerState pwr_state, Tick tick);
314 * One DRAMPower instance per rank
319 * Vector of Banks. Each rank is made of several devices which in
320 * term are made from several banks.
322 std::vector<Bank> banks;
325 * To track number of banks which are currently active for
328 unsigned int numBanksActive;
330 /** List to keep track of activate ticks */
331 std::deque<Tick> actTicks;
333 Rank(DRAMCtrl& _memory, const DRAMCtrlParams* _p);
335 const std::string name() const
337 return csprintf("%s_%d", memory.name(), rank);
341 * Kick off accounting for power and refresh states and
342 * schedule initial refresh.
344 * @param ref_tick Tick for first refresh
346 void startup(Tick ref_tick);
349 * Stop the refresh events.
354 * Check if the current rank is available for scheduling.
356 * @param Return true if the rank is idle from a refresh point of view
358 bool isAvailable() const { return refreshState == REF_IDLE; }
361 * Let the rank check if it was waiting for requests to drain
362 * to allow it to transition states.
364 void checkDrainDone();
367 * Function to register Stats
371 void processActivateEvent();
372 EventWrapper<Rank, &Rank::processActivateEvent>
375 void processPrechargeEvent();
376 EventWrapper<Rank, &Rank::processPrechargeEvent>
379 void processRefreshEvent();
380 EventWrapper<Rank, &Rank::processRefreshEvent>
383 void processPowerEvent();
384 EventWrapper<Rank, &Rank::processPowerEvent>
390 * A burst helper helps organize and manage a packet that is larger than
391 * the DRAM burst size. A system packet that is larger than the burst size
392 * is split into multiple DRAM packets and all those DRAM packets point to
393 * a single burst helper such that we know when the whole packet is served.
399 /** Number of DRAM bursts requred for a system packet **/
400 const unsigned int burstCount;
402 /** Number of DRAM bursts serviced so far for a system packet **/
403 unsigned int burstsServiced;
405 BurstHelper(unsigned int _burstCount)
406 : burstCount(_burstCount), burstsServiced(0)
411 * A DRAM packet stores packets along with the timestamp of when
412 * the packet entered the queue, and also the decoded address.
418 /** When did request enter the controller */
419 const Tick entryTime;
421 /** When will request leave the controller */
424 /** This comes from the outside world */
429 /** Will be populated by address decoder */
435 * Bank id is calculated considering banks in all the ranks
436 * eg: 2 ranks each with 8 banks, then bankId = 0 --> rank0, bank0 and
437 * bankId = 8 --> rank1, bank0
439 const uint16_t bankId;
442 * The starting address of the DRAM packet.
443 * This address could be unaligned to burst size boundaries. The
444 * reason is to keep the address offset so we can accurately check
445 * incoming read packets with packets in the write queue.
450 * The size of this dram packet in bytes
451 * It is always equal or smaller than DRAM burst size
456 * A pointer to the BurstHelper if this DRAMPacket is a split packet
457 * If not a split packet (common case), this is set to NULL
459 BurstHelper* burstHelper;
463 DRAMPacket(PacketPtr _pkt, bool is_read, uint8_t _rank, uint8_t _bank,
464 uint32_t _row, uint16_t bank_id, Addr _addr,
465 unsigned int _size, Bank& bank_ref, Rank& rank_ref)
466 : entryTime(curTick()), readyTime(curTick()),
467 pkt(_pkt), isRead(is_read), rank(_rank), bank(_bank), row(_row),
468 bankId(bank_id), addr(_addr), size(_size), burstHelper(NULL),
469 bankRef(bank_ref), rankRef(rank_ref)
475 * Bunch of things requires to setup "events" in gem5
476 * When event "respondEvent" occurs for example, the method
477 * processRespondEvent is called; no parameters are allowed
480 void processNextReqEvent();
481 EventWrapper<DRAMCtrl,&DRAMCtrl::processNextReqEvent> nextReqEvent;
483 void processRespondEvent();
484 EventWrapper<DRAMCtrl, &DRAMCtrl::processRespondEvent> respondEvent;
487 * Check if the read queue has room for more entries
489 * @param pktCount The number of entries needed in the read queue
490 * @return true if read queue is full, false otherwise
492 bool readQueueFull(unsigned int pktCount) const;
495 * Check if the write queue has room for more entries
497 * @param pktCount The number of entries needed in the write queue
498 * @return true if write queue is full, false otherwise
500 bool writeQueueFull(unsigned int pktCount) const;
503 * When a new read comes in, first check if the write q has a
504 * pending request to the same address.\ If not, decode the
505 * address to populate rank/bank/row, create one or mutliple
506 * "dram_pkt", and push them to the back of the read queue.\
507 * If this is the only
508 * read request in the system, schedule an event to start
511 * @param pkt The request packet from the outside world
512 * @param pktCount The number of DRAM bursts the pkt
513 * translate to. If pkt size is larger then one full burst,
514 * then pktCount is greater than one.
516 void addToReadQueue(PacketPtr pkt, unsigned int pktCount);
519 * Decode the incoming pkt, create a dram_pkt and push to the
520 * back of the write queue. \If the write q length is more than
521 * the threshold specified by the user, ie the queue is beginning
522 * to get full, stop reads, and start draining writes.
524 * @param pkt The request packet from the outside world
525 * @param pktCount The number of DRAM bursts the pkt
526 * translate to. If pkt size is larger then one full burst,
527 * then pktCount is greater than one.
529 void addToWriteQueue(PacketPtr pkt, unsigned int pktCount);
532 * Actually do the DRAM access - figure out the latency it
533 * will take to service the req based on bank state, channel state etc
534 * and then update those states to account for this request.\ Based
535 * on this, update the packet's "readyTime" and move it to the
536 * response q from where it will eventually go back to the outside
539 * @param pkt The DRAM packet created from the outside world pkt
541 void doDRAMAccess(DRAMPacket* dram_pkt);
544 * When a packet reaches its "readyTime" in the response Q,
545 * use the "access()" method in AbstractMemory to actually
546 * create the response packet, and send it back to the outside
549 * @param pkt The packet from the outside world
550 * @param static_latency Static latency to add before sending the packet
552 void accessAndRespond(PacketPtr pkt, Tick static_latency);
555 * Address decoder to figure out physical mapping onto ranks,
556 * banks, and rows. This function is called multiple times on the same
557 * system packet if the pakcet is larger than burst of the memory. The
558 * dramPktAddr is used for the offset within the packet.
560 * @param pkt The packet from the outside world
561 * @param dramPktAddr The starting address of the DRAM packet
562 * @param size The size of the DRAM packet in bytes
563 * @param isRead Is the request for a read or a write to DRAM
564 * @return A DRAMPacket pointer with the decoded information
566 DRAMPacket* decodeAddr(PacketPtr pkt, Addr dramPktAddr, unsigned int size,
570 * The memory schduler/arbiter - picks which request needs to
571 * go next, based on the specified policy such as FCFS or FR-FCFS
572 * and moves it to the head of the queue.
573 * Prioritizes accesses to the same rank as previous burst unless
574 * controller is switching command type.
576 * @param queue Queued requests to consider
577 * @param extra_col_delay Any extra delay due to a read/write switch
578 * @return true if a packet is scheduled to a rank which is available else
581 bool chooseNext(std::deque<DRAMPacket*>& queue, Tick extra_col_delay);
584 * For FR-FCFS policy reorder the read/write queue depending on row buffer
585 * hits and earliest bursts available in DRAM
587 * @param queue Queued requests to consider
588 * @param extra_col_delay Any extra delay due to a read/write switch
589 * @return true if a packet is scheduled to a rank which is available else
592 bool reorderQueue(std::deque<DRAMPacket*>& queue, Tick extra_col_delay);
595 * Find which are the earliest banks ready to issue an activate
596 * for the enqueued requests. Assumes maximum of 64 banks per DIMM
597 * Also checks if the bank is already prepped.
599 * @param queue Queued requests to consider
600 * @param time of seamless burst command
601 * @return One-hot encoded mask of bank indices
602 * @return boolean indicating burst can issue seamlessly, with no gaps
604 std::pair<uint64_t, bool> minBankPrep(const std::deque<DRAMPacket*>& queue,
605 Tick min_col_at) const;
608 * Keep track of when row activations happen, in order to enforce
609 * the maximum number of activations in the activation window. The
610 * method updates the time that the banks become available based
611 * on the current limits.
613 * @param rank_ref Reference to the rank
614 * @param bank_ref Reference to the bank
615 * @param act_tick Time when the activation takes place
616 * @param row Index of the row
618 void activateBank(Rank& rank_ref, Bank& bank_ref, Tick act_tick,
622 * Precharge a given bank and also update when the precharge is
623 * done. This will also deal with any stats related to the
624 * accesses to the open page.
626 * @param rank_ref The rank to precharge
627 * @param bank_ref The bank to precharge
628 * @param pre_at Time when the precharge takes place
629 * @param trace Is this an auto precharge then do not add to trace
631 void prechargeBank(Rank& rank_ref, Bank& bank_ref,
632 Tick pre_at, bool trace = true);
635 * Used for debugging to observe the contents of the queues.
637 void printQs() const;
640 * Burst-align an address.
642 * @param addr The potentially unaligned address
644 * @return An address aligned to a DRAM burst
646 Addr burstAlign(Addr addr) const { return (addr & ~(Addr(burstSize - 1))); }
649 * The controller's main read and write queues
651 std::deque<DRAMPacket*> readQueue;
652 std::deque<DRAMPacket*> writeQueue;
655 * To avoid iterating over the write queue to check for
656 * overlapping transactions, maintain a set of burst addresses
657 * that are currently queued. Since we merge writes to the same
658 * location we never have more than one address to the same burst
661 std::unordered_set<Addr> isInWriteQueue;
664 * Response queue where read packets wait after we're done working
665 * with them, but it's not time to send the response yet. The
666 * responses are stored seperately mostly to keep the code clean
667 * and help with events scheduling. For all logical purposes such
668 * as sizing the read queue, this and the main read queue need to
671 std::deque<DRAMPacket*> respQueue;
676 std::vector<Rank*> ranks;
679 * The following are basic design parameters of the memory
680 * controller, and are initialized based on parameter values.
681 * The rowsPerBank is determined based on the capacity, number of
682 * ranks and banks, the burst size, and the row buffer size.
684 const uint32_t deviceSize;
685 const uint32_t deviceBusWidth;
686 const uint32_t burstLength;
687 const uint32_t deviceRowBufferSize;
688 const uint32_t devicesPerRank;
689 const uint32_t burstSize;
690 const uint32_t rowBufferSize;
691 const uint32_t columnsPerRowBuffer;
692 const uint32_t columnsPerStripe;
693 const uint32_t ranksPerChannel;
694 const uint32_t bankGroupsPerRank;
695 const bool bankGroupArch;
696 const uint32_t banksPerRank;
697 const uint32_t channels;
698 uint32_t rowsPerBank;
699 const uint32_t readBufferSize;
700 const uint32_t writeBufferSize;
701 const uint32_t writeHighThreshold;
702 const uint32_t writeLowThreshold;
703 const uint32_t minWritesPerSwitch;
704 uint32_t writesThisTime;
705 uint32_t readsThisTime;
708 * Basic memory timing parameters initialized based on parameter
711 const Tick M5_CLASS_VAR_USED tCK;
728 const uint32_t activationLimit;
731 * Memory controller configuration initialized based on parameter
734 Enums::MemSched memSchedPolicy;
735 Enums::AddrMap addrMapping;
736 Enums::PageManage pageMgmt;
739 * Max column accesses (read and write) per row, before forefully
742 const uint32_t maxAccessesPerRow;
745 * Pipeline latency of the controller frontend. The frontend
746 * contribution is added to writes (that complete when they are in
747 * the write buffer) and reads that are serviced the write buffer.
749 const Tick frontendLatency;
752 * Pipeline latency of the backend and PHY. Along with the
753 * frontend contribution, this latency is added to reads serviced
756 const Tick backendLatency;
759 * Till when has the main data bus been spoken for already?
766 * The soonest you have to start thinking about the next request
767 * is the longest access time that can occur before
768 * busBusyUntil. Assuming you need to precharge, open a new row,
769 * and access, it is tRP + tRCD + tCL.
773 // All statistics that the model needs to capture
774 Stats::Scalar readReqs;
775 Stats::Scalar writeReqs;
776 Stats::Scalar readBursts;
777 Stats::Scalar writeBursts;
778 Stats::Scalar bytesReadDRAM;
779 Stats::Scalar bytesReadWrQ;
780 Stats::Scalar bytesWritten;
781 Stats::Scalar bytesReadSys;
782 Stats::Scalar bytesWrittenSys;
783 Stats::Scalar servicedByWrQ;
784 Stats::Scalar mergedWrBursts;
785 Stats::Scalar neitherReadNorWrite;
786 Stats::Vector perBankRdBursts;
787 Stats::Vector perBankWrBursts;
788 Stats::Scalar numRdRetry;
789 Stats::Scalar numWrRetry;
790 Stats::Scalar totGap;
791 Stats::Vector readPktSize;
792 Stats::Vector writePktSize;
793 Stats::Vector rdQLenPdf;
794 Stats::Vector wrQLenPdf;
795 Stats::Histogram bytesPerActivate;
796 Stats::Histogram rdPerTurnAround;
797 Stats::Histogram wrPerTurnAround;
799 // Latencies summed over all requests
800 Stats::Scalar totQLat;
801 Stats::Scalar totMemAccLat;
802 Stats::Scalar totBusLat;
804 // Average latencies per request
805 Stats::Formula avgQLat;
806 Stats::Formula avgBusLat;
807 Stats::Formula avgMemAccLat;
810 Stats::Formula avgRdBW;
811 Stats::Formula avgWrBW;
812 Stats::Formula avgRdBWSys;
813 Stats::Formula avgWrBWSys;
814 Stats::Formula peakBW;
815 Stats::Formula busUtil;
816 Stats::Formula busUtilRead;
817 Stats::Formula busUtilWrite;
819 // Average queue lengths
820 Stats::Average avgRdQLen;
821 Stats::Average avgWrQLen;
823 // Row hit count and rate
824 Stats::Scalar readRowHits;
825 Stats::Scalar writeRowHits;
826 Stats::Formula readRowHitRate;
827 Stats::Formula writeRowHitRate;
828 Stats::Formula avgGap;
830 // DRAM Power Calculation
831 Stats::Formula pageHitRate;
833 // Holds the value of the rank of burst issued
837 uint64_t timeStampOffset;
839 /** @todo this is a temporary workaround until the 4-phase code is
840 * committed. upstream caches needs this packet until true is returned, so
841 * hold onto it for deletion until a subsequent call
843 std::vector<PacketPtr> pendingDelete;
846 * This function increments the energy when called. If stats are
847 * dumped periodically, note accumulated energy values will
848 * appear in the stats (even if the stats are reset). This is a
849 * result of the energy values coming from DRAMPower, and there
850 * is currently no support for resetting the state.
852 * @param rank Currrent rank
854 void updatePowerStats(Rank& rank_ref);
857 * Function for sorting commands in the command list of DRAMPower.
859 * @param a Memory Command in command list of DRAMPower library
860 * @param next Memory Command in command list of DRAMPower
861 * @return true if timestamp of Command 1 < timestamp of Command 2
863 static bool sortTime(const Data::MemCommand& m1,
864 const Data::MemCommand& m2) {
865 return m1.getTime() < m2.getTime();
873 DRAMCtrl(const DRAMCtrlParams* p);
875 DrainState drain() M5_ATTR_OVERRIDE;
877 virtual BaseSlavePort& getSlavePort(const std::string& if_name,
878 PortID idx = InvalidPortID);
880 virtual void init() M5_ATTR_OVERRIDE;
881 virtual void startup() M5_ATTR_OVERRIDE;
882 virtual void drainResume() M5_ATTR_OVERRIDE;
886 Tick recvAtomic(PacketPtr pkt);
887 void recvFunctional(PacketPtr pkt);
888 bool recvTimingReq(PacketPtr pkt);
892 #endif //__MEM_DRAM_CTRL_HH__