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
|
/* $OpenBSD: i915_drv.c,v 1.123 2012/09/25 10:19:46 jsg Exp $ */
/*
* Copyright (c) 2008-2009 Owain G. Ainsworth <oga@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*-
* Copyright © 2008 Intel Corporation
* Copyright 2003 Tungsten Graphics, 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
* VA LINUX SYSTEMS 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.
*
* Authors:
* Gareth Hughes <gareth@valinux.com>
* Eric Anholt <eric@anholt.net>
*
*/
#include "drmP.h"
#include "drm.h"
#include "i915_drm.h"
#include "i915_drv.h"
#include "intel_drv.h"
#include <machine/pmap.h>
#include <sys/queue.h>
#include <sys/workq.h>
uint32_t i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size,
int tiling_mode);
uint32_t i915_gem_get_gtt_alignment(struct drm_device *dev,
uint32_t size, int tiling_mode);
void i915_gem_object_finish_gtt(struct drm_i915_gem_object *);
void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *);
int i915_gem_init_phys_object(struct drm_device *, int, int, int);
int i915_gem_phys_pwrite(struct drm_device *, struct drm_i915_gem_object *,
struct drm_i915_gem_pwrite *, struct drm_file *);
bool intel_enable_blt(struct drm_device *);
int i915_gem_handle_seqno_wrap(struct drm_device *);
void i915_gem_object_update_fence(struct drm_i915_gem_object *,
struct drm_i915_fence_reg *, bool);
int i915_gem_object_flush_fence(struct drm_i915_gem_object *);
struct drm_i915_fence_reg *i915_find_fence_reg(struct drm_device *);
void i915_gem_reset_ring_lists(drm_i915_private_t *,
struct intel_ring_buffer *);
void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *);
void i915_gem_request_remove_from_client(struct drm_i915_gem_request *);
int i915_gem_object_flush_active(struct drm_i915_gem_object *);
int i915_gem_check_olr(struct intel_ring_buffer *, u32);
void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
unsigned alignment, bool map_and_fenceable);
int i915_gem_wait_for_error(struct drm_device *);
int __wait_seqno(struct intel_ring_buffer *, uint32_t, bool, struct timespec *);
extern int ticks;
static inline void
i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
{
if (obj->tiling_mode)
i915_gem_release_mmap(obj);
/* As we do not have an associated fence register, we will force
* a tiling change if we ever need to acquire one.
*/
obj->fence_dirty = false;
obj->fence_reg = I915_FENCE_REG_NONE;
}
// i915_gem_info_add_obj
// i915_gem_info_remove_obj
int
i915_gem_wait_for_error(struct drm_device *dev)
{
drm_i915_private_t *dev_priv= dev->dev_private;
int ret;
if (!atomic_read(&dev_priv->mm.wedged))
return (0);
/*
* Only wait 10 seconds for the gpu reset to complete to avoid hanging
* userspace. If it takes that long something really bad is going on and
* we should simply try to bail out and fail as gracefully as possible.
*/
mtx_enter(&dev_priv->error_completion_lock);
while (dev_priv->error_completion == 0) {
ret = -msleep(&dev_priv->error_completion,
&dev_priv->error_completion_lock, PCATCH, "915wco", 10*hz);
if (ret != 0) {
mtx_leave(&dev_priv->error_completion_lock);
return (ret);
}
}
mtx_leave(&dev_priv->error_completion_lock);
if (atomic_read(&dev_priv->mm.wedged)) {
mtx_enter(&dev_priv->error_completion_lock);
dev_priv->error_completion++;
mtx_leave(&dev_priv->error_completion_lock);
}
return (0);
}
int
i915_mutex_lock_interruptible(struct drm_device *dev)
{
int ret;
ret = i915_gem_wait_for_error(dev);
if (ret)
return ret;
ret = rw_enter(&dev->dev_lock, RW_WRITE | RW_INTR);
if (ret)
return ret;
WARN_ON(i915_verify_lists(dev));
return 0;
}
static inline bool
i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
{
return obj->dmamap && !obj->active && obj->pin_count == 0;
}
int
i915_gem_init_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct drm_i915_gem_init *args = data;
if (drm_core_check_feature(dev, DRIVER_MODESET))
return (ENODEV);
DRM_LOCK();
if (args->gtt_start >= args->gtt_end ||
args->gtt_end > dev->agp->info.ai_aperture_size ||
(args->gtt_start & PAGE_MASK) != 0 ||
(args->gtt_end & PAGE_MASK) != 0) {
DRM_UNLOCK();
return (EINVAL);
}
/*
* putting stuff in the last page of the aperture can cause nasty
* problems with prefetch going into unassigned memory. Since we put
* a scratch page on all unused aperture pages, just leave the last
* page as a spill to prevent gpu hangs.
*/
if (args->gtt_end == dev->agp->info.ai_aperture_size)
args->gtt_end -= 4096;
if (agp_bus_dma_init((struct agp_softc *)dev->agp->agpdev,
dev->agp->base + args->gtt_start, dev->agp->base + args->gtt_end,
&dev_priv->agpdmat) != 0) {
DRM_UNLOCK();
return (ENOMEM);
}
dev->gtt_total = (uint32_t)(args->gtt_end - args->gtt_start);
inteldrm_set_max_obj_size(dev_priv);
DRM_UNLOCK();
return 0;
}
int
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_gem_get_aperture *args = data;
/* we need a write lock here to make sure we get the right value */
DRM_LOCK();
args->aper_size = dev->gtt_total;
args->aper_available_size = (args->aper_size -
atomic_read(&dev->pin_memory));
DRM_UNLOCK();
return (0);
}
int
i915_gem_create(struct drm_file *file, struct drm_device *dev, uint64_t size,
uint32_t *handle_p)
{
struct drm_i915_gem_object *obj;
uint32_t handle;
int ret;
size = round_page(size);
if (size == 0)
return (-EINVAL);
obj = i915_gem_alloc_object(dev, size);
if (obj == NULL)
return (-ENOMEM);
handle = 0;
ret = drm_handle_create(file, &obj->base, &handle);
if (ret != 0) {
drm_unref(&obj->base.uobj);
return (-ret);
}
*handle_p = handle;
return (0);
}
int
i915_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
struct drm_mode_create_dumb *args)
{
/* have to work out size/pitch and return them */
args->pitch = roundup2(args->width * ((args->bpp + 7) / 8), 64);
args->size = args->pitch * args->height;
return (i915_gem_create(file, dev, args->size, &args->handle));
}
int
i915_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
uint32_t handle)
{
printf("%s stub\n", __func__);
return ENOSYS;
// return (drm_gem_handle_delete(file, handle));
}
/**
* Creates a new mm object and returns a handle to it.
*/
int
i915_gem_create_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct drm_i915_gem_create *args = data;
struct drm_i915_gem_object *obj;
int handle, ret;
args->size = round_page(args->size);
/*
* XXX to avoid copying between 2 objs more than half the aperture size
* we don't allow allocations that are that big. This will be fixed
* eventually by intelligently falling back to cpu reads/writes in
* such cases. (linux allows this but does cpu maps in the ddx instead).
*/
if (args->size > dev_priv->max_gem_obj_size)
return (EFBIG);
/* Allocate the new object */
obj = i915_gem_alloc_object(dev, args->size);
if (obj == NULL)
return (ENOMEM);
/* we give our reference to the handle */
ret = drm_handle_create(file_priv, &obj->base, &handle);
if (ret == 0)
args->handle = handle;
else
drm_unref(&obj->base.uobj);
return (ret);
}
// i915_gem_object_needs_bit17_swizzle
// __copy_to_user_swizzled
// __copy_from_user_swizzled
// shmem_pread_fast
// shmem_clflush_swizzled_range
// shmem_pread_slow
// i915_gem_shmem_pread
/**
* Reads data from the object referenced by handle.
*
* On error, the contents of *data are undefined.
*/
int
i915_gem_pread_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct drm_i915_gem_pread *args = data;
struct drm_i915_gem_object *obj;
char *vaddr;
bus_space_handle_t bsh;
bus_size_t bsize;
voff_t offset;
int ret;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
if (obj == NULL)
return (EBADF);
DRM_READLOCK();
drm_hold_object(&obj->base);
/*
* Bounds check source.
*/
if (args->offset > obj->base.size || args->size > obj->base.size ||
args->offset + args->size > obj->base.size) {
ret = EINVAL;
goto out;
}
ret = i915_gem_object_pin(obj, 0, 1);
if (ret) {
goto out;
}
ret = i915_gem_object_set_to_gtt_domain(obj, false);
if (ret)
goto unpin;
offset = obj->gtt_offset + args->offset;
bsize = round_page(offset + args->size) - trunc_page(offset);
if ((ret = agp_map_subregion(dev_priv->agph,
trunc_page(offset), bsize, &bsh)) != 0)
goto unpin;
vaddr = bus_space_vaddr(dev->bst, bsh);
if (vaddr == NULL) {
ret = EFAULT;
goto unmap;
}
ret = copyout(vaddr + (offset & PAGE_MASK),
(char *)(uintptr_t)args->data_ptr, args->size);
unmap:
agp_unmap_subregion(dev_priv->agph, bsh, bsize);
unpin:
i915_gem_object_unpin(obj);
out:
drm_unhold_and_unref(&obj->base);
DRM_READUNLOCK();
return (ret);
}
// fast_user_write
// i915_gem_gtt_pwrite_fast
// shmem_pwrite_fast
// shmem_pwrite_slow
// i915_gem_shmem_pwrite
/**
* Writes data to the object referenced by handle.
*
* On error, the contents of the buffer that were to be modified are undefined.
*/
int
i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct drm_i915_gem_pwrite *args = data;
struct drm_i915_gem_object *obj;
char *vaddr;
bus_space_handle_t bsh;
bus_size_t bsize;
off_t offset;
int ret = 0;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
if (obj == NULL)
return (EBADF);
DRM_READLOCK();
drm_hold_object(&obj->base);
/* Bounds check destination. */
if (args->offset > obj->base.size || args->size > obj->base.size ||
args->offset + args->size > obj->base.size) {
ret = EINVAL;
goto out;
}
if (obj->phys_obj) {
ret = i915_gem_phys_pwrite(dev, obj, args, file);
goto out;
}
ret = i915_gem_object_pin(obj, 0, 1);
if (ret) {
goto out;
}
ret = i915_gem_object_set_to_gtt_domain(obj, true);
if (ret)
goto unpin;
offset = obj->gtt_offset + args->offset;
bsize = round_page(offset + args->size) - trunc_page(offset);
if ((ret = agp_map_subregion(dev_priv->agph,
trunc_page(offset), bsize, &bsh)) != 0)
goto unpin;
vaddr = bus_space_vaddr(dev_priv->bst, bsh);
if (vaddr == NULL) {
ret = EFAULT;
goto unmap;
}
ret = copyin((char *)(uintptr_t)args->data_ptr,
vaddr + (offset & PAGE_MASK), args->size);
unmap:
agp_unmap_subregion(dev_priv->agph, bsh, bsize);
unpin:
i915_gem_object_unpin(obj);
out:
drm_unhold_and_unref(&obj->base);
DRM_READUNLOCK();
return (ret);
}
int
i915_gem_check_wedge(struct inteldrm_softc *dev_priv,
bool interruptible)
{
if (atomic_read(&dev_priv->mm.wedged) != 0) {
bool recovery_complete;
/* Give the error handler a chance to run. */
mtx_enter(&dev_priv->error_completion_lock);
recovery_complete = (&dev_priv->error_completion) > 0;
mtx_leave(&dev_priv->error_completion_lock);
/* Non-interruptible callers can't handle -EAGAIN, hence return
* -EIO unconditionally for these. */
if (!interruptible)
return -EIO;
/* Recovery complete, but still wedged means reset failure. */
if (recovery_complete)
return -EIO;
return -EAGAIN;
}
return 0;
}
/*
* Compare seqno against outstanding lazy request. Emit a request if they are
* equal.
*/
int
i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
{
int ret;
// BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
ret = 0;
if (seqno == ring->outstanding_lazy_request)
ret = i915_add_request(ring, NULL, NULL);
return ret;
}
/**
* __wait_seqno - wait until execution of seqno has finished
* @ring: the ring expected to report seqno
* @seqno: duh!
* @interruptible: do an interruptible wait (normally yes)
* @timeout: in - how long to wait (NULL forever); out - how much time remaining
*
* Returns 0 if the seqno was found within the alloted time. Else returns the
* errno with remaining time filled in timeout argument.
*/
int
__wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno,
bool interruptible, struct timespec *timeout)
{
struct drm_device *dev = ring->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
int ret = 0;
mtx_enter(&dev_priv->irq_lock);
if (!i915_seqno_passed(ring->get_seqno(ring, true), seqno)) {
ring->irq_get(ring);
while (ret == 0) {
if (i915_seqno_passed(ring->get_seqno(ring, false),
seqno) || dev_priv->mm.wedged)
break;
ret = msleep(ring, &dev_priv->irq_lock,
PZERO | (interruptible ? PCATCH : 0),
"gemwt", 0);
}
ring->irq_put(ring);
}
mtx_leave(&dev_priv->irq_lock);
if (dev_priv->mm.wedged)
ret = EIO;
return (ret);
}
/**
* Waits for a sequence number to be signaled, and cleans up the
* request and object lists appropriately for that event.
*/
int
i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
{
struct drm_device *dev = ring->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
bool interruptible = dev_priv->mm.interruptible;
int ret;
// BUG_ON(!mutex_is_locked(&dev->struct_mutex));
BUG_ON(seqno == 0);
ret = i915_gem_check_wedge(dev_priv, interruptible);
if (ret)
return (ret);
ret = i915_gem_check_olr(ring, seqno);
if (ret)
return ret;
return __wait_seqno(ring, seqno, interruptible, NULL);
}
int
i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
bool readonly)
{
struct intel_ring_buffer *ring = obj->ring;
u32 seqno;
int ret;
seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
if (seqno == 0)
return 0;
ret = i915_wait_seqno(ring, seqno);
if (ret)
return ret;
i915_gem_retire_requests_ring(ring);
/* Manually manage the write flush as we may have not yet
* retired the buffer.
*/
if (obj->last_write_seqno &&
i915_seqno_passed(seqno, obj->last_write_seqno)) {
obj->last_write_seqno = 0;
obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
}
return 0;
}
// i915_gem_object_wait_rendering__nonblocking
/**
* Called when user space prepares to use an object with the CPU, either through
* the mmap ioctl's mapping or a GTT mapping.
*/
int
i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct drm_i915_gem_set_domain*args = data;
struct drm_i915_gem_object *obj;
uint32_t read_domains = args->read_domains;
uint32_t write_domain = args->write_domain;
int ret;
/*
* Only handle setting domains to types we allow the cpu to see.
* while linux allows the CPU domain here, we only allow GTT since that
* is all that we let userland near.
* Also sanity check that having something in the write domain implies
* it's in the read domain, and only that read domain.
*/
if ((write_domain | read_domains) & ~I915_GEM_DOMAIN_GTT ||
(write_domain != 0 && read_domains != write_domain))
return (EINVAL);
ret = i915_mutex_lock_interruptible(dev);
if (ret)
return ret;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
if (&obj->base == NULL) {
ret = EBADF;
goto unlock;
}
drm_hold_object(&obj->base);
ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
drm_unhold_and_unref(&obj->base);
/*
* Silently promote `you're not bound, there was nothing to do'
* to success, since the client was just asking us to make sure
* everything was done.
*/
if (ret == EINVAL)
ret = 0;
unlock:
DRM_UNLOCK();
return ret;
}
// i915_gem_sw_finish_ioctl
// i915_gem_mmap_ioctl
int
i915_gem_fault(struct drm_obj *gem_obj, struct uvm_faultinfo *ufi,
off_t offset, vaddr_t vaddr, vm_page_t *pps, int npages, int centeridx,
vm_prot_t access_type, int flags)
{
struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
struct drm_device *dev = obj->base.dev;
drm_i915_private_t *dev_priv = dev->dev_private;
paddr_t paddr;
int lcv, ret;
int write = !!(access_type & VM_PROT_WRITE);
vm_prot_t mapprot;
boolean_t locked = TRUE;
/* Are we about to suspend?, if so wait until we're done */
if (dev_priv->sc_flags & INTELDRM_QUIET) {
/* we're about to sleep, unlock the map etc */
uvmfault_unlockall(ufi, NULL, &obj->base.uobj, NULL);
while (dev_priv->sc_flags & INTELDRM_QUIET)
tsleep(&dev_priv->flags, 0, "intelflt", 0);
dev_priv->entries++;
/*
* relock so we're in the same state we would be in if we
* were not quiesced before
*/
locked = uvmfault_relock(ufi);
if (locked) {
drm_lock_obj(&obj->base);
} else {
dev_priv->entries--;
if (dev_priv->sc_flags & INTELDRM_QUIET)
wakeup(&dev_priv->entries);
return (VM_PAGER_REFAULT);
}
} else {
dev_priv->entries++;
}
if (rw_enter(&dev->dev_lock, RW_NOSLEEP | RW_READ) != 0) {
uvmfault_unlockall(ufi, NULL, &obj->base.uobj, NULL);
DRM_READLOCK();
locked = uvmfault_relock(ufi);
if (locked)
drm_lock_obj(&obj->base);
}
if (locked)
drm_hold_object_locked(&obj->base);
else { /* obj already unlocked */
dev_priv->entries--;
if (dev_priv->sc_flags & INTELDRM_QUIET)
wakeup(&dev_priv->entries);
return (VM_PAGER_REFAULT);
}
/* we have a hold set on the object now, we can unlock so that we can
* sleep in binding and flushing.
*/
drm_unlock_obj(&obj->base);
/* Now bind into the GTT if needed */
if (!obj->map_and_fenceable) {
ret = i915_gem_object_unbind(obj);
if (ret)
goto error;
}
if (obj->dmamap == NULL) {
ret = i915_gem_object_bind_to_gtt(obj, 0, true);
if (ret)
goto error;
ret = i915_gem_object_set_to_gtt_domain(obj, write);
if (ret)
goto error;
}
if (obj->tiling_mode == I915_TILING_NONE)
ret = i915_gem_object_put_fence(obj);
else
ret = i915_gem_object_get_fence(obj);
if (ret)
goto error;
if (i915_gem_object_is_inactive(obj))
list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
obj->fault_mappable = true;
mapprot = ufi->entry->protection;
/*
* if it's only a read fault, we only put ourselves into the gtt
* read domain, so make sure we fault again and set ourselves to write.
* this prevents us needing userland to do domain management and get
* it wrong, and makes us fully coherent with the gpu re mmap.
*/
if (write == 0)
mapprot &= ~VM_PROT_WRITE;
/* XXX try and be more efficient when we do this */
for (lcv = 0 ; lcv < npages ; lcv++, offset += PAGE_SIZE,
vaddr += PAGE_SIZE) {
if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
continue;
if (pps[lcv] == PGO_DONTCARE)
continue;
paddr = dev->agp->base + obj->gtt_offset + offset;
if (pmap_enter(ufi->orig_map->pmap, vaddr, paddr,
mapprot, PMAP_CANFAIL | mapprot) != 0) {
drm_unhold_object(&obj->base);
uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
NULL, NULL);
DRM_READUNLOCK();
dev_priv->entries--;
if (dev_priv->sc_flags & INTELDRM_QUIET)
wakeup(&dev_priv->entries);
uvm_wait("intelflt");
return (VM_PAGER_REFAULT);
}
}
error:
drm_unhold_object(&obj->base);
uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, NULL, NULL);
DRM_READUNLOCK();
dev_priv->entries--;
if (dev_priv->sc_flags & INTELDRM_QUIET)
wakeup(&dev_priv->entries);
pmap_update(ufi->orig_map->pmap);
if (ret == EIO) {
/*
* EIO means we're wedged, so upon resetting the gpu we'll
* be alright and can refault. XXX only on resettable chips.
*/
ret = VM_PAGER_REFAULT;
} else if (ret) {
ret = VM_PAGER_ERROR;
} else {
ret = VM_PAGER_OK;
}
return (ret);
}
/**
* i915_gem_release_mmap - remove physical page mappings
* @obj: obj in question
*
* Preserve the reservation of the mmapping with the DRM core code, but
* relinquish ownership of the pages back to the system.
*
* It is vital that we remove the page mapping if we have mapped a tiled
* object through the GTT and then lose the fence register due to
* resource pressure. Similarly if the object has been moved out of the
* aperture, than pages mapped into userspace must be revoked. Removing the
* mapping will then trigger a page fault on the next user access, allowing
* fixup by i915_gem_fault().
*/
void
i915_gem_release_mmap(struct drm_i915_gem_object *obj)
{
struct inteldrm_softc *dev_priv = obj->base.dev->dev_private;
struct vm_page *pg;
if (!obj->fault_mappable)
return;
for (pg = &dev_priv->pgs[atop(obj->gtt_offset)];
pg != &dev_priv->pgs[atop(obj->gtt_offset + obj->base.size)];
pg++)
pmap_page_protect(pg, VM_PROT_NONE);
obj->fault_mappable = false;
}
uint32_t
i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
{
uint32_t gtt_size;
if (INTEL_INFO(dev)->gen >= 4 ||
tiling_mode == I915_TILING_NONE)
return size;
/* Previous chips need a power-of-two fence region when tiling */
if (INTEL_INFO(dev)->gen == 3)
gtt_size = 1024*1024;
else
gtt_size = 512*1024;
while (gtt_size < size)
gtt_size <<= 1;
return gtt_size;
}
/**
* i915_gem_get_gtt_alignment - return required GTT alignment for an object
* @obj: object to check
*
* Return the required GTT alignment for an object, taking into account
* potential fence register mapping.
*/
uint32_t
i915_gem_get_gtt_alignment(struct drm_device *dev,
uint32_t size,
int tiling_mode)
{
/*
* Minimum alignment is 4k (GTT page size), but might be greater
* if a fence register is needed for the object.
*/
if (INTEL_INFO(dev)->gen >= 4 ||
tiling_mode == I915_TILING_NONE)
return 4096;
/*
* Previous chips need to be aligned to the size of the smallest
* fence register that can contain the object.
*/
return i915_gem_get_gtt_size(dev, size, tiling_mode);
}
/**
* i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
* unfenced object
* @dev: the device
* @size: size of the object
* @tiling_mode: tiling mode of the object
*
* Return the required GTT alignment for an object, only taking into account
* unfenced tiled surface requirements.
*/
uint32_t
i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
uint32_t size,
int tiling_mode)
{
/*
* Minimum alignment is 4k (GTT page size) for sane hw.
*/
if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
tiling_mode == I915_TILING_NONE)
return 4096;
/* Previous hardware however needs to be aligned to a power-of-two
* tile height. The simplest method for determining this is to reuse
* the power-of-tile object size.
*/
return i915_gem_get_gtt_size(dev, size, tiling_mode);
}
// i915_gem_object_create_mmap_offset
// i915_gem_object_free_mmap_offset
int
i915_gem_mmap_gtt(struct drm_file *file, struct drm_device *dev,
uint32_t handle, uint64_t *mmap_offset)
{
struct drm_i915_gem_object *obj;
struct drm_local_map *map;
voff_t offset;
vsize_t end, nsize;
int ret;
offset = (voff_t)*mmap_offset;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
if (obj == NULL)
return (EBADF);
/* Since we are doing purely uvm-related operations here we do
* not need to hold the object, a reference alone is sufficient
*/
/* Check size. */
if (offset > obj->base.size) {
ret = EINVAL;
goto done;
}
if (obj->madv != I915_MADV_WILLNEED) {
DRM_ERROR("Attempting to mmap a purgeable buffer\n");
ret = EINVAL;
goto done;
}
ret = i915_gem_object_bind_to_gtt(obj, 0, true);
if (ret) {
printf("%s: failed to bind\n", __func__);
goto done;
}
i915_gem_object_move_to_inactive(obj);
end = round_page(offset + obj->base.size);
offset = trunc_page(offset);
nsize = end - offset;
ret = drm_addmap(dev, offset + obj->gtt_offset, nsize, _DRM_AGP,
_DRM_WRITE_COMBINING, &map);
done:
if (ret == 0)
*mmap_offset = map->ext;
else
drm_unref(&obj->base.uobj);
return (ret);
}
int
i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct inteldrm_softc *dev_priv;
struct drm_i915_gem_mmap_gtt *args;
dev_priv = dev->dev_private;
args = data;
return (i915_gem_mmap_gtt(file, dev, args->handle, &args->offset));
}
/* Immediately discard the backing storage */
void
i915_gem_object_truncate(struct drm_i915_gem_object *obj)
{
DRM_ASSERT_HELD(&obj->base);
simple_lock(&obj->base.uao->vmobjlock);
obj->base.uao->pgops->pgo_flush(obj->base.uao, 0, obj->base.size,
PGO_ALLPAGES | PGO_FREE);
simple_unlock(&obj->base.uao->vmobjlock);
obj->madv = __I915_MADV_PURGED;
}
// i915_gem_object_is_purgeable
// i915_gem_object_put_pages_gtt
// i915_gem_object_put_pages
// __i915_gem_shrink
// i915_gem_purge
// i915_gem_shrink_all
// i915_gem_object_get_pages_gtt
// i915_gem_object_get_pages
/* called locked */
void
i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
struct intel_ring_buffer *ring)
{
struct drm_device *dev = obj->base.dev;
struct inteldrm_softc *dev_priv = dev->dev_private;
u_int32_t seqno;
seqno = intel_ring_get_seqno(ring);
BUG_ON(ring == NULL);
obj->ring = ring;
/* Add a reference if we're newly entering the active list. */
if (!obj->active) {
drm_gem_object_reference(&obj->base);
obj->active = 1;
}
/* Move from whatever list we were on to the tail of execution. */
list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
list_move_tail(&obj->ring_list, &ring->active_list);
obj->last_read_seqno = seqno;
if (obj->fenced_gpu_access) {
obj->last_fenced_seqno = seqno;
/* Bump MRU to take account of the delayed flush */
if (obj->fence_reg != I915_FENCE_REG_NONE) {
struct drm_i915_fence_reg *reg;
reg = &dev_priv->fence_regs[obj->fence_reg];
list_move_tail(®->lru_list,
&dev_priv->mm.fence_list);
}
}
}
/* called locked */
void
i915_gem_object_move_to_inactive_locked(struct drm_i915_gem_object *obj)
{
struct drm_device *dev = obj->base.dev;
struct inteldrm_softc *dev_priv = dev->dev_private;
DRM_OBJ_ASSERT_LOCKED(&obj->base);
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
BUG_ON(!obj->active);
if (obj->pin_count != 0)
list_del_init(&obj->mm_list);
else
list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
list_del_init(&obj->ring_list);
obj->ring = NULL;
obj->last_read_seqno = 0;
obj->last_write_seqno = 0;
obj->base.write_domain = 0;
obj->last_fenced_seqno = 0;
obj->fenced_gpu_access = false;
obj->active = 0;
drm_gem_object_unreference(&obj->base);
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
}
/* If you call this on an object that you have held, you must have your own
* reference, not just the reference from the active list.
*/
void
i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
{
drm_lock_obj(&obj->base);
/* unlocks object lock */
i915_gem_object_move_to_inactive_locked(obj);
}
int
i915_gem_handle_seqno_wrap(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct intel_ring_buffer *ring;
int ret, i, j;
/* The hardware uses various monotonic 32-bit counters, if we
* detect that they will wraparound we need to idle the GPU
* and reset those counters.
*/
ret = 0;
for_each_ring(ring, dev_priv, i) {
for (j = 0; j < nitems(ring->sync_seqno); j++)
ret |= ring->sync_seqno[j] != 0;
}
if (ret == 0)
return ret;
ret = i915_gpu_idle(dev);
if (ret)
return ret;
i915_gem_retire_requests(dev_priv);
for_each_ring(ring, dev_priv, i) {
for (j = 0; j < nitems(ring->sync_seqno); j++)
ring->sync_seqno[j] = 0;
}
return 0;
}
int
i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
{
drm_i915_private_t *dev_priv = dev->dev_private;
/* reserve 0 for non-seqno */
if (dev_priv->next_seqno == 0) {
int ret = i915_gem_handle_seqno_wrap(dev);
if (ret)
return ret;
dev_priv->next_seqno = 1;
}
*seqno = dev_priv->next_seqno++;
return 0;
}
/**
* Creates a new sequence number, emitting a write of it to the status page
* plus an interrupt, which will trigger and interrupt if they are currently
* enabled.
*
* Returned sequence numbers are nonzero on success.
*/
int
i915_add_request(struct intel_ring_buffer *ring,
struct drm_file *file,
u32 *out_seqno)
{
drm_i915_private_t *dev_priv = ring->dev->dev_private;
struct drm_i915_gem_request *request;
u32 request_ring_position;
int was_empty, ret;
/*
* Emit any outstanding flushes - execbuf can fail to emit the flush
* after having emitted the batchbuffer command. Hence we need to fix
* things up similar to emitting the lazy request. The difference here
* is that the flush _must_ happen before the next request, no matter
* what.
*/
ret = intel_ring_flush_all_caches(ring);
if (ret)
return ret;
request = drm_calloc(1, sizeof(*request));
if (request == NULL) {
printf("%s: failed to allocate request\n", __func__);
return -ENOMEM;
}
/* Record the position of the start of the request so that
* should we detect the updated seqno part-way through the
* GPU processing the request, we never over-estimate the
* position of the head.
*/
request_ring_position = intel_ring_get_tail(ring);
ret = ring->add_request(ring);
if (ret) {
drm_free(request);
return ret;
}
request->seqno = intel_ring_get_seqno(ring);
request->ring = ring;
request->tail = request_ring_position;
request->emitted_ticks = ticks;
was_empty = list_empty(&ring->request_list);
list_add_tail(&request->list, &ring->request_list);
DRM_DEBUG("%d\n", request->seqno);
if (file) {
struct drm_i915_file_private *file_priv = file->driver_priv;
mtx_enter(&file_priv->mm.lock);
request->file_priv = file_priv;
list_add_tail(&request->client_list,
&file_priv->mm.request_list);
mtx_leave(&file_priv->mm.lock);
}
ring->outstanding_lazy_request = 0;
if (dev_priv->mm.suspended == 0) {
if (was_empty)
timeout_add_sec(&dev_priv->mm.retire_timer, 1);
/* XXX was_empty? */
timeout_add_msec(&dev_priv->hangcheck_timer,
DRM_I915_HANGCHECK_PERIOD);
}
if (out_seqno)
*out_seqno = request->seqno;
return 0;
}
void
i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
{
struct drm_i915_file_private *file_priv = request->file_priv;
if (!file_priv)
return;
mtx_enter(&file_priv->mm.lock);
if (request->file_priv) {
list_del(&request->client_list);
request->file_priv = NULL;
}
mtx_leave(&file_priv->mm.lock);
}
void
i915_gem_reset_ring_lists(drm_i915_private_t *dev_priv,
struct intel_ring_buffer *ring)
{
while (!list_empty(&ring->request_list)) {
struct drm_i915_gem_request *request;
request = list_first_entry(&ring->request_list,
struct drm_i915_gem_request,
list);
list_del(&request->list);
i915_gem_request_remove_from_client(request);
free(request, M_DRM);
}
while (!list_empty(&ring->active_list)) {
struct drm_i915_gem_object *obj;
obj = list_first_entry(&ring->active_list,
struct drm_i915_gem_object,
ring_list);
i915_gem_object_move_to_inactive(obj);
}
}
void
i915_gem_reset_fences(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
int i;
for (i = 0; i < dev_priv->num_fence_regs; i++) {
struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
i915_gem_write_fence(dev, i, NULL);
if (reg->obj)
i915_gem_object_fence_lost(reg->obj);
reg->pin_count = 0;
reg->obj = NULL;
INIT_LIST_HEAD(®->lru_list);
}
INIT_LIST_HEAD(&dev_priv->mm.fence_list);
}
void
i915_gem_reset(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_object *obj;
int i;
for (i = 0; i < I915_NUM_RINGS; i++)
i915_gem_reset_ring_lists(dev_priv, &dev_priv->rings[i]);
/* Move everything out of the GPU domains to ensure we do any
* necessary invalidation upon reuse.
*/
list_for_each_entry(obj,
&dev_priv->mm.inactive_list,
mm_list)
{
obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
}
/* The fence registers are invalidated so clear them out */
i915_gem_reset_fences(dev);
}
/**
* This function clears the request list as sequence numbers are passed.
*/
void
i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
{
uint32_t seqno;
if (list_empty(&ring->request_list))
return;
seqno = ring->get_seqno(ring, true);
while (!list_empty(&ring->request_list)) {
struct drm_i915_gem_request *request;
request = list_first_entry(&ring->request_list,
struct drm_i915_gem_request,
list);
if (!i915_seqno_passed(seqno, request->seqno))
break;
// trace_i915_gem_request_retire(ring, request->seqno);
/* We know the GPU must have read the request to have
* sent us the seqno + interrupt, so use the position
* of tail of the request to update the last known position
* of the GPU head.
*/
ring->last_retired_head = request->tail;
list_del(&request->list);
i915_gem_request_remove_from_client(request);
drm_free(request);
}
/* Move any buffers on the active list that are no longer referenced
* by the ringbuffer to the flushing/inactive lists as appropriate.
*/
while (!list_empty(&ring->active_list)) {
struct drm_i915_gem_object *obj;
obj = list_first_entry(&ring->active_list,
struct drm_i915_gem_object,
ring_list);
if (!i915_seqno_passed(seqno, obj->last_read_seqno))
break;
i915_gem_object_move_to_inactive(obj);
}
}
void
i915_gem_retire_requests(struct inteldrm_softc *dev_priv)
{
struct intel_ring_buffer *ring;
int i;
for_each_ring(ring, dev_priv, i)
i915_gem_retire_requests_ring(ring);
}
void
i915_gem_retire_work_handler(void *arg1, void *unused)
{
struct inteldrm_softc *dev_priv = arg1;
struct intel_ring_buffer *ring;
bool idle;
int i;
i915_gem_retire_requests(dev_priv);
idle = true;
for_each_ring(ring, dev_priv, i) {
if (ring->gpu_caches_dirty)
i915_add_request(ring, NULL, NULL);
idle &= list_empty(&ring->request_list);
}
if (!dev_priv->mm.suspended && !idle)
timeout_add_sec(&dev_priv->mm.retire_timer, 1);
}
/**
* Ensures that an object will eventually get non-busy by flushing any required
* write domains, emitting any outstanding lazy request and retiring and
* completed requests.
*/
int
i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
{
int ret;
if (obj->active) {
ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
if (ret)
return ret;
i915_gem_retire_requests_ring(obj->ring);
}
return 0;
}
// i915_gem_wait_ioctl
/**
* i915_gem_object_sync - sync an object to a ring.
*
* @obj: object which may be in use on another ring.
* @to: ring we wish to use the object on. May be NULL.
*
* This code is meant to abstract object synchronization with the GPU.
* Calling with NULL implies synchronizing the object with the CPU
* rather than a particular GPU ring.
*
* Returns 0 if successful, else propagates up the lower layer error.
*/
int
i915_gem_object_sync(struct drm_i915_gem_object *obj,
struct intel_ring_buffer *to)
{
struct intel_ring_buffer *from = obj->ring;
u32 seqno;
int ret, idx;
if (from == NULL || to == from)
return 0;
if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
return i915_gem_object_wait_rendering(obj, false);
idx = intel_ring_sync_index(from, to);
seqno = obj->last_read_seqno;
if (seqno <= from->sync_seqno[idx])
return 0;
ret = i915_gem_check_olr(obj->ring, seqno);
if (ret)
return ret;
ret = to->sync_to(to, from, seqno);
if (!ret)
/* We use last_read_seqno because sync_to()
* might have just caused seqno wrap under
* the radar.
*/
from->sync_seqno[idx] = obj->last_read_seqno;
return ret;
}
void
i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
{
u32 old_write_domain, old_read_domains;
/* Act a barrier for all accesses through the GTT */
DRM_MEMORYBARRIER();
/* Force a pagefault for domain tracking on next user access */
i915_gem_release_mmap(obj);
if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
return;
old_read_domains = obj->base.read_domains;
old_write_domain = obj->base.write_domain;
obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
#if 0
trace_i915_gem_object_change_domain(obj,
old_read_domains,
old_write_domain);
#endif
}
/**
* Unbinds an object from the GTT aperture.
*
* XXX track dirty and pass down to uvm (note, DONTNEED buffers are clean).
*/
int
i915_gem_object_unbind(struct drm_i915_gem_object *obj)
{
struct drm_device *dev = obj->base.dev;
struct inteldrm_softc *dev_priv = dev->dev_private;
int ret = 0;
DRM_ASSERT_HELD(&obj->base);
/*
* if it's already unbound, or we've already done lastclose, just
* let it happen. XXX does this fail to unwire?
*/
if (obj->dmamap == NULL || dev_priv->agpdmat == NULL)
return 0;
if (obj->pin_count != 0) {
DRM_ERROR("Attempting to unbind pinned buffer\n");
return (EINVAL);
}
ret = i915_gem_object_finish_gpu(obj);
if (ret == ERESTART || ret == EINTR)
return ret;
/* Continue on if we fail due to EIO, the GPU is hung so we
* should be safe and we need to cleanup or else we might
* cause memory corruption through use-after-free.
*/
i915_gem_object_finish_gtt(obj);
/* release the fence reg _after_ flushing */
ret = i915_gem_object_put_fence(obj);
if (ret == ERESTART || ret == EINTR)
return ret;
KASSERT(!obj->active);
/* if it's purgeable don't bother dirtying the pages */
if (i915_gem_object_is_purgeable(obj))
obj->dirty = 0;
/*
* unload the map, then unwire the backing object.
*/
i915_gem_object_save_bit_17_swizzle(obj);
bus_dmamap_unload(dev_priv->agpdmat, obj->dmamap);
uvm_objunwire(obj->base.uao, 0, obj->base.size);
/* XXX persistent dmamap worth the memory? */
bus_dmamap_destroy(dev_priv->agpdmat, obj->dmamap);
obj->dmamap = NULL;
free(obj->dma_segs, M_DRM);
obj->dma_segs = NULL;
/* XXX this should change whether we tell uvm the page is dirty */
obj->dirty = 0;
list_del_init(&obj->gtt_list);
list_del_init(&obj->mm_list);
/* Avoid an unnecessary call to unbind on rebind. */
obj->map_and_fenceable = true;
obj->gtt_offset = 0;
atomic_dec(&dev->gtt_count);
atomic_sub(obj->base.size, &dev->gtt_memory);
if (i915_gem_object_is_purgeable(obj))
i915_gem_object_truncate(obj);
return (0);
}
int
i915_gpu_idle(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct intel_ring_buffer *ring;
int ret, i;
/* Flush everything onto the inactive list. */
for_each_ring(ring, dev_priv, i) {
#ifdef notyet
ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
if (ret)
return ret;
#endif
ret = intel_ring_idle(ring);
if (ret)
return ret;
}
return 0;
}
void
sandybridge_write_fence_reg(struct drm_device *dev, int reg,
struct drm_i915_gem_object *obj)
{
drm_i915_private_t *dev_priv = dev->dev_private;
uint64_t val;
if (obj) {
u32 size = obj->dmamap->dm_segs[0].ds_len;
val = (uint64_t)((obj->gtt_offset + size - 4096) &
0xfffff000) << 32;
val |= obj->gtt_offset & 0xfffff000;
val |= (uint64_t)((obj->stride / 128) - 1) <<
SANDYBRIDGE_FENCE_PITCH_SHIFT;
if (obj->tiling_mode == I915_TILING_Y)
val |= 1 << I965_FENCE_TILING_Y_SHIFT;
val |= I965_FENCE_REG_VALID;
} else
val = 0;
I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
}
void
i965_write_fence_reg(struct drm_device *dev, int reg,
struct drm_i915_gem_object *obj)
{
drm_i915_private_t *dev_priv = dev->dev_private;
uint64_t val;
if (obj) {
u32 size = obj->dmamap->dm_segs[0].ds_len;
val = (uint64_t)((obj->gtt_offset + size - 4096) &
0xfffff000) << 32;
val |= obj->gtt_offset & 0xfffff000;
val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
if (obj->tiling_mode == I915_TILING_Y)
val |= 1 << I965_FENCE_TILING_Y_SHIFT;
val |= I965_FENCE_REG_VALID;
} else
val = 0;
I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
POSTING_READ(FENCE_REG_965_0 + reg * 8);
}
void
i915_write_fence_reg(struct drm_device *dev, int reg,
struct drm_i915_gem_object *obj)
{
drm_i915_private_t *dev_priv = dev->dev_private;
u32 val;
if (obj) {
u32 size = obj->dmamap->dm_segs[0].ds_len;
int pitch_val;
int tile_width;
WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
(size & -size) != size ||
(obj->gtt_offset & (size - 1)),
"object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
obj->gtt_offset, obj->map_and_fenceable, size);
if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
tile_width = 128;
else
tile_width = 512;
/* Note: pitch better be a power of two tile widths */
pitch_val = obj->stride / tile_width;
pitch_val = ffs(pitch_val) - 1;
val = obj->gtt_offset;
if (obj->tiling_mode == I915_TILING_Y)
val |= 1 << I830_FENCE_TILING_Y_SHIFT;
val |= I915_FENCE_SIZE_BITS(size);
val |= pitch_val << I830_FENCE_PITCH_SHIFT;
val |= I830_FENCE_REG_VALID;
} else
val = 0;
if (reg < 8)
reg = FENCE_REG_830_0 + reg * 4;
else
reg = FENCE_REG_945_8 + (reg - 8) * 4;
I915_WRITE(reg, val);
POSTING_READ(reg);
}
void
i830_write_fence_reg(struct drm_device *dev, int reg,
struct drm_i915_gem_object *obj)
{
drm_i915_private_t *dev_priv = dev->dev_private;
uint32_t val;
if (obj) {
u32 size = obj->dmamap->dm_segs[0].ds_len;
uint32_t pitch_val;
WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
(size & -size) != size ||
(obj->gtt_offset & (size - 1)),
"object 0x%08x not 512K or pot-size 0x%08x aligned\n",
obj->gtt_offset, size);
pitch_val = obj->stride / 128;
pitch_val = ffs(pitch_val) - 1;
val = obj->gtt_offset;
if (obj->tiling_mode == I915_TILING_Y)
val |= 1 << I830_FENCE_TILING_Y_SHIFT;
val |= I830_FENCE_SIZE_BITS(size);
val |= pitch_val << I830_FENCE_PITCH_SHIFT;
val |= I830_FENCE_REG_VALID;
} else
val = 0;
I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
POSTING_READ(FENCE_REG_830_0 + reg * 4);
}
void
i915_gem_write_fence(struct drm_device *dev, int reg,
struct drm_i915_gem_object *obj)
{
switch (INTEL_INFO(dev)->gen) {
case 7:
case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
case 5:
case 4: i965_write_fence_reg(dev, reg, obj); break;
case 3: i915_write_fence_reg(dev, reg, obj); break;
case 2: i830_write_fence_reg(dev, reg, obj); break;
default: break;
}
}
static inline int
fence_number(drm_i915_private_t *dev_priv,
struct drm_i915_fence_reg *fence)
{
return fence - dev_priv->fence_regs;
}
void
i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
struct drm_i915_fence_reg *fence,
bool enable)
{
drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
int reg = fence_number(dev_priv, fence);
i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
if (enable) {
obj->fence_reg = reg;
fence->obj = obj;
list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
} else {
obj->fence_reg = I915_FENCE_REG_NONE;
fence->obj = NULL;
list_del_init(&fence->lru_list);
}
}
int
i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
{
if (obj->last_fenced_seqno) {
int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
if (ret)
return ret;
obj->last_fenced_seqno = 0;
}
/* Ensure that all CPU reads are completed before installing a fence
* and all writes before removing the fence.
*/
if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
DRM_WRITEMEMORYBARRIER();
obj->fenced_gpu_access = false;
return 0;
}
int
i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
{
drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
int ret;
ret = i915_gem_object_flush_fence(obj);
if (ret)
return ret;
if (obj->fence_reg == I915_FENCE_REG_NONE)
return 0;
i915_gem_object_update_fence(obj,
&dev_priv->fence_regs[obj->fence_reg],
false);
i915_gem_object_fence_lost(obj);
return 0;
}
struct drm_i915_fence_reg *
i915_find_fence_reg(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_fence_reg *reg, *avail;
int i;
/* First try to find a free reg */
avail = NULL;
for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
reg = &dev_priv->fence_regs[i];
if (!reg->obj)
return reg;
if (!reg->pin_count)
avail = reg;
}
if (avail == NULL)
return NULL;
/* None available, try to steal one or wait for a user to finish */
list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
if (reg->pin_count)
continue;
return reg;
}
return NULL;
}
/**
* i915_gem_object_get_fence - set up fencing for an object
* @obj: object to map through a fence reg
*
* When mapping objects through the GTT, userspace wants to be able to write
* to them without having to worry about swizzling if the object is tiled.
* This function walks the fence regs looking for a free one for @obj,
* stealing one if it can't find any.
*
* It then sets up the reg based on the object's properties: address, pitch
* and tiling format.
*
* For an untiled surface, this removes any existing fence.
*/
int
i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
{
struct drm_device *dev = obj->base.dev;
drm_i915_private_t *dev_priv = dev->dev_private;
bool enable = obj->tiling_mode != I915_TILING_NONE;
struct drm_i915_fence_reg *reg;
int ret;
/* Have we updated the tiling parameters upon the object and so
* will need to serialise the write to the associated fence register?
*/
if (obj->fence_dirty) {
ret = i915_gem_object_flush_fence(obj);
if (ret)
return ret;
}
/* Just update our place in the LRU if our fence is getting reused. */
if (obj->fence_reg != I915_FENCE_REG_NONE) {
reg = &dev_priv->fence_regs[obj->fence_reg];
if (!obj->fence_dirty) {
list_move_tail(®->lru_list,
&dev_priv->mm.fence_list);
return 0;
}
} else if (enable) {
reg = i915_find_fence_reg(dev);
if (reg == NULL)
return -EDEADLK;
if (reg->obj) {
struct drm_i915_gem_object *old = reg->obj;
ret = i915_gem_object_flush_fence(old);
if (ret)
return ret;
i915_gem_object_fence_lost(old);
}
} else
return 0;
i915_gem_object_update_fence(obj, reg, enable);
obj->fence_dirty = false;
return 0;
}
// i915_gem_valid_gtt_space
// i915_gem_verify_gtt
/**
* Finds free space in the GTT aperture and binds the object there.
*/
int
i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
unsigned alignment, bool map_and_fenceable)
{
struct drm_device *dev = obj->base.dev;
drm_i915_private_t *dev_priv = dev->dev_private;
u32 size, fence_size, fence_alignment, unfenced_alignment;
bool mappable, fenceable;
int ret;
int flags;
DRM_ASSERT_HELD(&obj->base);
if (obj->madv != I915_MADV_WILLNEED) {
DRM_ERROR("Attempting to bind a purgeable object\n");
return EINVAL;
}
fence_size = i915_gem_get_gtt_size(dev,
obj->base.size,
obj->tiling_mode);
fence_alignment = i915_gem_get_gtt_alignment(dev,
obj->base.size,
obj->tiling_mode);
unfenced_alignment =
i915_gem_get_unfenced_gtt_alignment(dev,
obj->base.size,
obj->tiling_mode);
if (alignment == 0)
alignment = map_and_fenceable ? fence_alignment :
unfenced_alignment;
if (map_and_fenceable && alignment & (fence_alignment - 1)) {
DRM_ERROR("Invalid object alignment requested %u\n", alignment);
return EINVAL;
}
size = map_and_fenceable ? fence_size : obj->base.size;
#ifdef notyet
/* If the object is bigger than the entire aperture, reject it early
* before evicting everything in a vain attempt to find space.
*/
if (obj->base.size >
(map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
DRM_ERROR("Attempting to bind an object larger than the aperture\n");
return -E2BIG;
}
#endif
if ((ret = bus_dmamap_create(dev_priv->agpdmat, size, 1,
size, 0, BUS_DMA_WAITOK, &obj->dmamap)) != 0) {
DRM_ERROR("Failed to create dmamap\n");
return (ret);
}
agp_bus_dma_set_alignment(dev_priv->agpdmat, obj->dmamap,
alignment);
search_free:
switch (obj->cache_level) {
case I915_CACHE_NONE:
flags = BUS_DMA_GTT_NOCACHE;
break;
case I915_CACHE_LLC:
flags = BUS_DMA_GTT_CACHE_LLC;
break;
case I915_CACHE_LLC_MLC:
flags = BUS_DMA_GTT_CACHE_LLC_MLC;
break;
default:
BUG();
}
/*
* the helper function wires the uao then binds it to the aperture for
* us, so all we have to do is set up the dmamap then load it.
*/
ret = drm_gem_load_uao(dev_priv->agpdmat, obj->dmamap, obj->base.uao,
obj->base.size, BUS_DMA_WAITOK | obj->dma_flags | flags,
&obj->dma_segs);
/* XXX NOWAIT? */
if (ret != 0) {
/* If the gtt is empty and we're still having trouble
* fitting our object in, we're out of memory.
*/
if (list_empty(&dev_priv->mm.inactive_list) &&
list_empty(&dev_priv->mm.active_list)) {
DRM_ERROR("GTT full, but LRU list empty\n");
goto error;
}
ret = i915_gem_evict_something(dev_priv, obj->base.size);
if (ret != 0)
goto error;
goto search_free;
}
i915_gem_object_save_bit_17_swizzle(obj);
list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
/* Assert that the object is not currently in any GPU domain. As it
* wasn't in the GTT, there shouldn't be any way it could have been in
* a GPU cache
*/
BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
obj->gtt_offset = obj->dmamap->dm_segs[0].ds_addr - dev->agp->base;
fenceable =
obj->dmamap->dm_segs[0].ds_len == fence_size &&
(obj->dmamap->dm_segs[0].ds_addr & (fence_alignment - 1)) == 0;
#ifdef notyet
mappable =
obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
#else
mappable = true;
#endif
obj->map_and_fenceable = mappable && fenceable;
atomic_inc(&dev->gtt_count);
atomic_add(obj->base.size, &dev->gtt_memory);
return (0);
error:
bus_dmamap_destroy(dev_priv->agpdmat, obj->dmamap);
obj->dmamap = NULL;
obj->gtt_offset = 0;
return (ret);
}
void
i915_gem_clflush_object(struct drm_i915_gem_object *obj)
{
struct drm_device *dev = obj->base.dev;
drm_i915_private_t *dev_priv = dev->dev_private;
/* If we don't have a page list set up, then we're not pinned
* to GPU, and we can ignore the cache flush because it'll happen
* again at bind time.
*/
if (obj->dmamap == NULL)
return;
/* If the GPU is snooping the contents of the CPU cache,
* we do not need to manually clear the CPU cache lines. However,
* the caches are only snooped when the render cache is
* flushed/invalidated. As we always have to emit invalidations
* and flushes when moving into and out of the RENDER domain, correct
* snooping behaviour occurs naturally as the result of our domain
* tracking.
*/
if (obj->cache_level != I915_CACHE_NONE)
return;
bus_dmamap_sync(dev_priv->agpdmat, obj->dmamap, 0,
obj->base.size, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
}
/** Flushes the GTT write domain for the object if it's dirty. */
void
i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
{
uint32_t old_write_domain;
if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
return;
/* No actual flushing is required for the GTT write domain. Writes
* to it immediately go to main memory as far as we know, so there's
* no chipset flush. It also doesn't land in render cache.
*
* However, we do have to enforce the order so that all writes through
* the GTT land before any writes to the device, such as updates to
* the GATT itself.
*/
DRM_WRITEMEMORYBARRIER();
old_write_domain = obj->base.write_domain;
obj->base.write_domain = 0;
#if 0
trace_i915_gem_object_change_domain(obj,
obj->base.read_domains,
old_write_domain);
#endif
}
/** Flushes the CPU write domain for the object if it's dirty. */
void
i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
{
uint32_t old_write_domain;
if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
return;
i915_gem_clflush_object(obj);
i915_gem_chipset_flush(obj->base.dev);
old_write_domain = obj->base.write_domain;
obj->base.write_domain = 0;
#if 0
trace_i915_gem_object_change_domain(obj,
obj->base.read_domains,
old_write_domain);
#endif
}
/**
* Moves a single object to the GTT read, and possibly write domain.
*
* This function returns when the move is complete, including waiting on
* flushes to occur.
*/
int
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
{
drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
// uint32_t old_write_domain, old_read_domains;
int ret;
DRM_ASSERT_HELD(&obj->base);
/* Not valid to be called on unbound objects. */
if (obj->dmamap == NULL)
return (EINVAL);
if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
return 0;
ret = i915_gem_object_wait_rendering(obj, !write);
if (ret)
return ret;
i915_gem_object_flush_cpu_write_domain(obj);
// old_write_domain = obj->base.write_domain;
// old_read_domains = obj->base.read_domains;
/* It should now be out of any other write domains, and we can update
* the domain values for our changes.
*/
WARN_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
if (write) {
obj->base.read_domains = I915_GEM_DOMAIN_GTT;
obj->base.write_domain = I915_GEM_DOMAIN_GTT;
obj->dirty = 1;
}
// trace_i915_gem_object_change_domain(obj,
// old_read_domains,
// old_write_domain);
/* And bump the LRU for this access */
if (i915_gem_object_is_inactive(obj))
list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
return 0;
}
int
i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
enum i915_cache_level cache_level)
{
// struct drm_device *dev = obj->base.dev;
// drm_i915_private_t *dev_priv = dev->dev_private;
int ret;
if (obj->cache_level == cache_level)
return 0;
if (obj->pin_count) {
DRM_DEBUG("can not change the cache level of pinned objects\n");
return -EBUSY;
}
if (obj->dmamap != NULL) {
ret = i915_gem_object_finish_gpu(obj);
if (ret)
return ret;
i915_gem_object_finish_gtt(obj);
/* Before SandyBridge, you could not use tiling or fence
* registers with snooped memory, so relinquish any fences
* currently pointing to our region in the aperture.
*/
if (INTEL_INFO(obj->base.dev)->gen < 6) {
ret = i915_gem_object_put_fence(obj);
if (ret)
return ret;
}
i915_gem_gtt_rebind_object(obj, cache_level);
#ifdef notyet
if (obj->has_aliasing_ppgtt_mapping)
i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
obj, cache_level);
#endif
}
if (cache_level == I915_CACHE_NONE) {
u32 old_read_domains, old_write_domain;
/* If we're coming from LLC cached, then we haven't
* actually been tracking whether the data is in the
* CPU cache or not, since we only allow one bit set
* in obj->write_domain and have been skipping the clflushes.
* Just set it to the CPU cache for now.
*/
WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
old_read_domains = obj->base.read_domains;
old_write_domain = obj->base.write_domain;
obj->base.read_domains = I915_GEM_DOMAIN_CPU;
obj->base.write_domain = I915_GEM_DOMAIN_CPU;
#if 0
trace_i915_gem_object_change_domain(obj,
old_read_domains,
old_write_domain);
#endif
}
obj->cache_level = cache_level;
return 0;
}
// i915_gem_get_caching_ioctl
// i915_gem_set_caching_ioctl
int
i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
u32 alignment,
struct intel_ring_buffer *pipelined)
{
// u32 old_read_domains, old_write_domain;
int ret;
if (pipelined != obj->ring) {
ret = i915_gem_object_sync(obj, pipelined);
if (ret)
return (ret);
}
/* The display engine is not coherent with the LLC cache on gen6. As
* a result, we make sure that the pinning that is about to occur is
* done with uncached PTEs. This is lowest common denominator for all
* chipsets.
*
* However for gen6+, we could do better by using the GFDT bit instead
* of uncaching, which would allow us to flush all the LLC-cached data
* with that bit in the PTE to main memory with just one PIPE_CONTROL.
*/
ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
if (ret)
return ret;
/* As the user may map the buffer once pinned in the display plane
* (e.g. libkms for the bootup splash), we have to ensure that we
* always use map_and_fenceable for all scanout buffers.
*/
ret = i915_gem_object_pin(obj, alignment, true);
if (ret)
return ret;
i915_gem_object_flush_cpu_write_domain(obj);
// old_write_domain = obj->write_domain;
// old_read_domains = obj->read_domains;
/* It should now be out of any other write domains, and we can update
* the domain values for our changes.
*/
obj->base.write_domain = 0;
obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
// trace_i915_gem_object_change_domain(obj,
// old_read_domains,
// old_write_domain);
return 0;
}
int
i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
{
int ret;
if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
return 0;
ret = i915_gem_object_wait_rendering(obj, false);
if (ret)
return ret;
/* Ensure that we invalidate the GPU's caches and TLBs. */
obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
return 0;
}
/**
* Moves a single object to the CPU read, and possibly write domain.
*
* This function returns when the move is complete, including waiting on
* flushes to return.
*/
int
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
{
// uint32_t old_write_domain, old_read_domains;
int ret;
DRM_ASSERT_HELD(obj);
if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
return 0;
ret = i915_gem_object_wait_rendering(obj, !write);
if (ret)
return ret;
i915_gem_object_flush_gtt_write_domain(obj);
// old_write_domain = obj->base.write_domain;
// old_read_domains = obj->base.read_domains;
/* Flush the CPU cache if it's still invalid. */
if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
i915_gem_clflush_object(obj);
obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
}
/* It should now be out of any other write domains, and we can update
* the domain values for our changes.
*/
BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
/* If we're writing through the CPU, then the GPU read domains will
* need to be invalidated at next use.
*/
if (write) {
obj->base.read_domains = I915_GEM_DOMAIN_CPU;
obj->base.write_domain = I915_GEM_DOMAIN_CPU;
}
// trace_i915_gem_object_change_domain(obj,
// old_read_domains,
// old_write_domain);
return 0;
}
/* Throttle our rendering by waiting until the ring has completed our requests
* emitted over 20 msec ago.
*
* Note that if we were to use the current jiffies each time around the loop,
* we wouldn't escape the function with any frames outstanding if the time to
* render a frame was over 20ms.
*
* This should get us reasonable parallelism between CPU and GPU but also
* relatively low latency when blocking on a particular request to finish.
*/
int
i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_file_private *file_priv = file->driver_priv;
unsigned long recent_enough = ticks - msecs_to_jiffies(20);
struct drm_i915_gem_request *request;
struct intel_ring_buffer *ring = NULL;
u32 seqno = 0;
int ret;
if (atomic_read(&dev_priv->mm.wedged))
return EIO;
mtx_enter(&file_priv->mm.lock);
list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
if (time_after_eq(request->emitted_ticks, recent_enough))
break;
ring = request->ring;
seqno = request->seqno;
}
mtx_leave(&file_priv->mm.lock);
if (seqno == 0)
return 0;
ret = __wait_seqno(ring, seqno, true, NULL);
if (ret == 0)
timeout_add_sec(&dev_priv->mm.retire_timer, 0);
return ret;
}
int
i915_gem_object_pin(struct drm_i915_gem_object *obj,
uint32_t alignment,
bool map_and_fenceable)
{
struct drm_device *dev = obj->base.dev;
int ret;
DRM_ASSERT_HELD(&obj->base);
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
if (obj->dmamap != NULL) {
if ((alignment && obj->gtt_offset & (alignment - 1)) ||
(map_and_fenceable && !obj->map_and_fenceable)) {
WARN(obj->pin_count,
"bo is already pinned with incorrect alignment:"
" offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
" obj->map_and_fenceable=%d\n",
obj->gtt_offset, alignment,
map_and_fenceable,
obj->map_and_fenceable);
ret = i915_gem_object_unbind(obj);
if (ret)
return ret;
}
}
if (obj->dmamap == NULL) {
ret = i915_gem_object_bind_to_gtt(obj, alignment,
map_and_fenceable);
if (ret)
return ret;
}
/*
* due to lazy fence destruction we may have an invalid fence now.
* So if so, nuke it before we do anything with the gpu.
* XXX 965+ can put this off.. and be faster
*/
if (obj->fence_dirty) {
ret= i915_gem_object_put_fence(obj);
if (ret)
return (ret);
}
/*
* Pre-965 chips may need a fence register set up in order to
* handle tiling properly. GTT mapping may have blown it away so
* restore.
* With execbuf2 support we don't always need it, but if we do grab
* it.
*/
if (map_and_fenceable && obj->tiling_mode != I915_TILING_NONE &&
(ret = i915_gem_object_get_fence(obj)) != 0)
return (ret);
if (obj->pin_count++ == 0) {
atomic_inc(&dev->pin_count);
atomic_add(obj->base.size, &dev->pin_memory);
if (!obj->active)
list_del_init(&obj->mm_list);
}
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
return (0);
}
void
i915_gem_object_unpin(struct drm_i915_gem_object *obj)
{
struct drm_device *dev = obj->base.dev;
drm_i915_private_t *dev_priv = dev->dev_private;
DRM_ASSERT_HELD(&obj->base);
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
BUG_ON(obj->pin_count == 0);
BUG_ON(obj->dmamap == NULL);
if (--obj->pin_count == 0) {
if (!obj->active)
list_move_tail(&obj->mm_list,
&dev_priv->mm.inactive_list);
atomic_dec(&dev->pin_count);
atomic_sub(obj->base.size, &dev->pin_memory);
}
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
}
int
i915_gem_pin_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct drm_i915_gem_pin *args = data;
struct drm_i915_gem_object *obj;
int ret;
ret = i915_mutex_lock_interruptible(dev);
if (ret)
return ret;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
if (&obj->base == NULL) {
ret = EBADF;
goto unlock;
}
drm_hold_object(&obj->base);
if (obj->madv != I915_MADV_WILLNEED) {
DRM_ERROR("Attempting to pin a purgeable buffer\n");
ret = EINVAL;
goto out;
}
if (++obj->user_pin_count == 1) {
ret = i915_gem_object_pin(obj, args->alignment, 1);
if (ret != 0)
goto out;
inteldrm_set_max_obj_size(dev_priv);
}
/* XXX - flush the CPU caches for pinned objects
* as the X server doesn't manage domains yet
*/
i915_gem_object_set_to_gtt_domain(obj, true);
args->offset = obj->gtt_offset;
out:
drm_unhold_and_unref(&obj->base);
unlock:
DRM_UNLOCK();
return ret;
}
int
i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct drm_i915_gem_pin *args = data;
struct drm_i915_gem_object *obj;
int ret;
ret = i915_mutex_lock_interruptible(dev);
if (ret)
return ret;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
if (&obj->base == NULL) {
ret = EBADF;
goto unlock;
}
drm_hold_object(&obj->base);
if (obj->user_pin_count == 0) {
ret = EINVAL;
goto out;
}
obj->user_pin_count--;
if (obj->user_pin_count == 0) {
i915_gem_object_unpin(obj);
inteldrm_set_max_obj_size(dev_priv);
}
out:
drm_unhold_and_unref(&obj->base);
unlock:
DRM_UNLOCK();
return ret;
}
int
i915_gem_busy_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct drm_i915_gem_busy *args = data;
struct drm_i915_gem_object *obj;
int ret;
ret = i915_mutex_lock_interruptible(dev);
if (ret)
return ret;
obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
if (&obj->base == NULL) {
ret = EBADF;
goto unlock;
}
ret = i915_gem_object_flush_active(obj);
args->busy = obj->active;
drm_gem_object_unreference(&obj->base);
unlock:
DRM_UNLOCK();
return ret;
}
// i915_gem_throttle_ioctl
int
i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_gem_madvise *args = data;
struct drm_i915_gem_object *obj;
int ret;
switch (args->madv) {
case I915_MADV_DONTNEED:
case I915_MADV_WILLNEED:
break;
default:
return (EINVAL);
}
ret = i915_mutex_lock_interruptible(dev);
if (ret)
return ret;
obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
if (&obj->base == NULL) {
ret = EBADF;
goto unlock;
}
drm_hold_object(&obj->base);
/* invalid to madvise on a pinned BO */
if (obj->pin_count) {
ret = EINVAL;
goto out;
}
if (obj->madv != __I915_MADV_PURGED)
obj->madv = args->madv;
/* if the object is no longer bound, discard its backing storage */
if (i915_gem_object_is_purgeable(obj) && obj->dmamap == NULL)
i915_gem_object_truncate(obj);
args->retained = obj->madv != __I915_MADV_PURGED;
out:
drm_unhold_and_unref(&obj->base);
unlock:
DRM_UNLOCK();
return ret;
}
struct drm_i915_gem_object *
i915_gem_alloc_object(struct drm_device *dev, size_t size)
{
struct drm_obj *obj;
struct drm_i915_gem_object *obj_priv;
obj = drm_gem_object_alloc(dev, size);
if (obj == NULL)
return NULL;
obj_priv = to_intel_bo(obj);
return (obj_priv);
}
int
i915_gem_init_object(struct drm_obj *obj)
{
struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
struct drm_device *dev = obj->dev;
/*
* We've just allocated pages from the kernel,
* so they've just been written by the CPU with
* zeros. They'll need to be flushed before we
* use them with the GPU.
*/
obj->write_domain = I915_GEM_DOMAIN_CPU;
obj->read_domains = I915_GEM_DOMAIN_CPU;
if (HAS_LLC(dev)) {
/* On some devices, we can have the GPU use the LLC (the CPU
* cache) for about a 10% performance improvement
* compared to uncached. Graphics requests other than
* display scanout are coherent with the CPU in
* accessing this cache. This means in this mode we
* don't need to clflush on the CPU side, and on the
* GPU side we only need to flush internal caches to
* get data visible to the CPU.
*
* However, we maintain the display planes as UC, and so
* need to rebind when first used as such.
*/
obj_priv->cache_level = I915_CACHE_LLC;
} else
obj_priv->cache_level = I915_CACHE_NONE;
/* normal objects don't need special treatment */
obj_priv->dma_flags = 0;
obj_priv->fence_reg = I915_FENCE_REG_NONE;
obj_priv->madv = I915_MADV_WILLNEED;
/* Avoid an unnecessary call to unbind on the first bind. */
obj_priv->map_and_fenceable = true;
INIT_LIST_HEAD(&obj_priv->mm_list);
INIT_LIST_HEAD(&obj_priv->gtt_list);
INIT_LIST_HEAD(&obj_priv->ring_list);
return 0;
}
/*
* NOTE all object unreferences in this driver need to hold the DRM_LOCK(),
* because if they free they poke around in driver structures.
*/
void
i915_gem_free_object(struct drm_obj *gem_obj)
{
struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
struct drm_device *dev = gem_obj->dev;
DRM_ASSERT_HELD(&obj->base);
if (obj->phys_obj)
i915_gem_detach_phys_object(dev, obj);
while (obj->pin_count > 0)
i915_gem_object_unpin(obj);
i915_gem_object_unbind(obj);
drm_free(obj->bit_17);
obj->bit_17 = NULL;
/* XXX dmatag went away? */
}
int
i915_gem_idle(struct inteldrm_softc *dev_priv)
{
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
int ret;
/* If drm attach failed */
if (dev == NULL)
return (0);
DRM_LOCK();
if (dev_priv->mm.suspended) {
DRM_UNLOCK();
return 0;
}
ret = i915_gpu_idle(dev);
if (ret) {
DRM_UNLOCK();
return (ret);
}
/* Under UMS, be paranoid and evict. */
if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
ret = i915_gem_evict_inactive(dev_priv);
if (ret) {
DRM_UNLOCK();
return (ret);
}
}
i915_gem_reset_fences(dev);
/* Hack! Don't let anybody do execbuf while we don't control the chip.
* We need to replace this with a semaphore, or something.
*/
dev_priv->mm.suspended = 1;
/* if we hung then the timer alredy fired. */
timeout_del(&dev_priv->hangcheck_timer);
i915_kernel_lost_context(dev);
i915_gem_cleanup_ringbuffer(dev);
DRM_UNLOCK();
/* this should be idle now */
timeout_del(&dev_priv->mm.retire_timer);
return 0;
}
// i915_gem_l3_remap
void
i915_gem_init_swizzling(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
if (INTEL_INFO(dev)->gen < 5 ||
dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
return;
I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
DISP_TILE_SURFACE_SWIZZLING);
if (IS_GEN5(dev))
return;
I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
if (IS_GEN6(dev))
I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
else
I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
}
bool
intel_enable_blt(struct drm_device *dev)
{
if (!HAS_BLT(dev))
return false;
#ifdef notyet
/* The blitter was dysfunctional on early prototypes */
if (IS_GEN6(dev) && dev->pdev->revision < 8) {
DRM_INFO("BLT not supported on this pre-production hardware;"
" graphics performance will be degraded.\n");
return false;
}
#endif
return true;
}
int
i915_gem_init_hw(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
int ret;
#ifdef notyet
if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
return -EIO;
if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
i915_gem_l3_remap(dev);
#endif
i915_gem_init_swizzling(dev);
ret = intel_init_render_ring_buffer(dev);
if (ret)
return ret;
if (HAS_BSD(dev)) {
ret = intel_init_bsd_ring_buffer(dev);
if (ret)
goto cleanup_render_ring;
}
if (intel_enable_blt(dev)) {
ret = intel_init_blt_ring_buffer(dev);
if (ret)
goto cleanup_bsd_ring;
}
dev_priv->next_seqno = 1;
/*
* XXX: There was some w/a described somewhere suggesting loading
* contexts before PPGTT.
*/
#ifdef notyet
i915_gem_context_init(dev);
i915_gem_init_ppgtt(dev);
#endif
return 0;
cleanup_bsd_ring:
intel_cleanup_ring_buffer(&dev_priv->rings[VCS]);
cleanup_render_ring:
intel_cleanup_ring_buffer(&dev_priv->rings[RCS]);
return ret;
}
// intel_enable_ppgtt
int
i915_gem_init(struct drm_device *dev)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
uint64_t gtt_start, gtt_end;
struct agp_softc *asc;
int ret;
DRM_LOCK();
asc = (struct agp_softc *)dev->agp->agpdev;
gtt_start = asc->sc_stolen_entries * 4096;
/*
* putting stuff in the last page of the aperture can cause nasty
* problems with prefetch going into unassigned memory. Since we put
* a scratch page on all unused aperture pages, just leave the last
* page as a spill to prevent gpu hangs.
*/
gtt_end = dev->agp->info.ai_aperture_size - 4096;
if (agp_bus_dma_init(asc,
dev->agp->base + gtt_start, dev->agp->base + gtt_end,
&dev_priv->agpdmat) != 0) {
DRM_UNLOCK();
return (ENOMEM);
}
dev->gtt_total = (uint32_t)(gtt_end - gtt_start);
inteldrm_set_max_obj_size(dev_priv);
ret = i915_gem_init_hw(dev);
if (ret != 0) {
DRM_UNLOCK();
return (ret);
}
DRM_UNLOCK();
return 0;
}
void
i915_gem_cleanup_ringbuffer(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct intel_ring_buffer *ring;
int i;
for_each_ring(ring, dev_priv, i)
intel_cleanup_ring_buffer(ring);
}
int
i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
struct intel_ring_buffer *ring;
int ret, i;
if (drm_core_check_feature(dev, DRIVER_MODESET))
return (0);
/* XXX until we have support for the rings on sandybridge */
if (IS_GEN6(dev) || IS_GEN7(dev))
return (0);
if (dev_priv->mm.wedged) {
DRM_ERROR("Reenabling wedged hardware, good luck\n");
dev_priv->mm.wedged = 0;
}
DRM_LOCK();
dev_priv->mm.suspended = 0;
ret = i915_gem_init_hw(dev);
if (ret != 0) {
DRM_UNLOCK();
return (ret);
}
/* gtt mapping means that the inactive list may not be empty */
KASSERT(list_empty(&dev_priv->mm.active_list));
for_each_ring(ring, dev_priv, i)
KASSERT(list_empty(&ring->request_list));
DRM_UNLOCK();
drm_irq_install(dev);
return (0);
}
int
i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct inteldrm_softc *dev_priv = dev->dev_private;
int ret;
if (drm_core_check_feature(dev, DRIVER_MODESET))
return 0;
/* don't unistall if we fail, repeat calls on failure will screw us */
if ((ret = i915_gem_idle(dev_priv)) == 0)
drm_irq_uninstall(dev);
return (ret);
}
// i915_gem_lastclose
void
init_ring_lists(struct intel_ring_buffer *ring)
{
INIT_LIST_HEAD(&ring->active_list);
INIT_LIST_HEAD(&ring->request_list);
}
// i915_gem_load
int
i915_gem_init_phys_object(struct drm_device *dev,
int id, int size, int align)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_phys_object *phys_obj;
int ret;
if (dev_priv->mm.phys_objs[id - 1] || !size)
return 0;
phys_obj = drm_alloc(sizeof(struct drm_i915_gem_phys_object));
if (!phys_obj)
return -ENOMEM;
phys_obj->id = id;
phys_obj->handle = drm_dmamem_alloc(dev->dmat, size, align, 1, size, BUS_DMA_NOCACHE, 0);
if (!phys_obj->handle) {
ret = -ENOMEM;
goto kfree_obj;
}
dev_priv->mm.phys_objs[id - 1] = phys_obj;
return 0;
kfree_obj:
drm_free(phys_obj);
return ret;
}
// i915_gem_free_phys_object
// i915_gem_free_all_phys_object
void i915_gem_detach_phys_object(struct drm_device *dev,
struct drm_i915_gem_object *obj)
{
char *vaddr;
int i;
int page_count;
if (!obj->phys_obj)
return;
vaddr = obj->phys_obj->handle->kva;
page_count = obj->base.size / PAGE_SIZE;
for (i = 0; i < page_count; i++) {
#ifdef notyet
struct page *page = shmem_read_mapping_page(mapping, i);
if (!IS_ERR(page)) {
char *dst = kmap_atomic(page);
memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
kunmap_atomic(dst);
drm_clflush_pages(&page, 1);
set_page_dirty(page);
mark_page_accessed(page);
page_cache_release(page);
}
#endif
}
i915_gem_chipset_flush(dev);
obj->phys_obj->cur_obj = NULL;
obj->phys_obj = NULL;
}
int
i915_gem_attach_phys_object(struct drm_device *dev,
struct drm_i915_gem_object *obj, int id, int align)
{
drm_i915_private_t *dev_priv = dev->dev_private;
int ret = 0;
int page_count;
int i;
if (id > I915_MAX_PHYS_OBJECT)
return -EINVAL;
if (obj->phys_obj) {
if (obj->phys_obj->id == id)
return 0;
i915_gem_detach_phys_object(dev, obj);
}
/* create a new object */
if (!dev_priv->mm.phys_objs[id - 1]) {
ret = i915_gem_init_phys_object(dev, id,
obj->base.size, align);
if (ret) {
DRM_ERROR("failed to init phys object %d size: %zu\n",
id, obj->base.size);
return (ret);
}
}
/* bind to the object */
obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
obj->phys_obj->cur_obj = obj;
page_count = obj->base.size / PAGE_SIZE;
for (i = 0; i < page_count; i++) {
#ifdef notyet
struct page *page;
char *dst, *src;
page = shmem_read_mapping_page(mapping, i);
if (IS_ERR(page))
return PTR_ERR(page);
src = kmap_atomic(page);
dst = obj->phys_obj->handle->kva + (i * PAGE_SIZE);
memcpy(dst, src, PAGE_SIZE);
kunmap_atomic(src);
mark_page_accessed(page);
page_cache_release(page);
#endif
}
return 0;
}
int
i915_gem_phys_pwrite(struct drm_device *dev,
struct drm_i915_gem_object *obj,
struct drm_i915_gem_pwrite *args,
struct drm_file *file_priv)
{
void *vaddr = obj->phys_obj->handle->kva + args->offset;
int ret;
ret = copyin((char *)(uintptr_t)args->data_ptr,
vaddr, args->size);
i915_gem_chipset_flush(dev);
return ret;
}
void
i915_gem_release(struct drm_device *dev, struct drm_file *file)
{
struct drm_i915_file_private *file_priv = file->driver_priv;
/* Clean up our request list when the client is going away, so that
* later retire_requests won't dereference our soon-to-be-gone
* file_priv.
*/
mtx_enter(&file_priv->mm.lock);
while (!list_empty(&file_priv->mm.request_list)) {
struct drm_i915_gem_request *request;
request = list_first_entry(&file_priv->mm.request_list,
struct drm_i915_gem_request,
client_list);
list_del(&request->client_list);
request->file_priv = NULL;
}
mtx_leave(&file_priv->mm.lock);
}
// i915_gem_release
// mutex_is_locked_by
// i915_gem_inactive_shrink
|