-
Notifications
You must be signed in to change notification settings - Fork 62
/
Dimm.c
6806 lines (5759 loc) · 201 KB
/
Dimm.c
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) 2018, Intel Corporation.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <Library/UefiBootServicesTableLib.h>
#include <Guid/SmBios.h>
#include <IndustryStandard/SmBios.h>
#include <Types.h>
#include <NvmDimmDriverData.h>
#include <Debug.h>
#include <Utility.h>
#include "Dimm.h"
#include "Namespace.h"
#include <Utility.h>
#include <SmbiosUtility.h>
#include "AsmCommands.h"
#include <NvmWorkarounds.h>
#include <Convert.h>
#include <NvmDimmDriver.h>
#ifdef OS_BUILD
#include <os_types.h>
#endif
#ifndef OS_BUILD
#include "Smbus.h"
#endif
#define SMBIOS_TYPE_MEM_DEV 17
#define SMBIOS_TYPE_MEM_DEV_MAPPED_ADDR 20
#ifdef PCD_CACHE_ENABLED
int gPCDCacheEnabled = 1;
#else
int gPCDCacheEnabled = 0;
#endif
extern NVMDIMMDRIVER_DATA *gNvmDimmData;
CONST UINT64 gSupportedBlockSizes[SUPPORTED_BLOCK_SIZES_COUNT] = {
512, // 512 (default)
514, // 512+2 (DIX)
520, // 512+8
528, // 512+16
4096, // 512*8
4112, // (512+2)*8 (DIX)
4160, // (512+8)*8
4224 // (512+16)*8
};
#ifdef OS_BUILD
/*
* Function get the ini configuration only on the first call
*
* It returns TRUE in case of large payload access is disabled and FALSE otherwise
*/
BOOLEAN config_is_large_payload_disabled()
{
static BOOLEAN config_large_payload_initialized = FALSE;
static UINT8 large_payload_disabled = 0;
EFI_STATUS efi_status;
EFI_GUID guid = { 0 };
UINTN size;
if (config_large_payload_initialized)
return large_payload_disabled;
size = sizeof(large_payload_disabled);
efi_status = GET_VARIABLE(INI_PREFERENCES_LARGE_PAYLOAD_DISABLED, guid, &size, &large_payload_disabled);
if ((EFI_SUCCESS != efi_status) || (large_payload_disabled > 1))
return FALSE;
config_large_payload_initialized = TRUE;
return (BOOLEAN)large_payload_disabled;
}
#endif // OS_BUILD
/**
Global pointers to the new processor assembler commands:
gClFlush has more than one implementation and we should store here the newest that the processor supports.
If the pointers are still NULL - the processor does not support any of the existing implementations.
**/
VOID
(*gClFlush)(
VOID *pLinearAddress
);
#ifndef OS_BUILD
struct {
UINT32 Eax;
union {
struct {
UINT32 Unused:23;
BOOLEAN ClFlushOpt:1; // EBX.CLFLUSHOPT[bit 23]
BOOLEAN ClWb:1; // EBX.CLWB[bit 24]
UINT32 Unused2:7;
} Separated;
UINT32 AsUint32;
} Ebx;
UINT32 Ecx;
UINT32 Edx;
} CpuInfo;
/**
InitializeCpuCommands
Checks what set of required instructions current processor supports and assigns proper function pointers.
The detection of new instructions is made, following the document: Ref # 319433-022, chapter 11-1.
**/
STATIC
VOID
InitializeCpuCommands(
)
{
SetMem(&CpuInfo, sizeof(CpuInfo), 0x0);
AsmCpuidEcx(CPUID_NEWMEM_FUNCTIONS_EAX, CPUID_NEWMEM_FUNCTIONS_ECX,
(UINT32 *)&CpuInfo.Eax, (UINT32 *)&CpuInfo.Ebx, (UINT32 *)&CpuInfo.Ecx, (UINT32 *)&CpuInfo.Edx);
if (CpuInfo.Ebx.Separated.ClFlushOpt) {
gClFlush = &AsmClFlushOpt;
NVDIMM_DBG("Flushing assigned to ClFlushOpt.");
} else {
NVDIMM_DBG("Flushing assigned to ClFlush.");
gClFlush = &AsmFlushCl;
}
}
#endif /** !OS_BUILD **/
STATIC EFI_STATUS PollOnArsDeviceBusy(IN DIMM *pDimm, IN UINT32 TimeoutSecs);
/**
Get dimm by Dimm ID
Scan the dimm list for a dimm identified by Dimm ID
@param[in] DimmID: The SMBIOS Type 17 handle of the dimm
@param[in] pDimms: The head of the dimm list
@retval DIMM struct pointer if matching dimm has been found
@retval NULL pointer if not found
**/
DIMM *
GetDimmByPid(
IN UINT32 DimmID,
IN LIST_ENTRY *pDimms
)
{
DIMM *pCurDimm = NULL;
DIMM *pTargetDimm = NULL;
LIST_ENTRY *pCurDimmNode = NULL;
NVDIMM_ENTRY();
for (pCurDimmNode = GetFirstNode(pDimms);
!IsNull(pDimms, pCurDimmNode);
pCurDimmNode = GetNextNode(pDimms, pCurDimmNode)) {
pCurDimm = DIMM_FROM_NODE(pCurDimmNode);
if (DimmID == pCurDimm->DimmID) {
pTargetDimm = pCurDimm;
break;
}
}
NVDIMM_EXIT();
return pTargetDimm;
}
/**
Get dimm by serial number
Scan the dimm list for a dimm identified by serial number
@param[in] pDimms The head of the dimm list
@param[in] DimmID The serial number of the dimm
@retval DIMM struct pointer if matching dimm has been found
@retval NULL pointer if not found
**/
DIMM *
GetDimmByHandle(
IN UINT32 DeviceHandle,
IN LIST_ENTRY *pDimms
)
{
DIMM *pCurDimm = NULL;
DIMM *pTargetDimm = NULL;
LIST_ENTRY *pCurDimmNode = NULL;
NVDIMM_ENTRY();
for (pCurDimmNode = GetFirstNode(pDimms);
!IsNull(pDimms, pCurDimmNode);
pCurDimmNode = GetNextNode(pDimms, pCurDimmNode)) {
pCurDimm = DIMM_FROM_NODE(pCurDimmNode);
if (DeviceHandle == pCurDimm->DeviceHandle.AsUint32) {
pTargetDimm = pCurDimm;
break;
}
}
NVDIMM_EXIT();
return pTargetDimm;
}
/**
Get dimm by serial number
Scan the dimm list for a dimm identified by serial number
@param[in] pDimms The head of the dimm list
@param[in] DimmID The serial number of the dimm
@retval DIMM struct pointer if matching dimm has been found
@retval NULL pointer if not found
**/
DIMM *
GetDimmBySerialNumber(
IN LIST_ENTRY *pDimms,
IN UINT32 SerialNumber
)
{
DIMM *pCurDimm = NULL;
DIMM *pTargetDimm = NULL;
LIST_ENTRY *pCurDimmNode = NULL;
NVDIMM_ENTRY();
LIST_FOR_EACH(pCurDimmNode, pDimms) {
pCurDimm = DIMM_FROM_NODE(pCurDimmNode);
if (pCurDimm->SerialNumber == SerialNumber) {
pTargetDimm = pCurDimm;
break;
}
}
NVDIMM_EXIT();
return pTargetDimm;
}
/**
Get dimm by its unique identifier structure
Scan the dimm list for a dimm identified by its
unique identifier structure
@param[in] pDimms The head of the dimm list
@param[in] DimmUniqueId The unique identifier structure of the dimm
@retval DIMM struct pointer if matching dimm has been found
@retval NULL pointer if not found
**/
DIMM *
GetDimmByUniqueIdentifier(
IN LIST_ENTRY *pDimms,
IN DIMM_UNIQUE_IDENTIFIER DimmUniqueId
)
{
DIMM *pCurDimm = NULL;
DIMM *pTargetDimm = NULL;
LIST_ENTRY *pCurDimmNode = NULL;
NVDIMM_ENTRY();
LIST_FOR_EACH(pCurDimmNode, pDimms) {
pCurDimm = DIMM_FROM_NODE(pCurDimmNode);
if ((pCurDimm->VendorId == DimmUniqueId.ManufacturerId) && (pCurDimm->SerialNumber == DimmUniqueId.SerialNumber) &&
(pCurDimm->ManufacturingInfoValid ? ((pCurDimm->ManufacturingLocation == DimmUniqueId.ManufacturingLocation) &&
(pCurDimm->ManufacturingDate == DimmUniqueId.ManufacturingDate)): TRUE)) {
pTargetDimm = pCurDimm;
break;
}
}
NVDIMM_EXIT();
return pTargetDimm;
}
/**
Get DIMM by index in global structure
@param[in] DimmIndex - Index
@param[in] pDev - pointer to global structure
@retval
**/
DIMM *
GetDimmByIndex(
IN INT32 DimmIndex,
IN PMEM_DEV *pDev
)
{
DIMM *pCurDimm = NULL;
DIMM *pTargetDimm = NULL;
LIST_ENTRY *pCurDimmNode = NULL;
INT32 Index = 0;
NVDIMM_ENTRY();
for (pCurDimmNode = GetFirstNode(&pDev->Dimms);
!IsNull(&pDev->Dimms, pCurDimmNode);
pCurDimmNode = GetNextNode(&pDev->Dimms, pCurDimmNode)) {
pCurDimm = DIMM_FROM_NODE(pCurDimmNode);
if (Index == DimmIndex) {
pTargetDimm = pCurDimm;
break;
}
Index++;
}
NVDIMM_EXIT();
return pTargetDimm;
}
/**
Get max Dimm ID
Scan the dimm list for a max Dimm ID
@param[in] pDimms: The head of the dimm list
@retval Max Dimm ID or 0 if not found
**/
UINT16
GetMaxPid(
IN LIST_ENTRY *pDimms
)
{
UINT16 MaxPid = 0;
DIMM *pCurDimm = NULL;
LIST_ENTRY *pCurDimmNode = NULL;
NVDIMM_ENTRY();
if (pDimms == NULL) {
goto Finish;
}
LIST_FOR_EACH(pCurDimmNode, pDimms) {
pCurDimm = DIMM_FROM_NODE(pCurDimmNode);
if (pCurDimm->DimmID > MaxPid) {
MaxPid = pCurDimm->DimmID;
}
}
Finish:
NVDIMM_EXIT();
return MaxPid;
}
/**
Print memory map list. Use for debug purposes only
@param[in] pMemmap: List head containing memmap range items
**/
VOID
PrintDimmMemmap(
IN LIST_ENTRY *pMemmap
)
{
LIST_ENTRY *pNode = NULL;
MEMMAP_RANGE *pRange = NULL;
UINT16 Index = 0;
NVDIMM_ENTRY();
if (pMemmap == NULL) {
return;
}
NVDIMM_DBG("DIMM Memmap:");
//display the memmap
LIST_FOR_EACH(pNode, pMemmap) {
pRange = MEMMAP_RANGE_FROM_NODE(pNode);
Index++;
NVDIMM_DBG("#%d %12llx - %12llx (%12llx) ", Index,
pRange->RangeStartDpa,
pRange->RangeStartDpa + pRange->RangeLength - 1,
pRange->RangeLength);
switch (pRange->RangeType) {
case MEMMAP_RANGE_VOLATILE:
NVDIMM_DBG("VOLATILE\n");
break;
case MEMMAP_RANGE_RESERVED:
NVDIMM_DBG("RESERVED\n");
break;
case MEMMAP_RANGE_PERSISTENT:
NVDIMM_DBG("PERSISTENT\n");
break;
case MEMMAP_RANGE_IS:
NVDIMM_DBG("INTERLEAVE SET\n");
break;
case MEMMAP_RANGE_IS_MIRROR:
NVDIMM_DBG("MIRRORED INTERLEAVE SET\n");
break;
case MEMMAP_RANGE_BLOCK_NAMESPACE:
NVDIMM_DBG("BLOCK NAMESPACE\n");
break;
case MEMMAP_RANGE_IS_NOT_INTERLEAVED:
NVDIMM_DBG("IS_NOT_INTERLEAVED\n");
break;
case MEMMAP_RANGE_STORAGE_ONLY:
NVDIMM_DBG("STORAGE_ONLY\n");
break;
case MEMMAP_RANGE_APPDIRECT_NAMESPACE:
NVDIMM_DBG("APPDIRECT NAMESPACE\n");
break;
case MEMMAP_RANGE_LAST_USABLE_DPA:
NVDIMM_DBG("LAST USABLE DPA\n");
break;
case MEMMAP_RANGE_FREE:
NVDIMM_DBG("FREE\n");
break;
default:
NVDIMM_DBG("UNKNOWN\n");
break;
}
}
NVDIMM_EXIT();
}
VOID
ShowDimmMemmap(
IN DIMM *pDimm
)
{
LIST_ENTRY *pMemmapList = NULL;
NVDIMM_ENTRY();
if (pDimm == NULL) {
goto Finish;
}
pMemmapList = AllocateZeroPool(sizeof(*pMemmapList));
if (pMemmapList == NULL) {
goto Finish;
}
InitializeListHead(pMemmapList);
GetDimmMemmap(pDimm, pMemmapList);
PrintDimmMemmap(pMemmapList);
Finish:
if (pMemmapList != NULL) {
FreeMemmapItems(pMemmapList);
FREE_POOL_SAFE(pMemmapList);
}
NVDIMM_EXIT();
}
/**
Add DIMM address space region to a linked list in appropriate place
making sure target list will be already sorted by start DPA
Function allocates memory for object with range item. It's caller
responsibility to free this memory after it's no longer needed
@param[in] pMemmapList Initialized list head to which region items will be added
@param[in] pDimm Target DIMM structure pointer
@param[in] Start Start address of a address range to be added
@param[in] Length Length of address range to be added
@param[in] Type of the range to be added (Interleave Set, Namespace, etc.)
@retval EFI_INVALID_PARAMETER Invalid set of parameters provided
@retval EFI_OUT_OF_RESOURCES Not enough free space on target
@retval EFI_SUCCESS List correctly retrieved
**/
EFI_STATUS
AddMemmapRange(
IN LIST_ENTRY *pMemmapList,
IN DIMM *pDimm,
IN UINT64 Start,
IN UINT64 Length,
IN UINT32 Type
)
{
EFI_STATUS ReturnCode = EFI_INVALID_PARAMETER;
MEMMAP_RANGE *pMemmapRange = NULL;
MEMMAP_RANGE *pCurrentRange = NULL;
MEMMAP_RANGE *pNextRange = NULL;
LIST_ENTRY *pNode = NULL;
LIST_ENTRY *pNextNode = NULL;
BOOLEAN Added = FALSE;
if (pMemmapList == NULL || pDimm == NULL) {
goto Finish;
}
pMemmapRange = (MEMMAP_RANGE *) AllocateZeroPool(sizeof(*pMemmapRange));
if (pMemmapRange == NULL) {
ReturnCode = EFI_OUT_OF_RESOURCES;
goto Finish;
}
pMemmapRange->Signature = MEMMAP_RANGE_SIGNATURE;
pMemmapRange->pDimm = pDimm;
pMemmapRange->RangeType = (UINT16)Type;
pMemmapRange->RangeStartDpa = Start;
pMemmapRange->RangeLength = Length;
NVDIMM_VERB("New memmap range: start=%x length=%x", Start, Length);
LIST_FOR_EACH(pNode, pMemmapList) {
pCurrentRange = MEMMAP_RANGE_FROM_NODE(pNode);
if (IsNodeAtEnd(pMemmapList, pNode)) {
if (pMemmapRange->RangeStartDpa >= pCurrentRange->RangeStartDpa) {
/** pMemmapRange->MemmapNode will be inserted after pNode, because pNode is treated as list head **/
InsertHeadList(pNode, &pMemmapRange->MemmapNode);
NVDIMM_VERB("Add after the last node.");
} else {
/** pMemmapRange->MemmapNode will be inserted before pNode, because pNode is treated as list head **/
InsertTailList(pNode, &pMemmapRange->MemmapNode);
NVDIMM_VERB("Add before the last node.");
}
Added = TRUE;
break;
}
pNextNode = GetNextNode(pMemmapList, pNode);
pNextRange = MEMMAP_RANGE_FROM_NODE(pNextNode);
if (pMemmapRange->RangeStartDpa >= pCurrentRange->RangeStartDpa &&
pMemmapRange->RangeStartDpa < pNextRange->RangeStartDpa) {
InsertHeadList(pNode, &pMemmapRange->MemmapNode);
NVDIMM_VERB("Added in the middle");
Added = TRUE;
break;
}
}
if (!Added) {
InsertTailList(pMemmapList, &pMemmapRange->MemmapNode);
NVDIMM_VERB("Added at tail");
}
ReturnCode = EFI_SUCCESS;
Finish:
NVDIMM_EXIT_I64(ReturnCode);
return ReturnCode;
}
#define MAX_STORAGE_ONLY_RANGES (MAX_IS_PER_DIMM + 1)
/**
Retrieve list of memory regions of a DIMM
Regions will be delivered in a form of sorted linked list with
items containing start DPA and length of free ranges and they may overlap.
Last item on the list will be a last DPA marker in order to point address boundary.
@param[in] pDimm Target DIMM structure pointer
@param[out] pMemmap Initialized list head to which region items will be added
@retval EFI_INVALID_PARAMETER Invalid set of parameters provided
@retval EFI_OUT_OF_RESOURCES Not enough free space on target
@retval EFI_SUCCESS List correctly retrieved
**/
EFI_STATUS
GetDimmMemmap(
IN DIMM *pDimm,
OUT LIST_ENTRY *pMemmap
)
{
EFI_STATUS ReturnCode = EFI_INVALID_PARAMETER;
UINT32 Index = 0;
UINT64 Offset = 0;
DIMM_REGION *pDimmRegion = NULL;
NAMESPACE *pNamespace = NULL;
LIST_ENTRY *pNode = NULL;
LIST_ENTRY *pNode2 = NULL;
struct _NVM_IS *pIS = NULL;
UINT32 RangeType = 0;
UINT64 Length = 0;
UINT32 RegionCount = 0;
BOOLEAN ISetInterleaved = FALSE;
UINT32 Type = 0;
struct {
UINT64 StartDpa;
UINT64 EndDpa;
} StorageOnly[MAX_STORAGE_ONLY_RANGES];
UINT32 StorageOnlyNum = 0;
UINT64 ISStart = 0;
UINT64 ISEnd = 0;
UINT64 StStart = 0;
UINT64 StEnd = 0;
NVDIMM_ENTRY();
ZeroMem(StorageOnly, sizeof(StorageOnly));
if (pDimm == NULL || pMemmap == NULL) {
goto Finish;
}
/**
Volatile Partition might not start at DPA 0.
For safety let's treat area starting at DPA 0 as Reserved
**/
if (pDimm->VolatileStart > 0) {
AddMemmapRange(pMemmap, pDimm, 0, pDimm->VolatileStart, MEMMAP_RANGE_RESERVED);
}
/** Volatile Partition **/
if (pDimm->VolatileCapacity > 0) {
AddMemmapRange(pMemmap, pDimm, pDimm->VolatileStart, pDimm->VolatileCapacity, MEMMAP_RANGE_VOLATILE);
}
/** Persistent Partition **/
if (pDimm->PmCapacity > 0) {
AddMemmapRange(pMemmap, pDimm, pDimm->PmStart, pDimm->PmCapacity, MEMMAP_RANGE_PERSISTENT);
}
/** At the end of Dimm may be reserved area **/
Offset = pDimm->VolatileStart + pDimm->VolatileCapacity + pDimm->PmCapacity;
Length = pDimm->RawCapacity - Offset;
if (Length > 0) {
AddMemmapRange(pMemmap, pDimm, Offset, Length, MEMMAP_RANGE_RESERVED);
}
/** Init storage only regions **/
if (pDimm->PmCapacity > 0) {
StorageOnly[0].StartDpa = pDimm->PmStart;
StorageOnly[0].EndDpa = pDimm->PmStart + pDimm->PmCapacity;
StorageOnlyNum = 1;
}
/** Interleave Sets **/
LIST_FOR_EACH(pNode, &gNvmDimmData->PMEMDev.ISs) {
pIS = IS_FROM_NODE(pNode);
ReturnCode = GetListSize(&pIS->DimmRegionList, &RegionCount);
if (EFI_ERROR(ReturnCode) || RegionCount == 0) {
goto Finish;
}
ISetInterleaved = RegionCount > 1;
LIST_FOR_EACH(pNode2, &pIS->DimmRegionList) {
pDimmRegion = DIMM_REGION_FROM_NODE(pNode2);
if (pDimmRegion->pDimm != pDimm) {
continue;
}
Offset = pDimm->PmStart + pDimmRegion->PartitionOffset;
if (pIS->MirrorEnable) {
Type = MEMMAP_RANGE_IS_MIRROR;
} else if (ISetInterleaved) {
Type = MEMMAP_RANGE_IS;
} else {
Type = MEMMAP_RANGE_IS_NOT_INTERLEAVED;
}
AddMemmapRange(pMemmap, pDimm, Offset, pDimmRegion->PartitionSize, Type);
/** Subtract interleave sets to get storage only regions **/
ISStart = Offset;
ISEnd = ISStart + pDimmRegion->PartitionSize;
for (Index = 0; Index < StorageOnlyNum; Index++) {
StStart = StorageOnly[Index].StartDpa;
StEnd = StorageOnly[Index].EndDpa;
if (ISStart >= StStart && ISEnd <= StEnd) {
if (ISStart > StStart && ISEnd < StEnd) {
/** Split storage only range **/
if ((Index + 1) >= MAX_STORAGE_ONLY_RANGES) {
NVDIMM_ERR("Critical error: index is out of range. It never should happen.");
goto Finish;
}
StorageOnly[Index + 1].StartDpa = ISEnd;
StorageOnly[Index + 1].EndDpa = StorageOnly[Index].EndDpa;
StorageOnly[Index].EndDpa = ISStart;
StorageOnlyNum++;
} else if (ISStart == StStart && ISEnd < StEnd) {
/** Decrease storage only range **/
StorageOnly[Index].StartDpa = ISEnd;
} else if (ISStart > StStart && ISEnd == StEnd) {
/** Decrease storage only range **/
StorageOnly[Index].EndDpa = ISStart;
}
/** Interleave set region may fit only to one storage region **/
break;
}
}
}
}
/** Add storage only ranges to map **/
for (Index = 0; Index < StorageOnlyNum; Index++) {
AddMemmapRange(pMemmap, pDimm,
StorageOnly[Index].StartDpa,
StorageOnly[Index].EndDpa - StorageOnly[Index].StartDpa,
MEMMAP_RANGE_STORAGE_ONLY);
}
/** Namespaces **/
LIST_FOR_EACH(pNode, &gNvmDimmData->PMEMDev.Namespaces) {
pNamespace = NAMESPACE_FROM_NODE(pNode, NamespaceNode);
RangeType = (pNamespace->NamespaceType == STORAGE_NAMESPACE) ?
MEMMAP_RANGE_BLOCK_NAMESPACE : MEMMAP_RANGE_APPDIRECT_NAMESPACE;
for (Index = 0; Index < pNamespace->RangesCount; Index++) {
if (pNamespace->Range[Index].pDimm != pDimm) {
continue;
}
AddMemmapRange(pMemmap, pDimm,
pNamespace->Range[Index].Dpa,
pNamespace->Range[Index].Size,
RangeType);
}
}
// Set last usable DPA to last PM partition address
Offset = pDimm->PmStart + pDimm->PmCapacity;
AddMemmapRange(pMemmap, pDimm, Offset, 0, MEMMAP_RANGE_LAST_USABLE_DPA);
ReturnCode = EFI_SUCCESS;
#ifdef MDEPKG_NDEBUG
PrintDimmMemmap(pMemmap);
#endif
Finish:
NVDIMM_EXIT_I64(ReturnCode);
return ReturnCode;
}
/**
Retrieve list of free regions of a DIMM based on capacity type
Free regions will be delivered in a form of sorted linked list with
items containing start DPA and length of free ranges and they don't overlap each other
@param[in] pDimm Target DIMM structure pointer
@param[in] FreeCapacityTypeArg Determine a type of free capacity
@param[out] pFreemap Initialized list head to which region items will be added
@retval EFI_INVALID_PARAMETER Invalid set of parameters provided
@retval EFI_OUT_OF_RESOURCES Not enough free space on target
@retval EFI_SUCCESS List correctly retrieved
**/
EFI_STATUS
GetDimmFreemap(
IN DIMM *pDimm,
IN FreeCapacityType FreeCapacityTypeArg,
OUT LIST_ENTRY *pFreemap
)
{
EFI_STATUS ReturnCode = EFI_INVALID_PARAMETER;
MEMMAP_RANGE *pMemmapRange = NULL;
LIST_ENTRY *pMemmapList = NULL;
LIST_ENTRY *pUsableRanges = NULL;
LIST_ENTRY *pOccupiedRanges = NULL;
LIST_ENTRY *pNode = NULL;
NVDIMM_ENTRY();
if (pDimm == NULL || pFreemap == NULL) {
goto Finish;
}
pMemmapList = (LIST_ENTRY *) AllocateZeroPool(sizeof(*pMemmapList));
if (pMemmapList == NULL) {
ReturnCode = EFI_OUT_OF_RESOURCES;
goto Finish;
}
InitializeListHead(pMemmapList);
pUsableRanges = (LIST_ENTRY *) AllocateZeroPool(sizeof(*pUsableRanges));
if (pUsableRanges == NULL) {
ReturnCode = EFI_OUT_OF_RESOURCES;
goto Finish;
}
InitializeListHead(pUsableRanges);
pOccupiedRanges = (LIST_ENTRY *) AllocateZeroPool(sizeof(*pOccupiedRanges));
if (pOccupiedRanges == NULL) {
ReturnCode = EFI_OUT_OF_RESOURCES;
goto Finish;
}
InitializeListHead(pOccupiedRanges);
ReturnCode = GetDimmMemmap(pDimm, pMemmapList);
if (EFI_ERROR(ReturnCode)) {
goto Finish;
}
LIST_FOR_EACH(pNode, pMemmapList) {
pMemmapRange = MEMMAP_RANGE_FROM_NODE(pNode);
/**
Make list of ranges that can be used for specified mode. For example AppDirect Namespaces can be created only on
Interleave Sets.
Ranges may overlap and they will be sorted by DPA start address.
**/
if (pMemmapRange->RangeType == MEMMAP_RANGE_PERSISTENT) {
if (FreeCapacityTypeArg == FreeCapacityForPersistentRegion ||
FreeCapacityTypeArg == FreeCapacityForStMode ||
FreeCapacityTypeArg == FreeCapacityForStModeOnStOnly) {
AddMemmapRange(pUsableRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
}
} else if (pMemmapRange->RangeType == MEMMAP_RANGE_IS_MIRROR) {
if (FreeCapacityTypeArg == FreeCapacityForMirrorRegion ||
FreeCapacityTypeArg == FreeCapacityForADMode) {
AddMemmapRange(pUsableRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
}
} else if (pMemmapRange->RangeType == MEMMAP_RANGE_IS) {
if (FreeCapacityTypeArg == FreeCapacityForADMode ||
FreeCapacityTypeArg == FreeCapacityForStModeOnInterleaved) {
AddMemmapRange(pUsableRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
}
} else if (pMemmapRange->RangeType == MEMMAP_RANGE_IS_NOT_INTERLEAVED) {
if (FreeCapacityTypeArg == FreeCapacityForADMode ||
FreeCapacityTypeArg == FreeCapacityForStModeOnNotInterleaved) {
AddMemmapRange(pUsableRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
}
}
/**
Make list of used ranges for specified mode. For example Block Namespaces can't be created on mirrored
Interleave Sets, Block and AppDirect Namespaces.
Ranges may overlap and they will be sorted by DPA start address.
**/
if (pMemmapRange->RangeType == MEMMAP_RANGE_BLOCK_NAMESPACE ||
pMemmapRange->RangeType == MEMMAP_RANGE_APPDIRECT_NAMESPACE) {
AddMemmapRange(pOccupiedRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
} else if (pMemmapRange->RangeType == MEMMAP_RANGE_IS_MIRROR) {
if (FreeCapacityTypeArg == FreeCapacityForPersistentRegion ||
FreeCapacityTypeArg == FreeCapacityForStMode ||
FreeCapacityTypeArg == FreeCapacityForStModeOnStOnly) {
AddMemmapRange(pOccupiedRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
}
} else if (pMemmapRange->RangeType == MEMMAP_RANGE_IS ||
pMemmapRange->RangeType == MEMMAP_RANGE_IS_NOT_INTERLEAVED) {
if (FreeCapacityTypeArg == FreeCapacityForStModeOnStOnly) {
AddMemmapRange(pOccupiedRanges, pMemmapRange->pDimm, pMemmapRange->RangeStartDpa, pMemmapRange->RangeLength,
pMemmapRange->RangeType);
}
}
}
/** Get non-overlapped free ranges **/
ReturnCode = FindFreeRanges(pUsableRanges, pOccupiedRanges, pFreemap);
if (EFI_ERROR(ReturnCode)) {
goto Finish;
}
ReturnCode = EFI_SUCCESS;
Finish:
if (pOccupiedRanges != NULL) {
FreeMemmapItems(pOccupiedRanges);
FREE_POOL_SAFE(pOccupiedRanges);
}
if (pUsableRanges != NULL) {
FreeMemmapItems(pUsableRanges);
FREE_POOL_SAFE(pUsableRanges);
}
if (pMemmapList != NULL) {
FreeMemmapItems(pMemmapList);
FREE_POOL_SAFE(pMemmapList);
}
NVDIMM_EXIT_I64(ReturnCode);
return ReturnCode;
}
/**
Free resources of memmap list items
@param[in, out] pMemmapList Memmap list that items will be freed for
**/
VOID
FreeMemmapItems(
IN OUT LIST_ENTRY *pMemmapList
)
{
MEMMAP_RANGE *pMemmapRange = NULL;
LIST_ENTRY *pNode = NULL;
LIST_ENTRY *pNext = NULL;
NVDIMM_ENTRY();
if (pMemmapList == NULL) {
goto Finish;
}
LIST_FOR_EACH_SAFE(pNode, pNext, pMemmapList) {
pMemmapRange = MEMMAP_RANGE_FROM_NODE(pNode);
RemoveEntryList(pNode);
FREE_POOL_SAFE(pMemmapRange);
}
Finish:
NVDIMM_EXIT();
}
/**
Merge overlapped ranges
Memmap ranges may overlap each other. This function merges overlapped ranges to continuous ranges.
Input list has to be sorted by DPA start address. Returned list will be sorted as well.
The caller is responsible for a memory deallocation of the returned list.
@param[in] pMemmapList Initialized list of ranges to merge.
@param[out] pMergedList Initialized, output list to fill with continuous ranges.
@retval EFI_SUCCESS Success
@retval EFI_INVALID_PARAMETER One or more parameters are NULL
@retval EFI_OUT_OF_RESOURCES Memory allocation failure
**/
EFI_STATUS
MergeMemmapItems(
IN LIST_ENTRY *pMemmapList,
OUT LIST_ENTRY *pMergedList
)
{
EFI_STATUS ReturnCode = EFI_SUCCESS;
LIST_ENTRY *pNode = NULL;
MEMMAP_RANGE *pMemmapRange = NULL;
UINT32 Index = 0;
DIMM *pDimm = NULL;
UINT64 RangeStartDpa = 0;
UINT64 RangeEndDpa = 0;
UINT64 RangeLength = 0;
NVDIMM_ENTRY();
if (pMemmapList == NULL || pMergedList == NULL) {
ReturnCode = EFI_INVALID_PARAMETER;
goto Finish;
}
if (!IsListEmpty(pMemmapList)) {
Index = 0;
LIST_FOR_EACH(pNode, pMemmapList) {
pMemmapRange = MEMMAP_RANGE_FROM_NODE(pNode);
if (Index == 0) {
pDimm = pMemmapRange->pDimm;
RangeStartDpa = pMemmapRange->RangeStartDpa;
RangeLength = pMemmapRange->RangeLength;
/**
The End DPA will always be 1 less than the value obtained by
adding the Range-Length to the Start DPA.
**/
RangeEndDpa = pMemmapRange->RangeStartDpa + pMemmapRange->RangeLength - 1;
} else if (pMemmapRange->RangeStartDpa <= RangeEndDpa) {
/** Merging ranges **/
if ((pMemmapRange->RangeStartDpa + pMemmapRange->RangeLength - 1) > RangeEndDpa) {
RangeEndDpa = pMemmapRange->RangeStartDpa + pMemmapRange->RangeLength - 1;
RangeLength = RangeEndDpa - RangeStartDpa + 1;
}
} else {
/** Separate, non-overlapped range **/
AddMemmapRange(pMergedList, pDimm, RangeStartDpa, RangeLength, MEMMAP_RANGE_UNDEFINED);
RangeStartDpa = pMemmapRange->RangeStartDpa;
RangeLength = pMemmapRange->RangeLength;
RangeEndDpa = pMemmapRange->RangeStartDpa + pMemmapRange->RangeLength - 1;
}
Index++;
}
AddMemmapRange(pMergedList, pDimm, RangeStartDpa, RangeLength, MEMMAP_RANGE_UNDEFINED);
}
Finish:
NVDIMM_EXIT_I64(ReturnCode);
return ReturnCode;
}
/**
Find free ranges
Take list of usable ranges and subtract occupied ranges. The result will be list of free ranges.
Input lists have to be sorted by DPA start address. Returned list will be sorted as well.
The caller is responsible for a memory deallocation of the returned list.
@param[in] pUsableRangesList Initialized list of usable ranges.
@param[in] pOccupiedRangesList Initialized list of occupied ranges to subtract.
@param[out] pFreeRangesList Initialized, output list to fill with free ranges.
@retval EFI_SUCCESS Success
@retval EFI_INVALID_PARAMETER One or more parameters are NULL
@retval EFI_OUT_OF_RESOURCES Memory allocation failure
**/
EFI_STATUS
FindFreeRanges(
IN LIST_ENTRY *pUsableRangesList,
IN LIST_ENTRY *pOccupiedRangesList,