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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
// SPDX-License-Identifier: GPL-2.0-only
// Copyright(c) 2021 Intel Corporation. All rights reserved.

#include <linux/platform_device.h>
#include <linux/memory_hotplug.h>
#include <linux/genalloc.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/acpi.h>
#include <linux/pci.h>
#include <linux/mm.h>
#include <cxlmem.h>

#include "../watermark.h"
#include "mock.h"

static int interleave_arithmetic;
static bool extended_linear_cache;
static bool fail_autoassemble;
static bool type2_test;

#define FAKE_QTG_ID	42

#define NR_CXL_HOST_BRIDGES 2
#define NR_CXL_SINGLE_HOST 1
#define NR_CXL_RCH 1
#define NR_CXL_ROOT_PORTS 2
#define NR_CXL_SWITCH_PORTS 2
#define NR_CXL_PORT_DECODERS 8
#define NR_BRIDGES (NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + NR_CXL_RCH)
#define NR_CXL_TYPE2_ACCEL 1

#define MOCK_AUTO_REGION_SIZE_DEFAULT SZ_512M
static int mock_auto_region_size = MOCK_AUTO_REGION_SIZE_DEFAULT;

static struct platform_device *cxl_acpi;
static struct platform_device *cxl_host_bridge[NR_CXL_HOST_BRIDGES];
#define NR_MULTI_ROOT (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS)
static struct platform_device *cxl_root_port[NR_MULTI_ROOT];
static struct platform_device *cxl_switch_uport[NR_MULTI_ROOT];
#define NR_MEM_MULTI \
	(NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS * NR_CXL_SWITCH_PORTS)
static struct platform_device *cxl_switch_dport[NR_MEM_MULTI];

static struct platform_device *cxl_hb_single[NR_CXL_SINGLE_HOST];
static struct platform_device *cxl_root_single[NR_CXL_SINGLE_HOST];
static struct platform_device *cxl_swu_single[NR_CXL_SINGLE_HOST];
#define NR_MEM_SINGLE (NR_CXL_SINGLE_HOST * NR_CXL_SWITCH_PORTS)
static struct platform_device *cxl_swd_single[NR_MEM_SINGLE];

struct platform_device *cxl_mem[NR_MEM_MULTI];
struct platform_device *cxl_mem_single[NR_MEM_SINGLE];

static struct platform_device *cxl_rch[NR_CXL_RCH];
static struct platform_device *cxl_rcd[NR_CXL_RCH];

/*
 * Decoder registry
 *
 * Record decoder programming so that the topology can be reconstructed
 * after cxl_acpi unbind/bind. This allows a user-created region config
 * to be replayed as if firmware had provided the region at enumeration
 * time.
 *
 * Entries are keyed by a stable port identity (port->uport_dev) combined
 * with the decoder id. Decoder state is saved at initialization and
 * updated on commit and reset.
 *
 * On re-enumeration mock_init_hdm_decoder() consults this registry to
 * restore enabled decoders. Disabled decoders are reinitialized to a
 * clean default state rather than replaying stale programming.
 */
static DEFINE_XARRAY(decoder_registry);

/*
 * When set, decoder reset will not update the registry. This allows
 * region destroy operations to reset live decoders without erasing
 * the saved programming needed for replay after re-enumeration.
 */
static bool decoder_reset_preserve_registry;

static inline bool is_multi_bridge(struct device *dev)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
		if (&cxl_host_bridge[i]->dev == dev)
			return true;
	return false;
}

static inline bool is_single_bridge(struct device *dev)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
		if (&cxl_hb_single[i]->dev == dev)
			return true;
	return false;
}

static struct acpi_device acpi0017_mock;
static struct acpi_device host_bridge[NR_BRIDGES] = {
	[0] = {
		.handle = &host_bridge[0],
		.pnp.unique_id = "0",
	},
	[1] = {
		.handle = &host_bridge[1],
		.pnp.unique_id = "1",
	},
	[2] = {
		.handle = &host_bridge[2],
		.pnp.unique_id = "2",
	},
	[3] = {
		.handle = &host_bridge[3],
		.pnp.unique_id = "3",
	},
};

static bool is_mock_dev(struct device *dev)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(cxl_mem); i++)
		if (dev == &cxl_mem[i]->dev)
			return true;
	for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++)
		if (dev == &cxl_mem_single[i]->dev)
			return true;
	for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++)
		if (dev == &cxl_rcd[i]->dev)
			return true;
	if (dev == &cxl_acpi->dev)
		return true;
	return false;
}

static bool is_mock_adev(struct acpi_device *adev)
{
	int i;

	if (adev == &acpi0017_mock)
		return true;

	for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
		if (adev == &host_bridge[i])
			return true;

	return false;
}

static struct {
	struct acpi_table_cedt cedt;
	struct acpi_cedt_chbs chbs[NR_BRIDGES];
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[1];
	} cfmws0;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[2];
	} cfmws1;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[1];
	} cfmws2;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[2];
	} cfmws3;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[1];
	} cfmws4;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[1];
	} cfmws5;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[1];
	} cfmws6;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[2];
	} cfmws7;
	struct {
		struct acpi_cedt_cfmws cfmws;
		u32 target[3];
	} cfmws8;
	struct {
		struct acpi_cedt_cxims cxims;
		u64 xormap_list[2];
	} cxims0;
} __packed mock_cedt = {
	.cedt = {
		.header = {
			.signature = "CEDT",
			.length = sizeof(mock_cedt),
			.revision = 1,
		},
	},
	.chbs[0] = {
		.header = {
			.type = ACPI_CEDT_TYPE_CHBS,
			.length = sizeof(mock_cedt.chbs[0]),
		},
		.uid = 0,
		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
	},
	.chbs[1] = {
		.header = {
			.type = ACPI_CEDT_TYPE_CHBS,
			.length = sizeof(mock_cedt.chbs[0]),
		},
		.uid = 1,
		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
	},
	.chbs[2] = {
		.header = {
			.type = ACPI_CEDT_TYPE_CHBS,
			.length = sizeof(mock_cedt.chbs[0]),
		},
		.uid = 2,
		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
	},
	.chbs[3] = {
		.header = {
			.type = ACPI_CEDT_TYPE_CHBS,
			.length = sizeof(mock_cedt.chbs[0]),
		},
		.uid = 3,
		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL11,
	},
	.cfmws0 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws0),
			},
			.interleave_ways = 0,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 4UL,
		},
		.target = { 0 },
	},
	.cfmws1 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws1),
			},
			.interleave_ways = 1,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 8UL,
		},
		.target = { 0, 1, },
	},
	.cfmws2 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws2),
			},
			.interleave_ways = 0,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 4UL,
		},
		.target = { 0 },
	},
	.cfmws3 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws3),
			},
			.interleave_ways = 1,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 8UL,
		},
		.target = { 0, 1, },
	},
	.cfmws4 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws4),
			},
			.interleave_ways = 0,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 4UL,
		},
		.target = { 2 },
	},
	.cfmws5 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws5),
			},
			.interleave_ways = 0,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M > PMD_SIZE ? SZ_256M : PMD_SIZE,
		},
		.target = { 3 },
	},
	/* .cfmws6,7,8 use ACPI_CEDT_CFMWS_ARITHMETIC_XOR */
	.cfmws6 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws6),
			},
			.interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
			.interleave_ways = 0,
			.granularity = 4,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 8UL,
		},
		.target = { 0, },
	},
	.cfmws7 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws7),
			},
			.interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
			.interleave_ways = 1,
			.granularity = 0,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_256M * 8UL,
		},
		.target = { 0, 1, },
	},
	.cfmws8 = {
		.cfmws = {
			.header = {
				.type = ACPI_CEDT_TYPE_CFMWS,
				.length = sizeof(mock_cedt.cfmws8),
			},
			.interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
			.interleave_ways = 8,
			.granularity = 1,
			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
			.qtg_id = FAKE_QTG_ID,
			.window_size = SZ_512M * 6UL,
		},
		.target = { 0, 1, 2, },
	},
	.cxims0 = {
		.cxims = {
			.header = {
				.type = ACPI_CEDT_TYPE_CXIMS,
				.length = sizeof(mock_cedt.cxims0),
			},
			.hbig = 0,
			.nr_xormaps = 2,
		},
		.xormap_list = { 0x404100, 0x808200, },
	},
};

static struct acpi_cedt_cfmws type2_cfmws0 = {
	.header = {
		.type = ACPI_CEDT_TYPE_CFMWS,
		.length = sizeof(mock_cedt.cfmws0),
	},
	.interleave_ways = 0,
	.granularity = 4,
	.restrictions = ACPI_CEDT_CFMWS_RESTRICT_DEVMEM |
			ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
	.qtg_id = FAKE_QTG_ID,
	.window_size = SZ_256M * 4,
};

struct acpi_cedt_cfmws *mock_cfmws[] = {
	[0] = &mock_cedt.cfmws0.cfmws,
	[1] = &mock_cedt.cfmws1.cfmws,
	[2] = &mock_cedt.cfmws2.cfmws,
	[3] = &mock_cedt.cfmws3.cfmws,
	[4] = &mock_cedt.cfmws4.cfmws,
	[5] = &mock_cedt.cfmws5.cfmws,
	/* Modulo Math above, XOR Math below */
	[6] = &mock_cedt.cfmws6.cfmws,
	[7] = &mock_cedt.cfmws7.cfmws,
	[8] = &mock_cedt.cfmws8.cfmws,
};

static int cfmws_start;
static int cfmws_end;
#define CFMWS_MOD_ARRAY_START 0
#define CFMWS_MOD_ARRAY_END   5
#define CFMWS_XOR_ARRAY_START 6
#define CFMWS_XOR_ARRAY_END   8

struct acpi_cedt_cxims *mock_cxims[1] = {
	[0] = &mock_cedt.cxims0.cxims,
};

struct cxl_mock_res {
	struct list_head list;
	struct range range;
};

static LIST_HEAD(mock_res);
static DEFINE_MUTEX(mock_res_lock);
static struct gen_pool *cxl_mock_pool;

static void depopulate_all_mock_resources(void)
{
	struct cxl_mock_res *res, *_res;

	mutex_lock(&mock_res_lock);
	list_for_each_entry_safe(res, _res, &mock_res, list) {
		gen_pool_free(cxl_mock_pool, res->range.start,
			      range_len(&res->range));
		list_del(&res->list);
		kfree(res);
	}
	mutex_unlock(&mock_res_lock);
}

static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
{
	struct genpool_data_align data = {
		.align = align,
	};
	unsigned long phys;

	struct cxl_mock_res *res __free(kfree) = kzalloc(sizeof(*res),
							 GFP_KERNEL);
	if (!res)
		return NULL;

	INIT_LIST_HEAD(&res->list);
	phys = gen_pool_alloc_algo(cxl_mock_pool, size,
				   gen_pool_first_fit_align, &data);
	if (!phys)
		return NULL;

	res->range = (struct range) {
		.start = phys,
		.end = phys + size - 1,
	};
	mutex_lock(&mock_res_lock);
	list_add(&res->list, &mock_res);
	mutex_unlock(&mock_res_lock);

	return no_free_ptr(res);
}

/* Only update CFMWS0 as this is used by the auto region. */
static void cfmws_elc_update(struct acpi_cedt_cfmws *window, int index)
{
	if (!extended_linear_cache)
		return;

	if (index != 0)
		return;

	/*
	 * The window size should be 2x of the CXL region size where half is
	 * DRAM and half is CXL
	 */
	window->window_size = mock_auto_region_size * 2;
}

static void update_type2_cfmws(void)
{
	memcpy(&mock_cedt.cfmws0.cfmws, &type2_cfmws0, sizeof(type2_cfmws0));
}

static int populate_cedt(void)
{
	struct cxl_mock_res *res;
	int i;

	for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
		struct acpi_cedt_chbs *chbs = &mock_cedt.chbs[i];
		resource_size_t size;

		if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL20)
			size = ACPI_CEDT_CHBS_LENGTH_CXL20;
		else
			size = ACPI_CEDT_CHBS_LENGTH_CXL11;

		res = alloc_mock_res(size, size);
		if (!res)
			return -ENOMEM;
		chbs->base = res->range.start;
		chbs->length = size;
	}

	if (type2_test)
		update_type2_cfmws();

	for (i = cfmws_start; i <= cfmws_end; i++) {
		struct acpi_cedt_cfmws *window = mock_cfmws[i];
		int align = SZ_256M;

		if (i == 0 && !type2_test)
			cfmws_elc_update(window, i);
		if (window->restrictions & ACPI_CEDT_CFMWS_RESTRICT_VOLATILE)
			align = max_t(int, SZ_256M, PMD_SIZE);
		res = alloc_mock_res(window->window_size, align);
		if (!res)
			return -ENOMEM;
		window->base_hpa = res->range.start;
	}

	return 0;
}

static bool is_mock_port(struct device *dev);

/*
 * WARNING, this hack assumes the format of 'struct cxl_cfmws_context'
 * and 'struct cxl_chbs_context' share the property that the first
 * struct member is a cxl_test device being probed by the cxl_acpi
 * driver.
 */
struct cxl_cedt_context {
	struct device *dev;
};

static int mock_acpi_table_parse_cedt(enum acpi_cedt_type id,
				      acpi_tbl_entry_handler_arg handler_arg,
				      void *arg)
{
	struct cxl_cedt_context *ctx = arg;
	struct device *dev = ctx->dev;
	union acpi_subtable_headers *h;
	unsigned long end;
	int i;

	if (!is_mock_port(dev) && !is_mock_dev(dev))
		return acpi_table_parse_cedt(id, handler_arg, arg);

	if (id == ACPI_CEDT_TYPE_CHBS)
		for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
			h = (union acpi_subtable_headers *)&mock_cedt.chbs[i];
			end = (unsigned long)&mock_cedt.chbs[i + 1];
			handler_arg(h, arg, end);
		}

	if (id == ACPI_CEDT_TYPE_CFMWS)
		for (i = cfmws_start; i <= cfmws_end; i++) {
			h = (union acpi_subtable_headers *) mock_cfmws[i];
			end = (unsigned long) h + mock_cfmws[i]->header.length;
			handler_arg(h, arg, end);
		}

	if (id == ACPI_CEDT_TYPE_CXIMS)
		for (i = 0; i < ARRAY_SIZE(mock_cxims); i++) {
			h = (union acpi_subtable_headers *)mock_cxims[i];
			end = (unsigned long)h + mock_cxims[i]->header.length;
			handler_arg(h, arg, end);
		}

	return 0;
}

static bool is_mock_bridge(struct device *dev)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
		if (dev == &cxl_host_bridge[i]->dev)
			return true;
	for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
		if (dev == &cxl_hb_single[i]->dev)
			return true;
	for (i = 0; i < ARRAY_SIZE(cxl_rch); i++)
		if (dev == &cxl_rch[i]->dev)
			return true;

	return false;
}

static bool is_mock_port(struct device *dev)
{
	int i;

	if (is_mock_bridge(dev))
		return true;

	for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++)
		if (dev == &cxl_root_port[i]->dev)
			return true;

	for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++)
		if (dev == &cxl_switch_uport[i]->dev)
			return true;

	for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++)
		if (dev == &cxl_switch_dport[i]->dev)
			return true;

	for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++)
		if (dev == &cxl_root_single[i]->dev)
			return true;

	for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++)
		if (dev == &cxl_swu_single[i]->dev)
			return true;

	for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++)
		if (dev == &cxl_swd_single[i]->dev)
			return true;

	if (is_cxl_memdev(dev))
		return is_mock_dev(dev->parent);

	return false;
}

static int host_bridge_index(struct acpi_device *adev)
{
	return adev - host_bridge;
}

static struct acpi_device *find_host_bridge(acpi_handle handle)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
		if (handle == host_bridge[i].handle)
			return &host_bridge[i];
	return NULL;
}

static acpi_status
mock_acpi_evaluate_integer(acpi_handle handle, acpi_string pathname,
			   struct acpi_object_list *arguments,
			   unsigned long long *data)
{
	struct acpi_device *adev = find_host_bridge(handle);

	if (!adev || strcmp(pathname, METHOD_NAME__UID) != 0)
		return acpi_evaluate_integer(handle, pathname, arguments, data);

	*data = host_bridge_index(adev);
	return AE_OK;
}

static int
mock_hmat_get_extended_linear_cache_size(struct resource *backing_res,
					 int nid, resource_size_t *cache_size)
{
	struct acpi_cedt_cfmws *window = mock_cfmws[0];
	struct resource cfmws0_res =
		DEFINE_RES_MEM(window->base_hpa, window->window_size);

	if (!extended_linear_cache ||
	    !resource_contains(&cfmws0_res, backing_res)) {
		return hmat_get_extended_linear_cache_size(backing_res,
							   nid, cache_size);
	}

	*cache_size = mock_auto_region_size;

	return 0;
}

static struct pci_bus mock_pci_bus[NR_BRIDGES];
static struct acpi_pci_root mock_pci_root[ARRAY_SIZE(mock_pci_bus)] = {
	[0] = {
		.bus = &mock_pci_bus[0],
	},
	[1] = {
		.bus = &mock_pci_bus[1],
	},
	[2] = {
		.bus = &mock_pci_bus[2],
	},
	[3] = {
		.bus = &mock_pci_bus[3],
	},

};

static bool is_mock_bus(struct pci_bus *bus)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(mock_pci_bus); i++)
		if (bus == &mock_pci_bus[i])
			return true;
	return false;
}

static struct acpi_pci_root *mock_acpi_pci_find_root(acpi_handle handle)
{
	struct acpi_device *adev = find_host_bridge(handle);

	if (!adev)
		return acpi_pci_find_root(handle);
	return &mock_pci_root[host_bridge_index(adev)];
}

static struct cxl_hdm *mock_cxl_setup_hdm(struct cxl_port *port,
					  struct cxl_endpoint_dvsec_info *info)
{
	struct cxl_hdm *cxlhdm = devm_kzalloc(&port->dev, sizeof(*cxlhdm), GFP_KERNEL);
	struct device *dev = &port->dev;

	if (!cxlhdm)
		return ERR_PTR(-ENOMEM);

	cxlhdm->port = port;
	cxlhdm->interleave_mask = ~0U;
	cxlhdm->iw_cap_mask = ~0UL;
	dev_set_drvdata(dev, cxlhdm);
	return cxlhdm;
}

struct target_map_ctx {
	u32 *target_map;
	int index;
	int target_count;
};

static int map_targets(struct device *dev, void *data)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct target_map_ctx *ctx = data;

	ctx->target_map[ctx->index++] = pdev->id;

	if (ctx->index > ctx->target_count) {
		dev_WARN_ONCE(dev, 1, "too many targets found?\n");
		return -ENXIO;
	}

	return 0;
}

/*
 * Build a stable registry key from the decoder's upstream port identity
 * and decoder id.
 *
 * Decoder objects and cxl_port objects are reallocated on each enumeration,
 * so their addresses cannot be used directly as replay keys. However,
 * port->uport_dev is stable for a given topology across cxl_acpi unbind/bind
 * in cxl_test, so use that as the port identity and pack the local decoder
 * id into the low bits.
 *
 * The key is formed as:
 *     ((unsigned long)port->uport_dev << 4) | cxld->id
 *
 * The low bits hold the decoder id (which must fit in 4 bits) while
 * the remaining bits identify the upstream port. This key is only used
 * within cxl_test to locate saved decoder state during replay.
 */
static unsigned long cxld_registry_index(struct cxl_decoder *cxld)
{
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);

	dev_WARN_ONCE(&port->dev, cxld->id >= 16,
		      "decoder id:%d out of range\n", cxld->id);
	return (((unsigned long)port->uport_dev) << 4) | cxld->id;
}

struct cxl_test_decoder {
	union {
		struct cxl_switch_decoder cxlsd;
		struct cxl_endpoint_decoder cxled;
	};
	struct range dpa_range;
};

static struct cxl_test_decoder *cxld_registry_find(struct cxl_decoder *cxld)
{
	return xa_load(&decoder_registry, cxld_registry_index(cxld));
}

#define dbg_cxld(port, msg, cxld)                                                       \
	do {                                                                            \
		struct cxl_decoder *___d = (cxld);                                      \
		dev_dbg((port)->uport_dev,                                              \
			"decoder%d: %s range: %#llx-%#llx iw: %d ig: %d flags: %#lx\n", \
			___d->id, msg, ___d->hpa_range.start,                           \
			___d->hpa_range.end + 1, ___d->interleave_ways,                 \
			___d->interleave_granularity, ___d->flags);                     \
	} while (0)

static int mock_decoder_commit(struct cxl_decoder *cxld);
static void mock_decoder_reset(struct cxl_decoder *cxld);
static void init_disabled_mock_decoder(struct cxl_decoder *cxld);

static void cxld_copy(struct cxl_decoder *a, struct cxl_decoder *b)
{
	a->id = b->id;
	a->hpa_range = b->hpa_range;
	a->interleave_ways = b->interleave_ways;
	a->interleave_granularity = b->interleave_granularity;
	a->target_type = b->target_type;
	a->flags = b->flags;
	a->commit = mock_decoder_commit;
	a->reset = mock_decoder_reset;
}

/*
 * Restore decoder programming saved in the registry.
 *
 * Only decoders that were saved enabled are restored. Disabled decoders
 * are left in their default inactive state so that stale programming is
 * not resurrected after topology replay.
 *
 * For endpoint decoders this also restores the DPA reservation needed
 * to reconstruct committed mappings.
 */
static int cxld_registry_restore(struct cxl_decoder *cxld,
				 struct cxl_test_decoder *td)
{
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
	int rc;

	if (is_switch_decoder(&cxld->dev)) {
		struct cxl_switch_decoder *cxlsd =
			to_cxl_switch_decoder(&cxld->dev);

		if (!(td->cxlsd.cxld.flags & CXL_DECODER_F_ENABLE))
			return 0;

		dbg_cxld(port, "restore", &td->cxlsd.cxld);
		cxld_copy(cxld, &td->cxlsd.cxld);
		WARN_ON(cxlsd->nr_targets != td->cxlsd.nr_targets);

		/* Restore saved target intent; live dport binding happens later */
		for (int i = 0; i < cxlsd->nr_targets; i++) {
			cxlsd->target[i] = NULL;
			cxld->target_map[i] = td->cxlsd.cxld.target_map[i];
		}

		port->commit_end = cxld->id;

	} else {
		struct cxl_endpoint_decoder *cxled =
			to_cxl_endpoint_decoder(&cxld->dev);

		if (!(td->cxled.cxld.flags & CXL_DECODER_F_ENABLE))
			return 0;

		dbg_cxld(port, "restore", &td->cxled.cxld);
		cxld_copy(cxld, &td->cxled.cxld);
		cxled->state = td->cxled.state;
		cxled->skip = td->cxled.skip;
		if (range_len(&td->dpa_range)) {
			rc = devm_cxl_dpa_reserve(cxled, td->dpa_range.start,
						  range_len(&td->dpa_range),
						  td->cxled.skip);
			if (rc) {
				init_disabled_mock_decoder(cxld);
				return rc;
			}
		}
		port->commit_end = cxld->id;
	}

	return 0;
}

static void __cxld_registry_save(struct cxl_test_decoder *td,
				 struct cxl_decoder *cxld)
{
	if (is_switch_decoder(&cxld->dev)) {
		struct cxl_switch_decoder *cxlsd =
			to_cxl_switch_decoder(&cxld->dev);

		cxld_copy(&td->cxlsd.cxld, cxld);
		td->cxlsd.nr_targets = cxlsd->nr_targets;

		/* Save target port_id as a stable identify for the dport */
		for (int i = 0; i < cxlsd->nr_targets; i++) {
			struct cxl_dport *dport;

			if (!cxlsd->target[i])
				continue;

			dport = cxlsd->target[i];
			td->cxlsd.cxld.target_map[i] = dport->port_id;
		}
	} else {
		struct cxl_endpoint_decoder *cxled =
			to_cxl_endpoint_decoder(&cxld->dev);

		cxld_copy(&td->cxled.cxld, cxld);
		td->cxled.state = cxled->state;
		td->cxled.skip = cxled->skip;

		if (!(cxld->flags & CXL_DECODER_F_ENABLE)) {
			td->dpa_range.start = 0;
			td->dpa_range.end = -1;
		} else if (cxled->dpa_res) {
			td->dpa_range.start = cxled->dpa_res->start;
			td->dpa_range.end = cxled->dpa_res->end;
		} else {
			td->dpa_range.start = 0;
			td->dpa_range.end = -1;
		}
	}
}

static void cxld_registry_save(struct cxl_test_decoder *td,
			       struct cxl_decoder *cxld)
{
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);

	dbg_cxld(port, "save", cxld);
	__cxld_registry_save(td, cxld);
}

static void cxld_registry_update(struct cxl_decoder *cxld)
{
	struct cxl_test_decoder *td = cxld_registry_find(cxld);
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);

	if (WARN_ON_ONCE(!td))
		return;

	dbg_cxld(port, "update", cxld);
	__cxld_registry_save(td, cxld);
}

static int mock_decoder_commit(struct cxl_decoder *cxld)
{
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
	int id = cxld->id;

	if (cxld->flags & CXL_DECODER_F_ENABLE)
		return 0;

	dev_dbg(&port->dev, "%s commit\n", dev_name(&cxld->dev));
	if (cxl_num_decoders_committed(port) != id) {
		dev_dbg(&port->dev,
			"%s: out of order commit, expected decoder%d.%d\n",
			dev_name(&cxld->dev), port->id,
			cxl_num_decoders_committed(port));
		return -EBUSY;
	}

	port->commit_end++;
	cxld->flags |= CXL_DECODER_F_ENABLE;
	if (is_endpoint_decoder(&cxld->dev)) {
		struct cxl_endpoint_decoder *cxled =
			to_cxl_endpoint_decoder(&cxld->dev);

		cxled->state = CXL_DECODER_STATE_AUTO;
	}
	cxld_registry_update(cxld);

	return 0;
}

static void mock_decoder_reset(struct cxl_decoder *cxld)
{
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
	int id = cxld->id;

	if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
		return;

	dev_dbg(&port->dev, "%s reset\n", dev_name(&cxld->dev));
	if (port->commit_end == id)
		cxl_port_commit_reap(cxld);
	else
		dev_dbg(&port->dev,
			"%s: out of order reset, expected decoder%d.%d\n",
			dev_name(&cxld->dev), port->id, port->commit_end);
	cxld->flags &= ~CXL_DECODER_F_ENABLE;

	if (is_endpoint_decoder(&cxld->dev)) {
		struct cxl_endpoint_decoder *cxled =
			to_cxl_endpoint_decoder(&cxld->dev);

		cxled->state = CXL_DECODER_STATE_MANUAL;
		cxled->skip = 0;
	}
	if (decoder_reset_preserve_registry)
		dev_dbg(port->uport_dev, "decoder%d: skip registry update\n",
			cxld->id);
	else
		cxld_registry_update(cxld);
}

static struct cxl_test_decoder *cxld_registry_new(struct cxl_decoder *cxld)
{
	struct cxl_test_decoder *td __free(kfree) =
		kzalloc(sizeof(*td), GFP_KERNEL);
	unsigned long key = cxld_registry_index(cxld);

	if (!td)
		return NULL;

	if (xa_insert(&decoder_registry, key, td, GFP_KERNEL)) {
		WARN_ON(1);
		return NULL;
	}

	cxld_registry_save(td, cxld);
	return no_free_ptr(td);
}

static void init_disabled_mock_decoder(struct cxl_decoder *cxld)
{
	cxld->hpa_range.start = 0;
	cxld->hpa_range.end = -1;
	cxld->interleave_ways = 1;
	cxld->interleave_granularity = 0;
	cxld->target_type = CXL_DECODER_HOSTONLYMEM;
	cxld->flags = 0;
	cxld->commit = mock_decoder_commit;
	cxld->reset = mock_decoder_reset;

	if (is_switch_decoder(&cxld->dev)) {
		struct cxl_switch_decoder *cxlsd =
			to_cxl_switch_decoder(&cxld->dev);

		for (int i = 0; i < cxlsd->nr_targets; i++) {
			cxlsd->target[i] = NULL;
			cxld->target_map[i] = 0;
		}
	} else {
		struct cxl_endpoint_decoder *cxled =
			to_cxl_endpoint_decoder(&cxld->dev);

		cxled->state = CXL_DECODER_STATE_MANUAL;
		cxled->skip = 0;
	}
}

static void default_mock_decoder(struct cxl_decoder *cxld)
{
	cxld->hpa_range = (struct range){
		.start = 0,
		.end = -1,
	};

	cxld->interleave_ways = 1;
	cxld->interleave_granularity = 256;
	cxld->target_type = CXL_DECODER_HOSTONLYMEM;
	cxld->commit = mock_decoder_commit;
	cxld->reset = mock_decoder_reset;

	WARN_ON_ONCE(!cxld_registry_new(cxld));
}

static int first_decoder(struct device *dev, const void *data)
{
	struct cxl_decoder *cxld;

	if (!is_switch_decoder(dev))
		return 0;
	cxld = to_cxl_decoder(dev);
	if (cxld->id == 0)
		return 1;
	return 0;
}

enum cxld_init_type {
	MOCK_DECODER_INIT_DEFAULT,
	MOCK_DECODER_INIT_SAVED,
	MOCK_DECODER_INIT_TYPE3_AUTO,
	MOCK_DECODER_INIT_TYPE2_AUTO,
};

static enum cxld_init_type get_decoder_init_type(struct cxl_decoder *cxld,
						 struct platform_device *pdev,
						 bool hb0,
						 struct cxl_test_decoder **td)
{
	struct cxl_test_decoder *found_td = cxld_registry_find(cxld);

	if (found_td) {
		*td = found_td;
		return MOCK_DECODER_INIT_SAVED;
	}

	*td = NULL;

	/*
	 * The first decoder on the first 2 devices on the first switch
	 * attached to host-bridge0 mock a fake / static RAM region. All
	 * other decoders are default disabled. Given the round robin
	 * assignment those devices are named cxl_mem.0, and cxl_mem.4.
	 *
	 * See 'cxl list -BMPu -m cxl_mem.0,cxl_mem.4'
	 */
	if (!is_endpoint_decoder(&cxld->dev) || !hb0 || pdev->id % 4 ||
	    pdev->id > 4 || cxld->id > 0)
		return MOCK_DECODER_INIT_DEFAULT;

	return type2_test ? MOCK_DECODER_INIT_TYPE2_AUTO :
			    MOCK_DECODER_INIT_TYPE3_AUTO;
}

static bool mock_decoder_handle_saved(struct cxl_decoder *cxld, struct cxl_test_decoder *td)
{
	bool enabled;

	if (is_switch_decoder(&cxld->dev))
		enabled = td->cxlsd.cxld.flags & CXL_DECODER_F_ENABLE;
	else
		enabled = td->cxled.cxld.flags & CXL_DECODER_F_ENABLE;

	if (enabled)
		return !cxld_registry_restore(cxld, td);

	init_disabled_mock_decoder(cxld);
	return false;
}

static void mock_init_hdm_type2_cxled(struct cxl_endpoint_decoder *cxled,
				      struct cxl_port *port)
{
	struct acpi_cedt_cfmws *window = mock_cfmws[0];
	struct cxl_decoder *cxld = &cxled->cxld;
	struct cxl_switch_decoder *cxlsd;
	struct cxl_dport *dport;
	struct cxl_port *root_port;
	struct device *dev;
	u64 base;

	base = window->base_hpa;
	cxld->hpa_range = (struct range) {
		.start = base,
		.end = base + mock_auto_region_size - 1,
	};

	cxld->interleave_ways = 1;
	eig_to_granularity(window->granularity, &cxld->interleave_granularity);
	cxld->target_type = CXL_DECODER_DEVMEM;
	cxld->flags = CXL_DECODER_F_ENABLE;
	cxled->state = CXL_DECODER_STATE_AUTO;
	port->commit_end = cxld->id;
	devm_cxl_dpa_reserve(cxled, 0,
			     mock_auto_region_size / cxld->interleave_ways, 0);
	cxld->commit = mock_decoder_commit;
	cxld->reset = mock_decoder_reset;

	WARN_ON_ONCE(!cxld_registry_new(cxld));
	/*
	 * Now that endpoint decoder is set up, walk up the hierarchy
	 * and setup the root port decoder targeting @cxlmd.
	 */
	dport = port->parent_dport;
	root_port = dport->port;
	dev = device_find_child(&root_port->dev, NULL, first_decoder);
	/*
	 * Ancestor ports are guaranteed to be enumerated before
	 * @port, and all ports have at least one decoder.
	 */
	if (WARN_ON(!dev))
		return;

	cxlsd = to_cxl_switch_decoder(dev);
	cxld = &cxlsd->cxld;
	cxld->target_type = CXL_DECODER_DEVMEM;
	cxld->flags = CXL_DECODER_F_ENABLE;
	root_port->commit_end = 0;
	cxld->interleave_ways = 1;
	cxld->interleave_granularity = 4096;
	cxld->target_map[0] = dport->port_id;
	cxld->hpa_range = (struct range) {
		.start = base,
		.end = base + mock_auto_region_size - 1,
	};
	cxld->commit = mock_decoder_commit;
	cxld->reset = mock_decoder_reset;

	/*
	 * Only target_map[] is programmed above, mimicking
	 * firmware. On real hardware target[] is populated as
	 * dports enumerate, via update_decoder_targets(). The
	 * mock's dports are already bound by now, so fire that
	 * resolution explicitly here rather than stamping
	 * target[] directly.
	 */
	cxl_port_update_decoder_targets(root_port, dport);

	cxld_registry_update(cxld);
	put_device(dev);
}

static void mock_init_hdm_type3_cxled(struct cxl_endpoint_decoder *cxled,
				      struct cxl_port *port,
				      struct platform_device *pdev,
				      bool hb0)
{
	struct acpi_cedt_cfmws *window = mock_cfmws[0];
	struct cxl_decoder *cxld = &cxled->cxld;
	struct cxl_switch_decoder *cxlsd;
	struct cxl_dport *dport;
	struct cxl_port *iter;
	struct device *dev;
	u64 base;
	int i;

	/* Simulate missing cxl_mem.4 configuration */
	if (hb0 && pdev->id == 4 && cxld->id == 0 && fail_autoassemble) {
		default_mock_decoder(cxld);
		return;
	}

	base = window->base_hpa;
	if (extended_linear_cache)
		base += mock_auto_region_size;
	cxld->hpa_range = (struct range) {
		.start = base,
		.end = base + mock_auto_region_size - 1,
	};

	cxld->interleave_ways = 2;
	eig_to_granularity(window->granularity, &cxld->interleave_granularity);
	cxld->target_type = CXL_DECODER_HOSTONLYMEM;
	cxld->flags = CXL_DECODER_F_ENABLE;
	cxled->state = CXL_DECODER_STATE_AUTO;
	port->commit_end = cxld->id;
	devm_cxl_dpa_reserve(cxled, 0,
			     mock_auto_region_size / cxld->interleave_ways, 0);
	cxld->commit = mock_decoder_commit;
	cxld->reset = mock_decoder_reset;

	WARN_ON_ONCE(!cxld_registry_new(cxld));
	/*
	 * Now that endpoint decoder is set up, walk up the hierarchy
	 * and setup the switch and root port decoders targeting @cxlmd.
	 */
	iter = port;
	for (i = 0; i < 2; i++) {
		dport = iter->parent_dport;
		iter = dport->port;
		dev = device_find_child(&iter->dev, NULL, first_decoder);
		/*
		 * Ancestor ports are guaranteed to be enumerated before
		 * @port, and all ports have at least one decoder.
		 */
		if (WARN_ON(!dev))
			continue;

		cxlsd = to_cxl_switch_decoder(dev);
		if (i == 0) {
			/* put cxl_mem.4 second in the decode order */
			if (pdev->id == 4)
				cxlsd->cxld.target_map[1] = dport->port_id;
			else
				cxlsd->cxld.target_map[0] = dport->port_id;
		} else {
			cxlsd->cxld.target_map[0] = dport->port_id;
		}
		cxld = &cxlsd->cxld;
		cxld->target_type = CXL_DECODER_HOSTONLYMEM;
		cxld->flags = CXL_DECODER_F_ENABLE;
		iter->commit_end = 0;
		/*
		 * Switch targets 2 endpoints, while host bridge targets
		 * one root port
		 */
		if (i == 0)
			cxld->interleave_ways = 2;
		else
			cxld->interleave_ways = 1;
		cxld->interleave_granularity = 4096;
		cxld->hpa_range = (struct range) {
			.start = base,
			.end = base + mock_auto_region_size - 1,
		};
		cxld->commit = mock_decoder_commit;
		cxld->reset = mock_decoder_reset;

		/*
		 * Only target_map[] is programmed above, mimicking
		 * firmware. On real hardware target[] is populated as
		 * dports enumerate, via update_decoder_targets(). The
		 * mock's dports are already bound by now, so fire that
		 * resolution explicitly here rather than stamping
		 * target[] directly.
		 */
		cxl_port_update_decoder_targets(iter, dport);

		cxld_registry_update(cxld);
		put_device(dev);
	}
}

/*
 * Initialize a decoder during HDM enumeration.
 *
 * If a saved registry entry exists:
 *   - enabled decoders are restored from the saved programming
 *   - disabled decoders are initialized in a clean disabled state
 *
 * If no registry entry exists the decoder follows the normal mock
 * initialization path, including the special auto-region setup for
 * the first endpoints under host-bridge0.
 *
 * Returns true if decoder state was restored from the registry. In
 * that case the saved decode configuration (including target mapping)
 * has already been applied and the map_targets() is skipped.
 */
static bool mock_init_hdm_decoder(struct cxl_decoder *cxld)
{
	struct cxl_endpoint_decoder *cxled = NULL;
	struct platform_device *pdev = NULL;
	struct cxl_test_decoder *td;
	struct cxl_memdev *cxlmd;
	struct cxl_port *port;
	bool hb0 = false;

	if (is_endpoint_decoder(&cxld->dev)) {
		cxled = to_cxl_endpoint_decoder(&cxld->dev);
		cxlmd = cxled_to_memdev(cxled);
		WARN_ON(!dev_is_platform(cxlmd->dev.parent));
		pdev = to_platform_device(cxlmd->dev.parent);

		/* check is endpoint is attach to host-bridge0 */
		port = cxled_to_port(cxled);
		do {
			if (port->uport_dev == &cxl_host_bridge[0]->dev) {
				hb0 = true;
				break;
			}
			if (is_cxl_port(port->dev.parent))
				port = to_cxl_port(port->dev.parent);
			else
				port = NULL;
		} while (port);
		port = cxled_to_port(cxled);
	} else {
		port = to_cxl_port(cxld->dev.parent);
	}

	switch (get_decoder_init_type(cxld, pdev, hb0, &td)) {
	case MOCK_DECODER_INIT_SAVED:
		if (WARN_ON(!td))
			return false;
		return mock_decoder_handle_saved(cxld, td);
	case MOCK_DECODER_INIT_DEFAULT:
		/*
		 * The default path picks up all the decoders that are not
		 * endpoint.
		 */
		default_mock_decoder(cxld);
		return false;
	case MOCK_DECODER_INIT_TYPE3_AUTO:
		mock_init_hdm_type3_cxled(cxled, port, pdev, hb0);
		return false;
	case MOCK_DECODER_INIT_TYPE2_AUTO:
		mock_init_hdm_type2_cxled(cxled, port);
		return false;
	default:
		return false;
	}
	return false;
}

static int mock_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
				       struct cxl_endpoint_dvsec_info *info)
{
	struct cxl_port *port = cxlhdm->port;
	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
	int target_count, i;
	bool restored;

	if (is_cxl_endpoint(port))
		target_count = 0;
	else if (is_cxl_root(parent_port))
		target_count = NR_CXL_ROOT_PORTS;
	else
		target_count = NR_CXL_SWITCH_PORTS;

	for (i = 0; i < NR_CXL_PORT_DECODERS; i++) {
		struct target_map_ctx ctx = {
			.target_count = target_count,
		};
		struct cxl_decoder *cxld;
		int rc;

		if (target_count) {
			struct cxl_switch_decoder *cxlsd;

			cxlsd = cxl_switch_decoder_alloc(port, target_count);
			if (IS_ERR(cxlsd)) {
				dev_warn(&port->dev,
					 "Failed to allocate the decoder\n");
				return PTR_ERR(cxlsd);
			}
			cxld = &cxlsd->cxld;
		} else {
			struct cxl_endpoint_decoder *cxled;

			cxled = cxl_endpoint_decoder_alloc(port);

			if (IS_ERR(cxled)) {
				dev_warn(&port->dev,
					 "Failed to allocate the decoder\n");
				return PTR_ERR(cxled);
			}
			cxld = &cxled->cxld;
		}

		ctx.target_map = cxld->target_map;
		restored = mock_init_hdm_decoder(cxld);
		if (target_count && !restored) {
			rc = device_for_each_child(port->uport_dev, &ctx,
						   map_targets);
			if (rc) {
				put_device(&cxld->dev);
				return rc;
			}
		}

		rc = cxl_decoder_add_locked(cxld);
		if (rc) {
			put_device(&cxld->dev);
			dev_err(&port->dev, "Failed to add decoder\n");
			return rc;
		}

		rc = cxl_decoder_autoremove(&port->dev, cxld);
		if (rc)
			return rc;
		dev_dbg(&cxld->dev, "Added to port %s\n", dev_name(&port->dev));
	}

	return 0;
}

static int __mock_cxl_decoders_setup(struct cxl_port *port)
{
	struct cxl_hdm *cxlhdm;

	cxlhdm = mock_cxl_setup_hdm(port, NULL);
	if (IS_ERR(cxlhdm)) {
		if (PTR_ERR(cxlhdm) != -ENODEV)
			dev_err(&port->dev, "Failed to map HDM decoder capability\n");
		return PTR_ERR(cxlhdm);
	}

	return mock_cxl_enumerate_decoders(cxlhdm, NULL);
}

static int mock_cxl_switch_port_decoders_setup(struct cxl_port *port)
{
	if (is_cxl_root(port) || is_cxl_endpoint(port))
		return -EOPNOTSUPP;

	return __mock_cxl_decoders_setup(port);
}

static int mock_cxl_endpoint_decoders_setup(struct cxl_port *port)
{
	if (!is_cxl_endpoint(port))
		return -EOPNOTSUPP;

	return __mock_cxl_decoders_setup(port);
}

static int get_port_array(struct cxl_port *port,
			  struct platform_device ***port_array,
			  int *port_array_size)
{
	struct platform_device **array;
	int array_size;

	if (port->depth == 1) {
		if (is_multi_bridge(port->uport_dev)) {
			array_size = ARRAY_SIZE(cxl_root_port);
			array = cxl_root_port;
		} else if (is_single_bridge(port->uport_dev)) {
			array_size = ARRAY_SIZE(cxl_root_single);
			array = cxl_root_single;
		} else {
			dev_dbg(&port->dev, "%s: unknown bridge type\n",
				dev_name(port->uport_dev));
			return -ENXIO;
		}
	} else if (port->depth == 2) {
		struct cxl_port *parent = to_cxl_port(port->dev.parent);

		if (is_multi_bridge(parent->uport_dev)) {
			array_size = ARRAY_SIZE(cxl_switch_dport);
			array = cxl_switch_dport;
		} else if (is_single_bridge(parent->uport_dev)) {
			array_size = ARRAY_SIZE(cxl_swd_single);
			array = cxl_swd_single;
		} else {
			dev_dbg(&port->dev, "%s: unknown bridge type\n",
				dev_name(port->uport_dev));
			return -ENXIO;
		}
	} else {
		dev_WARN_ONCE(&port->dev, 1, "unexpected depth %d\n",
			      port->depth);
		return -ENXIO;
	}

	*port_array = array;
	*port_array_size = array_size;

	return 0;
}

static struct cxl_dport *mock_cxl_add_dport_by_dev(struct cxl_port *port,
						   struct device *dport_dev)
{
	struct platform_device **array;
	int rc, i, array_size;

	rc = get_port_array(port, &array, &array_size);
	if (rc)
		return ERR_PTR(rc);

	for (i = 0; i < array_size; i++) {
		struct platform_device *pdev = array[i];

		if (pdev->dev.parent != port->uport_dev) {
			dev_dbg(&port->dev, "%s: mismatch parent %s\n",
				dev_name(port->uport_dev),
				dev_name(pdev->dev.parent));
			continue;
		}

		if (&pdev->dev != dport_dev)
			continue;

		return devm_cxl_add_dport(port, &pdev->dev, pdev->id,
					  CXL_RESOURCE_NONE);
	}

	return ERR_PTR(-ENODEV);
}

/*
 * Faking the cxl_dpa_perf for the memdev when appropriate.
 */
static void dpa_perf_setup(struct cxl_port *endpoint, struct range *range,
			   struct cxl_dpa_perf *dpa_perf)
{
	dpa_perf->qos_class = FAKE_QTG_ID;
	dpa_perf->dpa_range = *range;
	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
		dpa_perf->coord[i].read_latency = 500;
		dpa_perf->coord[i].write_latency = 500;
		dpa_perf->coord[i].read_bandwidth = 1000;
		dpa_perf->coord[i].write_bandwidth = 1000;
	}
}

static void mock_cxl_endpoint_parse_cdat(struct cxl_port *port)
{
	struct cxl_root *cxl_root __free(put_cxl_root) =
		find_cxl_root(port);
	struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
	struct cxl_dev_state *cxlds = cxlmd->cxlds;
	struct access_coordinate ep_c[ACCESS_COORDINATE_MAX];

	if (!cxl_root)
		return;

	for (int i = 0; i < cxlds->nr_partitions; i++) {
		struct resource *res = &cxlds->part[i].res;
		struct cxl_dpa_perf *perf = &cxlds->part[i].perf;
		struct range range = {
			.start = res->start,
			.end = res->end,
		};

		dpa_perf_setup(port, &range, perf);
	}

	cxl_memdev_update_perf(cxlmd);

	/*
	 * This function is here to only test the topology iterator. It serves
	 * no other purpose.
	 */
	cxl_endpoint_get_perf_coordinates(port, ep_c);
}

/*
 * Simulate that the first half of mock CXL Window 0 is "Soft Reserve" capacity
 */
static int mock_walk_hmem_resources(struct device *host, walk_hmem_fn fn)
{
	struct acpi_cedt_cfmws *cfmws = mock_cfmws[0];
	struct resource window =
		DEFINE_RES_MEM(cfmws->base_hpa, cfmws->window_size / 2);

	dev_dbg(host, "walk cxl_test resource: %pr\n", &window);
	return fn(host, 0, &window);
}

/*
 * This should only be called by the dax_hmem case, treat mismatches (negative
 * result) as "fallback to base region_intersects()". Simulate that the first
 * half of mock CXL Window 0 is IORES_DESC_CXL capacity.
 */
static int mock_region_intersects(resource_size_t start, size_t size,
				  unsigned long flags, unsigned long desc)
{
	struct resource res = DEFINE_RES_MEM(start, size);
	struct acpi_cedt_cfmws *cfmws = mock_cfmws[0];
	struct resource window =
		DEFINE_RES_MEM(cfmws->base_hpa, cfmws->window_size / 2);

	if (resource_overlaps(&res, &window))
		return REGION_INTERSECTS;
	pr_debug("warning: no cxl_test CXL intersection for %pr\n", &res);
	return -1;
}


static int
mock_region_intersects_soft_reserve(resource_size_t start, size_t size)
{
	struct resource res = DEFINE_RES_MEM(start, size);
	struct acpi_cedt_cfmws *cfmws = mock_cfmws[0];
	struct resource window =
		DEFINE_RES_MEM(cfmws->base_hpa, cfmws->window_size / 2);

	if (resource_overlaps(&res, &window))
		return REGION_INTERSECTS;
	pr_debug("warning: no cxl_test soft reserve intersection for %pr\n", &res);
	return -1;
}

static struct cxl_mock_ops cxl_mock_ops = {
	.is_mock_adev = is_mock_adev,
	.is_mock_bridge = is_mock_bridge,
	.is_mock_bus = is_mock_bus,
	.is_mock_port = is_mock_port,
	.is_mock_dev = is_mock_dev,
	.acpi_table_parse_cedt = mock_acpi_table_parse_cedt,
	.acpi_evaluate_integer = mock_acpi_evaluate_integer,
	.acpi_pci_find_root = mock_acpi_pci_find_root,
	.devm_cxl_switch_port_decoders_setup = mock_cxl_switch_port_decoders_setup,
	.devm_cxl_endpoint_decoders_setup = mock_cxl_endpoint_decoders_setup,
	.cxl_endpoint_parse_cdat = mock_cxl_endpoint_parse_cdat,
	.devm_cxl_add_dport_by_dev = mock_cxl_add_dport_by_dev,
	.hmat_get_extended_linear_cache_size =
		mock_hmat_get_extended_linear_cache_size,
	.walk_hmem_resources = mock_walk_hmem_resources,
	.region_intersects = mock_region_intersects,
	.region_intersects_soft_reserve = mock_region_intersects_soft_reserve,
	.list = LIST_HEAD_INIT(cxl_mock_ops.list),
};

static void mock_companion(struct acpi_device *adev, struct device *dev)
{
	device_initialize(&adev->dev);
	fwnode_init(&adev->fwnode, NULL);
	dev->fwnode = &adev->fwnode;
	adev->fwnode.dev = dev;
}

#ifndef SZ_64G
#define SZ_64G (SZ_32G * 2)
#endif

static int cxl_mock_platform_device_add(struct platform_device *pdev,
					struct platform_device **ppdev)
{
	int rc;

	if (ppdev)
		*ppdev = pdev;
	rc = platform_device_add(pdev);
	if (rc) {
		platform_device_put(pdev);
		if (ppdev)
			*ppdev = NULL;
	}

	return rc;
}

static __init int cxl_rch_topo_init(void)
{
	int rc, i;

	for (i = 0; i < ARRAY_SIZE(cxl_rch); i++) {
		int idx = NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + i;
		struct acpi_device *adev = &host_bridge[idx];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_host_bridge", idx);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_bridge;
		}

		mock_companion(adev, &pdev->dev);
		rc = cxl_mock_platform_device_add(pdev, &cxl_rch[i]);
		if (rc)
			goto err_bridge;

		mock_pci_bus[idx].bridge = &pdev->dev;
		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
				       "firmware_node");
		if (rc)
			goto err_bridge;
	}

	return 0;

err_bridge:
	for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_rch[i];

		if (!pdev)
			continue;
		sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
		platform_device_unregister(cxl_rch[i]);
	}

	return rc;
}

static void cxl_rch_topo_exit(void)
{
	int i;

	for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_rch[i];

		if (!pdev)
			continue;
		sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
		platform_device_unregister(cxl_rch[i]);
	}
}

static __init int cxl_single_topo_init(void)
{
	int i, rc;

	for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++) {
		struct acpi_device *adev =
			&host_bridge[NR_CXL_HOST_BRIDGES + i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_host_bridge",
					     NR_CXL_HOST_BRIDGES + i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_bridge;
		}

		mock_companion(adev, &pdev->dev);
		rc = cxl_mock_platform_device_add(pdev, &cxl_hb_single[i]);
		if (rc)
			goto err_bridge;

		mock_pci_bus[i + NR_CXL_HOST_BRIDGES].bridge = &pdev->dev;
		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
				       "physical_node");
		if (rc)
			goto err_bridge;
	}

	for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++) {
		struct platform_device *bridge =
			cxl_hb_single[i % ARRAY_SIZE(cxl_hb_single)];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_root_port",
					     NR_MULTI_ROOT + i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_port;
		}
		pdev->dev.parent = &bridge->dev;

		rc = cxl_mock_platform_device_add(pdev, &cxl_root_single[i]);
		if (rc)
			goto err_port;
	}

	for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++) {
		struct platform_device *root_port = cxl_root_single[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_switch_uport",
					     NR_MULTI_ROOT + i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_uport;
		}
		pdev->dev.parent = &root_port->dev;

		rc = cxl_mock_platform_device_add(pdev, &cxl_swu_single[i]);
		if (rc)
			goto err_uport;
	}

	for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++) {
		struct platform_device *uport =
			cxl_swu_single[i % ARRAY_SIZE(cxl_swu_single)];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_switch_dport",
					     i + NR_MEM_MULTI);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_dport;
		}
		pdev->dev.parent = &uport->dev;

		rc = cxl_mock_platform_device_add(pdev, &cxl_swd_single[i]);
		if (rc)
			goto err_dport;
	}

	return 0;

err_dport:
	for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_swd_single[i]);
err_uport:
	for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_swu_single[i]);
err_port:
	for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_root_single[i]);
err_bridge:
	for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_hb_single[i];

		if (!pdev)
			continue;
		sysfs_remove_link(&pdev->dev.kobj, "physical_node");
		platform_device_unregister(cxl_hb_single[i]);
	}

	return rc;
}

static void cxl_single_topo_exit(void)
{
	int i;

	for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_swd_single[i]);
	for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_swu_single[i]);
	for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_root_single[i]);
	for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_hb_single[i];

		if (!pdev)
			continue;
		sysfs_remove_link(&pdev->dev.kobj, "physical_node");
		platform_device_unregister(cxl_hb_single[i]);
	}
}

static void cxl_type3_mem_exit(void)
{
	struct platform_device *pdev;
	int i;

	for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--) {
		pdev = cxl_rcd[i];
		if (!pdev)
			continue;
		platform_device_unregister(cxl_rcd[i]);
	}

	for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--) {
		pdev = cxl_mem_single[i];
		if (!pdev)
			continue;
		platform_device_unregister(cxl_mem_single[i]);
	}

	for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--) {
		pdev = cxl_mem[i];
		if (!pdev)
			continue;
		platform_device_unregister(pdev);
	}
}

static void cxl_type2_mem_exit(void)
{
	for (int i = NR_CXL_TYPE2_ACCEL - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_mem[i];

		if (!pdev)
			continue;
		platform_device_unregister(pdev);
	}
}

static void cxl_mem_exit(void)
{
	if (type2_test) {
		cxl_type2_mem_exit();
		return;
	}

	cxl_type3_mem_exit();
}

static int cxl_type2_mem_init(void)
{
	int i, rc;

	for (i = 0; i < NR_CXL_TYPE2_ACCEL; i++) {
		struct platform_device *dport = cxl_root_port[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_type2_accel", i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_mem;
		}
		pdev->dev.parent = &dport->dev;
		set_dev_node(&pdev->dev, i % 2);

		rc = cxl_mock_platform_device_add(pdev, &cxl_mem[i]);
		if (rc)
			goto err_mem;
	}

	return 0;

err_mem:
	for (i = NR_CXL_TYPE2_ACCEL - 1; i >= 0; i--)
		platform_device_unregister(cxl_mem[i]);
	return rc;
}

static int cxl_type3_mem_init(void)
{
	int i, rc;

	for (i = 0; i < ARRAY_SIZE(cxl_mem); i++) {
		struct platform_device *dport = cxl_switch_dport[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_mem", i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_mem;
		}
		pdev->dev.parent = &dport->dev;
		set_dev_node(&pdev->dev, i % 2);

		rc = cxl_mock_platform_device_add(pdev, &cxl_mem[i]);
		if (rc)
			goto err_mem;
	}

	for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++) {
		struct platform_device *dport = cxl_swd_single[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_mem", NR_MEM_MULTI + i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_single;
		}
		pdev->dev.parent = &dport->dev;
		set_dev_node(&pdev->dev, i % 2);

		rc = cxl_mock_platform_device_add(pdev, &cxl_mem_single[i]);
		if (rc)
			goto err_single;
	}

	for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++) {
		int idx = NR_MEM_MULTI + NR_MEM_SINGLE + i;
		struct platform_device *rch = cxl_rch[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_rcd", idx);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_rcd;
		}
		pdev->dev.parent = &rch->dev;
		set_dev_node(&pdev->dev, i % 2);

		rc = cxl_mock_platform_device_add(pdev, &cxl_rcd[i]);
		if (rc)
			goto err_rcd;
	}

	return 0;

err_rcd:
	for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
		platform_device_unregister(cxl_rcd[i]);
err_single:
	for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
		platform_device_unregister(cxl_mem_single[i]);
err_mem:
	for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
		platform_device_unregister(cxl_mem[i]);
	return rc;
}

static int cxl_mem_init(void)
{
	if (type2_test)
		return cxl_type2_mem_init();
	return cxl_type3_mem_init();
}

static ssize_t
decoder_reset_preserve_registry_show(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
	return sysfs_emit(buf, "%d\n", decoder_reset_preserve_registry);
}

static ssize_t
decoder_reset_preserve_registry_store(struct device *dev,
				      struct device_attribute *attr,
				      const char *buf, size_t count)
{
	int rc;

	rc = kstrtobool(buf, &decoder_reset_preserve_registry);
	if (rc)
		return rc;
	return count;
}

static DEVICE_ATTR_RW(decoder_reset_preserve_registry);

static struct attribute *cxl_acpi_attrs[] = {
	&dev_attr_decoder_reset_preserve_registry.attr, NULL
};
ATTRIBUTE_GROUPS(cxl_acpi);

static bool __init have_multiple_modparms(void)
{
	int count = 0;

	if (interleave_arithmetic)
		count++;
	if (extended_linear_cache)
		count++;
	if (hmem_test)
		count++;
	if (type2_test)
		count++;

	return count > 1;
}

static void host_bridges_remove(void)
{
	int i;

	for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_host_bridge[i];

		if (!pdev)
			continue;

		sysfs_remove_link(&pdev->dev.kobj, "physical_node");
		platform_device_unregister(cxl_host_bridge[i]);
	}
}

static int host_bridges_populate(void)
{
	int rc = 0;

	for (int i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++) {
		struct acpi_device *adev = &host_bridge[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_host_bridge", i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_bridge;
		}

		mock_companion(adev, &pdev->dev);
		rc = cxl_mock_platform_device_add(pdev, &cxl_host_bridge[i]);
		if (rc)
			goto err_bridge;

		mock_pci_bus[i].bridge = &pdev->dev;
		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
				       "physical_node");
		if (rc)
			goto err_bridge;
	}

	return 0;

err_bridge:
	host_bridges_remove();
	return rc;
}

static void cxl_rootports_remove(void)
{
	for (int i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_root_port[i];

		if (!pdev)
			continue;

		platform_device_unregister(pdev);
	}
}

static int cxl_rootports_populate(void)
{
	int rc = 0;

	for (int i = 0; i < ARRAY_SIZE(cxl_root_port); i++) {
		struct platform_device *bridge =
			cxl_host_bridge[i % ARRAY_SIZE(cxl_host_bridge)];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_root_port", i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_port;
		}

		pdev->dev.parent = &bridge->dev;

		rc = cxl_mock_platform_device_add(pdev, &cxl_root_port[i]);
		if (rc)
			goto err_port;
	}

	return 0;

err_port:
	cxl_rootports_remove();
	return rc;
}

static void cxl_usps_remove(void)
{
	for (int i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_switch_uport[i];

		if (!pdev)
			continue;

		platform_device_unregister(cxl_switch_uport[i]);
	}
}

static int cxl_usps_populate(void)
{
	int rc = 0;

	for (int i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++) {
		struct platform_device *root_port = cxl_root_port[i];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_switch_uport", i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_uport;
		}

		pdev->dev.parent = &root_port->dev;

		rc = cxl_mock_platform_device_add(pdev, &cxl_switch_uport[i]);
		if (rc)
			goto err_uport;
	}

	return 0;

err_uport:
	cxl_usps_remove();
	return rc;
}

static void cxl_dsps_remove(void)
{
	for (int i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--) {
		struct platform_device *pdev = cxl_switch_dport[i];

		if (!pdev)
			continue;

		platform_device_unregister(cxl_switch_dport[i]);
	}
}


static int cxl_dsps_populate(void)
{
	int rc = 0;

	for (int i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++) {
		struct platform_device *uport =
			cxl_switch_uport[i % ARRAY_SIZE(cxl_switch_uport)];
		struct platform_device *pdev;

		pdev = platform_device_alloc("cxl_switch_dport", i);
		if (!pdev) {
			rc = -ENOMEM;
			goto err_dport;
		}
		pdev->dev.parent = &uport->dev;

		rc = cxl_mock_platform_device_add(pdev, &cxl_switch_dport[i]);
		if (rc)
			goto err_dport;
	}

	return 0;

err_dport:
	cxl_dsps_remove();
	return rc;
}

static void cxl_switches_remove(void)
{
	cxl_dsps_remove();
	cxl_usps_remove();
}

static int cxl_switches_populate(void)
{
	int rc;

	BUILD_BUG_ON(ARRAY_SIZE(cxl_switch_uport) != ARRAY_SIZE(cxl_root_port));
	rc = cxl_usps_populate();
	if (rc)
		return rc;

	rc = cxl_dsps_populate();
	if (rc) {
		cxl_usps_remove();
		return rc;
	}

	return 0;
}

static void cxl_type2_topo_exit(void)
{
	cxl_rootports_remove();
	host_bridges_remove();
}

static int cxl_type2_topo_init(void)
{
	int rc;

	rc = host_bridges_populate();
	if (rc)
		return rc;

	rc = cxl_rootports_populate();
	if (rc) {
		host_bridges_remove();
		return rc;
	}

	return 0;
}

static void cxl_type3_topo_exit(void)
{
	cxl_rch_topo_exit();
	cxl_single_topo_exit();
	cxl_switches_remove();
	cxl_rootports_remove();
	host_bridges_remove();
}

static int cxl_type3_topo_init(void)
{
	int rc;

	rc = host_bridges_populate();
	if (rc)
		return rc;

	rc = cxl_rootports_populate();
	if (rc)
		goto err_host_bridges;

	rc = cxl_switches_populate();
	if (rc)
		goto err_root_ports;

	rc = cxl_single_topo_init();
	if (rc)
		goto err_switches;

	rc = cxl_rch_topo_init();
	if (rc)
		goto err_single;

	return 0;

err_single:
	cxl_single_topo_exit();
err_switches:
	cxl_switches_remove();
err_root_ports:
	cxl_rootports_remove();
err_host_bridges:
	host_bridges_remove();
	return rc;
}

static void cxl_topo_exit(void)
{
	if (type2_test) {
		cxl_type2_topo_exit();
		return;
	}

	cxl_type3_topo_exit();
}

static int cxl_topo_init(void)
{
	if (type2_test)
		return cxl_type2_topo_init();
	return cxl_type3_topo_init();
}

static __init int cxl_test_init(void)
{
	struct range mappable;
	int rc;

	/* Enforce a single module param active at a time */
	if (have_multiple_modparms())
		return -EINVAL;

	if (!IS_ALIGNED(mock_auto_region_size, PMD_SIZE)) {
		pr_err_once("mock_auto_region_size %d must be PMD-aligned\n",
			    mock_auto_region_size);
		return -EINVAL;
	}

	cxl_acpi_test();
	cxl_core_test();
	cxl_mem_test();
	cxl_pmem_test();
	cxl_port_test();

	register_cxl_mock_ops(&cxl_mock_ops);

	cxl_mock_pool = gen_pool_create(ilog2(SZ_2M), NUMA_NO_NODE);
	if (!cxl_mock_pool) {
		rc = -ENOMEM;
		goto err_gen_pool_create;
	}
	mappable = mhp_get_pluggable_range(true);

	rc = gen_pool_add(cxl_mock_pool,
			  min(iomem_resource.end + 1 - SZ_64G,
			      mappable.end + 1 - SZ_64G),
			  SZ_64G, NUMA_NO_NODE);
	if (rc)
		goto err_gen_pool_add;

	if (interleave_arithmetic == 1) {
		cfmws_start = CFMWS_XOR_ARRAY_START;
		cfmws_end = CFMWS_XOR_ARRAY_END;
	} else {
		cfmws_start = CFMWS_MOD_ARRAY_START;
		cfmws_end = CFMWS_MOD_ARRAY_END;
	}

	rc = populate_cedt();
	if (rc)
		goto err_populate;

	rc = cxl_topo_init();
	if (rc)
		goto err_populate;

	cxl_acpi = platform_device_alloc("cxl_acpi", 0);
	if (!cxl_acpi) {
		rc = -ENOMEM;
		goto err_topo;
	}

	mock_companion(&acpi0017_mock, &cxl_acpi->dev);
	acpi0017_mock.dev.bus = &platform_bus_type;
	cxl_acpi->dev.groups = cxl_acpi_groups;

	rc = cxl_mock_platform_device_add(cxl_acpi, NULL);
	if (rc)
		goto err_topo;

	rc = cxl_mem_init();
	if (rc)
		goto err_root;

	rc = hmem_test_init();
	if (rc)
		goto err_mem;

	return 0;

err_mem:
	cxl_mem_exit();
err_root:
	platform_device_unregister(cxl_acpi);
err_topo:
	cxl_topo_exit();
err_populate:
	depopulate_all_mock_resources();
err_gen_pool_add:
	gen_pool_destroy(cxl_mock_pool);
err_gen_pool_create:
	unregister_cxl_mock_ops(&cxl_mock_ops);
	return rc;
}

static void free_decoder_registry(void)
{
	unsigned long index;
	void *entry;

	xa_for_each(&decoder_registry, index, entry) {
		xa_erase(&decoder_registry, index);
		kfree(entry);
	}
}

static __exit void cxl_test_exit(void)
{
	hmem_test_exit();
	cxl_mem_exit();
	platform_device_unregister(cxl_acpi);
	cxl_topo_exit();
	depopulate_all_mock_resources();
	gen_pool_destroy(cxl_mock_pool);
	unregister_cxl_mock_ops(&cxl_mock_ops);
	free_decoder_registry();
	xa_destroy(&decoder_registry);
}

module_param(interleave_arithmetic, int, 0444);
MODULE_PARM_DESC(interleave_arithmetic, "Modulo:0, XOR:1");
module_param(extended_linear_cache, bool, 0444);
MODULE_PARM_DESC(extended_linear_cache, "Enable extended linear cache support");
module_param(fail_autoassemble, bool, 0444);
MODULE_PARM_DESC(fail_autoassemble, "Simulate missing member of an auto-region");
module_param(type2_test, bool, 0444);
MODULE_PARM_DESC(type2_test, "Enable type 2 support testing");
module_init(cxl_test_init);
module_exit(cxl_test_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("cxl_test: setup module");
MODULE_IMPORT_NS("ACPI");
MODULE_IMPORT_NS("CXL");