-
Notifications
You must be signed in to change notification settings - Fork 2
/
mem_ctrl.cc
2182 lines (1906 loc) · 82.7 KB
/
mem_ctrl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2020 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* Copyright (c) 2013 Amin Farmahini-Farahani
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mem/mem_ctrl.hh"
#include "base/trace.hh"
#include "debug/DRAM.hh"
#include "debug/Drain.hh"
#include "debug/MemCtrl.hh"
#include "debug/NVM.hh"
#include "debug/QOS.hh"
#include "mem/dram_interface.hh"
#include "mem/mem_interface.hh"
#include "mem/nvm_interface.hh"
#include "sim/system.hh"
namespace gem5
{
namespace memory
{
MemCtrl::MemCtrl(const MemCtrlParams &p) :
qos::MemCtrl(p),
port(name() + ".port", *this), isTimingMode(false),
retryRdReq(false), retryWrReq(false),
nextReqEvent([this] {processNextReqEvent(dram, respQueue,
respondEvent, nextReqEvent, retryWrReq);}, name()),
respondEvent([this] {processRespondEvent(dram, respQueue,
respondEvent, retryRdReq); }, name()),
dram(p.dram),
readBufferSize(dram->readBufferSize),
writeBufferSize(dram->writeBufferSize),
writeHighThreshold(writeBufferSize * p.write_high_thresh_perc / 100.0),
writeLowThreshold(writeBufferSize * p.write_low_thresh_perc / 100.0),
minWritesPerSwitch(p.min_writes_per_switch),
minReadsPerSwitch(p.min_reads_per_switch),
writesThisTime(0), readsThisTime(0),
memSchedPolicy(p.mem_sched_policy),
frontendLatency(p.static_frontend_latency),
backendLatency(p.static_backend_latency),
commandWindow(p.command_window),
prevArrival(0),
stats(*this),
mcsquare(p.mcsquare)
{
DPRINTF(MemCtrl, "Setting up controller\n");
readQueue.resize(p.qos_priorities);
writeQueue.resize(p.qos_priorities);
dram->setCtrl(this, commandWindow);
// perform a basic check of the write thresholds
if (p.write_low_thresh_perc >= p.write_high_thresh_perc)
fatal("Write buffer low threshold %d must be smaller than the "
"high threshold %d\n", p.write_low_thresh_perc,
p.write_high_thresh_perc);
if (p.disable_sanity_check) {
port.disableSanityCheck();
}
}
void
MemCtrl::init()
{
if (!port.isConnected()) {
fatal("MemCtrl %s is unconnected!\n", name());
} else {
port.sendRangeChange();
}
}
void
MemCtrl::startup()
{
// remember the memory system mode of operation
isTimingMode = system()->isTimingMode();
if (isTimingMode) {
// shift the bus busy time sufficiently far ahead that we never
// have to worry about negative values when computing the time for
// the next request, this will add an insignificant bubble at the
// start of simulation
dram->nextBurstAt = curTick() + dram->commandOffset();
}
}
Tick
MemCtrl::recvAtomic(PacketPtr pkt)
{
if (!dram->getAddrRange().contains(pkt->getAddr())) {
panic("Can't handle address range for packet %s\n", pkt->print());
}
return recvAtomicLogic(pkt, dram);
}
Tick
MemCtrl::recvAtomicLogic(PacketPtr pkt, MemInterface* mem_intr)
{
DPRINTF(MemCtrl, "recvAtomic: %s 0x%x\n",
pkt->cmdString(), pkt->getAddr());
panic_if(pkt->cacheResponding(), "Should not see packets where cache "
"is responding");
// do the actual memory access and turn the packet into a response
mem_intr->access(pkt);
if (pkt->hasData()) {
// this value is not supposed to be accurate, just enough to
// keep things going, mimic a closed page
// also this latency can't be 0
return mem_intr->accessLatency();
}
return 0;
}
Tick
MemCtrl::recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &backdoor)
{
Tick latency = recvAtomic(pkt);
dram->getBackdoor(backdoor);
return latency;
}
bool
MemCtrl::readQueueFull(unsigned int neededEntries) const
{
DPRINTF(MemCtrl,
"Read queue limit %d, current size %d, entries needed %d\n",
readBufferSize, totalReadQueueSize + respQueue.size(),
neededEntries);
auto rdsize_new = totalReadQueueSize + respQueue.size() + neededEntries;
return rdsize_new > readBufferSize;
}
bool
MemCtrl::writeQueueFull(unsigned int neededEntries) const
{
DPRINTF(MemCtrl,
"Write queue limit %d, current size %d, entries needed %d\n",
writeBufferSize, totalWriteQueueSize, neededEntries);
auto wrsize_new = (totalWriteQueueSize + neededEntries);
return wrsize_new > writeBufferSize;
}
bool
MemCtrl::addToReadQueue(PacketPtr pkt,
unsigned int pkt_count, MemInterface* mem_intr)
{
// only add to the read queue here. whenever the request is
// eventually done, set the readyTime, and call schedule()
assert(!pkt->isWrite());
assert(pkt_count != 0);
// if the request size is larger than burst size, the pkt is split into
// multiple packets
// Note if the pkt starting address is not aligened to burst size, the
// address of first packet is kept unaliged. Subsequent packets
// are aligned to burst size boundaries. This is to ensure we accurately
// check read packets against packets in write queue.
const Addr base_addr = pkt->getAddr();
Addr addr = base_addr;
unsigned pktsServicedByWrQ = 0;
BurstHelper* burst_helper = NULL;
uint32_t burst_size = mem_intr->bytesPerBurst();
for (int cnt = 0; cnt < pkt_count; ++cnt) {
unsigned size = std::min((addr | (burst_size - 1)) + 1,
base_addr + pkt->getSize()) - addr;
stats.readPktSize[ceilLog2(size)]++;
stats.readBursts++;
stats.requestorReadAccesses[pkt->requestorId()]++;
// First check write buffer to see if the data is already at
// the controller
bool foundInWrQ = false;
Addr burst_addr = burstAlign(addr, mem_intr);
// if the burst address is not present then there is no need
// looking any further
if (isInWriteQueue.find(burst_addr) != isInWriteQueue.end()) {
for (const auto& vec : writeQueue) {
for (const auto& p : vec) {
// check if the read is subsumed in the write queue
// packet we are looking at
if (p->addr <= addr &&
((addr + size) <= (p->addr + p->size))) {
foundInWrQ = true;
stats.servicedByWrQ++;
pktsServicedByWrQ++;
DPRINTF(MemCtrl,
"Read to addr %#x with size %d serviced by "
"write queue\n",
addr, size);
stats.bytesReadWrQ += burst_size;
break;
}
}
}
}
// If not found in the write q, make a memory packet and
// push it onto the read queue
if (!foundInWrQ) {
// Make the burst helper for split packets
if (pkt_count > 1 && burst_helper == NULL) {
DPRINTF(MemCtrl, "Read to addr %#x translates to %d "
"memory requests\n", pkt->getAddr(), pkt_count);
burst_helper = new BurstHelper(pkt_count);
}
MemPacket* mem_pkt;
mem_pkt = mem_intr->decodePacket(pkt, addr, size, true,
mem_intr->pseudoChannel);
// Increment read entries of the rank (dram)
// Increment count to trigger issue of non-deterministic read (nvm)
mem_intr->setupRank(mem_pkt->rank, true);
// Default readyTime to Max; will be reset once read is issued
mem_pkt->readyTime = MaxTick;
mem_pkt->burstHelper = burst_helper;
assert(!readQueueFull(1));
stats.rdQLenPdf[totalReadQueueSize + respQueue.size()]++;
DPRINTF(MemCtrl, "Adding to read queue\n");
readQueue[mem_pkt->qosValue()].push_back(mem_pkt);
// log packet
logRequest(MemCtrl::READ, pkt->requestorId(),
pkt->qosValue(), mem_pkt->addr, 1);
// Update stats
stats.avgRdQLen = totalReadQueueSize + respQueue.size();
}
// Starting address of next memory pkt (aligned to burst boundary)
addr = (addr | (burst_size - 1)) + 1;
}
// If all packets are serviced by write queue, we send the repsonse back
if (pktsServicedByWrQ == pkt_count) {
accessAndRespond(pkt, frontendLatency, mem_intr);
return true;
}
// Update how many split packets are serviced by write queue
if (burst_helper != NULL)
burst_helper->burstsServiced = pktsServicedByWrQ;
// not all/any packets serviced by the write queue
return false;
}
void
MemCtrl::addToWriteQueue(PacketPtr pkt, unsigned int pkt_count,
MemInterface* mem_intr)
{
// only add to the write queue here. whenever the request is
// eventually done, set the readyTime, and call schedule()
assert(pkt->isWrite());
// if the request size is larger than burst size, the pkt is split into
// multiple packets
const Addr base_addr = pkt->getAddr();
Addr addr = base_addr;
uint32_t burst_size = mem_intr->bytesPerBurst();
for (int cnt = 0; cnt < pkt_count; ++cnt) {
unsigned size = std::min((addr | (burst_size - 1)) + 1,
base_addr + pkt->getSize()) - addr;
stats.writePktSize[ceilLog2(size)]++;
stats.writeBursts++;
stats.requestorWriteAccesses[pkt->requestorId()]++;
// see if we can merge with an existing item in the write
// queue and keep track of whether we have merged or not
bool merged = isInWriteQueue.find(burstAlign(addr, mem_intr)) !=
isInWriteQueue.end();
// if the item was not merged we need to create a new write
// and enqueue it
if (!merged) {
MemPacket* mem_pkt;
mem_pkt = mem_intr->decodePacket(pkt, addr, size, false,
mem_intr->pseudoChannel);
// Default readyTime to Max if nvm interface;
//will be reset once read is issued
mem_pkt->readyTime = MaxTick;
mem_intr->setupRank(mem_pkt->rank, false);
assert(totalWriteQueueSize < writeBufferSize);
stats.wrQLenPdf[totalWriteQueueSize]++;
DPRINTF(MemCtrl, "Adding to write queue\n");
writeQueue[mem_pkt->qosValue()].push_back(mem_pkt);
isInWriteQueue.insert(burstAlign(addr, mem_intr));
// log packet
logRequest(MemCtrl::WRITE, pkt->requestorId(),
pkt->qosValue(), mem_pkt->addr, 1);
assert(totalWriteQueueSize == isInWriteQueue.size());
// Update stats
stats.avgWrQLen = totalWriteQueueSize;
} else {
DPRINTF(MemCtrl,
"Merging write burst with existing queue entry\n");
// keep track of the fact that this burst effectively
// disappeared as it was merged with an existing one
stats.mergedWrBursts++;
}
// Starting address of next memory pkt (aligned to burst_size boundary)
addr = (addr | (burst_size - 1)) + 1;
}
// we do not wait for the writes to be send to the actual memory,
// but instead take responsibility for the consistency here and
// snoop the write queue for any upcoming reads
// @todo, if a pkt size is larger than burst size, we might need a
// different front end latency
accessAndRespond(pkt, frontendLatency, mem_intr);
}
void
MemCtrl::printQs() const
{
#if TRACING_ON
DPRINTF(MemCtrl, "===READ QUEUE===\n\n");
for (const auto& queue : readQueue) {
for (const auto& packet : queue) {
DPRINTF(MemCtrl, "Read %#x\n", packet->addr);
}
}
DPRINTF(MemCtrl, "\n===RESP QUEUE===\n\n");
for (const auto& packet : respQueue) {
DPRINTF(MemCtrl, "Response %#x\n", packet->addr);
}
DPRINTF(MemCtrl, "\n===WRITE QUEUE===\n\n");
for (const auto& queue : writeQueue) {
for (const auto& packet : queue) {
DPRINTF(MemCtrl, "Write %#x\n", packet->addr);
}
}
#endif // TRACING_ON
}
void
MemCtrl::checkBounceTable() {
for(auto i = mcsquare->m_bpq.begin(); i != mcsquare->m_bpq.end();) {
MCSquare::Types type = mcsquare->contains(i->first, 64);
//if(type != MCSquare::Types::TYPE_SRC) {
if(mcsquare->m_bpq.getPkts(i->first) == 0 ||
type != MCSquare::Types::TYPE_SRC) {
auto req = std::make_shared<Request>(i->first,
64, Request::MEM_ELIDE_REDIRECT_SRC,
Request::funcRequestorId);
auto bouncePkt = Packet::createWrite(req);
bouncePkt->allocate();
bouncePkt->setData(mcsquare->m_bpq.getData(i->first));
auto iter = i;
++i;
mcsquare->m_bpq.remove(iter->first);
DPRINTF(MCSquare, "BPQ writeback packet (%lx, %u)\n",
bouncePkt->getAddr(), bouncePkt->getSize());
accessAndRespond(bouncePkt,
frontendLatency + mcsquare->getBPQPenalty(), dram);
delete bouncePkt;
if (srcWritePause && retryWrReq) {
DPRINTF(MCSquare, "Yeet: Trying to retry write requests\n");
srcWritePause = false;
retryWrReq = false;
port.sendRetryReq();
}
continue;
} else {
DPRINTF(MCSquare, "BPQ entry %lx needs %d pkts\n",
i->first, mcsquare->m_bpq.getPkts(i->first));
}
++i;
}
// See if bounce for src is complete
if(mcsquare->ctt_freeing.size()) {
for(auto i = mcsquare->ctt_freeing.begin(); i != mcsquare->ctt_freeing.end();) {
MCSquare::Types type = mcsquare->contains(i->first, 64);
if(i->second == 0 || type != MCSquare::Types::TYPE_SRC) {
DPRINTF(MCSquare, "Reset ctt_src_entry %lx. CTT size now %d\n",
i->first, mcsquare->getCTTSize());
auto j = i;
++i;
mcsquare->ctt_freeing.erase(j);
//clearCTT();
} else {
++i;
}
}
}
}
void MemCtrl::clearCTT() {
// Start freeing CTT entries if needed
if(mcsquare->getCTTSize() >= mcsquare->ctt_free_frac * mcsquare->getMaxCTTSize() &&
mcsquare->ctt_freeing.size() < mcsquare->ctt_freeing_max) {
Addr candidate = mcsquare->getAddrToFree(getAddrRanges());
if(candidate) {
DPRINTF(MCSquare, "%d exceeds threshold of CTT size %d! Freeing entries; Candidate %lx\n",
mcsquare->getCTTSize(), (int)(mcsquare->ctt_free_frac *
mcsquare->getMaxCTTSize()), candidate);
unsigned size = 64;
uint32_t burst_size = dram->bytesPerBurst();
unsigned offset = candidate & (burst_size - 1);
unsigned int pkt_count = divCeil(offset + size, burst_size);
if (readQueueFull(pkt_count)) {
DPRINTF(MCSquare, "Trying to clear CTT but read queue full\n");
return;
}
// Create read to src request
auto req = std::make_shared<Request>(candidate, 64,
Request::MEM_ELIDE_WRITE_SRC, Request::funcRequestorId);
auto bouncePkt = Packet::createRead(req);
bouncePkt->allocate();
mcsquare->ctt_freeing[candidate] = -1;
if (!addToReadQueue(bouncePkt, pkt_count, dram)) {
// If we are not already scheduled to get a request out of the
// queue, do so now
if (!nextReqEvent.scheduled()) {
DPRINTF(MemCtrl, "Request scheduled immediately\n");
schedule(nextReqEvent, curTick());
}
}
}
}
}
bool
MemCtrl::canHandleMCPkt(PacketPtr pkt, bool &canHandle)
{
if(isMCSquare(pkt)) {
// See if this memctrl owns pkt location
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
// Already know cannot handle. Skip check and return.
if(canHandle == false) {
if(weContain) {
DPRINTF(MCSquare, "Found mem_elide %lx touching current src write, waiting\n",
pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
}
return false;
}
// Check if CTT is already full
if(mcsquare->getCTTSize() >= mcsquare->getMaxCTTSize()) {
DPRINTF(MCSquare, "%ld exceeds max CTT size %d! Cannot add %lx\n",
mcsquare->getCTTSize(), mcsquare->getMaxCTTSize(), pkt->getAddr());
canHandle = false;
if(weContain) {
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
}
mcsquare->stats.memElideBlockedCTTFull++;
return false;
}
// Check if mem_elide touches current src write.
for(auto i = mcsquare->m_bpq.begin(); i != mcsquare->m_bpq.end(); ++i) {
if(RangeSize(pkt->getAddr(), pkt->req->getSize()).
intersects(RangeSize(i->first, 64)) ||
(pkt->req->getFlags() & Request::MEM_ELIDE_FREE && pkt->getSize() == 1)) {
DPRINTF(MCSquare, "Found mem_elide %lx touching current src write %lx, waiting\n",
pkt->getAddr(), i->first);
canHandle = false;
if(weContain) {
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
}
return false;
}
}
}
return canHandle;
}
bool
MemCtrl::recvTimingReq(PacketPtr pkt)
{
// This is where we enter from the outside world
DPRINTF(MemCtrl, "recvTimingReq: request %s addr %#x size %d\n",
pkt->cmdString(), pkt->getAddr(), pkt->getSize());
panic_if(pkt->cacheResponding(), "Should not see packets where cache "
"is responding");
panic_if(!(pkt->isRead() || pkt->isWrite()),
"Should only see read and writes at memory controller\n");
// Calc avg gap between requests
if (prevArrival != 0) {
stats.totGap += curTick() - prevArrival;
}
prevArrival = curTick();
if(isMCSquare(pkt)) {
if(pkt->mc_dest_offset >= 100) {
bool canHandle = pkt->mc_dest_offset - 100;
return canHandleMCPkt(pkt, canHandle);
}
if(pkt->req->getFlags() & Request::MEM_ELIDE) {
mcsquare->insertEntry(pkt->getAddr(),
pkt->req->_paddr_src, pkt->req->getSize());
mcsquare->stats.sizeElided += pkt->req->getSize();
}
else if(pkt->req->getFlags() & Request::MEM_ELIDE_FREE)
mcsquare->deleteEntry(pkt->getAddr(), pkt->req->getSize());
checkBounceTable();
// See if we're responsible for sending a response back
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
// turn packet around to go back to requestor if response expected
// Only the memctrl that owns the packet will send.
if (pkt->needsResponse() && weContain) {
pkt->makeResponse();
port.schedTimingResp(pkt, curTick() + 1);
} else if(weContain) {
// @todo the packet is going to be deleted, and the MemPacket
// is still having a pointer to it
pendingDelete.reset(pkt);
}
clearCTT();
return true;
}
/*
if(MCSquare::Types type = mcsquare->contains(pkt)) {
if(pkt->isWrite()) {
// See if we're responsible for sending a response back
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
if(type == MCSquare::Types::TYPE_DEST) {
mcsquare->m_bpq.decPkts(pkt->req->_paddr_src);
mcsquare->splitEntry(pkt);
checkBounceTable();
// Response from other memory controller
// Just using this to update our table, so return here.
if(!weContain)
return true;
else
pkt->req->setFlags(Request::MEM_ELIDE_WRITE_DEST);
DPRINTF(MCSquare, "Found a write to dest %lx!\n", pkt->getAddr());
mcsquare->stats.destWriteSize += pkt->getSize();
// Will not create a response. Create a dummy duplicate packet for coherency of CTT
if(!pkt->needsResponse()) {
DPRINTF(MCSquare, "Does not need response, create fake packet %lx!\n", pkt->getAddr());
// Create read to src request
auto req = std::make_shared<Request>(pkt->getAddr(),
pkt->getSize(), Request::MEM_ELIDE_REDIRECT_SRC,
Request::funcRequestorId);
auto bouncePkt = Packet::createWrite(req);
bouncePkt->allocate();
bouncePkt->makeResponse();
port.schedTimingResp(bouncePkt, curTick());
}
} else if(type == MCSquare::Types::TYPE_SRC) {
// Sent as a write to dest. Just acknowledge and ignore.
if(pkt->req->getFlags() & Request::MEM_ELIDE_REDIRECT_SRC) {
mcsquare->m_bpq.decPkts(pkt->req->_paddr_src);
mcsquare->splitEntry(pkt);
checkBounceTable();
DPRINTF(MCSquare, "Found duplicate generated write to %lx, "
"ignoring this one\n", pkt->getAddr());
if(weContain)
delete pkt;
return true;
}
// Not this memctrl's problem. Just return true.
if(!weContain)
return true;
if(pkt->req->getFlags() & Request::MEM_ELIDE_WRITE_SRC) {
DPRINTF(MCSquare, "Found write to src %lx, waiting for bounce to complete\n",
pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
return false;
}
// If already generated write to src, ignore this request
if(mcsquare->m_bpq.find(pkt->getAddr())) {
mcsquare->m_bpq.setData(pkt->getAddr(), pkt->getPtr<uint8_t>());
DPRINTF(MCSquare, "Found write to src %lx in bounce table\n",
pkt->getAddr());
accessAndRespond(pkt,
frontendLatency + mcsquare->getBPQPenalty(), dram);
return true;
}
// Exceeded BPQ size
if(mcsquare->m_bpq.size() >= mcsquare->getMaxBPQSize()) {
DPRINTF(MCSquare, "Exceeded max BPQ size! Cannot add %lx\n", pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
return false;
}
// Fresh write to source, generate a read req
// See if we have space in read queue for read to src
unsigned size = pkt->getSize();
uint32_t burst_size = dram->bytesPerBurst();
unsigned offset = pkt->getAddr() & (burst_size - 1);
unsigned int pkt_count = divCeil(offset + size, burst_size);
if (readQueueFull(pkt_count)) {
DPRINTF(MCSquare, "Found write to src %lx, read queue full\n",
pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
return false;
}
mcsquare->m_bpq.insert(pkt->getAddr(), pkt->getPtr<uint8_t>(), true, false);
DPRINTF(MCSquare, "Inserted src %lx, BPQ size %ld\n",
pkt->getAddr(), mcsquare->m_bpq.size());
// Create read to src request
pkt->req->setFlags(Request::MEM_ELIDE_WRITE_SRC);
auto req = std::make_shared<Request>(pkt->getAddr(),
pkt->getSize(), Request::MEM_ELIDE_WRITE_SRC,
pkt->req->funcRequestorId);
auto bouncePkt = Packet::createRead(req);
bouncePkt->allocate();
if (!addToReadQueue(bouncePkt, pkt_count, dram)) {
// If we are not already scheduled to get a request out of the
// queue, do so now
if (!nextReqEvent.scheduled()) {
DPRINTF(MemCtrl, "Request scheduled immediately\n");
schedule(nextReqEvent, curTick());
}
}
stats.readReqs++;
stats.bytesReadSys += size;
// Now return until the final write to dest(s) is complete
mcsquare->stats.srcWriteSize += pkt->getSize();
mcsquare->stats.srcWritesBlocked++;
accessAndRespond(pkt, frontendLatency + mcsquare->getBPQPenalty(), dram);
return true;
}
}
// Read to destination. Add table access latency then bounce packet.
if(pkt->isRead() && !(pkt->req->getFlags() & Request::MEM_ELIDE_REDIRECT_SRC)) {
if(type == MCSquare::Types::TYPE_DEST) {
DPRINTF(MCSquare, "Found a read to dest %lx, applying elision penalty\n", pkt->getAddr());
mcsquare->stats.destReadSize += pkt->getSize();
accessAndRespond(pkt, mcsquare->getCTTPenalty(), dram);
return true;
} else if(type == MCSquare::Types::TYPE_SRC) {
mcsquare->stats.srcReadSize += pkt->getSize();
DPRINTF(MCSquare, "Found read to src %lx; fallthrough\n", pkt->getAddr());
}
}
} else if(pkt->isWrite() && pkt->req->getFlags() & Request::MEM_ELIDE_REDIRECT_SRC) {
// Write generated for dest. Since not in table, must be duplicate. Just drop it.
DPRINTF(MCSquare, "Found duplicate generated write to %lx (src = %lx), "
"ignoring this one\n", pkt->getAddr(), pkt->req->_paddr_src);
mcsquare->m_bpq.decPkts(pkt->req->_paddr_src);
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
if(weContain)
delete pkt;
return true;
}*/
if(!isMCReq(pkt)) {
if(pkt->isWrite()) {
if(mcsquare->isDest(pkt)) {
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
mcsquare->m_bpq.decPkts(pkt->req->_paddr_src);
if(mcsquare->ctt_freeing.find(pkt->req->_paddr_src) != mcsquare->ctt_freeing.end() &&
mcsquare->ctt_freeing[pkt->req->_paddr_src] > 0) {
mcsquare->ctt_freeing[pkt->req->_paddr_src]--;
DPRINTF(MCSquare, "Decrementing ctt_count to %d\n", mcsquare->ctt_freeing[pkt->req->_paddr_src]);
}
mcsquare->splitEntry(pkt);
checkBounceTable();
clearCTT();
// Response from other memory controller
// Just using this to update our table, so return here.
if(!weContain)
return true;
else
pkt->req->setFlags(Request::MEM_ELIDE_WRITE_DEST);
DPRINTF(MCSquare, "Found a write to dest %lx!\n", pkt->getAddr());
mcsquare->stats.destWriteSizeCPU += pkt->getSize();
// Will not create a response. Create a dummy duplicate packet for coherency of CTT
//if(!pkt->needsResponse()) {
//DPRINTF(MCSquare, "Does not need response, create fake packet %lx!\n", pkt->getAddr());
// Create read to src request
auto req = std::make_shared<Request>(pkt->getAddr(),
pkt->getSize(), Request::MEM_ELIDE_REDIRECT_SRC,
Request::funcRequestorId);
if(pkt->req->getFlags() & Request::UNCACHEABLE)
req->setFlags(Request::UNCACHEABLE);
auto bouncePkt = Packet::createWrite(req);
bouncePkt->allocate();
bouncePkt->makeResponse();
port.schedTimingResp(bouncePkt, curTick());
//}
}
if(mcsquare->isSrc(pkt)) {
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
// Not this memctrl's problem. Just return true.
if(!weContain)
return true;
if(pkt->req->getFlags() & Request::MEM_ELIDE_WRITE_SRC) {
mcsquare->stats.srcWritesBlocked++;
DPRINTF(MCSquare, "Found write to src %lx, waiting for bounce to complete\n",
pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
return false;
}
// If already generated write to src, ignore this request
if(mcsquare->m_bpq.find(pkt->getAddr())) {
mcsquare->m_bpq.setData(pkt->getAddr(), pkt->getPtr<uint8_t>());
DPRINTF(MCSquare, "Found write to src %lx in bounce table\n",
pkt->getAddr());
accessAndRespond(pkt,
frontendLatency + mcsquare->getBPQPenalty(), dram);
return true;
}
// Exceeded BPQ size
if(mcsquare->m_bpq.size() >= mcsquare->getMaxBPQSize()) {
mcsquare->stats.srcWritesBlocked++;
DPRINTF(MCSquare, "Exceeded max BPQ size! Cannot add %lx\n", pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
return false;
}
// Fresh write to source, generate a read req
// See if we have space in read queue for read to src
unsigned size = pkt->getSize();
uint32_t burst_size = dram->bytesPerBurst();
unsigned offset = pkt->getAddr() & (burst_size - 1);
unsigned int pkt_count = divCeil(offset + size, burst_size);
if (readQueueFull(pkt_count)) {
mcsquare->stats.srcWritesBlocked++;
DPRINTF(MCSquare, "Found write to src %lx, read queue full\n",
pkt->getAddr());
retryWrReq = true;
stats.numWrRetry++;
srcWritePause = true;
return false;
}
mcsquare->m_bpq.insert(pkt->getAddr(), pkt->getPtr<uint8_t>());
DPRINTF(MCSquare, "Inserted src %lx, BPQ size %ld\n",
pkt->getAddr(), mcsquare->m_bpq.size());
// Create read to src request
pkt->req->setFlags(Request::MEM_ELIDE_WRITE_SRC);
auto req = std::make_shared<Request>(pkt->getAddr(),
pkt->getSize(), Request::MEM_ELIDE_WRITE_SRC,
pkt->req->funcRequestorId);
auto bouncePkt = Packet::createRead(req);
bouncePkt->allocate();
if (!addToReadQueue(bouncePkt, pkt_count, dram)) {
// If we are not already scheduled to get a request out of the
// queue, do so now
if (!nextReqEvent.scheduled()) {
DPRINTF(MemCtrl, "Request scheduled immediately\n");
schedule(nextReqEvent, curTick());
}
}
stats.readReqs++;
stats.bytesReadSys += size;
// Now return until the final write to dest(s) is complete
mcsquare->stats.srcWriteSizeCPU += pkt->getSize();
accessAndRespond(pkt, frontendLatency + mcsquare->getBPQPenalty(), dram);
return true;
}
} else if(pkt->isRead()) {
if(mcsquare->isDest(pkt)) {
DPRINTF(MCSquare, "Found a read to dest %lx, applying elision penalty\n", pkt->getAddr());
pkt->req->_paddr_dest = pkt->getAddr();
mcsquare->bounceAddr(pkt);
mcsquare->stats.destReadSizeCPU += pkt->getSize();
pkt->req->setFlags(Request::MEM_ELIDE_REDIRECT_SRC);
// Push pkt to redirected source
if(pkt->req->_paddr_dest != pkt->getAddr()) {
Tick response_time = curTick() + mcsquare->getCTTPenalty() + pkt->headerDelay;
// Here we reset the timing of the packet before sending it out.
pkt->headerDelay = pkt->payloadDelay = 0;
pkt->makeResponse();
port.schedTimingResp(pkt, response_time);
return true;
}
} else if(mcsquare->isSrc(pkt)) {
mcsquare->stats.srcReadSizeCPU += pkt->getSize();
//DPRINTF(MCSquare, "Found read to src %lx; fallthrough\n", pkt->getAddr());
}
}
}
if(isMCReq(pkt) && pkt->isWrite()) {
bool weContain = false;
AddrRangeList addrList = getAddrRanges();
for(auto i = addrList.begin(); i != addrList.end(); ++i)
if(i->contains(pkt->getAddr())) {
weContain = true;
break;
}
if(pkt->req->getFlags() & Request::MEM_ELIDE_REDIRECT_SRC) {
mcsquare->m_bpq.decPkts(pkt->req->_paddr_src);
if(mcsquare->ctt_freeing.find(pkt->req->_paddr_src) != mcsquare->ctt_freeing.end() &&
mcsquare->ctt_freeing[pkt->req->_paddr_src] > 0) {
mcsquare->ctt_freeing[pkt->req->_paddr_src]--;
DPRINTF(MCSquare, "Decrementing ctt_count to %d\n", mcsquare->ctt_freeing[pkt->req->_paddr_src]);
}
checkBounceTable();
// Sent as a write to dest. Just acknowledge and ignore.
if(!mcsquare->isDest(pkt)) {
if(weContain) {
DPRINTF(MCSquare, "Found duplicate generated write to %lx, "
"ignoring this one (%lx)\n", pkt->getAddr(), pkt->req->_paddr_src);
delete pkt;
}
return true;
}
mcsquare->splitEntry(pkt);
clearCTT();
if(!weContain)
return true;
}
// Writeback generated for a read dest. Accomodate if space allows.
if(pkt->req->getFlags() & Request::MEM_ELIDE_DEST_WB) {
// Sent as a write to dest. Just ignore.
if(!mcsquare->isDest(pkt)) {
if(weContain) {
DPRINTF(MCSquare, "Found duplicate generated write to %lx, "
"ignoring this one (%lx)\n", pkt->getAddr(), pkt->req->_paddr_src);
}
return false;
}
// See if we can accommodate based on wb option
bool hasSpace = true;
switch(mcsquare->wbDestReads()) {
case 0: // Should not see these packets generated for this opt
assert(false);
break;
case 1: // Allow packets as long as space available
hasSpace = totalWriteQueueSize < writeBufferSize;
break;
case 2: // Allow if less than 50% full
hasSpace = totalWriteQueueSize < writeBufferSize / 2;
break;
case 3: // Allow if less than 75% full
hasSpace = totalWriteQueueSize < 3 * writeBufferSize / 4;
break;
case 4: // Allow if less than 90% full
hasSpace = totalWriteQueueSize < 9 * writeBufferSize / 10;
break;
default:
fprintf(stderr, "Invalid wb_dest_reads option for MCSquare\n");
assert(false);
break;
}
if(weContain && !hasSpace) {
DPRINTF(MCSquare, "Found gen write to %lx, but cannot fit"
" in WPQ\n", pkt->getAddr());
return false;
}
mcsquare->stats.destWriteSizeBounce += pkt->getSize();
// We have space. Let this proceed!
mcsquare->splitEntry(pkt);
checkBounceTable();
clearCTT();
if(!weContain)
return true;
}
} else if(mcsquare->m_bpq.find(pkt->getAddr())) {
if(pkt->isWrite()) {
mcsquare->m_bpq.setData(pkt->getAddr(), pkt->getPtr<uint8_t>());
DPRINTF(MCSquare, "Found write to src %lx, in bounce table\n", pkt->getAddr());
accessAndRespond(pkt, frontendLatency + mcsquare->getBPQPenalty(), dram);
return true;
} else if(pkt->isRead() && !(pkt->req->getFlags() & Request::MEM_ELIDE_REDIRECT_SRC)) {
pkt->setData(mcsquare->m_bpq.getData(pkt->getAddr()));
DPRINTF(MCSquare, "Found read to src %lx in bounce table\n", pkt->getAddr());
accessAndRespond(pkt, frontendLatency + mcsquare->getBPQPenalty(), dram);