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
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2017 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <assert.h>
#include <limits.h>
#include <pthread.h>
#include <math.h>
#include <ctype.h>
#include "drm.h"
#include "drmtest.h"
#include "igt_device_scan.h"
#include "intel_chipset.h"
#include "intel_reg.h"
#include "ioctl_wrappers.h"
#include "intel_io.h"
#include "igt_aux.h"
#include "igt_rand.h"
#include "igt_perf.h"
#include "sw_sync.h"
#include "i915/gem_create.h"
#include "i915/gem_engine_topology.h"
#include "i915/gem_mman.h"
#include "igt_syncobj.h"
#include "intel_allocator.h"
#include "xe_drm.h"
#include "xe/xe_ioctl.h"
#include "xe/xe_spin.h"
enum intel_engine_class {
RCS,
BCS,
VCS,
VECS,
CCS,
NUM_ENGINE_CLASSES,
};
_Static_assert(RCS == DRM_XE_ENGINE_CLASS_RENDER, "mismatch");
_Static_assert(BCS == DRM_XE_ENGINE_CLASS_COPY, "mismatch");
_Static_assert(VCS == DRM_XE_ENGINE_CLASS_VIDEO_DECODE, "mismatch");
_Static_assert(VECS == DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE, "mismatch");
_Static_assert(CCS == DRM_XE_ENGINE_CLASS_COMPUTE, "mismatch");
_Static_assert((int)RCS == (int)I915_ENGINE_CLASS_RENDER, "mismatch");
_Static_assert((int)BCS == (int)I915_ENGINE_CLASS_COPY, "mismatch");
_Static_assert((int)VCS == (int)I915_ENGINE_CLASS_VIDEO, "mismatch");
_Static_assert((int)VECS == (int)I915_ENGINE_CLASS_VIDEO_ENHANCE, "mismatch");
_Static_assert((int)CCS == (int)I915_ENGINE_CLASS_COMPUTE, "mismatch");
static const char *intel_engine_class_string(uint16_t engine_class)
{
switch (engine_class) {
case RCS:
return "RCS";
case BCS:
return "BCS";
case VCS:
return "VCS";
case VECS:
return "VECS";
case CCS:
return "CCS";
default:
igt_assert(0);
}
}
struct duration {
unsigned int min, max;
bool unbound;
};
enum w_type {
BATCH,
SYNC,
DELAY,
PERIOD,
THROTTLE,
QD_THROTTLE,
SW_FENCE,
SW_FENCE_SIGNAL,
CTX_PRIORITY,
PREEMPTION,
ENGINE_MAP,
LOAD_BALANCE,
BOND,
TERMINATE,
SSEU,
WORKINGSET,
};
struct dep_entry {
int target;
bool write;
int working_set; /* -1 = step dependecy, >= 0 working set id */
};
struct deps {
int nr;
bool submit_fence;
struct dep_entry *list;
};
#define for_each_dep(__dep, __deps) \
for (int __i = 0; __i < __deps.nr && \
(__dep = &__deps.list[__i]); ++__i)
struct w_arg {
char *filename;
char *desc;
int prio;
bool sseu;
};
#define INVALID_ID ((uint16_t)-2)
#define DEFAULT_ID ((uint16_t)-1)
typedef struct drm_xe_engine_class_instance intel_engine_t;
struct intel_engines {
unsigned int nr_engines;
intel_engine_t *engines;
};
struct bond {
struct intel_engines mask;
intel_engine_t master;
};
struct work_buffer_size {
unsigned long size;
unsigned long min;
unsigned long max;
};
struct working_set {
int id;
bool shared;
unsigned int nr;
uint32_t *handles;
struct work_buffer_size *sizes;
};
struct workload;
struct w_step {
struct workload *wrk;
/* Workload step metadata */
enum w_type type;
unsigned int context;
intel_engine_t engine;
unsigned int engine_idx;
struct duration duration;
struct deps data_deps;
struct deps fence_deps;
int emit_fence;
union {
int sync;
int delay;
int period;
int target;
int throttle;
int priority;
struct intel_engines engine_map;
bool load_balance;
struct bond bond;
int sseu;
struct working_set working_set;
};
/* Implementation details */
unsigned int idx;
struct igt_list_head rq_link;
unsigned int request_idx;
unsigned int preempt_us;
union {
struct {
struct drm_i915_gem_execbuffer2 eb;
struct drm_i915_gem_exec_object2 *obj;
struct drm_i915_gem_relocation_entry reloc[3];
uint32_t *bb_duration;
} i915;
struct {
struct drm_xe_exec exec;
struct {
struct xe_spin spin;
uint64_t vm_sync;
uint64_t exec_sync;
} *data;
struct drm_xe_sync *syncs;
} xe;
};
unsigned long bb_size;
uint32_t bb_handle;
};
struct vm {
uint32_t id;
bool compute_mode;
uint64_t ahnd;
};
struct xe_exec_queue {
uint32_t id;
unsigned int nr_hwes;
struct drm_xe_engine_class_instance *hwe_list;
};
struct ctx {
uint32_t id;
int priority;
struct intel_engines engine_map;
unsigned int bond_count;
struct bond *bonds;
bool load_balance;
uint64_t sseu;
/* reference to vm */
struct vm *vm;
struct {
/* exec queues */
unsigned int nr_queues;
struct xe_exec_queue *queue_list;
} xe;
};
struct workload {
unsigned int id;
unsigned int nr_steps;
struct w_step *steps;
int prio;
bool sseu;
pthread_t thread;
bool run;
bool background;
unsigned int repeat;
unsigned int flags;
bool print_stats;
uint32_t bb_prng;
uint32_t bo_prng;
unsigned int nr_ctxs;
struct ctx *ctx_list;
unsigned int nr_vms;
struct vm *vm_list;
struct working_set **working_sets; /* array indexed by set id */
int max_working_set_id;
int sync_timeline;
uint32_t sync_seqno;
struct igt_list_head *requests;
unsigned int *nrequest;
};
#define __for_each_ctx(__ctx, __wrk, __ctx_idx) \
for (typeof((__wrk)->nr_ctxs) __ctx_idx = 0; __ctx_idx < (__wrk)->nr_ctxs && \
(__ctx = &(__wrk)->ctx_list[__ctx_idx]); ++__ctx_idx)
#define for_each_ctx(__ctx, __wrk) \
__for_each_ctx(__ctx, __wrk, igt_unique(__ctx_idx))
/* igt_unique(idx) is same on both lines as macro when expanded comes out on one line */
#define for_each_w_step(__w_step, __wrk) \
for (typeof(__wrk->nr_steps) igt_unique(idx) = ({__w_step = __wrk->steps; 0; }); \
igt_unique(idx) < __wrk->nr_steps; igt_unique(idx)++, __w_step++)
static unsigned int master_prng;
static int verbose = 1;
static int fd;
static bool is_xe;
static struct drm_i915_gem_context_param_sseu device_sseu = {
.slice_mask = -1 /* Force read on first use. */
};
#define FLAG_SYNCEDCLIENTS (1<<1)
#define FLAG_DEPSYNC (1<<2)
#define FLAG_SSEU (1<<3)
static void w_step_sync(struct w_step *w)
{
if (is_xe)
igt_assert(syncobj_wait(fd, &w->xe.syncs[0].handle, 1, INT64_MAX, 0, NULL));
else
gem_sync(fd, w->i915.obj[0].handle);
}
static int read_timestamp_frequency(int i915)
{
int value = 0;
drm_i915_getparam_t gp = {
.value = &value,
.param = I915_PARAM_CS_TIMESTAMP_FREQUENCY,
};
ioctl(i915, DRM_IOCTL_I915_GETPARAM, &gp);
return value;
}
static uint64_t div64_u64_round_up(uint64_t x, uint64_t y)
{
return (x + y - 1) / y;
}
static uint64_t ns_to_ctx_ticks(uint64_t ns)
{
static long f;
if (!f) {
f = read_timestamp_frequency(fd);
if (intel_gen(intel_get_drm_devid(fd)) == 11)
f = 12500000; /* icl!!! are you feeling alright? */
}
return div64_u64_round_up(ns * f, NSEC_PER_SEC);
}
#define MI_STORE_DWORD_INDEX MI_INSTR(0x21, 1)
static unsigned int offset_in_page(void *addr)
{
return (uintptr_t)addr & 4095;
}
static void add_dep(struct deps *deps, struct dep_entry entry)
{
deps->list = realloc(deps->list, sizeof(*deps->list) * (deps->nr + 1));
igt_assert(deps->list);
deps->list[deps->nr++] = entry;
}
static int
parse_working_set_deps(struct workload *wrk,
struct deps *deps,
struct dep_entry _entry,
char *str)
{
/*
* 1 - target handle index in the specified working set.
* 2-4 - range
*/
struct dep_entry entry = _entry;
char *s;
s = index(str, '-');
if (s) {
int from, to;
from = atoi(str);
if (from < 0)
return -1;
to = atoi(++s);
if (to <= 0)
return -1;
if (to <= from)
return -1;
for (entry.target = from; entry.target <= to; entry.target++)
add_dep(deps, entry);
} else {
entry.target = atoi(str);
if (entry.target < 0)
return -1;
add_dep(deps, entry);
}
return 0;
}
static void __attribute__((format(printf, 1, 2)))
wsim_err(const char *fmt, ...)
{
va_list ap;
if (!verbose)
return;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static int
parse_dependency(unsigned int nr_steps, struct w_step *w, char *str)
{
struct dep_entry entry = { .working_set = -1 };
bool submit_fence = false;
char *s;
switch (str[0]) {
case '-':
if (str[1] < '0' || str[1] > '9')
return -1;
entry.target = atoi(str);
if (entry.target > 0 || ((int)nr_steps + entry.target) < 0)
return -1;
add_dep(&w->data_deps, entry);
break;
case 's':
/* no submit fence in xe */
if (is_xe) {
wsim_err("Submit fences are not supported with xe\n");
return -1;
}
submit_fence = true;
/* Fall-through. */
case 'f':
/* xe supports multiple fences */
if (!is_xe)
/* Multiple fences not yet supported. */
igt_assert_eq(w->fence_deps.nr, 0);
entry.target = atoi(++str);
if (entry.target > 0 || ((int)nr_steps + entry.target) < 0)
return -1;
add_dep(&w->fence_deps, entry);
w->fence_deps.submit_fence = submit_fence;
break;
case 'w':
entry.write = true;
/* Fall-through. */
case 'r':
/*
* [rw]N-<str>
* r1-<str> or w2-<str>, where N is working set id.
*/
s = index(++str, '-');
if (!s)
return -1;
entry.working_set = atoi(str);
if (entry.working_set < 0)
return -1;
if (parse_working_set_deps(w->wrk, &w->data_deps, entry, ++s))
return -1;
break;
default:
return -1;
};
return 0;
}
static int
parse_dependencies(unsigned int nr_steps, struct w_step *w, char *_desc)
{
char *desc = strdup(_desc);
char *token, *tctx = NULL, *tstart = desc;
int ret = 0;
/*
* Skip when no dependencies to avoid having to detect
* non-sensical "0/0/..." below.
*/
if (!strcmp(_desc, "0"))
goto out;
igt_assert(desc);
igt_assert(!w->data_deps.nr && w->data_deps.nr == w->fence_deps.nr);
igt_assert(!w->data_deps.list &&
w->data_deps.list == w->fence_deps.list);
while ((token = strtok_r(tstart, "/", &tctx)) != NULL) {
tstart = NULL;
ret = parse_dependency(nr_steps, w, token);
if (ret)
break;
}
out:
free(desc);
return ret;
}
#define check_arg(cond, fmt, ...) \
{ \
if (cond) { \
wsim_err(fmt, __VA_ARGS__); \
return NULL; \
} \
}
/* engine_class[<engine_instance>-<gt_id>] */
static intel_engine_t str_to_engine(const char *str)
{
intel_engine_t e = {INVALID_ID, DEFAULT_ID, DEFAULT_ID};
size_t pos;
if (!strcasecmp("DEFAULT", str)) {
e.engine_class = DEFAULT_ID;
return e;
} else if (!strncasecmp("RCS", str, 3)) {
e.engine_class = RCS;
pos = 3;
} else if (!strncasecmp("BCS", str, 3)) {
e.engine_class = BCS;
pos = 3;
} else if (!strncasecmp("VCS", str, 3)) {
e.engine_class = VCS;
pos = 3;
} else if (!strncasecmp("VECS", str, 4)) {
e.engine_class = VECS;
pos = 4;
} else if (!strncasecmp("CCS", str, 3)) {
e.engine_class = CCS;
pos = 3;
} else {
return (intel_engine_t){INVALID_ID};
}
if (str[pos]) {
char *s = strchr(&str[pos], '-');
char *endptr = NULL;
long id;
if (!s || (s && *s != str[pos])) {
id = strtol(&str[pos], &endptr, 10);
if (endptr == &str[pos] || id < 1 || id >= INVALID_ID)
return (intel_engine_t){INVALID_ID};
e.engine_instance = id - 1;
}
if (s && *(++s)) {
id = strtol(s, &endptr, 10);
if (endptr == s || id < 0 || id >= INVALID_ID)
return (intel_engine_t){INVALID_ID};
e.gt_id = id;
}
if (endptr && endptr != (str + strlen(str)))
return (intel_engine_t){INVALID_ID};
}
return e;
}
static struct i915_engine_class_instance
engine_to_i915_engine_class(const intel_engine_t *engine)
{
return (struct i915_engine_class_instance){ engine->engine_class,
engine->engine_instance };
}
static unsigned int
engine_to_i915_legacy_ring(const intel_engine_t *engine)
{
switch (engine->engine_class) {
case DEFAULT_ID:
return I915_EXEC_DEFAULT;
case RCS:
return I915_EXEC_RENDER;
case BCS:
return I915_EXEC_BLT;
case VCS:
if (engine->engine_instance == DEFAULT_ID)
return I915_EXEC_BSD;
else if (engine->engine_instance == 0)
return I915_EXEC_BSD | I915_EXEC_BSD_RING1;
else if (engine->engine_instance == 1)
return I915_EXEC_BSD | I915_EXEC_BSD_RING2;
break;
case VECS:
return I915_EXEC_VEBOX;
};
igt_assert(0);
}
static bool are_equal_engines(const intel_engine_t *e1,
const intel_engine_t *e2)
{
return e1->engine_class == e2->engine_class &&
e1->engine_instance == e2->engine_instance &&
e1->gt_id == e2->gt_id;
}
static bool find_engine_in_map(const intel_engine_t *engine,
struct intel_engines *engines, unsigned int *idx)
{
igt_assert(idx);
for (unsigned int i = 0; i < engines->nr_engines; ++i)
if (are_equal_engines(engine, &engines->engines[i])) {
*idx = i;
return true;
}
return false;
}
static struct intel_engines *query_engines(void)
{
static struct intel_engines engines = {};
if (engines.nr_engines)
return &engines;
if (is_xe) {
struct drm_xe_engine_class_instance *hwe;
engines.engines = calloc(xe_number_engines(fd), sizeof(intel_engine_t));
igt_assert(engines.engines);
engines.nr_engines = 0;
xe_for_each_engine(fd, hwe)
engines.engines[engines.nr_engines++] = *hwe;
igt_assert(engines.nr_engines);
} else {
struct intel_engine_data ed = {};
ed = intel_engine_list_of_physical(fd);
igt_assert(ed.nengines);
engines.nr_engines = ed.nengines;
engines.engines = calloc(engines.nr_engines, sizeof(intel_engine_t));
igt_assert(engines.engines);
for (int i = 0; i < ed.nengines; ++i) {
engines.engines[i].engine_class = ed.engines[i].class;
engines.engines[i].engine_instance = ed.engines[i].instance;
engines.engines[i].gt_id = DEFAULT_ID;
}
}
return &engines;
}
static bool is_valid_engine(const intel_engine_t *engine)
{
return engine->engine_class != INVALID_ID;
}
static bool is_default_engine(const intel_engine_t *engine)
{
return engine->engine_class == DEFAULT_ID &&
engine->engine_instance == DEFAULT_ID &&
engine->gt_id == DEFAULT_ID;
}
static bool engine_matches_filter(const intel_engine_t *engine, const intel_engine_t *filter)
{
return (filter->engine_class == DEFAULT_ID ||
filter->engine_class == engine->engine_class) &&
(filter->engine_instance == DEFAULT_ID ||
filter->engine_instance == engine->engine_instance) &&
(filter->gt_id == DEFAULT_ID ||
filter->gt_id == engine->gt_id);
}
#define for_each_matching_engine(__engine, __filter, __engines) \
for (unsigned int __i = 0; __i < (__engines)->nr_engines && \
((__engine) = &(__engines)->engines[__i]); __i++) \
for_if(engine_matches_filter((__engine), (__filter)))
static unsigned int
append_matching_engines(const intel_engine_t *filter, struct intel_engines *engines)
{
unsigned int prev_nr_engines;
struct intel_engines *all = query_engines();
intel_engine_t *engine;
igt_assert(engines);
prev_nr_engines = engines->nr_engines;
for_each_matching_engine(engine, filter, all) {
engines->nr_engines++;
engines->engines = realloc(engines->engines,
engines->nr_engines * sizeof(intel_engine_t));
igt_assert(engines->engines);
engines->engines[engines->nr_engines - 1] = *engine;
}
return engines->nr_engines - prev_nr_engines;
}
static intel_engine_t get_default_engine(void)
{
struct intel_engines *all_engines = query_engines();
const intel_engine_t filters[] = {
{RCS, DEFAULT_ID, DEFAULT_ID},
{CCS, DEFAULT_ID, DEFAULT_ID},
{DEFAULT_ID, DEFAULT_ID, DEFAULT_ID},
{INVALID_ID}
}, *filter, *default_engine;
for (filter = filters; is_valid_engine(filter); filter++)
for_each_matching_engine(default_engine, filter, all_engines)
return *default_engine;
igt_assert(0);
}
static intel_engine_t resolve_to_physical_engine_(const intel_engine_t *engine)
{
struct intel_engines *all_engines = query_engines();
intel_engine_t *resolved;
igt_assert(engine);
if (is_default_engine(engine))
return get_default_engine();
for_each_matching_engine(resolved, engine, all_engines)
return *resolved;
return (intel_engine_t){INVALID_ID};
}
static void resolve_to_physical_engine(intel_engine_t *engine)
{
*engine = resolve_to_physical_engine_(engine);
igt_assert(is_valid_engine(engine));
}
static int parse_engine_map(struct w_step *step, const char *_str)
{
char *token, *tctx = NULL, *tstart = (char *)_str;
intel_engine_t engine;
while ((token = strtok_r(tstart, "|", &tctx))) {
tstart = NULL;
engine = str_to_engine(token);
if (!is_valid_engine(&engine) || is_default_engine(&engine))
return -1;
if (!append_matching_engines(&engine, &step->engine_map))
return -1;
}
return 0;
}
static int parse_bond_engines(struct w_step *step, const char *_str)
{
char *token, *tctx = NULL, *tstart = (char *)_str;
intel_engine_t engine;
while ((token = strtok_r(tstart, "|", &tctx))) {
tstart = NULL;
engine = str_to_engine(token);
if (append_matching_engines(&engine, &step->bond.mask) != 1)
return -1;
}
return 0;
}
static unsigned long parse_size(char *str)
{
const unsigned int len = strlen(str);
unsigned int mult = 1;
long val;
/* "1234567890[gGmMkK]" */
if (len == 0)
return 0;
switch (str[len - 1]) {
case 'g':
case 'G':
mult *= 1024;
/* Fall-throuogh. */
case 'm':
case 'M':
mult *= 1024;
/* Fall-throuogh. */
case 'k':
case 'K':
mult *= 1024;
str[len - 1] = 0;
/* Fall-throuogh. */
case '0' ... '9':
break;
default:
return 0; /* Unrecognized non-digit. */
}
val = atol(str);
if (val <= 0)
return 0;
return val * mult;
}
static int add_buffers(struct working_set *set, char *str)
{
/*
* 4096
* 4k
* 4m
* 4g
* 10n4k - 10 4k batches
* 4096-16k - random size in range
*/
struct work_buffer_size *sizes;
unsigned long min_sz, max_sz;
char *n, *max = NULL;
int add, i;
n = index(str, 'n');
if (n) {
*n = 0;
add = atoi(str);
if (add <= 0)
return -1;
str = ++n;
} else {
add = 1;
}
n = index(str, '-');
if (n) {
*n = 0;
max = ++n;
}
min_sz = parse_size(str);
if (!min_sz)
return -1;
if (max) {
max_sz = parse_size(max);
if (!max_sz)
return -1;
} else {
max_sz = min_sz;
}
sizes = realloc(set->sizes, (set->nr + add) * sizeof(*sizes));
if (!sizes)
return -1;
for (i = 0; i < add; i++) {
struct work_buffer_size *sz = &sizes[set->nr + i];
sz->min = min_sz;
sz->max = max_sz;
sz->size = 0;
}
set->nr += add;
set->sizes = sizes;
return 0;
}
static int parse_working_set(struct working_set *set, char *str)
{
char *token, *tctx = NULL, *tstart = str;
while ((token = strtok_r(tstart, "/", &tctx))) {
tstart = NULL;
if (add_buffers(set, token))
return -1;
}
return 0;
}
static unsigned long
allocate_working_set(struct workload *wrk, struct working_set *set);
static long __duration(long dur, double scale)
{
return round(scale * dur);
}
static int
parse_duration(unsigned int nr_steps, struct duration *dur, double scale_dur, char *field)
{
char *sep = NULL;
long tmpl;
if (field[0] == '*') {
if (intel_gen(intel_get_drm_devid(fd)) < 8) {
wsim_err("Infinite batch at step %u needs Gen8+!\n", nr_steps);
return -1;
}
dur->unbound = true;
} else {
tmpl = strtol(field, &sep, 10);
if (tmpl <= 0 || tmpl == LONG_MIN || tmpl == LONG_MAX) {
wsim_err("Invalid duration at step %u!\n", nr_steps);
return -1;
}
dur->min = __duration(tmpl, scale_dur);
if (sep && *sep == '-') {
tmpl = strtol(sep + 1, NULL, 10);
if (tmpl <= 0 || __duration(tmpl, scale_dur) <= dur->min ||
tmpl == LONG_MIN || tmpl == LONG_MAX) {
wsim_err("Invalid maximum duration at step %u!\n", nr_steps);
return -1;
}
dur->max = __duration(tmpl, scale_dur);
} else {
dur->max = dur->min;
}
}
return 0;
}
#define int_field(_STEP_, _FIELD_, _COND_, _ERR_) \
do { \
field = strtok_r(fstart, ".", &fctx); \
if (field) { \
tmp = atoi(field); \
check_arg(_COND_, _ERR_, nr_steps); \
step.type = _STEP_; \
step._FIELD_ = tmp; \
goto add_step; \
} \
} while (0)
static struct workload *
parse_workload(struct w_arg *arg, unsigned int flags, double scale_dur,
double scale_time, struct workload *app_w)
{
struct workload *wrk;
unsigned int nr_steps = 0;
char *desc = strdup(arg->desc);
char *_token, *token, *tctx = NULL, *tstart = desc;
char *field, *fctx = NULL, *fstart;
struct w_step step, *w, *steps = NULL;
unsigned int valid;
int i, j, tmp;
igt_assert(desc);
while ((_token = strtok_r(tstart, ",", &tctx))) {
tstart = NULL;
token = strdup(_token);
igt_assert(token);
fstart = token;
valid = 0;
memset(&step, 0, sizeof(step));
field = strtok_r(fstart, ".", &fctx);
if (field) {
fstart = NULL;
/* line starting with # is a comment */
if (field[0] == '#') {
if (verbose > 3)
printf("skipped line: %s\n", _token);
free(token);
continue;
}
if (!strcmp(field, "d")) {
int_field(DELAY, delay, tmp <= 0,
"Invalid delay at step %u!\n");
} else if (!strcmp(field, "p")) {
int_field(PERIOD, period, tmp <= 0,
"Invalid period at step %u!\n");
} else if (!strcmp(field, "P")) {
unsigned int nr = 0;
if (is_xe) {
wsim_err("Priority step is not implemented with xe yet.\n");
free(token);
return NULL;
}
while ((field = strtok_r(fstart, ".", &fctx))) {
tmp = atoi(field);
check_arg(nr == 0 && tmp <= 0,
"Invalid context at step %u!\n",
nr_steps);
check_arg(nr > 1,
"Invalid priority format at step %u!\n",
nr_steps);
if (nr == 0)
step.context = tmp;
else
step.priority = tmp;
nr++;
}
step.type = CTX_PRIORITY;
goto add_step;
} else if (!strcmp(field, "s")) {
int_field(SYNC, target,
tmp >= 0 || ((int)nr_steps + tmp) < 0,
"Invalid sync target at step %u!\n");
} else if (!strcmp(field, "S")) {
unsigned int nr = 0;
if (is_xe) {
wsim_err("SSEU step is not implemented with xe yet.\n");
free(token);
return NULL;
}
while ((field = strtok_r(fstart, ".", &fctx))) {
tmp = atoi(field);
check_arg(tmp <= 0 && nr == 0,
"Invalid context at step %u!\n",
nr_steps);
check_arg(nr > 1,
"Invalid SSEU format at step %u!\n",
nr_steps);
if (nr == 0)
step.context = tmp;
else if (nr == 1)
step.sseu = tmp;
nr++;
}
step.type = SSEU;
goto add_step;
} else if (!strcmp(field, "t")) {
int_field(THROTTLE, throttle,
tmp < 0,
"Invalid throttle at step %u!\n");
} else if (!strcmp(field, "q")) {
int_field(QD_THROTTLE, throttle,
tmp < 0,
"Invalid qd throttle at step %u!\n");
} else if (!strcmp(field, "a")) {
int_field(SW_FENCE_SIGNAL, target,
tmp >= 0,
"Invalid sw fence signal at step %u!\n");
} else if (!strcmp(field, "f")) {
step.type = SW_FENCE;
goto add_step;
} else if (!strcmp(field, "M")) {
unsigned int nr = 0;
while ((field = strtok_r(fstart, ".", &fctx))) {
tmp = atoi(field);
check_arg(nr == 0 && tmp <= 0,
"Invalid context at step %u!\n",
nr_steps);
check_arg(nr > 1,
"Invalid engine map format at step %u!\n",
nr_steps);
if (nr == 0) {
step.context = tmp;
} else {
tmp = parse_engine_map(&step,
field);
check_arg(tmp < 0,
"Invalid engine map list at step %u!\n",
nr_steps);
}
nr++;
}
step.type = ENGINE_MAP;
goto add_step;
} else if (!strcmp(field, "T")) {
int_field(TERMINATE, target,
tmp >= 0 || ((int)nr_steps + tmp) < 0,
"Invalid terminate target at step %u!\n");
} else if (!strcmp(field, "X")) {
unsigned int nr = 0;
while ((field = strtok_r(fstart, ".", &fctx))) {
tmp = atoi(field);
check_arg(nr == 0 && tmp <= 0,
"Invalid context at step %u!\n",
nr_steps);
check_arg(nr == 1 && tmp < 0,
"Invalid preemption period at step %u!\n",
nr_steps);
check_arg(nr > 1,
"Invalid preemption format at step %u!\n",
nr_steps);
if (nr == 0)
step.context = tmp;
else
step.period = tmp;
nr++;
}
step.type = PREEMPTION;
goto add_step;
} else if (!strcmp(field, "B")) {
unsigned int nr = 0;
while ((field = strtok_r(fstart, ".", &fctx))) {
tmp = atoi(field);
check_arg(nr == 0 && tmp <= 0,
"Invalid context at step %u!\n",
nr_steps);
check_arg(nr > 0,
"Invalid load balance format at step %u!\n",
nr_steps);
step.context = tmp;
step.load_balance = true;
nr++;
}
step.type = LOAD_BALANCE;
goto add_step;
} else if (!strcmp(field, "b")) {
unsigned int nr = 0;
if (is_xe) {
wsim_err("Bonding is not implemented with xe yet.\n");
free(token);
return NULL;
}
while ((field = strtok_r(fstart, ".", &fctx))) {
check_arg(nr > 2,
"Invalid bond format at step %u!\n",
nr_steps);
if (nr == 0) {
tmp = atoi(field);
step.context = tmp;
check_arg(tmp <= 0,
"Invalid context at step %u!\n",
nr_steps);
} else if (nr == 1) {
tmp = parse_bond_engines(&step, field);
check_arg(tmp < 0,
"Invalid siblings list at step %u!\n",
nr_steps);
} else if (nr == 2) {
struct intel_engines engines;
step.bond.master = str_to_engine(field);
check_arg(append_matching_engines(&step.bond.master,
&engines) != 1,
"Invalid master engine at step %u!\n",
nr_steps);
free(engines.engines);
}
nr++;
}
step.type = BOND;
goto add_step;
} else if (!strcmp(field, "w") || !strcmp(field, "W")) {
unsigned int nr = 0;
if (is_xe) {
wsim_err("Working sets are not implemented with xe yet.\n");
free(token);
return NULL;
}
step.working_set.shared = field[0] == 'W';
while ((field = strtok_r(fstart, ".", &fctx))) {
tmp = atoi(field);
if (nr == 0) {
step.working_set.id = tmp;
} else {
tmp = parse_working_set(&step.working_set,
field);
check_arg(tmp < 0,
"Invalid working set at step %u!\n",
nr_steps);
}
nr++;
}
step.type = WORKINGSET;
goto add_step;
}
if (!field) {
if (verbose)
fprintf(stderr,
"Parse error at step %u!\n",
nr_steps);
return NULL;
}
tmp = atoi(field);
check_arg(tmp < 0, "Invalid ctx id at step %u!\n",
nr_steps);
step.context = tmp;
valid++;
}
field = strtok_r(fstart, ".", &fctx);
if (field) {
fstart = NULL;
step.engine = str_to_engine(field);
check_arg(!is_valid_engine(&step.engine),
"Invalid engine id at step %u!\n", nr_steps);
valid++;
}
field = strtok_r(fstart, ".", &fctx);
if (field) {
fstart = NULL;
if (parse_duration(nr_steps, &step.duration, scale_dur, field))
return NULL;
valid++;
}
field = strtok_r(fstart, ".", &fctx);
if (field) {
fstart = NULL;
tmp = parse_dependencies(nr_steps, &step, field);
check_arg(tmp < 0,
"Invalid dependency at step %u!\n", nr_steps);
valid++;
}
field = strtok_r(fstart, ".", &fctx);
if (field) {
fstart = NULL;
check_arg(strlen(field) != 1 ||
(field[0] != '0' && field[0] != '1'),
"Invalid wait boolean at step %u!\n",
nr_steps);
step.sync = field[0] - '0';
valid++;
}
check_arg(valid != 5, "Invalid record at step %u!\n", nr_steps);
step.type = BATCH;
add_step:
if (step.type == DELAY)
step.delay = __duration(step.delay, scale_time);
step.idx = nr_steps++;
step.rq_link.next = NULL;
step.rq_link.prev = NULL;
steps = realloc(steps, sizeof(step) * nr_steps);
igt_assert(steps);
memcpy(&steps[nr_steps - 1], &step, sizeof(step));
free(token);
}
if (app_w) {
steps = realloc(steps, sizeof(step) *
(nr_steps + app_w->nr_steps));
igt_assert(steps);
memcpy(&steps[nr_steps], app_w->steps,
sizeof(step) * app_w->nr_steps);
for (i = 0; i < app_w->nr_steps; i++)
steps[nr_steps + i].idx += nr_steps;
nr_steps += app_w->nr_steps;
}
wrk = malloc(sizeof(*wrk));
igt_assert(wrk);
wrk->nr_steps = nr_steps;
wrk->steps = steps;
wrk->prio = arg->prio;
wrk->sseu = arg->sseu;
wrk->max_working_set_id = -1;
wrk->working_sets = NULL;
wrk->bo_prng = (flags & FLAG_SYNCEDCLIENTS) ? master_prng : rand();
free(desc);
/*
* Tag all steps which need to emit a sync fence if another step is
* referencing them as a sync fence dependency.
*/
for (i = 0; i < nr_steps; i++) {
struct dep_entry *dep;
for_each_dep(dep, steps[i].fence_deps) {
tmp = steps[i].idx + dep->target;
check_arg(tmp < 0 || tmp >= i ||
(steps[tmp].type != BATCH &&
steps[tmp].type != SW_FENCE),
"Invalid dependency target %u!\n", i);
steps[tmp].emit_fence = -1;
}
}
/* Validate SW_FENCE_SIGNAL targets. */
for (i = 0; i < nr_steps; i++) {
if (steps[i].type == SW_FENCE_SIGNAL) {
tmp = steps[i].idx + steps[i].target;
check_arg(tmp < 0 || tmp >= i ||
steps[tmp].type != SW_FENCE,
"Invalid sw fence target %u!\n", i);
}
}
/*
* Check no duplicate working set ids.
*/
for_each_w_step(w, wrk) {
struct w_step *w2;
if (w->type != WORKINGSET)
continue;
for_each_w_step(w2, wrk) {
if (w->idx == w2->idx)
continue;
if (w2->type != WORKINGSET)
continue;
check_arg(w->working_set.id == w2->working_set.id,
"Duplicate working set id at %u!\n", j);
}
}
/*
* Allocate shared working sets.
*/
for_each_w_step(w, wrk) {
if (w->type == WORKINGSET && w->working_set.shared) {
unsigned long total =
allocate_working_set(wrk, &w->working_set);
if (verbose > 1)
printf("%u: %lu bytes in shared working set %u\n",
wrk->id, total, w->working_set.id);
}
}
wrk->max_working_set_id = -1;
for_each_w_step(w, wrk) {
if (w->type == WORKINGSET &&
w->working_set.shared &&
w->working_set.id > wrk->max_working_set_id)
wrk->max_working_set_id = w->working_set.id;
}
wrk->working_sets = calloc(wrk->max_working_set_id + 1,
sizeof(*wrk->working_sets));
igt_assert(wrk->working_sets);
for_each_w_step(w, wrk) {
if (w->type == WORKINGSET && w->working_set.shared)
wrk->working_sets[w->working_set.id] = &w->working_set;
}
return wrk;
}
static struct workload *
clone_workload(struct workload *_wrk)
{
int nr_engines = query_engines()->nr_engines;
struct workload *wrk;
struct w_step *w;
wrk = malloc(sizeof(*wrk));
igt_assert(wrk);
memset(wrk, 0, sizeof(*wrk));
wrk->prio = _wrk->prio;
wrk->sseu = _wrk->sseu;
wrk->nr_steps = _wrk->nr_steps;
wrk->steps = calloc(wrk->nr_steps, sizeof(struct w_step));
igt_assert(wrk->steps);
memcpy(wrk->steps, _wrk->steps, sizeof(struct w_step) * wrk->nr_steps);
wrk->max_working_set_id = _wrk->max_working_set_id;
if (wrk->max_working_set_id >= 0) {
wrk->working_sets = calloc(wrk->max_working_set_id + 1,
sizeof(*wrk->working_sets));
igt_assert(wrk->working_sets);
memcpy(wrk->working_sets,
_wrk->working_sets,
(wrk->max_working_set_id + 1) *
sizeof(*wrk->working_sets));
}
/* Check if we need a sw sync timeline. */
for_each_w_step(w, wrk) {
if (w->type == SW_FENCE) {
wrk->sync_timeline = sw_sync_timeline_create();
igt_assert(wrk->sync_timeline >= 0);
break;
}
}
wrk->requests = calloc(nr_engines, sizeof(*wrk->requests));
igt_assert(wrk->requests);
wrk->nrequest = calloc(nr_engines, sizeof(*wrk->nrequest));
igt_assert(wrk->nrequest);
while (--nr_engines >= 0)
IGT_INIT_LIST_HEAD(&wrk->requests[nr_engines]);
return wrk;
}
#define rounddown(x, y) (x - (x%y))
#ifndef PAGE_SIZE
#define PAGE_SIZE (4096)
#endif
static unsigned int get_duration(struct workload *wrk, struct w_step *w)
{
struct duration *dur = &w->duration;
if (dur->min == dur->max)
return dur->min;
else
return dur->min + hars_petruska_f54_1_random(&wrk->bb_prng) %
(dur->max + 1 - dur->min);
}
static struct ctx *
__get_ctx(struct workload *wrk, const struct w_step *w)
{
return &wrk->ctx_list[w->context];
}
static uint32_t mmio_base(int i915, const intel_engine_t *engine, int gen)
{
char name[16];
if (gen >= 11)
return 0;
switch (engine->engine_class) {
default:
return 0;
case DEFAULT_ID:
case RCS:
snprintf(name, sizeof(name), "rcs%u", engine->engine_instance);
break;
case BCS:
snprintf(name, sizeof(name), "bcs%u", engine->engine_instance);
break;
case VCS:
snprintf(name, sizeof(name), "vcs%u", engine->engine_instance);
break;
case VECS:
snprintf(name, sizeof(name), "vecs%u", engine->engine_instance);
break;
case CCS:
snprintf(name, sizeof(name), "ccs%u", engine->engine_instance);
break;
}
return gem_engine_mmio_base(i915, name);
}
static unsigned int create_bb(struct w_step *w, int self)
{
const int gen = intel_gen(intel_get_drm_devid(fd));
const uint32_t base = mmio_base(fd, &w->engine, gen);
#define CS_GPR(x) (base + 0x600 + 8 * (x))
#define TIMESTAMP (base + 0x3a8)
const int use_64b = gen >= 8;
enum { START_TS, NOW_TS };
uint32_t *cs, *jmp;
unsigned int r = 0;
/* Loop until CTX_TIMESTAMP - initial > target ns */
gem_set_domain(fd, w->bb_handle,
I915_GEM_DOMAIN_WC, I915_GEM_DOMAIN_WC);
if (__gem_set_caching(fd, w->bb_handle, I915_CACHING_CACHED) == 0) {
cs = gem_mmap__cpu(fd, w->bb_handle,
0, w->bb_size,
PROT_READ | PROT_WRITE);
} else {
cs = gem_mmap__device_coherent(fd,
w->bb_handle,
0, w->bb_size,
PROT_READ | PROT_WRITE);
}
/* Store initial 64b timestamp: start */
*cs++ = MI_LOAD_REGISTER_IMM(1) | MI_CS_MMIO_DST;
*cs++ = CS_GPR(START_TS) + 4;
*cs++ = 0;
*cs++ = MI_LOAD_REGISTER_REG | MI_CS_MMIO_DST | MI_CS_MMIO_SRC;
*cs++ = TIMESTAMP;
*cs++ = CS_GPR(START_TS);
if (offset_in_page(cs) & 4)
*cs++ = 0;
jmp = cs;
if (w->preempt_us) /* Not precise! */
*cs++ = MI_ARB_CHECK;
/* Store this 64b timestamp: now */
*cs++ = MI_LOAD_REGISTER_IMM(1) | MI_CS_MMIO_DST;
*cs++ = CS_GPR(NOW_TS) + 4;
*cs++ = 0;
*cs++ = MI_LOAD_REGISTER_REG | MI_CS_MMIO_DST | MI_CS_MMIO_SRC;
*cs++ = TIMESTAMP;
*cs++ = CS_GPR(NOW_TS);
/* delta = now - start; inverted to match COND_BBE */
*cs++ = MI_MATH(4);
*cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(NOW_TS));
*cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(START_TS));
*cs++ = MI_MATH_SUB;
*cs++ = MI_MATH_STOREINV(MI_MATH_REG(NOW_TS), MI_MATH_REG_ACCU);
/* Save delta for indirect read by COND_BBE */
*cs++ = MI_STORE_REGISTER_MEM_CMD | (1 + use_64b) | MI_CS_MMIO_DST;
*cs++ = CS_GPR(NOW_TS);
w->i915.reloc[r].presumed_offset = w->i915.obj[self].offset;
w->i915.reloc[r].target_handle = self;
w->i915.reloc[r].offset = offset_in_page(cs);
w->i915.reloc[r].delta = 4000;
*cs++ = w->i915.reloc[r].presumed_offset + w->i915.reloc[r].delta;
*cs++ = 0;
r++;
/* Delay between SRM and COND_BBE to post the writes */
for (int n = 0; n < 8; n++) {
*cs++ = MI_STORE_DWORD_INDEX;
*cs++ = 2048; /* offset into ppHWSP */
*cs++ = 0;
}
/* Break if delta [time elapsed] > target ns (target filled in later) */
*cs++ = MI_COND_BATCH_BUFFER_END | MI_DO_COMPARE | (1 + use_64b);
w->i915.bb_duration = cs;
*cs++ = 0;
w->i915.reloc[r].presumed_offset = w->i915.obj[self].offset;
w->i915.reloc[r].target_handle = self;
w->i915.reloc[r].offset = offset_in_page(cs);
w->i915.reloc[r].delta = 4000;
*cs++ = w->i915.reloc[r].presumed_offset + w->i915.reloc[r].delta;
*cs++ = 0;
r++;
/* Otherwise back to recalculating delta */
*cs++ = MI_BATCH_BUFFER_START | 1 << 8 | use_64b;
w->i915.reloc[r].presumed_offset = w->i915.obj[self].offset;
w->i915.reloc[r].target_handle = self;
w->i915.reloc[r].offset = offset_in_page(cs);
w->i915.reloc[r].delta = offset_in_page(jmp);
*cs++ = w->i915.reloc[r].presumed_offset + w->i915.reloc[r].delta;
*cs++ = 0;
r++;
/* returns still mmapped for w->bb_duration to be filled in later */
return r;
}
static void
eb_update_flags(struct workload *wrk, struct w_step *w)
{
w->i915.eb.flags = w->engine_idx;
w->i915.eb.flags |= I915_EXEC_HANDLE_LUT;
w->i915.eb.flags |= I915_EXEC_NO_RELOC;
igt_assert(w->emit_fence <= 0);
if (w->emit_fence)
w->i915.eb.flags |= I915_EXEC_FENCE_OUT;
}
static uint32_t
get_ctxid(struct workload *wrk, struct w_step *w)
{
return wrk->ctx_list[w->context].id;
}
static struct xe_exec_queue *
xe_get_eq(struct workload *wrk, const struct w_step *w)
{
struct ctx *ctx = __get_ctx(wrk, w);
igt_assert_lt(w->engine_idx, ctx->xe.nr_queues);
return &ctx->xe.queue_list[w->engine_idx];
}
static struct vm *
get_vm(struct workload *wrk, const struct w_step *w)
{
return wrk->vm_list;
}
static uint32_t alloc_bo(int i915, unsigned long *size)
{
uint32_t handle;
uint64_t sz = *size;
igt_assert_eq(__gem_create(i915, &sz, &handle), 0);
igt_assert(sz <= ULONG_MAX);
*size = sz;
return handle;
}
static void
alloc_step_batch(struct workload *wrk, struct w_step *w)
{
struct dep_entry *dep;
unsigned int j = 0;
unsigned int nr_obj = 2 + w->data_deps.nr;
unsigned int objflags = 0;
uint64_t addr;
struct vm *vm = get_vm(wrk, w);
addr = gem_aperture_size(fd) / 2;
if (addr >> 32)
objflags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
if (vm->ahnd)
objflags |= EXEC_OBJECT_PINNED;
w->i915.obj = calloc(nr_obj, sizeof(*w->i915.obj));
igt_assert(w->i915.obj);
w->bb_size = PAGE_SIZE;
w->i915.obj[j].handle = alloc_bo(fd, &w->bb_size);
w->i915.obj[j].flags = EXEC_OBJECT_WRITE;
if (vm->ahnd) {
addr = get_offset(vm->ahnd, w->i915.obj[j].handle, w->bb_size, 0);
w->i915.obj[j].offset = CANONICAL(addr);
w->i915.obj[j].flags |= objflags;
}
j++;
igt_assert(j < nr_obj);
for_each_dep(dep, w->data_deps) {
uint32_t dep_handle;
uint64_t dep_size;
if (dep->working_set == -1) {
int dep_idx = w->idx + dep->target;
igt_assert(dep->target <= 0);
igt_assert(dep_idx >= 0 && dep_idx < w->idx);
igt_assert(wrk->steps[dep_idx].type == BATCH);
dep_handle = wrk->steps[dep_idx].i915.obj[0].handle;
dep_size = w->bb_size;
} else {
struct working_set *set;
igt_assert(dep->working_set <=
wrk->max_working_set_id);
set = wrk->working_sets[dep->working_set];
igt_assert(set->nr);
igt_assert(dep->target < set->nr);
igt_assert(set->sizes[dep->target].size);
dep_handle = set->handles[dep->target];
dep_size = set->sizes[dep->target].size;
}
w->i915.obj[j].flags = dep->write ? EXEC_OBJECT_WRITE : 0;
w->i915.obj[j].handle = dep_handle;
if (vm->ahnd) {
addr = get_offset(vm->ahnd, w->i915.obj[j].handle, dep_size, 0);
w->i915.obj[j].offset = CANONICAL(addr);
w->i915.obj[j].flags |= objflags;
}
j++;
igt_assert(j < nr_obj);
}
w->bb_handle = w->i915.obj[j].handle = alloc_bo(fd, &w->bb_size);
if (vm->ahnd) {
addr = get_offset(vm->ahnd, w->i915.obj[j].handle, w->bb_size, 0);
w->i915.obj[j].offset = CANONICAL(addr);
w->i915.obj[j].flags |= objflags;
}
w->i915.obj[j].relocation_count = create_bb(w, j);
if (vm->ahnd) {
w->i915.obj[j].relocation_count = 0;
} else {
igt_assert(w->i915.obj[j].relocation_count <= ARRAY_SIZE(w->i915.reloc));
w->i915.obj[j].relocs_ptr = to_user_pointer(&w->i915.reloc);
}
w->i915.eb.buffers_ptr = to_user_pointer(w->i915.obj);
w->i915.eb.buffer_count = j + 1;
w->i915.eb.rsvd1 = get_ctxid(wrk, w);
eb_update_flags(wrk, w);
#ifdef DEBUG
printf("%u: %u:|", w->idx, w->i915.eb.buffer_count);
for (i = 0; i <= j; i++)
printf("%x|", w->i915.obj[i].handle);
printf(" flags=%llx bb=%x[%u] ctx[%u]=%u\n",
w->i915.eb.flags, w->bb_handle, j, w->context,
get_ctxid(wrk, w));
#endif
}
static void
xe_alloc_step_batch(struct workload *wrk, struct w_step *w)
{
struct vm *vm = get_vm(wrk, w);
struct xe_exec_queue *eq = xe_get_eq(wrk, w);
struct dep_entry *dep;
int i;
w->bb_size = xe_bb_size(fd, PAGE_SIZE);
w->bb_handle = xe_bo_create(fd, vm->id, w->bb_size,
vram_if_possible(fd, eq->hwe_list[0].gt_id),
DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);
w->xe.data = xe_bo_map(fd, w->bb_handle, w->bb_size);
w->xe.exec.address =
intel_allocator_alloc_with_strategy(vm->ahnd, w->bb_handle, w->bb_size,
0, ALLOC_STRATEGY_LOW_TO_HIGH);
xe_vm_bind_sync(fd, vm->id, w->bb_handle, 0, w->xe.exec.address, w->bb_size);
xe_spin_init_opts(&w->xe.data->spin, .addr = w->xe.exec.address,
.preempt = (w->preempt_us > 0),
.ctx_ticks = xe_spin_nsec_to_ticks(fd, eq->hwe_list[0].gt_id,
1000LL * get_duration(wrk, w)));
w->xe.exec.exec_queue_id = eq->id;
w->xe.exec.num_batch_buffer = 1;
/* always at least one out fence */
w->xe.exec.num_syncs = 1;
/* count syncs */
for_each_dep(dep, w->data_deps) {
int dep_idx = w->idx + dep->target;
igt_assert(dep_idx >= 0 && dep_idx < w->idx);
igt_assert(wrk->steps[dep_idx].type == BATCH);
w->xe.exec.num_syncs++;
}
for_each_dep(dep, w->fence_deps) {
int dep_idx = w->idx + dep->target;
igt_assert(dep_idx >= 0 && dep_idx < w->idx);
igt_assert(wrk->steps[dep_idx].type == SW_FENCE ||
wrk->steps[dep_idx].type == BATCH);
w->xe.exec.num_syncs++;
}
w->xe.syncs = calloc(w->xe.exec.num_syncs, sizeof(*w->xe.syncs));
/* fill syncs */
i = 0;
/* out fence */
w->xe.syncs[i].handle = syncobj_create(fd, 0);
w->xe.syncs[i].type = DRM_XE_SYNC_TYPE_SYNCOBJ;
w->xe.syncs[i++].flags = DRM_XE_SYNC_FLAG_SIGNAL;
/* in fence(s) */
for_each_dep(dep, w->data_deps) {
int dep_idx = w->idx + dep->target;
igt_assert(wrk->steps[dep_idx].xe.syncs && wrk->steps[dep_idx].xe.syncs[0].handle);
w->xe.syncs[i].handle = wrk->steps[dep_idx].xe.syncs[0].handle;
w->xe.syncs[i++].type = DRM_XE_SYNC_TYPE_SYNCOBJ;
}
for_each_dep(dep, w->fence_deps) {
int dep_idx = w->idx + dep->target;
igt_assert(wrk->steps[dep_idx].xe.syncs && wrk->steps[dep_idx].xe.syncs[0].handle);
w->xe.syncs[i].handle = wrk->steps[dep_idx].xe.syncs[0].handle;
w->xe.syncs[i++].type = DRM_XE_SYNC_TYPE_SYNCOBJ;
}
w->xe.exec.syncs = to_user_pointer(w->xe.syncs);
}
static bool set_priority(uint32_t ctx_id, int prio)
{
struct drm_i915_gem_context_param param = {
.ctx_id = ctx_id,
.param = I915_CONTEXT_PARAM_PRIORITY,
.value = prio,
};
return __gem_context_set_param(fd, ¶m) == 0;
}
static bool set_persistence(uint32_t ctx_id, bool state)
{
struct drm_i915_gem_context_param param = {
.ctx_id = ctx_id,
.param = I915_CONTEXT_PARAM_PERSISTENCE,
.value = state,
};
return __gem_context_set_param(fd, ¶m) == 0;
}
static void __configure_context(uint32_t ctx_id, unsigned int prio)
{
set_priority(ctx_id, prio);
/* Mark as non-persistent if supported. */
set_persistence(ctx_id, false);
}
static int __vm_destroy(int i915, uint32_t vm_id)
{
struct drm_i915_gem_vm_control ctl = { .vm_id = vm_id };
int err = 0;
if (igt_ioctl(i915, DRM_IOCTL_I915_GEM_VM_DESTROY, &ctl)) {
err = -errno;
igt_assume(err);
}
errno = 0;
return err;
}
static void vm_destroy(int i915, uint32_t vm_id)
{
igt_assert_eq(__vm_destroy(i915, vm_id), 0);
}
static struct drm_i915_gem_context_param_sseu get_device_sseu(void)
{
struct drm_i915_gem_context_param param = { };
if (device_sseu.slice_mask == -1) {
param.param = I915_CONTEXT_PARAM_SSEU;
param.value = (uintptr_t)&device_sseu;
gem_context_get_param(fd, ¶m);
}
return device_sseu;
}
static uint64_t
set_ctx_sseu(struct ctx *ctx, uint64_t slice_mask)
{
struct drm_i915_gem_context_param_sseu sseu = get_device_sseu();
struct drm_i915_gem_context_param param = { };
if (slice_mask == -1)
slice_mask = device_sseu.slice_mask;
if (ctx->engine_map.nr_engines && ctx->load_balance) {
sseu.flags = I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX;
sseu.engine.engine_class = I915_ENGINE_CLASS_INVALID;
sseu.engine.engine_instance = 0;
}
sseu.slice_mask = slice_mask;
param.ctx_id = ctx->id;
param.param = I915_CONTEXT_PARAM_SSEU;
param.size = sizeof(sseu);
param.value = (uintptr_t)&sseu;
gem_context_set_param(fd, ¶m);
return slice_mask;
}
static size_t sizeof_load_balance(int count)
{
return offsetof(struct i915_context_engines_load_balance,
engines[count]);
}
static size_t sizeof_param_engines(int count)
{
return offsetof(struct i915_context_param_engines,
engines[count]);
}
static size_t sizeof_engines_bond(int count)
{
return offsetof(struct i915_context_engines_bond,
engines[count]);
}
static unsigned long
get_buffer_size(struct workload *wrk, const struct work_buffer_size *sz)
{
if (sz->min == sz->max)
return sz->min;
else
return sz->min + hars_petruska_f54_1_random(&wrk->bo_prng) %
(sz->max + 1 - sz->min);
}
static unsigned long
allocate_working_set(struct workload *wrk, struct working_set *set)
{
unsigned long total = 0;
unsigned int i;
set->handles = calloc(set->nr, sizeof(*set->handles));
igt_assert(set->handles);
for (i = 0; i < set->nr; i++) {
set->sizes[i].size = get_buffer_size(wrk, &set->sizes[i]);
set->handles[i] = alloc_bo(fd, &set->sizes[i].size);
total += set->sizes[i].size;
}
return total;
}
static bool
find_dep(struct dep_entry *deps, unsigned int nr, struct dep_entry dep)
{
unsigned int i;
for (i = 0; i < nr; i++) {
if (deps[i].working_set == dep.working_set &&
deps[i].target == dep.target)
return true;
}
return false;
}
static void measure_active_set(struct workload *wrk)
{
unsigned long total = 0, batch_sizes = 0;
struct dep_entry *dep, *deps = NULL;
unsigned int nr = 0;
struct w_step *w;
if (verbose < 3)
return;
for_each_w_step(w, wrk) {
if (w->type != BATCH)
continue;
batch_sizes += w->bb_size;
if (is_xe)
continue;
for_each_dep(dep, w->data_deps) {
struct dep_entry _dep = *dep;
if (dep->working_set == -1 && dep->target < 0) {
int idx = w->idx + dep->target;
igt_assert(idx >= 0 && idx < w->idx);
igt_assert(wrk->steps[idx].type == BATCH);
_dep.target = wrk->steps[idx].i915.obj[0].handle;
}
if (!find_dep(deps, nr, _dep)) {
if (dep->working_set == -1) {
total += w->bb_size;
} else {
struct working_set *set;
igt_assert(dep->working_set <=
wrk->max_working_set_id);
set = wrk->working_sets[dep->working_set];
igt_assert(set->nr);
igt_assert(dep->target < set->nr);
igt_assert(set->sizes[dep->target].size);
total += set->sizes[dep->target].size;
}
deps = realloc(deps, (nr + 1) * sizeof(*deps));
deps[nr++] = *dep;
}
}
}
free(deps);
printf("%u: %lu bytes active working set in %u buffers. %lu in batch buffers.\n",
wrk->id, total, nr, batch_sizes);
}
#define alloca0(sz) ({ size_t sz__ = (sz); memset(alloca(sz__), 0, sz__); })
static void xe_vm_create_(struct vm *vm)
{
uint32_t flags = 0;
if (vm->compute_mode)
flags |= DRM_XE_VM_CREATE_FLAG_LR_MODE;
vm->id = xe_vm_create(fd, flags, 0);
}
static void xe_exec_queue_create_(struct ctx *ctx, struct xe_exec_queue *eq)
{
struct drm_xe_exec_queue_create create = {
.vm_id = ctx->vm->id,
.width = 1,
.num_placements = eq->nr_hwes,
.instances = to_user_pointer(eq->hwe_list),
};
igt_assert_eq(igt_ioctl(fd, DRM_IOCTL_XE_EXEC_QUEUE_CREATE, &create), 0);
eq->id = create.exec_queue_id;
}
static void allocate_contexts(unsigned int id, struct workload *wrk)
{
int max_ctx = -1;
struct w_step *w;
/*
* Pre-scan workload steps to allocate context list storage.
*/
for_each_w_step(w, wrk) {
int ctx = w->context + 1;
int delta;
w->wrk = wrk;
if (ctx <= max_ctx)
continue;
delta = ctx + 1 - wrk->nr_ctxs;
wrk->nr_ctxs += delta;
wrk->ctx_list = realloc(wrk->ctx_list,
wrk->nr_ctxs * sizeof(*wrk->ctx_list));
memset(&wrk->ctx_list[wrk->nr_ctxs - delta], 0,
delta * sizeof(*wrk->ctx_list));
max_ctx = ctx;
}
}
static int prepare_contexts(unsigned int id, struct workload *wrk)
{
uint32_t share_vm = 0;
struct w_step *w;
struct ctx *ctx, *ctx2;
unsigned int j;
/*
* Transfer over engine map configuration from the workload step.
*/
__for_each_ctx(ctx, wrk, ctx_idx) {
for_each_w_step(w, wrk) {
if (w->context != ctx_idx)
continue;
if (w->type == ENGINE_MAP) {
ctx->engine_map = w->engine_map;
} else if (w->type == LOAD_BALANCE) {
if (!ctx->engine_map.nr_engines) {
wsim_err("Load balancing needs an engine map!\n");
return 1;
}
if (intel_gen(intel_get_drm_devid(fd)) < 11) {
wsim_err("Load balancing needs relative mmio support, gen11+!\n");
return 1;
}
ctx->load_balance = w->load_balance;
} else if (w->type == BOND) {
if (!ctx->load_balance) {
wsim_err("Engine bonds need load balancing engine map!\n");
return 1;
}
ctx->bond_count++;
ctx->bonds = realloc(ctx->bonds,
ctx->bond_count *
sizeof(struct bond));
igt_assert(ctx->bonds);
ctx->bonds[ctx->bond_count - 1] = w->bond;
}
}
}
/*
* Create and configure contexts.
*/
__for_each_ctx(ctx, wrk, ctx_idx) {
struct drm_i915_gem_context_create_ext_setparam ext = {
.base.name = I915_CONTEXT_CREATE_EXT_SETPARAM,
.param.param = I915_CONTEXT_PARAM_VM,
};
struct drm_i915_gem_context_create_ext args = { };
uint32_t ctx_id;
igt_assert(!ctx->id);
/* Find existing context to share ppgtt with. */
if (!share_vm)
for_each_ctx(ctx2, wrk) {
struct drm_i915_gem_context_param param = {
.param = I915_CONTEXT_PARAM_VM,
.ctx_id = ctx2->id,
};
if (!param.ctx_id)
continue;
gem_context_get_param(fd, ¶m);
igt_assert(param.value);
share_vm = param.value;
wrk->nr_vms = 1;
wrk->vm_list = calloc(wrk->nr_vms, sizeof(struct vm));
igt_assert(wrk->vm_list);
wrk->vm_list->id = share_vm;
wrk->vm_list->ahnd = intel_allocator_open(fd, share_vm,
INTEL_ALLOCATOR_RELOC);
ctx2->vm = wrk->vm_list;
break;
}
if (share_vm) {
ext.param.value = share_vm;
args.flags = I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS;
args.extensions = to_user_pointer(&ext);
}
drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &args);
igt_assert(args.ctx_id);
ctx_id = args.ctx_id;
ctx->id = ctx_id;
ctx->sseu = device_sseu.slice_mask;
ctx->vm = wrk->vm_list;
__configure_context(ctx_id, wrk->prio);
if (ctx->engine_map.nr_engines) {
struct i915_context_param_engines *set_engines =
alloca0(sizeof_param_engines(ctx->engine_map.nr_engines + 1));
struct i915_context_engines_load_balance *load_balance =
alloca0(sizeof_load_balance(ctx->engine_map.nr_engines));
struct drm_i915_gem_context_param param = {
.ctx_id = ctx_id,
.param = I915_CONTEXT_PARAM_ENGINES,
.size = sizeof_param_engines(ctx->engine_map.nr_engines + 1),
.value = to_user_pointer(set_engines),
};
struct i915_context_engines_bond *last = NULL;
/* update engine_idx and request_idx */
for_each_w_step(w, wrk) {
if (w->context != ctx_idx)
continue;
if (w->type == BATCH) {
unsigned int map_idx = 0;
if (find_engine_in_map(&w->engine, &ctx->engine_map,
&map_idx))
/* 0 is virtual, map indexes are shifted by one */
w->engine_idx = map_idx + 1;
else
igt_assert(ctx->load_balance);
igt_assert(find_engine_in_map(&ctx->engine_map
.engines[map_idx],
query_engines(),
&w->request_idx));
}
}
if (ctx->load_balance) {
set_engines->extensions =
to_user_pointer(load_balance);
load_balance->base.name =
I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE;
load_balance->num_siblings =
ctx->engine_map.nr_engines;
for (j = 0; j < ctx->engine_map.nr_engines; j++)
load_balance->engines[j] =
engine_to_i915_engine_class(&ctx->engine_map
.engines[j]);
}
/* Reserve slot for virtual engine. */
set_engines->engines[0].engine_class =
I915_ENGINE_CLASS_INVALID;
set_engines->engines[0].engine_instance =
I915_ENGINE_CLASS_INVALID_NONE;
for (j = 1; j <= ctx->engine_map.nr_engines; j++)
set_engines->engines[j] =
engine_to_i915_engine_class(&ctx->engine_map
.engines[j - 1]);
last = NULL;
for (j = 0; j < ctx->bond_count; j++) {
struct intel_engines *mask = &ctx->bonds[j].mask;
struct i915_context_engines_bond *bond =
alloca0(sizeof_engines_bond(mask->nr_engines));
unsigned int b, e;
bond->base.next_extension = to_user_pointer(last);
bond->base.name = I915_CONTEXT_ENGINES_EXT_BOND;
bond->virtual_index = 0;
bond->master = engine_to_i915_engine_class(&ctx->bonds[j].master);
for (b = 0, e = 0; e < mask->nr_engines; e++) {
unsigned int idx;
igt_assert(find_engine_in_map(&mask->engines[e],
&ctx->engine_map,
&idx));
bond->engines[b++] = set_engines->engines[1 + idx];
}
last = bond;
}
load_balance->base.next_extension = to_user_pointer(last);
gem_context_set_param(fd, ¶m);
} else {
/* update engine_idx and request_idx */
for_each_w_step(w, wrk) {
if (w->context != ctx_idx)
continue;
if (w->type == BATCH) {
w->engine_idx = engine_to_i915_legacy_ring(&w->engine);
resolve_to_physical_engine(&w->engine);
igt_assert(find_engine_in_map(&w->engine,
query_engines(),
&w->request_idx));
}
}
}
if (wrk->sseu) {
/* Set to slice 0 only, one slice. */
ctx->sseu = set_ctx_sseu(ctx, 1);
}
}
if (share_vm)
vm_destroy(fd, share_vm);
return 0;
}
static int xe_prepare_contexts(unsigned int id, struct workload *wrk)
{
struct xe_exec_queue *eq;
struct w_step *w;
struct ctx *ctx;
unsigned int i;
/* shortcut, create one vm */
wrk->nr_vms = 1;
wrk->vm_list = calloc(wrk->nr_vms, sizeof(struct vm));
igt_assert(wrk->vm_list);
wrk->vm_list->compute_mode = false;
xe_vm_create_(wrk->vm_list);
wrk->vm_list->ahnd = intel_allocator_open(fd, wrk->vm_list->id,
INTEL_ALLOCATOR_RELOC);
__for_each_ctx(ctx, wrk, ctx_idx) {
/* link with vm */
ctx->vm = wrk->vm_list;
for_each_w_step(w, wrk) {
if (w->context != ctx_idx)
continue;
if (w->type == ENGINE_MAP) {
ctx->engine_map = w->engine_map;
} else if (w->type == LOAD_BALANCE) {
if (!ctx->engine_map.nr_engines) {
wsim_err("Load balancing needs an engine map!\n");
return 1;
}
ctx->load_balance = w->load_balance;
}
}
/* create exec queue for each referenced engine */
if (ctx->engine_map.nr_engines) {
ctx->xe.nr_queues = 1;
ctx->xe.queue_list = calloc(ctx->xe.nr_queues, sizeof(*ctx->xe.queue_list));
igt_assert(ctx->xe.queue_list);
eq = &ctx->xe.queue_list[ctx->xe.nr_queues - 1];
eq->nr_hwes = ctx->engine_map.nr_engines;
eq->hwe_list = calloc(eq->nr_hwes, sizeof(*eq->hwe_list));
for (i = 0; i < eq->nr_hwes; ++i) {
eq->hwe_list[i] = ctx->engine_map.engines[i];
/* check no mixing classes and no duplicates */
for (int j = 0; j < i; ++j) {
if (eq->hwe_list[j].engine_class !=
eq->hwe_list[i].engine_class) {
free(eq->hwe_list);
eq->nr_hwes = 0;
wsim_err("Mixing of engine class not supported!\n");
return 1;
}
if (eq->hwe_list[j].engine_instance ==
eq->hwe_list[i].engine_instance) {
free(eq->hwe_list);
eq->nr_hwes = 0;
wsim_err("Duplicate engine entry!\n");
return 1;
}
}
if (verbose > 3)
printf("%u ctx[%d] %s [%u:%u:%u]\n", id,
ctx_idx,
intel_engine_class_string(ctx->engine_map
.engines[i]
.engine_class),
eq->hwe_list[i].engine_class,
eq->hwe_list[i].engine_instance,
eq->hwe_list[i].gt_id);
}
xe_exec_queue_create_(ctx, eq);
} else {
/* create engine_map, update engine_idx */
for_each_w_step(w, wrk) {
if (w->context != ctx_idx)
continue;
if (w->type == BATCH) {
resolve_to_physical_engine(&w->engine);
if (!find_engine_in_map(&w->engine, &ctx->engine_map,
&w->engine_idx)) {
igt_assert(1 ==
append_matching_engines(&w->engine,
&ctx->engine_map)
);
w->engine_idx = ctx->engine_map.nr_engines - 1;
}
}
}
/* skip not referenced context */
if (!ctx->engine_map.nr_engines)
continue;
ctx->xe.nr_queues = ctx->engine_map.nr_engines;
ctx->xe.queue_list = calloc(ctx->xe.nr_queues, sizeof(*ctx->xe.queue_list));
for (i = 0; i < ctx->xe.nr_queues; i++) {
eq = &ctx->xe.queue_list[i];
eq->nr_hwes = 1;
eq->hwe_list = calloc(1, sizeof(*eq->hwe_list));
eq->hwe_list[0] = ctx->engine_map.engines[i];
if (verbose > 3)
printf("%u ctx[%d] %s [%d:%d:%d]\n",
id, ctx_idx,
intel_engine_class_string(ctx->engine_map
.engines[i]
.engine_class),
eq->hwe_list[0].engine_class,
eq->hwe_list[0].engine_instance,
eq->hwe_list[0].gt_id);
xe_exec_queue_create_(ctx, eq);
}
}
/* update request_idx */
for_each_w_step(w, wrk) {
if (w->context != ctx_idx)
continue;
if (w->type == BATCH) {
igt_assert(find_engine_in_map(&ctx->engine_map
.engines[w->engine_idx],
query_engines(),
&w->request_idx));
}
}
}
/* create syncobjs for SW_FENCE */
for_each_w_step(w, wrk)
if (w->type == SW_FENCE) {
w->xe.syncs = calloc(1, sizeof(struct drm_xe_sync));
w->xe.syncs[0].handle = syncobj_create(fd, 0);
w->xe.syncs[0].type = DRM_XE_SYNC_TYPE_SYNCOBJ;
}
return 0;
}
static void prepare_working_sets(unsigned int id, struct workload *wrk)
{
struct working_set **sets;
unsigned long total = 0;
struct w_step *w;
/*
* Allocate working sets.
*/
for_each_w_step(w, wrk) {
if (w->type == WORKINGSET && !w->working_set.shared)
total += allocate_working_set(wrk, &w->working_set);
}
if (verbose > 2)
printf("%u: %lu bytes in working sets.\n", wrk->id, total);
/*
* Map of working set ids.
*/
wrk->max_working_set_id = -1;
for_each_w_step(w, wrk) {
if (w->type == WORKINGSET &&
w->working_set.id > wrk->max_working_set_id)
wrk->max_working_set_id = w->working_set.id;
}
sets = wrk->working_sets;
wrk->working_sets = calloc(wrk->max_working_set_id + 1,
sizeof(*wrk->working_sets));
igt_assert(wrk->working_sets);
for_each_w_step(w, wrk) {
struct working_set *set;
if (w->type != WORKINGSET)
continue;
if (!w->working_set.shared) {
set = &w->working_set;
} else {
igt_assert(sets);
set = sets[w->working_set.id];
igt_assert(set->shared);
igt_assert(set->sizes);
}
wrk->working_sets[w->working_set.id] = set;
}
if (sets)
free(sets);
}
static int prepare_workload(unsigned int id, struct workload *wrk)
{
struct w_step *w;
int ret = 0;
wrk->id = id;
wrk->bb_prng = (wrk->flags & FLAG_SYNCEDCLIENTS) ? master_prng : rand();
wrk->bo_prng = (wrk->flags & FLAG_SYNCEDCLIENTS) ? master_prng : rand();
wrk->run = true;
allocate_contexts(id, wrk);
if (is_xe)
ret = xe_prepare_contexts(id, wrk);
else
ret = prepare_contexts(id, wrk);
if (ret)
return ret;
/* Record default preemption. */
for_each_w_step(w, wrk)
if (w->type == BATCH)
w->preempt_us = 100;
/*
* Scan for contexts with modified preemption config and record their
* preemption period for the following steps belonging to the same
* context.
*/
for_each_w_step(w, wrk) {
struct w_step *w2;
if (w->type != PREEMPTION)
continue;
for (int j = w->idx + 1; j < wrk->nr_steps; j++) {
w2 = &wrk->steps[j];
if (w2->context != w->context)
continue;
else if (w2->type == PREEMPTION)
break;
else if (w2->type != BATCH)
continue;
w2->preempt_us = w->period;
}
}
/*
* Scan for SSEU control steps.
*/
for_each_w_step(w, wrk) {
if (w->type == SSEU) {
get_device_sseu();
break;
}
}
prepare_working_sets(id, wrk);
/*
* Allocate batch buffers.
*/
for_each_w_step(w, wrk) {
if (w->type != BATCH)
continue;
if (is_xe)
xe_alloc_step_batch(wrk, w);
else
alloc_step_batch(wrk, w);
}
measure_active_set(wrk);
return ret;
}
static double elapsed(const struct timespec *start, const struct timespec *end)
{
return (end->tv_sec - start->tv_sec) +
(end->tv_nsec - start->tv_nsec) / 1e9;
}
static int elapsed_us(const struct timespec *start, const struct timespec *end)
{
return elapsed(start, end) * 1e6;
}
static void
update_bb_start(struct workload *wrk, struct w_step *w)
{
uint32_t ticks;
/* ticks is inverted for MI_DO_COMPARE (less-than comparison) */
ticks = 0;
if (!w->duration.unbound)
ticks = ~ns_to_ctx_ticks(1000LL * get_duration(wrk, w));
*w->i915.bb_duration = ticks;
}
static void w_sync_to(struct workload *wrk, struct w_step *w, int target)
{
if (target < 0)
target = wrk->nr_steps + target;
igt_assert(target < wrk->nr_steps);
while (wrk->steps[target].type != BATCH) {
if (--target < 0)
target = wrk->nr_steps + target;
}
igt_assert(target < wrk->nr_steps);
igt_assert(wrk->steps[target].type == BATCH);
w_step_sync(&wrk->steps[target]);
}
static void do_xe_exec(struct workload *wrk, struct w_step *w)
{
struct xe_exec_queue *eq = xe_get_eq(wrk, w);
igt_assert(w->emit_fence <= 0);
if (w->emit_fence == -1)
syncobj_reset(fd, &w->xe.syncs[0].handle, 1);
/* update duration if random */
if (w->duration.max != w->duration.min)
xe_spin_init_opts(&w->xe.data->spin,
.addr = w->xe.exec.address,
.preempt = (w->preempt_us > 0),
.ctx_ticks = xe_spin_nsec_to_ticks(fd, eq->hwe_list[0].gt_id,
1000LL * get_duration(wrk, w)));
xe_exec(fd, &w->xe.exec);
}
static void
do_eb(struct workload *wrk, struct w_step *w)
{
struct dep_entry *dep;
unsigned int i;
eb_update_flags(wrk, w);
update_bb_start(wrk, w);
for_each_dep(dep, w->fence_deps) {
int tgt = w->idx + dep->target;
/* TODO: fence merging needed to support multiple inputs */
igt_assert(i == 0);
igt_assert(tgt >= 0 && tgt < w->idx);
igt_assert(wrk->steps[tgt].emit_fence > 0);
if (w->fence_deps.submit_fence)
w->i915.eb.flags |= I915_EXEC_FENCE_SUBMIT;
else
w->i915.eb.flags |= I915_EXEC_FENCE_IN;
w->i915.eb.rsvd2 = wrk->steps[tgt].emit_fence;
}
if (w->i915.eb.flags & I915_EXEC_FENCE_OUT)
gem_execbuf_wr(fd, &w->i915.eb);
else
gem_execbuf(fd, &w->i915.eb);
if (w->i915.eb.flags & I915_EXEC_FENCE_OUT) {
w->emit_fence = w->i915.eb.rsvd2 >> 32;
igt_assert(w->emit_fence > 0);
}
}
static void sync_deps(struct workload *wrk, struct w_step *w)
{
unsigned int i;
for (i = 0; i < w->data_deps.nr; i++) {
struct dep_entry *entry = &w->data_deps.list[i];
int dep_idx;
if (entry->working_set == -1)
continue;
igt_assert(entry->target <= 0);
if (!entry->target)
continue;
dep_idx = w->idx + entry->target;
igt_assert(dep_idx >= 0 && dep_idx < w->idx);
igt_assert(wrk->steps[dep_idx].type == BATCH);
w_step_sync(&wrk->steps[dep_idx]);
}
}
static void *run_workload(void *data)
{
struct workload *wrk = (struct workload *)data;
struct timespec t_start, t_end, repeat_start;
struct w_step *w;
int throttle = -1;
int qd_throttle = -1;
int count, missed = 0;
unsigned long time_tot = 0, time_min = ULONG_MAX, time_max = 0;
clock_gettime(CLOCK_MONOTONIC, &t_start);
for (count = 0; wrk->run && (wrk->background || count < wrk->repeat);
count++) {
unsigned int cur_seqno = wrk->sync_seqno;
clock_gettime(CLOCK_MONOTONIC, &repeat_start);
for_each_w_step(w, wrk) {
int do_sleep = 0;
if (!wrk->run)
break;
if (w->type == DELAY) {
do_sleep = w->delay;
} else if (w->type == PERIOD) {
struct timespec now;
int elapsed;
clock_gettime(CLOCK_MONOTONIC, &now);
elapsed = elapsed_us(&repeat_start, &now);
do_sleep = w->period - elapsed;
time_tot += elapsed;
if (elapsed < time_min)
time_min = elapsed;
if (elapsed > time_max)
time_max = elapsed;
if (do_sleep < 0) {
missed++;
if (verbose > 2)
printf("%u: Dropped period @ %u/%u (%dus late)!\n",
wrk->id, count, w->idx, do_sleep);
continue;
}
} else if (w->type == SYNC) {
unsigned int s_idx = w->idx + w->target;
igt_assert(s_idx >= 0 && s_idx < w->idx);
igt_assert(wrk->steps[s_idx].type == BATCH);
w_step_sync(&wrk->steps[s_idx]);
continue;
} else if (w->type == THROTTLE) {
throttle = w->throttle;
continue;
} else if (w->type == QD_THROTTLE) {
qd_throttle = w->throttle;
continue;
} else if (w->type == SW_FENCE) {
igt_assert(w->emit_fence < 0);
w->emit_fence =
sw_sync_timeline_create_fence(wrk->sync_timeline,
cur_seqno + w->idx);
igt_assert(w->emit_fence > 0);
if (is_xe)
/* Convert sync file to syncobj */
syncobj_import_sync_file(fd, w->xe.syncs[0].handle,
w->emit_fence);
continue;
} else if (w->type == SW_FENCE_SIGNAL) {
int tgt = w->idx + w->target;
int inc;
igt_assert(tgt >= 0 && tgt < w->idx);
igt_assert(wrk->steps[tgt].type == SW_FENCE);
cur_seqno += wrk->steps[tgt].idx;
inc = cur_seqno - wrk->sync_seqno;
sw_sync_timeline_inc(wrk->sync_timeline, inc);
continue;
} else if (w->type == CTX_PRIORITY) {
if (w->priority != wrk->ctx_list[w->context].priority) {
struct drm_i915_gem_context_param param = {
.ctx_id = wrk->ctx_list[w->context].id,
.param = I915_CONTEXT_PARAM_PRIORITY,
.value = w->priority,
};
gem_context_set_param(fd, ¶m);
wrk->ctx_list[w->context].priority =
w->priority;
}
continue;
} else if (w->type == TERMINATE) {
unsigned int t_idx = w->idx + w->target;
igt_assert(t_idx >= 0 && t_idx < w->idx);
igt_assert(wrk->steps[t_idx].type == BATCH);
igt_assert(wrk->steps[t_idx].duration.unbound);
if (is_xe)
xe_spin_end(&wrk->steps[t_idx].xe.data->spin);
else
*wrk->steps[t_idx].i915.bb_duration = 0xffffffff;
__sync_synchronize();
continue;
} else if (w->type == SSEU) {
if (w->sseu != wrk->ctx_list[w->context * 2].sseu) {
wrk->ctx_list[w->context * 2].sseu =
set_ctx_sseu(&wrk->ctx_list[w->context * 2],
w->sseu);
}
continue;
} else if (w->type == PREEMPTION ||
w->type == ENGINE_MAP ||
w->type == LOAD_BALANCE ||
w->type == BOND ||
w->type == WORKINGSET) {
/* No action for these at execution time. */
continue;
}
if (do_sleep || w->type == PERIOD) {
usleep(do_sleep);
continue;
}
igt_assert(w->type == BATCH);
if (wrk->flags & FLAG_DEPSYNC)
sync_deps(wrk, w);
if (throttle > 0)
w_sync_to(wrk, w, w->idx - throttle);
if (is_xe)
do_xe_exec(wrk, w);
else
do_eb(wrk, w);
if (w->rq_link.next) {
igt_list_del(&w->rq_link);
wrk->nrequest[w->request_idx]--;
}
igt_list_add_tail(&w->rq_link, &wrk->requests[w->request_idx]);
wrk->nrequest[w->request_idx]++;
if (!wrk->run)
break;
if (w->sync)
w_step_sync(w);
if (qd_throttle > 0) {
while (wrk->nrequest[w->request_idx] > qd_throttle) {
struct w_step *s;
s = igt_list_first_entry(&wrk->requests[w->request_idx],
s, rq_link);
w_step_sync(s);
igt_list_del(&s->rq_link);
wrk->nrequest[w->request_idx]--;
}
}
}
if (wrk->sync_timeline) {
int inc;
inc = wrk->nr_steps - (cur_seqno - wrk->sync_seqno);
sw_sync_timeline_inc(wrk->sync_timeline, inc);
wrk->sync_seqno += wrk->nr_steps;
}
/* Cleanup all fences instantiated in this iteration. */
for_each_w_step(w, wrk) {
if (!wrk->run)
break;
if (w->emit_fence > 0) {
if (is_xe) {
igt_assert(w->type == SW_FENCE);
syncobj_reset(fd, &w->xe.syncs[0].handle, 1);
}
close(w->emit_fence);
w->emit_fence = -1;
}
}
}
for (int i = query_engines()->nr_engines; --i >= 0;) {
if (!wrk->nrequest[i])
continue;
w = igt_list_last_entry(&wrk->requests[i], w, rq_link);
w_step_sync(w);
}
if (is_xe) {
for_each_w_step(w, wrk) {
if (w->type == BATCH) {
w_step_sync(w);
syncobj_destroy(fd, w->xe.syncs[0].handle);
free(w->xe.syncs);
xe_vm_unbind_sync(fd, get_vm(wrk, w)->id, 0, w->xe.exec.address,
w->bb_size);
gem_munmap(w->xe.data, w->bb_size);
gem_close(fd, w->bb_handle);
} else if (w->type == SW_FENCE) {
syncobj_destroy(fd, w->xe.syncs[0].handle);
free(w->xe.syncs);
}
}
}
clock_gettime(CLOCK_MONOTONIC, &t_end);
if (wrk->print_stats) {
double t = elapsed(&t_start, &t_end);
printf("%c%u: %.3fs elapsed (%d cycles, %.3f workloads/s).",
wrk->background ? ' ' : '*', wrk->id,
t, count, count / t);
if (time_tot)
printf(" Time avg/min/max=%lu/%lu/%luus; %u missed.",
time_tot / count, time_min, time_max, missed);
putchar('\n');
}
return NULL;
}
static void fini_workload(struct workload *wrk)
{
free(wrk->steps);
free(wrk);
}
static void print_help(void)
{
puts(
"Usage: gem_wsim [OPTIONS]\n"
"\n"
"Runs a simulated workload on the GPU.\n"
"Options:\n"
" -h This text.\n"
" -q Be quiet - do not output anything to stdout.\n"
" -I <n> Initial randomness seed.\n"
" -p <n> Context priority to use for the following workload on the\n"
" command line.\n"
" -w <desc|path> Filename or a workload descriptor.\n"
" Can be given multiple times.\n"
" -W <desc|path> Filename or a master workload descriptor.\n"
" Only one master workload can be optinally specified in which\n"
" case all other workloads become background ones and run as\n"
" long as the master.\n"
" -a <desc|path> Append a workload to all other workloads.\n"
" -r <n> How many times to emit the workload.\n"
" -c <n> Fork N clients emitting the workload simultaneously.\n"
" -s Turn on small SSEU config for the next workload on the\n"
" command line. Subsequent -s switches it off.\n"
" -S Synchronize the sequence of random batch durations between\n"
" clients.\n"
" -d Sync between data dependencies in userspace.\n"
" -f <scale> Scale factor for batch durations.\n"
" -F <scale> Scale factor for delays.\n"
" -L List GPUs.\n"
" -l List physical engines.\n"
" -D <gpu> One of the GPUs from -L.\n"
);
}
static char *load_workload_descriptor(char *filename)
{
struct stat sbuf;
char *buf;
int infd, ret, i;
ssize_t len;
bool in_comment = false;
ret = stat(filename, &sbuf);
if (ret || !S_ISREG(sbuf.st_mode))
return filename;
igt_assert(sbuf.st_size < 1024 * 1024); /* Just so. */
buf = malloc(sbuf.st_size);
igt_assert(buf);
infd = open(filename, O_RDONLY);
igt_assert(infd >= 0);
len = read(infd, buf, sbuf.st_size);
igt_assert(len == sbuf.st_size);
close(infd);
for (i = 0; i < len; i++) {
/*
* Lines starting with '#' are skipped.
* If command line step separator (',') is encountered after '#'
* it is replaced with ';' to not break parsing.
*/
if (buf[i] == '#')
in_comment = true;
else if (buf[i] == '\n') {
buf[i] = ',';
in_comment = false;
} else if (in_comment && buf[i] == ',')
buf[i] = ';';
}
len--;
while (buf[len] == ',')
buf[len--] = 0;
return buf;
}
static struct w_arg *
add_workload_arg(struct w_arg *w_args, unsigned int nr_args, char *w_arg,
int prio, bool sseu)
{
w_args = realloc(w_args, sizeof(*w_args) * nr_args);
igt_assert(w_args);
w_args[nr_args - 1] = (struct w_arg) { w_arg, NULL, prio, sseu };
return w_args;
}
static void list_engines(void)
{
struct intel_engines *engines = query_engines();
int engine_class_count[NUM_ENGINE_CLASSES] = {};
unsigned int i;
for (i = 0; i < engines->nr_engines; ++i) {
igt_assert_lt(engines->engines[i].engine_class, NUM_ENGINE_CLASSES);
engine_class_count[engines->engines[i].engine_class]++;
}
for (i = 0; i < engines->nr_engines; ++i) {
if (engine_class_count[engines->engines[i].engine_class] > 1)
printf("%s%u",
intel_engine_class_string(engines->engines[i].engine_class),
engines->engines[i].engine_instance + 1);
else
printf("%s",
intel_engine_class_string(engines->engines[i].engine_class));
if (is_xe && engines->engines[i].gt_id)
printf("-%u", engines->engines[i].gt_id);
if (verbose > 3)
printf(" [%d:%d:%d]", engines->engines[i].engine_class,
engines->engines[i].engine_instance,
engines->engines[i].gt_id);
printf("\n");
}
}
int main(int argc, char **argv)
{
struct igt_device_card card = { };
bool list_devices_arg = false;
bool list_engines_arg = false;
unsigned int repeat = 1;
unsigned int clients = 1;
unsigned int flags = 0;
struct timespec t_start, t_end;
struct workload **w, **wrk = NULL;
struct workload *app_w = NULL;
unsigned int nr_w_args = 0;
int master_workload = -1;
char *append_workload_arg = NULL;
struct w_arg *w_args = NULL;
int exitcode = EXIT_FAILURE;
char *device_arg = NULL;
double scale_time = 1.0f;
double scale_dur = 1.0f;
int prio = 0;
double t;
int i, c, ret;
char *drm_dev;
master_prng = time(NULL);
while ((c = getopt(argc, argv,
"LlhqvsSdc:r:w:W:a:p:I:f:F:D:")) != -1) {
switch (c) {
case 'L':
list_devices_arg = true;
break;
case 'l':
list_engines_arg = true;
break;
case 'D':
device_arg = strdup(optarg);
break;
case 'W':
if (master_workload >= 0) {
wsim_err("Only one master workload can be given!\n");
goto err;
}
master_workload = nr_w_args;
/* Fall through */
case 'w':
w_args = add_workload_arg(w_args, ++nr_w_args, optarg,
prio, flags & FLAG_SSEU);
break;
case 'p':
prio = atoi(optarg);
break;
case 'a':
if (append_workload_arg) {
wsim_err("Only one append workload can be given!\n");
goto err;
}
append_workload_arg = optarg;
break;
case 'c':
clients = strtol(optarg, NULL, 0);
break;
case 'r':
repeat = strtol(optarg, NULL, 0);
break;
case 'q':
verbose = 0;
break;
case 'v':
verbose++;
break;
case 'S':
flags |= FLAG_SYNCEDCLIENTS;
break;
case 's':
flags ^= FLAG_SSEU;
break;
case 'd':
flags |= FLAG_DEPSYNC;
break;
case 'I':
master_prng = strtol(optarg, NULL, 0);
break;
case 'f':
scale_dur = atof(optarg);
break;
case 'F':
scale_time = atof(optarg);
break;
case 'h':
print_help();
goto out;
default:
goto err;
}
}
igt_devices_scan();
if (list_devices_arg) {
struct igt_devices_print_format fmt = {
.type = IGT_PRINT_USER,
.option = IGT_PRINT_DRM,
};
igt_devices_print(&fmt);
return EXIT_SUCCESS;
}
if (device_arg) {
ret = igt_device_card_match(device_arg, &card);
if (!ret) {
wsim_err("Requested device %s not found!\n",
device_arg);
free(device_arg);
return EXIT_FAILURE;
}
free(device_arg);
} else {
ret = igt_device_find_first_i915_discrete_card(&card);
if (!ret)
ret = igt_device_find_integrated_card(&card);
if (!ret)
ret = igt_device_find_first_xe_discrete_card(&card);
if (!ret)
ret = igt_device_find_xe_integrated_card(&card);
if (!ret) {
wsim_err("No device filter specified and no intel devices found!\n");
return EXIT_FAILURE;
}
}
if (strlen(card.card)) {
drm_dev = card.card;
} else if (strlen(card.render)) {
drm_dev = card.render;
} else {
wsim_err("Failed to detect device!\n");
return EXIT_FAILURE;
}
fd = open(drm_dev, O_RDWR);
if (fd < 0) {
wsim_err("Failed to open '%s'! (%s)\n",
drm_dev, strerror(errno));
return EXIT_FAILURE;
}
if (verbose > 1)
printf("Using device %s\n", drm_dev);
is_xe = is_xe_device(fd);
if (is_xe)
xe_device_get(fd);
if (list_engines_arg) {
list_engines();
goto out;
}
if (!nr_w_args) {
wsim_err("No workload descriptor(s)!\n");
goto err;
}
if (nr_w_args > 1 && clients > 1) {
wsim_err("Cloned clients cannot be combined with multiple workloads!\n");
goto err;
}
if (append_workload_arg) {
append_workload_arg = load_workload_descriptor(append_workload_arg);
if (!append_workload_arg) {
wsim_err("Failed to load append workload descriptor!\n");
goto err;
}
}
if (append_workload_arg) {
struct w_arg arg = { NULL, append_workload_arg, 0 };
app_w = parse_workload(&arg, flags, scale_dur, scale_time,
NULL);
if (!app_w) {
wsim_err("Failed to parse append workload!\n");
goto err;
}
}
wrk = calloc(nr_w_args, sizeof(*wrk));
igt_assert(wrk);
for (i = 0; i < nr_w_args; i++) {
w_args[i].desc = load_workload_descriptor(w_args[i].filename);
if (!w_args[i].desc) {
wsim_err("Failed to load workload descriptor %u!\n", i);
goto err;
}
wrk[i] = parse_workload(&w_args[i], flags, scale_dur,
scale_time, app_w);
if (!wrk[i]) {
wsim_err("Failed to parse workload %u!\n", i);
goto err;
}
}
if (nr_w_args > 1)
clients = nr_w_args;
if (verbose > 1) {
printf("Random seed is %u.\n", master_prng);
printf("%u client%s.\n", clients, clients > 1 ? "s" : "");
}
srand(master_prng);
master_prng = rand();
if (master_workload >= 0 && clients == 1)
master_workload = -1;
w = calloc(clients, sizeof(struct workload *));
igt_assert(w);
for (i = 0; i < clients; i++) {
w[i] = clone_workload(wrk[nr_w_args > 1 ? i : 0]);
w[i]->flags = flags;
w[i]->repeat = repeat;
w[i]->background = master_workload >= 0 && i != master_workload;
w[i]->print_stats = verbose > 1 ||
(verbose > 0 && master_workload == i);
if (prepare_workload(i, w[i])) {
wsim_err("Failed to prepare workload %u!\n", i);
goto err;
}
}
clock_gettime(CLOCK_MONOTONIC, &t_start);
for (i = 0; i < clients; i++) {
ret = pthread_create(&w[i]->thread, NULL, run_workload, w[i]);
igt_assert_eq(ret, 0);
}
if (master_workload >= 0) {
ret = pthread_join(w[master_workload]->thread, NULL);
igt_assert_eq(ret, 0);
for (i = 0; i < clients; i++)
w[i]->run = false;
}
for (i = 0; i < clients; i++) {
if (master_workload != i) {
ret = pthread_join(w[i]->thread, NULL);
igt_assert_eq(ret, 0);
}
}
clock_gettime(CLOCK_MONOTONIC, &t_end);
t = elapsed(&t_start, &t_end);
if (verbose)
printf("%.3fs elapsed (%.3f workloads/s)\n",
t, clients * repeat / t);
for (i = 0; i < clients; i++)
fini_workload(w[i]);
free(w);
for (i = 0; i < nr_w_args; i++)
fini_workload(wrk[i]);
free(w_args);
out:
exitcode = EXIT_SUCCESS;
err:
if (is_xe)
xe_device_put(fd);
return exitcode;
}
|