dram_ctrl.cc revision 10247
1/*
2 * Copyright (c) 2010-2014 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 */
44
45#include "base/bitfield.hh"
46#include "base/trace.hh"
47#include "debug/DRAM.hh"
48#include "debug/DRAMPower.hh"
49#include "debug/DRAMState.hh"
50#include "debug/Drain.hh"
51#include "mem/dram_ctrl.hh"
52#include "sim/system.hh"
53
54using namespace std;
55
56DRAMCtrl::DRAMCtrl(const DRAMCtrlParams* p) :
57    AbstractMemory(p),
58    port(name() + ".port", *this),
59    retryRdReq(false), retryWrReq(false),
60    busState(READ),
61    nextReqEvent(this), respondEvent(this), activateEvent(this),
62    prechargeEvent(this), refreshEvent(this), powerEvent(this),
63    drainManager(NULL),
64    deviceBusWidth(p->device_bus_width), burstLength(p->burst_length),
65    deviceRowBufferSize(p->device_rowbuffer_size),
66    devicesPerRank(p->devices_per_rank),
67    burstSize((devicesPerRank * burstLength * deviceBusWidth) / 8),
68    rowBufferSize(devicesPerRank * deviceRowBufferSize),
69    columnsPerRowBuffer(rowBufferSize / burstSize),
70    ranksPerChannel(p->ranks_per_channel),
71    banksPerRank(p->banks_per_rank), channels(p->channels), rowsPerBank(0),
72    readBufferSize(p->read_buffer_size),
73    writeBufferSize(p->write_buffer_size),
74    writeHighThreshold(writeBufferSize * p->write_high_thresh_perc / 100.0),
75    writeLowThreshold(writeBufferSize * p->write_low_thresh_perc / 100.0),
76    minWritesPerSwitch(p->min_writes_per_switch),
77    writesThisTime(0), readsThisTime(0),
78    tCK(p->tCK), tWTR(p->tWTR), tRTW(p->tRTW), tBURST(p->tBURST),
79    tRCD(p->tRCD), tCL(p->tCL), tRP(p->tRP), tRAS(p->tRAS), tWR(p->tWR),
80    tRTP(p->tRTP), tRFC(p->tRFC), tREFI(p->tREFI), tRRD(p->tRRD),
81    tXAW(p->tXAW), activationLimit(p->activation_limit),
82    memSchedPolicy(p->mem_sched_policy), addrMapping(p->addr_mapping),
83    pageMgmt(p->page_policy),
84    maxAccessesPerRow(p->max_accesses_per_row),
85    frontendLatency(p->static_frontend_latency),
86    backendLatency(p->static_backend_latency),
87    busBusyUntil(0), refreshDueAt(0), refreshState(REF_IDLE),
88    pwrStateTrans(PWR_IDLE), pwrState(PWR_IDLE), prevArrival(0),
89    nextReqTime(0), pwrStateTick(0), numBanksActive(0)
90{
91    // create the bank states based on the dimensions of the ranks and
92    // banks
93    banks.resize(ranksPerChannel);
94    actTicks.resize(ranksPerChannel);
95    for (size_t c = 0; c < ranksPerChannel; ++c) {
96        banks[c].resize(banksPerRank);
97        actTicks[c].resize(activationLimit, 0);
98    }
99
100    // set the bank indices
101    for (int r = 0; r < ranksPerChannel; r++) {
102        for (int b = 0; b < banksPerRank; b++) {
103            banks[r][b].rank = r;
104            banks[r][b].bank = b;
105        }
106    }
107
108    // perform a basic check of the write thresholds
109    if (p->write_low_thresh_perc >= p->write_high_thresh_perc)
110        fatal("Write buffer low threshold %d must be smaller than the "
111              "high threshold %d\n", p->write_low_thresh_perc,
112              p->write_high_thresh_perc);
113
114    // determine the rows per bank by looking at the total capacity
115    uint64_t capacity = ULL(1) << ceilLog2(AbstractMemory::size());
116
117    DPRINTF(DRAM, "Memory capacity %lld (%lld) bytes\n", capacity,
118            AbstractMemory::size());
119
120    DPRINTF(DRAM, "Row buffer size %d bytes with %d columns per row buffer\n",
121            rowBufferSize, columnsPerRowBuffer);
122
123    rowsPerBank = capacity / (rowBufferSize * banksPerRank * ranksPerChannel);
124
125    if (range.interleaved()) {
126        if (channels != range.stripes())
127            fatal("%s has %d interleaved address stripes but %d channel(s)\n",
128                  name(), range.stripes(), channels);
129
130        if (addrMapping == Enums::RoRaBaChCo) {
131            if (rowBufferSize != range.granularity()) {
132                fatal("Interleaving of %s doesn't match RoRaBaChCo "
133                      "address map\n", name());
134            }
135        } else if (addrMapping == Enums::RoRaBaCoCh) {
136            if (system()->cacheLineSize() != range.granularity()) {
137                fatal("Interleaving of %s doesn't match RoRaBaCoCh "
138                      "address map\n", name());
139            }
140        } else if (addrMapping == Enums::RoCoRaBaCh) {
141            if (system()->cacheLineSize() != range.granularity())
142                fatal("Interleaving of %s doesn't match RoCoRaBaCh "
143                      "address map\n", name());
144        }
145    }
146
147    // some basic sanity checks
148    if (tREFI <= tRP || tREFI <= tRFC) {
149        fatal("tREFI (%d) must be larger than tRP (%d) and tRFC (%d)\n",
150              tREFI, tRP, tRFC);
151    }
152}
153
154void
155DRAMCtrl::init()
156{
157    if (!port.isConnected()) {
158        fatal("DRAMCtrl %s is unconnected!\n", name());
159    } else {
160        port.sendRangeChange();
161    }
162}
163
164void
165DRAMCtrl::startup()
166{
167    // update the start tick for the precharge accounting to the
168    // current tick
169    pwrStateTick = curTick();
170
171    // shift the bus busy time sufficiently far ahead that we never
172    // have to worry about negative values when computing the time for
173    // the next request, this will add an insignificant bubble at the
174    // start of simulation
175    busBusyUntil = curTick() + tRP + tRCD + tCL;
176
177    // kick off the refresh, and give ourselves enough time to
178    // precharge
179    schedule(refreshEvent, curTick() + tREFI - tRP);
180}
181
182Tick
183DRAMCtrl::recvAtomic(PacketPtr pkt)
184{
185    DPRINTF(DRAM, "recvAtomic: %s 0x%x\n", pkt->cmdString(), pkt->getAddr());
186
187    // do the actual memory access and turn the packet into a response
188    access(pkt);
189
190    Tick latency = 0;
191    if (!pkt->memInhibitAsserted() && pkt->hasData()) {
192        // this value is not supposed to be accurate, just enough to
193        // keep things going, mimic a closed page
194        latency = tRP + tRCD + tCL;
195    }
196    return latency;
197}
198
199bool
200DRAMCtrl::readQueueFull(unsigned int neededEntries) const
201{
202    DPRINTF(DRAM, "Read queue limit %d, current size %d, entries needed %d\n",
203            readBufferSize, readQueue.size() + respQueue.size(),
204            neededEntries);
205
206    return
207        (readQueue.size() + respQueue.size() + neededEntries) > readBufferSize;
208}
209
210bool
211DRAMCtrl::writeQueueFull(unsigned int neededEntries) const
212{
213    DPRINTF(DRAM, "Write queue limit %d, current size %d, entries needed %d\n",
214            writeBufferSize, writeQueue.size(), neededEntries);
215    return (writeQueue.size() + neededEntries) > writeBufferSize;
216}
217
218DRAMCtrl::DRAMPacket*
219DRAMCtrl::decodeAddr(PacketPtr pkt, Addr dramPktAddr, unsigned size,
220                       bool isRead)
221{
222    // decode the address based on the address mapping scheme, with
223    // Ro, Ra, Co, Ba and Ch denoting row, rank, column, bank and
224    // channel, respectively
225    uint8_t rank;
226    uint8_t bank;
227    // use a 64-bit unsigned during the computations as the row is
228    // always the top bits, and check before creating the DRAMPacket
229    uint64_t row;
230
231    // truncate the address to the access granularity
232    Addr addr = dramPktAddr / burstSize;
233
234    // we have removed the lowest order address bits that denote the
235    // position within the column
236    if (addrMapping == Enums::RoRaBaChCo) {
237        // the lowest order bits denote the column to ensure that
238        // sequential cache lines occupy the same row
239        addr = addr / columnsPerRowBuffer;
240
241        // take out the channel part of the address
242        addr = addr / channels;
243
244        // after the channel bits, get the bank bits to interleave
245        // over the banks
246        bank = addr % banksPerRank;
247        addr = addr / banksPerRank;
248
249        // after the bank, we get the rank bits which thus interleaves
250        // over the ranks
251        rank = addr % ranksPerChannel;
252        addr = addr / ranksPerChannel;
253
254        // lastly, get the row bits
255        row = addr % rowsPerBank;
256        addr = addr / rowsPerBank;
257    } else if (addrMapping == Enums::RoRaBaCoCh) {
258        // take out the channel part of the address
259        addr = addr / channels;
260
261        // next, the column
262        addr = addr / columnsPerRowBuffer;
263
264        // after the column bits, we get the bank bits to interleave
265        // over the banks
266        bank = addr % banksPerRank;
267        addr = addr / banksPerRank;
268
269        // after the bank, we get the rank bits which thus interleaves
270        // over the ranks
271        rank = addr % ranksPerChannel;
272        addr = addr / ranksPerChannel;
273
274        // lastly, get the row bits
275        row = addr % rowsPerBank;
276        addr = addr / rowsPerBank;
277    } else if (addrMapping == Enums::RoCoRaBaCh) {
278        // optimise for closed page mode and utilise maximum
279        // parallelism of the DRAM (at the cost of power)
280
281        // take out the channel part of the address, not that this has
282        // to match with how accesses are interleaved between the
283        // controllers in the address mapping
284        addr = addr / channels;
285
286        // start with the bank bits, as this provides the maximum
287        // opportunity for parallelism between requests
288        bank = addr % banksPerRank;
289        addr = addr / banksPerRank;
290
291        // next get the rank bits
292        rank = addr % ranksPerChannel;
293        addr = addr / ranksPerChannel;
294
295        // next the column bits which we do not need to keep track of
296        // and simply skip past
297        addr = addr / columnsPerRowBuffer;
298
299        // lastly, get the row bits
300        row = addr % rowsPerBank;
301        addr = addr / rowsPerBank;
302    } else
303        panic("Unknown address mapping policy chosen!");
304
305    assert(rank < ranksPerChannel);
306    assert(bank < banksPerRank);
307    assert(row < rowsPerBank);
308    assert(row < Bank::NO_ROW);
309
310    DPRINTF(DRAM, "Address: %lld Rank %d Bank %d Row %d\n",
311            dramPktAddr, rank, bank, row);
312
313    // create the corresponding DRAM packet with the entry time and
314    // ready time set to the current tick, the latter will be updated
315    // later
316    uint16_t bank_id = banksPerRank * rank + bank;
317    return new DRAMPacket(pkt, isRead, rank, bank, row, bank_id, dramPktAddr,
318                          size, banks[rank][bank]);
319}
320
321void
322DRAMCtrl::addToReadQueue(PacketPtr pkt, unsigned int pktCount)
323{
324    // only add to the read queue here. whenever the request is
325    // eventually done, set the readyTime, and call schedule()
326    assert(!pkt->isWrite());
327
328    assert(pktCount != 0);
329
330    // if the request size is larger than burst size, the pkt is split into
331    // multiple DRAM packets
332    // Note if the pkt starting address is not aligened to burst size, the
333    // address of first DRAM packet is kept unaliged. Subsequent DRAM packets
334    // are aligned to burst size boundaries. This is to ensure we accurately
335    // check read packets against packets in write queue.
336    Addr addr = pkt->getAddr();
337    unsigned pktsServicedByWrQ = 0;
338    BurstHelper* burst_helper = NULL;
339    for (int cnt = 0; cnt < pktCount; ++cnt) {
340        unsigned size = std::min((addr | (burstSize - 1)) + 1,
341                        pkt->getAddr() + pkt->getSize()) - addr;
342        readPktSize[ceilLog2(size)]++;
343        readBursts++;
344
345        // First check write buffer to see if the data is already at
346        // the controller
347        bool foundInWrQ = false;
348        for (auto i = writeQueue.begin(); i != writeQueue.end(); ++i) {
349            // check if the read is subsumed in the write entry we are
350            // looking at
351            if ((*i)->addr <= addr &&
352                (addr + size) <= ((*i)->addr + (*i)->size)) {
353                foundInWrQ = true;
354                servicedByWrQ++;
355                pktsServicedByWrQ++;
356                DPRINTF(DRAM, "Read to addr %lld with size %d serviced by "
357                        "write queue\n", addr, size);
358                bytesReadWrQ += burstSize;
359                break;
360            }
361        }
362
363        // If not found in the write q, make a DRAM packet and
364        // push it onto the read queue
365        if (!foundInWrQ) {
366
367            // Make the burst helper for split packets
368            if (pktCount > 1 && burst_helper == NULL) {
369                DPRINTF(DRAM, "Read to addr %lld translates to %d "
370                        "dram requests\n", pkt->getAddr(), pktCount);
371                burst_helper = new BurstHelper(pktCount);
372            }
373
374            DRAMPacket* dram_pkt = decodeAddr(pkt, addr, size, true);
375            dram_pkt->burstHelper = burst_helper;
376
377            assert(!readQueueFull(1));
378            rdQLenPdf[readQueue.size() + respQueue.size()]++;
379
380            DPRINTF(DRAM, "Adding to read queue\n");
381
382            readQueue.push_back(dram_pkt);
383
384            // Update stats
385            avgRdQLen = readQueue.size() + respQueue.size();
386        }
387
388        // Starting address of next dram pkt (aligend to burstSize boundary)
389        addr = (addr | (burstSize - 1)) + 1;
390    }
391
392    // If all packets are serviced by write queue, we send the repsonse back
393    if (pktsServicedByWrQ == pktCount) {
394        accessAndRespond(pkt, frontendLatency);
395        return;
396    }
397
398    // Update how many split packets are serviced by write queue
399    if (burst_helper != NULL)
400        burst_helper->burstsServiced = pktsServicedByWrQ;
401
402    // If we are not already scheduled to get a request out of the
403    // queue, do so now
404    if (!nextReqEvent.scheduled()) {
405        DPRINTF(DRAM, "Request scheduled immediately\n");
406        schedule(nextReqEvent, curTick());
407    }
408}
409
410void
411DRAMCtrl::addToWriteQueue(PacketPtr pkt, unsigned int pktCount)
412{
413    // only add to the write queue here. whenever the request is
414    // eventually done, set the readyTime, and call schedule()
415    assert(pkt->isWrite());
416
417    // if the request size is larger than burst size, the pkt is split into
418    // multiple DRAM packets
419    Addr addr = pkt->getAddr();
420    for (int cnt = 0; cnt < pktCount; ++cnt) {
421        unsigned size = std::min((addr | (burstSize - 1)) + 1,
422                        pkt->getAddr() + pkt->getSize()) - addr;
423        writePktSize[ceilLog2(size)]++;
424        writeBursts++;
425
426        // see if we can merge with an existing item in the write
427        // queue and keep track of whether we have merged or not so we
428        // can stop at that point and also avoid enqueueing a new
429        // request
430        bool merged = false;
431        auto w = writeQueue.begin();
432
433        while(!merged && w != writeQueue.end()) {
434            // either of the two could be first, if they are the same
435            // it does not matter which way we go
436            if ((*w)->addr >= addr) {
437                // the existing one starts after the new one, figure
438                // out where the new one ends with respect to the
439                // existing one
440                if ((addr + size) >= ((*w)->addr + (*w)->size)) {
441                    // check if the existing one is completely
442                    // subsumed in the new one
443                    DPRINTF(DRAM, "Merging write covering existing burst\n");
444                    merged = true;
445                    // update both the address and the size
446                    (*w)->addr = addr;
447                    (*w)->size = size;
448                } else if ((addr + size) >= (*w)->addr &&
449                           ((*w)->addr + (*w)->size - addr) <= burstSize) {
450                    // the new one is just before or partially
451                    // overlapping with the existing one, and together
452                    // they fit within a burst
453                    DPRINTF(DRAM, "Merging write before existing burst\n");
454                    merged = true;
455                    // the existing queue item needs to be adjusted with
456                    // respect to both address and size
457                    (*w)->size = (*w)->addr + (*w)->size - addr;
458                    (*w)->addr = addr;
459                }
460            } else {
461                // the new one starts after the current one, figure
462                // out where the existing one ends with respect to the
463                // new one
464                if (((*w)->addr + (*w)->size) >= (addr + size)) {
465                    // check if the new one is completely subsumed in the
466                    // existing one
467                    DPRINTF(DRAM, "Merging write into existing burst\n");
468                    merged = true;
469                    // no adjustments necessary
470                } else if (((*w)->addr + (*w)->size) >= addr &&
471                           (addr + size - (*w)->addr) <= burstSize) {
472                    // the existing one is just before or partially
473                    // overlapping with the new one, and together
474                    // they fit within a burst
475                    DPRINTF(DRAM, "Merging write after existing burst\n");
476                    merged = true;
477                    // the address is right, and only the size has
478                    // to be adjusted
479                    (*w)->size = addr + size - (*w)->addr;
480                }
481            }
482            ++w;
483        }
484
485        // if the item was not merged we need to create a new write
486        // and enqueue it
487        if (!merged) {
488            DRAMPacket* dram_pkt = decodeAddr(pkt, addr, size, false);
489
490            assert(writeQueue.size() < writeBufferSize);
491            wrQLenPdf[writeQueue.size()]++;
492
493            DPRINTF(DRAM, "Adding to write queue\n");
494
495            writeQueue.push_back(dram_pkt);
496
497            // Update stats
498            avgWrQLen = writeQueue.size();
499        } else {
500            // keep track of the fact that this burst effectively
501            // disappeared as it was merged with an existing one
502            mergedWrBursts++;
503        }
504
505        // Starting address of next dram pkt (aligend to burstSize boundary)
506        addr = (addr | (burstSize - 1)) + 1;
507    }
508
509    // we do not wait for the writes to be send to the actual memory,
510    // but instead take responsibility for the consistency here and
511    // snoop the write queue for any upcoming reads
512    // @todo, if a pkt size is larger than burst size, we might need a
513    // different front end latency
514    accessAndRespond(pkt, frontendLatency);
515
516    // If we are not already scheduled to get a request out of the
517    // queue, do so now
518    if (!nextReqEvent.scheduled()) {
519        DPRINTF(DRAM, "Request scheduled immediately\n");
520        schedule(nextReqEvent, curTick());
521    }
522}
523
524void
525DRAMCtrl::printQs() const {
526    DPRINTF(DRAM, "===READ QUEUE===\n\n");
527    for (auto i = readQueue.begin() ;  i != readQueue.end() ; ++i) {
528        DPRINTF(DRAM, "Read %lu\n", (*i)->addr);
529    }
530    DPRINTF(DRAM, "\n===RESP QUEUE===\n\n");
531    for (auto i = respQueue.begin() ;  i != respQueue.end() ; ++i) {
532        DPRINTF(DRAM, "Response %lu\n", (*i)->addr);
533    }
534    DPRINTF(DRAM, "\n===WRITE QUEUE===\n\n");
535    for (auto i = writeQueue.begin() ;  i != writeQueue.end() ; ++i) {
536        DPRINTF(DRAM, "Write %lu\n", (*i)->addr);
537    }
538}
539
540bool
541DRAMCtrl::recvTimingReq(PacketPtr pkt)
542{
543    /// @todo temporary hack to deal with memory corruption issues until
544    /// 4-phase transactions are complete
545    for (int x = 0; x < pendingDelete.size(); x++)
546        delete pendingDelete[x];
547    pendingDelete.clear();
548
549    // This is where we enter from the outside world
550    DPRINTF(DRAM, "recvTimingReq: request %s addr %lld size %d\n",
551            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
552
553    // simply drop inhibited packets for now
554    if (pkt->memInhibitAsserted()) {
555        DPRINTF(DRAM, "Inhibited packet -- Dropping it now\n");
556        pendingDelete.push_back(pkt);
557        return true;
558    }
559
560    // Calc avg gap between requests
561    if (prevArrival != 0) {
562        totGap += curTick() - prevArrival;
563    }
564    prevArrival = curTick();
565
566
567    // Find out how many dram packets a pkt translates to
568    // If the burst size is equal or larger than the pkt size, then a pkt
569    // translates to only one dram packet. Otherwise, a pkt translates to
570    // multiple dram packets
571    unsigned size = pkt->getSize();
572    unsigned offset = pkt->getAddr() & (burstSize - 1);
573    unsigned int dram_pkt_count = divCeil(offset + size, burstSize);
574
575    // check local buffers and do not accept if full
576    if (pkt->isRead()) {
577        assert(size != 0);
578        if (readQueueFull(dram_pkt_count)) {
579            DPRINTF(DRAM, "Read queue full, not accepting\n");
580            // remember that we have to retry this port
581            retryRdReq = true;
582            numRdRetry++;
583            return false;
584        } else {
585            addToReadQueue(pkt, dram_pkt_count);
586            readReqs++;
587            bytesReadSys += size;
588        }
589    } else if (pkt->isWrite()) {
590        assert(size != 0);
591        if (writeQueueFull(dram_pkt_count)) {
592            DPRINTF(DRAM, "Write queue full, not accepting\n");
593            // remember that we have to retry this port
594            retryWrReq = true;
595            numWrRetry++;
596            return false;
597        } else {
598            addToWriteQueue(pkt, dram_pkt_count);
599            writeReqs++;
600            bytesWrittenSys += size;
601        }
602    } else {
603        DPRINTF(DRAM,"Neither read nor write, ignore timing\n");
604        neitherReadNorWrite++;
605        accessAndRespond(pkt, 1);
606    }
607
608    return true;
609}
610
611void
612DRAMCtrl::processRespondEvent()
613{
614    DPRINTF(DRAM,
615            "processRespondEvent(): Some req has reached its readyTime\n");
616
617    DRAMPacket* dram_pkt = respQueue.front();
618
619    if (dram_pkt->burstHelper) {
620        // it is a split packet
621        dram_pkt->burstHelper->burstsServiced++;
622        if (dram_pkt->burstHelper->burstsServiced ==
623            dram_pkt->burstHelper->burstCount) {
624            // we have now serviced all children packets of a system packet
625            // so we can now respond to the requester
626            // @todo we probably want to have a different front end and back
627            // end latency for split packets
628            accessAndRespond(dram_pkt->pkt, frontendLatency + backendLatency);
629            delete dram_pkt->burstHelper;
630            dram_pkt->burstHelper = NULL;
631        }
632    } else {
633        // it is not a split packet
634        accessAndRespond(dram_pkt->pkt, frontendLatency + backendLatency);
635    }
636
637    delete respQueue.front();
638    respQueue.pop_front();
639
640    if (!respQueue.empty()) {
641        assert(respQueue.front()->readyTime >= curTick());
642        assert(!respondEvent.scheduled());
643        schedule(respondEvent, respQueue.front()->readyTime);
644    } else {
645        // if there is nothing left in any queue, signal a drain
646        if (writeQueue.empty() && readQueue.empty() &&
647            drainManager) {
648            drainManager->signalDrainDone();
649            drainManager = NULL;
650        }
651    }
652
653    // We have made a location in the queue available at this point,
654    // so if there is a read that was forced to wait, retry now
655    if (retryRdReq) {
656        retryRdReq = false;
657        port.sendRetry();
658    }
659}
660
661void
662DRAMCtrl::chooseNext(std::deque<DRAMPacket*>& queue)
663{
664    // This method does the arbitration between requests. The chosen
665    // packet is simply moved to the head of the queue. The other
666    // methods know that this is the place to look. For example, with
667    // FCFS, this method does nothing
668    assert(!queue.empty());
669
670    if (queue.size() == 1) {
671        DPRINTF(DRAM, "Single request, nothing to do\n");
672        return;
673    }
674
675    if (memSchedPolicy == Enums::fcfs) {
676        // Do nothing, since the correct request is already head
677    } else if (memSchedPolicy == Enums::frfcfs) {
678        reorderQueue(queue);
679    } else
680        panic("No scheduling policy chosen\n");
681}
682
683void
684DRAMCtrl::reorderQueue(std::deque<DRAMPacket*>& queue)
685{
686    // Only determine this when needed
687    uint64_t earliest_banks = 0;
688
689    // Search for row hits first, if no row hit is found then schedule the
690    // packet to one of the earliest banks available
691    bool found_earliest_pkt = false;
692    auto selected_pkt_it = queue.begin();
693
694    for (auto i = queue.begin(); i != queue.end() ; ++i) {
695        DRAMPacket* dram_pkt = *i;
696        const Bank& bank = dram_pkt->bankRef;
697        // Check if it is a row hit
698        if (bank.openRow == dram_pkt->row) {
699            // FCFS within the hits
700            DPRINTF(DRAM, "Row buffer hit\n");
701            selected_pkt_it = i;
702            break;
703        } else if (!found_earliest_pkt) {
704            // No row hit, go for first ready
705            if (earliest_banks == 0)
706                earliest_banks = minBankActAt(queue);
707
708            // simplistic approximation of when the bank can issue an
709            // activate, this is calculated in minBankActAt and could
710            // be cached
711            Tick act_at = bank.openRow == Bank::NO_ROW ?
712                bank.actAllowedAt :
713                std::max(bank.preAllowedAt, curTick()) + tRP;
714
715            // Bank is ready or is the first available bank
716            if (act_at <= curTick() ||
717                bits(earliest_banks, dram_pkt->bankId, dram_pkt->bankId)) {
718                // Remember the packet to be scheduled to one of the earliest
719                // banks available, FCFS amongst the earliest banks
720                selected_pkt_it = i;
721                found_earliest_pkt = true;
722            }
723        }
724    }
725
726    DRAMPacket* selected_pkt = *selected_pkt_it;
727    queue.erase(selected_pkt_it);
728    queue.push_front(selected_pkt);
729}
730
731void
732DRAMCtrl::accessAndRespond(PacketPtr pkt, Tick static_latency)
733{
734    DPRINTF(DRAM, "Responding to Address %lld.. ",pkt->getAddr());
735
736    bool needsResponse = pkt->needsResponse();
737    // do the actual memory access which also turns the packet into a
738    // response
739    access(pkt);
740
741    // turn packet around to go back to requester if response expected
742    if (needsResponse) {
743        // access already turned the packet into a response
744        assert(pkt->isResponse());
745
746        // @todo someone should pay for this
747        pkt->busFirstWordDelay = pkt->busLastWordDelay = 0;
748
749        // queue the packet in the response queue to be sent out after
750        // the static latency has passed
751        port.schedTimingResp(pkt, curTick() + static_latency);
752    } else {
753        // @todo the packet is going to be deleted, and the DRAMPacket
754        // is still having a pointer to it
755        pendingDelete.push_back(pkt);
756    }
757
758    DPRINTF(DRAM, "Done\n");
759
760    return;
761}
762
763void
764DRAMCtrl::activateBank(Bank& bank, Tick act_tick, uint32_t row)
765{
766    // get the rank index from the bank
767    uint8_t rank = bank.rank;
768
769    assert(actTicks[rank].size() == activationLimit);
770
771    DPRINTF(DRAM, "Activate at tick %d\n", act_tick);
772
773    // update the open row
774    assert(bank.openRow == Bank::NO_ROW);
775    bank.openRow = row;
776
777    // start counting anew, this covers both the case when we
778    // auto-precharged, and when this access is forced to
779    // precharge
780    bank.bytesAccessed = 0;
781    bank.rowAccesses = 0;
782
783    ++numBanksActive;
784    assert(numBanksActive <= banksPerRank * ranksPerChannel);
785
786    DPRINTF(DRAM, "Activate bank %d, rank %d at tick %lld, now got %d active\n",
787            bank.bank, bank.rank, act_tick, numBanksActive);
788
789    DPRINTF(DRAMPower, "%llu,ACT,%d,%d\n", divCeil(act_tick, tCK), bank.bank,
790            bank.rank);
791
792    // The next access has to respect tRAS for this bank
793    bank.preAllowedAt = act_tick + tRAS;
794
795    // Respect the row-to-column command delay
796    bank.colAllowedAt = act_tick + tRCD;
797
798    // start by enforcing tRRD
799    for(int i = 0; i < banksPerRank; i++) {
800        // next activate to any bank in this rank must not happen
801        // before tRRD
802        banks[rank][i].actAllowedAt = std::max(act_tick + tRRD,
803                                               banks[rank][i].actAllowedAt);
804    }
805
806    // next, we deal with tXAW, if the activation limit is disabled
807    // then we are done
808    if (actTicks[rank].empty())
809        return;
810
811    // sanity check
812    if (actTicks[rank].back() && (act_tick - actTicks[rank].back()) < tXAW) {
813        panic("Got %d activates in window %d (%llu - %llu) which is smaller "
814              "than %llu\n", activationLimit, act_tick - actTicks[rank].back(),
815              act_tick, actTicks[rank].back(), tXAW);
816    }
817
818    // shift the times used for the book keeping, the last element
819    // (highest index) is the oldest one and hence the lowest value
820    actTicks[rank].pop_back();
821
822    // record an new activation (in the future)
823    actTicks[rank].push_front(act_tick);
824
825    // cannot activate more than X times in time window tXAW, push the
826    // next one (the X + 1'st activate) to be tXAW away from the
827    // oldest in our window of X
828    if (actTicks[rank].back() && (act_tick - actTicks[rank].back()) < tXAW) {
829        DPRINTF(DRAM, "Enforcing tXAW with X = %d, next activate no earlier "
830                "than %llu\n", activationLimit, actTicks[rank].back() + tXAW);
831            for(int j = 0; j < banksPerRank; j++)
832                // next activate must not happen before end of window
833                banks[rank][j].actAllowedAt =
834                    std::max(actTicks[rank].back() + tXAW,
835                             banks[rank][j].actAllowedAt);
836    }
837
838    // at the point when this activate takes place, make sure we
839    // transition to the active power state
840    if (!activateEvent.scheduled())
841        schedule(activateEvent, act_tick);
842    else if (activateEvent.when() > act_tick)
843        // move it sooner in time
844        reschedule(activateEvent, act_tick);
845}
846
847void
848DRAMCtrl::processActivateEvent()
849{
850    // we should transition to the active state as soon as any bank is active
851    if (pwrState != PWR_ACT)
852        // note that at this point numBanksActive could be back at
853        // zero again due to a precharge scheduled in the future
854        schedulePowerEvent(PWR_ACT, curTick());
855}
856
857void
858DRAMCtrl::prechargeBank(Bank& bank, Tick pre_at, bool trace)
859{
860    // make sure the bank has an open row
861    assert(bank.openRow != Bank::NO_ROW);
862
863    // sample the bytes per activate here since we are closing
864    // the page
865    bytesPerActivate.sample(bank.bytesAccessed);
866
867    bank.openRow = Bank::NO_ROW;
868
869    // no precharge allowed before this one
870    bank.preAllowedAt = pre_at;
871
872    Tick pre_done_at = pre_at + tRP;
873
874    bank.actAllowedAt = std::max(bank.actAllowedAt, pre_done_at);
875
876    assert(numBanksActive != 0);
877    --numBanksActive;
878
879    DPRINTF(DRAM, "Precharging bank %d, rank %d at tick %lld, now got "
880            "%d active\n", bank.bank, bank.rank, pre_at, numBanksActive);
881
882    if (trace)
883        DPRINTF(DRAMPower, "%llu,PRE,%d,%d\n", divCeil(pre_at, tCK),
884                bank.bank, bank.rank);
885
886    // if we look at the current number of active banks we might be
887    // tempted to think the DRAM is now idle, however this can be
888    // undone by an activate that is scheduled to happen before we
889    // would have reached the idle state, so schedule an event and
890    // rather check once we actually make it to the point in time when
891    // the (last) precharge takes place
892    if (!prechargeEvent.scheduled())
893        schedule(prechargeEvent, pre_done_at);
894    else if (prechargeEvent.when() < pre_done_at)
895        reschedule(prechargeEvent, pre_done_at);
896}
897
898void
899DRAMCtrl::processPrechargeEvent()
900{
901    // if we reached zero, then special conditions apply as we track
902    // if all banks are precharged for the power models
903    if (numBanksActive == 0) {
904        // we should transition to the idle state when the last bank
905        // is precharged
906        schedulePowerEvent(PWR_IDLE, curTick());
907    }
908}
909
910void
911DRAMCtrl::doDRAMAccess(DRAMPacket* dram_pkt)
912{
913    DPRINTF(DRAM, "Timing access to addr %lld, rank/bank/row %d %d %d\n",
914            dram_pkt->addr, dram_pkt->rank, dram_pkt->bank, dram_pkt->row);
915
916    // get the bank
917    Bank& bank = dram_pkt->bankRef;
918
919    // for the state we need to track if it is a row hit or not
920    bool row_hit = true;
921
922    // respect any constraints on the command (e.g. tRCD or tCCD)
923    Tick cmd_at = std::max(bank.colAllowedAt, curTick());
924
925    // Determine the access latency and update the bank state
926    if (bank.openRow == dram_pkt->row) {
927        // nothing to do
928    } else {
929        row_hit = false;
930
931        // If there is a page open, precharge it.
932        if (bank.openRow != Bank::NO_ROW) {
933            prechargeBank(bank, std::max(bank.preAllowedAt, curTick()));
934        }
935
936        // next we need to account for the delay in activating the
937        // page
938        Tick act_tick = std::max(bank.actAllowedAt, curTick());
939
940        // Record the activation and deal with all the global timing
941        // constraints caused be a new activation (tRRD and tXAW)
942        activateBank(bank, act_tick, dram_pkt->row);
943
944        // issue the command as early as possible
945        cmd_at = bank.colAllowedAt;
946    }
947
948    // we need to wait until the bus is available before we can issue
949    // the command
950    cmd_at = std::max(cmd_at, busBusyUntil - tCL);
951
952    // update the packet ready time
953    dram_pkt->readyTime = cmd_at + tCL + tBURST;
954
955    // only one burst can use the bus at any one point in time
956    assert(dram_pkt->readyTime - busBusyUntil >= tBURST);
957
958    // not strictly necessary, but update the time for the next
959    // read/write (add a max with tCCD here)
960    bank.colAllowedAt = cmd_at + tBURST;
961
962    // If this is a write, we also need to respect the write recovery
963    // time before a precharge, in the case of a read, respect the
964    // read to precharge constraint
965    bank.preAllowedAt = std::max(bank.preAllowedAt,
966                                 dram_pkt->isRead ? cmd_at + tRTP :
967                                 dram_pkt->readyTime + tWR);
968
969    // increment the bytes accessed and the accesses per row
970    bank.bytesAccessed += burstSize;
971    ++bank.rowAccesses;
972
973    // if we reached the max, then issue with an auto-precharge
974    bool auto_precharge = pageMgmt == Enums::close ||
975        bank.rowAccesses == maxAccessesPerRow;
976
977    // if we did not hit the limit, we might still want to
978    // auto-precharge
979    if (!auto_precharge &&
980        (pageMgmt == Enums::open_adaptive ||
981         pageMgmt == Enums::close_adaptive)) {
982        // a twist on the open and close page policies:
983        // 1) open_adaptive page policy does not blindly keep the
984        // page open, but close it if there are no row hits, and there
985        // are bank conflicts in the queue
986        // 2) close_adaptive page policy does not blindly close the
987        // page, but closes it only if there are no row hits in the queue.
988        // In this case, only force an auto precharge when there
989        // are no same page hits in the queue
990        bool got_more_hits = false;
991        bool got_bank_conflict = false;
992
993        // either look at the read queue or write queue
994        const deque<DRAMPacket*>& queue = dram_pkt->isRead ? readQueue :
995            writeQueue;
996        auto p = queue.begin();
997        // make sure we are not considering the packet that we are
998        // currently dealing with (which is the head of the queue)
999        ++p;
1000
1001        // keep on looking until we have found required condition or
1002        // reached the end
1003        while (!(got_more_hits &&
1004                 (got_bank_conflict || pageMgmt == Enums::close_adaptive)) &&
1005               p != queue.end()) {
1006            bool same_rank_bank = (dram_pkt->rank == (*p)->rank) &&
1007                (dram_pkt->bank == (*p)->bank);
1008            bool same_row = dram_pkt->row == (*p)->row;
1009            got_more_hits |= same_rank_bank && same_row;
1010            got_bank_conflict |= same_rank_bank && !same_row;
1011            ++p;
1012        }
1013
1014        // auto pre-charge when either
1015        // 1) open_adaptive policy, we have not got any more hits, and
1016        //    have a bank conflict
1017        // 2) close_adaptive policy and we have not got any more hits
1018        auto_precharge = !got_more_hits &&
1019            (got_bank_conflict || pageMgmt == Enums::close_adaptive);
1020    }
1021
1022    // DRAMPower trace command to be written
1023    std::string mem_cmd = dram_pkt->isRead ? "RD" : "WR";
1024
1025    // if this access should use auto-precharge, then we are
1026    // closing the row
1027    if (auto_precharge) {
1028        prechargeBank(bank, std::max(curTick(), bank.preAllowedAt), false);
1029
1030        mem_cmd.append("A");
1031
1032        DPRINTF(DRAM, "Auto-precharged bank: %d\n", dram_pkt->bankId);
1033    }
1034
1035    // Update bus state
1036    busBusyUntil = dram_pkt->readyTime;
1037
1038    DPRINTF(DRAM, "Access to %lld, ready at %lld bus busy until %lld.\n",
1039            dram_pkt->addr, dram_pkt->readyTime, busBusyUntil);
1040
1041    DPRINTF(DRAMPower, "%llu,%s,%d,%d\n", divCeil(cmd_at, tCK), mem_cmd,
1042            dram_pkt->bank, dram_pkt->rank);
1043
1044    // Update the minimum timing between the requests, this is a
1045    // conservative estimate of when we have to schedule the next
1046    // request to not introduce any unecessary bubbles. In most cases
1047    // we will wake up sooner than we have to.
1048    nextReqTime = busBusyUntil - (tRP + tRCD + tCL);
1049
1050    // Update the stats and schedule the next request
1051    if (dram_pkt->isRead) {
1052        ++readsThisTime;
1053        if (row_hit)
1054            readRowHits++;
1055        bytesReadDRAM += burstSize;
1056        perBankRdBursts[dram_pkt->bankId]++;
1057
1058        // Update latency stats
1059        totMemAccLat += dram_pkt->readyTime - dram_pkt->entryTime;
1060        totBusLat += tBURST;
1061        totQLat += cmd_at - dram_pkt->entryTime;
1062    } else {
1063        ++writesThisTime;
1064        if (row_hit)
1065            writeRowHits++;
1066        bytesWritten += burstSize;
1067        perBankWrBursts[dram_pkt->bankId]++;
1068    }
1069}
1070
1071void
1072DRAMCtrl::processNextReqEvent()
1073{
1074    if (busState == READ_TO_WRITE) {
1075        DPRINTF(DRAM, "Switching to writes after %d reads with %d reads "
1076                "waiting\n", readsThisTime, readQueue.size());
1077
1078        // sample and reset the read-related stats as we are now
1079        // transitioning to writes, and all reads are done
1080        rdPerTurnAround.sample(readsThisTime);
1081        readsThisTime = 0;
1082
1083        // now proceed to do the actual writes
1084        busState = WRITE;
1085    } else if (busState == WRITE_TO_READ) {
1086        DPRINTF(DRAM, "Switching to reads after %d writes with %d writes "
1087                "waiting\n", writesThisTime, writeQueue.size());
1088
1089        wrPerTurnAround.sample(writesThisTime);
1090        writesThisTime = 0;
1091
1092        busState = READ;
1093    }
1094
1095    if (refreshState != REF_IDLE) {
1096        // if a refresh waiting for this event loop to finish, then hand
1097        // over now, and do not schedule a new nextReqEvent
1098        if (refreshState == REF_DRAIN) {
1099            DPRINTF(DRAM, "Refresh drain done, now precharging\n");
1100
1101            refreshState = REF_PRE;
1102
1103            // hand control back to the refresh event loop
1104            schedule(refreshEvent, curTick());
1105        }
1106
1107        // let the refresh finish before issuing any further requests
1108        return;
1109    }
1110
1111    // when we get here it is either a read or a write
1112    if (busState == READ) {
1113
1114        // track if we should switch or not
1115        bool switch_to_writes = false;
1116
1117        if (readQueue.empty()) {
1118            // In the case there is no read request to go next,
1119            // trigger writes if we have passed the low threshold (or
1120            // if we are draining)
1121            if (!writeQueue.empty() &&
1122                (drainManager || writeQueue.size() > writeLowThreshold)) {
1123
1124                switch_to_writes = true;
1125            } else {
1126                // check if we are drained
1127                if (respQueue.empty () && drainManager) {
1128                    drainManager->signalDrainDone();
1129                    drainManager = NULL;
1130                }
1131
1132                // nothing to do, not even any point in scheduling an
1133                // event for the next request
1134                return;
1135            }
1136        } else {
1137            // Figure out which read request goes next, and move it to the
1138            // front of the read queue
1139            chooseNext(readQueue);
1140
1141            DRAMPacket* dram_pkt = readQueue.front();
1142
1143            doDRAMAccess(dram_pkt);
1144
1145            // At this point we're done dealing with the request
1146            readQueue.pop_front();
1147
1148            // sanity check
1149            assert(dram_pkt->size <= burstSize);
1150            assert(dram_pkt->readyTime >= curTick());
1151
1152            // Insert into response queue. It will be sent back to the
1153            // requestor at its readyTime
1154            if (respQueue.empty()) {
1155                assert(!respondEvent.scheduled());
1156                schedule(respondEvent, dram_pkt->readyTime);
1157            } else {
1158                assert(respQueue.back()->readyTime <= dram_pkt->readyTime);
1159                assert(respondEvent.scheduled());
1160            }
1161
1162            respQueue.push_back(dram_pkt);
1163
1164            // we have so many writes that we have to transition
1165            if (writeQueue.size() > writeHighThreshold) {
1166                switch_to_writes = true;
1167            }
1168        }
1169
1170        // switching to writes, either because the read queue is empty
1171        // and the writes have passed the low threshold (or we are
1172        // draining), or because the writes hit the hight threshold
1173        if (switch_to_writes) {
1174            // transition to writing
1175            busState = READ_TO_WRITE;
1176
1177            // add a bubble to the data bus, as defined by the
1178            // tRTW parameter
1179            busBusyUntil += tRTW;
1180
1181            // update the minimum timing between the requests,
1182            // this shifts us back in time far enough to do any
1183            // bank preparation
1184            nextReqTime = busBusyUntil - (tRP + tRCD + tCL);
1185        }
1186    } else {
1187        chooseNext(writeQueue);
1188        DRAMPacket* dram_pkt = writeQueue.front();
1189        // sanity check
1190        assert(dram_pkt->size <= burstSize);
1191        doDRAMAccess(dram_pkt);
1192
1193        writeQueue.pop_front();
1194        delete dram_pkt;
1195
1196        // If we emptied the write queue, or got sufficiently below the
1197        // threshold (using the minWritesPerSwitch as the hysteresis) and
1198        // are not draining, or we have reads waiting and have done enough
1199        // writes, then switch to reads.
1200        if (writeQueue.empty() ||
1201            (writeQueue.size() + minWritesPerSwitch < writeLowThreshold &&
1202             !drainManager) ||
1203            (!readQueue.empty() && writesThisTime >= minWritesPerSwitch)) {
1204            // turn the bus back around for reads again
1205            busState = WRITE_TO_READ;
1206
1207            // note that the we switch back to reads also in the idle
1208            // case, which eventually will check for any draining and
1209            // also pause any further scheduling if there is really
1210            // nothing to do
1211
1212            // here we get a bit creative and shift the bus busy time not
1213            // just the tWTR, but also a CAS latency to capture the fact
1214            // that we are allowed to prepare a new bank, but not issue a
1215            // read command until after tWTR, in essence we capture a
1216            // bubble on the data bus that is tWTR + tCL
1217            busBusyUntil += tWTR + tCL;
1218
1219            // update the minimum timing between the requests, this shifts
1220            // us back in time far enough to do any bank preparation
1221            nextReqTime = busBusyUntil - (tRP + tRCD + tCL);
1222        }
1223    }
1224
1225    schedule(nextReqEvent, std::max(nextReqTime, curTick()));
1226
1227    // If there is space available and we have writes waiting then let
1228    // them retry. This is done here to ensure that the retry does not
1229    // cause a nextReqEvent to be scheduled before we do so as part of
1230    // the next request processing
1231    if (retryWrReq && writeQueue.size() < writeBufferSize) {
1232        retryWrReq = false;
1233        port.sendRetry();
1234    }
1235}
1236
1237uint64_t
1238DRAMCtrl::minBankActAt(const deque<DRAMPacket*>& queue) const
1239{
1240    uint64_t bank_mask = 0;
1241    Tick min_act_at = MaxTick;
1242
1243    // deterimne if we have queued transactions targetting a
1244    // bank in question
1245    vector<bool> got_waiting(ranksPerChannel * banksPerRank, false);
1246    for (auto p = queue.begin(); p != queue.end(); ++p) {
1247        got_waiting[(*p)->bankId] = true;
1248    }
1249
1250    for (int i = 0; i < ranksPerChannel; i++) {
1251        for (int j = 0; j < banksPerRank; j++) {
1252            uint8_t bank_id = i * banksPerRank + j;
1253
1254            // if we have waiting requests for the bank, and it is
1255            // amongst the first available, update the mask
1256            if (got_waiting[bank_id]) {
1257                // simplistic approximation of when the bank can issue
1258                // an activate, ignoring any rank-to-rank switching
1259                // cost
1260                Tick act_at = banks[i][j].openRow == Bank::NO_ROW ?
1261                    banks[i][j].actAllowedAt :
1262                    std::max(banks[i][j].preAllowedAt, curTick()) + tRP;
1263
1264                if (act_at <= min_act_at) {
1265                    // reset bank mask if new minimum is found
1266                    if (act_at < min_act_at)
1267                        bank_mask = 0;
1268                    // set the bit corresponding to the available bank
1269                    replaceBits(bank_mask, bank_id, bank_id, 1);
1270                    min_act_at = act_at;
1271                }
1272            }
1273        }
1274    }
1275
1276    return bank_mask;
1277}
1278
1279void
1280DRAMCtrl::processRefreshEvent()
1281{
1282    // when first preparing the refresh, remember when it was due
1283    if (refreshState == REF_IDLE) {
1284        // remember when the refresh is due
1285        refreshDueAt = curTick();
1286
1287        // proceed to drain
1288        refreshState = REF_DRAIN;
1289
1290        DPRINTF(DRAM, "Refresh due\n");
1291    }
1292
1293    // let any scheduled read or write go ahead, after which it will
1294    // hand control back to this event loop
1295    if (refreshState == REF_DRAIN) {
1296        if (nextReqEvent.scheduled()) {
1297            // hand control over to the request loop until it is
1298            // evaluated next
1299            DPRINTF(DRAM, "Refresh awaiting draining\n");
1300
1301            return;
1302        } else {
1303            refreshState = REF_PRE;
1304        }
1305    }
1306
1307    // at this point, ensure that all banks are precharged
1308    if (refreshState == REF_PRE) {
1309        // precharge any active bank if we are not already in the idle
1310        // state
1311        if (pwrState != PWR_IDLE) {
1312            // at the moment, we use a precharge all even if there is
1313            // only a single bank open
1314            DPRINTF(DRAM, "Precharging all\n");
1315
1316            // first determine when we can precharge
1317            Tick pre_at = curTick();
1318            for (int i = 0; i < ranksPerChannel; i++) {
1319                for (int j = 0; j < banksPerRank; j++) {
1320                    // respect both causality and any existing bank
1321                    // constraints, some banks could already have a
1322                    // (auto) precharge scheduled
1323                    pre_at = std::max(banks[i][j].preAllowedAt, pre_at);
1324                }
1325            }
1326
1327            // make sure all banks are precharged, and for those that
1328            // already are, update their availability
1329            Tick act_allowed_at = pre_at + tRP;
1330
1331            for (int i = 0; i < ranksPerChannel; i++) {
1332                for (int j = 0; j < banksPerRank; j++) {
1333                    if (banks[i][j].openRow != Bank::NO_ROW) {
1334                        prechargeBank(banks[i][j], pre_at, false);
1335                    } else {
1336                        banks[i][j].actAllowedAt =
1337                            std::max(banks[i][j].actAllowedAt, act_allowed_at);
1338                        banks[i][j].preAllowedAt =
1339                            std::max(banks[i][j].preAllowedAt, pre_at);
1340                    }
1341                }
1342
1343                // at the moment this affects all ranks
1344                DPRINTF(DRAMPower, "%llu,PREA,0,%d\n", divCeil(pre_at, tCK),
1345                        i);
1346            }
1347        } else {
1348            DPRINTF(DRAM, "All banks already precharged, starting refresh\n");
1349
1350            // go ahead and kick the power state machine into gear if
1351            // we are already idle
1352            schedulePowerEvent(PWR_REF, curTick());
1353        }
1354
1355        refreshState = REF_RUN;
1356        assert(numBanksActive == 0);
1357
1358        // wait for all banks to be precharged, at which point the
1359        // power state machine will transition to the idle state, and
1360        // automatically move to a refresh, at that point it will also
1361        // call this method to get the refresh event loop going again
1362        return;
1363    }
1364
1365    // last but not least we perform the actual refresh
1366    if (refreshState == REF_RUN) {
1367        // should never get here with any banks active
1368        assert(numBanksActive == 0);
1369        assert(pwrState == PWR_REF);
1370
1371        Tick ref_done_at = curTick() + tRFC;
1372
1373        for (int i = 0; i < ranksPerChannel; i++) {
1374            for (int j = 0; j < banksPerRank; j++) {
1375                banks[i][j].actAllowedAt = ref_done_at;
1376            }
1377
1378            // at the moment this affects all ranks
1379            DPRINTF(DRAMPower, "%llu,REF,0,%d\n", divCeil(curTick(), tCK), i);
1380        }
1381
1382        // make sure we did not wait so long that we cannot make up
1383        // for it
1384        if (refreshDueAt + tREFI < ref_done_at) {
1385            fatal("Refresh was delayed so long we cannot catch up\n");
1386        }
1387
1388        // compensate for the delay in actually performing the refresh
1389        // when scheduling the next one
1390        schedule(refreshEvent, refreshDueAt + tREFI - tRP);
1391
1392        assert(!powerEvent.scheduled());
1393
1394        // move to the idle power state once the refresh is done, this
1395        // will also move the refresh state machine to the refresh
1396        // idle state
1397        schedulePowerEvent(PWR_IDLE, ref_done_at);
1398
1399        DPRINTF(DRAMState, "Refresh done at %llu and next refresh at %llu\n",
1400                ref_done_at, refreshDueAt + tREFI);
1401    }
1402}
1403
1404void
1405DRAMCtrl::schedulePowerEvent(PowerState pwr_state, Tick tick)
1406{
1407    // respect causality
1408    assert(tick >= curTick());
1409
1410    if (!powerEvent.scheduled()) {
1411        DPRINTF(DRAMState, "Scheduling power event at %llu to state %d\n",
1412                tick, pwr_state);
1413
1414        // insert the new transition
1415        pwrStateTrans = pwr_state;
1416
1417        schedule(powerEvent, tick);
1418    } else {
1419        panic("Scheduled power event at %llu to state %d, "
1420              "with scheduled event at %llu to %d\n", tick, pwr_state,
1421              powerEvent.when(), pwrStateTrans);
1422    }
1423}
1424
1425void
1426DRAMCtrl::processPowerEvent()
1427{
1428    // remember where we were, and for how long
1429    Tick duration = curTick() - pwrStateTick;
1430    PowerState prev_state = pwrState;
1431
1432    // update the accounting
1433    pwrStateTime[prev_state] += duration;
1434
1435    pwrState = pwrStateTrans;
1436    pwrStateTick = curTick();
1437
1438    if (pwrState == PWR_IDLE) {
1439        DPRINTF(DRAMState, "All banks precharged\n");
1440
1441        // if we were refreshing, make sure we start scheduling requests again
1442        if (prev_state == PWR_REF) {
1443            DPRINTF(DRAMState, "Was refreshing for %llu ticks\n", duration);
1444            assert(pwrState == PWR_IDLE);
1445
1446            // kick things into action again
1447            refreshState = REF_IDLE;
1448            assert(!nextReqEvent.scheduled());
1449            schedule(nextReqEvent, curTick());
1450        } else {
1451            assert(prev_state == PWR_ACT);
1452
1453            // if we have a pending refresh, and are now moving to
1454            // the idle state, direclty transition to a refresh
1455            if (refreshState == REF_RUN) {
1456                // there should be nothing waiting at this point
1457                assert(!powerEvent.scheduled());
1458
1459                // update the state in zero time and proceed below
1460                pwrState = PWR_REF;
1461            }
1462        }
1463    }
1464
1465    // we transition to the refresh state, let the refresh state
1466    // machine know of this state update and let it deal with the
1467    // scheduling of the next power state transition as well as the
1468    // following refresh
1469    if (pwrState == PWR_REF) {
1470        DPRINTF(DRAMState, "Refreshing\n");
1471        // kick the refresh event loop into action again, and that
1472        // in turn will schedule a transition to the idle power
1473        // state once the refresh is done
1474        assert(refreshState == REF_RUN);
1475        processRefreshEvent();
1476    }
1477}
1478
1479void
1480DRAMCtrl::regStats()
1481{
1482    using namespace Stats;
1483
1484    AbstractMemory::regStats();
1485
1486    readReqs
1487        .name(name() + ".readReqs")
1488        .desc("Number of read requests accepted");
1489
1490    writeReqs
1491        .name(name() + ".writeReqs")
1492        .desc("Number of write requests accepted");
1493
1494    readBursts
1495        .name(name() + ".readBursts")
1496        .desc("Number of DRAM read bursts, "
1497              "including those serviced by the write queue");
1498
1499    writeBursts
1500        .name(name() + ".writeBursts")
1501        .desc("Number of DRAM write bursts, "
1502              "including those merged in the write queue");
1503
1504    servicedByWrQ
1505        .name(name() + ".servicedByWrQ")
1506        .desc("Number of DRAM read bursts serviced by the write queue");
1507
1508    mergedWrBursts
1509        .name(name() + ".mergedWrBursts")
1510        .desc("Number of DRAM write bursts merged with an existing one");
1511
1512    neitherReadNorWrite
1513        .name(name() + ".neitherReadNorWriteReqs")
1514        .desc("Number of requests that are neither read nor write");
1515
1516    perBankRdBursts
1517        .init(banksPerRank * ranksPerChannel)
1518        .name(name() + ".perBankRdBursts")
1519        .desc("Per bank write bursts");
1520
1521    perBankWrBursts
1522        .init(banksPerRank * ranksPerChannel)
1523        .name(name() + ".perBankWrBursts")
1524        .desc("Per bank write bursts");
1525
1526    avgRdQLen
1527        .name(name() + ".avgRdQLen")
1528        .desc("Average read queue length when enqueuing")
1529        .precision(2);
1530
1531    avgWrQLen
1532        .name(name() + ".avgWrQLen")
1533        .desc("Average write queue length when enqueuing")
1534        .precision(2);
1535
1536    totQLat
1537        .name(name() + ".totQLat")
1538        .desc("Total ticks spent queuing");
1539
1540    totBusLat
1541        .name(name() + ".totBusLat")
1542        .desc("Total ticks spent in databus transfers");
1543
1544    totMemAccLat
1545        .name(name() + ".totMemAccLat")
1546        .desc("Total ticks spent from burst creation until serviced "
1547              "by the DRAM");
1548
1549    avgQLat
1550        .name(name() + ".avgQLat")
1551        .desc("Average queueing delay per DRAM burst")
1552        .precision(2);
1553
1554    avgQLat = totQLat / (readBursts - servicedByWrQ);
1555
1556    avgBusLat
1557        .name(name() + ".avgBusLat")
1558        .desc("Average bus latency per DRAM burst")
1559        .precision(2);
1560
1561    avgBusLat = totBusLat / (readBursts - servicedByWrQ);
1562
1563    avgMemAccLat
1564        .name(name() + ".avgMemAccLat")
1565        .desc("Average memory access latency per DRAM burst")
1566        .precision(2);
1567
1568    avgMemAccLat = totMemAccLat / (readBursts - servicedByWrQ);
1569
1570    numRdRetry
1571        .name(name() + ".numRdRetry")
1572        .desc("Number of times read queue was full causing retry");
1573
1574    numWrRetry
1575        .name(name() + ".numWrRetry")
1576        .desc("Number of times write queue was full causing retry");
1577
1578    readRowHits
1579        .name(name() + ".readRowHits")
1580        .desc("Number of row buffer hits during reads");
1581
1582    writeRowHits
1583        .name(name() + ".writeRowHits")
1584        .desc("Number of row buffer hits during writes");
1585
1586    readRowHitRate
1587        .name(name() + ".readRowHitRate")
1588        .desc("Row buffer hit rate for reads")
1589        .precision(2);
1590
1591    readRowHitRate = (readRowHits / (readBursts - servicedByWrQ)) * 100;
1592
1593    writeRowHitRate
1594        .name(name() + ".writeRowHitRate")
1595        .desc("Row buffer hit rate for writes")
1596        .precision(2);
1597
1598    writeRowHitRate = (writeRowHits / (writeBursts - mergedWrBursts)) * 100;
1599
1600    readPktSize
1601        .init(ceilLog2(burstSize) + 1)
1602        .name(name() + ".readPktSize")
1603        .desc("Read request sizes (log2)");
1604
1605     writePktSize
1606        .init(ceilLog2(burstSize) + 1)
1607        .name(name() + ".writePktSize")
1608        .desc("Write request sizes (log2)");
1609
1610     rdQLenPdf
1611        .init(readBufferSize)
1612        .name(name() + ".rdQLenPdf")
1613        .desc("What read queue length does an incoming req see");
1614
1615     wrQLenPdf
1616        .init(writeBufferSize)
1617        .name(name() + ".wrQLenPdf")
1618        .desc("What write queue length does an incoming req see");
1619
1620     bytesPerActivate
1621         .init(maxAccessesPerRow)
1622         .name(name() + ".bytesPerActivate")
1623         .desc("Bytes accessed per row activation")
1624         .flags(nozero);
1625
1626     rdPerTurnAround
1627         .init(readBufferSize)
1628         .name(name() + ".rdPerTurnAround")
1629         .desc("Reads before turning the bus around for writes")
1630         .flags(nozero);
1631
1632     wrPerTurnAround
1633         .init(writeBufferSize)
1634         .name(name() + ".wrPerTurnAround")
1635         .desc("Writes before turning the bus around for reads")
1636         .flags(nozero);
1637
1638    bytesReadDRAM
1639        .name(name() + ".bytesReadDRAM")
1640        .desc("Total number of bytes read from DRAM");
1641
1642    bytesReadWrQ
1643        .name(name() + ".bytesReadWrQ")
1644        .desc("Total number of bytes read from write queue");
1645
1646    bytesWritten
1647        .name(name() + ".bytesWritten")
1648        .desc("Total number of bytes written to DRAM");
1649
1650    bytesReadSys
1651        .name(name() + ".bytesReadSys")
1652        .desc("Total read bytes from the system interface side");
1653
1654    bytesWrittenSys
1655        .name(name() + ".bytesWrittenSys")
1656        .desc("Total written bytes from the system interface side");
1657
1658    avgRdBW
1659        .name(name() + ".avgRdBW")
1660        .desc("Average DRAM read bandwidth in MiByte/s")
1661        .precision(2);
1662
1663    avgRdBW = (bytesReadDRAM / 1000000) / simSeconds;
1664
1665    avgWrBW
1666        .name(name() + ".avgWrBW")
1667        .desc("Average achieved write bandwidth in MiByte/s")
1668        .precision(2);
1669
1670    avgWrBW = (bytesWritten / 1000000) / simSeconds;
1671
1672    avgRdBWSys
1673        .name(name() + ".avgRdBWSys")
1674        .desc("Average system read bandwidth in MiByte/s")
1675        .precision(2);
1676
1677    avgRdBWSys = (bytesReadSys / 1000000) / simSeconds;
1678
1679    avgWrBWSys
1680        .name(name() + ".avgWrBWSys")
1681        .desc("Average system write bandwidth in MiByte/s")
1682        .precision(2);
1683
1684    avgWrBWSys = (bytesWrittenSys / 1000000) / simSeconds;
1685
1686    peakBW
1687        .name(name() + ".peakBW")
1688        .desc("Theoretical peak bandwidth in MiByte/s")
1689        .precision(2);
1690
1691    peakBW = (SimClock::Frequency / tBURST) * burstSize / 1000000;
1692
1693    busUtil
1694        .name(name() + ".busUtil")
1695        .desc("Data bus utilization in percentage")
1696        .precision(2);
1697
1698    busUtil = (avgRdBW + avgWrBW) / peakBW * 100;
1699
1700    totGap
1701        .name(name() + ".totGap")
1702        .desc("Total gap between requests");
1703
1704    avgGap
1705        .name(name() + ".avgGap")
1706        .desc("Average gap between requests")
1707        .precision(2);
1708
1709    avgGap = totGap / (readReqs + writeReqs);
1710
1711    // Stats for DRAM Power calculation based on Micron datasheet
1712    busUtilRead
1713        .name(name() + ".busUtilRead")
1714        .desc("Data bus utilization in percentage for reads")
1715        .precision(2);
1716
1717    busUtilRead = avgRdBW / peakBW * 100;
1718
1719    busUtilWrite
1720        .name(name() + ".busUtilWrite")
1721        .desc("Data bus utilization in percentage for writes")
1722        .precision(2);
1723
1724    busUtilWrite = avgWrBW / peakBW * 100;
1725
1726    pageHitRate
1727        .name(name() + ".pageHitRate")
1728        .desc("Row buffer hit rate, read and write combined")
1729        .precision(2);
1730
1731    pageHitRate = (writeRowHits + readRowHits) /
1732        (writeBursts - mergedWrBursts + readBursts - servicedByWrQ) * 100;
1733
1734    pwrStateTime
1735        .init(5)
1736        .name(name() + ".memoryStateTime")
1737        .desc("Time in different power states");
1738    pwrStateTime.subname(0, "IDLE");
1739    pwrStateTime.subname(1, "REF");
1740    pwrStateTime.subname(2, "PRE_PDN");
1741    pwrStateTime.subname(3, "ACT");
1742    pwrStateTime.subname(4, "ACT_PDN");
1743}
1744
1745void
1746DRAMCtrl::recvFunctional(PacketPtr pkt)
1747{
1748    // rely on the abstract memory
1749    functionalAccess(pkt);
1750}
1751
1752BaseSlavePort&
1753DRAMCtrl::getSlavePort(const string &if_name, PortID idx)
1754{
1755    if (if_name != "port") {
1756        return MemObject::getSlavePort(if_name, idx);
1757    } else {
1758        return port;
1759    }
1760}
1761
1762unsigned int
1763DRAMCtrl::drain(DrainManager *dm)
1764{
1765    unsigned int count = port.drain(dm);
1766
1767    // if there is anything in any of our internal queues, keep track
1768    // of that as well
1769    if (!(writeQueue.empty() && readQueue.empty() &&
1770          respQueue.empty())) {
1771        DPRINTF(Drain, "DRAM controller not drained, write: %d, read: %d,"
1772                " resp: %d\n", writeQueue.size(), readQueue.size(),
1773                respQueue.size());
1774        ++count;
1775        drainManager = dm;
1776
1777        // the only part that is not drained automatically over time
1778        // is the write queue, thus kick things into action if needed
1779        if (!writeQueue.empty() && !nextReqEvent.scheduled()) {
1780            schedule(nextReqEvent, curTick());
1781        }
1782    }
1783
1784    if (count)
1785        setDrainState(Drainable::Draining);
1786    else
1787        setDrainState(Drainable::Drained);
1788    return count;
1789}
1790
1791DRAMCtrl::MemoryPort::MemoryPort(const std::string& name, DRAMCtrl& _memory)
1792    : QueuedSlavePort(name, &_memory, queue), queue(_memory, *this),
1793      memory(_memory)
1794{ }
1795
1796AddrRangeList
1797DRAMCtrl::MemoryPort::getAddrRanges() const
1798{
1799    AddrRangeList ranges;
1800    ranges.push_back(memory.getAddrRange());
1801    return ranges;
1802}
1803
1804void
1805DRAMCtrl::MemoryPort::recvFunctional(PacketPtr pkt)
1806{
1807    pkt->pushLabel(memory.name());
1808
1809    if (!queue.checkFunctional(pkt)) {
1810        // Default implementation of SimpleTimingPort::recvFunctional()
1811        // calls recvAtomic() and throws away the latency; we can save a
1812        // little here by just not calculating the latency.
1813        memory.recvFunctional(pkt);
1814    }
1815
1816    pkt->popLabel();
1817}
1818
1819Tick
1820DRAMCtrl::MemoryPort::recvAtomic(PacketPtr pkt)
1821{
1822    return memory.recvAtomic(pkt);
1823}
1824
1825bool
1826DRAMCtrl::MemoryPort::recvTimingReq(PacketPtr pkt)
1827{
1828    // pass it to the memory controller
1829    return memory.recvTimingReq(pkt);
1830}
1831
1832DRAMCtrl*
1833DRAMCtrlParams::create()
1834{
1835    return new DRAMCtrl(this);
1836}
1837