mem: Change warmupCycle stat to warmupTick
[gem5.git] / src / mem / mem_ctrl.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 * MemCtrl declaration
44 */
45
46 #ifndef __MEM_CTRL_HH__
47 #define __MEM_CTRL_HH__
48
49 #include <deque>
50 #include <string>
51 #include <unordered_set>
52 #include <utility>
53 #include <vector>
54
55 #include "base/callback.hh"
56 #include "base/statistics.hh"
57 #include "enums/MemSched.hh"
58 #include "mem/qos/mem_ctrl.hh"
59 #include "mem/qport.hh"
60 #include "params/MemCtrl.hh"
61 #include "sim/eventq.hh"
62
63 class DRAMInterface;
64 class NVMInterface;
65
66 /**
67 * A burst helper helps organize and manage a packet that is larger than
68 * the memory burst size. A system packet that is larger than the burst size
69 * is split into multiple packets and all those packets point to
70 * a single burst helper such that we know when the whole packet is served.
71 */
72 class BurstHelper
73 {
74 public:
75
76 /** Number of bursts requred for a system packet **/
77 const unsigned int burstCount;
78
79 /** Number of bursts serviced so far for a system packet **/
80 unsigned int burstsServiced;
81
82 BurstHelper(unsigned int _burstCount)
83 : burstCount(_burstCount), burstsServiced(0)
84 { }
85 };
86
87 /**
88 * A memory packet stores packets along with the timestamp of when
89 * the packet entered the queue, and also the decoded address.
90 */
91 class MemPacket
92 {
93 public:
94
95 /** When did request enter the controller */
96 const Tick entryTime;
97
98 /** When will request leave the controller */
99 Tick readyTime;
100
101 /** This comes from the outside world */
102 const PacketPtr pkt;
103
104 /** RequestorID associated with the packet */
105 const RequestorID _requestorId;
106
107 const bool read;
108
109 /** Does this packet access DRAM?*/
110 const bool dram;
111
112 /** Will be populated by address decoder */
113 const uint8_t rank;
114 const uint8_t bank;
115 const uint32_t row;
116
117 /**
118 * Bank id is calculated considering banks in all the ranks
119 * eg: 2 ranks each with 8 banks, then bankId = 0 --> rank0, bank0 and
120 * bankId = 8 --> rank1, bank0
121 */
122 const uint16_t bankId;
123
124 /**
125 * The starting address of the packet.
126 * This address could be unaligned to burst size boundaries. The
127 * reason is to keep the address offset so we can accurately check
128 * incoming read packets with packets in the write queue.
129 */
130 Addr addr;
131
132 /**
133 * The size of this dram packet in bytes
134 * It is always equal or smaller than the burst size
135 */
136 unsigned int size;
137
138 /**
139 * A pointer to the BurstHelper if this MemPacket is a split packet
140 * If not a split packet (common case), this is set to NULL
141 */
142 BurstHelper* burstHelper;
143
144 /**
145 * QoS value of the encapsulated packet read at queuing time
146 */
147 uint8_t _qosValue;
148
149 /**
150 * Set the packet QoS value
151 * (interface compatibility with Packet)
152 */
153 inline void qosValue(const uint8_t qv) { _qosValue = qv; }
154
155 /**
156 * Get the packet QoS value
157 * (interface compatibility with Packet)
158 */
159 inline uint8_t qosValue() const { return _qosValue; }
160
161 /**
162 * Get the packet RequestorID
163 * (interface compatibility with Packet)
164 */
165 inline RequestorID requestorId() const { return _requestorId; }
166
167 /**
168 * Get the packet size
169 * (interface compatibility with Packet)
170 */
171 inline unsigned int getSize() const { return size; }
172
173 /**
174 * Get the packet address
175 * (interface compatibility with Packet)
176 */
177 inline Addr getAddr() const { return addr; }
178
179 /**
180 * Return true if its a read packet
181 * (interface compatibility with Packet)
182 */
183 inline bool isRead() const { return read; }
184
185 /**
186 * Return true if its a write packet
187 * (interface compatibility with Packet)
188 */
189 inline bool isWrite() const { return !read; }
190
191 /**
192 * Return true if its a DRAM access
193 */
194 inline bool isDram() const { return dram; }
195
196 MemPacket(PacketPtr _pkt, bool is_read, bool is_dram, uint8_t _rank,
197 uint8_t _bank, uint32_t _row, uint16_t bank_id, Addr _addr,
198 unsigned int _size)
199 : entryTime(curTick()), readyTime(curTick()), pkt(_pkt),
200 _requestorId(pkt->requestorId()),
201 read(is_read), dram(is_dram), rank(_rank), bank(_bank), row(_row),
202 bankId(bank_id), addr(_addr), size(_size), burstHelper(NULL),
203 _qosValue(_pkt->qosValue())
204 { }
205
206 };
207
208 // The memory packets are store in a multiple dequeue structure,
209 // based on their QoS priority
210 typedef std::deque<MemPacket*> MemPacketQueue;
211
212
213 /**
214 * The memory controller is a single-channel memory controller capturing
215 * the most important timing constraints associated with a
216 * contemporary controller. For multi-channel memory systems, the controller
217 * is combined with a crossbar model, with the channel address
218 * interleaving taking part in the crossbar.
219 *
220 * As a basic design principle, this controller
221 * model is not cycle callable, but instead uses events to: 1) decide
222 * when new decisions can be made, 2) when resources become available,
223 * 3) when things are to be considered done, and 4) when to send
224 * things back. The controller interfaces to media specific interfaces
225 * to enable flexible topoloties.
226 * Through these simple principles, the model delivers
227 * high performance, and lots of flexibility, allowing users to
228 * evaluate the system impact of a wide range of memory technologies.
229 *
230 * For more details, please see Hansson et al, "Simulating DRAM
231 * controllers for future system architecture exploration",
232 * Proc. ISPASS, 2014. If you use this model as part of your research
233 * please cite the paper.
234 *
235 */
236 class MemCtrl : public QoS::MemCtrl
237 {
238 private:
239
240 // For now, make use of a queued response port to avoid dealing with
241 // flow control for the responses being sent back
242 class MemoryPort : public QueuedResponsePort
243 {
244
245 RespPacketQueue queue;
246 MemCtrl& ctrl;
247
248 public:
249
250 MemoryPort(const std::string& name, MemCtrl& _ctrl);
251
252 protected:
253
254 Tick recvAtomic(PacketPtr pkt) override;
255 Tick recvAtomicBackdoor(
256 PacketPtr pkt, MemBackdoorPtr &backdoor) override;
257
258 void recvFunctional(PacketPtr pkt) override;
259
260 bool recvTimingReq(PacketPtr) override;
261
262 AddrRangeList getAddrRanges() const override;
263
264 };
265
266 /**
267 * Our incoming port, for a multi-ported controller add a crossbar
268 * in front of it
269 */
270 MemoryPort port;
271
272 /**
273 * Remember if the memory system is in timing mode
274 */
275 bool isTimingMode;
276
277 /**
278 * Remember if we have to retry a request when available.
279 */
280 bool retryRdReq;
281 bool retryWrReq;
282
283 /**
284 * Bunch of things requires to setup "events" in gem5
285 * When event "respondEvent" occurs for example, the method
286 * processRespondEvent is called; no parameters are allowed
287 * in these methods
288 */
289 void processNextReqEvent();
290 EventFunctionWrapper nextReqEvent;
291
292 void processRespondEvent();
293 EventFunctionWrapper respondEvent;
294
295 /**
296 * Check if the read queue has room for more entries
297 *
298 * @param pkt_count The number of entries needed in the read queue
299 * @return true if read queue is full, false otherwise
300 */
301 bool readQueueFull(unsigned int pkt_count) const;
302
303 /**
304 * Check if the write queue has room for more entries
305 *
306 * @param pkt_count The number of entries needed in the write queue
307 * @return true if write queue is full, false otherwise
308 */
309 bool writeQueueFull(unsigned int pkt_count) const;
310
311 /**
312 * When a new read comes in, first check if the write q has a
313 * pending request to the same address.\ If not, decode the
314 * address to populate rank/bank/row, create one or mutliple
315 * "mem_pkt", and push them to the back of the read queue.\
316 * If this is the only
317 * read request in the system, schedule an event to start
318 * servicing it.
319 *
320 * @param pkt The request packet from the outside world
321 * @param pkt_count The number of memory bursts the pkt
322 * @param is_dram Does this packet access DRAM?
323 * translate to. If pkt size is larger then one full burst,
324 * then pkt_count is greater than one.
325 */
326 void addToReadQueue(PacketPtr pkt, unsigned int pkt_count, bool is_dram);
327
328 /**
329 * Decode the incoming pkt, create a mem_pkt and push to the
330 * back of the write queue. \If the write q length is more than
331 * the threshold specified by the user, ie the queue is beginning
332 * to get full, stop reads, and start draining writes.
333 *
334 * @param pkt The request packet from the outside world
335 * @param pkt_count The number of memory bursts the pkt
336 * @param is_dram Does this packet access DRAM?
337 * translate to. If pkt size is larger then one full burst,
338 * then pkt_count is greater than one.
339 */
340 void addToWriteQueue(PacketPtr pkt, unsigned int pkt_count, bool is_dram);
341
342 /**
343 * Actually do the burst based on media specific access function.
344 * Update bus statistics when complete.
345 *
346 * @param mem_pkt The memory packet created from the outside world pkt
347 */
348 void doBurstAccess(MemPacket* mem_pkt);
349
350 /**
351 * When a packet reaches its "readyTime" in the response Q,
352 * use the "access()" method in AbstractMemory to actually
353 * create the response packet, and send it back to the outside
354 * world requestor.
355 *
356 * @param pkt The packet from the outside world
357 * @param static_latency Static latency to add before sending the packet
358 */
359 void accessAndRespond(PacketPtr pkt, Tick static_latency);
360
361 /**
362 * Determine if there is a packet that can issue.
363 *
364 * @param pkt The packet to evaluate
365 */
366 bool packetReady(MemPacket* pkt);
367
368 /**
369 * Calculate the minimum delay used when scheduling a read-to-write
370 * transision.
371 * @param return minimum delay
372 */
373 Tick minReadToWriteDataGap();
374
375 /**
376 * Calculate the minimum delay used when scheduling a write-to-read
377 * transision.
378 * @param return minimum delay
379 */
380 Tick minWriteToReadDataGap();
381
382 /**
383 * The memory schduler/arbiter - picks which request needs to
384 * go next, based on the specified policy such as FCFS or FR-FCFS
385 * and moves it to the head of the queue.
386 * Prioritizes accesses to the same rank as previous burst unless
387 * controller is switching command type.
388 *
389 * @param queue Queued requests to consider
390 * @param extra_col_delay Any extra delay due to a read/write switch
391 * @return an iterator to the selected packet, else queue.end()
392 */
393 MemPacketQueue::iterator chooseNext(MemPacketQueue& queue,
394 Tick extra_col_delay);
395
396 /**
397 * For FR-FCFS policy reorder the read/write queue depending on row buffer
398 * hits and earliest bursts available in memory
399 *
400 * @param queue Queued requests to consider
401 * @param extra_col_delay Any extra delay due to a read/write switch
402 * @return an iterator to the selected packet, else queue.end()
403 */
404 MemPacketQueue::iterator chooseNextFRFCFS(MemPacketQueue& queue,
405 Tick extra_col_delay);
406
407 /**
408 * Calculate burst window aligned tick
409 *
410 * @param cmd_tick Initial tick of command
411 * @return burst window aligned tick
412 */
413 Tick getBurstWindow(Tick cmd_tick);
414
415 /**
416 * Used for debugging to observe the contents of the queues.
417 */
418 void printQs() const;
419
420 /**
421 * Burst-align an address.
422 *
423 * @param addr The potentially unaligned address
424 * @param is_dram Does this packet access DRAM?
425 *
426 * @return An address aligned to a memory burst
427 */
428 Addr burstAlign(Addr addr, bool is_dram) const;
429
430 /**
431 * The controller's main read and write queues,
432 * with support for QoS reordering
433 */
434 std::vector<MemPacketQueue> readQueue;
435 std::vector<MemPacketQueue> writeQueue;
436
437 /**
438 * To avoid iterating over the write queue to check for
439 * overlapping transactions, maintain a set of burst addresses
440 * that are currently queued. Since we merge writes to the same
441 * location we never have more than one address to the same burst
442 * address.
443 */
444 std::unordered_set<Addr> isInWriteQueue;
445
446 /**
447 * Response queue where read packets wait after we're done working
448 * with them, but it's not time to send the response yet. The
449 * responses are stored separately mostly to keep the code clean
450 * and help with events scheduling. For all logical purposes such
451 * as sizing the read queue, this and the main read queue need to
452 * be added together.
453 */
454 std::deque<MemPacket*> respQueue;
455
456 /**
457 * Holds count of commands issued in burst window starting at
458 * defined Tick. This is used to ensure that the command bandwidth
459 * does not exceed the allowable media constraints.
460 */
461 std::unordered_multiset<Tick> burstTicks;
462
463 /**
464 * Create pointer to interface of the actual dram media when connected
465 */
466 DRAMInterface* const dram;
467
468 /**
469 * Create pointer to interface of the actual nvm media when connected
470 */
471 NVMInterface* const nvm;
472
473 /**
474 * The following are basic design parameters of the memory
475 * controller, and are initialized based on parameter values.
476 * The rowsPerBank is determined based on the capacity, number of
477 * ranks and banks, the burst size, and the row buffer size.
478 */
479 const uint32_t readBufferSize;
480 const uint32_t writeBufferSize;
481 const uint32_t writeHighThreshold;
482 const uint32_t writeLowThreshold;
483 const uint32_t minWritesPerSwitch;
484 uint32_t writesThisTime;
485 uint32_t readsThisTime;
486
487 /**
488 * Memory controller configuration initialized based on parameter
489 * values.
490 */
491 Enums::MemSched memSchedPolicy;
492
493 /**
494 * Pipeline latency of the controller frontend. The frontend
495 * contribution is added to writes (that complete when they are in
496 * the write buffer) and reads that are serviced the write buffer.
497 */
498 const Tick frontendLatency;
499
500 /**
501 * Pipeline latency of the backend and PHY. Along with the
502 * frontend contribution, this latency is added to reads serviced
503 * by the memory.
504 */
505 const Tick backendLatency;
506
507 /**
508 * Length of a command window, used to check
509 * command bandwidth
510 */
511 const Tick commandWindow;
512
513 /**
514 * Till when must we wait before issuing next RD/WR burst?
515 */
516 Tick nextBurstAt;
517
518 Tick prevArrival;
519
520 /**
521 * The soonest you have to start thinking about the next request
522 * is the longest access time that can occur before
523 * nextBurstAt. Assuming you need to precharge, open a new row,
524 * and access, it is tRP + tRCD + tCL.
525 */
526 Tick nextReqTime;
527
528 struct CtrlStats : public Stats::Group
529 {
530 CtrlStats(MemCtrl &ctrl);
531
532 void regStats() override;
533
534 MemCtrl &ctrl;
535
536 // All statistics that the model needs to capture
537 Stats::Scalar readReqs;
538 Stats::Scalar writeReqs;
539 Stats::Scalar readBursts;
540 Stats::Scalar writeBursts;
541 Stats::Scalar servicedByWrQ;
542 Stats::Scalar mergedWrBursts;
543 Stats::Scalar neitherReadNorWriteReqs;
544 // Average queue lengths
545 Stats::Average avgRdQLen;
546 Stats::Average avgWrQLen;
547
548 Stats::Scalar numRdRetry;
549 Stats::Scalar numWrRetry;
550 Stats::Vector readPktSize;
551 Stats::Vector writePktSize;
552 Stats::Vector rdQLenPdf;
553 Stats::Vector wrQLenPdf;
554 Stats::Histogram rdPerTurnAround;
555 Stats::Histogram wrPerTurnAround;
556
557 Stats::Scalar bytesReadWrQ;
558 Stats::Scalar bytesReadSys;
559 Stats::Scalar bytesWrittenSys;
560 // Average bandwidth
561 Stats::Formula avgRdBWSys;
562 Stats::Formula avgWrBWSys;
563
564 Stats::Scalar totGap;
565 Stats::Formula avgGap;
566
567 // per-requestor bytes read and written to memory
568 Stats::Vector requestorReadBytes;
569 Stats::Vector requestorWriteBytes;
570
571 // per-requestor bytes read and written to memory rate
572 Stats::Formula requestorReadRate;
573 Stats::Formula requestorWriteRate;
574
575 // per-requestor read and write serviced memory accesses
576 Stats::Vector requestorReadAccesses;
577 Stats::Vector requestorWriteAccesses;
578
579 // per-requestor read and write total memory access latency
580 Stats::Vector requestorReadTotalLat;
581 Stats::Vector requestorWriteTotalLat;
582
583 // per-requestor raed and write average memory access latency
584 Stats::Formula requestorReadAvgLat;
585 Stats::Formula requestorWriteAvgLat;
586 };
587
588 CtrlStats stats;
589
590 /**
591 * Upstream caches need this packet until true is returned, so
592 * hold it for deletion until a subsequent call
593 */
594 std::unique_ptr<Packet> pendingDelete;
595
596 /**
597 * Select either the read or write queue
598 *
599 * @param is_read The current burst is a read, select read queue
600 * @return a reference to the appropriate queue
601 */
602 std::vector<MemPacketQueue>& selQueue(bool is_read)
603 {
604 return (is_read ? readQueue : writeQueue);
605 };
606
607 /**
608 * Remove commands that have already issued from burstTicks
609 */
610 void pruneBurstTick();
611
612 public:
613
614 MemCtrl(const MemCtrlParams &p);
615
616 /**
617 * Ensure that all interfaced have drained commands
618 *
619 * @return bool flag, set once drain complete
620 */
621 bool allIntfDrained() const;
622
623 DrainState drain() override;
624
625 /**
626 * Check for command bus contention for single cycle command.
627 * If there is contention, shift command to next burst.
628 * Check verifies that the commands issued per burst is less
629 * than a defined max number, maxCommandsPerWindow.
630 * Therefore, contention per cycle is not verified and instead
631 * is done based on a burst window.
632 *
633 * @param cmd_tick Initial tick of command, to be verified
634 * @param max_cmds_per_burst Number of commands that can issue
635 * in a burst window
636 * @return tick for command issue without contention
637 */
638 Tick verifySingleCmd(Tick cmd_tick, Tick max_cmds_per_burst);
639
640 /**
641 * Check for command bus contention for multi-cycle (2 currently)
642 * command. If there is contention, shift command(s) to next burst.
643 * Check verifies that the commands issued per burst is less
644 * than a defined max number, maxCommandsPerWindow.
645 * Therefore, contention per cycle is not verified and instead
646 * is done based on a burst window.
647 *
648 * @param cmd_tick Initial tick of command, to be verified
649 * @param max_multi_cmd_split Maximum delay between commands
650 * @param max_cmds_per_burst Number of commands that can issue
651 * in a burst window
652 * @return tick for command issue without contention
653 */
654 Tick verifyMultiCmd(Tick cmd_tick, Tick max_cmds_per_burst,
655 Tick max_multi_cmd_split = 0);
656
657 /**
658 * Is there a respondEvent scheduled?
659 *
660 * @return true if event is scheduled
661 */
662 bool respondEventScheduled() const { return respondEvent.scheduled(); }
663
664 /**
665 * Is there a read/write burst Event scheduled?
666 *
667 * @return true if event is scheduled
668 */
669 bool requestEventScheduled() const { return nextReqEvent.scheduled(); }
670
671 /**
672 * restart the controller
673 * This can be used by interfaces to restart the
674 * scheduler after maintainence commands complete
675 *
676 * @param Tick to schedule next event
677 */
678 void restartScheduler(Tick tick) { schedule(nextReqEvent, tick); }
679
680 /**
681 * Check the current direction of the memory channel
682 *
683 * @param next_state Check either the current or next bus state
684 * @return True when bus is currently in a read state
685 */
686 bool inReadBusState(bool next_state) const;
687
688 /**
689 * Check the current direction of the memory channel
690 *
691 * @param next_state Check either the current or next bus state
692 * @return True when bus is currently in a write state
693 */
694 bool inWriteBusState(bool next_state) const;
695
696 Port &getPort(const std::string &if_name,
697 PortID idx=InvalidPortID) override;
698
699 virtual void init() override;
700 virtual void startup() override;
701 virtual void drainResume() override;
702
703 protected:
704
705 Tick recvAtomic(PacketPtr pkt);
706 Tick recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &backdoor);
707 void recvFunctional(PacketPtr pkt);
708 bool recvTimingReq(PacketPtr pkt);
709
710 };
711
712 #endif //__MEM_CTRL_HH__