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
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright(c) 2022 Intel Corporation. All rights reserved. */
#include <linux/memregion.h>
#include <linux/genalloc.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/memory.h>
#include <linux/slab.h>
#include <linux/uuid.h>
#include <linux/sort.h>
#include <linux/idr.h>
#include <linux/memory-tiers.h>
#include <linux/string_choices.h>
#include <cxlmem.h>
#include <cxl.h>
#include "core.h"
#include "mce.h"

/**
 * DOC: cxl core region
 *
 * CXL Regions represent mapped memory capacity in system physical address
 * space. Whereas the CXL Root Decoders identify the bounds of potential CXL
 * Memory ranges, Regions represent the active mapped capacity by the HDM
 * Decoder Capability structures throughout the Host Bridges, Switches, and
 * Endpoints in the topology.
 *
 * Region configuration has ordering constraints. UUID may be set at any time
 * but is only visible for persistent regions.
 * 1. Interleave granularity
 * 2. Interleave size
 * 3. Decoder targets
 */

/*
 * nodemask that sets per node when the access_coordinates for the node has
 * been updated by the CXL memory hotplug notifier.
 */
static nodemask_t nodemask_region_seen = NODE_MASK_NONE;

static struct cxl_region *to_cxl_region(struct device *dev);

#define __ACCESS_ATTR_RO(_level, _name) {				\
	.attr	= { .name = __stringify(_name), .mode = 0444 },		\
	.show	= _name##_access##_level##_show,			\
}

#define ACCESS_DEVICE_ATTR_RO(level, name)	\
	struct device_attribute dev_attr_access##level##_##name = __ACCESS_ATTR_RO(level, name)

#define ACCESS_ATTR_RO(level, attrib)					      \
static ssize_t attrib##_access##level##_show(struct device *dev,	      \
					  struct device_attribute *attr,      \
					  char *buf)			      \
{									      \
	struct cxl_region *cxlr = to_cxl_region(dev);			      \
									      \
	if (cxlr->coord[level].attrib == 0)				      \
		return -ENOENT;						      \
									      \
	return sysfs_emit(buf, "%u\n", cxlr->coord[level].attrib);	      \
}									      \
static ACCESS_DEVICE_ATTR_RO(level, attrib)

ACCESS_ATTR_RO(0, read_bandwidth);
ACCESS_ATTR_RO(0, read_latency);
ACCESS_ATTR_RO(0, write_bandwidth);
ACCESS_ATTR_RO(0, write_latency);

#define ACCESS_ATTR_DECLARE(level, attrib)	\
	(&dev_attr_access##level##_##attrib.attr)

static struct attribute *access0_coordinate_attrs[] = {
	ACCESS_ATTR_DECLARE(0, read_bandwidth),
	ACCESS_ATTR_DECLARE(0, write_bandwidth),
	ACCESS_ATTR_DECLARE(0, read_latency),
	ACCESS_ATTR_DECLARE(0, write_latency),
	NULL
};

ACCESS_ATTR_RO(1, read_bandwidth);
ACCESS_ATTR_RO(1, read_latency);
ACCESS_ATTR_RO(1, write_bandwidth);
ACCESS_ATTR_RO(1, write_latency);

static struct attribute *access1_coordinate_attrs[] = {
	ACCESS_ATTR_DECLARE(1, read_bandwidth),
	ACCESS_ATTR_DECLARE(1, write_bandwidth),
	ACCESS_ATTR_DECLARE(1, read_latency),
	ACCESS_ATTR_DECLARE(1, write_latency),
	NULL
};

#define ACCESS_VISIBLE(level)						\
static umode_t cxl_region_access##level##_coordinate_visible(		\
		struct kobject *kobj, struct attribute *a, int n)	\
{									\
	struct device *dev = kobj_to_dev(kobj);				\
	struct cxl_region *cxlr = to_cxl_region(dev);			\
									\
	if (a == &dev_attr_access##level##_read_latency.attr &&		\
	    cxlr->coord[level].read_latency == 0)			\
		return 0;						\
									\
	if (a == &dev_attr_access##level##_write_latency.attr &&	\
	    cxlr->coord[level].write_latency == 0)			\
		return 0;						\
									\
	if (a == &dev_attr_access##level##_read_bandwidth.attr &&	\
	    cxlr->coord[level].read_bandwidth == 0)			\
		return 0;						\
									\
	if (a == &dev_attr_access##level##_write_bandwidth.attr &&	\
	    cxlr->coord[level].write_bandwidth == 0)			\
		return 0;						\
									\
	return a->mode;							\
}

ACCESS_VISIBLE(0);
ACCESS_VISIBLE(1);

static const struct attribute_group cxl_region_access0_coordinate_group = {
	.name = "access0",
	.attrs = access0_coordinate_attrs,
	.is_visible = cxl_region_access0_coordinate_visible,
};

static const struct attribute_group *get_cxl_region_access0_group(void)
{
	return &cxl_region_access0_coordinate_group;
}

static const struct attribute_group cxl_region_access1_coordinate_group = {
	.name = "access1",
	.attrs = access1_coordinate_attrs,
	.is_visible = cxl_region_access1_coordinate_visible,
};

static const struct attribute_group *get_cxl_region_access1_group(void)
{
	return &cxl_region_access1_coordinate_group;
}

static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	ssize_t rc;

	ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &region_rwsem)))
		return rc;
	if (cxlr->mode != CXL_PARTMODE_PMEM)
		return sysfs_emit(buf, "\n");
	return sysfs_emit(buf, "%pUb\n", &p->uuid);
}

static int is_dup(struct device *match, void *data)
{
	struct cxl_region_params *p;
	struct cxl_region *cxlr;
	uuid_t *uuid = data;

	if (!is_cxl_region(match))
		return 0;

	lockdep_assert_held(&cxl_rwsem.region);
	cxlr = to_cxl_region(match);
	p = &cxlr->params;

	if (uuid_equal(&p->uuid, uuid)) {
		dev_dbg(match, "already has uuid: %pUb\n", uuid);
		return -EBUSY;
	}

	return 0;
}

static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t len)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	uuid_t temp;
	ssize_t rc;

	if (len != UUID_STRING_LEN + 1)
		return -EINVAL;

	rc = uuid_parse(buf, &temp);
	if (rc)
		return rc;

	if (uuid_is_null(&temp))
		return -EINVAL;

	ACQUIRE(rwsem_write_kill, region_rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_write_kill, &region_rwsem)))
		return rc;

	if (uuid_equal(&p->uuid, &temp))
		return len;

	if (p->state >= CXL_CONFIG_ACTIVE)
		return -EBUSY;

	rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
	if (rc < 0)
		return rc;

	uuid_copy(&p->uuid, &temp);

	return len;
}
static DEVICE_ATTR_RW(uuid);

static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
					  struct cxl_region *cxlr)
{
	return xa_load(&port->regions, (unsigned long)cxlr);
}

static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
{
	if (!cpu_cache_has_invalidate_memregion()) {
		if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
			dev_info_once(
				&cxlr->dev,
				"Bypassing cpu_cache_invalidate_memregion() for testing!\n");
			return 0;
		}
		dev_WARN(&cxlr->dev,
			"Failed to synchronize CPU cache state\n");
		return -ENXIO;
	}

	if (!cxlr->params.res)
		return -ENXIO;
	cpu_cache_invalidate_memregion(cxlr->params.res->start,
				       resource_size(cxlr->params.res));
	return 0;
}

static void cxl_region_decode_reset(struct cxl_region *cxlr, int count)
{
	struct cxl_region_params *p = &cxlr->params;
	int i;

	if (test_bit(CXL_REGION_F_LOCK, &cxlr->flags))
		return;

	/*
	 * Before region teardown attempt to flush, evict any data cached for
	 * this region, or scream loudly about missing arch / platform support
	 * for CXL teardown.
	 */
	cxl_region_invalidate_memregion(cxlr);

	for (i = count - 1; i >= 0; i--) {
		struct cxl_endpoint_decoder *cxled = p->targets[i];
		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
		struct cxl_port *iter = cxled_to_port(cxled);
		struct cxl_dev_state *cxlds = cxlmd->cxlds;
		struct cxl_ep *ep;

		if (cxlds->rcd)
			goto endpoint_reset;

		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
			iter = to_cxl_port(iter->dev.parent);

		for (ep = cxl_ep_load(iter, cxlmd); iter;
		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
			struct cxl_region_ref *cxl_rr;
			struct cxl_decoder *cxld;

			cxl_rr = cxl_rr_load(iter, cxlr);
			cxld = cxl_rr->decoder;
			if (cxld->reset)
				cxld->reset(cxld);
			set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
		}

endpoint_reset:
		cxled->cxld.reset(&cxled->cxld);
		set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
	}

	/* all decoders associated with this region have been torn down */
	clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
}

static int commit_decoder(struct cxl_decoder *cxld)
{
	struct cxl_switch_decoder *cxlsd = NULL;

	if (cxld->commit)
		return cxld->commit(cxld);

	if (is_switch_decoder(&cxld->dev))
		cxlsd = to_cxl_switch_decoder(&cxld->dev);

	if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
			  "->commit() is required\n"))
		return -ENXIO;
	return 0;
}

static int cxl_region_decode_commit(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	int i, rc = 0;

	for (i = 0; i < p->nr_targets; i++) {
		struct cxl_endpoint_decoder *cxled = p->targets[i];
		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
		struct cxl_region_ref *cxl_rr;
		struct cxl_decoder *cxld;
		struct cxl_port *iter;
		struct cxl_ep *ep;

		/* commit bottom up */
		for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
		     iter = to_cxl_port(iter->dev.parent)) {
			cxl_rr = cxl_rr_load(iter, cxlr);
			cxld = cxl_rr->decoder;
			rc = commit_decoder(cxld);
			if (rc)
				break;
		}

		if (rc) {
			/* programming @iter failed, teardown */
			for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
			     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
				cxl_rr = cxl_rr_load(iter, cxlr);
				cxld = cxl_rr->decoder;
				if (cxld->reset)
					cxld->reset(cxld);
			}

			cxled->cxld.reset(&cxled->cxld);
			goto err;
		}
	}

	return 0;

err:
	/* undo the targets that were successfully committed */
	cxl_region_decode_reset(cxlr, i);
	return rc;
}

static int queue_reset(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	int rc;

	ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
		return rc;

	/* Already in the requested state? */
	if (p->state < CXL_CONFIG_COMMIT)
		return 0;

	p->state = CXL_CONFIG_RESET_PENDING;

	return 0;
}

static int __commit(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	int rc;

	ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
		return rc;

	/* Already in the requested state? */
	if (p->state >= CXL_CONFIG_COMMIT)
		return 0;

	/* Not ready to commit? */
	if (p->state < CXL_CONFIG_ACTIVE)
		return -ENXIO;

	/*
	 * Invalidate caches before region setup to drop any speculative
	 * consumption of this address space
	 */
	rc = cxl_region_invalidate_memregion(cxlr);
	if (rc)
		return rc;

	rc = cxl_region_decode_commit(cxlr);
	if (rc)
		return rc;

	p->state = CXL_CONFIG_COMMIT;

	return 0;
}

static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t len)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	bool commit;
	ssize_t rc;

	rc = kstrtobool(buf, &commit);
	if (rc)
		return rc;

	if (commit) {
		rc = __commit(cxlr);
		if (rc)
			return rc;
		return len;
	}

	if (test_bit(CXL_REGION_F_LOCK, &cxlr->flags))
		return -EPERM;

	rc = queue_reset(cxlr);
	if (rc)
		return rc;

	/*
	 * Unmap the region and depend the reset-pending state to ensure
	 * it does not go active again until post reset
	 */
	device_release_driver(&cxlr->dev);

	/*
	 * With the reset pending take cxl_rwsem.region unconditionally
	 * to ensure the reset gets handled before returning.
	 */
	guard(rwsem_write)(&cxl_rwsem.region);

	/*
	 * Revalidate that the reset is still pending in case another
	 * thread already handled this reset.
	 */
	if (p->state == CXL_CONFIG_RESET_PENDING) {
		cxl_region_decode_reset(cxlr, p->interleave_ways);
		p->state = CXL_CONFIG_ACTIVE;
	}

	return len;
}

static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	ssize_t rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;
	return sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
}
static DEVICE_ATTR_RW(commit);

static ssize_t interleave_ways_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	int rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;
	return sysfs_emit(buf, "%d\n", p->interleave_ways);
}

static const struct attribute_group *get_cxl_region_target_group(void);

static int set_interleave_ways(struct cxl_region *cxlr, int val)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
	struct cxl_region_params *p = &cxlr->params;
	int save, rc;
	u8 iw;

	rc = ways_to_eiw(val, &iw);
	if (rc)
		return rc;

	/*
	 * Even for x3, x6, and x12 interleaves the region interleave must be a
	 * power of 2 multiple of the host bridge interleave.
	 */
	if (!is_power_of_2(val / cxld->interleave_ways) ||
	    (val % cxld->interleave_ways)) {
		dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
		return -EINVAL;
	}

	lockdep_assert_held_write(&cxl_rwsem.region);

	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
		return -EBUSY;

	save = p->interleave_ways;
	p->interleave_ways = val;
	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
	if (rc)
		p->interleave_ways = save;

	return rc;
}

static ssize_t interleave_ways_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t len)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	int val;
	int rc;

	rc = kstrtoint(buf, 0, &val);
	if (rc)
		return rc;

	ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
		return rc;

	rc = set_interleave_ways(cxlr, val);
	if (rc)
		return rc;

	return len;
}
static DEVICE_ATTR_RW(interleave_ways);

static ssize_t interleave_granularity_show(struct device *dev,
					   struct device_attribute *attr,
					   char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	int rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;
	return sysfs_emit(buf, "%d\n", p->interleave_granularity);
}

static int set_interleave_granularity(struct cxl_region *cxlr, int val)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
	struct cxl_region_params *p = &cxlr->params;
	int rc;
	u16 ig;

	rc = granularity_to_eig(val, &ig);
	if (rc)
		return rc;

	/*
	 * When the host-bridge is interleaved, disallow region granularity !=
	 * root granularity. Regions with a granularity less than the root
	 * interleave result in needing multiple endpoints to support a single
	 * slot in the interleave (possible to support in the future). Regions
	 * with a granularity greater than the root interleave result in invalid
	 * DPA translations (invalid to support).
	 */
	if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
		return -EINVAL;

	lockdep_assert_held_write(&cxl_rwsem.region);

	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
		return -EBUSY;

	p->interleave_granularity = val;
	return 0;
}

static ssize_t interleave_granularity_store(struct device *dev,
					    struct device_attribute *attr,
					    const char *buf, size_t len)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	int rc, val;

	rc = kstrtoint(buf, 0, &val);
	if (rc)
		return rc;

	ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
		return rc;

	rc = set_interleave_granularity(cxlr, val);
	if (rc)
		return rc;

	return len;
}
static DEVICE_ATTR_RW(interleave_granularity);

static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	u64 resource = -1ULL;
	int rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;

	if (p->res)
		resource = p->res->start;
	return sysfs_emit(buf, "%#llx\n", resource);
}
static DEVICE_ATTR_RO(resource);

static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	const char *desc;

	if (cxlr->mode == CXL_PARTMODE_RAM)
		desc = "ram";
	else if (cxlr->mode == CXL_PARTMODE_PMEM)
		desc = "pmem";
	else
		desc = "";

	return sysfs_emit(buf, "%s\n", desc);
}
static DEVICE_ATTR_RO(mode);

static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_region_params *p = &cxlr->params;
	struct resource *res;
	u64 remainder = 0;

	lockdep_assert_held_write(&cxl_rwsem.region);

	/* Nothing to do... */
	if (p->res && resource_size(p->res) == size)
		return 0;

	/* To change size the old size must be freed first */
	if (p->res)
		return -EBUSY;

	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
		return -EBUSY;

	/* ways, granularity and uuid (if PMEM) need to be set before HPA */
	if (!p->interleave_ways || !p->interleave_granularity ||
	    (cxlr->mode == CXL_PARTMODE_PMEM && uuid_is_null(&p->uuid)))
		return -ENXIO;

	div64_u64_rem(size, (u64)SZ_256M * p->interleave_ways, &remainder);
	if (remainder)
		return -EINVAL;

	res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
				    dev_name(&cxlr->dev));
	if (IS_ERR(res)) {
		dev_dbg(&cxlr->dev,
			"HPA allocation error (%pe) for size:%pap in %s %pr\n",
			res, &size, cxlrd->res->name, cxlrd->res);
		return PTR_ERR(res);
	}

	cxlr->hpa_range = DEFINE_RANGE(res->start, res->end);

	p->res = res;
	p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;

	return 0;
}

static void cxl_region_iomem_release(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;

	if (device_is_registered(&cxlr->dev))
		lockdep_assert_held_write(&cxl_rwsem.region);
	if (p->res) {
		/*
		 * Autodiscovered regions may not have been able to insert their
		 * resource.
		 */
		if (p->res->parent)
			remove_resource(p->res);
		kfree(p->res);
		p->res = NULL;
	}
}

static int free_hpa(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;

	lockdep_assert_held_write(&cxl_rwsem.region);

	if (!p->res)
		return 0;

	if (p->state >= CXL_CONFIG_ACTIVE)
		return -EBUSY;

	cxlr->hpa_range = DEFINE_RANGE(0, -1);

	cxl_region_iomem_release(cxlr);
	p->state = CXL_CONFIG_IDLE;
	return 0;
}

static ssize_t size_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t len)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	u64 val;
	int rc;

	rc = kstrtou64(buf, 0, &val);
	if (rc)
		return rc;

	ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
		return rc;

	if (val)
		rc = alloc_hpa(cxlr, val);
	else
		rc = free_hpa(cxlr);

	if (rc)
		return rc;

	return len;
}

static ssize_t size_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	u64 size = 0;
	ssize_t rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;
	if (p->res)
		size = resource_size(p->res);
	return sysfs_emit(buf, "%#llx\n", size);
}
static DEVICE_ATTR_RW(size);

static ssize_t extended_linear_cache_size_show(struct device *dev,
					       struct device_attribute *attr,
					       char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	ssize_t rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;
	return sysfs_emit(buf, "%pap\n", &p->cache_size);
}
static DEVICE_ATTR_RO(extended_linear_cache_size);

static ssize_t locked_show(struct device *dev,
			   struct device_attribute *attr,
			   char *buf)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	int rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;

	rc = test_bit(CXL_REGION_F_LOCK, &cxlr->flags);
	return sysfs_emit(buf, "%d\n", rc);
}
static DEVICE_ATTR_RO(locked);

static struct attribute *cxl_region_attrs[] = {
	&dev_attr_uuid.attr,
	&dev_attr_commit.attr,
	&dev_attr_interleave_ways.attr,
	&dev_attr_interleave_granularity.attr,
	&dev_attr_resource.attr,
	&dev_attr_size.attr,
	&dev_attr_mode.attr,
	&dev_attr_extended_linear_cache_size.attr,
	&dev_attr_locked.attr,
	NULL,
};

static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
				  int n)
{
	struct device *dev = kobj_to_dev(kobj);
	struct cxl_region *cxlr = to_cxl_region(dev);

	/*
	 * Support tooling that expects to find a 'uuid' attribute for all
	 * regions regardless of mode.
	 */
	if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_PARTMODE_PMEM)
		return 0444;

	/*
	 * Don't display extended linear cache attribute if there is no
	 * extended linear cache.
	 */
	if (a == &dev_attr_extended_linear_cache_size.attr &&
	    cxlr->params.cache_size == 0)
		return 0;

	return a->mode;
}

static const struct attribute_group cxl_region_group = {
	.attrs = cxl_region_attrs,
	.is_visible = cxl_region_visible,
};

static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
{
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_endpoint_decoder *cxled;
	int rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;

	if (pos >= p->interleave_ways) {
		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
			p->interleave_ways);
		return -ENXIO;
	}

	cxled = p->targets[pos];
	if (!cxled)
		return sysfs_emit(buf, "\n");
	return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
}

static int check_commit_order(struct device *dev, void *data)
{
	struct cxl_decoder *cxld = to_cxl_decoder(dev);

	/*
	 * if port->commit_end is not the only free decoder, then out of
	 * order shutdown has occurred, block further allocations until
	 * that is resolved
	 */
	if (((cxld->flags & CXL_DECODER_F_ENABLE) == 0))
		return -EBUSY;
	return 0;
}

static int match_free_decoder(struct device *dev, const void *data)
{
	struct cxl_port *port = to_cxl_port(dev->parent);
	struct cxl_decoder *cxld;
	int rc;

	if (!is_switch_decoder(dev))
		return 0;

	cxld = to_cxl_decoder(dev);

	if (cxld->id != port->commit_end + 1)
		return 0;

	if (cxld->region) {
		dev_dbg(dev->parent,
			"next decoder to commit (%s) is already reserved (%s)\n",
			dev_name(dev), dev_name(&cxld->region->dev));
		return 0;
	}

	rc = device_for_each_child_reverse_from(dev->parent, dev, NULL,
						check_commit_order);
	if (rc) {
		dev_dbg(dev->parent,
			"unable to allocate %s due to out of order shutdown\n",
			dev_name(dev));
		return 0;
	}
	return 1;
}

static bool spa_maps_hpa(const struct cxl_region_params *p,
			 const struct range *range)
{
	if (!p->res)
		return false;

	/*
	 * The extended linear cache region is constructed by a 1:1 ratio
	 * where the SPA maps equal amounts of DRAM and CXL HPA capacity with
	 * CXL decoders at the high end of the SPA range.
	 */
	return p->res->start + p->cache_size == range->start &&
		p->res->end == range->end;
}

static int match_auto_decoder(struct device *dev, const void *data)
{
	const struct cxl_region_params *p = data;
	struct cxl_decoder *cxld;
	struct range *r;

	if (!is_switch_decoder(dev))
		return 0;

	cxld = to_cxl_decoder(dev);
	r = &cxld->hpa_range;

	if (spa_maps_hpa(p, r))
		return 1;

	return 0;
}

/**
 * cxl_port_pick_region_decoder() - assign or lookup a decoder for a region
 * @port: a port in the ancestry of the endpoint implied by @cxled
 * @cxled: endpoint decoder to be, or currently, mapped by @port
 * @cxlr: region to establish, or validate, decode @port
 *
 * In the region creation path cxl_port_pick_region_decoder() is an
 * allocator to find a free port. In the region assembly path, it is
 * recalling the decoder that platform firmware picked for validation
 * purposes.
 *
 * The result is recorded in a 'struct cxl_region_ref' in @port.
 */
static struct cxl_decoder *
cxl_port_pick_region_decoder(struct cxl_port *port,
			     struct cxl_endpoint_decoder *cxled,
			     struct cxl_region *cxlr)
{
	struct device *dev;

	if (port == cxled_to_port(cxled))
		return &cxled->cxld;

	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
		dev = device_find_child(&port->dev, &cxlr->params,
					match_auto_decoder);
	else
		dev = device_find_child(&port->dev, NULL, match_free_decoder);
	if (!dev)
		return NULL;
	/*
	 * This decoder is pinned registered as long as the endpoint decoder is
	 * registered, and endpoint decoder unregistration holds the
	 * cxl_rwsem.region over unregister events, so no need to hold on to
	 * this extra reference.
	 */
	put_device(dev);
	return to_cxl_decoder(dev);
}

static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
			  struct cxl_decoder *cxld)
{
	struct cxl_region_ref *rr = cxl_rr_load(port, cxlr_iter);
	struct cxl_decoder *cxld_iter = rr->decoder;

	/*
	 * Allow the out of order assembly of auto-discovered regions.
	 * Per CXL Spec 3.1 8.2.4.20.12 software must commit decoders
	 * in HPA order. Confirm that the decoder with the lesser HPA
	 * starting address has the lesser id.
	 */
	dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n",
		dev_name(&cxld->dev), cxld->id,
		dev_name(&cxld_iter->dev), cxld_iter->id);

	if (cxld_iter->id > cxld->id)
		return true;

	return false;
}

static struct cxl_region_ref *
alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
		 struct cxl_endpoint_decoder *cxled,
		 struct cxl_decoder *cxld)
{
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_region_ref *cxl_rr, *iter;
	unsigned long index;
	int rc;

	xa_for_each(&port->regions, index, iter) {
		struct cxl_region_params *ip = &iter->region->params;

		if (!ip->res || ip->res->start < p->res->start)
			continue;

		if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
			if (auto_order_ok(port, iter->region, cxld))
				continue;
		}
		dev_dbg(&cxlr->dev, "%s: HPA order violation %s:%pr vs %pr\n",
			dev_name(&port->dev),
			dev_name(&iter->region->dev), ip->res, p->res);

		return ERR_PTR(-EBUSY);
	}

	cxl_rr = kzalloc_obj(*cxl_rr);
	if (!cxl_rr)
		return ERR_PTR(-ENOMEM);
	cxl_rr->port = port;
	cxl_rr->region = cxlr;
	cxl_rr->nr_targets = 1;
	xa_init(&cxl_rr->endpoints);

	rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
	if (rc) {
		dev_dbg(&cxlr->dev,
			"%s: failed to track region reference: %d\n",
			dev_name(&port->dev), rc);
		kfree(cxl_rr);
		return ERR_PTR(rc);
	}

	return cxl_rr;
}

static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
{
	struct cxl_region *cxlr = cxl_rr->region;
	struct cxl_decoder *cxld = cxl_rr->decoder;

	if (!cxld)
		return;

	dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
	if (cxld->region == cxlr) {
		cxld->region = NULL;
		put_device(&cxlr->dev);
	}
}

static void free_region_ref(struct cxl_region_ref *cxl_rr)
{
	struct cxl_port *port = cxl_rr->port;
	struct cxl_region *cxlr = cxl_rr->region;

	cxl_rr_free_decoder(cxl_rr);
	xa_erase(&port->regions, (unsigned long)cxlr);
	xa_destroy(&cxl_rr->endpoints);
	kfree(cxl_rr);
}

static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
			 struct cxl_endpoint_decoder *cxled)
{
	int rc;
	struct cxl_port *port = cxl_rr->port;
	struct cxl_region *cxlr = cxl_rr->region;
	struct cxl_decoder *cxld = cxl_rr->decoder;
	struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));

	if (ep) {
		rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
			       GFP_KERNEL);
		if (rc)
			return rc;
	}
	cxl_rr->nr_eps++;

	if (!cxld->region) {
		cxld->region = cxlr;

		/*
		 * Now that cxld->region is set the intermediate staging state
		 * can be cleared.
		 */
		if (cxld == &cxled->cxld &&
		    cxled->state == CXL_DECODER_STATE_AUTO_STAGED)
			cxled->state = CXL_DECODER_STATE_AUTO;
		get_device(&cxlr->dev);
	}

	return 0;
}

static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr,
				 struct cxl_endpoint_decoder *cxled,
				 struct cxl_region_ref *cxl_rr,
				 struct cxl_decoder *cxld)
{
	if (cxld->region) {
		dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
			dev_name(&port->dev), dev_name(&cxld->dev),
			dev_name(&cxld->region->dev));
		return -EBUSY;
	}

	/*
	 * Endpoints should already match the region type, but backstop that
	 * assumption with an assertion. Switch-decoders change mapping-type
	 * based on what is mapped when they are assigned to a region.
	 */
	dev_WARN_ONCE(&cxlr->dev,
		      port == cxled_to_port(cxled) &&
			      cxld->target_type != cxlr->type,
		      "%s:%s mismatch decoder type %d -> %d\n",
		      dev_name(&cxled_to_memdev(cxled)->dev),
		      dev_name(&cxld->dev), cxld->target_type, cxlr->type);
	cxld->target_type = cxlr->type;
	cxl_rr->decoder = cxld;
	return 0;
}

static void cxl_region_setup_flags(struct cxl_region *cxlr,
				   struct cxl_decoder *cxld)
{
	if (is_endpoint_decoder(&cxld->dev)) {
		struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(&cxld->dev);
		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);

		/*
		 * When a region's memdevs specify an @attach method the attach
		 * provider is responsible for dispositioning the region for
		 * both probe and userspace management
		 */
		if (cxlmd->attach)
			set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
	}

	if (cxld->flags & CXL_DECODER_F_LOCK) {
		set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
		clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
	}

	if (cxld->flags & CXL_DECODER_F_NORMALIZED_ADDRESSING)
		set_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags);
}

/**
 * cxl_port_attach_region() - track a region's interest in a port by endpoint
 * @port: port to add a new region reference 'struct cxl_region_ref'
 * @cxlr: region to attach to @port
 * @cxled: endpoint decoder used to create or further pin a region reference
 * @pos: interleave position of @cxled in @cxlr
 *
 * The attach event is an opportunity to validate CXL decode setup
 * constraints and record metadata needed for programming HDM decoders,
 * in particular decoder target lists.
 *
 * The steps are:
 *
 * - validate that there are no other regions with a higher HPA already
 *   associated with @port
 * - establish a region reference if one is not already present
 *
 *   - additionally allocate a decoder instance that will host @cxlr on
 *     @port
 *
 * - pin the region reference by the endpoint
 * - account for how many entries in @port's target list are needed to
 *   cover all of the added endpoints.
 */
static int cxl_port_attach_region(struct cxl_port *port,
				  struct cxl_region *cxlr,
				  struct cxl_endpoint_decoder *cxled, int pos)
{
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
	struct cxl_region_ref *cxl_rr;
	bool nr_targets_inc = false;
	struct cxl_decoder *cxld;
	unsigned long index;
	int rc = -EBUSY;

	lockdep_assert_held_write(&cxl_rwsem.region);

	cxl_rr = cxl_rr_load(port, cxlr);
	if (cxl_rr) {
		struct cxl_ep *ep_iter;
		int found = 0;

		/*
		 * Walk the existing endpoints that have been attached to
		 * @cxlr at @port and see if they share the same 'next' port
		 * in the downstream direction. I.e. endpoints that share common
		 * upstream switch.
		 */
		xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
			if (ep_iter == ep)
				continue;
			if (ep_iter->next == ep->next) {
				found++;
				break;
			}
		}

		/*
		 * New target port, or @port is an endpoint port that always
		 * accounts its own local decode as a target.
		 */
		if (!found || !ep->next) {
			cxl_rr->nr_targets++;
			nr_targets_inc = true;
		}
	} else {
		cxld = cxl_port_pick_region_decoder(port, cxled, cxlr);
		if (!cxld) {
			dev_dbg(&cxlr->dev, "%s: no decoder available\n",
				dev_name(&port->dev));
			return -EBUSY;
		}

		cxl_rr = alloc_region_ref(port, cxlr, cxled, cxld);
		if (IS_ERR(cxl_rr)) {
			dev_dbg(&cxlr->dev,
				"%s: failed to allocate region reference\n",
				dev_name(&port->dev));
			return PTR_ERR(cxl_rr);
		}
		nr_targets_inc = true;

		rc = cxl_rr_assign_decoder(port, cxlr, cxled, cxl_rr, cxld);
		if (rc)
			goto out_erase;
	}
	cxld = cxl_rr->decoder;

	/*
	 * the number of targets should not exceed the target_count
	 * of the decoder
	 */
	if (is_switch_decoder(&cxld->dev)) {
		struct cxl_switch_decoder *cxlsd;

		cxlsd = to_cxl_switch_decoder(&cxld->dev);
		if (cxl_rr->nr_targets > cxlsd->nr_targets) {
			dev_dbg(&cxlr->dev,
				"%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
				dev_name(port->uport_dev), dev_name(&port->dev),
				dev_name(&cxld->dev), dev_name(&cxlmd->dev),
				dev_name(&cxled->cxld.dev), pos,
				cxlsd->nr_targets);
			rc = -ENXIO;
			goto out_erase;
		}
	}

	cxl_region_setup_flags(cxlr, cxld);

	rc = cxl_rr_ep_add(cxl_rr, cxled);
	if (rc) {
		dev_dbg(&cxlr->dev,
			"%s: failed to track endpoint %s:%s reference\n",
			dev_name(&port->dev), dev_name(&cxlmd->dev),
			dev_name(&cxld->dev));
		goto out_erase;
	}

	dev_dbg(&cxlr->dev,
		"%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
		dev_name(port->uport_dev), dev_name(&port->dev),
		dev_name(&cxld->dev), dev_name(&cxlmd->dev),
		dev_name(&cxled->cxld.dev), pos,
		ep ? ep->next ? dev_name(ep->next->uport_dev) :
				      dev_name(&cxlmd->dev) :
			   "none",
		cxl_rr->nr_eps, cxl_rr->nr_targets);

	return 0;
out_erase:
	if (nr_targets_inc)
		cxl_rr->nr_targets--;
	if (cxl_rr->nr_eps == 0)
		free_region_ref(cxl_rr);
	return rc;
}

static void cxl_port_detach_region(struct cxl_port *port,
				   struct cxl_region *cxlr,
				   struct cxl_endpoint_decoder *cxled)
{
	struct cxl_region_ref *cxl_rr;
	struct cxl_ep *ep = NULL;

	lockdep_assert_held_write(&cxl_rwsem.region);

	cxl_rr = cxl_rr_load(port, cxlr);
	if (!cxl_rr)
		return;

	/*
	 * Endpoint ports do not carry cxl_ep references, and they
	 * never target more than one endpoint by definition
	 */
	if (cxl_rr->decoder == &cxled->cxld)
		cxl_rr->nr_eps--;
	else
		ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
	if (ep) {
		struct cxl_ep *ep_iter;
		unsigned long index;
		int found = 0;

		cxl_rr->nr_eps--;
		xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
			if (ep_iter->next == ep->next) {
				found++;
				break;
			}
		}
		if (!found)
			cxl_rr->nr_targets--;
	}

	if (cxl_rr->nr_eps == 0)
		free_region_ref(cxl_rr);
}

static int check_last_peer(struct cxl_endpoint_decoder *cxled,
			   struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
			   int distance)
{
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_region *cxlr = cxl_rr->region;
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_endpoint_decoder *cxled_peer;
	struct cxl_port *port = cxl_rr->port;
	struct cxl_memdev *cxlmd_peer;
	struct cxl_ep *ep_peer;
	int pos = cxled->pos;

	/*
	 * If this position wants to share a dport with the last endpoint mapped
	 * then that endpoint, at index 'position - distance', must also be
	 * mapped by this dport.
	 */
	if (pos < distance) {
		dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
			dev_name(port->uport_dev), dev_name(&port->dev),
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
		return -ENXIO;
	}
	cxled_peer = p->targets[pos - distance];
	cxlmd_peer = cxled_to_memdev(cxled_peer);
	ep_peer = cxl_ep_load(port, cxlmd_peer);
	if (ep->dport != ep_peer->dport) {
		dev_dbg(&cxlr->dev,
			"%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
			dev_name(port->uport_dev), dev_name(&port->dev),
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
			dev_name(&cxlmd_peer->dev),
			dev_name(&cxled_peer->cxld.dev));
		return -ENXIO;
	}

	return 0;
}

static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
{
	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
	unsigned int interleave_mask;
	u8 eiw;
	u16 eig;
	int high_pos, low_pos;

	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
		return -ENXIO;
	/*
	 * Per CXL specification r3.1(8.2.4.20.13 Decoder Protection),
	 * if eiw < 8:
	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
	 *
	 *   when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
	 *   interleave bits are none.
	 *
	 * if eiw >= 8:
	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
	 *
	 *   when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
	 *   interleave bits are none.
	 */
	ways_to_eiw(iw, &eiw);
	if (eiw == 0 || eiw == 8)
		return 0;

	granularity_to_eig(ig, &eig);
	if (eiw > 8)
		high_pos = eiw + eig - 1;
	else
		high_pos = eiw + eig + 7;
	low_pos = eig + 8;
	interleave_mask = GENMASK(high_pos, low_pos);
	if (interleave_mask & ~cxlhdm->interleave_mask)
		return -ENXIO;

	return 0;
}

static int cxl_port_setup_targets(struct cxl_port *port,
				  struct cxl_region *cxlr,
				  struct cxl_endpoint_decoder *cxled)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	int parent_iw, parent_ig, ig, iw, rc, pos = cxled->pos;
	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_decoder *cxld = cxl_rr->decoder;
	struct cxl_switch_decoder *cxlsd;
	struct cxl_port *iter = port;
	u16 eig, peig;
	u8 eiw, peiw;

	/*
	 * While root level decoders support x3, x6, x12, switch level
	 * decoders only support powers of 2 up to x16.
	 */
	if (!is_power_of_2(cxl_rr->nr_targets)) {
		dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
			dev_name(port->uport_dev), dev_name(&port->dev),
			cxl_rr->nr_targets);
		return -EINVAL;
	}

	cxlsd = to_cxl_switch_decoder(&cxld->dev);
	if (cxl_rr->nr_targets_set) {
		int i, distance = 1;
		struct cxl_region_ref *cxl_rr_iter;

		/*
		 * The "distance" between peer downstream ports represents which
		 * endpoint positions in the region interleave a given port can
		 * host.
		 *
		 * For example, at the root of a hierarchy the distance is
		 * always 1 as every index targets a different host-bridge. At
		 * each subsequent switch level those ports map every Nth region
		 * position where N is the width of the switch == distance.
		 */
		do {
			cxl_rr_iter = cxl_rr_load(iter, cxlr);
			distance *= cxl_rr_iter->nr_targets;
			iter = to_cxl_port(iter->dev.parent);
		} while (!is_cxl_root(iter));
		distance *= cxlrd->cxlsd.cxld.interleave_ways;

		for (i = 0; i < cxl_rr->nr_targets_set; i++)
			if (ep->dport == cxlsd->target[i]) {
				rc = check_last_peer(cxled, ep, cxl_rr,
						     distance);
				if (rc)
					return rc;
				goto out_target_set;
			}
		goto add_target;
	}

	if (is_cxl_root(parent_port)) {
		/*
		 * Root decoder IG is always set to value in CFMWS which
		 * may be different than this region's IG.  We can use the
		 * region's IG here since interleave_granularity_store()
		 * does not allow interleaved host-bridges with
		 * root IG != region IG.
		 */
		parent_ig = p->interleave_granularity;
		parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
		/*
		 * For purposes of address bit routing, use power-of-2 math for
		 * switch ports.
		 */
		if (!is_power_of_2(parent_iw))
			parent_iw /= 3;
	} else {
		struct cxl_region_ref *parent_rr;
		struct cxl_decoder *parent_cxld;

		parent_rr = cxl_rr_load(parent_port, cxlr);
		parent_cxld = parent_rr->decoder;
		parent_ig = parent_cxld->interleave_granularity;
		parent_iw = parent_cxld->interleave_ways;
	}

	rc = granularity_to_eig(parent_ig, &peig);
	if (rc) {
		dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
			dev_name(parent_port->uport_dev),
			dev_name(&parent_port->dev), parent_ig);
		return rc;
	}

	rc = ways_to_eiw(parent_iw, &peiw);
	if (rc) {
		dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
			dev_name(parent_port->uport_dev),
			dev_name(&parent_port->dev), parent_iw);
		return rc;
	}

	iw = cxl_rr->nr_targets;
	rc = ways_to_eiw(iw, &eiw);
	if (rc) {
		dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
			dev_name(port->uport_dev), dev_name(&port->dev), iw);
		return rc;
	}

	/*
	 * Interleave granularity is a multiple of @parent_port granularity.
	 * Multiplier is the parent port interleave ways.
	 */
	rc = granularity_to_eig(parent_ig * parent_iw, &eig);
	if (rc) {
		dev_dbg(&cxlr->dev,
			"%s: invalid granularity calculation (%d * %d)\n",
			dev_name(&parent_port->dev), parent_ig, parent_iw);
		return rc;
	}

	rc = eig_to_granularity(eig, &ig);
	if (rc) {
		dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
			dev_name(port->uport_dev), dev_name(&port->dev),
			256 << eig);
		return rc;
	}

	if (iw > 8 || iw > cxlsd->nr_targets) {
		dev_dbg(&cxlr->dev,
			"%s:%s:%s: ways: %d overflows targets: %d\n",
			dev_name(port->uport_dev), dev_name(&port->dev),
			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
		return -ENXIO;
	}

	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
		if (cxld->interleave_ways != iw ||
		    (iw > 1 && cxld->interleave_granularity != ig) ||
		    !spa_maps_hpa(p, &cxld->hpa_range) ||
		    ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
			dev_err(&cxlr->dev,
				"%s:%s %s expected iw: %d ig: %d %pr\n",
				dev_name(port->uport_dev), dev_name(&port->dev),
				__func__, iw, ig, p->res);
			dev_err(&cxlr->dev,
				"%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
				dev_name(port->uport_dev), dev_name(&port->dev),
				__func__, cxld->interleave_ways,
				cxld->interleave_granularity,
				str_enabled_disabled(cxld->flags & CXL_DECODER_F_ENABLE),
				cxld->hpa_range.start, cxld->hpa_range.end);
			return -ENXIO;
		}
	} else {
		rc = check_interleave_cap(cxld, iw, ig);
		if (rc) {
			dev_dbg(&cxlr->dev,
				"%s:%s iw: %d ig: %d is not supported\n",
				dev_name(port->uport_dev),
				dev_name(&port->dev), iw, ig);
			return rc;
		}

		cxld->interleave_ways = iw;
		cxld->interleave_granularity = ig;
		cxld->hpa_range = (struct range) {
			.start = p->res->start,
			.end = p->res->end,
		};
	}
	dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport_dev),
		dev_name(&port->dev), iw, ig);
add_target:
	if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
		dev_dbg(&cxlr->dev,
			"%s:%s: targets full trying to add %s:%s at %d\n",
			dev_name(port->uport_dev), dev_name(&port->dev),
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
		return -ENXIO;
	}
	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
		if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) {
			dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n",
				dev_name(port->uport_dev), dev_name(&port->dev),
				dev_name(&cxlsd->cxld.dev),
				dev_name(ep->dport->dport_dev),
				cxl_rr->nr_targets_set);
			return -ENXIO;
		}
	} else {
		cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
		cxlsd->cxld.target_map[cxl_rr->nr_targets_set] = ep->dport->port_id;
	}
	cxl_rr->nr_targets_set++;
out_target_set:
	dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
		dev_name(port->uport_dev), dev_name(&port->dev),
		cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport_dev),
		dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);

	return 0;
}

static void cxl_port_reset_targets(struct cxl_port *port,
				   struct cxl_region *cxlr)
{
	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
	struct cxl_decoder *cxld;

	/*
	 * After the last endpoint has been detached the entire cxl_rr may now
	 * be gone.
	 */
	if (!cxl_rr)
		return;
	cxl_rr->nr_targets_set = 0;

	cxld = cxl_rr->decoder;
	cxld->hpa_range = (struct range) {
		.start = 0,
		.end = -1,
	};
}

static void cxl_region_teardown_targets(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_endpoint_decoder *cxled;
	struct cxl_dev_state *cxlds;
	struct cxl_memdev *cxlmd;
	struct cxl_port *iter;
	struct cxl_ep *ep;
	int i;

	/*
	 * In the auto-discovery case skip automatic teardown since the
	 * address space is already active
	 */
	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
		return;

	for (i = 0; i < p->nr_targets; i++) {
		cxled = p->targets[i];
		cxlmd = cxled_to_memdev(cxled);
		cxlds = cxlmd->cxlds;

		if (cxlds->rcd)
			continue;

		iter = cxled_to_port(cxled);
		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
			iter = to_cxl_port(iter->dev.parent);

		for (ep = cxl_ep_load(iter, cxlmd); iter;
		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
			cxl_port_reset_targets(iter, cxlr);
	}
}

static int cxl_region_setup_targets(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_endpoint_decoder *cxled;
	struct cxl_dev_state *cxlds;
	int i, rc, rch = 0, vh = 0;
	struct cxl_memdev *cxlmd;
	struct cxl_port *iter;
	struct cxl_ep *ep;

	for (i = 0; i < p->nr_targets; i++) {
		cxled = p->targets[i];
		cxlmd = cxled_to_memdev(cxled);
		cxlds = cxlmd->cxlds;

		/* validate that all targets agree on topology */
		if (!cxlds->rcd) {
			vh++;
		} else {
			rch++;
			continue;
		}

		iter = cxled_to_port(cxled);
		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
			iter = to_cxl_port(iter->dev.parent);

		/*
		 * Descend the topology tree programming / validating
		 * targets while looking for conflicts.
		 */
		for (ep = cxl_ep_load(iter, cxlmd); iter;
		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
			rc = cxl_port_setup_targets(iter, cxlr, cxled);
			if (rc) {
				cxl_region_teardown_targets(cxlr);
				return rc;
			}
		}
	}

	if (rch && vh) {
		dev_err(&cxlr->dev, "mismatched CXL topologies detected\n");
		cxl_region_teardown_targets(cxlr);
		return -ENXIO;
	}

	return 0;
}

static int cxl_region_validate_position(struct cxl_region *cxlr,
					struct cxl_endpoint_decoder *cxled,
					int pos)
{
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_region_params *p = &cxlr->params;
	int i;

	if (pos < 0 || pos >= p->interleave_ways) {
		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
			p->interleave_ways);
		return -ENXIO;
	}

	if (p->targets[pos] == cxled)
		return 0;

	if (p->targets[pos]) {
		struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
		struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);

		dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
			pos, dev_name(&cxlmd_target->dev),
			dev_name(&cxled_target->cxld.dev));
		return -EBUSY;
	}

	for (i = 0; i < p->interleave_ways; i++) {
		struct cxl_endpoint_decoder *cxled_target;
		struct cxl_memdev *cxlmd_target;

		cxled_target = p->targets[i];
		if (!cxled_target)
			continue;

		cxlmd_target = cxled_to_memdev(cxled_target);
		if (cxlmd_target == cxlmd) {
			dev_dbg(&cxlr->dev,
				"%s already specified at position %d via: %s\n",
				dev_name(&cxlmd->dev), pos,
				dev_name(&cxled_target->cxld.dev));
			return -EBUSY;
		}
	}

	return 0;
}

static int cxl_region_attach_position(struct cxl_region *cxlr,
				      struct cxl_endpoint_decoder *cxled,
				      const struct cxl_dport *dport, int pos)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
	struct cxl_decoder *cxld = &cxlsd->cxld;
	int iw = cxld->interleave_ways;
	struct cxl_port *iter;
	int rc;

	if (dport != cxlrd->cxlsd.target[pos % iw]) {
		dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			dev_name(&cxlrd->cxlsd.cxld.dev));
		return -ENXIO;
	}

	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
	     iter = to_cxl_port(iter->dev.parent)) {
		rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
		if (rc)
			goto err;
	}

	return 0;

err:
	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
	     iter = to_cxl_port(iter->dev.parent))
		cxl_port_detach_region(iter, cxlr, cxled);
	return rc;
}

static int cxl_region_attach_auto(struct cxl_region *cxlr,
				  struct cxl_endpoint_decoder *cxled, int pos)
{
	struct cxl_region_params *p = &cxlr->params;

	if (cxled->state != CXL_DECODER_STATE_AUTO) {
		dev_err(&cxlr->dev,
			"%s: unable to add decoder to autodetected region\n",
			dev_name(&cxled->cxld.dev));
		return -EINVAL;
	}

	if (pos >= 0) {
		dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
			dev_name(&cxled->cxld.dev), pos);
		return -EINVAL;
	}

	if (p->nr_targets >= p->interleave_ways) {
		dev_err(&cxlr->dev, "%s: no more target slots available\n",
			dev_name(&cxled->cxld.dev));
		return -ENXIO;
	}

	/*
	 * Temporarily record the endpoint decoder into the target array. Yes,
	 * this means that userspace can view devices in the wrong position
	 * before the region activates, and must be careful to understand when
	 * it might be racing region autodiscovery.
	 *
	 * The endpoint decoder will be recorded into the first free slot of
	 * the target array.
	 */
	for (pos = 0; pos < p->interleave_ways; pos++) {
		if (!p->targets[pos])
			break;
	}

	if (pos == p->interleave_ways) {
		dev_err(&cxlr->dev, "%s: unable to find a free target slot\n",
			dev_name(&cxled->cxld.dev));
		return -ENXIO;
	}

	p->targets[pos] = cxled;
	cxled->pos = pos;
	cxled->state = CXL_DECODER_STATE_AUTO_STAGED;
	p->nr_targets++;

	return 0;
}

static int cmp_interleave_pos(const void *a, const void *b)
{
	struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
	struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;

	return cxled_a->pos - cxled_b->pos;
}

static int match_switch_decoder_by_range(struct device *dev,
					 const void *data)
{
	struct cxl_switch_decoder *cxlsd;
	const struct range *r1, *r2 = data;


	if (!is_switch_decoder(dev))
		return 0;

	cxlsd = to_cxl_switch_decoder(dev);
	r1 = &cxlsd->cxld.hpa_range;

	if (is_root_decoder(dev))
		return range_contains(r1, r2);
	return (r1->start == r2->start && r1->end == r2->end);
}

static int find_pos_and_ways(struct cxl_port *port, struct range *range,
			     int *pos, int *ways)
{
	struct cxl_switch_decoder *cxlsd;
	struct cxl_port *parent;
	int rc = -ENXIO;

	parent = parent_port_of(port);
	if (!parent)
		return rc;

	struct device *dev __free(put_device) =
		device_find_child(&parent->dev, range, match_switch_decoder_by_range);
	if (!dev) {
		dev_err(port->uport_dev,
			"failed to find decoder mapping %#llx-%#llx\n",
			range->start, range->end);
		return rc;
	}
	cxlsd = to_cxl_switch_decoder(dev);
	*ways = cxlsd->cxld.interleave_ways;

	for (int i = 0; i < *ways; i++) {
		if (cxlsd->target[i] == port->parent_dport) {
			*pos = i;
			rc = 0;
			break;
		}
	}
	if (rc)
		dev_err(port->uport_dev,
			"failed to find %s:%s in target list of %s\n",
			dev_name(&port->dev),
			dev_name(port->parent_dport->dport_dev), dev_name(dev));

	return rc;
}

/**
 * cxl_calc_interleave_pos() - calculate an endpoint position in a region
 * @cxled: endpoint decoder member of given region
 * @hpa_range: translated HPA range of the endpoint
 *
 * The endpoint position is calculated by traversing the topology from
 * the endpoint to the root decoder and iteratively applying this
 * calculation:
 *
 *    position = position * parent_ways + parent_pos;
 *
 * ...where @position is inferred from switch and root decoder target lists.
 *
 * Return: position >= 0 on success
 *	   -ENXIO on failure
 */
static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
				   struct range *hpa_range)
{
	struct cxl_port *iter, *port = cxled_to_port(cxled);
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	int parent_ways = 0, parent_pos = 0, pos = 0;
	int rc;

	/*
	 * Example: the expected interleave order of the 4-way region shown
	 * below is: mem0, mem2, mem1, mem3
	 *
	 *		  root_port
	 *                 /      \
	 *      host_bridge_0    host_bridge_1
	 *        |    |           |    |
	 *       mem0 mem1        mem2 mem3
	 *
	 * In the example the calculator will iterate twice. The first iteration
	 * uses the mem position in the host-bridge and the ways of the host-
	 * bridge to generate the first, or local, position. The second
	 * iteration uses the host-bridge position in the root_port and the ways
	 * of the root_port to refine the position.
	 *
	 * A trace of the calculation per endpoint looks like this:
	 * mem0: pos = 0 * 2 + 0    mem2: pos = 0 * 2 + 0
	 *       pos = 0 * 2 + 0          pos = 0 * 2 + 1
	 *       pos: 0                   pos: 1
	 *
	 * mem1: pos = 0 * 2 + 1    mem3: pos = 0 * 2 + 1
	 *       pos = 1 * 2 + 0          pos = 1 * 2 + 1
	 *       pos: 2                   pos = 3
	 *
	 * Note that while this example is simple, the method applies to more
	 * complex topologies, including those with switches.
	 */

	/* Iterate from endpoint to root_port refining the position */
	for (iter = port; iter; iter = parent_port_of(iter)) {
		if (is_cxl_root(iter))
			break;

		rc = find_pos_and_ways(iter, hpa_range, &parent_pos,
				       &parent_ways);
		if (rc)
			return rc;

		pos = pos * parent_ways + parent_pos;
	}

	dev_dbg(&cxlmd->dev,
		"decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
		dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
		dev_name(&port->dev), hpa_range->start, hpa_range->end, pos);

	return pos;
}

static int cxl_region_sort_targets(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	int i, rc = 0;

	for (i = 0; i < p->nr_targets; i++) {
		struct cxl_endpoint_decoder *cxled = p->targets[i];

		cxled->pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range);
		/*
		 * Record that sorting failed, but still continue to calc
		 * cxled->pos so that cxl_calc_interleave_pos() emits its
		 * dev_dbg() for every member. which is useful for auto
		 * discovery debug.
		 */
		if (cxled->pos < 0)
			rc = -ENXIO;
	}
	/* Keep the cxlr target list in interleave position order */
	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
	     cmp_interleave_pos, NULL);

	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
	return rc;
}

static int cxl_region_attach(struct cxl_region *cxlr,
			     struct cxl_endpoint_decoder *cxled, int pos)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_dev_state *cxlds = cxlmd->cxlds;
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_port *ep_port, *root_port;
	struct cxl_dport *dport;
	int rc = -ENXIO;

	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
				  p->interleave_granularity);
	if (rc) {
		dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
			dev_name(&cxled->cxld.dev), p->interleave_ways,
			p->interleave_granularity);
		return rc;
	}

	if (cxled->part < 0) {
		dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
		return -ENODEV;
	}

	if (cxlds->part[cxled->part].mode != cxlr->mode) {
		dev_dbg(&cxlr->dev, "%s region mode: %d mismatch\n",
			dev_name(&cxled->cxld.dev), cxlr->mode);
		return -EINVAL;
	}

	/* all full of members, or interleave config not established? */
	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
		dev_dbg(&cxlr->dev, "region already active\n");
		return -EBUSY;
	}

	if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
		dev_dbg(&cxlr->dev, "interleave config missing\n");
		return -ENXIO;
	}

	if (p->nr_targets >= p->interleave_ways) {
		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
			p->nr_targets);
		return -EINVAL;
	}

	ep_port = cxled_to_port(cxled);
	root_port = cxlrd_to_port(cxlrd);
	dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
	if (!dport) {
		dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			dev_name(cxlr->dev.parent));
		return -ENXIO;
	}

	if (cxled->cxld.target_type != cxlr->type) {
		dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			cxled->cxld.target_type, cxlr->type);
		return -ENXIO;
	}

	if (!cxled->dpa_res) {
		dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
		return -ENXIO;
	}

	if (resource_size(cxled->dpa_res) * p->interleave_ways + p->cache_size !=
	    resource_size(p->res)) {
		dev_dbg(&cxlr->dev,
			"%s:%s-size-%#llx * ways-%d + cache-%#llx != region-size-%#llx\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			(u64)resource_size(cxled->dpa_res), p->interleave_ways,
			(u64)p->cache_size, (u64)resource_size(p->res));
		return -EINVAL;
	}

	cxl_region_perf_data_calculate(cxlr, cxled);

	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
		int i;

		rc = cxl_region_attach_auto(cxlr, cxled, pos);
		if (rc)
			return rc;

		/* await more targets to arrive... */
		if (p->nr_targets < p->interleave_ways)
			return 0;

		/*
		 * All targets are here, which implies all PCI enumeration that
		 * affects this region has been completed. Walk the topology to
		 * sort the devices into their relative region decode position.
		 */
		rc = cxl_region_sort_targets(cxlr);
		if (rc)
			return rc;

		for (i = 0; i < p->nr_targets; i++) {
			cxled = p->targets[i];
			ep_port = cxled_to_port(cxled);
			dport = cxl_find_dport_by_dev(root_port,
						      ep_port->host_bridge);
			rc = cxl_region_attach_position(cxlr, cxled, dport, i);
			if (rc)
				return rc;
		}

		rc = cxl_region_setup_targets(cxlr);
		if (rc)
			return rc;

		/*
		 * If target setup succeeds in the autodiscovery case
		 * then the region is already committed.
		 */
		p->state = CXL_CONFIG_COMMIT;
		cxl_region_shared_upstream_bandwidth_update(cxlr);

		return 0;
	}

	rc = cxl_region_validate_position(cxlr, cxled, pos);
	if (rc)
		return rc;

	rc = cxl_region_attach_position(cxlr, cxled, dport, pos);
	if (rc)
		return rc;

	p->targets[pos] = cxled;
	cxled->pos = pos;
	p->nr_targets++;

	if (p->nr_targets == p->interleave_ways) {
		rc = cxl_region_setup_targets(cxlr);
		if (rc)
			return rc;
		p->state = CXL_CONFIG_ACTIVE;
		cxl_region_shared_upstream_bandwidth_update(cxlr);
	}

	cxled->cxld.interleave_ways = p->interleave_ways;
	cxled->cxld.interleave_granularity = p->interleave_granularity;
	cxled->cxld.hpa_range = (struct range) {
		.start = p->res->start,
		.end = p->res->end,
	};

	if (p->nr_targets != p->interleave_ways)
		return 0;

	/*
	 * Test the auto-discovery position calculator function
	 * against this successfully created user-defined region.
	 * A fail message here means that this interleave config
	 * will fail when presented as CXL_REGION_F_AUTO.
	 */
	for (int i = 0; i < p->nr_targets; i++) {
		struct cxl_endpoint_decoder *target = p->targets[i];
		int test_pos;

		test_pos = cxl_calc_interleave_pos(target, &cxlr->hpa_range);
		dev_dbg(&target->cxld.dev,
			"Test cxl_calc_interleave_pos(): %s test_pos:%d target->pos:%d\n",
			(test_pos == target->pos) ? "success" : "fail",
			test_pos, target->pos);
	}

	return 0;
}

static int cxl_region_remove_target(struct device *dev, void *data)
{
	struct cxl_endpoint_decoder *cxled = data;
	struct cxl_region_params *p;
	struct cxl_region *cxlr;
	int i;

	if (!is_cxl_region(dev))
		return 0;

	cxlr = to_cxl_region(dev);
	p = &cxlr->params;
	for (i = 0; i < p->interleave_ways; i++) {
		if (p->targets[i] == cxled) {
			p->nr_targets--;
			cxled->state = CXL_DECODER_STATE_AUTO;
			cxled->pos = -1;
			p->targets[i] = NULL;

			return 1;
		}
	}

	return 0;
}

/*
 * When an auto-region fails to assemble the decoder may be listed as a target,
 * but not fully attached.
 */
static void cxl_cancel_auto_attach(struct cxl_endpoint_decoder *cxled)
{
	if (cxled->state != CXL_DECODER_STATE_AUTO_STAGED)
		return;

	bus_for_each_dev(&cxl_bus_type, NULL, cxled, cxl_region_remove_target);
}

static struct cxl_region *
__cxl_decoder_detach(struct cxl_region *cxlr,
		     struct cxl_endpoint_decoder *cxled, int pos,
		     enum cxl_detach_mode mode)
{
	struct cxl_region_params *p;

	lockdep_assert_held_write(&cxl_rwsem.region);

	if (!cxled) {
		p = &cxlr->params;

		if (pos >= p->interleave_ways) {
			dev_dbg(&cxlr->dev, "position %d out of range %d\n",
				pos, p->interleave_ways);
			return NULL;
		}

		if (!p->targets[pos])
			return NULL;
		cxled = p->targets[pos];
	} else {
		cxlr = cxled->cxld.region;
		if (!cxlr) {
			cxl_cancel_auto_attach(cxled);
			return NULL;
		}
		p = &cxlr->params;
	}

	if (mode == DETACH_INVALIDATE)
		cxled->part = -1;

	if (p->state > CXL_CONFIG_ACTIVE) {
		cxl_region_decode_reset(cxlr, p->interleave_ways);
		p->state = CXL_CONFIG_ACTIVE;
	}

	for (struct cxl_port *iter = cxled_to_port(cxled); !is_cxl_root(iter);
	     iter = to_cxl_port(iter->dev.parent))
		cxl_port_detach_region(iter, cxlr, cxled);

	if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
	    p->targets[cxled->pos] != cxled) {
		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);

		dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
			      dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			      cxled->pos);
		return NULL;
	}

	if (p->state == CXL_CONFIG_ACTIVE) {
		p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
		cxl_region_teardown_targets(cxlr);
	}
	p->targets[cxled->pos] = NULL;
	p->nr_targets--;
	cxled->cxld.hpa_range = (struct range) {
		.start = 0,
		.end = -1,
	};

	get_device(&cxlr->dev);
	return cxlr;
}

/*
 * Cleanup a decoder's interest in a region. There are 2 cases to
 * handle, removing an unknown @cxled from a known position in a region
 * (detach_target()) or removing a known @cxled from an unknown @cxlr
 * (cxld_unregister())
 *
 * When the detachment finds a region release the region driver.
 */
int cxl_decoder_detach(struct cxl_region *cxlr,
		       struct cxl_endpoint_decoder *cxled, int pos,
		       enum cxl_detach_mode mode)
{
	struct cxl_region *detach;

	/* when the decoder is being destroyed lock unconditionally */
	if (mode == DETACH_INVALIDATE) {
		guard(rwsem_write)(&cxl_rwsem.region);
		detach = __cxl_decoder_detach(cxlr, cxled, pos, mode);
	} else {
		int rc;

		ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
		if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
			return rc;
		detach = __cxl_decoder_detach(cxlr, cxled, pos, mode);
	}

	if (detach) {
		device_release_driver(&detach->dev);
		put_device(&detach->dev);
	}
	return 0;
}

static int __attach_target(struct cxl_region *cxlr,
			   struct cxl_endpoint_decoder *cxled, int pos,
			   unsigned int state)
{
	int rc;

	if (state == TASK_INTERRUPTIBLE) {
		ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
		if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
			return rc;
		guard(rwsem_read)(&cxl_rwsem.dpa);
		return cxl_region_attach(cxlr, cxled, pos);
	}
	guard(rwsem_write)(&cxl_rwsem.region);
	guard(rwsem_read)(&cxl_rwsem.dpa);
	return cxl_region_attach(cxlr, cxled, pos);
}

static int attach_target(struct cxl_region *cxlr,
			 struct cxl_endpoint_decoder *cxled, int pos,
			 unsigned int state)
{
	int rc = __attach_target(cxlr, cxled, pos, state);

	if (rc == 0)
		return 0;

	dev_warn(cxled->cxld.dev.parent, "failed to attach %s to %s: %d\n",
		 dev_name(&cxled->cxld.dev), dev_name(&cxlr->dev), rc);
	return rc;
}

static int detach_target(struct cxl_region *cxlr, int pos)
{
	return cxl_decoder_detach(cxlr, NULL, pos, DETACH_ONLY);
}

static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
			    size_t len)
{
	int rc;

	if (sysfs_streq(buf, "\n"))
		rc = detach_target(cxlr, pos);
	else {
		struct device *dev;

		dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
		if (!dev)
			return -ENODEV;

		if (!is_endpoint_decoder(dev)) {
			rc = -EINVAL;
			goto out;
		}

		rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
				   TASK_INTERRUPTIBLE);
out:
		put_device(dev);
	}

	if (rc < 0)
		return rc;
	return len;
}

#define TARGET_ATTR_RW(n)                                              \
static ssize_t target##n##_show(                                       \
	struct device *dev, struct device_attribute *attr, char *buf)  \
{                                                                      \
	return show_targetN(to_cxl_region(dev), buf, (n));             \
}                                                                      \
static ssize_t target##n##_store(struct device *dev,                   \
				 struct device_attribute *attr,        \
				 const char *buf, size_t len)          \
{                                                                      \
	return store_targetN(to_cxl_region(dev), buf, (n), len);       \
}                                                                      \
static DEVICE_ATTR_RW(target##n)

TARGET_ATTR_RW(0);
TARGET_ATTR_RW(1);
TARGET_ATTR_RW(2);
TARGET_ATTR_RW(3);
TARGET_ATTR_RW(4);
TARGET_ATTR_RW(5);
TARGET_ATTR_RW(6);
TARGET_ATTR_RW(7);
TARGET_ATTR_RW(8);
TARGET_ATTR_RW(9);
TARGET_ATTR_RW(10);
TARGET_ATTR_RW(11);
TARGET_ATTR_RW(12);
TARGET_ATTR_RW(13);
TARGET_ATTR_RW(14);
TARGET_ATTR_RW(15);

static struct attribute *target_attrs[] = {
	&dev_attr_target0.attr,
	&dev_attr_target1.attr,
	&dev_attr_target2.attr,
	&dev_attr_target3.attr,
	&dev_attr_target4.attr,
	&dev_attr_target5.attr,
	&dev_attr_target6.attr,
	&dev_attr_target7.attr,
	&dev_attr_target8.attr,
	&dev_attr_target9.attr,
	&dev_attr_target10.attr,
	&dev_attr_target11.attr,
	&dev_attr_target12.attr,
	&dev_attr_target13.attr,
	&dev_attr_target14.attr,
	&dev_attr_target15.attr,
	NULL,
};

static umode_t cxl_region_target_visible(struct kobject *kobj,
					 struct attribute *a, int n)
{
	struct device *dev = kobj_to_dev(kobj);
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;

	if (n < p->interleave_ways)
		return a->mode;
	return 0;
}

static const struct attribute_group cxl_region_target_group = {
	.attrs = target_attrs,
	.is_visible = cxl_region_target_visible,
};

static const struct attribute_group *get_cxl_region_target_group(void)
{
	return &cxl_region_target_group;
}

static const struct attribute_group *region_groups[] = {
	&cxl_base_attribute_group,
	&cxl_region_group,
	&cxl_region_target_group,
	&cxl_region_access0_coordinate_group,
	&cxl_region_access1_coordinate_group,
	NULL,
};

static void cxl_region_release(struct device *dev)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	int id = atomic_read(&cxlrd->region_id);

	/*
	 * Try to reuse the recently idled id rather than the cached
	 * next id to prevent the region id space from increasing
	 * unnecessarily.
	 */
	if (cxlr->id < id)
		if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
			memregion_free(id);
			goto out;
		}

	memregion_free(cxlr->id);
out:
	put_device(dev->parent);
	kfree(cxlr);
}

const struct device_type cxl_region_type = {
	.name = "cxl_region",
	.release = cxl_region_release,
	.groups = region_groups
};

bool is_cxl_region(struct device *dev)
{
	return dev->type == &cxl_region_type;
}
EXPORT_SYMBOL_NS_GPL(is_cxl_region, "CXL");

static struct cxl_region *to_cxl_region(struct device *dev)
{
	if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
			  "not a cxl_region device\n"))
		return NULL;

	return container_of(dev, struct cxl_region, dev);
}

static void unregister_region(struct cxl_region *cxlr)
{
	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
	struct cxl_region_params *p = &cxlr->params;
	int i;

	xa_erase(&cxlrd->regions, cxlr->id);
	device_del(&cxlr->dev);

	/*
	 * Now that region sysfs is shutdown, the parameter block is now
	 * read-only, so no need to hold the region rwsem to access the
	 * region parameters.
	 */
	for (i = 0; i < p->interleave_ways; i++)
		detach_target(cxlr, i);

	cxlr->hpa_range = DEFINE_RANGE(0, -1);

	cxl_region_iomem_release(cxlr);
	put_device(&cxlr->dev);
}

static void endpoint_unregister_region(void *_cxlr)
{
	struct cxl_region *cxlr = _cxlr;
	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);

	guard(mutex)(&cxlrd->regions_lock);
	if (xa_load(&cxlrd->regions, cxlr->id))
		unregister_region(cxlr);
	put_device(&cxlr->dev);
}

static struct lock_class_key cxl_region_key;

static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
{
	struct cxl_region *cxlr;
	struct device *dev;

	cxlr = kzalloc_obj(*cxlr);
	if (!cxlr) {
		memregion_free(id);
		return ERR_PTR(-ENOMEM);
	}

	dev = &cxlr->dev;
	device_initialize(dev);
	lockdep_set_class(&dev->mutex, &cxl_region_key);
	dev->parent = &cxlrd->cxlsd.cxld.dev;
	/*
	 * Keep root decoder pinned through cxl_region_release to fixup
	 * region id allocations
	 */
	get_device(dev->parent);
	cxlr->cxlrd = cxlrd;
	cxlr->id = id;

	device_set_pm_not_required(dev);
	dev->bus = &cxl_bus_type;
	dev->type = &cxl_region_type;
	cxl_region_setup_flags(cxlr, &cxlrd->cxlsd.cxld);

	return cxlr;
}

static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid)
{
	int cset = 0;
	int rc;

	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
		if (cxlr->coord[i].read_bandwidth) {
			node_update_perf_attrs(nid, &cxlr->coord[i], i);
			cset++;
		}
	}

	if (!cset)
		return false;

	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access0_group());
	if (rc)
		dev_dbg(&cxlr->dev, "Failed to update access0 group\n");

	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access1_group());
	if (rc)
		dev_dbg(&cxlr->dev, "Failed to update access1 group\n");

	return true;
}

static int cxl_region_perf_attrs_callback(struct notifier_block *nb,
					  unsigned long action, void *arg)
{
	struct cxl_region *cxlr = container_of(nb, struct cxl_region,
					       node_notifier);
	struct node_notify *nn = arg;
	int nid = nn->nid;
	int region_nid;

	if (action != NODE_ADDED_FIRST_MEMORY)
		return NOTIFY_DONE;

	/*
	 * No need to hold cxl_rwsem.region; region parameters are stable
	 * within the cxl_region driver.
	 */
	region_nid = phys_to_target_node(cxlr->params.res->start);
	if (nid != region_nid)
		return NOTIFY_DONE;

	/* No action needed if node bit already set */
	if (node_test_and_set(nid, nodemask_region_seen))
		return NOTIFY_DONE;

	if (!cxl_region_update_coordinates(cxlr, nid))
		return NOTIFY_DONE;

	return NOTIFY_OK;
}

static int cxl_region_calculate_adistance(struct notifier_block *nb,
					  unsigned long nid, void *data)
{
	struct cxl_region *cxlr = container_of(nb, struct cxl_region,
					       adist_notifier);
	struct access_coordinate *perf;
	int *adist = data;
	int region_nid;

	/*
	 * No need to hold cxl_rwsem.region; region parameters are stable
	 * within the cxl_region driver.
	 */
	region_nid = phys_to_target_node(cxlr->params.res->start);
	if (nid != region_nid)
		return NOTIFY_OK;

	perf = &cxlr->coord[ACCESS_COORDINATE_CPU];

	if (mt_perf_to_adistance(perf, adist))
		return NOTIFY_OK;

	return NOTIFY_STOP;
}

/* unwind all remaining regions */
void kill_regions(struct cxl_root_decoder *cxlrd)
{
	unsigned long index;
	struct cxl_region *cxlr;

	guard(mutex)(&cxlrd->regions_lock);
	/* no more region creation */
	cxlrd->dead = true;
	xa_for_each(&cxlrd->regions, index, cxlr)
		unregister_region(cxlr);
}

/**
 * devm_cxl_add_region - Adds a region to a decoder
 * @cxlrd: root decoder
 * @id: memregion id to create, or memregion_free() on failure
 * @mode: mode for the endpoint decoders of this region
 * @type: select whether this is an expander or accelerator (type-2 or type-3)
 *
 * This is the second step of region initialization. Regions exist within an
 * address space which is mapped by a @cxlrd.
 *
 * Return: 0 if the region was added to the @cxlrd, else returns negative error
 * code. The region will be named "regionZ" where Z is the unique region number.
 */
static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
					      int id,
					      enum cxl_partition_mode mode,
					      enum cxl_decoder_type type)
{
	struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
	struct cxl_region *cxlr;
	struct device *dev;
	int rc;

	cxlr = cxl_region_alloc(cxlrd, id);
	if (IS_ERR(cxlr))
		return cxlr;
	cxlr->mode = mode;
	cxlr->type = type;

	dev = &cxlr->dev;
	rc = dev_set_name(dev, "region%d", id);
	if (rc)
		goto err;

	rc = device_add(dev);
	if (rc)
		goto err;

	rc = xa_insert(&cxlrd->regions, cxlr->id, cxlr, GFP_KERNEL);
	if (rc) {
		unregister_region(cxlr);
		return ERR_PTR(rc);
	}

	dev_dbg(port->uport_dev, "%s: created %s\n",
		dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
	return cxlr;
err:
	put_device(dev);
	return ERR_PTR(rc);
}

static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
{
	return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
}

static ssize_t create_pmem_region_show(struct device *dev,
				       struct device_attribute *attr, char *buf)
{
	return __create_region_show(to_cxl_root_decoder(dev), buf);
}

static ssize_t create_ram_region_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	return __create_region_show(to_cxl_root_decoder(dev), buf);
}

static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
					  enum cxl_partition_mode mode, int id,
					  enum cxl_decoder_type target_type)
{
	int rc;

	if (cxlrd->dead)
		return ERR_PTR(-ENXIO);

	switch (mode) {
	case CXL_PARTMODE_RAM:
	case CXL_PARTMODE_PMEM:
		break;
	default:
		dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
		return ERR_PTR(-EINVAL);
	}

	rc = memregion_alloc(GFP_KERNEL);
	if (rc < 0)
		return ERR_PTR(rc);

	if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
		memregion_free(rc);
		return ERR_PTR(-EBUSY);
	}

	return devm_cxl_add_region(cxlrd, id, mode, target_type);
}

static ssize_t create_region_store(struct device *dev, const char *buf,
				   size_t len, enum cxl_partition_mode mode)
{
	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
	struct cxl_region *cxlr;
	int rc, id;

	rc = sscanf(buf, "region%d\n", &id);
	if (rc != 1)
		return -EINVAL;

	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
		return rc;

	cxlr = __create_region(cxlrd, mode, id, CXL_DECODER_HOSTONLYMEM);
	if (IS_ERR(cxlr))
		return PTR_ERR(cxlr);

	return len;
}

static ssize_t create_pmem_region_store(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t len)
{
	return create_region_store(dev, buf, len, CXL_PARTMODE_PMEM);
}
DEVICE_ATTR_RW(create_pmem_region);

static ssize_t create_ram_region_store(struct device *dev,
				       struct device_attribute *attr,
				       const char *buf, size_t len)
{
	return create_region_store(dev, buf, len, CXL_PARTMODE_RAM);
}
DEVICE_ATTR_RW(create_ram_region);

static ssize_t region_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct cxl_decoder *cxld = to_cxl_decoder(dev);
	ssize_t rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
		return rc;

	if (cxld->region)
		return sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
	return sysfs_emit(buf, "\n");
}
DEVICE_ATTR_RO(region);

static ssize_t delete_region_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t len)
{
	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
	struct cxl_region *cxlr;
	int rc, id;

	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
		return rc;

	rc = sscanf(buf, "region%d\n", &id);
	if (rc != 1)
		return -EINVAL;

	cxlr = xa_load(&cxlrd->regions, id);
	if (!cxlr || !sysfs_streq(buf, dev_name(&cxlr->dev)))
		return -ENODEV;

	unregister_region(cxlr);

	return len;
}
DEVICE_ATTR_WO(delete_region);

struct cxl_poison_context {
	struct cxl_port *port;
	int part;
	u64 offset;
};

static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
				   struct cxl_poison_context *ctx)
{
	struct cxl_dev_state *cxlds = cxlmd->cxlds;
	const struct resource *res;
	struct resource *p, *last;
	u64 offset, length;
	int rc = 0;

	if (ctx->part < 0)
		return 0;

	/*
	 * Collect poison for the remaining unmapped resources after
	 * poison is collected by committed endpoints decoders.
	 */
	for (int i = ctx->part; i < cxlds->nr_partitions; i++) {
		res = &cxlds->part[i].res;
		for (p = res->child, last = NULL; p; p = p->sibling)
			last = p;
		if (last)
			offset = last->end + 1;
		else
			offset = res->start;
		length = res->end - offset + 1;
		if (!length)
			break;
		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
			continue;
		if (rc)
			break;
	}

	return rc;
}

static int poison_by_decoder(struct device *dev, void *arg)
{
	struct cxl_poison_context *ctx = arg;
	struct cxl_endpoint_decoder *cxled;
	enum cxl_partition_mode mode;
	struct cxl_dev_state *cxlds;
	struct cxl_memdev *cxlmd;
	u64 offset, length;
	int rc = 0;

	if (!is_endpoint_decoder(dev))
		return rc;

	cxled = to_cxl_endpoint_decoder(dev);
	if (!cxled->dpa_res)
		return rc;

	cxlmd = cxled_to_memdev(cxled);
	cxlds = cxlmd->cxlds;
	mode = cxlds->part[cxled->part].mode;

	if (cxled->skip) {
		offset = cxled->dpa_res->start - cxled->skip;
		length = cxled->skip;
		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
		if (rc == -EFAULT && mode == CXL_PARTMODE_RAM)
			rc = 0;
		if (rc)
			return rc;
	}

	offset = cxled->dpa_res->start;
	length = cxled->dpa_res->end - offset + 1;
	rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
	if (rc == -EFAULT && mode == CXL_PARTMODE_RAM)
		rc = 0;
	if (rc)
		return rc;

	/* Iterate until commit_end is reached */
	if (cxled->cxld.id == ctx->port->commit_end) {
		ctx->offset = cxled->dpa_res->end + 1;
		ctx->part = cxled->part;
		return 1;
	}

	return 0;
}

int cxl_get_poison_by_endpoint(struct cxl_port *port)
{
	struct cxl_poison_context ctx;
	int rc = 0;

	ctx = (struct cxl_poison_context) {
		.port = port,
		.part = -1,
	};

	rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
	if (rc == 1)
		rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev),
					     &ctx);

	return rc;
}

struct cxl_dpa_to_region_context {
	struct cxl_region *cxlr;
	u64 dpa;
};

static int __cxl_dpa_to_region(struct device *dev, void *arg)
{
	struct cxl_dpa_to_region_context *ctx = arg;
	struct cxl_endpoint_decoder *cxled;
	struct cxl_region *cxlr;
	u64 dpa = ctx->dpa;

	if (!is_endpoint_decoder(dev))
		return 0;

	cxled = to_cxl_endpoint_decoder(dev);
	if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
		return 0;

	if (!cxl_resource_contains_addr(cxled->dpa_res, dpa))
		return 0;

	/*
	 * Stop the region search (return 1) when an endpoint mapping is
	 * found. The region may not be fully constructed so offering
	 * the cxlr in the context structure is not guaranteed.
	 */
	cxlr = cxled->cxld.region;
	if (cxlr)
		dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa,
			dev_name(&cxlr->dev));
	else
		dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa,
			dev_name(dev));

	ctx->cxlr = cxlr;

	return 1;
}

struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
{
	struct cxl_dpa_to_region_context ctx;
	struct cxl_port *port = cxlmd->endpoint;

	if (!cxlmd->dev.driver)
		return NULL;

	ctx = (struct cxl_dpa_to_region_context) {
		.dpa = dpa,
	};
	if (cxl_num_decoders_committed(port))
		device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region);

	return ctx.cxlr;
}

static bool cxl_is_hpa_in_chunk(u64 hpa, struct cxl_region *cxlr, int pos)
{
	struct cxl_region_params *p = &cxlr->params;
	int gran = p->interleave_granularity;
	int ways = p->interleave_ways;
	u64 offset;

	/* Is the hpa in an expected chunk for its pos(-ition) */
	offset = hpa - p->res->start;
	offset = do_div(offset, gran * ways);
	if ((offset >= pos * gran) && (offset < (pos + 1) * gran))
		return true;

	dev_dbg(&cxlr->dev,
		"Addr trans fail: hpa 0x%llx not in expected chunk\n", hpa);

	return false;
}

#define CXL_POS_ZERO 0
/**
 * cxl_validate_translation_params
 * @eiw: encoded interleave ways
 * @eig: encoded interleave granularity
 * @pos: position in interleave
 *
 * Callers pass CXL_POS_ZERO when no position parameter needs validating.
 *
 * Returns: 0 on success, -EINVAL on first invalid parameter
 */
int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)
{
	int ways, gran;

	if (eiw_to_ways(eiw, &ways)) {
		pr_debug("%s: invalid eiw=%u\n", __func__, eiw);
		return -EINVAL;
	}
	if (eig_to_granularity(eig, &gran)) {
		pr_debug("%s: invalid eig=%u\n", __func__, eig);
		return -EINVAL;
	}
	if (pos < 0 || pos >= ways) {
		pr_debug("%s: invalid pos=%d for ways=%d\n", __func__, pos,
			 ways);
		return -EINVAL;
	}

	return 0;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_validate_translation_params, "cxl_translate");

u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig)
{
	u64 dpa_offset, bits_lower, bits_upper, temp;
	int ret;

	ret = cxl_validate_translation_params(eiw, eig, CXL_POS_ZERO);
	if (ret)
		return ULLONG_MAX;

	/*
	 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
	 * Lower bits [IG+7:0] pass through unchanged
	 * (eiw < 8)
	 *	Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
	 *	Clear the position bits to isolate upper section, then
	 *	reverse the left shift by eiw that occurred during DPA->HPA
	 * (eiw >= 8)
	 *	Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
	 *	Extract upper bits from the correct bit range and divide by 3
	 *	to recover the original DPA upper bits
	 */
	bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);
	if (eiw < 8) {
		temp = hpa_offset &= ~GENMASK_ULL(eig + eiw + 8 - 1, 0);
		dpa_offset = temp >> eiw;
	} else {
		bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
		dpa_offset = bits_upper << (eig + 8);
	}
	dpa_offset |= bits_lower;

	return dpa_offset;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");

int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig)
{
	int ways = 0;
	u64 shifted, rem;
	int pos, ret;

	ret = cxl_validate_translation_params(eiw, eig, CXL_POS_ZERO);
	if (ret)
		return ret;

	if (!eiw)
		/* position is 0 if no interleaving */
		return 0;

	/*
	 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
	 * eiw < 8
	 *	Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
	 *	Per spec "remove IW bits starting with bit position IG+8"
	 * eiw >= 8
	 *	Position is not explicitly stored in HPA_OFFSET bits. It is
	 *	derived from the modulo operation of the upper bits using
	 *	the total number of interleave ways.
	 */
	if (eiw < 8) {
		pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
	} else {
		shifted = hpa_offset >> (eig + 8);
		eiw_to_ways(eiw, &ways);
		div64_u64_rem(shifted, ways, &rem);
		pos = rem;
	}

	return pos;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_position, "cxl_translate");

u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig)
{
	u64 mask_upper, hpa_offset, bits_upper;
	int ret;

	ret = cxl_validate_translation_params(eiw, eig, pos);
	if (ret)
		return ULLONG_MAX;

	/*
	 * The device position in the region interleave set was removed
	 * from the offset at HPA->DPA translation. To reconstruct the
	 * HPA, place the 'pos' in the offset.
	 *
	 * The placement of 'pos' in the HPA is determined by interleave
	 * ways and granularity and is defined in the CXL Spec 3.0 Section
	 * 8.2.4.19.13 Implementation Note: Device Decode Logic
	 */

	mask_upper = GENMASK_ULL(51, eig + 8);

	if (eiw < 8) {
		hpa_offset = (dpa_offset & mask_upper) << eiw;
		hpa_offset |= pos << (eig + 8);
	} else {
		bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
		bits_upper = bits_upper * 3;
		hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
	}

	/* The lower bits remain unchanged */
	hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);

	return hpa_offset;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_hpa_offset, "cxl_translate");

static int decode_pos(int region_ways, int hb_ways, int pos, int *pos_port,
		      int *pos_hb)
{
	int devices_per_hb;

	/*
	 * Decode for 3-6-12 way interleaves as defined in the CXL
	 * Spec 4.0 9.13.1.1 Legal Interleaving Configurations.
	 * Region creation should prevent invalid combinations but
	 * sanity check here to avoid a silent bad decode.
	 */
	switch (hb_ways) {
	case 3:
		if (region_ways != 3 && region_ways != 6 && region_ways != 12)
			return -EINVAL;
		break;
	case 6:
		if (region_ways != 6 && region_ways != 12)
			return -EINVAL;
		break;
	case 12:
		if (region_ways != 12)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}
	/*
	 * Each host bridge contributes an equal number of endpoints
	 * that are laid out contiguously per host bridge. Modulo
	 * selects the port within a host bridge and division selects
	 * the host bridge position.
	 */
	devices_per_hb = region_ways / hb_ways;
	*pos_port = pos % devices_per_hb;
	*pos_hb = pos / devices_per_hb;

	return 0;
}

/*
 * restore_parent() reconstruct the address in parent
 *
 * This math, specifically the bitmask creation 'mask = gran - 1' relies
 * on the CXL Spec requirement that interleave granularity is always a
 * power of two.
 *
 * [mask]		isolate the offset with the granularity
 * [addr & ~mask]	remove the offset leaving the aligned portion
 * [* ways]		distribute across all interleave ways
 * [+ (pos * gran)]	add the positional offset
 * [+ (addr & mask)]	restore the masked offset
 */
static u64 restore_parent(u64 addr, u64 pos, u64 gran, u64 ways)
{
	u64 mask = gran - 1;

	return ((addr & ~mask) * ways) + (pos * gran) + (addr & mask);
}

/*
 * unaligned_dpa_to_hpa() translates a DPA to HPA when the region resource
 * start address is not aligned at Host Bridge Interleave Ways * 256MB.
 *
 * Unaligned start addresses only occur with MOD3 interleaves. All power-
 * of-two interleaves are guaranteed aligned.
 */
static u64 unaligned_dpa_to_hpa(struct cxl_decoder *cxld,
				struct cxl_region_params *p, int pos, u64 dpa)
{
	int ways_port = p->interleave_ways / cxld->interleave_ways;
	int gran_port = p->interleave_granularity;
	int gran_hb = cxld->interleave_granularity;
	int ways_hb = cxld->interleave_ways;
	int pos_port, pos_hb, gran_shift;
	u64 hpa_port = 0;

	/* Decode an endpoint 'pos' into port and host-bridge components */
	if (decode_pos(p->interleave_ways, ways_hb, pos, &pos_port, &pos_hb)) {
		dev_dbg(&cxld->dev, "not supported for region ways:%d\n",
			p->interleave_ways);
		return ULLONG_MAX;
	}

	/* Restore the port parent address if needed */
	if (gran_hb != gran_port)
		hpa_port = restore_parent(dpa, pos_port, gran_port, ways_port);
	else
		hpa_port = dpa;

	/*
	 * Complete the HPA reconstruction by restoring the address as if
	 * each HB position is a candidate. Test against expected pos_hb
	 * to confirm match.
	 */
	gran_shift = ilog2(gran_hb);
	for (int position = 0; position < ways_hb; position++) {
		u64 shifted, hpa;

		hpa = restore_parent(hpa_port, position, gran_hb, ways_hb);
		hpa += p->res->start;

		shifted = hpa >> gran_shift;
		if (do_div(shifted, ways_hb) == pos_hb)
			return hpa;
	}

	dev_dbg(&cxld->dev, "fail dpa:%#llx region:%pr pos:%d\n", dpa, p->res,
		pos);
	dev_dbg(&cxld->dev, "     port-w/g/p:%d/%d/%d hb-w/g/p:%d/%d/%d\n",
		ways_port, gran_port, pos_port, ways_hb, gran_hb, pos_hb);

	return ULLONG_MAX;
}

static bool region_is_unaligned_mod3(struct cxl_region *cxlr)
{
	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
	struct cxl_region_params *p = &cxlr->params;
	int hbiw = cxld->interleave_ways;
	u64 rem;

	if (is_power_of_2(hbiw))
		return false;

	div64_u64_rem(p->res->start, (u64)hbiw * SZ_256M, &rem);

	return (rem != 0);
}

u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
		   u64 dpa)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_endpoint_decoder *cxled = NULL;
	u64 base, dpa_offset, hpa_offset, hpa;
	bool unaligned = false;
	u16 eig = 0;
	u8 eiw = 0;
	int pos;

	/*
	 * Conversion between SPA and DPA is not supported in
	 * Normalized Address mode.
	 */
	if (test_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags))
		return ULLONG_MAX;

	for (int i = 0; i < p->nr_targets; i++) {
		if (cxlmd == cxled_to_memdev(p->targets[i])) {
			cxled = p->targets[i];
			break;
		}
	}
	if (!cxled)
		return ULLONG_MAX;

	base = cxl_dpa_resource_start(cxled);
	if (base == RESOURCE_SIZE_MAX)
		return ULLONG_MAX;

	dpa_offset = dpa - base;

	/* Unaligned calc for MOD3 interleaves not hbiw * 256MB aligned */
	unaligned = region_is_unaligned_mod3(cxlr);
	if (unaligned) {
		hpa = unaligned_dpa_to_hpa(cxld, p, cxled->pos, dpa_offset);
		if (hpa == ULLONG_MAX)
			return ULLONG_MAX;

		goto skip_aligned;
	}
	/*
	 * Aligned calc for all power-of-2 interleaves and for MOD3
	 * interleaves that are aligned at hbiw * 256MB
	 */
	pos = cxled->pos;
	ways_to_eiw(p->interleave_ways, &eiw);
	granularity_to_eig(p->interleave_granularity, &eig);

	hpa_offset = cxl_calculate_hpa_offset(dpa_offset, pos, eiw, eig);
	if (hpa_offset == ULLONG_MAX)
		return ULLONG_MAX;

	/* Apply the hpa_offset to the region base address */
	hpa = hpa_offset + p->res->start;

skip_aligned:
	hpa += p->cache_size;

	/* Root decoder translation overrides typical modulo decode */
	if (cxlrd->ops.hpa_to_spa)
		hpa = cxlrd->ops.hpa_to_spa(cxlrd, hpa);

	if (hpa == ULLONG_MAX)
		return ULLONG_MAX;

	if (!cxl_resource_contains_addr(p->res, hpa)) {
		dev_dbg(&cxlr->dev,
			"Addr trans fail: hpa 0x%llx not in region\n", hpa);
		return ULLONG_MAX;
	}
	/* Chunk check applies to aligned modulo decodes only */
	if (!unaligned && !cxlrd->ops.hpa_to_spa &&
	    !cxl_is_hpa_in_chunk(hpa, cxlr, pos))
		return ULLONG_MAX;

	return hpa;
}

struct dpa_result {
	struct cxl_memdev *cxlmd;
	u64 dpa;
};

static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
						 u64 offset,
						 struct dpa_result *result)
{
	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
	struct cxl_region_params *p = &cxlr->params;
	u64 interleave_width, interleave_index;
	u64 gran, gran_offset, dpa_offset;
	u64 hpa = p->res->start + offset;
	u64 tmp = offset;

	/*
	 * Unaligned addresses are not algebraically invertible. Calculate
	 * a dpa_offset independent of the target device and then enumerate
	 * and test that dpa_offset against each candidate endpoint decoder.
	 */
	gran = cxld->interleave_granularity;
	interleave_width = gran * cxld->interleave_ways;
	interleave_index = div64_u64(offset, interleave_width);
	gran_offset = do_div(tmp, gran);

	dpa_offset = interleave_index * gran + gran_offset;

	for (int i = 0; i < p->nr_targets; i++) {
		struct cxl_endpoint_decoder *cxled = p->targets[i];
		int pos = cxled->pos;
		u64 test_hpa;

		test_hpa = unaligned_dpa_to_hpa(cxld, p, pos, dpa_offset);
		if (test_hpa == hpa) {
			result->cxlmd = cxled_to_memdev(cxled);
			result->dpa =
				cxl_dpa_resource_start(cxled) + dpa_offset;
			return 0;
		}
	}
	dev_err(&cxlr->dev,
		"failed to resolve HPA %#llx in unaligned MOD3 region\n", hpa);

	return -ENXIO;
}

static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
				       struct dpa_result *result)
{
	struct cxl_region_params *p = &cxlr->params;
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_endpoint_decoder *cxled;
	u64 hpa_offset = offset;
	u64 dpa, dpa_offset;
	u16 eig = 0;
	u8 eiw = 0;
	int pos;

	lockdep_assert_held(&cxl_rwsem.region);
	lockdep_assert_held(&cxl_rwsem.dpa);

	/* Input validation ensures valid ways and gran */
	granularity_to_eig(p->interleave_granularity, &eig);
	ways_to_eiw(p->interleave_ways, &eiw);

	/*
	 * If the root decoder has SPA to CXL HPA callback, use it. Otherwise
	 * CXL HPA is assumed to equal SPA.
	 */
	if (cxlrd->ops.spa_to_hpa) {
		hpa_offset = cxlrd->ops.spa_to_hpa(cxlrd, p->res->start + offset);
		if (hpa_offset == ULLONG_MAX) {
			dev_dbg(&cxlr->dev, "HPA not found for %pr offset %#llx\n",
				p->res, offset);
			return -ENXIO;
		}
		hpa_offset -= p->res->start;
	}

	if (region_is_unaligned_mod3(cxlr))
		return unaligned_region_offset_to_dpa_result(cxlr, offset,
							     result);

	pos = cxl_calculate_position(hpa_offset, eiw, eig);
	if (pos < 0 || pos >= p->nr_targets) {
		dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n",
			pos, p->nr_targets);
		return -ENXIO;
	}

	dpa_offset = cxl_calculate_dpa_offset(hpa_offset, eiw, eig);

	/* Look-up and return the result: a memdev and a DPA */
	for (int i = 0; i < p->nr_targets; i++) {
		cxled = p->targets[i];
		if (cxled->pos != pos)
			continue;

		dpa = cxl_dpa_resource_start(cxled);
		if (dpa != RESOURCE_SIZE_MAX)
			dpa += dpa_offset;

		result->cxlmd = cxled_to_memdev(cxled);
		result->dpa = dpa;

		return 0;
	}
	dev_err(&cxlr->dev, "No device found for position %d\n", pos);

	return -ENXIO;
}

static int match_root_decoder(struct device *dev, const void *data)
{
	const struct range *r1, *r2 = data;
	struct cxl_root_decoder *cxlrd;

	if (!is_root_decoder(dev))
		return 0;

	cxlrd = to_cxl_root_decoder(dev);
	r1 = &cxlrd->cxlsd.cxld.hpa_range;

	return range_contains(r1, r2);
}

static int cxl_root_setup_translation(struct cxl_root *cxl_root,
				      struct cxl_region_context *ctx)
{
	if (!cxl_root->ops.translation_setup_root)
		return 0;

	return cxl_root->ops.translation_setup_root(cxl_root, ctx);
}

/*
 * Note, when finished with the device, drop the reference with
 * put_device() or use the put_cxl_root_decoder helper.
 */
static struct cxl_root_decoder *
get_cxl_root_decoder(struct cxl_endpoint_decoder *cxled,
		     struct cxl_region_context *ctx)
{
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_port *port = cxled_to_port(cxled);
	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
	struct device *cxlrd_dev;
	int rc;

	/*
	 * Adjust the endpoint's HPA range and interleaving
	 * configuration to the root decoder’s memory space before
	 * setting up the root decoder.
	 */
	rc = cxl_root_setup_translation(cxl_root, ctx);
	if (rc) {
		dev_err(cxlmd->dev.parent,
			"%s:%s Failed to setup translation for address range %#llx:%#llx\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			ctx->hpa_range.start, ctx->hpa_range.end);
		return ERR_PTR(rc);
	}

	cxlrd_dev = device_find_child(&cxl_root->port.dev, &ctx->hpa_range,
				      match_root_decoder);
	if (!cxlrd_dev) {
		dev_err(cxlmd->dev.parent,
			"%s:%s no CXL window for range %#llx:%#llx\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			ctx->hpa_range.start, ctx->hpa_range.end);
		return ERR_PTR(-ENXIO);
	}

	return to_cxl_root_decoder(cxlrd_dev);
}

static int match_region_by_range(struct device *dev, const void *data)
{
	struct cxl_region_params *p;
	struct cxl_region *cxlr;
	const struct range *r = data;

	if (!is_cxl_region(dev))
		return 0;

	cxlr = to_cxl_region(dev);
	p = &cxlr->params;

	guard(rwsem_read)(&cxl_rwsem.region);
	return spa_maps_hpa(p, r);
}

static int cxl_extended_linear_cache_resize(struct cxl_region *cxlr,
					    struct resource *res)
{
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_region_params *p = &cxlr->params;
	resource_size_t size = resource_size(res);
	resource_size_t cache_size, start;

	cache_size = cxlrd->cache_size;
	if (!cache_size)
		return 0;

	if (size != cache_size) {
		dev_warn(&cxlr->dev,
			 "Extended Linear Cache size %pa != CXL size %pa. No Support!",
			 &cache_size, &size);
		return -ENXIO;
	}

	/*
	 * Move the start of the range to where the cache range starts. The
	 * implementation assumes that the cache range is in front of the
	 * CXL range. This is not dictated by the HMAT spec but is how the
	 * current known implementation is configured.
	 *
	 * The cache range is expected to be within the CFMWS. The adjusted
	 * res->start should not be less than cxlrd->res->start.
	 */
	start = res->start - cache_size;
	if (start < cxlrd->res->start)
		return -ENXIO;

	res->start = start;
	p->cache_size = cache_size;

	return 0;
}

static int __construct_region(struct cxl_region *cxlr,
			      struct cxl_region_context *ctx)
{
	struct cxl_endpoint_decoder *cxled = ctx->cxled;
	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct range *hpa_range = &ctx->hpa_range;
	struct cxl_region_params *p;
	struct resource *res;
	int rc;

	guard(rwsem_write)(&cxl_rwsem.region);
	p = &cxlr->params;
	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
		dev_err(cxlmd->dev.parent,
			"%s:%s: %s autodiscovery interrupted\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			__func__);
		return -EBUSY;
	}

	set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
	cxlr->hpa_range = *hpa_range;

	res = kmalloc_obj(*res);
	if (!res)
		return -ENOMEM;

	*res = DEFINE_RES_MEM_NAMED(hpa_range->start, range_len(hpa_range),
				    dev_name(&cxlr->dev));

	rc = cxl_extended_linear_cache_resize(cxlr, res);
	if (rc && rc != -EOPNOTSUPP) {
		/*
		 * Failing to support extended linear cache region resize does not
		 * prevent the region from functioning. Only causes cxl list showing
		 * incorrect region size.
		 */
		dev_warn(cxlmd->dev.parent,
			 "Extended linear cache calculation failed rc:%d\n", rc);
	}

	rc = sysfs_update_group(&cxlr->dev.kobj, &cxl_region_group);
	if (rc) {
		kfree(res);
		return rc;
	}

	rc = insert_resource(cxlrd->res, res);
	if (rc) {
		/*
		 * Platform-firmware may not have split resources like "System
		 * RAM" on CXL window boundaries see cxl_region_iomem_release()
		 */
		dev_warn(cxlmd->dev.parent,
			 "%s:%s: %s %s cannot insert resource\n",
			 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			 __func__, dev_name(&cxlr->dev));
	}

	p->res = res;
	p->interleave_ways = ctx->interleave_ways;
	p->interleave_granularity = ctx->interleave_granularity;
	p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;

	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
	if (rc)
		return rc;

	dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
		dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
		dev_name(&cxlr->dev), p->res, p->interleave_ways,
		p->interleave_granularity);

	/* ...to match put_device() in cxl_add_to_region() */
	get_device(&cxlr->dev);

	return 0;
}

/* Establish an empty region covering the given HPA range */
static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
					   struct cxl_region_context *ctx)
{
	struct cxl_endpoint_decoder *cxled = ctx->cxled;
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_dev_state *cxlds = cxlmd->cxlds;
	int rc, part = READ_ONCE(cxled->part);
	struct cxl_region *cxlr;

	if (part < 0)
		return ERR_PTR(-EBUSY);

	do {
		cxlr = __create_region(cxlrd, cxlds->part[part].mode,
				       atomic_read(&cxlrd->region_id),
				       cxled->cxld.target_type);
	} while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);

	if (IS_ERR(cxlr)) {
		dev_err(cxlmd->dev.parent,
			"%s:%s: %s failed assign region: %pe\n",
			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
			__func__, cxlr);
		return cxlr;
	}

	rc = __construct_region(cxlr, ctx);
	if (rc) {
		unregister_region(cxlr);
		return ERR_PTR(rc);
	}

	return cxlr;
}

static struct cxl_region *
cxl_find_region_by_range(struct cxl_root_decoder *cxlrd,
			 struct range *hpa_range)
{
	struct device *region_dev;

	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa_range,
				       match_region_by_range);
	if (!region_dev)
		return NULL;

	return to_cxl_region(region_dev);
}

int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
{
	struct cxl_region_context ctx;
	struct cxl_region_params *p;
	bool attach = false;
	int rc;

	ctx = (struct cxl_region_context) {
		.cxled = cxled,
		.hpa_range = cxled->cxld.hpa_range,
		.interleave_ways = cxled->cxld.interleave_ways,
		.interleave_granularity = cxled->cxld.interleave_granularity,
	};

	struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) =
		get_cxl_root_decoder(cxled, &ctx);

	if (IS_ERR(cxlrd))
		return PTR_ERR(cxlrd);

	/*
	 * Ensure that, if multiple threads race to construct_region()
	 * for the HPA range, one does the construction and the others
	 * add to that.
	 */
	guard(mutex)(&cxlrd->regions_lock);
	struct cxl_region *cxlr __free(put_cxl_region) =
		cxl_find_region_by_range(cxlrd, &ctx.hpa_range);
	if (!cxlr)
		cxlr = construct_region(cxlrd, &ctx);

	rc = PTR_ERR_OR_ZERO(cxlr);
	if (rc)
		return rc;

	attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);

	scoped_guard(rwsem_read, &cxl_rwsem.region) {
		p = &cxlr->params;
		attach = p->state == CXL_CONFIG_COMMIT;
	}

	if (attach) {
		/*
		 * If device_attach() fails the range may still be active via
		 * the platform-firmware memory map, otherwise the driver for
		 * regions is local to this file, so driver matching can't fail.
		 */
		if (device_attach(&cxlr->dev) < 0)
			dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
				p->res);
	}

	return rc;
}
EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");

static int is_system_ram(struct resource *res, void *arg)
{
	struct cxl_region *cxlr = arg;
	struct cxl_region_params *p = &cxlr->params;

	dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
	return 1;
}

static void shutdown_notifiers(void *_cxlr)
{
	struct cxl_region *cxlr = _cxlr;

	unregister_node_notifier(&cxlr->node_notifier);
	unregister_mt_adistance_algorithm(&cxlr->adist_notifier);
}

static void remove_debugfs(void *dentry)
{
	debugfs_remove_recursive(dentry);
}

static int validate_region_offset(struct cxl_region *cxlr, u64 offset)
{
	struct cxl_region_params *p = &cxlr->params;
	resource_size_t region_size;
	u64 hpa;

	if (offset < p->cache_size) {
		dev_err(&cxlr->dev,
			"Offset %#llx is within extended linear cache %pa\n",
			offset, &p->cache_size);
		return -EINVAL;
	}

	region_size = resource_size(p->res);
	if (offset >= region_size) {
		dev_err(&cxlr->dev, "Offset %#llx exceeds region size %pa\n",
			offset, &region_size);
		return -EINVAL;
	}

	hpa = p->res->start + offset;
	if (hpa < p->res->start || hpa > p->res->end) {
		dev_err(&cxlr->dev, "HPA %#llx not in region %pr\n", hpa,
			p->res);
		return -EINVAL;
	}

	return 0;
}

static int cxl_region_debugfs_poison_inject(void *data, u64 offset)
{
	struct dpa_result result = { .dpa = ULLONG_MAX, .cxlmd = NULL };
	struct cxl_region *cxlr = data;
	int rc;

	ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &region_rwsem)))
		return rc;

	ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem)))
		return rc;

	if (validate_region_offset(cxlr, offset))
		return -EINVAL;

	offset -= cxlr->params.cache_size;
	rc = region_offset_to_dpa_result(cxlr, offset, &result);
	if (rc || !result.cxlmd || result.dpa == ULLONG_MAX) {
		dev_dbg(&cxlr->dev,
			"Failed to resolve DPA for region offset %#llx rc %d\n",
			offset, rc);

		return rc ? rc : -EINVAL;
	}

	return cxl_inject_poison_locked(result.cxlmd, result.dpa);
}

DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_inject_fops, NULL,
			 cxl_region_debugfs_poison_inject, "%llx\n");

static int cxl_region_debugfs_poison_clear(void *data, u64 offset)
{
	struct dpa_result result = { .dpa = ULLONG_MAX, .cxlmd = NULL };
	struct cxl_region *cxlr = data;
	int rc;

	ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &region_rwsem)))
		return rc;

	ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem)))
		return rc;

	if (validate_region_offset(cxlr, offset))
		return -EINVAL;

	offset -= cxlr->params.cache_size;
	rc = region_offset_to_dpa_result(cxlr, offset, &result);
	if (rc || !result.cxlmd || result.dpa == ULLONG_MAX) {
		dev_dbg(&cxlr->dev,
			"Failed to resolve DPA for region offset %#llx rc %d\n",
			offset, rc);

		return rc ? rc : -EINVAL;
	}

	return cxl_clear_poison_locked(result.cxlmd, result.dpa);
}

DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL,
			 cxl_region_debugfs_poison_clear, "%llx\n");

static int cxl_region_setup_poison(struct cxl_region *cxlr)
{
	struct device *dev = &cxlr->dev;
	struct cxl_region_params *p = &cxlr->params;
	struct dentry *dentry;

	/*
	 * Do not enable poison injection in Normalized Address mode.
	 * Conversion between SPA and DPA is required for this, but it is
	 * not supported in this mode.
	 */
	if (test_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags))
		return 0;

	/* Create poison attributes if all memdevs support the capabilities */
	for (int i = 0; i < p->nr_targets; i++) {
		struct cxl_endpoint_decoder *cxled = p->targets[i];
		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);

		if (!cxl_memdev_has_poison_cmd(cxlmd, CXL_POISON_ENABLED_INJECT) ||
		    !cxl_memdev_has_poison_cmd(cxlmd, CXL_POISON_ENABLED_CLEAR))
			return 0;
	}

	dentry = cxl_debugfs_create_dir(dev_name(dev));
	debugfs_create_file("inject_poison", 0200, dentry, cxlr,
			    &cxl_poison_inject_fops);
	debugfs_create_file("clear_poison", 0200, dentry, cxlr,
			    &cxl_poison_clear_fops);

	return devm_add_action_or_reset(dev, remove_debugfs, dentry);
}

static int region_contains_resource(struct device *dev, const void *data)
{
	const struct resource *res = data;
	struct cxl_region *cxlr;
	struct cxl_region_params *p;

	if (!is_cxl_region(dev))
		return 0;

	cxlr = to_cxl_region(dev);
	p = &cxlr->params;

	if (p->state != CXL_CONFIG_COMMIT)
		return 0;

	if (!p->res)
		return 0;

	return resource_contains(p->res, res) ? 1 : 0;
}

bool cxl_region_contains_resource(const struct resource *res)
{
	guard(rwsem_read)(&cxl_rwsem.region);
	struct device *dev __free(put_device) = bus_find_device(
		&cxl_bus_type, NULL, res, region_contains_resource);
	return !!dev;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_region_contains_resource, "dax_hmem");

static int cxl_region_can_probe(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;
	int rc;

	ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) {
		dev_dbg(&cxlr->dev, "probe interrupted\n");
		return rc;
	}

	if (p->state < CXL_CONFIG_COMMIT) {
		dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
		return -ENXIO;
	}

	if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) {
		dev_err(&cxlr->dev,
			"failed to activate, re-commit region and retry\n");
		return -ENXIO;
	}

	return 0;
}

static int first_mapped_decoder(struct device *dev, const void *data)
{
	struct cxl_endpoint_decoder *cxled;

	if (!is_endpoint_decoder(dev))
		return 0;

	cxled = to_cxl_endpoint_decoder(dev);
	if (cxled->cxld.region)
		return 1;

	return 0;
}

/*
 * Runs in cxl_mem_probe context after successful endpoint probe, assumes the
 * simple case of single mapped decoder per memdev.
 */
int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
{
	struct cxl_attach_region *attach =
		container_of(cxlmd->attach, typeof(*attach), attach);
	struct cxl_port *endpoint = cxlmd->endpoint;
	struct cxl_endpoint_decoder *cxled;
	struct cxl_region *cxlr;
	int rc;

	/* hold endpoint lock to setup autoremove of the region */
	guard(device)(&endpoint->dev);
	if (!endpoint->dev.driver)
		return -ENXIO;
	guard(rwsem_read)(&cxl_rwsem.region);
	guard(rwsem_read)(&cxl_rwsem.dpa);

	/*
	 * TODO auto-instantiate a region, for now assume this will find an
	 * auto-region
	 */
	struct device *dev __free(put_device) =
		device_find_child(&endpoint->dev, NULL, first_mapped_decoder);

	if (!dev) {
		dev_dbg(cxlmd->cxlds->dev, "no region found for memdev %s\n",
			dev_name(&cxlmd->dev));
		return -ENXIO;
	}

	cxled = to_cxl_endpoint_decoder(dev);
	cxlr = cxled->cxld.region;

	if (cxlr->params.state < CXL_CONFIG_COMMIT) {
		dev_dbg(cxlmd->cxlds->dev,
			"region %s not committed for memdev %s\n",
			dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
		return -ENXIO;
	}

	if (cxlr->params.nr_targets > 1) {
		dev_dbg(cxlmd->cxlds->dev,
			"Only attach to local non-interleaved region\n");
		return -ENXIO;
	}

	/* Only teardown regions that pass validation, ignore the rest */
	get_device(&cxlr->dev);
	rc = devm_add_action_or_reset(&endpoint->dev,
				      endpoint_unregister_region, cxlr);
	if (rc)
		return rc;

	attach->hpa_range = (struct range) {
		.start = cxlr->params.res->start,
		.end = cxlr->params.res->end,
	};
	return 0;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");

/*
 * The presence of an attach method indicates that the region is designated for
 * a purpose outside of CXL core memory expansion defaults.
 */
static bool cxl_region_has_memdev_attach(struct cxl_region *cxlr)
{
	struct cxl_region_params *p = &cxlr->params;

	for (int i = 0; i < p->nr_targets; i++) {
		struct cxl_endpoint_decoder *cxled = p->targets[i];
		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);

		if (cxlmd->attach)
			return true;
	}

	return false;
}

static int cxl_region_probe(struct device *dev)
{
	struct cxl_region *cxlr = to_cxl_region(dev);
	struct cxl_region_params *p = &cxlr->params;
	int rc;

	rc = cxl_region_can_probe(cxlr);
	if (rc)
		return rc;

	/*
	 * From this point on any path that changes the region's state away from
	 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
	 */

	cxlr->node_notifier.notifier_call = cxl_region_perf_attrs_callback;
	cxlr->node_notifier.priority = CXL_CALLBACK_PRI;
	register_node_notifier(&cxlr->node_notifier);

	cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance;
	cxlr->adist_notifier.priority = 100;
	register_mt_adistance_algorithm(&cxlr->adist_notifier);

	rc = devm_add_action_or_reset(&cxlr->dev, shutdown_notifiers, cxlr);
	if (rc)
		return rc;

	/*
	 * Regions fronted by an extended linear cache need the MCE notifier to
	 * offline the aliased page on a memory error.
	 */
	if (p->cache_size) {
		rc = devm_cxl_register_mce_notifier(&cxlr->dev,
						    &cxlr->mce_notifier);
		if (rc == -EOPNOTSUPP)
			dev_warn(&cxlr->dev,
				 "CONFIG_CXL_MCE disabled, MCE notifier not registered\n");
		else if (rc)
			return rc;
	}

	rc = cxl_region_setup_poison(cxlr);
	if (rc)
		return rc;

	if (cxl_region_has_memdev_attach(cxlr))
		return 0;

	switch (cxlr->mode) {
	case CXL_PARTMODE_PMEM:
		rc = devm_cxl_region_edac_register(cxlr);
		if (rc)
			dev_dbg(&cxlr->dev, "CXL EDAC registration for region_id=%d failed\n",
				cxlr->id);

		return devm_cxl_add_pmem_region(cxlr);
	case CXL_PARTMODE_RAM:
		rc = devm_cxl_region_edac_register(cxlr);
		if (rc)
			dev_dbg(&cxlr->dev, "CXL EDAC registration for region_id=%d failed\n",
				cxlr->id);

		/*
		 * The region can not be manged by CXL if any portion of
		 * it is already online as 'System RAM'
		 */
		if (walk_iomem_res_desc(IORES_DESC_NONE,
					IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
					p->res->start, p->res->end, cxlr,
					is_system_ram) > 0)
			return 0;
		return devm_cxl_add_dax_region(cxlr);
	default:
		dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
			cxlr->mode);
		return -ENXIO;
	}
}

static struct cxl_driver cxl_region_driver = {
	.name = "cxl_region",
	.probe = cxl_region_probe,
	.id = CXL_DEVICE_REGION,
};

int cxl_region_init(void)
{
	return cxl_driver_register(&cxl_region_driver);
}

void cxl_region_exit(void)
{
	cxl_driver_unregister(&cxl_region_driver);
}

MODULE_IMPORT_NS("CXL");
MODULE_IMPORT_NS("DEVMEM");
MODULE_ALIAS_CXL(CXL_DEVICE_REGION);