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
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
|
/*
* 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 <machine/pmap.h>
#include <sys/queue.h>
#include <sys/workq.h>
#if 0
# define INTELDRM_WATCH_COHERENCY
# define WATCH_INACTIVE
#endif
#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
int inteldrm_probe(struct device *, void *, void *);
void inteldrm_attach(struct device *, struct device *, void *);
int inteldrm_detach(struct device *, int);
int inteldrm_activate(struct device *, int);
int inteldrm_ioctl(struct drm_device *, u_long, caddr_t, struct drm_file *);
int inteldrm_intr(void *);
void inteldrm_lastclose(struct drm_device *);
void inteldrm_wrap_ring(struct drm_i915_private *);
int inteldrm_gmch_match(struct pci_attach_args *);
void inteldrm_chipset_flush(struct drm_i915_private *);
void inteldrm_timeout(void *);
void inteldrm_hangcheck(void *);
void inteldrm_hung(void *, void *);
void inteldrm_965_reset(struct drm_i915_private *, u_int8_t);
int inteldrm_fault(struct drm_obj *, struct uvm_faultinfo *, off_t,
vaddr_t, vm_page_t *, int, int, vm_prot_t, int );
void inteldrm_wipe_mappings(struct drm_obj *);
void inteldrm_purge_obj(struct drm_obj *);
/* For reset and suspend */
int inteldrm_save_state(struct drm_i915_private *);
int inteldrm_restore_state(struct drm_i915_private *);
int inteldrm_save_display(struct drm_i915_private *);
int inteldrm_restore_display(struct drm_i915_private *);
void i915_save_vga(struct drm_i915_private *);
void i915_restore_vga(struct drm_i915_private *);
void i915_save_modeset_reg(struct drm_i915_private *);
void i915_restore_modeset_reg(struct drm_i915_private *);
u_int8_t i915_read_indexed(struct drm_i915_private *, u_int16_t,
u_int16_t, u_int8_t);
void i915_write_indexed(struct drm_i915_private *, u_int16_t,
u_int16_t, u_int8_t, u_int8_t);
void i915_write_ar(struct drm_i915_private *, u_int16_t, u_int8_t,
u_int8_t, u_int16_t);
u_int8_t i915_read_ar(struct drm_i915_private *, u_int16_t,
u_int8_t, u_int16_t);
void i915_save_palette(struct drm_i915_private *, enum pipe);
void i915_restore_palette(struct drm_i915_private *, enum pipe);
void i915_alloc_ifp(struct drm_i915_private *, struct pci_attach_args *);
void i965_alloc_ifp(struct drm_i915_private *, struct pci_attach_args *);
void inteldrm_detect_bit_6_swizzle(drm_i915_private_t *,
struct pci_attach_args *);
int inteldrm_setup_mchbar(struct drm_i915_private *,
struct pci_attach_args *);
void inteldrm_teardown_mchbar(struct drm_i915_private *,
struct pci_attach_args *, int);
/* Ioctls */
int i915_gem_init_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_create_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_pread_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_pwrite_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_set_domain_ioctl(struct drm_device *, void *,
struct drm_file *);
int i915_gem_execbuffer2(struct drm_device *, void *, struct drm_file *);
int i915_gem_pin_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_unpin_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_busy_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_entervt_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_leavevt_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_get_aperture_ioctl(struct drm_device *, void *,
struct drm_file *);
int i915_gem_set_tiling(struct drm_device *, void *, struct drm_file *);
int i915_gem_get_tiling(struct drm_device *, void *, struct drm_file *);
int i915_gem_gtt_map_ioctl(struct drm_device *, void *, struct drm_file *);
int i915_gem_madvise_ioctl(struct drm_device *, void *, struct drm_file *);
/* GEM memory manager functions */
int i915_gem_init_object(struct drm_obj *);
void i915_gem_free_object(struct drm_obj *);
int i915_gem_object_pin(struct drm_obj *, uint32_t, int);
void i915_gem_object_unpin(struct drm_obj *);
void i915_gem_retire_requests(struct drm_i915_private *);
void i915_gem_retire_request(struct drm_i915_private *,
struct inteldrm_request *);
void i915_gem_retire_work_handler(void *, void*);
int i915_gem_idle(struct drm_i915_private *);
void i915_gem_object_move_to_active(struct drm_obj *);
void i915_gem_object_move_off_active(struct drm_obj *);
void i915_gem_object_move_to_inactive(struct drm_obj *);
uint32_t i915_add_request(struct drm_i915_private *);
void inteldrm_process_flushing(struct drm_i915_private *, u_int32_t);
void i915_move_to_tail(struct inteldrm_obj *, struct i915_gem_list *);
void i915_list_remove(struct inteldrm_obj *);
int i915_gem_init_hws(struct drm_i915_private *);
void i915_gem_cleanup_hws(struct drm_i915_private *);
int i915_gem_init_ringbuffer(struct drm_i915_private *);
int inteldrm_start_ring(struct drm_i915_private *);
void i915_gem_cleanup_ringbuffer(struct drm_i915_private *);
int i915_gem_ring_throttle(struct drm_device *, struct drm_file *);
int i915_gem_evict_inactive(struct drm_i915_private *);
int i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *,
u_int32_t, struct drm_i915_gem_relocation_entry **);
int i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *,
u_int32_t, struct drm_i915_gem_relocation_entry *);
void i915_dispatch_gem_execbuffer(struct drm_device *,
struct drm_i915_gem_execbuffer2 *, uint64_t);
void i915_gem_object_set_to_gpu_domain(struct drm_obj *);
int i915_gem_object_pin_and_relocate(struct drm_obj *,
struct drm_file *, struct drm_i915_gem_exec_object2 *,
struct drm_i915_gem_relocation_entry *);
int i915_gem_object_bind_to_gtt(struct drm_obj *, bus_size_t, int);
int i915_wait_request(struct drm_i915_private *, uint32_t, int);
u_int32_t i915_gem_flush(struct drm_i915_private *, uint32_t, uint32_t);
int i915_gem_object_unbind(struct drm_obj *, int);
struct drm_obj *i915_gem_find_inactive_object(struct drm_i915_private *,
size_t);
int i915_gem_evict_everything(struct drm_i915_private *, int);
int i915_gem_evict_something(struct drm_i915_private *, size_t, int);
int i915_gem_object_set_to_gtt_domain(struct drm_obj *, int, int);
int i915_gem_object_set_to_cpu_domain(struct drm_obj *, int, int);
int i915_gem_object_flush_gpu_write_domain(struct drm_obj *, int, int);
int i915_gem_get_fence_reg(struct drm_obj *, int);
int i915_gem_object_put_fence_reg(struct drm_obj *, int);
bus_size_t i915_gem_get_gtt_alignment(struct drm_obj *);
bus_size_t i915_get_fence_size(struct drm_i915_private *, bus_size_t);
int i915_tiling_ok(struct drm_device *, int, int, int);
int i915_gem_object_fence_offset_ok(struct drm_obj *, int);
void i965_write_fence_reg(struct inteldrm_fence *);
void i915_write_fence_reg(struct inteldrm_fence *);
void i830_write_fence_reg(struct inteldrm_fence *);
void i915_gem_bit_17_swizzle(struct drm_obj *);
void i915_gem_save_bit_17_swizzle(struct drm_obj *);
int inteldrm_swizzle_page(struct vm_page *page);
/* Debug functions, mostly called from ddb */
void i915_gem_seqno_info(int);
void i915_interrupt_info(int);
void i915_gem_fence_regs_info(int);
void i915_hws_info(int);
void i915_batchbuffer_info(int);
void i915_ringbuffer_data(int);
void i915_ringbuffer_info(int);
#ifdef WATCH_INACTIVE
void inteldrm_verify_inactive(struct drm_i915_private *, char *, int);
#else
#define inteldrm_verify_inactive(dev,file,line)
#endif
const static struct drm_pcidev inteldrm_pciidlist[] = {
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82830M_IGD,
CHIP_I830|CHIP_M},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82845G_IGD,
CHIP_I845G},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82855GM_IGD,
CHIP_I85X|CHIP_M},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82865G_IGD,
CHIP_I865G},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82915G_IGD_1,
CHIP_I915G|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_E7221_IGD,
CHIP_I915G|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82915GM_IGD_1,
CHIP_I915GM|CHIP_I9XX|CHIP_M},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82945G_IGD_1,
CHIP_I945G|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82945GM_IGD_1,
CHIP_I945GM|CHIP_I9XX|CHIP_M},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82945GME_IGD_1,
CHIP_I945GM|CHIP_I9XX|CHIP_M},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82946GZ_IGD_1,
CHIP_I965|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82G35_IGD_1,
CHIP_I965|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82Q965_IGD_1,
CHIP_I965|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82G965_IGD_1,
CHIP_I965|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82GM965_IGD_1,
CHIP_I965GM|CHIP_I965|CHIP_I9XX|CHIP_M},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82GME965_IGD_1,
CHIP_I965|CHIP_I9XX},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82G33_IGD_1,
CHIP_G33|CHIP_I9XX|CHIP_HWS},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82Q35_IGD_1,
CHIP_G33|CHIP_I9XX|CHIP_HWS},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82Q33_IGD_1,
CHIP_G33|CHIP_I9XX|CHIP_HWS},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82GM45_IGD_1,
CHIP_G4X|CHIP_GM45|CHIP_I965|CHIP_I9XX|CHIP_M|CHIP_HWS},
{PCI_VENDOR_INTEL, 0x2E02,
CHIP_G4X|CHIP_I965|CHIP_I9XX|CHIP_HWS},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82Q45_IGD_1,
CHIP_G4X|CHIP_I965|CHIP_I9XX|CHIP_HWS},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82G45_IGD_1,
CHIP_G4X|CHIP_I965|CHIP_I9XX|CHIP_HWS},
{PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82G41_IGD_1,
CHIP_G4X|CHIP_I965|CHIP_I9XX|CHIP_HWS},
{0, 0, 0}
};
static const struct drm_driver_info inteldrm_driver = {
.buf_priv_size = 1, /* No dev_priv */
.file_priv_size = sizeof(struct inteldrm_file),
.ioctl = inteldrm_ioctl,
.lastclose = inteldrm_lastclose,
.vblank_pipes = 2,
.get_vblank_counter = i915_get_vblank_counter,
.enable_vblank = i915_enable_vblank,
.disable_vblank = i915_disable_vblank,
.irq_install = i915_driver_irq_install,
.irq_uninstall = i915_driver_irq_uninstall,
#ifdef INTELDRM_GEM
.gem_init_object = i915_gem_init_object,
.gem_free_object = i915_gem_free_object,
.gem_fault = inteldrm_fault,
.gem_size = sizeof(struct inteldrm_obj),
#endif /* INTELDRM_GEM */
.name = DRIVER_NAME,
.desc = DRIVER_DESC,
.date = DRIVER_DATE,
.major = DRIVER_MAJOR,
.minor = DRIVER_MINOR,
.patchlevel = DRIVER_PATCHLEVEL,
.flags = DRIVER_AGP | DRIVER_AGP_REQUIRE |
DRIVER_MTRR | DRIVER_IRQ
#ifdef INTELDRM_GEM
| DRIVER_GEM,
#endif /* INTELDRM_GEM */
};
int
inteldrm_probe(struct device *parent, void *match, void *aux)
{
return (drm_pciprobe((struct pci_attach_args *)aux,
inteldrm_pciidlist));
}
/*
* We're intel IGD, bus 0 function 0 dev 0 should be the GMCH, so it should
* be Intel
*/
int
inteldrm_gmch_match(struct pci_attach_args *pa)
{
if (pa->pa_bus == 0 && pa->pa_device == 0 && pa->pa_function == 0 &&
PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL &&
PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_HOST)
return (1);
return (0);
}
void
inteldrm_attach(struct device *parent, struct device *self, void *aux)
{
struct drm_i915_private *dev_priv = (struct drm_i915_private *)self;
struct pci_attach_args *pa = aux, bpa;
struct vga_pci_bar *bar;
struct drm_device *dev;
const struct drm_pcidev *id_entry;
int i;
id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
PCI_PRODUCT(pa->pa_id), inteldrm_pciidlist);
dev_priv->flags = id_entry->driver_private;
dev_priv->pci_device = PCI_PRODUCT(pa->pa_id);
dev_priv->pc = pa->pa_pc;
dev_priv->tag = pa->pa_tag;
dev_priv->dmat = pa->pa_dmat;
dev_priv->bst = pa->pa_memt;
/* we need to use this api for now due to sharing with intagp */
bar = vga_pci_bar_info((struct vga_pci_softc *)parent,
(IS_I9XX(dev_priv) ? 0 : 1));
if (bar == NULL) {
printf(": can't get BAR info\n");
return;
}
dev_priv->regs = vga_pci_bar_map((struct vga_pci_softc *)parent,
bar->addr, 0, 0);
if (dev_priv->regs == NULL) {
printf(": can't map mmio space\n");
return;
}
if (pci_intr_map(pa, &dev_priv->ih) != 0) {
printf(": couldn't map interrupt\n");
return;
}
/*
* set up interrupt handler, note that we don't switch the interrupt
* on until the X server talks to us, kms will change this.
*/
dev_priv->irqh = pci_intr_establish(dev_priv->pc, dev_priv->ih, IPL_TTY,
inteldrm_intr, dev_priv, dev_priv->dev.dv_xname);
if (dev_priv->irqh == NULL) {
printf(": couldn't establish interrupt\n");
return;
}
/* Unmask the interrupts that we always want on. */
dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
#ifdef INTELDRM_GEM
dev_priv->workq = workq_create("intelrel", 1, IPL_TTY);
if (dev_priv->workq == NULL) {
printf("couldn't create workq\n");
return;
}
/* GEM init */
TAILQ_INIT(&dev_priv->mm.active_list);
TAILQ_INIT(&dev_priv->mm.flushing_list);
TAILQ_INIT(&dev_priv->mm.inactive_list);
TAILQ_INIT(&dev_priv->mm.gpu_write_list);
TAILQ_INIT(&dev_priv->mm.request_list);
TAILQ_INIT(&dev_priv->mm.fence_list);
timeout_set(&dev_priv->mm.retire_timer, inteldrm_timeout, dev_priv);
timeout_set(&dev_priv->mm.hang_timer, inteldrm_hangcheck, dev_priv);
dev_priv->mm.next_gem_seqno = 1;
dev_priv->mm.suspended = 1;
#endif /* INTELDRM_GEM */
/* For the X server, in kms mode this will not be needed */
dev_priv->fence_reg_start = 3;
if (IS_I965G(dev_priv) || IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
IS_G33(dev_priv))
dev_priv->num_fence_regs = 16;
else
dev_priv->num_fence_regs = 8;
/* Initialise fences to zero, else on some macs we'll get corruption */
if (IS_I965G(dev_priv)) {
for (i = 0; i < 16; i++)
I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
} else {
for (i = 0; i < 8; i++)
I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
IS_G33(dev_priv))
for (i = 0; i < 8; i++)
I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
}
if (pci_find_device(&bpa, inteldrm_gmch_match) == 0) {
printf(": can't find GMCH\n");
return;
}
/* Set up the IFP for chipset flushing */
if (dev_priv->flags & (CHIP_I915G|CHIP_I915GM|CHIP_I945G|CHIP_I945GM)) {
i915_alloc_ifp(dev_priv, &bpa);
} else if (IS_I965G(dev_priv) || IS_G33(dev_priv)) {
i965_alloc_ifp(dev_priv, &bpa);
} else {
int nsegs;
/*
* I8XX has no flush page mechanism, we fake it by writing until
* the cache is empty. allocate a page to scribble on
*/
dev_priv->ifp.i8xx.kva = NULL;
if (bus_dmamem_alloc(pa->pa_dmat, PAGE_SIZE, 0, 0,
&dev_priv->ifp.i8xx.seg, 1, &nsegs, BUS_DMA_WAITOK) == 0) {
if (bus_dmamem_map(pa->pa_dmat, &dev_priv->ifp.i8xx.seg,
1, PAGE_SIZE, &dev_priv->ifp.i8xx.kva, 0) != 0) {
bus_dmamem_free(pa->pa_dmat,
&dev_priv->ifp.i8xx.seg, nsegs);
dev_priv->ifp.i8xx.kva = NULL;
}
}
}
#ifdef INTELDRM_GEM
inteldrm_detect_bit_6_swizzle(dev_priv, &bpa);
#endif /* INTELDRM_GEM */
/* Init HWS */
if (!I915_NEED_GFX_HWS(dev_priv)) {
if (i915_init_phys_hws(dev_priv, pa->pa_dmat) != 0) {
printf(": couldn't alloc HWS page\n");
return;
}
}
printf(": %s\n", pci_intr_string(pa->pa_pc, dev_priv->ih));
mtx_init(&dev_priv->user_irq_lock, IPL_TTY);
/* All intel chipsets need to be treated as agp, so just pass one */
dev_priv->drmdev = drm_attach_pci(&inteldrm_driver, pa, 1, self);
dev = (struct drm_device *)dev_priv->drmdev;
#ifdef INTELDRM_GEM
/* XXX would be a lot nicer to get agp info before now */
uvm_page_physload_flags(atop(dev->agp->base), atop(dev->agp->base +
dev->agp->info.ai_aperture_size), atop(dev->agp->base),
atop(dev->agp->base + dev->agp->info.ai_aperture_size), 0,
PHYSLOAD_DEVICE);
/* array of vm pages that physload introduced. */
dev_priv->pgs = PHYS_TO_VM_PAGE(dev->agp->base);
KASSERT(dev_priv->pgs != NULL);
/* XXX wc and do earlier */
if (bus_space_map(dev_priv->bst, dev->agp->base,
dev->agp->info.ai_aperture_size, BUS_SPACE_MAP_LINEAR,
&dev_priv->aperture_bsh) != 0)
panic("can't map aperture");
#endif /* INTELDRM_GEM */
}
int
inteldrm_detach(struct device *self, int flags)
{
struct drm_i915_private *dev_priv = (struct drm_i915_private *)self;
/* this will quiesce any dma that's going on and kill the timeouts. */
if (dev_priv->drmdev != NULL) {
config_detach(dev_priv->drmdev, flags);
dev_priv->drmdev = NULL;
}
i915_free_hws(dev_priv, dev_priv->dmat);
if (IS_I9XX(dev_priv) && dev_priv->ifp.i9xx.bsh != NULL) {
bus_space_unmap(dev_priv->ifp.i9xx.bst, dev_priv->ifp.i9xx.bsh,
PAGE_SIZE);
} else if (dev_priv->flags & (CHIP_I830 | CHIP_I845G | CHIP_I85X |
CHIP_I865G) && dev_priv->ifp.i8xx.kva != NULL) {
bus_dmamem_unmap(dev_priv->dmat, dev_priv->ifp.i8xx.kva,
PAGE_SIZE);
bus_dmamem_free(dev_priv->dmat, &dev_priv->ifp.i8xx.seg, 1);
}
pci_intr_disestablish(dev_priv->pc, dev_priv->irqh);
if (dev_priv->regs != NULL)
vga_pci_bar_unmap(dev_priv->regs);
return (0);
}
int
inteldrm_activate(struct device *arg, int act)
{
struct drm_i915_private *dev_priv = (struct drm_i915_private *)arg;
switch (act) {
case DVACT_SUSPEND:
inteldrm_save_state(dev_priv);
break;
case DVACT_RESUME:
inteldrm_restore_state(dev_priv);
break;
}
return (0);
}
struct cfattach inteldrm_ca = {
sizeof(struct drm_i915_private), inteldrm_probe, inteldrm_attach,
inteldrm_detach, inteldrm_activate
};
struct cfdriver inteldrm_cd = {
0, "inteldrm", DV_DULL
};
int
inteldrm_ioctl(struct drm_device *dev, u_long cmd, caddr_t data,
struct drm_file *file_priv)
{
if (file_priv->authenticated == 1) {
switch (cmd) {
case DRM_IOCTL_I915_FLUSH:
return (i915_flush_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_BATCHBUFFER:
return (i915_batchbuffer(dev, data, file_priv));
case DRM_IOCTL_I915_IRQ_EMIT:
return (i915_irq_emit(dev, data, file_priv));
case DRM_IOCTL_I915_IRQ_WAIT:
return (i915_irq_wait(dev, data, file_priv));
case DRM_IOCTL_I915_GETPARAM:
return (i915_getparam(dev, data, file_priv));
case DRM_IOCTL_I915_CMDBUFFER:
return (i915_cmdbuffer(dev, data, file_priv));
case DRM_IOCTL_I915_GET_VBLANK_PIPE:
return (i915_vblank_pipe_get(dev, data, file_priv));
#ifdef INTELDRM_GEM
case DRM_IOCTL_I915_GEM_EXECBUFFER2:
return (i915_gem_execbuffer2(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_BUSY:
return (i915_gem_busy_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_THROTTLE:
return (i915_gem_ring_throttle(dev, file_priv));
case DRM_IOCTL_I915_GEM_MMAP:
return (i915_gem_gtt_map_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_CREATE:
return (i915_gem_create_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_PREAD:
return (i915_gem_pread_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_PWRITE:
return (i915_gem_pwrite_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_SET_DOMAIN:
return (i915_gem_set_domain_ioctl(dev, data,
file_priv));
case DRM_IOCTL_I915_GEM_SET_TILING:
return (i915_gem_set_tiling(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_GET_TILING:
return (i915_gem_get_tiling(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_GET_APERTURE:
return (i915_gem_get_aperture_ioctl(dev, data,
file_priv));
case DRM_IOCTL_I915_GEM_MADVISE:
return (i915_gem_madvise_ioctl(dev, data, file_priv));
#endif /* INTELDRM_GEM */
default:
break;
}
}
if (file_priv->master == 1) {
switch (cmd) {
case DRM_IOCTL_I915_SETPARAM:
return (i915_setparam(dev, data, file_priv));
case DRM_IOCTL_I915_INIT:
return (i915_dma_init(dev, data, file_priv));
case DRM_IOCTL_I915_HWS_ADDR:
return (i915_set_status_page(dev, data, file_priv));
/* Removed, but still used by userland, so just say `fine' */
case DRM_IOCTL_I915_INIT_HEAP:
case DRM_IOCTL_I915_DESTROY_HEAP:
case DRM_IOCTL_I915_SET_VBLANK_PIPE:
return (0);
#ifdef INTELDRM_GEM
case DRM_IOCTL_I915_GEM_INIT:
return (i915_gem_init_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_ENTERVT:
return (i915_gem_entervt_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_LEAVEVT:
return (i915_gem_leavevt_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_PIN:
return (i915_gem_pin_ioctl(dev, data, file_priv));
case DRM_IOCTL_I915_GEM_UNPIN:
return (i915_gem_unpin_ioctl(dev, data, file_priv));
#endif /* INTELDRM_GEM */
}
}
return (EINVAL);
}
int
inteldrm_intr(void *arg)
{
drm_i915_private_t *dev_priv = arg;
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
u_int32_t iir, pipea_stats = 0, pipeb_stats = 0;
/* we're not set up, don't poke the hw */
if (dev_priv->hw_status_page == NULL)
return (0);
/*
* lock is to protect from writes to PIPESTAT and IMR from other cores.
*/
mtx_enter(&dev_priv->user_irq_lock);
iir = I915_READ(IIR);
if (iir == 0) {
mtx_leave(&dev_priv->user_irq_lock);
return (0);
}
/*
* Clear the PIPE(A|B)STAT regs before the IIR
*/
if (iir & I915_DISPLAY_PIPE_A_EVENT_INTERRUPT) {
pipea_stats = I915_READ(PIPEASTAT);
I915_WRITE(PIPEASTAT, pipea_stats);
}
if (iir & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) {
pipeb_stats = I915_READ(PIPEBSTAT);
I915_WRITE(PIPEBSTAT, pipeb_stats);
}
#ifdef INTELDRM_GEM
if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
inteldrm_error(dev_priv);
#endif /* INTELDRM_GEM */
I915_WRITE(IIR, iir);
(void)I915_READ(IIR); /* Flush posted writes */
if (dev_priv->sarea_priv != NULL)
dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
if (iir & I915_USER_INTERRUPT) {
wakeup(dev_priv);
#ifdef INTELDRM_GEM
dev_priv->mm.hang_cnt = 0;
timeout_add_msec(&dev_priv->mm.hang_timer, 750);
#endif /* INTELDRM_GEM */
}
mtx_leave(&dev_priv->user_irq_lock);
if (pipea_stats & I915_VBLANK_INTERRUPT_STATUS)
drm_handle_vblank(dev, 0);
if (pipeb_stats & I915_VBLANK_INTERRUPT_STATUS)
drm_handle_vblank(dev, 1);
return (1);
}
u_int32_t
inteldrm_read_hws(struct drm_i915_private *dev_priv, int reg)
{
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
u_int32_t val;
/*
* When we eventually go GEM only we'll always have a dmamap, so this
* madness won't be for long.
*/
if (dev_priv->hws_dmamem)
bus_dmamap_sync(dev->dmat, dev_priv->hws_dmamem->map, 0,
PAGE_SIZE, BUS_DMASYNC_POSTREAD);
val = ((volatile u_int32_t *)(dev_priv->hw_status_page))[reg];
if (dev_priv->hws_dmamem)
bus_dmamap_sync(dev->dmat, dev_priv->hws_dmamem->map, 0,
PAGE_SIZE, BUS_DMASYNC_PREREAD);
return (val);
}
/*
* These five ring manipulation functions are protected by dev->dev_lock.
*/
int
inteldrm_wait_ring(struct drm_i915_private *dev_priv, int n)
{
struct inteldrm_ring *ring = &dev_priv->ring;
u_int32_t acthd_reg, acthd, last_acthd, last_head;
int i;
acthd_reg = IS_I965G(dev_priv) ? ACTHD_I965 : ACTHD;
last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
last_acthd = I915_READ(acthd_reg);
/* ugh. Could really do with a proper, resettable timer here. */
for (i = 0; i < 100000; i++) {
ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
acthd = I915_READ(acthd_reg);
ring->space = ring->head - (ring->tail + 8);
INTELDRM_VPRINTF("%s: head: %x tail: %x space: %x\n", __func__,
ring->head, ring->tail, ring->space);
if (ring->space < 0)
ring->space += ring->size;
if (ring->space >= n)
return (0);
/* Only timeout if the ring isn't chewing away on something */
if (ring->head != last_head || acthd != last_acthd)
i = 0;
last_head = ring->head;
last_acthd = acthd;
delay(10);
}
return (EBUSY);
}
void
inteldrm_wrap_ring(struct drm_i915_private *dev_priv)
{
u_int32_t rem;;
rem = dev_priv->ring.size - dev_priv->ring.tail;
if (dev_priv->ring.space < rem &&
inteldrm_wait_ring(dev_priv, rem) != 0)
return; /* XXX */
dev_priv->ring.space -= rem;
bus_space_set_region_4(dev_priv->bst, dev_priv->ring.bsh,
dev_priv->ring.woffset, MI_NOOP, rem / 4);
dev_priv->ring.tail = 0;
}
void
inteldrm_begin_ring(struct drm_i915_private *dev_priv, int ncmd)
{
int bytes = 4 * ncmd;
INTELDRM_VPRINTF("%s: %d\n", __func__, ncmd);
if (dev_priv->ring.tail + bytes > dev_priv->ring.size)
inteldrm_wrap_ring(dev_priv);
if (dev_priv->ring.space < bytes)
inteldrm_wait_ring(dev_priv, bytes);
dev_priv->ring.woffset = dev_priv->ring.tail;
dev_priv->ring.tail += bytes;
dev_priv->ring.tail &= dev_priv->ring.size - 1;
dev_priv->ring.space -= bytes;
}
void
inteldrm_out_ring(struct drm_i915_private *dev_priv, u_int32_t cmd)
{
INTELDRM_VPRINTF("%s: %x\n", __func__, cmd);
bus_space_write_4(dev_priv->bst, dev_priv->ring.bsh,
dev_priv->ring.woffset, cmd);
/*
* don't need to deal with wrap here because we padded
* the ring out if we would wrap
*/
dev_priv->ring.woffset += 4;
}
void
inteldrm_advance_ring(struct drm_i915_private *dev_priv)
{
INTELDRM_VPRINTF("%s: %x, %x\n", __func__, dev_priv->ring.wspace,
dev_priv->ring.woffset);
I915_WRITE(PRB0_TAIL, dev_priv->ring.tail);
}
void
inteldrm_update_ring(struct drm_i915_private *dev_priv)
{
struct inteldrm_ring *ring = &dev_priv->ring;
ring->head = (I915_READ(PRB0_HEAD) & HEAD_ADDR);
ring->tail = (I915_READ(PRB0_TAIL) & TAIL_ADDR);
ring->space = ring->head - (ring->tail + 8);
if (ring->space < 0)
ring->space += ring->size;
INTELDRM_VPRINTF("%s: head: %x tail: %x space: %x\n", __func__,
ring->head, ring->tail, ring->space);
}
void
i915_alloc_ifp(struct drm_i915_private *dev_priv, struct pci_attach_args *bpa)
{
bus_addr_t addr;
u_int32_t reg;
dev_priv->ifp.i9xx.bst = bpa->pa_memt;
reg = pci_conf_read(bpa->pa_pc, bpa->pa_tag, I915_IFPADDR);
if (reg & 0x1) {
addr = (bus_addr_t)reg;
addr &= ~0x1;
/* XXX extents ... need data on whether bioses alloc or not. */
if (bus_space_map(bpa->pa_memt, addr, PAGE_SIZE, 0,
&dev_priv->ifp.i9xx.bsh) != 0)
goto nope;
return;
} else if (bpa->pa_memex == NULL || extent_alloc(bpa->pa_memex,
PAGE_SIZE, PAGE_SIZE, 0, 0, 0, &addr) || bus_space_map(bpa->pa_memt,
addr, PAGE_SIZE, 0, &dev_priv->ifp.i9xx.bsh))
goto nope;
pci_conf_write(bpa->pa_pc, bpa->pa_tag, I915_IFPADDR, addr | 0x1);
return;
nope:
dev_priv->ifp.i9xx.bsh = NULL;
printf(": no ifp ");
}
void
i965_alloc_ifp(struct drm_i915_private *dev_priv, struct pci_attach_args *bpa)
{
bus_addr_t addr;
u_int32_t lo, hi;
dev_priv->ifp.i9xx.bst = bpa->pa_memt;
hi = pci_conf_read(bpa->pa_pc, bpa->pa_tag, I965_IFPADDR + 4);
lo = pci_conf_read(bpa->pa_pc, bpa->pa_tag, I965_IFPADDR);
if (lo & 0x1) {
addr = (((u_int64_t)hi << 32) | lo);
addr &= ~0x1;
/* XXX extents ... need data on whether bioses alloc or not. */
if (bus_space_map(bpa->pa_memt, addr, PAGE_SIZE, 0,
&dev_priv->ifp.i9xx.bsh) != 0)
goto nope;
return;
} else if (bpa->pa_memex == NULL || extent_alloc(bpa->pa_memex,
PAGE_SIZE, PAGE_SIZE, 0, 0, 0, &addr) || bus_space_map(bpa->pa_memt,
addr, PAGE_SIZE, 0, &dev_priv->ifp.i9xx.bsh))
goto nope;
pci_conf_write(bpa->pa_pc, bpa->pa_tag, I965_IFPADDR + 4,
upper_32_bits(addr));
pci_conf_write(bpa->pa_pc, bpa->pa_tag, I965_IFPADDR,
(addr & 0xffffffff) | 0x1);
return;
nope:
dev_priv->ifp.i9xx.bsh = NULL;
printf(": no ifp ");
}
void
inteldrm_chipset_flush(struct drm_i915_private *dev_priv)
{
/*
* Write to this flush page flushes the chipset write cache.
* The write will return when it is done.
*/
if (IS_I9XX(dev_priv)) {
if (dev_priv->ifp.i9xx.bsh != NULL)
bus_space_write_4(dev_priv->ifp.i9xx.bst,
dev_priv->ifp.i9xx.bsh, 0, 1);
} else {
/*
* I8XX don't have a flush page mechanism, but do have the
* cache. Do it the bruteforce way. we write 1024 byes into
* the cache, then clflush them out so they'll kick the stuff
* we care about out of the chipset cache.
*/
if (dev_priv->ifp.i8xx.kva != NULL) {
memset(dev_priv->ifp.i8xx.kva, 0, 1024);
agp_flush_cache_range((vaddr_t)dev_priv->ifp.i8xx.kva,
1024);
}
}
}
void
inteldrm_lastclose(struct drm_device *dev)
{
drm_i915_private_t *dev_priv = dev->dev_private;
#ifdef INTELDRM_GEM
struct vm_page *p;
int ret;
ret = i915_gem_idle(dev_priv);
if (ret)
DRM_ERROR("failed to idle hardware: %d\n", ret);
if (dev_priv->agpdmat != NULL) {
/*
* make sure we nuke everything, we may have mappings that we've
* unrefed, but uvm has a reference to them for maps. Make sure
* they get unbound and any accesses will segfault.
* XXX only do ones in GEM.
*/
for (p = dev_priv->pgs; p < dev_priv->pgs +
(dev->agp->info.ai_aperture_size / PAGE_SIZE); p++)
pmap_page_protect(p, VM_PROT_NONE);
agp_bus_dma_destroy((struct agp_softc *)dev->agp->agpdev,
dev_priv->agpdmat);
}
#endif /* INTELDRM_GEM */
dev_priv->agpdmat = NULL;
dev_priv->sarea_priv = NULL;
i915_dma_cleanup(dev);
}
#ifdef INTELDRM_GEM
int
i915_gem_init_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_init *args = data;
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);
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;
args->aper_size = dev->gtt_total;
args->aper_available_size = (args->aper_size -
atomic_read(&dev->pin_memory));
return (0);
}
/**
* 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 drm_i915_gem_create *args = data;
struct drm_obj *obj;
int handle, ret;
args->size = round_page(args->size);
/* Allocate the new object */
obj = drm_gem_object_alloc(dev, args->size);
if (obj == NULL)
return (ENOMEM);
ret = drm_handle_create(file_priv, obj, &handle);
/* handle has a reference now, drop ours. */
DRM_LOCK();
drm_gem_object_unreference(obj);
DRM_UNLOCK();
if (ret == 0)
args->handle = handle;
return (ret);
}
/**
* 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_priv)
{
struct drm_i915_private *dev_priv = dev->dev_private;
struct drm_i915_gem_pread *args = data;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
char *vaddr;
bus_space_handle_t bsh;
bus_size_t bsize;
voff_t offset;
int ret;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
obj_priv = (struct inteldrm_obj *)obj;
/*
* Bounds check source.
*/
DRM_LOCK();
if (args->offset > obj->size || args->size > obj->size ||
args->offset + args->size > obj->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, 0, 1);
if (ret)
goto unpin;
obj_priv = (struct inteldrm_obj *)obj;
offset = obj_priv->gtt_offset + args->offset;
bsize = round_page(offset + args->size) - trunc_page(offset);
if ((ret = bus_space_subregion(dev_priv->bst, dev_priv->aperture_bsh,
trunc_page(offset), bsize, &bsh)) != 0)
goto unpin;
vaddr = bus_space_vaddr(dev->bst, bsh);
if (vaddr == NULL) {
ret = EFAULT;
goto unpin;
}
ret = copyout(vaddr + (offset & PAGE_MASK),
(char *)(uintptr_t)args->data_ptr, args->size);
if (ret)
goto unpin;
unpin:
i915_gem_object_unpin(obj);
out:
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return (ret);
}
/**
* 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_priv)
{
struct drm_i915_private *dev_priv = dev->dev_private;
struct drm_i915_gem_pwrite *args = data;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
char *vaddr;
bus_space_handle_t bsh;
bus_size_t bsize;
off_t offset;
int ret = 0;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
obj_priv = (struct inteldrm_obj *)obj;
DRM_LOCK();
/* Bounds check destination. */
if (args->offset > obj->size || args->size > obj->size ||
args->offset + args->size > obj->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, 1, 1);
if (ret)
goto done;
obj_priv = (struct inteldrm_obj *)obj;
offset = obj_priv->gtt_offset + args->offset;
bsize = round_page(offset + args->size) - trunc_page(offset);
if ((ret = bus_space_subregion(dev_priv->bst, dev_priv->aperture_bsh,
trunc_page(offset), bsize, &bsh)) != 0)
goto done;
vaddr = bus_space_vaddr(dev_priv->bst, bsh);
if (vaddr == NULL) {
ret = EFAULT;
goto done;
}
ret = copyin((char *)(uintptr_t)args->data_ptr,
vaddr + (offset & PAGE_MASK), args->size);
done:
i915_gem_object_unpin(obj);
out:
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return (ret);
}
/**
* 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_priv)
{
struct drm_i915_gem_set_domain *args = data;
struct drm_obj *obj;
u_int32_t read_domains = args->read_domains;
u_int32_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);
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
DRM_LOCK();
ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0, 1);
drm_gem_object_unreference(obj);
DRM_UNLOCK();
/*
* 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.
*/
return ((ret == EINVAL) ? 0 : ret);
}
int
i915_gem_gtt_map_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_gem_mmap *args = data;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
vaddr_t addr;
voff_t offset;
vsize_t end, nsize;
int ret;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
DRM_LOCK();
obj_priv = (struct inteldrm_obj *)obj;
/* Check size. Also ensure that the object is not purgeable */
if (args->size == 0 || args->offset > obj->size || args->size >
obj->size || (args->offset + args->size) > obj->size ||
i915_obj_purgeable(obj_priv)) {
ret = EINVAL;
goto done;
}
/* bind to the gtt to speed faulting */
if (obj_priv->dmamap == NULL) {
ret = i915_gem_object_bind_to_gtt(obj, 0, 1);
if (ret)
goto done;
i915_gem_object_move_to_inactive(obj);
}
end = round_page(args->offset + args->size);
offset = trunc_page(args->offset);
nsize = end - offset;
/*
* We give our reference from object_lookup to the mmap, so only
* must free it in the case that the map fails.
*/
addr = uvm_map_hint(curproc, VM_PROT_READ | VM_PROT_WRITE);
ret = uvm_map_p(&curproc->p_vmspace->vm_map, &addr, nsize, &obj->uobj,
offset, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
UVM_INH_SHARE, UVM_ADV_RANDOM, 0), curproc);
done:
if (ret != 0)
drm_gem_object_unreference(obj);
DRM_UNLOCK();
if (ret == 0)
args->addr_ptr = (uint64_t) addr + (args->offset & PAGE_MASK);
return (ret);
}
/* called locked */
void
i915_gem_object_move_to_active(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
struct inteldrm_fence *reg;
u_int32_t seqno = dev_priv->mm.next_gem_seqno;
/* Add a reference if we're newly entering the active list. */
if (!inteldrm_is_active(obj_priv)) {
drm_gem_object_reference(obj);
atomic_setbits_int(&obj_priv->io_flags, I915_ACTIVE);
}
if (inteldrm_needs_fence(obj_priv)) {
reg = &dev_priv->fence_regs[obj_priv->fence_reg];
reg->last_rendering_seqno = seqno;
}
/* Move from whatever list we were on to the tail of execution. */
i915_move_to_tail(obj_priv, &dev_priv->mm.active_list);
obj_priv->last_rendering_seqno = seqno;
}
void
i915_gem_object_move_off_active(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
struct inteldrm_fence *reg;
obj_priv->last_rendering_seqno = 0;
if (inteldrm_needs_fence(obj_priv)) {
reg = &dev_priv->fence_regs[obj_priv->fence_reg];
reg->last_rendering_seqno = 0;
}
}
/* called locked */
void
i915_gem_object_move_to_inactive(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
if (obj_priv->pin_count != 0)
i915_list_remove(obj_priv);
else
i915_move_to_tail(obj_priv, &dev_priv->mm.inactive_list);
i915_gem_object_move_off_active(obj);
atomic_clearbits_int(&obj_priv->io_flags, I915_FENCED_EXEC);
KASSERT((obj_priv->io_flags & I915_GPU_WRITE) == 0);
if (inteldrm_is_active(obj_priv)) {
atomic_clearbits_int(&obj_priv->io_flags,
I915_ACTIVE);
drm_gem_object_unreference(obj);
}
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
}
void
inteldrm_purge_obj(struct drm_obj *obj)
{
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
/*
* may sleep. We free here instead of deactivate (which
* the madvise() syscall would do) because in this case
* (userland bo cache and GL_APPLE_object_purgeable objects in
* OpenGL) the pages are defined to be freed if they were cleared
* so kill them and free up the memory
*/
simple_lock(&obj->uao->vmobjlock);
obj->uao->pgops->pgo_flush(obj->uao, 0, obj->size,
PGO_ALLPAGES | PGO_FREE);
simple_unlock(&obj->uao->vmobjlock);
/*
* If flush failed, it may have halfway through, so just
* always mark as purged
*/
atomic_setbits_int(&obj_priv->io_flags, I915_PURGED);
}
void
inteldrm_process_flushing(struct drm_i915_private *dev_priv,
u_int32_t flush_domains)
{
struct inteldrm_obj *obj_priv, *next;
for (obj_priv = TAILQ_FIRST(&dev_priv->mm.gpu_write_list);
obj_priv != TAILQ_END(&dev_priv->mm.gpu_write_list);
obj_priv = next) {
struct drm_obj *obj = &(obj_priv->obj);
next = TAILQ_NEXT(obj_priv, write_list);
if ((obj->write_domain & flush_domains) == obj->write_domain) {
obj->write_domain = 0;
TAILQ_REMOVE(&dev_priv->mm.gpu_write_list,
obj_priv, write_list);
atomic_clearbits_int(&obj_priv->io_flags,
I915_GPU_WRITE);
i915_gem_object_move_to_active(obj);
/* if we still need the fence, update LRU */
if (inteldrm_needs_fence(obj_priv)) {
KASSERT(obj_priv->fence_reg !=
I915_FENCE_REG_NONE);
/* we have a fence, won't sleep, can't fail */
i915_gem_get_fence_reg(obj, 1);
}
}
}
}
/**
* 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.
*
* Must be called with struct_lock held.
*
* Returned sequence numbers are nonzero on success.
*/
uint32_t
i915_add_request(struct drm_i915_private *dev_priv)
{
struct inteldrm_request *request;
uint32_t seqno;
int was_empty;
request = drm_calloc(1, sizeof(*request));
if (request == NULL) {
printf("%s: failed to allocate request\n", __func__);
return 0;
}
/* Grab the seqno we're going to make this request be, and bump the
* next (skipping 0 so it can be the reserved no-seqno value).
*/
seqno = dev_priv->mm.next_gem_seqno;
dev_priv->mm.next_gem_seqno++;
if (dev_priv->mm.next_gem_seqno == 0)
dev_priv->mm.next_gem_seqno++;
BEGIN_LP_RING(4);
OUT_RING(MI_STORE_DWORD_INDEX);
OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
OUT_RING(seqno);
OUT_RING(MI_USER_INTERRUPT);
ADVANCE_LP_RING();
DRM_DEBUG("%d\n", seqno);
/* XXX request timing for throttle */
request->seqno = seqno;
was_empty = TAILQ_EMPTY(&dev_priv->mm.request_list);
TAILQ_INSERT_TAIL(&dev_priv->mm.request_list, request, list);
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->mm.hang_timer, 750);
}
return seqno;
}
/**
* Moves buffers associated only with the given active seqno from the active
* to inactive list, potentially freeing them.
*
* called with and sleeps with the drm_lock.
*/
void
i915_gem_retire_request(struct drm_i915_private *dev_priv,
struct inteldrm_request *request)
{
struct inteldrm_obj *obj_priv;
/* Move any buffers on the active list that are no longer referenced
* by the ringbuffer to the flushing/inactive lists as appropriate.
*/
while ((obj_priv = TAILQ_FIRST(&dev_priv->mm.active_list)) != NULL) {
struct drm_obj *obj = &obj_priv->obj;
/* If the seqno being retired doesn't match the oldest in the
* list, then the oldest in the list must still be newer than
* this seqno.
*/
if (obj_priv->last_rendering_seqno != request->seqno)
return;
/*
* If we're now clean and can be read from, move inactive,
* else put on the flushing list to signify that we're not
* available quite yet.
*/
if (obj->write_domain != 0) {
KASSERT(inteldrm_is_active(obj_priv));
i915_move_to_tail(obj_priv,
&dev_priv->mm.flushing_list);
i915_gem_object_move_off_active(obj);
} else {
i915_gem_object_move_to_inactive(obj);
}
}
}
/**
* This function clears the request list as sequence numbers are passed.
*/
void
i915_gem_retire_requests(struct drm_i915_private *dev_priv)
{
struct inteldrm_request *request;
uint32_t seqno;
if (dev_priv->hw_status_page == NULL)
return;
seqno = i915_get_gem_seqno(dev_priv);
while ((request = TAILQ_FIRST(&dev_priv->mm.request_list)) != NULL) {
if (i915_seqno_passed(seqno, request->seqno) ||
dev_priv->mm.wedged) {
i915_gem_retire_request(dev_priv, request);
TAILQ_REMOVE(&dev_priv->mm.request_list, request, list);
drm_free(request);
} else
break;
}
}
void
i915_gem_retire_work_handler(void *arg1, void *unused)
{
drm_i915_private_t *dev_priv = arg1;
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
DRM_LOCK();
i915_gem_retire_requests(dev_priv);
if (!TAILQ_EMPTY(&dev_priv->mm.request_list))
timeout_add_sec(&dev_priv->mm.retire_timer, 1);
DRM_UNLOCK();
}
/**
* Waits for a sequence number to be signaled, and cleans up the
* request and object lists appropriately for that event.
*
* Called locked, sleeps with it.
*/
int
i915_wait_request(struct drm_i915_private *dev_priv, uint32_t seqno,
int interruptible)
{
int ret = 0;
KASSERT(seqno != dev_priv->mm.next_gem_seqno);
/* Check first because poking a wedged chip is bad. */
if (dev_priv->mm.wedged)
return (EIO);
if (seqno == dev_priv->mm.next_gem_seqno) {
seqno = i915_add_request(dev_priv);
if (seqno == 0)
return (ENOMEM);
}
if (!i915_seqno_passed(i915_get_gem_seqno(dev_priv), seqno)) {
mtx_enter(&dev_priv->user_irq_lock);
i915_user_irq_get(dev_priv);
while (ret == 0) {
if (i915_seqno_passed(i915_get_gem_seqno(dev_priv),
seqno) || dev_priv->mm.wedged)
break;
ret = msleep(dev_priv, &dev_priv->user_irq_lock,
PZERO | (interruptible ? PCATCH : 0), "gemwt", 0);
}
i915_user_irq_put(dev_priv);
mtx_leave(&dev_priv->user_irq_lock);
}
if (dev_priv->mm.wedged)
ret = EIO;
/* Directly dispatch request retiring. While we have the work queue
* to handle this, the waiter on a request often wants an associated
* buffer to have made it to the inactive list, and we would need
* a separate wait queue to handle that.
*/
if (ret == 0)
i915_gem_retire_requests(dev_priv);
return (ret);
}
/*
* flush and invalidate the provided domains
* if we have successfully queued a gpu flush, then we return a seqno from
* the request. else (failed or just cpu flushed) we return 0.
*/
u_int32_t
i915_gem_flush(struct drm_i915_private *dev_priv, uint32_t invalidate_domains,
uint32_t flush_domains)
{
uint32_t cmd;
if (flush_domains & I915_GEM_DOMAIN_CPU)
inteldrm_chipset_flush(dev_priv);
if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
/*
* read/write caches:
*
* I915_GEM_DOMAIN_RENDER is always invalidated, but is
* only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
* also flushed at 2d versus 3d pipeline switches.
*
* read-only caches:
*
* I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
* MI_READ_FLUSH is set, and is always flushed on 965.
*
* I915_GEM_DOMAIN_COMMAND may not exist?
*
* I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
* invalidated when MI_EXE_FLUSH is set.
*
* I915_GEM_DOMAIN_VERTEX, which exists on 965, is
* invalidated with every MI_FLUSH.
*
* TLBs:
*
* On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
* and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
* I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
* are flushed at any MI_FLUSH.
*/
cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
if ((invalidate_domains | flush_domains) &
I915_GEM_DOMAIN_RENDER)
cmd &= ~MI_NO_WRITE_FLUSH;
/*
* On the 965, the sampler cache always gets flushed
* and this bit is reserved.
*/
if (!IS_I965G(dev_priv) &&
invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
cmd |= MI_READ_FLUSH;
if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
cmd |= MI_EXE_FLUSH;
BEGIN_LP_RING(2);
OUT_RING(cmd);
OUT_RING(MI_NOOP);
ADVANCE_LP_RING();
}
/* if this is a gpu flush, process the results */
if (flush_domains & I915_GEM_GPU_DOMAINS) {
inteldrm_process_flushing(dev_priv, flush_domains);
return (i915_add_request(dev_priv));
}
return (0);
}
/**
* 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_obj *obj, int interruptible)
{
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int ret = 0;
/*
* if it's already unbound, or we've already done lastclose, just
* let it happen. XXX does this fail to unwire?
*/
if (obj_priv->dmamap == NULL || dev_priv->agpdmat == NULL)
return 0;
if (obj_priv->pin_count != 0) {
DRM_ERROR("Attempting to unbind pinned buffer\n");
return (EINVAL);
}
KASSERT(!i915_obj_purged(obj_priv));
/* Move the object to the CPU domain to ensure that
* any possible CPU writes while it's not in the GTT
* are flushed when we go to remap it. This will
* also ensure that all pending GPU writes are finished
* before we unbind.
*/
ret = i915_gem_object_set_to_cpu_domain(obj, 1, interruptible);
if (ret)
return ret;
KASSERT(!inteldrm_is_active(obj_priv));
/* if it's purgeable don't bother dirtying the pages */
if (i915_obj_purgeable(obj_priv))
atomic_clearbits_int(&obj_priv->io_flags, I915_DIRTY);
/*
* unload the map, then unwire the backing object.
*/
i915_gem_save_bit_17_swizzle(obj);
bus_dmamap_unload(dev_priv->agpdmat, obj_priv->dmamap);
uvm_objunwire(obj->uao, 0, obj->size);
/* XXX persistent dmamap worth the memory? */
bus_dmamap_destroy(dev_priv->agpdmat, obj_priv->dmamap);
obj_priv->dmamap = NULL;
free(obj_priv->dma_segs, M_DRM);
obj_priv->dma_segs = NULL;
/* XXX this should change whether we tell uvm the page is dirty */
atomic_clearbits_int(&obj_priv->io_flags, I915_DIRTY);
obj_priv->gtt_offset = 0;
atomic_dec(&dev->gtt_count);
atomic_sub(obj->size, &dev->gtt_memory);
/* Remove ourselves from any LRU list if present. */
i915_list_remove((struct inteldrm_obj *)obj);
if (i915_obj_purgeable(obj_priv))
inteldrm_purge_obj(obj);
return (0);
}
int
i915_gem_evict_something(struct drm_i915_private *dev_priv, size_t min_size,
int interruptible)
{
struct drm_obj *obj;
struct inteldrm_request *request;
struct inteldrm_obj *obj_priv;
int ret = 0;
for (;;) {
i915_gem_retire_requests(dev_priv);
/* If there's an inactive buffer available now, grab it
* and be done.
*/
obj = i915_gem_find_inactive_object(dev_priv, min_size);
if (obj != NULL) {
obj_priv = (struct inteldrm_obj *)obj;
KASSERT(obj_priv->pin_count == 0);
KASSERT(!inteldrm_is_active(obj_priv));
/* Wait on the rendering and unbind the buffer. */
return (i915_gem_object_unbind(obj, interruptible));
}
/* If we didn't get anything, but the ring is still processing
* things, wait for one of those things to finish and hopefully
* leave us a buffer to evict.
*/
if ((request = TAILQ_FIRST(&dev_priv->mm.request_list))
!= NULL) {
ret = i915_wait_request(dev_priv, request->seqno,
interruptible);
if (ret)
return (ret);
continue;
}
/* If we didn't have anything on the request list but there
* are buffers awaiting a flush, emit one and try again.
* When we wait on it, those buffers waiting for that flush
* will get moved to inactive.
*/
TAILQ_FOREACH(obj_priv, &dev_priv->mm.flushing_list, list) {
obj = &obj_priv->obj;
if (obj->size >= min_size)
break;
obj = NULL;
}
if (obj != NULL) {
if (i915_gem_flush(dev_priv, obj->write_domain,
obj->write_domain) == 0)
return (ENOMEM);
continue;
}
/*
* If we didn't do any of the above, there's no single buffer
* large enough to swap out for the new one, so just evict
* everything and start again. (This should be rare.)
*/
if (!TAILQ_EMPTY(&dev_priv->mm.inactive_list))
return (i915_gem_evict_inactive(dev_priv));
else
return (i915_gem_evict_everything(dev_priv,
interruptible));
}
/* NOTREACHED */
}
struct drm_obj *
i915_gem_find_inactive_object(struct drm_i915_private *dev_priv,
size_t min_size)
{
struct drm_obj *obj, *best = NULL, *first = NULL;
struct inteldrm_obj *obj_priv;
TAILQ_FOREACH(obj_priv, &dev_priv->mm.inactive_list, list) {
obj = &obj_priv->obj;
if (obj->size >= min_size) {
if ((!inteldrm_is_dirty(obj_priv) ||
i915_obj_purgeable(obj_priv)) &&
(best == NULL || obj->size < best->size)) {
best = obj;
if (best->size == min_size)
return (best);
}
}
if (first == NULL)
first = obj;
}
return ((best != NULL) ? best : first);
}
int
i915_gem_evict_everything(struct drm_i915_private *dev_priv, int interruptible)
{
u_int32_t seqno;
int ret;
if (TAILQ_EMPTY(&dev_priv->mm.inactive_list) &&
TAILQ_EMPTY(&dev_priv->mm.flushing_list) &&
TAILQ_EMPTY(&dev_priv->mm.active_list))
return (ENOSPC);
seqno = i915_gem_flush(dev_priv, I915_GEM_GPU_DOMAINS,
I915_GEM_GPU_DOMAINS);
if (seqno == 0)
return (ENOMEM);
if ((ret = i915_wait_request(dev_priv, seqno, interruptible)) != 0 ||
(ret = i915_gem_evict_inactive(dev_priv)) != 0)
return (ret);
/*
* All lists should be empty because we flushed the whole queue, then
* we evicted the whole shebang, only pinned objects are still bound.
*/
KASSERT(TAILQ_EMPTY(&dev_priv->mm.inactive_list));
KASSERT(TAILQ_EMPTY(&dev_priv->mm.flushing_list));
KASSERT(TAILQ_EMPTY(&dev_priv->mm.active_list));
return (0);
}
/*
* return required GTT alignment for an object, taking into account potential
* fence register needs
*/
bus_size_t
i915_gem_get_gtt_alignment(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
bus_size_t start, i;
/*
* Minimum alignment is 4k (GTT page size), but fence registers may
* modify this
*/
if (IS_I965G(dev_priv) || obj_priv->tiling_mode == I915_TILING_NONE)
return (4096);
/*
* Older chips need to be aligned to the size of the smallest fence
* register that can contain the object.
*/
if (IS_I9XX(dev_priv))
start = 1024 * 1024;
else
start = 512 * 1024;
for (i = start; i < obj->size; i <<= 1)
;
return (i);
}
void
i965_write_fence_reg(struct inteldrm_fence *reg)
{
struct drm_obj *obj = reg->obj;
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int regnum = obj_priv->fence_reg;
u_int64_t val;
val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
0xfffff000) << 32;
val |= obj_priv->gtt_offset & 0xfffff000;
val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
if (obj_priv->tiling_mode == I915_TILING_Y)
val |= 1 << I965_FENCE_TILING_Y_SHIFT;
val |= I965_FENCE_REG_VALID;
I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
}
void
i915_write_fence_reg(struct inteldrm_fence *reg)
{
struct drm_obj *obj = reg->obj;
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
bus_size_t fence_reg;
u_int32_t val;
u_int32_t pitch_val;
int regnum = obj_priv->fence_reg;
int tile_width;
if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
(obj_priv->gtt_offset & (obj->size - 1))) {
DRM_ERROR("object 0x%lx not 1M or size (0x%zx) aligned\n",
obj_priv->gtt_offset, obj->size);
return;
}
if (obj_priv->tiling_mode == I915_TILING_Y &&
HAS_128_BYTE_Y_TILING(dev_priv))
tile_width = 128;
else
tile_width = 512;
/* Note: pitch better be a power of two tile widths */
pitch_val = obj_priv->stride / tile_width;
pitch_val = ffs(pitch_val) - 1;
val = obj_priv->gtt_offset;
if (obj_priv->tiling_mode == I915_TILING_Y)
val |= 1 << I830_FENCE_TILING_Y_SHIFT;
val |= I915_FENCE_SIZE_BITS(obj->size);
val |= pitch_val << I830_FENCE_PITCH_SHIFT;
val |= I830_FENCE_REG_VALID;
if (regnum < 8)
fence_reg = FENCE_REG_830_0 + (regnum * 4);
else
fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
I915_WRITE(fence_reg, val);
}
void
i830_write_fence_reg(struct inteldrm_fence *reg)
{
struct drm_obj *obj = reg->obj;
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int regnum = obj_priv->fence_reg;
u_int32_t pitch_val, val;
if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
(obj_priv->gtt_offset & (obj->size - 1))) {
DRM_ERROR("object 0x%08x not 512K or size aligned 0x%lx\n",
obj_priv->gtt_offset, obj->size);
return;
}
pitch_val = ffs(obj_priv->stride / 128) - 1;
val = obj_priv->gtt_offset;
if (obj_priv->tiling_mode == I915_TILING_Y)
val |= 1 << I830_FENCE_TILING_Y_SHIFT;
val |= I830_FENCE_SIZE_BITS(obj->size);
val |= pitch_val << I830_FENCE_PITCH_SHIFT;
val |= I830_FENCE_REG_VALID;
I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
}
/*
* i915_gem_get_fence_reg - set up a fence reg for an object
*
* 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, 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.
*/
int
i915_gem_get_fence_reg(struct drm_obj *obj, int interruptible)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
struct inteldrm_obj *old_obj_priv = NULL;
struct drm_obj *old_obj = NULL;
struct inteldrm_fence *reg = NULL;
int i, ret, avail;
/* If our fence is getting used, just update our place in the LRU */
if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
reg = &dev_priv->fence_regs[obj_priv->fence_reg];
TAILQ_REMOVE(&dev_priv->mm.fence_list, reg, list);
TAILQ_INSERT_TAIL(&dev_priv->mm.fence_list, reg, list);
return (0);
}
switch (obj_priv->tiling_mode) {
case I915_TILING_NONE:
DRM_ERROR("allocating a fence for non-tiled object?\n");
break;
case I915_TILING_X:
if (obj_priv->stride == 0)
return (EINVAL);
if (obj_priv->stride & (512 - 1))
DRM_ERROR("object 0x%08x is X tiled but has non-512B"
" pitch\n", obj_priv->gtt_offset);
break;
case I915_TILING_Y:
if (obj_priv->stride == 0)
return (EINVAL);
if (obj_priv->stride & (128 - 1))
DRM_ERROR("object 0x%08x is Y tiled but has non-128B"
" pitch\n", obj_priv->gtt_offset);
break;
}
/* First try to find a free reg */
avail = 0;
for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
reg = &dev_priv->fence_regs[i];
if (reg->obj == NULL)
break;
old_obj_priv = (struct inteldrm_obj *)reg->obj;
if (old_obj_priv->pin_count == 0)
avail++;
}
/* None available, try to steal one or wait for a user to finish */
if (i == dev_priv->num_fence_regs) {
if (avail == 0)
return (ENOMEM);
TAILQ_FOREACH(reg, &dev_priv->mm.fence_list,
list) {
old_obj = reg->obj;
old_obj_priv = (struct inteldrm_obj *)old_obj;
if (old_obj_priv->pin_count)
continue;
/* Ref it so that wait_rendering doesn't free it under
* us.
*/
drm_gem_object_reference(old_obj);
break;
}
i = old_obj_priv->fence_reg;
reg = &dev_priv->fence_regs[i];
ret = i915_gem_object_put_fence_reg(old_obj, interruptible);
drm_gem_object_unreference(old_obj);
if (ret != 0)
return (ret);
}
obj_priv->fence_reg = i;
reg->obj = obj;
TAILQ_INSERT_TAIL(&dev_priv->mm.fence_list, reg, list);
if (IS_I965G(dev_priv))
i965_write_fence_reg(reg);
else if (IS_I9XX(dev_priv))
i915_write_fence_reg(reg);
else
i830_write_fence_reg(reg);
return 0;
}
int
i915_gem_object_put_fence_reg(struct drm_obj *obj, int interruptible)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
struct inteldrm_fence *reg;
int ret;
if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
return (0);
/*
* If the last execbuffer we did on the object needed a fence then
* we must emit a flush.
*/
if (inteldrm_needs_fence(obj_priv)) {
ret = i915_gem_object_flush_gpu_write_domain(obj, 1,
interruptible);
if (ret != 0)
return (ret);
}
/* if rendering is queued up that depends on the fence, wait for it */
reg = &dev_priv->fence_regs[obj_priv->fence_reg];
if (reg->last_rendering_seqno != 0) {
ret = i915_wait_request(dev_priv, reg->last_rendering_seqno,
interruptible);
if (ret != 0)
return (ret);
}
/* tiling changed, must wipe userspace mappings */
if ((obj->write_domain | obj->read_domains) & I915_GEM_DOMAIN_GTT) {
inteldrm_wipe_mappings(obj);
if (obj->write_domain == I915_GEM_DOMAIN_GTT)
obj->write_domain = 0;
}
if (IS_I965G(dev_priv)) {
I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
} else {
u_int32_t fence_reg;
if (obj_priv->fence_reg < 8)
fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
else
fence_reg = FENCE_REG_945_8 +
(obj_priv->fence_reg - 8) * 4;
I915_WRITE(fence_reg , 0);
}
reg->obj = NULL;
TAILQ_REMOVE(&dev_priv->mm.fence_list, reg, list);
obj_priv->fence_reg = I915_FENCE_REG_NONE;
return (0);
}
int
inteldrm_fault(struct drm_obj *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_device *dev = obj->dev;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
paddr_t paddr;
int lcv, ret;
int write = !!(access_type & VM_PROT_WRITE);
vm_prot_t mapprot;
DRM_LOCK();
/*
* XXX is it ok to sleep in fault handlers? If not, we may have some
* problems... (i can has race plz? -> judicious use of
* uvmfault_unlockall ahoy)
*/
if (obj_priv->dmamap == NULL) {
ret = i915_gem_object_bind_to_gtt(obj, 0, 0);
if (ret) {
printf("%s: failed to bind\n", __func__);
goto error;
}
i915_gem_object_move_to_inactive(obj);
}
/*
* We could only do this on bind so allow for map_buffer_range
* unsynchronised objects (where buffer suballocation
* is done by the GL application, however it gives coherency problems
* normally.
*/
ret = i915_gem_object_set_to_gtt_domain(obj, write, 0);
if (ret) {
printf("%s: failed to set to gtt (%d)\n",
__func__, ret);
goto error;
}
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_priv->gtt_offset + offset;
UVMHIST_LOG(maphist,
" MAPPING: device: pm=%p, va=0x%lx, pa=0x%lx, at=%ld",
ufi->orig_map->pmap, vaddr, (u_long)paddr, mapprot);
/* XXX writecombining */
if (pmap_enter(ufi->orig_map->pmap, vaddr, paddr | PMAP_NOCACHE,
mapprot, PMAP_CANFAIL | mapprot) != 0) {
DRM_UNLOCK();
printf("%s: enter failed\n", __func__);
return (VM_PAGER_REFAULT);
}
}
DRM_UNLOCK();
return (VM_PAGER_OK);
error:
/*
* EIO means we're wedged so when we reset the gpu this will
* work, so don't segfault.
*/
DRM_UNLOCK();
return ((ret == EIO) ? VM_PAGER_REFAULT : VM_PAGER_ERROR);
}
void
inteldrm_wipe_mappings(struct drm_obj *obj)
{
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct vm_page *pg;
/* nuke all our mappings. XXX optimise. */
for (pg = &dev_priv->pgs[atop(obj_priv->gtt_offset)]; pg !=
&dev_priv->pgs[atop(obj_priv->gtt_offset + obj->size)]; pg++)
pmap_page_protect(pg, VM_PROT_NONE);
}
/**
* Finds free space in the GTT aperture and binds the object there.
*/
int
i915_gem_object_bind_to_gtt(struct drm_obj *obj, bus_size_t alignment,
int interruptible)
{
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int ret;
if (dev_priv->agpdmat == NULL)
return (EINVAL);
if (alignment == 0) {
alignment = i915_gem_get_gtt_alignment(obj);
} else if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
DRM_ERROR("Invalid object alignment requested %u\n", alignment);
return (EINVAL);
}
/* Can't bind a purgeable buffer */
if (i915_obj_purgeable(obj_priv)) {
printf("tried to bind purgeable buffer");
return (EINVAL);
}
if ((ret = bus_dmamap_create(dev_priv->agpdmat, obj->size, 1,
obj->size, 0, BUS_DMA_WAITOK, &obj_priv->dmamap)) != 0) {
DRM_ERROR("Failed to create dmamap\n");
return (ret);
}
agp_bus_dma_set_alignment(dev_priv->agpdmat, obj_priv->dmamap,
alignment);
search_free:
/*
* 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_priv->dmamap, obj->uao,
obj->size, BUS_DMA_WAITOK | obj_priv->dma_flags,
&obj_priv->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 (TAILQ_EMPTY(&dev_priv->mm.inactive_list) &&
TAILQ_EMPTY(&dev_priv->mm.flushing_list) &&
TAILQ_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->size,
interruptible);
if (ret != 0)
goto error;
goto search_free;
}
i915_gem_bit_17_swizzle(obj);
obj_priv->gtt_offset = obj_priv->dmamap->dm_segs[0].ds_addr -
dev->agp->base;
atomic_inc(&dev->gtt_count);
atomic_add(obj->size, &dev->gtt_memory);
/* 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
*/
KASSERT((obj->read_domains & I915_GEM_GPU_DOMAINS) == 0);
KASSERT((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0);
return (0);
error:
bus_dmamap_destroy(dev_priv->agpdmat, obj_priv->dmamap);
obj_priv->dmamap = NULL;
obj_priv->gtt_offset = 0;
return (ret);
}
/*
* Flush the GPU write domain for the object if dirty, then wait for the
* rendering to complete. When this returns it is safe to unbind from the
* GTT or access from the CPU.
*/
int
i915_gem_object_flush_gpu_write_domain(struct drm_obj *obj, int pipelined,
int interruptible)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int ret = 0;
if ((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0) {
/*
* Queue the GPU write cache flushing we need.
* This call will move stuff form the flushing list to the
* active list so all we need to is wait for it.
*/
(void)i915_gem_flush(dev_priv, 0, obj->write_domain);
KASSERT(obj->write_domain == 0);
}
/* wait for queued rendering so we know it's flushed and bo is idle */
if (pipelined == 0 && inteldrm_is_active(obj_priv)) {
ret = i915_wait_request(dev_priv,
obj_priv->last_rendering_seqno, interruptible);
}
return (ret);
}
/*
* Moves a single object to the GTT 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_obj *obj, int write,
int interruptible)
{
struct drm_device *dev = (struct drm_device *)obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int ret;
/* Not valid to be called on unbound objects. */
if (obj_priv->dmamap == NULL)
return (EINVAL);
/* Wait on any GPU rendering and flushing to occur. */
if ((ret = i915_gem_object_flush_gpu_write_domain(obj, 0,
interruptible)) != 0)
return (ret);
if (obj->write_domain == I915_GEM_DOMAIN_CPU) {
/* clflush the pages, and flush chipset cache */
bus_dmamap_sync(dev_priv->agpdmat, obj_priv->dmamap, 0,
obj->size, BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
inteldrm_chipset_flush(dev_priv);
obj->write_domain = 0;
}
/* We're accessing through the gpu, so grab a new fence register or
* update the LRU.
*/
if (obj_priv->tiling_mode != I915_TILING_NONE)
ret = i915_gem_get_fence_reg(obj, interruptible);
/*
* If we're writing through the GTT domain then the CPU and GPU caches
* will need to be invalidated at next use.
* It should now be out of any other write domains and we can update
* to the correct ones
*/
KASSERT((obj->write_domain & ~I915_GEM_DOMAIN_GTT) == 0);
if (write) {
obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_GTT;
atomic_setbits_int(&obj_priv->io_flags, I915_DIRTY);
} else {
obj->read_domains |= I915_GEM_DOMAIN_GTT;
}
return (ret);
}
/*
* 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_obj *obj, int write,
int interruptible)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int ret;
/* Wait on any GPU rendering and flushing to occur. */
if ((ret = i915_gem_object_flush_gpu_write_domain(obj, 0,
interruptible)) != 0)
return (ret);
if (obj->write_domain == I915_GEM_DOMAIN_GTT ||
(write && obj->read_domains & I915_GEM_DOMAIN_GTT)) {
/*
* No actual flushing is required for the GTT write domain.
* Writes to it immeditately go to main memory as far as we
* know, so there's no chipset flush. It also doesn't land
* in render cache.
*/
inteldrm_wipe_mappings(obj);
if (obj->write_domain == I915_GEM_DOMAIN_GTT)
obj->write_domain = 0;
}
/* remove the fence register since we're not using it anymore */
if ((ret = i915_gem_object_put_fence_reg(obj, interruptible)) != 0)
return (ret);
/* Flush the CPU cache if it's still invalid. */
if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
bus_dmamap_sync(dev_priv->agpdmat, obj_priv->dmamap, 0,
obj->size, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
obj->read_domains |= I915_GEM_DOMAIN_CPU;
}
/*
* It should now be out of any other write domain, and we can update
* the domain value for our changes.
*/
KASSERT((obj->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->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
return (0);
}
/*
* Set the next domain for the specified object. This
* may not actually perform the necessary flushing/invaliding though,
* as that may want to be batched with other set_domain operations
*
* This is (we hope) the only really tricky part of gem. The goal
* is fairly simple -- track which caches hold bits of the object
* and make sure they remain coherent. A few concrete examples may
* help to explain how it works. For shorthand, we use the notation
* (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
* a pair of read and write domain masks.
*
* Case 1: the batch buffer
*
* 1. Allocated
* 2. Written by CPU
* 3. Mapped to GTT
* 4. Read by GPU
* 5. Unmapped from GTT
* 6. Freed
*
* Let's take these a step at a time
*
* 1. Allocated
* Pages allocated from the kernel may still have
* cache contents, so we set them to (CPU, CPU) always.
* 2. Written by CPU (using pwrite)
* The pwrite function calls set_domain (CPU, CPU) and
* this function does nothing (as nothing changes)
* 3. Mapped by GTT
* This function asserts that the object is not
* currently in any GPU-based read or write domains
* 4. Read by GPU
* i915_gem_execbuffer calls set_domain (COMMAND, 0).
* As write_domain is zero, this function adds in the
* current read domains (CPU+COMMAND, 0).
* flush_domains is set to CPU.
* invalidate_domains is set to COMMAND
* clflush is run to get data out of the CPU caches
* then i915_dev_set_domain calls i915_gem_flush to
* emit an MI_FLUSH and drm_agp_chipset_flush
* 5. Unmapped from GTT
* i915_gem_object_unbind calls set_domain (CPU, CPU)
* flush_domains and invalidate_domains end up both zero
* so no flushing/invalidating happens
* 6. Freed
* yay, done
*
* Case 2: The shared render buffer
*
* 1. Allocated
* 2. Mapped to GTT
* 3. Read/written by GPU
* 4. set_domain to (CPU,CPU)
* 5. Read/written by CPU
* 6. Read/written by GPU
*
* 1. Allocated
* Same as last example, (CPU, CPU)
* 2. Mapped to GTT
* Nothing changes (assertions find that it is not in the GPU)
* 3. Read/written by GPU
* execbuffer calls set_domain (RENDER, RENDER)
* flush_domains gets CPU
* invalidate_domains gets GPU
* clflush (obj)
* MI_FLUSH and drm_agp_chipset_flush
* 4. set_domain (CPU, CPU)
* flush_domains gets GPU
* invalidate_domains gets CPU
* flush_gpu_write (obj) to make sure all drawing is complete.
* This will include an MI_FLUSH to get the data from GPU
* to memory
* clflush (obj) to invalidate the CPU cache
* Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
* 5. Read/written by CPU
* cache lines are loaded and dirtied
* 6. Read written by GPU
* Same as last GPU access
*
* Case 3: The constant buffer
*
* 1. Allocated
* 2. Written by CPU
* 3. Read by GPU
* 4. Updated (written) by CPU again
* 5. Read by GPU
*
* 1. Allocated
* (CPU, CPU)
* 2. Written by CPU
* (CPU, CPU)
* 3. Read by GPU
* (CPU+RENDER, 0)
* flush_domains = CPU
* invalidate_domains = RENDER
* clflush (obj)
* MI_FLUSH
* drm_agp_chipset_flush
* 4. Updated (written) by CPU again
* (CPU, CPU)
* flush_domains = 0 (no previous write domain)
* invalidate_domains = 0 (no new read domains)
* 5. Read by GPU
* (CPU+RENDER, 0)
* flush_domains = CPU
* invalidate_domains = RENDER
* clflush (obj)
* MI_FLUSH
* drm_agp_chipset_flush
*/
void
i915_gem_object_set_to_gpu_domain(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
u_int32_t invalidate_domains = 0;
u_int32_t flush_domains = 0;
KASSERT((obj->pending_read_domains & I915_GEM_DOMAIN_CPU) == 0);
KASSERT(obj->pending_write_domain != I915_GEM_DOMAIN_CPU);
/*
* If the object isn't moving to a new write domain,
* let the object stay in multiple read domains
*/
if (obj->pending_write_domain == 0)
obj->pending_read_domains |= obj->read_domains;
else
atomic_setbits_int(&obj_priv->io_flags, I915_DIRTY);
/*
* Flush the current write domain if
* the new read domains don't match. Invalidate
* any read domains which differ from the old
* write domain
*/
if (obj->write_domain &&
obj->write_domain != obj->pending_read_domains) {
flush_domains |= obj->write_domain;
invalidate_domains |= obj->pending_read_domains &
~obj->write_domain;
}
/*
* Invalidate any read caches which may have
* stale data. That is, any new read domains.
*/
invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
/* clflush the cpu now, gpu caches get queued. */
if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
bus_dmamap_sync(dev_priv->agpdmat, obj_priv->dmamap, 0,
obj->size, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
}
if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT) {
inteldrm_wipe_mappings(obj);
}
/* The actual obj->write_domain will be updated with
* pending_write_domain after we emit the accumulated flush for all of
* the domain changes in execuffer (which clears object's write
* domains). So if we have a current write domain that we aren't
* changing, set pending_write_domain to it.
*/
if (flush_domains == 0 && obj->pending_write_domain == 0 &&
(obj->pending_read_domains == obj->write_domain ||
obj->write_domain == 0))
obj->pending_write_domain = obj->write_domain;
obj->read_domains = obj->pending_read_domains;
obj->pending_read_domains = 0;
dev->invalidate_domains |= invalidate_domains;
dev->flush_domains |= flush_domains;
}
/**
* Pin an object to the GTT and evaluate the relocations landing in it.
*/
int
i915_gem_object_pin_and_relocate(struct drm_obj *obj,
struct drm_file *file_priv, struct drm_i915_gem_exec_object2 *entry,
struct drm_i915_gem_relocation_entry *relocs)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct drm_obj *target_obj;
struct inteldrm_obj *obj_priv =
(struct inteldrm_obj *)obj;
bus_space_handle_t bsh;
int i, ret, needs_fence;
needs_fence = (entry->flags & EXEC_OBJECT_NEEDS_FENCE) &&
obj_priv->tiling_mode != I915_TILING_NONE;
if (needs_fence)
atomic_setbits_int(&obj_priv->io_flags, I915_EXEC_NEEDS_FENCE);
/* Choose the GTT offset for our buffer and put it there. */
ret = i915_gem_object_pin(obj, (u_int32_t)entry->alignment,
needs_fence);
/* XXX what if already bound at a different alignment? */
if (ret)
return ret;
entry->offset = obj_priv->gtt_offset;
/* Apply the relocations, using the GTT aperture to avoid cache
* flushing requirements.
*/
for (i = 0; i < entry->relocation_count; i++) {
struct drm_i915_gem_relocation_entry *reloc = &relocs[i];
struct inteldrm_obj *target_obj_priv;
uint32_t reloc_val, reloc_offset;
target_obj = drm_gem_object_lookup(obj->dev, file_priv,
reloc->target_handle);
if (target_obj == NULL) {
i915_gem_object_unpin(obj);
return (EBADF);
}
target_obj_priv = (struct inteldrm_obj *)target_obj;
/* The target buffer should have appeared before us in the
* exec_object list, so it should have a GTT space bound by now.
*/
if (target_obj_priv->dmamap == 0) {
DRM_ERROR("No GTT space found for object %d\n",
reloc->target_handle);
ret = EINVAL;
goto err;
}
/* must be in one write domain and one only */
if (reloc->write_domain & (reloc->write_domain - 1)) {
ret = EINVAL;
goto err;
}
if (reloc->read_domains & I915_GEM_DOMAIN_CPU ||
reloc->write_domain & I915_GEM_DOMAIN_CPU) {
DRM_ERROR("relocation with read/write CPU domains: "
"obj %p target %d offset %d "
"read %08x write %08x", obj,
reloc->target_handle, (int)reloc->offset,
reloc->read_domains, reloc->write_domain);
ret = EINVAL;
goto err;
}
if (reloc->write_domain && target_obj->pending_write_domain &&
reloc->write_domain != target_obj->pending_write_domain) {
DRM_ERROR("Write domain conflict: "
"obj %p target %d offset %d "
"new %08x old %08x\n",
obj, reloc->target_handle,
(int) reloc->offset,
reloc->write_domain,
target_obj->pending_write_domain);
ret = EINVAL;
goto err;
}
target_obj->pending_read_domains |= reloc->read_domains;
target_obj->pending_write_domain |= reloc->write_domain;
if (reloc->offset > obj->size - 4) {
DRM_ERROR("Relocation beyond object bounds: "
"obj %p target %d offset %d size %d.\n",
obj, reloc->target_handle,
(int) reloc->offset, (int) obj->size);
ret = EINVAL;
goto err;
}
if (reloc->offset & 3) {
DRM_ERROR("Relocation not 4-byte aligned: "
"obj %p target %d offset %d.\n",
obj, reloc->target_handle,
(int) reloc->offset);
ret = EINVAL;
goto err;
}
if (reloc->delta > target_obj->size) {
DRM_ERROR("reloc larger than target\n");
ret = EINVAL;
goto err;
}
/* Map the page containing the relocation we're going to
* perform.
*/
reloc_offset = obj_priv->gtt_offset + reloc->offset;
reloc_val = target_obj_priv->gtt_offset + reloc->delta;
if ((ret = bus_space_subregion(dev_priv->bst,
dev_priv->aperture_bsh, trunc_page(reloc_offset),
PAGE_SIZE, &bsh)) != 0) {
DRM_ERROR("map failed...\n");
goto err;
}
/*
* we do this differently to linux, in the case where the
* presumed offset matches we actually read to check it's
* correct, but at least it won't involve idling the gpu if
* it was reading from it before, only if writing (which would
* be bad anyway since we're now using it as a command buffer).
*/
if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
ret = i915_gem_object_set_to_gtt_domain(obj, 0, 1);
if (ret != 0)
goto err;
if (bus_space_read_4(dev_priv->bst, bsh,
reloc_offset & PAGE_MASK) == reloc_val) {
drm_gem_object_unreference(target_obj);
continue;
}
DRM_DEBUG("reloc tested and found incorrect\n");
}
ret = i915_gem_object_set_to_gtt_domain(obj, 1, 1);
if (ret != 0)
goto err;
bus_space_write_4(dev_priv->bst, bsh, reloc_offset & PAGE_MASK,
reloc_val);
reloc->presumed_offset = target_obj_priv->gtt_offset;
drm_gem_object_unreference(target_obj);
}
return 0;
err:
/* we always jump to here mid-loop */
drm_gem_object_unreference(target_obj);
i915_gem_object_unpin(obj);
return (ret);
}
/** Dispatch a batchbuffer to the ring
*/
void
i915_dispatch_gem_execbuffer(struct drm_device *dev,
struct drm_i915_gem_execbuffer2 *exec, uint64_t exec_offset)
{
drm_i915_private_t *dev_priv = dev->dev_private;
uint32_t exec_start, exec_len;
exec_start = (uint32_t)exec_offset + exec->batch_start_offset;
exec_len = (uint32_t)exec->batch_len;
if (IS_I830(dev_priv) || IS_845G(dev_priv)) {
BEGIN_LP_RING(6);
OUT_RING(MI_BATCH_BUFFER);
OUT_RING(exec_start | MI_BATCH_NON_SECURE);
OUT_RING(exec_start + exec_len - 4);
OUT_RING(MI_NOOP);
} else {
BEGIN_LP_RING(4);
if (IS_I965G(dev_priv)) {
OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) |
MI_BATCH_NON_SECURE_I965);
OUT_RING(exec_start);
} else {
OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
OUT_RING(exec_start | MI_BATCH_NON_SECURE);
}
}
/*
* Ensure that the commands in the batch buffer are
* finished before the interrupt fires (from a subsequent request
* added). We get back a seqno representing the execution of the
* current buffer, which we can wait on. We would like to mitigate
* these interrupts, likely by only creating seqnos occasionally
* (so that we have *some* interrupts representing completion of
* buffers that we can wait on when trying to clear up gtt space).
*/
OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH);
OUT_RING(MI_NOOP);
ADVANCE_LP_RING();
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
/* The sampler always gets flushed on i965 (sigh) */
if (IS_I965G(dev_priv))
inteldrm_process_flushing(dev_priv, I915_GEM_DOMAIN_SAMPLER);
}
/* Throttle our rendering by waiting until the ring has completed our requests
* emitted over 20 msec ago.
*
* 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_priv)
{
#if 0
struct inteldrm_file *intel_file = (struct inteldrm_file *)file_priv;
u_int32_t seqno;
#endif
int ret = 0;
return ret;
}
int
i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
u_int32_t buffer_count, struct drm_i915_gem_relocation_entry **relocs)
{
u_int32_t reloc_count = 0, reloc_index = 0, i;
int ret;
*relocs = NULL;
for (i = 0; i < buffer_count; i++) {
if (reloc_count + exec_list[i].relocation_count < reloc_count)
return (EINVAL);
reloc_count += exec_list[i].relocation_count;
}
if (reloc_count == 0)
return (0);
if (SIZE_MAX / reloc_count < sizeof(**relocs))
return (EINVAL);
*relocs = drm_alloc(reloc_count * sizeof(**relocs));
for (i = 0; i < buffer_count; i++) {
if ((ret = copyin((void *)(uintptr_t)exec_list[i].relocs_ptr,
&(*relocs)[reloc_index], exec_list[i].relocation_count *
sizeof(**relocs))) != 0) {
drm_free(*relocs);
*relocs = NULL;
return (ret);
}
reloc_index += exec_list[i].relocation_count;
}
return (0);
}
int
i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
u_int32_t buffer_count, struct drm_i915_gem_relocation_entry *relocs)
{
u_int32_t reloc_count = 0, i;
int ret = 0;
if (relocs == NULL)
return (0);
for (i = 0; i < buffer_count; i++) {
if ((ret = copyout(&relocs[reloc_count],
(void *)(uintptr_t)exec_list[i].relocs_ptr,
exec_list[i].relocation_count * sizeof(*relocs))) != 0)
break;
reloc_count += exec_list[i].relocation_count;
}
drm_free(relocs);
return (ret);
}
int
i915_gem_execbuffer2(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_execbuffer2 *args = data;
struct drm_i915_gem_exec_object2 *exec_list = NULL;
struct drm_i915_gem_relocation_entry *relocs;
struct inteldrm_obj *obj_priv, *batch_obj_priv;
struct drm_obj **object_list = NULL;
struct drm_obj *batch_obj, *obj;
size_t oflow;
int ret, ret2, i;
int pinned = 0, pin_tries;
uint32_t reloc_index;
/*
* Check for valid execbuffer offset. We can do this early because
* bound object are always page aligned, so only the start offset
* matters. Also check for integer overflow in the batch offset and size
*/
if ((args->batch_start_offset | args->batch_len) & 0x7 ||
args->batch_start_offset + args->batch_len < args->batch_len ||
args->batch_start_offset + args->batch_len <
args->batch_start_offset)
return (EINVAL);
if (args->buffer_count < 1) {
DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
return (EINVAL);
}
/* Copy in the exec list from userland, check for overflow */
oflow = SIZE_MAX / args->buffer_count;
if (oflow < sizeof(*exec_list) || oflow < sizeof(*object_list))
return (EINVAL);
exec_list = drm_alloc(sizeof(*exec_list) * args->buffer_count);
object_list = drm_alloc(sizeof(*object_list) * args->buffer_count);
if (exec_list == NULL || object_list == NULL) {
ret = ENOMEM;
goto pre_mutex_err;
}
ret = copyin((void *)(uintptr_t)args->buffers_ptr, exec_list,
sizeof(*exec_list) * args->buffer_count);
if (ret != 0)
goto pre_mutex_err;
ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
&relocs);
if (ret != 0)
goto pre_mutex_err;
DRM_LOCK();
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
/* XXX check these before we copyin... but we do need the lock */
if (dev_priv->mm.wedged) {
ret = EIO;
goto unlock;
}
if (dev_priv->mm.suspended) {
ret = EBUSY;
goto unlock;
}
/* Look up object handles */
for (i = 0; i < args->buffer_count; i++) {
object_list[i] = drm_gem_object_lookup(dev, file_priv,
exec_list[i].handle);
if (object_list[i] == NULL) {
DRM_ERROR("Invalid object handle %d at index %d\n",
exec_list[i].handle, i);
ret = EBADF;
goto err;
}
obj_priv = (struct inteldrm_obj *)object_list[i];
if (obj_priv->io_flags & I915_IN_EXEC) {
DRM_ERROR("Object %p appears more than once in object_list\n",
object_list[i]);
ret = EBADF;
goto err;
}
atomic_setbits_int(&obj_priv->io_flags, I915_IN_EXEC);
}
/* Pin and relocate */
for (pin_tries = 0; ; pin_tries++) {
ret = pinned = 0;
reloc_index = 0;
for (i = 0; i < args->buffer_count; i++) {
object_list[i]->pending_read_domains = 0;
object_list[i]->pending_write_domain = 0;
ret = i915_gem_object_pin_and_relocate(object_list[i],
file_priv, &exec_list[i], &relocs[reloc_index]);
if (ret)
break;
pinned++;
reloc_index += exec_list[i].relocation_count;
}
/* success */
if (ret == 0)
break;
/* error other than GTT full, or we've already tried again */
if (ret != ENOSPC || pin_tries >= 1)
goto err;
/* unpin all of our buffers */
for (i = 0; i < pinned; i++)
i915_gem_object_unpin(object_list[i]);
/* evict everyone we can from the aperture */
ret = i915_gem_evict_everything(dev_priv, 1);
if (ret)
goto err;
}
/* Set the pending read domains for the batch buffer to COMMAND */
batch_obj = object_list[args->buffer_count - 1];
batch_obj_priv = (struct inteldrm_obj *)batch_obj;
if (args->batch_start_offset + args->batch_len > batch_obj->size ||
batch_obj->pending_write_domain) {
ret = EINVAL;
goto err;
}
batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
/*
* Zero the global flush/invalidate flags. These will be modified as
* new domains are computed for each object
*/
dev->invalidate_domains = 0;
dev->flush_domains = 0;
/* Compute new gpu domains and update invalidate/flush */
for (i = 0; i < args->buffer_count; i++)
i915_gem_object_set_to_gpu_domain(object_list[i]);
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
/* flush and invalidate any domains that need them. */
(void)i915_gem_flush(dev_priv, dev->invalidate_domains,
dev->flush_domains);
/*
* update the write domains, and fence/gpu write accounting information.
* Also do the move to active list here. The lazy seqno accounting will
* make sure that they have the correct seqno. If the add_request
* fails, then we will wait for a later batch (or one added on the
* wait), which will waste some time, but if we're that low on memory
* then we could fail in much worse ways.
*/
for (i = 0; i < args->buffer_count; i++) {
obj = object_list[i];
obj_priv = (struct inteldrm_obj *)obj;
obj->write_domain = obj->pending_write_domain;
/*
* if we have a write domain, add us to the gpu write list
* else we can remove the bit because it has been flushed.
*/
if (obj_priv->io_flags & I915_GPU_WRITE)
TAILQ_REMOVE(&dev_priv->mm.gpu_write_list, obj_priv,
write_list);
if (obj->write_domain) {
TAILQ_INSERT_TAIL(&dev_priv->mm.gpu_write_list,
obj_priv, write_list);
atomic_setbits_int(&obj_priv->io_flags, I915_GPU_WRITE);
} else {
atomic_clearbits_int(&obj_priv->io_flags,
I915_GPU_WRITE);
}
/* if this batchbuffer needs a fence, then the object is
* counted as fenced exec. else any outstanding fence waits
* will just wait on the fence last_seqno.
*/
if (inteldrm_exec_needs_fence(obj_priv)) {
atomic_setbits_int(&obj_priv->io_flags,
I915_FENCED_EXEC);
} else {
atomic_clearbits_int(&obj_priv->io_flags,
I915_FENCED_EXEC);
}
i915_gem_object_move_to_active(object_list[i]);
}
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
/* Exec the batchbuffer */
/*
* XXX make sure that this may never fail by preallocating the request.
*/
i915_dispatch_gem_execbuffer(dev, args, batch_obj_priv->gtt_offset);
/*
* move to active associated all previous buffers with the seqno
* that this call will emit. so we don't need the return.
*/
(void)i915_add_request(dev_priv);
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
ret = copyout(exec_list, (void *)(uintptr_t)args->buffers_ptr,
sizeof(*exec_list) * args->buffer_count);
err:
for (i = 0; i < args->buffer_count; i++) {
if (object_list[i] == NULL)
break;
obj_priv = (struct inteldrm_obj *)object_list[i];
if (i < pinned)
i915_gem_object_unpin(object_list[i]);
atomic_clearbits_int(&obj_priv->io_flags, I915_IN_EXEC |
I915_EXEC_NEEDS_FENCE);
drm_gem_object_unreference(object_list[i]);
}
unlock:
DRM_UNLOCK();
pre_mutex_err:
/* update userlands reloc state. */
ret2 = i915_gem_put_relocs_to_user(exec_list,
args->buffer_count, relocs);
if (ret2 != 0 && ret == 0)
ret = ret2;
drm_free(object_list);
drm_free(exec_list);
return ret;
}
int
i915_gem_object_pin(struct drm_obj *obj, uint32_t alignment, int needs_fence)
{
struct drm_device *dev = obj->dev;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
int ret;
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
/*
* if already bound, but alignment is unsuitable, unbind so we can
* fix it. Similarly if we have constraints due to fence registers,
* adjust if needed. Note that if we are already pinned we may as well
* fail because whatever depends on this alignment will render poorly
* otherwise, so just fail the pin (with a printf so we can fix a
* wrong userland).
*/
if ((alignment && obj_priv->dmamap != NULL &&
obj_priv->gtt_offset & (alignment - 1)) || (needs_fence &&
!i915_gem_object_fence_offset_ok(obj, obj_priv->tiling_mode))) {
if (obj_priv->pin_count == 0) {
ret = i915_gem_object_unbind(obj, 1);
if (ret)
return (ret);
} else {
DRM_ERROR("repinning an object with bad alignment\n");
}
return (EINVAL);
}
if (obj_priv->dmamap == NULL) {
ret = i915_gem_object_bind_to_gtt(obj, alignment, 1);
if (ret != 0)
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 (needs_fence && obj_priv->tiling_mode != I915_TILING_NONE &&
(ret = i915_gem_get_fence_reg(obj, 1)) != 0)
return (ret);
/* If the object is not active and not pending a flush,
* remove it from the inactive list
*/
if (++obj_priv->pin_count == 1) {
atomic_inc(&dev->pin_count);
atomic_add(obj->size, &dev->pin_memory);
if (!inteldrm_is_active(obj_priv))
i915_list_remove(obj_priv);
}
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
return (0);
}
void
i915_gem_object_unpin(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
inteldrm_verify_inactive(dev_priv, __FILE__, __LINE__);
KASSERT(obj_priv->pin_count >= 1);
KASSERT(obj_priv->dmamap != NULL);
/* If the object is no longer pinned, and is
* neither active nor being flushed, then stick it on
* the inactive list
*/
if (--obj_priv->pin_count == 0) {
if (!inteldrm_is_active(obj_priv))
i915_gem_object_move_to_inactive(obj);
atomic_dec(&dev->pin_count);
atomic_sub(obj->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_priv)
{
struct drm_i915_gem_pin *args = data;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
int ret = 0;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
obj_priv = (struct inteldrm_obj *)obj;
DRM_LOCK();
if (i915_obj_purgeable(obj_priv)) {
printf("pinning purgeable object\n");
ret = EINVAL;
goto out;
}
if (++obj_priv->user_pin_count == 1) {
ret = i915_gem_object_pin(obj, args->alignment, 1);
if (ret != 0)
goto out;
}
/* 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, 1, 1);
args->offset = obj_priv->gtt_offset;
out:
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return (ret);
}
int
i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_gem_pin *args = data;
struct inteldrm_obj *obj_priv;
struct drm_obj *obj;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
DRM_LOCK();
obj_priv = (struct inteldrm_obj *)obj;
if (obj_priv->user_pin_count == 0) {
DRM_UNLOCK();
return (EINVAL);
}
if (--obj_priv->user_pin_count == 0)
i915_gem_object_unpin(obj);
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return (0);
}
int
i915_gem_busy_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_private *dev_priv = dev->dev_private;
struct drm_i915_gem_busy *args = data;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL) {
DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
args->handle);
return (EBADF);
}
DRM_LOCK();
/*
* Update the active list for the hardware's current position.
* otherwise this will only update on a delayed timer or when
* the irq is unmasked. This keeps our working set smaller.
*/
i915_gem_retire_requests(dev_priv);
obj_priv = (struct inteldrm_obj *)obj;
/*
* Don't count being on the flushing list being done. Otherwise, a
* buffer left on the flushing list but not getting flushed (because
* no one is flushing that domain) won't ever return unbusy and get
* reused by libdrm's bo cache. The other expected consumer of this
* interface, OpenGL's occlusion queries, also specs that the object
* get unbusy "eventually" without any interference.
*/
args->busy = inteldrm_is_active(obj_priv) &&
obj_priv->last_rendering_seqno != 0;
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return 0;
}
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_obj *obj;
struct inteldrm_obj *obj_priv;
int need, ret = 0;
switch (args->madv) {
case I915_MADV_DONTNEED:
need = 0;
break;
case I915_MADV_WILLNEED:
need = 1;
break;
default:
return (EINVAL);
}
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
obj_priv = (struct inteldrm_obj *)obj;
DRM_LOCK();
/* invalid to madvise on a pinned BO */
if (obj_priv->pin_count) {
ret = EINVAL;
goto out;
}
if (!i915_obj_purged(obj_priv)) {
if (need) {
atomic_clearbits_int(&obj_priv->io_flags,
I915_DONTNEED);
} else {
atomic_setbits_int(&obj_priv->io_flags, I915_DONTNEED);
}
}
/* if the object is no longer bound, discard its backing storage */
if (i915_obj_purgeable(obj_priv) && obj_priv->dmamap == NULL)
inteldrm_purge_obj(obj);
args->retained = !i915_obj_purged(obj_priv);
out:
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return (ret);
}
int
i915_gem_init_object(struct drm_obj *obj)
{
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
/*
* 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;
/* normal objects don't need special treatment */
obj_priv->dma_flags = 0;
obj_priv->fence_reg = I915_FENCE_REG_NONE;
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 *obj)
{
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
while (obj_priv->pin_count > 0)
i915_gem_object_unpin(obj);
i915_gem_object_unbind(obj, 0);
/* XXX dmatag went away? */
}
/** Unbinds all objects that are on the given buffer list. */
int
i915_gem_evict_inactive(struct drm_i915_private *dev_priv)
{
struct inteldrm_obj *obj_priv;
int ret;
while ((obj_priv = TAILQ_FIRST(&dev_priv->mm.inactive_list)) != NULL) {
if (obj_priv->pin_count != 0) {
DRM_ERROR("Pinned object in unbind list\n");
return (EINVAL);
}
if ((ret = i915_gem_object_unbind(&obj_priv->obj, 1)) != 0)
break;
}
return (ret);
}
int
i915_gem_idle(struct drm_i915_private *dev_priv)
{
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
int ret;
DRM_LOCK();
if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
DRM_UNLOCK();
return (0);
}
/*
* If we're wedged, the workq will clear everything, else this will
* empty out the lists for us.
*/
if ((ret = i915_gem_evict_everything(dev_priv, 1)) != 0 && ret != ENOSPC) {
DRM_UNLOCK();
return (ret);
}
/* 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->mm.hang_timer);
inteldrm_update_ring(dev_priv);
i915_gem_cleanup_ringbuffer(dev_priv);
DRM_UNLOCK();
/* this should be idle now */
timeout_del(&dev_priv->mm.retire_timer);
return 0;
}
int
i915_gem_init_hws(struct drm_i915_private *dev_priv)
{
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
int ret;
/* If we need a physical address for the status page, it's already
* initialized at driver load time.
*/
if (!I915_NEED_GFX_HWS(dev_priv))
return 0;
obj = drm_gem_object_alloc(dev, 4096);
if (obj == NULL) {
DRM_ERROR("Failed to allocate status page\n");
return (ENOMEM);
}
obj_priv = (struct inteldrm_obj *)obj;
/*
* snooped gtt mapping please .
* Normally this flag is only to dmamem_map, but it's been overloaded
* for the agp mapping
*/
obj_priv->dma_flags = BUS_DMA_COHERENT | BUS_DMA_READ;
ret = i915_gem_object_pin(obj, 4096, 0);
if (ret != 0) {
drm_gem_object_unreference(obj);
return ret;
}
dev_priv->hw_status_page = (void *)vm_map_min(kernel_map);
obj->uao->pgops->pgo_reference(obj->uao);
if ((ret = uvm_map(kernel_map, (vaddr_t *)&dev_priv->hw_status_page,
PAGE_SIZE, obj->uao, 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
UVM_INH_SHARE, UVM_ADV_RANDOM, 0))) != 0)
if (ret != 0) {
DRM_ERROR("Failed to map status page.\n");
obj->uao->pgops->pgo_detach(obj->uao);
memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
i915_gem_object_unpin(obj);
drm_gem_object_unreference(obj);
return (EINVAL);
}
dev_priv->hws_obj = obj;
memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
I915_WRITE(HWS_PGA, obj_priv->gtt_offset);
I915_READ(HWS_PGA); /* posting read */
DRM_DEBUG("hws offset: 0x%08x\n", obj_priv->gtt_offset);
return 0;
}
void
i915_gem_cleanup_hws(struct drm_i915_private *dev_priv)
{
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
if (dev_priv->hws_obj == NULL)
return;
obj = dev_priv->hws_obj;
obj_priv = (struct inteldrm_obj *)obj;
uvm_unmap(kernel_map, (vaddr_t)dev_priv->hw_status_page,
(vaddr_t)dev_priv->hw_status_page + PAGE_SIZE);
i915_gem_object_unpin(obj);
drm_gem_object_unreference(obj);
dev_priv->hws_obj = NULL;
memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
dev_priv->hw_status_page = NULL;
/* Write high address into HWS_PGA when disabling. */
I915_WRITE(HWS_PGA, 0x1ffff000);
}
int
i915_gem_init_ringbuffer(struct drm_i915_private *dev_priv)
{
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
int ret;
ret = i915_gem_init_hws(dev_priv);
if (ret != 0)
return ret;
obj = drm_gem_object_alloc(dev, 128 * 1024);
if (obj == NULL) {
DRM_ERROR("Failed to allocate ringbuffer\n");
ret = ENOMEM;
goto delhws;
}
obj_priv = (struct inteldrm_obj *)obj;
ret = i915_gem_object_pin(obj, 4096, 0);
if (ret != 0)
goto unref;
/* Set up the kernel mapping for the ring. */
dev_priv->ring.size = obj->size;
/* XXX WC */
if ((ret = bus_space_subregion(dev_priv->bst, dev_priv->aperture_bsh,
obj_priv->gtt_offset, obj->size, &dev_priv->ring.bsh)) != 0) {
DRM_INFO("can't map ringbuffer\n");
goto unpin;
}
dev_priv->ring.ring_obj = obj;
if ((ret = inteldrm_start_ring(dev_priv)) != 0)
goto unmap;
return (0);
unmap:
unpin:
memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
i915_gem_object_unpin(obj);
unref:
drm_gem_object_unreference(obj);
delhws:
i915_gem_cleanup_hws(dev_priv);
return (ret);
}
int
inteldrm_start_ring(struct drm_i915_private *dev_priv)
{
struct drm_obj *obj = dev_priv->ring.ring_obj;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
u_int32_t head;
/* Stop the ring if it's running. */
I915_WRITE(PRB0_CTL, 0);
I915_WRITE(PRB0_TAIL, 0);
I915_WRITE(PRB0_HEAD, 0);
/* Initialize the ring. */
I915_WRITE(PRB0_START, obj_priv->gtt_offset);
head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
/* G45 ring initialisation fails to reset head to zero */
if (head != 0) {
I915_WRITE(PRB0_HEAD, 0);
DRM_DEBUG("Forced ring head to zero ctl %08x head %08x"
"tail %08x start %08x\n", I915_READ(PRB0_CTL),
I915_READ(PRB0_HEAD), I915_READ(PRB0_TAIL),
I915_READ(PRB0_START));
}
I915_WRITE(PRB0_CTL, ((obj->size - 4096) & RING_NR_PAGES) |
RING_NO_REPORT | RING_VALID);
head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
/* If ring head still != 0, the ring is dead */
if (head != 0) {
DRM_ERROR("Ring initialisation failed: ctl %08x head %08x"
"tail %08x start %08x\n", I915_READ(PRB0_CTL),
I915_READ(PRB0_HEAD), I915_READ(PRB0_TAIL),
I915_READ(PRB0_START));
return (EIO);
}
/* Update our cache of the ring state */
inteldrm_update_ring(dev_priv);
return (0);
}
void
i915_gem_cleanup_ringbuffer(struct drm_i915_private *dev_priv)
{
if (dev_priv->ring.ring_obj == NULL)
return;
i915_gem_object_unpin(dev_priv->ring.ring_obj);
drm_gem_object_unreference(dev_priv->ring.ring_obj);
dev_priv->ring.ring_obj = NULL;
memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
i915_gem_cleanup_hws(dev_priv);
}
int
i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
drm_i915_private_t *dev_priv = dev->dev_private;
int ret;
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_ringbuffer(dev_priv);
if (ret != 0) {
DRM_UNLOCK();
return (ret);
}
/* gtt mapping means that the inactive list may not be empty */
KASSERT(TAILQ_EMPTY(&dev_priv->mm.active_list));
KASSERT(TAILQ_EMPTY(&dev_priv->mm.flushing_list));
KASSERT(TAILQ_EMPTY(&dev_priv->mm.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 drm_i915_private *dev_priv = dev->dev_private;
int ret;
ret = i915_gem_idle(dev_priv);
drm_irq_uninstall(dev);
return (ret);
}
void
inteldrm_timeout(void *arg)
{
drm_i915_private_t *dev_priv = arg;
if (workq_add_task(dev_priv->workq, 0, i915_gem_retire_work_handler,
dev_priv, NULL) == ENOMEM)
DRM_ERROR("failed to run retire handler\n");
}
/*
* handle hung hardware, or error interrupts. for now print debug info.
*/
void
inteldrm_error(struct drm_i915_private *dev_priv)
{
u_int32_t eir, ipeir, pgtbl_err, pipea_stats, pipeb_stats;
u_int8_t reset = GDRST_RENDER;
eir = I915_READ(EIR);
pipea_stats = I915_READ(PIPEASTAT);
pipeb_stats = I915_READ(PIPEBSTAT);
/*
* only actually check the error bits if we register one.
* else we just hung, stay silent.
*/
if (eir != 0) {
printf("render error detected, EIR: 0x%08x\n", eir);
if (IS_G4X(dev_priv)) {
if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
ipeir = I915_READ(IPEIR_I965);
printf(" IPEIR: 0x%08x\n",
I915_READ(IPEIR_I965));
printf(" IPEHR: 0x%08x\n",
I915_READ(IPEHR_I965));
printf(" INSTDONE: 0x%08x\n",
I915_READ(INSTDONE_I965));
printf(" INSTPS: 0x%08x\n",
I915_READ(INSTPS));
printf(" INSTDONE1: 0x%08x\n",
I915_READ(INSTDONE1));
printf(" ACTHD: 0x%08x\n",
I915_READ(ACTHD_I965));
I915_WRITE(IPEIR_I965, ipeir);
(void)I915_READ(IPEIR_I965);
}
if (eir & GM45_ERROR_PAGE_TABLE) {
pgtbl_err = I915_READ(PGTBL_ER);
printf("page table error\n");
printf(" PGTBL_ER: 0x%08x\n", pgtbl_err);
I915_WRITE(PGTBL_ER, pgtbl_err);
(void)I915_READ(PGTBL_ER);
dev_priv->mm.wedged = 1;
reset = GDRST_FULL;
}
} else if (IS_I9XX(dev_priv) && eir & I915_ERROR_PAGE_TABLE) {
pgtbl_err = I915_READ(PGTBL_ER);
printf("page table error\n");
printf(" PGTBL_ER: 0x%08x\n", pgtbl_err);
I915_WRITE(PGTBL_ER, pgtbl_err);
(void)I915_READ(PGTBL_ER);
dev_priv->mm.wedged = 1;
reset = GDRST_FULL;
}
if (eir & I915_ERROR_MEMORY_REFRESH) {
printf("memory refresh error\n");
printf("PIPEASTAT: 0x%08x\n",
pipea_stats);
printf("PIPEBSTAT: 0x%08x\n",
pipeb_stats);
/* pipestat has already been acked */
}
if (eir & I915_ERROR_INSTRUCTION) {
printf("instruction error\n");
printf(" INSTPM: 0x%08x\n",
I915_READ(INSTPM));
if (!IS_I965G(dev_priv)) {
ipeir = I915_READ(IPEIR);
printf(" IPEIR: 0x%08x\n",
I915_READ(IPEIR));
printf(" IPEHR: 0x%08x\n",
I915_READ(IPEHR));
printf(" INSTDONE: 0x%08x\n",
I915_READ(INSTDONE));
printf(" ACTHD: 0x%08x\n",
I915_READ(ACTHD));
I915_WRITE(IPEIR, ipeir);
(void)I915_READ(IPEIR);
} else {
ipeir = I915_READ(IPEIR_I965);
printf(" IPEIR: 0x%08x\n",
I915_READ(IPEIR_I965));
printf(" IPEHR: 0x%08x\n",
I915_READ(IPEHR_I965));
printf(" INSTDONE: 0x%08x\n",
I915_READ(INSTDONE_I965));
printf(" INSTPS: 0x%08x\n",
I915_READ(INSTPS));
printf(" INSTDONE1: 0x%08x\n",
I915_READ(INSTDONE1));
printf(" ACTHD: 0x%08x\n",
I915_READ(ACTHD_I965));
I915_WRITE(IPEIR_I965, ipeir);
(void)I915_READ(IPEIR_I965);
}
}
I915_WRITE(EIR, eir);
eir = I915_READ(EIR);
}
/*
* nasty errors don't clear and need a reset, mask them until we reset
* else we'll get infinite interrupt storms.
*/
if (eir) {
/* print so we know that we may want to reset here too */
if (dev_priv->mm.wedged == 0)
DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
I915_WRITE(EMR, I915_READ(EMR) | eir);
I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
}
/*
* if it was a pagetable error, or we were called from hangcheck, then
* reset the gpu.
*/
if (dev_priv->mm.wedged && workq_add_task(dev_priv->workq, 0,
inteldrm_hung, dev_priv, (void *)(uintptr_t)reset) == ENOMEM)
DRM_INFO("failed to schedule reset task\n");
}
void
inteldrm_hung(void *arg, void *reset_type)
{
struct drm_i915_private *dev_priv = arg;
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
struct inteldrm_obj *obj_priv;
u_int8_t reset = (u_int8_t)(uintptr_t)reset_type;
DRM_LOCK();
if (HAS_RESET(dev_priv)) {
DRM_INFO("resetting gpu: ");
inteldrm_965_reset(dev_priv, reset);
printf("done!\n");
} else
printf("no reset function for chipset.\n");
/*
* Clear out all of the requests and make everything inactive.
*/
i915_gem_retire_requests(dev_priv);
/*
* Clear the active and flushing lists to inactive. Since
* we've reset the hardware then they're not going to get
* flushed or completed otherwise. nuke the domains since
* they're now irrelavent.
*/
while ((obj_priv = TAILQ_FIRST(&dev_priv->mm.active_list)) != NULL) {
if (obj_priv->obj.write_domain & I915_GEM_GPU_DOMAINS) {
TAILQ_REMOVE(&dev_priv->mm.gpu_write_list,
obj_priv, write_list);
atomic_clearbits_int(&obj_priv->io_flags,
I915_GPU_WRITE);
obj_priv->obj.write_domain &= ~I915_GEM_GPU_DOMAINS;
}
i915_gem_object_move_to_inactive(&obj_priv->obj);;
}
while ((obj_priv = TAILQ_FIRST(&dev_priv->mm.flushing_list)) != NULL) {
if (obj_priv->obj.write_domain & I915_GEM_GPU_DOMAINS) {
TAILQ_REMOVE(&dev_priv->mm.gpu_write_list,
obj_priv, write_list);
atomic_clearbits_int(&obj_priv->io_flags,
I915_GPU_WRITE);
obj_priv->obj.write_domain &= ~I915_GEM_GPU_DOMAINS;
}
i915_gem_object_move_to_inactive(&obj_priv->obj);
}
/* unbind everything */
(void)i915_gem_evict_inactive(dev_priv);
if (HAS_RESET(dev_priv))
dev_priv->mm.wedged = 0;
DRM_UNLOCK();
}
void
inteldrm_hangcheck(void *arg)
{
struct drm_i915_private *dev_priv = arg;
u_int32_t acthd;
/* are we idle? */
if (TAILQ_EMPTY(&dev_priv->mm.request_list) ||
i915_seqno_passed(i915_get_gem_seqno(dev_priv),
TAILQ_LAST(&dev_priv->mm.request_list, i915_request)->seqno)) {
dev_priv->mm.hang_cnt = 0;
return;
}
if (IS_I965G(dev_priv))
acthd = I915_READ(ACTHD_I965);
else
acthd = I915_READ(ACTHD);
/* if we've hit ourselves before and the hardware hasn't moved, hung. */
if (dev_priv->mm.last_acthd == acthd) {
/* if that's twice we didn't hit it, then we're hung */
if (++dev_priv->mm.hang_cnt >= 2) {
dev_priv->mm.hang_cnt = 0;
/* XXX atomic */
dev_priv->mm.wedged = 1;
DRM_INFO("gpu hung!\n");
/* XXX locking */
wakeup(dev_priv);
inteldrm_error(dev_priv);
return;
}
} else {
dev_priv->mm.hang_cnt = 0;
}
dev_priv->mm.last_acthd = acthd;
/* Set ourselves up again, in case we haven't added another batch */
timeout_add_msec(&dev_priv->mm.hang_timer, 750);
}
void
i915_move_to_tail(struct inteldrm_obj *obj_priv, struct i915_gem_list *head)
{
i915_list_remove(obj_priv);
TAILQ_INSERT_TAIL(head, obj_priv, list);
obj_priv->current_list = head;
}
void
i915_list_remove(struct inteldrm_obj *obj_priv)
{
if (obj_priv->current_list != NULL)
TAILQ_REMOVE(obj_priv->current_list, obj_priv, list);
obj_priv->current_list = NULL;
}
/*
*
* Support for managing tiling state of buffer objects.
*
* The idea behind tiling is to increase cache hit rates by rearranging
* pixel data so that a group of pixel accesses are in the same cacheline.
* Performance improvement from doing this on the back/depth buffer are on
* the order of 30%.
*
* Intel architectures make this somewhat more complicated, though, by
* adjustments made to addressing of data when the memory is in interleaved
* mode (matched pairs of DIMMS) to improve memory bandwidth.
* For interleaved memory, the CPU sends every sequential 64 bytes
* to an alternate memory channel so it can get the bandwidth from both.
*
* The GPU also rearranges its accesses for increased bandwidth to interleaved
* memory, and it matches what the CPU does for non-tiled. However, when tiled
* it does it a little differently, since one walks addresses not just in the
* X direction but also Y. So, along with alternating channels when bit
* 6 of the address flips, it also alternates when other bits flip -- Bits 9
* (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
* are common to both the 915 and 965-class hardware.
*
* The CPU also sometimes XORs in higher bits as well, to improve
* bandwidth doing strided access like we do so frequently in graphics. This
* is called "Channel XOR Randomization" in the MCH documentation. The result
* is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
* decode.
*
* All of this bit 6 XORing has an effect on our memory management,
* as we need to make sure that the 3d driver can correctly address object
* contents.
*
* If we don't have interleaved memory, all tiling is safe and no swizzling is
* required.
*
* When bit 17 is XORed in, we simply refuse to tile at all. Bit
* 17 is not just a page offset, so as we page an object out and back in,
* individual pages in it will have different bit 17 addresses, resulting in
* each 64 bytes being swapped with its neighbor!
*
* Otherwise, if interleaved, we have to tell the 3d driver what the address
* swizzling it needs to do is, since it's writing with the CPU to the pages
* (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
* pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
* required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
* to match what the GPU expects.
*/
#define MCHBAR_I915 0x44
#define MCHBAR_I965 0x48
#define MCHBAR_SIZE (4*4096)
#define DEVEN_REG 0x54
#define DEVEN_MCHBAR_EN (1 << 28)
/*
* Check the MCHBAR on the host bridge is enabled, and if not allocate it.
* we do not need to actually map it because we access the bar through it's
* mirror on the IGD, however, if it is disabled or not allocated then
* the mirror does not work. *sigh*.
*
* we return a trinary state:
* 0 = already enabled, or can not enable
* 1 = enabled, needs disable
* 2 = enabled, needs disable and free.
*/
int
inteldrm_setup_mchbar(struct drm_i915_private *dev_priv,
struct pci_attach_args *bpa)
{
u_int64_t mchbar_addr;
pcireg_t tmp, low, high = 0;
u_long addr;
int reg = IS_I965G(dev_priv) ? MCHBAR_I965 : MCHBAR_I915;
int ret = 1, enabled = 0;
if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
tmp = pci_conf_read(bpa->pa_pc, bpa->pa_tag, DEVEN_REG);
enabled = !!(tmp & DEVEN_MCHBAR_EN);
} else {
tmp = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg);
enabled = tmp & 1;
}
if (enabled) {
return (0);
}
if (IS_I965G(dev_priv))
high = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg + 4);
low = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg);
mchbar_addr = ((u_int64_t)high << 32) | low;
/*
* XXX need to check to see if it's allocated in the pci resources,
* right now we just check to see if there's any address there
*
* if there's no address, then we allocate one.
* note that we can't just use pci_mapreg_map here since some intel
* BARs are special in that they set bit 0 to show they're enabled,
* this is not handled by generic pci code.
*/
if (mchbar_addr == 0) {
addr = (u_long)mchbar_addr;
if (bpa->pa_memex == NULL || extent_alloc(bpa->pa_memex,
MCHBAR_SIZE, MCHBAR_SIZE, 0, 0, 0, &addr)) {
return (0); /* just say we don't need to disable */
} else {
mchbar_addr = addr;
ret = 2;
/* We've allocated it, now fill in the BAR again */
if (IS_I965G(dev_priv))
pci_conf_write(bpa->pa_pc, bpa->pa_tag,
reg + 4, upper_32_bits(mchbar_addr));
pci_conf_write(bpa->pa_pc, bpa->pa_tag,
reg, mchbar_addr & 0xffffffff);
}
}
/* set the enable bit */
if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
pci_conf_write(bpa->pa_pc, bpa->pa_tag, DEVEN_REG,
tmp | DEVEN_MCHBAR_EN);
} else {
tmp = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg);
pci_conf_write(bpa->pa_pc, bpa->pa_tag, reg, tmp | 1);
}
return (ret);
}
/*
* we take the trinary returned from inteldrm_setup_mchbar and clean up after
* it.
*/
void
inteldrm_teardown_mchbar(struct drm_i915_private *dev_priv,
struct pci_attach_args *bpa, int disable)
{
u_int64_t mchbar_addr;
pcireg_t tmp, low, high = 0;
int reg = IS_I965G(dev_priv) ? MCHBAR_I965 : MCHBAR_I915;
switch(disable) {
case 2:
if (IS_I965G(dev_priv))
high = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg + 4);
low = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg);
mchbar_addr = ((u_int64_t)high << 32) | low;
extent_free(bpa->pa_memex, mchbar_addr, MCHBAR_SIZE, 0);
/* FALLTHROUGH */
case 1:
if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
tmp = pci_conf_read(bpa->pa_pc, bpa->pa_tag, DEVEN_REG);
tmp &= ~DEVEN_MCHBAR_EN;
pci_conf_write(bpa->pa_pc, bpa->pa_tag, DEVEN_REG, tmp);
} else {
tmp = pci_conf_read(bpa->pa_pc, bpa->pa_tag, reg);
tmp &= ~1;
pci_conf_write(bpa->pa_pc, bpa->pa_tag, reg, tmp);
}
break;
case 0:
default:
break;
};
}
/**
* Detects bit 6 swizzling of address lookup between IGD access and CPU
* access through main memory.
*/
void
inteldrm_detect_bit_6_swizzle(drm_i915_private_t *dev_priv,
struct pci_attach_args *bpa)
{
uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
int need_disable;
if (!IS_I9XX(dev_priv)) {
/* As far as we know, the 865 doesn't have these bit 6
* swizzling issues.
*/
swizzle_x = I915_BIT_6_SWIZZLE_NONE;
swizzle_y = I915_BIT_6_SWIZZLE_NONE;
} else if (IS_MOBILE(dev_priv)) {
uint32_t dcc;
/* try to enable MCHBAR, a lot of biosen disable it */
need_disable = inteldrm_setup_mchbar(dev_priv, bpa);
/* On 915-945 and GM965, channel interleave by the CPU is
* determined by DCC. The CPU will alternate based on bit 6
* in interleaved mode, and the GPU will then also alternate
* on bit 6, 9, and 10 for X, but the CPU may also optionally
* alternate based on bit 17 (XOR not disabled and XOR
* bit == 17).
*/
dcc = I915_READ(DCC);
switch (dcc & DCC_ADDRESSING_MODE_MASK) {
case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
swizzle_x = I915_BIT_6_SWIZZLE_NONE;
swizzle_y = I915_BIT_6_SWIZZLE_NONE;
break;
case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
if (dcc & DCC_CHANNEL_XOR_DISABLE) {
/* This is the base swizzling by the GPU for
* tiled buffers.
*/
swizzle_x = I915_BIT_6_SWIZZLE_9_10;
swizzle_y = I915_BIT_6_SWIZZLE_9;
} else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
/* Bit 11 swizzling by the CPU in addition. */
swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
swizzle_y = I915_BIT_6_SWIZZLE_9_11;
} else {
/* Bit 17 swizzling by the CPU in addition. */
swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
swizzle_y = I915_BIT_6_SWIZZLE_9_17;
}
break;
}
if (dcc == 0xffffffff) {
DRM_ERROR("Couldn't read from MCHBAR. "
"Disabling tiling.\n");
swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
}
inteldrm_teardown_mchbar(dev_priv, bpa, need_disable);
} else {
/* The 965, G33, and newer, have a very flexible memory
* configuration. It will enable dual-channel mode
* (interleaving) on as much memory as it can, and the GPU
* will additionally sometimes enable different bit 6
* swizzling for tiled objects from the CPU.
*
* Here's what I found on G965:
*
* slot fill memory size swizzling
* 0A 0B 1A 1B 1-ch 2-ch
* 512 0 0 0 512 0 O
* 512 0 512 0 16 1008 X
* 512 0 0 512 16 1008 X
* 0 512 0 512 16 1008 X
* 1024 1024 1024 0 2048 1024 O
*
* We could probably detect this based on either the DRB
* matching, which was the case for the swizzling required in
* the table above, or from the 1-ch value being less than
* the minimum size of a rank.
*/
if (I915_READ16(C0DRB3) != I915_READ16(C1DRB3)) {
swizzle_x = I915_BIT_6_SWIZZLE_NONE;
swizzle_y = I915_BIT_6_SWIZZLE_NONE;
} else {
swizzle_x = I915_BIT_6_SWIZZLE_9_10;
swizzle_y = I915_BIT_6_SWIZZLE_9;
}
}
dev_priv->mm.bit_6_swizzle_x = swizzle_x;
dev_priv->mm.bit_6_swizzle_y = swizzle_y;
}
int
inteldrm_swizzle_page(struct vm_page *pg)
{
vaddr_t va;
int i;
u_int8_t temp[64], *vaddr;
#if defined (__HAVE_PMAP_DIRECT)
va = pmap_map_direct(pg);
#else
va = uvm_km_valloc(kernel_map, PAGE_SIZE);
if (va == 0)
return (ENOMEM);
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg), UVM_PROT_RW);
pmap_update(pmap_kernel());
#endif
vaddr = (u_int8_t *)va;
for (i = 0; i < PAGE_SIZE; i += 128) {
memcpy(temp, &vaddr[i], 64);
memcpy(&vaddr[i], &vaddr[i + 64], 64);
memcpy(&vaddr[i + 64], temp, 64);
}
#if defined (__HAVE_PMAP_DIRECT)
pmap_unmap_direct(va);
#else
pmap_kremove(va, va + PAGE_SIZE);
pmap_update(pmap_kernel());
uvm_km_free(kernel_map, va, PAGE_SIZE);
#endif
return (0);
}
void
i915_gem_bit_17_swizzle(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
struct vm_page *pg;
bus_dma_segment_t *segp;
int page_count = obj->size >> PAGE_SHIFT;
int i, n, ret;
if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17 ||
obj_priv->bit_17 == NULL)
return;
while (i < page_count) {
/* compare bit 17 with previous one (in case we swapped).
* if they don't match we'll have to swizzle the page
*/
if ((((segp->ds_addr + n) >> 17) & 0x1) !=
test_bit(i, obj_priv->bit_17)) {
/* XXX move this to somewhere where we already have pg */
pg = PHYS_TO_VM_PAGE(segp->ds_addr + n * PAGE_SIZE);
KASSERT(pg != NULL);
ret = inteldrm_swizzle_page(pg);
if (ret)
return;
atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
}
if (++n * PAGE_SIZE > segp->ds_len) {
n = 0;
segp++;
}
}
}
void
i915_gem_save_bit_17_swizzle(struct drm_obj *obj)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
bus_dma_segment_t *segp;
int page_count = obj->size >> PAGE_SHIFT, i, n;
if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
return;
if (obj_priv->bit_17 == NULL) {
/* round up number of pages to a multiple of 32 so we know what
* size to make the bitmask. XXX this is wasteful with malloc
* and a better way should be done
*/
size_t nb17 = ((page_count + 31) & ~31)/32;
obj_priv->bit_17 = drm_alloc(nb17 * sizeof(u_int32_t));
if (obj_priv-> bit_17 == NULL) {
return;
}
}
segp = &obj_priv->dma_segs[0];
n = 0;
while (i < page_count) {
if ((segp->ds_addr + (n * PAGE_SIZE)) & (1 << 17))
set_bit(i, obj_priv->bit_17);
else
clear_bit(i, obj_priv->bit_17);
if (++n * PAGE_SIZE > segp->ds_len) {
n = 0;
segp++;
}
}
}
bus_size_t
i915_get_fence_size(struct drm_i915_private *dev_priv, bus_size_t size)
{
bus_size_t i, start;
if (IS_I965G(dev_priv)) {
/* 965 can have fences anywhere, so align to gpu-page size */
return ((size + (4096 - 1)) & ~(4096 - 1));
} else {
/*
* Align the size to a power of two greater than the smallest
* fence size.
*/
if (IS_I9XX(dev_priv))
start = 1024 * 1024;
else
start = 512 * 1024;
for (i = start; i < size; i <<= 1)
;
return (i);
}
}
int
i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
{
struct drm_i915_private *dev_priv = dev->dev_private;
int tile_width;
/* Linear is always ok */
if (tiling_mode == I915_TILING_NONE)
return (1);
if (!IS_I9XX(dev_priv) || (tiling_mode == I915_TILING_Y &&
HAS_128_BYTE_Y_TILING(dev_priv)))
tile_width = 128;
else
tile_width = 512;
/* Check stride and size constraints */
if (IS_I965G(dev_priv)) {
/* fence reg has end address, so size is ok */
if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
return (0);
} else if (IS_I9XX(dev_priv)) {
u_int32_t pitch_val = ffs(stride / tile_width) - 1;
/*
* XXX: for Y tiling, max pitch is actually 6 (8k) instead of 4
* (2k) on the 945.
*/
if (pitch_val > I915_FENCE_MAX_PITCH_VAL ||
size > (I830_FENCE_MAX_SIZE_VAL << 20))
return (0);
} else {
u_int32_t pitch_val = ffs(stride / tile_width) - 1;
if (pitch_val > I830_FENCE_MAX_PITCH_VAL ||
size > (I830_FENCE_MAX_SIZE_VAL << 19))
return (0);
}
/* 965+ just needs multiples of the tile width */
if (IS_I965G(dev_priv))
return ((stride & (tile_width - 1)) == 0);
/* Pre-965 needs power-of-two */
if (stride < tile_width || stride & (stride - 1) ||
i915_get_fence_size(dev_priv, size) != size)
return (0);
return (1);
}
int
i915_gem_object_fence_offset_ok(struct drm_obj *obj, int tiling_mode)
{
struct drm_device *dev = obj->dev;
struct drm_i915_private *dev_priv = dev->dev_private;
struct inteldrm_obj *obj_priv = (struct inteldrm_obj *)obj;
if (obj_priv->dmamap == NULL || tiling_mode == I915_TILING_NONE)
return (1);
if (!IS_I965G(dev_priv)) {
if (obj_priv->gtt_offset & (obj->size -1))
return (0);
if (IS_I9XX(dev_priv)) {
if (obj_priv->gtt_offset & ~I915_FENCE_START_MASK)
return (0);
} else {
if (obj_priv->gtt_offset & ~I830_FENCE_START_MASK)
return (0);
}
}
return (1);
}
/**
* Sets the tiling mode of an object, returning the required swizzling of
* bit 6 of addresses in the object.
*/
int
i915_gem_set_tiling(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_gem_set_tiling *args = data;
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
int ret = 0;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
obj_priv = (struct inteldrm_obj *)obj;
if (i915_tiling_ok(dev, args->stride, obj->size,
args->tiling_mode) == 0) {
ret = EINVAL;
DRM_LOCK(); /* for unref */
goto out;
}
DRM_LOCK();
if (args->tiling_mode == I915_TILING_NONE) {
args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
args->stride = 0;
} else {
if (args->tiling_mode == I915_TILING_X)
args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
else
args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
/* If we can't handle the swizzling, make it untiled. */
if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
args->tiling_mode = I915_TILING_NONE;
args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
args->stride = 0;
}
}
if (args->tiling_mode != obj_priv->tiling_mode ||
args->stride != obj_priv->stride) {
/*
* We need to rebind the object if its current allocation no
* longer meets the alignment restrictions for its new tiling
* mode. Otherwise we can leave it alone, but must clear any
* fence register.
*/
if (i915_gem_object_fence_offset_ok(obj, args->tiling_mode)) {
if (obj_priv->pin_count)
ret = EINVAL;
else
ret = i915_gem_object_unbind(obj, 1);
} else if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
ret = i915_gem_object_put_fence_reg(obj, 1);
} else {
inteldrm_wipe_mappings(obj);
}
if (ret != 0) {
args->tiling_mode = obj_priv->tiling_mode;
args->stride = obj_priv->stride;
goto out;
}
obj_priv->tiling_mode = args->tiling_mode;
obj_priv->stride = args->stride;
}
out:
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return (ret);
}
/**
* Returns the current tiling mode and required bit 6 swizzling for the object.
*/
int
i915_gem_get_tiling(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
struct drm_i915_gem_get_tiling *args = data;
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
if (obj == NULL)
return (EBADF);
obj_priv = (struct inteldrm_obj *)obj;
DRM_LOCK();
args->tiling_mode = obj_priv->tiling_mode;
switch (obj_priv->tiling_mode) {
case I915_TILING_X:
args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
break;
case I915_TILING_Y:
args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
break;
case I915_TILING_NONE:
args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
break;
default:
DRM_ERROR("unknown tiling mode\n");
}
drm_gem_object_unreference(obj);
DRM_UNLOCK();
return 0;
}
#endif /* INTELDRM_GEM */
/**
* inteldrm_pipe_enabled - check if a pipe is enabled
* @dev: DRM device
* @pipe: pipe to check
*
* Reading certain registers when the pipe is disabled can hang the chip.
* Use this routine to make sure the PLL is running and the pipe is active
* before reading such registers if unsure.
*/
int
inteldrm_pipe_enabled(struct drm_i915_private *dev_priv, int pipe)
{
bus_size_t pipeconf = pipe ? PIPEBCONF : PIPEACONF;
return ((I915_READ(pipeconf) & PIPEACONF_ENABLE) == PIPEACONF_ENABLE);
}
/*
* Register save/restore for various instances
*/
void
i915_save_palette(struct drm_i915_private *dev_priv, enum pipe pipe)
{
u_int32_t *array;
bus_size_t reg = (pipe == PIPE_A ? PALETTE_A : PALETTE_B);
int i;
if (!inteldrm_pipe_enabled(dev_priv, pipe))
return;
if (pipe == PIPE_A)
array = dev_priv->save_palette_a;
else
array = dev_priv->save_palette_b;
for (i = 0; i < 256; i++)
array[i] = I915_READ(reg + (i << 2));
}
void
i915_restore_palette(struct drm_i915_private *dev_priv, enum pipe pipe)
{
u_int32_t *array;
bus_size_t reg = (pipe == PIPE_A ? PALETTE_A : PALETTE_B);
int i;
if (!inteldrm_pipe_enabled(dev_priv, pipe))
return;
if (pipe == PIPE_A)
array = dev_priv->save_palette_a;
else
array = dev_priv->save_palette_b;
for(i = 0; i < 256; i++)
I915_WRITE(reg + (i << 2), array[i]);
}
u_int8_t
i915_read_ar(struct drm_i915_private *dev_priv, u_int16_t st01,
u_int8_t reg, u_int16_t palette_enable)
{
I915_READ8(st01);
I915_WRITE8(VGA_AR_INDEX, palette_enable | reg);
return I915_READ8(VGA_AR_DATA_READ);
}
void
i915_write_ar(struct drm_i915_private *dev_priv, u_int16_t st01, u_int8_t reg,
u_int8_t val, u_int16_t palette_enable)
{
I915_READ8(st01);
I915_WRITE8(VGA_AR_INDEX, palette_enable | reg);
I915_WRITE8(VGA_AR_DATA_WRITE, val);
}
u_int8_t
i915_read_indexed(struct drm_i915_private *dev_priv, u_int16_t index_port,
u_int16_t data_port, u_int8_t reg)
{
I915_WRITE8(index_port, reg);
return I915_READ8(data_port);
}
void
i915_write_indexed(struct drm_i915_private *dev_priv, u_int16_t index_port,
u_int16_t data_port, u_int8_t reg, u_int8_t val)
{
I915_WRITE8(index_port, reg);
I915_WRITE8(data_port, val);
}
void
i915_save_vga(struct drm_i915_private *dev_priv)
{
int i;
u16 cr_index, cr_data, st01;
/* VGA color palette registers */
dev_priv->saveDACMASK = I915_READ8(VGA_DACMASK);
/* MSR bits */
dev_priv->saveMSR = I915_READ8(VGA_MSR_READ);
if (dev_priv->saveMSR & VGA_MSR_CGA_MODE) {
cr_index = VGA_CR_INDEX_CGA;
cr_data = VGA_CR_DATA_CGA;
st01 = VGA_ST01_CGA;
} else {
cr_index = VGA_CR_INDEX_MDA;
cr_data = VGA_CR_DATA_MDA;
st01 = VGA_ST01_MDA;
}
/* CRT controller regs */
i915_write_indexed(dev_priv, cr_index, cr_data, 0x11,
i915_read_indexed(dev_priv, cr_index, cr_data, 0x11) & (~0x80));
for (i = 0; i <= 0x24; i++)
dev_priv->saveCR[i] = i915_read_indexed(dev_priv,
cr_index, cr_data, i);
/* Make sure we don't turn off CR group 0 writes */
dev_priv->saveCR[0x11] &= ~0x80;
/* Attribute controller registers */
I915_READ8(st01);
dev_priv->saveAR_INDEX = I915_READ8(VGA_AR_INDEX);
for (i = 0; i <= 0x14; i++)
dev_priv->saveAR[i] = i915_read_ar(dev_priv, st01, i, 0);
I915_READ8(st01);
I915_WRITE8(VGA_AR_INDEX, dev_priv->saveAR_INDEX);
I915_READ8(st01);
/* Graphics controller registers */
for (i = 0; i < 9; i++)
dev_priv->saveGR[i] = i915_read_indexed(dev_priv,
VGA_GR_INDEX, VGA_GR_DATA, i);
dev_priv->saveGR[0x10] = i915_read_indexed(dev_priv,
VGA_GR_INDEX, VGA_GR_DATA, 0x10);
dev_priv->saveGR[0x11] = i915_read_indexed(dev_priv,
VGA_GR_INDEX, VGA_GR_DATA, 0x11);
dev_priv->saveGR[0x18] = i915_read_indexed(dev_priv,
VGA_GR_INDEX, VGA_GR_DATA, 0x18);
/* Sequencer registers */
for (i = 0; i < 8; i++)
dev_priv->saveSR[i] = i915_read_indexed(dev_priv,
VGA_SR_INDEX, VGA_SR_DATA, i);
}
void
i915_restore_vga(struct drm_i915_private *dev_priv)
{
u_int16_t cr_index, cr_data, st01;
int i;
/* MSR bits */
I915_WRITE8(VGA_MSR_WRITE, dev_priv->saveMSR);
if (dev_priv->saveMSR & VGA_MSR_CGA_MODE) {
cr_index = VGA_CR_INDEX_CGA;
cr_data = VGA_CR_DATA_CGA;
st01 = VGA_ST01_CGA;
} else {
cr_index = VGA_CR_INDEX_MDA;
cr_data = VGA_CR_DATA_MDA;
st01 = VGA_ST01_MDA;
}
/* Sequencer registers, don't write SR07 */
for (i = 0; i < 7; i++)
i915_write_indexed(dev_priv, VGA_SR_INDEX, VGA_SR_DATA, i,
dev_priv->saveSR[i]);
/* CRT controller regs */
/* Enable CR group 0 writes */
i915_write_indexed(dev_priv, cr_index, cr_data, 0x11,
dev_priv->saveCR[0x11]);
for (i = 0; i <= 0x24; i++)
i915_write_indexed(dev_priv, cr_index, cr_data,
i, dev_priv->saveCR[i]);
/* Graphics controller regs */
for (i = 0; i < 9; i++)
i915_write_indexed(dev_priv, VGA_GR_INDEX, VGA_GR_DATA, i,
dev_priv->saveGR[i]);
i915_write_indexed(dev_priv, VGA_GR_INDEX, VGA_GR_DATA, 0x10,
dev_priv->saveGR[0x10]);
i915_write_indexed(dev_priv, VGA_GR_INDEX, VGA_GR_DATA, 0x11,
dev_priv->saveGR[0x11]);
i915_write_indexed(dev_priv, VGA_GR_INDEX, VGA_GR_DATA, 0x18,
dev_priv->saveGR[0x18]);
/* Attribute controller registers */
I915_READ8(st01); /* switch back to index mode */
for (i = 0; i <= 0x14; i++)
i915_write_ar(dev_priv, st01, i, dev_priv->saveAR[i], 0);
I915_READ8(st01); /* switch back to index mode */
I915_WRITE8(VGA_AR_INDEX, dev_priv->saveAR_INDEX | 0x20);
I915_READ8(st01);
/* VGA color palette registers */
I915_WRITE8(VGA_DACMASK, dev_priv->saveDACMASK);
}
void
i915_save_modeset_reg(struct drm_i915_private *dev_priv)
{
/* Pipe & plane A info */
dev_priv->savePIPEACONF = I915_READ(PIPEACONF);
dev_priv->savePIPEASRC = I915_READ(PIPEASRC);
dev_priv->saveFPA0 = I915_READ(FPA0);
dev_priv->saveFPA1 = I915_READ(FPA1);
dev_priv->saveDPLL_A = I915_READ(DPLL_A);
if (IS_I965G(dev_priv))
dev_priv->saveDPLL_A_MD = I915_READ(DPLL_A_MD);
dev_priv->saveHTOTAL_A = I915_READ(HTOTAL_A);
dev_priv->saveHBLANK_A = I915_READ(HBLANK_A);
dev_priv->saveHSYNC_A = I915_READ(HSYNC_A);
dev_priv->saveVTOTAL_A = I915_READ(VTOTAL_A);
dev_priv->saveVBLANK_A = I915_READ(VBLANK_A);
dev_priv->saveVSYNC_A = I915_READ(VSYNC_A);
dev_priv->saveBCLRPAT_A = I915_READ(BCLRPAT_A);
dev_priv->saveDSPACNTR = I915_READ(DSPACNTR);
dev_priv->saveDSPASTRIDE = I915_READ(DSPASTRIDE);
dev_priv->saveDSPASIZE = I915_READ(DSPASIZE);
dev_priv->saveDSPAPOS = I915_READ(DSPAPOS);
dev_priv->saveDSPAADDR = I915_READ(DSPAADDR);
if (IS_I965G(dev_priv)) {
dev_priv->saveDSPASURF = I915_READ(DSPASURF);
dev_priv->saveDSPATILEOFF = I915_READ(DSPATILEOFF);
}
i915_save_palette(dev_priv, PIPE_A);
dev_priv->savePIPEASTAT = I915_READ(PIPEASTAT);
/* Pipe & plane B info */
dev_priv->savePIPEBCONF = I915_READ(PIPEBCONF);
dev_priv->savePIPEBSRC = I915_READ(PIPEBSRC);
dev_priv->saveFPB0 = I915_READ(FPB0);
dev_priv->saveFPB1 = I915_READ(FPB1);
dev_priv->saveDPLL_B = I915_READ(DPLL_B);
if (IS_I965G(dev_priv))
dev_priv->saveDPLL_B_MD = I915_READ(DPLL_B_MD);
dev_priv->saveHTOTAL_B = I915_READ(HTOTAL_B);
dev_priv->saveHBLANK_B = I915_READ(HBLANK_B);
dev_priv->saveHSYNC_B = I915_READ(HSYNC_B);
dev_priv->saveVTOTAL_B = I915_READ(VTOTAL_B);
dev_priv->saveVBLANK_B = I915_READ(VBLANK_B);
dev_priv->saveVSYNC_B = I915_READ(VSYNC_B);
dev_priv->saveBCLRPAT_A = I915_READ(BCLRPAT_A);
dev_priv->saveDSPBCNTR = I915_READ(DSPBCNTR);
dev_priv->saveDSPBSTRIDE = I915_READ(DSPBSTRIDE);
dev_priv->saveDSPBSIZE = I915_READ(DSPBSIZE);
dev_priv->saveDSPBPOS = I915_READ(DSPBPOS);
dev_priv->saveDSPBADDR = I915_READ(DSPBADDR);
if (IS_I965GM(dev_priv) || IS_GM45(dev_priv)) {
dev_priv->saveDSPBSURF = I915_READ(DSPBSURF);
dev_priv->saveDSPBTILEOFF = I915_READ(DSPBTILEOFF);
}
i915_save_palette(dev_priv, PIPE_B);
dev_priv->savePIPEBSTAT = I915_READ(PIPEBSTAT);
}
void
i915_restore_modeset_reg(struct drm_i915_private *dev_priv)
{
/* Pipe & plane A info */
/* Prime the clock */
if (dev_priv->saveDPLL_A & DPLL_VCO_ENABLE) {
I915_WRITE(DPLL_A, dev_priv->saveDPLL_A &
~DPLL_VCO_ENABLE);
DRM_UDELAY(150);
}
I915_WRITE(FPA0, dev_priv->saveFPA0);
I915_WRITE(FPA1, dev_priv->saveFPA1);
/* Actually enable it */
I915_WRITE(DPLL_A, dev_priv->saveDPLL_A);
DRM_UDELAY(150);
if (IS_I965G(dev_priv))
I915_WRITE(DPLL_A_MD, dev_priv->saveDPLL_A_MD);
DRM_UDELAY(150);
/* Restore mode */
I915_WRITE(HTOTAL_A, dev_priv->saveHTOTAL_A);
I915_WRITE(HBLANK_A, dev_priv->saveHBLANK_A);
I915_WRITE(HSYNC_A, dev_priv->saveHSYNC_A);
I915_WRITE(VTOTAL_A, dev_priv->saveVTOTAL_A);
I915_WRITE(VBLANK_A, dev_priv->saveVBLANK_A);
I915_WRITE(VSYNC_A, dev_priv->saveVSYNC_A);
I915_WRITE(BCLRPAT_A, dev_priv->saveBCLRPAT_A);
/* Restore plane info */
I915_WRITE(DSPASIZE, dev_priv->saveDSPASIZE);
I915_WRITE(DSPAPOS, dev_priv->saveDSPAPOS);
I915_WRITE(PIPEASRC, dev_priv->savePIPEASRC);
I915_WRITE(DSPAADDR, dev_priv->saveDSPAADDR);
I915_WRITE(DSPASTRIDE, dev_priv->saveDSPASTRIDE);
if (IS_I965G(dev_priv)) {
I915_WRITE(DSPASURF, dev_priv->saveDSPASURF);
I915_WRITE(DSPATILEOFF, dev_priv->saveDSPATILEOFF);
}
I915_WRITE(PIPEACONF, dev_priv->savePIPEACONF);
i915_restore_palette(dev_priv, PIPE_A);
/* Enable the plane */
I915_WRITE(DSPACNTR, dev_priv->saveDSPACNTR);
I915_WRITE(DSPAADDR, I915_READ(DSPAADDR));
/* Pipe & plane B info */
if (dev_priv->saveDPLL_B & DPLL_VCO_ENABLE) {
I915_WRITE(DPLL_B, dev_priv->saveDPLL_B &
~DPLL_VCO_ENABLE);
DRM_UDELAY(150);
}
I915_WRITE(FPB0, dev_priv->saveFPB0);
I915_WRITE(FPB1, dev_priv->saveFPB1);
/* Actually enable it */
I915_WRITE(DPLL_B, dev_priv->saveDPLL_B);
DRM_UDELAY(150);
if (IS_I965G(dev_priv))
I915_WRITE(DPLL_B_MD, dev_priv->saveDPLL_B_MD);
DRM_UDELAY(150);
/* Restore mode */
I915_WRITE(HTOTAL_B, dev_priv->saveHTOTAL_B);
I915_WRITE(HBLANK_B, dev_priv->saveHBLANK_B);
I915_WRITE(HSYNC_B, dev_priv->saveHSYNC_B);
I915_WRITE(VTOTAL_B, dev_priv->saveVTOTAL_B);
I915_WRITE(VBLANK_B, dev_priv->saveVBLANK_B);
I915_WRITE(VSYNC_B, dev_priv->saveVSYNC_B);
I915_WRITE(BCLRPAT_B, dev_priv->saveBCLRPAT_B);
/* Restore plane info */
I915_WRITE(DSPBSIZE, dev_priv->saveDSPBSIZE);
I915_WRITE(DSPBPOS, dev_priv->saveDSPBPOS);
I915_WRITE(PIPEBSRC, dev_priv->savePIPEBSRC);
I915_WRITE(DSPBADDR, dev_priv->saveDSPBADDR);
I915_WRITE(DSPBSTRIDE, dev_priv->saveDSPBSTRIDE);
if (IS_I965G(dev_priv)) {
I915_WRITE(DSPBSURF, dev_priv->saveDSPBSURF);
I915_WRITE(DSPBTILEOFF, dev_priv->saveDSPBTILEOFF);
}
I915_WRITE(PIPEBCONF, dev_priv->savePIPEBCONF);
i915_restore_palette(dev_priv, PIPE_B);
/* Enable the plane */
I915_WRITE(DSPBCNTR, dev_priv->saveDSPBCNTR);
I915_WRITE(DSPBADDR, I915_READ(DSPBADDR));
}
int
inteldrm_save_display(struct drm_i915_private *dev_priv)
{
/* Display arbitration control */
dev_priv->saveDSPARB = I915_READ(DSPARB);
/* This is only meaningful in non-KMS mode */
/* Don't save them in KMS mode */
i915_save_modeset_reg(dev_priv);
/* Cursor state */
dev_priv->saveCURACNTR = I915_READ(CURACNTR);
dev_priv->saveCURAPOS = I915_READ(CURAPOS);
dev_priv->saveCURABASE = I915_READ(CURABASE);
dev_priv->saveCURBCNTR = I915_READ(CURBCNTR);
dev_priv->saveCURBPOS = I915_READ(CURBPOS);
dev_priv->saveCURBBASE = I915_READ(CURBBASE);
if (!IS_I9XX(dev_priv))
dev_priv->saveCURSIZE = I915_READ(CURSIZE);
/* CRT state */
dev_priv->saveADPA = I915_READ(ADPA);
/* LVDS state */
dev_priv->savePP_CONTROL = I915_READ(PP_CONTROL);
dev_priv->savePFIT_PGM_RATIOS = I915_READ(PFIT_PGM_RATIOS);
dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
if (IS_I965G(dev_priv))
dev_priv->saveBLC_PWM_CTL2 = I915_READ(BLC_PWM_CTL2);
if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
dev_priv->saveLVDS = I915_READ(LVDS);
if (!IS_I830(dev_priv) && !IS_845G(dev_priv))
dev_priv->savePFIT_CONTROL = I915_READ(PFIT_CONTROL);
dev_priv->savePP_ON_DELAYS = I915_READ(PP_ON_DELAYS);
dev_priv->savePP_OFF_DELAYS = I915_READ(PP_OFF_DELAYS);
dev_priv->savePP_DIVISOR = I915_READ(PP_DIVISOR);
/* FIXME: save TV & SDVO state */
/* FBC state */
dev_priv->saveFBC_CFB_BASE = I915_READ(FBC_CFB_BASE);
dev_priv->saveFBC_LL_BASE = I915_READ(FBC_LL_BASE);
dev_priv->saveFBC_CONTROL2 = I915_READ(FBC_CONTROL2);
dev_priv->saveFBC_CONTROL = I915_READ(FBC_CONTROL);
/* VGA state */
dev_priv->saveVGA0 = I915_READ(VGA0);
dev_priv->saveVGA1 = I915_READ(VGA1);
dev_priv->saveVGA_PD = I915_READ(VGA_PD);
dev_priv->saveVGACNTRL = I915_READ(VGACNTRL);
i915_save_vga(dev_priv);
return 0;
}
int
inteldrm_restore_display(struct drm_i915_private *dev_priv)
{
/* Display arbitration */
I915_WRITE(DSPARB, dev_priv->saveDSPARB);
/* This is only meaningful in non-KMS mode */
/* Don't restore them in KMS mode */
i915_restore_modeset_reg(dev_priv);
/* Cursor state */
I915_WRITE(CURAPOS, dev_priv->saveCURAPOS);
I915_WRITE(CURACNTR, dev_priv->saveCURACNTR);
I915_WRITE(CURABASE, dev_priv->saveCURABASE);
I915_WRITE(CURBPOS, dev_priv->saveCURBPOS);
I915_WRITE(CURBCNTR, dev_priv->saveCURBCNTR);
I915_WRITE(CURBBASE, dev_priv->saveCURBBASE);
if (!IS_I9XX(dev_priv))
I915_WRITE(CURSIZE, dev_priv->saveCURSIZE);
/* CRT state */
I915_WRITE(ADPA, dev_priv->saveADPA);
/* LVDS state */
if (IS_I965G(dev_priv))
I915_WRITE(BLC_PWM_CTL2, dev_priv->saveBLC_PWM_CTL2);
if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
I915_WRITE(LVDS, dev_priv->saveLVDS);
if (!IS_I830(dev_priv) && !IS_845G(dev_priv))
I915_WRITE(PFIT_CONTROL, dev_priv->savePFIT_CONTROL);
I915_WRITE(PFIT_PGM_RATIOS, dev_priv->savePFIT_PGM_RATIOS);
I915_WRITE(BLC_PWM_CTL, dev_priv->saveBLC_PWM_CTL);
I915_WRITE(PP_ON_DELAYS, dev_priv->savePP_ON_DELAYS);
I915_WRITE(PP_OFF_DELAYS, dev_priv->savePP_OFF_DELAYS);
I915_WRITE(PP_DIVISOR, dev_priv->savePP_DIVISOR);
I915_WRITE(PP_CONTROL, dev_priv->savePP_CONTROL);
/* FIXME: restore TV & SDVO state */
/* FBC info */
I915_WRITE(FBC_CFB_BASE, dev_priv->saveFBC_CFB_BASE);
I915_WRITE(FBC_LL_BASE, dev_priv->saveFBC_LL_BASE);
I915_WRITE(FBC_CONTROL2, dev_priv->saveFBC_CONTROL2);
I915_WRITE(FBC_CONTROL, dev_priv->saveFBC_CONTROL);
/* VGA state */
I915_WRITE(VGACNTRL, dev_priv->saveVGACNTRL);
I915_WRITE(VGA0, dev_priv->saveVGA0);
I915_WRITE(VGA1, dev_priv->saveVGA1);
I915_WRITE(VGA_PD, dev_priv->saveVGA_PD);
DRM_UDELAY(150);
i915_restore_vga(dev_priv);
return 0;
}
int
inteldrm_save_state(struct drm_i915_private *dev_priv)
{
int i;
dev_priv->saveLBB = pci_conf_read(dev_priv->pc, dev_priv->tag, LBB);
/* Render Standby */
if (IS_I965G(dev_priv) && IS_MOBILE(dev_priv))
dev_priv->saveRENDERSTANDBY = I915_READ(MCHBAR_RENDER_STANDBY);
/* Hardware status page */
dev_priv->saveHWS = I915_READ(HWS_PGA);
/* Display arbitration control */
dev_priv->saveDSPARB = I915_READ(DSPARB);
/* This is only meaningful in non-KMS mode */
/* Don't save them in KMS mode */
i915_save_modeset_reg(dev_priv);
/* Cursor state */
dev_priv->saveCURACNTR = I915_READ(CURACNTR);
dev_priv->saveCURAPOS = I915_READ(CURAPOS);
dev_priv->saveCURABASE = I915_READ(CURABASE);
dev_priv->saveCURBCNTR = I915_READ(CURBCNTR);
dev_priv->saveCURBPOS = I915_READ(CURBPOS);
dev_priv->saveCURBBASE = I915_READ(CURBBASE);
if (!IS_I9XX(dev_priv))
dev_priv->saveCURSIZE = I915_READ(CURSIZE);
/* CRT state */
dev_priv->saveADPA = I915_READ(ADPA);
/* LVDS state */
dev_priv->savePP_CONTROL = I915_READ(PP_CONTROL);
dev_priv->savePFIT_PGM_RATIOS = I915_READ(PFIT_PGM_RATIOS);
dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
if (IS_I965G(dev_priv))
dev_priv->saveBLC_PWM_CTL2 = I915_READ(BLC_PWM_CTL2);
if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
dev_priv->saveLVDS = I915_READ(LVDS);
if (!IS_I830(dev_priv) && !IS_845G(dev_priv))
dev_priv->savePFIT_CONTROL = I915_READ(PFIT_CONTROL);
dev_priv->savePP_ON_DELAYS = I915_READ(PP_ON_DELAYS);
dev_priv->savePP_OFF_DELAYS = I915_READ(PP_OFF_DELAYS);
dev_priv->savePP_DIVISOR = I915_READ(PP_DIVISOR);
/* XXX: displayport */
/* FIXME: save TV & SDVO state */
/* FBC state */
dev_priv->saveFBC_CFB_BASE = I915_READ(FBC_CFB_BASE);
dev_priv->saveFBC_LL_BASE = I915_READ(FBC_LL_BASE);
dev_priv->saveFBC_CONTROL2 = I915_READ(FBC_CONTROL2);
dev_priv->saveFBC_CONTROL = I915_READ(FBC_CONTROL);
/* Interrupt state */
dev_priv->saveIIR = I915_READ(IIR);
dev_priv->saveIER = I915_READ(IER);
dev_priv->saveIMR = I915_READ(IMR);
/* VGA state */
dev_priv->saveVGA0 = I915_READ(VGA0);
dev_priv->saveVGA1 = I915_READ(VGA1);
dev_priv->saveVGA_PD = I915_READ(VGA_PD);
dev_priv->saveVGACNTRL = I915_READ(VGACNTRL);
/* Clock gating state */
dev_priv->saveD_STATE = I915_READ(D_STATE);
dev_priv->saveDSPCLK_GATE_D = I915_READ(DSPCLK_GATE_D);
/* Cache mode state */
dev_priv->saveCACHE_MODE_0 = I915_READ(CACHE_MODE_0);
/* Memory Arbitration state */
dev_priv->saveMI_ARB_STATE = I915_READ(MI_ARB_STATE);
/* Scratch space */
for (i = 0; i < 16; i++) {
dev_priv->saveSWF0[i] = I915_READ(SWF00 + (i << 2));
dev_priv->saveSWF1[i] = I915_READ(SWF10 + (i << 2));
}
for (i = 0; i < 3; i++)
dev_priv->saveSWF2[i] = I915_READ(SWF30 + (i << 2));
/* Fences */
if (IS_I965G(dev_priv)) {
for (i = 0; i < 16; i++)
dev_priv->saveFENCE[i] = I915_READ64(FENCE_REG_965_0 +
(i * 8));
} else {
for (i = 0; i < 8; i++)
dev_priv->saveFENCE[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
IS_G33(dev_priv))
for (i = 0; i < 8; i++)
dev_priv->saveFENCE[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
}
i915_save_vga(dev_priv);
return 0;
}
int
inteldrm_restore_state(struct drm_i915_private *dev_priv)
{
int i;
pci_conf_write(dev_priv->pc, dev_priv->tag, LBB, dev_priv->saveLBB);
/* Render Standby */
if (IS_I965G(dev_priv) && IS_MOBILE(dev_priv))
I915_WRITE(MCHBAR_RENDER_STANDBY, dev_priv->saveRENDERSTANDBY);
/* Hardware status page */
I915_WRITE(HWS_PGA, dev_priv->saveHWS);
/* Display arbitration */
I915_WRITE(DSPARB, dev_priv->saveDSPARB);
/* Fences */
if (IS_I965G(dev_priv)) {
for (i = 0; i < 16; i++)
I915_WRITE64(FENCE_REG_965_0 + (i * 8), dev_priv->saveFENCE[i]);
} else {
for (i = 0; i < 8; i++)
I915_WRITE(FENCE_REG_830_0 + (i * 4), dev_priv->saveFENCE[i]);
if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
IS_G33(dev_priv))
for (i = 0; i < 8; i++)
I915_WRITE(FENCE_REG_945_8 + (i * 4), dev_priv->saveFENCE[i+8]);
}
/* This is only meaningful in non-KMS mode */
/* Don't restore them in KMS mode */
i915_restore_modeset_reg(dev_priv);
/* Cursor state */
I915_WRITE(CURAPOS, dev_priv->saveCURAPOS);
I915_WRITE(CURACNTR, dev_priv->saveCURACNTR);
I915_WRITE(CURABASE, dev_priv->saveCURABASE);
I915_WRITE(CURBPOS, dev_priv->saveCURBPOS);
I915_WRITE(CURBCNTR, dev_priv->saveCURBCNTR);
I915_WRITE(CURBBASE, dev_priv->saveCURBBASE);
if (!IS_I9XX(dev_priv))
I915_WRITE(CURSIZE, dev_priv->saveCURSIZE);
/* CRT state */
I915_WRITE(ADPA, dev_priv->saveADPA);
/* LVDS state */
if (IS_I965G(dev_priv))
I915_WRITE(BLC_PWM_CTL2, dev_priv->saveBLC_PWM_CTL2);
if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
I915_WRITE(LVDS, dev_priv->saveLVDS);
if (!IS_I830(dev_priv) && !IS_845G(dev_priv))
I915_WRITE(PFIT_CONTROL, dev_priv->savePFIT_CONTROL);
I915_WRITE(PFIT_PGM_RATIOS, dev_priv->savePFIT_PGM_RATIOS);
I915_WRITE(BLC_PWM_CTL, dev_priv->saveBLC_PWM_CTL);
I915_WRITE(PP_ON_DELAYS, dev_priv->savePP_ON_DELAYS);
I915_WRITE(PP_OFF_DELAYS, dev_priv->savePP_OFF_DELAYS);
I915_WRITE(PP_DIVISOR, dev_priv->savePP_DIVISOR);
I915_WRITE(PP_CONTROL, dev_priv->savePP_CONTROL);
/* XXX: Display Port state */
/* FIXME: restore TV & SDVO state */
/* FBC info */
I915_WRITE(FBC_CFB_BASE, dev_priv->saveFBC_CFB_BASE);
I915_WRITE(FBC_LL_BASE, dev_priv->saveFBC_LL_BASE);
I915_WRITE(FBC_CONTROL2, dev_priv->saveFBC_CONTROL2);
I915_WRITE(FBC_CONTROL, dev_priv->saveFBC_CONTROL);
/* VGA state */
I915_WRITE(VGACNTRL, dev_priv->saveVGACNTRL);
I915_WRITE(VGA0, dev_priv->saveVGA0);
I915_WRITE(VGA1, dev_priv->saveVGA1);
I915_WRITE(VGA_PD, dev_priv->saveVGA_PD);
DRM_UDELAY(150);
/* Clock gating state */
I915_WRITE (D_STATE, dev_priv->saveD_STATE);
I915_WRITE (DSPCLK_GATE_D, dev_priv->saveDSPCLK_GATE_D);
/* Cache mode state */
I915_WRITE (CACHE_MODE_0, dev_priv->saveCACHE_MODE_0 | 0xffff0000);
/* Memory arbitration state */
I915_WRITE (MI_ARB_STATE, dev_priv->saveMI_ARB_STATE | 0xffff0000);
for (i = 0; i < 16; i++) {
I915_WRITE(SWF00 + (i << 2), dev_priv->saveSWF0[i]);
I915_WRITE(SWF10 + (i << 2), dev_priv->saveSWF1[i]);
}
for (i = 0; i < 3; i++)
I915_WRITE(SWF30 + (i << 2), dev_priv->saveSWF2[i]);
i915_restore_vga(dev_priv);
return 0;
}
#ifdef INTELDRM_GEM
/*
* Reset the chip after a hang (965 only)
*
* The procedure that should be followed is relatively simple:
* - reset the chip using the reset reg
* - re-init context state
* - re-init Hardware status page
* - re-init ringbuffer
* - re-init interrupt state
* - re-init display
*/
void
inteldrm_965_reset(struct drm_i915_private *dev_priv, u_int8_t flags)
{
pcireg_t reg;
int i = 0;
if (flags == GDRST_FULL)
inteldrm_save_display(dev_priv);
reg = pci_conf_read(dev_priv->pc, dev_priv->tag, GDRST);
/*
* Set the domains we want to reset, then bit 0 (reset itself).
* then we wait for the hardware to clear it.
*/
pci_conf_write(dev_priv->pc, dev_priv->tag, GDRST,
reg | (u_int32_t)flags | ((flags == GDRST_FULL) ? 0x1 : 0x0));
delay(50);
/* don't clobber the rest of the register */
pci_conf_write(dev_priv->pc, dev_priv->tag, GDRST, reg & 0xfe);
/* if this fails we're pretty much fucked, but don't loop forever */
do {
delay(100);
reg = pci_conf_read(dev_priv->pc, dev_priv->tag, GDRST);
} while ((reg & 0x1) && ++i < 10);
if (reg & 0x1)
printf("bit 0 not cleared .. ");
/* put everything back together again */
/*
* GTT is already up (we didn't do a pci-level reset, thank god.
*
* We don't have to restore the contexts (we don't use them yet).
* So, if X is running we need to put the ringbuffer back first.
*/
if (dev_priv->mm.suspended == 0) {
struct drm_device *dev = (struct drm_device *)dev_priv->drmdev;
if (inteldrm_start_ring(dev_priv) != 0)
panic("can't restart ring, we're fucked");
/* put the hardware status page back */
if (I915_NEED_GFX_HWS(dev_priv))
I915_WRITE(HWS_PGA, ((struct inteldrm_obj *)
dev_priv->hws_obj)->gtt_offset);
else
I915_WRITE(HWS_PGA,
dev_priv->hws_dmamem->map->dm_segs[0].ds_addr);
I915_READ(HWS_PGA); /* posting read */
/* so we remove the handler and can put it back in */
DRM_UNLOCK();
drm_irq_uninstall(dev);
drm_irq_install(dev);
DRM_LOCK();
} else
printf("not restarting ring...\n");
if (flags == GDRST_FULL)
inteldrm_restore_display(dev_priv);
}
#endif /* INTELDRM_GEM */
/*
* Debug code from here.
*/
#ifdef WATCH_INACTIVE
void
inteldrm_verify_inactive(struct drm_i915_private *dev_priv, char *file,
int line)
{
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
TAILQ_FOREACH(obj_priv, &dev_priv->mm.inactive_list, list) {
obj = (struct drm_obj *)obj_priv;
if (obj_priv->pin_count || inteldrm_is_active(obj_priv) ||
obj->write_domain & I915_GEM_GPU_DOMAINS)
DRM_ERROR("inactive %p (p $d a $d w $x) %s:%d\n",
obj, obj_priv->pin_count,
inteldrm_is_active(obj_priv),
obj->write_domain, file, line);
}
}
#endif /* WATCH_INACTIVE */
#if (INTELDRM_DEBUG > 1)
static const char *get_pin_flag(struct inteldrm_obj *obj_priv)
{
if (obj_priv->pin_count > 0)
return "p";
else
return " ";
}
static const char *get_tiling_flag(struct inteldrm_obj *obj_priv)
{
switch (obj_priv->tiling_mode) {
default:
case I915_TILING_NONE: return " ";
case I915_TILING_X: return "X";
case I915_TILING_Y: return "Y";
}
}
void
i915_gem_seqno_info(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
if (dev_priv->hw_status_page != NULL) {
printf("Current sequence: %d\n", i915_get_gem_seqno(dev_priv));
} else {
printf("Current sequence: hws uninitialized\n");
}
}
void
i915_interrupt_info(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
printf("Interrupt enable: %08x\n",
I915_READ(IER));
printf("Interrupt identity: %08x\n",
I915_READ(IIR));
printf("Interrupt mask: %08x\n",
I915_READ(IMR));
printf("Pipe A stat: %08x\n",
I915_READ(PIPEASTAT));
printf("Pipe B stat: %08x\n",
I915_READ(PIPEBSTAT));
printf("Interrupts received: 0\n");
if (dev_priv->hw_status_page != NULL) {
printf("Current sequence: %d\n",
i915_get_gem_seqno(dev_priv));
} else {
printf("Current sequence: hws uninitialized\n");
}
}
void
i915_gem_fence_regs_info(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
int i;
printf("Reserved fences = %d\n", dev_priv->fence_reg_start);
printf("Total fences = %d\n", dev_priv->num_fence_regs);
for (i = 0; i < dev_priv->num_fence_regs; i++) {
struct drm_obj *obj = dev_priv->fence_regs[i].obj;
if (obj == NULL) {
printf("Fenced object[%2d] = unused\n", i);
} else {
struct inteldrm_obj *obj_priv;
obj_priv = (struct inteldrm_obj *)obj;
printf("Fenced object[%2d] = %p: %s "
"%08x %08zx %08x %s %08x %08x %d",
i, obj, get_pin_flag(obj_priv),
obj_priv->gtt_offset,
obj->size, obj_priv->stride,
get_tiling_flag(obj_priv),
obj->read_domains, obj->write_domain,
obj_priv->last_rendering_seqno);
if (obj->name)
printf(" (name: %d)", obj->name);
printf("\n");
}
}
}
void
i915_hws_info(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
int i;
volatile u32 *hws;
hws = (volatile u32 *)dev_priv->hw_status_page;
if (hws == NULL)
return;
for (i = 0; i < 4096 / sizeof(u32) / 4; i += 4) {
printf("0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n",
i * 4,
hws[i], hws[i + 1], hws[i + 2], hws[i + 3]);
}
}
static void
i915_dump_pages(bus_space_tag_t bst, bus_space_handle_t bsh,
bus_size_t size)
{
bus_addr_t offset = 0;
int i = 0;
/*
* this is a bit odd so i don't have to play with the intel
* tools too much.
*/
for (offset = 0; offset < size; offset += 4, i += 4) {
if (i == PAGE_SIZE)
i = 0;
printf("%08x : %08x\n", i, bus_space_read_4(bst, bsh,
offset));
}
}
void
i915_batchbuffer_info(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_obj *obj;
struct inteldrm_obj *obj_priv;
bus_space_handle_t bsh;
int ret;
TAILQ_FOREACH(obj_priv, &dev_priv->mm.active_list, list) {
obj = &obj_priv->obj;
if (obj->read_domains & I915_GEM_DOMAIN_COMMAND) {
if ((ret = bus_space_subregion(dev_priv->bst,
dev_priv->aperture_bsh, obj_priv->gtt_offset,
obj->size, &bsh)) != 0) {
DRM_ERROR("Failed to map pages: %d\n", ret);
return;
}
printf("--- gtt_offset = 0x%08x\n",
obj_priv->gtt_offset);
i915_dump_pages(dev_priv->bst, bsh, obj->size);
}
}
}
void
i915_ringbuffer_data(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
bus_size_t off;
if (!dev_priv->ring.ring_obj) {
printf("No ringbuffer setup\n");
return;
}
for (off = 0; off < dev_priv->ring.size; off += 4)
printf("%08x : %08x\n", off, bus_space_read_4(dev_priv->bst,
dev_priv->ring.bsh, off));
}
void
i915_ringbuffer_info(int kdev)
{
struct drm_device *dev = drm_get_device_from_kdev(kdev);
drm_i915_private_t *dev_priv = dev->dev_private;
u_int32_t head, tail;
head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
printf("RingHead : %08x\n", head);
printf("RingTail : %08x\n", tail);
printf("RingMask : %08x\n", dev_priv->ring.size - 1);
printf("RingSize : %08lx\n", dev_priv->ring.size);
printf("Acthd : %08x\n", I915_READ(IS_I965G(dev_priv) ?
ACTHD_I965 : ACTHD));
}
#endif
|