dram_ctrl.hh revision 10246
19243SN/A/*
210206Sandreas.hansson@arm.com * Copyright (c) 2012-2014 ARM Limited
39243SN/A * All rights reserved
49243SN/A *
59243SN/A * The license below extends only to copyright in the software and shall
69243SN/A * not be construed as granting a license to any other intellectual
79243SN/A * property including but not limited to intellectual property relating
89243SN/A * to a hardware implementation of the functionality of the software
99243SN/A * licensed hereunder.  You may use the software subject to the license
109243SN/A * terms below provided that you ensure that this notice is replicated
119243SN/A * unmodified and in its entirety in all distributions of the software,
129243SN/A * modified or unmodified, in source code or in binary form.
139243SN/A *
149831SN/A * Copyright (c) 2013 Amin Farmahini-Farahani
159831SN/A * All rights reserved.
169831SN/A *
179243SN/A * Redistribution and use in source and binary forms, with or without
189243SN/A * modification, are permitted provided that the following conditions are
199243SN/A * met: redistributions of source code must retain the above copyright
209243SN/A * notice, this list of conditions and the following disclaimer;
219243SN/A * redistributions in binary form must reproduce the above copyright
229243SN/A * notice, this list of conditions and the following disclaimer in the
239243SN/A * documentation and/or other materials provided with the distribution;
249243SN/A * neither the name of the copyright holders nor the names of its
259243SN/A * contributors may be used to endorse or promote products derived from
269243SN/A * this software without specific prior written permission.
279243SN/A *
289243SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
299243SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
309243SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
319243SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
329243SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
339243SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
349243SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
359243SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
369243SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
379243SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
389243SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
399243SN/A *
409243SN/A * Authors: Andreas Hansson
419243SN/A *          Ani Udipi
429967SN/A *          Neha Agarwal
439243SN/A */
449243SN/A
459243SN/A/**
469243SN/A * @file
4710146Sandreas.hansson@arm.com * DRAMCtrl declaration
489243SN/A */
499243SN/A
5010146Sandreas.hansson@arm.com#ifndef __MEM_DRAM_CTRL_HH__
5110146Sandreas.hansson@arm.com#define __MEM_DRAM_CTRL_HH__
529243SN/A
539488SN/A#include <deque>
549488SN/A
559243SN/A#include "base/statistics.hh"
569243SN/A#include "enums/AddrMap.hh"
579243SN/A#include "enums/MemSched.hh"
589243SN/A#include "enums/PageManage.hh"
599243SN/A#include "mem/abstract_mem.hh"
609243SN/A#include "mem/qport.hh"
6110146Sandreas.hansson@arm.com#include "params/DRAMCtrl.hh"
629243SN/A#include "sim/eventq.hh"
639243SN/A
649243SN/A/**
6510146Sandreas.hansson@arm.com * The DRAM controller is a basic single-channel memory controller
6610146Sandreas.hansson@arm.com * aiming to mimic a high-level DRAM controller and the most important
6710146Sandreas.hansson@arm.com * timing constraints associated with the DRAM. The focus is really on
689243SN/A * modelling the impact on the system rather than the DRAM itself,
699243SN/A * hence the focus is on the controller model and not on the
709243SN/A * memory. By adhering to the correct timing constraints, ultimately
719243SN/A * there is no need for a memory model in addition to the controller
729243SN/A * model.
739243SN/A *
749243SN/A * As a basic design principle, this controller is not cycle callable,
759243SN/A * but instead uses events to decide when new decisions can be made,
769243SN/A * when resources become available, when things are to be considered
779243SN/A * done, and when to send things back. Through these simple
789243SN/A * principles, we achieve a performant model that is not
799243SN/A * cycle-accurate, but enables us to evaluate the system impact of a
809243SN/A * wide range of memory technologies, and also collect statistics
819243SN/A * about the use of the memory.
829243SN/A */
8310146Sandreas.hansson@arm.comclass DRAMCtrl : public AbstractMemory
849243SN/A{
859243SN/A
869243SN/A  private:
879243SN/A
889243SN/A    // For now, make use of a queued slave port to avoid dealing with
899243SN/A    // flow control for the responses being sent back
909243SN/A    class MemoryPort : public QueuedSlavePort
919243SN/A    {
929243SN/A
939243SN/A        SlavePacketQueue queue;
9410146Sandreas.hansson@arm.com        DRAMCtrl& memory;
959243SN/A
969243SN/A      public:
979243SN/A
9810146Sandreas.hansson@arm.com        MemoryPort(const std::string& name, DRAMCtrl& _memory);
999243SN/A
1009243SN/A      protected:
1019243SN/A
1029243SN/A        Tick recvAtomic(PacketPtr pkt);
1039243SN/A
1049243SN/A        void recvFunctional(PacketPtr pkt);
1059243SN/A
1069243SN/A        bool recvTimingReq(PacketPtr);
1079243SN/A
1089243SN/A        virtual AddrRangeList getAddrRanges() const;
1099243SN/A
1109243SN/A    };
1119243SN/A
1129243SN/A    /**
1139243SN/A     * Our incoming port, for a multi-ported controller add a crossbar
1149243SN/A     * in front of it
1159243SN/A     */
1169243SN/A    MemoryPort port;
1179243SN/A
1189243SN/A    /**
1199243SN/A     * Remember if we have to retry a request when available.
1209243SN/A     */
1219243SN/A    bool retryRdReq;
1229243SN/A    bool retryWrReq;
1239243SN/A
1249243SN/A    /**
12510206Sandreas.hansson@arm.com     * Bus state used to control the read/write switching and drive
12610206Sandreas.hansson@arm.com     * the scheduling of the next request.
1279243SN/A     */
12810206Sandreas.hansson@arm.com    enum BusState {
12910206Sandreas.hansson@arm.com        READ = 0,
13010206Sandreas.hansson@arm.com        READ_TO_WRITE,
13110206Sandreas.hansson@arm.com        WRITE,
13210206Sandreas.hansson@arm.com        WRITE_TO_READ
13310206Sandreas.hansson@arm.com    };
13410206Sandreas.hansson@arm.com
13510206Sandreas.hansson@arm.com    BusState busState;
1369243SN/A
1379488SN/A    /** List to keep track of activate ticks */
1389969SN/A    std::vector<std::deque<Tick>> actTicks;
1399488SN/A
1409243SN/A    /**
14110210Sandreas.hansson@arm.com     * A basic class to track the bank state, i.e. what row is
14210210Sandreas.hansson@arm.com     * currently open (if any), when is the bank free to accept a new
14310211Sandreas.hansson@arm.com     * column (read/write) command, when can it be precharged, and
14410211Sandreas.hansson@arm.com     * when can it be activated.
14510210Sandreas.hansson@arm.com     *
14610210Sandreas.hansson@arm.com     * The bank also keeps track of how many bytes have been accessed
14710210Sandreas.hansson@arm.com     * in the open row since it was opened.
1489243SN/A     */
1499243SN/A    class Bank
1509243SN/A    {
1519243SN/A
1529243SN/A      public:
1539243SN/A
15410207Sandreas.hansson@arm.com        static const uint32_t NO_ROW = -1;
1559243SN/A
1569243SN/A        uint32_t openRow;
15710246Sandreas.hansson@arm.com        uint8_t rank;
15810246Sandreas.hansson@arm.com        uint8_t bank;
1599243SN/A
16010211Sandreas.hansson@arm.com        Tick colAllowedAt;
16110210Sandreas.hansson@arm.com        Tick preAllowedAt;
1629969SN/A        Tick actAllowedAt;
1639243SN/A
16410141SN/A        uint32_t rowAccesses;
1659727SN/A        uint32_t bytesAccessed;
1669727SN/A
1679727SN/A        Bank() :
16810246Sandreas.hansson@arm.com            openRow(NO_ROW), rank(0), bank(0),
16910246Sandreas.hansson@arm.com            colAllowedAt(0), preAllowedAt(0), actAllowedAt(0),
17010141SN/A            rowAccesses(0), bytesAccessed(0)
1719243SN/A        { }
1729243SN/A    };
1739243SN/A
1749243SN/A    /**
1759831SN/A     * A burst helper helps organize and manage a packet that is larger than
1769831SN/A     * the DRAM burst size. A system packet that is larger than the burst size
1779831SN/A     * is split into multiple DRAM packets and all those DRAM packets point to
1789831SN/A     * a single burst helper such that we know when the whole packet is served.
1799831SN/A     */
1809831SN/A    class BurstHelper {
1819831SN/A
1829831SN/A      public:
1839831SN/A
1849831SN/A        /** Number of DRAM bursts requred for a system packet **/
1859831SN/A        const unsigned int burstCount;
1869831SN/A
1879831SN/A        /** Number of DRAM bursts serviced so far for a system packet **/
1889831SN/A        unsigned int burstsServiced;
1899831SN/A
1909831SN/A        BurstHelper(unsigned int _burstCount)
1919831SN/A            : burstCount(_burstCount), burstsServiced(0)
1929831SN/A            { }
1939831SN/A    };
1949831SN/A
1959831SN/A    /**
1969243SN/A     * A DRAM packet stores packets along with the timestamp of when
1979243SN/A     * the packet entered the queue, and also the decoded address.
1989243SN/A     */
1999243SN/A    class DRAMPacket {
2009243SN/A
2019243SN/A      public:
2029243SN/A
2039243SN/A        /** When did request enter the controller */
2049243SN/A        const Tick entryTime;
2059243SN/A
2069243SN/A        /** When will request leave the controller */
2079243SN/A        Tick readyTime;
2089243SN/A
2099243SN/A        /** This comes from the outside world */
2109243SN/A        const PacketPtr pkt;
2119243SN/A
2129966SN/A        const bool isRead;
2139966SN/A
2149243SN/A        /** Will be populated by address decoder */
2159243SN/A        const uint8_t rank;
2169967SN/A        const uint8_t bank;
21710245Sandreas.hansson@arm.com        const uint32_t row;
2189831SN/A
2199831SN/A        /**
2209967SN/A         * Bank id is calculated considering banks in all the ranks
2219967SN/A         * eg: 2 ranks each with 8 banks, then bankId = 0 --> rank0, bank0 and
2229967SN/A         * bankId = 8 --> rank1, bank0
2239967SN/A         */
2249967SN/A        const uint16_t bankId;
2259967SN/A
2269967SN/A        /**
2279831SN/A         * The starting address of the DRAM packet.
2289831SN/A         * This address could be unaligned to burst size boundaries. The
2299831SN/A         * reason is to keep the address offset so we can accurately check
2309831SN/A         * incoming read packets with packets in the write queue.
2319831SN/A         */
2329832SN/A        Addr addr;
2339831SN/A
2349831SN/A        /**
2359831SN/A         * The size of this dram packet in bytes
2369831SN/A         * It is always equal or smaller than DRAM burst size
2379831SN/A         */
2389832SN/A        unsigned int size;
2399831SN/A
2409831SN/A        /**
2419831SN/A         * A pointer to the BurstHelper if this DRAMPacket is a split packet
2429831SN/A         * If not a split packet (common case), this is set to NULL
2439831SN/A         */
2449831SN/A        BurstHelper* burstHelper;
2459967SN/A        Bank& bankRef;
2469243SN/A
2479967SN/A        DRAMPacket(PacketPtr _pkt, bool is_read, uint8_t _rank, uint8_t _bank,
24810245Sandreas.hansson@arm.com                   uint32_t _row, uint16_t bank_id, Addr _addr,
2499967SN/A                   unsigned int _size, Bank& bank_ref)
2509243SN/A            : entryTime(curTick()), readyTime(curTick()),
2519967SN/A              pkt(_pkt), isRead(is_read), rank(_rank), bank(_bank), row(_row),
2529967SN/A              bankId(bank_id), addr(_addr), size(_size), burstHelper(NULL),
2539967SN/A              bankRef(bank_ref)
2549243SN/A        { }
2559243SN/A
2569243SN/A    };
2579243SN/A
2589243SN/A    /**
2599243SN/A     * Bunch of things requires to setup "events" in gem5
26010206Sandreas.hansson@arm.com     * When event "respondEvent" occurs for example, the method
26110206Sandreas.hansson@arm.com     * processRespondEvent is called; no parameters are allowed
2629243SN/A     * in these methods
2639243SN/A     */
26410208Sandreas.hansson@arm.com    void processNextReqEvent();
26510208Sandreas.hansson@arm.com    EventWrapper<DRAMCtrl,&DRAMCtrl::processNextReqEvent> nextReqEvent;
26610208Sandreas.hansson@arm.com
2679243SN/A    void processRespondEvent();
26810146Sandreas.hansson@arm.com    EventWrapper<DRAMCtrl, &DRAMCtrl::processRespondEvent> respondEvent;
2699243SN/A
27010208Sandreas.hansson@arm.com    void processActivateEvent();
27110208Sandreas.hansson@arm.com    EventWrapper<DRAMCtrl, &DRAMCtrl::processActivateEvent> activateEvent;
27210208Sandreas.hansson@arm.com
27310208Sandreas.hansson@arm.com    void processPrechargeEvent();
27410208Sandreas.hansson@arm.com    EventWrapper<DRAMCtrl, &DRAMCtrl::processPrechargeEvent> prechargeEvent;
27510208Sandreas.hansson@arm.com
2769243SN/A    void processRefreshEvent();
27710146Sandreas.hansson@arm.com    EventWrapper<DRAMCtrl, &DRAMCtrl::processRefreshEvent> refreshEvent;
2789243SN/A
27910208Sandreas.hansson@arm.com    void processPowerEvent();
28010208Sandreas.hansson@arm.com    EventWrapper<DRAMCtrl,&DRAMCtrl::processPowerEvent> powerEvent;
2819243SN/A
2829243SN/A    /**
2839243SN/A     * Check if the read queue has room for more entries
2849243SN/A     *
2859831SN/A     * @param pktCount The number of entries needed in the read queue
2869243SN/A     * @return true if read queue is full, false otherwise
2879243SN/A     */
2889831SN/A    bool readQueueFull(unsigned int pktCount) const;
2899243SN/A
2909243SN/A    /**
2919243SN/A     * Check if the write queue has room for more entries
2929243SN/A     *
2939831SN/A     * @param pktCount The number of entries needed in the write queue
2949243SN/A     * @return true if write queue is full, false otherwise
2959243SN/A     */
2969831SN/A    bool writeQueueFull(unsigned int pktCount) const;
2979243SN/A
2989243SN/A    /**
2999243SN/A     * When a new read comes in, first check if the write q has a
3009243SN/A     * pending request to the same address.\ If not, decode the
3019831SN/A     * address to populate rank/bank/row, create one or mutliple
3029831SN/A     * "dram_pkt", and push them to the back of the read queue.\
3039831SN/A     * If this is the only
3049243SN/A     * read request in the system, schedule an event to start
3059243SN/A     * servicing it.
3069243SN/A     *
3079243SN/A     * @param pkt The request packet from the outside world
3089831SN/A     * @param pktCount The number of DRAM bursts the pkt
3099831SN/A     * translate to. If pkt size is larger then one full burst,
3109831SN/A     * then pktCount is greater than one.
3119243SN/A     */
3129831SN/A    void addToReadQueue(PacketPtr pkt, unsigned int pktCount);
3139243SN/A
3149243SN/A    /**
3159243SN/A     * Decode the incoming pkt, create a dram_pkt and push to the
3169243SN/A     * back of the write queue. \If the write q length is more than
3179243SN/A     * the threshold specified by the user, ie the queue is beginning
3189243SN/A     * to get full, stop reads, and start draining writes.
3199243SN/A     *
3209243SN/A     * @param pkt The request packet from the outside world
3219831SN/A     * @param pktCount The number of DRAM bursts the pkt
3229831SN/A     * translate to. If pkt size is larger then one full burst,
3239831SN/A     * then pktCount is greater than one.
3249243SN/A     */
3259831SN/A    void addToWriteQueue(PacketPtr pkt, unsigned int pktCount);
3269243SN/A
3279243SN/A    /**
3289243SN/A     * Actually do the DRAM access - figure out the latency it
3299243SN/A     * will take to service the req based on bank state, channel state etc
3309243SN/A     * and then update those states to account for this request.\ Based
3319243SN/A     * on this, update the packet's "readyTime" and move it to the
3329243SN/A     * response q from where it will eventually go back to the outside
3339243SN/A     * world.
3349243SN/A     *
3359243SN/A     * @param pkt The DRAM packet created from the outside world pkt
3369243SN/A     */
3379243SN/A    void doDRAMAccess(DRAMPacket* dram_pkt);
3389243SN/A
3399243SN/A    /**
3409243SN/A     * When a packet reaches its "readyTime" in the response Q,
3419243SN/A     * use the "access()" method in AbstractMemory to actually
3429243SN/A     * create the response packet, and send it back to the outside
3439243SN/A     * world requestor.
3449243SN/A     *
3459243SN/A     * @param pkt The packet from the outside world
3469726SN/A     * @param static_latency Static latency to add before sending the packet
3479243SN/A     */
3489726SN/A    void accessAndRespond(PacketPtr pkt, Tick static_latency);
3499243SN/A
3509243SN/A    /**
3519243SN/A     * Address decoder to figure out physical mapping onto ranks,
3529831SN/A     * banks, and rows. This function is called multiple times on the same
3539831SN/A     * system packet if the pakcet is larger than burst of the memory. The
3549831SN/A     * dramPktAddr is used for the offset within the packet.
3559243SN/A     *
3569243SN/A     * @param pkt The packet from the outside world
3579831SN/A     * @param dramPktAddr The starting address of the DRAM packet
3589831SN/A     * @param size The size of the DRAM packet in bytes
3599966SN/A     * @param isRead Is the request for a read or a write to DRAM
3609243SN/A     * @return A DRAMPacket pointer with the decoded information
3619243SN/A     */
36210143SN/A    DRAMPacket* decodeAddr(PacketPtr pkt, Addr dramPktAddr, unsigned int size,
36310143SN/A                           bool isRead);
3649243SN/A
3659243SN/A    /**
36610206Sandreas.hansson@arm.com     * The memory schduler/arbiter - picks which request needs to
3679567SN/A     * go next, based on the specified policy such as FCFS or FR-FCFS
36810206Sandreas.hansson@arm.com     * and moves it to the head of the queue.
3699243SN/A     */
37010206Sandreas.hansson@arm.com    void chooseNext(std::deque<DRAMPacket*>& queue);
3719243SN/A
3729243SN/A    /**
3739974SN/A     * For FR-FCFS policy reorder the read/write queue depending on row buffer
3749974SN/A     * hits and earliest banks available in DRAM
3759974SN/A     */
3769974SN/A    void reorderQueue(std::deque<DRAMPacket*>& queue);
3779974SN/A
3789974SN/A    /**
37910211Sandreas.hansson@arm.com     * Find which are the earliest banks ready to issue an activate
38010211Sandreas.hansson@arm.com     * for the enqueued requests. Assumes maximum of 64 banks per DIMM
3819967SN/A     *
3829967SN/A     * @param Queued requests to consider
3839967SN/A     * @return One-hot encoded mask of bank indices
3849967SN/A     */
38510211Sandreas.hansson@arm.com    uint64_t minBankActAt(const std::deque<DRAMPacket*>& queue) const;
3869488SN/A
3879488SN/A    /**
3889488SN/A     * Keep track of when row activations happen, in order to enforce
3899488SN/A     * the maximum number of activations in the activation window. The
3909488SN/A     * method updates the time that the banks become available based
3919488SN/A     * on the current limits.
39210210Sandreas.hansson@arm.com     *
39310246Sandreas.hansson@arm.com     * @param bank Reference to the bank
39410210Sandreas.hansson@arm.com     * @param act_tick Time when the activation takes place
39510210Sandreas.hansson@arm.com     * @param row Index of the row
3969488SN/A     */
39710246Sandreas.hansson@arm.com    void activateBank(Bank& bank, Tick act_tick, uint32_t row);
39810207Sandreas.hansson@arm.com
39910207Sandreas.hansson@arm.com    /**
40010207Sandreas.hansson@arm.com     * Precharge a given bank and also update when the precharge is
40110207Sandreas.hansson@arm.com     * done. This will also deal with any stats related to the
40210207Sandreas.hansson@arm.com     * accesses to the open page.
40310207Sandreas.hansson@arm.com     *
40410207Sandreas.hansson@arm.com     * @param bank The bank to precharge
40510211Sandreas.hansson@arm.com     * @param pre_at Time when the precharge takes place
40610207Sandreas.hansson@arm.com     */
40710211Sandreas.hansson@arm.com    void prechargeBank(Bank& bank, Tick pre_at);
4089488SN/A
40910143SN/A    /**
41010143SN/A     * Used for debugging to observe the contents of the queues.
41110143SN/A     */
4129243SN/A    void printQs() const;
4139243SN/A
4149243SN/A    /**
4159243SN/A     * The controller's main read and write queues
4169243SN/A     */
4179833SN/A    std::deque<DRAMPacket*> readQueue;
4189833SN/A    std::deque<DRAMPacket*> writeQueue;
4199243SN/A
4209243SN/A    /**
4219243SN/A     * Response queue where read packets wait after we're done working
4229567SN/A     * with them, but it's not time to send the response yet. The
4239567SN/A     * responses are stored seperately mostly to keep the code clean
4249567SN/A     * and help with events scheduling. For all logical purposes such
4259567SN/A     * as sizing the read queue, this and the main read queue need to
4269567SN/A     * be added together.
4279243SN/A     */
4289833SN/A    std::deque<DRAMPacket*> respQueue;
4299243SN/A
4309567SN/A    /**
4319567SN/A     * If we need to drain, keep the drain manager around until we're
4329567SN/A     * done here.
4339243SN/A     */
4349342SN/A    DrainManager *drainManager;
4359243SN/A
4369243SN/A    /**
4379243SN/A     * Multi-dimensional vector of banks, first dimension is ranks,
4389243SN/A     * second is bank
4399243SN/A     */
4409243SN/A    std::vector<std::vector<Bank> > banks;
4419243SN/A
4429243SN/A    /**
4439243SN/A     * The following are basic design parameters of the memory
4449831SN/A     * controller, and are initialized based on parameter values.
4459831SN/A     * The rowsPerBank is determined based on the capacity, number of
4469831SN/A     * ranks and banks, the burst size, and the row buffer size.
4479243SN/A     */
4489831SN/A    const uint32_t deviceBusWidth;
4499831SN/A    const uint32_t burstLength;
4509831SN/A    const uint32_t deviceRowBufferSize;
4519831SN/A    const uint32_t devicesPerRank;
4529831SN/A    const uint32_t burstSize;
4539831SN/A    const uint32_t rowBufferSize;
45410140SN/A    const uint32_t columnsPerRowBuffer;
4559243SN/A    const uint32_t ranksPerChannel;
4569243SN/A    const uint32_t banksPerRank;
4579566SN/A    const uint32_t channels;
4589243SN/A    uint32_t rowsPerBank;
4599243SN/A    const uint32_t readBufferSize;
4609243SN/A    const uint32_t writeBufferSize;
46110140SN/A    const uint32_t writeHighThreshold;
46210140SN/A    const uint32_t writeLowThreshold;
46310140SN/A    const uint32_t minWritesPerSwitch;
46410140SN/A    uint32_t writesThisTime;
46510147Sandreas.hansson@arm.com    uint32_t readsThisTime;
4669243SN/A
4679243SN/A    /**
4689243SN/A     * Basic memory timing parameters initialized based on parameter
4699243SN/A     * values.
4709243SN/A     */
47110216Sandreas.hansson@arm.com    const Tick tCK;
4729243SN/A    const Tick tWTR;
47310206Sandreas.hansson@arm.com    const Tick tRTW;
4749243SN/A    const Tick tBURST;
4759243SN/A    const Tick tRCD;
4769243SN/A    const Tick tCL;
4779243SN/A    const Tick tRP;
4789963SN/A    const Tick tRAS;
47910210Sandreas.hansson@arm.com    const Tick tWR;
48010212Sandreas.hansson@arm.com    const Tick tRTP;
4819243SN/A    const Tick tRFC;
4829243SN/A    const Tick tREFI;
4839971SN/A    const Tick tRRD;
4849488SN/A    const Tick tXAW;
4859488SN/A    const uint32_t activationLimit;
4869243SN/A
4879243SN/A    /**
4889243SN/A     * Memory controller configuration initialized based on parameter
4899243SN/A     * values.
4909243SN/A     */
4919243SN/A    Enums::MemSched memSchedPolicy;
4929243SN/A    Enums::AddrMap addrMapping;
4939243SN/A    Enums::PageManage pageMgmt;
4949243SN/A
4959243SN/A    /**
49610141SN/A     * Max column accesses (read and write) per row, before forefully
49710141SN/A     * closing it.
49810141SN/A     */
49910141SN/A    const uint32_t maxAccessesPerRow;
50010141SN/A
50110141SN/A    /**
5029726SN/A     * Pipeline latency of the controller frontend. The frontend
5039726SN/A     * contribution is added to writes (that complete when they are in
5049726SN/A     * the write buffer) and reads that are serviced the write buffer.
5059726SN/A     */
5069726SN/A    const Tick frontendLatency;
5079726SN/A
5089726SN/A    /**
5099726SN/A     * Pipeline latency of the backend and PHY. Along with the
5109726SN/A     * frontend contribution, this latency is added to reads serviced
5119726SN/A     * by the DRAM.
5129726SN/A     */
5139726SN/A    const Tick backendLatency;
5149726SN/A
5159726SN/A    /**
5169243SN/A     * Till when has the main data bus been spoken for already?
5179243SN/A     */
5189243SN/A    Tick busBusyUntil;
5199243SN/A
52010207Sandreas.hansson@arm.com    /**
52110207Sandreas.hansson@arm.com     * Keep track of when a refresh is due.
52210207Sandreas.hansson@arm.com     */
52310207Sandreas.hansson@arm.com    Tick refreshDueAt;
52410207Sandreas.hansson@arm.com
52510207Sandreas.hansson@arm.com    /**
52610207Sandreas.hansson@arm.com     * The refresh state is used to control the progress of the
52710207Sandreas.hansson@arm.com     * refresh scheduling. When normal operation is in progress the
52810207Sandreas.hansson@arm.com     * refresh state is idle. From there, it progresses to the refresh
52910207Sandreas.hansson@arm.com     * drain state once tREFI has passed. The refresh drain state
53010207Sandreas.hansson@arm.com     * captures the DRAM row active state, as it will stay there until
53110207Sandreas.hansson@arm.com     * all ongoing accesses complete. Thereafter all banks are
53210207Sandreas.hansson@arm.com     * precharged, and lastly, the DRAM is refreshed.
53310207Sandreas.hansson@arm.com     */
53410207Sandreas.hansson@arm.com    enum RefreshState {
53510207Sandreas.hansson@arm.com        REF_IDLE = 0,
53610207Sandreas.hansson@arm.com        REF_DRAIN,
53710207Sandreas.hansson@arm.com        REF_PRE,
53810207Sandreas.hansson@arm.com        REF_RUN
53910207Sandreas.hansson@arm.com    };
54010207Sandreas.hansson@arm.com
54110207Sandreas.hansson@arm.com    RefreshState refreshState;
54210207Sandreas.hansson@arm.com
54310208Sandreas.hansson@arm.com    /**
54410208Sandreas.hansson@arm.com     * The power state captures the different operational states of
54510208Sandreas.hansson@arm.com     * the DRAM and interacts with the bus read/write state machine,
54610208Sandreas.hansson@arm.com     * and the refresh state machine. In the idle state all banks are
54710208Sandreas.hansson@arm.com     * precharged. From there we either go to an auto refresh (as
54810208Sandreas.hansson@arm.com     * determined by the refresh state machine), or to a precharge
54910208Sandreas.hansson@arm.com     * power down mode. From idle the memory can also go to the active
55010208Sandreas.hansson@arm.com     * state (with one or more banks active), and in turn from there
55110208Sandreas.hansson@arm.com     * to active power down. At the moment we do not capture the deep
55210208Sandreas.hansson@arm.com     * power down and self-refresh state.
55310208Sandreas.hansson@arm.com     */
55410208Sandreas.hansson@arm.com    enum PowerState {
55510208Sandreas.hansson@arm.com        PWR_IDLE = 0,
55610208Sandreas.hansson@arm.com        PWR_REF,
55710208Sandreas.hansson@arm.com        PWR_PRE_PDN,
55810208Sandreas.hansson@arm.com        PWR_ACT,
55910208Sandreas.hansson@arm.com        PWR_ACT_PDN
56010208Sandreas.hansson@arm.com    };
56110208Sandreas.hansson@arm.com
56210208Sandreas.hansson@arm.com    /**
56310208Sandreas.hansson@arm.com     * Since we are taking decisions out of order, we need to keep
56410208Sandreas.hansson@arm.com     * track of what power transition is happening at what time, such
56510208Sandreas.hansson@arm.com     * that we can go back in time and change history. For example, if
56610208Sandreas.hansson@arm.com     * we precharge all banks and schedule going to the idle state, we
56710208Sandreas.hansson@arm.com     * might at a later point decide to activate a bank before the
56810208Sandreas.hansson@arm.com     * transition to idle would have taken place.
56910208Sandreas.hansson@arm.com     */
57010208Sandreas.hansson@arm.com    PowerState pwrStateTrans;
57110208Sandreas.hansson@arm.com
57210208Sandreas.hansson@arm.com    /**
57310208Sandreas.hansson@arm.com     * Current power state.
57410208Sandreas.hansson@arm.com     */
57510208Sandreas.hansson@arm.com    PowerState pwrState;
57610208Sandreas.hansson@arm.com
57710208Sandreas.hansson@arm.com    /**
57810208Sandreas.hansson@arm.com     * Schedule a power state transition in the future, and
57910208Sandreas.hansson@arm.com     * potentially override an already scheduled transition.
58010208Sandreas.hansson@arm.com     *
58110208Sandreas.hansson@arm.com     * @param pwr_state Power state to transition to
58210208Sandreas.hansson@arm.com     * @param tick Tick when transition should take place
58310208Sandreas.hansson@arm.com     */
58410208Sandreas.hansson@arm.com    void schedulePowerEvent(PowerState pwr_state, Tick tick);
58510208Sandreas.hansson@arm.com
5869243SN/A    Tick prevArrival;
5879243SN/A
58810206Sandreas.hansson@arm.com    /**
58910206Sandreas.hansson@arm.com     * The soonest you have to start thinking about the next request
59010206Sandreas.hansson@arm.com     * is the longest access time that can occur before
59110206Sandreas.hansson@arm.com     * busBusyUntil. Assuming you need to precharge, open a new row,
59210206Sandreas.hansson@arm.com     * and access, it is tRP + tRCD + tCL.
59310206Sandreas.hansson@arm.com     */
59410206Sandreas.hansson@arm.com    Tick nextReqTime;
5959972SN/A
5969243SN/A    // All statistics that the model needs to capture
5979243SN/A    Stats::Scalar readReqs;
5989243SN/A    Stats::Scalar writeReqs;
5999831SN/A    Stats::Scalar readBursts;
6009831SN/A    Stats::Scalar writeBursts;
6019975SN/A    Stats::Scalar bytesReadDRAM;
6029975SN/A    Stats::Scalar bytesReadWrQ;
6039243SN/A    Stats::Scalar bytesWritten;
6049977SN/A    Stats::Scalar bytesReadSys;
6059977SN/A    Stats::Scalar bytesWrittenSys;
6069243SN/A    Stats::Scalar servicedByWrQ;
6079977SN/A    Stats::Scalar mergedWrBursts;
6089243SN/A    Stats::Scalar neitherReadNorWrite;
6099977SN/A    Stats::Vector perBankRdBursts;
6109977SN/A    Stats::Vector perBankWrBursts;
6119243SN/A    Stats::Scalar numRdRetry;
6129243SN/A    Stats::Scalar numWrRetry;
6139243SN/A    Stats::Scalar totGap;
6149243SN/A    Stats::Vector readPktSize;
6159243SN/A    Stats::Vector writePktSize;
6169243SN/A    Stats::Vector rdQLenPdf;
6179243SN/A    Stats::Vector wrQLenPdf;
6189727SN/A    Stats::Histogram bytesPerActivate;
61910147Sandreas.hansson@arm.com    Stats::Histogram rdPerTurnAround;
62010147Sandreas.hansson@arm.com    Stats::Histogram wrPerTurnAround;
6219243SN/A
6229243SN/A    // Latencies summed over all requests
6239243SN/A    Stats::Scalar totQLat;
6249243SN/A    Stats::Scalar totMemAccLat;
6259243SN/A    Stats::Scalar totBusLat;
6269243SN/A
6279243SN/A    // Average latencies per request
6289243SN/A    Stats::Formula avgQLat;
6299243SN/A    Stats::Formula avgBusLat;
6309243SN/A    Stats::Formula avgMemAccLat;
6319243SN/A
6329243SN/A    // Average bandwidth
6339243SN/A    Stats::Formula avgRdBW;
6349243SN/A    Stats::Formula avgWrBW;
6359977SN/A    Stats::Formula avgRdBWSys;
6369977SN/A    Stats::Formula avgWrBWSys;
6379243SN/A    Stats::Formula peakBW;
6389243SN/A    Stats::Formula busUtil;
6399975SN/A    Stats::Formula busUtilRead;
6409975SN/A    Stats::Formula busUtilWrite;
6419243SN/A
6429243SN/A    // Average queue lengths
6439243SN/A    Stats::Average avgRdQLen;
6449243SN/A    Stats::Average avgWrQLen;
6459243SN/A
6469243SN/A    // Row hit count and rate
6479243SN/A    Stats::Scalar readRowHits;
6489243SN/A    Stats::Scalar writeRowHits;
6499243SN/A    Stats::Formula readRowHitRate;
6509243SN/A    Stats::Formula writeRowHitRate;
6519243SN/A    Stats::Formula avgGap;
6529243SN/A
6539975SN/A    // DRAM Power Calculation
6549975SN/A    Stats::Formula pageHitRate;
65510208Sandreas.hansson@arm.com    Stats::Vector pwrStateTime;
6569975SN/A
65710208Sandreas.hansson@arm.com    // Track when we transitioned to the current power state
65810208Sandreas.hansson@arm.com    Tick pwrStateTick;
65910207Sandreas.hansson@arm.com
6609975SN/A    // To track number of banks which are currently active
6619975SN/A    unsigned int numBanksActive;
6629975SN/A
6639349SN/A    /** @todo this is a temporary workaround until the 4-phase code is
6649349SN/A     * committed. upstream caches needs this packet until true is returned, so
6659349SN/A     * hold onto it for deletion until a subsequent call
6669349SN/A     */
6679349SN/A    std::vector<PacketPtr> pendingDelete;
6689349SN/A
6699243SN/A  public:
6709243SN/A
6719243SN/A    void regStats();
6729243SN/A
67310146Sandreas.hansson@arm.com    DRAMCtrl(const DRAMCtrlParams* p);
6749243SN/A
6759342SN/A    unsigned int drain(DrainManager* dm);
6769243SN/A
6779294SN/A    virtual BaseSlavePort& getSlavePort(const std::string& if_name,
6789294SN/A                                        PortID idx = InvalidPortID);
6799243SN/A
6809243SN/A    virtual void init();
6819243SN/A    virtual void startup();
6829243SN/A
6839243SN/A  protected:
6849243SN/A
6859243SN/A    Tick recvAtomic(PacketPtr pkt);
6869243SN/A    void recvFunctional(PacketPtr pkt);
6879243SN/A    bool recvTimingReq(PacketPtr pkt);
6889243SN/A
6899243SN/A};
6909243SN/A
69110146Sandreas.hansson@arm.com#endif //__MEM_DRAM_CTRL_HH__
692