mem-cache: Fix setting prefetch bit
[gem5.git] / src / mem / mem_interface.hh
1 /*
2 * Copyright (c) 2012-2020 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
41 /**
42 * @file
43 * MemInterface declaration
44 */
45
46 #ifndef __MEM_INTERFACE_HH__
47 #define __MEM_INTERFACE_HH__
48
49 #include <deque>
50 #include <string>
51 #include <unordered_set>
52 #include <utility>
53 #include <vector>
54
55 #include "base/statistics.hh"
56 #include "enums/AddrMap.hh"
57 #include "enums/PageManage.hh"
58 #include "mem/abstract_mem.hh"
59 #include "mem/drampower.hh"
60 #include "mem/mem_ctrl.hh"
61 #include "params/DRAMInterface.hh"
62 #include "params/MemInterface.hh"
63 #include "params/NVMInterface.hh"
64 #include "sim/eventq.hh"
65
66 /**
67 * General interface to memory device
68 * Includes functions and parameters shared across media types
69 */
70 class MemInterface : public AbstractMemory
71 {
72 protected:
73 /**
74 * A basic class to track the bank state, i.e. what row is
75 * currently open (if any), when is the bank free to accept a new
76 * column (read/write) command, when can it be precharged, and
77 * when can it be activated.
78 *
79 * The bank also keeps track of how many bytes have been accessed
80 * in the open row since it was opened.
81 */
82 class Bank
83 {
84
85 public:
86 static const uint32_t NO_ROW = -1;
87
88 uint32_t openRow;
89 uint8_t bank;
90 uint8_t bankgr;
91
92 Tick rdAllowedAt;
93 Tick wrAllowedAt;
94 Tick preAllowedAt;
95 Tick actAllowedAt;
96
97 uint32_t rowAccesses;
98 uint32_t bytesAccessed;
99
100 Bank() :
101 openRow(NO_ROW), bank(0), bankgr(0),
102 rdAllowedAt(0), wrAllowedAt(0), preAllowedAt(0), actAllowedAt(0),
103 rowAccesses(0), bytesAccessed(0)
104 { }
105 };
106
107 /**
108 * A pointer to the parent MemCtrl instance
109 */
110 MemCtrl* ctrl;
111
112 /**
113 * Number of commands that can issue in the defined controller
114 * command window, used to verify command bandwidth
115 */
116 unsigned int maxCommandsPerWindow;
117
118 /**
119 * Memory controller configuration initialized based on parameter
120 * values.
121 */
122 Enums::AddrMap addrMapping;
123
124 /**
125 * General device and channel characteristics
126 * The rowsPerBank is determined based on the capacity, number of
127 * ranks and banks, the burst size, and the row buffer size.
128 */
129 const uint32_t burstSize;
130 const uint32_t deviceSize;
131 const uint32_t deviceRowBufferSize;
132 const uint32_t devicesPerRank;
133 const uint32_t rowBufferSize;
134 const uint32_t burstsPerRowBuffer;
135 const uint32_t burstsPerStripe;
136 const uint32_t ranksPerChannel;
137 const uint32_t banksPerRank;
138 uint32_t rowsPerBank;
139
140 /**
141 * General timing requirements
142 */
143 M5_CLASS_VAR_USED const Tick tCK;
144 const Tick tCS;
145 const Tick tBURST;
146 const Tick tRTW;
147 const Tick tWTR;
148
149 /*
150 * @return delay between write and read commands
151 */
152 virtual Tick writeToReadDelay() const { return tBURST + tWTR; }
153
154 /*
155 * @return delay between write and read commands
156 */
157 Tick readToWriteDelay() const { return tBURST + tRTW; }
158
159 /*
160 * @return delay between accesses to different ranks
161 */
162 Tick rankToRankDelay() const { return tBURST + tCS; }
163
164
165 public:
166
167 /**
168 * Buffer sizes for read and write queues in the controller
169 * These are passed to the controller on instantiation
170 * Defining them here allows for buffers to be resized based
171 * on memory type / configuration.
172 */
173 const uint32_t readBufferSize;
174 const uint32_t writeBufferSize;
175
176 /** Set a pointer to the controller and initialize
177 * interface based on controller parameters
178 * @param _ctrl pointer to the parent controller
179 * @param command_window size of command window used to
180 * check command bandwidth
181 */
182 void setCtrl(MemCtrl* _ctrl, unsigned int command_window);
183
184 /**
185 * Get an address in a dense range which starts from 0. The input
186 * address is the physical address of the request in an address
187 * space that contains other SimObjects apart from this
188 * controller.
189 *
190 * @param addr The intput address which should be in the addrRange
191 * @return An address in the continues range [0, max)
192 */
193 Addr getCtrlAddr(Addr addr) { return range.getOffset(addr); }
194
195 /**
196 * Setup the rank based on packet received
197 *
198 * @param integer value of rank to be setup. used to index ranks vector
199 * @param are we setting up rank for read or write packet?
200 */
201 virtual void setupRank(const uint8_t rank, const bool is_read) = 0;
202
203 /**
204 * Check drain state of interface
205 *
206 * @return true if all ranks are drained and idle
207 *
208 */
209 virtual bool allRanksDrained() const = 0;
210
211 /**
212 * For FR-FCFS policy, find first command that can issue
213 * Function will be overriden by interface to select based
214 * on media characteristics, used to determine when read
215 * or write can issue.
216 *
217 * @param queue Queued requests to consider
218 * @param min_col_at Minimum tick for 'seamless' issue
219 * @return an iterator to the selected packet, else queue.end()
220 * @return the tick when the packet selected will issue
221 */
222 virtual std::pair<MemPacketQueue::iterator, Tick>
223 chooseNextFRFCFS(MemPacketQueue& queue, Tick min_col_at) const = 0;
224
225 /*
226 * Function to calulate unloaded latency
227 */
228 virtual Tick accessLatency() const = 0;
229
230 /**
231 * @return number of bytes in a burst for this interface
232 */
233 uint32_t bytesPerBurst() const { return burstSize; }
234
235 /*
236 * @return time to offset next command
237 */
238 virtual Tick commandOffset() const = 0;
239
240 /**
241 * Check if a burst operation can be issued to the interface
242 *
243 * @param Return true if RD/WR can issue
244 */
245 virtual bool burstReady(MemPacket* pkt) const = 0;
246
247 /**
248 * Determine the required delay for an access to a different rank
249 *
250 * @return required rank to rank delay
251 */
252 Tick rankDelay() const { return tCS; }
253
254 /**
255 *
256 * @return minimum additional bus turnaround required for read-to-write
257 */
258 Tick minReadToWriteDataGap() const { return std::min(tRTW, tCS); }
259
260 /**
261 *
262 * @return minimum additional bus turnaround required for write-to-read
263 */
264 Tick minWriteToReadDataGap() const { return std::min(tWTR, tCS); }
265
266 /**
267 * Address decoder to figure out physical mapping onto ranks,
268 * banks, and rows. This function is called multiple times on the same
269 * system packet if the pakcet is larger than burst of the memory. The
270 * pkt_addr is used for the offset within the packet.
271 *
272 * @param pkt The packet from the outside world
273 * @param pkt_addr The starting address of the packet
274 * @param size The size of the packet in bytes
275 * @param is_read Is the request for a read or a write to memory
276 * @param is_dram Is the request to a DRAM interface
277 * @return A MemPacket pointer with the decoded information
278 */
279 MemPacket* decodePacket(const PacketPtr pkt, Addr pkt_addr,
280 unsigned int size, bool is_read, bool is_dram);
281
282 /**
283 * Add rank to rank delay to bus timing to all banks in all ranks
284 * when access to an alternate interface is issued
285 *
286 * param cmd_at Time of current command used as starting point for
287 * addition of rank-to-rank delay
288 */
289 virtual void addRankToRankDelay(Tick cmd_at) = 0;
290
291 typedef MemInterfaceParams Params;
292 MemInterface(const Params &_p);
293 };
294
295 /**
296 * Interface to DRAM devices with media specific parameters,
297 * statistics, and functions.
298 * The DRAMInterface includes a class for individual ranks
299 * and per rank functions.
300 */
301 class DRAMInterface : public MemInterface
302 {
303 private:
304 /**
305 * Simple structure to hold the values needed to keep track of
306 * commands for DRAMPower
307 */
308 struct Command
309 {
310 Data::MemCommand::cmds type;
311 uint8_t bank;
312 Tick timeStamp;
313
314 constexpr Command(Data::MemCommand::cmds _type, uint8_t _bank,
315 Tick time_stamp)
316 : type(_type), bank(_bank), timeStamp(time_stamp)
317 { }
318 };
319
320 /**
321 * The power state captures the different operational states of
322 * the DRAM and interacts with the bus read/write state machine,
323 * and the refresh state machine.
324 *
325 * PWR_IDLE : The idle state in which all banks are closed
326 * From here can transition to: PWR_REF, PWR_ACT,
327 * PWR_PRE_PDN
328 *
329 * PWR_REF : Auto-refresh state. Will transition when refresh is
330 * complete based on power state prior to PWR_REF
331 * From here can transition to: PWR_IDLE, PWR_PRE_PDN,
332 * PWR_SREF
333 *
334 * PWR_SREF : Self-refresh state. Entered after refresh if
335 * previous state was PWR_PRE_PDN
336 * From here can transition to: PWR_IDLE
337 *
338 * PWR_PRE_PDN : Precharge power down state
339 * From here can transition to: PWR_REF, PWR_IDLE
340 *
341 * PWR_ACT : Activate state in which one or more banks are open
342 * From here can transition to: PWR_IDLE, PWR_ACT_PDN
343 *
344 * PWR_ACT_PDN : Activate power down state
345 * From here can transition to: PWR_ACT
346 */
347 enum PowerState
348 {
349 PWR_IDLE = 0,
350 PWR_REF,
351 PWR_SREF,
352 PWR_PRE_PDN,
353 PWR_ACT,
354 PWR_ACT_PDN
355 };
356
357 /**
358 * The refresh state is used to control the progress of the
359 * refresh scheduling. When normal operation is in progress the
360 * refresh state is idle. Once tREFI has elasped, a refresh event
361 * is triggered to start the following STM transitions which are
362 * used to issue a refresh and return back to normal operation
363 *
364 * REF_IDLE : IDLE state used during normal operation
365 * From here can transition to: REF_DRAIN
366 *
367 * REF_SREF_EXIT : Exiting a self-refresh; refresh event scheduled
368 * after self-refresh exit completes
369 * From here can transition to: REF_DRAIN
370 *
371 * REF_DRAIN : Drain state in which on going accesses complete.
372 * From here can transition to: REF_PD_EXIT
373 *
374 * REF_PD_EXIT : Evaluate pwrState and issue wakeup if needed
375 * Next state dependent on whether banks are open
376 * From here can transition to: REF_PRE, REF_START
377 *
378 * REF_PRE : Close (precharge) all open banks
379 * From here can transition to: REF_START
380 *
381 * REF_START : Issue refresh command and update DRAMPower stats
382 * From here can transition to: REF_RUN
383 *
384 * REF_RUN : Refresh running, waiting for tRFC to expire
385 * From here can transition to: REF_IDLE, REF_SREF_EXIT
386 */
387 enum RefreshState
388 {
389 REF_IDLE = 0,
390 REF_DRAIN,
391 REF_PD_EXIT,
392 REF_SREF_EXIT,
393 REF_PRE,
394 REF_START,
395 REF_RUN
396 };
397
398 class Rank;
399 struct RankStats : public Stats::Group
400 {
401 RankStats(DRAMInterface &dram, Rank &rank);
402
403 void regStats() override;
404 void resetStats() override;
405 void preDumpStats() override;
406
407 Rank &rank;
408
409 /*
410 * Command energies
411 */
412 Stats::Scalar actEnergy;
413 Stats::Scalar preEnergy;
414 Stats::Scalar readEnergy;
415 Stats::Scalar writeEnergy;
416 Stats::Scalar refreshEnergy;
417
418 /*
419 * Active Background Energy
420 */
421 Stats::Scalar actBackEnergy;
422
423 /*
424 * Precharge Background Energy
425 */
426 Stats::Scalar preBackEnergy;
427
428 /*
429 * Active Power-Down Energy
430 */
431 Stats::Scalar actPowerDownEnergy;
432
433 /*
434 * Precharge Power-Down Energy
435 */
436 Stats::Scalar prePowerDownEnergy;
437
438 /*
439 * self Refresh Energy
440 */
441 Stats::Scalar selfRefreshEnergy;
442
443 Stats::Scalar totalEnergy;
444 Stats::Scalar averagePower;
445
446 /**
447 * Stat to track total DRAM idle time
448 *
449 */
450 Stats::Scalar totalIdleTime;
451
452 /**
453 * Track time spent in each power state.
454 */
455 Stats::Vector pwrStateTime;
456 };
457
458 /**
459 * Rank class includes a vector of banks. Refresh and Power state
460 * machines are defined per rank. Events required to change the
461 * state of the refresh and power state machine are scheduled per
462 * rank. This class allows the implementation of rank-wise refresh
463 * and rank-wise power-down.
464 */
465 class Rank : public EventManager
466 {
467 private:
468
469 /**
470 * A reference to the parent DRAMInterface instance
471 */
472 DRAMInterface& dram;
473
474 /**
475 * Since we are taking decisions out of order, we need to keep
476 * track of what power transition is happening at what time
477 */
478 PowerState pwrStateTrans;
479
480 /**
481 * Previous low-power state, which will be re-entered after refresh.
482 */
483 PowerState pwrStatePostRefresh;
484
485 /**
486 * Track when we transitioned to the current power state
487 */
488 Tick pwrStateTick;
489
490 /**
491 * Keep track of when a refresh is due.
492 */
493 Tick refreshDueAt;
494
495 /**
496 * Function to update Power Stats
497 */
498 void updatePowerStats();
499
500 /**
501 * Schedule a power state transition in the future, and
502 * potentially override an already scheduled transition.
503 *
504 * @param pwr_state Power state to transition to
505 * @param tick Tick when transition should take place
506 */
507 void schedulePowerEvent(PowerState pwr_state, Tick tick);
508
509 public:
510
511 /**
512 * Current power state.
513 */
514 PowerState pwrState;
515
516 /**
517 * current refresh state
518 */
519 RefreshState refreshState;
520
521 /**
522 * rank is in or transitioning to power-down or self-refresh
523 */
524 bool inLowPowerState;
525
526 /**
527 * Current Rank index
528 */
529 uint8_t rank;
530
531 /**
532 * Track number of packets in read queue going to this rank
533 */
534 uint32_t readEntries;
535
536 /**
537 * Track number of packets in write queue going to this rank
538 */
539 uint32_t writeEntries;
540
541 /**
542 * Number of ACT, RD, and WR events currently scheduled
543 * Incremented when a refresh event is started as well
544 * Used to determine when a low-power state can be entered
545 */
546 uint8_t outstandingEvents;
547
548 /**
549 * delay low-power exit until this requirement is met
550 */
551 Tick wakeUpAllowedAt;
552
553 /**
554 * One DRAMPower instance per rank
555 */
556 DRAMPower power;
557
558 /**
559 * List of commands issued, to be sent to DRAMPpower at refresh
560 * and stats dump. Keep commands here since commands to different
561 * banks are added out of order. Will only pass commands up to
562 * curTick() to DRAMPower after sorting.
563 */
564 std::vector<Command> cmdList;
565
566 /**
567 * Vector of Banks. Each rank is made of several devices which in
568 * term are made from several banks.
569 */
570 std::vector<Bank> banks;
571
572 /**
573 * To track number of banks which are currently active for
574 * this rank.
575 */
576 unsigned int numBanksActive;
577
578 /** List to keep track of activate ticks */
579 std::deque<Tick> actTicks;
580
581 /**
582 * Track when we issued the last read/write burst
583 */
584 Tick lastBurstTick;
585
586 Rank(const DRAMInterfaceParams &_p, int _rank,
587 DRAMInterface& _dram);
588
589 const std::string name() const { return csprintf("%d", rank); }
590
591 /**
592 * Kick off accounting for power and refresh states and
593 * schedule initial refresh.
594 *
595 * @param ref_tick Tick for first refresh
596 */
597 void startup(Tick ref_tick);
598
599 /**
600 * Stop the refresh events.
601 */
602 void suspend();
603
604 /**
605 * Check if there is no refresh and no preparation of refresh ongoing
606 * i.e. the refresh state machine is in idle
607 *
608 * @param Return true if the rank is idle from a refresh point of view
609 */
610 bool inRefIdleState() const { return refreshState == REF_IDLE; }
611
612 /**
613 * Check if the current rank has all banks closed and is not
614 * in a low power state
615 *
616 * @param Return true if the rank is idle from a bank
617 * and power point of view
618 */
619 bool inPwrIdleState() const { return pwrState == PWR_IDLE; }
620
621 /**
622 * Trigger a self-refresh exit if there are entries enqueued
623 * Exit if there are any read entries regardless of the bus state.
624 * If we are currently issuing write commands, exit if we have any
625 * write commands enqueued as well.
626 * Could expand this in the future to analyze state of entire queue
627 * if needed.
628 *
629 * @return boolean indicating self-refresh exit should be scheduled
630 */
631 bool forceSelfRefreshExit() const;
632
633 /**
634 * Check if the command queue of current rank is idle
635 *
636 * @param Return true if the there are no commands in Q.
637 * Bus direction determines queue checked.
638 */
639 bool isQueueEmpty() const;
640
641 /**
642 * Let the rank check if it was waiting for requests to drain
643 * to allow it to transition states.
644 */
645 void checkDrainDone();
646
647 /**
648 * Push command out of cmdList queue that are scheduled at
649 * or before curTick() to DRAMPower library
650 * All commands before curTick are guaranteed to be complete
651 * and can safely be flushed.
652 */
653 void flushCmdList();
654
655 /**
656 * Computes stats just prior to dump event
657 */
658 void computeStats();
659
660 /**
661 * Reset stats on a stats event
662 */
663 void resetStats();
664
665 /**
666 * Schedule a transition to power-down (sleep)
667 *
668 * @param pwr_state Power state to transition to
669 * @param tick Absolute tick when transition should take place
670 */
671 void powerDownSleep(PowerState pwr_state, Tick tick);
672
673 /**
674 * schedule and event to wake-up from power-down or self-refresh
675 * and update bank timing parameters
676 *
677 * @param exit_delay Relative tick defining the delay required between
678 * low-power exit and the next command
679 */
680 void scheduleWakeUpEvent(Tick exit_delay);
681
682 void processWriteDoneEvent();
683 EventFunctionWrapper writeDoneEvent;
684
685 void processActivateEvent();
686 EventFunctionWrapper activateEvent;
687
688 void processPrechargeEvent();
689 EventFunctionWrapper prechargeEvent;
690
691 void processRefreshEvent();
692 EventFunctionWrapper refreshEvent;
693
694 void processPowerEvent();
695 EventFunctionWrapper powerEvent;
696
697 void processWakeUpEvent();
698 EventFunctionWrapper wakeUpEvent;
699
700 protected:
701 RankStats stats;
702 };
703
704 /**
705 * Function for sorting Command structures based on timeStamp
706 *
707 * @param a Memory Command
708 * @param next Memory Command
709 * @return true if timeStamp of Command 1 < timeStamp of Command 2
710 */
711 static bool
712 sortTime(const Command& cmd, const Command& cmd_next)
713 {
714 return cmd.timeStamp < cmd_next.timeStamp;
715 }
716
717 /**
718 * DRAM specific device characteristics
719 */
720 const uint32_t bankGroupsPerRank;
721 const bool bankGroupArch;
722
723 /**
724 * DRAM specific timing requirements
725 */
726 const Tick tCL;
727 const Tick tBURST_MIN;
728 const Tick tBURST_MAX;
729 const Tick tCCD_L_WR;
730 const Tick tCCD_L;
731 const Tick tRCD;
732 const Tick tRP;
733 const Tick tRAS;
734 const Tick tWR;
735 const Tick tRTP;
736 const Tick tRFC;
737 const Tick tREFI;
738 const Tick tRRD;
739 const Tick tRRD_L;
740 const Tick tPPD;
741 const Tick tAAD;
742 const Tick tXAW;
743 const Tick tXP;
744 const Tick tXS;
745 const Tick clkResyncDelay;
746 const bool dataClockSync;
747 const bool burstInterleave;
748 const uint8_t twoCycleActivate;
749 const uint32_t activationLimit;
750 const Tick wrToRdDlySameBG;
751 const Tick rdToWrDlySameBG;
752
753
754 Enums::PageManage pageMgmt;
755 /**
756 * Max column accesses (read and write) per row, before forefully
757 * closing it.
758 */
759 const uint32_t maxAccessesPerRow;
760
761 // timestamp offset
762 uint64_t timeStampOffset;
763
764 // Holds the value of the DRAM rank of burst issued
765 uint8_t activeRank;
766
767 /** Enable or disable DRAM powerdown states. */
768 bool enableDRAMPowerdown;
769
770 /** The time when stats were last reset used to calculate average power */
771 Tick lastStatsResetTick;
772
773 /**
774 * Keep track of when row activations happen, in order to enforce
775 * the maximum number of activations in the activation window. The
776 * method updates the time that the banks become available based
777 * on the current limits.
778 *
779 * @param rank_ref Reference to the rank
780 * @param bank_ref Reference to the bank
781 * @param act_tick Time when the activation takes place
782 * @param row Index of the row
783 */
784 void activateBank(Rank& rank_ref, Bank& bank_ref, Tick act_tick,
785 uint32_t row);
786
787 /**
788 * Precharge a given bank and also update when the precharge is
789 * done. This will also deal with any stats related to the
790 * accesses to the open page.
791 *
792 * @param rank_ref The rank to precharge
793 * @param bank_ref The bank to precharge
794 * @param pre_tick Time when the precharge takes place
795 * @param auto_or_preall Is this an auto-precharge or precharge all command
796 * @param trace Is this an auto precharge then do not add to trace
797 */
798 void prechargeBank(Rank& rank_ref, Bank& bank_ref,
799 Tick pre_tick, bool auto_or_preall = false,
800 bool trace = true);
801
802 struct DRAMStats : public Stats::Group
803 {
804 DRAMStats(DRAMInterface &dram);
805
806 void regStats() override;
807 void resetStats() override;
808
809 DRAMInterface &dram;
810
811 /** total number of DRAM bursts serviced */
812 Stats::Scalar readBursts;
813 Stats::Scalar writeBursts;
814
815 /** DRAM per bank stats */
816 Stats::Vector perBankRdBursts;
817 Stats::Vector perBankWrBursts;
818
819 // Latencies summed over all requests
820 Stats::Scalar totQLat;
821 Stats::Scalar totBusLat;
822 Stats::Scalar totMemAccLat;
823
824 // Average latencies per request
825 Stats::Formula avgQLat;
826 Stats::Formula avgBusLat;
827 Stats::Formula avgMemAccLat;
828
829 // Row hit count and rate
830 Stats::Scalar readRowHits;
831 Stats::Scalar writeRowHits;
832 Stats::Formula readRowHitRate;
833 Stats::Formula writeRowHitRate;
834 Stats::Histogram bytesPerActivate;
835 // Number of bytes transferred to/from DRAM
836 Stats::Scalar bytesRead;
837 Stats::Scalar bytesWritten;
838
839 // Average bandwidth
840 Stats::Formula avgRdBW;
841 Stats::Formula avgWrBW;
842 Stats::Formula peakBW;
843 // bus utilization
844 Stats::Formula busUtil;
845 Stats::Formula busUtilRead;
846 Stats::Formula busUtilWrite;
847 Stats::Formula pageHitRate;
848 };
849
850 DRAMStats stats;
851
852 /**
853 * Vector of dram ranks
854 */
855 std::vector<Rank*> ranks;
856
857 /*
858 * @return delay between write and read commands
859 */
860 Tick writeToReadDelay() const override { return tBURST + tWTR + tCL; }
861
862 /**
863 * Find which are the earliest banks ready to issue an activate
864 * for the enqueued requests. Assumes maximum of 32 banks per rank
865 * Also checks if the bank is already prepped.
866 *
867 * @param queue Queued requests to consider
868 * @param min_col_at time of seamless burst command
869 * @return One-hot encoded mask of bank indices
870 * @return boolean indicating burst can issue seamlessly, with no gaps
871 */
872 std::pair<std::vector<uint32_t>, bool>
873 minBankPrep(const MemPacketQueue& queue, Tick min_col_at) const;
874
875 /*
876 * @return time to send a burst of data without gaps
877 */
878 Tick
879 burstDelay() const
880 {
881 return (burstInterleave ? tBURST_MAX / 2 : tBURST);
882 }
883
884 public:
885 /**
886 * Initialize the DRAM interface and verify parameters
887 */
888 void init() override;
889
890 /**
891 * Iterate through dram ranks and instantiate per rank startup routine
892 */
893 void startup() override;
894
895 /**
896 * Setup the rank based on packet received
897 *
898 * @param integer value of rank to be setup. used to index ranks vector
899 * @param are we setting up rank for read or write packet?
900 */
901 void setupRank(const uint8_t rank, const bool is_read) override;
902
903 /**
904 * Iterate through dram ranks to exit self-refresh in order to drain
905 */
906 void drainRanks();
907
908 /**
909 * Return true once refresh is complete for all ranks and there are no
910 * additional commands enqueued. (only evaluated when draining)
911 * This will ensure that all banks are closed, power state is IDLE, and
912 * power stats have been updated
913 *
914 * @return true if all ranks have refreshed, with no commands enqueued
915 *
916 */
917 bool allRanksDrained() const override;
918
919 /**
920 * Iterate through DRAM ranks and suspend them
921 */
922 void suspend();
923
924 /*
925 * @return time to offset next command
926 */
927 Tick commandOffset() const override { return (tRP + tRCD); }
928
929 /*
930 * Function to calulate unloaded, closed bank access latency
931 */
932 Tick accessLatency() const override { return (tRP + tRCD + tCL); }
933
934 /**
935 * For FR-FCFS policy, find first DRAM command that can issue
936 *
937 * @param queue Queued requests to consider
938 * @param min_col_at Minimum tick for 'seamless' issue
939 * @return an iterator to the selected packet, else queue.end()
940 * @return the tick when the packet selected will issue
941 */
942 std::pair<MemPacketQueue::iterator, Tick>
943 chooseNextFRFCFS(MemPacketQueue& queue, Tick min_col_at) const override;
944
945 /**
946 * Actually do the burst - figure out the latency it
947 * will take to service the req based on bank state, channel state etc
948 * and then update those states to account for this request. Based
949 * on this, update the packet's "readyTime" and move it to the
950 * response q from where it will eventually go back to the outside
951 * world.
952 *
953 * @param mem_pkt The packet created from the outside world pkt
954 * @param next_burst_at Minimum bus timing requirement from controller
955 * @param queue Reference to the read or write queue with the packet
956 * @return pair, tick when current burst is issued and
957 * tick when next burst can issue
958 */
959 std::pair<Tick, Tick>
960 doBurstAccess(MemPacket* mem_pkt, Tick next_burst_at,
961 const std::vector<MemPacketQueue>& queue);
962
963 /**
964 * Check if a burst operation can be issued to the DRAM
965 *
966 * @param Return true if RD/WR can issue
967 * This requires the DRAM to be in the
968 * REF IDLE state
969 */
970 bool
971 burstReady(MemPacket* pkt) const override
972 {
973 return ranks[pkt->rank]->inRefIdleState();
974 }
975
976 /**
977 * This function checks if ranks are actively refreshing and
978 * therefore busy. The function also checks if ranks are in
979 * the self-refresh state, in which case, a self-refresh exit
980 * is initiated.
981 *
982 * return boolean if all ranks are in refresh and therefore busy
983 */
984 bool isBusy();
985
986 /**
987 * Add rank to rank delay to bus timing to all DRAM banks in alli ranks
988 * when access to an alternate interface is issued
989 *
990 * param cmd_at Time of current command used as starting point for
991 * addition of rank-to-rank delay
992 */
993 void addRankToRankDelay(Tick cmd_at) override;
994
995 /**
996 * Complete response process for DRAM when read burst is complete
997 * This will update the counters and check if a power down state
998 * can be entered.
999 *
1000 * @param rank Specifies rank associated with read burst
1001 */
1002 void respondEvent(uint8_t rank);
1003
1004 /**
1005 * Check the refresh state to determine if refresh needs
1006 * to be kicked back into action after a read response
1007 *
1008 * @param rank Specifies rank associated with read burst
1009 */
1010 void checkRefreshState(uint8_t rank);
1011
1012 DRAMInterface(const DRAMInterfaceParams &_p);
1013 };
1014
1015 /**
1016 * Interface to NVM devices with media specific parameters,
1017 * statistics, and functions.
1018 * The NVMInterface includes a class for individual ranks
1019 * and per rank functions.
1020 */
1021 class NVMInterface : public MemInterface
1022 {
1023 private:
1024 /**
1025 * NVM rank class simply includes a vector of banks.
1026 */
1027 class Rank : public EventManager
1028 {
1029 public:
1030
1031 /**
1032 * Current Rank index
1033 */
1034 uint8_t rank;
1035
1036 /**
1037 * Vector of NVM banks. Each rank is made of several banks
1038 * that can be accessed in parallel.
1039 */
1040 std::vector<Bank> banks;
1041
1042 Rank(const NVMInterfaceParams &_p, int _rank,
1043 NVMInterface& _nvm);
1044 };
1045
1046 /**
1047 * NVM specific device and channel characteristics
1048 */
1049 const uint32_t maxPendingWrites;
1050 const uint32_t maxPendingReads;
1051 const bool twoCycleRdWr;
1052
1053 /**
1054 * NVM specific timing requirements
1055 */
1056 const Tick tREAD;
1057 const Tick tWRITE;
1058 const Tick tSEND;
1059
1060 struct NVMStats : public Stats::Group
1061 {
1062 NVMStats(NVMInterface &nvm);
1063
1064 void regStats() override;
1065
1066 NVMInterface &nvm;
1067
1068 /** NVM stats */
1069 Stats::Scalar readBursts;
1070 Stats::Scalar writeBursts;
1071
1072 Stats::Vector perBankRdBursts;
1073 Stats::Vector perBankWrBursts;
1074
1075 // Latencies summed over all requests
1076 Stats::Scalar totQLat;
1077 Stats::Scalar totBusLat;
1078 Stats::Scalar totMemAccLat;
1079
1080 // Average latencies per request
1081 Stats::Formula avgQLat;
1082 Stats::Formula avgBusLat;
1083 Stats::Formula avgMemAccLat;
1084
1085 Stats::Scalar bytesRead;
1086 Stats::Scalar bytesWritten;
1087
1088 // Average bandwidth
1089 Stats::Formula avgRdBW;
1090 Stats::Formula avgWrBW;
1091 Stats::Formula peakBW;
1092 Stats::Formula busUtil;
1093 Stats::Formula busUtilRead;
1094 Stats::Formula busUtilWrite;
1095
1096 /** NVM stats */
1097 Stats::Histogram pendingReads;
1098 Stats::Histogram pendingWrites;
1099 Stats::Histogram bytesPerBank;
1100 };
1101 NVMStats stats;
1102
1103 void processWriteRespondEvent();
1104 EventFunctionWrapper writeRespondEvent;
1105
1106 void processReadReadyEvent();
1107 EventFunctionWrapper readReadyEvent;
1108
1109 /**
1110 * Vector of nvm ranks
1111 */
1112 std::vector<Rank*> ranks;
1113
1114 /**
1115 * Holding queue for non-deterministic write commands, which
1116 * maintains writes that have been issued but have not completed
1117 * Stored seperately mostly to keep the code clean and help with
1118 * events scheduling.
1119 * This mimics a buffer on the media controller and therefore is
1120 * not added to the main write queue for sizing
1121 */
1122 std::list<Tick> writeRespQueue;
1123
1124 std::deque<Tick> readReadyQueue;
1125
1126 /**
1127 * Check if the write response queue is empty
1128 *
1129 * @param Return true if empty
1130 */
1131 bool writeRespQueueEmpty() const { return writeRespQueue.empty(); }
1132
1133 /**
1134 * Till when must we wait before issuing next read command?
1135 */
1136 Tick nextReadAt;
1137
1138 // keep track of reads that have issued for which data is either
1139 // not yet ready or has not yet been transferred to the ctrl
1140 uint16_t numPendingReads;
1141 uint16_t numReadDataReady;
1142
1143 public:
1144 // keep track of the number of reads that have yet to be issued
1145 uint16_t numReadsToIssue;
1146
1147 // number of writes in the writeQueue for the NVM interface
1148 uint32_t numWritesQueued;
1149
1150 /**
1151 * Initialize the NVM interface and verify parameters
1152 */
1153 void init() override;
1154
1155 /**
1156 * Setup the rank based on packet received
1157 *
1158 * @param integer value of rank to be setup. used to index ranks vector
1159 * @param are we setting up rank for read or write packet?
1160 */
1161 void setupRank(const uint8_t rank, const bool is_read) override;
1162
1163 /**
1164 * Check drain state of NVM interface
1165 *
1166 * @return true if write response queue is empty
1167 *
1168 */
1169 bool allRanksDrained() const override { return writeRespQueueEmpty(); }
1170
1171 /*
1172 * @return time to offset next command
1173 */
1174 Tick commandOffset() const override { return tBURST; }
1175
1176 /**
1177 * Check if a burst operation can be issued to the NVM
1178 *
1179 * @param Return true if RD/WR can issue
1180 * for reads, also verfy that ready count
1181 * has been updated to a non-zero value to
1182 * account for race conditions between events
1183 */
1184 bool burstReady(MemPacket* pkt) const override;
1185
1186 /**
1187 * This function checks if ranks are busy.
1188 * This state is true when either:
1189 * 1) There is no command with read data ready to transmit or
1190 * 2) The NVM inteface has reached the maximum number of outstanding
1191 * writes commands.
1192 * @param read_queue_empty There are no read queued
1193 * @param all_writes_nvm All writes in queue are for NVM interface
1194 * @return true of NVM is busy
1195 *
1196 */
1197 bool isBusy(bool read_queue_empty, bool all_writes_nvm);
1198 /**
1199 * For FR-FCFS policy, find first NVM command that can issue
1200 * default to first command to prepped region
1201 *
1202 * @param queue Queued requests to consider
1203 * @param min_col_at Minimum tick for 'seamless' issue
1204 * @return an iterator to the selected packet, else queue.end()
1205 * @return the tick when the packet selected will issue
1206 */
1207 std::pair<MemPacketQueue::iterator, Tick>
1208 chooseNextFRFCFS(MemPacketQueue& queue, Tick min_col_at) const override;
1209
1210 /**
1211 * Add rank to rank delay to bus timing to all NVM banks in alli ranks
1212 * when access to an alternate interface is issued
1213 *
1214 * param cmd_at Time of current command used as starting point for
1215 * addition of rank-to-rank delay
1216 */
1217 void addRankToRankDelay(Tick cmd_at) override;
1218
1219
1220 /**
1221 * Select read command to issue asynchronously
1222 */
1223 void chooseRead(MemPacketQueue& queue);
1224
1225 /*
1226 * Function to calulate unloaded access latency
1227 */
1228 Tick accessLatency() const override { return (tREAD + tSEND); }
1229
1230 /**
1231 * Check if the write response queue has reached defined threshold
1232 *
1233 * @param Return true if full
1234 */
1235 bool
1236 writeRespQueueFull() const
1237 {
1238 return writeRespQueue.size() == maxPendingWrites;
1239 }
1240
1241 bool
1242 readsWaitingToIssue() const
1243 {
1244 return ((numReadsToIssue != 0) &&
1245 (numPendingReads < maxPendingReads));
1246 }
1247
1248 /**
1249 * Actually do the burst and update stats.
1250 *
1251 * @param pkt The packet created from the outside world pkt
1252 * @param next_burst_at Minimum bus timing requirement from controller
1253 * @return pair, tick when current burst is issued and
1254 * tick when next burst can issue
1255 */
1256 std::pair<Tick, Tick>
1257 doBurstAccess(MemPacket* pkt, Tick next_burst_at);
1258
1259 NVMInterface(const NVMInterfaceParams &_p);
1260 };
1261
1262 #endif //__MEM_INTERFACE_HH__