summaryrefslogtreecommitdiffstats
path: root/src/usr/hwpf/hwp/mc_config/mss_freq/mss_freq.C
blob: 18a4bbc24a9d48f0cc907edb6bac4ef2786adced (plain)
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
1001
1002
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/hwpf/hwp/mc_config/mss_freq/mss_freq.C $              */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2014              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
// $Id: mss_freq.C,v 1.28 2014/04/30 19:32:56 jdsloat Exp $
/* File mss_freq.C created by JEFF SABROWSKI on Fri 21 Oct 2011. */

//------------------------------------------------------------------------------
// *! (C) Copyright International Business Machines Corp. 2007
// *! All Rights Reserved -- Property of IBM
// *! *** IBM Confidential ***
//------------------------------------------------------------------------------
// *! TITLE : mss_freq.C
// *! DESCRIPTION : Tools for centaur procedures
// *! OWNER NAME :   Jacob Sloat (jdsloat@us.ibm.com)
// *! BACKUP NAME :  
// #! ADDITIONAL COMMENTS :
//
// General purpose funcs

//------------------------------------------------------------------------------
// Don't forget to create CVS comments when you check in your changes!
//------------------------------------------------------------------------------
// CHANGE HISTORY:
//------------------------------------------------------------------------------
// Version:|  Author: |   Date:  | Comment:
//---------|----------|----------|----------------------------------------------
//  1.1    | jsabrow  | 09/30/11 | Initial draft.
//  1.2    | bellows  | 12/21/11 | fixed function call to mss_freq
//  1.4    | jsabrow  |
//  1.6    | jdsloat  | 05/03/12 | Fixed per Firmware request, added CL calc
//  1.7    | jdsloat  | 05/03/12 | Uncommented Set Attributes
//  1.8    | jdsloat  | 05/07/12 | fixed per Firmware request
//  1.9    | jdsloat  | 05/07/12 | Unused Variables removed
//  1.11   | jdsloat  | 05/07/12 | Uncommented Set Attributes
//  1.12   | jdsloat  | 05/08/12 | Fixed per Firmware request, fixed CL attribute set, fixed MTB usage.
//  1.13   | jdsloat  | 05/09/12 | Fixed per Firmware request
//  1.14   | jdsloat  | 05/10/12 | Fixed per Firmware Request, RC checks, 0 checks
//  1.15   | jdsloat  | 06/04/12 | Added a Configuration check
//  1.16   | jdsloat  | 06/08/12 | Updates per Firware request
//  1.17   | bellows  | 07/16/12 | added in Id tag
//  1.18   | jdsloat  | 09/07/12 | Added FTB offset to TAA and TCK
//  1.19   | jdsloat  | 01/30/13 | Added Check for l_spd_min_tck_max
//  1.20   | jdsloat  | 02/12/13 | Added path for freq_override
//  1.21   | jdsloat  | 02/12/13 | Added Debug messages
//  1.22   | jdsloat  | 06/27/13 | Fixed overridng RC error that results in coredump on no centaur SPD info.
//  1.23   | jdsloat  | 02/05/14 | Added support for DMI capable frequecies via ATTR_MSS_NEST_CAPABLE_FREQUENCIES
//  1.24   | jdsloat  | 02/18/14 | Added support for DDR4
//  1.25   | jdsloat  | 03/05/14 | RAS review Edits -- Error HW callouts
//  1.26   | jdsloat  | 03/12/14 | Fixed an assignment within a boolean expression.
//  1.27   | jdsloat  | 03/12/14 | Fixed inf loop bug associated with edit 1.26
//  1.28   | jdsloat  | 04/30/14 | Fixed a divide by 0 error opened up by RAS review Edits -- Error HW callouts v1.25
//
// This procedure takes CENTAUR as argument.  for each DIMM (under each MBA)
// DIMM SPD attributes are read to determine optimal DRAM frequency
// frequency bins:  800*, 1066*, 1333, 1600, 1866, 2133, 2400*, 2666*
//    (*=not supported in product as of feb'12)

//----------------------------------------------------------------------
//  Includes - FAPI
//----------------------------------------------------------------------
#include <fapi.H>
#include <mss_freq.H>

//----------------------------------------------------------------------
// ENUMs   
//----------------------------------------------------------------------
enum {
    MSS_FREQ_EMPTY = 0,
    MSS_FREQ_SINGLE_DROP = 1,
    MSS_FREQ_DUAL_DROP = 2,
    MSS_FREQ_VALID = 255,
};

//----------------------------------------------------------------------
//  Constants
//----------------------------------------------------------------------
const uint8_t DDR4_MTB_DIVIDEND = 1;
const uint8_t DDR4_MTB_DIVISOR = 8;
const uint8_t DDR4_FTB_DIVIDEND = 1;
const uint8_t DDR4_FTB_DIVISOR = 1;



using namespace fapi;

fapi::ReturnCode mss_freq(const fapi::Target &i_target_memb)
{

    // Define attribute array size
    const uint8_t PORT_SIZE = 2;
    const uint8_t DIMM_SIZE = 2;

    fapi::ReturnCode l_rc;
    std::vector<fapi::Target> l_mbaChiplets;
    std::vector<fapi::Target> l_dimm_targets;
    std::vector<fapi::Target> l_dimm_targets_deconfig;
    uint8_t l_spd_mtb_dividend=0;
    uint8_t l_spd_mtb_divisor=0;
    uint8_t l_spd_ftb_dividend=0;
    uint8_t l_spd_ftb_divisor=0;
    uint32_t l_dimm_freq_calc=0;
    uint32_t l_dimm_freq_min=9999;
    uint8_t l_spd_min_tck_MTB=0;
    uint8_t l_spd_tck_offset_FTB=0;
    uint8_t l_spd_tck_offset=0;
    uint32_t l_spd_min_tck=0;
    uint32_t l_spd_min_tck_max=0;
    uint8_t  l_spd_min_taa_MTB=0;
    uint8_t  l_spd_taa_offset_FTB=0;
    uint8_t  l_spd_taa_offset=0;
    uint32_t l_spd_min_taa=0;
    uint32_t l_spd_min_taa_max=0;
    uint32_t l_selected_dimm_freq=0;
    uint32_t l_spd_cas_lat_supported = 0xFFFFFFFF;
    uint32_t l_spd_cas_lat_supported_all = 0xFFFFFFFF;
    uint8_t l_cas_latency = 0;
    uint32_t l_cl_mult_tck = 0;
    uint8_t cur_mba_port = 0;
    uint8_t cur_mba_dimm = 0;
    uint8_t cur_dimm_spd_valid_u8array[PORT_SIZE][DIMM_SIZE] = {{0}};
    uint8_t plug_config = 0;
    uint8_t module_type = 0;
    uint8_t module_type_deconfig = 0;
    uint8_t module_type_group_1 = 0;
    uint8_t module_type_group_2 = 0;
    uint8_t module_type_group_1_total = 0;
    uint8_t module_type_group_2_total = 0;
    uint8_t num_ranks = 0;
    uint8_t num_ranks_total = 0;
    uint32_t  l_freq_override = 0;
    uint8_t l_override_path = 0;
    uint8_t l_nest_capable_frequencies = 0; 
    uint8_t l_spd_dram_dev_type;
    uint8_t l_spd_tb_mtb_ddr4=0;
    uint8_t l_spd_tb_ftb_ddr4=0;
    uint8_t l_spd_tckmax_ddr4=0;
    uint8_t cl_count_array[20];
    uint8_t highest_common_cl = 0;
    uint8_t highest_cl_count = 0;
    uint8_t lowest_common_cl = 0;
    uint32_t lowest_cl_count = 0xFFFFFFFF;

    for(uint8_t i=0;i<20;i++) 
    { 
        cl_count_array[i] = 0; // Initializing each element separately 
    } 
    do
    {
        // Get associated MBA's on this centaur                                                                                      
        l_rc=fapiGetChildChiplets(i_target_memb, fapi::TARGET_TYPE_MBA_CHIPLET, l_mbaChiplets);
        if (l_rc)
        {
            FAPI_ERR("Error Getting MBA targets.");
            break;
        }
        // Loop through the 2 MBA's                                                                                                  
        for (uint32_t i=0; i < l_mbaChiplets.size(); i++)
        {
            // Get a vector of DIMM targets                                                                                          
            l_rc = fapiGetAssociatedDimms(l_mbaChiplets[i], l_dimm_targets);
            if (l_rc)
            {
                FAPI_ERR("Error Getting DIMM targets.");
                break;
            }
            for (uint32_t j=0; j < l_dimm_targets.size(); j++)
            {

                l_rc = FAPI_ATTR_GET(ATTR_SPD_DRAM_DEVICE_TYPE, &l_dimm_targets[j], l_spd_dram_dev_type);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read SPD Dram Device Type.");
                    break;
                }
                if (l_spd_dram_dev_type == fapi::ENUM_ATTR_SPD_DRAM_DEVICE_TYPE_DDR4)
                {		
                    // DDR4 ONLY
                    FAPI_DBG("DDR4 detected");

                    l_rc = FAPI_ATTR_GET(ATTR_SPD_TIMEBASE_MTB_DDR4, &l_dimm_targets[j], l_spd_tb_mtb_ddr4);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD DDR4 Medium Timebase");
                        break;
                    }

                    l_rc = FAPI_ATTR_GET(ATTR_SPD_TIMEBASE_FTB_DDR4, &l_dimm_targets[j], l_spd_tb_ftb_ddr4);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD DDR4 Fine Timebase");
                        break;
                    }

                    l_rc = FAPI_ATTR_GET(ATTR_SPD_TCKMAX_DDR4, &l_dimm_targets[j], l_spd_tckmax_ddr4);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD DDR4 TCK Max");
                        break;
                    }

                    if ( (l_spd_tb_mtb_ddr4 == 0)&&(l_spd_tb_ftb_ddr4 == 0))
                    {
                        // These are now considered constant within DDR4 
                        // If DDR4 spec changes to include other values, these const's need to be updated
                        l_spd_mtb_dividend = DDR4_MTB_DIVIDEND;
                        l_spd_mtb_divisor = DDR4_MTB_DIVISOR;	
                        l_spd_ftb_dividend = DDR4_FTB_DIVIDEND;
                        l_spd_ftb_divisor = DDR4_FTB_DIVISOR;
                    }
                    else
                    {

                        //Invalid due to the fact that JEDEC dictates that these should be zero.
                        // Log error and continue to next DIMM
                        FAPI_ERR("Invalid data received from SPD DDR4 MTB/FTB Timebase");
                        const uint8_t &MTB_DDR4 = l_spd_tb_mtb_ddr4;
                        const uint8_t &FTB_DDR4 = l_spd_tb_ftb_ddr4;
                        const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
                        FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_SPD_DATA_DDR4);
                        fapiLogError(l_rc);
                        continue;
                    }

                }
                else
                {		
                    // DDR3 ONLY
                    FAPI_DBG("DDR3 detected");

                    l_rc = FAPI_ATTR_GET(ATTR_SPD_MTB_DIVIDEND, &l_dimm_targets[j], l_spd_mtb_dividend);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD Medium Timebase Dividend.");
                        break;
                    }

                    l_rc = FAPI_ATTR_GET(ATTR_SPD_MTB_DIVISOR, &l_dimm_targets[j], l_spd_mtb_divisor);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD Medium Timebase Divisor.");
                        break;
                    }

                    l_rc = FAPI_ATTR_GET(ATTR_SPD_FTB_DIVIDEND,  &l_dimm_targets[j], l_spd_ftb_dividend);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read the SPD FTB dividend");
                        break;
                    }
                    l_rc = FAPI_ATTR_GET(ATTR_SPD_FTB_DIVISOR,  &l_dimm_targets[j], l_spd_ftb_divisor);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read the SPD FTB divisor");
                        break;
                    }
                    if ( (l_spd_mtb_dividend == 0)||(l_spd_mtb_divisor == 0)||(l_spd_ftb_dividend == 0)||(l_spd_ftb_divisor == 0))
                    {
                        //Invalid due to the fact that JEDEC dictates that these should be non-zero.
                        // Log error and continue to next DIMM
                        FAPI_ERR("Invalid data received from SPD within MTB/FTB Dividend, MTB/FTB Divisor");
                        const uint8_t &MTB_DIVIDEND = l_spd_mtb_dividend;
                        const uint8_t &MTB_DIVISOR = l_spd_mtb_divisor;
                        const uint8_t &FTB_DIVIDEND = l_spd_ftb_dividend;
                        const uint8_t &FTB_DIVISOR = l_spd_ftb_divisor;
                        const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
                        FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_SPD_DATA_DDR3);
                        fapiLogError(l_rc);
                        continue;
                    }
                }
                // common to both DDR3 & DDR4
                l_rc = FAPI_ATTR_GET(ATTR_SPD_TCKMIN, &l_dimm_targets[j], l_spd_min_tck_MTB);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read SPD Minimum TCK (Min Clock Cycle).");
                    break;
                }

                l_rc = FAPI_ATTR_GET(ATTR_SPD_TAAMIN, &l_dimm_targets[j], l_spd_min_taa_MTB);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read SPD Minimum TAA (Min CAS Latency Time).");
                    break;
                }
                l_rc = FAPI_ATTR_GET(ATTR_SPD_CAS_LATENCIES_SUPPORTED, &l_dimm_targets[j], l_spd_cas_lat_supported);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read SPD Supported CAS Latencies.");
                    break;
                }

                l_rc = FAPI_ATTR_GET(ATTR_MBA_PORT,  &l_dimm_targets[j], cur_mba_port);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read the Port Info in order to determine configuration.");
                    break;
                }
                l_rc = FAPI_ATTR_GET(ATTR_MBA_DIMM,  &l_dimm_targets[j], cur_mba_dimm);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read the DIMM Info in order to determine configuration.");
                    break;
                }
                l_rc = FAPI_ATTR_GET(ATTR_SPD_MODULE_TYPE,  &l_dimm_targets[j], module_type);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read the SPD module type.");
                    break;
                }
                // from dimm_spd_attributes.xml, R1 = 0x00, R2 = 0x01, R3 = 0x02, R4 = 0x03
                l_rc = FAPI_ATTR_GET(ATTR_SPD_NUM_RANKS,  &l_dimm_targets[j], num_ranks);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read the SPD number of ranks");
                    break;
                }
                l_rc = FAPI_ATTR_GET(ATTR_SPD_FINE_OFFSET_TAAMIN,  &l_dimm_targets[j], l_spd_taa_offset_FTB); 
                if (l_rc)
                {
                    FAPI_ERR("Unable to read the SPD TAA offset (FTB)");
                    break;
                }
                l_rc = FAPI_ATTR_GET(ATTR_SPD_FINE_OFFSET_TCKMIN,  &l_dimm_targets[j], l_spd_tck_offset_FTB);
                if (l_rc)
                {
                    FAPI_ERR("Unable to read the SPD TCK offset (FTB)");
                    break;
                }

                cur_dimm_spd_valid_u8array[cur_mba_port][cur_mba_dimm] = MSS_FREQ_VALID;

                if ((l_spd_min_tck_MTB == 0)||(l_spd_min_taa_MTB == 0))
                {
                    //Invalid due to the fact that JEDEC dictates that these should be non-zero.
                    // Log error and continue to next DIMM
                    FAPI_ERR("Invalid data received from SPD within TCK Min, or TAA Min");
                    const uint8_t &MIN_TCK = l_spd_min_tck_MTB;
                    const uint8_t &MIN_TAA = l_spd_min_taa_MTB;
                    const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
		            const fapi::Target &TARGET = i_target_memb;
                    FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_SPD_DATA_COMMON);
                    fapiLogError(l_rc);
                    continue;
                }

                // Calc done on PS units (the multiplication of 1000) to avoid rounding errors.
                // Frequency listed with multiplication of 2 as clocking data on both +- edges
                l_spd_min_tck =  ( 1000 * l_spd_min_tck_MTB * l_spd_mtb_dividend ) / l_spd_mtb_divisor;
                l_spd_min_taa =  ( 1000 * l_spd_min_taa_MTB * l_spd_mtb_dividend ) / l_spd_mtb_divisor;

                FAPI_INF("min tck = %i, taa = %i", l_spd_min_tck, l_spd_min_taa);
                FAPI_INF("FTB tck 0x%x, taa 0x%x",l_spd_tck_offset_FTB,l_spd_taa_offset_FTB);
                // Adjusting by tck offset -- tck offset represented in 2's compliment as it could be positive or negative adjustment
                // No multiplication of 1000 as it is already in picoseconds.
                if (l_spd_tck_offset_FTB & 0x80)
                {
                    l_spd_tck_offset_FTB = ~( l_spd_tck_offset_FTB ) + 1;
                    l_spd_tck_offset = (l_spd_tck_offset_FTB  * l_spd_ftb_dividend )  / l_spd_ftb_divisor;
                    l_spd_min_tck =  l_spd_min_tck - l_spd_tck_offset;
                    FAPI_INF("FTB minus offset %i, min tck %i",l_spd_tck_offset,l_spd_min_tck);
                }
                else
                {
                    l_spd_tck_offset = (l_spd_tck_offset_FTB  * l_spd_ftb_dividend )  / l_spd_ftb_divisor;
                    l_spd_min_tck =  l_spd_min_tck + l_spd_tck_offset;
                    FAPI_INF("FTB plus offset %i, min tck %i",l_spd_tck_offset,l_spd_min_tck);
                }

                // Adjusting by taa offset -- taa offset represented in 2's compliment as it could be positive or negative adjustment
                if (l_spd_taa_offset_FTB & 0x80)
                {
                    l_spd_taa_offset_FTB = ~( l_spd_taa_offset_FTB) + 1;
                    l_spd_taa_offset = (l_spd_taa_offset_FTB  * l_spd_ftb_dividend )  / l_spd_ftb_divisor;
                    l_spd_min_taa =  l_spd_min_taa - l_spd_taa_offset;
                }
                else
                {
                    l_spd_taa_offset = (l_spd_taa_offset_FTB  * l_spd_ftb_dividend )  / l_spd_ftb_divisor;
                    l_spd_min_taa =  l_spd_min_taa + l_spd_taa_offset;
                }

                if ((l_spd_min_tck == 0)||(l_spd_min_taa == 0))
                {
                    //Invalid due to the fact that JEDEC dictates that these should be non-zero.
                    // Log error and continue to next DIMM
                    FAPI_ERR("Invalid data received from SPD causing TCK Min or TAA Min to be 0");
                    const uint8_t &MIN_TCK = l_spd_min_tck_MTB;
                    const uint8_t &MIN_TAA = l_spd_min_taa_MTB;
                    const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
		            const fapi::Target &TARGET = i_target_memb;
                    FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_SPD_DATA_COMMON);
                    fapiLogError(l_rc);
                    continue;
                }
                l_dimm_freq_calc = 2000000 / l_spd_min_tck;

                FAPI_INF( "TAA(ps): %d TCK(ps): %d Calc'ed Freq for this dimm: %d", l_spd_min_taa, l_spd_min_tck, l_dimm_freq_calc);

                //is this the slowest dimm?
                if (l_dimm_freq_calc < l_dimm_freq_min)
                {
                    l_dimm_freq_min = l_dimm_freq_calc;
                }

                if (l_spd_min_tck > l_spd_min_tck_max)
                {
                    l_spd_min_tck_max = l_spd_min_tck;
                }

                if (l_spd_min_taa > l_spd_min_taa_max)
                {
                    l_spd_min_taa_max = l_spd_min_taa;
                }

                if ( l_spd_cas_lat_supported & 0x00000001 )
                {
                    cl_count_array[0]++;
                }
                else if ( l_spd_cas_lat_supported & 0x00000002 )
                {
                    cl_count_array[1]++;
                }
                else if ( l_spd_cas_lat_supported & 0x00000004 )
                {
                    cl_count_array[2]++;
                }
                else if ( l_spd_cas_lat_supported & 0x00000008 )
                {
                    cl_count_array[3]++;	
                }
                else if ( l_spd_cas_lat_supported & 0x00000010 )
                {
                    cl_count_array[4]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000020 )
                {
                    cl_count_array[5]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000040 )
                {
                    cl_count_array[6]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000080 )
                {
                    cl_count_array[7]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000100 )
                {
                    cl_count_array[8]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000200 )
                {
                    cl_count_array[9]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000400)
                {
                    cl_count_array[10]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00000800 )
                {
                    cl_count_array[11]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00001000 )
                {
                    cl_count_array[12]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00002000 )
                {
                    cl_count_array[13]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00004000)
                {
                    cl_count_array[14]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00008000 )
                {
                    cl_count_array[15]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00010000 )
                {
                    cl_count_array[16]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00020000 )
                {
                    cl_count_array[17]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00040000)
                {
                    cl_count_array[18]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00080000 )
                {
                    cl_count_array[19]++;		
                }
                else if ( l_spd_cas_lat_supported & 0x00100000 )
                {
                    cl_count_array[20]++;		
                }


                l_spd_cas_lat_supported_all = l_spd_cas_lat_supported_all & l_spd_cas_lat_supported;


                num_ranks_total = num_ranks_total + num_ranks + 1;

                if ( (module_type_group_1 == module_type) || (module_type_group_1 == 0) ) 
                {
                    module_type_group_1 = module_type;
                    module_type_group_1_total++;
                }
                else if ( (module_type_group_2 == module_type) || (module_type_group_2 == 0) ) 
                {
                    module_type_group_2 = module_type;
                    module_type_group_2_total++;
                }

            } // DIMM
            if (l_rc)
            {
                break;
            }
        } // MBA
        if (l_rc)
        {
            // Break out of do...while(0)
            break;
        }
        // Check for DIMM Module Type Mixing
        if (module_type_group_2 != 0)
        {
            if (module_type_group_1_total > module_type_group_2_total)
            {
                module_type_deconfig = module_type_group_1;
            }
            else
            {
                module_type_deconfig = module_type_group_2;
            }

            // Loop through the 2 MBA's                                                                                                  
            for (uint32_t i=0; i < l_mbaChiplets.size(); i++)
            {
                // Get a vector of DIMM targets                                                                                          
                l_rc = fapiGetAssociatedDimms(l_mbaChiplets[i], l_dimm_targets);
                if (l_rc)
                {
                    FAPI_ERR("Error Getting DIMM targets.");
                    break;
                }
                for (uint32_t j=0; j < l_dimm_targets.size(); j++)
                {
                    l_rc = FAPI_ATTR_GET(ATTR_SPD_MODULE_TYPE,  &l_dimm_targets[j], module_type);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read the SPD module type.");
                        break;
                    }
                    if (module_type == module_type_deconfig)
                    {
                        const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
                        const uint8_t &MODULE_TYPE = module_type;
                        FAPI_ERR("Mixing of DIMM Module Types (%d, %d) deconfiguring minority type: %d", module_type_group_1, module_type_group_2, module_type_deconfig);
                        FAPI_SET_HWP_ERROR(l_rc, RC_MSS_MODULE_TYPE_MIX);
                        fapiLogError(l_rc);
                    }
                } // DIMM
                if (l_rc)
                {
                    break;
                }
            } // MBA
        } // if
        if (l_rc)
        {
            // Break out of do...while(0)
            break;
        }
        FAPI_INF( "Highest Supported Frequency amongst DIMMs: %d", l_dimm_freq_min);
        FAPI_INF( "Minimum TAA(ps) amongst DIMMs: %d Minimum TCK(ps) amongst DIMMs: %d", l_spd_min_taa_max, l_spd_min_tck_max);

        //Determining the cnfg for imposing any cnfg speed limitations
        if ((cur_dimm_spd_valid_u8array[0][0] == MSS_FREQ_VALID) && (cur_dimm_spd_valid_u8array[0][1] == MSS_FREQ_VALID))
        {
            plug_config = MSS_FREQ_DUAL_DROP;
        }
        else if ((cur_dimm_spd_valid_u8array[0][0] == MSS_FREQ_VALID) && (cur_dimm_spd_valid_u8array[0][1] == MSS_FREQ_EMPTY))
        {
            plug_config = MSS_FREQ_SINGLE_DROP;
        }
        else
        {
            plug_config = MSS_FREQ_EMPTY;
        }


        FAPI_INF( "PLUG CONFIG(from SPD): %d, Type of Dimm(from SPD): 0x%02X, Num Ranks(from SPD): %d",  plug_config, module_type, num_ranks);

        // Impose configuration limitations
        // Single Drop RDIMMs Cnfgs cannot run faster than 1333 unless it only has 1 rank
        if ((module_type_group_1 == ENUM_ATTR_SPD_MODULE_TYPE_RDIMM)&&(plug_config == MSS_FREQ_SINGLE_DROP)&&(num_ranks_total > 1)&&(l_dimm_freq_min > 1333))
        {
            l_dimm_freq_min = 1333;
            l_spd_min_tck_max = 1500;
            FAPI_INF( "Single Drop RDIMM with more than 1 Rank Cnfg limitation.  New Freq: %d", l_dimm_freq_min); 
        }
        // Double Drop RDIMMs Cnfgs cannot run faster than 1333 with 4 ranks total
        else if ((module_type_group_1 == ENUM_ATTR_SPD_MODULE_TYPE_RDIMM)&&(plug_config == MSS_FREQ_DUAL_DROP)&&(num_ranks_total == 4)&&(l_dimm_freq_min > 1333))
        {
            l_dimm_freq_min = 1333;
            l_spd_min_tck_max = 1500;
            FAPI_INF( "Dual Drop RDIMM with more than 4 Rank Cnfg limitation.  New Freq: %d", l_dimm_freq_min); 
        }
        // Double Drop RDIMMs Cnfgs cannot run faster than 1066 with 8 ranks total
        else if ((module_type_group_1 == ENUM_ATTR_SPD_MODULE_TYPE_RDIMM)&&(plug_config == MSS_FREQ_DUAL_DROP)&&(num_ranks_total == 8)&&(l_dimm_freq_min > 1066))
        {
            l_dimm_freq_min = 1066;
            l_spd_min_tck_max = 1875;
            FAPI_INF( "Dual Drop RDIMM with more than 8 Rank Cnfg limitation.  New Freq: %d", l_dimm_freq_min); 
        }
        // Single Drop LRDIMMs Cnfgs cannot run faster than 1333 with greater than 2 ranks
        else if ((module_type_group_1 == ENUM_ATTR_SPD_MODULE_TYPE_LRDIMM)&&(plug_config == MSS_FREQ_SINGLE_DROP)&&(num_ranks_total > 2)&&(l_dimm_freq_min > 1333))
        {
            l_dimm_freq_min = 1333;
            l_spd_min_tck_max = 1500;
            FAPI_INF( "Single Drop LRDIMM with more than 2 Rank Cnfg limitation.  New Freq: %d", l_dimm_freq_min); 
        }
        // Dual Drop LRDIMMs Cnfgs cannot run faster than 1333
        else if ((module_type_group_1 == ENUM_ATTR_SPD_MODULE_TYPE_LRDIMM)&&(plug_config == MSS_FREQ_DUAL_DROP)&&(l_dimm_freq_min > 1333))
        {
            l_dimm_freq_min = 1333;
            l_spd_min_tck_max = 1500;
            FAPI_INF( "Dual Drop LRDIMM Cnfg limitation.  New Freq: %d", l_dimm_freq_min); 
        }

        if ( l_spd_min_tck_max == 0)
        {
	        // Loop through the 2 MBA's                                                                                                  
            for (uint32_t i=0; i < l_mbaChiplets.size(); i++)
            {
                // Get a vector of DIMM targets                                                                                          
                l_rc = fapiGetAssociatedDimms(l_mbaChiplets[i], l_dimm_targets);
                if (l_rc)
                {
                    FAPI_ERR("Error Getting DIMM targets.");
                    break;
                }
                for (uint32_t j=0; j < l_dimm_targets.size(); j++)
                {
                    l_rc = FAPI_ATTR_GET(ATTR_SPD_TCKMIN, &l_dimm_targets[j], l_spd_min_tck_MTB);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD Minimum TCK (Min Clock Cycle).");
                        break;
                    }
                    if ( l_spd_min_tck_MTB == 0 )
                    {
                        FAPI_ERR("l_spd_min_tck_max = 0 unable to calculate freq or cl.  Possibly no centaurs configured. ");
                        const uint32_t &MIN_TCK = l_spd_min_tck_max;
                        const uint32_t &MIN_TAA = l_spd_min_taa_max;
                        const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
                        const fapi::Target &TARGET = i_target_memb;
                        FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_SPD_DATA_COMMON);
                        fapiLogError(l_rc);
                    }
                } // DIMM
                if (l_rc)
                {
                    break;
                }
	        } // MBA
        } // if
        if (l_rc)
        {
            // Break out of do...while(0)
            break;
        }
        if (!l_rc)
        {
            l_rc = FAPI_ATTR_GET(ATTR_MSS_FREQ_OVERRIDE,  &i_target_memb, l_freq_override); 
            if ( l_freq_override != 0)
            {
                // The relationship is as such
                // l_dimm_freq_min = 2000000 / l_spd_min_tck_max

                if (l_freq_override == 1866)
                {
                    l_dimm_freq_min = 1866;
                    l_spd_min_tck_max = 1072;
                }

                if (l_freq_override == 1600)
                {
                    l_dimm_freq_min = 1600;
                    l_spd_min_tck_max = 1250;
                }

                if (l_freq_override == 1333)
                {
                    l_dimm_freq_min = 1333;
                    l_spd_min_tck_max = 1500;
                }

                if (l_freq_override == 1066)
                {
                    l_dimm_freq_min = 1066;
                    l_spd_min_tck_max = 1875;
                }

                FAPI_INF( "Override Frequency Detected: %d", l_dimm_freq_min); 
            }
        }



        //If no common supported CL get rid of the minority DIMMs
        if ((l_spd_cas_lat_supported_all == 0) && (!l_rc))
        {
            for(uint8_t i=0;i<20;i++) 
            { 
                if (cl_count_array[i] > highest_cl_count)
                {
                    highest_common_cl = i; 
                }
            } 


            // Loop through the 2 MBA's                                                                                                  
            for (uint32_t i=0; i < l_mbaChiplets.size(); i++)
            {
                // Get a vector of DIMM targets                                                                                          
                l_rc = fapiGetAssociatedDimms(l_mbaChiplets[i], l_dimm_targets);
                if (l_rc)
                {
                    FAPI_ERR("Error Getting DIMM targets.");
                    break;
                }
                for (uint32_t j=0; j < l_dimm_targets.size(); j++)
                {
                    l_rc = FAPI_ATTR_GET(ATTR_SPD_CAS_LATENCIES_SUPPORTED, &l_dimm_targets[j], l_spd_cas_lat_supported);
                    if (l_rc)
                    {
                        FAPI_ERR("Unable to read SPD Supported CAS Latencies.");
                        break;
                    }
                    if ( !(l_spd_cas_lat_supported & 0x0000001 << highest_common_cl) )
                    {
                        const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
                        const uint32_t &CL_SUPPORTED = l_spd_cas_lat_supported;
                        FAPI_ERR("No common supported CAS latencies between DIMMS.");
                        FAPI_SET_HWP_ERROR(l_rc, RC_MSS_NO_COMMON_SUPPORTED_CL);
                        fapiLogError(l_rc);
                    }
                } // DIMM
                if (l_rc)
                {
                    break;
                }
            } // MBA
        } // if
        if (l_rc)
        {
            // Break out of do...while(0)
            break;
        }
        if (!l_rc) 
        {

            //Determine a proposed CAS latency
            l_cas_latency = l_spd_min_taa_max / l_spd_min_tck_max;

            FAPI_INF( "CL = TAA / TCK ... TAA(ps): %d TCK(ps): %d", l_spd_min_taa_max, l_spd_min_tck_max);
            FAPI_INF( "Calculated CL: %d", l_cas_latency);

            if ( l_spd_min_taa_max % l_spd_min_tck_max)
            {
                l_cas_latency++;
                FAPI_INF( "After rounding up ... CL: %d", l_cas_latency);
            }

            l_cl_mult_tck = l_cas_latency * l_spd_min_tck_max;

            // If the CL proposed is not supported or the TAA exceeds TAA max
            // Spec defines tAAmax as 20 ns for all DDR3 speed grades.
            // Break loop if we have an override condition without a solution.

            while (    ( (!( l_spd_cas_lat_supported_all & (0x00000001<<(l_cas_latency-4)))) || (l_cl_mult_tck > 20000) )
                    && ( l_override_path == 0 ) )
            {

                FAPI_INF( "Warning calculated CL is not supported in VPD.  Searching for a new CL.");

                // If not supported, increment the CL up to 18 (highest supported CL) looking for Supported CL 
                while ((!( l_spd_cas_lat_supported_all & (0x00000001<<(l_cas_latency-4))))&&(l_cas_latency < 18))
                {
                    l_cas_latency++;
                }

                // If still not supported CL or TAA is > 20 ns ... pick a slower TCK and start again
                l_cl_mult_tck = l_cas_latency * l_spd_min_tck_max;

                // Do not move freq if using an override freq.  Just continue.  Hence the overide in this if statement
                if ( ( (!( l_spd_cas_lat_supported_all & (0x00000001<<(l_cas_latency-4)))) || (l_cl_mult_tck > 20000) )
                        && ( l_freq_override == 0) )
                {
                    FAPI_INF( "No Supported CL works for calculating frequency.  Lowering frequency and trying CL Algorithm again.");

                    if (l_spd_min_tck_max < 1500)
                    {
                        //1600 to 1333
                        l_spd_min_tck_max = 1500;

                    }
                    else if (l_spd_min_tck_max < 1875)
                    {
                        //1333 to 1066 
                        l_spd_min_tck_max = 1875;
                    }
                    else if (l_spd_min_tck_max < 2500)
                    {
                        //1066 to 800
                        l_spd_min_tck_max = 2500;
                    }
                    else
                    {
                        //This is minimum frequency and cannot be lowered
                        //Therefore we will deconfig the minority dimms.
                        for(uint8_t i=0;i<20;i++) 
                        { 
                            if (cl_count_array[i] > lowest_cl_count)
                            {
                                lowest_common_cl = i; 
                            }
                        } 


                        // Loop through the 2 MBA's                                                                                                  
                        for (uint32_t i=0; i < l_mbaChiplets.size(); i++)
                        {
                            // Get a vector of DIMM targets                                                                                          
                            l_rc = fapiGetAssociatedDimms(l_mbaChiplets[i], l_dimm_targets);
                            if (l_rc)
                            {
                                FAPI_ERR("Error Getting DIMM targets.");
                                return l_rc;
                            }
                            for (uint32_t j=0; j < l_dimm_targets.size(); j++)
                            {
                                l_rc = FAPI_ATTR_GET(ATTR_SPD_CAS_LATENCIES_SUPPORTED, &l_dimm_targets[j], l_spd_cas_lat_supported);
                                if (l_rc)
                                {
                                    FAPI_ERR("Unable to read SPD Supported CAS Latencies.");
                                    break;
                                }
                                if (l_spd_cas_lat_supported & 0x0000001 << lowest_common_cl)
                                {
                                    const fapi::Target &DIMM_TARGET = l_dimm_targets[j];
                                    const uint32_t &CL_SUPPORTED = l_spd_cas_lat_supported;
                                    FAPI_ERR("Lowered Frequency to TCLK MIN finding no supported CL without exceeding TAA MAX.");
                                    FAPI_SET_HWP_ERROR(l_rc, RC_MSS_EXCEED_TAA_MAX_NO_CL );
                                    fapiLogError(l_rc);
                                }
                            } // DIMM
                            if (l_rc)
                            {
                                break;
                            }
                        } // MBA
                    } // else
                    if (l_rc)
                    {
                        // Break out of while loop
                        break;
                    }
                    // Re-calculate with new tck
                    l_cas_latency = l_spd_min_taa_max / l_spd_min_tck_max;
                    if ( l_spd_min_taa_max % l_spd_min_tck_max)
                    {
                        l_cas_latency++;
                    } 
                    l_cl_mult_tck = l_cas_latency * l_spd_min_tck_max;
                    l_dimm_freq_min = 2000000 / l_spd_min_tck_max;

                } // if
                // Need to break the loop in case we reach this condition because no longer modify freq and CL
                // With an overrride
                if ( ( (!( l_spd_cas_lat_supported_all & (0x00000001<<(l_cas_latency-4)))) || (l_cl_mult_tck > 20000) )
                        && ( l_freq_override != 0) )
                {

                    FAPI_INF( "No Supported CL works for override frequency.  Using override frequency with an unsupported CL.");
                    l_override_path = 1;
                }
            } // while
        } // if
        if (l_rc)
        {
            // Break out of do...while(0)
            break;
        }
        //bucketize dimm freq.
        if (!l_rc) 
        {
            if (l_dimm_freq_min < 1013)
            {
                FAPI_ERR("Unsupported frequency:  DIMM Freq calculated < 1013 MHz");
                const uint32_t &DIMM_MIN_FREQ = l_dimm_freq_min;
                FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_FREQ_CALCULATED);
                break;
            }
            else if (l_dimm_freq_min < 1266)
            {
                // 1066 
                l_selected_dimm_freq=1066;
            }
            else if (l_dimm_freq_min < 1520)
            {
                // 1333
                l_selected_dimm_freq=1333;
            }
            else if (l_dimm_freq_min < 1773)
            {
                // 1600
                l_selected_dimm_freq=1600;
            }
            else if (l_dimm_freq_min < 2026)
            {
                // 1866
                l_selected_dimm_freq=1866;
            }
            else if (l_dimm_freq_min < 2280)
            {
                // 2133
                l_selected_dimm_freq=2133;
            }
            else
            {
                FAPI_ERR("Unsupported frequency:  DIMM Freq calculated > 2133 MHz: %d", l_dimm_freq_min);
                const uint32_t &DIMM_MIN_FREQ = l_dimm_freq_min;
                FAPI_SET_HWP_ERROR(l_rc, RC_MSS_UNSUPPORTED_FREQ_CALCULATED);
                break;
            }
        }

        if (!l_rc)
        {
            // 0x03 = capable of both 8.0G/9.6G, 0x01 = capable of 8.0G, 0x02 = capable 9.6G
            if ( l_selected_dimm_freq == 1066)
            {
                l_nest_capable_frequencies = 0x01;
                l_rc = FAPI_ATTR_SET(ATTR_MSS_NEST_CAPABLE_FREQUENCIES, &i_target_memb, l_nest_capable_frequencies);
            }
            else
            {
                l_nest_capable_frequencies = 0x02;
                l_rc = FAPI_ATTR_SET(ATTR_MSS_NEST_CAPABLE_FREQUENCIES, &i_target_memb, l_nest_capable_frequencies);
            }

        }

        // set frequency in centaur attribute ATTR_MSS_FREQ
        if (!l_rc) 
        {
            l_rc = FAPI_ATTR_SET(ATTR_MSS_FREQ, &i_target_memb, l_selected_dimm_freq);
            if (l_rc) 
            {
                break;
            }
            FAPI_INF( "Final Chosen Frequency: %d ",  l_selected_dimm_freq);
            FAPI_INF( "Final Chosen CL: %d ",  l_cas_latency);
            for (uint32_t k=0; k < l_mbaChiplets.size(); k++)
            {
                l_rc = FAPI_ATTR_SET(ATTR_EFF_DRAM_CL, &l_mbaChiplets[k], l_cas_latency);
                if (l_rc) 
                {
                    break;
                }
            }
        }
    }while(0);
    //all done.
    return l_rc;
}

OpenPOWER on IntegriCloud