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 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313
|
/**
* \file xf86drm.c
* User-level interface to DRM device
*
* \author Rickard E. (Rik) Faith <faith@valinux.com>
* \author Kevin E. Martin <martin@valinux.com>
*/
/*
* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
* All Rights Reserved.
*
* 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
* PRECISION INSIGHT AND/OR ITS SUPPLIERS 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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <dirent.h>
#include <stddef.h>
#include <fcntl.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#define stat_t struct stat
#include <sys/ioctl.h>
#include <sys/time.h>
#include <stdarg.h>
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#endif
#ifdef MAJOR_IN_SYSMACROS
#include <sys/sysmacros.h>
#endif
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#include <inttypes.h>
#if defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/pciio.h>
#endif
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
/* Not all systems have MAP_FAILED defined */
#ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
#include "xf86drm.h"
#include "libdrm_macros.h"
#include "drm_fourcc.h"
#include "util_math.h"
#ifdef __DragonFly__
#define DRM_MAJOR 145
#endif
#ifdef __NetBSD__
#define DRM_MAJOR 34
#endif
#ifdef __OpenBSD__
#ifdef __i386__
#define DRM_MAJOR 88
#else
#define DRM_MAJOR 87
#endif
#endif /* __OpenBSD__ */
#ifndef DRM_MAJOR
#define DRM_MAJOR 226 /* Linux */
#endif
#if defined(__OpenBSD__) || defined(__DragonFly__)
struct drm_pciinfo {
uint16_t domain;
uint8_t bus;
uint8_t dev;
uint8_t func;
uint16_t vendor_id;
uint16_t device_id;
uint16_t subvendor_id;
uint16_t subdevice_id;
uint8_t revision_id;
};
#define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
#endif
#define DRM_MSG_VERBOSITY 3
#define memclear(s) memset(&s, 0, sizeof(s))
static drmServerInfoPtr drm_server_info;
static bool drmNodeIsDRM(int maj, int min);
static char *drmGetMinorNameForFD(int fd, int type);
#define DRM_MODIFIER(v, f, f_name) \
.modifier = DRM_FORMAT_MOD_##v ## _ ##f, \
.modifier_name = #f_name
#define DRM_MODIFIER_INVALID(v, f_name) \
.modifier = DRM_FORMAT_MOD_INVALID, .modifier_name = #f_name
#define DRM_MODIFIER_LINEAR(v, f_name) \
.modifier = DRM_FORMAT_MOD_LINEAR, .modifier_name = #f_name
/* Intel is abit special as the format doesn't follow other vendors naming
* scheme */
#define DRM_MODIFIER_INTEL(f, f_name) \
.modifier = I915_FORMAT_MOD_##f, .modifier_name = #f_name
struct drmFormatModifierInfo {
uint64_t modifier;
const char *modifier_name;
};
struct drmFormatModifierVendorInfo {
uint8_t vendor;
const char *vendor_name;
};
#include "generated_static_table_fourcc.h"
struct drmVendorInfo {
uint8_t vendor;
char *(*vendor_cb)(uint64_t modifier);
};
struct drmFormatVendorModifierInfo {
uint64_t modifier;
const char *modifier_name;
};
static char *
drmGetFormatModifierNameFromArm(uint64_t modifier);
static char *
drmGetFormatModifierNameFromNvidia(uint64_t modifier);
static char *
drmGetFormatModifierNameFromAmd(uint64_t modifier);
static char *
drmGetFormatModifierNameFromAmlogic(uint64_t modifier);
static char *
drmGetFormatModifierNameFromVivante(uint64_t modifier);
static const struct drmVendorInfo modifier_format_vendor_table[] = {
{ DRM_FORMAT_MOD_VENDOR_ARM, drmGetFormatModifierNameFromArm },
{ DRM_FORMAT_MOD_VENDOR_NVIDIA, drmGetFormatModifierNameFromNvidia },
{ DRM_FORMAT_MOD_VENDOR_AMD, drmGetFormatModifierNameFromAmd },
{ DRM_FORMAT_MOD_VENDOR_AMLOGIC, drmGetFormatModifierNameFromAmlogic },
{ DRM_FORMAT_MOD_VENDOR_VIVANTE, drmGetFormatModifierNameFromVivante },
};
#ifndef AFBC_FORMAT_MOD_MODE_VALUE_MASK
#define AFBC_FORMAT_MOD_MODE_VALUE_MASK 0x000fffffffffffffULL
#endif
static const struct drmFormatVendorModifierInfo arm_mode_value_table[] = {
{ AFBC_FORMAT_MOD_YTR, "YTR" },
{ AFBC_FORMAT_MOD_SPLIT, "SPLIT" },
{ AFBC_FORMAT_MOD_SPARSE, "SPARSE" },
{ AFBC_FORMAT_MOD_CBR, "CBR" },
{ AFBC_FORMAT_MOD_TILED, "TILED" },
{ AFBC_FORMAT_MOD_SC, "SC" },
{ AFBC_FORMAT_MOD_DB, "DB" },
{ AFBC_FORMAT_MOD_BCH, "BCH" },
{ AFBC_FORMAT_MOD_USM, "USM" },
};
static bool
drmGetAfbcFormatModifierNameFromArm(uint64_t modifier, FILE *fp)
{
uint64_t mode_value = modifier & AFBC_FORMAT_MOD_MODE_VALUE_MASK;
uint64_t block_size = mode_value & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK;
const char *block = NULL;
const char *mode = NULL;
bool did_print_mode = false;
/* add block, can only have a (single) block */
switch (block_size) {
case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
block = "16x16";
break;
case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
block = "32x8";
break;
case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
block = "64x4";
break;
case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
block = "32x8_64x4";
break;
}
if (!block) {
return false;
}
fprintf(fp, "BLOCK_SIZE=%s,", block);
/* add mode */
for (unsigned int i = 0; i < ARRAY_SIZE(arm_mode_value_table); i++) {
if (arm_mode_value_table[i].modifier & mode_value) {
mode = arm_mode_value_table[i].modifier_name;
if (!did_print_mode) {
fprintf(fp, "MODE=%s", mode);
did_print_mode = true;
} else {
fprintf(fp, "|%s", mode);
}
}
}
return true;
}
static bool
drmGetAfrcFormatModifierNameFromArm(uint64_t modifier, FILE *fp)
{
bool scan_layout;
for (unsigned int i = 0; i < 2; ++i) {
uint64_t coding_unit_block =
(modifier >> (i * 4)) & AFRC_FORMAT_MOD_CU_SIZE_MASK;
const char *coding_unit_size = NULL;
switch (coding_unit_block) {
case AFRC_FORMAT_MOD_CU_SIZE_16:
coding_unit_size = "CU_16";
break;
case AFRC_FORMAT_MOD_CU_SIZE_24:
coding_unit_size = "CU_24";
break;
case AFRC_FORMAT_MOD_CU_SIZE_32:
coding_unit_size = "CU_32";
break;
}
if (!coding_unit_size) {
if (i == 0) {
return false;
}
break;
}
if (i == 0) {
fprintf(fp, "P0=%s,", coding_unit_size);
} else {
fprintf(fp, "P12=%s,", coding_unit_size);
}
}
scan_layout =
(modifier & AFRC_FORMAT_MOD_LAYOUT_SCAN) == AFRC_FORMAT_MOD_LAYOUT_SCAN;
if (scan_layout) {
fprintf(fp, "SCAN");
} else {
fprintf(fp, "ROT");
}
return true;
}
static char *
drmGetFormatModifierNameFromArm(uint64_t modifier)
{
uint64_t type = (modifier >> 52) & 0xf;
FILE *fp;
size_t size = 0;
char *modifier_name = NULL;
bool result = false;
fp = open_memstream(&modifier_name, &size);
if (!fp)
return NULL;
switch (type) {
case DRM_FORMAT_MOD_ARM_TYPE_AFBC:
result = drmGetAfbcFormatModifierNameFromArm(modifier, fp);
break;
case DRM_FORMAT_MOD_ARM_TYPE_AFRC:
result = drmGetAfrcFormatModifierNameFromArm(modifier, fp);
break;
/* misc type is already handled by the static table */
case DRM_FORMAT_MOD_ARM_TYPE_MISC:
default:
result = false;
break;
}
fclose(fp);
if (!result) {
free(modifier_name);
return NULL;
}
return modifier_name;
}
static char *
drmGetFormatModifierNameFromNvidia(uint64_t modifier)
{
uint64_t height, kind, gen, sector, compression;
height = modifier & 0xf;
kind = (modifier >> 12) & 0xff;
gen = (modifier >> 20) & 0x3;
sector = (modifier >> 22) & 0x1;
compression = (modifier >> 23) & 0x7;
/* just in case there could other simpler modifiers, not yet added, avoid
* testing against TEGRA_TILE */
if ((modifier & 0x10) == 0x10) {
char *mod_nvidia;
asprintf(&mod_nvidia, "BLOCK_LINEAR_2D,HEIGHT=%"PRIu64",KIND=%"PRIu64","
"GEN=%"PRIu64",SECTOR=%"PRIu64",COMPRESSION=%"PRIu64"", height,
kind, gen, sector, compression);
return mod_nvidia;
}
return NULL;
}
static char *
drmGetFormatModifierNameFromAmd(uint64_t modifier)
{
static const char *gfx9_gfx11_tile_strings[32] = {
"LINEAR",
"256B_S",
"256B_D",
"256B_R",
"4KB_Z",
"4KB_S",
"4KB_D",
"4KB_R",
"64KB_Z",
"64KB_S",
"64KB_D",
"64KB_R",
"INVALID12",
"INVALID13",
"INVALID14",
"INVALID15",
"64KB_Z_T",
"64KB_S_T",
"64KB_D_T",
"64KB_R_T",
"4KB_Z_X",
"4KB_S_X",
"4KB_D_X",
"4KB_R_X",
"64KB_Z_X",
"64KB_S_X",
"64KB_D_X",
"64KB_R_X",
"256KB_Z_X",
"256KB_S_X",
"256KB_D_X",
"256KB_R_X",
};
static const char *gfx12_tile_strings[32] = {
"LINEAR",
"256B_2D",
"4KB_2D",
"64KB_2D",
"256KB_2D",
"4KB_3D",
"64KB_3D",
"256KB_3D",
/* other values are unused */
};
uint64_t tile_version = AMD_FMT_MOD_GET(TILE_VERSION, modifier);
FILE *fp;
char *mod_amd = NULL;
size_t size = 0;
fp = open_memstream(&mod_amd, &size);
if (!fp)
return NULL;
switch (tile_version) {
case AMD_FMT_MOD_TILE_VER_GFX9:
fprintf(fp, "GFX9");
break;
case AMD_FMT_MOD_TILE_VER_GFX10:
fprintf(fp, "GFX10");
break;
case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS:
fprintf(fp, "GFX10_RBPLUS");
break;
case AMD_FMT_MOD_TILE_VER_GFX11:
fprintf(fp, "GFX11");
break;
case AMD_FMT_MOD_TILE_VER_GFX12:
fprintf(fp, "GFX12");
break;
default:
fclose(fp);
free(mod_amd);
return NULL;
}
if (tile_version >= AMD_FMT_MOD_TILE_VER_GFX12) {
unsigned tile = AMD_FMT_MOD_GET(TILE, modifier);
fprintf(fp, ",%s", gfx12_tile_strings[tile]);
if (AMD_FMT_MOD_GET(DCC, modifier)) {
fprintf(fp, ",DCC,DCC_MAX_COMPRESSED_BLOCK=%uB",
64 << AMD_FMT_MOD_GET(DCC_MAX_COMPRESSED_BLOCK, modifier));
/* Other DCC fields are unused by GFX12. */
}
} else {
unsigned tile = AMD_FMT_MOD_GET(TILE, modifier);
fprintf(fp, ",%s", gfx9_gfx11_tile_strings[tile]);
/* All *_T and *_X modes are affected by chip-specific fields. */
if (tile >= 16) {
fprintf(fp, ",PIPE_XOR_BITS=%u",
(unsigned)AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier));
switch (tile_version) {
case AMD_FMT_MOD_TILE_VER_GFX9:
fprintf(fp, ",BANK_XOR_BITS=%u",
(unsigned)AMD_FMT_MOD_GET(BANK_XOR_BITS, modifier));
break;
case AMD_FMT_MOD_TILE_VER_GFX10:
/* Nothing else for GFX10. */
break;
case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS:
case AMD_FMT_MOD_TILE_VER_GFX11:
/* This also determines the DCC layout, but DCC is only legal
* with tile=27 and tile=31 (*_R_X modes).
*/
fprintf(fp, ",PACKERS=%u",
(unsigned)AMD_FMT_MOD_GET(PACKERS, modifier));
break;
}
}
if (AMD_FMT_MOD_GET(DCC, modifier)) {
if (tile_version == AMD_FMT_MOD_TILE_VER_GFX9 &&
(AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier) ||
AMD_FMT_MOD_GET(DCC_RETILE, modifier))) {
/* These two only determine the layout of
* the non-displayable DCC plane.
*/
fprintf(fp, ",RB=%u",
(unsigned)AMD_FMT_MOD_GET(RB, modifier));
fprintf(fp, ",PIPE=%u",
(unsigned)AMD_FMT_MOD_GET(PIPE, modifier));
}
fprintf(fp, ",DCC,DCC_MAX_COMPRESSED_BLOCK=%uB",
64 << AMD_FMT_MOD_GET(DCC_MAX_COMPRESSED_BLOCK, modifier));
if (AMD_FMT_MOD_GET(DCC_INDEPENDENT_64B, modifier))
fprintf(fp, ",DCC_INDEPENDENT_64B");
if (AMD_FMT_MOD_GET(DCC_INDEPENDENT_128B, modifier))
fprintf(fp, ",DCC_INDEPENDENT_128B");
if (AMD_FMT_MOD_GET(DCC_CONSTANT_ENCODE, modifier))
fprintf(fp, ",DCC_CONSTANT_ENCODE");
if (AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier))
fprintf(fp, ",DCC_PIPE_ALIGN");
if (AMD_FMT_MOD_GET(DCC_RETILE, modifier))
fprintf(fp, ",DCC_RETILE");
}
}
fclose(fp);
return mod_amd;
}
static char *
drmGetFormatModifierNameFromAmlogic(uint64_t modifier)
{
uint64_t layout = modifier & 0xff;
uint64_t options = (modifier >> 8) & 0xff;
char *mod_amlogic = NULL;
const char *layout_str;
const char *opts_str;
switch (layout) {
case AMLOGIC_FBC_LAYOUT_BASIC:
layout_str = "BASIC";
break;
case AMLOGIC_FBC_LAYOUT_SCATTER:
layout_str = "SCATTER";
break;
default:
layout_str = "INVALID_LAYOUT";
break;
}
if (options & AMLOGIC_FBC_OPTION_MEM_SAVING)
opts_str = "MEM_SAVING";
else
opts_str = "0";
asprintf(&mod_amlogic, "FBC,LAYOUT=%s,OPTIONS=%s", layout_str, opts_str);
return mod_amlogic;
}
static char *
drmGetFormatModifierNameFromVivante(uint64_t modifier)
{
const char *color_tiling, *tile_status, *compression;
char *mod_vivante = NULL;
switch (modifier & VIVANTE_MOD_TS_MASK) {
case 0:
tile_status = "";
break;
case VIVANTE_MOD_TS_64_4:
tile_status = ",TS=64B_4";
break;
case VIVANTE_MOD_TS_64_2:
tile_status = ",TS=64B_2";
break;
case VIVANTE_MOD_TS_128_4:
tile_status = ",TS=128B_4";
break;
case VIVANTE_MOD_TS_256_4:
tile_status = ",TS=256B_4";
break;
default:
tile_status = ",TS=UNKNOWN";
break;
}
switch (modifier & VIVANTE_MOD_COMP_MASK) {
case 0:
compression = "";
break;
case VIVANTE_MOD_COMP_DEC400:
compression = ",COMP=DEC400";
break;
default:
compression = ",COMP=UNKNOWN";
break;
}
switch (modifier & ~VIVANTE_MOD_EXT_MASK) {
case 0:
color_tiling = "LINEAR";
break;
case DRM_FORMAT_MOD_VIVANTE_TILED:
color_tiling = "TILED";
break;
case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
color_tiling = "SUPER_TILED";
break;
case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
color_tiling = "SPLIT_TILED";
break;
case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
color_tiling = "SPLIT_SUPER_TILED";
break;
default:
color_tiling = "UNKNOWN";
break;
}
asprintf(&mod_vivante, "%s%s%s", color_tiling, tile_status, compression);
return mod_vivante;
}
static unsigned log2_int(unsigned x)
{
unsigned l;
if (x < 2) {
return 0;
}
for (l = 2; ; l++) {
if ((unsigned)(1 << l) > x) {
return l - 1;
}
}
return 0;
}
drm_public void drmSetServerInfo(drmServerInfoPtr info)
{
drm_server_info = info;
}
/**
* Output a message to stderr.
*
* \param format printf() like format string.
*
* \internal
* This function is a wrapper around vfprintf().
*/
static int DRM_PRINTFLIKE(1, 0)
drmDebugPrint(const char *format, va_list ap)
{
return vfprintf(stderr, format, ap);
}
drm_public void
drmMsg(const char *format, ...)
{
va_list ap;
const char *env;
if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
(drm_server_info && drm_server_info->debug_print))
{
va_start(ap, format);
if (drm_server_info) {
drm_server_info->debug_print(format,ap);
} else {
drmDebugPrint(format, ap);
}
va_end(ap);
}
}
static void *drmHashTable = NULL; /* Context switch callbacks */
drm_public void *drmGetHashTable(void)
{
return drmHashTable;
}
drm_public void *drmMalloc(int size)
{
return calloc(1, size);
}
drm_public void drmFree(void *pt)
{
free(pt);
}
/**
* Call ioctl, restarting if it is interrupted
*/
drm_public int
drmIoctl(int fd, unsigned long request, void *arg)
{
int ret;
do {
ret = ioctl(fd, request, arg);
} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
return ret;
}
static unsigned long drmGetKeyFromFd(int fd)
{
stat_t st;
st.st_rdev = 0;
fstat(fd, &st);
return st.st_rdev;
}
drm_public drmHashEntry *drmGetEntry(int fd)
{
unsigned long key = drmGetKeyFromFd(fd);
void *value;
drmHashEntry *entry;
if (!drmHashTable)
drmHashTable = drmHashCreate();
if (drmHashLookup(drmHashTable, key, &value)) {
entry = drmMalloc(sizeof(*entry));
entry->fd = fd;
entry->f = NULL;
entry->tagTable = drmHashCreate();
drmHashInsert(drmHashTable, key, entry);
} else {
entry = value;
}
return entry;
}
/**
* Compare two busid strings
*
* \param first
* \param second
*
* \return 1 if matched.
*
* \internal
* This function compares two bus ID strings. It understands the older
* PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
* domain, b is bus, d is device, f is function.
*/
static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
{
/* First, check if the IDs are exactly the same */
if (strcasecmp(id1, id2) == 0)
return 1;
/* Try to match old/new-style PCI bus IDs. */
if (strncasecmp(id1, "pci", 3) == 0) {
unsigned int o1, b1, d1, f1;
unsigned int o2, b2, d2, f2;
int ret;
ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
if (ret != 4) {
o1 = 0;
ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
if (ret != 3)
return 0;
}
ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
if (ret != 4) {
o2 = 0;
ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
if (ret != 3)
return 0;
}
/* If domains aren't properly supported by the kernel interface,
* just ignore them, which sucks less than picking a totally random
* card with "open by name"
*/
if (!pci_domain_ok)
o1 = o2 = 0;
if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
return 0;
else
return 1;
}
return 0;
}
/**
* Handles error checking for chown call.
*
* \param path to file.
* \param id of the new owner.
* \param id of the new group.
*
* \return zero if success or -1 if failure.
*
* \internal
* Checks for failure. If failure was caused by signal call chown again.
* If any other failure happened then it will output error message using
* drmMsg() call.
*/
#if !UDEV
static int chown_check_return(const char *path, uid_t owner, gid_t group)
{
int rv;
do {
rv = chown(path, owner, group);
} while (rv != 0 && errno == EINTR);
if (rv == 0)
return 0;
drmMsg("Failed to change owner or group for file %s! %d: %s\n",
path, errno, strerror(errno));
return -1;
}
#endif
static const char *drmGetDeviceName(int type)
{
switch (type) {
case DRM_NODE_PRIMARY:
return DRM_DEV_NAME;
case DRM_NODE_RENDER:
return DRM_RENDER_DEV_NAME;
}
return NULL;
}
/**
* Open the DRM device, creating it if necessary.
*
* \param dev major and minor numbers of the device.
* \param minor minor number of the device.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* Assembles the device name from \p minor and opens it, creating the device
* special file node with the major and minor numbers specified by \p dev and
* parent directory if necessary and was called by root.
*/
static int drmOpenDevice(dev_t dev, int minor, int type)
{
stat_t st;
const char *dev_name = drmGetDeviceName(type);
char buf[DRM_NODE_NAME_MAX];
int fd;
mode_t devmode = DRM_DEV_MODE, serv_mode;
gid_t serv_group;
#if !UDEV
int isroot = !geteuid();
uid_t user = DRM_DEV_UID;
gid_t group = DRM_DEV_GID;
#endif
if (!dev_name)
return -EINVAL;
sprintf(buf, dev_name, DRM_DIR_NAME, minor);
drmMsg("drmOpenDevice: node name is %s\n", buf);
if (drm_server_info && drm_server_info->get_perms) {
drm_server_info->get_perms(&serv_group, &serv_mode);
devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
}
#if !UDEV
if (stat(DRM_DIR_NAME, &st)) {
if (!isroot)
return DRM_ERR_NOT_ROOT;
mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
}
/* Check if the device node exists and create it if necessary. */
if (stat(buf, &st)) {
if (!isroot)
return DRM_ERR_NOT_ROOT;
remove(buf);
mknod(buf, S_IFCHR | devmode, dev);
}
if (drm_server_info && drm_server_info->get_perms) {
group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
chown_check_return(buf, user, group);
chmod(buf, devmode);
}
#else
/* if we modprobed then wait for udev */
{
int udev_count = 0;
wait_for_udev:
if (stat(DRM_DIR_NAME, &st)) {
usleep(20);
udev_count++;
if (udev_count == 50)
return -1;
goto wait_for_udev;
}
if (stat(buf, &st)) {
usleep(20);
udev_count++;
if (udev_count == 50)
return -1;
goto wait_for_udev;
}
}
#endif
fd = open(buf, O_RDWR | O_CLOEXEC);
drmMsg("drmOpenDevice: open result is %d, (%s)\n",
fd, fd < 0 ? strerror(errno) : "OK");
if (fd >= 0)
return fd;
#if !UDEV
/* Check if the device node is not what we expect it to be, and recreate it
* and try again if so.
*/
if (st.st_rdev != dev) {
if (!isroot)
return DRM_ERR_NOT_ROOT;
remove(buf);
mknod(buf, S_IFCHR | devmode, dev);
if (drm_server_info && drm_server_info->get_perms) {
chown_check_return(buf, user, group);
chmod(buf, devmode);
}
}
fd = open(buf, O_RDWR | O_CLOEXEC);
drmMsg("drmOpenDevice: open result is %d, (%s)\n",
fd, fd < 0 ? strerror(errno) : "OK");
if (fd >= 0)
return fd;
drmMsg("drmOpenDevice: Open failed\n");
remove(buf);
#endif
return -errno;
}
/**
* Open the DRM device
*
* \param minor device minor number.
* \param create allow to create the device if set.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* Calls drmOpenDevice() if \p create is set, otherwise assembles the device
* name from \p minor and opens it.
*/
static int drmOpenMinor(int minor, int create, int type)
{
int fd;
char buf[DRM_NODE_NAME_MAX];
const char *dev_name = drmGetDeviceName(type);
if (create)
return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
if (!dev_name)
return -EINVAL;
sprintf(buf, dev_name, DRM_DIR_NAME, minor);
if ((fd = open(buf, O_RDWR | O_CLOEXEC)) >= 0)
return fd;
return -errno;
}
/**
* Determine whether the DRM kernel driver has been loaded.
*
* \return 1 if the DRM driver is loaded, 0 otherwise.
*
* \deprecated
* This function doesn't work when a device goes away (as is often the case
* with simpledrm). drmGetDevices2 should be used instead to enumerate DRM
* devices.
*
* \internal
* Determine the presence of the kernel driver by attempting to open the 0
* minor and get version information. For backward compatibility with older
* Linux implementations, /proc/dri is also checked.
*/
drm_public int drmAvailable(void)
{
drmVersionPtr version;
int retval = 0;
int fd;
if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
#ifdef __linux__
/* Try proc for backward Linux compatibility */
if (!access("/proc/dri/0", R_OK))
return 1;
#endif
return 0;
}
if ((version = drmGetVersion(fd))) {
retval = 1;
drmFreeVersion(version);
}
close(fd);
return retval;
}
static int drmGetMinorBase(int type)
{
switch (type) {
case DRM_NODE_PRIMARY:
return 0;
case DRM_NODE_RENDER:
return 128;
default:
return -1;
};
}
static int drmGetMinorType(int major, int minor)
{
#ifdef __FreeBSD__
char name[SPECNAMELEN];
int id;
if (!devname_r(makedev(major, minor), S_IFCHR, name, sizeof(name)))
return -1;
if (sscanf(name, "drm/%d", &id) != 1) {
// If not in /dev/drm/ we have the type in the name
if (sscanf(name, "dri/card%d\n", &id) >= 1)
return DRM_NODE_PRIMARY;
else if (sscanf(name, "dri/renderD%d\n", &id) >= 1)
return DRM_NODE_RENDER;
return -1;
}
minor = id;
#endif
char path[DRM_NODE_NAME_MAX];
const char *dev_name;
int i;
for (i = DRM_NODE_PRIMARY; i < DRM_NODE_MAX; i++) {
dev_name = drmGetDeviceName(i);
if (!dev_name)
continue;
snprintf(path, sizeof(path), dev_name, DRM_DIR_NAME, minor);
if (!access(path, F_OK))
return i;
}
return -1;
}
static const char *drmGetMinorName(int type)
{
switch (type) {
case DRM_NODE_PRIMARY:
return DRM_PRIMARY_MINOR_NAME;
case DRM_NODE_RENDER:
return DRM_RENDER_MINOR_NAME;
default:
return NULL;
}
}
/**
* Open the device by bus ID.
*
* \param busid bus ID.
* \param type device node type.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* This function attempts to open every possible minor (up to DRM_MAX_MINOR),
* comparing the device bus ID with the one supplied.
*
* \sa drmOpenMinor() and drmGetBusid().
*/
static int drmOpenByBusid(const char *busid, int type)
{
int i, pci_domain_ok = 1;
int fd;
const char *buf;
drmSetVersion sv;
int base = drmGetMinorBase(type);
if (base < 0)
return -1;
drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
for (i = base; i < base + DRM_MAX_MINOR; i++) {
fd = drmOpenMinor(i, 1, type);
drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
if (fd >= 0) {
/* We need to try for 1.4 first for proper PCI domain support
* and if that fails, we know the kernel is busted
*/
sv.drm_di_major = 1;
sv.drm_di_minor = 4;
sv.drm_dd_major = -1; /* Don't care */
sv.drm_dd_minor = -1; /* Don't care */
if (drmSetInterfaceVersion(fd, &sv)) {
#ifndef __alpha__
pci_domain_ok = 0;
#endif
sv.drm_di_major = 1;
sv.drm_di_minor = 1;
sv.drm_dd_major = -1; /* Don't care */
sv.drm_dd_minor = -1; /* Don't care */
drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
drmSetInterfaceVersion(fd, &sv);
}
buf = drmGetBusid(fd);
drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
drmFreeBusid(buf);
return fd;
}
if (buf)
drmFreeBusid(buf);
close(fd);
}
}
return -1;
}
/**
* Open the device by name.
*
* \param name driver name.
* \param type the device node type.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* This function opens the first minor number that matches the driver name and
* isn't already in use. If it's in use it then it will already have a bus ID
* assigned.
*
* \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
*/
static int drmOpenByName(const char *name, int type)
{
int i;
int fd;
drmVersionPtr version;
char * id;
int base = drmGetMinorBase(type);
if (base < 0)
return -1;
/*
* Open the first minor number that matches the driver name and isn't
* already in use. If it's in use it will have a busid assigned already.
*/
for (i = base; i < base + DRM_MAX_MINOR; i++) {
if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
if ((version = drmGetVersion(fd))) {
if (!strcmp(version->name, name)) {
drmFreeVersion(version);
id = drmGetBusid(fd);
drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
if (!id || !*id) {
if (id)
drmFreeBusid(id);
return fd;
} else {
drmFreeBusid(id);
}
} else {
drmFreeVersion(version);
}
}
close(fd);
}
}
#ifdef __linux__
/* Backward-compatibility /proc support */
for (i = 0; i < 8; i++) {
char proc_name[64], buf[512];
char *driver, *pt, *devstring;
int retcode;
sprintf(proc_name, "/proc/dri/%d/name", i);
if ((fd = open(proc_name, O_RDONLY)) >= 0) {
retcode = read(fd, buf, sizeof(buf)-1);
close(fd);
if (retcode) {
buf[retcode-1] = '\0';
for (driver = pt = buf; *pt && *pt != ' '; ++pt)
;
if (*pt) { /* Device is next */
*pt = '\0';
if (!strcmp(driver, name)) { /* Match */
for (devstring = ++pt; *pt && *pt != ' '; ++pt)
;
if (*pt) { /* Found busid */
return drmOpenByBusid(++pt, type);
} else { /* No busid */
return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
}
}
}
}
}
}
#endif
return -1;
}
/**
* Open the DRM device.
*
* Looks up the specified name and bus ID, and opens the device found. The
* entry in /dev/dri is created if necessary and if called by root.
*
* \param name driver name. Not referenced if bus ID is supplied.
* \param busid bus ID. Zero if not known.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
* otherwise.
*/
drm_public int drmOpen(const char *name, const char *busid)
{
return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
}
/**
* Open the DRM device with specified type.
*
* Looks up the specified name and bus ID, and opens the device found. The
* entry in /dev/dri is created if necessary and if called by root.
*
* \param name driver name. Not referenced if bus ID is supplied.
* \param busid bus ID. Zero if not known.
* \param type the device node type to open, PRIMARY or RENDER
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
* otherwise.
*/
drm_public int drmOpenWithType(const char *name, const char *busid, int type)
{
if (name != NULL && drm_server_info &&
drm_server_info->load_module && !drmAvailable()) {
/* try to load the kernel module */
if (!drm_server_info->load_module(name)) {
drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
return -1;
}
}
if (busid) {
int fd = drmOpenByBusid(busid, type);
if (fd >= 0)
return fd;
}
if (name)
return drmOpenByName(name, type);
return -1;
}
drm_public int drmOpenControl(int minor)
{
return -EINVAL;
}
drm_public int drmOpenRender(int minor)
{
return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
}
/**
* Free the version information returned by drmGetVersion().
*
* \param v pointer to the version information.
*
* \internal
* It frees the memory pointed by \p %v as well as all the non-null strings
* pointers in it.
*/
drm_public void drmFreeVersion(drmVersionPtr v)
{
if (!v)
return;
drmFree(v->name);
drmFree(v->date);
drmFree(v->desc);
drmFree(v);
}
/**
* Free the non-public version information returned by the kernel.
*
* \param v pointer to the version information.
*
* \internal
* Used by drmGetVersion() to free the memory pointed by \p %v as well as all
* the non-null strings pointers in it.
*/
static void drmFreeKernelVersion(drm_version_t *v)
{
if (!v)
return;
drmFree(v->name);
drmFree(v->date);
drmFree(v->desc);
drmFree(v);
}
/**
* Copy version information.
*
* \param d destination pointer.
* \param s source pointer.
*
* \internal
* Used by drmGetVersion() to translate the information returned by the ioctl
* interface in a private structure into the public structure counterpart.
*/
static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
{
d->version_major = s->version_major;
d->version_minor = s->version_minor;
d->version_patchlevel = s->version_patchlevel;
d->name_len = s->name_len;
d->name = s->name ? strdup(s->name) : NULL;
d->date_len = s->date_len;
d->date = s->date ? strdup(s->date) : NULL;
d->desc_len = s->desc_len;
d->desc = s->desc ? strdup(s->desc) : NULL;
}
/**
* Query the driver version information.
*
* \param fd file descriptor.
*
* \return pointer to a drmVersion structure which should be freed with
* drmFreeVersion().
*
* \note Similar information is available via /proc/dri.
*
* \internal
* It gets the version information via successive DRM_IOCTL_VERSION ioctls,
* first with zeros to get the string lengths, and then the actually strings.
* It also null-terminates them since they might not be already.
*/
drm_public drmVersionPtr drmGetVersion(int fd)
{
drmVersionPtr retval;
drm_version_t *version = drmMalloc(sizeof(*version));
if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
drmFreeKernelVersion(version);
return NULL;
}
if (version->name_len)
version->name = drmMalloc(version->name_len + 1);
if (version->date_len)
version->date = drmMalloc(version->date_len + 1);
if (version->desc_len)
version->desc = drmMalloc(version->desc_len + 1);
if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
drmFreeKernelVersion(version);
return NULL;
}
/* The results might not be null-terminated strings, so terminate them. */
if (version->name_len) version->name[version->name_len] = '\0';
if (version->date_len) version->date[version->date_len] = '\0';
if (version->desc_len) version->desc[version->desc_len] = '\0';
retval = drmMalloc(sizeof(*retval));
drmCopyVersion(retval, version);
drmFreeKernelVersion(version);
return retval;
}
/**
* Get version information for the DRM user space library.
*
* This version number is driver independent.
*
* \param fd file descriptor.
*
* \return version information.
*
* \internal
* This function allocates and fills a drm_version structure with a hard coded
* version number.
*/
drm_public drmVersionPtr drmGetLibVersion(int fd)
{
drm_version_t *version = drmMalloc(sizeof(*version));
/* Version history:
* NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
* revision 1.0.x = original DRM interface with no drmGetLibVersion
* entry point and many drm<Device> extensions
* revision 1.1.x = added drmCommand entry points for device extensions
* added drmGetLibVersion to identify libdrm.a version
* revision 1.2.x = added drmSetInterfaceVersion
* modified drmOpen to handle both busid and name
* revision 1.3.x = added server + memory manager
*/
version->version_major = 1;
version->version_minor = 3;
version->version_patchlevel = 0;
return (drmVersionPtr)version;
}
drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
{
struct drm_get_cap cap;
int ret;
memclear(cap);
cap.capability = capability;
ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
if (ret)
return ret;
*value = cap.value;
return 0;
}
drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
{
struct drm_set_client_cap cap;
memclear(cap);
cap.capability = capability;
cap.value = value;
return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
}
/**
* Free the bus ID information.
*
* \param busid bus ID information string as given by drmGetBusid().
*
* \internal
* This function is just frees the memory pointed by \p busid.
*/
drm_public void drmFreeBusid(const char *busid)
{
drmFree((void *)busid);
}
/**
* Get the bus ID of the device.
*
* \param fd file descriptor.
*
* \return bus ID string.
*
* \internal
* This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
* get the string length and data, passing the arguments in a drm_unique
* structure.
*/
drm_public char *drmGetBusid(int fd)
{
drm_unique_t u;
memclear(u);
if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
return NULL;
u.unique = drmMalloc(u.unique_len + 1);
if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
drmFree(u.unique);
return NULL;
}
u.unique[u.unique_len] = '\0';
return u.unique;
}
/**
* Set the bus ID of the device.
*
* \param fd file descriptor.
* \param busid bus ID string.
*
* \return zero on success, negative on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
* the arguments in a drm_unique structure.
*/
drm_public int drmSetBusid(int fd, const char *busid)
{
drm_unique_t u;
memclear(u);
u.unique = (char *)busid;
u.unique_len = strlen(busid);
if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
return -errno;
}
return 0;
}
drm_public int drmGetMagic(int fd, drm_magic_t * magic)
{
drm_auth_t auth;
memclear(auth);
*magic = 0;
if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
return -errno;
*magic = auth.magic;
return 0;
}
drm_public int drmAuthMagic(int fd, drm_magic_t magic)
{
drm_auth_t auth;
memclear(auth);
auth.magic = magic;
if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
return -errno;
return 0;
}
/**
* Specifies a range of memory that is available for mapping by a
* non-root process.
*
* \param fd file descriptor.
* \param offset usually the physical address. The actual meaning depends of
* the \p type parameter. See below.
* \param size of the memory in bytes.
* \param type type of the memory to be mapped.
* \param flags combination of several flags to modify the function actions.
* \param handle will be set to a value that may be used as the offset
* parameter for mmap().
*
* \return zero on success or a negative value on error.
*
* \par Mapping the frame buffer
* For the frame buffer
* - \p offset will be the physical address of the start of the frame buffer,
* - \p size will be the size of the frame buffer in bytes, and
* - \p type will be DRM_FRAME_BUFFER.
*
* \par
* The area mapped will be uncached. If MTRR support is available in the
* kernel, the frame buffer area will be set to write combining.
*
* \par Mapping the MMIO register area
* For the MMIO register area,
* - \p offset will be the physical address of the start of the register area,
* - \p size will be the size of the register area bytes, and
* - \p type will be DRM_REGISTERS.
* \par
* The area mapped will be uncached.
*
* \par Mapping the SAREA
* For the SAREA,
* - \p offset will be ignored and should be set to zero,
* - \p size will be the desired size of the SAREA in bytes,
* - \p type will be DRM_SHM.
*
* \par
* A shared memory area of the requested size will be created and locked in
* kernel memory. This area may be mapped into client-space by using the handle
* returned.
*
* \note May only be called by root.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
* the arguments in a drm_map structure.
*/
drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
drmMapFlags flags, drm_handle_t *handle)
{
drm_map_t map;
memclear(map);
map.offset = offset;
map.size = size;
map.type = (enum drm_map_type)type;
map.flags = (enum drm_map_flags)flags;
if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
return -errno;
if (handle)
*handle = (drm_handle_t)(uintptr_t)map.handle;
return 0;
}
drm_public int drmRmMap(int fd, drm_handle_t handle)
{
drm_map_t map;
memclear(map);
map.handle = (void *)(uintptr_t)handle;
if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
return -errno;
return 0;
}
/**
* Make buffers available for DMA transfers.
*
* \param fd file descriptor.
* \param count number of buffers.
* \param size size of each buffer.
* \param flags buffer allocation flags.
* \param agp_offset offset in the AGP aperture
*
* \return number of buffers allocated, negative on error.
*
* \internal
* This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
*
* \sa drm_buf_desc.
*/
drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
int agp_offset)
{
drm_buf_desc_t request;
memclear(request);
request.count = count;
request.size = size;
request.flags = (int)flags;
request.agp_start = agp_offset;
if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
return -errno;
return request.count;
}
drm_public int drmMarkBufs(int fd, double low, double high)
{
drm_buf_info_t info;
int i;
memclear(info);
if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
return -EINVAL;
if (!info.count)
return -EINVAL;
if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
return -ENOMEM;
if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
int retval = -errno;
drmFree(info.list);
return retval;
}
for (i = 0; i < info.count; i++) {
info.list[i].low_mark = low * info.list[i].count;
info.list[i].high_mark = high * info.list[i].count;
if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
int retval = -errno;
drmFree(info.list);
return retval;
}
}
drmFree(info.list);
return 0;
}
/**
* Free buffers.
*
* \param fd file descriptor.
* \param count number of buffers to free.
* \param list list of buffers to be freed.
*
* \return zero on success, or a negative value on failure.
*
* \note This function is primarily used for debugging.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
* the arguments in a drm_buf_free structure.
*/
drm_public int drmFreeBufs(int fd, int count, int *list)
{
drm_buf_free_t request;
memclear(request);
request.count = count;
request.list = list;
if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
return -errno;
return 0;
}
/**
* Close the device.
*
* \param fd file descriptor.
*
* \internal
* This function closes the file descriptor.
*/
drm_public int drmClose(int fd)
{
unsigned long key = drmGetKeyFromFd(fd);
drmHashEntry *entry = drmGetEntry(fd);
drmHashDestroy(entry->tagTable);
entry->fd = 0;
entry->f = NULL;
entry->tagTable = NULL;
drmHashDelete(drmHashTable, key);
drmFree(entry);
return close(fd);
}
/**
* Map a region of memory.
*
* \param fd file descriptor.
* \param handle handle returned by drmAddMap().
* \param size size in bytes. Must match the size used by drmAddMap().
* \param address will contain the user-space virtual address where the mapping
* begins.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper for mmap().
*/
drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
drmAddressPtr address)
{
static unsigned long pagesize_mask = 0;
if (fd < 0)
return -EINVAL;
if (!pagesize_mask)
pagesize_mask = getpagesize() - 1;
size = (size + pagesize_mask) & ~pagesize_mask;
*address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
if (*address == MAP_FAILED)
return -errno;
return 0;
}
/**
* Unmap mappings obtained with drmMap().
*
* \param address address as given by drmMap().
* \param size size in bytes. Must match the size used by drmMap().
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper for munmap().
*/
drm_public int drmUnmap(drmAddress address, drmSize size)
{
return drm_munmap(address, size);
}
drm_public drmBufInfoPtr drmGetBufInfo(int fd)
{
drm_buf_info_t info;
drmBufInfoPtr retval;
int i;
memclear(info);
if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
return NULL;
if (info.count) {
if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
return NULL;
if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
drmFree(info.list);
return NULL;
}
retval = drmMalloc(sizeof(*retval));
retval->count = info.count;
if (!(retval->list = drmMalloc(info.count * sizeof(*retval->list)))) {
drmFree(retval);
drmFree(info.list);
return NULL;
}
for (i = 0; i < info.count; i++) {
retval->list[i].count = info.list[i].count;
retval->list[i].size = info.list[i].size;
retval->list[i].low_mark = info.list[i].low_mark;
retval->list[i].high_mark = info.list[i].high_mark;
}
drmFree(info.list);
return retval;
}
return NULL;
}
/**
* Map all DMA buffers into client-virtual space.
*
* \param fd file descriptor.
*
* \return a pointer to a ::drmBufMap structure.
*
* \note The client may not use these buffers until obtaining buffer indices
* with drmDMA().
*
* \internal
* This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
* information about the buffers in a drm_buf_map structure into the
* client-visible data structures.
*/
drm_public drmBufMapPtr drmMapBufs(int fd)
{
drm_buf_map_t bufs;
drmBufMapPtr retval;
int i;
memclear(bufs);
if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
return NULL;
if (!bufs.count)
return NULL;
if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
return NULL;
if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
drmFree(bufs.list);
return NULL;
}
retval = drmMalloc(sizeof(*retval));
retval->count = bufs.count;
retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
for (i = 0; i < bufs.count; i++) {
retval->list[i].idx = bufs.list[i].idx;
retval->list[i].total = bufs.list[i].total;
retval->list[i].used = 0;
retval->list[i].address = bufs.list[i].address;
}
drmFree(bufs.list);
return retval;
}
/**
* Unmap buffers allocated with drmMapBufs().
*
* \return zero on success, or negative value on failure.
*
* \internal
* Calls munmap() for every buffer stored in \p bufs and frees the
* memory allocated by drmMapBufs().
*/
drm_public int drmUnmapBufs(drmBufMapPtr bufs)
{
int i;
for (i = 0; i < bufs->count; i++) {
drm_munmap(bufs->list[i].address, bufs->list[i].total);
}
drmFree(bufs->list);
drmFree(bufs);
return 0;
}
#define DRM_DMA_RETRY 16
/**
* Reserve DMA buffers.
*
* \param fd file descriptor.
* \param request
*
* \return zero on success, or a negative value on failure.
*
* \internal
* Assemble the arguments into a drm_dma structure and keeps issuing the
* DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
*/
drm_public int drmDMA(int fd, drmDMAReqPtr request)
{
drm_dma_t dma;
int ret, i = 0;
dma.context = request->context;
dma.send_count = request->send_count;
dma.send_indices = request->send_list;
dma.send_sizes = request->send_sizes;
dma.flags = (enum drm_dma_flags)request->flags;
dma.request_count = request->request_count;
dma.request_size = request->request_size;
dma.request_indices = request->request_list;
dma.request_sizes = request->request_sizes;
dma.granted_count = 0;
do {
ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
} while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
if ( ret == 0 ) {
request->granted_count = dma.granted_count;
return 0;
} else {
return -errno;
}
}
/**
* Obtain heavyweight hardware lock.
*
* \param fd file descriptor.
* \param context context.
* \param flags flags that determine the state of the hardware when the function
* returns.
*
* \return always zero.
*
* \internal
* This function translates the arguments into a drm_lock structure and issue
* the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
*/
drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
{
drm_lock_t lock;
memclear(lock);
lock.context = context;
lock.flags = 0;
if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
;
return 0;
}
/**
* Release the hardware lock.
*
* \param fd file descriptor.
* \param context context.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
* argument in a drm_lock structure.
*/
drm_public int drmUnlock(int fd, drm_context_t context)
{
drm_lock_t lock;
memclear(lock);
lock.context = context;
return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
}
drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
{
drm_ctx_res_t res;
drm_ctx_t *list;
drm_context_t * retval;
int i;
memclear(res);
if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
return NULL;
if (!res.count)
return NULL;
if (!(list = drmMalloc(res.count * sizeof(*list))))
return NULL;
if (!(retval = drmMalloc(res.count * sizeof(*retval))))
goto err_free_list;
res.contexts = list;
if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
goto err_free_context;
for (i = 0; i < res.count; i++)
retval[i] = list[i].handle;
drmFree(list);
*count = res.count;
return retval;
err_free_list:
drmFree(list);
err_free_context:
drmFree(retval);
return NULL;
}
drm_public void drmFreeReservedContextList(drm_context_t *pt)
{
drmFree(pt);
}
/**
* Create context.
*
* Used by the X server during GLXContext initialization. This causes
* per-context kernel-level resources to be allocated.
*
* \param fd file descriptor.
* \param handle is set on success. To be used by the client when requesting DMA
* dispatch with drmDMA().
*
* \return zero on success, or a negative value on failure.
*
* \note May only be called by root.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
* argument in a drm_ctx structure.
*/
drm_public int drmCreateContext(int fd, drm_context_t *handle)
{
drm_ctx_t ctx;
memclear(ctx);
if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
return -errno;
*handle = ctx.handle;
return 0;
}
drm_public int drmSwitchToContext(int fd, drm_context_t context)
{
drm_ctx_t ctx;
memclear(ctx);
ctx.handle = context;
if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
return -errno;
return 0;
}
drm_public int drmSetContextFlags(int fd, drm_context_t context,
drm_context_tFlags flags)
{
drm_ctx_t ctx;
/*
* Context preserving means that no context switches are done between DMA
* buffers from one context and the next. This is suitable for use in the
* X server (which promises to maintain hardware context), or in the
* client-side library when buffers are swapped on behalf of two threads.
*/
memclear(ctx);
ctx.handle = context;
if (flags & DRM_CONTEXT_PRESERVED)
ctx.flags |= _DRM_CONTEXT_PRESERVED;
if (flags & DRM_CONTEXT_2DONLY)
ctx.flags |= _DRM_CONTEXT_2DONLY;
if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
return -errno;
return 0;
}
drm_public int drmGetContextFlags(int fd, drm_context_t context,
drm_context_tFlagsPtr flags)
{
drm_ctx_t ctx;
memclear(ctx);
ctx.handle = context;
if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
return -errno;
*flags = 0;
if (ctx.flags & _DRM_CONTEXT_PRESERVED)
*flags |= DRM_CONTEXT_PRESERVED;
if (ctx.flags & _DRM_CONTEXT_2DONLY)
*flags |= DRM_CONTEXT_2DONLY;
return 0;
}
/**
* Destroy context.
*
* Free any kernel-level resources allocated with drmCreateContext() associated
* with the context.
*
* \param fd file descriptor.
* \param handle handle given by drmCreateContext().
*
* \return zero on success, or a negative value on failure.
*
* \note May only be called by root.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
* argument in a drm_ctx structure.
*/
drm_public int drmDestroyContext(int fd, drm_context_t handle)
{
drm_ctx_t ctx;
memclear(ctx);
ctx.handle = handle;
if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
return -errno;
return 0;
}
drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
{
drm_draw_t draw;
memclear(draw);
if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
return -errno;
*handle = draw.handle;
return 0;
}
drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
{
drm_draw_t draw;
memclear(draw);
draw.handle = handle;
if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
return -errno;
return 0;
}
drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
drm_drawable_info_type_t type,
unsigned int num, void *data)
{
drm_update_draw_t update;
memclear(update);
update.handle = handle;
update.type = type;
update.num = num;
update.data = (unsigned long long)(unsigned long)data;
if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
return -errno;
return 0;
}
drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
uint64_t *ns)
{
struct drm_crtc_get_sequence get_seq;
int ret;
memclear(get_seq);
get_seq.crtc_id = crtcId;
ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
if (ret)
return ret;
if (sequence)
*sequence = get_seq.sequence;
if (ns)
*ns = get_seq.sequence_ns;
return 0;
}
drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
uint64_t sequence,
uint64_t *sequence_queued,
uint64_t user_data)
{
struct drm_crtc_queue_sequence queue_seq;
int ret;
memclear(queue_seq);
queue_seq.crtc_id = crtcId;
queue_seq.flags = flags;
queue_seq.sequence = sequence;
queue_seq.user_data = user_data;
ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
if (ret == 0 && sequence_queued)
*sequence_queued = queue_seq.sequence;
return ret;
}
/**
* Acquire the AGP device.
*
* Must be called before any of the other AGP related calls.
*
* \param fd file descriptor.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
*/
drm_public int drmAgpAcquire(int fd)
{
if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
return -errno;
return 0;
}
/**
* Release the AGP device.
*
* \param fd file descriptor.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
*/
drm_public int drmAgpRelease(int fd)
{
if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
return -errno;
return 0;
}
/**
* Set the AGP mode.
*
* \param fd file descriptor.
* \param mode AGP mode.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
* argument in a drm_agp_mode structure.
*/
drm_public int drmAgpEnable(int fd, unsigned long mode)
{
drm_agp_mode_t m;
memclear(m);
m.mode = mode;
if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
return -errno;
return 0;
}
/**
* Allocate a chunk of AGP memory.
*
* \param fd file descriptor.
* \param size requested memory size in bytes. Will be rounded to page boundary.
* \param type type of memory to allocate.
* \param address if not zero, will be set to the physical address of the
* allocated memory.
* \param handle on success will be set to a handle of the allocated memory.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
* arguments in a drm_agp_buffer structure.
*/
drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
unsigned long *address, drm_handle_t *handle)
{
drm_agp_buffer_t b;
memclear(b);
*handle = DRM_AGP_NO_HANDLE;
b.size = size;
b.type = type;
if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
return -errno;
if (address != 0UL)
*address = b.physical;
*handle = b.handle;
return 0;
}
/**
* Free a chunk of AGP memory.
*
* \param fd file descriptor.
* \param handle handle to the allocated memory, as given by drmAgpAllocate().
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
* argument in a drm_agp_buffer structure.
*/
drm_public int drmAgpFree(int fd, drm_handle_t handle)
{
drm_agp_buffer_t b;
memclear(b);
b.handle = handle;
if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
return -errno;
return 0;
}
/**
* Bind a chunk of AGP memory.
*
* \param fd file descriptor.
* \param handle handle to the allocated memory, as given by drmAgpAllocate().
* \param offset offset in bytes. It will round to page boundary.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
* argument in a drm_agp_binding structure.
*/
drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
{
drm_agp_binding_t b;
memclear(b);
b.handle = handle;
b.offset = offset;
if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
return -errno;
return 0;
}
/**
* Unbind a chunk of AGP memory.
*
* \param fd file descriptor.
* \param handle handle to the allocated memory, as given by drmAgpAllocate().
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
* the argument in a drm_agp_binding structure.
*/
drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
{
drm_agp_binding_t b;
memclear(b);
b.handle = handle;
if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
return -errno;
return 0;
}
/**
* Get AGP driver major version number.
*
* \param fd file descriptor.
*
* \return major version number on success, or a negative value on failure..
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public int drmAgpVersionMajor(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return -errno;
return i.agp_version_major;
}
/**
* Get AGP driver minor version number.
*
* \param fd file descriptor.
*
* \return minor version number on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public int drmAgpVersionMinor(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return -errno;
return i.agp_version_minor;
}
/**
* Get AGP mode.
*
* \param fd file descriptor.
*
* \return mode on success, or zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned long drmAgpGetMode(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.mode;
}
/**
* Get AGP aperture base.
*
* \param fd file descriptor.
*
* \return aperture base on success, zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned long drmAgpBase(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.aperture_base;
}
/**
* Get AGP aperture size.
*
* \param fd file descriptor.
*
* \return aperture size on success, zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned long drmAgpSize(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.aperture_size;
}
/**
* Get used AGP memory.
*
* \param fd file descriptor.
*
* \return memory used on success, or zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned long drmAgpMemoryUsed(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.memory_used;
}
/**
* Get available AGP memory.
*
* \param fd file descriptor.
*
* \return memory available on success, or zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned long drmAgpMemoryAvail(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.memory_allowed;
}
/**
* Get hardware vendor ID.
*
* \param fd file descriptor.
*
* \return vendor ID on success, or zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned int drmAgpVendorId(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.id_vendor;
}
/**
* Get hardware device ID.
*
* \param fd file descriptor.
*
* \return zero on success, or zero on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
* necessary information in a drm_agp_info structure.
*/
drm_public unsigned int drmAgpDeviceId(int fd)
{
drm_agp_info_t i;
memclear(i);
if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
return 0;
return i.id_device;
}
drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
drm_handle_t *handle)
{
drm_scatter_gather_t sg;
memclear(sg);
*handle = 0;
sg.size = size;
if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
return -errno;
*handle = sg.handle;
return 0;
}
drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
{
drm_scatter_gather_t sg;
memclear(sg);
sg.handle = handle;
if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
return -errno;
return 0;
}
/**
* Wait for VBLANK.
*
* \param fd file descriptor.
* \param vbl pointer to a drmVBlank structure.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
*/
drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
{
struct timespec timeout, cur;
int ret;
ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
if (ret < 0) {
fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
goto out;
}
timeout.tv_sec++;
do {
ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
vbl->request.type &= ~DRM_VBLANK_RELATIVE;
if (ret && errno == EINTR) {
clock_gettime(CLOCK_MONOTONIC, &cur);
/* Timeout after 1s */
if (cur.tv_sec > timeout.tv_sec + 1 ||
(cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
timeout.tv_nsec)) {
errno = EBUSY;
ret = -1;
break;
}
}
} while (ret && errno == EINTR);
out:
return ret;
}
drm_public int drmError(int err, const char *label)
{
switch (err) {
case DRM_ERR_NO_DEVICE:
fprintf(stderr, "%s: no device\n", label);
break;
case DRM_ERR_NO_ACCESS:
fprintf(stderr, "%s: no access\n", label);
break;
case DRM_ERR_NOT_ROOT:
fprintf(stderr, "%s: not root\n", label);
break;
case DRM_ERR_INVALID:
fprintf(stderr, "%s: invalid args\n", label);
break;
default:
if (err < 0)
err = -err;
fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
break;
}
return 1;
}
/**
* Install IRQ handler.
*
* \param fd file descriptor.
* \param irq IRQ number.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
* argument in a drm_control structure.
*/
drm_public int drmCtlInstHandler(int fd, int irq)
{
drm_control_t ctl;
memclear(ctl);
ctl.func = DRM_INST_HANDLER;
ctl.irq = irq;
if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
return -errno;
return 0;
}
/**
* Uninstall IRQ handler.
*
* \param fd file descriptor.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
* argument in a drm_control structure.
*/
drm_public int drmCtlUninstHandler(int fd)
{
drm_control_t ctl;
memclear(ctl);
ctl.func = DRM_UNINST_HANDLER;
ctl.irq = 0;
if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
return -errno;
return 0;
}
drm_public int drmFinish(int fd, int context, drmLockFlags flags)
{
drm_lock_t lock;
memclear(lock);
lock.context = context;
if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
return -errno;
return 0;
}
/**
* Get IRQ from bus ID.
*
* \param fd file descriptor.
* \param busnum bus number.
* \param devnum device number.
* \param funcnum function number.
*
* \return IRQ number on success, or a negative value on failure.
*
* \internal
* This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
* arguments in a drm_irq_busid structure.
*/
drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
int funcnum)
{
drm_irq_busid_t p;
memclear(p);
p.busnum = busnum;
p.devnum = devnum;
p.funcnum = funcnum;
if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
return -errno;
return p.irq;
}
drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
{
drmHashEntry *entry = drmGetEntry(fd);
if (drmHashInsert(entry->tagTable, context, tag)) {
drmHashDelete(entry->tagTable, context);
drmHashInsert(entry->tagTable, context, tag);
}
return 0;
}
drm_public int drmDelContextTag(int fd, drm_context_t context)
{
drmHashEntry *entry = drmGetEntry(fd);
return drmHashDelete(entry->tagTable, context);
}
drm_public void *drmGetContextTag(int fd, drm_context_t context)
{
drmHashEntry *entry = drmGetEntry(fd);
void *value;
if (drmHashLookup(entry->tagTable, context, &value))
return NULL;
return value;
}
drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
drm_handle_t handle)
{
drm_ctx_priv_map_t map;
memclear(map);
map.ctx_id = ctx_id;
map.handle = (void *)(uintptr_t)handle;
if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
return -errno;
return 0;
}
drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
drm_handle_t *handle)
{
drm_ctx_priv_map_t map;
memclear(map);
map.ctx_id = ctx_id;
if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
return -errno;
if (handle)
*handle = (drm_handle_t)(uintptr_t)map.handle;
return 0;
}
drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
drmMapType *type, drmMapFlags *flags,
drm_handle_t *handle, int *mtrr)
{
drm_map_t map;
memclear(map);
map.offset = idx;
if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
return -errno;
*offset = map.offset;
*size = map.size;
*type = (drmMapType)map.type;
*flags = (drmMapFlags)map.flags;
*handle = (unsigned long)map.handle;
*mtrr = map.mtrr;
return 0;
}
drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
unsigned long *magic, unsigned long *iocs)
{
drm_client_t client;
memclear(client);
client.idx = idx;
if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
return -errno;
*auth = client.auth;
*pid = client.pid;
*uid = client.uid;
*magic = client.magic;
*iocs = client.iocs;
return 0;
}
drm_public int drmGetStats(int fd, drmStatsT *stats)
{
drm_stats_t s;
unsigned i;
memclear(s);
if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
return -errno;
stats->count = 0;
memset(stats, 0, sizeof(*stats));
if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
return -1;
#define SET_VALUE \
stats->data[i].long_format = "%-20.20s"; \
stats->data[i].rate_format = "%8.8s"; \
stats->data[i].isvalue = 1; \
stats->data[i].verbose = 0
#define SET_COUNT \
stats->data[i].long_format = "%-20.20s"; \
stats->data[i].rate_format = "%5.5s"; \
stats->data[i].isvalue = 0; \
stats->data[i].mult_names = "kgm"; \
stats->data[i].mult = 1000; \
stats->data[i].verbose = 0
#define SET_BYTE \
stats->data[i].long_format = "%-20.20s"; \
stats->data[i].rate_format = "%5.5s"; \
stats->data[i].isvalue = 0; \
stats->data[i].mult_names = "KGM"; \
stats->data[i].mult = 1024; \
stats->data[i].verbose = 0
stats->count = s.count;
for (i = 0; i < s.count; i++) {
stats->data[i].value = s.data[i].value;
switch (s.data[i].type) {
case _DRM_STAT_LOCK:
stats->data[i].long_name = "Lock";
stats->data[i].rate_name = "Lock";
SET_VALUE;
break;
case _DRM_STAT_OPENS:
stats->data[i].long_name = "Opens";
stats->data[i].rate_name = "O";
SET_COUNT;
stats->data[i].verbose = 1;
break;
case _DRM_STAT_CLOSES:
stats->data[i].long_name = "Closes";
stats->data[i].rate_name = "Lock";
SET_COUNT;
stats->data[i].verbose = 1;
break;
case _DRM_STAT_IOCTLS:
stats->data[i].long_name = "Ioctls";
stats->data[i].rate_name = "Ioc/s";
SET_COUNT;
break;
case _DRM_STAT_LOCKS:
stats->data[i].long_name = "Locks";
stats->data[i].rate_name = "Lck/s";
SET_COUNT;
break;
case _DRM_STAT_UNLOCKS:
stats->data[i].long_name = "Unlocks";
stats->data[i].rate_name = "Unl/s";
SET_COUNT;
break;
case _DRM_STAT_IRQ:
stats->data[i].long_name = "IRQs";
stats->data[i].rate_name = "IRQ/s";
SET_COUNT;
break;
case _DRM_STAT_PRIMARY:
stats->data[i].long_name = "Primary Bytes";
stats->data[i].rate_name = "PB/s";
SET_BYTE;
break;
case _DRM_STAT_SECONDARY:
stats->data[i].long_name = "Secondary Bytes";
stats->data[i].rate_name = "SB/s";
SET_BYTE;
break;
case _DRM_STAT_DMA:
stats->data[i].long_name = "DMA";
stats->data[i].rate_name = "DMA/s";
SET_COUNT;
break;
case _DRM_STAT_SPECIAL:
stats->data[i].long_name = "Special DMA";
stats->data[i].rate_name = "dma/s";
SET_COUNT;
break;
case _DRM_STAT_MISSED:
stats->data[i].long_name = "Miss";
stats->data[i].rate_name = "Ms/s";
SET_COUNT;
break;
case _DRM_STAT_VALUE:
stats->data[i].long_name = "Value";
stats->data[i].rate_name = "Value";
SET_VALUE;
break;
case _DRM_STAT_BYTE:
stats->data[i].long_name = "Bytes";
stats->data[i].rate_name = "B/s";
SET_BYTE;
break;
case _DRM_STAT_COUNT:
default:
stats->data[i].long_name = "Count";
stats->data[i].rate_name = "Cnt/s";
SET_COUNT;
break;
}
}
return 0;
}
/**
* Issue a set-version ioctl.
*
* \param fd file descriptor.
* \param drmCommandIndex command index
* \param data source pointer of the data to be read and written.
* \param size size of the data to be read and written.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* It issues a read-write ioctl given by
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
*/
drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
{
int retcode = 0;
drm_set_version_t sv;
memclear(sv);
sv.drm_di_major = version->drm_di_major;
sv.drm_di_minor = version->drm_di_minor;
sv.drm_dd_major = version->drm_dd_major;
sv.drm_dd_minor = version->drm_dd_minor;
if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
retcode = -errno;
}
version->drm_di_major = sv.drm_di_major;
version->drm_di_minor = sv.drm_di_minor;
version->drm_dd_major = sv.drm_dd_major;
version->drm_dd_minor = sv.drm_dd_minor;
return retcode;
}
/**
* Send a device-specific command.
*
* \param fd file descriptor.
* \param drmCommandIndex command index
*
* \return zero on success, or a negative value on failure.
*
* \internal
* It issues a ioctl given by
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
*/
drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
{
unsigned long request;
request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
if (drmIoctl(fd, request, NULL)) {
return -errno;
}
return 0;
}
/**
* Send a device-specific read command.
*
* \param fd file descriptor.
* \param drmCommandIndex command index
* \param data destination pointer of the data to be read.
* \param size size of the data to be read.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* It issues a read ioctl given by
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
*/
drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
void *data, unsigned long size)
{
unsigned long request;
request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
DRM_COMMAND_BASE + drmCommandIndex, size);
if (drmIoctl(fd, request, data)) {
return -errno;
}
return 0;
}
/**
* Send a device-specific write command.
*
* \param fd file descriptor.
* \param drmCommandIndex command index
* \param data source pointer of the data to be written.
* \param size size of the data to be written.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* It issues a write ioctl given by
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
*/
drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
void *data, unsigned long size)
{
unsigned long request;
request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
DRM_COMMAND_BASE + drmCommandIndex, size);
if (drmIoctl(fd, request, data)) {
return -errno;
}
return 0;
}
/**
* Send a device-specific read-write command.
*
* \param fd file descriptor.
* \param drmCommandIndex command index
* \param data source pointer of the data to be read and written.
* \param size size of the data to be read and written.
*
* \return zero on success, or a negative value on failure.
*
* \internal
* It issues a read-write ioctl given by
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
*/
drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
void *data, unsigned long size)
{
unsigned long request;
request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
DRM_COMMAND_BASE + drmCommandIndex, size);
if (drmIoctl(fd, request, data))
return -errno;
return 0;
}
#define DRM_MAX_FDS 16
static struct {
char *BusID;
int fd;
int refcount;
int type;
} connection[DRM_MAX_FDS];
static int nr_fds = 0;
drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
{
return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
}
drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
int type)
{
int i;
int fd;
for (i = 0; i < nr_fds; i++)
if ((strcmp(BusID, connection[i].BusID) == 0) &&
(connection[i].type == type)) {
connection[i].refcount++;
*newlyopened = 0;
return connection[i].fd;
}
fd = drmOpenWithType(NULL, BusID, type);
if (fd < 0 || nr_fds == DRM_MAX_FDS)
return fd;
connection[nr_fds].BusID = strdup(BusID);
connection[nr_fds].fd = fd;
connection[nr_fds].refcount = 1;
connection[nr_fds].type = type;
*newlyopened = 1;
if (0)
fprintf(stderr, "saved connection %d for %s %d\n",
nr_fds, connection[nr_fds].BusID,
strcmp(BusID, connection[nr_fds].BusID));
nr_fds++;
return fd;
}
drm_public void drmCloseOnce(int fd)
{
int i;
for (i = 0; i < nr_fds; i++) {
if (fd == connection[i].fd) {
if (--connection[i].refcount == 0) {
drmClose(connection[i].fd);
free(connection[i].BusID);
if (i < --nr_fds)
connection[i] = connection[nr_fds];
return;
}
}
}
}
drm_public int drmSetMaster(int fd)
{
return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
}
drm_public int drmDropMaster(int fd)
{
return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
}
drm_public int drmIsMaster(int fd)
{
/* Detect master by attempting something that requires master.
*
* Authenticating magic tokens requires master and 0 is an
* internal kernel detail which we could use. Attempting this on
* a master fd would fail therefore fail with EINVAL because 0
* is invalid.
*
* A non-master fd will fail with EACCES, as the kernel checks
* for master before attempting to do anything else.
*
* Since we don't want to leak implementation details, use
* EACCES.
*/
return drmAuthMagic(fd, 0) != -EACCES;
}
drm_public char *drmGetDeviceNameFromFd(int fd)
{
#ifdef __FreeBSD__
struct stat sbuf;
int maj, min;
int nodetype;
if (fstat(fd, &sbuf))
return NULL;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
nodetype = drmGetMinorType(maj, min);
return drmGetMinorNameForFD(fd, nodetype);
#else
char name[128];
struct stat sbuf;
dev_t d;
int i;
/* The whole drmOpen thing is a fiasco and we need to find a way
* back to just using open(2). For now, however, lets just make
* things worse with even more ad hoc directory walking code to
* discover the device file name. */
fstat(fd, &sbuf);
d = sbuf.st_rdev;
for (i = 0; i < DRM_MAX_MINOR; i++) {
snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
break;
}
if (i == DRM_MAX_MINOR)
return NULL;
return strdup(name);
#endif
}
static bool drmNodeIsDRM(int maj, int min)
{
#ifdef __linux__
char path[64];
struct stat sbuf;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
maj, min);
return stat(path, &sbuf) == 0;
#elif defined(__FreeBSD__)
char name[SPECNAMELEN];
if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
return 0;
/* Handle drm/ and dri/ as both are present in different FreeBSD version
* FreeBSD on amd64/i386/powerpc external kernel modules create node in
* in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
* only device nodes in /dev/dri/ */
return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
#else
return maj == DRM_MAJOR;
#endif
}
drm_public int drmGetNodeTypeFromFd(int fd)
{
struct stat sbuf;
int maj, min, type;
if (fstat(fd, &sbuf))
return -1;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
errno = EINVAL;
return -1;
}
type = drmGetMinorType(maj, min);
if (type == -1)
errno = ENODEV;
return type;
}
drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
int *prime_fd)
{
struct drm_prime_handle args;
int ret;
memclear(args);
args.fd = -1;
args.handle = handle;
args.flags = flags;
ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
if (ret)
return ret;
*prime_fd = args.fd;
return 0;
}
drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
{
struct drm_prime_handle args;
int ret;
memclear(args);
args.fd = prime_fd;
ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
if (ret)
return ret;
*handle = args.handle;
return 0;
}
drm_public int drmCloseBufferHandle(int fd, uint32_t handle)
{
struct drm_gem_close args;
memclear(args);
args.handle = handle;
return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &args);
}
static char *drmGetMinorNameForFD(int fd, int type)
{
#ifdef __linux__
DIR *sysdir;
struct dirent *ent;
struct stat sbuf;
const char *name = drmGetMinorName(type);
int len;
char dev_name[64], buf[64];
int maj, min;
if (!name)
return NULL;
len = strlen(name);
if (fstat(fd, &sbuf))
return NULL;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return NULL;
snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
sysdir = opendir(buf);
if (!sysdir)
return NULL;
while ((ent = readdir(sysdir))) {
if (strncmp(ent->d_name, name, len) == 0) {
if (snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
ent->d_name) < 0)
return NULL;
closedir(sysdir);
return strdup(dev_name);
}
}
closedir(sysdir);
return NULL;
#elif defined(__FreeBSD__)
struct stat sbuf;
char dname[SPECNAMELEN];
const char *mname;
char name[SPECNAMELEN];
int id, maj, min, nodetype, i;
if (fstat(fd, &sbuf))
return NULL;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return NULL;
if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname)))
return NULL;
/* Handle both /dev/drm and /dev/dri
* FreeBSD on amd64/i386/powerpc external kernel modules create node in
* in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
* only device nodes in /dev/dri/ */
/* Get the node type represented by fd so we can deduce the target name */
nodetype = drmGetMinorType(maj, min);
if (nodetype == -1)
return (NULL);
mname = drmGetMinorName(type);
for (i = 0; i < SPECNAMELEN; i++) {
if (isalpha(dname[i]) == 0 && dname[i] != '/')
break;
}
if (dname[i] == '\0')
return (NULL);
id = (int)strtol(&dname[i], NULL, 10);
id -= drmGetMinorBase(nodetype);
snprintf(name, sizeof(name), DRM_DIR_NAME "/%s%d", mname,
id + drmGetMinorBase(type));
return strdup(name);
#else
struct stat sbuf;
char buf[PATH_MAX + 1];
const char *dev_name = drmGetDeviceName(type);
unsigned int maj, min;
int n;
if (fstat(fd, &sbuf))
return NULL;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return NULL;
if (!dev_name)
return NULL;
n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
if (n == -1 || n >= sizeof(buf))
return NULL;
return strdup(buf);
#endif
}
drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
{
return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
}
drm_public char *drmGetRenderDeviceNameFromFd(int fd)
{
return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
}
#ifdef __linux__
static char * DRM_PRINTFLIKE(2, 3)
sysfs_uevent_get(const char *path, const char *fmt, ...)
{
char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
size_t size = 0, len;
ssize_t num;
va_list ap;
FILE *fp;
va_start(ap, fmt);
num = vasprintf(&key, fmt, ap);
va_end(ap);
len = num;
snprintf(filename, sizeof(filename), "%s/uevent", path);
fp = fopen(filename, "r");
if (!fp) {
free(key);
return NULL;
}
while ((num = getline(&line, &size, fp)) >= 0) {
if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
char *start = line + len + 1, *end = line + num - 1;
if (*end != '\n')
end++;
value = strndup(start, end - start);
break;
}
}
free(line);
fclose(fp);
free(key);
return value;
}
#endif
/* Little white lie to avoid major rework of the existing code */
#define DRM_BUS_VIRTIO 0x10
#ifdef __linux__
static int get_subsystem_type(const char *device_path)
{
char path[PATH_MAX + 1] = "";
char link[PATH_MAX + 1] = "";
char *name;
struct {
const char *name;
int bus_type;
} bus_types[] = {
{ "/pci", DRM_BUS_PCI },
{ "/usb", DRM_BUS_USB },
{ "/platform", DRM_BUS_PLATFORM },
{ "/spi", DRM_BUS_PLATFORM },
{ "/host1x", DRM_BUS_HOST1X },
{ "/virtio", DRM_BUS_VIRTIO },
{ "/faux", DRM_BUS_FAUX },
};
strncpy(path, device_path, PATH_MAX);
strncat(path, "/subsystem", PATH_MAX);
if (readlink(path, link, PATH_MAX) < 0)
return -errno;
name = strrchr(link, '/');
if (!name)
return -EINVAL;
for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
return bus_types[i].bus_type;
}
return -EINVAL;
}
#endif
static int drmParseSubsystemType(int maj, int min)
{
#ifdef __linux__
char path[PATH_MAX + 1] = "";
char real_path[PATH_MAX + 1] = "";
int subsystem_type;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
subsystem_type = get_subsystem_type(path);
/* Try to get the parent (underlying) device type */
if (subsystem_type == DRM_BUS_VIRTIO) {
/* Assume virtio-pci on error */
if (!realpath(path, real_path))
return DRM_BUS_VIRTIO;
strncat(path, "/..", PATH_MAX);
subsystem_type = get_subsystem_type(path);
if (subsystem_type < 0)
return DRM_BUS_VIRTIO;
}
return subsystem_type;
#elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
return DRM_BUS_PCI;
#else
#warning "Missing implementation of drmParseSubsystemType"
return -EINVAL;
#endif
}
#ifdef __linux__
static void
get_pci_path(int maj, int min, char *pci_path)
{
char path[PATH_MAX + 1], *term;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
if (!realpath(path, pci_path)) {
strcpy(pci_path, path);
return;
}
term = strrchr(pci_path, '/');
if (term && strncmp(term, "/virtio", 7) == 0)
*term = 0;
}
#endif
#ifdef __FreeBSD__
static int get_sysctl_pci_bus_info(int maj, int min, drmPciBusInfoPtr info)
{
char dname[SPECNAMELEN];
char sysctl_name[16];
char sysctl_val[256];
size_t sysctl_len;
int id, type, nelem;
unsigned int rdev, majmin, domain, bus, dev, func;
rdev = makedev(maj, min);
if (!devname_r(rdev, S_IFCHR, dname, sizeof(dname)))
return -EINVAL;
if (sscanf(dname, "drm/%d\n", &id) != 1)
return -EINVAL;
type = drmGetMinorType(maj, min);
if (type == -1)
return -EINVAL;
/* BUG: This above section is iffy, since it mandates that a driver will
* create both card and render node.
* If it does not, the next DRM device will create card#X and
* renderD#(128+X)-1.
* This is a possibility in FreeBSD but for now there is no good way for
* obtaining the info.
*/
switch (type) {
case DRM_NODE_PRIMARY:
break;
case DRM_NODE_RENDER:
id -= 128;
break;
}
if (id < 0)
return -EINVAL;
if (snprintf(sysctl_name, sizeof(sysctl_name), "hw.dri.%d.busid", id) <= 0)
return -EINVAL;
sysctl_len = sizeof(sysctl_val);
if (sysctlbyname(sysctl_name, sysctl_val, &sysctl_len, NULL, 0))
return -EINVAL;
#define bus_fmt "pci:%04x:%02x:%02x.%u"
nelem = sscanf(sysctl_val, bus_fmt, &domain, &bus, &dev, &func);
if (nelem != 4)
return -EINVAL;
info->domain = domain;
info->bus = bus;
info->dev = dev;
info->func = func;
return 0;
}
#endif
static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
{
#ifdef __linux__
unsigned int domain, bus, dev, func;
char pci_path[PATH_MAX + 1], *value;
int num;
get_pci_path(maj, min, pci_path);
value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
if (!value)
return -ENOENT;
num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
free(value);
if (num != 4)
return -EINVAL;
info->domain = domain;
info->bus = bus;
info->dev = dev;
info->func = func;
return 0;
#elif defined(__OpenBSD__) || defined(__DragonFly__)
struct drm_pciinfo pinfo;
int fd, type;
type = drmGetMinorType(maj, min);
if (type == -1)
return -ENODEV;
fd = drmOpenMinor(min, 0, type);
if (fd < 0)
return -errno;
if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
close(fd);
return -errno;
}
close(fd);
info->domain = pinfo.domain;
info->bus = pinfo.bus;
info->dev = pinfo.dev;
info->func = pinfo.func;
return 0;
#elif defined(__FreeBSD__)
return get_sysctl_pci_bus_info(maj, min, info);
#else
#warning "Missing implementation of drmParsePciBusInfo"
return -EINVAL;
#endif
}
drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
{
if (a == NULL || b == NULL)
return 0;
if (a->bustype != b->bustype)
return 0;
switch (a->bustype) {
case DRM_BUS_PCI:
return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
case DRM_BUS_USB:
return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
case DRM_BUS_PLATFORM:
return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
case DRM_BUS_HOST1X:
return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
case DRM_BUS_FAUX:
return memcmp(a->businfo.faux, b->businfo.faux, sizeof(drmFauxBusInfo)) == 0;
default:
break;
}
return 0;
}
static int drmGetNodeType(const char *name)
{
if (strncmp(name, DRM_RENDER_MINOR_NAME,
sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
return DRM_NODE_RENDER;
if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
return DRM_NODE_PRIMARY;
return -EINVAL;
}
static int drmGetMaxNodeName(void)
{
return sizeof(DRM_DIR_NAME) +
MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
sizeof(DRM_CONTROL_MINOR_NAME),
sizeof(DRM_RENDER_MINOR_NAME)) +
3 /* length of the node number */;
}
#ifdef __linux__
static int parse_separate_sysfs_files(int maj, int min,
drmPciDeviceInfoPtr device,
bool ignore_revision)
{
static const char *attrs[] = {
"revision", /* Older kernels are missing the file, so check for it first */
"vendor",
"device",
"subsystem_vendor",
"subsystem_device",
};
char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
unsigned int data[ARRAY_SIZE(attrs)];
FILE *fp;
int ret;
get_pci_path(maj, min, pci_path);
for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
if (snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]) < 0)
return -errno;
fp = fopen(path, "r");
if (!fp)
return -errno;
ret = fscanf(fp, "%x", &data[i]);
fclose(fp);
if (ret != 1)
return -errno;
}
device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
device->vendor_id = data[1] & 0xffff;
device->device_id = data[2] & 0xffff;
device->subvendor_id = data[3] & 0xffff;
device->subdevice_id = data[4] & 0xffff;
return 0;
}
static int parse_config_sysfs_file(int maj, int min,
drmPciDeviceInfoPtr device)
{
char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
unsigned char config[64];
int fd, ret;
get_pci_path(maj, min, pci_path);
if (snprintf(path, PATH_MAX, "%s/config", pci_path) < 0)
return -errno;
fd = open(path, O_RDONLY);
if (fd < 0)
return -errno;
ret = read(fd, config, sizeof(config));
close(fd);
if (ret < 0)
return -errno;
device->vendor_id = config[0] | (config[1] << 8);
device->device_id = config[2] | (config[3] << 8);
device->revision_id = config[8];
device->subvendor_id = config[44] | (config[45] << 8);
device->subdevice_id = config[46] | (config[47] << 8);
return 0;
}
#endif
static int drmParsePciDeviceInfo(int maj, int min,
drmPciDeviceInfoPtr device,
uint32_t flags)
{
#ifdef __linux__
if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
return parse_separate_sysfs_files(maj, min, device, true);
if (parse_separate_sysfs_files(maj, min, device, false))
return parse_config_sysfs_file(maj, min, device);
return 0;
#elif defined(__OpenBSD__) || defined(__DragonFly__)
struct drm_pciinfo pinfo;
int fd, type;
type = drmGetMinorType(maj, min);
if (type == -1)
return -ENODEV;
fd = drmOpenMinor(min, 0, type);
if (fd < 0)
return -errno;
if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
close(fd);
return -errno;
}
close(fd);
device->vendor_id = pinfo.vendor_id;
device->device_id = pinfo.device_id;
device->revision_id = pinfo.revision_id;
device->subvendor_id = pinfo.subvendor_id;
device->subdevice_id = pinfo.subdevice_id;
return 0;
#elif defined(__FreeBSD__)
drmPciBusInfo info;
struct pci_conf_io pc;
struct pci_match_conf patterns[1];
struct pci_conf results[1];
int fd, error;
if (get_sysctl_pci_bus_info(maj, min, &info) != 0)
return -EINVAL;
fd = open("/dev/pci", O_RDONLY);
if (fd < 0)
return -errno;
bzero(&patterns, sizeof(patterns));
patterns[0].pc_sel.pc_domain = info.domain;
patterns[0].pc_sel.pc_bus = info.bus;
patterns[0].pc_sel.pc_dev = info.dev;
patterns[0].pc_sel.pc_func = info.func;
patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS
| PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC;
bzero(&pc, sizeof(struct pci_conf_io));
pc.num_patterns = 1;
pc.pat_buf_len = sizeof(patterns);
pc.patterns = patterns;
pc.match_buf_len = sizeof(results);
pc.matches = results;
if (ioctl(fd, PCIOCGETCONF, &pc) || pc.status == PCI_GETCONF_ERROR) {
error = errno;
close(fd);
return -error;
}
close(fd);
device->vendor_id = results[0].pc_vendor;
device->device_id = results[0].pc_device;
device->subvendor_id = results[0].pc_subvendor;
device->subdevice_id = results[0].pc_subdevice;
device->revision_id = results[0].pc_revid;
return 0;
#else
#warning "Missing implementation of drmParsePciDeviceInfo"
return -EINVAL;
#endif
}
static void drmFreePlatformDevice(drmDevicePtr device)
{
if (device->deviceinfo.platform) {
if (device->deviceinfo.platform->compatible) {
char **compatible = device->deviceinfo.platform->compatible;
while (*compatible) {
free(*compatible);
compatible++;
}
free(device->deviceinfo.platform->compatible);
}
}
}
static void drmFreeHost1xDevice(drmDevicePtr device)
{
if (device->deviceinfo.host1x) {
if (device->deviceinfo.host1x->compatible) {
char **compatible = device->deviceinfo.host1x->compatible;
while (*compatible) {
free(*compatible);
compatible++;
}
free(device->deviceinfo.host1x->compatible);
}
}
}
drm_public void drmFreeDevice(drmDevicePtr *device)
{
if (device == NULL)
return;
if (*device) {
switch ((*device)->bustype) {
case DRM_BUS_PLATFORM:
drmFreePlatformDevice(*device);
break;
case DRM_BUS_HOST1X:
drmFreeHost1xDevice(*device);
break;
}
}
free(*device);
*device = NULL;
}
drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
{
int i;
if (devices == NULL)
return;
for (i = 0; i < count; i++)
if (devices[i])
drmFreeDevice(&devices[i]);
}
static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
size_t bus_size, size_t device_size,
char **ptrp)
{
size_t max_node_length, extra, size;
drmDevicePtr device;
unsigned int i;
char *ptr;
max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
size = sizeof(*device) + extra + bus_size + device_size;
device = calloc(1, size);
if (!device)
return NULL;
device->available_nodes = 1 << type;
ptr = (char *)device + sizeof(*device);
device->nodes = (char **)ptr;
ptr += DRM_NODE_MAX * sizeof(void *);
for (i = 0; i < DRM_NODE_MAX; i++) {
device->nodes[i] = ptr;
ptr += max_node_length;
}
memcpy(device->nodes[type], node, max_node_length);
*ptrp = ptr;
return device;
}
static int drmProcessPciDevice(drmDevicePtr *device,
const char *node, int node_type,
int maj, int min, bool fetch_deviceinfo,
uint32_t flags)
{
drmDevicePtr dev;
char *addr;
int ret;
dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
sizeof(drmPciDeviceInfo), &addr);
if (!dev)
return -ENOMEM;
dev->bustype = DRM_BUS_PCI;
dev->businfo.pci = (drmPciBusInfoPtr)addr;
ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
if (ret)
goto free_device;
// Fetch the device info if the user has requested it
if (fetch_deviceinfo) {
addr += sizeof(drmPciBusInfo);
dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
if (ret)
goto free_device;
}
*device = dev;
return 0;
free_device:
free(dev);
return ret;
}
#ifdef __linux__
static int drm_usb_dev_path(int maj, int min, char *path, size_t len)
{
char *value, *tmp_path, *slash;
bool usb_device, usb_interface;
snprintf(path, len, "/sys/dev/char/%d:%d/device", maj, min);
value = sysfs_uevent_get(path, "DEVTYPE");
if (!value)
return -ENOENT;
usb_device = strcmp(value, "usb_device") == 0;
usb_interface = strcmp(value, "usb_interface") == 0;
free(value);
if (usb_device)
return 0;
if (!usb_interface)
return -ENOTSUP;
/* The parent of a usb_interface is a usb_device */
tmp_path = realpath(path, NULL);
if (!tmp_path)
return -errno;
slash = strrchr(tmp_path, '/');
if (!slash) {
free(tmp_path);
return -EINVAL;
}
*slash = '\0';
if (snprintf(path, len, "%s", tmp_path) >= (int)len) {
free(tmp_path);
return -EINVAL;
}
free(tmp_path);
return 0;
}
#endif
static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
{
#ifdef __linux__
char path[PATH_MAX + 1], *value;
unsigned int bus, dev;
int ret;
ret = drm_usb_dev_path(maj, min, path, sizeof(path));
if (ret < 0)
return ret;
value = sysfs_uevent_get(path, "BUSNUM");
if (!value)
return -ENOENT;
ret = sscanf(value, "%03u", &bus);
free(value);
if (ret <= 0)
return -errno;
value = sysfs_uevent_get(path, "DEVNUM");
if (!value)
return -ENOENT;
ret = sscanf(value, "%03u", &dev);
free(value);
if (ret <= 0)
return -errno;
info->bus = bus;
info->dev = dev;
return 0;
#else
#warning "Missing implementation of drmParseUsbBusInfo"
return -EINVAL;
#endif
}
static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
{
#ifdef __linux__
char path[PATH_MAX + 1], *value;
unsigned int vendor, product;
int ret;
ret = drm_usb_dev_path(maj, min, path, sizeof(path));
if (ret < 0)
return ret;
value = sysfs_uevent_get(path, "PRODUCT");
if (!value)
return -ENOENT;
ret = sscanf(value, "%x/%x", &vendor, &product);
free(value);
if (ret <= 0)
return -errno;
info->vendor = vendor;
info->product = product;
return 0;
#else
#warning "Missing implementation of drmParseUsbDeviceInfo"
return -EINVAL;
#endif
}
static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
int node_type, int maj, int min,
bool fetch_deviceinfo, uint32_t flags)
{
drmDevicePtr dev;
char *ptr;
int ret;
dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
sizeof(drmUsbDeviceInfo), &ptr);
if (!dev)
return -ENOMEM;
dev->bustype = DRM_BUS_USB;
dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
if (ret < 0)
goto free_device;
if (fetch_deviceinfo) {
ptr += sizeof(drmUsbBusInfo);
dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
if (ret < 0)
goto free_device;
}
*device = dev;
return 0;
free_device:
free(dev);
return ret;
}
static int drmParseOFBusInfo(int maj, int min, char *fullname)
{
#ifdef __linux__
char path[PATH_MAX + 1], *name, *tmp_name;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
name = sysfs_uevent_get(path, "OF_FULLNAME");
tmp_name = name;
if (!name) {
/* If the device lacks OF data, pick the MODALIAS info */
name = sysfs_uevent_get(path, "MODALIAS");
if (!name)
return -ENOENT;
/* .. and strip the MODALIAS=[platform,usb...]: part. */
tmp_name = strrchr(name, ':');
if (!tmp_name) {
free(name);
return -ENOENT;
}
tmp_name++;
}
strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
free(name);
return 0;
#else
#warning "Missing implementation of drmParseOFBusInfo"
return -EINVAL;
#endif
}
static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
{
#ifdef __linux__
char path[PATH_MAX + 1], *value, *tmp_name;
unsigned int count, i;
int err;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
if (value) {
sscanf(value, "%u", &count);
free(value);
} else {
/* Assume one entry if the device lack OF data */
count = 1;
}
*compatible = calloc(count + 1, sizeof(char *));
if (!*compatible)
return -ENOMEM;
for (i = 0; i < count; i++) {
value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
tmp_name = value;
if (!value) {
/* If the device lacks OF data, pick the MODALIAS info */
value = sysfs_uevent_get(path, "MODALIAS");
if (!value) {
err = -ENOENT;
goto free;
}
/* .. and strip the MODALIAS=[platform,usb...]: part. */
tmp_name = strrchr(value, ':');
if (!tmp_name) {
free(value);
return -ENOENT;
}
tmp_name = strdup(tmp_name + 1);
free(value);
}
(*compatible)[i] = tmp_name;
}
return 0;
free:
while (i--)
free((*compatible)[i]);
free(*compatible);
return err;
#else
#warning "Missing implementation of drmParseOFDeviceInfo"
return -EINVAL;
#endif
}
static int drmProcessPlatformDevice(drmDevicePtr *device,
const char *node, int node_type,
int maj, int min, bool fetch_deviceinfo,
uint32_t flags)
{
drmDevicePtr dev;
char *ptr;
int ret;
dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
sizeof(drmPlatformDeviceInfo), &ptr);
if (!dev)
return -ENOMEM;
dev->bustype = DRM_BUS_PLATFORM;
dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
if (ret < 0)
goto free_device;
if (fetch_deviceinfo) {
ptr += sizeof(drmPlatformBusInfo);
dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
if (ret < 0)
goto free_device;
}
*device = dev;
return 0;
free_device:
free(dev);
return ret;
}
static int drmProcessHost1xDevice(drmDevicePtr *device,
const char *node, int node_type,
int maj, int min, bool fetch_deviceinfo,
uint32_t flags)
{
drmDevicePtr dev;
char *ptr;
int ret;
dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
sizeof(drmHost1xDeviceInfo), &ptr);
if (!dev)
return -ENOMEM;
dev->bustype = DRM_BUS_HOST1X;
dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
if (ret < 0)
goto free_device;
if (fetch_deviceinfo) {
ptr += sizeof(drmHost1xBusInfo);
dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
if (ret < 0)
goto free_device;
}
*device = dev;
return 0;
free_device:
free(dev);
return ret;
}
static int drmParseFauxBusInfo(int maj, int min, char *fullname)
{
#ifdef __linux__
char path[PATH_MAX + 1] = "";
char real_path[PATH_MAX + 1] = "";
char *name;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
if (!realpath(path, real_path))
return -errno;
name = basename(real_path);
if (!name)
return -ENOENT;
strncpy(fullname, name, DRM_FAUX_DEVICE_NAME_LEN - 1);
fullname[DRM_FAUX_DEVICE_NAME_LEN - 1] = '\0';
return 0;
#else
#warning "Missing implementation of drmParseFauxBusInfo"
return -EINVAL;
#endif
}
static int drmProcessFauxDevice(drmDevicePtr *device,
const char *node, int node_type,
int maj, int min, bool fetch_deviceinfo,
uint32_t flags)
{
drmDevicePtr dev;
char *ptr;
int ret;
dev = drmDeviceAlloc(node_type, node, sizeof(drmFauxBusInfo), 0, &ptr);
if (!dev)
return -ENOMEM;
dev->bustype = DRM_BUS_FAUX;
dev->businfo.faux = (drmFauxBusInfoPtr)ptr;
ret = drmParseFauxBusInfo(maj, min, dev->businfo.faux->name);
if (ret < 0)
goto free_device;
*device = dev;
return 0;
free_device:
free(dev);
return ret;
}
static int
process_device(drmDevicePtr *device, const char *d_name,
int req_subsystem_type,
bool fetch_deviceinfo, uint32_t flags)
{
struct stat sbuf;
char node[PATH_MAX + 1];
int node_type, subsystem_type, written;
unsigned int maj, min;
const int max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
node_type = drmGetNodeType(d_name);
if (node_type < 0)
return -1;
written = snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
if (written < 0)
return -1;
/* anything longer than this will be truncated in drmDeviceAlloc.
* Account for NULL byte
*/
if (written + 1 > max_node_length)
return -1;
if (stat(node, &sbuf))
return -1;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return -1;
subsystem_type = drmParseSubsystemType(maj, min);
if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
return -1;
switch (subsystem_type) {
case DRM_BUS_PCI:
case DRM_BUS_VIRTIO:
return drmProcessPciDevice(device, node, node_type, maj, min,
fetch_deviceinfo, flags);
case DRM_BUS_USB:
return drmProcessUsbDevice(device, node, node_type, maj, min,
fetch_deviceinfo, flags);
case DRM_BUS_PLATFORM:
return drmProcessPlatformDevice(device, node, node_type, maj, min,
fetch_deviceinfo, flags);
case DRM_BUS_HOST1X:
return drmProcessHost1xDevice(device, node, node_type, maj, min,
fetch_deviceinfo, flags);
case DRM_BUS_FAUX:
return drmProcessFauxDevice(device, node, node_type, maj, min,
fetch_deviceinfo, flags);
default:
return -1;
}
}
/* Consider devices located on the same bus as duplicate and fold the respective
* entries into a single one.
*
* Note: this leaves "gaps" in the array, while preserving the length.
*/
static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
{
int node_type, i, j;
for (i = 0; i < count; i++) {
for (j = i + 1; j < count; j++) {
if (drmDevicesEqual(local_devices[i], local_devices[j])) {
local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
node_type = log2_int(local_devices[j]->available_nodes);
memcpy(local_devices[i]->nodes[node_type],
local_devices[j]->nodes[node_type], drmGetMaxNodeName());
drmFreeDevice(&local_devices[j]);
}
}
}
}
/* Check that the given flags are valid returning 0 on success */
static int
drm_device_validate_flags(uint32_t flags)
{
return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
}
static bool
drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
{
struct stat sbuf;
for (int i = 0; i < DRM_NODE_MAX; i++) {
if (device->available_nodes & 1 << i) {
if (stat(device->nodes[i], &sbuf) == 0 &&
sbuf.st_rdev == find_rdev)
return true;
}
}
return false;
}
/*
* The kernel drm core has a number of places that assume maximum of
* 3x64 devices nodes. That's 64 for each of primary, control and
* render nodes. Rounded it up to 256 for simplicity.
*/
#define MAX_DRM_NODES 256
/**
* Get information about a device from its dev_t identifier
*
* \param find_rdev dev_t identifier of the device
* \param flags feature/behaviour bitmask
* \param device the address of a drmDevicePtr where the information
* will be allocated in stored
*
* \return zero on success, negative error code otherwise.
*/
drm_public int drmGetDeviceFromDevId(dev_t find_rdev, uint32_t flags, drmDevicePtr *device)
{
#ifdef __OpenBSD__
/*
* DRI device nodes on OpenBSD are not in their own directory, they reside
* in /dev along with a large number of statically generated /dev nodes.
* Avoid stat'ing all of /dev needlessly by implementing this custom path.
*/
drmDevicePtr d;
char node[PATH_MAX + 1];
const char *dev_name;
int node_type, subsystem_type;
int maj, min, n, ret;
const int max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
struct stat sbuf;
if (device == NULL)
return -EINVAL;
maj = major(find_rdev);
min = minor(find_rdev);
if (!drmNodeIsDRM(maj, min))
return -EINVAL;
node_type = drmGetMinorType(maj, min);
if (node_type == -1)
return -ENODEV;
dev_name = drmGetDeviceName(node_type);
if (!dev_name)
return -EINVAL;
/* anything longer than this will be truncated in drmDeviceAlloc.
* Account for NULL byte
*/
n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
if (n == -1 || n >= PATH_MAX)
return -errno;
if (n + 1 > max_node_length)
return -EINVAL;
if (stat(node, &sbuf))
return -EINVAL;
subsystem_type = drmParseSubsystemType(maj, min);
if (subsystem_type != DRM_BUS_PCI)
return -ENODEV;
ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
if (ret)
return ret;
*device = d;
return 0;
#else
drmDevicePtr local_devices[MAX_DRM_NODES];
drmDevicePtr d;
DIR *sysdir;
struct dirent *dent;
int subsystem_type;
int maj, min;
int ret, i, node_count;
if (drm_device_validate_flags(flags))
return -EINVAL;
if (device == NULL)
return -EINVAL;
maj = major(find_rdev);
min = minor(find_rdev);
if (!drmNodeIsDRM(maj, min))
return -EINVAL;
subsystem_type = drmParseSubsystemType(maj, min);
if (subsystem_type < 0)
return subsystem_type;
sysdir = opendir(DRM_DIR_NAME);
if (!sysdir)
return -errno;
i = 0;
while ((dent = readdir(sysdir))) {
ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
if (ret)
continue;
if (i >= MAX_DRM_NODES) {
fprintf(stderr, "More than %d drm nodes detected. "
"Please report a bug - that should not happen.\n"
"Skipping extra nodes\n", MAX_DRM_NODES);
break;
}
local_devices[i] = d;
i++;
}
node_count = i;
drmFoldDuplicatedDevices(local_devices, node_count);
*device = NULL;
for (i = 0; i < node_count; i++) {
if (!local_devices[i])
continue;
if (drm_device_has_rdev(local_devices[i], find_rdev))
*device = local_devices[i];
else
drmFreeDevice(&local_devices[i]);
}
closedir(sysdir);
if (*device == NULL)
return -ENODEV;
return 0;
#endif
}
drm_public int drmGetNodeTypeFromDevId(dev_t devid)
{
int maj, min, node_type;
maj = major(devid);
min = minor(devid);
if (!drmNodeIsDRM(maj, min))
return -EINVAL;
node_type = drmGetMinorType(maj, min);
if (node_type == -1)
return -ENODEV;
return node_type;
}
/**
* Get information about the opened drm device
*
* \param fd file descriptor of the drm device
* \param flags feature/behaviour bitmask
* \param device the address of a drmDevicePtr where the information
* will be allocated in stored
*
* \return zero on success, negative error code otherwise.
*
* \note Unlike drmGetDevice it does not retrieve the pci device revision field
* unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
*/
drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
{
struct stat sbuf;
if (fd == -1)
return -EINVAL;
if (fstat(fd, &sbuf))
return -errno;
if (!S_ISCHR(sbuf.st_mode))
return -EINVAL;
return drmGetDeviceFromDevId(sbuf.st_rdev, flags, device);
}
/**
* Get information about the opened drm device
*
* \param fd file descriptor of the drm device
* \param device the address of a drmDevicePtr where the information
* will be allocated in stored
*
* \return zero on success, negative error code otherwise.
*/
drm_public int drmGetDevice(int fd, drmDevicePtr *device)
{
return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
}
/**
* Get drm devices on the system
*
* \param flags feature/behaviour bitmask
* \param devices the array of devices with drmDevicePtr elements
* can be NULL to get the device number first
* \param max_devices the maximum number of devices for the array
*
* \return on error - negative error code,
* if devices is NULL - total number of devices available on the system,
* alternatively the number of devices stored in devices[], which is
* capped by the max_devices.
*
* \note Unlike drmGetDevices it does not retrieve the pci device revision field
* unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
*/
drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
int max_devices)
{
drmDevicePtr local_devices[MAX_DRM_NODES];
drmDevicePtr device;
DIR *sysdir;
struct dirent *dent;
int ret, i, node_count, device_count;
if (drm_device_validate_flags(flags))
return -EINVAL;
sysdir = opendir(DRM_DIR_NAME);
if (!sysdir)
return -errno;
i = 0;
while ((dent = readdir(sysdir))) {
ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
if (ret)
continue;
if (i >= MAX_DRM_NODES) {
fprintf(stderr, "More than %d drm nodes detected. "
"Please report a bug - that should not happen.\n"
"Skipping extra nodes\n", MAX_DRM_NODES);
break;
}
local_devices[i] = device;
i++;
}
node_count = i;
drmFoldDuplicatedDevices(local_devices, node_count);
device_count = 0;
for (i = 0; i < node_count; i++) {
if (!local_devices[i])
continue;
if ((devices != NULL) && (device_count < max_devices))
devices[device_count] = local_devices[i];
else
drmFreeDevice(&local_devices[i]);
device_count++;
}
closedir(sysdir);
if (devices != NULL)
return MIN2(device_count, max_devices);
return device_count;
}
/**
* Get drm devices on the system
*
* \param devices the array of devices with drmDevicePtr elements
* can be NULL to get the device number first
* \param max_devices the maximum number of devices for the array
*
* \return on error - negative error code,
* if devices is NULL - total number of devices available on the system,
* alternatively the number of devices stored in devices[], which is
* capped by the max_devices.
*/
drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
{
return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
}
drm_public char *drmGetDeviceNameFromFd2(int fd)
{
#ifdef __linux__
struct stat sbuf;
char path[PATH_MAX + 1], *value;
unsigned int maj, min;
if (fstat(fd, &sbuf))
return NULL;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return NULL;
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
value = sysfs_uevent_get(path, "DEVNAME");
if (!value)
return NULL;
snprintf(path, sizeof(path), "/dev/%s", value);
free(value);
return strdup(path);
#elif defined(__FreeBSD__)
return drmGetDeviceNameFromFd(fd);
#else
struct stat sbuf;
char node[PATH_MAX + 1];
const char *dev_name;
int node_type;
int maj, min, n;
if (fstat(fd, &sbuf))
return NULL;
maj = major(sbuf.st_rdev);
min = minor(sbuf.st_rdev);
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return NULL;
node_type = drmGetMinorType(maj, min);
if (node_type == -1)
return NULL;
dev_name = drmGetDeviceName(node_type);
if (!dev_name)
return NULL;
n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
if (n == -1 || n >= PATH_MAX)
return NULL;
return strdup(node);
#endif
}
drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
{
struct drm_syncobj_create args;
int ret;
memclear(args);
args.flags = flags;
args.handle = 0;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
if (ret)
return ret;
*handle = args.handle;
return 0;
}
drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
{
struct drm_syncobj_destroy args;
memclear(args);
args.handle = handle;
return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
}
drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
{
struct drm_syncobj_handle args;
int ret;
memclear(args);
args.fd = -1;
args.handle = handle;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
if (ret)
return ret;
*obj_fd = args.fd;
return 0;
}
drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
{
struct drm_syncobj_handle args;
int ret;
memclear(args);
args.fd = obj_fd;
args.handle = 0;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
if (ret)
return ret;
*handle = args.handle;
return 0;
}
drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
int sync_file_fd)
{
struct drm_syncobj_handle args;
memclear(args);
args.fd = sync_file_fd;
args.handle = handle;
args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
}
drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
int *sync_file_fd)
{
struct drm_syncobj_handle args;
int ret;
memclear(args);
args.fd = -1;
args.handle = handle;
args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
if (ret)
return ret;
*sync_file_fd = args.fd;
return 0;
}
drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
int64_t timeout_nsec, unsigned flags,
uint32_t *first_signaled)
{
struct drm_syncobj_wait args;
int ret;
memclear(args);
args.handles = (uintptr_t)handles;
args.timeout_nsec = timeout_nsec;
args.count_handles = num_handles;
args.flags = flags;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
if (ret < 0)
return -errno;
if (first_signaled)
*first_signaled = args.first_signaled;
return ret;
}
drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
uint32_t handle_count)
{
struct drm_syncobj_array args;
int ret;
memclear(args);
args.handles = (uintptr_t)handles;
args.count_handles = handle_count;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
return ret;
}
drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
uint32_t handle_count)
{
struct drm_syncobj_array args;
int ret;
memclear(args);
args.handles = (uintptr_t)handles;
args.count_handles = handle_count;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
return ret;
}
drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
uint64_t *points, uint32_t handle_count)
{
struct drm_syncobj_timeline_array args;
int ret;
memclear(args);
args.handles = (uintptr_t)handles;
args.points = (uintptr_t)points;
args.count_handles = handle_count;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
return ret;
}
drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
unsigned num_handles,
int64_t timeout_nsec, unsigned flags,
uint32_t *first_signaled)
{
struct drm_syncobj_timeline_wait args;
int ret;
memclear(args);
args.handles = (uintptr_t)handles;
args.points = (uintptr_t)points;
args.timeout_nsec = timeout_nsec;
args.count_handles = num_handles;
args.flags = flags;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
if (ret < 0)
return -errno;
if (first_signaled)
*first_signaled = args.first_signaled;
return ret;
}
drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
uint32_t handle_count)
{
struct drm_syncobj_timeline_array args;
int ret;
memclear(args);
args.handles = (uintptr_t)handles;
args.points = (uintptr_t)points;
args.count_handles = handle_count;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
if (ret)
return ret;
return 0;
}
drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
uint32_t handle_count, uint32_t flags)
{
struct drm_syncobj_timeline_array args;
memclear(args);
args.handles = (uintptr_t)handles;
args.points = (uintptr_t)points;
args.count_handles = handle_count;
args.flags = flags;
return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
}
drm_public int drmSyncobjTransfer(int fd,
uint32_t dst_handle, uint64_t dst_point,
uint32_t src_handle, uint64_t src_point,
uint32_t flags)
{
struct drm_syncobj_transfer args;
int ret;
memclear(args);
args.src_handle = src_handle;
args.dst_handle = dst_handle;
args.src_point = src_point;
args.dst_point = dst_point;
args.flags = flags;
ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);
return ret;
}
drm_public int drmSyncobjEventfd(int fd, uint32_t handle, uint64_t point, int ev_fd,
uint32_t flags)
{
struct drm_syncobj_eventfd args;
memclear(args);
args.handle = handle;
args.point = point;
args.fd = ev_fd;
args.flags = flags;
return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_EVENTFD, &args);
}
static char *
drmGetFormatModifierFromSimpleTokens(uint64_t modifier)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(drm_format_modifier_table); i++) {
if (drm_format_modifier_table[i].modifier == modifier)
return strdup(drm_format_modifier_table[i].modifier_name);
}
return NULL;
}
/** Retrieves a human-readable representation of a vendor (as a string) from
* the format token modifier
*
* \param modifier the format modifier token
* \return a char pointer to the human-readable form of the vendor. Caller is
* responsible for freeing it.
*/
drm_public char *
drmGetFormatModifierVendor(uint64_t modifier)
{
unsigned int i;
uint8_t vendor = fourcc_mod_get_vendor(modifier);
for (i = 0; i < ARRAY_SIZE(drm_format_modifier_vendor_table); i++) {
if (drm_format_modifier_vendor_table[i].vendor == vendor)
return strdup(drm_format_modifier_vendor_table[i].vendor_name);
}
return NULL;
}
/** Retrieves a human-readable representation string from a format token
* modifier
*
* If the dedicated function was not able to extract a valid name or searching
* the format modifier was not in the table, this function would return NULL.
*
* \param modifier the token format
* \return a malloc'ed string representation of the modifier. Caller is
* responsible for freeing the string returned.
*
*/
drm_public char *
drmGetFormatModifierName(uint64_t modifier)
{
uint8_t vendorid = fourcc_mod_get_vendor(modifier);
char *modifier_found = NULL;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(modifier_format_vendor_table); i++) {
if (modifier_format_vendor_table[i].vendor == vendorid)
modifier_found = modifier_format_vendor_table[i].vendor_cb(modifier);
}
if (!modifier_found)
return drmGetFormatModifierFromSimpleTokens(modifier);
return modifier_found;
}
/**
* Get a human-readable name for a DRM FourCC format.
*
* \param format The format.
* \return A malloc'ed string containing the format name. Caller is responsible
* for freeing it.
*/
drm_public char *
drmGetFormatName(uint32_t format)
{
char *str, code[5];
const char *be;
size_t str_size, i;
be = (format & DRM_FORMAT_BIG_ENDIAN) ? "_BE" : "";
format &= ~DRM_FORMAT_BIG_ENDIAN;
if (format == DRM_FORMAT_INVALID)
return strdup("INVALID");
code[0] = (char) ((format >> 0) & 0xFF);
code[1] = (char) ((format >> 8) & 0xFF);
code[2] = (char) ((format >> 16) & 0xFF);
code[3] = (char) ((format >> 24) & 0xFF);
code[4] = '\0';
/* Trim spaces at the end */
for (i = 3; i > 0 && code[i] == ' '; i--)
code[i] = '\0';
str_size = strlen(code) + strlen(be) + 1;
str = malloc(str_size);
if (!str)
return NULL;
snprintf(str, str_size, "%s%s", code, be);
return str;
}
|