forked from illumos/illumos-gate
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zen_umc.c
4016 lines (3743 loc) · 147 KB
/
zen_umc.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
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2024 Oxide Computer Company
*/
/*
* AMD Zen Unified Memory Controller Driver
*
* This file forms the core logic around transforming a physical address that
* we're used to using into a specific location on a DIMM. This has support for
* a wide range of AMD CPUs and APUs ranging from Zen 1 - Zen 4.
*
* The goal of this driver is to implement the infrastructure and support
* necessary to understand how DRAM requests are being routed in the system and
* to be able to map those to particular channels and then DIMMs. This is used
* as part of RAS (reliability, availability, and serviceability) to enable
* aspects around understanding ECC errors, hardware topology, and more. Like
* with any software project, there is more to do here. Please see the Future
* Work section at the end of this big theory statement for more information.
*
* -------------------
* Driver Organization
* -------------------
*
* This driver is organized into two major pieces:
*
* 1. Logic to interface with hardware, discover the data fabric, memory
* controller configuration, and transform that into a normalized fashion
* that can be used across all different Zen family CPUs. This is
* implemented generally in this file, and is designed to assume it is in
* the kernel (as it requires access to the SMN, DF PCI registers, and the
* amdzen nexus driver client services).
*
* 2. Logic that can take the above normalized memory information and perform
* decoding (e.g. physical address to DIMM information). This generally
* lives in common/mc/zen_uc/zen_umc_decode.c. This file is in common/,
* meaning it is designed to be shared by userland and the kernel. Even
* more so, it is designed to operate on a const version of our primary
* data structure (zen_umc_t), not allowing it to be modified. This allows
* us to more easily unit test the decoding logic and utilize it in other
* circumstances such as with the mcdecode utility.
*
* There is corresponding traditional dev_ops(9S) and cb_ops(9S) logic in the
* driver (currently this file) which take care of interfacing with the broader
* operating system environment.
*
* There is only ever one instance of this driver, e.g. it is a singleton in
* design pattern parlance. There is a single struct, the zen_umc_t found in the
* global (albeit static) variable zen_umc. This structure itself contains a
* hierarchical set of structures that describe the system. To make management
* of memory simpler, all of the nested structures that we discover from
* hardware are allocated in the same structure. The only exception to this rule
* is when we cache serialized nvlists for dumping.
*
* The organization of the structures inside the zen_umc_t, generally mimics the
* hardware organization and is structured as follows:
*
* +-----------+
* | zen_umc_t |
* +-----------+
* |
* +-------------------------------+
* v v
* +--------------+ +--------------+ One instance of the
* | zen_umc_df_t | ... | zen_umc_df_t | zen_umc_df_t per
* +--------------+ +--------------+ discovered DF.
* |||
* |||
* ||| +----------------+ +----------------+ Global DRAM
* ||+--->| df_dram_rule_t | ... | df_dram_rule_t | rules for the
* || +----------------+ +----------------+ platform.
* ||
* || +--------------------+ +--------------------+ UMC remap
* |+--->| zen_umc_cs_remap_t | ... | zen_umc_cs_remap_t | rule arrays.
* | +--------------------+ +--------------------+
* |
* v
* +----------------+ +----------------+ One structure per
* | zen_umc_chan_t | ... | zen_umc_chan_t | discovered DDR4/5
* +----------------+ +----------------+ memory channel.
* ||||
* ||||
* |||| +----------------+ +----------------+ Channel specific
* |||+--->| df_dram_rule_t | ... | df_dram_rule_t | copy of DRAM rules.
* ||| +----------------+ +----------------+ Less than global.
* |||
* ||| +---------------+ +---------------+ Per-Channel DRAM
* ||+---->| chan_offset_t | ... | chan_offset_t | offset that is used
* || +---------------+ +---------------+ for normalization.
* ||
* || +-----------------+ Channel-specific
* |+----->| umc_chan_hash_t | hashing rules.
* | +-----------------+
* |
* | +------------+ +------------+ One structure for
* +------>| umc_dimm_t | ... | umc_dimm_t | each DIMM in the
* +------------+ +------------+ channel. Always two.
* |
* | +----------+ +----------+ Per chip-select
* +---> | umc_cs_t | ... | umc_cs_t | data. Always two.
* +----------+ +----------+
*
* In the data structures themselves you'll often find several pieces of data
* that have the term 'raw' in their name. The point of these is to basically
* capture the original value that we read from the register before processing
* it. These are generally used either for debugging or to help answer future
* curiosity with resorting to the udf and usmn tooling, which hopefully aren't
* actually installed on systems.
*
* With the exception of some of the members in the zen_umc_t that are around
* management of state for userland ioctls, everything in the structure is
* basically write-once and from that point on should be treated as read-only.
*
* ---------------
* Memory Decoding
* ---------------
*
* To understand the process of memory decoding, it's worth going through and
* understanding a bunch of the terminology that is used in this process. As an
* additional reference when understanding this, you may want to turn to either
* an older generation AMD BIOS and Kernel Developer's Guide or the more current
* Processor Programming Reference. In addition, the imc driver, which is the
* Intel equivalent, also provides an additional bit of reference.
*
* SYSTEM ADDRESS
*
* This is a physical address and is the way that the operating system
* normally thinks of memory. System addresses can refer to many different
* things. For example, you have traditional DRAM, memory-mapped PCIe
* devices, peripherals that the processor exposes such as the xAPIC, data
* from the FCH (Fusion Controller Hub), etc.
*
* TOM, TOM2, and the DRAM HOLE
*
* Physical memory has a complicated layout on x86 in part because of
* support for traditional 16-bit and 32-bit systems. As a result, contrary
* to popular belief, DRAM is not at a consistent address range in the
* processor. AMD processors have a few different ranges. There is a 32-bit
* region that starts at effectively physical address zero and goes to the
* TOM MSR (top of memory -- Core::X86::Msr::TOP_MEM). This indicates a
* limit below 4 GiB, generally around 2 GiB.
*
* From there, the next region of DRAM starts at 4 GiB and goes to TOM2
* (top of memory 2 -- Core::X86::Msr::TOM2). The region between TOM and
* 4 GiB is called the DRAM hole. Physical addresses in this region are
* used for memory mapped I/O. This breaks up contiguous physical
* addresses being used for DRAM, creating a "hole".
*
* DATA FABRIC
*
* The data fabric (DF) is the primary interface that different parts of
* the system use to communicate with one another. This includes the I/O
* engines (where PCIe traffic goes), CPU caches and their cores, memory
* channels, cross-socket communication, and a whole lot more. The first
* part of decoding addresses and figuring out which DRAM channel an
* address should be directed to all come from the data fabric.
*
* The data fabric is comprised of instances. So there is one instance for
* each group of cores, each memory channel, etc. Each instance has its own
* independent set of register information. As the data fabric is a series
* of devices exposed over PCI, if you do a normal PCI configuration space
* read or write that'll end up broadcasting the I/O. Instead, to access a
* particular instance's register information there is an indirect access
* mechanism. The primary way that this driver accesses data fabric
* registers is via these indirect reads.
*
* There is one instance of the Data Fabric per socket starting with Zen 2.
* In Zen 1, there was one instance of the data fabric per CCD -- core
* complex die (see cpuid.c's big theory statement for more information).
*
* DF INSTANCE ID
*
* A DF instance ID is an identifier for a single entity or component in a
* data fabric. The set of instance IDs is unique only with a single data
* fabric. So for example, each memory channel, I/O endpoint (e.g. PCIe
* logic), group of cores, has its own instance ID. Anything within the
* same data fabric (e.g. the same die) can be reached via its instance ID.
* The instance ID is used to indicate which instance to contact when
* performing indirect accesses.
*
* Not everything that has an instance ID will be globally routable (e.g.
* between multiple sockets). For things that are, such as the memory
* channels and coherent core initiators, there is a second ID called a
* fabric ID.
*
* DF FABRIC ID
*
* A DF fabric ID is an identifier that combines information to indicate
* both which instance of the data fabric a component is on and a component
* itself. So with this number you can distinguish between a memory channel
* on one of two sockets. A Fabric ID is made up of two parts. The upper
* part indicates which DF we are talking to and is referred to as a Node
* ID. The Node ID is itself broken into two parts: one that identifies a
* socket, and one that identifies a die. The lower part of a fabric ID is
* called a component ID and indicates which component in a particular data
* fabric that we are talking to. While only a subset of the total
* components in the data fabric are routable, for everything that is, its
* component ID matches its instance ID.
*
* Put differently, the component portion of a fabric ID and a component's
* instance ID are always the same for routable entities. For things which
* cannot be routed, they only have an instance ID and no fabric ID.
* Because this code is always interacting with data fabric components that
* are routable, sometimes instance ID and the component ID portion of the
* data fabric ID may be used interchangeably.
*
* Finally, it's worth calling out that the number of bits that are used to
* indicate the socket, die, and component in a fabric ID changes from
* hardware generation to hardware generation.
*
* Inside the code here, the socket and die decomposition information is
* always relative to the node ID. AMD phrases the decomposition
* information in terms of a series of masks and shifts. This is
* information that can be retrieved from the data fabric itself, allowing
* us to avoid hardcoding too much information other than which registers
* actually have which fields. With both masks and shifts, it's important
* to establish which comes first. We follow AMD's convention and always
* apply masks before shifts. With that, let's look at an example of a
* made up bit set:
*
* Assumptions (to make this example simple):
* o The fabric ID is 16 bits
* o The component ID is 8 bits
* o The node ID is 8 bits
* o The socket and die ID are both 4 bits
*
* Here, let's say that we have the ID 0x2106. This decomposes into a
* socket 0x2, die 0x1, and component 0x6. Here is how that works in more
* detail:
*
* 0x21 0x06
* |------| |------|
* Node ID Component ID
* Mask: 0xff00 0x00ff
* Shift: 8 0
*
* Next we would decompose the Node ID as:
* 0x2 0x1
* |------| |------|
* Sock ID Die ID
* Mask: 0xf0 0x0f
* Shift: 4 0
*
* Composing a fabric ID from its parts would work in a similar way by
* applying masks and shifts.
*
* NORMAL ADDRESS
*
* A normal address is one of the primary address types that AMD uses in
* memory decoding. It takes into account the DRAM hole, interleave
* settings, and is basically the address that is dispatched to the broader
* data fabric towards a particular DRAM channel.
*
* Often, phrases like 'normalizing the address' or normalization refer to
* the process of transforming a system address into the channel address.
*
* INTERLEAVING
*
* The idea of interleaving is to take a contiguous range and weave it
* between multiple different actual entities. Generally certain bits in
* the range are used to select one of several smaller regions. For
* example, if you have 8 regions each that are 4 GiB in size, that creates
* a single 32 GiB region. You can use three bits in that 32 GiB space to
* select one of the 8 regions. For a more visual example, see the
* definition of this in uts/intel/io/imc/imc.c.
*
* CHANNEL
*
* A channel is used to refer to a single memory channel. This is sometimes
* called a DRAM channel as well. A channel operates in a specific mode
* based on the JEDEC DRAM standards (e.g. DDR4, LPDDR5, etc.). A
* (LP)DDR4/5 channel may support up to two DIMMs inside the channel. The
* number of slots is platform dependent and from there the number of DIMMs
* installed can vary. Generally speaking, a DRAM channel defines a set
* number of signals, most of which go to all DIMMs in the channel, what
* varies is which "chip-select" is activated which causes a given DIMM to
* pay attention or not.
*
* DIMM
*
* A DIMM refers to a physical hardware component that is installed into a
* computer to provide access to dynamic memory. Originally this stood for
* dual-inline memory module, though the DIMM itself has evolved beyond
* that. A DIMM is organized into various pages, which are addressed by
* a combination of rows, columns, banks, bank groups, and ranks. How this
* fits together changes from generation to generation and is standardized
* in something like DDR4, LPDDR4, DDR5, LPDDR5, etc. These standards
* define the general individual modules that are assembled into a DIMM.
* There are slightly different standards for combined memory modules
* (which is what we use the term DIMM for). Examples of those include
* things like registered DIMMs (RDIMMs).
*
* A DDR4 DIMM contains a single channel that is 64-bits wide with 8 check
* bits. A DDR5 DIMM has a notable change in this scheme from earlier DDR
* standards. It breaks a single DDR5 DIMM into two sub-channels. Each
* sub-channel is independently addressed and contains 32-bits of data and
* 8-bits of check data.
*
* ROW AND COLUMN
*
* The most basic building block of a DIMM is a die. A DIMM consists of
* multiple dies that are organized together (we'll discuss the
* organization next). A given die is organized into a series of rows and
* columns. First, one selects a row. At which point one is able to select
* a specific column. It is more expensive to change rows than columns,
* leading a given row to contain approximately 1 KiB of data spread across
* its columns. The exact size depends on the device. Each row/column is a
* series of capacitors and transistors. The transistor is used to select
* data from the capacitor and the capacitor actually contains the logical
* 0/1 value.
*
* BANKS AND BANK GROUPS
*
* An individual DRAM die is organized in something called a bank. A DIMM
* has a number of banks that sit in series. These are then grouped into
* larger bank groups. Generally speaking, each bank group has the same
* number of banks. Let's take a look at an example of a system with 4
* bank groups, each with 4 banks.
*
* +-----------------------+ +-----------------------+
* | Bank Group 0 | | Bank Group 1 |
* | +--------+ +--------+ | | +--------+ +--------+ |
* | | Bank 0 | | Bank 1 | | | | Bank 0 | | Bank 1 | |
* | +--------+ +--------+ | | +--------+ +--------+ |
* | +--------+ +--------+ | | +--------+ +--------+ |
* | | Bank 2 | | Bank 3 | | | | Bank 2 | | Bank 3 | |
* | +--------+ +--------+ | | +--------+ +--------+ |
* +-----------------------+ +-----------------------+
*
* +-----------------------+ +-----------------------+
* | Bank Group 2 | | Bank Group 3 |
* | +--------+ +--------+ | | +--------+ +--------+ |
* | | Bank 0 | | Bank 1 | | | | Bank 0 | | Bank 1 | |
* | +--------+ +--------+ | | +--------+ +--------+ |
* | +--------+ +--------+ | | +--------+ +--------+ |
* | | Bank 2 | | Bank 3 | | | | Bank 2 | | Bank 3 | |
* | +--------+ +--------+ | | +--------+ +--------+ |
* +-----------------------+ +-----------------------+
*
* On a DIMM, only a single bank and bank group can be active at a time for
* reading or writing an 8 byte chunk of data. However, these are still
* pretty important and useful because of the time involved to switch
* between them. It is much cheaper to switch between bank groups than
* between banks and that time can be cheaper than activating a new row.
* This allows memory controllers to pipeline this substantially.
*
* RANK AND CHIP-SELECT
*
* The next level of organization is a rank. A rank is effectively an
* independent copy of all the bank and bank groups on a DIMM. That is,
* there are additional copies of the DIMM's organization, but not the data
* itself. Originally a
* single or dual rank DIMM was built such that one copy of everything was
* on each physical side of the DIMM. As the number of ranks has increased
* this has changed as well. Generally speaking, the contents of the rank
* are equivalent. That is, you have the same number of bank groups, banks,
* and each bank has the same number of rows and columns.
*
* Ranks are selected by what's called a chip-select, often abbreviated as
* CS_L in the various DRAM standards. AMD also often abbreviates this as a
* CS (which is not to be confused with the DF class of device called a
* CS). These signals are used to select a rank to activate on a DIMM.
* There are some number of these for each DIMM which is how the memory
* controller chooses which of the DIMMs it's actually going to activate in
* the system.
*
* One interesting gotcha here is how AMD organizes things. Each DIMM
* logically is broken into two chip-selects in hardware. Between DIMMs
* with more than 2 ranks and 3D stacked RDIMMs, there are ways to
* potentially activate more bits. Ultimately these are mapped to a series
* of rank multiplication logic internally. These ultimately then control
* some of these extra pins, though the exact method isn't 100% clear at
* this time.
*
* -----------------------
* Rough Hardware Process
* -----------------------
*
* To better understand how everything is implemented and structured, it's worth
* briefly describing what happens when hardware wants to read a given physical
* address. This is roughly summarized in the following chart. In the left hand
* side is the type of address, which is transformed and generally shrinks along
* the way. Next to it is the actor that is taking action and the type of
* address that it starts with.
*
* +---------+ +------+
* | Virtual | | CPU |
* | Address | | Core |
* +---------+ +------+
* | | The CPU core receives a memory request and then
* | * . . . . determines whether this request is DRAM or MMIO
* | | (memory-mapped I/O) and then sends it to the data
* v v fabric.
* +----------+ +--------+
* | Physical | | Data |
* | Address | | Fabric |
* +----------+ +--------+
* | | The data fabric instance in the CCX/D uses the
* | * . . . . programmed DRAM rules to determine what DRAM
* | | channel to direct a request to and what the
* | | channel-relative address is. It then sends the
* | | request through the fabric. Note, the number of
* | | DRAM rules varies based on the processor SoC.
* | | Server parts like Milan have many more rules than
* | | an APU like Cezanne. The DRAM rules tell us both
* v v how to find and normalize the physical address.
* +---------+ +---------+
* | Channel | | DRAM |
* | Address | | Channel |
* +---------+ +---------+
* | | The UMC (unified memory controller) receives the
* | * . . . . DRAM request and determines which DIMM to send
* | | the request to along with the rank, banks, row,
* | | column, etc. It initiates a DRAM transaction and
* | | then sends the results back through the data
* v v fabric to the CPU core.
* +---------+ +--------+
* | DIMM | | Target |
* | Address | | DIMM |
* +---------+ +--------+
*
* The above is all generally done in hardware. There are multiple steps
* internal to this that we end up mimicking in software. This includes things
* like, applying hashing logic, address transformations, and related.
* Thankfully the hardware is fairly generic and programmed with enough
* information that we can pull out to figure this out. The rest of this theory
* statement covers the major parts of this: interleaving, the act of
* determining which memory channel to actually go to, and normalization, the
* act of removing some portion of the physical address bits to determine the
* address relative to a channel.
*
* ------------------------
* Data Fabric Interleaving
* ------------------------
*
* One of the major parts of address decoding is to understand how the
* interleaving features work in the data fabric. This is used to allow an
* address range to be spread out between multiple memory channels and then,
* later on, when normalizing the address. As mentioned above, a system address
* matches a rule which has information on interleaving. Interleaving comes in
* many different flavors. It can be used to just switch between channels,
* sockets, and dies. It can also end up involving some straightforward and some
* fairly complex hashing operations.
*
* Each DRAM rule has instructions on how to perform this interleaving. The way
* this works is that the rule first says to start at a given address bit,
* generally ranging from bit 8-12. These influence the granularity of the
* interleaving going on. From there, the rules determine how many bits to use
* from the address to determine the die, socket, and channel. In the simplest
* form, these perform a log2 of the actual number of things you're interleaving
* across (we'll come back to non-powers of two). So let's work a few common
* examples:
*
* o 8-channel interleave, 1-die interleave, 2-socket interleave
* Start at bit 9
*
* In this case we have 3 bits that determine the channel to use, 0 bits
* for the die, 1 bit for the socket. Here we would then use the following
* bits to determine what the channel, die, and socket IDs are:
*
* [12] - Socket ID
* [11:9] - Channel ID
*
* You'll note that there was no die-interleave, which means the die ID is
* always zero. This is the general thing you expect to see in Zen 2 and 3
* based systems as they only have one die or a Zen 1 APU.
*
* o 2-channel interleave, 4-die interleave, 2-socket interleave
* Start at bit 10
*
* In this case we have 1 bit for the channel and socket interleave. We
* have 2 bits for the die. This is something you might see on a Zen 1
* system. This results in the following bits:
*
* [13] - Socket ID
* [12:11] - Die ID
* [10] - Channel ID
*
*
* COD, NPS, and MI3H HASHING
*
* However, this isn't the only primary extraction rule of the above values. The
* other primary method is using a hash. While the exact hash methods vary
* between Zen 2/3 and Zen 4 based systems, they follow a general scheme. In the
* system there are three interleaving configurations that are either global or
* enabled on a per-rule basis. These indicate whether one should perform the
* XOR computation using addresses at:
*
* o 64 KiB (starting at bit 16)
* o 2 MiB (starting at bit 21)
* o 1 GiB (starting at bit 30)
*
* In this world, you take the starting address bit defined by the rule and XOR
* it with each enabled interleave address. If you have more than one bit to
* select (e.g. because you are hashing across more than 2 channels), then you
* continue taking subsequent bits from each enabled region. So the second bit
* would use 17, 21, and 31 if all three ranges were enabled while the third bit
* would use 18, 22, and 32. While these are straightforward, there is a catch.
*
* While the DRAM rule contains what the starting address bit, you don't
* actually use subsequent bits in the same way. Instead subsequent bits are
* deterministic and use bits 12 and 13 from the address. This is not the same
* consecutive thing that one might expect. Let's look at a Rome/Milan based
* example:
*
* o 8-channel "COD" hashing, starting at address 9. All three ranges enabled.
* 1-die and 1-socket interleaving.
*
* In this model we are using 3 bits for the channel, 0 bits for the socket
* and die.
*
* Channel ID[0] = addr[9] ^ addr[16] ^ addr[21] ^ addr[30]
* Channel ID[1] = addr[12] ^ addr[17] ^ addr[22] ^ addr[31]
* Channel ID[2] = addr[13] ^ addr[18] ^ addr[23] ^ addr[32]
*
* So through this scheme we'd have a socket/die of 0, and then the channel
* ID is computed based on that. The number of bits that we use here
* depends on how many channels the hash is going across.
*
* The Genoa and related variants, termed "NPS", has a few wrinkles. First,
* rather than 3 bits being used for the channel, up to 4 bits are. Second,
* while the Rome/Milan "COD" hash above does not support socket or die
* interleaving, the "NPS" hash actually supports socket interleaving. However,
* unlike the straightforward non-hashing scheme, the first bit is used to
* determine the socket when enabled as opposed to the last one. In addition, if
* we're not performing socket interleaving, then we end up throwing address bit
* 14 into the mix here. Let's look at examples:
*
* o 4-channel "NPS" hashing, starting at address 8. All three ranges enabled.
* 1-die and 1-socket interleaving.
*
* In this model we are using 2 bits for the channel, 0 bits for the socket
* and die. Because socket interleaving is not being used, bit 14 ends up
* being added into the first bit of the channel selection. Presumably this
* is to improve the address distribution in some form.
*
* Channel ID[0] = addr[8] ^ addr[16] ^ addr[21] ^ addr[30] ^ addr[14]
* Channel ID[1] = addr[12] ^ addr[17] ^ addr[22] ^ addr[31]
*
* o 8-channel "NPS" hashing, starting at address 9. All three ranges enabled.
* 1-die and 2-socket interleaving.
*
* In this model we are using 3 bits for the channel and 1 for the socket.
* The die is always set to 0. Unlike the above, address bit 14 is not used
* because it ends up being required for the 4th address bit.
*
* Socket ID[0] = addr[9] ^ addr[16] ^ addr[21] ^ addr[30]
* Channel ID[0] = addr[12] ^ addr[17] ^ addr[22] ^ addr[31]
* Channel ID[1] = addr[13] ^ addr[18] ^ addr[23] ^ addr[32]
* Channel ID[2] = addr[14] ^ addr[19] ^ addr[24] ^ addr[33]
*
* DF 4D2 NPS 1K/2K
*
* In our DF 4D2 variant, the interleave controls were changed and the way that
* hashes work is different. There are two main families here, a variant on the
* prior NPS hashing that is either NPS 1K or NPS 2K and the MI300 variant that
* we call MI3H. First, there are two additional address ranges that have been
* added:
*
* o 4 KiB (starting at bit 12)
* o 1 TiB (starting at bit 40)
*
* Of these, our understanding is that the 4 KiB range is only used for MI3H
* based hashing. When it is used, only bits 12-14 will be used, but that's
* because the hash algorithm for the MI3H series is, well, unique. The 1T
* otherwise works somewhat as normal. Currently we don't support the MI3H
* decoding, but know that it exists in the code so we can provide a better
* error code.
*
* The NPS 1K/2K hashes use a similar style. These are designed to support up to
* 32 channel hashes, which causes up to 5 bits to be used. The 5 bit form is
* only supported in the 1K variant. It starts at bit 8 (the nominally required
* starting interleave address) and then uses bit 9, before jumping up to bits
* 12-14 as required. The XOR addresses count up in a similar fashion. So the 64
* KiB interleave would use up to bits 16-20 in this scheme (corresponding to
* result bits 0-4).
*
* When the 2K form is used, only 4 bits are supported and the entire bit 9 row
* is ignored. This looks very similar to the NPS form; however, the gap is also
* there in the XOR bits and there is no longer the question of using bit 14 or
* not with socket interleaving. It is only ever used if we need the 5th channel
* bit. To see the difference let's look at two examples where the only
* difference between the two is whether we are using 1 or 2K hashing.
*
* o 8-channel "NPS" 1K hashing, starting at address 8. 64 KiB, 2 MiB, 1 GiB,
* and 1 TiB are enabled. 1-die and 1-socket.
*
* In this model, there are always 3 bits for the channel. This means that
* we only will use bits 8, 9, and 12 from the address to start with.
*
* Channel ID[0] = addr[8] ^ addr[16] ^ addr[21] ^ addr[30]
* Channel ID[1] = addr[9] ^ addr[17] ^ addr[22] ^ addr[31]
* Channel ID[2] = addr[12] ^ addr[18] ^ addr[23] ^ addr[32]
*
* o 8-channel "NPS" 2K hashing, starting at address 8. 64 KiB, 2 MiB, 1 GiB,
* and 1 TiB are enabled. 1-die and 1-socket.
*
* In this model, we also use 3 bits for the channel. However, we no longer
* use bit 9, which is the 1K mode only. Similarly, you'll see that the bits
* from the hash that would have been used for determining interleaving with
* bit 9 are skipped entirely. This is why the 1K/2K variants are
* incompatible with the original NPS hashing.
*
* Channel ID[0] = addr[8] ^ addr[16] ^ addr[21] ^ addr[30]
* Channel ID[1] = addr[12] ^ addr[18] ^ addr[23] ^ addr[32]
* Channel ID[2] = addr[13] ^ addr[19] ^ addr[24] ^ addr[33]
*
* ZEN 3 6-CHANNEL
*
* These were the simple cases. Things get more complex when we move to
* non-power of 2 based hashes between channels. There are two different sets of
* these schemes. The first of these is 6-channel hashing that was added in Zen
* 3. The second of these is a more complex and general form that was added in
* Zen 4. Let's start with the Zen 3 case. The Zen 3 6-channel hash requires
* starting at address bits 11 or 12 and varies its logic somewhat from there.
* In the 6-channel world, the socket and die interleaving must be disabled.
* Let's walk through an example:
*
* o 6-channel Zen 3, starting at address 11. 2M and 1G range enabled.
* 1-die and 1-socket interleaving.
*
* Regardless of the starting address, we will always use three bits to
* determine a channel address. However, it's worth calling out that the
* 64K range is not considered for this at all. Another oddity is that when
* calculating the hash bits the order of the extracted 2M and 1G addresses
* are different.
*
* This flow starts by calculating the three hash bits. This is defined
* below. In the following, all bits marked with an '@' are ones that will
* change when starting at address bit 12. In those cases the value will
* increase by 1. Here's how we calculate the hash bits:
*
* hash[0] = addr[11@] ^ addr[14@] ^ addr[23] ^ addr[32]
* hash[1] = addr[12@] ^ addr[21] ^ addr[30]
* hash[2] = addr[13@] ^ addr[22] ^ addr[31]
*
* With this calculated, we always assign the first bit of the channel
* based on the hash. The other bits are more complicated as we have to
* deal with that gnarly power of two problem. We determine whether or not
* to use the hash bits directly in the channel based on their value. If
* they are not equal to 3, then we use it, otherwise if they are, then we
* need to go back to the physical address and we take its modulus.
* Basically:
*
* Channel Id[0] = hash[0]
* if (hash[2:1] == 3)
* Channel ID[2:1] = (addr >> [11@+3]) % 3
* else
* Channel ID[2:1] = hash[2:1]
*
*
* ZEN 4 NON-POWER OF 2
*
* I hope you like modulus calculations, because things get even more complex
* here now in Zen 4 which has many more modulus variations. These function in a
* similar way to the older 6-channel hash in Milan. They require one to start
* at address bit 8, they require that there is no die interleaving, and they
* support socket interleaving. The different channel arrangements end up in one
* of two sets of modulus values: a mod % 3 and a mod % 5 based on the number
* of channels used. Unlike the Milan form, all three address ranges (64 KiB, 2
* MiB, 1 GiB) are allowed to be used.
*
* o 6-channel Zen 4, starting at address 8. 64K, 2M, and 1G range enabled.
* 1-die and 2-socket interleaving.
*
* We start by calculating the following set of hash bits regardless of
* the number of channels that exist. The set of hash bits that is actually
* used in various computations ends up varying based upon the number of
* channels used. In 3-5 configs, only hash[0] is used. 6-10, both hash[0]
* and hash[2] (yes, not hash[1]). The 12 channel config uses all three.
*
* hash[0] = addr[8] ^ addr[16] ^ addr[21] ^ addr[30] ^ addr[14]
* hash[1] = addr[12] ^ addr[17] ^ addr[22] ^ addr[31]
* hash[2] = addr[13] ^ addr[18] ^ addr[23] ^ addr[32]
*
* Unlike other schemes where bits directly map here, they instead are used
* to seed the overall value. Depending on whether hash[0] is a 0 or 1, the
* system goes through two different calculations entirely. Though all of
* them end up involving the remainder of the system address going through
* the modulus. In the following, a '3@' indicates the modulus value would
* be swapped to 5 in a different scenario.
*
* Channel ID = addr[63:14] % 3@
* if (hash[0] == 1)
* Channel ID = (Channel ID + 1) % 3@
*
* Once this base has for the channel ID has been calculated, additional
* portions are added in. As this is the 6-channel form, we say:
*
* Channel ID = Channel ID + (hash[2] * 3@)
*
* Finally the socket is deterministic and always comes from hash[0].
* Basically:
*
* Socket ID = hash[0]
*
* o 12-channel Zen 4, starting at address 8. 64K, 2M, and 1G range enabled.
* 1-die and 1-socket interleaving.
*
* This is a variant of the above. The hash is calculated the same way.
* The base Channel ID is the same and if socket interleaving were enabled
* it would also be hash[0]. What instead differs is how we use hash[1]
* and hash[2]. The following logic is used instead of the final
* calculation above.
*
* Channel ID = Channel ID + (hash[2:1] * 3@)
*
* NPS 1K/2K NON-POWER of 2
*
* Just as the normal hashing changed with the introduction of the 1K/2K
* variants, so does the non-power of 2 hashing. This NP2 scheme is rather
* different than the base Zen 4 one. This uses the 64 KiB, 2 MiB, 1 GiB, and 1
* TiB ranges for hashing. Logically there are both 3 and 5 channel hashes again
* like Zen 4 and when socket interleaving is enabled, address bit 8 is always
* going to the socket.
*
* The 1K and 2K modes change which addresses are used and considered just like
* the non-NP2 case. The same interleave bit skipping for 2K still applies,
* meaning bit 9 will not be used for hashing and will instead be part of the
* normal address calculations that we have.
*
* Like in the Zen 4 case, we are going to be constructing our normalized
* address from three regions of bits. The low region which is everything that
* is used before the hashing, the bits skipped in the middle, and then the
* upper bits that have been untouched. These are not rearranged, rather its
* best to think of it as bits are removed from this, causing shifts and
* shrinks.
*
* Another important difference to call out before we get to examples is that
* each variant here uses a different address range as the upper portion to use.
* Unfortunately, where as for Zen 4 we had some regular rules, each of these
* cases seems rather different. However, there is some general logic which is
* that in each case we calculate some modulus value from different addresses
* which we use to determine the channel, sometimes mixed with other hash bits.
* Then we calculate a new normalized address by taking the divisor as the high
* portion. Let's look at some examples here:
*
* o 12 Channel 1K Zen 5, starting at address 8. 64K, 2M, 1G, and 1T ranges
* enabled. 1-die and 1-socket interleaving.
*
* This 12 channel mode is a modulus 3 case. This particular case needs two
* hash bits. Because it is a 1K mode it uses bits 8 and 9. If we were in a
* 2K mode, we'd use bits 8 and 12. Bit 8 always also hashes in bit 14 just
* like the Zen 4 case.
*
* hash[0] = addr[8] ^ addr[16] ^ addr[21] ^ addr[30] ^ addr[40] ^
* addr[14]
* hash[1] = addr[9] ^ addr[17] ^ addr[22] ^ addr[31] ^ addr[41]
*
* Now that we have that, it's time to calculate the address we need to
* take the modulus of to stick into the channel. For this particular case,
* we construct an address as PA >> 12 | 0b00. In other words we take bits
* [48+, 12] and move them to bit 2. Once we have that, we can go ahead and
* construct the value modulus 3. Symbolically:
*
* modAddr = (addr[64:12] & ~3) | 0b00 (or (addr >> 12) << 2)
* modVal = modAddr % 3
*
* Channel ID[0] = hash[0]
* Channel ID[1] = hash[1]
* Channel ID[2] = modval[0]
* Channel ID[3] = modval[1]
*
* In the 2K version we use (addr[64:13] & ~7) | 0b000 and hash[1] is based
* on addr[12] rather than addr[9].
*
* o 5 Channel 2K Zen 5, starting at address 8. 64K, 2M, 1G, and 1T ranges
* enabled. 1-die and 1-socket interleaving.
*
* With the 5-channel based mode we now will working modulus five rather
* than three. In this case, we have similar logic, except the way the
* address is constructed to take the mod of is different. We can think of
* this as:
*
* modAddr = addr[64:12] | addr[8] | 0b0
* modVal = modAddr % 5
*
* Channel ID[0] = modVal[0]
* Channel ID[1] = modVal[1]
* Channel ID[2] = modVal[2]
*
* Basically this ends up using a rather similar logical construction;
* however, the values that it plugs in are different. Note, that there was
* no use of the hash in this case.
*
* POST BIT EXTRACTION
*
* Now, all of this was done to concoct up a series of indexes used. However,
* you'll note that a given DRAM rule actually already has a fabric target. So
* what do we do here? We add them together.
*
* The data fabric has registers that describe which bits in a fabric ID
* correspond to a socket, die, and channel. Taking the channel, die, and socket
* IDs above, one can construct a fabric ID. From there, we add the two data
* fabric IDs together and can then get to the fabric ID of the actual logical
* target. This is why all of the socket and die interleaving examples with no
* interleaving are OK to result in a zero. The idea here is that the base
* fabric ID in the DRAM rule will take care of indicating those other things as
* required.
*
* You'll note the use of the term "logical target" up above. That's because
* some platforms have the ability to remap logical targets to physical targets
* (identified by the use of the ZEN_UMC_FAM_F_TARG_REMAP flag in the family
* data or the DF::DfCapability register once we're at the DF 4D2 variant). The
* way that remapping works changes based on the hardware generation. This was
* first added in Milan (Zen 3) CPUs. In that model, you would use the socket
* and component information from the target ID to identify which remapping
* rules to use. On Genoa (Zen 4) CPUs, you would instead use information in the
* rule itself to determine which of the remap rule sets to use and then uses
* the component ID to select which rewrite rule to use.
*
* Finally, there's one small wrinkle with this whole scheme that we haven't
* discussed: what actually is the address that we plug into this calculation.
* While you might think it actually is just the system address itself, that
* isn't actually always the case. Sometimes rather than using the address
* itself, it gets normalized based on the DRAM rule, which involves subtracting
* out the base address and potentially subtracting out the size of the DRAM
* hole (if the address is above the hole and hoisting is active for that
* range). When this is performed appears to tie to the DF generation. The
* following table relates the DF generation to our behavior:
*
* o DF 2 (Zen 1): Use the raw address
* o DF 3 (Zen 2-3): Use the raw address if it's not a power of 2
* o DF 3.5: Use the adjusted address
* o DF 4 (Zen 4): Use the adjusted address
* o DF 4D2 (Zen 4/5): Use the raw address
*
* --------------------------------------------
* Data Fabric Interleave Address Normalization
* --------------------------------------------
*
* While you may have thought that we were actually done with the normalization
* fun in the last section, there's still a bit more here that we need to
* consider. In particular, there's a secondary transformation beyond
* interleaving that occurs as part of constructing the channel normalized
* address. Effectively, we need to account for all the bits that were used in
* the interleaving and generally speaking remove them from our normalized
* address.
*
* While this may sound weird on paper, the way to think about it is that
* interleaving at some granularity means that each device is grabbing the same
* set of addresses, the interleave just is used to direct it to its own
* location. When working with a channel normalized address, we're effectively
* creating a new region of addresses that have meaning within the DIMMs
* themselves. The channel doesn't care about what got it there, mainly just
* what it is now. So with that in mind, we need to discuss how we remove all
* the interleaving information in our different modes.
*
* Just to make sure it's clear, we are _removing_ all bits that were used for
* interleaving. This causes all bits above the removed ones to be shifted
* right.
*
* First, we have the case of standard power of 2 interleaving that applies to
* the 1, 2, 4, 8, 16, and 32 channel configurations. Here, we need to account
* for the total number of bits that are used for the channel, die, and socket
* interleaving and we simply remove all those bits starting from the starting
* address.
*
* o 8-channel interleave, 1-die interleave, 2-socket interleave
* Start at bit 9
*
* If we look at this example, we are using 3 bits for the channel, 1 for
* the socket, for a total of 4 bits. Because this is starting at bit 9,
* this means that interleaving covers the bit range [12:9]. In this case
* our new address would be (orig[63:13] >> 4) | orig[8:0].
*
*
* COD and NPS HASHING
*
* That was the simple case, next we have the COD/NPS hashing case that we need
* to consider. If we look at these, the way that they work is that they split
* which bits they use for determining the channel address and then hash others
* in. Here, we need to extract the starting address bit, then continue at bit
* 12 based on the number of bits in use and whether or not socket interleaving
* is at play for the NPS variant. Let's look at an example here:
*
* o 8-channel "COD" hashing, starting at address 9. All three ranges enabled.
* 1-die and 1-socket interleaving.
*
* Here we have three total bits being used. Because we start at bit 9, this
* means we need to drop bits [13:12], [9]. So our new address would be:
*
* orig[63:14] >> 3 | orig[11:10] >> 1 | orig[8:0]
* | | +-> stays the same
* | +-> relocated to bit 9 -- shifted by 1 because we
* | removed bit 9.
* +--> Relocated to bit 11 -- shifted by 3 because we removed bits, 9, 12,
* and 13.
*
* o 8-channel "NPS" hashing, starting at address 8. All three ranges enabled.
* 1-die and 2-socket interleaving.
*
* Here we need to remove bits [14:12], [8]. We're removing an extra bit
* because we have 2-socket interleaving. This results in a new address of:
*
* orig[63:15] >> 4 | orig[11:9] >> 1 | orig[7:0]
* | | +-> stays the same
* | +-> relocated to bit 8 -- shifted by 1 because we
* | removed bit 8.
* +--> Relocated to bit 11 -- shifted by 4 because we removed bits, 8, 12,
* 13, and 14.
*
* NPS 1K/2K Hashing
*
* This case is a fairly straightforward variant on what we just discussed. In
* fact, 2K hashing looks just like what we've done before. The only difference
* with 1K hashing is that we'll consider bit 9 also for removal before we jump
* up to bit 12. Let's look at an example:
*
* o 8-channel "NPS" 1K hashing, starting at address 8. All three ranges
* enabled. 1-die and 2-socket interleaving.
*
* Here we need to remove a total of 4 bits, which is now broken into
* [13:12] and [9:8]. This results in a new address of:
*
* orig[63:14] >> 4 | orig[11:10] >> 2 | orig[7:0]
* | | +-> stays the same
* | +-> relocated to bit 8 -- shifted by 2 because we
* | removed bits 8 and 9.
* +--> Relocated to bit 11 -- shifted by 4 because we removed bits, 8, 9,
* 12, and 13.
*
* ZEN 3 6-CHANNEL
*
* Now, to the real fun stuff, our non-powers of two. First, let's start with
* our friend, the Zen 3 6-channel hash. So, the first thing that we need to do
* here is start by recomputing our hash again based on the current normalized
* address. Regardless of the hash value, this first removes all three bits from
* the starting address, so that's removing either [14:12] or [13:11].
*
* The rest of the normalization process here is quite complex and somewhat mind
* bending. Let's start working through an example here and build this up.
* First, let's assume that each channel has a single 16 GiB RDIMM. This would
* mean that the channel itself has 96 GiB RDIMM. However, by removing 3 bits
* worth, that technically corresponds to an 8-channel configuration that
* normally suggest a 128 GiB configuration. The processor requires us to record
* this fact in the DF::Np2ChannelConfig register. The value that it wants us a
* bit weird. We believe it's calculated by the following:
*
* 1. Round the channel size up to the next power of 2.
* 2. Divide this total size by 64 KiB.
* 3. Determine the log base 2 that satisfies this value.
*
* In our particular example above. We have a 96 GiB channel, so for (1) we end
* up with 128 GiB (2^37). We now divide that by 64 KiB (2^16), so this becomes
* 2^(37 - 16) or 2^21. Because we want the log base 2 of 2^21 from (2), this
* simply becomes 21. The DF::Np2ChannelConfig has two members, a 'space 0' and
* 'space 1'. Near as we can tell, in this mode only 'space 0' is used.
*
* Before we get into the actual normalization scheme, we have to ask ourselves
* how do we actually interleave data 6 ways. The scheme here is involved.
* First, it's important to remember like with other normalization schemes, we
* do adjust for the address for the base address in the DRAM rule and then also
* take into account the DRAM hole if present.
*
* If we delete 3 bits, let's take a sample address and see where it would end
* up in the above scheme. We're going to take our 3 address bits and say that
* they start at bit 12, so this means that the bits removed are [14:12]. So the
* following are the 8 addresses that we have here and where they end up
* starting with 1ff:
*
* o 0x01ff -> 0x1ff, Channel 0 (hash 0b000)
* o 0x11ff -> 0x1ff, Channel 1 (hash 0b001)
* o 0x21ff -> 0x1ff, Channel 2 (hash 0b010)
* o 0x31ff -> 0x1ff, Channel 3 (hash 0b011)
* o 0x41ff -> 0x1ff, Channel 4 (hash 0b100)
* o 0x51ff -> 0x1ff, Channel 5 (hash 0b101)
* o 0x61ff -> 0x3000001ff, Channel 0 (hash 0b110)
* o 0x71ff -> 0x3000001ff, Channel 1 (hash 0b111)
*
* Yes, we did just jump to near the top of what is a 16 GiB DIMM's range for
* those last two. The way we determine when to do this jump is based on our
* hash. Effectively we ask what is hash[2:1]. If it is 0b11, then we need to
* do something different and enter this special case, basically jumping to the
* top of the range. If we think about a 6-channel configuration for a moment,
* the thing that doesn't exist are the traditional 8-channel hash DIMMs 0b110
* and 0b111.
*
* If you go back to the interleave this kind of meshes, that tried to handle
* the case of the hash being 0, 1, and 2, normally, and then did special things
* with the case of the hash being in this upper quadrant. The hash then
* determined where it went by shifting over the upper address and doing a mod
* 3 and using that to determine the upper two bits. With that weird address at
* the top of the range, let's go through and see what else actually goes to
* those weird addresses:
*
* o 0x08000061ff -> 0x3000001ff, Channel 2 (hash 0b110)
* o 0x08000071ff -> 0x3000001ff, Channel 3 (hash 0b111)
* o 0x10000061ff -> 0x3000001ff, Channel 4 (hash 0b110)
* o 0x10000071ff -> 0x3000001ff, Channel 5 (hash 0b111)