summaryrefslogtreecommitdiffstats
path: root/staging/yocto-emgd-emgd-1.14-driver.patch
blob: 34fa339206bdfd809b36fb4725540dca5c60d979 (plain)
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
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
28548
28549
28550
28551
28552
28553
28554
28555
28556
28557
28558
28559
28560
28561
28562
28563
28564
28565
28566
28567
28568
28569
28570
28571
28572
28573
28574
28575
28576
28577
28578
28579
28580
28581
28582
28583
28584
28585
28586
28587
28588
28589
28590
28591
28592
28593
28594
28595
28596
28597
28598
28599
28600
28601
28602
28603
28604
28605
28606
28607
28608
28609
28610
28611
28612
28613
28614
28615
28616
28617
28618
28619
28620
28621
28622
28623
28624
28625
28626
28627
28628
28629
28630
28631
28632
28633
28634
28635
28636
28637
28638
28639
28640
28641
28642
28643
28644
28645
28646
28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
28662
28663
28664
28665
28666
28667
28668
28669
28670
28671
28672
28673
28674
28675
28676
28677
28678
28679
28680
28681
28682
28683
28684
28685
28686
28687
28688
28689
28690
28691
28692
28693
28694
28695
28696
28697
28698
28699
28700
28701
28702
28703
28704
28705
28706
28707
28708
28709
28710
28711
28712
28713
28714
28715
28716
28717
28718
28719
28720
28721
28722
28723
28724
28725
28726
28727
28728
28729
28730
28731
28732
28733
28734
28735
28736
28737
28738
28739
28740
28741
28742
28743
28744
28745
28746
28747
28748
28749
28750
28751
28752
28753
28754
28755
28756
28757
28758
28759
28760
28761
28762
28763
28764
28765
28766
28767
28768
28769
28770
28771
28772
28773
28774
28775
28776
28777
28778
28779
28780
28781
28782
28783
28784
28785
28786
28787
28788
28789
28790
28791
28792
28793
28794
28795
28796
28797
28798
28799
28800
28801
28802
28803
28804
28805
28806
28807
28808
28809
28810
28811
28812
28813
28814
28815
28816
28817
28818
28819
28820
28821
28822
28823
28824
28825
28826
28827
28828
28829
28830
28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
28845
28846
28847
28848
28849
28850
28851
28852
28853
28854
28855
28856
28857
28858
28859
28860
28861
28862
28863
28864
28865
28866
28867
28868
28869
28870
28871
28872
28873
28874
28875
28876
28877
28878
28879
28880
28881
28882
28883
28884
28885
28886
28887
28888
28889
28890
28891
28892
28893
28894
28895
28896
28897
28898
28899
28900
28901
28902
28903
28904
28905
28906
28907
28908
28909
28910
28911
28912
28913
28914
28915
28916
28917
28918
28919
28920
28921
28922
28923
28924
28925
28926
28927
28928
28929
28930
28931
28932
28933
28934
28935
28936
28937
28938
28939
28940
28941
28942
28943
28944
28945
28946
28947
28948
28949
28950
28951
28952
28953
28954
28955
28956
28957
28958
28959
28960
28961
28962
28963
28964
28965
28966
28967
28968
28969
28970
28971
28972
28973
28974
28975
28976
28977
28978
28979
28980
28981
28982
28983
28984
28985
28986
28987
28988
28989
28990
28991
28992
28993
28994
28995
28996
28997
28998
28999
29000
29001
29002
29003
29004
29005
29006
29007
29008
29009
29010
29011
29012
29013
29014
29015
29016
29017
29018
29019
29020
29021
29022
29023
29024
29025
29026
29027
29028
29029
29030
29031
29032
29033
29034
29035
29036
29037
29038
29039
29040
29041
29042
29043
29044
29045
29046
29047
29048
29049
29050
29051
29052
29053
29054
29055
29056
29057
29058
29059
29060
29061
29062
29063
29064
29065
29066
29067
29068
29069
29070
29071
29072
29073
29074
29075
29076
29077
29078
29079
29080
29081
29082
29083
29084
29085
29086
29087
29088
29089
29090
29091
29092
29093
29094
29095
29096
29097
29098
29099
29100
29101
29102
29103
29104
29105
29106
29107
29108
29109
29110
29111
29112
29113
29114
29115
29116
29117
29118
29119
29120
29121
29122
29123
29124
29125
29126
29127
29128
29129
29130
29131
29132
29133
29134
29135
29136
29137
29138
29139
29140
29141
29142
29143
29144
29145
29146
29147
29148
29149
29150
29151
29152
29153
29154
29155
29156
29157
29158
29159
29160
29161
29162
29163
29164
29165
29166
29167
29168
29169
29170
29171
29172
29173
29174
29175
29176
29177
29178
29179
29180
29181
29182
29183
29184
29185
29186
29187
29188
29189
29190
29191
29192
29193
29194
29195
29196
29197
29198
29199
29200
29201
29202
29203
29204
29205
29206
29207
29208
29209
29210
29211
29212
29213
29214
29215
29216
29217
29218
29219
29220
29221
29222
29223
29224
29225
29226
29227
29228
29229
29230
29231
29232
29233
29234
29235
29236
29237
29238
29239
29240
29241
29242
29243
29244
29245
29246
29247
29248
29249
29250
29251
29252
29253
29254
29255
29256
29257
29258
29259
29260
29261
29262
29263
29264
29265
29266
29267
29268
29269
29270
29271
29272
29273
29274
29275
29276
29277
29278
29279
29280
29281
29282
29283
29284
29285
29286
29287
29288
29289
29290
29291
29292
29293
29294
29295
29296
29297
29298
29299
29300
29301
29302
29303
29304
29305
29306
29307
29308
29309
29310
29311
29312
29313
29314
29315
29316
29317
29318
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
29620
29621
29622
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
30420
30421
30422
30423
30424
30425
30426
30427
30428
30429
30430
30431
30432
30433
30434
30435
30436
30437
30438
30439
30440
30441
30442
30443
30444
30445
30446
30447
30448
30449
30450
30451
30452
30453
30454
30455
30456
30457
30458
30459
30460
30461
30462
30463
30464
30465
30466
30467
30468
30469
30470
30471
30472
30473
30474
30475
30476
30477
30478
30479
30480
30481
30482
30483
30484
30485
30486
30487
30488
30489
30490
30491
30492
30493
30494
30495
30496
30497
30498
30499
30500
30501
30502
30503
30504
30505
30506
30507
30508
30509
30510
30511
30512
30513
30514
30515
30516
30517
30518
30519
30520
30521
30522
30523
30524
30525
30526
30527
30528
30529
30530
30531
30532
30533
30534
30535
30536
30537
30538
30539
30540
30541
30542
30543
30544
30545
30546
30547
30548
30549
30550
30551
30552
30553
30554
30555
30556
30557
30558
30559
30560
30561
30562
30563
30564
30565
30566
30567
30568
30569
30570
30571
30572
30573
30574
30575
30576
30577
30578
30579
30580
30581
30582
30583
30584
30585
30586
30587
30588
30589
30590
30591
30592
30593
30594
30595
30596
30597
30598
30599
30600
30601
30602
30603
30604
30605
30606
30607
30608
30609
30610
30611
30612
30613
30614
30615
30616
30617
30618
30619
30620
30621
30622
30623
30624
30625
30626
30627
30628
30629
30630
30631
30632
30633
30634
30635
30636
30637
30638
30639
30640
30641
30642
30643
30644
30645
30646
30647
30648
30649
30650
30651
30652
30653
30654
30655
30656
30657
30658
30659
30660
30661
30662
30663
30664
30665
30666
30667
30668
30669
30670
30671
30672
30673
30674
30675
30676
30677
30678
30679
30680
30681
30682
30683
30684
30685
30686
30687
30688
30689
30690
30691
30692
30693
30694
30695
30696
30697
30698
30699
30700
30701
30702
30703
30704
30705
30706
30707
30708
30709
30710
30711
30712
30713
30714
30715
30716
30717
30718
30719
30720
30721
30722
30723
30724
30725
30726
30727
30728
30729
30730
30731
30732
30733
30734
30735
30736
30737
30738
30739
30740
30741
30742
30743
30744
30745
30746
30747
30748
30749
30750
30751
30752
30753
30754
30755
30756
30757
30758
30759
30760
30761
30762
30763
30764
30765
30766
30767
30768
30769
30770
30771
30772
30773
30774
30775
30776
30777
30778
30779
30780
30781
30782
30783
30784
30785
30786
30787
30788
30789
30790
30791
30792
30793
30794
30795
30796
30797
30798
30799
30800
30801
30802
30803
30804
30805
30806
30807
30808
30809
30810
30811
30812
30813
30814
30815
30816
30817
30818
30819
30820
30821
30822
30823
30824
30825
30826
30827
30828
30829
30830
30831
30832
30833
30834
30835
30836
30837
30838
30839
30840
30841
30842
30843
30844
30845
30846
30847
30848
30849
30850
30851
30852
30853
30854
30855
30856
30857
30858
30859
30860
30861
30862
30863
30864
30865
30866
30867
30868
30869
30870
30871
30872
30873
30874
30875
30876
30877
30878
30879
30880
30881
30882
30883
30884
30885
30886
30887
30888
30889
30890
30891
30892
30893
30894
30895
30896
30897
30898
30899
30900
30901
30902
30903
30904
30905
30906
30907
30908
30909
30910
30911
30912
30913
30914
30915
30916
30917
30918
30919
30920
30921
30922
30923
30924
30925
30926
30927
30928
30929
30930
30931
30932
30933
30934
30935
30936
30937
30938
30939
30940
30941
30942
30943
30944
30945
30946
30947
30948
30949
30950
30951
30952
30953
30954
30955
30956
30957
30958
30959
30960
30961
30962
30963
30964
30965
30966
30967
30968
30969
30970
30971
30972
30973
30974
30975
30976
30977
30978
30979
30980
30981
30982
30983
30984
30985
30986
30987
30988
30989
30990
30991
30992
30993
30994
30995
30996
30997
30998
30999
31000
31001
31002
31003
31004
31005
31006
31007
31008
31009
31010
31011
31012
31013
31014
31015
31016
31017
31018
31019
31020
31021
31022
31023
31024
31025
31026
31027
31028
31029
31030
31031
31032
31033
31034
31035
31036
31037
31038
31039
31040
31041
31042
31043
31044
31045
31046
31047
31048
31049
31050
31051
31052
31053
31054
31055
31056
31057
31058
31059
31060
31061
31062
31063
31064
31065
31066
31067
31068
31069
31070
31071
31072
31073
31074
31075
31076
31077
31078
31079
31080
31081
31082
31083
31084
31085
31086
31087
31088
31089
31090
31091
31092
31093
31094
31095
31096
31097
31098
31099
31100
31101
31102
31103
31104
31105
31106
31107
31108
31109
31110
31111
31112
31113
31114
31115
31116
31117
31118
31119
31120
31121
31122
31123
31124
31125
31126
31127
31128
31129
31130
31131
31132
31133
31134
31135
31136
31137
31138
31139
31140
31141
31142
31143
31144
31145
31146
31147
31148
31149
31150
31151
31152
31153
31154
31155
31156
31157
31158
31159
31160
31161
31162
31163
31164
31165
31166
31167
31168
31169
31170
31171
31172
31173
31174
31175
31176
31177
31178
31179
31180
31181
31182
31183
31184
31185
31186
31187
31188
31189
31190
31191
31192
31193
31194
31195
31196
31197
31198
31199
31200
31201
31202
31203
31204
31205
31206
31207
31208
31209
31210
31211
31212
31213
31214
31215
31216
31217
31218
31219
31220
31221
31222
31223
31224
31225
31226
31227
31228
31229
31230
31231
31232
31233
31234
31235
31236
31237
31238
31239
31240
31241
31242
31243
31244
31245
31246
31247
31248
31249
31250
31251
31252
31253
31254
31255
31256
31257
31258
31259
31260
31261
31262
31263
31264
31265
31266
31267
31268
31269
31270
31271
31272
31273
31274
31275
31276
31277
31278
31279
31280
31281
31282
31283
31284
31285
31286
31287
31288
31289
31290
31291
31292
31293
31294
31295
31296
31297
31298
31299
31300
31301
31302
31303
31304
31305
31306
31307
31308
31309
31310
31311
31312
31313
31314
31315
31316
31317
31318
31319
31320
31321
31322
31323
31324
31325
31326
31327
31328
31329
31330
31331
31332
31333
31334
31335
31336
31337
31338
31339
31340
31341
31342
31343
31344
31345
31346
31347
31348
31349
31350
31351
31352
31353
31354
31355
31356
31357
31358
31359
31360
31361
31362
31363
31364
31365
31366
31367
31368
31369
31370
31371
31372
31373
31374
31375
31376
31377
31378
31379
31380
31381
31382
31383
31384
31385
31386
31387
31388
31389
31390
31391
31392
31393
31394
31395
31396
31397
31398
31399
31400
31401
31402
31403
31404
31405
31406
31407
31408
31409
31410
31411
31412
31413
31414
31415
31416
31417
31418
31419
31420
31421
31422
31423
31424
31425
31426
31427
31428
31429
31430
31431
31432
31433
31434
31435
31436
31437
31438
31439
31440
31441
31442
31443
31444
31445
31446
31447
31448
31449
31450
31451
31452
31453
31454
31455
31456
31457
31458
31459
31460
31461
31462
31463
31464
31465
31466
31467
31468
31469
31470
31471
31472
31473
31474
31475
31476
31477
31478
31479
31480
31481
31482
31483
31484
31485
31486
31487
31488
31489
31490
31491
31492
31493
31494
31495
31496
31497
31498
31499
31500
31501
31502
31503
31504
31505
31506
31507
31508
31509
31510
31511
31512
31513
31514
31515
31516
31517
31518
31519
31520
31521
31522
31523
31524
31525
31526
31527
31528
31529
31530
31531
31532
31533
31534
31535
31536
31537
31538
31539
31540
31541
31542
31543
31544
31545
31546
31547
31548
31549
31550
31551
31552
31553
31554
31555
31556
31557
31558
31559
31560
31561
31562
31563
31564
31565
31566
31567
31568
31569
31570
31571
31572
31573
31574
31575
31576
31577
31578
31579
31580
31581
31582
31583
31584
31585
31586
31587
31588
31589
31590
31591
31592
31593
31594
31595
31596
31597
31598
31599
31600
31601
31602
31603
31604
31605
31606
31607
31608
31609
31610
31611
31612
31613
31614
31615
31616
31617
31618
31619
31620
31621
31622
31623
31624
31625
31626
31627
31628
31629
31630
31631
31632
31633
31634
31635
31636
31637
31638
31639
31640
31641
31642
31643
31644
31645
31646
31647
31648
31649
31650
31651
31652
31653
31654
31655
31656
31657
31658
31659
31660
31661
31662
31663
31664
31665
31666
31667
31668
31669
31670
31671
31672
31673
31674
31675
31676
31677
31678
31679
31680
31681
31682
31683
31684
31685
31686
31687
31688
31689
31690
31691
31692
31693
31694
31695
31696
31697
31698
31699
31700
31701
31702
31703
31704
31705
31706
31707
31708
31709
31710
31711
31712
31713
31714
31715
31716
31717
31718
31719
31720
31721
31722
31723
31724
31725
31726
31727
31728
31729
31730
31731
31732
31733
31734
31735
31736
31737
31738
31739
31740
31741
31742
31743
31744
31745
31746
31747
31748
31749
31750
31751
31752
31753
31754
31755
31756
31757
31758
31759
31760
31761
31762
31763
31764
31765
31766
31767
31768
31769
31770
31771
31772
31773
31774
31775
31776
31777
31778
31779
31780
31781
31782
31783
31784
31785
31786
31787
31788
31789
31790
31791
31792
31793
31794
31795
31796
31797
31798
31799
31800
31801
31802
31803
31804
31805
31806
31807
31808
31809
31810
31811
31812
31813
31814
31815
31816
31817
31818
31819
31820
31821
31822
31823
31824
31825
31826
31827
31828
31829
31830
31831
31832
31833
31834
31835
31836
31837
31838
31839
31840
31841
31842
31843
31844
31845
31846
31847
31848
31849
31850
31851
31852
31853
31854
31855
31856
31857
31858
31859
31860
31861
31862
31863
31864
31865
31866
31867
31868
31869
31870
31871
31872
31873
31874
31875
31876
31877
31878
31879
31880
31881
31882
31883
31884
31885
31886
31887
31888
31889
31890
31891
31892
31893
31894
31895
31896
31897
31898
31899
31900
31901
31902
31903
31904
31905
31906
31907
31908
31909
31910
31911
31912
31913
31914
31915
31916
31917
31918
31919
31920
31921
31922
31923
31924
31925
31926
31927
31928
31929
31930
31931
31932
31933
31934
31935
31936
31937
31938
31939
31940
31941
31942
31943
31944
31945
31946
31947
31948
31949
31950
31951
31952
31953
31954
31955
31956
31957
31958
31959
31960
31961
31962
31963
31964
31965
31966
31967
31968
31969
31970
31971
31972
31973
31974
31975
31976
31977
31978
31979
31980
31981
31982
31983
31984
31985
31986
31987
31988
31989
31990
31991
31992
31993
31994
31995
31996
31997
31998
31999
32000
32001
32002
32003
32004
32005
32006
32007
32008
32009
32010
32011
32012
32013
32014
32015
32016
32017
32018
32019
32020
32021
32022
32023
32024
32025
32026
32027
32028
32029
32030
32031
32032
32033
32034
32035
32036
32037
32038
32039
32040
32041
32042
32043
32044
32045
32046
32047
32048
32049
32050
32051
32052
32053
32054
32055
32056
32057
32058
32059
32060
32061
32062
32063
32064
32065
32066
32067
32068
32069
32070
32071
32072
32073
32074
32075
32076
32077
32078
32079
32080
32081
32082
32083
32084
32085
32086
32087
32088
32089
32090
32091
32092
32093
32094
32095
32096
32097
32098
32099
32100
32101
32102
32103
32104
32105
32106
32107
32108
32109
32110
32111
32112
32113
32114
32115
32116
32117
32118
32119
32120
32121
32122
32123
32124
32125
32126
32127
32128
32129
32130
32131
32132
32133
32134
32135
32136
32137
32138
32139
32140
32141
32142
32143
32144
32145
32146
32147
32148
32149
32150
32151
32152
32153
32154
32155
32156
32157
32158
32159
32160
32161
32162
32163
32164
32165
32166
32167
32168
32169
32170
32171
32172
32173
32174
32175
32176
32177
32178
32179
32180
32181
32182
32183
32184
32185
32186
32187
32188
32189
32190
32191
32192
32193
32194
32195
32196
32197
32198
32199
32200
32201
32202
32203
32204
32205
32206
32207
32208
32209
32210
32211
32212
32213
32214
32215
32216
32217
32218
32219
32220
32221
32222
32223
32224
32225
32226
32227
32228
32229
32230
32231
32232
32233
32234
32235
32236
32237
32238
32239
32240
32241
32242
32243
32244
32245
32246
32247
32248
32249
32250
32251
32252
32253
32254
32255
32256
32257
32258
32259
32260
32261
32262
32263
32264
32265
32266
32267
32268
32269
32270
32271
32272
32273
32274
32275
32276
32277
32278
32279
32280
32281
32282
32283
32284
32285
32286
32287
32288
32289
32290
32291
32292
32293
32294
32295
32296
32297
32298
32299
32300
32301
32302
32303
32304
32305
32306
32307
32308
32309
32310
32311
32312
32313
32314
32315
32316
32317
32318
32319
32320
32321
32322
32323
32324
32325
32326
32327
32328
32329
32330
32331
32332
32333
32334
32335
32336
32337
32338
32339
32340
32341
32342
32343
32344
32345
32346
32347
32348
32349
32350
32351
32352
32353
32354
32355
32356
32357
32358
32359
32360
32361
32362
32363
32364
32365
32366
32367
32368
32369
32370
32371
32372
32373
32374
32375
32376
32377
32378
32379
32380
32381
32382
32383
32384
32385
32386
32387
32388
32389
32390
32391
32392
32393
32394
32395
32396
32397
32398
32399
32400
32401
32402
32403
32404
32405
32406
32407
32408
32409
32410
32411
32412
32413
32414
32415
32416
32417
32418
32419
32420
32421
32422
32423
32424
32425
32426
32427
32428
32429
32430
32431
32432
32433
32434
32435
32436
32437
32438
32439
32440
32441
32442
32443
32444
32445
32446
32447
32448
32449
32450
32451
32452
32453
32454
32455
32456
32457
32458
32459
32460
32461
32462
32463
32464
32465
32466
32467
32468
32469
32470
32471
32472
32473
32474
32475
32476
32477
32478
32479
32480
32481
32482
32483
32484
32485
32486
32487
32488
32489
32490
32491
32492
32493
32494
32495
32496
32497
32498
32499
32500
32501
32502
32503
32504
32505
32506
32507
32508
32509
32510
32511
32512
32513
32514
32515
32516
32517
32518
32519
32520
32521
32522
32523
32524
32525
32526
32527
32528
32529
32530
32531
32532
32533
32534
32535
32536
32537
32538
32539
32540
32541
32542
32543
32544
32545
32546
32547
32548
32549
32550
32551
32552
32553
32554
32555
32556
32557
32558
32559
32560
32561
32562
32563
32564
32565
32566
32567
32568
32569
32570
32571
32572
32573
32574
32575
32576
32577
32578
32579
32580
32581
32582
32583
32584
32585
32586
32587
32588
32589
32590
32591
32592
32593
32594
32595
32596
32597
32598
32599
32600
32601
32602
32603
32604
32605
32606
32607
32608
32609
32610
32611
32612
32613
32614
32615
32616
32617
32618
32619
32620
32621
32622
32623
32624
32625
32626
32627
32628
32629
32630
32631
32632
32633
32634
32635
32636
32637
32638
32639
32640
32641
32642
32643
32644
32645
32646
32647
32648
32649
32650
32651
32652
32653
32654
32655
32656
32657
32658
32659
32660
32661
32662
32663
32664
32665
32666
32667
32668
32669
32670
32671
32672
32673
32674
32675
32676
32677
32678
32679
32680
32681
32682
32683
32684
32685
32686
32687
32688
32689
32690
32691
32692
32693
32694
32695
32696
32697
32698
32699
32700
32701
32702
32703
32704
32705
32706
32707
32708
32709
32710
32711
32712
32713
32714
32715
32716
32717
32718
32719
32720
32721
32722
32723
32724
32725
32726
32727
32728
32729
32730
32731
32732
32733
32734
32735
32736
32737
32738
32739
32740
32741
32742
32743
32744
32745
32746
32747
32748
32749
32750
32751
32752
32753
32754
32755
32756
32757
32758
32759
32760
32761
32762
32763
32764
32765
32766
32767
32768
32769
32770
32771
32772
32773
32774
32775
32776
32777
32778
32779
32780
32781
32782
32783
32784
32785
32786
32787
32788
32789
32790
32791
32792
32793
32794
32795
32796
32797
32798
32799
32800
32801
32802
32803
32804
32805
32806
32807
32808
32809
32810
32811
32812
32813
32814
32815
32816
32817
32818
32819
32820
32821
32822
32823
32824
32825
32826
32827
32828
32829
32830
32831
32832
32833
32834
32835
32836
32837
32838
32839
32840
32841
32842
32843
32844
32845
32846
32847
32848
32849
32850
32851
32852
32853
32854
32855
32856
32857
32858
32859
32860
32861
32862
32863
32864
32865
32866
32867
32868
32869
32870
32871
32872
32873
32874
32875
32876
32877
32878
32879
32880
32881
32882
32883
32884
32885
32886
32887
32888
32889
32890
32891
32892
32893
32894
32895
32896
32897
32898
32899
32900
32901
32902
32903
32904
32905
32906
32907
32908
32909
32910
32911
32912
32913
32914
32915
32916
32917
32918
32919
32920
32921
32922
32923
32924
32925
32926
32927
32928
32929
32930
32931
32932
32933
32934
32935
32936
32937
32938
32939
32940
32941
32942
32943
32944
32945
32946
32947
32948
32949
32950
32951
32952
32953
32954
32955
32956
32957
32958
32959
32960
32961
32962
32963
32964
32965
32966
32967
32968
32969
32970
32971
32972
32973
32974
32975
32976
32977
32978
32979
32980
32981
32982
32983
32984
32985
32986
32987
32988
32989
32990
32991
32992
32993
32994
32995
32996
32997
32998
32999
33000
33001
33002
33003
33004
33005
33006
33007
33008
33009
33010
33011
33012
33013
33014
33015
33016
33017
33018
33019
33020
33021
33022
33023
33024
33025
33026
33027
33028
33029
33030
33031
33032
33033
33034
33035
33036
33037
33038
33039
33040
33041
33042
33043
33044
33045
33046
33047
33048
33049
33050
33051
33052
33053
33054
33055
33056
33057
33058
33059
33060
33061
33062
33063
33064
33065
33066
33067
33068
33069
33070
33071
33072
33073
33074
33075
33076
33077
33078
33079
33080
33081
33082
33083
33084
33085
33086
33087
33088
33089
33090
33091
33092
33093
33094
33095
33096
33097
33098
33099
33100
33101
33102
33103
33104
33105
33106
33107
33108
33109
33110
33111
33112
33113
33114
33115
33116
33117
33118
33119
33120
33121
33122
33123
33124
33125
33126
33127
33128
33129
33130
33131
33132
33133
33134
33135
33136
33137
33138
33139
33140
33141
33142
33143
33144
33145
33146
33147
33148
33149
33150
33151
33152
33153
33154
33155
33156
33157
33158
33159
33160
33161
33162
33163
33164
33165
33166
33167
33168
33169
33170
33171
33172
33173
33174
33175
33176
33177
33178
33179
33180
33181
33182
33183
33184
33185
33186
33187
33188
33189
33190
33191
33192
33193
33194
33195
33196
33197
33198
33199
33200
33201
33202
33203
33204
33205
33206
33207
33208
33209
33210
33211
33212
33213
33214
33215
33216
33217
33218
33219
33220
33221
33222
33223
33224
33225
33226
33227
33228
33229
33230
33231
33232
33233
33234
33235
33236
33237
33238
33239
33240
33241
33242
33243
33244
33245
33246
33247
33248
33249
33250
33251
33252
33253
33254
33255
33256
33257
33258
33259
33260
33261
33262
33263
33264
33265
33266
33267
33268
33269
33270
33271
33272
33273
33274
33275
33276
33277
33278
33279
33280
33281
33282
33283
33284
33285
33286
33287
33288
33289
33290
33291
33292
33293
33294
33295
33296
33297
33298
33299
33300
33301
33302
33303
33304
33305
33306
33307
33308
33309
33310
33311
33312
33313
33314
33315
33316
33317
33318
33319
33320
33321
33322
33323
33324
33325
33326
33327
33328
33329
33330
33331
33332
33333
33334
33335
33336
33337
33338
33339
33340
33341
33342
33343
33344
33345
33346
33347
33348
33349
33350
33351
33352
33353
33354
33355
33356
33357
33358
33359
33360
33361
33362
33363
33364
33365
33366
33367
33368
33369
33370
33371
33372
33373
33374
33375
33376
33377
33378
33379
33380
33381
33382
33383
33384
33385
33386
33387
33388
33389
33390
33391
33392
33393
33394
33395
33396
33397
33398
33399
33400
33401
33402
33403
33404
33405
33406
33407
33408
33409
33410
33411
33412
33413
33414
33415
33416
33417
33418
33419
33420
33421
33422
33423
33424
33425
33426
33427
33428
33429
33430
33431
33432
33433
33434
33435
33436
33437
33438
33439
33440
33441
33442
33443
33444
33445
33446
33447
33448
33449
33450
33451
33452
33453
33454
33455
33456
33457
33458
33459
33460
33461
33462
33463
33464
33465
33466
33467
33468
33469
33470
33471
33472
33473
33474
33475
33476
33477
33478
33479
33480
33481
33482
33483
33484
33485
33486
33487
33488
33489
33490
33491
33492
33493
33494
33495
33496
33497
33498
33499
33500
33501
33502
33503
33504
33505
33506
33507
33508
33509
33510
33511
33512
33513
33514
33515
33516
33517
33518
33519
33520
33521
33522
33523
33524
33525
33526
33527
33528
33529
33530
33531
33532
33533
33534
33535
33536
33537
33538
33539
33540
33541
33542
33543
33544
33545
33546
33547
33548
33549
33550
33551
33552
33553
33554
33555
33556
33557
33558
33559
33560
33561
33562
33563
33564
33565
33566
33567
33568
33569
33570
33571
33572
33573
33574
33575
33576
33577
33578
33579
33580
33581
33582
33583
33584
33585
33586
33587
33588
33589
33590
33591
33592
33593
33594
33595
33596
33597
33598
33599
33600
33601
33602
33603
33604
33605
33606
33607
33608
33609
33610
33611
33612
33613
33614
33615
33616
33617
33618
33619
33620
33621
33622
33623
33624
33625
33626
33627
33628
33629
33630
33631
33632
33633
33634
33635
33636
33637
33638
33639
33640
33641
33642
33643
33644
33645
33646
33647
33648
33649
33650
33651
33652
33653
33654
33655
33656
33657
33658
33659
33660
33661
33662
33663
33664
33665
33666
33667
33668
33669
33670
33671
33672
33673
33674
33675
33676
33677
33678
33679
33680
33681
33682
33683
33684
33685
33686
33687
33688
33689
33690
33691
33692
33693
33694
33695
33696
33697
33698
33699
33700
33701
33702
33703
33704
33705
33706
33707
33708
33709
33710
33711
33712
33713
33714
33715
33716
33717
33718
33719
33720
33721
33722
33723
33724
33725
33726
33727
33728
33729
33730
33731
33732
33733
33734
33735
33736
33737
33738
33739
33740
33741
33742
33743
33744
33745
33746
33747
33748
33749
33750
33751
33752
33753
33754
33755
33756
33757
33758
33759
33760
33761
33762
33763
33764
33765
33766
33767
33768
33769
33770
33771
33772
33773
33774
33775
33776
33777
33778
33779
33780
33781
33782
33783
33784
33785
33786
33787
33788
33789
33790
33791
33792
33793
33794
33795
33796
33797
33798
33799
33800
33801
33802
33803
33804
33805
33806
33807
33808
33809
33810
33811
33812
33813
33814
33815
33816
33817
33818
33819
33820
33821
33822
33823
33824
33825
33826
33827
33828
33829
33830
33831
33832
33833
33834
33835
33836
33837
33838
33839
33840
33841
33842
33843
33844
33845
33846
33847
33848
33849
33850
33851
33852
33853
33854
33855
33856
33857
33858
33859
33860
33861
33862
33863
33864
33865
33866
33867
33868
33869
33870
33871
33872
33873
33874
33875
33876
33877
33878
33879
33880
33881
33882
33883
33884
33885
33886
33887
33888
33889
33890
33891
33892
33893
33894
33895
33896
33897
33898
33899
33900
33901
33902
33903
33904
33905
33906
33907
33908
33909
33910
33911
33912
33913
33914
33915
33916
33917
33918
33919
33920
33921
33922
33923
33924
33925
33926
33927
33928
33929
33930
33931
33932
33933
33934
33935
33936
33937
33938
33939
33940
33941
33942
33943
33944
33945
33946
33947
33948
33949
33950
33951
33952
33953
33954
33955
33956
33957
33958
33959
33960
33961
33962
33963
33964
33965
33966
33967
33968
33969
33970
33971
33972
33973
33974
33975
33976
33977
33978
33979
33980
33981
33982
33983
33984
33985
33986
33987
33988
33989
33990
33991
33992
33993
33994
33995
33996
33997
33998
33999
34000
34001
34002
34003
34004
34005
34006
34007
34008
34009
34010
34011
34012
34013
34014
34015
34016
34017
34018
34019
34020
34021
34022
34023
34024
34025
34026
34027
34028
34029
34030
34031
34032
34033
34034
34035
34036
34037
34038
34039
34040
34041
34042
34043
34044
34045
34046
34047
34048
34049
34050
34051
34052
34053
34054
34055
34056
34057
34058
34059
34060
34061
34062
34063
34064
34065
34066
34067
34068
34069
34070
34071
34072
34073
34074
34075
34076
34077
34078
34079
34080
34081
34082
34083
34084
34085
34086
34087
34088
34089
34090
34091
34092
34093
34094
34095
34096
34097
34098
34099
34100
34101
34102
34103
34104
34105
34106
34107
34108
34109
34110
34111
34112
34113
34114
34115
34116
34117
34118
34119
34120
34121
34122
34123
34124
34125
34126
34127
34128
34129
34130
34131
34132
34133
34134
34135
34136
34137
34138
34139
34140
34141
34142
34143
34144
34145
34146
34147
34148
34149
34150
34151
34152
34153
34154
34155
34156
34157
34158
34159
34160
34161
34162
34163
34164
34165
34166
34167
34168
34169
34170
34171
34172
34173
34174
34175
34176
34177
34178
34179
34180
34181
34182
34183
34184
34185
34186
34187
34188
34189
34190
34191
34192
34193
34194
34195
34196
34197
34198
34199
34200
34201
34202
34203
34204
34205
34206
34207
34208
34209
34210
34211
34212
34213
34214
34215
34216
34217
34218
34219
34220
34221
34222
34223
34224
34225
34226
34227
34228
34229
34230
34231
34232
34233
34234
34235
34236
34237
34238
34239
34240
34241
34242
34243
34244
34245
34246
34247
34248
34249
34250
34251
34252
34253
34254
34255
34256
34257
34258
34259
34260
34261
34262
34263
34264
34265
34266
34267
34268
34269
34270
34271
34272
34273
34274
34275
34276
34277
34278
34279
34280
34281
34282
34283
34284
34285
34286
34287
34288
34289
34290
34291
34292
34293
34294
34295
34296
34297
34298
34299
34300
34301
34302
34303
34304
34305
34306
34307
34308
34309
34310
34311
34312
34313
34314
34315
34316
34317
34318
34319
34320
34321
34322
34323
34324
34325
34326
34327
34328
34329
34330
34331
34332
34333
34334
34335
34336
34337
34338
34339
34340
34341
34342
34343
34344
34345
34346
34347
34348
34349
34350
34351
34352
34353
34354
34355
34356
34357
34358
34359
34360
34361
34362
34363
34364
34365
34366
34367
34368
34369
34370
34371
34372
34373
34374
34375
34376
34377
34378
34379
34380
34381
34382
34383
34384
34385
34386
34387
34388
34389
34390
34391
34392
34393
34394
34395
34396
34397
34398
34399
34400
34401
34402
34403
34404
34405
34406
34407
34408
34409
34410
34411
34412
34413
34414
34415
34416
34417
34418
34419
34420
34421
34422
34423
34424
34425
34426
34427
34428
34429
34430
34431
34432
34433
34434
34435
34436
34437
34438
34439
34440
34441
34442
34443
34444
34445
34446
34447
34448
34449
34450
34451
34452
34453
34454
34455
34456
34457
34458
34459
34460
34461
34462
34463
34464
34465
34466
34467
34468
34469
34470
34471
34472
34473
34474
34475
34476
34477
34478
34479
34480
34481
34482
34483
34484
34485
34486
34487
34488
34489
34490
34491
34492
34493
34494
34495
34496
34497
34498
34499
34500
34501
34502
34503
34504
34505
34506
34507
34508
34509
34510
34511
34512
34513
34514
34515
34516
34517
34518
34519
34520
34521
34522
34523
34524
34525
34526
34527
34528
34529
34530
34531
34532
34533
34534
34535
34536
34537
34538
34539
34540
34541
34542
34543
34544
34545
34546
34547
34548
34549
34550
34551
34552
34553
34554
34555
34556
34557
34558
34559
34560
34561
34562
34563
34564
34565
34566
34567
34568
34569
34570
34571
34572
34573
34574
34575
34576
34577
34578
34579
34580
34581
34582
34583
34584
34585
34586
34587
34588
34589
34590
34591
34592
34593
34594
34595
34596
34597
34598
34599
34600
34601
34602
34603
34604
34605
34606
34607
34608
34609
34610
34611
34612
34613
34614
34615
34616
34617
34618
34619
34620
34621
34622
34623
34624
34625
34626
34627
34628
34629
34630
34631
34632
34633
34634
34635
34636
34637
34638
34639
34640
34641
34642
34643
34644
34645
34646
34647
34648
34649
34650
34651
34652
34653
34654
34655
34656
34657
34658
34659
34660
34661
34662
34663
34664
34665
34666
34667
34668
34669
34670
34671
34672
34673
34674
34675
34676
34677
34678
34679
34680
34681
34682
34683
34684
34685
34686
34687
34688
34689
34690
34691
34692
34693
34694
34695
34696
34697
34698
34699
34700
34701
34702
34703
34704
34705
34706
34707
34708
34709
34710
34711
34712
34713
34714
34715
34716
34717
34718
34719
34720
34721
34722
34723
34724
34725
34726
34727
34728
34729
34730
34731
34732
34733
34734
34735
34736
34737
34738
34739
34740
34741
34742
34743
34744
34745
34746
34747
34748
34749
34750
34751
34752
34753
34754
34755
34756
34757
34758
34759
34760
34761
34762
34763
34764
34765
34766
34767
34768
34769
34770
34771
34772
34773
34774
34775
34776
34777
34778
34779
34780
34781
34782
34783
34784
34785
34786
34787
34788
34789
34790
34791
34792
34793
34794
34795
34796
34797
34798
34799
34800
34801
34802
34803
34804
34805
34806
34807
34808
34809
34810
34811
34812
34813
34814
34815
34816
34817
34818
34819
34820
34821
34822
34823
34824
34825
34826
34827
34828
34829
34830
34831
34832
34833
34834
34835
34836
34837
34838
34839
34840
34841
34842
34843
34844
34845
34846
34847
34848
34849
34850
34851
34852
34853
34854
34855
34856
34857
34858
34859
34860
34861
34862
34863
34864
34865
34866
34867
34868
34869
34870
34871
34872
34873
34874
34875
34876
34877
34878
34879
34880
34881
34882
34883
34884
34885
34886
34887
34888
34889
34890
34891
34892
34893
34894
34895
34896
34897
34898
34899
34900
34901
34902
34903
34904
34905
34906
34907
34908
34909
34910
34911
34912
34913
34914
34915
34916
34917
34918
34919
34920
34921
34922
34923
34924
34925
34926
34927
34928
34929
34930
34931
34932
34933
34934
34935
34936
34937
34938
34939
34940
34941
34942
34943
34944
34945
34946
34947
34948
34949
34950
34951
34952
34953
34954
34955
34956
34957
34958
34959
34960
34961
34962
34963
34964
34965
34966
34967
34968
34969
34970
34971
34972
34973
34974
34975
34976
34977
34978
34979
34980
34981
34982
34983
34984
34985
34986
34987
34988
34989
34990
34991
34992
34993
34994
34995
34996
34997
34998
34999
35000
35001
35002
35003
35004
35005
35006
35007
35008
35009
35010
35011
35012
35013
35014
35015
35016
35017
35018
35019
35020
35021
35022
35023
35024
35025
35026
35027
35028
35029
35030
35031
35032
35033
35034
35035
35036
35037
35038
35039
35040
35041
35042
35043
35044
35045
35046
35047
35048
35049
35050
35051
35052
35053
35054
35055
35056
35057
35058
35059
35060
35061
35062
35063
35064
35065
35066
35067
35068
35069
35070
35071
35072
35073
35074
35075
35076
35077
35078
35079
35080
35081
35082
35083
35084
35085
35086
35087
35088
35089
35090
35091
35092
35093
35094
35095
35096
35097
35098
35099
35100
35101
35102
35103
35104
35105
35106
35107
35108
35109
35110
35111
35112
35113
35114
35115
35116
35117
35118
35119
35120
35121
35122
35123
35124
35125
35126
35127
35128
35129
35130
35131
35132
35133
35134
35135
35136
35137
35138
35139
35140
35141
35142
35143
35144
35145
35146
35147
35148
35149
35150
35151
35152
35153
35154
35155
35156
35157
35158
35159
35160
35161
35162
35163
35164
35165
35166
35167
35168
35169
35170
35171
35172
35173
35174
35175
35176
35177
35178
35179
35180
35181
35182
35183
35184
35185
35186
35187
35188
35189
35190
35191
35192
35193
35194
35195
35196
35197
35198
35199
35200
35201
35202
35203
35204
35205
35206
35207
35208
35209
35210
35211
35212
35213
35214
35215
35216
35217
35218
35219
35220
35221
35222
35223
35224
35225
35226
35227
35228
35229
35230
35231
35232
35233
35234
35235
35236
35237
35238
35239
35240
35241
35242
35243
35244
35245
35246
35247
35248
35249
35250
35251
35252
35253
35254
35255
35256
35257
35258
35259
35260
35261
35262
35263
35264
35265
35266
35267
35268
35269
35270
35271
35272
35273
35274
35275
35276
35277
35278
35279
35280
35281
35282
35283
35284
35285
35286
35287
35288
35289
35290
35291
35292
35293
35294
35295
35296
35297
35298
35299
35300
35301
35302
35303
35304
35305
35306
35307
35308
35309
35310
35311
35312
35313
35314
35315
35316
35317
35318
35319
35320
35321
35322
35323
35324
35325
35326
35327
35328
35329
35330
35331
35332
35333
35334
35335
35336
35337
35338
35339
35340
35341
35342
35343
35344
35345
35346
35347
35348
35349
35350
35351
35352
35353
35354
35355
35356
35357
35358
35359
35360
35361
35362
35363
35364
35365
35366
35367
35368
35369
35370
35371
35372
35373
35374
35375
35376
35377
35378
35379
35380
35381
35382
35383
35384
35385
35386
35387
35388
35389
35390
35391
35392
35393
35394
35395
35396
35397
35398
35399
35400
35401
35402
35403
35404
35405
35406
35407
35408
35409
35410
35411
35412
35413
35414
35415
35416
35417
35418
35419
35420
35421
35422
35423
35424
35425
35426
35427
35428
35429
35430
35431
35432
35433
35434
35435
35436
35437
35438
35439
35440
35441
35442
35443
35444
35445
35446
35447
35448
35449
35450
35451
35452
35453
35454
35455
35456
35457
35458
35459
35460
35461
35462
35463
35464
35465
35466
35467
35468
35469
35470
35471
35472
35473
35474
35475
35476
35477
35478
35479
35480
35481
35482
35483
35484
35485
35486
35487
35488
35489
35490
35491
35492
35493
35494
35495
35496
35497
35498
35499
35500
35501
35502
35503
35504
35505
35506
35507
35508
35509
35510
35511
35512
35513
35514
35515
35516
35517
35518
35519
35520
35521
35522
35523
35524
35525
35526
35527
35528
35529
35530
35531
35532
35533
35534
35535
35536
35537
35538
35539
35540
35541
35542
35543
35544
35545
35546
35547
35548
35549
35550
35551
35552
35553
35554
35555
35556
35557
35558
35559
35560
35561
35562
35563
35564
35565
35566
35567
35568
35569
35570
35571
35572
35573
35574
35575
35576
35577
35578
35579
35580
35581
35582
35583
35584
35585
35586
35587
35588
35589
35590
35591
35592
35593
35594
35595
35596
35597
35598
35599
35600
35601
35602
35603
35604
35605
35606
35607
35608
35609
35610
35611
35612
35613
35614
35615
35616
35617
35618
35619
35620
35621
35622
35623
35624
35625
35626
35627
35628
35629
35630
35631
35632
35633
35634
35635
35636
35637
35638
35639
35640
35641
35642
35643
35644
35645
35646
35647
35648
35649
35650
35651
35652
35653
35654
35655
35656
35657
35658
35659
35660
35661
35662
35663
35664
35665
35666
35667
35668
35669
35670
35671
35672
35673
35674
35675
35676
35677
35678
35679
35680
35681
35682
35683
35684
35685
35686
35687
35688
35689
35690
35691
35692
35693
35694
35695
35696
35697
35698
35699
35700
35701
35702
35703
35704
35705
35706
35707
35708
35709
35710
35711
35712
35713
35714
35715
35716
35717
35718
35719
35720
35721
35722
35723
35724
35725
35726
35727
35728
35729
35730
35731
35732
35733
35734
35735
35736
35737
35738
35739
35740
35741
35742
35743
35744
35745
35746
35747
35748
35749
35750
35751
35752
35753
35754
35755
35756
35757
35758
35759
35760
35761
35762
35763
35764
35765
35766
35767
35768
35769
35770
35771
35772
35773
35774
35775
35776
35777
35778
35779
35780
35781
35782
35783
35784
35785
35786
35787
35788
35789
35790
35791
35792
35793
35794
35795
35796
35797
35798
35799
35800
35801
35802
35803
35804
35805
35806
35807
35808
35809
35810
35811
35812
35813
35814
35815
35816
35817
35818
35819
35820
35821
35822
35823
35824
35825
35826
35827
35828
35829
35830
35831
35832
35833
35834
35835
35836
35837
35838
35839
35840
35841
35842
35843
35844
35845
35846
35847
35848
35849
35850
35851
35852
35853
35854
35855
35856
35857
35858
35859
35860
35861
35862
35863
35864
35865
35866
35867
35868
35869
35870
35871
35872
35873
35874
35875
35876
35877
35878
35879
35880
35881
35882
35883
35884
35885
35886
35887
35888
35889
35890
35891
35892
35893
35894
35895
35896
35897
35898
35899
35900
35901
35902
35903
35904
35905
35906
35907
35908
35909
35910
35911
35912
35913
35914
35915
35916
35917
35918
35919
35920
35921
35922
35923
35924
35925
35926
35927
35928
35929
35930
35931
35932
35933
35934
35935
35936
35937
35938
35939
35940
35941
35942
35943
35944
35945
35946
35947
35948
35949
35950
35951
35952
35953
35954
35955
35956
35957
35958
35959
35960
35961
35962
35963
35964
35965
35966
35967
35968
35969
35970
35971
35972
35973
35974
35975
35976
35977
35978
35979
35980
35981
35982
35983
35984
35985
35986
35987
35988
35989
35990
35991
35992
35993
35994
35995
35996
35997
35998
35999
36000
36001
36002
36003
36004
36005
36006
36007
36008
36009
36010
36011
36012
36013
36014
36015
36016
36017
36018
36019
36020
36021
36022
36023
36024
36025
36026
36027
36028
36029
36030
36031
36032
36033
36034
36035
36036
36037
36038
36039
36040
36041
36042
36043
36044
36045
36046
36047
36048
36049
36050
36051
36052
36053
36054
36055
36056
36057
36058
36059
36060
36061
36062
36063
36064
36065
36066
36067
36068
36069
36070
36071
36072
36073
36074
36075
36076
36077
36078
36079
36080
36081
36082
36083
36084
36085
36086
36087
36088
36089
36090
36091
36092
36093
36094
36095
36096
36097
36098
36099
36100
36101
36102
36103
36104
36105
36106
36107
36108
36109
36110
36111
36112
36113
36114
36115
36116
36117
36118
36119
36120
36121
36122
36123
36124
36125
36126
36127
36128
36129
36130
36131
36132
36133
36134
36135
36136
36137
36138
36139
36140
36141
36142
36143
36144
36145
36146
36147
36148
36149
36150
36151
36152
36153
36154
36155
36156
36157
36158
36159
36160
36161
36162
36163
36164
36165
36166
36167
36168
36169
36170
36171
36172
36173
36174
36175
36176
36177
36178
36179
36180
36181
36182
36183
36184
36185
36186
36187
36188
36189
36190
36191
36192
36193
36194
36195
36196
36197
36198
36199
36200
36201
36202
36203
36204
36205
36206
36207
36208
36209
36210
36211
36212
36213
36214
36215
36216
36217
36218
36219
36220
36221
36222
36223
36224
36225
36226
36227
36228
36229
36230
36231
36232
36233
36234
36235
36236
36237
36238
36239
36240
36241
36242
36243
36244
36245
36246
36247
36248
36249
36250
36251
36252
36253
36254
36255
36256
36257
36258
36259
36260
36261
36262
36263
36264
36265
36266
36267
36268
36269
36270
36271
36272
36273
36274
36275
36276
36277
36278
36279
36280
36281
36282
36283
36284
36285
36286
36287
36288
36289
36290
36291
36292
36293
36294
36295
36296
36297
36298
36299
36300
36301
36302
36303
36304
36305
36306
36307
36308
36309
36310
36311
36312
36313
36314
36315
36316
36317
36318
36319
36320
36321
36322
36323
36324
36325
36326
36327
36328
36329
36330
36331
36332
36333
36334
36335
36336
36337
36338
36339
36340
36341
36342
36343
36344
36345
36346
36347
36348
36349
36350
36351
36352
36353
36354
36355
36356
36357
36358
36359
36360
36361
36362
36363
36364
36365
36366
36367
36368
36369
36370
36371
36372
36373
36374
36375
36376
36377
36378
36379
36380
36381
36382
36383
36384
36385
36386
36387
36388
36389
36390
36391
36392
36393
36394
36395
36396
36397
36398
36399
36400
36401
36402
36403
36404
36405
36406
36407
36408
36409
36410
36411
36412
36413
36414
36415
36416
36417
36418
36419
36420
36421
36422
36423
36424
36425
36426
36427
36428
36429
36430
36431
36432
36433
36434
36435
36436
36437
36438
36439
36440
36441
36442
36443
36444
36445
36446
36447
36448
36449
36450
36451
36452
36453
36454
36455
36456
36457
36458
36459
36460
36461
36462
36463
36464
36465
36466
36467
36468
36469
36470
36471
36472
36473
36474
36475
36476
36477
36478
36479
36480
36481
36482
36483
36484
36485
36486
36487
36488
36489
36490
36491
36492
36493
36494
36495
36496
36497
36498
36499
36500
36501
36502
36503
36504
36505
36506
36507
36508
36509
36510
36511
36512
36513
36514
36515
36516
36517
36518
36519
36520
36521
36522
36523
36524
36525
36526
36527
36528
36529
36530
36531
36532
36533
36534
36535
36536
36537
36538
36539
36540
36541
36542
36543
36544
36545
36546
36547
36548
36549
36550
36551
36552
36553
36554
36555
36556
36557
36558
36559
36560
36561
36562
36563
36564
36565
36566
36567
36568
36569
36570
36571
36572
36573
36574
36575
36576
36577
36578
36579
36580
36581
36582
36583
36584
36585
36586
36587
36588
36589
36590
36591
36592
36593
36594
36595
36596
36597
36598
36599
36600
36601
36602
36603
36604
36605
36606
36607
36608
36609
36610
36611
36612
36613
36614
36615
36616
36617
36618
36619
36620
36621
36622
36623
36624
36625
36626
36627
36628
36629
36630
36631
36632
36633
36634
36635
36636
36637
36638
36639
36640
36641
36642
36643
36644
36645
36646
36647
36648
36649
36650
36651
36652
36653
36654
36655
36656
36657
36658
36659
36660
36661
36662
36663
36664
36665
36666
36667
36668
36669
36670
36671
36672
36673
36674
36675
36676
36677
36678
36679
36680
36681
36682
36683
36684
36685
36686
36687
36688
36689
36690
36691
36692
36693
36694
36695
36696
36697
36698
36699
36700
36701
36702
36703
36704
36705
36706
36707
36708
36709
36710
36711
36712
36713
36714
36715
36716
36717
36718
36719
36720
36721
36722
36723
36724
36725
36726
36727
36728
36729
36730
36731
36732
36733
36734
36735
36736
36737
36738
36739
36740
36741
36742
36743
36744
36745
36746
36747
36748
36749
36750
36751
36752
36753
36754
36755
36756
36757
36758
36759
36760
36761
36762
36763
36764
36765
36766
36767
36768
36769
36770
36771
36772
36773
36774
36775
36776
36777
36778
36779
36780
36781
36782
36783
36784
36785
36786
36787
36788
36789
36790
36791
36792
36793
36794
36795
36796
36797
36798
36799
36800
36801
36802
36803
36804
36805
36806
36807
36808
36809
36810
36811
36812
36813
36814
36815
36816
36817
36818
36819
36820
36821
36822
36823
36824
36825
36826
36827
36828
36829
36830
36831
36832
36833
36834
36835
36836
36837
36838
36839
36840
36841
36842
36843
36844
36845
36846
36847
36848
36849
36850
36851
36852
36853
36854
36855
36856
36857
36858
36859
36860
36861
36862
36863
36864
36865
36866
36867
36868
36869
36870
36871
36872
36873
36874
36875
36876
36877
36878
36879
36880
36881
36882
36883
36884
36885
36886
36887
36888
36889
36890
36891
36892
36893
36894
36895
36896
36897
36898
36899
36900
36901
36902
36903
36904
36905
36906
36907
36908
36909
36910
36911
36912
36913
36914
36915
36916
36917
36918
36919
36920
36921
36922
36923
36924
36925
36926
36927
36928
36929
36930
36931
36932
36933
36934
36935
36936
36937
36938
36939
36940
36941
36942
36943
36944
36945
36946
36947
36948
36949
36950
36951
36952
36953
36954
36955
36956
36957
36958
36959
36960
36961
36962
36963
36964
36965
36966
36967
36968
36969
36970
36971
36972
36973
36974
36975
36976
36977
36978
36979
36980
36981
36982
36983
36984
36985
36986
36987
36988
36989
36990
36991
36992
36993
36994
36995
36996
36997
36998
36999
37000
37001
37002
37003
37004
37005
37006
37007
37008
37009
37010
37011
37012
37013
37014
37015
37016
37017
37018
37019
37020
37021
37022
37023
37024
37025
37026
37027
37028
37029
37030
37031
37032
37033
37034
37035
37036
37037
37038
37039
37040
37041
37042
37043
37044
37045
37046
37047
37048
37049
37050
37051
37052
37053
37054
37055
37056
37057
37058
37059
37060
37061
37062
37063
37064
37065
37066
37067
37068
37069
37070
37071
37072
37073
37074
37075
37076
37077
37078
37079
37080
37081
37082
37083
37084
37085
37086
37087
37088
37089
37090
37091
37092
37093
37094
37095
37096
37097
37098
37099
37100
37101
37102
37103
37104
37105
37106
37107
37108
37109
37110
37111
37112
37113
37114
37115
37116
37117
37118
37119
37120
37121
37122
37123
37124
37125
37126
37127
37128
37129
37130
37131
37132
37133
37134
37135
37136
37137
37138
37139
37140
37141
37142
37143
37144
37145
37146
37147
37148
37149
37150
37151
37152
37153
37154
37155
37156
37157
37158
37159
37160
37161
37162
37163
37164
37165
37166
37167
37168
37169
37170
37171
37172
37173
37174
37175
37176
37177
37178
37179
37180
37181
37182
37183
37184
37185
37186
37187
37188
37189
37190
37191
37192
37193
37194
37195
37196
37197
37198
37199
37200
37201
37202
37203
37204
37205
37206
37207
37208
37209
37210
37211
37212
37213
37214
37215
37216
37217
37218
37219
37220
37221
37222
37223
37224
37225
37226
37227
37228
37229
37230
37231
37232
37233
37234
37235
37236
37237
37238
37239
37240
37241
37242
37243
37244
37245
37246
37247
37248
37249
37250
37251
37252
37253
37254
37255
37256
37257
37258
37259
37260
37261
37262
37263
37264
37265
37266
37267
37268
37269
37270
37271
37272
37273
37274
37275
37276
37277
37278
37279
37280
37281
37282
37283
37284
37285
37286
37287
37288
37289
37290
37291
37292
37293
37294
37295
37296
37297
37298
37299
37300
37301
37302
37303
37304
37305
37306
37307
37308
37309
37310
37311
37312
37313
37314
37315
37316
37317
37318
37319
37320
37321
37322
37323
37324
37325
37326
37327
37328
37329
37330
37331
37332
37333
37334
37335
37336
37337
37338
37339
37340
37341
37342
37343
37344
37345
37346
37347
37348
37349
37350
37351
37352
37353
37354
37355
37356
37357
37358
37359
37360
37361
37362
37363
37364
37365
37366
37367
37368
37369
37370
37371
37372
37373
37374
37375
37376
37377
37378
37379
37380
37381
37382
37383
37384
37385
37386
37387
37388
37389
37390
37391
37392
37393
37394
37395
37396
37397
37398
37399
37400
37401
37402
37403
37404
37405
37406
37407
37408
37409
37410
37411
37412
37413
37414
37415
37416
37417
37418
37419
37420
37421
37422
37423
37424
37425
37426
37427
37428
37429
37430
37431
37432
37433
37434
37435
37436
37437
37438
37439
37440
37441
37442
37443
37444
37445
37446
37447
37448
37449
37450
37451
37452
37453
37454
37455
37456
37457
37458
37459
37460
37461
37462
37463
37464
37465
37466
37467
37468
37469
37470
37471
37472
37473
37474
37475
37476
37477
37478
37479
37480
37481
37482
37483
37484
37485
37486
37487
37488
37489
37490
37491
37492
37493
37494
37495
37496
37497
37498
37499
37500
37501
37502
37503
37504
37505
37506
37507
37508
37509
37510
37511
37512
37513
37514
37515
37516
37517
37518
37519
37520
37521
37522
37523
37524
37525
37526
37527
37528
37529
37530
37531
37532
37533
37534
37535
37536
37537
37538
37539
37540
37541
37542
37543
37544
37545
37546
37547
37548
37549
37550
37551
37552
37553
37554
37555
37556
37557
37558
37559
37560
37561
37562
37563
37564
37565
37566
37567
37568
37569
37570
37571
37572
37573
37574
37575
37576
37577
37578
37579
37580
37581
37582
37583
37584
37585
37586
37587
37588
37589
37590
37591
37592
37593
37594
37595
37596
37597
37598
37599
37600
37601
37602
37603
37604
37605
37606
37607
37608
37609
37610
37611
37612
37613
37614
37615
37616
37617
37618
37619
37620
37621
37622
37623
37624
37625
37626
37627
37628
37629
37630
37631
37632
37633
37634
37635
37636
37637
37638
37639
37640
37641
37642
37643
37644
37645
37646
37647
37648
37649
37650
37651
37652
37653
37654
37655
37656
37657
37658
37659
37660
37661
37662
37663
37664
37665
37666
37667
37668
37669
37670
37671
37672
37673
37674
37675
37676
37677
37678
37679
37680
37681
37682
37683
37684
37685
37686
37687
37688
37689
37690
37691
37692
37693
37694
37695
37696
37697
37698
37699
37700
37701
37702
37703
37704
37705
37706
37707
37708
37709
37710
37711
37712
37713
37714
37715
37716
37717
37718
37719
37720
37721
37722
37723
37724
37725
37726
37727
37728
37729
37730
37731
37732
37733
37734
37735
37736
37737
37738
37739
37740
37741
37742
37743
37744
37745
37746
37747
37748
37749
37750
37751
37752
37753
37754
37755
37756
37757
37758
37759
37760
37761
37762
37763
37764
37765
37766
37767
37768
37769
37770
37771
37772
37773
37774
37775
37776
37777
37778
37779
37780
37781
37782
37783
37784
37785
37786
37787
37788
37789
37790
37791
37792
37793
37794
37795
37796
37797
37798
37799
37800
37801
37802
37803
37804
37805
37806
37807
37808
37809
37810
37811
37812
37813
37814
37815
37816
37817
37818
37819
37820
37821
37822
37823
37824
37825
37826
37827
37828
37829
37830
37831
37832
37833
37834
37835
37836
37837
37838
37839
37840
37841
37842
37843
37844
37845
37846
37847
37848
37849
37850
37851
37852
37853
37854
37855
37856
37857
37858
37859
37860
37861
37862
37863
37864
37865
37866
37867
37868
37869
37870
37871
37872
37873
37874
37875
37876
37877
37878
37879
37880
37881
37882
37883
37884
37885
37886
37887
37888
37889
37890
37891
37892
37893
37894
37895
37896
37897
37898
37899
37900
37901
37902
37903
37904
37905
37906
37907
37908
37909
37910
37911
37912
37913
37914
37915
37916
37917
37918
37919
37920
37921
37922
37923
37924
37925
37926
37927
37928
37929
37930
37931
37932
37933
37934
37935
37936
37937
37938
37939
37940
37941
37942
37943
37944
37945
37946
37947
37948
37949
37950
37951
37952
37953
37954
37955
37956
37957
37958
37959
37960
37961
37962
37963
37964
37965
37966
37967
37968
37969
37970
37971
37972
37973
37974
37975
37976
37977
37978
37979
37980
37981
37982
37983
37984
37985
37986
37987
37988
37989
37990
37991
37992
37993
37994
37995
37996
37997
37998
37999
38000
38001
38002
38003
38004
38005
38006
38007
38008
38009
38010
38011
38012
38013
38014
38015
38016
38017
38018
38019
38020
38021
38022
38023
38024
38025
38026
38027
38028
38029
38030
38031
38032
38033
38034
38035
38036
38037
38038
38039
38040
38041
38042
38043
38044
38045
38046
38047
38048
38049
38050
38051
38052
38053
38054
38055
38056
38057
38058
38059
38060
38061
38062
38063
38064
38065
38066
38067
38068
38069
38070
38071
38072
38073
38074
38075
38076
38077
38078
38079
38080
38081
38082
38083
38084
38085
38086
38087
38088
38089
38090
38091
38092
38093
38094
38095
38096
38097
38098
38099
38100
38101
38102
38103
38104
38105
38106
38107
38108
38109
38110
38111
38112
38113
38114
38115
38116
38117
38118
38119
38120
38121
38122
38123
38124
38125
38126
38127
38128
38129
38130
38131
38132
38133
38134
38135
38136
38137
38138
38139
38140
38141
38142
38143
38144
38145
38146
38147
38148
38149
38150
38151
38152
38153
38154
38155
38156
38157
38158
38159
38160
38161
38162
38163
38164
38165
38166
38167
38168
38169
38170
38171
38172
38173
38174
38175
38176
38177
38178
38179
38180
38181
38182
38183
38184
38185
38186
38187
38188
38189
38190
38191
38192
38193
38194
38195
38196
38197
38198
38199
38200
38201
38202
38203
38204
38205
38206
38207
38208
38209
38210
38211
38212
38213
38214
38215
38216
38217
38218
38219
38220
38221
38222
38223
38224
38225
38226
38227
38228
38229
38230
38231
38232
38233
38234
38235
38236
38237
38238
38239
38240
38241
38242
38243
38244
38245
38246
38247
38248
38249
38250
38251
38252
38253
38254
38255
38256
38257
38258
38259
38260
38261
38262
38263
38264
38265
38266
38267
38268
38269
38270
38271
38272
38273
38274
38275
38276
38277
38278
38279
38280
38281
38282
38283
38284
38285
38286
38287
38288
38289
38290
38291
38292
38293
38294
38295
38296
38297
38298
38299
38300
38301
38302
38303
38304
38305
38306
38307
38308
38309
38310
38311
38312
38313
38314
38315
38316
38317
38318
38319
38320
38321
38322
38323
38324
38325
38326
38327
38328
38329
38330
38331
38332
38333
38334
38335
38336
38337
38338
38339
38340
38341
38342
38343
38344
38345
38346
38347
38348
38349
38350
38351
38352
38353
38354
38355
38356
38357
38358
38359
38360
38361
38362
38363
38364
38365
38366
38367
38368
38369
38370
38371
38372
38373
38374
38375
38376
38377
38378
38379
38380
38381
38382
38383
38384
38385
38386
38387
38388
38389
38390
38391
38392
38393
38394
38395
38396
38397
38398
38399
38400
38401
38402
38403
38404
38405
38406
38407
38408
38409
38410
38411
38412
38413
38414
38415
38416
38417
38418
38419
38420
38421
38422
38423
38424
38425
38426
38427
38428
38429
38430
38431
38432
38433
38434
38435
38436
38437
38438
38439
38440
38441
38442
38443
38444
38445
38446
38447
38448
38449
38450
38451
38452
38453
38454
38455
38456
38457
38458
38459
38460
38461
38462
38463
38464
38465
38466
38467
38468
38469
38470
38471
38472
38473
38474
38475
38476
38477
38478
38479
38480
38481
38482
38483
38484
38485
38486
38487
38488
38489
38490
38491
38492
38493
38494
38495
38496
38497
38498
38499
38500
38501
38502
38503
38504
38505
38506
38507
38508
38509
38510
38511
38512
38513
38514
38515
38516
38517
38518
38519
38520
38521
38522
38523
38524
38525
38526
38527
38528
38529
38530
38531
38532
38533
38534
38535
38536
38537
38538
38539
38540
38541
38542
38543
38544
38545
38546
38547
38548
38549
38550
38551
38552
38553
38554
38555
38556
38557
38558
38559
38560
38561
38562
38563
38564
38565
38566
38567
38568
38569
38570
38571
38572
38573
38574
38575
38576
38577
38578
38579
38580
38581
38582
38583
38584
38585
38586
38587
38588
38589
38590
38591
38592
38593
38594
38595
38596
38597
38598
38599
38600
38601
38602
38603
38604
38605
38606
38607
38608
38609
38610
38611
38612
38613
38614
38615
38616
38617
38618
38619
38620
38621
38622
38623
38624
38625
38626
38627
38628
38629
38630
38631
38632
38633
38634
38635
38636
38637
38638
38639
38640
38641
38642
38643
38644
38645
38646
38647
38648
38649
38650
38651
38652
38653
38654
38655
38656
38657
38658
38659
38660
38661
38662
38663
38664
38665
38666
38667
38668
38669
38670
38671
38672
38673
38674
38675
38676
38677
38678
38679
38680
38681
38682
38683
38684
38685
38686
38687
38688
38689
38690
38691
38692
38693
38694
38695
38696
38697
38698
38699
38700
38701
38702
38703
38704
38705
38706
38707
38708
38709
38710
38711
38712
38713
38714
38715
38716
38717
38718
38719
38720
38721
38722
38723
38724
38725
38726
38727
38728
38729
38730
38731
38732
38733
38734
38735
38736
38737
38738
38739
38740
38741
38742
38743
38744
38745
38746
38747
38748
38749
38750
38751
38752
38753
38754
38755
38756
38757
38758
38759
38760
38761
38762
38763
38764
38765
38766
38767
38768
38769
38770
38771
38772
38773
38774
38775
38776
38777
38778
38779
38780
38781
38782
38783
38784
38785
38786
38787
38788
38789
38790
38791
38792
38793
38794
38795
38796
38797
38798
38799
38800
38801
38802
38803
38804
38805
38806
38807
38808
38809
38810
38811
38812
38813
38814
38815
38816
38817
38818
38819
38820
38821
38822
38823
38824
38825
38826
38827
38828
38829
38830
38831
38832
38833
38834
38835
38836
38837
38838
38839
38840
38841
38842
38843
38844
38845
38846
38847
38848
38849
38850
38851
38852
38853
38854
38855
38856
38857
38858
38859
38860
38861
38862
38863
38864
38865
38866
38867
38868
38869
38870
38871
38872
38873
38874
38875
38876
38877
38878
38879
38880
38881
38882
38883
38884
38885
38886
38887
38888
38889
38890
38891
38892
38893
38894
38895
38896
38897
38898
38899
38900
38901
38902
38903
38904
38905
38906
38907
38908
38909
38910
38911
38912
38913
38914
38915
38916
38917
38918
38919
38920
38921
38922
38923
38924
38925
38926
38927
38928
38929
38930
38931
38932
38933
38934
38935
38936
38937
38938
38939
38940
38941
38942
38943
38944
38945
38946
38947
38948
38949
38950
38951
38952
38953
38954
38955
38956
38957
38958
38959
38960
38961
38962
38963
38964
38965
38966
38967
38968
38969
38970
38971
38972
38973
38974
38975
38976
38977
38978
38979
38980
38981
38982
38983
38984
38985
38986
38987
38988
38989
38990
38991
38992
38993
38994
38995
38996
38997
38998
38999
39000
39001
39002
39003
39004
39005
39006
39007
39008
39009
39010
39011
39012
39013
39014
39015
39016
39017
39018
39019
39020
39021
39022
39023
39024
39025
39026
39027
39028
39029
39030
39031
39032
39033
39034
39035
39036
39037
39038
39039
39040
39041
39042
39043
39044
39045
39046
39047
39048
39049
39050
39051
39052
39053
39054
39055
39056
39057
39058
39059
39060
39061
39062
39063
39064
39065
39066
39067
39068
39069
39070
39071
39072
39073
39074
39075
39076
39077
39078
39079
39080
39081
39082
39083
39084
39085
39086
39087
39088
39089
39090
39091
39092
39093
39094
39095
39096
39097
39098
39099
39100
39101
39102
39103
39104
39105
39106
39107
39108
39109
39110
39111
39112
39113
39114
39115
39116
39117
39118
39119
39120
39121
39122
39123
39124
39125
39126
39127
39128
39129
39130
39131
39132
39133
39134
39135
39136
39137
39138
39139
39140
39141
39142
39143
39144
39145
39146
39147
39148
39149
39150
39151
39152
39153
39154
39155
39156
39157
39158
39159
39160
39161
39162
39163
39164
39165
39166
39167
39168
39169
39170
39171
39172
39173
39174
39175
39176
39177
39178
39179
39180
39181
39182
39183
39184
39185
39186
39187
39188
39189
39190
39191
39192
39193
39194
39195
39196
39197
39198
39199
39200
39201
39202
39203
39204
39205
39206
39207
39208
39209
39210
39211
39212
39213
39214
39215
39216
39217
39218
39219
39220
39221
39222
39223
39224
39225
39226
39227
39228
39229
39230
39231
39232
39233
39234
39235
39236
39237
39238
39239
39240
39241
39242
39243
39244
39245
39246
39247
39248
39249
39250
39251
39252
39253
39254
39255
39256
39257
39258
39259
39260
39261
39262
39263
39264
39265
39266
39267
39268
39269
39270
39271
39272
39273
39274
39275
39276
39277
39278
39279
39280
39281
39282
39283
39284
39285
39286
39287
39288
39289
39290
39291
39292
39293
39294
39295
39296
39297
39298
39299
39300
39301
39302
39303
39304
39305
39306
39307
39308
39309
39310
39311
39312
39313
39314
39315
39316
39317
39318
39319
39320
39321
39322
39323
39324
39325
39326
39327
39328
39329
39330
39331
39332
39333
39334
39335
39336
39337
39338
39339
39340
39341
39342
39343
39344
39345
39346
39347
39348
39349
39350
39351
39352
39353
39354
39355
39356
39357
39358
39359
39360
39361
39362
39363
39364
39365
39366
39367
39368
39369
39370
39371
39372
39373
39374
39375
39376
39377
39378
39379
39380
39381
39382
39383
39384
39385
39386
39387
39388
39389
39390
39391
39392
39393
39394
39395
39396
39397
39398
39399
39400
39401
39402
39403
39404
39405
39406
39407
39408
39409
39410
39411
39412
39413
39414
39415
39416
39417
39418
39419
39420
39421
39422
39423
39424
39425
39426
39427
39428
39429
39430
39431
39432
39433
39434
39435
39436
39437
39438
39439
39440
39441
39442
39443
39444
39445
39446
39447
39448
39449
39450
39451
39452
39453
39454
39455
39456
39457
39458
39459
39460
39461
39462
39463
39464
39465
39466
39467
39468
39469
39470
39471
39472
39473
39474
39475
39476
39477
39478
39479
39480
39481
39482
39483
39484
39485
39486
39487
39488
39489
39490
39491
39492
39493
39494
39495
39496
39497
39498
39499
39500
39501
39502
39503
39504
39505
39506
39507
39508
39509
39510
39511
39512
39513
39514
39515
39516
39517
39518
39519
39520
39521
39522
39523
39524
39525
39526
39527
39528
39529
39530
39531
39532
39533
39534
39535
39536
39537
39538
39539
39540
39541
39542
39543
39544
39545
39546
39547
39548
39549
39550
39551
39552
39553
39554
39555
39556
39557
39558
39559
39560
39561
39562
39563
39564
39565
39566
39567
39568
39569
39570
39571
39572
39573
39574
39575
39576
39577
39578
39579
39580
39581
39582
39583
39584
39585
39586
39587
39588
39589
39590
39591
39592
39593
39594
39595
39596
39597
39598
39599
39600
39601
39602
39603
39604
39605
39606
39607
39608
39609
39610
39611
39612
39613
39614
39615
39616
39617
39618
39619
39620
39621
39622
39623
39624
39625
39626
39627
39628
39629
39630
39631
39632
39633
39634
39635
39636
39637
39638
39639
39640
39641
39642
39643
39644
39645
39646
39647
39648
39649
39650
39651
39652
39653
39654
39655
39656
39657
39658
39659
39660
39661
39662
39663
39664
39665
39666
39667
39668
39669
39670
39671
39672
39673
39674
39675
39676
39677
39678
39679
39680
39681
39682
39683
39684
39685
39686
39687
39688
39689
39690
39691
39692
39693
39694
39695
39696
39697
39698
39699
39700
39701
39702
39703
39704
39705
39706
39707
39708
39709
39710
39711
39712
39713
39714
39715
39716
39717
39718
39719
39720
39721
39722
39723
39724
39725
39726
39727
39728
39729
39730
39731
39732
39733
39734
39735
39736
39737
39738
39739
39740
39741
39742
39743
39744
39745
39746
39747
39748
39749
39750
39751
39752
39753
39754
39755
39756
39757
39758
39759
39760
39761
39762
39763
39764
39765
39766
39767
39768
39769
39770
39771
39772
39773
39774
39775
39776
39777
39778
39779
39780
39781
39782
39783
39784
39785
39786
39787
39788
39789
39790
39791
39792
39793
39794
39795
39796
39797
39798
39799
39800
39801
39802
39803
39804
39805
39806
39807
39808
39809
39810
39811
39812
39813
39814
39815
39816
39817
39818
39819
39820
39821
39822
39823
39824
39825
39826
39827
39828
39829
39830
39831
39832
39833
39834
39835
39836
39837
39838
39839
39840
39841
39842
39843
39844
39845
39846
39847
39848
39849
39850
39851
39852
39853
39854
39855
39856
39857
39858
39859
39860
39861
39862
39863
39864
39865
39866
39867
39868
39869
39870
39871
39872
39873
39874
39875
39876
39877
39878
39879
39880
39881
39882
39883
39884
39885
39886
39887
39888
39889
39890
39891
39892
39893
39894
39895
39896
39897
39898
39899
39900
39901
39902
39903
39904
39905
39906
39907
39908
39909
39910
39911
39912
39913
39914
39915
39916
39917
39918
39919
39920
39921
39922
39923
39924
39925
39926
39927
39928
39929
39930
39931
39932
39933
39934
39935
39936
39937
39938
39939
39940
39941
39942
39943
39944
39945
39946
39947
39948
39949
39950
39951
39952
39953
39954
39955
39956
39957
39958
39959
39960
39961
39962
39963
39964
39965
39966
39967
39968
39969
39970
39971
39972
39973
39974
39975
39976
39977
39978
39979
39980
39981
39982
39983
39984
39985
39986
39987
39988
39989
39990
39991
39992
39993
39994
39995
39996
39997
39998
39999
40000
40001
40002
40003
40004
40005
40006
40007
40008
40009
40010
40011
40012
40013
40014
40015
40016
40017
40018
40019
40020
40021
40022
40023
40024
40025
40026
40027
40028
40029
40030
40031
40032
40033
40034
40035
40036
40037
40038
40039
40040
40041
40042
40043
40044
40045
40046
40047
40048
40049
40050
40051
40052
40053
40054
40055
40056
40057
40058
40059
40060
40061
40062
40063
40064
40065
40066
40067
40068
40069
40070
40071
40072
40073
40074
40075
40076
40077
40078
40079
40080
40081
40082
40083
40084
40085
40086
40087
40088
40089
40090
40091
40092
40093
40094
40095
40096
40097
40098
40099
40100
40101
40102
40103
40104
40105
40106
40107
40108
40109
40110
40111
40112
40113
40114
40115
40116
40117
40118
40119
40120
40121
40122
40123
40124
40125
40126
40127
40128
40129
40130
40131
40132
40133
40134
40135
40136
40137
40138
40139
40140
40141
40142
40143
40144
40145
40146
40147
40148
40149
40150
40151
40152
40153
40154
40155
40156
40157
40158
40159
40160
40161
40162
40163
40164
40165
40166
40167
40168
40169
40170
40171
40172
40173
40174
40175
40176
40177
40178
40179
40180
40181
40182
40183
40184
40185
40186
40187
40188
40189
40190
40191
40192
40193
40194
40195
40196
40197
40198
40199
40200
40201
40202
40203
40204
40205
40206
40207
40208
40209
40210
40211
40212
40213
40214
40215
40216
40217
40218
40219
40220
40221
40222
40223
40224
40225
40226
40227
40228
40229
40230
40231
40232
40233
40234
40235
40236
40237
40238
40239
40240
40241
40242
40243
40244
40245
40246
40247
40248
40249
40250
40251
40252
40253
40254
40255
40256
40257
40258
40259
40260
40261
40262
40263
40264
40265
40266
40267
40268
40269
40270
40271
40272
40273
40274
40275
40276
40277
40278
40279
40280
40281
40282
40283
40284
40285
40286
40287
40288
40289
40290
40291
40292
40293
40294
40295
40296
40297
40298
40299
40300
40301
40302
40303
40304
40305
40306
40307
40308
40309
40310
40311
40312
40313
40314
40315
40316
40317
40318
40319
40320
40321
40322
40323
40324
40325
40326
40327
40328
40329
40330
40331
40332
40333
40334
40335
40336
40337
40338
40339
40340
40341
40342
40343
40344
40345
40346
40347
40348
40349
40350
40351
40352
40353
40354
40355
40356
40357
40358
40359
40360
40361
40362
40363
40364
40365
40366
40367
40368
40369
40370
40371
40372
40373
40374
40375
40376
40377
40378
40379
40380
40381
40382
40383
40384
40385
40386
40387
40388
40389
40390
40391
40392
40393
40394
40395
40396
40397
40398
40399
40400
40401
40402
40403
40404
40405
40406
40407
40408
40409
40410
40411
40412
40413
40414
40415
40416
40417
40418
40419
40420
40421
40422
40423
40424
40425
40426
40427
40428
40429
40430
40431
40432
40433
40434
40435
40436
40437
40438
40439
40440
40441
40442
40443
40444
40445
40446
40447
40448
40449
40450
40451
40452
40453
40454
40455
40456
40457
40458
40459
40460
40461
40462
40463
40464
40465
40466
40467
40468
40469
40470
40471
40472
40473
40474
40475
40476
40477
40478
40479
40480
40481
40482
40483
40484
40485
40486
40487
40488
40489
40490
40491
40492
40493
40494
40495
40496
40497
40498
40499
40500
40501
40502
40503
40504
40505
40506
40507
40508
40509
40510
40511
40512
40513
40514
40515
40516
40517
40518
40519
40520
40521
40522
40523
40524
40525
40526
40527
40528
40529
40530
40531
40532
40533
40534
40535
40536
40537
40538
40539
40540
40541
40542
40543
40544
40545
40546
40547
40548
40549
40550
40551
40552
40553
40554
40555
40556
40557
40558
40559
40560
40561
40562
40563
40564
40565
40566
40567
40568
40569
40570
40571
40572
40573
40574
40575
40576
40577
40578
40579
40580
40581
40582
40583
40584
40585
40586
40587
40588
40589
40590
40591
40592
40593
40594
40595
40596
40597
40598
40599
40600
40601
40602
40603
40604
40605
40606
40607
40608
40609
40610
40611
40612
40613
40614
40615
40616
40617
40618
40619
40620
40621
40622
40623
40624
40625
40626
40627
40628
40629
40630
40631
40632
40633
40634
40635
40636
40637
40638
40639
40640
40641
40642
40643
40644
40645
40646
40647
40648
40649
40650
40651
40652
40653
40654
40655
40656
40657
40658
40659
40660
40661
40662
40663
40664
40665
40666
40667
40668
40669
40670
40671
40672
40673
40674
40675
40676
40677
40678
40679
40680
40681
40682
40683
40684
40685
40686
40687
40688
40689
40690
40691
40692
40693
40694
40695
40696
40697
40698
40699
40700
40701
40702
40703
40704
40705
40706
40707
40708
40709
40710
40711
40712
40713
40714
40715
40716
40717
40718
40719
40720
40721
40722
40723
40724
40725
40726
40727
40728
40729
40730
40731
40732
40733
40734
40735
40736
40737
40738
40739
40740
40741
40742
40743
40744
40745
40746
40747
40748
40749
40750
40751
40752
40753
40754
40755
40756
40757
40758
40759
40760
40761
40762
40763
40764
40765
40766
40767
40768
40769
40770
40771
40772
40773
40774
40775
40776
40777
40778
40779
40780
40781
40782
40783
40784
40785
40786
40787
40788
40789
40790
40791
40792
40793
40794
40795
40796
40797
40798
40799
40800
40801
40802
40803
40804
40805
40806
40807
40808
40809
40810
40811
40812
40813
40814
40815
40816
40817
40818
40819
40820
40821
40822
40823
40824
40825
40826
40827
40828
40829
40830
40831
40832
40833
40834
40835
40836
40837
40838
40839
40840
40841
40842
40843
40844
40845
40846
40847
40848
40849
40850
40851
40852
40853
40854
40855
40856
40857
40858
40859
40860
40861
40862
40863
40864
40865
40866
40867
40868
40869
40870
40871
40872
40873
40874
40875
40876
40877
40878
40879
40880
40881
40882
40883
40884
40885
40886
40887
40888
40889
40890
40891
40892
40893
40894
40895
40896
40897
40898
40899
40900
40901
40902
40903
40904
40905
40906
40907
40908
40909
40910
40911
40912
40913
40914
40915
40916
40917
40918
40919
40920
40921
40922
40923
40924
40925
40926
40927
40928
40929
40930
40931
40932
40933
40934
40935
40936
40937
40938
40939
40940
40941
40942
40943
40944
40945
40946
40947
40948
40949
40950
40951
40952
40953
40954
40955
40956
40957
40958
40959
40960
40961
40962
40963
40964
40965
40966
40967
40968
40969
40970
40971
40972
40973
40974
40975
40976
40977
40978
40979
40980
40981
40982
40983
40984
40985
40986
40987
40988
40989
40990
40991
40992
40993
40994
40995
40996
40997
40998
40999
41000
41001
41002
41003
41004
41005
41006
41007
41008
41009
41010
41011
41012
41013
41014
41015
41016
41017
41018
41019
41020
41021
41022
41023
41024
41025
41026
41027
41028
41029
41030
41031
41032
41033
41034
41035
41036
41037
41038
41039
41040
41041
41042
41043
41044
41045
41046
41047
41048
41049
41050
41051
41052
41053
41054
41055
41056
41057
41058
41059
41060
41061
41062
41063
41064
41065
41066
41067
41068
41069
41070
41071
41072
41073
41074
41075
41076
41077
41078
41079
41080
41081
41082
41083
41084
41085
41086
41087
41088
41089
41090
41091
41092
41093
41094
41095
41096
41097
41098
41099
41100
41101
41102
41103
41104
41105
41106
41107
41108
41109
41110
41111
41112
41113
41114
41115
41116
41117
41118
41119
41120
41121
41122
41123
41124
41125
41126
41127
41128
41129
41130
41131
41132
41133
41134
41135
41136
41137
41138
41139
41140
41141
41142
41143
41144
41145
41146
41147
41148
41149
41150
41151
41152
41153
41154
41155
41156
41157
41158
41159
41160
41161
41162
41163
41164
41165
41166
41167
41168
41169
41170
41171
41172
41173
41174
41175
41176
41177
41178
41179
41180
41181
41182
41183
41184
41185
41186
41187
41188
41189
41190
41191
41192
41193
41194
41195
41196
41197
41198
41199
41200
41201
41202
41203
41204
41205
41206
41207
41208
41209
41210
41211
41212
41213
41214
41215
41216
41217
41218
41219
41220
41221
41222
41223
41224
41225
41226
41227
41228
41229
41230
41231
41232
41233
41234
41235
41236
41237
41238
41239
41240
41241
41242
41243
41244
41245
41246
41247
41248
41249
41250
41251
41252
41253
41254
41255
41256
41257
41258
41259
41260
41261
41262
41263
41264
41265
41266
41267
41268
41269
41270
41271
41272
41273
41274
41275
41276
41277
41278
41279
41280
41281
41282
41283
41284
41285
41286
41287
41288
41289
41290
41291
41292
41293
41294
41295
41296
41297
41298
41299
41300
41301
41302
41303
41304
41305
41306
41307
41308
41309
41310
41311
41312
41313
41314
41315
41316
41317
41318
41319
41320
41321
41322
41323
41324
41325
41326
41327
41328
41329
41330
41331
41332
41333
41334
41335
41336
41337
41338
41339
41340
41341
41342
41343
41344
41345
41346
41347
41348
41349
41350
41351
41352
41353
41354
41355
41356
41357
41358
41359
41360
41361
41362
41363
41364
41365
41366
41367
41368
41369
41370
41371
41372
41373
41374
41375
41376
41377
41378
41379
41380
41381
41382
41383
41384
41385
41386
41387
41388
41389
41390
41391
41392
41393
41394
41395
41396
41397
41398
41399
41400
41401
41402
41403
41404
41405
41406
41407
41408
41409
41410
41411
41412
41413
41414
41415
41416
41417
41418
41419
41420
41421
41422
41423
41424
41425
41426
41427
41428
41429
41430
41431
41432
41433
41434
41435
41436
41437
41438
41439
41440
41441
41442
41443
41444
41445
41446
41447
41448
41449
41450
41451
41452
41453
41454
41455
41456
41457
41458
41459
41460
41461
41462
41463
41464
41465
41466
41467
41468
41469
41470
41471
41472
41473
41474
41475
41476
41477
41478
41479
41480
41481
41482
41483
41484
41485
41486
41487
41488
41489
41490
41491
41492
41493
41494
41495
41496
41497
41498
41499
41500
41501
41502
41503
41504
41505
41506
41507
41508
41509
41510
41511
41512
41513
41514
41515
41516
41517
41518
41519
41520
41521
41522
41523
41524
41525
41526
41527
41528
41529
41530
41531
41532
41533
41534
41535
41536
41537
41538
41539
41540
41541
41542
41543
41544
41545
41546
41547
41548
41549
41550
41551
41552
41553
41554
41555
41556
41557
41558
41559
41560
41561
41562
41563
41564
41565
41566
41567
41568
41569
41570
41571
41572
41573
41574
41575
41576
41577
41578
41579
41580
41581
41582
41583
41584
41585
41586
41587
41588
41589
41590
41591
41592
41593
41594
41595
41596
41597
41598
41599
41600
41601
41602
41603
41604
41605
41606
41607
41608
41609
41610
41611
41612
41613
41614
41615
41616
41617
41618
41619
41620
41621
41622
41623
41624
41625
41626
41627
41628
41629
41630
41631
41632
41633
41634
41635
41636
41637
41638
41639
41640
41641
41642
41643
41644
41645
41646
41647
41648
41649
41650
41651
41652
41653
41654
41655
41656
41657
41658
41659
41660
41661
41662
41663
41664
41665
41666
41667
41668
41669
41670
41671
41672
41673
41674
41675
41676
41677
41678
41679
41680
41681
41682
41683
41684
41685
41686
41687
41688
41689
41690
41691
41692
41693
41694
41695
41696
41697
41698
41699
41700
41701
41702
41703
41704
41705
41706
41707
41708
41709
41710
41711
41712
41713
41714
41715
41716
41717
41718
41719
41720
41721
41722
41723
41724
41725
41726
41727
41728
41729
41730
41731
41732
41733
41734
41735
41736
41737
41738
41739
41740
41741
41742
41743
41744
41745
41746
41747
41748
41749
41750
41751
41752
41753
41754
41755
41756
41757
41758
41759
41760
41761
41762
41763
41764
41765
41766
41767
41768
41769
41770
41771
41772
41773
41774
41775
41776
41777
41778
41779
41780
41781
41782
41783
41784
41785
41786
41787
41788
41789
41790
41791
41792
41793
41794
41795
41796
41797
41798
41799
41800
41801
41802
41803
41804
41805
41806
41807
41808
41809
41810
41811
41812
41813
41814
41815
41816
41817
41818
41819
41820
41821
41822
41823
41824
41825
41826
41827
41828
41829
41830
41831
41832
41833
41834
41835
41836
41837
41838
41839
41840
41841
41842
41843
41844
41845
41846
41847
41848
41849
41850
41851
41852
41853
41854
41855
41856
41857
41858
41859
41860
41861
41862
41863
41864
41865
41866
41867
41868
41869
41870
41871
41872
41873
41874
41875
41876
41877
41878
41879
41880
41881
41882
41883
41884
41885
41886
41887
41888
41889
41890
41891
41892
41893
41894
41895
41896
41897
41898
41899
41900
41901
41902
41903
41904
41905
41906
41907
41908
41909
41910
41911
41912
41913
41914
41915
41916
41917
41918
41919
41920
41921
41922
41923
41924
41925
41926
41927
41928
41929
41930
41931
41932
41933
41934
41935
41936
41937
41938
41939
41940
41941
41942
41943
41944
41945
41946
41947
41948
41949
41950
41951
41952
41953
41954
41955
41956
41957
41958
41959
41960
41961
41962
41963
41964
41965
41966
41967
41968
41969
41970
41971
41972
41973
41974
41975
41976
41977
41978
41979
41980
41981
41982
41983
41984
41985
41986
41987
41988
41989
41990
41991
41992
41993
41994
41995
41996
41997
41998
41999
42000
42001
42002
42003
42004
42005
42006
42007
42008
42009
42010
42011
42012
42013
42014
42015
42016
42017
42018
42019
42020
42021
42022
42023
42024
42025
42026
42027
42028
42029
42030
42031
42032
42033
42034
42035
42036
42037
42038
42039
42040
42041
42042
42043
42044
42045
42046
42047
42048
42049
42050
42051
42052
42053
42054
42055
42056
42057
42058
42059
42060
42061
42062
42063
42064
42065
42066
42067
42068
42069
42070
42071
42072
42073
42074
42075
42076
42077
42078
42079
42080
42081
42082
42083
42084
42085
42086
42087
42088
42089
42090
42091
42092
42093
42094
42095
42096
42097
42098
42099
42100
42101
42102
42103
42104
42105
42106
42107
42108
42109
42110
42111
42112
42113
42114
42115
42116
42117
42118
42119
42120
42121
42122
42123
42124
42125
42126
42127
42128
42129
42130
42131
42132
42133
42134
42135
42136
42137
42138
42139
42140
42141
42142
42143
42144
42145
42146
42147
42148
42149
42150
42151
42152
42153
42154
42155
42156
42157
42158
42159
42160
42161
42162
42163
42164
42165
42166
42167
42168
42169
42170
42171
42172
42173
42174
42175
42176
42177
42178
42179
42180
42181
42182
42183
42184
42185
42186
42187
42188
42189
42190
42191
42192
42193
42194
42195
42196
42197
42198
42199
42200
42201
42202
42203
42204
42205
42206
42207
42208
42209
42210
42211
42212
42213
42214
42215
42216
42217
42218
42219
42220
42221
42222
42223
42224
42225
42226
42227
42228
42229
42230
42231
42232
42233
42234
42235
42236
42237
42238
42239
42240
42241
42242
42243
42244
42245
42246
42247
42248
42249
42250
42251
42252
42253
42254
42255
42256
42257
42258
42259
42260
42261
42262
42263
42264
42265
42266
42267
42268
42269
42270
42271
42272
42273
42274
42275
42276
42277
42278
42279
42280
42281
42282
42283
42284
42285
42286
42287
42288
42289
42290
42291
42292
42293
42294
42295
42296
42297
42298
42299
42300
42301
42302
42303
42304
42305
42306
42307
42308
42309
42310
42311
42312
42313
42314
42315
42316
42317
42318
42319
42320
42321
42322
42323
42324
42325
42326
42327
42328
42329
42330
42331
42332
42333
42334
42335
42336
42337
42338
42339
42340
42341
42342
42343
42344
42345
42346
42347
42348
42349
42350
42351
42352
42353
42354
42355
42356
42357
42358
42359
42360
42361
42362
42363
42364
42365
42366
42367
42368
42369
42370
42371
42372
42373
42374
42375
42376
42377
42378
42379
42380
42381
42382
42383
42384
42385
42386
42387
42388
42389
42390
42391
42392
42393
42394
42395
42396
42397
42398
42399
42400
42401
42402
42403
42404
42405
42406
42407
42408
42409
42410
42411
42412
42413
42414
42415
42416
42417
42418
42419
42420
42421
42422
42423
42424
42425
42426
42427
42428
42429
42430
42431
42432
42433
42434
42435
42436
42437
42438
42439
42440
42441
42442
42443
42444
42445
42446
42447
42448
42449
42450
42451
42452
42453
42454
42455
42456
42457
42458
42459
42460
42461
42462
42463
42464
42465
42466
42467
42468
42469
42470
42471
42472
42473
42474
42475
42476
42477
42478
42479
42480
42481
42482
42483
42484
42485
42486
42487
42488
42489
42490
42491
42492
42493
42494
42495
42496
42497
42498
42499
42500
42501
42502
42503
42504
42505
42506
42507
42508
42509
42510
42511
42512
42513
42514
42515
42516
42517
42518
42519
42520
42521
42522
42523
42524
42525
42526
42527
42528
42529
42530
42531
42532
42533
42534
42535
42536
42537
42538
42539
42540
42541
42542
42543
42544
42545
42546
42547
42548
42549
42550
42551
42552
42553
42554
42555
42556
42557
42558
42559
42560
42561
42562
42563
42564
42565
42566
42567
42568
42569
42570
42571
42572
42573
42574
42575
42576
42577
42578
42579
42580
42581
42582
42583
42584
42585
42586
42587
42588
42589
42590
42591
42592
42593
42594
42595
42596
42597
42598
42599
42600
42601
42602
42603
42604
42605
42606
42607
42608
42609
42610
42611
42612
42613
42614
42615
42616
42617
42618
42619
42620
42621
42622
42623
42624
42625
42626
42627
42628
42629
42630
42631
42632
42633
42634
42635
42636
42637
42638
42639
42640
42641
42642
42643
42644
42645
42646
42647
42648
42649
42650
42651
42652
42653
42654
42655
42656
42657
42658
42659
42660
42661
42662
42663
42664
42665
42666
42667
42668
42669
42670
42671
42672
42673
42674
42675
42676
42677
42678
42679
42680
42681
42682
42683
42684
42685
42686
42687
42688
42689
42690
42691
42692
42693
42694
42695
42696
42697
42698
42699
42700
42701
42702
42703
42704
42705
42706
42707
42708
42709
42710
42711
42712
42713
42714
42715
42716
42717
42718
42719
42720
42721
42722
42723
42724
42725
42726
42727
42728
42729
42730
42731
42732
42733
42734
42735
42736
42737
42738
42739
42740
42741
42742
42743
42744
42745
42746
42747
42748
42749
42750
42751
42752
42753
42754
42755
42756
42757
42758
42759
42760
42761
42762
42763
42764
42765
42766
42767
42768
42769
42770
42771
42772
42773
42774
42775
42776
42777
42778
42779
42780
42781
42782
42783
42784
42785
42786
42787
42788
42789
42790
42791
42792
42793
42794
42795
42796
42797
42798
42799
42800
42801
42802
42803
42804
42805
42806
42807
42808
42809
42810
42811
42812
42813
42814
42815
42816
42817
42818
42819
42820
42821
42822
42823
42824
42825
42826
42827
42828
42829
42830
42831
42832
42833
42834
42835
42836
42837
42838
42839
42840
42841
42842
42843
42844
42845
42846
42847
42848
42849
42850
42851
42852
42853
42854
42855
42856
42857
42858
42859
42860
42861
42862
42863
42864
42865
42866
42867
42868
42869
42870
42871
42872
42873
42874
42875
42876
42877
42878
42879
42880
42881
42882
42883
42884
42885
42886
42887
42888
42889
42890
42891
42892
42893
42894
42895
42896
42897
42898
42899
42900
42901
42902
42903
42904
42905
42906
42907
42908
42909
42910
42911
42912
42913
42914
42915
42916
42917
42918
42919
42920
42921
42922
42923
42924
42925
42926
42927
42928
42929
42930
42931
42932
42933
42934
42935
42936
42937
42938
42939
42940
42941
42942
42943
42944
42945
42946
42947
42948
42949
42950
42951
42952
42953
42954
42955
42956
42957
42958
42959
42960
42961
42962
42963
42964
42965
42966
42967
42968
42969
42970
42971
42972
42973
42974
42975
42976
42977
42978
42979
42980
42981
42982
42983
42984
42985
42986
42987
42988
42989
42990
42991
42992
42993
42994
42995
42996
42997
42998
42999
43000
43001
43002
43003
43004
43005
43006
43007
43008
43009
43010
43011
43012
43013
43014
43015
43016
43017
43018
43019
43020
43021
43022
43023
43024
43025
43026
43027
43028
43029
43030
43031
43032
43033
43034
43035
43036
43037
43038
43039
43040
43041
43042
43043
43044
43045
43046
43047
43048
43049
43050
43051
43052
43053
43054
43055
43056
43057
43058
43059
43060
43061
43062
43063
43064
43065
43066
43067
43068
43069
43070
43071
43072
43073
43074
43075
43076
43077
43078
43079
43080
43081
43082
43083
43084
43085
43086
43087
43088
43089
43090
43091
43092
43093
43094
43095
43096
43097
43098
43099
43100
43101
43102
43103
43104
43105
43106
43107
43108
43109
43110
43111
43112
43113
43114
43115
43116
43117
43118
43119
43120
43121
43122
43123
43124
43125
43126
43127
43128
43129
43130
43131
43132
43133
43134
43135
43136
43137
43138
43139
43140
43141
43142
43143
43144
43145
43146
43147
43148
43149
43150
43151
43152
43153
43154
43155
43156
43157
43158
43159
43160
43161
43162
43163
43164
43165
43166
43167
43168
43169
43170
43171
43172
43173
43174
43175
43176
43177
43178
43179
43180
43181
43182
43183
43184
43185
43186
43187
43188
43189
43190
43191
43192
43193
43194
43195
43196
43197
43198
43199
43200
43201
43202
43203
43204
43205
43206
43207
43208
43209
43210
43211
43212
43213
43214
43215
43216
43217
43218
43219
43220
43221
43222
43223
43224
43225
43226
43227
43228
43229
43230
43231
43232
43233
43234
43235
43236
43237
43238
43239
43240
43241
43242
43243
43244
43245
43246
43247
43248
43249
43250
43251
43252
43253
43254
43255
43256
43257
43258
43259
43260
43261
43262
43263
43264
43265
43266
43267
43268
43269
43270
43271
43272
43273
43274
43275
43276
43277
43278
43279
43280
43281
43282
43283
43284
43285
43286
43287
43288
43289
43290
43291
43292
43293
43294
43295
43296
43297
43298
43299
43300
43301
43302
43303
43304
43305
43306
43307
43308
43309
43310
43311
43312
43313
43314
43315
43316
43317
43318
43319
43320
43321
43322
43323
43324
43325
43326
43327
43328
43329
43330
43331
43332
43333
43334
43335
43336
43337
43338
43339
43340
43341
43342
43343
43344
43345
43346
43347
43348
43349
43350
43351
43352
43353
43354
43355
43356
43357
43358
43359
43360
43361
43362
43363
43364
43365
43366
43367
43368
43369
43370
43371
43372
43373
43374
43375
43376
43377
43378
43379
43380
43381
43382
43383
43384
43385
43386
43387
43388
43389
43390
43391
43392
43393
43394
43395
43396
43397
43398
43399
43400
43401
43402
43403
43404
43405
43406
43407
43408
43409
43410
43411
43412
43413
43414
43415
43416
43417
43418
43419
43420
43421
43422
43423
43424
43425
43426
43427
43428
43429
43430
43431
43432
43433
43434
43435
43436
43437
43438
43439
43440
43441
43442
43443
43444
43445
43446
43447
43448
43449
43450
43451
43452
43453
43454
43455
43456
43457
43458
43459
43460
43461
43462
43463
43464
43465
43466
43467
43468
43469
43470
43471
43472
43473
43474
43475
43476
43477
43478
43479
43480
43481
43482
43483
43484
43485
43486
43487
43488
43489
43490
43491
43492
43493
43494
43495
43496
43497
43498
43499
43500
43501
43502
43503
43504
43505
43506
43507
43508
43509
43510
43511
43512
43513
43514
43515
43516
43517
43518
43519
43520
43521
43522
43523
43524
43525
43526
43527
43528
43529
43530
43531
43532
43533
43534
43535
43536
43537
43538
43539
43540
43541
43542
43543
43544
43545
43546
43547
43548
43549
43550
43551
43552
43553
43554
43555
43556
43557
43558
43559
43560
43561
43562
43563
43564
43565
43566
43567
43568
43569
43570
43571
43572
43573
43574
43575
43576
43577
43578
43579
43580
43581
43582
43583
43584
43585
43586
43587
43588
43589
43590
43591
43592
43593
43594
43595
43596
43597
43598
43599
43600
43601
43602
43603
43604
43605
43606
43607
43608
43609
43610
43611
43612
43613
43614
43615
43616
43617
43618
43619
43620
43621
43622
43623
43624
43625
43626
43627
43628
43629
43630
43631
43632
43633
43634
43635
43636
43637
43638
43639
43640
43641
43642
43643
43644
43645
43646
43647
43648
43649
43650
43651
43652
43653
43654
43655
43656
43657
43658
43659
43660
43661
43662
43663
43664
43665
43666
43667
43668
43669
43670
43671
43672
43673
43674
43675
43676
43677
43678
43679
43680
43681
43682
43683
43684
43685
43686
43687
43688
43689
43690
43691
43692
43693
43694
43695
43696
43697
43698
43699
43700
43701
43702
43703
43704
43705
43706
43707
43708
43709
43710
43711
43712
43713
43714
43715
43716
43717
43718
43719
43720
43721
43722
43723
43724
43725
43726
43727
43728
43729
43730
43731
43732
43733
43734
43735
43736
43737
43738
43739
43740
43741
43742
43743
43744
43745
43746
43747
43748
43749
43750
43751
43752
43753
43754
43755
43756
43757
43758
43759
43760
43761
43762
43763
43764
43765
43766
43767
43768
43769
43770
43771
43772
43773
43774
43775
43776
43777
43778
43779
43780
43781
43782
43783
43784
43785
43786
43787
43788
43789
43790
43791
43792
43793
43794
43795
43796
43797
43798
43799
43800
43801
43802
43803
43804
43805
43806
43807
43808
43809
43810
43811
43812
43813
43814
43815
43816
43817
43818
43819
43820
43821
43822
43823
43824
43825
43826
43827
43828
43829
43830
43831
43832
43833
43834
43835
43836
43837
43838
43839
43840
43841
43842
43843
43844
43845
43846
43847
43848
43849
43850
43851
43852
43853
43854
43855
43856
43857
43858
43859
43860
43861
43862
43863
43864
43865
43866
43867
43868
43869
43870
43871
43872
43873
43874
43875
43876
43877
43878
43879
43880
43881
43882
43883
43884
43885
43886
43887
43888
43889
43890
43891
43892
43893
43894
43895
43896
43897
43898
43899
43900
43901
43902
43903
43904
43905
43906
43907
43908
43909
43910
43911
43912
43913
43914
43915
43916
43917
43918
43919
43920
43921
43922
43923
43924
43925
43926
43927
43928
43929
43930
43931
43932
43933
43934
43935
43936
43937
43938
43939
43940
43941
43942
43943
43944
43945
43946
43947
43948
43949
43950
43951
43952
43953
43954
43955
43956
43957
43958
43959
43960
43961
43962
43963
43964
43965
43966
43967
43968
43969
43970
43971
43972
43973
43974
43975
43976
43977
43978
43979
43980
43981
43982
43983
43984
43985
43986
43987
43988
43989
43990
43991
43992
43993
43994
43995
43996
43997
43998
43999
44000
44001
44002
44003
44004
44005
44006
44007
44008
44009
44010
44011
44012
44013
44014
44015
44016
44017
44018
44019
44020
44021
44022
44023
44024
44025
44026
44027
44028
44029
44030
44031
44032
44033
44034
44035
44036
44037
44038
44039
44040
44041
44042
44043
44044
44045
44046
44047
44048
44049
44050
44051
44052
44053
44054
44055
44056
44057
44058
44059
44060
44061
44062
44063
44064
44065
44066
44067
44068
44069
44070
44071
44072
44073
44074
44075
44076
44077
44078
44079
44080
44081
44082
44083
44084
44085
44086
44087
44088
44089
44090
44091
44092
44093
44094
44095
44096
44097
44098
44099
44100
44101
44102
44103
44104
44105
44106
44107
44108
44109
44110
44111
44112
44113
44114
44115
44116
44117
44118
44119
44120
44121
44122
44123
44124
44125
44126
44127
44128
44129
44130
44131
44132
44133
44134
44135
44136
44137
44138
44139
44140
44141
44142
44143
44144
44145
44146
44147
44148
44149
44150
44151
44152
44153
44154
44155
44156
44157
44158
44159
44160
44161
44162
44163
44164
44165
44166
44167
44168
44169
44170
44171
44172
44173
44174
44175
44176
44177
44178
44179
44180
44181
44182
44183
44184
44185
44186
44187
44188
44189
44190
44191
44192
44193
44194
44195
44196
44197
44198
44199
44200
44201
44202
44203
44204
44205
44206
44207
44208
44209
44210
44211
44212
44213
44214
44215
44216
44217
44218
44219
44220
44221
44222
44223
44224
44225
44226
44227
44228
44229
44230
44231
44232
44233
44234
44235
44236
44237
44238
44239
44240
44241
44242
44243
44244
44245
44246
44247
44248
44249
44250
44251
44252
44253
44254
44255
44256
44257
44258
44259
44260
44261
44262
44263
44264
44265
44266
44267
44268
44269
44270
44271
44272
44273
44274
44275
44276
44277
44278
44279
44280
44281
44282
44283
44284
44285
44286
44287
44288
44289
44290
44291
44292
44293
44294
44295
44296
44297
44298
44299
44300
44301
44302
44303
44304
44305
44306
44307
44308
44309
44310
44311
44312
44313
44314
44315
44316
44317
44318
44319
44320
44321
44322
44323
44324
44325
44326
44327
44328
44329
44330
44331
44332
44333
44334
44335
44336
44337
44338
44339
44340
44341
44342
44343
44344
44345
44346
44347
44348
44349
44350
44351
44352
44353
44354
44355
44356
44357
44358
44359
44360
44361
44362
44363
44364
44365
44366
44367
44368
44369
44370
44371
44372
44373
44374
44375
44376
44377
44378
44379
44380
44381
44382
44383
44384
44385
44386
44387
44388
44389
44390
44391
44392
44393
44394
44395
44396
44397
44398
44399
44400
44401
44402
44403
44404
44405
44406
44407
44408
44409
44410
44411
44412
44413
44414
44415
44416
44417
44418
44419
44420
44421
44422
44423
44424
44425
44426
44427
44428
44429
44430
44431
44432
44433
44434
44435
44436
44437
44438
44439
44440
44441
44442
44443
44444
44445
44446
44447
44448
44449
44450
44451
44452
44453
44454
44455
44456
44457
44458
44459
44460
44461
44462
44463
44464
44465
44466
44467
44468
44469
44470
44471
44472
44473
44474
44475
44476
44477
44478
44479
44480
44481
44482
44483
44484
44485
44486
44487
44488
44489
44490
44491
44492
44493
44494
44495
44496
44497
44498
44499
44500
44501
44502
44503
44504
44505
44506
44507
44508
44509
44510
44511
44512
44513
44514
44515
44516
44517
44518
44519
44520
44521
44522
44523
44524
44525
44526
44527
44528
44529
44530
44531
44532
44533
44534
44535
44536
44537
44538
44539
44540
44541
44542
44543
44544
44545
44546
44547
44548
44549
44550
44551
44552
44553
44554
44555
44556
44557
44558
44559
44560
44561
44562
44563
44564
44565
44566
44567
44568
44569
44570
44571
44572
44573
44574
44575
44576
44577
44578
44579
44580
44581
44582
44583
44584
44585
44586
44587
44588
44589
44590
44591
44592
44593
44594
44595
44596
44597
44598
44599
44600
44601
44602
44603
44604
44605
44606
44607
44608
44609
44610
44611
44612
44613
44614
44615
44616
44617
44618
44619
44620
44621
44622
44623
44624
44625
44626
44627
44628
44629
44630
44631
44632
44633
44634
44635
44636
44637
44638
44639
44640
44641
44642
44643
44644
44645
44646
44647
44648
44649
44650
44651
44652
44653
44654
44655
44656
44657
44658
44659
44660
44661
44662
44663
44664
44665
44666
44667
44668
44669
44670
44671
44672
44673
44674
44675
44676
44677
44678
44679
44680
44681
44682
44683
44684
44685
44686
44687
44688
44689
44690
44691
44692
44693
44694
44695
44696
44697
44698
44699
44700
44701
44702
44703
44704
44705
44706
44707
44708
44709
44710
44711
44712
44713
44714
44715
44716
44717
44718
44719
44720
44721
44722
44723
44724
44725
44726
44727
44728
44729
44730
44731
44732
44733
44734
44735
44736
44737
44738
44739
44740
44741
44742
44743
44744
44745
44746
44747
44748
44749
44750
44751
44752
44753
44754
44755
44756
44757
44758
44759
44760
44761
44762
44763
44764
44765
44766
44767
44768
44769
44770
44771
44772
44773
44774
44775
44776
44777
44778
44779
44780
44781
44782
44783
44784
44785
44786
44787
44788
44789
44790
44791
44792
44793
44794
44795
44796
44797
44798
44799
44800
44801
44802
44803
44804
44805
44806
44807
44808
44809
44810
44811
44812
44813
44814
44815
44816
44817
44818
44819
44820
44821
44822
44823
44824
44825
44826
44827
44828
44829
44830
44831
44832
44833
44834
44835
44836
44837
44838
44839
44840
44841
44842
44843
44844
44845
44846
44847
44848
44849
44850
44851
44852
44853
44854
44855
44856
44857
44858
44859
44860
44861
44862
44863
44864
44865
44866
44867
44868
44869
44870
44871
44872
44873
44874
44875
44876
44877
44878
44879
44880
44881
44882
44883
44884
44885
44886
44887
44888
44889
44890
44891
44892
44893
44894
44895
44896
44897
44898
44899
44900
44901
44902
44903
44904
44905
44906
44907
44908
44909
44910
44911
44912
44913
44914
44915
44916
44917
44918
44919
44920
44921
44922
44923
44924
44925
44926
44927
44928
44929
44930
44931
44932
44933
44934
44935
44936
44937
44938
44939
44940
44941
44942
44943
44944
44945
44946
44947
44948
44949
44950
44951
44952
44953
44954
44955
44956
44957
44958
44959
44960
44961
44962
44963
44964
44965
44966
44967
44968
44969
44970
44971
44972
44973
44974
44975
44976
44977
44978
44979
44980
44981
44982
44983
44984
44985
44986
44987
44988
44989
44990
44991
44992
44993
44994
44995
44996
44997
44998
44999
45000
45001
45002
45003
45004
45005
45006
45007
45008
45009
45010
45011
45012
45013
45014
45015
45016
45017
45018
45019
45020
45021
45022
45023
45024
45025
45026
45027
45028
45029
45030
45031
45032
45033
45034
45035
45036
45037
45038
45039
45040
45041
45042
45043
45044
45045
45046
45047
45048
45049
45050
45051
45052
45053
45054
45055
45056
45057
45058
45059
45060
45061
45062
45063
45064
45065
45066
45067
45068
45069
45070
45071
45072
45073
45074
45075
45076
45077
45078
45079
45080
45081
45082
45083
45084
45085
45086
45087
45088
45089
45090
45091
45092
45093
45094
45095
45096
45097
45098
45099
45100
45101
45102
45103
45104
45105
45106
45107
45108
45109
45110
45111
45112
45113
45114
45115
45116
45117
45118
45119
45120
45121
45122
45123
45124
45125
45126
45127
45128
45129
45130
45131
45132
45133
45134
45135
45136
45137
45138
45139
45140
45141
45142
45143
45144
45145
45146
45147
45148
45149
45150
45151
45152
45153
45154
45155
45156
45157
45158
45159
45160
45161
45162
45163
45164
45165
45166
45167
45168
45169
45170
45171
45172
45173
45174
45175
45176
45177
45178
45179
45180
45181
45182
45183
45184
45185
45186
45187
45188
45189
45190
45191
45192
45193
45194
45195
45196
45197
45198
45199
45200
45201
45202
45203
45204
45205
45206
45207
45208
45209
45210
45211
45212
45213
45214
45215
45216
45217
45218
45219
45220
45221
45222
45223
45224
45225
45226
45227
45228
45229
45230
45231
45232
45233
45234
45235
45236
45237
45238
45239
45240
45241
45242
45243
45244
45245
45246
45247
45248
45249
45250
45251
45252
45253
45254
45255
45256
45257
45258
45259
45260
45261
45262
45263
45264
45265
45266
45267
45268
45269
45270
45271
45272
45273
45274
45275
45276
45277
45278
45279
45280
45281
45282
45283
45284
45285
45286
45287
45288
45289
45290
45291
45292
45293
45294
45295
45296
45297
45298
45299
45300
45301
45302
45303
45304
45305
45306
45307
45308
45309
45310
45311
45312
45313
45314
45315
45316
45317
45318
45319
45320
45321
45322
45323
45324
45325
45326
45327
45328
45329
45330
45331
45332
45333
45334
45335
45336
45337
45338
45339
45340
45341
45342
45343
45344
45345
45346
45347
45348
45349
45350
45351
45352
45353
45354
45355
45356
45357
45358
45359
45360
45361
45362
45363
45364
45365
45366
45367
45368
45369
45370
45371
45372
45373
45374
45375
45376
45377
45378
45379
45380
45381
45382
45383
45384
45385
45386
45387
45388
45389
45390
45391
45392
45393
45394
45395
45396
45397
45398
45399
45400
45401
45402
45403
45404
45405
45406
45407
45408
45409
45410
45411
45412
45413
45414
45415
45416
45417
45418
45419
45420
45421
45422
45423
45424
45425
45426
45427
45428
45429
45430
45431
45432
45433
45434
45435
45436
45437
45438
45439
45440
45441
45442
45443
45444
45445
45446
45447
45448
45449
45450
45451
45452
45453
45454
45455
45456
45457
45458
45459
45460
45461
45462
45463
45464
45465
45466
45467
45468
45469
45470
45471
45472
45473
45474
45475
45476
45477
45478
45479
45480
45481
45482
45483
45484
45485
45486
45487
45488
45489
45490
45491
45492
45493
45494
45495
45496
45497
45498
45499
45500
45501
45502
45503
45504
45505
45506
45507
45508
45509
45510
45511
45512
45513
45514
45515
45516
45517
45518
45519
45520
45521
45522
45523
45524
45525
45526
45527
45528
45529
45530
45531
45532
45533
45534
45535
45536
45537
45538
45539
45540
45541
45542
45543
45544
45545
45546
45547
45548
45549
45550
45551
45552
45553
45554
45555
45556
45557
45558
45559
45560
45561
45562
45563
45564
45565
45566
45567
45568
45569
45570
45571
45572
45573
45574
45575
45576
45577
45578
45579
45580
45581
45582
45583
45584
45585
45586
45587
45588
45589
45590
45591
45592
45593
45594
45595
45596
45597
45598
45599
45600
45601
45602
45603
45604
45605
45606
45607
45608
45609
45610
45611
45612
45613
45614
45615
45616
45617
45618
45619
45620
45621
45622
45623
45624
45625
45626
45627
45628
45629
45630
45631
45632
45633
45634
45635
45636
45637
45638
45639
45640
45641
45642
45643
45644
45645
45646
45647
45648
45649
45650
45651
45652
45653
45654
45655
45656
45657
45658
45659
45660
45661
45662
45663
45664
45665
45666
45667
45668
45669
45670
45671
45672
45673
45674
45675
45676
45677
45678
45679
45680
45681
45682
45683
45684
45685
45686
45687
45688
45689
45690
45691
45692
45693
45694
45695
45696
45697
45698
45699
45700
45701
45702
45703
45704
45705
45706
45707
45708
45709
45710
45711
45712
45713
45714
45715
45716
45717
45718
45719
45720
45721
45722
45723
45724
45725
45726
45727
45728
45729
45730
45731
45732
45733
45734
45735
45736
45737
45738
45739
45740
45741
45742
45743
45744
45745
45746
45747
45748
45749
45750
45751
45752
45753
45754
45755
45756
45757
45758
45759
45760
45761
45762
45763
45764
45765
45766
45767
45768
45769
45770
45771
45772
45773
45774
45775
45776
45777
45778
45779
45780
45781
45782
45783
45784
45785
45786
45787
45788
45789
45790
45791
45792
45793
45794
45795
45796
45797
45798
45799
45800
45801
45802
45803
45804
45805
45806
45807
45808
45809
45810
45811
45812
45813
45814
45815
45816
45817
45818
45819
45820
45821
45822
45823
45824
45825
45826
45827
45828
45829
45830
45831
45832
45833
45834
45835
45836
45837
45838
45839
45840
45841
45842
45843
45844
45845
45846
45847
45848
45849
45850
45851
45852
45853
45854
45855
45856
45857
45858
45859
45860
45861
45862
45863
45864
45865
45866
45867
45868
45869
45870
45871
45872
45873
45874
45875
45876
45877
45878
45879
45880
45881
45882
45883
45884
45885
45886
45887
45888
45889
45890
45891
45892
45893
45894
45895
45896
45897
45898
45899
45900
45901
45902
45903
45904
45905
45906
45907
45908
45909
45910
45911
45912
45913
45914
45915
45916
45917
45918
45919
45920
45921
45922
45923
45924
45925
45926
45927
45928
45929
45930
45931
45932
45933
45934
45935
45936
45937
45938
45939
45940
45941
45942
45943
45944
45945
45946
45947
45948
45949
45950
45951
45952
45953
45954
45955
45956
45957
45958
45959
45960
45961
45962
45963
45964
45965
45966
45967
45968
45969
45970
45971
45972
45973
45974
45975
45976
45977
45978
45979
45980
45981
45982
45983
45984
45985
45986
45987
45988
45989
45990
45991
45992
45993
45994
45995
45996
45997
45998
45999
46000
46001
46002
46003
46004
46005
46006
46007
46008
46009
46010
46011
46012
46013
46014
46015
46016
46017
46018
46019
46020
46021
46022
46023
46024
46025
46026
46027
46028
46029
46030
46031
46032
46033
46034
46035
46036
46037
46038
46039
46040
46041
46042
46043
46044
46045
46046
46047
46048
46049
46050
46051
46052
46053
46054
46055
46056
46057
46058
46059
46060
46061
46062
46063
46064
46065
46066
46067
46068
46069
46070
46071
46072
46073
46074
46075
46076
46077
46078
46079
46080
46081
46082
46083
46084
46085
46086
46087
46088
46089
46090
46091
46092
46093
46094
46095
46096
46097
46098
46099
46100
46101
46102
46103
46104
46105
46106
46107
46108
46109
46110
46111
46112
46113
46114
46115
46116
46117
46118
46119
46120
46121
46122
46123
46124
46125
46126
46127
46128
46129
46130
46131
46132
46133
46134
46135
46136
46137
46138
46139
46140
46141
46142
46143
46144
46145
46146
46147
46148
46149
46150
46151
46152
46153
46154
46155
46156
46157
46158
46159
46160
46161
46162
46163
46164
46165
46166
46167
46168
46169
46170
46171
46172
46173
46174
46175
46176
46177
46178
46179
46180
46181
46182
46183
46184
46185
46186
46187
46188
46189
46190
46191
46192
46193
46194
46195
46196
46197
46198
46199
46200
46201
46202
46203
46204
46205
46206
46207
46208
46209
46210
46211
46212
46213
46214
46215
46216
46217
46218
46219
46220
46221
46222
46223
46224
46225
46226
46227
46228
46229
46230
46231
46232
46233
46234
46235
46236
46237
46238
46239
46240
46241
46242
46243
46244
46245
46246
46247
46248
46249
46250
46251
46252
46253
46254
46255
46256
46257
46258
46259
46260
46261
46262
46263
46264
46265
46266
46267
46268
46269
46270
46271
46272
46273
46274
46275
46276
46277
46278
46279
46280
46281
46282
46283
46284
46285
46286
46287
46288
46289
46290
46291
46292
46293
46294
46295
46296
46297
46298
46299
46300
46301
46302
46303
46304
46305
46306
46307
46308
46309
46310
46311
46312
46313
46314
46315
46316
46317
46318
46319
46320
46321
46322
46323
46324
46325
46326
46327
46328
46329
46330
46331
46332
46333
46334
46335
46336
46337
46338
46339
46340
46341
46342
46343
46344
46345
46346
46347
46348
46349
46350
46351
46352
46353
46354
46355
46356
46357
46358
46359
46360
46361
46362
46363
46364
46365
46366
46367
46368
46369
46370
46371
46372
46373
46374
46375
46376
46377
46378
46379
46380
46381
46382
46383
46384
46385
46386
46387
46388
46389
46390
46391
46392
46393
46394
46395
46396
46397
46398
46399
46400
46401
46402
46403
46404
46405
46406
46407
46408
46409
46410
46411
46412
46413
46414
46415
46416
46417
46418
46419
46420
46421
46422
46423
46424
46425
46426
46427
46428
46429
46430
46431
46432
46433
46434
46435
46436
46437
46438
46439
46440
46441
46442
46443
46444
46445
46446
46447
46448
46449
46450
46451
46452
46453
46454
46455
46456
46457
46458
46459
46460
46461
46462
46463
46464
46465
46466
46467
46468
46469
46470
46471
46472
46473
46474
46475
46476
46477
46478
46479
46480
46481
46482
46483
46484
46485
46486
46487
46488
46489
46490
46491
46492
46493
46494
46495
46496
46497
46498
46499
46500
46501
46502
46503
46504
46505
46506
46507
46508
46509
46510
46511
46512
46513
46514
46515
46516
46517
46518
46519
46520
46521
46522
46523
46524
46525
46526
46527
46528
46529
46530
46531
46532
46533
46534
46535
46536
46537
46538
46539
46540
46541
46542
46543
46544
46545
46546
46547
46548
46549
46550
46551
46552
46553
46554
46555
46556
46557
46558
46559
46560
46561
46562
46563
46564
46565
46566
46567
46568
46569
46570
46571
46572
46573
46574
46575
46576
46577
46578
46579
46580
46581
46582
46583
46584
46585
46586
46587
46588
46589
46590
46591
46592
46593
46594
46595
46596
46597
46598
46599
46600
46601
46602
46603
46604
46605
46606
46607
46608
46609
46610
46611
46612
46613
46614
46615
46616
46617
46618
46619
46620
46621
46622
46623
46624
46625
46626
46627
46628
46629
46630
46631
46632
46633
46634
46635
46636
46637
46638
46639
46640
46641
46642
46643
46644
46645
46646
46647
46648
46649
46650
46651
46652
46653
46654
46655
46656
46657
46658
46659
46660
46661
46662
46663
46664
46665
46666
46667
46668
46669
46670
46671
46672
46673
46674
46675
46676
46677
46678
46679
46680
46681
46682
46683
46684
46685
46686
46687
46688
46689
46690
46691
46692
46693
46694
46695
46696
46697
46698
46699
46700
46701
46702
46703
46704
46705
46706
46707
46708
46709
46710
46711
46712
46713
46714
46715
46716
46717
46718
46719
46720
46721
46722
46723
46724
46725
46726
46727
46728
46729
46730
46731
46732
46733
46734
46735
46736
46737
46738
46739
46740
46741
46742
46743
46744
46745
46746
46747
46748
46749
46750
46751
46752
46753
46754
46755
46756
46757
46758
46759
46760
46761
46762
46763
46764
46765
46766
46767
46768
46769
46770
46771
46772
46773
46774
46775
46776
46777
46778
46779
46780
46781
46782
46783
46784
46785
46786
46787
46788
46789
46790
46791
46792
46793
46794
46795
46796
46797
46798
46799
46800
46801
46802
46803
46804
46805
46806
46807
46808
46809
46810
46811
46812
46813
46814
46815
46816
46817
46818
46819
46820
46821
46822
46823
46824
46825
46826
46827
46828
46829
46830
46831
46832
46833
46834
46835
46836
46837
46838
46839
46840
46841
46842
46843
46844
46845
46846
46847
46848
46849
46850
46851
46852
46853
46854
46855
46856
46857
46858
46859
46860
46861
46862
46863
46864
46865
46866
46867
46868
46869
46870
46871
46872
46873
46874
46875
46876
46877
46878
46879
46880
46881
46882
46883
46884
46885
46886
46887
46888
46889
46890
46891
46892
46893
46894
46895
46896
46897
46898
46899
46900
46901
46902
46903
46904
46905
46906
46907
46908
46909
46910
46911
46912
46913
46914
46915
46916
46917
46918
46919
46920
46921
46922
46923
46924
46925
46926
46927
46928
46929
46930
46931
46932
46933
46934
46935
46936
46937
46938
46939
46940
46941
46942
46943
46944
46945
46946
46947
46948
46949
46950
46951
46952
46953
46954
46955
46956
46957
46958
46959
46960
46961
46962
46963
46964
46965
46966
46967
46968
46969
46970
46971
46972
46973
46974
46975
46976
46977
46978
46979
46980
46981
46982
46983
46984
46985
46986
46987
46988
46989
46990
46991
46992
46993
46994
46995
46996
46997
46998
46999
47000
47001
47002
47003
47004
47005
47006
47007
47008
47009
47010
47011
47012
47013
47014
47015
47016
47017
47018
47019
47020
47021
47022
47023
47024
47025
47026
47027
47028
47029
47030
47031
47032
47033
47034
47035
47036
47037
47038
47039
47040
47041
47042
47043
47044
47045
47046
47047
47048
47049
47050
47051
47052
47053
47054
47055
47056
47057
47058
47059
47060
47061
47062
47063
47064
47065
47066
47067
47068
47069
47070
47071
47072
47073
47074
47075
47076
47077
47078
47079
47080
47081
47082
47083
47084
47085
47086
47087
47088
47089
47090
47091
47092
47093
47094
47095
47096
47097
47098
47099
47100
47101
47102
47103
47104
47105
47106
47107
47108
47109
47110
47111
47112
47113
47114
47115
47116
47117
47118
47119
47120
47121
47122
47123
47124
47125
47126
47127
47128
47129
47130
47131
47132
47133
47134
47135
47136
47137
47138
47139
47140
47141
47142
47143
47144
47145
47146
47147
47148
47149
47150
47151
47152
47153
47154
47155
47156
47157
47158
47159
47160
47161
47162
47163
47164
47165
47166
47167
47168
47169
47170
47171
47172
47173
47174
47175
47176
47177
47178
47179
47180
47181
47182
47183
47184
47185
47186
47187
47188
47189
47190
47191
47192
47193
47194
47195
47196
47197
47198
47199
47200
47201
47202
47203
47204
47205
47206
47207
47208
47209
47210
47211
47212
47213
47214
47215
47216
47217
47218
47219
47220
47221
47222
47223
47224
47225
47226
47227
47228
47229
47230
47231
47232
47233
47234
47235
47236
47237
47238
47239
47240
47241
47242
47243
47244
47245
47246
47247
47248
47249
47250
47251
47252
47253
47254
47255
47256
47257
47258
47259
47260
47261
47262
47263
47264
47265
47266
47267
47268
47269
47270
47271
47272
47273
47274
47275
47276
47277
47278
47279
47280
47281
47282
47283
47284
47285
47286
47287
47288
47289
47290
47291
47292
47293
47294
47295
47296
47297
47298
47299
47300
47301
47302
47303
47304
47305
47306
47307
47308
47309
47310
47311
47312
47313
47314
47315
47316
47317
47318
47319
47320
47321
47322
47323
47324
47325
47326
47327
47328
47329
47330
47331
47332
47333
47334
47335
47336
47337
47338
47339
47340
47341
47342
47343
47344
47345
47346
47347
47348
47349
47350
47351
47352
47353
47354
47355
47356
47357
47358
47359
47360
47361
47362
47363
47364
47365
47366
47367
47368
47369
47370
47371
47372
47373
47374
47375
47376
47377
47378
47379
47380
47381
47382
47383
47384
47385
47386
47387
47388
47389
47390
47391
47392
47393
47394
47395
47396
47397
47398
47399
47400
47401
47402
47403
47404
47405
47406
47407
47408
47409
47410
47411
47412
47413
47414
47415
47416
47417
47418
47419
47420
47421
47422
47423
47424
47425
47426
47427
47428
47429
47430
47431
47432
47433
47434
47435
47436
47437
47438
47439
47440
47441
47442
47443
47444
47445
47446
47447
47448
47449
47450
47451
47452
47453
47454
47455
47456
47457
47458
47459
47460
47461
47462
47463
47464
47465
47466
47467
47468
47469
47470
47471
47472
47473
47474
47475
47476
47477
47478
47479
47480
47481
47482
47483
47484
47485
47486
47487
47488
47489
47490
47491
47492
47493
47494
47495
47496
47497
47498
47499
47500
47501
47502
47503
47504
47505
47506
47507
47508
47509
47510
47511
47512
47513
47514
47515
47516
47517
47518
47519
47520
47521
47522
47523
47524
47525
47526
47527
47528
47529
47530
47531
47532
47533
47534
47535
47536
47537
47538
47539
47540
47541
47542
47543
47544
47545
47546
47547
47548
47549
47550
47551
47552
47553
47554
47555
47556
47557
47558
47559
47560
47561
47562
47563
47564
47565
47566
47567
47568
47569
47570
47571
47572
47573
47574
47575
47576
47577
47578
47579
47580
47581
47582
47583
47584
47585
47586
47587
47588
47589
47590
47591
47592
47593
47594
47595
47596
47597
47598
47599
47600
47601
47602
47603
47604
47605
47606
47607
47608
47609
47610
47611
47612
47613
47614
47615
47616
47617
47618
47619
47620
47621
47622
47623
47624
47625
47626
47627
47628
47629
47630
47631
47632
47633
47634
47635
47636
47637
47638
47639
47640
47641
47642
47643
47644
47645
47646
47647
47648
47649
47650
47651
47652
47653
47654
47655
47656
47657
47658
47659
47660
47661
47662
47663
47664
47665
47666
47667
47668
47669
47670
47671
47672
47673
47674
47675
47676
47677
47678
47679
47680
47681
47682
47683
47684
47685
47686
47687
47688
47689
47690
47691
47692
47693
47694
47695
47696
47697
47698
47699
47700
47701
47702
47703
47704
47705
47706
47707
47708
47709
47710
47711
47712
47713
47714
47715
47716
47717
47718
47719
47720
47721
47722
47723
47724
47725
47726
47727
47728
47729
47730
47731
47732
47733
47734
47735
47736
47737
47738
47739
47740
47741
47742
47743
47744
47745
47746
47747
47748
47749
47750
47751
47752
47753
47754
47755
47756
47757
47758
47759
47760
47761
47762
47763
47764
47765
47766
47767
47768
47769
47770
47771
47772
47773
47774
47775
47776
47777
47778
47779
47780
47781
47782
47783
47784
47785
47786
47787
47788
47789
47790
47791
47792
47793
47794
47795
47796
47797
47798
47799
47800
47801
47802
47803
47804
47805
47806
47807
47808
47809
47810
47811
47812
47813
47814
47815
47816
47817
47818
47819
47820
47821
47822
47823
47824
47825
47826
47827
47828
47829
47830
47831
47832
47833
47834
47835
47836
47837
47838
47839
47840
47841
47842
47843
47844
47845
47846
47847
47848
47849
47850
47851
47852
47853
47854
47855
47856
47857
47858
47859
47860
47861
47862
47863
47864
47865
47866
47867
47868
47869
47870
47871
47872
47873
47874
47875
47876
47877
47878
47879
47880
47881
47882
47883
47884
47885
47886
47887
47888
47889
47890
47891
47892
47893
47894
47895
47896
47897
47898
47899
47900
47901
47902
47903
47904
47905
47906
47907
47908
47909
47910
47911
47912
47913
47914
47915
47916
47917
47918
47919
47920
47921
47922
47923
47924
47925
47926
47927
47928
47929
47930
47931
47932
47933
47934
47935
47936
47937
47938
47939
47940
47941
47942
47943
47944
47945
47946
47947
47948
47949
47950
47951
47952
47953
47954
47955
47956
47957
47958
47959
47960
47961
47962
47963
47964
47965
47966
47967
47968
47969
47970
47971
47972
47973
47974
47975
47976
47977
47978
47979
47980
47981
47982
47983
47984
47985
47986
47987
47988
47989
47990
47991
47992
47993
47994
47995
47996
47997
47998
47999
48000
48001
48002
48003
48004
48005
48006
48007
48008
48009
48010
48011
48012
48013
48014
48015
48016
48017
48018
48019
48020
48021
48022
48023
48024
48025
48026
48027
48028
48029
48030
48031
48032
48033
48034
48035
48036
48037
48038
48039
48040
48041
48042
48043
48044
48045
48046
48047
48048
48049
48050
48051
48052
48053
48054
48055
48056
48057
48058
48059
48060
48061
48062
48063
48064
48065
48066
48067
48068
48069
48070
48071
48072
48073
48074
48075
48076
48077
48078
48079
48080
48081
48082
48083
48084
48085
48086
48087
48088
48089
48090
48091
48092
48093
48094
48095
48096
48097
48098
48099
48100
48101
48102
48103
48104
48105
48106
48107
48108
48109
48110
48111
48112
48113
48114
48115
48116
48117
48118
48119
48120
48121
48122
48123
48124
48125
48126
48127
48128
48129
48130
48131
48132
48133
48134
48135
48136
48137
48138
48139
48140
48141
48142
48143
48144
48145
48146
48147
48148
48149
48150
48151
48152
48153
48154
48155
48156
48157
48158
48159
48160
48161
48162
48163
48164
48165
48166
48167
48168
48169
48170
48171
48172
48173
48174
48175
48176
48177
48178
48179
48180
48181
48182
48183
48184
48185
48186
48187
48188
48189
48190
48191
48192
48193
48194
48195
48196
48197
48198
48199
48200
48201
48202
48203
48204
48205
48206
48207
48208
48209
48210
48211
48212
48213
48214
48215
48216
48217
48218
48219
48220
48221
48222
48223
48224
48225
48226
48227
48228
48229
48230
48231
48232
48233
48234
48235
48236
48237
48238
48239
48240
48241
48242
48243
48244
48245
48246
48247
48248
48249
48250
48251
48252
48253
48254
48255
48256
48257
48258
48259
48260
48261
48262
48263
48264
48265
48266
48267
48268
48269
48270
48271
48272
48273
48274
48275
48276
48277
48278
48279
48280
48281
48282
48283
48284
48285
48286
48287
48288
48289
48290
48291
48292
48293
48294
48295
48296
48297
48298
48299
48300
48301
48302
48303
48304
48305
48306
48307
48308
48309
48310
48311
48312
48313
48314
48315
48316
48317
48318
48319
48320
48321
48322
48323
48324
48325
48326
48327
48328
48329
48330
48331
48332
48333
48334
48335
48336
48337
48338
48339
48340
48341
48342
48343
48344
48345
48346
48347
48348
48349
48350
48351
48352
48353
48354
48355
48356
48357
48358
48359
48360
48361
48362
48363
48364
48365
48366
48367
48368
48369
48370
48371
48372
48373
48374
48375
48376
48377
48378
48379
48380
48381
48382
48383
48384
48385
48386
48387
48388
48389
48390
48391
48392
48393
48394
48395
48396
48397
48398
48399
48400
48401
48402
48403
48404
48405
48406
48407
48408
48409
48410
48411
48412
48413
48414
48415
48416
48417
48418
48419
48420
48421
48422
48423
48424
48425
48426
48427
48428
48429
48430
48431
48432
48433
48434
48435
48436
48437
48438
48439
48440
48441
48442
48443
48444
48445
48446
48447
48448
48449
48450
48451
48452
48453
48454
48455
48456
48457
48458
48459
48460
48461
48462
48463
48464
48465
48466
48467
48468
48469
48470
48471
48472
48473
48474
48475
48476
48477
48478
48479
48480
48481
48482
48483
48484
48485
48486
48487
48488
48489
48490
48491
48492
48493
48494
48495
48496
48497
48498
48499
48500
48501
48502
48503
48504
48505
48506
48507
48508
48509
48510
48511
48512
48513
48514
48515
48516
48517
48518
48519
48520
48521
48522
48523
48524
48525
48526
48527
48528
48529
48530
48531
48532
48533
48534
48535
48536
48537
48538
48539
48540
48541
48542
48543
48544
48545
48546
48547
48548
48549
48550
48551
48552
48553
48554
48555
48556
48557
48558
48559
48560
48561
48562
48563
48564
48565
48566
48567
48568
48569
48570
48571
48572
48573
48574
48575
48576
48577
48578
48579
48580
48581
48582
48583
48584
48585
48586
48587
48588
48589
48590
48591
48592
48593
48594
48595
48596
48597
48598
48599
48600
48601
48602
48603
48604
48605
48606
48607
48608
48609
48610
48611
48612
48613
48614
48615
48616
48617
48618
48619
48620
48621
48622
48623
48624
48625
48626
48627
48628
48629
48630
48631
48632
48633
48634
48635
48636
48637
48638
48639
48640
48641
48642
48643
48644
48645
48646
48647
48648
48649
48650
48651
48652
48653
48654
48655
48656
48657
48658
48659
48660
48661
48662
48663
48664
48665
48666
48667
48668
48669
48670
48671
48672
48673
48674
48675
48676
48677
48678
48679
48680
48681
48682
48683
48684
48685
48686
48687
48688
48689
48690
48691
48692
48693
48694
48695
48696
48697
48698
48699
48700
48701
48702
48703
48704
48705
48706
48707
48708
48709
48710
48711
48712
48713
48714
48715
48716
48717
48718
48719
48720
48721
48722
48723
48724
48725
48726
48727
48728
48729
48730
48731
48732
48733
48734
48735
48736
48737
48738
48739
48740
48741
48742
48743
48744
48745
48746
48747
48748
48749
48750
48751
48752
48753
48754
48755
48756
48757
48758
48759
48760
48761
48762
48763
48764
48765
48766
48767
48768
48769
48770
48771
48772
48773
48774
48775
48776
48777
48778
48779
48780
48781
48782
48783
48784
48785
48786
48787
48788
48789
48790
48791
48792
48793
48794
48795
48796
48797
48798
48799
48800
48801
48802
48803
48804
48805
48806
48807
48808
48809
48810
48811
48812
48813
48814
48815
48816
48817
48818
48819
48820
48821
48822
48823
48824
48825
48826
48827
48828
48829
48830
48831
48832
48833
48834
48835
48836
48837
48838
48839
48840
48841
48842
48843
48844
48845
48846
48847
48848
48849
48850
48851
48852
48853
48854
48855
48856
48857
48858
48859
48860
48861
48862
48863
48864
48865
48866
48867
48868
48869
48870
48871
48872
48873
48874
48875
48876
48877
48878
48879
48880
48881
48882
48883
48884
48885
48886
48887
48888
48889
48890
48891
48892
48893
48894
48895
48896
48897
48898
48899
48900
48901
48902
48903
48904
48905
48906
48907
48908
48909
48910
48911
48912
48913
48914
48915
48916
48917
48918
48919
48920
48921
48922
48923
48924
48925
48926
48927
48928
48929
48930
48931
48932
48933
48934
48935
48936
48937
48938
48939
48940
48941
48942
48943
48944
48945
48946
48947
48948
48949
48950
48951
48952
48953
48954
48955
48956
48957
48958
48959
48960
48961
48962
48963
48964
48965
48966
48967
48968
48969
48970
48971
48972
48973
48974
48975
48976
48977
48978
48979
48980
48981
48982
48983
48984
48985
48986
48987
48988
48989
48990
48991
48992
48993
48994
48995
48996
48997
48998
48999
49000
49001
49002
49003
49004
49005
49006
49007
49008
49009
49010
49011
49012
49013
49014
49015
49016
49017
49018
49019
49020
49021
49022
49023
49024
49025
49026
49027
49028
49029
49030
49031
49032
49033
49034
49035
49036
49037
49038
49039
49040
49041
49042
49043
49044
49045
49046
49047
49048
49049
49050
49051
49052
49053
49054
49055
49056
49057
49058
49059
49060
49061
49062
49063
49064
49065
49066
49067
49068
49069
49070
49071
49072
49073
49074
49075
49076
49077
49078
49079
49080
49081
49082
49083
49084
49085
49086
49087
49088
49089
49090
49091
49092
49093
49094
49095
49096
49097
49098
49099
49100
49101
49102
49103
49104
49105
49106
49107
49108
49109
49110
49111
49112
49113
49114
49115
49116
49117
49118
49119
49120
49121
49122
49123
49124
49125
49126
49127
49128
49129
49130
49131
49132
49133
49134
49135
49136
49137
49138
49139
49140
49141
49142
49143
49144
49145
49146
49147
49148
49149
49150
49151
49152
49153
49154
49155
49156
49157
49158
49159
49160
49161
49162
49163
49164
49165
49166
49167
49168
49169
49170
49171
49172
49173
49174
49175
49176
49177
49178
49179
49180
49181
49182
49183
49184
49185
49186
49187
49188
49189
49190
49191
49192
49193
49194
49195
49196
49197
49198
49199
49200
49201
49202
49203
49204
49205
49206
49207
49208
49209
49210
49211
49212
49213
49214
49215
49216
49217
49218
49219
49220
49221
49222
49223
49224
49225
49226
49227
49228
49229
49230
49231
49232
49233
49234
49235
49236
49237
49238
49239
49240
49241
49242
49243
49244
49245
49246
49247
49248
49249
49250
49251
49252
49253
49254
49255
49256
49257
49258
49259
49260
49261
49262
49263
49264
49265
49266
49267
49268
49269
49270
49271
49272
49273
49274
49275
49276
49277
49278
49279
49280
49281
49282
49283
49284
49285
49286
49287
49288
49289
49290
49291
49292
49293
49294
49295
49296
49297
49298
49299
49300
49301
49302
49303
49304
49305
49306
49307
49308
49309
49310
49311
49312
49313
49314
49315
49316
49317
49318
49319
49320
49321
49322
49323
49324
49325
49326
49327
49328
49329
49330
49331
49332
49333
49334
49335
49336
49337
49338
49339
49340
49341
49342
49343
49344
49345
49346
49347
49348
49349
49350
49351
49352
49353
49354
49355
49356
49357
49358
49359
49360
49361
49362
49363
49364
49365
49366
49367
49368
49369
49370
49371
49372
49373
49374
49375
49376
49377
49378
49379
49380
49381
49382
49383
49384
49385
49386
49387
49388
49389
49390
49391
49392
49393
49394
49395
49396
49397
49398
49399
49400
49401
49402
49403
49404
49405
49406
49407
49408
49409
49410
49411
49412
49413
49414
49415
49416
49417
49418
49419
49420
49421
49422
49423
49424
49425
49426
49427
49428
49429
49430
49431
49432
49433
49434
49435
49436
49437
49438
49439
49440
49441
49442
49443
49444
49445
49446
49447
49448
49449
49450
49451
49452
49453
49454
49455
49456
49457
49458
49459
49460
49461
49462
49463
49464
49465
49466
49467
49468
49469
49470
49471
49472
49473
49474
49475
49476
49477
49478
49479
49480
49481
49482
49483
49484
49485
49486
49487
49488
49489
49490
49491
49492
49493
49494
49495
49496
49497
49498
49499
49500
49501
49502
49503
49504
49505
49506
49507
49508
49509
49510
49511
49512
49513
49514
49515
49516
49517
49518
49519
49520
49521
49522
49523
49524
49525
49526
49527
49528
49529
49530
49531
49532
49533
49534
49535
49536
49537
49538
49539
49540
49541
49542
49543
49544
49545
49546
49547
49548
49549
49550
49551
49552
49553
49554
49555
49556
49557
49558
49559
49560
49561
49562
49563
49564
49565
49566
49567
49568
49569
49570
49571
49572
49573
49574
49575
49576
49577
49578
49579
49580
49581
49582
49583
49584
49585
49586
49587
49588
49589
49590
49591
49592
49593
49594
49595
49596
49597
49598
49599
49600
49601
49602
49603
49604
49605
49606
49607
49608
49609
49610
49611
49612
49613
49614
49615
49616
49617
49618
49619
49620
49621
49622
49623
49624
49625
49626
49627
49628
49629
49630
49631
49632
49633
49634
49635
49636
49637
49638
49639
49640
49641
49642
49643
49644
49645
49646
49647
49648
49649
49650
49651
49652
49653
49654
49655
49656
49657
49658
49659
49660
49661
49662
49663
49664
49665
49666
49667
49668
49669
49670
49671
49672
49673
49674
49675
49676
49677
49678
49679
49680
49681
49682
49683
49684
49685
49686
49687
49688
49689
49690
49691
49692
49693
49694
49695
49696
49697
49698
49699
49700
49701
49702
49703
49704
49705
49706
49707
49708
49709
49710
49711
49712
49713
49714
49715
49716
49717
49718
49719
49720
49721
49722
49723
49724
49725
49726
49727
49728
49729
49730
49731
49732
49733
49734
49735
49736
49737
49738
49739
49740
49741
49742
49743
49744
49745
49746
49747
49748
49749
49750
49751
49752
49753
49754
49755
49756
49757
49758
49759
49760
49761
49762
49763
49764
49765
49766
49767
49768
49769
49770
49771
49772
49773
49774
49775
49776
49777
49778
49779
49780
49781
49782
49783
49784
49785
49786
49787
49788
49789
49790
49791
49792
49793
49794
49795
49796
49797
49798
49799
49800
49801
49802
49803
49804
49805
49806
49807
49808
49809
49810
49811
49812
49813
49814
49815
49816
49817
49818
49819
49820
49821
49822
49823
49824
49825
49826
49827
49828
49829
49830
49831
49832
49833
49834
49835
49836
49837
49838
49839
49840
49841
49842
49843
49844
49845
49846
49847
49848
49849
49850
49851
49852
49853
49854
49855
49856
49857
49858
49859
49860
49861
49862
49863
49864
49865
49866
49867
49868
49869
49870
49871
49872
49873
49874
49875
49876
49877
49878
49879
49880
49881
49882
49883
49884
49885
49886
49887
49888
49889
49890
49891
49892
49893
49894
49895
49896
49897
49898
49899
49900
49901
49902
49903
49904
49905
49906
49907
49908
49909
49910
49911
49912
49913
49914
49915
49916
49917
49918
49919
49920
49921
49922
49923
49924
49925
49926
49927
49928
49929
49930
49931
49932
49933
49934
49935
49936
49937
49938
49939
49940
49941
49942
49943
49944
49945
49946
49947
49948
49949
49950
49951
49952
49953
49954
49955
49956
49957
49958
49959
49960
49961
49962
49963
49964
49965
49966
49967
49968
49969
49970
49971
49972
49973
49974
49975
49976
49977
49978
49979
49980
49981
49982
49983
49984
49985
49986
49987
49988
49989
49990
49991
49992
49993
49994
49995
49996
49997
49998
49999
50000
50001
50002
50003
50004
50005
50006
50007
50008
50009
50010
50011
50012
50013
50014
50015
50016
50017
50018
50019
50020
50021
50022
50023
50024
50025
50026
50027
50028
50029
50030
50031
50032
50033
50034
50035
50036
50037
50038
50039
50040
50041
50042
50043
50044
50045
50046
50047
50048
50049
50050
50051
50052
50053
50054
50055
50056
50057
50058
50059
50060
50061
50062
50063
50064
50065
50066
50067
50068
50069
50070
50071
50072
50073
50074
50075
50076
50077
50078
50079
50080
50081
50082
50083
50084
50085
50086
50087
50088
50089
50090
50091
50092
50093
50094
50095
50096
50097
50098
50099
50100
50101
50102
50103
50104
50105
50106
50107
50108
50109
50110
50111
50112
50113
50114
50115
50116
50117
50118
50119
50120
50121
50122
50123
50124
50125
50126
50127
50128
50129
50130
50131
50132
50133
50134
50135
50136
50137
50138
50139
50140
50141
50142
50143
50144
50145
50146
50147
50148
50149
50150
50151
50152
50153
50154
50155
50156
50157
50158
50159
50160
50161
50162
50163
50164
50165
50166
50167
50168
50169
50170
50171
50172
50173
50174
50175
50176
50177
50178
50179
50180
50181
50182
50183
50184
50185
50186
50187
50188
50189
50190
50191
50192
50193
50194
50195
50196
50197
50198
50199
50200
50201
50202
50203
50204
50205
50206
50207
50208
50209
50210
50211
50212
50213
50214
50215
50216
50217
50218
50219
50220
50221
50222
50223
50224
50225
50226
50227
50228
50229
50230
50231
50232
50233
50234
50235
50236
50237
50238
50239
50240
50241
50242
50243
50244
50245
50246
50247
50248
50249
50250
50251
50252
50253
50254
50255
50256
50257
50258
50259
50260
50261
50262
50263
50264
50265
50266
50267
50268
50269
50270
50271
50272
50273
50274
50275
50276
50277
50278
50279
50280
50281
50282
50283
50284
50285
50286
50287
50288
50289
50290
50291
50292
50293
50294
50295
50296
50297
50298
50299
50300
50301
50302
50303
50304
50305
50306
50307
50308
50309
50310
50311
50312
50313
50314
50315
50316
50317
50318
50319
50320
50321
50322
50323
50324
50325
50326
50327
50328
50329
50330
50331
50332
50333
50334
50335
50336
50337
50338
50339
50340
50341
50342
50343
50344
50345
50346
50347
50348
50349
50350
50351
50352
50353
50354
50355
50356
50357
50358
50359
50360
50361
50362
50363
50364
50365
50366
50367
50368
50369
50370
50371
50372
50373
50374
50375
50376
50377
50378
50379
50380
50381
50382
50383
50384
50385
50386
50387
50388
50389
50390
50391
50392
50393
50394
50395
50396
50397
50398
50399
50400
50401
50402
50403
50404
50405
50406
50407
50408
50409
50410
50411
50412
50413
50414
50415
50416
50417
50418
50419
50420
50421
50422
50423
50424
50425
50426
50427
50428
50429
50430
50431
50432
50433
50434
50435
50436
50437
50438
50439
50440
50441
50442
50443
50444
50445
50446
50447
50448
50449
50450
50451
50452
50453
50454
50455
50456
50457
50458
50459
50460
50461
50462
50463
50464
50465
50466
50467
50468
50469
50470
50471
50472
50473
50474
50475
50476
50477
50478
50479
50480
50481
50482
50483
50484
50485
50486
50487
50488
50489
50490
50491
50492
50493
50494
50495
50496
50497
50498
50499
50500
50501
50502
50503
50504
50505
50506
50507
50508
50509
50510
50511
50512
50513
50514
50515
50516
50517
50518
50519
50520
50521
50522
50523
50524
50525
50526
50527
50528
50529
50530
50531
50532
50533
50534
50535
50536
50537
50538
50539
50540
50541
50542
50543
50544
50545
50546
50547
50548
50549
50550
50551
50552
50553
50554
50555
50556
50557
50558
50559
50560
50561
50562
50563
50564
50565
50566
50567
50568
50569
50570
50571
50572
50573
50574
50575
50576
50577
50578
50579
50580
50581
50582
50583
50584
50585
50586
50587
50588
50589
50590
50591
50592
50593
50594
50595
50596
50597
50598
50599
50600
50601
50602
50603
50604
50605
50606
50607
50608
50609
50610
50611
50612
50613
50614
50615
50616
50617
50618
50619
50620
50621
50622
50623
50624
50625
50626
50627
50628
50629
50630
50631
50632
50633
50634
50635
50636
50637
50638
50639
50640
50641
50642
50643
50644
50645
50646
50647
50648
50649
50650
50651
50652
50653
50654
50655
50656
50657
50658
50659
50660
50661
50662
50663
50664
50665
50666
50667
50668
50669
50670
50671
50672
50673
50674
50675
50676
50677
50678
50679
50680
50681
50682
50683
50684
50685
50686
50687
50688
50689
50690
50691
50692
50693
50694
50695
50696
50697
50698
50699
50700
50701
50702
50703
50704
50705
50706
50707
50708
50709
50710
50711
50712
50713
50714
50715
50716
50717
50718
50719
50720
50721
50722
50723
50724
50725
50726
50727
50728
50729
50730
50731
50732
50733
50734
50735
50736
50737
50738
50739
50740
50741
50742
50743
50744
50745
50746
50747
50748
50749
50750
50751
50752
50753
50754
50755
50756
50757
50758
50759
50760
50761
50762
50763
50764
50765
50766
50767
50768
50769
50770
50771
50772
50773
50774
50775
50776
50777
50778
50779
50780
50781
50782
50783
50784
50785
50786
50787
50788
50789
50790
50791
50792
50793
50794
50795
50796
50797
50798
50799
50800
50801
50802
50803
50804
50805
50806
50807
50808
50809
50810
50811
50812
50813
50814
50815
50816
50817
50818
50819
50820
50821
50822
50823
50824
50825
50826
50827
50828
50829
50830
50831
50832
50833
50834
50835
50836
50837
50838
50839
50840
50841
50842
50843
50844
50845
50846
50847
50848
50849
50850
50851
50852
50853
50854
50855
50856
50857
50858
50859
50860
50861
50862
50863
50864
50865
50866
50867
50868
50869
50870
50871
50872
50873
50874
50875
50876
50877
50878
50879
50880
50881
50882
50883
50884
50885
50886
50887
50888
50889
50890
50891
50892
50893
50894
50895
50896
50897
50898
50899
50900
50901
50902
50903
50904
50905
50906
50907
50908
50909
50910
50911
50912
50913
50914
50915
50916
50917
50918
50919
50920
50921
50922
50923
50924
50925
50926
50927
50928
50929
50930
50931
50932
50933
50934
50935
50936
50937
50938
50939
50940
50941
50942
50943
50944
50945
50946
50947
50948
50949
50950
50951
50952
50953
50954
50955
50956
50957
50958
50959
50960
50961
50962
50963
50964
50965
50966
50967
50968
50969
50970
50971
50972
50973
50974
50975
50976
50977
50978
50979
50980
50981
50982
50983
50984
50985
50986
50987
50988
50989
50990
50991
50992
50993
50994
50995
50996
50997
50998
50999
51000
51001
51002
51003
51004
51005
51006
51007
51008
51009
51010
51011
51012
51013
51014
51015
51016
51017
51018
51019
51020
51021
51022
51023
51024
51025
51026
51027
51028
51029
51030
51031
51032
51033
51034
51035
51036
51037
51038
51039
51040
51041
51042
51043
51044
51045
51046
51047
51048
51049
51050
51051
51052
51053
51054
51055
51056
51057
51058
51059
51060
51061
51062
51063
51064
51065
51066
51067
51068
51069
51070
51071
51072
51073
51074
51075
51076
51077
51078
51079
51080
51081
51082
51083
51084
51085
51086
51087
51088
51089
51090
51091
51092
51093
51094
51095
51096
51097
51098
51099
51100
51101
51102
51103
51104
51105
51106
51107
51108
51109
51110
51111
51112
51113
51114
51115
51116
51117
51118
51119
51120
51121
51122
51123
51124
51125
51126
51127
51128
51129
51130
51131
51132
51133
51134
51135
51136
51137
51138
51139
51140
51141
51142
51143
51144
51145
51146
51147
51148
51149
51150
51151
51152
51153
51154
51155
51156
51157
51158
51159
51160
51161
51162
51163
51164
51165
51166
51167
51168
51169
51170
51171
51172
51173
51174
51175
51176
51177
51178
51179
51180
51181
51182
51183
51184
51185
51186
51187
51188
51189
51190
51191
51192
51193
51194
51195
51196
51197
51198
51199
51200
51201
51202
51203
51204
51205
51206
51207
51208
51209
51210
51211
51212
51213
51214
51215
51216
51217
51218
51219
51220
51221
51222
51223
51224
51225
51226
51227
51228
51229
51230
51231
51232
51233
51234
51235
51236
51237
51238
51239
51240
51241
51242
51243
51244
51245
51246
51247
51248
51249
51250
51251
51252
51253
51254
51255
51256
51257
51258
51259
51260
51261
51262
51263
51264
51265
51266
51267
51268
51269
51270
51271
51272
51273
51274
51275
51276
51277
51278
51279
51280
51281
51282
51283
51284
51285
51286
51287
51288
51289
51290
51291
51292
51293
51294
51295
51296
51297
51298
51299
51300
51301
51302
51303
51304
51305
51306
51307
51308
51309
51310
51311
51312
51313
51314
51315
51316
51317
51318
51319
51320
51321
51322
51323
51324
51325
51326
51327
51328
51329
51330
51331
51332
51333
51334
51335
51336
51337
51338
51339
51340
51341
51342
51343
51344
51345
51346
51347
51348
51349
51350
51351
51352
51353
51354
51355
51356
51357
51358
51359
51360
51361
51362
51363
51364
51365
51366
51367
51368
51369
51370
51371
51372
51373
51374
51375
51376
51377
51378
51379
51380
51381
51382
51383
51384
51385
51386
51387
51388
51389
51390
51391
51392
51393
51394
51395
51396
51397
51398
51399
51400
51401
51402
51403
51404
51405
51406
51407
51408
51409
51410
51411
51412
51413
51414
51415
51416
51417
51418
51419
51420
51421
51422
51423
51424
51425
51426
51427
51428
51429
51430
51431
51432
51433
51434
51435
51436
51437
51438
51439
51440
51441
51442
51443
51444
51445
51446
51447
51448
51449
51450
51451
51452
51453
51454
51455
51456
51457
51458
51459
51460
51461
51462
51463
51464
51465
51466
51467
51468
51469
51470
51471
51472
51473
51474
51475
51476
51477
51478
51479
51480
51481
51482
51483
51484
51485
51486
51487
51488
51489
51490
51491
51492
51493
51494
51495
51496
51497
51498
51499
51500
51501
51502
51503
51504
51505
51506
51507
51508
51509
51510
51511
51512
51513
51514
51515
51516
51517
51518
51519
51520
51521
51522
51523
51524
51525
51526
51527
51528
51529
51530
51531
51532
51533
51534
51535
51536
51537
51538
51539
51540
51541
51542
51543
51544
51545
51546
51547
51548
51549
51550
51551
51552
51553
51554
51555
51556
51557
51558
51559
51560
51561
51562
51563
51564
51565
51566
51567
51568
51569
51570
51571
51572
51573
51574
51575
51576
51577
51578
51579
51580
51581
51582
51583
51584
51585
51586
51587
51588
51589
51590
51591
51592
51593
51594
51595
51596
51597
51598
51599
51600
51601
51602
51603
51604
51605
51606
51607
51608
51609
51610
51611
51612
51613
51614
51615
51616
51617
51618
51619
51620
51621
51622
51623
51624
51625
51626
51627
51628
51629
51630
51631
51632
51633
51634
51635
51636
51637
51638
51639
51640
51641
51642
51643
51644
51645
51646
51647
51648
51649
51650
51651
51652
51653
51654
51655
51656
51657
51658
51659
51660
51661
51662
51663
51664
51665
51666
51667
51668
51669
51670
51671
51672
51673
51674
51675
51676
51677
51678
51679
51680
51681
51682
51683
51684
51685
51686
51687
51688
51689
51690
51691
51692
51693
51694
51695
51696
51697
51698
51699
51700
51701
51702
51703
51704
51705
51706
51707
51708
51709
51710
51711
51712
51713
51714
51715
51716
51717
51718
51719
51720
51721
51722
51723
51724
51725
51726
51727
51728
51729
51730
51731
51732
51733
51734
51735
51736
51737
51738
51739
51740
51741
51742
51743
51744
51745
51746
51747
51748
51749
51750
51751
51752
51753
51754
51755
51756
51757
51758
51759
51760
51761
51762
51763
51764
51765
51766
51767
51768
51769
51770
51771
51772
51773
51774
51775
51776
51777
51778
51779
51780
51781
51782
51783
51784
51785
51786
51787
51788
51789
51790
51791
51792
51793
51794
51795
51796
51797
51798
51799
51800
51801
51802
51803
51804
51805
51806
51807
51808
51809
51810
51811
51812
51813
51814
51815
51816
51817
51818
51819
51820
51821
51822
51823
51824
51825
51826
51827
51828
51829
51830
51831
51832
51833
51834
51835
51836
51837
51838
51839
51840
51841
51842
51843
51844
51845
51846
51847
51848
51849
51850
51851
51852
51853
51854
51855
51856
51857
51858
51859
51860
51861
51862
51863
51864
51865
51866
51867
51868
51869
51870
51871
51872
51873
51874
51875
51876
51877
51878
51879
51880
51881
51882
51883
51884
51885
51886
51887
51888
51889
51890
51891
51892
51893
51894
51895
51896
51897
51898
51899
51900
51901
51902
51903
51904
51905
51906
51907
51908
51909
51910
51911
51912
51913
51914
51915
51916
51917
51918
51919
51920
51921
51922
51923
51924
51925
51926
51927
51928
51929
51930
51931
51932
51933
51934
51935
51936
51937
51938
51939
51940
51941
51942
51943
51944
51945
51946
51947
51948
51949
51950
51951
51952
51953
51954
51955
51956
51957
51958
51959
51960
51961
51962
51963
51964
51965
51966
51967
51968
51969
51970
51971
51972
51973
51974
51975
51976
51977
51978
51979
51980
51981
51982
51983
51984
51985
51986
51987
51988
51989
51990
51991
51992
51993
51994
51995
51996
51997
51998
51999
52000
52001
52002
52003
52004
52005
52006
52007
52008
52009
52010
52011
52012
52013
52014
52015
52016
52017
52018
52019
52020
52021
52022
52023
52024
52025
52026
52027
52028
52029
52030
52031
52032
52033
52034
52035
52036
52037
52038
52039
52040
52041
52042
52043
52044
52045
52046
52047
52048
52049
52050
52051
52052
52053
52054
52055
52056
52057
52058
52059
52060
52061
52062
52063
52064
52065
52066
52067
52068
52069
52070
52071
52072
52073
52074
52075
52076
52077
52078
52079
52080
52081
52082
52083
52084
52085
52086
52087
52088
52089
52090
52091
52092
52093
52094
52095
52096
52097
52098
52099
52100
52101
52102
52103
52104
52105
52106
52107
52108
52109
52110
52111
52112
52113
52114
52115
52116
52117
52118
52119
52120
52121
52122
52123
52124
52125
52126
52127
52128
52129
52130
52131
52132
52133
52134
52135
52136
52137
52138
52139
52140
52141
52142
52143
52144
52145
52146
52147
52148
52149
52150
52151
52152
52153
52154
52155
52156
52157
52158
52159
52160
52161
52162
52163
52164
52165
52166
52167
52168
52169
52170
52171
52172
52173
52174
52175
52176
52177
52178
52179
52180
52181
52182
52183
52184
52185
52186
52187
52188
52189
52190
52191
52192
52193
52194
52195
52196
52197
52198
52199
52200
52201
52202
52203
52204
52205
52206
52207
52208
52209
52210
52211
52212
52213
52214
52215
52216
52217
52218
52219
52220
52221
52222
52223
52224
52225
52226
52227
52228
52229
52230
52231
52232
52233
52234
52235
52236
52237
52238
52239
52240
52241
52242
52243
52244
52245
52246
52247
52248
52249
52250
52251
52252
52253
52254
52255
52256
52257
52258
52259
52260
52261
52262
52263
52264
52265
52266
52267
52268
52269
52270
52271
52272
52273
52274
52275
52276
52277
52278
52279
52280
52281
52282
52283
52284
52285
52286
52287
52288
52289
52290
52291
52292
52293
52294
52295
52296
52297
52298
52299
52300
52301
52302
52303
52304
52305
52306
52307
52308
52309
52310
52311
52312
52313
52314
52315
52316
52317
52318
52319
52320
52321
52322
52323
52324
52325
52326
52327
52328
52329
52330
52331
52332
52333
52334
52335
52336
52337
52338
52339
52340
52341
52342
52343
52344
52345
52346
52347
52348
52349
52350
52351
52352
52353
52354
52355
52356
52357
52358
52359
52360
52361
52362
52363
52364
52365
52366
52367
52368
52369
52370
52371
52372
52373
52374
52375
52376
52377
52378
52379
52380
52381
52382
52383
52384
52385
52386
52387
52388
52389
52390
52391
52392
52393
52394
52395
52396
52397
52398
52399
52400
52401
52402
52403
52404
52405
52406
52407
52408
52409
52410
52411
52412
52413
52414
52415
52416
52417
52418
52419
52420
52421
52422
52423
52424
52425
52426
52427
52428
52429
52430
52431
52432
52433
52434
52435
52436
52437
52438
52439
52440
52441
52442
52443
52444
52445
52446
52447
52448
52449
52450
52451
52452
52453
52454
52455
52456
52457
52458
52459
52460
52461
52462
52463
52464
52465
52466
52467
52468
52469
52470
52471
52472
52473
52474
52475
52476
52477
52478
52479
52480
52481
52482
52483
52484
52485
52486
52487
52488
52489
52490
52491
52492
52493
52494
52495
52496
52497
52498
52499
52500
52501
52502
52503
52504
52505
52506
52507
52508
52509
52510
52511
52512
52513
52514
52515
52516
52517
52518
52519
52520
52521
52522
52523
52524
52525
52526
52527
52528
52529
52530
52531
52532
52533
52534
52535
52536
52537
52538
52539
52540
52541
52542
52543
52544
52545
52546
52547
52548
52549
52550
52551
52552
52553
52554
52555
52556
52557
52558
52559
52560
52561
52562
52563
52564
52565
52566
52567
52568
52569
52570
52571
52572
52573
52574
52575
52576
52577
52578
52579
52580
52581
52582
52583
52584
52585
52586
52587
52588
52589
52590
52591
52592
52593
52594
52595
52596
52597
52598
52599
52600
52601
52602
52603
52604
52605
52606
52607
52608
52609
52610
52611
52612
52613
52614
52615
52616
52617
52618
52619
52620
52621
52622
52623
52624
52625
52626
52627
52628
52629
52630
52631
52632
52633
52634
52635
52636
52637
52638
52639
52640
52641
52642
52643
52644
52645
52646
52647
52648
52649
52650
52651
52652
52653
52654
52655
52656
52657
52658
52659
52660
52661
52662
52663
52664
52665
52666
52667
52668
52669
52670
52671
52672
52673
52674
52675
52676
52677
52678
52679
52680
52681
52682
52683
52684
52685
52686
52687
52688
52689
52690
52691
52692
52693
52694
52695
52696
52697
52698
52699
52700
52701
52702
52703
52704
52705
52706
52707
52708
52709
52710
52711
52712
52713
52714
52715
52716
52717
52718
52719
52720
52721
52722
52723
52724
52725
52726
52727
52728
52729
52730
52731
52732
52733
52734
52735
52736
52737
52738
52739
52740
52741
52742
52743
52744
52745
52746
52747
52748
52749
52750
52751
52752
52753
52754
52755
52756
52757
52758
52759
52760
52761
52762
52763
52764
52765
52766
52767
52768
52769
52770
52771
52772
52773
52774
52775
52776
52777
52778
52779
52780
52781
52782
52783
52784
52785
52786
52787
52788
52789
52790
52791
52792
52793
52794
52795
52796
52797
52798
52799
52800
52801
52802
52803
52804
52805
52806
52807
52808
52809
52810
52811
52812
52813
52814
52815
52816
52817
52818
52819
52820
52821
52822
52823
52824
52825
52826
52827
52828
52829
52830
52831
52832
52833
52834
52835
52836
52837
52838
52839
52840
52841
52842
52843
52844
52845
52846
52847
52848
52849
52850
52851
52852
52853
52854
52855
52856
52857
52858
52859
52860
52861
52862
52863
52864
52865
52866
52867
52868
52869
52870
52871
52872
52873
52874
52875
52876
52877
52878
52879
52880
52881
52882
52883
52884
52885
52886
52887
52888
52889
52890
52891
52892
52893
52894
52895
52896
52897
52898
52899
52900
52901
52902
52903
52904
52905
52906
52907
52908
52909
52910
52911
52912
52913
52914
52915
52916
52917
52918
52919
52920
52921
52922
52923
52924
52925
52926
52927
52928
52929
52930
52931
52932
52933
52934
52935
52936
52937
52938
52939
52940
52941
52942
52943
52944
52945
52946
52947
52948
52949
52950
52951
52952
52953
52954
52955
52956
52957
52958
52959
52960
52961
52962
52963
52964
52965
52966
52967
52968
52969
52970
52971
52972
52973
52974
52975
52976
52977
52978
52979
52980
52981
52982
52983
52984
52985
52986
52987
52988
52989
52990
52991
52992
52993
52994
52995
52996
52997
52998
52999
53000
53001
53002
53003
53004
53005
53006
53007
53008
53009
53010
53011
53012
53013
53014
53015
53016
53017
53018
53019
53020
53021
53022
53023
53024
53025
53026
53027
53028
53029
53030
53031
53032
53033
53034
53035
53036
53037
53038
53039
53040
53041
53042
53043
53044
53045
53046
53047
53048
53049
53050
53051
53052
53053
53054
53055
53056
53057
53058
53059
53060
53061
53062
53063
53064
53065
53066
53067
53068
53069
53070
53071
53072
53073
53074
53075
53076
53077
53078
53079
53080
53081
53082
53083
53084
53085
53086
53087
53088
53089
53090
53091
53092
53093
53094
53095
53096
53097
53098
53099
53100
53101
53102
53103
53104
53105
53106
53107
53108
53109
53110
53111
53112
53113
53114
53115
53116
53117
53118
53119
53120
53121
53122
53123
53124
53125
53126
53127
53128
53129
53130
53131
53132
53133
53134
53135
53136
53137
53138
53139
53140
53141
53142
53143
53144
53145
53146
53147
53148
53149
53150
53151
53152
53153
53154
53155
53156
53157
53158
53159
53160
53161
53162
53163
53164
53165
53166
53167
53168
53169
53170
53171
53172
53173
53174
53175
53176
53177
53178
53179
53180
53181
53182
53183
53184
53185
53186
53187
53188
53189
53190
53191
53192
53193
53194
53195
53196
53197
53198
53199
53200
53201
53202
53203
53204
53205
53206
53207
53208
53209
53210
53211
53212
53213
53214
53215
53216
53217
53218
53219
53220
53221
53222
53223
53224
53225
53226
53227
53228
53229
53230
53231
53232
53233
53234
53235
53236
53237
53238
53239
53240
53241
53242
53243
53244
53245
53246
53247
53248
53249
53250
53251
53252
53253
53254
53255
53256
53257
53258
53259
53260
53261
53262
53263
53264
53265
53266
53267
53268
53269
53270
53271
53272
53273
53274
53275
53276
53277
53278
53279
53280
53281
53282
53283
53284
53285
53286
53287
53288
53289
53290
53291
53292
53293
53294
53295
53296
53297
53298
53299
53300
53301
53302
53303
53304
53305
53306
53307
53308
53309
53310
53311
53312
53313
53314
53315
53316
53317
53318
53319
53320
53321
53322
53323
53324
53325
53326
53327
53328
53329
53330
53331
53332
53333
53334
53335
53336
53337
53338
53339
53340
53341
53342
53343
53344
53345
53346
53347
53348
53349
53350
53351
53352
53353
53354
53355
53356
53357
53358
53359
53360
53361
53362
53363
53364
53365
53366
53367
53368
53369
53370
53371
53372
53373
53374
53375
53376
53377
53378
53379
53380
53381
53382
53383
53384
53385
53386
53387
53388
53389
53390
53391
53392
53393
53394
53395
53396
53397
53398
53399
53400
53401
53402
53403
53404
53405
53406
53407
53408
53409
53410
53411
53412
53413
53414
53415
53416
53417
53418
53419
53420
53421
53422
53423
53424
53425
53426
53427
53428
53429
53430
53431
53432
53433
53434
53435
53436
53437
53438
53439
53440
53441
53442
53443
53444
53445
53446
53447
53448
53449
53450
53451
53452
53453
53454
53455
53456
53457
53458
53459
53460
53461
53462
53463
53464
53465
53466
53467
53468
53469
53470
53471
53472
53473
53474
53475
53476
53477
53478
53479
53480
53481
53482
53483
53484
53485
53486
53487
53488
53489
53490
53491
53492
53493
53494
53495
53496
53497
53498
53499
53500
53501
53502
53503
53504
53505
53506
53507
53508
53509
53510
53511
53512
53513
53514
53515
53516
53517
53518
53519
53520
53521
53522
53523
53524
53525
53526
53527
53528
53529
53530
53531
53532
53533
53534
53535
53536
53537
53538
53539
53540
53541
53542
53543
53544
53545
53546
53547
53548
53549
53550
53551
53552
53553
53554
53555
53556
53557
53558
53559
53560
53561
53562
53563
53564
53565
53566
53567
53568
53569
53570
53571
53572
53573
53574
53575
53576
53577
53578
53579
53580
53581
53582
53583
53584
53585
53586
53587
53588
53589
53590
53591
53592
53593
53594
53595
53596
53597
53598
53599
53600
53601
53602
53603
53604
53605
53606
53607
53608
53609
53610
53611
53612
53613
53614
53615
53616
53617
53618
53619
53620
53621
53622
53623
53624
53625
53626
53627
53628
53629
53630
53631
53632
53633
53634
53635
53636
53637
53638
53639
53640
53641
53642
53643
53644
53645
53646
53647
53648
53649
53650
53651
53652
53653
53654
53655
53656
53657
53658
53659
53660
53661
53662
53663
53664
53665
53666
53667
53668
53669
53670
53671
53672
53673
53674
53675
53676
53677
53678
53679
53680
53681
53682
53683
53684
53685
53686
53687
53688
53689
53690
53691
53692
53693
53694
53695
53696
53697
53698
53699
53700
53701
53702
53703
53704
53705
53706
53707
53708
53709
53710
53711
53712
53713
53714
53715
53716
53717
53718
53719
53720
53721
53722
53723
53724
53725
53726
53727
53728
53729
53730
53731
53732
53733
53734
53735
53736
53737
53738
53739
53740
53741
53742
53743
53744
53745
53746
53747
53748
53749
53750
53751
53752
53753
53754
53755
53756
53757
53758
53759
53760
53761
53762
53763
53764
53765
53766
53767
53768
53769
53770
53771
53772
53773
53774
53775
53776
53777
53778
53779
53780
53781
53782
53783
53784
53785
53786
53787
53788
53789
53790
53791
53792
53793
53794
53795
53796
53797
53798
53799
53800
53801
53802
53803
53804
53805
53806
53807
53808
53809
53810
53811
53812
53813
53814
53815
53816
53817
53818
53819
53820
53821
53822
53823
53824
53825
53826
53827
53828
53829
53830
53831
53832
53833
53834
53835
53836
53837
53838
53839
53840
53841
53842
53843
53844
53845
53846
53847
53848
53849
53850
53851
53852
53853
53854
53855
53856
53857
53858
53859
53860
53861
53862
53863
53864
53865
53866
53867
53868
53869
53870
53871
53872
53873
53874
53875
53876
53877
53878
53879
53880
53881
53882
53883
53884
53885
53886
53887
53888
53889
53890
53891
53892
53893
53894
53895
53896
53897
53898
53899
53900
53901
53902
53903
53904
53905
53906
53907
53908
53909
53910
53911
53912
53913
53914
53915
53916
53917
53918
53919
53920
53921
53922
53923
53924
53925
53926
53927
53928
53929
53930
53931
53932
53933
53934
53935
53936
53937
53938
53939
53940
53941
53942
53943
53944
53945
53946
53947
53948
53949
53950
53951
53952
53953
53954
53955
53956
53957
53958
53959
53960
53961
53962
53963
53964
53965
53966
53967
53968
53969
53970
53971
53972
53973
53974
53975
53976
53977
53978
53979
53980
53981
53982
53983
53984
53985
53986
53987
53988
53989
53990
53991
53992
53993
53994
53995
53996
53997
53998
53999
54000
54001
54002
54003
54004
54005
54006
54007
54008
54009
54010
54011
54012
54013
54014
54015
54016
54017
54018
54019
54020
54021
54022
54023
54024
54025
54026
54027
54028
54029
54030
54031
54032
54033
54034
54035
54036
54037
54038
54039
54040
54041
54042
54043
54044
54045
54046
54047
54048
54049
54050
54051
54052
54053
54054
54055
54056
54057
54058
54059
54060
54061
54062
54063
54064
54065
54066
54067
54068
54069
54070
54071
54072
54073
54074
54075
54076
54077
54078
54079
54080
54081
54082
54083
54084
54085
54086
54087
54088
54089
54090
54091
54092
54093
54094
54095
54096
54097
54098
54099
54100
54101
54102
54103
54104
54105
54106
54107
54108
54109
54110
54111
54112
54113
54114
54115
54116
54117
54118
54119
54120
54121
54122
54123
54124
54125
54126
54127
54128
54129
54130
54131
54132
54133
54134
54135
54136
54137
54138
54139
54140
54141
54142
54143
54144
54145
54146
54147
54148
54149
54150
54151
54152
54153
54154
54155
54156
54157
54158
54159
54160
54161
54162
54163
54164
54165
54166
54167
54168
54169
54170
54171
54172
54173
54174
54175
54176
54177
54178
54179
54180
54181
54182
54183
54184
54185
54186
54187
54188
54189
54190
54191
54192
54193
54194
54195
54196
54197
54198
54199
54200
54201
54202
54203
54204
54205
54206
54207
54208
54209
54210
54211
54212
54213
54214
54215
54216
54217
54218
54219
54220
54221
54222
54223
54224
54225
54226
54227
54228
54229
54230
54231
54232
54233
54234
54235
54236
54237
54238
54239
54240
54241
54242
54243
54244
54245
54246
54247
54248
54249
54250
54251
54252
54253
54254
54255
54256
54257
54258
54259
54260
54261
54262
54263
54264
54265
54266
54267
54268
54269
54270
54271
54272
54273
54274
54275
54276
54277
54278
54279
54280
54281
54282
54283
54284
54285
54286
54287
54288
54289
54290
54291
54292
54293
54294
54295
54296
54297
54298
54299
54300
54301
54302
54303
54304
54305
54306
54307
54308
54309
54310
54311
54312
54313
54314
54315
54316
54317
54318
54319
54320
54321
54322
54323
54324
54325
54326
54327
54328
54329
54330
54331
54332
54333
54334
54335
54336
54337
54338
54339
54340
54341
54342
54343
54344
54345
54346
54347
54348
54349
54350
54351
54352
54353
54354
54355
54356
54357
54358
54359
54360
54361
54362
54363
54364
54365
54366
54367
54368
54369
54370
54371
54372
54373
54374
54375
54376
54377
54378
54379
54380
54381
54382
54383
54384
54385
54386
54387
54388
54389
54390
54391
54392
54393
54394
54395
54396
54397
54398
54399
54400
54401
54402
54403
54404
54405
54406
54407
54408
54409
54410
54411
54412
54413
54414
54415
54416
54417
54418
54419
54420
54421
54422
54423
54424
54425
54426
54427
54428
54429
54430
54431
54432
54433
54434
54435
54436
54437
54438
54439
54440
54441
54442
54443
54444
54445
54446
54447
54448
54449
54450
54451
54452
54453
54454
54455
54456
54457
54458
54459
54460
54461
54462
54463
54464
54465
54466
54467
54468
54469
54470
54471
54472
54473
54474
54475
54476
54477
54478
54479
54480
54481
54482
54483
54484
54485
54486
54487
54488
54489
54490
54491
54492
54493
54494
54495
54496
54497
54498
54499
54500
54501
54502
54503
54504
54505
54506
54507
54508
54509
54510
54511
54512
54513
54514
54515
54516
54517
54518
54519
54520
54521
54522
54523
54524
54525
54526
54527
54528
54529
54530
54531
54532
54533
54534
54535
54536
54537
54538
54539
54540
54541
54542
54543
54544
54545
54546
54547
54548
54549
54550
54551
54552
54553
54554
54555
54556
54557
54558
54559
54560
54561
54562
54563
54564
54565
54566
54567
54568
54569
54570
54571
54572
54573
54574
54575
54576
54577
54578
54579
54580
54581
54582
54583
54584
54585
54586
54587
54588
54589
54590
54591
54592
54593
54594
54595
54596
54597
54598
54599
54600
54601
54602
54603
54604
54605
54606
54607
54608
54609
54610
54611
54612
54613
54614
54615
54616
54617
54618
54619
54620
54621
54622
54623
54624
54625
54626
54627
54628
54629
54630
54631
54632
54633
54634
54635
54636
54637
54638
54639
54640
54641
54642
54643
54644
54645
54646
54647
54648
54649
54650
54651
54652
54653
54654
54655
54656
54657
54658
54659
54660
54661
54662
54663
54664
54665
54666
54667
54668
54669
54670
54671
54672
54673
54674
54675
54676
54677
54678
54679
54680
54681
54682
54683
54684
54685
54686
54687
54688
54689
54690
54691
54692
54693
54694
54695
54696
54697
54698
54699
54700
54701
54702
54703
54704
54705
54706
54707
54708
54709
54710
54711
54712
54713
54714
54715
54716
54717
54718
54719
54720
54721
54722
54723
54724
54725
54726
54727
54728
54729
54730
54731
54732
54733
54734
54735
54736
54737
54738
54739
54740
54741
54742
54743
54744
54745
54746
54747
54748
54749
54750
54751
54752
54753
54754
54755
54756
54757
54758
54759
54760
54761
54762
54763
54764
54765
54766
54767
54768
54769
54770
54771
54772
54773
54774
54775
54776
54777
54778
54779
54780
54781
54782
54783
54784
54785
54786
54787
54788
54789
54790
54791
54792
54793
54794
54795
54796
54797
54798
54799
54800
54801
54802
54803
54804
54805
54806
54807
54808
54809
54810
54811
54812
54813
54814
54815
54816
54817
54818
54819
54820
54821
54822
54823
54824
54825
54826
54827
54828
54829
54830
54831
54832
54833
54834
54835
54836
54837
54838
54839
54840
54841
54842
54843
54844
54845
54846
54847
54848
54849
54850
54851
54852
54853
54854
54855
54856
54857
54858
54859
54860
54861
54862
54863
54864
54865
54866
54867
54868
54869
54870
54871
54872
54873
54874
54875
54876
54877
54878
54879
54880
54881
54882
54883
54884
54885
54886
54887
54888
54889
54890
54891
54892
54893
54894
54895
54896
54897
54898
54899
54900
54901
54902
54903
54904
54905
54906
54907
54908
54909
54910
54911
54912
54913
54914
54915
54916
54917
54918
54919
54920
54921
54922
54923
54924
54925
54926
54927
54928
54929
54930
54931
54932
54933
54934
54935
54936
54937
54938
54939
54940
54941
54942
54943
54944
54945
54946
54947
54948
54949
54950
54951
54952
54953
54954
54955
54956
54957
54958
54959
54960
54961
54962
54963
54964
54965
54966
54967
54968
54969
54970
54971
54972
54973
54974
54975
54976
54977
54978
54979
54980
54981
54982
54983
54984
54985
54986
54987
54988
54989
54990
54991
54992
54993
54994
54995
54996
54997
54998
54999
55000
55001
55002
55003
55004
55005
55006
55007
55008
55009
55010
55011
55012
55013
55014
55015
55016
55017
55018
55019
55020
55021
55022
55023
55024
55025
55026
55027
55028
55029
55030
55031
55032
55033
55034
55035
55036
55037
55038
55039
55040
55041
55042
55043
55044
55045
55046
55047
55048
55049
55050
55051
55052
55053
55054
55055
55056
55057
55058
55059
55060
55061
55062
55063
55064
55065
55066
55067
55068
55069
55070
55071
55072
55073
55074
55075
55076
55077
55078
55079
55080
55081
55082
55083
55084
55085
55086
55087
55088
55089
55090
55091
55092
55093
55094
55095
55096
55097
55098
55099
55100
55101
55102
55103
55104
55105
55106
55107
55108
55109
55110
55111
55112
55113
55114
55115
55116
55117
55118
55119
55120
55121
55122
55123
55124
55125
55126
55127
55128
55129
55130
55131
55132
55133
55134
55135
55136
55137
55138
55139
55140
55141
55142
55143
55144
55145
55146
55147
55148
55149
55150
55151
55152
55153
55154
55155
55156
55157
55158
55159
55160
55161
55162
55163
55164
55165
55166
55167
55168
55169
55170
55171
55172
55173
55174
55175
55176
55177
55178
55179
55180
55181
55182
55183
55184
55185
55186
55187
55188
55189
55190
55191
55192
55193
55194
55195
55196
55197
55198
55199
55200
55201
55202
55203
55204
55205
55206
55207
55208
55209
55210
55211
55212
55213
55214
55215
55216
55217
55218
55219
55220
55221
55222
55223
55224
55225
55226
55227
55228
55229
55230
55231
55232
55233
55234
55235
55236
55237
55238
55239
55240
55241
55242
55243
55244
55245
55246
55247
55248
55249
55250
55251
55252
55253
55254
55255
55256
55257
55258
55259
55260
55261
55262
55263
55264
55265
55266
55267
55268
55269
55270
55271
55272
55273
55274
55275
55276
55277
55278
55279
55280
55281
55282
55283
55284
55285
55286
55287
55288
55289
55290
55291
55292
55293
55294
55295
55296
55297
55298
55299
55300
55301
55302
55303
55304
55305
55306
55307
55308
55309
55310
55311
55312
55313
55314
55315
55316
55317
55318
55319
55320
55321
55322
55323
55324
55325
55326
55327
55328
55329
55330
55331
55332
55333
55334
55335
55336
55337
55338
55339
55340
55341
55342
55343
55344
55345
55346
55347
55348
55349
55350
55351
55352
55353
55354
55355
55356
55357
55358
55359
55360
55361
55362
55363
55364
55365
55366
55367
55368
55369
55370
55371
55372
55373
55374
55375
55376
55377
55378
55379
55380
55381
55382
55383
55384
55385
55386
55387
55388
55389
55390
55391
55392
55393
55394
55395
55396
55397
55398
55399
55400
55401
55402
55403
55404
55405
55406
55407
55408
55409
55410
55411
55412
55413
55414
55415
55416
55417
55418
55419
55420
55421
55422
55423
55424
55425
55426
55427
55428
55429
55430
55431
55432
55433
55434
55435
55436
55437
55438
55439
55440
55441
55442
55443
55444
55445
55446
55447
55448
55449
55450
55451
55452
55453
55454
55455
55456
55457
55458
55459
55460
55461
55462
55463
55464
55465
55466
55467
55468
55469
55470
55471
55472
55473
55474
55475
55476
55477
55478
55479
55480
55481
55482
55483
55484
55485
55486
55487
55488
55489
55490
55491
55492
55493
55494
55495
55496
55497
55498
55499
55500
55501
55502
55503
55504
55505
55506
55507
55508
55509
55510
55511
55512
55513
55514
55515
55516
55517
55518
55519
55520
55521
55522
55523
55524
55525
55526
55527
55528
55529
55530
55531
55532
55533
55534
55535
55536
55537
55538
55539
55540
55541
55542
55543
55544
55545
55546
55547
55548
55549
55550
55551
55552
55553
55554
55555
55556
55557
55558
55559
55560
55561
55562
55563
55564
55565
55566
55567
55568
55569
55570
55571
55572
55573
55574
55575
55576
55577
55578
55579
55580
55581
55582
55583
55584
55585
55586
55587
55588
55589
55590
55591
55592
55593
55594
55595
55596
55597
55598
55599
55600
55601
55602
55603
55604
55605
55606
55607
55608
55609
55610
55611
55612
55613
55614
55615
55616
55617
55618
55619
55620
55621
55622
55623
55624
55625
55626
55627
55628
55629
55630
55631
55632
55633
55634
55635
55636
55637
55638
55639
55640
55641
55642
55643
55644
55645
55646
55647
55648
55649
55650
55651
55652
55653
55654
55655
55656
55657
55658
55659
55660
55661
55662
55663
55664
55665
55666
55667
55668
55669
55670
55671
55672
55673
55674
55675
55676
55677
55678
55679
55680
55681
55682
55683
55684
55685
55686
55687
55688
55689
55690
55691
55692
55693
55694
55695
55696
55697
55698
55699
55700
55701
55702
55703
55704
55705
55706
55707
55708
55709
55710
55711
55712
55713
55714
55715
55716
55717
55718
55719
55720
55721
55722
55723
55724
55725
55726
55727
55728
55729
55730
55731
55732
55733
55734
55735
55736
55737
55738
55739
55740
55741
55742
55743
55744
55745
55746
55747
55748
55749
55750
55751
55752
55753
55754
55755
55756
55757
55758
55759
55760
55761
55762
55763
55764
55765
55766
55767
55768
55769
55770
55771
55772
55773
55774
55775
55776
55777
55778
55779
55780
55781
55782
55783
55784
55785
55786
55787
55788
55789
55790
55791
55792
55793
55794
55795
55796
55797
55798
55799
55800
55801
55802
55803
55804
55805
55806
55807
55808
55809
55810
55811
55812
55813
55814
55815
55816
55817
55818
55819
55820
55821
55822
55823
55824
55825
55826
55827
55828
55829
55830
55831
55832
55833
55834
55835
55836
55837
55838
55839
55840
55841
55842
55843
55844
55845
55846
55847
55848
55849
55850
55851
55852
55853
55854
55855
55856
55857
55858
55859
55860
55861
55862
55863
55864
55865
55866
55867
55868
55869
55870
55871
55872
55873
55874
55875
55876
55877
55878
55879
55880
55881
55882
55883
55884
55885
55886
55887
55888
55889
55890
55891
55892
55893
55894
55895
55896
55897
55898
55899
55900
55901
55902
55903
55904
55905
55906
55907
55908
55909
55910
55911
55912
55913
55914
55915
55916
55917
55918
55919
55920
55921
55922
55923
55924
55925
55926
55927
55928
55929
55930
55931
55932
55933
55934
55935
55936
55937
55938
55939
55940
55941
55942
55943
55944
55945
55946
55947
55948
55949
55950
55951
55952
55953
55954
55955
55956
55957
55958
55959
55960
55961
55962
55963
55964
55965
55966
55967
55968
55969
55970
55971
55972
55973
55974
55975
55976
55977
55978
55979
55980
55981
55982
55983
55984
55985
55986
55987
55988
55989
55990
55991
55992
55993
55994
55995
55996
55997
55998
55999
56000
56001
56002
56003
56004
56005
56006
56007
56008
56009
56010
56011
56012
56013
56014
56015
56016
56017
56018
56019
56020
56021
56022
56023
56024
56025
56026
56027
56028
56029
56030
56031
56032
56033
56034
56035
56036
56037
56038
56039
56040
56041
56042
56043
56044
56045
56046
56047
56048
56049
56050
56051
56052
56053
56054
56055
56056
56057
56058
56059
56060
56061
56062
56063
56064
56065
56066
56067
56068
56069
56070
56071
56072
56073
56074
56075
56076
56077
56078
56079
56080
56081
56082
56083
56084
56085
56086
56087
56088
56089
56090
56091
56092
56093
56094
56095
56096
56097
56098
56099
56100
56101
56102
56103
56104
56105
56106
56107
56108
56109
56110
56111
56112
56113
56114
56115
56116
56117
56118
56119
56120
56121
56122
56123
56124
56125
56126
56127
56128
56129
56130
56131
56132
56133
56134
56135
56136
56137
56138
56139
56140
56141
56142
56143
56144
56145
56146
56147
56148
56149
56150
56151
56152
56153
56154
56155
56156
56157
56158
56159
56160
56161
56162
56163
56164
56165
56166
56167
56168
56169
56170
56171
56172
56173
56174
56175
56176
56177
56178
56179
56180
56181
56182
56183
56184
56185
56186
56187
56188
56189
56190
56191
56192
56193
56194
56195
56196
56197
56198
56199
56200
56201
56202
56203
56204
56205
56206
56207
56208
56209
56210
56211
56212
56213
56214
56215
56216
56217
56218
56219
56220
56221
56222
56223
56224
56225
56226
56227
56228
56229
56230
56231
56232
56233
56234
56235
56236
56237
56238
56239
56240
56241
56242
56243
56244
56245
56246
56247
56248
56249
56250
56251
56252
56253
56254
56255
56256
56257
56258
56259
56260
56261
56262
56263
56264
56265
56266
56267
56268
56269
56270
56271
56272
56273
56274
56275
56276
56277
56278
56279
56280
56281
56282
56283
56284
56285
56286
56287
56288
56289
56290
56291
56292
56293
56294
56295
56296
56297
56298
56299
56300
56301
56302
56303
56304
56305
56306
56307
56308
56309
56310
56311
56312
56313
56314
56315
56316
56317
56318
56319
56320
56321
56322
56323
56324
56325
56326
56327
56328
56329
56330
56331
56332
56333
56334
56335
56336
56337
56338
56339
56340
56341
56342
56343
56344
56345
56346
56347
56348
56349
56350
56351
56352
56353
56354
56355
56356
56357
56358
56359
56360
56361
56362
56363
56364
56365
56366
56367
56368
56369
56370
56371
56372
56373
56374
56375
56376
56377
56378
56379
56380
56381
56382
56383
56384
56385
56386
56387
56388
56389
56390
56391
56392
56393
56394
56395
56396
56397
56398
56399
56400
56401
56402
56403
56404
56405
56406
56407
56408
56409
56410
56411
56412
56413
56414
56415
56416
56417
56418
56419
56420
56421
56422
56423
56424
56425
56426
56427
56428
56429
56430
56431
56432
56433
56434
56435
56436
56437
56438
56439
56440
56441
56442
56443
56444
56445
56446
56447
56448
56449
56450
56451
56452
56453
56454
56455
56456
56457
56458
56459
56460
56461
56462
56463
56464
56465
56466
56467
56468
56469
56470
56471
56472
56473
56474
56475
56476
56477
56478
56479
56480
56481
56482
56483
56484
56485
56486
56487
56488
56489
56490
56491
56492
56493
56494
56495
56496
56497
56498
56499
56500
56501
56502
56503
56504
56505
56506
56507
56508
56509
56510
56511
56512
56513
56514
56515
56516
56517
56518
56519
56520
56521
56522
56523
56524
56525
56526
56527
56528
56529
56530
56531
56532
56533
56534
56535
56536
56537
56538
56539
56540
56541
56542
56543
56544
56545
56546
56547
56548
56549
56550
56551
56552
56553
56554
56555
56556
56557
56558
56559
56560
56561
56562
56563
56564
56565
56566
56567
56568
56569
56570
56571
56572
56573
56574
56575
56576
56577
56578
56579
56580
56581
56582
56583
56584
56585
56586
56587
56588
56589
56590
56591
56592
56593
56594
56595
56596
56597
56598
56599
56600
56601
56602
56603
56604
56605
56606
56607
56608
56609
56610
56611
56612
56613
56614
56615
56616
56617
56618
56619
56620
56621
56622
56623
56624
56625
56626
56627
56628
56629
56630
56631
56632
56633
56634
56635
56636
56637
56638
56639
56640
56641
56642
56643
56644
56645
56646
56647
56648
56649
56650
56651
56652
56653
56654
56655
56656
56657
56658
56659
56660
56661
56662
56663
56664
56665
56666
56667
56668
56669
56670
56671
56672
56673
56674
56675
56676
56677
56678
56679
56680
56681
56682
56683
56684
56685
56686
56687
56688
56689
56690
56691
56692
56693
56694
56695
56696
56697
56698
56699
56700
56701
56702
56703
56704
56705
56706
56707
56708
56709
56710
56711
56712
56713
56714
56715
56716
56717
56718
56719
56720
56721
56722
56723
56724
56725
56726
56727
56728
56729
56730
56731
56732
56733
56734
56735
56736
56737
56738
56739
56740
56741
56742
56743
56744
56745
56746
56747
56748
56749
56750
56751
56752
56753
56754
56755
56756
56757
56758
56759
56760
56761
56762
56763
56764
56765
56766
56767
56768
56769
56770
56771
56772
56773
56774
56775
56776
56777
56778
56779
56780
56781
56782
56783
56784
56785
56786
56787
56788
56789
56790
56791
56792
56793
56794
56795
56796
56797
56798
56799
56800
56801
56802
56803
56804
56805
56806
56807
56808
56809
56810
56811
56812
56813
56814
56815
56816
56817
56818
56819
56820
56821
56822
56823
56824
56825
56826
56827
56828
56829
56830
56831
56832
56833
56834
56835
56836
56837
56838
56839
56840
56841
56842
56843
56844
56845
56846
56847
56848
56849
56850
56851
56852
56853
56854
56855
56856
56857
56858
56859
56860
56861
56862
56863
56864
56865
56866
56867
56868
56869
56870
56871
56872
56873
56874
56875
56876
56877
56878
56879
56880
56881
56882
56883
56884
56885
56886
56887
56888
56889
56890
56891
56892
56893
56894
56895
56896
56897
56898
56899
56900
56901
56902
56903
56904
56905
56906
56907
56908
56909
56910
56911
56912
56913
56914
56915
56916
56917
56918
56919
56920
56921
56922
56923
56924
56925
56926
56927
56928
56929
56930
56931
56932
56933
56934
56935
56936
56937
56938
56939
56940
56941
56942
56943
56944
56945
56946
56947
56948
56949
56950
56951
56952
56953
56954
56955
56956
56957
56958
56959
56960
56961
56962
56963
56964
56965
56966
56967
56968
56969
56970
56971
56972
56973
56974
56975
56976
56977
56978
56979
56980
56981
56982
56983
56984
56985
56986
56987
56988
56989
56990
56991
56992
56993
56994
56995
56996
56997
56998
56999
57000
57001
57002
57003
57004
57005
57006
57007
57008
57009
57010
57011
57012
57013
57014
57015
57016
57017
57018
57019
57020
57021
57022
57023
57024
57025
57026
57027
57028
57029
57030
57031
57032
57033
57034
57035
57036
57037
57038
57039
57040
57041
57042
57043
57044
57045
57046
57047
57048
57049
57050
57051
57052
57053
57054
57055
57056
57057
57058
57059
57060
57061
57062
57063
57064
57065
57066
57067
57068
57069
57070
57071
57072
57073
57074
57075
57076
57077
57078
57079
57080
57081
57082
57083
57084
57085
57086
57087
57088
57089
57090
57091
57092
57093
57094
57095
57096
57097
57098
57099
57100
57101
57102
57103
57104
57105
57106
57107
57108
57109
57110
57111
57112
57113
57114
57115
57116
57117
57118
57119
57120
57121
57122
57123
57124
57125
57126
57127
57128
57129
57130
57131
57132
57133
57134
57135
57136
57137
57138
57139
57140
57141
57142
57143
57144
57145
57146
57147
57148
57149
57150
57151
57152
57153
57154
57155
57156
57157
57158
57159
57160
57161
57162
57163
57164
57165
57166
57167
57168
57169
57170
57171
57172
57173
57174
57175
57176
57177
57178
57179
57180
57181
57182
57183
57184
57185
57186
57187
57188
57189
57190
57191
57192
57193
57194
57195
57196
57197
57198
57199
57200
57201
57202
57203
57204
57205
57206
57207
57208
57209
57210
57211
57212
57213
57214
57215
57216
57217
57218
57219
57220
57221
57222
57223
57224
57225
57226
57227
57228
57229
57230
57231
57232
57233
57234
57235
57236
57237
57238
57239
57240
57241
57242
57243
57244
57245
57246
57247
57248
57249
57250
57251
57252
57253
57254
57255
57256
57257
57258
57259
57260
57261
57262
57263
57264
57265
57266
57267
57268
57269
57270
57271
57272
57273
57274
57275
57276
57277
57278
57279
57280
57281
57282
57283
57284
57285
57286
57287
57288
57289
57290
57291
57292
57293
57294
57295
57296
57297
57298
57299
57300
57301
57302
57303
57304
57305
57306
57307
57308
57309
57310
57311
57312
57313
57314
57315
57316
57317
57318
57319
57320
57321
57322
57323
57324
57325
57326
57327
57328
57329
57330
57331
57332
57333
57334
57335
57336
57337
57338
57339
57340
57341
57342
57343
57344
57345
57346
57347
57348
57349
57350
57351
57352
57353
57354
57355
57356
57357
57358
57359
57360
57361
57362
57363
57364
57365
57366
57367
57368
57369
57370
57371
57372
57373
57374
57375
57376
57377
57378
57379
57380
57381
57382
57383
57384
57385
57386
57387
57388
57389
57390
57391
57392
57393
57394
57395
57396
57397
57398
57399
57400
57401
57402
57403
57404
57405
57406
57407
57408
57409
57410
57411
57412
57413
57414
57415
57416
57417
57418
57419
57420
57421
57422
57423
57424
57425
57426
57427
57428
57429
57430
57431
57432
57433
57434
57435
57436
57437
57438
57439
57440
57441
57442
57443
57444
57445
57446
57447
57448
57449
57450
57451
57452
57453
57454
57455
57456
57457
57458
57459
57460
57461
57462
57463
57464
57465
57466
57467
57468
57469
57470
57471
57472
57473
57474
57475
57476
57477
57478
57479
57480
57481
57482
57483
57484
57485
57486
57487
57488
57489
57490
57491
57492
57493
57494
57495
57496
57497
57498
57499
57500
57501
57502
57503
57504
57505
57506
57507
57508
57509
57510
57511
57512
57513
57514
57515
57516
57517
57518
57519
57520
57521
57522
57523
57524
57525
57526
57527
57528
57529
57530
57531
57532
57533
57534
57535
57536
57537
57538
57539
57540
57541
57542
57543
57544
57545
57546
57547
57548
57549
57550
57551
57552
57553
57554
57555
57556
57557
57558
57559
57560
57561
57562
57563
57564
57565
57566
57567
57568
57569
57570
57571
57572
57573
57574
57575
57576
57577
57578
57579
57580
57581
57582
57583
57584
57585
57586
57587
57588
57589
57590
57591
57592
57593
57594
57595
57596
57597
57598
57599
57600
57601
57602
57603
57604
57605
57606
57607
57608
57609
57610
57611
57612
57613
57614
57615
57616
57617
57618
57619
57620
57621
57622
57623
57624
57625
57626
57627
57628
57629
57630
57631
57632
57633
57634
57635
57636
57637
57638
57639
57640
57641
57642
57643
57644
57645
57646
57647
57648
57649
57650
57651
57652
57653
57654
57655
57656
57657
57658
57659
57660
57661
57662
57663
57664
57665
57666
57667
57668
57669
57670
57671
57672
57673
57674
57675
57676
57677
57678
57679
57680
57681
57682
57683
57684
57685
57686
57687
57688
57689
57690
57691
57692
57693
57694
57695
57696
57697
57698
57699
57700
57701
57702
57703
57704
57705
57706
57707
57708
57709
57710
57711
57712
57713
57714
57715
57716
57717
57718
57719
57720
57721
57722
57723
57724
57725
57726
57727
57728
57729
57730
57731
57732
57733
57734
57735
57736
57737
57738
57739
57740
57741
57742
57743
57744
57745
57746
57747
57748
57749
57750
57751
57752
57753
57754
57755
57756
57757
57758
57759
57760
57761
57762
57763
57764
57765
57766
57767
57768
57769
57770
57771
57772
57773
57774
57775
57776
57777
57778
57779
57780
57781
57782
57783
57784
57785
57786
57787
57788
57789
57790
57791
57792
57793
57794
57795
57796
57797
57798
57799
57800
57801
57802
57803
57804
57805
57806
57807
57808
57809
57810
57811
57812
57813
57814
57815
57816
57817
57818
57819
57820
57821
57822
57823
57824
57825
57826
57827
57828
57829
57830
57831
57832
57833
57834
57835
57836
57837
57838
57839
57840
57841
57842
57843
57844
57845
57846
57847
57848
57849
57850
57851
57852
57853
57854
57855
57856
57857
57858
57859
57860
57861
57862
57863
57864
57865
57866
57867
57868
57869
57870
57871
57872
57873
57874
57875
57876
57877
57878
57879
57880
57881
57882
57883
57884
57885
57886
57887
57888
57889
57890
57891
57892
57893
57894
57895
57896
57897
57898
57899
57900
57901
57902
57903
57904
57905
57906
57907
57908
57909
57910
57911
57912
57913
57914
57915
57916
57917
57918
57919
57920
57921
57922
57923
57924
57925
57926
57927
57928
57929
57930
57931
57932
57933
57934
57935
57936
57937
57938
57939
57940
57941
57942
57943
57944
57945
57946
57947
57948
57949
57950
57951
57952
57953
57954
57955
57956
57957
57958
57959
57960
57961
57962
57963
57964
57965
57966
57967
57968
57969
57970
57971
57972
57973
57974
57975
57976
57977
57978
57979
57980
57981
57982
57983
57984
57985
57986
57987
57988
57989
57990
57991
57992
57993
57994
57995
57996
57997
57998
57999
58000
58001
58002
58003
58004
58005
58006
58007
58008
58009
58010
58011
58012
58013
58014
58015
58016
58017
58018
58019
58020
58021
58022
58023
58024
58025
58026
58027
58028
58029
58030
58031
58032
58033
58034
58035
58036
58037
58038
58039
58040
58041
58042
58043
58044
58045
58046
58047
58048
58049
58050
58051
58052
58053
58054
58055
58056
58057
58058
58059
58060
58061
58062
58063
58064
58065
58066
58067
58068
58069
58070
58071
58072
58073
58074
58075
58076
58077
58078
58079
58080
58081
58082
58083
58084
58085
58086
58087
58088
58089
58090
58091
58092
58093
58094
58095
58096
58097
58098
58099
58100
58101
58102
58103
58104
58105
58106
58107
58108
58109
58110
58111
58112
58113
58114
58115
58116
58117
58118
58119
58120
58121
58122
58123
58124
58125
58126
58127
58128
58129
58130
58131
58132
58133
58134
58135
58136
58137
58138
58139
58140
58141
58142
58143
58144
58145
58146
58147
58148
58149
58150
58151
58152
58153
58154
58155
58156
58157
58158
58159
58160
58161
58162
58163
58164
58165
58166
58167
58168
58169
58170
58171
58172
58173
58174
58175
58176
58177
58178
58179
58180
58181
58182
58183
58184
58185
58186
58187
58188
58189
58190
58191
58192
58193
58194
58195
58196
58197
58198
58199
58200
58201
58202
58203
58204
58205
58206
58207
58208
58209
58210
58211
58212
58213
58214
58215
58216
58217
58218
58219
58220
58221
58222
58223
58224
58225
58226
58227
58228
58229
58230
58231
58232
58233
58234
58235
58236
58237
58238
58239
58240
58241
58242
58243
58244
58245
58246
58247
58248
58249
58250
58251
58252
58253
58254
58255
58256
58257
58258
58259
58260
58261
58262
58263
58264
58265
58266
58267
58268
58269
58270
58271
58272
58273
58274
58275
58276
58277
58278
58279
58280
58281
58282
58283
58284
58285
58286
58287
58288
58289
58290
58291
58292
58293
58294
58295
58296
58297
58298
58299
58300
58301
58302
58303
58304
58305
58306
58307
58308
58309
58310
58311
58312
58313
58314
58315
58316
58317
58318
58319
58320
58321
58322
58323
58324
58325
58326
58327
58328
58329
58330
58331
58332
58333
58334
58335
58336
58337
58338
58339
58340
58341
58342
58343
58344
58345
58346
58347
58348
58349
58350
58351
58352
58353
58354
58355
58356
58357
58358
58359
58360
58361
58362
58363
58364
58365
58366
58367
58368
58369
58370
58371
58372
58373
58374
58375
58376
58377
58378
58379
58380
58381
58382
58383
58384
58385
58386
58387
58388
58389
58390
58391
58392
58393
58394
58395
58396
58397
58398
58399
58400
58401
58402
58403
58404
58405
58406
58407
58408
58409
58410
58411
58412
58413
58414
58415
58416
58417
58418
58419
58420
58421
58422
58423
58424
58425
58426
58427
58428
58429
58430
58431
58432
58433
58434
58435
58436
58437
58438
58439
58440
58441
58442
58443
58444
58445
58446
58447
58448
58449
58450
58451
58452
58453
58454
58455
58456
58457
58458
58459
58460
58461
58462
58463
58464
58465
58466
58467
58468
58469
58470
58471
58472
58473
58474
58475
58476
58477
58478
58479
58480
58481
58482
58483
58484
58485
58486
58487
58488
58489
58490
58491
58492
58493
58494
58495
58496
58497
58498
58499
58500
58501
58502
58503
58504
58505
58506
58507
58508
58509
58510
58511
58512
58513
58514
58515
58516
58517
58518
58519
58520
58521
58522
58523
58524
58525
58526
58527
58528
58529
58530
58531
58532
58533
58534
58535
58536
58537
58538
58539
58540
58541
58542
58543
58544
58545
58546
58547
58548
58549
58550
58551
58552
58553
58554
58555
58556
58557
58558
58559
58560
58561
58562
58563
58564
58565
58566
58567
58568
58569
58570
58571
58572
58573
58574
58575
58576
58577
58578
58579
58580
58581
58582
58583
58584
58585
58586
58587
58588
58589
58590
58591
58592
58593
58594
58595
58596
58597
58598
58599
58600
58601
58602
58603
58604
58605
58606
58607
58608
58609
58610
58611
58612
58613
58614
58615
58616
58617
58618
58619
58620
58621
58622
58623
58624
58625
58626
58627
58628
58629
58630
58631
58632
58633
58634
58635
58636
58637
58638
58639
58640
58641
58642
58643
58644
58645
58646
58647
58648
58649
58650
58651
58652
58653
58654
58655
58656
58657
58658
58659
58660
58661
58662
58663
58664
58665
58666
58667
58668
58669
58670
58671
58672
58673
58674
58675
58676
58677
58678
58679
58680
58681
58682
58683
58684
58685
58686
58687
58688
58689
58690
58691
58692
58693
58694
58695
58696
58697
58698
58699
58700
58701
58702
58703
58704
58705
58706
58707
58708
58709
58710
58711
58712
58713
58714
58715
58716
58717
58718
58719
58720
58721
58722
58723
58724
58725
58726
58727
58728
58729
58730
58731
58732
58733
58734
58735
58736
58737
58738
58739
58740
58741
58742
58743
58744
58745
58746
58747
58748
58749
58750
58751
58752
58753
58754
58755
58756
58757
58758
58759
58760
58761
58762
58763
58764
58765
58766
58767
58768
58769
58770
58771
58772
58773
58774
58775
58776
58777
58778
58779
58780
58781
58782
58783
58784
58785
58786
58787
58788
58789
58790
58791
58792
58793
58794
58795
58796
58797
58798
58799
58800
58801
58802
58803
58804
58805
58806
58807
58808
58809
58810
58811
58812
58813
58814
58815
58816
58817
58818
58819
58820
58821
58822
58823
58824
58825
58826
58827
58828
58829
58830
58831
58832
58833
58834
58835
58836
58837
58838
58839
58840
58841
58842
58843
58844
58845
58846
58847
58848
58849
58850
58851
58852
58853
58854
58855
58856
58857
58858
58859
58860
58861
58862
58863
58864
58865
58866
58867
58868
58869
58870
58871
58872
58873
58874
58875
58876
58877
58878
58879
58880
58881
58882
58883
58884
58885
58886
58887
58888
58889
58890
58891
58892
58893
58894
58895
58896
58897
58898
58899
58900
58901
58902
58903
58904
58905
58906
58907
58908
58909
58910
58911
58912
58913
58914
58915
58916
58917
58918
58919
58920
58921
58922
58923
58924
58925
58926
58927
58928
58929
58930
58931
58932
58933
58934
58935
58936
58937
58938
58939
58940
58941
58942
58943
58944
58945
58946
58947
58948
58949
58950
58951
58952
58953
58954
58955
58956
58957
58958
58959
58960
58961
58962
58963
58964
58965
58966
58967
58968
58969
58970
58971
58972
58973
58974
58975
58976
58977
58978
58979
58980
58981
58982
58983
58984
58985
58986
58987
58988
58989
58990
58991
58992
58993
58994
58995
58996
58997
58998
58999
59000
59001
59002
59003
59004
59005
59006
59007
59008
59009
59010
59011
59012
59013
59014
59015
59016
59017
59018
59019
59020
59021
59022
59023
59024
59025
59026
59027
59028
59029
59030
59031
59032
59033
59034
59035
59036
59037
59038
59039
59040
59041
59042
59043
59044
59045
59046
59047
59048
59049
59050
59051
59052
59053
59054
59055
59056
59057
59058
59059
59060
59061
59062
59063
59064
59065
59066
59067
59068
59069
59070
59071
59072
59073
59074
59075
59076
59077
59078
59079
59080
59081
59082
59083
59084
59085
59086
59087
59088
59089
59090
59091
59092
59093
59094
59095
59096
59097
59098
59099
59100
59101
59102
59103
59104
59105
59106
59107
59108
59109
59110
59111
59112
59113
59114
59115
59116
59117
59118
59119
59120
59121
59122
59123
59124
59125
59126
59127
59128
59129
59130
59131
59132
59133
59134
59135
59136
59137
59138
59139
59140
59141
59142
59143
59144
59145
59146
59147
59148
59149
59150
59151
59152
59153
59154
59155
59156
59157
59158
59159
59160
59161
59162
59163
59164
59165
59166
59167
59168
59169
59170
59171
59172
59173
59174
59175
59176
59177
59178
59179
59180
59181
59182
59183
59184
59185
59186
59187
59188
59189
59190
59191
59192
59193
59194
59195
59196
59197
59198
59199
59200
59201
59202
59203
59204
59205
59206
59207
59208
59209
59210
59211
59212
59213
59214
59215
59216
59217
59218
59219
59220
59221
59222
59223
59224
59225
59226
59227
59228
59229
59230
59231
59232
59233
59234
59235
59236
59237
59238
59239
59240
59241
59242
59243
59244
59245
59246
59247
59248
59249
59250
59251
59252
59253
59254
59255
59256
59257
59258
59259
59260
59261
59262
59263
59264
59265
59266
59267
59268
59269
59270
59271
59272
59273
59274
59275
59276
59277
59278
59279
59280
59281
59282
59283
59284
59285
59286
59287
59288
59289
59290
59291
59292
59293
59294
59295
59296
59297
59298
59299
59300
59301
59302
59303
59304
59305
59306
59307
59308
59309
59310
59311
59312
59313
59314
59315
59316
59317
59318
59319
59320
59321
59322
59323
59324
59325
59326
59327
59328
59329
59330
59331
59332
59333
59334
59335
59336
59337
59338
59339
59340
59341
59342
59343
59344
59345
59346
59347
59348
59349
59350
59351
59352
59353
59354
59355
59356
59357
59358
59359
59360
59361
59362
59363
59364
59365
59366
59367
59368
59369
59370
59371
59372
59373
59374
59375
59376
59377
59378
59379
59380
59381
59382
59383
59384
59385
59386
59387
59388
59389
59390
59391
59392
59393
59394
59395
59396
59397
59398
59399
59400
59401
59402
59403
59404
59405
59406
59407
59408
59409
59410
59411
59412
59413
59414
59415
59416
59417
59418
59419
59420
59421
59422
59423
59424
59425
59426
59427
59428
59429
59430
59431
59432
59433
59434
59435
59436
59437
59438
59439
59440
59441
59442
59443
59444
59445
59446
59447
59448
59449
59450
59451
59452
59453
59454
59455
59456
59457
59458
59459
59460
59461
59462
59463
59464
59465
59466
59467
59468
59469
59470
59471
59472
59473
59474
59475
59476
59477
59478
59479
59480
59481
59482
59483
59484
59485
59486
59487
59488
59489
59490
59491
59492
59493
59494
59495
59496
59497
59498
59499
59500
59501
59502
59503
59504
59505
59506
59507
59508
59509
59510
59511
59512
59513
59514
59515
59516
59517
59518
59519
59520
59521
59522
59523
59524
59525
59526
59527
59528
59529
59530
59531
59532
59533
59534
59535
59536
59537
59538
59539
59540
59541
59542
59543
59544
59545
59546
59547
59548
59549
59550
59551
59552
59553
59554
59555
59556
59557
59558
59559
59560
59561
59562
59563
59564
59565
59566
59567
59568
59569
59570
59571
59572
59573
59574
59575
59576
59577
59578
59579
59580
59581
59582
59583
59584
59585
59586
59587
59588
59589
59590
59591
59592
59593
59594
59595
59596
59597
59598
59599
59600
59601
59602
59603
59604
59605
59606
59607
59608
59609
59610
59611
59612
59613
59614
59615
59616
59617
59618
59619
59620
59621
59622
59623
59624
59625
59626
59627
59628
59629
59630
59631
59632
59633
59634
59635
59636
59637
59638
59639
59640
59641
59642
59643
59644
59645
59646
59647
59648
59649
59650
59651
59652
59653
59654
59655
59656
59657
59658
59659
59660
59661
59662
59663
59664
59665
59666
59667
59668
59669
59670
59671
59672
59673
59674
59675
59676
59677
59678
59679
59680
59681
59682
59683
59684
59685
59686
59687
59688
59689
59690
59691
59692
59693
59694
59695
59696
59697
59698
59699
59700
59701
59702
59703
59704
59705
59706
59707
59708
59709
59710
59711
59712
59713
59714
59715
59716
59717
59718
59719
59720
59721
59722
59723
59724
59725
59726
59727
59728
59729
59730
59731
59732
59733
59734
59735
59736
59737
59738
59739
59740
59741
59742
59743
59744
59745
59746
59747
59748
59749
59750
59751
59752
59753
59754
59755
59756
59757
59758
59759
59760
59761
59762
59763
59764
59765
59766
59767
59768
59769
59770
59771
59772
59773
59774
59775
59776
59777
59778
59779
59780
59781
59782
59783
59784
59785
59786
59787
59788
59789
59790
59791
59792
59793
59794
59795
59796
59797
59798
59799
59800
59801
59802
59803
59804
59805
59806
59807
59808
59809
59810
59811
59812
59813
59814
59815
59816
59817
59818
59819
59820
59821
59822
59823
59824
59825
59826
59827
59828
59829
59830
59831
59832
59833
59834
59835
59836
59837
59838
59839
59840
59841
59842
59843
59844
59845
59846
59847
59848
59849
59850
59851
59852
59853
59854
59855
59856
59857
59858
59859
59860
59861
59862
59863
59864
59865
59866
59867
59868
59869
59870
59871
59872
59873
59874
59875
59876
59877
59878
59879
59880
59881
59882
59883
59884
59885
59886
59887
59888
59889
59890
59891
59892
59893
59894
59895
59896
59897
59898
59899
59900
59901
59902
59903
59904
59905
59906
59907
59908
59909
59910
59911
59912
59913
59914
59915
59916
59917
59918
59919
59920
59921
59922
59923
59924
59925
59926
59927
59928
59929
59930
59931
59932
59933
59934
59935
59936
59937
59938
59939
59940
59941
59942
59943
59944
59945
59946
59947
59948
59949
59950
59951
59952
59953
59954
59955
59956
59957
59958
59959
59960
59961
59962
59963
59964
59965
59966
59967
59968
59969
59970
59971
59972
59973
59974
59975
59976
59977
59978
59979
59980
59981
59982
59983
59984
59985
59986
59987
59988
59989
59990
59991
59992
59993
59994
59995
59996
59997
59998
59999
60000
60001
60002
60003
60004
60005
60006
60007
60008
60009
60010
60011
60012
60013
60014
60015
60016
60017
60018
60019
60020
60021
60022
60023
60024
60025
60026
60027
60028
60029
60030
60031
60032
60033
60034
60035
60036
60037
60038
60039
60040
60041
60042
60043
60044
60045
60046
60047
60048
60049
60050
60051
60052
60053
60054
60055
60056
60057
60058
60059
60060
60061
60062
60063
60064
60065
60066
60067
60068
60069
60070
60071
60072
60073
60074
60075
60076
60077
60078
60079
60080
60081
60082
60083
60084
60085
60086
60087
60088
60089
60090
60091
60092
60093
60094
60095
60096
60097
60098
60099
60100
60101
60102
60103
60104
60105
60106
60107
60108
60109
60110
60111
60112
60113
60114
60115
60116
60117
60118
60119
60120
60121
60122
60123
60124
60125
60126
60127
60128
60129
60130
60131
60132
60133
60134
60135
60136
60137
60138
60139
60140
60141
60142
60143
60144
60145
60146
60147
60148
60149
60150
60151
60152
60153
60154
60155
60156
60157
60158
60159
60160
60161
60162
60163
60164
60165
60166
60167
60168
60169
60170
60171
60172
60173
60174
60175
60176
60177
60178
60179
60180
60181
60182
60183
60184
60185
60186
60187
60188
60189
60190
60191
60192
60193
60194
60195
60196
60197
60198
60199
60200
60201
60202
60203
60204
60205
60206
60207
60208
60209
60210
60211
60212
60213
60214
60215
60216
60217
60218
60219
60220
60221
60222
60223
60224
60225
60226
60227
60228
60229
60230
60231
60232
60233
60234
60235
60236
60237
60238
60239
60240
60241
60242
60243
60244
60245
60246
60247
60248
60249
60250
60251
60252
60253
60254
60255
60256
60257
60258
60259
60260
60261
60262
60263
60264
60265
60266
60267
60268
60269
60270
60271
60272
60273
60274
60275
60276
60277
60278
60279
60280
60281
60282
60283
60284
60285
60286
60287
60288
60289
60290
60291
60292
60293
60294
60295
60296
60297
60298
60299
60300
60301
60302
60303
60304
60305
60306
60307
60308
60309
60310
60311
60312
60313
60314
60315
60316
60317
60318
60319
60320
60321
60322
60323
60324
60325
60326
60327
60328
60329
60330
60331
60332
60333
60334
60335
60336
60337
60338
60339
60340
60341
60342
60343
60344
60345
60346
60347
60348
60349
60350
60351
60352
60353
60354
60355
60356
60357
60358
60359
60360
60361
60362
60363
60364
60365
60366
60367
60368
60369
60370
60371
60372
60373
60374
60375
60376
60377
60378
60379
60380
60381
60382
60383
60384
60385
60386
60387
60388
60389
60390
60391
60392
60393
60394
60395
60396
60397
60398
60399
60400
60401
60402
60403
60404
60405
60406
60407
60408
60409
60410
60411
60412
60413
60414
60415
60416
60417
60418
60419
60420
60421
60422
60423
60424
60425
60426
60427
60428
60429
60430
60431
60432
60433
60434
60435
60436
60437
60438
60439
60440
60441
60442
60443
60444
60445
60446
60447
60448
60449
60450
60451
60452
60453
60454
60455
60456
60457
60458
60459
60460
60461
60462
60463
60464
60465
60466
60467
60468
60469
60470
60471
60472
60473
60474
60475
60476
60477
60478
60479
60480
60481
60482
60483
60484
60485
60486
60487
60488
60489
60490
60491
60492
60493
60494
60495
60496
60497
60498
60499
60500
60501
60502
60503
60504
60505
60506
60507
60508
60509
60510
60511
60512
60513
60514
60515
60516
60517
60518
60519
60520
60521
60522
60523
60524
60525
60526
60527
60528
60529
60530
60531
60532
60533
60534
60535
60536
60537
60538
60539
60540
60541
60542
60543
60544
60545
60546
60547
60548
60549
60550
60551
60552
60553
60554
60555
60556
60557
60558
60559
60560
60561
60562
60563
60564
60565
60566
60567
60568
60569
60570
60571
60572
60573
60574
60575
60576
60577
60578
60579
60580
60581
60582
60583
60584
60585
60586
60587
60588
60589
60590
60591
60592
60593
60594
60595
60596
60597
60598
60599
60600
60601
60602
60603
60604
60605
60606
60607
60608
60609
60610
60611
60612
60613
60614
60615
60616
60617
60618
60619
60620
60621
60622
60623
60624
60625
60626
60627
60628
60629
60630
60631
60632
60633
60634
60635
60636
60637
60638
60639
60640
60641
60642
60643
60644
60645
60646
60647
60648
60649
60650
60651
60652
60653
60654
60655
60656
60657
60658
60659
60660
60661
60662
60663
60664
60665
60666
60667
60668
60669
60670
60671
60672
60673
60674
60675
60676
60677
60678
60679
60680
60681
60682
60683
60684
60685
60686
60687
60688
60689
60690
60691
60692
60693
60694
60695
60696
60697
60698
60699
60700
60701
60702
60703
60704
60705
60706
60707
60708
60709
60710
60711
60712
60713
60714
60715
60716
60717
60718
60719
60720
60721
60722
60723
60724
60725
60726
60727
60728
60729
60730
60731
60732
60733
60734
60735
60736
60737
60738
60739
60740
60741
60742
60743
60744
60745
60746
60747
60748
60749
60750
60751
60752
60753
60754
60755
60756
60757
60758
60759
60760
60761
60762
60763
60764
60765
60766
60767
60768
60769
60770
60771
60772
60773
60774
60775
60776
60777
60778
60779
60780
60781
60782
60783
60784
60785
60786
60787
60788
60789
60790
60791
60792
60793
60794
60795
60796
60797
60798
60799
60800
60801
60802
60803
60804
60805
60806
60807
60808
60809
60810
60811
60812
60813
60814
60815
60816
60817
60818
60819
60820
60821
60822
60823
60824
60825
60826
60827
60828
60829
60830
60831
60832
60833
60834
60835
60836
60837
60838
60839
60840
60841
60842
60843
60844
60845
60846
60847
60848
60849
60850
60851
60852
60853
60854
60855
60856
60857
60858
60859
60860
60861
60862
60863
60864
60865
60866
60867
60868
60869
60870
60871
60872
60873
60874
60875
60876
60877
60878
60879
60880
60881
60882
60883
60884
60885
60886
60887
60888
60889
60890
60891
60892
60893
60894
60895
60896
60897
60898
60899
60900
60901
60902
60903
60904
60905
60906
60907
60908
60909
60910
60911
60912
60913
60914
60915
60916
60917
60918
60919
60920
60921
60922
60923
60924
60925
60926
60927
60928
60929
60930
60931
60932
60933
60934
60935
60936
60937
60938
60939
60940
60941
60942
60943
60944
60945
60946
60947
60948
60949
60950
60951
60952
60953
60954
60955
60956
60957
60958
60959
60960
60961
60962
60963
60964
60965
60966
60967
60968
60969
60970
60971
60972
60973
60974
60975
60976
60977
60978
60979
60980
60981
60982
60983
60984
60985
60986
60987
60988
60989
60990
60991
60992
60993
60994
60995
60996
60997
60998
60999
61000
61001
61002
61003
61004
61005
61006
61007
61008
61009
61010
61011
61012
61013
61014
61015
61016
61017
61018
61019
61020
61021
61022
61023
61024
61025
61026
61027
61028
61029
61030
61031
61032
61033
61034
61035
61036
61037
61038
61039
61040
61041
61042
61043
61044
61045
61046
61047
61048
61049
61050
61051
61052
61053
61054
61055
61056
61057
61058
61059
61060
61061
61062
61063
61064
61065
61066
61067
61068
61069
61070
61071
61072
61073
61074
61075
61076
61077
61078
61079
61080
61081
61082
61083
61084
61085
61086
61087
61088
61089
61090
61091
61092
61093
61094
61095
61096
61097
61098
61099
61100
61101
61102
61103
61104
61105
61106
61107
61108
61109
61110
61111
61112
61113
61114
61115
61116
61117
61118
61119
61120
61121
61122
61123
61124
61125
61126
61127
61128
61129
61130
61131
61132
61133
61134
61135
61136
61137
61138
61139
61140
61141
61142
61143
61144
61145
61146
61147
61148
61149
61150
61151
61152
61153
61154
61155
61156
61157
61158
61159
61160
61161
61162
61163
61164
61165
61166
61167
61168
61169
61170
61171
61172
61173
61174
61175
61176
61177
61178
61179
61180
61181
61182
61183
61184
61185
61186
61187
61188
61189
61190
61191
61192
61193
61194
61195
61196
61197
61198
61199
61200
61201
61202
61203
61204
61205
61206
61207
61208
61209
61210
61211
61212
61213
61214
61215
61216
61217
61218
61219
61220
61221
61222
61223
61224
61225
61226
61227
61228
61229
61230
61231
61232
61233
61234
61235
61236
61237
61238
61239
61240
61241
61242
61243
61244
61245
61246
61247
61248
61249
61250
61251
61252
61253
61254
61255
61256
61257
61258
61259
61260
61261
61262
61263
61264
61265
61266
61267
61268
61269
61270
61271
61272
61273
61274
61275
61276
61277
61278
61279
61280
61281
61282
61283
61284
61285
61286
61287
61288
61289
61290
61291
61292
61293
61294
61295
61296
61297
61298
61299
61300
61301
61302
61303
61304
61305
61306
61307
61308
61309
61310
61311
61312
61313
61314
61315
61316
61317
61318
61319
61320
61321
61322
61323
61324
61325
61326
61327
61328
61329
61330
61331
61332
61333
61334
61335
61336
61337
61338
61339
61340
61341
61342
61343
61344
61345
61346
61347
61348
61349
61350
61351
61352
61353
61354
61355
61356
61357
61358
61359
61360
61361
61362
61363
61364
61365
61366
61367
61368
61369
61370
61371
61372
61373
61374
61375
61376
61377
61378
61379
61380
61381
61382
61383
61384
61385
61386
61387
61388
61389
61390
61391
61392
61393
61394
61395
61396
61397
61398
61399
61400
61401
61402
61403
61404
61405
61406
61407
61408
61409
61410
61411
61412
61413
61414
61415
61416
61417
61418
61419
61420
61421
61422
61423
61424
61425
61426
61427
61428
61429
61430
61431
61432
61433
61434
61435
61436
61437
61438
61439
61440
61441
61442
61443
61444
61445
61446
61447
61448
61449
61450
61451
61452
61453
61454
61455
61456
61457
61458
61459
61460
61461
61462
61463
61464
61465
61466
61467
61468
61469
61470
61471
61472
61473
61474
61475
61476
61477
61478
61479
61480
61481
61482
61483
61484
61485
61486
61487
61488
61489
61490
61491
61492
61493
61494
61495
61496
61497
61498
61499
61500
61501
61502
61503
61504
61505
61506
61507
61508
61509
61510
61511
61512
61513
61514
61515
61516
61517
61518
61519
61520
61521
61522
61523
61524
61525
61526
61527
61528
61529
61530
61531
61532
61533
61534
61535
61536
61537
61538
61539
61540
61541
61542
61543
61544
61545
61546
61547
61548
61549
61550
61551
61552
61553
61554
61555
61556
61557
61558
61559
61560
61561
61562
61563
61564
61565
61566
61567
61568
61569
61570
61571
61572
61573
61574
61575
61576
61577
61578
61579
61580
61581
61582
61583
61584
61585
61586
61587
61588
61589
61590
61591
61592
61593
61594
61595
61596
61597
61598
61599
61600
61601
61602
61603
61604
61605
61606
61607
61608
61609
61610
61611
61612
61613
61614
61615
61616
61617
61618
61619
61620
61621
61622
61623
61624
61625
61626
61627
61628
61629
61630
61631
61632
61633
61634
61635
61636
61637
61638
61639
61640
61641
61642
61643
61644
61645
61646
61647
61648
61649
61650
61651
61652
61653
61654
61655
61656
61657
61658
61659
61660
61661
61662
61663
61664
61665
61666
61667
61668
61669
61670
61671
61672
61673
61674
61675
61676
61677
61678
61679
61680
61681
61682
61683
61684
61685
61686
61687
61688
61689
61690
61691
61692
61693
61694
61695
61696
61697
61698
61699
61700
61701
61702
61703
61704
61705
61706
61707
61708
61709
61710
61711
61712
61713
61714
61715
61716
61717
61718
61719
61720
61721
61722
61723
61724
61725
61726
61727
61728
61729
61730
61731
61732
61733
61734
61735
61736
61737
61738
61739
61740
61741
61742
61743
61744
61745
61746
61747
61748
61749
61750
61751
61752
61753
61754
61755
61756
61757
61758
61759
61760
61761
61762
61763
61764
61765
61766
61767
61768
61769
61770
61771
61772
61773
61774
61775
61776
61777
61778
61779
61780
61781
61782
61783
61784
61785
61786
61787
61788
61789
61790
61791
61792
61793
61794
61795
61796
61797
61798
61799
61800
61801
61802
61803
61804
61805
61806
61807
61808
61809
61810
61811
61812
61813
61814
61815
61816
61817
61818
61819
61820
61821
61822
61823
61824
61825
61826
61827
61828
61829
61830
61831
61832
61833
61834
61835
61836
61837
61838
61839
61840
61841
61842
61843
61844
61845
61846
61847
61848
61849
61850
61851
61852
61853
61854
61855
61856
61857
61858
61859
61860
61861
61862
61863
61864
61865
61866
61867
61868
61869
61870
61871
61872
61873
61874
61875
61876
61877
61878
61879
61880
61881
61882
61883
61884
61885
61886
61887
61888
61889
61890
61891
61892
61893
61894
61895
61896
61897
61898
61899
61900
61901
61902
61903
61904
61905
61906
61907
61908
61909
61910
61911
61912
61913
61914
61915
61916
61917
61918
61919
61920
61921
61922
61923
61924
61925
61926
61927
61928
61929
61930
61931
61932
61933
61934
61935
61936
61937
61938
61939
61940
61941
61942
61943
61944
61945
61946
61947
61948
61949
61950
61951
61952
61953
61954
61955
61956
61957
61958
61959
61960
61961
61962
61963
61964
61965
61966
61967
61968
61969
61970
61971
61972
61973
61974
61975
61976
61977
61978
61979
61980
61981
61982
61983
61984
61985
61986
61987
61988
61989
61990
61991
61992
61993
61994
61995
61996
61997
61998
61999
62000
62001
62002
62003
62004
62005
62006
62007
62008
62009
62010
62011
62012
62013
62014
62015
62016
62017
62018
62019
62020
62021
62022
62023
62024
62025
62026
62027
62028
62029
62030
62031
62032
62033
62034
62035
62036
62037
62038
62039
62040
62041
62042
62043
62044
62045
62046
62047
62048
62049
62050
62051
62052
62053
62054
62055
62056
62057
62058
62059
62060
62061
62062
62063
62064
62065
62066
62067
62068
62069
62070
62071
62072
62073
62074
62075
62076
62077
62078
62079
62080
62081
62082
62083
62084
62085
62086
62087
62088
62089
62090
62091
62092
62093
62094
62095
62096
62097
62098
62099
62100
62101
62102
62103
62104
62105
62106
62107
62108
62109
62110
62111
62112
62113
62114
62115
62116
62117
62118
62119
62120
62121
62122
62123
62124
62125
62126
62127
62128
62129
62130
62131
62132
62133
62134
62135
62136
62137
62138
62139
62140
62141
62142
62143
62144
62145
62146
62147
62148
62149
62150
62151
62152
62153
62154
62155
62156
62157
62158
62159
62160
62161
62162
62163
62164
62165
62166
62167
62168
62169
62170
62171
62172
62173
62174
62175
62176
62177
62178
62179
62180
62181
62182
62183
62184
62185
62186
62187
62188
62189
62190
62191
62192
62193
62194
62195
62196
62197
62198
62199
62200
62201
62202
62203
62204
62205
62206
62207
62208
62209
62210
62211
62212
62213
62214
62215
62216
62217
62218
62219
62220
62221
62222
62223
62224
62225
62226
62227
62228
62229
62230
62231
62232
62233
62234
62235
62236
62237
62238
62239
62240
62241
62242
62243
62244
62245
62246
62247
62248
62249
62250
62251
62252
62253
62254
62255
62256
62257
62258
62259
62260
62261
62262
62263
62264
62265
62266
62267
62268
62269
62270
62271
62272
62273
62274
62275
62276
62277
62278
62279
62280
62281
62282
62283
62284
62285
62286
62287
62288
62289
62290
62291
62292
62293
62294
62295
62296
62297
62298
62299
62300
62301
62302
62303
62304
62305
62306
62307
62308
62309
62310
62311
62312
62313
62314
62315
62316
62317
62318
62319
62320
62321
62322
62323
62324
62325
62326
62327
62328
62329
62330
62331
62332
62333
62334
62335
62336
62337
62338
62339
62340
62341
62342
62343
62344
62345
62346
62347
62348
62349
62350
62351
62352
62353
62354
62355
62356
62357
62358
62359
62360
62361
62362
62363
62364
62365
62366
62367
62368
62369
62370
62371
62372
62373
62374
62375
62376
62377
62378
62379
62380
62381
62382
62383
62384
62385
62386
62387
62388
62389
62390
62391
62392
62393
62394
62395
62396
62397
62398
62399
62400
62401
62402
62403
62404
62405
62406
62407
62408
62409
62410
62411
62412
62413
62414
62415
62416
62417
62418
62419
62420
62421
62422
62423
62424
62425
62426
62427
62428
62429
62430
62431
62432
62433
62434
62435
62436
62437
62438
62439
62440
62441
62442
62443
62444
62445
62446
62447
62448
62449
62450
62451
62452
62453
62454
62455
62456
62457
62458
62459
62460
62461
62462
62463
62464
62465
62466
62467
62468
62469
62470
62471
62472
62473
62474
62475
62476
62477
62478
62479
62480
62481
62482
62483
62484
62485
62486
62487
62488
62489
62490
62491
62492
62493
62494
62495
62496
62497
62498
62499
62500
62501
62502
62503
62504
62505
62506
62507
62508
62509
62510
62511
62512
62513
62514
62515
62516
62517
62518
62519
62520
62521
62522
62523
62524
62525
62526
62527
62528
62529
62530
62531
62532
62533
62534
62535
62536
62537
62538
62539
62540
62541
62542
62543
62544
62545
62546
62547
62548
62549
62550
62551
62552
62553
62554
62555
62556
62557
62558
62559
62560
62561
62562
62563
62564
62565
62566
62567
62568
62569
62570
62571
62572
62573
62574
62575
62576
62577
62578
62579
62580
62581
62582
62583
62584
62585
62586
62587
62588
62589
62590
62591
62592
62593
62594
62595
62596
62597
62598
62599
62600
62601
62602
62603
62604
62605
62606
62607
62608
62609
62610
62611
62612
62613
62614
62615
62616
62617
62618
62619
62620
62621
62622
62623
62624
62625
62626
62627
62628
62629
62630
62631
62632
62633
62634
62635
62636
62637
62638
62639
62640
62641
62642
62643
62644
62645
62646
62647
62648
62649
62650
62651
62652
62653
62654
62655
62656
62657
62658
62659
62660
62661
62662
62663
62664
62665
62666
62667
62668
62669
62670
62671
62672
62673
62674
62675
62676
62677
62678
62679
62680
62681
62682
62683
62684
62685
62686
62687
62688
62689
62690
62691
62692
62693
62694
62695
62696
62697
62698
62699
62700
62701
62702
62703
62704
62705
62706
62707
62708
62709
62710
62711
62712
62713
62714
62715
62716
62717
62718
62719
62720
62721
62722
62723
62724
62725
62726
62727
62728
62729
62730
62731
62732
62733
62734
62735
62736
62737
62738
62739
62740
62741
62742
62743
62744
62745
62746
62747
62748
62749
62750
62751
62752
62753
62754
62755
62756
62757
62758
62759
62760
62761
62762
62763
62764
62765
62766
62767
62768
62769
62770
62771
62772
62773
62774
62775
62776
62777
62778
62779
62780
62781
62782
62783
62784
62785
62786
62787
62788
62789
62790
62791
62792
62793
62794
62795
62796
62797
62798
62799
62800
62801
62802
62803
62804
62805
62806
62807
62808
62809
62810
62811
62812
62813
62814
62815
62816
62817
62818
62819
62820
62821
62822
62823
62824
62825
62826
62827
62828
62829
62830
62831
62832
62833
62834
62835
62836
62837
62838
62839
62840
62841
62842
62843
62844
62845
62846
62847
62848
62849
62850
62851
62852
62853
62854
62855
62856
62857
62858
62859
62860
62861
62862
62863
62864
62865
62866
62867
62868
62869
62870
62871
62872
62873
62874
62875
62876
62877
62878
62879
62880
62881
62882
62883
62884
62885
62886
62887
62888
62889
62890
62891
62892
62893
62894
62895
62896
62897
62898
62899
62900
62901
62902
62903
62904
62905
62906
62907
62908
62909
62910
62911
62912
62913
62914
62915
62916
62917
62918
62919
62920
62921
62922
62923
62924
62925
62926
62927
62928
62929
62930
62931
62932
62933
62934
62935
62936
62937
62938
62939
62940
62941
62942
62943
62944
62945
62946
62947
62948
62949
62950
62951
62952
62953
62954
62955
62956
62957
62958
62959
62960
62961
62962
62963
62964
62965
62966
62967
62968
62969
62970
62971
62972
62973
62974
62975
62976
62977
62978
62979
62980
62981
62982
62983
62984
62985
62986
62987
62988
62989
62990
62991
62992
62993
62994
62995
62996
62997
62998
62999
63000
63001
63002
63003
63004
63005
63006
63007
63008
63009
63010
63011
63012
63013
63014
63015
63016
63017
63018
63019
63020
63021
63022
63023
63024
63025
63026
63027
63028
63029
63030
63031
63032
63033
63034
63035
63036
63037
63038
63039
63040
63041
63042
63043
63044
63045
63046
63047
63048
63049
63050
63051
63052
63053
63054
63055
63056
63057
63058
63059
63060
63061
63062
63063
63064
63065
63066
63067
63068
63069
63070
63071
63072
63073
63074
63075
63076
63077
63078
63079
63080
63081
63082
63083
63084
63085
63086
63087
63088
63089
63090
63091
63092
63093
63094
63095
63096
63097
63098
63099
63100
63101
63102
63103
63104
63105
63106
63107
63108
63109
63110
63111
63112
63113
63114
63115
63116
63117
63118
63119
63120
63121
63122
63123
63124
63125
63126
63127
63128
63129
63130
63131
63132
63133
63134
63135
63136
63137
63138
63139
63140
63141
63142
63143
63144
63145
63146
63147
63148
63149
63150
63151
63152
63153
63154
63155
63156
63157
63158
63159
63160
63161
63162
63163
63164
63165
63166
63167
63168
63169
63170
63171
63172
63173
63174
63175
63176
63177
63178
63179
63180
63181
63182
63183
63184
63185
63186
63187
63188
63189
63190
63191
63192
63193
63194
63195
63196
63197
63198
63199
63200
63201
63202
63203
63204
63205
63206
63207
63208
63209
63210
63211
63212
63213
63214
63215
63216
63217
63218
63219
63220
63221
63222
63223
63224
63225
63226
63227
63228
63229
63230
63231
63232
63233
63234
63235
63236
63237
63238
63239
63240
63241
63242
63243
63244
63245
63246
63247
63248
63249
63250
63251
63252
63253
63254
63255
63256
63257
63258
63259
63260
63261
63262
63263
63264
63265
63266
63267
63268
63269
63270
63271
63272
63273
63274
63275
63276
63277
63278
63279
63280
63281
63282
63283
63284
63285
63286
63287
63288
63289
63290
63291
63292
63293
63294
63295
63296
63297
63298
63299
63300
63301
63302
63303
63304
63305
63306
63307
63308
63309
63310
63311
63312
63313
63314
63315
63316
63317
63318
63319
63320
63321
63322
63323
63324
63325
63326
63327
63328
63329
63330
63331
63332
63333
63334
63335
63336
63337
63338
63339
63340
63341
63342
63343
63344
63345
63346
63347
63348
63349
63350
63351
63352
63353
63354
63355
63356
63357
63358
63359
63360
63361
63362
63363
63364
63365
63366
63367
63368
63369
63370
63371
63372
63373
63374
63375
63376
63377
63378
63379
63380
63381
63382
63383
63384
63385
63386
63387
63388
63389
63390
63391
63392
63393
63394
63395
63396
63397
63398
63399
63400
63401
63402
63403
63404
63405
63406
63407
63408
63409
63410
63411
63412
63413
63414
63415
63416
63417
63418
63419
63420
63421
63422
63423
63424
63425
63426
63427
63428
63429
63430
63431
63432
63433
63434
63435
63436
63437
63438
63439
63440
63441
63442
63443
63444
63445
63446
63447
63448
63449
63450
63451
63452
63453
63454
63455
63456
63457
63458
63459
63460
63461
63462
63463
63464
63465
63466
63467
63468
63469
63470
63471
63472
63473
63474
63475
63476
63477
63478
63479
63480
63481
63482
63483
63484
63485
63486
63487
63488
63489
63490
63491
63492
63493
63494
63495
63496
63497
63498
63499
63500
63501
63502
63503
63504
63505
63506
63507
63508
63509
63510
63511
63512
63513
63514
63515
63516
63517
63518
63519
63520
63521
63522
63523
63524
63525
63526
63527
63528
63529
63530
63531
63532
63533
63534
63535
63536
63537
63538
63539
63540
63541
63542
63543
63544
63545
63546
63547
63548
63549
63550
63551
63552
63553
63554
63555
63556
63557
63558
63559
63560
63561
63562
63563
63564
63565
63566
63567
63568
63569
63570
63571
63572
63573
63574
63575
63576
63577
63578
63579
63580
63581
63582
63583
63584
63585
63586
63587
63588
63589
63590
63591
63592
63593
63594
63595
63596
63597
63598
63599
63600
63601
63602
63603
63604
63605
63606
63607
63608
63609
63610
63611
63612
63613
63614
63615
63616
63617
63618
63619
63620
63621
63622
63623
63624
63625
63626
63627
63628
63629
63630
63631
63632
63633
63634
63635
63636
63637
63638
63639
63640
63641
63642
63643
63644
63645
63646
63647
63648
63649
63650
63651
63652
63653
63654
63655
63656
63657
63658
63659
63660
63661
63662
63663
63664
63665
63666
63667
63668
63669
63670
63671
63672
63673
63674
63675
63676
63677
63678
63679
63680
63681
63682
63683
63684
63685
63686
63687
63688
63689
63690
63691
63692
63693
63694
63695
63696
63697
63698
63699
63700
63701
63702
63703
63704
63705
63706
63707
63708
63709
63710
63711
63712
63713
63714
63715
63716
63717
63718
63719
63720
63721
63722
63723
63724
63725
63726
63727
63728
63729
63730
63731
63732
63733
63734
63735
63736
63737
63738
63739
63740
63741
63742
63743
63744
63745
63746
63747
63748
63749
63750
63751
63752
63753
63754
63755
63756
63757
63758
63759
63760
63761
63762
63763
63764
63765
63766
63767
63768
63769
63770
63771
63772
63773
63774
63775
63776
63777
63778
63779
63780
63781
63782
63783
63784
63785
63786
63787
63788
63789
63790
63791
63792
63793
63794
63795
63796
63797
63798
63799
63800
63801
63802
63803
63804
63805
63806
63807
63808
63809
63810
63811
63812
63813
63814
63815
63816
63817
63818
63819
63820
63821
63822
63823
63824
63825
63826
63827
63828
63829
63830
63831
63832
63833
63834
63835
63836
63837
63838
63839
63840
63841
63842
63843
63844
63845
63846
63847
63848
63849
63850
63851
63852
63853
63854
63855
63856
63857
63858
63859
63860
63861
63862
63863
63864
63865
63866
63867
63868
63869
63870
63871
63872
63873
63874
63875
63876
63877
63878
63879
63880
63881
63882
63883
63884
63885
63886
63887
63888
63889
63890
63891
63892
63893
63894
63895
63896
63897
63898
63899
63900
63901
63902
63903
63904
63905
63906
63907
63908
63909
63910
63911
63912
63913
63914
63915
63916
63917
63918
63919
63920
63921
63922
63923
63924
63925
63926
63927
63928
63929
63930
63931
63932
63933
63934
63935
63936
63937
63938
63939
63940
63941
63942
63943
63944
63945
63946
63947
63948
63949
63950
63951
63952
63953
63954
63955
63956
63957
63958
63959
63960
63961
63962
63963
63964
63965
63966
63967
63968
63969
63970
63971
63972
63973
63974
63975
63976
63977
63978
63979
63980
63981
63982
63983
63984
63985
63986
63987
63988
63989
63990
63991
63992
63993
63994
63995
63996
63997
63998
63999
64000
64001
64002
64003
64004
64005
64006
64007
64008
64009
64010
64011
64012
64013
64014
64015
64016
64017
64018
64019
64020
64021
64022
64023
64024
64025
64026
64027
64028
64029
64030
64031
64032
64033
64034
64035
64036
64037
64038
64039
64040
64041
64042
64043
64044
64045
64046
64047
64048
64049
64050
64051
64052
64053
64054
64055
64056
64057
64058
64059
64060
64061
64062
64063
64064
64065
64066
64067
64068
64069
64070
64071
64072
64073
64074
64075
64076
64077
64078
64079
64080
64081
64082
64083
64084
64085
64086
64087
64088
64089
64090
64091
64092
64093
64094
64095
64096
64097
64098
64099
64100
64101
64102
64103
64104
64105
64106
64107
64108
64109
64110
64111
64112
64113
64114
64115
64116
64117
64118
64119
64120
64121
64122
64123
64124
64125
64126
64127
64128
64129
64130
64131
64132
64133
64134
64135
64136
64137
64138
64139
64140
64141
64142
64143
64144
64145
64146
64147
64148
64149
64150
64151
64152
64153
64154
64155
64156
64157
64158
64159
64160
64161
64162
64163
64164
64165
64166
64167
64168
64169
64170
64171
64172
64173
64174
64175
64176
64177
64178
64179
64180
64181
64182
64183
64184
64185
64186
64187
64188
64189
64190
64191
64192
64193
64194
64195
64196
64197
64198
64199
64200
64201
64202
64203
64204
64205
64206
64207
64208
64209
64210
64211
64212
64213
64214
64215
64216
64217
64218
64219
64220
64221
64222
64223
64224
64225
64226
64227
64228
64229
64230
64231
64232
64233
64234
64235
64236
64237
64238
64239
64240
64241
64242
64243
64244
64245
64246
64247
64248
64249
64250
64251
64252
64253
64254
64255
64256
64257
64258
64259
64260
64261
64262
64263
64264
64265
64266
64267
64268
64269
64270
64271
64272
64273
64274
64275
64276
64277
64278
64279
64280
64281
64282
64283
64284
64285
64286
64287
64288
64289
64290
64291
64292
64293
64294
64295
64296
64297
64298
64299
64300
64301
64302
64303
64304
64305
64306
64307
64308
64309
64310
64311
64312
64313
64314
64315
64316
64317
64318
64319
64320
64321
64322
64323
64324
64325
64326
64327
64328
64329
64330
64331
64332
64333
64334
64335
64336
64337
64338
64339
64340
64341
64342
64343
64344
64345
64346
64347
64348
64349
64350
64351
64352
64353
64354
64355
64356
64357
64358
64359
64360
64361
64362
64363
64364
64365
64366
64367
64368
64369
64370
64371
64372
64373
64374
64375
64376
64377
64378
64379
64380
64381
64382
64383
64384
64385
64386
64387
64388
64389
64390
64391
64392
64393
64394
64395
64396
64397
64398
64399
64400
64401
64402
64403
64404
64405
64406
64407
64408
64409
64410
64411
64412
64413
64414
64415
64416
64417
64418
64419
64420
64421
64422
64423
64424
64425
64426
64427
64428
64429
64430
64431
64432
64433
64434
64435
64436
64437
64438
64439
64440
64441
64442
64443
64444
64445
64446
64447
64448
64449
64450
64451
64452
64453
64454
64455
64456
64457
64458
64459
64460
64461
64462
64463
64464
64465
64466
64467
64468
64469
64470
64471
64472
64473
64474
64475
64476
64477
64478
64479
64480
64481
64482
64483
64484
64485
64486
64487
64488
64489
64490
64491
64492
64493
64494
64495
64496
64497
64498
64499
64500
64501
64502
64503
64504
64505
64506
64507
64508
64509
64510
64511
64512
64513
64514
64515
64516
64517
64518
64519
64520
64521
64522
64523
64524
64525
64526
64527
64528
64529
64530
64531
64532
64533
64534
64535
64536
64537
64538
64539
64540
64541
64542
64543
64544
64545
64546
64547
64548
64549
64550
64551
64552
64553
64554
64555
64556
64557
64558
64559
64560
64561
64562
64563
64564
64565
64566
64567
64568
64569
64570
64571
64572
64573
64574
64575
64576
64577
64578
64579
64580
64581
64582
64583
64584
64585
64586
64587
64588
64589
64590
64591
64592
64593
64594
64595
64596
64597
64598
64599
64600
64601
64602
64603
64604
64605
64606
64607
64608
64609
64610
64611
64612
64613
64614
64615
64616
64617
64618
64619
64620
64621
64622
64623
64624
64625
64626
64627
64628
64629
64630
64631
64632
64633
64634
64635
64636
64637
64638
64639
64640
64641
64642
64643
64644
64645
64646
64647
64648
64649
64650
64651
64652
64653
64654
64655
64656
64657
64658
64659
64660
64661
64662
64663
64664
64665
64666
64667
64668
64669
64670
64671
64672
64673
64674
64675
64676
64677
64678
64679
64680
64681
64682
64683
64684
64685
64686
64687
64688
64689
64690
64691
64692
64693
64694
64695
64696
64697
64698
64699
64700
64701
64702
64703
64704
64705
64706
64707
64708
64709
64710
64711
64712
64713
64714
64715
64716
64717
64718
64719
64720
64721
64722
64723
64724
64725
64726
64727
64728
64729
64730
64731
64732
64733
64734
64735
64736
64737
64738
64739
64740
64741
64742
64743
64744
64745
64746
64747
64748
64749
64750
64751
64752
64753
64754
64755
64756
64757
64758
64759
64760
64761
64762
64763
64764
64765
64766
64767
64768
64769
64770
64771
64772
64773
64774
64775
64776
64777
64778
64779
64780
64781
64782
64783
64784
64785
64786
64787
64788
64789
64790
64791
64792
64793
64794
64795
64796
64797
64798
64799
64800
64801
64802
64803
64804
64805
64806
64807
64808
64809
64810
64811
64812
64813
64814
64815
64816
64817
64818
64819
64820
64821
64822
64823
64824
64825
64826
64827
64828
64829
64830
64831
64832
64833
64834
64835
64836
64837
64838
64839
64840
64841
64842
64843
64844
64845
64846
64847
64848
64849
64850
64851
64852
64853
64854
64855
64856
64857
64858
64859
64860
64861
64862
64863
64864
64865
64866
64867
64868
64869
64870
64871
64872
64873
64874
64875
64876
64877
64878
64879
64880
64881
64882
64883
64884
64885
64886
64887
64888
64889
64890
64891
64892
64893
64894
64895
64896
64897
64898
64899
64900
64901
64902
64903
64904
64905
64906
64907
64908
64909
64910
64911
64912
64913
64914
64915
64916
64917
64918
64919
64920
64921
64922
64923
64924
64925
64926
64927
64928
64929
64930
64931
64932
64933
64934
64935
64936
64937
64938
64939
64940
64941
64942
64943
64944
64945
64946
64947
64948
64949
64950
64951
64952
64953
64954
64955
64956
64957
64958
64959
64960
64961
64962
64963
64964
64965
64966
64967
64968
64969
64970
64971
64972
64973
64974
64975
64976
64977
64978
64979
64980
64981
64982
64983
64984
64985
64986
64987
64988
64989
64990
64991
64992
64993
64994
64995
64996
64997
64998
64999
65000
65001
65002
65003
65004
65005
65006
65007
65008
65009
65010
65011
65012
65013
65014
65015
65016
65017
65018
65019
65020
65021
65022
65023
65024
65025
65026
65027
65028
65029
65030
65031
65032
65033
65034
65035
65036
65037
65038
65039
65040
65041
65042
65043
65044
65045
65046
65047
65048
65049
65050
65051
65052
65053
65054
65055
65056
65057
65058
65059
65060
65061
65062
65063
65064
65065
65066
65067
65068
65069
65070
65071
65072
65073
65074
65075
65076
65077
65078
65079
65080
65081
65082
65083
65084
65085
65086
65087
65088
65089
65090
65091
65092
65093
65094
65095
65096
65097
65098
65099
65100
65101
65102
65103
65104
65105
65106
65107
65108
65109
65110
65111
65112
65113
65114
65115
65116
65117
65118
65119
65120
65121
65122
65123
65124
65125
65126
65127
65128
65129
65130
65131
65132
65133
65134
65135
65136
65137
65138
65139
65140
65141
65142
65143
65144
65145
65146
65147
65148
65149
65150
65151
65152
65153
65154
65155
65156
65157
65158
65159
65160
65161
65162
65163
65164
65165
65166
65167
65168
65169
65170
65171
65172
65173
65174
65175
65176
65177
65178
65179
65180
65181
65182
65183
65184
65185
65186
65187
65188
65189
65190
65191
65192
65193
65194
65195
65196
65197
65198
65199
65200
65201
65202
65203
65204
65205
65206
65207
65208
65209
65210
65211
65212
65213
65214
65215
65216
65217
65218
65219
65220
65221
65222
65223
65224
65225
65226
65227
65228
65229
65230
65231
65232
65233
65234
65235
65236
65237
65238
65239
65240
65241
65242
65243
65244
65245
65246
65247
65248
65249
65250
65251
65252
65253
65254
65255
65256
65257
65258
65259
65260
65261
65262
65263
65264
65265
65266
65267
65268
65269
65270
65271
65272
65273
65274
65275
65276
65277
65278
65279
65280
65281
65282
65283
65284
65285
65286
65287
65288
65289
65290
65291
65292
65293
65294
65295
65296
65297
65298
65299
65300
65301
65302
65303
65304
65305
65306
65307
65308
65309
65310
65311
65312
65313
65314
65315
65316
65317
65318
65319
65320
65321
65322
65323
65324
65325
65326
65327
65328
65329
65330
65331
65332
65333
65334
65335
65336
65337
65338
65339
65340
65341
65342
65343
65344
65345
65346
65347
65348
65349
65350
65351
65352
65353
65354
65355
65356
65357
65358
65359
65360
65361
65362
65363
65364
65365
65366
65367
65368
65369
65370
65371
65372
65373
65374
65375
65376
65377
65378
65379
65380
65381
65382
65383
65384
65385
65386
65387
65388
65389
65390
65391
65392
65393
65394
65395
65396
65397
65398
65399
65400
65401
65402
65403
65404
65405
65406
65407
65408
65409
65410
65411
65412
65413
65414
65415
65416
65417
65418
65419
65420
65421
65422
65423
65424
65425
65426
65427
65428
65429
65430
65431
65432
65433
65434
65435
65436
65437
65438
65439
65440
65441
65442
65443
65444
65445
65446
65447
65448
65449
65450
65451
65452
65453
65454
65455
65456
65457
65458
65459
65460
65461
65462
65463
65464
65465
65466
65467
65468
65469
65470
65471
65472
65473
65474
65475
65476
65477
65478
65479
65480
65481
65482
65483
65484
65485
65486
65487
65488
65489
65490
65491
65492
65493
65494
65495
65496
65497
65498
65499
65500
65501
65502
65503
65504
65505
65506
65507
65508
65509
65510
65511
65512
65513
65514
65515
65516
65517
65518
65519
65520
65521
65522
65523
65524
65525
65526
65527
65528
65529
65530
65531
65532
65533
65534
65535
65536
65537
65538
65539
65540
65541
65542
65543
65544
65545
65546
65547
65548
65549
65550
65551
65552
65553
65554
65555
65556
65557
65558
65559
65560
65561
65562
65563
65564
65565
65566
65567
65568
65569
65570
65571
65572
65573
65574
65575
65576
65577
65578
65579
65580
65581
65582
65583
65584
65585
65586
65587
65588
65589
65590
65591
65592
65593
65594
65595
65596
65597
65598
65599
65600
65601
65602
65603
65604
65605
65606
65607
65608
65609
65610
65611
65612
65613
65614
65615
65616
65617
65618
65619
65620
65621
65622
65623
65624
65625
65626
65627
65628
65629
65630
65631
65632
65633
65634
65635
65636
65637
65638
65639
65640
65641
65642
65643
65644
65645
65646
65647
65648
65649
65650
65651
65652
65653
65654
65655
65656
65657
65658
65659
65660
65661
65662
65663
65664
65665
65666
65667
65668
65669
65670
65671
65672
65673
65674
65675
65676
65677
65678
65679
65680
65681
65682
65683
65684
65685
65686
65687
65688
65689
65690
65691
65692
65693
65694
65695
65696
65697
65698
65699
65700
65701
65702
65703
65704
65705
65706
65707
65708
65709
65710
65711
65712
65713
65714
65715
65716
65717
65718
65719
65720
65721
65722
65723
65724
65725
65726
65727
65728
65729
65730
65731
65732
65733
65734
65735
65736
65737
65738
65739
65740
65741
65742
65743
65744
65745
65746
65747
65748
65749
65750
65751
65752
65753
65754
65755
65756
65757
65758
65759
65760
65761
65762
65763
65764
65765
65766
65767
65768
65769
65770
65771
65772
65773
65774
65775
65776
65777
65778
65779
65780
65781
65782
65783
65784
65785
65786
65787
65788
65789
65790
65791
65792
65793
65794
65795
65796
65797
65798
65799
65800
65801
65802
65803
65804
65805
65806
65807
65808
65809
65810
65811
65812
65813
65814
65815
65816
65817
65818
65819
65820
65821
65822
65823
65824
65825
65826
65827
65828
65829
65830
65831
65832
65833
65834
65835
65836
65837
65838
65839
65840
65841
65842
65843
65844
65845
65846
65847
65848
65849
65850
65851
65852
65853
65854
65855
65856
65857
65858
65859
65860
65861
65862
65863
65864
65865
65866
65867
65868
65869
65870
65871
65872
65873
65874
65875
65876
65877
65878
65879
65880
65881
65882
65883
65884
65885
65886
65887
65888
65889
65890
65891
65892
65893
65894
65895
65896
65897
65898
65899
65900
65901
65902
65903
65904
65905
65906
65907
65908
65909
65910
65911
65912
65913
65914
65915
65916
65917
65918
65919
65920
65921
65922
65923
65924
65925
65926
65927
65928
65929
65930
65931
65932
65933
65934
65935
65936
65937
65938
65939
65940
65941
65942
65943
65944
65945
65946
65947
65948
65949
65950
65951
65952
65953
65954
65955
65956
65957
65958
65959
65960
65961
65962
65963
65964
65965
65966
65967
65968
65969
65970
65971
65972
65973
65974
65975
65976
65977
65978
65979
65980
65981
65982
65983
65984
65985
65986
65987
65988
65989
65990
65991
65992
65993
65994
65995
65996
65997
65998
65999
66000
66001
66002
66003
66004
66005
66006
66007
66008
66009
66010
66011
66012
66013
66014
66015
66016
66017
66018
66019
66020
66021
66022
66023
66024
66025
66026
66027
66028
66029
66030
66031
66032
66033
66034
66035
66036
66037
66038
66039
66040
66041
66042
66043
66044
66045
66046
66047
66048
66049
66050
66051
66052
66053
66054
66055
66056
66057
66058
66059
66060
66061
66062
66063
66064
66065
66066
66067
66068
66069
66070
66071
66072
66073
66074
66075
66076
66077
66078
66079
66080
66081
66082
66083
66084
66085
66086
66087
66088
66089
66090
66091
66092
66093
66094
66095
66096
66097
66098
66099
66100
66101
66102
66103
66104
66105
66106
66107
66108
66109
66110
66111
66112
66113
66114
66115
66116
66117
66118
66119
66120
66121
66122
66123
66124
66125
66126
66127
66128
66129
66130
66131
66132
66133
66134
66135
66136
66137
66138
66139
66140
66141
66142
66143
66144
66145
66146
66147
66148
66149
66150
66151
66152
66153
66154
66155
66156
66157
66158
66159
66160
66161
66162
66163
66164
66165
66166
66167
66168
66169
66170
66171
66172
66173
66174
66175
66176
66177
66178
66179
66180
66181
66182
66183
66184
66185
66186
66187
66188
66189
66190
66191
66192
66193
66194
66195
66196
66197
66198
66199
66200
66201
66202
66203
66204
66205
66206
66207
66208
66209
66210
66211
66212
66213
66214
66215
66216
66217
66218
66219
66220
66221
66222
66223
66224
66225
66226
66227
66228
66229
66230
66231
66232
66233
66234
66235
66236
66237
66238
66239
66240
66241
66242
66243
66244
66245
66246
66247
66248
66249
66250
66251
66252
66253
66254
66255
66256
66257
66258
66259
66260
66261
66262
66263
66264
66265
66266
66267
66268
66269
66270
66271
66272
66273
66274
66275
66276
66277
66278
66279
66280
66281
66282
66283
66284
66285
66286
66287
66288
66289
66290
66291
66292
66293
66294
66295
66296
66297
66298
66299
66300
66301
66302
66303
66304
66305
66306
66307
66308
66309
66310
66311
66312
66313
66314
66315
66316
66317
66318
66319
66320
66321
66322
66323
66324
66325
66326
66327
66328
66329
66330
66331
66332
66333
66334
66335
66336
66337
66338
66339
66340
66341
66342
66343
66344
66345
66346
66347
66348
66349
66350
66351
66352
66353
66354
66355
66356
66357
66358
66359
66360
66361
66362
66363
66364
66365
66366
66367
66368
66369
66370
66371
66372
66373
66374
66375
66376
66377
66378
66379
66380
66381
66382
66383
66384
66385
66386
66387
66388
66389
66390
66391
66392
66393
66394
66395
66396
66397
66398
66399
66400
66401
66402
66403
66404
66405
66406
66407
66408
66409
66410
66411
66412
66413
66414
66415
66416
66417
66418
66419
66420
66421
66422
66423
66424
66425
66426
66427
66428
66429
66430
66431
66432
66433
66434
66435
66436
66437
66438
66439
66440
66441
66442
66443
66444
66445
66446
66447
66448
66449
66450
66451
66452
66453
66454
66455
66456
66457
66458
66459
66460
66461
66462
66463
66464
66465
66466
66467
66468
66469
66470
66471
66472
66473
66474
66475
66476
66477
66478
66479
66480
66481
66482
66483
66484
66485
66486
66487
66488
66489
66490
66491
66492
66493
66494
66495
66496
66497
66498
66499
66500
66501
66502
66503
66504
66505
66506
66507
66508
66509
66510
66511
66512
66513
66514
66515
66516
66517
66518
66519
66520
66521
66522
66523
66524
66525
66526
66527
66528
66529
66530
66531
66532
66533
66534
66535
66536
66537
66538
66539
66540
66541
66542
66543
66544
66545
66546
66547
66548
66549
66550
66551
66552
66553
66554
66555
66556
66557
66558
66559
66560
66561
66562
66563
66564
66565
66566
66567
66568
66569
66570
66571
66572
66573
66574
66575
66576
66577
66578
66579
66580
66581
66582
66583
66584
66585
66586
66587
66588
66589
66590
66591
66592
66593
66594
66595
66596
66597
66598
66599
66600
66601
66602
66603
66604
66605
66606
66607
66608
66609
66610
66611
66612
66613
66614
66615
66616
66617
66618
66619
66620
66621
66622
66623
66624
66625
66626
66627
66628
66629
66630
66631
66632
66633
66634
66635
66636
66637
66638
66639
66640
66641
66642
66643
66644
66645
66646
66647
66648
66649
66650
66651
66652
66653
66654
66655
66656
66657
66658
66659
66660
66661
66662
66663
66664
66665
66666
66667
66668
66669
66670
66671
66672
66673
66674
66675
66676
66677
66678
66679
66680
66681
66682
66683
66684
66685
66686
66687
66688
66689
66690
66691
66692
66693
66694
66695
66696
66697
66698
66699
66700
66701
66702
66703
66704
66705
66706
66707
66708
66709
66710
66711
66712
66713
66714
66715
66716
66717
66718
66719
66720
66721
66722
66723
66724
66725
66726
66727
66728
66729
66730
66731
66732
66733
66734
66735
66736
66737
66738
66739
66740
66741
66742
66743
66744
66745
66746
66747
66748
66749
66750
66751
66752
66753
66754
66755
66756
66757
66758
66759
66760
66761
66762
66763
66764
66765
66766
66767
66768
66769
66770
66771
66772
66773
66774
66775
66776
66777
66778
66779
66780
66781
66782
66783
66784
66785
66786
66787
66788
66789
66790
66791
66792
66793
66794
66795
66796
66797
66798
66799
66800
66801
66802
66803
66804
66805
66806
66807
66808
66809
66810
66811
66812
66813
66814
66815
66816
66817
66818
66819
66820
66821
66822
66823
66824
66825
66826
66827
66828
66829
66830
66831
66832
66833
66834
66835
66836
66837
66838
66839
66840
66841
66842
66843
66844
66845
66846
66847
66848
66849
66850
66851
66852
66853
66854
66855
66856
66857
66858
66859
66860
66861
66862
66863
66864
66865
66866
66867
66868
66869
66870
66871
66872
66873
66874
66875
66876
66877
66878
66879
66880
66881
66882
66883
66884
66885
66886
66887
66888
66889
66890
66891
66892
66893
66894
66895
66896
66897
66898
66899
66900
66901
66902
66903
66904
66905
66906
66907
66908
66909
66910
66911
66912
66913
66914
66915
66916
66917
66918
66919
66920
66921
66922
66923
66924
66925
66926
66927
66928
66929
66930
66931
66932
66933
66934
66935
66936
66937
66938
66939
66940
66941
66942
66943
66944
66945
66946
66947
66948
66949
66950
66951
66952
66953
66954
66955
66956
66957
66958
66959
66960
66961
66962
66963
66964
66965
66966
66967
66968
66969
66970
66971
66972
66973
66974
66975
66976
66977
66978
66979
66980
66981
66982
66983
66984
66985
66986
66987
66988
66989
66990
66991
66992
66993
66994
66995
66996
66997
66998
66999
67000
67001
67002
67003
67004
67005
67006
67007
67008
67009
67010
67011
67012
67013
67014
67015
67016
67017
67018
67019
67020
67021
67022
67023
67024
67025
67026
67027
67028
67029
67030
67031
67032
67033
67034
67035
67036
67037
67038
67039
67040
67041
67042
67043
67044
67045
67046
67047
67048
67049
67050
67051
67052
67053
67054
67055
67056
67057
67058
67059
67060
67061
67062
67063
67064
67065
67066
67067
67068
67069
67070
67071
67072
67073
67074
67075
67076
67077
67078
67079
67080
67081
67082
67083
67084
67085
67086
67087
67088
67089
67090
67091
67092
67093
67094
67095
67096
67097
67098
67099
67100
67101
67102
67103
67104
67105
67106
67107
67108
67109
67110
67111
67112
67113
67114
67115
67116
67117
67118
67119
67120
67121
67122
67123
67124
67125
67126
67127
67128
67129
67130
67131
67132
67133
67134
67135
67136
67137
67138
67139
67140
67141
67142
67143
67144
67145
67146
67147
67148
67149
67150
67151
67152
67153
67154
67155
67156
67157
67158
67159
67160
67161
67162
67163
67164
67165
67166
67167
67168
67169
67170
67171
67172
67173
67174
67175
67176
67177
67178
67179
67180
67181
67182
67183
67184
67185
67186
67187
67188
67189
67190
67191
67192
67193
67194
67195
67196
67197
67198
67199
67200
67201
67202
67203
67204
67205
67206
67207
67208
67209
67210
67211
67212
67213
67214
67215
67216
67217
67218
67219
67220
67221
67222
67223
67224
67225
67226
67227
67228
67229
67230
67231
67232
67233
67234
67235
67236
67237
67238
67239
67240
67241
67242
67243
67244
67245
67246
67247
67248
67249
67250
67251
67252
67253
67254
67255
67256
67257
67258
67259
67260
67261
67262
67263
67264
67265
67266
67267
67268
67269
67270
67271
67272
67273
67274
67275
67276
67277
67278
67279
67280
67281
67282
67283
67284
67285
67286
67287
67288
67289
67290
67291
67292
67293
67294
67295
67296
67297
67298
67299
67300
67301
67302
67303
67304
67305
67306
67307
67308
67309
67310
67311
67312
67313
67314
67315
67316
67317
67318
67319
67320
67321
67322
67323
67324
67325
67326
67327
67328
67329
67330
67331
67332
67333
67334
67335
67336
67337
67338
67339
67340
67341
67342
67343
67344
67345
67346
67347
67348
67349
67350
67351
67352
67353
67354
67355
67356
67357
67358
67359
67360
67361
67362
67363
67364
67365
67366
67367
67368
67369
67370
67371
67372
67373
67374
67375
67376
67377
67378
67379
67380
67381
67382
67383
67384
67385
67386
67387
67388
67389
67390
67391
67392
67393
67394
67395
67396
67397
67398
67399
67400
67401
67402
67403
67404
67405
67406
67407
67408
67409
67410
67411
67412
67413
67414
67415
67416
67417
67418
67419
67420
67421
67422
67423
67424
67425
67426
67427
67428
67429
67430
67431
67432
67433
67434
67435
67436
67437
67438
67439
67440
67441
67442
67443
67444
67445
67446
67447
67448
67449
67450
67451
67452
67453
67454
67455
67456
67457
67458
67459
67460
67461
67462
67463
67464
67465
67466
67467
67468
67469
67470
67471
67472
67473
67474
67475
67476
67477
67478
67479
67480
67481
67482
67483
67484
67485
67486
67487
67488
67489
67490
67491
67492
67493
67494
67495
67496
67497
67498
67499
67500
67501
67502
67503
67504
67505
67506
67507
67508
67509
67510
67511
67512
67513
67514
67515
67516
67517
67518
67519
67520
67521
67522
67523
67524
67525
67526
67527
67528
67529
67530
67531
67532
67533
67534
67535
67536
67537
67538
67539
67540
67541
67542
67543
67544
67545
67546
67547
67548
67549
67550
67551
67552
67553
67554
67555
67556
67557
67558
67559
67560
67561
67562
67563
67564
67565
67566
67567
67568
67569
67570
67571
67572
67573
67574
67575
67576
67577
67578
67579
67580
67581
67582
67583
67584
67585
67586
67587
67588
67589
67590
67591
67592
67593
67594
67595
67596
67597
67598
67599
67600
67601
67602
67603
67604
67605
67606
67607
67608
67609
67610
67611
67612
67613
67614
67615
67616
67617
67618
67619
67620
67621
67622
67623
67624
67625
67626
67627
67628
67629
67630
67631
67632
67633
67634
67635
67636
67637
67638
67639
67640
67641
67642
67643
67644
67645
67646
67647
67648
67649
67650
67651
67652
67653
67654
67655
67656
67657
67658
67659
67660
67661
67662
67663
67664
67665
67666
67667
67668
67669
67670
67671
67672
67673
67674
67675
67676
67677
67678
67679
67680
67681
67682
67683
67684
67685
67686
67687
67688
67689
67690
67691
67692
67693
67694
67695
67696
67697
67698
67699
67700
67701
67702
67703
67704
67705
67706
67707
67708
67709
67710
67711
67712
67713
67714
67715
67716
67717
67718
67719
67720
67721
67722
67723
67724
67725
67726
67727
67728
67729
67730
67731
67732
67733
67734
67735
67736
67737
67738
67739
67740
67741
67742
67743
67744
67745
67746
67747
67748
67749
67750
67751
67752
67753
67754
67755
67756
67757
67758
67759
67760
67761
67762
67763
67764
67765
67766
67767
67768
67769
67770
67771
67772
67773
67774
67775
67776
67777
67778
67779
67780
67781
67782
67783
67784
67785
67786
67787
67788
67789
67790
67791
67792
67793
67794
67795
67796
67797
67798
67799
67800
67801
67802
67803
67804
67805
67806
67807
67808
67809
67810
67811
67812
67813
67814
67815
67816
67817
67818
67819
67820
67821
67822
67823
67824
67825
67826
67827
67828
67829
67830
67831
67832
67833
67834
67835
67836
67837
67838
67839
67840
67841
67842
67843
67844
67845
67846
67847
67848
67849
67850
67851
67852
67853
67854
67855
67856
67857
67858
67859
67860
67861
67862
67863
67864
67865
67866
67867
67868
67869
67870
67871
67872
67873
67874
67875
67876
67877
67878
67879
67880
67881
67882
67883
67884
67885
67886
67887
67888
67889
67890
67891
67892
67893
67894
67895
67896
67897
67898
67899
67900
67901
67902
67903
67904
67905
67906
67907
67908
67909
67910
67911
67912
67913
67914
67915
67916
67917
67918
67919
67920
67921
67922
67923
67924
67925
67926
67927
67928
67929
67930
67931
67932
67933
67934
67935
67936
67937
67938
67939
67940
67941
67942
67943
67944
67945
67946
67947
67948
67949
67950
67951
67952
67953
67954
67955
67956
67957
67958
67959
67960
67961
67962
67963
67964
67965
67966
67967
67968
67969
67970
67971
67972
67973
67974
67975
67976
67977
67978
67979
67980
67981
67982
67983
67984
67985
67986
67987
67988
67989
67990
67991
67992
67993
67994
67995
67996
67997
67998
67999
68000
68001
68002
68003
68004
68005
68006
68007
68008
68009
68010
68011
68012
68013
68014
68015
68016
68017
68018
68019
68020
68021
68022
68023
68024
68025
68026
68027
68028
68029
68030
68031
68032
68033
68034
68035
68036
68037
68038
68039
68040
68041
68042
68043
68044
68045
68046
68047
68048
68049
68050
68051
68052
68053
68054
68055
68056
68057
68058
68059
68060
68061
68062
68063
68064
68065
68066
68067
68068
68069
68070
68071
68072
68073
68074
68075
68076
68077
68078
68079
68080
68081
68082
68083
68084
68085
68086
68087
68088
68089
68090
68091
68092
68093
68094
68095
68096
68097
68098
68099
68100
68101
68102
68103
68104
68105
68106
68107
68108
68109
68110
68111
68112
68113
68114
68115
68116
68117
68118
68119
68120
68121
68122
68123
68124
68125
68126
68127
68128
68129
68130
68131
68132
68133
68134
68135
68136
68137
68138
68139
68140
68141
68142
68143
68144
68145
68146
68147
68148
68149
68150
68151
68152
68153
68154
68155
68156
68157
68158
68159
68160
68161
68162
68163
68164
68165
68166
68167
68168
68169
68170
68171
68172
68173
68174
68175
68176
68177
68178
68179
68180
68181
68182
68183
68184
68185
68186
68187
68188
68189
68190
68191
68192
68193
68194
68195
68196
68197
68198
68199
68200
68201
68202
68203
68204
68205
68206
68207
68208
68209
68210
68211
68212
68213
68214
68215
68216
68217
68218
68219
68220
68221
68222
68223
68224
68225
68226
68227
68228
68229
68230
68231
68232
68233
68234
68235
68236
68237
68238
68239
68240
68241
68242
68243
68244
68245
68246
68247
68248
68249
68250
68251
68252
68253
68254
68255
68256
68257
68258
68259
68260
68261
68262
68263
68264
68265
68266
68267
68268
68269
68270
68271
68272
68273
68274
68275
68276
68277
68278
68279
68280
68281
68282
68283
68284
68285
68286
68287
68288
68289
68290
68291
68292
68293
68294
68295
68296
68297
68298
68299
68300
68301
68302
68303
68304
68305
68306
68307
68308
68309
68310
68311
68312
68313
68314
68315
68316
68317
68318
68319
68320
68321
68322
68323
68324
68325
68326
68327
68328
68329
68330
68331
68332
68333
68334
68335
68336
68337
68338
68339
68340
68341
68342
68343
68344
68345
68346
68347
68348
68349
68350
68351
68352
68353
68354
68355
68356
68357
68358
68359
68360
68361
68362
68363
68364
68365
68366
68367
68368
68369
68370
68371
68372
68373
68374
68375
68376
68377
68378
68379
68380
68381
68382
68383
68384
68385
68386
68387
68388
68389
68390
68391
68392
68393
68394
68395
68396
68397
68398
68399
68400
68401
68402
68403
68404
68405
68406
68407
68408
68409
68410
68411
68412
68413
68414
68415
68416
68417
68418
68419
68420
68421
68422
68423
68424
68425
68426
68427
68428
68429
68430
68431
68432
68433
68434
68435
68436
68437
68438
68439
68440
68441
68442
68443
68444
68445
68446
68447
68448
68449
68450
68451
68452
68453
68454
68455
68456
68457
68458
68459
68460
68461
68462
68463
68464
68465
68466
68467
68468
68469
68470
68471
68472
68473
68474
68475
68476
68477
68478
68479
68480
68481
68482
68483
68484
68485
68486
68487
68488
68489
68490
68491
68492
68493
68494
68495
68496
68497
68498
68499
68500
68501
68502
68503
68504
68505
68506
68507
68508
68509
68510
68511
68512
68513
68514
68515
68516
68517
68518
68519
68520
68521
68522
68523
68524
68525
68526
68527
68528
68529
68530
68531
68532
68533
68534
68535
68536
68537
68538
68539
68540
68541
68542
68543
68544
68545
68546
68547
68548
68549
68550
68551
68552
68553
68554
68555
68556
68557
68558
68559
68560
68561
68562
68563
68564
68565
68566
68567
68568
68569
68570
68571
68572
68573
68574
68575
68576
68577
68578
68579
68580
68581
68582
68583
68584
68585
68586
68587
68588
68589
68590
68591
68592
68593
68594
68595
68596
68597
68598
68599
68600
68601
68602
68603
68604
68605
68606
68607
68608
68609
68610
68611
68612
68613
68614
68615
68616
68617
68618
68619
68620
68621
68622
68623
68624
68625
68626
68627
68628
68629
68630
68631
68632
68633
68634
68635
68636
68637
68638
68639
68640
68641
68642
68643
68644
68645
68646
68647
68648
68649
68650
68651
68652
68653
68654
68655
68656
68657
68658
68659
68660
68661
68662
68663
68664
68665
68666
68667
68668
68669
68670
68671
68672
68673
68674
68675
68676
68677
68678
68679
68680
68681
68682
68683
68684
68685
68686
68687
68688
68689
68690
68691
68692
68693
68694
68695
68696
68697
68698
68699
68700
68701
68702
68703
68704
68705
68706
68707
68708
68709
68710
68711
68712
68713
68714
68715
68716
68717
68718
68719
68720
68721
68722
68723
68724
68725
68726
68727
68728
68729
68730
68731
68732
68733
68734
68735
68736
68737
68738
68739
68740
68741
68742
68743
68744
68745
68746
68747
68748
68749
68750
68751
68752
68753
68754
68755
68756
68757
68758
68759
68760
68761
68762
68763
68764
68765
68766
68767
68768
68769
68770
68771
68772
68773
68774
68775
68776
68777
68778
68779
68780
68781
68782
68783
68784
68785
68786
68787
68788
68789
68790
68791
68792
68793
68794
68795
68796
68797
68798
68799
68800
68801
68802
68803
68804
68805
68806
68807
68808
68809
68810
68811
68812
68813
68814
68815
68816
68817
68818
68819
68820
68821
68822
68823
68824
68825
68826
68827
68828
68829
68830
68831
68832
68833
68834
68835
68836
68837
68838
68839
68840
68841
68842
68843
68844
68845
68846
68847
68848
68849
68850
68851
68852
68853
68854
68855
68856
68857
68858
68859
68860
68861
68862
68863
68864
68865
68866
68867
68868
68869
68870
68871
68872
68873
68874
68875
68876
68877
68878
68879
68880
68881
68882
68883
68884
68885
68886
68887
68888
68889
68890
68891
68892
68893
68894
68895
68896
68897
68898
68899
68900
68901
68902
68903
68904
68905
68906
68907
68908
68909
68910
68911
68912
68913
68914
68915
68916
68917
68918
68919
68920
68921
68922
68923
68924
68925
68926
68927
68928
68929
68930
68931
68932
68933
68934
68935
68936
68937
68938
68939
68940
68941
68942
68943
68944
68945
68946
68947
68948
68949
68950
68951
68952
68953
68954
68955
68956
68957
68958
68959
68960
68961
68962
68963
68964
68965
68966
68967
68968
68969
68970
68971
68972
68973
68974
68975
68976
68977
68978
68979
68980
68981
68982
68983
68984
68985
68986
68987
68988
68989
68990
68991
68992
68993
68994
68995
68996
68997
68998
68999
69000
69001
69002
69003
69004
69005
69006
69007
69008
69009
69010
69011
69012
69013
69014
69015
69016
69017
69018
69019
69020
69021
69022
69023
69024
69025
69026
69027
69028
69029
69030
69031
69032
69033
69034
69035
69036
69037
69038
69039
69040
69041
69042
69043
69044
69045
69046
69047
69048
69049
69050
69051
69052
69053
69054
69055
69056
69057
69058
69059
69060
69061
69062
69063
69064
69065
69066
69067
69068
69069
69070
69071
69072
69073
69074
69075
69076
69077
69078
69079
69080
69081
69082
69083
69084
69085
69086
69087
69088
69089
69090
69091
69092
69093
69094
69095
69096
69097
69098
69099
69100
69101
69102
69103
69104
69105
69106
69107
69108
69109
69110
69111
69112
69113
69114
69115
69116
69117
69118
69119
69120
69121
69122
69123
69124
69125
69126
69127
69128
69129
69130
69131
69132
69133
69134
69135
69136
69137
69138
69139
69140
69141
69142
69143
69144
69145
69146
69147
69148
69149
69150
69151
69152
69153
69154
69155
69156
69157
69158
69159
69160
69161
69162
69163
69164
69165
69166
69167
69168
69169
69170
69171
69172
69173
69174
69175
69176
69177
69178
69179
69180
69181
69182
69183
69184
69185
69186
69187
69188
69189
69190
69191
69192
69193
69194
69195
69196
69197
69198
69199
69200
69201
69202
69203
69204
69205
69206
69207
69208
69209
69210
69211
69212
69213
69214
69215
69216
69217
69218
69219
69220
69221
69222
69223
69224
69225
69226
69227
69228
69229
69230
69231
69232
69233
69234
69235
69236
69237
69238
69239
69240
69241
69242
69243
69244
69245
69246
69247
69248
69249
69250
69251
69252
69253
69254
69255
69256
69257
69258
69259
69260
69261
69262
69263
69264
69265
69266
69267
69268
69269
69270
69271
69272
69273
69274
69275
69276
69277
69278
69279
69280
69281
69282
69283
69284
69285
69286
69287
69288
69289
69290
69291
69292
69293
69294
69295
69296
69297
69298
69299
69300
69301
69302
69303
69304
69305
69306
69307
69308
69309
69310
69311
69312
69313
69314
69315
69316
69317
69318
69319
69320
69321
69322
69323
69324
69325
69326
69327
69328
69329
69330
69331
69332
69333
69334
69335
69336
69337
69338
69339
69340
69341
69342
69343
69344
69345
69346
69347
69348
69349
69350
69351
69352
69353
69354
69355
69356
69357
69358
69359
69360
69361
69362
69363
69364
69365
69366
69367
69368
69369
69370
69371
69372
69373
69374
69375
69376
69377
69378
69379
69380
69381
69382
69383
69384
69385
69386
69387
69388
69389
69390
69391
69392
69393
69394
69395
69396
69397
69398
69399
69400
69401
69402
69403
69404
69405
69406
69407
69408
69409
69410
69411
69412
69413
69414
69415
69416
69417
69418
69419
69420
69421
69422
69423
69424
69425
69426
69427
69428
69429
69430
69431
69432
69433
69434
69435
69436
69437
69438
69439
69440
69441
69442
69443
69444
69445
69446
69447
69448
69449
69450
69451
69452
69453
69454
69455
69456
69457
69458
69459
69460
69461
69462
69463
69464
69465
69466
69467
69468
69469
69470
69471
69472
69473
69474
69475
69476
69477
69478
69479
69480
69481
69482
69483
69484
69485
69486
69487
69488
69489
69490
69491
69492
69493
69494
69495
69496
69497
69498
69499
69500
69501
69502
69503
69504
69505
69506
69507
69508
69509
69510
69511
69512
69513
69514
69515
69516
69517
69518
69519
69520
69521
69522
69523
69524
69525
69526
69527
69528
69529
69530
69531
69532
69533
69534
69535
69536
69537
69538
69539
69540
69541
69542
69543
69544
69545
69546
69547
69548
69549
69550
69551
69552
69553
69554
69555
69556
69557
69558
69559
69560
69561
69562
69563
69564
69565
69566
69567
69568
69569
69570
69571
69572
69573
69574
69575
69576
69577
69578
69579
69580
69581
69582
69583
69584
69585
69586
69587
69588
69589
69590
69591
69592
69593
69594
69595
69596
69597
69598
69599
69600
69601
69602
69603
69604
69605
69606
69607
69608
69609
69610
69611
69612
69613
69614
69615
69616
69617
69618
69619
69620
69621
69622
69623
69624
69625
69626
69627
69628
69629
69630
69631
69632
69633
69634
69635
69636
69637
69638
69639
69640
69641
69642
69643
69644
69645
69646
69647
69648
69649
69650
69651
69652
69653
69654
69655
69656
69657
69658
69659
69660
69661
69662
69663
69664
69665
69666
69667
69668
69669
69670
69671
69672
69673
69674
69675
69676
69677
69678
69679
69680
69681
69682
69683
69684
69685
69686
69687
69688
69689
69690
69691
69692
69693
69694
69695
69696
69697
69698
69699
69700
69701
69702
69703
69704
69705
69706
69707
69708
69709
69710
69711
69712
69713
69714
69715
69716
69717
69718
69719
69720
69721
69722
69723
69724
69725
69726
69727
69728
69729
69730
69731
69732
69733
69734
69735
69736
69737
69738
69739
69740
69741
69742
69743
69744
69745
69746
69747
69748
69749
69750
69751
69752
69753
69754
69755
69756
69757
69758
69759
69760
69761
69762
69763
69764
69765
69766
69767
69768
69769
69770
69771
69772
69773
69774
69775
69776
69777
69778
69779
69780
69781
69782
69783
69784
69785
69786
69787
69788
69789
69790
69791
69792
69793
69794
69795
69796
69797
69798
69799
69800
69801
69802
69803
69804
69805
69806
69807
69808
69809
69810
69811
69812
69813
69814
69815
69816
69817
69818
69819
69820
69821
69822
69823
69824
69825
69826
69827
69828
69829
69830
69831
69832
69833
69834
69835
69836
69837
69838
69839
69840
69841
69842
69843
69844
69845
69846
69847
69848
69849
69850
69851
69852
69853
69854
69855
69856
69857
69858
69859
69860
69861
69862
69863
69864
69865
69866
69867
69868
69869
69870
69871
69872
69873
69874
69875
69876
69877
69878
69879
69880
69881
69882
69883
69884
69885
69886
69887
69888
69889
69890
69891
69892
69893
69894
69895
69896
69897
69898
69899
69900
69901
69902
69903
69904
69905
69906
69907
69908
69909
69910
69911
69912
69913
69914
69915
69916
69917
69918
69919
69920
69921
69922
69923
69924
69925
69926
69927
69928
69929
69930
69931
69932
69933
69934
69935
69936
69937
69938
69939
69940
69941
69942
69943
69944
69945
69946
69947
69948
69949
69950
69951
69952
69953
69954
69955
69956
69957
69958
69959
69960
69961
69962
69963
69964
69965
69966
69967
69968
69969
69970
69971
69972
69973
69974
69975
69976
69977
69978
69979
69980
69981
69982
69983
69984
69985
69986
69987
69988
69989
69990
69991
69992
69993
69994
69995
69996
69997
69998
69999
70000
70001
70002
70003
70004
70005
70006
70007
70008
70009
70010
70011
70012
70013
70014
70015
70016
70017
70018
70019
70020
70021
70022
70023
70024
70025
70026
70027
70028
70029
70030
70031
70032
70033
70034
70035
70036
70037
70038
70039
70040
70041
70042
70043
70044
70045
70046
70047
70048
70049
70050
70051
70052
70053
70054
70055
70056
70057
70058
70059
70060
70061
70062
70063
70064
70065
70066
70067
70068
70069
70070
70071
70072
70073
70074
70075
70076
70077
70078
70079
70080
70081
70082
70083
70084
70085
70086
70087
70088
70089
70090
70091
70092
70093
70094
70095
70096
70097
70098
70099
70100
70101
70102
70103
70104
70105
70106
70107
70108
70109
70110
70111
70112
70113
70114
70115
70116
70117
70118
70119
70120
70121
70122
70123
70124
70125
70126
70127
70128
70129
70130
70131
70132
70133
70134
70135
70136
70137
70138
70139
70140
70141
70142
70143
70144
70145
70146
70147
70148
70149
70150
70151
70152
70153
70154
70155
70156
70157
70158
70159
70160
70161
70162
70163
70164
70165
70166
70167
70168
70169
70170
70171
70172
70173
70174
70175
70176
70177
70178
70179
70180
70181
70182
70183
70184
70185
70186
70187
70188
70189
70190
70191
70192
70193
70194
70195
70196
70197
70198
70199
70200
70201
70202
70203
70204
70205
70206
70207
70208
70209
70210
70211
70212
70213
70214
70215
70216
70217
70218
70219
70220
70221
70222
70223
70224
70225
70226
70227
70228
70229
70230
70231
70232
70233
70234
70235
70236
70237
70238
70239
70240
70241
70242
70243
70244
70245
70246
70247
70248
70249
70250
70251
70252
70253
70254
70255
70256
70257
70258
70259
70260
70261
70262
70263
70264
70265
70266
70267
70268
70269
70270
70271
70272
70273
70274
70275
70276
70277
70278
70279
70280
70281
70282
70283
70284
70285
70286
70287
70288
70289
70290
70291
70292
70293
70294
70295
70296
70297
70298
70299
70300
70301
70302
70303
70304
70305
70306
70307
70308
70309
70310
70311
70312
70313
70314
70315
70316
70317
70318
70319
70320
70321
70322
70323
70324
70325
70326
70327
70328
70329
70330
70331
70332
70333
70334
70335
70336
70337
70338
70339
70340
70341
70342
70343
70344
70345
70346
70347
70348
70349
70350
70351
70352
70353
70354
70355
70356
70357
70358
70359
70360
70361
70362
70363
70364
70365
70366
70367
70368
70369
70370
70371
70372
70373
70374
70375
70376
70377
70378
70379
70380
70381
70382
70383
70384
70385
70386
70387
70388
70389
70390
70391
70392
70393
70394
70395
70396
70397
70398
70399
70400
70401
70402
70403
70404
70405
70406
70407
70408
70409
70410
70411
70412
70413
70414
70415
70416
70417
70418
70419
70420
70421
70422
70423
70424
70425
70426
70427
70428
70429
70430
70431
70432
70433
70434
70435
70436
70437
70438
70439
70440
70441
70442
70443
70444
70445
70446
70447
70448
70449
70450
70451
70452
70453
70454
70455
70456
70457
70458
70459
70460
70461
70462
70463
70464
70465
70466
70467
70468
70469
70470
70471
70472
70473
70474
70475
70476
70477
70478
70479
70480
70481
70482
70483
70484
70485
70486
70487
70488
70489
70490
70491
70492
70493
70494
70495
70496
70497
70498
70499
70500
70501
70502
70503
70504
70505
70506
70507
70508
70509
70510
70511
70512
70513
70514
70515
70516
70517
70518
70519
70520
70521
70522
70523
70524
70525
70526
70527
70528
70529
70530
70531
70532
70533
70534
70535
70536
70537
70538
70539
70540
70541
70542
70543
70544
70545
70546
70547
70548
70549
70550
70551
70552
70553
70554
70555
70556
70557
70558
70559
70560
70561
70562
70563
70564
70565
70566
70567
70568
70569
70570
70571
70572
70573
70574
70575
70576
70577
70578
70579
70580
70581
70582
70583
70584
70585
70586
70587
70588
70589
70590
70591
70592
70593
70594
70595
70596
70597
70598
70599
70600
70601
70602
70603
70604
70605
70606
70607
70608
70609
70610
70611
70612
70613
70614
70615
70616
70617
70618
70619
70620
70621
70622
70623
70624
70625
70626
70627
70628
70629
70630
70631
70632
70633
70634
70635
70636
70637
70638
70639
70640
70641
70642
70643
70644
70645
70646
70647
70648
70649
70650
70651
70652
70653
70654
70655
70656
70657
70658
70659
70660
70661
70662
70663
70664
70665
70666
70667
70668
70669
70670
70671
70672
70673
70674
70675
70676
70677
70678
70679
70680
70681
70682
70683
70684
70685
70686
70687
70688
70689
70690
70691
70692
70693
70694
70695
70696
70697
70698
70699
70700
70701
70702
70703
70704
70705
70706
70707
70708
70709
70710
70711
70712
70713
70714
70715
70716
70717
70718
70719
70720
70721
70722
70723
70724
70725
70726
70727
70728
70729
70730
70731
70732
70733
70734
70735
70736
70737
70738
70739
70740
70741
70742
70743
70744
70745
70746
70747
70748
70749
70750
70751
70752
70753
70754
70755
70756
70757
70758
70759
70760
70761
70762
70763
70764
70765
70766
70767
70768
70769
70770
70771
70772
70773
70774
70775
70776
70777
70778
70779
70780
70781
70782
70783
70784
70785
70786
70787
70788
70789
70790
70791
70792
70793
70794
70795
70796
70797
70798
70799
70800
70801
70802
70803
70804
70805
70806
70807
70808
70809
70810
70811
70812
70813
70814
70815
70816
70817
70818
70819
70820
70821
70822
70823
70824
70825
70826
70827
70828
70829
70830
70831
70832
70833
70834
70835
70836
70837
70838
70839
70840
70841
70842
70843
70844
70845
70846
70847
70848
70849
70850
70851
70852
70853
70854
70855
70856
70857
70858
70859
70860
70861
70862
70863
70864
70865
70866
70867
70868
70869
70870
70871
70872
70873
70874
70875
70876
70877
70878
70879
70880
70881
70882
70883
70884
70885
70886
70887
70888
70889
70890
70891
70892
70893
70894
70895
70896
70897
70898
70899
70900
70901
70902
70903
70904
70905
70906
70907
70908
70909
70910
70911
70912
70913
70914
70915
70916
70917
70918
70919
70920
70921
70922
70923
70924
70925
70926
70927
70928
70929
70930
70931
70932
70933
70934
70935
70936
70937
70938
70939
70940
70941
70942
70943
70944
70945
70946
70947
70948
70949
70950
70951
70952
70953
70954
70955
70956
70957
70958
70959
70960
70961
70962
70963
70964
70965
70966
70967
70968
70969
70970
70971
70972
70973
70974
70975
70976
70977
70978
70979
70980
70981
70982
70983
70984
70985
70986
70987
70988
70989
70990
70991
70992
70993
70994
70995
70996
70997
70998
70999
71000
71001
71002
71003
71004
71005
71006
71007
71008
71009
71010
71011
71012
71013
71014
71015
71016
71017
71018
71019
71020
71021
71022
71023
71024
71025
71026
71027
71028
71029
71030
71031
71032
71033
71034
71035
71036
71037
71038
71039
71040
71041
71042
71043
71044
71045
71046
71047
71048
71049
71050
71051
71052
71053
71054
71055
71056
71057
71058
71059
71060
71061
71062
71063
71064
71065
71066
71067
71068
71069
71070
71071
71072
71073
71074
71075
71076
71077
71078
71079
71080
71081
71082
71083
71084
71085
71086
71087
71088
71089
71090
71091
71092
71093
71094
71095
71096
71097
71098
71099
71100
71101
71102
71103
71104
71105
71106
71107
71108
71109
71110
71111
71112
71113
71114
71115
71116
71117
71118
71119
71120
71121
71122
71123
71124
71125
71126
71127
71128
71129
71130
71131
71132
71133
71134
71135
71136
71137
71138
71139
71140
71141
71142
71143
71144
71145
71146
71147
71148
71149
71150
71151
71152
71153
71154
71155
71156
71157
71158
71159
71160
71161
71162
71163
71164
71165
71166
71167
71168
71169
71170
71171
71172
71173
71174
71175
71176
71177
71178
71179
71180
71181
71182
71183
71184
71185
71186
71187
71188
71189
71190
71191
71192
71193
71194
71195
71196
71197
71198
71199
71200
71201
71202
71203
71204
71205
71206
71207
71208
71209
71210
71211
71212
71213
71214
71215
71216
71217
71218
71219
71220
71221
71222
71223
71224
71225
71226
71227
71228
71229
71230
71231
71232
71233
71234
71235
71236
71237
71238
71239
71240
71241
71242
71243
71244
71245
71246
71247
71248
71249
71250
71251
71252
71253
71254
71255
71256
71257
71258
71259
71260
71261
71262
71263
71264
71265
71266
71267
71268
71269
71270
71271
71272
71273
71274
71275
71276
71277
71278
71279
71280
71281
71282
71283
71284
71285
71286
71287
71288
71289
71290
71291
71292
71293
71294
71295
71296
71297
71298
71299
71300
71301
71302
71303
71304
71305
71306
71307
71308
71309
71310
71311
71312
71313
71314
71315
71316
71317
71318
71319
71320
71321
71322
71323
71324
71325
71326
71327
71328
71329
71330
71331
71332
71333
71334
71335
71336
71337
71338
71339
71340
71341
71342
71343
71344
71345
71346
71347
71348
71349
71350
71351
71352
71353
71354
71355
71356
71357
71358
71359
71360
71361
71362
71363
71364
71365
71366
71367
71368
71369
71370
71371
71372
71373
71374
71375
71376
71377
71378
71379
71380
71381
71382
71383
71384
71385
71386
71387
71388
71389
71390
71391
71392
71393
71394
71395
71396
71397
71398
71399
71400
71401
71402
71403
71404
71405
71406
71407
71408
71409
71410
71411
71412
71413
71414
71415
71416
71417
71418
71419
71420
71421
71422
71423
71424
71425
71426
71427
71428
71429
71430
71431
71432
71433
71434
71435
71436
71437
71438
71439
71440
71441
71442
71443
71444
71445
71446
71447
71448
71449
71450
71451
71452
71453
71454
71455
71456
71457
71458
71459
71460
71461
71462
71463
71464
71465
71466
71467
71468
71469
71470
71471
71472
71473
71474
71475
71476
71477
71478
71479
71480
71481
71482
71483
71484
71485
71486
71487
71488
71489
71490
71491
71492
71493
71494
71495
71496
71497
71498
71499
71500
71501
71502
71503
71504
71505
71506
71507
71508
71509
71510
71511
71512
71513
71514
71515
71516
71517
71518
71519
71520
71521
71522
71523
71524
71525
71526
71527
71528
71529
71530
71531
71532
71533
71534
71535
71536
71537
71538
71539
71540
71541
71542
71543
71544
71545
71546
71547
71548
71549
71550
71551
71552
71553
71554
71555
71556
71557
71558
71559
71560
71561
71562
71563
71564
71565
71566
71567
71568
71569
71570
71571
71572
71573
71574
71575
71576
71577
71578
71579
71580
71581
71582
71583
71584
71585
71586
71587
71588
71589
71590
71591
71592
71593
71594
71595
71596
71597
71598
71599
71600
71601
71602
71603
71604
71605
71606
71607
71608
71609
71610
71611
71612
71613
71614
71615
71616
71617
71618
71619
71620
71621
71622
71623
71624
71625
71626
71627
71628
71629
71630
71631
71632
71633
71634
71635
71636
71637
71638
71639
71640
71641
71642
71643
71644
71645
71646
71647
71648
71649
71650
71651
71652
71653
71654
71655
71656
71657
71658
71659
71660
71661
71662
71663
71664
71665
71666
71667
71668
71669
71670
71671
71672
71673
71674
71675
71676
71677
71678
71679
71680
71681
71682
71683
71684
71685
71686
71687
71688
71689
71690
71691
71692
71693
71694
71695
71696
71697
71698
71699
71700
71701
71702
71703
71704
71705
71706
71707
71708
71709
71710
71711
71712
71713
71714
71715
71716
71717
71718
71719
71720
71721
71722
71723
71724
71725
71726
71727
71728
71729
71730
71731
71732
71733
71734
71735
71736
71737
71738
71739
71740
71741
71742
71743
71744
71745
71746
71747
71748
71749
71750
71751
71752
71753
71754
71755
71756
71757
71758
71759
71760
71761
71762
71763
71764
71765
71766
71767
71768
71769
71770
71771
71772
71773
71774
71775
71776
71777
71778
71779
71780
71781
71782
71783
71784
71785
71786
71787
71788
71789
71790
71791
71792
71793
71794
71795
71796
71797
71798
71799
71800
71801
71802
71803
71804
71805
71806
71807
71808
71809
71810
71811
71812
71813
71814
71815
71816
71817
71818
71819
71820
71821
71822
71823
71824
71825
71826
71827
71828
71829
71830
71831
71832
71833
71834
71835
71836
71837
71838
71839
71840
71841
71842
71843
71844
71845
71846
71847
71848
71849
71850
71851
71852
71853
71854
71855
71856
71857
71858
71859
71860
71861
71862
71863
71864
71865
71866
71867
71868
71869
71870
71871
71872
71873
71874
71875
71876
71877
71878
71879
71880
71881
71882
71883
71884
71885
71886
71887
71888
71889
71890
71891
71892
71893
71894
71895
71896
71897
71898
71899
71900
71901
71902
71903
71904
71905
71906
71907
71908
71909
71910
71911
71912
71913
71914
71915
71916
71917
71918
71919
71920
71921
71922
71923
71924
71925
71926
71927
71928
71929
71930
71931
71932
71933
71934
71935
71936
71937
71938
71939
71940
71941
71942
71943
71944
71945
71946
71947
71948
71949
71950
71951
71952
71953
71954
71955
71956
71957
71958
71959
71960
71961
71962
71963
71964
71965
71966
71967
71968
71969
71970
71971
71972
71973
71974
71975
71976
71977
71978
71979
71980
71981
71982
71983
71984
71985
71986
71987
71988
71989
71990
71991
71992
71993
71994
71995
71996
71997
71998
71999
72000
72001
72002
72003
72004
72005
72006
72007
72008
72009
72010
72011
72012
72013
72014
72015
72016
72017
72018
72019
72020
72021
72022
72023
72024
72025
72026
72027
72028
72029
72030
72031
72032
72033
72034
72035
72036
72037
72038
72039
72040
72041
72042
72043
72044
72045
72046
72047
72048
72049
72050
72051
72052
72053
72054
72055
72056
72057
72058
72059
72060
72061
72062
72063
72064
72065
72066
72067
72068
72069
72070
72071
72072
72073
72074
72075
72076
72077
72078
72079
72080
72081
72082
72083
72084
72085
72086
72087
72088
72089
72090
72091
72092
72093
72094
72095
72096
72097
72098
72099
72100
72101
72102
72103
72104
72105
72106
72107
72108
72109
72110
72111
72112
72113
72114
72115
72116
72117
72118
72119
72120
72121
72122
72123
72124
72125
72126
72127
72128
72129
72130
72131
72132
72133
72134
72135
72136
72137
72138
72139
72140
72141
72142
72143
72144
72145
72146
72147
72148
72149
72150
72151
72152
72153
72154
72155
72156
72157
72158
72159
72160
72161
72162
72163
72164
72165
72166
72167
72168
72169
72170
72171
72172
72173
72174
72175
72176
72177
72178
72179
72180
72181
72182
72183
72184
72185
72186
72187
72188
72189
72190
72191
72192
72193
72194
72195
72196
72197
72198
72199
72200
72201
72202
72203
72204
72205
72206
72207
72208
72209
72210
72211
72212
72213
72214
72215
72216
72217
72218
72219
72220
72221
72222
72223
72224
72225
72226
72227
72228
72229
72230
72231
72232
72233
72234
72235
72236
72237
72238
72239
72240
72241
72242
72243
72244
72245
72246
72247
72248
72249
72250
72251
72252
72253
72254
72255
72256
72257
72258
72259
72260
72261
72262
72263
72264
72265
72266
72267
72268
72269
72270
72271
72272
72273
72274
72275
72276
72277
72278
72279
72280
72281
72282
72283
72284
72285
72286
72287
72288
72289
72290
72291
72292
72293
72294
72295
72296
72297
72298
72299
72300
72301
72302
72303
72304
72305
72306
72307
72308
72309
72310
72311
72312
72313
72314
72315
72316
72317
72318
72319
72320
72321
72322
72323
72324
72325
72326
72327
72328
72329
72330
72331
72332
72333
72334
72335
72336
72337
72338
72339
72340
72341
72342
72343
72344
72345
72346
72347
72348
72349
72350
72351
72352
72353
72354
72355
72356
72357
72358
72359
72360
72361
72362
72363
72364
72365
72366
72367
72368
72369
72370
72371
72372
72373
72374
72375
72376
72377
72378
72379
72380
72381
72382
72383
72384
72385
72386
72387
72388
72389
72390
72391
72392
72393
72394
72395
72396
72397
72398
72399
72400
72401
72402
72403
72404
72405
72406
72407
72408
72409
72410
72411
72412
72413
72414
72415
72416
72417
72418
72419
72420
72421
72422
72423
72424
72425
72426
72427
72428
72429
72430
72431
72432
72433
72434
72435
72436
72437
72438
72439
72440
72441
72442
72443
72444
72445
72446
72447
72448
72449
72450
72451
72452
72453
72454
72455
72456
72457
72458
72459
72460
72461
72462
72463
72464
72465
72466
72467
72468
72469
72470
72471
72472
72473
72474
72475
72476
72477
72478
72479
72480
72481
72482
72483
72484
72485
72486
72487
72488
72489
72490
72491
72492
72493
72494
72495
72496
72497
72498
72499
72500
72501
72502
72503
72504
72505
72506
72507
72508
72509
72510
72511
72512
72513
72514
72515
72516
72517
72518
72519
72520
72521
72522
72523
72524
72525
72526
72527
72528
72529
72530
72531
72532
72533
72534
72535
72536
72537
72538
72539
72540
72541
72542
72543
72544
72545
72546
72547
72548
72549
72550
72551
72552
72553
72554
72555
72556
72557
72558
72559
72560
72561
72562
72563
72564
72565
72566
72567
72568
72569
72570
72571
72572
72573
72574
72575
72576
72577
72578
72579
72580
72581
72582
72583
72584
72585
72586
72587
72588
72589
72590
72591
72592
72593
72594
72595
72596
72597
72598
72599
72600
72601
72602
72603
72604
72605
72606
72607
72608
72609
72610
72611
72612
72613
72614
72615
72616
72617
72618
72619
72620
72621
72622
72623
72624
72625
72626
72627
72628
72629
72630
72631
72632
72633
72634
72635
72636
72637
72638
72639
72640
72641
72642
72643
72644
72645
72646
72647
72648
72649
72650
72651
72652
72653
72654
72655
72656
72657
72658
72659
72660
72661
72662
72663
72664
72665
72666
72667
72668
72669
72670
72671
72672
72673
72674
72675
72676
72677
72678
72679
72680
72681
72682
72683
72684
72685
72686
72687
72688
72689
72690
72691
72692
72693
72694
72695
72696
72697
72698
72699
72700
72701
72702
72703
72704
72705
72706
72707
72708
72709
72710
72711
72712
72713
72714
72715
72716
72717
72718
72719
72720
72721
72722
72723
72724
72725
72726
72727
72728
72729
72730
72731
72732
72733
72734
72735
72736
72737
72738
72739
72740
72741
72742
72743
72744
72745
72746
72747
72748
72749
72750
72751
72752
72753
72754
72755
72756
72757
72758
72759
72760
72761
72762
72763
72764
72765
72766
72767
72768
72769
72770
72771
72772
72773
72774
72775
72776
72777
72778
72779
72780
72781
72782
72783
72784
72785
72786
72787
72788
72789
72790
72791
72792
72793
72794
72795
72796
72797
72798
72799
72800
72801
72802
72803
72804
72805
72806
72807
72808
72809
72810
72811
72812
72813
72814
72815
72816
72817
72818
72819
72820
72821
72822
72823
72824
72825
72826
72827
72828
72829
72830
72831
72832
72833
72834
72835
72836
72837
72838
72839
72840
72841
72842
72843
72844
72845
72846
72847
72848
72849
72850
72851
72852
72853
72854
72855
72856
72857
72858
72859
72860
72861
72862
72863
72864
72865
72866
72867
72868
72869
72870
72871
72872
72873
72874
72875
72876
72877
72878
72879
72880
72881
72882
72883
72884
72885
72886
72887
72888
72889
72890
72891
72892
72893
72894
72895
72896
72897
72898
72899
72900
72901
72902
72903
72904
72905
72906
72907
72908
72909
72910
72911
72912
72913
72914
72915
72916
72917
72918
72919
72920
72921
72922
72923
72924
72925
72926
72927
72928
72929
72930
72931
72932
72933
72934
72935
72936
72937
72938
72939
72940
72941
72942
72943
72944
72945
72946
72947
72948
72949
72950
72951
72952
72953
72954
72955
72956
72957
72958
72959
72960
72961
72962
72963
72964
72965
72966
72967
72968
72969
72970
72971
72972
72973
72974
72975
72976
72977
72978
72979
72980
72981
72982
72983
72984
72985
72986
72987
72988
72989
72990
72991
72992
72993
72994
72995
72996
72997
72998
72999
73000
73001
73002
73003
73004
73005
73006
73007
73008
73009
73010
73011
73012
73013
73014
73015
73016
73017
73018
73019
73020
73021
73022
73023
73024
73025
73026
73027
73028
73029
73030
73031
73032
73033
73034
73035
73036
73037
73038
73039
73040
73041
73042
73043
73044
73045
73046
73047
73048
73049
73050
73051
73052
73053
73054
73055
73056
73057
73058
73059
73060
73061
73062
73063
73064
73065
73066
73067
73068
73069
73070
73071
73072
73073
73074
73075
73076
73077
73078
73079
73080
73081
73082
73083
73084
73085
73086
73087
73088
73089
73090
73091
73092
73093
73094
73095
73096
73097
73098
73099
73100
73101
73102
73103
73104
73105
73106
73107
73108
73109
73110
73111
73112
73113
73114
73115
73116
73117
73118
73119
73120
73121
73122
73123
73124
73125
73126
73127
73128
73129
73130
73131
73132
73133
73134
73135
73136
73137
73138
73139
73140
73141
73142
73143
73144
73145
73146
73147
73148
73149
73150
73151
73152
73153
73154
73155
73156
73157
73158
73159
73160
73161
73162
73163
73164
73165
73166
73167
73168
73169
73170
73171
73172
73173
73174
73175
73176
73177
73178
73179
73180
73181
73182
73183
73184
73185
73186
73187
73188
73189
73190
73191
73192
73193
73194
73195
73196
73197
73198
73199
73200
73201
73202
73203
73204
73205
73206
73207
73208
73209
73210
73211
73212
73213
73214
73215
73216
73217
73218
73219
73220
73221
73222
73223
73224
73225
73226
73227
73228
73229
73230
73231
73232
73233
73234
73235
73236
73237
73238
73239
73240
73241
73242
73243
73244
73245
73246
73247
73248
73249
73250
73251
73252
73253
73254
73255
73256
73257
73258
73259
73260
73261
73262
73263
73264
73265
73266
73267
73268
73269
73270
73271
73272
73273
73274
73275
73276
73277
73278
73279
73280
73281
73282
73283
73284
73285
73286
73287
73288
73289
73290
73291
73292
73293
73294
73295
73296
73297
73298
73299
73300
73301
73302
73303
73304
73305
73306
73307
73308
73309
73310
73311
73312
73313
73314
73315
73316
73317
73318
73319
73320
73321
73322
73323
73324
73325
73326
73327
73328
73329
73330
73331
73332
73333
73334
73335
73336
73337
73338
73339
73340
73341
73342
73343
73344
73345
73346
73347
73348
73349
73350
73351
73352
73353
73354
73355
73356
73357
73358
73359
73360
73361
73362
73363
73364
73365
73366
73367
73368
73369
73370
73371
73372
73373
73374
73375
73376
73377
73378
73379
73380
73381
73382
73383
73384
73385
73386
73387
73388
73389
73390
73391
73392
73393
73394
73395
73396
73397
73398
73399
73400
73401
73402
73403
73404
73405
73406
73407
73408
73409
73410
73411
73412
73413
73414
73415
73416
73417
73418
73419
73420
73421
73422
73423
73424
73425
73426
73427
73428
73429
73430
73431
73432
73433
73434
73435
73436
73437
73438
73439
73440
73441
73442
73443
73444
73445
73446
73447
73448
73449
73450
73451
73452
73453
73454
73455
73456
73457
73458
73459
73460
73461
73462
73463
73464
73465
73466
73467
73468
73469
73470
73471
73472
73473
73474
73475
73476
73477
73478
73479
73480
73481
73482
73483
73484
73485
73486
73487
73488
73489
73490
73491
73492
73493
73494
73495
73496
73497
73498
73499
73500
73501
73502
73503
73504
73505
73506
73507
73508
73509
73510
73511
73512
73513
73514
73515
73516
73517
73518
73519
73520
73521
73522
73523
73524
73525
73526
73527
73528
73529
73530
73531
73532
73533
73534
73535
73536
73537
73538
73539
73540
73541
73542
73543
73544
73545
73546
73547
73548
73549
73550
73551
73552
73553
73554
73555
73556
73557
73558
73559
73560
73561
73562
73563
73564
73565
73566
73567
73568
73569
73570
73571
73572
73573
73574
73575
73576
73577
73578
73579
73580
73581
73582
73583
73584
73585
73586
73587
73588
73589
73590
73591
73592
73593
73594
73595
73596
73597
73598
73599
73600
73601
73602
73603
73604
73605
73606
73607
73608
73609
73610
73611
73612
73613
73614
73615
73616
73617
73618
73619
73620
73621
73622
73623
73624
73625
73626
73627
73628
73629
73630
73631
73632
73633
73634
73635
73636
73637
73638
73639
73640
73641
73642
73643
73644
73645
73646
73647
73648
73649
73650
73651
73652
73653
73654
73655
73656
73657
73658
73659
73660
73661
73662
73663
73664
73665
73666
73667
73668
73669
73670
73671
73672
73673
73674
73675
73676
73677
73678
73679
73680
73681
73682
73683
73684
73685
73686
73687
73688
73689
73690
73691
73692
73693
73694
73695
73696
73697
73698
73699
73700
73701
73702
73703
73704
73705
73706
73707
73708
73709
73710
73711
73712
73713
73714
73715
73716
73717
73718
73719
73720
73721
73722
73723
73724
73725
73726
73727
73728
73729
73730
73731
73732
73733
73734
73735
73736
73737
73738
73739
73740
73741
73742
73743
73744
73745
73746
73747
73748
73749
73750
73751
73752
73753
73754
73755
73756
73757
73758
73759
73760
73761
73762
73763
73764
73765
73766
73767
73768
73769
73770
73771
73772
73773
73774
73775
73776
73777
73778
73779
73780
73781
73782
73783
73784
73785
73786
73787
73788
73789
73790
73791
73792
73793
73794
73795
73796
73797
73798
73799
73800
73801
73802
73803
73804
73805
73806
73807
73808
73809
73810
73811
73812
73813
73814
73815
73816
73817
73818
73819
73820
73821
73822
73823
73824
73825
73826
73827
73828
73829
73830
73831
73832
73833
73834
73835
73836
73837
73838
73839
73840
73841
73842
73843
73844
73845
73846
73847
73848
73849
73850
73851
73852
73853
73854
73855
73856
73857
73858
73859
73860
73861
73862
73863
73864
73865
73866
73867
73868
73869
73870
73871
73872
73873
73874
73875
73876
73877
73878
73879
73880
73881
73882
73883
73884
73885
73886
73887
73888
73889
73890
73891
73892
73893
73894
73895
73896
73897
73898
73899
73900
73901
73902
73903
73904
73905
73906
73907
73908
73909
73910
73911
73912
73913
73914
73915
73916
73917
73918
73919
73920
73921
73922
73923
73924
73925
73926
73927
73928
73929
73930
73931
73932
73933
73934
73935
73936
73937
73938
73939
73940
73941
73942
73943
73944
73945
73946
73947
73948
73949
73950
73951
73952
73953
73954
73955
73956
73957
73958
73959
73960
73961
73962
73963
73964
73965
73966
73967
73968
73969
73970
73971
73972
73973
73974
73975
73976
73977
73978
73979
73980
73981
73982
73983
73984
73985
73986
73987
73988
73989
73990
73991
73992
73993
73994
73995
73996
73997
73998
73999
74000
74001
74002
74003
74004
74005
74006
74007
74008
74009
74010
74011
74012
74013
74014
74015
74016
74017
74018
74019
74020
74021
74022
74023
74024
74025
74026
74027
74028
74029
74030
74031
74032
74033
74034
74035
74036
74037
74038
74039
74040
74041
74042
74043
74044
74045
74046
74047
74048
74049
74050
74051
74052
74053
74054
74055
74056
74057
74058
74059
74060
74061
74062
74063
74064
74065
74066
74067
74068
74069
74070
74071
74072
74073
74074
74075
74076
74077
74078
74079
74080
74081
74082
74083
74084
74085
74086
74087
74088
74089
74090
74091
74092
74093
74094
74095
74096
74097
74098
74099
74100
74101
74102
74103
74104
74105
74106
74107
74108
74109
74110
74111
74112
74113
74114
74115
74116
74117
74118
74119
74120
74121
74122
74123
74124
74125
74126
74127
74128
74129
74130
74131
74132
74133
74134
74135
74136
74137
74138
74139
74140
74141
74142
74143
74144
74145
74146
74147
74148
74149
74150
74151
74152
74153
74154
74155
74156
74157
74158
74159
74160
74161
74162
74163
74164
74165
74166
74167
74168
74169
74170
74171
74172
74173
74174
74175
74176
74177
74178
74179
74180
74181
74182
74183
74184
74185
74186
74187
74188
74189
74190
74191
74192
74193
74194
74195
74196
74197
74198
74199
74200
74201
74202
74203
74204
74205
74206
74207
74208
74209
74210
74211
74212
74213
74214
74215
74216
74217
74218
74219
74220
74221
74222
74223
74224
74225
74226
74227
74228
74229
74230
74231
74232
74233
74234
74235
74236
74237
74238
74239
74240
74241
74242
74243
74244
74245
74246
74247
74248
74249
74250
74251
74252
74253
74254
74255
74256
74257
74258
74259
74260
74261
74262
74263
74264
74265
74266
74267
74268
74269
74270
74271
74272
74273
74274
74275
74276
74277
74278
74279
74280
74281
74282
74283
74284
74285
74286
74287
74288
74289
74290
74291
74292
74293
74294
74295
74296
74297
74298
74299
74300
74301
74302
74303
74304
74305
74306
74307
74308
74309
74310
74311
74312
74313
74314
74315
74316
74317
74318
74319
74320
74321
74322
74323
74324
74325
74326
74327
74328
74329
74330
74331
74332
74333
74334
74335
74336
74337
74338
74339
74340
74341
74342
74343
74344
74345
74346
74347
74348
74349
74350
74351
74352
74353
74354
74355
74356
74357
74358
74359
74360
74361
74362
74363
74364
74365
74366
74367
74368
74369
74370
74371
74372
74373
74374
74375
74376
74377
74378
74379
74380
74381
74382
74383
74384
74385
74386
74387
74388
74389
74390
74391
74392
74393
74394
74395
74396
74397
74398
74399
74400
74401
74402
74403
74404
74405
74406
74407
74408
74409
74410
74411
74412
74413
74414
74415
74416
74417
74418
74419
74420
74421
74422
74423
74424
74425
74426
74427
74428
74429
74430
74431
74432
74433
74434
74435
74436
74437
74438
74439
74440
74441
74442
74443
74444
74445
74446
74447
74448
74449
74450
74451
74452
74453
74454
74455
74456
74457
74458
74459
74460
74461
74462
74463
74464
74465
74466
74467
74468
74469
74470
74471
74472
74473
74474
74475
74476
74477
74478
74479
74480
74481
74482
74483
74484
74485
74486
74487
74488
74489
74490
74491
74492
74493
74494
74495
74496
74497
74498
74499
74500
74501
74502
74503
74504
74505
74506
74507
74508
74509
74510
74511
74512
74513
74514
74515
74516
74517
74518
74519
74520
74521
74522
74523
74524
74525
74526
74527
74528
74529
74530
74531
74532
74533
74534
74535
74536
74537
74538
74539
74540
74541
74542
74543
74544
74545
74546
74547
74548
74549
74550
74551
74552
74553
74554
74555
74556
74557
74558
74559
74560
74561
74562
74563
74564
74565
74566
74567
74568
74569
74570
74571
74572
74573
74574
74575
74576
74577
74578
74579
74580
74581
74582
74583
74584
74585
74586
74587
74588
74589
74590
74591
74592
74593
74594
74595
74596
74597
74598
74599
74600
74601
74602
74603
74604
74605
74606
74607
74608
74609
74610
74611
74612
74613
74614
74615
74616
74617
74618
74619
74620
74621
74622
74623
74624
74625
74626
74627
74628
74629
74630
74631
74632
74633
74634
74635
74636
74637
74638
74639
74640
74641
74642
74643
74644
74645
74646
74647
74648
74649
74650
74651
74652
74653
74654
74655
74656
74657
74658
74659
74660
74661
74662
74663
74664
74665
74666
74667
74668
74669
74670
74671
74672
74673
74674
74675
74676
74677
74678
74679
74680
74681
74682
74683
74684
74685
74686
74687
74688
74689
74690
74691
74692
74693
74694
74695
74696
74697
74698
74699
74700
74701
74702
74703
74704
74705
74706
74707
74708
74709
74710
74711
74712
74713
74714
74715
74716
74717
74718
74719
74720
74721
74722
74723
74724
74725
74726
74727
74728
74729
74730
74731
74732
74733
74734
74735
74736
74737
74738
74739
74740
74741
74742
74743
74744
74745
74746
74747
74748
74749
74750
74751
74752
74753
74754
74755
74756
74757
74758
74759
74760
74761
74762
74763
74764
74765
74766
74767
74768
74769
74770
74771
74772
74773
74774
74775
74776
74777
74778
74779
74780
74781
74782
74783
74784
74785
74786
74787
74788
74789
74790
74791
74792
74793
74794
74795
74796
74797
74798
74799
74800
74801
74802
74803
74804
74805
74806
74807
74808
74809
74810
74811
74812
74813
74814
74815
74816
74817
74818
74819
74820
74821
74822
74823
74824
74825
74826
74827
74828
74829
74830
74831
74832
74833
74834
74835
74836
74837
74838
74839
74840
74841
74842
74843
74844
74845
74846
74847
74848
74849
74850
74851
74852
74853
74854
74855
74856
74857
74858
74859
74860
74861
74862
74863
74864
74865
74866
74867
74868
74869
74870
74871
74872
74873
74874
74875
74876
74877
74878
74879
74880
74881
74882
74883
74884
74885
74886
74887
74888
74889
74890
74891
74892
74893
74894
74895
74896
74897
74898
74899
74900
74901
74902
74903
74904
74905
74906
74907
74908
74909
74910
74911
74912
74913
74914
74915
74916
74917
74918
74919
74920
74921
74922
74923
74924
74925
74926
74927
74928
74929
74930
74931
74932
74933
74934
74935
74936
74937
74938
74939
74940
74941
74942
74943
74944
74945
74946
74947
74948
74949
74950
74951
74952
74953
74954
74955
74956
74957
74958
74959
74960
74961
74962
74963
74964
74965
74966
74967
74968
74969
74970
74971
74972
74973
74974
74975
74976
74977
74978
74979
74980
74981
74982
74983
74984
74985
74986
74987
74988
74989
74990
74991
74992
74993
74994
74995
74996
74997
74998
74999
75000
75001
75002
75003
75004
75005
75006
75007
75008
75009
75010
75011
75012
75013
75014
75015
75016
75017
75018
75019
75020
75021
75022
75023
75024
75025
75026
75027
75028
75029
75030
75031
75032
75033
75034
75035
75036
75037
75038
75039
75040
75041
75042
75043
75044
75045
75046
75047
75048
75049
75050
75051
75052
75053
75054
75055
75056
75057
75058
75059
75060
75061
75062
75063
75064
75065
75066
75067
75068
75069
75070
75071
75072
75073
75074
75075
75076
75077
75078
75079
75080
75081
75082
75083
75084
75085
75086
75087
75088
75089
75090
75091
75092
75093
75094
75095
75096
75097
75098
75099
75100
75101
75102
75103
75104
75105
75106
75107
75108
75109
75110
75111
75112
75113
75114
75115
75116
75117
75118
75119
75120
75121
75122
75123
75124
75125
75126
75127
75128
75129
75130
75131
75132
75133
75134
75135
75136
75137
75138
75139
75140
75141
75142
75143
75144
75145
75146
75147
75148
75149
75150
75151
75152
75153
75154
75155
75156
75157
75158
75159
75160
75161
75162
75163
75164
75165
75166
75167
75168
75169
75170
75171
75172
75173
75174
75175
75176
75177
75178
75179
75180
75181
75182
75183
75184
75185
75186
75187
75188
75189
75190
75191
75192
75193
75194
75195
75196
75197
75198
75199
75200
75201
75202
75203
75204
75205
75206
75207
75208
75209
75210
75211
75212
75213
75214
75215
75216
75217
75218
75219
75220
75221
75222
75223
75224
75225
75226
75227
75228
75229
75230
75231
75232
75233
75234
75235
75236
75237
75238
75239
75240
75241
75242
75243
75244
75245
75246
75247
75248
75249
75250
75251
75252
75253
75254
75255
75256
75257
75258
75259
75260
75261
75262
75263
75264
75265
75266
75267
75268
75269
75270
75271
75272
75273
75274
75275
75276
75277
75278
75279
75280
75281
75282
75283
75284
75285
75286
75287
75288
75289
75290
75291
75292
75293
75294
75295
75296
75297
75298
75299
75300
75301
75302
75303
75304
75305
75306
75307
75308
75309
75310
75311
75312
75313
75314
75315
75316
75317
75318
75319
75320
75321
75322
75323
75324
75325
75326
75327
75328
75329
75330
75331
75332
75333
75334
75335
75336
75337
75338
75339
75340
75341
75342
75343
75344
75345
75346
75347
75348
75349
75350
75351
75352
75353
75354
75355
75356
75357
75358
75359
75360
75361
75362
75363
75364
75365
75366
75367
75368
75369
75370
75371
75372
75373
75374
75375
75376
75377
75378
75379
75380
75381
75382
75383
75384
75385
75386
75387
75388
75389
75390
75391
75392
75393
75394
75395
75396
75397
75398
75399
75400
75401
75402
75403
75404
75405
75406
75407
75408
75409
75410
75411
75412
75413
75414
75415
75416
75417
75418
75419
75420
75421
75422
75423
75424
75425
75426
75427
75428
75429
75430
75431
75432
75433
75434
75435
75436
75437
75438
75439
75440
75441
75442
75443
75444
75445
75446
75447
75448
75449
75450
75451
75452
75453
75454
75455
75456
75457
75458
75459
75460
75461
75462
75463
75464
75465
75466
75467
75468
75469
75470
75471
75472
75473
75474
75475
75476
75477
75478
75479
75480
75481
75482
75483
75484
75485
75486
75487
75488
75489
75490
75491
75492
75493
75494
75495
75496
75497
75498
75499
75500
75501
75502
75503
75504
75505
75506
75507
75508
75509
75510
75511
75512
75513
75514
75515
75516
75517
75518
75519
75520
75521
75522
75523
75524
75525
75526
75527
75528
75529
75530
75531
75532
75533
75534
75535
75536
75537
75538
75539
75540
75541
75542
75543
75544
75545
75546
75547
75548
75549
75550
75551
75552
75553
75554
75555
75556
75557
75558
75559
75560
75561
75562
75563
75564
75565
75566
75567
75568
75569
75570
75571
75572
75573
75574
75575
75576
75577
75578
75579
75580
75581
75582
75583
75584
75585
75586
75587
75588
75589
75590
75591
75592
75593
75594
75595
75596
75597
75598
75599
75600
75601
75602
75603
75604
75605
75606
75607
75608
75609
75610
75611
75612
75613
75614
75615
75616
75617
75618
75619
75620
75621
75622
75623
75624
75625
75626
75627
75628
75629
75630
75631
75632
75633
75634
75635
75636
75637
75638
75639
75640
75641
75642
75643
75644
75645
75646
75647
75648
75649
75650
75651
75652
75653
75654
75655
75656
75657
75658
75659
75660
75661
75662
75663
75664
75665
75666
75667
75668
75669
75670
75671
75672
75673
75674
75675
75676
75677
75678
75679
75680
75681
75682
75683
75684
75685
75686
75687
75688
75689
75690
75691
75692
75693
75694
75695
75696
75697
75698
75699
75700
75701
75702
75703
75704
75705
75706
75707
75708
75709
75710
75711
75712
75713
75714
75715
75716
75717
75718
75719
75720
75721
75722
75723
75724
75725
75726
75727
75728
75729
75730
75731
75732
75733
75734
75735
75736
75737
75738
75739
75740
75741
75742
75743
75744
75745
75746
75747
75748
75749
75750
75751
75752
75753
75754
75755
75756
75757
75758
75759
75760
75761
75762
75763
75764
75765
75766
75767
75768
75769
75770
75771
75772
75773
75774
75775
75776
75777
75778
75779
75780
75781
75782
75783
75784
75785
75786
75787
75788
75789
75790
75791
75792
75793
75794
75795
75796
75797
75798
75799
75800
75801
75802
75803
75804
75805
75806
75807
75808
75809
75810
75811
75812
75813
75814
75815
75816
75817
75818
75819
75820
75821
75822
75823
75824
75825
75826
75827
75828
75829
75830
75831
75832
75833
75834
75835
75836
75837
75838
75839
75840
75841
75842
75843
75844
75845
75846
75847
75848
75849
75850
75851
75852
75853
75854
75855
75856
75857
75858
75859
75860
75861
75862
75863
75864
75865
75866
75867
75868
75869
75870
75871
75872
75873
75874
75875
75876
75877
75878
75879
75880
75881
75882
75883
75884
75885
75886
75887
75888
75889
75890
75891
75892
75893
75894
75895
75896
75897
75898
75899
75900
75901
75902
75903
75904
75905
75906
75907
75908
75909
75910
75911
75912
75913
75914
75915
75916
75917
75918
75919
75920
75921
75922
75923
75924
75925
75926
75927
75928
75929
75930
75931
75932
75933
75934
75935
75936
75937
75938
75939
75940
75941
75942
75943
75944
75945
75946
75947
75948
75949
75950
75951
75952
75953
75954
75955
75956
75957
75958
75959
75960
75961
75962
75963
75964
75965
75966
75967
75968
75969
75970
75971
75972
75973
75974
75975
75976
75977
75978
75979
75980
75981
75982
75983
75984
75985
75986
75987
75988
75989
75990
75991
75992
75993
75994
75995
75996
75997
75998
75999
76000
76001
76002
76003
76004
76005
76006
76007
76008
76009
76010
76011
76012
76013
76014
76015
76016
76017
76018
76019
76020
76021
76022
76023
76024
76025
76026
76027
76028
76029
76030
76031
76032
76033
76034
76035
76036
76037
76038
76039
76040
76041
76042
76043
76044
76045
76046
76047
76048
76049
76050
76051
76052
76053
76054
76055
76056
76057
76058
76059
76060
76061
76062
76063
76064
76065
76066
76067
76068
76069
76070
76071
76072
76073
76074
76075
76076
76077
76078
76079
76080
76081
76082
76083
76084
76085
76086
76087
76088
76089
76090
76091
76092
76093
76094
76095
76096
76097
76098
76099
76100
76101
76102
76103
76104
76105
76106
76107
76108
76109
76110
76111
76112
76113
76114
76115
76116
76117
76118
76119
76120
76121
76122
76123
76124
76125
76126
76127
76128
76129
76130
76131
76132
76133
76134
76135
76136
76137
76138
76139
76140
76141
76142
76143
76144
76145
76146
76147
76148
76149
76150
76151
76152
76153
76154
76155
76156
76157
76158
76159
76160
76161
76162
76163
76164
76165
76166
76167
76168
76169
76170
76171
76172
76173
76174
76175
76176
76177
76178
76179
76180
76181
76182
76183
76184
76185
76186
76187
76188
76189
76190
76191
76192
76193
76194
76195
76196
76197
76198
76199
76200
76201
76202
76203
76204
76205
76206
76207
76208
76209
76210
76211
76212
76213
76214
76215
76216
76217
76218
76219
76220
76221
76222
76223
76224
76225
76226
76227
76228
76229
76230
76231
76232
76233
76234
76235
76236
76237
76238
76239
76240
76241
76242
76243
76244
76245
76246
76247
76248
76249
76250
76251
76252
76253
76254
76255
76256
76257
76258
76259
76260
76261
76262
76263
76264
76265
76266
76267
76268
76269
76270
76271
76272
76273
76274
76275
76276
76277
76278
76279
76280
76281
76282
76283
76284
76285
76286
76287
76288
76289
76290
76291
76292
76293
76294
76295
76296
76297
76298
76299
76300
76301
76302
76303
76304
76305
76306
76307
76308
76309
76310
76311
76312
76313
76314
76315
76316
76317
76318
76319
76320
76321
76322
76323
76324
76325
76326
76327
76328
76329
76330
76331
76332
76333
76334
76335
76336
76337
76338
76339
76340
76341
76342
76343
76344
76345
76346
76347
76348
76349
76350
76351
76352
76353
76354
76355
76356
76357
76358
76359
76360
76361
76362
76363
76364
76365
76366
76367
76368
76369
76370
76371
76372
76373
76374
76375
76376
76377
76378
76379
76380
76381
76382
76383
76384
76385
76386
76387
76388
76389
76390
76391
76392
76393
76394
76395
76396
76397
76398
76399
76400
76401
76402
76403
76404
76405
76406
76407
76408
76409
76410
76411
76412
76413
76414
76415
76416
76417
76418
76419
76420
76421
76422
76423
76424
76425
76426
76427
76428
76429
76430
76431
76432
76433
76434
76435
76436
76437
76438
76439
76440
76441
76442
76443
76444
76445
76446
76447
76448
76449
76450
76451
76452
76453
76454
76455
76456
76457
76458
76459
76460
76461
76462
76463
76464
76465
76466
76467
76468
76469
76470
76471
76472
76473
76474
76475
76476
76477
76478
76479
76480
76481
76482
76483
76484
76485
76486
76487
76488
76489
76490
76491
76492
76493
76494
76495
76496
76497
76498
76499
76500
76501
76502
76503
76504
76505
76506
76507
76508
76509
76510
76511
76512
76513
76514
76515
76516
76517
76518
76519
76520
76521
76522
76523
76524
76525
76526
76527
76528
76529
76530
76531
76532
76533
76534
76535
76536
76537
76538
76539
76540
76541
76542
76543
76544
76545
76546
76547
76548
76549
76550
76551
76552
76553
76554
76555
76556
76557
76558
76559
76560
76561
76562
76563
76564
76565
76566
76567
76568
76569
76570
76571
76572
76573
76574
76575
76576
76577
76578
76579
76580
76581
76582
76583
76584
76585
76586
76587
76588
76589
76590
76591
76592
76593
76594
76595
76596
76597
76598
76599
76600
76601
76602
76603
76604
76605
76606
76607
76608
76609
76610
76611
76612
76613
76614
76615
76616
76617
76618
76619
76620
76621
76622
76623
76624
76625
76626
76627
76628
76629
76630
76631
76632
76633
76634
76635
76636
76637
76638
76639
76640
76641
76642
76643
76644
76645
76646
76647
76648
76649
76650
76651
76652
76653
76654
76655
76656
76657
76658
76659
76660
76661
76662
76663
76664
76665
76666
76667
76668
76669
76670
76671
76672
76673
76674
76675
76676
76677
76678
76679
76680
76681
76682
76683
76684
76685
76686
76687
76688
76689
76690
76691
76692
76693
76694
76695
76696
76697
76698
76699
76700
76701
76702
76703
76704
76705
76706
76707
76708
76709
76710
76711
76712
76713
76714
76715
76716
76717
76718
76719
76720
76721
76722
76723
76724
76725
76726
76727
76728
76729
76730
76731
76732
76733
76734
76735
76736
76737
76738
76739
76740
76741
76742
76743
76744
76745
76746
76747
76748
76749
76750
76751
76752
76753
76754
76755
76756
76757
76758
76759
76760
76761
76762
76763
76764
76765
76766
76767
76768
76769
76770
76771
76772
76773
76774
76775
76776
76777
76778
76779
76780
76781
76782
76783
76784
76785
76786
76787
76788
76789
76790
76791
76792
76793
76794
76795
76796
76797
76798
76799
76800
76801
76802
76803
76804
76805
76806
76807
76808
76809
76810
76811
76812
76813
76814
76815
76816
76817
76818
76819
76820
76821
76822
76823
76824
76825
76826
76827
76828
76829
76830
76831
76832
76833
76834
76835
76836
76837
76838
76839
76840
76841
76842
76843
76844
76845
76846
76847
76848
76849
76850
76851
76852
76853
76854
76855
76856
76857
76858
76859
76860
76861
76862
76863
76864
76865
76866
76867
76868
76869
76870
76871
76872
76873
76874
76875
76876
76877
76878
76879
76880
76881
76882
76883
76884
76885
76886
76887
76888
76889
76890
76891
76892
76893
76894
76895
76896
76897
76898
76899
76900
76901
76902
76903
76904
76905
76906
76907
76908
76909
76910
76911
76912
76913
76914
76915
76916
76917
76918
76919
76920
76921
76922
76923
76924
76925
76926
76927
76928
76929
76930
76931
76932
76933
76934
76935
76936
76937
76938
76939
76940
76941
76942
76943
76944
76945
76946
76947
76948
76949
76950
76951
76952
76953
76954
76955
76956
76957
76958
76959
76960
76961
76962
76963
76964
76965
76966
76967
76968
76969
76970
76971
76972
76973
76974
76975
76976
76977
76978
76979
76980
76981
76982
76983
76984
76985
76986
76987
76988
76989
76990
76991
76992
76993
76994
76995
76996
76997
76998
76999
77000
77001
77002
77003
77004
77005
77006
77007
77008
77009
77010
77011
77012
77013
77014
77015
77016
77017
77018
77019
77020
77021
77022
77023
77024
77025
77026
77027
77028
77029
77030
77031
77032
77033
77034
77035
77036
77037
77038
77039
77040
77041
77042
77043
77044
77045
77046
77047
77048
77049
77050
77051
77052
77053
77054
77055
77056
77057
77058
77059
77060
77061
77062
77063
77064
77065
77066
77067
77068
77069
77070
77071
77072
77073
77074
77075
77076
77077
77078
77079
77080
77081
77082
77083
77084
77085
77086
77087
77088
77089
77090
77091
77092
77093
77094
77095
77096
77097
77098
77099
77100
77101
77102
77103
77104
77105
77106
77107
77108
77109
77110
77111
77112
77113
77114
77115
77116
77117
77118
77119
77120
77121
77122
77123
77124
77125
77126
77127
77128
77129
77130
77131
77132
77133
77134
77135
77136
77137
77138
77139
77140
77141
77142
77143
77144
77145
77146
77147
77148
77149
77150
77151
77152
77153
77154
77155
77156
77157
77158
77159
77160
77161
77162
77163
77164
77165
77166
77167
77168
77169
77170
77171
77172
77173
77174
77175
77176
77177
77178
77179
77180
77181
77182
77183
77184
77185
77186
77187
77188
77189
77190
77191
77192
77193
77194
77195
77196
77197
77198
77199
77200
77201
77202
77203
77204
77205
77206
77207
77208
77209
77210
77211
77212
77213
77214
77215
77216
77217
77218
77219
77220
77221
77222
77223
77224
77225
77226
77227
77228
77229
77230
77231
77232
77233
77234
77235
77236
77237
77238
77239
77240
77241
77242
77243
77244
77245
77246
77247
77248
77249
77250
77251
77252
77253
77254
77255
77256
77257
77258
77259
77260
77261
77262
77263
77264
77265
77266
77267
77268
77269
77270
77271
77272
77273
77274
77275
77276
77277
77278
77279
77280
77281
77282
77283
77284
77285
77286
77287
77288
77289
77290
77291
77292
77293
77294
77295
77296
77297
77298
77299
77300
77301
77302
77303
77304
77305
77306
77307
77308
77309
77310
77311
77312
77313
77314
77315
77316
77317
77318
77319
77320
77321
77322
77323
77324
77325
77326
77327
77328
77329
77330
77331
77332
77333
77334
77335
77336
77337
77338
77339
77340
77341
77342
77343
77344
77345
77346
77347
77348
77349
77350
77351
77352
77353
77354
77355
77356
77357
77358
77359
77360
77361
77362
77363
77364
77365
77366
77367
77368
77369
77370
77371
77372
77373
77374
77375
77376
77377
77378
77379
77380
77381
77382
77383
77384
77385
77386
77387
77388
77389
77390
77391
77392
77393
77394
77395
77396
77397
77398
77399
77400
77401
77402
77403
77404
77405
77406
77407
77408
77409
77410
77411
77412
77413
77414
77415
77416
77417
77418
77419
77420
77421
77422
77423
77424
77425
77426
77427
77428
77429
77430
77431
77432
77433
77434
77435
77436
77437
77438
77439
77440
77441
77442
77443
77444
77445
77446
77447
77448
77449
77450
77451
77452
77453
77454
77455
77456
77457
77458
77459
77460
77461
77462
77463
77464
77465
77466
77467
77468
77469
77470
77471
77472
77473
77474
77475
77476
77477
77478
77479
77480
77481
77482
77483
77484
77485
77486
77487
77488
77489
77490
77491
77492
77493
77494
77495
77496
77497
77498
77499
77500
77501
77502
77503
77504
77505
77506
77507
77508
77509
77510
77511
77512
77513
77514
77515
77516
77517
77518
77519
77520
77521
77522
77523
77524
77525
77526
77527
77528
77529
77530
77531
77532
77533
77534
77535
77536
77537
77538
77539
77540
77541
77542
77543
77544
77545
77546
77547
77548
77549
77550
77551
77552
77553
77554
77555
77556
77557
77558
77559
77560
77561
77562
77563
77564
77565
77566
77567
77568
77569
77570
77571
77572
77573
77574
77575
77576
77577
77578
77579
77580
77581
77582
77583
77584
77585
77586
77587
77588
77589
77590
77591
77592
77593
77594
77595
77596
77597
77598
77599
77600
77601
77602
77603
77604
77605
77606
77607
77608
77609
77610
77611
77612
77613
77614
77615
77616
77617
77618
77619
77620
77621
77622
77623
77624
77625
77626
77627
77628
77629
77630
77631
77632
77633
77634
77635
77636
77637
77638
77639
77640
77641
77642
77643
77644
77645
77646
77647
77648
77649
77650
77651
77652
77653
77654
77655
77656
77657
77658
77659
77660
77661
77662
77663
77664
77665
77666
77667
77668
77669
77670
77671
77672
77673
77674
77675
77676
77677
77678
77679
77680
77681
77682
77683
77684
77685
77686
77687
77688
77689
77690
77691
77692
77693
77694
77695
77696
77697
77698
77699
77700
77701
77702
77703
77704
77705
77706
77707
77708
77709
77710
77711
77712
77713
77714
77715
77716
77717
77718
77719
77720
77721
77722
77723
77724
77725
77726
77727
77728
77729
77730
77731
77732
77733
77734
77735
77736
77737
77738
77739
77740
77741
77742
77743
77744
77745
77746
77747
77748
77749
77750
77751
77752
77753
77754
77755
77756
77757
77758
77759
77760
77761
77762
77763
77764
77765
77766
77767
77768
77769
77770
77771
77772
77773
77774
77775
77776
77777
77778
77779
77780
77781
77782
77783
77784
77785
77786
77787
77788
77789
77790
77791
77792
77793
77794
77795
77796
77797
77798
77799
77800
77801
77802
77803
77804
77805
77806
77807
77808
77809
77810
77811
77812
77813
77814
77815
77816
77817
77818
77819
77820
77821
77822
77823
77824
77825
77826
77827
77828
77829
77830
77831
77832
77833
77834
77835
77836
77837
77838
77839
77840
77841
77842
77843
77844
77845
77846
77847
77848
77849
77850
77851
77852
77853
77854
77855
77856
77857
77858
77859
77860
77861
77862
77863
77864
77865
77866
77867
77868
77869
77870
77871
77872
77873
77874
77875
77876
77877
77878
77879
77880
77881
77882
77883
77884
77885
77886
77887
77888
77889
77890
77891
77892
77893
77894
77895
77896
77897
77898
77899
77900
77901
77902
77903
77904
77905
77906
77907
77908
77909
77910
77911
77912
77913
77914
77915
77916
77917
77918
77919
77920
77921
77922
77923
77924
77925
77926
77927
77928
77929
77930
77931
77932
77933
77934
77935
77936
77937
77938
77939
77940
77941
77942
77943
77944
77945
77946
77947
77948
77949
77950
77951
77952
77953
77954
77955
77956
77957
77958
77959
77960
77961
77962
77963
77964
77965
77966
77967
77968
77969
77970
77971
77972
77973
77974
77975
77976
77977
77978
77979
77980
77981
77982
77983
77984
77985
77986
77987
77988
77989
77990
77991
77992
77993
77994
77995
77996
77997
77998
77999
78000
78001
78002
78003
78004
78005
78006
78007
78008
78009
78010
78011
78012
78013
78014
78015
78016
78017
78018
78019
78020
78021
78022
78023
78024
78025
78026
78027
78028
78029
78030
78031
78032
78033
78034
78035
78036
78037
78038
78039
78040
78041
78042
78043
78044
78045
78046
78047
78048
78049
78050
78051
78052
78053
78054
78055
78056
78057
78058
78059
78060
78061
78062
78063
78064
78065
78066
78067
78068
78069
78070
78071
78072
78073
78074
78075
78076
78077
78078
78079
78080
78081
78082
78083
78084
78085
78086
78087
78088
78089
78090
78091
78092
78093
78094
78095
78096
78097
78098
78099
78100
78101
78102
78103
78104
78105
78106
78107
78108
78109
78110
78111
78112
78113
78114
78115
78116
78117
78118
78119
78120
78121
78122
78123
78124
78125
78126
78127
78128
78129
78130
78131
78132
78133
78134
78135
78136
78137
78138
78139
78140
78141
78142
78143
78144
78145
78146
78147
78148
78149
78150
78151
78152
78153
78154
78155
78156
78157
78158
78159
78160
78161
78162
78163
78164
78165
78166
78167
78168
78169
78170
78171
78172
78173
78174
78175
78176
78177
78178
78179
78180
78181
78182
78183
78184
78185
78186
78187
78188
78189
78190
78191
78192
78193
78194
78195
78196
78197
78198
78199
78200
78201
78202
78203
78204
78205
78206
78207
78208
78209
78210
78211
78212
78213
78214
78215
78216
78217
78218
78219
78220
78221
78222
78223
78224
78225
78226
78227
78228
78229
78230
78231
78232
78233
78234
78235
78236
78237
78238
78239
78240
78241
78242
78243
78244
78245
78246
78247
78248
78249
78250
78251
78252
78253
78254
78255
78256
78257
78258
78259
78260
78261
78262
78263
78264
78265
78266
78267
78268
78269
78270
78271
78272
78273
78274
78275
78276
78277
78278
78279
78280
78281
78282
78283
78284
78285
78286
78287
78288
78289
78290
78291
78292
78293
78294
78295
78296
78297
78298
78299
78300
78301
78302
78303
78304
78305
78306
78307
78308
78309
78310
78311
78312
78313
78314
78315
78316
78317
78318
78319
78320
78321
78322
78323
78324
78325
78326
78327
78328
78329
78330
78331
78332
78333
78334
78335
78336
78337
78338
78339
78340
78341
78342
78343
78344
78345
78346
78347
78348
78349
78350
78351
78352
78353
78354
78355
78356
78357
78358
78359
78360
78361
78362
78363
78364
78365
78366
78367
78368
78369
78370
78371
78372
78373
78374
78375
78376
78377
78378
78379
78380
78381
78382
78383
78384
78385
78386
78387
78388
78389
78390
78391
78392
78393
78394
78395
78396
78397
78398
78399
78400
78401
78402
78403
78404
78405
78406
78407
78408
78409
78410
78411
78412
78413
78414
78415
78416
78417
78418
78419
78420
78421
78422
78423
78424
78425
78426
78427
78428
78429
78430
78431
78432
78433
78434
78435
78436
78437
78438
78439
78440
78441
78442
78443
78444
78445
78446
78447
78448
78449
78450
78451
78452
78453
78454
78455
78456
78457
78458
78459
78460
78461
78462
78463
78464
78465
78466
78467
78468
78469
78470
78471
78472
78473
78474
78475
78476
78477
78478
78479
78480
78481
78482
78483
78484
78485
78486
78487
78488
78489
78490
78491
78492
78493
78494
78495
78496
78497
78498
78499
78500
78501
78502
78503
78504
78505
78506
78507
78508
78509
78510
78511
78512
78513
78514
78515
78516
78517
78518
78519
78520
78521
78522
78523
78524
78525
78526
78527
78528
78529
78530
78531
78532
78533
78534
78535
78536
78537
78538
78539
78540
78541
78542
78543
78544
78545
78546
78547
78548
78549
78550
78551
78552
78553
78554
78555
78556
78557
78558
78559
78560
78561
78562
78563
78564
78565
78566
78567
78568
78569
78570
78571
78572
78573
78574
78575
78576
78577
78578
78579
78580
78581
78582
78583
78584
78585
78586
78587
78588
78589
78590
78591
78592
78593
78594
78595
78596
78597
78598
78599
78600
78601
78602
78603
78604
78605
78606
78607
78608
78609
78610
78611
78612
78613
78614
78615
78616
78617
78618
78619
78620
78621
78622
78623
78624
78625
78626
78627
78628
78629
78630
78631
78632
78633
78634
78635
78636
78637
78638
78639
78640
78641
78642
78643
78644
78645
78646
78647
78648
78649
78650
78651
78652
78653
78654
78655
78656
78657
78658
78659
78660
78661
78662
78663
78664
78665
78666
78667
78668
78669
78670
78671
78672
78673
78674
78675
78676
78677
78678
78679
78680
78681
78682
78683
78684
78685
78686
78687
78688
78689
78690
78691
78692
78693
78694
78695
78696
78697
78698
78699
78700
78701
78702
78703
78704
78705
78706
78707
78708
78709
78710
78711
78712
78713
78714
78715
78716
78717
78718
78719
78720
78721
78722
78723
78724
78725
78726
78727
78728
78729
78730
78731
78732
78733
78734
78735
78736
78737
78738
78739
78740
78741
78742
78743
78744
78745
78746
78747
78748
78749
78750
78751
78752
78753
78754
78755
78756
78757
78758
78759
78760
78761
78762
78763
78764
78765
78766
78767
78768
78769
78770
78771
78772
78773
78774
78775
78776
78777
78778
78779
78780
78781
78782
78783
78784
78785
78786
78787
78788
78789
78790
78791
78792
78793
78794
78795
78796
78797
78798
78799
78800
78801
78802
78803
78804
78805
78806
78807
78808
78809
78810
78811
78812
78813
78814
78815
78816
78817
78818
78819
78820
78821
78822
78823
78824
78825
78826
78827
78828
78829
78830
78831
78832
78833
78834
78835
78836
78837
78838
78839
78840
78841
78842
78843
78844
78845
78846
78847
78848
78849
78850
78851
78852
78853
78854
78855
78856
78857
78858
78859
78860
78861
78862
78863
78864
78865
78866
78867
78868
78869
78870
78871
78872
78873
78874
78875
78876
78877
78878
78879
78880
78881
78882
78883
78884
78885
78886
78887
78888
78889
78890
78891
78892
78893
78894
78895
78896
78897
78898
78899
78900
78901
78902
78903
78904
78905
78906
78907
78908
78909
78910
78911
78912
78913
78914
78915
78916
78917
78918
78919
78920
78921
78922
78923
78924
78925
78926
78927
78928
78929
78930
78931
78932
78933
78934
78935
78936
78937
78938
78939
78940
78941
78942
78943
78944
78945
78946
78947
78948
78949
78950
78951
78952
78953
78954
78955
78956
78957
78958
78959
78960
78961
78962
78963
78964
78965
78966
78967
78968
78969
78970
78971
78972
78973
78974
78975
78976
78977
78978
78979
78980
78981
78982
78983
78984
78985
78986
78987
78988
78989
78990
78991
78992
78993
78994
78995
78996
78997
78998
78999
79000
79001
79002
79003
79004
79005
79006
79007
79008
79009
79010
79011
79012
79013
79014
79015
79016
79017
79018
79019
79020
79021
79022
79023
79024
79025
79026
79027
79028
79029
79030
79031
79032
79033
79034
79035
79036
79037
79038
79039
79040
79041
79042
79043
79044
79045
79046
79047
79048
79049
79050
79051
79052
79053
79054
79055
79056
79057
79058
79059
79060
79061
79062
79063
79064
79065
79066
79067
79068
79069
79070
79071
79072
79073
79074
79075
79076
79077
79078
79079
79080
79081
79082
79083
79084
79085
79086
79087
79088
79089
79090
79091
79092
79093
79094
79095
79096
79097
79098
79099
79100
79101
79102
79103
79104
79105
79106
79107
79108
79109
79110
79111
79112
79113
79114
79115
79116
79117
79118
79119
79120
79121
79122
79123
79124
79125
79126
79127
79128
79129
79130
79131
79132
79133
79134
79135
79136
79137
79138
79139
79140
79141
79142
79143
79144
79145
79146
79147
79148
79149
79150
79151
79152
79153
79154
79155
79156
79157
79158
79159
79160
79161
79162
79163
79164
79165
79166
79167
79168
79169
79170
79171
79172
79173
79174
79175
79176
79177
79178
79179
79180
79181
79182
79183
79184
79185
79186
79187
79188
79189
79190
79191
79192
79193
79194
79195
79196
79197
79198
79199
79200
79201
79202
79203
79204
79205
79206
79207
79208
79209
79210
79211
79212
79213
79214
79215
79216
79217
79218
79219
79220
79221
79222
79223
79224
79225
79226
79227
79228
79229
79230
79231
79232
79233
79234
79235
79236
79237
79238
79239
79240
79241
79242
79243
79244
79245
79246
79247
79248
79249
79250
79251
79252
79253
79254
79255
79256
79257
79258
79259
79260
79261
79262
79263
79264
79265
79266
79267
79268
79269
79270
79271
79272
79273
79274
79275
79276
79277
79278
79279
79280
79281
79282
79283
79284
79285
79286
79287
79288
79289
79290
79291
79292
79293
79294
79295
79296
79297
79298
79299
79300
79301
79302
79303
79304
79305
79306
79307
79308
79309
79310
79311
79312
79313
79314
79315
79316
79317
79318
79319
79320
79321
79322
79323
79324
79325
79326
79327
79328
79329
79330
79331
79332
79333
79334
79335
79336
79337
79338
79339
79340
79341
79342
79343
79344
79345
79346
79347
79348
79349
79350
79351
79352
79353
79354
79355
79356
79357
79358
79359
79360
79361
79362
79363
79364
79365
79366
79367
79368
79369
79370
79371
79372
79373
79374
79375
79376
79377
79378
79379
79380
79381
79382
79383
79384
79385
79386
79387
79388
79389
79390
79391
79392
79393
79394
79395
79396
79397
79398
79399
79400
79401
79402
79403
79404
79405
79406
79407
79408
79409
79410
79411
79412
79413
79414
79415
79416
79417
79418
79419
79420
79421
79422
79423
79424
79425
79426
79427
79428
79429
79430
79431
79432
79433
79434
79435
79436
79437
79438
79439
79440
79441
79442
79443
79444
79445
79446
79447
79448
79449
79450
79451
79452
79453
79454
79455
79456
79457
79458
79459
79460
79461
79462
79463
79464
79465
79466
79467
79468
79469
79470
79471
79472
79473
79474
79475
79476
79477
79478
79479
79480
79481
79482
79483
79484
79485
79486
79487
79488
79489
79490
79491
79492
79493
79494
79495
79496
79497
79498
79499
79500
79501
79502
79503
79504
79505
79506
79507
79508
79509
79510
79511
79512
79513
79514
79515
79516
79517
79518
79519
79520
79521
79522
79523
79524
79525
79526
79527
79528
79529
79530
79531
79532
79533
79534
79535
79536
79537
79538
79539
79540
79541
79542
79543
79544
79545
79546
79547
79548
79549
79550
79551
79552
79553
79554
79555
79556
79557
79558
79559
79560
79561
79562
79563
79564
79565
79566
79567
79568
79569
79570
79571
79572
79573
79574
79575
79576
79577
79578
79579
79580
79581
79582
79583
79584
79585
79586
79587
79588
79589
79590
79591
79592
79593
79594
79595
79596
79597
79598
79599
79600
79601
79602
79603
79604
79605
79606
79607
79608
79609
79610
79611
79612
79613
79614
79615
79616
79617
79618
79619
79620
79621
79622
79623
79624
79625
79626
79627
79628
79629
79630
79631
79632
79633
79634
79635
79636
79637
79638
79639
79640
79641
79642
79643
79644
79645
79646
79647
79648
79649
79650
79651
79652
79653
79654
79655
79656
79657
79658
79659
79660
79661
79662
79663
79664
79665
79666
79667
79668
79669
79670
79671
79672
79673
79674
79675
79676
79677
79678
79679
79680
79681
79682
79683
79684
79685
79686
79687
79688
79689
79690
79691
79692
79693
79694
79695
79696
79697
79698
79699
79700
79701
79702
79703
79704
79705
79706
79707
79708
79709
79710
79711
79712
79713
79714
79715
79716
79717
79718
79719
79720
79721
79722
79723
79724
79725
79726
79727
79728
79729
79730
79731
79732
79733
79734
79735
79736
79737
79738
79739
79740
79741
79742
79743
79744
79745
79746
79747
79748
79749
79750
79751
79752
79753
79754
79755
79756
79757
79758
79759
79760
79761
79762
79763
79764
79765
79766
79767
79768
79769
79770
79771
79772
79773
79774
79775
79776
79777
79778
79779
79780
79781
79782
79783
79784
79785
79786
79787
79788
79789
79790
79791
79792
79793
79794
79795
79796
79797
79798
79799
79800
79801
79802
79803
79804
79805
79806
79807
79808
79809
79810
79811
79812
79813
79814
79815
79816
79817
79818
79819
79820
79821
79822
79823
79824
79825
79826
79827
79828
79829
79830
79831
79832
79833
79834
79835
79836
79837
79838
79839
79840
79841
79842
79843
79844
79845
79846
79847
79848
79849
79850
79851
79852
79853
79854
79855
79856
79857
79858
79859
79860
79861
79862
79863
79864
79865
79866
79867
79868
79869
79870
79871
79872
79873
79874
79875
79876
79877
79878
79879
79880
79881
79882
79883
79884
79885
79886
79887
79888
79889
79890
79891
79892
79893
79894
79895
79896
79897
79898
79899
79900
79901
79902
79903
79904
79905
79906
79907
79908
79909
79910
79911
79912
79913
79914
79915
79916
79917
79918
79919
79920
79921
79922
79923
79924
79925
79926
79927
79928
79929
79930
79931
79932
79933
79934
79935
79936
79937
79938
79939
79940
79941
79942
79943
79944
79945
79946
79947
79948
79949
79950
79951
79952
79953
79954
79955
79956
79957
79958
79959
79960
79961
79962
79963
79964
79965
79966
79967
79968
79969
79970
79971
79972
79973
79974
79975
79976
79977
79978
79979
79980
79981
79982
79983
79984
79985
79986
79987
79988
79989
79990
79991
79992
79993
79994
79995
79996
79997
79998
79999
80000
80001
80002
80003
80004
80005
80006
80007
80008
80009
80010
80011
80012
80013
80014
80015
80016
80017
80018
80019
80020
80021
80022
80023
80024
80025
80026
80027
80028
80029
80030
80031
80032
80033
80034
80035
80036
80037
80038
80039
80040
80041
80042
80043
80044
80045
80046
80047
80048
80049
80050
80051
80052
80053
80054
80055
80056
80057
80058
80059
80060
80061
80062
80063
80064
80065
80066
80067
80068
80069
80070
80071
80072
80073
80074
80075
80076
80077
80078
80079
80080
80081
80082
80083
80084
80085
80086
80087
80088
80089
80090
80091
80092
80093
80094
80095
80096
80097
80098
80099
80100
80101
80102
80103
80104
80105
80106
80107
80108
80109
80110
80111
80112
80113
80114
80115
80116
80117
80118
80119
80120
80121
80122
80123
80124
80125
80126
80127
80128
80129
80130
80131
80132
80133
80134
80135
80136
80137
80138
80139
80140
80141
80142
80143
80144
80145
80146
80147
80148
80149
80150
80151
80152
80153
80154
80155
80156
80157
80158
80159
80160
80161
80162
80163
80164
80165
80166
80167
80168
80169
80170
80171
80172
80173
80174
80175
80176
80177
80178
80179
80180
80181
80182
80183
80184
80185
80186
80187
80188
80189
80190
80191
80192
80193
80194
80195
80196
80197
80198
80199
80200
80201
80202
80203
80204
80205
80206
80207
80208
80209
80210
80211
80212
80213
80214
80215
80216
80217
80218
80219
80220
80221
80222
80223
80224
80225
80226
80227
80228
80229
80230
80231
80232
80233
80234
80235
80236
80237
80238
80239
80240
80241
80242
80243
80244
80245
80246
80247
80248
80249
80250
80251
80252
80253
80254
80255
80256
80257
80258
80259
80260
80261
80262
80263
80264
80265
80266
80267
80268
80269
80270
80271
80272
80273
80274
80275
80276
80277
80278
80279
80280
80281
80282
80283
80284
80285
80286
80287
80288
80289
80290
80291
80292
80293
80294
80295
80296
80297
80298
80299
80300
80301
80302
80303
80304
80305
80306
80307
80308
80309
80310
80311
80312
80313
80314
80315
80316
80317
80318
80319
80320
80321
80322
80323
80324
80325
80326
80327
80328
80329
80330
80331
80332
80333
80334
80335
80336
80337
80338
80339
80340
80341
80342
80343
80344
80345
80346
80347
80348
80349
80350
80351
80352
80353
80354
80355
80356
80357
80358
80359
80360
80361
80362
80363
80364
80365
80366
80367
80368
80369
80370
80371
80372
80373
80374
80375
80376
80377
80378
80379
80380
80381
80382
80383
80384
80385
80386
80387
80388
80389
80390
80391
80392
80393
80394
80395
80396
80397
80398
80399
80400
80401
80402
80403
80404
80405
80406
80407
80408
80409
80410
80411
80412
80413
80414
80415
80416
80417
80418
80419
80420
80421
80422
80423
80424
80425
80426
80427
80428
80429
80430
80431
80432
80433
80434
80435
80436
80437
80438
80439
80440
80441
80442
80443
80444
80445
80446
80447
80448
80449
80450
80451
80452
80453
80454
80455
80456
80457
80458
80459
80460
80461
80462
80463
80464
80465
80466
80467
80468
80469
80470
80471
80472
80473
80474
80475
80476
80477
80478
80479
80480
80481
80482
80483
80484
80485
80486
80487
80488
80489
80490
80491
80492
80493
80494
80495
80496
80497
80498
80499
80500
80501
80502
80503
80504
80505
80506
80507
80508
80509
80510
80511
80512
80513
80514
80515
80516
80517
80518
80519
80520
80521
80522
80523
80524
80525
80526
80527
80528
80529
80530
80531
80532
80533
80534
80535
80536
80537
80538
80539
80540
80541
80542
80543
80544
80545
80546
80547
80548
80549
80550
80551
80552
80553
80554
80555
80556
80557
80558
80559
80560
80561
80562
80563
80564
80565
80566
80567
80568
80569
80570
80571
80572
80573
80574
80575
80576
80577
80578
80579
80580
80581
80582
80583
80584
80585
80586
80587
80588
80589
80590
80591
80592
80593
80594
80595
80596
80597
80598
80599
80600
80601
80602
80603
80604
80605
80606
80607
80608
80609
80610
80611
80612
80613
80614
80615
80616
80617
80618
80619
80620
80621
80622
80623
80624
80625
80626
80627
80628
80629
80630
80631
80632
80633
80634
80635
80636
80637
80638
80639
80640
80641
80642
80643
80644
80645
80646
80647
80648
80649
80650
80651
80652
80653
80654
80655
80656
80657
80658
80659
80660
80661
80662
80663
80664
80665
80666
80667
80668
80669
80670
80671
80672
80673
80674
80675
80676
80677
80678
80679
80680
80681
80682
80683
80684
80685
80686
80687
80688
80689
80690
80691
80692
80693
80694
80695
80696
80697
80698
80699
80700
80701
80702
80703
80704
80705
80706
80707
80708
80709
80710
80711
80712
80713
80714
80715
80716
80717
80718
80719
80720
80721
80722
80723
80724
80725
80726
80727
80728
80729
80730
80731
80732
80733
80734
80735
80736
80737
80738
80739
80740
80741
80742
80743
80744
80745
80746
80747
80748
80749
80750
80751
80752
80753
80754
80755
80756
80757
80758
80759
80760
80761
80762
80763
80764
80765
80766
80767
80768
80769
80770
80771
80772
80773
80774
80775
80776
80777
80778
80779
80780
80781
80782
80783
80784
80785
80786
80787
80788
80789
80790
80791
80792
80793
80794
80795
80796
80797
80798
80799
80800
80801
80802
80803
80804
80805
80806
80807
80808
80809
80810
80811
80812
80813
80814
80815
80816
80817
80818
80819
80820
80821
80822
80823
80824
80825
80826
80827
80828
80829
80830
80831
80832
80833
80834
80835
80836
80837
80838
80839
80840
80841
80842
80843
80844
80845
80846
80847
80848
80849
80850
80851
80852
80853
80854
80855
80856
80857
80858
80859
80860
80861
80862
80863
80864
80865
80866
80867
80868
80869
80870
80871
80872
80873
80874
80875
80876
80877
80878
80879
80880
80881
80882
80883
80884
80885
80886
80887
80888
80889
80890
80891
80892
80893
80894
80895
80896
80897
80898
80899
80900
80901
80902
80903
80904
80905
80906
80907
80908
80909
80910
80911
80912
80913
80914
80915
80916
80917
80918
80919
80920
80921
80922
80923
80924
80925
80926
80927
80928
80929
80930
80931
80932
80933
80934
80935
80936
80937
80938
80939
80940
80941
80942
80943
80944
80945
80946
80947
80948
80949
80950
80951
80952
80953
80954
80955
80956
80957
80958
80959
80960
80961
80962
80963
80964
80965
80966
80967
80968
80969
80970
80971
80972
80973
80974
80975
80976
80977
80978
80979
80980
80981
80982
80983
80984
80985
80986
80987
80988
80989
80990
80991
80992
80993
80994
80995
80996
80997
80998
80999
81000
81001
81002
81003
81004
81005
81006
81007
81008
81009
81010
81011
81012
81013
81014
81015
81016
81017
81018
81019
81020
81021
81022
81023
81024
81025
81026
81027
81028
81029
81030
81031
81032
81033
81034
81035
81036
81037
81038
81039
81040
81041
81042
81043
81044
81045
81046
81047
81048
81049
81050
81051
81052
81053
81054
81055
81056
81057
81058
81059
81060
81061
81062
81063
81064
81065
81066
81067
81068
81069
81070
81071
81072
81073
81074
81075
81076
81077
81078
81079
81080
81081
81082
81083
81084
81085
81086
81087
81088
81089
81090
81091
81092
81093
81094
81095
81096
81097
81098
81099
81100
81101
81102
81103
81104
81105
81106
81107
81108
81109
81110
81111
81112
81113
81114
81115
81116
81117
81118
81119
81120
81121
81122
81123
81124
81125
81126
81127
81128
81129
81130
81131
81132
81133
81134
81135
81136
81137
81138
81139
81140
81141
81142
81143
81144
81145
81146
81147
81148
81149
81150
81151
81152
81153
81154
81155
81156
81157
81158
81159
81160
81161
81162
81163
81164
81165
81166
81167
81168
81169
81170
81171
81172
81173
81174
81175
81176
81177
81178
81179
81180
81181
81182
81183
81184
81185
81186
81187
81188
81189
81190
81191
81192
81193
81194
81195
81196
81197
81198
81199
81200
81201
81202
81203
81204
81205
81206
81207
81208
81209
81210
81211
81212
81213
81214
81215
81216
81217
81218
81219
81220
81221
81222
81223
81224
81225
81226
81227
81228
81229
81230
81231
81232
81233
81234
81235
81236
81237
81238
81239
81240
81241
81242
81243
81244
81245
81246
81247
81248
81249
81250
81251
81252
81253
81254
81255
81256
81257
81258
81259
81260
81261
81262
81263
81264
81265
81266
81267
81268
81269
81270
81271
81272
81273
81274
81275
81276
81277
81278
81279
81280
81281
81282
81283
81284
81285
81286
81287
81288
81289
81290
81291
81292
81293
81294
81295
81296
81297
81298
81299
81300
81301
81302
81303
81304
81305
81306
81307
81308
81309
81310
81311
81312
81313
81314
81315
81316
81317
81318
81319
81320
81321
81322
81323
81324
81325
81326
81327
81328
81329
81330
81331
81332
81333
81334
81335
81336
81337
81338
81339
81340
81341
81342
81343
81344
81345
81346
81347
81348
81349
81350
81351
81352
81353
81354
81355
81356
81357
81358
81359
81360
81361
81362
81363
81364
81365
81366
81367
81368
81369
81370
81371
81372
81373
81374
81375
81376
81377
81378
81379
81380
81381
81382
81383
81384
81385
81386
81387
81388
81389
81390
81391
81392
81393
81394
81395
81396
81397
81398
81399
81400
81401
81402
81403
81404
81405
81406
81407
81408
81409
81410
81411
81412
81413
81414
81415
81416
81417
81418
81419
81420
81421
81422
81423
81424
81425
81426
81427
81428
81429
81430
81431
81432
81433
81434
81435
81436
81437
81438
81439
81440
81441
81442
81443
81444
81445
81446
81447
81448
81449
81450
81451
81452
81453
81454
81455
81456
81457
81458
81459
81460
81461
81462
81463
81464
81465
81466
81467
81468
81469
81470
81471
81472
81473
81474
81475
81476
81477
81478
81479
81480
81481
81482
81483
81484
81485
81486
81487
81488
81489
81490
81491
81492
81493
81494
81495
81496
81497
81498
81499
81500
81501
81502
81503
81504
81505
81506
81507
81508
81509
81510
81511
81512
81513
81514
81515
81516
81517
81518
81519
81520
81521
81522
81523
81524
81525
81526
81527
81528
81529
81530
81531
81532
81533
81534
81535
81536
81537
81538
81539
81540
81541
81542
81543
81544
81545
81546
81547
81548
81549
81550
81551
81552
81553
81554
81555
81556
81557
81558
81559
81560
81561
81562
81563
81564
81565
81566
81567
81568
81569
81570
81571
81572
81573
81574
81575
81576
81577
81578
81579
81580
81581
81582
81583
81584
81585
81586
81587
81588
81589
81590
81591
81592
81593
81594
81595
81596
81597
81598
81599
81600
81601
81602
81603
81604
81605
81606
81607
81608
81609
81610
81611
81612
81613
81614
81615
81616
81617
81618
81619
81620
81621
81622
81623
81624
81625
81626
81627
81628
81629
81630
81631
81632
81633
81634
81635
81636
81637
81638
81639
81640
81641
81642
81643
81644
81645
81646
81647
81648
81649
81650
81651
81652
81653
81654
81655
81656
81657
81658
81659
81660
81661
81662
81663
81664
81665
81666
81667
81668
81669
81670
81671
81672
81673
81674
81675
81676
81677
81678
81679
81680
81681
81682
81683
81684
81685
81686
81687
81688
81689
81690
81691
81692
81693
81694
81695
81696
81697
81698
81699
81700
81701
81702
81703
81704
81705
81706
81707
81708
81709
81710
81711
81712
81713
81714
81715
81716
81717
81718
81719
81720
81721
81722
81723
81724
81725
81726
81727
81728
81729
81730
81731
81732
81733
81734
81735
81736
81737
81738
81739
81740
81741
81742
81743
81744
81745
81746
81747
81748
81749
81750
81751
81752
81753
81754
81755
81756
81757
81758
81759
81760
81761
81762
81763
81764
81765
81766
81767
81768
81769
81770
81771
81772
81773
81774
81775
81776
81777
81778
81779
81780
81781
81782
81783
81784
81785
81786
81787
81788
81789
81790
81791
81792
81793
81794
81795
81796
81797
81798
81799
81800
81801
81802
81803
81804
81805
81806
81807
81808
81809
81810
81811
81812
81813
81814
81815
81816
81817
81818
81819
81820
81821
81822
81823
81824
81825
81826
81827
81828
81829
81830
81831
81832
81833
81834
81835
81836
81837
81838
81839
81840
81841
81842
81843
81844
81845
81846
81847
81848
81849
81850
81851
81852
81853
81854
81855
81856
81857
81858
81859
81860
81861
81862
81863
81864
81865
81866
81867
81868
81869
81870
81871
81872
81873
81874
81875
81876
81877
81878
81879
81880
81881
81882
81883
81884
81885
81886
81887
81888
81889
81890
81891
81892
81893
81894
81895
81896
81897
81898
81899
81900
81901
81902
81903
81904
81905
81906
81907
81908
81909
81910
81911
81912
81913
81914
81915
81916
81917
81918
81919
81920
81921
81922
81923
81924
81925
81926
81927
81928
81929
81930
81931
81932
81933
81934
81935
81936
81937
81938
81939
81940
81941
81942
81943
81944
81945
81946
81947
81948
81949
81950
81951
81952
81953
81954
81955
81956
81957
81958
81959
81960
81961
81962
81963
81964
81965
81966
81967
81968
81969
81970
81971
81972
81973
81974
81975
81976
81977
81978
81979
81980
81981
81982
81983
81984
81985
81986
81987
81988
81989
81990
81991
81992
81993
81994
81995
81996
81997
81998
81999
82000
82001
82002
82003
82004
82005
82006
82007
82008
82009
82010
82011
82012
82013
82014
82015
82016
82017
82018
82019
82020
82021
82022
82023
82024
82025
82026
82027
82028
82029
82030
82031
82032
82033
82034
82035
82036
82037
82038
82039
82040
82041
82042
82043
82044
82045
82046
82047
82048
82049
82050
82051
82052
82053
82054
82055
82056
82057
82058
82059
82060
82061
82062
82063
82064
82065
82066
82067
82068
82069
82070
82071
82072
82073
82074
82075
82076
82077
82078
82079
82080
82081
82082
82083
82084
82085
82086
82087
82088
82089
82090
82091
82092
82093
82094
82095
82096
82097
82098
82099
82100
82101
82102
82103
82104
82105
82106
82107
82108
82109
82110
82111
82112
82113
82114
82115
82116
82117
82118
82119
82120
82121
82122
82123
82124
82125
82126
82127
82128
82129
82130
82131
82132
82133
82134
82135
82136
82137
82138
82139
82140
82141
82142
82143
82144
82145
82146
82147
82148
82149
82150
82151
82152
82153
82154
82155
82156
82157
82158
82159
82160
82161
82162
82163
82164
82165
82166
82167
82168
82169
82170
82171
82172
82173
82174
82175
82176
82177
82178
82179
82180
82181
82182
82183
82184
82185
82186
82187
82188
82189
82190
82191
82192
82193
82194
82195
82196
82197
82198
82199
82200
82201
82202
82203
82204
82205
82206
82207
82208
82209
82210
82211
82212
82213
82214
82215
82216
82217
82218
82219
82220
82221
82222
82223
82224
82225
82226
82227
82228
82229
82230
82231
82232
82233
82234
82235
82236
82237
82238
82239
82240
82241
82242
82243
82244
82245
82246
82247
82248
82249
82250
82251
82252
82253
82254
82255
82256
82257
82258
82259
82260
82261
82262
82263
82264
82265
82266
82267
82268
82269
82270
82271
82272
82273
82274
82275
82276
82277
82278
82279
82280
82281
82282
82283
82284
82285
82286
82287
82288
82289
82290
82291
82292
82293
82294
82295
82296
82297
82298
82299
82300
82301
82302
82303
82304
82305
82306
82307
82308
82309
82310
82311
82312
82313
82314
82315
82316
82317
82318
82319
82320
82321
82322
82323
82324
82325
82326
82327
82328
82329
82330
82331
82332
82333
82334
82335
82336
82337
82338
82339
82340
82341
82342
82343
82344
82345
82346
82347
82348
82349
82350
82351
82352
82353
82354
82355
82356
82357
82358
82359
82360
82361
82362
82363
82364
82365
82366
82367
82368
82369
82370
82371
82372
82373
82374
82375
82376
82377
82378
82379
82380
82381
82382
82383
82384
82385
82386
82387
82388
82389
82390
82391
82392
82393
82394
82395
82396
82397
82398
82399
82400
82401
82402
82403
82404
82405
82406
82407
82408
82409
82410
82411
82412
82413
82414
82415
82416
82417
82418
82419
82420
82421
82422
82423
82424
82425
82426
82427
82428
82429
82430
82431
82432
82433
82434
82435
82436
82437
82438
82439
82440
82441
82442
82443
82444
82445
82446
82447
82448
82449
82450
82451
82452
82453
82454
82455
82456
82457
82458
82459
82460
82461
82462
82463
82464
82465
82466
82467
82468
82469
82470
82471
82472
82473
82474
82475
82476
82477
82478
82479
82480
82481
82482
82483
82484
82485
82486
82487
82488
82489
82490
82491
82492
82493
82494
82495
82496
82497
82498
82499
82500
82501
82502
82503
82504
82505
82506
82507
82508
82509
82510
82511
82512
82513
82514
82515
82516
82517
82518
82519
82520
82521
82522
82523
82524
82525
82526
82527
82528
82529
82530
82531
82532
82533
82534
82535
82536
82537
82538
82539
82540
82541
82542
82543
82544
82545
82546
82547
82548
82549
82550
82551
82552
82553
82554
82555
82556
82557
82558
82559
82560
82561
82562
82563
82564
82565
82566
82567
82568
82569
82570
82571
82572
82573
82574
82575
82576
82577
82578
82579
82580
82581
82582
82583
82584
82585
82586
82587
82588
82589
82590
82591
82592
82593
82594
82595
82596
82597
82598
82599
82600
82601
82602
82603
82604
82605
82606
82607
82608
82609
82610
82611
82612
82613
82614
82615
82616
82617
82618
82619
82620
82621
82622
82623
82624
82625
82626
82627
82628
82629
82630
82631
82632
82633
82634
82635
82636
82637
82638
82639
82640
82641
82642
82643
82644
82645
82646
82647
82648
82649
82650
82651
82652
82653
82654
82655
82656
82657
82658
82659
82660
82661
82662
82663
82664
82665
82666
82667
82668
82669
82670
82671
82672
82673
82674
82675
82676
82677
82678
82679
82680
82681
82682
82683
82684
82685
82686
82687
82688
82689
82690
82691
82692
82693
82694
82695
82696
82697
82698
82699
82700
82701
82702
82703
82704
82705
82706
82707
82708
82709
82710
82711
82712
82713
82714
82715
82716
82717
82718
82719
82720
82721
82722
82723
82724
82725
82726
82727
82728
82729
82730
82731
82732
82733
82734
82735
82736
82737
82738
82739
82740
82741
82742
82743
82744
82745
82746
82747
82748
82749
82750
82751
82752
82753
82754
82755
82756
82757
82758
82759
82760
82761
82762
82763
82764
82765
82766
82767
82768
82769
82770
82771
82772
82773
82774
82775
82776
82777
82778
82779
82780
82781
82782
82783
82784
82785
82786
82787
82788
82789
82790
82791
82792
82793
82794
82795
82796
82797
82798
82799
82800
82801
82802
82803
82804
82805
82806
82807
82808
82809
82810
82811
82812
82813
82814
82815
82816
82817
82818
82819
82820
82821
82822
82823
82824
82825
82826
82827
82828
82829
82830
82831
82832
82833
82834
82835
82836
82837
82838
82839
82840
82841
82842
82843
82844
82845
82846
82847
82848
82849
82850
82851
82852
82853
82854
82855
82856
82857
82858
82859
82860
82861
82862
82863
82864
82865
82866
82867
82868
82869
82870
82871
82872
82873
82874
82875
82876
82877
82878
82879
82880
82881
82882
82883
82884
82885
82886
82887
82888
82889
82890
82891
82892
82893
82894
82895
82896
82897
82898
82899
82900
82901
82902
82903
82904
82905
82906
82907
82908
82909
82910
82911
82912
82913
82914
82915
82916
82917
82918
82919
82920
82921
82922
82923
82924
82925
82926
82927
82928
82929
82930
82931
82932
82933
82934
82935
82936
82937
82938
82939
82940
82941
82942
82943
82944
82945
82946
82947
82948
82949
82950
82951
82952
82953
82954
82955
82956
82957
82958
82959
82960
82961
82962
82963
82964
82965
82966
82967
82968
82969
82970
82971
82972
82973
82974
82975
82976
82977
82978
82979
82980
82981
82982
82983
82984
82985
82986
82987
82988
82989
82990
82991
82992
82993
82994
82995
82996
82997
82998
82999
83000
83001
83002
83003
83004
83005
83006
83007
83008
83009
83010
83011
83012
83013
83014
83015
83016
83017
83018
83019
83020
83021
83022
83023
83024
83025
83026
83027
83028
83029
83030
83031
83032
83033
83034
83035
83036
83037
83038
83039
83040
83041
83042
83043
83044
83045
83046
83047
83048
83049
83050
83051
83052
83053
83054
83055
83056
83057
83058
83059
83060
83061
83062
83063
83064
83065
83066
83067
83068
83069
83070
83071
83072
83073
83074
83075
83076
83077
83078
83079
83080
83081
83082
83083
83084
83085
83086
83087
83088
83089
83090
83091
83092
83093
83094
83095
83096
83097
83098
83099
83100
83101
83102
83103
83104
83105
83106
83107
83108
83109
83110
83111
83112
83113
83114
83115
83116
83117
83118
83119
83120
83121
83122
83123
83124
83125
83126
83127
83128
83129
83130
83131
83132
83133
83134
83135
83136
83137
83138
83139
83140
83141
83142
83143
83144
83145
83146
83147
83148
83149
83150
83151
83152
83153
83154
83155
83156
83157
83158
83159
83160
83161
83162
83163
83164
83165
83166
83167
83168
83169
83170
83171
83172
83173
83174
83175
83176
83177
83178
83179
83180
83181
83182
83183
83184
83185
83186
83187
83188
83189
83190
83191
83192
83193
83194
83195
83196
83197
83198
83199
83200
83201
83202
83203
83204
83205
83206
83207
83208
83209
83210
83211
83212
83213
83214
83215
83216
83217
83218
83219
83220
83221
83222
83223
83224
83225
83226
83227
83228
83229
83230
83231
83232
83233
83234
83235
83236
83237
83238
83239
83240
83241
83242
83243
83244
83245
83246
83247
83248
83249
83250
83251
83252
83253
83254
83255
83256
83257
83258
83259
83260
83261
83262
83263
83264
83265
83266
83267
83268
83269
83270
83271
83272
83273
83274
83275
83276
83277
83278
83279
83280
83281
83282
83283
83284
83285
83286
83287
83288
83289
83290
83291
83292
83293
83294
83295
83296
83297
83298
83299
83300
83301
83302
83303
83304
83305
83306
83307
83308
83309
83310
83311
83312
83313
83314
83315
83316
83317
83318
83319
83320
83321
83322
83323
83324
83325
83326
83327
83328
83329
83330
83331
83332
83333
83334
83335
83336
83337
83338
83339
83340
83341
83342
83343
83344
83345
83346
83347
83348
83349
83350
83351
83352
83353
83354
83355
83356
83357
83358
83359
83360
83361
83362
83363
83364
83365
83366
83367
83368
83369
83370
83371
83372
83373
83374
83375
83376
83377
83378
83379
83380
83381
83382
83383
83384
83385
83386
83387
83388
83389
83390
83391
83392
83393
83394
83395
83396
83397
83398
83399
83400
83401
83402
83403
83404
83405
83406
83407
83408
83409
83410
83411
83412
83413
83414
83415
83416
83417
83418
83419
83420
83421
83422
83423
83424
83425
83426
83427
83428
83429
83430
83431
83432
83433
83434
83435
83436
83437
83438
83439
83440
83441
83442
83443
83444
83445
83446
83447
83448
83449
83450
83451
83452
83453
83454
83455
83456
83457
83458
83459
83460
83461
83462
83463
83464
83465
83466
83467
83468
83469
83470
83471
83472
83473
83474
83475
83476
83477
83478
83479
83480
83481
83482
83483
83484
83485
83486
83487
83488
83489
83490
83491
83492
83493
83494
83495
83496
83497
83498
83499
83500
83501
83502
83503
83504
83505
83506
83507
83508
83509
83510
83511
83512
83513
83514
83515
83516
83517
83518
83519
83520
83521
83522
83523
83524
83525
83526
83527
83528
83529
83530
83531
83532
83533
83534
83535
83536
83537
83538
83539
83540
83541
83542
83543
83544
83545
83546
83547
83548
83549
83550
83551
83552
83553
83554
83555
83556
83557
83558
83559
83560
83561
83562
83563
83564
83565
83566
83567
83568
83569
83570
83571
83572
83573
83574
83575
83576
83577
83578
83579
83580
83581
83582
83583
83584
83585
83586
83587
83588
83589
83590
83591
83592
83593
83594
83595
83596
83597
83598
83599
83600
83601
83602
83603
83604
83605
83606
83607
83608
83609
83610
83611
83612
83613
83614
83615
83616
83617
83618
83619
83620
83621
83622
83623
83624
83625
83626
83627
83628
83629
83630
83631
83632
83633
83634
83635
83636
83637
83638
83639
83640
83641
83642
83643
83644
83645
83646
83647
83648
83649
83650
83651
83652
83653
83654
83655
83656
83657
83658
83659
83660
83661
83662
83663
83664
83665
83666
83667
83668
83669
83670
83671
83672
83673
83674
83675
83676
83677
83678
83679
83680
83681
83682
83683
83684
83685
83686
83687
83688
83689
83690
83691
83692
83693
83694
83695
83696
83697
83698
83699
83700
83701
83702
83703
83704
83705
83706
83707
83708
83709
83710
83711
83712
83713
83714
83715
83716
83717
83718
83719
83720
83721
83722
83723
83724
83725
83726
83727
83728
83729
83730
83731
83732
83733
83734
83735
83736
83737
83738
83739
83740
83741
83742
83743
83744
83745
83746
83747
83748
83749
83750
83751
83752
83753
83754
83755
83756
83757
83758
83759
83760
83761
83762
83763
83764
83765
83766
83767
83768
83769
83770
83771
83772
83773
83774
83775
83776
83777
83778
83779
83780
83781
83782
83783
83784
83785
83786
83787
83788
83789
83790
83791
83792
83793
83794
83795
83796
83797
83798
83799
83800
83801
83802
83803
83804
83805
83806
83807
83808
83809
83810
83811
83812
83813
83814
83815
83816
83817
83818
83819
83820
83821
83822
83823
83824
83825
83826
83827
83828
83829
83830
83831
83832
83833
83834
83835
83836
83837
83838
83839
83840
83841
83842
83843
83844
83845
83846
83847
83848
83849
83850
83851
83852
83853
83854
83855
83856
83857
83858
83859
83860
83861
83862
83863
83864
83865
83866
83867
83868
83869
83870
83871
83872
83873
83874
83875
83876
83877
83878
83879
83880
83881
83882
83883
83884
83885
83886
83887
83888
83889
83890
83891
83892
83893
83894
83895
83896
83897
83898
83899
83900
83901
83902
83903
83904
83905
83906
83907
83908
83909
83910
83911
83912
83913
83914
83915
83916
83917
83918
83919
83920
83921
83922
83923
83924
83925
83926
83927
83928
83929
83930
83931
83932
83933
83934
83935
83936
83937
83938
83939
83940
83941
83942
83943
83944
83945
83946
83947
83948
83949
83950
83951
83952
83953
83954
83955
83956
83957
83958
83959
83960
83961
83962
83963
83964
83965
83966
83967
83968
83969
83970
83971
83972
83973
83974
83975
83976
83977
83978
83979
83980
83981
83982
83983
83984
83985
83986
83987
83988
83989
83990
83991
83992
83993
83994
83995
83996
83997
83998
83999
84000
84001
84002
84003
84004
84005
84006
84007
84008
84009
84010
84011
84012
84013
84014
84015
84016
84017
84018
84019
84020
84021
84022
84023
84024
84025
84026
84027
84028
84029
84030
84031
84032
84033
84034
84035
84036
84037
84038
84039
84040
84041
84042
84043
84044
84045
84046
84047
84048
84049
84050
84051
84052
84053
84054
84055
84056
84057
84058
84059
84060
84061
84062
84063
84064
84065
84066
84067
84068
84069
84070
84071
84072
84073
84074
84075
84076
84077
84078
84079
84080
84081
84082
84083
84084
84085
84086
84087
84088
84089
84090
84091
84092
84093
84094
84095
84096
84097
84098
84099
84100
84101
84102
84103
84104
84105
84106
84107
84108
84109
84110
84111
84112
84113
84114
84115
84116
84117
84118
84119
84120
84121
84122
84123
84124
84125
84126
84127
84128
84129
84130
84131
84132
84133
84134
84135
84136
84137
84138
84139
84140
84141
84142
84143
84144
84145
84146
84147
84148
84149
84150
84151
84152
84153
84154
84155
84156
84157
84158
84159
84160
84161
84162
84163
84164
84165
84166
84167
84168
84169
84170
84171
84172
84173
84174
84175
84176
84177
84178
84179
84180
84181
84182
84183
84184
84185
84186
84187
84188
84189
84190
84191
84192
84193
84194
84195
84196
84197
84198
84199
84200
84201
84202
84203
84204
84205
84206
84207
84208
84209
84210
84211
84212
84213
84214
84215
84216
84217
84218
84219
84220
84221
84222
84223
84224
84225
84226
84227
84228
84229
84230
84231
84232
84233
84234
84235
84236
84237
84238
84239
84240
84241
84242
84243
84244
84245
84246
84247
84248
84249
84250
84251
84252
84253
84254
84255
84256
84257
84258
84259
84260
84261
84262
84263
84264
84265
84266
84267
84268
84269
84270
84271
84272
84273
84274
84275
84276
84277
84278
84279
84280
84281
84282
84283
84284
84285
84286
84287
84288
84289
84290
84291
84292
84293
84294
84295
84296
84297
84298
84299
84300
84301
84302
84303
84304
84305
84306
84307
84308
84309
84310
84311
84312
84313
84314
84315
84316
84317
84318
84319
84320
84321
84322
84323
84324
84325
84326
84327
84328
84329
84330
84331
84332
84333
84334
84335
84336
84337
84338
84339
84340
84341
84342
84343
84344
84345
84346
84347
84348
84349
84350
84351
84352
84353
84354
84355
84356
84357
84358
84359
84360
84361
84362
84363
84364
84365
84366
84367
84368
84369
84370
84371
84372
84373
84374
84375
84376
84377
84378
84379
84380
84381
84382
84383
84384
84385
84386
84387
84388
84389
84390
84391
84392
84393
84394
84395
84396
84397
84398
84399
84400
84401
84402
84403
84404
84405
84406
84407
84408
84409
84410
84411
84412
84413
84414
84415
84416
84417
84418
84419
84420
84421
84422
84423
84424
84425
84426
84427
84428
84429
84430
84431
84432
84433
84434
84435
84436
84437
84438
84439
84440
84441
84442
84443
84444
84445
84446
84447
84448
84449
84450
84451
84452
84453
84454
84455
84456
84457
84458
84459
84460
84461
84462
84463
84464
84465
84466
84467
84468
84469
84470
84471
84472
84473
84474
84475
84476
84477
84478
84479
84480
84481
84482
84483
84484
84485
84486
84487
84488
84489
84490
84491
84492
84493
84494
84495
84496
84497
84498
84499
84500
84501
84502
84503
84504
84505
84506
84507
84508
84509
84510
84511
84512
84513
84514
84515
84516
84517
84518
84519
84520
84521
84522
84523
84524
84525
84526
84527
84528
84529
84530
84531
84532
84533
84534
84535
84536
84537
84538
84539
84540
84541
84542
84543
84544
84545
84546
84547
84548
84549
84550
84551
84552
84553
84554
84555
84556
84557
84558
84559
84560
84561
84562
84563
84564
84565
84566
84567
84568
84569
84570
84571
84572
84573
84574
84575
84576
84577
84578
84579
84580
84581
84582
84583
84584
84585
84586
84587
84588
84589
84590
84591
84592
84593
84594
84595
84596
84597
84598
84599
84600
84601
84602
84603
84604
84605
84606
84607
84608
84609
84610
84611
84612
84613
84614
84615
84616
84617
84618
84619
84620
84621
84622
84623
84624
84625
84626
84627
84628
84629
84630
84631
84632
84633
84634
84635
84636
84637
84638
84639
84640
84641
84642
84643
84644
84645
84646
84647
84648
84649
84650
84651
84652
84653
84654
84655
84656
84657
84658
84659
84660
84661
84662
84663
84664
84665
84666
84667
84668
84669
84670
84671
84672
84673
84674
84675
84676
84677
84678
84679
84680
84681
84682
84683
84684
84685
84686
84687
84688
84689
84690
84691
84692
84693
84694
84695
84696
84697
84698
84699
84700
84701
84702
84703
84704
84705
84706
84707
84708
84709
84710
84711
84712
84713
84714
84715
84716
84717
84718
84719
84720
84721
84722
84723
84724
84725
84726
84727
84728
84729
84730
84731
84732
84733
84734
84735
84736
84737
84738
84739
84740
84741
84742
84743
84744
84745
84746
84747
84748
84749
84750
84751
84752
84753
84754
84755
84756
84757
84758
84759
84760
84761
84762
84763
84764
84765
84766
84767
84768
84769
84770
84771
84772
84773
84774
84775
84776
84777
84778
84779
84780
84781
84782
84783
84784
84785
84786
84787
84788
84789
84790
84791
84792
84793
84794
84795
84796
84797
84798
84799
84800
84801
84802
84803
84804
84805
84806
84807
84808
84809
84810
84811
84812
84813
84814
84815
84816
84817
84818
84819
84820
84821
84822
84823
84824
84825
84826
84827
84828
84829
84830
84831
84832
84833
84834
84835
84836
84837
84838
84839
84840
84841
84842
84843
84844
84845
84846
84847
84848
84849
84850
84851
84852
84853
84854
84855
84856
84857
84858
84859
84860
84861
84862
84863
84864
84865
84866
84867
84868
84869
84870
84871
84872
84873
84874
84875
84876
84877
84878
84879
84880
84881
84882
84883
84884
84885
84886
84887
84888
84889
84890
84891
84892
84893
84894
84895
84896
84897
84898
84899
84900
84901
84902
84903
84904
84905
84906
84907
84908
84909
84910
84911
84912
84913
84914
84915
84916
84917
84918
84919
84920
84921
84922
84923
84924
84925
84926
84927
84928
84929
84930
84931
84932
84933
84934
84935
84936
84937
84938
84939
84940
84941
84942
84943
84944
84945
84946
84947
84948
84949
84950
84951
84952
84953
84954
84955
84956
84957
84958
84959
84960
84961
84962
84963
84964
84965
84966
84967
84968
84969
84970
84971
84972
84973
84974
84975
84976
84977
84978
84979
84980
84981
84982
84983
84984
84985
84986
84987
84988
84989
84990
84991
84992
84993
84994
84995
84996
84997
84998
84999
85000
85001
85002
85003
85004
85005
85006
85007
85008
85009
85010
85011
85012
85013
85014
85015
85016
85017
85018
85019
85020
85021
85022
85023
85024
85025
85026
85027
85028
85029
85030
85031
85032
85033
85034
85035
85036
85037
85038
85039
85040
85041
85042
85043
85044
85045
85046
85047
85048
85049
85050
85051
85052
85053
85054
85055
85056
85057
85058
85059
85060
85061
85062
85063
85064
85065
85066
85067
85068
85069
85070
85071
85072
85073
85074
85075
85076
85077
85078
85079
85080
85081
85082
85083
85084
85085
85086
85087
85088
85089
85090
85091
85092
85093
85094
85095
85096
85097
85098
85099
85100
85101
85102
85103
85104
85105
85106
85107
85108
85109
85110
85111
85112
85113
85114
85115
85116
85117
85118
85119
85120
85121
85122
85123
85124
85125
85126
85127
85128
85129
85130
85131
85132
85133
85134
85135
85136
85137
85138
85139
85140
85141
85142
85143
85144
85145
85146
85147
85148
85149
85150
85151
85152
85153
85154
85155
85156
85157
85158
85159
85160
85161
85162
85163
85164
85165
85166
85167
85168
85169
85170
85171
85172
85173
85174
85175
85176
85177
85178
85179
85180
85181
85182
85183
85184
85185
85186
85187
85188
85189
85190
85191
85192
85193
85194
85195
85196
85197
85198
85199
85200
85201
85202
85203
85204
85205
85206
85207
85208
85209
85210
85211
85212
85213
85214
85215
85216
85217
85218
85219
85220
85221
85222
85223
85224
85225
85226
85227
85228
85229
85230
85231
85232
85233
85234
85235
85236
85237
85238
85239
85240
85241
85242
85243
85244
85245
85246
85247
85248
85249
85250
85251
85252
85253
85254
85255
85256
85257
85258
85259
85260
85261
85262
85263
85264
85265
85266
85267
85268
85269
85270
85271
85272
85273
85274
85275
85276
85277
85278
85279
85280
85281
85282
85283
85284
85285
85286
85287
85288
85289
85290
85291
85292
85293
85294
85295
85296
85297
85298
85299
85300
85301
85302
85303
85304
85305
85306
85307
85308
85309
85310
85311
85312
85313
85314
85315
85316
85317
85318
85319
85320
85321
85322
85323
85324
85325
85326
85327
85328
85329
85330
85331
85332
85333
85334
85335
85336
85337
85338
85339
85340
85341
85342
85343
85344
85345
85346
85347
85348
85349
85350
85351
85352
85353
85354
85355
85356
85357
85358
85359
85360
85361
85362
85363
85364
85365
85366
85367
85368
85369
85370
85371
85372
85373
85374
85375
85376
85377
85378
85379
85380
85381
85382
85383
85384
85385
85386
85387
85388
85389
85390
85391
85392
85393
85394
85395
85396
85397
85398
85399
85400
85401
85402
85403
85404
85405
85406
85407
85408
85409
85410
85411
85412
85413
85414
85415
85416
85417
85418
85419
85420
85421
85422
85423
85424
85425
85426
85427
85428
85429
85430
85431
85432
85433
85434
85435
85436
85437
85438
85439
85440
85441
85442
85443
85444
85445
85446
85447
85448
85449
85450
85451
85452
85453
85454
85455
85456
85457
85458
85459
85460
85461
85462
85463
85464
85465
85466
85467
85468
85469
85470
85471
85472
85473
85474
85475
85476
85477
85478
85479
85480
85481
85482
85483
85484
85485
85486
85487
85488
85489
85490
85491
85492
85493
85494
85495
85496
85497
85498
85499
85500
85501
85502
85503
85504
85505
85506
85507
85508
85509
85510
85511
85512
85513
85514
85515
85516
85517
85518
85519
85520
85521
85522
85523
85524
85525
85526
85527
85528
85529
85530
85531
85532
85533
85534
85535
85536
85537
85538
85539
85540
85541
85542
85543
85544
85545
85546
85547
85548
85549
85550
85551
85552
85553
85554
85555
85556
85557
85558
85559
85560
85561
85562
85563
85564
85565
85566
85567
85568
85569
85570
85571
85572
85573
85574
85575
85576
85577
85578
85579
85580
85581
85582
85583
85584
85585
85586
85587
85588
85589
85590
85591
85592
85593
85594
85595
85596
85597
85598
85599
85600
85601
85602
85603
85604
85605
85606
85607
85608
85609
85610
85611
85612
85613
85614
85615
85616
85617
85618
85619
85620
85621
85622
85623
85624
85625
85626
85627
85628
85629
85630
85631
85632
85633
85634
85635
85636
85637
85638
85639
85640
85641
85642
85643
85644
85645
85646
85647
85648
85649
85650
85651
85652
85653
85654
85655
85656
85657
85658
85659
85660
85661
85662
85663
85664
85665
85666
85667
85668
85669
85670
85671
85672
85673
85674
85675
85676
85677
85678
85679
85680
85681
85682
85683
85684
85685
85686
85687
85688
85689
85690
85691
85692
85693
85694
85695
85696
85697
85698
85699
85700
85701
85702
85703
85704
85705
85706
85707
85708
85709
85710
85711
85712
85713
85714
85715
85716
85717
85718
85719
85720
85721
85722
85723
85724
85725
85726
85727
85728
85729
85730
85731
85732
85733
85734
85735
85736
85737
85738
85739
85740
85741
85742
85743
85744
85745
85746
85747
85748
85749
85750
85751
85752
85753
85754
85755
85756
85757
85758
85759
85760
85761
85762
85763
85764
85765
85766
85767
85768
85769
85770
85771
85772
85773
85774
85775
85776
85777
85778
85779
85780
85781
85782
85783
85784
85785
85786
85787
85788
85789
85790
85791
85792
85793
85794
85795
85796
85797
85798
85799
85800
85801
85802
85803
85804
85805
85806
85807
85808
85809
85810
85811
85812
85813
85814
85815
85816
85817
85818
85819
85820
85821
85822
85823
85824
85825
85826
85827
85828
85829
85830
85831
85832
85833
85834
85835
85836
85837
85838
85839
85840
85841
85842
85843
85844
85845
85846
85847
85848
85849
85850
85851
85852
85853
85854
85855
85856
85857
85858
85859
85860
85861
85862
85863
85864
85865
85866
85867
85868
85869
85870
85871
85872
85873
85874
85875
85876
85877
85878
85879
85880
85881
85882
85883
85884
85885
85886
85887
85888
85889
85890
85891
85892
85893
85894
85895
85896
85897
85898
85899
85900
85901
85902
85903
85904
85905
85906
85907
85908
85909
85910
85911
85912
85913
85914
85915
85916
85917
85918
85919
85920
85921
85922
85923
85924
85925
85926
85927
85928
85929
85930
85931
85932
85933
85934
85935
85936
85937
85938
85939
85940
85941
85942
85943
85944
85945
85946
85947
85948
85949
85950
85951
85952
85953
85954
85955
85956
85957
85958
85959
85960
85961
85962
85963
85964
85965
85966
85967
85968
85969
85970
85971
85972
85973
85974
85975
85976
85977
85978
85979
85980
85981
85982
85983
85984
85985
85986
85987
85988
85989
85990
85991
85992
85993
85994
85995
85996
85997
85998
85999
86000
86001
86002
86003
86004
86005
86006
86007
86008
86009
86010
86011
86012
86013
86014
86015
86016
86017
86018
86019
86020
86021
86022
86023
86024
86025
86026
86027
86028
86029
86030
86031
86032
86033
86034
86035
86036
86037
86038
86039
86040
86041
86042
86043
86044
86045
86046
86047
86048
86049
86050
86051
86052
86053
86054
86055
86056
86057
86058
86059
86060
86061
86062
86063
86064
86065
86066
86067
86068
86069
86070
86071
86072
86073
86074
86075
86076
86077
86078
86079
86080
86081
86082
86083
86084
86085
86086
86087
86088
86089
86090
86091
86092
86093
86094
86095
86096
86097
86098
86099
86100
86101
86102
86103
86104
86105
86106
86107
86108
86109
86110
86111
86112
86113
86114
86115
86116
86117
86118
86119
86120
86121
86122
86123
86124
86125
86126
86127
86128
86129
86130
86131
86132
86133
86134
86135
86136
86137
86138
86139
86140
86141
86142
86143
86144
86145
86146
86147
86148
86149
86150
86151
86152
86153
86154
86155
86156
86157
86158
86159
86160
86161
86162
86163
86164
86165
86166
86167
86168
86169
86170
86171
86172
86173
86174
86175
86176
86177
86178
86179
86180
86181
86182
86183
86184
86185
86186
86187
86188
86189
86190
86191
86192
86193
86194
86195
86196
86197
86198
86199
86200
86201
86202
86203
86204
86205
86206
86207
86208
86209
86210
86211
86212
86213
86214
86215
86216
86217
86218
86219
86220
86221
86222
86223
86224
86225
86226
86227
86228
86229
86230
86231
86232
86233
86234
86235
86236
86237
86238
86239
86240
86241
86242
86243
86244
86245
86246
86247
86248
86249
86250
86251
86252
86253
86254
86255
86256
86257
86258
86259
86260
86261
86262
86263
86264
86265
86266
86267
86268
86269
86270
86271
86272
86273
86274
86275
86276
86277
86278
86279
86280
86281
86282
86283
86284
86285
86286
86287
86288
86289
86290
86291
86292
86293
86294
86295
86296
86297
86298
86299
86300
86301
86302
86303
86304
86305
86306
86307
86308
86309
86310
86311
86312
86313
86314
86315
86316
86317
86318
86319
86320
86321
86322
86323
86324
86325
86326
86327
86328
86329
86330
86331
86332
86333
86334
86335
86336
86337
86338
86339
86340
86341
86342
86343
86344
86345
86346
86347
86348
86349
86350
86351
86352
86353
86354
86355
86356
86357
86358
86359
86360
86361
86362
86363
86364
86365
86366
86367
86368
86369
86370
86371
86372
86373
86374
86375
86376
86377
86378
86379
86380
86381
86382
86383
86384
86385
86386
86387
86388
86389
86390
86391
86392
86393
86394
86395
86396
86397
86398
86399
86400
86401
86402
86403
86404
86405
86406
86407
86408
86409
86410
86411
86412
86413
86414
86415
86416
86417
86418
86419
86420
86421
86422
86423
86424
86425
86426
86427
86428
86429
86430
86431
86432
86433
86434
86435
86436
86437
86438
86439
86440
86441
86442
86443
86444
86445
86446
86447
86448
86449
86450
86451
86452
86453
86454
86455
86456
86457
86458
86459
86460
86461
86462
86463
86464
86465
86466
86467
86468
86469
86470
86471
86472
86473
86474
86475
86476
86477
86478
86479
86480
86481
86482
86483
86484
86485
86486
86487
86488
86489
86490
86491
86492
86493
86494
86495
86496
86497
86498
86499
86500
86501
86502
86503
86504
86505
86506
86507
86508
86509
86510
86511
86512
86513
86514
86515
86516
86517
86518
86519
86520
86521
86522
86523
86524
86525
86526
86527
86528
86529
86530
86531
86532
86533
86534
86535
86536
86537
86538
86539
86540
86541
86542
86543
86544
86545
86546
86547
86548
86549
86550
86551
86552
86553
86554
86555
86556
86557
86558
86559
86560
86561
86562
86563
86564
86565
86566
86567
86568
86569
86570
86571
86572
86573
86574
86575
86576
86577
86578
86579
86580
86581
86582
86583
86584
86585
86586
86587
86588
86589
86590
86591
86592
86593
86594
86595
86596
86597
86598
86599
86600
86601
86602
86603
86604
86605
86606
86607
86608
86609
86610
86611
86612
86613
86614
86615
86616
86617
86618
86619
86620
86621
86622
86623
86624
86625
86626
86627
86628
86629
86630
86631
86632
86633
86634
86635
86636
86637
86638
86639
86640
86641
86642
86643
86644
86645
86646
86647
86648
86649
86650
86651
86652
86653
86654
86655
86656
86657
86658
86659
86660
86661
86662
86663
86664
86665
86666
86667
86668
86669
86670
86671
86672
86673
86674
86675
86676
86677
86678
86679
86680
86681
86682
86683
86684
86685
86686
86687
86688
86689
86690
86691
86692
86693
86694
86695
86696
86697
86698
86699
86700
86701
86702
86703
86704
86705
86706
86707
86708
86709
86710
86711
86712
86713
86714
86715
86716
86717
86718
86719
86720
86721
86722
86723
86724
86725
86726
86727
86728
86729
86730
86731
86732
86733
86734
86735
86736
86737
86738
86739
86740
86741
86742
86743
86744
86745
86746
86747
86748
86749
86750
86751
86752
86753
86754
86755
86756
86757
86758
86759
86760
86761
86762
86763
86764
86765
86766
86767
86768
86769
86770
86771
86772
86773
86774
86775
86776
86777
86778
86779
86780
86781
86782
86783
86784
86785
86786
86787
86788
86789
86790
86791
86792
86793
86794
86795
86796
86797
86798
86799
86800
86801
86802
86803
86804
86805
86806
86807
86808
86809
86810
86811
86812
86813
86814
86815
86816
86817
86818
86819
86820
86821
86822
86823
86824
86825
86826
86827
86828
86829
86830
86831
86832
86833
86834
86835
86836
86837
86838
86839
86840
86841
86842
86843
86844
86845
86846
86847
86848
86849
86850
86851
86852
86853
86854
86855
86856
86857
86858
86859
86860
86861
86862
86863
86864
86865
86866
86867
86868
86869
86870
86871
86872
86873
86874
86875
86876
86877
86878
86879
86880
86881
86882
86883
86884
86885
86886
86887
86888
86889
86890
86891
86892
86893
86894
86895
86896
86897
86898
86899
86900
86901
86902
86903
86904
86905
86906
86907
86908
86909
86910
86911
86912
86913
86914
86915
86916
86917
86918
86919
86920
86921
86922
86923
86924
86925
86926
86927
86928
86929
86930
86931
86932
86933
86934
86935
86936
86937
86938
86939
86940
86941
86942
86943
86944
86945
86946
86947
86948
86949
86950
86951
86952
86953
86954
86955
86956
86957
86958
86959
86960
86961
86962
86963
86964
86965
86966
86967
86968
86969
86970
86971
86972
86973
86974
86975
86976
86977
86978
86979
86980
86981
86982
86983
86984
86985
86986
86987
86988
86989
86990
86991
86992
86993
86994
86995
86996
86997
86998
86999
87000
87001
87002
87003
87004
87005
87006
87007
87008
87009
87010
87011
87012
87013
87014
87015
87016
87017
87018
87019
87020
87021
87022
87023
87024
87025
87026
87027
87028
87029
87030
87031
87032
87033
87034
87035
87036
87037
87038
87039
87040
87041
87042
87043
87044
87045
87046
87047
87048
87049
87050
87051
87052
87053
87054
87055
87056
87057
87058
87059
87060
87061
87062
87063
87064
87065
87066
87067
87068
87069
87070
87071
87072
87073
87074
87075
87076
87077
87078
87079
87080
87081
87082
87083
87084
87085
87086
87087
87088
87089
87090
87091
87092
87093
87094
87095
87096
87097
87098
87099
87100
87101
87102
87103
87104
87105
87106
87107
87108
87109
87110
87111
87112
87113
87114
87115
87116
87117
87118
87119
87120
87121
87122
87123
87124
87125
87126
87127
87128
87129
87130
87131
87132
87133
87134
87135
87136
87137
87138
87139
87140
87141
87142
87143
87144
87145
87146
87147
87148
87149
87150
87151
87152
87153
87154
87155
87156
87157
87158
87159
87160
87161
87162
87163
87164
87165
87166
87167
87168
87169
87170
87171
87172
87173
87174
87175
87176
87177
87178
87179
87180
87181
87182
87183
87184
87185
87186
87187
87188
87189
87190
87191
87192
87193
87194
87195
87196
87197
87198
87199
87200
87201
87202
87203
87204
87205
87206
87207
87208
87209
87210
87211
87212
87213
87214
87215
87216
87217
87218
87219
87220
87221
87222
87223
87224
87225
87226
87227
87228
87229
87230
87231
87232
87233
87234
87235
87236
87237
87238
87239
87240
87241
87242
87243
87244
87245
87246
87247
87248
87249
87250
87251
87252
87253
87254
87255
87256
87257
87258
87259
87260
87261
87262
87263
87264
87265
87266
87267
87268
87269
87270
87271
87272
87273
87274
87275
87276
87277
87278
87279
87280
87281
87282
87283
87284
87285
87286
87287
87288
87289
87290
87291
87292
87293
87294
87295
87296
87297
87298
87299
87300
87301
87302
87303
87304
87305
87306
87307
87308
87309
87310
87311
87312
87313
87314
87315
87316
87317
87318
87319
87320
87321
87322
87323
87324
87325
87326
87327
87328
87329
87330
87331
87332
87333
87334
87335
87336
87337
87338
87339
87340
87341
87342
87343
87344
87345
87346
87347
87348
87349
87350
87351
87352
87353
87354
87355
87356
87357
87358
87359
87360
87361
87362
87363
87364
87365
87366
87367
87368
87369
87370
87371
87372
87373
87374
87375
87376
87377
87378
87379
87380
87381
87382
87383
87384
87385
87386
87387
87388
87389
87390
87391
87392
87393
87394
87395
87396
87397
87398
87399
87400
87401
87402
87403
87404
87405
87406
87407
87408
87409
87410
87411
87412
87413
87414
87415
87416
87417
87418
87419
87420
87421
87422
87423
87424
87425
87426
87427
87428
87429
87430
87431
87432
87433
87434
87435
87436
87437
87438
87439
87440
87441
87442
87443
87444
87445
87446
87447
87448
87449
87450
87451
87452
87453
87454
87455
87456
87457
87458
87459
87460
87461
87462
87463
87464
87465
87466
87467
87468
87469
87470
87471
87472
87473
87474
87475
87476
87477
87478
87479
87480
87481
87482
87483
87484
87485
87486
87487
87488
87489
87490
87491
87492
87493
87494
87495
87496
87497
87498
87499
87500
87501
87502
87503
87504
87505
87506
87507
87508
87509
87510
87511
87512
87513
87514
87515
87516
87517
87518
87519
87520
87521
87522
87523
87524
87525
87526
87527
87528
87529
87530
87531
87532
87533
87534
87535
87536
87537
87538
87539
87540
87541
87542
87543
87544
87545
87546
87547
87548
87549
87550
87551
87552
87553
87554
87555
87556
87557
87558
87559
87560
87561
87562
87563
87564
87565
87566
87567
87568
87569
87570
87571
87572
87573
87574
87575
87576
87577
87578
87579
87580
87581
87582
87583
87584
87585
87586
87587
87588
87589
87590
87591
87592
87593
87594
87595
87596
87597
87598
87599
87600
87601
87602
87603
87604
87605
87606
87607
87608
87609
87610
87611
87612
87613
87614
87615
87616
87617
87618
87619
87620
87621
87622
87623
87624
87625
87626
87627
87628
87629
87630
87631
87632
87633
87634
87635
87636
87637
87638
87639
87640
87641
87642
87643
87644
87645
87646
87647
87648
87649
87650
87651
87652
87653
87654
87655
87656
87657
87658
87659
87660
87661
87662
87663
87664
87665
87666
87667
87668
87669
87670
87671
87672
87673
87674
87675
87676
87677
87678
87679
87680
87681
87682
87683
87684
87685
87686
87687
87688
87689
87690
87691
87692
87693
87694
87695
87696
87697
87698
87699
87700
87701
87702
87703
87704
87705
87706
87707
87708
87709
87710
87711
87712
87713
87714
87715
87716
87717
87718
87719
87720
87721
87722
87723
87724
87725
87726
87727
87728
87729
87730
87731
87732
87733
87734
87735
87736
87737
87738
87739
87740
87741
87742
87743
87744
87745
87746
87747
87748
87749
87750
87751
87752
87753
87754
87755
87756
87757
87758
87759
87760
87761
87762
87763
87764
87765
87766
87767
87768
87769
87770
87771
87772
87773
87774
87775
87776
87777
87778
87779
87780
87781
87782
87783
87784
87785
87786
87787
87788
87789
87790
87791
87792
87793
87794
87795
87796
87797
87798
87799
87800
87801
87802
87803
87804
87805
87806
87807
87808
87809
87810
87811
87812
87813
87814
87815
87816
87817
87818
87819
87820
87821
87822
87823
87824
87825
87826
87827
87828
87829
87830
87831
87832
87833
87834
87835
87836
87837
87838
87839
87840
87841
87842
87843
87844
87845
87846
87847
87848
87849
87850
87851
87852
87853
87854
87855
87856
87857
87858
87859
87860
87861
87862
87863
87864
87865
87866
87867
87868
87869
87870
87871
87872
87873
87874
87875
87876
87877
87878
87879
87880
87881
87882
87883
87884
87885
87886
87887
87888
87889
87890
87891
87892
87893
87894
87895
87896
87897
87898
87899
87900
87901
87902
87903
87904
87905
87906
87907
87908
87909
87910
87911
87912
87913
87914
87915
87916
87917
87918
87919
87920
87921
87922
87923
87924
87925
87926
87927
87928
87929
87930
87931
87932
87933
87934
87935
87936
87937
87938
87939
87940
87941
87942
87943
87944
87945
87946
87947
87948
87949
87950
87951
87952
87953
87954
87955
87956
87957
87958
87959
87960
87961
87962
87963
87964
87965
87966
87967
87968
87969
87970
87971
87972
87973
87974
87975
87976
87977
87978
87979
87980
87981
87982
87983
87984
87985
87986
87987
87988
87989
87990
87991
87992
87993
87994
87995
87996
87997
87998
87999
88000
88001
88002
88003
88004
88005
88006
88007
88008
88009
88010
88011
88012
88013
88014
88015
88016
88017
88018
88019
88020
88021
88022
88023
88024
88025
88026
88027
88028
88029
88030
88031
88032
88033
88034
88035
88036
88037
88038
88039
88040
88041
88042
88043
88044
88045
88046
88047
88048
88049
88050
88051
88052
88053
88054
88055
88056
88057
88058
88059
88060
88061
88062
88063
88064
88065
88066
88067
88068
88069
88070
88071
88072
88073
88074
88075
88076
88077
88078
88079
88080
88081
88082
88083
88084
88085
88086
88087
88088
88089
88090
88091
88092
88093
88094
88095
88096
88097
88098
88099
88100
88101
88102
88103
88104
88105
88106
88107
88108
88109
88110
88111
88112
88113
88114
88115
88116
88117
88118
88119
88120
88121
88122
88123
88124
88125
88126
88127
88128
88129
88130
88131
88132
88133
88134
88135
88136
88137
88138
88139
88140
88141
88142
88143
88144
88145
88146
88147
88148
88149
88150
88151
88152
88153
88154
88155
88156
88157
88158
88159
88160
88161
88162
88163
88164
88165
88166
88167
88168
88169
88170
88171
88172
88173
88174
88175
88176
88177
88178
88179
88180
88181
88182
88183
88184
88185
88186
88187
88188
88189
88190
88191
88192
88193
88194
88195
88196
88197
88198
88199
88200
88201
88202
88203
88204
88205
88206
88207
88208
88209
88210
88211
88212
88213
88214
88215
88216
88217
88218
88219
88220
88221
88222
88223
88224
88225
88226
88227
88228
88229
88230
88231
88232
88233
88234
88235
88236
88237
88238
88239
88240
88241
88242
88243
88244
88245
88246
88247
88248
88249
88250
88251
88252
88253
88254
88255
88256
88257
88258
88259
88260
88261
88262
88263
88264
88265
88266
88267
88268
88269
88270
88271
88272
88273
88274
88275
88276
88277
88278
88279
88280
88281
88282
88283
88284
88285
88286
88287
88288
88289
88290
88291
88292
88293
88294
88295
88296
88297
88298
88299
88300
88301
88302
88303
88304
88305
88306
88307
88308
88309
88310
88311
88312
88313
88314
88315
88316
88317
88318
88319
88320
88321
88322
88323
88324
88325
88326
88327
88328
88329
88330
88331
88332
88333
88334
88335
88336
88337
88338
88339
88340
88341
88342
88343
88344
88345
88346
88347
88348
88349
88350
88351
88352
88353
88354
88355
88356
88357
88358
88359
88360
88361
88362
88363
88364
88365
88366
88367
88368
88369
88370
88371
88372
88373
88374
88375
88376
88377
88378
88379
88380
88381
88382
88383
88384
88385
88386
88387
88388
88389
88390
88391
88392
88393
88394
88395
88396
88397
88398
88399
88400
88401
88402
88403
88404
88405
88406
88407
88408
88409
88410
88411
88412
88413
88414
88415
88416
88417
88418
88419
88420
88421
88422
88423
88424
88425
88426
88427
88428
88429
88430
88431
88432
88433
88434
88435
88436
88437
88438
88439
88440
88441
88442
88443
88444
88445
88446
88447
88448
88449
88450
88451
88452
88453
88454
88455
88456
88457
88458
88459
88460
88461
88462
88463
88464
88465
88466
88467
88468
88469
88470
88471
88472
88473
88474
88475
88476
88477
88478
88479
88480
88481
88482
88483
88484
88485
88486
88487
88488
88489
88490
88491
88492
88493
88494
88495
88496
88497
88498
88499
88500
88501
88502
88503
88504
88505
88506
88507
88508
88509
88510
88511
88512
88513
88514
88515
88516
88517
88518
88519
88520
88521
88522
88523
88524
88525
88526
88527
88528
88529
88530
88531
88532
88533
88534
88535
88536
88537
88538
88539
88540
88541
88542
88543
88544
88545
88546
88547
88548
88549
88550
88551
88552
88553
88554
88555
88556
88557
88558
88559
88560
88561
88562
88563
88564
88565
88566
88567
88568
88569
88570
88571
88572
88573
88574
88575
88576
88577
88578
88579
88580
88581
88582
88583
88584
88585
88586
88587
88588
88589
88590
88591
88592
88593
88594
88595
88596
88597
88598
88599
88600
88601
88602
88603
88604
88605
88606
88607
88608
88609
88610
88611
88612
88613
88614
88615
88616
88617
88618
88619
88620
88621
88622
88623
88624
88625
88626
88627
88628
88629
88630
88631
88632
88633
88634
88635
88636
88637
88638
88639
88640
88641
88642
88643
88644
88645
88646
88647
88648
88649
88650
88651
88652
88653
88654
88655
88656
88657
88658
88659
88660
88661
88662
88663
88664
88665
88666
88667
88668
88669
88670
88671
88672
88673
88674
88675
88676
88677
88678
88679
88680
88681
88682
88683
88684
88685
88686
88687
88688
88689
88690
88691
88692
88693
88694
88695
88696
88697
88698
88699
88700
88701
88702
88703
88704
88705
88706
88707
88708
88709
88710
88711
88712
88713
88714
88715
88716
88717
88718
88719
88720
88721
88722
88723
88724
88725
88726
88727
88728
88729
88730
88731
88732
88733
88734
88735
88736
88737
88738
88739
88740
88741
88742
88743
88744
88745
88746
88747
88748
88749
88750
88751
88752
88753
88754
88755
88756
88757
88758
88759
88760
88761
88762
88763
88764
88765
88766
88767
88768
88769
88770
88771
88772
88773
88774
88775
88776
88777
88778
88779
88780
88781
88782
88783
88784
88785
88786
88787
88788
88789
88790
88791
88792
88793
88794
88795
88796
88797
88798
88799
88800
88801
88802
88803
88804
88805
88806
88807
88808
88809
88810
88811
88812
88813
88814
88815
88816
88817
88818
88819
88820
88821
88822
88823
88824
88825
88826
88827
88828
88829
88830
88831
88832
88833
88834
88835
88836
88837
88838
88839
88840
88841
88842
88843
88844
88845
88846
88847
88848
88849
88850
88851
88852
88853
88854
88855
88856
88857
88858
88859
88860
88861
88862
88863
88864
88865
88866
88867
88868
88869
88870
88871
88872
88873
88874
88875
88876
88877
88878
88879
88880
88881
88882
88883
88884
88885
88886
88887
88888
88889
88890
88891
88892
88893
88894
88895
88896
88897
88898
88899
88900
88901
88902
88903
88904
88905
88906
88907
88908
88909
88910
88911
88912
88913
88914
88915
88916
88917
88918
88919
88920
88921
88922
88923
88924
88925
88926
88927
88928
88929
88930
88931
88932
88933
88934
88935
88936
88937
88938
88939
88940
88941
88942
88943
88944
88945
88946
88947
88948
88949
88950
88951
88952
88953
88954
88955
88956
88957
88958
88959
88960
88961
88962
88963
88964
88965
88966
88967
88968
88969
88970
88971
88972
88973
88974
88975
88976
88977
88978
88979
88980
88981
88982
88983
88984
88985
88986
88987
88988
88989
88990
88991
88992
88993
88994
88995
88996
88997
88998
88999
89000
89001
89002
89003
89004
89005
89006
89007
89008
89009
89010
89011
89012
89013
89014
89015
89016
89017
89018
89019
89020
89021
89022
89023
89024
89025
89026
89027
89028
89029
89030
89031
89032
89033
89034
89035
89036
89037
89038
89039
89040
89041
89042
89043
89044
89045
89046
89047
89048
89049
89050
89051
89052
89053
89054
89055
89056
89057
89058
89059
89060
89061
89062
89063
89064
89065
89066
89067
89068
89069
89070
89071
89072
89073
89074
89075
89076
89077
89078
89079
89080
89081
89082
89083
89084
89085
89086
89087
89088
89089
89090
89091
89092
89093
89094
89095
89096
89097
89098
89099
89100
89101
89102
89103
89104
89105
89106
89107
89108
89109
89110
89111
89112
89113
89114
89115
89116
89117
89118
89119
89120
89121
89122
89123
89124
89125
89126
89127
89128
89129
89130
89131
89132
89133
89134
89135
89136
89137
89138
89139
89140
89141
89142
89143
89144
89145
89146
89147
89148
89149
89150
89151
89152
89153
89154
89155
89156
89157
89158
89159
89160
89161
89162
89163
89164
89165
89166
89167
89168
89169
89170
89171
89172
89173
89174
89175
89176
89177
89178
89179
89180
89181
89182
89183
89184
89185
89186
89187
89188
89189
89190
89191
89192
89193
89194
89195
89196
89197
89198
89199
89200
89201
89202
89203
89204
89205
89206
89207
89208
89209
89210
89211
89212
89213
89214
89215
89216
89217
89218
89219
89220
89221
89222
89223
89224
89225
89226
89227
89228
89229
89230
89231
89232
89233
89234
89235
89236
89237
89238
89239
89240
89241
89242
89243
89244
89245
89246
89247
89248
89249
89250
89251
89252
89253
89254
89255
89256
89257
89258
89259
89260
89261
89262
89263
89264
89265
89266
89267
89268
89269
89270
89271
89272
89273
89274
89275
89276
89277
89278
89279
89280
89281
89282
89283
89284
89285
89286
89287
89288
89289
89290
89291
89292
89293
89294
89295
89296
89297
89298
89299
89300
89301
89302
89303
89304
89305
89306
89307
89308
89309
89310
89311
89312
89313
89314
89315
89316
89317
89318
89319
89320
89321
89322
89323
89324
89325
89326
89327
89328
89329
89330
89331
89332
89333
89334
89335
89336
89337
89338
89339
89340
89341
89342
89343
89344
89345
89346
89347
89348
89349
89350
89351
89352
89353
89354
89355
89356
89357
89358
89359
89360
89361
89362
89363
89364
89365
89366
89367
89368
89369
89370
89371
89372
89373
89374
89375
89376
89377
89378
89379
89380
89381
89382
89383
89384
89385
89386
89387
89388
89389
89390
89391
89392
89393
89394
89395
89396
89397
89398
89399
89400
89401
89402
89403
89404
89405
89406
89407
89408
89409
89410
89411
89412
89413
89414
89415
89416
89417
89418
89419
89420
89421
89422
89423
89424
89425
89426
89427
89428
89429
89430
89431
89432
89433
89434
89435
89436
89437
89438
89439
89440
89441
89442
89443
89444
89445
89446
89447
89448
89449
89450
89451
89452
89453
89454
89455
89456
89457
89458
89459
89460
89461
89462
89463
89464
89465
89466
89467
89468
89469
89470
89471
89472
89473
89474
89475
89476
89477
89478
89479
89480
89481
89482
89483
89484
89485
89486
89487
89488
89489
89490
89491
89492
89493
89494
89495
89496
89497
89498
89499
89500
89501
89502
89503
89504
89505
89506
89507
89508
89509
89510
89511
89512
89513
89514
89515
89516
89517
89518
89519
89520
89521
89522
89523
89524
89525
89526
89527
89528
89529
89530
89531
89532
89533
89534
89535
89536
89537
89538
89539
89540
89541
89542
89543
89544
89545
89546
89547
89548
89549
89550
89551
89552
89553
89554
89555
89556
89557
89558
89559
89560
89561
89562
89563
89564
89565
89566
89567
89568
89569
89570
89571
89572
89573
89574
89575
89576
89577
89578
89579
89580
89581
89582
89583
89584
89585
89586
89587
89588
89589
89590
89591
89592
89593
89594
89595
89596
89597
89598
89599
89600
89601
89602
89603
89604
89605
89606
89607
89608
89609
89610
89611
89612
89613
89614
89615
89616
89617
89618
89619
89620
89621
89622
89623
89624
89625
89626
89627
89628
89629
89630
89631
89632
89633
89634
89635
89636
89637
89638
89639
89640
89641
89642
89643
89644
89645
89646
89647
89648
89649
89650
89651
89652
89653
89654
89655
89656
89657
89658
89659
89660
89661
89662
89663
89664
89665
89666
89667
89668
89669
89670
89671
89672
89673
89674
89675
89676
89677
89678
89679
89680
89681
89682
89683
89684
89685
89686
89687
89688
89689
89690
89691
89692
89693
89694
89695
89696
89697
89698
89699
89700
89701
89702
89703
89704
89705
89706
89707
89708
89709
89710
89711
89712
89713
89714
89715
89716
89717
89718
89719
89720
89721
89722
89723
89724
89725
89726
89727
89728
89729
89730
89731
89732
89733
89734
89735
89736
89737
89738
89739
89740
89741
89742
89743
89744
89745
89746
89747
89748
89749
89750
89751
89752
89753
89754
89755
89756
89757
89758
89759
89760
89761
89762
89763
89764
89765
89766
89767
89768
89769
89770
89771
89772
89773
89774
89775
89776
89777
89778
89779
89780
89781
89782
89783
89784
89785
89786
89787
89788
89789
89790
89791
89792
89793
89794
89795
89796
89797
89798
89799
89800
89801
89802
89803
89804
89805
89806
89807
89808
89809
89810
89811
89812
89813
89814
89815
89816
89817
89818
89819
89820
89821
89822
89823
89824
89825
89826
89827
89828
89829
89830
89831
89832
89833
89834
89835
89836
89837
89838
89839
89840
89841
89842
89843
89844
89845
89846
89847
89848
89849
89850
89851
89852
89853
89854
89855
89856
89857
89858
89859
89860
89861
89862
89863
89864
89865
89866
89867
89868
89869
89870
89871
89872
89873
89874
89875
89876
89877
89878
89879
89880
89881
89882
89883
89884
89885
89886
89887
89888
89889
89890
89891
89892
89893
89894
89895
89896
89897
89898
89899
89900
89901
89902
89903
89904
89905
89906
89907
89908
89909
89910
89911
89912
89913
89914
89915
89916
89917
89918
89919
89920
89921
89922
89923
89924
89925
89926
89927
89928
89929
89930
89931
89932
89933
89934
89935
89936
89937
89938
89939
89940
89941
89942
89943
89944
89945
89946
89947
89948
89949
89950
89951
89952
89953
89954
89955
89956
89957
89958
89959
89960
89961
89962
89963
89964
89965
89966
89967
89968
89969
89970
89971
89972
89973
89974
89975
89976
89977
89978
89979
89980
89981
89982
89983
89984
89985
89986
89987
89988
89989
89990
89991
89992
89993
89994
89995
89996
89997
89998
89999
90000
90001
90002
90003
90004
90005
90006
90007
90008
90009
90010
90011
90012
90013
90014
90015
90016
90017
90018
90019
90020
90021
90022
90023
90024
90025
90026
90027
90028
90029
90030
90031
90032
90033
90034
90035
90036
90037
90038
90039
90040
90041
90042
90043
90044
90045
90046
90047
90048
90049
90050
90051
90052
90053
90054
90055
90056
90057
90058
90059
90060
90061
90062
90063
90064
90065
90066
90067
90068
90069
90070
90071
90072
90073
90074
90075
90076
90077
90078
90079
90080
90081
90082
90083
90084
90085
90086
90087
90088
90089
90090
90091
90092
90093
90094
90095
90096
90097
90098
90099
90100
90101
90102
90103
90104
90105
90106
90107
90108
90109
90110
90111
90112
90113
90114
90115
90116
90117
90118
90119
90120
90121
90122
90123
90124
90125
90126
90127
90128
90129
90130
90131
90132
90133
90134
90135
90136
90137
90138
90139
90140
90141
90142
90143
90144
90145
90146
90147
90148
90149
90150
90151
90152
90153
90154
90155
90156
90157
90158
90159
90160
90161
90162
90163
90164
90165
90166
90167
90168
90169
90170
90171
90172
90173
90174
90175
90176
90177
90178
90179
90180
90181
90182
90183
90184
90185
90186
90187
90188
90189
90190
90191
90192
90193
90194
90195
90196
90197
90198
90199
90200
90201
90202
90203
90204
90205
90206
90207
90208
90209
90210
90211
90212
90213
90214
90215
90216
90217
90218
90219
90220
90221
90222
90223
90224
90225
90226
90227
90228
90229
90230
90231
90232
90233
90234
90235
90236
90237
90238
90239
90240
90241
90242
90243
90244
90245
90246
90247
90248
90249
90250
90251
90252
90253
90254
90255
90256
90257
90258
90259
90260
90261
90262
90263
90264
90265
90266
90267
90268
90269
90270
90271
90272
90273
90274
90275
90276
90277
90278
90279
90280
90281
90282
90283
90284
90285
90286
90287
90288
90289
90290
90291
90292
90293
90294
90295
90296
90297
90298
90299
90300
90301
90302
90303
90304
90305
90306
90307
90308
90309
90310
90311
90312
90313
90314
90315
90316
90317
90318
90319
90320
90321
90322
90323
90324
90325
90326
90327
90328
90329
90330
90331
90332
90333
90334
90335
90336
90337
90338
90339
90340
90341
90342
90343
90344
90345
90346
90347
90348
90349
90350
90351
90352
90353
90354
90355
90356
90357
90358
90359
90360
90361
90362
90363
90364
90365
90366
90367
90368
90369
90370
90371
90372
90373
90374
90375
90376
90377
90378
90379
90380
90381
90382
90383
90384
90385
90386
90387
90388
90389
90390
90391
90392
90393
90394
90395
90396
90397
90398
90399
90400
90401
90402
90403
90404
90405
90406
90407
90408
90409
90410
90411
90412
90413
90414
90415
90416
90417
90418
90419
90420
90421
90422
90423
90424
90425
90426
90427
90428
90429
90430
90431
90432
90433
90434
90435
90436
90437
90438
90439
90440
90441
90442
90443
90444
90445
90446
90447
90448
90449
90450
90451
90452
90453
90454
90455
90456
90457
90458
90459
90460
90461
90462
90463
90464
90465
90466
90467
90468
90469
90470
90471
90472
90473
90474
90475
90476
90477
90478
90479
90480
90481
90482
90483
90484
90485
90486
90487
90488
90489
90490
90491
90492
90493
90494
90495
90496
90497
90498
90499
90500
90501
90502
90503
90504
90505
90506
90507
90508
90509
90510
90511
90512
90513
90514
90515
90516
90517
90518
90519
90520
90521
90522
90523
90524
90525
90526
90527
90528
90529
90530
90531
90532
90533
90534
90535
90536
90537
90538
90539
90540
90541
90542
90543
90544
90545
90546
90547
90548
90549
90550
90551
90552
90553
90554
90555
90556
90557
90558
90559
90560
90561
90562
90563
90564
90565
90566
90567
90568
90569
90570
90571
90572
90573
90574
90575
90576
90577
90578
90579
90580
90581
90582
90583
90584
90585
90586
90587
90588
90589
90590
90591
90592
90593
90594
90595
90596
90597
90598
90599
90600
90601
90602
90603
90604
90605
90606
90607
90608
90609
90610
90611
90612
90613
90614
90615
90616
90617
90618
90619
90620
90621
90622
90623
90624
90625
90626
90627
90628
90629
90630
90631
90632
90633
90634
90635
90636
90637
90638
90639
90640
90641
90642
90643
90644
90645
90646
90647
90648
90649
90650
90651
90652
90653
90654
90655
90656
90657
90658
90659
90660
90661
90662
90663
90664
90665
90666
90667
90668
90669
90670
90671
90672
90673
90674
90675
90676
90677
90678
90679
90680
90681
90682
90683
90684
90685
90686
90687
90688
90689
90690
90691
90692
90693
90694
90695
90696
90697
90698
90699
90700
90701
90702
90703
90704
90705
90706
90707
90708
90709
90710
90711
90712
90713
90714
90715
90716
90717
90718
90719
90720
90721
90722
90723
90724
90725
90726
90727
90728
90729
90730
90731
90732
90733
90734
90735
90736
90737
90738
90739
90740
90741
90742
90743
90744
90745
90746
90747
90748
90749
90750
90751
90752
90753
90754
90755
90756
90757
90758
90759
90760
90761
90762
90763
90764
90765
90766
90767
90768
90769
90770
90771
90772
90773
90774
90775
90776
90777
90778
90779
90780
90781
90782
90783
90784
90785
90786
90787
90788
90789
90790
90791
90792
90793
90794
90795
90796
90797
90798
90799
90800
90801
90802
90803
90804
90805
90806
90807
90808
90809
90810
90811
90812
90813
90814
90815
90816
90817
90818
90819
90820
90821
90822
90823
90824
90825
90826
90827
90828
90829
90830
90831
90832
90833
90834
90835
90836
90837
90838
90839
90840
90841
90842
90843
90844
90845
90846
90847
90848
90849
90850
90851
90852
90853
90854
90855
90856
90857
90858
90859
90860
90861
90862
90863
90864
90865
90866
90867
90868
90869
90870
90871
90872
90873
90874
90875
90876
90877
90878
90879
90880
90881
90882
90883
90884
90885
90886
90887
90888
90889
90890
90891
90892
90893
90894
90895
90896
90897
90898
90899
90900
90901
90902
90903
90904
90905
90906
90907
90908
90909
90910
90911
90912
90913
90914
90915
90916
90917
90918
90919
90920
90921
90922
90923
90924
90925
90926
90927
90928
90929
90930
90931
90932
90933
90934
90935
90936
90937
90938
90939
90940
90941
90942
90943
90944
90945
90946
90947
90948
90949
90950
90951
90952
90953
90954
90955
90956
90957
90958
90959
90960
90961
90962
90963
90964
90965
90966
90967
90968
90969
90970
90971
90972
90973
90974
90975
90976
90977
90978
90979
90980
90981
90982
90983
90984
90985
90986
90987
90988
90989
90990
90991
90992
90993
90994
90995
90996
90997
90998
90999
91000
91001
91002
91003
91004
91005
91006
91007
91008
91009
91010
91011
91012
91013
91014
91015
91016
91017
91018
91019
91020
91021
91022
91023
91024
91025
91026
91027
91028
91029
91030
91031
91032
91033
91034
91035
91036
91037
91038
91039
91040
91041
91042
91043
91044
91045
91046
91047
91048
91049
91050
91051
91052
91053
91054
91055
91056
91057
91058
91059
91060
91061
91062
91063
91064
91065
91066
91067
91068
91069
91070
91071
91072
91073
91074
91075
91076
91077
91078
91079
91080
91081
91082
91083
91084
91085
91086
91087
91088
91089
91090
91091
91092
91093
91094
91095
91096
91097
91098
91099
91100
91101
91102
91103
91104
91105
91106
91107
91108
91109
91110
91111
91112
91113
91114
91115
91116
91117
91118
91119
91120
91121
91122
91123
91124
91125
91126
91127
91128
91129
91130
91131
91132
91133
91134
91135
91136
91137
91138
91139
91140
91141
91142
91143
91144
91145
91146
91147
91148
91149
91150
91151
91152
91153
91154
91155
91156
91157
91158
91159
91160
91161
91162
91163
91164
91165
91166
91167
91168
91169
91170
91171
91172
91173
91174
91175
91176
91177
91178
91179
91180
91181
91182
91183
91184
91185
91186
91187
91188
91189
91190
91191
91192
91193
91194
91195
91196
91197
91198
91199
91200
91201
91202
91203
91204
91205
91206
91207
91208
91209
91210
91211
91212
91213
91214
91215
91216
91217
91218
91219
91220
91221
91222
91223
91224
91225
91226
91227
91228
91229
91230
91231
91232
91233
91234
91235
91236
91237
91238
91239
91240
91241
91242
91243
91244
91245
91246
91247
91248
91249
91250
91251
91252
91253
91254
91255
91256
91257
91258
91259
91260
91261
91262
91263
91264
91265
91266
91267
91268
91269
91270
91271
91272
91273
91274
91275
91276
91277
91278
91279
91280
91281
91282
91283
91284
91285
91286
91287
91288
91289
91290
91291
91292
91293
91294
91295
91296
91297
91298
91299
91300
91301
91302
91303
91304
91305
91306
91307
91308
91309
91310
91311
91312
91313
91314
91315
91316
91317
91318
91319
91320
91321
91322
91323
91324
91325
91326
91327
91328
91329
91330
91331
91332
91333
91334
91335
91336
91337
91338
91339
91340
91341
91342
91343
91344
91345
91346
91347
91348
91349
91350
91351
91352
91353
91354
91355
91356
91357
91358
91359
91360
91361
91362
91363
91364
91365
91366
91367
91368
91369
91370
91371
91372
91373
91374
91375
91376
91377
91378
91379
91380
91381
91382
91383
91384
91385
91386
91387
91388
91389
91390
91391
91392
91393
91394
91395
91396
91397
91398
91399
91400
91401
91402
91403
91404
91405
91406
91407
91408
91409
91410
91411
91412
91413
91414
91415
91416
91417
91418
91419
91420
91421
91422
91423
91424
91425
91426
91427
91428
91429
91430
91431
91432
91433
91434
91435
91436
91437
91438
91439
91440
91441
91442
91443
91444
91445
91446
91447
91448
91449
91450
91451
91452
91453
91454
91455
91456
91457
91458
91459
91460
91461
91462
91463
91464
91465
91466
91467
91468
91469
91470
91471
91472
91473
91474
91475
91476
91477
91478
91479
91480
91481
91482
91483
91484
91485
91486
91487
91488
91489
91490
91491
91492
91493
91494
91495
91496
91497
91498
91499
91500
91501
91502
91503
91504
91505
91506
91507
91508
91509
91510
91511
91512
91513
91514
91515
91516
91517
91518
91519
91520
91521
91522
91523
91524
91525
91526
91527
91528
91529
91530
91531
91532
91533
91534
91535
91536
91537
91538
91539
91540
91541
91542
91543
91544
91545
91546
91547
91548
91549
91550
91551
91552
91553
91554
91555
91556
91557
91558
91559
91560
91561
91562
91563
91564
91565
91566
91567
91568
91569
91570
91571
91572
91573
91574
91575
91576
91577
91578
91579
91580
91581
91582
91583
91584
91585
91586
91587
91588
91589
91590
91591
91592
91593
91594
91595
91596
91597
91598
91599
91600
91601
91602
91603
91604
91605
91606
91607
91608
91609
91610
91611
91612
91613
91614
91615
91616
91617
91618
91619
91620
91621
91622
91623
91624
91625
91626
91627
91628
91629
91630
91631
91632
91633
91634
91635
91636
91637
91638
91639
91640
91641
91642
91643
91644
91645
91646
91647
91648
91649
91650
91651
91652
91653
91654
91655
91656
91657
91658
91659
91660
91661
91662
91663
91664
91665
91666
91667
91668
91669
91670
91671
91672
91673
91674
91675
91676
91677
91678
91679
91680
91681
91682
91683
91684
91685
91686
91687
91688
91689
91690
91691
91692
91693
91694
91695
91696
91697
91698
91699
91700
91701
91702
91703
91704
91705
91706
91707
91708
91709
91710
91711
91712
91713
91714
91715
91716
91717
91718
91719
91720
91721
91722
91723
91724
91725
91726
91727
91728
91729
91730
91731
91732
91733
91734
91735
91736
91737
91738
91739
91740
91741
91742
91743
91744
91745
91746
91747
91748
91749
91750
91751
91752
91753
91754
91755
91756
91757
91758
91759
91760
91761
91762
91763
91764
91765
91766
91767
91768
91769
91770
91771
91772
91773
91774
91775
91776
91777
91778
91779
91780
91781
91782
91783
91784
91785
91786
91787
91788
91789
91790
91791
91792
91793
91794
91795
91796
91797
91798
91799
91800
91801
91802
91803
91804
91805
91806
91807
91808
91809
91810
91811
91812
91813
91814
91815
91816
91817
91818
91819
91820
91821
91822
91823
91824
91825
91826
91827
91828
91829
91830
91831
91832
91833
91834
91835
91836
91837
91838
91839
91840
91841
91842
91843
91844
91845
91846
91847
91848
91849
91850
91851
91852
91853
91854
91855
91856
91857
91858
91859
91860
91861
91862
91863
91864
91865
91866
91867
91868
91869
91870
91871
91872
91873
91874
91875
91876
91877
91878
91879
91880
91881
91882
91883
91884
91885
91886
91887
91888
91889
91890
91891
91892
91893
91894
91895
91896
91897
91898
91899
91900
91901
91902
91903
91904
91905
91906
91907
91908
91909
91910
91911
91912
91913
91914
91915
91916
91917
91918
91919
91920
91921
91922
91923
91924
91925
91926
91927
91928
91929
91930
91931
91932
91933
91934
91935
91936
91937
91938
91939
91940
91941
91942
91943
91944
91945
91946
91947
91948
91949
91950
91951
91952
91953
91954
91955
91956
91957
91958
91959
91960
91961
91962
91963
91964
91965
91966
91967
91968
91969
91970
91971
91972
91973
91974
91975
91976
91977
91978
91979
91980
91981
91982
91983
91984
91985
91986
91987
91988
91989
91990
91991
91992
91993
91994
91995
91996
91997
91998
91999
92000
92001
92002
92003
92004
92005
92006
92007
92008
92009
92010
92011
92012
92013
92014
92015
92016
92017
92018
92019
92020
92021
92022
92023
92024
92025
92026
92027
92028
92029
92030
92031
92032
92033
92034
92035
92036
92037
92038
92039
92040
92041
92042
92043
92044
92045
92046
92047
92048
92049
92050
92051
92052
92053
92054
92055
92056
92057
92058
92059
92060
92061
92062
92063
92064
92065
92066
92067
92068
92069
92070
92071
92072
92073
92074
92075
92076
92077
92078
92079
92080
92081
92082
92083
92084
92085
92086
92087
92088
92089
92090
92091
92092
92093
92094
92095
92096
92097
92098
92099
92100
92101
92102
92103
92104
92105
92106
92107
92108
92109
92110
92111
92112
92113
92114
92115
92116
92117
92118
92119
92120
92121
92122
92123
92124
92125
92126
92127
92128
92129
92130
92131
92132
92133
92134
92135
92136
92137
92138
92139
92140
92141
92142
92143
92144
92145
92146
92147
92148
92149
92150
92151
92152
92153
92154
92155
92156
92157
92158
92159
92160
92161
92162
92163
92164
92165
92166
92167
92168
92169
92170
92171
92172
92173
92174
92175
92176
92177
92178
92179
92180
92181
92182
92183
92184
92185
92186
92187
92188
92189
92190
92191
92192
92193
92194
92195
92196
92197
92198
92199
92200
92201
92202
92203
92204
92205
92206
92207
92208
92209
92210
92211
92212
92213
92214
92215
92216
92217
92218
92219
92220
92221
92222
92223
92224
92225
92226
92227
92228
92229
92230
92231
92232
92233
92234
92235
92236
92237
92238
92239
92240
92241
92242
92243
92244
92245
92246
92247
92248
92249
92250
92251
92252
92253
92254
92255
92256
92257
92258
92259
92260
92261
92262
92263
92264
92265
92266
92267
92268
92269
92270
92271
92272
92273
92274
92275
92276
92277
92278
92279
92280
92281
92282
92283
92284
92285
92286
92287
92288
92289
92290
92291
92292
92293
92294
92295
92296
92297
92298
92299
92300
92301
92302
92303
92304
92305
92306
92307
92308
92309
92310
92311
92312
92313
92314
92315
92316
92317
92318
92319
92320
92321
92322
92323
92324
92325
92326
92327
92328
92329
92330
92331
92332
92333
92334
92335
92336
92337
92338
92339
92340
92341
92342
92343
92344
92345
92346
92347
92348
92349
92350
92351
92352
92353
92354
92355
92356
92357
92358
92359
92360
92361
92362
92363
92364
92365
92366
92367
92368
92369
92370
92371
92372
92373
92374
92375
92376
92377
92378
92379
92380
92381
92382
92383
92384
92385
92386
92387
92388
92389
92390
92391
92392
92393
92394
92395
92396
92397
92398
92399
92400
92401
92402
92403
92404
92405
92406
92407
92408
92409
92410
92411
92412
92413
92414
92415
92416
92417
92418
92419
92420
92421
92422
92423
92424
92425
92426
92427
92428
92429
92430
92431
92432
92433
92434
92435
92436
92437
92438
92439
92440
92441
92442
92443
92444
92445
92446
92447
92448
92449
92450
92451
92452
92453
92454
92455
92456
92457
92458
92459
92460
92461
92462
92463
92464
92465
92466
92467
92468
92469
92470
92471
92472
92473
92474
92475
92476
92477
92478
92479
92480
92481
92482
92483
92484
92485
92486
92487
92488
92489
92490
92491
92492
92493
92494
92495
92496
92497
92498
92499
92500
92501
92502
92503
92504
92505
92506
92507
92508
92509
92510
92511
92512
92513
92514
92515
92516
92517
92518
92519
92520
92521
92522
92523
92524
92525
92526
92527
92528
92529
92530
92531
92532
92533
92534
92535
92536
92537
92538
92539
92540
92541
92542
92543
92544
92545
92546
92547
92548
92549
92550
92551
92552
92553
92554
92555
92556
92557
92558
92559
92560
92561
92562
92563
92564
92565
92566
92567
92568
92569
92570
92571
92572
92573
92574
92575
92576
92577
92578
92579
92580
92581
92582
92583
92584
92585
92586
92587
92588
92589
92590
92591
92592
92593
92594
92595
92596
92597
92598
92599
92600
92601
92602
92603
92604
92605
92606
92607
92608
92609
92610
92611
92612
92613
92614
92615
92616
92617
92618
92619
92620
92621
92622
92623
92624
92625
92626
92627
92628
92629
92630
92631
92632
92633
92634
92635
92636
92637
92638
92639
92640
92641
92642
92643
92644
92645
92646
92647
92648
92649
92650
92651
92652
92653
92654
92655
92656
92657
92658
92659
92660
92661
92662
92663
92664
92665
92666
92667
92668
92669
92670
92671
92672
92673
92674
92675
92676
92677
92678
92679
92680
92681
92682
92683
92684
92685
92686
92687
92688
92689
92690
92691
92692
92693
92694
92695
92696
92697
92698
92699
92700
92701
92702
92703
92704
92705
92706
92707
92708
92709
92710
92711
92712
92713
92714
92715
92716
92717
92718
92719
92720
92721
92722
92723
92724
92725
92726
92727
92728
92729
92730
92731
92732
92733
92734
92735
92736
92737
92738
92739
92740
92741
92742
92743
92744
92745
92746
92747
92748
92749
92750
92751
92752
92753
92754
92755
92756
92757
92758
92759
92760
92761
92762
92763
92764
92765
92766
92767
92768
92769
92770
92771
92772
92773
92774
92775
92776
92777
92778
92779
92780
92781
92782
92783
92784
92785
92786
92787
92788
92789
92790
92791
92792
92793
92794
92795
92796
92797
92798
92799
92800
92801
92802
92803
92804
92805
92806
92807
92808
92809
92810
92811
92812
92813
92814
92815
92816
92817
92818
92819
92820
92821
92822
92823
92824
92825
92826
92827
92828
92829
92830
92831
92832
92833
92834
92835
92836
92837
92838
92839
92840
92841
92842
92843
92844
92845
92846
92847
92848
92849
92850
92851
92852
92853
92854
92855
92856
92857
92858
92859
92860
92861
92862
92863
92864
92865
92866
92867
92868
92869
92870
92871
92872
92873
92874
92875
92876
92877
92878
92879
92880
92881
92882
92883
92884
92885
92886
92887
92888
92889
92890
92891
92892
92893
92894
92895
92896
92897
92898
92899
92900
92901
92902
92903
92904
92905
92906
92907
92908
92909
92910
92911
92912
92913
92914
92915
92916
92917
92918
92919
92920
92921
92922
92923
92924
92925
92926
92927
92928
92929
92930
92931
92932
92933
92934
92935
92936
92937
92938
92939
92940
92941
92942
92943
92944
92945
92946
92947
92948
92949
92950
92951
92952
92953
92954
92955
92956
92957
92958
92959
92960
92961
92962
92963
92964
92965
92966
92967
92968
92969
92970
92971
92972
92973
92974
92975
92976
92977
92978
92979
92980
92981
92982
92983
92984
92985
92986
92987
92988
92989
92990
92991
92992
92993
92994
92995
92996
92997
92998
92999
93000
93001
93002
93003
93004
93005
93006
93007
93008
93009
93010
93011
93012
93013
93014
93015
93016
93017
93018
93019
93020
93021
93022
93023
93024
93025
93026
93027
93028
93029
93030
93031
93032
93033
93034
93035
93036
93037
93038
93039
93040
93041
93042
93043
93044
93045
93046
93047
93048
93049
93050
93051
93052
93053
93054
93055
93056
93057
93058
93059
93060
93061
93062
93063
93064
93065
93066
93067
93068
93069
93070
93071
93072
93073
93074
93075
93076
93077
93078
93079
93080
93081
93082
93083
93084
93085
93086
93087
93088
93089
93090
93091
93092
93093
93094
93095
93096
93097
93098
93099
93100
93101
93102
93103
93104
93105
93106
93107
93108
93109
93110
93111
93112
93113
93114
93115
93116
93117
93118
93119
93120
93121
93122
93123
93124
93125
93126
93127
93128
93129
93130
93131
93132
93133
93134
93135
93136
93137
93138
93139
93140
93141
93142
93143
93144
93145
93146
93147
93148
93149
93150
93151
93152
93153
93154
93155
93156
93157
93158
93159
93160
93161
93162
93163
93164
93165
93166
93167
93168
93169
93170
93171
93172
93173
93174
93175
93176
93177
93178
93179
93180
93181
93182
93183
93184
93185
93186
93187
93188
93189
93190
93191
93192
93193
93194
93195
93196
93197
93198
93199
93200
93201
93202
93203
93204
93205
93206
93207
93208
93209
93210
93211
93212
93213
93214
93215
93216
93217
93218
93219
93220
93221
93222
93223
93224
93225
93226
93227
93228
93229
93230
93231
93232
93233
93234
93235
93236
93237
93238
93239
93240
93241
93242
93243
93244
93245
93246
93247
93248
93249
93250
93251
93252
93253
93254
93255
93256
93257
93258
93259
93260
93261
93262
93263
93264
93265
93266
93267
93268
93269
93270
93271
93272
93273
93274
93275
93276
93277
93278
93279
93280
93281
93282
93283
93284
93285
93286
93287
93288
93289
93290
93291
93292
93293
93294
93295
93296
93297
93298
93299
93300
93301
93302
93303
93304
93305
93306
93307
93308
93309
93310
93311
93312
93313
93314
93315
93316
93317
93318
93319
93320
93321
93322
93323
93324
93325
93326
93327
93328
93329
93330
93331
93332
93333
93334
93335
93336
93337
93338
93339
93340
93341
93342
93343
93344
93345
93346
93347
93348
93349
93350
93351
93352
93353
93354
93355
93356
93357
93358
93359
93360
93361
93362
93363
93364
93365
93366
93367
93368
93369
93370
93371
93372
93373
93374
93375
93376
93377
93378
93379
93380
93381
93382
93383
93384
93385
93386
93387
93388
93389
93390
93391
93392
93393
93394
93395
93396
93397
93398
93399
93400
93401
93402
93403
93404
93405
93406
93407
93408
93409
93410
93411
93412
93413
93414
93415
93416
93417
93418
93419
93420
93421
93422
93423
93424
93425
93426
93427
93428
93429
93430
93431
93432
93433
93434
93435
93436
93437
93438
93439
93440
93441
93442
93443
93444
93445
93446
93447
93448
93449
93450
93451
93452
93453
93454
93455
93456
93457
93458
93459
93460
93461
93462
93463
93464
93465
93466
93467
93468
93469
93470
93471
93472
93473
93474
93475
93476
93477
93478
93479
93480
93481
93482
93483
93484
93485
93486
93487
93488
93489
93490
93491
93492
93493
93494
93495
93496
93497
93498
93499
93500
93501
93502
93503
93504
93505
93506
93507
93508
93509
93510
93511
93512
93513
93514
93515
93516
93517
93518
93519
93520
93521
93522
93523
93524
93525
93526
93527
93528
93529
93530
93531
93532
93533
93534
93535
93536
93537
93538
93539
93540
93541
93542
93543
93544
93545
93546
93547
93548
93549
93550
93551
93552
93553
93554
93555
93556
93557
93558
93559
93560
93561
93562
93563
93564
93565
93566
93567
93568
93569
93570
93571
93572
93573
93574
93575
93576
93577
93578
93579
93580
93581
93582
93583
93584
93585
93586
93587
93588
93589
93590
93591
93592
93593
93594
93595
93596
93597
93598
93599
93600
93601
93602
93603
93604
93605
93606
93607
93608
93609
93610
93611
93612
93613
93614
93615
93616
93617
93618
93619
93620
93621
93622
93623
93624
93625
93626
93627
93628
93629
93630
93631
93632
93633
93634
93635
93636
93637
93638
93639
93640
93641
93642
93643
93644
93645
93646
93647
93648
93649
93650
93651
93652
93653
93654
93655
93656
93657
93658
93659
93660
93661
93662
93663
93664
93665
93666
93667
93668
93669
93670
93671
93672
93673
93674
93675
93676
93677
93678
93679
93680
93681
93682
93683
93684
93685
93686
93687
93688
93689
93690
93691
93692
93693
93694
93695
93696
93697
93698
93699
93700
93701
93702
93703
93704
93705
93706
93707
93708
93709
93710
93711
93712
93713
93714
93715
93716
93717
93718
93719
93720
93721
93722
93723
93724
93725
93726
93727
93728
93729
93730
93731
93732
93733
93734
93735
93736
93737
93738
93739
93740
93741
93742
93743
93744
93745
93746
93747
93748
93749
93750
93751
93752
93753
93754
93755
93756
93757
93758
93759
93760
93761
93762
93763
93764
93765
93766
93767
93768
93769
93770
93771
93772
93773
93774
93775
93776
93777
93778
93779
93780
93781
93782
93783
93784
93785
93786
93787
93788
93789
93790
93791
93792
93793
93794
93795
93796
93797
93798
93799
93800
93801
93802
93803
93804
93805
93806
93807
93808
93809
93810
93811
93812
93813
93814
93815
93816
93817
93818
93819
93820
93821
93822
93823
93824
93825
93826
93827
93828
93829
93830
93831
93832
93833
93834
93835
93836
93837
93838
93839
93840
93841
93842
93843
93844
93845
93846
93847
93848
93849
93850
93851
93852
93853
93854
93855
93856
93857
93858
93859
93860
93861
93862
93863
93864
93865
93866
93867
93868
93869
93870
93871
93872
93873
93874
93875
93876
93877
93878
93879
93880
93881
93882
93883
93884
93885
93886
93887
93888
93889
93890
93891
93892
93893
93894
93895
93896
93897
93898
93899
93900
93901
93902
93903
93904
93905
93906
93907
93908
93909
93910
93911
93912
93913
93914
93915
93916
93917
93918
93919
93920
93921
93922
93923
93924
93925
93926
93927
93928
93929
93930
93931
93932
93933
93934
93935
93936
93937
93938
93939
93940
93941
93942
93943
93944
93945
93946
93947
93948
93949
93950
93951
93952
93953
93954
93955
93956
93957
93958
93959
93960
93961
93962
93963
93964
93965
93966
93967
93968
93969
93970
93971
93972
93973
93974
93975
93976
93977
93978
93979
93980
93981
93982
93983
93984
93985
93986
93987
93988
93989
93990
93991
93992
93993
93994
93995
93996
93997
93998
93999
94000
94001
94002
94003
94004
94005
94006
94007
94008
94009
94010
94011
94012
94013
94014
94015
94016
94017
94018
94019
94020
94021
94022
94023
94024
94025
94026
94027
94028
94029
94030
94031
94032
94033
94034
94035
94036
94037
94038
94039
94040
94041
94042
94043
94044
94045
94046
94047
94048
94049
94050
94051
94052
94053
94054
94055
94056
94057
94058
94059
94060
94061
94062
94063
94064
94065
94066
94067
94068
94069
94070
94071
94072
94073
94074
94075
94076
94077
94078
94079
94080
94081
94082
94083
94084
94085
94086
94087
94088
94089
94090
94091
94092
94093
94094
94095
94096
94097
94098
94099
94100
94101
94102
94103
94104
94105
94106
94107
94108
94109
94110
94111
94112
94113
94114
94115
94116
94117
94118
94119
94120
94121
94122
94123
94124
94125
94126
94127
94128
94129
94130
94131
94132
94133
94134
94135
94136
94137
94138
94139
94140
94141
94142
94143
94144
94145
94146
94147
94148
94149
94150
94151
94152
94153
94154
94155
94156
94157
94158
94159
94160
94161
94162
94163
94164
94165
94166
94167
94168
94169
94170
94171
94172
94173
94174
94175
94176
94177
94178
94179
94180
94181
94182
94183
94184
94185
94186
94187
94188
94189
94190
94191
94192
94193
94194
94195
94196
94197
94198
94199
94200
94201
94202
94203
94204
94205
94206
94207
94208
94209
94210
94211
94212
94213
94214
94215
94216
94217
94218
94219
94220
94221
94222
94223
94224
94225
94226
94227
94228
94229
94230
94231
94232
94233
94234
94235
94236
94237
94238
94239
94240
94241
94242
94243
94244
94245
94246
94247
94248
94249
94250
94251
94252
94253
94254
94255
94256
94257
94258
94259
94260
94261
94262
94263
94264
94265
94266
94267
94268
94269
94270
94271
94272
94273
94274
94275
94276
94277
94278
94279
94280
94281
94282
94283
94284
94285
94286
94287
94288
94289
94290
94291
94292
94293
94294
94295
94296
94297
94298
94299
94300
94301
94302
94303
94304
94305
94306
94307
94308
94309
94310
94311
94312
94313
94314
94315
94316
94317
94318
94319
94320
94321
94322
94323
94324
94325
94326
94327
94328
94329
94330
94331
94332
94333
94334
94335
94336
94337
94338
94339
94340
94341
94342
94343
94344
94345
94346
94347
94348
94349
94350
94351
94352
94353
94354
94355
94356
94357
94358
94359
94360
94361
94362
94363
94364
94365
94366
94367
94368
94369
94370
94371
94372
94373
94374
94375
94376
94377
94378
94379
94380
94381
94382
94383
94384
94385
94386
94387
94388
94389
94390
94391
94392
94393
94394
94395
94396
94397
94398
94399
94400
94401
94402
94403
94404
94405
94406
94407
94408
94409
94410
94411
94412
94413
94414
94415
94416
94417
94418
94419
94420
94421
94422
94423
94424
94425
94426
94427
94428
94429
94430
94431
94432
94433
94434
94435
94436
94437
94438
94439
94440
94441
94442
94443
94444
94445
94446
94447
94448
94449
94450
94451
94452
94453
94454
94455
94456
94457
94458
94459
94460
94461
94462
94463
94464
94465
94466
94467
94468
94469
94470
94471
94472
94473
94474
94475
94476
94477
94478
94479
94480
94481
94482
94483
94484
94485
94486
94487
94488
94489
94490
94491
94492
94493
94494
94495
94496
94497
94498
94499
94500
94501
94502
94503
94504
94505
94506
94507
94508
94509
94510
94511
94512
94513
94514
94515
94516
94517
94518
94519
94520
94521
94522
94523
94524
94525
94526
94527
94528
94529
94530
94531
94532
94533
94534
94535
94536
94537
94538
94539
94540
94541
94542
94543
94544
94545
94546
94547
94548
94549
94550
94551
94552
94553
94554
94555
94556
94557
94558
94559
94560
94561
94562
94563
94564
94565
94566
94567
94568
94569
94570
94571
94572
94573
94574
94575
94576
94577
94578
94579
94580
94581
94582
94583
94584
94585
94586
94587
94588
94589
94590
94591
94592
94593
94594
94595
94596
94597
94598
94599
94600
94601
94602
94603
94604
94605
94606
94607
94608
94609
94610
94611
94612
94613
94614
94615
94616
94617
94618
94619
94620
94621
94622
94623
94624
94625
94626
94627
94628
94629
94630
94631
94632
94633
94634
94635
94636
94637
94638
94639
94640
94641
94642
94643
94644
94645
94646
94647
94648
94649
94650
94651
94652
94653
94654
94655
94656
94657
94658
94659
94660
94661
94662
94663
94664
94665
94666
94667
94668
94669
94670
94671
94672
94673
94674
94675
94676
94677
94678
94679
94680
94681
94682
94683
94684
94685
94686
94687
94688
94689
94690
94691
94692
94693
94694
94695
94696
94697
94698
94699
94700
94701
94702
94703
94704
94705
94706
94707
94708
94709
94710
94711
94712
94713
94714
94715
94716
94717
94718
94719
94720
94721
94722
94723
94724
94725
94726
94727
94728
94729
94730
94731
94732
94733
94734
94735
94736
94737
94738
94739
94740
94741
94742
94743
94744
94745
94746
94747
94748
94749
94750
94751
94752
94753
94754
94755
94756
94757
94758
94759
94760
94761
94762
94763
94764
94765
94766
94767
94768
94769
94770
94771
94772
94773
94774
94775
94776
94777
94778
94779
94780
94781
94782
94783
94784
94785
94786
94787
94788
94789
94790
94791
94792
94793
94794
94795
94796
94797
94798
94799
94800
94801
94802
94803
94804
94805
94806
94807
94808
94809
94810
94811
94812
94813
94814
94815
94816
94817
94818
94819
94820
94821
94822
94823
94824
94825
94826
94827
94828
94829
94830
94831
94832
94833
94834
94835
94836
94837
94838
94839
94840
94841
94842
94843
94844
94845
94846
94847
94848
94849
94850
94851
94852
94853
94854
94855
94856
94857
94858
94859
94860
94861
94862
94863
94864
94865
94866
94867
94868
94869
94870
94871
94872
94873
94874
94875
94876
94877
94878
94879
94880
94881
94882
94883
94884
94885
94886
94887
94888
94889
94890
94891
94892
94893
94894
94895
94896
94897
94898
94899
94900
94901
94902
94903
94904
94905
94906
94907
94908
94909
94910
94911
94912
94913
94914
94915
94916
94917
94918
94919
94920
94921
94922
94923
94924
94925
94926
94927
94928
94929
94930
94931
94932
94933
94934
94935
94936
94937
94938
94939
94940
94941
94942
94943
94944
94945
94946
94947
94948
94949
94950
94951
94952
94953
94954
94955
94956
94957
94958
94959
94960
94961
94962
94963
94964
94965
94966
94967
94968
94969
94970
94971
94972
94973
94974
94975
94976
94977
94978
94979
94980
94981
94982
94983
94984
94985
94986
94987
94988
94989
94990
94991
94992
94993
94994
94995
94996
94997
94998
94999
95000
95001
95002
95003
95004
95005
95006
95007
95008
95009
95010
95011
95012
95013
95014
95015
95016
95017
95018
95019
95020
95021
95022
95023
95024
95025
95026
95027
95028
95029
95030
95031
95032
95033
95034
95035
95036
95037
95038
95039
95040
95041
95042
95043
95044
95045
95046
95047
95048
95049
95050
95051
95052
95053
95054
95055
95056
95057
95058
95059
95060
95061
95062
95063
95064
95065
95066
95067
95068
95069
95070
95071
95072
95073
95074
95075
95076
95077
95078
95079
95080
95081
95082
95083
95084
95085
95086
95087
95088
95089
95090
95091
95092
95093
95094
95095
95096
95097
95098
95099
95100
95101
95102
95103
95104
95105
95106
95107
95108
95109
95110
95111
95112
95113
95114
95115
95116
95117
95118
95119
95120
95121
95122
95123
95124
95125
95126
95127
95128
95129
95130
95131
95132
95133
95134
95135
95136
95137
95138
95139
95140
95141
95142
95143
95144
95145
95146
95147
95148
95149
95150
95151
95152
95153
95154
95155
95156
95157
95158
95159
95160
95161
95162
95163
95164
95165
95166
95167
95168
95169
95170
95171
95172
95173
95174
95175
95176
95177
95178
95179
95180
95181
95182
95183
95184
95185
95186
95187
95188
95189
95190
95191
95192
95193
95194
95195
95196
95197
95198
95199
95200
95201
95202
95203
95204
95205
95206
95207
95208
95209
95210
95211
95212
95213
95214
95215
95216
95217
95218
95219
95220
95221
95222
95223
95224
95225
95226
95227
95228
95229
95230
95231
95232
95233
95234
95235
95236
95237
95238
95239
95240
95241
95242
95243
95244
95245
95246
95247
95248
95249
95250
95251
95252
95253
95254
95255
95256
95257
95258
95259
95260
95261
95262
95263
95264
95265
95266
95267
95268
95269
95270
95271
95272
95273
95274
95275
95276
95277
95278
95279
95280
95281
95282
95283
95284
95285
95286
95287
95288
95289
95290
95291
95292
95293
95294
95295
95296
95297
95298
95299
95300
95301
95302
95303
95304
95305
95306
95307
95308
95309
95310
95311
95312
95313
95314
95315
95316
95317
95318
95319
95320
95321
95322
95323
95324
95325
95326
95327
95328
95329
95330
95331
95332
95333
95334
95335
95336
95337
95338
95339
95340
95341
95342
95343
95344
95345
95346
95347
95348
95349
95350
95351
95352
95353
95354
95355
95356
95357
95358
95359
95360
95361
95362
95363
95364
95365
95366
95367
95368
95369
95370
95371
95372
95373
95374
95375
95376
95377
95378
95379
95380
95381
95382
95383
95384
95385
95386
95387
95388
95389
95390
95391
95392
95393
95394
95395
95396
95397
95398
95399
95400
95401
95402
95403
95404
95405
95406
95407
95408
95409
95410
95411
95412
95413
95414
95415
95416
95417
95418
95419
95420
95421
95422
95423
95424
95425
95426
95427
95428
95429
95430
95431
95432
95433
95434
95435
95436
95437
95438
95439
95440
95441
95442
95443
95444
95445
95446
95447
95448
95449
95450
95451
95452
95453
95454
95455
95456
95457
95458
95459
95460
95461
95462
95463
95464
95465
95466
95467
95468
95469
95470
95471
95472
95473
95474
95475
95476
95477
95478
95479
95480
95481
95482
95483
95484
95485
95486
95487
95488
95489
95490
95491
95492
95493
95494
95495
95496
95497
95498
95499
95500
95501
95502
95503
95504
95505
95506
95507
95508
95509
95510
95511
95512
95513
95514
95515
95516
95517
95518
95519
95520
95521
95522
95523
95524
95525
95526
95527
95528
95529
95530
95531
95532
95533
95534
95535
95536
95537
95538
95539
95540
95541
95542
95543
95544
95545
95546
95547
95548
95549
95550
95551
95552
95553
95554
95555
95556
95557
95558
95559
95560
95561
95562
95563
95564
95565
95566
95567
95568
95569
95570
95571
95572
95573
95574
95575
95576
95577
95578
95579
95580
95581
95582
95583
95584
95585
95586
95587
95588
95589
95590
95591
95592
95593
95594
95595
95596
95597
95598
95599
95600
95601
95602
95603
95604
95605
95606
95607
95608
95609
95610
95611
95612
95613
95614
95615
95616
95617
95618
95619
95620
95621
95622
95623
95624
95625
95626
95627
95628
95629
95630
95631
95632
95633
95634
95635
95636
95637
95638
95639
95640
95641
95642
95643
95644
95645
95646
95647
95648
95649
95650
95651
95652
95653
95654
95655
95656
95657
95658
95659
95660
95661
95662
95663
95664
95665
95666
95667
95668
95669
95670
95671
95672
95673
95674
95675
95676
95677
95678
95679
95680
95681
95682
95683
95684
95685
95686
95687
95688
95689
95690
95691
95692
95693
95694
95695
95696
95697
95698
95699
95700
95701
95702
95703
95704
95705
95706
95707
95708
95709
95710
95711
95712
95713
95714
95715
95716
95717
95718
95719
95720
95721
95722
95723
95724
95725
95726
95727
95728
95729
95730
95731
95732
95733
95734
95735
95736
95737
95738
95739
95740
95741
95742
95743
95744
95745
95746
95747
95748
95749
95750
95751
95752
95753
95754
95755
95756
95757
95758
95759
95760
95761
95762
95763
95764
95765
95766
95767
95768
95769
95770
95771
95772
95773
95774
95775
95776
95777
95778
95779
95780
95781
95782
95783
95784
95785
95786
95787
95788
95789
95790
95791
95792
95793
95794
95795
95796
95797
95798
95799
95800
95801
95802
95803
95804
95805
95806
95807
95808
95809
95810
95811
95812
95813
95814
95815
95816
95817
95818
95819
95820
95821
95822
95823
95824
95825
95826
95827
95828
95829
95830
95831
95832
95833
95834
95835
95836
95837
95838
95839
95840
95841
95842
95843
95844
95845
95846
95847
95848
95849
95850
95851
95852
95853
95854
95855
95856
95857
95858
95859
95860
95861
95862
95863
95864
95865
95866
95867
95868
95869
95870
95871
95872
95873
95874
95875
95876
95877
95878
95879
95880
95881
95882
95883
95884
95885
95886
95887
95888
95889
95890
95891
95892
95893
95894
95895
95896
95897
95898
95899
95900
95901
95902
95903
95904
95905
95906
95907
95908
95909
95910
95911
95912
95913
95914
95915
95916
95917
95918
95919
95920
95921
95922
95923
95924
95925
95926
95927
95928
95929
95930
95931
95932
95933
95934
95935
95936
95937
95938
95939
95940
95941
95942
95943
95944
95945
95946
95947
95948
95949
95950
95951
95952
95953
95954
95955
95956
95957
95958
95959
95960
95961
95962
95963
95964
95965
95966
95967
95968
95969
95970
95971
95972
95973
95974
95975
95976
95977
95978
95979
95980
95981
95982
95983
95984
95985
95986
95987
95988
95989
95990
95991
95992
95993
95994
95995
95996
95997
95998
95999
96000
96001
96002
96003
96004
96005
96006
96007
96008
96009
96010
96011
96012
96013
96014
96015
96016
96017
96018
96019
96020
96021
96022
96023
96024
96025
96026
96027
96028
96029
96030
96031
96032
96033
96034
96035
96036
96037
96038
96039
96040
96041
96042
96043
96044
96045
96046
96047
96048
96049
96050
96051
96052
96053
96054
96055
96056
96057
96058
96059
96060
96061
96062
96063
96064
96065
96066
96067
96068
96069
96070
96071
96072
96073
96074
96075
96076
96077
96078
96079
96080
96081
96082
96083
96084
96085
96086
96087
96088
96089
96090
96091
96092
96093
96094
96095
96096
96097
96098
96099
96100
96101
96102
96103
96104
96105
96106
96107
96108
96109
96110
96111
96112
96113
96114
96115
96116
96117
96118
96119
96120
96121
96122
96123
96124
96125
96126
96127
96128
96129
96130
96131
96132
96133
96134
96135
96136
96137
96138
96139
96140
96141
96142
96143
96144
96145
96146
96147
96148
96149
96150
96151
96152
96153
96154
96155
96156
96157
96158
96159
96160
96161
96162
96163
96164
96165
96166
96167
96168
96169
96170
96171
96172
96173
96174
96175
96176
96177
96178
96179
96180
96181
96182
96183
96184
96185
96186
96187
96188
96189
96190
96191
96192
96193
96194
96195
96196
96197
96198
96199
96200
96201
96202
96203
96204
96205
96206
96207
96208
96209
96210
96211
96212
96213
96214
96215
96216
96217
96218
96219
96220
96221
96222
96223
96224
96225
96226
96227
96228
96229
96230
96231
96232
96233
96234
96235
96236
96237
96238
96239
96240
96241
96242
96243
96244
96245
96246
96247
96248
96249
96250
96251
96252
96253
96254
96255
96256
96257
96258
96259
96260
96261
96262
96263
96264
96265
96266
96267
96268
96269
96270
96271
96272
96273
96274
96275
96276
96277
96278
96279
96280
96281
96282
96283
96284
96285
96286
96287
96288
96289
96290
96291
96292
96293
96294
96295
96296
96297
96298
96299
96300
96301
96302
96303
96304
96305
96306
96307
96308
96309
96310
96311
96312
96313
96314
96315
96316
96317
96318
96319
96320
96321
96322
96323
96324
96325
96326
96327
96328
96329
96330
96331
96332
96333
96334
96335
96336
96337
96338
96339
96340
96341
96342
96343
96344
96345
96346
96347
96348
96349
96350
96351
96352
96353
96354
96355
96356
96357
96358
96359
96360
96361
96362
96363
96364
96365
96366
96367
96368
96369
96370
96371
96372
96373
96374
96375
96376
96377
96378
96379
96380
96381
96382
96383
96384
96385
96386
96387
96388
96389
96390
96391
96392
96393
96394
96395
96396
96397
96398
96399
96400
96401
96402
96403
96404
96405
96406
96407
96408
96409
96410
96411
96412
96413
96414
96415
96416
96417
96418
96419
96420
96421
96422
96423
96424
96425
96426
96427
96428
96429
96430
96431
96432
96433
96434
96435
96436
96437
96438
96439
96440
96441
96442
96443
96444
96445
96446
96447
96448
96449
96450
96451
96452
96453
96454
96455
96456
96457
96458
96459
96460
96461
96462
96463
96464
96465
96466
96467
96468
96469
96470
96471
96472
96473
96474
96475
96476
96477
96478
96479
96480
96481
96482
96483
96484
96485
96486
96487
96488
96489
96490
96491
96492
96493
96494
96495
96496
96497
96498
96499
96500
96501
96502
96503
96504
96505
96506
96507
96508
96509
96510
96511
96512
96513
96514
96515
96516
96517
96518
96519
96520
96521
96522
96523
96524
96525
96526
96527
96528
96529
96530
96531
96532
96533
96534
96535
96536
96537
96538
96539
96540
96541
96542
96543
96544
96545
96546
96547
96548
96549
96550
96551
96552
96553
96554
96555
96556
96557
96558
96559
96560
96561
96562
96563
96564
96565
96566
96567
96568
96569
96570
96571
96572
96573
96574
96575
96576
96577
96578
96579
96580
96581
96582
96583
96584
96585
96586
96587
96588
96589
96590
96591
96592
96593
96594
96595
96596
96597
96598
96599
96600
96601
96602
96603
96604
96605
96606
96607
96608
96609
96610
96611
96612
96613
96614
96615
96616
96617
96618
96619
96620
96621
96622
96623
96624
96625
96626
96627
96628
96629
96630
96631
96632
96633
96634
96635
96636
96637
96638
96639
96640
96641
96642
96643
96644
96645
96646
96647
96648
96649
96650
96651
96652
96653
96654
96655
96656
96657
96658
96659
96660
96661
96662
96663
96664
96665
96666
96667
96668
96669
96670
96671
96672
96673
96674
96675
96676
96677
96678
96679
96680
96681
96682
96683
96684
96685
96686
96687
96688
96689
96690
96691
96692
96693
96694
96695
96696
96697
96698
96699
96700
96701
96702
96703
96704
96705
96706
96707
96708
96709
96710
96711
96712
96713
96714
96715
96716
96717
96718
96719
96720
96721
96722
96723
96724
96725
96726
96727
96728
96729
96730
96731
96732
96733
96734
96735
96736
96737
96738
96739
96740
96741
96742
96743
96744
96745
96746
96747
96748
96749
96750
96751
96752
96753
96754
96755
96756
96757
96758
96759
96760
96761
96762
96763
96764
96765
96766
96767
96768
96769
96770
96771
96772
96773
96774
96775
96776
96777
96778
96779
96780
96781
96782
96783
96784
96785
96786
96787
96788
96789
96790
96791
96792
96793
96794
96795
96796
96797
96798
96799
96800
96801
96802
96803
96804
96805
96806
96807
96808
96809
96810
96811
96812
96813
96814
96815
96816
96817
96818
96819
96820
96821
96822
96823
96824
96825
96826
96827
96828
96829
96830
96831
96832
96833
96834
96835
96836
96837
96838
96839
96840
96841
96842
96843
96844
96845
96846
96847
96848
96849
96850
96851
96852
96853
96854
96855
96856
96857
96858
96859
96860
96861
96862
96863
96864
96865
96866
96867
96868
96869
96870
96871
96872
96873
96874
96875
96876
96877
96878
96879
96880
96881
96882
96883
96884
96885
96886
96887
96888
96889
96890
96891
96892
96893
96894
96895
96896
96897
96898
96899
96900
96901
96902
96903
96904
96905
96906
96907
96908
96909
96910
96911
96912
96913
96914
96915
96916
96917
96918
96919
96920
96921
96922
96923
96924
96925
96926
96927
96928
96929
96930
96931
96932
96933
96934
96935
96936
96937
96938
96939
96940
96941
96942
96943
96944
96945
96946
96947
96948
96949
96950
96951
96952
96953
96954
96955
96956
96957
96958
96959
96960
96961
96962
96963
96964
96965
96966
96967
96968
96969
96970
96971
96972
96973
96974
96975
96976
96977
96978
96979
96980
96981
96982
96983
96984
96985
96986
96987
96988
96989
96990
96991
96992
96993
96994
96995
96996
96997
96998
96999
97000
97001
97002
97003
97004
97005
97006
97007
97008
97009
97010
97011
97012
97013
97014
97015
97016
97017
97018
97019
97020
97021
97022
97023
97024
97025
97026
97027
97028
97029
97030
97031
97032
97033
97034
97035
97036
97037
97038
97039
97040
97041
97042
97043
97044
97045
97046
97047
97048
97049
97050
97051
97052
97053
97054
97055
97056
97057
97058
97059
97060
97061
97062
97063
97064
97065
97066
97067
97068
97069
97070
97071
97072
97073
97074
97075
97076
97077
97078
97079
97080
97081
97082
97083
97084
97085
97086
97087
97088
97089
97090
97091
97092
97093
97094
97095
97096
97097
97098
97099
97100
97101
97102
97103
97104
97105
97106
97107
97108
97109
97110
97111
97112
97113
97114
97115
97116
97117
97118
97119
97120
97121
97122
97123
97124
97125
97126
97127
97128
97129
97130
97131
97132
97133
97134
97135
97136
97137
97138
97139
97140
97141
97142
97143
97144
97145
97146
97147
97148
97149
97150
97151
97152
97153
97154
97155
97156
97157
97158
97159
97160
97161
97162
97163
97164
97165
97166
97167
97168
97169
97170
97171
97172
97173
97174
97175
97176
97177
97178
97179
97180
97181
97182
97183
97184
97185
97186
97187
97188
97189
97190
97191
97192
97193
97194
97195
97196
97197
97198
97199
97200
97201
97202
97203
97204
97205
97206
97207
97208
97209
97210
97211
97212
97213
97214
97215
97216
97217
97218
97219
97220
97221
97222
97223
97224
97225
97226
97227
97228
97229
97230
97231
97232
97233
97234
97235
97236
97237
97238
97239
97240
97241
97242
97243
97244
97245
97246
97247
97248
97249
97250
97251
97252
97253
97254
97255
97256
97257
97258
97259
97260
97261
97262
97263
97264
97265
97266
97267
97268
97269
97270
97271
97272
97273
97274
97275
97276
97277
97278
97279
97280
97281
97282
97283
97284
97285
97286
97287
97288
97289
97290
97291
97292
97293
97294
97295
97296
97297
97298
97299
97300
97301
97302
97303
97304
97305
97306
97307
97308
97309
97310
97311
97312
97313
97314
97315
97316
97317
97318
97319
97320
97321
97322
97323
97324
97325
97326
97327
97328
97329
97330
97331
97332
97333
97334
97335
97336
97337
97338
97339
97340
97341
97342
97343
97344
97345
97346
97347
97348
97349
97350
97351
97352
97353
97354
97355
97356
97357
97358
97359
97360
97361
97362
97363
97364
97365
97366
97367
97368
97369
97370
97371
97372
97373
97374
97375
97376
97377
97378
97379
97380
97381
97382
97383
97384
97385
97386
97387
97388
97389
97390
97391
97392
97393
97394
97395
97396
97397
97398
97399
97400
97401
97402
97403
97404
97405
97406
97407
97408
97409
97410
97411
97412
97413
97414
97415
97416
97417
97418
97419
97420
97421
97422
97423
97424
97425
97426
97427
97428
97429
97430
97431
97432
97433
97434
97435
97436
97437
97438
97439
97440
97441
97442
97443
97444
97445
97446
97447
97448
97449
97450
97451
97452
97453
97454
97455
97456
97457
97458
97459
97460
97461
97462
97463
97464
97465
97466
97467
97468
97469
97470
97471
97472
97473
97474
97475
97476
97477
97478
97479
97480
97481
97482
97483
97484
97485
97486
97487
97488
97489
97490
97491
97492
97493
97494
97495
97496
97497
97498
97499
97500
97501
97502
97503
97504
97505
97506
97507
97508
97509
97510
97511
97512
97513
97514
97515
97516
97517
97518
97519
97520
97521
97522
97523
97524
97525
97526
97527
97528
97529
97530
97531
97532
97533
97534
97535
97536
97537
97538
97539
97540
97541
97542
97543
97544
97545
97546
97547
97548
97549
97550
97551
97552
97553
97554
97555
97556
97557
97558
97559
97560
97561
97562
97563
97564
97565
97566
97567
97568
97569
97570
97571
97572
97573
97574
97575
97576
97577
97578
97579
97580
97581
97582
97583
97584
97585
97586
97587
97588
97589
97590
97591
97592
97593
97594
97595
97596
97597
97598
97599
97600
97601
97602
97603
97604
97605
97606
97607
97608
97609
97610
97611
97612
97613
97614
97615
97616
97617
97618
97619
97620
97621
97622
97623
97624
97625
97626
97627
97628
97629
97630
97631
97632
97633
97634
97635
97636
97637
97638
97639
97640
97641
97642
97643
97644
97645
97646
97647
97648
97649
97650
97651
97652
97653
97654
97655
97656
97657
97658
97659
97660
97661
97662
97663
97664
97665
97666
97667
97668
97669
97670
97671
97672
97673
97674
97675
97676
97677
97678
97679
97680
97681
97682
97683
97684
97685
97686
97687
97688
97689
97690
97691
97692
97693
97694
97695
97696
97697
97698
97699
97700
97701
97702
97703
97704
97705
97706
97707
97708
97709
97710
97711
97712
97713
97714
97715
97716
97717
97718
97719
97720
97721
97722
97723
97724
97725
97726
97727
97728
97729
97730
97731
97732
97733
97734
97735
97736
97737
97738
97739
97740
97741
97742
97743
97744
97745
97746
97747
97748
97749
97750
97751
97752
97753
97754
97755
97756
97757
97758
97759
97760
97761
97762
97763
97764
97765
97766
97767
97768
97769
97770
97771
97772
97773
97774
97775
97776
97777
97778
97779
97780
97781
97782
97783
97784
97785
97786
97787
97788
97789
97790
97791
97792
97793
97794
97795
97796
97797
97798
97799
97800
97801
97802
97803
97804
97805
97806
97807
97808
97809
97810
97811
97812
97813
97814
97815
97816
97817
97818
97819
97820
97821
97822
97823
97824
97825
97826
97827
97828
97829
97830
97831
97832
97833
97834
97835
97836
97837
97838
97839
97840
97841
97842
97843
97844
97845
97846
97847
97848
97849
97850
97851
97852
97853
97854
97855
97856
97857
97858
97859
97860
97861
97862
97863
97864
97865
97866
97867
97868
97869
97870
97871
97872
97873
97874
97875
97876
97877
97878
97879
97880
97881
97882
97883
97884
97885
97886
97887
97888
97889
97890
97891
97892
97893
97894
97895
97896
97897
97898
97899
97900
97901
97902
97903
97904
97905
97906
97907
97908
97909
97910
97911
97912
97913
97914
97915
97916
97917
97918
97919
97920
97921
97922
97923
97924
97925
97926
97927
97928
97929
97930
97931
97932
97933
97934
97935
97936
97937
97938
97939
97940
97941
97942
97943
97944
97945
97946
97947
97948
97949
97950
97951
97952
97953
97954
97955
97956
97957
97958
97959
97960
97961
97962
97963
97964
97965
97966
97967
97968
97969
97970
97971
97972
97973
97974
97975
97976
97977
97978
97979
97980
97981
97982
97983
97984
97985
97986
97987
97988
97989
97990
97991
97992
97993
97994
97995
97996
97997
97998
97999
98000
98001
98002
98003
98004
98005
98006
98007
98008
98009
98010
98011
98012
98013
98014
98015
98016
98017
98018
98019
98020
98021
98022
98023
98024
98025
98026
98027
98028
98029
98030
98031
98032
98033
98034
98035
98036
98037
98038
98039
98040
98041
98042
98043
98044
98045
98046
98047
98048
98049
98050
98051
98052
98053
98054
98055
98056
98057
98058
98059
98060
98061
98062
98063
98064
98065
98066
98067
98068
98069
98070
98071
98072
98073
98074
98075
98076
98077
98078
98079
98080
98081
98082
98083
98084
98085
98086
98087
98088
98089
98090
98091
98092
98093
98094
98095
98096
98097
98098
98099
98100
98101
98102
98103
98104
98105
98106
98107
98108
98109
98110
98111
98112
98113
98114
98115
98116
98117
98118
98119
98120
98121
98122
98123
98124
98125
98126
98127
98128
98129
98130
98131
98132
98133
98134
98135
98136
98137
98138
98139
98140
98141
98142
98143
98144
98145
98146
98147
98148
98149
98150
98151
98152
98153
98154
98155
98156
98157
98158
98159
98160
98161
98162
98163
98164
98165
98166
98167
98168
98169
98170
98171
98172
98173
98174
98175
98176
98177
98178
98179
98180
98181
98182
98183
98184
98185
98186
98187
98188
98189
98190
98191
98192
98193
98194
98195
98196
98197
98198
98199
98200
98201
98202
98203
98204
98205
98206
98207
98208
98209
98210
98211
98212
98213
98214
98215
98216
98217
98218
98219
98220
98221
98222
98223
98224
98225
98226
98227
98228
98229
98230
98231
98232
98233
98234
98235
98236
98237
98238
98239
98240
98241
98242
98243
98244
98245
98246
98247
98248
98249
98250
98251
98252
98253
98254
98255
98256
98257
98258
98259
98260
98261
98262
98263
98264
98265
98266
98267
98268
98269
98270
98271
98272
98273
98274
98275
98276
98277
98278
98279
98280
98281
98282
98283
98284
98285
98286
98287
98288
98289
98290
98291
98292
98293
98294
98295
98296
98297
98298
98299
98300
98301
98302
98303
98304
98305
98306
98307
98308
98309
98310
98311
98312
98313
98314
98315
98316
98317
98318
98319
98320
98321
98322
98323
98324
98325
98326
98327
98328
98329
98330
98331
98332
98333
98334
98335
98336
98337
98338
98339
98340
98341
98342
98343
98344
98345
98346
98347
98348
98349
98350
98351
98352
98353
98354
98355
98356
98357
98358
98359
98360
98361
98362
98363
98364
98365
98366
98367
98368
98369
98370
98371
98372
98373
98374
98375
98376
98377
98378
98379
98380
98381
98382
98383
98384
98385
98386
98387
98388
98389
98390
98391
98392
98393
98394
98395
98396
98397
98398
98399
98400
98401
98402
98403
98404
98405
98406
98407
98408
98409
98410
98411
98412
98413
98414
98415
98416
98417
98418
98419
98420
98421
98422
98423
98424
98425
98426
98427
98428
98429
98430
98431
98432
98433
98434
98435
98436
98437
98438
98439
98440
98441
98442
98443
98444
98445
98446
98447
98448
98449
98450
98451
98452
98453
98454
98455
98456
98457
98458
98459
98460
98461
98462
98463
98464
98465
98466
98467
98468
98469
98470
98471
98472
98473
98474
98475
98476
98477
98478
98479
98480
98481
98482
98483
98484
98485
98486
98487
98488
98489
98490
98491
98492
98493
98494
98495
98496
98497
98498
98499
98500
98501
98502
98503
98504
98505
98506
98507
98508
98509
98510
98511
98512
98513
98514
98515
98516
98517
98518
98519
98520
98521
98522
98523
98524
98525
98526
98527
98528
98529
98530
98531
98532
98533
98534
98535
98536
98537
98538
98539
98540
98541
98542
98543
98544
98545
98546
98547
98548
98549
98550
98551
98552
98553
98554
98555
98556
98557
98558
98559
98560
98561
98562
98563
98564
98565
98566
98567
98568
98569
98570
98571
98572
98573
98574
98575
98576
98577
98578
98579
98580
98581
98582
98583
98584
98585
98586
98587
98588
98589
98590
98591
98592
98593
98594
98595
98596
98597
98598
98599
98600
98601
98602
98603
98604
98605
98606
98607
98608
98609
98610
98611
98612
98613
98614
98615
98616
98617
98618
98619
98620
98621
98622
98623
98624
98625
98626
98627
98628
98629
98630
98631
98632
98633
98634
98635
98636
98637
98638
98639
98640
98641
98642
98643
98644
98645
98646
98647
98648
98649
98650
98651
98652
98653
98654
98655
98656
98657
98658
98659
98660
98661
98662
98663
98664
98665
98666
98667
98668
98669
98670
98671
98672
98673
98674
98675
98676
98677
98678
98679
98680
98681
98682
98683
98684
98685
98686
98687
98688
98689
98690
98691
98692
98693
98694
98695
98696
98697
98698
98699
98700
98701
98702
98703
98704
98705
98706
98707
98708
98709
98710
98711
98712
98713
98714
98715
98716
98717
98718
98719
98720
98721
98722
98723
98724
98725
98726
98727
98728
98729
98730
98731
98732
98733
98734
98735
98736
98737
98738
98739
98740
98741
98742
98743
98744
98745
98746
98747
98748
98749
98750
98751
98752
98753
98754
98755
98756
98757
98758
98759
98760
98761
98762
98763
98764
98765
98766
98767
98768
98769
98770
98771
98772
98773
98774
98775
98776
98777
98778
98779
98780
98781
98782
98783
98784
98785
98786
98787
98788
98789
98790
98791
98792
98793
98794
98795
98796
98797
98798
98799
98800
98801
98802
98803
98804
98805
98806
98807
98808
98809
98810
98811
98812
98813
98814
98815
98816
98817
98818
98819
98820
98821
98822
98823
98824
98825
98826
98827
98828
98829
98830
98831
98832
98833
98834
98835
98836
98837
98838
98839
98840
98841
98842
98843
98844
98845
98846
98847
98848
98849
98850
98851
98852
98853
98854
98855
98856
98857
98858
98859
98860
98861
98862
98863
98864
98865
98866
98867
98868
98869
98870
98871
98872
98873
98874
98875
98876
98877
98878
98879
98880
98881
98882
98883
98884
98885
98886
98887
98888
98889
98890
98891
98892
98893
98894
98895
98896
98897
98898
98899
98900
98901
98902
98903
98904
98905
98906
98907
98908
98909
98910
98911
98912
98913
98914
98915
98916
98917
98918
98919
98920
98921
98922
98923
98924
98925
98926
98927
98928
98929
98930
98931
98932
98933
98934
98935
98936
98937
98938
98939
98940
98941
98942
98943
98944
98945
98946
98947
98948
98949
98950
98951
98952
98953
98954
98955
98956
98957
98958
98959
98960
98961
98962
98963
98964
98965
98966
98967
98968
98969
98970
98971
98972
98973
98974
98975
98976
98977
98978
98979
98980
98981
98982
98983
98984
98985
98986
98987
98988
98989
98990
98991
98992
98993
98994
98995
98996
98997
98998
98999
99000
99001
99002
99003
99004
99005
99006
99007
99008
99009
99010
99011
99012
99013
99014
99015
99016
99017
99018
99019
99020
99021
99022
99023
99024
99025
99026
99027
99028
99029
99030
99031
99032
99033
99034
99035
99036
99037
99038
99039
99040
99041
99042
99043
99044
99045
99046
99047
99048
99049
99050
99051
99052
99053
99054
99055
99056
99057
99058
99059
99060
99061
99062
99063
99064
99065
99066
99067
99068
99069
99070
99071
99072
99073
99074
99075
99076
99077
99078
99079
99080
99081
99082
99083
99084
99085
99086
99087
99088
99089
99090
99091
99092
99093
99094
99095
99096
99097
99098
99099
99100
99101
99102
99103
99104
99105
99106
99107
99108
99109
99110
99111
99112
99113
99114
99115
99116
99117
99118
99119
99120
99121
99122
99123
99124
99125
99126
99127
99128
99129
99130
99131
99132
99133
99134
99135
99136
99137
99138
99139
99140
99141
99142
99143
99144
99145
99146
99147
99148
99149
99150
99151
99152
99153
99154
99155
99156
99157
99158
99159
99160
99161
99162
99163
99164
99165
99166
99167
99168
99169
99170
99171
99172
99173
99174
99175
99176
99177
99178
99179
99180
99181
99182
99183
99184
99185
99186
99187
99188
99189
99190
99191
99192
99193
99194
99195
99196
99197
99198
99199
99200
99201
99202
99203
99204
99205
99206
99207
99208
99209
99210
99211
99212
99213
99214
99215
99216
99217
99218
99219
99220
99221
99222
99223
99224
99225
99226
99227
99228
99229
99230
99231
99232
99233
99234
99235
99236
99237
99238
99239
99240
99241
99242
99243
99244
99245
99246
99247
99248
99249
99250
99251
99252
99253
99254
99255
99256
99257
99258
99259
99260
99261
99262
99263
99264
99265
99266
99267
99268
99269
99270
99271
99272
99273
99274
99275
99276
99277
99278
99279
99280
99281
99282
99283
99284
99285
99286
99287
99288
99289
99290
99291
99292
99293
99294
99295
99296
99297
99298
99299
99300
99301
99302
99303
99304
99305
99306
99307
99308
99309
99310
99311
99312
99313
99314
99315
99316
99317
99318
99319
99320
99321
99322
99323
99324
99325
99326
99327
99328
99329
99330
99331
99332
99333
99334
99335
99336
99337
99338
99339
99340
99341
99342
99343
99344
99345
99346
99347
99348
99349
99350
99351
99352
99353
99354
99355
99356
99357
99358
99359
99360
99361
99362
99363
99364
99365
99366
99367
99368
99369
99370
99371
99372
99373
99374
99375
99376
99377
99378
99379
99380
99381
99382
99383
99384
99385
99386
99387
99388
99389
99390
99391
99392
99393
99394
99395
99396
99397
99398
99399
99400
99401
99402
99403
99404
99405
99406
99407
99408
99409
99410
99411
99412
99413
99414
99415
99416
99417
99418
99419
99420
99421
99422
99423
99424
99425
99426
99427
99428
99429
99430
99431
99432
99433
99434
99435
99436
99437
99438
99439
99440
99441
99442
99443
99444
99445
99446
99447
99448
99449
99450
99451
99452
99453
99454
99455
99456
99457
99458
99459
99460
99461
99462
99463
99464
99465
99466
99467
99468
99469
99470
99471
99472
99473
99474
99475
99476
99477
99478
99479
99480
99481
99482
99483
99484
99485
99486
99487
99488
99489
99490
99491
99492
99493
99494
99495
99496
99497
99498
99499
99500
99501
99502
99503
99504
99505
99506
99507
99508
99509
99510
99511
99512
99513
99514
99515
99516
99517
99518
99519
99520
99521
99522
99523
99524
99525
99526
99527
99528
99529
99530
99531
99532
99533
99534
99535
99536
99537
99538
99539
99540
99541
99542
99543
99544
99545
99546
99547
99548
99549
99550
99551
99552
99553
99554
99555
99556
99557
99558
99559
99560
99561
99562
99563
99564
99565
99566
99567
99568
99569
99570
99571
99572
99573
99574
99575
99576
99577
99578
99579
99580
99581
99582
99583
99584
99585
99586
99587
99588
99589
99590
99591
99592
99593
99594
99595
99596
99597
99598
99599
99600
99601
99602
99603
99604
99605
99606
99607
99608
99609
99610
99611
99612
99613
99614
99615
99616
99617
99618
99619
99620
99621
99622
99623
99624
99625
99626
99627
99628
99629
99630
99631
99632
99633
99634
99635
99636
99637
99638
99639
99640
99641
99642
99643
99644
99645
99646
99647
99648
99649
99650
99651
99652
99653
99654
99655
99656
99657
99658
99659
99660
99661
99662
99663
99664
99665
99666
99667
99668
99669
99670
99671
99672
99673
99674
99675
99676
99677
99678
99679
99680
99681
99682
99683
99684
99685
99686
99687
99688
99689
99690
99691
99692
99693
99694
99695
99696
99697
99698
99699
99700
99701
99702
99703
99704
99705
99706
99707
99708
99709
99710
99711
99712
99713
99714
99715
99716
99717
99718
99719
99720
99721
99722
99723
99724
99725
99726
99727
99728
99729
99730
99731
99732
99733
99734
99735
99736
99737
99738
99739
99740
99741
99742
99743
99744
99745
99746
99747
99748
99749
99750
99751
99752
99753
99754
99755
99756
99757
99758
99759
99760
99761
99762
99763
99764
99765
99766
99767
99768
99769
99770
99771
99772
99773
99774
99775
99776
99777
99778
99779
99780
99781
99782
99783
99784
99785
99786
99787
99788
99789
99790
99791
99792
99793
99794
99795
99796
99797
99798
99799
99800
99801
99802
99803
99804
99805
99806
99807
99808
99809
99810
99811
99812
99813
99814
99815
99816
99817
99818
99819
99820
99821
99822
99823
99824
99825
99826
99827
99828
99829
99830
99831
99832
99833
99834
99835
99836
99837
99838
99839
99840
99841
99842
99843
99844
99845
99846
99847
99848
99849
99850
99851
99852
99853
99854
99855
99856
99857
99858
99859
99860
99861
99862
99863
99864
99865
99866
99867
99868
99869
99870
99871
99872
99873
99874
99875
99876
99877
99878
99879
99880
99881
99882
99883
99884
99885
99886
99887
99888
99889
99890
99891
99892
99893
99894
99895
99896
99897
99898
99899
99900
99901
99902
99903
99904
99905
99906
99907
99908
99909
99910
99911
99912
99913
99914
99915
99916
99917
99918
99919
99920
99921
99922
99923
99924
99925
99926
99927
99928
99929
99930
99931
99932
99933
99934
99935
99936
99937
99938
99939
99940
99941
99942
99943
99944
99945
99946
99947
99948
99949
99950
99951
99952
99953
99954
99955
99956
99957
99958
99959
99960
99961
99962
99963
99964
99965
99966
99967
99968
99969
99970
99971
99972
99973
99974
99975
99976
99977
99978
99979
99980
99981
99982
99983
99984
99985
99986
99987
99988
99989
99990
99991
99992
99993
99994
99995
99996
99997
99998
99999
100000
100001
100002
100003
100004
100005
100006
100007
100008
100009
100010
100011
100012
100013
100014
100015
100016
100017
100018
100019
100020
100021
100022
100023
100024
100025
100026
100027
100028
100029
100030
100031
100032
100033
100034
100035
100036
100037
100038
100039
100040
100041
100042
100043
100044
100045
100046
100047
100048
100049
100050
100051
100052
100053
100054
100055
100056
100057
100058
100059
100060
100061
100062
100063
100064
100065
100066
100067
100068
100069
100070
100071
100072
100073
100074
100075
100076
100077
100078
100079
100080
100081
100082
100083
100084
100085
100086
100087
100088
100089
100090
100091
100092
100093
100094
100095
100096
100097
100098
100099
100100
100101
100102
100103
100104
100105
100106
100107
100108
100109
100110
100111
100112
100113
100114
100115
100116
100117
100118
100119
100120
100121
100122
100123
100124
100125
100126
100127
100128
100129
100130
100131
100132
100133
100134
100135
100136
100137
100138
100139
100140
100141
100142
100143
100144
100145
100146
100147
100148
100149
100150
100151
100152
100153
100154
100155
100156
100157
100158
100159
100160
100161
100162
100163
100164
100165
100166
100167
100168
100169
100170
100171
100172
100173
100174
100175
100176
100177
100178
100179
100180
100181
100182
100183
100184
100185
100186
100187
100188
100189
100190
100191
100192
100193
100194
100195
100196
100197
100198
100199
100200
100201
100202
100203
100204
100205
100206
100207
100208
100209
100210
100211
100212
100213
100214
100215
100216
100217
100218
100219
100220
100221
100222
100223
100224
100225
100226
100227
100228
100229
100230
100231
100232
100233
100234
100235
100236
100237
100238
100239
100240
100241
100242
100243
100244
100245
100246
100247
100248
100249
100250
100251
100252
100253
100254
100255
100256
100257
100258
100259
100260
100261
100262
100263
100264
100265
100266
100267
100268
100269
100270
100271
100272
100273
100274
100275
100276
100277
100278
100279
100280
100281
100282
100283
100284
100285
100286
100287
100288
100289
100290
100291
100292
100293
100294
100295
100296
100297
100298
100299
100300
100301
100302
100303
100304
100305
100306
100307
100308
100309
100310
100311
100312
100313
100314
100315
100316
100317
100318
100319
100320
100321
100322
100323
100324
100325
100326
100327
100328
100329
100330
100331
100332
100333
100334
100335
100336
100337
100338
100339
100340
100341
100342
100343
100344
100345
100346
100347
100348
100349
100350
100351
100352
100353
100354
100355
100356
100357
100358
100359
100360
100361
100362
100363
100364
100365
100366
100367
100368
100369
100370
100371
100372
100373
100374
100375
100376
100377
100378
100379
100380
100381
100382
100383
100384
100385
100386
100387
100388
100389
100390
100391
100392
100393
100394
100395
100396
100397
100398
100399
100400
100401
100402
100403
100404
100405
100406
100407
100408
100409
100410
100411
100412
100413
100414
100415
100416
100417
100418
100419
100420
100421
100422
100423
100424
100425
100426
100427
100428
100429
100430
100431
100432
100433
100434
100435
100436
100437
100438
100439
100440
100441
100442
100443
100444
100445
100446
100447
100448
100449
100450
100451
100452
100453
100454
100455
100456
100457
100458
100459
100460
100461
100462
100463
100464
100465
100466
100467
100468
100469
100470
100471
100472
100473
100474
100475
100476
100477
100478
100479
100480
100481
100482
100483
100484
100485
100486
100487
100488
100489
100490
100491
100492
100493
100494
100495
100496
100497
100498
100499
100500
100501
100502
100503
100504
100505
100506
100507
100508
100509
100510
100511
100512
100513
100514
100515
100516
100517
100518
100519
100520
100521
100522
100523
100524
100525
100526
100527
100528
100529
100530
100531
100532
100533
100534
100535
100536
100537
100538
100539
100540
100541
100542
100543
100544
100545
100546
100547
100548
100549
100550
100551
100552
100553
100554
100555
100556
100557
100558
100559
100560
100561
100562
100563
100564
100565
100566
100567
100568
100569
100570
100571
100572
100573
100574
100575
100576
100577
100578
100579
100580
100581
100582
100583
100584
100585
100586
100587
100588
100589
100590
100591
100592
100593
100594
100595
100596
100597
100598
100599
100600
100601
100602
100603
100604
100605
100606
100607
100608
100609
100610
100611
100612
100613
100614
100615
100616
100617
100618
100619
100620
100621
100622
100623
100624
100625
100626
100627
100628
100629
100630
100631
100632
100633
100634
100635
100636
100637
100638
100639
100640
100641
100642
100643
100644
100645
100646
100647
100648
100649
100650
100651
100652
100653
100654
100655
100656
100657
100658
100659
100660
100661
100662
100663
100664
100665
100666
100667
100668
100669
100670
100671
100672
100673
100674
100675
100676
100677
100678
100679
100680
100681
100682
100683
100684
100685
100686
100687
100688
100689
100690
100691
100692
100693
100694
100695
100696
100697
100698
100699
100700
100701
100702
100703
100704
100705
100706
100707
100708
100709
100710
100711
100712
100713
100714
100715
100716
100717
100718
100719
100720
100721
100722
100723
100724
100725
100726
100727
100728
100729
100730
100731
100732
100733
100734
100735
100736
100737
100738
100739
100740
100741
100742
100743
100744
100745
100746
100747
100748
100749
100750
100751
100752
100753
100754
100755
100756
100757
100758
100759
100760
100761
100762
100763
100764
100765
100766
100767
100768
100769
100770
100771
100772
100773
100774
100775
100776
100777
100778
100779
100780
100781
100782
100783
100784
100785
100786
100787
100788
100789
100790
100791
100792
100793
100794
100795
100796
100797
100798
100799
100800
100801
100802
100803
100804
100805
100806
100807
100808
100809
100810
100811
100812
100813
100814
100815
100816
100817
100818
100819
100820
100821
100822
100823
100824
100825
100826
100827
100828
100829
100830
100831
100832
100833
100834
100835
100836
100837
100838
100839
100840
100841
100842
100843
100844
100845
100846
100847
100848
100849
100850
100851
100852
100853
100854
100855
100856
100857
100858
100859
100860
100861
100862
100863
100864
100865
100866
100867
100868
100869
100870
100871
100872
100873
100874
100875
100876
100877
100878
100879
100880
100881
100882
100883
100884
100885
100886
100887
100888
100889
100890
100891
100892
100893
100894
100895
100896
100897
100898
100899
100900
100901
100902
100903
100904
100905
100906
100907
100908
100909
100910
100911
100912
100913
100914
100915
100916
100917
100918
100919
100920
100921
100922
100923
100924
100925
100926
100927
100928
100929
100930
100931
100932
100933
100934
100935
100936
100937
100938
100939
100940
100941
100942
100943
100944
100945
100946
100947
100948
100949
100950
100951
100952
100953
100954
100955
100956
100957
100958
100959
100960
100961
100962
100963
100964
100965
100966
100967
100968
100969
100970
100971
100972
100973
100974
100975
100976
100977
100978
100979
100980
100981
100982
100983
100984
100985
100986
100987
100988
100989
100990
100991
100992
100993
100994
100995
100996
100997
100998
100999
101000
101001
101002
101003
101004
101005
101006
101007
101008
101009
101010
101011
101012
101013
101014
101015
101016
101017
101018
101019
101020
101021
101022
101023
101024
101025
101026
101027
101028
101029
101030
101031
101032
101033
101034
101035
101036
101037
101038
101039
101040
101041
101042
101043
101044
101045
101046
101047
101048
101049
101050
101051
101052
101053
101054
101055
101056
101057
101058
101059
101060
101061
101062
101063
101064
101065
101066
101067
101068
101069
101070
101071
101072
101073
101074
101075
101076
101077
101078
101079
101080
101081
101082
101083
101084
101085
101086
101087
101088
101089
101090
101091
101092
101093
101094
101095
101096
101097
101098
101099
101100
101101
101102
101103
101104
101105
101106
101107
101108
101109
101110
101111
101112
101113
101114
101115
101116
101117
101118
101119
101120
101121
101122
101123
101124
101125
101126
101127
101128
101129
101130
101131
101132
101133
101134
101135
101136
101137
101138
101139
101140
101141
101142
101143
101144
101145
101146
101147
101148
101149
101150
101151
101152
101153
101154
101155
101156
101157
101158
101159
101160
101161
101162
101163
101164
101165
101166
101167
101168
101169
101170
101171
101172
101173
101174
101175
101176
101177
101178
101179
101180
101181
101182
101183
101184
101185
101186
101187
101188
101189
101190
101191
101192
101193
101194
101195
101196
101197
101198
101199
101200
101201
101202
101203
101204
101205
101206
101207
101208
101209
101210
101211
101212
101213
101214
101215
101216
101217
101218
101219
101220
101221
101222
101223
101224
101225
101226
101227
101228
101229
101230
101231
101232
101233
101234
101235
101236
101237
101238
101239
101240
101241
101242
101243
101244
101245
101246
101247
101248
101249
101250
101251
101252
101253
101254
101255
101256
101257
101258
101259
101260
101261
101262
101263
101264
101265
101266
101267
101268
101269
101270
101271
101272
101273
101274
101275
101276
101277
101278
101279
101280
101281
101282
101283
101284
101285
101286
101287
101288
101289
101290
101291
101292
101293
101294
101295
101296
101297
101298
101299
101300
101301
101302
101303
101304
101305
101306
101307
101308
101309
101310
101311
101312
101313
101314
101315
101316
101317
101318
101319
101320
101321
101322
101323
101324
101325
101326
101327
101328
101329
101330
101331
101332
101333
101334
101335
101336
101337
101338
101339
101340
101341
101342
101343
101344
101345
101346
101347
101348
101349
101350
101351
101352
101353
101354
101355
101356
101357
101358
101359
101360
101361
101362
101363
101364
101365
101366
101367
101368
101369
101370
101371
101372
101373
101374
101375
101376
101377
101378
101379
101380
101381
101382
101383
101384
101385
101386
101387
101388
101389
101390
101391
101392
101393
101394
101395
101396
101397
101398
101399
101400
101401
101402
101403
101404
101405
101406
101407
101408
101409
101410
101411
101412
101413
101414
101415
101416
101417
101418
101419
101420
101421
101422
101423
101424
101425
101426
101427
101428
101429
101430
101431
101432
101433
101434
101435
101436
101437
101438
101439
101440
101441
101442
101443
101444
101445
101446
101447
101448
101449
101450
101451
101452
101453
101454
101455
101456
101457
101458
101459
101460
101461
101462
101463
101464
101465
101466
101467
101468
101469
101470
101471
101472
101473
101474
101475
101476
101477
101478
101479
101480
101481
101482
101483
101484
101485
101486
101487
101488
101489
101490
101491
101492
101493
101494
101495
101496
101497
101498
101499
101500
101501
101502
101503
101504
101505
101506
101507
101508
101509
101510
101511
101512
101513
101514
101515
101516
101517
101518
101519
101520
101521
101522
101523
101524
101525
101526
101527
101528
101529
101530
101531
101532
101533
101534
101535
101536
101537
101538
101539
101540
101541
101542
101543
101544
101545
101546
101547
101548
101549
101550
101551
101552
101553
101554
101555
101556
101557
101558
101559
101560
101561
101562
101563
101564
101565
101566
101567
101568
101569
101570
101571
101572
101573
101574
101575
101576
101577
101578
101579
101580
101581
101582
101583
101584
101585
101586
101587
101588
101589
101590
101591
101592
101593
101594
101595
101596
101597
101598
101599
101600
101601
101602
101603
101604
101605
101606
101607
101608
101609
101610
101611
101612
101613
101614
101615
101616
101617
101618
101619
101620
101621
101622
101623
101624
101625
101626
101627
101628
101629
101630
101631
101632
101633
101634
101635
101636
101637
101638
101639
101640
101641
101642
101643
101644
101645
101646
101647
101648
101649
101650
101651
101652
101653
101654
101655
101656
101657
101658
101659
101660
101661
101662
101663
101664
101665
101666
101667
101668
101669
101670
101671
101672
101673
101674
101675
101676
101677
101678
101679
101680
101681
101682
101683
101684
101685
101686
101687
101688
101689
101690
101691
101692
101693
101694
101695
101696
101697
101698
101699
101700
101701
101702
101703
101704
101705
101706
101707
101708
101709
101710
101711
101712
101713
101714
101715
101716
101717
101718
101719
101720
101721
101722
101723
101724
101725
101726
101727
101728
101729
101730
101731
101732
101733
101734
101735
101736
101737
101738
101739
101740
101741
101742
101743
101744
101745
101746
101747
101748
101749
101750
101751
101752
101753
101754
101755
101756
101757
101758
101759
101760
101761
101762
101763
101764
101765
101766
101767
101768
101769
101770
101771
101772
101773
101774
101775
101776
101777
101778
101779
101780
101781
101782
101783
101784
101785
101786
101787
101788
101789
101790
101791
101792
101793
101794
101795
101796
101797
101798
101799
101800
101801
101802
101803
101804
101805
101806
101807
101808
101809
101810
101811
101812
101813
101814
101815
101816
101817
101818
101819
101820
101821
101822
101823
101824
101825
101826
101827
101828
101829
101830
101831
101832
101833
101834
101835
101836
101837
101838
101839
101840
101841
101842
101843
101844
101845
101846
101847
101848
101849
101850
101851
101852
101853
101854
101855
101856
101857
101858
101859
101860
101861
101862
101863
101864
101865
101866
101867
101868
101869
101870
101871
101872
101873
101874
101875
101876
101877
101878
101879
101880
101881
101882
101883
101884
101885
101886
101887
101888
101889
101890
101891
101892
101893
101894
101895
101896
101897
101898
101899
101900
101901
101902
101903
101904
101905
101906
101907
101908
101909
101910
101911
101912
101913
101914
101915
101916
101917
101918
101919
101920
101921
101922
101923
101924
101925
101926
101927
101928
101929
101930
101931
101932
101933
101934
101935
101936
101937
101938
101939
101940
101941
101942
101943
101944
101945
101946
101947
101948
101949
101950
101951
101952
101953
101954
101955
101956
101957
101958
101959
101960
101961
101962
101963
101964
101965
101966
101967
101968
101969
101970
101971
101972
101973
101974
101975
101976
101977
101978
101979
101980
101981
101982
101983
101984
101985
101986
101987
101988
101989
101990
101991
101992
101993
101994
101995
101996
101997
101998
101999
102000
102001
102002
102003
102004
102005
102006
102007
102008
102009
102010
102011
102012
102013
102014
102015
102016
102017
102018
102019
102020
102021
102022
102023
102024
102025
102026
102027
102028
102029
102030
102031
102032
102033
102034
102035
102036
102037
102038
102039
102040
102041
102042
102043
102044
102045
102046
102047
102048
102049
102050
102051
102052
102053
102054
102055
102056
102057
102058
102059
102060
102061
102062
102063
102064
102065
102066
102067
102068
102069
102070
102071
102072
102073
102074
102075
102076
102077
102078
102079
102080
102081
102082
102083
102084
102085
102086
102087
102088
102089
102090
102091
102092
102093
102094
102095
102096
102097
102098
102099
102100
102101
102102
102103
102104
102105
102106
102107
102108
102109
102110
102111
102112
102113
102114
102115
102116
102117
102118
102119
102120
102121
102122
102123
102124
102125
102126
102127
102128
102129
102130
102131
102132
102133
102134
102135
102136
102137
102138
102139
102140
102141
102142
102143
102144
102145
102146
102147
102148
102149
102150
102151
102152
102153
102154
102155
102156
102157
102158
102159
102160
102161
102162
102163
102164
102165
102166
102167
102168
102169
102170
102171
102172
102173
102174
102175
102176
102177
102178
102179
102180
102181
102182
102183
102184
102185
102186
102187
102188
102189
102190
102191
102192
102193
102194
102195
102196
102197
102198
102199
102200
102201
102202
102203
102204
102205
102206
102207
102208
102209
102210
102211
102212
102213
102214
102215
102216
102217
102218
102219
102220
102221
102222
102223
102224
102225
102226
102227
102228
102229
102230
102231
102232
102233
102234
102235
102236
102237
102238
102239
102240
102241
102242
102243
102244
102245
102246
102247
102248
102249
102250
102251
102252
102253
102254
102255
102256
102257
102258
102259
102260
102261
102262
102263
102264
102265
102266
102267
102268
102269
102270
102271
102272
102273
102274
102275
102276
102277
102278
102279
102280
102281
102282
102283
102284
102285
102286
102287
102288
102289
102290
102291
102292
102293
102294
102295
102296
102297
102298
102299
102300
102301
102302
102303
102304
102305
102306
102307
102308
102309
102310
102311
102312
102313
102314
102315
102316
102317
102318
102319
102320
102321
102322
102323
102324
102325
102326
102327
102328
102329
102330
102331
102332
102333
102334
102335
102336
102337
102338
102339
102340
102341
102342
102343
102344
102345
102346
102347
102348
102349
102350
102351
102352
102353
102354
102355
102356
102357
102358
102359
102360
102361
102362
102363
102364
102365
102366
102367
102368
102369
102370
102371
102372
102373
102374
102375
102376
102377
102378
102379
102380
102381
102382
102383
102384
102385
102386
102387
102388
102389
102390
102391
102392
102393
102394
102395
102396
102397
102398
102399
102400
102401
102402
102403
102404
102405
102406
102407
102408
102409
102410
102411
102412
102413
102414
102415
102416
102417
102418
102419
102420
102421
102422
102423
102424
102425
102426
102427
102428
102429
102430
102431
102432
102433
102434
102435
102436
102437
102438
102439
102440
102441
102442
102443
102444
102445
102446
102447
102448
102449
102450
102451
102452
102453
102454
102455
102456
102457
102458
102459
102460
102461
102462
102463
102464
102465
102466
102467
102468
102469
102470
102471
102472
102473
102474
102475
102476
102477
102478
102479
102480
102481
102482
102483
102484
102485
102486
102487
102488
102489
102490
102491
102492
102493
102494
102495
102496
102497
102498
102499
102500
102501
102502
102503
102504
102505
102506
102507
102508
102509
102510
102511
102512
102513
102514
102515
102516
102517
102518
102519
102520
102521
102522
102523
102524
102525
102526
102527
102528
102529
102530
102531
102532
102533
102534
102535
102536
102537
102538
102539
102540
102541
102542
102543
102544
102545
102546
102547
102548
102549
102550
102551
102552
102553
102554
102555
102556
102557
102558
102559
102560
102561
102562
102563
102564
102565
102566
102567
102568
102569
102570
102571
102572
102573
102574
102575
102576
102577
102578
102579
102580
102581
102582
102583
102584
102585
102586
102587
102588
102589
102590
102591
102592
102593
102594
102595
102596
102597
102598
102599
102600
102601
102602
102603
102604
102605
102606
102607
102608
102609
102610
102611
102612
102613
102614
102615
102616
102617
102618
102619
102620
102621
102622
102623
102624
102625
102626
102627
102628
102629
102630
102631
102632
102633
102634
102635
102636
102637
102638
102639
102640
102641
102642
102643
102644
102645
102646
102647
102648
102649
102650
102651
102652
102653
102654
102655
102656
102657
102658
102659
102660
102661
102662
102663
102664
102665
102666
102667
102668
102669
102670
102671
102672
102673
102674
102675
102676
102677
102678
102679
102680
102681
102682
102683
102684
102685
102686
102687
102688
102689
102690
102691
102692
102693
102694
102695
102696
102697
102698
102699
102700
102701
102702
102703
102704
102705
102706
102707
102708
102709
102710
102711
102712
102713
102714
102715
102716
102717
102718
102719
102720
102721
102722
102723
102724
102725
102726
102727
102728
102729
102730
102731
102732
102733
102734
102735
102736
102737
102738
102739
102740
102741
102742
102743
102744
102745
102746
102747
102748
102749
102750
102751
102752
102753
102754
102755
102756
102757
102758
102759
102760
102761
102762
102763
102764
102765
102766
102767
102768
102769
102770
102771
102772
102773
102774
102775
102776
102777
102778
102779
102780
102781
102782
102783
102784
102785
102786
102787
102788
102789
102790
102791
102792
102793
102794
102795
102796
102797
102798
102799
102800
102801
102802
102803
102804
102805
102806
102807
102808
102809
102810
102811
102812
102813
102814
102815
102816
102817
102818
102819
102820
102821
102822
102823
102824
102825
102826
102827
102828
102829
102830
102831
102832
102833
102834
102835
102836
102837
102838
102839
102840
102841
102842
102843
102844
102845
102846
102847
102848
102849
102850
102851
102852
102853
102854
102855
102856
102857
102858
102859
102860
102861
102862
102863
102864
102865
102866
102867
102868
102869
102870
102871
102872
102873
102874
102875
102876
102877
102878
102879
102880
102881
102882
102883
102884
102885
102886
102887
102888
102889
102890
102891
102892
102893
102894
102895
102896
102897
102898
102899
102900
102901
102902
102903
102904
102905
102906
102907
102908
102909
102910
102911
102912
102913
102914
102915
102916
102917
102918
102919
102920
102921
102922
102923
102924
102925
102926
102927
102928
102929
102930
102931
102932
102933
102934
102935
102936
102937
102938
102939
102940
102941
102942
102943
102944
102945
102946
102947
102948
102949
102950
102951
102952
102953
102954
102955
102956
102957
102958
102959
102960
102961
102962
102963
102964
102965
102966
102967
102968
102969
102970
102971
102972
102973
102974
102975
102976
102977
102978
102979
102980
102981
102982
102983
102984
102985
102986
102987
102988
102989
102990
102991
102992
102993
102994
102995
102996
102997
102998
102999
103000
103001
103002
103003
103004
103005
103006
103007
103008
103009
103010
103011
103012
103013
103014
103015
103016
103017
103018
103019
103020
103021
103022
103023
103024
103025
103026
103027
103028
103029
103030
103031
103032
103033
103034
103035
103036
103037
103038
103039
103040
103041
103042
103043
103044
103045
103046
103047
103048
103049
103050
103051
103052
103053
103054
103055
103056
103057
103058
103059
103060
103061
103062
103063
103064
103065
103066
103067
103068
103069
103070
103071
103072
103073
103074
103075
103076
103077
103078
103079
103080
103081
103082
103083
103084
103085
103086
103087
103088
103089
103090
103091
103092
103093
103094
103095
103096
103097
103098
103099
103100
103101
103102
103103
103104
103105
103106
103107
103108
103109
103110
103111
103112
103113
103114
103115
103116
103117
103118
103119
103120
103121
103122
103123
103124
103125
103126
103127
103128
103129
103130
103131
103132
103133
103134
103135
103136
103137
103138
103139
103140
103141
103142
103143
103144
103145
103146
103147
103148
103149
103150
103151
103152
103153
103154
103155
103156
103157
103158
103159
103160
103161
103162
103163
103164
103165
103166
103167
103168
103169
103170
103171
103172
103173
103174
103175
103176
103177
103178
103179
103180
103181
103182
103183
103184
103185
103186
103187
103188
103189
103190
103191
103192
103193
103194
103195
103196
103197
103198
103199
103200
103201
103202
103203
103204
103205
103206
103207
103208
103209
103210
103211
103212
103213
103214
103215
103216
103217
103218
103219
103220
103221
103222
103223
103224
103225
103226
103227
103228
103229
103230
103231
103232
103233
103234
103235
103236
103237
103238
103239
103240
103241
103242
103243
103244
103245
103246
103247
103248
103249
103250
103251
103252
103253
103254
103255
103256
103257
103258
103259
103260
103261
103262
103263
103264
103265
103266
103267
103268
103269
103270
103271
103272
103273
103274
103275
103276
103277
103278
103279
103280
103281
103282
103283
103284
103285
103286
103287
103288
103289
103290
103291
103292
103293
103294
103295
103296
103297
103298
103299
103300
103301
103302
103303
103304
103305
103306
103307
103308
103309
103310
103311
103312
103313
103314
103315
103316
103317
103318
103319
103320
103321
103322
103323
103324
103325
103326
103327
103328
103329
103330
103331
103332
103333
103334
103335
103336
103337
103338
103339
103340
103341
103342
103343
103344
103345
103346
103347
103348
103349
103350
103351
103352
103353
103354
103355
103356
103357
103358
103359
103360
103361
103362
103363
103364
103365
103366
103367
103368
103369
103370
103371
103372
103373
103374
103375
103376
103377
103378
103379
103380
103381
103382
103383
103384
103385
103386
103387
103388
103389
103390
103391
103392
103393
103394
103395
103396
103397
103398
103399
103400
103401
103402
103403
103404
103405
103406
103407
103408
103409
103410
103411
103412
103413
103414
103415
103416
103417
103418
103419
103420
103421
103422
103423
103424
103425
103426
103427
103428
103429
103430
103431
103432
103433
103434
103435
103436
103437
103438
103439
103440
103441
103442
103443
103444
103445
103446
103447
103448
103449
103450
103451
103452
103453
103454
103455
103456
103457
103458
103459
103460
103461
103462
103463
103464
103465
103466
103467
103468
103469
103470
103471
103472
103473
103474
103475
103476
103477
103478
103479
103480
103481
103482
103483
103484
103485
103486
103487
103488
103489
103490
103491
103492
103493
103494
103495
103496
103497
103498
103499
103500
103501
103502
103503
103504
103505
103506
103507
103508
103509
103510
103511
103512
103513
103514
103515
103516
103517
103518
103519
103520
103521
103522
103523
103524
103525
103526
103527
103528
103529
103530
103531
103532
103533
103534
103535
103536
103537
103538
103539
103540
103541
103542
103543
103544
103545
103546
103547
103548
103549
103550
103551
103552
103553
103554
103555
103556
103557
103558
103559
103560
103561
103562
103563
103564
103565
103566
103567
103568
103569
103570
103571
103572
103573
103574
103575
103576
103577
103578
103579
103580
103581
103582
103583
103584
103585
103586
103587
103588
103589
103590
103591
103592
103593
103594
103595
103596
103597
103598
103599
103600
103601
103602
103603
103604
103605
103606
103607
103608
103609
103610
103611
103612
103613
103614
103615
103616
103617
103618
103619
103620
103621
103622
103623
103624
103625
103626
103627
103628
103629
103630
103631
103632
103633
103634
103635
103636
103637
103638
103639
103640
103641
103642
103643
103644
103645
103646
103647
103648
103649
103650
103651
103652
103653
103654
103655
103656
103657
103658
103659
103660
103661
103662
103663
103664
103665
103666
103667
103668
103669
103670
103671
103672
103673
103674
103675
103676
103677
103678
103679
103680
103681
103682
103683
103684
103685
103686
103687
103688
103689
103690
103691
103692
103693
103694
103695
103696
103697
103698
103699
103700
103701
103702
103703
103704
103705
103706
103707
103708
103709
103710
103711
103712
103713
103714
103715
103716
103717
103718
103719
103720
103721
103722
103723
103724
103725
103726
103727
103728
103729
103730
103731
103732
103733
103734
103735
103736
103737
103738
103739
103740
103741
103742
103743
103744
103745
103746
103747
103748
103749
103750
103751
103752
103753
103754
103755
103756
103757
103758
103759
103760
103761
103762
103763
103764
103765
103766
103767
103768
103769
103770
103771
103772
103773
103774
103775
103776
103777
103778
103779
103780
103781
103782
103783
103784
103785
103786
103787
103788
103789
103790
103791
103792
103793
103794
103795
103796
103797
103798
103799
103800
103801
103802
103803
103804
103805
103806
103807
103808
103809
103810
103811
103812
103813
103814
103815
103816
103817
103818
103819
103820
103821
103822
103823
103824
103825
103826
103827
103828
103829
103830
103831
103832
103833
103834
103835
103836
103837
103838
103839
103840
103841
103842
103843
103844
103845
103846
103847
103848
103849
103850
103851
103852
103853
103854
103855
103856
103857
103858
103859
103860
103861
103862
103863
103864
103865
103866
103867
103868
103869
103870
103871
103872
103873
103874
103875
103876
103877
103878
103879
103880
103881
103882
103883
103884
103885
103886
103887
103888
103889
103890
103891
103892
103893
103894
103895
103896
103897
103898
103899
103900
103901
103902
103903
103904
103905
103906
103907
103908
103909
103910
103911
103912
103913
103914
103915
103916
103917
103918
103919
103920
103921
103922
103923
103924
103925
103926
103927
103928
103929
103930
103931
103932
103933
103934
103935
103936
103937
103938
103939
103940
103941
103942
103943
103944
103945
103946
103947
103948
103949
103950
103951
103952
103953
103954
103955
103956
103957
103958
103959
103960
103961
103962
103963
103964
103965
103966
103967
103968
103969
103970
103971
103972
103973
103974
103975
103976
103977
103978
103979
103980
103981
103982
103983
103984
103985
103986
103987
103988
103989
103990
103991
103992
103993
103994
103995
103996
103997
103998
103999
104000
104001
104002
104003
104004
104005
104006
104007
104008
104009
104010
104011
104012
104013
104014
104015
104016
104017
104018
104019
104020
104021
104022
104023
104024
104025
104026
104027
104028
104029
104030
104031
104032
104033
104034
104035
104036
104037
104038
104039
104040
104041
104042
104043
104044
104045
104046
104047
104048
104049
104050
104051
104052
104053
104054
104055
104056
104057
104058
104059
104060
104061
104062
104063
104064
104065
104066
104067
104068
104069
104070
104071
104072
104073
104074
104075
104076
104077
104078
104079
104080
104081
104082
104083
104084
104085
104086
104087
104088
104089
104090
104091
104092
104093
104094
104095
104096
104097
104098
104099
104100
104101
104102
104103
104104
104105
104106
104107
104108
104109
104110
104111
104112
104113
104114
104115
104116
104117
104118
104119
104120
104121
104122
104123
104124
104125
104126
104127
104128
104129
104130
104131
104132
104133
104134
104135
104136
104137
104138
104139
104140
104141
104142
104143
104144
104145
104146
104147
104148
104149
104150
104151
104152
104153
104154
104155
104156
104157
104158
104159
104160
104161
104162
104163
104164
104165
104166
104167
104168
104169
104170
104171
104172
104173
104174
104175
104176
104177
104178
104179
104180
104181
104182
104183
104184
104185
104186
104187
104188
104189
104190
104191
104192
104193
104194
104195
104196
104197
104198
104199
104200
104201
104202
104203
104204
104205
104206
104207
104208
104209
104210
104211
104212
104213
104214
104215
104216
104217
104218
104219
104220
104221
104222
104223
104224
104225
104226
104227
104228
104229
104230
104231
104232
104233
104234
104235
104236
104237
104238
104239
104240
104241
104242
104243
104244
104245
104246
104247
104248
104249
104250
104251
104252
104253
104254
104255
104256
104257
104258
104259
104260
104261
104262
104263
104264
104265
104266
104267
104268
104269
104270
104271
104272
104273
104274
104275
104276
104277
104278
104279
104280
104281
104282
104283
104284
104285
104286
104287
104288
104289
104290
104291
104292
104293
104294
104295
104296
104297
104298
104299
104300
104301
104302
104303
104304
104305
104306
104307
104308
104309
104310
104311
104312
104313
104314
104315
104316
104317
104318
104319
104320
104321
104322
104323
104324
104325
104326
104327
104328
104329
104330
104331
104332
104333
104334
104335
104336
104337
104338
104339
104340
104341
104342
104343
104344
104345
104346
104347
104348
104349
104350
104351
104352
104353
104354
104355
104356
104357
104358
104359
104360
104361
104362
104363
104364
104365
104366
104367
104368
104369
104370
104371
104372
104373
104374
104375
104376
104377
104378
104379
104380
104381
104382
104383
104384
104385
104386
104387
104388
104389
104390
104391
104392
104393
104394
104395
104396
104397
104398
104399
104400
104401
104402
104403
104404
104405
104406
104407
104408
104409
104410
104411
104412
104413
104414
104415
104416
104417
104418
104419
104420
104421
104422
104423
104424
104425
104426
104427
104428
104429
104430
104431
104432
104433
104434
104435
104436
104437
104438
104439
104440
104441
104442
104443
104444
104445
104446
104447
104448
104449
104450
104451
104452
104453
104454
104455
104456
104457
104458
104459
104460
104461
104462
104463
104464
104465
104466
104467
104468
104469
104470
104471
104472
104473
104474
104475
104476
104477
104478
104479
104480
104481
104482
104483
104484
104485
104486
104487
104488
104489
104490
104491
104492
104493
104494
104495
104496
104497
104498
104499
104500
104501
104502
104503
104504
104505
104506
104507
104508
104509
104510
104511
104512
104513
104514
104515
104516
104517
104518
104519
104520
104521
104522
104523
104524
104525
104526
104527
104528
104529
104530
104531
104532
104533
104534
104535
104536
104537
104538
104539
104540
104541
104542
104543
104544
104545
104546
104547
104548
104549
104550
104551
104552
104553
104554
104555
104556
104557
104558
104559
104560
104561
104562
104563
104564
104565
104566
104567
104568
104569
104570
104571
104572
104573
104574
104575
104576
104577
104578
104579
104580
104581
104582
104583
104584
104585
104586
104587
104588
104589
104590
104591
104592
104593
104594
104595
104596
104597
104598
104599
104600
104601
104602
104603
104604
104605
104606
104607
104608
104609
104610
104611
104612
104613
104614
104615
104616
104617
104618
104619
104620
104621
104622
104623
104624
104625
104626
104627
104628
104629
104630
104631
104632
104633
104634
104635
104636
104637
104638
104639
104640
104641
104642
104643
104644
104645
104646
104647
104648
104649
104650
104651
104652
104653
104654
104655
104656
104657
104658
104659
104660
104661
104662
104663
104664
104665
104666
104667
104668
104669
104670
104671
104672
104673
104674
104675
104676
104677
104678
104679
104680
104681
104682
104683
104684
104685
104686
104687
104688
104689
104690
104691
104692
104693
104694
104695
104696
104697
104698
104699
104700
104701
104702
104703
104704
104705
104706
104707
104708
104709
104710
104711
104712
104713
104714
104715
104716
104717
104718
104719
104720
104721
104722
104723
104724
104725
104726
104727
104728
104729
104730
104731
104732
104733
104734
104735
104736
104737
104738
104739
104740
104741
104742
104743
104744
104745
104746
104747
104748
104749
104750
104751
104752
104753
104754
104755
104756
104757
104758
104759
104760
104761
104762
104763
104764
104765
104766
104767
104768
104769
104770
104771
104772
104773
104774
104775
104776
104777
104778
104779
104780
104781
104782
104783
104784
104785
104786
104787
104788
104789
104790
104791
104792
104793
104794
104795
104796
104797
104798
104799
104800
104801
104802
104803
104804
104805
104806
104807
104808
104809
104810
104811
104812
104813
104814
104815
104816
104817
104818
104819
104820
104821
104822
104823
104824
104825
104826
104827
104828
104829
104830
104831
104832
104833
104834
104835
104836
104837
104838
104839
104840
104841
104842
104843
104844
104845
104846
104847
104848
104849
104850
104851
104852
104853
104854
104855
104856
104857
104858
104859
104860
104861
104862
104863
104864
104865
104866
104867
104868
104869
104870
104871
104872
104873
104874
104875
104876
104877
104878
104879
104880
104881
104882
104883
104884
104885
104886
104887
104888
104889
104890
104891
104892
104893
104894
104895
104896
104897
104898
104899
104900
104901
104902
104903
104904
104905
104906
104907
104908
104909
104910
104911
104912
104913
104914
104915
104916
104917
104918
104919
104920
104921
104922
104923
104924
104925
104926
104927
104928
104929
104930
104931
104932
104933
104934
104935
104936
104937
104938
104939
104940
104941
104942
104943
104944
104945
104946
104947
104948
104949
104950
104951
104952
104953
104954
104955
104956
104957
104958
104959
104960
104961
104962
104963
104964
104965
104966
104967
104968
104969
104970
104971
104972
104973
104974
104975
104976
104977
104978
104979
104980
104981
104982
104983
104984
104985
104986
104987
104988
104989
104990
104991
104992
104993
104994
104995
104996
104997
104998
104999
105000
105001
105002
105003
105004
105005
105006
105007
105008
105009
105010
105011
105012
105013
105014
105015
105016
105017
105018
105019
105020
105021
105022
105023
105024
105025
105026
105027
105028
105029
105030
105031
105032
105033
105034
105035
105036
105037
105038
105039
105040
105041
105042
105043
105044
105045
105046
105047
105048
105049
105050
105051
105052
105053
105054
105055
105056
105057
105058
105059
105060
105061
105062
105063
105064
105065
105066
105067
105068
105069
105070
105071
105072
105073
105074
105075
105076
105077
105078
105079
105080
105081
105082
105083
105084
105085
105086
105087
105088
105089
105090
105091
105092
105093
105094
105095
105096
105097
105098
105099
105100
105101
105102
105103
105104
105105
105106
105107
105108
105109
105110
105111
105112
105113
105114
105115
105116
105117
105118
105119
105120
105121
105122
105123
105124
105125
105126
105127
105128
105129
105130
105131
105132
105133
105134
105135
105136
105137
105138
105139
105140
105141
105142
105143
105144
105145
105146
105147
105148
105149
105150
105151
105152
105153
105154
105155
105156
105157
105158
105159
105160
105161
105162
105163
105164
105165
105166
105167
105168
105169
105170
105171
105172
105173
105174
105175
105176
105177
105178
105179
105180
105181
105182
105183
105184
105185
105186
105187
105188
105189
105190
105191
105192
105193
105194
105195
105196
105197
105198
105199
105200
105201
105202
105203
105204
105205
105206
105207
105208
105209
105210
105211
105212
105213
105214
105215
105216
105217
105218
105219
105220
105221
105222
105223
105224
105225
105226
105227
105228
105229
105230
105231
105232
105233
105234
105235
105236
105237
105238
105239
105240
105241
105242
105243
105244
105245
105246
105247
105248
105249
105250
105251
105252
105253
105254
105255
105256
105257
105258
105259
105260
105261
105262
105263
105264
105265
105266
105267
105268
105269
105270
105271
105272
105273
105274
105275
105276
105277
105278
105279
105280
105281
105282
105283
105284
105285
105286
105287
105288
105289
105290
105291
105292
105293
105294
105295
105296
105297
105298
105299
105300
105301
105302
105303
105304
105305
105306
105307
105308
105309
105310
105311
105312
105313
105314
105315
105316
105317
105318
105319
105320
105321
105322
105323
105324
105325
105326
105327
105328
105329
105330
105331
105332
105333
105334
105335
105336
105337
105338
105339
105340
105341
105342
105343
105344
105345
105346
105347
105348
105349
105350
105351
105352
105353
105354
105355
105356
105357
105358
105359
105360
105361
105362
105363
105364
105365
105366
105367
105368
105369
105370
105371
105372
105373
105374
105375
105376
105377
105378
105379
105380
105381
105382
105383
105384
105385
105386
105387
105388
105389
105390
105391
105392
105393
105394
105395
105396
105397
105398
105399
105400
105401
105402
105403
105404
105405
105406
105407
105408
105409
105410
105411
105412
105413
105414
105415
105416
105417
105418
105419
105420
105421
105422
105423
105424
105425
105426
105427
105428
105429
105430
105431
105432
105433
105434
105435
105436
105437
105438
105439
105440
105441
105442
105443
105444
105445
105446
105447
105448
105449
105450
105451
105452
105453
105454
105455
105456
105457
105458
105459
105460
105461
105462
105463
105464
105465
105466
105467
105468
105469
105470
105471
105472
105473
105474
105475
105476
105477
105478
105479
105480
105481
105482
105483
105484
105485
105486
105487
105488
105489
105490
105491
105492
105493
105494
105495
105496
105497
105498
105499
105500
105501
105502
105503
105504
105505
105506
105507
105508
105509
105510
105511
105512
105513
105514
105515
105516
105517
105518
105519
105520
105521
105522
105523
105524
105525
105526
105527
105528
105529
105530
105531
105532
105533
105534
105535
105536
105537
105538
105539
105540
105541
105542
105543
105544
105545
105546
105547
105548
105549
105550
105551
105552
105553
105554
105555
105556
105557
105558
105559
105560
105561
105562
105563
105564
105565
105566
105567
105568
105569
105570
105571
105572
105573
105574
105575
105576
105577
105578
105579
105580
105581
105582
105583
105584
105585
105586
105587
105588
105589
105590
105591
105592
105593
105594
105595
105596
105597
105598
105599
105600
105601
105602
105603
105604
105605
105606
105607
105608
105609
105610
105611
105612
105613
105614
105615
105616
105617
105618
105619
105620
105621
105622
105623
105624
105625
105626
105627
105628
105629
105630
105631
105632
105633
105634
105635
105636
105637
105638
105639
105640
105641
105642
105643
105644
105645
105646
105647
105648
105649
105650
105651
105652
105653
105654
105655
105656
105657
105658
105659
105660
105661
105662
105663
105664
105665
105666
105667
105668
105669
105670
105671
105672
105673
105674
105675
105676
105677
105678
105679
105680
105681
105682
105683
105684
105685
105686
105687
105688
105689
105690
105691
105692
105693
105694
105695
105696
105697
105698
105699
105700
105701
105702
105703
105704
105705
105706
105707
105708
105709
105710
105711
105712
105713
105714
105715
105716
105717
105718
105719
105720
105721
105722
105723
105724
105725
105726
105727
105728
105729
105730
105731
105732
105733
105734
105735
105736
105737
105738
105739
105740
105741
105742
105743
105744
105745
105746
105747
105748
105749
105750
105751
105752
105753
105754
105755
105756
105757
105758
105759
105760
105761
105762
105763
105764
105765
105766
105767
105768
105769
105770
105771
105772
105773
105774
105775
105776
105777
105778
105779
105780
105781
105782
105783
105784
105785
105786
105787
105788
105789
105790
105791
105792
105793
105794
105795
105796
105797
105798
105799
105800
105801
105802
105803
105804
105805
105806
105807
105808
105809
105810
105811
105812
105813
105814
105815
105816
105817
105818
105819
105820
105821
105822
105823
105824
105825
105826
105827
105828
105829
105830
105831
105832
105833
105834
105835
105836
105837
105838
105839
105840
105841
105842
105843
105844
105845
105846
105847
105848
105849
105850
105851
105852
105853
105854
105855
105856
105857
105858
105859
105860
105861
105862
105863
105864
105865
105866
105867
105868
105869
105870
105871
105872
105873
105874
105875
105876
105877
105878
105879
105880
105881
105882
105883
105884
105885
105886
105887
105888
105889
105890
105891
105892
105893
105894
105895
105896
105897
105898
105899
105900
105901
105902
105903
105904
105905
105906
105907
105908
105909
105910
105911
105912
105913
105914
105915
105916
105917
105918
105919
105920
105921
105922
105923
105924
105925
105926
105927
105928
105929
105930
105931
105932
105933
105934
105935
105936
105937
105938
105939
105940
105941
105942
105943
105944
105945
105946
105947
105948
105949
105950
105951
105952
105953
105954
105955
105956
105957
105958
105959
105960
105961
105962
105963
105964
105965
105966
105967
105968
105969
105970
105971
105972
105973
105974
105975
105976
105977
105978
105979
105980
105981
105982
105983
105984
105985
105986
105987
105988
105989
105990
105991
105992
105993
105994
105995
105996
105997
105998
105999
106000
106001
106002
106003
106004
106005
106006
106007
106008
106009
106010
106011
106012
106013
106014
106015
106016
106017
106018
106019
106020
106021
106022
106023
106024
106025
106026
106027
106028
106029
106030
106031
106032
106033
106034
106035
106036
106037
106038
106039
106040
106041
106042
106043
106044
106045
106046
106047
106048
106049
106050
106051
106052
106053
106054
106055
106056
106057
106058
106059
106060
106061
106062
106063
106064
106065
106066
106067
106068
106069
106070
106071
106072
106073
106074
106075
106076
106077
106078
106079
106080
106081
106082
106083
106084
106085
106086
106087
106088
106089
106090
106091
106092
106093
106094
106095
106096
106097
106098
106099
106100
106101
106102
106103
106104
106105
106106
106107
106108
106109
106110
106111
106112
106113
106114
106115
106116
106117
106118
106119
106120
106121
106122
106123
106124
106125
106126
106127
106128
106129
106130
106131
106132
106133
106134
106135
106136
106137
106138
106139
106140
106141
106142
106143
106144
106145
106146
106147
106148
106149
106150
106151
106152
106153
106154
106155
106156
106157
106158
106159
106160
106161
106162
106163
106164
106165
106166
106167
106168
106169
106170
106171
106172
106173
106174
106175
106176
106177
106178
106179
106180
106181
106182
106183
106184
106185
106186
106187
106188
106189
106190
106191
106192
106193
106194
106195
106196
106197
106198
106199
106200
106201
106202
106203
106204
106205
106206
106207
106208
106209
106210
106211
106212
106213
106214
106215
106216
106217
106218
106219
106220
106221
106222
106223
106224
106225
106226
106227
106228
106229
106230
106231
106232
106233
106234
106235
106236
106237
106238
106239
106240
106241
106242
106243
106244
106245
106246
106247
106248
106249
106250
106251
106252
106253
106254
106255
106256
106257
106258
106259
106260
106261
106262
106263
106264
106265
106266
106267
106268
106269
106270
106271
106272
106273
106274
106275
106276
106277
106278
106279
106280
106281
106282
106283
106284
106285
106286
106287
106288
106289
106290
106291
106292
106293
106294
106295
106296
106297
106298
106299
106300
106301
106302
106303
106304
106305
106306
106307
106308
106309
106310
106311
106312
106313
106314
106315
106316
106317
106318
106319
106320
106321
106322
106323
106324
106325
106326
106327
106328
106329
106330
106331
106332
106333
106334
106335
106336
106337
106338
106339
106340
106341
106342
106343
106344
106345
106346
106347
106348
106349
106350
106351
106352
106353
106354
106355
106356
106357
106358
106359
106360
106361
106362
106363
106364
106365
106366
106367
106368
106369
106370
106371
106372
106373
106374
106375
106376
106377
106378
106379
106380
106381
106382
106383
106384
106385
106386
106387
106388
106389
106390
106391
106392
106393
106394
106395
106396
106397
106398
106399
106400
106401
106402
106403
106404
106405
106406
106407
106408
106409
106410
106411
106412
106413
106414
106415
106416
106417
106418
106419
106420
106421
106422
106423
106424
106425
106426
106427
106428
106429
106430
106431
106432
106433
106434
106435
106436
106437
106438
106439
106440
106441
106442
106443
106444
106445
106446
106447
106448
106449
106450
106451
106452
106453
106454
106455
106456
106457
106458
106459
106460
106461
106462
106463
106464
106465
106466
106467
106468
106469
106470
106471
106472
106473
106474
106475
106476
106477
106478
106479
106480
106481
106482
106483
106484
106485
106486
106487
106488
106489
106490
106491
106492
106493
106494
106495
106496
106497
106498
106499
106500
106501
106502
106503
106504
106505
106506
106507
106508
106509
106510
106511
106512
106513
106514
106515
106516
106517
106518
106519
106520
106521
106522
106523
106524
106525
106526
106527
106528
106529
106530
106531
106532
106533
106534
106535
106536
106537
106538
106539
106540
106541
106542
106543
106544
106545
106546
106547
106548
106549
106550
106551
106552
106553
106554
106555
106556
106557
106558
106559
106560
106561
106562
106563
106564
106565
106566
106567
106568
106569
106570
106571
106572
106573
106574
106575
106576
106577
106578
106579
106580
106581
106582
106583
106584
106585
106586
106587
106588
106589
106590
106591
106592
106593
106594
106595
106596
106597
106598
106599
106600
106601
106602
106603
106604
106605
106606
106607
106608
106609
106610
106611
106612
106613
106614
106615
106616
106617
106618
106619
106620
106621
106622
106623
106624
106625
106626
106627
106628
106629
106630
106631
106632
106633
106634
106635
106636
106637
106638
106639
106640
106641
106642
106643
106644
106645
106646
106647
106648
106649
106650
106651
106652
106653
106654
106655
106656
106657
106658
106659
106660
106661
106662
106663
106664
106665
106666
106667
106668
106669
106670
106671
106672
106673
106674
106675
106676
106677
106678
106679
106680
106681
106682
106683
106684
106685
106686
106687
106688
106689
106690
106691
106692
106693
106694
106695
106696
106697
106698
106699
106700
106701
106702
106703
106704
106705
106706
106707
106708
106709
106710
106711
106712
106713
106714
106715
106716
106717
106718
106719
106720
106721
106722
106723
106724
106725
106726
106727
106728
106729
106730
106731
106732
106733
106734
106735
106736
106737
106738
106739
106740
106741
106742
106743
106744
106745
106746
106747
106748
106749
106750
106751
106752
106753
106754
106755
106756
106757
106758
106759
106760
106761
106762
106763
106764
106765
106766
106767
106768
106769
106770
106771
106772
106773
106774
106775
106776
106777
106778
106779
106780
106781
106782
106783
106784
106785
106786
106787
106788
106789
106790
106791
106792
106793
106794
106795
106796
106797
106798
106799
106800
106801
106802
106803
106804
106805
106806
106807
106808
106809
106810
106811
106812
106813
106814
106815
106816
106817
106818
106819
106820
106821
106822
106823
106824
106825
106826
106827
106828
106829
106830
106831
106832
106833
106834
106835
106836
106837
106838
106839
106840
106841
106842
106843
106844
106845
106846
106847
106848
106849
106850
106851
106852
106853
106854
106855
106856
106857
106858
106859
106860
106861
106862
106863
106864
106865
106866
106867
106868
106869
106870
106871
106872
106873
106874
106875
106876
106877
106878
106879
106880
106881
106882
106883
106884
106885
106886
106887
106888
106889
106890
106891
106892
106893
106894
106895
106896
106897
106898
106899
106900
106901
106902
106903
106904
106905
106906
106907
106908
106909
106910
106911
106912
106913
106914
106915
106916
106917
106918
106919
106920
106921
106922
106923
106924
106925
106926
106927
106928
106929
106930
106931
106932
106933
106934
106935
106936
106937
106938
106939
106940
106941
106942
106943
106944
106945
106946
106947
106948
106949
106950
106951
106952
106953
106954
106955
106956
106957
106958
106959
106960
106961
106962
106963
106964
106965
106966
106967
106968
106969
106970
106971
106972
106973
106974
106975
106976
106977
106978
106979
106980
106981
106982
106983
106984
106985
106986
106987
106988
106989
106990
106991
106992
106993
106994
106995
106996
106997
106998
106999
107000
107001
107002
107003
107004
107005
107006
107007
107008
107009
107010
107011
107012
107013
107014
107015
107016
107017
107018
107019
107020
107021
107022
107023
107024
107025
107026
107027
107028
107029
107030
107031
107032
107033
107034
107035
107036
107037
107038
107039
107040
107041
107042
107043
107044
107045
107046
107047
107048
107049
107050
107051
107052
107053
107054
107055
107056
107057
107058
107059
107060
107061
107062
107063
107064
107065
107066
107067
107068
107069
107070
107071
107072
107073
107074
107075
107076
107077
107078
107079
107080
107081
107082
107083
107084
107085
107086
107087
107088
107089
107090
107091
107092
107093
107094
107095
107096
107097
107098
107099
107100
107101
107102
107103
107104
107105
107106
107107
107108
107109
107110
107111
107112
107113
107114
107115
107116
107117
107118
107119
107120
107121
107122
107123
107124
107125
107126
107127
107128
107129
107130
107131
107132
107133
107134
107135
107136
107137
107138
107139
107140
107141
107142
107143
107144
107145
107146
107147
107148
107149
107150
107151
107152
107153
107154
107155
107156
107157
107158
107159
107160
107161
107162
107163
107164
107165
107166
107167
107168
107169
107170
107171
107172
107173
107174
107175
107176
107177
107178
107179
107180
107181
107182
107183
107184
107185
107186
107187
107188
107189
107190
107191
107192
107193
107194
107195
107196
107197
107198
107199
107200
107201
107202
107203
107204
107205
107206
107207
107208
107209
107210
107211
107212
107213
107214
107215
107216
107217
107218
107219
107220
107221
107222
107223
107224
107225
107226
107227
107228
107229
107230
107231
107232
107233
107234
107235
107236
107237
107238
107239
107240
107241
107242
107243
107244
107245
107246
107247
107248
107249
107250
107251
107252
107253
107254
107255
107256
107257
107258
107259
107260
107261
107262
107263
107264
107265
107266
107267
107268
107269
107270
107271
107272
107273
107274
107275
107276
107277
107278
107279
107280
107281
107282
107283
107284
107285
107286
107287
107288
107289
107290
107291
107292
107293
107294
107295
107296
107297
107298
107299
107300
107301
107302
107303
107304
107305
107306
107307
107308
107309
107310
107311
107312
107313
107314
107315
107316
107317
107318
107319
107320
107321
107322
107323
107324
107325
107326
107327
107328
107329
107330
107331
107332
107333
107334
107335
107336
107337
107338
107339
107340
107341
107342
107343
107344
107345
107346
107347
107348
107349
107350
107351
107352
107353
107354
107355
107356
107357
107358
107359
107360
107361
107362
107363
107364
107365
107366
107367
107368
107369
107370
107371
107372
107373
107374
107375
107376
107377
107378
107379
107380
107381
107382
107383
107384
107385
107386
107387
107388
107389
107390
107391
107392
107393
107394
107395
107396
107397
107398
107399
107400
107401
107402
107403
107404
107405
107406
107407
107408
107409
107410
107411
107412
107413
107414
107415
107416
107417
107418
107419
107420
107421
107422
107423
107424
107425
107426
107427
107428
107429
107430
107431
107432
107433
107434
107435
107436
107437
107438
107439
107440
107441
107442
107443
107444
107445
107446
107447
107448
107449
107450
107451
107452
107453
107454
107455
107456
107457
107458
107459
107460
107461
107462
107463
107464
107465
107466
107467
107468
107469
107470
107471
107472
107473
107474
107475
107476
107477
107478
107479
107480
107481
107482
107483
107484
107485
107486
107487
107488
107489
107490
107491
107492
107493
107494
107495
107496
107497
107498
107499
107500
107501
107502
107503
107504
107505
107506
107507
107508
107509
107510
107511
107512
107513
107514
107515
107516
107517
107518
107519
107520
107521
107522
107523
107524
107525
107526
107527
107528
107529
107530
107531
107532
107533
107534
107535
107536
107537
107538
107539
107540
107541
107542
107543
107544
107545
107546
107547
107548
107549
107550
107551
107552
107553
107554
107555
107556
107557
107558
107559
107560
107561
107562
107563
107564
107565
107566
107567
107568
107569
107570
107571
107572
107573
107574
107575
107576
107577
107578
107579
107580
107581
107582
107583
107584
107585
107586
107587
107588
107589
107590
107591
107592
107593
107594
107595
107596
107597
107598
107599
107600
107601
107602
107603
107604
107605
107606
107607
107608
107609
107610
107611
107612
107613
107614
107615
107616
107617
107618
107619
107620
107621
107622
107623
107624
107625
107626
107627
107628
107629
107630
107631
107632
107633
107634
107635
107636
107637
107638
107639
107640
107641
107642
107643
107644
107645
107646
107647
107648
107649
107650
107651
107652
107653
107654
107655
107656
107657
107658
107659
107660
107661
107662
107663
107664
107665
107666
107667
107668
107669
107670
107671
107672
107673
107674
107675
107676
107677
107678
107679
107680
107681
107682
107683
107684
107685
107686
107687
107688
107689
107690
107691
107692
107693
107694
107695
107696
107697
107698
107699
107700
107701
107702
107703
107704
107705
107706
107707
107708
107709
107710
107711
107712
107713
107714
107715
107716
107717
107718
107719
107720
107721
107722
107723
107724
107725
107726
107727
107728
107729
107730
107731
107732
107733
107734
107735
107736
107737
107738
107739
107740
107741
107742
107743
107744
107745
107746
107747
107748
107749
107750
107751
107752
107753
107754
107755
107756
107757
107758
107759
107760
107761
107762
107763
107764
107765
107766
107767
107768
107769
107770
107771
107772
107773
107774
107775
107776
107777
107778
107779
107780
107781
107782
107783
107784
107785
107786
107787
107788
107789
107790
107791
107792
107793
107794
107795
107796
107797
107798
107799
107800
107801
107802
107803
107804
107805
107806
107807
107808
107809
107810
107811
107812
107813
107814
107815
107816
107817
107818
107819
107820
107821
107822
107823
107824
107825
107826
107827
107828
107829
107830
107831
107832
107833
107834
107835
107836
107837
107838
107839
107840
107841
107842
107843
107844
107845
107846
107847
107848
107849
107850
107851
107852
107853
107854
107855
107856
107857
107858
107859
107860
107861
107862
107863
107864
107865
107866
107867
107868
107869
107870
107871
107872
107873
107874
107875
107876
107877
107878
107879
107880
107881
107882
107883
107884
107885
107886
107887
107888
107889
107890
107891
107892
107893
107894
107895
107896
107897
107898
107899
107900
107901
107902
107903
107904
107905
107906
107907
107908
107909
107910
107911
107912
107913
107914
107915
107916
107917
107918
107919
107920
107921
107922
107923
107924
107925
107926
107927
107928
107929
107930
107931
107932
107933
107934
107935
107936
107937
107938
107939
107940
107941
107942
107943
107944
107945
107946
107947
107948
107949
107950
107951
107952
107953
107954
107955
107956
107957
107958
107959
107960
107961
107962
107963
107964
107965
107966
107967
107968
107969
107970
107971
107972
107973
107974
107975
107976
107977
107978
107979
107980
107981
107982
107983
107984
107985
107986
107987
107988
107989
107990
107991
107992
107993
107994
107995
107996
107997
107998
107999
108000
108001
108002
108003
108004
108005
108006
108007
108008
108009
108010
108011
108012
108013
108014
108015
108016
108017
108018
108019
108020
108021
108022
108023
108024
108025
108026
108027
108028
108029
108030
108031
108032
108033
108034
108035
108036
108037
108038
108039
108040
108041
108042
108043
108044
108045
108046
108047
108048
108049
108050
108051
108052
108053
108054
108055
108056
108057
108058
108059
108060
108061
108062
108063
108064
108065
108066
108067
108068
108069
108070
108071
108072
108073
108074
108075
108076
108077
108078
108079
108080
108081
108082
108083
108084
108085
108086
108087
108088
108089
108090
108091
108092
108093
108094
108095
108096
108097
108098
108099
108100
108101
108102
108103
108104
108105
108106
108107
108108
108109
108110
108111
108112
108113
108114
108115
108116
108117
108118
108119
108120
108121
108122
108123
108124
108125
108126
108127
108128
108129
108130
108131
108132
108133
108134
108135
108136
108137
108138
108139
108140
108141
108142
108143
108144
108145
108146
108147
108148
108149
108150
108151
108152
108153
108154
108155
108156
108157
108158
108159
108160
108161
108162
108163
108164
108165
108166
108167
108168
108169
108170
108171
108172
108173
108174
108175
108176
108177
108178
108179
108180
108181
108182
108183
108184
108185
108186
108187
108188
108189
108190
108191
108192
108193
108194
108195
108196
108197
108198
108199
108200
108201
108202
108203
108204
108205
108206
108207
108208
108209
108210
108211
108212
108213
108214
108215
108216
108217
108218
108219
108220
108221
108222
108223
108224
108225
108226
108227
108228
108229
108230
108231
108232
108233
108234
108235
108236
108237
108238
108239
108240
108241
108242
108243
108244
108245
108246
108247
108248
108249
108250
108251
108252
108253
108254
108255
108256
108257
108258
108259
108260
108261
108262
108263
108264
108265
108266
108267
108268
108269
108270
108271
108272
108273
108274
108275
108276
108277
108278
108279
108280
108281
108282
108283
108284
108285
108286
108287
108288
108289
108290
108291
108292
108293
108294
108295
108296
108297
108298
108299
108300
108301
108302
108303
108304
108305
108306
108307
108308
108309
108310
108311
108312
108313
108314
108315
108316
108317
108318
108319
108320
108321
108322
108323
108324
108325
108326
108327
108328
108329
108330
108331
108332
108333
108334
108335
108336
108337
108338
108339
108340
108341
108342
108343
108344
108345
108346
108347
108348
108349
108350
108351
108352
108353
108354
108355
108356
108357
108358
108359
108360
108361
108362
108363
108364
108365
108366
108367
108368
108369
108370
108371
108372
108373
108374
108375
108376
108377
108378
108379
108380
108381
108382
108383
108384
108385
108386
108387
108388
108389
108390
108391
108392
108393
108394
108395
108396
108397
108398
108399
108400
108401
108402
108403
108404
108405
108406
108407
108408
108409
108410
108411
108412
108413
108414
108415
108416
108417
108418
108419
108420
108421
108422
108423
108424
108425
108426
108427
108428
108429
108430
108431
108432
108433
108434
108435
108436
108437
108438
108439
108440
108441
108442
108443
108444
108445
108446
108447
108448
108449
108450
108451
108452
108453
108454
108455
108456
108457
108458
108459
108460
108461
108462
108463
108464
108465
108466
108467
108468
108469
108470
108471
108472
108473
108474
108475
108476
108477
108478
108479
108480
108481
108482
108483
108484
108485
108486
108487
108488
108489
108490
108491
108492
108493
108494
108495
108496
108497
108498
108499
108500
108501
108502
108503
108504
108505
108506
108507
108508
108509
108510
108511
108512
108513
108514
108515
108516
108517
108518
108519
108520
108521
108522
108523
108524
108525
108526
108527
108528
108529
108530
108531
108532
108533
108534
108535
108536
108537
108538
108539
108540
108541
108542
108543
108544
108545
108546
108547
108548
108549
108550
108551
108552
108553
108554
108555
108556
108557
108558
108559
108560
108561
108562
108563
108564
108565
108566
108567
108568
108569
108570
108571
108572
108573
108574
108575
108576
108577
108578
108579
108580
108581
108582
108583
108584
108585
108586
108587
108588
108589
108590
108591
108592
108593
108594
108595
108596
108597
108598
108599
108600
108601
108602
108603
108604
108605
108606
108607
108608
108609
108610
108611
108612
108613
108614
108615
108616
108617
108618
108619
108620
108621
108622
108623
108624
108625
108626
108627
108628
108629
108630
108631
108632
108633
108634
108635
108636
108637
108638
108639
108640
108641
108642
108643
108644
108645
108646
108647
108648
108649
108650
108651
108652
108653
108654
108655
108656
108657
108658
108659
108660
108661
108662
108663
108664
108665
108666
108667
108668
108669
108670
108671
108672
108673
108674
108675
108676
108677
108678
108679
108680
108681
108682
108683
108684
108685
108686
108687
108688
108689
108690
108691
108692
108693
108694
108695
108696
108697
108698
108699
108700
108701
108702
108703
108704
108705
108706
108707
108708
108709
108710
108711
108712
108713
108714
108715
108716
108717
108718
108719
108720
108721
108722
108723
108724
108725
108726
108727
108728
108729
108730
108731
108732
108733
108734
108735
108736
108737
108738
108739
108740
108741
108742
108743
108744
108745
108746
108747
108748
108749
108750
108751
108752
108753
108754
108755
108756
108757
108758
108759
108760
108761
108762
108763
108764
108765
108766
108767
108768
108769
108770
108771
108772
108773
108774
108775
108776
108777
108778
108779
108780
108781
108782
108783
108784
108785
108786
108787
108788
108789
108790
108791
108792
108793
108794
108795
108796
108797
108798
108799
108800
108801
108802
108803
108804
108805
108806
108807
108808
108809
108810
108811
108812
108813
108814
108815
108816
108817
108818
108819
108820
108821
108822
108823
108824
108825
108826
108827
108828
108829
108830
108831
108832
108833
108834
108835
108836
108837
108838
108839
108840
108841
108842
108843
108844
108845
108846
108847
108848
108849
108850
108851
108852
108853
108854
108855
108856
108857
108858
108859
108860
108861
108862
108863
108864
108865
108866
108867
108868
108869
108870
108871
108872
108873
108874
108875
108876
108877
108878
108879
108880
108881
108882
108883
108884
108885
108886
108887
108888
108889
108890
108891
108892
108893
108894
108895
108896
108897
108898
108899
108900
108901
108902
108903
108904
108905
108906
108907
108908
108909
108910
108911
108912
108913
108914
108915
108916
108917
108918
108919
108920
108921
108922
108923
108924
108925
108926
108927
108928
108929
108930
108931
108932
108933
108934
108935
108936
108937
108938
108939
108940
108941
108942
108943
108944
108945
108946
108947
108948
108949
108950
108951
108952
108953
108954
108955
108956
108957
108958
108959
108960
108961
108962
108963
108964
108965
108966
108967
108968
108969
108970
108971
108972
108973
108974
108975
108976
108977
108978
108979
108980
108981
108982
108983
108984
108985
108986
108987
108988
108989
108990
108991
108992
108993
108994
108995
108996
108997
108998
108999
109000
109001
109002
109003
109004
109005
109006
109007
109008
109009
109010
109011
109012
109013
109014
109015
109016
109017
109018
109019
109020
109021
109022
109023
109024
109025
109026
109027
109028
109029
109030
109031
109032
109033
109034
109035
109036
109037
109038
109039
109040
109041
109042
109043
109044
109045
109046
109047
109048
109049
109050
109051
109052
109053
109054
109055
109056
109057
109058
109059
109060
109061
109062
109063
109064
109065
109066
109067
109068
109069
109070
109071
109072
109073
109074
109075
109076
109077
109078
109079
109080
109081
109082
109083
109084
109085
109086
109087
109088
109089
109090
109091
109092
109093
109094
109095
109096
109097
109098
109099
109100
109101
109102
109103
109104
109105
109106
109107
109108
109109
109110
109111
109112
109113
109114
109115
109116
109117
109118
109119
109120
109121
109122
109123
109124
109125
109126
109127
109128
109129
109130
109131
109132
109133
109134
109135
109136
109137
109138
109139
109140
109141
109142
109143
109144
109145
109146
109147
109148
109149
109150
109151
109152
109153
109154
109155
109156
109157
109158
109159
109160
109161
109162
109163
109164
109165
109166
109167
109168
109169
109170
109171
109172
109173
109174
109175
109176
109177
109178
109179
109180
109181
109182
109183
109184
109185
109186
109187
109188
109189
109190
109191
109192
109193
109194
109195
109196
109197
109198
109199
109200
109201
109202
109203
109204
109205
109206
109207
109208
109209
109210
109211
109212
109213
109214
109215
109216
109217
109218
109219
109220
109221
109222
109223
109224
109225
109226
109227
109228
109229
109230
109231
109232
109233
109234
109235
109236
109237
109238
109239
109240
109241
109242
109243
109244
109245
109246
109247
109248
109249
109250
109251
109252
109253
109254
109255
109256
109257
109258
109259
109260
109261
109262
109263
109264
109265
109266
109267
109268
109269
109270
109271
109272
109273
109274
109275
109276
109277
109278
109279
109280
109281
109282
109283
109284
109285
109286
109287
109288
109289
109290
109291
109292
109293
109294
109295
109296
109297
109298
109299
109300
109301
109302
109303
109304
109305
109306
109307
109308
109309
109310
109311
109312
109313
109314
109315
109316
109317
109318
109319
109320
109321
109322
109323
109324
109325
109326
109327
109328
109329
109330
109331
109332
109333
109334
109335
109336
109337
109338
109339
109340
109341
109342
109343
109344
109345
109346
109347
109348
109349
109350
109351
109352
109353
109354
109355
109356
109357
109358
109359
109360
109361
109362
109363
109364
109365
109366
109367
109368
109369
109370
109371
109372
109373
109374
109375
109376
109377
109378
109379
109380
109381
109382
109383
109384
109385
109386
109387
109388
109389
109390
109391
109392
109393
109394
109395
109396
109397
109398
109399
109400
109401
109402
109403
109404
109405
109406
109407
109408
109409
109410
109411
109412
109413
109414
109415
109416
109417
109418
109419
109420
109421
109422
109423
109424
109425
109426
109427
109428
109429
109430
109431
109432
109433
109434
109435
109436
109437
109438
109439
109440
109441
109442
109443
109444
109445
109446
109447
109448
109449
109450
109451
109452
109453
109454
109455
109456
109457
109458
109459
109460
109461
109462
109463
109464
109465
109466
109467
109468
109469
109470
109471
109472
109473
109474
109475
109476
109477
109478
109479
109480
109481
109482
109483
109484
109485
109486
109487
109488
109489
109490
109491
109492
109493
109494
109495
109496
109497
109498
109499
109500
109501
109502
109503
109504
109505
109506
109507
109508
109509
109510
109511
109512
109513
109514
109515
109516
109517
109518
109519
109520
109521
109522
109523
109524
109525
109526
109527
109528
109529
109530
109531
109532
109533
109534
109535
109536
109537
109538
109539
109540
109541
109542
109543
109544
109545
109546
109547
109548
109549
109550
109551
109552
109553
109554
109555
109556
109557
109558
109559
109560
109561
109562
109563
109564
109565
109566
109567
109568
109569
109570
109571
109572
109573
109574
109575
109576
109577
109578
109579
109580
109581
109582
109583
109584
109585
109586
109587
109588
109589
109590
109591
109592
109593
109594
109595
109596
109597
109598
109599
109600
109601
109602
109603
109604
109605
109606
109607
109608
109609
109610
109611
109612
109613
109614
109615
109616
109617
109618
109619
109620
109621
109622
109623
109624
109625
109626
109627
109628
109629
109630
109631
109632
109633
109634
109635
109636
109637
109638
109639
109640
109641
109642
109643
109644
109645
109646
109647
109648
109649
109650
109651
109652
109653
109654
109655
109656
109657
109658
109659
109660
109661
109662
109663
109664
109665
109666
109667
109668
109669
109670
109671
109672
109673
109674
109675
109676
109677
109678
109679
109680
109681
109682
109683
109684
109685
109686
109687
109688
109689
109690
109691
109692
109693
109694
109695
109696
109697
109698
109699
109700
109701
109702
109703
109704
109705
109706
109707
109708
109709
109710
109711
109712
109713
109714
109715
109716
109717
109718
109719
109720
109721
109722
109723
109724
109725
109726
109727
109728
109729
109730
109731
109732
109733
109734
109735
109736
109737
109738
109739
109740
109741
109742
109743
109744
109745
109746
109747
109748
109749
109750
109751
109752
109753
109754
109755
109756
109757
109758
109759
109760
109761
109762
109763
109764
109765
109766
109767
109768
109769
109770
109771
109772
109773
109774
109775
109776
109777
109778
109779
109780
109781
109782
109783
109784
109785
109786
109787
109788
109789
109790
109791
109792
109793
109794
109795
109796
109797
109798
109799
109800
109801
109802
109803
109804
109805
109806
109807
109808
109809
109810
109811
109812
109813
109814
109815
109816
109817
109818
109819
109820
109821
109822
109823
109824
109825
109826
109827
109828
109829
109830
109831
109832
109833
109834
109835
109836
109837
109838
109839
109840
109841
109842
109843
109844
109845
109846
109847
109848
109849
109850
109851
109852
109853
109854
109855
109856
109857
109858
109859
109860
109861
109862
109863
109864
109865
109866
109867
109868
109869
109870
109871
109872
109873
109874
109875
109876
109877
109878
109879
109880
109881
109882
109883
109884
109885
109886
109887
109888
109889
109890
109891
109892
109893
109894
109895
109896
109897
109898
109899
109900
109901
109902
109903
109904
109905
109906
109907
109908
109909
109910
109911
109912
109913
109914
109915
109916
109917
109918
109919
109920
109921
109922
109923
109924
109925
109926
109927
109928
109929
109930
109931
109932
109933
109934
109935
109936
109937
109938
109939
109940
109941
109942
109943
109944
109945
109946
109947
109948
109949
109950
109951
109952
109953
109954
109955
109956
109957
109958
109959
109960
109961
109962
109963
109964
109965
109966
109967
109968
109969
109970
109971
109972
109973
109974
109975
109976
109977
109978
109979
109980
109981
109982
109983
109984
109985
109986
109987
109988
109989
109990
109991
109992
109993
109994
109995
109996
109997
109998
109999
110000
110001
110002
110003
110004
110005
110006
110007
110008
110009
110010
110011
110012
110013
110014
110015
110016
110017
110018
110019
110020
110021
110022
110023
110024
110025
110026
110027
110028
110029
110030
110031
110032
110033
110034
110035
110036
110037
110038
110039
110040
110041
110042
110043
110044
110045
110046
110047
110048
110049
110050
110051
110052
110053
110054
110055
110056
110057
110058
110059
110060
110061
110062
110063
110064
110065
110066
110067
110068
110069
110070
110071
110072
110073
110074
110075
110076
110077
110078
110079
110080
110081
110082
110083
110084
110085
110086
110087
110088
110089
110090
110091
110092
110093
110094
110095
110096
110097
110098
110099
110100
110101
110102
110103
110104
110105
110106
110107
110108
110109
110110
110111
110112
110113
110114
110115
110116
110117
110118
110119
110120
110121
110122
110123
110124
110125
110126
110127
110128
110129
110130
110131
110132
110133
110134
110135
110136
110137
110138
110139
110140
110141
110142
110143
110144
110145
110146
110147
110148
110149
110150
110151
110152
110153
110154
110155
110156
110157
110158
110159
110160
110161
110162
110163
110164
110165
110166
110167
110168
110169
110170
110171
110172
110173
110174
110175
110176
110177
110178
110179
110180
110181
110182
110183
110184
110185
110186
110187
110188
110189
110190
110191
110192
110193
110194
110195
110196
110197
110198
110199
110200
110201
110202
110203
110204
110205
110206
110207
110208
110209
110210
110211
110212
110213
110214
110215
110216
110217
110218
110219
110220
110221
110222
110223
110224
110225
110226
110227
110228
110229
110230
110231
110232
110233
110234
110235
110236
110237
110238
110239
110240
110241
110242
110243
110244
110245
110246
110247
110248
110249
110250
110251
110252
110253
110254
110255
110256
110257
110258
110259
110260
110261
110262
110263
110264
110265
110266
110267
110268
110269
110270
110271
110272
110273
110274
110275
110276
110277
110278
110279
110280
110281
110282
110283
110284
110285
110286
110287
110288
110289
110290
110291
110292
110293
110294
110295
110296
110297
110298
110299
110300
110301
110302
110303
110304
110305
110306
110307
110308
110309
110310
110311
110312
110313
110314
110315
110316
110317
110318
110319
110320
110321
110322
110323
110324
110325
110326
110327
110328
110329
110330
110331
110332
110333
110334
110335
110336
110337
110338
110339
110340
110341
110342
110343
110344
110345
110346
110347
110348
110349
110350
110351
110352
110353
110354
110355
110356
110357
110358
110359
110360
110361
110362
110363
110364
110365
110366
110367
110368
110369
110370
110371
110372
110373
110374
110375
110376
110377
110378
110379
110380
110381
110382
110383
110384
110385
110386
110387
110388
110389
110390
110391
110392
110393
110394
110395
110396
110397
110398
110399
110400
110401
110402
110403
110404
110405
110406
110407
110408
110409
110410
110411
110412
110413
110414
110415
110416
110417
110418
110419
110420
110421
110422
110423
110424
110425
110426
110427
110428
110429
110430
110431
110432
110433
110434
110435
110436
110437
110438
110439
110440
110441
110442
110443
110444
110445
110446
110447
110448
110449
110450
110451
110452
110453
110454
110455
110456
110457
110458
110459
110460
110461
110462
110463
110464
110465
110466
110467
110468
110469
110470
110471
110472
110473
110474
110475
110476
110477
110478
110479
110480
110481
110482
110483
110484
110485
110486
110487
110488
110489
110490
110491
110492
110493
110494
110495
110496
110497
110498
110499
110500
110501
110502
110503
110504
110505
110506
110507
110508
110509
110510
110511
110512
110513
110514
110515
110516
110517
110518
110519
110520
110521
110522
110523
110524
110525
110526
110527
110528
110529
110530
110531
110532
110533
110534
110535
110536
110537
110538
110539
110540
110541
110542
110543
110544
110545
110546
110547
110548
110549
110550
110551
110552
110553
110554
110555
110556
110557
110558
110559
110560
110561
110562
110563
110564
110565
110566
110567
110568
110569
110570
110571
110572
110573
110574
110575
110576
110577
110578
110579
110580
110581
110582
110583
110584
110585
110586
110587
110588
110589
110590
110591
110592
110593
110594
110595
110596
110597
110598
110599
110600
110601
110602
110603
110604
110605
110606
110607
110608
110609
110610
110611
110612
110613
110614
110615
110616
110617
110618
110619
110620
110621
110622
110623
110624
110625
110626
110627
110628
110629
110630
110631
110632
110633
110634
110635
110636
110637
110638
110639
110640
110641
110642
110643
110644
110645
110646
110647
110648
110649
110650
110651
110652
110653
110654
110655
110656
110657
110658
110659
110660
110661
110662
110663
110664
110665
110666
110667
110668
110669
110670
110671
110672
110673
110674
110675
110676
110677
110678
110679
110680
110681
110682
110683
110684
110685
110686
110687
110688
110689
110690
110691
110692
110693
110694
110695
110696
110697
110698
110699
110700
110701
110702
110703
110704
110705
110706
110707
110708
110709
110710
110711
110712
110713
110714
110715
110716
110717
110718
110719
110720
110721
110722
110723
110724
110725
110726
110727
110728
110729
110730
110731
110732
110733
110734
110735
110736
110737
110738
110739
110740
110741
110742
110743
110744
110745
110746
110747
110748
110749
110750
110751
110752
110753
110754
110755
110756
110757
110758
110759
110760
110761
110762
110763
110764
110765
110766
110767
110768
110769
110770
110771
110772
110773
110774
110775
110776
110777
110778
110779
110780
110781
110782
110783
110784
110785
110786
110787
110788
110789
110790
110791
110792
110793
110794
110795
110796
110797
110798
110799
110800
110801
110802
110803
110804
110805
110806
110807
110808
110809
110810
110811
110812
110813
110814
110815
110816
110817
110818
110819
110820
110821
110822
110823
110824
110825
110826
110827
110828
110829
110830
110831
110832
110833
110834
110835
110836
110837
110838
110839
110840
110841
110842
110843
110844
110845
110846
110847
110848
110849
110850
110851
110852
110853
110854
110855
110856
110857
110858
110859
110860
110861
110862
110863
110864
110865
110866
110867
110868
110869
110870
110871
110872
110873
110874
110875
110876
110877
110878
110879
110880
110881
110882
110883
110884
110885
110886
110887
110888
110889
110890
110891
110892
110893
110894
110895
110896
110897
110898
110899
110900
110901
110902
110903
110904
110905
110906
110907
110908
110909
110910
110911
110912
110913
110914
110915
110916
110917
110918
110919
110920
110921
110922
110923
110924
110925
110926
110927
110928
110929
110930
110931
110932
110933
110934
110935
110936
110937
110938
110939
110940
110941
110942
110943
110944
110945
110946
110947
110948
110949
110950
110951
110952
110953
110954
110955
110956
110957
110958
110959
110960
110961
110962
110963
110964
110965
110966
110967
110968
110969
110970
110971
110972
110973
110974
110975
110976
110977
110978
110979
110980
110981
110982
110983
110984
110985
110986
110987
110988
110989
110990
110991
110992
110993
110994
110995
110996
110997
110998
110999
111000
111001
111002
111003
111004
111005
111006
111007
111008
111009
111010
111011
111012
111013
111014
111015
111016
111017
111018
111019
111020
111021
111022
111023
111024
111025
111026
111027
111028
111029
111030
111031
111032
111033
111034
111035
111036
111037
111038
111039
111040
111041
111042
111043
111044
111045
111046
111047
111048
111049
111050
111051
111052
111053
111054
111055
111056
111057
111058
111059
111060
111061
111062
111063
111064
111065
111066
111067
111068
111069
111070
111071
111072
111073
111074
111075
111076
111077
111078
111079
111080
111081
111082
111083
111084
111085
111086
111087
111088
111089
111090
111091
111092
111093
111094
111095
111096
111097
111098
111099
111100
111101
111102
111103
111104
111105
111106
111107
111108
111109
111110
111111
111112
111113
111114
111115
111116
111117
111118
111119
111120
111121
111122
111123
111124
111125
111126
111127
111128
111129
111130
111131
111132
111133
111134
111135
111136
111137
111138
111139
111140
111141
111142
111143
111144
111145
111146
111147
111148
111149
111150
111151
111152
111153
111154
111155
111156
111157
111158
111159
111160
111161
111162
111163
111164
111165
111166
111167
111168
111169
111170
111171
111172
111173
111174
111175
111176
111177
111178
111179
111180
111181
111182
111183
111184
111185
111186
111187
111188
111189
111190
111191
111192
111193
111194
111195
111196
111197
111198
111199
111200
111201
111202
111203
111204
111205
111206
111207
111208
111209
111210
111211
111212
111213
111214
111215
111216
111217
111218
111219
111220
111221
111222
111223
111224
111225
111226
111227
111228
111229
111230
111231
111232
111233
111234
111235
111236
111237
111238
111239
111240
111241
111242
111243
111244
111245
111246
111247
111248
111249
111250
111251
111252
111253
111254
111255
111256
111257
111258
111259
111260
111261
111262
111263
111264
111265
111266
111267
111268
111269
111270
111271
111272
111273
111274
111275
111276
111277
111278
111279
111280
111281
111282
111283
111284
111285
111286
111287
111288
111289
111290
111291
111292
111293
111294
111295
111296
111297
111298
111299
111300
111301
111302
111303
111304
111305
111306
111307
111308
111309
111310
111311
111312
111313
111314
111315
111316
111317
111318
111319
111320
111321
111322
111323
111324
111325
111326
111327
111328
111329
111330
111331
111332
111333
111334
111335
111336
111337
111338
111339
111340
111341
111342
111343
111344
111345
111346
111347
111348
111349
111350
111351
111352
111353
111354
111355
111356
111357
111358
111359
111360
111361
111362
111363
111364
111365
111366
111367
111368
111369
111370
111371
111372
111373
111374
111375
111376
111377
111378
111379
111380
111381
111382
111383
111384
111385
111386
111387
111388
111389
111390
111391
111392
111393
111394
111395
111396
111397
111398
111399
111400
111401
111402
111403
111404
111405
111406
111407
111408
111409
111410
111411
111412
111413
111414
111415
111416
111417
111418
111419
111420
111421
111422
111423
111424
111425
111426
111427
111428
111429
111430
111431
111432
111433
111434
111435
111436
111437
111438
111439
111440
111441
111442
111443
111444
111445
111446
111447
111448
111449
111450
111451
111452
111453
111454
111455
111456
111457
111458
111459
111460
111461
111462
111463
111464
111465
111466
111467
111468
111469
111470
111471
111472
111473
111474
111475
111476
111477
111478
111479
111480
111481
111482
111483
111484
111485
111486
111487
111488
111489
111490
111491
111492
111493
111494
111495
111496
111497
111498
111499
111500
111501
111502
111503
111504
111505
111506
111507
111508
111509
111510
111511
111512
111513
111514
111515
111516
111517
111518
111519
111520
111521
111522
111523
111524
111525
111526
111527
111528
111529
111530
111531
111532
111533
111534
111535
111536
111537
111538
111539
111540
111541
111542
111543
111544
111545
111546
111547
111548
111549
111550
111551
111552
111553
111554
111555
111556
111557
111558
111559
111560
111561
111562
111563
111564
111565
111566
111567
111568
111569
111570
111571
111572
111573
111574
111575
111576
111577
111578
111579
111580
111581
111582
111583
111584
111585
111586
111587
111588
111589
111590
111591
111592
111593
111594
111595
111596
111597
111598
111599
111600
111601
111602
111603
111604
111605
111606
111607
111608
111609
111610
111611
111612
111613
111614
111615
111616
111617
111618
111619
111620
111621
111622
111623
111624
111625
111626
111627
111628
111629
111630
111631
111632
111633
111634
111635
111636
111637
111638
111639
111640
111641
111642
111643
111644
111645
111646
111647
111648
111649
111650
111651
111652
111653
111654
111655
111656
111657
111658
111659
111660
111661
111662
111663
111664
111665
111666
111667
111668
111669
111670
111671
111672
111673
111674
111675
111676
111677
111678
111679
111680
111681
111682
111683
111684
111685
111686
111687
111688
111689
111690
111691
111692
111693
111694
111695
111696
111697
111698
111699
111700
111701
111702
111703
111704
111705
111706
111707
111708
111709
111710
111711
111712
111713
111714
111715
111716
111717
111718
111719
111720
111721
111722
111723
111724
111725
111726
111727
111728
111729
111730
111731
111732
111733
111734
111735
111736
111737
111738
111739
111740
111741
111742
111743
111744
111745
111746
111747
111748
111749
111750
111751
111752
111753
111754
111755
111756
111757
111758
111759
111760
111761
111762
111763
111764
111765
111766
111767
111768
111769
111770
111771
111772
111773
111774
111775
111776
111777
111778
111779
111780
111781
111782
111783
111784
111785
111786
111787
111788
111789
111790
111791
111792
111793
111794
111795
111796
111797
111798
111799
111800
111801
111802
111803
111804
111805
111806
111807
111808
111809
111810
111811
111812
111813
111814
111815
111816
111817
111818
111819
111820
111821
111822
111823
111824
111825
111826
111827
111828
111829
111830
111831
111832
111833
111834
111835
111836
111837
111838
111839
111840
111841
111842
111843
111844
111845
111846
111847
111848
111849
111850
111851
111852
111853
111854
111855
111856
111857
111858
111859
111860
111861
111862
111863
111864
111865
111866
111867
111868
111869
111870
111871
111872
111873
111874
111875
111876
111877
111878
111879
111880
111881
111882
111883
111884
111885
111886
111887
111888
111889
111890
111891
111892
111893
111894
111895
111896
111897
111898
111899
111900
111901
111902
111903
111904
111905
111906
111907
111908
111909
111910
111911
111912
111913
111914
111915
111916
111917
111918
111919
111920
111921
111922
111923
111924
111925
111926
111927
111928
111929
111930
111931
111932
111933
111934
111935
111936
111937
111938
111939
111940
111941
111942
111943
111944
111945
111946
111947
111948
111949
111950
111951
111952
111953
111954
111955
111956
111957
111958
111959
111960
111961
111962
111963
111964
111965
111966
111967
111968
111969
111970
111971
111972
111973
111974
111975
111976
111977
111978
111979
111980
111981
111982
111983
111984
111985
111986
111987
111988
111989
111990
111991
111992
111993
111994
111995
111996
111997
111998
111999
112000
112001
112002
112003
112004
112005
112006
112007
112008
112009
112010
112011
112012
112013
112014
112015
112016
112017
112018
112019
112020
112021
112022
112023
112024
112025
112026
112027
112028
112029
112030
112031
112032
112033
112034
112035
112036
112037
112038
112039
112040
112041
112042
112043
112044
112045
112046
112047
112048
112049
112050
112051
112052
112053
112054
112055
112056
112057
112058
112059
112060
112061
112062
112063
112064
112065
112066
112067
112068
112069
112070
112071
112072
112073
112074
112075
112076
112077
112078
112079
112080
112081
112082
112083
112084
112085
112086
112087
112088
112089
112090
112091
112092
112093
112094
112095
112096
112097
112098
112099
112100
112101
112102
112103
112104
112105
112106
112107
112108
112109
112110
112111
112112
112113
112114
112115
112116
112117
112118
112119
112120
112121
112122
112123
112124
112125
112126
112127
112128
112129
112130
112131
112132
112133
112134
112135
112136
112137
112138
112139
112140
112141
112142
112143
112144
112145
112146
112147
112148
112149
112150
112151
112152
112153
112154
112155
112156
112157
112158
112159
112160
112161
112162
112163
112164
112165
112166
112167
112168
112169
112170
112171
112172
112173
112174
112175
112176
112177
112178
112179
112180
112181
112182
112183
112184
112185
112186
112187
112188
112189
112190
112191
112192
112193
112194
112195
112196
112197
112198
112199
112200
112201
112202
112203
112204
112205
112206
112207
112208
112209
112210
112211
112212
112213
112214
112215
112216
112217
112218
112219
112220
112221
112222
112223
112224
112225
112226
112227
112228
112229
112230
112231
112232
112233
112234
112235
112236
112237
112238
112239
112240
112241
112242
112243
112244
112245
112246
112247
112248
112249
112250
112251
112252
112253
112254
112255
112256
112257
112258
112259
112260
112261
112262
112263
112264
112265
112266
112267
112268
112269
112270
112271
112272
112273
112274
112275
112276
112277
112278
112279
112280
112281
112282
112283
112284
112285
112286
112287
112288
112289
112290
112291
112292
112293
112294
112295
112296
112297
112298
112299
112300
112301
112302
112303
112304
112305
112306
112307
112308
112309
112310
112311
112312
112313
112314
112315
112316
112317
112318
112319
112320
112321
112322
112323
112324
112325
112326
112327
112328
112329
112330
112331
112332
112333
112334
112335
112336
112337
112338
112339
112340
112341
112342
112343
112344
112345
112346
112347
112348
112349
112350
112351
112352
112353
112354
112355
112356
112357
112358
112359
112360
112361
112362
112363
112364
112365
112366
112367
112368
112369
112370
112371
112372
112373
112374
112375
112376
112377
112378
112379
112380
112381
112382
112383
112384
112385
112386
112387
112388
112389
112390
112391
112392
112393
112394
112395
112396
112397
112398
112399
112400
112401
112402
112403
112404
112405
112406
112407
112408
112409
112410
112411
112412
112413
112414
112415
112416
112417
112418
112419
112420
112421
112422
112423
112424
112425
112426
112427
112428
112429
112430
112431
112432
112433
112434
112435
112436
112437
112438
112439
112440
112441
112442
112443
112444
112445
112446
112447
112448
112449
112450
112451
112452
112453
112454
112455
112456
112457
112458
112459
112460
112461
112462
112463
112464
112465
112466
112467
112468
112469
112470
112471
112472
112473
112474
112475
112476
112477
112478
112479
112480
112481
112482
112483
112484
112485
112486
112487
112488
112489
112490
112491
112492
112493
112494
112495
112496
112497
112498
112499
112500
112501
112502
112503
112504
112505
112506
112507
112508
112509
112510
112511
112512
112513
112514
112515
112516
112517
112518
112519
112520
112521
112522
112523
112524
112525
112526
112527
112528
112529
112530
112531
112532
112533
112534
112535
112536
112537
112538
112539
112540
112541
112542
112543
112544
112545
112546
112547
112548
112549
112550
112551
112552
112553
112554
112555
112556
112557
112558
112559
112560
112561
112562
112563
112564
112565
112566
112567
112568
112569
112570
112571
112572
112573
112574
112575
112576
112577
112578
112579
112580
112581
112582
112583
112584
112585
112586
112587
112588
112589
112590
112591
112592
112593
112594
112595
112596
112597
112598
112599
112600
112601
112602
112603
112604
112605
112606
112607
112608
112609
112610
112611
112612
112613
112614
112615
112616
112617
112618
112619
112620
112621
112622
112623
112624
112625
112626
112627
112628
112629
112630
112631
112632
112633
112634
112635
112636
112637
112638
112639
112640
112641
112642
112643
112644
112645
112646
112647
112648
112649
112650
112651
112652
112653
112654
112655
112656
112657
112658
112659
112660
112661
112662
112663
112664
112665
112666
112667
112668
112669
112670
112671
112672
112673
112674
112675
112676
112677
112678
112679
112680
112681
112682
112683
112684
112685
112686
112687
112688
112689
112690
112691
112692
112693
112694
112695
112696
112697
112698
112699
112700
112701
112702
112703
112704
112705
112706
112707
112708
112709
112710
112711
112712
112713
112714
112715
112716
112717
112718
112719
112720
112721
112722
112723
112724
112725
112726
112727
112728
112729
112730
112731
112732
112733
112734
112735
112736
112737
112738
112739
112740
112741
112742
112743
112744
112745
112746
112747
112748
112749
112750
112751
112752
112753
112754
112755
112756
112757
112758
112759
112760
112761
112762
112763
112764
112765
112766
112767
112768
112769
112770
112771
112772
112773
112774
112775
112776
112777
112778
112779
112780
112781
112782
112783
112784
112785
112786
112787
112788
112789
112790
112791
112792
112793
112794
112795
112796
112797
112798
112799
112800
112801
112802
112803
112804
112805
112806
112807
112808
112809
112810
112811
112812
112813
112814
112815
112816
112817
112818
112819
112820
112821
112822
112823
112824
112825
112826
112827
112828
112829
112830
112831
112832
112833
112834
112835
112836
112837
112838
112839
112840
112841
112842
112843
112844
112845
112846
112847
112848
112849
112850
112851
112852
112853
112854
112855
112856
112857
112858
112859
112860
112861
112862
112863
112864
112865
112866
112867
112868
112869
112870
112871
112872
112873
112874
112875
112876
112877
112878
112879
112880
112881
112882
112883
112884
112885
112886
112887
112888
112889
112890
112891
112892
112893
112894
112895
112896
112897
112898
112899
112900
112901
112902
112903
112904
112905
112906
112907
112908
112909
112910
112911
112912
112913
112914
112915
112916
112917
112918
112919
112920
112921
112922
112923
112924
112925
112926
112927
112928
112929
112930
112931
112932
112933
112934
112935
112936
112937
112938
112939
112940
112941
112942
112943
112944
112945
112946
112947
112948
112949
112950
112951
112952
112953
112954
112955
112956
112957
112958
112959
112960
112961
112962
112963
112964
112965
112966
112967
112968
112969
112970
112971
112972
112973
112974
112975
112976
112977
112978
112979
112980
112981
112982
112983
112984
112985
112986
112987
112988
112989
112990
112991
112992
112993
112994
112995
112996
112997
112998
112999
113000
113001
113002
113003
113004
113005
113006
113007
113008
113009
113010
113011
113012
113013
113014
113015
113016
113017
113018
113019
113020
113021
113022
113023
113024
113025
113026
113027
113028
113029
113030
113031
113032
113033
113034
113035
113036
113037
113038
113039
113040
113041
113042
113043
113044
113045
113046
113047
113048
113049
113050
113051
113052
113053
113054
113055
113056
113057
113058
113059
113060
113061
113062
113063
113064
113065
113066
113067
113068
113069
113070
113071
113072
113073
113074
113075
113076
113077
113078
113079
113080
113081
113082
113083
113084
113085
113086
113087
113088
113089
113090
113091
113092
113093
113094
113095
113096
113097
113098
113099
113100
113101
113102
113103
113104
113105
113106
113107
113108
113109
113110
113111
113112
113113
113114
113115
113116
113117
113118
113119
113120
113121
113122
113123
113124
113125
113126
113127
113128
113129
113130
113131
113132
113133
113134
113135
113136
113137
113138
113139
113140
113141
113142
113143
113144
113145
113146
113147
113148
113149
113150
113151
113152
113153
113154
113155
113156
113157
113158
113159
113160
113161
113162
113163
113164
113165
113166
113167
113168
113169
113170
113171
113172
113173
113174
113175
113176
113177
113178
113179
113180
113181
113182
113183
113184
113185
113186
113187
113188
113189
113190
113191
113192
113193
113194
113195
113196
113197
113198
113199
113200
113201
113202
113203
113204
113205
113206
113207
113208
113209
113210
113211
113212
113213
113214
113215
113216
113217
113218
113219
113220
113221
113222
113223
113224
113225
113226
113227
113228
113229
113230
113231
113232
113233
113234
113235
113236
113237
113238
113239
113240
113241
113242
113243
113244
113245
113246
113247
113248
113249
113250
113251
113252
113253
113254
113255
113256
113257
113258
113259
113260
113261
113262
113263
113264
113265
113266
113267
113268
113269
113270
113271
113272
113273
113274
113275
113276
113277
113278
113279
113280
113281
113282
113283
113284
113285
113286
113287
113288
113289
113290
113291
113292
113293
113294
113295
113296
113297
113298
113299
113300
113301
113302
113303
113304
113305
113306
113307
113308
113309
113310
113311
113312
113313
113314
113315
113316
113317
113318
113319
113320
113321
113322
113323
113324
113325
113326
113327
113328
113329
113330
113331
113332
113333
113334
113335
113336
113337
113338
113339
113340
113341
113342
113343
113344
113345
113346
113347
113348
113349
113350
113351
113352
113353
113354
113355
113356
113357
113358
113359
113360
113361
113362
113363
113364
113365
113366
113367
113368
113369
113370
113371
113372
113373
113374
113375
113376
113377
113378
113379
113380
113381
113382
113383
113384
113385
113386
113387
113388
113389
113390
113391
113392
113393
113394
113395
113396
113397
113398
113399
113400
113401
113402
113403
113404
113405
113406
113407
113408
113409
113410
113411
113412
113413
113414
113415
113416
113417
113418
113419
113420
113421
113422
113423
113424
113425
113426
113427
113428
113429
113430
113431
113432
113433
113434
113435
113436
113437
113438
113439
113440
113441
113442
113443
113444
113445
113446
113447
113448
113449
113450
113451
113452
113453
113454
113455
113456
113457
113458
113459
113460
113461
113462
113463
113464
113465
113466
113467
113468
113469
113470
113471
113472
113473
113474
113475
113476
113477
113478
113479
113480
113481
113482
113483
113484
113485
113486
113487
113488
113489
113490
113491
113492
113493
113494
113495
113496
113497
113498
113499
113500
113501
113502
113503
113504
113505
113506
113507
113508
113509
113510
113511
113512
113513
113514
113515
113516
113517
113518
113519
113520
113521
113522
113523
113524
113525
113526
113527
113528
113529
113530
113531
113532
113533
113534
113535
113536
113537
113538
113539
113540
113541
113542
113543
113544
113545
113546
113547
113548
113549
113550
113551
113552
113553
113554
113555
113556
113557
113558
113559
113560
113561
113562
113563
113564
113565
113566
113567
113568
113569
113570
113571
113572
113573
113574
113575
113576
113577
113578
113579
113580
113581
113582
113583
113584
113585
113586
113587
113588
113589
113590
113591
113592
113593
113594
113595
113596
113597
113598
113599
113600
113601
113602
113603
113604
113605
113606
113607
113608
113609
113610
113611
113612
113613
113614
113615
113616
113617
113618
113619
113620
113621
113622
113623
113624
113625
113626
113627
113628
113629
113630
113631
113632
113633
113634
113635
113636
113637
113638
113639
113640
113641
113642
113643
113644
113645
113646
113647
113648
113649
113650
113651
113652
113653
113654
113655
113656
113657
113658
113659
113660
113661
113662
113663
113664
113665
113666
113667
113668
113669
113670
113671
113672
113673
113674
113675
113676
113677
113678
113679
113680
113681
113682
113683
113684
113685
113686
113687
113688
113689
113690
113691
113692
113693
113694
113695
113696
113697
113698
113699
113700
113701
113702
113703
113704
113705
113706
113707
113708
113709
113710
113711
113712
113713
113714
113715
113716
113717
113718
113719
113720
113721
113722
113723
113724
113725
113726
113727
113728
113729
113730
113731
113732
113733
113734
113735
113736
113737
113738
113739
113740
113741
113742
113743
113744
113745
113746
113747
113748
113749
113750
113751
113752
113753
113754
113755
113756
113757
113758
113759
113760
113761
113762
113763
113764
113765
113766
113767
113768
113769
113770
113771
113772
113773
113774
113775
113776
113777
113778
113779
113780
113781
113782
113783
113784
113785
113786
113787
113788
113789
113790
113791
113792
113793
113794
113795
113796
113797
113798
113799
113800
113801
113802
113803
113804
113805
113806
113807
113808
113809
113810
113811
113812
113813
113814
113815
113816
113817
113818
113819
113820
113821
113822
113823
113824
113825
113826
113827
113828
113829
113830
113831
113832
113833
113834
113835
113836
113837
113838
113839
113840
113841
113842
113843
113844
113845
113846
113847
113848
113849
113850
113851
113852
113853
113854
113855
113856
113857
113858
113859
113860
113861
113862
113863
113864
113865
113866
113867
113868
113869
113870
113871
113872
113873
113874
113875
113876
113877
113878
113879
113880
113881
113882
113883
113884
113885
113886
113887
113888
113889
113890
113891
113892
113893
113894
113895
113896
113897
113898
113899
113900
113901
113902
113903
113904
113905
113906
113907
113908
113909
113910
113911
113912
113913
113914
113915
113916
113917
113918
113919
113920
113921
113922
113923
113924
113925
113926
113927
113928
113929
113930
113931
113932
113933
113934
113935
113936
113937
113938
113939
113940
113941
113942
113943
113944
113945
113946
113947
113948
113949
113950
113951
113952
113953
113954
113955
113956
113957
113958
113959
113960
113961
113962
113963
113964
113965
113966
113967
113968
113969
113970
113971
113972
113973
113974
113975
113976
113977
113978
113979
113980
113981
113982
113983
113984
113985
113986
113987
113988
113989
113990
113991
113992
113993
113994
113995
113996
113997
113998
113999
114000
114001
114002
114003
114004
114005
114006
114007
114008
114009
114010
114011
114012
114013
114014
114015
114016
114017
114018
114019
114020
114021
114022
114023
114024
114025
114026
114027
114028
114029
114030
114031
114032
114033
114034
114035
114036
114037
114038
114039
114040
114041
114042
114043
114044
114045
114046
114047
114048
114049
114050
114051
114052
114053
114054
114055
114056
114057
114058
114059
114060
114061
114062
114063
114064
114065
114066
114067
114068
114069
114070
114071
114072
114073
114074
114075
114076
114077
114078
114079
114080
114081
114082
114083
114084
114085
114086
114087
114088
114089
114090
114091
114092
114093
114094
114095
114096
114097
114098
114099
114100
114101
114102
114103
114104
114105
114106
114107
114108
114109
114110
114111
114112
114113
114114
114115
114116
114117
114118
114119
114120
114121
114122
114123
114124
114125
114126
114127
114128
114129
114130
114131
114132
114133
114134
114135
114136
114137
114138
114139
114140
114141
114142
114143
114144
114145
114146
114147
114148
114149
114150
114151
114152
114153
114154
114155
114156
114157
114158
114159
114160
114161
114162
114163
114164
114165
114166
114167
114168
114169
114170
114171
114172
114173
114174
114175
114176
114177
114178
114179
114180
114181
114182
114183
114184
114185
114186
114187
114188
114189
114190
114191
114192
114193
114194
114195
114196
114197
114198
114199
114200
114201
114202
114203
114204
114205
114206
114207
114208
114209
114210
114211
114212
114213
114214
114215
114216
114217
114218
114219
114220
114221
114222
114223
114224
114225
114226
114227
114228
114229
114230
114231
114232
114233
114234
114235
114236
114237
114238
114239
114240
114241
114242
114243
114244
114245
114246
114247
114248
114249
114250
114251
114252
114253
114254
114255
114256
114257
114258
114259
114260
114261
114262
114263
114264
114265
114266
114267
114268
114269
114270
114271
114272
114273
114274
114275
114276
114277
114278
114279
114280
114281
114282
114283
114284
114285
114286
114287
114288
114289
114290
114291
114292
114293
114294
114295
114296
114297
114298
114299
114300
114301
114302
114303
114304
114305
114306
114307
114308
114309
114310
114311
114312
114313
114314
114315
114316
114317
114318
114319
114320
114321
114322
114323
114324
114325
114326
114327
114328
114329
114330
114331
114332
114333
114334
114335
114336
114337
114338
114339
114340
114341
114342
114343
114344
114345
114346
114347
114348
114349
114350
114351
114352
114353
114354
114355
114356
114357
114358
114359
114360
114361
114362
114363
114364
114365
114366
114367
114368
114369
114370
114371
114372
114373
114374
114375
114376
114377
114378
114379
114380
114381
114382
114383
114384
114385
114386
114387
114388
114389
114390
114391
114392
114393
114394
114395
114396
114397
114398
114399
114400
114401
114402
114403
114404
114405
114406
114407
114408
114409
114410
114411
114412
114413
114414
114415
114416
114417
114418
114419
114420
114421
114422
114423
114424
114425
114426
114427
114428
114429
114430
114431
114432
114433
114434
114435
114436
114437
114438
114439
114440
114441
114442
114443
114444
114445
114446
114447
114448
114449
114450
114451
114452
114453
114454
114455
114456
114457
114458
114459
114460
114461
114462
114463
114464
114465
114466
114467
114468
114469
114470
114471
114472
114473
114474
114475
114476
114477
114478
114479
114480
114481
114482
114483
114484
114485
114486
114487
114488
114489
114490
114491
114492
114493
114494
114495
114496
114497
114498
114499
114500
114501
114502
114503
114504
114505
114506
114507
114508
114509
114510
114511
114512
114513
114514
114515
114516
114517
114518
114519
114520
114521
114522
114523
114524
114525
114526
114527
114528
114529
114530
114531
114532
114533
114534
114535
114536
114537
114538
114539
114540
114541
114542
114543
114544
114545
114546
114547
114548
114549
114550
114551
114552
114553
114554
114555
114556
114557
114558
114559
114560
114561
114562
114563
114564
114565
114566
114567
114568
114569
114570
114571
114572
114573
114574
114575
114576
114577
114578
114579
114580
114581
114582
114583
114584
114585
114586
114587
114588
114589
114590
114591
114592
114593
114594
114595
114596
114597
114598
114599
114600
114601
114602
114603
114604
114605
114606
114607
114608
114609
114610
114611
114612
114613
114614
114615
114616
114617
114618
114619
114620
114621
114622
114623
114624
114625
114626
114627
114628
114629
114630
114631
114632
114633
114634
114635
114636
114637
114638
114639
114640
114641
114642
114643
114644
114645
114646
114647
114648
114649
114650
114651
114652
114653
114654
114655
114656
114657
114658
114659
114660
114661
114662
114663
114664
114665
114666
114667
114668
114669
114670
114671
114672
114673
114674
114675
114676
114677
114678
114679
114680
114681
114682
114683
114684
114685
114686
114687
114688
114689
114690
114691
114692
114693
114694
114695
114696
114697
114698
114699
114700
114701
114702
114703
114704
114705
114706
114707
114708
114709
114710
114711
114712
114713
114714
114715
114716
114717
114718
114719
114720
114721
114722
114723
114724
114725
114726
114727
114728
114729
114730
114731
114732
114733
114734
114735
114736
114737
114738
114739
114740
114741
114742
114743
114744
114745
114746
114747
114748
114749
114750
114751
114752
114753
114754
114755
114756
114757
114758
114759
114760
114761
114762
114763
114764
114765
114766
114767
114768
114769
114770
114771
114772
114773
114774
114775
114776
114777
114778
114779
114780
114781
114782
114783
114784
114785
114786
114787
114788
114789
114790
114791
114792
114793
114794
114795
114796
114797
114798
114799
114800
114801
114802
114803
114804
114805
114806
114807
114808
114809
114810
114811
114812
114813
114814
114815
114816
114817
114818
114819
114820
114821
114822
114823
114824
114825
114826
114827
114828
114829
114830
114831
114832
114833
114834
114835
114836
114837
114838
114839
114840
114841
114842
114843
114844
114845
114846
114847
114848
114849
114850
114851
114852
114853
114854
114855
114856
114857
114858
114859
114860
114861
114862
114863
114864
114865
114866
114867
114868
114869
114870
114871
114872
114873
114874
114875
114876
114877
114878
114879
114880
114881
114882
114883
114884
114885
114886
114887
114888
114889
114890
114891
114892
114893
114894
114895
114896
114897
114898
114899
114900
114901
114902
114903
114904
114905
114906
114907
114908
114909
114910
114911
114912
114913
114914
114915
114916
114917
114918
114919
114920
114921
114922
114923
114924
114925
114926
114927
114928
114929
114930
114931
114932
114933
114934
114935
114936
114937
114938
114939
114940
114941
114942
114943
114944
114945
114946
114947
114948
114949
114950
114951
114952
114953
114954
114955
114956
114957
114958
114959
114960
114961
114962
114963
114964
114965
114966
114967
114968
114969
114970
114971
114972
114973
114974
114975
114976
114977
114978
114979
114980
114981
114982
114983
114984
114985
114986
114987
114988
114989
114990
114991
114992
114993
114994
114995
114996
114997
114998
114999
115000
115001
115002
115003
115004
115005
115006
115007
115008
115009
115010
115011
115012
115013
115014
115015
115016
115017
115018
115019
115020
115021
115022
115023
115024
115025
115026
115027
115028
115029
115030
115031
115032
115033
115034
115035
115036
115037
115038
115039
115040
115041
115042
115043
115044
115045
115046
115047
115048
115049
115050
115051
115052
115053
115054
115055
115056
115057
115058
115059
115060
115061
115062
115063
115064
115065
115066
115067
115068
115069
115070
115071
115072
115073
115074
115075
115076
115077
115078
115079
115080
115081
115082
115083
115084
115085
115086
115087
115088
115089
115090
115091
115092
115093
115094
115095
115096
115097
115098
115099
115100
115101
115102
115103
115104
115105
115106
115107
115108
115109
115110
115111
115112
115113
115114
115115
115116
115117
115118
115119
115120
115121
115122
115123
115124
115125
115126
115127
115128
115129
115130
115131
115132
115133
115134
115135
115136
115137
115138
115139
115140
115141
115142
115143
115144
115145
115146
115147
115148
115149
115150
115151
115152
115153
115154
115155
115156
115157
115158
115159
115160
115161
115162
115163
115164
115165
115166
115167
115168
115169
115170
115171
115172
115173
115174
115175
115176
115177
115178
115179
115180
115181
115182
115183
115184
115185
115186
115187
115188
115189
115190
115191
115192
115193
115194
115195
115196
115197
115198
115199
115200
115201
115202
115203
115204
115205
115206
115207
115208
115209
115210
115211
115212
115213
115214
115215
115216
115217
115218
115219
115220
115221
115222
115223
115224
115225
115226
115227
115228
115229
115230
115231
115232
115233
115234
115235
115236
115237
115238
115239
115240
115241
115242
115243
115244
115245
115246
115247
115248
115249
115250
115251
115252
115253
115254
115255
115256
115257
115258
115259
115260
115261
115262
115263
115264
115265
115266
115267
115268
115269
115270
115271
115272
115273
115274
115275
115276
115277
115278
115279
115280
115281
115282
115283
115284
115285
115286
115287
115288
115289
115290
115291
115292
115293
115294
115295
115296
115297
115298
115299
115300
115301
115302
115303
115304
115305
115306
115307
115308
115309
115310
115311
115312
115313
115314
115315
115316
115317
115318
115319
115320
115321
115322
115323
115324
115325
115326
115327
115328
115329
115330
115331
115332
115333
115334
115335
115336
115337
115338
115339
115340
115341
115342
115343
115344
115345
115346
115347
115348
115349
115350
115351
115352
115353
115354
115355
115356
115357
115358
115359
115360
115361
115362
115363
115364
115365
115366
115367
115368
115369
115370
115371
115372
115373
115374
115375
115376
115377
115378
115379
115380
115381
115382
115383
115384
115385
115386
115387
115388
115389
115390
115391
115392
115393
115394
115395
115396
115397
115398
115399
115400
115401
115402
115403
115404
115405
115406
115407
115408
115409
115410
115411
115412
115413
115414
115415
115416
115417
115418
115419
115420
115421
115422
115423
115424
115425
115426
115427
115428
115429
115430
115431
115432
115433
115434
115435
115436
115437
115438
115439
115440
115441
115442
115443
115444
115445
115446
115447
115448
115449
115450
115451
115452
115453
115454
115455
115456
115457
115458
115459
115460
115461
115462
115463
115464
115465
115466
115467
115468
115469
115470
115471
115472
115473
115474
115475
115476
115477
115478
115479
115480
115481
115482
115483
115484
115485
115486
115487
115488
115489
115490
115491
115492
115493
115494
115495
115496
115497
115498
115499
115500
115501
115502
115503
115504
115505
115506
115507
115508
115509
115510
115511
115512
115513
115514
115515
115516
115517
115518
115519
115520
115521
115522
115523
115524
115525
115526
115527
115528
115529
115530
115531
115532
115533
115534
115535
115536
115537
115538
115539
115540
115541
115542
115543
115544
115545
115546
115547
115548
115549
115550
115551
115552
115553
115554
115555
115556
115557
115558
115559
115560
115561
115562
115563
115564
115565
115566
115567
115568
115569
115570
115571
115572
115573
115574
115575
115576
115577
115578
115579
115580
115581
115582
115583
115584
115585
115586
115587
115588
115589
115590
115591
115592
115593
115594
115595
115596
115597
115598
115599
115600
115601
115602
115603
115604
115605
115606
115607
115608
115609
115610
115611
115612
115613
115614
115615
115616
115617
115618
115619
115620
115621
115622
115623
115624
115625
115626
115627
115628
115629
115630
115631
115632
115633
115634
115635
115636
115637
115638
115639
115640
115641
115642
115643
115644
115645
115646
115647
115648
115649
115650
115651
115652
115653
115654
115655
115656
115657
115658
115659
115660
115661
115662
115663
115664
115665
115666
115667
115668
115669
115670
115671
115672
115673
115674
115675
115676
115677
115678
115679
115680
115681
115682
115683
115684
115685
115686
115687
115688
115689
115690
115691
115692
115693
115694
115695
115696
115697
115698
115699
115700
115701
115702
115703
115704
115705
115706
115707
115708
115709
115710
115711
115712
115713
115714
115715
115716
115717
115718
115719
115720
115721
115722
115723
115724
115725
115726
115727
115728
115729
115730
115731
115732
115733
115734
115735
115736
115737
115738
115739
115740
115741
115742
115743
115744
115745
115746
115747
115748
115749
115750
115751
115752
115753
115754
115755
115756
115757
115758
115759
115760
115761
115762
115763
115764
115765
115766
115767
115768
115769
115770
115771
115772
115773
115774
115775
115776
115777
115778
115779
115780
115781
115782
115783
115784
115785
115786
115787
115788
115789
115790
115791
115792
115793
115794
115795
115796
115797
115798
115799
115800
115801
115802
115803
115804
115805
115806
115807
115808
115809
115810
115811
115812
115813
115814
115815
115816
115817
115818
115819
115820
115821
115822
115823
115824
115825
115826
115827
115828
115829
115830
115831
115832
115833
115834
115835
115836
115837
115838
115839
115840
115841
115842
115843
115844
115845
115846
115847
115848
115849
115850
115851
115852
115853
115854
115855
115856
115857
115858
115859
115860
115861
115862
115863
115864
115865
115866
115867
115868
115869
115870
115871
115872
115873
115874
115875
115876
115877
115878
115879
115880
115881
115882
115883
115884
115885
115886
115887
115888
115889
115890
115891
115892
115893
115894
115895
115896
115897
115898
115899
115900
115901
115902
115903
115904
115905
115906
115907
115908
115909
115910
115911
115912
115913
115914
115915
115916
115917
115918
115919
115920
115921
115922
115923
115924
115925
115926
115927
115928
115929
115930
115931
115932
115933
115934
115935
115936
115937
115938
115939
115940
115941
115942
115943
115944
115945
115946
115947
115948
115949
115950
115951
115952
115953
115954
115955
115956
115957
115958
115959
115960
115961
115962
115963
115964
115965
115966
115967
115968
115969
115970
115971
115972
115973
115974
115975
115976
115977
115978
115979
115980
115981
115982
115983
115984
115985
115986
115987
115988
115989
115990
115991
115992
115993
115994
115995
115996
115997
115998
115999
116000
116001
116002
116003
116004
116005
116006
116007
116008
116009
116010
116011
116012
116013
116014
116015
116016
116017
116018
116019
116020
116021
116022
116023
116024
116025
116026
116027
116028
116029
116030
116031
116032
116033
116034
116035
116036
116037
116038
116039
116040
116041
116042
116043
116044
116045
116046
116047
116048
116049
116050
116051
116052
116053
116054
116055
116056
116057
116058
116059
116060
116061
116062
116063
116064
116065
116066
116067
116068
116069
116070
116071
116072
116073
116074
116075
116076
116077
116078
116079
116080
116081
116082
116083
116084
116085
116086
116087
116088
116089
116090
116091
116092
116093
116094
116095
116096
116097
116098
116099
116100
116101
116102
116103
116104
116105
116106
116107
116108
116109
116110
116111
116112
116113
116114
116115
116116
116117
116118
116119
116120
116121
116122
116123
116124
116125
116126
116127
116128
116129
116130
116131
116132
116133
116134
116135
116136
116137
116138
116139
116140
116141
116142
116143
116144
116145
116146
116147
116148
116149
116150
116151
116152
116153
116154
116155
116156
116157
116158
116159
116160
116161
116162
116163
116164
116165
116166
116167
116168
116169
116170
116171
116172
116173
116174
116175
116176
116177
116178
116179
116180
116181
116182
116183
116184
116185
116186
116187
116188
116189
116190
116191
116192
116193
116194
116195
116196
116197
116198
116199
116200
116201
116202
116203
116204
116205
116206
116207
116208
116209
116210
116211
116212
116213
116214
116215
116216
116217
116218
116219
116220
116221
116222
116223
116224
116225
116226
116227
116228
116229
116230
116231
116232
116233
116234
116235
116236
116237
116238
116239
116240
116241
116242
116243
116244
116245
116246
116247
116248
116249
116250
116251
116252
116253
116254
116255
116256
116257
116258
116259
116260
116261
116262
116263
116264
116265
116266
116267
116268
116269
116270
116271
116272
116273
116274
116275
116276
116277
116278
116279
116280
116281
116282
116283
116284
116285
116286
116287
116288
116289
116290
116291
116292
116293
116294
116295
116296
116297
116298
116299
116300
116301
116302
116303
116304
116305
116306
116307
116308
116309
116310
116311
116312
116313
116314
116315
116316
116317
116318
116319
116320
116321
116322
116323
116324
116325
116326
116327
116328
116329
116330
116331
116332
116333
116334
116335
116336
116337
116338
116339
116340
116341
116342
116343
116344
116345
116346
116347
116348
116349
116350
116351
116352
116353
116354
116355
116356
116357
116358
116359
116360
116361
116362
116363
116364
116365
116366
116367
116368
116369
116370
116371
116372
116373
116374
116375
116376
116377
116378
116379
116380
116381
116382
116383
116384
116385
116386
116387
116388
116389
116390
116391
116392
116393
116394
116395
116396
116397
116398
116399
116400
116401
116402
116403
116404
116405
116406
116407
116408
116409
116410
116411
116412
116413
116414
116415
116416
116417
116418
116419
116420
116421
116422
116423
116424
116425
116426
116427
116428
116429
116430
116431
116432
116433
116434
116435
116436
116437
116438
116439
116440
116441
116442
116443
116444
116445
116446
116447
116448
116449
116450
116451
116452
116453
116454
116455
116456
116457
116458
116459
116460
116461
116462
116463
116464
116465
116466
116467
116468
116469
116470
116471
116472
116473
116474
116475
116476
116477
116478
116479
116480
116481
116482
116483
116484
116485
116486
116487
116488
116489
116490
116491
116492
116493
116494
116495
116496
116497
116498
116499
116500
116501
116502
116503
116504
116505
116506
116507
116508
116509
116510
116511
116512
116513
116514
116515
116516
116517
116518
116519
116520
116521
116522
116523
116524
116525
116526
116527
116528
116529
116530
116531
116532
116533
116534
116535
116536
116537
116538
116539
116540
116541
116542
116543
116544
116545
116546
116547
116548
116549
116550
116551
116552
116553
116554
116555
116556
116557
116558
116559
116560
116561
116562
116563
116564
116565
116566
116567
116568
116569
116570
116571
116572
116573
116574
116575
116576
116577
116578
116579
116580
116581
116582
116583
116584
116585
116586
116587
116588
116589
116590
116591
116592
116593
116594
116595
116596
116597
116598
116599
116600
116601
116602
116603
116604
116605
116606
116607
116608
116609
116610
116611
116612
116613
116614
116615
116616
116617
116618
116619
116620
116621
116622
116623
116624
116625
116626
116627
116628
116629
116630
116631
116632
116633
116634
116635
116636
116637
116638
116639
116640
116641
116642
116643
116644
116645
116646
116647
116648
116649
116650
116651
116652
116653
116654
116655
116656
116657
116658
116659
116660
116661
116662
116663
116664
116665
116666
116667
116668
116669
116670
116671
116672
116673
116674
116675
116676
116677
116678
116679
116680
116681
116682
116683
116684
116685
116686
116687
116688
116689
116690
116691
116692
116693
116694
116695
116696
116697
116698
116699
116700
116701
116702
116703
116704
116705
116706
116707
116708
116709
116710
116711
116712
116713
116714
116715
116716
116717
116718
116719
116720
116721
116722
116723
116724
116725
116726
116727
116728
116729
116730
116731
116732
116733
116734
116735
116736
116737
116738
116739
116740
116741
116742
116743
116744
116745
116746
116747
116748
116749
116750
116751
116752
116753
116754
116755
116756
116757
116758
116759
116760
116761
116762
116763
116764
116765
116766
116767
116768
116769
116770
116771
116772
116773
116774
116775
116776
116777
116778
116779
116780
116781
116782
116783
116784
116785
116786
116787
116788
116789
116790
116791
116792
116793
116794
116795
116796
116797
116798
116799
116800
116801
116802
116803
116804
116805
116806
116807
116808
116809
116810
116811
116812
116813
116814
116815
116816
116817
116818
116819
116820
116821
116822
116823
116824
116825
116826
116827
116828
116829
116830
116831
116832
116833
116834
116835
116836
116837
116838
116839
116840
116841
116842
116843
116844
116845
116846
116847
116848
116849
116850
116851
116852
116853
116854
116855
116856
116857
116858
116859
116860
116861
116862
116863
116864
116865
116866
116867
116868
116869
116870
116871
116872
116873
116874
116875
116876
116877
116878
116879
116880
116881
116882
116883
116884
116885
116886
116887
116888
116889
116890
116891
116892
116893
116894
116895
116896
116897
116898
116899
116900
116901
116902
116903
116904
116905
116906
116907
116908
116909
116910
116911
116912
116913
116914
116915
116916
116917
116918
116919
116920
116921
116922
116923
116924
116925
116926
116927
116928
116929
116930
116931
116932
116933
116934
116935
116936
116937
116938
116939
116940
116941
116942
116943
116944
116945
116946
116947
116948
116949
116950
116951
116952
116953
116954
116955
116956
116957
116958
116959
116960
116961
116962
116963
116964
116965
116966
116967
116968
116969
116970
116971
116972
116973
116974
116975
116976
116977
116978
116979
116980
116981
116982
116983
116984
116985
116986
116987
116988
116989
116990
116991
116992
116993
116994
116995
116996
116997
116998
116999
117000
117001
117002
117003
117004
117005
117006
117007
117008
117009
117010
117011
117012
117013
117014
117015
117016
117017
117018
117019
117020
117021
117022
117023
117024
117025
117026
117027
117028
117029
117030
117031
117032
117033
117034
117035
117036
117037
117038
117039
117040
117041
117042
117043
117044
117045
117046
117047
117048
117049
117050
117051
117052
117053
117054
117055
117056
117057
117058
117059
117060
117061
117062
117063
117064
117065
117066
117067
117068
117069
117070
117071
117072
117073
117074
117075
117076
117077
117078
117079
117080
117081
117082
117083
117084
117085
117086
117087
117088
117089
117090
117091
117092
117093
117094
117095
117096
117097
117098
117099
117100
117101
117102
117103
117104
117105
117106
117107
117108
117109
117110
117111
117112
117113
117114
117115
117116
117117
117118
117119
117120
117121
117122
117123
117124
117125
117126
117127
117128
117129
117130
117131
117132
117133
117134
117135
117136
117137
117138
117139
117140
117141
117142
117143
117144
117145
117146
117147
117148
117149
117150
117151
117152
117153
117154
117155
117156
117157
117158
117159
117160
117161
117162
117163
117164
117165
117166
117167
117168
117169
117170
117171
117172
117173
117174
117175
117176
117177
117178
117179
117180
117181
117182
117183
117184
117185
117186
117187
117188
117189
117190
117191
117192
117193
117194
117195
117196
117197
117198
117199
117200
117201
117202
117203
117204
117205
117206
117207
117208
117209
117210
117211
117212
117213
117214
117215
117216
117217
117218
117219
117220
117221
117222
117223
117224
117225
117226
117227
117228
117229
117230
117231
117232
117233
117234
117235
117236
117237
117238
117239
117240
117241
117242
117243
117244
117245
117246
117247
117248
117249
117250
117251
117252
117253
117254
117255
117256
117257
117258
117259
117260
117261
117262
117263
117264
117265
117266
117267
117268
117269
117270
117271
117272
117273
117274
117275
117276
117277
117278
117279
117280
117281
117282
117283
117284
117285
117286
117287
117288
117289
117290
117291
117292
117293
117294
117295
117296
117297
117298
117299
117300
117301
117302
117303
117304
117305
117306
117307
117308
117309
117310
117311
117312
117313
117314
117315
117316
117317
117318
117319
117320
117321
117322
117323
117324
117325
117326
117327
117328
117329
117330
117331
117332
117333
117334
117335
117336
117337
117338
117339
117340
117341
117342
117343
117344
117345
117346
117347
117348
117349
117350
117351
117352
117353
117354
117355
117356
117357
117358
117359
117360
117361
117362
117363
117364
117365
117366
117367
117368
117369
117370
117371
117372
117373
117374
117375
117376
117377
117378
117379
117380
117381
117382
117383
117384
117385
117386
117387
117388
117389
117390
117391
117392
117393
117394
117395
117396
117397
117398
117399
117400
117401
117402
117403
117404
117405
117406
117407
117408
117409
117410
117411
117412
117413
117414
117415
117416
117417
117418
117419
117420
117421
117422
117423
117424
117425
117426
117427
117428
117429
117430
117431
117432
117433
117434
117435
117436
117437
117438
117439
117440
117441
117442
117443
117444
117445
117446
117447
117448
117449
117450
117451
117452
117453
117454
117455
117456
117457
117458
117459
117460
117461
117462
117463
117464
117465
117466
117467
117468
117469
117470
117471
117472
117473
117474
117475
117476
117477
117478
117479
117480
117481
117482
117483
117484
117485
117486
117487
117488
117489
117490
117491
117492
117493
117494
117495
117496
117497
117498
117499
117500
117501
117502
117503
117504
117505
117506
117507
117508
117509
117510
117511
117512
117513
117514
117515
117516
117517
117518
117519
117520
117521
117522
117523
117524
117525
117526
117527
117528
117529
117530
117531
117532
117533
117534
117535
117536
117537
117538
117539
117540
117541
117542
117543
117544
117545
117546
117547
117548
117549
117550
117551
117552
117553
117554
117555
117556
117557
117558
117559
117560
117561
117562
117563
117564
117565
117566
117567
117568
117569
117570
117571
117572
117573
117574
117575
117576
117577
117578
117579
117580
117581
117582
117583
117584
117585
117586
117587
117588
117589
117590
117591
117592
117593
117594
117595
117596
117597
117598
117599
117600
117601
117602
117603
117604
117605
117606
117607
117608
117609
117610
117611
117612
117613
117614
117615
117616
117617
117618
117619
117620
117621
117622
117623
117624
117625
117626
117627
117628
117629
117630
117631
117632
117633
117634
117635
117636
117637
117638
117639
117640
117641
117642
117643
117644
117645
117646
117647
117648
117649
117650
117651
117652
117653
117654
117655
117656
117657
117658
117659
117660
117661
117662
117663
117664
117665
117666
117667
117668
117669
117670
117671
117672
117673
117674
117675
117676
117677
117678
117679
117680
117681
117682
117683
117684
117685
117686
117687
117688
117689
117690
117691
117692
117693
117694
117695
117696
117697
117698
117699
117700
117701
117702
117703
117704
117705
117706
117707
117708
117709
117710
117711
117712
117713
117714
117715
117716
117717
117718
117719
117720
117721
117722
117723
117724
117725
117726
117727
117728
117729
117730
117731
117732
117733
117734
117735
117736
117737
117738
117739
117740
117741
117742
117743
117744
117745
117746
117747
117748
117749
117750
117751
117752
117753
117754
117755
117756
117757
117758
117759
117760
117761
117762
117763
117764
117765
117766
117767
117768
117769
117770
117771
117772
117773
117774
117775
117776
117777
117778
117779
117780
117781
117782
117783
117784
117785
117786
117787
117788
117789
117790
117791
117792
117793
117794
117795
117796
117797
117798
117799
117800
117801
117802
117803
117804
117805
117806
117807
117808
117809
117810
117811
117812
117813
117814
117815
117816
117817
117818
117819
117820
117821
117822
117823
117824
117825
117826
117827
117828
117829
117830
117831
117832
117833
117834
117835
117836
117837
117838
117839
117840
117841
117842
117843
117844
117845
117846
117847
117848
117849
117850
117851
117852
117853
117854
117855
117856
117857
117858
117859
117860
117861
117862
117863
117864
117865
117866
117867
117868
117869
117870
117871
117872
117873
117874
117875
117876
117877
117878
117879
117880
117881
117882
117883
117884
117885
117886
117887
117888
117889
117890
117891
117892
117893
117894
117895
117896
117897
117898
117899
117900
117901
117902
117903
117904
117905
117906
117907
117908
117909
117910
117911
117912
117913
117914
117915
117916
117917
117918
117919
117920
117921
117922
117923
117924
117925
117926
117927
117928
117929
117930
117931
117932
117933
117934
117935
117936
117937
117938
117939
117940
117941
117942
117943
117944
117945
117946
117947
117948
117949
117950
117951
117952
117953
117954
117955
117956
117957
117958
117959
117960
117961
117962
117963
117964
117965
117966
117967
117968
117969
117970
117971
117972
117973
117974
117975
117976
117977
117978
117979
117980
117981
117982
117983
117984
117985
117986
117987
117988
117989
117990
117991
117992
117993
117994
117995
117996
117997
117998
117999
118000
118001
118002
118003
118004
118005
118006
118007
118008
118009
118010
118011
118012
118013
118014
118015
118016
118017
118018
118019
118020
118021
118022
118023
118024
118025
118026
118027
118028
118029
118030
118031
118032
118033
118034
118035
118036
118037
118038
118039
118040
118041
118042
118043
118044
118045
118046
118047
118048
118049
118050
118051
118052
118053
118054
118055
118056
118057
118058
118059
118060
118061
118062
118063
118064
118065
118066
118067
118068
118069
118070
118071
118072
118073
118074
118075
118076
118077
118078
118079
118080
118081
118082
118083
118084
118085
118086
118087
118088
118089
118090
118091
118092
118093
118094
118095
118096
118097
118098
118099
118100
118101
118102
118103
118104
118105
118106
118107
118108
118109
118110
118111
118112
118113
118114
118115
118116
118117
118118
118119
118120
118121
118122
118123
118124
118125
118126
118127
118128
118129
118130
118131
118132
118133
118134
118135
118136
118137
118138
118139
118140
118141
118142
118143
118144
118145
118146
118147
118148
118149
118150
118151
118152
118153
118154
118155
118156
118157
118158
118159
118160
118161
118162
118163
118164
118165
118166
118167
118168
118169
118170
118171
118172
118173
118174
118175
118176
118177
118178
118179
118180
118181
118182
118183
118184
118185
118186
118187
118188
118189
118190
118191
118192
118193
118194
118195
118196
118197
118198
118199
118200
118201
118202
118203
118204
118205
118206
118207
118208
118209
118210
118211
118212
118213
118214
118215
118216
118217
118218
118219
118220
118221
118222
118223
118224
118225
118226
118227
118228
118229
118230
118231
118232
118233
118234
118235
118236
118237
118238
118239
118240
118241
118242
118243
118244
118245
118246
118247
118248
118249
118250
118251
118252
118253
118254
118255
118256
118257
118258
118259
118260
118261
118262
118263
118264
118265
118266
118267
118268
118269
118270
118271
118272
118273
118274
118275
118276
118277
118278
118279
118280
118281
118282
118283
118284
118285
118286
118287
118288
118289
118290
118291
118292
118293
118294
118295
118296
118297
118298
118299
118300
118301
118302
118303
118304
118305
118306
118307
118308
118309
118310
118311
118312
118313
118314
118315
118316
118317
118318
118319
118320
118321
118322
118323
118324
118325
118326
118327
118328
118329
118330
118331
118332
118333
118334
118335
118336
118337
118338
118339
118340
118341
118342
118343
118344
118345
118346
118347
118348
118349
118350
118351
118352
118353
118354
118355
118356
118357
118358
118359
118360
118361
118362
118363
118364
118365
118366
118367
118368
118369
118370
118371
118372
118373
118374
118375
118376
118377
118378
118379
118380
118381
118382
118383
118384
118385
118386
118387
118388
118389
118390
118391
118392
118393
118394
118395
118396
118397
118398
118399
118400
118401
118402
118403
118404
118405
118406
118407
118408
118409
118410
118411
118412
118413
118414
118415
118416
118417
118418
118419
118420
118421
118422
118423
118424
118425
118426
118427
118428
118429
118430
118431
118432
118433
118434
118435
118436
118437
118438
118439
118440
118441
118442
118443
118444
118445
118446
118447
118448
118449
118450
118451
118452
118453
118454
118455
118456
118457
118458
118459
118460
118461
118462
118463
118464
118465
118466
118467
118468
118469
118470
118471
118472
118473
118474
118475
118476
118477
118478
118479
118480
118481
118482
118483
118484
118485
118486
118487
118488
118489
118490
118491
118492
118493
118494
118495
118496
118497
118498
118499
118500
118501
118502
118503
118504
118505
118506
118507
118508
118509
118510
118511
118512
118513
118514
118515
118516
118517
118518
118519
118520
118521
118522
118523
118524
118525
118526
118527
118528
118529
118530
118531
118532
118533
118534
118535
118536
118537
118538
118539
118540
118541
118542
118543
118544
118545
118546
118547
118548
118549
118550
118551
118552
118553
118554
118555
118556
118557
118558
118559
118560
118561
118562
118563
118564
118565
118566
118567
118568
118569
118570
118571
118572
118573
118574
118575
118576
118577
118578
118579
118580
118581
118582
118583
118584
118585
118586
118587
118588
118589
118590
118591
118592
118593
118594
118595
118596
118597
118598
118599
118600
118601
118602
118603
118604
118605
118606
118607
118608
118609
118610
118611
118612
118613
118614
118615
118616
118617
118618
118619
118620
118621
118622
118623
118624
118625
118626
118627
118628
118629
118630
118631
118632
118633
118634
118635
118636
118637
118638
118639
118640
118641
118642
118643
118644
118645
118646
118647
118648
118649
118650
118651
118652
118653
118654
118655
118656
118657
118658
118659
118660
118661
118662
118663
118664
118665
118666
118667
118668
118669
118670
118671
118672
118673
118674
118675
118676
118677
118678
118679
118680
118681
118682
118683
118684
118685
118686
118687
118688
118689
118690
118691
118692
118693
118694
118695
118696
118697
118698
118699
118700
118701
118702
118703
118704
118705
118706
118707
118708
118709
118710
118711
118712
118713
118714
118715
118716
118717
118718
118719
118720
118721
118722
118723
118724
118725
118726
118727
118728
118729
118730
118731
118732
118733
118734
118735
118736
118737
118738
118739
118740
118741
118742
118743
118744
118745
118746
118747
118748
118749
118750
118751
118752
118753
118754
118755
118756
118757
118758
118759
118760
118761
118762
118763
118764
118765
118766
118767
118768
118769
118770
118771
118772
118773
118774
118775
118776
118777
118778
118779
118780
118781
118782
118783
118784
118785
118786
118787
118788
118789
118790
118791
118792
118793
118794
118795
118796
118797
118798
118799
118800
118801
118802
118803
118804
118805
118806
118807
118808
118809
118810
118811
118812
118813
118814
118815
118816
118817
118818
118819
118820
118821
118822
118823
118824
118825
118826
118827
118828
118829
118830
118831
118832
118833
118834
118835
118836
118837
118838
118839
118840
118841
118842
118843
118844
118845
118846
118847
118848
118849
118850
118851
118852
118853
118854
118855
118856
118857
118858
118859
118860
118861
118862
118863
118864
118865
118866
118867
118868
118869
118870
118871
118872
118873
118874
118875
118876
118877
118878
118879
118880
118881
118882
118883
118884
118885
118886
118887
118888
118889
118890
118891
118892
118893
118894
118895
118896
118897
118898
118899
118900
118901
118902
118903
118904
118905
118906
118907
118908
118909
118910
118911
118912
118913
118914
118915
118916
118917
118918
118919
118920
118921
118922
118923
118924
118925
118926
118927
118928
118929
118930
118931
118932
118933
118934
118935
118936
118937
118938
118939
118940
118941
118942
118943
118944
118945
118946
118947
118948
118949
118950
118951
118952
118953
118954
118955
118956
118957
118958
118959
118960
118961
118962
118963
118964
118965
118966
118967
118968
118969
118970
118971
118972
118973
118974
118975
118976
118977
118978
118979
118980
118981
118982
118983
118984
118985
118986
118987
118988
118989
118990
118991
118992
118993
118994
118995
118996
118997
118998
118999
119000
119001
119002
119003
119004
119005
119006
119007
119008
119009
119010
119011
119012
119013
119014
119015
119016
119017
119018
119019
119020
119021
119022
119023
119024
119025
119026
119027
119028
119029
119030
119031
119032
119033
119034
119035
119036
119037
119038
119039
119040
119041
119042
119043
119044
119045
119046
119047
119048
119049
119050
119051
119052
119053
119054
119055
119056
119057
119058
119059
119060
119061
119062
119063
119064
119065
119066
119067
119068
119069
119070
119071
119072
119073
119074
119075
119076
119077
119078
119079
119080
119081
119082
119083
119084
119085
119086
119087
119088
119089
119090
119091
119092
119093
119094
119095
119096
119097
119098
119099
119100
119101
119102
119103
119104
119105
119106
119107
119108
119109
119110
119111
119112
119113
119114
119115
119116
119117
119118
119119
119120
119121
119122
119123
119124
119125
119126
119127
119128
119129
119130
119131
119132
119133
119134
119135
119136
119137
119138
119139
119140
119141
119142
119143
119144
119145
119146
119147
119148
119149
119150
119151
119152
119153
119154
119155
119156
119157
119158
119159
119160
119161
119162
119163
119164
119165
119166
119167
119168
119169
119170
119171
119172
119173
119174
119175
119176
119177
119178
119179
119180
119181
119182
119183
119184
119185
119186
119187
119188
119189
119190
119191
119192
119193
119194
119195
119196
119197
119198
119199
119200
119201
119202
119203
119204
119205
119206
119207
119208
119209
119210
119211
119212
119213
119214
119215
119216
119217
119218
119219
119220
119221
119222
119223
119224
119225
119226
119227
119228
119229
119230
119231
119232
119233
119234
119235
119236
119237
119238
119239
119240
119241
119242
119243
119244
119245
119246
119247
119248
119249
119250
119251
119252
119253
119254
119255
119256
119257
119258
119259
119260
119261
119262
119263
119264
119265
119266
119267
119268
119269
119270
119271
119272
119273
119274
119275
119276
119277
119278
119279
119280
119281
119282
119283
119284
119285
119286
119287
119288
119289
119290
119291
119292
119293
119294
119295
119296
119297
119298
119299
119300
119301
119302
119303
119304
119305
119306
119307
119308
119309
119310
119311
119312
119313
119314
119315
119316
119317
119318
119319
119320
119321
119322
119323
119324
119325
119326
119327
119328
119329
119330
119331
119332
119333
119334
119335
119336
119337
119338
119339
119340
119341
119342
119343
119344
119345
119346
119347
119348
119349
119350
119351
119352
119353
119354
119355
119356
119357
119358
119359
119360
119361
119362
119363
119364
119365
119366
119367
119368
119369
119370
119371
119372
119373
119374
119375
119376
119377
119378
119379
119380
119381
119382
119383
119384
119385
119386
119387
119388
119389
119390
119391
119392
119393
119394
119395
119396
119397
119398
119399
119400
119401
119402
119403
119404
119405
119406
119407
119408
119409
119410
119411
119412
119413
119414
119415
119416
119417
119418
119419
119420
119421
119422
119423
119424
119425
119426
119427
119428
119429
119430
119431
119432
119433
119434
119435
119436
119437
119438
119439
119440
119441
119442
119443
119444
119445
119446
119447
119448
119449
119450
119451
119452
119453
119454
119455
119456
119457
119458
119459
119460
119461
119462
119463
119464
119465
119466
119467
119468
119469
119470
119471
119472
119473
119474
119475
119476
119477
119478
119479
119480
119481
119482
119483
119484
119485
119486
119487
119488
119489
119490
119491
119492
119493
119494
119495
119496
119497
119498
119499
119500
119501
119502
119503
119504
119505
119506
119507
119508
119509
119510
119511
119512
119513
119514
119515
119516
119517
119518
119519
119520
119521
119522
119523
119524
119525
119526
119527
119528
119529
119530
119531
119532
119533
119534
119535
119536
119537
119538
119539
119540
119541
119542
119543
119544
119545
119546
119547
119548
119549
119550
119551
119552
119553
119554
119555
119556
119557
119558
119559
119560
119561
119562
119563
119564
119565
119566
119567
119568
119569
119570
119571
119572
119573
119574
119575
119576
119577
119578
119579
119580
119581
119582
119583
119584
119585
119586
119587
119588
119589
119590
119591
119592
119593
119594
119595
119596
119597
119598
119599
119600
119601
119602
119603
119604
119605
119606
119607
119608
119609
119610
119611
119612
119613
119614
119615
119616
119617
119618
119619
119620
119621
119622
119623
119624
119625
119626
119627
119628
119629
119630
119631
119632
119633
119634
119635
119636
119637
119638
119639
119640
119641
119642
119643
119644
119645
119646
119647
119648
119649
119650
119651
119652
119653
119654
119655
119656
119657
119658
119659
119660
119661
119662
119663
119664
119665
119666
119667
119668
119669
119670
119671
119672
119673
119674
119675
119676
119677
119678
119679
119680
119681
119682
119683
119684
119685
119686
119687
119688
119689
119690
119691
119692
119693
119694
119695
119696
119697
119698
119699
119700
119701
119702
119703
119704
119705
119706
119707
119708
119709
119710
119711
119712
119713
119714
119715
119716
119717
119718
119719
119720
119721
119722
119723
119724
119725
119726
119727
119728
119729
119730
119731
119732
119733
119734
119735
119736
119737
119738
119739
119740
119741
119742
119743
119744
119745
119746
119747
119748
119749
119750
119751
119752
119753
119754
119755
119756
119757
119758
119759
119760
119761
119762
119763
119764
119765
119766
119767
119768
119769
119770
119771
119772
119773
119774
119775
119776
119777
119778
119779
119780
119781
119782
119783
119784
119785
119786
119787
119788
119789
119790
119791
119792
119793
119794
119795
119796
119797
119798
119799
119800
119801
119802
119803
119804
119805
119806
119807
119808
119809
119810
119811
119812
119813
119814
119815
119816
119817
119818
119819
119820
119821
119822
119823
119824
119825
119826
119827
119828
119829
119830
119831
119832
119833
119834
119835
119836
119837
119838
119839
119840
119841
119842
119843
119844
119845
119846
119847
119848
119849
119850
119851
119852
119853
119854
119855
119856
119857
119858
119859
119860
119861
119862
119863
119864
119865
119866
119867
119868
119869
119870
119871
119872
119873
119874
119875
119876
119877
119878
119879
119880
119881
119882
119883
119884
119885
119886
119887
119888
119889
119890
119891
119892
119893
119894
119895
119896
119897
119898
119899
119900
119901
119902
119903
119904
119905
119906
119907
119908
119909
119910
119911
119912
119913
119914
119915
119916
119917
119918
119919
119920
119921
119922
119923
119924
119925
119926
119927
119928
119929
119930
119931
119932
119933
119934
119935
119936
119937
119938
119939
119940
119941
119942
119943
119944
119945
119946
119947
119948
119949
119950
119951
119952
119953
119954
119955
119956
119957
119958
119959
119960
119961
119962
119963
119964
119965
119966
119967
119968
119969
119970
119971
119972
119973
119974
119975
119976
119977
119978
119979
119980
119981
119982
119983
119984
119985
119986
119987
119988
119989
119990
119991
119992
119993
119994
119995
119996
119997
119998
119999
120000
120001
120002
120003
120004
120005
120006
120007
120008
120009
120010
120011
120012
120013
120014
120015
120016
120017
120018
120019
120020
120021
120022
120023
120024
120025
120026
120027
120028
120029
120030
120031
120032
120033
120034
120035
120036
120037
120038
120039
120040
120041
120042
120043
120044
120045
120046
120047
120048
120049
120050
120051
120052
120053
120054
120055
120056
120057
120058
120059
120060
120061
120062
120063
120064
120065
120066
120067
120068
120069
120070
120071
120072
120073
120074
120075
120076
120077
120078
120079
120080
120081
120082
120083
120084
120085
120086
120087
120088
120089
120090
120091
120092
120093
120094
120095
120096
120097
120098
120099
120100
120101
120102
120103
120104
120105
120106
120107
120108
120109
120110
120111
120112
120113
120114
120115
120116
120117
120118
120119
120120
120121
120122
120123
120124
120125
120126
120127
120128
120129
120130
120131
120132
120133
120134
120135
120136
120137
120138
120139
120140
120141
120142
120143
120144
120145
120146
120147
120148
120149
120150
120151
120152
120153
120154
120155
120156
120157
120158
120159
120160
120161
120162
120163
120164
120165
120166
120167
120168
120169
120170
120171
120172
120173
120174
120175
120176
120177
120178
120179
120180
120181
120182
120183
120184
120185
120186
120187
120188
120189
120190
120191
120192
120193
120194
120195
120196
120197
120198
120199
120200
120201
120202
120203
120204
120205
120206
120207
120208
120209
120210
120211
120212
120213
120214
120215
120216
120217
120218
120219
120220
120221
120222
120223
120224
120225
120226
120227
120228
120229
120230
120231
120232
120233
120234
120235
120236
120237
120238
120239
120240
120241
120242
120243
120244
120245
120246
120247
120248
120249
120250
120251
120252
120253
120254
120255
120256
120257
120258
120259
120260
120261
120262
120263
120264
120265
120266
120267
120268
120269
120270
120271
120272
120273
120274
120275
120276
120277
120278
120279
120280
120281
120282
120283
120284
120285
120286
120287
120288
120289
120290
120291
120292
120293
120294
120295
120296
120297
120298
120299
120300
120301
120302
120303
120304
120305
120306
120307
120308
120309
120310
120311
120312
120313
120314
120315
120316
120317
120318
120319
120320
120321
120322
120323
120324
120325
120326
120327
120328
120329
120330
120331
120332
120333
120334
120335
120336
120337
120338
120339
120340
120341
120342
120343
120344
120345
120346
120347
120348
120349
120350
120351
120352
120353
120354
120355
120356
120357
120358
120359
120360
120361
120362
120363
120364
120365
120366
120367
120368
120369
120370
120371
120372
120373
120374
120375
120376
120377
120378
120379
120380
120381
120382
120383
120384
120385
120386
120387
120388
120389
120390
120391
120392
120393
120394
120395
120396
120397
120398
120399
120400
120401
120402
120403
120404
120405
120406
120407
120408
120409
120410
120411
120412
120413
120414
120415
120416
120417
120418
120419
120420
120421
120422
120423
120424
120425
120426
120427
120428
120429
120430
120431
120432
120433
120434
120435
120436
120437
120438
120439
120440
120441
120442
120443
120444
120445
120446
120447
120448
120449
120450
120451
120452
120453
120454
120455
120456
120457
120458
120459
120460
120461
120462
120463
120464
120465
120466
120467
120468
120469
120470
120471
120472
120473
120474
120475
120476
120477
120478
120479
120480
120481
120482
120483
120484
120485
120486
120487
120488
120489
120490
120491
120492
120493
120494
120495
120496
120497
120498
120499
120500
120501
120502
120503
120504
120505
120506
120507
120508
120509
120510
120511
120512
120513
120514
120515
120516
120517
120518
120519
120520
120521
120522
120523
120524
120525
120526
120527
120528
120529
120530
120531
120532
120533
120534
120535
120536
120537
120538
120539
120540
120541
120542
120543
120544
120545
120546
120547
120548
120549
120550
120551
120552
120553
120554
120555
120556
120557
120558
120559
120560
120561
120562
120563
120564
120565
120566
120567
120568
120569
120570
120571
120572
120573
120574
120575
120576
120577
120578
120579
120580
120581
120582
120583
120584
120585
120586
120587
120588
120589
120590
120591
120592
120593
120594
120595
120596
120597
120598
120599
120600
120601
120602
120603
120604
120605
120606
120607
120608
120609
120610
120611
120612
120613
120614
120615
120616
120617
120618
120619
120620
120621
120622
120623
120624
120625
120626
120627
120628
120629
120630
120631
120632
120633
120634
120635
120636
120637
120638
120639
120640
120641
120642
120643
120644
120645
120646
120647
120648
120649
120650
120651
120652
120653
120654
120655
120656
120657
120658
120659
120660
120661
120662
120663
120664
120665
120666
120667
120668
120669
120670
120671
120672
120673
120674
120675
120676
120677
120678
120679
120680
120681
120682
120683
120684
120685
120686
120687
120688
120689
120690
120691
120692
120693
120694
120695
120696
120697
120698
120699
120700
120701
120702
120703
120704
120705
120706
120707
120708
120709
120710
120711
120712
120713
120714
120715
120716
120717
120718
120719
120720
120721
120722
120723
120724
120725
120726
120727
120728
120729
120730
120731
120732
120733
120734
120735
120736
120737
120738
120739
120740
120741
120742
120743
120744
120745
120746
120747
120748
120749
120750
120751
120752
120753
120754
120755
120756
120757
120758
120759
120760
120761
120762
120763
120764
120765
120766
120767
120768
120769
120770
120771
120772
120773
120774
120775
120776
120777
120778
120779
120780
120781
120782
120783
120784
120785
120786
120787
120788
120789
120790
120791
120792
120793
120794
120795
120796
120797
120798
120799
120800
120801
120802
120803
120804
120805
120806
120807
120808
120809
120810
120811
120812
120813
120814
120815
120816
120817
120818
120819
120820
120821
120822
120823
120824
120825
120826
120827
120828
120829
120830
120831
120832
120833
120834
120835
120836
120837
120838
120839
120840
120841
120842
120843
120844
120845
120846
120847
120848
120849
120850
120851
120852
120853
120854
120855
120856
120857
120858
120859
120860
120861
120862
120863
120864
120865
120866
120867
120868
120869
120870
120871
120872
120873
120874
120875
120876
120877
120878
120879
120880
120881
120882
120883
120884
120885
120886
120887
120888
120889
120890
120891
120892
120893
120894
120895
120896
120897
120898
120899
120900
120901
120902
120903
120904
120905
120906
120907
120908
120909
120910
120911
120912
120913
120914
120915
120916
120917
120918
120919
120920
120921
120922
120923
120924
120925
120926
120927
120928
120929
120930
120931
120932
120933
120934
120935
120936
120937
120938
120939
120940
120941
120942
120943
120944
120945
120946
120947
120948
120949
120950
120951
120952
120953
120954
120955
120956
120957
120958
120959
120960
120961
120962
120963
120964
120965
120966
120967
120968
120969
120970
120971
120972
120973
120974
120975
120976
120977
120978
120979
120980
120981
120982
120983
120984
120985
120986
120987
120988
120989
120990
120991
120992
120993
120994
120995
120996
120997
120998
120999
121000
121001
121002
121003
121004
121005
121006
121007
121008
121009
121010
121011
121012
121013
121014
121015
121016
121017
121018
121019
121020
121021
121022
121023
121024
121025
121026
121027
121028
121029
121030
121031
121032
121033
121034
121035
121036
121037
121038
121039
121040
121041
121042
121043
121044
121045
121046
121047
121048
121049
121050
121051
121052
121053
121054
121055
121056
121057
121058
121059
121060
121061
121062
121063
121064
121065
121066
121067
121068
121069
121070
121071
121072
121073
121074
121075
121076
121077
121078
121079
121080
121081
121082
121083
121084
121085
121086
121087
121088
121089
121090
121091
121092
121093
121094
121095
121096
121097
121098
121099
121100
121101
121102
121103
121104
121105
121106
121107
121108
121109
121110
121111
121112
121113
121114
121115
121116
121117
121118
121119
121120
121121
121122
121123
121124
121125
121126
121127
121128
121129
121130
121131
121132
121133
121134
121135
121136
121137
121138
121139
121140
121141
121142
121143
121144
121145
121146
121147
121148
121149
121150
121151
121152
121153
121154
121155
121156
121157
121158
121159
121160
121161
121162
121163
121164
121165
121166
121167
121168
121169
121170
121171
121172
121173
121174
121175
121176
121177
121178
121179
121180
121181
121182
121183
121184
121185
121186
121187
121188
121189
121190
121191
121192
121193
121194
121195
121196
121197
121198
121199
121200
121201
121202
121203
121204
121205
121206
121207
121208
121209
121210
121211
121212
121213
121214
121215
121216
121217
121218
121219
121220
121221
121222
121223
121224
121225
121226
121227
121228
121229
121230
121231
121232
121233
121234
121235
121236
121237
121238
121239
121240
121241
121242
121243
121244
121245
121246
121247
121248
121249
121250
121251
121252
121253
121254
121255
121256
121257
121258
121259
121260
121261
121262
121263
121264
121265
121266
121267
121268
121269
121270
121271
121272
121273
121274
121275
121276
121277
121278
121279
121280
121281
121282
121283
121284
121285
121286
121287
121288
121289
121290
121291
121292
121293
121294
121295
121296
121297
121298
121299
121300
121301
121302
121303
121304
121305
121306
121307
121308
121309
121310
121311
121312
121313
121314
121315
121316
121317
121318
121319
121320
121321
121322
121323
121324
121325
121326
121327
121328
121329
121330
121331
121332
121333
121334
121335
121336
121337
121338
121339
121340
121341
121342
121343
121344
121345
121346
121347
121348
121349
121350
121351
121352
121353
121354
121355
121356
121357
121358
121359
121360
121361
121362
121363
121364
121365
121366
121367
121368
121369
121370
121371
121372
121373
121374
121375
121376
121377
121378
121379
121380
121381
121382
121383
121384
121385
121386
121387
121388
121389
121390
121391
121392
121393
121394
121395
121396
121397
121398
121399
121400
121401
121402
121403
121404
121405
121406
121407
121408
121409
121410
121411
121412
121413
121414
121415
121416
121417
121418
121419
121420
121421
121422
121423
121424
121425
121426
121427
121428
121429
121430
121431
121432
121433
121434
121435
121436
121437
121438
121439
121440
121441
121442
121443
121444
121445
121446
121447
121448
121449
121450
121451
121452
121453
121454
121455
121456
121457
121458
121459
121460
121461
121462
121463
121464
121465
121466
121467
121468
121469
121470
121471
121472
121473
121474
121475
121476
121477
121478
121479
121480
121481
121482
121483
121484
121485
121486
121487
121488
121489
121490
121491
121492
121493
121494
121495
121496
121497
121498
121499
121500
121501
121502
121503
121504
121505
121506
121507
121508
121509
121510
121511
121512
121513
121514
121515
121516
121517
121518
121519
121520
121521
121522
121523
121524
121525
121526
121527
121528
121529
121530
121531
121532
121533
121534
121535
121536
121537
121538
121539
121540
121541
121542
121543
121544
121545
121546
121547
121548
121549
121550
121551
121552
121553
121554
121555
121556
121557
121558
121559
121560
121561
121562
121563
121564
121565
121566
121567
121568
121569
121570
121571
121572
121573
121574
121575
121576
121577
121578
121579
121580
121581
121582
121583
121584
121585
121586
121587
121588
121589
121590
121591
121592
121593
121594
121595
121596
121597
121598
121599
121600
121601
121602
121603
121604
121605
121606
121607
121608
121609
121610
121611
121612
121613
121614
121615
121616
121617
121618
121619
121620
121621
121622
121623
121624
121625
121626
121627
121628
121629
121630
121631
121632
121633
121634
121635
121636
121637
121638
121639
121640
121641
121642
121643
121644
121645
121646
121647
121648
121649
121650
121651
121652
121653
121654
121655
121656
121657
121658
121659
121660
121661
121662
121663
121664
121665
121666
121667
121668
121669
121670
121671
121672
121673
121674
121675
121676
121677
121678
121679
121680
121681
121682
121683
121684
121685
121686
121687
121688
121689
121690
121691
121692
121693
121694
121695
121696
121697
121698
121699
121700
121701
121702
121703
121704
121705
121706
121707
121708
121709
121710
121711
121712
121713
121714
121715
121716
121717
121718
121719
121720
121721
121722
121723
121724
121725
121726
121727
121728
121729
121730
121731
121732
121733
121734
121735
121736
121737
121738
121739
121740
121741
121742
121743
121744
121745
121746
121747
121748
121749
121750
121751
121752
121753
121754
121755
121756
121757
121758
121759
121760
121761
121762
121763
121764
121765
121766
121767
121768
121769
121770
121771
121772
121773
121774
121775
121776
121777
121778
121779
121780
121781
121782
121783
121784
121785
121786
121787
121788
121789
121790
121791
121792
121793
121794
121795
121796
121797
121798
121799
121800
121801
121802
121803
121804
121805
121806
121807
121808
121809
121810
121811
121812
121813
121814
121815
121816
121817
121818
121819
121820
121821
121822
121823
121824
121825
121826
121827
121828
121829
121830
121831
121832
121833
121834
121835
121836
121837
121838
121839
121840
121841
121842
121843
121844
121845
121846
121847
121848
121849
121850
121851
121852
121853
121854
121855
121856
121857
121858
121859
121860
121861
121862
121863
121864
121865
121866
121867
121868
121869
121870
121871
121872
121873
121874
121875
121876
121877
121878
121879
121880
121881
121882
121883
121884
121885
121886
121887
121888
121889
121890
121891
121892
121893
121894
121895
121896
121897
121898
121899
121900
121901
121902
121903
121904
121905
121906
121907
121908
121909
121910
121911
121912
121913
121914
121915
121916
121917
121918
121919
121920
121921
121922
121923
121924
121925
121926
121927
121928
121929
121930
121931
121932
121933
121934
121935
121936
121937
121938
121939
121940
121941
121942
121943
121944
121945
121946
121947
121948
121949
121950
121951
121952
121953
121954
121955
121956
121957
121958
121959
121960
121961
121962
121963
121964
121965
121966
121967
121968
121969
121970
121971
121972
121973
121974
121975
121976
121977
121978
121979
121980
121981
121982
121983
121984
121985
121986
121987
121988
121989
121990
121991
121992
121993
121994
121995
121996
121997
121998
121999
122000
122001
122002
122003
122004
122005
122006
122007
122008
122009
122010
122011
122012
122013
122014
122015
122016
122017
122018
122019
122020
122021
122022
122023
122024
122025
122026
122027
122028
122029
122030
122031
122032
122033
122034
122035
122036
122037
122038
122039
122040
122041
122042
122043
122044
122045
122046
122047
122048
122049
122050
122051
122052
122053
122054
122055
122056
122057
122058
122059
122060
122061
122062
122063
122064
122065
122066
122067
122068
122069
122070
122071
122072
122073
122074
122075
122076
122077
122078
122079
122080
122081
122082
122083
122084
122085
122086
122087
122088
122089
122090
122091
122092
122093
122094
122095
122096
122097
122098
122099
122100
122101
122102
122103
122104
122105
122106
122107
122108
122109
122110
122111
122112
122113
122114
122115
122116
122117
122118
122119
122120
122121
122122
122123
122124
122125
122126
122127
122128
122129
122130
122131
122132
122133
122134
122135
122136
122137
122138
122139
122140
122141
122142
122143
122144
122145
122146
122147
122148
122149
122150
122151
122152
122153
122154
122155
122156
122157
122158
122159
122160
122161
122162
122163
122164
122165
122166
122167
122168
122169
122170
122171
122172
122173
122174
122175
122176
122177
122178
122179
122180
122181
122182
122183
122184
122185
122186
122187
122188
122189
122190
122191
122192
122193
122194
122195
122196
122197
122198
122199
122200
122201
122202
122203
122204
122205
122206
122207
122208
122209
122210
122211
122212
122213
122214
122215
122216
122217
122218
122219
122220
122221
122222
122223
122224
122225
122226
122227
122228
122229
122230
122231
122232
122233
122234
122235
122236
122237
122238
122239
122240
122241
122242
122243
122244
122245
122246
122247
122248
122249
122250
122251
122252
122253
122254
122255
122256
122257
122258
122259
122260
122261
122262
122263
122264
122265
122266
122267
122268
122269
122270
122271
122272
122273
122274
122275
122276
122277
122278
122279
122280
122281
122282
122283
122284
122285
122286
122287
122288
122289
122290
122291
122292
122293
122294
122295
122296
122297
122298
122299
122300
122301
122302
122303
122304
122305
122306
122307
122308
122309
122310
122311
122312
122313
122314
122315
122316
122317
122318
122319
122320
122321
122322
122323
122324
122325
122326
122327
122328
122329
122330
122331
122332
122333
122334
122335
122336
122337
122338
122339
122340
122341
122342
122343
122344
122345
122346
122347
122348
122349
122350
122351
122352
122353
122354
122355
122356
122357
122358
122359
122360
122361
122362
122363
122364
122365
122366
122367
122368
122369
122370
122371
122372
122373
122374
122375
122376
122377
122378
122379
122380
122381
122382
122383
122384
122385
122386
122387
122388
122389
122390
122391
122392
122393
122394
122395
122396
122397
122398
122399
122400
122401
122402
122403
122404
122405
122406
122407
122408
122409
122410
122411
122412
122413
122414
122415
122416
122417
122418
122419
122420
122421
122422
122423
122424
122425
122426
122427
122428
122429
122430
122431
122432
122433
122434
122435
122436
122437
122438
122439
122440
122441
122442
122443
122444
122445
122446
122447
122448
122449
122450
122451
122452
122453
122454
122455
122456
122457
122458
122459
122460
122461
122462
122463
122464
122465
122466
122467
122468
122469
122470
122471
122472
122473
122474
122475
122476
122477
122478
122479
122480
122481
122482
122483
122484
122485
122486
122487
122488
122489
122490
122491
122492
122493
122494
122495
122496
122497
122498
122499
122500
122501
122502
122503
122504
122505
122506
122507
122508
122509
122510
122511
122512
122513
122514
122515
122516
122517
122518
122519
122520
122521
122522
122523
122524
122525
122526
122527
122528
122529
122530
122531
122532
122533
122534
122535
122536
122537
122538
122539
122540
122541
122542
122543
122544
122545
122546
122547
122548
122549
122550
122551
122552
122553
122554
122555
122556
122557
122558
122559
122560
122561
122562
122563
122564
122565
122566
122567
122568
122569
122570
122571
122572
122573
122574
122575
122576
122577
122578
122579
122580
122581
122582
122583
122584
122585
122586
122587
122588
122589
122590
122591
122592
122593
122594
122595
122596
122597
122598
122599
122600
122601
122602
122603
122604
122605
122606
122607
122608
122609
122610
122611
122612
122613
122614
122615
122616
122617
122618
122619
122620
122621
122622
122623
122624
122625
122626
122627
122628
122629
122630
122631
122632
122633
122634
122635
122636
122637
122638
122639
122640
122641
122642
122643
122644
122645
122646
122647
122648
122649
122650
122651
122652
122653
122654
122655
122656
122657
122658
122659
122660
122661
122662
122663
122664
122665
122666
122667
122668
122669
122670
122671
122672
122673
122674
122675
122676
122677
122678
122679
122680
122681
122682
122683
122684
122685
122686
122687
122688
122689
122690
122691
122692
122693
122694
122695
122696
122697
122698
122699
122700
122701
122702
122703
122704
122705
122706
122707
122708
122709
122710
122711
122712
122713
122714
122715
122716
122717
122718
122719
122720
122721
122722
122723
122724
122725
122726
122727
122728
122729
122730
122731
122732
122733
122734
122735
122736
122737
122738
122739
122740
122741
122742
122743
122744
122745
122746
122747
122748
122749
122750
122751
122752
122753
122754
122755
122756
122757
122758
122759
122760
122761
122762
122763
122764
122765
122766
122767
122768
122769
122770
122771
122772
122773
122774
122775
122776
122777
122778
122779
122780
122781
122782
122783
122784
122785
122786
122787
122788
122789
122790
122791
122792
122793
122794
122795
122796
122797
122798
122799
122800
122801
122802
122803
122804
122805
122806
122807
122808
122809
122810
122811
122812
122813
122814
122815
122816
122817
122818
122819
122820
122821
122822
122823
122824
122825
122826
122827
122828
122829
122830
122831
122832
122833
122834
122835
122836
122837
122838
122839
122840
122841
122842
122843
122844
122845
122846
122847
122848
122849
122850
122851
122852
122853
122854
122855
122856
122857
122858
122859
122860
122861
122862
122863
122864
122865
122866
122867
122868
122869
122870
122871
122872
122873
122874
122875
122876
122877
122878
122879
122880
122881
122882
122883
122884
122885
122886
122887
122888
122889
122890
122891
122892
122893
122894
122895
122896
122897
122898
122899
122900
122901
122902
122903
122904
122905
122906
122907
122908
122909
122910
122911
122912
122913
122914
122915
122916
122917
122918
122919
122920
122921
122922
122923
122924
122925
122926
122927
122928
122929
122930
122931
122932
122933
122934
122935
122936
122937
122938
122939
122940
122941
122942
122943
122944
122945
122946
122947
122948
122949
122950
122951
122952
122953
122954
122955
122956
122957
122958
122959
122960
122961
122962
122963
122964
122965
122966
122967
122968
122969
122970
122971
122972
122973
122974
122975
122976
122977
122978
122979
122980
122981
122982
122983
122984
122985
122986
122987
122988
122989
122990
122991
122992
122993
122994
122995
122996
122997
122998
122999
123000
123001
123002
123003
123004
123005
123006
123007
123008
123009
123010
123011
123012
123013
123014
123015
123016
123017
123018
123019
123020
123021
123022
123023
123024
123025
123026
123027
123028
123029
123030
123031
123032
123033
123034
123035
123036
123037
123038
123039
123040
123041
123042
123043
123044
123045
123046
123047
123048
123049
123050
123051
123052
123053
123054
123055
123056
123057
123058
123059
123060
123061
123062
123063
123064
123065
123066
123067
123068
123069
123070
123071
123072
123073
123074
123075
123076
123077
123078
123079
123080
123081
123082
123083
123084
123085
123086
123087
123088
123089
123090
123091
123092
123093
123094
123095
123096
123097
123098
123099
123100
123101
123102
123103
123104
123105
123106
123107
123108
123109
123110
123111
123112
123113
123114
123115
123116
123117
123118
123119
123120
123121
123122
123123
123124
123125
123126
123127
123128
123129
123130
123131
123132
123133
123134
123135
123136
123137
123138
123139
123140
123141
123142
123143
123144
123145
123146
123147
123148
123149
123150
123151
123152
123153
123154
123155
123156
123157
123158
123159
123160
123161
123162
123163
123164
123165
123166
123167
123168
123169
123170
123171
123172
123173
123174
123175
123176
123177
123178
123179
123180
123181
123182
123183
123184
123185
123186
123187
123188
123189
123190
123191
123192
123193
123194
123195
123196
123197
123198
123199
123200
123201
123202
123203
123204
123205
123206
123207
123208
123209
123210
123211
123212
123213
123214
123215
123216
123217
123218
123219
123220
123221
123222
123223
123224
123225
123226
123227
123228
123229
123230
123231
123232
123233
123234
123235
123236
123237
123238
123239
123240
123241
123242
123243
123244
123245
123246
123247
123248
123249
123250
123251
123252
123253
123254
123255
123256
123257
123258
123259
123260
123261
123262
123263
123264
123265
123266
123267
123268
123269
123270
123271
123272
123273
123274
123275
123276
123277
123278
123279
123280
123281
123282
123283
123284
123285
123286
123287
123288
123289
123290
123291
123292
123293
123294
123295
123296
123297
123298
123299
123300
123301
123302
123303
123304
123305
123306
123307
123308
123309
123310
123311
123312
123313
123314
123315
123316
123317
123318
123319
123320
123321
123322
123323
123324
123325
123326
123327
123328
123329
123330
123331
123332
123333
123334
123335
123336
123337
123338
123339
123340
123341
123342
123343
123344
123345
123346
123347
123348
123349
123350
123351
123352
123353
123354
123355
123356
123357
123358
123359
123360
123361
123362
123363
123364
123365
123366
123367
123368
123369
123370
123371
123372
123373
123374
123375
123376
123377
123378
123379
123380
123381
123382
123383
123384
123385
123386
123387
123388
123389
123390
123391
123392
123393
123394
123395
123396
123397
123398
123399
123400
123401
123402
123403
123404
123405
123406
123407
123408
123409
123410
123411
123412
123413
123414
123415
123416
123417
123418
123419
123420
123421
123422
123423
123424
123425
123426
123427
123428
123429
123430
123431
123432
123433
123434
123435
123436
123437
123438
123439
123440
123441
123442
123443
123444
123445
123446
123447
123448
123449
123450
123451
123452
123453
123454
123455
123456
123457
123458
123459
123460
123461
123462
123463
123464
123465
123466
123467
123468
123469
123470
123471
123472
123473
123474
123475
123476
123477
123478
123479
123480
123481
123482
123483
123484
123485
123486
123487
123488
123489
123490
123491
123492
123493
123494
123495
123496
123497
123498
123499
123500
123501
123502
123503
123504
123505
123506
123507
123508
123509
123510
123511
123512
123513
123514
123515
123516
123517
123518
123519
123520
123521
123522
123523
123524
123525
123526
123527
123528
123529
123530
123531
123532
123533
123534
123535
123536
123537
123538
123539
123540
123541
123542
123543
123544
123545
123546
123547
123548
123549
123550
123551
123552
123553
123554
123555
123556
123557
123558
123559
123560
123561
123562
123563
123564
123565
123566
123567
123568
123569
123570
123571
123572
123573
123574
123575
123576
123577
123578
123579
123580
123581
123582
123583
123584
123585
123586
123587
123588
123589
123590
123591
123592
123593
123594
123595
123596
123597
123598
123599
123600
123601
123602
123603
123604
123605
123606
123607
123608
123609
123610
123611
123612
123613
123614
123615
123616
123617
123618
123619
123620
123621
123622
123623
123624
123625
123626
123627
123628
123629
123630
123631
123632
123633
123634
123635
123636
123637
123638
123639
123640
123641
123642
123643
123644
123645
123646
123647
123648
123649
123650
123651
123652
123653
123654
123655
123656
123657
123658
123659
123660
123661
123662
123663
123664
123665
123666
123667
123668
123669
123670
123671
123672
123673
123674
123675
123676
123677
123678
123679
123680
123681
123682
123683
123684
123685
123686
123687
123688
123689
123690
123691
123692
123693
123694
123695
123696
123697
123698
123699
123700
123701
123702
123703
123704
123705
123706
123707
123708
123709
123710
123711
123712
123713
123714
123715
123716
123717
123718
123719
123720
123721
123722
123723
123724
123725
123726
123727
123728
123729
123730
123731
123732
123733
123734
123735
123736
123737
123738
123739
123740
123741
123742
123743
123744
123745
123746
123747
123748
123749
123750
123751
123752
123753
123754
123755
123756
123757
123758
123759
123760
123761
123762
123763
123764
123765
123766
123767
123768
123769
123770
123771
123772
123773
123774
123775
123776
123777
123778
123779
123780
123781
123782
123783
123784
123785
123786
123787
123788
123789
123790
123791
123792
123793
123794
123795
123796
123797
123798
123799
123800
123801
123802
123803
123804
123805
123806
123807
123808
123809
123810
123811
123812
123813
123814
123815
123816
123817
123818
123819
123820
123821
123822
123823
123824
123825
123826
123827
123828
123829
123830
123831
123832
123833
123834
123835
123836
123837
123838
123839
123840
123841
123842
123843
123844
123845
123846
123847
123848
123849
123850
123851
123852
123853
123854
123855
123856
123857
123858
123859
123860
123861
123862
123863
123864
123865
123866
123867
123868
123869
123870
123871
123872
123873
123874
123875
123876
123877
123878
123879
123880
123881
123882
123883
123884
123885
123886
123887
123888
123889
123890
123891
123892
123893
123894
123895
123896
123897
123898
123899
123900
123901
123902
123903
123904
123905
123906
123907
123908
123909
123910
123911
123912
123913
123914
123915
123916
123917
123918
123919
123920
123921
123922
123923
123924
123925
123926
123927
123928
123929
123930
123931
123932
123933
123934
123935
123936
123937
123938
123939
123940
123941
123942
123943
123944
123945
123946
123947
123948
123949
123950
123951
123952
123953
123954
123955
123956
123957
123958
123959
123960
123961
123962
123963
123964
123965
123966
123967
123968
123969
123970
123971
123972
123973
123974
123975
123976
123977
123978
123979
123980
123981
123982
123983
123984
123985
123986
123987
123988
123989
123990
123991
123992
123993
123994
123995
123996
123997
123998
123999
124000
124001
124002
124003
124004
124005
124006
124007
124008
124009
124010
124011
124012
124013
124014
124015
124016
124017
124018
124019
124020
124021
124022
124023
124024
124025
124026
124027
124028
124029
124030
124031
124032
124033
124034
124035
124036
124037
124038
124039
124040
124041
124042
124043
124044
124045
124046
124047
124048
124049
124050
124051
124052
124053
124054
124055
124056
124057
124058
124059
124060
124061
124062
124063
124064
124065
124066
124067
124068
124069
124070
124071
124072
124073
124074
124075
124076
124077
124078
124079
124080
124081
124082
124083
124084
124085
124086
124087
124088
124089
124090
124091
124092
124093
124094
124095
124096
124097
124098
124099
124100
124101
124102
124103
124104
124105
124106
124107
124108
124109
124110
124111
124112
124113
124114
124115
124116
124117
124118
124119
124120
124121
124122
124123
124124
124125
124126
124127
124128
124129
124130
124131
124132
124133
124134
124135
124136
124137
124138
124139
124140
124141
124142
124143
124144
124145
124146
124147
124148
124149
124150
124151
124152
124153
124154
124155
124156
124157
124158
124159
124160
124161
124162
124163
124164
124165
124166
124167
124168
124169
124170
124171
124172
124173
124174
124175
124176
124177
124178
124179
124180
124181
124182
124183
124184
124185
124186
124187
124188
124189
124190
124191
124192
124193
124194
124195
124196
124197
124198
124199
124200
124201
124202
124203
124204
124205
124206
124207
124208
124209
124210
124211
124212
124213
124214
124215
124216
124217
124218
124219
124220
124221
124222
124223
124224
124225
124226
124227
124228
124229
124230
124231
124232
124233
124234
124235
124236
124237
124238
124239
124240
124241
124242
124243
124244
124245
124246
124247
124248
124249
124250
124251
124252
124253
124254
124255
124256
124257
124258
124259
124260
124261
124262
124263
124264
124265
124266
124267
124268
124269
124270
124271
124272
124273
124274
124275
124276
124277
124278
124279
124280
124281
124282
124283
124284
124285
124286
124287
124288
124289
124290
124291
124292
124293
124294
124295
124296
124297
124298
124299
124300
124301
124302
124303
124304
124305
124306
124307
124308
124309
124310
124311
124312
124313
124314
124315
124316
124317
124318
124319
124320
124321
124322
124323
124324
124325
124326
124327
124328
124329
124330
124331
124332
124333
124334
124335
124336
124337
124338
124339
124340
124341
124342
124343
124344
124345
124346
124347
124348
124349
124350
124351
124352
124353
124354
124355
124356
124357
124358
124359
124360
124361
124362
124363
124364
124365
124366
124367
124368
124369
124370
124371
124372
124373
124374
124375
124376
124377
124378
124379
124380
124381
124382
124383
124384
124385
124386
124387
124388
124389
124390
124391
124392
124393
124394
124395
124396
124397
124398
124399
124400
124401
124402
124403
124404
124405
124406
124407
124408
124409
124410
124411
124412
124413
124414
124415
124416
124417
124418
124419
124420
124421
124422
124423
124424
124425
124426
124427
124428
124429
124430
124431
124432
124433
124434
124435
124436
124437
124438
124439
124440
124441
124442
124443
124444
124445
124446
124447
124448
124449
124450
124451
124452
124453
124454
124455
124456
124457
124458
124459
124460
124461
124462
124463
124464
124465
124466
124467
124468
124469
124470
124471
124472
124473
124474
124475
124476
124477
124478
124479
124480
124481
124482
124483
124484
124485
124486
124487
124488
124489
124490
124491
124492
124493
124494
124495
124496
124497
124498
124499
124500
124501
124502
124503
124504
124505
124506
124507
124508
124509
124510
124511
124512
124513
124514
124515
124516
124517
124518
124519
124520
124521
124522
124523
124524
124525
124526
124527
124528
124529
124530
124531
124532
124533
124534
124535
124536
124537
124538
124539
124540
124541
124542
124543
124544
124545
124546
124547
124548
124549
124550
124551
124552
124553
124554
124555
124556
124557
124558
124559
124560
124561
124562
124563
124564
124565
124566
124567
124568
124569
124570
124571
124572
124573
124574
124575
124576
124577
124578
124579
124580
124581
124582
124583
124584
124585
124586
124587
124588
124589
124590
124591
124592
124593
124594
124595
124596
124597
124598
124599
124600
124601
124602
124603
124604
124605
124606
124607
124608
124609
124610
124611
124612
124613
124614
124615
124616
124617
124618
124619
124620
124621
124622
124623
124624
124625
124626
124627
124628
124629
124630
124631
124632
124633
124634
124635
124636
124637
124638
124639
124640
124641
124642
124643
124644
124645
124646
124647
124648
124649
124650
124651
124652
124653
124654
124655
124656
124657
124658
124659
124660
124661
124662
124663
124664
124665
124666
124667
124668
124669
124670
124671
124672
124673
124674
124675
124676
124677
124678
124679
124680
124681
124682
124683
124684
124685
124686
124687
124688
124689
124690
124691
124692
124693
124694
124695
124696
124697
124698
124699
124700
124701
124702
124703
124704
124705
124706
124707
124708
124709
124710
124711
124712
124713
124714
124715
124716
124717
124718
124719
124720
124721
124722
124723
124724
124725
124726
124727
124728
124729
124730
124731
124732
124733
124734
124735
124736
124737
124738
124739
124740
124741
124742
124743
124744
124745
124746
124747
124748
124749
124750
124751
124752
124753
124754
124755
124756
124757
124758
124759
124760
124761
124762
124763
124764
124765
124766
124767
124768
124769
124770
124771
124772
124773
124774
124775
124776
124777
124778
124779
124780
124781
124782
124783
124784
124785
124786
124787
124788
124789
124790
124791
124792
124793
124794
124795
124796
124797
124798
124799
124800
124801
124802
124803
124804
124805
124806
124807
124808
124809
124810
124811
124812
124813
124814
124815
124816
124817
124818
124819
124820
124821
124822
124823
124824
124825
124826
124827
124828
124829
124830
124831
124832
124833
124834
124835
124836
124837
124838
124839
124840
124841
124842
124843
124844
124845
124846
124847
124848
124849
124850
124851
124852
124853
124854
124855
124856
124857
124858
124859
124860
124861
124862
124863
124864
124865
124866
124867
124868
124869
124870
124871
124872
124873
124874
124875
124876
124877
124878
124879
124880
124881
124882
124883
124884
124885
124886
124887
124888
124889
124890
124891
124892
124893
124894
124895
124896
124897
124898
124899
124900
124901
124902
124903
124904
124905
124906
124907
124908
124909
124910
124911
124912
124913
124914
124915
124916
124917
124918
124919
124920
124921
124922
124923
124924
124925
124926
124927
124928
124929
124930
124931
124932
124933
124934
124935
124936
124937
124938
124939
124940
124941
124942
124943
124944
124945
124946
124947
124948
124949
124950
124951
124952
124953
124954
124955
124956
124957
124958
124959
124960
124961
124962
124963
124964
124965
124966
124967
124968
124969
124970
124971
124972
124973
124974
124975
124976
124977
124978
124979
124980
124981
124982
124983
124984
124985
124986
124987
124988
124989
124990
124991
124992
124993
124994
124995
124996
124997
124998
124999
125000
125001
125002
125003
125004
125005
125006
125007
125008
125009
125010
125011
125012
125013
125014
125015
125016
125017
125018
125019
125020
125021
125022
125023
125024
125025
125026
125027
125028
125029
125030
125031
125032
125033
125034
125035
125036
125037
125038
125039
125040
125041
125042
125043
125044
125045
125046
125047
125048
125049
125050
125051
125052
125053
125054
125055
125056
125057
125058
125059
125060
125061
125062
125063
125064
125065
125066
125067
125068
125069
125070
125071
125072
125073
125074
125075
125076
125077
125078
125079
125080
125081
125082
125083
125084
125085
125086
125087
125088
125089
125090
125091
125092
125093
125094
125095
125096
125097
125098
125099
125100
125101
125102
125103
125104
125105
125106
125107
125108
125109
125110
125111
125112
125113
125114
125115
125116
125117
125118
125119
125120
125121
125122
125123
125124
125125
125126
125127
125128
125129
125130
125131
125132
125133
125134
125135
125136
125137
125138
125139
125140
125141
125142
125143
125144
125145
125146
125147
125148
125149
125150
125151
125152
125153
125154
125155
125156
125157
125158
125159
125160
125161
125162
125163
125164
125165
125166
125167
125168
125169
125170
125171
125172
125173
125174
125175
125176
125177
125178
125179
125180
125181
125182
125183
125184
125185
125186
125187
125188
125189
125190
125191
125192
125193
125194
125195
125196
125197
125198
125199
125200
125201
125202
125203
125204
125205
125206
125207
125208
125209
125210
125211
125212
125213
125214
125215
125216
125217
125218
125219
125220
125221
125222
125223
125224
125225
125226
125227
125228
125229
125230
125231
125232
125233
125234
125235
125236
125237
125238
125239
125240
125241
125242
125243
125244
125245
125246
125247
125248
125249
125250
125251
125252
125253
125254
125255
125256
125257
125258
125259
125260
125261
125262
125263
125264
125265
125266
125267
125268
125269
125270
125271
125272
125273
125274
125275
125276
125277
125278
125279
125280
125281
125282
125283
125284
125285
125286
125287
125288
125289
125290
125291
125292
125293
125294
125295
125296
125297
125298
125299
125300
125301
125302
125303
125304
125305
125306
125307
125308
125309
125310
125311
125312
125313
125314
125315
125316
125317
125318
125319
125320
125321
125322
125323
125324
125325
125326
125327
125328
125329
125330
125331
125332
125333
125334
125335
125336
125337
125338
125339
125340
125341
125342
125343
125344
125345
125346
125347
125348
125349
125350
125351
125352
125353
125354
125355
125356
125357
125358
125359
125360
125361
125362
125363
125364
125365
125366
125367
125368
125369
125370
125371
125372
125373
125374
125375
125376
125377
125378
125379
125380
125381
125382
125383
125384
125385
125386
125387
125388
125389
125390
125391
125392
125393
125394
125395
125396
125397
125398
125399
125400
125401
125402
125403
125404
125405
125406
125407
125408
125409
125410
125411
125412
125413
125414
125415
125416
125417
125418
125419
125420
125421
125422
125423
125424
125425
125426
125427
125428
125429
125430
125431
125432
125433
125434
125435
125436
125437
125438
125439
125440
125441
125442
125443
125444
125445
125446
125447
125448
125449
125450
125451
125452
125453
125454
125455
125456
125457
125458
125459
125460
125461
125462
125463
125464
125465
125466
125467
125468
125469
125470
125471
125472
125473
125474
125475
125476
125477
125478
125479
125480
125481
125482
125483
125484
125485
125486
125487
125488
125489
125490
125491
125492
125493
125494
125495
125496
125497
125498
125499
125500
125501
125502
125503
125504
125505
125506
125507
125508
125509
125510
125511
125512
125513
125514
125515
125516
125517
125518
125519
125520
125521
125522
125523
125524
125525
125526
125527
125528
125529
125530
125531
125532
125533
125534
125535
125536
125537
125538
125539
125540
125541
125542
125543
125544
125545
125546
125547
125548
125549
125550
125551
125552
125553
125554
125555
125556
125557
125558
125559
125560
125561
125562
125563
125564
125565
125566
125567
125568
125569
125570
125571
125572
125573
125574
125575
125576
125577
125578
125579
125580
125581
125582
125583
125584
125585
125586
125587
125588
125589
125590
125591
125592
125593
125594
125595
125596
125597
125598
125599
125600
125601
125602
125603
125604
125605
125606
125607
125608
125609
125610
125611
125612
125613
125614
125615
125616
125617
125618
125619
125620
125621
125622
125623
125624
125625
125626
125627
125628
125629
125630
125631
125632
125633
125634
125635
125636
125637
125638
125639
125640
125641
125642
125643
125644
125645
125646
125647
125648
125649
125650
125651
125652
125653
125654
125655
125656
125657
125658
125659
125660
125661
125662
125663
125664
125665
125666
125667
125668
125669
125670
125671
125672
125673
125674
125675
125676
125677
125678
125679
125680
125681
125682
125683
125684
125685
125686
125687
125688
125689
125690
125691
125692
125693
125694
125695
125696
125697
125698
125699
125700
125701
125702
125703
125704
125705
125706
125707
125708
125709
125710
125711
125712
125713
125714
125715
125716
125717
125718
125719
125720
125721
125722
125723
125724
125725
125726
125727
125728
125729
125730
125731
125732
125733
125734
125735
125736
125737
125738
125739
125740
125741
125742
125743
125744
125745
125746
125747
125748
125749
125750
125751
125752
125753
125754
125755
125756
125757
125758
125759
125760
125761
125762
125763
125764
125765
125766
125767
125768
125769
125770
125771
125772
125773
125774
125775
125776
125777
125778
125779
125780
125781
125782
125783
125784
125785
125786
125787
125788
125789
125790
125791
125792
125793
125794
125795
125796
125797
125798
125799
125800
125801
125802
125803
125804
125805
125806
125807
125808
125809
125810
125811
125812
125813
125814
125815
125816
125817
125818
125819
125820
125821
125822
125823
125824
125825
125826
125827
125828
125829
125830
125831
125832
125833
125834
125835
125836
125837
125838
125839
125840
125841
125842
125843
125844
125845
125846
125847
125848
125849
125850
125851
125852
125853
125854
125855
125856
125857
125858
125859
125860
125861
125862
125863
125864
125865
125866
125867
125868
125869
125870
125871
125872
125873
125874
125875
125876
125877
125878
125879
125880
125881
125882
125883
125884
125885
125886
125887
125888
125889
125890
125891
125892
125893
125894
125895
125896
125897
125898
125899
125900
125901
125902
125903
125904
125905
125906
125907
125908
125909
125910
125911
125912
125913
125914
125915
125916
125917
125918
125919
125920
125921
125922
125923
125924
125925
125926
125927
125928
125929
125930
125931
125932
125933
125934
125935
125936
125937
125938
125939
125940
125941
125942
125943
125944
125945
125946
125947
125948
125949
125950
125951
125952
125953
125954
125955
125956
125957
125958
125959
125960
125961
125962
125963
125964
125965
125966
125967
125968
125969
125970
125971
125972
125973
125974
125975
125976
125977
125978
125979
125980
125981
125982
125983
125984
125985
125986
125987
125988
125989
125990
125991
125992
125993
125994
125995
125996
125997
125998
125999
126000
126001
126002
126003
126004
126005
126006
126007
126008
126009
126010
126011
126012
126013
126014
126015
126016
126017
126018
126019
126020
126021
126022
126023
126024
126025
126026
126027
126028
126029
126030
126031
126032
126033
126034
126035
126036
126037
126038
126039
126040
126041
126042
126043
126044
126045
126046
126047
126048
126049
126050
126051
126052
126053
126054
126055
126056
126057
126058
126059
126060
126061
126062
126063
126064
126065
126066
126067
126068
126069
126070
126071
126072
126073
126074
126075
126076
126077
126078
126079
126080
126081
126082
126083
126084
126085
126086
126087
126088
126089
126090
126091
126092
126093
126094
126095
126096
126097
126098
126099
126100
126101
126102
126103
126104
126105
126106
126107
126108
126109
126110
126111
126112
126113
126114
126115
126116
126117
126118
126119
126120
126121
126122
126123
126124
126125
126126
126127
126128
126129
126130
126131
126132
126133
126134
126135
126136
126137
126138
126139
126140
126141
126142
126143
126144
126145
126146
126147
126148
126149
126150
126151
126152
126153
126154
126155
126156
126157
126158
126159
126160
126161
126162
126163
126164
126165
126166
126167
126168
126169
126170
126171
126172
126173
126174
126175
126176
126177
126178
126179
126180
126181
126182
126183
126184
126185
126186
126187
126188
126189
126190
126191
126192
126193
126194
126195
126196
126197
126198
126199
126200
126201
126202
126203
126204
126205
126206
126207
126208
126209
126210
126211
126212
126213
126214
126215
126216
126217
126218
126219
126220
126221
126222
126223
126224
126225
126226
126227
126228
126229
126230
126231
126232
126233
126234
126235
126236
126237
126238
126239
126240
126241
126242
126243
126244
126245
126246
126247
126248
126249
126250
126251
126252
126253
126254
126255
126256
126257
126258
126259
126260
126261
126262
126263
126264
126265
126266
126267
126268
126269
126270
126271
126272
126273
126274
126275
126276
126277
126278
126279
126280
126281
126282
126283
126284
126285
126286
126287
126288
126289
126290
126291
126292
126293
126294
126295
126296
126297
126298
126299
126300
126301
126302
126303
126304
126305
126306
126307
126308
126309
126310
126311
126312
126313
126314
126315
126316
126317
126318
126319
126320
126321
126322
126323
126324
126325
126326
126327
126328
126329
126330
126331
126332
126333
126334
126335
126336
126337
126338
126339
126340
126341
126342
126343
126344
126345
126346
126347
126348
126349
126350
126351
126352
126353
126354
126355
126356
126357
126358
126359
126360
126361
126362
126363
126364
126365
126366
126367
126368
126369
126370
126371
126372
126373
126374
126375
126376
126377
126378
126379
126380
126381
126382
126383
126384
126385
126386
126387
126388
126389
126390
126391
126392
126393
126394
126395
126396
126397
126398
126399
126400
126401
126402
126403
126404
126405
126406
126407
126408
126409
126410
126411
126412
126413
126414
126415
126416
126417
126418
126419
126420
126421
126422
126423
126424
126425
126426
126427
126428
126429
126430
126431
126432
126433
126434
126435
126436
126437
126438
126439
126440
126441
126442
126443
126444
126445
126446
126447
126448
126449
126450
126451
126452
126453
126454
126455
126456
126457
126458
126459
126460
126461
126462
126463
126464
126465
126466
126467
126468
126469
126470
126471
126472
126473
126474
126475
126476
126477
126478
126479
126480
126481
126482
126483
126484
126485
126486
126487
126488
126489
126490
126491
126492
126493
126494
126495
126496
126497
126498
126499
126500
126501
126502
126503
126504
126505
126506
126507
126508
126509
126510
126511
126512
126513
126514
126515
126516
126517
126518
126519
126520
126521
126522
126523
126524
126525
126526
126527
126528
126529
126530
126531
126532
126533
126534
126535
126536
126537
126538
126539
126540
126541
126542
126543
126544
126545
126546
126547
126548
126549
126550
126551
126552
126553
126554
126555
126556
126557
126558
126559
126560
126561
126562
126563
126564
126565
126566
126567
126568
126569
126570
126571
126572
126573
126574
126575
126576
126577
126578
126579
126580
126581
126582
126583
126584
126585
126586
126587
126588
126589
126590
126591
126592
126593
126594
126595
126596
126597
126598
126599
126600
126601
126602
126603
126604
126605
126606
126607
126608
126609
126610
126611
126612
126613
126614
126615
126616
126617
126618
126619
126620
126621
126622
126623
126624
126625
126626
126627
126628
126629
126630
126631
126632
126633
126634
126635
126636
126637
126638
126639
126640
126641
126642
126643
126644
126645
126646
126647
126648
126649
126650
126651
126652
126653
126654
126655
126656
126657
126658
126659
126660
126661
126662
126663
126664
126665
126666
126667
126668
126669
126670
126671
126672
126673
126674
126675
126676
126677
126678
126679
126680
126681
126682
126683
126684
126685
126686
126687
126688
126689
126690
126691
126692
126693
126694
126695
126696
126697
126698
126699
126700
126701
126702
126703
126704
126705
126706
126707
126708
126709
126710
126711
126712
126713
126714
126715
126716
126717
126718
126719
126720
126721
126722
126723
126724
126725
126726
126727
126728
126729
126730
126731
126732
126733
126734
126735
126736
126737
126738
126739
126740
126741
126742
126743
126744
126745
126746
126747
126748
126749
126750
126751
126752
126753
126754
126755
126756
126757
126758
126759
126760
126761
126762
126763
126764
126765
126766
126767
126768
126769
126770
126771
126772
126773
126774
126775
126776
126777
126778
126779
126780
126781
126782
126783
126784
126785
126786
126787
126788
126789
126790
126791
126792
126793
126794
126795
126796
126797
126798
126799
126800
126801
126802
126803
126804
126805
126806
126807
126808
126809
126810
126811
126812
126813
126814
126815
126816
126817
126818
126819
126820
126821
126822
126823
126824
126825
126826
126827
126828
126829
126830
126831
126832
126833
126834
126835
126836
126837
126838
126839
126840
126841
126842
126843
126844
126845
126846
126847
126848
126849
126850
126851
126852
126853
126854
126855
126856
126857
126858
126859
126860
126861
126862
126863
126864
126865
126866
126867
126868
126869
126870
126871
126872
126873
126874
126875
126876
126877
126878
126879
126880
126881
126882
126883
126884
126885
126886
126887
126888
126889
126890
126891
126892
126893
126894
126895
126896
126897
126898
126899
126900
126901
126902
126903
126904
126905
126906
126907
126908
126909
126910
126911
126912
126913
126914
126915
126916
126917
126918
126919
126920
126921
126922
126923
126924
126925
126926
126927
126928
126929
126930
126931
126932
126933
126934
126935
126936
126937
126938
126939
126940
126941
126942
126943
126944
126945
126946
126947
126948
126949
126950
126951
126952
126953
126954
126955
126956
126957
126958
126959
126960
126961
126962
126963
126964
126965
126966
126967
126968
126969
126970
126971
126972
126973
126974
126975
126976
126977
126978
126979
126980
126981
126982
126983
126984
126985
126986
126987
126988
126989
126990
126991
126992
126993
126994
126995
126996
126997
126998
126999
127000
127001
127002
127003
127004
127005
127006
127007
127008
127009
127010
127011
127012
127013
127014
127015
127016
127017
127018
127019
127020
127021
127022
127023
127024
127025
127026
127027
127028
127029
127030
127031
127032
127033
127034
127035
127036
127037
127038
127039
127040
127041
127042
127043
127044
127045
127046
127047
127048
127049
127050
127051
127052
127053
127054
127055
127056
127057
127058
127059
127060
127061
127062
127063
127064
127065
127066
127067
127068
127069
127070
127071
127072
127073
127074
127075
127076
127077
127078
127079
127080
127081
127082
127083
127084
127085
127086
127087
127088
127089
127090
127091
127092
127093
127094
127095
127096
127097
127098
127099
127100
127101
127102
127103
127104
127105
127106
127107
127108
127109
127110
127111
127112
127113
127114
127115
127116
127117
127118
127119
127120
127121
127122
127123
127124
127125
127126
127127
127128
127129
127130
127131
127132
127133
127134
127135
127136
127137
127138
127139
127140
127141
127142
127143
127144
127145
127146
127147
127148
127149
127150
127151
127152
127153
127154
127155
127156
127157
127158
127159
127160
127161
127162
127163
127164
127165
127166
127167
127168
127169
127170
127171
127172
127173
127174
127175
127176
127177
127178
127179
127180
127181
127182
127183
127184
127185
127186
127187
127188
127189
127190
127191
127192
127193
127194
127195
127196
127197
127198
127199
127200
127201
127202
127203
127204
127205
127206
127207
127208
127209
127210
127211
127212
127213
127214
127215
127216
127217
127218
127219
127220
127221
127222
127223
127224
127225
127226
127227
127228
127229
127230
127231
127232
127233
127234
127235
127236
127237
127238
127239
127240
127241
127242
127243
127244
127245
127246
127247
127248
127249
127250
127251
127252
127253
127254
127255
127256
127257
127258
127259
127260
127261
127262
127263
127264
127265
127266
127267
127268
127269
127270
127271
127272
127273
127274
127275
127276
127277
127278
127279
127280
127281
127282
127283
127284
127285
127286
127287
127288
127289
127290
127291
127292
127293
127294
127295
127296
127297
127298
127299
127300
127301
127302
127303
127304
127305
127306
127307
127308
127309
127310
127311
127312
127313
127314
127315
127316
127317
127318
127319
127320
127321
127322
127323
127324
127325
127326
127327
127328
127329
127330
127331
127332
127333
127334
127335
127336
127337
127338
127339
127340
127341
127342
127343
127344
127345
127346
127347
127348
127349
127350
127351
127352
127353
127354
127355
127356
127357
127358
127359
127360
127361
127362
127363
127364
127365
127366
127367
127368
127369
127370
127371
127372
127373
127374
127375
127376
127377
127378
127379
127380
127381
127382
127383
127384
127385
127386
127387
127388
127389
127390
127391
127392
127393
127394
127395
127396
127397
127398
127399
127400
127401
127402
127403
127404
127405
127406
127407
127408
127409
127410
127411
127412
127413
127414
127415
127416
127417
127418
127419
127420
127421
127422
127423
127424
127425
127426
127427
127428
127429
127430
127431
127432
127433
127434
127435
127436
127437
127438
127439
127440
127441
127442
127443
127444
127445
127446
127447
127448
127449
127450
127451
127452
127453
127454
127455
127456
127457
127458
127459
127460
127461
127462
127463
127464
127465
127466
127467
127468
127469
127470
127471
127472
127473
127474
127475
127476
127477
127478
127479
127480
127481
127482
127483
127484
127485
127486
127487
127488
127489
127490
127491
127492
127493
127494
127495
127496
127497
127498
127499
127500
127501
127502
127503
127504
127505
127506
127507
127508
127509
127510
127511
127512
127513
127514
127515
127516
127517
127518
127519
127520
127521
127522
127523
127524
127525
127526
127527
127528
127529
127530
127531
127532
127533
127534
127535
127536
127537
127538
127539
127540
127541
127542
127543
127544
127545
127546
127547
127548
127549
127550
127551
127552
127553
127554
127555
127556
127557
127558
127559
127560
127561
127562
127563
127564
127565
127566
127567
127568
127569
127570
127571
127572
127573
127574
127575
127576
127577
127578
127579
127580
127581
127582
127583
127584
127585
127586
127587
127588
127589
127590
127591
127592
127593
127594
127595
127596
127597
127598
127599
127600
127601
127602
127603
127604
127605
127606
127607
127608
127609
127610
127611
127612
127613
127614
127615
127616
127617
127618
127619
127620
127621
127622
127623
127624
127625
127626
127627
127628
127629
127630
127631
127632
127633
127634
127635
127636
127637
127638
127639
127640
127641
127642
127643
127644
127645
127646
127647
127648
127649
127650
127651
127652
127653
127654
127655
127656
127657
127658
127659
127660
127661
127662
127663
127664
127665
127666
127667
127668
127669
127670
127671
127672
127673
127674
127675
127676
127677
127678
127679
127680
127681
127682
127683
127684
127685
127686
127687
127688
127689
127690
127691
127692
127693
127694
127695
127696
127697
127698
127699
127700
127701
127702
127703
127704
127705
127706
127707
127708
127709
127710
127711
127712
127713
127714
127715
127716
127717
127718
127719
127720
127721
127722
127723
127724
127725
127726
127727
127728
127729
127730
127731
127732
127733
127734
127735
127736
127737
127738
127739
127740
127741
127742
127743
127744
127745
127746
127747
127748
127749
127750
127751
127752
127753
127754
127755
127756
127757
127758
127759
127760
127761
127762
127763
127764
127765
127766
127767
127768
127769
127770
127771
127772
127773
127774
127775
127776
127777
127778
127779
127780
127781
127782
127783
127784
127785
127786
127787
127788
127789
127790
127791
127792
127793
127794
127795
127796
127797
127798
127799
127800
127801
127802
127803
127804
127805
127806
127807
127808
127809
127810
127811
127812
127813
127814
127815
127816
127817
127818
127819
127820
127821
127822
127823
127824
127825
127826
127827
127828
127829
127830
127831
127832
127833
127834
127835
127836
127837
127838
127839
127840
127841
127842
127843
127844
127845
127846
127847
127848
127849
127850
127851
127852
127853
127854
127855
127856
127857
127858
127859
127860
127861
127862
127863
127864
127865
127866
127867
127868
127869
127870
127871
127872
127873
127874
127875
127876
127877
127878
127879
127880
127881
127882
127883
127884
127885
127886
127887
127888
127889
127890
127891
127892
127893
127894
127895
127896
127897
127898
127899
127900
127901
127902
127903
127904
127905
127906
127907
127908
127909
127910
127911
127912
127913
127914
127915
127916
127917
127918
127919
127920
127921
127922
127923
127924
127925
127926
127927
127928
127929
127930
127931
127932
127933
127934
127935
127936
127937
127938
127939
127940
127941
127942
127943
127944
127945
127946
127947
127948
127949
127950
127951
127952
127953
127954
127955
127956
127957
127958
127959
127960
127961
127962
127963
127964
127965
127966
127967
127968
127969
127970
127971
127972
127973
127974
127975
127976
127977
127978
127979
127980
127981
127982
127983
127984
127985
127986
127987
127988
127989
127990
127991
127992
127993
127994
127995
127996
127997
127998
127999
128000
128001
128002
128003
128004
128005
128006
128007
128008
128009
128010
128011
128012
128013
128014
128015
128016
128017
128018
128019
128020
128021
128022
128023
128024
128025
128026
128027
128028
128029
128030
128031
128032
128033
128034
128035
128036
128037
128038
128039
128040
128041
128042
128043
128044
128045
128046
128047
128048
128049
128050
128051
128052
128053
128054
128055
128056
128057
128058
128059
128060
128061
128062
128063
128064
128065
128066
128067
128068
128069
128070
128071
128072
128073
128074
128075
128076
128077
128078
128079
128080
128081
128082
128083
128084
128085
128086
128087
128088
128089
128090
128091
128092
128093
128094
128095
128096
128097
128098
128099
128100
128101
128102
128103
128104
128105
128106
128107
128108
128109
128110
128111
128112
128113
128114
128115
128116
128117
128118
128119
128120
128121
128122
128123
128124
128125
128126
128127
128128
128129
128130
128131
128132
128133
128134
128135
128136
128137
128138
128139
128140
128141
128142
128143
128144
128145
128146
128147
128148
128149
128150
128151
128152
128153
128154
128155
128156
128157
128158
128159
128160
128161
128162
128163
128164
128165
128166
128167
128168
128169
128170
128171
128172
128173
128174
128175
128176
128177
128178
128179
128180
128181
128182
128183
128184
128185
128186
128187
128188
128189
128190
128191
128192
128193
128194
128195
128196
128197
128198
128199
128200
128201
128202
128203
128204
128205
128206
128207
128208
128209
128210
128211
128212
128213
128214
128215
128216
128217
128218
128219
128220
128221
128222
128223
128224
128225
128226
128227
128228
128229
128230
128231
128232
128233
128234
128235
128236
128237
128238
128239
128240
128241
128242
128243
128244
128245
128246
128247
128248
128249
128250
128251
128252
128253
128254
128255
128256
128257
128258
128259
128260
128261
128262
128263
128264
128265
128266
128267
128268
128269
128270
128271
128272
128273
128274
128275
128276
128277
128278
128279
128280
128281
128282
128283
128284
128285
128286
128287
128288
128289
128290
128291
128292
128293
128294
128295
128296
128297
128298
128299
128300
128301
128302
128303
128304
128305
128306
128307
128308
128309
128310
128311
128312
128313
128314
128315
128316
128317
128318
128319
128320
128321
128322
128323
128324
128325
128326
128327
128328
128329
128330
128331
128332
128333
128334
128335
128336
128337
128338
128339
128340
128341
128342
128343
128344
128345
128346
128347
128348
128349
128350
128351
128352
128353
128354
128355
128356
128357
128358
128359
128360
128361
128362
128363
128364
128365
128366
128367
128368
128369
128370
128371
128372
128373
128374
128375
128376
128377
128378
128379
128380
128381
128382
128383
128384
128385
128386
128387
128388
128389
128390
128391
128392
128393
128394
128395
128396
128397
128398
128399
128400
128401
128402
128403
128404
128405
128406
128407
128408
128409
128410
128411
128412
128413
128414
128415
128416
128417
128418
128419
128420
128421
128422
128423
128424
128425
128426
128427
128428
128429
128430
128431
128432
128433
128434
128435
128436
128437
128438
128439
128440
128441
128442
128443
128444
128445
128446
128447
128448
128449
128450
128451
128452
128453
128454
128455
128456
128457
128458
128459
128460
128461
128462
128463
128464
128465
128466
128467
128468
128469
128470
128471
128472
128473
128474
128475
128476
128477
128478
128479
128480
128481
128482
128483
128484
128485
128486
128487
128488
128489
128490
128491
128492
128493
128494
128495
128496
128497
128498
128499
128500
128501
128502
128503
128504
128505
128506
128507
128508
128509
128510
128511
128512
128513
128514
128515
128516
128517
128518
128519
128520
128521
128522
128523
128524
128525
128526
128527
128528
128529
128530
128531
128532
128533
128534
128535
128536
128537
128538
128539
128540
128541
128542
128543
128544
128545
128546
128547
128548
128549
128550
128551
128552
128553
128554
128555
128556
128557
128558
128559
128560
128561
128562
128563
128564
128565
128566
128567
128568
128569
128570
128571
128572
128573
128574
128575
128576
128577
128578
128579
128580
128581
128582
128583
128584
128585
128586
128587
128588
128589
128590
128591
128592
128593
128594
128595
128596
128597
128598
128599
128600
128601
128602
128603
128604
128605
128606
128607
128608
128609
128610
128611
128612
128613
128614
128615
128616
128617
128618
128619
128620
128621
128622
128623
128624
128625
128626
128627
128628
128629
128630
128631
128632
128633
128634
128635
128636
128637
128638
128639
128640
128641
128642
128643
128644
128645
128646
128647
128648
128649
128650
128651
128652
128653
128654
128655
128656
128657
128658
128659
128660
128661
128662
128663
128664
128665
128666
128667
128668
128669
128670
128671
128672
128673
128674
128675
128676
128677
128678
128679
128680
128681
128682
128683
128684
128685
128686
128687
128688
128689
128690
128691
128692
128693
128694
128695
128696
128697
128698
128699
128700
128701
128702
128703
128704
128705
128706
128707
128708
128709
128710
128711
128712
128713
128714
128715
128716
128717
128718
128719
128720
128721
128722
128723
128724
128725
128726
128727
128728
128729
128730
128731
128732
128733
128734
128735
128736
128737
128738
128739
128740
128741
128742
128743
128744
128745
128746
128747
128748
128749
128750
128751
128752
128753
128754
128755
128756
128757
128758
128759
128760
128761
128762
128763
128764
128765
128766
128767
128768
128769
128770
128771
128772
128773
128774
128775
128776
128777
128778
128779
128780
128781
128782
128783
128784
128785
128786
128787
128788
128789
128790
128791
128792
128793
128794
128795
128796
128797
128798
128799
128800
128801
128802
128803
128804
128805
128806
128807
128808
128809
128810
128811
128812
128813
128814
128815
128816
128817
128818
128819
128820
128821
128822
128823
128824
128825
128826
128827
128828
128829
128830
128831
128832
128833
128834
128835
128836
128837
128838
128839
128840
128841
128842
128843
128844
128845
128846
128847
128848
128849
128850
128851
128852
128853
128854
128855
128856
128857
128858
128859
128860
128861
128862
128863
128864
128865
128866
128867
128868
128869
128870
128871
128872
128873
128874
128875
128876
128877
128878
128879
128880
128881
128882
128883
128884
128885
128886
128887
128888
128889
128890
128891
128892
128893
128894
128895
128896
128897
128898
128899
128900
128901
128902
128903
128904
128905
128906
128907
128908
128909
128910
128911
128912
128913
128914
128915
128916
128917
128918
128919
128920
128921
128922
128923
128924
128925
128926
128927
128928
128929
128930
128931
128932
128933
128934
128935
128936
128937
128938
128939
128940
128941
128942
128943
128944
128945
128946
128947
128948
128949
128950
128951
128952
128953
128954
128955
128956
128957
128958
128959
128960
128961
128962
128963
128964
128965
128966
128967
128968
128969
128970
128971
128972
128973
128974
128975
128976
128977
128978
128979
128980
128981
128982
128983
128984
128985
128986
128987
128988
128989
128990
128991
128992
128993
128994
128995
128996
128997
128998
128999
129000
129001
129002
129003
129004
129005
129006
129007
129008
129009
129010
129011
129012
129013
129014
129015
129016
129017
129018
129019
129020
129021
129022
129023
129024
129025
129026
129027
129028
129029
129030
129031
129032
129033
129034
129035
129036
129037
129038
129039
129040
129041
129042
129043
129044
129045
129046
129047
129048
129049
129050
129051
129052
129053
129054
129055
129056
129057
129058
129059
129060
129061
129062
129063
129064
129065
129066
129067
129068
129069
129070
129071
129072
129073
129074
129075
129076
129077
129078
129079
129080
129081
129082
129083
129084
129085
129086
129087
129088
129089
129090
129091
129092
129093
129094
129095
129096
129097
129098
129099
129100
129101
129102
129103
129104
129105
129106
129107
129108
129109
129110
129111
129112
129113
129114
129115
129116
129117
129118
129119
129120
129121
129122
129123
129124
129125
129126
129127
129128
129129
129130
129131
129132
129133
129134
129135
129136
129137
129138
129139
129140
129141
129142
129143
129144
129145
129146
129147
129148
129149
129150
129151
129152
129153
129154
129155
129156
129157
129158
129159
129160
129161
129162
129163
129164
129165
129166
129167
129168
129169
129170
129171
129172
129173
129174
129175
129176
129177
129178
129179
129180
129181
129182
129183
129184
129185
129186
129187
129188
129189
129190
129191
129192
129193
129194
129195
129196
129197
129198
129199
129200
129201
129202
129203
129204
129205
129206
129207
129208
129209
129210
129211
129212
129213
129214
129215
129216
129217
129218
129219
129220
129221
129222
129223
129224
129225
129226
129227
129228
129229
129230
129231
129232
129233
129234
129235
129236
129237
129238
129239
129240
129241
129242
129243
129244
129245
129246
129247
129248
129249
129250
129251
129252
129253
129254
129255
129256
129257
129258
129259
129260
129261
129262
129263
129264
129265
129266
129267
129268
129269
129270
129271
129272
129273
129274
129275
129276
129277
129278
129279
129280
129281
129282
129283
129284
129285
129286
129287
129288
129289
129290
129291
129292
129293
129294
129295
129296
129297
129298
129299
129300
129301
129302
129303
129304
129305
129306
129307
129308
129309
129310
129311
129312
129313
129314
129315
129316
129317
129318
129319
129320
129321
129322
129323
129324
129325
129326
129327
129328
129329
129330
129331
129332
129333
129334
129335
129336
129337
129338
129339
129340
129341
129342
129343
129344
129345
129346
129347
129348
129349
129350
129351
129352
129353
129354
129355
129356
129357
129358
129359
129360
129361
129362
129363
129364
129365
129366
129367
129368
129369
129370
129371
129372
129373
129374
129375
129376
129377
129378
129379
129380
129381
129382
129383
129384
129385
129386
129387
129388
129389
129390
129391
129392
129393
129394
129395
129396
129397
129398
129399
129400
129401
129402
129403
129404
129405
129406
129407
129408
129409
129410
129411
129412
129413
129414
129415
129416
129417
129418
129419
129420
129421
129422
129423
129424
129425
129426
129427
129428
129429
129430
129431
129432
129433
129434
129435
129436
129437
129438
129439
129440
129441
129442
129443
129444
129445
129446
129447
129448
129449
129450
129451
129452
129453
129454
129455
129456
129457
129458
129459
129460
129461
129462
129463
129464
129465
129466
129467
129468
129469
129470
129471
129472
129473
129474
129475
129476
129477
129478
129479
129480
129481
129482
129483
129484
129485
129486
129487
129488
129489
129490
129491
129492
129493
129494
129495
129496
129497
129498
129499
129500
129501
129502
129503
129504
129505
129506
129507
129508
129509
129510
129511
129512
129513
129514
129515
129516
129517
129518
129519
129520
129521
129522
129523
129524
129525
129526
129527
129528
129529
129530
129531
129532
129533
129534
129535
129536
129537
129538
129539
129540
129541
129542
129543
129544
129545
129546
129547
129548
129549
129550
129551
129552
129553
129554
129555
129556
129557
129558
129559
129560
129561
129562
129563
129564
129565
129566
129567
129568
129569
129570
129571
129572
129573
129574
129575
129576
129577
129578
129579
129580
129581
129582
129583
129584
129585
129586
129587
129588
129589
129590
129591
129592
129593
129594
129595
129596
129597
129598
129599
129600
129601
129602
129603
129604
129605
129606
129607
129608
129609
129610
129611
129612
129613
129614
129615
129616
129617
129618
129619
129620
129621
129622
129623
129624
129625
129626
129627
129628
129629
129630
129631
129632
129633
129634
129635
129636
129637
129638
129639
129640
129641
129642
129643
129644
129645
129646
129647
129648
129649
129650
129651
129652
129653
129654
129655
129656
129657
129658
129659
129660
129661
129662
129663
129664
129665
129666
129667
129668
129669
129670
129671
129672
129673
129674
129675
129676
129677
129678
129679
129680
129681
129682
129683
129684
129685
129686
129687
129688
129689
129690
129691
129692
129693
129694
129695
129696
129697
129698
129699
129700
129701
129702
129703
129704
129705
129706
129707
129708
129709
129710
129711
129712
129713
129714
129715
129716
129717
129718
129719
129720
129721
129722
129723
129724
129725
129726
129727
129728
129729
129730
129731
129732
129733
129734
129735
129736
129737
129738
129739
129740
129741
129742
129743
129744
129745
129746
129747
129748
129749
129750
129751
129752
129753
129754
129755
129756
129757
129758
129759
129760
129761
129762
129763
129764
129765
129766
129767
129768
129769
129770
129771
129772
129773
129774
129775
129776
129777
129778
129779
129780
129781
129782
129783
129784
129785
129786
129787
129788
129789
129790
129791
129792
129793
129794
129795
129796
129797
129798
129799
129800
129801
129802
129803
129804
129805
129806
129807
129808
129809
129810
129811
129812
129813
129814
129815
129816
129817
129818
129819
129820
129821
129822
129823
129824
129825
129826
129827
129828
129829
129830
129831
129832
129833
129834
129835
129836
129837
129838
129839
129840
129841
129842
129843
129844
129845
129846
129847
129848
129849
129850
129851
129852
129853
129854
129855
129856
129857
129858
129859
129860
129861
129862
129863
129864
129865
129866
129867
129868
129869
129870
129871
129872
129873
129874
129875
129876
129877
129878
129879
129880
129881
129882
129883
129884
129885
129886
129887
129888
129889
129890
129891
129892
129893
129894
129895
129896
129897
129898
129899
129900
129901
129902
129903
129904
129905
129906
129907
129908
129909
129910
129911
129912
129913
129914
129915
129916
129917
129918
129919
129920
129921
129922
129923
129924
129925
129926
129927
129928
129929
129930
129931
129932
129933
129934
129935
129936
129937
129938
129939
129940
129941
129942
129943
129944
129945
129946
129947
129948
129949
129950
129951
129952
129953
129954
129955
129956
129957
129958
129959
129960
129961
129962
129963
129964
129965
129966
129967
129968
129969
129970
129971
129972
129973
129974
129975
129976
129977
129978
129979
129980
129981
129982
129983
129984
129985
129986
129987
129988
129989
129990
129991
129992
129993
129994
129995
129996
129997
129998
129999
130000
130001
130002
130003
130004
130005
130006
130007
130008
130009
130010
130011
130012
130013
130014
130015
130016
130017
130018
130019
130020
130021
130022
130023
130024
130025
130026
130027
130028
130029
130030
130031
130032
130033
130034
130035
130036
130037
130038
130039
130040
130041
130042
130043
130044
130045
130046
130047
130048
130049
130050
130051
130052
130053
130054
130055
130056
130057
130058
130059
130060
130061
130062
130063
130064
130065
130066
130067
130068
130069
130070
130071
130072
130073
130074
130075
130076
130077
130078
130079
130080
130081
130082
130083
130084
130085
130086
130087
130088
130089
130090
130091
130092
130093
130094
130095
130096
130097
130098
130099
130100
130101
130102
130103
130104
130105
130106
130107
130108
130109
130110
130111
130112
130113
130114
130115
130116
130117
130118
130119
130120
130121
130122
130123
130124
130125
130126
130127
130128
130129
130130
130131
130132
130133
130134
130135
130136
130137
130138
130139
130140
130141
130142
130143
130144
130145
130146
130147
130148
130149
130150
130151
130152
130153
130154
130155
130156
130157
130158
130159
130160
130161
130162
130163
130164
130165
130166
130167
130168
130169
130170
130171
130172
130173
130174
130175
130176
130177
130178
130179
130180
130181
130182
130183
130184
130185
130186
130187
130188
130189
130190
130191
130192
130193
130194
130195
130196
130197
130198
130199
130200
130201
130202
130203
130204
130205
130206
130207
130208
130209
130210
130211
130212
130213
130214
130215
130216
130217
130218
130219
130220
130221
130222
130223
130224
130225
130226
130227
130228
130229
130230
130231
130232
130233
130234
130235
130236
130237
130238
130239
130240
130241
130242
130243
130244
130245
130246
130247
130248
130249
130250
130251
130252
130253
130254
130255
130256
130257
130258
130259
130260
130261
130262
130263
130264
130265
130266
130267
130268
130269
130270
130271
130272
130273
130274
130275
130276
130277
130278
130279
130280
130281
130282
130283
130284
130285
130286
130287
130288
130289
130290
130291
130292
130293
130294
130295
130296
130297
130298
130299
130300
130301
130302
130303
130304
130305
130306
130307
130308
130309
130310
130311
130312
130313
130314
130315
130316
130317
130318
130319
130320
130321
130322
130323
130324
130325
130326
130327
130328
130329
130330
130331
130332
130333
130334
130335
130336
130337
130338
130339
130340
130341
130342
130343
130344
130345
130346
130347
130348
130349
130350
130351
130352
130353
130354
130355
130356
130357
130358
130359
130360
130361
130362
130363
130364
130365
130366
130367
130368
130369
130370
130371
130372
130373
130374
130375
130376
130377
130378
130379
130380
130381
130382
130383
130384
130385
130386
130387
130388
130389
130390
130391
130392
130393
130394
130395
130396
130397
130398
130399
130400
130401
130402
130403
130404
130405
130406
130407
130408
130409
130410
130411
130412
130413
130414
130415
130416
130417
130418
130419
130420
130421
130422
130423
130424
130425
130426
130427
130428
130429
130430
130431
130432
130433
130434
130435
130436
130437
130438
130439
130440
130441
130442
130443
130444
130445
130446
130447
130448
130449
130450
130451
130452
130453
130454
130455
130456
130457
130458
130459
130460
130461
130462
130463
130464
130465
130466
130467
130468
130469
130470
130471
130472
130473
130474
130475
130476
130477
130478
130479
130480
130481
130482
130483
130484
130485
130486
130487
130488
130489
130490
130491
130492
130493
130494
130495
130496
130497
130498
130499
130500
130501
130502
130503
130504
130505
130506
130507
130508
130509
130510
130511
130512
130513
130514
130515
130516
130517
130518
130519
130520
130521
130522
130523
130524
130525
130526
130527
130528
130529
130530
130531
130532
130533
130534
130535
130536
130537
130538
130539
130540
130541
130542
130543
130544
130545
130546
130547
130548
130549
130550
130551
130552
130553
130554
130555
130556
130557
130558
130559
130560
130561
130562
130563
130564
130565
130566
130567
130568
130569
130570
130571
130572
130573
130574
130575
130576
130577
130578
130579
130580
130581
130582
130583
130584
130585
130586
130587
130588
130589
130590
130591
130592
130593
130594
130595
130596
130597
130598
130599
130600
130601
130602
130603
130604
130605
130606
130607
130608
130609
130610
130611
130612
130613
130614
130615
130616
130617
130618
130619
130620
130621
130622
130623
130624
130625
130626
130627
130628
130629
130630
130631
130632
130633
130634
130635
130636
130637
130638
130639
130640
130641
130642
130643
130644
130645
130646
130647
130648
130649
130650
130651
130652
130653
130654
130655
130656
130657
130658
130659
130660
130661
130662
130663
130664
130665
130666
130667
130668
130669
130670
130671
130672
130673
130674
130675
130676
130677
130678
130679
130680
130681
130682
130683
130684
130685
130686
130687
130688
130689
130690
130691
130692
130693
130694
130695
130696
130697
130698
130699
130700
130701
130702
130703
130704
130705
130706
130707
130708
130709
130710
130711
130712
130713
130714
130715
130716
130717
130718
130719
130720
130721
130722
130723
130724
130725
130726
130727
130728
130729
130730
130731
130732
130733
130734
130735
130736
130737
130738
130739
130740
130741
130742
130743
130744
130745
130746
130747
130748
130749
130750
130751
130752
130753
130754
130755
130756
130757
130758
130759
130760
130761
130762
130763
130764
130765
130766
130767
130768
130769
130770
130771
130772
130773
130774
130775
130776
130777
130778
130779
130780
130781
130782
130783
130784
130785
130786
130787
130788
130789
130790
130791
130792
130793
130794
130795
130796
130797
130798
130799
130800
130801
130802
130803
130804
130805
130806
130807
130808
130809
130810
130811
130812
130813
130814
130815
130816
130817
130818
130819
130820
130821
130822
130823
130824
130825
130826
130827
130828
130829
130830
130831
130832
130833
130834
130835
130836
130837
130838
130839
130840
130841
130842
130843
130844
130845
130846
130847
130848
130849
130850
130851
130852
130853
130854
130855
130856
130857
130858
130859
130860
130861
130862
130863
130864
130865
130866
130867
130868
130869
130870
130871
130872
130873
130874
130875
130876
130877
130878
130879
130880
130881
130882
130883
130884
130885
130886
130887
130888
130889
130890
130891
130892
130893
130894
130895
130896
130897
130898
130899
130900
130901
130902
130903
130904
130905
130906
130907
130908
130909
130910
130911
130912
130913
130914
130915
130916
130917
130918
130919
130920
130921
130922
130923
130924
130925
130926
130927
130928
130929
130930
130931
130932
130933
130934
130935
130936
130937
130938
130939
130940
130941
130942
130943
130944
130945
130946
130947
130948
130949
130950
130951
130952
130953
130954
130955
130956
130957
130958
130959
130960
130961
130962
130963
130964
130965
130966
130967
130968
130969
130970
130971
130972
130973
130974
130975
130976
130977
130978
130979
130980
130981
130982
130983
130984
130985
130986
130987
130988
130989
130990
130991
130992
130993
130994
130995
130996
130997
130998
130999
131000
131001
131002
131003
131004
131005
131006
131007
131008
131009
131010
131011
131012
131013
131014
131015
131016
131017
131018
131019
131020
131021
131022
131023
131024
131025
131026
131027
131028
131029
131030
131031
131032
131033
131034
131035
131036
131037
131038
131039
131040
131041
131042
131043
131044
131045
131046
131047
131048
131049
131050
131051
131052
131053
131054
131055
131056
131057
131058
131059
131060
131061
131062
131063
131064
131065
131066
131067
131068
131069
131070
131071
131072
131073
131074
131075
131076
131077
131078
131079
131080
131081
131082
131083
131084
131085
131086
131087
131088
131089
131090
131091
131092
131093
131094
131095
131096
131097
131098
131099
131100
131101
131102
131103
131104
131105
131106
131107
131108
131109
131110
131111
131112
131113
131114
131115
131116
131117
131118
131119
131120
131121
131122
131123
131124
131125
131126
131127
131128
131129
131130
131131
131132
131133
131134
131135
131136
131137
131138
131139
131140
131141
131142
131143
131144
131145
131146
131147
131148
131149
131150
131151
131152
131153
131154
131155
131156
131157
131158
131159
131160
131161
131162
131163
131164
131165
131166
131167
131168
131169
131170
131171
131172
131173
131174
131175
131176
131177
131178
131179
131180
131181
131182
131183
131184
131185
131186
131187
131188
131189
131190
131191
131192
131193
131194
131195
131196
131197
131198
131199
131200
131201
131202
131203
131204
131205
131206
131207
131208
131209
131210
131211
131212
131213
131214
131215
131216
131217
131218
131219
131220
131221
131222
131223
131224
131225
131226
131227
131228
131229
131230
131231
131232
131233
131234
131235
131236
131237
131238
131239
131240
131241
131242
131243
131244
131245
131246
131247
131248
131249
131250
131251
131252
131253
131254
131255
131256
131257
131258
131259
131260
131261
131262
131263
131264
131265
131266
131267
131268
131269
131270
131271
131272
131273
131274
131275
131276
131277
131278
131279
131280
131281
131282
131283
131284
131285
131286
131287
131288
131289
131290
131291
131292
131293
131294
131295
131296
131297
131298
131299
131300
131301
131302
131303
131304
131305
131306
131307
131308
131309
131310
131311
131312
131313
131314
131315
131316
131317
131318
131319
131320
131321
131322
131323
131324
131325
131326
131327
131328
131329
131330
131331
131332
131333
131334
131335
131336
131337
131338
131339
131340
131341
131342
131343
131344
131345
131346
131347
131348
131349
131350
131351
131352
131353
131354
131355
131356
131357
131358
131359
131360
131361
131362
131363
131364
131365
131366
131367
131368
131369
131370
131371
131372
131373
131374
131375
131376
131377
131378
131379
131380
131381
131382
131383
131384
131385
131386
131387
131388
131389
131390
131391
131392
131393
131394
131395
131396
131397
131398
131399
131400
131401
131402
131403
131404
131405
131406
131407
131408
131409
131410
131411
131412
131413
131414
131415
131416
131417
131418
131419
131420
131421
131422
131423
131424
131425
131426
131427
131428
131429
131430
131431
131432
131433
131434
131435
131436
131437
131438
131439
131440
131441
131442
131443
131444
131445
131446
131447
131448
131449
131450
131451
131452
131453
131454
131455
131456
131457
131458
131459
131460
131461
131462
131463
131464
131465
131466
131467
131468
131469
131470
131471
131472
131473
131474
131475
131476
131477
131478
131479
131480
131481
131482
131483
131484
131485
131486
131487
131488
131489
131490
131491
131492
131493
131494
131495
131496
131497
131498
131499
131500
131501
131502
131503
131504
131505
131506
131507
131508
131509
131510
131511
131512
131513
131514
131515
131516
131517
131518
131519
131520
131521
131522
131523
131524
131525
131526
131527
131528
131529
131530
131531
131532
131533
131534
131535
131536
131537
131538
131539
131540
131541
131542
131543
131544
131545
131546
131547
131548
131549
131550
131551
131552
131553
131554
131555
131556
131557
131558
131559
131560
131561
131562
131563
131564
131565
131566
131567
131568
131569
131570
131571
131572
131573
131574
131575
131576
131577
131578
131579
131580
131581
131582
131583
131584
131585
131586
131587
131588
131589
131590
131591
131592
131593
131594
131595
131596
131597
131598
131599
131600
131601
131602
131603
131604
131605
131606
131607
131608
131609
131610
131611
131612
131613
131614
131615
131616
131617
131618
131619
131620
131621
131622
131623
131624
131625
131626
131627
131628
131629
131630
131631
131632
131633
131634
131635
131636
131637
131638
131639
131640
131641
131642
131643
131644
131645
131646
131647
131648
131649
131650
131651
131652
131653
131654
131655
131656
131657
131658
131659
131660
131661
131662
131663
131664
131665
131666
131667
131668
131669
131670
131671
131672
131673
131674
131675
131676
131677
131678
131679
131680
131681
131682
131683
131684
131685
131686
131687
131688
131689
131690
131691
131692
131693
131694
131695
131696
131697
131698
131699
131700
131701
131702
131703
131704
131705
131706
131707
131708
131709
131710
131711
131712
131713
131714
131715
131716
131717
131718
131719
131720
131721
131722
131723
131724
131725
131726
131727
131728
131729
131730
131731
131732
131733
131734
131735
131736
131737
131738
131739
131740
131741
131742
131743
131744
131745
131746
131747
131748
131749
131750
131751
131752
131753
131754
131755
131756
131757
131758
131759
131760
131761
131762
131763
131764
131765
131766
131767
131768
131769
131770
131771
131772
131773
131774
131775
131776
131777
131778
131779
131780
131781
131782
131783
131784
131785
131786
131787
131788
131789
131790
131791
131792
131793
131794
131795
131796
131797
131798
131799
131800
131801
131802
131803
131804
131805
131806
131807
131808
131809
131810
131811
131812
131813
131814
131815
131816
131817
131818
131819
131820
131821
131822
131823
131824
131825
131826
131827
131828
131829
131830
131831
131832
131833
131834
131835
131836
131837
131838
131839
131840
131841
131842
131843
131844
131845
131846
131847
131848
131849
131850
131851
131852
131853
131854
131855
131856
131857
131858
131859
131860
131861
131862
131863
131864
131865
131866
131867
131868
131869
131870
131871
131872
131873
131874
131875
131876
131877
131878
131879
131880
131881
131882
131883
131884
131885
131886
131887
131888
131889
131890
131891
131892
131893
131894
131895
131896
131897
131898
131899
131900
131901
131902
131903
131904
131905
131906
131907
131908
131909
131910
131911
131912
131913
131914
131915
131916
131917
131918
131919
131920
131921
131922
131923
131924
131925
131926
131927
131928
131929
131930
131931
131932
131933
131934
131935
131936
131937
131938
131939
131940
131941
131942
131943
131944
131945
131946
131947
131948
131949
131950
131951
131952
131953
131954
131955
131956
131957
131958
131959
131960
131961
131962
131963
131964
131965
131966
131967
131968
131969
131970
131971
131972
131973
131974
131975
131976
131977
131978
131979
131980
131981
131982
131983
131984
131985
131986
131987
131988
131989
131990
131991
131992
131993
131994
131995
131996
131997
131998
131999
132000
132001
132002
132003
132004
132005
132006
132007
132008
132009
132010
132011
132012
132013
132014
132015
132016
132017
132018
132019
132020
132021
132022
132023
132024
132025
132026
132027
132028
132029
132030
132031
132032
132033
132034
132035
132036
132037
132038
132039
132040
132041
132042
132043
132044
132045
132046
132047
132048
132049
132050
132051
132052
132053
132054
132055
132056
132057
132058
132059
132060
132061
132062
132063
132064
132065
132066
132067
132068
132069
132070
132071
132072
132073
132074
132075
132076
132077
132078
132079
132080
132081
132082
132083
132084
132085
132086
132087
132088
132089
132090
132091
132092
132093
132094
132095
132096
132097
132098
132099
132100
132101
132102
132103
132104
132105
132106
132107
132108
132109
132110
132111
132112
132113
132114
132115
132116
132117
132118
132119
132120
132121
132122
132123
132124
132125
132126
132127
132128
132129
132130
132131
132132
132133
132134
132135
132136
132137
132138
132139
132140
132141
132142
132143
132144
132145
132146
132147
132148
132149
132150
132151
132152
132153
132154
132155
132156
132157
132158
132159
132160
132161
132162
132163
132164
132165
132166
132167
132168
132169
132170
132171
132172
132173
132174
132175
132176
132177
132178
132179
132180
132181
132182
132183
132184
132185
132186
132187
132188
132189
132190
132191
132192
132193
132194
132195
132196
132197
132198
132199
132200
132201
132202
132203
132204
132205
132206
132207
132208
132209
132210
132211
132212
132213
132214
132215
132216
132217
132218
132219
132220
132221
132222
132223
132224
132225
132226
132227
132228
132229
132230
132231
132232
132233
132234
132235
132236
132237
132238
132239
132240
132241
132242
132243
132244
132245
132246
132247
132248
132249
132250
132251
132252
132253
132254
132255
132256
132257
132258
132259
132260
132261
132262
132263
132264
132265
132266
132267
132268
132269
132270
132271
132272
132273
132274
132275
132276
132277
132278
132279
132280
132281
132282
132283
132284
132285
132286
132287
132288
132289
132290
132291
132292
132293
132294
132295
132296
132297
132298
132299
132300
132301
132302
132303
132304
132305
132306
132307
132308
132309
132310
132311
132312
132313
132314
132315
132316
132317
132318
132319
132320
132321
132322
132323
132324
132325
132326
132327
132328
132329
132330
132331
132332
132333
132334
132335
132336
132337
132338
132339
132340
132341
132342
132343
132344
132345
132346
132347
132348
132349
132350
132351
132352
132353
132354
132355
132356
132357
132358
132359
132360
132361
132362
132363
132364
132365
132366
132367
132368
132369
132370
132371
132372
132373
132374
132375
132376
132377
132378
132379
132380
132381
132382
132383
132384
132385
132386
132387
132388
132389
132390
132391
132392
132393
132394
132395
132396
132397
132398
132399
132400
132401
132402
132403
132404
132405
132406
132407
132408
132409
132410
132411
132412
132413
132414
132415
132416
132417
132418
132419
132420
132421
132422
132423
132424
132425
132426
132427
132428
132429
132430
132431
132432
132433
132434
132435
132436
132437
132438
132439
132440
132441
132442
132443
132444
132445
132446
132447
132448
132449
132450
132451
132452
132453
132454
132455
132456
132457
132458
132459
132460
132461
132462
132463
132464
132465
132466
132467
132468
132469
132470
132471
132472
132473
132474
132475
132476
132477
132478
132479
132480
132481
132482
132483
132484
132485
132486
132487
132488
132489
132490
132491
132492
132493
132494
132495
132496
132497
132498
132499
132500
132501
132502
132503
132504
132505
132506
132507
132508
132509
132510
132511
132512
132513
132514
132515
132516
132517
132518
132519
132520
132521
132522
132523
132524
132525
132526
132527
132528
132529
132530
132531
132532
132533
132534
132535
132536
132537
132538
132539
132540
132541
132542
132543
132544
132545
132546
132547
132548
132549
132550
132551
132552
132553
132554
132555
132556
132557
132558
132559
132560
132561
132562
132563
132564
132565
132566
132567
132568
132569
132570
132571
132572
132573
132574
132575
132576
132577
132578
132579
132580
132581
132582
132583
132584
132585
132586
132587
132588
132589
132590
132591
132592
132593
132594
132595
132596
132597
132598
132599
132600
132601
132602
132603
132604
132605
132606
132607
132608
132609
132610
132611
132612
132613
132614
132615
132616
132617
132618
132619
132620
132621
132622
132623
132624
132625
132626
132627
132628
132629
132630
132631
132632
132633
132634
132635
132636
132637
132638
132639
132640
132641
132642
132643
132644
132645
132646
132647
132648
132649
132650
132651
132652
132653
132654
132655
132656
132657
132658
132659
132660
132661
132662
132663
132664
132665
132666
132667
132668
132669
132670
132671
132672
132673
132674
132675
132676
132677
132678
132679
132680
132681
132682
132683
132684
132685
132686
132687
132688
132689
132690
132691
132692
132693
132694
132695
132696
132697
132698
132699
132700
132701
132702
132703
132704
132705
132706
132707
132708
132709
132710
132711
132712
132713
132714
132715
132716
132717
132718
132719
132720
132721
132722
132723
132724
132725
132726
132727
132728
132729
132730
132731
132732
132733
132734
132735
132736
132737
132738
132739
132740
132741
132742
132743
132744
132745
132746
132747
132748
132749
132750
132751
132752
132753
132754
132755
132756
132757
132758
132759
132760
132761
132762
132763
132764
132765
132766
132767
132768
132769
132770
132771
132772
132773
132774
132775
132776
132777
132778
132779
132780
132781
132782
132783
132784
132785
132786
132787
132788
132789
132790
132791
132792
132793
132794
132795
132796
132797
132798
132799
132800
132801
132802
132803
132804
132805
132806
132807
132808
132809
132810
132811
132812
132813
132814
132815
132816
132817
132818
132819
132820
132821
132822
132823
132824
132825
132826
132827
132828
132829
132830
132831
132832
132833
132834
132835
132836
132837
132838
132839
132840
132841
132842
132843
132844
132845
132846
132847
132848
132849
132850
132851
132852
132853
132854
132855
132856
132857
132858
132859
132860
132861
132862
132863
132864
132865
132866
132867
132868
132869
132870
132871
132872
132873
132874
132875
132876
132877
132878
132879
132880
132881
132882
132883
132884
132885
132886
132887
132888
132889
132890
132891
132892
132893
132894
132895
132896
132897
132898
132899
132900
132901
132902
132903
132904
132905
132906
132907
132908
132909
132910
132911
132912
132913
132914
132915
132916
132917
132918
132919
132920
132921
132922
132923
132924
132925
132926
132927
132928
132929
132930
132931
132932
132933
132934
132935
132936
132937
132938
132939
132940
132941
132942
132943
132944
132945
132946
132947
132948
132949
132950
132951
132952
132953
132954
132955
132956
132957
132958
132959
132960
132961
132962
132963
132964
132965
132966
132967
132968
132969
132970
132971
132972
132973
132974
132975
132976
132977
132978
132979
132980
132981
132982
132983
132984
132985
132986
132987
132988
132989
132990
132991
132992
132993
132994
132995
132996
132997
132998
132999
133000
133001
133002
133003
133004
133005
133006
133007
133008
133009
133010
133011
133012
133013
133014
133015
133016
133017
133018
133019
133020
133021
133022
133023
133024
133025
133026
133027
133028
133029
133030
133031
133032
133033
133034
133035
133036
133037
133038
133039
133040
133041
133042
133043
133044
133045
133046
133047
133048
133049
133050
133051
133052
133053
133054
133055
133056
133057
133058
133059
133060
133061
133062
133063
133064
133065
133066
133067
133068
133069
133070
133071
133072
133073
133074
133075
133076
133077
133078
133079
133080
133081
133082
133083
133084
133085
133086
133087
133088
133089
133090
133091
133092
133093
133094
133095
133096
133097
133098
133099
133100
133101
133102
133103
133104
133105
133106
133107
133108
133109
133110
133111
133112
133113
133114
133115
133116
133117
133118
133119
133120
133121
133122
133123
133124
133125
133126
133127
133128
133129
133130
133131
133132
133133
133134
133135
133136
133137
133138
133139
133140
133141
133142
133143
133144
133145
133146
133147
133148
133149
133150
133151
133152
133153
133154
133155
133156
133157
133158
133159
133160
133161
133162
133163
133164
133165
133166
133167
133168
133169
133170
133171
133172
133173
133174
133175
133176
133177
133178
133179
133180
133181
133182
133183
133184
133185
133186
133187
133188
133189
133190
133191
133192
133193
133194
133195
133196
133197
133198
133199
133200
133201
133202
133203
133204
133205
133206
133207
133208
133209
133210
133211
133212
133213
133214
133215
133216
133217
133218
133219
133220
133221
133222
133223
133224
133225
133226
133227
133228
133229
133230
133231
133232
133233
133234
133235
133236
133237
133238
133239
133240
133241
133242
133243
133244
133245
133246
133247
133248
133249
133250
133251
133252
133253
133254
133255
133256
133257
133258
133259
133260
133261
133262
133263
133264
133265
133266
133267
133268
133269
133270
133271
133272
133273
133274
133275
133276
133277
133278
133279
133280
133281
133282
133283
133284
133285
133286
133287
133288
133289
133290
133291
133292
133293
133294
133295
133296
133297
133298
133299
133300
133301
133302
133303
133304
133305
133306
133307
133308
133309
133310
133311
133312
133313
133314
133315
133316
133317
133318
133319
133320
133321
133322
133323
133324
133325
133326
133327
133328
133329
133330
133331
133332
133333
133334
133335
133336
133337
133338
133339
133340
133341
133342
133343
133344
133345
133346
133347
133348
133349
133350
133351
133352
133353
133354
133355
133356
133357
133358
133359
133360
133361
133362
133363
133364
133365
133366
133367
133368
133369
133370
133371
133372
133373
133374
133375
133376
133377
133378
133379
133380
133381
133382
133383
133384
133385
133386
133387
133388
133389
133390
133391
133392
133393
133394
133395
133396
133397
133398
133399
133400
133401
133402
133403
133404
133405
133406
133407
133408
133409
133410
133411
133412
133413
133414
133415
133416
133417
133418
133419
133420
133421
133422
133423
133424
133425
133426
133427
133428
133429
133430
133431
133432
133433
133434
133435
133436
133437
133438
133439
133440
133441
133442
133443
133444
133445
133446
133447
133448
133449
133450
133451
133452
133453
133454
133455
133456
133457
133458
133459
133460
133461
133462
133463
133464
133465
133466
133467
133468
133469
133470
133471
133472
133473
133474
133475
133476
133477
133478
133479
133480
133481
133482
133483
133484
133485
133486
133487
133488
133489
133490
133491
133492
133493
133494
133495
133496
133497
133498
133499
133500
133501
133502
133503
133504
133505
133506
133507
133508
133509
133510
133511
133512
133513
133514
133515
133516
133517
133518
133519
133520
133521
133522
133523
133524
133525
133526
133527
133528
133529
133530
133531
133532
133533
133534
133535
133536
133537
133538
133539
133540
133541
133542
133543
133544
133545
133546
133547
133548
133549
133550
133551
133552
133553
133554
133555
133556
133557
133558
133559
133560
133561
133562
133563
133564
133565
133566
133567
133568
133569
133570
133571
133572
133573
133574
133575
133576
133577
133578
133579
133580
133581
133582
133583
133584
133585
133586
133587
133588
133589
133590
133591
133592
133593
133594
133595
133596
133597
133598
133599
133600
133601
133602
133603
133604
133605
133606
133607
133608
133609
133610
133611
133612
133613
133614
133615
133616
133617
133618
133619
133620
133621
133622
133623
133624
133625
133626
133627
133628
133629
133630
133631
133632
133633
133634
133635
133636
133637
133638
133639
133640
133641
133642
133643
133644
133645
133646
133647
133648
133649
133650
133651
133652
133653
133654
133655
133656
133657
133658
133659
133660
133661
133662
133663
133664
133665
133666
133667
133668
133669
133670
133671
133672
133673
133674
133675
133676
133677
133678
133679
133680
133681
133682
133683
133684
133685
133686
133687
133688
133689
133690
133691
133692
133693
133694
133695
133696
133697
133698
133699
133700
133701
133702
133703
133704
133705
133706
133707
133708
133709
133710
133711
133712
133713
133714
133715
133716
133717
133718
133719
133720
133721
133722
133723
133724
133725
133726
133727
133728
133729
133730
133731
133732
133733
133734
133735
133736
133737
133738
133739
133740
133741
133742
133743
133744
133745
133746
133747
133748
133749
133750
133751
133752
133753
133754
133755
133756
133757
133758
133759
133760
133761
133762
133763
133764
133765
133766
133767
133768
133769
133770
133771
133772
133773
133774
133775
133776
133777
133778
133779
133780
133781
133782
133783
133784
133785
133786
133787
133788
133789
133790
133791
133792
133793
133794
133795
133796
133797
133798
133799
133800
133801
133802
133803
133804
133805
133806
133807
133808
133809
133810
133811
133812
133813
133814
133815
133816
133817
133818
133819
133820
133821
133822
133823
133824
133825
133826
133827
133828
133829
133830
133831
133832
133833
133834
133835
133836
133837
133838
133839
133840
133841
133842
133843
133844
133845
133846
133847
133848
133849
133850
133851
133852
133853
133854
133855
133856
133857
133858
133859
133860
133861
133862
133863
133864
133865
133866
133867
133868
133869
133870
133871
133872
133873
133874
133875
133876
133877
133878
133879
133880
133881
133882
133883
133884
133885
133886
133887
133888
133889
133890
133891
133892
133893
133894
133895
133896
133897
133898
133899
133900
133901
133902
133903
133904
133905
133906
133907
133908
133909
133910
133911
133912
133913
133914
133915
133916
133917
133918
133919
133920
133921
133922
133923
133924
133925
133926
133927
133928
133929
133930
133931
133932
133933
133934
133935
133936
133937
133938
133939
133940
133941
133942
133943
133944
133945
133946
133947
133948
133949
133950
133951
133952
133953
133954
133955
133956
133957
133958
133959
133960
133961
133962
133963
133964
133965
133966
133967
133968
133969
133970
133971
133972
133973
133974
133975
133976
133977
133978
133979
133980
133981
133982
133983
133984
133985
133986
133987
133988
133989
133990
133991
133992
133993
133994
133995
133996
133997
133998
133999
134000
134001
134002
134003
134004
134005
134006
134007
134008
134009
134010
134011
134012
134013
134014
134015
134016
134017
134018
134019
134020
134021
134022
134023
134024
134025
134026
134027
134028
134029
134030
134031
134032
134033
134034
134035
134036
134037
134038
134039
134040
134041
134042
134043
134044
134045
134046
134047
134048
134049
134050
134051
134052
134053
134054
134055
134056
134057
134058
134059
134060
134061
134062
134063
134064
134065
134066
134067
134068
134069
134070
134071
134072
134073
134074
134075
134076
134077
134078
134079
134080
134081
134082
134083
134084
134085
134086
134087
134088
134089
134090
134091
134092
134093
134094
134095
134096
134097
134098
134099
134100
134101
134102
134103
134104
134105
134106
134107
134108
134109
134110
134111
134112
134113
134114
134115
134116
134117
134118
134119
134120
134121
134122
134123
134124
134125
134126
134127
134128
134129
134130
134131
134132
134133
134134
134135
134136
134137
134138
134139
134140
134141
134142
134143
134144
134145
134146
134147
134148
134149
134150
134151
134152
134153
134154
134155
134156
134157
134158
134159
134160
134161
134162
134163
134164
134165
134166
134167
134168
134169
134170
134171
134172
134173
134174
134175
134176
134177
134178
134179
134180
134181
134182
134183
134184
134185
134186
134187
134188
134189
134190
134191
134192
134193
134194
134195
134196
134197
134198
134199
134200
134201
134202
134203
134204
134205
134206
134207
134208
134209
134210
134211
134212
134213
134214
134215
134216
134217
134218
134219
134220
134221
134222
134223
134224
134225
134226
134227
134228
134229
134230
134231
134232
134233
134234
134235
134236
134237
134238
134239
134240
134241
134242
134243
134244
134245
134246
134247
134248
134249
134250
134251
134252
134253
134254
134255
134256
134257
134258
134259
134260
134261
134262
134263
134264
134265
134266
134267
134268
134269
134270
134271
134272
134273
134274
134275
134276
134277
134278
134279
134280
134281
134282
134283
134284
134285
134286
134287
134288
134289
134290
134291
134292
134293
134294
134295
134296
134297
134298
134299
134300
134301
134302
134303
134304
134305
134306
134307
134308
134309
134310
134311
134312
134313
134314
134315
134316
134317
134318
134319
134320
134321
134322
134323
134324
134325
134326
134327
134328
134329
134330
134331
134332
134333
134334
134335
134336
134337
134338
134339
134340
134341
134342
134343
134344
134345
134346
134347
134348
134349
134350
134351
134352
134353
134354
134355
134356
134357
134358
134359
134360
134361
134362
134363
134364
134365
134366
134367
134368
134369
134370
134371
134372
134373
134374
134375
134376
134377
134378
134379
134380
134381
134382
134383
134384
134385
134386
134387
134388
134389
134390
134391
134392
134393
134394
134395
134396
134397
134398
134399
134400
134401
134402
134403
134404
134405
134406
134407
134408
134409
134410
134411
134412
134413
134414
134415
134416
134417
134418
134419
134420
134421
134422
134423
134424
134425
134426
134427
134428
134429
134430
134431
134432
134433
134434
134435
134436
134437
134438
134439
134440
134441
134442
134443
134444
134445
134446
134447
134448
134449
134450
134451
134452
134453
134454
134455
134456
134457
134458
134459
134460
134461
134462
134463
134464
134465
134466
134467
134468
134469
134470
134471
134472
134473
134474
134475
134476
134477
134478
134479
134480
134481
134482
134483
134484
134485
134486
134487
134488
134489
134490
134491
134492
134493
134494
134495
134496
134497
134498
134499
134500
134501
134502
134503
134504
134505
134506
134507
134508
134509
134510
134511
134512
134513
134514
134515
134516
134517
134518
134519
134520
134521
134522
134523
134524
134525
134526
134527
134528
134529
134530
134531
134532
134533
134534
134535
134536
134537
134538
134539
134540
134541
134542
134543
134544
134545
134546
134547
134548
134549
134550
134551
134552
134553
134554
134555
134556
134557
134558
134559
134560
134561
134562
134563
134564
134565
134566
134567
134568
134569
134570
134571
134572
134573
134574
134575
134576
134577
134578
134579
134580
134581
134582
134583
134584
134585
134586
134587
134588
134589
134590
134591
134592
134593
134594
134595
134596
134597
134598
134599
134600
134601
134602
134603
134604
134605
134606
134607
134608
134609
134610
134611
134612
134613
134614
134615
134616
134617
134618
134619
134620
134621
134622
134623
134624
134625
134626
134627
134628
134629
134630
134631
134632
134633
134634
134635
134636
134637
134638
134639
134640
134641
134642
134643
134644
134645
134646
134647
134648
134649
134650
134651
134652
134653
134654
134655
134656
134657
134658
134659
134660
134661
134662
134663
134664
134665
134666
134667
134668
134669
134670
134671
134672
134673
134674
134675
134676
134677
134678
134679
134680
134681
134682
134683
134684
134685
134686
134687
134688
134689
134690
134691
134692
134693
134694
134695
134696
134697
134698
134699
134700
134701
134702
134703
134704
134705
134706
134707
134708
134709
134710
134711
134712
134713
134714
134715
134716
134717
134718
134719
134720
134721
134722
134723
134724
134725
134726
134727
134728
134729
134730
134731
134732
134733
134734
134735
134736
134737
134738
134739
134740
134741
134742
134743
134744
134745
134746
134747
134748
134749
134750
134751
134752
134753
134754
134755
134756
134757
134758
134759
134760
134761
134762
134763
134764
134765
134766
134767
134768
134769
134770
134771
134772
134773
134774
134775
134776
134777
134778
134779
134780
134781
134782
134783
134784
134785
134786
134787
134788
134789
134790
134791
134792
134793
134794
134795
134796
134797
134798
134799
134800
134801
134802
134803
134804
134805
134806
134807
134808
134809
134810
134811
134812
134813
134814
134815
134816
134817
134818
134819
134820
134821
134822
134823
134824
134825
134826
134827
134828
134829
134830
134831
134832
134833
134834
134835
134836
134837
134838
134839
134840
134841
134842
134843
134844
134845
134846
134847
134848
134849
134850
134851
134852
134853
134854
134855
134856
134857
134858
134859
134860
134861
134862
134863
134864
134865
134866
134867
134868
134869
134870
134871
134872
134873
134874
134875
134876
134877
134878
134879
134880
134881
134882
134883
134884
134885
134886
134887
134888
134889
134890
134891
134892
134893
134894
134895
134896
134897
134898
134899
134900
134901
134902
134903
134904
134905
134906
134907
134908
134909
134910
134911
134912
134913
134914
134915
134916
134917
134918
134919
134920
134921
134922
134923
134924
134925
134926
134927
134928
134929
134930
134931
134932
134933
134934
134935
134936
134937
134938
134939
134940
134941
134942
134943
134944
134945
134946
134947
134948
134949
134950
134951
134952
134953
134954
134955
134956
134957
134958
134959
134960
134961
134962
134963
134964
134965
134966
134967
134968
134969
134970
134971
134972
134973
134974
134975
134976
134977
134978
134979
134980
134981
134982
134983
134984
134985
134986
134987
134988
134989
134990
134991
134992
134993
134994
134995
134996
134997
134998
134999
135000
135001
135002
135003
135004
135005
135006
135007
135008
135009
135010
135011
135012
135013
135014
135015
135016
135017
135018
135019
135020
135021
135022
135023
135024
135025
135026
135027
135028
135029
135030
135031
135032
135033
135034
135035
135036
135037
135038
135039
135040
135041
135042
135043
135044
135045
135046
135047
135048
135049
135050
135051
135052
135053
135054
135055
135056
135057
135058
135059
135060
135061
135062
135063
135064
135065
135066
135067
135068
135069
135070
135071
135072
135073
135074
135075
135076
135077
135078
135079
135080
135081
135082
135083
135084
135085
135086
135087
135088
135089
135090
135091
135092
135093
135094
135095
135096
135097
135098
135099
135100
135101
135102
135103
135104
135105
135106
135107
135108
135109
135110
135111
135112
135113
135114
135115
135116
135117
135118
135119
135120
135121
135122
135123
135124
135125
135126
135127
135128
135129
135130
135131
135132
135133
135134
135135
135136
135137
135138
135139
135140
135141
135142
135143
135144
135145
135146
135147
135148
135149
135150
135151
135152
135153
135154
135155
135156
135157
135158
135159
135160
135161
135162
135163
135164
135165
135166
135167
135168
135169
135170
135171
135172
135173
135174
135175
135176
135177
135178
135179
135180
135181
135182
135183
135184
135185
135186
135187
135188
135189
135190
135191
135192
135193
135194
135195
135196
135197
135198
135199
135200
135201
135202
135203
135204
135205
135206
135207
135208
135209
135210
135211
135212
135213
135214
135215
135216
135217
135218
135219
135220
135221
135222
135223
135224
135225
135226
135227
135228
135229
135230
135231
135232
135233
135234
135235
135236
135237
135238
135239
135240
135241
135242
135243
135244
135245
135246
135247
135248
135249
135250
135251
135252
135253
135254
135255
135256
135257
135258
135259
135260
135261
135262
135263
135264
135265
135266
135267
135268
135269
135270
135271
135272
135273
135274
135275
135276
135277
135278
135279
135280
135281
135282
135283
135284
135285
135286
135287
135288
135289
135290
135291
135292
135293
135294
135295
135296
135297
135298
135299
135300
135301
135302
135303
135304
135305
135306
135307
135308
135309
135310
135311
135312
135313
135314
135315
135316
135317
135318
135319
135320
135321
135322
135323
135324
135325
135326
135327
135328
135329
135330
135331
135332
135333
135334
135335
135336
135337
135338
135339
135340
135341
135342
135343
135344
135345
135346
135347
135348
135349
135350
135351
135352
135353
135354
135355
135356
135357
135358
135359
135360
135361
135362
135363
135364
135365
135366
135367
135368
135369
135370
135371
135372
135373
135374
135375
135376
135377
135378
135379
135380
135381
135382
135383
135384
135385
135386
135387
135388
135389
135390
135391
135392
135393
135394
135395
135396
135397
135398
135399
135400
135401
135402
135403
135404
135405
135406
135407
135408
135409
135410
135411
135412
135413
135414
135415
135416
135417
135418
135419
135420
135421
135422
135423
135424
135425
135426
135427
135428
135429
135430
135431
135432
135433
135434
135435
135436
135437
135438
135439
135440
135441
135442
135443
135444
135445
135446
135447
135448
135449
135450
135451
135452
135453
135454
135455
135456
135457
135458
135459
135460
135461
135462
135463
135464
135465
135466
135467
135468
135469
135470
135471
135472
135473
135474
135475
135476
135477
135478
135479
135480
135481
135482
135483
135484
135485
135486
135487
135488
135489
135490
135491
135492
135493
135494
135495
135496
135497
135498
135499
135500
135501
135502
135503
135504
135505
135506
135507
135508
135509
135510
135511
135512
135513
135514
135515
135516
135517
135518
135519
135520
135521
135522
135523
135524
135525
135526
135527
135528
135529
135530
135531
135532
135533
135534
135535
135536
135537
135538
135539
135540
135541
135542
135543
135544
135545
135546
135547
135548
135549
135550
135551
135552
135553
135554
135555
135556
135557
135558
135559
135560
135561
135562
135563
135564
135565
135566
135567
135568
135569
135570
135571
135572
135573
135574
135575
135576
135577
135578
135579
135580
135581
135582
135583
135584
135585
135586
135587
135588
135589
135590
135591
135592
135593
135594
135595
135596
135597
135598
135599
135600
135601
135602
135603
135604
135605
135606
135607
135608
135609
135610
135611
135612
135613
135614
135615
135616
135617
135618
135619
135620
135621
135622
135623
135624
135625
135626
135627
135628
135629
135630
135631
135632
135633
135634
135635
135636
135637
135638
135639
135640
135641
135642
135643
135644
135645
135646
135647
135648
135649
135650
135651
135652
135653
135654
135655
135656
135657
135658
135659
135660
135661
135662
135663
135664
135665
135666
135667
135668
135669
135670
135671
135672
135673
135674
135675
135676
135677
135678
135679
135680
135681
135682
135683
135684
135685
135686
135687
135688
135689
135690
135691
135692
135693
135694
135695
135696
135697
135698
135699
135700
135701
135702
135703
135704
135705
135706
135707
135708
135709
135710
135711
135712
135713
135714
135715
135716
135717
135718
135719
135720
135721
135722
135723
135724
135725
135726
135727
135728
135729
135730
135731
135732
135733
135734
135735
135736
135737
135738
135739
135740
135741
135742
135743
135744
135745
135746
135747
135748
135749
135750
135751
135752
135753
135754
135755
135756
135757
135758
135759
135760
135761
135762
135763
135764
135765
135766
135767
135768
135769
135770
135771
135772
135773
135774
135775
135776
135777
135778
135779
135780
135781
135782
135783
135784
135785
135786
135787
135788
135789
135790
135791
135792
135793
135794
135795
135796
135797
135798
135799
135800
135801
135802
135803
135804
135805
135806
135807
135808
135809
135810
135811
135812
135813
135814
135815
135816
135817
135818
135819
135820
135821
135822
135823
135824
135825
135826
135827
135828
135829
135830
135831
135832
135833
135834
135835
135836
135837
135838
135839
135840
135841
135842
135843
135844
135845
135846
135847
135848
135849
135850
135851
135852
135853
135854
135855
135856
135857
135858
135859
135860
135861
135862
135863
135864
135865
135866
135867
135868
135869
135870
135871
135872
135873
135874
135875
135876
135877
135878
135879
135880
135881
135882
135883
135884
135885
135886
135887
135888
135889
135890
135891
135892
135893
135894
135895
135896
135897
135898
135899
135900
135901
135902
135903
135904
135905
135906
135907
135908
135909
135910
135911
135912
135913
135914
135915
135916
135917
135918
135919
135920
135921
135922
135923
135924
135925
135926
135927
135928
135929
135930
135931
135932
135933
135934
135935
135936
135937
135938
135939
135940
135941
135942
135943
135944
135945
135946
135947
135948
135949
135950
135951
135952
135953
135954
135955
135956
135957
135958
135959
135960
135961
135962
135963
135964
135965
135966
135967
135968
135969
135970
135971
135972
135973
135974
135975
135976
135977
135978
135979
135980
135981
135982
135983
135984
135985
135986
135987
135988
135989
135990
135991
135992
135993
135994
135995
135996
135997
135998
135999
136000
136001
136002
136003
136004
136005
136006
136007
136008
136009
136010
136011
136012
136013
136014
136015
136016
136017
136018
136019
136020
136021
136022
136023
136024
136025
136026
136027
136028
136029
136030
136031
136032
136033
136034
136035
136036
136037
136038
136039
136040
136041
136042
136043
136044
136045
136046
136047
136048
136049
136050
136051
136052
136053
136054
136055
136056
136057
136058
136059
136060
136061
136062
136063
136064
136065
136066
136067
136068
136069
136070
136071
136072
136073
136074
136075
136076
136077
136078
136079
136080
136081
136082
136083
136084
136085
136086
136087
136088
136089
136090
136091
136092
136093
136094
136095
136096
136097
136098
136099
136100
136101
136102
136103
136104
136105
136106
136107
136108
136109
136110
136111
136112
136113
136114
136115
136116
136117
136118
136119
136120
136121
136122
136123
136124
136125
136126
136127
136128
136129
136130
136131
136132
136133
136134
136135
136136
136137
136138
136139
136140
136141
136142
136143
136144
136145
136146
136147
136148
136149
136150
136151
136152
136153
136154
136155
136156
136157
136158
136159
136160
136161
136162
136163
136164
136165
136166
136167
136168
136169
136170
136171
136172
136173
136174
136175
136176
136177
136178
136179
136180
136181
136182
136183
136184
136185
136186
136187
136188
136189
136190
136191
136192
136193
136194
136195
136196
136197
136198
136199
136200
136201
136202
136203
136204
136205
136206
136207
136208
136209
136210
136211
136212
136213
136214
136215
136216
136217
136218
136219
136220
136221
136222
136223
136224
136225
136226
136227
136228
136229
136230
136231
136232
136233
136234
136235
136236
136237
136238
136239
136240
136241
136242
136243
136244
136245
136246
136247
136248
136249
136250
136251
136252
136253
136254
136255
136256
136257
136258
136259
136260
136261
136262
136263
136264
136265
136266
136267
136268
136269
136270
136271
136272
136273
136274
136275
136276
136277
136278
136279
136280
136281
136282
136283
136284
136285
136286
136287
136288
136289
136290
136291
136292
136293
136294
136295
136296
136297
136298
136299
136300
136301
136302
136303
136304
136305
136306
136307
136308
136309
136310
136311
136312
136313
136314
136315
136316
136317
136318
136319
136320
136321
136322
136323
136324
136325
136326
136327
136328
136329
136330
136331
136332
136333
136334
136335
136336
136337
136338
136339
136340
136341
136342
136343
136344
136345
136346
136347
136348
136349
136350
136351
136352
136353
136354
136355
136356
136357
136358
136359
136360
136361
136362
136363
136364
136365
136366
136367
136368
136369
136370
136371
136372
136373
136374
136375
136376
136377
136378
136379
136380
136381
136382
136383
136384
136385
136386
136387
136388
136389
136390
136391
136392
136393
136394
136395
136396
136397
136398
136399
136400
136401
136402
136403
136404
136405
136406
136407
136408
136409
136410
136411
136412
136413
136414
136415
136416
136417
136418
136419
136420
136421
136422
136423
136424
136425
136426
136427
136428
136429
136430
136431
136432
136433
136434
136435
136436
136437
136438
136439
136440
136441
136442
136443
136444
136445
136446
136447
136448
136449
136450
136451
136452
136453
136454
136455
136456
136457
136458
136459
136460
136461
136462
136463
136464
136465
136466
136467
136468
136469
136470
136471
136472
136473
136474
136475
136476
136477
136478
136479
136480
136481
136482
136483
136484
136485
136486
136487
136488
136489
136490
136491
136492
136493
136494
136495
136496
136497
136498
136499
136500
136501
136502
136503
136504
136505
136506
136507
136508
136509
136510
136511
136512
136513
136514
136515
136516
136517
136518
136519
136520
136521
136522
136523
136524
136525
136526
136527
136528
136529
136530
136531
136532
136533
136534
136535
136536
136537
136538
136539
136540
136541
136542
136543
136544
136545
136546
136547
136548
136549
136550
136551
136552
136553
136554
136555
136556
136557
136558
136559
136560
136561
136562
136563
136564
136565
136566
136567
136568
136569
136570
136571
136572
136573
136574
136575
136576
136577
136578
136579
136580
136581
136582
136583
136584
136585
136586
136587
136588
136589
136590
136591
136592
136593
136594
136595
136596
136597
136598
136599
136600
136601
136602
136603
136604
136605
136606
136607
136608
136609
136610
136611
136612
136613
136614
136615
136616
136617
136618
136619
136620
136621
136622
136623
136624
136625
136626
136627
136628
136629
136630
136631
136632
136633
136634
136635
136636
136637
136638
136639
136640
136641
136642
136643
136644
136645
136646
136647
136648
136649
136650
136651
136652
136653
136654
136655
136656
136657
136658
136659
136660
136661
136662
136663
136664
136665
136666
136667
136668
136669
136670
136671
136672
136673
136674
136675
136676
136677
136678
136679
136680
136681
136682
136683
136684
136685
136686
136687
136688
136689
136690
136691
136692
136693
136694
136695
136696
136697
136698
136699
136700
136701
136702
136703
136704
136705
136706
136707
136708
136709
136710
136711
136712
136713
136714
136715
136716
136717
136718
136719
136720
136721
136722
136723
136724
136725
136726
136727
136728
136729
136730
136731
136732
136733
136734
136735
136736
136737
136738
136739
136740
136741
136742
136743
136744
136745
136746
136747
136748
136749
136750
136751
136752
136753
136754
136755
136756
136757
136758
136759
136760
136761
136762
136763
136764
136765
136766
136767
136768
136769
136770
136771
136772
136773
136774
136775
136776
136777
136778
136779
136780
136781
136782
136783
136784
136785
136786
136787
136788
136789
136790
136791
136792
136793
136794
136795
136796
136797
136798
136799
136800
136801
136802
136803
136804
136805
136806
136807
136808
136809
136810
136811
136812
136813
136814
136815
136816
136817
136818
136819
136820
136821
136822
136823
136824
136825
136826
136827
136828
136829
136830
136831
136832
136833
136834
136835
136836
136837
136838
136839
136840
136841
136842
136843
136844
136845
136846
136847
136848
136849
136850
136851
136852
136853
136854
136855
136856
136857
136858
136859
136860
136861
136862
136863
136864
136865
136866
136867
136868
136869
136870
136871
136872
136873
136874
136875
136876
136877
136878
136879
136880
136881
136882
136883
136884
136885
136886
136887
136888
136889
136890
136891
136892
136893
136894
136895
136896
136897
136898
136899
136900
136901
136902
136903
136904
136905
136906
136907
136908
136909
136910
136911
136912
136913
136914
136915
136916
136917
136918
136919
136920
136921
136922
136923
136924
136925
136926
136927
136928
136929
136930
136931
136932
136933
136934
136935
136936
136937
136938
136939
136940
136941
136942
136943
136944
136945
136946
136947
136948
136949
136950
136951
136952
136953
136954
136955
136956
136957
136958
136959
136960
136961
136962
136963
136964
136965
136966
136967
136968
136969
136970
136971
136972
136973
136974
136975
136976
136977
136978
136979
136980
136981
136982
136983
136984
136985
136986
136987
136988
136989
136990
136991
136992
136993
136994
136995
136996
136997
136998
136999
137000
137001
137002
137003
137004
137005
137006
137007
137008
137009
137010
137011
137012
137013
137014
137015
137016
137017
137018
137019
137020
137021
137022
137023
137024
137025
137026
137027
137028
137029
137030
137031
137032
137033
137034
137035
137036
137037
137038
137039
137040
137041
137042
137043
137044
137045
137046
137047
137048
137049
137050
137051
137052
137053
137054
137055
137056
137057
137058
137059
137060
137061
137062
137063
137064
137065
137066
137067
137068
137069
137070
137071
137072
137073
137074
137075
137076
137077
137078
137079
137080
137081
137082
137083
137084
137085
137086
137087
137088
137089
137090
137091
137092
137093
137094
137095
137096
137097
137098
137099
137100
137101
137102
137103
137104
137105
137106
137107
137108
137109
137110
137111
137112
137113
137114
137115
137116
137117
137118
137119
137120
137121
137122
137123
137124
137125
137126
137127
137128
137129
137130
137131
137132
137133
137134
137135
137136
137137
137138
137139
137140
137141
137142
137143
137144
137145
137146
137147
137148
137149
137150
137151
137152
137153
137154
137155
137156
137157
137158
137159
137160
137161
137162
137163
137164
137165
137166
137167
137168
137169
137170
137171
137172
137173
137174
137175
137176
137177
137178
137179
137180
137181
137182
137183
137184
137185
137186
137187
137188
137189
137190
137191
137192
137193
137194
137195
137196
137197
137198
137199
137200
137201
137202
137203
137204
137205
137206
137207
137208
137209
137210
137211
137212
137213
137214
137215
137216
137217
137218
137219
137220
137221
137222
137223
137224
137225
137226
137227
137228
137229
137230
137231
137232
137233
137234
137235
137236
137237
137238
137239
137240
137241
137242
137243
137244
137245
137246
137247
137248
137249
137250
137251
137252
137253
137254
137255
137256
137257
137258
137259
137260
137261
137262
137263
137264
137265
137266
137267
137268
137269
137270
137271
137272
137273
137274
137275
137276
137277
137278
137279
137280
137281
137282
137283
137284
137285
137286
137287
137288
137289
137290
137291
137292
137293
137294
137295
137296
137297
137298
137299
137300
137301
137302
137303
137304
137305
137306
137307
137308
137309
137310
137311
137312
137313
137314
137315
137316
137317
137318
137319
137320
137321
137322
137323
137324
137325
137326
137327
137328
137329
137330
137331
137332
137333
137334
137335
137336
137337
137338
137339
137340
137341
137342
137343
137344
137345
137346
137347
137348
137349
137350
137351
137352
137353
137354
137355
137356
137357
137358
137359
137360
137361
137362
137363
137364
137365
137366
137367
137368
137369
137370
137371
137372
137373
137374
137375
137376
137377
137378
137379
137380
137381
137382
137383
137384
137385
137386
137387
137388
137389
137390
137391
137392
137393
137394
137395
137396
137397
137398
137399
137400
137401
137402
137403
137404
137405
137406
137407
137408
137409
137410
137411
137412
137413
137414
137415
137416
137417
137418
137419
137420
137421
137422
137423
137424
137425
137426
137427
137428
137429
137430
137431
137432
137433
137434
137435
137436
137437
137438
137439
137440
137441
137442
137443
137444
137445
137446
137447
137448
137449
137450
137451
137452
137453
137454
137455
137456
137457
137458
137459
137460
137461
137462
137463
137464
137465
137466
137467
137468
137469
137470
137471
137472
137473
137474
137475
137476
137477
137478
137479
137480
137481
137482
137483
137484
137485
137486
137487
137488
137489
137490
137491
137492
137493
137494
137495
137496
137497
137498
137499
137500
137501
137502
137503
137504
137505
137506
137507
137508
137509
137510
137511
137512
137513
137514
137515
137516
137517
137518
137519
137520
137521
137522
137523
137524
137525
137526
137527
137528
137529
137530
137531
137532
137533
137534
137535
137536
137537
137538
137539
137540
137541
137542
137543
137544
137545
137546
137547
137548
137549
137550
137551
137552
137553
137554
137555
137556
137557
137558
137559
137560
137561
137562
137563
137564
137565
137566
137567
137568
137569
137570
137571
137572
137573
137574
137575
137576
137577
137578
137579
137580
137581
137582
137583
137584
137585
137586
137587
137588
137589
137590
137591
137592
137593
137594
137595
137596
137597
137598
137599
137600
137601
137602
137603
137604
137605
137606
137607
137608
137609
137610
137611
137612
137613
137614
137615
137616
137617
137618
137619
137620
137621
137622
137623
137624
137625
137626
137627
137628
137629
137630
137631
137632
137633
137634
137635
137636
137637
137638
137639
137640
137641
137642
137643
137644
137645
137646
137647
137648
137649
137650
137651
137652
137653
137654
137655
137656
137657
137658
137659
137660
137661
137662
137663
137664
137665
137666
137667
137668
137669
137670
137671
137672
137673
137674
137675
137676
137677
137678
137679
137680
137681
137682
137683
137684
137685
137686
137687
137688
137689
137690
137691
137692
137693
137694
137695
137696
137697
137698
137699
137700
137701
137702
137703
137704
137705
137706
137707
137708
137709
137710
137711
137712
137713
137714
137715
137716
137717
137718
137719
137720
137721
137722
137723
137724
137725
137726
137727
137728
137729
137730
137731
137732
137733
137734
137735
137736
137737
137738
137739
137740
137741
137742
137743
137744
137745
137746
137747
137748
137749
137750
137751
137752
137753
137754
137755
137756
137757
137758
137759
137760
137761
137762
137763
137764
137765
137766
137767
137768
137769
137770
137771
137772
137773
137774
137775
137776
137777
137778
137779
137780
137781
137782
137783
137784
137785
137786
137787
137788
137789
137790
137791
137792
137793
137794
137795
137796
137797
137798
137799
137800
137801
137802
137803
137804
137805
137806
137807
137808
137809
137810
137811
137812
137813
137814
137815
137816
137817
137818
137819
137820
137821
137822
137823
137824
137825
137826
137827
137828
137829
137830
137831
137832
137833
137834
137835
137836
137837
137838
137839
137840
137841
137842
137843
137844
137845
137846
137847
137848
137849
137850
137851
137852
137853
137854
137855
137856
137857
137858
137859
137860
137861
137862
137863
137864
137865
137866
137867
137868
137869
137870
137871
137872
137873
137874
137875
137876
137877
137878
137879
137880
137881
137882
137883
137884
137885
137886
137887
137888
137889
137890
137891
137892
137893
137894
137895
137896
137897
137898
137899
137900
137901
137902
137903
137904
137905
137906
137907
137908
137909
137910
137911
137912
137913
137914
137915
137916
137917
137918
137919
137920
137921
137922
137923
137924
137925
137926
137927
137928
137929
137930
137931
137932
137933
137934
137935
137936
137937
137938
137939
137940
137941
137942
137943
137944
137945
137946
137947
137948
137949
137950
137951
137952
137953
137954
137955
137956
137957
137958
137959
137960
137961
137962
137963
137964
137965
137966
137967
137968
137969
137970
137971
137972
137973
137974
137975
137976
137977
137978
137979
137980
137981
137982
137983
137984
137985
137986
137987
137988
137989
137990
137991
137992
137993
137994
137995
137996
137997
137998
137999
138000
138001
138002
138003
138004
138005
138006
138007
138008
138009
138010
138011
138012
138013
138014
138015
138016
138017
138018
138019
138020
138021
138022
138023
138024
138025
138026
138027
138028
138029
138030
138031
138032
138033
138034
138035
138036
138037
138038
138039
138040
138041
138042
138043
138044
138045
138046
138047
138048
138049
138050
138051
138052
138053
138054
138055
138056
138057
138058
138059
138060
138061
138062
138063
138064
138065
138066
138067
138068
138069
138070
138071
138072
138073
138074
138075
138076
138077
138078
138079
138080
138081
138082
138083
138084
138085
138086
138087
138088
138089
138090
138091
138092
138093
138094
138095
138096
138097
138098
138099
138100
138101
138102
138103
138104
138105
138106
138107
138108
138109
138110
138111
138112
138113
138114
138115
138116
138117
138118
138119
138120
138121
138122
138123
138124
138125
138126
138127
138128
138129
138130
138131
138132
138133
138134
138135
138136
138137
138138
138139
138140
138141
138142
138143
138144
138145
138146
138147
138148
138149
138150
138151
138152
138153
138154
138155
138156
138157
138158
138159
138160
138161
138162
138163
138164
138165
138166
138167
138168
138169
138170
138171
138172
138173
138174
138175
138176
138177
138178
138179
138180
138181
138182
138183
138184
138185
138186
138187
138188
138189
138190
138191
138192
138193
138194
138195
138196
138197
138198
138199
138200
138201
138202
138203
138204
138205
138206
138207
138208
138209
138210
138211
138212
138213
138214
138215
138216
138217
138218
138219
138220
138221
138222
138223
138224
138225
138226
138227
138228
138229
138230
138231
138232
138233
138234
138235
138236
138237
138238
138239
138240
138241
138242
138243
138244
138245
138246
138247
138248
138249
138250
138251
138252
138253
138254
138255
138256
138257
138258
138259
138260
138261
138262
138263
138264
138265
138266
138267
138268
138269
138270
138271
138272
138273
138274
138275
138276
138277
138278
138279
138280
138281
138282
138283
138284
138285
138286
138287
138288
138289
138290
138291
138292
138293
138294
138295
138296
138297
138298
138299
138300
138301
138302
138303
138304
138305
138306
138307
138308
138309
138310
138311
138312
138313
138314
138315
138316
138317
138318
138319
138320
138321
138322
138323
138324
138325
138326
138327
138328
138329
138330
138331
138332
138333
138334
138335
138336
138337
138338
138339
138340
138341
138342
138343
138344
138345
138346
138347
138348
138349
138350
138351
138352
138353
138354
138355
138356
138357
138358
138359
138360
138361
138362
138363
138364
138365
138366
138367
138368
138369
138370
138371
138372
138373
138374
138375
138376
138377
138378
138379
138380
138381
138382
138383
138384
138385
138386
138387
138388
138389
138390
138391
138392
138393
138394
138395
138396
138397
138398
138399
138400
138401
138402
138403
138404
138405
138406
138407
138408
138409
138410
138411
138412
138413
138414
138415
138416
138417
138418
138419
138420
138421
138422
138423
138424
138425
138426
138427
138428
138429
138430
138431
138432
138433
138434
138435
138436
138437
138438
138439
138440
138441
138442
138443
138444
138445
138446
138447
138448
138449
138450
138451
138452
138453
138454
138455
138456
138457
138458
138459
138460
138461
138462
138463
138464
138465
138466
138467
138468
138469
138470
138471
138472
138473
138474
138475
138476
138477
138478
138479
138480
138481
138482
138483
138484
138485
138486
138487
138488
138489
138490
138491
138492
138493
138494
138495
138496
138497
138498
138499
138500
138501
138502
138503
138504
138505
138506
138507
138508
138509
138510
138511
138512
138513
138514
138515
138516
138517
138518
138519
138520
138521
138522
138523
138524
138525
138526
138527
138528
138529
138530
138531
138532
138533
138534
138535
138536
138537
138538
138539
138540
138541
138542
138543
138544
138545
138546
138547
138548
138549
138550
138551
138552
138553
138554
138555
138556
138557
138558
138559
138560
138561
138562
138563
138564
138565
138566
138567
138568
138569
138570
138571
138572
138573
138574
138575
138576
138577
138578
138579
138580
138581
138582
138583
138584
138585
138586
138587
138588
138589
138590
138591
138592
138593
138594
138595
138596
138597
138598
138599
138600
138601
138602
138603
138604
138605
138606
138607
138608
138609
138610
138611
138612
138613
138614
138615
138616
138617
138618
138619
138620
138621
138622
138623
138624
138625
138626
138627
138628
138629
138630
138631
138632
138633
138634
138635
138636
138637
138638
138639
138640
138641
138642
138643
138644
138645
138646
138647
138648
138649
138650
138651
138652
138653
138654
138655
138656
138657
138658
138659
138660
138661
138662
138663
138664
138665
138666
138667
138668
138669
138670
138671
138672
138673
138674
138675
138676
138677
138678
138679
138680
138681
138682
138683
138684
138685
138686
138687
138688
138689
138690
138691
138692
138693
138694
138695
138696
138697
138698
138699
138700
138701
138702
138703
138704
138705
138706
138707
138708
138709
138710
138711
138712
138713
138714
138715
138716
138717
138718
138719
138720
138721
138722
138723
138724
138725
138726
138727
138728
138729
138730
138731
138732
138733
138734
138735
138736
138737
138738
138739
138740
138741
138742
138743
138744
138745
138746
138747
138748
138749
138750
138751
138752
138753
138754
138755
138756
138757
138758
138759
138760
138761
138762
138763
138764
138765
138766
138767
138768
138769
138770
138771
138772
138773
138774
138775
138776
138777
138778
138779
138780
138781
138782
138783
138784
138785
138786
138787
138788
138789
138790
138791
138792
138793
138794
138795
138796
138797
138798
138799
138800
138801
138802
138803
138804
138805
138806
138807
138808
138809
138810
138811
138812
138813
138814
138815
138816
138817
138818
138819
138820
138821
138822
138823
138824
138825
138826
138827
138828
138829
138830
138831
138832
138833
138834
138835
138836
138837
138838
138839
138840
138841
138842
138843
138844
138845
138846
138847
138848
138849
138850
138851
138852
138853
138854
138855
138856
138857
138858
138859
138860
138861
138862
138863
138864
138865
138866
138867
138868
138869
138870
138871
138872
138873
138874
138875
138876
138877
138878
138879
138880
138881
138882
138883
138884
138885
138886
138887
138888
138889
138890
138891
138892
138893
138894
138895
138896
138897
138898
138899
138900
138901
138902
138903
138904
138905
138906
138907
138908
138909
138910
138911
138912
138913
138914
138915
138916
138917
138918
138919
138920
138921
138922
138923
138924
138925
138926
138927
138928
138929
138930
138931
138932
138933
138934
138935
138936
138937
138938
138939
138940
138941
138942
138943
138944
138945
138946
138947
138948
138949
138950
138951
138952
138953
138954
138955
138956
138957
138958
138959
138960
138961
138962
138963
138964
138965
138966
138967
138968
138969
138970
138971
138972
138973
138974
138975
138976
138977
138978
138979
138980
138981
138982
138983
138984
138985
138986
138987
138988
138989
138990
138991
138992
138993
138994
138995
138996
138997
138998
138999
139000
139001
139002
139003
139004
139005
139006
139007
139008
139009
139010
139011
139012
139013
139014
139015
139016
139017
139018
139019
139020
139021
139022
139023
139024
139025
139026
139027
139028
139029
139030
139031
139032
139033
139034
139035
139036
139037
139038
139039
139040
139041
139042
139043
139044
139045
139046
139047
139048
139049
139050
139051
139052
139053
139054
139055
139056
139057
139058
139059
139060
139061
139062
139063
139064
139065
139066
139067
139068
139069
139070
139071
139072
139073
139074
139075
139076
139077
139078
139079
139080
139081
139082
139083
139084
139085
139086
139087
139088
139089
139090
139091
139092
139093
139094
139095
139096
139097
139098
139099
139100
139101
139102
139103
139104
139105
139106
139107
139108
139109
139110
139111
139112
139113
139114
139115
139116
139117
139118
139119
139120
139121
139122
139123
139124
139125
139126
139127
139128
139129
139130
139131
139132
139133
139134
139135
139136
139137
139138
139139
139140
139141
139142
139143
139144
139145
139146
139147
139148
139149
139150
139151
139152
139153
139154
139155
139156
139157
139158
139159
139160
139161
139162
139163
139164
139165
139166
139167
139168
139169
139170
139171
139172
139173
139174
139175
139176
139177
139178
139179
139180
139181
139182
139183
139184
139185
139186
139187
139188
139189
139190
139191
139192
139193
139194
139195
139196
139197
139198
139199
139200
139201
139202
139203
139204
139205
139206
139207
139208
139209
139210
139211
139212
139213
139214
139215
139216
139217
139218
139219
139220
139221
139222
139223
139224
139225
139226
139227
139228
139229
139230
139231
139232
139233
139234
139235
139236
139237
139238
139239
139240
139241
139242
139243
139244
139245
139246
139247
139248
139249
139250
139251
139252
139253
139254
139255
139256
139257
139258
139259
139260
139261
139262
139263
139264
139265
139266
139267
139268
139269
139270
139271
139272
139273
139274
139275
139276
139277
139278
139279
139280
139281
139282
139283
139284
139285
139286
139287
139288
139289
139290
139291
139292
139293
139294
139295
139296
139297
139298
139299
139300
139301
139302
139303
139304
139305
139306
139307
139308
139309
139310
139311
139312
139313
139314
139315
139316
139317
139318
139319
139320
139321
139322
139323
139324
139325
139326
139327
139328
139329
139330
139331
139332
139333
139334
139335
139336
139337
139338
139339
139340
139341
139342
139343
139344
139345
139346
139347
139348
139349
139350
139351
139352
139353
139354
139355
139356
139357
139358
139359
139360
139361
139362
139363
139364
139365
139366
139367
139368
139369
139370
139371
139372
139373
139374
139375
139376
139377
139378
139379
139380
139381
139382
139383
139384
139385
139386
139387
139388
139389
139390
139391
139392
139393
139394
139395
139396
139397
139398
139399
139400
139401
139402
139403
139404
139405
139406
139407
139408
139409
139410
139411
139412
139413
139414
139415
139416
139417
139418
139419
139420
139421
139422
139423
139424
139425
139426
139427
139428
139429
139430
139431
139432
139433
139434
139435
139436
139437
139438
139439
139440
139441
139442
139443
139444
139445
139446
139447
139448
139449
139450
139451
139452
139453
139454
139455
139456
139457
139458
139459
139460
139461
139462
139463
139464
139465
139466
139467
139468
139469
139470
139471
139472
139473
139474
139475
139476
139477
139478
139479
139480
139481
139482
139483
139484
139485
139486
139487
139488
139489
139490
139491
139492
139493
139494
139495
139496
139497
139498
139499
139500
139501
139502
139503
139504
139505
139506
139507
139508
139509
139510
139511
139512
139513
139514
139515
139516
139517
139518
139519
139520
139521
139522
139523
139524
139525
139526
139527
139528
139529
139530
139531
139532
139533
139534
139535
139536
139537
139538
139539
139540
139541
139542
139543
139544
139545
139546
139547
139548
139549
139550
139551
139552
139553
139554
139555
139556
139557
139558
139559
139560
139561
139562
139563
139564
139565
139566
139567
139568
139569
139570
139571
139572
139573
139574
139575
139576
139577
139578
139579
139580
139581
139582
139583
139584
139585
139586
139587
139588
139589
139590
139591
139592
139593
139594
139595
139596
139597
139598
139599
139600
139601
139602
139603
139604
139605
139606
139607
139608
139609
139610
139611
139612
139613
139614
139615
139616
139617
139618
139619
139620
139621
139622
139623
139624
139625
139626
139627
139628
139629
139630
139631
139632
139633
139634
139635
139636
139637
139638
139639
139640
139641
139642
139643
139644
139645
139646
139647
139648
139649
139650
139651
139652
139653
139654
139655
139656
139657
139658
139659
139660
139661
139662
139663
139664
139665
139666
139667
139668
139669
139670
139671
139672
139673
139674
139675
139676
139677
139678
139679
139680
139681
139682
139683
139684
139685
139686
139687
139688
139689
139690
139691
139692
139693
139694
139695
139696
139697
139698
139699
139700
139701
139702
139703
139704
139705
139706
139707
139708
139709
139710
139711
139712
139713
139714
139715
139716
139717
139718
139719
139720
139721
139722
139723
139724
139725
139726
139727
139728
139729
139730
139731
139732
139733
139734
139735
139736
139737
139738
139739
139740
139741
139742
139743
139744
139745
139746
139747
139748
139749
139750
139751
139752
139753
139754
139755
139756
139757
139758
139759
139760
139761
139762
139763
139764
139765
139766
139767
139768
139769
139770
139771
139772
139773
139774
139775
139776
139777
139778
139779
139780
139781
139782
139783
139784
139785
139786
139787
139788
139789
139790
139791
139792
139793
139794
139795
139796
139797
139798
139799
139800
139801
139802
139803
139804
139805
139806
139807
139808
139809
139810
139811
139812
139813
139814
139815
139816
139817
139818
139819
139820
139821
139822
139823
139824
139825
139826
139827
139828
139829
139830
139831
139832
139833
139834
139835
139836
139837
139838
139839
139840
139841
139842
139843
139844
139845
139846
139847
139848
139849
139850
139851
139852
139853
139854
139855
139856
139857
139858
139859
139860
139861
139862
139863
139864
139865
139866
139867
139868
139869
139870
139871
139872
139873
139874
139875
139876
139877
139878
139879
139880
139881
139882
139883
139884
139885
139886
139887
139888
139889
139890
139891
139892
139893
139894
139895
139896
139897
139898
139899
139900
139901
139902
139903
139904
139905
139906
139907
139908
139909
139910
139911
139912
139913
139914
139915
139916
139917
139918
139919
139920
139921
139922
139923
139924
139925
139926
139927
139928
139929
139930
139931
139932
139933
139934
139935
139936
139937
139938
139939
139940
139941
139942
139943
139944
139945
139946
139947
139948
139949
139950
139951
139952
139953
139954
139955
139956
139957
139958
139959
139960
139961
139962
139963
139964
139965
139966
139967
139968
139969
139970
139971
139972
139973
139974
139975
139976
139977
139978
139979
139980
139981
139982
139983
139984
139985
139986
139987
139988
139989
139990
139991
139992
139993
139994
139995
139996
139997
139998
139999
140000
140001
140002
140003
140004
140005
140006
140007
140008
140009
140010
140011
140012
140013
140014
140015
140016
140017
140018
140019
140020
140021
140022
140023
140024
140025
140026
140027
140028
140029
140030
140031
140032
140033
140034
140035
140036
140037
140038
140039
140040
140041
140042
140043
140044
140045
140046
140047
140048
140049
140050
140051
140052
140053
140054
140055
140056
140057
140058
140059
140060
140061
140062
140063
140064
140065
140066
140067
140068
140069
140070
140071
140072
140073
140074
140075
140076
140077
140078
140079
140080
140081
140082
140083
140084
140085
140086
140087
140088
140089
140090
140091
140092
140093
140094
140095
140096
140097
140098
140099
140100
140101
140102
140103
140104
140105
140106
140107
140108
140109
140110
140111
140112
140113
140114
140115
140116
140117
140118
140119
140120
140121
140122
140123
140124
140125
140126
140127
140128
140129
140130
140131
140132
140133
140134
140135
140136
140137
140138
140139
140140
140141
140142
140143
140144
140145
140146
140147
140148
140149
140150
140151
140152
140153
140154
140155
140156
140157
140158
140159
140160
140161
140162
140163
140164
140165
140166
140167
140168
140169
140170
140171
140172
140173
140174
140175
140176
140177
140178
140179
140180
140181
140182
140183
140184
140185
140186
140187
140188
140189
140190
140191
140192
140193
140194
140195
140196
140197
140198
140199
140200
140201
140202
140203
140204
140205
140206
140207
140208
140209
140210
140211
140212
140213
140214
140215
140216
140217
140218
140219
140220
140221
140222
140223
140224
140225
140226
140227
140228
140229
140230
140231
140232
140233
140234
140235
140236
140237
140238
140239
140240
140241
140242
140243
140244
140245
140246
140247
140248
140249
140250
140251
140252
140253
140254
140255
140256
140257
140258
140259
140260
140261
140262
140263
140264
140265
140266
140267
140268
140269
140270
140271
140272
140273
140274
140275
140276
140277
140278
140279
140280
140281
140282
140283
140284
140285
140286
140287
140288
140289
140290
140291
140292
140293
140294
140295
140296
140297
140298
140299
140300
140301
140302
140303
140304
140305
140306
140307
140308
140309
140310
140311
140312
140313
140314
140315
140316
140317
140318
140319
140320
140321
140322
140323
140324
140325
140326
140327
140328
140329
140330
140331
140332
140333
140334
140335
140336
140337
140338
140339
140340
140341
140342
140343
140344
140345
140346
140347
140348
140349
140350
140351
140352
140353
140354
140355
140356
140357
140358
140359
140360
140361
140362
140363
140364
140365
140366
140367
140368
140369
140370
140371
140372
140373
140374
140375
140376
140377
140378
140379
140380
140381
140382
140383
140384
140385
140386
140387
140388
140389
140390
140391
140392
140393
140394
140395
140396
140397
140398
140399
140400
140401
140402
140403
140404
140405
140406
140407
140408
140409
140410
140411
140412
140413
140414
140415
140416
140417
140418
140419
140420
140421
140422
140423
140424
140425
140426
140427
140428
140429
140430
140431
140432
140433
140434
140435
140436
140437
140438
140439
140440
140441
140442
140443
140444
140445
140446
140447
140448
140449
140450
140451
140452
140453
140454
140455
140456
140457
140458
140459
140460
140461
140462
140463
140464
140465
140466
140467
140468
140469
140470
140471
140472
140473
140474
140475
140476
140477
140478
140479
140480
140481
140482
140483
140484
140485
140486
140487
140488
140489
140490
140491
140492
140493
140494
140495
140496
140497
140498
140499
140500
140501
140502
140503
140504
140505
140506
140507
140508
140509
140510
140511
140512
140513
140514
140515
140516
140517
140518
140519
140520
140521
140522
140523
140524
140525
140526
140527
140528
140529
140530
140531
140532
140533
140534
140535
140536
140537
140538
140539
140540
140541
140542
140543
140544
140545
140546
140547
140548
140549
140550
140551
140552
140553
140554
140555
140556
140557
140558
140559
140560
140561
140562
140563
140564
140565
140566
140567
140568
140569
140570
140571
140572
140573
140574
140575
140576
140577
140578
140579
140580
140581
140582
140583
140584
140585
140586
140587
140588
140589
140590
140591
140592
140593
140594
140595
140596
140597
140598
140599
140600
140601
140602
140603
140604
140605
140606
140607
140608
140609
140610
140611
140612
140613
140614
140615
140616
140617
140618
140619
140620
140621
140622
140623
140624
140625
140626
140627
140628
140629
140630
140631
140632
140633
140634
140635
140636
140637
140638
140639
140640
140641
140642
140643
140644
140645
140646
140647
140648
140649
140650
140651
140652
140653
140654
140655
140656
140657
140658
140659
140660
140661
140662
140663
140664
140665
140666
140667
140668
140669
140670
140671
140672
140673
140674
140675
140676
140677
140678
140679
140680
140681
140682
140683
140684
140685
140686
140687
140688
140689
140690
140691
140692
140693
140694
140695
140696
140697
140698
140699
140700
140701
140702
140703
140704
140705
140706
140707
140708
140709
140710
140711
140712
140713
140714
140715
140716
140717
140718
140719
140720
140721
140722
140723
140724
140725
140726
140727
140728
140729
140730
140731
140732
140733
140734
140735
140736
140737
140738
140739
140740
140741
140742
140743
140744
140745
140746
140747
140748
140749
140750
140751
140752
140753
140754
140755
140756
140757
140758
140759
140760
140761
140762
140763
140764
140765
140766
140767
140768
140769
140770
140771
140772
140773
140774
140775
140776
140777
140778
140779
140780
140781
140782
140783
140784
140785
140786
140787
140788
140789
140790
140791
140792
140793
140794
140795
140796
140797
140798
140799
140800
140801
140802
140803
140804
140805
140806
140807
140808
140809
140810
140811
140812
140813
140814
140815
140816
140817
140818
140819
140820
140821
140822
140823
140824
140825
140826
140827
140828
140829
140830
140831
140832
140833
140834
140835
140836
140837
140838
140839
140840
140841
140842
140843
140844
140845
140846
140847
140848
140849
140850
140851
140852
140853
140854
140855
140856
140857
140858
140859
140860
140861
140862
140863
140864
140865
140866
140867
140868
140869
140870
140871
140872
140873
140874
140875
140876
140877
140878
140879
140880
140881
140882
140883
140884
140885
140886
140887
140888
140889
140890
140891
140892
140893
140894
140895
140896
140897
140898
140899
140900
140901
140902
140903
140904
140905
140906
140907
140908
140909
140910
140911
140912
140913
140914
140915
140916
140917
140918
140919
140920
140921
140922
140923
140924
140925
140926
140927
140928
140929
140930
140931
140932
140933
140934
140935
140936
140937
140938
140939
140940
140941
140942
140943
140944
140945
140946
140947
140948
140949
140950
140951
140952
140953
140954
140955
140956
140957
140958
140959
140960
140961
140962
140963
140964
140965
140966
140967
140968
140969
140970
140971
140972
140973
140974
140975
140976
140977
140978
140979
140980
140981
140982
140983
140984
140985
140986
140987
140988
140989
140990
140991
140992
140993
140994
140995
140996
140997
140998
140999
141000
141001
141002
141003
141004
141005
141006
141007
141008
141009
141010
141011
141012
141013
141014
141015
141016
141017
141018
141019
141020
141021
141022
141023
141024
141025
141026
141027
141028
141029
141030
141031
141032
141033
141034
141035
141036
141037
141038
141039
141040
141041
141042
141043
141044
141045
141046
141047
141048
141049
141050
141051
141052
141053
141054
141055
141056
141057
141058
141059
141060
141061
141062
141063
141064
141065
141066
141067
141068
141069
141070
141071
141072
141073
141074
141075
141076
141077
141078
141079
141080
141081
141082
141083
141084
141085
141086
141087
141088
141089
141090
141091
141092
141093
141094
141095
141096
141097
141098
141099
141100
141101
141102
141103
141104
141105
141106
141107
141108
141109
141110
141111
141112
141113
141114
141115
141116
141117
141118
141119
141120
141121
141122
141123
141124
141125
141126
141127
141128
141129
141130
141131
141132
141133
141134
141135
141136
141137
141138
141139
141140
141141
141142
141143
141144
141145
141146
141147
141148
141149
141150
141151
141152
141153
141154
141155
141156
141157
141158
141159
141160
141161
141162
141163
141164
141165
141166
141167
141168
141169
141170
141171
141172
141173
141174
141175
141176
141177
141178
141179
141180
141181
141182
141183
141184
141185
141186
141187
141188
141189
141190
141191
141192
141193
141194
141195
141196
141197
141198
141199
141200
141201
141202
141203
141204
141205
141206
141207
141208
141209
141210
141211
141212
141213
141214
141215
141216
141217
141218
141219
141220
141221
141222
141223
141224
141225
141226
141227
141228
141229
141230
141231
141232
141233
141234
141235
141236
141237
141238
141239
141240
141241
141242
141243
141244
141245
141246
141247
141248
141249
141250
141251
141252
141253
141254
141255
141256
141257
141258
141259
141260
141261
141262
141263
141264
141265
141266
141267
141268
141269
141270
141271
141272
141273
141274
141275
141276
141277
141278
141279
141280
141281
141282
141283
141284
141285
141286
141287
141288
141289
141290
141291
141292
141293
141294
141295
141296
141297
141298
141299
141300
141301
141302
141303
141304
141305
141306
141307
141308
141309
141310
141311
141312
141313
141314
141315
141316
141317
141318
141319
141320
141321
141322
141323
141324
141325
141326
141327
141328
141329
141330
141331
141332
141333
141334
141335
141336
141337
141338
141339
141340
141341
141342
141343
141344
141345
141346
141347
141348
141349
141350
141351
141352
141353
141354
141355
141356
141357
141358
141359
141360
141361
141362
141363
141364
141365
141366
141367
141368
141369
141370
141371
141372
141373
141374
141375
141376
141377
141378
141379
141380
141381
141382
141383
141384
141385
141386
141387
141388
141389
141390
141391
141392
141393
141394
141395
141396
141397
141398
141399
141400
141401
141402
141403
141404
141405
141406
141407
141408
141409
141410
141411
141412
141413
141414
141415
141416
141417
141418
141419
141420
141421
141422
141423
141424
141425
141426
141427
141428
141429
141430
141431
141432
141433
141434
141435
141436
141437
141438
141439
141440
141441
141442
141443
141444
141445
141446
141447
141448
141449
141450
141451
141452
141453
141454
141455
141456
141457
141458
141459
141460
141461
141462
141463
141464
141465
141466
141467
141468
141469
141470
141471
141472
141473
141474
141475
141476
141477
141478
141479
141480
141481
141482
141483
141484
141485
141486
141487
141488
141489
141490
141491
141492
141493
141494
141495
141496
141497
141498
141499
141500
141501
141502
141503
141504
141505
141506
141507
141508
141509
141510
141511
141512
141513
141514
141515
141516
141517
141518
141519
141520
141521
141522
141523
141524
141525
141526
141527
141528
141529
141530
141531
141532
141533
141534
141535
141536
141537
141538
141539
141540
141541
141542
141543
141544
141545
141546
141547
141548
141549
141550
141551
141552
141553
141554
141555
141556
141557
141558
141559
141560
141561
141562
141563
141564
141565
141566
141567
141568
141569
141570
141571
141572
141573
141574
141575
141576
141577
141578
141579
141580
141581
141582
141583
141584
141585
141586
141587
141588
141589
141590
141591
141592
141593
141594
141595
141596
141597
141598
141599
141600
141601
141602
141603
141604
141605
141606
141607
141608
141609
141610
141611
141612
141613
141614
141615
141616
141617
141618
141619
141620
141621
141622
141623
141624
141625
141626
141627
141628
141629
141630
141631
141632
141633
141634
141635
141636
141637
141638
141639
141640
141641
141642
141643
141644
141645
141646
141647
141648
141649
141650
141651
141652
141653
141654
141655
141656
141657
141658
141659
141660
141661
141662
141663
141664
141665
141666
141667
141668
141669
141670
141671
141672
141673
141674
141675
141676
141677
141678
141679
141680
141681
141682
141683
141684
141685
141686
141687
141688
141689
141690
141691
141692
141693
141694
141695
141696
141697
141698
141699
141700
141701
141702
141703
141704
141705
141706
141707
141708
141709
141710
141711
141712
141713
141714
141715
141716
141717
141718
141719
141720
141721
141722
141723
141724
141725
141726
141727
141728
141729
141730
141731
141732
141733
141734
141735
141736
141737
141738
141739
141740
141741
141742
141743
141744
141745
141746
141747
141748
141749
141750
141751
141752
141753
141754
141755
141756
141757
141758
141759
141760
141761
141762
141763
141764
141765
141766
141767
141768
141769
141770
141771
141772
141773
141774
141775
141776
141777
141778
141779
141780
141781
141782
141783
141784
141785
141786
141787
141788
141789
141790
141791
141792
141793
141794
141795
141796
141797
141798
141799
141800
141801
141802
141803
141804
141805
141806
141807
141808
141809
141810
141811
141812
141813
141814
141815
141816
141817
141818
141819
141820
141821
141822
141823
141824
141825
141826
141827
141828
141829
141830
141831
141832
141833
141834
141835
141836
141837
141838
141839
141840
141841
141842
141843
141844
141845
141846
141847
141848
141849
141850
141851
141852
141853
141854
141855
141856
141857
141858
141859
141860
141861
141862
141863
141864
141865
141866
141867
141868
141869
141870
141871
141872
141873
141874
141875
141876
141877
141878
141879
141880
141881
141882
141883
141884
141885
141886
141887
141888
141889
141890
141891
141892
141893
141894
141895
141896
141897
141898
141899
141900
141901
141902
141903
141904
141905
141906
141907
141908
141909
141910
141911
141912
141913
141914
141915
141916
141917
141918
141919
141920
141921
141922
141923
141924
141925
141926
141927
141928
141929
141930
141931
141932
141933
141934
141935
141936
141937
141938
141939
141940
141941
141942
141943
141944
141945
141946
141947
141948
141949
141950
141951
141952
141953
141954
141955
141956
141957
141958
141959
141960
141961
141962
141963
141964
141965
141966
141967
141968
141969
141970
141971
141972
141973
141974
141975
141976
141977
141978
141979
141980
141981
141982
141983
141984
141985
141986
141987
141988
141989
141990
141991
141992
141993
141994
141995
141996
141997
141998
141999
142000
142001
142002
142003
142004
142005
142006
142007
142008
142009
142010
142011
142012
142013
142014
142015
142016
142017
142018
142019
142020
142021
142022
142023
142024
142025
142026
142027
142028
142029
142030
142031
142032
142033
142034
142035
142036
142037
142038
142039
142040
142041
142042
142043
142044
142045
142046
142047
142048
142049
142050
142051
142052
142053
142054
142055
142056
142057
142058
142059
142060
142061
142062
142063
142064
142065
142066
142067
142068
142069
142070
142071
142072
142073
142074
142075
142076
142077
142078
142079
142080
142081
142082
142083
142084
142085
142086
142087
142088
142089
142090
142091
142092
142093
142094
142095
142096
142097
142098
142099
142100
142101
142102
142103
142104
142105
142106
142107
142108
142109
142110
142111
142112
142113
142114
142115
142116
142117
142118
142119
142120
142121
142122
142123
142124
142125
142126
142127
142128
142129
142130
142131
142132
142133
142134
142135
142136
142137
142138
142139
142140
142141
142142
142143
142144
142145
142146
142147
142148
142149
142150
142151
142152
142153
142154
142155
142156
142157
142158
142159
142160
142161
142162
142163
142164
142165
142166
142167
142168
142169
142170
142171
142172
142173
142174
142175
142176
142177
142178
142179
142180
142181
142182
142183
142184
142185
142186
142187
142188
142189
142190
142191
142192
142193
142194
142195
142196
142197
142198
142199
142200
142201
142202
142203
142204
142205
142206
142207
142208
142209
142210
142211
142212
142213
142214
142215
142216
142217
142218
142219
142220
142221
142222
142223
142224
142225
142226
142227
142228
142229
142230
142231
142232
142233
142234
142235
142236
142237
142238
142239
142240
142241
142242
142243
142244
142245
142246
142247
142248
142249
142250
142251
142252
142253
142254
142255
142256
142257
142258
142259
142260
142261
142262
142263
142264
142265
142266
142267
142268
142269
142270
142271
142272
142273
142274
142275
142276
142277
142278
142279
142280
142281
142282
142283
142284
142285
142286
142287
142288
142289
142290
142291
142292
142293
142294
142295
142296
142297
142298
142299
142300
142301
142302
142303
142304
142305
142306
142307
142308
142309
142310
142311
142312
142313
142314
142315
142316
142317
142318
142319
142320
142321
142322
142323
142324
142325
142326
142327
142328
142329
142330
142331
142332
142333
142334
142335
142336
142337
142338
142339
142340
142341
142342
142343
142344
142345
142346
142347
142348
142349
142350
142351
142352
142353
142354
142355
142356
142357
142358
142359
142360
142361
142362
142363
142364
142365
142366
142367
142368
142369
142370
142371
142372
142373
142374
142375
142376
142377
142378
142379
142380
142381
142382
142383
142384
142385
142386
142387
142388
142389
142390
142391
142392
142393
142394
142395
142396
142397
142398
142399
142400
142401
142402
142403
142404
142405
142406
142407
142408
142409
142410
142411
142412
142413
142414
142415
142416
142417
142418
142419
142420
142421
142422
142423
142424
142425
142426
142427
142428
142429
142430
142431
142432
142433
142434
142435
142436
142437
142438
142439
142440
142441
142442
142443
142444
142445
142446
142447
142448
142449
142450
142451
142452
142453
142454
142455
142456
142457
142458
142459
142460
142461
142462
142463
142464
142465
142466
142467
142468
142469
142470
142471
142472
142473
142474
142475
142476
142477
142478
142479
142480
142481
142482
142483
142484
142485
142486
142487
142488
142489
142490
142491
142492
142493
142494
142495
142496
142497
142498
142499
142500
142501
142502
142503
142504
142505
142506
142507
142508
142509
142510
142511
142512
142513
142514
142515
142516
142517
142518
142519
142520
142521
142522
142523
142524
142525
142526
142527
142528
142529
142530
142531
142532
142533
142534
142535
142536
142537
142538
142539
142540
142541
142542
142543
142544
142545
142546
142547
142548
142549
142550
142551
142552
142553
142554
142555
142556
142557
142558
142559
142560
142561
142562
142563
142564
142565
142566
142567
142568
142569
142570
142571
142572
142573
142574
142575
142576
142577
142578
142579
142580
142581
142582
142583
142584
142585
142586
142587
142588
142589
142590
142591
142592
142593
142594
142595
142596
142597
142598
142599
142600
142601
142602
142603
142604
142605
142606
142607
142608
142609
142610
142611
142612
142613
142614
142615
142616
142617
142618
142619
142620
142621
142622
142623
142624
142625
142626
142627
142628
142629
142630
142631
142632
142633
142634
142635
142636
142637
142638
142639
142640
142641
142642
142643
142644
142645
142646
142647
142648
142649
142650
142651
142652
142653
142654
142655
142656
142657
142658
142659
142660
142661
142662
142663
142664
142665
142666
142667
142668
142669
142670
142671
142672
142673
142674
142675
142676
142677
142678
142679
142680
142681
142682
142683
142684
142685
142686
142687
142688
142689
142690
142691
142692
142693
142694
142695
142696
142697
142698
142699
142700
142701
142702
142703
142704
142705
142706
142707
142708
142709
142710
142711
142712
142713
142714
142715
142716
142717
142718
142719
142720
142721
142722
142723
142724
142725
142726
142727
142728
142729
142730
142731
142732
142733
142734
142735
142736
142737
142738
142739
142740
142741
142742
142743
142744
142745
142746
142747
142748
142749
142750
142751
142752
142753
142754
142755
142756
142757
142758
142759
142760
142761
142762
142763
142764
142765
142766
142767
142768
142769
142770
142771
142772
142773
142774
142775
142776
142777
142778
142779
142780
142781
142782
142783
142784
142785
142786
142787
142788
142789
142790
142791
142792
142793
142794
142795
142796
142797
142798
142799
142800
142801
142802
142803
142804
142805
142806
142807
142808
142809
142810
142811
142812
142813
142814
142815
142816
142817
142818
142819
142820
142821
142822
142823
142824
142825
142826
142827
142828
142829
142830
142831
142832
142833
142834
142835
142836
142837
142838
142839
142840
142841
142842
142843
142844
142845
142846
142847
142848
142849
142850
142851
142852
142853
142854
142855
142856
142857
142858
142859
142860
142861
142862
142863
142864
142865
142866
142867
142868
142869
142870
142871
142872
142873
142874
142875
142876
142877
142878
142879
142880
142881
142882
142883
142884
142885
142886
142887
142888
142889
142890
142891
142892
142893
142894
142895
142896
142897
142898
142899
142900
142901
142902
142903
142904
142905
142906
142907
142908
142909
142910
142911
142912
142913
142914
142915
142916
142917
142918
142919
142920
142921
142922
142923
142924
142925
142926
142927
142928
142929
142930
142931
142932
142933
142934
142935
142936
142937
142938
142939
142940
142941
142942
142943
142944
142945
142946
142947
142948
142949
142950
142951
142952
142953
142954
142955
142956
142957
142958
142959
142960
142961
142962
142963
142964
142965
142966
142967
142968
142969
142970
142971
142972
142973
142974
142975
142976
142977
142978
142979
142980
142981
142982
142983
142984
142985
142986
142987
142988
142989
142990
142991
142992
142993
142994
142995
142996
142997
142998
142999
143000
143001
143002
143003
143004
143005
143006
143007
143008
143009
143010
143011
143012
143013
143014
143015
143016
143017
143018
143019
143020
143021
143022
143023
143024
143025
143026
143027
143028
143029
143030
143031
143032
143033
143034
143035
143036
143037
143038
143039
143040
143041
143042
143043
143044
143045
143046
143047
143048
143049
143050
143051
143052
143053
143054
143055
143056
143057
143058
143059
143060
143061
143062
143063
143064
143065
143066
143067
143068
143069
143070
143071
143072
143073
143074
143075
143076
143077
143078
143079
143080
143081
143082
143083
143084
143085
143086
143087
143088
143089
143090
143091
143092
143093
143094
143095
143096
143097
143098
143099
143100
143101
143102
143103
143104
143105
143106
143107
143108
143109
143110
143111
143112
143113
143114
143115
143116
143117
143118
143119
143120
143121
143122
143123
143124
143125
143126
143127
143128
143129
143130
143131
143132
143133
143134
143135
143136
143137
143138
143139
143140
143141
143142
143143
143144
143145
143146
143147
143148
143149
143150
143151
143152
143153
143154
143155
143156
143157
143158
143159
143160
143161
143162
143163
143164
143165
143166
143167
143168
143169
143170
143171
143172
143173
143174
143175
143176
143177
143178
143179
143180
143181
143182
143183
143184
143185
143186
143187
143188
143189
143190
143191
143192
143193
143194
143195
143196
143197
143198
143199
143200
143201
143202
143203
143204
143205
143206
143207
143208
143209
143210
143211
143212
143213
143214
143215
143216
143217
143218
143219
143220
143221
143222
143223
143224
143225
143226
143227
143228
143229
143230
143231
143232
143233
143234
143235
143236
143237
143238
143239
143240
143241
143242
143243
143244
143245
143246
143247
143248
143249
143250
143251
143252
143253
143254
143255
143256
143257
143258
143259
143260
143261
143262
143263
143264
143265
143266
143267
143268
143269
143270
143271
143272
143273
143274
143275
143276
143277
143278
143279
143280
143281
143282
143283
143284
143285
143286
143287
143288
143289
143290
143291
143292
143293
143294
143295
143296
143297
143298
143299
143300
143301
143302
143303
143304
143305
143306
143307
143308
143309
143310
143311
143312
143313
143314
143315
143316
143317
143318
143319
143320
143321
143322
143323
143324
143325
143326
143327
143328
143329
143330
143331
143332
143333
143334
143335
143336
143337
143338
143339
143340
143341
143342
143343
143344
143345
143346
143347
143348
143349
143350
143351
143352
143353
143354
143355
143356
143357
143358
143359
143360
143361
143362
143363
143364
143365
143366
143367
143368
143369
143370
143371
143372
143373
143374
143375
143376
143377
143378
143379
143380
143381
143382
143383
143384
143385
143386
143387
143388
143389
143390
143391
143392
143393
143394
143395
143396
143397
143398
143399
143400
143401
143402
143403
143404
143405
143406
143407
143408
143409
143410
143411
143412
143413
143414
143415
143416
143417
143418
143419
143420
143421
143422
143423
143424
143425
143426
143427
143428
143429
143430
143431
143432
143433
143434
143435
143436
143437
143438
143439
143440
143441
143442
143443
143444
143445
143446
143447
143448
143449
143450
143451
143452
143453
143454
143455
143456
143457
143458
143459
143460
143461
143462
143463
143464
143465
143466
143467
143468
143469
143470
143471
143472
143473
143474
143475
143476
143477
143478
143479
143480
143481
143482
143483
143484
143485
143486
143487
143488
143489
143490
143491
143492
143493
143494
143495
143496
143497
143498
143499
143500
143501
143502
143503
143504
143505
143506
143507
143508
143509
143510
143511
143512
143513
143514
143515
143516
143517
143518
143519
143520
143521
143522
143523
143524
143525
143526
143527
143528
143529
143530
143531
143532
143533
143534
143535
143536
143537
143538
143539
143540
143541
143542
143543
143544
143545
143546
143547
143548
143549
143550
143551
143552
143553
143554
143555
143556
143557
143558
143559
143560
143561
143562
143563
143564
143565
143566
143567
143568
143569
143570
143571
143572
143573
143574
143575
143576
143577
143578
143579
143580
143581
143582
143583
143584
143585
143586
143587
143588
143589
143590
143591
143592
143593
143594
143595
143596
143597
143598
143599
143600
143601
143602
143603
143604
143605
143606
143607
143608
143609
143610
143611
143612
143613
143614
143615
143616
143617
143618
143619
143620
143621
143622
143623
143624
143625
143626
143627
143628
143629
143630
143631
143632
143633
143634
143635
143636
143637
143638
143639
143640
143641
143642
143643
143644
143645
143646
143647
143648
143649
143650
143651
143652
143653
143654
143655
143656
143657
143658
143659
143660
143661
143662
143663
143664
143665
143666
143667
143668
143669
143670
143671
143672
143673
143674
143675
143676
143677
143678
143679
143680
143681
143682
143683
143684
143685
143686
143687
143688
143689
143690
143691
143692
143693
143694
143695
143696
143697
143698
143699
143700
143701
143702
143703
143704
143705
143706
143707
143708
143709
143710
143711
143712
143713
143714
143715
143716
143717
143718
143719
143720
143721
143722
143723
143724
143725
143726
143727
143728
143729
143730
143731
143732
143733
143734
143735
143736
143737
143738
143739
143740
143741
143742
143743
143744
143745
143746
143747
143748
143749
143750
143751
143752
143753
143754
143755
143756
143757
143758
143759
143760
143761
143762
143763
143764
143765
143766
143767
143768
143769
143770
143771
143772
143773
143774
143775
143776
143777
143778
143779
143780
143781
143782
143783
143784
143785
143786
143787
143788
143789
143790
143791
143792
143793
143794
143795
143796
143797
143798
143799
143800
143801
143802
143803
143804
143805
143806
143807
143808
143809
143810
143811
143812
143813
143814
143815
143816
143817
143818
143819
143820
143821
143822
143823
143824
143825
143826
143827
143828
143829
143830
143831
143832
143833
143834
143835
143836
143837
143838
143839
143840
143841
143842
143843
143844
143845
143846
143847
143848
143849
143850
143851
143852
143853
143854
143855
143856
143857
143858
143859
143860
143861
143862
143863
143864
143865
143866
143867
143868
143869
143870
143871
143872
143873
143874
143875
143876
143877
143878
143879
143880
143881
143882
143883
143884
143885
143886
143887
143888
143889
143890
143891
143892
143893
143894
143895
143896
143897
143898
143899
143900
143901
143902
143903
143904
143905
143906
143907
143908
143909
143910
143911
143912
143913
143914
143915
143916
143917
143918
143919
143920
143921
143922
143923
143924
143925
143926
143927
143928
143929
143930
143931
143932
143933
143934
143935
143936
143937
143938
143939
143940
143941
143942
143943
143944
143945
143946
143947
143948
143949
143950
143951
143952
143953
143954
143955
143956
143957
143958
143959
143960
143961
143962
143963
143964
143965
143966
143967
143968
143969
143970
143971
143972
143973
143974
143975
143976
143977
143978
143979
143980
143981
143982
143983
143984
143985
143986
143987
143988
143989
143990
143991
143992
143993
143994
143995
143996
143997
143998
143999
144000
144001
144002
144003
144004
144005
144006
144007
144008
144009
144010
144011
144012
144013
144014
144015
144016
144017
144018
144019
144020
144021
144022
144023
144024
144025
144026
144027
144028
144029
144030
144031
144032
144033
144034
144035
144036
144037
144038
144039
144040
144041
144042
144043
144044
144045
144046
144047
144048
144049
144050
144051
144052
144053
144054
144055
144056
144057
144058
144059
144060
144061
144062
144063
144064
144065
144066
144067
144068
144069
144070
144071
144072
144073
144074
144075
144076
144077
144078
144079
144080
144081
144082
144083
144084
144085
144086
144087
144088
144089
144090
144091
144092
144093
144094
144095
144096
144097
144098
144099
144100
144101
144102
144103
144104
144105
144106
144107
144108
144109
144110
144111
144112
144113
144114
144115
144116
144117
144118
144119
144120
144121
144122
144123
144124
144125
144126
144127
144128
144129
144130
144131
144132
144133
144134
144135
144136
144137
144138
144139
144140
144141
144142
144143
144144
144145
144146
144147
144148
144149
144150
144151
144152
144153
144154
144155
144156
144157
144158
144159
144160
144161
144162
144163
144164
144165
144166
144167
144168
144169
144170
144171
144172
144173
144174
144175
144176
144177
144178
144179
144180
144181
144182
144183
144184
144185
144186
144187
144188
144189
144190
144191
144192
144193
144194
144195
144196
144197
144198
144199
144200
144201
144202
144203
144204
144205
144206
144207
144208
144209
144210
144211
144212
144213
144214
144215
144216
144217
144218
144219
144220
144221
144222
144223
144224
144225
144226
144227
144228
144229
144230
144231
144232
144233
144234
144235
144236
144237
144238
144239
144240
144241
144242
144243
144244
144245
144246
144247
144248
144249
144250
144251
144252
144253
144254
144255
144256
144257
144258
144259
144260
144261
144262
144263
144264
144265
144266
144267
144268
144269
144270
144271
144272
144273
144274
144275
144276
144277
144278
144279
144280
144281
144282
144283
144284
144285
144286
144287
144288
144289
144290
144291
144292
144293
144294
144295
144296
144297
144298
144299
144300
144301
144302
144303
144304
144305
144306
144307
144308
144309
144310
144311
144312
144313
144314
144315
144316
144317
144318
144319
144320
144321
144322
144323
144324
144325
144326
144327
144328
144329
144330
144331
144332
144333
144334
144335
144336
144337
144338
144339
144340
144341
144342
144343
144344
144345
144346
144347
144348
144349
144350
144351
144352
144353
144354
144355
144356
144357
144358
144359
144360
144361
144362
144363
144364
144365
144366
144367
144368
144369
144370
144371
144372
144373
144374
144375
144376
144377
144378
144379
144380
144381
144382
144383
144384
144385
144386
144387
144388
144389
144390
144391
144392
144393
144394
144395
144396
144397
144398
144399
144400
144401
144402
144403
144404
144405
144406
144407
144408
144409
144410
144411
144412
144413
144414
144415
144416
144417
144418
144419
144420
144421
144422
144423
144424
144425
144426
144427
144428
144429
144430
144431
144432
144433
144434
144435
144436
144437
144438
144439
144440
144441
144442
144443
144444
144445
144446
144447
144448
144449
144450
144451
144452
144453
144454
144455
144456
144457
144458
144459
144460
144461
144462
144463
144464
144465
144466
144467
144468
144469
144470
144471
144472
144473
144474
144475
144476
144477
144478
144479
144480
144481
144482
144483
144484
144485
144486
144487
144488
144489
144490
144491
144492
144493
144494
144495
144496
144497
144498
144499
144500
144501
144502
144503
144504
144505
144506
144507
144508
144509
144510
144511
144512
144513
144514
144515
144516
144517
144518
144519
144520
144521
144522
144523
144524
144525
144526
144527
144528
144529
144530
144531
144532
144533
144534
144535
144536
144537
144538
144539
144540
144541
144542
144543
144544
144545
144546
144547
144548
144549
144550
144551
144552
144553
144554
144555
144556
144557
144558
144559
144560
144561
144562
144563
144564
144565
144566
144567
144568
144569
144570
144571
144572
144573
144574
144575
144576
144577
144578
144579
144580
144581
144582
144583
144584
144585
144586
144587
144588
144589
144590
144591
144592
144593
144594
144595
144596
144597
144598
144599
144600
144601
144602
144603
144604
144605
144606
144607
144608
144609
144610
144611
144612
144613
144614
144615
144616
144617
144618
144619
144620
144621
144622
144623
144624
144625
144626
144627
144628
144629
144630
144631
144632
144633
144634
144635
144636
144637
144638
144639
144640
144641
144642
144643
144644
144645
144646
144647
144648
144649
144650
144651
144652
144653
144654
144655
144656
144657
144658
144659
144660
144661
144662
144663
144664
144665
144666
144667
144668
144669
144670
144671
144672
144673
144674
144675
144676
144677
144678
144679
144680
144681
144682
144683
144684
144685
144686
144687
144688
144689
144690
144691
144692
144693
144694
144695
144696
144697
144698
144699
144700
144701
144702
144703
144704
144705
144706
144707
144708
144709
144710
144711
144712
144713
144714
144715
144716
144717
144718
144719
144720
144721
144722
144723
144724
144725
144726
144727
144728
144729
144730
144731
144732
144733
144734
144735
144736
144737
144738
144739
144740
144741
144742
144743
144744
144745
144746
144747
144748
144749
144750
144751
144752
144753
144754
144755
144756
144757
144758
144759
144760
144761
144762
144763
144764
144765
144766
144767
144768
144769
144770
144771
144772
144773
144774
144775
144776
144777
144778
144779
144780
144781
144782
144783
144784
144785
144786
144787
144788
144789
144790
144791
144792
144793
144794
144795
144796
144797
144798
144799
144800
144801
144802
144803
144804
144805
144806
144807
144808
144809
144810
144811
144812
144813
144814
144815
144816
144817
144818
144819
144820
144821
144822
144823
144824
144825
144826
144827
144828
144829
144830
144831
144832
144833
144834
144835
144836
144837
144838
144839
144840
144841
144842
144843
144844
144845
144846
144847
144848
144849
144850
144851
144852
144853
144854
144855
144856
144857
144858
144859
144860
144861
144862
144863
144864
144865
144866
144867
144868
144869
144870
144871
144872
144873
144874
144875
144876
144877
144878
144879
144880
144881
144882
144883
144884
144885
144886
144887
144888
144889
144890
144891
144892
144893
144894
144895
144896
144897
144898
144899
144900
144901
144902
144903
144904
144905
144906
144907
144908
144909
144910
144911
144912
144913
144914
144915
144916
144917
144918
144919
144920
144921
144922
144923
144924
144925
144926
144927
144928
144929
144930
144931
144932
144933
144934
144935
144936
144937
144938
144939
144940
144941
144942
144943
144944
144945
144946
144947
144948
144949
144950
144951
144952
144953
144954
144955
144956
144957
144958
144959
144960
144961
144962
144963
144964
144965
144966
144967
144968
144969
144970
144971
144972
144973
144974
144975
144976
144977
144978
144979
144980
144981
144982
144983
144984
144985
144986
144987
144988
144989
144990
144991
144992
144993
144994
144995
144996
144997
144998
144999
145000
145001
145002
145003
145004
145005
145006
145007
145008
145009
145010
145011
145012
145013
145014
145015
145016
145017
145018
145019
145020
145021
145022
145023
145024
145025
145026
145027
145028
145029
145030
145031
145032
145033
145034
145035
145036
145037
145038
145039
145040
145041
145042
145043
145044
145045
145046
145047
145048
145049
145050
145051
145052
145053
145054
145055
145056
145057
145058
145059
145060
145061
145062
145063
145064
145065
145066
145067
145068
145069
145070
145071
145072
145073
145074
145075
145076
145077
145078
145079
145080
145081
145082
145083
145084
145085
145086
145087
145088
145089
145090
145091
145092
145093
145094
145095
145096
145097
145098
145099
145100
145101
145102
145103
145104
145105
145106
145107
145108
145109
145110
145111
145112
145113
145114
145115
145116
145117
145118
145119
145120
145121
145122
145123
145124
145125
145126
145127
145128
145129
145130
145131
145132
145133
145134
145135
145136
145137
145138
145139
145140
145141
145142
145143
145144
145145
145146
145147
145148
145149
145150
145151
145152
145153
145154
145155
145156
145157
145158
145159
145160
145161
145162
145163
145164
145165
145166
145167
145168
145169
145170
145171
145172
145173
145174
145175
145176
145177
145178
145179
145180
145181
145182
145183
145184
145185
145186
145187
145188
145189
145190
145191
145192
145193
145194
145195
145196
145197
145198
145199
145200
145201
145202
145203
145204
145205
145206
145207
145208
145209
145210
145211
145212
145213
145214
145215
145216
145217
145218
145219
145220
145221
145222
145223
145224
145225
145226
145227
145228
145229
145230
145231
145232
145233
145234
145235
145236
145237
145238
145239
145240
145241
145242
145243
145244
145245
145246
145247
145248
145249
145250
145251
145252
145253
145254
145255
145256
145257
145258
145259
145260
145261
145262
145263
145264
145265
145266
145267
145268
145269
145270
145271
145272
145273
145274
145275
145276
145277
145278
145279
145280
145281
145282
145283
145284
145285
145286
145287
145288
145289
145290
145291
145292
145293
145294
145295
145296
145297
145298
145299
145300
145301
145302
145303
145304
145305
145306
145307
145308
145309
145310
145311
145312
145313
145314
145315
145316
145317
145318
145319
145320
145321
145322
145323
145324
145325
145326
145327
145328
145329
145330
145331
145332
145333
145334
145335
145336
145337
145338
145339
145340
145341
145342
145343
145344
145345
145346
145347
145348
145349
145350
145351
145352
145353
145354
145355
145356
145357
145358
145359
145360
145361
145362
145363
145364
145365
145366
145367
145368
145369
145370
145371
145372
145373
145374
145375
145376
145377
145378
145379
145380
145381
145382
145383
145384
145385
145386
145387
145388
145389
145390
145391
145392
145393
145394
145395
145396
145397
145398
145399
145400
145401
145402
145403
145404
145405
145406
145407
145408
145409
145410
145411
145412
145413
145414
145415
145416
145417
145418
145419
145420
145421
145422
145423
145424
145425
145426
145427
145428
145429
145430
145431
145432
145433
145434
145435
145436
145437
145438
145439
145440
145441
145442
145443
145444
145445
145446
145447
145448
145449
145450
145451
145452
145453
145454
145455
145456
145457
145458
145459
145460
145461
145462
145463
145464
145465
145466
145467
145468
145469
145470
145471
145472
145473
145474
145475
145476
145477
145478
145479
145480
145481
145482
145483
145484
145485
145486
145487
145488
145489
145490
145491
145492
145493
145494
145495
145496
145497
145498
145499
145500
145501
145502
145503
145504
145505
145506
145507
145508
145509
145510
145511
145512
145513
145514
145515
145516
145517
145518
145519
145520
145521
145522
145523
145524
145525
145526
145527
145528
145529
145530
145531
145532
145533
145534
145535
145536
145537
145538
145539
145540
145541
145542
145543
145544
145545
145546
145547
145548
145549
145550
145551
145552
145553
145554
145555
145556
145557
145558
145559
145560
145561
145562
145563
145564
145565
145566
145567
145568
145569
145570
145571
145572
145573
145574
145575
145576
145577
145578
145579
145580
145581
145582
145583
145584
145585
145586
145587
145588
145589
145590
145591
145592
145593
145594
145595
145596
145597
145598
145599
145600
145601
145602
145603
145604
145605
145606
145607
145608
145609
145610
145611
145612
145613
145614
145615
145616
145617
145618
145619
145620
145621
145622
145623
145624
145625
145626
145627
145628
145629
145630
145631
145632
145633
145634
145635
145636
145637
145638
145639
145640
145641
145642
145643
145644
145645
145646
145647
145648
145649
145650
145651
145652
145653
145654
145655
145656
145657
145658
145659
145660
145661
145662
145663
145664
145665
145666
145667
145668
145669
145670
145671
145672
145673
145674
145675
145676
145677
145678
145679
145680
145681
145682
145683
145684
145685
145686
145687
145688
145689
145690
145691
145692
145693
145694
145695
145696
145697
145698
145699
145700
145701
145702
145703
145704
145705
145706
145707
145708
145709
145710
145711
145712
145713
145714
145715
145716
145717
145718
145719
145720
145721
145722
145723
145724
145725
145726
145727
145728
145729
145730
145731
145732
145733
145734
145735
145736
145737
145738
145739
145740
145741
145742
145743
145744
145745
145746
145747
145748
145749
145750
145751
145752
145753
145754
145755
145756
145757
145758
145759
145760
145761
145762
145763
145764
145765
145766
145767
145768
145769
145770
145771
145772
145773
145774
145775
145776
145777
145778
145779
145780
145781
145782
145783
145784
145785
145786
145787
145788
145789
145790
145791
145792
145793
145794
145795
145796
145797
145798
145799
145800
145801
145802
145803
145804
145805
145806
145807
145808
145809
145810
145811
145812
145813
145814
145815
145816
145817
145818
145819
145820
145821
145822
145823
145824
145825
145826
145827
145828
145829
145830
145831
145832
145833
145834
145835
145836
145837
145838
145839
145840
145841
145842
145843
145844
145845
145846
145847
145848
145849
145850
145851
145852
145853
145854
145855
145856
145857
145858
145859
145860
145861
145862
145863
145864
145865
145866
145867
145868
145869
145870
145871
145872
145873
145874
145875
145876
145877
145878
145879
145880
145881
145882
145883
145884
145885
145886
145887
145888
145889
145890
145891
145892
145893
145894
145895
145896
145897
145898
145899
145900
145901
145902
145903
145904
145905
145906
145907
145908
145909
145910
145911
145912
145913
145914
145915
145916
145917
145918
145919
145920
145921
145922
145923
145924
145925
145926
145927
145928
145929
145930
145931
145932
145933
145934
145935
145936
145937
145938
145939
145940
145941
145942
145943
145944
145945
145946
145947
145948
145949
145950
145951
145952
145953
145954
145955
145956
145957
145958
145959
145960
145961
145962
145963
145964
145965
145966
145967
145968
145969
145970
145971
145972
145973
145974
145975
145976
145977
145978
145979
145980
145981
145982
145983
145984
145985
145986
145987
145988
145989
145990
145991
145992
145993
145994
145995
145996
145997
145998
145999
146000
146001
146002
146003
146004
146005
146006
146007
146008
146009
146010
146011
146012
146013
146014
146015
146016
146017
146018
146019
146020
146021
146022
146023
146024
146025
146026
146027
146028
146029
146030
146031
146032
146033
146034
146035
146036
146037
146038
146039
146040
146041
146042
146043
146044
146045
146046
146047
146048
146049
146050
146051
146052
146053
146054
146055
146056
146057
146058
146059
146060
146061
146062
146063
146064
146065
146066
146067
146068
146069
146070
146071
146072
146073
146074
146075
146076
146077
146078
146079
146080
146081
146082
146083
146084
146085
146086
146087
146088
146089
146090
146091
146092
146093
146094
146095
146096
146097
146098
146099
146100
146101
146102
146103
146104
146105
146106
146107
146108
146109
146110
146111
146112
146113
146114
146115
146116
146117
146118
146119
146120
146121
146122
146123
146124
146125
146126
146127
146128
146129
146130
146131
146132
146133
146134
146135
146136
146137
146138
146139
146140
146141
146142
146143
146144
146145
146146
146147
146148
146149
146150
146151
146152
146153
146154
146155
146156
146157
146158
146159
146160
146161
146162
146163
146164
146165
146166
146167
146168
146169
146170
146171
146172
146173
146174
146175
146176
146177
146178
146179
146180
146181
146182
146183
146184
146185
146186
146187
146188
146189
146190
146191
146192
146193
146194
146195
146196
146197
146198
146199
146200
146201
146202
146203
146204
146205
146206
146207
146208
146209
146210
146211
146212
146213
146214
146215
146216
146217
146218
146219
146220
146221
146222
146223
146224
146225
146226
146227
146228
146229
146230
146231
146232
146233
146234
146235
146236
146237
146238
146239
146240
146241
146242
146243
146244
146245
146246
146247
146248
146249
146250
146251
146252
146253
146254
146255
146256
146257
146258
146259
146260
146261
146262
146263
146264
146265
146266
146267
146268
146269
146270
146271
146272
146273
146274
146275
146276
146277
146278
146279
146280
146281
146282
146283
146284
146285
146286
146287
146288
146289
146290
146291
146292
146293
146294
146295
146296
146297
146298
146299
146300
146301
146302
146303
146304
146305
146306
146307
146308
146309
146310
146311
146312
146313
146314
146315
146316
146317
146318
146319
146320
146321
146322
146323
146324
146325
146326
146327
146328
146329
146330
146331
146332
146333
146334
146335
146336
146337
146338
146339
146340
146341
146342
146343
146344
146345
146346
146347
146348
146349
146350
146351
146352
146353
146354
146355
146356
146357
146358
146359
146360
146361
146362
146363
146364
146365
146366
146367
146368
146369
146370
146371
146372
146373
146374
146375
146376
146377
146378
146379
146380
146381
146382
146383
146384
146385
146386
146387
146388
146389
146390
146391
146392
146393
146394
146395
146396
146397
146398
146399
146400
146401
146402
146403
146404
146405
146406
146407
146408
146409
146410
146411
146412
146413
146414
146415
146416
146417
146418
146419
146420
146421
146422
146423
146424
146425
146426
146427
146428
146429
146430
146431
146432
146433
146434
146435
146436
146437
146438
146439
146440
146441
146442
146443
146444
146445
146446
146447
146448
146449
146450
146451
146452
146453
146454
146455
146456
146457
146458
146459
146460
146461
146462
146463
146464
146465
146466
146467
146468
146469
146470
146471
146472
146473
146474
146475
146476
146477
146478
146479
146480
146481
146482
146483
146484
146485
146486
146487
146488
146489
146490
146491
146492
146493
146494
146495
146496
146497
146498
146499
146500
146501
146502
146503
146504
146505
146506
146507
146508
146509
146510
146511
146512
146513
146514
146515
146516
146517
146518
146519
146520
146521
146522
146523
146524
146525
146526
146527
146528
146529
146530
146531
146532
146533
146534
146535
146536
146537
146538
146539
146540
146541
146542
146543
146544
146545
146546
146547
146548
146549
146550
146551
146552
146553
146554
146555
146556
146557
146558
146559
146560
146561
146562
146563
146564
146565
146566
146567
146568
146569
146570
146571
146572
146573
146574
146575
146576
146577
146578
146579
146580
146581
146582
146583
146584
146585
146586
146587
146588
146589
146590
146591
146592
146593
146594
146595
146596
146597
146598
146599
146600
146601
146602
146603
146604
146605
146606
146607
146608
146609
146610
146611
146612
146613
146614
146615
146616
146617
146618
146619
146620
146621
146622
146623
146624
146625
146626
146627
146628
146629
146630
146631
146632
146633
146634
146635
146636
146637
146638
146639
146640
146641
146642
146643
146644
146645
146646
146647
146648
146649
146650
146651
146652
146653
146654
146655
146656
146657
146658
146659
146660
146661
146662
146663
146664
146665
146666
146667
146668
146669
146670
146671
146672
146673
146674
146675
146676
146677
146678
146679
146680
146681
146682
146683
146684
146685
146686
146687
146688
146689
146690
146691
146692
146693
146694
146695
146696
146697
146698
146699
146700
146701
146702
146703
146704
146705
146706
146707
146708
146709
146710
146711
146712
146713
146714
146715
146716
146717
146718
146719
146720
146721
146722
146723
146724
146725
146726
146727
146728
146729
146730
146731
146732
146733
146734
146735
146736
146737
146738
146739
146740
146741
146742
146743
146744
146745
146746
146747
146748
146749
146750
146751
146752
146753
146754
146755
146756
146757
146758
146759
146760
146761
146762
146763
146764
146765
146766
146767
146768
146769
146770
146771
146772
146773
146774
146775
146776
146777
146778
146779
146780
146781
146782
146783
146784
146785
146786
146787
146788
146789
146790
146791
146792
146793
146794
146795
146796
146797
146798
146799
146800
146801
146802
146803
146804
146805
146806
146807
146808
146809
146810
146811
146812
146813
146814
146815
146816
146817
146818
146819
146820
146821
146822
146823
146824
146825
146826
146827
146828
146829
146830
146831
146832
146833
146834
146835
146836
146837
146838
146839
146840
146841
146842
146843
146844
146845
146846
146847
146848
146849
146850
146851
146852
146853
146854
146855
146856
146857
146858
146859
146860
146861
146862
146863
146864
146865
146866
146867
146868
146869
146870
146871
146872
146873
146874
146875
146876
146877
146878
146879
146880
146881
146882
146883
146884
146885
146886
146887
146888
146889
146890
146891
146892
146893
146894
146895
146896
146897
146898
146899
146900
146901
146902
146903
146904
146905
146906
146907
146908
146909
146910
146911
146912
146913
146914
146915
146916
146917
146918
146919
146920
146921
146922
146923
146924
146925
146926
146927
146928
146929
146930
146931
146932
146933
146934
146935
146936
146937
146938
146939
146940
146941
146942
146943
146944
146945
146946
146947
146948
146949
146950
146951
146952
146953
146954
146955
146956
146957
146958
146959
146960
146961
146962
146963
146964
146965
146966
146967
146968
146969
146970
146971
146972
146973
146974
146975
146976
146977
146978
146979
146980
146981
146982
146983
146984
146985
146986
146987
146988
146989
146990
146991
146992
146993
146994
146995
146996
146997
146998
146999
147000
147001
147002
147003
147004
147005
147006
147007
147008
147009
147010
147011
147012
147013
147014
147015
147016
147017
147018
147019
147020
147021
147022
147023
147024
147025
147026
147027
147028
147029
147030
147031
147032
147033
147034
147035
147036
147037
147038
147039
147040
147041
147042
147043
147044
147045
147046
147047
147048
147049
147050
147051
147052
147053
147054
147055
147056
147057
147058
147059
147060
147061
147062
147063
147064
147065
147066
147067
147068
147069
147070
147071
147072
147073
147074
147075
147076
147077
147078
147079
147080
147081
147082
147083
147084
147085
147086
147087
147088
147089
147090
147091
147092
147093
147094
147095
147096
147097
147098
147099
147100
147101
147102
147103
147104
147105
147106
147107
147108
147109
147110
147111
147112
147113
147114
147115
147116
147117
147118
147119
147120
147121
147122
147123
147124
147125
147126
147127
147128
147129
147130
147131
147132
147133
147134
147135
147136
147137
147138
147139
147140
147141
147142
147143
147144
147145
147146
147147
147148
147149
147150
147151
147152
147153
147154
147155
147156
147157
147158
147159
147160
147161
147162
147163
147164
147165
147166
147167
147168
147169
147170
147171
147172
147173
147174
147175
147176
147177
147178
147179
147180
147181
147182
147183
147184
147185
147186
147187
147188
147189
147190
147191
147192
147193
147194
147195
147196
147197
147198
147199
147200
147201
147202
147203
147204
147205
147206
147207
147208
147209
147210
147211
147212
147213
147214
147215
147216
147217
147218
147219
147220
147221
147222
147223
147224
147225
147226
147227
147228
147229
147230
147231
147232
147233
147234
147235
147236
147237
147238
147239
147240
147241
147242
147243
147244
147245
147246
147247
147248
147249
147250
147251
147252
147253
147254
147255
147256
147257
147258
147259
147260
147261
147262
147263
147264
147265
147266
147267
147268
147269
147270
147271
147272
147273
147274
147275
147276
147277
147278
147279
147280
147281
147282
147283
147284
147285
147286
147287
147288
147289
147290
147291
147292
147293
147294
147295
147296
147297
147298
147299
147300
147301
147302
147303
147304
147305
147306
147307
147308
147309
147310
147311
147312
147313
147314
147315
147316
147317
147318
147319
147320
147321
147322
147323
147324
147325
147326
147327
147328
147329
147330
147331
147332
147333
147334
147335
147336
147337
147338
147339
147340
147341
147342
147343
147344
147345
147346
147347
147348
147349
147350
147351
147352
147353
147354
147355
147356
147357
147358
147359
147360
147361
147362
147363
147364
147365
147366
147367
147368
147369
147370
147371
147372
147373
147374
147375
147376
147377
147378
147379
147380
147381
147382
147383
147384
147385
147386
147387
147388
147389
147390
147391
147392
147393
147394
147395
147396
147397
147398
147399
147400
147401
147402
147403
147404
147405
147406
147407
147408
147409
147410
147411
147412
147413
147414
147415
147416
147417
147418
147419
147420
147421
147422
147423
147424
147425
147426
147427
147428
147429
147430
147431
147432
147433
147434
147435
147436
147437
147438
147439
147440
147441
147442
147443
147444
147445
147446
147447
147448
147449
147450
147451
147452
147453
147454
147455
147456
147457
147458
147459
147460
147461
147462
147463
147464
147465
147466
147467
147468
147469
147470
147471
147472
147473
147474
147475
147476
147477
147478
147479
147480
147481
147482
147483
147484
147485
147486
147487
147488
147489
147490
147491
147492
147493
147494
147495
147496
147497
147498
147499
147500
147501
147502
147503
147504
147505
147506
147507
147508
147509
147510
147511
147512
147513
147514
147515
147516
147517
147518
147519
147520
147521
147522
147523
147524
147525
147526
147527
147528
147529
147530
147531
147532
147533
147534
147535
147536
147537
147538
147539
147540
147541
147542
147543
147544
147545
147546
147547
147548
147549
147550
147551
147552
147553
147554
147555
147556
147557
147558
147559
147560
147561
147562
147563
147564
147565
147566
147567
147568
147569
147570
147571
147572
147573
147574
147575
147576
147577
147578
147579
147580
147581
147582
147583
147584
147585
147586
147587
147588
147589
147590
147591
147592
147593
147594
147595
147596
147597
147598
147599
147600
147601
147602
147603
147604
147605
147606
147607
147608
147609
147610
147611
147612
147613
147614
147615
147616
147617
147618
147619
147620
147621
147622
147623
147624
147625
147626
147627
147628
147629
147630
147631
147632
147633
147634
147635
147636
147637
147638
147639
147640
147641
147642
147643
147644
147645
147646
147647
147648
147649
147650
147651
147652
147653
147654
147655
147656
147657
147658
147659
147660
147661
147662
147663
147664
147665
147666
147667
147668
147669
147670
147671
147672
147673
147674
147675
147676
147677
147678
147679
147680
147681
147682
147683
147684
147685
147686
147687
147688
147689
147690
147691
147692
147693
147694
147695
147696
147697
147698
147699
147700
147701
147702
147703
147704
147705
147706
147707
147708
147709
147710
147711
147712
147713
147714
147715
147716
147717
147718
147719
147720
147721
147722
147723
147724
147725
147726
147727
147728
147729
147730
147731
147732
147733
147734
147735
147736
147737
147738
147739
147740
147741
147742
147743
147744
147745
147746
147747
147748
147749
147750
147751
147752
147753
147754
147755
147756
147757
147758
147759
147760
147761
147762
147763
147764
147765
147766
147767
147768
147769
147770
147771
147772
147773
147774
147775
147776
147777
147778
147779
147780
147781
147782
147783
147784
147785
147786
147787
147788
147789
147790
147791
147792
147793
147794
147795
147796
147797
147798
147799
147800
147801
147802
147803
147804
147805
147806
147807
147808
147809
147810
147811
147812
147813
147814
147815
147816
147817
147818
147819
147820
147821
147822
147823
147824
147825
147826
147827
147828
147829
147830
147831
147832
147833
147834
147835
147836
147837
147838
147839
147840
147841
147842
147843
147844
147845
147846
147847
147848
147849
147850
147851
147852
147853
147854
147855
147856
147857
147858
147859
147860
147861
147862
147863
147864
147865
147866
147867
147868
147869
147870
147871
147872
147873
147874
147875
147876
147877
147878
147879
147880
147881
147882
147883
147884
147885
147886
147887
147888
147889
147890
147891
147892
147893
147894
147895
147896
147897
147898
147899
147900
147901
147902
147903
147904
147905
147906
147907
147908
147909
147910
147911
147912
147913
147914
147915
147916
147917
147918
147919
147920
147921
147922
147923
147924
147925
147926
147927
147928
147929
147930
147931
147932
147933
147934
147935
147936
147937
147938
147939
147940
147941
147942
147943
147944
147945
147946
147947
147948
147949
147950
147951
147952
147953
147954
147955
147956
147957
147958
147959
147960
147961
147962
147963
147964
147965
147966
147967
147968
147969
147970
147971
147972
147973
147974
147975
147976
147977
147978
147979
147980
147981
147982
147983
147984
147985
147986
147987
147988
147989
147990
147991
147992
147993
147994
147995
147996
147997
147998
147999
148000
148001
148002
148003
148004
148005
148006
148007
148008
148009
148010
148011
148012
148013
148014
148015
148016
148017
148018
148019
148020
148021
148022
148023
148024
148025
148026
148027
148028
148029
148030
148031
148032
148033
148034
148035
148036
148037
148038
148039
148040
148041
148042
148043
148044
148045
148046
148047
148048
148049
148050
148051
148052
148053
148054
148055
148056
148057
148058
148059
148060
148061
148062
148063
148064
148065
148066
148067
148068
148069
148070
148071
148072
148073
148074
148075
148076
148077
148078
148079
148080
148081
148082
148083
148084
148085
148086
148087
148088
148089
148090
148091
148092
148093
148094
148095
148096
148097
148098
148099
148100
148101
148102
148103
148104
148105
148106
148107
148108
148109
148110
148111
148112
148113
148114
148115
148116
148117
148118
148119
148120
148121
148122
148123
148124
148125
148126
148127
148128
148129
148130
148131
148132
148133
148134
148135
148136
148137
148138
148139
148140
148141
148142
148143
148144
148145
148146
148147
148148
148149
148150
148151
148152
148153
148154
148155
148156
148157
148158
148159
148160
148161
148162
148163
148164
148165
148166
148167
148168
148169
148170
148171
148172
148173
148174
148175
148176
148177
148178
148179
148180
148181
148182
148183
148184
148185
148186
148187
148188
148189
148190
148191
148192
148193
148194
148195
148196
148197
148198
148199
148200
148201
148202
148203
148204
148205
148206
148207
148208
148209
148210
148211
148212
148213
148214
148215
148216
148217
148218
148219
148220
148221
148222
148223
148224
148225
148226
148227
148228
148229
148230
148231
148232
148233
148234
148235
148236
148237
148238
148239
148240
148241
148242
148243
148244
148245
148246
148247
148248
148249
148250
148251
148252
148253
148254
148255
148256
148257
148258
148259
148260
148261
148262
148263
148264
148265
148266
148267
148268
148269
148270
148271
148272
148273
148274
148275
148276
148277
148278
148279
148280
148281
148282
148283
148284
148285
148286
148287
148288
148289
148290
148291
148292
148293
148294
148295
148296
148297
148298
148299
148300
148301
148302
148303
148304
148305
148306
148307
148308
148309
148310
148311
148312
148313
148314
148315
148316
148317
148318
148319
148320
148321
148322
148323
148324
148325
148326
148327
148328
148329
148330
148331
148332
148333
148334
148335
148336
148337
148338
148339
148340
148341
148342
148343
148344
148345
148346
148347
148348
148349
148350
148351
148352
148353
148354
148355
148356
148357
148358
148359
148360
148361
148362
148363
148364
148365
148366
148367
148368
148369
148370
148371
148372
148373
148374
148375
148376
148377
148378
148379
148380
148381
148382
148383
148384
148385
148386
148387
148388
148389
148390
148391
148392
148393
148394
148395
148396
148397
148398
148399
148400
148401
148402
148403
148404
148405
148406
148407
148408
148409
148410
148411
148412
148413
148414
148415
148416
148417
148418
148419
148420
148421
148422
148423
148424
148425
148426
148427
148428
148429
148430
148431
148432
148433
148434
148435
148436
148437
148438
148439
148440
148441
148442
148443
148444
148445
148446
148447
148448
148449
148450
148451
148452
148453
148454
148455
148456
148457
148458
148459
148460
148461
148462
148463
148464
148465
148466
148467
148468
148469
148470
148471
148472
148473
148474
148475
148476
148477
148478
148479
148480
148481
148482
148483
148484
148485
148486
148487
148488
148489
148490
148491
148492
148493
148494
148495
148496
148497
148498
148499
148500
148501
148502
148503
148504
148505
148506
148507
148508
148509
148510
148511
148512
148513
148514
148515
148516
148517
148518
148519
148520
148521
148522
148523
148524
148525
148526
148527
148528
148529
148530
148531
148532
148533
148534
148535
148536
148537
148538
148539
148540
148541
148542
148543
148544
148545
148546
148547
148548
148549
148550
148551
148552
148553
148554
148555
148556
148557
148558
148559
148560
148561
148562
148563
148564
148565
148566
148567
148568
148569
148570
148571
148572
148573
148574
148575
148576
148577
148578
148579
148580
148581
148582
148583
148584
148585
148586
148587
148588
148589
148590
148591
148592
148593
148594
148595
148596
148597
148598
148599
148600
148601
148602
148603
148604
148605
148606
148607
148608
148609
148610
148611
148612
148613
148614
148615
148616
148617
148618
148619
148620
148621
148622
148623
148624
148625
148626
148627
148628
148629
148630
148631
148632
148633
148634
148635
148636
148637
148638
148639
148640
148641
148642
148643
148644
148645
148646
148647
148648
148649
148650
148651
148652
148653
148654
148655
148656
148657
148658
148659
148660
148661
148662
148663
148664
148665
148666
148667
148668
148669
148670
148671
148672
148673
148674
148675
148676
148677
148678
148679
148680
148681
148682
148683
148684
148685
148686
148687
148688
148689
148690
148691
148692
148693
148694
148695
148696
148697
148698
148699
148700
148701
148702
148703
148704
148705
148706
148707
148708
148709
148710
148711
148712
148713
148714
148715
148716
148717
148718
148719
148720
148721
148722
148723
148724
148725
148726
148727
148728
148729
148730
148731
148732
148733
148734
148735
148736
148737
148738
148739
148740
148741
148742
148743
148744
148745
148746
148747
148748
148749
148750
148751
148752
148753
148754
148755
148756
148757
148758
148759
148760
148761
148762
148763
148764
148765
148766
148767
148768
148769
148770
148771
148772
148773
148774
148775
148776
148777
148778
148779
148780
148781
148782
148783
148784
148785
148786
148787
148788
148789
148790
148791
148792
148793
148794
148795
148796
148797
148798
148799
148800
148801
148802
148803
148804
148805
148806
148807
148808
148809
148810
148811
148812
148813
148814
148815
148816
148817
148818
148819
148820
148821
148822
148823
148824
148825
148826
148827
148828
148829
148830
148831
148832
148833
148834
148835
148836
148837
148838
148839
148840
148841
148842
148843
148844
148845
148846
148847
148848
148849
148850
148851
148852
148853
148854
148855
148856
148857
148858
148859
148860
148861
148862
148863
148864
148865
148866
148867
148868
148869
148870
148871
148872
148873
148874
148875
148876
148877
148878
148879
148880
148881
148882
148883
148884
148885
148886
148887
148888
148889
148890
148891
148892
148893
148894
148895
148896
148897
148898
148899
148900
148901
148902
148903
148904
148905
148906
148907
148908
148909
148910
148911
148912
148913
148914
148915
148916
148917
148918
148919
148920
148921
148922
148923
148924
148925
148926
148927
148928
148929
148930
148931
148932
148933
148934
148935
148936
148937
148938
148939
148940
148941
148942
148943
148944
148945
148946
148947
148948
148949
148950
148951
148952
148953
148954
148955
148956
148957
148958
148959
148960
148961
148962
148963
148964
148965
148966
148967
148968
148969
148970
148971
148972
148973
148974
148975
148976
148977
148978
148979
148980
148981
148982
148983
148984
148985
148986
148987
148988
148989
148990
148991
148992
148993
148994
148995
148996
148997
148998
148999
149000
149001
149002
149003
149004
149005
149006
149007
149008
149009
149010
149011
149012
149013
149014
149015
149016
149017
149018
149019
149020
149021
149022
149023
149024
149025
149026
149027
149028
149029
149030
149031
149032
149033
149034
149035
149036
149037
149038
149039
149040
149041
149042
149043
149044
149045
149046
149047
149048
149049
149050
149051
149052
149053
149054
149055
149056
149057
149058
149059
149060
149061
149062
149063
149064
149065
149066
149067
149068
149069
149070
149071
149072
149073
149074
149075
149076
149077
149078
149079
149080
149081
149082
149083
149084
149085
149086
149087
149088
149089
149090
149091
149092
149093
149094
149095
149096
149097
149098
149099
149100
149101
149102
149103
149104
149105
149106
149107
149108
149109
149110
149111
149112
149113
149114
149115
149116
149117
149118
149119
149120
149121
149122
149123
149124
149125
149126
149127
149128
149129
149130
149131
149132
149133
149134
149135
149136
149137
149138
149139
149140
149141
149142
149143
149144
149145
149146
149147
149148
149149
149150
149151
149152
149153
149154
149155
149156
149157
149158
149159
149160
149161
149162
149163
149164
149165
149166
149167
149168
149169
149170
149171
149172
149173
149174
149175
149176
149177
149178
149179
149180
149181
149182
149183
149184
149185
149186
149187
149188
149189
149190
149191
149192
149193
149194
149195
149196
149197
149198
149199
149200
149201
149202
149203
149204
149205
149206
149207
149208
149209
149210
149211
149212
149213
149214
149215
149216
149217
149218
149219
149220
149221
149222
149223
149224
149225
149226
149227
149228
149229
149230
149231
149232
149233
149234
149235
149236
149237
149238
149239
149240
149241
149242
149243
149244
149245
149246
149247
149248
149249
149250
149251
149252
149253
149254
149255
149256
149257
149258
149259
149260
149261
149262
149263
149264
149265
149266
149267
149268
149269
149270
149271
149272
149273
149274
149275
149276
149277
149278
149279
149280
149281
149282
149283
149284
149285
149286
149287
149288
149289
149290
149291
149292
149293
149294
149295
149296
149297
149298
149299
149300
149301
149302
149303
149304
149305
149306
149307
149308
149309
149310
149311
149312
149313
149314
149315
149316
149317
149318
149319
149320
149321
149322
149323
149324
149325
149326
149327
149328
149329
149330
149331
149332
149333
149334
149335
149336
149337
149338
149339
149340
149341
149342
149343
149344
149345
149346
149347
149348
149349
149350
149351
149352
149353
149354
149355
149356
149357
149358
149359
149360
149361
149362
149363
149364
149365
149366
149367
149368
149369
149370
149371
149372
149373
149374
149375
149376
149377
149378
149379
149380
149381
149382
149383
149384
149385
149386
149387
149388
149389
149390
149391
149392
149393
149394
149395
149396
149397
149398
149399
149400
149401
149402
149403
149404
149405
149406
149407
149408
149409
149410
149411
149412
149413
149414
149415
149416
149417
149418
149419
149420
149421
149422
149423
149424
149425
149426
149427
149428
149429
149430
149431
149432
149433
149434
149435
149436
149437
149438
149439
149440
149441
149442
149443
149444
149445
149446
149447
149448
149449
149450
149451
149452
149453
149454
149455
149456
149457
149458
149459
149460
149461
149462
149463
149464
149465
149466
149467
149468
149469
149470
149471
149472
149473
149474
149475
149476
149477
149478
149479
149480
149481
149482
149483
149484
149485
149486
149487
149488
149489
149490
149491
149492
149493
149494
149495
149496
149497
149498
149499
149500
149501
149502
149503
149504
149505
149506
149507
149508
149509
149510
149511
149512
149513
149514
149515
149516
149517
149518
149519
149520
149521
149522
149523
149524
149525
149526
149527
149528
149529
149530
149531
149532
149533
149534
149535
149536
149537
149538
149539
149540
149541
149542
149543
149544
149545
149546
149547
149548
149549
149550
149551
149552
149553
149554
149555
149556
149557
149558
149559
149560
149561
149562
149563
149564
149565
149566
149567
149568
149569
149570
149571
149572
149573
149574
149575
149576
149577
149578
149579
149580
149581
149582
149583
149584
149585
149586
149587
149588
149589
149590
149591
149592
149593
149594
149595
149596
149597
149598
149599
149600
149601
149602
149603
149604
149605
149606
149607
149608
149609
149610
149611
149612
149613
149614
149615
149616
149617
149618
149619
149620
149621
149622
149623
149624
149625
149626
149627
149628
149629
149630
149631
149632
149633
149634
149635
149636
149637
149638
149639
149640
149641
149642
149643
149644
149645
149646
149647
149648
149649
149650
149651
149652
149653
149654
149655
149656
149657
149658
149659
149660
149661
149662
149663
149664
149665
149666
149667
149668
149669
149670
149671
149672
149673
149674
149675
149676
149677
149678
149679
149680
149681
149682
149683
149684
149685
149686
149687
149688
149689
149690
149691
149692
149693
149694
149695
149696
149697
149698
149699
149700
149701
149702
149703
149704
149705
149706
149707
149708
149709
149710
149711
149712
149713
149714
149715
149716
149717
149718
149719
149720
149721
149722
149723
149724
149725
149726
149727
149728
149729
149730
149731
149732
149733
149734
149735
149736
149737
149738
149739
149740
149741
149742
149743
149744
149745
149746
149747
149748
149749
149750
149751
149752
149753
149754
149755
149756
149757
149758
149759
149760
149761
149762
149763
149764
149765
149766
149767
149768
149769
149770
149771
149772
149773
149774
149775
149776
149777
149778
149779
149780
149781
149782
149783
149784
149785
149786
149787
149788
149789
149790
149791
149792
149793
149794
149795
149796
149797
149798
149799
149800
149801
149802
149803
149804
149805
149806
149807
149808
149809
149810
149811
149812
149813
149814
149815
149816
149817
149818
149819
149820
149821
149822
149823
149824
149825
149826
149827
149828
149829
149830
149831
149832
149833
149834
149835
149836
149837
149838
149839
149840
149841
149842
149843
149844
149845
149846
149847
149848
149849
149850
149851
149852
149853
149854
149855
149856
149857
149858
149859
149860
149861
149862
149863
149864
149865
149866
149867
149868
149869
149870
149871
149872
149873
149874
149875
149876
149877
149878
149879
149880
149881
149882
149883
149884
149885
149886
149887
149888
149889
149890
149891
149892
149893
149894
149895
149896
149897
149898
149899
149900
149901
149902
149903
149904
149905
149906
149907
149908
149909
149910
149911
149912
149913
149914
149915
149916
149917
149918
149919
149920
149921
149922
149923
149924
149925
149926
149927
149928
149929
149930
149931
149932
149933
149934
149935
149936
149937
149938
149939
149940
149941
149942
149943
149944
149945
149946
149947
149948
149949
149950
149951
149952
149953
149954
149955
149956
149957
149958
149959
149960
149961
149962
149963
149964
149965
149966
149967
149968
149969
149970
149971
149972
149973
149974
149975
149976
149977
149978
149979
149980
149981
149982
149983
149984
149985
149986
149987
149988
149989
149990
149991
149992
149993
149994
149995
149996
149997
149998
149999
150000
150001
150002
150003
150004
150005
150006
150007
150008
150009
150010
150011
150012
150013
150014
150015
150016
150017
150018
150019
150020
150021
150022
150023
150024
150025
150026
150027
150028
150029
150030
150031
150032
150033
150034
150035
150036
150037
150038
150039
150040
150041
150042
150043
150044
150045
150046
150047
150048
150049
150050
150051
150052
150053
150054
150055
150056
150057
150058
150059
150060
150061
150062
150063
150064
150065
150066
150067
150068
150069
150070
150071
150072
150073
150074
150075
150076
150077
150078
150079
150080
150081
150082
150083
150084
150085
150086
150087
150088
150089
150090
150091
150092
150093
150094
150095
150096
150097
150098
150099
150100
150101
150102
150103
150104
150105
150106
150107
150108
150109
150110
150111
150112
150113
150114
150115
150116
150117
150118
150119
150120
150121
150122
150123
150124
150125
150126
150127
150128
150129
150130
150131
150132
150133
150134
150135
150136
150137
150138
150139
150140
150141
150142
150143
150144
150145
150146
150147
150148
150149
150150
150151
150152
150153
150154
150155
150156
150157
150158
150159
150160
150161
150162
150163
150164
150165
150166
150167
150168
150169
150170
150171
150172
150173
150174
150175
150176
150177
150178
150179
150180
150181
150182
150183
150184
150185
150186
150187
150188
150189
150190
150191
150192
150193
150194
150195
150196
150197
150198
150199
150200
150201
150202
150203
150204
150205
150206
150207
150208
150209
150210
150211
150212
150213
150214
150215
150216
150217
150218
150219
150220
150221
150222
150223
150224
150225
150226
150227
150228
150229
150230
150231
150232
150233
150234
150235
150236
150237
150238
150239
150240
150241
150242
150243
150244
150245
150246
150247
150248
150249
150250
150251
150252
150253
150254
150255
150256
150257
150258
150259
150260
150261
150262
150263
150264
150265
150266
150267
150268
150269
150270
150271
150272
150273
150274
150275
150276
150277
150278
150279
150280
150281
150282
150283
150284
150285
150286
150287
150288
150289
150290
150291
150292
150293
150294
150295
150296
150297
150298
150299
150300
150301
150302
150303
150304
150305
150306
150307
150308
150309
150310
150311
150312
150313
150314
150315
150316
150317
150318
150319
150320
150321
150322
150323
150324
150325
150326
150327
150328
150329
150330
150331
150332
150333
150334
150335
150336
150337
150338
150339
150340
150341
150342
150343
150344
150345
150346
150347
150348
150349
150350
150351
150352
150353
150354
150355
150356
150357
150358
150359
150360
150361
150362
150363
150364
150365
150366
150367
150368
150369
150370
150371
150372
150373
150374
150375
150376
150377
150378
150379
150380
150381
150382
150383
150384
150385
150386
150387
150388
150389
150390
150391
150392
150393
150394
150395
150396
150397
150398
150399
150400
150401
150402
150403
150404
150405
150406
150407
150408
150409
150410
150411
150412
150413
150414
150415
150416
150417
150418
150419
150420
150421
150422
150423
150424
150425
150426
150427
150428
150429
150430
150431
150432
150433
150434
150435
150436
150437
150438
150439
150440
150441
150442
150443
150444
150445
150446
150447
150448
150449
150450
150451
150452
150453
150454
150455
150456
150457
150458
150459
150460
150461
150462
150463
150464
150465
150466
150467
150468
150469
150470
150471
150472
150473
150474
150475
150476
150477
150478
150479
150480
150481
150482
150483
150484
150485
150486
150487
150488
150489
150490
150491
150492
150493
150494
150495
150496
150497
150498
150499
150500
150501
150502
150503
150504
150505
150506
150507
150508
150509
150510
150511
150512
150513
150514
150515
150516
150517
150518
150519
150520
150521
150522
150523
150524
150525
150526
150527
150528
150529
150530
150531
150532
150533
150534
150535
150536
150537
150538
150539
150540
150541
150542
150543
150544
150545
150546
150547
150548
150549
150550
150551
150552
150553
150554
150555
150556
150557
150558
150559
150560
150561
150562
150563
150564
150565
150566
150567
150568
150569
150570
150571
150572
150573
150574
150575
150576
150577
150578
150579
150580
150581
150582
150583
150584
150585
150586
150587
150588
150589
150590
150591
150592
150593
150594
150595
150596
150597
150598
150599
150600
150601
150602
150603
150604
150605
150606
150607
150608
150609
150610
150611
150612
150613
150614
150615
150616
150617
150618
150619
150620
150621
150622
150623
150624
150625
150626
150627
150628
150629
150630
150631
150632
150633
150634
150635
150636
150637
150638
150639
150640
150641
150642
150643
150644
150645
150646
150647
150648
150649
150650
150651
150652
150653
150654
150655
150656
150657
150658
150659
150660
150661
150662
150663
150664
150665
150666
150667
150668
150669
150670
150671
150672
150673
150674
150675
150676
150677
150678
150679
150680
150681
150682
150683
150684
150685
150686
150687
150688
150689
150690
150691
150692
150693
150694
150695
150696
150697
150698
150699
150700
150701
150702
150703
150704
150705
150706
150707
150708
150709
150710
150711
150712
150713
150714
150715
150716
150717
150718
150719
150720
150721
150722
150723
150724
150725
150726
150727
150728
150729
150730
150731
150732
150733
150734
150735
150736
150737
150738
150739
150740
150741
150742
150743
150744
150745
150746
150747
150748
150749
150750
150751
150752
150753
150754
150755
150756
150757
150758
150759
150760
150761
150762
150763
150764
150765
150766
150767
150768
150769
150770
150771
150772
150773
150774
150775
150776
150777
150778
150779
150780
150781
150782
150783
150784
150785
150786
150787
150788
150789
150790
150791
150792
150793
150794
150795
150796
150797
150798
150799
150800
150801
150802
150803
150804
150805
150806
150807
150808
150809
150810
150811
150812
150813
150814
150815
150816
150817
150818
150819
150820
150821
150822
150823
150824
150825
150826
150827
150828
150829
150830
150831
150832
150833
150834
150835
150836
150837
150838
150839
150840
150841
150842
150843
150844
150845
150846
150847
150848
150849
150850
150851
150852
150853
150854
150855
150856
150857
150858
150859
150860
150861
150862
150863
150864
150865
150866
150867
150868
150869
150870
150871
150872
150873
150874
150875
150876
150877
150878
150879
150880
150881
150882
150883
150884
150885
150886
150887
150888
150889
150890
150891
150892
150893
150894
150895
150896
150897
150898
150899
150900
150901
150902
150903
150904
150905
150906
150907
150908
150909
150910
150911
150912
150913
150914
150915
150916
150917
150918
150919
150920
150921
150922
150923
150924
150925
150926
150927
150928
150929
150930
150931
150932
150933
150934
150935
150936
150937
150938
150939
150940
150941
150942
150943
150944
150945
150946
150947
150948
150949
150950
150951
150952
150953
150954
150955
150956
150957
150958
150959
150960
150961
150962
150963
150964
150965
150966
150967
150968
150969
150970
150971
150972
150973
150974
150975
150976
150977
150978
150979
150980
150981
150982
150983
150984
150985
150986
150987
150988
150989
150990
150991
150992
150993
150994
150995
150996
150997
150998
150999
151000
151001
151002
151003
151004
151005
151006
151007
151008
151009
151010
151011
151012
151013
151014
151015
151016
151017
151018
151019
151020
151021
151022
151023
151024
151025
151026
151027
151028
151029
151030
151031
151032
151033
151034
151035
151036
151037
151038
151039
151040
151041
151042
151043
151044
151045
151046
151047
151048
151049
151050
151051
151052
151053
151054
151055
151056
151057
151058
151059
151060
151061
151062
151063
151064
151065
151066
151067
151068
151069
151070
151071
151072
151073
151074
151075
151076
151077
151078
151079
151080
151081
151082
151083
151084
151085
151086
151087
151088
151089
151090
151091
151092
151093
151094
151095
151096
151097
151098
151099
151100
151101
151102
151103
151104
151105
151106
151107
151108
151109
151110
151111
151112
151113
151114
151115
151116
151117
151118
151119
151120
151121
151122
151123
151124
151125
151126
151127
151128
151129
151130
151131
151132
151133
151134
151135
151136
151137
151138
151139
151140
151141
151142
151143
151144
151145
151146
151147
151148
151149
151150
151151
151152
151153
151154
151155
151156
151157
151158
151159
151160
151161
151162
151163
151164
151165
151166
151167
151168
151169
151170
151171
151172
151173
151174
151175
151176
151177
151178
151179
151180
151181
151182
151183
151184
151185
151186
151187
151188
151189
151190
151191
151192
151193
151194
151195
151196
151197
151198
151199
151200
151201
151202
151203
151204
151205
151206
151207
151208
151209
151210
151211
151212
151213
151214
151215
151216
151217
151218
151219
151220
151221
151222
151223
151224
151225
151226
151227
151228
151229
151230
151231
151232
151233
151234
151235
151236
151237
151238
151239
151240
151241
151242
151243
151244
151245
151246
151247
151248
151249
151250
151251
151252
151253
151254
151255
151256
151257
151258
151259
151260
151261
151262
151263
151264
151265
151266
151267
151268
151269
151270
151271
151272
151273
151274
151275
151276
151277
151278
151279
151280
151281
151282
151283
151284
151285
151286
151287
151288
151289
151290
151291
151292
151293
151294
151295
151296
151297
151298
151299
151300
151301
151302
151303
151304
151305
151306
151307
151308
151309
151310
151311
151312
151313
151314
151315
151316
151317
151318
151319
151320
151321
151322
151323
151324
151325
151326
151327
151328
151329
151330
151331
151332
151333
151334
151335
151336
151337
151338
151339
151340
151341
151342
151343
151344
151345
151346
151347
151348
151349
151350
151351
151352
151353
151354
151355
151356
151357
151358
151359
151360
151361
151362
151363
151364
151365
151366
151367
151368
151369
151370
151371
151372
151373
151374
151375
151376
151377
151378
151379
151380
151381
151382
151383
151384
151385
151386
151387
151388
151389
151390
151391
151392
151393
151394
151395
151396
151397
151398
151399
151400
151401
151402
151403
151404
151405
151406
151407
151408
151409
151410
151411
151412
151413
151414
151415
151416
151417
151418
151419
151420
151421
151422
151423
151424
151425
151426
151427
151428
151429
151430
151431
151432
151433
151434
151435
151436
151437
151438
151439
151440
151441
151442
151443
151444
151445
151446
151447
151448
151449
151450
151451
151452
151453
151454
151455
151456
151457
151458
151459
151460
151461
151462
151463
151464
151465
151466
151467
151468
151469
151470
151471
151472
151473
151474
151475
151476
151477
151478
151479
151480
151481
151482
151483
151484
151485
151486
151487
151488
151489
151490
151491
151492
151493
151494
151495
151496
151497
151498
151499
151500
151501
151502
151503
151504
151505
151506
151507
151508
151509
151510
151511
151512
151513
151514
151515
151516
151517
151518
151519
151520
151521
151522
151523
151524
151525
151526
151527
151528
151529
151530
151531
151532
151533
151534
151535
151536
151537
151538
151539
151540
151541
151542
151543
151544
151545
151546
151547
151548
151549
151550
151551
151552
151553
151554
151555
151556
151557
151558
151559
151560
151561
151562
151563
151564
151565
151566
151567
151568
151569
151570
151571
151572
151573
151574
151575
151576
151577
151578
151579
151580
151581
151582
151583
151584
151585
151586
151587
151588
151589
151590
151591
151592
151593
151594
151595
151596
151597
151598
151599
151600
151601
151602
151603
151604
151605
151606
151607
151608
151609
151610
151611
151612
151613
151614
151615
151616
151617
151618
151619
151620
151621
151622
151623
151624
151625
151626
151627
151628
151629
151630
151631
151632
151633
151634
151635
151636
151637
151638
151639
151640
151641
151642
151643
151644
151645
151646
151647
151648
151649
151650
151651
151652
151653
151654
151655
151656
151657
151658
151659
151660
151661
151662
151663
151664
151665
151666
151667
151668
151669
151670
151671
151672
151673
151674
151675
151676
151677
151678
151679
151680
151681
151682
151683
151684
151685
151686
151687
151688
151689
151690
151691
151692
151693
151694
151695
151696
151697
151698
151699
151700
151701
151702
151703
151704
151705
151706
151707
151708
151709
151710
151711
151712
151713
151714
151715
151716
151717
151718
151719
151720
151721
151722
151723
151724
151725
151726
151727
151728
151729
151730
151731
151732
151733
151734
151735
151736
151737
151738
151739
151740
151741
151742
151743
151744
151745
151746
151747
151748
151749
151750
151751
151752
151753
151754
151755
151756
151757
151758
151759
151760
151761
151762
151763
151764
151765
151766
151767
151768
151769
151770
151771
151772
151773
151774
151775
151776
151777
151778
151779
151780
151781
151782
151783
151784
151785
151786
151787
151788
151789
151790
151791
151792
151793
151794
151795
151796
151797
151798
151799
151800
151801
151802
151803
151804
151805
151806
151807
151808
151809
151810
151811
151812
151813
151814
151815
151816
151817
151818
151819
151820
151821
151822
151823
151824
151825
151826
151827
151828
151829
151830
151831
151832
151833
151834
151835
151836
151837
151838
151839
151840
151841
151842
151843
151844
151845
151846
151847
151848
151849
151850
151851
151852
151853
151854
151855
151856
151857
151858
151859
151860
151861
151862
151863
151864
151865
151866
151867
151868
151869
151870
151871
151872
151873
151874
151875
151876
151877
151878
151879
151880
151881
151882
151883
151884
151885
151886
151887
151888
151889
151890
151891
151892
151893
151894
151895
151896
151897
151898
151899
151900
151901
151902
151903
151904
151905
151906
151907
151908
151909
151910
151911
151912
151913
151914
151915
151916
151917
151918
151919
151920
151921
151922
151923
151924
151925
151926
151927
151928
151929
151930
151931
151932
151933
151934
151935
151936
151937
151938
151939
151940
151941
151942
151943
151944
151945
151946
151947
151948
151949
151950
151951
151952
151953
151954
151955
151956
151957
151958
151959
151960
151961
151962
151963
151964
151965
151966
151967
151968
151969
151970
151971
151972
151973
151974
151975
151976
151977
151978
151979
151980
151981
151982
151983
151984
151985
151986
151987
151988
151989
151990
151991
151992
151993
151994
151995
151996
151997
151998
151999
152000
152001
152002
152003
152004
152005
152006
152007
152008
152009
152010
152011
152012
152013
152014
152015
152016
152017
152018
152019
152020
152021
152022
152023
152024
152025
152026
152027
152028
152029
152030
152031
152032
152033
152034
152035
152036
152037
152038
152039
152040
152041
152042
152043
152044
152045
152046
152047
152048
152049
152050
152051
152052
152053
152054
152055
152056
152057
152058
152059
152060
152061
152062
152063
152064
152065
152066
152067
152068
152069
152070
152071
152072
152073
152074
152075
152076
152077
152078
152079
152080
152081
152082
152083
152084
152085
152086
152087
152088
152089
152090
152091
152092
152093
152094
152095
152096
152097
152098
152099
152100
152101
152102
152103
152104
152105
152106
152107
152108
152109
152110
152111
152112
152113
152114
152115
152116
152117
152118
152119
152120
152121
152122
152123
152124
152125
152126
152127
152128
152129
152130
152131
152132
152133
152134
152135
152136
152137
152138
152139
152140
152141
152142
152143
152144
152145
152146
152147
152148
152149
152150
152151
152152
152153
152154
152155
152156
152157
152158
152159
152160
152161
152162
152163
152164
152165
152166
152167
152168
152169
152170
152171
152172
152173
152174
152175
152176
152177
152178
152179
152180
152181
152182
152183
152184
152185
152186
152187
152188
152189
152190
152191
152192
152193
152194
152195
152196
152197
152198
152199
152200
152201
152202
152203
152204
152205
152206
152207
152208
152209
152210
152211
152212
152213
152214
152215
152216
152217
152218
152219
152220
152221
152222
152223
152224
152225
152226
152227
152228
152229
152230
152231
152232
152233
152234
152235
152236
152237
152238
152239
152240
152241
152242
152243
152244
152245
152246
152247
152248
152249
152250
152251
152252
152253
152254
152255
152256
152257
152258
152259
152260
152261
152262
152263
152264
152265
152266
152267
152268
152269
152270
152271
152272
152273
152274
152275
152276
152277
152278
152279
152280
152281
152282
152283
152284
152285
152286
152287
152288
152289
152290
152291
152292
152293
152294
152295
152296
152297
152298
152299
152300
152301
152302
152303
152304
152305
152306
152307
152308
152309
152310
152311
152312
152313
152314
152315
152316
152317
152318
152319
152320
152321
152322
152323
152324
152325
152326
152327
152328
152329
152330
152331
152332
152333
152334
152335
152336
152337
152338
152339
152340
152341
152342
152343
152344
152345
152346
152347
152348
152349
152350
152351
152352
152353
152354
152355
152356
152357
152358
152359
152360
152361
152362
152363
152364
152365
152366
152367
152368
152369
152370
152371
152372
152373
152374
152375
152376
152377
152378
152379
152380
152381
152382
152383
152384
152385
152386
152387
152388
152389
152390
152391
152392
152393
152394
152395
152396
152397
152398
152399
152400
152401
152402
152403
152404
152405
152406
152407
152408
152409
152410
152411
152412
152413
152414
152415
152416
152417
152418
152419
152420
152421
152422
152423
152424
152425
152426
152427
152428
152429
152430
152431
152432
152433
152434
152435
152436
152437
152438
152439
152440
152441
152442
152443
152444
152445
152446
152447
152448
152449
152450
152451
152452
152453
152454
152455
152456
152457
152458
152459
152460
152461
152462
152463
152464
152465
152466
152467
152468
152469
152470
152471
152472
152473
152474
152475
152476
152477
152478
152479
152480
152481
152482
152483
152484
152485
152486
152487
152488
152489
152490
152491
152492
152493
152494
152495
152496
152497
152498
152499
152500
152501
152502
152503
152504
152505
152506
152507
152508
152509
152510
152511
152512
152513
152514
152515
152516
152517
152518
152519
152520
152521
152522
152523
152524
152525
152526
152527
152528
152529
152530
152531
152532
152533
152534
152535
152536
152537
152538
152539
152540
152541
152542
152543
152544
152545
152546
152547
152548
152549
152550
152551
152552
152553
152554
152555
152556
152557
152558
152559
152560
152561
152562
152563
152564
152565
152566
152567
152568
152569
152570
152571
152572
152573
152574
152575
152576
152577
152578
152579
152580
152581
152582
152583
152584
152585
152586
152587
152588
152589
152590
152591
152592
152593
152594
152595
152596
152597
152598
152599
152600
152601
152602
152603
152604
152605
152606
152607
152608
152609
152610
152611
152612
152613
152614
152615
152616
152617
152618
152619
152620
152621
152622
152623
152624
152625
152626
152627
152628
152629
152630
152631
152632
152633
152634
152635
152636
152637
152638
152639
152640
152641
152642
152643
152644
152645
152646
152647
152648
152649
152650
152651
152652
152653
152654
152655
152656
152657
152658
152659
152660
152661
152662
152663
152664
152665
152666
152667
152668
152669
152670
152671
152672
152673
152674
152675
152676
152677
152678
152679
152680
152681
152682
152683
152684
152685
152686
152687
152688
152689
152690
152691
152692
152693
152694
152695
152696
152697
152698
152699
152700
152701
152702
152703
152704
152705
152706
152707
152708
152709
152710
152711
152712
152713
152714
152715
152716
152717
152718
152719
152720
152721
152722
152723
152724
152725
152726
152727
152728
152729
152730
152731
152732
152733
152734
152735
152736
152737
152738
152739
152740
152741
152742
152743
152744
152745
152746
152747
152748
152749
152750
152751
152752
152753
152754
152755
152756
152757
152758
152759
152760
152761
152762
152763
152764
152765
152766
152767
152768
152769
152770
152771
152772
152773
152774
152775
152776
152777
152778
152779
152780
152781
152782
152783
152784
152785
152786
152787
152788
152789
152790
152791
152792
152793
152794
152795
152796
152797
152798
152799
152800
152801
152802
152803
152804
152805
152806
152807
152808
152809
152810
152811
152812
152813
152814
152815
152816
152817
152818
152819
152820
152821
152822
152823
152824
152825
152826
152827
152828
152829
152830
152831
152832
152833
152834
152835
152836
152837
152838
152839
152840
152841
152842
152843
152844
152845
152846
152847
152848
152849
152850
152851
152852
152853
152854
152855
152856
152857
152858
152859
152860
152861
152862
152863
152864
152865
152866
152867
152868
152869
152870
152871
152872
152873
152874
152875
152876
152877
152878
152879
152880
152881
152882
152883
152884
152885
152886
152887
152888
152889
152890
152891
152892
152893
152894
152895
152896
152897
152898
152899
152900
152901
152902
152903
152904
152905
152906
152907
152908
152909
152910
152911
152912
152913
152914
152915
152916
152917
152918
152919
152920
152921
152922
152923
152924
152925
152926
152927
152928
152929
152930
152931
152932
152933
152934
152935
152936
152937
152938
152939
152940
152941
152942
152943
152944
152945
152946
152947
152948
152949
152950
152951
152952
152953
152954
152955
152956
152957
152958
152959
152960
152961
152962
152963
152964
152965
152966
152967
152968
152969
152970
152971
152972
152973
152974
152975
152976
152977
152978
152979
152980
152981
152982
152983
152984
152985
152986
152987
152988
152989
152990
152991
152992
152993
152994
152995
152996
152997
152998
152999
153000
153001
153002
153003
153004
153005
153006
153007
153008
153009
153010
153011
153012
153013
153014
153015
153016
153017
153018
153019
153020
153021
153022
153023
153024
153025
153026
153027
153028
153029
153030
153031
153032
153033
153034
153035
153036
153037
153038
153039
153040
153041
153042
153043
153044
153045
153046
153047
153048
153049
153050
153051
153052
153053
153054
153055
153056
153057
153058
153059
153060
153061
153062
153063
153064
153065
153066
153067
153068
153069
153070
153071
153072
153073
153074
153075
153076
153077
153078
153079
153080
153081
153082
153083
153084
153085
153086
153087
153088
153089
153090
153091
153092
153093
153094
153095
153096
153097
153098
153099
153100
153101
153102
153103
153104
153105
153106
153107
153108
153109
153110
153111
153112
153113
153114
153115
153116
153117
153118
153119
153120
153121
153122
153123
153124
153125
153126
153127
153128
153129
153130
153131
153132
153133
153134
153135
153136
153137
153138
153139
153140
153141
153142
153143
153144
153145
153146
153147
153148
153149
153150
153151
153152
153153
153154
153155
153156
153157
153158
153159
153160
153161
153162
153163
153164
153165
153166
153167
153168
153169
153170
153171
153172
153173
153174
153175
153176
153177
153178
153179
153180
153181
153182
153183
153184
153185
153186
153187
153188
153189
153190
153191
153192
153193
153194
153195
153196
153197
153198
153199
153200
153201
153202
153203
153204
153205
153206
153207
153208
153209
153210
153211
153212
153213
153214
153215
153216
153217
153218
153219
153220
153221
153222
153223
153224
153225
153226
153227
153228
153229
153230
153231
153232
153233
153234
153235
153236
153237
153238
153239
153240
153241
153242
153243
153244
153245
153246
153247
153248
153249
153250
153251
153252
153253
153254
153255
153256
153257
153258
153259
153260
153261
153262
153263
153264
153265
153266
153267
153268
153269
153270
153271
153272
153273
153274
153275
153276
153277
153278
153279
153280
153281
153282
153283
153284
153285
153286
153287
153288
153289
153290
153291
153292
153293
153294
153295
153296
153297
153298
153299
153300
153301
153302
153303
153304
153305
153306
153307
153308
153309
153310
153311
153312
153313
153314
153315
153316
153317
153318
153319
153320
153321
153322
153323
153324
153325
153326
153327
153328
153329
153330
153331
153332
153333
153334
153335
153336
153337
153338
153339
153340
153341
153342
153343
153344
153345
153346
153347
153348
153349
153350
153351
153352
153353
153354
153355
153356
153357
153358
153359
153360
153361
153362
153363
153364
153365
153366
153367
153368
153369
153370
153371
153372
153373
153374
153375
153376
153377
153378
153379
153380
153381
153382
153383
153384
153385
153386
153387
153388
153389
153390
153391
153392
153393
153394
153395
153396
153397
153398
153399
153400
153401
153402
153403
153404
153405
153406
153407
153408
153409
153410
153411
153412
153413
153414
153415
153416
153417
153418
153419
153420
153421
153422
153423
153424
153425
153426
153427
153428
153429
153430
153431
153432
153433
153434
153435
153436
153437
153438
153439
153440
153441
153442
153443
153444
153445
153446
153447
153448
153449
153450
153451
153452
153453
153454
153455
153456
153457
153458
153459
153460
153461
153462
153463
153464
153465
153466
153467
153468
153469
153470
153471
153472
153473
153474
153475
153476
153477
153478
153479
153480
153481
153482
153483
153484
153485
153486
153487
153488
153489
153490
153491
153492
153493
153494
153495
153496
153497
153498
153499
153500
153501
153502
153503
153504
153505
153506
153507
153508
153509
153510
153511
153512
153513
153514
153515
153516
153517
153518
153519
153520
153521
153522
153523
153524
153525
153526
153527
153528
153529
153530
153531
153532
153533
153534
153535
153536
153537
153538
153539
153540
153541
153542
153543
153544
153545
153546
153547
153548
153549
153550
153551
153552
153553
153554
153555
153556
153557
153558
153559
153560
153561
153562
153563
153564
153565
153566
153567
153568
153569
153570
153571
153572
153573
153574
153575
153576
153577
153578
153579
153580
153581
153582
153583
153584
153585
153586
153587
153588
153589
153590
153591
153592
153593
153594
153595
153596
153597
153598
153599
153600
153601
153602
153603
153604
153605
153606
153607
153608
153609
153610
153611
153612
153613
153614
153615
153616
153617
153618
153619
153620
153621
153622
153623
153624
153625
153626
153627
153628
153629
153630
153631
153632
153633
153634
153635
153636
153637
153638
153639
153640
153641
153642
153643
153644
153645
153646
153647
153648
153649
153650
153651
153652
153653
153654
153655
153656
153657
153658
153659
153660
153661
153662
153663
153664
153665
153666
153667
153668
153669
153670
153671
153672
153673
153674
153675
153676
153677
153678
153679
153680
153681
153682
153683
153684
153685
153686
153687
153688
153689
153690
153691
153692
153693
153694
153695
153696
153697
153698
153699
153700
153701
153702
153703
153704
153705
153706
153707
153708
153709
153710
153711
153712
153713
153714
153715
153716
153717
153718
153719
153720
153721
153722
153723
153724
153725
153726
153727
153728
153729
153730
153731
153732
153733
153734
153735
153736
153737
153738
153739
153740
153741
153742
153743
153744
153745
153746
153747
153748
153749
153750
153751
153752
153753
153754
153755
153756
153757
153758
153759
153760
153761
153762
153763
153764
153765
153766
153767
153768
153769
153770
153771
153772
153773
153774
153775
153776
153777
153778
153779
153780
153781
153782
153783
153784
153785
153786
153787
153788
153789
153790
153791
153792
153793
153794
153795
153796
153797
153798
153799
153800
153801
153802
153803
153804
153805
153806
153807
153808
153809
153810
153811
153812
153813
153814
153815
153816
153817
153818
153819
153820
153821
153822
153823
153824
153825
153826
153827
153828
153829
153830
153831
153832
153833
153834
153835
153836
153837
153838
153839
153840
153841
153842
153843
153844
153845
153846
153847
153848
153849
153850
153851
153852
153853
153854
153855
153856
153857
153858
153859
153860
153861
153862
153863
153864
153865
153866
153867
153868
153869
153870
153871
153872
153873
153874
153875
153876
153877
153878
153879
153880
153881
153882
153883
153884
153885
153886
153887
153888
153889
153890
153891
153892
153893
153894
153895
153896
153897
153898
153899
153900
153901
153902
153903
153904
153905
153906
153907
153908
153909
153910
153911
153912
153913
153914
153915
153916
153917
153918
153919
153920
153921
153922
153923
153924
153925
153926
153927
153928
153929
153930
153931
153932
153933
153934
153935
153936
153937
153938
153939
153940
153941
153942
153943
153944
153945
153946
153947
153948
153949
153950
153951
153952
153953
153954
153955
153956
153957
153958
153959
153960
153961
153962
153963
153964
153965
153966
153967
153968
153969
153970
153971
153972
153973
153974
153975
153976
153977
153978
153979
153980
153981
153982
153983
153984
153985
153986
153987
153988
153989
153990
153991
153992
153993
153994
153995
153996
153997
153998
153999
154000
154001
154002
154003
154004
154005
154006
154007
154008
154009
154010
154011
154012
154013
154014
154015
154016
154017
154018
154019
154020
154021
154022
154023
154024
154025
154026
154027
154028
154029
154030
154031
154032
154033
154034
154035
154036
154037
154038
154039
154040
154041
154042
154043
154044
154045
154046
154047
154048
154049
154050
154051
154052
154053
154054
154055
154056
154057
154058
154059
154060
154061
154062
154063
154064
154065
154066
154067
154068
154069
154070
154071
154072
154073
154074
154075
154076
154077
154078
154079
154080
154081
154082
154083
154084
154085
154086
154087
154088
154089
154090
154091
154092
154093
154094
154095
154096
154097
154098
154099
154100
154101
154102
154103
154104
154105
154106
154107
154108
154109
154110
154111
154112
154113
154114
154115
154116
154117
154118
154119
154120
154121
154122
154123
154124
154125
154126
154127
154128
154129
154130
154131
154132
154133
154134
154135
154136
154137
154138
154139
154140
154141
154142
154143
154144
154145
154146
154147
154148
154149
154150
154151
154152
154153
154154
154155
154156
154157
154158
154159
154160
154161
154162
154163
154164
154165
154166
154167
154168
154169
154170
154171
154172
154173
154174
154175
154176
154177
154178
154179
154180
154181
154182
154183
154184
154185
154186
154187
154188
154189
154190
154191
154192
154193
154194
154195
154196
154197
154198
154199
154200
154201
154202
154203
154204
154205
154206
154207
154208
154209
154210
154211
154212
154213
154214
154215
154216
154217
154218
154219
154220
154221
154222
154223
154224
154225
154226
154227
154228
154229
154230
154231
154232
154233
154234
154235
154236
154237
154238
154239
154240
154241
154242
154243
154244
154245
154246
154247
154248
154249
154250
154251
154252
154253
154254
154255
154256
154257
154258
154259
154260
154261
154262
154263
154264
154265
154266
154267
154268
154269
154270
154271
154272
154273
154274
154275
154276
154277
154278
154279
154280
154281
154282
154283
154284
154285
154286
154287
154288
154289
154290
154291
154292
154293
154294
154295
154296
154297
154298
154299
154300
154301
154302
154303
154304
154305
154306
154307
154308
154309
154310
154311
154312
154313
154314
154315
154316
154317
154318
154319
154320
154321
154322
154323
154324
154325
154326
154327
154328
154329
154330
154331
154332
154333
154334
154335
154336
154337
154338
154339
154340
154341
154342
154343
154344
154345
154346
154347
154348
154349
154350
154351
154352
154353
154354
154355
154356
154357
154358
154359
154360
154361
154362
154363
154364
154365
154366
154367
154368
154369
154370
154371
154372
154373
154374
154375
154376
154377
154378
154379
154380
154381
154382
154383
154384
154385
154386
154387
154388
154389
154390
154391
154392
154393
154394
154395
154396
154397
154398
154399
154400
154401
154402
154403
154404
154405
154406
154407
154408
154409
154410
154411
154412
154413
154414
154415
154416
154417
154418
154419
154420
154421
154422
154423
154424
154425
154426
154427
154428
154429
154430
154431
154432
154433
154434
154435
154436
154437
154438
154439
154440
154441
154442
154443
154444
154445
154446
154447
154448
154449
154450
154451
154452
154453
154454
154455
154456
154457
154458
154459
154460
154461
154462
154463
154464
154465
154466
154467
154468
154469
154470
154471
154472
154473
154474
154475
154476
154477
154478
154479
154480
154481
154482
154483
154484
154485
154486
154487
154488
154489
154490
154491
154492
154493
154494
154495
154496
154497
154498
154499
154500
154501
154502
154503
154504
154505
154506
154507
154508
154509
154510
154511
154512
154513
154514
154515
154516
154517
154518
154519
154520
154521
154522
154523
154524
154525
154526
154527
154528
154529
154530
154531
154532
154533
154534
154535
154536
154537
154538
154539
154540
154541
154542
154543
154544
154545
154546
154547
154548
154549
154550
154551
154552
154553
154554
154555
154556
154557
154558
154559
154560
154561
154562
154563
154564
154565
154566
154567
154568
154569
154570
154571
154572
154573
154574
154575
154576
154577
154578
154579
154580
154581
154582
154583
154584
154585
154586
154587
154588
154589
154590
154591
154592
154593
154594
154595
154596
154597
154598
154599
154600
154601
154602
154603
154604
154605
154606
154607
154608
154609
154610
154611
154612
154613
154614
154615
154616
154617
154618
154619
154620
154621
154622
154623
154624
154625
154626
154627
154628
154629
154630
154631
154632
154633
154634
154635
154636
154637
154638
154639
154640
154641
154642
154643
154644
154645
154646
154647
154648
154649
154650
154651
154652
154653
154654
154655
154656
154657
154658
154659
154660
154661
154662
154663
154664
154665
154666
154667
154668
154669
154670
154671
154672
154673
154674
154675
154676
154677
154678
154679
154680
154681
154682
154683
154684
154685
154686
154687
154688
154689
154690
154691
154692
154693
154694
154695
154696
154697
154698
154699
154700
154701
154702
154703
154704
154705
154706
154707
154708
154709
154710
154711
154712
154713
154714
154715
154716
154717
154718
154719
154720
154721
154722
154723
154724
154725
154726
154727
154728
154729
154730
154731
154732
154733
154734
154735
154736
154737
154738
154739
154740
154741
154742
154743
154744
154745
154746
154747
154748
154749
154750
154751
154752
154753
154754
154755
154756
154757
154758
154759
154760
154761
154762
154763
154764
154765
154766
154767
154768
154769
154770
154771
154772
154773
154774
154775
154776
154777
154778
154779
154780
154781
154782
154783
154784
154785
154786
154787
154788
154789
154790
154791
154792
154793
154794
154795
154796
154797
154798
154799
154800
154801
154802
154803
154804
154805
154806
154807
154808
154809
154810
154811
154812
154813
154814
154815
154816
154817
154818
154819
154820
154821
154822
154823
154824
154825
154826
154827
154828
154829
154830
154831
154832
154833
154834
154835
154836
154837
154838
154839
154840
154841
154842
154843
154844
154845
154846
154847
154848
154849
154850
154851
154852
154853
154854
154855
154856
154857
154858
154859
154860
154861
154862
154863
154864
154865
154866
154867
154868
154869
154870
154871
154872
154873
154874
154875
154876
154877
154878
154879
154880
154881
154882
154883
154884
154885
154886
154887
154888
154889
154890
154891
154892
154893
154894
154895
154896
154897
154898
154899
154900
154901
154902
154903
154904
154905
154906
154907
154908
154909
154910
154911
154912
154913
154914
154915
154916
154917
154918
154919
154920
154921
154922
154923
154924
154925
154926
154927
154928
154929
154930
154931
154932
154933
154934
154935
154936
154937
154938
154939
154940
154941
154942
154943
154944
154945
154946
154947
154948
154949
154950
154951
154952
154953
154954
154955
154956
154957
154958
154959
154960
154961
154962
154963
154964
154965
154966
154967
154968
154969
154970
154971
154972
154973
154974
154975
154976
154977
154978
154979
154980
154981
154982
154983
154984
154985
154986
154987
154988
154989
154990
154991
154992
154993
154994
154995
154996
154997
154998
154999
155000
155001
155002
155003
155004
155005
155006
155007
155008
155009
155010
155011
155012
155013
155014
155015
155016
155017
155018
155019
155020
155021
155022
155023
155024
155025
155026
155027
155028
155029
155030
155031
155032
155033
155034
155035
155036
155037
155038
155039
155040
155041
155042
155043
155044
155045
155046
155047
155048
155049
155050
155051
155052
155053
155054
155055
155056
155057
155058
155059
155060
155061
155062
155063
155064
155065
155066
155067
155068
155069
155070
155071
155072
155073
155074
155075
155076
155077
155078
155079
155080
155081
155082
155083
155084
155085
155086
155087
155088
155089
155090
155091
155092
155093
155094
155095
155096
155097
155098
155099
155100
155101
155102
155103
155104
155105
155106
155107
155108
155109
155110
155111
155112
155113
155114
155115
155116
155117
155118
155119
155120
155121
155122
155123
155124
155125
155126
155127
155128
155129
155130
155131
155132
155133
155134
155135
155136
155137
155138
155139
155140
155141
155142
155143
155144
155145
155146
155147
155148
155149
155150
155151
155152
155153
155154
155155
155156
155157
155158
155159
155160
155161
155162
155163
155164
155165
155166
155167
155168
155169
155170
155171
155172
155173
155174
155175
155176
155177
155178
155179
155180
155181
155182
155183
155184
155185
155186
155187
155188
155189
155190
155191
155192
155193
155194
155195
155196
155197
155198
155199
155200
155201
155202
155203
155204
155205
155206
155207
155208
155209
155210
155211
155212
155213
155214
155215
155216
155217
155218
155219
155220
155221
155222
155223
155224
155225
155226
155227
155228
155229
155230
155231
155232
155233
155234
155235
155236
155237
155238
155239
155240
155241
155242
155243
155244
155245
155246
155247
155248
155249
155250
155251
155252
155253
155254
155255
155256
155257
155258
155259
155260
155261
155262
155263
155264
155265
155266
155267
155268
155269
155270
155271
155272
155273
155274
155275
155276
155277
155278
155279
155280
155281
155282
155283
155284
155285
155286
155287
155288
155289
155290
155291
155292
155293
155294
155295
155296
155297
155298
155299
155300
155301
155302
155303
155304
155305
155306
155307
155308
155309
155310
155311
155312
155313
155314
155315
155316
155317
155318
155319
155320
155321
155322
155323
155324
155325
155326
155327
155328
155329
155330
155331
155332
155333
155334
155335
155336
155337
155338
155339
155340
155341
155342
155343
155344
155345
155346
155347
155348
155349
155350
155351
155352
155353
155354
155355
155356
155357
155358
155359
155360
155361
155362
155363
155364
155365
155366
155367
155368
155369
155370
155371
155372
155373
155374
155375
155376
155377
155378
155379
155380
155381
155382
155383
155384
155385
155386
155387
155388
155389
155390
155391
155392
155393
155394
155395
155396
155397
155398
155399
155400
155401
155402
155403
155404
155405
155406
155407
155408
155409
155410
155411
155412
155413
155414
155415
155416
155417
155418
155419
155420
155421
155422
155423
155424
155425
155426
155427
155428
155429
155430
155431
155432
155433
155434
155435
155436
155437
155438
155439
155440
155441
155442
155443
155444
155445
155446
155447
155448
155449
155450
155451
155452
155453
155454
155455
155456
155457
155458
155459
155460
155461
155462
155463
155464
155465
155466
155467
155468
155469
155470
155471
155472
155473
155474
155475
155476
155477
155478
155479
155480
155481
155482
155483
155484
155485
155486
155487
155488
155489
155490
155491
155492
155493
155494
155495
155496
155497
155498
155499
155500
155501
155502
155503
155504
155505
155506
155507
155508
155509
155510
155511
155512
155513
155514
155515
155516
155517
155518
155519
155520
155521
155522
155523
155524
155525
155526
155527
155528
155529
155530
155531
155532
155533
155534
155535
155536
155537
155538
155539
155540
155541
155542
155543
155544
155545
155546
155547
155548
155549
155550
155551
155552
155553
155554
155555
155556
155557
155558
155559
155560
155561
155562
155563
155564
155565
155566
155567
155568
155569
155570
155571
155572
155573
155574
155575
155576
155577
155578
155579
155580
155581
155582
155583
155584
155585
155586
155587
155588
155589
155590
155591
155592
155593
155594
155595
155596
155597
155598
155599
155600
155601
155602
155603
155604
155605
155606
155607
155608
155609
155610
155611
155612
155613
155614
155615
155616
155617
155618
155619
155620
155621
155622
155623
155624
155625
155626
155627
155628
155629
155630
155631
155632
155633
155634
155635
155636
155637
155638
155639
155640
155641
155642
155643
155644
155645
155646
155647
155648
155649
155650
155651
155652
155653
155654
155655
155656
155657
155658
155659
155660
155661
155662
155663
155664
155665
155666
155667
155668
155669
155670
155671
155672
155673
155674
155675
155676
155677
155678
155679
155680
155681
155682
155683
155684
155685
155686
155687
155688
155689
155690
155691
155692
155693
155694
155695
155696
155697
155698
155699
155700
155701
155702
155703
155704
155705
155706
155707
155708
155709
155710
155711
155712
155713
155714
155715
155716
155717
155718
155719
155720
155721
155722
155723
155724
155725
155726
155727
155728
155729
155730
155731
155732
155733
155734
155735
155736
155737
155738
155739
155740
155741
155742
155743
155744
155745
155746
155747
155748
155749
155750
155751
155752
155753
155754
155755
155756
155757
155758
155759
155760
155761
155762
155763
155764
155765
155766
155767
155768
155769
155770
155771
155772
155773
155774
155775
155776
155777
155778
155779
155780
155781
155782
155783
155784
155785
155786
155787
155788
155789
155790
155791
155792
155793
155794
155795
155796
155797
155798
155799
155800
155801
155802
155803
155804
155805
155806
155807
155808
155809
155810
155811
155812
155813
155814
155815
155816
155817
155818
155819
155820
155821
155822
155823
155824
155825
155826
155827
155828
155829
155830
155831
155832
155833
155834
155835
155836
155837
155838
155839
155840
155841
155842
155843
155844
155845
155846
155847
155848
155849
155850
155851
155852
155853
155854
155855
155856
155857
155858
155859
155860
155861
155862
155863
155864
155865
155866
155867
155868
155869
155870
155871
155872
155873
155874
155875
155876
155877
155878
155879
155880
155881
155882
155883
155884
155885
155886
155887
155888
155889
155890
155891
155892
155893
155894
155895
155896
155897
155898
155899
155900
155901
155902
155903
155904
155905
155906
155907
155908
155909
155910
155911
155912
155913
155914
155915
155916
155917
155918
155919
155920
155921
155922
155923
155924
155925
155926
155927
155928
155929
155930
155931
155932
155933
155934
155935
155936
155937
155938
155939
155940
155941
155942
155943
155944
155945
155946
155947
155948
155949
155950
155951
155952
155953
155954
155955
155956
155957
155958
155959
155960
155961
155962
155963
155964
155965
155966
155967
155968
155969
155970
155971
155972
155973
155974
155975
155976
155977
155978
155979
155980
155981
155982
155983
155984
155985
155986
155987
155988
155989
155990
155991
155992
155993
155994
155995
155996
155997
155998
155999
156000
156001
156002
156003
156004
156005
156006
156007
156008
156009
156010
156011
156012
156013
156014
156015
156016
156017
156018
156019
156020
156021
156022
156023
156024
156025
156026
156027
156028
156029
156030
156031
156032
156033
156034
156035
156036
156037
156038
156039
156040
156041
156042
156043
156044
156045
156046
156047
156048
156049
156050
156051
156052
156053
156054
156055
156056
156057
156058
156059
156060
156061
156062
156063
156064
156065
156066
156067
156068
156069
156070
156071
156072
156073
156074
156075
156076
156077
156078
156079
156080
156081
156082
156083
156084
156085
156086
156087
156088
156089
156090
156091
156092
156093
156094
156095
156096
156097
156098
156099
156100
156101
156102
156103
156104
156105
156106
156107
156108
156109
156110
156111
156112
156113
156114
156115
156116
156117
156118
156119
156120
156121
156122
156123
156124
156125
156126
156127
156128
156129
156130
156131
156132
156133
156134
156135
156136
156137
156138
156139
156140
156141
156142
156143
156144
156145
156146
156147
156148
156149
156150
156151
156152
156153
156154
156155
156156
156157
156158
156159
156160
156161
156162
156163
156164
156165
156166
156167
156168
156169
156170
156171
156172
156173
156174
156175
156176
156177
156178
156179
156180
156181
156182
156183
156184
156185
156186
156187
156188
156189
156190
156191
156192
156193
156194
156195
156196
156197
156198
156199
156200
156201
156202
156203
156204
156205
156206
156207
156208
156209
156210
156211
156212
156213
156214
156215
156216
156217
156218
156219
156220
156221
156222
156223
156224
156225
156226
156227
156228
156229
156230
156231
156232
156233
156234
156235
156236
156237
156238
156239
156240
156241
156242
156243
156244
156245
156246
156247
156248
156249
156250
156251
156252
156253
156254
156255
156256
156257
156258
156259
156260
156261
156262
156263
156264
156265
156266
156267
156268
156269
156270
156271
156272
156273
156274
156275
156276
156277
156278
156279
156280
156281
156282
156283
156284
156285
156286
156287
156288
156289
156290
156291
156292
156293
156294
156295
156296
156297
156298
156299
156300
156301
156302
156303
156304
156305
156306
156307
156308
156309
156310
156311
156312
156313
156314
156315
156316
156317
156318
156319
156320
156321
156322
156323
156324
156325
156326
156327
156328
156329
156330
156331
156332
156333
156334
156335
156336
156337
156338
156339
156340
156341
156342
156343
156344
156345
156346
156347
156348
156349
156350
156351
156352
156353
156354
156355
156356
156357
156358
156359
156360
156361
156362
156363
156364
156365
156366
156367
156368
156369
156370
156371
156372
156373
156374
156375
156376
156377
156378
156379
156380
156381
156382
156383
156384
156385
156386
156387
156388
156389
156390
156391
156392
156393
156394
156395
156396
156397
156398
156399
156400
156401
156402
156403
156404
156405
156406
156407
156408
156409
156410
156411
156412
156413
156414
156415
156416
156417
156418
156419
156420
156421
156422
156423
156424
156425
156426
156427
156428
156429
156430
156431
156432
156433
156434
156435
156436
156437
156438
156439
156440
156441
156442
156443
156444
156445
156446
156447
156448
156449
156450
156451
156452
156453
156454
156455
156456
156457
156458
156459
156460
156461
156462
156463
156464
156465
156466
156467
156468
156469
156470
156471
156472
156473
156474
156475
156476
156477
156478
156479
156480
156481
156482
156483
156484
156485
156486
156487
156488
156489
156490
156491
156492
156493
156494
156495
156496
156497
156498
156499
156500
156501
156502
156503
156504
156505
156506
156507
156508
156509
156510
156511
156512
156513
156514
156515
156516
156517
156518
156519
156520
156521
156522
156523
156524
156525
156526
156527
156528
156529
156530
156531
156532
156533
156534
156535
156536
156537
156538
156539
156540
156541
156542
156543
156544
156545
156546
156547
156548
156549
156550
156551
156552
156553
156554
156555
156556
156557
156558
156559
156560
156561
156562
156563
156564
156565
156566
156567
156568
156569
156570
156571
156572
156573
156574
156575
156576
156577
156578
156579
156580
156581
156582
156583
156584
156585
156586
156587
156588
156589
156590
156591
156592
156593
156594
156595
156596
156597
156598
156599
156600
156601
156602
156603
156604
156605
156606
156607
156608
156609
156610
156611
156612
156613
156614
156615
156616
156617
156618
156619
156620
156621
156622
156623
156624
156625
156626
156627
156628
156629
156630
156631
156632
156633
156634
156635
156636
156637
156638
156639
156640
156641
156642
156643
156644
156645
156646
156647
156648
156649
156650
156651
156652
156653
156654
156655
156656
156657
156658
156659
156660
156661
156662
156663
156664
156665
156666
156667
156668
156669
156670
156671
156672
156673
156674
156675
156676
156677
156678
156679
156680
156681
156682
156683
156684
156685
156686
156687
156688
156689
156690
156691
156692
156693
156694
156695
156696
156697
156698
156699
156700
156701
156702
156703
156704
156705
156706
156707
156708
156709
156710
156711
156712
156713
156714
156715
156716
156717
156718
156719
156720
156721
156722
156723
156724
156725
156726
156727
156728
156729
156730
156731
156732
156733
156734
156735
156736
156737
156738
156739
156740
156741
156742
156743
156744
156745
156746
156747
156748
156749
156750
156751
156752
156753
156754
156755
156756
156757
156758
156759
156760
156761
156762
156763
156764
156765
156766
156767
156768
156769
156770
156771
156772
156773
156774
156775
156776
156777
156778
156779
156780
156781
156782
156783
156784
156785
156786
156787
156788
156789
156790
156791
156792
156793
156794
156795
156796
156797
156798
156799
156800
156801
156802
156803
156804
156805
156806
156807
156808
156809
156810
156811
156812
156813
156814
156815
156816
156817
156818
156819
156820
156821
156822
156823
156824
156825
156826
156827
156828
156829
156830
156831
156832
156833
156834
156835
156836
156837
156838
156839
156840
156841
156842
156843
156844
156845
156846
156847
156848
156849
156850
156851
156852
156853
156854
156855
156856
156857
156858
156859
156860
156861
156862
156863
156864
156865
156866
156867
156868
156869
156870
156871
156872
156873
156874
156875
156876
156877
156878
156879
156880
156881
156882
156883
156884
156885
156886
156887
156888
156889
156890
156891
156892
156893
156894
156895
156896
156897
156898
156899
156900
156901
156902
156903
156904
156905
156906
156907
156908
156909
156910
156911
156912
156913
156914
156915
156916
156917
156918
156919
156920
156921
156922
156923
156924
156925
156926
156927
156928
156929
156930
156931
156932
156933
156934
156935
156936
156937
156938
156939
156940
156941
156942
156943
156944
156945
156946
156947
156948
156949
156950
156951
156952
156953
156954
156955
156956
156957
156958
156959
156960
156961
156962
156963
156964
156965
156966
156967
156968
156969
156970
156971
156972
156973
156974
156975
156976
156977
156978
156979
156980
156981
156982
156983
156984
156985
156986
156987
156988
156989
156990
156991
156992
156993
156994
156995
156996
156997
156998
156999
157000
157001
157002
157003
157004
157005
157006
157007
157008
157009
157010
157011
157012
157013
157014
157015
157016
157017
157018
157019
157020
157021
157022
157023
157024
157025
157026
157027
157028
157029
157030
157031
157032
157033
157034
157035
157036
157037
157038
157039
157040
157041
157042
157043
157044
157045
157046
157047
157048
157049
157050
157051
157052
157053
157054
157055
157056
157057
157058
157059
157060
157061
157062
157063
157064
157065
157066
157067
157068
157069
157070
157071
157072
157073
157074
157075
157076
157077
157078
157079
157080
157081
157082
157083
157084
157085
157086
157087
157088
157089
157090
157091
157092
157093
157094
157095
157096
157097
157098
157099
157100
157101
157102
157103
157104
157105
157106
157107
157108
157109
157110
157111
157112
157113
157114
157115
157116
157117
157118
157119
157120
157121
157122
157123
157124
157125
157126
157127
157128
157129
157130
157131
157132
157133
157134
157135
157136
157137
157138
157139
157140
157141
157142
157143
157144
157145
157146
157147
157148
157149
157150
157151
157152
157153
157154
157155
157156
157157
157158
157159
157160
157161
157162
157163
157164
157165
157166
157167
157168
157169
157170
157171
157172
157173
157174
157175
157176
157177
157178
157179
157180
157181
157182
157183
157184
157185
157186
157187
157188
157189
157190
157191
157192
157193
157194
157195
157196
157197
157198
157199
157200
157201
157202
157203
157204
157205
157206
157207
157208
157209
157210
157211
157212
157213
157214
157215
157216
157217
157218
157219
157220
157221
157222
157223
157224
157225
157226
157227
157228
157229
157230
157231
157232
157233
157234
157235
157236
157237
157238
157239
157240
157241
157242
157243
157244
157245
157246
157247
157248
157249
157250
157251
157252
157253
157254
157255
157256
157257
157258
157259
157260
157261
157262
157263
157264
157265
157266
157267
157268
157269
157270
157271
157272
157273
157274
157275
157276
157277
157278
157279
157280
157281
157282
157283
157284
157285
157286
157287
157288
157289
157290
157291
157292
157293
157294
157295
157296
157297
157298
157299
157300
157301
157302
157303
157304
157305
157306
157307
157308
157309
157310
157311
157312
157313
157314
157315
157316
157317
157318
157319
157320
157321
157322
157323
157324
157325
157326
157327
157328
157329
157330
157331
157332
157333
157334
157335
157336
157337
157338
157339
157340
157341
157342
157343
157344
157345
157346
157347
157348
157349
157350
157351
157352
157353
157354
157355
157356
157357
157358
157359
157360
157361
157362
157363
157364
157365
157366
157367
157368
157369
157370
157371
157372
157373
157374
157375
157376
157377
157378
157379
157380
157381
157382
157383
157384
157385
157386
157387
157388
157389
157390
157391
157392
157393
157394
157395
157396
157397
157398
157399
157400
157401
157402
157403
157404
157405
157406
157407
157408
157409
157410
157411
157412
157413
157414
157415
157416
157417
157418
157419
157420
157421
157422
157423
157424
157425
157426
157427
157428
157429
157430
157431
157432
157433
157434
157435
157436
157437
157438
157439
157440
157441
157442
157443
157444
157445
157446
157447
157448
157449
157450
157451
157452
157453
157454
157455
157456
157457
157458
157459
157460
157461
157462
157463
157464
157465
157466
157467
157468
157469
157470
157471
157472
157473
157474
157475
157476
157477
157478
157479
157480
157481
157482
157483
157484
157485
157486
157487
157488
157489
157490
157491
157492
157493
157494
157495
157496
157497
157498
157499
157500
157501
157502
157503
157504
157505
157506
157507
157508
157509
157510
157511
157512
157513
157514
157515
157516
157517
157518
157519
157520
157521
157522
157523
157524
157525
157526
157527
157528
157529
157530
157531
157532
157533
157534
157535
157536
157537
157538
157539
157540
157541
157542
157543
157544
157545
157546
157547
157548
157549
157550
157551
157552
157553
157554
157555
157556
157557
157558
157559
157560
157561
157562
157563
157564
157565
157566
157567
157568
157569
157570
157571
157572
157573
157574
157575
157576
157577
157578
157579
157580
157581
157582
157583
157584
157585
157586
157587
157588
157589
157590
157591
157592
157593
157594
157595
157596
157597
157598
157599
157600
157601
157602
157603
157604
157605
157606
157607
157608
157609
157610
157611
157612
157613
157614
157615
157616
157617
157618
157619
157620
157621
157622
157623
157624
157625
157626
157627
157628
157629
157630
157631
157632
157633
157634
157635
157636
157637
157638
157639
157640
157641
157642
157643
157644
157645
157646
157647
157648
157649
157650
157651
157652
157653
157654
157655
157656
157657
157658
157659
157660
157661
157662
157663
157664
157665
157666
157667
157668
157669
157670
157671
157672
157673
157674
157675
157676
157677
157678
157679
157680
157681
157682
157683
157684
157685
157686
157687
157688
157689
157690
157691
157692
157693
157694
157695
157696
157697
157698
157699
157700
157701
157702
157703
157704
157705
157706
157707
157708
157709
157710
157711
157712
157713
157714
157715
157716
157717
157718
157719
157720
157721
157722
157723
157724
157725
157726
157727
157728
157729
157730
157731
157732
157733
157734
157735
157736
157737
157738
157739
157740
157741
157742
157743
157744
157745
157746
157747
157748
157749
157750
157751
157752
157753
157754
157755
157756
157757
157758
157759
157760
157761
157762
157763
157764
157765
157766
157767
157768
157769
157770
157771
157772
157773
157774
157775
157776
157777
157778
157779
157780
157781
157782
157783
157784
157785
157786
157787
157788
157789
157790
157791
157792
157793
157794
157795
157796
157797
157798
157799
157800
157801
157802
157803
157804
157805
157806
157807
157808
157809
157810
157811
157812
157813
157814
157815
157816
157817
157818
157819
157820
157821
157822
157823
157824
157825
157826
157827
157828
157829
157830
157831
157832
157833
157834
157835
157836
157837
157838
157839
157840
157841
157842
157843
157844
157845
157846
157847
157848
157849
157850
157851
157852
157853
157854
157855
157856
157857
157858
157859
157860
157861
157862
157863
157864
157865
157866
157867
157868
157869
157870
157871
157872
157873
157874
157875
157876
157877
157878
157879
157880
157881
157882
157883
157884
157885
157886
157887
157888
157889
157890
157891
157892
157893
157894
157895
157896
157897
157898
157899
157900
157901
157902
157903
157904
157905
157906
157907
157908
157909
157910
157911
157912
157913
157914
157915
157916
157917
157918
157919
157920
157921
157922
157923
157924
157925
157926
157927
157928
157929
157930
157931
157932
157933
157934
157935
157936
157937
157938
157939
157940
157941
157942
157943
157944
157945
157946
157947
157948
157949
157950
157951
157952
157953
157954
157955
157956
157957
157958
157959
157960
157961
157962
157963
157964
157965
157966
157967
157968
157969
157970
157971
157972
157973
157974
157975
157976
157977
157978
157979
157980
157981
157982
157983
157984
157985
157986
157987
157988
157989
157990
157991
157992
157993
157994
157995
157996
157997
157998
157999
158000
158001
158002
158003
158004
158005
158006
158007
158008
158009
158010
158011
158012
158013
158014
158015
158016
158017
158018
158019
158020
158021
158022
158023
158024
158025
158026
158027
158028
158029
158030
158031
158032
158033
158034
158035
158036
158037
158038
158039
158040
158041
158042
158043
158044
158045
158046
158047
158048
158049
158050
158051
158052
158053
158054
158055
158056
158057
158058
158059
158060
158061
158062
158063
158064
158065
158066
158067
158068
158069
158070
158071
158072
158073
158074
158075
158076
158077
158078
158079
158080
158081
158082
158083
158084
158085
158086
158087
158088
158089
158090
158091
158092
158093
158094
158095
158096
158097
158098
158099
158100
158101
158102
158103
158104
158105
158106
158107
158108
158109
158110
158111
158112
158113
158114
158115
158116
158117
158118
158119
158120
158121
158122
158123
158124
158125
158126
158127
158128
158129
158130
158131
158132
158133
158134
158135
158136
158137
158138
158139
158140
158141
158142
158143
158144
158145
158146
158147
158148
158149
158150
158151
158152
158153
158154
158155
158156
158157
158158
158159
158160
158161
158162
158163
158164
158165
158166
158167
158168
158169
158170
158171
158172
158173
158174
158175
158176
158177
158178
158179
158180
158181
158182
158183
158184
158185
158186
158187
158188
158189
158190
158191
158192
158193
158194
158195
158196
158197
158198
158199
158200
158201
158202
158203
158204
158205
158206
158207
158208
158209
158210
158211
158212
158213
158214
158215
158216
158217
158218
158219
158220
158221
158222
158223
158224
158225
158226
158227
158228
158229
158230
158231
158232
158233
158234
158235
158236
158237
158238
158239
158240
158241
158242
158243
158244
158245
158246
158247
158248
158249
158250
158251
158252
158253
158254
158255
158256
158257
158258
158259
158260
158261
158262
158263
158264
158265
158266
158267
158268
158269
158270
158271
158272
158273
158274
158275
158276
158277
158278
158279
158280
158281
158282
158283
158284
158285
158286
158287
158288
158289
158290
158291
158292
158293
158294
158295
158296
158297
158298
158299
158300
158301
158302
158303
158304
158305
158306
158307
158308
158309
158310
158311
158312
158313
158314
158315
158316
158317
158318
158319
158320
158321
158322
158323
158324
158325
158326
158327
158328
158329
158330
158331
158332
158333
158334
158335
158336
158337
158338
158339
158340
158341
158342
158343
158344
158345
158346
158347
158348
158349
158350
158351
158352
158353
158354
158355
158356
158357
158358
158359
158360
158361
158362
158363
158364
158365
158366
158367
158368
158369
158370
158371
158372
158373
158374
158375
158376
158377
158378
158379
158380
158381
158382
158383
158384
158385
158386
158387
158388
158389
158390
158391
158392
158393
158394
158395
158396
158397
158398
158399
158400
158401
158402
158403
158404
158405
158406
158407
158408
158409
158410
158411
158412
158413
158414
158415
158416
158417
158418
158419
158420
158421
158422
158423
158424
158425
158426
158427
158428
158429
158430
158431
158432
158433
158434
158435
158436
158437
158438
158439
158440
158441
158442
158443
158444
158445
158446
158447
158448
158449
158450
158451
158452
158453
158454
158455
158456
158457
158458
158459
158460
158461
158462
158463
158464
158465
158466
158467
158468
158469
158470
158471
158472
158473
158474
158475
158476
158477
158478
158479
158480
158481
158482
158483
158484
158485
158486
158487
158488
158489
158490
158491
158492
158493
158494
158495
158496
158497
158498
158499
158500
158501
158502
158503
158504
158505
158506
158507
158508
158509
158510
158511
158512
158513
158514
158515
158516
158517
158518
158519
158520
158521
158522
158523
158524
158525
158526
158527
158528
158529
158530
158531
158532
158533
158534
158535
158536
158537
158538
158539
158540
158541
158542
158543
158544
158545
158546
158547
158548
158549
158550
158551
158552
158553
158554
158555
158556
158557
158558
158559
158560
158561
158562
158563
158564
158565
158566
158567
158568
158569
158570
158571
158572
158573
158574
158575
158576
158577
158578
158579
158580
158581
158582
158583
158584
158585
158586
158587
158588
158589
158590
158591
158592
158593
158594
158595
158596
158597
158598
158599
158600
158601
158602
158603
158604
158605
158606
158607
158608
158609
158610
158611
158612
158613
158614
158615
158616
158617
158618
158619
158620
158621
158622
158623
158624
158625
158626
158627
158628
158629
158630
158631
158632
158633
158634
158635
158636
158637
158638
158639
158640
158641
158642
158643
158644
158645
158646
158647
158648
158649
158650
158651
158652
158653
158654
158655
158656
158657
158658
158659
158660
158661
158662
158663
158664
158665
158666
158667
158668
158669
158670
158671
158672
158673
158674
158675
158676
158677
158678
158679
158680
158681
158682
158683
158684
158685
158686
158687
158688
158689
158690
158691
158692
158693
158694
158695
158696
158697
158698
158699
158700
158701
158702
158703
158704
158705
158706
158707
158708
158709
158710
158711
158712
158713
158714
158715
158716
158717
158718
158719
158720
158721
158722
158723
158724
158725
158726
158727
158728
158729
158730
158731
158732
158733
158734
158735
158736
158737
158738
158739
158740
158741
158742
158743
158744
158745
158746
158747
158748
158749
158750
158751
158752
158753
158754
158755
158756
158757
158758
158759
158760
158761
158762
158763
158764
158765
158766
158767
158768
158769
158770
158771
158772
158773
158774
158775
158776
158777
158778
158779
158780
158781
158782
158783
158784
158785
158786
158787
158788
158789
158790
158791
158792
158793
158794
158795
158796
158797
158798
158799
158800
158801
158802
158803
158804
158805
158806
158807
158808
158809
158810
158811
158812
158813
158814
158815
158816
158817
158818
158819
158820
158821
158822
158823
158824
158825
158826
158827
158828
158829
158830
158831
158832
158833
158834
158835
158836
158837
158838
158839
158840
158841
158842
158843
158844
158845
158846
158847
158848
158849
158850
158851
158852
158853
158854
158855
158856
158857
158858
158859
158860
158861
158862
158863
158864
158865
158866
158867
158868
158869
158870
158871
158872
158873
158874
158875
158876
158877
158878
158879
158880
158881
158882
158883
158884
158885
158886
158887
158888
158889
158890
158891
158892
158893
158894
158895
158896
158897
158898
158899
158900
158901
158902
158903
158904
158905
158906
158907
158908
158909
158910
158911
158912
158913
158914
158915
158916
158917
158918
158919
158920
158921
158922
158923
158924
158925
158926
158927
158928
158929
158930
158931
158932
158933
158934
158935
158936
158937
158938
158939
158940
158941
158942
158943
158944
158945
158946
158947
158948
158949
158950
158951
158952
158953
158954
158955
158956
158957
158958
158959
158960
158961
158962
158963
158964
158965
158966
158967
158968
158969
158970
158971
158972
158973
158974
158975
158976
158977
158978
158979
158980
158981
158982
158983
158984
158985
158986
158987
158988
158989
158990
158991
158992
158993
158994
158995
158996
158997
158998
158999
159000
159001
159002
159003
159004
159005
159006
159007
159008
159009
159010
159011
159012
159013
159014
159015
159016
159017
159018
159019
159020
159021
159022
159023
159024
159025
159026
159027
159028
159029
159030
159031
159032
159033
159034
159035
159036
159037
159038
159039
159040
159041
159042
159043
159044
159045
159046
159047
159048
159049
159050
159051
159052
159053
159054
159055
159056
159057
159058
159059
159060
159061
159062
159063
159064
159065
159066
159067
159068
159069
159070
159071
159072
159073
159074
159075
159076
159077
159078
159079
159080
159081
159082
159083
159084
159085
159086
159087
159088
159089
159090
159091
159092
159093
159094
159095
159096
159097
159098
159099
159100
159101
159102
159103
159104
159105
159106
159107
159108
159109
159110
159111
159112
159113
159114
159115
159116
159117
159118
159119
159120
159121
159122
159123
159124
159125
159126
159127
159128
159129
159130
159131
159132
159133
159134
159135
159136
159137
159138
159139
159140
159141
159142
159143
159144
159145
159146
159147
159148
159149
159150
159151
159152
159153
159154
159155
159156
159157
159158
159159
159160
159161
159162
159163
159164
159165
159166
159167
159168
159169
159170
159171
159172
159173
159174
159175
159176
159177
159178
159179
159180
159181
159182
159183
159184
159185
159186
159187
159188
159189
159190
159191
159192
159193
159194
159195
159196
159197
159198
159199
159200
159201
159202
159203
159204
159205
159206
159207
159208
159209
159210
159211
159212
159213
159214
159215
159216
159217
159218
159219
159220
159221
159222
159223
159224
159225
159226
159227
159228
159229
159230
159231
159232
159233
159234
159235
159236
159237
159238
159239
159240
159241
159242
159243
159244
159245
159246
159247
159248
159249
159250
159251
159252
159253
159254
159255
159256
159257
159258
159259
159260
159261
159262
159263
159264
159265
159266
159267
159268
159269
159270
159271
159272
159273
159274
159275
159276
159277
159278
159279
159280
159281
159282
159283
159284
159285
159286
159287
159288
159289
159290
159291
159292
159293
159294
159295
159296
159297
159298
159299
159300
159301
159302
159303
159304
159305
159306
159307
159308
159309
159310
159311
159312
159313
159314
159315
159316
159317
159318
159319
159320
159321
159322
159323
159324
159325
159326
159327
159328
159329
159330
159331
159332
159333
159334
159335
159336
159337
159338
159339
159340
159341
159342
159343
159344
159345
159346
159347
159348
159349
159350
159351
159352
159353
159354
159355
159356
159357
159358
159359
159360
159361
159362
159363
159364
159365
159366
159367
159368
159369
159370
159371
159372
159373
159374
159375
159376
159377
159378
159379
159380
159381
159382
159383
159384
159385
159386
159387
159388
159389
159390
159391
159392
159393
159394
159395
159396
159397
159398
159399
159400
159401
159402
159403
159404
159405
159406
159407
159408
159409
159410
159411
159412
159413
159414
159415
159416
159417
159418
159419
159420
159421
159422
159423
159424
159425
159426
159427
159428
159429
159430
159431
159432
159433
159434
159435
159436
159437
159438
159439
159440
159441
159442
159443
159444
159445
159446
159447
159448
159449
159450
159451
159452
159453
159454
159455
159456
159457
159458
159459
159460
159461
159462
159463
159464
159465
159466
159467
159468
159469
159470
159471
159472
159473
159474
159475
159476
159477
159478
159479
159480
159481
159482
159483
159484
159485
159486
159487
159488
159489
159490
159491
159492
159493
159494
159495
159496
159497
159498
159499
159500
159501
159502
159503
159504
159505
159506
159507
159508
159509
159510
159511
159512
159513
159514
159515
159516
159517
159518
159519
159520
159521
159522
159523
159524
159525
159526
159527
159528
159529
159530
159531
159532
159533
159534
159535
159536
159537
159538
159539
159540
159541
159542
159543
159544
159545
159546
159547
159548
159549
159550
159551
159552
159553
159554
159555
159556
159557
159558
159559
159560
159561
159562
159563
159564
159565
159566
159567
159568
159569
159570
159571
159572
159573
159574
159575
159576
159577
159578
159579
159580
159581
159582
159583
159584
159585
159586
159587
159588
159589
159590
159591
159592
159593
159594
159595
159596
159597
159598
159599
159600
159601
159602
159603
159604
159605
159606
159607
159608
159609
159610
159611
159612
159613
159614
159615
159616
159617
159618
159619
159620
159621
159622
159623
159624
159625
159626
159627
159628
159629
159630
159631
159632
159633
159634
159635
159636
159637
159638
159639
159640
159641
159642
159643
159644
159645
159646
159647
159648
159649
159650
159651
159652
159653
159654
159655
159656
159657
159658
159659
159660
159661
159662
159663
159664
159665
159666
159667
159668
159669
159670
159671
159672
159673
159674
159675
159676
159677
159678
159679
159680
159681
159682
159683
159684
159685
159686
159687
159688
159689
159690
159691
159692
159693
159694
159695
159696
159697
159698
159699
159700
159701
159702
159703
159704
159705
159706
159707
159708
159709
159710
159711
159712
159713
159714
159715
159716
159717
159718
159719
159720
159721
159722
159723
159724
159725
159726
159727
159728
159729
159730
159731
159732
159733
159734
159735
159736
159737
159738
159739
159740
159741
159742
159743
159744
159745
159746
159747
159748
159749
159750
159751
159752
159753
159754
159755
159756
159757
159758
159759
159760
159761
159762
159763
159764
159765
159766
159767
159768
159769
159770
159771
159772
159773
159774
159775
159776
159777
159778
159779
159780
159781
159782
159783
159784
159785
159786
159787
159788
159789
159790
159791
159792
159793
159794
159795
159796
159797
159798
159799
159800
159801
159802
159803
159804
159805
159806
159807
159808
159809
159810
159811
159812
159813
159814
159815
159816
159817
159818
159819
159820
159821
159822
159823
159824
159825
159826
159827
159828
159829
159830
159831
159832
159833
159834
159835
159836
159837
159838
159839
159840
159841
159842
159843
159844
159845
159846
159847
159848
159849
159850
159851
159852
159853
159854
159855
159856
159857
159858
159859
159860
159861
159862
159863
159864
159865
159866
159867
159868
159869
159870
159871
159872
159873
159874
159875
159876
159877
159878
159879
159880
159881
159882
159883
159884
159885
159886
159887
159888
159889
159890
159891
159892
159893
159894
159895
159896
159897
159898
159899
159900
159901
159902
159903
159904
159905
159906
159907
159908
159909
159910
159911
159912
159913
159914
159915
159916
159917
159918
159919
159920
159921
159922
159923
159924
159925
159926
159927
159928
159929
159930
159931
159932
159933
159934
159935
159936
159937
159938
159939
159940
159941
159942
159943
159944
159945
159946
159947
159948
159949
159950
159951
159952
159953
159954
159955
159956
159957
159958
159959
159960
159961
159962
159963
159964
159965
159966
159967
159968
159969
159970
159971
159972
159973
159974
159975
159976
159977
159978
159979
159980
159981
159982
159983
159984
159985
159986
159987
159988
159989
159990
159991
159992
159993
159994
159995
159996
159997
159998
159999
160000
160001
160002
160003
160004
160005
160006
160007
160008
160009
160010
160011
160012
160013
160014
160015
160016
160017
160018
160019
160020
160021
160022
160023
160024
160025
160026
160027
160028
160029
160030
160031
160032
160033
160034
160035
160036
160037
160038
160039
160040
160041
160042
160043
160044
160045
160046
160047
160048
160049
160050
160051
160052
160053
160054
160055
160056
160057
160058
160059
160060
160061
160062
160063
160064
160065
160066
160067
160068
160069
160070
160071
160072
160073
160074
160075
160076
160077
160078
160079
160080
160081
160082
160083
160084
160085
160086
160087
160088
160089
160090
160091
160092
160093
160094
160095
160096
160097
160098
160099
160100
160101
160102
160103
160104
160105
160106
160107
160108
160109
160110
160111
160112
160113
160114
160115
160116
160117
160118
160119
160120
160121
160122
160123
160124
160125
160126
160127
160128
160129
160130
160131
160132
160133
160134
160135
160136
160137
160138
160139
160140
160141
160142
160143
160144
160145
160146
160147
160148
160149
160150
160151
160152
160153
160154
160155
160156
160157
160158
160159
160160
160161
160162
160163
160164
160165
160166
160167
160168
160169
160170
160171
160172
160173
160174
160175
160176
160177
160178
160179
160180
160181
160182
160183
160184
160185
160186
160187
160188
160189
160190
160191
160192
160193
160194
160195
160196
160197
160198
160199
160200
160201
160202
160203
160204
160205
160206
160207
160208
160209
160210
160211
160212
160213
160214
160215
160216
160217
160218
160219
160220
160221
160222
160223
160224
160225
160226
160227
160228
160229
160230
160231
160232
160233
160234
160235
160236
160237
160238
160239
160240
160241
160242
160243
160244
160245
160246
160247
160248
160249
160250
160251
160252
160253
160254
160255
160256
160257
160258
160259
160260
160261
160262
160263
160264
160265
160266
160267
160268
160269
160270
160271
160272
160273
160274
160275
160276
160277
160278
160279
160280
160281
160282
160283
160284
160285
160286
160287
160288
160289
160290
160291
160292
160293
160294
160295
160296
160297
160298
160299
160300
160301
160302
160303
160304
160305
160306
160307
160308
160309
160310
160311
160312
160313
160314
160315
160316
160317
160318
160319
160320
160321
160322
160323
160324
160325
160326
160327
160328
160329
160330
160331
160332
160333
160334
160335
160336
160337
160338
160339
160340
160341
160342
160343
160344
160345
160346
160347
160348
160349
160350
160351
160352
160353
160354
160355
160356
160357
160358
160359
160360
160361
160362
160363
160364
160365
160366
160367
160368
160369
160370
160371
160372
160373
160374
160375
160376
160377
160378
160379
160380
160381
160382
160383
160384
160385
160386
160387
160388
160389
160390
160391
160392
160393
160394
160395
160396
160397
160398
160399
160400
160401
160402
160403
160404
160405
160406
160407
160408
160409
160410
160411
160412
160413
160414
160415
160416
160417
160418
160419
160420
160421
160422
160423
160424
160425
160426
160427
160428
160429
160430
160431
160432
160433
160434
160435
160436
160437
160438
160439
160440
160441
160442
160443
160444
160445
160446
160447
160448
160449
160450
160451
160452
160453
160454
160455
160456
160457
160458
160459
160460
160461
160462
160463
160464
160465
160466
160467
160468
160469
160470
160471
160472
160473
160474
160475
160476
160477
160478
160479
160480
160481
160482
160483
160484
160485
160486
160487
160488
160489
160490
160491
160492
160493
160494
160495
160496
160497
160498
160499
160500
160501
160502
160503
160504
160505
160506
160507
160508
160509
160510
160511
160512
160513
160514
160515
160516
160517
160518
160519
160520
160521
160522
160523
160524
160525
160526
160527
160528
160529
160530
160531
160532
160533
160534
160535
160536
160537
160538
160539
160540
160541
160542
160543
160544
160545
160546
160547
160548
160549
160550
160551
160552
160553
160554
160555
160556
160557
160558
160559
160560
160561
160562
160563
160564
160565
160566
160567
160568
160569
160570
160571
160572
160573
160574
160575
160576
160577
160578
160579
160580
160581
160582
160583
160584
160585
160586
160587
160588
160589
160590
160591
160592
160593
160594
160595
160596
160597
160598
160599
160600
160601
160602
160603
160604
160605
160606
160607
160608
160609
160610
160611
160612
160613
160614
160615
160616
160617
160618
160619
160620
160621
160622
160623
160624
160625
160626
160627
160628
160629
160630
160631
160632
160633
160634
160635
160636
160637
160638
160639
160640
160641
160642
160643
160644
160645
160646
160647
160648
160649
160650
160651
160652
160653
160654
160655
160656
160657
160658
160659
160660
160661
160662
160663
160664
160665
160666
160667
160668
160669
160670
160671
160672
160673
160674
160675
160676
160677
160678
160679
160680
160681
160682
160683
160684
160685
160686
160687
160688
160689
160690
160691
160692
160693
160694
160695
160696
160697
160698
160699
160700
160701
160702
160703
160704
160705
160706
160707
160708
160709
160710
160711
160712
160713
160714
160715
160716
160717
160718
160719
160720
160721
160722
160723
160724
160725
160726
160727
160728
160729
160730
160731
160732
160733
160734
160735
160736
160737
160738
160739
160740
160741
160742
160743
160744
160745
160746
160747
160748
160749
160750
160751
160752
160753
160754
160755
160756
160757
160758
160759
160760
160761
160762
160763
160764
160765
160766
160767
160768
160769
160770
160771
160772
160773
160774
160775
160776
160777
160778
160779
160780
160781
160782
160783
160784
160785
160786
160787
160788
160789
160790
160791
160792
160793
160794
160795
160796
160797
160798
160799
160800
160801
160802
160803
160804
160805
160806
160807
160808
160809
160810
160811
160812
160813
160814
160815
160816
160817
160818
160819
160820
160821
160822
160823
160824
160825
160826
160827
160828
160829
160830
160831
160832
160833
160834
160835
160836
160837
160838
160839
160840
160841
160842
160843
160844
160845
160846
160847
160848
160849
160850
160851
160852
160853
160854
160855
160856
160857
160858
160859
160860
160861
160862
160863
160864
160865
160866
160867
160868
160869
160870
160871
160872
160873
160874
160875
160876
160877
160878
160879
160880
160881
160882
160883
160884
160885
160886
160887
160888
160889
160890
160891
160892
160893
160894
160895
160896
160897
160898
160899
160900
160901
160902
160903
160904
160905
160906
160907
160908
160909
160910
160911
160912
160913
160914
160915
160916
160917
160918
160919
160920
160921
160922
160923
160924
160925
160926
160927
160928
160929
160930
160931
160932
160933
160934
160935
160936
160937
160938
160939
160940
160941
160942
160943
160944
160945
160946
160947
160948
160949
160950
160951
160952
160953
160954
160955
160956
160957
160958
160959
160960
160961
160962
160963
160964
160965
160966
160967
160968
160969
160970
160971
160972
160973
160974
160975
160976
160977
160978
160979
160980
160981
160982
160983
160984
160985
160986
160987
160988
160989
160990
160991
160992
160993
160994
160995
160996
160997
160998
160999
161000
161001
161002
161003
161004
161005
161006
161007
161008
161009
161010
161011
161012
161013
161014
161015
161016
161017
161018
161019
161020
161021
161022
161023
161024
161025
161026
161027
161028
161029
161030
161031
161032
161033
161034
161035
161036
161037
161038
161039
161040
161041
161042
161043
161044
161045
161046
161047
161048
161049
161050
161051
161052
161053
161054
161055
161056
161057
161058
161059
161060
161061
161062
161063
161064
161065
161066
161067
161068
161069
161070
161071
161072
161073
161074
161075
161076
161077
161078
161079
161080
161081
161082
161083
161084
161085
161086
161087
161088
161089
161090
161091
161092
161093
161094
161095
161096
161097
161098
161099
161100
161101
161102
161103
161104
161105
161106
161107
161108
161109
161110
161111
161112
161113
161114
161115
161116
161117
161118
161119
161120
161121
161122
161123
161124
161125
161126
161127
161128
161129
161130
161131
161132
161133
161134
161135
161136
161137
161138
161139
161140
161141
161142
161143
161144
161145
161146
161147
161148
161149
161150
161151
161152
161153
161154
161155
161156
161157
161158
161159
161160
161161
161162
161163
161164
161165
161166
161167
161168
161169
161170
161171
161172
161173
161174
161175
161176
161177
161178
161179
161180
161181
161182
161183
161184
161185
161186
161187
161188
161189
161190
161191
161192
161193
161194
161195
161196
161197
161198
161199
161200
161201
161202
161203
161204
161205
161206
161207
161208
161209
161210
161211
161212
161213
161214
161215
161216
161217
161218
161219
161220
161221
161222
161223
161224
161225
161226
161227
161228
161229
161230
161231
161232
161233
161234
161235
161236
161237
161238
161239
161240
161241
161242
161243
161244
161245
161246
161247
161248
161249
161250
161251
161252
161253
161254
161255
161256
161257
161258
161259
161260
161261
161262
161263
161264
161265
161266
161267
161268
161269
161270
161271
161272
161273
161274
161275
161276
161277
161278
161279
161280
161281
161282
161283
161284
161285
161286
161287
161288
161289
161290
161291
161292
161293
161294
161295
161296
161297
161298
161299
161300
161301
161302
161303
161304
161305
161306
161307
161308
161309
161310
161311
161312
161313
161314
161315
161316
161317
161318
161319
161320
161321
161322
161323
161324
161325
161326
161327
161328
161329
161330
161331
161332
161333
161334
161335
161336
161337
161338
161339
161340
161341
161342
161343
161344
161345
161346
161347
161348
161349
161350
161351
161352
161353
161354
161355
161356
161357
161358
161359
161360
161361
161362
161363
161364
161365
161366
161367
161368
161369
161370
161371
161372
161373
161374
161375
161376
161377
161378
161379
161380
161381
161382
161383
161384
161385
161386
161387
161388
161389
161390
161391
161392
161393
161394
161395
161396
161397
161398
161399
161400
161401
161402
161403
161404
161405
161406
161407
161408
161409
161410
161411
161412
161413
161414
161415
161416
161417
161418
161419
161420
161421
161422
161423
161424
161425
161426
161427
161428
161429
161430
161431
161432
161433
161434
161435
161436
161437
161438
161439
161440
161441
161442
161443
161444
161445
161446
161447
161448
161449
161450
161451
161452
161453
161454
161455
161456
161457
161458
161459
161460
161461
161462
161463
161464
161465
161466
161467
161468
161469
161470
161471
161472
161473
161474
161475
161476
161477
161478
161479
161480
161481
161482
161483
161484
161485
161486
161487
161488
161489
161490
161491
161492
161493
161494
161495
161496
161497
161498
161499
161500
161501
161502
161503
161504
161505
161506
161507
161508
161509
161510
161511
161512
161513
161514
161515
161516
161517
161518
161519
161520
161521
161522
161523
161524
161525
161526
161527
161528
161529
161530
161531
161532
161533
161534
161535
161536
161537
161538
161539
161540
161541
161542
161543
161544
161545
161546
161547
161548
161549
161550
161551
161552
161553
161554
161555
161556
161557
161558
161559
161560
161561
161562
161563
161564
161565
161566
161567
161568
161569
161570
161571
161572
161573
161574
161575
161576
161577
161578
161579
161580
161581
161582
161583
161584
161585
161586
161587
161588
161589
161590
161591
161592
161593
161594
161595
161596
161597
161598
161599
161600
161601
161602
161603
161604
161605
161606
161607
161608
161609
161610
161611
161612
161613
161614
161615
161616
161617
161618
161619
161620
161621
161622
161623
161624
161625
161626
161627
161628
161629
161630
161631
161632
161633
161634
161635
161636
161637
161638
161639
161640
161641
161642
161643
161644
161645
161646
161647
161648
161649
161650
161651
161652
161653
161654
161655
161656
161657
161658
161659
161660
161661
161662
161663
161664
161665
161666
161667
161668
161669
161670
161671
161672
161673
161674
161675
161676
161677
161678
161679
161680
161681
161682
161683
161684
161685
161686
161687
161688
161689
161690
161691
161692
161693
161694
161695
161696
161697
161698
161699
161700
161701
161702
161703
161704
161705
161706
161707
161708
161709
161710
161711
161712
161713
161714
161715
161716
161717
161718
161719
161720
161721
161722
161723
161724
161725
161726
161727
161728
161729
161730
161731
161732
161733
161734
161735
161736
161737
161738
161739
161740
161741
161742
161743
161744
161745
161746
161747
161748
161749
161750
161751
161752
161753
161754
161755
161756
161757
161758
161759
161760
161761
161762
161763
161764
161765
161766
161767
161768
161769
161770
161771
161772
161773
161774
161775
161776
161777
161778
161779
161780
161781
161782
161783
161784
161785
161786
161787
161788
161789
161790
161791
161792
161793
161794
161795
161796
161797
161798
161799
161800
161801
161802
161803
161804
161805
161806
161807
161808
161809
161810
161811
161812
161813
161814
161815
161816
161817
161818
161819
161820
161821
161822
161823
161824
161825
161826
161827
161828
161829
161830
161831
161832
161833
161834
161835
161836
161837
161838
161839
161840
161841
161842
161843
161844
161845
161846
161847
161848
161849
161850
161851
161852
161853
161854
161855
161856
161857
161858
161859
161860
161861
161862
161863
161864
161865
161866
161867
161868
161869
161870
161871
161872
161873
161874
161875
161876
161877
161878
161879
161880
161881
161882
161883
161884
161885
161886
161887
161888
161889
161890
161891
161892
161893
161894
161895
161896
161897
161898
161899
161900
161901
161902
161903
161904
161905
161906
161907
161908
161909
161910
161911
161912
161913
161914
161915
161916
161917
161918
161919
161920
161921
161922
161923
161924
161925
161926
161927
161928
161929
161930
161931
161932
161933
161934
161935
161936
161937
161938
161939
161940
161941
161942
161943
161944
161945
161946
161947
161948
161949
161950
161951
161952
161953
161954
161955
161956
161957
161958
161959
161960
161961
161962
161963
161964
161965
161966
161967
161968
161969
161970
161971
161972
161973
161974
161975
161976
161977
161978
161979
161980
161981
161982
161983
161984
161985
161986
161987
161988
161989
161990
161991
161992
161993
161994
161995
161996
161997
161998
161999
162000
162001
162002
162003
162004
162005
162006
162007
162008
162009
162010
162011
162012
162013
162014
162015
162016
162017
162018
162019
162020
162021
162022
162023
162024
162025
162026
162027
162028
162029
162030
162031
162032
162033
162034
162035
162036
162037
162038
162039
162040
162041
162042
162043
162044
162045
162046
162047
162048
162049
162050
162051
162052
162053
162054
162055
162056
162057
162058
162059
162060
162061
162062
162063
162064
162065
162066
162067
162068
162069
162070
162071
162072
162073
162074
162075
162076
162077
162078
162079
162080
162081
162082
162083
162084
162085
162086
162087
162088
162089
162090
162091
162092
162093
162094
162095
162096
162097
162098
162099
162100
162101
162102
162103
162104
162105
162106
162107
162108
162109
162110
162111
162112
162113
162114
162115
162116
162117
162118
162119
162120
162121
162122
162123
162124
162125
162126
162127
162128
162129
162130
162131
162132
162133
162134
162135
162136
162137
162138
162139
162140
162141
162142
162143
162144
162145
162146
162147
162148
162149
162150
162151
162152
162153
162154
162155
162156
162157
162158
162159
162160
162161
162162
162163
162164
162165
162166
162167
162168
162169
162170
162171
162172
162173
162174
162175
162176
162177
162178
162179
162180
162181
162182
162183
162184
162185
162186
162187
162188
162189
162190
162191
162192
162193
162194
162195
162196
162197
162198
162199
162200
162201
162202
162203
162204
162205
162206
162207
162208
162209
162210
162211
162212
162213
162214
162215
162216
162217
162218
162219
162220
162221
162222
162223
162224
162225
162226
162227
162228
162229
162230
162231
162232
162233
162234
162235
162236
162237
162238
162239
162240
162241
162242
162243
162244
162245
162246
162247
162248
162249
162250
162251
162252
162253
162254
162255
162256
162257
162258
162259
162260
162261
162262
162263
162264
162265
162266
162267
162268
162269
162270
162271
162272
162273
162274
162275
162276
162277
162278
162279
162280
162281
162282
162283
162284
162285
162286
162287
162288
162289
162290
162291
162292
162293
162294
162295
162296
162297
162298
162299
162300
162301
162302
162303
162304
162305
162306
162307
162308
162309
162310
162311
162312
162313
162314
162315
162316
162317
162318
162319
162320
162321
162322
162323
162324
162325
162326
162327
162328
162329
162330
162331
162332
162333
162334
162335
162336
162337
162338
162339
162340
162341
162342
162343
162344
162345
162346
162347
162348
162349
162350
162351
162352
162353
162354
162355
162356
162357
162358
162359
162360
162361
162362
162363
162364
162365
162366
162367
162368
162369
162370
162371
162372
162373
162374
162375
162376
162377
162378
162379
162380
162381
162382
162383
162384
162385
162386
162387
162388
162389
162390
162391
162392
162393
162394
162395
162396
162397
162398
162399
162400
162401
162402
162403
162404
162405
162406
162407
162408
162409
162410
162411
162412
162413
162414
162415
162416
162417
162418
162419
162420
162421
162422
162423
162424
162425
162426
162427
162428
162429
162430
162431
162432
162433
162434
162435
162436
162437
162438
162439
162440
162441
162442
162443
162444
162445
162446
162447
162448
162449
162450
162451
162452
162453
162454
162455
162456
162457
162458
162459
162460
162461
162462
162463
162464
162465
162466
162467
162468
162469
162470
162471
162472
162473
162474
162475
162476
162477
162478
162479
162480
162481
162482
162483
162484
162485
162486
162487
162488
162489
162490
162491
162492
162493
162494
162495
162496
162497
162498
162499
162500
162501
162502
162503
162504
162505
162506
162507
162508
162509
162510
162511
162512
162513
162514
162515
162516
162517
162518
162519
162520
162521
162522
162523
162524
162525
162526
162527
162528
162529
162530
162531
162532
162533
162534
162535
162536
162537
162538
162539
162540
162541
162542
162543
162544
162545
162546
162547
162548
162549
162550
162551
162552
162553
162554
162555
162556
162557
162558
162559
162560
162561
162562
162563
162564
162565
162566
162567
162568
162569
162570
162571
162572
162573
162574
162575
162576
162577
162578
162579
162580
162581
162582
162583
162584
162585
162586
162587
162588
162589
162590
162591
162592
162593
162594
162595
162596
162597
162598
162599
162600
162601
162602
162603
162604
162605
162606
162607
162608
162609
162610
162611
162612
162613
162614
162615
162616
162617
162618
162619
162620
162621
162622
162623
162624
162625
162626
162627
162628
162629
162630
162631
162632
162633
162634
162635
162636
162637
162638
162639
162640
162641
162642
162643
162644
162645
162646
162647
162648
162649
162650
162651
162652
162653
162654
162655
162656
162657
162658
162659
162660
162661
162662
162663
162664
162665
162666
162667
162668
162669
162670
162671
162672
162673
162674
162675
162676
162677
162678
162679
162680
162681
162682
162683
162684
162685
162686
162687
162688
162689
162690
162691
162692
162693
162694
162695
162696
162697
162698
162699
162700
162701
162702
162703
162704
162705
162706
162707
162708
162709
162710
162711
162712
162713
162714
162715
162716
162717
162718
162719
162720
162721
162722
162723
162724
162725
162726
162727
162728
162729
162730
162731
162732
162733
162734
162735
162736
162737
162738
162739
162740
162741
162742
162743
162744
162745
162746
162747
162748
162749
162750
162751
162752
162753
162754
162755
162756
162757
162758
162759
162760
162761
162762
162763
162764
162765
162766
162767
162768
162769
162770
162771
162772
162773
162774
162775
162776
162777
162778
162779
162780
162781
162782
162783
162784
162785
162786
162787
162788
162789
162790
162791
162792
162793
162794
162795
162796
162797
162798
162799
162800
162801
162802
162803
162804
162805
162806
162807
162808
162809
162810
162811
162812
162813
162814
162815
162816
162817
162818
162819
162820
162821
162822
162823
162824
162825
162826
162827
162828
162829
162830
162831
162832
162833
162834
162835
162836
162837
162838
162839
162840
162841
162842
162843
162844
162845
162846
162847
162848
162849
162850
162851
162852
162853
162854
162855
162856
162857
162858
162859
162860
162861
162862
162863
162864
162865
162866
162867
162868
162869
162870
162871
162872
162873
162874
162875
162876
162877
162878
162879
162880
162881
162882
162883
162884
162885
162886
162887
162888
162889
162890
162891
162892
162893
162894
162895
162896
162897
162898
162899
162900
162901
162902
162903
162904
162905
162906
162907
162908
162909
162910
162911
162912
162913
162914
162915
162916
162917
162918
162919
162920
162921
162922
162923
162924
162925
162926
162927
162928
162929
162930
162931
162932
162933
162934
162935
162936
162937
162938
162939
162940
162941
162942
162943
162944
162945
162946
162947
162948
162949
162950
162951
162952
162953
162954
162955
162956
162957
162958
162959
162960
162961
162962
162963
162964
162965
162966
162967
162968
162969
162970
162971
162972
162973
162974
162975
162976
162977
162978
162979
162980
162981
162982
162983
162984
162985
162986
162987
162988
162989
162990
162991
162992
162993
162994
162995
162996
162997
162998
162999
163000
163001
163002
163003
163004
163005
163006
163007
163008
163009
163010
163011
163012
163013
163014
163015
163016
163017
163018
163019
163020
163021
163022
163023
163024
163025
163026
163027
163028
163029
163030
163031
163032
163033
163034
163035
163036
163037
163038
163039
163040
163041
163042
163043
163044
163045
163046
163047
163048
163049
163050
163051
163052
163053
163054
163055
163056
163057
163058
163059
163060
163061
163062
163063
163064
163065
163066
163067
163068
163069
163070
163071
163072
163073
163074
163075
163076
163077
163078
163079
163080
163081
163082
163083
163084
163085
163086
163087
163088
163089
163090
163091
163092
163093
163094
163095
163096
163097
163098
163099
163100
163101
163102
163103
163104
163105
163106
163107
163108
163109
163110
163111
163112
163113
163114
163115
163116
163117
163118
163119
163120
163121
163122
163123
163124
163125
163126
163127
163128
163129
163130
163131
163132
163133
163134
163135
163136
163137
163138
163139
163140
163141
163142
163143
163144
163145
163146
163147
163148
163149
163150
163151
163152
163153
163154
163155
163156
163157
163158
163159
163160
163161
163162
163163
163164
163165
163166
163167
163168
163169
163170
163171
163172
163173
163174
163175
163176
163177
163178
163179
163180
163181
163182
163183
163184
163185
163186
163187
163188
163189
163190
163191
163192
163193
163194
163195
163196
163197
163198
163199
163200
163201
163202
163203
163204
163205
163206
163207
163208
163209
163210
163211
163212
163213
163214
163215
163216
163217
163218
163219
163220
163221
163222
163223
163224
163225
163226
163227
163228
163229
163230
163231
163232
163233
163234
163235
163236
163237
163238
163239
163240
163241
163242
163243
163244
163245
163246
163247
163248
163249
163250
163251
163252
163253
163254
163255
163256
163257
163258
163259
163260
163261
163262
163263
163264
163265
163266
163267
163268
163269
163270
163271
163272
163273
163274
163275
163276
163277
163278
163279
163280
163281
163282
163283
163284
163285
163286
163287
163288
163289
163290
163291
163292
163293
163294
163295
163296
163297
163298
163299
163300
163301
163302
163303
163304
163305
163306
163307
163308
163309
163310
163311
163312
163313
163314
163315
163316
163317
163318
163319
163320
163321
163322
163323
163324
163325
163326
163327
163328
163329
163330
163331
163332
163333
163334
163335
163336
163337
163338
163339
163340
163341
163342
163343
163344
163345
163346
163347
163348
163349
163350
163351
163352
163353
163354
163355
163356
163357
163358
163359
163360
163361
163362
163363
163364
163365
163366
163367
163368
163369
163370
163371
163372
163373
163374
163375
163376
163377
163378
163379
163380
163381
163382
163383
163384
163385
163386
163387
163388
163389
163390
163391
163392
163393
163394
163395
163396
163397
163398
163399
163400
163401
163402
163403
163404
163405
163406
163407
163408
163409
163410
163411
163412
163413
163414
163415
163416
163417
163418
163419
163420
163421
163422
163423
163424
163425
163426
163427
163428
163429
163430
163431
163432
163433
163434
163435
163436
163437
163438
163439
163440
163441
163442
163443
163444
163445
163446
163447
163448
163449
163450
163451
163452
163453
163454
163455
163456
163457
163458
163459
163460
163461
163462
163463
163464
163465
163466
163467
163468
163469
163470
163471
163472
163473
163474
163475
163476
163477
163478
163479
163480
163481
163482
163483
163484
163485
163486
163487
163488
163489
163490
163491
163492
163493
163494
163495
163496
163497
163498
163499
163500
163501
163502
163503
163504
163505
163506
163507
163508
163509
163510
163511
163512
163513
163514
163515
163516
163517
163518
163519
163520
163521
163522
163523
163524
163525
163526
163527
163528
163529
163530
163531
163532
163533
163534
163535
163536
163537
163538
163539
163540
163541
163542
163543
163544
163545
163546
163547
163548
163549
163550
163551
163552
163553
163554
163555
163556
163557
163558
163559
163560
163561
163562
163563
163564
163565
163566
163567
163568
163569
163570
163571
163572
163573
163574
163575
163576
163577
163578
163579
163580
163581
163582
163583
163584
163585
163586
163587
163588
163589
163590
163591
163592
163593
163594
163595
163596
163597
163598
163599
163600
163601
163602
163603
163604
163605
163606
163607
163608
163609
163610
163611
163612
163613
163614
163615
163616
163617
163618
163619
163620
163621
163622
163623
163624
163625
163626
163627
163628
163629
163630
163631
163632
163633
163634
163635
163636
163637
163638
163639
163640
163641
163642
163643
163644
163645
163646
163647
163648
163649
163650
163651
163652
163653
163654
163655
163656
163657
163658
163659
163660
163661
163662
163663
163664
163665
163666
163667
163668
163669
163670
163671
163672
163673
163674
163675
163676
163677
163678
163679
163680
163681
163682
163683
163684
163685
163686
163687
163688
163689
163690
163691
163692
163693
163694
163695
163696
163697
163698
163699
163700
163701
163702
163703
163704
163705
163706
163707
163708
163709
163710
163711
163712
163713
163714
163715
163716
163717
163718
163719
163720
163721
163722
163723
163724
163725
163726
163727
163728
163729
163730
163731
163732
163733
163734
163735
163736
163737
163738
163739
163740
163741
163742
163743
163744
163745
163746
163747
163748
163749
163750
163751
163752
163753
163754
163755
163756
163757
163758
163759
163760
163761
163762
163763
163764
163765
163766
163767
163768
163769
163770
163771
163772
163773
163774
163775
163776
163777
163778
163779
163780
163781
163782
163783
163784
163785
163786
163787
163788
163789
163790
163791
163792
163793
163794
163795
163796
163797
163798
163799
163800
163801
163802
163803
163804
163805
163806
163807
163808
163809
163810
163811
163812
163813
163814
163815
163816
163817
163818
163819
163820
163821
163822
163823
163824
163825
163826
163827
163828
163829
163830
163831
163832
163833
163834
163835
163836
163837
163838
163839
163840
163841
163842
163843
163844
163845
163846
163847
163848
163849
163850
163851
163852
163853
163854
163855
163856
163857
163858
163859
163860
163861
163862
163863
163864
163865
163866
163867
163868
163869
163870
163871
163872
163873
163874
163875
163876
163877
163878
163879
163880
163881
163882
163883
163884
163885
163886
163887
163888
163889
163890
163891
163892
163893
163894
163895
163896
163897
163898
163899
163900
163901
163902
163903
163904
163905
163906
163907
163908
163909
163910
163911
163912
163913
163914
163915
163916
163917
163918
163919
163920
163921
163922
163923
163924
163925
163926
163927
163928
163929
163930
163931
163932
163933
163934
163935
163936
163937
163938
163939
163940
163941
163942
163943
163944
163945
163946
163947
163948
163949
163950
163951
163952
163953
163954
163955
163956
163957
163958
163959
163960
163961
163962
163963
163964
163965
163966
163967
163968
163969
163970
163971
163972
163973
163974
163975
163976
163977
163978
163979
163980
163981
163982
163983
163984
163985
163986
163987
163988
163989
163990
163991
163992
163993
163994
163995
163996
163997
163998
163999
164000
164001
164002
164003
164004
164005
164006
164007
164008
164009
164010
164011
164012
164013
164014
164015
164016
164017
164018
164019
164020
164021
164022
164023
164024
164025
164026
164027
164028
164029
164030
164031
164032
164033
164034
164035
164036
164037
164038
164039
164040
164041
164042
164043
164044
164045
164046
164047
164048
164049
164050
164051
164052
164053
164054
164055
164056
164057
164058
164059
164060
164061
164062
164063
164064
164065
164066
164067
164068
164069
164070
164071
164072
164073
164074
164075
164076
164077
164078
164079
164080
164081
164082
164083
164084
164085
164086
164087
164088
164089
164090
164091
164092
164093
164094
164095
164096
164097
164098
164099
164100
164101
164102
164103
164104
164105
164106
164107
164108
164109
164110
164111
164112
164113
164114
164115
164116
164117
164118
164119
164120
164121
164122
164123
164124
164125
164126
164127
164128
164129
164130
164131
164132
164133
164134
164135
164136
164137
164138
164139
164140
164141
164142
164143
164144
164145
164146
164147
164148
164149
164150
164151
164152
164153
164154
164155
164156
164157
164158
164159
164160
164161
164162
164163
164164
164165
164166
164167
164168
164169
164170
164171
164172
164173
164174
164175
164176
164177
164178
164179
164180
164181
164182
164183
164184
164185
164186
164187
164188
164189
164190
164191
164192
164193
164194
164195
164196
164197
164198
164199
164200
164201
164202
164203
164204
164205
164206
164207
164208
164209
164210
164211
164212
164213
164214
164215
164216
164217
164218
164219
164220
164221
164222
164223
164224
164225
164226
164227
164228
164229
164230
164231
164232
164233
164234
164235
164236
164237
164238
164239
164240
164241
164242
164243
164244
164245
164246
164247
164248
164249
164250
164251
164252
164253
164254
164255
164256
164257
164258
164259
164260
164261
164262
164263
164264
164265
164266
164267
164268
164269
164270
164271
164272
164273
164274
164275
164276
164277
164278
164279
164280
164281
164282
164283
164284
164285
164286
164287
164288
164289
164290
164291
164292
164293
164294
164295
164296
164297
164298
164299
164300
164301
164302
164303
164304
164305
164306
164307
164308
164309
164310
164311
164312
164313
164314
164315
164316
164317
164318
164319
164320
164321
164322
164323
164324
164325
164326
164327
164328
164329
164330
164331
164332
164333
164334
164335
164336
164337
164338
164339
164340
164341
164342
164343
164344
164345
164346
164347
164348
164349
164350
164351
164352
164353
164354
164355
164356
164357
164358
164359
164360
164361
164362
164363
164364
164365
164366
164367
164368
164369
164370
164371
164372
164373
164374
164375
164376
164377
164378
164379
164380
164381
164382
164383
164384
164385
164386
164387
164388
164389
164390
164391
164392
164393
164394
164395
164396
164397
164398
164399
164400
164401
164402
164403
164404
164405
164406
164407
164408
164409
164410
164411
164412
164413
164414
164415
164416
164417
164418
164419
164420
164421
164422
164423
164424
164425
164426
164427
164428
164429
164430
164431
164432
164433
164434
164435
164436
164437
164438
164439
164440
164441
164442
164443
164444
164445
164446
164447
164448
164449
164450
164451
164452
164453
164454
164455
164456
164457
164458
164459
164460
164461
164462
164463
164464
164465
164466
164467
164468
164469
164470
164471
164472
164473
164474
164475
164476
164477
164478
164479
164480
164481
164482
164483
164484
164485
164486
164487
164488
164489
164490
164491
164492
164493
164494
164495
164496
164497
164498
164499
164500
164501
164502
164503
164504
164505
164506
164507
164508
164509
164510
164511
164512
164513
164514
164515
164516
164517
164518
164519
164520
164521
164522
164523
164524
164525
164526
164527
164528
164529
164530
164531
164532
164533
164534
164535
164536
164537
164538
164539
164540
164541
164542
164543
164544
164545
164546
164547
164548
164549
164550
164551
164552
164553
164554
164555
164556
164557
164558
164559
164560
164561
164562
164563
164564
164565
164566
164567
164568
164569
164570
164571
164572
164573
164574
164575
164576
164577
164578
164579
164580
164581
164582
164583
164584
164585
164586
164587
164588
164589
164590
164591
164592
164593
164594
164595
164596
164597
164598
164599
164600
164601
164602
164603
164604
164605
164606
164607
164608
164609
164610
164611
164612
164613
164614
164615
164616
164617
164618
164619
164620
164621
164622
164623
164624
164625
164626
164627
164628
164629
164630
164631
164632
164633
164634
164635
164636
164637
164638
164639
164640
164641
164642
164643
164644
164645
164646
164647
164648
164649
164650
164651
164652
164653
164654
164655
164656
164657
164658
164659
164660
164661
164662
164663
164664
164665
164666
164667
164668
164669
164670
164671
164672
164673
164674
164675
164676
164677
164678
164679
164680
164681
164682
164683
164684
164685
164686
164687
164688
164689
164690
164691
164692
164693
164694
164695
164696
164697
164698
164699
164700
164701
164702
164703
164704
164705
164706
164707
164708
164709
164710
164711
164712
164713
164714
164715
164716
164717
164718
164719
164720
164721
164722
164723
164724
164725
164726
164727
164728
164729
164730
164731
164732
164733
164734
164735
164736
164737
164738
164739
164740
164741
164742
164743
164744
164745
164746
164747
164748
164749
164750
164751
164752
164753
164754
164755
164756
164757
164758
164759
164760
164761
164762
164763
164764
164765
164766
164767
164768
164769
164770
164771
164772
164773
164774
164775
164776
164777
164778
164779
164780
164781
164782
164783
164784
164785
164786
164787
164788
164789
164790
164791
164792
164793
164794
164795
164796
164797
164798
164799
164800
164801
164802
164803
164804
164805
164806
164807
164808
164809
164810
164811
164812
164813
164814
164815
164816
164817
164818
164819
164820
164821
164822
164823
164824
164825
164826
164827
164828
164829
164830
164831
164832
164833
164834
164835
164836
164837
164838
164839
164840
164841
164842
164843
164844
164845
164846
164847
164848
164849
164850
164851
164852
164853
164854
164855
164856
164857
164858
164859
164860
164861
164862
164863
164864
164865
164866
164867
164868
164869
164870
164871
164872
164873
164874
164875
164876
164877
164878
164879
164880
164881
164882
164883
164884
164885
164886
164887
164888
164889
164890
164891
164892
164893
164894
164895
164896
164897
164898
164899
164900
164901
164902
164903
164904
164905
164906
164907
164908
164909
164910
164911
164912
164913
164914
164915
164916
164917
164918
164919
164920
164921
164922
164923
164924
164925
164926
164927
164928
164929
164930
164931
164932
164933
164934
164935
164936
164937
164938
164939
164940
164941
164942
164943
164944
164945
164946
164947
164948
164949
164950
164951
164952
164953
164954
164955
164956
164957
164958
164959
164960
164961
164962
164963
164964
164965
164966
164967
164968
164969
164970
164971
164972
164973
164974
164975
164976
164977
164978
164979
164980
164981
164982
164983
164984
164985
164986
164987
164988
164989
164990
164991
164992
164993
164994
164995
164996
164997
164998
164999
165000
165001
165002
165003
165004
165005
165006
165007
165008
165009
165010
165011
165012
165013
165014
165015
165016
165017
165018
165019
165020
165021
165022
165023
165024
165025
165026
165027
165028
165029
165030
165031
165032
165033
165034
165035
165036
165037
165038
165039
165040
165041
165042
165043
165044
165045
165046
165047
165048
165049
165050
165051
165052
165053
165054
165055
165056
165057
165058
165059
165060
165061
165062
165063
165064
165065
165066
165067
165068
165069
165070
165071
165072
165073
165074
165075
165076
165077
165078
165079
165080
165081
165082
165083
165084
165085
165086
165087
165088
165089
165090
165091
165092
165093
165094
165095
165096
165097
165098
165099
165100
165101
165102
165103
165104
165105
165106
165107
165108
165109
165110
165111
165112
165113
165114
165115
165116
165117
165118
165119
165120
165121
165122
165123
165124
165125
165126
165127
165128
165129
165130
165131
165132
165133
165134
165135
165136
165137
165138
165139
165140
165141
165142
165143
165144
165145
165146
165147
165148
165149
165150
165151
165152
165153
165154
165155
165156
165157
165158
165159
165160
165161
165162
165163
165164
165165
165166
165167
165168
165169
165170
165171
165172
165173
165174
165175
165176
165177
165178
165179
165180
165181
165182
165183
165184
165185
165186
165187
165188
165189
165190
165191
165192
165193
165194
165195
165196
165197
165198
165199
165200
165201
165202
165203
165204
165205
165206
165207
165208
165209
165210
165211
165212
165213
165214
165215
165216
165217
165218
165219
165220
165221
165222
165223
165224
165225
165226
165227
165228
165229
165230
165231
165232
165233
165234
165235
165236
165237
165238
165239
165240
165241
165242
165243
165244
165245
165246
165247
165248
165249
165250
165251
165252
165253
165254
165255
165256
165257
165258
165259
165260
165261
165262
165263
165264
165265
165266
165267
165268
165269
165270
165271
165272
165273
165274
165275
165276
165277
165278
165279
165280
165281
165282
165283
165284
165285
165286
165287
165288
165289
165290
165291
165292
165293
165294
165295
165296
165297
165298
165299
165300
165301
165302
165303
165304
165305
165306
165307
165308
165309
165310
165311
165312
165313
165314
165315
165316
165317
165318
165319
165320
165321
165322
165323
165324
165325
165326
165327
165328
165329
165330
165331
165332
165333
165334
165335
165336
165337
165338
165339
165340
165341
165342
165343
165344
165345
165346
165347
165348
165349
165350
165351
165352
165353
165354
165355
165356
165357
165358
165359
165360
165361
165362
165363
165364
165365
165366
165367
165368
165369
165370
165371
165372
165373
165374
165375
165376
165377
165378
165379
165380
165381
165382
165383
165384
165385
165386
165387
165388
165389
165390
165391
165392
165393
165394
165395
165396
165397
165398
165399
165400
165401
165402
165403
165404
165405
165406
165407
165408
165409
165410
165411
165412
165413
165414
165415
165416
165417
165418
165419
165420
165421
165422
165423
165424
165425
165426
165427
165428
165429
165430
165431
165432
165433
165434
165435
165436
165437
165438
165439
165440
165441
165442
165443
165444
165445
165446
165447
165448
165449
165450
165451
165452
165453
165454
165455
165456
165457
165458
165459
165460
165461
165462
165463
165464
165465
165466
165467
165468
165469
165470
165471
165472
165473
165474
165475
165476
165477
165478
165479
165480
165481
165482
165483
165484
165485
165486
165487
165488
165489
165490
165491
165492
165493
165494
165495
165496
165497
165498
165499
165500
165501
165502
165503
165504
165505
165506
165507
165508
165509
165510
165511
165512
165513
165514
165515
165516
165517
165518
165519
165520
165521
165522
165523
165524
165525
165526
165527
165528
165529
165530
165531
165532
165533
165534
165535
165536
165537
165538
165539
165540
165541
165542
165543
165544
165545
165546
165547
165548
165549
165550
165551
165552
165553
165554
165555
165556
165557
165558
165559
165560
165561
165562
165563
165564
165565
165566
165567
165568
165569
165570
165571
165572
165573
165574
165575
165576
165577
165578
165579
165580
165581
165582
165583
165584
165585
165586
165587
165588
165589
165590
165591
165592
165593
165594
165595
165596
165597
165598
165599
165600
165601
165602
165603
165604
165605
165606
165607
165608
165609
165610
165611
165612
165613
165614
165615
165616
165617
165618
165619
165620
165621
165622
165623
165624
165625
165626
165627
165628
165629
165630
165631
165632
165633
165634
165635
165636
165637
165638
165639
165640
165641
165642
165643
165644
165645
165646
165647
165648
165649
165650
165651
165652
165653
165654
165655
165656
165657
165658
165659
165660
165661
165662
165663
165664
165665
165666
165667
165668
165669
165670
165671
165672
165673
165674
165675
165676
165677
165678
165679
165680
165681
165682
165683
165684
165685
165686
165687
165688
165689
165690
165691
165692
165693
165694
165695
165696
165697
165698
165699
165700
165701
165702
165703
165704
165705
165706
165707
165708
165709
165710
165711
165712
165713
165714
165715
165716
165717
165718
165719
165720
165721
165722
165723
165724
165725
165726
165727
165728
165729
165730
165731
165732
165733
165734
165735
165736
165737
165738
165739
165740
165741
165742
165743
165744
165745
165746
165747
165748
165749
165750
165751
165752
165753
165754
165755
165756
165757
165758
165759
165760
165761
165762
165763
165764
165765
165766
165767
165768
165769
165770
165771
165772
165773
165774
165775
165776
165777
165778
165779
165780
165781
165782
165783
165784
165785
165786
165787
165788
165789
165790
165791
165792
165793
165794
165795
165796
165797
165798
165799
165800
165801
165802
165803
165804
165805
165806
165807
165808
165809
165810
165811
165812
165813
165814
165815
165816
165817
165818
165819
165820
165821
165822
165823
165824
165825
165826
165827
165828
165829
165830
165831
165832
165833
165834
165835
165836
165837
165838
165839
165840
165841
165842
165843
165844
165845
165846
165847
165848
165849
165850
165851
165852
165853
165854
165855
165856
165857
165858
165859
165860
165861
165862
165863
165864
165865
165866
165867
165868
165869
165870
165871
165872
165873
165874
165875
165876
165877
165878
165879
165880
165881
165882
165883
165884
165885
165886
165887
165888
165889
165890
165891
165892
165893
165894
165895
165896
165897
165898
165899
165900
165901
165902
165903
165904
165905
165906
165907
165908
165909
165910
165911
165912
165913
165914
165915
165916
165917
165918
165919
165920
165921
165922
165923
165924
165925
165926
165927
165928
165929
165930
165931
165932
165933
165934
165935
165936
165937
165938
165939
165940
165941
165942
165943
165944
165945
165946
165947
165948
165949
165950
165951
165952
165953
165954
165955
165956
165957
165958
165959
165960
165961
165962
165963
165964
165965
165966
165967
165968
165969
165970
165971
165972
165973
165974
165975
165976
165977
165978
165979
165980
165981
165982
165983
165984
165985
165986
165987
165988
165989
165990
165991
165992
165993
165994
165995
165996
165997
165998
165999
166000
166001
166002
166003
166004
166005
166006
166007
166008
166009
166010
166011
166012
166013
166014
166015
166016
166017
166018
166019
166020
166021
166022
166023
166024
166025
166026
166027
166028
166029
166030
166031
166032
166033
166034
166035
166036
166037
166038
166039
166040
166041
166042
166043
166044
166045
166046
166047
166048
166049
166050
166051
166052
166053
166054
166055
166056
166057
166058
166059
166060
166061
166062
166063
166064
166065
166066
166067
166068
166069
166070
166071
166072
166073
166074
166075
166076
166077
166078
166079
166080
166081
166082
166083
166084
166085
166086
166087
166088
166089
166090
166091
166092
166093
166094
166095
166096
166097
166098
166099
166100
166101
166102
166103
166104
166105
166106
166107
166108
166109
166110
166111
166112
166113
166114
166115
166116
166117
166118
166119
166120
166121
166122
166123
166124
166125
166126
166127
166128
166129
166130
166131
166132
166133
166134
166135
166136
166137
166138
166139
166140
166141
166142
166143
166144
166145
166146
166147
166148
166149
166150
166151
166152
166153
166154
166155
166156
166157
166158
166159
166160
166161
166162
166163
166164
166165
166166
166167
166168
166169
166170
166171
166172
166173
166174
166175
166176
166177
166178
166179
166180
166181
166182
166183
166184
166185
166186
166187
166188
166189
166190
166191
166192
166193
166194
166195
166196
166197
166198
166199
166200
166201
166202
166203
166204
166205
166206
166207
166208
166209
166210
166211
166212
166213
166214
166215
166216
166217
166218
166219
166220
166221
166222
166223
166224
166225
166226
166227
166228
166229
166230
166231
166232
166233
166234
166235
166236
166237
166238
166239
166240
166241
166242
166243
166244
166245
166246
166247
166248
166249
166250
166251
166252
166253
166254
166255
166256
166257
166258
166259
166260
166261
166262
166263
166264
166265
166266
166267
166268
166269
166270
166271
166272
166273
166274
166275
166276
166277
166278
166279
166280
166281
166282
166283
166284
166285
166286
166287
166288
166289
166290
166291
166292
166293
166294
166295
166296
166297
166298
166299
166300
166301
166302
166303
166304
166305
166306
166307
166308
166309
166310
166311
166312
166313
166314
166315
166316
166317
166318
166319
166320
166321
166322
166323
166324
166325
166326
166327
166328
166329
166330
166331
166332
166333
166334
166335
166336
166337
166338
166339
166340
166341
166342
166343
166344
166345
166346
166347
166348
166349
166350
166351
166352
166353
166354
166355
166356
166357
166358
166359
166360
166361
166362
166363
166364
166365
166366
166367
166368
166369
166370
166371
166372
166373
166374
166375
166376
166377
166378
166379
166380
166381
166382
166383
166384
166385
166386
166387
166388
166389
166390
166391
166392
166393
166394
166395
166396
166397
166398
166399
166400
166401
166402
166403
166404
166405
166406
166407
166408
166409
166410
166411
166412
166413
166414
166415
166416
166417
166418
166419
166420
166421
166422
166423
166424
166425
166426
166427
166428
166429
166430
166431
166432
166433
166434
166435
166436
166437
166438
166439
166440
166441
166442
166443
166444
166445
166446
166447
166448
166449
166450
166451
166452
166453
166454
166455
166456
166457
166458
166459
166460
166461
166462
166463
166464
166465
166466
166467
166468
166469
166470
166471
166472
166473
166474
166475
166476
166477
166478
166479
166480
166481
166482
166483
166484
166485
166486
166487
166488
166489
166490
166491
166492
166493
166494
166495
166496
166497
166498
166499
166500
166501
166502
166503
166504
166505
166506
166507
166508
166509
166510
166511
166512
166513
166514
166515
166516
166517
166518
166519
166520
166521
166522
166523
166524
166525
166526
166527
166528
166529
166530
166531
166532
166533
166534
166535
166536
166537
166538
166539
166540
166541
166542
166543
166544
166545
166546
166547
166548
166549
166550
166551
166552
166553
166554
166555
166556
166557
166558
166559
166560
166561
166562
166563
166564
166565
166566
166567
166568
166569
166570
166571
166572
166573
166574
166575
166576
166577
166578
166579
166580
166581
166582
166583
166584
166585
166586
166587
166588
166589
166590
166591
166592
166593
166594
166595
166596
166597
166598
166599
166600
166601
166602
166603
166604
166605
166606
166607
166608
166609
166610
166611
166612
166613
166614
166615
166616
166617
166618
166619
166620
166621
166622
166623
166624
166625
166626
166627
166628
166629
166630
166631
166632
166633
166634
166635
166636
166637
166638
166639
166640
166641
166642
166643
166644
166645
166646
166647
166648
166649
166650
166651
166652
166653
166654
166655
166656
166657
166658
166659
166660
166661
166662
166663
166664
166665
166666
166667
166668
166669
166670
166671
166672
166673
166674
166675
166676
166677
166678
166679
166680
166681
166682
166683
166684
166685
166686
166687
166688
166689
166690
166691
166692
166693
166694
166695
166696
166697
166698
166699
166700
166701
166702
166703
166704
166705
166706
166707
166708
166709
166710
166711
166712
166713
166714
166715
166716
166717
166718
166719
166720
166721
166722
166723
166724
166725
166726
166727
166728
166729
166730
166731
166732
166733
166734
166735
166736
166737
166738
166739
166740
166741
166742
166743
166744
166745
166746
166747
166748
166749
166750
166751
166752
166753
166754
166755
166756
166757
166758
166759
166760
166761
166762
166763
166764
166765
166766
166767
166768
166769
166770
166771
166772
166773
166774
166775
166776
166777
166778
166779
166780
166781
166782
166783
166784
166785
166786
166787
166788
166789
166790
166791
166792
166793
166794
166795
166796
166797
166798
166799
166800
166801
166802
166803
166804
166805
166806
166807
166808
166809
166810
166811
166812
166813
166814
166815
166816
166817
166818
166819
166820
166821
166822
166823
166824
166825
166826
166827
166828
166829
166830
166831
166832
166833
166834
166835
166836
166837
166838
166839
166840
166841
166842
166843
166844
166845
166846
166847
166848
166849
166850
166851
166852
166853
166854
166855
166856
166857
166858
166859
166860
166861
166862
166863
166864
166865
166866
166867
166868
166869
166870
166871
166872
166873
166874
166875
166876
166877
166878
166879
166880
166881
166882
166883
166884
166885
166886
166887
166888
166889
166890
166891
166892
166893
166894
166895
166896
166897
166898
166899
166900
166901
166902
166903
166904
166905
166906
166907
166908
166909
166910
166911
166912
166913
166914
166915
166916
166917
166918
166919
166920
166921
166922
166923
166924
166925
166926
166927
166928
166929
166930
166931
166932
166933
166934
166935
166936
166937
166938
166939
166940
166941
166942
166943
166944
166945
166946
166947
166948
166949
166950
166951
166952
166953
166954
166955
166956
166957
166958
166959
166960
166961
166962
166963
166964
166965
166966
166967
166968
166969
166970
166971
166972
166973
166974
166975
166976
166977
166978
166979
166980
166981
166982
166983
166984
166985
166986
166987
166988
166989
166990
166991
166992
166993
166994
166995
166996
166997
166998
166999
167000
167001
167002
167003
167004
167005
167006
167007
167008
167009
167010
167011
167012
167013
167014
167015
167016
167017
167018
167019
167020
167021
167022
167023
167024
167025
167026
167027
167028
167029
167030
167031
167032
167033
167034
167035
167036
167037
167038
167039
167040
167041
167042
167043
167044
167045
167046
167047
167048
167049
167050
167051
167052
167053
167054
167055
167056
167057
167058
167059
167060
167061
167062
167063
167064
167065
167066
167067
167068
167069
167070
167071
167072
167073
167074
167075
167076
167077
167078
167079
167080
167081
167082
167083
167084
167085
167086
167087
167088
167089
167090
167091
167092
167093
167094
167095
167096
167097
167098
167099
167100
167101
167102
167103
167104
167105
167106
167107
167108
167109
167110
167111
167112
167113
167114
167115
167116
167117
167118
167119
167120
167121
167122
167123
167124
167125
167126
167127
167128
167129
167130
167131
167132
167133
167134
167135
167136
167137
167138
167139
167140
167141
167142
167143
167144
167145
167146
167147
167148
167149
167150
167151
167152
167153
167154
167155
167156
167157
167158
167159
167160
167161
167162
167163
167164
167165
167166
167167
167168
167169
167170
167171
167172
167173
167174
167175
167176
167177
167178
167179
167180
167181
167182
167183
167184
167185
167186
167187
167188
167189
167190
167191
167192
167193
167194
167195
167196
167197
167198
167199
167200
167201
167202
167203
167204
167205
167206
167207
167208
167209
167210
167211
167212
167213
167214
167215
167216
167217
167218
167219
167220
167221
167222
167223
167224
167225
167226
167227
167228
167229
167230
167231
167232
167233
167234
167235
167236
167237
167238
167239
167240
167241
167242
167243
167244
167245
167246
167247
167248
167249
167250
167251
167252
167253
167254
167255
167256
167257
167258
167259
167260
167261
167262
167263
167264
167265
167266
167267
167268
167269
167270
167271
167272
167273
167274
167275
167276
167277
167278
167279
167280
167281
167282
167283
167284
167285
167286
167287
167288
167289
167290
167291
167292
167293
167294
167295
167296
167297
167298
167299
167300
167301
167302
167303
167304
167305
167306
167307
167308
167309
167310
167311
167312
167313
167314
167315
167316
167317
167318
167319
167320
167321
167322
167323
167324
167325
167326
167327
167328
167329
167330
167331
167332
167333
167334
167335
167336
167337
167338
167339
167340
167341
167342
167343
167344
167345
167346
167347
167348
167349
167350
167351
167352
167353
167354
167355
167356
167357
167358
167359
167360
167361
167362
167363
167364
167365
167366
167367
167368
167369
167370
167371
167372
167373
167374
167375
167376
167377
167378
167379
167380
167381
167382
167383
167384
167385
167386
167387
167388
167389
167390
167391
167392
167393
167394
167395
167396
167397
167398
167399
167400
167401
167402
167403
167404
167405
167406
167407
167408
167409
167410
167411
167412
167413
167414
167415
167416
167417
167418
167419
167420
167421
167422
167423
167424
167425
167426
167427
167428
167429
167430
167431
167432
167433
167434
167435
167436
167437
167438
167439
167440
167441
167442
167443
167444
167445
167446
167447
167448
167449
167450
167451
167452
167453
167454
167455
167456
167457
167458
167459
167460
167461
167462
167463
167464
167465
167466
167467
167468
167469
167470
167471
167472
167473
167474
167475
167476
167477
167478
167479
167480
167481
167482
167483
167484
167485
167486
167487
167488
167489
167490
167491
167492
167493
167494
167495
167496
167497
167498
167499
167500
167501
167502
167503
167504
167505
167506
167507
167508
167509
167510
167511
167512
167513
167514
167515
167516
167517
167518
167519
167520
167521
167522
167523
167524
167525
167526
167527
167528
167529
167530
167531
167532
167533
167534
167535
167536
167537
167538
167539
167540
167541
167542
167543
167544
167545
167546
167547
167548
167549
167550
167551
167552
167553
167554
167555
167556
167557
167558
167559
167560
167561
167562
167563
167564
167565
167566
167567
167568
167569
167570
167571
167572
167573
167574
167575
167576
167577
167578
167579
167580
167581
167582
167583
167584
167585
167586
167587
167588
167589
167590
167591
167592
167593
167594
167595
167596
167597
167598
167599
167600
167601
167602
167603
167604
167605
167606
167607
167608
167609
167610
167611
167612
167613
167614
167615
167616
167617
167618
167619
167620
167621
167622
167623
167624
167625
167626
167627
167628
167629
167630
167631
167632
167633
167634
167635
167636
167637
167638
167639
167640
167641
167642
167643
167644
167645
167646
167647
167648
167649
167650
167651
167652
167653
167654
167655
167656
167657
167658
167659
167660
167661
167662
167663
167664
167665
167666
167667
167668
167669
167670
167671
167672
167673
167674
167675
167676
167677
167678
167679
167680
167681
167682
167683
167684
167685
167686
167687
167688
167689
167690
167691
167692
167693
167694
167695
167696
167697
167698
167699
167700
167701
167702
167703
167704
167705
167706
167707
167708
167709
167710
167711
167712
167713
167714
167715
167716
167717
167718
167719
167720
167721
167722
167723
167724
167725
167726
167727
167728
167729
167730
167731
167732
167733
167734
167735
167736
167737
167738
167739
167740
167741
167742
167743
167744
167745
167746
167747
167748
167749
167750
167751
167752
167753
167754
167755
167756
167757
167758
167759
167760
167761
167762
167763
167764
167765
167766
167767
167768
167769
167770
167771
167772
167773
167774
167775
167776
167777
167778
167779
167780
167781
167782
167783
167784
167785
167786
167787
167788
167789
167790
167791
167792
167793
167794
167795
167796
167797
167798
167799
167800
167801
167802
167803
167804
167805
167806
167807
167808
167809
167810
167811
167812
167813
167814
167815
167816
167817
167818
167819
167820
167821
167822
167823
167824
167825
167826
167827
167828
167829
167830
167831
167832
167833
167834
167835
167836
167837
167838
167839
167840
167841
167842
167843
167844
167845
167846
167847
167848
167849
167850
167851
167852
167853
167854
167855
167856
167857
167858
167859
167860
167861
167862
167863
167864
167865
167866
167867
167868
167869
167870
167871
167872
167873
167874
167875
167876
167877
167878
167879
167880
167881
167882
167883
167884
167885
167886
167887
167888
167889
167890
167891
167892
167893
167894
167895
167896
167897
167898
167899
167900
167901
167902
167903
167904
167905
167906
167907
167908
167909
167910
167911
167912
167913
167914
167915
167916
167917
167918
167919
167920
167921
167922
167923
167924
167925
167926
167927
167928
167929
167930
167931
167932
167933
167934
167935
167936
167937
167938
167939
167940
167941
167942
167943
167944
167945
167946
167947
167948
167949
167950
167951
167952
167953
167954
167955
167956
167957
167958
167959
167960
167961
167962
167963
167964
167965
167966
167967
167968
167969
167970
167971
167972
167973
167974
167975
167976
167977
167978
167979
167980
167981
167982
167983
167984
167985
167986
167987
167988
167989
167990
167991
167992
167993
167994
167995
167996
167997
167998
167999
168000
168001
168002
168003
168004
168005
168006
168007
168008
168009
168010
168011
168012
168013
168014
168015
168016
168017
168018
168019
168020
168021
168022
168023
168024
168025
168026
168027
168028
168029
168030
168031
168032
168033
168034
168035
168036
168037
168038
168039
168040
168041
168042
168043
168044
168045
168046
168047
168048
168049
168050
168051
168052
168053
168054
168055
168056
168057
168058
168059
168060
168061
168062
168063
168064
168065
168066
168067
168068
168069
168070
168071
168072
168073
168074
168075
168076
168077
168078
168079
168080
168081
168082
168083
168084
168085
168086
168087
168088
168089
168090
168091
168092
168093
168094
168095
168096
168097
168098
168099
168100
168101
168102
168103
168104
168105
168106
168107
168108
168109
168110
168111
168112
168113
168114
168115
168116
168117
168118
168119
168120
168121
168122
168123
168124
168125
168126
168127
168128
168129
168130
168131
168132
168133
168134
168135
168136
168137
168138
168139
168140
168141
168142
168143
168144
168145
168146
168147
168148
168149
168150
168151
168152
168153
168154
168155
168156
168157
168158
168159
168160
168161
168162
168163
168164
168165
168166
168167
168168
168169
168170
168171
168172
168173
168174
168175
168176
168177
168178
168179
168180
168181
168182
168183
168184
168185
168186
168187
168188
168189
168190
168191
168192
168193
168194
168195
168196
168197
168198
168199
168200
168201
168202
168203
168204
168205
168206
168207
168208
168209
168210
168211
168212
168213
168214
168215
168216
168217
168218
168219
168220
168221
168222
168223
168224
168225
168226
168227
168228
168229
168230
168231
168232
168233
168234
168235
168236
168237
168238
168239
168240
168241
168242
168243
168244
168245
168246
168247
168248
168249
168250
168251
168252
168253
168254
168255
168256
168257
168258
168259
168260
168261
168262
168263
168264
168265
168266
168267
168268
168269
168270
168271
168272
168273
168274
168275
168276
168277
168278
168279
168280
168281
168282
168283
168284
168285
168286
168287
168288
168289
168290
168291
168292
168293
168294
168295
168296
168297
168298
168299
168300
168301
168302
168303
168304
168305
168306
168307
168308
168309
168310
168311
168312
168313
168314
168315
168316
168317
168318
168319
168320
168321
168322
168323
168324
168325
168326
168327
168328
168329
168330
168331
168332
168333
168334
168335
168336
168337
168338
168339
168340
168341
168342
168343
168344
168345
168346
168347
168348
168349
168350
168351
168352
168353
168354
168355
168356
168357
168358
168359
168360
168361
168362
168363
168364
168365
168366
168367
168368
168369
168370
168371
168372
168373
168374
168375
168376
168377
168378
168379
168380
168381
168382
168383
168384
168385
168386
168387
168388
168389
168390
168391
168392
168393
168394
168395
168396
168397
168398
168399
168400
168401
168402
168403
168404
168405
168406
168407
168408
168409
168410
168411
168412
168413
168414
168415
168416
168417
168418
168419
168420
168421
168422
168423
168424
168425
168426
168427
168428
168429
168430
168431
168432
168433
168434
168435
168436
168437
168438
168439
168440
168441
168442
168443
168444
168445
168446
168447
168448
168449
168450
168451
168452
168453
168454
168455
168456
168457
168458
168459
168460
168461
168462
168463
168464
168465
168466
168467
168468
168469
168470
168471
168472
168473
168474
168475
168476
168477
168478
168479
168480
168481
168482
168483
168484
168485
168486
168487
168488
168489
168490
168491
168492
168493
168494
168495
168496
168497
168498
168499
168500
168501
168502
168503
168504
168505
168506
168507
168508
168509
168510
168511
168512
168513
168514
168515
168516
168517
168518
168519
168520
168521
168522
168523
168524
168525
168526
168527
168528
168529
168530
168531
168532
168533
168534
168535
168536
168537
168538
168539
168540
168541
168542
168543
168544
168545
168546
168547
168548
168549
168550
168551
168552
168553
168554
168555
168556
168557
168558
168559
168560
168561
168562
168563
168564
168565
168566
168567
168568
168569
168570
168571
168572
168573
168574
168575
168576
168577
168578
168579
168580
168581
168582
168583
168584
168585
168586
168587
168588
168589
168590
168591
168592
168593
168594
168595
168596
168597
168598
168599
168600
168601
168602
168603
168604
168605
168606
168607
168608
168609
168610
168611
168612
168613
168614
168615
168616
168617
168618
168619
168620
168621
168622
168623
168624
168625
168626
168627
168628
168629
168630
168631
168632
168633
168634
168635
168636
168637
168638
168639
168640
168641
168642
168643
168644
168645
168646
168647
168648
168649
168650
168651
168652
168653
168654
168655
168656
168657
168658
168659
168660
168661
168662
168663
168664
168665
168666
168667
168668
168669
168670
168671
168672
168673
168674
168675
168676
168677
168678
168679
168680
168681
168682
168683
168684
168685
168686
168687
168688
168689
168690
168691
168692
168693
168694
168695
168696
168697
168698
168699
168700
168701
168702
168703
168704
168705
168706
168707
168708
168709
168710
168711
168712
168713
168714
168715
168716
168717
168718
168719
168720
168721
168722
168723
168724
168725
168726
168727
168728
168729
168730
168731
168732
168733
168734
168735
168736
168737
168738
168739
168740
168741
168742
168743
168744
168745
168746
168747
168748
168749
168750
168751
168752
168753
168754
168755
168756
168757
168758
168759
168760
168761
168762
168763
168764
168765
168766
168767
168768
168769
168770
168771
168772
168773
168774
168775
168776
168777
168778
168779
168780
168781
168782
168783
168784
168785
168786
168787
168788
168789
168790
168791
168792
168793
168794
168795
168796
168797
168798
168799
168800
168801
168802
168803
168804
168805
168806
168807
168808
168809
168810
168811
168812
168813
168814
168815
168816
168817
168818
168819
168820
168821
168822
168823
168824
168825
168826
168827
168828
168829
168830
168831
168832
168833
168834
168835
168836
168837
168838
168839
168840
168841
168842
168843
168844
168845
168846
168847
168848
168849
168850
168851
168852
168853
168854
168855
168856
168857
168858
168859
168860
168861
168862
168863
168864
168865
168866
168867
168868
168869
168870
168871
168872
168873
168874
168875
168876
168877
168878
168879
168880
168881
168882
168883
168884
168885
168886
168887
168888
168889
168890
168891
168892
168893
168894
168895
168896
168897
168898
168899
168900
168901
168902
168903
168904
168905
168906
168907
168908
168909
168910
168911
168912
168913
168914
168915
168916
168917
168918
168919
168920
168921
168922
168923
168924
168925
168926
168927
168928
168929
168930
168931
168932
168933
168934
168935
168936
168937
168938
168939
168940
168941
168942
168943
168944
168945
168946
168947
168948
168949
168950
168951
168952
168953
168954
168955
168956
168957
168958
168959
168960
168961
168962
168963
168964
168965
168966
168967
168968
168969
168970
168971
168972
168973
168974
168975
168976
168977
168978
168979
168980
168981
168982
168983
168984
168985
168986
168987
168988
168989
168990
168991
168992
168993
168994
168995
168996
168997
168998
168999
169000
169001
169002
169003
169004
169005
169006
169007
169008
169009
169010
169011
169012
169013
169014
169015
169016
169017
169018
169019
169020
169021
169022
169023
169024
169025
169026
169027
169028
169029
169030
169031
169032
169033
169034
169035
169036
169037
169038
169039
169040
169041
169042
169043
169044
169045
169046
169047
169048
169049
169050
169051
169052
169053
169054
169055
169056
169057
169058
169059
169060
169061
169062
169063
169064
169065
169066
169067
169068
169069
169070
169071
169072
169073
169074
169075
169076
169077
169078
169079
169080
169081
169082
169083
169084
169085
169086
169087
169088
169089
169090
169091
169092
169093
169094
169095
169096
169097
169098
169099
169100
169101
169102
169103
169104
169105
169106
169107
169108
169109
169110
169111
169112
169113
169114
169115
169116
169117
169118
169119
169120
169121
169122
169123
169124
169125
169126
169127
169128
169129
169130
169131
169132
169133
169134
169135
169136
169137
169138
169139
169140
169141
169142
169143
169144
169145
169146
169147
169148
169149
169150
169151
169152
169153
169154
169155
169156
169157
169158
169159
169160
169161
169162
169163
169164
169165
169166
169167
169168
169169
169170
169171
169172
169173
169174
169175
169176
169177
169178
169179
169180
169181
169182
169183
169184
169185
169186
169187
169188
169189
169190
169191
169192
169193
169194
169195
169196
169197
169198
169199
169200
169201
169202
169203
169204
169205
169206
169207
169208
169209
169210
169211
169212
169213
169214
169215
169216
169217
169218
169219
169220
169221
169222
169223
169224
169225
169226
169227
169228
169229
169230
169231
169232
169233
169234
169235
169236
169237
169238
169239
169240
169241
169242
169243
169244
169245
169246
169247
169248
169249
169250
169251
169252
169253
169254
169255
169256
169257
169258
169259
169260
169261
169262
169263
169264
169265
169266
169267
169268
169269
169270
169271
169272
169273
169274
169275
169276
169277
169278
169279
169280
169281
169282
169283
169284
169285
169286
169287
169288
169289
169290
169291
169292
169293
169294
169295
169296
169297
169298
169299
169300
169301
169302
169303
169304
169305
169306
169307
169308
169309
169310
169311
169312
169313
169314
169315
169316
169317
169318
169319
169320
169321
169322
169323
169324
169325
169326
169327
169328
169329
169330
169331
169332
169333
169334
169335
169336
169337
169338
169339
169340
169341
169342
169343
169344
169345
169346
169347
169348
169349
169350
169351
169352
169353
169354
169355
169356
169357
169358
169359
169360
169361
169362
169363
169364
169365
169366
169367
169368
169369
169370
169371
169372
169373
169374
169375
169376
169377
169378
169379
169380
169381
169382
169383
169384
169385
169386
169387
169388
169389
169390
169391
169392
169393
169394
169395
169396
169397
169398
169399
169400
169401
169402
169403
169404
169405
169406
169407
169408
169409
169410
169411
169412
169413
169414
169415
169416
169417
169418
169419
169420
169421
169422
169423
169424
169425
169426
169427
169428
169429
169430
169431
169432
169433
169434
169435
169436
169437
169438
169439
169440
169441
169442
169443
169444
169445
169446
169447
169448
169449
169450
169451
169452
169453
169454
169455
169456
169457
169458
169459
169460
169461
169462
169463
169464
169465
169466
169467
169468
169469
169470
169471
169472
169473
169474
169475
169476
169477
169478
169479
169480
169481
169482
169483
169484
169485
169486
169487
169488
169489
169490
169491
169492
169493
169494
169495
169496
169497
169498
169499
169500
169501
169502
169503
169504
169505
169506
169507
169508
169509
169510
169511
169512
169513
169514
169515
169516
169517
169518
169519
169520
169521
169522
169523
169524
169525
169526
169527
169528
169529
169530
169531
169532
169533
169534
169535
169536
169537
169538
169539
169540
169541
169542
169543
169544
169545
169546
169547
169548
169549
169550
169551
169552
169553
169554
169555
169556
169557
169558
169559
169560
169561
169562
169563
169564
169565
169566
169567
169568
169569
169570
169571
169572
169573
169574
169575
169576
169577
169578
169579
169580
169581
169582
169583
169584
169585
169586
169587
169588
169589
169590
169591
169592
169593
169594
169595
169596
169597
169598
169599
169600
169601
169602
169603
169604
169605
169606
169607
169608
169609
169610
169611
169612
169613
169614
169615
169616
169617
169618
169619
169620
169621
169622
169623
169624
169625
169626
169627
169628
169629
169630
169631
169632
169633
169634
169635
169636
169637
169638
169639
169640
169641
169642
169643
169644
169645
169646
169647
169648
169649
169650
169651
169652
169653
169654
169655
169656
169657
169658
169659
169660
169661
169662
169663
169664
169665
169666
169667
169668
169669
169670
169671
169672
169673
169674
169675
169676
169677
169678
169679
169680
169681
169682
169683
169684
169685
169686
169687
169688
169689
169690
169691
169692
169693
169694
169695
169696
169697
169698
169699
169700
169701
169702
169703
169704
169705
169706
169707
169708
169709
169710
169711
169712
169713
169714
169715
169716
169717
169718
169719
169720
169721
169722
169723
169724
169725
169726
169727
169728
169729
169730
169731
169732
169733
169734
169735
169736
169737
169738
169739
169740
169741
169742
169743
169744
169745
169746
169747
169748
169749
169750
169751
169752
169753
169754
169755
169756
169757
169758
169759
169760
169761
169762
169763
169764
169765
169766
169767
169768
169769
169770
169771
169772
169773
169774
169775
169776
169777
169778
169779
169780
169781
169782
169783
169784
169785
169786
169787
169788
169789
169790
169791
169792
169793
169794
169795
169796
169797
169798
169799
169800
169801
169802
169803
169804
169805
169806
169807
169808
169809
169810
169811
169812
169813
169814
169815
169816
169817
169818
169819
169820
169821
169822
169823
169824
169825
169826
169827
169828
169829
169830
169831
169832
169833
169834
169835
169836
169837
169838
169839
169840
169841
169842
169843
169844
169845
169846
169847
169848
169849
169850
169851
169852
169853
169854
169855
169856
169857
169858
169859
169860
169861
169862
169863
169864
169865
169866
169867
169868
169869
169870
169871
169872
169873
169874
169875
169876
169877
169878
169879
169880
169881
169882
169883
169884
169885
169886
169887
169888
169889
169890
169891
169892
169893
169894
169895
169896
169897
169898
169899
169900
169901
169902
169903
169904
169905
169906
169907
169908
169909
169910
169911
169912
169913
169914
169915
169916
169917
169918
169919
169920
169921
169922
169923
169924
169925
169926
169927
169928
169929
169930
169931
169932
169933
169934
169935
169936
169937
169938
169939
169940
169941
169942
169943
169944
169945
169946
169947
169948
169949
169950
169951
169952
169953
169954
169955
169956
169957
169958
169959
169960
169961
169962
169963
169964
169965
169966
169967
169968
169969
169970
169971
169972
169973
169974
169975
169976
169977
169978
169979
169980
169981
169982
169983
169984
169985
169986
169987
169988
169989
169990
169991
169992
169993
169994
169995
169996
169997
169998
169999
170000
170001
170002
170003
170004
170005
170006
170007
170008
170009
170010
170011
170012
170013
170014
170015
170016
170017
170018
170019
170020
170021
170022
170023
170024
170025
170026
170027
170028
170029
170030
170031
170032
170033
170034
170035
170036
170037
170038
170039
170040
170041
170042
170043
170044
170045
170046
170047
170048
170049
170050
170051
170052
170053
170054
170055
170056
170057
170058
170059
170060
170061
170062
170063
170064
170065
170066
170067
170068
170069
170070
170071
170072
170073
170074
170075
170076
170077
170078
170079
170080
170081
170082
170083
170084
170085
170086
170087
170088
170089
170090
170091
170092
170093
170094
170095
170096
170097
170098
170099
170100
170101
170102
170103
170104
170105
170106
170107
170108
170109
170110
170111
170112
170113
170114
170115
170116
170117
170118
170119
170120
170121
170122
170123
170124
170125
170126
170127
170128
170129
170130
170131
170132
170133
170134
170135
170136
170137
170138
170139
170140
170141
170142
170143
170144
170145
170146
170147
170148
170149
170150
170151
170152
170153
170154
170155
170156
170157
170158
170159
170160
170161
170162
170163
170164
170165
170166
170167
170168
170169
170170
170171
170172
170173
170174
170175
170176
170177
170178
170179
170180
170181
170182
170183
170184
170185
170186
170187
170188
170189
170190
170191
170192
170193
170194
170195
170196
170197
170198
170199
170200
170201
170202
170203
170204
170205
170206
170207
170208
170209
170210
170211
170212
170213
170214
170215
170216
170217
170218
170219
170220
170221
170222
170223
170224
170225
170226
170227
170228
170229
170230
170231
170232
170233
170234
170235
170236
170237
170238
170239
170240
170241
170242
170243
170244
170245
170246
170247
170248
170249
170250
170251
170252
170253
170254
170255
170256
170257
170258
170259
170260
170261
170262
170263
170264
170265
170266
170267
170268
170269
170270
170271
170272
170273
170274
170275
170276
170277
170278
170279
170280
170281
170282
170283
170284
170285
170286
170287
170288
170289
170290
170291
170292
170293
170294
170295
170296
170297
170298
170299
170300
170301
170302
170303
170304
170305
170306
170307
170308
170309
170310
170311
170312
170313
170314
170315
170316
170317
170318
170319
170320
170321
170322
170323
170324
170325
170326
170327
170328
170329
170330
170331
170332
170333
170334
170335
170336
170337
170338
170339
170340
170341
170342
170343
170344
170345
170346
170347
170348
170349
170350
170351
170352
170353
170354
170355
170356
170357
170358
170359
170360
170361
170362
170363
170364
170365
170366
170367
170368
170369
170370
170371
170372
170373
170374
170375
170376
170377
170378
170379
170380
170381
170382
170383
170384
170385
170386
170387
170388
170389
170390
170391
170392
170393
170394
170395
170396
170397
170398
170399
170400
170401
170402
170403
170404
170405
170406
170407
170408
170409
170410
170411
170412
170413
170414
170415
170416
170417
170418
170419
170420
170421
170422
170423
170424
170425
170426
170427
170428
170429
170430
170431
170432
170433
170434
170435
170436
170437
170438
170439
170440
170441
170442
170443
170444
170445
170446
170447
170448
170449
170450
170451
170452
170453
170454
170455
170456
170457
170458
170459
170460
170461
170462
170463
170464
170465
170466
170467
170468
170469
170470
170471
170472
170473
170474
170475
170476
170477
170478
170479
170480
170481
170482
170483
170484
170485
170486
170487
170488
170489
170490
170491
170492
170493
170494
170495
170496
170497
170498
170499
170500
170501
170502
170503
170504
170505
170506
170507
170508
170509
170510
170511
170512
170513
170514
170515
170516
170517
170518
170519
170520
170521
170522
170523
170524
170525
170526
170527
170528
170529
170530
170531
170532
170533
170534
170535
170536
170537
170538
170539
170540
170541
170542
170543
170544
170545
170546
170547
170548
170549
170550
170551
170552
170553
170554
170555
170556
170557
170558
170559
170560
170561
170562
170563
170564
170565
170566
170567
170568
170569
170570
170571
170572
170573
170574
170575
170576
170577
170578
170579
170580
170581
170582
170583
170584
170585
170586
170587
170588
170589
170590
170591
170592
170593
170594
170595
170596
170597
170598
170599
170600
170601
170602
170603
170604
170605
170606
170607
170608
170609
170610
170611
170612
170613
170614
170615
170616
170617
170618
170619
170620
170621
170622
170623
170624
170625
170626
170627
170628
170629
170630
170631
170632
170633
170634
170635
170636
170637
170638
170639
170640
170641
170642
170643
170644
170645
170646
170647
170648
170649
170650
170651
170652
170653
170654
170655
170656
170657
170658
170659
170660
170661
170662
170663
170664
170665
170666
170667
170668
170669
170670
170671
170672
170673
170674
170675
170676
170677
170678
170679
170680
170681
170682
170683
170684
170685
170686
170687
170688
170689
170690
170691
170692
170693
170694
170695
170696
170697
170698
170699
170700
170701
170702
170703
170704
170705
170706
170707
170708
170709
170710
170711
170712
170713
170714
170715
170716
170717
170718
170719
170720
170721
170722
170723
170724
170725
170726
170727
170728
170729
170730
170731
170732
170733
170734
170735
170736
170737
170738
170739
170740
170741
170742
170743
170744
170745
170746
170747
170748
170749
170750
170751
170752
170753
170754
170755
170756
170757
170758
170759
170760
170761
170762
170763
170764
170765
170766
170767
170768
170769
170770
170771
170772
170773
170774
170775
170776
170777
170778
170779
170780
170781
170782
170783
170784
170785
170786
170787
170788
170789
170790
170791
170792
170793
170794
170795
170796
170797
170798
170799
170800
170801
170802
170803
170804
170805
170806
170807
170808
170809
170810
170811
170812
170813
170814
170815
170816
170817
170818
170819
170820
170821
170822
170823
170824
170825
170826
170827
170828
170829
170830
170831
170832
170833
170834
170835
170836
170837
170838
170839
170840
170841
170842
170843
170844
170845
170846
170847
170848
170849
170850
170851
170852
170853
170854
170855
170856
170857
170858
170859
170860
170861
170862
170863
170864
170865
170866
170867
170868
170869
170870
170871
170872
170873
170874
170875
170876
170877
170878
170879
170880
170881
170882
170883
170884
170885
170886
170887
170888
170889
170890
170891
170892
170893
170894
170895
170896
170897
170898
170899
170900
170901
170902
170903
170904
170905
170906
170907
170908
170909
170910
170911
170912
170913
170914
170915
170916
170917
170918
170919
170920
170921
170922
170923
170924
170925
170926
170927
170928
170929
170930
170931
170932
170933
170934
170935
170936
170937
170938
170939
170940
170941
170942
170943
170944
170945
170946
170947
170948
170949
170950
170951
170952
170953
170954
170955
170956
170957
170958
170959
170960
170961
170962
170963
170964
170965
170966
170967
170968
170969
170970
170971
170972
170973
170974
170975
170976
170977
170978
170979
170980
170981
170982
170983
170984
170985
170986
170987
170988
170989
170990
170991
170992
170993
170994
170995
170996
170997
170998
170999
171000
171001
171002
171003
171004
171005
171006
171007
171008
171009
171010
171011
171012
171013
171014
171015
171016
171017
171018
171019
171020
171021
171022
171023
171024
171025
171026
171027
171028
171029
171030
171031
171032
171033
171034
171035
171036
171037
171038
171039
171040
171041
171042
171043
171044
171045
171046
171047
171048
171049
171050
171051
171052
171053
171054
171055
171056
171057
171058
171059
171060
171061
171062
171063
171064
171065
171066
171067
171068
171069
171070
171071
171072
171073
171074
171075
171076
171077
171078
171079
171080
171081
171082
171083
171084
171085
171086
171087
171088
171089
171090
171091
171092
171093
171094
171095
171096
171097
171098
171099
171100
171101
171102
171103
171104
171105
171106
171107
171108
171109
171110
171111
171112
171113
171114
171115
171116
171117
171118
171119
171120
171121
171122
171123
171124
171125
171126
171127
171128
171129
171130
171131
171132
171133
171134
171135
171136
171137
171138
171139
171140
171141
171142
171143
171144
171145
171146
171147
171148
171149
171150
171151
171152
171153
171154
171155
171156
171157
171158
171159
171160
171161
171162
171163
171164
171165
171166
171167
171168
171169
171170
171171
171172
171173
171174
171175
171176
171177
171178
171179
171180
171181
171182
171183
171184
171185
171186
171187
171188
171189
171190
171191
171192
171193
171194
171195
171196
171197
171198
171199
171200
171201
171202
171203
171204
171205
171206
171207
171208
171209
171210
171211
171212
171213
171214
171215
171216
171217
171218
171219
171220
171221
171222
171223
171224
171225
171226
171227
171228
171229
171230
171231
171232
171233
171234
171235
171236
171237
171238
171239
171240
171241
171242
171243
171244
171245
171246
171247
171248
171249
171250
171251
171252
171253
171254
171255
171256
171257
171258
171259
171260
171261
171262
171263
171264
171265
171266
171267
171268
171269
171270
171271
171272
171273
171274
171275
171276
171277
171278
171279
171280
171281
171282
171283
171284
171285
171286
171287
171288
171289
171290
171291
171292
171293
171294
171295
171296
171297
171298
171299
171300
171301
171302
171303
171304
171305
171306
171307
171308
171309
171310
171311
171312
171313
171314
171315
171316
171317
171318
171319
171320
171321
171322
171323
171324
171325
171326
171327
171328
171329
171330
171331
171332
171333
171334
171335
171336
171337
171338
171339
171340
171341
171342
171343
171344
171345
171346
171347
171348
171349
171350
171351
171352
171353
171354
171355
171356
171357
171358
171359
171360
171361
171362
171363
171364
171365
171366
171367
171368
171369
171370
171371
171372
171373
171374
171375
171376
171377
171378
171379
171380
171381
171382
171383
171384
171385
171386
171387
171388
171389
171390
171391
171392
171393
171394
171395
171396
171397
171398
171399
171400
171401
171402
171403
171404
171405
171406
171407
171408
171409
171410
171411
171412
171413
171414
171415
171416
171417
171418
171419
171420
171421
171422
171423
171424
171425
171426
171427
171428
171429
171430
171431
171432
171433
171434
171435
171436
171437
171438
171439
171440
171441
171442
171443
171444
171445
171446
171447
171448
171449
171450
171451
171452
171453
171454
171455
171456
171457
171458
171459
171460
171461
171462
171463
171464
171465
171466
171467
171468
171469
171470
171471
171472
171473
171474
171475
171476
171477
171478
171479
171480
171481
171482
171483
171484
171485
171486
171487
171488
171489
171490
171491
171492
171493
171494
171495
171496
171497
171498
171499
171500
171501
171502
171503
171504
171505
171506
171507
171508
171509
171510
171511
171512
171513
171514
171515
171516
171517
171518
171519
171520
171521
171522
171523
171524
171525
171526
171527
171528
171529
171530
171531
171532
171533
171534
171535
171536
171537
171538
171539
171540
171541
171542
171543
171544
171545
171546
171547
171548
171549
171550
171551
171552
171553
171554
171555
171556
171557
171558
171559
171560
171561
171562
171563
171564
171565
171566
171567
171568
171569
171570
171571
171572
171573
171574
171575
171576
171577
171578
171579
171580
171581
171582
171583
171584
171585
171586
171587
171588
171589
171590
171591
171592
171593
171594
171595
171596
171597
171598
171599
171600
171601
171602
171603
171604
171605
171606
171607
171608
171609
171610
171611
171612
171613
171614
171615
171616
171617
171618
171619
171620
171621
171622
171623
171624
171625
171626
171627
171628
171629
171630
171631
171632
171633
171634
171635
171636
171637
171638
171639
171640
171641
171642
171643
171644
171645
171646
171647
171648
171649
171650
171651
171652
171653
171654
171655
171656
171657
171658
171659
171660
171661
171662
171663
171664
171665
171666
171667
171668
171669
171670
171671
171672
171673
171674
171675
171676
171677
171678
171679
171680
171681
171682
171683
171684
171685
171686
171687
171688
171689
171690
171691
171692
171693
171694
171695
171696
171697
171698
171699
171700
171701
171702
171703
171704
171705
171706
171707
171708
171709
171710
171711
171712
171713
171714
171715
171716
171717
171718
171719
171720
171721
171722
171723
171724
171725
171726
171727
171728
171729
171730
171731
171732
171733
171734
171735
171736
171737
171738
171739
171740
171741
171742
171743
171744
171745
171746
171747
171748
171749
171750
171751
171752
171753
171754
171755
171756
171757
171758
171759
171760
171761
171762
171763
171764
171765
171766
171767
171768
171769
171770
171771
171772
171773
171774
171775
171776
171777
171778
171779
171780
171781
171782
171783
171784
171785
171786
171787
171788
171789
171790
171791
171792
171793
171794
171795
171796
171797
171798
171799
171800
171801
171802
171803
171804
171805
171806
171807
171808
171809
171810
171811
171812
171813
171814
171815
171816
171817
171818
171819
171820
171821
171822
171823
171824
171825
171826
171827
171828
171829
171830
171831
171832
171833
171834
171835
171836
171837
171838
171839
171840
171841
171842
171843
171844
171845
171846
171847
171848
171849
171850
171851
171852
171853
171854
171855
171856
171857
171858
171859
171860
171861
171862
171863
171864
171865
171866
171867
171868
171869
171870
171871
171872
171873
171874
171875
171876
171877
171878
171879
171880
171881
171882
171883
171884
171885
171886
171887
171888
171889
171890
171891
171892
171893
171894
171895
171896
171897
171898
171899
171900
171901
171902
171903
171904
171905
171906
171907
171908
171909
171910
171911
171912
171913
171914
171915
171916
171917
171918
171919
171920
171921
171922
171923
171924
171925
171926
171927
171928
171929
171930
171931
171932
171933
171934
171935
171936
171937
171938
171939
171940
171941
171942
171943
171944
171945
171946
171947
171948
171949
171950
171951
171952
171953
171954
171955
171956
171957
171958
171959
171960
171961
171962
171963
171964
171965
171966
171967
171968
171969
171970
171971
171972
171973
171974
171975
171976
171977
171978
171979
171980
171981
171982
171983
171984
171985
171986
171987
171988
171989
171990
171991
171992
171993
171994
171995
171996
171997
171998
171999
172000
172001
172002
172003
172004
172005
172006
172007
172008
172009
172010
172011
172012
172013
172014
172015
172016
172017
172018
172019
172020
172021
172022
172023
172024
172025
172026
172027
172028
172029
172030
172031
172032
172033
172034
172035
172036
172037
172038
172039
172040
172041
172042
172043
172044
172045
172046
172047
172048
172049
172050
172051
172052
172053
172054
172055
172056
172057
172058
172059
172060
172061
172062
172063
172064
172065
172066
172067
172068
172069
172070
172071
172072
172073
172074
172075
172076
172077
172078
172079
172080
172081
172082
172083
172084
172085
172086
172087
172088
172089
172090
172091
172092
172093
172094
172095
172096
172097
172098
172099
172100
172101
172102
172103
172104
172105
172106
172107
172108
172109
172110
172111
172112
172113
172114
172115
172116
172117
172118
172119
172120
172121
172122
172123
172124
172125
172126
172127
172128
172129
172130
172131
172132
172133
172134
172135
172136
172137
172138
172139
172140
172141
172142
172143
172144
172145
172146
172147
172148
172149
172150
172151
172152
172153
172154
172155
172156
172157
172158
172159
172160
172161
172162
172163
172164
172165
172166
172167
172168
172169
172170
172171
172172
172173
172174
172175
172176
172177
172178
172179
172180
172181
172182
172183
172184
172185
172186
172187
172188
172189
172190
172191
172192
172193
172194
172195
172196
172197
172198
172199
172200
172201
172202
172203
172204
172205
172206
172207
172208
172209
172210
172211
172212
172213
172214
172215
172216
172217
172218
172219
172220
172221
172222
172223
172224
172225
172226
172227
172228
172229
172230
172231
172232
172233
172234
172235
172236
172237
172238
172239
172240
172241
172242
172243
172244
172245
172246
172247
172248
172249
172250
172251
172252
172253
172254
172255
172256
172257
172258
172259
172260
172261
172262
172263
172264
172265
172266
172267
172268
172269
172270
172271
172272
172273
172274
172275
172276
172277
172278
172279
172280
172281
172282
172283
172284
172285
172286
172287
172288
172289
172290
172291
172292
172293
172294
172295
172296
172297
172298
172299
172300
172301
172302
172303
172304
172305
172306
172307
172308
172309
172310
172311
172312
172313
172314
172315
172316
172317
172318
172319
172320
172321
172322
172323
172324
172325
172326
172327
172328
172329
172330
172331
172332
172333
172334
172335
172336
172337
172338
172339
172340
172341
172342
172343
172344
172345
172346
172347
172348
172349
172350
172351
172352
172353
172354
172355
172356
172357
172358
172359
172360
172361
172362
172363
172364
172365
172366
172367
172368
172369
172370
172371
172372
172373
172374
172375
172376
172377
172378
172379
172380
172381
172382
172383
172384
172385
172386
172387
172388
172389
172390
172391
172392
172393
172394
172395
172396
172397
172398
172399
172400
172401
172402
172403
172404
172405
172406
172407
172408
172409
172410
172411
172412
172413
172414
172415
172416
172417
172418
172419
172420
172421
172422
172423
172424
172425
172426
172427
172428
172429
172430
172431
172432
172433
172434
172435
172436
172437
172438
172439
172440
172441
172442
172443
172444
172445
172446
172447
172448
172449
172450
172451
172452
172453
172454
172455
172456
172457
172458
172459
172460
172461
172462
172463
172464
172465
172466
172467
172468
172469
172470
172471
172472
172473
172474
172475
172476
172477
172478
172479
172480
172481
172482
172483
172484
172485
172486
172487
172488
172489
172490
172491
172492
172493
172494
172495
172496
172497
172498
172499
172500
172501
172502
172503
172504
172505
172506
172507
172508
172509
172510
172511
172512
172513
172514
172515
172516
172517
172518
172519
172520
172521
172522
172523
172524
172525
172526
172527
172528
172529
172530
172531
172532
172533
172534
172535
172536
172537
172538
172539
172540
172541
172542
172543
172544
172545
172546
172547
172548
172549
172550
172551
172552
172553
172554
172555
172556
172557
172558
172559
172560
172561
172562
172563
172564
172565
172566
172567
172568
172569
172570
172571
172572
172573
172574
172575
172576
172577
172578
172579
172580
172581
172582
172583
172584
172585
172586
172587
172588
172589
172590
172591
172592
172593
172594
172595
172596
172597
172598
172599
172600
172601
172602
172603
172604
172605
172606
172607
172608
172609
172610
172611
172612
172613
172614
172615
172616
172617
172618
172619
172620
172621
172622
172623
172624
172625
172626
172627
172628
172629
172630
172631
172632
172633
172634
172635
172636
172637
172638
172639
172640
172641
172642
172643
172644
172645
172646
172647
172648
172649
172650
172651
172652
172653
172654
172655
172656
172657
172658
172659
172660
172661
172662
172663
172664
172665
172666
172667
172668
172669
172670
172671
172672
172673
172674
172675
172676
172677
172678
172679
172680
172681
172682
172683
172684
172685
172686
172687
172688
172689
172690
172691
172692
172693
172694
172695
172696
172697
172698
172699
172700
172701
172702
172703
172704
172705
172706
172707
172708
172709
172710
172711
172712
172713
172714
172715
172716
172717
172718
172719
172720
172721
172722
172723
172724
172725
172726
172727
172728
172729
172730
172731
172732
172733
172734
172735
172736
172737
172738
172739
172740
172741
172742
172743
172744
172745
172746
172747
172748
172749
172750
172751
172752
172753
172754
172755
172756
172757
172758
172759
172760
172761
172762
172763
172764
172765
172766
172767
172768
172769
172770
172771
172772
172773
172774
172775
172776
172777
172778
172779
172780
172781
172782
172783
172784
172785
172786
172787
172788
172789
172790
172791
172792
172793
172794
172795
172796
172797
172798
172799
172800
172801
172802
172803
172804
172805
172806
172807
172808
172809
172810
172811
172812
172813
172814
172815
172816
172817
172818
172819
172820
172821
172822
172823
172824
172825
172826
172827
172828
172829
172830
172831
172832
172833
172834
172835
172836
172837
172838
172839
172840
172841
172842
172843
172844
172845
172846
172847
172848
172849
172850
172851
172852
172853
172854
172855
172856
172857
172858
172859
172860
172861
172862
172863
172864
172865
172866
172867
172868
172869
172870
172871
172872
172873
172874
172875
172876
172877
172878
172879
172880
172881
172882
172883
172884
172885
172886
172887
172888
172889
172890
172891
172892
172893
172894
172895
172896
172897
172898
172899
172900
172901
172902
172903
172904
172905
172906
172907
172908
172909
172910
172911
172912
172913
172914
172915
172916
172917
172918
172919
172920
172921
172922
172923
172924
172925
172926
172927
172928
172929
172930
172931
172932
172933
172934
172935
172936
172937
172938
172939
172940
172941
172942
172943
172944
172945
172946
172947
172948
172949
172950
172951
172952
172953
172954
172955
172956
172957
172958
172959
172960
172961
172962
172963
172964
172965
172966
172967
172968
172969
172970
172971
172972
172973
172974
172975
172976
172977
172978
172979
172980
172981
172982
172983
172984
172985
172986
172987
172988
172989
172990
172991
172992
172993
172994
172995
172996
172997
172998
172999
173000
173001
173002
173003
173004
173005
173006
173007
173008
173009
173010
173011
173012
173013
173014
173015
173016
173017
173018
173019
173020
173021
173022
173023
173024
173025
173026
173027
173028
173029
173030
173031
173032
173033
173034
173035
173036
173037
173038
173039
173040
173041
173042
173043
173044
173045
173046
173047
173048
173049
173050
173051
173052
173053
173054
173055
173056
173057
173058
173059
173060
173061
173062
173063
173064
173065
173066
173067
173068
173069
173070
173071
173072
173073
173074
173075
173076
173077
173078
173079
173080
173081
173082
173083
173084
173085
173086
173087
173088
173089
173090
173091
173092
173093
173094
173095
173096
173097
173098
173099
173100
173101
173102
173103
173104
173105
173106
173107
173108
173109
173110
173111
173112
173113
173114
173115
173116
173117
173118
173119
173120
173121
173122
173123
173124
173125
173126
173127
173128
173129
173130
173131
173132
173133
173134
173135
173136
173137
173138
173139
173140
173141
173142
173143
173144
173145
173146
173147
173148
173149
173150
173151
173152
173153
173154
173155
173156
173157
173158
173159
173160
173161
173162
173163
173164
173165
173166
173167
173168
173169
173170
173171
173172
173173
173174
173175
173176
173177
173178
173179
173180
173181
173182
173183
173184
173185
173186
173187
173188
173189
173190
173191
173192
173193
173194
173195
173196
173197
173198
173199
173200
173201
173202
173203
173204
173205
173206
173207
173208
173209
173210
173211
173212
173213
173214
173215
173216
173217
173218
173219
173220
173221
173222
173223
173224
173225
173226
173227
173228
173229
173230
173231
173232
173233
173234
173235
173236
173237
173238
173239
173240
173241
173242
173243
173244
173245
173246
173247
173248
173249
173250
173251
173252
173253
173254
173255
173256
173257
173258
173259
173260
173261
173262
173263
173264
173265
173266
173267
173268
173269
173270
173271
173272
173273
173274
173275
173276
173277
173278
173279
173280
173281
173282
173283
173284
173285
173286
173287
173288
173289
173290
173291
173292
173293
173294
173295
173296
173297
173298
173299
173300
173301
173302
173303
173304
173305
173306
173307
173308
173309
173310
173311
173312
173313
173314
173315
173316
173317
173318
173319
173320
173321
173322
173323
173324
173325
173326
173327
173328
173329
173330
173331
173332
173333
173334
173335
173336
173337
173338
173339
173340
173341
173342
173343
173344
173345
173346
173347
173348
173349
173350
173351
173352
173353
173354
173355
173356
173357
173358
173359
173360
173361
173362
173363
173364
173365
173366
173367
173368
173369
173370
173371
173372
173373
173374
173375
173376
173377
173378
173379
173380
173381
173382
173383
173384
173385
173386
173387
173388
173389
173390
173391
173392
173393
173394
173395
173396
173397
173398
173399
173400
173401
173402
173403
173404
173405
173406
173407
173408
173409
173410
173411
173412
173413
173414
173415
173416
173417
173418
173419
173420
173421
173422
173423
173424
173425
173426
173427
173428
173429
173430
173431
173432
173433
173434
173435
173436
173437
173438
173439
173440
173441
173442
173443
173444
173445
173446
173447
173448
173449
173450
173451
173452
173453
173454
173455
173456
173457
173458
173459
173460
173461
173462
173463
173464
173465
173466
173467
173468
173469
173470
173471
173472
173473
173474
173475
173476
173477
173478
173479
173480
173481
173482
173483
173484
173485
173486
173487
173488
173489
173490
173491
173492
173493
173494
173495
173496
173497
173498
173499
173500
173501
173502
173503
173504
173505
173506
173507
173508
173509
173510
173511
173512
173513
173514
173515
173516
173517
173518
173519
173520
173521
173522
173523
173524
173525
173526
173527
173528
173529
173530
173531
173532
173533
173534
173535
173536
173537
173538
173539
173540
173541
173542
173543
173544
173545
173546
173547
173548
173549
173550
173551
173552
173553
173554
173555
173556
173557
173558
173559
173560
173561
173562
173563
173564
173565
173566
173567
173568
173569
173570
173571
173572
173573
173574
173575
173576
173577
173578
173579
173580
173581
173582
173583
173584
173585
173586
173587
173588
173589
173590
173591
173592
173593
173594
173595
173596
173597
173598
173599
173600
173601
173602
173603
173604
173605
173606
173607
173608
173609
173610
173611
173612
173613
173614
173615
173616
173617
173618
173619
173620
173621
173622
173623
173624
173625
173626
173627
173628
173629
173630
173631
173632
173633
173634
173635
173636
173637
173638
173639
173640
173641
173642
173643
173644
173645
173646
173647
173648
173649
173650
173651
173652
173653
173654
173655
173656
173657
173658
173659
173660
173661
173662
173663
173664
173665
173666
173667
173668
173669
173670
173671
173672
173673
173674
173675
173676
173677
173678
173679
173680
173681
173682
173683
173684
173685
173686
173687
173688
173689
173690
173691
173692
173693
173694
173695
173696
173697
173698
173699
173700
173701
173702
173703
173704
173705
173706
173707
173708
173709
173710
173711
173712
173713
173714
173715
173716
173717
173718
173719
173720
173721
173722
173723
173724
173725
173726
173727
173728
173729
173730
173731
173732
173733
173734
173735
173736
173737
173738
173739
173740
173741
173742
173743
173744
173745
173746
173747
173748
173749
173750
173751
173752
173753
173754
173755
173756
173757
173758
173759
173760
173761
173762
173763
173764
173765
173766
173767
173768
173769
173770
173771
173772
173773
173774
173775
173776
173777
173778
173779
173780
173781
173782
173783
173784
173785
173786
173787
173788
173789
173790
173791
173792
173793
173794
173795
173796
173797
173798
173799
173800
173801
173802
173803
173804
173805
173806
173807
173808
173809
173810
173811
173812
173813
173814
173815
173816
173817
173818
173819
173820
173821
173822
173823
173824
173825
173826
173827
173828
173829
173830
173831
173832
173833
173834
173835
173836
173837
173838
173839
173840
173841
173842
173843
173844
173845
173846
173847
173848
173849
173850
173851
173852
173853
173854
173855
173856
173857
173858
173859
173860
173861
173862
173863
173864
173865
173866
173867
173868
173869
173870
173871
173872
173873
173874
173875
173876
173877
173878
173879
173880
173881
173882
173883
173884
173885
173886
173887
173888
173889
173890
173891
173892
173893
173894
173895
173896
173897
173898
173899
173900
173901
173902
173903
173904
173905
173906
173907
173908
173909
173910
173911
173912
173913
173914
173915
173916
173917
173918
173919
173920
173921
173922
173923
173924
173925
173926
173927
173928
173929
173930
173931
173932
173933
173934
173935
173936
173937
173938
173939
173940
173941
173942
173943
173944
173945
173946
173947
173948
173949
173950
173951
173952
173953
173954
173955
173956
173957
173958
173959
173960
173961
173962
173963
173964
173965
173966
173967
173968
173969
173970
173971
173972
173973
173974
173975
173976
173977
173978
173979
173980
173981
173982
173983
173984
173985
173986
173987
173988
173989
173990
173991
173992
173993
173994
173995
173996
173997
173998
173999
174000
174001
174002
174003
174004
174005
174006
174007
174008
174009
174010
174011
174012
174013
174014
174015
174016
174017
174018
174019
174020
174021
174022
174023
174024
174025
174026
174027
174028
174029
174030
174031
174032
174033
174034
174035
174036
174037
174038
174039
174040
174041
174042
174043
174044
174045
174046
174047
174048
174049
174050
174051
174052
174053
174054
174055
174056
174057
174058
174059
174060
174061
174062
174063
174064
174065
174066
174067
174068
174069
174070
174071
174072
174073
174074
174075
174076
174077
174078
174079
174080
174081
174082
174083
174084
174085
174086
174087
174088
174089
174090
174091
174092
174093
174094
174095
174096
174097
174098
174099
174100
174101
174102
174103
174104
174105
174106
174107
174108
174109
174110
174111
174112
174113
174114
174115
174116
174117
174118
174119
174120
174121
174122
174123
174124
174125
174126
174127
174128
174129
174130
174131
174132
174133
174134
174135
174136
174137
174138
174139
174140
174141
174142
174143
174144
174145
174146
174147
174148
174149
174150
174151
174152
174153
174154
174155
174156
174157
174158
174159
174160
174161
174162
174163
174164
174165
174166
174167
174168
174169
174170
174171
174172
174173
174174
174175
174176
174177
174178
174179
174180
174181
174182
174183
174184
174185
174186
174187
174188
174189
174190
174191
174192
174193
174194
174195
174196
174197
174198
174199
174200
174201
174202
174203
174204
174205
174206
174207
174208
174209
174210
174211
174212
174213
174214
174215
174216
174217
174218
174219
174220
174221
174222
174223
174224
174225
174226
174227
174228
174229
174230
174231
174232
174233
174234
174235
174236
174237
174238
174239
174240
174241
174242
174243
174244
174245
174246
174247
174248
174249
174250
174251
174252
174253
174254
174255
174256
174257
174258
174259
174260
174261
174262
174263
174264
174265
174266
174267
174268
174269
174270
174271
174272
174273
174274
174275
174276
174277
174278
174279
174280
174281
174282
174283
174284
174285
174286
174287
174288
174289
174290
174291
174292
174293
174294
174295
174296
174297
174298
174299
174300
174301
174302
174303
174304
174305
174306
174307
174308
174309
174310
174311
174312
174313
174314
174315
174316
174317
174318
174319
174320
174321
174322
174323
174324
174325
174326
174327
174328
174329
174330
174331
174332
174333
174334
174335
174336
174337
174338
174339
174340
174341
174342
174343
174344
174345
174346
174347
174348
174349
174350
174351
174352
174353
174354
174355
174356
174357
174358
174359
174360
174361
174362
174363
174364
174365
174366
174367
174368
174369
174370
174371
174372
174373
174374
174375
174376
174377
174378
174379
174380
174381
174382
174383
174384
174385
174386
174387
174388
174389
174390
174391
174392
174393
174394
174395
174396
174397
174398
174399
174400
174401
174402
174403
174404
174405
174406
174407
174408
174409
174410
174411
174412
174413
174414
174415
174416
174417
174418
174419
174420
174421
174422
174423
174424
174425
174426
174427
174428
174429
174430
174431
174432
174433
174434
174435
174436
174437
174438
174439
174440
174441
174442
174443
174444
174445
174446
174447
174448
174449
174450
174451
174452
174453
174454
174455
174456
174457
174458
174459
174460
174461
174462
174463
174464
174465
174466
174467
174468
174469
174470
174471
174472
174473
174474
174475
174476
174477
174478
174479
174480
174481
174482
174483
174484
174485
174486
174487
174488
174489
174490
174491
174492
174493
174494
174495
174496
174497
174498
174499
174500
174501
174502
174503
174504
174505
174506
174507
174508
174509
174510
174511
174512
174513
174514
174515
174516
174517
174518
174519
174520
174521
174522
174523
174524
174525
174526
174527
174528
174529
174530
174531
174532
174533
174534
174535
174536
174537
174538
174539
174540
174541
174542
174543
174544
174545
174546
174547
174548
174549
174550
174551
174552
174553
174554
174555
174556
174557
174558
174559
174560
174561
174562
174563
174564
174565
174566
174567
174568
174569
174570
174571
174572
174573
174574
174575
174576
174577
174578
174579
174580
174581
174582
174583
174584
174585
174586
174587
174588
174589
174590
174591
174592
174593
174594
174595
174596
174597
174598
174599
174600
174601
174602
174603
174604
174605
174606
174607
174608
174609
174610
174611
174612
174613
174614
174615
174616
174617
174618
174619
174620
174621
174622
174623
174624
174625
174626
174627
174628
174629
174630
174631
174632
174633
174634
174635
174636
174637
174638
174639
174640
174641
174642
174643
174644
174645
174646
174647
174648
174649
174650
174651
174652
174653
174654
174655
174656
174657
174658
174659
174660
174661
174662
174663
174664
174665
174666
174667
174668
174669
174670
174671
174672
174673
174674
174675
174676
174677
174678
174679
174680
174681
174682
174683
174684
174685
174686
174687
174688
174689
174690
174691
174692
174693
174694
174695
174696
174697
174698
174699
174700
174701
174702
174703
174704
174705
174706
174707
174708
174709
174710
174711
174712
174713
174714
174715
174716
174717
174718
174719
174720
174721
174722
174723
174724
174725
174726
174727
174728
174729
174730
174731
174732
174733
174734
174735
174736
174737
174738
174739
174740
174741
174742
174743
174744
174745
174746
174747
174748
174749
174750
174751
174752
174753
174754
174755
174756
174757
174758
174759
174760
174761
174762
174763
174764
174765
174766
174767
174768
174769
174770
174771
174772
174773
174774
174775
174776
174777
174778
174779
174780
174781
174782
174783
174784
174785
174786
174787
174788
174789
174790
174791
174792
174793
174794
174795
174796
174797
174798
174799
174800
174801
174802
174803
174804
174805
174806
174807
174808
174809
174810
174811
174812
174813
174814
174815
174816
174817
174818
174819
174820
174821
174822
174823
174824
174825
174826
174827
174828
174829
174830
174831
174832
174833
174834
174835
174836
174837
174838
174839
174840
174841
174842
174843
174844
174845
174846
174847
174848
174849
174850
174851
174852
174853
174854
174855
174856
174857
174858
174859
174860
174861
174862
174863
174864
174865
174866
174867
174868
174869
174870
174871
174872
174873
174874
174875
174876
174877
174878
174879
174880
174881
174882
174883
174884
174885
174886
174887
174888
174889
174890
174891
174892
174893
174894
174895
174896
174897
174898
174899
174900
174901
174902
174903
174904
174905
174906
174907
174908
174909
174910
174911
174912
174913
174914
174915
174916
174917
174918
174919
174920
174921
174922
174923
174924
174925
174926
174927
174928
174929
174930
174931
174932
174933
174934
174935
174936
174937
174938
174939
174940
174941
174942
174943
174944
174945
174946
174947
174948
174949
174950
174951
174952
174953
174954
174955
174956
174957
174958
174959
174960
174961
174962
174963
174964
174965
174966
174967
174968
174969
174970
174971
174972
174973
174974
174975
174976
174977
174978
174979
174980
174981
174982
174983
174984
174985
174986
174987
174988
174989
174990
174991
174992
174993
174994
174995
174996
174997
174998
174999
175000
175001
175002
175003
175004
175005
175006
175007
175008
175009
175010
175011
175012
175013
175014
175015
175016
175017
175018
175019
175020
175021
175022
175023
175024
175025
175026
175027
175028
175029
175030
175031
175032
175033
175034
175035
175036
175037
175038
175039
175040
175041
175042
175043
175044
175045
175046
175047
175048
175049
175050
175051
175052
175053
175054
175055
175056
175057
175058
175059
175060
175061
175062
175063
175064
175065
175066
175067
175068
175069
175070
175071
175072
175073
175074
175075
175076
175077
175078
175079
175080
175081
175082
175083
175084
175085
175086
175087
175088
175089
175090
175091
175092
175093
175094
175095
175096
175097
175098
175099
175100
175101
175102
175103
175104
175105
175106
175107
175108
175109
175110
175111
175112
175113
175114
175115
175116
175117
175118
175119
175120
175121
175122
175123
175124
175125
175126
175127
175128
175129
175130
175131
175132
175133
175134
175135
175136
175137
175138
175139
175140
175141
175142
175143
175144
175145
175146
175147
175148
175149
175150
175151
175152
175153
175154
175155
175156
175157
175158
175159
175160
175161
175162
175163
175164
175165
175166
175167
175168
175169
175170
175171
175172
175173
175174
175175
175176
175177
175178
175179
175180
175181
175182
175183
175184
175185
175186
175187
175188
175189
175190
175191
175192
175193
175194
175195
175196
175197
175198
175199
175200
175201
175202
175203
175204
175205
175206
175207
175208
175209
175210
175211
175212
175213
175214
175215
175216
175217
175218
175219
175220
175221
175222
175223
175224
175225
175226
175227
175228
175229
175230
175231
175232
175233
175234
175235
175236
175237
175238
175239
175240
175241
175242
175243
175244
175245
175246
175247
175248
175249
175250
175251
175252
175253
175254
175255
175256
175257
175258
175259
175260
175261
175262
175263
175264
175265
175266
175267
175268
175269
175270
175271
175272
175273
175274
175275
175276
175277
175278
175279
175280
175281
175282
175283
175284
175285
175286
175287
175288
175289
175290
175291
175292
175293
175294
175295
175296
175297
175298
175299
175300
175301
175302
175303
175304
175305
175306
175307
175308
175309
175310
175311
175312
175313
175314
175315
175316
175317
175318
175319
175320
175321
175322
175323
175324
175325
175326
175327
175328
175329
175330
175331
175332
175333
175334
175335
175336
175337
175338
175339
175340
175341
175342
175343
175344
175345
175346
175347
175348
175349
175350
175351
175352
175353
175354
175355
175356
175357
175358
175359
175360
175361
175362
175363
175364
175365
175366
175367
175368
175369
175370
175371
175372
175373
175374
175375
175376
175377
175378
175379
175380
175381
175382
175383
175384
175385
175386
175387
175388
175389
175390
175391
175392
175393
175394
175395
175396
175397
175398
175399
175400
175401
175402
175403
175404
175405
175406
175407
175408
175409
175410
175411
175412
175413
175414
175415
175416
175417
175418
175419
175420
175421
175422
175423
175424
175425
175426
175427
175428
175429
175430
175431
175432
175433
175434
175435
175436
175437
175438
175439
175440
175441
175442
175443
175444
175445
175446
175447
175448
175449
175450
175451
175452
175453
175454
175455
175456
175457
175458
175459
175460
175461
175462
175463
175464
175465
175466
175467
175468
175469
175470
175471
175472
175473
175474
175475
175476
175477
175478
175479
175480
175481
175482
175483
175484
175485
175486
175487
175488
175489
175490
175491
175492
175493
175494
175495
175496
175497
175498
175499
175500
175501
175502
175503
175504
175505
175506
175507
175508
175509
175510
175511
175512
175513
175514
175515
175516
175517
175518
175519
175520
175521
175522
175523
175524
175525
175526
175527
175528
175529
175530
175531
175532
175533
175534
175535
175536
175537
175538
175539
175540
175541
175542
175543
175544
175545
175546
175547
175548
175549
175550
175551
175552
175553
175554
175555
175556
175557
175558
175559
175560
175561
175562
175563
175564
175565
175566
175567
175568
175569
175570
175571
175572
175573
175574
175575
175576
175577
175578
175579
175580
175581
175582
175583
175584
175585
175586
175587
175588
175589
175590
175591
175592
175593
175594
175595
175596
175597
175598
175599
175600
175601
175602
175603
175604
175605
175606
175607
175608
175609
175610
175611
175612
175613
175614
175615
175616
175617
175618
175619
175620
175621
175622
175623
175624
175625
175626
175627
175628
175629
175630
175631
175632
175633
175634
175635
175636
175637
175638
175639
175640
175641
175642
175643
175644
175645
175646
175647
175648
175649
175650
175651
175652
175653
175654
175655
175656
175657
175658
175659
175660
175661
175662
175663
175664
175665
175666
175667
175668
175669
175670
175671
175672
175673
175674
175675
175676
175677
175678
175679
175680
175681
175682
175683
175684
175685
175686
175687
175688
175689
175690
175691
175692
175693
175694
175695
175696
175697
175698
175699
175700
175701
175702
175703
175704
175705
175706
175707
175708
175709
175710
175711
175712
175713
175714
175715
175716
175717
175718
175719
175720
175721
175722
175723
175724
175725
175726
175727
175728
175729
175730
175731
175732
175733
175734
175735
175736
175737
175738
175739
175740
175741
175742
175743
175744
175745
175746
175747
175748
175749
175750
175751
175752
175753
175754
175755
175756
175757
175758
175759
175760
175761
175762
175763
175764
175765
175766
175767
175768
175769
175770
175771
175772
175773
175774
175775
175776
175777
175778
175779
175780
175781
175782
175783
175784
175785
175786
175787
175788
175789
175790
175791
175792
175793
175794
175795
175796
175797
175798
175799
175800
175801
175802
175803
175804
175805
175806
175807
175808
175809
175810
175811
175812
175813
175814
175815
175816
175817
175818
175819
175820
175821
175822
175823
175824
175825
175826
175827
175828
175829
175830
175831
175832
175833
175834
175835
175836
175837
175838
175839
175840
175841
175842
175843
175844
175845
175846
175847
175848
175849
175850
175851
175852
175853
175854
175855
175856
175857
175858
175859
175860
175861
175862
175863
175864
175865
175866
175867
175868
175869
175870
175871
175872
175873
175874
175875
175876
175877
175878
175879
175880
175881
175882
175883
175884
175885
175886
175887
175888
175889
175890
175891
175892
175893
175894
175895
175896
175897
175898
175899
175900
175901
175902
175903
175904
175905
175906
175907
175908
175909
175910
175911
175912
175913
175914
175915
175916
175917
175918
175919
175920
175921
175922
175923
175924
175925
175926
175927
175928
175929
175930
175931
175932
175933
175934
175935
175936
175937
175938
175939
175940
175941
175942
175943
175944
175945
175946
175947
175948
175949
175950
175951
175952
175953
175954
175955
175956
175957
175958
175959
175960
175961
175962
175963
175964
175965
175966
175967
175968
175969
175970
175971
175972
175973
175974
175975
175976
175977
175978
175979
175980
175981
175982
175983
175984
175985
175986
175987
175988
175989
175990
175991
175992
175993
175994
175995
175996
175997
175998
175999
176000
176001
176002
176003
176004
176005
176006
176007
176008
176009
176010
176011
176012
176013
176014
176015
176016
176017
176018
176019
176020
176021
176022
176023
176024
176025
176026
176027
176028
176029
176030
176031
176032
176033
176034
176035
176036
176037
176038
176039
176040
176041
176042
176043
176044
176045
176046
176047
176048
176049
176050
176051
176052
176053
176054
176055
176056
176057
176058
176059
176060
176061
176062
176063
176064
176065
176066
176067
176068
176069
176070
176071
176072
176073
176074
176075
176076
176077
176078
176079
176080
176081
176082
176083
176084
176085
176086
176087
176088
176089
176090
176091
176092
176093
176094
176095
176096
176097
176098
176099
176100
176101
176102
176103
176104
176105
176106
176107
176108
176109
176110
176111
176112
176113
176114
176115
176116
176117
176118
176119
176120
176121
176122
176123
176124
176125
176126
176127
176128
176129
176130
176131
176132
176133
176134
176135
176136
176137
176138
176139
176140
176141
176142
176143
176144
176145
176146
176147
176148
176149
176150
176151
176152
176153
176154
176155
176156
176157
176158
176159
176160
176161
176162
176163
176164
176165
176166
176167
176168
176169
176170
176171
176172
176173
176174
176175
176176
176177
176178
176179
176180
176181
176182
176183
176184
176185
176186
176187
176188
176189
176190
176191
176192
176193
176194
176195
176196
176197
176198
176199
176200
176201
176202
176203
176204
176205
176206
176207
176208
176209
176210
176211
176212
176213
176214
176215
176216
176217
176218
176219
176220
176221
176222
176223
176224
176225
176226
176227
176228
176229
176230
176231
176232
176233
176234
176235
176236
176237
176238
176239
176240
176241
176242
176243
176244
176245
176246
176247
176248
176249
176250
176251
176252
176253
176254
176255
176256
176257
176258
176259
176260
176261
176262
176263
176264
176265
176266
176267
176268
176269
176270
176271
176272
176273
176274
176275
176276
176277
176278
176279
176280
176281
176282
176283
176284
176285
176286
176287
176288
176289
176290
176291
176292
176293
176294
176295
176296
176297
176298
176299
176300
176301
176302
176303
176304
176305
176306
176307
176308
176309
176310
176311
176312
176313
176314
176315
176316
176317
176318
176319
176320
176321
176322
176323
176324
176325
176326
176327
176328
176329
176330
176331
176332
176333
176334
176335
176336
176337
176338
176339
176340
176341
176342
176343
176344
176345
176346
176347
176348
176349
176350
176351
176352
176353
176354
176355
176356
176357
176358
176359
176360
176361
176362
176363
176364
176365
176366
176367
176368
176369
176370
176371
176372
176373
176374
176375
176376
176377
176378
176379
176380
176381
176382
176383
176384
176385
176386
176387
176388
176389
176390
176391
176392
176393
176394
176395
176396
176397
176398
176399
176400
176401
176402
176403
176404
176405
176406
176407
176408
176409
176410
176411
176412
176413
176414
176415
176416
176417
176418
176419
176420
176421
176422
176423
176424
176425
176426
176427
176428
176429
176430
176431
176432
176433
176434
176435
176436
176437
176438
176439
176440
176441
176442
176443
176444
176445
176446
176447
176448
176449
176450
176451
176452
176453
176454
176455
176456
176457
176458
176459
176460
176461
176462
176463
176464
176465
176466
176467
176468
176469
176470
176471
176472
176473
176474
176475
176476
176477
176478
176479
176480
176481
176482
176483
176484
176485
176486
176487
176488
176489
176490
176491
176492
176493
176494
176495
176496
176497
176498
176499
176500
176501
176502
176503
176504
176505
176506
176507
176508
176509
176510
176511
176512
176513
176514
176515
176516
176517
176518
176519
176520
176521
176522
176523
176524
176525
176526
176527
176528
176529
176530
176531
176532
176533
176534
176535
176536
176537
176538
176539
176540
176541
176542
176543
176544
176545
176546
176547
176548
176549
176550
176551
176552
176553
176554
176555
176556
176557
176558
176559
176560
176561
176562
176563
176564
176565
176566
176567
176568
176569
176570
176571
176572
176573
176574
176575
176576
176577
176578
176579
176580
176581
176582
176583
176584
176585
176586
176587
176588
176589
176590
176591
176592
176593
176594
176595
176596
176597
176598
176599
176600
176601
176602
176603
176604
176605
176606
176607
176608
176609
176610
176611
176612
176613
176614
176615
176616
176617
176618
176619
176620
176621
176622
176623
176624
176625
176626
176627
176628
176629
176630
176631
176632
176633
176634
176635
176636
176637
176638
176639
176640
176641
176642
176643
176644
176645
176646
176647
176648
176649
176650
176651
176652
176653
176654
176655
176656
176657
176658
176659
176660
176661
176662
176663
176664
176665
176666
176667
176668
176669
176670
176671
176672
176673
176674
176675
176676
176677
176678
176679
176680
176681
176682
176683
176684
176685
176686
176687
176688
176689
176690
176691
176692
176693
176694
176695
176696
176697
176698
176699
176700
176701
176702
176703
176704
176705
176706
176707
176708
176709
176710
176711
176712
176713
176714
176715
176716
176717
176718
176719
176720
176721
176722
176723
176724
176725
176726
176727
176728
176729
176730
176731
176732
176733
176734
176735
176736
176737
176738
176739
176740
176741
176742
176743
176744
176745
176746
176747
176748
176749
176750
176751
176752
176753
176754
176755
176756
176757
176758
176759
176760
176761
176762
176763
176764
176765
176766
176767
176768
176769
176770
176771
176772
176773
176774
176775
176776
176777
176778
176779
176780
176781
176782
176783
176784
176785
176786
176787
176788
176789
176790
176791
176792
176793
176794
176795
176796
176797
176798
176799
176800
176801
176802
176803
176804
176805
176806
176807
176808
176809
176810
176811
176812
176813
176814
176815
176816
176817
176818
176819
176820
176821
176822
176823
176824
176825
176826
176827
176828
176829
176830
176831
176832
176833
176834
176835
176836
176837
176838
176839
176840
176841
176842
176843
176844
176845
176846
176847
176848
176849
176850
176851
176852
176853
176854
176855
176856
176857
176858
176859
176860
176861
176862
176863
176864
176865
176866
176867
176868
176869
176870
176871
176872
176873
176874
176875
176876
176877
176878
176879
176880
176881
176882
176883
176884
176885
176886
176887
176888
176889
176890
176891
176892
176893
176894
176895
176896
176897
176898
176899
176900
176901
176902
176903
176904
176905
176906
176907
176908
176909
176910
176911
176912
176913
176914
176915
176916
176917
176918
176919
176920
176921
176922
176923
176924
176925
176926
176927
176928
176929
176930
176931
176932
176933
176934
176935
176936
176937
176938
176939
176940
176941
176942
176943
176944
176945
176946
176947
176948
176949
176950
176951
176952
176953
176954
176955
176956
176957
176958
176959
176960
176961
176962
176963
176964
176965
176966
176967
176968
176969
176970
176971
176972
176973
176974
176975
176976
176977
176978
176979
176980
176981
176982
176983
176984
176985
176986
176987
176988
176989
176990
176991
176992
176993
176994
176995
176996
176997
176998
176999
177000
177001
177002
177003
177004
177005
177006
177007
177008
177009
177010
177011
177012
177013
177014
177015
177016
177017
177018
177019
177020
177021
177022
177023
177024
177025
177026
177027
177028
177029
177030
177031
177032
177033
177034
177035
177036
177037
177038
177039
177040
177041
177042
177043
177044
177045
177046
177047
177048
177049
177050
177051
177052
177053
177054
177055
177056
177057
177058
177059
177060
177061
177062
177063
177064
177065
177066
177067
177068
177069
177070
177071
177072
177073
177074
177075
177076
177077
177078
177079
177080
177081
177082
177083
177084
177085
177086
177087
177088
177089
177090
177091
177092
177093
177094
177095
177096
177097
177098
177099
177100
177101
177102
177103
177104
177105
177106
177107
177108
177109
177110
177111
177112
177113
177114
177115
177116
177117
177118
177119
177120
177121
177122
177123
177124
177125
177126
177127
177128
177129
177130
177131
177132
177133
177134
177135
177136
177137
177138
177139
177140
177141
177142
177143
177144
177145
177146
177147
177148
177149
177150
177151
177152
177153
177154
177155
177156
177157
177158
177159
177160
177161
177162
177163
177164
177165
177166
177167
177168
177169
177170
177171
177172
177173
177174
177175
177176
177177
177178
177179
177180
177181
177182
177183
177184
177185
177186
177187
177188
177189
177190
177191
177192
177193
177194
177195
177196
177197
177198
177199
177200
177201
177202
177203
177204
177205
177206
177207
177208
177209
177210
177211
177212
177213
177214
177215
177216
177217
177218
177219
177220
177221
177222
177223
177224
177225
177226
177227
177228
177229
177230
177231
177232
177233
177234
177235
177236
177237
177238
177239
177240
177241
177242
177243
177244
177245
177246
177247
177248
177249
177250
177251
177252
177253
177254
177255
177256
177257
177258
177259
177260
177261
177262
177263
177264
177265
177266
177267
177268
177269
177270
177271
177272
177273
177274
177275
177276
177277
177278
177279
177280
177281
177282
177283
177284
177285
177286
177287
177288
177289
177290
177291
177292
177293
177294
177295
177296
177297
177298
177299
177300
177301
177302
177303
177304
177305
177306
177307
177308
177309
177310
177311
177312
177313
177314
177315
177316
177317
177318
177319
177320
177321
177322
177323
177324
177325
177326
177327
177328
177329
177330
177331
177332
177333
177334
177335
177336
177337
177338
177339
177340
177341
177342
177343
177344
177345
177346
177347
177348
177349
177350
177351
177352
177353
177354
177355
177356
177357
177358
177359
177360
177361
177362
177363
177364
177365
177366
177367
177368
177369
177370
177371
177372
177373
177374
177375
177376
177377
177378
177379
177380
177381
177382
177383
177384
177385
177386
177387
177388
177389
177390
177391
177392
177393
177394
177395
177396
177397
177398
177399
177400
177401
177402
177403
177404
177405
177406
177407
177408
177409
177410
177411
177412
177413
177414
177415
177416
177417
177418
177419
177420
177421
177422
177423
177424
177425
177426
177427
177428
177429
177430
177431
177432
177433
177434
177435
177436
177437
177438
177439
177440
177441
177442
177443
177444
177445
177446
177447
177448
177449
177450
177451
177452
177453
177454
177455
177456
177457
177458
177459
177460
177461
177462
177463
177464
177465
177466
177467
177468
177469
177470
177471
177472
177473
177474
177475
177476
177477
177478
177479
177480
177481
177482
177483
177484
177485
177486
177487
177488
177489
177490
177491
177492
177493
177494
177495
177496
177497
177498
177499
177500
177501
177502
177503
177504
177505
177506
177507
177508
177509
177510
177511
177512
177513
177514
177515
177516
177517
177518
177519
177520
177521
177522
177523
177524
177525
177526
177527
177528
177529
177530
177531
177532
177533
177534
177535
177536
177537
177538
177539
177540
177541
177542
177543
177544
177545
177546
177547
177548
177549
177550
177551
177552
177553
177554
177555
177556
177557
177558
177559
177560
177561
177562
177563
177564
177565
177566
177567
177568
177569
177570
177571
177572
177573
177574
177575
177576
177577
177578
177579
177580
177581
177582
177583
177584
177585
177586
177587
177588
177589
177590
177591
177592
177593
177594
177595
177596
177597
177598
177599
177600
177601
177602
177603
177604
177605
177606
177607
177608
177609
177610
177611
177612
177613
177614
177615
177616
177617
177618
177619
177620
177621
177622
177623
177624
177625
177626
177627
177628
177629
177630
177631
177632
177633
177634
177635
177636
177637
177638
177639
177640
177641
177642
177643
177644
177645
177646
177647
177648
177649
177650
177651
177652
177653
177654
177655
177656
177657
177658
177659
177660
177661
177662
177663
177664
177665
177666
177667
177668
177669
177670
177671
177672
177673
177674
177675
177676
177677
177678
177679
177680
177681
177682
177683
177684
177685
177686
177687
177688
177689
177690
177691
177692
177693
177694
177695
177696
177697
177698
177699
177700
177701
177702
177703
177704
177705
177706
177707
177708
177709
177710
177711
177712
177713
177714
177715
177716
177717
177718
177719
177720
177721
177722
177723
177724
177725
177726
177727
177728
177729
177730
177731
177732
177733
177734
177735
177736
177737
177738
177739
177740
177741
177742
177743
177744
177745
177746
177747
177748
177749
177750
177751
177752
177753
177754
177755
177756
177757
177758
177759
177760
177761
177762
177763
177764
177765
177766
177767
177768
177769
177770
177771
177772
177773
177774
177775
177776
177777
177778
177779
177780
177781
177782
177783
177784
177785
177786
177787
177788
177789
177790
177791
177792
177793
177794
177795
177796
177797
177798
177799
177800
177801
177802
177803
177804
177805
177806
177807
177808
177809
177810
177811
177812
177813
177814
177815
177816
177817
177818
177819
177820
177821
177822
177823
177824
177825
177826
177827
177828
177829
177830
177831
177832
177833
177834
177835
177836
177837
177838
177839
177840
177841
177842
177843
177844
177845
177846
177847
177848
177849
177850
177851
177852
177853
177854
177855
177856
177857
177858
177859
177860
177861
177862
177863
177864
177865
177866
177867
177868
177869
177870
177871
177872
177873
177874
177875
177876
177877
177878
177879
177880
177881
177882
177883
177884
177885
177886
177887
177888
177889
177890
177891
177892
177893
177894
177895
177896
177897
177898
177899
177900
177901
177902
177903
177904
177905
177906
177907
177908
177909
177910
177911
177912
177913
177914
177915
177916
177917
177918
177919
177920
177921
177922
177923
177924
177925
177926
177927
177928
177929
177930
177931
177932
177933
177934
177935
177936
177937
177938
177939
177940
177941
177942
177943
177944
177945
177946
177947
177948
177949
177950
177951
177952
177953
177954
177955
177956
177957
177958
177959
177960
177961
177962
177963
177964
177965
177966
177967
177968
177969
177970
177971
177972
177973
177974
177975
177976
177977
177978
177979
177980
177981
177982
177983
177984
177985
177986
177987
177988
177989
177990
177991
177992
177993
177994
177995
177996
177997
177998
177999
178000
178001
178002
178003
178004
178005
178006
178007
178008
178009
178010
178011
178012
178013
178014
178015
178016
178017
178018
178019
178020
178021
178022
178023
178024
178025
178026
178027
178028
178029
178030
178031
178032
178033
178034
178035
178036
178037
178038
178039
178040
178041
178042
178043
178044
178045
178046
178047
178048
178049
178050
178051
178052
178053
178054
178055
178056
178057
178058
178059
178060
178061
178062
178063
178064
178065
178066
178067
178068
178069
178070
178071
178072
178073
178074
178075
178076
178077
178078
178079
178080
178081
178082
178083
178084
178085
178086
178087
178088
178089
178090
178091
178092
178093
178094
178095
178096
178097
178098
178099
178100
178101
178102
178103
178104
178105
178106
178107
178108
178109
178110
178111
178112
178113
178114
178115
178116
178117
178118
178119
178120
178121
178122
178123
178124
178125
178126
178127
178128
178129
178130
178131
178132
178133
178134
178135
178136
178137
178138
178139
178140
178141
178142
178143
178144
178145
178146
178147
178148
178149
178150
178151
178152
178153
178154
178155
178156
178157
178158
178159
178160
178161
178162
178163
178164
178165
178166
178167
178168
178169
178170
178171
178172
178173
178174
178175
178176
178177
178178
178179
178180
178181
178182
178183
178184
178185
178186
178187
178188
178189
178190
178191
178192
178193
178194
178195
178196
178197
178198
178199
178200
178201
178202
178203
178204
178205
178206
178207
178208
178209
178210
178211
178212
178213
178214
178215
178216
178217
178218
178219
178220
178221
178222
178223
178224
178225
178226
178227
178228
178229
178230
178231
178232
178233
178234
178235
178236
178237
178238
178239
178240
178241
178242
178243
178244
178245
178246
178247
178248
178249
178250
178251
178252
178253
178254
178255
178256
178257
178258
178259
178260
178261
178262
178263
178264
178265
178266
178267
178268
178269
178270
178271
178272
178273
178274
178275
178276
178277
178278
178279
178280
178281
178282
178283
178284
178285
178286
178287
178288
178289
178290
178291
178292
178293
178294
178295
178296
178297
178298
178299
178300
178301
178302
178303
178304
178305
178306
178307
178308
178309
178310
178311
178312
178313
178314
178315
178316
178317
178318
178319
178320
178321
178322
178323
178324
178325
178326
178327
178328
178329
178330
178331
178332
178333
178334
178335
178336
178337
178338
178339
178340
178341
178342
178343
178344
178345
178346
178347
178348
178349
178350
178351
178352
178353
178354
178355
178356
178357
178358
178359
178360
178361
178362
178363
178364
178365
178366
178367
178368
178369
178370
178371
178372
178373
178374
178375
178376
178377
178378
178379
178380
178381
178382
178383
178384
178385
178386
178387
178388
178389
178390
178391
178392
178393
178394
178395
178396
178397
178398
178399
178400
178401
178402
178403
178404
178405
178406
178407
178408
178409
178410
178411
178412
178413
178414
178415
178416
178417
178418
178419
178420
178421
178422
178423
178424
178425
178426
178427
178428
178429
178430
178431
178432
178433
178434
178435
178436
178437
178438
178439
178440
178441
178442
178443
178444
178445
178446
178447
178448
178449
178450
178451
178452
178453
178454
178455
178456
178457
178458
178459
178460
178461
178462
178463
178464
178465
178466
178467
178468
178469
178470
178471
178472
178473
178474
178475
178476
178477
178478
178479
178480
178481
178482
178483
178484
178485
178486
178487
178488
178489
178490
178491
178492
178493
178494
178495
178496
178497
178498
178499
178500
178501
178502
178503
178504
178505
178506
178507
178508
178509
178510
178511
178512
178513
178514
178515
178516
178517
178518
178519
178520
178521
178522
178523
178524
178525
178526
178527
178528
178529
178530
178531
178532
178533
178534
178535
178536
178537
178538
178539
178540
178541
178542
178543
178544
178545
178546
178547
178548
178549
178550
178551
178552
178553
178554
178555
178556
178557
178558
178559
178560
178561
178562
178563
178564
178565
178566
178567
178568
178569
178570
178571
178572
178573
178574
178575
178576
178577
178578
178579
178580
178581
178582
178583
178584
178585
178586
178587
178588
178589
178590
178591
178592
178593
178594
178595
178596
178597
178598
178599
178600
178601
178602
178603
178604
178605
178606
178607
178608
178609
178610
178611
178612
178613
178614
178615
178616
178617
178618
178619
178620
178621
178622
178623
178624
178625
178626
178627
178628
178629
178630
178631
178632
178633
178634
178635
178636
178637
178638
178639
178640
178641
178642
178643
178644
178645
178646
178647
178648
178649
178650
178651
178652
178653
178654
178655
178656
178657
178658
178659
178660
178661
178662
178663
178664
178665
178666
178667
178668
178669
178670
178671
178672
178673
178674
178675
178676
178677
178678
178679
178680
178681
178682
178683
178684
178685
178686
178687
178688
178689
178690
178691
178692
178693
178694
178695
178696
178697
178698
178699
178700
178701
178702
178703
178704
178705
178706
178707
178708
178709
178710
178711
178712
178713
178714
178715
178716
178717
178718
178719
178720
178721
178722
178723
178724
178725
178726
178727
178728
178729
178730
178731
178732
178733
178734
178735
178736
178737
178738
178739
178740
178741
178742
178743
178744
178745
178746
178747
178748
178749
178750
178751
178752
178753
178754
178755
178756
178757
178758
178759
178760
178761
178762
178763
178764
178765
178766
178767
178768
178769
178770
178771
178772
178773
178774
178775
178776
178777
178778
178779
178780
178781
178782
178783
178784
178785
178786
178787
178788
178789
178790
178791
178792
178793
178794
178795
178796
178797
178798
178799
178800
178801
178802
178803
178804
178805
178806
178807
178808
178809
178810
178811
178812
178813
178814
178815
178816
178817
178818
178819
178820
178821
178822
178823
178824
178825
178826
178827
178828
178829
178830
178831
178832
178833
178834
178835
178836
178837
178838
178839
178840
178841
178842
178843
178844
178845
178846
178847
178848
178849
178850
178851
178852
178853
178854
178855
178856
178857
178858
178859
178860
178861
178862
178863
178864
178865
178866
178867
178868
178869
178870
178871
178872
178873
178874
178875
178876
178877
178878
178879
178880
178881
178882
178883
178884
178885
178886
178887
178888
178889
178890
178891
178892
178893
178894
178895
178896
178897
178898
178899
178900
178901
178902
178903
178904
178905
178906
178907
178908
178909
178910
178911
178912
178913
178914
178915
178916
178917
178918
178919
178920
178921
178922
178923
178924
178925
178926
178927
178928
178929
178930
178931
178932
178933
178934
178935
178936
178937
178938
178939
178940
178941
178942
178943
178944
178945
178946
178947
178948
178949
178950
178951
178952
178953
178954
178955
178956
178957
178958
178959
178960
178961
178962
178963
178964
178965
178966
178967
178968
178969
178970
178971
178972
178973
178974
178975
178976
178977
178978
178979
178980
178981
178982
178983
178984
178985
178986
178987
178988
178989
178990
178991
178992
178993
178994
178995
178996
178997
178998
178999
179000
179001
179002
179003
179004
179005
179006
179007
179008
179009
179010
179011
179012
179013
179014
179015
179016
179017
179018
179019
179020
179021
179022
179023
179024
179025
179026
179027
179028
179029
179030
179031
179032
179033
179034
179035
179036
179037
179038
179039
179040
179041
179042
179043
179044
179045
179046
179047
179048
179049
179050
179051
179052
179053
179054
179055
179056
179057
179058
179059
179060
179061
179062
179063
179064
179065
179066
179067
179068
179069
179070
179071
179072
179073
179074
179075
179076
179077
179078
179079
179080
179081
179082
179083
179084
179085
179086
179087
179088
179089
179090
179091
179092
179093
179094
179095
179096
179097
179098
179099
179100
179101
179102
179103
179104
179105
179106
179107
179108
179109
179110
179111
179112
179113
179114
179115
179116
179117
179118
179119
179120
179121
179122
179123
179124
179125
179126
179127
179128
179129
179130
179131
179132
179133
179134
179135
179136
179137
179138
179139
179140
179141
179142
179143
179144
179145
179146
179147
179148
179149
179150
179151
179152
179153
179154
179155
179156
179157
179158
179159
179160
179161
179162
179163
179164
179165
179166
179167
179168
179169
179170
179171
179172
179173
179174
179175
179176
179177
179178
179179
179180
179181
179182
179183
179184
179185
179186
179187
179188
179189
179190
179191
179192
179193
179194
179195
179196
179197
179198
179199
179200
179201
179202
179203
179204
179205
179206
179207
179208
179209
179210
179211
179212
179213
179214
179215
179216
179217
179218
179219
179220
179221
179222
179223
179224
179225
179226
179227
179228
179229
179230
179231
179232
179233
179234
179235
179236
179237
179238
179239
179240
179241
179242
179243
179244
179245
179246
179247
179248
179249
179250
179251
179252
179253
179254
179255
179256
179257
179258
179259
179260
179261
179262
179263
179264
179265
179266
179267
179268
179269
179270
179271
179272
179273
179274
179275
179276
179277
179278
179279
179280
179281
179282
179283
179284
179285
179286
179287
179288
179289
179290
179291
179292
179293
179294
179295
179296
179297
179298
179299
179300
179301
179302
179303
179304
179305
179306
179307
179308
179309
179310
179311
179312
179313
179314
179315
179316
179317
179318
179319
179320
179321
179322
179323
179324
179325
179326
179327
179328
179329
179330
179331
179332
179333
179334
179335
179336
179337
179338
179339
179340
179341
179342
179343
179344
179345
179346
179347
179348
179349
179350
179351
179352
179353
179354
179355
179356
179357
179358
179359
179360
179361
179362
179363
179364
179365
179366
179367
179368
179369
179370
179371
179372
179373
179374
179375
179376
179377
179378
179379
179380
179381
179382
179383
179384
179385
179386
179387
179388
179389
179390
179391
179392
179393
179394
179395
179396
179397
179398
179399
179400
179401
179402
179403
179404
179405
179406
179407
179408
179409
179410
179411
179412
179413
179414
179415
179416
179417
179418
179419
179420
179421
179422
179423
179424
179425
179426
179427
179428
179429
179430
179431
179432
179433
179434
179435
179436
179437
179438
179439
179440
179441
179442
179443
179444
179445
179446
179447
179448
179449
179450
179451
179452
179453
179454
179455
179456
179457
179458
179459
179460
179461
179462
179463
179464
179465
179466
179467
179468
179469
179470
179471
179472
179473
179474
179475
179476
179477
179478
179479
179480
179481
179482
179483
179484
179485
179486
179487
179488
179489
179490
179491
179492
179493
179494
179495
179496
179497
179498
179499
179500
179501
179502
179503
179504
179505
179506
179507
179508
179509
179510
179511
179512
179513
179514
179515
179516
179517
179518
179519
179520
179521
179522
179523
179524
179525
179526
179527
179528
179529
179530
179531
179532
179533
179534
179535
179536
179537
179538
179539
179540
179541
179542
179543
179544
179545
179546
179547
179548
179549
179550
179551
179552
179553
179554
179555
179556
179557
179558
179559
179560
179561
179562
179563
179564
179565
179566
179567
179568
179569
179570
179571
179572
179573
179574
179575
179576
179577
179578
179579
179580
179581
179582
179583
179584
179585
179586
179587
179588
179589
179590
179591
179592
179593
179594
179595
179596
179597
179598
179599
179600
179601
179602
179603
179604
179605
179606
179607
179608
179609
179610
179611
179612
179613
179614
179615
179616
179617
179618
179619
179620
179621
179622
179623
179624
179625
179626
179627
179628
179629
179630
179631
179632
179633
179634
179635
179636
179637
179638
179639
179640
179641
179642
179643
179644
179645
179646
179647
179648
179649
179650
179651
179652
179653
179654
179655
179656
179657
179658
179659
179660
179661
179662
179663
179664
179665
179666
179667
179668
179669
179670
179671
179672
179673
179674
179675
179676
179677
179678
179679
179680
179681
179682
179683
179684
179685
179686
179687
179688
179689
179690
179691
179692
179693
179694
179695
179696
179697
179698
179699
179700
179701
179702
179703
179704
179705
179706
179707
179708
179709
179710
179711
179712
179713
179714
179715
179716
179717
179718
179719
179720
179721
179722
179723
179724
179725
179726
179727
179728
179729
179730
179731
179732
179733
179734
179735
179736
179737
179738
179739
179740
179741
179742
179743
179744
179745
179746
179747
179748
179749
179750
179751
179752
179753
179754
179755
179756
179757
179758
179759
179760
179761
179762
179763
179764
179765
179766
179767
179768
179769
179770
179771
179772
179773
179774
179775
179776
179777
179778
179779
179780
179781
179782
179783
179784
179785
179786
179787
179788
179789
179790
179791
179792
179793
179794
179795
179796
179797
179798
179799
179800
179801
179802
179803
179804
179805
179806
179807
179808
179809
179810
179811
179812
179813
179814
179815
179816
179817
179818
179819
179820
179821
179822
179823
179824
179825
179826
179827
179828
179829
179830
179831
179832
179833
179834
179835
179836
179837
179838
179839
179840
179841
179842
179843
179844
179845
179846
179847
179848
179849
179850
179851
179852
179853
179854
179855
179856
179857
179858
179859
179860
179861
179862
179863
179864
179865
179866
179867
179868
179869
179870
179871
179872
179873
179874
179875
179876
179877
179878
179879
179880
179881
179882
179883
179884
179885
179886
179887
179888
179889
179890
179891
179892
179893
179894
179895
179896
179897
179898
179899
179900
179901
179902
179903
179904
179905
179906
179907
179908
179909
179910
179911
179912
179913
179914
179915
179916
179917
179918
179919
179920
179921
179922
179923
179924
179925
179926
179927
179928
179929
179930
179931
179932
179933
179934
179935
179936
179937
179938
179939
179940
179941
179942
179943
179944
179945
179946
179947
179948
179949
179950
179951
179952
179953
179954
179955
179956
179957
179958
179959
179960
179961
179962
179963
179964
179965
179966
179967
179968
179969
179970
179971
179972
179973
179974
179975
179976
179977
179978
179979
179980
179981
179982
179983
179984
179985
179986
179987
179988
179989
179990
179991
179992
179993
179994
179995
179996
179997
179998
179999
180000
180001
180002
180003
180004
180005
180006
180007
180008
180009
180010
180011
180012
180013
180014
180015
180016
180017
180018
180019
180020
180021
180022
180023
180024
180025
180026
180027
180028
180029
180030
180031
180032
180033
180034
180035
180036
180037
180038
180039
180040
180041
180042
180043
180044
180045
180046
180047
180048
180049
180050
180051
180052
180053
180054
180055
180056
180057
180058
180059
180060
180061
180062
180063
180064
180065
180066
180067
180068
180069
180070
180071
180072
180073
180074
180075
180076
180077
180078
180079
180080
180081
180082
180083
180084
180085
180086
180087
180088
180089
180090
180091
180092
180093
180094
180095
180096
180097
180098
180099
180100
180101
180102
180103
180104
180105
180106
180107
180108
180109
180110
180111
180112
180113
180114
180115
180116
180117
180118
180119
180120
180121
180122
180123
180124
180125
180126
180127
180128
180129
180130
180131
180132
180133
180134
180135
180136
180137
180138
180139
180140
180141
180142
180143
180144
180145
180146
180147
180148
180149
180150
180151
180152
180153
180154
180155
180156
180157
180158
180159
180160
180161
180162
180163
180164
180165
180166
180167
180168
180169
180170
180171
180172
180173
180174
180175
180176
180177
180178
180179
180180
180181
180182
180183
180184
180185
180186
180187
180188
180189
180190
180191
180192
180193
180194
180195
180196
180197
180198
180199
180200
180201
180202
180203
180204
180205
180206
180207
180208
180209
180210
180211
180212
180213
180214
180215
180216
180217
180218
180219
180220
180221
180222
180223
180224
180225
180226
180227
180228
180229
180230
180231
180232
180233
180234
180235
180236
180237
180238
180239
180240
180241
180242
180243
180244
180245
180246
180247
180248
180249
180250
180251
180252
180253
180254
180255
180256
180257
180258
180259
180260
180261
180262
180263
180264
180265
180266
180267
180268
180269
180270
180271
180272
180273
180274
180275
180276
180277
180278
180279
180280
180281
180282
180283
180284
180285
180286
180287
180288
180289
180290
180291
180292
180293
180294
180295
180296
180297
180298
180299
180300
180301
180302
180303
180304
180305
180306
180307
180308
180309
180310
180311
180312
180313
180314
180315
180316
180317
180318
180319
180320
180321
180322
180323
180324
180325
180326
180327
180328
180329
180330
180331
180332
180333
180334
180335
180336
180337
180338
180339
180340
180341
180342
180343
180344
180345
180346
180347
180348
180349
180350
180351
180352
180353
180354
180355
180356
180357
180358
180359
180360
180361
180362
180363
180364
180365
180366
180367
180368
180369
180370
180371
180372
180373
180374
180375
180376
180377
180378
180379
180380
180381
180382
180383
180384
180385
180386
180387
180388
180389
180390
180391
180392
180393
180394
180395
180396
180397
180398
180399
180400
180401
180402
180403
180404
180405
180406
180407
180408
180409
180410
180411
180412
180413
180414
180415
180416
180417
180418
180419
180420
180421
180422
180423
180424
180425
180426
180427
180428
180429
180430
180431
180432
180433
180434
180435
180436
180437
180438
180439
180440
180441
180442
180443
180444
180445
180446
180447
180448
180449
180450
180451
180452
180453
180454
180455
180456
180457
180458
180459
180460
180461
180462
180463
180464
180465
180466
180467
180468
180469
180470
180471
180472
180473
180474
180475
180476
180477
180478
180479
180480
180481
180482
180483
180484
180485
180486
180487
180488
180489
180490
180491
180492
180493
180494
180495
180496
180497
180498
180499
180500
180501
180502
180503
180504
180505
180506
180507
180508
180509
180510
180511
180512
180513
180514
180515
180516
180517
180518
180519
180520
180521
180522
180523
180524
180525
180526
180527
180528
180529
180530
180531
180532
180533
180534
180535
180536
180537
180538
180539
180540
180541
180542
180543
180544
180545
180546
180547
180548
180549
180550
180551
180552
180553
180554
180555
180556
180557
180558
180559
180560
180561
180562
180563
180564
180565
180566
180567
180568
180569
180570
180571
180572
180573
180574
180575
180576
180577
180578
180579
180580
180581
180582
180583
180584
180585
180586
180587
180588
180589
180590
180591
180592
180593
180594
180595
180596
180597
180598
180599
180600
180601
180602
180603
180604
180605
180606
180607
180608
180609
180610
180611
180612
180613
180614
180615
180616
180617
180618
180619
180620
180621
180622
180623
180624
180625
180626
180627
180628
180629
180630
180631
180632
180633
180634
180635
180636
180637
180638
180639
180640
180641
180642
180643
180644
180645
180646
180647
180648
180649
180650
180651
180652
180653
180654
180655
180656
180657
180658
180659
180660
180661
180662
180663
180664
180665
180666
180667
180668
180669
180670
180671
180672
180673
180674
180675
180676
180677
180678
180679
180680
180681
180682
180683
180684
180685
180686
180687
180688
180689
180690
180691
180692
180693
180694
180695
180696
180697
180698
180699
180700
180701
180702
180703
180704
180705
180706
180707
180708
180709
180710
180711
180712
180713
180714
180715
180716
180717
180718
180719
180720
180721
180722
180723
180724
180725
180726
180727
180728
180729
180730
180731
180732
180733
180734
180735
180736
180737
180738
180739
180740
180741
180742
180743
180744
180745
180746
180747
180748
180749
180750
180751
180752
180753
180754
180755
180756
180757
180758
180759
180760
180761
180762
180763
180764
180765
180766
180767
180768
180769
180770
180771
180772
180773
180774
180775
180776
180777
180778
180779
180780
180781
180782
180783
180784
180785
180786
180787
180788
180789
180790
180791
180792
180793
180794
180795
180796
180797
180798
180799
180800
180801
180802
180803
180804
180805
180806
180807
180808
180809
180810
180811
180812
180813
180814
180815
180816
180817
180818
180819
180820
180821
180822
180823
180824
180825
180826
180827
180828
180829
180830
180831
180832
180833
180834
180835
180836
180837
180838
180839
180840
180841
180842
180843
180844
180845
180846
180847
180848
180849
180850
180851
180852
180853
180854
180855
180856
180857
180858
180859
180860
180861
180862
180863
180864
180865
180866
180867
180868
180869
180870
180871
180872
180873
180874
180875
180876
180877
180878
180879
180880
180881
180882
180883
180884
180885
180886
180887
180888
180889
180890
180891
180892
180893
180894
180895
180896
180897
180898
180899
180900
180901
180902
180903
180904
180905
180906
180907
180908
180909
180910
180911
180912
180913
180914
180915
180916
180917
180918
180919
180920
180921
180922
180923
180924
180925
180926
180927
180928
180929
180930
180931
180932
180933
180934
180935
180936
180937
180938
180939
180940
180941
180942
180943
180944
180945
180946
180947
180948
180949
180950
180951
180952
180953
180954
180955
180956
180957
180958
180959
180960
180961
180962
180963
180964
180965
180966
180967
180968
180969
180970
180971
180972
180973
180974
180975
180976
180977
180978
180979
180980
180981
180982
180983
180984
180985
180986
180987
180988
180989
180990
180991
180992
180993
180994
180995
180996
180997
180998
180999
181000
181001
181002
181003
181004
181005
181006
181007
181008
181009
181010
181011
181012
181013
181014
181015
181016
181017
181018
181019
181020
181021
181022
181023
181024
181025
181026
181027
181028
181029
181030
181031
181032
181033
181034
181035
181036
181037
181038
181039
181040
181041
181042
181043
181044
181045
181046
181047
181048
181049
181050
181051
181052
181053
181054
181055
181056
181057
181058
181059
181060
181061
181062
181063
181064
181065
181066
181067
181068
181069
181070
181071
181072
181073
181074
181075
181076
181077
181078
181079
181080
181081
181082
181083
181084
181085
181086
181087
181088
181089
181090
181091
181092
181093
181094
181095
181096
181097
181098
181099
181100
181101
181102
181103
181104
181105
181106
181107
181108
181109
181110
181111
181112
181113
181114
181115
181116
181117
181118
181119
181120
181121
181122
181123
181124
181125
181126
181127
181128
181129
181130
181131
181132
181133
181134
181135
181136
181137
181138
181139
181140
181141
181142
181143
181144
181145
181146
181147
181148
181149
181150
181151
181152
181153
181154
181155
181156
181157
181158
181159
181160
181161
181162
181163
181164
181165
181166
181167
181168
181169
181170
181171
181172
181173
181174
181175
181176
181177
181178
181179
181180
181181
181182
181183
181184
181185
181186
181187
181188
181189
181190
181191
181192
181193
181194
181195
181196
181197
181198
181199
181200
181201
181202
181203
181204
181205
181206
181207
181208
181209
181210
181211
181212
181213
181214
181215
181216
181217
181218
181219
181220
181221
181222
181223
181224
181225
181226
181227
181228
181229
181230
181231
181232
181233
181234
181235
181236
181237
181238
181239
181240
181241
181242
181243
181244
181245
181246
181247
181248
181249
181250
181251
181252
181253
181254
181255
181256
181257
181258
181259
181260
181261
181262
181263
181264
181265
181266
181267
181268
181269
181270
181271
181272
181273
181274
181275
181276
181277
181278
181279
181280
181281
181282
181283
181284
181285
181286
181287
181288
181289
181290
181291
181292
181293
181294
181295
181296
181297
181298
181299
181300
181301
181302
181303
181304
181305
181306
181307
181308
181309
181310
181311
181312
181313
181314
181315
181316
181317
181318
181319
181320
181321
181322
181323
181324
181325
181326
181327
181328
181329
181330
181331
181332
181333
181334
181335
181336
181337
181338
181339
181340
181341
181342
181343
181344
181345
181346
181347
181348
181349
181350
181351
181352
181353
181354
181355
181356
181357
181358
181359
181360
181361
181362
181363
181364
181365
181366
181367
181368
181369
181370
181371
181372
181373
181374
181375
181376
181377
181378
181379
181380
181381
181382
181383
181384
181385
181386
181387
181388
181389
181390
181391
181392
181393
181394
181395
181396
181397
181398
181399
181400
181401
181402
181403
181404
181405
181406
181407
181408
181409
181410
181411
181412
181413
181414
181415
181416
181417
181418
181419
181420
181421
181422
181423
181424
181425
181426
181427
181428
181429
181430
181431
181432
181433
181434
181435
181436
181437
181438
181439
181440
181441
181442
181443
181444
181445
181446
181447
181448
181449
181450
181451
181452
181453
181454
181455
181456
181457
181458
181459
181460
181461
181462
181463
181464
181465
181466
181467
181468
181469
181470
181471
181472
181473
181474
181475
181476
181477
181478
181479
181480
181481
181482
181483
181484
181485
181486
181487
181488
181489
181490
181491
181492
181493
181494
181495
181496
181497
181498
181499
181500
181501
181502
181503
181504
181505
181506
181507
181508
181509
181510
181511
181512
181513
181514
181515
181516
181517
181518
181519
181520
181521
181522
181523
181524
181525
181526
181527
181528
181529
181530
181531
181532
181533
181534
181535
181536
181537
181538
181539
181540
181541
181542
181543
181544
181545
181546
181547
181548
181549
181550
181551
181552
181553
181554
181555
181556
181557
181558
181559
181560
181561
181562
181563
181564
181565
181566
181567
181568
181569
181570
181571
181572
181573
181574
181575
181576
181577
181578
181579
181580
181581
181582
181583
181584
181585
181586
181587
181588
181589
181590
181591
181592
181593
181594
181595
181596
181597
181598
181599
181600
181601
181602
181603
181604
181605
181606
181607
181608
181609
181610
181611
181612
181613
181614
181615
181616
181617
181618
181619
181620
181621
181622
181623
181624
181625
181626
181627
181628
181629
181630
181631
181632
181633
181634
181635
181636
181637
181638
181639
181640
181641
181642
181643
181644
181645
181646
181647
181648
181649
181650
181651
181652
181653
181654
181655
181656
181657
181658
181659
181660
181661
181662
181663
181664
181665
181666
181667
181668
181669
181670
181671
181672
181673
181674
181675
181676
181677
181678
181679
181680
181681
181682
181683
181684
181685
181686
181687
181688
181689
181690
181691
181692
181693
181694
181695
181696
181697
181698
181699
181700
181701
181702
181703
181704
181705
181706
181707
181708
181709
181710
181711
181712
181713
181714
181715
181716
181717
181718
181719
181720
181721
181722
181723
181724
181725
181726
181727
181728
181729
181730
181731
181732
181733
181734
181735
181736
181737
181738
181739
181740
181741
181742
181743
181744
181745
181746
181747
181748
181749
181750
181751
181752
181753
181754
181755
181756
181757
181758
181759
181760
181761
181762
181763
181764
181765
181766
181767
181768
181769
181770
181771
181772
181773
181774
181775
181776
181777
181778
181779
181780
181781
181782
181783
181784
181785
181786
181787
181788
181789
181790
181791
181792
181793
181794
181795
181796
181797
181798
181799
181800
181801
181802
181803
181804
181805
181806
181807
181808
181809
181810
181811
181812
181813
181814
181815
181816
181817
181818
181819
181820
181821
181822
181823
181824
181825
181826
181827
181828
181829
181830
181831
181832
181833
181834
181835
181836
181837
181838
181839
181840
181841
181842
181843
181844
181845
181846
181847
181848
181849
181850
181851
181852
181853
181854
181855
181856
181857
181858
181859
181860
181861
181862
181863
181864
181865
181866
181867
181868
181869
181870
181871
181872
181873
181874
181875
181876
181877
181878
181879
181880
181881
181882
181883
181884
181885
181886
181887
181888
181889
181890
181891
181892
181893
181894
181895
181896
181897
181898
181899
181900
181901
181902
181903
181904
181905
181906
181907
181908
181909
181910
181911
181912
181913
181914
181915
181916
181917
181918
181919
181920
181921
181922
181923
181924
181925
181926
181927
181928
181929
181930
181931
181932
181933
181934
181935
181936
181937
181938
181939
181940
181941
181942
181943
181944
181945
181946
181947
181948
181949
181950
181951
181952
181953
181954
181955
181956
181957
181958
181959
181960
181961
181962
181963
181964
181965
181966
181967
181968
181969
181970
181971
181972
181973
181974
181975
181976
181977
181978
181979
181980
181981
181982
181983
181984
181985
181986
181987
181988
181989
181990
181991
181992
181993
181994
181995
181996
181997
181998
181999
182000
182001
182002
182003
182004
182005
182006
182007
182008
182009
182010
182011
182012
182013
182014
182015
182016
182017
182018
182019
182020
182021
182022
182023
182024
182025
182026
182027
182028
182029
182030
182031
182032
182033
182034
182035
182036
182037
182038
182039
182040
182041
182042
182043
182044
182045
182046
182047
182048
182049
182050
182051
182052
182053
182054
182055
182056
182057
182058
182059
182060
182061
182062
182063
182064
182065
182066
182067
182068
182069
182070
182071
182072
182073
182074
182075
182076
182077
182078
182079
182080
182081
182082
182083
182084
182085
182086
182087
182088
182089
182090
182091
182092
182093
182094
182095
182096
182097
182098
182099
182100
182101
182102
182103
182104
182105
182106
182107
182108
182109
182110
182111
182112
182113
182114
182115
182116
182117
182118
182119
182120
182121
182122
182123
182124
182125
182126
182127
182128
182129
182130
182131
182132
182133
182134
182135
182136
182137
182138
182139
182140
182141
182142
182143
182144
182145
182146
182147
182148
182149
182150
182151
182152
182153
182154
182155
182156
182157
182158
182159
182160
182161
182162
182163
182164
182165
182166
182167
182168
182169
182170
182171
182172
182173
182174
182175
182176
182177
182178
182179
182180
182181
182182
182183
182184
182185
182186
182187
182188
182189
182190
182191
182192
182193
182194
182195
182196
182197
182198
182199
182200
182201
182202
182203
182204
182205
182206
182207
182208
182209
182210
182211
182212
182213
182214
182215
182216
182217
182218
182219
182220
182221
182222
182223
182224
182225
182226
182227
182228
182229
182230
182231
182232
182233
182234
182235
182236
182237
182238
182239
182240
182241
182242
182243
182244
182245
182246
182247
182248
182249
182250
182251
182252
182253
182254
182255
182256
182257
182258
182259
182260
182261
182262
182263
182264
182265
182266
182267
182268
182269
182270
182271
182272
182273
182274
182275
182276
182277
182278
182279
182280
182281
182282
182283
182284
182285
182286
182287
182288
182289
182290
182291
182292
182293
182294
182295
182296
182297
182298
182299
182300
182301
182302
182303
182304
182305
182306
182307
182308
182309
182310
182311
182312
182313
182314
182315
182316
182317
182318
182319
182320
182321
182322
182323
182324
182325
182326
182327
182328
182329
182330
182331
182332
182333
182334
182335
182336
182337
182338
182339
182340
182341
182342
182343
182344
182345
182346
182347
182348
182349
182350
182351
182352
182353
182354
182355
182356
182357
182358
182359
182360
182361
182362
182363
182364
182365
182366
182367
182368
182369
182370
182371
182372
182373
182374
182375
182376
182377
182378
182379
182380
182381
182382
182383
182384
182385
182386
182387
182388
182389
182390
182391
182392
182393
182394
182395
182396
182397
182398
182399
182400
182401
182402
182403
182404
182405
182406
182407
182408
182409
182410
182411
182412
182413
182414
182415
182416
182417
182418
182419
182420
182421
182422
182423
182424
182425
182426
182427
182428
182429
182430
182431
182432
182433
182434
182435
182436
182437
182438
182439
182440
182441
182442
182443
182444
182445
182446
182447
182448
182449
182450
182451
182452
182453
182454
182455
182456
182457
182458
182459
182460
182461
182462
182463
182464
182465
182466
182467
182468
182469
182470
182471
182472
182473
182474
182475
182476
182477
182478
182479
182480
182481
182482
182483
182484
182485
182486
182487
182488
182489
182490
182491
182492
182493
182494
182495
182496
182497
182498
182499
182500
182501
182502
182503
182504
182505
182506
182507
182508
182509
182510
182511
182512
182513
182514
182515
182516
182517
182518
182519
182520
182521
182522
182523
182524
182525
182526
182527
182528
182529
182530
182531
182532
182533
182534
182535
182536
182537
182538
182539
182540
182541
182542
182543
182544
182545
182546
182547
182548
182549
182550
182551
182552
182553
182554
182555
182556
182557
182558
182559
182560
182561
182562
182563
182564
182565
182566
182567
182568
182569
182570
182571
182572
182573
182574
182575
182576
182577
182578
182579
182580
182581
182582
182583
182584
182585
182586
182587
182588
182589
182590
182591
182592
182593
182594
182595
182596
182597
182598
182599
182600
182601
182602
182603
182604
182605
182606
182607
182608
182609
182610
182611
182612
182613
182614
182615
182616
182617
182618
182619
182620
182621
182622
182623
182624
182625
182626
182627
182628
182629
182630
182631
182632
182633
182634
182635
182636
182637
182638
182639
182640
182641
182642
182643
182644
182645
182646
182647
182648
182649
182650
182651
182652
182653
182654
182655
182656
182657
182658
182659
182660
182661
182662
182663
182664
182665
182666
182667
182668
182669
182670
182671
182672
182673
182674
182675
182676
182677
182678
182679
182680
182681
182682
182683
182684
182685
182686
182687
182688
182689
182690
182691
182692
182693
182694
182695
182696
182697
182698
182699
182700
182701
182702
182703
182704
182705
182706
182707
182708
182709
182710
182711
182712
182713
182714
182715
182716
182717
182718
182719
182720
182721
182722
182723
182724
182725
182726
182727
182728
182729
182730
182731
182732
182733
182734
182735
182736
182737
182738
182739
182740
182741
182742
182743
182744
182745
182746
182747
182748
182749
182750
182751
182752
182753
182754
182755
182756
182757
182758
182759
182760
182761
182762
182763
182764
182765
182766
182767
182768
182769
182770
182771
182772
182773
182774
182775
182776
182777
182778
182779
182780
182781
182782
182783
182784
182785
182786
182787
182788
182789
182790
182791
182792
182793
182794
182795
182796
182797
182798
182799
182800
182801
182802
182803
182804
182805
182806
182807
182808
182809
182810
182811
182812
182813
182814
182815
182816
182817
182818
182819
182820
182821
182822
182823
182824
182825
182826
182827
182828
182829
182830
182831
182832
182833
182834
182835
182836
182837
182838
182839
182840
182841
182842
182843
182844
182845
182846
182847
182848
182849
182850
182851
182852
182853
182854
182855
182856
182857
182858
182859
182860
182861
182862
182863
182864
182865
182866
182867
182868
182869
182870
182871
182872
182873
182874
182875
182876
182877
182878
182879
182880
182881
182882
182883
182884
182885
182886
182887
182888
182889
182890
182891
182892
182893
182894
182895
182896
182897
182898
182899
182900
182901
182902
182903
182904
182905
182906
182907
182908
182909
182910
182911
182912
182913
182914
182915
182916
182917
182918
182919
182920
182921
182922
182923
182924
182925
182926
182927
182928
182929
182930
182931
182932
182933
182934
182935
182936
182937
182938
182939
182940
182941
182942
182943
182944
182945
182946
182947
182948
182949
182950
182951
182952
182953
182954
182955
182956
182957
182958
182959
182960
182961
182962
182963
182964
182965
182966
182967
182968
182969
182970
182971
182972
182973
182974
182975
182976
182977
182978
182979
182980
182981
182982
182983
182984
182985
182986
182987
182988
182989
182990
182991
182992
182993
182994
182995
182996
182997
182998
182999
183000
183001
183002
183003
183004
183005
183006
183007
183008
183009
183010
183011
183012
183013
183014
183015
183016
183017
183018
183019
183020
183021
183022
183023
183024
183025
183026
183027
183028
183029
183030
183031
183032
183033
183034
183035
183036
183037
183038
183039
183040
183041
183042
183043
183044
183045
183046
183047
183048
183049
183050
183051
183052
183053
183054
183055
183056
183057
183058
183059
183060
183061
183062
183063
183064
183065
183066
183067
183068
183069
183070
183071
183072
183073
183074
183075
183076
183077
183078
183079
183080
183081
183082
183083
183084
183085
183086
183087
183088
183089
183090
183091
183092
183093
183094
183095
183096
183097
183098
183099
183100
183101
183102
183103
183104
183105
183106
183107
183108
183109
183110
183111
183112
183113
183114
183115
183116
183117
183118
183119
183120
183121
183122
183123
183124
183125
183126
183127
183128
183129
183130
183131
183132
183133
183134
183135
183136
183137
183138
183139
183140
183141
183142
183143
183144
183145
183146
183147
183148
183149
183150
183151
183152
183153
183154
183155
183156
183157
183158
183159
183160
183161
183162
183163
183164
183165
183166
183167
183168
183169
183170
183171
183172
183173
183174
183175
183176
183177
183178
183179
183180
183181
183182
183183
183184
183185
183186
183187
183188
183189
183190
183191
183192
183193
183194
183195
183196
183197
183198
183199
183200
183201
183202
183203
183204
183205
183206
183207
183208
183209
183210
183211
183212
183213
183214
183215
183216
183217
183218
183219
183220
183221
183222
183223
183224
183225
183226
183227
183228
183229
183230
183231
183232
183233
183234
183235
183236
183237
183238
183239
183240
183241
183242
183243
183244
183245
183246
183247
183248
183249
183250
183251
183252
183253
183254
183255
183256
183257
183258
183259
183260
183261
183262
183263
183264
183265
183266
183267
183268
183269
183270
183271
183272
183273
183274
183275
183276
183277
183278
183279
183280
183281
183282
183283
183284
183285
183286
183287
183288
183289
183290
183291
183292
183293
183294
183295
183296
183297
183298
183299
183300
183301
183302
183303
183304
183305
183306
183307
183308
183309
183310
183311
183312
183313
183314
183315
183316
183317
183318
183319
183320
183321
183322
183323
183324
183325
183326
183327
183328
183329
183330
183331
183332
183333
183334
183335
183336
183337
183338
183339
183340
183341
183342
183343
183344
183345
183346
183347
183348
183349
183350
183351
183352
183353
183354
183355
183356
183357
183358
183359
183360
183361
183362
183363
183364
183365
183366
183367
183368
183369
183370
183371
183372
183373
183374
183375
183376
183377
183378
183379
183380
183381
183382
183383
183384
183385
183386
183387
183388
183389
183390
183391
183392
183393
183394
183395
183396
183397
183398
183399
183400
183401
183402
183403
183404
183405
183406
183407
183408
183409
183410
183411
183412
183413
183414
183415
183416
183417
183418
183419
183420
183421
183422
183423
183424
183425
183426
183427
183428
183429
183430
183431
183432
183433
183434
183435
183436
183437
183438
183439
183440
183441
183442
183443
183444
183445
183446
183447
183448
183449
183450
183451
183452
183453
183454
183455
183456
183457
183458
183459
183460
183461
183462
183463
183464
183465
183466
183467
183468
183469
183470
183471
183472
183473
183474
183475
183476
183477
183478
183479
183480
183481
183482
183483
183484
183485
183486
183487
183488
183489
183490
183491
183492
183493
183494
183495
183496
183497
183498
183499
183500
183501
183502
183503
183504
183505
183506
183507
183508
183509
183510
183511
183512
183513
183514
183515
183516
183517
183518
183519
183520
183521
183522
183523
183524
183525
183526
183527
183528
183529
183530
183531
183532
183533
183534
183535
183536
183537
183538
183539
183540
183541
183542
183543
183544
183545
183546
183547
183548
183549
183550
183551
183552
183553
183554
183555
183556
183557
183558
183559
183560
183561
183562
183563
183564
183565
183566
183567
183568
183569
183570
183571
183572
183573
183574
183575
183576
183577
183578
183579
183580
183581
183582
183583
183584
183585
183586
183587
183588
183589
183590
183591
183592
183593
183594
183595
183596
183597
183598
183599
183600
183601
183602
183603
183604
183605
183606
183607
183608
183609
183610
183611
183612
183613
183614
183615
183616
183617
183618
183619
183620
183621
183622
183623
183624
183625
183626
183627
183628
183629
183630
183631
183632
183633
183634
183635
183636
183637
183638
183639
183640
183641
183642
183643
183644
183645
183646
183647
183648
183649
183650
183651
183652
183653
183654
183655
183656
183657
183658
183659
183660
183661
183662
183663
183664
183665
183666
183667
183668
183669
183670
183671
183672
183673
183674
183675
183676
183677
183678
183679
183680
183681
183682
183683
183684
183685
183686
183687
183688
183689
183690
183691
183692
183693
183694
183695
183696
183697
183698
183699
183700
183701
183702
183703
183704
183705
183706
183707
183708
183709
183710
183711
183712
183713
183714
183715
183716
183717
183718
183719
183720
183721
183722
183723
183724
183725
183726
183727
183728
183729
183730
183731
183732
183733
183734
183735
183736
183737
183738
183739
183740
183741
183742
183743
183744
183745
183746
183747
183748
183749
183750
183751
183752
183753
183754
183755
183756
183757
183758
183759
183760
183761
183762
183763
183764
183765
183766
183767
183768
183769
183770
183771
183772
183773
183774
183775
183776
183777
183778
183779
183780
183781
183782
183783
183784
183785
183786
183787
183788
183789
183790
183791
183792
183793
183794
183795
183796
183797
183798
183799
183800
183801
183802
183803
183804
183805
183806
183807
183808
183809
183810
183811
183812
183813
183814
183815
183816
183817
183818
183819
183820
183821
183822
183823
183824
183825
183826
183827
183828
183829
183830
183831
183832
183833
183834
183835
183836
183837
183838
183839
183840
183841
183842
183843
183844
183845
183846
183847
183848
183849
183850
183851
183852
183853
183854
183855
183856
183857
183858
183859
183860
183861
183862
183863
183864
183865
183866
183867
183868
183869
183870
183871
183872
183873
183874
183875
183876
183877
183878
183879
183880
183881
183882
183883
183884
183885
183886
183887
183888
183889
183890
183891
183892
183893
183894
183895
183896
183897
183898
183899
183900
183901
183902
183903
183904
183905
183906
183907
183908
183909
183910
183911
183912
183913
183914
183915
183916
183917
183918
183919
183920
183921
183922
183923
183924
183925
183926
183927
183928
183929
183930
183931
183932
183933
183934
183935
183936
183937
183938
183939
183940
183941
183942
183943
183944
183945
183946
183947
183948
183949
183950
183951
183952
183953
183954
183955
183956
183957
183958
183959
183960
183961
183962
183963
183964
183965
183966
183967
183968
183969
183970
183971
183972
183973
183974
183975
183976
183977
183978
183979
183980
183981
183982
183983
183984
183985
183986
183987
183988
183989
183990
183991
183992
183993
183994
183995
183996
183997
183998
183999
184000
184001
184002
184003
184004
184005
184006
184007
184008
184009
184010
184011
184012
184013
184014
184015
184016
184017
184018
184019
184020
184021
184022
184023
184024
184025
184026
184027
184028
184029
184030
184031
184032
184033
184034
184035
184036
184037
184038
184039
184040
184041
184042
184043
184044
184045
184046
184047
184048
184049
184050
184051
184052
184053
184054
184055
184056
184057
184058
184059
184060
184061
184062
184063
184064
184065
184066
184067
184068
184069
184070
184071
184072
184073
184074
184075
184076
184077
184078
184079
184080
184081
184082
184083
184084
184085
184086
184087
184088
184089
184090
184091
184092
184093
184094
184095
184096
184097
184098
184099
184100
184101
184102
184103
184104
184105
184106
184107
184108
184109
184110
184111
184112
184113
184114
184115
184116
184117
184118
184119
184120
184121
184122
184123
184124
184125
184126
184127
184128
184129
184130
184131
184132
184133
184134
184135
184136
184137
184138
184139
184140
184141
184142
184143
184144
184145
184146
184147
184148
184149
184150
184151
184152
184153
184154
184155
184156
184157
184158
184159
184160
184161
184162
184163
184164
184165
184166
184167
184168
184169
184170
184171
184172
184173
184174
184175
184176
184177
184178
184179
184180
184181
184182
184183
184184
184185
184186
184187
184188
184189
184190
184191
184192
184193
184194
184195
184196
184197
184198
184199
184200
184201
184202
184203
184204
184205
184206
184207
184208
184209
184210
184211
184212
184213
184214
184215
184216
184217
184218
184219
184220
184221
184222
184223
184224
184225
184226
184227
184228
184229
184230
184231
184232
184233
184234
184235
184236
184237
184238
184239
184240
184241
184242
184243
184244
184245
184246
184247
184248
184249
184250
184251
184252
184253
184254
184255
184256
184257
184258
184259
184260
184261
184262
184263
184264
184265
184266
184267
184268
184269
184270
184271
184272
184273
184274
184275
184276
184277
184278
184279
184280
184281
184282
184283
184284
184285
184286
184287
184288
184289
184290
184291
184292
184293
184294
184295
184296
184297
184298
184299
184300
184301
184302
184303
184304
184305
184306
184307
184308
184309
184310
184311
184312
184313
184314
184315
184316
184317
184318
184319
184320
184321
184322
184323
184324
184325
184326
184327
184328
184329
184330
184331
184332
184333
184334
184335
184336
184337
184338
184339
184340
184341
184342
184343
184344
184345
184346
184347
184348
184349
184350
184351
184352
184353
184354
184355
184356
184357
184358
184359
184360
184361
184362
184363
184364
184365
184366
184367
184368
184369
184370
184371
184372
184373
184374
184375
184376
184377
184378
184379
184380
184381
184382
184383
184384
184385
184386
184387
184388
184389
184390
184391
184392
184393
184394
184395
184396
184397
184398
184399
184400
184401
184402
184403
184404
184405
184406
184407
184408
184409
184410
184411
184412
184413
184414
184415
184416
184417
184418
184419
184420
184421
184422
184423
184424
184425
184426
184427
184428
184429
184430
184431
184432
184433
184434
184435
184436
184437
184438
184439
184440
184441
184442
184443
184444
184445
184446
184447
184448
184449
184450
184451
184452
184453
184454
184455
184456
184457
184458
184459
184460
184461
184462
184463
184464
184465
184466
184467
184468
184469
184470
184471
184472
184473
184474
184475
184476
184477
184478
184479
184480
184481
184482
184483
184484
184485
184486
184487
184488
184489
184490
184491
184492
184493
184494
184495
184496
184497
184498
184499
184500
184501
184502
184503
184504
184505
184506
184507
184508
184509
184510
184511
184512
184513
184514
184515
184516
184517
184518
184519
184520
184521
184522
184523
184524
184525
184526
184527
184528
184529
184530
184531
184532
184533
184534
184535
184536
184537
184538
184539
184540
184541
184542
184543
184544
184545
184546
184547
184548
184549
184550
184551
184552
184553
184554
184555
184556
184557
184558
184559
184560
184561
184562
184563
184564
184565
184566
184567
184568
184569
184570
184571
184572
184573
184574
184575
184576
184577
184578
184579
184580
184581
184582
184583
184584
184585
184586
184587
184588
184589
184590
184591
184592
184593
184594
184595
184596
184597
184598
184599
184600
184601
184602
184603
184604
184605
184606
184607
184608
184609
184610
184611
184612
184613
184614
184615
184616
184617
184618
184619
184620
184621
184622
184623
184624
184625
184626
184627
184628
184629
184630
184631
184632
184633
184634
184635
184636
184637
184638
184639
184640
184641
184642
184643
184644
184645
184646
184647
184648
184649
184650
184651
184652
184653
184654
184655
184656
184657
184658
184659
184660
184661
184662
184663
184664
184665
184666
184667
184668
184669
184670
184671
184672
184673
184674
184675
184676
184677
184678
184679
184680
184681
184682
184683
184684
184685
184686
184687
184688
184689
184690
184691
184692
184693
184694
184695
184696
184697
184698
184699
184700
184701
184702
184703
184704
184705
184706
184707
184708
184709
184710
184711
184712
184713
184714
184715
184716
184717
184718
184719
184720
184721
184722
184723
184724
184725
184726
184727
184728
184729
184730
184731
184732
184733
184734
184735
184736
184737
184738
184739
184740
184741
184742
184743
184744
184745
184746
184747
184748
184749
184750
184751
184752
184753
184754
184755
184756
184757
184758
184759
184760
184761
184762
184763
184764
184765
184766
184767
184768
184769
184770
184771
184772
184773
184774
184775
184776
184777
184778
184779
184780
184781
184782
184783
184784
184785
184786
184787
184788
184789
184790
184791
184792
184793
184794
184795
184796
184797
184798
184799
184800
184801
184802
184803
184804
184805
184806
184807
184808
184809
184810
184811
184812
184813
184814
184815
184816
184817
184818
184819
184820
184821
184822
184823
184824
184825
184826
184827
184828
184829
184830
184831
184832
184833
184834
184835
184836
184837
184838
184839
184840
184841
184842
184843
184844
184845
184846
184847
184848
184849
184850
184851
184852
184853
184854
184855
184856
184857
184858
184859
184860
184861
184862
184863
184864
184865
184866
184867
184868
184869
184870
184871
184872
184873
184874
184875
184876
184877
184878
184879
184880
184881
184882
184883
184884
184885
184886
184887
184888
184889
184890
184891
184892
184893
184894
184895
184896
184897
184898
184899
184900
184901
184902
184903
184904
184905
184906
184907
184908
184909
184910
184911
184912
184913
184914
184915
184916
184917
184918
184919
184920
184921
184922
184923
184924
184925
184926
184927
184928
184929
184930
184931
184932
184933
184934
184935
184936
184937
184938
184939
184940
184941
184942
184943
184944
184945
184946
184947
184948
184949
184950
184951
184952
184953
184954
184955
184956
184957
184958
184959
184960
184961
184962
184963
184964
184965
184966
184967
184968
184969
184970
184971
184972
184973
184974
184975
184976
184977
184978
184979
184980
184981
184982
184983
184984
184985
184986
184987
184988
184989
184990
184991
184992
184993
184994
184995
184996
184997
184998
184999
185000
185001
185002
185003
185004
185005
185006
185007
185008
185009
185010
185011
185012
185013
185014
185015
185016
185017
185018
185019
185020
185021
185022
185023
185024
185025
185026
185027
185028
185029
185030
185031
185032
185033
185034
185035
185036
185037
185038
185039
185040
185041
185042
185043
185044
185045
185046
185047
185048
185049
185050
185051
185052
185053
185054
185055
185056
185057
185058
185059
185060
185061
185062
185063
185064
185065
185066
185067
185068
185069
185070
185071
185072
185073
185074
185075
185076
185077
185078
185079
185080
185081
185082
185083
185084
185085
185086
185087
185088
185089
185090
185091
185092
185093
185094
185095
185096
185097
185098
185099
185100
185101
185102
185103
185104
185105
185106
185107
185108
185109
185110
185111
185112
185113
185114
185115
185116
185117
185118
185119
185120
185121
185122
185123
185124
185125
185126
185127
185128
185129
185130
185131
185132
185133
185134
185135
185136
185137
185138
185139
185140
185141
185142
185143
185144
185145
185146
185147
185148
185149
185150
185151
185152
185153
185154
185155
185156
185157
185158
185159
185160
185161
185162
185163
185164
185165
185166
185167
185168
185169
185170
185171
185172
185173
185174
185175
185176
185177
185178
185179
185180
185181
185182
185183
185184
185185
185186
185187
185188
185189
185190
185191
185192
185193
185194
185195
185196
185197
185198
185199
185200
185201
185202
185203
185204
185205
185206
185207
185208
185209
185210
185211
185212
185213
185214
185215
185216
185217
185218
185219
185220
185221
185222
185223
185224
185225
185226
185227
185228
185229
185230
185231
185232
185233
185234
185235
185236
185237
185238
185239
185240
185241
185242
185243
185244
185245
185246
185247
185248
185249
185250
185251
185252
185253
185254
185255
185256
185257
185258
185259
185260
185261
185262
185263
185264
185265
185266
185267
185268
185269
185270
185271
185272
185273
185274
185275
185276
185277
185278
185279
185280
185281
185282
185283
185284
185285
185286
185287
185288
185289
185290
185291
185292
185293
185294
185295
185296
185297
185298
185299
185300
185301
185302
185303
185304
185305
185306
185307
185308
185309
185310
185311
185312
185313
185314
185315
185316
185317
185318
185319
185320
185321
185322
185323
185324
185325
185326
185327
185328
185329
185330
185331
185332
185333
185334
185335
185336
185337
185338
185339
185340
185341
185342
185343
185344
185345
185346
185347
185348
185349
185350
185351
185352
185353
185354
185355
185356
185357
185358
185359
185360
185361
185362
185363
185364
185365
185366
185367
185368
185369
185370
185371
185372
185373
185374
185375
185376
185377
185378
185379
185380
185381
185382
185383
185384
185385
185386
185387
185388
185389
185390
185391
185392
185393
185394
185395
185396
185397
185398
185399
185400
185401
185402
185403
185404
185405
185406
185407
185408
185409
185410
185411
185412
185413
185414
185415
185416
185417
185418
185419
185420
185421
185422
185423
185424
185425
185426
185427
185428
185429
185430
185431
185432
185433
185434
185435
185436
185437
185438
185439
185440
185441
185442
185443
185444
185445
185446
185447
185448
185449
185450
185451
185452
185453
185454
185455
185456
185457
185458
185459
185460
185461
185462
185463
185464
185465
185466
185467
185468
185469
185470
185471
185472
185473
185474
185475
185476
185477
185478
185479
185480
185481
185482
185483
185484
185485
185486
185487
185488
185489
185490
185491
185492
185493
185494
185495
185496
185497
185498
185499
185500
185501
185502
185503
185504
185505
185506
185507
185508
185509
185510
185511
185512
185513
185514
185515
185516
185517
185518
185519
185520
185521
185522
185523
185524
185525
185526
185527
185528
185529
185530
185531
185532
185533
185534
185535
185536
185537
185538
185539
185540
185541
185542
185543
185544
185545
185546
185547
185548
185549
185550
185551
185552
185553
185554
185555
185556
185557
185558
185559
185560
185561
185562
185563
185564
185565
185566
185567
185568
185569
185570
185571
185572
185573
185574
185575
185576
185577
185578
185579
185580
185581
185582
185583
185584
185585
185586
185587
185588
185589
185590
185591
185592
185593
185594
185595
185596
185597
185598
185599
185600
185601
185602
185603
185604
185605
185606
185607
185608
185609
185610
185611
185612
185613
185614
185615
185616
185617
185618
185619
185620
185621
185622
185623
185624
185625
185626
185627
185628
185629
185630
185631
185632
185633
185634
185635
185636
185637
185638
185639
185640
185641
185642
185643
185644
185645
185646
185647
185648
185649
185650
185651
185652
185653
185654
185655
185656
185657
185658
185659
185660
185661
185662
185663
185664
185665
185666
185667
185668
185669
185670
185671
185672
185673
185674
185675
185676
185677
185678
185679
185680
185681
185682
185683
185684
185685
185686
185687
185688
185689
185690
185691
185692
185693
185694
185695
185696
185697
185698
185699
185700
185701
185702
185703
185704
185705
185706
185707
185708
185709
185710
185711
185712
185713
185714
185715
185716
185717
185718
185719
185720
185721
185722
185723
185724
185725
185726
185727
185728
185729
185730
185731
185732
185733
185734
185735
185736
185737
185738
185739
185740
185741
185742
185743
185744
185745
185746
185747
185748
185749
185750
185751
185752
185753
185754
185755
185756
185757
185758
185759
185760
185761
185762
185763
185764
185765
185766
185767
185768
185769
185770
185771
185772
185773
185774
185775
185776
185777
185778
185779
185780
185781
185782
185783
185784
185785
185786
185787
185788
185789
185790
185791
185792
185793
185794
185795
185796
185797
185798
185799
185800
185801
185802
185803
185804
185805
185806
185807
185808
185809
185810
185811
185812
185813
185814
185815
185816
185817
185818
185819
185820
185821
185822
185823
185824
185825
185826
185827
185828
185829
185830
185831
185832
185833
185834
185835
185836
185837
185838
185839
185840
185841
185842
185843
185844
185845
185846
185847
185848
185849
185850
185851
185852
185853
185854
185855
185856
185857
185858
185859
185860
185861
185862
185863
185864
185865
185866
185867
185868
185869
185870
185871
185872
185873
185874
185875
185876
185877
185878
185879
185880
185881
185882
185883
185884
185885
185886
185887
185888
185889
185890
185891
185892
185893
185894
185895
185896
185897
185898
185899
185900
185901
185902
185903
185904
185905
185906
185907
185908
185909
185910
185911
185912
185913
185914
185915
185916
185917
185918
185919
185920
185921
185922
185923
185924
185925
185926
185927
185928
185929
185930
185931
185932
185933
185934
185935
185936
185937
185938
185939
185940
185941
185942
185943
185944
185945
185946
185947
185948
185949
185950
185951
185952
185953
185954
185955
185956
185957
185958
185959
185960
185961
185962
185963
185964
185965
185966
185967
185968
185969
185970
185971
185972
185973
185974
185975
185976
185977
185978
185979
185980
185981
185982
185983
185984
185985
185986
185987
185988
185989
185990
185991
185992
185993
185994
185995
185996
185997
185998
185999
186000
186001
186002
186003
186004
186005
186006
186007
186008
186009
186010
186011
186012
186013
186014
186015
186016
186017
186018
186019
186020
186021
186022
186023
186024
186025
186026
186027
186028
186029
186030
186031
186032
186033
186034
186035
186036
186037
186038
186039
186040
186041
186042
186043
186044
186045
186046
186047
186048
186049
186050
186051
186052
186053
186054
186055
186056
186057
186058
186059
186060
186061
186062
186063
186064
186065
186066
186067
186068
186069
186070
186071
186072
186073
186074
186075
186076
186077
186078
186079
186080
186081
186082
186083
186084
186085
186086
186087
186088
186089
186090
186091
186092
186093
186094
186095
186096
186097
186098
186099
186100
186101
186102
186103
186104
186105
186106
186107
186108
186109
186110
186111
186112
186113
186114
186115
186116
186117
186118
186119
186120
186121
186122
186123
186124
186125
186126
186127
186128
186129
186130
186131
186132
186133
186134
186135
186136
186137
186138
186139
186140
186141
186142
186143
186144
186145
186146
186147
186148
186149
186150
186151
186152
186153
186154
186155
186156
186157
186158
186159
186160
186161
186162
186163
186164
186165
186166
186167
186168
186169
186170
186171
186172
186173
186174
186175
186176
186177
186178
186179
186180
186181
186182
186183
186184
186185
186186
186187
186188
186189
186190
186191
186192
186193
186194
186195
186196
186197
186198
186199
186200
186201
186202
186203
186204
186205
186206
186207
186208
186209
186210
186211
186212
186213
186214
186215
186216
186217
186218
186219
186220
186221
186222
186223
186224
186225
186226
186227
186228
186229
186230
186231
186232
186233
186234
186235
186236
186237
186238
186239
186240
186241
186242
186243
186244
186245
186246
186247
186248
186249
186250
186251
186252
186253
186254
186255
186256
186257
186258
186259
186260
186261
186262
186263
186264
186265
186266
186267
186268
186269
186270
186271
186272
186273
186274
186275
186276
186277
186278
186279
186280
186281
186282
186283
186284
186285
186286
186287
186288
186289
186290
186291
186292
186293
186294
186295
186296
186297
186298
186299
186300
186301
186302
186303
186304
186305
186306
186307
186308
186309
186310
186311
186312
186313
186314
186315
186316
186317
186318
186319
186320
186321
186322
186323
186324
186325
186326
186327
186328
186329
186330
186331
186332
186333
186334
186335
186336
186337
186338
186339
186340
186341
186342
186343
186344
186345
186346
186347
186348
186349
186350
186351
186352
186353
186354
186355
186356
186357
186358
186359
186360
186361
186362
186363
186364
186365
186366
186367
186368
186369
186370
186371
186372
186373
186374
186375
186376
186377
186378
186379
186380
186381
186382
186383
186384
186385
186386
186387
186388
186389
186390
186391
186392
186393
186394
186395
186396
186397
186398
186399
186400
186401
186402
186403
186404
186405
186406
186407
186408
186409
186410
186411
186412
186413
186414
186415
186416
186417
186418
186419
186420
186421
186422
186423
186424
186425
186426
186427
186428
186429
186430
186431
186432
186433
186434
186435
186436
186437
186438
186439
186440
186441
186442
186443
186444
186445
186446
186447
186448
186449
186450
186451
186452
186453
186454
186455
186456
186457
186458
186459
186460
186461
186462
186463
186464
186465
186466
186467
186468
186469
186470
186471
186472
186473
186474
186475
186476
186477
186478
186479
186480
186481
186482
186483
186484
186485
186486
186487
186488
186489
186490
186491
186492
186493
186494
186495
186496
186497
186498
186499
186500
186501
186502
186503
186504
186505
186506
186507
186508
186509
186510
186511
186512
186513
186514
186515
186516
186517
186518
186519
186520
186521
186522
186523
186524
186525
186526
186527
186528
186529
186530
186531
186532
186533
186534
186535
186536
186537
186538
186539
186540
186541
186542
186543
186544
186545
186546
186547
186548
186549
186550
186551
186552
186553
186554
186555
186556
186557
186558
186559
186560
186561
186562
186563
186564
186565
186566
186567
186568
186569
186570
186571
186572
186573
186574
186575
186576
186577
186578
186579
186580
186581
186582
186583
186584
186585
186586
186587
186588
186589
186590
186591
186592
186593
186594
186595
186596
186597
186598
186599
186600
186601
186602
186603
186604
186605
186606
186607
186608
186609
186610
186611
186612
186613
186614
186615
186616
186617
186618
186619
186620
186621
186622
186623
186624
186625
186626
186627
186628
186629
186630
186631
186632
186633
186634
186635
186636
186637
186638
186639
186640
186641
186642
186643
186644
186645
186646
186647
186648
186649
186650
186651
186652
186653
186654
186655
186656
186657
186658
186659
186660
186661
186662
186663
186664
186665
186666
186667
186668
186669
186670
186671
186672
186673
186674
186675
186676
186677
186678
186679
186680
186681
186682
186683
186684
186685
186686
186687
186688
186689
186690
186691
186692
186693
186694
186695
186696
186697
186698
186699
186700
186701
186702
186703
186704
186705
186706
186707
186708
186709
186710
186711
186712
186713
186714
186715
186716
186717
186718
186719
186720
186721
186722
186723
186724
186725
186726
186727
186728
186729
186730
186731
186732
186733
186734
186735
186736
186737
186738
186739
186740
186741
186742
186743
186744
186745
186746
186747
186748
186749
186750
186751
186752
186753
186754
186755
186756
186757
186758
186759
186760
186761
186762
186763
186764
186765
186766
186767
186768
186769
186770
186771
186772
186773
186774
186775
186776
186777
186778
186779
186780
186781
186782
186783
186784
186785
186786
186787
186788
186789
186790
186791
186792
186793
186794
186795
186796
186797
186798
186799
186800
186801
186802
186803
186804
186805
186806
186807
186808
186809
186810
186811
186812
186813
186814
186815
186816
186817
186818
186819
186820
186821
186822
186823
186824
186825
186826
186827
186828
186829
186830
186831
186832
186833
186834
186835
186836
186837
186838
186839
186840
186841
186842
186843
186844
186845
186846
186847
186848
186849
186850
186851
186852
186853
186854
186855
186856
186857
186858
186859
186860
186861
186862
186863
186864
186865
186866
186867
186868
186869
186870
186871
186872
186873
186874
186875
186876
186877
186878
186879
186880
186881
186882
186883
186884
186885
186886
186887
186888
186889
186890
186891
186892
186893
186894
186895
186896
186897
186898
186899
186900
186901
186902
186903
186904
186905
186906
186907
186908
186909
186910
186911
186912
186913
186914
186915
186916
186917
186918
186919
186920
186921
186922
186923
186924
186925
186926
186927
186928
186929
186930
186931
186932
186933
186934
186935
186936
186937
186938
186939
186940
186941
186942
186943
186944
186945
186946
186947
186948
186949
186950
186951
186952
186953
186954
186955
186956
186957
186958
186959
186960
186961
186962
186963
186964
186965
186966
186967
186968
186969
186970
186971
186972
186973
186974
186975
186976
186977
186978
186979
186980
186981
186982
186983
186984
186985
186986
186987
186988
186989
186990
186991
186992
186993
186994
186995
186996
186997
186998
186999
187000
187001
187002
187003
187004
187005
187006
187007
187008
187009
187010
187011
187012
187013
187014
187015
187016
187017
187018
187019
187020
187021
187022
187023
187024
187025
187026
187027
187028
187029
187030
187031
187032
187033
187034
187035
187036
187037
187038
187039
187040
187041
187042
187043
187044
187045
187046
187047
187048
187049
187050
187051
187052
187053
187054
187055
187056
187057
187058
187059
187060
187061
187062
187063
187064
187065
187066
187067
187068
187069
187070
187071
187072
187073
187074
187075
187076
187077
187078
187079
187080
187081
187082
187083
187084
187085
187086
187087
187088
187089
187090
187091
187092
187093
187094
187095
187096
187097
187098
187099
187100
187101
187102
187103
187104
187105
187106
187107
187108
187109
187110
187111
187112
187113
187114
187115
187116
187117
187118
187119
187120
187121
187122
187123
187124
187125
187126
187127
187128
187129
187130
187131
187132
187133
187134
187135
187136
187137
187138
187139
187140
187141
187142
187143
187144
187145
187146
187147
187148
187149
187150
187151
187152
187153
187154
187155
187156
187157
187158
187159
187160
187161
187162
187163
187164
187165
187166
187167
187168
187169
187170
187171
187172
187173
187174
187175
187176
187177
187178
187179
187180
187181
187182
187183
187184
187185
187186
187187
187188
187189
187190
187191
187192
187193
187194
187195
187196
187197
187198
187199
187200
187201
187202
187203
187204
187205
187206
187207
187208
187209
187210
187211
187212
187213
187214
187215
187216
187217
187218
187219
187220
187221
187222
187223
187224
187225
187226
187227
187228
187229
187230
187231
187232
187233
187234
187235
187236
187237
187238
187239
187240
187241
187242
187243
187244
187245
187246
187247
187248
187249
187250
187251
187252
187253
187254
187255
187256
187257
187258
187259
187260
187261
187262
187263
187264
187265
187266
187267
187268
187269
187270
187271
187272
187273
187274
187275
187276
187277
187278
187279
187280
187281
187282
187283
187284
187285
187286
187287
187288
187289
187290
187291
187292
187293
187294
187295
187296
187297
187298
187299
187300
187301
187302
187303
187304
187305
187306
187307
187308
187309
187310
187311
187312
187313
187314
187315
187316
187317
187318
187319
187320
187321
187322
187323
187324
187325
187326
187327
187328
187329
187330
187331
187332
187333
187334
187335
187336
187337
187338
187339
187340
187341
187342
187343
187344
187345
187346
187347
187348
187349
187350
187351
187352
187353
187354
187355
187356
187357
187358
187359
187360
187361
187362
187363
187364
187365
187366
187367
187368
187369
187370
187371
187372
187373
187374
187375
187376
187377
187378
187379
187380
187381
187382
187383
187384
187385
187386
187387
187388
187389
187390
187391
187392
187393
187394
187395
187396
187397
187398
187399
187400
187401
187402
187403
187404
187405
187406
187407
187408
187409
187410
187411
187412
187413
187414
187415
187416
187417
187418
187419
187420
187421
187422
187423
187424
187425
187426
187427
187428
187429
187430
187431
187432
187433
187434
187435
187436
187437
187438
187439
187440
187441
187442
187443
187444
187445
187446
187447
187448
187449
187450
187451
187452
187453
187454
187455
187456
187457
187458
187459
187460
187461
187462
187463
187464
187465
187466
187467
187468
187469
187470
187471
187472
187473
187474
187475
187476
187477
187478
187479
187480
187481
187482
187483
187484
187485
187486
187487
187488
187489
187490
187491
187492
187493
187494
187495
187496
187497
187498
187499
187500
187501
187502
187503
187504
187505
187506
187507
187508
187509
187510
187511
187512
187513
187514
187515
187516
187517
187518
187519
187520
187521
187522
187523
187524
187525
187526
187527
187528
187529
187530
187531
187532
187533
187534
187535
187536
187537
187538
187539
187540
187541
187542
187543
187544
187545
187546
187547
187548
187549
187550
187551
187552
187553
187554
187555
187556
187557
187558
187559
187560
187561
187562
187563
187564
187565
187566
187567
187568
187569
187570
187571
187572
187573
187574
187575
187576
187577
187578
187579
187580
187581
187582
187583
187584
187585
187586
187587
187588
187589
187590
187591
187592
187593
187594
187595
187596
187597
187598
187599
187600
187601
187602
187603
187604
187605
187606
187607
187608
187609
187610
187611
187612
187613
187614
187615
187616
187617
187618
187619
187620
187621
187622
187623
187624
187625
187626
187627
187628
187629
187630
187631
187632
187633
187634
187635
187636
187637
187638
187639
187640
187641
187642
187643
187644
187645
187646
187647
187648
187649
187650
187651
187652
187653
187654
187655
187656
187657
187658
187659
187660
187661
187662
187663
187664
187665
187666
187667
187668
187669
187670
187671
187672
187673
187674
187675
187676
187677
187678
187679
187680
187681
187682
187683
187684
187685
187686
187687
187688
187689
187690
187691
187692
187693
187694
187695
187696
187697
187698
187699
187700
187701
187702
187703
187704
187705
187706
187707
187708
187709
187710
187711
187712
187713
187714
187715
187716
187717
187718
187719
187720
187721
187722
187723
187724
187725
187726
187727
187728
187729
187730
187731
187732
187733
187734
187735
187736
187737
187738
187739
187740
187741
187742
187743
187744
187745
187746
187747
187748
187749
187750
187751
187752
187753
187754
187755
187756
187757
187758
187759
187760
187761
187762
187763
187764
187765
187766
187767
187768
187769
187770
187771
187772
187773
187774
187775
187776
187777
187778
187779
187780
187781
187782
187783
187784
187785
187786
187787
187788
187789
187790
187791
187792
187793
187794
187795
187796
187797
187798
187799
187800
187801
187802
187803
187804
187805
187806
187807
187808
187809
187810
187811
187812
187813
187814
187815
187816
187817
187818
187819
187820
187821
187822
187823
187824
187825
187826
187827
187828
187829
187830
187831
187832
187833
187834
187835
187836
187837
187838
187839
187840
187841
187842
187843
187844
187845
187846
187847
187848
187849
187850
187851
187852
187853
187854
187855
187856
187857
187858
187859
187860
187861
187862
187863
187864
187865
187866
187867
187868
187869
187870
187871
187872
187873
187874
187875
187876
187877
187878
187879
187880
187881
187882
187883
187884
187885
187886
187887
187888
187889
187890
187891
187892
187893
187894
187895
187896
187897
187898
187899
187900
187901
187902
187903
187904
187905
187906
187907
187908
187909
187910
187911
187912
187913
187914
187915
187916
187917
187918
187919
187920
187921
187922
187923
187924
187925
187926
187927
187928
187929
187930
187931
187932
187933
187934
187935
187936
187937
187938
187939
187940
187941
187942
187943
187944
187945
187946
187947
187948
187949
187950
187951
187952
187953
187954
187955
187956
187957
187958
187959
187960
187961
187962
187963
187964
187965
187966
187967
187968
187969
187970
187971
187972
187973
187974
187975
187976
187977
187978
187979
187980
187981
187982
187983
187984
187985
187986
187987
187988
187989
187990
187991
187992
187993
187994
187995
187996
187997
187998
187999
188000
188001
188002
188003
188004
188005
188006
188007
188008
188009
188010
188011
188012
188013
188014
188015
188016
188017
188018
188019
188020
188021
188022
188023
188024
188025
188026
188027
188028
188029
188030
188031
188032
188033
188034
188035
188036
188037
188038
188039
188040
188041
188042
188043
188044
188045
188046
188047
188048
188049
188050
188051
188052
188053
188054
188055
188056
188057
188058
188059
188060
188061
188062
188063
188064
188065
188066
188067
188068
188069
188070
188071
188072
188073
188074
188075
188076
188077
188078
188079
188080
188081
188082
188083
188084
188085
188086
188087
188088
188089
188090
188091
188092
188093
188094
188095
188096
188097
188098
188099
188100
188101
188102
188103
188104
188105
188106
188107
188108
188109
188110
188111
188112
188113
188114
188115
188116
188117
188118
188119
188120
188121
188122
188123
188124
188125
188126
188127
188128
188129
188130
188131
188132
188133
188134
188135
188136
188137
188138
188139
188140
188141
188142
188143
188144
188145
188146
188147
188148
188149
188150
188151
188152
188153
188154
188155
188156
188157
188158
188159
188160
188161
188162
188163
188164
188165
188166
188167
188168
188169
188170
188171
188172
188173
188174
188175
188176
188177
188178
188179
188180
188181
188182
188183
188184
188185
188186
188187
188188
188189
188190
188191
188192
188193
188194
188195
188196
188197
188198
188199
188200
188201
188202
188203
188204
188205
188206
188207
188208
188209
188210
188211
188212
188213
188214
188215
188216
188217
188218
188219
188220
188221
188222
188223
188224
188225
188226
188227
188228
188229
188230
188231
188232
188233
188234
188235
188236
188237
188238
188239
188240
188241
188242
188243
188244
188245
188246
188247
188248
188249
188250
188251
188252
188253
188254
188255
188256
188257
188258
188259
188260
188261
188262
188263
188264
188265
188266
188267
188268
188269
188270
188271
188272
188273
188274
188275
188276
188277
188278
188279
188280
188281
188282
188283
188284
188285
188286
188287
188288
188289
188290
188291
188292
188293
188294
188295
188296
188297
188298
188299
188300
188301
188302
188303
188304
188305
188306
188307
188308
188309
188310
188311
188312
188313
188314
188315
188316
188317
188318
188319
188320
188321
188322
188323
188324
188325
188326
188327
188328
188329
188330
188331
188332
188333
188334
188335
188336
188337
188338
188339
188340
188341
188342
188343
188344
188345
188346
188347
188348
188349
188350
188351
188352
188353
188354
188355
188356
188357
188358
188359
188360
188361
188362
188363
188364
188365
188366
188367
188368
188369
188370
188371
188372
188373
188374
188375
188376
188377
188378
188379
188380
188381
188382
188383
188384
188385
188386
188387
188388
188389
188390
188391
188392
188393
188394
188395
188396
188397
188398
188399
188400
188401
188402
188403
188404
188405
188406
188407
188408
188409
188410
188411
188412
188413
188414
188415
188416
188417
188418
188419
188420
188421
188422
188423
188424
188425
188426
188427
188428
188429
188430
188431
188432
188433
188434
188435
188436
188437
188438
188439
188440
188441
188442
188443
188444
188445
188446
188447
188448
188449
188450
188451
188452
188453
188454
188455
188456
188457
188458
188459
188460
188461
188462
188463
188464
188465
188466
188467
188468
188469
188470
188471
188472
188473
188474
188475
188476
188477
188478
188479
188480
188481
188482
188483
188484
188485
188486
188487
188488
188489
188490
188491
188492
188493
188494
188495
188496
188497
188498
188499
188500
188501
188502
188503
188504
188505
188506
188507
188508
188509
188510
188511
188512
188513
188514
188515
188516
188517
188518
188519
188520
188521
188522
188523
188524
188525
188526
188527
188528
188529
188530
188531
188532
188533
188534
188535
188536
188537
188538
188539
188540
188541
188542
188543
188544
188545
188546
188547
188548
188549
188550
188551
188552
188553
188554
188555
188556
188557
188558
188559
188560
188561
188562
188563
188564
188565
188566
188567
188568
188569
188570
188571
188572
188573
188574
188575
188576
188577
188578
188579
188580
188581
188582
188583
188584
188585
188586
188587
188588
188589
188590
188591
188592
188593
188594
188595
188596
188597
188598
188599
188600
188601
188602
188603
188604
188605
188606
188607
188608
188609
188610
188611
188612
188613
188614
188615
188616
188617
188618
188619
188620
188621
188622
188623
188624
188625
188626
188627
188628
188629
188630
188631
188632
188633
188634
188635
188636
188637
188638
188639
188640
188641
188642
188643
188644
188645
188646
188647
188648
188649
188650
188651
188652
188653
188654
188655
188656
188657
188658
188659
188660
188661
188662
188663
188664
188665
188666
188667
188668
188669
188670
188671
188672
188673
188674
188675
188676
188677
188678
188679
188680
188681
188682
188683
188684
188685
188686
188687
188688
188689
188690
188691
188692
188693
188694
188695
188696
188697
188698
188699
188700
188701
188702
188703
188704
188705
188706
188707
188708
188709
188710
188711
188712
188713
188714
188715
188716
188717
188718
188719
188720
188721
188722
188723
188724
188725
188726
188727
188728
188729
188730
188731
188732
188733
188734
188735
188736
188737
188738
188739
188740
188741
188742
188743
188744
188745
188746
188747
188748
188749
188750
188751
188752
188753
188754
188755
188756
188757
188758
188759
188760
188761
188762
188763
188764
188765
188766
188767
188768
188769
188770
188771
188772
188773
188774
188775
188776
188777
188778
188779
188780
188781
188782
188783
188784
188785
188786
188787
188788
188789
188790
188791
188792
188793
188794
188795
188796
188797
188798
188799
188800
188801
188802
188803
188804
188805
188806
188807
188808
188809
188810
188811
188812
188813
188814
188815
188816
188817
188818
188819
188820
188821
188822
188823
188824
188825
188826
188827
188828
188829
188830
188831
188832
188833
188834
188835
188836
188837
188838
188839
188840
188841
188842
188843
188844
188845
188846
188847
188848
188849
188850
188851
188852
188853
188854
188855
188856
188857
188858
188859
188860
188861
188862
188863
188864
188865
188866
188867
188868
188869
188870
188871
188872
188873
188874
188875
188876
188877
188878
188879
188880
188881
188882
188883
188884
188885
188886
188887
188888
188889
188890
188891
188892
188893
188894
188895
188896
188897
188898
188899
188900
188901
188902
188903
188904
188905
188906
188907
188908
188909
188910
188911
188912
188913
188914
188915
188916
188917
188918
188919
188920
188921
188922
188923
188924
188925
188926
188927
188928
188929
188930
188931
188932
188933
188934
188935
188936
188937
188938
188939
188940
188941
188942
188943
188944
188945
188946
188947
188948
188949
188950
188951
188952
188953
188954
188955
188956
188957
188958
188959
188960
188961
188962
188963
188964
188965
188966
188967
188968
188969
188970
188971
188972
188973
188974
188975
188976
188977
188978
188979
188980
188981
188982
188983
188984
188985
188986
188987
188988
188989
188990
188991
188992
188993
188994
188995
188996
188997
188998
188999
189000
189001
189002
189003
189004
189005
189006
189007
189008
189009
189010
189011
189012
189013
189014
189015
189016
189017
189018
189019
189020
189021
189022
189023
189024
189025
189026
189027
189028
189029
189030
189031
189032
189033
189034
189035
189036
189037
189038
189039
189040
189041
189042
189043
189044
189045
189046
189047
189048
189049
189050
189051
189052
189053
189054
189055
189056
189057
189058
189059
189060
189061
189062
189063
189064
189065
189066
189067
189068
189069
189070
189071
189072
189073
189074
189075
189076
189077
189078
189079
189080
189081
189082
189083
189084
189085
189086
189087
189088
189089
189090
189091
189092
189093
189094
189095
189096
189097
189098
189099
189100
189101
189102
189103
189104
189105
189106
189107
189108
189109
189110
189111
189112
189113
189114
189115
189116
189117
189118
189119
189120
189121
189122
189123
189124
189125
189126
189127
189128
189129
189130
189131
189132
189133
189134
189135
189136
189137
189138
189139
189140
189141
189142
189143
189144
189145
189146
189147
189148
189149
189150
189151
189152
189153
189154
189155
189156
189157
189158
189159
189160
189161
189162
189163
189164
189165
189166
189167
189168
189169
189170
189171
189172
189173
189174
189175
189176
189177
189178
189179
189180
189181
189182
189183
189184
189185
189186
189187
189188
189189
189190
189191
189192
189193
189194
189195
189196
189197
189198
189199
189200
189201
189202
189203
189204
189205
189206
189207
189208
189209
189210
189211
189212
189213
189214
189215
189216
189217
189218
189219
189220
189221
189222
189223
189224
189225
189226
189227
189228
189229
189230
189231
189232
189233
189234
189235
189236
189237
189238
189239
189240
189241
189242
189243
189244
189245
189246
189247
189248
189249
189250
189251
189252
189253
189254
189255
189256
189257
189258
189259
189260
189261
189262
189263
189264
189265
189266
189267
189268
189269
189270
189271
189272
189273
189274
189275
189276
189277
189278
189279
189280
189281
189282
189283
189284
189285
189286
189287
189288
189289
189290
189291
189292
189293
189294
189295
189296
189297
189298
189299
189300
189301
189302
189303
189304
189305
189306
189307
189308
189309
189310
189311
189312
189313
189314
189315
189316
189317
189318
189319
189320
189321
189322
189323
189324
189325
189326
189327
189328
189329
189330
189331
189332
189333
189334
189335
189336
189337
189338
189339
189340
189341
189342
189343
189344
189345
189346
189347
189348
189349
189350
189351
189352
189353
189354
189355
189356
189357
189358
189359
189360
189361
189362
189363
189364
189365
189366
189367
189368
189369
189370
189371
189372
189373
189374
189375
189376
189377
189378
189379
189380
189381
189382
189383
189384
189385
189386
189387
189388
189389
189390
189391
189392
189393
189394
189395
189396
189397
189398
189399
189400
189401
189402
189403
189404
189405
189406
189407
189408
189409
189410
189411
189412
189413
189414
189415
189416
189417
189418
189419
189420
189421
189422
189423
189424
189425
189426
189427
189428
189429
189430
189431
189432
189433
189434
189435
189436
189437
189438
189439
189440
189441
189442
189443
189444
189445
189446
189447
189448
189449
189450
189451
189452
189453
189454
189455
189456
189457
189458
189459
189460
189461
189462
189463
189464
189465
189466
189467
189468
189469
189470
189471
189472
189473
189474
189475
189476
189477
189478
189479
189480
189481
189482
189483
189484
189485
189486
189487
189488
189489
189490
189491
189492
189493
189494
189495
189496
189497
189498
189499
189500
189501
189502
189503
189504
189505
189506
189507
189508
189509
189510
189511
189512
189513
189514
189515
189516
189517
189518
189519
189520
189521
189522
189523
189524
189525
189526
189527
189528
189529
189530
189531
189532
189533
189534
189535
189536
189537
189538
189539
189540
189541
189542
189543
189544
189545
189546
189547
189548
189549
189550
189551
189552
189553
189554
189555
189556
189557
189558
189559
189560
189561
189562
189563
189564
189565
189566
189567
189568
189569
189570
189571
189572
189573
189574
189575
189576
189577
189578
189579
189580
189581
189582
189583
189584
189585
189586
189587
189588
189589
189590
189591
189592
189593
189594
189595
189596
189597
189598
189599
189600
189601
189602
189603
189604
189605
189606
189607
189608
189609
189610
189611
189612
189613
189614
189615
189616
189617
189618
189619
189620
189621
189622
189623
189624
189625
189626
189627
189628
189629
189630
189631
189632
189633
189634
189635
189636
189637
189638
189639
189640
189641
189642
189643
189644
189645
189646
189647
189648
189649
189650
189651
189652
189653
189654
189655
189656
189657
189658
189659
189660
189661
189662
189663
189664
189665
189666
189667
189668
189669
189670
189671
189672
189673
189674
189675
189676
189677
189678
189679
189680
189681
189682
189683
189684
189685
189686
189687
189688
189689
189690
189691
189692
189693
189694
189695
189696
189697
189698
189699
189700
189701
189702
189703
189704
189705
189706
189707
189708
189709
189710
189711
189712
189713
189714
189715
189716
189717
189718
189719
189720
189721
189722
189723
189724
189725
189726
189727
189728
189729
189730
189731
189732
189733
189734
189735
189736
189737
189738
189739
189740
189741
189742
189743
189744
189745
189746
189747
189748
189749
189750
189751
189752
189753
189754
189755
189756
189757
189758
189759
189760
189761
189762
189763
189764
189765
189766
189767
189768
189769
189770
189771
189772
189773
189774
189775
189776
189777
189778
189779
189780
189781
189782
189783
189784
189785
189786
189787
189788
189789
189790
189791
189792
189793
189794
189795
189796
189797
189798
189799
189800
189801
189802
189803
189804
189805
189806
189807
189808
189809
189810
189811
189812
189813
189814
189815
189816
189817
189818
189819
189820
189821
189822
189823
189824
189825
189826
189827
189828
189829
189830
189831
189832
189833
189834
189835
189836
189837
189838
189839
189840
189841
189842
189843
189844
189845
189846
189847
189848
189849
189850
189851
189852
189853
189854
189855
189856
189857
189858
189859
189860
189861
189862
189863
189864
189865
189866
189867
189868
189869
189870
189871
189872
189873
189874
189875
189876
189877
189878
189879
189880
189881
189882
189883
189884
189885
189886
189887
189888
189889
189890
189891
189892
189893
189894
189895
189896
189897
189898
189899
189900
189901
189902
189903
189904
189905
189906
189907
189908
189909
189910
189911
189912
189913
189914
189915
189916
189917
189918
189919
189920
189921
189922
189923
189924
189925
189926
189927
189928
189929
189930
189931
189932
189933
189934
189935
189936
189937
189938
189939
189940
189941
189942
189943
189944
189945
189946
189947
189948
189949
189950
189951
189952
189953
189954
189955
189956
189957
189958
189959
189960
189961
189962
189963
189964
189965
189966
189967
189968
189969
189970
189971
189972
189973
189974
189975
189976
189977
189978
189979
189980
189981
189982
189983
189984
189985
189986
189987
189988
189989
189990
189991
189992
189993
189994
189995
189996
189997
189998
189999
190000
190001
190002
190003
190004
190005
190006
190007
190008
190009
190010
190011
190012
190013
190014
190015
190016
190017
From 731a7a32eab3bddf5c29d84ae773f11e95110717 Mon Sep 17 00:00:00 2001
From: Nitin A Kamble <nitin.a.kamble@intel.com>
Date: Mon, 16 Jul 2012 07:14:38 -0700
Subject: [PATCH 1/2] yocto/emgd: emgd 1.14 driver

The starting-point code that subsequent patches will modify.  This is
a straight copy of the code in the emgd 1.14 emgd driver, specifically
IEMGD_HEAD_Linux/common/drm/emgd_drm.tgz from
LIN_IEMGD_1_14_GOLD_2443.tgz, the 'Linux Tar Ball' release downloaded
from http://edc.intel.com/Software/Downloads/EMGD/.

Signed-off-by: Nitin A Kamble <nitin.a.kamble@intel.com>
Signed-off-by: Tom Zanussi <tom.zanussi@intel.com>
---
 drivers/gpu/drm/emgd/Makefile                      |  332 ++
 drivers/gpu/drm/emgd/emgd/cfg/config.h             |  113 +
 drivers/gpu/drm/emgd/emgd/cfg/config_default.h     |  199 +
 drivers/gpu/drm/emgd/emgd/cfg/config_helper.c      |  244 ++
 .../gpu/drm/emgd/emgd/core/init/cmn/igd_global.c   |   34 +
 drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_init.c |  918 +++++
 .../drm/emgd/emgd/core/init/cmn/init_dispatch.h    |   65 +
 drivers/gpu/drm/emgd/emgd/core/init/plb/init_plb.c |  458 +++
 .../drm/emgd/emgd/core/init/plb/micro_init_plb.c   |  631 ++++
 drivers/gpu/drm/emgd/emgd/core/init/tnc/init_tnc.c |  621 ++++
 .../drm/emgd/emgd/core/init/tnc/micro_init_tnc.c   |  998 +++++
 drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp.c    | 2366 ++++++++++++
 .../drm/emgd/emgd/display/dsp/cmn/dsp_dispatch.h   |   64 +
 .../gpu/drm/emgd/emgd/display/dsp/plb/dsp_plb.c    |  709 ++++
 .../gpu/drm/emgd/emgd/display/dsp/tnc/dsp_tnc.c    |  542 +++
 .../gpu/drm/emgd/emgd/display/mode/cmn/igd_mode.c  | 2340 ++++++++++++
 drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.c | 1347 +++++++
 drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.h |   59 +
 .../drm/emgd/emgd/display/mode/cmn/micro_mode.c    | 1767 +++++++++
 .../drm/emgd/emgd/display/mode/cmn/mode_dispatch.h |  390 ++
 .../gpu/drm/emgd/emgd/display/mode/cmn/vga_mode.c  | 1467 ++++++++
 .../drm/emgd/emgd/display/mode/plb/clocks_plb.c    |  711 ++++
 .../drm/emgd/emgd/display/mode/plb/kms_mode_plb.c  | 1102 ++++++
 .../emgd/emgd/display/mode/plb/micro_mode_plb.c    | 1378 +++++++
 .../gpu/drm/emgd/emgd/display/mode/plb/mode_plb.c  | 1946 ++++++++++
 .../gpu/drm/emgd/emgd/display/mode/plb/mode_plb.h  |   47 +
 .../drm/emgd/emgd/display/mode/tnc/clocks_tnc.c    | 1184 ++++++
 .../drm/emgd/emgd/display/mode/tnc/kms_mode_tnc.c  | 1746 +++++++++
 .../emgd/emgd/display/mode/tnc/micro_mode_tnc.c    | 2646 +++++++++++++
 .../gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.c  | 2074 +++++++++++
 .../gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.h  |   52 +
 drivers/gpu/drm/emgd/emgd/display/pd/cmn/pd.c      |  516 +++
 .../gpu/drm/emgd/emgd/display/pi/cmn/displayid.c   | 1109 ++++++
 drivers/gpu/drm/emgd/emgd/display/pi/cmn/edid.c    | 1187 ++++++
 .../drm/emgd/emgd/display/pi/cmn/i2c_dispatch.h    |   78 +
 drivers/gpu/drm/emgd/emgd/display/pi/cmn/igd_pi.c  |  260 ++
 .../gpu/drm/emgd/emgd/display/pi/cmn/mode_table.c  | 2545 +++++++++++++
 .../gpu/drm/emgd/emgd/display/pi/cmn/pd_init_all.c |  215 ++
 drivers/gpu/drm/emgd/emgd/display/pi/cmn/pi.c      | 1919 ++++++++++
 drivers/gpu/drm/emgd/emgd/display/pi/plb/i2c_plb.c |  943 +++++
 .../drm/emgd/emgd/display/pi/tnc/i2c_bitbash_tnc.c |  599 +++
 .../drm/emgd/emgd/display/pi/tnc/i2c_gmbus_tnc.c   |  929 +++++
 drivers/gpu/drm/emgd/emgd/drm/drm_emgd_private.h   |  176 +
 drivers/gpu/drm/emgd/emgd/drm/emgd_connector.c     |  512 +++
 drivers/gpu/drm/emgd/emgd/drm/emgd_crtc.c          |  997 +++++
 drivers/gpu/drm/emgd/emgd/drm/emgd_drv.c           | 2472 +++++++++++++
 drivers/gpu/drm/emgd/emgd/drm/emgd_drv.h           |  209 ++
 drivers/gpu/drm/emgd/emgd/drm/emgd_encoder.c       |  477 +++
 drivers/gpu/drm/emgd/emgd/drm/emgd_fb.c            | 1413 +++++++
 drivers/gpu/drm/emgd/emgd/drm/emgd_fbcon.c         |  805 ++++
 drivers/gpu/drm/emgd/emgd/drm/emgd_interface.c     | 2730 ++++++++++++++
 drivers/gpu/drm/emgd/emgd/drm/emgd_mmap.c          |  186 +
 drivers/gpu/drm/emgd/emgd/drm/emgd_test_pvrsrv.c   | 1365 +++++++
 drivers/gpu/drm/emgd/emgd/drm/image_data.h         |   33 +
 drivers/gpu/drm/emgd/emgd/drm/splash_screen.c      | 2221 +++++++++++
 drivers/gpu/drm/emgd/emgd/drm/splash_screen.h      |  280 ++
 drivers/gpu/drm/emgd/emgd/drm/user_config.c        |  252 ++
 drivers/gpu/drm/emgd/emgd/drm/user_config.h        |  113 +
 drivers/gpu/drm/emgd/emgd/gmm/gmm.c                | 1382 +++++++
 drivers/gpu/drm/emgd/emgd/gmm/gtt.c                |  436 +++
 drivers/gpu/drm/emgd/emgd/include/cmd.h            |   47 +
 drivers/gpu/drm/emgd/emgd/include/context.h        |  255 ++
 drivers/gpu/drm/emgd/emgd/include/debug.h          |  169 +
 drivers/gpu/drm/emgd/emgd/include/decode.h         |   76 +
 drivers/gpu/drm/emgd/emgd/include/dispatch.h       |   59 +
 drivers/gpu/drm/emgd/emgd/include/dispatch_utils.h |   78 +
 drivers/gpu/drm/emgd/emgd/include/displayid.h      |  663 ++++
 drivers/gpu/drm/emgd/emgd/include/dsp.h            |   46 +
 drivers/gpu/drm/emgd/emgd/include/edid.h           |  130 +
 drivers/gpu/drm/emgd/emgd/include/general.h        |   84 +
 drivers/gpu/drm/emgd/emgd/include/instr_common.h   |   54 +
 drivers/gpu/drm/emgd/emgd/include/intelpci.h       |   98 +
 drivers/gpu/drm/emgd/emgd/include/math_fix.h       |   59 +
 drivers/gpu/drm/emgd/emgd/include/memlist.h        |  154 +
 drivers/gpu/drm/emgd/emgd/include/memory.h         |  421 +++
 drivers/gpu/drm/emgd/emgd/include/mode.h           |  444 +++
 drivers/gpu/drm/emgd/emgd/include/mode_access.h    |   52 +
 drivers/gpu/drm/emgd/emgd/include/module_init.h    |  109 +
 drivers/gpu/drm/emgd/emgd/include/msvdx.h          |  257 ++
 drivers/gpu/drm/emgd/emgd/include/pci.h            |  257 ++
 drivers/gpu/drm/emgd/emgd/include/pd.h             |  766 ++++
 drivers/gpu/drm/emgd/emgd/include/pd_init.h        |  191 +
 drivers/gpu/drm/emgd/emgd/include/pi.h             |   86 +
 drivers/gpu/drm/emgd/emgd/include/plb/appcontext.h |   67 +
 drivers/gpu/drm/emgd/emgd/include/plb/cmd.h        |   43 +
 drivers/gpu/drm/emgd/emgd/include/plb/context.h    |  210 ++
 drivers/gpu/drm/emgd/emgd/include/plb/instr.h      |  224 ++
 drivers/gpu/drm/emgd/emgd/include/plb/mi.h         |   77 +
 drivers/gpu/drm/emgd/emgd/include/plb/regs.h       |  747 ++++
 drivers/gpu/drm/emgd/emgd/include/plb/sgx.h        |  217 ++
 drivers/gpu/drm/emgd/emgd/include/plb/state3d.h    |  398 ++
 .../gpu/drm/emgd/emgd/include/plb/state3d_plb.h    | 1299 +++++++
 drivers/gpu/drm/emgd/emgd/include/psb_regs.h       |  658 ++++
 drivers/gpu/drm/emgd/emgd/include/rb.h             |  186 +
 drivers/gpu/drm/emgd/emgd/include/reset.h          |   43 +
 drivers/gpu/drm/emgd/emgd/include/sched.h          |  197 +
 drivers/gpu/drm/emgd/emgd/include/state2d.h        |   69 +
 drivers/gpu/drm/emgd/emgd/include/tnc/appcontext.h |   40 +
 drivers/gpu/drm/emgd/emgd/include/tnc/cmd.h        |   39 +
 drivers/gpu/drm/emgd/emgd/include/tnc/context.h    |   37 +
 drivers/gpu/drm/emgd/emgd/include/tnc/igd_tnc_wa.h |  133 +
 drivers/gpu/drm/emgd/emgd/include/tnc/instr.h      |   40 +
 drivers/gpu/drm/emgd/emgd/include/tnc/mi.h         |   41 +
 drivers/gpu/drm/emgd/emgd/include/tnc/regs.h       |  889 +++++
 drivers/gpu/drm/emgd/emgd/include/tnc/sgx.h        |   36 +
 drivers/gpu/drm/emgd/emgd/include/tnc/state3d.h    |   43 +
 .../gpu/drm/emgd/emgd/include/tnc/state3d_plb.h    |   38 +
 drivers/gpu/drm/emgd/emgd/include/topaz.h          |  209 ++
 drivers/gpu/drm/emgd/emgd/include/utils.h          |  174 +
 drivers/gpu/drm/emgd/emgd/include/vga.h            |  116 +
 drivers/gpu/drm/emgd/emgd/oal/src/math_fix.c       |  138 +
 drivers/gpu/drm/emgd/emgd/oal/src/memmap.c         |   55 +
 drivers/gpu/drm/emgd/emgd/oal/src/pci.c            |  277 ++
 drivers/gpu/drm/emgd/emgd/pal/Makefile.include     |   50 +
 drivers/gpu/drm/emgd/emgd/pal/ch7036/Makefile.gnu  |   44 +
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.c      | 2366 ++++++++++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.def    |    5 +
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.h      |   51 +
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.c | 1061 ++++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.h |   85 +
 .../drm/emgd/emgd/pal/ch7036/ch7036_def_regmap.h   |  700 ++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.c   |  468 +++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.h   |  213 ++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.c  |  296 ++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.h  |   52 +
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.c | 1994 ++++++++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.h |  243 ++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_pm.c   |  492 +++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.c | 1548 ++++++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.h |   59 +
 .../drm/emgd/emgd/pal/ch7036/ch7036_reg_table.c    |  224 ++
 .../drm/emgd/emgd/pal/ch7036/ch7036_reg_table.h    |  125 +
 .../gpu/drm/emgd/emgd/pal/ch7036/ch7036_typedef.h  |  472 +++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/config_.h     |   73 +
 drivers/gpu/drm/emgd/emgd/pal/ch7036/edid7036.car  | 3362 +++++++++++++++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.c   | 1587 ++++++++
 drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.h   |  170 +
 drivers/gpu/drm/emgd/emgd/pal/lpd/lpd.c            |  114 +
 drivers/gpu/drm/emgd/emgd/pal/lpd/pd_print.h       |   65 +
 drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.c          | 1555 ++++++++
 drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.h          |  164 +
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo.def        |   25 +
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.c     | 1484 ++++++++
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.h     |  141 +
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.c     |  524 +++
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.h     |  182 +
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.c     |  725 ++++
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.h     |  482 +++
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.c     | 3898 ++++++++++++++++++++
 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.h     |   71 +
 .../state/appcontext/cmn/appcontext_dispatch.h     |   56 +
 .../emgd/state/appcontext/cmn/igd_appcontext.c     |  148 +
 .../emgd/state/appcontext/plb/appcontext_plb.c     |  207 ++
 .../gpu/drm/emgd/emgd/state/power/cmn/igd_pwr.c    |  305 ++
 .../drm/emgd/emgd/state/power/cmn/pwr_dispatch.h   |   52 +
 .../gpu/drm/emgd/emgd/state/power/plb/pwr_plb.c    |  123 +
 drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg.c      |  451 +++
 .../gpu/drm/emgd/emgd/state/reg/cmn/reg_dispatch.h |   73 +
 drivers/gpu/drm/emgd/emgd/state/reg/plb/reg_plb.c  | 1126 ++++++
 drivers/gpu/drm/emgd/emgd/state/reg/tnc/reg_tnc.c  | 1205 ++++++
 drivers/gpu/drm/emgd/emgd/utils/math_fix.c         |  138 +
 drivers/gpu/drm/emgd/emgd/utils/memmap.c           |   55 +
 drivers/gpu/drm/emgd/emgd/utils/pci.c              |  277 ++
 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx.c      |  913 +++++
 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_init.c | 1516 ++++++++
 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.c  |  583 +++
 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.h  |   78 +
 .../gpu/drm/emgd/emgd/video/overlay/cmn/igd_ovl.c  |  540 +++
 .../drm/emgd/emgd/video/overlay/cmn/micro_ovl.c    |  165 +
 .../drm/emgd/emgd/video/overlay/cmn/ovl_coeff.c    | 1127 ++++++
 .../drm/emgd/emgd/video/overlay/cmn/ovl_coeff.h    |   45 +
 .../drm/emgd/emgd/video/overlay/cmn/ovl_dispatch.h |   57 +
 .../gpu/drm/emgd/emgd/video/overlay/cmn/ovl_virt.h |  107 +
 .../emgd/emgd/video/overlay/plb/micro_ovl_plb.c    | 1725 +++++++++
 .../gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.c |  542 +++
 .../gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.h |   55 +
 .../emgd/emgd/video/overlay/plb/ovl2_regs_plb.h    |   76 +
 .../gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb.c  | 2188 +++++++++++
 .../emgd/emgd/video/overlay/plb/ovl_plb_cache.c    |  321 ++
 .../emgd/emgd/video/overlay/plb/ovl_plb_cache.h    |  161 +
 .../drm/emgd/emgd/video/overlay/plb/ovl_regs_plb.h |  185 +
 .../emgd/emgd/video/overlay/tnc/micro_ovl_tnc.c    | 1738 +++++++++
 .../emgd/emgd/video/overlay/tnc/ovl2_regs_tnc.h    |   76 +
 .../gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.c |  460 +++
 .../gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.h |   55 +
 .../drm/emgd/emgd/video/overlay/tnc/ovl_regs_tnc.h |  190 +
 .../gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc.c  | 2128 +++++++++++
 .../emgd/emgd/video/overlay/tnc/ovl_tnc_cache.c    |  246 ++
 .../emgd/emgd/video/overlay/tnc/ovl_tnc_cache.h    |  162 +
 drivers/gpu/drm/emgd/emgd/video/topaz/topaz.c      |  300 ++
 drivers/gpu/drm/emgd/emgd/video/topaz/topaz_hdr.h  |  128 +
 drivers/gpu/drm/emgd/emgd/video/topaz/topaz_init.c |  985 +++++
 drivers/gpu/drm/emgd/include/emgd_drm.h            |  916 +++++
 drivers/gpu/drm/emgd/include/emgd_shared.h         |   98 +
 drivers/gpu/drm/emgd/include/gart.h                |   38 +
 drivers/gpu/drm/emgd/include/igd.h                 | 1625 ++++++++
 drivers/gpu/drm/emgd/include/igd_2d.h              |  380 ++
 drivers/gpu/drm/emgd/include/igd_appcontext.h      |   72 +
 drivers/gpu/drm/emgd/include/igd_blend.h           |   73 +
 drivers/gpu/drm/emgd/include/igd_debug.h           |   72 +
 drivers/gpu/drm/emgd/include/igd_errno.h           |   60 +
 drivers/gpu/drm/emgd/include/igd_gart.h            |   77 +
 drivers/gpu/drm/emgd/include/igd_gmm.h             |  383 ++
 drivers/gpu/drm/emgd/include/igd_init.h            |  895 +++++
 drivers/gpu/drm/emgd/include/igd_interrupt.h       |  300 ++
 drivers/gpu/drm/emgd/include/igd_mode.h            |  935 +++++
 drivers/gpu/drm/emgd/include/igd_ovl.h             |  326 ++
 drivers/gpu/drm/emgd/include/igd_pd.h              |  544 +++
 drivers/gpu/drm/emgd/include/igd_pi.h              |  130 +
 drivers/gpu/drm/emgd/include/igd_pwr.h             |   65 +
 drivers/gpu/drm/emgd/include/igd_rb.h              |   92 +
 drivers/gpu/drm/emgd/include/igd_render.h          |  532 +++
 drivers/gpu/drm/emgd/include/igd_reset.h           |   55 +
 drivers/gpu/drm/emgd/include/igd_version.h         |   41 +
 drivers/gpu/drm/emgd/include/igd_vga.h             |   65 +
 drivers/gpu/drm/emgd/include/io.h                  |  422 +++
 drivers/gpu/drm/emgd/include/memmap.h              |  104 +
 drivers/gpu/drm/emgd/pvr/include4/dbgdrvif.h       |  263 ++
 drivers/gpu/drm/emgd/pvr/include4/img_defs.h       |  104 +
 drivers/gpu/drm/emgd/pvr/include4/img_types.h      |  124 +
 drivers/gpu/drm/emgd/pvr/include4/ioctldef.h       |   94 +
 drivers/gpu/drm/emgd/pvr/include4/pdumpdefs.h      |   95 +
 drivers/gpu/drm/emgd/pvr/include4/pvr_debug.h      |  123 +
 drivers/gpu/drm/emgd/pvr/include4/pvrmodule.h      |   27 +
 drivers/gpu/drm/emgd/pvr/include4/pvrversion.h     |   34 +
 drivers/gpu/drm/emgd/pvr/include4/regpaths.h       |   39 +
 drivers/gpu/drm/emgd/pvr/include4/services.h       |  866 +++++
 drivers/gpu/drm/emgd/pvr/include4/servicesext.h    |  647 ++++
 drivers/gpu/drm/emgd/pvr/include4/sgx_options.h    |  220 ++
 drivers/gpu/drm/emgd/pvr/include4/sgxapi_km.h      |  323 ++
 drivers/gpu/drm/emgd/pvr/include4/sgxscript.h      |   77 +
 .../services4/3rdparty/emgd_bufferclass/emgd_bc.c  |  384 ++
 .../services4/3rdparty/emgd_bufferclass/emgd_bc.h  |  173 +
 .../3rdparty/emgd_bufferclass/emgd_bc_linux.c      | 1000 +++++
 .../services4/3rdparty/emgd_displayclass/emgd_dc.c | 2814 ++++++++++++++
 .../services4/3rdparty/emgd_displayclass/emgd_dc.h |  342 ++
 .../3rdparty/emgd_displayclass/emgd_dc_linux.c     |  153 +
 .../services4/include/env/linux/pvr_drm_shared.h   |   59 +
 .../drm/emgd/pvr/services4/include/kernelbuffer.h  |   57 +
 .../drm/emgd/pvr/services4/include/kerneldisplay.h |  152 +
 .../drm/emgd/pvr/services4/include/pvr_bridge.h    | 1380 +++++++
 .../drm/emgd/pvr/services4/include/pvr_bridge_km.h |  292 ++
 .../gpu/drm/emgd/pvr/services4/include/pvrmmap.h   |   32 +
 .../drm/emgd/pvr/services4/include/pvrsrv_errors.h |  189 +
 .../drm/emgd/pvr/services4/include/servicesint.h   |  272 ++
 .../drm/emgd/pvr/services4/include/sgx_bridge.h    |  473 +++
 .../drm/emgd/pvr/services4/include/sgx_mkif_km.h   |  339 ++
 .../gpu/drm/emgd/pvr/services4/include/sgxinfo.h   |  288 ++
 .../services4/srvkm/bridged/bridged_pvr_bridge.c   | 3447 +++++++++++++++++
 .../services4/srvkm/bridged/bridged_pvr_bridge.h   |  227 ++
 .../pvr/services4/srvkm/bridged/bridged_support.c  |   81 +
 .../pvr/services4/srvkm/bridged/bridged_support.h  |   39 +
 .../srvkm/bridged/sgx/bridged_sgx_bridge.c         | 2510 +++++++++++++
 .../srvkm/bridged/sgx/bridged_sgx_bridge.h         |   38 +
 .../pvr/services4/srvkm/common/buffer_manager.c    | 2069 +++++++++++
 .../emgd/pvr/services4/srvkm/common/deviceclass.c  | 2060 +++++++++++
 .../emgd/pvr/services4/srvkm/common/devicemem.c    | 1546 ++++++++
 .../drm/emgd/pvr/services4/srvkm/common/handle.c   | 1545 ++++++++
 .../gpu/drm/emgd/pvr/services4/srvkm/common/hash.c |  459 +++
 .../drm/emgd/pvr/services4/srvkm/common/lists.c    |   95 +
 .../gpu/drm/emgd/pvr/services4/srvkm/common/mem.c  |  147 +
 .../emgd/pvr/services4/srvkm/common/mem_debug.c    |  246 ++
 .../drm/emgd/pvr/services4/srvkm/common/metrics.c  |  156 +
 .../emgd/pvr/services4/srvkm/common/pdump_common.c | 1723 +++++++++
 .../drm/emgd/pvr/services4/srvkm/common/perproc.c  |  279 ++
 .../drm/emgd/pvr/services4/srvkm/common/power.c    |  743 ++++
 .../drm/emgd/pvr/services4/srvkm/common/pvrsrv.c   | 1194 ++++++
 .../drm/emgd/pvr/services4/srvkm/common/queue.c    | 1161 ++++++
 .../gpu/drm/emgd/pvr/services4/srvkm/common/ra.c   | 1867 ++++++++++
 .../drm/emgd/pvr/services4/srvkm/common/resman.c   |  704 ++++
 .../drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.c | 2772 ++++++++++++++
 .../drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.h |  135 +
 .../drm/emgd/pvr/services4/srvkm/devices/sgx/pb.c  |  454 +++
 .../services4/srvkm/devices/sgx/sgx_bridge_km.h    |  143 +
 .../pvr/services4/srvkm/devices/sgx/sgxconfig.h    |  161 +
 .../pvr/services4/srvkm/devices/sgx/sgxinfokm.h    |  348 ++
 .../emgd/pvr/services4/srvkm/devices/sgx/sgxinit.c | 2267 ++++++++++++
 .../emgd/pvr/services4/srvkm/devices/sgx/sgxkick.c |  740 ++++
 .../pvr/services4/srvkm/devices/sgx/sgxpower.c     |  465 +++
 .../pvr/services4/srvkm/devices/sgx/sgxreset.c     |  485 +++
 .../pvr/services4/srvkm/devices/sgx/sgxtransfer.c  |  549 +++
 .../pvr/services4/srvkm/devices/sgx/sgxutils.c     | 1054 ++++++
 .../pvr/services4/srvkm/devices/sgx/sgxutils.h     |   95 +
 .../emgd/pvr/services4/srvkm/env/linux/env_data.h  |   62 +
 .../pvr/services4/srvkm/env/linux/env_perproc.h    |   52 +
 .../drm/emgd/pvr/services4/srvkm/env/linux/event.c |  267 ++
 .../drm/emgd/pvr/services4/srvkm/env/linux/event.h |   28 +
 .../pvr/services4/srvkm/env/linux/kbuild/Makefile  |  151 +
 .../emgd/pvr/services4/srvkm/env/linux/linkage.h   |   57 +
 .../drm/emgd/pvr/services4/srvkm/env/linux/lock.h  |   28 +
 .../drm/emgd/pvr/services4/srvkm/env/linux/mm.c    | 2377 ++++++++++++
 .../drm/emgd/pvr/services4/srvkm/env/linux/mm.h    |  323 ++
 .../drm/emgd/pvr/services4/srvkm/env/linux/mmap.c  | 1149 ++++++
 .../drm/emgd/pvr/services4/srvkm/env/linux/mmap.h  |  103 +
 .../emgd/pvr/services4/srvkm/env/linux/module.c    |  756 ++++
 .../drm/emgd/pvr/services4/srvkm/env/linux/mutex.c |   28 +
 .../drm/emgd/pvr/services4/srvkm/env/linux/mutex.h |   37 +
 .../emgd/pvr/services4/srvkm/env/linux/mutils.c    |  126 +
 .../emgd/pvr/services4/srvkm/env/linux/mutils.h    |   93 +
 .../emgd/pvr/services4/srvkm/env/linux/osfunc.c    | 2529 +++++++++++++
 .../emgd/pvr/services4/srvkm/env/linux/osperproc.c |  109 +
 .../drm/emgd/pvr/services4/srvkm/env/linux/pdump.c |  658 ++++
 .../pvr/services4/srvkm/env/linux/private_data.h   |   63 +
 .../drm/emgd/pvr/services4/srvkm/env/linux/proc.c  |  962 +++++
 .../drm/emgd/pvr/services4/srvkm/env/linux/proc.h  |  111 +
 .../pvr/services4/srvkm/env/linux/pvr_bridge_k.c   |  647 ++++
 .../emgd/pvr/services4/srvkm/env/linux/pvr_debug.c |  418 +++
 .../emgd/pvr/services4/srvkm/env/linux/pvr_drm.c   |  301 ++
 .../emgd/pvr/services4/srvkm/env/linux/pvr_drm.h   |   65 +
 .../emgd/pvr/services4/srvkm/hwdefs/sgx535defs.h   |  633 ++++
 .../emgd/pvr/services4/srvkm/hwdefs/sgx540defs.h   |  586 +++
 .../emgd/pvr/services4/srvkm/hwdefs/sgx545defs.h   |  828 +++++
 .../drm/emgd/pvr/services4/srvkm/hwdefs/sgxdefs.h  |   78 +
 .../emgd/pvr/services4/srvkm/hwdefs/sgxerrata.h    |  310 ++
 .../pvr/services4/srvkm/hwdefs/sgxfeaturedefs.h    |  159 +
 .../drm/emgd/pvr/services4/srvkm/hwdefs/sgxmmu.h   |   75 +
 .../pvr/services4/srvkm/include/buffer_manager.h   |  214 ++
 .../drm/emgd/pvr/services4/srvkm/include/device.h  |  274 ++
 .../drm/emgd/pvr/services4/srvkm/include/handle.h  |  378 ++
 .../drm/emgd/pvr/services4/srvkm/include/hash.h    |   69 +
 .../drm/emgd/pvr/services4/srvkm/include/lists.h   |  172 +
 .../drm/emgd/pvr/services4/srvkm/include/metrics.h |  126 +
 .../drm/emgd/pvr/services4/srvkm/include/osfunc.h  |  483 +++
 .../emgd/pvr/services4/srvkm/include/osperproc.h   |   72 +
 .../emgd/pvr/services4/srvkm/include/pdump_km.h    |  448 +++
 .../pvr/services4/srvkm/include/pdump_osfunc.h     |  133 +
 .../drm/emgd/pvr/services4/srvkm/include/perproc.h |  106 +
 .../drm/emgd/pvr/services4/srvkm/include/power.h   |  116 +
 .../drm/emgd/pvr/services4/srvkm/include/queue.h   |  115 +
 .../gpu/drm/emgd/pvr/services4/srvkm/include/ra.h  |  151 +
 .../drm/emgd/pvr/services4/srvkm/include/resman.h  |  109 +
 .../pvr/services4/srvkm/include/services_headers.h |   45 +
 .../drm/emgd/pvr/services4/srvkm/include/srvkm.h   |   65 +
 .../emgd/pvr/services4/system/common/sysconfig.c   | 1367 +++++++
 .../emgd/pvr/services4/system/common/sysutils.c    |   26 +
 .../emgd/pvr/services4/system/include/oemfuncs.h   |   68 +
 .../services4/system/include/sys_pvr_drm_shared.h  |   34 +
 .../emgd/pvr/services4/system/include/syscommon.h  |   27 +
 .../emgd/pvr/services4/system/include/sysconfig.h  |  326 ++
 .../emgd/pvr/services4/system/include/sysinfo.h    |   42 +
 .../emgd/pvr/services4/system/include/syslocal.h   |   80 +
 .../drm/emgd/pvr/services4/system/plb/sysconfig.c  |   48 +
 .../gpu/drm/emgd/pvr/services4/system/plb/sysplb.h |   33 +
 .../drm/emgd/pvr/services4/system/tnc/sysconfig.c  |   48 +
 .../gpu/drm/emgd/pvr/services4/system/tnc/systnc.h |   33 +
 .../emgd/pvr/tools/intern/debug/client/linuxsrv.h  |   44 +
 .../tools/intern/debug/dbgdriv/common/dbgdriv.c    | 2072 +++++++++++
 .../tools/intern/debug/dbgdriv/common/dbgdriv.h    |  112 +
 .../tools/intern/debug/dbgdriv/common/hostfunc.h   |   54 +
 .../pvr/tools/intern/debug/dbgdriv/common/hotkey.c |  131 +
 .../pvr/tools/intern/debug/dbgdriv/common/hotkey.h |   56 +
 .../pvr/tools/intern/debug/dbgdriv/common/ioctl.c  |  367 ++
 .../pvr/tools/intern/debug/dbgdriv/common/ioctl.h  |   83 +
 .../tools/intern/debug/dbgdriv/linux/hostfunc.c    |  300 ++
 .../intern/debug/dbgdriv/linux/kbuild/Makefile     |   33 +
 .../pvr/tools/intern/debug/dbgdriv/linux/main.c    |  294 ++
 .../debug/dbgdriv/linux/makefile.linux.common      |   38 +
 357 files changed, 187142 insertions(+), 0 deletions(-)
 create mode 100755 drivers/gpu/drm/emgd/Makefile
 create mode 100644 drivers/gpu/drm/emgd/emgd/cfg/config.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/cfg/config_default.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/cfg/config_helper.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_global.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_init.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/cmn/init_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/plb/init_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/plb/micro_init_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/tnc/init_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/core/init/tnc/micro_init_tnc.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/dsp/plb/dsp_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/dsp/tnc/dsp_tnc.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/mode/cmn/igd_mode.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/cmn/micro_mode.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/cmn/mode_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/cmn/vga_mode.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/plb/clocks_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/plb/kms_mode_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/plb/micro_mode_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/tnc/clocks_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/tnc/kms_mode_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/tnc/micro_mode_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/pd/cmn/pd.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/cmn/displayid.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/cmn/edid.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/pi/cmn/i2c_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/cmn/igd_pi.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/cmn/mode_table.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/cmn/pd_init_all.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/display/pi/cmn/pi.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/plb/i2c_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_bitbash_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_gmbus_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/drm_emgd_private.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_connector.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_crtc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_drv.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_drv.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_encoder.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_fb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_fbcon.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_interface.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_mmap.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/emgd_test_pvrsrv.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/drm/image_data.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/drm/splash_screen.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/drm/splash_screen.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/user_config.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/drm/user_config.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/gmm/gmm.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/gmm/gtt.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/cmd.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/context.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/debug.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/decode.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/dispatch_utils.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/displayid.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/dsp.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/edid.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/general.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/instr_common.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/intelpci.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/math_fix.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/memlist.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/memory.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/mode.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/mode_access.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/module_init.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/msvdx.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/pci.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/pd.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/pd_init.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/pi.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/appcontext.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/cmd.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/context.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/instr.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/mi.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/regs.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/sgx.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/state3d.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/plb/state3d_plb.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/psb_regs.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/rb.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/reset.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/sched.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/state2d.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/appcontext.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/cmd.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/context.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/igd_tnc_wa.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/instr.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/mi.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/regs.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/sgx.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/state3d.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/tnc/state3d_plb.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/include/topaz.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/utils.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/include/vga.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/oal/src/math_fix.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/oal/src/memmap.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/oal/src/pci.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/Makefile.include
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/Makefile.gnu
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.def
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_def_regmap.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_pm.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.c
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_typedef.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/config_.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/edid7036.car
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/lpd/lpd.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/lpd/pd_print.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo.def
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/appcontext_dispatch.h
 create mode 100755 drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/igd_appcontext.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/appcontext/plb/appcontext_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/power/cmn/igd_pwr.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/power/cmn/pwr_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/power/plb/pwr_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/reg/plb/reg_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/state/reg/tnc/reg_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/utils/math_fix.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/utils/memmap.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/utils/pci.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_init.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/cmn/igd_ovl.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/cmn/micro_ovl.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_dispatch.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_virt.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/micro_ovl_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_regs_plb.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_regs_plb.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/micro_ovl_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_regs_tnc.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_regs_tnc.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/topaz/topaz.c
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/topaz/topaz_hdr.h
 create mode 100644 drivers/gpu/drm/emgd/emgd/video/topaz/topaz_init.c
 create mode 100644 drivers/gpu/drm/emgd/include/emgd_drm.h
 create mode 100644 drivers/gpu/drm/emgd/include/emgd_shared.h
 create mode 100644 drivers/gpu/drm/emgd/include/gart.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_2d.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_appcontext.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_blend.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_debug.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_errno.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_gart.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_gmm.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_init.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_interrupt.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_mode.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_ovl.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_pd.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_pi.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_pwr.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_rb.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_render.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_reset.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_version.h
 create mode 100644 drivers/gpu/drm/emgd/include/igd_vga.h
 create mode 100644 drivers/gpu/drm/emgd/include/io.h
 create mode 100644 drivers/gpu/drm/emgd/include/memmap.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/dbgdrvif.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/img_defs.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/img_types.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/ioctldef.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/pdumpdefs.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/pvr_debug.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/pvrmodule.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/pvrversion.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/regpaths.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/services.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/servicesext.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/sgx_options.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/sgxapi_km.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/include4/sgxscript.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc_linux.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc_linux.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/env/linux/pvr_drm_shared.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/kernelbuffer.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/kerneldisplay.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge_km.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/pvrmmap.h
 create mode 100755 drivers/gpu/drm/emgd/pvr/services4/include/pvrsrv_errors.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/servicesint.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/sgx_bridge.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/sgx_mkif_km.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/include/sgxinfo.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/buffer_manager.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/deviceclass.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/devicemem.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/handle.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/hash.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/lists.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem_debug.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/metrics.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pdump_common.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/perproc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/power.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pvrsrv.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/queue.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/ra.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/common/resman.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/pb.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgx_bridge_km.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxconfig.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinfokm.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinit.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxkick.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxpower.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxreset.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxtransfer.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_data.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_perproc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/kbuild/Makefile
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/linkage.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/lock.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/module.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osfunc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osperproc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pdump.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/private_data.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_bridge_k.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_debug.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx535defs.h
 create mode 100755 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx540defs.h
 create mode 100755 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx545defs.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxdefs.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxerrata.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxfeaturedefs.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxmmu.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/buffer_manager.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/device.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/handle.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/hash.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/lists.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/metrics.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osfunc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osperproc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_km.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_osfunc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/perproc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/power.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/queue.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/ra.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/resman.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/services_headers.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/srvkm/include/srvkm.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/common/sysconfig.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/common/sysutils.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/include/oemfuncs.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/include/sys_pvr_drm_shared.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/include/syscommon.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/include/sysconfig.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/include/sysinfo.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/include/syslocal.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/plb/sysconfig.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/plb/sysplb.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/tnc/sysconfig.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/services4/system/tnc/systnc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/client/linuxsrv.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hostfunc.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.h
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/hostfunc.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/kbuild/Makefile
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/main.c
 create mode 100644 drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/makefile.linux.common

diff --git a/drivers/gpu/drm/emgd/Makefile b/drivers/gpu/drm/emgd/Makefile
new file mode 100755
index 0000000..c4bd102
--- /dev/null
+++ b/drivers/gpu/drm/emgd/Makefile
@@ -0,0 +1,332 @@
+#----------------------------------------------------------------------------
+# Filename: Makefile.gnu
+# $Revision: 1.58 $
+#----------------------------------------------------------------------------
+# Copyright (c) 2002-2010, Intel Corporation.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#----------------------------------------------------------------------------
+export EGD_TOPLEVEL = DRM Driver
+
+KERNELVER ?= $(shell uname -r)
+KERNELDIR ?= /lib/modules/$(KERNELVER)/build
+INSTALLDIR ?= /lib/modules/$(KERNELVER)/kernel/drivers/gpu/drm/emgd
+
+BLUE = \033[34m
+OFF = \033[0m
+BUILD ?= release
+CONFIG_PVR_RELEASE ?= $(BUILD)
+CONFIG_DRM_EGD ?= m
+
+# Get the include paths pointed to the right place. 
+export  EMGD_MOD_DIR ?= $(CURDIR)
+
+BUILDDATE ?= $(shell date +%Y%m%d)
+
+PROJECT_INCLUDES = \
+	   -I$(EMGD_MOD_DIR)/include \
+	   -I$(EMGD_MOD_DIR)/emgd/display/mode/cmn \
+	   -I$(EMGD_MOD_DIR)/emgd/video/overlay/cmn \
+	   -I$(EMGD_MOD_DIR)/emgd/video/msvdx \
+	   -I$(EMGD_MOD_DIR)/emgd/include \
+	   -I$(EMGD_MOD_DIR)/emgd/cfg \
+	   -I$(EMGD_MOD_DIR)/emgd/pal/lpd \
+	   -I$(EMGD_MOD_DIR)/emgd/pal/lvds \
+	   -I$(EMGD_MOD_DIR)/emgd/pal/ch7036 \
+	   -I$(EMGD_MOD_DIR)/emgd/drm \
+	   -I$(KERNELDIR)/include/drm \
+	   -I/usr/src/linux-headers-2.6.32-5-common/include/drm \
+	   -I$(EMGD_MOD_DIR)/pvr/include4 \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/include \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/include/env/linux \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/srvkm/env/linux \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/srvkm/include \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/srvkm/bridged \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/system/plb \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/system/tnc \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/system/include \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/srvkm/hwdefs \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/srvkm/bridged/sgx \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/srvkm/devices/sgx \
+	   -I$(EMGD_MOD_DIR)/pvr/services4/3rdparty/emgd_bufferclass \
+	   -I$(EMGD_MOD_DIR)/pvr/tools/intern/debug \
+	   -DSUPPORT_DRI_DRM_EXT \
+	   -DLINUX \
+	   -DPVR_BUILD_DIR="\"emgd\"" \
+	   -DPVR_BUILD_DATE="\"$(BUILDDATE)\"" \
+	   -DPVR_BUILD_TYPE="\"$(CONFIG_PVR_RELEASE)\"" \
+	   -DBUILD=$(CONFIG_PVR_RELEASE) \
+	   -DPVR_SECURE_HANDLES \
+	   -DPVR_PROC_USE_SEQ_FILE \
+	   -DLDM_PCI \
+	   -DSUPPORT_CACHEFLUSH_ON_ALLOC \
+	   -DSUPPORT_DRI_DRM \
+	   -DSGX535 \
+	   -DSGX_CORE_REV=121 \
+	   -UDEBUG_LOG_PATH_TRUNCATE \
+	   -DDISPLAY_CONTROLLER=emgd_dc \
+	   -D_XOPEN_SOURCE=600 \
+	   -DSERVICES4 \
+	   -DPVR2D_VALIDATE_INPUT_PARAMS \
+	   -DSUPPORT_SRVINIT \
+	   -DSUPPORT_SGX \
+	   -DSUPPORT_PERCONTEXT_PB \
+	   -DSUPPORT_LINUX_X86_WRITECOMBINE \
+	   -DSUPPORT_SECURE_DRM_AUTH_EXPORT \
+	   -DSUPPORT_PDUMP_DELAYED_INITPHASE_TERMINATION \
+	   -DTRANSFER_QUEUE \
+	   -DSYS_USING_INTERRUPTS \
+	   -DSUPPORT_HW_RECOVERY \
+	   -DSUPPORT_ACTIVE_POWER_MANAGEMENT \
+	   -DPVR_SECURE_HANDLES \
+	   -DUSE_PTHREADS \
+	   -DSUPPORT_SGX_EVENT_OBJECT \
+	   -DSUPPORT_SGX_HWPERF \
+	   -DSUPPORT_SGX_LOW_LATENCY_SCHEDULING \
+	   -DSUPPORT_LINUX_X86_PAT \
+	   -DSUPPORT_SGX535 \
+	   -DSUPPORT_CACHE_LINE_FLUSH \
+	   -DSUPPORT_CPU_CACHED_BUFFERS \
+	   -DDEBUG_MESA_OGL_TRACE \
+	   -DSUPPORT_EGL_IMAGE_SYNC_DEPENDENCY \
+
+
+ifeq "$(strip $(CONFIG_PVR_RELEASE))" "release"
+	ccflags-y += -DRELEASE
+else
+	# FIXME: Looks like this causes conflicts in the emgd code.
+	ccflags-y += -DDEBUG
+	ccflags-y += -DDEBUG_BUILD_TYPE 
+endif
+
+EXTRA_CFLAGS += $(PROJECT_INCLUDES)
+
+ifeq ($(PDUMP),1)
+	EXTRA_CFLAGS += -DPDUMP=1
+endif
+
+EMGD_OBJS := \
+	emgd/drm/emgd_fb.o \
+	emgd/drm/emgd_fbcon.o \
+	emgd/drm/emgd_crtc.o \
+	emgd/drm/emgd_encoder.o \
+	emgd/drm/emgd_connector.o \
+	emgd/drm/emgd_mmap.o \
+	emgd/drm/emgd_drv.o \
+	emgd/drm/emgd_interface.o \
+	emgd/drm/emgd_test_pvrsrv.o \
+	emgd/drm/user_config.o \
+	emgd/drm/splash_screen.o \
+	emgd/display/pd/cmn/pd.o \
+	emgd/display/pi/cmn/igd_pi.o \
+	emgd/display/pi/cmn/displayid.o \
+	emgd/display/pi/cmn/pd_init_all.o \
+	emgd/display/pi/cmn/edid.o \
+	emgd/display/pi/cmn/pi.o \
+	emgd/display/pi/cmn/mode_table.o \
+	emgd/display/pi/tnc/i2c_gmbus_tnc.o \
+	emgd/display/pi/tnc/i2c_bitbash_tnc.o \
+	emgd/display/pi/plb/i2c_plb.o \
+	emgd/display/mode/cmn/match.o \
+	emgd/display/mode/cmn/micro_mode.o \
+	emgd/display/mode/cmn/vga_mode.o \
+	emgd/display/mode/cmn/igd_mode.o \
+	emgd/display/mode/tnc/micro_mode_tnc.o \
+	emgd/display/mode/tnc/mode_tnc.o \
+	emgd/display/mode/tnc/kms_mode_tnc.o \
+	emgd/display/mode/tnc/clocks_tnc.o \
+	emgd/display/mode/plb/micro_mode_plb.o \
+	emgd/display/mode/plb/clocks_plb.o \
+	emgd/display/mode/plb/mode_plb.o \
+	emgd/display/mode/plb/kms_mode_plb.o \
+	emgd/display/dsp/cmn/dsp.o \
+	emgd/display/dsp/tnc/dsp_tnc.o \
+	emgd/display/dsp/plb/dsp_plb.o \
+	emgd/core/init/cmn/igd_global.o \
+	emgd/core/init/cmn/igd_init.o \
+	emgd/core/init/tnc/micro_init_tnc.o \
+	emgd/core/init/tnc/init_tnc.o \
+	emgd/core/init/plb/init_plb.o \
+	emgd/core/init/plb/micro_init_plb.o \
+	emgd/state/power/cmn/igd_pwr.o \
+	emgd/state/power/plb/pwr_plb.o \
+	emgd/state/appcontext/cmn/igd_appcontext.o \
+	emgd/state/appcontext/plb/appcontext_plb.o \
+	emgd/state/reg/cmn/reg.o \
+	emgd/state/reg/tnc/reg_tnc.o \
+	emgd/state/reg/plb/reg_plb.o \
+	emgd/video/overlay/cmn/ovl_coeff.o \
+	emgd/video/overlay/cmn/igd_ovl.o \
+	emgd/video/overlay/cmn/micro_ovl.o \
+	emgd/video/overlay/tnc/ovl_tnc_cache.o \
+	emgd/video/overlay/tnc/ovl_tnc.o \
+	emgd/video/overlay/tnc/ovl2_tnc.o \
+	emgd/video/overlay/tnc/micro_ovl_tnc.o \
+	emgd/video/overlay/plb/ovl_plb_cache.o \
+	emgd/video/overlay/plb/ovl2_plb.o \
+	emgd/video/overlay/plb/ovl_plb.o \
+	emgd/video/overlay/plb/micro_ovl_plb.o \
+	emgd/video/msvdx/msvdx_init.o \
+	emgd/video/msvdx/msvdx.o \
+	emgd/video/msvdx/msvdx_pvr.o \
+	emgd/video/topaz/topaz_init.o \
+	emgd/video/topaz/topaz.o \
+	emgd/pal/sdvo/sdvo_attr.o \
+	emgd/pal/sdvo/sdvo_hdmi.o \
+	emgd/pal/sdvo/sdvo_port.o \
+	emgd/pal/sdvo/sdvo_intf.o \
+	emgd/pal/ch7036/ch7036_attr.o \
+	emgd/pal/ch7036/ch7036_fw.o \
+	emgd/pal/ch7036/ch7036_intf.o \
+	emgd/pal/ch7036/ch7036_port.o \
+	emgd/pal/ch7036/ch7036.o \
+	emgd/pal/ch7036/ch7036_iic.o \
+	emgd/pal/ch7036/ch7036_pm.o \
+	emgd/pal/ch7036/ch7036_reg_table.o \
+	emgd/pal/ch7036/lvds/lvds.o \
+	emgd/pal/lvds/lvds.o \
+	emgd/pal/lpd/lpd.o \
+	emgd/gmm/gmm.o \
+	emgd/gmm/gtt.o \
+	emgd/utils/pci.o \
+	emgd/utils/memmap.o \
+	emgd/utils/math_fix.o \
+
+ENVDIR = pvr/services4/srvkm/env/linux
+COMMONDIR = pvr/services4/srvkm/common
+BRIDGEDDIR = pvr/services4/srvkm/bridged
+SYSCONFIGDIR = pvr/services4/system/common
+SGXDIR = pvr/services4/srvkm/devices/sgx
+DISPCLASSDIR = pvr/services4/3rdparty/emgd_displayclass
+BUFFERCLASSDIR = pvr/services4/3rdparty/emgd_bufferclass
+
+ifeq ($(PDUMP),1)
+DBGDRVDIR = pvr/tools/intern/debug/dbgdriv
+
+DBGDRV_OBJS = $(DBGDRVDIR)/linux/main.o \
+              $(DBGDRVDIR)/common/dbgdriv.o \
+              $(DBGDRVDIR)/common/ioctl.o \
+              $(DBGDRVDIR)/linux/hostfunc.o \
+              $(DBGDRVDIR)/common/hotkey.o
+endif
+
+ENV_OBJS = $(ENVDIR)/osfunc.o \
+	   $(ENVDIR)/mutils.o \
+	   $(ENVDIR)/mmap.o \
+	   $(ENVDIR)/module.o \
+	   $(ENVDIR)/pdump.o \
+	   $(ENVDIR)/proc.o \
+	   $(ENVDIR)/pvr_bridge_k.o \
+	   $(ENVDIR)/pvr_debug.o \
+	   $(ENVDIR)/mm.o \
+	   $(ENVDIR)/mutex.o \
+	   $(ENVDIR)/event.o \
+	   $(ENVDIR)/osperproc.o \
+	   $(ENVDIR)/pvr_drm.o
+
+COMMON_OBJS = $(COMMONDIR)/buffer_manager.o \
+	    $(COMMONDIR)/devicemem.o \
+	    $(COMMONDIR)/deviceclass.o \
+	    $(COMMONDIR)/handle.o \
+	    $(COMMONDIR)/hash.o \
+	    $(COMMONDIR)/metrics.o \
+	    $(COMMONDIR)/pvrsrv.o \
+	    $(COMMONDIR)/queue.o \
+	    $(COMMONDIR)/ra.o \
+	    $(COMMONDIR)/resman.o \
+	    $(COMMONDIR)/power.o \
+	    $(COMMONDIR)/mem.o \
+	    $(COMMONDIR)/pdump_common.o \
+	    $(COMMONDIR)/perproc.o \
+	    $(COMMONDIR)/lists.o \
+	    $(COMMONDIR)/mem_debug.o
+
+BRIDGED_OBJS = $(BRIDGEDDIR)/bridged_support.o \
+	     $(BRIDGEDDIR)/bridged_pvr_bridge.o \
+	     $(BRIDGEDDIR)/sgx/bridged_sgx_bridge.o
+
+SYSCONFIG_OBJS = $(SYSCONFIGDIR)/sysconfig.o \
+	        pvr/services4/system/tnc/sysconfig.o \
+	        pvr/services4/system/plb/sysconfig.o \
+		$(SYSCONFIGDIR)/sysutils.o
+#		 $(SYSCONFIGDIR)/sysirq.o
+#	 	 $(SYSCONFIGDIR)/ospm_power.o \
+
+SGX_OBJS = $(SGXDIR)/sgxinit.o \
+	 $(SGXDIR)/sgxpower.o \
+	 $(SGXDIR)/sgxreset.o \
+	 $(SGXDIR)/sgxutils.o \
+	 $(SGXDIR)/sgxkick.o \
+	 $(SGXDIR)/sgxtransfer.o \
+	 $(SGXDIR)/mmu.o \
+	 $(SGXDIR)/pb.o
+
+DC_OBJS = $(DISPCLASSDIR)/emgd_dc.o \
+	  $(DISPCLASSDIR)/emgd_dc_linux.o
+
+BC_OBJS = $(BUFFERCLASSDIR)/emgd_bc.o \
+	  $(BUFFERCLASSDIR)/emgd_bc_linux.o
+
+
+emgd-y := \
+	$(DC_OBJS) \
+	$(EMGD_OBJS) \
+	$(ENV_OBJS) \
+	$(COMMON_OBJS) \
+	$(BRIDGED_OBJS) \
+	$(SYSCONFIG_OBJS) \
+	$(SGX_OBJS) \
+	$(BC_OBJS) \
+
+ifeq ($(PDUMP),1)
+	emgd-y += $(DBGDRV_OBJS)
+endif
+
+obj-$(CONFIG_DRM_EGD) += emgd.o
+
+all:: clean modules
+
+modules::
+	@echo $(CURDIR) -- $(CONFIG_PVR_RELEASE)
+	@echo "$(MAKE) -C $(KERNELDIR) M=$(CURDIR) modules"
+	@$(MAKE) -C $(KERNELDIR) M=$(CURDIR) modules
+
+clean::
+	@rm -f $(emgd-y)
+	@rm -f emgd.o emgd.mod.* emgd.ko Module.* modules.order
+	@find . -name "*.cmd" -exec rm '{}' \;
+
+install::
+	install -o root -g root -m 755 -d $(INSTALLDIR)
+	install -o root -g root -m 744 emgd.ko $(INSTALLDIR)
+	/sbin/depmod -a
+
+uninstall::
+	rmmod $(INSTALLDIR)/emgd.ko
+	rm -rf $(INSTALLDIR)/emgd.ko
+	/sbin/depmod -a
+
+debug::
+	export CONFIG_PVR_RELEASE=debug; $(MAKE) modules
+
+package:: clean
+	@echo -e "$(BLUE)Packaging $(EGD_TOPLEVEL)$(OFF)";
+	mkdir -p $(EGD_PKG)
+	tar -C $(EMGD_MOD_DIR) --exclude "CVS" -czf $(EGD_PKG)/emgd_drm.tgz *
+
diff --git a/drivers/gpu/drm/emgd/emgd/cfg/config.h b/drivers/gpu/drm/emgd/emgd/cfg/config.h
new file mode 100644
index 0000000..14e7ca8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/cfg/config.h
@@ -0,0 +1,113 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: config.h
+ * $Revision: 1.14 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the compile options for the IGD compile. It is included
+ *  by all IGD OAL and RAL modules. Do not remove valid options from this
+ *  file, simply comment them out.
+ *  Eventually a config tool will auto generate this file based on selected
+ *  options.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_CONFIG_H
+#define _HAL_CONFIG_H
+
+/*
+ * Which Cores are supported
+ *
+ * Use Defaults
+ */
+
+/*
+ * This macro configures the DRM/kernel's EMGD_DEBUG() and EMGD_DEBUG_S() macros to
+ * use the KERN_INFO message priority, instead of the normal KERN_DEBUG message
+ * priority.  This is useful for bugs (e.g. crashes) where dmesg can't be used
+ * to obtain debug messages.
+ */
+/* #define CONFIG_USE_INFO_PRIORITY */
+
+
+/*
+ * Which of the optional modules are included in the build
+ * for the most part this is for modules that need an init
+ * or power entry point.
+ *
+ * Use Defaults.
+ */
+
+/*
+ * Default FB/Display Resolution
+ */
+#define CONFIG_DEFAULT_WIDTH  640
+#define CONFIG_DEFAULT_HEIGHT 480
+#define CONFIG_DEFAULT_PF     IGD_PF_ARGB32
+
+
+/*
+  power modes supported
+  0 -don't support
+  1 - support
+
+  Use Defaults.
+*/
+
+/*
+ * Turn off fences for performance analysis. 3d makes use of "Use Fences"
+ * So this will make fences regions become linear but everything should
+ * still work.
+ *
+ * #define CONFIG_NOFENCES
+ */
+
+/* Don't enable Dynamic port driver loading for simple driver. For simple,
+ * one can limit the port drivers by enabling CONFIG_LIMIT_PDS to
+ * required port drivers *
+ *
+ * Enable Dynamic port driver loading
+ *
+ * #define IGD_DPD_ENABLED 1 */
+
+/* Enable required port drivers. */
+#define CONFIG_LIMIT_PDS 1
+#define CONFIG_PD_ANALOG 0
+#define CONFIG_PD_LVDS   1
+#define CONFIG_PD_SDVO   1
+#define CONFIG_PD_TV     0 /* Integrated TV for NAPA */
+#define CONFIG_PD_CH7036  1
+
+#define CONFIG_LINK_PD_LVDS
+#define CONFIG_LINK_PD_SDVO
+#define CONFIG_LINK_PD_CH7036
+
+#define CONFIG_DECODE
+
+#define CONFIG_ST
+
+#include <config_default.h>
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/cfg/config_default.h b/drivers/gpu/drm/emgd/emgd/cfg/config_default.h
new file mode 100644
index 0000000..18e97f5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/cfg/config_default.h
@@ -0,0 +1,199 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: config_default.h
+ * $Revision: 1.16 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is used in conjunction with the platform's config.h to
+ *  gererate a full set of build defines. This file should provide defaults
+ *  for defines such that a platform's config.h can include only the
+ *  minimal set of non-standard options.
+ *  Defines should be named such that:
+ *  CONFIG_<FOO>: Is defined or undefined suitable for use with ifdef and
+ *  can be used with the build system's DIRS_FOO or OBJECTS_FOO. Any
+ *  CONFIG_FOO added here must also have an entry in config_helper.c.
+ *  CONFIG_ENABLE_FOO: Should be defined always and defined to a 1 or 0.
+ *  This is suitble for use in if(CONFIG_ENABLE_FOO) and expected that
+ *  a compiler will optimize away if(0)'s.
+ *  CONFIG_LIMIT_FOO: Should prevent some default set of FOO defines
+ *  from being included. For instance CONFIG_LIMIT_MODES prevents the
+ *  long default list of default modes from being used and instead the
+ *  platform's config.h must define the requested modes manually.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_CONFIG_DEFAULT_H
+#define _HAL_CONFIG_DEFAULT_H
+
+#include <igd_version.h>
+
+#ifndef CONFIG_MICRO
+#define CONFIG_FULL
+#endif
+
+#ifndef CONFIG_LIMIT_CORES
+#define CONFIG_PLB
+#define CONFIG_TNC
+#endif /* CONFIG_LIMIT_CORES */
+
+#ifdef CONFIG_DEPRECATED
+#if 0 /* WHT Modules need some updating */
+#define CONFIG_810
+#define CONFIG_810DC
+#define CONFIG_810E
+#define CONFIG_815
+#define CONFIG_830
+#define CONFIG_835
+#define CONFIG_845
+#define CONFIG_855
+#define CONFIG_865
+#define CONFIG_915GD
+#define CONFIG_915AL
+#define	CONFIG_945G
+#define	CONFIG_945GM
+#define	CONFIG_945GME
+#define CONFIG_Q35
+#define CONFIG_965G
+#define CONFIG_965GM
+#define CONFIG_CTG
+#define CONFIG_Q45
+#define CONFIG_PNV
+#endif
+#endif
+
+#ifndef CONFIG_LIMIT_MODULES
+#define CONFIG_INIT
+#define CONFIG_REG
+#define CONFIG_POWER
+#define CONFIG_MODE
+#define CONFIG_DSP
+#define CONFIG_PI
+#define CONFIG_PD
+#define CONFIG_APPCONTEXT
+#define CONFIG_OVERLAY
+#endif /* CONFIG_LIMIT_MODULES */
+
+#ifndef CONFIG_LIMIT_PDS
+#define CONFIG_PD_ANALOG
+#define CONFIG_PD_LVDS
+#define CONFIG_PD_TV
+#define CONFIG_PD_HDMI
+#define CONFIG_PD_SDVO
+#define CONFIG_PD_SOFTPD
+#define CONFIG_PD_CH7036
+#endif
+
+#ifdef CONFIG_DEPRECATED
+#ifndef CONFIG_LIMIT_PDS
+#define CONFIG_PD_SII164
+#define CONFIG_PD_CH7009
+#define CONFIG_PD_TL955
+#define CONFIG_PD_RGBA
+#define CONFIG_PD_NS2501
+#define CONFIG_PD_TH164
+#define CONFIG_PD_FS454
+#define CONFIG_PD_NS387
+#define CONFIG_PD_CX873
+#define CONFIG_PD_FS460
+#define CONFIG_PD_CH7017
+#define CONFIG_PD_TI410
+#endif
+#endif
+
+#ifndef CONFIG_DEBUG_FLAGS
+#define CONFIG_DEBUG_FLAGS			  \
+	0, /* Command Module */			  \
+		0,	/* DSP Module */		  \
+		0,	/* Mode Module */		  \
+		0,	/* Init Module */		  \
+		0,	/* Overlay Module */	  \
+		0,	/* Power Module */		  \
+		0,	/* 2D Module */			  \
+		0,	/* Blend Module */		  \
+		0,	/* State Module */		  \
+		0,	/* GMM Module */		  \
+		0,	/* Gart Module */		  \
+		0,	/* OAL Module */		  \
+		0,	/* Interrupt Module */	  \
+		0,	/* Port Driver Module */  \
+		0,	/* Video Decode Module */ \
+		0,	/* PVR 3-Ptr Disp Drv */  \
+		0,	/* Buffer Class Module */ \
+\
+		0,	/* Global Tracing */	  \
+		0,	/* Global Instructions */ \
+		0,	/* Global Debug */		  \
+\
+		0,	/* Verbose Blend Stats */ \
+		0,	/* Verbose Overlay Dump */\
+		0,	/* Verbose Cmd Dump */	  \
+		0,	/* Verbose GMM Dump */    \
+		0	/* Verbose Shader Dump */
+
+#endif
+
+#ifndef CONFIG_DEBUG_IAL_FLAGS
+#define CONFIG_DEBUG_IAL_FLAGS 0
+#endif
+
+/* we ensure IAL's that do not support hw binning has this flag as '0' */
+#ifndef CONFIG_ENABLE_BINNING
+#define CONFIG_ENABLE_BINNING 0
+#endif
+
+#ifndef CONFIG_ENABLE_THREADS
+#define CONFIG_ENABLE_THREADS 0
+#endif
+
+
+
+/*
+ * These Meta-Defines should not be set in the config.h. They are enabled
+ * here based on more granular defines that come from config.h. For instance
+ * CONFIG_NAP should be enabled when any chips from the NAP family are enabled.
+ */
+#if defined(CONFIG_810) || defined(CONFIG_810DC) || defined(CONFIG_810E) ||\
+	defined(CONFIG_815)
+#define CONFIG_WHT
+#endif
+
+#if defined(CONFIG_830) || defined(CONFIG_835) || defined(CONFIG_845) ||\
+	defined(CONFIG_855) || defined(CONFIG_865)
+#define CONFIG_ALM
+#endif
+
+#if defined(CONFIG_915GD) || defined(CONFIG_915AL) || defined(CONFIG_945G) ||\
+	defined(CONFIG_945GM) || defined(CONFIG_945GME) || defined(CONFIG_Q35) ||\
+	defined(CONFIG_PNV)
+#define CONFIG_NAP
+#endif
+
+#if defined(CONFIG_965G) || defined(CONFIG_965GM) || \
+	defined(CONFIG_CTG) || defined(CONFIG_Q45)
+#define CONFIG_GN4
+#endif
+
+
+#endif /* _HAL_CONFIG_DEFAULT_H */
+
diff --git a/drivers/gpu/drm/emgd/emgd/cfg/config_helper.c b/drivers/gpu/drm/emgd/emgd/cfg/config_helper.c
new file mode 100644
index 0000000..d758648
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/cfg/config_helper.c
@@ -0,0 +1,244 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: config_helper.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include <stdio.h>
+
+#include <config.h>
+
+int main(int argc, char **argv)
+{
+	char configs[] = ""
+#ifdef CONFIG_MICRO
+		"MICRO "
+#else
+		"FULL "
+#endif
+#ifdef CONFIG_WHT
+		"WHT "
+#endif
+#ifdef CONFIG_ALM
+		"ALM "
+#endif
+#ifdef CONFIG_NAP
+		"NAP "
+#endif
+#ifdef CONFIG_GN4
+		"GN4 "
+#endif
+#ifdef CONFIG_PLB
+		"PLB "
+#endif
+#ifdef CONFIG_TNC
+		"TNC "
+#endif
+#ifdef CONFIG_MODE
+		"MODE "
+#endif
+#ifdef CONFIG_DSP
+		"DSP "
+#endif
+#ifdef CONFIG_PI
+		"PI "
+#endif
+#ifdef CONFIG_PD
+		"PD "
+#endif
+#ifdef CONFIG_INIT
+		"INIT "
+#endif
+#ifdef CONFIG_INTERRUPT
+		"INTERRUPT "
+#endif
+#ifdef CONFIG_GART
+		"GART "
+#endif
+#ifdef CONFIG_REG
+		"REG "
+#endif
+#ifdef CONFIG_RESET
+		"RESET "
+#endif
+#ifdef CONFIG_POWER
+		"POWER "
+#endif
+#ifdef CONFIG_GMM
+		"GMM "
+#endif
+#ifdef CONFIG_MICRO_GMM
+		"MICRO_GMM "
+#endif
+#ifdef CONFIG_APPCONTEXT
+		"APPCONTEXT "
+#endif
+#ifdef CONFIG_CMD
+		"CMD "
+#endif
+#ifdef CONFIG_2D
+		"2D "
+#endif
+#ifdef CONFIG_BLEND
+		"BLEND "
+#endif
+#ifdef CONFIG_OVERLAY
+		"OVERLAY "
+#endif
+#ifdef CONFIG_DECODE
+		"DECODE "
+#endif
+		/*
+		 * Port Driver Compile Options
+		 */
+#ifdef CONFIG_PD_ANALOG
+		"PD_ANALOG "
+#endif
+#ifdef CONFIG_PD_SII164
+		"PD_SII164 "
+#endif
+#ifdef CONFIG_PD_CH7009
+		"PD_CH7009 "
+#endif
+#ifdef CONFIG_PD_TL955
+		"PD_TL955 "
+#endif
+#ifdef CONFIG_PD_RGBA
+		"PD_RGBA "
+#endif
+#ifdef CONFIG_PD_NS2501
+		"PD_NS2501 "
+#endif
+#ifdef CONFIG_PD_TH164
+		"PD_TH164 "
+#endif
+#ifdef CONFIG_PD_FS454
+		"PD_FS454 "
+#endif
+#ifdef CONFIG_PD_NS387
+		"PD_NS387 "
+#endif
+#ifdef CONFIG_PD_CX873
+		"PD_CX873 "
+#endif
+#ifdef CONFIG_PD_LVDS
+		"PD_LVDS "
+#endif
+#ifdef CONFIG_PD_FS460
+		"PD_FS460 "
+#endif
+#ifdef CONFIG_PD_CH7017
+		"PD_CH7017 "
+#endif
+#ifdef CONFIG_PD_TI410
+		"PD_TI410 "
+#endif
+#ifdef CONFIG_PD_TV
+		"PD_TV "
+#endif
+#ifdef CONFIG_PD_HDMI
+		"PD_HDMI "
+#endif
+#ifdef CONFIG_PD_SDVO
+		"PD_SDVO "
+#endif
+#ifdef CONFIG_PD_SOFTPD
+		"PD_SOFTPD "
+#endif
+#ifdef CONFIG_PD_CH7036
+		"PD_CH7036 "
+#endif	
+		/*
+		 * Port Driver Link Options
+		 */
+#ifdef CONFIG_LINK_PD_ANALOG
+		"LINK_PD_ANALOG "
+#endif
+#ifdef CONFIG_LINK_PD_SII164
+		"LINK_PD_SII164 "
+#endif
+#ifdef CONFIG_LINK_PD_CH7009
+		"LINK_PD_CH7009 "
+#endif
+#ifdef CONFIG_LINK_PD_TL955
+		"LINK_PD_TL955 "
+#endif
+#ifdef CONFIG_LINK_PD_RGBA
+		"LINK_PD_RGBA "
+#endif
+#ifdef CONFIG_LINK_PD_NS2501
+		"LINK_PD_NS2501 "
+#endif
+#ifdef CONFIG_LINK_PD_TH164
+		"LINK_PD_TH164 "
+#endif
+#ifdef CONFIG_LINK_PD_FS454
+		"LINK_PD_FS454 "
+#endif
+#ifdef CONFIG_LINK_PD_NS387
+		"LINK_PD_NS387 "
+#endif
+#ifdef CONFIG_LINK_PD_CX873
+		"LINK_PD_CX873 "
+#endif
+#ifdef CONFIG_LINK_PD_LVDS
+		"LINK_PD_LVDS "
+#endif
+#ifdef CONFIG_LINK_PD_FS460
+		"LINK_PD_FS460 "
+#endif
+#ifdef CONFIG_LINK_PD_CH7017
+		"LINK_PD_CH7017 "
+#endif
+#ifdef CONFIG_LINK_PD_TI410
+		"LINK_PD_TI410 "
+#endif
+#ifdef CONFIG_LINK_PD_TV
+		"LINK_PD_TV "
+#endif
+#ifdef CONFIG_LINK_PD_HDMI
+		"LINK_PD_HDMI "
+#endif
+#ifdef CONFIG_LINK_PD_SDVO
+		"LINK_PD_SDVO "
+#endif
+#ifdef CONFIG_LINK_PD_SOFTPD
+		"LINK_PD_SOFTPD "
+#endif
+#ifdef CONFIG_LINK_PD_CH7036
+      "LINK_PD_CH7036 "
+#endif
+
+#ifdef CONFIG_COPP
+		"COPP "
+#endif
+		;
+	printf("%s\n", configs);
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_global.c b/drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_global.c
new file mode 100644
index 0000000..52bc62e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_global.c
@@ -0,0 +1,34 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_global.c
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+#include <config.h>
+#include <igd_debug.h>
+
+/* Global debug flag has been moved to egd_drm/emgd/drm/emgd_drv.c */
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_init.c b/drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_init.c
new file mode 100644
index 0000000..335a796
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/cmn/igd_init.c
@@ -0,0 +1,918 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_init.c
+ * $Revision: 1.24 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the implementation of the core init module. It
+ *  is responsible from initializing the HAL and the device and gathering
+ *  all necessary general device information. This module is partially
+ *  optional such that portions may be disabled to save code space.
+ *  When CONFIG_MICRO is enabled:
+ *  Firmware parameters are NOT read.
+ *  Only the first device instance is queried and configured.
+ *  Revision ID is NOT queried, it is assumed to be 0.
+ *  MMIO regions are not discovered or mapped.
+ *  FB memory is not discovered or mapped.
+ *  Shutdown is a no-op.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.init
+
+#include <io.h>
+#include <pci.h>
+#include <memory.h>
+#include <memmap.h>
+
+#include <igd_init.h>
+#include <igd_pwr.h>
+#include <igd_errno.h>
+#include <igd_mode.h>
+#include <igd_debug.h>
+
+#include <context.h>
+#include <cmd.h>
+#include <reset.h>
+#include <dsp.h>
+#include <utils.h>
+#include <general.h>
+#include <intelpci.h>
+#include <mode_access.h>
+#include <module_init.h>
+#include <mode.h>
+
+#include "init_dispatch.h"
+
+/* OAL header */
+#include <sched.h>
+
+/*!
+ * @addtogroup core_group
+ * @{
+ */
+
+
+unsigned long _sgx_base, _msvdx_base, _topaz_base;
+
+/* Notes: If the bus is of value 0xFFFF, then the particular
+ * device is searched for in the whole PCI topology
+ */
+typedef struct _iegd_pci {
+	unsigned short vendor_id;
+	unsigned short device_id;
+	unsigned short bus;
+	unsigned short dev;
+	unsigned short func;
+} iegd_pci_t;
+
+static iegd_pci_t intel_pci_device_table[] = {
+#ifdef CONFIG_810
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_810, 0, 2, 0},
+#endif
+#ifdef CONFIG_810DC
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_810DC, 0, 2, 0},
+#endif
+#ifdef CONFIG_810E
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_810E, 0, 2, 0},
+#endif
+#ifdef CONFIG_815
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_815, 0, 2, 0},
+#endif
+#ifdef CONFIG_855
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_855, 0, 2, 0},
+#endif
+#ifdef CONFIG_830
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_830M, 0, 2, 0},
+#endif
+#ifdef CONFIG_835
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_835, 0, 2, 0},
+#endif
+#ifdef CONFIG_845
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_845G, 0, 2, 0},
+#endif
+#ifdef CONFIG_865
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_865G, 0, 2, 0},
+#endif
+#ifdef CONFIG_915GD
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_915GD, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_910GL, 0, 2, 0},
+#endif
+#ifdef CONFIG_915AL
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_915AL, 0, 2, 0},
+#endif
+#ifdef CONFIG_945G
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_945G, 0, 2, 0},
+#endif
+#ifdef CONFIG_945GM
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_945GM, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_945GME, 0, 2, 0},
+#endif
+#ifdef CONFIG_Q35
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_Q35, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_Q35A2, 0, 2, 0},
+#endif
+#ifdef CONFIG_965G
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_965G, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_G965, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_Q965, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_946GZ, 0, 2, 0},
+#endif
+#ifdef CONFIG_965GM
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_GME965, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_GM965, 0, 2, 0},
+#endif
+#ifdef CONFIG_CTG
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_CTG, 0, 2, 0},
+#endif
+#ifdef CONFIG_PLB
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_PLB, 0, 2, 0},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_TNC, 0, 2, 0},
+#ifdef CONFIG_MSRT
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_TNC_A0, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_LNC, 0, 2, 0},
+#endif
+#endif
+#ifdef CONFIG_Q45
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_ELK, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_Q45, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_G45, 0, 2, 0},
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_VGA_G41, 0, 2, 0},
+#endif
+
+};
+
+
+/*
+ * On platforms with multiple PCI devices (currently only Atom E6xx), we
+ * need to disable legacy VGA decoding on the second device, otherwise
+ * the VGA arbiter will prevent DRI from being used.  Keep a list of
+ * devices we need to disable this on.
+ */
+static iegd_pci_t disabled_legacy_vga_list[] = {
+	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDVO_TNC, 0, 0, 0},
+};
+
+#define MAX_PCI_DEVICE_SUPPORTED \
+	(sizeof(intel_pci_device_table)/sizeof(intel_pci_device_table[0]))
+#define MAX_LEGACY_VGA_DISABLE \
+	(sizeof(disabled_legacy_vga_list)/sizeof(disabled_legacy_vga_list[0]))
+
+
+static dispatch_table_t init_dispatch_table[] = {
+
+	DISPATCH_PLB(&init_dispatch_plb)
+	DISPATCH_TNC(&init_dispatch_tnc)
+#ifdef CONFIG_MSRT
+	DISPATCH_TNC_A0(&init_dispatch_tnc_a0)
+	DISPATCH_LNC(&init_dispatch_lnc)
+#endif
+	DISPATCH_END
+};
+
+
+
+static init_dispatch_t *init_dispatch;
+
+
+/*---------------------------------------------------------------------------
+ * Optional Init Module Components
+ *--------------------------------------------------------------------------*/
+#ifndef CONFIG_MICRO
+
+/*!
+ * This function allows for calling the igd_get_param function with a
+ * display handle instead of a driver handle. This version is exported in
+ * the dispatch table.
+ *
+ * @param display_handle
+ * @param id
+ * @param value
+ *
+ * @return igd_get_param()
+ */
+static int _igd_get_param(igd_display_h display_handle,
+	unsigned long id,
+	unsigned long *value)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	return igd_get_param((igd_driver_h)display->context, id, value);
+}
+
+/*!
+ * This function allows for calling the igd_set_param function with a
+ * display handle instead of a driver handle. This version is exported in
+ * the dispatch table.
+ *
+ * @param display_handle
+ * @param id
+ * @param value
+ *
+ * @return igd_set_param()
+ */
+static int _igd_set_param(igd_display_h display_handle,
+	unsigned long id,
+	unsigned long value)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	return igd_set_param((igd_driver_h)display->context, id, value);
+}
+
+/*!
+ * This function should never be called directly. It comprises the optional
+ * portion of the igd_set_param function which should be used instead.
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return set_param()
+ * @return 0
+ */
+static int _init_set_param(igd_context_t *context,
+	unsigned long id,
+	unsigned long value)
+{
+
+	switch(id) {
+	case IGD_PARAM_DEBUG_MASK:
+		*((unsigned long *)emgd_debug) = value;
+		break;
+	default:
+		return init_dispatch->set_param(context, id, value);
+	}
+
+	return 0;
+}
+
+/*!
+ * This function should never be called directly. It comprises the optional
+ * portion of the igd_get_param function which should be used instead.
+ *
+ * @param display_handle
+ * @param id
+ * @param value
+ *
+ * @return set_param()
+ * @return 0
+ */
+static int _init_get_param(igd_display_h display_handle,
+	unsigned long id,
+	unsigned long *value)
+{
+	switch(id) {
+	case IGD_PARAM_DEBUG_MASK:
+		*value = *((unsigned long *)emgd_debug);
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
+/*!
+ * Hook up the dispatch pointers. These are only available when the
+ * full init module is compiled in.
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void _init_dispatch(igd_context_t *context)
+{
+	/* Hook up top level dispatch table functions owner by init */
+	context->dispatch.get_param           = _igd_get_param;
+	context->dispatch.set_param           = _igd_set_param;
+	return;
+}
+
+/*!
+ * Shutdown all modules in the required order. Optional modules must
+ * only be called if they exist.
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void shutdown_modules(igd_context_t *context)
+{
+	EMGD_TRACE_ENTER;
+
+	if(context->mod_dispatch.shutdown_2d) {
+		context->mod_dispatch.shutdown_2d(context);
+	}
+	if(context->mod_dispatch.blend_shutdown) {
+		context->mod_dispatch.blend_shutdown(context);
+	}
+	if(context->mod_dispatch.interrupt_shutdown) {
+		context->mod_dispatch.interrupt_shutdown(context);
+	}
+	if(context->mod_dispatch.appcontext_shutdown) {
+		context->mod_dispatch.appcontext_shutdown(context);
+	}
+	if(context->mod_dispatch.reset_shutdown) {
+		context->mod_dispatch.reset_shutdown(context);
+	}
+	if(context->mod_dispatch.mode_shutdown) {
+		context->mod_dispatch.mode_shutdown(context);
+	}
+	if(context->mod_dispatch.pwr_shutdown) {
+		context->mod_dispatch.pwr_shutdown(context);
+	}
+	if(context->mod_dispatch.overlay_shutdown) {
+		context->mod_dispatch.overlay_shutdown(context);
+	}
+	if(context->mod_dispatch.cmd_shutdown) {
+		context->mod_dispatch.cmd_shutdown(context);
+	}
+	/*
+	 * GMM is not optional shutdown must exist.
+	 */
+	gmm_shutdown(context);
+
+	/*
+	 * Reg module must be last to restore the state of the device to the
+	 * way it was before the driver started.
+	 */
+	EMGD_DEBUG("post reg_shutdown: %p", context->mod_dispatch.reg_shutdown);
+	if(context->mod_dispatch.reg_shutdown) {
+		context->mod_dispatch.reg_shutdown(context);
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+#endif
+
+/*!
+ * Empty idle function. This insures that anyone can call
+ * dispatch->idle() in any configuration. If there is a command
+ * module this will get replaced with a real idle function.
+ *
+ * @param driver_handle
+ *
+ * @return 0
+ */
+static int empty_idle(igd_driver_h driver_handle)
+{
+	return 0;
+}
+
+/*!
+ * Empty sync function. This insures that anyone can call
+ * dispatch->sync() in any configuration.
+ *
+ * @param display_handle pointer to an IGD_DISPLAY pointer returned
+ *    from a successful call to dispatch->alter_displays().
+ *
+ * @param priority The command queue to use. IGD_PRIORITY_NORMAL is
+ *    correct for most circumstances.
+ *
+ * @param sync The sync identifier that will be populated and returned
+ *    during the call. To insert a new sync, this should be passed
+ *    containing 0 (A pointer to a zero). To check the status of an
+ *    existing sync pass the value returned from a previous call to
+ *    this function.
+ *
+ * @param flags Sync flags.
+ *
+ * @returns
+ *   0: On Success
+ *   -IGD_ERROR_BUSY: When the sync is not yet complete
+ */
+static int empty_sync(igd_display_h display_handle, int priority,
+		unsigned long *sync, unsigned long flags)
+{
+	return 0;
+}
+
+/*!
+ * Non-Optional Init Module Components
+ *
+ * @param found_device
+ * @param pdev
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_NODEV on failure
+ */
+static int detect_device(iegd_pci_t **found_device,
+	os_pci_dev_t *pdev)
+{
+	int i;
+
+	/* Scan the PCI bus for supported device */
+	for(i = 0; i < MAX_PCI_DEVICE_SUPPORTED; i++) {
+
+		*pdev = OS_PCI_FIND_DEVICE(intel_pci_device_table[i].vendor_id,
+			intel_pci_device_table[i].device_id,
+			intel_pci_device_table[i].bus,
+			intel_pci_device_table[i].dev,
+			intel_pci_device_table[i].func,
+			(os_pci_dev_t)0);
+
+		if(*pdev) {
+			*found_device = &intel_pci_device_table[i];
+			break;
+		}
+	}
+	if(!*pdev) {
+		EMGD_ERROR("No supported VGA devices found.");
+		return -IGD_ERROR_NODEV;
+	}
+
+	EMGD_DEBUG("VGA device found: 0x%x", (*found_device)->device_id);
+
+	return 0;
+}
+
+/* This is currently a global context, because the context is needed
+ * in io.c for vbios in OS_READx and OS_WRITEx functions. */
+igd_context_t *fixme_vbios_context;
+
+/* By declaring io_mapped and io_base as globals, we no longer need to
+ * include context.h in the vbios common io.c */
+
+/* Device 0:2:0 io_base */
+unsigned char io_mapped;      /* True for io mapped MMIO space */
+unsigned short io_base;
+
+/* Device 0:2:0 [VGA device] io_base */
+unsigned char io_mapped_lvds; /* True for io mapped MMIO space */
+unsigned short io_base_lvds;
+
+/* Device 0:3:0 [SDVO device] io_base */
+unsigned char io_mapped_sdvo; /* True for io mapped MMIO space */
+unsigned short io_base_sdvo;
+
+/* Device 0:31:0 [LPC device] io_base */
+unsigned char io_mapped_lpc; /* True for io mapped MMIO space */
+unsigned short io_base_lpc;
+
+/* Device 6:0:1 [STMicro SDVO device] io_base */
+unsigned char io_mapped_sdvo_st; /* True for io mapped MMIO space */
+unsigned short io_base_sdvo_st;
+unsigned char io_mapped_sdvo_st_gpio; /* True for io mapped MMIO space */
+unsigned short io_base_sdvo_st_gpio;
+
+
+/*!
+ * This function is directly exported.
+ *
+ * @param found_device
+ * @param pdev
+ *
+ * @return igd_driver_h
+ * @return NULL on failure
+ */
+igd_driver_h igd_driver_init( igd_init_info_t *init_info )
+{
+	igd_context_t *context;
+	os_pci_dev_t pdev = (os_pci_dev_t)NULL;
+	os_pci_dev_t vga_disable_dev;
+	iegd_pci_t *found_device;
+	int ret;
+	int i;
+
+	EMGD_TRACE_ENTER;
+
+	/* Allocate a context */
+	context = (void *) OS_ALLOC(sizeof(igd_context_t));
+	fixme_vbios_context = context;
+	if(!context) {
+		EMGD_ERROR_EXIT("igd_driver_init failed to create context");
+		return NULL;
+	}
+	OS_MEMSET(context, 0, sizeof(igd_context_t));
+
+	/* Search VGA devices for a supported one */
+	ret = detect_device(&found_device, &pdev);
+	if(ret) {
+		OS_FREE(context);
+		return NULL;
+	}
+
+	/*
+	 * Some platforms (currently only Atom E6xx) use two PCI devices (the
+	 * second device being for SDVO) and this causes the VGA arbiter to get
+	 * involved.  Legacy VGA decoding must be disabled for all PCI devices
+	 * except one, otherwise the VGA arbiter will prevent DRI usage in the
+	 * X server.
+	 */
+	for (i = 0; i < MAX_LEGACY_VGA_DISABLE; i++) {
+		vga_disable_dev = os_pci_find_device(PCI_VENDOR_ID_INTEL,
+				PCI_DEVICE_ID_SDVO_TNC, 0xFFFF, 0, 0, NULL);
+		if (vga_disable_dev) {
+			printk(KERN_INFO "VGA arbiter detected; disabling legacy VGA"
+					" decoding on SDVO device\n");
+			os_pci_disable_legacy_vga_decoding(vga_disable_dev);
+			os_pci_free_device(vga_disable_dev);
+		}
+	}
+
+	context->device_context.did = found_device->device_id;
+	init_dispatch = (init_dispatch_t *)dispatch_acquire(context,
+		init_dispatch_table);
+
+	if(!init_dispatch) {
+		EMGD_ERROR_EXIT("No dispatch found for listed device");
+		return NULL;
+	}
+
+	ret = init_dispatch->query(context, init_dispatch, pdev, &init_info->bus,
+		&init_info->slot, &init_info->func);
+	if(ret) {
+		OS_FREE(context);
+		EMGD_ERROR_EXIT("Device Dependent Query Failed");
+		return NULL;
+	}
+
+	/* init info */
+	init_info->vendor_id = found_device->vendor_id;
+	init_info->device_id = found_device->device_id;
+	init_info->name = init_dispatch->name;
+	init_info->chipset = init_dispatch->chipset;
+	init_info->default_pd_list = init_dispatch->default_pd_list;
+
+	EMGD_TRACE_EXIT;
+
+	return (igd_driver_h)context;
+}
+
+/*!
+ * This function is directly exported.
+ *
+ * @param driver_handle
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int igd_driver_config(igd_driver_h driver_handle)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(context, "Null context!", -IGD_ERROR_INVAL);
+
+	ret = init_dispatch->config(context, init_dispatch);
+	if(ret) {
+		EMGD_ERROR_EXIT("Device Dependent Config Failed");
+		return ret;
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Initialize modules in the required order. Optional modules must be called
+ * with their initalization macro to ensure that they are not called when
+ * their option is not enabled.
+ *
+ * @param params
+ * @param context
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+/* FIXME: All modules should get params from mod_dispatch */
+static int init_modules(igd_param_t *params, igd_context_t *context)
+{
+	unsigned int ret;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Reg module must be first so that the state of the device can be
+	 * saved before anything else is touched.
+	 */
+	ret = REG_INIT(context, (params->preserve_regs)?IGD_DRIVER_SAVE_RESTORE:0);
+	if (ret) {
+		EMGD_DEBUG("Error initializing register module");
+	}
+
+	/*
+	 *  GMM is not optional. Its init function must exist.
+	 */
+	ret = gmm_init(context, params->page_request, params->max_fb_size);
+	if(ret) {
+		EMGD_ERROR_EXIT("GMM Module Init Failed");
+		return ret;
+	}
+
+	ret = CMD_INIT(context);
+	if(ret) {
+		EMGD_ERROR_EXIT("Command Module Init Failed");
+		return ret;
+	}
+
+	/*
+	 *  Mode is not optional. Its init function must exist.
+	 */
+	ret = mode_init(context);
+	if (ret) {
+		EMGD_ERROR_EXIT("Mode Module Init Failed");
+		return ret;
+	}
+
+	ret = APPCONTEXT_INIT(context);
+	if (ret) {
+		EMGD_ERROR_EXIT("Appcontext Module Init Failed");
+		return ret;
+	}
+
+	ret = OVERLAY_INIT(context, params);
+	if(ret) {
+		EMGD_ERROR_EXIT("Overlay Module Init Failed");
+		return ret;
+	}
+
+	ret = PWR_INIT(context);
+	if(ret) {
+		EMGD_DEBUG("Error initializing power module");
+	}
+
+	ret = RESET_INIT(context);
+	if(ret) {
+		EMGD_DEBUG("Error initializing reset module");
+	}
+
+	ret = OS_INIT_INTERRUPT(context->device_context.did,
+		context->device_context.virt_mmadr);
+	if(ret) {
+		EMGD_ERROR_EXIT("Interrupt Module Init Failed");
+		return ret;
+	}
+
+	ret = BLEND_INIT(context);
+	if(ret) {
+		EMGD_DEBUG("Error initializing blend module");
+	}
+
+	ret = INIT_2D(context);
+	if(ret) {
+		EMGD_DEBUG("Error initializing 2d module");
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function is directly exported.
+ *
+ * @param driver_handle
+ * @param dsp
+ * @param params
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int igd_module_init(igd_driver_h driver_handle,
+	igd_dispatch_t **dsp,
+	igd_param_t *params)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	device_context_t *device;
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	device = &context->device_context;
+	context->device_context.power_state = IGD_POWERSTATE_D0;
+	context->mod_dispatch.init_params = params;
+	context->dispatch.idle = empty_idle;
+	context->dispatch.sync = empty_sync;
+	context->mod_dispatch.in_dih_clone_mode = 0;
+	context->mod_dispatch.dih_clone_display = 0;
+	context->mod_dispatch.fb_blend_ovl_override = 0;
+
+	/* Intialize IGD Modules */
+	ret = init_modules(params, context);
+	if (ret) {
+		EMGD_ERROR_EXIT("Init Modules Failed");
+		return ret;
+	}
+
+	OPT_MICRO_VOID_CALL(_init_dispatch(context));
+
+	*dsp = &context->dispatch;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function is directly exported.
+ *
+ * @param driver_handle
+ * @param info
+ *
+ * @return 0
+ */
+int igd_get_config_info(igd_driver_h driver_handle,
+	igd_config_info_t *config_info)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(context, "Null context!", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(config_info, "Null config_info!", -IGD_ERROR_INVAL);
+
+	OS_MEMSET(config_info, 0, sizeof(igd_config_info_t));
+
+	/* Config information already obtained from driver_config() */
+	config_info->mmio_base_phys = context->device_context.mmadr;
+	config_info->mmio_base_virt = context->device_context.virt_mmadr;
+	config_info->gtt_memory_base_phys = context->device_context.fb_adr;
+	/* config_info->gtt_memory_base_virt = context->device_context.virt_fb_adr; */
+	config_info->gtt_memory_size = context->device_context.mem_size;
+	config_info->revision_id = context->device_context.rid;
+	config_info->hw_status_offset = context->device_context.hw_status_offset;
+	config_info->stolen_memory_base_virt = 0; /* FIXME: remove this */
+
+	/* get the portions held in the dsp module */
+	if(context->mod_dispatch.dsp_get_config_info) {
+		context->mod_dispatch.dsp_get_config_info(context, config_info);
+	}
+	/* get the portions held in the pi module */
+	if(context->mod_dispatch.pi_get_config_info) {
+		context->mod_dispatch.pi_get_config_info(context, config_info);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function is directly exported. When compiled without the init
+ * module the function calls the DD layer get_param function. When the
+ * full init module is included this function calls _init_get_param to
+ * first get the DI parameters then calls the DD layer.
+ *
+ * @param driver_handle
+ * @param id
+ * @param value
+ *
+ * @return get_param()
+ */
+int igd_get_param(igd_driver_h driver_handle,
+	unsigned long id,
+	unsigned long *value)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+
+	EMGD_ASSERT(context, "Null Driver Handle", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(value, "Null Value", -IGD_ERROR_INVAL);
+
+	OPT_MICRO_CALL(_init_get_param(driver_handle, id, value));
+
+	return init_dispatch->get_param(context, id, value);
+}
+
+/*!
+ * This function is directly exported. When compiled without the init
+ * module the function does nothing and returns 0. When the full init
+ * module is included this function calls _init_set_param and returns
+ * the result.
+ *
+ * @param driver_handle
+ * @param id
+ * @param value
+ *
+ * @return 0
+ */
+int igd_set_param(igd_driver_h driver_handle,
+	unsigned long id,
+	unsigned long value)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+
+	EMGD_ASSERT(context, "Null Driver Handle", -IGD_ERROR_INVAL);
+
+	OPT_MICRO_CALL(_init_set_param(context, id, value));
+	return 0;
+}
+
+/*!
+ * This function is exported directly. It will shutdown an instance
+ * of the HAL that was initialized with igd_driver_init.
+ *
+ * Since the symbol is exported as part of the documented API it must
+ * always exist, however it becomes an empty function when the init
+ * module is not fully included.
+ *
+ * @param driver_handle
+ *
+ * @return void
+ */
+void igd_driver_shutdown(igd_driver_h driver_handle)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(context, "Null Driver Handle", );
+
+
+	/* Shutdown the device context */
+	init_dispatch->shutdown(context);
+
+	/* release the driver's context */
+	if(context) {
+		EMGD_DEBUG("Freeing context");
+		OS_FREE(context);
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ * This function is exported. It will shutdown most of the display
+ * functions.
+ *
+ * @param driver_handle
+ *
+ * @return void
+ */
+void igd_driver_shutdown_hal(igd_driver_h driver_handle)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(context, "Null Driver Handle", );
+
+	if(context->device_context.power_state != IGD_POWERSTATE_D0) {
+		return;
+	}
+
+	/* Shutdown Modules */
+	shutdown_modules(context);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+/*!
+ * This function is exported directly. It will shutdown an instance
+ * of the HAL that was initialized with igd_driver_init.
+ *
+ * Since the symbol is exported as part of the documented API it must
+ * always exist, however it becomes an empty function when the init
+ * module is not fully included.
+ *
+ * @param driver_handle
+ *
+ * @return void
+ */
+void igd_query_2d_caps_hwhint(igd_driver_h driver_handle, 
+		unsigned long caps_val,
+		unsigned long *status)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	
+	EMGD_TRACE_ENTER;
+
+	if (init_dispatch->query_2d_caps_hwint != NULL){
+		init_dispatch->
+			query_2d_caps_hwint(context, caps_val, status);
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+/*----------------------------------------------------------------------------
+ * File Revision History
+ * $Id: igd_init.c,v 1.24 2011/09/30 07:53:25 rlim Exp $
+ * $Source: /nfs/fm/proj/eia/cvsroot/koheo/linux/egd_drm/emgd/core/init/cmn/igd_init.c,v $
+ *----------------------------------------------------------------------------
+ */
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/cmn/init_dispatch.h b/drivers/gpu/drm/emgd/emgd/core/init/cmn/init_dispatch.h
new file mode 100644
index 0000000..7c58997
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/cmn/init_dispatch.h
@@ -0,0 +1,65 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: init_dispatch.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _INIT_DISPATCH_H
+#define _INIT_DISPATCH_H
+
+#include <pci.h>
+
+#include <dispatch.h>
+
+/*
+ * Note: Platforms extend this data structure so the pointer can be used
+ * as either this DI dispatch or cast to the DD dipatch.
+ */
+typedef struct _init_dispatch {
+	char *name;
+	char *chipset;
+	char *default_pd_list;
+	int (*query)(igd_context_t *context, struct _init_dispatch *dispatch,
+		os_pci_dev_t vga_dev, unsigned int *bus, unsigned int *slot,
+		unsigned int *func);
+	int (*config)(igd_context_t *context, struct _init_dispatch *dispatch);
+	int (*set_param)(igd_context_t *context, unsigned long id,
+		unsigned long value);
+	int (*get_param)(igd_context_t *context, unsigned long id,
+		unsigned long *value);
+	void (*shutdown)(igd_context_t *context);
+	int (*query_2d_caps_hwint) (igd_context_t *context,
+		unsigned long caps_val, unsigned long *status);
+} init_dispatch_t;
+
+extern init_dispatch_t init_dispatch_plb;
+extern init_dispatch_t init_dispatch_tnc;
+extern init_dispatch_t init_dispatch_tnc_a0;
+extern init_dispatch_t init_dispatch_lnc;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/plb/init_plb.c b/drivers/gpu/drm/emgd/emgd/core/init/plb/init_plb.c
new file mode 100644
index 0000000..d6a92ac
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/plb/init_plb.c
@@ -0,0 +1,458 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: init_plb.c
+ * $Revision: 1.19 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.init
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+#include <gart.h>
+
+#include <memory.h>
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+#include <igd_gart.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+
+#include "../cmn/init_dispatch.h"
+#include "/usr/include/linux/pci_regs.h"
+/*!
+ * @addtogroup core_group
+ * @{
+ */
+
+static int bus_master_enable_plb(platform_context_plb_t *platform_context);
+static int full_config_vga_plb(igd_context_t *context,
+	init_dispatch_t *dispatch);
+static int get_stolen_mem_plb(igd_context_t *context, unsigned long *pages);
+
+int full_get_param_plb(igd_context_t *context,
+	unsigned long id,
+	unsigned long *value);
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ * @param vga_dev
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+int get_revision_id_plb(igd_context_t *context,
+	os_pci_dev_t vga_dev)
+{
+	EMGD_TRACE_ENTER;
+
+	/* Read RID */
+	if(OS_PCI_READ_CONFIG_8(vga_dev, PCI_RID,
+			(unsigned char *)&context->device_context.rid)) {
+		EMGD_ERROR_EXIT("Error occured reading RID");
+		return -IGD_ERROR_NODEV;
+	}
+
+	EMGD_DEBUG(" rid = 0x%lx", context->device_context.rid);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ *
+ * @return 1 on failure
+ * @return 0 on success
+ */
+int full_config_plb(igd_context_t *context,
+	init_dispatch_t *dispatch)
+{
+	unsigned long /* FIXME - reserved_mem, */ graphics_frequency ;
+	platform_context_plb_t *platform_context;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	platform_context = (platform_context_plb_t *)context->platform_context;
+
+	_sgx_base = 0x40000;
+	_msvdx_base = 0x50000;
+
+	/*
+	 * Enable bus mastering for platforms whose BIOS did not perform this
+	 * task for us.
+	 */
+	ret = bus_master_enable_plb(platform_context);
+	if(ret) {
+		EMGD_ERROR("Error: Enabling bus master");
+	}
+
+	/* Config VGA */
+	ret = full_config_vga_plb(context, dispatch);
+	if(ret) {
+		EMGD_ERROR_EXIT("Config VGA Failed");
+		return ret;
+	}
+
+	get_stolen_mem_plb(context, &context->device_context.reserved_mem);
+
+#if 0 /* FIXME - WHY IS THIS RETURNING 0 AND SETTING reserved_mem TO 0 ALSO? */
+	/* Get mem reservation param if it exists */
+	if(!full_get_param_plb(context, IGD_PARAM_MEM_RESERVATION,
+			&reserved_mem)) {
+		context->device_context.reserved_mem = reserved_mem;
+	}
+#endif
+
+	/* Get graphics frequency param if it exists */
+	if(!full_get_param_plb(context, IGD_PARAM_GFX_FREQ,
+			&graphics_frequency)) {
+		context->device_context.gfx_freq = (unsigned short)graphics_frequency;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param platform_context
+ *
+ * @return -1 on failure
+ * @return 0 on success
+ */
+static int bus_master_enable_plb(platform_context_plb_t *platform_context){
+	int ret;
+	unsigned char tmp;
+
+	EMGD_TRACE_ENTER;
+
+	ret = OS_PCI_READ_CONFIG_8(platform_context->pcidev0, PCI_COMMAND_MASTER, &tmp);
+	if(ret) {
+		EMGD_ERROR_EXIT("PCI read of bus master");
+		return -1;
+	}
+
+	/*
+	 * Get Bit 2, 1, and 0 and see if it is == 1
+	 * all 3 bits has to be enabled. This is to enable register read/write
+	 * in the case of a PCI card being added
+	 */
+	if((tmp & 0x7) != 0x7 ) {
+
+		tmp |= 0x7;
+		ret = OS_PCI_WRITE_CONFIG_8(platform_context->pcidev0, PCI_COMMAND_MASTER, tmp);
+		if(ret) {
+			EMGD_ERROR_EXIT("PCI write of bus master");
+			return -1;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+static int full_config_vga_plb(igd_context_t *context,
+	init_dispatch_t *dispatch)
+{
+	platform_context_plb_t *platform_context =
+		(platform_context_plb_t *)context->platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	if(OS_PCI_READ_CONFIG_32(platform_context->pcidev0,
+			PLB_PCI_MMADR, (void*)&context->device_context.mmadr)) {
+		EMGD_ERROR_EXIT("Reading MMADR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	context->device_context.mmadr &= 0xfffffff9;
+	context->device_context.virt_mmadr =
+		OS_MAP_IO_TO_MEM_NOCACHE(context->device_context.mmadr, PLB_MMIO_SIZE);
+
+	if (!context->device_context.virt_mmadr) {
+		EMGD_ERROR_EXIT("Failed to map MMADR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	EMGD_DEBUG("mmadr mapped %dKB @           (phys):0x%lx (virt):%p",
+		PLB_MMIO_SIZE/1024,
+		context->device_context.mmadr,
+		context->device_context.virt_mmadr);
+
+	/* PCI Interrupt Line */
+	if(OS_PCI_READ_CONFIG_8(platform_context->pcidev0,
+			PCI_INTERRUPT_LINE, (void*)&platform_context->irq)) {
+		platform_context->irq = 0;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Get the # of pages used for video memory. This does not use information from
+ * the scratch register, since this is done later if it exists.
+ *
+ * @param context
+ * @param pages
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+static int get_stolen_mem_plb(igd_context_t *context, unsigned long *pages)
+{
+	platform_context_plb_t *platform_context;
+	os_pci_dev_t           vga_dev;
+	unsigned short         gmch_ctl;
+	unsigned long          stolen_mem; /* in bytes */
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	platform_context = (platform_context_plb_t *)context->platform_context;
+	vga_dev = platform_context->pcidev0;
+
+	ret = OS_PCI_READ_CONFIG_16(vga_dev, PLB_PCI_GC, &gmch_ctl);
+	if (ret) {
+		EMGD_ERROR_EXIT("Unable to read PLB_PCI_GC");
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch (gmch_ctl & 0x70) {
+	case 0x00:
+		stolen_mem = 0;
+		break;
+	case 0x10:
+		/* 1M */
+		stolen_mem = 1*1024*1024;
+		break;
+	case 0x20:
+		/* 4M */
+		stolen_mem = 4*1024*1024;
+		break;
+	case 0x30:
+		/* 8M */
+		stolen_mem = 8*1024*1024;
+		break;
+	case 0x40:
+		/* 16M */
+		stolen_mem = 16*1024*1024;
+		break;
+	case 0x50:
+		/* 32M */
+		stolen_mem = 32*1024*1024;
+		break;
+	case 0x60:
+		/* 48M */
+		stolen_mem = 48*1024*1024;
+	case 0x70:
+		/* 64M */
+		stolen_mem = 64*1024*1024;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Unknown Stolen Memory Size");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (stolen_mem) {
+		/*
+		 * Subtract off the size of the GTT which is
+		 * (number of entries in DWORDS) * 4 to get it into bytes
+		 */
+		stolen_mem -= context->device_context.gatt_pages*4;
+		/* Subtract off 1 page for the scratch page */
+		stolen_mem -= 4*1024;
+	}
+
+	/* Convert to the # of pages available for stolen memory */
+	*pages = stolen_mem / 4096;
+
+	EMGD_DEBUG("Stolen memory: 0x%lx pages", *pages);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int full_get_param_plb(igd_context_t *context,
+	unsigned long id,
+	unsigned long *value)
+{
+	int ret = 0;
+	unsigned char *mmio;
+	unsigned long control_reg;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("ID: 0x%lx", id);
+
+	/* Scratch registers used as below:
+	 *
+	 * 0x71410:
+	 * --------
+	 *   Bits 31-16 - EID Firmware identifier 0xE1DF
+	 *   Bits 15-00 - Tell what data is present.
+	 * Here are bits for what we are using know:
+	 *   Bit 0 - Panel id
+	 *   Bit 1 - List of ports for which displays are attached
+	 *   Bit 2 - Memory reservation
+	 * If any of the above bits is set that mean data is followed
+	 * in the next registers.
+	 *
+	 * 0x71414:
+	 * --------
+	 *   Bits 07-00 - Panel Id
+	 *   Bits 11-08 - Port list
+	 * Information for Port list: If any of the bit is set means,
+	 *   a display is attached to that port as follows:
+	 *    Bit 08 - CRT
+	 *    Bit 09 - DVOA/Internal LVDS
+	 *    Bit 10 - DVOB/RGBA
+	 *    Bit 11 - DVOC
+	 *
+	 * 0x71418:
+	 * --------
+	 *	 Bits 15-00 - Reserved Memory value in number of 4k size pages
+	 */
+	mmio = context->device_context.virt_mmadr;
+	control_reg = EMGD_READ32(EMGD_MMIO(mmio) + 0x71410);
+	*value = 0;
+
+	switch(id) {
+	case IGD_PARAM_PANEL_ID:
+		/*
+		 * Check for Embedded firmware
+		 */
+		if ((control_reg>>16) != 0xE1DF) {
+			EMGD_DEBUG("No Embedded vBIOS found");
+			EMGD_TRACE_EXIT;
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * The panel id bit must be set in the control register
+		 * to indicate valid panel (config) ID value.
+		 */
+		if (control_reg & 0x1) {
+			*value = EMGD_READ32(EMGD_MMIO(mmio) + 0x71414) & 0xFF;
+			if(!(*value)) {
+				/* we cannot allow for config id = 0 */
+				ret = -IGD_ERROR_INVAL;
+			}
+		} else {
+			EMGD_DEBUG("Panel ID read failed: Incorrect Operation");
+			ret = -IGD_ERROR_INVAL;
+		}
+		break;
+	case IGD_PARAM_MEM_RESERVATION:
+		/*
+		 * Check for Embedded firmware
+		 */
+		if ((control_reg>>16) != 0xE1DF) {
+			EMGD_DEBUG("No Embedded vBIOS found");
+			EMGD_TRACE_EXIT;
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * The mem reservation bit must be set in the control register
+		 * to indicate valid mem reservation value.
+		 */
+		if (control_reg & 0x4) {
+			*value = (EMGD_READ32(EMGD_MMIO(mmio) + 0x71418) & 0xFFFF);
+		} else {
+			EMGD_DEBUG("Mem Reservation read failed: Incorrect Operation");
+			ret = -IGD_ERROR_INVAL;
+		}
+		break;
+	default:
+		ret = -IGD_ERROR_INVAL;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+void full_shutdown_plb(igd_context_t *context)
+{
+	platform_context_plb_t *platform_context =
+		(platform_context_plb_t *)context->platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	/* unmap registers */
+	if(context->device_context.virt_mmadr) {
+		EMGD_DEBUG("Unmapping Gfx registers and GTT Table...");
+		OS_UNMAP_IO_FROM_MEM((void *) context->device_context.virt_mmadr,
+			PLB_MMIO_SIZE);
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_gttadr,
+			context->device_context.gatt_pages * 4);
+	} else {
+		printk(KERN_ERR "Unmapping MMIO space failed.\n");
+	}
+
+	if (platform_context) {
+		OS_PCI_FREE_DEVICE(platform_context->pcidev0);
+	}
+	EMGD_TRACE_EXIT;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/plb/micro_init_plb.c b/drivers/gpu/drm/emgd/emgd/core/init/plb/micro_init_plb.c
new file mode 100644
index 0000000..c91adb6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/plb/micro_init_plb.c
@@ -0,0 +1,631 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_init_plb.c
+ * $Revision: 1.13 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.init
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+#include <utils.h>
+
+#include <igd_debug.h>
+#include <drmP.h>
+#include <memory.h>
+#include <asm/cacheflush.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+
+#include "user_config.h"
+#include "../cmn/init_dispatch.h"
+
+/*!
+ * @addtogroup core_group
+ * @{
+ */
+
+#define PFX "EMGD: "
+
+#define SCR1    0x71410 /* scratch register set by vbios indicating status*/
+#define SCR2    0x71418 /* scratch register set by vbios indicating amount of stolen memory */
+#define FW_ID   0xE1DF0000 /* firmware identifier */
+#define ST_BIT  0x00000004 /* bit2- stolen memory bit */
+#define PSB_GMCH_CTRL       0x52
+#define PSB_GMCH_ENABLED    0x04
+#define PSB_PGETBL_CTL      0x00002020
+#define PSB_PGETBL_ENABLED  0x00000001
+#define PSB_GATT_RESOURCE   2
+#define PSB_GTT_RESOURCE    3
+#define PSB_BSM             0x5c
+#define PSB_PTE_VALID       0x0001
+
+
+#ifdef CONFIG_PLB
+
+extern unsigned char io_mapped;
+extern unsigned short io_base;
+extern emgd_drm_config_t config_drm;
+
+extern int full_config_plb(igd_context_t *context,
+	init_dispatch_t *dispatch);
+extern int get_revision_id_plb(igd_context_t *context, os_pci_dev_t vga_dev);
+extern int full_get_param_plb(igd_context_t *context, unsigned long id,
+	unsigned long *value);
+extern void full_shutdown_plb(igd_context_t *context);
+
+static int query_plb(igd_context_t *context,init_dispatch_t *dispatch,
+	os_pci_dev_t vga_dev, unsigned int *bus, unsigned int *slot,
+	unsigned int *func);
+static int config_plb(igd_context_t *context,
+	init_dispatch_t *dispatch);
+static int set_param_plb(igd_context_t *context, unsigned long id,
+	unsigned long value);
+static int get_param_plb(igd_context_t *context, unsigned long id,
+	unsigned long *value);
+static void shutdown_plb(igd_context_t *context);
+
+static void gtt_shutdown_plb(igd_context_t *context);
+static void gtt_init_plb(igd_context_t *context);
+
+
+static platform_context_plb_t platform_context_plb;
+
+/* Graphics frequency list. This is valid for pouslbo only. This value is obtained
+ * From the Cspec - SCH Message Network-Port 5*/
+static unsigned short plb_gfx_freq_list[] =
+{
+	100, 133, 150, 178, 200, 266, 0, 0
+};
+
+init_dispatch_t init_dispatch_plb = {
+	"Intel SCH US15 Chipset",
+	"US15",
+	"lvds",
+	query_plb,
+	config_plb,
+	set_param_plb,
+	get_param_plb,
+	shutdown_plb,
+	NULL
+};
+
+
+
+/*
+ * GTT shutdown.
+ *
+ * Unmap the GTT mapping that was done during init time.
+ */
+static void gtt_shutdown_plb(igd_context_t *context)
+{
+	if (context->device_context.virt_gttadr) {
+		iounmap(context->device_context.virt_gttadr);
+
+		context->device_context.virt_gttadr = NULL;
+	}
+}
+
+
+/*
+ * Initialize the GTT.
+ *   - Find the size of stolen memory
+ *   - Add stolen memory to the GTT
+ *   - Map the GTT and video memory
+ */
+
+static void gtt_init_plb(igd_context_t *context)
+{
+	struct drm_device *dev;
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long dvmt_mode = 0;
+	unsigned long gtt_pages = 0;
+	unsigned long stolen_mem_size = 0;
+	unsigned long scratch;
+	unsigned long base;
+	unsigned long pte;
+	unsigned short gmch_ctl;
+	unsigned long pge_ctl;
+	unsigned long gtt_phys_start;
+	unsigned long gatt_start;
+	unsigned long gatt_pages;
+	unsigned long gtt_start;
+	unsigned long gtt_order;
+	unsigned long stolen_mem_base;
+	unsigned long *gtt_table;
+	int gtt_enabled = FALSE;
+	struct page *gtt_table_page;
+	int i;
+
+	dev = (struct drm_device *)context->drm_dev;
+
+	/* Enable the GMCH */
+	OS_PCI_READ_CONFIG_16((os_pci_dev_t)dev->pdev, PSB_GMCH_CTRL,
+			&gmch_ctl);
+	OS_PCI_WRITE_CONFIG_16((os_pci_dev_t)dev->pdev, PSB_GMCH_CTRL,
+			(gmch_ctl | PSB_GMCH_ENABLED));
+	context->device_context.gmch_ctl = gmch_ctl;
+
+	/* Get the page table control register */
+	pge_ctl = readl(mmio + PSB_PGETBL_CTL);
+	gtt_phys_start = pge_ctl & PAGE_MASK;
+
+	gtt_enabled = pge_ctl & PSB_PGETBL_ENABLED;
+
+	/* Create a scratch page to initialize empty GTT entries */
+	context->device_context.scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
+
+	/*
+	* Is pci_resource_start(dev->pdev, PSB_GATT_RESOURCE); the same
+	* as pci_read_config_dword(dev->pdev, 0x1C, &value)?
+	*
+	* PSB_GATT_RESOURCE length is the amount of memory addressable
+	* by the GTT table.
+	*/
+	gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
+	gatt_pages = (pci_resource_len(dev->pdev, PSB_GATT_RESOURCE) >> PAGE_SHIFT);
+	context->device_context.gatt_pages = gatt_pages;
+
+	/*
+	 * The GTT wasn't set up by the vBios
+	 */
+	if (!gtt_enabled) {
+		context->device_context.stolen_pages = 0;
+
+		gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) >> PAGE_SHIFT;
+		gtt_order = get_order(gtt_pages << PAGE_SHIFT);
+		gtt_table = (unsigned long *)__get_free_pages(GFP_KERNEL, gtt_order);
+		/* Make sure allocation was successful */
+		if (NULL == gtt_table) {
+			EMGD_ERROR("Failed to allocate kernel pages for GTT");
+			return;
+		}
+		context->device_context.virt_gttadr = gtt_table;
+
+		for (i=0; i < (1 << gtt_order); i++) {
+			gtt_table_page = virt_to_page(gtt_table + (PAGE_SIZE * i));
+			EMGD_DEBUG("Setting reserved bit on %p", gtt_table_page);
+			set_bit(PG_reserved, &gtt_table_page->flags);
+		}
+
+		gtt_phys_start = virt_to_phys(gtt_table);
+
+		for (i = 0; i < gtt_pages; i++) {
+			gtt_table[i] = (unsigned long)context->device_context.scratch_page;
+		}
+
+		printk(KERN_INFO "Detected GTT was not enabled by firmware");
+		printk(KERN_INFO "GMMADR(region 0) start: 0x%08lx (%ldM).\n",
+			gatt_start, (gatt_pages / 256));
+		printk(KERN_INFO "GTTADR(region 3) start: 0x%08lx (can map %ldM RAM), and "
+			"actual RAM base 0x%08lx.\n",
+			(unsigned long)gtt_table, (gtt_pages * 4), gtt_phys_start);
+
+		/* Enable the newly created GTT */
+		EMGD_DEBUG("Enabling new GTT");
+		writel((gtt_phys_start|PSB_PGETBL_ENABLED), mmio + PSB_PGETBL_CTL);
+		pge_ctl = readl(mmio + PSB_PGETBL_CTL);
+
+	} else {
+
+		/*
+		 * Get the start address of the GTT page table
+		 *
+		 * In full_config_vga, this is done differently.  The address is read
+		 * from pcidev0's pci config space, at TNC_PCI_GTTADR and the size comes
+		 * from TNC_OFFSET_VGA_MSAC. The value read for size is a size id
+		 *    1 = 128, 2 = 256, 3 = 512
+		 * emgd_gtt->gtt_start = OS_PCI_READ_CONFIG_32(
+		 *            context->platform_context->pcidev0, TNC_PCI_GTTADDR)
+		 * gtt_pages = OS_PCI_READ_CONFIG_8(context->platform_context->pcidev0,
+		 *            TNC_OFFSET_VGA_MSAC) * 1024;
+		 *
+		 * PSB_GTT_RESOURCE length is the size of the GTT table. Thus,
+		 * gtt_pages is the number of pages that make up the table.
+		 */
+		gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
+		gtt_pages = (pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) >> PAGE_SHIFT);
+
+		/* Get stolen memory configuration. */
+		pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)&stolen_mem_base);
+		stolen_mem_size = gtt_phys_start - stolen_mem_base - PAGE_SIZE;
+
+		/* Display useful information in the kernel log */
+		printk(KERN_INFO "GMMADR(region 0) start: 0x%08lx (%ldM).\n",
+				gatt_start, (gatt_pages / 256));
+		printk(KERN_INFO "GTTADR(region 3) start: 0x%08lx (can map %ldM RAM), and "
+				"actual RAM base 0x%08lx.\n",
+				gtt_start, (gtt_pages * 4), gtt_phys_start);
+		printk(KERN_INFO "Stolen memory information \n");
+		printk(KERN_INFO "       base in RAM: 0x%lx \n", stolen_mem_base);
+		printk(KERN_INFO "       size: %luK, calculated by (GTT RAM base) - "
+				"(Stolen base)\n", (stolen_mem_size / 1024));
+		dvmt_mode = (gmch_ctl >> 4) & 0x7;
+		printk(KERN_INFO "       size: %dM (dvmt mode=%ld)\n",
+				(dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode);
+
+		context->device_context.virt_gttadr =
+		ioremap_nocache(gtt_start, gtt_pages << PAGE_SHIFT);
+
+		if (!context->device_context.virt_gttadr) {
+			printk(KERN_ERR "Failed to map the GTT.\n");
+			/* TODO: Clean up somelthing here */
+			return;
+		}
+
+		/* Insert stolen memory pages into the beginning of GTT */
+		base = stolen_mem_base >> PAGE_SHIFT;
+		context->device_context.stolen_pages = stolen_mem_size >> PAGE_SHIFT;
+
+		printk(KERN_INFO "Set up %ld stolen pages starting at 0x%08lx, "
+			"GTT offset %dK\n", context->device_context.stolen_pages, base, 0);
+
+		for (i = 0; i < context->device_context.stolen_pages; i++) {
+			pte = ((base + i) << PAGE_SHIFT) | PSB_PTE_VALID;
+			writel(pte, context->device_context.virt_gttadr + i);
+		}
+
+	}
+
+	/* Update the scratch registers to say we have no stolen memory */
+	scratch = readl(mmio + SCR1);
+	if ((scratch & FW_ID) == FW_ID) {
+		/* if an EMGD vBios modify only the stolen memory bit */
+		scratch |= ST_BIT;
+		writel(scratch, mmio + SCR1);
+	} else {
+		/* Not an EMGD vBios so just set the entire register to a known value */
+		writel((FW_ID|ST_BIT), mmio + SCR1);
+	}
+
+	/*
+	 * Report back that there is 0MB of stolen memory regardless of
+	 * what was really in there.  Fresh pages will be inserted over
+	 * the top of the existing stolen memory.
+	 */
+	writel(0, mmio + SCR2);
+
+	/*
+	 * FIXME: Shouldn't this fill in all the GTT page table entries with
+	 * the scratch page?
+	 */
+
+	return;
+}
+
+
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ * @param vga_dev
+ * @param bus
+ * @param slot
+ * @param func
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+static int query_plb(
+	igd_context_t *context,
+	init_dispatch_t *dispatch,
+	os_pci_dev_t vga_dev,
+	unsigned int *bus,
+	unsigned int *slot,
+	unsigned int *func)
+{
+	platform_context_plb_t *platform_context = &platform_context_plb;
+
+	EMGD_TRACE_ENTER;
+
+	context->platform_context = (void *)&platform_context_plb;
+
+	OS_PTHREAD_MUTEX_INIT(&platform_context_plb.flip_mutex, NULL);
+
+	/*
+	 * Current specs indicate that PLB has only one PCI function.
+	 * If this changes then we need to make sure we have func 0
+	 * here as in previous chips.
+	 */
+	platform_context->pcidev0 = vga_dev;
+
+	OS_PCI_GET_SLOT_ADDRESS(vga_dev, bus, slot, func);
+
+	OPT_MICRO_CALL(get_revision_id_plb(context, vga_dev));
+
+	/*
+	 * Read BSM.
+	 * This must be in query so it is available early for the vBIOS.
+	 */
+	if(OS_PCI_READ_CONFIG_32(vga_dev,
+			PLB_PCI_BSM, &context->device_context.fb_adr)) {
+		EMGD_ERROR_EXIT("Reading BSM");
+		return -IGD_ERROR_NODEV;
+	}
+	context->device_context.fb_adr &= 0xFFFFF000;
+
+	EMGD_DEBUG("BSM (High)@: 0x%lx, (Low) 0x%4lx", (context->device_context.fb_adr >> 16), context->device_context.fb_adr);
+
+	/*
+	 * Read IO Base.
+	 * This must be in query so it is available early for the vBIOS.
+	 */
+	if(OS_PCI_READ_CONFIG_16(vga_dev, PLB_PCI_IOBAR, &io_base)) {
+		EMGD_ERROR_EXIT("Reading IO Base");
+		return -IGD_ERROR_NODEV;
+	}
+	io_base &= 0xfffe;
+	EMGD_DEBUG("io @: 0x%x", io_base);
+
+	/* Gen4 is always io_mapped */
+	io_mapped = 1;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+static int config_plb(igd_context_t *context,
+	init_dispatch_t *dispatch)
+{
+	unsigned long coreclk;
+	unsigned long graphics_frequency;
+
+	platform_context_plb_t *platform_context =
+		(platform_context_plb_t *)context->platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	OPT_MICRO_CALL(full_config_plb(context, dispatch));
+
+	/* If KMS is set, we need to unset it as KMS is not supported on PLB */
+	config_drm.kms = 0;
+
+	/* Set the Max Dclock */
+	if(OS_PCI_READ_CONFIG_32(platform_context->pcidev0,
+			INTEL_OFFSET_VGA_CORECLK, &coreclk)) {
+		EMGD_ERROR_EXIT("PCI Read of VGA Core Clock");
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Get graphics frequency param if it exists */
+	if(!get_param_plb(context, IGD_PARAM_GFX_FREQ,
+			&graphics_frequency)) {
+		context->device_context.gfx_freq = (unsigned short)graphics_frequency;
+	}
+
+	/*
+	 * FIXME:
+	 *  Coreclk register above is used to determine some clocking information
+	 *  there is also a fuse to limit the dclk. More research needed.
+	 */
+	context->device_context.max_dclk = 762000;
+
+	gtt_init_plb(context);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+static int get_param_plb(igd_context_t *context, unsigned long id,
+	unsigned long *value)
+{
+	int ret = 0;
+	unsigned long control_reg;
+	os_pci_dev_t bridge_dev = (os_pci_dev_t)0;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("ID: 0x%lx", id);
+
+	/* Scratch registers used as below:
+	 *
+	 * 0x71410:
+	 * --------
+	 *   Bits 31-16 - EID Firmware identifier 0xE1DF
+	 *   Bits 15-00 - Tell what data is present.
+	 * Here are bits for what we are using know:
+	 *   Bit 0 - Panel id
+	 *   Bit 1 - List of ports for which displays are attached
+	 *   Bit 2 - Memory reservation
+	 * If any of the above bits is set that mean data is followed
+	 * in the next registers.
+	 *
+	 * 0x71414:
+	 * --------
+	 *   Bits 07-00 - Panel Id
+	 *   Bits 11-08 - Port list
+	 * Information for Port list: If any of the bit is set means,
+	 *   a display is attached to that port as follows:
+	 *    Bit 08 - CRT
+	 *    Bit 09 - DVOA/Internal LVDS
+	 *    Bit 10 - DVOB/RGBA
+	 *    Bit 11 - DVOC
+	 *
+	 * 0x71418:
+	 * --------
+	 * Bits 15-00 - Reserved Memory value in number of 4k size pages
+	 */
+	*value = 0;
+
+	switch(id) {
+	case IGD_PARAM_PORT_LIST:
+
+		control_reg = EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) +
+			0x71410);
+
+		/*
+		 * Verify that the Embedded Firware is present.
+		 */
+		if ((control_reg>>16) != 0xE1DF) {
+			EMGD_DEBUG("Exit No Embedded vBIOS found");
+			EMGD_TRACE_EXIT;
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * If the port list bit is set in control register,
+		 * read the port list
+		 */
+		if (control_reg & 0x2) {
+			unsigned char temp;
+			int i = 0;
+
+			temp = (unsigned char)((EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) + 0x71414)>>8) & 0xFF);
+			EMGD_DEBUG("Connected Port Bits: 0x%x", temp);
+
+			/*
+			 * The meanings of bits in temp were dictated by VBIOS
+			 * and should not change due to backward compatibility
+			 * with Legacy VBIOS
+			 */
+			if (temp & 0x01) {
+				/* Analog port */
+				value[i++] = 5;
+			}
+			if (temp & 0x02) {
+				/* Internal LVDS port */
+				value[i++] = 4;
+			}
+			if (temp & 0x04) {
+				/* DVOB Port */
+				value[i++] = 2;
+			}
+		} else {
+			EMGD_DEBUG("Port List read failed: Incorrect Operation");
+			ret = -IGD_ERROR_INVAL;
+		}
+		break;
+	case IGD_PARAM_GFX_FREQ:
+		/* Query register values from the bridge. This method uses the Poulsbo
+		 * SCH Message Network. Setting offset 0xD0 in the host bridge config
+		 * register sends an opcode to the Message Network. Reading register 0xD4
+		 * from the host bridge config register will get the return value of the
+		 * sent opcode.
+		 *
+		 * This feature is for Pouslbo Only */
+
+		bridge_dev = OS_PCI_FIND_DEVICE(
+				PCI_VENDOR_ID_INTEL,
+				PCI_DEVICE_ID_BRIDGE_PLB,
+				0xFFFF, /* Scan the whole PCI bus */
+				0,
+				0,
+				(os_pci_dev_t)0);
+
+		if(!bridge_dev) {
+			EMGD_ERROR_EXIT("Bridge device NOT found.");
+			return -IGD_ERROR_INVAL;
+		}
+		/* write into the Message Control Register (MCR)
+		 * [INPUT] should contain the formatted opcode
+		 * that needs to be sent into the MCR */
+		ret = OS_PCI_WRITE_CONFIG_32(bridge_dev, 0xD0,
+			(0xD0<<24)/*opcode*/|(5<<16)/*port*/|(3<<8)/*reg*/|(0xF<<4));
+		if(ret) {
+			EMGD_ERROR("Writing into the MCR Failed");
+			return -IGD_ERROR_INVAL;
+		}
+		/* read from the Message Data Register (MDR) */
+		if(OS_PCI_READ_CONFIG_32(bridge_dev, 0xD4,
+			(void*) &control_reg)) {
+			EMGD_ERROR_EXIT("Reading from MDR Failed");
+			return -IGD_ERROR_INVAL;
+		}
+		*value = plb_gfx_freq_list[control_reg & 0x7];
+		OS_PCI_FREE_DEVICE(bridge_dev);
+		break;
+	default:
+		/*
+		 * If the param is not found here then it may only be in the
+		 * full version.
+		 */
+		OPT_MICRO_CALL_RET(ret, full_get_param_plb(context, id, value));
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ * No Settable Params for PLB
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL
+ */
+static int set_param_plb(igd_context_t *context, unsigned long id,
+	unsigned long value)
+{
+	return -IGD_ERROR_INVAL;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void shutdown_plb(igd_context_t *context)
+{
+	gtt_shutdown_plb(context);
+
+	OPT_MICRO_VOID_CALL(full_shutdown_plb(context));
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/tnc/init_tnc.c b/drivers/gpu/drm/emgd/emgd/core/init/tnc/init_tnc.c
new file mode 100644
index 0000000..c07d82d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/tnc/init_tnc.c
@@ -0,0 +1,621 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: init_tnc.c
+ * $Revision: 1.24 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.init
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+#include <gart.h>
+
+#include <memory.h>
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+#include <igd_gart.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+
+#include <tnc/regs.h>
+#include <tnc/context.h>
+#include <tnc/igd_tnc_wa.h>
+
+#include "../cmn/init_dispatch.h"
+#include "/usr/include/linux/pci_regs.h"
+
+/*!
+ * @addtogroup core_group
+ * @{
+ */
+
+static int bus_master_enable_tnc(platform_context_tnc_t *platform_context);
+static int full_config_vga_tnc(igd_context_t *context,
+	init_dispatch_t *dispatch);
+static int get_stolen_mem_tnc(igd_context_t *context, unsigned long *pages);
+
+int full_get_param_tnc(igd_context_t *context,
+	unsigned long id,
+	unsigned long *value);
+
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ *
+ * @return 1 on failure
+ * @return 0 on success
+ */
+int full_config_tnc(igd_context_t *context,
+	init_dispatch_t *dispatch)
+{
+	unsigned long reserved_mem;
+	platform_context_tnc_t *platform_context;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	platform_context = (platform_context_tnc_t *)context->platform_context;
+
+	_sgx_base = 0x80000;
+	_msvdx_base = 0x90000;
+	_topaz_base = 0xA0000;
+
+	/*
+	 * Enable bus mastering for platforms whose BIOS did not perform this
+	 * task for us.
+	 */
+	ret = bus_master_enable_tnc(platform_context);
+	if(ret) {
+		EMGD_ERROR("Error: Enabling bus master");
+	}
+
+	/* Config VGA */
+	ret = full_config_vga_tnc(context, dispatch);
+	if(ret) {
+		EMGD_ERROR_EXIT("Config VGA Failed");
+		return ret;
+	}
+
+	get_stolen_mem_tnc(context, &context->device_context.reserved_mem);
+
+	/* Get mem reservation param if it exists */
+	if(!full_get_param_tnc(context, IGD_PARAM_MEM_RESERVATION,
+			&reserved_mem)) {
+		context->device_context.reserved_mem = reserved_mem;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param platform_context
+ *
+ * @return -1 on failure
+ * @return 0 on success
+ */
+/*
+ */
+static int bus_master_enable_tnc(platform_context_tnc_t *platform_context){
+	int ret;
+	unsigned char tmp, tmp_sdvo;
+
+	EMGD_TRACE_ENTER;
+
+	ret = OS_PCI_READ_CONFIG_8(platform_context->pcidev0, PCI_COMMAND_MASTER, &tmp);
+	if(ret) {
+		EMGD_ERROR_EXIT("PCI read of bus master");
+		return -1;
+	}
+
+	/*
+	 * Get Bit 2, 1, and 0 and see if it is == 1
+	 * all 3 bits has to be enabled. This is to enable register read/write
+	 * in the case of a PCI card being added
+	 */
+	if((tmp & 0x7) != 0x7 ) {
+
+		tmp |= 0x7;
+		ret = OS_PCI_WRITE_CONFIG_8(platform_context->pcidev0,PCI_COMMAND_MASTER, tmp);
+		if(ret) {
+			EMGD_ERROR_EXIT("PCI write of bus master");
+			return -1;
+		}
+	}
+
+
+	if (platform_context->pcidev1) {
+		ret = OS_PCI_READ_CONFIG_8(platform_context->pcidev1,PCI_COMMAND_MASTER, &tmp_sdvo);
+		if(ret) {
+			EMGD_ERROR_EXIT("PCI read of bus master");
+			return -1;
+		}
+
+		if((tmp_sdvo & 0x7) != 0x7 ) {
+			tmp_sdvo |= 0x7;
+			ret = OS_PCI_WRITE_CONFIG_8(platform_context->pcidev1,
+				PCI_COMMAND_MASTER,tmp_sdvo);
+			if(ret) {
+				EMGD_ERROR_EXIT("PCI write of bus master");
+				return -1;
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+static int full_config_vga_tnc(igd_context_t *context,
+	init_dispatch_t *dispatch)
+{
+	platform_context_tnc_t *platform_context =
+		(platform_context_tnc_t *)context->platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Map the Device 2 MMIO register. These registers are similar to LNC
+	 * and is located in the Atom E6xx Hard Macro
+	 */
+	if(OS_PCI_READ_CONFIG_32(platform_context->pcidev0,
+			TNC_PCI_MMADR, (void*)&context->device_context.mmadr)) {
+		EMGD_ERROR_EXIT("Reading MMADR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	context->device_context.mmadr &= 0xfffffff9;
+	context->device_context.virt_mmadr =
+		OS_MAP_IO_TO_MEM_NOCACHE(context->device_context.mmadr, TNC_D2_MMIO_SIZE);
+
+	if (!context->device_context.virt_mmadr) {
+		EMGD_ERROR_EXIT("Failed to map MMADR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	EMGD_DEBUG("mmadr mapped %dKB @ (phys):0x%lx  (virt):%p",
+		TNC_D2_MMIO_SIZE/1024,
+		context->device_context.mmadr,
+		context->device_context.virt_mmadr);
+
+
+	/*
+	 * Map the device 3 MMIO registers. These are Atom E6xx specific registers
+	 * located in the Atom E6xx Overlay. On LNC, these registers are part of
+	 * the IOH (Langwell)
+	 */
+	if(OS_PCI_READ_CONFIG_32(platform_context->pcidev1,
+			TNC_PCI_MMADR, (void*)&context->device_context.mmadr_sdvo)) {
+		EMGD_ERROR_EXIT("Reading MMADR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	context->device_context.mmadr_sdvo &= 0xfffffff9;
+	context->device_context.virt_mmadr_sdvo =
+		OS_MAP_IO_TO_MEM_NOCACHE(context->device_context.mmadr_sdvo,
+		TNC_D3_MMIO_SIZE);
+
+	if (!context->device_context.virt_mmadr_sdvo) {
+		EMGD_ERROR_EXIT("Failed to map MMADR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	EMGD_DEBUG("sdvo mmadr mapped %dKB @ (phys):0x%lx  (virt):%p",
+		TNC_D3_MMIO_SIZE/1024,
+		context->device_context.mmadr_sdvo,
+		context->device_context.virt_mmadr_sdvo);
+
+	/* Map the STMicro SDVO registers. */
+	if(platform_context->stbridgedev) {
+		if(OS_PCI_READ_CONFIG_32(platform_context->stbridgedev,
+					TNC_PCI_MMADR, (void*)&context->device_context.mmadr_sdvo_st)) {
+			EMGD_ERROR_EXIT("Reading MMADR");
+			return -IGD_ERROR_NODEV;
+		}
+
+		context->device_context.mmadr_sdvo_st &= 0xfffffff9;
+		context->device_context.virt_mmadr_sdvo_st =
+			OS_MAP_IO_TO_MEM_NOCACHE(context->device_context.mmadr_sdvo_st,
+					TNC_ST_SDVO_MMIO_SIZE);
+
+		if (!context->device_context.virt_mmadr_sdvo_st) {
+			EMGD_ERROR_EXIT("Failed to map MMADR");
+			return -IGD_ERROR_NODEV;
+		}
+
+		EMGD_DEBUG("STMicro sdvo mmadr mapped %dKB @ (phys):0x%lx  (virt):%p",
+				TNC_ST_SDVO_MMIO_SIZE/1024,
+				context->device_context.mmadr_sdvo_st,
+				context->device_context.virt_mmadr_sdvo_st);
+
+		/* Map the STMicro GPIO registers. */
+		if(OS_PCI_READ_CONFIG_32(platform_context->stgpiodev,
+					TNC_PCI_MMADR, (void*)&context->device_context.mmadr_sdvo_st_gpio)) {
+			EMGD_ERROR_EXIT("Reading MMADR");
+			return -IGD_ERROR_NODEV;
+		}
+
+		context->device_context.mmadr_sdvo_st_gpio &= 0xfffffff9;
+		context->device_context.virt_mmadr_sdvo_st_gpio =
+			OS_MAP_IO_TO_MEM_NOCACHE(context->device_context.mmadr_sdvo_st_gpio,
+					TNC_ST_SDVO_MMIO_SIZE);
+
+		if (!context->device_context.virt_mmadr_sdvo_st_gpio) {
+			EMGD_ERROR_EXIT("Failed to map MMADR");
+			return -IGD_ERROR_NODEV;
+		}
+
+		EMGD_DEBUG("STMicro sdvo gpio mmadr mapped %dKB @ (phys):0x%lx  (virt):%p",
+				TNC_ST_SDVO_MMIO_SIZE/1024,
+				context->device_context.mmadr_sdvo_st_gpio,
+				context->device_context.virt_mmadr_sdvo_st_gpio);
+	}
+
+	/* Map the GPIO BAR. Provides the 64 bytes of I/O space for GPIO
+	 * BAR is defined by bits 15:6 */
+
+	if(OS_PCI_READ_CONFIG_16(platform_context->lpc_dev,
+			TNC_PCI_GBA, (void*)&context->device_context.gpio_bar)) {
+
+	}
+	context->device_context.gpio_bar |= (1L<<31);
+	/* Enable the decode of IO Range ppointed to by the BA */
+	if(OS_PCI_WRITE_CONFIG_32(platform_context->lpc_dev,
+		TNC_PCI_GBA, context->device_context.gpio_bar)) {
+		EMGD_ERROR_EXIT("Writing LPC GPIO BAR Enable");
+	}
+
+	/* read the GPIO BAR (OFFSET 44:47) */
+	if(OS_PCI_READ_CONFIG_16(platform_context->lpc_dev,
+			TNC_PCI_GBA, (void*)&context->device_context.gpio_bar)) {
+		EMGD_ERROR_EXIT("Reading LPC GPIO BAR");
+		/* We cannot read the GPIO BAR. It is a problem but we can go on with init
+		 * return with NO ERROR*/
+		return 0;
+	}
+
+	context->device_context.gpio_bar &= 0xffc0;
+
+	context->device_context.virt_gpio_bar =
+		OS_MAP_IO_TO_MEM_NOCACHE(
+		context->device_context.gpio_bar, 64);
+
+	if (!context->device_context.virt_gpio_bar) {
+		EMGD_ERROR_EXIT("Failed to map LPC GPIO BAR");
+		return -IGD_ERROR_NODEV;
+	}
+
+	EMGD_DEBUG("GPIO mapped %dKB @ (phys):0x%lx  (virt):%p",
+		64,
+		context->device_context.gpio_bar,
+		context->device_context.virt_gpio_bar);
+
+	/* PCI Interrupt Line */
+	if(OS_PCI_READ_CONFIG_8(platform_context->pcidev0,
+			PCI_INTERRUPT_LINE, (void*)&platform_context->irq)) {
+		platform_context->irq = 0;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Get the # of pages used for video memory. This does not use information from
+ * the scratch register, since this is done later if it exists.
+ *
+ * @param context
+ * @param pages
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+static int get_stolen_mem_tnc(igd_context_t *context, unsigned long *pages)
+{
+	platform_context_tnc_t *platform_context;
+	os_pci_dev_t           vga_dev;
+	unsigned short         gmch_ctl;
+	unsigned long          stolen_mem; /* in bytes */
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	platform_context = (platform_context_tnc_t *)context->platform_context;
+	vga_dev = platform_context->pcidev0;
+
+	ret = OS_PCI_READ_CONFIG_16(vga_dev, TNC_PCI_GC, &gmch_ctl);
+	if (ret) {
+		EMGD_ERROR_EXIT("Unable to read TNC_PCI_GC");
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch (gmch_ctl & 0x70) {
+	case 0x00:
+		stolen_mem = 0;
+		break;
+	case 0x10:
+		/* 1M */
+		stolen_mem = 1*1024*1024;
+		break;
+	case 0x20:
+		/* 4M */
+		stolen_mem = 4*1024*1024;
+		break;
+	case 0x30:
+		/* 8M */
+		stolen_mem = 8*1024*1024;
+		break;
+	case 0x40:
+		/* 16M */
+		stolen_mem = 16*1024*1024;
+		break;
+	case 0x50:
+		/* 32M */
+		stolen_mem = 32*1024*1024;
+		break;
+	case 0x60:
+		/* 48M */
+		stolen_mem = 48*1024*1024;
+	case 0x70:
+		/* 64M */
+		stolen_mem = 64*1024*1024;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Unknown Stolen Memory Size");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (stolen_mem) {
+		/*
+		 * Subtract off the size of the GTT which is
+		 * (number of entries in DWORDS) * 4 to get it into bytes
+		 */
+		stolen_mem -= context->device_context.gatt_pages*4;
+		/* Subtract off 1 page for the scratch page */
+		stolen_mem -= 4*1024;
+	}
+
+	/* Convert to the # of pages available for stolen memory */
+	*pages = stolen_mem / 4096;
+
+	EMGD_DEBUG("Stolen memory: 0x%lx pages", *pages);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int full_get_param_tnc(igd_context_t *context,
+	unsigned long id,
+	unsigned long *value)
+{
+	int ret = 0;
+	unsigned char *mmio;
+	unsigned long control_reg;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("ID: 0x%lx", id);
+
+	/* Scratch registers used as below:
+	 *
+	 * 0x71410:
+	 * --------
+	 *   Bits 31-16 - EID Firmware identifier 0xE1DF
+	 *   Bits 15-00 - Tell what data is present.
+	 * Here are bits for what we are using know:
+	 *   Bit 0 - Panel id
+	 *   Bit 1 - List of ports for which displays are attached
+	 *   Bit 2 - Memory reservation
+	 * If any of the above bits is set that mean data is followed
+	 * in the next registers.
+	 *
+	 * 0x71414:
+	 * --------
+	 *   Bits 07-00 - Panel Id
+	 *   Bits 11-08 - Port list
+	 * Information for Port list: If any of the bit is set means,
+	 *   a display is attached to that port as follows:
+	 *    Bit 08 - CRT
+	 *    Bit 09 - DVOA/Internal LVDS
+	 *    Bit 10 - DVOB/RGBA
+	 *    Bit 11 - DVOC
+	 *
+	 * 0x71418:
+	 * --------
+	 *	 Bits 15-00 - Reserved Memory value in number of 4k size pages
+	 */
+	mmio = context->device_context.virt_mmadr;
+	control_reg = EMGD_READ32(EMGD_MMIO(mmio) + 0x71410);
+	*value = 0;
+
+	switch(id) {
+	case IGD_PARAM_PANEL_ID:
+		/*
+		 * Check for Embedded firmware
+		 */
+		if ((control_reg>>16) != 0xE1DF) {
+			EMGD_DEBUG("No Embedded vBIOS found");
+			EMGD_TRACE_EXIT;
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * The panel id bit must be set in the control register
+		 * to indicate valid panel (config) ID value.
+		 */
+		if (control_reg & 0x1) {
+			*value = EMGD_READ32(EMGD_MMIO(mmio) + 0x71414) & 0xFF;
+			if(!(*value)) {
+				/* we cannot allow for config id = 0 */
+				ret = -IGD_ERROR_INVAL;
+			}
+		} else {
+			EMGD_DEBUG("Panel ID read failed: Incorrect Operation");
+			ret = -IGD_ERROR_INVAL;
+		}
+		break;
+	case IGD_PARAM_MEM_RESERVATION:
+		/*
+		 * Check for Embedded firmware
+		 */
+		if ((control_reg>>16) != 0xE1DF) {
+			EMGD_DEBUG("No Embedded vBIOS found");
+			EMGD_TRACE_EXIT;
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * The mem reservation bit must be set in the control register
+		 * to indicate valid mem reservation value.
+		 */
+		if (control_reg & 0x4) {
+			*value = (EMGD_READ32(EMGD_MMIO(mmio) + 0x71418) & 0xFFFF);
+		} else {
+			EMGD_DEBUG("Mem Reservation read failed: Incorrect Operation");
+			ret = -IGD_ERROR_INVAL;
+		}
+		break;
+	default:
+		ret = -IGD_ERROR_INVAL;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+void full_shutdown_tnc(igd_context_t *context)
+{
+	platform_context_tnc_t *platform_context =
+		(platform_context_tnc_t *)context->platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	/* unmap registers */
+	if(context->device_context.virt_mmadr) {
+		EMGD_DEBUG("Unmapping Gfx registers and GTT Table...");
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_mmadr,
+			TNC_D2_MMIO_SIZE);
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_mmadr_sdvo,
+			TNC_D3_MMIO_SIZE);
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_mmadr_sdvo_st,
+			TNC_ST_SDVO_MMIO_SIZE);
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_mmadr_sdvo_st_gpio,
+			TNC_ST_SDVO_MMIO_SIZE);
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_gpio_bar, 64);
+
+		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_gttadr,
+			context->device_context.gatt_pages * 4);
+	} else {
+		printk(KERN_ERR "Unmapping MMIO space failed.\n");
+	}
+
+	if (platform_context) {
+		OS_PCI_FREE_DEVICE(platform_context->pcidev0);
+		OS_PCI_FREE_DEVICE(platform_context->pcidev1);
+		OS_PCI_FREE_DEVICE(platform_context->bridgedev);
+		if (platform_context->lpc_dev) {
+			OS_PCI_FREE_DEVICE(platform_context->lpc_dev);
+		}
+	}
+	EMGD_TRACE_EXIT;
+}
+
+
+ int query_2d_caps_hwhint_tnc(
+  	         igd_context_t *context,
+  	         unsigned long caps_val,
+  	         unsigned long *status)
+{
+	platform_context_tnc_t *platform_context;
+	  	 
+	EMGD_TRACE_ENTER;
+
+ 
+	platform_context = (platform_context_tnc_t *)context->platform_context;
+ 	 	 
+	/*
+	* Only 2D BLT capability query is currently implemented.
+	* This is required by TNC B0 flickering workaround.
+	* Query for other 2D capability can be implemented
+	* in the future if neeeded.
+	*/
+	switch (caps_val) {
+		case IGD_2D_CAPS_BLT:
+			if((context->device_context.did == PCI_DEVICE_ID_VGA_TNC) &&
+				(context->device_context.rid == TNC_B0_RID) &&
+				(platform_context->tnc_dev3_rid == TNC_B0_DEV3_RID)) {
+					*status = IGD_2D_HW_DISABLE;                    
+					/* disable 2d blt hardware acceleration for TNC B0 */
+			} else {
+					*status = IGD_2D_HW_ENABLE;                             
+					/* enable 2d blt hardware acceleration */  	
+			}
+		break;
+  	 
+		default:
+			*status = IGD_2D_CAPS_UNKNOWN;                          
+			/* Unknown 2d capability to query */
+			EMGD_ERROR("2D caps to query is unknown!");
+		break;
+	}
+  	 
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/core/init/tnc/micro_init_tnc.c b/drivers/gpu/drm/emgd/emgd/core/init/tnc/micro_init_tnc.c
new file mode 100644
index 0000000..dd2d1d0
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/core/init/tnc/micro_init_tnc.c
@@ -0,0 +1,998 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_init_tnc.c
+ * $Revision: 1.25 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.init
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+#include <utils.h>
+
+#include <igd_debug.h>
+#include <drmP.h>
+#include <memory.h>
+#include <asm/cacheflush.h>
+
+#include <tnc/regs.h>
+#include <tnc/context.h>
+#include <linux/pci_ids.h>
+
+#include "../cmn/init_dispatch.h"
+
+#define PFX "EMGD: "
+
+#define SCR1	0x71410 /* scratch register set by vbios indicating status*/
+#define SCR2	0x71418 /* scratch register set by vbios indicating amount of stolen memory */
+#define FW_ID	0xE1DF0000 /* firmware identifier */
+#define ST_BIT	0x00000004 /* bit2- stolen memory bit */
+#define PSB_GMCH_CTRL       0x52
+#define PSB_GMCH_ENABLED    0x04
+#define PSB_PGETBL_CTL      0x00002020
+#define PSB_GATT_RESOURCE   2
+#define PSB_GTT_RESOURCE    3
+#define PSB_BSM             0x5c
+#define PSB_PTE_VALID       0x0001
+
+
+
+/*!
+ * @addtogroup core_group
+ * @{
+ */
+
+#ifdef CONFIG_TNC
+
+extern unsigned char io_mapped;
+extern unsigned short io_base;
+
+/* For dev2 [0:2:0] */
+extern unsigned char io_mapped_lvds;
+extern unsigned short io_base_lvds;
+
+/* For dev3 [0:3:0] */
+extern unsigned char io_mapped_sdvo;
+extern unsigned short io_base_sdvo;
+
+/* For dev31 [0:31:0] */
+extern unsigned char io_mapped_lpc;
+extern unsigned short io_base_lpc;
+
+/* For STMicro SDVO [6:0:1] */
+extern unsigned char io_mapped_sdvo_st;
+extern unsigned short io_base_sdvo_st;
+extern unsigned char io_mapped_sdvo_st_gpio;
+extern unsigned short io_base_sdvo_st_gpio;
+
+extern int full_config_tnc(igd_context_t *context,
+	init_dispatch_t *dispatch);
+extern int get_revision_id_tnc(igd_context_t *context, os_pci_dev_t vga_dev, os_pci_dev_t sdvo_dev);
+extern int full_get_param_tnc(igd_context_t *context, unsigned long id,
+	unsigned long *value);
+extern void full_shutdown_tnc(igd_context_t *context);
+
+extern int query_2d_caps_hwhint_tnc(
+  	         igd_context_t *context,
+  	         unsigned long caps_val,
+  	         unsigned long *status);
+
+static int query_tnc(igd_context_t *context,init_dispatch_t *dispatch,
+	os_pci_dev_t vga_dev, unsigned int *bus, unsigned int *slot,
+	unsigned int *func);
+static int config_tnc(igd_context_t *context,
+	init_dispatch_t *dispatch);
+static int set_param_tnc(igd_context_t *context, unsigned long id,
+	unsigned long value);
+static int get_param_tnc(igd_context_t *context, unsigned long id,
+	unsigned long *value);
+static void shutdown_tnc(igd_context_t *context);
+
+static void gtt_shutdown_tnc(igd_context_t *context);
+static void gtt_init_tnc(igd_context_t *context);
+
+/* Helper Functions */
+static int query_sch_message(unsigned long reg, unsigned long* value);
+static int dump_fuse_values(void);
+
+static platform_context_tnc_t platform_context_tnc;
+
+os_pci_dev_t bridge_dev;
+
+init_dispatch_t init_dispatch_tnc = {
+	"Intel Atom E6xx Processor",
+	"Atom_E6xx",
+	"lvds",
+	query_tnc,
+	config_tnc,
+	set_param_tnc,
+	get_param_tnc,
+	shutdown_tnc,
+	query_2d_caps_hwhint_tnc
+};
+
+/* Array to keep the Bridge ID. Atom E6xx ULP uses a different bridge ID */
+static unsigned short bridge_id[] =
+{
+	PCI_DEVICE_ID_BRIDGE_TNC,
+	PCI_DEVICE_ID_BRIDGE_TNC_ULP,
+	0
+};
+
+#define SKU_NO 3
+#define RATIO_NO 8
+/*
+ * Atom E6xx GFX frequencies
+ * The gfx clock frequencies depends on the board SKU and ratio
+ * The table of frequencies can be found in Atom E6xx EAS
+ * Chapter: Clocks and Reset Unit
+ */
+static unsigned short tnc_gfx_freq_list[RATIO_NO][SKU_NO] =
+{
+	/* rows represent the gfx clock ratio,
+	 * columns the sku */
+
+	/* sku_100 sku_100L sku_83 */
+	{200,	100,	166}, /*1:1*/
+	{266,	133,	222}, /*4:3*/
+	{320,	160,	266}, /*8:5*/
+	{400,	200,	333}, /*2:1 DEFAULT*/
+	{0,		0,		0 }, /*16:7 RSVD*/
+	{533,	266,	444}, /*8:3*/
+	{640,	320,	553}, /*16:5*/
+	{800,	400,	666}	 /*4:1 RSVD*/
+};
+
+static unsigned short tnc_core_freq_list[SKU_NO] =
+{
+/* sku_100 sku_100L sku_83 */
+	200,	100,	166
+};
+
+/* MCR define */
+#define READ_FUS_EFF0			0xD08106F0
+#define READ_FUS_EFF1			0xD08107F0
+#define READ_FUS_EFF2			0xD08108F0
+#define READ_FUS_EFF3			0xD08109F0
+#define READ_FUS_EFF4			0xD0810AF0
+#define READ_FUS_EFF5			0xD0810BF0
+
+
+
+/*
+ * GTT shutdown.
+ *
+ * Unmap the GTT mapping that was done during init time.
+ */
+static void gtt_shutdown_tnc(igd_context_t *context)
+{
+	if (context->device_context.virt_gttadr) {
+		iounmap(context->device_context.virt_gttadr);
+
+		context->device_context.virt_gttadr = NULL;
+	}
+	if(context->device_context.scratch_page){
+		__free_page(context->device_context.scratch_page);
+		context->device_context.scratch_page = NULL;
+	}
+}
+
+
+/*
+ * Initialize the GTT.
+ *   - Find the size of stolen memory
+ *   - Add stolen memory to the GTT
+ *   - Map the GTT and video memory
+ */
+
+static void gtt_init_tnc(igd_context_t *context)
+{
+	struct drm_device *dev;
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long dvmt_mode = 0;
+	unsigned long gtt_pages = 0;
+	unsigned long stolen_mem_size = 0;
+	unsigned long scratch;
+	unsigned long base;
+	unsigned long pte;
+	unsigned short gmch_ctl;
+	unsigned long pge_ctl;
+	unsigned long gtt_phys_start;
+	unsigned long gatt_start;
+	unsigned long gatt_pages;
+	unsigned long gtt_start;
+	unsigned long gtt_order;
+	unsigned long stolen_mem_base;
+	unsigned long *gtt_table;
+	struct page *gtt_table_page;
+	int i;
+
+	dev = (struct drm_device *)context->drm_dev;
+
+	/* Enable the GMCH */
+	OS_PCI_READ_CONFIG_16((os_pci_dev_t)dev->pdev, PSB_GMCH_CTRL,
+			&gmch_ctl);
+	OS_PCI_WRITE_CONFIG_16((os_pci_dev_t)dev->pdev, PSB_GMCH_CTRL,
+			(gmch_ctl | PSB_GMCH_ENABLED));
+	context->device_context.gmch_ctl = gmch_ctl;
+
+	/* Get the page table control register */
+	pge_ctl = readl(mmio + PSB_PGETBL_CTL);
+	gtt_phys_start = pge_ctl & PAGE_MASK;
+
+	/* Create a scratch page to initialize empty GTT entries */
+	if(NULL == context->device_context.scratch_page){
+		context->device_context.scratch_page = alloc_page(GFP_DMA32 | __GFP_ZERO);
+	}
+
+	/*
+	* Is pci_resource_start(dev->pdev, PSB_GATT_RESOURCE); the same
+	* as pci_read_config_dword(dev->pdev, 0x1C, &value)?
+	*
+	* PSB_GATT_RESOURCE length is the amount of memory addressable
+	* by the GTT table.
+	*/
+	gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
+	gatt_pages = (pci_resource_len(dev->pdev, PSB_GATT_RESOURCE) >> PAGE_SHIFT);
+	context->device_context.gatt_pages = gatt_pages;
+
+	/*
+	 * The GTT wasn't set up by the vBios
+	 */
+	if (!pge_ctl) {
+		context->device_context.stolen_pages = 0;
+
+		gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) >> PAGE_SHIFT;
+		gtt_order = get_order(gtt_pages << PAGE_SHIFT);
+		gtt_table = (unsigned long *)__get_free_pages(GFP_KERNEL, gtt_order);
+		/* Make sure allocation was successful */
+		if (NULL == gtt_table) {
+			EMGD_ERROR("Failed to allocate kernel pages for GTT");
+			return;
+		}
+		context->device_context.virt_gttadr = gtt_table;
+
+		for (i=0; i < (1 << gtt_order); i++) {
+			gtt_table_page = virt_to_page(gtt_table + (PAGE_SIZE * i));
+			EMGD_DEBUG("Setting reserved bit on %p", gtt_table_page);
+			set_bit(PG_reserved, &gtt_table_page->flags);
+		}
+
+		gtt_phys_start = virt_to_phys(gtt_table);
+
+		for (i = 0; i < gtt_pages; i++) {
+			gtt_table[i] = (unsigned long)context->device_context.scratch_page;
+		}
+
+		printk(KERN_INFO "Detected GTT was not enabled by firmware");
+		printk(KERN_INFO "GMMADR(region 0) start: 0x%08lx (%ldM).\n",
+			gatt_start, (gatt_pages / 256));
+		printk(KERN_INFO "GTTADR(region 3) start: 0x%08lx (can map %ldM RAM), and "
+			"actual RAM base 0x%08lx.\n",
+			(unsigned long)gtt_table, (gtt_pages * 4), gtt_phys_start);
+
+		/* Enable the newly created GTT */
+		EMGD_DEBUG("Enabling new GTT");
+		writel(gtt_phys_start, mmio + PSB_PGETBL_CTL);
+		pge_ctl = readl(mmio + PSB_PGETBL_CTL);
+
+	} else {
+
+		/*
+		 * Get the start address of the GTT page table
+		 *
+		 * In full_config_vga, this is done differently.  The address is read
+		 * from pcidev0's pci config space, at TNC_PCI_GTTADR and the size comes
+		 * from TNC_OFFSET_VGA_MSAC. The value read for size is a size id
+		 *    1 = 128, 2 = 256, 3 = 512
+		 * emgd_gtt->gtt_start = OS_PCI_READ_CONFIG_32(
+		 *            context->platform_context->pcidev0, TNC_PCI_GTTADDR)
+		 * gtt_pages = OS_PCI_READ_CONFIG_8(context->platform_context->pcidev0,
+		 *            TNC_OFFSET_VGA_MSAC) * 1024;
+		 *
+		 * PSB_GTT_RESOURCE length is the size of the GTT table. Thus,
+		 * gtt_pages is the number of pages that make up the table.
+		 */
+		gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
+		gtt_pages = (pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) >> PAGE_SHIFT);
+
+		/* Get stolen memory configuration. */
+		pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)&stolen_mem_base);
+		stolen_mem_size = gtt_phys_start - stolen_mem_base - PAGE_SIZE;
+
+		/* Display useful information in the kernel log */
+		printk(KERN_INFO "GMMADR(region 0) start: 0x%08lx (%ldM).\n",
+				gatt_start, (gatt_pages / 256));
+		printk(KERN_INFO "GTTADR(region 3) start: 0x%08lx (can map %ldM RAM), and "
+				"actual RAM base 0x%08lx.\n",
+				gtt_start, (gtt_pages * 4), gtt_phys_start);
+		printk(KERN_INFO "Stolen memory information \n");
+		printk(KERN_INFO "       base in RAM: 0x%lx \n", stolen_mem_base);
+		printk(KERN_INFO "       size: %luK, calculated by (GTT RAM base) - "
+				"(Stolen base)\n", (stolen_mem_size / 1024));
+		dvmt_mode = (gmch_ctl >> 4) & 0x7;
+		printk(KERN_INFO "       size: %dM (dvmt mode=%ld)\n",
+				(dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode);
+
+		context->device_context.virt_gttadr =
+		ioremap_nocache(gtt_start, gtt_pages << PAGE_SHIFT);
+
+		if (!context->device_context.virt_gttadr) {
+			printk(KERN_ERR "Failed to map the GTT.\n");
+			/* TODO: Clean up somelthing here */
+			return;
+		}
+
+		/* Insert stolen memory pages into the beginning of GTT */
+		base = stolen_mem_base >> PAGE_SHIFT;
+		context->device_context.stolen_pages = stolen_mem_size >> PAGE_SHIFT;
+
+		printk(KERN_INFO "Set up %ld stolen pages starting at 0x%08lx, "
+			"GTT offset %dK\n", context->device_context.stolen_pages, base, 0);
+
+		for (i = 0; i < context->device_context.stolen_pages; i++) {
+			pte = ((base + i) << PAGE_SHIFT) | PSB_PTE_VALID;
+			writel(pte, context->device_context.virt_gttadr + i);
+		}
+
+	}
+
+	/* Update the scratch registers to say we have no stolen memory */
+	scratch = readl(mmio + SCR1);
+	if ((scratch & FW_ID) == FW_ID) {
+		/* if an EMGD vBios modify only the stolen memory bit */
+		scratch |= ST_BIT;
+		writel(scratch, mmio + SCR1);
+	} else {
+		/* Not an EMGD vBios so just set the entire register to a known value */
+		writel((FW_ID|ST_BIT), mmio + SCR1);
+	}
+
+	/*
+	 * Report back that there is 0MB of stolen memory regardless of
+	 * what was really in there.  Fresh pages will be inserted over
+	 * the top of the existing stolen memory.
+	 */
+	writel(0, mmio + SCR2);
+
+	/*
+	 * FIXME: Shouldn't this fill in all the GTT page table entries with
+	 * the scratch page?
+	 */
+
+	return;
+}
+
+
+/*!
+ * Helper function to query MCR registers
+ * @param reg
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL on error
+ * @return 0 on success
+ */
+static int query_sch_message(unsigned long reg, unsigned long* value){
+
+	platform_context_tnc_t *platform_context = &platform_context_tnc;
+
+	/* Send the opcode into the MCR */
+	if(OS_PCI_WRITE_CONFIG_32(platform_context->bridgedev,
+		0xD0, reg)){
+		EMGD_ERROR_EXIT("Writing into the MCR Failed");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if(OS_PCI_READ_CONFIG_32(platform_context->bridgedev,
+		0xD4, value)) {
+
+		EMGD_ERROR_EXIT("Writing to MDR Failed");
+		return -IGD_ERROR_INVAL;
+	}
+
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ * @param vga_dev
+ * @param bus
+ * @param slot
+ * @param func
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+static int query_tnc(
+	igd_context_t *context,
+	init_dispatch_t *dispatch,
+	os_pci_dev_t vga_dev,
+	unsigned int *bus,
+	unsigned int *slot,
+	unsigned int *func)
+{
+	platform_context_tnc_t *platform_context = &platform_context_tnc;
+
+	int i = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+
+	/* So we don't have to pollute our function tables with multiple
+	 * entries for every variant of TNC, update the device ID if one
+	 * of the other SKUs is found
+	 */
+	context->device_context.did = PCI_DEVICE_ID_VGA_TNC;
+
+	platform_context->did = context->device_context.did;
+	context->platform_context = (void *)&platform_context_tnc;
+
+	OS_PTHREAD_MUTEX_INIT(&platform_context_tnc.flip_mutex, NULL);
+
+	/* find and store the bridge dev since we will be using it a lot
+	 * in the init modules */
+	while(bridge_id[i] != 0){
+	platform_context->bridgedev = OS_PCI_FIND_DEVICE(
+				PCI_VENDOR_ID_INTEL,
+				bridge_id[i],
+				0xFFFF, /* Scan the whole PCI bus */
+				0,
+				0,
+				(os_pci_dev_t)0);
+		if(platform_context->bridgedev){
+			bridge_dev = platform_context->bridgedev;
+	        context->device_context.bid = bridge_id[i];
+	        break;
+		}
+		i++;
+	 }
+	/*
+	 * Current specs indicate that Atom E6xx has only one PCI function.
+	 * If this changes then we need to make sure we have func 0
+	 * here as in previous chips.
+	 */
+	platform_context->pcidev0 = vga_dev;
+
+	/* find device 3 */
+	platform_context->pcidev1 = OS_PCI_FIND_DEVICE(PCI_VENDOR_ID_INTEL,
+			PCI_DEVICE_ID_SDVO_TNC,
+			0,
+			3,
+			0,
+			(os_pci_dev_t)0);
+
+	platform_context->stbridgedev = OS_PCI_FIND_DEVICE(PCI_VENDOR_ID_STMICRO,
+			PCI_DEVICE_ID_SDVO_TNC_ST,
+			6,
+			0,
+			1,
+			(os_pci_dev_t)0);
+
+	if (platform_context->stbridgedev) {
+		platform_context->stgpiodev = OS_PCI_FIND_DEVICE(PCI_VENDOR_ID_STMICRO,
+			PCI_DEVICE_ID_SDVO_TNC_ST_GPIO,
+			3,
+			0,
+			0,
+			(os_pci_dev_t)0);
+
+		if (!platform_context->stgpiodev) {
+			platform_context->stgpiodev = OS_PCI_FIND_DEVICE(PCI_VENDOR_ID_STMICRO,
+				PCI_DEVICE_ID_SDVO_TNC_ST_GPIO,
+				4,
+				0,
+				5,
+				(os_pci_dev_t)0);
+			if (!platform_context->stgpiodev) {
+				printk("Using STM device, but is not CUT1 or CUT2\n");
+				EMGD_ERROR_EXIT("Using STM device, but is not Cut1 or Cut2");
+				return -IGD_ERROR_NODEV;
+			}
+		}
+	}
+
+	/* Set to NULL, so full_shutdown_tnc() knows whether it was initialized: */
+	platform_context->lpc_dev = NULL;
+
+	/*
+	 * finds the bus, device, func to be returned. Do this for D2:F0 only.
+	 * the OS does not need to know the existence of D3:F0
+	 */
+	OS_PCI_GET_SLOT_ADDRESS(vga_dev, bus, slot, func);
+
+	get_revision_id_tnc(context, vga_dev, platform_context->pcidev1);
+
+	/*
+	 * Read BSM.
+	 * This must be in query so it is available early for the vBIOS.
+	 */
+	if(OS_PCI_READ_CONFIG_32(vga_dev,
+			TNC_PCI_BSM, &context->device_context.fb_adr)) {
+		EMGD_ERROR_EXIT("Reading BSM");
+		return -IGD_ERROR_NODEV;
+	}
+	context->device_context.fb_adr &= 0xFFFFF000;
+
+	EMGD_DEBUG("BSM (High)@: 0x%lx, (Low) 0x%4lx",
+		(context->device_context.fb_adr >> 16), context->device_context.fb_adr);
+
+	/*
+	 * Read IO Base.
+	 * This must be in query so it is available early for the vBIOS.
+	 */
+	if(OS_PCI_READ_CONFIG_16(vga_dev, TNC_PCI_IOBAR, &io_base)) {
+		EMGD_ERROR_EXIT("Reading IO Base");
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Base Address is defined in Bits 15:3*/
+	io_base_lvds = io_base &= 0xfff8;
+	EMGD_DEBUG("io @: 0x%x", io_base);
+
+	/* Gen4 is always io_mapped */
+	io_mapped_lvds = io_mapped = 1;
+
+	/* Set dev3 iobase.  */
+	if(OS_PCI_READ_CONFIG_16((os_pci_dev_t)platform_context->pcidev1,
+		TNC_PCI_IOBAR, &io_base_sdvo)) {
+
+		EMGD_ERROR_EXIT("Reading SDVO IO Base");
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Base Address is defined in Bits 15:3*/
+	io_base_sdvo &= 0xfff8;
+
+	io_mapped_sdvo = 1;
+	EMGD_DEBUG("sdvo io @: 0x%x", io_base_sdvo);
+
+	/* Set stmicro sdvo iobase.  */
+	if(OS_PCI_READ_CONFIG_16((os_pci_dev_t)platform_context->stbridgedev,
+		TNC_PCI_IOBAR, &io_base_sdvo_st)) {
+
+		EMGD_ERROR_EXIT("Reading SDVO IO Base");
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Base Address is defined in Bits 15:3*/
+	io_base_sdvo_st &= 0xfff8;
+
+	io_mapped_sdvo_st = 1;
+	EMGD_DEBUG("STMicro's sdvo io @: 0x%x", io_base_sdvo_st);
+
+	/* Set stmicro gpio sdvo iobase.  */
+	if(OS_PCI_READ_CONFIG_16((os_pci_dev_t)platform_context->stgpiodev,
+		TNC_PCI_IOBAR, &io_base_sdvo_st_gpio)) {
+
+		EMGD_ERROR_EXIT("Reading SDVO IO Base");
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Base Address is defined in Bits 15:3*/
+	io_base_sdvo_st_gpio &= 0xfff8;
+
+	io_mapped_sdvo_st_gpio = 1;
+	EMGD_DEBUG("STMicro's gpio io @: 0x%x", io_base_sdvo_st_gpio);
+
+	/* ---------------------------------------------------
+	 * Initialize Device 31 : LPC Interface
+	 * --------------------------------------------------*/
+	/*
+	 * Map the LPC Interface Configuration [D31:F0]GPIO_BAR.
+	 * The Atom E6xx LVDS pins are connected to GPIO pins,
+	 * accessible using LPC Interface GPIO_BAR. These registers
+	 * will later be used to "bit bash" the LVDS DDC signals
+	 * SDVO does not need these registers.
+	 * VBIOS may need access to these registers
+	 */
+
+	platform_context->lpc_dev = OS_PCI_FIND_DEVICE(PCI_VENDOR_ID_INTEL,
+			PCI_DEVICE_ID_LPC_TNC,
+			0,
+			31, /* LPC[D31:F0] */
+			0,
+			(os_pci_dev_t)0);
+
+	if(!platform_context->lpc_dev){
+		/*
+		 * We could not detect the LPC interface in the PCI Bus. This will
+		 * be a problem. Sound the alarm, return with NO ERROR so that we do
+		 * not go and map the GPIO_BAR
+		 */
+		EMGD_ERROR_EXIT("Reading GPIO BAR");
+		return 0;
+	}
+
+	/* Set dev31 iobase */
+
+	/* Do not enable LPC device as System BIOS owns and does this */
+
+	/* read the GPIO BAR (OFFSET 44:47) */
+	if(OS_PCI_READ_CONFIG_16(platform_context->lpc_dev,
+			TNC_PCI_GBA, &io_base_lpc)) {
+		EMGD_ERROR_EXIT("Reading LPC GPIO BAR");
+		/* We cannot read the GPIO BAR. It is a problem but we can go on with init
+		 * return with NO ERROR*/
+		return 0;
+	}
+
+	io_base_lpc &= 0xffc0;
+
+	io_mapped_lpc = 1;
+	EMGD_DEBUG("lpc io @: 0x%x", io_base_lpc);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+static int config_tnc(igd_context_t *context,
+	init_dispatch_t *dispatch)
+{
+	unsigned long freq[2];
+#ifndef CONFIG_MICRO
+	unsigned int lp_ctrl_reg;
+	unsigned int hp_ctrl_reg;
+	unsigned int ved_cg_dis_reg;
+#endif
+
+	EMGD_TRACE_ENTER;
+
+	OPT_MICRO_CALL(full_config_tnc(context, dispatch));
+
+	/* Get graphics and core frequency param if it exists */
+	if(!get_param_tnc(context, IGD_PARAM_GFX_FREQ,
+			freq)) {
+		context->device_context.gfx_freq = (unsigned short)freq[0];
+		context->device_context.core_freq = (unsigned short)freq[1];
+	}
+
+	/*
+	 * FIXME:
+	 *  Coreclk register above is used to determine some clocking information
+	 *  there is also a fuse to limit the dclk. More research needed.
+	 */
+	/* From KT: Atom E6xx LVDS min and max dot clocks are 19.75 MHz to 79.5 MHz,
+	 *          Atom E6xx SDVO min and max dot clocks are 25 MHz to 165 MHz */
+	context->device_context.max_dclk = 79500;   /* in KHz */
+
+#ifndef CONFIG_MICRO
+	/* This breaks VBIOS LVDS display.  If this is truly a workaround for
+	 * system BIOS then we need to understand what the system BIOS is going
+	 * to do and make sure it doesn't re-break VBIOS. The hp_ctrl_reg write
+	 * is the write that actually breaks LVDS display.  Tested with BIOS34
+	 * which has the P-Unit workaround and LVDS still works.
+     */
+
+	/* This is just a workaround.
+	 * GVD.G_LP_Control register is set to default mode for BIT0~BIT3.
+	 * GVD.H_HP Control register's BIT1 is set 1.
+	 * TODO: Removed this after this is fix in system BIOS.
+	 */
+	lp_ctrl_reg = EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) + 0x20f4);
+	lp_ctrl_reg |= BIT1;
+	lp_ctrl_reg &= ~(BIT2 | BIT3);
+    EMGD_WRITE32(lp_ctrl_reg, EMGD_MMIO(context->device_context.virt_mmadr) + 0x20f4);
+
+	hp_ctrl_reg = EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) + 0x20f8);
+	hp_ctrl_reg |= BIT1;
+    EMGD_WRITE32(hp_ctrl_reg, EMGD_MMIO(context->device_context.virt_mmadr) + 0x20f8);
+
+	/* This is just a workaround.
+	 * GVD.VED_CG_DIS register is set to disable clock gating for BIT16, BIT0~BIT8.
+	 * Tested with Punit B0_500309_CFG2 and Punit C0_060510_CFG2 in BIOS39 and
+	 * BIOS41 or above.
+	 */
+	ved_cg_dis_reg = EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) + 0x2064);
+	ved_cg_dis_reg |= (BIT16 | BIT8 | 0xFF);
+    EMGD_WRITE32(ved_cg_dis_reg, EMGD_MMIO(context->device_context.virt_mmadr) + 0x2064);
+
+	/* read out the fuse values */
+	dump_fuse_values( );
+#endif
+
+	gtt_init_tnc(context);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+static int get_param_tnc(igd_context_t *context, unsigned long id,
+	unsigned long *value)
+{
+#define FB_SKU_MASK  (BIT12|BIT13|BIT14)
+#define FB_SKU_SHIFT 12
+#define FB_GFX_CLOCK_DIVIDE_MASK  (BIT20|BIT21|BIT22)
+#define FB_GFX_CLOCK_DIVIDE_SHIFT 20
+
+	int ret = 0;
+	unsigned long control_reg;
+	unsigned short sku;
+	unsigned short ratio;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("ID: 0x%lx", id);
+
+	/* Scratch registers used as below:
+	 *
+	 * 0x71410:
+	 * --------
+	 *   Bits 31-16 - EID Firmware identifier 0xE1DF
+	 *   Bits 15-00 - Tell what data is present.
+	 * Here are bits for what we are using know:
+	 *   Bit 0 - Panel id
+	 *   Bit 1 - List of ports for which displays are attached
+	 *   Bit 2 - Memory reservation
+	 * If any of the above bits is set that mean data is followed
+	 * in the next registers.
+	 *
+	 * 0x71414:
+	 * --------
+	 *   Bits 07-00 - Panel Id
+	 *   Bits 11-08 - Port list
+	 * Information for Port list: If any of the bit is set means,
+	 *   a display is attached to that port as follows:
+	 *    Bit 08 - CRT
+	 *    Bit 09 - DVOA/Internal LVDS
+	 *    Bit 10 - DVOB/RGBA
+	 *    Bit 11 - DVOC
+	 *
+	 * 0x71418:
+	 * --------
+	 * Bits 15-00 - Reserved Memory value in number of 4k size pages
+	 */
+	*value = 0;
+
+	switch(id) {
+	case IGD_PARAM_PORT_LIST:
+
+		control_reg = EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) +
+			0x71410);
+
+		/*
+		 * Verify that the Embedded Firware is present.
+		 */
+		if ((control_reg>>16) != 0xE1DF) {
+			EMGD_DEBUG("Exit No Embedded vBIOS found");
+			EMGD_TRACE_EXIT;
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * If the port list bit is set in control register,
+		 * read the port list
+		 */
+		if (control_reg & 0x2) {
+			unsigned char temp;
+			int i = 0;
+
+			temp = (unsigned char)((EMGD_READ32(EMGD_MMIO(context->device_context.virt_mmadr) + 0x71414)>>8) & 0xFF);
+			EMGD_DEBUG("Connected Port Bits: 0x%x", temp);
+
+			/*
+			 * The meanings of bits in temp were dictated by VBIOS
+			 * and should not change due to backward compatibility
+			 * with Legacy VBIOS
+			 */
+			if (temp & 0x02) {
+				/* Internal LVDS port */
+				value[i++] = 4;
+			}
+			if (temp & 0x04) {
+				/* DVOB Port */
+				value[i++] = 2;
+			}
+		} else {
+			EMGD_DEBUG("Port List read failed: Incorrect Operation");
+			ret = -IGD_ERROR_INVAL;
+		}
+		break;
+	case IGD_PARAM_GFX_FREQ:
+
+		/* Read the fuse value */
+		if(query_sch_message(READ_FUS_EFF3, &control_reg)){
+			EMGD_ERROR("Cannot read GFX clock");
+		}
+		EMGD_DEBUG("SKU [reg 0x%x] value = 0x%lx", READ_FUS_EFF3, control_reg);
+
+		/*
+		 * Sku and Ratio bits determine the gfx clock speed
+		 * sku - 0:sku_100 1:sku_100L 2:sku_83
+		 * ratio - 0-1:1 1-4:3 2-8:5 3-2:1 4-16:7(rsvd) 5-8:3 6-16:5 7:4:1(rsvd)
+		 */
+		sku = (unsigned short)((control_reg & FB_SKU_MASK) >> FB_SKU_SHIFT) & 0x3;
+		ratio = (unsigned short)((control_reg & FB_GFX_CLOCK_DIVIDE_MASK) >> FB_GFX_CLOCK_DIVIDE_SHIFT) & 0x7;
+
+		EMGD_DEBUG("sku = 0x%x Ratio = 0x%x", sku, ratio);
+
+		if(sku < SKU_NO && ratio < RATIO_NO){
+			/* get the graphics clock speed from the sku-ratio array */
+			value[0] = tnc_gfx_freq_list[ratio][sku];
+			value[1] = tnc_core_freq_list[sku];
+		} else {
+			EMGD_ERROR("tnc_gfx_freq_list ARRAY OUT OF RANGE");
+			/* set to the lowest default value */
+			value[0] = 333;
+			value[1] = 166;
+		}
+
+		EMGD_DEBUG("TNC GFX core frequency = %lu MHz", value[0]);
+		EMGD_DEBUG("TNC Core clock frequency = %lu MHz", value[1]);
+
+		break;
+
+	default:
+		/* If the param is not found here then it may only be in the
+		 * full version.
+		 */
+		OPT_MICRO_CALL_RET(ret, full_get_param_tnc(context, id, value));
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ *
+ * @param context
+ * @param id
+ * @param value
+ *
+ * @return -IGD_ERROR_INVAL
+ */
+static int set_param_tnc(igd_context_t *context, unsigned long id,
+	unsigned long value)
+{
+	return 0;
+}
+
+/*!
+ * Functions reads all the fuse values and dumps out the value
+ * @return -IGD_ERROR_INVAL
+ */
+#ifndef CONFIG_MICRO
+static int dump_fuse_values(void)
+{
+	unsigned long value = 0;
+
+	if(query_sch_message(READ_FUS_EFF0, &value)){
+		EMGD_ERROR_EXIT("Reading Fuse Value Failed");
+	}
+	EMGD_DEBUG("READ_FUS_EFF0 [%lx]", value);
+
+	if(query_sch_message(READ_FUS_EFF1, &value)){
+		EMGD_ERROR_EXIT("Reading Fuse Value Failed");
+	}
+	EMGD_DEBUG("READ_FUS_EFF1 [%lx]", value);
+
+	if(query_sch_message(READ_FUS_EFF2, &value)){
+		EMGD_ERROR_EXIT("Reading Fuse Value Failed");
+	}
+	EMGD_DEBUG("READ_FUS_EFF2 [%lx]", value);
+
+	if(query_sch_message(READ_FUS_EFF3, &value)){
+		EMGD_ERROR_EXIT("Reading Fuse Value Failed");
+	}
+	EMGD_DEBUG("READ_FUS_EFF3 [%lx]", value);
+
+	if(query_sch_message(READ_FUS_EFF4, &value)){
+		EMGD_ERROR_EXIT("Reading Fuse Value Failed");
+	}
+	EMGD_DEBUG("READ_FUS_EFF4 [%lx]", value);
+
+	if(query_sch_message(READ_FUS_EFF5, &value)){
+		EMGD_ERROR_EXIT("Reading Fuse Value Failed");
+	}
+	EMGD_DEBUG("READ_FUS_EFF5 [%lx]", value);
+
+	return 0;
+}
+#endif
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void shutdown_tnc(igd_context_t *context)
+{
+	gtt_shutdown_tnc(context);
+
+	OPT_MICRO_VOID_CALL(full_shutdown_tnc(context));
+}
+
+
+/*!
+ *
+ * @param context
+ * @param dispatch
+ * @param vga_dev
+ *
+ * @return -IGD_ERROR_NODEV on failure
+ * @return 0 on success
+ */
+int get_revision_id_tnc(igd_context_t *context,
+	os_pci_dev_t vga_dev,
+	os_pci_dev_t sdvo_dev)
+{
+	platform_context_tnc_t *platform_context;
+
+	EMGD_TRACE_ENTER;
+  	 
+	platform_context = (platform_context_tnc_t *)context->platform_context;
+
+	/* Read RID */
+	if(OS_PCI_READ_CONFIG_8(vga_dev, PCI_RID,
+		(unsigned char *)&context->device_context.rid)) {
+		EMGD_ERROR_EXIT("Error occured reading RID");
+		return -IGD_ERROR_NODEV;
+	}
+  	 
+	if(OS_PCI_READ_CONFIG_8(sdvo_dev, PCI_RID,
+		&platform_context->tnc_dev3_rid)) {
+		EMGD_ERROR_EXIT("Error occured reading TNC SDVO RID");
+		return -IGD_ERROR_NODEV;
+	}
+  	 
+	EMGD_DEBUG(" rid = 0x%lx", context->device_context.rid);
+	
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp.c b/drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp.c
new file mode 100755
index 0000000..d4abefb
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp.c
@@ -0,0 +1,2366 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dsp.c
+ * $Revision: 1.27 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains all the necessary functions for display resource
+ *  manager. This module abstracts all hardware resources and manages them.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dsp
+
+#include <config.h>
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd.h>
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_errno.h>
+#include <igd_gmm.h>
+
+#include <context.h>
+#include <utils.h>
+#include <intelpci.h>
+#include <dsp.h>
+#include <debug.h>
+#include <dispatch.h>
+#include <pd.h>
+#include <mode.h>
+#include <pd_init.h>
+#include <gart.h>
+
+#include "dsp_dispatch.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+static dispatch_table_t dsp_dispatch_list[] = {
+
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &dsp_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &dsp_dispatch_tnc},
+#endif
+	{0, NULL}
+};
+
+static int dsp_full_init(igd_context_t *context);
+static int dsp_init_cursor(igd_context_t *context, igd_cursor_t *cursor);
+static igd_display_port_t *dsp_get_next_port(igd_context_t *context,
+	igd_display_port_t *last, int reverse);
+
+/*
+ * This simple structure wraps the display configuration list so that
+ * it is easier to manipulate.
+ *    count is the current number of items in the list.
+ *    size is the maximum number of items the list can hold
+ *    dc_list is a pointer to the array of DC's
+ */
+typedef struct _dsp_dc_list_t {
+	int count;
+	int size;
+	unsigned long *dc_list;
+} dsp_dc_list_t;
+
+typedef struct _dsp_context {
+
+	dsp_dispatch_t *dispatch;
+	igd_context_t *context;
+	unsigned long num_dsp_planes;
+	unsigned long num_dsp_pipes;
+	unsigned long display_flags;
+
+	unsigned long current_dc;
+	unsigned long fw_dc; /* The DC programmed by the EFI or VBIOS */
+	igd_display_context_t display_list[MAX_DISPLAYS];
+	dsp_dc_list_t dsp_dc_list;
+	/* holds pointer to port based on port numbers (1 based not zero based) */
+	igd_display_port_t *port_list[IGD_MAX_PORTS + 1];
+
+	/* holds pointer to display context based on port numbers
+	 * (1 based not zero based) */
+	igd_display_context_t *display_ptr_list[IGD_MAX_PORTS + 1];
+} dsp_context_t;
+
+static dsp_context_t dsp_context;
+
+
+#ifndef CONFIG_MICRO
+#define FREE_PIPE(x) free_pipe(x)
+
+
+/*
+ * This macro is used to check the validity of a clone/extended DC. It
+ * makes sure that the ports are capable of using both pipes.  p1 and
+ * p2 need to be on unique pipes, if they both require the same pipe,
+ * this check returns false.
+ *
+ * Note that this is only enabled for the driver since it does add some
+ * code and currently we have no hardware that could be configured like
+ * this.
+ */
+#define DSP_PIPE_OK(p1, p2) ( \
+		((p1->port_features & IGD_PORT_USE_PIPE_MASK) | \
+		(p2->port_features & IGD_PORT_USE_PIPE_MASK)) == \
+		(IGD_PORT_USE_PIPEA | IGD_PORT_USE_PIPEB))
+
+#else
+#define FREE_PIPE(x)
+#define DSP_PIPE_OK(p1, p2) 1
+#endif
+
+/*
+ * Static framebuffer structures used for all chipsets.
+ */
+igd_framebuffer_info_t fb_info_cmn[2] = {
+	{0, 0, 0, 0, 0, 0, 0},
+	{0, 0, 0, 0, 0, 0, 0}
+};
+
+/* Design Notes:
+ *
+ *   1. The heart of display resources is the display configuration list.
+ *      During initialization, the list display configuration list is
+ *      created with all valid pipe/plane/port combinations.  Allocations
+ *      are limited to combinations specified on the list.
+ *
+ *   2. Two display handles are maintaine corresponding to the two
+ *      display pipes. To support hardware with more than two pipes,
+ *      the DC value will need to expand beyond the current 32 bit value
+ *      and the number of display handles increased appropriately.
+ */
+
+#ifndef CONFIG_MICRO /* This is N/A for VBIOS */
+
+/*!
+ * Re-construct the DC ( Display Configuration ) that the Gfx hardware
+ * was programmed by Video BIOS or EFI Video Driver.
+ *
+ * TODO: This function does not handle three-display scenario.
+ *
+ * @param context
+ *
+ * @return fw_dc
+ */
+static unsigned long dsp_get_fw_dc(igd_context_t *context)
+{
+	igd_display_port_t *p = NULL;
+	int pipeb_allocated = 0, pipea_allocated = 0;
+	int port1 = 0, port2 = 0, mode = 0;
+	int port_allocated = 0;
+	unsigned long port_value;
+	unsigned long fw_dc = 0;
+	unsigned char *mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+
+	EMGD_TRACE_ENTER;
+
+	/* Go through the port table */
+	while ((p = dsp_get_next_port(context, p, 0)) != NULL) {
+
+		port_value = EMGD_READ32(mmio + p->port_reg);
+		if(port_value & BIT(31)) { /* is the port ON? */
+
+			if(port1) {
+				port2 = p->port_number;
+			} else {
+				port1 = p->port_number;
+			}
+			port_allocated++;
+
+			if (port_value & BIT(30)) {
+				pipeb_allocated++;
+			} else	{
+				pipea_allocated++;
+			}
+		}
+	} /* while */
+
+	if( pipea_allocated > 1 || pipeb_allocated > 1)	{
+		/* Twin Mode */
+		EMGD_DEBUG("Twin mode");
+		mode = IGD_DISPLAY_CONFIG_TWIN;
+
+		/* Re-construct the DC back */
+		fw_dc = (port2 & 0x0F)<< 8 | (port1 & 0x0F)<< 4 | (mode & 0x0F);
+
+		EMGD_DEBUG("fw_dc in Twin Mode: 0x%08lx", fw_dc);
+
+		/* Check the fw_dc is in the list */
+		if(!dsp_valid_dc(fw_dc, 0)) {
+			EMGD_DEBUG("check the fw_dc  again in the DC list");
+
+			/* If the assumed dc doesn't match reverse the order and
+			 * and check again
+			 */
+			fw_dc = (port1 & 0x0F)<< 8 | (port2 & 0x0F)<< 4 | (mode & 0x0F);
+			EMGD_DEBUG("hw_dc: 0x%08lx", fw_dc);
+
+			if(!dsp_valid_dc(fw_dc, 0)) {
+				EMGD_DEBUG("FW DC doesnt match dc list!!");
+				fw_dc = 0L;
+			}
+		}
+
+	} else if(port_allocated == 1) {
+
+		EMGD_DEBUG("Single Configuration");
+		mode = IGD_DISPLAY_CONFIG_SINGLE;
+		/* Single Mode */
+		fw_dc = (port2 & 0x0F)<< 20 |(port1 & 0x0F)<<4 |(mode & 0x0F);
+
+		EMGD_DEBUG("fw_dc in Single Config  = 0x%08lx", fw_dc);
+
+		/* Checking the assumed dc with the dc list */
+		if(!dsp_valid_dc(fw_dc, 0)) {
+			EMGD_DEBUG("check again");
+
+			/* If the assumed dc doesn't match reverse the order and
+			 * and check again
+			 */
+			fw_dc = (port1 & 0x0F)<< 20 |(port2 & 0x0F)<<4 |(mode & 0x0F);
+			if(!dsp_valid_dc(fw_dc, 0)) {
+				EMGD_DEBUG("FW DC doesnt match dc list!!!");
+				fw_dc = 0L;
+			}
+		}
+
+	} else {
+
+		/*  VBIOS does not support extended. So i'ts most
+		 *  likely clone.
+		 */
+		EMGD_DEBUG("Extended/Clone");
+		mode = IGD_DISPLAY_CONFIG_CLONE;
+
+		/* Re-construct the DC back */
+		fw_dc = (port2 & 0x0F) << 20 | (port1 & 0x0F) << 4 | (mode & 0x0F);
+
+		EMGD_DEBUG("fw_dc in Clone Mode:a 0x%08lx", fw_dc);
+
+		/* Check the fw_dc is in the list */
+		if(!dsp_valid_dc(fw_dc, 0)) {
+			EMGD_DEBUG("Check again. This time reverse primary and secondary");
+
+			/* if the assumed dc doesn't match reverse the order and
+			 * check again
+			 */
+			fw_dc = (port1 & 0x0F) << 20 | (port2 & 0x0F) << 4 | (mode & 0x0F);
+			if(!dsp_valid_dc(fw_dc, 0)) {
+				EMGD_DEBUG("FW DC doesnot match dc list!!!");
+				fw_dc = 0L;
+			}
+		}
+	} /* else */
+
+	EMGD_DEBUG("DC programmed by the firmware = 0x%08lx", fw_dc);
+	EMGD_TRACE_EXIT;
+	//*dc_list=&current_dc;
+	return fw_dc;
+
+} /* end of dsp_get_fw_dc */
+
+#endif /* ifndef  CONFIG_MICRO */
+
+/*!
+ * This function gets the next value in a plane, pipe, or port table. The
+ * caller provides a pointer to the last entry retrieved or NULL to get
+ * the first entry.
+ *
+ * @param list
+ * @param last
+ * @param reverse
+ *
+ * @return void
+ */
+static void *dsp_get_next(void **list, void *last, int reverse)
+
+{
+	void **list_end = list;
+	void **list_start = list;
+	void **last_p = NULL;
+	int off = 1;
+
+	while(*list_end) {
+		if(*list_end == last) {
+			last_p = list_end;
+		}
+		list_end++;
+	}
+	list_end--;
+
+	if(reverse) {
+		list_start = list_end;
+		list_end = list;
+		off = -1;
+	}
+	if(!last) {
+		return *list_start;
+	}
+	if((last == *list_end) || !last_p) {
+		return NULL;
+	}
+	return last_p[off];
+}
+
+/*!
+ * This function allows the caller to loop through the (port | plane | pipe)
+ * table list and get each entry in turn. The call must provide a pointer
+ * to the last entry retrieved (or NULL to get the first port).
+ *
+ * These functions are called from many places, including some functions
+ * external to the DSP module.
+ *
+ * @param context pointer to the current driver context.
+ * @param last pointer to the last entry retrieved.
+ * @param reverse
+ *
+ * @return igd_display_port_t pointer to the next entry or NULL if no more ports available.
+ */
+static igd_display_port_t *dsp_get_next_port(igd_context_t *context,
+	igd_display_port_t *last, int reverse)
+{
+	/*EMGD_DEBUG("Entry, dsp_get_next_port");*/
+
+	return (igd_display_port_t *)dsp_get_next(
+		(void **)dsp_context.dispatch->ports, (void *)last, reverse);
+}
+
+static igd_plane_t *dsp_get_next_plane(igd_context_t *context,
+	igd_plane_t *last, int reverse)
+{
+	EMGD_DEBUG("Entry, dsp_get_next_plane");
+
+	return (igd_plane_t *)dsp_get_next(
+		(void **)dsp_context.dispatch->planes, (void *)last, reverse);
+}
+
+static igd_display_pipe_t *dsp_get_next_pipe(igd_context_t *context,
+	igd_display_pipe_t *last, int reverse)
+{
+	EMGD_DEBUG("Entry, dsp_get_next_pipe");
+
+	return (igd_display_pipe_t *)dsp_get_next(
+		(void **)dsp_context.dispatch->pipes, (void *)last, reverse);
+}
+
+/*!
+ * Check the DC to see if it is in the list.
+ *
+ * Flags:
+ *       IGD_DC_EXACT_MATCH
+ *       IGD_DC_CLOSEST_MATCH
+ *
+ * Flags are currently ignored.
+ *
+ * @param dc
+ * @param flags
+ *
+ * @return DC
+ * @return 0 if no match
+ */
+unsigned long dsp_valid_dc(unsigned long dc, unsigned long flags)
+{
+	int i;
+
+	for (i = 0; i < dsp_context.dsp_dc_list.count; i++) {
+		if (dc == dsp_context.dsp_dc_list.dc_list[i]) {
+			return dsp_context.dsp_dc_list.dc_list[i];
+		}
+	}
+
+	/* Need to define how a closest match is determined */
+
+	return 0;
+}
+
+/*!
+ * Convert a DC value into to bitmasks, one for each pipe.
+ *
+ * @param dc
+ * @param pipe1
+ * @param pipe2
+ *
+ * @return void
+ */
+static void dsp_dc_to_masks(unsigned long dc,
+		unsigned char *pipe1,
+		unsigned char *pipe2)
+{
+	int i;
+	int pn;
+
+	*pipe1 = 0;
+	*pipe2 = 0;
+
+	for (i = 1; i < 8; i++) {
+		if ((pn = DC_PORT_NUMBER(dc, i)) > 0) {
+			if ((i < 5)) {
+				*pipe1 |= (1 << pn);
+			} else {
+				*pipe2 |= (1 << pn);
+			}
+		}
+	}
+}
+
+/*!
+ *
+ * @param context
+ * @param port
+ *
+ * @return 1 if connected
+ * @return 0 if not connected
+ */
+int dsp_display_connected(igd_context_t *context,
+	igd_display_port_t *port)
+{
+	unsigned long port_list[IGD_MAX_PORTS];
+	unsigned long connected_ports;
+	int ret;
+	pd_port_status_t port_status;
+
+	/* Display detection -
+	 *   First, check if the VBIOS has provided the "connected_bits"
+	 *   information. If it has, then check if the passed in port is in
+	 *   the list.  Of course, this is only valid in the driver, vBIOS
+	 *   can't check vBIOS bits.
+	 *
+	 *   If there isn't any "connected_bits" info, then have the port
+	 *   driver try and detect the display.  If the display is detected
+	 *   or if the port driver is incapabile of doing display detection,
+	 *   then return connected.
+	 */
+	OS_MEMSET(port_list, 0, sizeof(unsigned long) * IGD_MAX_PORTS);
+	connected_ports = 0;
+	if (igd_get_param((igd_driver_h)context, IGD_PARAM_PORT_LIST,
+				port_list) == 0) {
+		while ((connected_ports < IGD_MAX_PORTS) &&
+				port_list[connected_ports]) {
+			if(port_list[connected_ports] == port->port_number) {
+					return 1;
+			}
+			connected_ports++;
+		}
+		return 0;
+	}
+
+	/* No vBIOS info, so do runtime detection if possible. */
+	if (port->pd_context == NULL) {
+		return 0;
+	}
+
+	ret = port->pd_driver->pd_get_port_status(port->pd_context, &port_status);
+	if ((ret != PD_SUCCESS) ||
+			(port_status.connected == PD_DISP_STATUS_DETACHED)) {
+		return 0;
+	}
+
+	/* Port is connected (as far as we can tell) and OK to use */
+	return 1;
+}
+
+/*!
+ * Checks a given port to make sure it is ok to use it in a
+ * display configuration. Two things can make a port unusable.
+ * First it can be marked inuse, which means it isn't listed in
+ * the current port_order.
+ * Second, if display detect is enabled and no display is detected.
+ *
+ * @param context
+ * @param port
+ * @param display_detect
+ *
+ * @return 0 if not ok to use
+ * @return 1 if ok to use
+ */
+static int dsp_port_is_ok(igd_context_t *context,
+	igd_display_port_t *port,
+	int display_detect)
+{
+	/*
+	 * The port->inuse value is used in two different ways.  This is
+	 * causing a conflict now.
+	 *
+	 *   - It is set if the port doesn't show up in the port order.
+	 *   - It is set if the port is allocated by dsp_alloc()
+	 *
+	 * This is ok if this function is called prior to any dsp_alloc's
+	 *
+	 * Maybe what is really needed here is a check to see if the port
+	 * shows up in the port order.
+	 */
+
+	/* If it is inuse, then it isn't currently ok to use */
+	if (port->inuse == 0x80) {
+		EMGD_DEBUG("port_ok? Port is marked unsable");
+		return 0;
+	}
+
+	/* Does port have a port driver? */
+	if (port->pd_driver == NULL) {
+		EMGD_DEBUG("port %ld has no port driver", port->port_number);
+		return 0;
+	}
+
+	/* If display detect, then check to make sure display is present */
+	if (display_detect) {
+		return dsp_display_connected(context, port);
+	}
+	return 1;
+}
+
+/*!
+ * Add a new DC to the end of the list. Checks to make sure there is
+ * space in the allocated list first.
+ *
+ * @param dc_list
+ * @param dc
+ * @param ext
+ *
+ * @return void
+ */
+void dsp_add_to_dc_list(dsp_dc_list_t *dc_list, unsigned long dc,
+		unsigned long ext)
+{
+	unsigned char pipe1, pipe2;
+	unsigned char p1, p2;
+	int d;
+	int ok = 1;
+
+	/* Check for duplicate DC first */
+	if ((dc_list->count != 0) && (dc != 0)) {
+		dsp_dc_to_masks(dc, &pipe1, &pipe2);
+		for (d = 0; d < dc_list->count; d++) {
+			/* build a bitmap of port used by DC under test */
+			dsp_dc_to_masks(dc_list->dc_list[d], &p1, &p2);
+
+			/*
+			 * Compare the bitmaps.
+			 *
+			 * There are two bitmaps, one for each pipe. This is needed so
+			 * 0x00500232 and 0x00200532 aren't flaged as duplicate.
+			 */
+			if ((pipe1 == p1) && (pipe2 == p2)) {
+				ok = 0; /* duplicate */
+			}
+			if ((pipe1 == p2) && (pipe2 == p1)) {
+				ok = 0; /* a pipe reversed duplicate DC */
+			}
+		}
+	}
+
+	if (ok) {
+		if (dc_list->count < dc_list->size) {
+			dc_list->dc_list[dc_list->count] = dc;
+			dc_list->count++;
+		}
+		if (ext && (dc_list->count < dc_list->size)) {
+			dc = (dc & 0xfffffff0) | 8;
+			dc_list->dc_list[dc_list->count] = dc;
+			dc_list->count++;
+		}
+	}
+}
+
+/*!
+ * Given a port number, find which display context currently controls
+ * that port.
+ *
+ * @param port_number
+ * @param display
+ * @param port
+ * @param display_detect
+ *
+ * @return void
+ */
+static void dsp_get_display(unsigned short port_number,
+	igd_display_context_t **display,
+	igd_display_port_t **_port,
+	int display_detect)
+{
+	igd_display_port_t *port = NULL;
+
+	if(_port) {
+		while ((port = dsp_get_next_port(dsp_context.context, port, 0))) {
+			if (port->port_number == port_number) {
+				*_port = port;
+				if (display_detect &&
+					(dsp_display_connected(dsp_context.context, port) != 1)) {
+					EMGD_DEBUG("Usable but unattached port found");
+					*_port = NULL;
+				}
+				break;
+			}
+		}
+	}
+	if(display) {
+	  *display = dsp_context.display_ptr_list[port_number];
+	}
+	return;
+}
+
+/*!
+ *
+ * @param dc
+ * @param primary
+ * @param secondary
+ *
+ * @return void
+ */
+static void dsp_get_dc(unsigned long *dc,
+	igd_display_context_t **primary,
+	igd_display_context_t **secondary)
+{
+	/*EMGD_TRACE_ENTER;*/
+	if(dc) {
+		*dc = dsp_context.current_dc;
+	}
+	if(primary) {
+		*primary =
+			dsp_context.display_ptr_list[
+				IGD_DC_PRIMARY(dsp_context.current_dc)];
+	}
+	if(secondary) {
+		*secondary =
+			dsp_context.display_ptr_list[
+				IGD_DC_SECONDARY(dsp_context.current_dc)];
+	}
+	/*EMGD_TRACE_EXIT;*/
+}
+
+/*!
+ *
+ * @param primary_display_plane
+ * @param secondary_display_plane
+ * @param primary_pipe
+ * @param secondary_pipe
+ *
+ * @return void
+ */
+static void dsp_get_planes_pipes(igd_plane_t **primary_display_plane,
+	igd_plane_t **secondary_display_plane,
+	igd_display_pipe_t **primary_pipe,
+	igd_display_pipe_t **secondary_pipe)
+{
+	igd_plane_t          **plane;
+	igd_display_pipe_t   **pipe;
+	int is_primary = 1;
+
+	EMGD_TRACE_ENTER;
+
+	if(!primary_display_plane || !secondary_display_plane ||
+		!primary_pipe || !secondary_pipe) {
+
+		EMGD_ERROR("Invalid parameters");
+	} else {
+
+		plane = dsp_context.dispatch->planes;
+
+		while (*plane) {
+			if ((*plane)->plane_features & IGD_PLANE_DISPLAY) {
+
+				if(is_primary) {
+					*primary_display_plane = *plane;
+					is_primary = 0;
+				} else {
+					*secondary_display_plane = *plane;
+					break;
+				}
+			}
+			plane++;
+		}
+
+		pipe = dsp_context.dispatch->pipes;
+		is_primary = 1;
+
+		while (*pipe) {
+			if(is_primary) {
+				*primary_pipe = *pipe;
+				is_primary = 0;
+			} else {
+				*secondary_pipe = *pipe;
+				break;
+			}
+			pipe++;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+static void dsp_control_plane_format(int enable, int display_plane, igd_plane_t *plane_override)
+{
+	EMGD_TRACE_ENTER;
+
+	dsp_context.dispatch->dsp_control_plane_format(dsp_context.context,
+			enable, display_plane, plane_override);
+
+	dsp_context.context->mod_dispatch.fb_blend_ovl_override = 1;
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ * Check port features of multiple ports to see if they can share a pipe.
+ * Ports that are passed in as NULL are not considered.  There must be
+ * at least two ports.
+ *
+ * Sort the ports so that any port that must be pipe master is listed
+ * first. Return the sorted port order in a format that can be easily
+ * merged into a full DC value.
+ *
+ * @param m
+ * @param p1
+ * @param p2
+ * @param p3
+ *
+ * @return 0 if error
+ * @return DC
+ */
+unsigned long dsp_shareable(igd_display_port_t *m,
+		igd_display_port_t *p1,
+		igd_display_port_t *p2,
+		igd_display_port_t *p3)
+{
+	unsigned long features;
+	unsigned long shareable;
+	unsigned long dc;
+
+	if (m == NULL) {
+		/* Error checking */
+		EMGD_ERROR("dsp_shareable: port pointer is NULL.");
+		return 0;
+	}
+
+	if (p1 == NULL) {
+		dc = m->port_number;
+		return dc;
+	}
+
+	/*
+	 * OR all the port secondary port type bits together.
+	 * If ANDing this with the primary port's feature bits changes
+	 * the value, then all the ports are not shareable.
+	 *
+	 * This does assume that if port A can share with port B,
+	 * then port B can share with port A.
+	 */
+	features = m->port_features & IGD_PORT_SHARE_MASK;
+	shareable = (p1->port_type & IGD_PORT_SHARE_MASK);
+	if (p2 != NULL) {
+		shareable |= (p2->port_type & IGD_PORT_SHARE_MASK);
+		if (p3 != NULL) {
+			shareable |= (p3->port_type & IGD_PORT_SHARE_MASK);
+		}
+	}
+
+	/* make features a true/false type value */
+	features = ((features & shareable) == shareable);
+
+	/*
+	 * This section re-arranges the order of the ports so that the
+	 * port that needs to be the pipe master is always listed first.
+	 *
+	 * FIXME:
+	 *   This doesn't support the case where more than one port must
+	 *   be pipe master.
+	 *
+	 * FIXME:
+	 *   Not sure if this is handling CLOCK_MASTER correctly. CLOCK_MASTER
+	 *   can share a pipe with other ports but wants the PLL to be programmed
+	 *   with its clock.
+	 */
+	if (features) {
+		dc = m->port_number;
+		if (p1->pd_flags & PD_FLAG_PIPE_MASTER) {
+			dc = (dc << 4) | p1->port_number;
+		} else if (p1->pd_flags & PD_FLAG_CLOCK_MASTER) {
+			dc = (dc << 4) | p1->port_number;
+		} else {
+			dc = dc | (p1->port_number << 4);
+		}
+		if (p2 != NULL) {
+			if (p2->pd_flags & PD_FLAG_PIPE_MASTER) {
+				dc = (dc << 4) | p2->port_number;
+			} else if (p2->pd_flags & PD_FLAG_CLOCK_MASTER) {
+				dc = (dc << 4) | p2->port_number;
+			} else {
+				dc = dc | (p2->port_number << 8);
+			}
+		}
+		if (p3 != NULL) {
+			if (p3->pd_flags & PD_FLAG_PIPE_MASTER) {
+				dc = (dc << 4) | p3->port_number;
+			} else if (p3->pd_flags & PD_FLAG_CLOCK_MASTER) {
+				dc = (dc << 4) | p3->port_number;
+			} else {
+				dc = dc | (p3->port_number << 12);
+			}
+		}
+		return dc;
+	} else {
+		return 0;
+	}
+}
+
+/*!
+ * Given a group of ports and some basic infomation like how many pipes
+ * are available, construct a set of valid display configurations. The
+ * number of ports passed in is variable so that if a port doesn't exist
+ * it should be passed in as a NULL.  At least one port must be passed
+ * in. Also, NULL ports should be at the end of the list. Don't pass in
+ * NULL, NULL, NULL, vaid_port because it will break this function.
+ *
+ * The flags value is used to enabled dual pipe support and extended
+ * support.  If neither are set, only TWIN configurations will be returned.
+ *
+ * @param m
+ * @param t1
+ * @param t2
+ * @param t3
+ * @param flags
+ * @param dc_list
+ *
+ * @return void
+ */
+void dsp_build_display_configs(igd_display_port_t *m,
+		igd_display_port_t *t1,
+		igd_display_port_t *t2,
+		igd_display_port_t *t3,
+		unsigned long flags, dsp_dc_list_t *dc_list)
+{
+
+	unsigned long dc, dc2;
+
+	/* If two pipes, do all clone/extended configurations first */
+	if ((flags & 0x0f) > 1) {
+		/* first two ports on different pipes, all remaining ports on pipe 2 */
+		/* can port 0 share with port1 and port 2 */
+		if ((dc = dsp_shareable(t1, t2, t3, NULL)) && DSP_PIPE_OK(m, t1)) {
+			dc = (dc << 20) | 2 | ((m->port_number & 0x0f) << 4);
+			dsp_add_to_dc_list(dc_list, dc, (flags & IGD_PLANE_DIH));
+		}
+		/* first two ports on different pipes, all remaining ports on pipe 1 */
+		if ((dc = dsp_shareable(m, t2, t3, NULL)) && DSP_PIPE_OK(m, t1)) {
+			dc = (dc << 4) | 2 | ((t1->port_number & 0x0f) << 20);
+			dsp_add_to_dc_list(dc_list, dc, (flags & IGD_PLANE_DIH));
+		}
+
+		/* first two ports on different pipes, remianing ports split */
+		if ((dc = dsp_shareable(m, t2, NULL, NULL)) &&
+				(dc2 = dsp_shareable(t1, t3, NULL, NULL)) &&
+				DSP_PIPE_OK(m, t1)) {
+			dc = (dc << 4) | 2 | (dc2 << 20);
+			dsp_add_to_dc_list(dc_list, dc, (flags & IGD_PLANE_DIH));
+		}
+
+		/* first two ports on different pipes, remianing ports split */
+		if ((dc = dsp_shareable(m, t3, NULL, NULL)) &&
+				(dc2 = dsp_shareable(t1, t2, NULL, NULL)) &&
+				DSP_PIPE_OK(m, t1)) {
+			dc = (dc << 4) | 2 | (dc2 << 20);
+			dsp_add_to_dc_list(dc_list, dc, (flags & IGD_PLANE_DIH));
+		}
+	}
+
+	/* Twinned type configurations, all ports on first pipe */
+	if ((dc = dsp_shareable(m, t1, t2, t3))) {
+		dc = (dc << 4) | 4;
+		dsp_add_to_dc_list(dc_list, dc, 0);
+	}
+}
+
+/*!
+ * This routine puts the port table entries as per user (via IAL) mentioned
+ * port order & firmware settings.
+ *
+ * @param context
+ * @param port_order
+ *
+ * @return void
+ */
+void do_port_order(igd_context_t *context, unsigned long *port_order)
+{
+	igd_display_port_t **port_table;
+	igd_display_port_t *p;
+	unsigned long i, j, k, num_ports;
+	unsigned long num_ports1;
+
+	/* Adjust port detect/allocation order with user/caller preference */
+	port_table = dsp_context.dispatch->ports;
+	num_ports = 0;
+	while (port_table[num_ports]) {
+		/* Build a port number to port array */
+		dsp_context.port_list[port_table[num_ports]->port_number] =
+			port_table[num_ports];
+		num_ports++;
+	}
+
+#if 0
+	EMGD_DEBUG("Initial port table = ");
+	for (i = 0; i < num_ports; i++) {
+		EMGD_DEBUG("\t%ld, inuse=%d", port_table[i]->port_number,
+		port_table[i]->inuse);
+	}
+#endif
+
+	num_ports1 = 0;
+	while ((num_ports1 < IGD_MAX_PORTS) && port_order[num_ports1]) {
+		num_ports1++;
+	}
+
+	/*
+	 * If port order is specified, then the ports that are not present in the
+	 * port order list should not be allowed to be allocated. Since it is not
+	 * possible to remove ports from port_table, mark ports that cannot be
+	 * allocated as inuse with 0x80.  Ports marked in-use by the display
+	 * allocation function will set this to 0x01.  This is used in the
+	 * dsp_dc_init() function to detmine what ports are usable.
+	 */
+	if (num_ports1 != 0) {
+		for (i = 0; i < num_ports; i++) {
+			port_table[i]->inuse = 0x80;
+		}
+	} else {
+#ifndef CONFIG_MICRO
+		/* Copying back the port order to the mode_context's port order */
+		for (i = 0; i < num_ports; i++) {
+			port_order[i] = port_table[i]->port_number;
+		}
+#endif
+		return;
+	}
+
+	/* Arrange port_table list to match IAL provided port_order list.
+	 * Dont forget to unmark its in-use flag to ensure it's allowed
+	 * for usage. */
+	k = 0;
+	for (i = 0; i < IGD_MAX_PORTS; i++) {
+		/* Find the port for given port number */
+		for (j = k; j < num_ports; j++) {
+			if (port_table[j]->port_number == port_order[i]) {
+				port_table[j]->inuse = 0;
+				p = port_table[k];
+				port_table[k] = port_table[j];
+				port_table[j] = p;
+				k++;
+				break;
+			}
+		}
+	}
+	return;
+} /* end do_port_order() */
+
+/*!
+ *
+ * @param context
+ * @param config_info
+ *
+ * @return 0
+ */
+static int dsp_get_config_info(igd_context_t *context,
+	igd_config_info_t *config_info)
+{
+	EMGD_ASSERT(context, "Null context", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(config_info, "Null config_info", -IGD_ERROR_INVAL);
+
+	config_info->num_dsp_planes    = dsp_context.num_dsp_planes;
+	config_info->num_dsp_pipes     = dsp_context.num_dsp_pipes;
+	config_info->fb_caps           = dsp_context.dispatch->caps;
+
+	return 0;
+}
+
+
+#if 0
+/*!
+ * Print out the DC list for debug purposes
+ *
+ * @param void
+ *
+ * @return void
+ */
+void debug_print_dc_list( void )
+{
+	int i = 0;
+	unsigned long mask;
+	int x, c, d;
+	int port;
+	char tstr[50];
+	char *pname[6];
+
+	pname[0] = "Unused";
+	pname[1] = "TV";
+	pname[2] = "DVOB";
+	pname[3] = "DVOC";
+	pname[4] = "LVDS";
+	pname[5] = "CRT";
+
+	for (d = 0; d < dsp_context.dsp_dc_list.count; d++) {
+		sprintf(tstr, "%3d: ", i);
+		mask = 0x0000000f;
+		c = 0;
+		for (x = 0; x < 7; x++) {
+			mask = mask << 4;
+			port = (dsp_context.dsp_dc_list.dc_list[d] & mask) >> (4 * (x+1));
+			if ((x == 4) && (port != 0)) {
+				strcat(tstr, " + ");
+				c = 0;
+			}
+			if (port != 0) {
+				if (c) {
+					strcat(tstr, ",");
+				}
+				strcat(tstr, pname[port]);
+				c = 1;
+			}
+		}
+		if ((dsp_context.dsp_dc_list.dc_list[d] & 0x0000000f) == 8) {
+			strcat(tstr, "/e");
+		}
+		EMGD_ERROR("%-20s 0x%08lx", tstr, dsp_context.dsp_dc_list.dc_list[d]);
+		i++;
+	}
+}
+#endif
+
+/*!
+ * Build a list of valid display configurations (DC) and store for later
+ * use by the driver.
+ *
+ * @param context
+ *
+ * @return void
+ */
+void dsp_dc_init(igd_context_t *context)
+{
+	igd_display_port_t *p0 = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL;
+	unsigned long dc_flags;
+	int num_ports;
+	igd_display_port_t **port_table;
+	igd_plane_t **plane;
+	unsigned long display_detect;
+
+	/* Build the display configuration list */
+	dc_flags = dsp_context.num_dsp_pipes & 0x0f;
+
+	/* Check plane features to see if DIH is possible (IGD_PLANE_DIH) */
+	plane = dsp_context.dispatch->planes;
+	if (*plane) {
+		if ((*plane)->plane_features & IGD_PLANE_DIH) {
+			dc_flags |= IGD_PLANE_DIH;
+		}
+	}
+
+	display_detect = (dsp_context.display_flags & IGD_DISPLAY_DETECT)?1:0;
+
+	/*
+	 * Given the number of ports and number of pipes can the max number
+	 * of DC's be calculated?
+	 *
+	 * Number of ports squared.
+	 * If clone double.
+	 * If DIH, double again.
+	 */
+
+	/* Find the number of ports */
+	port_table = dsp_context.dispatch->ports;
+	num_ports = 0;
+	while (port_table[num_ports]) {
+		num_ports++;
+	}
+	EMGD_DEBUG("ports = %d, pipes = %ld", num_ports,
+		dsp_context.num_dsp_pipes);
+
+
+	/* If this is the first time called, then allocate memory for the list */
+	if (dsp_context.dsp_dc_list.dc_list == NULL) {
+		dsp_context.dsp_dc_list.size = num_ports * num_ports;
+		if (dsp_context.num_dsp_pipes > 1) {
+			dsp_context.dsp_dc_list.size *= 2;
+			if (dc_flags & IGD_PLANE_DIH) { /* extended modes */
+				dsp_context.dsp_dc_list.size *= 2;
+			}
+		}
+
+		/* Need to allocate memory for list */
+		dsp_context.dsp_dc_list.dc_list =
+			OS_ALLOC(sizeof(unsigned long) * dsp_context.dsp_dc_list.size);
+	}
+	dsp_context.dsp_dc_list.count = 0;
+
+	if (dsp_context.dsp_dc_list.dc_list == NULL) {
+		EMGD_DEBUG("Memory allocation error, can't allocate DC list");
+		return;
+	}
+
+#if 0
+	/* Get all possible one display configs */
+	while ((p0 = dsp_get_next_port(context, p0)) != NULL) {
+		if (dsp_port_is_ok(context, p0, display_detect)) {
+			dsp_add_to_dc_list(&dsp_context.dsp_dc_list,
+					(1 | ((p0->port_number & 0x0f) << 4)), 0);
+		}
+	}
+
+	/* Get all possible two display configs */
+	p0 = NULL;
+	while ((p0 = dsp_get_next_port(context, p0)) != NULL) {
+		p1 = NULL;
+		if (!dsp_port_is_ok(context, p0, display_detect)) {
+			continue; /* skip ports that are inuse (not to be used) */
+		}
+		while ((p1 = dsp_get_next_port(context, p1)) != NULL) {
+			if (!dsp_port_is_ok(context, p1, display_detect) || (p1 == p0)) {
+				continue; /* can't use the same port twice! */
+			}
+			dsp_build_display_configs(p0, p1, NULL, NULL, dc_flags,
+					&dsp_context.dsp_dc_list);
+		}
+	}
+
+	/* Get all possible three display configs */
+	p0 = NULL;
+	while ((p0 = dsp_get_next_port(context, p0)) != NULL) {
+		if (!dsp_port_is_ok(context, p0, display_detect)) {
+			continue; /* skip ports that are inuse (not to be used) */
+		}
+		p1 = NULL;
+		while ((p1 = dsp_get_next_port(context, p1)) != NULL) {
+			if (!dsp_port_is_ok(context, p1, display_detect) || (p1 == p0)) {
+				continue; /* can't use the same port twice! */
+			}
+			p2 = NULL;
+			while ((p2 = dsp_get_next_port(context, p2)) != NULL) {
+				if (!dsp_port_is_ok(context, p2, display_detect) ||
+						(p2 == p0) || (p2 == p1)) {
+					continue; /* can't use the same port multiple times! */
+				}
+				dsp_build_display_configs(p0, p1, p2, NULL, dc_flags,
+						&dsp_dc_list);
+			}
+		}
+	}
+
+	/* Get all possible four display configs */
+	p0 = NULL;
+	while ((p0 = dsp_get_next_port(context, p0)) != NULL) {
+		if (!dsp_port_is_ok(context, p0, display_detect)) {
+			continue; /* skip ports that are inuse (not to be used) */
+		}
+		p1 = NULL;
+		while ((p1 = dsp_get_next_port(context, p1)) != NULL) {
+			if (!dsp_port_is_ok(context, p1, display_detect) || (p1 == p0)) {
+				continue; /* can't use the same port twice! */
+			}
+			p2 = NULL;
+			while ((p2 = dsp_get_next_port(context, p2)) != NULL) {
+				if (!dsp_port_is_ok(context, p2, display_detect) ||
+						(p2 == p0) || (p2 == p1)) {
+					continue; /* can't use the same port multiple times! */
+				}
+				while ((p3 = dsp_get_next_port(context, p3)) != NULL) {
+					if (!dsp_port_is_ok(context, p3, display_detect) ||
+							(p3 == p0) || (p3 == p2) || (p3 == p1)) {
+						continue; /* can't use the same port multiple times! */
+					}
+					dsp_build_display_configs(p0, p1, p2, p3, dc_flags,
+							&dsp_dc_list);
+				}
+			}
+		}
+	}
+#else
+
+	/*
+	 * This code is much smaller than above but doesn't sort the list
+	 * in the same way. It saves over 300 bytes.
+	 */
+	p0 = NULL;
+	while ((p0 = dsp_get_next_port(context, p0, 0)) != NULL) {
+		if (dsp_port_is_ok(context, p0, display_detect)) {
+			dsp_add_to_dc_list(&dsp_context.dsp_dc_list,
+					(1 | ((p0->port_number & 0x0f) << 4)), 0);
+			p1 = NULL;
+			while ((p1 = dsp_get_next_port(context, p1, 0)) != NULL) {
+				if ((p1 != p0) && dsp_port_is_ok(context, p1, display_detect)) {
+					dsp_build_display_configs(p0, p1, NULL, NULL, dc_flags,
+							&dsp_context.dsp_dc_list);
+					p2 = NULL;
+					while ((p2 = dsp_get_next_port(context, p2, 0)) != NULL) {
+						if ((p2 != p0) && (p2 != p1) &&
+								dsp_port_is_ok(context, p2, display_detect)) {
+							dsp_build_display_configs(p0, p1, p2, NULL,
+									dc_flags, &dsp_context.dsp_dc_list);
+							while ((p3 = dsp_get_next_port(context, p3, 0)) !=
+									NULL) {
+								if ((p3 != p0) && (p3 != p2) && (p3 != p1) &&
+										dsp_port_is_ok(context, p3, display_detect)) {
+									dsp_build_display_configs(p0, p1, p2, p3,
+											dc_flags, &dsp_context.dsp_dc_list);
+								}
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+#endif
+
+	/* zero terminate the list */
+	dsp_add_to_dc_list(&dsp_context.dsp_dc_list, 0L, 0L);
+
+	/*If quickboot, get the dc that was programmed by the firware
+	 * i.e. Video BIOS or EFI Video Driver
+	 */
+
+#ifndef CONFIG_MICRO  /* We don't need this in firmware code */
+	dsp_context.fw_dc = dsp_get_fw_dc(context);
+	dsp_context.context->mod_dispatch.dsp_fw_dc = dsp_context.fw_dc;
+
+#endif
+
+	/*OPT_DEBUG_VOID_CALL(debug_print_dc_list());*/
+
+} /* end dsp_dc_init() */
+
+
+/*!
+ * Return the list of valid display configurations. The list returned is
+ * currently the "live" list. The IAL should not modify it in any way.
+ *
+ * For the driver, this will re-initialize the DC list every time it's
+ * called so that the list will reflect displays that have been attached
+ * or detached since the last call.  If the list is empty (i.e. no
+ * displays attached and/or no port drivers loaded), then fall back to
+ * the following default behavior:
+ *   1. Make sure analog port is marked as available
+ *   2. Turn off the display detect option.
+ *   3. Re-initialize the DC list.
+ * This guarentees that we'll always have at least one valid display (CRT)
+ * on the list.
+ *
+ * @param driver_handle
+ *
+ * @param request - The requested display mode. The first DC in the list
+ *    with a matching display mode is returned when the IGD_QUERY_DC_INIT
+ *    flag is set.
+ *
+ * @param dc_list - The returned DC list. This will point to the live list
+ *    and as such, should not be freed or altered.
+ *
+ * @param flags - Determines what portion of the list is returned.
+ *
+ * @return 0: success.
+ * @return -IGD_ERROR_INVAL: No valid DC list can be returned.
+ */
+int igd_query_dc(igd_driver_h driver_handle,
+	unsigned long request,
+	unsigned long **dc_list,
+	unsigned long flags)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	int d;
+
+	EMGD_DEBUG ("Enter igd_query_dc");
+
+#ifndef CONIFG_MICRO
+	dsp_dc_init(context);
+	/*
+	 * If the list is empty at this point, add single/analog so that
+	 * the driver can run.  Under normal conditions, this shouldn't
+	 * happen.  In this case, turn off display detect too?
+	 */
+	if (dsp_context.dsp_dc_list.count == 1) {
+
+		EMGD_ERROR("No displays detected or available:");
+
+		/* If display detect is on, turn it off and try again */
+		if (dsp_context.display_flags & IGD_DISPLAY_DETECT) {
+
+			dsp_context.display_flags &= ~IGD_DISPLAY_DETECT;
+
+			/* Turn off display detect */
+			EMGD_ERROR("  Disabling display detect.");
+			dsp_dc_init(context);
+		}
+
+#if 0
+		/* Now, analog port driver also loads dynamically, so there is
+		 * no fallback case to analog */
+		if (dsp_context.dsp_dc_list.count == 1) {
+			igd_display_port_t *port = NULL;
+			void *handle = NULL;
+
+			EMGD_ERROR("  Defaulting to CRT only.");
+			dsp_get_display(5, NULL, &port, 0);
+			if (port == NULL) {
+				EMGD_ERROR("No analog port available, this is bad!");
+			} else {
+				if (port->inuse == 0x80) {
+					/* port was never initialized, so better do that! */
+					ANALOG_INIT(handle);
+				}
+				port->inuse = 0; /* Mark as available */
+			}
+
+			/* Resetting DC list */
+			dsp_context.dsp_dc_list.count = 0;
+			dsp_add_to_dc_list(&dsp_context.dsp_dc_list, 0x00000051L, 0L);
+			dsp_add_to_dc_list(&dsp_context.dsp_dc_list, 0L, 0L);
+		}
+#endif
+	}
+#endif
+
+	if (flags == IGD_QUERY_DC_ALL) {
+		*dc_list = dsp_context.dsp_dc_list.dc_list;
+		return 0;
+	} else if (flags == IGD_QUERY_DC_PREFERRED) {
+		/*
+		 * FIXME:
+		 *  This case is not yet implemented. No IAL currently uses this
+		 *  capability.
+		 *
+		 *  Somehow this needs to build a new smaller list and return
+		 *  that.  The caller would then be responsibile for freeing the
+		 *  memory?
+		 *
+		 *  For now, this returns the full list so that if an IAL tries
+		 *  to use it, it doesn't break.
+		 */
+		*dc_list = dsp_context.dsp_dc_list.dc_list;
+		return 0;
+	} else if (flags == IGD_QUERY_DC_INIT) {
+		/* what is the current display config? */
+		for(d = 0; d < dsp_context.dsp_dc_list.count; d++) {
+			if (dsp_context.dsp_dc_list.dc_list[d] == request) {
+				*dc_list = &dsp_context.dsp_dc_list.dc_list[d];
+				return 0;
+			}
+		}
+
+		/* Find first match of same type */
+		for(d = 0; d < dsp_context.dsp_dc_list.count; d++) {
+			if (dsp_context.dsp_dc_list.dc_list[d] & (request & 0x0f)) {
+				*dc_list = &dsp_context.dsp_dc_list.dc_list[d];
+				return 0;
+			}
+		}
+	} else if (flags == IGD_QUERY_DC_EXISTING) {
+		*dc_list = &dsp_context.fw_dc;
+		return 0;
+	}
+	EMGD_ERROR ("igd_query_dc found no match for requested dc = %ld", request);
+	*dc_list = NULL;
+	return -IGD_ERROR_INVAL;
+}
+
+/*!
+ * Mark a plane as available. If there are surfaces allocated to the
+ * plane, free them.
+ *
+ * @param context
+ * @param plane
+ *
+ * @return void
+ */
+void free_plane(igd_context_t *context, igd_plane_t *plane)
+{
+	igd_cursor_info_t *ci;
+	igd_framebuffer_info_t *fb;
+
+	EMGD_DEBUG("free_plane Entry");
+
+	if(!plane) {
+		EMGD_ERROR("Attempt to free NULL plane");
+		return;
+	}
+
+	/* If a framebuffer was allocated for this plane, free it */
+	if (!plane->mirror && (plane->plane_info != NULL)) {
+		if ((IGD_PLANE_CURSOR & plane->plane_features) == IGD_PLANE_CURSOR){
+			ci = (igd_cursor_info_t *)plane->plane_info;
+			EMGD_DEBUG("Freeing cursor image @ 0x%08lx", ci->xor_offset);
+			context->dispatch.gmm_free(ci->xor_offset);
+			EMGD_DEBUG("Freeing cursor image @ 0x%08lx", ci->argb_offset);
+			context->dispatch.gmm_free(ci->argb_offset);
+			/* Free plane info */
+			OS_FREE(plane->plane_info);
+			plane->plane_info = NULL;
+		} else {
+			fb = (igd_framebuffer_info_t *)plane->plane_info;
+			EMGD_DEBUG("Freeing framebuffer @ 0x%08lx", fb->fb_base_offset);
+			if(fb->allocated) {
+				context->dispatch.gmm_free(fb->fb_base_offset);
+			}
+			/* Must retain the offset because it is our reservation */
+			fb->allocated = 0;
+			/* The FB Info is static, don't free it */
+		}
+	} else {
+		/* If this is mirrored (for clone displays), break the mirror */
+		plane->mirror->mirror = NULL;
+		plane->mirror = NULL;
+		/* Note: The following fixes a kernel Oops when the DRM module
+		 * initializes the display when it's loaded, and then the X driver uses
+		 * a different port order in its DC (and a dsp_shutdown() frees this,
+		 * followed by an dsp_alloc() which needs to allocate a new cursor).
+		 * That's because the primary and secondary ports/planes switch, and
+		 * plane->plane_info in this context becomes cursor->cursor_info in
+		 * dsp_alloc(), and it must be NULL so that a new cursor is allocated
+		 * (otherwise the old memory location is re-used, even though the
+		 * memory was freed and reallocated for another purpose):
+		 */
+		if ((IGD_PLANE_CURSOR & plane->plane_features) == IGD_PLANE_CURSOR){
+			plane->plane_info = NULL;
+		}
+	}
+
+	EMGD_DEBUG("De-allocating plane 0x%lx", plane->plane_reg);
+	plane->inuse = 0;
+} /* end free_plane() */
+
+/*!
+ * Take a DC and configure the primary and secondary display handles.
+ * The two display handles are returned to the caller.
+ *
+ * It should be valid to use the 0/1 indexes for pipes & planes here
+ * because we should never get a DC that isn't valid. So if the hardware
+ * can't support extended, and extended DC will never show up.
+ *
+ * @param context
+ * @param dc
+ * @param flags
+ *
+ * @return 0: success.
+ * @return -IGD_INVAL: No valid DC list can be returned.
+ */
+int dsp_alloc(igd_context_t *context,
+	unsigned long dc,
+	unsigned long flags)
+{
+	igd_plane_t *plane = NULL, *plane2 = NULL, *temp = NULL;
+	igd_plane_t **plane_tbl;
+	igd_cursor_t *cursor = NULL, *cursor2 = NULL;
+	igd_cursor_t *cursora = NULL, *cursorb = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	igd_display_port_t *port = NULL;
+	unsigned short port_number;
+	int i, swap_plane = 0;
+	int secondary_pipe = 1;
+
+
+	EMGD_TRACE_ENTER;
+
+#ifndef CONFIG_MICRO
+	/*
+	 * Surfaces in the surface cache have a display handle associated with
+	 * them. To be safe we need to flush everyone out of the cache before
+	 * freeing a handle.
+	 */
+	if(context->dispatch.gmm_flush_cache) {
+		if(dsp_context.display_list[0].allocated ||
+			dsp_context.display_list[1].allocated) {
+			context->dispatch.gmm_flush_cache();
+		}
+	}
+#endif
+
+	/* Clear all the port assignments */
+	for (i = 0; i < IGD_MAX_PORTS; i++) {
+		dsp_context.display_list[0].port[i] = NULL;
+		dsp_context.display_list[1].port[i] = NULL;
+	}
+
+	/* Clear display ptr assignments */
+	OS_MEMSET(dsp_context.display_ptr_list, 0,
+		sizeof(dsp_context.display_ptr_list));
+
+	/* Free any allocated pipes */
+	if (dsp_context.display_list[0].pipe) {
+		PIPE(&dsp_context.display_list[0])->inuse = 0;
+	}
+	if (dsp_context.display_list[1].pipe) {
+		PIPE(&dsp_context.display_list[1])->inuse = 0;
+	}
+
+	/* Set up planes... */
+	plane_tbl = dsp_context.dispatch->planes;
+	while(*plane_tbl) {
+		(*plane_tbl)->inuse = 0;
+
+		if ((IGD_PLANE_CURSOR & (*plane_tbl)->plane_features) ==
+				IGD_PLANE_CURSOR) {
+			if ((cursora == NULL) &&
+				(IGD_CURSOR_USE_PIPEA & (*plane_tbl)->plane_features)) {
+				cursora = (igd_cursor_t *)(*plane_tbl);
+				cursor = cursora;
+			} else if (cursorb == NULL) {
+				cursorb = (igd_cursor_t *)(*plane_tbl);
+				cursor2 = cursorb;
+			}
+		} else {
+			if (plane == NULL) {
+				plane = *plane_tbl;
+				/* plane->mirror = NULL; */
+			} else if (plane2 == NULL) {
+				plane2 = *plane_tbl;
+				/* plane2->mirror = NULL; */
+			}
+		}
+		plane_tbl++;
+	}
+
+
+	/*
+	 * Get the first port from the DC and hook it up as master.
+	 * FIXME:
+	 *   If this fails becaue the display isn't attached, and there
+	 *   are more choices, this should move on and try the other
+	 *   choices, maybe?
+	 */
+	OS_MEMSET(&dsp_context.display_list[0], 0, sizeof(igd_display_context_t));
+
+	port_number = (unsigned short)((dc & 0x000000f0) >> 4);
+	dsp_get_display(port_number, NULL, &port, 0);
+
+	if (port) {
+		/*
+		 * Allocate a pipe for this display. Depending on the port
+		 * requirements this could be either pipe.
+		 */
+		if (port->port_features & IGD_PORT_USE_PIPEA) {
+			pipe = dsp_context.dispatch->pipes[0];
+			cursor = cursora;
+			cursor2 = cursorb;
+			secondary_pipe = 1;
+			if(plane->plane_features & IGD_PLANE_USE_PIPEB){
+				swap_plane = 1;
+			}
+		} else if (port->port_features & IGD_PORT_USE_PIPEB) {
+			pipe = dsp_context.dispatch->pipes[1];
+			cursor2 = cursora;
+			cursor = cursorb;
+			secondary_pipe = 0;
+			if(plane->plane_features & IGD_PLANE_USE_PIPEA){
+				swap_plane = 1;
+			}
+		} else {
+			EMGD_ERROR("Error, master port can't use either pipeA or pipeB!");
+			return -IGD_INVAL;
+		}
+
+		if(swap_plane){
+			temp = plane;
+			plane = plane2;
+			plane2 = temp;
+		}
+		pipe->inuse = 0;
+
+
+		/* set up the display handle */
+		dsp_context.display_list[0].plane = (void *)plane;
+		dsp_context.display_list[0].cursor = (void *)cursor;
+		dsp_context.display_list[0].pipe = (void *)pipe;
+		dsp_context.display_list[0].port[port_number-1] = (void *)port;
+		dsp_context.display_list[0].context = context;
+		dsp_context.display_list[0].port_number = port_number;
+		dsp_context.display_list[0].allocated = 1;
+		dsp_context.display_ptr_list[port_number]= &dsp_context.display_list[0];
+		pipe->inuse = 1;
+
+#ifndef CONFIG_MICRO
+		if (cursor) {
+			pipe->cursor = cursor;
+			cursor->inuse = 1;
+			if (cursor->cursor_info == NULL) {
+				cursor->cursor_info = OS_ALLOC(sizeof(igd_cursor_info_t));
+				if(!cursor->cursor_info) {
+					EMGD_ERROR("Error, memory allocation for cursor_info "
+							"failed.");
+					pipe->cursor = NULL;
+					cursor->inuse = 0;
+				} else {
+					if (dsp_init_cursor(context, cursor) != 0) {
+						pipe->cursor = NULL;
+						cursor->inuse = 0;
+					}
+				}
+			}
+		}
+#endif
+
+	} /* else {
+		EMGD_ERROR("Failed to set up primary: port = %p, pipe = %p, plane = %p",
+				port, pipe, plane);
+	}
+	*/
+
+
+	/* Set up secondary. How it is set up depends on the display config */
+	switch (dc & 0x0000000f) {
+	case IGD_DISPLAY_CONFIG_SINGLE:
+	case IGD_DISPLAY_CONFIG_TWIN:
+	case 0:
+		/*
+		 * If display 1 was previouslly allocated, then we were in either
+		 * clone or extended. In either case, we just want to make sure
+		 * the display is no longer allocated and any clone mirror links
+		 * are removed.
+		 *
+		 * Note: This doesn't reduce any reference counts or free any
+		 * resources allocated to display 1.
+		 */
+		if (dsp_context.display_list[1].allocated) {
+			dsp_context.display_list[1].allocated = 0;
+			dsp_context.display_list[1].plane = NULL;
+			dsp_context.display_list[1].cursor = NULL;
+			dsp_context.display_list[1].pipe = NULL;
+
+			/* If the cursor and/or fb were cloned, break the clone */
+			if (cursor->mirror != NULL) {
+				cursor->mirror = NULL;
+				cursor2->mirror = NULL;
+				cursor2->cursor_info = NULL;
+			}
+
+			/* break plane mirror??? */
+			if (plane->mirror != NULL) {
+				plane->mirror = NULL;
+				plane2->mirror = NULL;
+			}
+		}
+		break;
+	case IGD_DISPLAY_CONFIG_CLONE:
+		/*
+		 * When switching to clone we need to free any resources allocated
+		 * to the second display.  This can happen if we were ever in
+		 * extended mode prior to this.
+		 *
+		 * NOTE:
+		 *   This then needs to fall through to the extended branch because
+		 *   the rest of the set up is the same. DO NOT ADD A BREAK STATEMENT!
+		 */
+
+		/*
+		 * If display_list[1].plane->plane_info exist and the plane_info
+		 * reference count is 1, then this was allocated as an independent
+		 * plane.  The surface must be freed and the plane_info record reset.
+		 *
+		 * The same logic applies to the secondary cursor info.  Does this
+		 * imply that the display_list needs to have a cursor pointer too?
+		 * What about moving the cursor from the pipe to the display?
+		 */
+		if (dsp_context.display_list[1].plane && !plane2->mirror) {
+			free_plane(context, dsp_context.display_list[1].plane);
+		}
+		if (dsp_context.display_list[1].cursor && !cursor2->mirror) {
+			free_plane(context, (igd_plane_t *)cursor2);
+		}
+
+		/* Mirror the cursor and framebuffer to the second display */
+		plane->mirror = plane2;
+		plane2->mirror = plane;
+		/*
+		 * Note: plane_info points to a static data structure so we have
+		 * to get the original pointer back when going into extended.
+		 */
+		plane2->plane_info = plane->plane_info;
+
+		/*
+		 * WinCE VEXT uses clone mode, but needs indpendent cursors. Thus,
+		 * we don't want to do this mirroring in this one specific case.
+		 */
+		cursor->mirror = cursor2;
+		cursor2->mirror = cursor;
+		cursor2->cursor_info = cursor->cursor_info;
+
+	case IGD_DISPLAY_CONFIG_EXTENDED:
+
+		if (plane2) {
+			/* Need to check here because the last case, CLONE, falls
+			 * through to this one */
+			if (!(dc & IGD_DISPLAY_CONFIG_CLONE)) {
+				/* If this is really extended and not clone, break mirrors */
+				plane->mirror = NULL;
+				plane2->mirror = NULL;
+				if (plane2->plane_info == plane->plane_info) {
+					/*
+					 * Get back the original static pointer by guessing
+					 * and switching it if it is the same as plane 1.
+					 */
+					plane2->plane_info = &fb_info_cmn[1];
+					if (plane2->plane_info == plane->plane_info) {
+						plane2->plane_info = &fb_info_cmn[0];
+					}
+				}
+				cursor->mirror = NULL;
+				cursor2->mirror = NULL;
+				if (cursor2->cursor_info == cursor->cursor_info) {
+					cursor2->cursor_info = NULL;
+				}
+			}
+
+			port_number = IGD_DC_SECONDARY(dc);
+			dsp_context.display_list[1].plane = (void *)plane2;
+			dsp_context.display_list[1].context = context;
+			dsp_context.display_list[1].cursor = (void *)cursor2;
+
+			/* Allocate which ever pipe wasn't used above */
+			pipe = dsp_context.dispatch->pipes[secondary_pipe];
+
+			if (pipe) {
+				dsp_context.display_list[1].allocated = 1;
+				pipe->inuse = 1;
+				dsp_context.display_list[1].pipe = (void *)pipe;
+
+				dsp_get_display(port_number, NULL, &port, 0);
+				if (port) {
+					dsp_context.display_list[1].port[port_number-1] =
+						(void *)port;
+					dsp_context.display_list[1].port_number = port_number;
+					dsp_context.display_ptr_list[port_number] =
+						&dsp_context.display_list[1];
+				} else {
+					EMGD_ERROR("Failed to get port %d", port_number);
+				}
+
+#ifndef CONFIG_MICRO
+				if (cursor2) {
+					pipe->cursor = cursor2;
+					cursor2->inuse = 1;
+					if (cursor2->cursor_info == NULL) {
+						cursor2->cursor_info =
+							OS_ALLOC(sizeof(igd_cursor_info_t));
+						if(!cursor2->cursor_info) {
+							EMGD_ERROR("Error, memory allocation for cursor_info "
+									"failed.");
+							pipe->cursor = NULL;
+							cursor2->inuse = 0;
+						} else {
+							if (dsp_init_cursor(context, cursor2) != 0) {
+								pipe->cursor = NULL;
+								cursor2->inuse = 0;
+							}
+						}
+					}
+				}
+#endif
+			} else {
+				EMGD_ERROR("Failed to get pipe #1");
+			}
+		} else {
+			EMGD_ERROR("Plane #1 not available");
+		}
+		break;
+	default:
+		EMGD_DEBUG("DC has an invalid display config!  0x%08lx", dc);
+		break;
+	}
+
+	/* Assign all twin'd ports to the proper displays */
+	for (i = 7; i > 1; i--) {
+		port_number = DC_PORT_NUMBER(dc, i);
+		if ((port_number) && (i != 5)) { /* Skip Owner ports */
+			dsp_get_display(port_number, NULL, &port, 0);
+			if (port) {
+				if (i > 5) {
+					dsp_context.display_list[1].port[port_number-1] =
+						(void *)port;
+					dsp_context.display_ptr_list[port_number] =
+						&(dsp_context.display_list[1]);
+				} else {
+					dsp_context.display_list[0].port[port_number-1] =
+						(void *)port;
+					dsp_context.display_ptr_list[port_number] =
+						&(dsp_context.display_list[0]);
+				}
+			}
+		}
+	}
+
+	dsp_context.current_dc = dc;
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Does the initialization of dsp module including initializing
+ * unset values in the plane, pipe and port tables.
+ *
+ * @param context
+ *
+ * @return 0: success.
+ * @return -IGD_ERROR_NODEV
+ */
+int dsp_init(igd_context_t *context)
+{
+	igd_display_params_t *display_params;
+	igd_plane_t          **plane;
+	igd_display_pipe_t   **pipe;
+	unsigned long        i, num_gpio, *gpio_reg;
+	unsigned long        hal_attr_index, init_attr_index;
+	igd_param_t   *params = context->mod_dispatch.init_params;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	OS_MEMSET(&dsp_context, 0, sizeof(dsp_context));
+
+	/* Hook up plane, pipe, port, cursor lists here */
+	dsp_context.dispatch = (dsp_dispatch_t *)dispatch_acquire(context,
+		dsp_dispatch_list);
+	if(!dsp_context.dispatch) {
+		return -IGD_ERROR_NODEV;
+	}
+
+	dsp_context.context = context;
+
+	if(dsp_context.dispatch->dsp_init) {
+		ret = dsp_context.dispatch->dsp_init(context);
+		if (ret) {
+			return ret;
+		}
+	}
+
+	/* Hook up top-level dispatch functions */
+	context->dispatch.query_dc = igd_query_dc;
+
+	/* Hook up inter-module dispatch functions */
+	context->mod_dispatch.dsp_get_config_info = dsp_get_config_info;
+	context->mod_dispatch.dsp_get_next_plane = dsp_get_next_plane;
+	context->mod_dispatch.dsp_get_next_pipe = dsp_get_next_pipe;
+	context->mod_dispatch.dsp_get_next_port = dsp_get_next_port;
+	context->mod_dispatch.dsp_get_dc = dsp_get_dc;
+	context->mod_dispatch.dsp_get_display = dsp_get_display;
+	context->mod_dispatch.dsp_get_planes_pipes = dsp_get_planes_pipes;
+	context->mod_dispatch.dsp_alloc = dsp_alloc;
+	context->mod_dispatch.dsp_control_plane_format = dsp_control_plane_format;
+
+	/* Dsp data members in inter module dispatch. This is done to make
+	 * these data members available for vBIOS after init when dsp is
+	 * discarded. */
+	context->mod_dispatch.dsp_current_dc = &dsp_context.current_dc;
+	context->mod_dispatch.dsp_port_list = dsp_context.port_list;
+	context->mod_dispatch.dsp_display_list = dsp_context.display_ptr_list;
+
+	/* Call the full init if we are not micro */
+	OPT_MICRO_CALL(dsp_full_init(context));
+
+	dsp_context.display_flags = params->display_flags;
+
+	/* Fill the number of planes and number of pipes in mode_context */
+	plane = dsp_context.dispatch->planes;
+	pipe  = dsp_context.dispatch->pipes;
+
+	i=0;
+	while (*plane) {
+		if ((*plane)->plane_features & IGD_PLANE_DISPLAY) {
+			/*
+			 * Initialize the offsets of the frame buffer to zero.
+			 */
+			(*(igd_display_plane_t **)plane)->fb_info->fb_base_offset = 0;
+			(*(igd_display_plane_t **)plane)->fb_info->visible_offset = 0;
+			dsp_context.num_dsp_planes++;
+		}
+		plane++;
+	}
+
+	while (*pipe) {
+		dsp_context.num_dsp_pipes++;
+		pipe++;
+	}
+
+	dsp_context.dsp_dc_list.dc_list = NULL;
+
+	/* Initialize port table with user specified display parameters. */
+	do_port_order(context, params->port_order);
+
+	/* Now set the other parameters in port table */
+	num_gpio = context->mod_dispatch.mode_get_gpio_sets(&gpio_reg);
+
+	for (i = 1, display_params = params->display_params; i <= 5; i++,
+			 display_params++) {
+		unsigned long temp;
+		igd_display_port_t *port = NULL;
+
+		dsp_get_display((unsigned short)display_params->port_number,
+			NULL, &port, 0);
+		if (!port) {
+			/* If the port number is unknown/undefined,
+			 * then skip this parameter */
+			continue;
+		}
+
+		/* process present_params flags */
+		if (IGD_PARAM_DDC_GPIO & display_params->present_params) {
+			temp = display_params->ddc_gpio;
+			if (temp >= num_gpio) {
+				EMGD_DEBUG("Invalid GPIO pin pair %ld specified for DDC.",
+						temp);
+			} else {
+				port->ddc_reg = gpio_reg[temp];
+			}
+		}
+		if (IGD_PARAM_DDC_SPEED & display_params->present_params) {
+			temp = display_params->ddc_speed;
+			if (temp > 400 || temp < 10) {
+				EMGD_DEBUG("DDC speed %ld is outside [10-400KHz] range.",
+						temp);
+			} else {
+				port->ddc_speed = temp;
+			}
+		}
+		if (IGD_PARAM_DDC_DAB & display_params->present_params) {
+			port->ddc_dab = display_params->ddc_dab;
+		}
+		if (IGD_PARAM_I2C_GPIO & display_params->present_params) {
+			temp = display_params->i2c_gpio;
+			if (temp >= num_gpio) {
+				EMGD_DEBUG("Invalid GPIO pin pair %ld specified for I2C.",
+						temp);
+			} else {
+				port->i2c_reg = gpio_reg[temp];
+			}
+		}
+		if (IGD_PARAM_I2C_SPEED & display_params->present_params) {
+			temp = display_params->i2c_speed;
+			if (temp > 400 || temp < 10) {
+				EMGD_DEBUG("I2C speed %ld is outside [10-400KHz] range.",
+						temp);
+			} else {
+				port->i2c_speed = temp;
+			}
+		}
+		if (IGD_PARAM_DAB & display_params->present_params) {
+			port->dab = display_params->i2c_dab;
+		}
+		if (IGD_PARAM_FP_INFO & display_params->present_params) {
+			port->fp_info = (igd_param_fp_info_t *)
+				OS_ALLOC(sizeof(igd_param_fp_info_t));
+			if (NULL != port->fp_info) {
+				OS_MEMCPY(port->fp_info, &display_params->fp_info,
+					sizeof(igd_param_fp_info_t));
+				EMGD_DEBUG("IGD_PARAM_FP_INFO: FP width %ld height %ld",
+					port->fp_info->fp_width, port->fp_info->fp_height);
+			} else {
+				EMGD_DEBUG("IGD_PARAM_FP_INFO: allocation of igd_param_fp_info_t "
+						"struct failed");
+			}
+		}
+		if (IGD_PARAM_DTD_LIST & display_params->present_params) {
+			port->dtd_list = OS_ALLOC(sizeof(igd_param_dtd_list_t));
+			if (NULL != port->dtd_list) {
+				OS_MEMCPY(port->dtd_list, &display_params->dtd_list,
+						sizeof(*port->dtd_list));
+			} else {
+				EMGD_DEBUG("IGD_PARAM_DTD_LIST: allocation of "
+						"igd_param_dtd_list_t struct failed");
+			}
+		}
+		if (IGD_PARAM_ATTR_LIST & display_params->present_params) {
+			port->attr_list = OS_ALLOC(sizeof(igd_param_attr_list_t));
+			if (NULL != port->attr_list) {
+				OS_MEMCPY(port->attr_list, &display_params->attr_list,
+						sizeof(*port->attr_list));
+				/* Now allocate memory for attributes */
+				port->attr_list->attr = OS_ALLOC(sizeof(igd_param_attr_t) *
+								port->attr_list->num_attrs);
+				if (NULL != port->attr_list->attr) {
+					OS_MEMCPY(port->attr_list->attr,
+						display_params->attr_list.attr,
+						sizeof(igd_param_attr_t) * port->attr_list->num_attrs);
+				}
+			}
+
+			/* Initialize HAL's attributes */
+			for( init_attr_index = 0;
+				 init_attr_index < port->attr_list->num_attrs;
+				 init_attr_index++ ) {
+
+				hal_attr_index = 0;
+
+				while (PD_ATTR_LIST_END != port->attributes[hal_attr_index].id) {
+					if (port->attributes[hal_attr_index].id ==
+						port->attr_list->attr[init_attr_index].id) {
+							port->attributes[hal_attr_index].current_value =
+								port->attr_list->attr[init_attr_index].value;
+					}
+
+					hal_attr_index++;
+				}
+			} /* for: initialize HAL's attributes */
+
+		}
+	}
+
+	/*
+	 * Build the list of valid display configurations.
+	 */
+	/* dsp_dc_init(context); */
+	EMGD_TRACE_EXIT;
+
+	return 0;
+} /* end dsp_init() */
+
+#ifndef CONFIG_MICRO
+
+/*!
+ * This function returns the list of available pixel formats for the
+ * framebuffer and cursor if the pointers are not NULL.
+ *
+ * @param display_handle A igd_display_h type returned from a previous
+ * igd_alloc_display call.
+ * @param fb_list_pfs Returns the list of pixel formats for framebuffer.
+ * @param cu_list_pfs Returns the list of pixel formats for the cursor.
+ * Both of the above lists ends with 0. Dframebuffer and cursor if the
+ * pointers are not NULL.
+ * @param overlay_pfs
+ * @param render_pfs
+ * @param texture_pfs
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int igd_get_pixelformats(igd_display_h display_handle,
+	unsigned long **fb_list_pfs, unsigned long **cu_list_pfs,
+	unsigned long **overlay_pfs, unsigned long **render_pfs,
+	unsigned long **texture_pfs)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+
+
+	if (!display || !PLANE(display) || !PIPE(display)) {
+		return -IGD_INVAL;
+	}
+
+	if (fb_list_pfs) {
+		*fb_list_pfs = PLANE(display)->pixel_formats;
+	}
+	if (cu_list_pfs) {
+		*cu_list_pfs = PIPE(display)->cursor->pixel_formats;
+	}
+	if (overlay_pfs) {
+		*overlay_pfs = dsp_context.dispatch->overlay_pfs;
+	}
+	if (render_pfs) {
+		*render_pfs = dsp_context.dispatch->render_pfs;
+	}
+	if (texture_pfs) {
+		*texture_pfs = dsp_context.dispatch->texture_pfs;
+	}
+
+	return 0;
+
+} /* end igd_get_pixelformats() */
+
+/*!
+ * Given a newly allocated cursor_info record, allocate the
+ * surfaces needed for both the xor and argb cursor images. Also
+ * fill in the cursor_info as much as possible.
+ *
+ * @param context
+ * @param cursor
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_NOMEM on failure
+ */
+static int dsp_init_cursor(igd_context_t *context, igd_cursor_t *cursor)
+{
+	unsigned long buffer;
+	unsigned long buffer_phys = 0;
+	void* cursor_mem;
+	unsigned int width = 64;
+	unsigned int height = 64;
+	unsigned int pitch = 0;
+	unsigned long size = 0;
+	unsigned long flags = IGD_SURFACE_CURSOR;
+	int ret;
+	int has_rgb32 = 0;
+	unsigned long *tmp;
+
+	tmp = (unsigned long *)cursor->pixel_formats;
+	while (*tmp) {
+		if (*tmp == IGD_PF_ARGB32) {
+			has_rgb32 = 1;
+			break;
+		}
+		tmp++;
+	}
+
+	OS_MEMSET(cursor->cursor_info, 0, sizeof(igd_cursor_info_t));
+
+	if (has_rgb32) {
+		/* ARGB32 is used for any 32bit format. */
+		GMM_SET_DEBUG_NAME("ARGB Cursor");
+		ret = context->dispatch.gmm_alloc_surface(&buffer,
+				IGD_PF_ARGB32,
+				&width, &height, &pitch, &size,
+				IGD_GMM_ALLOC_TYPE_NORMAL, &flags);
+		if (ret) {
+			EMGD_ERROR("ERROR: No memory for ARGB cursor!");
+			return -IGD_ERROR_NOMEM;
+		}
+
+		/* Get the register update physical address in RAM */
+		if (context->dispatch.gmm_virt_to_phys(buffer, &buffer_phys)) {
+printk(KERN_ALERT "[EGD]     dsp_init_cursor(): Virtual to Physical Address "
+"translation failed\n");
+			EMGD_ERROR_EXIT("Virtual to Physical Address translation failed");
+			return -IGD_ERROR_NOMEM;
+		}
+
+		cursor_mem = phys_to_virt(buffer_phys);
+		if(cursor_mem){
+			OS_MEMSET(cursor_mem, 0, size);
+		}
+		EMGD_DEBUG("Allocating cursor surface @ 0x%08lx", buffer);
+		cursor->cursor_info->argb_offset = buffer;
+		cursor->cursor_info->argb_pitch = pitch;
+	}
+	flags = IGD_SURFACE_CURSOR;
+
+	GMM_SET_DEBUG_NAME("XOR Cursor");
+	ret = context->dispatch.gmm_alloc_surface(&buffer,
+			IGD_PF_RGB_2,
+			&width, &height, &pitch, &size,
+			IGD_GMM_ALLOC_TYPE_NORMAL, &flags);
+	if (ret) {
+		if(has_rgb32) {
+			context->dispatch.gmm_free(cursor->cursor_info->argb_offset);
+			cursor->cursor_info->argb_offset = 0;
+		}
+		EMGD_ERROR("ERROR: No memory for XOR cursor!");
+		return -IGD_ERROR_NOMEM;
+	}
+
+	/* Get the register update physical address in RAM */
+	if (context->dispatch.gmm_virt_to_phys(buffer, &buffer_phys)) {
+		EMGD_ERROR_EXIT("Virtual to Physical Address translation failed");
+		return -IGD_ERROR_NOMEM;
+	}
+
+	/*
+	 * TODO: Verify that phys_to_virt returns a valid address for
+	 * agp memory
+	 */
+	cursor_mem = phys_to_virt(buffer_phys);
+	if(cursor_mem){
+		OS_MEMSET(cursor_mem, 0, size);
+	}
+	EMGD_DEBUG("Allocating cursor surface @ 0x%08lx", buffer);
+	cursor->cursor_info->xor_offset = buffer;
+	cursor->cursor_info->xor_pitch = pitch;
+
+	/* Set the dsp cursor resource table */
+	cursor->cursor_info->width = width;
+	cursor->cursor_info->height = height;
+	cursor->cursor_info->pixel_format =
+		(has_rgb32)?IGD_PF_ARGB32:IGD_PF_RGB_2;
+
+	return 0;
+}
+
+/*!
+ * Wait for all instructions on the ringbuffer to complete. This needs to be
+ * done before changing the framebuffer or the display.  If we do not wait
+ * for the ringbuffer to empty, the hardware may lockup.  It would be nice,
+ * if once the timeout occurs, to disable the ringbuffer, and continue with
+ * the mode changed, but this does not seem to be possible.
+ *
+ * @param context
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+/* FIXME: Move this to rb module */
+int dsp_wait_rb(igd_context_t *context)
+{
+	int p;
+	int wait_time = 15;
+	int ret;
+	unsigned long sync_val;
+	os_alarm_t timeout;
+
+	if (context->dispatch.sync) {
+		for (p = 0; p < 2; p++) {
+			if (dsp_context.display_list[p].allocated) {
+				/* Sync the Normal Ring. */
+				sync_val = 0;
+				timeout = OS_SET_ALARM(wait_time * 1000);
+				do {
+					ret = context->dispatch.sync(
+							&dsp_context.display_list[p],
+							IGD_PRIORITY_NORMAL, &sync_val, IGD_SYNC_BLOCK);
+					OS_SCHEDULE();
+				} while ((ret == -IGD_ERROR_BUSY) && (!OS_TEST_ALARM(timeout)));
+
+				if (ret == -IGD_ERROR_BUSY) {
+					EMGD_ERROR("Timeout waiting for sync");
+					return (-IGD_INVAL);
+				}
+
+				if(PIPE(&dsp_context.display_list[p])->queue[IGD_PRIORITY_BIN]) {
+					/* Sync the Binner also. */
+					sync_val = 0;
+					timeout = OS_SET_ALARM(wait_time * 1000);
+					do {
+						ret = context->dispatch.sync(
+								&dsp_context.display_list[p],
+								IGD_PRIORITY_BIN, &sync_val,
+								IGD_SYNC_BLOCK | IGD_SYNC_NOFLUSH_PIPE);
+						OS_SCHEDULE();
+					} while ((ret == -IGD_ERROR_BUSY) && (!OS_TEST_ALARM(timeout)));
+
+					if (ret == -IGD_ERROR_BUSY) {
+						EMGD_ERROR("Timeout waiting for Binner sync");
+						return (-IGD_INVAL);
+					}
+				}
+			}
+		}
+	}
+	return 0;
+}
+
+/*!
+ * Browse through the list of displays and frees the display.
+ *
+ * @param context
+ *
+ * @return void
+ */
+void dsp_shutdown(igd_context_t *context)
+{
+	int i;
+	igd_display_port_t *port = NULL;
+	igd_plane_t *plane = NULL;
+	igd_display_pipe_t *temp_pipe = NULL;
+	unsigned int hal_attr_index = 0;
+
+	EMGD_DEBUG("dsp_shutdown Entry");
+
+	context->mod_dispatch.fb_blend_ovl_override = 0;
+
+	/*
+	 * Free all the ports pt_info's. Need to add this here because there
+	 * is no longer a 1-to-1 relationship between displays and ports.
+	 */
+	while ((port = dsp_get_next_port(context, port, 0)) != NULL) {
+		if (port->pt_info != NULL) {
+			OS_FREE(port->pt_info);
+			port->pt_info = NULL;
+		}
+		if (port->dtd_list != NULL) {
+			OS_FREE(port->dtd_list);
+			port->dtd_list = NULL;
+		}
+		if (port->attr_list != NULL) {
+			if (port->attr_list->attr != NULL) {
+				OS_FREE(port->attr_list->attr);
+			}
+			OS_FREE(port->attr_list);
+			port->attr_list = NULL;
+		}
+
+		/* We should restore the gamma, brightness and contrast attributes
+		 * to their defaults when the HAL is shutting down or else they
+		 * would retain their previous values.
+		 */
+		hal_attr_index = 0;
+		while (PD_ATTR_LIST_END != port->attributes[hal_attr_index].id) {
+			port->attributes[hal_attr_index].current_value =
+				port->attributes[hal_attr_index].default_value;
+
+			hal_attr_index++;
+		}
+	}
+
+
+	/* Free pipes, cursors, and any allocated command queues */
+	while ((temp_pipe = dsp_get_next_pipe(context, temp_pipe, 0)) != NULL) {
+
+		/* probably not needed since shutting down */
+		temp_pipe->inuse = 0;
+		temp_pipe->plane = NULL;
+		temp_pipe->timing = NULL;
+		temp_pipe->owner = NULL;
+	}
+
+	/* Free planes */
+	while ((plane = dsp_get_next_plane(context, plane, 0)) != NULL) {
+		if (plane->plane_info) {
+			free_plane(context, plane);
+		}
+	}
+
+	/* Clear display list */
+	for(i=0; i<MAX_DISPLAYS; i++) {
+		if(dsp_context.display_list[i].allocated) {
+			OS_MEMSET(&dsp_context.display_list[i], 0,
+				sizeof(igd_display_context_t));
+		}
+	}
+
+	/* Clear display ptr assignments */
+	OS_MEMSET(dsp_context.display_ptr_list, 0,
+		sizeof(dsp_context.display_ptr_list));
+
+	if (dsp_context.dsp_dc_list.dc_list != NULL) {
+		OS_FREE(dsp_context.dsp_dc_list.dc_list);
+	}
+
+	return;
+} /* end dsp_shutdown() */
+
+/*!
+ *
+ * @param context
+ *
+ * @return 0
+ */
+static int dsp_full_init(igd_context_t *context)
+{
+	/* Optional Top level Entry Points */
+	context->dispatch.get_pixelformats = igd_get_pixelformats;
+
+	/* Hook up inter-module dispatch functions */
+	context->mod_dispatch.dsp_shutdown = dsp_shutdown;
+
+	return 0;
+}
+
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp_dispatch.h b/drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp_dispatch.h
new file mode 100644
index 0000000..8431df4
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/dsp/cmn/dsp_dispatch.h
@@ -0,0 +1,64 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dsp_dispatch.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _DSP_DISPATCH_H
+#define _DSP_DISPATCH_H
+
+#include <igd.h>
+#include <igd_init.h>
+#include <igd_mode.h>
+
+#include <mode.h>
+
+/*
+ * FIXME: This belong to PI, remove the defaults and let PI set
+ * them as it does for i2c.
+ */
+#define DDC_DEFAULT_SPEED  10  /* Default DDC bus speed in KHz */
+
+typedef struct _dsp_dispatch {
+	igd_plane_t **planes;
+	igd_display_pipe_t **pipes;
+	igd_display_port_t **ports;
+	igd_fb_caps_t *caps;
+	unsigned long *overlay_pfs;
+	unsigned long *render_pfs;
+	unsigned long *texture_pfs;
+	int (*dsp_init)(igd_context_t *context);
+	void (*dsp_control_plane_format)(igd_context_t *context,
+			int enable, int display_plane, 
+			igd_plane_t *plane_override);
+} dsp_dispatch_t;
+
+extern dsp_dispatch_t dsp_dispatch_plb;
+extern dsp_dispatch_t dsp_dispatch_tnc;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/dsp/plb/dsp_plb.c b/drivers/gpu/drm/emgd/emgd/display/dsp/plb/dsp_plb.c
new file mode 100644
index 0000000..9330537
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/dsp/plb/dsp_plb.c
@@ -0,0 +1,709 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dsp_plb.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+
+#include <io.h>
+#include <memory.h>
+
+#include <igd.h>
+#include <igd_mode.h>
+#include <igd_pwr.h>
+
+#include <mode.h>
+#include <utils.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+
+#include "../cmn/dsp_dispatch.h"
+
+#ifdef CONFIG_PLB
+
+extern igd_framebuffer_info_t fb_info_cmn[];
+
+/*
+ * NOTE: Some of these format lists are shared with GMM. For this reason
+ * they cannot be static.
+ */
+unsigned long fb_pixel_formats_plb[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_RGB16_565,
+	IGD_PF_ARGB8_INDEXED,
+	0
+};
+
+unsigned long vga_pixel_formats_plb[] = {
+	IGD_PF_ARGB8_INDEXED,
+	0
+};
+
+#ifndef CONFIG_MICRO
+unsigned long sprite_pixel_formats_plb[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_RGB16_565,
+	IGD_PF_ARGB8_INDEXED,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	0
+};
+
+unsigned long render_pixel_formats_plb[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_RGB16_565,
+	IGD_PF_xRGB16_555,
+	IGD_PF_ARGB16_1555,
+	IGD_PF_ARGB16_4444,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_R16F,
+	IGD_PF_GR32_1616F,
+	IGD_PF_R32F,
+	IGD_PF_ABGR64_16161616F,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	0
+};
+
+unsigned long texture_pixel_formats_plb[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_xBGR32,
+	IGD_PF_RGB16_565,
+	IGD_PF_xRGB16_555,
+	IGD_PF_ARGB16_1555,
+	IGD_PF_ARGB16_4444,
+	IGD_PF_ARGB8_INDEXED,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_YUV420_PLANAR_I420,
+	IGD_PF_YUV420_PLANAR_IYUV,
+	IGD_PF_YUV420_PLANAR_YV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_DVDU_88,
+	IGD_PF_LDVDU_655,
+	IGD_PF_xLDVDU_8888,
+	IGD_PF_DXT1,
+	IGD_PF_DXT2,
+	IGD_PF_DXT3,
+	IGD_PF_DXT4,
+	IGD_PF_DXT5,
+	IGD_PF_L8,
+	IGD_PF_A8,
+	IGD_PF_AL88,
+	IGD_PF_AI44,
+	IGD_PF_L16,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_AWVU32_2101010,
+	IGD_PF_QWVU32_8888,
+	IGD_PF_GR32_1616,
+	IGD_PF_VU32_1616,
+	IGD_PF_R16F,
+	IGD_PF_GR32_1616F,
+	IGD_PF_R32F,
+	IGD_PF_ABGR64_16161616F,
+	0
+};
+
+unsigned long depth_pixel_formats_plb[] = {
+	IGD_PF_Z16,
+	IGD_PF_Z24,
+	IGD_PF_S8Z24,
+	0
+};
+
+
+unsigned long cursor_pixel_formats_plb[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_RGB_2,
+	IGD_PF_RGB_XOR_2,
+	IGD_PF_RGB_T_2,
+	0
+};
+
+unsigned long overlay_pixel_formats_plb[] = {
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_YUV420_PLANAR_I420,
+	IGD_PF_YUV420_PLANAR_IYUV,
+	IGD_PF_YUV420_PLANAR_YV12,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	0
+};
+
+unsigned long video_pixel_formats_plb[] = {
+	IGD_PF_YUV420_PLANAR_NV12,
+	0
+};
+
+unsigned long blt_pixel_formats_plb[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_xBGR32,
+	IGD_PF_RGB16_565,
+	IGD_PF_xRGB16_555,
+	IGD_PF_ARGB16_1555,
+	IGD_PF_ARGB16_4444,
+	IGD_PF_ARGB8_INDEXED,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_YUV420_PLANAR_I420,
+	IGD_PF_YUV420_PLANAR_IYUV,
+	IGD_PF_YUV420_PLANAR_YV12,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	IGD_PF_DVDU_88,
+	IGD_PF_LDVDU_655,
+	IGD_PF_xLDVDU_8888,
+	IGD_PF_DXT1,
+	IGD_PF_DXT2,
+	IGD_PF_DXT3,
+	IGD_PF_DXT4,
+	IGD_PF_DXT5,
+	IGD_PF_Z16,
+	IGD_PF_Z24,
+	IGD_PF_S8Z24,
+	IGD_PF_RGB_2,
+	IGD_PF_RGB_XOR_2,
+	IGD_PF_RGB_T_2,
+	IGD_PF_L8,
+	IGD_PF_A8,
+	IGD_PF_AL88,
+	IGD_PF_AI44,
+	IGD_PF_L16,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_AWVU32_2101010,
+	IGD_PF_QWVU32_8888,
+	IGD_PF_GR32_1616,
+	IGD_PF_VU32_1616,
+	IGD_PF_R16F,
+	IGD_PF_GR32_1616F,
+	IGD_PF_R32F,
+	IGD_PF_ABGR64_16161616F,
+	0
+};
+
+static igd_fb_caps_t caps_table_plb[] = {
+	{IGD_PF_ARGB32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_xRGB32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_ABGR32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_xBGR32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_ARGB32_2101010, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_RGB16_565, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_ARGB8_INDEXED, IGD_CAP_FULL_2D},
+	{0, 0}
+};
+
+#endif
+
+/*
+ * Plane Definitions for PLB family.
+ */
+static igd_plane_t planea_plb = {
+	DSPACNTR, IGD_PLANE_DISPLAY | IGD_PLANE_DIH, 0, 0,
+	fb_pixel_formats_plb, &fb_info_cmn[0], NULL
+};
+
+static igd_plane_t planeb_plb = {
+	DSPBCNTR, IGD_PLANE_DISPLAY | IGD_PLANE_SPRITE | IGD_PLANE_DIH, 0, 0,
+	fb_pixel_formats_plb, &fb_info_cmn[1], NULL
+};
+
+static igd_plane_t planec_plb = {
+	DSPCCNTR, IGD_PLANE_SPRITE, 0, 0,
+	OPT_MICRO_VALUE(sprite_pixel_formats_plb, NULL), NULL, NULL
+};
+
+static igd_plane_t plane_vga_plb = {
+	VGACNTRL, IGD_PLANE_VGA, 0, 0,
+	vga_pixel_formats_plb, NULL, NULL
+};
+
+static igd_plane_t plane_overlay_plb = {
+	OVADD, IGD_PLANE_OVERLAY, 0, 0,
+	OPT_MICRO_VALUE(overlay_pixel_formats_plb, NULL), NULL, NULL
+};
+
+static igd_plane_t plane_cursora_plb = {
+	CUR_A_CNTR, IGD_PLANE_CURSOR|IGD_CURSOR_USE_PIPEA|IGD_CURSOR_USE_PIPEB, 0,0,
+	OPT_MICRO_VALUE(cursor_pixel_formats_plb, NULL), NULL, NULL
+};
+
+static igd_plane_t plane_cursorb_plb = {
+	CUR_B_CNTR, IGD_PLANE_CURSOR|IGD_CURSOR_USE_PIPEA|IGD_CURSOR_USE_PIPEB, 0,0,
+	OPT_MICRO_VALUE(cursor_pixel_formats_plb, NULL), NULL, NULL
+};
+
+/*
+ * Plane lists for PLB family members.
+ */
+/* Two Main Plane, One Sprite, One VGA, One Overlay, Two Cursor */
+static igd_plane_t *plane_table_plb[] = {
+	&planeb_plb,
+	&planea_plb,
+	&planec_plb,
+	&plane_vga_plb,
+	&plane_overlay_plb,
+	&plane_cursora_plb,
+	&plane_cursorb_plb,
+	NULL
+};
+
+static igd_clock_t clock_a_plb = {
+	DPLLACNTR, FPA0, 16
+};
+
+static igd_clock_t clock_b_plb = {
+	DPLLBCNTR, FPB0, 16
+};
+
+/*
+ * Pipe definitions for PLB family.
+ */
+static igd_display_pipe_t pipea_plb = {
+	0, PIPEA_CONF, PIPEA_TIMINGS, DPALETTE_A, &clock_a_plb,
+	(IGD_PIPE_IS_PIPEA | IGD_PORT_SHARE_DIGITAL),
+	0, 0,{NULL, NULL, NULL}, NULL, NULL, NULL,
+	NULL, NULL
+};
+
+static igd_display_pipe_t pipeb_plb = {
+	1, PIPEB_CONF, PIPEB_TIMINGS, DPALETTE_B, &clock_b_plb,
+	(IGD_PIPE_IS_PIPEB | IGD_PORT_SHARE_LVDS),
+	0, 0,{NULL, NULL, NULL}, NULL, NULL, NULL,
+	NULL, NULL
+};
+
+static igd_display_pipe_t *pipe_table_plb[] = {
+	&pipea_plb,
+	&pipeb_plb,
+	NULL
+};
+
+/*
+ * Port definitions for PLB family.
+ */
+
+/*
+ * Port number: Port number is 1-number of available ports on any hardware.
+ * Here are the definitions:
+ *
+ * On PLB:
+ * =======
+ * Port mappings:
+ *   1 - None
+ *   2 - DVO B port
+ *   3 - None
+ *   4 - Internal LVDS port
+ *   5 - None
+ *
+ * Note: Port number should match with port numbers in port parameters.
+ *       See igd_init.h for more information.
+ */
+
+#endif
+/*
+ * These are the port attributes that the PLB core support.
+ * Note that currently it only contains color correction attributes.
+ * Eventually, this will include all the attributes.
+ */
+igd_attr_t port_attrib_plb[IGD_MAX_PORTS][5] = {
+	{ /* Config for port 1:  Integrated TV Encoder (Alviso only) */
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_GAMMA,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Gamma",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x202020,  /* default */
+			0x202020,  /* current */
+			0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+			0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_BRIGHTNESS,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Brightness",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_CONTRAST,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Contrast",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_EXTENSION,
+			0,
+			"",
+			PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+			0,
+			0,
+			0,
+			0,
+			0),
+		PD_MAKE_ATTR(
+			PD_ATTR_LIST_END,
+			0,
+			"",
+			0,
+			0,
+			0,
+			0,
+			0,
+			0)
+	},
+	{ /* Config for port 2:  DVO B */
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_GAMMA,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Gamma",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x202020,  /* default */
+			0x202020,  /* current */
+			0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+			0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_BRIGHTNESS,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Brightness",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_CONTRAST,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Contrast",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_EXTENSION,
+			0,
+			"",
+			PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+			0,
+			0,
+			0,
+			0,
+			0),
+		PD_MAKE_ATTR(
+			PD_ATTR_LIST_END,
+			0,
+			"",
+			0,
+			0,
+			0,
+			0,
+			0,
+			0)
+	},
+	{ /* Config for port 3:  DVO C */
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_GAMMA,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Gamma",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x202020,  /* default */
+			0x202020,  /* current */
+			0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+			0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_BRIGHTNESS,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Brightness",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_CONTRAST,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Contrast",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_EXTENSION,
+			0,
+			"",
+			PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+			0,
+			0,
+			0,
+			0,
+			0),
+		PD_MAKE_ATTR(
+			PD_ATTR_LIST_END,
+			0,
+			"",
+			0,
+			0,
+			0,
+			0,
+			0,
+			0)
+	},
+	{ /* Config for port 4:  LVDS */
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_GAMMA,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Gamma",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x202020,  /* default */
+			0x202020,  /* current */
+			0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+			0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_BRIGHTNESS,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Brightness",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_CONTRAST,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Contrast",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_EXTENSION,
+			0,
+			"",
+			PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+			0,
+			0,
+			0,
+			0,
+			0),
+		PD_MAKE_ATTR(
+			PD_ATTR_LIST_END,
+			0,
+			"",
+			0,
+			0,
+			0,
+			0,
+			0,
+			0)
+	},
+	{ /* Config for port 5:  ANALOG */
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_GAMMA,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Gamma",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x202020,  /* default */
+			0x202020,  /* current */
+			0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+			0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_BRIGHTNESS,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Brightness",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_FB_CONTRAST,
+			PD_ATTR_TYPE_RANGE,
+			"Frame Buffer Contrast",
+			PD_ATTR_FLAG_PD_INVISIBLE,
+			0x808080,
+			0x808080,
+			0x000000,    /* Min: */
+			0xFFFFFF,    /* Max: */
+			1),
+		PD_MAKE_ATTR(
+			PD_ATTR_ID_EXTENSION,
+			0,
+			"",
+			PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+			0,
+			0,
+			0,
+			0,
+			0),
+		PD_MAKE_ATTR(
+			PD_ATTR_LIST_END,
+			0,
+			"",
+			0,
+			0,
+			0,
+			0,
+			0,
+			0)
+	}
+};
+
+#ifdef CONFIG_PLB
+
+igd_display_port_t dvob_port_plb = {
+	IGD_PORT_DIGITAL, 2, "SDVO B", 0x61140, GMBUS_DVO_REG, 0,
+	GMBUS_DVOB_DDC, 0xA0,
+	(IGD_PORT_USE_PIPEA | IGD_VGA_COMPRESS | IGD_RGBA_COLOR |
+	 IGD_PORT_GANG),
+	TVCLKINBC, 0, IGD_POWERSTATE_D0, IGD_POWERSTATE_D0,
+	NULL, NULL,
+	NULL, NULL, NULL, 0, NULL, 0,
+	DDC_DEFAULT_SPEED, NULL, NULL, NULL, NULL, 0, NULL, 0, 0,
+	IGD_POWERSTATE_UNDEFINED,
+	port_attrib_plb[2 - 1], /* Port Number - 1 */
+	0, { NULL },
+	(BIT14 | BIT16 | BIT17),
+	(BIT17), 1,
+
+};
+
+static igd_display_port_t lvds_port_plb = {
+	IGD_PORT_LVDS, 4, "IntLVDS", 0x61180, 0, 0,
+	GMBUS_INT_LVDS_DDC, 0xA0,
+	(IGD_PORT_USE_PIPEB | IGD_VGA_COMPRESS),
+	DREFCLK, 0, IGD_POWERSTATE_D0, IGD_POWERSTATE_D0, NULL, NULL,
+	NULL, NULL, NULL, 0, NULL, 0,
+	DDC_DEFAULT_SPEED, NULL, NULL, NULL, NULL, 0, NULL, 0, 0,
+	IGD_POWERSTATE_UNDEFINED,
+	port_attrib_plb[4 - 1], /* Port Number - 1 */
+	0, { NULL }, 0, 0, 0,
+};
+
+static igd_display_port_t *port_table_plb[] = {
+	&lvds_port_plb,
+	&dvob_port_plb,
+	NULL
+};
+
+static int dsp_init_plb(igd_context_t *context)
+{
+	return 0;
+}
+
+void dsp_control_plane_format_plb(igd_context_t *context,
+		int enable, int plane, igd_plane_t *plane_override)
+{
+	igd_plane_t * pl = NULL;
+	unsigned char *mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+	unsigned long tmp;
+
+	if (plane_override == NULL) {
+		pl = (plane == 0) ? &planea_plb : &planeb_plb;
+	} else {
+		pl = plane_override;
+	}
+	tmp = EMGD_READ32(mmio +  pl->plane_reg);
+
+	/*
+	 * Pixel format bits (29:26) are in plane control register 0x70180 for
+	 * Plane A and 0x71180 for Plane B
+	 * 0110 = XRGB pixel format
+	 * 0111 = ARGB pixel format
+	 * Note that the plane control register is double buffered and will be
+	 * updated on the next VBLANK operation so there is no need to sync with
+	 * an explicit VSYNC.
+	 */
+	if(enable) {
+		if((tmp & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_RGB_8888) {
+			EMGD_WRITE32(tmp | DSPxCNTR_ARGB_8888, mmio +  pl->plane_reg);
+			tmp = EMGD_READ32(mmio + pl->plane_reg + 4);
+			EMGD_WRITE32(tmp, mmio + pl->plane_reg + 4);
+			EMGD_DEBUG("Changed pixel format from XRGB to ARGB\n");
+		}
+	} else {
+		if((tmp & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_ARGB_8888) {
+			tmp = tmp & (~(DSPxCNTR_SRC_FMT_MASK));
+			EMGD_WRITE32(tmp | DSPxCNTR_RGB_8888, mmio +  pl->plane_reg);
+			tmp = EMGD_READ32(mmio + pl->plane_reg + 4);
+			EMGD_WRITE32(tmp, mmio + pl->plane_reg + 4);
+			OS_SLEEP(100);
+			EMGD_DEBUG("Changed pixel format from ARGB to XRGB\n");
+		}
+	}
+	EMGD_DEBUG("Plane register 0x%lX has value of 0x%X\n", pl->plane_reg,
+			EMGD_READ32(mmio + pl->plane_reg));
+
+}
+
+dsp_dispatch_t dsp_dispatch_plb = {
+	plane_table_plb, pipe_table_plb, port_table_plb,
+	OPT_MICRO_VALUE(caps_table_plb, NULL),
+	OPT_MICRO_VALUE(overlay_pixel_formats_plb, NULL),
+	OPT_MICRO_VALUE(render_pixel_formats_plb, NULL),
+	OPT_MICRO_VALUE(texture_pixel_formats_plb, NULL),
+	dsp_init_plb,
+	dsp_control_plane_format_plb,
+};
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/dsp/tnc/dsp_tnc.c b/drivers/gpu/drm/emgd/emgd/display/dsp/tnc/dsp_tnc.c
new file mode 100644
index 0000000..0697657
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/dsp/tnc/dsp_tnc.c
@@ -0,0 +1,542 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dsp_tnc.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include <io.h>
+#include <memory.h>
+
+#include <igd.h>
+#include <igd_mode.h>
+#include <igd_pwr.h>
+
+#include <mode.h>
+#include <utils.h>
+
+#include <tnc/regs.h>
+#include <tnc/context.h>
+
+#include "../cmn/dsp_dispatch.h"
+
+#ifdef CONFIG_TNC
+
+extern igd_framebuffer_info_t fb_info_cmn[];
+
+/*
+ * NOTE: Some of these format lists are shared with GMM. For this reason
+ * they cannot be static.
+ */
+unsigned long fb_pixel_formats_tnc[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_RGB16_565,
+	IGD_PF_ARGB8_INDEXED,
+	0
+};
+
+unsigned long vga_pixel_formats_tnc[] = {
+	IGD_PF_ARGB8_INDEXED,
+	0
+};
+
+#ifndef CONFIG_MICRO
+unsigned long sprite_pixel_formats_tnc[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_RGB16_565,
+	IGD_PF_ARGB8_INDEXED,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	0
+};
+
+unsigned long render_pixel_formats_tnc[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_RGB16_565,
+	IGD_PF_xRGB16_555,
+	IGD_PF_ARGB16_1555,
+	IGD_PF_ARGB16_4444,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_R16F,
+	IGD_PF_GR32_1616F,
+	IGD_PF_R32F,
+	IGD_PF_ABGR64_16161616F,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	0
+};
+
+unsigned long texture_pixel_formats_tnc[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_xBGR32,
+	IGD_PF_RGB16_565,
+	IGD_PF_xRGB16_555,
+	IGD_PF_ARGB16_1555,
+	IGD_PF_ARGB16_4444,
+	IGD_PF_ARGB8_INDEXED,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_YUV420_PLANAR_I420,
+	IGD_PF_YUV420_PLANAR_IYUV,
+	IGD_PF_YUV420_PLANAR_YV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_DVDU_88,
+	IGD_PF_LDVDU_655,
+	IGD_PF_xLDVDU_8888,
+	IGD_PF_DXT1,
+	IGD_PF_DXT2,
+	IGD_PF_DXT3,
+	IGD_PF_DXT4,
+	IGD_PF_DXT5,
+	IGD_PF_L8,
+	IGD_PF_A8,
+	IGD_PF_AL88,
+	IGD_PF_AI44,
+	IGD_PF_L16,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_AWVU32_2101010,
+	IGD_PF_QWVU32_8888,
+	IGD_PF_GR32_1616,
+	IGD_PF_VU32_1616,
+	IGD_PF_R16F,
+	IGD_PF_GR32_1616F,
+	IGD_PF_R32F,
+	IGD_PF_ABGR64_16161616F,
+	0
+};
+
+unsigned long depth_pixel_formats_tnc[] = {
+	IGD_PF_Z16,
+	IGD_PF_Z24,
+	IGD_PF_S8Z24,
+	0
+};
+
+
+unsigned long cursor_pixel_formats_tnc[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_RGB_2,
+	IGD_PF_RGB_XOR_2,
+	IGD_PF_RGB_T_2,
+	0
+};
+
+unsigned long overlay_pixel_formats_tnc[] = {
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_YUV420_PLANAR_I420,
+	IGD_PF_YUV420_PLANAR_IYUV,
+	IGD_PF_YUV420_PLANAR_YV12,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	0
+};
+
+unsigned long video_pixel_formats_tnc[] = {
+	IGD_PF_YUV420_PLANAR_NV12,
+	0
+};
+
+unsigned long blt_pixel_formats_tnc[] = {
+	IGD_PF_ARGB32,
+	IGD_PF_xRGB32,
+	IGD_PF_ABGR32,
+	IGD_PF_xBGR32,
+	IGD_PF_RGB16_565,
+	IGD_PF_xRGB16_555,
+	IGD_PF_ARGB16_1555,
+	IGD_PF_ARGB16_4444,
+	IGD_PF_ARGB8_INDEXED,
+	IGD_PF_YUV422_PACKED_YUY2,
+	IGD_PF_YUV422_PACKED_UYVY,
+	IGD_PF_YUV420_PLANAR_I420,
+	IGD_PF_YUV420_PLANAR_IYUV,
+	IGD_PF_YUV420_PLANAR_YV12,
+	IGD_PF_YUV420_PLANAR_NV12,
+	IGD_PF_YUV410_PLANAR_YVU9,
+	IGD_PF_DVDU_88,
+	IGD_PF_LDVDU_655,
+	IGD_PF_xLDVDU_8888,
+	IGD_PF_DXT1,
+	IGD_PF_DXT2,
+	IGD_PF_DXT3,
+	IGD_PF_DXT4,
+	IGD_PF_DXT5,
+	IGD_PF_Z16,
+	IGD_PF_Z24,
+	IGD_PF_S8Z24,
+	IGD_PF_RGB_2,
+	IGD_PF_RGB_XOR_2,
+	IGD_PF_RGB_T_2,
+	IGD_PF_L8,
+	IGD_PF_A8,
+	IGD_PF_AL88,
+	IGD_PF_AI44,
+	IGD_PF_L16,
+	IGD_PF_ARGB32_2101010,
+	IGD_PF_AWVU32_2101010,
+	IGD_PF_QWVU32_8888,
+	IGD_PF_GR32_1616,
+	IGD_PF_VU32_1616,
+	IGD_PF_R16F,
+	IGD_PF_GR32_1616F,
+	IGD_PF_R32F,
+	IGD_PF_ABGR64_16161616F,
+	0
+};
+
+static igd_fb_caps_t caps_table_tnc[] = {
+	{IGD_PF_ARGB32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_xRGB32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_ABGR32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_xBGR32, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_ARGB32_2101010, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_RGB16_565, IGD_CAP_FULL_2D | IGD_CAP_BLEND},
+	{IGD_PF_ARGB8_INDEXED, IGD_CAP_FULL_2D},
+	{0, 0}
+};
+
+#endif
+
+/*
+ * Plane Definitions for Atom E6xx family.
+ */
+static igd_plane_t planea_tnc = {
+	DSPACNTR, IGD_PLANE_DISPLAY | IGD_PLANE_DIH | IGD_PLANE_USE_PIPEA, 0, 0,
+	fb_pixel_formats_tnc, &fb_info_cmn[0], NULL
+};
+
+static igd_plane_t planeb_tnc = {
+	DSPBCNTR, IGD_PLANE_DISPLAY | IGD_PLANE_SPRITE | IGD_PLANE_DIH | IGD_PLANE_USE_PIPEB, 0, 0,
+	fb_pixel_formats_tnc, &fb_info_cmn[1], NULL
+};
+
+static igd_plane_t planec_tnc = {
+	DSPCCNTR, IGD_PLANE_SPRITE, 0, 0,
+	OPT_MICRO_VALUE(sprite_pixel_formats_tnc, NULL), NULL, NULL
+};
+
+static igd_plane_t plane_vga_tnc = {
+	VGACNTRL, IGD_PLANE_VGA, 0, 0,
+	vga_pixel_formats_tnc, NULL, NULL
+};
+
+static igd_plane_t plane_overlay_tnc = {
+	OVADD, IGD_PLANE_OVERLAY, 0, 0,
+	OPT_MICRO_VALUE(overlay_pixel_formats_tnc, NULL), NULL, NULL
+};
+
+static igd_plane_t plane_cursora_tnc = {
+	CUR_A_CNTR, IGD_PLANE_CURSOR|IGD_CURSOR_USE_PIPEA|IGD_CURSOR_USE_PIPEB, 0,0,
+	OPT_MICRO_VALUE(cursor_pixel_formats_tnc, NULL), NULL, NULL
+};
+
+static igd_plane_t plane_cursorb_tnc = {
+	CUR_B_CNTR, IGD_PLANE_CURSOR|IGD_CURSOR_USE_PIPEA|IGD_CURSOR_USE_PIPEB, 0,0,
+	OPT_MICRO_VALUE(cursor_pixel_formats_tnc, NULL), NULL, NULL
+};
+
+/*
+ * Plane lists for Atom E6xx family members.
+ */
+/* Two Main Plane, One Sprite, One VGA, One Overlay, Two Cursor */
+static igd_plane_t *plane_table_tnc[] = {
+	&planea_tnc,
+	&planeb_tnc,
+	&planec_tnc,
+	&plane_vga_tnc,
+	&plane_overlay_tnc,
+	&plane_cursora_tnc,
+	&plane_cursorb_tnc,
+	NULL
+};
+
+static igd_clock_t clock_a_tnc = {
+	DPLLACNTR, FPA0, 17
+};
+
+static igd_clock_t clock_b_tnc = {
+	DPLLBCNTR, FPB0, 16
+};
+
+/*
+ * Pipe definitions for Atom E6xx family.
+ * Pipe A is always tied to LVDS and Pipe B always to SDVO
+ */
+static igd_display_pipe_t pipea_tnc = {
+	0, PIPEA_CONF, PIPEA_TIMINGS, DPALETTE_A, &clock_a_tnc,
+	(IGD_PIPE_IS_PIPEA | IGD_PORT_SHARE_LVDS),
+	0, 0,{NULL, NULL, NULL}, NULL, NULL, NULL,
+	NULL, NULL
+};
+
+static igd_display_pipe_t pipeb_tnc = {
+	1, PIPEB_CONF, PIPEB_TIMINGS, DPALETTE_B, &clock_b_tnc,
+	(IGD_PIPE_IS_PIPEB | IGD_PORT_SHARE_DIGITAL),
+	0, 0,{NULL, NULL, NULL}, NULL, NULL, NULL,
+	NULL, NULL
+};
+
+static igd_display_pipe_t *pipe_table_tnc[] = {
+	&pipea_tnc,
+	&pipeb_tnc,
+	NULL
+};
+#endif /*CONFIG_TNC*/
+
+/*
+ * HAL attributes for Atom E6xx display ports
+ * These are the port attributes that the PLB core support.
+ * Note that currently it only contains color correction attributes.
+ */
+igd_attr_t port_attrib_sdvo_tnc[5] = {
+	/* Config for port 2:  DVO B */
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_FB_GAMMA,
+		PD_ATTR_TYPE_RANGE,
+		"Frame Buffer Gamma",
+		PD_ATTR_FLAG_PD_INVISIBLE,
+		0x202020,  /* default */
+		0x202020,  /* current */
+		0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+		0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+		1),
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_FB_BRIGHTNESS,
+		PD_ATTR_TYPE_RANGE,
+		"Frame Buffer Brightness",
+		PD_ATTR_FLAG_PD_INVISIBLE,
+		0x808080,
+		0x808080,
+		0x000000,    /* Min: */
+		0xFFFFFF,    /* Max: */
+		1),
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_FB_CONTRAST,
+		PD_ATTR_TYPE_RANGE,
+		"Frame Buffer Contrast",
+		PD_ATTR_FLAG_PD_INVISIBLE,
+		0x808080,
+		0x808080,
+		0x000000,    /* Min: */
+		0xFFFFFF,    /* Max: */
+		1),
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_EXTENSION,
+		0,
+		"",
+		PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+		0,
+		0,
+		0,
+		0,
+		0),
+	PD_MAKE_ATTR(
+		PD_ATTR_LIST_END,
+		0,
+		"",
+		0,
+		0,
+		0,
+		0,
+		0,
+		0)
+};
+
+/* HAL attributes for LVDS port */
+igd_attr_t port_attrib_lvds_tnc[5] = {
+	/* Config for port 4:  LVDS */
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_FB_GAMMA,
+		PD_ATTR_TYPE_RANGE,
+		"Frame Buffer Gamma",
+		PD_ATTR_FLAG_PD_INVISIBLE,
+		0x202020,  /* default */
+		0x202020,  /* current */
+		0x131313,  /* Min:  ~0.6 in 3i.5f format for R-G-B*/
+		0xC0C0C0,  /* Max:  6 in 3i.5f format for R-G-B   */
+		1),
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_FB_BRIGHTNESS,
+		PD_ATTR_TYPE_RANGE,
+		"Frame Buffer Brightness",
+		PD_ATTR_FLAG_PD_INVISIBLE,
+		0x808080,
+		0x808080,
+		0x000000,    /* Min: */
+		0xFFFFFF,    /* Max: */
+		1),
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_FB_CONTRAST,
+		PD_ATTR_TYPE_RANGE,
+		"Frame Buffer Contrast",
+		PD_ATTR_FLAG_PD_INVISIBLE,
+		0x808080,
+		0x808080,
+		0x000000,    /* Min: */
+		0xFFFFFF,    /* Max: */
+		1),
+	PD_MAKE_ATTR(
+		PD_ATTR_ID_EXTENSION,
+		0,
+		"",
+		PD_ATTR_FLAG_PD_INVISIBLE|PD_ATTR_FLAG_USER_INVISIBLE,
+		0,
+		0,
+		0,
+		0,
+		0),
+	PD_MAKE_ATTR(
+		PD_ATTR_LIST_END,
+		0,
+		"",
+		0,
+		0,
+		0,
+		0,
+		0,
+		0)
+};
+
+#ifdef CONFIG_TNC
+/*
+ * Port definitions for Atom E6xx gfx.
+ *
+ * Port mappings:
+ *   1 - None
+ *   2 - sDVO B port
+ *   3 - None
+ *   4 - Internal LVDS port
+ *   5 - None
+ */
+igd_display_port_t dvob_port_tnc = {
+	IGD_PORT_DIGITAL, 2, "SDVO B", 0x61140, GMBUS_DVO_REG, 0,
+	GMBUS_DVOB_DDC, 0xA0,
+	(IGD_PORT_USE_PIPEB | IGD_VGA_COMPRESS),
+	TVCLKINBC, 0, IGD_POWERSTATE_D0, IGD_POWERSTATE_D0,
+	NULL, NULL,
+	NULL, NULL, NULL, 0, NULL, 0,
+	DDC_DEFAULT_SPEED, NULL, NULL, NULL, NULL, 0, NULL, 0, 0,
+	IGD_POWERSTATE_UNDEFINED,
+	port_attrib_sdvo_tnc,
+	0, { NULL },
+	(BIT14 | BIT16 | BIT17),
+	(BIT17), 1,
+
+};
+
+static igd_display_port_t lvds_port_tnc = {
+	IGD_PORT_LVDS, 4, "IntLVDS", 0x61180, 0, 0,
+	I2C_INT_LVDS_DDC, 0xA0,
+	(IGD_PORT_USE_PIPEA | IGD_VGA_COMPRESS),
+	DREFCLK, 0, IGD_POWERSTATE_D0, IGD_POWERSTATE_D0, NULL, NULL,
+	NULL, NULL, NULL, 0, NULL, 0,
+	DDC_DEFAULT_SPEED, NULL, NULL, NULL, NULL, 0, NULL, 0, 0,
+	IGD_POWERSTATE_UNDEFINED,
+	port_attrib_lvds_tnc,
+	0, { NULL }, 0, 0, 0,
+};
+
+static igd_display_port_t *port_table_tnc[] = {
+	&lvds_port_tnc,
+	&dvob_port_tnc,
+	NULL
+};
+
+static int dsp_init_tnc(igd_context_t *context)
+{
+	return 0;
+}
+
+void dsp_control_plane_format_tnc(igd_context_t *context,
+		int enable, int plane, igd_plane_t *plane_override)
+{
+	igd_plane_t * pl = NULL;
+	unsigned char *mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+	unsigned long tmp;
+
+	if (plane_override == NULL) {
+		pl = (plane == 0) ? &planea_tnc : &planeb_tnc;
+	} else {
+		pl = plane_override;
+	}
+	tmp = EMGD_READ32(mmio +  pl->plane_reg);
+	/*
+	 * Pixel format bits (29:26) are in plane control register 0x70180 for
+	 * Plane A and 0x71180 for Plane B
+	 * 0110 = XRGB pixel format
+	 * 0111 = ARGB pixel format
+	 * Note that the plane control register is double buffered and will be
+	 * updated on the next VBLANK operation so there is no need to sync with
+	 * an explicit VSYNC.
+	 */
+	if(enable) {
+		if((tmp & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_RGB_8888) {
+			tmp = tmp & (~(DSPxCNTR_SRC_FMT_MASK));
+			EMGD_WRITE32(tmp | DSPxCNTR_ARGB_8888, mmio + pl->plane_reg);
+			EMGD_READ32(mmio + pl->plane_reg);
+			tmp = EMGD_READ32(mmio + pl->plane_reg + 0x1c);
+			EMGD_WRITE32(tmp, mmio + pl->plane_reg + 0x1c);
+			EMGD_DEBUG("Changed pixel format from XRGB to ARGB\n");
+		}
+	} else {
+		if((tmp & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_ARGB_8888) {
+			tmp = tmp & (~(DSPxCNTR_SRC_FMT_MASK));
+			EMGD_WRITE32(tmp | DSPxCNTR_RGB_8888, mmio +  pl->plane_reg);
+			EMGD_READ32(mmio + pl->plane_reg);
+			tmp = EMGD_READ32(mmio + pl->plane_reg + 0x1c);
+			EMGD_WRITE32(tmp, mmio + pl->plane_reg + 0x1c);
+			OS_SLEEP(100);
+			EMGD_DEBUG("Changed pixel format from ARGB to XRGB\n");
+		}
+	}
+	EMGD_DEBUG("Plane register 0x%lX has value of 0x%X\n", pl->plane_reg,
+			EMGD_READ32(mmio + pl->plane_reg));
+}
+
+dsp_dispatch_t dsp_dispatch_tnc = {
+	plane_table_tnc, pipe_table_tnc, port_table_tnc,
+	OPT_MICRO_VALUE(caps_table_tnc, NULL),
+	OPT_MICRO_VALUE(overlay_pixel_formats_tnc, NULL),
+	OPT_MICRO_VALUE(render_pixel_formats_tnc, NULL),
+	OPT_MICRO_VALUE(texture_pixel_formats_tnc, NULL),
+	dsp_init_tnc,
+	dsp_control_plane_format_tnc,
+};
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/cmn/igd_mode.c b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/igd_mode.c
new file mode 100755
index 0000000..70de834
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/igd_mode.c
@@ -0,0 +1,2340 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_mode.c
+ * $Revision: 1.35 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Contains client interface support functions for display allocations
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <config.h>
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+#include <gart.h>
+
+#include <igd.h>
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_render.h>
+#include <igd_pwr.h>
+#include <igd_errno.h>
+#include <igd_gmm.h>
+#include <igd_pd.h>
+
+#include <debug.h>
+#include <math_fix.h>
+#include <context.h>
+#include <rb.h>
+#include <pd.h>
+#include <intelpci.h>
+#include <dsp.h>
+#include <pi.h>
+#include <dispatch.h>
+#include <mode.h>
+#include <mode_access.h>
+#include <dsp.h>
+#include <utils.h>
+#include <general.h>
+
+#include "emgd_drv.h"
+#include "drm_emgd_private.h"
+#include "match.h"
+#include "mode_dispatch.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+#define CURSOR_1_STATE 0x01
+#define CURSOR_2_STATE 0x02
+#define CURSOR_STATE(display) \
+	((display == &display_list[0]) ? CURSOR_1_STATE : CURSOR_2_STATE)
+
+/*!
+ * This function is an exported utility function.
+ * Its meant for calculating backbuffer to frontbuffer coordinates when in
+ * rotation, render-scaling and / or flipping (in any combination).
+ *
+ * Eventually, this function will be only for HAL usage.
+ *
+ * @param rotation
+ * @param do_flip
+ * @param do_rscale
+ * @param x_rnd_scale
+ * @param y_rnd_scale
+ * @param front_width
+ * @param front_height
+ * @param x
+ * @param y
+ * @param hotx
+ * @param hoty
+ *
+ * @return void
+ */
+void igd_fb_to_screen(unsigned short rotation,
+					unsigned char do_flip, unsigned char do_rscale,
+					unsigned long x_rnd_scale, unsigned long y_rnd_scale,
+					unsigned short front_width, unsigned short front_height,
+					unsigned short *x, unsigned short *y,
+					unsigned short hotx, unsigned short hoty);
+
+int igd_kms_match_mode(void *emgd_encoder,
+	void *fb_info, igd_timing_info_h **timing);
+
+/* Do not malloc the context */
+extern mode_context_t mode_context[];
+
+extern emgd_drm_config_t config_drm;
+
+/* This symbol has to be in this file as it is part of
+ *  driver ONLY.
+ */
+static fw_info_t global_fw_info;
+
+/*!
+ * This function sets the per-port attribute to the values given in
+ * the parameter.  If the requested port is the PIPE master, then
+ * this function will proceed to program the palette.
+ *
+ * @param display used to program palette, if necessary
+ * @param attr_to_set contains the new color attribute to set
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+int set_color_correct(igd_display_context_t *display,
+	const igd_range_attr_t *attr_to_set)
+{
+	igd_display_port_t    *port;
+	igd_attr_t            *hal_attr_list;
+	mode_dispatch_t       *dispatch = mode_context->dispatch;
+	igd_range_attr_t      *attr     = NULL;
+	unsigned int          i         = 0;
+	int  				  changed_flag_set = 1;
+
+	EMGD_TRACE_ENTER;
+
+	port          = PORT_OWNER(display);
+	hal_attr_list = port->attributes;
+
+
+	/* update the HAL's own copy of attributes */
+	while (PD_ATTR_LIST_END != hal_attr_list[i].id) {
+		if (attr_to_set->id == hal_attr_list[i].id) {
+			attr = (igd_range_attr_t *) &hal_attr_list[i];
+
+			if (!(attr_to_set->flags & PD_ATTR_FLAG_VALUE_CHANGED)){
+				changed_flag_set = 0;
+			}
+			/* make sure the value is within range */
+			attr->current_value = OS_MAX(attr_to_set->current_value,
+									attr->min);
+
+			attr->current_value = OS_MIN(attr_to_set->current_value,
+									attr->max);
+
+			break;
+		}
+
+		i++;
+	}
+
+	/* if we didn't find anything, then quit with an error */
+	if (PD_ATTR_LIST_END == hal_attr_list[i].id) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* If the current display is not the pipe master, then we're done */
+	if (PIPE(display)->owner != display) {
+		return 0;
+	}
+
+	/* Program palette */
+	if(changed_flag_set){
+		dispatch = mode_context->dispatch;
+		dispatch->full->set_color_correct(display);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function is used to put the mode module into the
+ * requested powerstate.
+ *
+ * @param context SS level igd_context
+ * @param powerstate IGD_POWERSTATE_D*
+ *
+ * @return 0 on success
+ * @return >0 on failure
+ */
+int mode_pwr(igd_context_t *context,
+	unsigned long powerstate)
+{
+
+#if 0
+	igd_display_context_t *display_list[2];
+	igd_display_context_t *display = NULL;
+	int i,j;
+#endif
+
+	EMGD_TRACE_ENTER;
+
+	switch(powerstate) {
+	case IGD_POWERSTATE_D0:
+		mode_context->dispatch->program_cdvo();
+		toggle_vblank_interrupts(TRUE);
+		break;
+
+#if 0
+		for(j = 0; j < 2; j++) {
+			display = display_list[j];
+			/* if there is no display or display not allocated, continue */
+			if(!display || !display->allocated) {
+				continue;
+			}
+
+			if (!PIPE(display)->timing) {
+				/* if there is no pipe timing, cannot enable, continue */
+				EMGD_DEBUG("No pipe timing for port = %lu",
+							display->port_number);
+				continue;
+			}
+
+			/* Set port power state */
+			for (i = 0; i < IGD_MAX_PORTS; i++) {
+				if (display->port[i] &&
+					(((igd_display_port_t *)display->port[i])->pt_info->flags &
+						IGD_DISPLAY_ENABLE)) {
+					mode_context->dispatch->program_port(display, i+1, TRUE);
+				}
+			}
+
+			mode_context->dispatch->program_pipe(display, TRUE);
+			mode_context->dispatch->program_plane(display, TRUE);
+
+			for (i = 0; i < IGD_MAX_PORTS; i++) {
+				if (display->port[i] &&
+					(((igd_display_port_t *)display->port[i])->pt_info->flags &
+						IGD_DISPLAY_ENABLE)) {
+					mode_context->dispatch->post_program_port(display, i+1, 0);
+				}
+			}
+
+		}
+
+		break;
+#endif
+
+	case IGD_POWERSTATE_D1:
+	case IGD_POWERSTATE_D2:
+	case IGD_POWERSTATE_D3:
+		toggle_vblank_interrupts(FALSE);
+		mode_context->dispatch->reset_plane_pipe_ports(mode_context->context);
+		break;
+	default:
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end mode_pwr() */
+
+/*!
+ * This function is used to save mode module register state.
+ *
+ * @param context SS level igd_context
+ * @param state pointer to module_state handle, where module_state_h is
+ * pointer to actual state
+ *@param flags should have IGD_REG_SAVE_MODE bit set for save
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int mode_save(igd_context_t *context, module_state_h *state,
+	unsigned long *flags)
+{
+	mode_state_t       *mstate;
+	int                i, ret;
+	igd_display_port_t *port = NULL;
+	inter_module_dispatch_t *md;
+
+	EMGD_TRACE_ENTER;
+
+	if (!state || !(*flags & IGD_REG_SAVE_MODE)) {
+		EMGD_ERROR_EXIT("NULL pointer to save mode state or"
+			" flags don't have IGD_REG_SAVE_MODE bit set.");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* First allocate memory for mode state which includes pd states */
+	mstate = OS_ALLOC(sizeof(mode_state_t));
+	if (!mstate) {
+		EMGD_ERROR_EXIT("memory allocation failed.");
+		return -IGD_ERROR_NOMEM;
+	}
+	OS_MEMSET(mstate, 0, sizeof(mode_state_t));
+
+	md = &context->mod_dispatch;
+
+	/* Call pd_save */
+	port = NULL;
+	i = 0;
+	while ((port = md->dsp_get_next_port(context, port, 0)) != NULL) {
+		if (port->pd_driver) {
+			EMGD_DEBUG("saving %s", port->pd_driver->name);
+			ret = port->pd_driver->pd_save(port->pd_context,
+				&(mstate->pd_state[i].state), 0);
+			if (ret) {
+				EMGD_DEBUG("pd_save failed for %s", port->pd_driver->name);
+			}
+
+			mstate->pd_state[i].port = port;
+			i++;
+		}
+	}
+
+	/* Update mode state */
+	*state = (module_state_h) mstate;
+
+	EMGD_DEBUG("mode_save: saved %d port driver states.", i);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end mode_save() */
+
+/*!
+ * This function is used to save mode module register state.
+ *
+ * @param context SS level igd_context
+ * @param state pointer to module_state handle, where module_state_h is
+ * pointer to actual state
+ * *@param flags should have IGD_REG_SAVE_MODE bit set for restore
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+int mode_restore(igd_context_t *context, module_state_h *state,
+	unsigned long *flags)
+{
+	int                i, ret;
+	igd_display_port_t *port = NULL;
+	mode_state_t       *mstate;
+
+	EMGD_TRACE_ENTER;
+
+	if (!state || !(*flags & IGD_REG_SAVE_MODE)) {
+		EMGD_ERROR_EXIT("Null mode state.or trying to restore without a save");
+		return 0;
+	}
+
+	mstate = (mode_state_t *)(*state);
+
+	if (!mstate) {
+		EMGD_DEBUG("mode_restore: mstate = NULL");
+		EMGD_TRACE_EXIT;
+		return 0;
+	}
+
+	/* Restore all PD drivers */
+	i = 0;
+	while (mstate->pd_state[i].port) {
+		port = mstate->pd_state[i].port;
+
+		EMGD_DEBUG("restoring %s", port->pd_driver->name);
+		ret = port->pd_driver->pd_restore(port->pd_context,
+				mstate->pd_state[i].state, 0);
+		if (ret) {
+			/* What can be done if restore fails */
+			EMGD_DEBUG("pd_restore failed for %s", port->pd_driver->name);
+		}
+
+		i++;
+	}
+
+	/* Free the memory allocated */
+	OS_FREE(mstate);
+	*state = NULL;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end mode_restore() */
+
+/*!
+ *
+ * @param cursor
+ * @param image
+ * @param rotate
+ * @param flip
+ * @param width
+ * @param height
+ *
+ * @return void
+ */
+static void load_argb_cursor_image(unsigned long *cursor,
+		unsigned long *image,
+		int rotate, int flip,
+		int width, int height)
+{
+	int w, h, x, y;
+	unsigned short nx, ny;
+	unsigned long *i;
+
+	w = width;
+	h = height;
+
+	/* make sure size is constrained to 64x64 */
+	if (w > 64) {
+		w = 64;
+	}
+	if (h > 64) {
+		h = 64;
+	}
+
+	/* Copy image */
+	for (y = 0; y < h; y++) {
+		i = image;
+		image += width;
+		for (x = 0; x < w; x++) {
+			/* rotate, flip  x,y here. No scaling!  */
+			nx = (unsigned short) x;
+			ny = (unsigned short) y;
+			igd_fb_to_screen((unsigned short) rotate, (unsigned char) flip,
+				0, 0, 0, 64, 64, &nx, &ny, 0, 0);
+			cursor[nx + (64 * ny)] = *i++;
+		}
+	}
+}
+
+/*!
+ *
+ * @param cursor
+ * @param image
+ * @param rotate
+ * @param flip
+ * @param width
+ * @param height
+ *
+ * @return void
+ */
+static void load_xor_cursor_image(unsigned char *cursor,
+		unsigned char *image,
+		int rotate, int flip,
+		int width, int height)
+{
+	int j, x, y;
+	int pixel_num, byte_num, line_num;
+	int npixel_num, nbyte_num, nline_num, nbit_num;
+	unsigned short nx, ny;
+	unsigned char b_val, sbit, mask, pixel;
+
+	for (j = 0; j < 2; j++) {
+		cursor += (j * 8);
+		image += (j * 8);
+		for (y = 0; y < 64; y++) {
+			for (x = 0; x < 64; x++) {
+				pixel_num = x + (y * 64);
+				line_num = pixel_num / 64;
+				byte_num = (pixel_num & 63) / 8;
+				b_val = *(image + (16 * line_num) + byte_num);
+				pixel = (b_val >> ( 7 - (pixel_num & 7))) & 0x01;
+
+				nx = (unsigned short) x;
+				ny = (unsigned short) y;
+				igd_fb_to_screen((unsigned short) rotate, (unsigned char) flip,
+					0, 0, 0, (unsigned short) width, (unsigned short) height,
+					&nx, &ny, 0 , 0);
+				npixel_num = nx + (ny * 64);
+				nline_num = npixel_num / 64;
+				nbyte_num = (npixel_num & 63) / 8;
+				nbit_num = 7 - (npixel_num & 7);
+				b_val = *(cursor + (16 * nline_num) + nbyte_num);
+
+				sbit = pixel << nbit_num;
+				mask = 0x01 << nbit_num;
+				b_val = (b_val & ~mask) | sbit;
+				*(cursor + (16 * nline_num) + nbyte_num) = b_val;
+
+				/*(cursor + (16 * line_num) + byte_num) = b_val; */
+			}
+		}
+	}
+}
+
+/*!
+ * This function caclulates the correct cursor position from IAL
+ * provided coordinates.  It takes into account hotspot, rotation, flip,
+ * and render_scale.
+ *
+ * This takes an x and y coordinate and sets the internal cursor
+ * info structure with the updated values.  In addition, it
+ * sets a flag if the coordiantes are outside the displays active
+ * area.
+ *
+ * @param display
+ * @param x
+ * @param y
+ * @param hotx
+ * @param hoty
+ *
+ * @return void
+ */
+static void igd_set_cursor_pos(igd_display_context_t *display,
+		unsigned short x, unsigned short y,
+		unsigned short hotx, unsigned short hoty)
+{
+	igd_display_context_t *primary;
+	igd_cursor_info_t *internal_ci;
+	igd_display_info_t *timing;
+	igd_display_plane_t *plane;
+	unsigned char render_scale = 0;
+	unsigned short rotation, flip;
+	unsigned long cursor_state;
+
+	if (!display || !PLANE(display) || !PIPE(display) ||
+		!PIPE(display)->cursor || !PIPE(display)->cursor->cursor_info ||
+		!PORT_OWNER(display) || !PORT_OWNER(display)->pt_info) {
+		return;
+	}
+
+	internal_ci = PIPE(display)->cursor->cursor_info;
+	timing = PORT_OWNER(display)->pt_info;
+	plane = PLANE(display);
+
+	rotation = (unsigned short) ((internal_ci->rotation &
+			IGD_RENDER_OP_ROT_MASK) >> 8) * 90;
+	flip = (unsigned short) (internal_ci->rotation &
+			IGD_RENDER_OP_FLIP) >> 10;
+
+	/*
+	 * Handle rotation, flip and render scale
+	 * Note that the x,y arguments are unsigned, but cursor cordinates
+	 * are signed and we need to preserve the signness when saving back
+	 * into internal_ci structure!
+	 */
+	if ((internal_ci->render_scale_x > 0) ||
+			(internal_ci->render_scale_y > 0)) {
+		render_scale = 1;
+	} else {
+		render_scale = 0;
+	}
+
+	igd_fb_to_screen((unsigned short) rotation, (unsigned char) flip,
+			render_scale,
+			internal_ci->render_scale_x, internal_ci->render_scale_y,
+			(unsigned short) plane->fb_info->width,
+			(unsigned short) plane->fb_info->height,
+			&x, &y,
+			hotx, hoty);
+
+	/* Adjust the x and y values relative to the current display offset */
+	internal_ci->x_offset = (long)((short)x - (short)timing->x_offset);
+	internal_ci->y_offset = (long)((short)y - (short)timing->y_offset);
+
+
+	/* Adjust the cursor offset for rotation and flip */
+
+	switch (rotation) {
+	case 0:
+		if (flip) {
+			internal_ci->x_offset -= 63;
+		}
+		break;
+	case 90:
+		internal_ci->y_offset += 1;
+		if (!flip) {
+			internal_ci->y_offset -= 63;
+		}
+		break;
+	case 180:
+		internal_ci->x_offset += 1;
+		internal_ci->y_offset -= 63;
+		if (!flip) {
+			internal_ci->x_offset -= 63;
+		}
+		break;
+	case 270:
+		internal_ci->x_offset -= 63;
+		if (flip) {
+			internal_ci->y_offset -= 63;
+		}
+		break;
+	default:
+		break;
+	}
+
+	display->context->mod_dispatch.dsp_get_dc(NULL, &primary, NULL);
+	if(display == primary) {
+		cursor_state = CURSOR_1_STATE;
+	} else {
+		cursor_state = CURSOR_2_STATE;
+	}
+
+	/*
+	 * When panning, the cursor can be positioned off screen. The hardware
+	 * doesn't like it if this happens.  Thus, we set a flag to indicate
+	 * that the cursor is currently off screen. That way it can be
+	 * turned off when actually programmed.
+	 *
+	 * This also moves the position so that it is at the very edge of the
+	 * screen, just in case it is turned on.
+	 */
+
+	internal_ci->off_screen &= ~cursor_state;
+
+	/* Make sure the cursor is fully displayed */
+	if (internal_ci->x_offset < -63) {
+		internal_ci->x_offset = -63;
+		internal_ci->off_screen |= cursor_state;
+	}
+	if (internal_ci->x_offset >= (long)timing->width) {
+		internal_ci->x_offset = timing->width - 1;
+		internal_ci->off_screen |= cursor_state;
+	}
+	if (internal_ci->y_offset < -63) {
+		internal_ci->y_offset = -63;
+		internal_ci->off_screen |= cursor_state;
+	}
+	if (internal_ci->y_offset >= (long)timing->height) {
+		internal_ci->y_offset = timing->height - 1;
+		internal_ci->off_screen |= cursor_state;
+	}
+
+	return;
+}
+
+/*!
+ * This function sets the cursor_info obtained from igd_alloc_cursor
+ * and programs the cursor
+ *
+ * @param display_handle
+ * @param cursor_info
+ * @param image
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int igd_alter_cursor(igd_display_h display_handle,
+	igd_cursor_info_t *cursor_info,
+	unsigned char *image)
+{
+	igd_display_context_t *display = (igd_display_context_t *) display_handle;
+	igd_display_context_t *display2;
+	igd_display_context_t *primary;
+	igd_cursor_info_t *internal_ci;
+	unsigned short rotation, flip;
+	unsigned long cursor_state;
+	unsigned long cursor_state2;
+	unsigned long *cursora = NULL;
+	unsigned char *cursorx = NULL;
+//	unsigned long in_dihclone=0;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(display, "Null Display Handle", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(cursor_info, "Null cursor info", -IGD_ERROR_INVAL);
+
+//	in_dihclone = display->context->mod_dispatch.in_dih_clone_mode;
+	if(validate_cursor(cursor_info, display)) {
+		EMGD_ERROR_EXIT("pixel_format validation failed.");
+		return -IGD_ERROR_INVAL;
+	}
+
+	internal_ci = PIPE(display)->cursor->cursor_info;
+
+	cursor_info->argb_offset = internal_ci->argb_offset;
+	cursor_info->xor_offset = internal_ci->xor_offset;
+
+	rotation = (unsigned short) ((cursor_info->rotation &
+		IGD_RENDER_OP_ROT_MASK) >> 8) * 90;
+	flip = (unsigned short) (cursor_info->rotation & IGD_RENDER_OP_FLIP) >> 10;
+
+	display->context->mod_dispatch.dsp_get_dc(NULL, &primary, NULL);
+
+	/* If cursor plane is mirrored, then do the same for the other cursor */
+//	if (PIPE(display)->cursor->mirror != NULL || in_dihclone) {
+	if (PIPE(display)->cursor->mirror != NULL ) {
+		if(display == primary) {
+			display->context->mod_dispatch.dsp_get_dc(NULL, NULL,
+				&display2);
+		} else {
+			display2 = primary;
+		}
+	}
+
+	/*
+	 * Loading new cursor (for both primary and clone):
+	 * 1. Blank cursor image: This can be avoided if new hotspot and
+	 *    new bitmap size are same as existing hotspot and image size.
+	 * 2. Move cursor to new location accounting for new hotspot
+	 * 3. Wait for vblank to load the new cursor to avoid flashing/tearing.
+	 *    This impacts performace tests as cursor shape changes several times
+	 *    while running/loading tests and this wait for vblank counts against
+	 *    test times. So going without a wait for vblank. If flashing/tearing
+	 *    becomes a must fix issue, then uncomment below wait_vblank().
+	 * 4. Load new image
+	 */
+
+	if ((image != NULL) && (cursor_info->flags & IGD_CURSOR_LOAD_ARGB_IMAGE)) {
+		unsigned long buffer_phys = 0;
+
+		/* Calculate the cursor's address in kernel-space: */
+		if (display->context->dispatch.gmm_virt_to_phys(internal_ci->argb_offset,
+				&buffer_phys)) {
+			EMGD_ERROR_EXIT("GMM Virtual to Physical Address translation failed");
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * TODO: Verify that phys_to_virt returns a valid address for
+		 * agp memory
+		 */
+		cursora = phys_to_virt(buffer_phys);
+		EMGD_DEBUG("ARGB cursor virtual address is 0x%p", cursora);
+		if (cursora == NULL) {
+			EMGD_ERROR_EXIT("Physical to Virtual Address translation failed");
+			return -IGD_ERROR_INVAL;
+		}
+
+		/* Clear cursor plane */
+		OS_MEMSET(cursora, 0, 64*64*4);
+
+	} else if ((image != NULL) &&
+		(cursor_info->flags & IGD_CURSOR_LOAD_XOR_IMAGE)) {
+
+		unsigned long buffer_phys = 0;
+
+		/* Calculate the cursor's address in kernel-space: */
+		if (display->context->dispatch.gmm_virt_to_phys(internal_ci->xor_offset,
+				&buffer_phys)) {
+			EMGD_ERROR_EXIT("GMM Virtual to Physical Address translation failed");
+			return -IGD_ERROR_INVAL;
+		}
+
+		/*
+		 * TODO: Verify that phys_to_virt returns a valid address for
+		 * agp memory
+		 */
+		cursorx = phys_to_virt(buffer_phys);
+		EMGD_DEBUG("XOR cursor virtual address is 0x%p", cursorx);
+		if (cursorx == NULL) {
+			EMGD_ERROR_EXIT("Physical to Virtual Address translation failed");
+			return -IGD_ERROR_INVAL;
+		}
+	}
+
+	/* calculate the cursor position adjusting to new hotspot */
+	igd_set_cursor_pos(display,
+		(unsigned short)cursor_info->x_offset,
+		(unsigned short)cursor_info->y_offset,
+		(unsigned short)cursor_info->hot_x,
+		(unsigned short)cursor_info->hot_y);
+
+	/* Update internal structure with altered data */
+	internal_ci->pixel_format = cursor_info->pixel_format;
+	internal_ci->palette[0] = cursor_info->palette[0];
+	internal_ci->palette[1] = cursor_info->palette[1];
+	internal_ci->palette[2] = cursor_info->palette[2];
+	internal_ci->palette[3] = cursor_info->palette[3];
+	internal_ci->flags = cursor_info->flags;
+	internal_ci->rotation = cursor_info->rotation;
+	internal_ci->render_scale_x = cursor_info->render_scale_x;
+	internal_ci->render_scale_y = cursor_info->render_scale_y;
+
+	if(display == primary) {
+		cursor_state = CURSOR_1_STATE;
+		cursor_state2 = CURSOR_2_STATE;
+	} else {
+		cursor_state = CURSOR_2_STATE;
+		cursor_state2 = CURSOR_1_STATE;
+	}
+
+	if ((cursor_info->flags & IGD_CURSOR_ON) &&
+			!(internal_ci->off_screen & cursor_state)) {
+		mode_context->dispatch->full->program_cursor(display, TRUE);
+	} else {
+		mode_context->dispatch->full->program_cursor(display, FALSE);
+	}
+
+//	if (PIPE(display)->cursor->mirror || (in_dihclone && display == primary) ) {
+	if (PIPE(display)->cursor->mirror ) {
+		igd_set_cursor_pos(display2,
+			(unsigned short)cursor_info->x_offset,
+			(unsigned short)cursor_info->y_offset,
+			(unsigned short)cursor_info->hot_x,
+			(unsigned short)cursor_info->hot_y);
+
+		if ((cursor_info->flags & IGD_CURSOR_ON) &&
+				!(internal_ci->off_screen & cursor_state2)) {
+			mode_context->dispatch->full->program_cursor(display2, TRUE);
+		} else {
+			mode_context->dispatch->full->program_cursor(display2, FALSE);
+		}
+	}
+
+	/* Pitch may have been altered by program_cursor */
+	cursor_info->xor_pitch = internal_ci->xor_pitch;
+	cursor_info->argb_pitch = internal_ci->argb_pitch;
+
+	/* wait for VBLANK */
+	/* mode_context->dispatch->wait_vblank(display); */
+
+	if ((image != NULL) &&
+			(cursor_info->flags & IGD_CURSOR_LOAD_ARGB_IMAGE)) {
+		load_argb_cursor_image(cursora, (unsigned long *)image, rotation, flip,
+				cursor_info->width, cursor_info->height);
+
+	} else if ((image != NULL) &&
+			(cursor_info->flags & IGD_CURSOR_LOAD_XOR_IMAGE)) {
+		load_xor_cursor_image(cursorx, image, rotation, flip,
+				cursor_info->width, cursor_info->height);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function programs the cursor position.  It takes into account
+ * rotation, flip, and render_scale.  It also knows about clone mode
+ * and manages the clone cursor automaticlly.
+ *
+ * @param display_handle
+ * @param cursor_info
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int igd_alter_cursor_pos(igd_display_h display_handle,
+	igd_cursor_info_t *cursor_info)
+{
+	igd_display_context_t *display = (igd_display_context_t *) display_handle;
+	igd_display_context_t *display2 = NULL;
+//	igd_display_context_t * primary= NULL;
+	igd_cursor_info_t *internal_ci;
+	unsigned long cursor_on_screen;
+	unsigned long cursor_state=0;
+//	unsigned long in_dihclone=0;
+
+	/* If there is no cursor, return immediately */
+	if (!display || !PIPE(display) || !PIPE(display)->cursor) {
+		return -IGD_INVAL;
+	}
+
+//	in_dihclone = display->context->mod_dispatch.in_dih_clone_mode;
+
+//	display->context->mod_dispatch.dsp_get_dc(NULL, &primary, NULL);
+
+
+//	if (PIPE(display)->cursor->mirror != NULL || ( in_dihclone && display == primary )) {
+	if (PIPE(display)->cursor->mirror != NULL) {
+		display->context->mod_dispatch.dsp_get_dc(NULL, NULL, &display2);
+		if (display == display2) {
+			display->context->mod_dispatch.dsp_get_dc(NULL, &display2, NULL);
+			cursor_state = CURSOR_2_STATE;
+		} else {
+			cursor_state = CURSOR_1_STATE;
+		}
+	}
+	/* Reset display2 if cursor isn't setup */
+	if (display2) {
+		if (!PIPE(display2) || !PIPE(display2)->cursor) {
+			display2 = NULL;
+		}
+	}
+
+	while (display) {
+		internal_ci = PIPE(display)->cursor->cursor_info;
+		cursor_on_screen = (internal_ci->off_screen & cursor_state);
+
+		igd_set_cursor_pos(display,
+				(unsigned short)cursor_info->x_offset,
+				(unsigned short)cursor_info->y_offset,
+				(unsigned short)cursor_info->hot_x,
+				(unsigned short)cursor_info->hot_y);
+
+		if (cursor_on_screen !=
+			(internal_ci->off_screen & cursor_state)) {
+			/*
+			 * Cursor has moved either on or off screen since the last
+			 * call. If it has moved back on screen, turn the cursor
+			 * back on.  If it moved off screen, turn it off.
+			 */
+			if (internal_ci->off_screen & cursor_state) {
+				mode_context->dispatch->full->program_cursor(display, FALSE);
+
+			} else if (cursor_info->flags & IGD_CURSOR_ON) {
+			/*
+			 * Only program the cursor back on if the IAL already has it
+			 * ON.  This is to prevent the problem where the IAL turned
+			 * of the cursor on purpose, e.g. a user app turns it off, and
+			 * then have the HAL turn it back on when moving the cursor
+			 * from one screen to another.
+			 */
+				mode_context->dispatch->full->program_cursor(display, TRUE);
+			}
+		}
+
+		/* Program cursor position */
+		if ((cursor_info->flags & IGD_CURSOR_ON) &&
+			!(internal_ci->off_screen & cursor_state)) {
+			mode_context->dispatch->full->alter_cursor_pos(
+				(igd_display_h)display, internal_ci);
+		}
+
+		/* Switch to the second display if it is mirrored */
+		display = display2;
+		display2 = NULL;
+		/* Switch the cursor states as well */
+		if(cursor_state == CURSOR_1_STATE) {
+			cursor_state = CURSOR_2_STATE;
+		} else {
+			cursor_state = CURSOR_1_STATE;
+		}
+	}
+
+	return 0;
+}
+
+/*!
+ * This function returns the current framebuffer and
+ * display information.
+ *
+ * @param hDisplay required.  The hDisplay contains the display
+ * 	information to return.  This parameter was returned from a
+ * 	previous call to igd_alloc_display().
+ * @param port_number
+ * @param pFbInfo required and allocated by the caller.  The pFbInfo
+ * 	struct is returned to the caller describing the current
+ * 	frame buffer.
+ * @param pPtInfo required and allocated by the caller.  The
+ * 	pPtInfo struct is returned to caller describing the
+ *	requested display parameters.
+ * @param ulFlags Currently not used
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int igd_get_display(
+	igd_display_h hDisplay,
+	unsigned short port_number,
+	pigd_framebuffer_info_t pFbInfo,
+	pigd_display_info_t pPtInfo,
+	unsigned long ulFlags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)hDisplay;
+
+	EMGD_TRACE_ENTER;
+
+	/* Check for NULL pointers */
+	EMGD_ASSERT(pFbInfo, "Null FB Info", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(pPtInfo, "Null PT Info", -IGD_ERROR_INVAL);
+
+	/* If the port->pt_info isn't set that means it is called just after
+	 * igd_alloc_display and before igd_alter_display(). So, just fill
+	 * port_type into pPtInfo */
+	if (PORT(display, port_number)->pt_info == NULL) {
+		OS_MEMSET(pPtInfo, 0, sizeof(igd_display_info_t));
+		pPtInfo->flags = PORT(display, port_number)->port_type;
+	} else {
+		OS_MEMCPY(pPtInfo, PORT(display, port_number)->pt_info,
+			sizeof(igd_display_info_t));
+	}
+
+	if (PLANE(display)->fb_info == NULL) {
+		OS_MEMSET(pFbInfo, 0, sizeof(igd_framebuffer_info_t));
+	} else {
+		OS_MEMCPY(pFbInfo, PLANE(display)->fb_info,
+			sizeof(igd_framebuffer_info_t));
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function pans the display on the display device.
+ * It takes a x_offset, y_offset into the frame buffer and
+ * sets the display from (x_offset, y_offset) to
+ * (x_offset+width, y_offset+height).
+ * If x_offset+width, y_offset+height crosses frame buffer
+ * width and heigth, then it will return error.
+ *
+ * @param hDisplay pointer to an IGD_DISPLAY pointer returned
+ * 	from a successful call to igd_allocate_display().
+ * @param x_offset frame buffer offsets from (0, 0)
+ * @param y_offset frame buffer offsets from (0, 0)
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+long igd_pan_display(igd_display_h hDisplay,
+		unsigned long x_offset, unsigned long y_offset)
+{
+	igd_display_context_t   *display = (igd_display_context_t *)hDisplay;
+	igd_framebuffer_info_t  *fb_info;
+	igd_display_info_t      *pt_info;
+	unsigned long           x_old;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT((display && PORT_OWNER(display) && PORT_OWNER(display)->pt_info),
+		"Unvalid Display Handle", -IGD_ERROR_INVAL);
+
+	fb_info = PLANE(display)->fb_info;
+	pt_info = PORT_OWNER(display)->pt_info;
+
+	/* Check paning can be done or not */
+	if (!fb_info || !pt_info) {
+		EMGD_ERROR_EXIT("Panning cannot be done. fb, pt infos weren't set.");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (! (PORT_OWNER(display)->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+		EMGD_DEBUG("Currently this display is not enabled.");
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((pt_info->width == fb_info->width) &&
+		(pt_info->height == fb_info->height)) {
+		EMGD_DEBUG("FB and display pt_info are same size, no paning.");
+		EMGD_TRACE_EXIT;
+		return 0;
+	}
+
+	/*
+	 * handle case primary: 1280x720  seconday:1024x768
+	 * TODO:
+	 * "ideally the FB 720 height should center on LVDS 768 height and pan across horizontally that is 1280 FB width pans on LVDS 1024 width."
+	 */
+	if (fb_info->height >= pt_info->height) {
+		if (pt_info->width + x_offset > fb_info->width ||
+			pt_info->height + y_offset > fb_info->height) {
+			EMGD_ERROR_EXIT("invalid offsets are passing frame buffer.");
+			return -IGD_ERROR_INVAL;
+		}
+	} else {
+		if (pt_info->width + x_offset > fb_info->width ) {
+			EMGD_ERROR_EXIT("invalid offsets are passing frame buffer.");
+			return -IGD_ERROR_INVAL;
+		}
+	}
+
+	/* Now do the paning.
+	 * Note: Never change the fb_base_offset (it always points to the
+	 *       first pixel in the frame buffer), nor the visible_offset (it
+	 *       always points to the first pixel of the visible buffer). */
+	/* TODO: How to know which fb we are using, frontbuffer/backbuffer? */
+
+	x_old = x_offset;
+	mode_context->dispatch->full->set_display_base(display, fb_info,
+			&x_offset, &y_offset);
+
+	/* Save offsets into pt_info */
+	pt_info->x_offset = (unsigned short)x_offset;
+	pt_info->y_offset = (unsigned short)y_offset;
+
+	/* FIXME:
+	 *   This could be negative, and thus conflict with the error codes
+	 *   returned above.  However, this is needed for Whitney to compensate
+	 *   for the framebuffer offset alignment requirements and should only
+	 *   be positive.
+	 */
+	EMGD_TRACE_EXIT;
+	return (x_offset - x_old);
+
+} /* end igd_pan_display() */
+
+/*!
+ * This function get attributes for a port. SS will
+ * allocate the memory required to return the *attr_list.
+ * This is a live copy of attributes used by both IAL and SS.
+ * Don't deallocate this memory. This will be freed by SS.
+ *
+ * The list returned will contain a list of HAL attributes
+ * followed by a pointer to the PD attributes.
+ *
+ * @param driver_handle pointer to an IGD context pointer returned
+ * 	from a successful call to igd_init().
+ * @param port_number port number of port to get pd attributes.
+ * @param num_attrs pointer to return the number of attributes
+ * 	returned in attr_list.
+ * @param attr_list - pointer to return the attributes.
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int igd_get_attrs(
+	igd_driver_h  driver_handle,
+	unsigned short port_number,
+	unsigned long *num_attrs,
+	igd_attr_t **attr_list)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_context_t *display;
+	int                   ret;
+	unsigned long         hal_attrib_num, pd_attr_num;
+	igd_display_port_t    *port;
+	igd_attr_t            *pd_attr_list = NULL;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* basic parameter check */
+	EMGD_ASSERT(driver_handle, "Null Driver Handle", -IGD_ERROR_INVAL);
+
+	/* Get the display context that is currently using this port */
+	context->mod_dispatch.dsp_get_display(port_number, &display, &port, 0);
+
+	if(!display || !port) {
+		/*
+		 * No display is using this port,
+		 * should we abort at this point?
+		 */
+		EMGD_TRACE_EXIT;
+		printk(KERN_ALERT "[EGD] igd_get_attrs exiting: "
+				"No display is using port %d.\n", port_number);
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Get PD attributes */
+	pd_attr_num = *num_attrs;
+	ret = port->pd_driver->get_attrs( port->pd_context,
+									&pd_attr_num,
+									&pd_attr_list );
+
+	if (ret) {
+		pd_attr_num  = 0;
+		pd_attr_list = NULL;
+	}
+
+	/* find the extension attribute and attach the pd_attr_list */
+	for (hal_attrib_num = 0;
+			PD_ATTR_LIST_END != port->attributes[hal_attrib_num].id;
+			hal_attrib_num++ ) {
+
+		if (PD_ATTR_ID_EXTENSION == port->attributes[hal_attrib_num].id ) {
+			((igd_extension_attr_t *)(&port->attributes[hal_attrib_num]))->extension = pd_attr_list;
+		}
+	}
+
+	/* If attr_list is NULL then the caller is only interested in
+	 * the number of attributes
+	 */
+	if( NULL != attr_list ) {
+		*attr_list = port->attributes;
+	}
+
+	if (0 == pd_attr_num ) {
+		/* if there are no PD attributes, then subtract 1 from hal_attrib_num
+		 * so that the "extension" attribute is not counted*/
+		*num_attrs = hal_attrib_num - 1;
+	} else {
+		/* subtract 1 because we should not count the "extension" attribute */
+		*num_attrs = hal_attrib_num + pd_attr_num - 1;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end igd_get_attrs() */
+
+/*!
+ * This function set attributes on a port.
+ *
+ * @param driver_handle pointer to an IGD context pointer returned
+ * 	from a successful call to igd_init().
+ * @param port_number port number of port to get pd attributes.
+ * @param num_attrs pointer to return the number of attributes
+ * 	returned in attr_list.
+ * @param attr_list - pointer to return the attributes.
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int igd_set_attrs(
+	igd_driver_h  driver_handle,
+	unsigned short port_number,
+	unsigned long num_attrs,
+	igd_attr_t *attr_list)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_context_t *display;
+	igd_display_port_t    *port;
+	igd_attr_t            *attr;
+	unsigned int          i;
+
+	unsigned long         num_attrs_set = 0;
+	int                   ret = 0, setmode = 0;
+	igd_timing_info_t     *pd_timing_table = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	/* basic parameter check */
+	EMGD_ASSERT(driver_handle, "Null Driver Handle", -IGD_ERROR_INVAL);
+
+	/* Get the display context that is currently using this port */
+	context->mod_dispatch.dsp_get_display(port_number, &display, &port, 0);
+	if(!display || !port) {
+		/* No display is using this port, should we abort at this point? */
+		return -IGD_ERROR_INVAL;
+	}
+
+	if(num_attrs == 0) {
+		return 0;
+	}
+
+
+	/*
+	 * Take care of HAL attributes.  Here we keep track of the number of
+	 * attributes set.  If the number set is equal to num_attrs, then we
+	 * don't need to call port driver's set_attr when this loop is done
+	 */
+	for( i = 0; i < num_attrs; i ++ ) {
+		switch (attr_list[i].id) {
+			case PD_ATTR_ID_FB_GAMMA:
+			case PD_ATTR_ID_FB_BRIGHTNESS:
+			case PD_ATTR_ID_FB_CONTRAST:
+				set_color_correct(display, (igd_range_attr_t *) &attr_list[i]);
+				num_attrs_set++;
+				break;
+
+			default:
+				/* ignore all non HAL-related attributes */
+				break;
+		}
+	}
+
+	/* Pass the attribute list down to the port driver for futher processing
+	 * if necessary */
+	if (num_attrs > num_attrs_set) {
+		ret = port->pd_driver->set_attrs(port->pd_context, num_attrs, attr_list);
+
+		if (ret) {
+			return -IGD_INVAL;
+		}
+	}
+
+	attr = attr_list;
+	i = 0;
+	while (i++ < num_attrs) {
+		if (attr->flags & PD_ATTR_FLAG_SETMODE) {
+			setmode = 1;
+			break;
+		}
+		attr++;
+	}
+
+	ret = 0;
+	if (setmode) {
+		/* Update internal timings */
+		ret = port->pd_driver->get_timing_list(port->pd_context,
+				(pd_timing_t *)port->timing_table,
+				(pd_timing_t **)&pd_timing_table);
+
+		if (ret) {
+			EMGD_ERROR_EXIT("get_timing_list failed.");
+			return -IGD_ERROR_INVAL;
+		}
+
+		port->timing_table = pd_timing_table;
+		port->num_timing = get_native_dtd(pd_timing_table,
+				PI_SUPPORTED_TIMINGS, &port->fp_native_dtd,
+				PD_MODE_DTD_FP_NATIVE);
+		ret = IGD_DO_QRY_SETMODE;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* end igd_set_attrs() */
+
+/*!
+ *
+ * @param display_h
+ * @param width
+ * @param height
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int mode_getresolution(
+	igd_display_h display_h,
+	unsigned long *width,
+	unsigned long *height)
+{
+	igd_display_context_t *display;
+	int ret = 0;
+
+	if (display_h) {
+		display = (igd_display_context_t *) display_h;
+
+		if (PORT_OWNER(display)->pt_info) {
+			*width  = PORT_OWNER(display)->pt_info->width;
+			*height = PORT_OWNER(display)->pt_info->height;
+			ret = 0;
+		}
+		else {
+			ret = -IGD_ERROR_INVAL;
+		}
+
+	} else {
+		ret = -IGD_ERROR_INVAL;
+	}
+
+	return ret;
+}
+
+/*!
+ *
+ * @param driver_handle A igd_driver_h type returned from a previous
+ * 	igd_init call.
+ * @param port_number
+ * @param port_info Returns the information about port
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int igd_get_port_info(igd_driver_h driver_handle,
+	unsigned short port_number,
+	igd_port_info_t *port_info)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_port_t    *port;
+	pd_port_status_t      port_status;
+	int                   ret;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(driver_handle, "Invalid Driver Handle", -IGD_ERROR_INVAL);
+
+	if(port_number >= (IGD_MAX_PORTS + 1)) {
+		EMGD_ERROR("Error, invalid port number. ");
+		return -IGD_INVAL;
+	}
+
+	OS_MEMSET(port_info, 0, sizeof(igd_port_info_t));
+
+	context->mod_dispatch.dsp_get_display(port_number, NULL, &port, 0);
+
+	if (!port || !port->pd_driver || !port->pd_driver->pd_get_port_status) {
+		EMGD_ERROR_EXIT("pd_get_port_status not implemented. ");
+		return -IGD_ERROR_INVAL;
+	}
+
+	pd_strcpy(port_info->pd_name, port->pd_driver->name);
+	pd_strcpy(port_info->port_name, port->port_name);
+
+	port_info->port_num = port->port_number;
+	OS_MEMCPY(&port_info->driver_version, port->pd_driver->version,
+		sizeof(pd_version_t));
+
+	ret = port->pd_driver->pd_get_port_status(port->pd_context, &port_status);
+	if (ret == PD_SUCCESS) {
+		if (port_status.connected == PD_DISP_STATUS_DETACHED) {
+			/* 0 = Not Connected */
+			port_info->connected = 0;
+		} else {
+			/* PD_DISP_STATUS_ATTACHED, PD_DISP_STATUS_UNKNOWN */
+			port_info->connected = 1;
+		}
+	} else {
+		port_info->connected = 1;
+	}
+
+	port_info->display_type = port_status.display_type;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param driver_handle A igd_driver_h type returned from a previous
+ * 	igd_init call.
+ * @param i2c_reg
+ * @param flags
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int igd_access_i2c(
+	igd_driver_h  driver_handle,
+	igd_i2c_reg_t *i2c_reg,
+	unsigned long flags)
+{
+	igd_context_t  *context = (igd_context_t *)driver_handle;
+	unsigned char  i;
+	unsigned long  *gpio, num_gpio;
+	int            ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	if (!driver_handle || !i2c_reg) {
+		return -IGD_INVAL;
+	}
+
+	num_gpio = mode_context->dispatch->get_gpio_sets(&gpio);
+
+	if (i2c_reg->bus_id >= num_gpio) {
+		EMGD_DEBUG("Invalid bus number specified.");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((unsigned long)(i2c_reg->reg + i2c_reg->num_bytes) > 0xFF) {
+		EMGD_DEBUG("Invalid number of %d bytes requested from reg 0x%x.",
+			i2c_reg->num_bytes, i2c_reg->reg);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (flags == IGD_I2C_WRITE) {
+		pd_reg_t temp_reg[2];
+		temp_reg[1].reg = PD_REG_LIST_END;
+
+		/* I2C write operation */
+		for (i=0; i<i2c_reg->num_bytes; i++) {
+			temp_reg[0].reg = i2c_reg->reg + i;
+			temp_reg[0].value = i2c_reg->buffer[i];
+			ret = context->mod_dispatch.i2c_write_reg_list(
+				context,
+				gpio[i2c_reg->bus_id],
+				i2c_reg->i2c_speed,
+				i2c_reg->dab,
+				temp_reg,
+				0);
+			if (ret) {
+				EMGD_DEBUG("i2c write error.");
+				break;
+			}
+		}
+	} else if (flags == IGD_I2C_READ) {
+		/* I2C read operation */
+		ret = context->mod_dispatch.i2c_read_regs(
+			context,
+			gpio[i2c_reg->bus_id],
+			i2c_reg->i2c_speed,
+			i2c_reg->dab,
+			i2c_reg->reg,
+			i2c_reg->buffer,
+			i2c_reg->num_bytes,
+			0);
+		if (ret) {
+			EMGD_DEBUG("i2c read error.");
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* end igd_access_i2c() */
+
+/*!
+ *
+ * @param driver_handle A igd_driver_h type returned from a previous
+ * 	igd_init call.
+ * @param port_number
+ * @param edid_version
+ * @param edid_revision
+ * @param edid_size
+ *
+ * @return -IGD_ERROR_INVAL or -IGD_ERROR_EDID on failure
+ * @return 0 on success
+ */
+/* FIXME: Move to PI */
+static int igd_get_EDID_info(igd_driver_h driver_handle,
+	unsigned short port_number,
+	unsigned char *edid_version, unsigned char *edid_revision,
+	unsigned long *edid_size)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_port_t *port;
+	int                   ret;
+	unsigned char         temp_buf[25];
+	unsigned char         edid_1_3_header[] = {
+		0x00, 0xFF, 0xFF, 0xFF,
+		0xFF, 0xFF, 0xFF, 0x00
+	};
+
+	EMGD_TRACE_ENTER;
+
+	if (!driver_handle || !edid_version || !edid_revision || !edid_size) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	context->mod_dispatch.dsp_get_display(port_number, NULL, &port, 0);
+	if(!port) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Read EDID */
+	ret = context->mod_dispatch.i2c_read_regs(context,
+		port->ddc_reg,
+		10,              /* DDC speed 10 KHz */
+		port->ddc_dab,
+		0x00,             /* DDC Address */
+		temp_buf,         /* Read 20 bytes into temp_buf */
+		20,
+		0);
+	if (ret) {
+		return -IGD_ERROR_EDID;
+	}
+
+	/* Check for EDID version */
+	if (!OS_MEMCMP((void *)temp_buf, (void *)edid_1_3_header, 8)) {
+		*edid_version  = temp_buf[18];
+		*edid_revision = temp_buf[19];
+		*edid_size     = 128;
+
+		/* Read EDID byte 0x7E which gives the number of (optional) 128-byte
+		 * EDID extension blocks to follow. */
+		temp_buf[0] = 0;
+		ret = context->mod_dispatch.i2c_read_regs(context,
+			port->ddc_reg,
+			10,              /* DDC speed 10 KHz */
+			port->ddc_dab,
+			0x7E,             /* DDC Address */
+			&temp_buf[0],     /* Read 1 byte into temp_buf */
+			1,
+			0);
+		if (ret) {
+			return -IGD_ERROR_EDID;
+		}
+
+		*edid_size += (temp_buf[0] * 128);
+
+	} else {
+		*edid_version  = temp_buf[0] >> 4;
+		if (*edid_version != 2) {
+			return -IGD_ERROR_EDID;
+		}
+
+		*edid_revision = temp_buf[0] & 0x0F;
+		*edid_size     = 256;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end igd_get_EDID_info() */
+
+/*!
+ * This function sets enables or disables a specific port. Or, if
+ * the port number passed in is zero, it applies the change to all
+ * ports associated with the display handle.
+ *
+ * @param driver_handle display handle.
+ * @param port_number port number to enable or disable (or zero).
+ * @param flag IGD_ENABLE or IGD_DISABLE
+ * @param test IGD_TEST or zero
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+static int igd_enable_port(igd_display_h display_handle,
+		unsigned short port_number,
+		unsigned long flag,
+		unsigned long test)
+{
+	igd_display_context_t *display;
+	igd_display_port_t *port;
+	int i;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	display = (igd_display_context_t *)display_handle;
+
+	/*
+	 * There are a number of things that will probably need to be checked.
+	 *
+	 * When enabling a port, make sure the display has pipe timings.
+	 *
+	 * When enabling a port and display_detect is on, make sure a display
+	 * is detected before enabling the port.
+	 *
+	 * When enabling a port, check if it must be master, if so, the timings
+	 * will need to be changed.
+	 *
+	 * When disabling a port, don't disable the only port allocated to the
+	 * display.
+	 *
+	 * When disabling a port and it was the master, change timings to next
+	 * allocated port.
+	 */
+
+	if (!PIPE(display)->timing && (flag & IGD_DISPLAY_ENABLE)) {
+		EMGD_ERROR("port enable requested without pipe timings.");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((port_number > 0) && (port_number <= IGD_MAX_PORTS)) {
+		port = (igd_display_port_t *)display->port[port_number - 1];
+		if (port == NULL) {
+			return -IGD_ERROR_INVAL;
+		}
+
+		if (test == IGD_TEST) {
+			if ((port->pt_info->flags & IGD_DISPLAY_ENABLE) ==
+					(flag & IGD_DISPLAY_ENABLE)) {
+				return 0;
+			} else {
+				return -IGD_ERROR_INVAL;
+			}
+		}
+
+		if (flag & IGD_DISPLAY_ENABLE) {
+			/* Can we enable this port? */
+			if (dsp_display_connected(display->context, port)) {
+				port->pt_info->flags |= IGD_DISPLAY_ENABLE;
+			} else {
+				return -IGD_ERROR_INVAL;
+			}
+		} else {
+			port->pt_info->flags &= ~IGD_DISPLAY_ENABLE;
+		}
+	} else if ((port_number == 0) && !(flag & IGD_DISPLAY_ENABLE)) {
+		/* special case, disable all ports */
+		for (i = 0; i < IGD_MAX_PORTS; i++) {
+			if (display->port[i]) {
+				port = (igd_display_port_t *)display->port[i];
+				port->pt_info->flags &= ~IGD_DISPLAY_ENABLE;
+			}
+		}
+	}
+
+	/* Program ports to the proper enable/disable state */
+	for(i = 0; i < IGD_MAX_PORTS; i++) {
+		if ((port = (igd_display_port_t *)display->port[i]) != NULL) {
+			if (!port_number || (port->port_number == port_number)) {
+				if (port->pt_info->flags & IGD_DISPLAY_ENABLE) {
+					ret = mode_context->dispatch->program_port(display, i+1,
+						TRUE);
+				} else {
+					ret = mode_context->dispatch->program_port(display, i+1,
+						FALSE);
+				}
+				if (ret != 0) {
+					EMGD_ERROR("programming port %d failed", i+1);
+				}
+				if (port->pt_info->flags & IGD_DISPLAY_ENABLE) {
+					/* Call post_program_port if port is getting enabled. */
+					ret = mode_context->dispatch->post_program_port
+						(display, i+1, 0);
+				} else {
+					ret = 0;
+				}
+				if (ret != 0) {
+					EMGD_ERROR("post programming port %d failed", i+1);
+				}
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+
+/*!
+ * Free memory allocated to hold timing infomation. This must have been
+ * allocated using the igd_query_mode_list.
+ *
+ * @param mode_list
+ *
+ * @return void
+ */
+void igd_free_mode_list(igd_display_info_t *mode_list)
+{
+	if (mode_list) {
+		OS_FREE(mode_list);
+	}
+}
+
+/*!
+ * Generate a mode list that is correct for the given pipe master and
+ * any twins.
+ *
+ * @param driver_handle
+ * @param dc
+ * @param mode_list
+ * @param port
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int full_mode_query(igd_driver_h driver_handle,
+	unsigned long dc,
+	igd_display_info_t **mode_list,
+	igd_display_port_t *port)
+{
+	igd_timing_info_t *tt;
+	igd_timing_info_t *xt;
+	int timings = 0;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Looking up timings for port: %ld", port->port_number);
+
+	/* determine the size of the mode list including the extensions */
+	tt = port->timing_table;
+	if (!tt) {
+		EMGD_ERROR("igd_query_mode_list:  No Timings");
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+	while (tt->width != IGD_TIMING_TABLE_END) {
+		/*
+		 * Check here to see if this timing is valid on all other
+		 * ports. If it is, then we'll add it to the list, otherwise
+		 * it's skipped.
+		 */
+		if (tt->mode_info_flags & PD_MODE_SUPPORTED) {
+			timings++;
+			EMGD_DEBUG("Adding timing: (%dx%d)", tt->width, tt->height);
+		}
+		tt++;
+
+		/*
+		 * If reached the first table END, then check for timings
+		 * added by the user or EDID.
+		 */
+		if ((tt->width == IGD_TIMING_TABLE_END) && tt->extn_ptr) {
+			tt = tt->extn_ptr;
+		}
+	}
+	timings++; /* add one for the terminating marker */
+
+	*mode_list = OS_ALLOC(sizeof(igd_timing_info_t) * timings);
+	if (*mode_list != NULL) {
+		/* Now build the new mode list */
+		tt = port->timing_table;
+		xt = (igd_timing_info_t *)*mode_list;
+		while (tt->width != IGD_TIMING_TABLE_END) {
+			if (tt->mode_info_flags & PD_MODE_SUPPORTED) {
+				OS_MEMCPY(xt, tt, sizeof(igd_timing_info_t));
+				xt++;
+			}
+
+			tt++;
+			/*
+			 * If reached the first table END, then check for timings
+			 * added by the user or EDID.
+			 */
+			if ((tt->width == IGD_TIMING_TABLE_END) && tt->extn_ptr) {
+				tt = tt->extn_ptr;
+			}
+		}
+		/* copy end of list marker */
+		OS_MEMCPY(xt, tt, sizeof(igd_timing_info_t));
+	} else {
+		EMGD_ERROR("igd_query_mode_list: Memory allocation failure.");
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function is used to shutdown any module/dsp
+ * module specific structures or tables etc.
+ *
+ * @param context SS level igd_context.
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+static void mode_shutdown(igd_context_t *context)
+{
+	inter_module_dispatch_t *md;
+	module_state_h *mode_state = NULL;
+	unsigned long *flags = NULL;
+	reg_state_id_t id;
+
+	EMGD_DEBUG("mode_shutdown Entry");
+
+	/*
+	 * Disable all the displays in decending pipe order
+	 * Note: This isn't exactly device independent. It works for all
+	 * multipipe platforms that we know of but is limited to 2 pipes
+	 * and assumes that it is ok to disable pipe b before a.
+	 */
+
+	dsp_wait_rb(mode_context->context);
+
+	toggle_vblank_interrupts(FALSE);
+
+	/* Disable all VBlank interrupts: */
+	context->dispatch.disable_vblank_callback(ALL_PORT_CALLBACKS);
+
+	/* Reset all planes, pipe, ports to a known "off" state */
+	mode_context->dispatch->reset_plane_pipe_ports(context);
+
+	/* Shutdown dsp module */
+	context->mod_dispatch.dsp_shutdown(context);
+
+	/* Restore mode state */
+	md = &context->mod_dispatch;
+	if (config_drm.init) {
+		id = REG_MODE_STATE_CON;
+	} else {
+		id = REG_MODE_STATE_REG;
+	}
+	md->reg_get_mod_state(id, &mode_state, &flags);
+	mode_restore(context, mode_state, flags);
+
+	/* Shutdown PI module */
+	context->mod_dispatch.pi_shutdown(context);
+
+	/*
+	 * Do not clear mode_context pointer. It needs to stay around until
+	 *  the very last thing.
+	 *
+	 *	context->mode_context = NULL;
+	 */
+
+	EMGD_DEBUG("Return");
+	return;
+} /* end mode_shutdown() */
+
+/*!
+ * This function is used to shutdown any module/dsp
+ * module specific structures or tables etc.
+ *
+ * @param context SS level igd_context.
+ * @param mode_context mode module initialization parameters
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int full_mode_init(igd_context_t *context,
+	mode_context_t *mode_context)
+{
+	igd_dispatch_t     *dispatch = &context->dispatch;
+
+	/* Hook up the IGD dispatch table entires for mode */
+	dispatch->get_display = igd_get_display;
+	dispatch->pan_display = igd_pan_display;
+	dispatch->alter_cursor = igd_alter_cursor;
+	dispatch->alter_cursor_pos = igd_alter_cursor_pos;
+	dispatch->get_attrs = igd_get_attrs;
+	dispatch->set_attrs = igd_set_attrs;
+	dispatch->get_port_info = igd_get_port_info;
+	dispatch->access_i2c = igd_access_i2c;
+	dispatch->get_EDID_info = igd_get_EDID_info;
+	dispatch->free_mode_list = igd_free_mode_list;
+	dispatch->enable_port = igd_enable_port;
+
+	/* Hook up optional inter-module functions */
+	context->mod_dispatch.mode_save = mode_save;
+	context->mod_dispatch.mode_restore = mode_restore;
+	context->mod_dispatch.mode_pwr = mode_pwr;
+	context->mod_dispatch.mode_shutdown = mode_shutdown;
+	context->mod_dispatch.set_flip_pending =
+		mode_context->dispatch->full->set_flip_pending;
+	context->mod_dispatch.check_flip_pending =
+		mode_context->dispatch->full->check_flip_pending;
+	context->mod_dispatch.get_dd_timing =
+		mode_context->dispatch->get_dd_timing;
+	context->mod_dispatch.check_port_supported =
+		mode_context->dispatch->check_port_supported;
+	context->mod_dispatch.get_refresh_in_border =
+		mode_context->dispatch->get_refresh_in_border;
+
+
+	/* Hook up Core specific IGD dispatch table entries */
+	dispatch->set_palette_entry = mode_context->dispatch->set_palette_entry;
+	dispatch->get_scanline = mode_context->dispatch->full->get_scanline;
+	dispatch->wait_vsync = mode_context->dispatch->full->wait_vsync;
+	dispatch->query_in_vblank = mode_context->dispatch->full->query_in_vblank;
+	dispatch->get_surface = mode_context->dispatch->full->get_surface;
+	dispatch->set_surface = mode_context->dispatch->full->set_surface;
+	dispatch->query_event = mode_context->dispatch->full->query_event;
+	dispatch->register_vblank_callback =
+		mode_context->dispatch->full->register_vblank_callback;
+	dispatch->unregister_vblank_callback =
+		mode_context->dispatch->full->unregister_vblank_callback;
+	dispatch->enable_vblank_callback =
+		mode_context->dispatch->full->enable_vblank_callback;
+	dispatch->disable_vblank_callback =
+		mode_context->dispatch->full->disable_vblank_callback;
+
+	/* Assign the fw_info structure and Zero-out the contents */
+	mode_context->fw_info = &global_fw_info;
+	OS_MEMSET(mode_context->fw_info, 0, sizeof(fw_info_t));
+
+    /* Set the mode context quickboot options from the params */
+    mode_context->quickboot = context->mod_dispatch.init_params->quickboot;
+    mode_context->seamless = context->mod_dispatch.init_params->qb_seamless;
+    mode_context->video_input = context->mod_dispatch.init_params->qb_video_input;
+    mode_context->splash = context->mod_dispatch.init_params->qb_splash;
+
+	/* Just in case, disable all VBlank interrupts: */
+	dispatch->disable_vblank_callback(ALL_PORT_CALLBACKS);
+
+	return 0;
+} /* end full_mode_init() */
+
+/*!
+ * This function clears the framebutffer.
+ *
+ * @param mode_context mode module initialization parameters
+ * @param fb_info
+ * @param user_fb - the caller can provide an fb. If they do, then this
+ * function will not mem map it. If they provide this parameter as NULL,
+ * then we will go ahead and map one here.
+ *
+ * @return void
+ */
+void full_clear_fb(mode_context_t *mode_context,
+		igd_framebuffer_info_t *fb_info,
+		unsigned char *user_fb)
+{
+	unsigned short line, index;
+	unsigned char *fb;
+	volatile unsigned long *base_line_pitch;
+	volatile unsigned long pitch_value;
+
+	if(user_fb) {
+		fb = user_fb;
+	} else {
+		fb = mode_context->context->dispatch.gmm_map(
+				fb_info->fb_base_offset);
+		EMGD_DEBUG("After gmm_map(), fb = 0x%p", fb);
+	}
+	base_line_pitch = (unsigned long *)fb;
+	pitch_value = fb_info->screen_pitch / 4;
+
+	for (line = 0; line < fb_info->height; line++) {
+		for(index = 0; index < pitch_value; index++) {
+			base_line_pitch[index] = mode_context->display_color;
+		}
+		base_line_pitch += pitch_value;
+	}
+
+	if(!user_fb) {
+		mode_context->context->dispatch.gmm_unmap(fb);
+	}
+}
+
+/*!
+ * This is done in an attempt to re-use the plane and cursor allocated
+ * to a port when that port is moved to a new display handle.  The basic
+ * case is when the DC changes in this way:
+ *
+ *     0x00200058 -> 0x00000021
+ *     0x00000021 -> 0x00200058
+ *
+ * In both these cases, it is better if the plane and cursor are sticky
+ * to port 2.
+ *
+ * @param void
+ *
+ * @return void
+ */
+void swap_fb_cursor( void )
+{
+	igd_plane_t *display_plane1, *display_plane2;
+	igd_display_pipe_t *pipe1, *pipe2;
+	void *tmp;
+
+	mode_context->context->mod_dispatch.dsp_get_planes_pipes(
+		&display_plane1, &display_plane2,
+		&pipe1, &pipe2);
+
+	/* Swap the plane info data */
+	if (display_plane1 && display_plane2) {
+
+		tmp = display_plane1->plane_info;
+		display_plane1->plane_info = display_plane2->plane_info;
+		display_plane2->plane_info = tmp;
+	}
+
+	/* Swap the cursor info */
+	if (pipe1 && pipe2 && pipe1->cursor && pipe2->cursor) {
+
+		tmp = pipe1->cursor->cursor_info;
+		pipe1->cursor->cursor_info = pipe2->cursor->cursor_info;
+		pipe2->cursor->cursor_info = tmp;
+	}
+}
+
+/*!
+ * This function calculates target X and Y coordincates
+ * for a provided front buffer dimension after including
+ * corrections for render-scaling, rotation and flipping.
+ *
+ * @param rotation 0, 90, 180 or 270
+ * @param do_flip 0 or 1
+ * @param do_rscale 0 or 1
+ * @param x_rnd_scale fixed point int = (native_width  << 16) / fb_width
+ * @param y_rnd_scale fixed point int = (native_height << 16) / fb_height
+ * @param front_width front buffer width for this pipe
+ * @param front_height front buffer height for this pipe
+ * @param x
+ * @param y
+ * @param hotx
+ * @param hoty
+ *
+ * @return void
+ */
+void igd_fb_to_screen(unsigned short rotation,
+					unsigned char do_flip, unsigned char do_rscale,
+					unsigned long x_rnd_scale, unsigned long y_rnd_scale,
+					unsigned short front_width, unsigned short front_height,
+					unsigned short *x, unsigned short *y,
+					unsigned short hotx, unsigned short hoty)
+{
+	unsigned short x_temp;
+	unsigned short y_temp;
+
+	/*
+	 * we only need to up(or down)-scale the coordinates of
+	 * of the cursor when we are moving the cursor... dont care
+	 * about scaling the cursor image itself
+	 */
+	if(do_rscale){
+		unsigned long phys_x_pos_scale = x_rnd_scale;
+		unsigned long phys_y_pos_scale = y_rnd_scale;
+
+		if(rotation == 90 || rotation == 270) {
+			phys_x_pos_scale = y_rnd_scale;
+			phys_y_pos_scale = x_rnd_scale;
+		}
+
+		/* Added code to fix rounding error */
+		if(*x & 0x8000) {
+			/* if its a negative x_offset, we need to put in additional
+			 * type casting so the negative signage doesnt get scaled up
+			 * and down and end up being a very large positive number
+			 */
+			*x = (unsigned short)(-(short)((((unsigned long )
+				((short)-*x) * phys_x_pos_scale) + ((1<<15)-1)) >> 16));
+		} else {
+			*x = (unsigned short)((((unsigned long)*x * phys_x_pos_scale) + ((1<<15)-1)) >> 16);
+		}
+
+		if(*y & 0x8000) {
+			/* if its a negative y_offset, we need to put in additional
+			 * type casting so the negative signage doesnt get scaled up
+			 * and down and end up being a very large positive number
+			 */
+			*y = (unsigned short)(-(short)((((unsigned long )
+				((short)-*y) * phys_y_pos_scale) + ((1<<15)-1)) >> 16));
+		} else  {
+			*y = (unsigned short)((((unsigned long)*y * phys_y_pos_scale) + ((1<<15)-1)) >> 16);
+		}
+	}
+
+	*x -= hotx;
+	*y -= hoty;
+
+	x_temp = *x;
+	y_temp = *y;
+
+	switch(rotation) {
+	case 0:
+	default:
+		if(do_flip) {
+			*x = front_width-1 - x_temp;
+		}
+		break;
+	case 90:
+		*x = y_temp;
+		*y = (front_height - 1) - x_temp;
+		if(do_flip) {
+			*y = front_height-1 - *y ;
+		}
+		break;
+	case 180:
+		/* This is accurate for a 180 rotate */
+		*x = (front_width -1) -  x_temp;
+		*y = (front_height-1) - y_temp;
+		if(do_flip) {
+			*x = (front_width -1) - *x;
+		}
+		break;
+	case 270:
+		*x = (front_width - 1) - y_temp;
+		*y=  x_temp;
+		if(do_flip) {
+			*y = (front_height -1) - *y;
+		}
+		break;
+	}
+}
+
+
+
+/*!
+ * Compare the incoming dc, timing, fb with the one that
+ * the firmware has already programmed and see if seamless
+ * is possible.
+ *
+ * @param dc
+ * @param index 0 for primary and 1 for secondary
+ * @param pt
+ * @param pf
+ * @param flags Not used right now
+ *
+ * @return TRUE if seamless is possible
+ * @return FALSE if not possible
+ */
+int query_seamless(unsigned long dc,
+		int index,
+		igd_timing_info_t *pt,
+		igd_framebuffer_info_t *pf,
+		unsigned long flags)
+{
+	int ret = FALSE;
+	igd_display_info_t *timing;
+	igd_framebuffer_info_t *fb_info;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Incoming dc = 0x%08lx", dc);
+
+	/* Get the fw programmed DC from the inter-module data
+	 * structure
+     */
+	mode_context->fw_info->fw_dc =
+				 mode_context->context->mod_dispatch.dsp_fw_dc;
+
+	if(dc != mode_context->fw_info->fw_dc) {
+		/* DC doesn't match */
+		return FALSE;
+	}
+
+	/* Note: this test both overcomes a compiler warning, as well as a
+	 * potential kernel Oops from chasing a NULL pointer:
+	 */
+	if ((pt == NULL) || (pf == NULL)) {
+		return FALSE;
+	}
+	EMGD_DEBUG("Incoming Timing Width   = %hu", pt->width);
+	EMGD_DEBUG("Incoming Timing height  = %hu", pt->height);
+	EMGD_DEBUG("Incoming Timing Refresh = %hu", pt->refresh);
+
+	/* Check pipe Timings */
+	if(pt != NULL) {
+		timing = &mode_context->fw_info->timing_arr[index];
+		ret = FALSE;
+
+		if( (timing->width  == pt->width) &&
+			(timing->height == pt->height) )
+
+			/* Have to build in some tolerance here because the fresh rate may
+			 * not match exactly */
+			if (abs(timing->refresh - pt->refresh) <= 1) {
+
+			ret = TRUE;
+		}
+	}
+
+	EMGD_DEBUG(" ");
+	EMGD_DEBUG("Firmware Timing Width  = %hu", timing->width);
+	EMGD_DEBUG("Fimrware Timing Height = %hu", timing->height);
+	EMGD_DEBUG("Fimrware Timing Refesh = %hu", timing->refresh);
+	EMGD_DEBUG("-------------------------");
+
+	if(ret == FALSE) {
+		EMGD_DEBUG("Incoming Timings and Firmware Timings Do NOT match!");
+		EMGD_DEBUG("Seamless is NOT possible");
+		EMGD_TRACE_EXIT;
+		return ret;
+	}
+
+
+	EMGD_DEBUG("Incoming FB Width  = %u", pf->width);
+	EMGD_DEBUG("Incoming FB Height = %u", pf->height);
+	EMGD_DEBUG("Incoming FB Pitch  = %u", pf->screen_pitch);
+
+	/* Check Plane information */
+	if(pf != NULL) {
+		fb_info = &mode_context->fw_info->fb_info[index];
+		ret = FALSE;
+
+		if( (fb_info->screen_pitch != pf->screen_pitch) ||
+			(fb_info->width != pf->width) ||
+			(fb_info->height != pf->height) ) {
+
+			/* If width, height or pitch is different
+			 * Don't have to turn-off pipe, just update
+			 * the registers with the new values.
+			 * Later we just call program_plane to update
+			 * the registers.
+			 */
+			mode_context->fw_info->program_plane = 1;
+			ret = TRUE;
+		}
+
+	}
+
+	EMGD_DEBUG(" ");
+	EMGD_DEBUG("Firmware FB Width  = %u", fb_info->width);
+	EMGD_DEBUG("Firmware FB Height = %u", fb_info->height);
+	EMGD_DEBUG("Firmware FB Pitch  = %u", fb_info->screen_pitch);
+	EMGD_DEBUG("-----------------------");
+
+	EMGD_DEBUG("value of ret = %d", ret);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* end of query_seamless */
+
+
+/*!
+ * Takes inter-module function calls and calls mode-specific
+ * kms_match_mode.
+ *
+ * @param emgd_encoder
+ * @param fb_info
+ * @param timing
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int igd_kms_match_mode(void *emgd_encoder,
+    void *fb_info,
+	igd_timing_info_h **timing)
+{
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	ret = kms_match_mode((emgd_encoder_t *)emgd_encoder,
+		(igd_framebuffer_info_t *)fb_info,
+		(igd_timing_info_t **)timing);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*
+ * Name: toggle_vblank_interrupts
+ *
+ * Description:
+ * Requests/stops requesting for  vblank notification interrupts for ports 
+ * where blit-batching has been enabled. Called from mode_init, mode_pwr and 
+ * emgd_driver_pre_init to enable interrupts if needed, and from mode_pwr and
+ * mode_shutdown to disable the interrupts when shutting down the driver. 
+ *
+ * @param status 0 to disable and 1 to enable interrupts
+ *
+ * @return None
+ */
+void toggle_vblank_interrupts(bool status) {
+
+	unsigned long toggle_for;
+	unsigned char * mmio;
+	int i;
+
+	mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	for (i=0; i < IGD_MAX_PORTS; i++) {
+		if(mode_context->batch_blits[i]) {
+			toggle_for = 0;
+			if (i == IGD_PORT_TYPE_SDVOB - 1) {
+				toggle_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT2);
+			} else if (i == IGD_PORT_TYPE_LVDS - 1) {
+				toggle_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT4);
+			}
+
+			if (toggle_for) {
+
+				if (status) {
+					mode_context->dispatch->full->request_vblanks(toggle_for, mmio);
+				} else {
+					mode_context->dispatch->full->end_request(toggle_for, mmio);
+				}
+			}
+		}
+	}
+
+}
+
+/* 
+ * Notifies the user-space of a VBlank event by adding an event to the event
+ * queue of the DRM master file-descriptor
+ */
+void notify_userspace_vblank(struct drm_device *dev, int port)
+{
+
+	drm_emgd_priv_t *devpriv = dev->dev_private;
+	struct drm_pending_vblank_event *e = NULL;
+	struct timeval now;
+	unsigned long flags = 0;
+
+
+	EMGD_TRACE_ENTER;
+
+	if (!(devpriv->drm_master_fd)) {
+		EMGD_DEBUG("DRM master file-descriptor not set - exiting");
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	spin_lock_irqsave(&dev->event_lock,flags);
+
+	/* Check if there is space for new event object */
+	if (devpriv->drm_master_fd->event_space < sizeof(e->event)) {
+		spin_unlock_irqrestore(&dev->event_lock, flags);
+		EMGD_DEBUG("No space on file-descriptor event queue");
+		EMGD_TRACE_EXIT;
+		return;
+	}
+	devpriv->drm_master_fd->event_space -= sizeof(e->event);
+
+	spin_unlock_irqrestore(&dev->event_lock, flags);
+
+	e = OS_ALLOC(sizeof(struct drm_pending_vblank_event));
+	if (e == NULL) {
+		spin_lock_irqsave(&dev->event_lock, flags);
+		devpriv->drm_master_fd->event_space += sizeof(e->event);
+		spin_unlock_irqrestore(&dev->event_lock, flags);
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	/* Setup event struct */
+	OS_MEMSET(e, 0, sizeof(struct drm_pending_vblank_event));
+	e->event.base.type = DRM_EVENT_VBLANK;
+	e->event.base.length = sizeof(e->event);
+	e->event.user_data = port;
+	e->base.event = &e->event.base;
+	e->base.file_priv = devpriv->drm_master_fd;
+	e->base.destroy = (void (*)(struct drm_pending_event *))kfree;
+
+	do_gettimeofday(&now);
+	e->event.tv_sec = now.tv_sec;
+	e->event.tv_usec = now.tv_usec;
+
+	/* Add event to the event list */
+	list_add_tail(&e->base.link, &e->base.file_priv->event_list);
+	wake_up_interruptible(&e->base.file_priv->event_wait);
+
+	EMGD_TRACE_EXIT;
+
+}
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.c b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.c
new file mode 100755
index 0000000..4db71aa
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.c
@@ -0,0 +1,1347 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: match.c
+ * $Revision: 1.12 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+
+#define CURSOR_DEFAULT_WIDTH	64
+#define CURSOR_DEFAULT_HEIGHT	64
+
+#include <context.h>
+#include <igd_init.h>
+#include <io.h>
+#include <memory.h>
+#include <edid.h>
+#include <pi.h>
+
+#include <igd_mode.h>
+#include <igd_errno.h>
+
+#include <mode.h>
+#include <config.h>
+
+#include "match.h"
+
+
+#define MATCH_MOD(x)  ((x>0)?x:-x)
+#define MATCH_EXACT    0x01
+#define MATCH_NATIVE   0x02
+#define MATCH_CENTER   0x10
+#define MATCH_FOR_VGA  0x20
+
+extern igd_timing_info_t vga_timing_table[];
+extern igd_timing_info_t crt_timing_table[];
+static igd_timing_info_t scaled_timing[IGD_MAX_PIPES];
+
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/* Local variables */
+#ifndef CONFIG_MICRO
+igd_cursor_info_t default_cursor = {
+	CURSOR_DEFAULT_WIDTH,
+	CURSOR_DEFAULT_HEIGHT,
+	CONFIG_DEFAULT_PF,
+	0, 0, 0, 0, 0, 0,
+	0, 0, {0, 0, 0, 0}, IGD_CURSOR_ON, 0, 0, 0, 0
+};
+
+/*!
+ *
+ * @param cursor_info
+ * @param display
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+int validate_cursor(igd_cursor_info_t *cursor_info,
+	igd_display_context_t *display)
+{
+	unsigned long *list_pfs;
+	igd_display_pipe_t *pipe = (igd_display_pipe_t *)(display->pipe);
+
+	EMGD_TRACE_ENTER;
+	if (pipe) {
+		if (pipe->cursor) {
+			list_pfs = pipe->cursor->pixel_formats;
+
+			while (*list_pfs) {
+				if (cursor_info->pixel_format == *list_pfs) {
+					return 0;
+				}
+				list_pfs++;
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return -IGD_INVAL;
+}
+#endif
+
+/*!
+ *
+ * @param timing
+ * @param pt_info
+ *
+ * @return void
+ */
+static void fill_pt(
+	igd_timing_info_t *timing,
+	pigd_display_info_t pt_info)
+{
+	unsigned long flags;
+
+	EMGD_DEBUG("fill_pt Entry");
+
+	/* preserve existing pt_info flags */
+	flags = pt_info->flags;
+
+	/* Simply memcpy the structures and fix up the flags */
+	OS_MEMCPY(pt_info, timing, sizeof(igd_timing_info_t));
+
+	pt_info->flags |= flags;
+
+	/* pt_info doesn't require a IGD_MODE_VESA flag, so clear IGD_MODE_VESA
+	 * Setting this flag creates issues in match mode. */
+	pt_info->flags &= ~IGD_MODE_VESA;
+	return;
+}
+
+
+
+/*!
+ *
+ * @param emgd_encoder
+ * @param timing_table
+ * @param pt_info
+ * @param type
+ *
+ * @return NULL on failure
+ * @return timing on success
+ */
+igd_timing_info_t *kms_match_resolution(
+		emgd_encoder_t *emgd_encoder,
+		igd_timing_info_t *timing_table,
+		igd_display_info_t *pt_info,
+		int type)
+{
+	struct drm_device  *dev          = NULL;
+	igd_timing_info_t  *timing       = NULL;
+	igd_timing_info_t  *match        = NULL;
+	igd_timing_info_t  *native_match = NULL;
+	struct drm_encoder *encoder      = NULL;
+	igd_display_port_t *port         = NULL;
+	struct drm_crtc    *crtc         = NULL;
+	emgd_crtc_t        *emgd_crtc    = NULL;
+	igd_display_pipe_t *pipe         = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Width=%d, height=%d, refresh=%d mode_number=0x%x",
+		pt_info->width, pt_info->height, pt_info->refresh,
+		pt_info->mode_number);
+
+	encoder = &emgd_encoder->base;
+	dev = encoder->dev;
+	timing = timing_table;
+	port = emgd_encoder->igd_port;
+	match = NULL;
+
+	/*
+	 * Note on Native matching.
+	 * The Ideal thing is for a fp_native_dtd to already be marked as such.
+	 * If there is no native timing indicated then we must choose what is
+	 * most likely correct.
+	 * If the mode is not VGA then we should choose any DTD that closely
+	 * matches the mode being set. Failing that we should choose any timing
+	 * that closely matches the mode.
+	 * If the mode is VGA then we should take the current mode as it is
+	 * more likely correct.
+	 */
+	if(type == MATCH_NATIVE) {
+		if(port->fp_native_dtd) {
+			EMGD_DEBUG("Returning quick with a native match");
+
+			EMGD_DEBUG("NATIVE Width=%d, height=%d, refresh=%d mode_num=0x%x",
+				port->fp_native_dtd->width, port->fp_native_dtd->height,
+				port->fp_native_dtd->refresh, port->fp_native_dtd->mode_number);
+
+			return port->fp_native_dtd;
+		}
+		if((pt_info->flags & IGD_MODE_VESA) &&
+			(pt_info->mode_number <= 0x13)) {
+
+			list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+				if (crtc == encoder->crtc) {
+					emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+					pipe = emgd_crtc->igd_pipe;
+					if(pipe->timing) {
+						native_match = pipe->timing;
+					}
+				}
+			}
+		}
+	}
+
+	while (timing->width != IGD_TIMING_TABLE_END) {
+		if(!(timing->mode_info_flags & IGD_MODE_SUPPORTED)) {
+			timing++;
+			continue;
+		}
+
+		if(type == MATCH_NATIVE) {
+			if(timing->mode_info_flags & IGD_MODE_DTD_FP_NATIVE) {
+				port->fp_native_dtd = timing;
+				return timing;
+			}
+
+			if(port->fp_info) {
+				/*
+				 * We may have only fp_width and fp_height which is really
+				 * not enough information to be useful. If we find a
+				 * matching width and height we'll keep the first one while
+				 * still hoping to find an actual native mode later.
+				 */
+				if(!match &&
+					(port->fp_info->fp_width ==
+						(unsigned long)timing->width) &&
+					(port->fp_info->fp_height ==
+						(unsigned long)timing->height)) {
+					match = timing;
+				}
+			} else {
+				/*
+				 * Keep a match because in the event that we never find a
+				 * native DTD then we will just take the exact match.
+				 */
+				if(!match &&
+					(timing->width == pt_info->width) &&
+					(timing->height == pt_info->height) &&
+					(timing->refresh == pt_info->refresh)) {
+					match = timing;
+				}
+			}
+
+			/*
+			 * If it is a DTD then keep it only if it is better than any
+			 * found before.
+			 */
+			if(timing->mode_info_flags & IGD_MODE_DTD_USER) {
+				if(native_match) {
+					if(MATCH_MOD((int)(pt_info->width*pt_info->height) -
+							(native_match->width * native_match->height)) >
+						MATCH_MOD((int)(pt_info->width*pt_info->height) -
+							(timing->width*timing->height))) {
+						native_match = timing;
+					}
+				} else {
+					native_match = timing;
+				}
+			}
+		} else if (type == MATCH_EXACT) {
+			/*
+			 * Looking for an exact match. For VGA/VESA it must match
+			 * mode number. Otherwise it must match width, height, refresh
+			 * etc.
+			 */
+			if(pt_info->flags & IGD_MODE_VESA) {
+				/* ((timing->mode_info_flags & IGD_MODE_VESA)) */
+				if((pt_info->mode_number == timing->mode_number) &&
+					(!pt_info->refresh ||
+						(pt_info->refresh == timing->refresh))) {
+					match = timing;
+					break;
+				}
+			} else {
+				/* If exact match found, then break the loop */
+				if((timing->width == pt_info->width) &&
+					(timing->height == pt_info->height) &&
+					(timing->refresh == pt_info->refresh) &&
+					(
+						(timing->mode_info_flags &
+							(IGD_SCAN_INTERLACE|IGD_PIXEL_DOUBLE|
+								IGD_LINE_DOUBLE)) ==
+						(pt_info->flags &
+							(IGD_SCAN_INTERLACE|IGD_PIXEL_DOUBLE|
+								IGD_LINE_DOUBLE)))) {
+					match = timing;
+
+					/* If exact match found, then break the loop */
+					if ((timing->mode_info_flags & PD_MODE_DTD_USER) ||
+						(timing->mode_info_flags & PD_MODE_DTD)) {
+						break;
+					}
+				}
+			}
+		}
+
+
+		/* Center needs only to be bigger. Aspect ratio doesn't matter. */
+		/*
+		 * Note: The timings have to be big enough to fit the pt_info
+		 * including any pixel double flags. VGA modes will sometimes be
+		 * pixel doubled and need to be centered in a pipe that is double
+		 * in size.
+		 *
+		 * Note2: 720x400 VGA modes can be centered in 640x480 with a
+		 * special hardware config that drops every 9th pixel. Only do
+		 * this when requested.
+		 */
+		else if(type & MATCH_CENTER) {
+			unsigned short eff_width = pt_info->width;
+			unsigned short eff_height = pt_info->height;
+
+			if(type & MATCH_FOR_VGA) {
+				/*
+				 * 720x400 is a magic mode that means all VGA modes are supported
+				 * always use that mode for centering if found.
+				 */
+				if((timing->width == 720) && (timing->height == 400)) {
+					EMGD_DEBUG("Returning with a magic VGA mode");
+					return timing;
+				}
+				if(pt_info->flags & IGD_PIXEL_DOUBLE) {
+					eff_width *= 2;
+				}
+				if(pt_info->flags & IGD_LINE_DOUBLE) {
+					eff_height *= 2;
+				}
+				if((eff_width == 720) &&
+					(port->port_features & IGD_VGA_COMPRESS)) {
+					eff_width = 640;
+				}
+			}
+
+			if((timing->width >= eff_width) &&
+				(timing->height >= eff_height) &&
+				(timing->mode_info_flags & IGD_SCAN_INTERLACE) ==
+				(pt_info->flags & IGD_SCAN_INTERLACE)) {
+				if(match) {
+					/* Check for tighter fit */
+					if((match->width > timing->width) ||
+						(match->height > timing->height)) {
+						match = timing;
+					}
+					/* Try to match refreshrate as well */
+					if((match->width == timing->width) &&
+					   (match->height == timing->height) &&
+					   (pt_info->refresh == timing->refresh)){
+						match = timing;
+					}
+				} else {
+					match = timing;
+				}
+			}
+		}
+		timing++;
+	}
+
+	if(native_match) {
+		EMGD_DEBUG("Returning with a native match");
+		EMGD_DEBUG("Width=%d, height=%d, refresh=%d mode_number=0x%x",
+			native_match->width, native_match->height, native_match->refresh,
+			native_match->mode_number);
+		return native_match;
+	}
+	if (!match) {
+		EMGD_DEBUG("Returning with NO match");
+		return NULL;
+	}
+
+	EMGD_DEBUG("Returning with a match");
+	EMGD_DEBUG("Width=%d, height=%d, refresh=%d mode_number=0x%x",
+		match->width, match->height, match->refresh, match->mode_number);
+	return match;
+} /* end match_resolution */
+
+
+
+/*!
+ * Match the fb and pt structures to a Mode Structure from the table.
+ * When a mode is found update the input structures to reflect the
+ * values found.
+ *
+ * If the frambuffer is smaller than the timings requested, then we
+ * modify the timings so that the framebuffer will be centered.  That is,
+ * unless we are asked to upscale the framebuffer to fit the timings requested.
+ *
+ * In the case of LVDS both centering and scaling can happen. If the mode
+ * is in the list it will be scaled to the Native Timings. If the mode
+ * is not in the list (common or VGA) it will be centered in the next larger
+ * supported mode and then scaled to the native timings.
+ *
+ * Centering is always indicated by returning the timings that should be
+ * programmed to the pipe. The timings will then have their extension pointer
+ * set to point to the centered timings. For centering with scaling the
+ * first extension pointer will contain the scalable timings and the
+ * second will contain the centering timings. The static "scaled_timings"
+ * data structure will be used when the scaled timings need to be
+ * created on the fly due to a framebuffer that is smaller than the
+ * timings.
+ *
+ * FIXME: There is a lot of mentioning of VGA modes in this function from
+ * an earlier implementation of this feature.  The VGA-related code may
+ * not be relevant anymore if we are not building the VBIOS.
+ *
+ * @param emgd_encoder [IN]  Encoder expected to have a mode change
+ * @param fb_info      [IN]  Dimension of the FB to be displayed
+ * @param timing       [OUT] Best matched timing
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int kms_match_mode (
+	emgd_encoder_t *emgd_encoder,
+	igd_framebuffer_info_t *fb_info,
+	igd_timing_info_t **timing)
+{
+	struct drm_device  *dev;
+	struct drm_encoder *encoder;
+	igd_display_port_t *port;
+	igd_timing_info_t  *timing_table;
+	igd_timing_info_t  *exact_timing  = NULL;
+	igd_timing_info_t  *pipe_timing   = NULL;
+	igd_timing_info_t  *user_timing   = NULL;
+	igd_timing_info_t  *native_timing = NULL;
+	igd_timing_info_t  *vga_timing    = NULL;
+	igd_timing_info_t  *vesa_timing   = NULL;
+	igd_display_info_t *pt_info       = NULL;
+	struct drm_crtc    *crtc          = NULL;
+	emgd_crtc_t        *emgd_crtc     = NULL;
+	igd_display_pipe_t *pipe          = NULL;
+	short               cntr_dff_w    = 0;
+	short               cntr_dff_h    = 0;
+	unsigned long       upscale       = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	encoder      = &emgd_encoder->base;
+	dev          = encoder->dev;
+	port         = emgd_encoder->igd_port;
+	pt_info      = port->pt_info;
+	timing_table = port->timing_table;
+
+	if(!pt_info) {
+		EMGD_ERROR("NULL Port info detected, returning");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Check for default case */
+	if (!(pt_info->flags & IGD_MODE_VESA) &&
+		(pt_info->width == 0) && (pt_info->height == 0)) {
+		EMGD_DEBUG("Display Info width, height are zero, using default case");
+		pt_info->width  = CONFIG_DEFAULT_WIDTH;
+		pt_info->height = CONFIG_DEFAULT_HEIGHT;
+	}
+
+	EMGD_DEBUG("Checking for exact mode match");
+	exact_timing = kms_match_resolution(emgd_encoder, timing_table, pt_info,
+		MATCH_EXACT);
+
+	/*
+	 * At this point we have one of these cases:
+	 *  1) Found an exact match, VGA, VESA or other.
+	 *    -> Go check for FB centering and finish up.
+	 *  2) Found nothing
+	 *    -> Check for VGA/VESA mode to center.
+	 *    -> Check common modes.
+	 */
+	if(exact_timing) {
+		pipe_timing = exact_timing;
+		user_timing = exact_timing;
+		pipe_timing->extn_ptr = NULL;
+	} else {
+		/* No match found? Is it VGA? */
+		if( (pt_info->flags & IGD_MODE_VESA) &&
+			(pt_info->mode_number < 0x1D)    ){
+			EMGD_DEBUG("Checking for exact match in VGA table");
+			/* this only happens if it was a VGA mode number */
+			pt_info->refresh = 0;
+			vga_timing = kms_match_resolution(emgd_encoder, vga_timing_table,
+				pt_info, MATCH_EXACT);
+
+			if(!vga_timing) {
+				return -IGD_ERROR_INVAL;
+			}
+
+			vga_timing->extn_ptr = NULL;
+			/* We got something sane that needs to be centered */
+			user_timing = vga_timing;
+			fill_pt(vga_timing,pt_info);
+
+			/* continue at the bottom where we have
+			 * pipe_timing = NULL, so we will look
+			 * for centered timings for pt_info and
+			 * use cmn_vga_timings to tell kms_match_resolution
+			 * to take into account special VGA mode
+			 * centering regulations
+			 */
+		}
+	}
+
+	/* Find UPSCALING attr value
+	 * this PI func will not modify value of upscale if attr does not exist */
+	pi_pd_find_attr_and_value(port,
+			PD_ATTR_ID_PANEL_FIT,
+			0,/*no PD_FLAG for UPSCALING */
+			NULL, /* dont need the attr ptr*/
+			&upscale);
+
+
+	if(!pipe_timing){
+		/* At this point, one of 2 things has happenned:
+		 *      - we have a mode request that we could not match exactly.
+		 *        and it WASNT a VESA_MODE number request.
+		 *      - we have a request based on VESA_MODE number (maybe from
+		 *        VBIOS IAL) and we could not get a exact match from the
+		 *        port_timing_table, but we did get a match from the vga-
+		 *        timing_table.
+		 * In this case, there is one thing to do - MATCH_CENTER. Match
+		 * resolution will handle it this way:
+		 *      - if its VESA MODE number based, we only need to get
+		 *        the best (tightest) match if its VGA OR DONT match
+		 *        if its one of those magic timings
+		 *      - Else, we need to get the best (tightest) match, AND
+		 *        we need to center requested timings in that tightest fitting
+		 *        timing. But wait! This could mean if the requested pt_info
+		 *        is bigger than anything in the port timing table, we have
+		 *        no choice but to fail.
+		 */
+		unsigned char match_type = MATCH_CENTER;
+
+		EMGD_DEBUG("Checking for a safe centered match");
+		if(vga_timing) {
+			match_type |= MATCH_FOR_VGA;
+		} else if(pt_info->flags & IGD_MODE_VESA) {
+			/* if a vesa mode number was requested...
+			 * and we are centering that mode, we
+			 * need to get the common mode fb size
+			 * in case we need it later for VBIOS
+			 * which doesnt populate the FBInfo
+			 */
+			vesa_timing = kms_match_resolution(emgd_encoder, crt_timing_table,
+				pt_info, MATCH_EXACT);
+		}
+
+		if (upscale && vga_timing) {
+			/* If port supports upscaling and match is called for VGA,
+			 * then center vga mode resolution directly in the native mode
+			 * instead of centering VGA in another resolution */
+			pipe_timing = vga_timing;
+		} else {
+			pipe_timing = kms_match_resolution(emgd_encoder, timing_table,
+							pt_info, match_type);
+			/* This can happen if there is a spurious pt_info from IAL */
+			if (!pipe_timing) {
+				return -IGD_ERROR_INVAL;
+			}
+			pipe_timing->extn_ptr = vga_timing;
+			/* for the case of non VGA mode call,
+			 * at this point, vga_timing is NULL
+			 */
+		}
+
+		if(!vga_timing) {
+			user_timing = pipe_timing;
+		}
+	}
+
+	/*
+	 * At this point pipe_timing is what we are going to program the
+	 * pipe to roughly speaking. If there is a common timing then we
+	 * want it centered in the pipe_timing.
+	 *
+	 * If the framebuffer is smaller than the timings then we need to
+	 * generate a centered set of timings by copying the pipe timings
+	 * and shifting them a bit.
+	 *
+	 * If fb width and height are zero just assume that we want it to
+	 * match the timings and make up a pixel format. This is mostly because
+	 * VGA/VESA modes will just be set by number. We don't know their size
+	 * until we look up the number.
+	 */
+	if(fb_info) {
+		/*
+		 * fb_info is sometimes NULL when just testing something.
+		 */
+		if(!fb_info->pixel_format) {
+			/* Ugly VGA modes, it doesn't matter */
+			fb_info->pixel_format = IGD_PF_ARGB8_INDEXED;
+		}
+		if(!fb_info->width) {
+			if(vga_timing) {
+				fb_info->width = vga_timing->width;
+				fb_info->height = vga_timing->height;
+			} else {
+				if(!vesa_timing){
+					vesa_timing = pipe_timing;
+					/* in case vesa_timing is false set it to
+					 * pipe_timing so we dont need to check for
+					 * validity later, when increasing fb size for
+					 * VBIOS in clone mode (see 18 lines below)
+					 */
+				}
+				fb_info->width = vesa_timing->width;
+				fb_info->height = vesa_timing->height;
+			}
+		}
+
+		/*
+		 * VGA common timings are centered in pipe timings by hardware.
+		 * Otherwise we need to adjust the timings when centering is
+		 * needed.
+		 */
+		if (!vga_timing) {
+			/*
+			 * For VBIOS clone modes the FB should be the biggest mode
+			 * if this is the second match we may need to update the fb
+			 * data structure.
+			 */
+			if(fb_info->flags & IGD_VBIOS_FB) {
+				if ((fb_info->width < vesa_timing->width) ||
+					(fb_info->height < vesa_timing->height)) {
+					fb_info->width = vesa_timing->width;
+					fb_info->height = vesa_timing->height;
+				}
+			}
+
+
+			/* Do centering if fb is smaller than timing except on TV */
+			if ((fb_info->width < pipe_timing->width) ||
+				(fb_info->height < pipe_timing->height)) {
+				unsigned short temp_width = pipe_timing->width;
+				unsigned short temp_height = pipe_timing->height;
+				/* Normally, we should NOT be in here. All IALs only
+				 * are supposed to request for timings that ARE surely
+				 * supported by the HAL,... i.e. query the list of
+				 * supported timings by the port first!
+				 *
+				 * The exception would be if the IAL is purposely
+				 * asking for CENTERING!!! (pt_info's that were not
+				 * part of the supported mode list). This could indicate an
+				 * error or an explicit request for VESA centering!.
+				 */
+
+				/* let's use these 2 variables as flags... and do the
+				 * actual "centering" of the timings later since we do
+				 * also need to acomodate native timings as well
+				 */
+				/* NOTE: we could never be in here in fb_info was NULL */
+				cntr_dff_w = (pipe_timing->width  - fb_info->width)  / 2;
+				cntr_dff_h = (pipe_timing->height - fb_info->height) / 2;
+
+				/* Dont forget to use a different storage sice we dont
+				 * want to change the original (and to be used later)
+				 * ports mode list timings
+				 */
+				list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+					if (crtc == encoder->crtc) {
+						emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+						pipe = emgd_crtc->igd_pipe;
+
+						OS_MEMCPY(&scaled_timing[(pipe->pipe_num)],
+							pipe_timing, sizeof(igd_timing_info_t));
+						pipe_timing = &scaled_timing[(pipe->pipe_num)];
+					}
+				}
+
+				if(port->pd_type != PD_DISPLAY_TVOUT ) {
+					/* TV display don't like changed pipe actives,
+					 * Updating syncs work for TV centering */
+					if (fb_info->width < temp_width) {
+						pipe_timing->width = (unsigned short)fb_info->width;
+						pipe_timing->hblank_start -= cntr_dff_w;
+						pipe_timing->hblank_end   -= cntr_dff_w;
+					}
+
+					if (fb_info->height < temp_height) {
+						pipe_timing->height = (unsigned short)fb_info->height;
+						pipe_timing->vblank_start -= cntr_dff_h;
+						pipe_timing->vblank_end   -= cntr_dff_h;
+					}
+				}
+
+				if (fb_info->width < temp_width) {
+					pipe_timing->hsync_start -= cntr_dff_w;
+					pipe_timing->hsync_end   -= cntr_dff_w;
+				}
+
+				if (fb_info->height < temp_height) {
+					pipe_timing->vsync_start -= cntr_dff_h;
+					pipe_timing->vsync_end   -= cntr_dff_h;
+				}
+			}
+		}
+	}
+
+	if(upscale) {
+		/* Get the native timings */
+		EMGD_DEBUG("Checking for Native LVDS match for scaling");
+		native_timing = kms_match_resolution(emgd_encoder, timing_table,
+							pt_info, MATCH_NATIVE);
+		if(native_timing && (native_timing != pipe_timing)) {
+			native_timing->extn_ptr = pipe_timing;
+			pipe_timing = native_timing;
+		}
+	}
+
+	/*
+	 * Match mode returns as follows:
+	 * In case of VGA setmode:
+	 * 1) We will end up with either:
+	 *   magic->vga   ---   For displays supports native VGA
+	 *      or
+	 *   native->vga  ---   Upscaling displays
+	 *      or
+	 *   pipe->vga    ---   For other displays
+	 *
+	 * 2) In case of regular setmode:
+	 *   pipe         ---   For regular displays
+	 *      or
+	 *   native->vesa ---   Upscaling displays
+	 *
+	 *   Note: 1) Here "pipe" can be munged if centering is required.
+	 *         2) "vesa" is the requested mode, native is the native timing
+	 *            of the display.
+	 */
+
+	/*
+	 * Update Input Structures with values found
+	 * Note: This might not be what is going to be programmed. It is what
+	 * the user thinks they set. Scaling or centering could have altered
+	 * that.
+	 */
+	fill_pt(user_timing, pt_info);
+	*timing = pipe_timing;
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+
+
+/*!
+ *
+ * @param display
+ * @param timing_table
+ * @param pt_info
+ * @param type
+ *
+ * @return NULL on failure
+ * @return timing on success
+ */
+static igd_timing_info_t *match_resolution(
+		igd_display_context_t *display,
+		igd_timing_info_t *timing_table,
+		igd_display_info_t *pt_info,
+		int type)
+{
+	igd_timing_info_t *timing;
+	igd_timing_info_t *match;
+	igd_timing_info_t *native_match = NULL;
+	igd_display_port_t *port;
+
+	EMGD_DEBUG("Enter match_resolution");
+
+	EMGD_DEBUG("Width=%d, height=%d, refresh=%d mode_number=0x%x",
+		pt_info->width, pt_info->height, pt_info->refresh,
+		pt_info->mode_number);
+
+	timing = timing_table;
+	match = NULL;
+	port = PORT_OWNER(display);
+
+	/*
+	 * Note on Native matching.
+	 * The Ideal thing is for a fp_native_dtd to already be marked as such.
+	 * If there is no native timing indicated then we must choose what is
+	 * most likely correct.
+	 * If the mode is not VGA then we should choose any DTD that closely
+	 * matches the mode being set. Failing that we should choose any timing
+	 * that closely matches the mode.
+	 * If the mode is VGA then we should take the current mode as it is
+	 * more likely correct.
+	 */
+	if(type == MATCH_NATIVE) {
+		if(port->fp_native_dtd) {
+			EMGD_DEBUG("Returning quick with a native match");
+
+			EMGD_DEBUG("NATIVE Width=%d, height=%d, refresh=%d mode_num=0x%x",
+				port->fp_native_dtd->width, port->fp_native_dtd->height,
+				port->fp_native_dtd->refresh, port->fp_native_dtd->mode_number);
+
+			return port->fp_native_dtd;
+		}
+		if((pt_info->flags & IGD_MODE_VESA) &&
+			(pt_info->mode_number <= 0x13)) {
+			if(PIPE(display)->timing) {
+				native_match = PIPE(display)->timing;
+			}
+		}
+	}
+
+	while (timing->width != IGD_TIMING_TABLE_END) {
+		if(!(timing->mode_info_flags & IGD_MODE_SUPPORTED)) {
+			timing++;
+			continue;
+		}
+
+		if(type == MATCH_NATIVE) {
+			if(timing->mode_info_flags & IGD_MODE_DTD_FP_NATIVE) {
+				port->fp_native_dtd = timing;
+				return timing;
+			}
+
+			if(port->fp_info) {
+				/*
+				 * We may have only fp_width and fp_height which is really
+				 * not enough information to be useful. If we find a
+				 * matching width and height we'll keep the first one while
+				 * still hoping to find an actual native mode later.
+				 */
+				if(!match &&
+					(port->fp_info->fp_width ==
+						(unsigned long)timing->width) &&
+					(port->fp_info->fp_height ==
+						(unsigned long)timing->height)) {
+					match = timing;
+				}
+			} else {
+				/*
+				 * Keep a match because in the event that we never find a
+				 * native DTD then we will just take the exact match.
+				 */
+				if(!match &&
+					(timing->width == pt_info->width) &&
+					(timing->height == pt_info->height) &&
+					(timing->refresh == pt_info->refresh)) {
+					match = timing;
+				}
+			}
+
+			/*
+			 * If it is a DTD then keep it only if it is better than any
+			 * found before.
+			 */
+			if(timing->mode_info_flags & IGD_MODE_DTD_USER) {
+				if(native_match) {
+					if(MATCH_MOD((int)(pt_info->width*pt_info->height) -
+							(native_match->width * native_match->height)) >
+						MATCH_MOD((int)(pt_info->width*pt_info->height) -
+							(timing->width*timing->height))) {
+						native_match = timing;
+					}
+				} else {
+					native_match = timing;
+				}
+			}
+		} else if (type == MATCH_EXACT) {
+			/*
+			 * Looking for an exact match. For VGA/VESA it must match
+			 * mode number. Otherwise it must match width, height, refresh
+			 * etc.
+			 */
+			if(pt_info->flags & IGD_MODE_VESA) {
+				/* ((timing->mode_info_flags & IGD_MODE_VESA)) */
+				if((pt_info->mode_number == timing->mode_number) &&
+					(!pt_info->refresh ||
+						(pt_info->refresh == timing->refresh))) {
+					match = timing;
+					break;
+				}
+			} else {
+				/* If exact match found, then break the loop */
+				if((timing->width == pt_info->width) &&
+					(timing->height == pt_info->height) &&
+					(timing->refresh == pt_info->refresh) &&
+					(
+						(timing->mode_info_flags &
+							(IGD_SCAN_INTERLACE|IGD_PIXEL_DOUBLE|
+								IGD_LINE_DOUBLE)) ==
+						(pt_info->flags &
+							(IGD_SCAN_INTERLACE|IGD_PIXEL_DOUBLE|
+								IGD_LINE_DOUBLE)))) {
+					match = timing;
+
+					/* If exact match found, then break the loop */
+					if ((timing->mode_info_flags & PD_MODE_DTD_USER) ||
+						(timing->mode_info_flags & PD_MODE_DTD)) {
+						break;
+					}
+				}
+			}
+		}
+
+
+		/* Center needs only to be bigger. Aspect ratio doesn't matter. */
+		/*
+		 * Note: The timings have to be big enough to fit the pt_info
+		 * including any pixel double flags. VGA modes will sometimes be
+		 * pixel doubled and need to be centered in a pipe that is double
+		 * in size.
+		 *
+		 * Note2: 720x400 VGA modes can be centered in 640x480 with a
+		 * special hardware config that drops every 9th pixel. Only do
+		 * this when requested.
+		 */
+		else if(type & MATCH_CENTER) {
+			unsigned short eff_width = pt_info->width;
+			unsigned short eff_height = pt_info->height;
+
+			if(type & MATCH_FOR_VGA) {
+				/*
+				 * 720x400 is a magic mode that means all VGA modes are supported
+				 * always use that mode for centering if found.
+				 */
+				if((timing->width == 720) && (timing->height == 400)) {
+					EMGD_DEBUG("Returning with a magic VGA mode");
+					return timing;
+				}
+				if(pt_info->flags & IGD_PIXEL_DOUBLE) {
+					eff_width *= 2;
+				}
+				if(pt_info->flags & IGD_LINE_DOUBLE) {
+					eff_height *= 2;
+				}
+				if((eff_width == 720) &&
+					(port->port_features & IGD_VGA_COMPRESS)) {
+					eff_width = 640;
+				}
+			}
+
+			if((timing->width >= eff_width) &&
+				(timing->height >= eff_height) &&
+				(timing->mode_info_flags & IGD_SCAN_INTERLACE) ==
+				(pt_info->flags & IGD_SCAN_INTERLACE)) {
+				if(match) {
+					/* Check for tighter fit */
+					if((match->width > timing->width) ||
+						(match->height > timing->height)) {
+						match = timing;
+					}
+					/* Try to match refreshrate as well */
+					if((match->width == timing->width) &&
+					   (match->height == timing->height) &&
+					   (pt_info->refresh == timing->refresh)){
+						match = timing;
+					}
+				} else {
+					match = timing;
+				}
+			}
+		}
+		timing++;
+	}
+
+	if(native_match) {
+		EMGD_DEBUG("Returning with a native match");
+		EMGD_DEBUG("Width=%d, height=%d, refresh=%d mode_number=0x%x",
+			native_match->width, native_match->height, native_match->refresh,
+			native_match->mode_number);
+		return native_match;
+	}
+	if (!match) {
+		EMGD_DEBUG("Returning with NO match");
+		return NULL;
+	}
+
+	EMGD_DEBUG("Returning with a match");
+	EMGD_DEBUG("Width=%d, height=%d, refresh=%d mode_number=0x%x",
+		match->width, match->height, match->refresh, match->mode_number);
+	return match;
+} /* end match_resolution */
+
+
+
+/*!
+ * Match the fb and pt structures to a Mode Structure from the table.
+ * When a mode is found update the input structures to reflect the
+ * values found.
+ *
+ * Notes:
+ *  Match mode has several options for what it can do. Foremost it should
+ * attempt to find a mode matching the requested one from the timing table
+ * provided. If the mode requested is not in the list this means one of
+ * two things.
+ *   1) The IAL is calling without checking modes. It is just passing down
+ *  something that a user asked for. This is ok but we need to be safe so
+ *  we return the next smaller mode with the same aspect ratio.
+ *
+ *   2) The IAL is requesting a very common "required" mode even though the
+ *  port doesn't support it. In this case it should be in the static common
+ *  modes table and can be centered in the next larger timings in the
+ *  mode table.
+ *
+ * If the Frambuffer is smaller than the timings requested a fake set of
+ * centered timings is returned to program the pipe.
+ *
+ * In the case of VGA modes. If the mode is in the mode table everything is
+ * fine and we just return that. If it is not in the table we find the next
+ * larger suitable mode and prepare to center in that mode. Using the static
+ * timings from the VGA table as the VGA mode. We do not need to generate
+ * a fake set of timings because VGA will center itself automatically in
+ * hardware.
+ *
+ * In the case of LVDS both centering and scaling can happen. If the mode
+ * is in the list it will be scaled to the Native Timings. If the mode
+ * is not in the list (common or VGA) it will be centered in the next larger
+ * supported mode and then scaled to the native timings.
+ *
+ * Centering is always indicated by returning the timings that should be
+ * programmed to the pipe. The timings will then have their extension pointer
+ * set to point to the centered timings. For centering with scaling the
+ * first extension pointer will contain the scalable timings and the
+ * second will contain the centering timings. The static "scaled_timings"
+ * data structure will be used when the scaled timings need to be
+ * created on the fly due to a Framebuffer that is smaller than the
+ * timings.
+ *
+ * @param display
+ * @param timing_table
+ * @param fb_info
+ * @param pt_info
+ * @param timing
+ *
+ * @return -IGD_ERROR_INVAL on failure
+ * @return 0 on success
+ */
+int match_mode (
+	igd_display_context_t *display,
+	igd_timing_info_t *timing_table,
+	igd_framebuffer_info_t *fb_info,
+	igd_display_info_t *pt_info,
+	igd_timing_info_t **timing)
+{
+	igd_timing_info_t *exact_timing = NULL;
+	igd_timing_info_t *pipe_timing = NULL;
+	igd_timing_info_t *user_timing = NULL;
+	igd_timing_info_t *native_timing = NULL;
+	igd_timing_info_t *vga_timing = NULL;
+	igd_timing_info_t *vesa_timing = NULL;
+	short cntr_dff_w = 0;
+	short cntr_dff_h = 0;
+	unsigned long upscale = 0;
+
+	EMGD_DEBUG("Enter Match Mode");
+
+	if(!pt_info) {
+		EMGD_ERROR("NULL Port info detected, returning");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Check for default case */
+	if (!(pt_info->flags & IGD_MODE_VESA) &&
+		(pt_info->width == 0) && (pt_info->height == 0)) {
+		EMGD_DEBUG("Display Info width, height are zero, using default case");
+		pt_info->width = CONFIG_DEFAULT_WIDTH;
+		pt_info->height = CONFIG_DEFAULT_HEIGHT;
+	}
+
+	EMGD_DEBUG("Checking for exact mode match");
+	exact_timing = match_resolution(display, timing_table, pt_info,
+		MATCH_EXACT);
+	/*
+	 * At this point we have one of these cases:
+	 *  1) Found an exact match, VGA, VESA or other.
+	 *    -> Go check for FB centering and finish up.
+	 *  2) Found nothing
+	 *    -> Check for VGA/VESA mode to center.
+	 *    -> Check common modes.
+	 */
+	if(exact_timing) {
+		pipe_timing = exact_timing;
+		user_timing = exact_timing;
+		pipe_timing->extn_ptr = NULL;
+	} else {
+		/* No match found? Is it VGA? */
+		if( (pt_info->flags & IGD_MODE_VESA) &&
+			(pt_info->mode_number < 0x1D)    ){
+			EMGD_DEBUG("Checking for exact match in VGA table");
+			/* this only happens if it was a VGA mode number */
+			pt_info->refresh = 0;
+			vga_timing = match_resolution(display, vga_timing_table,
+				pt_info, MATCH_EXACT);
+
+			if(!vga_timing) {
+				return -IGD_ERROR_INVAL;
+			}
+
+			vga_timing->extn_ptr = NULL;
+			/* We got something sane that needs to be centered */
+			user_timing = vga_timing;
+			fill_pt(vga_timing,pt_info);
+
+			/* continue at the bottom where we have
+			 * pipe_timing = NULL, so we will look
+			 * for centered timings for pt_info and
+			 * use cmn_vga_timings to tell match_resolution
+			 * to take into account special VGA mode
+			 * centering regulations
+			 */
+		}
+	}
+
+	/* Find UPSCALING attr value*/
+	pi_pd_find_attr_and_value(PORT_OWNER(display),
+			PD_ATTR_ID_PANEL_FIT,
+			0,/*no PD_FLAG for UPSCALING */
+			NULL, /* dont need the attr ptr*/
+			&upscale);
+	/* this PI func will not modify value of upscale if attr does not exist */
+
+	if(!pipe_timing){
+		/* At this point, one of 2 things has happenned:
+		 *      - we have a mode request that we could not match exactly.
+		 *        and it WASNT a VESA_MODE number request.
+		 *      - we have a request based on VESA_MODE number (maybe from
+		 *        VBIOS IAL) and we could not get a exact match from the
+		 *        port_timing_table, but we did get a match from the vga-
+		 *        timing_table.
+		 * In this case, there is one thing to do - MATCH_CENTER. Match
+		 * resolution will handle it this way:
+		 *      - if its VESA MODE number based, we only need to get
+		 *        the best (tightest) match if its VGA OR DONT match
+		 *        if its one of those magic timings
+		 *      - Else, we need to get the best (tightest) match, AND
+		 *        we need to center requested timings in that tightest fitting
+		 *        timing. But wait! This could mean if the requested pt_info
+		 *        is bigger than anything in the port timing table, we have
+		 *        no choice but to fail.
+		 */
+		unsigned char match_type = MATCH_CENTER;
+
+		EMGD_DEBUG("Checking for a safe centered match");
+		if(vga_timing) {
+			match_type |= MATCH_FOR_VGA;
+		} else if(pt_info->flags & IGD_MODE_VESA) {
+			/* if a vesa mode number was requested...
+			 * and we are centering that mode, we
+			 * need to get the common mode fb size
+			 * in case we need it later for VBIOS
+			 * which doesnt populate the FBInfo
+			 */
+			vesa_timing = match_resolution(display, crt_timing_table,
+				pt_info, MATCH_EXACT);
+		}
+
+		if (upscale && vga_timing) {
+			/* If port supports upscaling and match is called for VGA,
+			 * then center vga mode resolution directly in the native mode
+			 * instead of centering VGA in another resolution */
+			pipe_timing = vga_timing;
+		} else {
+			pipe_timing = match_resolution(display, timing_table, pt_info,
+				match_type);
+			/* This can happen if there is a spurious pt_info from IAL */
+			if (!pipe_timing) {
+				return -IGD_ERROR_INVAL;
+			}
+			pipe_timing->extn_ptr = vga_timing;
+			/* for the case of non VGA mode call,
+			 * at this point, vga_timing is NULL
+			 */
+		}
+
+		if(!vga_timing) {
+			user_timing = pipe_timing;
+		}
+	}
+
+	/*
+	 * At this point pipe_timing is what we are going to program the
+	 * pipe to roughly speaking. If there is a common timing then we
+	 * want it centered in the pipe_timing.
+	 *
+	 * If the framebuffer is smaller than the timings then we need to
+	 * generate a centered set of timings by copying the pipe timings
+	 * and shifting them a bit.
+	 *
+	 * If fb width and height are zero just assume that we want it to
+	 * match the timings and make up a pixel format. This is mostly because
+	 * VGA/VESA modes will just be set by number. We don't know their size
+	 * until we look up the number.
+	 */
+	if(fb_info) {
+		/*
+		 * fb_info is sometimes NULL when just testing something.
+		 */
+		if(!fb_info->pixel_format) {
+			/* Ugly VGA modes, it doesn't matter */
+			fb_info->pixel_format = IGD_PF_ARGB8_INDEXED;
+		}
+		if(!fb_info->width) {
+			if(vga_timing) {
+				fb_info->width = vga_timing->width;
+				fb_info->height = vga_timing->height;
+			} else {
+				if(!vesa_timing){
+					vesa_timing = pipe_timing;
+					/* in case vesa_timing is false set it to
+					 * pipe_timing so we dont need to check for
+					 * validity later, when increasing fb size for
+					 * VBIOS in clone mode (see 18 lines below)
+					 */
+				}
+				fb_info->width = vesa_timing->width;
+				fb_info->height = vesa_timing->height;
+			}
+		}
+
+		/*
+		 * VGA common timings are centered in pipe timings by hardware.
+		 * Otherwise we need to adjust the timings when centering is
+		 * needed.
+		 */
+		if (!vga_timing) {
+			/*
+			 * For VBIOS clone modes the FB should be the biggest mode
+			 * if this is the second match we may need to update the fb
+			 * data structure.
+			 */
+			if(fb_info->flags & IGD_VBIOS_FB) {
+				if ((fb_info->width < vesa_timing->width) ||
+					(fb_info->height < vesa_timing->height)) {
+					fb_info->width = vesa_timing->width;
+					fb_info->height = vesa_timing->height;
+				}
+			}
+
+
+			/* Do centering if fb is smaller than timing except on TV */
+			if ((fb_info->width < pipe_timing->width) ||
+				(fb_info->height < pipe_timing->height)) {
+				unsigned short temp_width = pipe_timing->width;
+				unsigned short temp_height = pipe_timing->height;
+				/* Normally, we should NOT be in here. All IALs only
+				 * are supposed to request for timings that ARE surely
+				 * supported by the HAL,... i.e. query the list of
+				 * supported timings by the port first!
+				 *
+				 * The exception would be if the IAL is purposely
+				 * asking for CENTERING!!! (pt_info's that were not
+				 * part of the supported mode list). This could indicate an
+				 * error or an explicit request for VESA centering!.
+				 */
+
+				/* let's use these 2 variables as flags... and do the
+				 * actual "centering" of the timings later since we do
+				 * also need to acomodate native timings as well
+				 */
+				/* NOTE: we could never be in here in fb_info was NULL */
+				cntr_dff_w = (pipe_timing->width - fb_info->width) / 2;
+				cntr_dff_h = (pipe_timing->height - fb_info->height) / 2;
+
+				/* Dont forget to use a different storage sice we dont
+				 * want to change the original (and to be used later)
+				 * ports mode list timings
+				 */
+				OS_MEMCPY(&scaled_timing[(PIPE(display)->pipe_num)],
+					pipe_timing,
+					sizeof(igd_timing_info_t));
+
+				pipe_timing = &scaled_timing[(PIPE(display)->pipe_num)];
+
+				if(PORT_OWNER(display)->pd_type != PD_DISPLAY_TVOUT ) {
+					/* TV display don't like changed pipe actives,
+					 * Updating syncs work for TV centering */
+					if (fb_info->width < temp_width) {
+						pipe_timing->width = (unsigned short)fb_info->width;
+						pipe_timing->hblank_start -= cntr_dff_w;
+						pipe_timing->hblank_end -= cntr_dff_w;
+					}
+
+					if (fb_info->height < temp_height) {
+						pipe_timing->height = (unsigned short)fb_info->height;
+						pipe_timing->vblank_start -= cntr_dff_h;
+						pipe_timing->vblank_end -= cntr_dff_h;
+					}
+				}
+
+				if (fb_info->width < temp_width) {
+					pipe_timing->hsync_start -= cntr_dff_w;
+					pipe_timing->hsync_end -= cntr_dff_w;
+				}
+
+				if (fb_info->height < temp_height) {
+					pipe_timing->vsync_start -= cntr_dff_h;
+					pipe_timing->vsync_end -= cntr_dff_h;
+				}
+			}
+		}
+	}
+
+	if(upscale) {
+		/* Get the native timings */
+		EMGD_DEBUG("Checking for Native LVDS match for scaling");
+		native_timing = match_resolution(display, timing_table, pt_info,
+			MATCH_NATIVE);
+		if(native_timing && (native_timing != pipe_timing)) {
+			native_timing->extn_ptr = pipe_timing;
+			pipe_timing = native_timing;
+		}
+	}
+
+	/*
+	 * Match mode returns as follows:
+	 * In case of VGA setmode:
+	 * 1) We will end up with either:
+	 *   magic->vga   ---   For displays supports native VGA
+	 *      or
+	 *   native->vga  ---   Upscaling displays
+	 *      or
+	 *   pipe->vga    ---   For other displays
+	 *
+	 * 2) In case of regular setmode:
+	 *   pipe         ---   For regular displays
+	 *      or
+	 *   native->vesa ---   Upscaling displays
+	 *
+	 *   Note: 1) Here "pipe" can be munged if centering is required.
+	 *         2) "vesa" is the requested mode, native is the native timing
+	 *            of the display.
+	 */
+
+	/*
+	 * Update Input Structures with values found
+	 * Note: This might not be what is going to be programmed. It is what
+	 * the user thinks they set. Scaling or centering could have altered
+	 * that.
+	 */
+	fill_pt(user_timing, pt_info);
+	*timing = pipe_timing;
+	EMGD_DEBUG("Return");
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.h b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.h
new file mode 100755
index 0000000..40a6721
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/match.h
@@ -0,0 +1,59 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: match.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the mode matching algorithms
+ *-----------------------------------------------------------------------------
+ */
+
+#include <mode.h>
+
+int validate_fb(
+	pigd_framebuffer_info_t fb_info,
+	igd_display_plane_t *plane);
+
+int validate_cursor(
+	igd_cursor_info_t *cursor_info,
+	igd_display_context_t *display);
+
+int match_mode (
+	igd_display_context_t *display,
+	igd_timing_info_t *timing_table,
+	igd_framebuffer_info_t *fb_info,
+	igd_display_info_t *pt_info,
+	igd_timing_info_t **timing);
+
+int kms_match_mode (
+	emgd_encoder_t *emgd_encoder,
+	igd_framebuffer_info_t *fb_info,
+	igd_timing_info_t **timing);
+
+igd_timing_info_t *kms_match_resolution(
+	emgd_encoder_t *emgd_encoder,
+	igd_timing_info_t *timing_table,
+	igd_display_info_t *pt_info,
+	int type);
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/cmn/micro_mode.c b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/micro_mode.c
new file mode 100644
index 0000000..5d615aa
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/micro_mode.c
@@ -0,0 +1,1767 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_mode.c
+ * $Revision: 1.32 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Contains client interface support functions for display allocations
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <config.h>
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd.h>
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_render.h>
+#include <igd_pwr.h>
+#include <igd_errno.h>
+#include <igd_gmm.h>
+#include <igd_pd.h>
+
+#include <debug.h>
+#include <context.h>
+#include <rb.h>
+#include <pd.h>
+#include <intelpci.h>
+#include <dsp.h>
+#include <pi.h>
+#include <pd_init.h>
+#include <dispatch.h>
+#include <mode.h>
+#include <mode_access.h>
+#include <dsp.h>
+#include <utils.h>
+#include <general.h>
+#include <module_init.h>
+
+#include "drm_emgd_private.h"
+#include "match.h"
+#include "mode_dispatch.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+#ifndef CONFIG_MICRO
+#define TIMING_CHANGED(a,b,c,d,e,f,g) timing_changed(a,b,c,d,e,f,g)
+#define CALCULATE_ELD_INFOFRAMES
+#else
+#define TIMING_CHANGED(a,b,c,d,e,f,g) 1
+#endif /* CONFIG_MICRO */
+
+#define MODE_MIN(x, y) (x<y)?x:y
+
+extern emgd_drm_config_t config_drm;
+
+/*
+ * NOTE: Do not add comma's to this dispatch table. The macro's
+ * remove the entire entry.
+ */
+static dispatch_table_t mode_dispatch[] = {
+	DISPATCH_PLB( &mode_dispatch_plb )
+	DISPATCH_TNC( &mode_dispatch_tnc )
+	DISPATCH_END
+};
+
+
+
+/*
+ * Kernel Mode Setting (KMS) dispatch table
+ */
+static dispatch_table_t mode_kms_dispatch[] = {
+	DISPATCH_PLB( &mode_kms_dispatch_plb )
+	DISPATCH_TNC( &mode_kms_dispatch_tnc )
+	DISPATCH_END
+};
+
+
+
+/*
+ * Do not malloc the context for two reasons.
+ *  1) vBIOS needs to minimize mallocs
+ *  2) Mode context needs to stay around until after all modules are
+ *    shut down for the register restore functionality.
+*/
+mode_context_t mode_context[1];
+
+
+#ifndef CONFIG_MICRO
+
+/*!
+ * Check user supplied timing against what is currently
+ * programmed to see if it needs to change.  If the timing
+ * isn't going to change, then we don't want to turn off and
+ * reprogram the port.
+ *
+ * NOTE: If this increases the size too much for vbios, then it
+ * could be ifndef CONFIG_MICRO with an else that returns 1. That
+ * would mean vbios wouldn't have these checks and always assume
+ * the timing was changing.
+ *
+ * @param display
+ * @param new_dc DC changes always require port/plane/pipe programming.
+ * @param pt_info User supplied timing info to check.
+ * @param fb_info User supplied framebuffer info.
+ * @param port_number
+ * @param display_mask Which display is being checked.
+ * @param alter flags used to force the alter
+ *
+ * @return 1 timing has changed
+ * @return 0 timing hasn't changed
+ */
+static int timing_changed(igd_display_context_t *display,
+		unsigned long new_dc,
+		unsigned long dc,
+		igd_display_info_t *pt_info,
+		igd_framebuffer_info_t *fb_info,
+		unsigned long display_mask,
+		unsigned long flags)
+{
+	igd_framebuffer_info_t *cfb;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * will this cover every case?  Can a change on one display effect
+	 * the timings of the other?  I don't think so.
+	 */
+	if ((new_dc & display_mask) != (dc & display_mask)) {
+		return 1; /* Ports are changing on this display, must re-program. */
+	}
+
+	/*
+	 * Make sure the owner port has a pt_info, if not then we'll assume
+	 * that it hasn't been programmed yet and thus the timings are going
+	 * to change.
+	 */
+	if (PORT_OWNER(display)->pt_info == NULL) {
+		return 1;
+	}
+
+	/*
+	 * Make sure we have valid timing info.  If not, then don't try
+	 * and change the timings.
+	 */
+	if (!pt_info || !fb_info) {
+		return 0;
+	}
+
+	/*
+	 * If the caller really wants to re-program the planes/pipes/ports
+	 * then do it
+	 */
+
+	if (flags & IGD_FORCE_ALTER) {
+		return 1;
+	}
+
+	/*
+	 * Check only width, height, refresh. If these don't match, then we
+	 * know that something is changing.
+	 */
+
+	if ((pt_info->width == PORT_OWNER(display)->pt_info->width) &&
+			(pt_info->height == PORT_OWNER(display)->pt_info->height) &&
+			(pt_info->refresh == PORT_OWNER(display)->pt_info->refresh)) {
+
+		/* Check framebuffer for changes, fb changes may change timing */
+		if ((cfb = PLANE(display)->fb_info) != NULL) {
+			if ((cfb->width != fb_info->width) ||
+				(cfb->height != fb_info->height) ||
+				(cfb->pixel_format != fb_info->pixel_format) ||
+				(cfb->flags != fb_info->flags)) {
+				/* Timing ok buf fb_info doesn't match */
+				return 1;
+			}
+		}
+	} else {
+		/* Timing doesn't match */
+		return 1;
+	}
+
+	/* Timing and fb have not changed */
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+#endif
+
+/*!
+ * Update internal data structures for the plane, pipe, and port as
+ * requested. Allocate a new framebuffer if the new parameters do not
+ * match the existing framebuffer.
+ *
+ * @param display
+ * @param port_number
+ * @param timing
+ * @param pt_info User supplied timing info to check.
+ * @param fb_info User supplied framebuffer info.
+ * @param flags
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int mode_update_plane_pipe_ports(
+	igd_display_context_t *display,
+	unsigned short port_number,
+	igd_timing_info_t *timing,
+	igd_framebuffer_info_t *fb_info,
+	igd_display_info_t *pt_info,
+	unsigned long flags)
+{
+	int ret;
+	int alloc_fb;
+	unsigned long size = 0;
+	igd_framebuffer_info_t *plane_fb_info;
+	igd_display_plane_t *mirror;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Port Number (%d)", port_number);
+
+	EMGD_ASSERT( (fb_info || pt_info), "ERROR: fb_info & pt_info are NULL",
+		-IGD_ERROR_INVAL);
+
+	EMGD_ASSERT( PLANE(display)->fb_info, "ERROR: fb_info in plane is NULL",
+		-IGD_ERROR_INVAL);
+
+	plane_fb_info = PLANE(display)->fb_info;
+	mirror = PLANE(display)->mirror;
+
+	/*
+	 * If there is a mirror plane (for Clone) and the mirror is populated
+	 * then update our plane from the mirror. If the mirror is not populated
+	 * then update the mirror from ours.
+	 */
+	if (mirror) {
+		if(mirror->fb_info->flags) {
+			OS_MEMCPY(plane_fb_info, mirror->fb_info,
+				sizeof(igd_framebuffer_info_t));
+		} else {
+			OS_MEMCPY(mirror->fb_info, plane_fb_info,
+				sizeof(igd_framebuffer_info_t));
+		}
+	}
+
+	if (PORT(display, port_number)->pt_info == NULL) {
+		if ((PORT(display, port_number)->pt_info = (igd_display_info_t *)
+				OS_ALLOC(sizeof(igd_display_info_t))) == NULL) {
+			EMGD_ERROR_EXIT("unable to alloc a pt_info struct in pipe.");
+			return -IGD_ERROR_INVAL;
+		}
+	}
+
+	/*
+	 * If the fb_info was provided, and either we were asked to update
+	 * the internal structures via the flags, or we are allocating a new
+	 * framebuffer.
+	 */
+	if(fb_info && (flags & MODE_UPDATE_PLANE)) {
+
+		/* Assume we will be allocating a FB */
+		alloc_fb = 1;
+
+		/* If the frambuffer parameters are unchanged then do not re-alloc */
+		if((fb_info->width == plane_fb_info->width) &&
+			(fb_info->height == plane_fb_info->height) &&
+			(fb_info->pixel_format == plane_fb_info->pixel_format) &&
+			(fb_info->flags == plane_fb_info->flags)) {
+			alloc_fb = 0;
+		}
+
+		/* Do not re-alloc a framebuffer if the re-use flag is set. */
+		if(fb_info->flags & IGD_REUSE_FB) {
+			alloc_fb = 0;
+			/* May need to get the MIN_PITCH flags */
+			plane_fb_info->flags = (fb_info->flags & IGD_FB_FLAGS_MASK) |
+				(plane_fb_info->flags & ~IGD_FB_FLAGS_MASK);
+		}
+
+		/*
+		 * If we don't have a framebuffer at all then we MUST allocate
+		 * one.
+		 */
+		if(!plane_fb_info->allocated && !fb_info->allocated) {
+			alloc_fb = 1;
+		}
+
+		EMGD_DEBUG("plane_fb_info->fb_base_offset = 0x%08lx",
+			plane_fb_info->fb_base_offset);
+		if(alloc_fb) {
+			if(plane_fb_info->allocated) {
+				/* Free frame buffer memory */
+				display->context->dispatch.gmm_free(
+					plane_fb_info->fb_base_offset);
+				plane_fb_info->allocated = 0;
+			}
+
+			fb_info->fb_base_offset = plane_fb_info->fb_base_offset;
+			/*
+			 * Keep the FB flags, add in Displayable flag and blank out
+			 * the rest. This insures that any tiled or usage flags from an
+			 * earlier call do not get reused.
+			 */
+			fb_info->flags = (fb_info->flags & IGD_FB_FLAGS_MASK) |
+				IGD_SURFACE_DISPLAY;
+
+			/*
+			 * Framebuffer allocations must always come from a reservation
+			 * if the IAL changes the address the new address must also be
+			 * from a reservation.
+			 */
+			GMM_SET_DEBUG_NAME("Framebuffer");
+			ret = display->context->dispatch.gmm_alloc_surface(
+				&fb_info->fb_base_offset,
+				fb_info->pixel_format,
+				&fb_info->width,
+				&fb_info->height,
+				&fb_info->screen_pitch,
+				&size,
+				IGD_GMM_ALLOC_TYPE_RESERVATION,
+				&fb_info->flags);
+			if(ret) {
+				EMGD_ERROR_EXIT("Allocation of Front buffer failed: %d", ret);
+				return ret;
+			}
+			fb_info->allocated = 1;
+			/* Set the visible offset to the newly-allocated offset: */
+			fb_info->visible_offset = fb_info->fb_base_offset;
+		} else {
+			/* If not reallocating, use back the offset in plane_fb_info */
+			fb_info->fb_base_offset = plane_fb_info->fb_base_offset;
+			 /* We must set the visible offset and screen pitch
+			  * to proper value if we use back the plane.
+			  */
+                        fb_info->visible_offset = fb_info->fb_base_offset;
+                        fb_info->screen_pitch = plane_fb_info->screen_pitch;
+		}
+
+		OS_MEMCPY(plane_fb_info, fb_info, sizeof(igd_framebuffer_info_t));
+		plane_fb_info->allocated = 1;
+		EMGD_DEBUG("plane_fb_info->fb_base_offset = 0x%08lx",
+			plane_fb_info->fb_base_offset);
+
+	}
+
+	if(timing && (flags & MODE_UPDATE_PIPE)) {
+		EMGD_DEBUG("Updating pipe timing.");
+		PIPE(display)->timing = timing;
+		PIPE(display)->owner = display;
+	}
+
+	if(pt_info && (flags & MODE_UPDATE_PORT)) {
+		EMGD_DEBUG("OLD_PT========NEW PT ");
+		IGD_PRINTK_PTINFO_2(PORT(display, port_number)->pt_info, pt_info);
+		OS_MEMCPY(PORT(display, port_number)->pt_info, pt_info,
+				sizeof(igd_display_info_t));
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end mode_update_plane_pipe_ports() */
+
+#ifdef CALCULATE_ELD_INFOFRAMES
+
+/*!
+ * Calculates infoframes information top be used by HDMI port drivers
+ *
+ * @param port
+ * @param timing_info
+ * @param temp_cea
+ *
+ * @return 0
+ */
+static int calculate_infoframes(
+	igd_display_port_t *port,
+	igd_timing_info_t *timing_info,
+	cea_extension_t *temp_cea)
+{
+
+	pd_timing_t     *cea_timings = NULL, *cea_timing_temp = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	/* VBIOS has no access to CEA timing tables and this is not supported
+	   there as well */
+	if(timing_info->mode_info_flags & PD_MODE_CEA){
+		if(timing_info->width != 640 && timing_info->height != 480){
+			port->edid->cea->quantization	= HDMI_QUANTIZATION_RGB_220;
+		}
+
+		/* Based on DPG algorithm. If monitors support more than 2 channels
+		   for 192Khz or/and 92Khz then set two pixel repeat one.
+		   KIV: Add pruning for pixel PIX_REPLICATION_3 if required  */
+		if(temp_cea->audio_cap[CAP_192_KHZ].max_channels>2 ||
+			temp_cea->audio_cap[CAP_96_KHZ].max_channels>2){
+			port->edid->cea->pixel_rep = PIX_REPLICATION_1;
+		}
+
+
+		/* Based on HDMI spec 6.7.1 & 6.7.2 */
+		if ((timing_info->width == 720) && ((timing_info->height == 480) ||
+			 (timing_info->height== 576))){
+			port->edid->cea->colorimetry	= HDMI_COLORIMETRY_ITU601;
+		} else if(((timing_info->width==1280) && (timing_info->height==720)) ||
+			((timing_info->width == 1920) && (timing_info->height == 1080))){
+			port->edid->cea->colorimetry	= HDMI_COLORIMETRY_ITU709;
+		}
+
+		cea_timings = (igd_timing_info_t *) OS_ALLOC(cea_timing_table_size);
+		OS_MEMCPY(cea_timings, cea_timing_table, cea_timing_table_size);
+		cea_timing_temp = cea_timings;
+
+		while (cea_timings->width != IGD_TIMING_TABLE_END){
+			if(cea_timings->width == timing_info->width &&
+			   cea_timings->height == timing_info->height &&
+			   cea_timings->refresh == timing_info->refresh &&
+			   cea_timings->dclk == timing_info->dclk &&
+			   (cea_timings->mode_info_flags &
+			   (PD_ASPECT_16_9| IGD_SCAN_INTERLACE)) ==
+			   (timing_info->mode_info_flags &
+			   (PD_ASPECT_16_9| IGD_SCAN_INTERLACE))){
+					port->edid->cea->video_code		= cea_timings->mode_number;
+					break;
+				}
+			cea_timings++;
+		}
+
+		OS_FREE(cea_timing_temp);
+
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+#endif /* CALCULATE_ELD_INFOFRAMES */
+
+/*!
+ * Calculates the Edid like data (ELD) if port supports audio transmission
+ *
+ * @param port
+ * @param timing_info
+ *
+ * @return 0
+ */
+int calculate_eld(
+	igd_display_port_t *port,
+	igd_timing_info_t *timing_info)
+{
+	/* Calculate for non-content protected & content protected(Array of 2) */
+#ifdef CALCULATE_ELD_INFOFRAMES
+	unsigned long cal_NPL[2]; /* Number of packer per line calculate*/
+	unsigned long poss_NPL[2]; /* Number of packer per line possible*/
+	unsigned long max_bitRate_2[2],max_bitRate_8[2];
+	unsigned long h_refresh, audio_freq;
+	unsigned char input;
+	int i,j,pix_rep;
+#endif
+	cea_extension_t *temp_cea = NULL ;
+
+	EMGD_TRACE_ENTER;
+	/* Only calculate eld for HDMI port */
+	if((port->pd_driver->type !=  PD_DISPLAY_HDMI_EXT &&
+		port->pd_driver->type !=  PD_DISPLAY_HDMI_INT)){
+		return 0;
+	}
+	if(port->firmware_type == PI_FIRMWARE_EDID){
+		temp_cea = (cea_extension_t*)port->edid->cea;
+	}
+	/* Displayid unsupported for now. Uncomment this code when audio
+	information is available for Display ID
+	temp_cea = (&cea_extension_t)port->displayid->cea;*/
+
+	if(temp_cea == NULL){
+		/* CEA data unavailable, display does not have audio capability? */
+		/* We would allocate dummy edid structure and here and we should ony
+		   used canned ELD */
+		if(port->edid == NULL) {
+			port->edid = (edid_t *) OS_ALLOC(sizeof(edid_t));
+			OS_MEMSET(port->edid, 0 , (sizeof(edid_t)));
+		}
+
+		port->edid->cea = (cea_extension_t *) OS_ALLOC(sizeof(cea_extension_t));
+		OS_MEMSET(port->edid->cea, 0 , (sizeof(cea_extension_t)));
+		port->edid->cea->canned_eld = 1;
+		temp_cea = (cea_extension_t*)port->edid->cea;
+		port->callback->eld = &(port->edid->cea);
+	}
+
+	/* Default to canned ELD data */
+	temp_cea->LPCM_CAD[0] = 0x9;
+	temp_cea->speaker_alloc_block[0] = 0x1;
+	/* Default 0 Pixel replication */
+	port->edid->cea->pixel_rep = PIX_REPLICATION_0;
+	/* Default */
+	port->edid->cea->colorimetry = HDMI_COLORIMETRY_NODATA;
+	/* Default RGB 256 wuantization full range */
+	port->edid->cea->quantization = HDMI_QUANTIZATION_RGB_256;
+	/* Default Unknown video code */
+	port->edid->cea->video_code = 0;
+	port->edid->cea->aspect_ratio = (timing_info->mode_info_flags & PD_ASPECT_16_9)
+	             ? PD_ASPECT_RATIO_16_9 : PD_ASPECT_RATIO_4_3;
+
+#ifdef CALCULATE_ELD_INFOFRAMES
+	calculate_infoframes(port,timing_info,temp_cea);
+	/* If canned eld is not set and audio info from transmitter is available */
+	if(temp_cea->canned_eld != 1 && (temp_cea->audio_flag & PD_AUDIO_CHAR_AVAIL)){
+		pix_rep = port->edid->cea->pixel_rep;
+		/*h_refresh = timing_info->dclk/timing_info->htotal;*/
+		h_refresh = timing_info->refresh;
+		cal_NPL[0] = (pix_rep*(timing_info->hsync_end - timing_info->hsync_start) -
+					port->edid->cea->K0) /32;
+		cal_NPL[1] = (pix_rep*(timing_info->hsync_end - timing_info->hsync_start) -
+					port->edid->cea->K1) /32;
+
+		poss_NPL[0] = MODE_MIN(cal_NPL[0],port->edid->cea->NPL);
+		poss_NPL[1] = MODE_MIN(cal_NPL[1],port->edid->cea->NPL);
+
+		max_bitRate_2[0] = h_refresh * poss_NPL[0] - 1500;
+		max_bitRate_2[1] = h_refresh * poss_NPL[1] - 1500;
+
+		max_bitRate_8[0] = h_refresh * poss_NPL[0] * 4 - 1500;
+		max_bitRate_8[1] = h_refresh * poss_NPL[1] * 4 - 1500;
+
+		/* Loop trough Content Protection disabled then enabled */
+		for(i=0 ; i<2; i++){
+			for(j=0 ; j<3; j++){
+				input = 0;
+				audio_freq = 48000 * (1<<j); /* 48Khz->96Khz->192Khz */
+				if(max_bitRate_8[i] >= audio_freq){
+					input = 7;
+				}else if(max_bitRate_2[i] >= audio_freq){
+					input = 1;
+				}
+				/* take the minimum value min(transmitter, receiver) */
+				input = MODE_MIN(input,temp_cea->audio_cap[j].max_channels);
+				temp_cea->LPCM_CAD[j] |= input<<((1-i)*3);
+				if(temp_cea->audio_cap[j]._24bit){
+					temp_cea->LPCM_CAD[j] |= BIT(7);
+				}
+				if(temp_cea->audio_cap[j]._20bit){
+					temp_cea->LPCM_CAD[j] |= BIT(6);
+				}
+			}
+		}
+
+		/* TODO: Further construction of ELD from Monitor Name String begins here
+		   for now we only support VSDB */
+		/* By default we don send any vendor specific block unless latency value
+		   use for audio sync feature is available */
+		temp_cea->vsdbl = 0;
+		/* This means the latecy field is available VSBD_LATENCY_FIELD = 8*/
+		if(temp_cea->vendor_block.vendor_block_size > VSBD_LATENCY_FIELD){
+			OS_MEMCPY(temp_cea->misc_data, temp_cea->vendor_data_block,
+				temp_cea->vendor_block.vendor_block_size);
+			temp_cea->vsdbl = temp_cea->vendor_block.vendor_block_size;
+			/* If the VSBD has latency fields */
+			if(*(temp_cea->vendor_data_block + VSBD_LATENCY_FIELD - 1) & 0x80){
+				if(timing_info->mode_info_flags & IGD_SCAN_INTERLACE){
+					if(*(temp_cea->vendor_data_block + VSBD_LATENCY_FIELD - 1) & 0x40){
+						temp_cea->vendor_block.p_latency = 1;
+						temp_cea->vendor_block.i_latency = 1;
+					}else{
+						/* No latency available: Since it is an interlace mode but no
+						   vsbd_intlc_fld_present is available */
+						temp_cea->vendor_block.p_latency = 0;
+						temp_cea->vendor_block.i_latency = 0;
+					}
+				}else{
+					temp_cea->vendor_block.p_latency = 1;
+					temp_cea->vendor_block.i_latency = 0;
+				}
+			}
+		}
+	}
+#endif /* CALCULATE_ELD_INFOFRAMES */
+	temp_cea->audio_flag |= ELD_AVAIL;
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Configure either the primary or secondary display. This means all the
+ * timings, the framebuffer, the ports, the plane, and the pipe.
+ *
+ * The port range could be calculated based on primary or secondary
+ * display but it seems easier at this point to pass the port range
+ * in since it is used for all the for loops.
+ *
+ * @param driver_handle
+ * @param display
+ * @param pt_info
+ * @param fb_info
+ * @param dc
+ * @param p0
+ * @param pn
+ * @param flags
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int configure_display(
+	igd_driver_h driver_handle,
+	igd_display_context_t *display,
+	igd_display_info_t *pt_info,
+	igd_framebuffer_info_t *fb_info,
+	unsigned long dc,
+	int p0, int pn,
+	unsigned long flags)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	int p;
+	igd_display_port_t *port;
+	igd_timing_info_t *timing_info;
+	unsigned long update_flags;
+	unsigned short port_number = 0;
+	int ret;
+	int seamless = FALSE;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: Should this be an assert? */
+	if (display == NULL) {
+		EMGD_DEBUG("Trying to configure a NULL display");
+		return 0;
+	}
+
+	EMGD_DEBUG("Configure timings");
+	for (p = pn; p > p0; p--) {
+		EMGD_DEBUG("Configure port %d", DC_PORT_NUMBER(dc, p));
+		if ((port_number = DC_PORT_NUMBER(dc, p))) {
+			port = context->mod_dispatch.dsp_port_list[port_number];
+			if (!port) {
+				EMGD_DEBUG("Port %d not found", port_number);
+			} else {
+
+				/* Put a copy of the timings in the port's structure */
+				if (pt_info) {
+					if (port->pt_info == NULL) {
+						port->pt_info = OS_ALLOC(sizeof(igd_display_info_t));
+						if (!port->pt_info) {
+							EMGD_ERROR_EXIT("unable to alloc a pt_info "
+								"struct in port.");
+							return -IGD_ERROR_INVAL;
+						}
+					}
+					OS_MEMCPY(port->pt_info, pt_info,
+						sizeof(igd_display_info_t));
+				} else {
+					EMGD_ERROR("No primary timing info!");
+				}
+			}
+		}
+	}
+
+	if(!(pt_info->flags & IGD_DISPLAY_ENABLE)) {
+		EMGD_ERROR_EXIT("Ptinfo has no IGD_DISPLAY_ENABLE!");
+		return 0;
+	}
+
+	display->port_number = DC_PORT_NUMBER(dc, (p0 + 1));
+
+	/* Set mode */
+	EMGD_DEBUG("Set mode, using port %ld", display->port_number);
+	port = PORT(display, display->port_number);
+
+	EMGD_DEBUG("Calling matchmode on display");
+	ret = match_mode(display, port->timing_table, fb_info, pt_info,
+			&timing_info);
+	if(ret) {
+		EMGD_DEBUG("Match Mode for display failed");
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Now b4, we program the timing_info, let's first see if seamless
+     * option is requested, if it is then
+	 * We need to make sure the incoming dc, timing, framebuffer
+	 * info and etc match. We must respect the FORCE_ALTER flag.
+	 *
+	 * Seamless option buys you a one-time ticket for the seamless
+	 * experience from the firmware to the driver. After the first mode set
+	 * in driver, you don't get it the next time when you alter display.
+	 *
+	 */
+#ifndef CONFIG_MICRO
+	if(dc &&  !(flags & IGD_FORCE_ALTER) &&
+			 (mode_context->seamless == TRUE) ) {
+
+		/* User wants seamless */
+		if(mode_context->fw_info != NULL) {
+
+			OPT_MICRO_CALL_RET(seamless, query_seamless(dc,
+				/*(p0/4),*/
+				PIPE(display)->pipe_num,
+			    timing_info,
+				fb_info,
+				0));
+
+			EMGD_DEBUG(":Seamless = %s", seamless ?"ON" : "OFF");
+			mode_context->seamless = FALSE;
+			/* FIXME: For clone you get called twice. Need to
+			 * Fix that corner case
+			 */
+
+		}
+	}
+#endif
+	/* In case the seamless is FALSE, we do reset_plane_pipe_ports
+	 * which is supposed to be called in alter_displays anyway.
+	 * But we have to delay it since the user asked for seamless.
+	 * And we don't want to switch-off the display during
+	 * seamless.
+	 * Now we know that even though the user asked for it, we cannot
+	 * support seamless, so we call reset_plane_pipe_ports now.
+	 */
+	if(seamless == FALSE) {
+
+		/* Reset planes/pipes/ports before doing first alter display */
+		if (mode_context->first_alter) {
+			mode_context->dispatch->reset_plane_pipe_ports(
+									mode_context->context);
+			mode_context->first_alter = FALSE;
+		}
+
+	}
+
+	if(calculate_eld(port, timing_info)){
+		EMGD_DEBUG("Fail to calculate ELD");
+	}
+	/* turn on all ports */
+	EMGD_DEBUG("turn on displays plane_pipe_ports %d..%d", (p0 + 1), (pn-1));
+
+	for (p = (p0 + 1); p <= pn; p++) {
+		if (DC_PORT_NUMBER(dc, p)) {
+			port = context->mod_dispatch.dsp_port_list[DC_PORT_NUMBER(dc, p)];
+
+			display->allocated = 1;
+
+			/* Update mode info for the port */
+			if (p == (p0 + 1)) {
+				update_flags = MODE_UPDATE_PLANE | MODE_UPDATE_PIPE |
+						MODE_UPDATE_PORT;
+			} else {
+				update_flags = MODE_UPDATE_PORT;
+			}
+			ret = mode_update_plane_pipe_ports(display, DC_PORT_NUMBER(dc, p),
+					timing_info, fb_info, pt_info, update_flags);
+			if (ret) {
+				/*
+				 * This could happen if there was no memory for the
+				 * framebuffer or the FB was an invalid format. The
+				 * first is a too bad failure. The second should have
+				 * been checked by the IAL.
+				 */
+				EMGD_ERROR_EXIT("mode_update_plane_pipe_ports returned error "
+					"%d", ret);
+				port->pt_info->flags &= ~IGD_DISPLAY_ENABLE;
+				return ret;
+			}
+
+			/* Program the port registers */
+			if(seamless == TRUE) {
+				/* Don't have to program the registers, Since it's
+				 * all updated. Just return 0
+				 */
+				ret = 0;
+			} else {
+				ret = mode_context->dispatch->program_port(display,
+					DC_PORT_NUMBER(dc, p), TRUE);
+			}
+			if (ret == 0) {
+				port->inuse = 1;
+			} else {
+				port->pt_info->flags &= ~IGD_DISPLAY_ENABLE;
+			}
+		}
+	}
+	EMGD_DEBUG("done - turn on displays plane_pipe_ports %d", p);
+
+	/* Clear the Framebuffer after the planes, pipes and ports are
+	 * disabled and before they are enabled. */
+	if (flags & IGD_CLEAR_FB) {
+		OPT_MICRO_VOID_CALL(full_clear_fb(mode_context, fb_info, NULL));
+	}
+
+	/* program the pipe/plane/port if seamless is FALSE */
+	if(seamless == FALSE) {
+
+		EMGD_DEBUG("Seamless is FALSE");
+
+		ret = TRUE;
+
+		do{
+		/* turn on pipe */
+		mode_context->dispatch->program_pipe(display, TRUE);
+
+		/* turn on plane */
+		mode_context->dispatch->program_plane(display, TRUE);
+
+		/* turn on port */
+		for (p = pn; p > p0; p--) {
+			if (DC_PORT_NUMBER(dc, p)) {
+					mode_context->dispatch->post_program_port(display,
+				DC_PORT_NUMBER(dc, p), TRUE);
+			}
+		}
+
+			/* Check is display working fine */
+			OPT_MICRO_CALL_RET(ret, mode_context->dispatch->
+					check_display(display, DC_PORT_NUMBER(dc, p),TRUE));
+
+			if(!ret){
+				/* turn off plane and pipe */
+				mode_context->dispatch->program_plane(display, FALSE);
+				mode_context->dispatch->program_pipe(display, FALSE);
+			}
+		}while(!ret);
+
+	}
+#ifndef CONFIG_MICRO
+	else  { /* Seamless is TRUE */
+
+		/* Updating the plane registers, does not require us to
+		 * turn-off the pipe and we can still have seamless
+		 * because the display is not turned-off
+		 */
+
+		EMGD_DEBUG(" Seamless is TRUE");
+		if(mode_context->fw_info->program_plane == 1) {
+
+			/* This means we have to update the plane registers
+			 * with the new values.eg. Change in pitch size between
+			 * firmware values and driver values. But we MUST also
+			 * update the palette registers for this to work and
+			 * palette registers are programmed when pipe is programmed.
+			 * This means we program the pipe , followed by the plane.
+			 */
+
+			/* By doing this, we update the palette */
+			mode_context->dispatch->program_pipe(display, TRUE);
+
+			/* update the plane registers */
+			mode_context->dispatch->program_plane(display, TRUE);
+		}
+	}
+#endif
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int igd_configure_display(
+	igd_driver_h driver_handle,
+	igd_display_h *display,
+	igd_display_info_t *pt_info,
+	igd_framebuffer_info_t *fb_info,
+	unsigned long dc,
+	int fb_index,
+	unsigned long flags)
+{
+	int p0, pn;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+    /* Which ports do we loop through in the dc */
+    if (fb_index == 0) {
+        p0 = 0;
+        pn = 4;
+    } else {
+        p0 = 4;
+        pn = 7;
+    }
+
+	ret = configure_display(driver_handle, (igd_display_context_t *)display,
+		pt_info, fb_info, dc, p0, pn, flags);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+#ifndef CONFIG_MICRO
+
+/*!
+ * This function checks whether all the ports on the primary pipe of a dc are
+ * moving to a secondary pipe on a new dc.
+ *
+ * @param current_dc current_dc the system is in.
+ * @param dc     new requested dc.
+ *
+ * @return 0 if not all the ports are moving
+ * @return 1 if all the ports are moving
+ */
+int all_ports_moving(unsigned long current_dc, unsigned long dc)
+{
+	unsigned long index, index2, port;
+	int found_port;
+	int result = 1;
+
+	/*
+	 * We need to look for each of the primary ports on the current dc,
+	 * and see if they exist on the secondary port of the new dc.
+	 */
+	for (index = IGD_DC_IDX_PRIMARY_MASTER;
+		index <= IGD_DC_IDX_PRIMARY_TWIN3; index++) {
+		found_port = 0;
+		port = IGD_DC_PORT_NUMBER(current_dc, index);
+		/* If there is a port, let's look for it. */
+		if (port) {
+			/* Try to find this port on the secondary pipe */
+			for (index2 = IGD_DC_IDX_SECONDARY_MASTER;
+				index2 <= IGD_DC_IDX_SECONDARY_TWIN2; index2++) {
+				/*
+				 * We found the port, let's stop looking for this port and
+				 * move on to the next port
+				 */
+				if (port == IGD_DC_PORT_NUMBER(dc, index2)) {
+					found_port = 1;
+					break;
+				}
+			}
+			/* As soon as there is a port that we don't find, we can exit */
+			if (!found_port) {
+				result = 0;
+				break;
+			}
+		}
+	}
+
+	return result;
+}
+
+/*!
+ * This function checks whether a specified port exists in a dc, and if it does
+ * it returns the pipe master index for the pipe which contained the port.
+ *
+ * @param dc     the dc to check for a port.
+ * @param port   the port to check for.
+ *
+ * @return 0 if the specified does not exist
+ * @return IGD_DC_IDX_PRIMARY_MASTER if the port was found on the primary pipe
+ * @return IGD_DC_IDX_SECONDARY_MASTER if the port was found on the secondary
+ *   pipe
+ */
+int dc_contains_port_type(unsigned long dc, unsigned long port)
+{
+	unsigned long index;
+	int result = 0;
+
+	for (index = IGD_DC_IDX_PRIMARY_MASTER;
+		index <= IGD_DC_IDX_PRIMARY_TWIN3; index++) {
+
+		if (IGD_DC_PORT_NUMBER(dc, index) == port) {
+			result = IGD_DC_IDX_PRIMARY_MASTER;
+			break;
+		}
+	}
+
+	for (index = IGD_DC_IDX_SECONDARY_MASTER;
+		index <= IGD_DC_IDX_SECONDARY_TWIN2; index++) {
+
+		if (IGD_DC_PORT_NUMBER(dc, index) == port) {
+			result = IGD_DC_IDX_SECONDARY_MASTER;
+			break;
+		}
+	}
+
+	return result;
+}
+
+/*!
+ * This function checks whether a frame buffer swap and cursor
+ * swap is required based on given current_dc and new_dc.
+ *
+ * @param current_dc current_dc the system is in.
+ * @param dc     new requested dc.
+ * @param dsp    pointer to a display context pointer
+ *
+ * @return 0 if no swap is required
+ * @return 1 if swap is required
+ */
+int swap_required(
+	unsigned long current_dc,
+	unsigned long dc,
+	igd_display_context_t **dsp)
+{
+
+	/*
+	 * Note: Preserve the order of conditions as is. Changing the order
+	 * of below conditions require relook into the whole function to
+	 * make sure sematics match to return right value.
+	 */
+
+	/* Do not swap if no dc or current_dc */
+	if (!dc || !current_dc) {
+		return 0;
+	}
+
+	/*
+	 * Do NOT swap the frame buffer and cursor if dc is going
+	 * from twin to extended and not all the ports are moving to the other
+	 * pipe. If all the ports are moving to the other pipe, then DO swap
+	 * the frame buffer and cursor.
+	 * We also need to make sure we are not in the special case where our
+	 * extended mode contains LVDS and we are on a platform which does not
+	 * support LVDS on PIPE A. In this case we do not want to swap here because
+	 * we will not be swapping later since the twin configuration will stay on
+	 * PIPE A so that the LVDS can move to PIPE B.
+	 */
+	if (IGD_DC_TWIN(current_dc) && IGD_DC_EXTENDED(dc)) {
+		if (dsp && *dsp && all_ports_moving(current_dc, dc) &&
+			!(dc_contains_port_type(dc, IGD_PORT_TYPE_LVDS) &&
+			!(PIPE(*dsp)->pipe_features & IGD_PORT_SHARE_LVDS))) {
+			return 1;
+		} else {
+			return 0;
+		}
+	}
+
+	/*
+	 * Do NOT swap if dc is changing from Single to clone or
+	 * vice versa. In this case there is only 1 fb and no swap is required
+	 * and it stays with primary display.
+	 */
+	if ((IGD_DC_SINGLE(current_dc) && IGD_DC_CLONE(dc)) ||
+		(IGD_DC_CLONE(current_dc) && IGD_DC_SINGLE(dc))) {
+		return 0;
+	}
+
+	/*
+	 * If secondary master port is moving to primary master or
+	 * primary master is moving to secondary master, try
+	 * and keep the framebuffer address and cursor the same.
+	 */
+	if (DC_PORT_NUMBER(current_dc, 5) == DC_PORT_NUMBER(dc, 1) ||
+		(DC_PORT_NUMBER(current_dc, 1) == DC_PORT_NUMBER(dc, 5))) {
+		/*
+		Note: This was previosuly return 1 to indicate swap required, which will cause
+		a frame buffer swap when changing display from twin to clone. If the display is
+		in extended mode before changing from twin to clone, then both Plane A and Plane B
+		will point to the secondary frame buffer offset adress in clone mode and cause
+		blank screen (.
+		*/
+		return 0;
+	}
+	return 0;
+}
+#endif
+
+/*!
+ * This function sets up planes, pipes, and ports
+ * with the configuration passed in and returnes either one
+ * or two display handle lists.
+ *
+ * @param driver_handle from igd_init_driver().
+ * @param primary on return, this points to a list of displays.
+ * @param primary_ptinfo incoming timing info for the primary.
+ * @param primary_fbinfo incoming framebuffer info.
+ * @param secondary on return, this points to a list of displays.
+ * @param secondary_fbinfo incoming framebuffer info.
+ * @param dc display configuration
+ * @param flags modify function behavior
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+int igd_alter_displays(
+	igd_driver_h driver_handle,
+	igd_display_h *_primary,
+	igd_display_info_t *primary_pt_info,
+	igd_framebuffer_info_t *primary_fb_info,
+	igd_display_h *_secondary,
+	igd_display_info_t *secondary_pt_info,
+	igd_framebuffer_info_t *secondary_fb_info,
+	unsigned long dc,
+	unsigned long flags)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_context_t **primary = (igd_display_context_t **)_primary;
+	igd_display_context_t **secondary = (igd_display_context_t **)_secondary;
+	igd_framebuffer_info_t *fb_info = NULL;
+	igd_display_context_t *display = NULL,*tv_display=NULL;
+	drm_emgd_priv_t *priv = ((struct drm_device *)context->drm_dev)->dev_private;
+	int p;
+	int ret;
+	unsigned short tv_port_num=0;
+	int p_chng = 1, s_chng = 1;
+	unsigned char disable_plane_pipe = 0;
+	unsigned long current_dc;
+
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Make sure the DC is valid
+	 *
+	 * vBIOS won't be able to do this every time, for now only have
+	 * the drivers's do the check.
+	 */
+#ifndef CONFIG_MICRO
+	if (dc && !dsp_valid_dc(dc, 0)) {
+		EMGD_ERROR_EXIT("Invalid display configuration: 0x%08lx", dc);
+		return -IGD_ERROR_INVAL;
+	}
+#endif
+
+
+	/*
+	 * Can all display_info's and fb_info's be NULL?  I.E. make this
+	 * function do an alloc of display handles only?  If so, then
+	 * check for that condition and return without error. Otherwise
+	 * return an error.
+	 */
+	if (dc && (!primary_pt_info && !primary_fb_info) &&
+			(!secondary_pt_info && !secondary_fb_info)) {
+		EMGD_ERROR_EXIT("Invalid timing and framebuffer info");
+		return -IGD_ERROR_INVAL;
+	}
+
+#ifndef CONFIG_MICRO
+	/* FIXME: GDK Change this to dispatch->idle() */
+	if (dsp_wait_rb(mode_context->context) != 0) {
+		return -IGD_ERROR_INVAL;
+	}
+#endif
+
+	/* If seamless request is NOT set , then do reset_plane_pipe_ports
+	 * else delay it until we cannot support it.
+	 * If seamless is requested by the user and we CAN support it
+	 * then we need to make sure reset_plane_pipe_ports is NOT
+	 * called. That's the whole point anyway. Not to reset anything
+	 * during seamless transition
+	 */
+	if(mode_context->seamless != TRUE) {
+
+		/* Reset planes/pipes/ports before doing first alter display */
+		if (mode_context->first_alter) {
+			mode_context->dispatch->reset_plane_pipe_ports(
+									mode_context->context);
+			mode_context->first_alter = FALSE;
+		}
+	}
+
+	current_dc = *(context->mod_dispatch.dsp_current_dc);
+
+#ifndef CONFIG_MICRO
+	/* Check if platform needs force alter
+	* 	to make sure we run tuning code. This
+	* 	is for TNC-B0 workaround.*/
+	if (mode_context->dispatch->dsp_is_force_alter_required){
+		if (mode_context->dispatch-> dsp_is_force_alter_required(context->
+						mod_dispatch.dsp_display_list[IGD_DC_PRIMARY(current_dc)],
+						current_dc, dc)){
+				flags |= IGD_FORCE_ALTER;
+		}
+	}
+#endif
+
+	/*
+	 * Turn off the planes, pipes, and ports associated with the current
+	 * DC. However, limit the change to the primary if the secondary
+	 * display handle is NULL or limit the change to the secondary if the
+	 * the pimary display handle is NULL.
+	 */
+	for (p = 7; p > 0; p--) {
+		if (p > 4) {
+			display = NULL;
+			if (DC_PORT_NUMBER(current_dc, p)) {
+				display = context->mod_dispatch.
+					dsp_display_list[IGD_DC_SECONDARY(current_dc)];
+				s_chng = TIMING_CHANGED(display, dc, current_dc,
+						secondary_pt_info, secondary_fb_info,
+						(unsigned long)0xfff00000, flags);
+			}
+
+			if (s_chng && display && secondary) {
+		/* if the port is TV, then don't set the power to S3 as this causes
+		 * blank screen and system hang on LVDS on FSDOS, probably because the
+		 * external clock needs to be on till the pipes and
+		 * DPLLs are off
+		 */
+				/* Invalidate flip-chains to avoid race conditions during the
+				 * mode-set */
+				priv->invalidate_flip_chains(IGD_DISPLAY_SECONDARY);
+
+				if(PORT(display,DC_PORT_NUMBER(current_dc, p))->pd_type ==
+					PD_DISPLAY_TVOUT) {
+					tv_display = display;
+					tv_port_num = DC_PORT_NUMBER(current_dc, p);
+				} else {
+					ret = mode_context->dispatch->program_port(display,
+					      DC_PORT_NUMBER(current_dc, p), FALSE);
+				}
+				/* The secondary pipe master */
+				if (p == 5) {
+					disable_plane_pipe = 1;
+				}
+			}
+		} else {
+			display = NULL;
+			if (DC_PORT_NUMBER(current_dc, p)) {
+				display = context->mod_dispatch.
+					dsp_display_list[IGD_DC_PRIMARY(current_dc)];
+				p_chng = TIMING_CHANGED(display, dc, current_dc,
+						primary_pt_info, primary_fb_info,
+						(unsigned long)0x000ffff0, flags);
+			}
+
+			if (p_chng && display && primary) {
+		/* if the port is TV, then don't set the power to S3 as this causes
+		 * blank screen and system hang on LVDS on FSDOS, probably because the
+		 * external clock needs to be on till the pipes and
+		 * DPLLs are off
+		 */
+				/* Invalidate flip-chains to avoid race conditions during the
+				 * mode-set */
+				priv->invalidate_flip_chains(IGD_DISPLAY_PRIMARY);
+
+				if(PORT(display,DC_PORT_NUMBER(current_dc, p))->pd_type ==
+					PD_DISPLAY_TVOUT) {
+					tv_display = display;
+					tv_port_num = DC_PORT_NUMBER(current_dc, p);
+				} else {
+					ret = mode_context->dispatch->program_port(display,
+				      DC_PORT_NUMBER(current_dc, p), FALSE);
+				}
+				/* The primary pipe master */
+				if (p == 1) {
+					disable_plane_pipe = 1;
+				}
+			}
+		}
+
+		/* Disable plane and pipe after disabling the ports */
+		if (disable_plane_pipe) {
+			if(mode_context->dispatch->full) {
+				mode_context->dispatch->full->program_cursor(display, FALSE);
+			}
+			mode_context->dispatch->program_plane(display, FALSE);
+			mode_context->dispatch->program_pipe(display, FALSE);
+			/*pipes and dplls are off, now turn off tv port */
+			if(tv_display) {
+				ret = mode_context->dispatch->program_port(tv_display,
+					tv_port_num, FALSE);
+				tv_display = NULL;
+			}
+			disable_plane_pipe = 0;
+		}
+	}
+
+#ifndef CONFIG_MICRO
+	/* If DC is zero, then return here. A zero dc turns everything off */
+	/* This never happens for VBIOS since it only always calls *
+	 * alter_displays at the same point with the same valid DC */
+	if (!dc) {
+		int i;
+
+		priv->invalidate_flip_chains(IGD_DISPLAY_ALL);
+
+		mode_context->dispatch->reset_plane_pipe_ports(mode_context->context);
+		/* Should de-allocate everything here */
+		dsp_alloc(driver_handle, dc, flags);
+		/*
+		 * FIXME: This should be done inside dsp alloc, mode module does
+		 * not own this information.
+		 * When dc = 0, set all displays allocated to 0.
+		 */
+		for (i=0; i<IGD_MAX_PORTS+1; i++) {
+			if (context->mod_dispatch.dsp_display_list[i]) {
+				context->mod_dispatch.dsp_display_list[i]->allocated = 0;
+			}
+			context->mod_dispatch.dsp_display_list[i] = NULL;
+		}
+
+		return 0;
+	}
+#endif
+
+	/*
+	 * Check the DC (display configuration). If it is the same as the
+	 * current configuration, then don't change any allocations, only
+	 * modify the framebuffers and timings.
+	 */
+	if (dc != current_dc) {
+		EMGD_DEBUG("Allocate display handles based on DC");
+
+#ifndef CONFIG_MICRO
+		if (swap_required(current_dc, dc, primary)) {
+			swap_fb_cursor();
+		}
+#endif
+		/*
+		 * This function should never be called after VBIOS initialization *
+		 * The dsp_alloc is discarded after VBIOS init and is over-  *
+		 * written by font tables. Thus in VBIOS IAL, alter_displays *
+		 * is never get called with a different DC from the 1st time *
+		 */
+		dsp_alloc(driver_handle, dc, flags);
+
+	}
+
+	/* Attach the displays to the caller's pointers */
+	if (primary) {
+		*primary = context->mod_dispatch.dsp_display_list[IGD_DC_PRIMARY(dc)];
+	}
+	if (secondary) {
+		EMGD_DEBUG("Attaching display 1 to secondary pointer");
+		*secondary = context->mod_dispatch.
+			dsp_display_list[IGD_DC_SECONDARY(dc)];
+	}
+
+	/*
+	 * Configure the primary display. This configures the timings and the
+	 * framebuffer. Once configured, it turns everythying on.
+	 */
+	if(primary && *primary && (primary_pt_info || primary_fb_info) && p_chng) {
+		EMGD_DEBUG("Configure primary timings");
+		/* make framebuffer changes */
+		if (primary_fb_info) {
+			/* set up new frame buffer info */
+			fb_info = primary_fb_info;
+		} else {
+			fb_info = PLANE(*primary)->fb_info;
+		}
+
+		ret = configure_display(driver_handle,
+				(igd_display_context_t *)(*primary), primary_pt_info,
+				fb_info, dc, 0, 4, flags);
+		if (ret) {
+			EMGD_DEBUG("Primary display disabled.");
+		}
+	}
+
+	/*
+	 * Configure the secondary display. This configures the timings and the
+	 * framebuffer. Once configured, it turns everythying on.
+	 *
+	 * How close is this code to the code for the primary?  Could this
+	 * be moved to a separate function?
+	 */
+	if (secondary != NULL) {
+
+#ifndef CONFIG_MICRO
+		/*
+		 * In the case where we are in extended or clone and our pipe is not
+		 * turned on, we need to turn the pipes on.
+		 * We can run into this situation on pre-Cantiga Gen platforms on Linux
+		 * where LVDS was the primary display and was assigned PIPE B. Then we
+		 * are switching from LVDS to another display and that other display
+		 * wants to take PIPE A. In this case PIPE B will be turned on, the
+		 * display's new port will take PIPE A and turn on PIPE A.  The second
+		 * display thinks it is still PIPE A and nothing has changed for it.
+		 * In this case where our pipe is not turned on, we need to let the
+		 * system know that something has changed.
+		 */
+		if ((IGD_DC_CLONE(dc) || IGD_DC_EXTENDED(dc))
+			&& !(EMGD_READ32(MMIO(*secondary) + PIPE(*secondary)->pipe_reg)
+			& 0x80000000)) {
+			s_chng = 1;
+		}
+#endif
+
+		EMGD_DEBUG("Starting secondary pipe programming");
+		if ((*secondary != NULL) && (secondary_pt_info || secondary_fb_info) &&
+				s_chng){
+			/*
+			 * Configure the framebuffer.  For clone, it is the same
+			 * as the primary. For DIH, it is a unique fb.
+			 */
+			EMGD_DEBUG("configure secondary framebuffer");
+			if (dc & IGD_DISPLAY_CONFIG_CLONE) {
+				fb_info = PLANE(*primary)->fb_info;
+			} else {
+				if (secondary_fb_info) {
+					fb_info = secondary_fb_info;
+				} else {
+					fb_info = PLANE(*secondary)->fb_info;
+				}
+			}
+
+			ret = configure_display(driver_handle,
+					(igd_display_context_t *)(*secondary), secondary_pt_info,
+					fb_info, dc, 4, 7, flags);
+			if (ret) {
+				EMGD_DEBUG("Secondary display disabled.");
+				EMGD_ERROR("Secondary display disabled.");
+			}
+		}
+	} else {
+		EMGD_DEBUG("Skipped secondary programming, NULL handle");
+	}
+
+	/*
+	 * Workaround: wait for Vblank to avoid people accessing display
+	 * plane registers before the register is updated properly.
+	 */
+	if (primary && *primary) {
+		EMGD_DEBUG("Wait for vblank on primary display (%p)", primary);
+		EMGD_DEBUG("Wait for vblank on primary display (%p)", *primary);
+		mode_context->dispatch->wait_vblank(*primary);
+	} else if (secondary && *secondary) {
+		EMGD_DEBUG("Wait for vblank on secondary display");
+		mode_context->dispatch->wait_vblank(*secondary);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function sets the power state for the passed
+ * display handle.  This only updates the power state for the
+ * display/port.  The pipe, plane, and ringbuffer are left in
+ * the same power state.  There is also no need to
+ * alter_display, since only the port is modified.
+ *
+ * @param driver_handle from igd_init().
+ * @param port_number
+ * @param power_state
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+int igd_power_display(igd_driver_h driver_handle,
+	unsigned short port_number,
+	unsigned int power_state)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_context_t *display;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Requested power state = %d", power_state);
+
+	/* Get the display context that is currently using this port.  */
+	display = context->mod_dispatch.dsp_display_list[port_number];
+	if(!display) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* If this display is allocated, but has not been altered, return
+	 * an error. */
+	if (!PORT(display, port_number)->pt_info || !PIPE(display)->timing) {
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Set the desired power state to the display handle and let program_port
+	 * take care of the rest
+	 */
+	PORT(display, port_number)->power_state = (unsigned long)power_state;
+
+	switch(power_state) {
+	case IGD_POWERSTATE_D0:
+		mode_context->dispatch->program_port(display, port_number, TRUE);
+		mode_context->dispatch->post_program_port(display, port_number, 0);
+		break;
+	case IGD_POWERSTATE_D1:
+	case IGD_POWERSTATE_D2:
+	case IGD_POWERSTATE_D3:
+		mode_context->dispatch->program_port(display, port_number, TRUE);
+		break;
+	default:
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param driver_handle from igd_init().
+ * @param port_number
+ * @param edid_ptr
+ * @param block_number
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL or -IGD_ERROR_EDID on failure
+ */
+/* FIXME: Move this to PI */
+static int igd_get_EDID_block(igd_driver_h driver_handle,
+		unsigned short port_number,
+		unsigned char FAR *edid_ptr,
+		unsigned char block_number)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_port_t *port;
+	int                   ret;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(driver_handle, "Null driver_handle", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(edid_ptr, "Null edid_ptr", -IGD_ERROR_INVAL);
+
+	port = context->mod_dispatch.dsp_port_list[port_number];
+	if(!port) {
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+	/* Read EDID */
+	ret = context->mod_dispatch.i2c_read_regs(
+		context,
+		port->ddc_reg,
+		10,              /* DDC speed 10 KHz */
+		port->ddc_dab,
+		128*block_number,
+		edid_ptr,
+		128,
+		0);
+
+	if (ret) {
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_EDID;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end igd_get_EDID_block() */
+
+/*!
+ * Return either a pointer to the live mode list or a copy of the mode list
+ * for the requested display. This will be the mode list for the master port
+ * on the pipe.
+ *
+ * @note Currently (AS of 3.3 development) the mode list is
+ * described as a igd_display_info_t. However IT IS NOT, this
+ * pointer must be cast to a igd_timing_info_t to be used. After
+ * 3.3 the igd_display_info_t will be altered to match the
+ * igd_timing_info_t with the exception of the private pointers.
+ *
+ * @param driver_handle handle returned from a successful call to
+ * 	igd_driver_init().
+ * @param dc Display configuration that will determine which port
+ * 	controlls the pipe timings and thus, which set of timings to return.
+ * @param mode_list The returned mode list. This data may be LIVE. If
+ * 	a live list is returned, care should be taken to not free or alter
+ * 	the data.
+ * @param flags The flags will determine which display to query (primary
+ * 	or secondary) and if the mode list returned should be the live list.
+ *
+ * @return 0 on success.
+ * @return -IGD_INVAL if an error occured (memory allocation failed)
+ */
+int igd_query_mode_list(igd_driver_h driver_handle,
+		unsigned long dc,
+		igd_display_info_t **mode_list,
+		unsigned long flags)
+{
+	igd_context_t *context;
+	unsigned short port_number;
+	igd_display_port_t *port;
+
+	EMGD_TRACE_ENTER;
+
+	context = (igd_context_t *)driver_handle;
+	*mode_list = NULL;
+
+	/* given the DC and flags, which port number to check? */
+	port_number = (flags & IGD_QUERY_SECONDARY_MODES) ? DC_PORT_NUMBER(dc, 5) :
+		DC_PORT_NUMBER(dc, 1);
+
+	port = context->mod_dispatch.dsp_port_list[port_number];
+	if (port) {
+		if (flags & IGD_QUERY_LIVE_MODES) {
+			/*
+			 * FIXME:
+			 * timing_table is not an igd_dislay_info_t structure but
+			 * eventually it will be?
+			 */
+			*mode_list = (igd_display_info_t *)port->timing_table;
+		} else {
+			OPT_MICRO_CALL(full_mode_query(driver_handle, dc, mode_list,
+					port));
+		}
+	}
+
+	if (*mode_list == NULL) {
+		EMGD_DEBUG("No port on requested pipe");
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function is used to initialize any module/dsp
+ * module specific structures or tables etc.
+ *
+ * @param context SS level igd_context.
+ *
+ * @return 0 on success.
+ * @return -IGD_INVAL or -IGD_ERROR_NODEV on failure
+ */
+int mode_init(igd_context_t *context)
+{
+	igd_dispatch_t     *dispatch = &context->dispatch;
+	inter_module_dispatch_t *md;
+	int port_num;
+	int i;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Allocating a mode context...");
+
+	/* Clear the allocated memory for mode context */
+	OS_MEMSET((void *)mode_context, 0, sizeof(mode_context_t));
+
+	/* Set the pointer to igd level context */
+	mode_context->context = context;
+	mode_context->first_alter = TRUE;
+	mode_context->display_color =
+		context->mod_dispatch.init_params->display_color;
+#ifndef CONFIG_MICRO
+	mode_context->ref_freq =
+		context->mod_dispatch.init_params->ref_freq;
+	mode_context->tuning_wa =
+		context->mod_dispatch.init_params->tuning_wa;
+	/*To give option for validation*/
+	mode_context->clip_hw_fix =
+		context->mod_dispatch.init_params->clip_hw_fix;
+	mode_context->async_flip_wa =
+		context->mod_dispatch.init_params->async_flip_wa;
+	mode_context->en_reg_override =
+		context->mod_dispatch.init_params->en_reg_override;
+	mode_context->disp_arb =
+		context->mod_dispatch.init_params->disp_arb;
+	mode_context->fifo_watermark1 =
+		context->mod_dispatch.init_params->fifo_watermark1;
+	mode_context->fifo_watermark2 =
+		context->mod_dispatch.init_params->fifo_watermark2;
+	mode_context->fifo_watermark3 =
+		context->mod_dispatch.init_params->fifo_watermark3;
+	mode_context->fifo_watermark4 =
+		context->mod_dispatch.init_params->fifo_watermark4;
+	mode_context->fifo_watermark5 =
+		context->mod_dispatch.init_params->fifo_watermark5;
+	mode_context->fifo_watermark6 =
+		context->mod_dispatch.init_params->fifo_watermark6;
+	mode_context->gvd_hp_control =
+		context->mod_dispatch.init_params->gvd_hp_control;
+	mode_context->bunit_chicken_bits =
+		context->mod_dispatch.init_params->bunit_chicken_bits;
+	mode_context->bunit_write_flush =
+		context->mod_dispatch.init_params->bunit_write_flush;
+	mode_context->disp_chicken_bits =
+		context->mod_dispatch.init_params->disp_chicken_bits;
+#endif
+
+	for (i=0; i < IGD_MAX_PORTS; i++) {
+		port_num = context->mod_dispatch.init_params->display_params[i].port_number;
+		mode_context->batch_blits[port_num - 1] =
+			(context->mod_dispatch.init_params->display_params[i].flags
+			& IGD_DISPLAY_BATCH_BLITS);
+	}
+
+	/* Get mode's dispatch table */
+	mode_context->dispatch = (mode_dispatch_t *)
+		dispatch_acquire(context, mode_dispatch);
+	if(!mode_context->dispatch) {
+		EMGD_ERROR_EXIT("Unsupported Device");
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Hook up KMS dispatch table */
+	mode_context->kms_dispatch = (mode_kms_dispatch_t *)
+		dispatch_acquire(context, mode_kms_dispatch);
+	if(!mode_context->kms_dispatch) {
+		EMGD_ERROR_EXIT("Unsupported Device");
+		return -IGD_ERROR_NODEV;
+	}
+
+
+	md = &context->mod_dispatch;
+
+	/* Set the fw_info to 0 */
+	mode_context->fw_info = NULL;
+
+	/* Hook up the IGD dispatch table entires for mode */
+	dispatch->get_EDID_block = igd_get_EDID_block;
+	dispatch->power_display = igd_power_display;
+	dispatch->query_mode_list = igd_query_mode_list;
+	dispatch->alter_displays = igd_alter_displays;
+	dispatch->igd_configure_display = igd_configure_display;
+
+	OPT_MICRO_CALL(full_mode_init(context, mode_context));
+
+	/* Hook up inter-module dispatch functions */
+	md->mode_get_gpio_sets = mode_context->dispatch->get_gpio_sets;
+	md->mode_reset_plane_pipe_ports =
+		mode_context->dispatch->reset_plane_pipe_ports;
+	md->filter_modes = mode_context->dispatch->filter_modes;
+
+	/* Hook up Core specific IGD dispatch table entries */
+	dispatch->set_palette_entries =
+		mode_context->dispatch->full->set_palette_entries;
+	dispatch->set_palette_entry = mode_context->dispatch->set_palette_entry;
+	dispatch->get_palette_entry = mode_context->dispatch->get_palette_entry;
+	dispatch->wait_vblank = mode_context->dispatch->wait_vblank;
+
+	/* Initialize dsp module */
+	if (dsp_init(context)) {
+		EMGD_ERROR("dsp_init() failed.");
+		return -IGD_INVAL;
+	}
+
+	/* Initialze port interface (pi) module */
+	if (pi_init(context)) {
+		EMGD_ERROR_EXIT("pi_init() failed.");
+		if(md->dsp_shutdown) {
+			md->dsp_shutdown(context);
+		}
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (mode_context->dispatch->full && md->reg_get_mod_state) {
+		module_state_h *state = NULL;
+
+		/* Save mode state for the regular case*/
+		unsigned long *flags = NULL;
+		md->reg_get_mod_state(REG_MODE_STATE_REG, &state, &flags);
+		md->mode_save(context, state, flags);
+
+		/*Save mode state for the console case */
+		if (config_drm.init) {
+			state = NULL;
+			md->reg_get_mod_state(REG_MODE_STATE_CON, &state, &flags);
+			md->mode_save(context, state, flags);
+		}
+	}
+
+	toggle_vblank_interrupts(TRUE);
+	/* Initialize the Display Configuration List */
+	/* FIXME: This should be done in dsp init */
+	dsp_dc_init(context);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/cmn/mode_dispatch.h b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/mode_dispatch.h
new file mode 100644
index 0000000..b2ae62e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/mode_dispatch.h
@@ -0,0 +1,390 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_dispatch.h
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This header file defined the interface between the DI layer of the mode
+ *  module and the DD layer. Additionally it defines the interface between
+ *  the different DI object files within the mode module.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _MODE_DISPATCH_H
+#define _MODE_DISPATCH_H
+
+#include <mode.h>
+
+
+
+typedef struct _mode_kms_dispatch {
+	void (*kms_program_pipe) (emgd_crtc_t *emgd_crtc);
+	void (*kms_set_pipe_pwr) (emgd_crtc_t *emgd_crtc, unsigned long enable);
+	void (*kms_program_plane)(emgd_crtc_t *emgd_crtc, unsigned long status);
+	void (*kms_set_plane_pwr)(emgd_crtc_t *emgd_crtc, unsigned long enable);
+	int  (*kms_program_port) (emgd_encoder_t *emgd_encoder,
+		unsigned long status);
+	int  (*kms_post_program_port)(emgd_encoder_t *emgd_encoder,
+		unsigned long status);
+	u32  (*kms_get_vblank_counter)(emgd_crtc_t *emgd_crtc);
+	int (*kms_match_mode)(emgd_encoder_t *emgd_encoder,
+		igd_framebuffer_info_t *fb_info, igd_timing_info_t **timing);
+} mode_kms_dispatch_t;
+
+
+
+typedef struct _mode_full_dispatch {
+	int (*alter_cursor_pos)(igd_display_h display_handle,
+		igd_cursor_info_t *cursor_info);
+	int (*set_palette_entries)(igd_display_h display_handle,
+		unsigned long *palette_colors, unsigned int start_index,
+		unsigned int count);
+	int (*wait_vsync)(igd_display_h display_handle);
+	int (*query_in_vblank)(igd_display_h display_handle);
+	int (*get_scanline)(igd_display_h display_handle, int *scanline);
+	int (*set_display_base)(igd_display_context_t *display,
+		igd_framebuffer_info_t *fb, unsigned long *x, unsigned long *y);
+	void (*program_cursor)(igd_display_context_t *display,
+		unsigned long status);
+	int (*set_color_correct)(igd_display_context_t *display);
+	int (*get_surface)(igd_display_h display_handle, igd_buffertype_t type,
+		igd_surface_t *surface, igd_appcontext_h appcontext);
+	int (*set_surface)(igd_display_h display_handle, int priority,
+		igd_buffertype_t type, igd_surface_t *surface,
+		igd_appcontext_h appcontext, unsigned long flags);
+	int (*query_event)(igd_display_h display_handle, igd_event_t event,
+		unsigned long *status);
+
+	int (*set_flip_pending)(unsigned char *mmio,
+		unsigned long pipe_status_reg);
+	int (*check_flip_pending)(unsigned char *mmio,
+		unsigned long pipe_status_reg);
+
+	int (*get_plane_info)(void); /* dispatch routines that gets fw info */
+	int (*get_pipe_info)(igd_display_h *display );
+	int (*get_port_info)(void);
+	/* Implementation of "public" igd_dispatch_t.register_vblank_callback(). */
+	emgd_vblank_callback_h (*register_vblank_callback)(
+		emgd_process_vblank_interrupt_t callback,
+		void *priv,
+		unsigned long port_number);
+	/* Implementation of "public" igd_dispatch_t.unregister_vblank_callback().
+	 */
+	void (*unregister_vblank_callback)(
+		emgd_vblank_callback_h callback_h);
+	/* Implementation of "public" igd_dispatch_t.enable_vblank_callback(). */
+	int (*enable_vblank_callback)(emgd_vblank_callback_h callback_h);
+	/* Implementation of "public" igd_dispatch_t.disable_vblank_callback(). */
+	void (*disable_vblank_callback)(
+		emgd_vblank_callback_h callback_h);
+	/*!
+	 * Implementation of "protected" function (i.e. for use within the mode
+	 * module) to request VBlank interrupts for a particular purpose & port.
+	 * Based upon the outstanding requests, this function decides whether to
+	 * touch registers, register the interrupt handler, etc.
+	 *
+	 * @param request_for (IN).  A bit that identifies a who and what (e.g.
+	 *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+	 *
+	 * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+	 * registers.
+	 *
+	 * @return Zero for success, non-zero for failure.
+	 */
+	int (*request_vblanks)(unsigned long request_for, unsigned char *mmio);
+	/*!
+	 * Implementation of "protected" function (i.e. for use within the mode
+	 * module) to end a previous request VBlank interrupts for a particular
+	 * purpose & port.  Based upon the outstanding requests, this function
+	 * decides whether to touch registers, unregister the interrupt handler,
+	 * etc.
+	 *
+	 * @param request_for (IN).  A bit that identifies a who and what (e.g.
+	 *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+	 *
+	 * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+	 * registers.
+	 *
+	 * @return Zero for success, non-zero for failure.
+	 */
+	int (*end_request)(unsigned long request_for, unsigned char *mmio);
+	/*!
+	 * Implementation of "protected" function (i.e. for use within the mode
+	 * module) to check whether a requested VBlank interrupt occured.
+	 *
+	 * @param request_for (IN).  A bit that identifies a who and what (e.g.
+	 *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+	 *
+	 * @return Non-zero if the requested VBlank occured, zero if not.
+	 */
+	int (*vblank_occured)(unsigned long request_for);
+} mode_full_dispatch_t;
+
+typedef struct _mode_dispatch {
+	int (*set_palette_entry)(igd_display_h display_handle,
+		unsigned long palette_entry, unsigned long palette_color);
+	int (*get_palette_entry)(igd_display_h display_handle,
+		unsigned long palette_entry, unsigned long *palette_color);
+	int (*wait_vblank)(igd_display_h display_handle);
+	void (*program_plane)(igd_display_context_t *display,
+		unsigned long status);
+	void (*program_pipe)(igd_display_context_t *display, unsigned long status);
+	int (*program_port)(igd_display_context_t *display,
+		unsigned short port_number, unsigned long status);
+	int (*post_program_port)(igd_display_context_t *display,
+		unsigned short port_number, unsigned long status);
+	int (*program_clock)(igd_display_context_t *display,
+		igd_clock_t *clock, unsigned long dclk);
+	int (*program_cdvo)(void);
+	void (*reset_plane_pipe_ports)(igd_context_t *context);
+	unsigned long (*get_gpio_sets)(unsigned long **);
+	void (*filter_modes)(igd_context_t *context,
+		igd_display_port_t *port, pd_timing_t *in_list);
+	int (*check_display)(igd_display_context_t *display,
+		unsigned short port_number, unsigned long status);
+	int (*get_dd_timing)(igd_display_context_t *display,
+		pd_timing_t *in_list);
+	int (*check_port_supported)(void *port_tmp);
+	int (*get_refresh_in_border)(pd_timing_t *in_list);
+	bool (*dsp_is_force_alter_required)(igd_display_context_t *display,
+			unsigned long current_dc, unsigned long dc_to_set);
+	mode_full_dispatch_t *full;
+} mode_dispatch_t;
+
+
+/*
+ * Firmware(VBIOS or EFI Video Driver) related information
+ * that needs to be populated  before the driver re-programs
+ * the Hardware Registers. This information is needed to provide
+ * seamless transition from firmware to driver.
+ */
+typedef struct _fw_info {
+
+	/* TODO: Fill this up */
+	unsigned long fw_dc; /* The dsp module already has this value */
+
+	/* Plane information */
+	igd_framebuffer_info_t fb_info[2]; /* one for each plane */
+
+	/* Pipe information */
+	igd_display_info_t timing_arr[2]; /* one for each pipe */
+
+	/* Port information */
+
+	/* if the plane registers needs an update, set this field to 1 */
+	int program_plane;
+
+} fw_info_t;
+
+
+
+typedef struct _mode_context {
+	/*
+	 * All of the below values will be initialized in mode module
+	 * init function mode_init().
+	 */
+	unsigned long        first_alter;
+	mode_dispatch_t     *dispatch;
+	mode_kms_dispatch_t *kms_dispatch;
+
+	igd_context_t *context;
+	unsigned long display_color;
+	fw_info_t* fw_info; /* This needs to be zero for VBIOS */
+
+    /* quickboot options */
+    unsigned long quickboot;
+    int seamless;
+    unsigned long video_input;
+    int splash;
+	unsigned long ref_freq;
+	int tuning_wa;
+	unsigned long clip_hw_fix;
+	unsigned long async_flip_wa;
+
+	/*
+	 * Enable override of following registers when en_reg_override=1.
+	 * Display Arbitration, FIFO Watermark Control, GVD HP_CONTROL,
+	 * Bunit Chickenbits, Bunit Write Flush, Display Chickenbits
+	 */
+	unsigned long en_reg_override;
+	unsigned long disp_arb;
+	unsigned long fifo_watermark1;
+	unsigned long fifo_watermark2;
+	unsigned long fifo_watermark3;
+	unsigned long fifo_watermark4;
+	unsigned long fifo_watermark5;
+	unsigned long fifo_watermark6;
+	unsigned long gvd_hp_control;
+	unsigned long bunit_chicken_bits;
+	unsigned long bunit_write_flush;
+	unsigned long disp_chicken_bits;
+
+	/* Flags specifying whether to notify user-space of a v-blank event.
+	 * This is used when synchronizing back-buffer blits */
+	bool batch_blits[IGD_MAX_PORTS];
+
+} mode_context_t;
+
+extern int full_mode_init(igd_context_t *context,
+	mode_context_t *mode_context);
+
+extern int full_mode_query(igd_driver_h driver_handle, unsigned long dc,
+	igd_display_info_t **mode_list, igd_display_port_t *port);
+
+extern void full_clear_fb(mode_context_t *mode_context,
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb);
+
+extern int query_seamless(unsigned long dc,
+		int index,
+		igd_timing_info_t *pt,
+		igd_framebuffer_info_t *pf,
+		unsigned long flags);
+
+extern void swap_fb_cursor( void );
+
+extern int set_color_correct(igd_display_context_t *display,
+	const igd_range_attr_t *attr_to_set);
+
+extern void toggle_vblank_interrupts(bool status);
+
+extern mode_context_t mode_context[];
+
+/*
+ * NOTE: Some of these externs are declared with the struct name because the
+ * contents of that struct are unavailable at the DI layer. The symbol
+ * is used as the generic mode_dispatch_t which is a subset.
+ */
+
+
+extern mode_dispatch_t mode_dispatch_plb;
+extern mode_dispatch_t mode_dispatch_tnc;
+
+extern mode_kms_dispatch_t mode_kms_dispatch_plb;
+extern mode_kms_dispatch_t mode_kms_dispatch_tnc;
+
+
+
+/*******************************************************************************
+ *
+ * The following macros provide part of the "protected" interface to support
+ * mode-module "requests" for VBlank interrupts.
+ *
+ * Requests are for a "who" and "what."  The "who" is what type of code is
+ * making the request, and the "what" is the port that the requestor wants to
+ * know about VBlanks for.  Here is additional information:
+ *
+ * - Who - which software is asking:
+ *   - WAIT - The code that implements the wait_vblank() function pointer.
+ *            When interrupts are requested for WAIT, the interrupt handler
+ *            makes note of when VBlanks occur.  The WAIT code queries (polls)
+ *            whether a VBlank has occured since its request.
+ *   - FLIP - The code that implements the {check|set}_flip_pending() function
+ *            pointers.  When interrupts are requested for FLIP, the interrupt
+ *            handler makes note of when VBlanks occur.  The FLIP code queries
+ *            (polls) whether a VBlank has occured since its request.
+ *   - CB -   The non-HAL code that registers a VBlank interrupt "callback"
+ *            (CB).  When interrupts are requested for CB, the interrupt
+ *            handler calls the callback when VBlanks occur.
+ *
+ * - What - which port (Note: space is reserve for 4 ports, even only two exist
+ *   at this time):
+ *   - PORT2 (Port 2, Pipe A, SDVO-B)
+ *   - PORT4 (Port 4, Pipe B, Int-LVDS)
+ *
+ * Note: internally, the requests are stored in bits within an unsigned long.
+ * This helps explain the way the macros are implemented:
+ *
+ ******************************************************************************/
+
+/* A requestor uses this macro to generate the bit request for who and what: */
+#define VBINT_REQUEST(who,port) ((port) << (who))
+
+/* A requestor uses one of these macros to specify a what (i.e. port): */
+#define VBINT_PORT2 0x01
+#define VBINT_PORT4 0x02
+/* Note: the following 2 macros reserve space for 2 more (future) ports: */
+#define VBINT_PORTn 0x04
+#define VBINT_PORTm 0x08
+
+/* A requestor uses one of these macros to identify itself (the what): */
+/* Note: Each "who" has 4 bits (for 4 ports); the value is a shift amount: */
+#define VBINT_WAIT 0
+#define VBINT_FLIP 4
+#define VBINT_CB   8
+
+
+/*******************************************************************************
+ *
+ * The following macros provide part of the "private" interface to support
+ * mode-module "requests" for VBlank interrupts.  The VBlank-interrupt code
+ * uses these macros to manage requests, and to record VBlanks that occur
+ * (a.k.a. "answers").
+ *
+ * Other parts of the "mode" module should not use these macros.
+ *
+ ******************************************************************************/
+
+/* Answers for a request are stored in bits to the left of the request bits: */
+#define VBINT_ANSWER_SHIFT 12
+#define VBINT_ANSWER(who,port) (((port) << (who)) << VBINT_ANSWER_SHIFT)
+#define VBINT_ANSWER4_REQUEST(request) ((request) << VBINT_ANSWER_SHIFT)
+
+/* The following special bit is used by disable_vblank_interrupts_{plb|tnc}() to
+ * disable the hardware, but not unregister the never-registered interrupt
+ * handler:
+ */
+#define VBLANK_DISABLE_HW_ONLY BIT31
+
+/* The following macros aggregate all of the who's can enable interrupts for a
+ * given port:
+ */
+#define VBLANK_INT4_PORT2 (VBINT_REQUEST(VBINT_WAIT, VBINT_PORT2) | \
+	VBINT_REQUEST(VBINT_FLIP, VBINT_PORT2) |						\
+	VBINT_REQUEST(VBINT_CB, VBINT_PORT2))
+#define VBLANK_INT4_PORT4 (VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4) | \
+	VBINT_REQUEST(VBINT_FLIP, VBINT_PORT4) |						\
+	VBINT_REQUEST(VBINT_CB, VBINT_PORT4))
+
+/* The following macros aggregate all of the whats (ports) that can enable
+ * interrupts for a given who (they aren't used, but do help document that
+ * 4 bits are reserved for each "who"):
+ */
+/* FIXME -- KEEP THESE??? */
+#define VBLANK_INT4_WAIT 0x0000000f
+#define VBLANK_INT4_FLIP 0x000000f0
+#define VBLANK_INT4_CB   0x00000f00
+
+/* The following macros tell whether interrupts are enabled, either in general,
+ * or for a certain port.
+ */
+#define VBLANK_INTERRUPTS_ENABLED \
+	(vblank_interrupt_state & (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4 | \
+		VBLANK_DISABLE_HW_ONLY))
+#define VBLANK_INTERRUPTS_ENABLED4_PORT2 \
+	(vblank_interrupt_state & VBLANK_INT4_PORT2)
+#define VBLANK_INTERRUPTS_ENABLED4_PORT4 \
+	(vblank_interrupt_state & VBLANK_INT4_PORT4)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/cmn/vga_mode.c b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/vga_mode.c
new file mode 100644
index 0000000..546b699
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/cmn/vga_mode.c
@@ -0,0 +1,1467 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: vga_mode.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains VGA plane/pipe programming functions that can
+ *  be used on any VGA compatible hardware. Hardware specific functions
+ *  should put the device in VGA compatible mode prior to calling thses
+ *  functions.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <context.h>
+
+#include <sched.h>
+
+#include <igd_vga.h>
+
+#include "drm_emgd_private.h"
+
+#include <mode.h>
+#include <utils.h>
+#include <vga.h>
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*
+ * For mono modes this is 0  (CRTC is at 0x3b4)
+ * For other modes it is 0x20 (CRTC is at 0x3d4)
+ */
+unsigned char vga_port_offset = 0x20;
+
+/* This is a global variable used by the vBIOS to override the mode table
+ * if a User Defined Mode Table is set. */
+vga_mode_data_t FAR (*vga_mode_data_ptr)[]=&vga_mode_data;
+
+/* This is a global variable used by the vBIOS to enable/disable the
+ * loading of the palette during a mode change.  A global variable is
+ * the best choice, since this is not really an OAL interface.
+ *   0=Do load the default palette on a mode change.
+ *   1=Do NOT load the default palette on a mode change. */
+unsigned char vga_disable_default_palette_load = 0;
+
+/*!
+ *
+ * @param mmio
+ * @param port
+ * @param index
+ * @param value
+ *
+ * @return void
+ */
+void write_vga_reg(unsigned char *mmio, unsigned short port,
+	unsigned char index, unsigned char value)
+{
+	WRITE_VGA(mmio, port, index, value);
+}
+
+/*!
+ *
+ * @param display
+ * @param timings
+ *
+ * @return void
+ */
+void kms_program_plane_vga(unsigned char *mmio,
+	igd_timing_info_t *timings)
+{
+
+	EMGD_DEBUG("Enter program_plane_vga");
+
+	/* Set Bit 5 so the plane remains off.  It will be turned on
+	 * in the IAL.  This is necessary, so the clear screen can occur
+	 * before the mode is enabled. */
+	write_vga_reg(mmio, SR_PORT, 0x01,
+		(*vga_mode_data_ptr)[timings->mode_number].sr_regs[0] | 0x20);
+	OS_SLEEP(1000);
+
+	return;
+}
+
+/*!
+ *
+ * @param display
+ * @param timings
+ *
+ * @return void
+ */
+void program_plane_vga(igd_display_context_t *display,
+	igd_timing_info_t *timings)
+{
+	unsigned char *mmio;
+
+	EMGD_DEBUG("Enter program_plane_vga");
+
+	mmio = (unsigned char *)MMIO(display);
+	/* Set Bit 5 so the plane remains off.  It will be turned on
+	 * in the IAL.  This is necessary, so the clear screen can occur
+	 * before the mode is enabled. */
+	write_vga_reg(mmio, SR_PORT, 0x01,
+		(*vga_mode_data_ptr)[timings->mode_number].sr_regs[0] | 0x20);
+	OS_SLEEP(1000);
+
+	return;
+}
+
+unsigned char mono_colors[] = {0x00, 0x2a, 0x00, 0x3f};
+unsigned char normal_colors[] = {0x00, 0x2a, 0x15, 0x3f};
+
+unsigned char mono_color_bits[] = {8,16,8,16,8,16};
+unsigned char p16_color_bits[] = {4,16,2,16,1,16};
+unsigned char p64_color_bits[] = {4,32,2,16,1,8};
+
+/*!
+ *
+ * @param mmio
+ * @param n
+ *
+ * @return void
+ */
+void set_3f_palette(unsigned char *mmio, int n)
+{
+	int i;
+	for(i=0; i<n*3; i++) {
+		EMGD_WRITE8(0x3f, EMGD_MMIO(mmio) + 0x3c9);
+	}
+}
+
+/*!
+ *
+ * @param mmio
+ * @param n
+ * @param bits
+ * @param colors
+ * @param palette_hack
+ *
+ * @return void
+ */
+void set_palette_vga(unsigned char *mmio, int n, unsigned char *bits,
+	unsigned char *colors, unsigned char palette_hack)
+{
+	int i, j;
+	unsigned char *current_bits;
+	unsigned char color;
+
+	for(i=0; i<n; i++) {
+		current_bits = bits;
+		for(j=0; j<3; j++) {
+			color = colors[((i & current_bits[1])?2:0) |
+				((i & current_bits[0])?1:0)];
+			/* Nasty Hack for Special Case VGA palette */
+			if(palette_hack && (color == 0x2a) && (i%8==6) && (j==1)) {
+				color = 0x15;
+			}
+			EMGD_WRITE8( color, EMGD_MMIO(mmio) + 0x3c9);
+			current_bits += 2;
+		}
+	}
+}
+
+/*!
+ *
+ * @param mmio
+ *
+ * @return void
+ */
+void set_256_palette(unsigned char *mmio)
+{
+	unsigned char top_of_range[] = {
+		0x3f, 0x1c, 0x10
+	};
+	unsigned char bottom_of_range[] = {
+		0x00, 0x1f, 0x2d,
+		0x00, 0x0e, 0x14,
+		0x00, 0x08, 0x0b
+	};
+	unsigned char mono_256_color[]={
+		0x00, 0x05, 0x08,
+		0x0B, 0x0E, 0x11,
+		0x14, 0x18, 0x1C,
+		0x20, 0x24, 0x28,
+		0x2D, 0x32, 0x38,
+		0x3F
+	};
+	unsigned char p256_color_bits[] = {4,8,2,8,1,8};
+	unsigned char *bottom = bottom_of_range;
+	unsigned char *top = top_of_range;
+	unsigned char pattern_number[] = {4, 0, 0};
+	unsigned char changing_pattern = 2;
+	unsigned short adder;
+	char diff = 1;
+	int h,i,j,k,l;
+
+	set_palette_vga(mmio, 16, p256_color_bits, normal_colors, 1);
+
+	/* mono data */
+	for(i=0; i<16; i++) {
+		for (j=0; j<3; j++) {
+			EMGD_WRITE8(mono_256_color[i], EMGD_MMIO(mmio) + 0x3c9);
+		}
+	}
+	for(l=0; l<3; l++) {
+		for(k=0; k<3; k++) {
+			/* Adder is in 14.2 fixed point */
+			adder =  *top - *bottom;
+			for(j=0; j<6; j++) {
+				for(i=0; i<4; i++) {
+					for(h=2; h>=0; h--) {
+						EMGD_WRITE8( *bottom + (pattern_number[h]*adder + 1)/4,
+							EMGD_MMIO(mmio) + 0x3c9);
+					}
+					pattern_number[changing_pattern] += diff;
+				}
+				diff = -diff;
+				changing_pattern++;
+				changing_pattern %= 3;
+			}
+			bottom++;
+		}
+		top++;
+	}
+	set_3f_palette(mmio, 8);
+}
+
+static unsigned char next_ar;
+static unsigned char next_cr;
+static unsigned char next_gr;
+static unsigned char *g_mmio;
+
+/*!
+ * This is a little trick to save some space. Write these registers
+ * sequentially to save us a little bit of data space.
+ *
+ * @param value
+ *
+ * @return void
+ */
+void write_next_ar_reg(unsigned char value)
+{
+	WRITE_AR(g_mmio, next_ar, value);
+	next_ar++;
+}
+
+/*!
+ * This is a little trick to save some space. Write these registers
+ * sequentially to save us a little bit of data space.
+ *
+ * @param value
+ *
+ * @return void
+ */
+void write_next_cr_reg(unsigned char value)
+{
+	write_vga_reg(g_mmio, CR_PORT, next_cr, value);
+	next_cr++;
+}
+
+/*!
+ * This is a little trick to save some space. Write these registers
+ * sequentially to save us a little bit of data space.
+ *
+ * @param value
+ *
+ * @return void
+ */
+void write_next_gr_reg(unsigned char value)
+{
+	write_vga_reg(g_mmio, GR_PORT, next_gr, value);
+	next_gr++;
+}
+
+/*!
+ * This function programs the Timing registers and clock registers and
+ * other control registers for PIPE.
+ *
+ * @param emgd_crtc
+ * @param timings
+ *
+ * @return void
+ */
+void kms_program_pipe_vga(emgd_crtc_t *emgd_crtc,
+	igd_timing_info_t *timings)
+{
+	struct drm_device  *dev = NULL;
+	igd_context_t      *context = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	unsigned char *mmio, i;
+	unsigned char *colors=0;
+	unsigned char palette_hack=0;
+	unsigned char *color_bits = 0;
+	int mode_index;
+	unsigned char msr_temp;
+
+	/* This is a mapping from the HAL mode number to the type of Palette
+	 * being programmed for this mode. */
+	char palette_type[] = {
+	/* 9=Don't care.
+	 *  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
+		1, 1, 1, 1, 1, 1, 1, 2,	9, 9, 9, 9, 9, 1, 1, 9,
+		9, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 4 };
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	mmio = (unsigned char *)context->device_context.virt_mmadr;
+	g_mmio = mmio;
+	mode_index = timings->mode_number;
+
+	EMGD_DEBUG("IGD Mode#:0x%x", mode_index);
+
+	/* Disable Group 0 Protection */
+	write_vga_reg(mmio, CR_PORT, 0x11, 0x00);
+
+	/*
+	 * Note: for monochrome modes this will cause the IO port to change
+	 * for the CRTC and the Status regs.
+	 */
+	 msr_temp = (*vga_mode_data_ptr)[mode_index].misc_reg;
+
+#ifdef CONFIG_GN4
+	/*
+	 * According to Chrontel the VGA By-pass requires the HSYNC and VSYNC to be
+	 * of positive polarity.  In the MSR - Miscellaneous Output register
+	 * Bit 7 is CRT VSYNC polarity (0 = Positive, 1 = Negative)
+	 * Bit 6 is CRT HSYNC polarity (0 = Positive, 1 = Negative)
+	 */
+	{
+		pt = get_port_type(emgd_crtc->crtc_id);
+		if (pt == IGD_PORT_DIGITAL) {
+			msr_temp &= (~(BIT(7)|BIT(6)));
+		}
+	}
+#endif
+	EMGD_WRITE8(msr_temp, EMGD_MMIO(mmio) + 0x3c2);
+
+	if((*vga_mode_data_ptr)[mode_index].misc_reg & 1) {
+		vga_port_offset = 0x20;
+	} else {
+		vga_port_offset = 0;
+	}
+
+	/* Sequencer registers */
+	/*
+	 * Note: Most specs say 0 in SR00 scratch bits but in practice seems
+	 * that everyone uses 3.
+	 */
+	write_vga_reg(mmio, SR_PORT, 0x00, 0x03);
+	/* SR01 is on/off and done in program plane */
+	for (i=2; i<=4; i++) {
+		write_vga_reg(mmio, SR_PORT, i,
+			/* The SR Regs in the table are from SR01-SR04, there is
+			 * no SR00 in the table, so -1. */
+			(*vga_mode_data_ptr)[mode_index].sr_regs[i-1]);
+	}
+
+	/* Graphics control registers 0x0-0x8,0x10 */
+	next_gr = 0;
+	for(i=0; i<=0x8; i++) {
+		write_next_gr_reg((*vga_mode_data_ptr)[mode_index].gr_regs[i]);
+	}
+	/*
+	 * GR10 is a non-standard register that controls the mapping of
+	 * 0xa000 to MMIO or GTT memory.
+	 */
+	next_gr = 0x10;
+	write_next_gr_reg(0x0);
+
+	next_ar = 0;
+	for(i=0; i<=0x13; i++) {
+		write_next_ar_reg((*vga_mode_data_ptr)[mode_index].ar_regs[i]);
+	}
+	/* Spec says 0x8 for text modes, not done in practice */
+	write_next_ar_reg(0x00);
+
+	/* Ensure the Pixel Data Mask Register does not mask the pixel data */
+	EMGD_WRITE8(0xFF, EMGD_MMIO(mmio) + 0x3c6);
+
+	/* set DAC data value */
+	EMGD_WRITE8(0, EMGD_MMIO(mmio) + 0x3c8);
+
+	/* Load RAMDAC*/
+	switch(palette_type[mode_index]){
+	case 0:
+		color_bits = p64_color_bits;
+		colors = normal_colors;
+		palette_hack = 0;
+		break;
+	case 1:
+		color_bits = p16_color_bits;
+		colors = normal_colors;
+		palette_hack = 1;
+		break;
+	case 2:
+		color_bits = mono_color_bits;
+		colors = mono_colors;
+		palette_hack = 0;
+		break;
+	default:
+		break;
+	}
+	/* Program the Palette based on the mode. */
+	if (!vga_disable_default_palette_load) {
+		if (color_bits) {
+			set_palette_vga(mmio, 64, color_bits, colors, palette_hack);
+			set_3f_palette(mmio, 192);
+		} else {
+			set_256_palette(mmio);
+		}
+	}
+
+	/* Timings */
+	next_cr = 0;
+	for(i=0; i<=0x18; i++) {
+		write_next_cr_reg((*vga_mode_data_ptr)[mode_index].crtc_regs[i]);
+	}
+
+	return;
+}
+
+
+/*!
+ * This function programs the Timing registers and clock registers and
+ * other control registers for PIPE.
+ *
+ * @param display
+ * @param timings
+ *
+ * @return void
+ */
+void program_pipe_vga(igd_display_context_t *display,
+	igd_timing_info_t *timings)
+{
+	unsigned char *mmio, i;
+	unsigned char *colors=0;
+	unsigned char palette_hack=0;
+	unsigned char *color_bits = 0;
+	int mode_index;
+	unsigned char msr_temp;
+
+	/* This is a mapping from the HAL mode number to the type of Palette
+	 * being programmed for this mode. */
+	char palette_type[] = {
+	/* 9=Don't care.
+	 *  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
+		1, 1, 1, 1, 1, 1, 1, 2,	9, 9, 9, 9, 9, 1, 1, 9,
+		9, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 4 };
+
+	EMGD_DEBUG("Enter program_pipe_vga");
+
+	mmio = (unsigned char *)MMIO(display);
+	g_mmio = mmio;
+	mode_index = timings->mode_number;
+
+	EMGD_DEBUG("IGD Mode#:0x%x", mode_index);
+
+	/* Disable Group 0 Protection */
+	write_vga_reg(mmio, CR_PORT, 0x11, 0x00);
+
+	/*
+	 * Note: for monochrome modes this will cause the IO port to change
+	 * for the CRTC and the Status regs.
+	 */
+	 msr_temp = (*vga_mode_data_ptr)[mode_index].misc_reg;
+
+#ifdef CONFIG_GN4
+	/*
+	 * According to Chrontel the VGA By-pass requires the HSYNC and VSYNC to be
+	 * of positive polarity.  In the MSR - Miscellaneous Output register
+	 * Bit 7 is CRT VSYNC polarity (0 = Positive, 1 = Negative)
+	 * Bit 6 is CRT HSYNC polarity (0 = Positive, 1 = Negative)
+	 */
+	{
+		igd_display_port_t *port;
+		port = PORT_OWNER(display);
+		if (port->port_type == IGD_PORT_DIGITAL) {
+			msr_temp &= (~(BIT(7)|BIT(6)));
+		}
+	}
+#endif
+	EMGD_WRITE8(msr_temp, EMGD_MMIO(mmio) + 0x3c2);
+
+	if((*vga_mode_data_ptr)[mode_index].misc_reg & 1) {
+		vga_port_offset = 0x20;
+	} else {
+		vga_port_offset = 0;
+	}
+
+	/* Sequencer registers */
+	/*
+	 * Note: Most specs say 0 in SR00 scratch bits but in practice seems
+	 * that everyone uses 3.
+	 */
+	write_vga_reg(mmio, SR_PORT, 0x00, 0x03);
+	/* SR01 is on/off and done in program plane */
+	for (i=2; i<=4; i++) {
+		write_vga_reg(mmio, SR_PORT, i,
+			/* The SR Regs in the table are from SR01-SR04, there is
+			 * no SR00 in the table, so -1. */
+			(*vga_mode_data_ptr)[mode_index].sr_regs[i-1]);
+	}
+
+	/* Graphics control registers 0x0-0x8,0x10 */
+	next_gr = 0;
+	for(i=0; i<=0x8; i++) {
+		write_next_gr_reg((*vga_mode_data_ptr)[mode_index].gr_regs[i]);
+	}
+	/*
+	 * GR10 is a non-standard register that controls the mapping of
+	 * 0xa000 to MMIO or GTT memory.
+	 */
+	next_gr = 0x10;
+	write_next_gr_reg(0x0);
+
+	next_ar = 0;
+	for(i=0; i<=0x13; i++) {
+		write_next_ar_reg((*vga_mode_data_ptr)[mode_index].ar_regs[i]);
+	}
+	/* Spec says 0x8 for text modes, not done in practice */
+	write_next_ar_reg(0x00);
+
+	/* Ensure the Pixel Data Mask Register does not mask the pixel data */
+	EMGD_WRITE8(0xFF, EMGD_MMIO(mmio) + 0x3c6);
+
+	/* set DAC data value */
+	EMGD_WRITE8(0, EMGD_MMIO(mmio) + 0x3c8);
+
+	/* Load RAMDAC*/
+	switch(palette_type[mode_index]){
+	case 0:
+		color_bits = p64_color_bits;
+		colors = normal_colors;
+		palette_hack = 0;
+		break;
+	case 1:
+		color_bits = p16_color_bits;
+		colors = normal_colors;
+		palette_hack = 1;
+		break;
+	case 2:
+		color_bits = mono_color_bits;
+		colors = mono_colors;
+		palette_hack = 0;
+		break;
+	default:
+		break;
+	}
+	/* Program the Palette based on the mode. */
+	if (!vga_disable_default_palette_load) {
+		if (color_bits) {
+			set_palette_vga(mmio, 64, color_bits, colors, palette_hack);
+			set_3f_palette(mmio, 192);
+		} else {
+			set_256_palette(mmio);
+		}
+	}
+
+	/* Timings */
+	next_cr = 0;
+	for(i=0; i<=0x18; i++) {
+		write_next_cr_reg((*vga_mode_data_ptr)[mode_index].crtc_regs[i]);
+	}
+
+	return;
+}
+
+/*
+ * The HAL mode numbers match up to VGA modes in this way:
+ *   VGA Mode : HAL Mode
+ *      00         00
+ *      01         01
+ *      02         02
+ *      03         03
+ *      04         04
+ *      05         05
+ *      06         06
+ *      07         07
+ *      0d         0d
+ *      0e         0e
+ *      0f         11
+ *      10         12
+ *      00*        13
+ *      01*        14
+ *      02*        15
+ *      03*        16
+ *      00+        17
+ *      01+        17
+ *      02+        18
+ *      03+        18
+ *      07+        19
+ *      11         1A
+ *      12         1B
+ *      13         1C
+ */
+
+/* This can not be a static, since the vBIOS will use this as a global */
+vga_mode_data_t VB_CODE_SEG vga_mode_data[] = {
+	/*============================================================================*/
+	/* Modes 00h (Text 40 x 25 Colors 16 gray Font 8 x 8) - Table Entry 00h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08,	/* Text Columns Rows Font */
+		0x0800,			/* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x09,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0xA0,0xBF,0x1F,
+		 0x00,0xC7,0x06,0x07,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x14,0x1F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 01h (Text 40 x 25 Colors 16 gray Font 8 x 8) - Table Entry 01h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08,	/* Text Columns Rows Font */
+		0x0800,			/* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x09,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0xA0,0xBF,0x1F,
+		 0x00,0xC7,0x06,0x07,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x14,0x1F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 02h (Text 80 x 25 Colors 16 gray Font 8 x 8) - Table Entry 02h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x08,	/* Text Columns Rows Font */
+		0x1000,			/* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0xC7,0x06,0x07,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x1F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 03h (Text 80 x 25 Colors 16 Font 8 x 8) - Table Entry 03h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x08,	/* Text Columns Rows Font */
+		0x1000,			/* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0xC7,0x06,0x07,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x1F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 04h (Graphics 320 x 200 Colors 4 Font 8 x 8) - Table Entry 04h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08,	/* Text Columns Rows Font */
+		0x4000,	        /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x09,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0x80,0xBF,0x1F,
+		 0x00,0xC1,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x14,0x00,0x96,0xB9,0xA2,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x13,0x15,0x17,0x02,0x04,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x01,0x00,0x03,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x30,0x0F,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 05h (Graphics 320 x 200 Colors 4 gray Font 8 x 8) - Table 05h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08,	/* Text Columns Rows Font */
+		0x4000,	        /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x09,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0x80,0xBF,0x1F,
+		 0x00,0xC1,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x14,0x00,0x96,0xB9,0xA2,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x13,0x15,0x17,0x02,0x04,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x01,0x00,0x03,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x30,0x0F,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 06h (Graphics 640 x 200 Colors 2 Font 8 x 8) - Table Entry 06h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x08,	/* Text Columns Rows Font */
+		0x4000,	        /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x01,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F,
+		 0x00,0xC1,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x00,0x96,0xB9,0xC2,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x17,0x17,0x17,0x17,0x17,0x17,0x17,
+		 0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,
+		 0x01,0x00,0x01,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 07h (Text 80 x 25 Colors 2 Font 9 x 14) - Table Entry 07h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E,	/* Text Columns Rows Font */
+		0x1000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x00,0x03,0x00,0x03},
+
+		/* Miscellaneous output register value. */
+		0xA6,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x4D,0x0B,0x0C,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x28,0x0D,0x63,0xBA,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,
+		 0x10,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+		 0x0E,0x00,0x0F,0x08},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0A,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 08h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x10,	/* Text Columns Rows Font */
+		0x7D00,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x21,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x1F,0x96,0xB9,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 09h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x00,0x00,0x00,	/* Text Columns Rows Font */
+		0x0000,         /* Page size */
+
+		/* Sequencer register values. */
+		{0x00,0x00,0x00,0x00},
+
+		/* Miscellaneous output register value. */
+		0x00,
+
+		/* CRT Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00},
+
+		/* Attribute Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00,0x00,0x00,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 0Ah */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08,	/* Text Columns Rows Font */
+		0x4000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x00,0x00,0x00,0x03},
+
+		/* Miscellaneous output register value. */
+		0x23,
+
+		/* CRT Controller register values. */
+		{0x37,0x27,0x2D,0x37,0x31,0x15,0x04,0x11,
+		 0x00,0x47,0x06,0x07,0x00,0x00,0x00,0x00,
+		 0xE1,0x24,0xC7,0x14,0x08,0xE0,0xF0,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 0Bh */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x00,0x00,	/* Text Columns Rows Font */
+		0x0000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x29,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0x62,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x1F,0x96,0xB9,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x0F,0x00,0x00,0x08,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 0Ch */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x00,0x00,	/* Text Columns Rows Font */
+		0x0000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x29,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x1F,0x96,0xB9,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x0F,0x00,0x00,0x08,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 0Dh (Graphics 320 x 200 Colors 16 Font 8 x 8) - Table Entry 0Dh */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08,	/* Text Columns Rows Font */
+		0x2000,         /* Page size */
+
+		/* Sequencer register values. */
+		{0x09,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0x80,0xBF,0x1F,
+		 0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x14,0x00,0x96,0xB9,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 0Eh (Graphics 640 x 200 Colors 16 Font 8 x 8) - Table Entry 0Eh */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x08,	/* Text Columns Rows Font */
+		0x4000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F,
+		 0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x00,0x96,0xB9,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 0Fh */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E,	/* Text Columns Rows Font */
+		0x8000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x05,0x0F,0x00,0x00},
+
+		/* Miscellaneous output register value. */
+		0xA2,
+
+		/* CRT Controller register values. */
+		{0x60,0x4F,0x56,0x1A,0x50,0xE0,0x70,0x1F,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x5E,0x2E,0x5D,0x14,0x00,0x5E,0x6E,0x8B,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x08,0x00,0x00,0x18,0x18,0x00,0x00,
+		 0x00,0x08,0x00,0x00,0x00,0x18,0x00,0x00,
+		 0x0B,0x00,0x05,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x07,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Blank Entry - Table Entry 10h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E,	/* Text Columns Rows Font */
+		0x8000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x05,0x0F,0x00,0x00},
+
+		/* Miscellaneous output register value. */
+		0xA7,
+
+		/* CRT Controller register values. */
+		{0x5B,0x4F,0x53,0x17,0x50,0xBA,0x6C,0x1F,
+		 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x5E,0x2B,0x5D,0x14,0x0F,0x5F,0x0A,0x8B,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x00,0x00,0x04,0x07,0x00,0x00,
+		 0x00,0x01,0x00,0x00,0x04,0x07,0x00,0x00,
+		 0x01,0x00,0x05,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x07,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 0Fh; (Graphics 640 x 350 Colors 2 Font 8 x 14) - Table Entry 11h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E,	/* Text Columns Rows Font */
+		0x8000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0xA2,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x28,0x0F,0x63,0xBA,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x08,0x00,0x00,0x18,0x18,0x00,0x00,
+		 0x00,0x08,0x00,0x00,0x00,0x18,0x00,0x00,
+		 0x0B,0x00,0x05,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x05,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 10h; (Graphics 640 x 350 Colors 16 Font 8 x 14) - Table Entry 12h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E,	/* Text Columns Rows Font */
+		0x8000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0xA3,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x28,0x0F,0x63,0xBA,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 00h; (Text 40 x 25 Colors 16 gray Font 8 x 14) - Table Entry 13h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x0E,	/* Text Columns Rows Font */
+		0x0800,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x09,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0xA3,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0xA0,0xBF,0x1F,
+		 0x00,0x4D,0x0B,0x0C,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x14,0x1F,0x63,0xBA,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 01h; (Text 40 x 25 Colors 16 Font 8 x 14) - Table Entry 14h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x0E,	/* Text Columns Rows Font */
+		0x0800,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x09,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0xA3,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0xA0,0xBF,0x1F,
+		 0x00,0x4D,0x0B,0x0C,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x14,0x1F,0x63,0xBA,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+// ; Modes 02h; (Text 80 x 25 Colors 16 gray Font 8 x 14) - Table Entry 15h //
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E,	/* Text Columns Rows Font */
+		0x1000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0xA3,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x4D,0x0B,0x0C,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x28,0x1F,0x63,0xBA,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 03h; (Text 80 x 25 Colors 16 Font 8 x 14) - Table Entry 16h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x0E, /* Text Columns Rows Font */
+		0x1000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0xA3,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x4D,0x0B,0x0C,0x00,0x00,0x00,0x00,
+		 0x83,0x85,0x5D,0x28,0x1F,0x63,0xBA,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x08,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 00h+ ; 01h+ (Text 40 x 25 Colors 16 Font 9 x 16) - Table 17h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x10, /* Text Columns Rows Font */
+		0x0800,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x08,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x67,
+
+		/* CRT Controller register values. */
+		{0x2D,0x27,0x28,0x90,0x2B,0xA0,0xBF,0x1F,
+		 0x00,0x4F,0x0D,0x0E,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x14,0x1F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x0C,0x00,0x0F,0x08},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 02h+ ; 03h+ (Text 80 x 25 Colors 16 Font 9 x 16) - Table 18h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x10,	/* Text Columns Rows Font */
+		0x1000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x00,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x67,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x4F,0x0D,0x0E,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x1F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x0C,0x00,0x0F,0x08},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 07h+ (Text 80 x 25 Colors 2 Font 9 x 16) - Table Entry 19h */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x18,0x10,	/* Text Columns Rows Font */
+		0x1000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x00,0x03,0x00,0x02},
+
+		/* Miscellaneous output register value. */
+		0x66,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x55,0x81,0xBF,0x1F,
+		 0x00,0x4F,0x0D,0x0E,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x0F,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,
+		 0x10,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+		 0x0E,0x00,0x0F,0x08},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x10,0x0A,0x00,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 11h (Graphics 640 x 480 Colors 2 Font 8 x 16) - Table Entry 1Ah */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x1D,0x10,	/* Text Columns Rows Font */
+		0xA000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0xE3,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0x0B,0x3E,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0xEA,0x8C,0xDF,0x28,0x00,0xE7,0x04,0xC3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
+		 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
+		 0x01,0x00,0x01,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x01,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 12h (Graphics 640 x 480 Colors 16 Font 8 x 16) - Table Entry 1Bh */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x50,0x1D,0x10,	/* Text Columns Rows Font */
+		0xA000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x0F,0x00,0x06},
+
+		/* Miscellaneous output register value. */
+		0xE3,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0x0B,0x3E,
+		 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0xEA,0x8C,0xDF,0x28,0x00,0xE7,0x04,0xE3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,
+		 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+		 0x01,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x0F,
+		 0xFF},
+	},
+
+	/*============================================================================*/
+	/* Modes 13h (Graphics 320 x 200 Colors 256 Font 8 x 8) - Table Entry 1Ch */
+	/*----------------------------------------------------------------------------*/
+	{
+		0x28,0x18,0x08, /* Text Columns Rows Font */
+		0x2000,         /* Page size */
+
+		/* Sequencer register values, SR01-SR04. */
+		{0x01,0x0F,0x00,0x0E},
+
+		/* Miscellaneous output register value. */
+		0x63,
+
+		/* CRT Controller register values. */
+		{0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F,
+		 0x00,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
+		 0x9C,0x8E,0x8F,0x28,0x40,0x96,0xB9,0xA3,
+		 0xFF},
+
+		/* Attribute Controller register values. */
+		{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
+		 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
+		 0x41,0x00,0x0F,0x00},
+
+		/* Graphics Controller register values. */
+		{0x00,0x00,0x00,0x00,0x00,0x40,0x05,0x0F,
+		 0xFF}
+	},
+};
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/plb/clocks_plb.c b/drivers/gpu/drm/emgd/emgd/display/mode/plb/clocks_plb.c
new file mode 100644
index 0000000..f39e9d2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/plb/clocks_plb.c
@@ -0,0 +1,711 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: clocks_plb.c
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <io.h>
+
+#include <igd_mode.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <dsp.h>
+#include <utils.h>
+#include <mode.h>
+#include <pi.h>
+#include <sched.h>
+
+#include "drm_emgd_private.h"
+#include <plb/regs.h>
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*===========================================================================
+; File Global Data
+;--------------------------------------------------------------------------*/
+typedef struct _fixed_clock {
+	unsigned long dclk;
+	unsigned long n;
+	unsigned long m1;
+	unsigned long m2;
+	unsigned long p;
+}fixed_clock_t;
+
+static fixed_clock_t fixed_clock_table[] =  {
+	/* Clock    N    M1    M2  Post Div */
+    { 43163, 0x03, 0x12, 0x06, 0x84},  /* 43.163 GTF for 640x480 @ 100Hz */
+    { 81624, 0x03, 0x18, 0x04, 0x82},  /* 81.624MHz */
+	{0xffffffff, 0x00, 0x00, 0x00, 0x00}
+};
+
+/*!
+ * This function translates from the calculated M value to the M1, M2
+ * register values.
+ *
+ * @param m
+ * @param *m1
+ * @param *m2
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int calculate_m1_m2(unsigned long m,
+	unsigned long *m1,
+	unsigned long *m2)
+{
+	unsigned long current_m1, current_m2;
+
+	EMGD_DEBUG("Enter calculate_m1_m2 %d", 1);
+
+	/* ori was in steps of 2*/
+	for(current_m1 = (10+2); current_m1 <= (20+2); current_m1 += 1) {
+		for(current_m2 = (5+2); current_m2 <= (9+2); current_m2++)  {
+			if((current_m1 * 5 + current_m2) == m) {
+				*m1 = current_m1 - 2;
+				*m2 = current_m2 - 2;
+				return 0;
+			}
+
+		}
+	}
+
+	EMGD_DEBUG("M1, M2 not found for M == %ld", m);
+	return 1;
+}
+
+#define MAX_M 120 //216
+#define MIN_M 70 //96
+
+#define MAX_N 8 //16
+#define MIN_N 3
+
+#define MAX_P 80 //126
+#define MIN_P 5 //2
+
+#define LVDS_MAX_P 98
+#define LVDS_MIN_P 7
+
+#define FIX_P2_LO 5 //126
+#define FIX_P2_HI 10 //2
+
+#define MAX_P1 8 //126
+#define MIN_P1 1 //2
+
+#define REF_FREQ 96000 //48000
+#define MAX_VCO 2800000 //1400000
+#define MAX_FP 200000
+/* For LVDS port */
+#define LVDS_FIX_P2_LO 7
+#define LVDS_FIX_P2_HI 14
+
+/*!
+ *
+ * @param dclk
+ * @param ref_freq
+ * @param m1
+ * @param m2
+ * @param n
+ * @param p
+ * @param min_vco
+ * @param target_error
+ * @param port_type
+ * @param dual_channel
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int calculate_clock(unsigned long dclk,
+	unsigned long ref_freq,
+	unsigned long *m1,
+	unsigned long *m2,
+	unsigned long *n,
+	unsigned long *p,
+	unsigned long min_vco,
+	unsigned long target_error,
+	unsigned long port_type,
+	unsigned long dual_channel)
+
+{
+	unsigned long p1;
+	unsigned long p2;
+	/* Parameters */
+    unsigned long freqmult_p2;
+
+	/* Intermidiate variables */
+	unsigned long  pdiv;
+	unsigned long  target_vco, actual_freq, actual_vco;
+	long freq_error, min_error;
+
+	unsigned long current_m, current_n, current_p1;
+	unsigned long best_m = 0;
+	unsigned long best_n = 0;
+	unsigned long best_p1 = 0;
+
+
+	EMGD_DEBUG("Enter calculate_clock");
+
+	min_error = 100000;
+
+	if (dclk > MAX_FP) {
+		freqmult_p2 = FIX_P2_LO;
+	} else {
+		freqmult_p2 = FIX_P2_HI;
+	}
+
+	/* For LVDS port */
+	if(port_type==IGD_PORT_LVDS){
+		/* Test if we are dual channel */
+		if(dual_channel){
+			freqmult_p2=LVDS_FIX_P2_LO;
+		} else{
+			freqmult_p2=LVDS_FIX_P2_HI;
+		}
+
+	}
+
+	for(current_m = MIN_M; current_m <= MAX_M; current_m++) {
+		for(current_n = MIN_N; current_n < MAX_N; current_n++) {
+			for(current_p1 = MIN_P1; current_p1 <= MAX_P1; current_p1++) {
+
+				pdiv = freqmult_p2 * current_p1;
+				target_vco = dclk * pdiv;
+
+				if ((target_vco <= MAX_VCO) && (target_vco >= min_vco)) {
+					actual_freq = (ref_freq * current_m) /
+						(current_n * pdiv);
+					actual_vco = actual_freq * pdiv;
+					freq_error = 10000 - (dclk * 10000 / actual_freq);
+
+					if (freq_error < 0) {
+						freq_error = -freq_error;
+					}
+					if (freq_error < min_error)  {
+						best_n = current_n;
+						best_m = current_m;
+						best_p1 = current_p1;
+						min_error = freq_error;
+					}
+					if (min_error == 0) {
+						break;
+					}
+				}
+			}
+			if (min_error == 0) {
+				break;
+			}
+		}
+		if (min_error == 0) {
+			break;
+		}
+	}
+	/*
+	 * No clock found that meets error requirement
+	 */
+	if (min_error > (long)target_error) {
+		return 1;
+	}
+
+
+	/* Translate M,N,P to m1,m2,n,p register values */
+	*n = best_n - 2;
+	if(calculate_m1_m2(best_m, m1, m2)) {
+		/* No M1, M2 match for M */
+		return 1;
+	}
+
+	p1 = (1 << (best_p1-1));
+	if((freqmult_p2-5)) {
+		p2 = 0x0;
+	}
+	else {
+		p2 = 0x1;
+	}
+	/* Set p2 for LVDS */
+	if(port_type==IGD_PORT_LVDS){
+		if(dual_channel){
+			p2=0x1;
+		}else{
+			p2=0x0;
+		}
+
+	}
+	*p = (  p1 | (p2<<8) );
+
+	return 0;
+}
+
+#define MIN_VCO_PASS1 2000000 //1000000
+#define MIN_VCO_PASS2 1400000 //930000
+
+/*!
+ *
+ * @param dclk
+ * @param ref_freq
+ * @param m1
+ * @param m2
+ * @param n
+ * @param p
+ * @param target_error
+ * @param port_type
+ * @param dual_channel
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int get_clock(unsigned long dclk,
+	unsigned long ref_freq,
+	unsigned long *m1,
+	unsigned long *m2,
+	unsigned long *n,
+	unsigned long *p,
+	unsigned long target_error,
+	unsigned long port_type,
+	unsigned long dual_channel)
+{
+	fixed_clock_t *fixed = fixed_clock_table;
+
+	EMGD_DEBUG("Enter get_clock");
+
+	/*
+	 * First check for a fixed clock from the table. These are ones that
+	 * can't be calculated correctly.
+	 */
+	while(fixed->dclk != 0xffffffff) {
+		if(fixed->dclk == dclk) {
+			EMGD_DEBUG("Using Fixed Clock From table for clock %ld", dclk);
+			*m1 = fixed->m1;
+			*m2 = fixed->m2;
+			*n = fixed->n;
+			*p = fixed->p;
+			return 0;
+		}
+		fixed++;
+	}
+
+
+	/*
+	 * No fixed clock found so calculate one.
+	 */
+	EMGD_DEBUG("Calculating dynamic clock for clock %ld", dclk);
+
+	/*
+	 * First pass try to find a clock with min_vco at 1000000.
+	 * If a clock doesn't come out then try 930000.
+	 */
+	if(calculate_clock(dclk, ref_freq, m1, m2, n, p, MIN_VCO_PASS1,
+		   target_error,port_type,dual_channel)) {
+		if(calculate_clock(dclk, ref_freq, m1, m2, n, p, MIN_VCO_PASS2,
+			   target_error,port_type,dual_channel)) {
+			/* No usable clock */
+			EMGD_ERROR("Could not calculate clock %ld, returning default.",
+				dclk);
+			*m1 = 0x14;
+			*m2 = 0xc;
+			*n = 0x3;
+			*p = 0x82;
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
+#define TARGET_ERROR 46
+
+/*!
+ *
+ * @param emgd_crtc
+ * @param clock
+ * @param dclk
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int kms_program_clock_plb(emgd_crtc_t *emgd_crtc,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	unsigned long m1, m2, n, p;
+	unsigned long control;
+	unsigned long ref_freq;
+	int ret;
+	unsigned long port_mult, vga_mult;
+	unsigned long dual_channel = 0;
+	unsigned long index;
+	unsigned long pt;
+	struct drm_device  *dev          = NULL;
+	igd_display_pipe_t *pipe         = NULL;
+	igd_context_t      *context      = NULL;
+	igd_display_port_t *port         = NULL;
+	struct drm_encoder *encoder      = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+
+	EMGD_DEBUG("Enter program_clock");
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			pt = port->port_type;
+			break;
+		}
+	}
+	if (!port) {
+		EMGD_ERROR_EXIT("No port");
+		return -1;
+	}
+
+	if (dclk > 100000) {        /*	100-200 MHz	*/
+
+		port_mult = 1;
+	}
+	else if (dclk > 50000) {    /*	50-100 Mhz	*/
+
+		port_mult = 2;
+	}
+	else {                      /*	25-50 Mhz	*/
+
+		port_mult = 4;
+	}
+
+	/*
+	 * Clock Multiplier : sDVO ports on all plb chipsets
+	 */
+	if (pt == IGD_PORT_DIGITAL) {
+
+		dclk *= port_mult;
+	}
+
+	vga_mult = EMGD_READ32(context->device_context.virt_mmadr + clock->dpll_control) & 0x3;
+
+	/* For Int-LVDS need to find out if its dual channel and pass
+	 * that info into caluculating for p2. Apperently halving
+	 * of dot-clock is also required by Ch7017 when operating in
+	 * dual channel
+	 */
+	if (pt == IGD_PORT_LVDS) {
+		/* Find PD_ATTR_ID_2_CHANNEL_PANEL attr value*/
+		pi_pd_find_attr_and_value(port,
+				PD_ATTR_ID_2_CHANNEL_PANEL,
+				0/*no PD_FLAG for 2_channel*/,
+				NULL,
+				&dual_channel);
+	}
+
+	/* For external clock sources always use ref_clock == dclk */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		ref_freq = dclk;
+	} else {
+		ref_freq = 96000;
+	}
+	/* LVDS reference clock can be 96 or 100 MHz. However there
+	 * are no mention in the specification to specify which register
+	 * to select/set this.
+	 */
+
+	/* When the clock source is provided externally by the port driver,
+	 * the allowed error range is 0. */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		ret = get_clock(dclk, ref_freq, &m1, &m2, &n, &p, 0
+				,pt,dual_channel);
+	} else {
+		ret = get_clock(dclk, ref_freq, &m1, &m2, &n, &p, TARGET_ERROR
+				,pt,dual_channel);
+	}
+
+	if(ret) {
+		EMGD_ERROR("Clock %ld could not be programmed", dclk);
+		return ret;
+	}
+
+	/* Disable DPLL, Write an 0x80 into P for saftey */
+	control = 0x10000000 | (0x80<<clock->p_shift) | BIT26 | vga_mult;
+	EMGD_WRITE32(control, context->device_context.virt_mmadr + clock->dpll_control);
+
+	/* Program N, M1,and M2 */
+	EMGD_WRITE32((n<<16) | (m1<<8) | m2, context->device_context.virt_mmadr + clock->mnp);
+
+	/* Enable DPLL, Disable VGAm Mode and sitck in new P values */
+	if(pt == IGD_PORT_LVDS){
+		/* If LVDS set the appropriate bits for mode select */
+		control = (BIT31 | BIT28 | BIT27 )
+			| (p<<clock->p_shift) | vga_mult;
+
+		if(port->attr_list) {
+
+			for(index = 0; index < port->attr_list->num_attrs; index++) {
+
+				/* Set spread spectrum and pulse phase */
+				if(port->attr_list->attr[index].id == PD_ATTR_ID_SSC) {
+
+					/*
+					 * Pulse Phase for Poulsbo only has valid values between
+					 * 3 and 9
+					 */
+					if(port->attr_list->attr[index].value >= 3 &&
+						port->attr_list->attr[index].value <= 9) {
+
+						control |= BIT13 | BIT14;
+						/*
+						 * Set the Pulse Phase to the clock phase specified by
+						 * the user
+						 */
+						control |= (port->attr_list->attr[index].value<<9);
+					}
+					break;
+				}
+			}
+		}
+	} else{
+	/* else DAC/SDVO */
+		control = (BIT31 | BIT28 | BIT26) | (p<<clock->p_shift) | vga_mult;
+	}
+	/*
+	 * Poulsbo has high speed clock on always
+	 */
+	control |= BIT30;
+
+
+
+	/* Set the clock source correctly based on PD settings */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		control |= port->clock_bits;
+	} else {
+		control |= port->clock_bits & ~0x00006000;
+	}
+
+	/* sDVO Multiplier bits[7:0] */
+	if (pt == IGD_PORT_DIGITAL) {
+
+		if (port_mult == 2) {
+
+			control |= (1 << 4);
+
+		} else if (port_mult == 4) {
+
+			control |= (3 << 4);
+		}
+	}
+
+	EMGD_WRITE32(control, context->device_context.virt_mmadr + clock->dpll_control);
+
+	/* We must wait for 150 us for the dpll clock to warm up */
+	OS_SLEEP(150);
+	pipe->dclk = dclk;
+
+	return 0;
+}
+
+
+/*!
+ *
+ * @param display
+ * @param clock
+ * @param dclk
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int program_clock_plb(igd_display_context_t *display,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	unsigned long m1, m2, n, p;
+	unsigned long control;
+	unsigned long ref_freq;
+	int ret;
+	igd_display_port_t *port;
+	unsigned long port_mult, vga_mult;
+	unsigned long dual_channel = 0;
+	unsigned long index;
+
+	EMGD_DEBUG("Enter program_clock");
+
+	port=PORT_OWNER(display);
+
+	if (dclk > 100000) {        /*	100-200 MHz	*/
+
+		port_mult = 1;
+	}
+	else if (dclk > 50000) {    /*	50-100 Mhz	*/
+
+		port_mult = 2;
+	}
+	else {                      /*	25-50 Mhz	*/
+
+		port_mult = 4;
+	}
+
+	/*
+	 * Clock Multiplier : sDVO ports on all plb chipsets
+	 */
+	if (port->port_type == IGD_PORT_DIGITAL) {
+
+		dclk *= port_mult;
+	}
+
+	vga_mult = READ_MMIO_REG(display, clock->dpll_control) & 0x3;
+
+	/* For Int-LVDS need to find out if its dual channel and pass
+	 * that info into caluculating for p2. Apperently halving
+	 * of dot-clock is also required by Ch7017 when operating in
+	 * dual channel
+	 */
+	if (port->port_type == IGD_PORT_LVDS) {
+		/* Find PD_ATTR_ID_2_CHANNEL_PANEL attr value*/
+		pi_pd_find_attr_and_value(PORT_OWNER(display),
+				PD_ATTR_ID_2_CHANNEL_PANEL,
+				0/*no PD_FLAG for 2_channel*/,
+				NULL,
+				&dual_channel);
+	}
+
+	/* For external clock sources always use ref_clock == dclk */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		ref_freq = dclk;
+	} else {
+		ref_freq = 96000;
+
+		/* If SSC is enabled, then the reference frequency needs to be
+		 * 100000 instead of 96000.
+		 */
+		for(index = 0; index < port->attr_list->num_attrs; index++) {
+			if(port->attr_list->attr[index].id == PD_ATTR_ID_SSC) {
+				ref_freq = 100000;
+				break;
+			}
+		}
+	}
+	/* LVDS reference clock can be 96 or 100 MHz. However there
+	 * are no mention in the specification to specify which register
+	 * to select/set this.
+	 */
+
+	/* When the clock source is provided externally by the port driver,
+	 * the allowed error range is 0. */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		ret = get_clock(dclk, ref_freq, &m1, &m2, &n, &p, 0
+				,port->port_type,dual_channel);
+	} else {
+		ret = get_clock(dclk, ref_freq, &m1, &m2, &n, &p, TARGET_ERROR
+				,port->port_type,dual_channel);
+	}
+
+	if(ret) {
+		EMGD_ERROR("Clock %ld could not be programmed", dclk);
+		return ret;
+	}
+
+	/* Disable DPLL, Write an 0x80 into P for saftey */
+	control = 0x10000000 | (0x80<<clock->p_shift) | BIT26 | vga_mult;
+	WRITE_MMIO_REG(display, clock->dpll_control, control);
+
+	/* Program N, M1,and M2 */
+	WRITE_MMIO_REG(display, clock->mnp, (n<<16) | (m1<<8) | m2);
+
+	/* Enable DPLL, Disable VGAm Mode and sitck in new P values */
+	if(port->port_type==IGD_PORT_LVDS){
+		/* If LVDS set the appropriate bits for mode select */
+		control = (BIT31 | BIT28 | BIT27 )
+			| (p<<clock->p_shift) | vga_mult;
+
+		if(port->attr_list) {
+
+			for(index = 0; index < port->attr_list->num_attrs; index++) {
+
+				/* Set spread spectrum and pulse phase */
+				if(port->attr_list->attr[index].id == PD_ATTR_ID_SSC) {
+
+					/*
+					 * Pulse Phase for Poulsbo only has valid values between
+					 * 3 and 9
+					 */
+					if(port->attr_list->attr[index].value >= 3 &&
+						port->attr_list->attr[index].value <= 9) {
+
+						control |= BIT13 | BIT14;
+						/*
+						 * Set the Pulse Phase to the clock phase specified by
+						 * the user
+						 */
+						control |= (port->attr_list->attr[index].value<<9);
+					}
+					break;
+				}
+			}
+		}
+	} else{
+	/* else DAC/SDVO */
+		control = (BIT31 | BIT28 | BIT26) | (p<<clock->p_shift) | vga_mult;
+	}
+	/*
+	 * Poulsbo has high speed clock on always
+	 */
+	control |= BIT30;
+
+
+
+	/* Set the clock source correctly based on PD settings */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		control |= port->clock_bits;
+	} else {
+		control |= port->clock_bits & ~0x00006000;
+	}
+
+	/* sDVO Multiplier bits[7:0] */
+	if (port->port_type == IGD_PORT_DIGITAL) {
+
+		if (port_mult == 2) {
+
+			control |= (1 << 4);
+
+		} else if (port_mult == 4) {
+
+			control |= (3 << 4);
+		}
+	}
+
+	WRITE_MMIO_REG(display, clock->dpll_control, control);
+
+	/* We must wait for 150 us for the dpll clock to warm up */
+	OS_SLEEP(150);
+	PIPE(display)->dclk = dclk;
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/plb/kms_mode_plb.c b/drivers/gpu/drm/emgd/emgd/display/mode/plb/kms_mode_plb.c
new file mode 100644
index 0000000..ec1546d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/plb/kms_mode_plb.c
@@ -0,0 +1,1102 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: kms_mode_plb.c
+ * $Revision: 1.2 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+
+#include <igd.h>
+#include <igd_pwr.h>
+#include <vga.h>
+#include <pi.h>
+#include <plb/regs.h>
+#include "drm_emgd_private.h"
+#include "../cmn/mode_dispatch.h"
+#include "../cmn/match.h"
+#include "mode_plb.h"
+#include <math_fix.h>
+#include <memory.h>
+
+
+/*------------------------------------------------------------------------------
+ * Function Prototypes
+ *----------------------------------------------------------------------------*/
+int kms_mode_get_stride_stereo_plb(igd_display_pipe_t *pipe,
+		igd_display_plane_t *plane,
+		unsigned long *stride,
+		unsigned long *stereo,
+		unsigned long flags);
+void kms_program_pipe_vga_plb(emgd_crtc_t *emgd_crtc);
+static void kms_program_pipe_plb(emgd_crtc_t *emgd_crtc);
+static void kms_set_pipe_pwr_plb(emgd_crtc_t *emgd_crtc, unsigned long enable);
+static void kms_program_plane_plb(emgd_crtc_t *emgd_crtc, unsigned long status);
+static void kms_set_plane_pwr_plb(emgd_crtc_t *emgd_crtc, unsigned long enable);
+static int set_color_correct_plb(emgd_crtc_t *emgd_crtc);
+
+
+extern int kms_program_clock_plb(emgd_crtc_t *emgd_crtc,
+	igd_clock_t *clock, unsigned long dclk);
+extern int wait_for_vblank_plb(unsigned char *mmio,
+	unsigned long pipe_reg);
+extern void kms_program_pipe_vga(emgd_crtc_t *emgd_crtc,
+	igd_timing_info_t *timings);
+extern void disable_vga_plb(unsigned char *mmio);
+
+
+/*------------------------------------------------------------------------------
+ * Global Variables
+ *----------------------------------------------------------------------------*/
+extern mode_data_plb_t device_data_plb[1];
+
+
+/*------------------------------------------------------------------------------
+ * KMS Dispatch Table
+ *----------------------------------------------------------------------------*/
+mode_kms_dispatch_t mode_kms_dispatch_plb = {
+	kms_program_pipe_plb,
+	kms_set_pipe_pwr_plb,
+	kms_program_plane_plb,
+	kms_set_plane_pwr_plb,
+	NULL,
+	NULL,
+	NULL,                       /* kms_get_vblank_counter */
+	kms_match_mode,
+};
+
+
+/*!
+ * Get the stride and stereo values based on the display.  This is also used
+ * by the MI instructions.
+ *
+ * @param pipe Pointer to hardware device instance data
+ * @param plane Pointer to hardware device instance data
+ * @param stride
+ * @param stereo
+ * @param flags Should the stereo be for the frontbuffer or backbuffer?
+ *
+ * @return stride - Stride of the display
+ * @return stereo - Stereo address of the display
+ */
+int kms_mode_get_stride_stereo_plb(igd_display_pipe_t *pipe,
+	igd_display_plane_t *plane,
+	unsigned long *stride,
+	unsigned long *stereo,
+	unsigned long flags)
+{
+	unsigned long pitch = plane->fb_info->screen_pitch;
+	igd_timing_info_t *timing = pipe->timing;
+	unsigned long base_offset;
+
+	base_offset = plane->fb_info->visible_offset;
+
+	*stride = pitch;
+	*stereo = 0;
+
+	/* For field replication, valid for interlaced modes only
+	 *     set stereo = fb_base,
+	 *         stride = pitch
+	 */
+	if (timing->mode_info_flags & IGD_SCAN_INTERLACE) {
+
+		if(timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* Interlaced + Line double flags means field replication.
+			 * same lines are sent for both fields. Program the
+			 * second eye to be same as the first.
+			 */
+			*stereo = base_offset;
+		} else {
+			/* Regular interlaced. Second eye starts on line 2.
+			 * Skip every other line.
+			 */
+			*stereo = base_offset + pitch;
+			*stride = pitch * 2;
+		}
+	}
+
+	return 0;
+}
+
+
+
+/*!
+ *
+ * @param emgd_crtc
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL if color attributes not found
+ */
+static int set_color_correct_plb(emgd_crtc_t *emgd_crtc)
+{
+	const int        MID_PIXEL_VAL    = 125;
+	const int        MAX_PIXEL_VAL    = 255;
+	const int        NUM_PALETTE_ENTRIES = 256;
+
+	struct drm_device  *dev = NULL;
+	igd_context_t      *context = NULL;
+	igd_display_port_t *port = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	struct drm_encoder *encoder = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+
+	unsigned int     gamma_r_max_24i_8f, gamma_r_min_24i_8f;
+	unsigned int     gamma_g_max_24i_8f, gamma_g_min_24i_8f;
+	unsigned int     gamma_b_max_24i_8f, gamma_b_min_24i_8f;
+	unsigned int     new_gamma_r_24i_8f, new_gamma_g_24i_8f;
+	unsigned int     new_gamma_b_24i_8f;
+	unsigned int     gamma_normal_r_24i_8f, gamma_normal_g_24i_8f;
+	unsigned int     gamma_normal_b_24i_8f;
+	int              brightness_factor_r, brightness_factor_g;
+	int              brightness_factor_b;
+	int              contrast_factor_r, contrast_factor_g;
+	int              contrast_factor_b;
+
+	unsigned int      *palette;
+	unsigned int      i;
+
+	igd_range_attr_t *gamma_attr      = NULL, *contrast_attr = NULL;
+	igd_range_attr_t *brightness_attr = NULL;
+	igd_attr_t       *hal_attr_list   = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			break;
+		}
+	}
+	if (!port) {
+		EMGD_ERROR_EXIT("No port being used.");
+		return -IGD_ERROR_INVAL;
+	}
+	hal_attr_list  = port->attributes;
+
+	/* Using OS_ALLOC to avoid using > 1024 on stack (frame size warning ) */
+	palette = OS_ALLOC(sizeof (unsigned int) * NUM_PALETTE_ENTRIES);
+
+	/* start with a fresh palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		palette[i] = (i << 16) | (i << 8) | i;
+	}
+
+	/* get a pointer to gamma, contrast, and brightness attr */
+	i = 0;
+
+	while (PD_ATTR_LIST_END != hal_attr_list[i].id) {
+		switch (hal_attr_list[i].id) {
+		case PD_ATTR_ID_FB_GAMMA:
+			gamma_attr      = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_BRIGHTNESS:
+			brightness_attr = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_CONTRAST:
+			contrast_attr   = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		default:
+			break;
+		}
+
+		i++;
+	}
+
+	if(!gamma_attr || !brightness_attr || !contrast_attr) {
+		EMGD_ERROR("Color Correction Atrributes not found!");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Get the max and min */
+	gamma_r_max_24i_8f = ((gamma_attr->max >> 16) & 0xFF) << 3;
+	gamma_g_max_24i_8f = ((gamma_attr->max >>  8) & 0xFF) << 3;
+	gamma_b_max_24i_8f =  (gamma_attr->max        & 0xFF) << 3;
+
+	gamma_r_min_24i_8f = ((gamma_attr->min >> 16) & 0xFF) << 3;
+	gamma_g_min_24i_8f = ((gamma_attr->min >>  8) & 0xFF) << 3;
+	gamma_b_min_24i_8f =  (gamma_attr->min        & 0xFF) << 3;
+
+	/* The new gamma values are in 3i.5f format, but we must convert it
+	 * to 24i.8f format before passing it to OS_POW_FIX
+	 */
+	new_gamma_r_24i_8f = ((gamma_attr->current_value >> 16) & 0xFF) << 3;
+	new_gamma_g_24i_8f = ((gamma_attr->current_value >> 8) & 0xFF) << 3;
+	new_gamma_b_24i_8f = (gamma_attr->current_value & 0xFF) << 3;
+
+	/* make sure the new gamma is within range */
+	new_gamma_r_24i_8f = OS_MIN(gamma_r_max_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_r_24i_8f = OS_MAX(gamma_r_min_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_g_24i_8f = OS_MIN(gamma_g_max_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_g_24i_8f = OS_MAX(gamma_g_min_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_b_24i_8f = OS_MIN(gamma_b_max_24i_8f, new_gamma_b_24i_8f);
+	new_gamma_b_24i_8f = OS_MAX(gamma_b_min_24i_8f, new_gamma_b_24i_8f);
+
+
+	gamma_normal_r_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_r_24i_8f);
+
+	gamma_normal_g_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_g_24i_8f);
+
+	gamma_normal_b_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_b_24i_8f);
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		unsigned int new_gamma;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* Note that we do not try to calculate the gamma if it
+		 * is 1.0, e.g. 0x100.  This is to avoid round-off errors
+		 */
+
+		/* red: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_r_24i_8f) {
+			cur_color  = (cur_palette >> 16) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_r_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_r_24i_8f;
+			palette[i] &= 0x00FFFF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_g_24i_8f) {
+			cur_color  = (cur_palette >> 8) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_g_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_g_24i_8f;
+			palette[i] &= 0xFF00FF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_b_24i_8f) {
+			cur_color  = cur_palette & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_b_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_b_24i_8f;
+			palette[i] &= 0xFFFF00;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF);
+		}
+	}
+
+
+	/* Brightness correction */
+	brightness_factor_r = (brightness_attr->current_value >> 16) & 0xFF;
+	brightness_factor_g = (brightness_attr->current_value >> 8) & 0xFF;
+	brightness_factor_b = brightness_attr->current_value & 0xFF;
+
+	/* The factors are offset by 0x80 because 0x80 is 0 correction */
+	brightness_factor_r -= 0x80;
+	brightness_factor_g -= 0x80;
+	brightness_factor_b -= 0x80;
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int          new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 16) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_r;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0x00FFFF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+
+		/* green: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 8) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_g;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFF00FF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+
+		/* blue: calculate and make sure the result is within range */
+		cur_color     =  cur_palette & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_b;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFFFF00;
+		palette[i]    |= OS_MAX(new_pixel_val, 0) & 0xFF;
+	}
+
+
+	/* contrast correction */
+	contrast_factor_r = (contrast_attr->current_value >> 16) & 0xFF;
+	contrast_factor_g = (contrast_attr->current_value >> 8) & 0xFF;
+	contrast_factor_b = contrast_attr->current_value & 0xFF;
+
+	/* make sure values are within range */
+	contrast_factor_r -= 0x80;
+	contrast_factor_g -= 0x80;
+	contrast_factor_b -= 0x80;
+
+
+	/* We're doing integer division in this loop using 16i.16f
+	 * integers.  The result will then be converted back into a
+	 * regular, 32-bit integer
+	 */
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		if (0 != contrast_factor_r ) {
+			cur_color     = (cur_palette >> 16) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_r);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0x00FFFF;  /* clear out the R color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0 != contrast_factor_g ) {
+			cur_color     = (cur_palette >> 8) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_g);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFF00FF;  /* clear out the G color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0 != contrast_factor_b) {
+			cur_color     = cur_palette & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_b);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFFFF00;  /* clear out the B color */
+			palette[i]    |=   OS_MAX(new_pixel_val, 0) & 0xFF;
+		}
+	}
+
+
+	/* write the new values in the palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		EMGD_WRITE32(palette[i], context->device_context.virt_mmadr +
+			pipe->palette_reg + i*4);
+	}
+	OS_FREE(palette);
+
+	return 0;
+}
+
+
+
+/*!
+ *
+ * @param emgd_crtc Pointer to hardware device instance data
+ *
+ * @return void
+ */
+void kms_program_pipe_vga_plb(
+	emgd_crtc_t *emgd_crtc)
+{
+	struct drm_device  *dev          = NULL;
+	igd_timing_info_t  *timing       = NULL;
+	igd_display_pipe_t *pipe         = NULL;
+	igd_context_t      *context      = NULL;
+	struct drm_encoder *encoder      = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	igd_display_port_t *port         = NULL;
+	unsigned long vga_control;
+	unsigned long upscale = 0;
+	int centering = 1;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			break;
+		}
+	}
+
+#ifdef CONFIG_MICRO
+	/*
+	 * We cannot set a VGA mode unless the display planes are turned off.
+     * This becomes evident during a Windows BSOD.  Since neither Windows
+     * nor IEGD got a chance to turn off these registers, and the VGA mode was
+     * set by the VBIOS, the screen gets corrupted.  In order to fix this
+     * problem, we will turn the cursor and display planes here.
+     *
+     * Note: Removing previous partial-fix in favor of this complete one.
+	 */
+    /* Cursor A */
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x70080);
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x70084);
+    /* Cursor B */
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x700C0);
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x700C4);
+    /* Display A */
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x70180);
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x70184);
+    /* Display B */
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x71180);
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x71184);
+    /* Display C */
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x72180);
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + 0x72184);
+#endif
+
+	/*
+	 * VGA Plane can attach to only one pipe at a time. LVDS can
+	 * only attach to pipe B. We need to use the display passed to
+	 * determine the pipe number to use. (Plba is same as Alm).
+	 */
+
+	/*
+	 * We can come here with following cases:
+	 *   1. magic->vga    CRT, DVI type displays
+	 *   2. native->vga   int-lvds, and up-scaling lvds displays
+	 *   3. pipe->vga     TV and other unscaled-lvds displays
+	 */
+	vga_control = EMGD_READ32(context->device_context.virt_mmadr + 0x71400);
+	vga_control &= 0x18e3ff00;
+	vga_control |= 0x8e;
+
+	timing = pipe->timing;
+	if(!timing->extn_ptr) {
+		EMGD_ERROR_EXIT("No Extension pointer in program_pipe_vga_plb");
+		return;
+	}
+
+	if (port) {
+		/* Find UPSCALING attr value*/
+		pi_pd_find_attr_and_value(port,
+			PD_ATTR_ID_PANEL_FIT,
+			0,/*no PD_FLAG for UPSCALING */
+			NULL, /* dont need the attr ptr*/
+			&upscale);
+		/* this PI func will not modify value of upscale if attr does not exist */
+	}
+
+	/* magic->vga or native->vga cases */
+	if ((timing->width == 720 && timing->height == 400) || upscale) {
+		centering = 0;
+	}
+
+	/* Enable border */
+	if((timing->width >= 800) && !upscale) {
+		EMGD_DEBUG("Enable VGA Border");
+		vga_control |= (1L<<26);
+	}
+
+	if(timing->width == 640) {
+		EMGD_DEBUG("Enable Nine Dot Disable");
+		vga_control |= (1L<<18);
+	}
+
+	if(centering) {
+		EMGD_DEBUG("Enable VGA Center Centering");
+		vga_control |= 1L<<24;
+
+		if(timing->height >= 960) {
+			if(timing->width >= 1280) {
+				EMGD_DEBUG("Enable VGA 2x (Nine Dot Disable)");
+				vga_control |= (1L<<30) | (1L<<18);
+			}
+		}
+	} else {
+		if (port) {
+			if(port->port_type == IGD_PORT_LVDS) {
+				EMGD_DEBUG("Enable VGA Upper-Left Centering & Nine Dot Disable");
+				vga_control |= (1L<<25 | (1L<<18));
+			} else if (upscale) {
+				EMGD_DEBUG("Enable VGA Center Upper-left for upscale ports");
+				vga_control |= 1L<<25;
+			}
+		}
+	}
+
+	if(pipe->pipe_num) {
+		vga_control |= 1L<<29;
+	}
+
+	kms_program_pipe_vga(emgd_crtc, (igd_timing_info_t *)timing->extn_ptr);
+	EMGD_WRITE32(vga_control, context->device_context.virt_mmadr + 0x71400);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+/**
+ * kms_set_pipe_pwr_plb
+ *
+ * Turns the pipe ON or OFF depending on the input
+ *
+ * @param emgd_crtc (IN) specifies the pipe to change
+ * @param enable    (IN) TRUE to enable pipe, FALSE to disable
+ *
+ * @return
+ */
+static void kms_set_pipe_pwr_plb(emgd_crtc_t *emgd_crtc, unsigned long enable)
+{
+	unsigned long       pipe_conf;
+	struct drm_device  *dev;
+	igd_display_pipe_t *pipe;
+	igd_context_t      *context;
+
+
+	EMGD_TRACE_ENTER;
+
+
+	dev       = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context   = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	pipe      = emgd_crtc->igd_pipe;
+	pipe_conf = device_data_plb->pipe_preserve &
+		EMGD_READ32(context->device_context.virt_mmadr + pipe->pipe_reg);
+
+
+	/* Do nothing if current power state is same as what we want to set */
+	/* The PIPE_ENABLE bit is at bit-position 31 */
+	if ( (enable << 31) == (pipe_conf & PIPE_ENABLE) ){
+
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+
+	if (!enable) {
+		/* Disable pipe */
+		EMGD_WRITE32(pipe_conf & ~PIPE_ENABLE,
+			context->device_context.virt_mmadr + pipe->pipe_reg);
+
+		EMGD_DEBUG("Set Pipe Power: OFF");
+
+	} else {
+		/* Enable pipe */
+		EMGD_WRITE32(pipe_conf | PIPE_ENABLE,
+			context->device_context.virt_mmadr + pipe->pipe_reg);
+
+		EMGD_DEBUG("Set Pipe Power: ON");
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+/*!
+ * This function programs the Timing registers and clock registers and
+ * other control registers for PIPE.
+ *
+ * @param emgd_crtc
+ * @param status
+ *
+ * @return void
+ */
+static void kms_program_pipe_plb(emgd_crtc_t *emgd_crtc)
+{
+	unsigned long       timing_reg;
+	unsigned long       pipe_conf;
+	unsigned long       hactive, vactive;
+	igd_timing_info_t  *pTimings;
+	unsigned long       temp;
+	struct drm_device  *dev     = NULL;
+	igd_context_t      *context = NULL;
+	igd_display_pipe_t *pipe    = NULL;
+	igd_display_port_t *port    = NULL;
+	int i;
+
+	EMGD_TRACE_ENTER;
+
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	EMGD_DEBUG("Device power state: D%ld", context->device_context.power_state);
+
+	pipe_conf = device_data_plb->pipe_preserve &
+		EMGD_READ32(context->device_context.virt_mmadr + pipe->pipe_reg);
+
+	/* Reset the plane of this pipe back to NULL, it will be set on the
+	 * call to program_plane, which is ok, since program_pipe occurs
+	 * before program_plane */
+	pipe->plane = NULL;
+
+	pTimings = pipe->timing;
+
+	/*
+	 * If the mode is VGA and the PD says it handles all VGA modes without
+	 * reprogramming then just set the mode and leave centering off.
+	 */
+	if(pTimings->mode_info_flags & IGD_MODE_VESA) {
+		if (pTimings->mode_number <= VGA_MODE_NUM_MAX) {
+			/* Pipe timings and clocks are not used but it must be on anyway */
+			EMGD_WRITE32(pipe_conf | 0x80000000,
+				context->device_context.virt_mmadr + pipe->pipe_reg);
+
+			kms_program_pipe_vga_plb(emgd_crtc);
+			return;
+		}
+	}
+
+	/* Program dot clock divisors. */
+	kms_program_clock_plb(emgd_crtc, pipe->clock_reg, pTimings->dclk);
+
+	/* Program timing registers for the pipe */
+	timing_reg = pipe->timing_reg;
+	if (pTimings->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		hactive = (unsigned long)pTimings->width*2 - 1;
+	} else {
+		hactive = (unsigned long)pTimings->width - 1;
+	}
+
+	if (pTimings->mode_info_flags & IGD_LINE_DOUBLE) {
+		if (pTimings->mode_info_flags & IGD_SCAN_INTERLACE) {
+			vactive = (unsigned long)pTimings->height - 1;
+		} else {
+			vactive = (unsigned long)pTimings->height*2 - 1;
+		}
+	} else {
+		if (pTimings->mode_info_flags & IGD_SCAN_INTERLACE) {
+			vactive = (unsigned long)pTimings->height/2 - 1;
+		} else {
+			vactive = (unsigned long)pTimings->height - 1;
+		}
+	}
+
+	/*
+	 * DPLL should be on at this point which is required for touching
+	 * the palette.
+	 */
+	/* reset the palette */
+	for (i = 0; i < 256; i++) {
+		EMGD_WRITE32(((i<<16) | (i<<8) | i),
+			context->device_context.virt_mmadr + pipe->palette_reg + i*4);
+	}
+
+	/* apply color correction */
+	for( i = 0; PD_ATTR_LIST_END != port->attributes[i].id; i++ ) {
+
+		if ((PD_ATTR_ID_FB_GAMMA      == (port->attributes[i].id)) ||
+			(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)) ||
+			(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)))  {
+
+			set_color_correct_plb(emgd_crtc);
+		}
+	}
+
+
+	/*
+	 * NOTE: For size reasons the timng table contains unsigned short
+	 * values. Don't shift them past 16. Use a temp instead.
+	 * All register offsets and bit shift are verified for Napa
+	 */
+	temp = ((unsigned long)pTimings->htotal << 16) | hactive;
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg);
+
+	temp = ((unsigned long)pTimings->hblank_end << 16) |
+		(unsigned long)pTimings->hblank_start;
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg + 0x04);
+
+	temp = ((unsigned long)pTimings->hsync_end << 16) |
+		(unsigned long)pTimings->hsync_start;
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg + 0x08);
+
+	temp = ((unsigned long)pTimings->vtotal << 16) | vactive;
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg + 0x0C);
+
+	temp = ((unsigned long)pTimings->vblank_end << 16) |
+		(unsigned long)pTimings->vblank_start;
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg + 0x10);
+
+	temp = ((unsigned long)pTimings->vsync_end << 16) |
+		(unsigned long)pTimings->vsync_start;
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg + 0x14);
+
+	/*
+	 * If there is a linked mode it is either the VGA or a scaled
+	 * mode. If it is scaled then we need to use it as the source size.
+	 */
+	if(pTimings->extn_ptr) {
+		igd_timing_info_t *scaled_timings =
+			(igd_timing_info_t *)pTimings->extn_ptr;
+		if((scaled_timings->mode_info_flags & IGD_MODE_VESA) &&
+			(scaled_timings->mode_number <= VGA_MODE_NUM_MAX)) {
+			temp = (hactive << 16) | vactive;
+		} else {
+			temp = (unsigned long)scaled_timings->width  - 1;
+			temp = (temp << 16) |
+				(unsigned long)(scaled_timings->height - 1);
+		}
+	} else {
+		temp = (hactive << 16) | vactive;
+	}
+	EMGD_WRITE32(temp, context->device_context.virt_mmadr + timing_reg + 0x1C);
+
+	/* Set other registers */
+
+	/*
+	 * FIXME: max_dclk needs to be determined from core clock
+	 * at init time. 915 etc has several skus with different
+	 * clocks for the same device ID.
+	 *
+	 */
+
+	/* These values are derived from the Poulsbo B-Spec as
+	 * the suggested values */
+	EMGD_WRITE32(device_data_plb->fw_blc1,
+		context->device_context.virt_mmadr + FW_BLC1);
+	EMGD_WRITE32(device_data_plb->fw_blc2,
+		context->device_context.virt_mmadr + FW_BLC2);
+	EMGD_WRITE32(device_data_plb->fw_blc3,
+		context->device_context.virt_mmadr + FW_BLC3);
+	EMGD_WRITE32(device_data_plb->fw_self,
+		context->device_context.virt_mmadr + FW_BLC_SELF);
+	EMGD_WRITE32(device_data_plb->dsp_arb,
+		context->device_context.virt_mmadr + PIPEA_DISP_ARB_CTRL);
+
+	/* The SGX 2D engine can saturate the memory bus and starve
+	 * the display engine causing visible screen tearing.
+	 * This reduces the priority of the SGX vs. display engine
+	 */
+	temp = EMGD_READ32(context->device_context.virt_mmadr + G_DEBUG);
+	EMGD_WRITE32((temp | (1 << 11)),
+		context->device_context.virt_mmadr + G_DEBUG);
+
+	EMGD_WRITE32(pipe_conf,
+		context->device_context.virt_mmadr + pipe->pipe_reg);
+
+	/*
+	 * Set the VGA address range to 0xa0000 so that a normal (not VGA)
+	 * mode can be accessed through 0xa0000 in a 16bit world.
+	 */
+	WRITE_AR(context->device_context.virt_mmadr, 0x10, 0xb);
+	WRITE_VGA(context->device_context.virt_mmadr, GR_PORT, 0x06, 0x5);
+	WRITE_VGA(context->device_context.virt_mmadr, GR_PORT, 0x10, 0x1);
+
+	if(pTimings->extn_ptr) {
+		/* This means either internal scaling (LVDS) or centered VGA */
+		pTimings = pTimings->extn_ptr;
+		if(pTimings->extn_ptr) {
+			/* This is both the scaled and centered VGA */
+			pTimings = pTimings->extn_ptr;
+		}
+		if(pTimings->mode_info_flags & IGD_MODE_VESA) {
+			if (pTimings->mode_number <= VGA_MODE_NUM_MAX) {
+				kms_program_pipe_vga_plb(emgd_crtc);
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+/*!
+ * kms_set_plane_pwr_plb
+ *
+ * Enables or disables the plane associated with the given CRTC
+ *
+ * @param enable [IN] turns the plane on or off
+ *
+ * @return void
+ */
+static void kms_set_plane_pwr_plb(emgd_crtc_t *emgd_crtc, unsigned long enable)
+{
+	unsigned long           plane_control;
+	unsigned long           plane_reg;
+	struct drm_device      *dev         = NULL;
+	igd_display_plane_t    *plane       = NULL;
+	igd_display_pipe_t     *pipe        = NULL;
+	igd_context_t          *context     = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program Plane: %s", enable?"ENABLE":"DISABLE");
+
+	pipe    = emgd_crtc->igd_pipe;
+	dev     = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	plane   = PLANE(pipe->owner);
+
+	if (!plane) {
+		EMGD_ERROR_EXIT("Trying to set power to a plane that is not tied "
+			" to a crtc.");
+		return;
+	}
+
+	/* In case a plane update is already in progress */
+	wait_for_vblank_plb(context->device_context.virt_mmadr, pipe->pipe_reg);
+
+	/* Get the current value of the plane control register */
+	plane_reg     = plane->plane_reg;
+	plane_control = EMGD_READ32(context->device_context.virt_mmadr + plane_reg);
+
+	if(plane->plane_reg == DSPACNTR) {
+		plane_control &= device_data_plb->plane_a_preserve;
+	} else { /* if it's plane b or plane c */
+		plane_control &= device_data_plb->plane_b_c_preserve;
+	}
+
+
+	if((enable == FALSE) ||
+		(context->device_context.power_state != IGD_POWERSTATE_D0)) {
+
+		/*
+		 * Note: The vga programming code does not have an "off". So
+		 * when programming the plane to off we make sure VGA is off
+		 * as well.
+		 */
+		disable_vga_plb(context->device_context.virt_mmadr);
+
+		/*
+		 * To turn off plane A or B, the program have to trigger the plane A
+		 * or B start register.  Or else, it will not work.
+		 */
+		plane_control &= 0xEFFFFFFF;
+
+		EMGD_WRITE32(plane_control,
+						context->device_context.virt_mmadr + plane_reg);
+
+		EMGD_WRITE32(EMGD_READ32(context->device_context.virt_mmadr +
+			plane_reg + DSP_START_OFFSET),
+			context->device_context.virt_mmadr + plane_reg + DSP_START_OFFSET);
+	} else {
+		/* Enable Pipe */
+		plane_control |= 0x80000000;
+
+		EMGD_WRITE32(plane_control,
+			context->device_context.virt_mmadr + plane_reg);
+	}
+
+
+	wait_for_vblank_plb(context->device_context.virt_mmadr, pipe->pipe_reg);
+	EMGD_TRACE_EXIT;
+
+	return;
+}
+
+
+
+/*!
+ * Program Display Plane Values.
+ *
+ * @param emgd_crtc Pointer to hardware device instance data
+ *
+ * @return void
+ */
+static void kms_program_plane_plb(emgd_crtc_t *emgd_crtc,
+	unsigned long status)
+{
+	unsigned long stereo;
+	unsigned long stride;
+	unsigned long size;
+	unsigned long plane_control;
+	unsigned long plane_reg;
+	unsigned long start_addr_reg;
+	igd_timing_info_t      *timing  = NULL;
+	struct drm_device      *dev     = NULL;
+	igd_display_plane_t    *plane   = NULL;
+	igd_display_pipe_t     *pipe    = NULL;
+	igd_context_t          *context = NULL;
+	igd_framebuffer_info_t *fb_info = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	plane   = PLANE(pipe->owner);
+	fb_info = plane->fb_info;
+	plane_reg = plane->plane_reg;
+	start_addr_reg = DSPAADDR;
+
+	EMGD_DEBUG("Device power state: D%ld", context->device_context.power_state);
+
+	wait_for_vblank_plb(context->device_context.virt_mmadr, pipe->pipe_reg);
+
+	plane_control = EMGD_READ32(context->device_context.virt_mmadr + plane_reg);
+	if(plane->plane_reg == DSPACNTR) {
+		plane_control &= device_data_plb->plane_a_preserve;
+	}
+	else { /* if it's plane b or plane c */
+		plane_control &= device_data_plb->plane_b_c_preserve;
+		start_addr_reg = 0x71184;
+	}
+
+	if((status == FALSE) ||
+		(context->device_context.power_state != IGD_POWERSTATE_D0)) {
+
+		/*
+		 * Note: The vga programming code does not have an "off". So
+		 * when programming the plane to off we make sure VGA is off
+		 * as well.
+		 */
+		disable_vga_plb(context->device_context.virt_mmadr);
+
+		/*
+		 * To turn off plane A or B, the program have to triger the plane A or B
+		 * start register.  Or else, it will not work.
+		 */
+		EMGD_WRITE32(plane_control, context->device_context.virt_mmadr + plane_reg);
+		EMGD_WRITE32(EMGD_READ32(context->device_context.virt_mmadr + start_addr_reg),
+			context->device_context.virt_mmadr + start_addr_reg);
+
+		wait_for_vblank_plb(context->device_context.virt_mmadr, pipe->pipe_reg);
+		return;
+	}
+	/*
+	 * Note: The very first pass through this function will be with
+	 * status false and timings == NULL. Don't use the timings before
+	 * the check above.
+	 */
+	timing = pipe->timing;
+	/* There is a special case code for legacy VGA modes */
+	while (timing->extn_ptr) {
+		timing = (igd_timing_info_t *)timing->extn_ptr;
+	}
+	if(MODE_IS_VGA(timing)) {
+		kms_program_plane_vga(context->device_context.virt_mmadr, timing);
+		return;
+	}
+
+	disable_vga_plb(context->device_context.virt_mmadr);
+
+	size = (((unsigned long)timing->height - 1)<<16) |
+		(unsigned long)(timing->width - 1);
+
+	/* enable plane, select pipe, enable gamma correction logic */
+	plane_control |= 0x80000000 | (pipe->pipe_num<<24);
+	pipe->plane = plane;
+#ifndef CONFIG_MICRO
+	plane_control |= (1<<30);
+#endif
+
+	/* Here the settings:
+	 *   If line + pixel dbling, set 21,20 to 01b, and set Horz Multiply
+	 *   If line dbling only,    set 21,20 to 11b
+	 *   If pixel dbling only,   set 21,20 to 00b, but set Horz Multiply
+	 *   If no doubling,         set 21,20 to 00b (no Horz Multiply)
+	 * For pixel doubling
+	 *           --> both progressive/interlaced modes
+	 * For Line doubling
+	 *           --> progressive modes only
+	 */
+
+	if (!(timing->mode_info_flags & IGD_SCAN_INTERLACE)) {
+		/* Line doubling in progressive mode requires special bits */
+		if (timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* BIT 20 for line & pixel doubling*/
+			plane_control |= BIT20;
+			/* check later, if no pixel doubling, set bit 21 too*/
+		}
+	}
+	if (timing->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		/* Horz pixel multiply must be set for double */
+		plane_control |= BIT11;
+		/* TODO -> Plba can more than double,
+		It can 3X, 4X etc. These arent exposed now */
+	}
+	else if(plane_control & BIT20){
+		/* For line ONLY doubling, set bit 21 also '1' */
+		plane_control |= BIT21;
+	}
+
+	kms_mode_get_stride_stereo_plb(pipe, plane, &stride, &stereo, 0);
+
+	/* set color depth */
+	switch (IGD_PF_DEPTH(fb_info->pixel_format)) {
+	case PF_DEPTH_8:
+		plane_control |= BIT27 | BIT30;
+		break;
+	case PF_DEPTH_16:
+		plane_control |= BIT28 | BIT26;
+		break;
+	default:
+	case PF_DEPTH_32:
+		plane_control |= BIT28 | BIT27;
+		break;
+	}
+
+	if(fb_info->flags & IGD_ENABLE_DISPLAY_GAMMA) {
+		plane_control |= (BIT30);
+	}
+
+	EMGD_DEBUG(" Plane Control = 0x%lx", plane_control);
+	EMGD_DEBUG(" Plane Base = 0x%lx", fb_info->visible_offset);
+	EMGD_DEBUG(" Plane Pitch = 0x%lx", stride);
+	EMGD_DEBUG(" Plane Size = 0x%lx", size);
+
+	EMGD_WRITE32(stride, context->device_context.virt_mmadr + plane_reg + DSP_STRIDE_OFFSET);
+	/*
+	 * In reality this only exists for plane B. It doesn't seem to hurt
+	 * plane A so just do it anyway and save us another case.
+	 */
+	EMGD_WRITE32(size, context->device_context.virt_mmadr + plane_reg + DSP_SIZE_OFFSET);
+
+	/*EMGD_WRITE32(stereo, MMIO(display) + plane_reg + DSP_STEREO_OFFSET);
+		- This register is Reserved ON plba */
+	EMGD_WRITE32(fb_info->visible_offset,
+		context->device_context.virt_mmadr + plane_reg + DSP_START_OFFSET);
+
+	/* It seems we need push or trigger plane A/B to start to work
+	 * on Poulsbo, especially for sDVO port. Let's write plane control
+	 * register and start address register at last.
+	 */
+	EMGD_WRITE32(plane_control, context->device_context.virt_mmadr + plane_reg);
+	EMGD_WRITE32(EMGD_READ32(context->device_context.virt_mmadr + start_addr_reg),
+		context->device_context.virt_mmadr + start_addr_reg);
+
+	wait_for_vblank_plb(context->device_context.virt_mmadr, pipe->pipe_reg);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/plb/micro_mode_plb.c b/drivers/gpu/drm/emgd/emgd/display/mode/plb/micro_mode_plb.c
new file mode 100644
index 0000000..9fbb378
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/plb/micro_mode_plb.c
@@ -0,0 +1,1378 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_mode_plb.c
+ * $Revision: 1.22 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Napa Core implementations for the mode dispatch functions.
+ *  NOTE: This file is designed to produce tiny code and is used in the
+ *  vbios port. Make sure that all MMIO reads and writes do all mmio
+ *  calculation within the OS_READ and OS_WRITE macros so that this code
+ *  will be correctly removed.
+ *  This is OK -> EMGD_READ32( MMIO(display) + foo_offset )
+ *  This is NOT OK ->  mmio = MMIO(display)
+ *  EMGD_READ32(mmio + foo_offset)
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <io.h>
+
+#include <igd.h>
+#include <igd_pwr.h>
+
+#include <context.h>
+#include <rb.h>
+#include <mode.h>
+#include <utils.h>
+#include <dsp.h>
+#include <vga.h>
+#include <intelpci.h>
+#include <mode_access.h>
+#include <pi.h>
+#include <dispatch.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+#include <plb/mi.h>
+#include "drm_emgd_private.h"
+
+#include "../cmn/match.h"
+#include "../cmn/mode_dispatch.h"
+#include "mode_plb.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+#ifdef CONFIG_PLB
+
+/*
+ * Exports from the other components of this module.
+ */
+
+extern int program_clock_plb(igd_display_context_t *display,
+	igd_clock_t *clock, unsigned long dclk);
+extern mode_full_dispatch_t mode_full_dispatch_plb;
+
+static unsigned long gpio_plb[] = {
+	0x5010,
+	0x5014,
+	0x5018,
+	0x501c,
+	0x5020,
+	0x5024,
+	0x5028
+};
+
+mode_data_plb_t device_data_plb[1] = {
+	{
+		0x000b0000, /* plane a preservation */
+		0x00000000, /* plane b/c preservation */
+		0x01000000, /* pipe preservation */
+		0, /* port preservation */
+		0x490A010A, /* watermark/burst length 1 */
+		0x14100D0A, /* watermark/burst length 2*/
+		0x00007770, /* watermark/burst length 3 */
+		0x0B0C9812, /* watermark/burst length self */
+		0,  /* mem_mode */
+		0x00001D9C, /* dsp arb */
+	}
+};
+
+/*!
+ *
+ * @param mmio
+ *
+ * @return void
+ */
+void disable_vga_plb (unsigned char *mmio)
+{
+	unsigned long temp;
+	unsigned char sr01;
+
+	EMGD_TRACE_ENTER;
+
+	/* Disable VGA plane if it is enabled. */
+	temp = EMGD_READ32(EMGD_MMIO(mmio) + VGACNTRL);
+	if ((temp & BIT31) == 0) {
+		/* Read SR01 */
+		READ_VGA(mmio, SR_PORT, 0x01, sr01);
+
+		/* Turn on SR01 bit 5 */
+		WRITE_VGA(mmio, SR_PORT, 0x01, sr01|BIT(5));
+		/* Wait for 30us */
+		OS_SLEEP(30);
+
+		temp |= BIT31;     /* set bit 31 to disable */
+		temp &= ~BIT30;    /* clear bit 30 to get VGA display in normal size */
+		EMGD_WRITE32(temp, EMGD_MMIO(mmio) + VGACNTRL);
+	}
+	/*
+	 * When turing off the VGA plane the palette sometimes gets stuck.
+	 * if we do a couple reads to the palette it will unstuck.
+	 */
+	if((1L<<31) & EMGD_READ32( EMGD_MMIO(mmio) + PIPEA_CONF )) {
+		EMGD_DEBUG("VGA Palette workaround");
+		EMGD_READ32(EMGD_MMIO(mmio) + DPALETTE_A);
+		EMGD_READ32(EMGD_MMIO(mmio) + DPALETTE_A);
+	}
+	if((1L<<31) & EMGD_READ32( EMGD_MMIO(mmio) + PIPEB_CONF )) {
+		EMGD_DEBUG("VGA Palette workaround");
+		EMGD_READ32(EMGD_MMIO(mmio) + DPALETTE_B);
+		EMGD_READ32(EMGD_MMIO(mmio) + DPALETTE_B);
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param palette_entry
+ * @param palette_color
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int igd_set_palette_entry_plb(
+	igd_display_h display_handle,
+	unsigned long palette_entry,
+	unsigned long palette_color)
+{
+	/* Too Slow For Tracing */
+
+	/* Return if Pipe is not on */
+	if(!((1L<<31) & EMGD_READ32(MMIO(display_handle) +
+			PIPE(display_handle)->pipe_reg))) {
+		return -IGD_INVAL;
+	}
+	/*
+	EMGD_DEBUG("%x : %ld : %lx", display_handle,
+		palette_entry, palette_color);
+	*/
+	EMGD_WRITE32(palette_color, MMIO(display_handle) +
+		PIPE(display_handle)->palette_reg + palette_entry * 4);
+
+	return 0;
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param palette_entry
+ * @param palette_color
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int igd_get_palette_entry_plb(
+	igd_display_h display_handle,
+	unsigned long palette_entry,
+	unsigned long *palette_color)
+{
+	/* Too Slow For Tracing */
+
+	/* Return if Pipe is not on */
+	if(!((1L<<31) & EMGD_READ32(MMIO(display_handle) +
+			PIPE(display_handle)->pipe_reg))) {
+		return -IGD_INVAL;
+	}
+
+	*palette_color = 0xffffff & EMGD_READ32(MMIO(display_handle) +
+		PIPE(display_handle)->palette_reg + palette_entry * 4);
+	return 0;
+}
+
+/*!
+ *
+ * @param mmio
+ * @param pipe_reg
+ * @param time_interval
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int wait_for_vblank_timeout_plb(
+	unsigned char *mmio,
+	unsigned long pipe_reg,
+	unsigned long time_interval)
+{
+	int ret;
+	unsigned long pipe_status_reg = pipe_reg + PIPE_STATUS_OFFSET;
+	unsigned long tmp;
+	os_alarm_t timeout;
+	unsigned long request_for;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: MMIO = %p, pipe_reg = %lx, time_interval = %lx",
+		mmio, pipe_reg, time_interval);
+
+	/* If pipe is off then just return */
+	if(!((1L<<31) & EMGD_READ32(EMGD_MMIO(mmio) + pipe_reg))) {
+		EMGD_DEBUG("Pipe disabled/Off");
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	/*
+	 * When VGA plane is on the normal wait for vblank won't work
+	 * so just skip it.
+	 */
+	if(!(EMGD_READ32(EMGD_MMIO(mmio) + 0x71400) & 0x80000000)) {
+		EMGD_DEBUG("VGA Plane On");
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	/* 1. Request the interrupt handler to record the next VBlank: */
+	request_for = VBINT_REQUEST(VBINT_WAIT,
+		(pipe_status_reg == PIPEA_STAT) ? VBINT_PORT2 : VBINT_PORT4);
+	mode_context->dispatch->full->request_vblanks(request_for, mmio);
+
+	/* 2. Wait & poll for the next VBlank: */
+	timeout = OS_SET_ALARM(time_interval);
+	do {
+		OS_SCHEDULE();
+		tmp = mode_context->dispatch->full->vblank_occured(request_for);
+	} while ((tmp == 0x00) && (!OS_TEST_ALARM(timeout)));
+	if (tmp == 0) {
+		EMGD_ERROR_EXIT("Timeout waiting for VBLANK");
+		ret = 0;
+	} else {
+		ret = 1;
+	}
+
+	/* 3. End our request for the next VBlank: */
+	mode_context->dispatch->full->end_request(request_for, mmio);
+
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* wait_for_vblank_timeout_plb */
+
+/*!
+ *
+ * @param mmio
+ * @param pipe_reg
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int wait_for_vblank_plb(unsigned char *mmio,
+	unsigned long pipe_reg)
+{
+	return wait_for_vblank_timeout_plb(mmio, pipe_reg, 100);
+} /* wait_for_vblank_plb */
+
+
+/*!
+ * This procedure waits for the next vertical blanking (vertical retrace)
+ * period. If the display is already in a vertical blanking period, this
+ * procedure exits.
+ *
+ * Note: A timeout is included to prevent an endless loop.
+ *
+ * @param display_handle
+ *
+ * @return FALSE if timed out
+ */
+static int igd_wait_vblank_plb(igd_display_h display_handle)
+{
+
+	return wait_for_vblank_plb(MMIO(display_handle),
+		PIPE(display_handle)->pipe_reg);
+
+}  /* wait_vblank */
+
+
+/*!
+ * Get the stride and stereo values based on the display.  This is also used
+ * by the MI instructions.
+ *
+ * @param display Pointer to hardware device instance data
+ * @param stride
+ * @param stereo
+ * @param flags Should the stereo be for the frontbuffer or backbuffer?
+ *
+ * @return stride - Stride of the display
+ * @return stereo - Stereo address of the display
+ */
+static int mode_get_stride_stereo_plb(igd_display_context_t *display,
+	unsigned long *stride,
+	unsigned long *stereo,
+	unsigned long flags)
+{
+	unsigned long pitch = PLANE(display)->fb_info->screen_pitch;
+	igd_timing_info_t *timing = PIPE(display)->timing;
+	unsigned long base_offset;
+
+	base_offset = PLANE(display)->fb_info->visible_offset;
+
+	*stride = pitch;
+	*stereo = 0;
+
+	/* For field replication, valid for interlaced modes only
+	 *     set stereo = fb_base,
+	 *         stride = pitch
+	 */
+	if (timing->mode_info_flags & IGD_SCAN_INTERLACE) {
+
+		if(timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* Interlaced + Line double flags means field replication.
+			 * same lines are sent for both fields. Program the
+			 * second eye to be same as the first.
+			 */
+			*stereo = base_offset;
+		} else {
+			/* Regular interlaced. Second eye starts on line 2.
+			 * Skip every other line.
+			 */
+			*stereo = base_offset + pitch;
+			*stride = pitch * 2;
+		}
+	}
+
+	return 0;
+}
+
+/*!
+ *
+ * @param display Pointer to hardware device instance data
+ *
+ * @return void
+ */
+static void program_pipe_vga_plb(
+	igd_display_context_t *display)
+{
+	igd_timing_info_t *timing;
+	unsigned long vga_control;
+	unsigned long upscale = 0;
+	int centering = 1;
+
+	EMGD_TRACE_ENTER;
+
+#ifdef CONFIG_MICRO
+	/*
+	 * We cannot set a VGA mode unless the display planes are turned off.
+     * This becomes evident during a Windows BSOD.  Since neither Windows
+     * nor IEGD got a chance to turn off these registers, and the VGA mode was
+     * set by the VBIOS, the screen gets corrupted.  In order to fix this
+     * problem, we will turn the cursor and display planes here.
+     *
+     * Note: Removing previous partial-fix in favor of this complete one.
+	 */
+    /* Cursor A */
+	WRITE_MMIO_REG(display, 0x70080, 0);
+	WRITE_MMIO_REG(display, 0x70084, 0);
+    /* Cursor B */
+	WRITE_MMIO_REG(display, 0x700C0, 0);
+	WRITE_MMIO_REG(display, 0x700C4, 0);
+    /* Display A */
+	WRITE_MMIO_REG(display, 0x70180, 0);
+	WRITE_MMIO_REG(display, 0x70184, 0);
+    /* Display B */
+	WRITE_MMIO_REG(display, 0x71180, 0);
+	WRITE_MMIO_REG(display, 0x71184, 0);
+    /* Display C */
+	WRITE_MMIO_REG(display, 0x72180, 0);
+	WRITE_MMIO_REG(display, 0x72184, 0);
+#endif
+
+	/*
+	 * VGA Plane can attach to only one pipe at a time. LVDS can
+	 * only attach to pipe B. We need to use the display passed to
+	 * determine the pipe number to use. (Plba is same as Alm).
+	 */
+
+	/*
+	 * We can come here with following cases:
+	 *   1. magic->vga    CRT, DVI type displays
+	 *   2. native->vga   int-lvds, and up-scaling lvds displays
+	 *   3. pipe->vga     TV and other unscaled-lvds displays
+	 */
+	vga_control = READ_MMIO_REG(display, 0x71400);
+	vga_control &= 0x18e3ff00;
+	vga_control |= 0x8e;
+
+	timing = PIPE(display)->timing;
+	if(!timing->extn_ptr) {
+		EMGD_ERROR_EXIT("No Extension pointer in program_pipe_vga_plb");
+		return;
+	}
+
+	/* Find UPSCALING attr value*/
+	pi_pd_find_attr_and_value(PORT_OWNER(display),
+			PD_ATTR_ID_PANEL_FIT,
+			0,/*no PD_FLAG for UPSCALING */
+			NULL, /* dont need the attr ptr*/
+			&upscale);
+	/* this PI func will not modify value of upscale if attr does not exist */
+
+	/* magic->vga or native->vga cases */
+	if ((timing->width == 720 && timing->height == 400) || upscale) {
+		centering = 0;
+	}
+
+	/* Enable border */
+	if((timing->width >= 800) && !upscale) {
+		EMGD_DEBUG("Enable VGA Border");
+		vga_control |= (1L<<26);
+	}
+
+	if(timing->width == 640) {
+		EMGD_DEBUG("Enable Nine Dot Disable");
+		vga_control |= (1L<<18);
+	}
+
+	if(centering) {
+		EMGD_DEBUG("Enable VGA Center Centering");
+		vga_control |= 1L<<24;
+
+		if(timing->height >= 960) {
+			if(timing->width >= 1280) {
+				EMGD_DEBUG("Enable VGA 2x (Nine Dot Disable)");
+				vga_control |= (1L<<30) | (1L<<18);
+			}
+		}
+	} else {
+		if(PORT_OWNER(display)->port_type == IGD_PORT_LVDS) {
+			EMGD_DEBUG("Enable VGA Upper-Left Centering & Nine Dot Disable");
+			vga_control |= (1L<<25 | (1L<<18));
+		} else if (upscale) {
+			EMGD_DEBUG("Enable VGA Center Upper-left for upscale ports");
+			vga_control |= 1L<<25;
+		}
+	}
+
+	if(PIPE(display)->pipe_num) {
+		vga_control |= 1L<<29;
+	}
+
+	program_pipe_vga(display, (igd_timing_info_t *)timing->extn_ptr);
+	WRITE_MMIO_REG(display, 0x71400, vga_control);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ * Program Display Plane Values.
+ *
+ * @param display Pointer to hardware device instance data
+ * @param status
+ *
+ * @return void
+ */
+static void program_plane_plb(igd_display_context_t *display,
+	unsigned long status)
+{
+	unsigned long stereo;
+	unsigned long stride;
+	unsigned long size;
+	unsigned long plane_control;
+	igd_timing_info_t *timing;
+	igd_framebuffer_info_t *fb_info = PLANE(display)->fb_info;
+	unsigned long plane_reg = PLANE(display)->plane_reg;
+	unsigned long start_addr_reg = DSPAADDR;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program Plane: %s", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("Device power state: D%ld", GET_DEVICE_POWER_STATE(display));
+
+	igd_wait_vblank_plb((igd_display_h)display);
+
+	plane_control = EMGD_READ32(MMIO(display) + plane_reg);
+	if(PLANE(display)->plane_reg == DSPACNTR) {
+		plane_control &= device_data_plb->plane_a_preserve;
+	}
+	else { /* if it's plane b or plane c */
+		plane_control &= device_data_plb->plane_b_c_preserve;
+		start_addr_reg = 0x71184;
+	}
+
+	if((status == FALSE) ||
+		(GET_DEVICE_POWER_STATE(display) != IGD_POWERSTATE_D0)) {
+
+		/*
+		 * Note: The vga programming code does not have an "off". So
+		 * when programming the plane to off we make sure VGA is off
+		 * as well.
+		 */
+		disable_vga_plb(MMIO(display));
+
+		/*
+		 * To turn off plane A or B, the program have to triger the plane A or B
+		 * start register.  Or else, it will not work.
+		 */
+		EMGD_WRITE32(plane_control, MMIO(display) + plane_reg);
+		EMGD_WRITE32(EMGD_READ32(MMIO(display) + start_addr_reg),
+			MMIO(display) + start_addr_reg);
+
+		igd_wait_vblank_plb((igd_display_h)display);
+		return;
+	}
+	/*
+	 * Note: The very first pass through this function will be with
+	 * status false and timings == NULL. Don't use the timings before
+	 * the check above.
+	 */
+	timing = PIPE(display)->timing;
+	/* There is a special case code for legacy VGA modes */
+	while (timing->extn_ptr) {
+		timing = (igd_timing_info_t *)timing->extn_ptr;
+	}
+	if(MODE_IS_VGA(timing)) {
+		program_plane_vga(display, timing);
+		return;
+	}
+
+	disable_vga_plb(MMIO(display));
+
+	size = (((unsigned long)timing->height - 1)<<16) |
+		(unsigned long)(timing->width - 1);
+
+	/* enable plane, select pipe, enable gamma correction logic */
+	plane_control |= 0x80000000 | (PIPE(display)->pipe_num<<24);
+	PIPE(display)->plane = PLANE(display);
+#ifndef CONFIG_MICRO
+	plane_control |= (1<<30);
+#endif
+
+	/* Here the settings:
+	 *   If line + pixel dbling, set 21,20 to 01b, and set Horz Multiply
+	 *   If line dbling only,    set 21,20 to 11b
+	 *   If pixel dbling only,   set 21,20 to 00b, but set Horz Multiply
+	 *   If no doubling,         set 21,20 to 00b (no Horz Multiply)
+	 * For pixel doubling
+	 *           --> both progressive/interlaced modes
+	 * For Line doubling
+	 *           --> progressive modes only
+	 */
+
+	if (!(timing->mode_info_flags & IGD_SCAN_INTERLACE)) {
+		/* Line doubling in progressive mode requires special bits */
+		if (timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* BIT 20 for line & pixel doubling*/
+			plane_control |= BIT20;
+			/* check later, if no pixel doubling, set bit 21 too*/
+		}
+	}
+	if (timing->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		/* Horz pixel multiply must be set for double */
+		plane_control |= BIT11;
+		/* TODO -> Plba can more than double,
+		It can 3X, 4X etc. These arent exposed now */
+	}
+	else if(plane_control & BIT20){
+		/* For line ONLY doubling, set bit 21 also '1' */
+		plane_control |= BIT21;
+	}
+
+	mode_get_stride_stereo_plb(display, &stride, &stereo, 0);
+
+	/* set color depth */
+	switch (IGD_PF_DEPTH(fb_info->pixel_format)) {
+	case PF_DEPTH_8:
+		plane_control |= BIT27 | BIT30;
+		break;
+	case PF_DEPTH_16:
+		plane_control |= BIT28 | BIT26;
+		break;
+	default:
+	case PF_DEPTH_32:
+		plane_control |= BIT28 | BIT27;
+		break;
+	}
+
+	if(fb_info->flags & IGD_ENABLE_DISPLAY_GAMMA) {
+		plane_control |= (BIT30);
+	}
+
+	EMGD_DEBUG(" Plane Control = 0x%lx", plane_control);
+	EMGD_DEBUG(" Plane Base = 0x%lx", fb_info->visible_offset);
+	EMGD_DEBUG(" Plane Pitch = 0x%lx", stride);
+	EMGD_DEBUG(" Plane Size = 0x%lx", size);
+
+	EMGD_WRITE32(stride, MMIO(display) + plane_reg + DSP_STRIDE_OFFSET);
+	/*
+	 * In reality this only exists for plane B. It doesn't seem to hurt
+	 * plane A so just do it anyway and save us another case.
+	 */
+	EMGD_WRITE32(size, MMIO(display) + plane_reg + DSP_SIZE_OFFSET);
+
+	/*EMGD_WRITE32(stereo, MMIO(display) + plane_reg + DSP_STEREO_OFFSET);
+		- This register is Reserved ON plba */
+	EMGD_WRITE32(fb_info->visible_offset,
+		MMIO(display) + plane_reg + DSP_START_OFFSET);
+
+	/* It seems we need push or trigger plane A/B to start to work
+	 * on Poulsbo, especially for sDVO port. Let's write plane control
+	 * register and start address register at last.
+	 */
+	EMGD_WRITE32(plane_control, MMIO(display) + plane_reg);
+	EMGD_WRITE32(EMGD_READ32(MMIO(display) + start_addr_reg),
+		MMIO(display) + start_addr_reg);
+
+	igd_wait_vblank_plb((igd_display_h)display);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ * This function programs the Timing registers and clock registers and
+ * other control registers for PIPE.
+ *
+ * @param display
+ * @param status
+ *
+ * @return void
+ */
+static void program_pipe_plb(igd_display_context_t *display,
+	unsigned long status)
+{
+	unsigned long   timing_reg;
+	unsigned long   pipe_conf;
+	unsigned long   hactive, vactive;
+	igd_timing_info_t  *pTimings;
+	unsigned long temp;
+#ifndef CONFIG_MICRO
+	igd_display_port_t *port;
+	int i;
+#endif
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Program Pipe: %s", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("Device power state: D%ld", GET_DEVICE_POWER_STATE(display));
+
+	pipe_conf = device_data_plb->pipe_preserve &
+		EMGD_READ32(MMIO(display) + PIPE(display)->pipe_reg);
+
+	/* Reset the plane of this pipe back to NULL, it will be set on the
+	 * call to program_plane, which is ok, since program_pipe occurs
+	 * before program_plane */
+	PIPE(display)->plane = NULL;
+
+	if((status == FALSE) ||
+		(GET_DEVICE_POWER_STATE(display) == IGD_POWERSTATE_D3)) {
+		/* Disable pipe */
+		EMGD_WRITE32(pipe_conf & (~0x80000000),
+			MMIO(display) + PIPE(display)->pipe_reg);
+
+		return;
+	}
+
+	pTimings = PIPE(display)->timing;
+
+	/*
+	 * If the mode is VGA and the PD says it handles all VGA modes without
+	 * reprogramming then just set the mode and leave centering off.
+	 */
+	if(pTimings->mode_info_flags & IGD_MODE_VESA) {
+		if (pTimings->mode_number <= VGA_MODE_NUM_MAX) {
+			/* Pipe timings and clocks are not used but it must be on anyway */
+			EMGD_WRITE32(pipe_conf | 0x80000000,
+				MMIO(display) + PIPE(display)->pipe_reg);
+			program_pipe_vga_plb(display);
+			return;
+		} else {
+#ifdef CONFIG_MICRO
+			set_256_palette(MMIO(display));
+#endif
+		}
+	}
+
+	/* Program dot clock divisors. */
+	program_clock_plb(display, PIPE(display)->clock_reg, pTimings->dclk);
+
+	/* Program timing registers for the pipe */
+	timing_reg = PIPE(display)->timing_reg;
+	if (pTimings->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		hactive = (unsigned long)pTimings->width*2 - 1;
+	} else {
+		hactive = (unsigned long)pTimings->width - 1;
+	}
+
+	if (pTimings->mode_info_flags & IGD_LINE_DOUBLE) {
+		if (pTimings->mode_info_flags & IGD_SCAN_INTERLACE) {
+			vactive = (unsigned long)pTimings->height - 1;
+		} else {
+			vactive = (unsigned long)pTimings->height*2 - 1;
+		}
+	} else {
+		if (pTimings->mode_info_flags & IGD_SCAN_INTERLACE) {
+			vactive = (unsigned long)pTimings->height/2 - 1;
+		} else {
+			vactive = (unsigned long)pTimings->height - 1;
+		}
+	}
+
+	/*
+	 * DPLL should be on at this point which is required for touching
+	 * the palette.
+	 */
+#ifndef CONFIG_MICRO
+	/* reset the palette */
+	for (i = 0; i < 256; i++) {
+		EMGD_WRITE32(((i<<16) | (i<<8) | i),
+			MMIO(display) + PIPE(display)->palette_reg + i*4);
+	}
+
+	/* apply color correction */
+	port = PORT_OWNER(display);
+	for( i = 0; PD_ATTR_LIST_END != port->attributes[i].id; i++ ) {
+
+		if ((PD_ATTR_ID_FB_GAMMA      == (port->attributes[i].id)) ||
+			(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)) ||
+			(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)))  {
+
+			mode_context->dispatch->full->set_color_correct(display);
+		}
+	}
+#endif
+
+	/*
+	 * NOTE: For size reasons the timng table contains unsigned short
+	 * values. Don't shift them past 16. Use a temp instead.
+	 * All register offsets and bit shift are verified for Napa
+	 */
+	temp = ((unsigned long)pTimings->htotal << 16) | hactive;
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg);
+
+	temp = ((unsigned long)pTimings->hblank_end << 16) |
+		(unsigned long)pTimings->hblank_start;
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg + 0x04);
+
+	temp = ((unsigned long)pTimings->hsync_end << 16) |
+		(unsigned long)pTimings->hsync_start;
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg + 0x08);
+
+	temp = ((unsigned long)pTimings->vtotal << 16) | vactive;
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg + 0x0C);
+
+	temp = ((unsigned long)pTimings->vblank_end << 16) |
+		(unsigned long)pTimings->vblank_start;
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg + 0x10);
+
+	temp = ((unsigned long)pTimings->vsync_end << 16) |
+		(unsigned long)pTimings->vsync_start;
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg + 0x14);
+
+	/*
+	 * If there is a linked mode it is either the VGA or a scaled
+	 * mode. If it is scaled then we need to use it as the source size.
+	 */
+	if(pTimings->extn_ptr) {
+		igd_timing_info_t *scaled_timings =
+			(igd_timing_info_t *)pTimings->extn_ptr;
+		if((scaled_timings->mode_info_flags & IGD_MODE_VESA) &&
+			(scaled_timings->mode_number <= VGA_MODE_NUM_MAX)) {
+			temp = (hactive << 16) | vactive;
+		} else {
+			temp = (unsigned long)scaled_timings->width  - 1;
+			temp = (temp << 16) |
+				(unsigned long)(scaled_timings->height - 1);
+		}
+	} else {
+		temp = (hactive << 16) | vactive;
+	}
+	EMGD_WRITE32(temp, MMIO(display) + timing_reg + 0x1C);
+
+	/* Set other registers */
+
+	/*
+	 * FIXME: max_dclk needs to be determined from core clock
+	 * at init time. 915 etc has several skus with different
+	 * clocks for the same device ID.
+	 *
+	 */
+
+	/* These values are derived from the Poulsbo B-Spec as
+	 * the suggested values */
+	WRITE_MMIO_REG (display, FW_BLC1, device_data_plb->fw_blc1);
+	WRITE_MMIO_REG (display, FW_BLC2, device_data_plb->fw_blc2);
+	WRITE_MMIO_REG (display, FW_BLC3, device_data_plb->fw_blc3);
+	WRITE_MMIO_REG (display, FW_BLC_SELF, device_data_plb->fw_self);
+	WRITE_MMIO_REG (display, PIPEA_DISP_ARB_CTRL, device_data_plb->dsp_arb);
+
+	/* The SGX 2D engine can saturate the memory bus and starve
+	 * the display engine causing visible screen tearing.
+	 * This reduces the priority of the SGX vs. display engine
+	 */
+	temp = READ_MMIO_REG (display, G_DEBUG);
+	WRITE_MMIO_REG (display, G_DEBUG, (temp | (1 << 11)));
+
+	pipe_conf |= PIPE_ENABLE;
+	WRITE_MMIO_REG(display, PIPE(display)->pipe_reg, pipe_conf);
+
+	/*
+	 * Set the VGA address range to 0xa0000 so that a normal (not VGA)
+	 * mode can be accessed through 0xa0000 in a 16bit world.
+	 */
+	WRITE_AR(MMIO(display), 0x10, 0xb);
+	WRITE_VGA(MMIO(display), GR_PORT, 0x06, 0x5);
+	WRITE_VGA(MMIO(display), GR_PORT, 0x10, 0x1);
+
+	if(pTimings->extn_ptr) {
+		/* This means either internal scaling (LVDS) or centered VGA */
+		pTimings = pTimings->extn_ptr;
+		if(pTimings->extn_ptr) {
+			/* This is both the scaled and centered VGA */
+			pTimings = pTimings->extn_ptr;
+		}
+		if(pTimings->mode_info_flags & IGD_MODE_VESA) {
+			if (pTimings->mode_number <= VGA_MODE_NUM_MAX) {
+				program_pipe_vga_plb(display);
+			} else {
+#ifdef CONFIG_MICRO
+				set_256_palette(MMIO(display));
+#endif
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void reset_plane_pipe_ports_plb(igd_context_t *context)
+{
+	igd_plane_t        *plane;
+	igd_display_pipe_t *pipe;
+	igd_display_port_t *port,*tv_port=NULL;
+	unsigned long temp;
+	unsigned long i;
+	unsigned char *mmio;
+	inter_module_dispatch_t *md;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Disable all plane, pipe and port registers because the
+	 * bios may have been using a different set. Only unset the
+	 * enable bit.
+	 */
+	mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+	md = &context->mod_dispatch;
+
+	disable_vga_plb(EMGD_MMIO(mmio));
+
+	/* Turn off ports */
+	port = NULL;
+	while((port = md->dsp_get_next_port(context, port, 0)) != NULL) {
+		/* if the port is TV, then don't set the power to S3 as this causes
+		 * blank screen on analog port after killx or cosole mode,
+		 * probably because the external clock needs to be on till the pipes and
+		 * DPLLs are off
+		 */
+		if (port->pd_driver) {
+#ifndef CONFIG_FASTBOOT
+			if(port->pd_type == PD_DISPLAY_TVOUT) {
+				tv_port = port;
+			}else {
+				port->pd_driver->set_power(port->pd_context, IGD_POWERSTATE_D3);
+			}
+#endif
+		}
+
+		/* Disable WRITE protection on PIPE B for parts with Int-LVDS*/
+		/* This should never happen as the panel power was set to D3 above */
+		if (port->port_reg == LVDSCNTR) {
+			if(EMGD_READ32(EMGD_MMIO(mmio) + LVDS_PNL_PWR_CTL) & 0x1) {
+				EMGD_WRITE32(0xABCD0000, EMGD_MMIO(mmio) + LVDS_PNL_PWR_CTL);
+				i=0;
+				while(i++ < 0x10) {
+					OS_SLEEP(10);
+					if((EMGD_READ32(EMGD_MMIO(mmio)+LVDS_PNL_PWR_STS)&BIT(31))==0) {
+						break;
+					}
+				}
+			}
+		}
+
+		temp = EMGD_READ32(EMGD_MMIO(mmio) + port->port_reg);
+		EMGD_WRITE32((temp & ~BIT31), EMGD_MMIO(mmio) + port->port_reg);
+	}
+
+	/* disable plane C */
+	temp = EMGD_READ32(EMGD_MMIO(mmio) + DSPCCNTR);
+	if(temp & BIT31) {
+		EMGD_WRITE32(0x0, EMGD_MMIO(mmio) + DSPCCNTR);
+		EMGD_WRITE32(0x0, EMGD_MMIO(mmio) + DSPCCNTR + DSP_START_OFFSET);
+	}
+
+	plane = NULL;
+	while ((plane = md->dsp_get_next_plane(context, plane, 1)) != NULL) {
+		/* Only display display planes.
+		 *  Leave cursor, VGA, overlay, sprite planes alone since they will
+		 *  need a different disable bit/sequence.
+		 */
+		temp = EMGD_READ32(EMGD_MMIO(mmio) + plane->plane_reg);
+		if ((plane->plane_features & IGD_PLANE_DISPLAY)) {
+			if ( temp & BIT31 ) {
+				if(plane->plane_reg == DSPACNTR) {
+					EMGD_WRITE32((temp & device_data_plb->plane_a_preserve),
+						EMGD_MMIO(mmio) + plane->plane_reg);
+				}
+				else { /* if it's plane b or plane c */
+					EMGD_WRITE32((temp & device_data_plb->plane_b_c_preserve),
+						EMGD_MMIO(mmio) + plane->plane_reg);
+				}
+				EMGD_WRITE32(0, EMGD_MMIO(mmio) + plane->plane_reg+4);
+			}
+		} else if ((plane->plane_features & IGD_PLANE_CURSOR)) {
+			EMGD_WRITE32((temp & 0xffffffe8),
+				EMGD_MMIO(mmio) + plane->plane_reg);
+			EMGD_WRITE32(0, EMGD_MMIO(mmio) + plane->plane_reg+4);
+		}
+	}
+
+	/* Turn off pipes */
+	pipe = NULL;
+	while ((pipe = md->dsp_get_next_pipe(context, pipe, 0))) {
+		wait_for_vblank_plb(EMGD_MMIO(mmio), pipe->pipe_reg);
+		temp = EMGD_READ32(EMGD_MMIO(mmio) + pipe->pipe_reg);
+		if ( temp & BIT31 ) {
+			EMGD_WRITE32((temp & device_data_plb->pipe_preserve),
+				EMGD_MMIO(mmio) + pipe->pipe_reg);
+		}
+	}
+	/* pipes and DPLLs are off, now set the power for TV */
+	if(tv_port && tv_port->pd_driver) {
+		tv_port->pd_driver->set_power(tv_port->pd_context, IGD_POWERSTATE_D3);
+	}
+	EMGD_TRACE_EXIT;
+
+} /* end reset_plane_pipe_ports */
+
+/*!
+ *
+ * @param display
+ * @param port_number
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int	program_port_plb(igd_display_context_t *display,
+	unsigned short port_number,
+	unsigned long status)
+{
+	unsigned long pipe_number;
+	unsigned long port_control;
+	unsigned long port_control_analog = 0;
+	unsigned long mult_port_control;
+	unsigned long pd_powerstate = 0;
+	unsigned long upscale = 0;
+	pd_timing_t *timing;
+	pd_timing_t local_timing;
+	unsigned long port_type;
+	int ret;
+
+	/* get the pipe	number */
+	pipe_number = PIPE(display)->pipe_num;
+
+	/* get the timings */
+	timing = PIPE(display)->timing;
+
+	/* keep the port type as local as we access it frequently */
+	port_type = PORT(display, port_number)->port_type;
+
+	/* Reading the preservation	bits */
+	port_control = PORT(display, port_number)->preserve &
+		READ_MMIO_REG(display, PORT(display, port_number)->port_reg);
+
+	/* Reading the preservation bits for SDVO Gang Mode */
+	mult_port_control = PORT(display, port_number)->mult_preserve &
+		READ_MMIO_REG(display, PORT(display, port_number)->port_reg);
+
+	/* If status is false, quickly disable the display */
+	if(status == FALSE) {
+		ret = PORT(display, port_number)->pd_driver->set_power(
+				PORT(display, port_number)->pd_context, PD_POWER_MODE_D3);
+
+		if (ret) {
+			EMGD_ERROR_EXIT("PD set_power() returned: 0x%x", ret);
+			return -IGD_ERROR_INVAL;
+		}
+
+		if(port_type == IGD_PORT_DIGITAL) {
+			WRITE_MMIO_REG(display, PORT(display, port_number)->port_reg,
+					port_control);
+
+			if(PORT(display, port_number)->mult_port) {
+				/* either gang mode or RGBA */
+				WRITE_MMIO_REG(display,
+						PORT(display, port_number)->mult_port->port_reg,
+						mult_port_control);
+			}
+		} else if (port_type == IGD_PORT_ANALOG) {
+			port_control |= (BIT11 | BIT10);	 /* put in D3 state */
+			WRITE_MMIO_REG(display, PORT(display, port_number)->port_reg,
+				port_control);
+
+			return 0;
+		}
+		return 0;
+	}
+
+	EMGD_DEBUG("status isn't false, Check port enabled");
+
+	if(! (PORT(display, port_number)->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+		return 0;
+	}
+
+	/*
+	 * Is this is the magic mode then turn on VGA syncs
+	 */
+	EMGD_DEBUG("Check vga_sync");
+	if(PORT(display, port_number)->vga_sync == 1) {
+		/*
+		 * Is this is the magic mode then turn on VGA syncs
+		 */
+		EMGD_DEBUG("VGA sync true, is width x height 720 x 400?");
+		if((timing->width == 720) && (timing->height == 400)) {
+			EMGD_DEBUG("Modify port control and multi_port_control");
+			port_control |= (1L<<15);
+			mult_port_control |= (1L<<15);
+		}
+	}
+
+	EMGD_DEBUG("Check analog port");
+	if(port_type == IGD_PORT_ANALOG) {
+		port_control |= (0x80000000 | (pipe_number<<30));
+		if(timing->mode_info_flags & IGD_VSYNC_HIGH) {
+			port_control |= (1L<<4);
+		}
+		if(timing->mode_info_flags & IGD_HSYNC_HIGH) {
+			port_control |= (1L<<3);
+		}
+		/* To differentiate	between	analog and other ports */
+		port_control_analog = port_control;
+	}
+
+	EMGD_DEBUG("Get power state");
+	EMGD_DEBUG("power state = %ld ", GET_DISPLAY_POWER_STATE(display, port_number));
+	switch(GET_DISPLAY_POWER_STATE(display, port_number)) {
+	case IGD_POWERSTATE_D0:
+		EMGD_DEBUG("Power State is D0");
+		pi_pd_find_attr_and_value(PORT(display, port_number),
+			PD_ATTR_ID_PANEL_FIT,
+			0, /*no PD_FLAG for UPSCALING */
+			NULL, /* dont need the attr ptr*/
+			&upscale);
+		if(port_type == IGD_PORT_DIGITAL) {
+			/* Reach the end timing if upscaling is enabled */
+			if (timing->extn_ptr && upscale) {
+				timing = (pd_timing_t *)timing->extn_ptr;
+			}
+
+			local_timing = *timing;
+			if (upscale) {
+				/* For timings smaller than width 360 and height 200,
+				 * double the size. This is because the active area of the mode
+				 * is double the size of the resolution for these modes
+				 *  - Very tricky huh */
+				if (local_timing.width <= 360) {
+					local_timing.width <<= 1;
+				}
+				if (local_timing.height <= 200) {
+					local_timing.height <<= 1;
+				}
+			}
+
+			ret = PORT(display, port_number)->pd_driver->set_mode(
+				PORT(display, port_number)->pd_context, &local_timing, 0);
+			if (ret) {
+				EMGD_ERROR_EXIT("PD set_mode returned: 0x%x", ret);
+				return -IGD_ERROR_INVAL;
+			}
+
+			/* in Plba B-Speecs, there are no bits, *
+			 * for the polarity of the H-sync/V-sync */
+
+			/* in Plba B-Speecs, there are no bits,	 *
+			 * for data ordering/format for DVO data */
+			/* Gang-Mode and RGBA models are "exclusive-or" */
+			if((PORT(display, port_number)->pd_driver->flags) &
+					PD_FLAG_GANG_MODE) {
+				mult_port_control |= (1L<<16);
+			} else if(PORT(display, port_number)->pd_type == PD_DISPLAY_RGBA) {
+				mult_port_control |= (1L<<2);
+			}
+
+			timing = PIPE(display)->timing;
+
+			if(timing->dclk > 100000) {
+				/* 100MPs < pixel rate < 200MPs */
+				/* SDVO clock rate multiplier = 1x */
+				/*
+				port_control &=	~BIT23;
+				mult_port_control |= ~BIT23;
+					redundant code since BIT23 is
+					already 0 at this point
+				*/
+			} else if(timing->dclk > 50000) {
+				/* 50MPs < pixel rate < 100MPs */
+				/* SDVO clock rate multiplier = 2x */
+				port_control |= (1L<<23);
+				mult_port_control |= (1L<<23);
+			} else {
+				/* 25MPs < pixel rate < 50MPs */
+				/* SDVO clock rate multiplier = 4x */
+				port_control |= (3L<<23);
+				mult_port_control |= (3L<<23);
+			}
+
+			/*
+			 * BIT7 = enable the border
+			 *   Do we need to disable the SDVO border for native
+			 *   VGA timings(i.e.,	use	DE)?
+			 * BIT22->BIT19 = setup the clock phase-9
+			 * BIT29 = enable the stall
+			 *   Only set stall on DVO-B for gang mode
+			 * BIT30 = pipe number
+			 * BIT31 = port enable
+			 */
+			port_control |= ( ( pipe_number<<30 ) | (BIT31) | ((0x9l) << 19) |
+					(BIT29) | (BIT7) );
+			mult_port_control |= ( (BIT31) | ((0x9l) << 19) | (BIT7) );
+
+			WRITE_MMIO_REG(display, PORT(display, port_number)->port_reg,
+					port_control);
+
+			if(PORT(display, port_number)->mult_port) { /* gang mode or rgba*/
+				WRITE_MMIO_REG(display,
+						PORT(display, port_number)->mult_port->port_reg,
+						mult_port_control);
+			}
+			return 0;
+		} else if(port_type == IGD_PORT_LVDS) {
+			/*
+			 * There is	a special case for LVDS	scaling. If	the	timing is
+			 * the native one and the extension	points to another non-vga
+			 * mode	then send the extension	pointer.
+			 */
+			/* Reach the end timing to get user requested mode */
+			if(timing->extn_ptr) {
+				timing = (pd_timing_t *)timing->extn_ptr;
+			}
+		}
+		/* set mode	will take care of port control */
+		ret = PORT(display, port_number)->pd_driver->set_mode(
+			PORT(display, port_number)->pd_context, (pd_timing_t *)timing,
+			1<<PIPE(display)->pipe_num);
+		if (ret) {
+			EMGD_ERROR_EXIT("PD set_mode returned: 0x%x",ret);
+			return -IGD_INVAL;
+		}
+		break;
+	case IGD_POWERSTATE_D1:
+		port_control_analog &= ~0x80000000;
+		port_control_analog |= 0x00000800 &
+			~(PORT(display, port_number)->preserve);
+		pd_powerstate =PD_POWER_MODE_D1;
+		break;
+	case IGD_POWERSTATE_D2:
+		port_control_analog &= ~0x80000000;
+		port_control_analog |= 0x00000400 &
+			~(PORT(display, port_number)->preserve);
+		pd_powerstate = PD_POWER_MODE_D2;
+		break;
+	case IGD_POWERSTATE_D3:
+		port_control_analog &= ~0x80000000;
+		port_control_analog |= 0x00003c00 &
+			~(PORT(display, port_number)->preserve);
+		pd_powerstate = PD_POWER_MODE_D3;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid power state: 0x%lx",
+			GET_DISPLAY_POWER_STATE(display, port_number));
+		return -IGD_ERROR_INVAL;
+	}
+
+	ret = PORT(display, port_number)->pd_driver->set_power(
+			PORT(display, port_number)->pd_context, pd_powerstate);
+	if (ret) {
+		EMGD_ERROR_EXIT("PD set_power returned: 0x%x", ret);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if(port_type == IGD_PORT_DIGITAL) {
+		EMGD_DEBUG("Port_control = 0x%lx", port_control);
+
+		WRITE_MMIO_REG(display, PORT(display, port_number)->port_reg,
+				port_control);
+		if(PORT(display, port_number)->mult_port) { /* gang mode or rgba*/
+			WRITE_MMIO_REG(display,
+					PORT(display, port_number)->mult_port->port_reg,
+					mult_port_control);
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Status is currently not used
+ *
+ * @param display
+ * @param port_number
+ * @param status
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int post_program_port_plb(igd_display_context_t *display,
+	unsigned short port_number,
+	unsigned long status)
+{
+	int ret;
+	igd_display_port_t *port;
+	igd_timing_info_t  *timings;
+	unsigned long portreg;
+
+	EMGD_TRACE_ENTER;
+
+	port = PORT(display, port_number);
+	timings = PIPE(display)->timing;
+
+	/*
+	 * Writing the sDVO control register here works around a problem
+	 * where the sDVO port is not turning on when using a CH7308
+	 * card in a 915 GM based system and the port order is 5200.
+	 *
+	 * In addition, post_set_mode() below, will report that the
+	 * "inputs are not trained", however, this does not seem to
+	 * have any negative effects.
+	 */
+	portreg = READ_MMIO_REG(display, port->port_reg);
+	WRITE_MMIO_REG(display, port->port_reg, (portreg & ~BIT31));
+	WRITE_MMIO_REG(display, port->port_reg, portreg);
+
+	/* Reenable/Redisable other	port */
+	if (port->port_reg == 0x61140) {
+		WRITE_MMIO_REG(display, 0x61160, READ_MMIO_REG(display,
+					0x61160));
+	} else {
+		WRITE_MMIO_REG(display, 0x61140,
+				READ_MMIO_REG(display, 0x61140));
+	}
+
+	/*
+	 * Added for Lakeport A0
+	 * Port clock multiplier bits 4-7, needs to be rewritten
+	 */
+	WRITE_MMIO_REG(display, PIPE(display)->clock_reg->dpll_control,
+		READ_MMIO_REG(display, PIPE(display)->clock_reg->dpll_control));
+
+	/* We must wait for 150 us for the dpll clock to warm up */
+	OS_SLEEP(150);
+
+	ret = 0;
+	/* call post_set_mode() if exists */
+	if (port->pd_driver->post_set_mode) {
+		ret = port->pd_driver->post_set_mode(port->pd_context, timings,
+			1<<PIPE(display)->pipe_num);
+		if (ret) {
+			EMGD_ERROR("PD post_set_mode returned: 0x%x", ret);
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+int program_cdvo_plb(void)
+{
+	return TRUE;
+}
+
+/*!
+ *
+ * @param gpio
+ *
+ * @return size
+ */
+static unsigned long get_gpio_sets_plb(unsigned long **gpio)
+{
+	*gpio = gpio_plb;
+	return sizeof(gpio_plb)/sizeof(unsigned long);
+}
+
+/*!
+ *
+ * @param context
+ * @param in_list
+ *
+ * @return void - To small to trace
+ */
+static void filter_modes_plb(igd_context_t *context, igd_display_port_t *port,
+	pd_timing_t *in_list)
+{
+
+	return;
+}
+
+#ifndef CONFIG_MICRO
+int check_display_plb(igd_display_context_t *display,
+					  unsigned short port_number, unsigned long status){
+	/* For poulsbo display should be working fine */
+	return TRUE;
+}
+#endif
+
+mode_dispatch_t mode_dispatch_plb = {
+	igd_set_palette_entry_plb,
+	igd_get_palette_entry_plb,
+	igd_wait_vblank_plb,
+	program_plane_plb,
+	program_pipe_plb,
+	program_port_plb,
+	post_program_port_plb,
+	program_clock_plb,
+	program_cdvo_plb,
+	reset_plane_pipe_ports_plb,
+	get_gpio_sets_plb,
+	filter_modes_plb,
+	OPT_MICRO_VALUE(&check_display_plb, NULL),
+	NULL,
+	NULL,
+	NULL,
+	NULL,
+	OPT_MICRO_VALUE(&mode_full_dispatch_plb, NULL),
+};
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.c b/drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.c
new file mode 100644
index 0000000..ac6ea5d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.c
@@ -0,0 +1,1946 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_plb.c
+ * $Revision: 1.34 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Almador Core implementations for the mode dispatch functions.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <io.h>
+
+#include <igd.h>
+#include <igd_pwr.h>
+
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+#include <dsp.h>
+#include <intelpci.h>
+#include <mode_access.h>
+#include <rb.h>
+#include <pi.h>
+#include <math_fix.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+#include <plb/mi.h>
+#include <plb/cmd.h>
+
+#include "drm_emgd_private.h"
+#include "../cmn/match.h"
+#include "../cmn/mode_dispatch.h"
+
+#include "emgd_shared.h"
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <drm/drmP.h>
+
+
+/* Registered VBlank interrupt callbacks (one-per-pipe): */
+static emgd_vblank_callback_t interrupt_callbacks_plb[IGD_MAX_PORTS] =
+	{{NULL,NULL,0},{NULL,NULL,1},{NULL,NULL,2},{NULL,NULL,3},{NULL,NULL,4}};
+
+/* This variable contains a number of bits, which tell whether the interrupt
+ * code has enabled interrupts, and if so, for what software and for what
+ * port(s); and whether certain events have happened.
+ */
+static unsigned long vblank_interrupt_state = 0;
+
+/* This variables keeps track of the number of clients currently using
+ * the vblank interrupt
+ */
+static int vblank_interrupt_ref_cnt_port2 = 0;
+static int vblank_interrupt_ref_cnt_port4 = 0;
+
+/* Spin lock for synchronization of the vblank_interrupt_state variable,
+ * between the VBlank interrupt handler and the non-interrupt handler code:
+ */
+DEFINE_SPINLOCK(vblank_lock_plb);
+
+
+
+int set_flip_pending_plb(unsigned char *mmio, unsigned long pipe_status_reg);
+int check_flip_pending_plb(unsigned char *mmio, unsigned long pipe_status_reg);
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+int wait_for_vblank_plb(unsigned char *mmio, unsigned long pipe_reg);
+
+void notify_userspace_vblank(struct drm_device *dev, int port);
+
+/*!
+ *
+ * @param display
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL if color attributes not found
+ */
+static int set_color_correct_plb(igd_display_context_t *display)
+{
+	const int        MID_PIXEL_VAL    = 125;
+	const int        MAX_PIXEL_VAL    = 255;
+	const int        NUM_PALETTE_ENTRIES = 256;
+
+	unsigned int     gamma_r_max_24i_8f, gamma_r_min_24i_8f;
+	unsigned int     gamma_g_max_24i_8f, gamma_g_min_24i_8f;
+	unsigned int     gamma_b_max_24i_8f, gamma_b_min_24i_8f;
+	unsigned int     new_gamma_r_24i_8f, new_gamma_g_24i_8f;
+	unsigned int     new_gamma_b_24i_8f;
+	unsigned int     gamma_normal_r_24i_8f, gamma_normal_g_24i_8f;
+	unsigned int     gamma_normal_b_24i_8f;
+	int              brightness_factor_r, brightness_factor_g;
+	int              brightness_factor_b;
+	int              contrast_factor_r, contrast_factor_g;
+	int              contrast_factor_b;
+
+	unsigned int      *palette;
+	unsigned int      i;
+
+	igd_range_attr_t *gamma_attr      = NULL, *contrast_attr = NULL;
+	igd_range_attr_t *brightness_attr = NULL;
+	igd_attr_t       *hal_attr_list  = PORT_OWNER(display)->attributes;
+
+
+	/* Using OS_ALLOC to avoid using > 1024 on stack (frame size warning ) */
+	palette = OS_ALLOC(sizeof (unsigned int) * NUM_PALETTE_ENTRIES);
+
+	/* start with a fresh palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		palette[i] = (i << 16) | (i << 8) | i;
+	}
+
+	/* get a pointer to gamma, contrast, and brightness attr */
+	i = 0;
+
+	while (PD_ATTR_LIST_END != hal_attr_list[i].id) {
+		switch (hal_attr_list[i].id) {
+		case PD_ATTR_ID_FB_GAMMA:
+			gamma_attr      = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_BRIGHTNESS:
+			brightness_attr = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_CONTRAST:
+			contrast_attr   = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		default:
+			break;
+		}
+
+		i++;
+	}
+
+	if(!gamma_attr || !brightness_attr || !contrast_attr) {
+		EMGD_ERROR("Color Correction Atrributes not found!");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Get the max and min */
+	gamma_r_max_24i_8f = ((gamma_attr->max >> 16) & 0xFF) << 3;
+	gamma_g_max_24i_8f = ((gamma_attr->max >>  8) & 0xFF) << 3;
+	gamma_b_max_24i_8f =  (gamma_attr->max        & 0xFF) << 3;
+
+	gamma_r_min_24i_8f = ((gamma_attr->min >> 16) & 0xFF) << 3;
+	gamma_g_min_24i_8f = ((gamma_attr->min >>  8) & 0xFF) << 3;
+	gamma_b_min_24i_8f =  (gamma_attr->min        & 0xFF) << 3;
+
+	/* The new gamma values are in 3i.5f format, but we must convert it
+	 * to 24i.8f format before passing it to OS_POW_FIX
+	 */
+	new_gamma_r_24i_8f = ((gamma_attr->current_value >> 16) & 0xFF) << 3;
+	new_gamma_g_24i_8f = ((gamma_attr->current_value >> 8) & 0xFF) << 3;
+	new_gamma_b_24i_8f = (gamma_attr->current_value & 0xFF) << 3;
+
+	/* make sure the new gamma is within range */
+	new_gamma_r_24i_8f = OS_MIN(gamma_r_max_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_r_24i_8f = OS_MAX(gamma_r_min_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_g_24i_8f = OS_MIN(gamma_g_max_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_g_24i_8f = OS_MAX(gamma_g_min_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_b_24i_8f = OS_MIN(gamma_b_max_24i_8f, new_gamma_b_24i_8f);
+	new_gamma_b_24i_8f = OS_MAX(gamma_b_min_24i_8f, new_gamma_b_24i_8f);
+
+
+	gamma_normal_r_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_r_24i_8f);
+
+	gamma_normal_g_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_g_24i_8f);
+
+	gamma_normal_b_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_b_24i_8f);
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		unsigned int new_gamma;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* Note that we do not try to calculate the gamma if it
+		 * is 1.0, e.g. 0x100.  This is to avoid round-off errors
+		 */
+
+		/* red: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_r_24i_8f) {
+			cur_color  = (cur_palette >> 16) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_r_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_r_24i_8f;
+			palette[i] &= 0x00FFFF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_g_24i_8f) {
+			cur_color  = (cur_palette >> 8) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_g_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_g_24i_8f;
+			palette[i] &= 0xFF00FF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_b_24i_8f) {
+			cur_color  = cur_palette & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_b_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_b_24i_8f;
+			palette[i] &= 0xFFFF00;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF);
+		}
+	}
+
+
+	/* Brightness correction */
+	brightness_factor_r = (brightness_attr->current_value >> 16) & 0xFF;
+	brightness_factor_g = (brightness_attr->current_value >> 8) & 0xFF;
+	brightness_factor_b = brightness_attr->current_value & 0xFF;
+
+	/* The factors are offset by 0x80 because 0x80 is 0 correction */
+	brightness_factor_r -= 0x80;
+	brightness_factor_g -= 0x80;
+	brightness_factor_b -= 0x80;
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int          new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 16) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_r;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0x00FFFF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+
+		/* green: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 8) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_g;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFF00FF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+
+		/* blue: calculate and make sure the result is within range */
+		cur_color     =  cur_palette & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_b;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFFFF00;
+		palette[i]    |= OS_MAX(new_pixel_val, 0) & 0xFF;
+	}
+
+
+	/* contrast correction */
+	contrast_factor_r = (contrast_attr->current_value >> 16) & 0xFF;
+	contrast_factor_g = (contrast_attr->current_value >> 8) & 0xFF;
+	contrast_factor_b = contrast_attr->current_value & 0xFF;
+
+	/* make sure values are within range */
+	contrast_factor_r -= 0x80;
+	contrast_factor_g -= 0x80;
+	contrast_factor_b -= 0x80;
+
+
+	/* We're doing integer division in this loop using 16i.16f
+	 * integers.  The result will then be converted back into a
+	 * regular, 32-bit integer
+	 */
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		if (0 != contrast_factor_r ) {
+			cur_color     = (cur_palette >> 16) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_r);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0x00FFFF;  /* clear out the R color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0 != contrast_factor_g ) {
+			cur_color     = (cur_palette >> 8) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_g);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFF00FF;  /* clear out the G color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0 != contrast_factor_b) {
+			cur_color     = cur_palette & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_b);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFFFF00;  /* clear out the B color */
+			palette[i]    |=   OS_MAX(new_pixel_val, 0) & 0xFF;
+		}
+	}
+
+
+	/* write the new values in the palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		EMGD_WRITE32(palette[i], MMIO(display) +
+			PIPE(display)->palette_reg + i*4);
+	}
+	OS_FREE(palette);
+
+	return 0;
+}
+
+/*!
+ * Note: When panning in clone mode, the clone must be panned seperatly
+ * because it may (probably does) have a different x,y offset.
+ *
+ * @param display
+ * @param fb
+ * @param x
+ * @param y
+ *
+ * @return 0
+ */
+static int set_display_base_plb(igd_display_context_t *display,
+		igd_framebuffer_info_t *fb, unsigned long *x, unsigned long *y)
+{
+	unsigned long base;
+
+	/* FIXME/TODO: Compare the difference between the plb/tnc versions of this
+	 * function, as the plb code adds-in the offset of the frame buffer.
+	 */
+	base = fb->visible_offset;
+
+	base += ((*y * fb->screen_pitch) + (*x * IGD_PF_BYPP(fb->pixel_format)));
+
+	WRITE_MMIO_REG(display, PLANE(display)->plane_reg
+		+ DSP_START_OFFSET, base);
+
+#if 0
+	/* If this plane is a mirrored plane, then update the other plane offset */
+	if (PLANE(display)->mirror) {
+		WRITE_MMIO_REG(display, PLANE(display)->mirror->plane_reg
+				+ DSP_START_OFFSET, base);
+	}
+#endif
+
+	return 0;
+}
+
+/*!
+ * This function alters the position parameters associated with a cursor.
+ *
+ * @param display_handle
+ * @param cursor_info
+ *
+ * @return 0
+ */
+static int igd_alter_cursor_pos_plb(igd_display_h display_handle,
+	igd_cursor_info_t *cursor_info)
+{
+	unsigned long cursor_reg;
+	unsigned long new_pos;
+	unsigned long cursor_base;
+
+	igd_display_context_t *display = (igd_display_context_t *) display_handle;
+	cursor_reg = PIPE(display)->cursor->cursor_reg;
+
+	if (0x27 & READ_MMIO_REG(display, cursor_reg)) {
+		/* unlike almador, for plba, u must program the base offset
+		to trigger the position update. However, this also means we
+		accidentally enable an invalid cursor surface if the cursor
+		was not enabled already. So do this check first */
+
+		/*
+		 * Encode the cursor position in the format required for the
+		 * cursor position register.
+		 */
+		if(cursor_info->y_offset >= 0) {
+			new_pos = (cursor_info->y_offset << 16);
+		} else {
+			new_pos = ((-(cursor_info->y_offset)) << 16) | 0x80000000;
+		}
+		if(cursor_info->x_offset >= 0) {
+			new_pos |= (cursor_info->x_offset);
+		} else {
+			new_pos |= (-(cursor_info->x_offset)) | 0x00008000;
+		}
+
+		cursor_base = READ_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET);
+
+		WRITE_MMIO_REG(display, cursor_reg + CUR_POS_OFFSET, new_pos);
+		WRITE_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET, cursor_base);
+	}
+
+	return 0;
+}
+
+/*!
+ * The assumption here is that palette_colors points to index 0 and
+ * this function indexes into the palette_colors array by start_index
+ *
+ * @param display_handle
+ * @param palette_colors
+ * @param start_index
+ * @param count
+ *
+ * @return 0
+ */
+static int igd_set_palette_entries_plb(
+	igd_display_h display_handle,
+	unsigned long *palette_colors,
+	unsigned int start_index,
+	unsigned int count)
+{
+	unsigned int i;
+
+	for(i=start_index; i<start_index+count; i++) {
+		EMGD_WRITE32(palette_colors[i],
+			MMIO(display_handle) + PIPE(display_handle)->palette_reg + i*4);
+	}
+
+	return 0;
+}
+
+/*!
+ * This procedure waits for the next vertical sync
+ * period. If the display is already in a vertical sync period, this
+ * procedure exits.
+ *
+ * Note: A timeout is included to prevent an endless loop.
+ *
+ * @param display_handle
+ * @param palette_colors
+ * @param start_index
+ * @param count
+ *
+ * @return FALSE - if timed out
+ */
+static int igd_wait_vsync_plb(igd_display_h display_handle)
+{
+	long i = 0;	/* General counter */
+	unsigned long tmp;
+	unsigned char *Status_Reg;
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	unsigned char *mmio = MMIO(display);
+
+	Status_Reg = mmio + PIPE(display)->pipe_reg + PIPE_STATUS_OFFSET;
+
+	/* If pipe is off then just return */
+	if(!((1<<31) & EMGD_READ32(mmio + PIPE(display)->pipe_reg))) {
+		return 1;
+	}
+
+	/* 1. Disable VSync interrupt */
+	tmp = EMGD_READ32(Status_Reg);
+	/* The sticky status bits are cleared by writing a 1, so zero them: */
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	EMGD_WRITE32 (tmp & ~(1<<25), Status_Reg);
+
+	/* 2. Clear interrupt status (by writing a 1) */
+	tmp = EMGD_READ32(Status_Reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	EMGD_WRITE32 (tmp | (1<<9), Status_Reg);
+
+	/* 3. Enable VSync interrupt */
+	tmp = EMGD_READ32(Status_Reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	EMGD_WRITE32 (tmp | (1<<25), Status_Reg);
+
+	/* 4. Wait for VSync */
+	while ((i++ < 0x1000000) &&  /* Check for timeout */
+			((EMGD_READ32(Status_Reg) & (1<<9)) == 0x00)) {
+		;
+	}
+
+	/* 5. Disable VSync interrupt */
+	tmp = EMGD_READ32(Status_Reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	EMGD_WRITE32 (tmp & ~(1<<25), Status_Reg);
+
+	/* 6. Clear interrupt status (by writing a 1) */
+	tmp = EMGD_READ32(Status_Reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	EMGD_WRITE32 (tmp | (1<<9), Status_Reg);
+
+	if (i >= 0x10000000)
+		return (0);
+
+	return (1);
+}  /* igd_wait_vsync*/
+
+
+/*!
+ *
+ * @param display_handle
+ * @param scanline
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+
+static int igd_get_scanline_plb(igd_display_h display_handle, int *scanline)
+{
+	unsigned int tmp;
+	unsigned char *reg;
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	unsigned long fb_height = PLANE(display)->fb_info->height;
+	unsigned long dp_height = PIPE(display)->timing->height;
+
+	/* Scanline reg is -8 from control reg */
+	reg = MMIO(display) + PIPE(display)->pipe_reg - 0x8;
+
+	tmp = EMGD_READ32(reg);
+
+	if(!(PORT_OWNER(display)->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	tmp = (tmp * fb_height) / dp_height;
+
+	if(tmp >= fb_height) {
+		*scanline = IGD_IN_VBLANK;
+	} else {
+		*scanline = (int)tmp;
+	}
+	return 0;
+} /* end igd_get_scanline() */
+
+/*!
+ *
+ * @param display_handle
+ *
+ * @return 1 if TRUE
+ * @return 0 if FALSE
+ */
+static int igd_query_in_vblank_plb(igd_display_h display_handle)
+{
+	int sl;
+
+	igd_get_scanline_plb(display_handle, &sl);
+	if (sl == IGD_IN_VBLANK) {
+		return 1; /*TRUE*/
+	} else {
+		return 0; /*FALSE*/
+	}
+}
+
+/*!
+ * This function programs the cursor registers for Grantsdale
+ *
+ * @param display
+ * @param status
+ *
+ * @return void
+ */
+static void program_cursor_plb(igd_display_context_t *display,
+	unsigned long status)
+{
+	unsigned long cursor_reg;
+	unsigned long cursor_control = 0x00000000;
+	unsigned long cursor_pos;
+	unsigned long cursor_base;
+	igd_cursor_info_t *cursor_info;
+	int i;
+
+	EMGD_DEBUG("Enter program_cursor: %s", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("Device power state: D%ld", GET_DEVICE_POWER_STATE(display));
+
+	if (!(PIPE(display)->cursor)) {
+		return;
+	}
+
+	cursor_reg = PIPE(display)->cursor->cursor_reg;
+	cursor_info = PIPE(display)->cursor->cursor_info;
+
+	/* Turn off cursor before changing anything */
+	cursor_base = READ_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET);
+
+	WRITE_MMIO_REG(display, cursor_reg, cursor_control);
+	WRITE_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET, cursor_base);
+
+	if(cursor_info->flags & IGD_CURSOR_GAMMA) {
+		cursor_control |= BIT26;
+	}
+
+	cursor_info->argb_pitch = 64*4;
+	cursor_info->xor_pitch = 16;
+
+	/* Setting the cursor format/pitch */
+	switch(cursor_info->pixel_format) {
+	case IGD_PF_ARGB32:
+		cursor_control |= BIT5 | 0x7;
+		break;
+	case IGD_PF_RGB_XOR_2:
+		cursor_control |= 0x5;
+		break;
+	case IGD_PF_RGB_T_2:
+		cursor_control |= 0x4;
+		break;
+	case IGD_PF_RGB_2:
+		cursor_control |= 0x6;
+		break;
+	default:
+		return;
+	}
+
+	switch(cursor_info->pixel_format) {
+	case IGD_PF_ARGB32:
+		if(display->context->dispatch.gmm_virt_to_phys(
+				cursor_info->argb_offset, &cursor_base)) {
+			EMGD_ERROR("No Phys pointer available for ARGB cursor");
+			return;
+		}
+		break;
+	default:
+		if(display->context->dispatch.gmm_virt_to_phys(
+				cursor_info->xor_offset, &cursor_base)) {
+			EMGD_ERROR("No Phys pointer available for XOR cursor");
+			return;
+		}
+		break;
+	}
+
+	/* If status is FALSE return with the cursor off */
+	if((!status) ||
+		(GET_DEVICE_POWER_STATE(display) == IGD_POWERSTATE_D3)) {
+		return;
+	}
+
+	if(cursor_info->y_offset >= 0) {
+		cursor_pos = cursor_info->y_offset << 16;
+	} else {
+		cursor_pos = ((-(cursor_info->y_offset)) << 16) | 0x80000000;
+	}
+	if(cursor_info->x_offset >= 0) {
+		cursor_pos |= cursor_info->x_offset;
+	} else {
+		cursor_pos |= (-(cursor_info->x_offset)) | 0x00008000;
+	}
+
+	WRITE_MMIO_REG(display, cursor_reg + CUR_POS_OFFSET,
+		cursor_pos);
+
+	for(i=0; i<4; i++) {
+		WRITE_MMIO_REG(display, cursor_reg + CUR_PAL0_OFFSET + i*4,
+			cursor_info->palette[i]);
+	}
+
+	WRITE_MMIO_REG(display, cursor_reg,
+		cursor_control | (PIPE(display)->pipe_num<<28));
+	WRITE_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET, cursor_base);
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param type
+ * @param surface
+ * @param appcontext
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_get_surface_plb(igd_display_h display_handle,
+	igd_buffertype_t type,
+	igd_surface_t *surface,
+	igd_appcontext_h appcontext)
+{
+#if 0
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	state3d_plb_t *state = STATE3D_PLB(appcontext);
+
+	if(!surface) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch(type) {
+	case IGD_BUFFER_DISPLAY:
+		surface->offset = PLANE(display)->fb_info->visible_offset;
+		surface->pitch = PLANE(display)->fb_info->screen_pitch;
+		surface->width = PLANE(display)->fb_info->width;
+		surface->height = PLANE(display)->fb_info->height;
+		surface->u_offset = 0;
+		surface->u_pitch = 0;
+		surface->v_offset = 0;
+		surface->v_pitch = 0;
+		surface->pixel_format = PLANE(display)->fb_info->pixel_format;
+		surface->palette_info = 0;
+		surface->flags = PLANE(display)->fb_info->flags;
+		surface->logic_ops = 0;
+		surface->render_ops = 0;
+		surface->alpha = 0;
+		surface->diffuse = 0;
+		surface->chroma_high = 0;
+		surface->chroma_low = 0;
+		return 0;
+	case IGD_BUFFER_COLOR:
+		OS_MEMCPY(surface, &state->color_buffer,
+			sizeof(igd_surface_t));
+		return 0;
+	case IGD_BUFFER_DEPTH:
+		OS_MEMCPY(surface, &state->depth_buffer,
+			sizeof(igd_surface_t));
+		return 0;
+	default:
+		EMGD_ERROR("Invalid type in get_surface");
+		break;
+	}
+
+	return -IGD_ERROR_INVAL;
+#else
+	return 0;
+#endif
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param type
+ * @param surface
+ * @param appcontext
+ * @param flags
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_set_surface_plb(igd_display_h display_handle,
+	int priority,
+	igd_buffertype_t type,
+	igd_surface_t *surface,
+	igd_appcontext_h appcontext,
+	unsigned long flags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	unsigned int dsp_current;
+	unsigned long plane_reg;
+	unsigned long plane_control;
+	unsigned long surface_offset;
+
+	EMGD_TRACE_ENTER;
+	if(!surface) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch(type) {
+	case IGD_BUFFER_DISPLAY:
+		if(! (surface->flags & IGD_SURFACE_DISPLAY)) {
+			EMGD_ERROR_EXIT("Surface is not a display surface");
+			return -IGD_ERROR_INVAL;
+		}
+
+		if (PLANE(display)->plane_reg == DSPACNTR) {
+			EMGD_DEBUG("About to flip a buffer for display/pipe A");
+			dsp_current = 0;
+		} else {
+			EMGD_DEBUG("About to flip a buffer for display/pipe B");
+			dsp_current = 1;
+		}
+
+		if(flags & IGD_BUFFER_WAIT) {
+			/* If this is just a wait for flip, so return */
+			return 0;
+		}
+
+		/*
+		 * Async flips only work when the offset is on a 256kb boundary.
+		 */
+		if(PLANE(display)->fb_info->visible_offset & 0x3ffff) {
+			EMGD_ERROR("FB offset must be 256kb aligned in Poulsbo");
+		}
+
+		/* Save new fb_info */
+		PLANE(display)->fb_info->visible_offset = surface->offset;
+		PLANE(display)->fb_info->screen_pitch = surface->pitch;
+		PLANE(display)->fb_info->width = surface->width;
+		PLANE(display)->fb_info->height = surface->height;
+		PLANE(display)->fb_info->pixel_format = surface->pixel_format;
+		PLANE(display)->fb_info->flags = surface->flags;
+
+		/* Get the correct stride and stereo */
+		/* TODO - Does Poulsbo flip need to handle stereo mode? */
+		/*mode_get_stride_stereo_plb(display, &stride, &stereo, 0);*/
+
+		/* calculate the real offset, taking panning into account */
+		surface_offset = surface->offset;
+		surface_offset +=
+			(PORT_OWNER(display)->pt_info->y_offset * surface->pitch) +
+			(PORT_OWNER(display)->pt_info->x_offset *
+				IGD_PF_BYPP(surface->pixel_format));
+		EMGD_DEBUG("surface_offset = 0x%08lx", surface_offset);
+
+		plane_reg = PLANE(display)->plane_reg;
+		plane_control = EMGD_READ32(MMIO(display) + plane_reg);
+
+		/* Perform the flip by doing the following:
+		 *
+		 *   Write the current plane_control value to the plane_reg
+		 *   Write the surface offset to either:
+		 *     1) the plane_reg - 4  if async
+		 *     2) plane_reg + DSP_START_OFFSET (+4) if not async
+		 */
+		EMGD_WRITE32(plane_control, MMIO(display) + plane_reg);
+		EMGD_WRITE32(surface_offset,
+			MMIO(display) + plane_reg + DSP_START_OFFSET);
+
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_COLOR:
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_DEPTH:
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_SAVE:
+		PLANE(display)->fb_info->saved_offset = surface->offset;
+		EMGD_DEBUG("saving surface_offset = 0x%08lx", surface->offset);
+		EMGD_TRACE_EXIT;
+		return 0;
+	default:
+		EMGD_ERROR("Invalid type in set_surface");
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/* Poulsbo does not support a flip pending, since there is no
+ * Display Buffer Info instruction.  So this must be done with vBlank.
+ * However, a wait_for_vblank can be given while a flip is also in
+ * progress, so a semaphore is required when changing flip_pending or
+ * when modifying the vBlank interrupt bits.
+ *
+ * This function should only be called with a flip_mutex around it */
+int set_flip_pending_plb(unsigned char *mmio, unsigned long pipe_status_reg)
+{
+	platform_context_plb_t *plb_context =
+		(platform_context_plb_t *)mode_context->context->platform_context;
+	unsigned long request_for;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT((pipe_status_reg == PIPEA_STAT) ||
+		(pipe_status_reg == PIPEB_STAT), "Invalid pipe_status_reg", 0);
+
+	if (pipe_status_reg == PIPEA_STAT) {
+		plb_context->flip_pending |= PLB_FLIP_PIPE_A_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT2);
+	} else {
+		plb_context->flip_pending |= PLB_FLIP_PIPE_B_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT4);
+	}
+	mode_context->dispatch->full->request_vblanks(request_for, mmio);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/* This function should only be called with a flip_mutex around it */
+int check_flip_pending_plb(unsigned char *mmio,
+	unsigned long pipe_status_reg)
+{
+	platform_context_plb_t *plb_context =
+		(platform_context_plb_t *)mode_context->context->platform_context;
+	unsigned long request_for;
+	unsigned int flip_pending;
+
+	EMGD_TRACE_ENTER;
+	EMGD_ASSERT((pipe_status_reg == PIPEA_STAT) ||
+		(pipe_status_reg == PIPEB_STAT), "Invalid pipe_status_reg", 0);
+
+	if (pipe_status_reg == PIPEA_STAT) {
+		flip_pending = plb_context->flip_pending & PLB_FLIP_PIPE_A_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT2);
+	} else {
+		flip_pending = plb_context->flip_pending & PLB_FLIP_PIPE_B_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT4);
+	}
+
+	if (flip_pending) {
+		if (mode_context->dispatch->full->vblank_occured(request_for)) {
+			/* VBlank occured, flip complete */
+			plb_context->flip_pending &= ~flip_pending;
+			mode_context->dispatch->full->end_request(request_for, mmio);
+			EMGD_DEBUG("VBlank occured--returning 0");
+			return 0;
+		} else {
+			/* VBlank not done, flip still in progress */
+			EMGD_DEBUG("VBlank hasn't yet occured--returning 1");
+			return 1;
+		}
+	} else {
+		/* No flip pending, so it must have completed */
+		EMGD_DEBUG("returning 0");
+		return 0;
+	}
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param event
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_query_event_plb(igd_display_h display_handle,
+	igd_event_t event, unsigned long *status)
+{
+	platform_context_plb_t *plb_context =
+		(platform_context_plb_t *)mode_context->context->platform_context;
+	unsigned char *mmio = MMIO(display_handle);
+	unsigned long pipe_status_reg =
+		(PLANE(display_handle)->plane_reg == DSPACNTR) ? 0x70024 : 0x71024;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("mmio=0x%p, pipe_status_reg=0x%08lx", mmio, pipe_status_reg);
+
+	switch (event) {
+	case IGD_EVENT_FLIP_PENDING:
+		ret = OS_PTHREAD_MUTEX_LOCK(&plb_context->flip_mutex);
+		*status = check_flip_pending_plb(mmio, pipe_status_reg);
+		OS_PTHREAD_MUTEX_UNLOCK(&plb_context->flip_mutex);
+		break;
+	default:
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_DEBUG("Returning status=%lu", *status);
+	return IGD_SUCCESS;
+}
+
+/*!
+ * Function gets the width, height and pitch of the framebuffer.
+ *
+ * @param void
+ *
+ * @return 0
+ */
+static int get_plane_info_plb(void)
+{
+	igd_framebuffer_info_t *buffer_info;
+	unsigned char* mmio = NULL;
+	unsigned long plane_control = 0;
+	unsigned long reg = 0;
+
+	EMGD_TRACE_ENTER;
+
+	mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	/* Check that plane A is active and process it */
+	plane_control = EMGD_READ32(mmio + DSPACNTR);
+	if(plane_control & PLANE_ENABLE){
+		buffer_info = &mode_context->fw_info->fb_info[0];
+
+		/* get the DSPASIZE register value */
+		reg = (unsigned long)EMGD_READ32(mmio + DSPACNTR + DSP_SIZE_OFFSET);
+		buffer_info[0].height = (reg >> 16) & 0xFFF;
+		buffer_info[0].width =	reg & 0xFFF;
+
+		/* get the DSPASTRIDE register value */
+		buffer_info[0].screen_pitch =
+			(unsigned int)EMGD_READ32(mmio + DSPACNTR + DSP_STRIDE_OFFSET);
+
+		/* Following are NOT offset by 1 in fb info */
+		buffer_info[0].width++;
+		buffer_info[0].height++;
+	}
+
+	/* Check that plane B is active and process it */
+	plane_control = EMGD_READ32(mmio + DSPBCNTR);
+	if(plane_control & PLANE_ENABLE){
+		buffer_info = &mode_context->fw_info->fb_info[0];
+
+		/* get the DSPBSIZE register value */
+		reg = (unsigned long)EMGD_READ32(mmio + DSPBCNTR + DSP_SIZE_OFFSET);
+		buffer_info[1].height = (reg >> 16) & 0xFFF;
+		buffer_info[1].width  =	reg & 0xFFF;
+
+		/* get the DSPBSTRIDE register value */
+		buffer_info[1].screen_pitch =
+			(unsigned int)EMGD_READ32(mmio + DSPBCNTR + DSP_STRIDE_OFFSET);
+
+		/* Following are NOT offset by 1 in fb info */
+		buffer_info[1].width++;
+		buffer_info[1].height++;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param void
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int get_pipe_info_plb(igd_display_h *display)
+{
+	unsigned char *mmio = NULL;
+	unsigned long pipe_conf = 0;
+	igd_display_info_t *timing;
+	unsigned long reg = 0;
+
+	EMGD_TRACE_ENTER;
+
+	mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	pipe_conf = EMGD_READ32(mmio + PIPEA_CONF);
+
+	if(pipe_conf & BIT(31)) { /* pipe A is active */
+		timing = &mode_context->fw_info->timing_arr[0];
+
+		reg = EMGD_READ32(mmio + HTOTAL_A);
+		timing[0].htotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[0].width = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + HBLANK_A);
+		timing[0].hblank_start = (unsigned short)reg & 0x1FFF;
+		timing[0].hblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + HSYNC_A);
+		timing[0].hsync_start = (unsigned short)reg & 0x1FFF;
+		timing[0].hsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + VTOTAL_A);
+		timing[0].vtotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[0].height = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + VBLANK_A);
+		timing[0].vblank_start = (unsigned short)reg & 0x1FFF;
+		timing[0].vblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + VSYNC_A);
+		timing[0].vsync_start = (unsigned short)reg & 0x1FFF;
+		timing[0].vsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		/* Following are not offset by 1 in ptinfo */
+		timing[0].width++;
+		timing[0].height++;
+
+		EMGD_DEBUG("Pipe A timing width = %d", timing[0].width);
+		EMGD_DEBUG("Pipe A timing width = %d", timing[0].height);
+
+		{
+			/* Calculate the firmware programmed dot clock */
+			unsigned long dplla, fpa0, fpa1;
+			unsigned long ma1, ma2, na, pa1, pa2, plla_select;
+			unsigned long ref_freq = 0, dclk;
+			unsigned long temp; /* To store intermediate values b4 dclk */
+			int j;
+
+			dplla = EMGD_READ32(mmio + DPLLACNTR);
+			fpa0  = EMGD_READ32(mmio + FPA0);
+			fpa1  = EMGD_READ32(mmio + FPA1);
+
+			if(dplla & BIT(31)) {
+
+				ma1 = (fpa0 >> 8) & 0x3F;    /* M1 is bits 13:8 */
+				ma2 = (fpa0) & 0x1F;         /* M1 is bits 5:0 */
+				na = (fpa0 >> 16) & 0x3F;    /* N is bits 21:16 */
+				pa1 = (dplla >> 16) & 0xFF; /* P1 is bits 23:16 */
+
+				/* Check for illegal values of P1
+				 * The bit representation MUST be power of 2
+				 * All other values are illegal including zero.
+				 */
+				if( (pa1 == 0) ||
+					( (pa1 & (pa1-1)) != 0 ) ) {
+
+					EMGD_ERROR("Invalid P1 bits set");
+					return -IGD_ERROR_INVAL;
+				}
+				for(j = 0; j < 8; j++) {
+					if(pa1 & BIT(j)) {  /* P1 is divide by 1 to 8 */
+						pa1 = j+1;
+						break;
+					}
+				}
+				pa2 = (dplla >> 24) & 0x3; /* P2 is bits 25:24 */
+
+				/* The post divisor values are different if the
+				 * attached port is internal LVDS. Since Pipe A
+				 * does not support internal LVDS, we just follow
+				 * the normal divisor values
+				 */
+				if(pa2 == 0) {
+					pa2 = 10;
+				} else if(pa2 == 1) {
+					pa2 = 5;
+				} else {
+					EMGD_ERROR("Invalid P2 bits set = 0x%lx", pa2);
+				}
+				plla_select = (dplla >> 13) & 0x3; /* PLL Ref I/P Select */
+
+				/* Equation that calculates the dot clk
+				 * -------------------------------------
+                 *
+				 * pll_freq_factor = ((float)(5 * (ma1+2)+(ma2+2))/(na+2))/
+				 *	((pa1*pa2));
+				 *
+				 * fdclk = pll_freq_factor * ref_freq * 1000000;
+				 *
+				 * Support for FPU in Kernel Code is not straightforward
+				 * we will just stick to int operations. We will just re-
+                 * arrange the factors.
+				 */
+				if(plla_select == 0) {
+					ref_freq = 96; /* 96MHz */
+				} else if( (plla_select == 1) || (plla_select == 3) ) {
+
+					EMGD_ERROR("Invalid PLL Reference Input Select Reserved");
+					return -IGD_ERROR_INVAL;
+
+				} else if(plla_select  == 2) {
+					EMGD_DEBUG("PLL ref is SDVO TV CLK");
+					/* TODO: How to handle this value? */
+					ref_freq = 0;
+				}
+
+				/* First let's multiply by 1000 * 1000
+				 * so that we don't get zero during integer
+				 * division
+				 */
+				temp = 1000 * 1000;
+				temp = temp * (5 * (ma1+2) + (ma2+2));
+				temp = temp/(na+2);
+				temp = temp/(pa1*pa2);
+
+				/* dclk  = temp * ref_freq; */
+
+				/*  FIXME:  This is a workaround to get dclk.  We are supposed
+				 *  to be calculating this based on the formula, but DPLL
+				 *  is somehow locked and does not return the programmed
+				 *  p1 value.  Once this is fixed, we no longer need to have
+				 *  igd_display_handle in the parameter of get_pipe_info
+				 */
+				if (NULL != display) {
+					/*igd_display_context_t *display_context =
+					  (igd_display_context_t *) display;*/
+
+					/* dclk = ref_freq * m / (p1 * p2); */
+
+					/* PIPE(display)->dclk is in KHhz */
+					dclk = PIPE(display)->dclk * 1000;
+
+				} else {
+					dclk = 0;
+				}
+
+				if( (dclk == 0) || (ref_freq == 0) ) {
+					EMGD_ERROR(" Dot Clock/Ref Frequency is Zero!!!");
+					return -IGD_ERROR_INVAL;
+				}
+				EMGD_DEBUG("Ref frequency = %lu", ref_freq);
+				EMGD_DEBUG("Pipe A constructed Dot clock is = %lu", dclk);
+				timing[0].dclk = dclk/1000; /* Make it to KHz */
+				EMGD_DEBUG("Pipe A Dot clock in KHz = %lu", timing[0].dclk);
+
+				timing[0].refresh = (unsigned short)(dclk/
+					((timing[0].htotal+1)*(timing[0].vtotal+1)));
+
+				EMGD_DEBUG("Pipe A refresh = %u", timing[0].refresh);
+			} /* if  DPLL A active */
+		} /* dot clock code block */
+	} /* if Pipe A active */
+
+	pipe_conf = EMGD_READ32(mmio + PIPEB_CONF);
+
+	if(pipe_conf & BIT(31)) { /* pipe B is active */
+		timing = &mode_context->fw_info->timing_arr[0];
+
+		reg = EMGD_READ32(mmio + HTOTAL_B);
+		timing[1].htotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[1].width = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + HBLANK_B);
+		timing[1].hblank_start = (unsigned short)reg & 0x1FFF;
+		timing[1].hblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + HSYNC_B);
+		timing[1].hsync_start = (unsigned short)reg & 0x1FFF;
+		timing[1].hsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + VTOTAL_B);
+		timing[1].vtotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[1].height = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + VBLANK_B);
+		timing[1].vblank_start = (unsigned short)reg & 0x1FFF;
+		timing[1].vblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		EMGD_READ32(mmio + VSYNC_B);
+		timing[1].vsync_start = (unsigned short)reg & 0x1FFF;
+		timing[1].vsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		/* Following are not offset by 1 in ptinfo */
+		timing[1].width++;
+		timing[1].height++;
+
+		EMGD_DEBUG("Pipe B timing width = %d", timing[1].width);
+		EMGD_DEBUG("Pipe B timing width = %d", timing[1].height);
+
+		{
+			/* Calculate the firmware programmed dot clock */
+			unsigned long dpllb, fpb0, fpb1;
+			unsigned long mb1, mb2, nb, pb1, pb2, pllb_select;
+			unsigned long ref_freq = 0, dclk;
+			unsigned long temp; /* To store intermediate values b4 dclk */
+			int j;
+			unsigned long lvds_port;
+
+			dpllb = EMGD_READ32(mmio + DPLLBCNTR);
+			fpb0  = EMGD_READ32(mmio + FPB0);
+			fpb1  = EMGD_READ32(mmio + FPB1);
+			/* Note: Only Pipe B supports LVDS */
+			lvds_port = EMGD_READ32(mmio + LVDSCNTR);
+
+			if(dpllb & BIT(31)) {
+
+				mb1 = (fpb0 >> 8) & 0x3F;    /* M1 is bits 13:8 */
+				mb2 = (fpb0) & 0x1F;         /* M1 is bits 5:0 */
+				nb = (fpb0 >> 16) & 0x3F;    /* N is bits 21:16 */
+				pb1 = (dpllb >> 16) & 0xFF; /* P1 is bits 23:16 */
+
+				/* Check for illegal values of P1
+				 * The bit representation MUST be power of 2
+				 * All other values are illegal including zero.
+				 */
+				if( (pb1 == 0) ||
+					( (pb1 & (pb1-1)) != 0 ) ) {
+					EMGD_ERROR("Invalid P1 bits set");
+					return -IGD_ERROR_INVAL;
+				}
+
+				for(j = 0; j < 8; j++) {
+					if(pb1 & BIT(j)) {  /* P1 is divide by 1 to 8 */
+						pb1 = j+1;
+						break;
+					}
+				}
+
+				pb2 = (dpllb >> 24) & 0x3; /* P2 is bits 25:24 */
+
+				/* For LVDS port, the post divisor factors are different */
+				if((lvds_port & 0xC0000000) == 0xC0000000) {
+					/* Pipe is used for LVDS port */
+					if(pb2 == 0) {
+						pb2 = 14;
+					} else if(pb2 == 1) {
+						pb2 = 7;
+					} else {
+						EMGD_ERROR("Invalid P2 bits set = 0x%lx", pb2);
+						return -IGD_ERROR_HWERROR;
+					}
+
+				} else {
+					/* Pipe is used for Non-LVDS port */
+					if(pb2 == 0) {
+						pb2 = 10;
+					} else if(pb2 == 1) {
+						pb2 = 5;
+					} else {
+						EMGD_ERROR(":Invalid P2 bits set = 0x%lx", pb2);
+						return -IGD_ERROR_HWERROR;
+					}
+
+				}
+
+				pllb_select = (dpllb >> 13) & 0x3; /* PLL Ref I/P Select */
+
+				/* Equation that calculates the dot clk
+				 * -------------------------------------
+                 *
+				 * pll_freq_factor = ((float)(5 * (mb1+2)+(mb2+2))/(nb+2))/
+				 *	((pb1*pb2));
+				 *
+				 * fdclk = pll_freq_factor * ref_freq * 1000000;
+				 *
+				 * Support for FPU in Kernel Code is not straightforward
+				 * we will just stick to int operations. We will just re-
+                 * arrange the factors.
+				 */
+				if(pllb_select == 0) {
+					ref_freq = 96; /* 96MHz */
+
+				} else if( (pllb_select == 1) || (pllb_select == 3) ) {
+
+					EMGD_ERROR("Invalid PLL Reference Input Select Reserved");
+					return -IGD_ERROR_INVAL;
+
+				} else if(pllb_select  == 2) {
+					EMGD_DEBUG("PLL ref is SDVO TV CLK");
+					/* TODO: How to handle this value? */
+					return -IGD_ERROR_INVAL;
+				}
+
+				/* First let's multiply by 1000 * 1000
+				 * so that we don't end up in zero during
+				 * integer division
+				 */
+				temp = 1000 * 1000;
+				temp = temp * (5 * (mb1+2) + (mb2+2));
+				temp = temp /(nb+2);
+				dclk = temp/(pb1*pb2);
+
+				/* dclk = temp * ref_freq; */
+				/*  FIXME:  This is a workaround to get dclk.  We are supposed
+				 *  to be calculating this based on the formula, but DPLL
+				 *  is somehow locked and does not return the programmed
+				 *  p1 value.  Once this is fixed, we no longer need to have
+				 *  igd_display_handle in the parameter of get_pipe_info
+				 */
+				if (NULL != display) {
+					/*igd_display_context_t *display_context =
+					  (igd_display_context_t *) display;*/
+
+					/* dclk = ref_freq * m / (p1 * p2); */
+
+					/* PIPE(display)->dclk is in KHhz */
+					dclk = PIPE(display)->dclk * 1000;
+
+				} else {
+					dclk = 0;
+				}
+
+
+				if( (dclk == 0) || (ref_freq == 0) ) {
+					EMGD_ERROR("Dot Clock/Ref Frequency is Zero!!!");
+					return -IGD_ERROR_INVAL;
+				}
+
+				EMGD_DEBUG("Ref frequency = %lu", ref_freq);
+				EMGD_DEBUG("Pipe B constructed Dot clock is = %lu", dclk);
+				timing[1].dclk = dclk/1000; /* Make it to KHz */
+				EMGD_DEBUG("Pipe B Dot clock in KHz = %lu", timing[1].dclk);
+
+				timing[1].refresh = (unsigned short) (dclk/
+					((timing[1].htotal+1)*(timing[1].vtotal+1)));
+
+				EMGD_DEBUG("Pipe B refresh = %u", timing[1].refresh);
+			} /* if  DPLL B  active */
+		} /* dot clock code block */
+	} /* if Pipe B is active */
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param void
+ *
+ * @return 0
+ */
+static int get_port_info_plb(void)
+{
+	/* TODO: Any port related info that needs to be populated ? */
+	EMGD_TRACE_ENTER;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+/*******************************************************************************
+ *
+ * The following code provides VBlank interrupt support both within and without
+ * the EMGD HAL.
+ *
+ ******************************************************************************/
+
+/*!
+ * Interrupt handler for VBlanks.
+ *
+ * @param irq (IN).  Unused.
+ *
+ * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+ * registers.
+ *
+ * @return A value to tell the kernel whether or not we handled the interrupt.
+ */
+static irqreturn_t interrupt_handler_plb(int irq, void* mmio)
+{
+	unsigned long iir;
+	unsigned long lock_flags;
+	unsigned long tmp;
+	unsigned long port2_interrupt = 0;
+	unsigned long port4_interrupt = 0;
+	emgd_vblank_callback_t *cb;
+
+	EMGD_TRACE_ENTER;
+
+
+	iir = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+
+	/* Detect whether a vblank interrupt occured, and if so, what type of
+	 * processing is needed (do the simple processing now):
+	 */
+	spin_lock_irqsave(&vblank_lock_plb, lock_flags);
+	if ((port2_interrupt = iir & BIT7 /* Port 2/Pipe A/SDVO-B */) != 0) {
+		if ((tmp = vblank_interrupt_state & VBLANK_INT4_PORT2) != 0) {
+			/* Record "answers" for all requestors: */
+			vblank_interrupt_state |= VBINT_ANSWER4_REQUEST(tmp);
+		}
+	}
+	if ((port4_interrupt = iir & BIT5 /* Port 4/Pipe B/Int-LVDS */) != 0) {
+		if ((tmp = vblank_interrupt_state & VBLANK_INT4_PORT4) != 0) {
+			/* Record "answers" for all requestors: */
+			vblank_interrupt_state |= VBINT_ANSWER4_REQUEST(tmp);
+		}
+	}
+	spin_unlock_irqrestore(&vblank_lock_plb, lock_flags);
+
+
+	if (port4_interrupt) {
+		if (mode_context->batch_blits[IGD_PORT_TYPE_LVDS - 1]) {
+			notify_userspace_vblank(mode_context->context->drm_dev,
+				IGD_PORT_TYPE_LVDS);
+		}
+	}
+	else if (port2_interrupt) {
+		if (mode_context->batch_blits[IGD_PORT_TYPE_SDVOB - 1]) {
+			notify_userspace_vblank(mode_context->context->drm_dev,
+				IGD_PORT_TYPE_SDVOB);
+		}
+	}
+
+	/* Call any registered/enabled callbacks for this interrupt: */
+	cb = &interrupt_callbacks_plb[2];
+	if (port2_interrupt && cb->callback &&
+		(vblank_interrupt_state & VBINT_ANSWER(VBINT_CB, VBINT_PORT2))) {
+		/* Clear the state to indicate the vblank has occured prior to
+		 * invoking the callback.
+		 */
+		vblank_interrupt_state &= ~VBINT_ANSWER(VBINT_CB, VBINT_PORT2);
+		cb->callback(cb->priv);
+	}
+	cb = &interrupt_callbacks_plb[4];
+	if (port4_interrupt && cb->callback &&
+		(vblank_interrupt_state & VBINT_ANSWER(VBINT_CB, VBINT_PORT4))) {
+		/* Clear the state to indicate the vblank has occured prior to
+		 * invoking the callback.
+		 */
+		vblank_interrupt_state &= ~VBINT_ANSWER(VBINT_CB, VBINT_PORT4);
+		cb->callback(cb->priv);
+	}
+
+	/* Clear interrupt status registers: */
+	if (port2_interrupt || port4_interrupt) {
+		/* Clear the corresponding bits in the PIPE{A|B}_STAT register(s): */
+		if (port2_interrupt) {
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS), EMGD_MMIO(mmio) + PIPEA_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+		}
+		if (port4_interrupt) {
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS), EMGD_MMIO(mmio) + PIPEB_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+		}
+
+		/* Clear the corresponding bits in the IIR register: */
+		EMGD_WRITE32((port2_interrupt | port4_interrupt), EMGD_MMIO(mmio)+IIR);
+
+
+		EMGD_DEBUG("EXIT--IRQ_HANDLED");
+		return IRQ_HANDLED;
+	} else {
+		EMGD_DEBUG("EXIT--IRQ_NONE");
+		return IRQ_NONE;
+	}
+}
+
+/*!
+ * Implementation of "protected" function (i.e. for use within the mode
+ * module) to request VBlank interrupts for a particular purpose & port.
+ * Based upon the outstanding requests, this function decides whether to
+ * touch registers, register the interrupt handler, etc.
+ *
+ * @param request_for (IN).  A bit that identifies a who and what (e.g.
+ *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+ *
+ * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+ * registers.
+ *
+ * @return Zero for success, non-zero for failure.
+ */
+int request_vblanks_plb(unsigned long request_for, unsigned char *mmio)
+{
+	unsigned long lock_flags;
+	unsigned long tmp;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: request_for=0x%lx, mmio=0x%p", request_for, mmio);
+
+	/* Perform error checking--ensure a valid bit was set: */
+	if (!(request_for & (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4))) {
+		EMGD_ERROR_EXIT("Invalid parameter, request_for=0x%lx",
+			request_for);
+		return -1;
+	}
+
+	/* If we're just enabling interrupts, register the interrupt handler: */
+	if (!VBLANK_INTERRUPTS_ENABLED) {
+		struct drm_device *drm_device = mode_context->context->drm_dev;
+
+		EMGD_DEBUG("Registering interrupt_handler_plb()");
+		if (request_irq(drm_device->pdev->irq, interrupt_handler_plb,
+			IRQF_SHARED, EMGD_DRIVER_NAME, mmio)) {
+			EMGD_ERROR_EXIT("Failed to register interrupt_handler_plb()");
+			return -1;
+		} else {
+			EMGD_DEBUG("Successfully registered interrupt_handler_plb()");
+		}
+	}
+
+	/* Lock here to stop the interrupt handler until after changing bits: */
+	spin_lock_irqsave(&vblank_lock_plb, lock_flags);
+
+	/* Enable interrupts for the requested purpose/port, actually touching the
+	 * hardware registers if newly enabling interrupts for the given port/pipe:
+	 */
+	if (request_for & VBLANK_INT4_PORT2) {
+		if (!VBLANK_INTERRUPTS_ENABLED4_PORT2) {
+			/* 1. Change Pipe Display Status Register for this pipe: set the
+			 *    Vertical Blank Interrupt Enable bit & clear (by setting) the
+			 *    Vertical Blank Interrupt Status bit:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+			/* Clear bits that are written by a 1, so we don't clear them: */
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS_EN | VBLANK_STS),
+				EMGD_MMIO(mmio) + PIPEA_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+
+			/* 2. Just in case, clear (by setting) the Interrupt Identity
+			 *    Register bit for this pipe:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+			EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IIR);
+
+			/* 3. Clear the Interrupt Mask Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+			EMGD_WRITE32((tmp & (~BIT7)), EMGD_MMIO(mmio) + IMR);
+
+			/* 4. Set the Interrupt Enable Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+			EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IER);
+		}
+		vblank_interrupt_state |= request_for;
+		vblank_interrupt_ref_cnt_port2++;
+	} else /* if (request_for & VBLANK_INT4_PORT4) */ {
+		if (!VBLANK_INTERRUPTS_ENABLED4_PORT4) {
+			/* 1. Change Pipe Display Status Register for this pipe: set the
+			 *    Vertical Blank Interrupt Enable bit & clear (by setting) the
+			 *    Vertical Blank Interrupt Status bit:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+			/* Clear bits that are written by a 1, so we don't clear them: */
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS_EN | VBLANK_STS),
+				EMGD_MMIO(mmio) + PIPEB_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+
+			/* 2. Just in case, clear (by setting) the Interrupt Identity
+			 *    Register bit for this pipe:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+			EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IIR);
+
+			/* 3. Clear the Interrupt Mask Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+			EMGD_WRITE32((tmp & (~BIT5)), EMGD_MMIO(mmio) + IMR);
+
+			/* 4. Set the Interrupt Enable Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+			EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IER);
+		}
+		vblank_interrupt_state |= request_for;
+		vblank_interrupt_ref_cnt_port4++;
+	}
+
+	/* Unlock to allow the interrupt handler to proceed: */
+	spin_unlock_irqrestore(&vblank_lock_plb, lock_flags);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*!
+ * Implementation of "protected" function (i.e. for use within the mode
+ * module) to end a previous request VBlank interrupts for a particular
+ * purpose & port.  Based upon the outstanding requests, this function
+ * decides whether to touch registers, unregister the interrupt handler,
+ * etc.
+ *
+ * @param request_for (IN).  A bit that identifies a who and what (e.g.
+ *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+ *
+ * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+ * registers.
+ *
+ * @return Zero for success, non-zero for failure.
+ */
+int end_request_plb(unsigned long request_for, unsigned char *mmio)
+{
+	unsigned long lock_flags;
+	unsigned long tmp;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: request_for=0x%lx, mmio=0x%p", request_for, mmio);
+
+	/* Perform error checking--ensure a valid bit was set: */
+	if (!(request_for & (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4)) ||
+		!(vblank_interrupt_state & request_for)) {
+		EMGD_ERROR_EXIT("Invalid parameter, request_for=0x%lx", request_for);
+		return -1;
+	}
+
+	/* Lock here to stop the interrupt handler until after changing bits: */
+	spin_lock_irqsave(&vblank_lock_plb, lock_flags);
+
+	/* Disable interrupts for the requested purpose/port, actually touching the
+	 * hardware registers no software wants interrupts for the given port/pipe:
+	 */
+	if (request_for & VBLANK_INT4_PORT2) {
+		/* Decrement reference count */
+		vblank_interrupt_ref_cnt_port2--;
+		if (0 > vblank_interrupt_ref_cnt_port2) {
+			EMGD_DEBUG("WARNING:  Disabled vblank INT too many times.");
+			vblank_interrupt_ref_cnt_port2 = 0;
+		}
+
+		if (0 == vblank_interrupt_ref_cnt_port2) {
+			/* Turn off both the request and the answer bits: */
+			tmp = request_for & VBLANK_INT4_PORT2;
+			vblank_interrupt_state &= ~(tmp | VBINT_ANSWER4_REQUEST(tmp));
+			if (!VBLANK_INTERRUPTS_ENABLED4_PORT2) {
+				/* 1. Change Pipe Display Status Register for this pipe: clear
+				 *    the Vertical Blank Interrupt Enable bit & clear (by
+				 *    setting) the Vertical Blank Interrupt Status bit:
+				 */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+				 /* Clear bits that are written by a 1, so don't clear them: */
+				 tmp = tmp & (~PIPESTAT_STS_BITS);
+				 EMGD_WRITE32(((tmp & (~VBLANK_STS_EN)) | VBLANK_STS),
+				 	EMGD_MMIO(mmio) + PIPEA_STAT);
+				 EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+
+				 /* 2. Clear the Interrupt Enable Register bit for this pipe: */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+				 EMGD_WRITE32((tmp & (~BIT7)), EMGD_MMIO(mmio) + IER);
+
+				 /* 3. Set the Interrupt Mask Register bit for this pipe: */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+				 EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IMR);
+
+				 /* 4. Just in case, clear (by setting) the Interrupt Identity
+				  *    Register bit for this pipe:
+				  */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+				 EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IIR);
+			}
+		}
+	}
+
+	if (request_for & VBLANK_INT4_PORT4) {
+		/* Decrement reference count */
+		vblank_interrupt_ref_cnt_port4--;
+		if (0 > vblank_interrupt_ref_cnt_port4) {
+			EMGD_DEBUG("WARNING:  Disabled vblank INT too many times.");
+			vblank_interrupt_ref_cnt_port4 = 0;
+		}
+
+		if (0 == vblank_interrupt_ref_cnt_port4) {
+			/* Turn off both the request and the answer bits: */
+			tmp = request_for & VBLANK_INT4_PORT4;
+			vblank_interrupt_state &= ~(tmp | VBINT_ANSWER4_REQUEST(tmp));
+			if (!VBLANK_INTERRUPTS_ENABLED4_PORT4) {
+				/* 1. Change Pipe Display Status Register for this pipe: clear
+				 *    the Vertical Blank Interrupt Enable bit & clear (by
+				 *    setting the Vertical Blank Interrupt Status bit:
+				 */
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+				/* Clear bits that are written by a 1, so don't clear them: */
+				tmp = tmp & (~PIPESTAT_STS_BITS);
+				EMGD_WRITE32(((tmp & (~VBLANK_STS_EN)) | VBLANK_STS),
+					EMGD_MMIO(mmio) + PIPEB_STAT);
+				EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+
+				/* 2. Clear the Interrupt Enable Register bit for this pipe: */
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+				EMGD_WRITE32((tmp & (~BIT5)), EMGD_MMIO(mmio) + IER);
+
+				/* 3. Set the Interrupt Mask Register bit for this pipe: */
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+				EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IMR);
+
+				/* 4. Just in case, clear (by setting) the Interrupt Identity
+				*    Register bit for this pipe:
+				*/
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+				EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IIR);
+			}
+		}
+	}
+
+	/* Unlock to allow the interrupt handler to proceed: */
+	spin_unlock_irqrestore(&vblank_lock_plb, lock_flags);
+
+	/* If we've completely disabled all causes for interrupts, unregister the
+	 * interrupt handler:
+	 */
+	if (!VBLANK_INTERRUPTS_ENABLED) {
+		struct drm_device *drm_device = mode_context->context->drm_dev;
+
+		EMGD_DEBUG("Unregistering interrupt_handler_plb()");
+		free_irq(drm_device->pdev->irq, mmio);
+		EMGD_DEBUG("Successfully unregistered interrupt_handler_plb()");
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Implementation of "protected" function (i.e. for use within the mode
+ * module) to check whether a requested VBlank interrupt occured.
+ *
+ * @param request_for (IN).  A bit that identifies a who and what (e.g.
+ *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+ *
+ * @return Non-zero if the requested VBlank occured, zero if not.
+ */
+int vblank_occured_plb(unsigned long request_for)
+{
+	return (vblank_interrupt_state & VBINT_ANSWER4_REQUEST(request_for));
+}
+
+/*!
+ *  Registers a VBlank interrupt callback function (and its parameter) to
+ *  call when a VBlank interrupt occurs for a given port.
+ *
+ * @param callback (IN).  A callback (function pointer) to a non-HAL
+ * function that processes a VBlank interrupt.
+ *
+ * @param priv (IN).  An opaque pointer to a non-HAL data structure.
+ *  This pointer is passed as a parameter of the callback function.
+ *
+ * @param port_number (IN).  The EMGD port number to register a VBlank
+ *  interrupt callback for.
+ *
+ * @return A handle that uniquely identifies this callback/port
+ *  combination, or NULL if a failure.
+ */
+emgd_vblank_callback_h register_vblank_callback_plb(
+	emgd_process_vblank_interrupt_t callback,
+	void *priv,
+	unsigned long port_number)
+{
+	EMGD_TRACE_ENTER;
+
+	if (!callback || !priv ||
+		!((port_number == 2) || (port_number == 4))) {
+		EMGD_ERROR_EXIT("Invalid parameter amongst the following:\n"
+			"  process_interrupt=0x%p, priv=0x%p, port_number=%lu",
+			callback, priv, port_number);
+		return NULL;
+	}
+
+	interrupt_callbacks_plb[port_number].callback = callback;
+	interrupt_callbacks_plb[port_number].priv = priv;
+
+	EMGD_TRACE_EXIT;
+	return &interrupt_callbacks_plb[port_number];
+}
+
+/*!
+ *  Unregisters a previously-registered VBlank interrupt callback function
+ *  for a given port.
+ *
+ * @param callback_h (IN).  The handle that uniquely identifies the VBlank
+ *  interrupt callback to unregister.
+ */
+void unregister_vblank_callback_plb(emgd_vblank_callback_h callback_h)
+{
+	emgd_vblank_callback_t *cb = (emgd_vblank_callback_t *) callback_h;
+
+	EMGD_TRACE_ENTER;
+
+	cb->callback = NULL;
+	cb->priv = NULL;
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ *  Enable delivering VBlank interrupts to the callback function for the
+ *  registered callback/port combination.
+ *
+ * @param callback_h (IN).  The handle that uniquely identifies which
+ *  VBlank interrupt callback/port combination to enable.
+ *
+ * @return Zero if successful, non-zero if a failure.
+ */
+int enable_vblank_callback_plb(emgd_vblank_callback_h callback_h)
+{
+	emgd_vblank_callback_t *cb = (emgd_vblank_callback_t *) callback_h;
+	unsigned long enable_for = VBINT_REQUEST(VBINT_CB,
+		(cb->port_number == 2) ? VBINT_PORT2 : VBINT_PORT4);
+	unsigned char* mmio =
+		EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameter: callback_h=0x%p", callback_h);
+
+	ret = request_vblanks_plb(enable_for, mmio);
+
+	EMGD_DEBUG("Return %d", ret);
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ *  Disable delivering VBlank interrupts to the callback function for the
+ *  registered function/port combination.
+ *
+ * @param callback_h (IN).  The handle that uniquely identifies which
+ *  VBlank interrupt callback/port combination to disable.
+ */
+void disable_vblank_callback_plb(emgd_vblank_callback_h callback_h)
+{
+	unsigned long lock_flags;
+	unsigned long enable_for;
+	unsigned char* mmio =
+		EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameter: callback_h=0x%p", callback_h);
+
+	if (callback_h == ALL_PORT_CALLBACKS) {
+		/* Need to do some push-ups in order to get interrupts disabled: */
+		spin_lock_irqsave(&vblank_lock_plb, lock_flags);
+		enable_for = (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4);
+		if (!VBLANK_INTERRUPTS_ENABLED) {
+			/* Nothing has enabled interrupts, so there's no interrupt handler
+			 * to unregister.  Therefore, use a special value to prevent that:
+			 */
+			vblank_interrupt_state = (VBLANK_INT4_PORT2 |
+				VBLANK_INT4_PORT4 | VBLANK_DISABLE_HW_ONLY);
+		} else {
+			vblank_interrupt_state = (VBLANK_INT4_PORT2 |
+				VBLANK_INT4_PORT4);
+		}
+		spin_unlock_irqrestore(&vblank_lock_plb, lock_flags);
+
+		end_request_plb(enable_for, mmio);
+
+		spin_lock_irqsave(&vblank_lock_plb, lock_flags);
+		vblank_interrupt_state = 0;
+		spin_unlock_irqrestore(&vblank_lock_plb, lock_flags);
+	} else {
+		emgd_vblank_callback_t *cb =
+			(emgd_vblank_callback_t *) callback_h;
+		enable_for = VBINT_REQUEST(VBINT_CB,
+			(cb->port_number == 2) ? VBINT_PORT2 : VBINT_PORT4);
+
+		end_request_plb(enable_for, mmio);
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+mode_full_dispatch_t mode_full_dispatch_plb = {
+	igd_alter_cursor_pos_plb,
+	igd_set_palette_entries_plb,
+	igd_wait_vsync_plb,
+	igd_query_in_vblank_plb,
+	igd_get_scanline_plb,
+	set_display_base_plb,
+	program_cursor_plb,
+	set_color_correct_plb,
+	igd_get_surface_plb,
+	igd_set_surface_plb,
+	igd_query_event_plb,
+	set_flip_pending_plb,
+	check_flip_pending_plb,
+	get_plane_info_plb,
+	get_pipe_info_plb,
+	get_port_info_plb,
+	register_vblank_callback_plb,
+	unregister_vblank_callback_plb,
+	enable_vblank_callback_plb,
+	disable_vblank_callback_plb,
+	request_vblanks_plb,
+	end_request_plb,
+	vblank_occured_plb,
+};
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.h b/drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.h
new file mode 100644
index 0000000..0c42d29
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/plb/mode_plb.h
@@ -0,0 +1,47 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_plb.h
+ * $Revision: 1.2 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+
+#ifdef CONFIG_PLB
+
+typedef struct _mode_data_plb {
+	unsigned long plane_a_preserve;
+	unsigned long plane_b_c_preserve;
+	unsigned long pipe_preserve;
+	unsigned long port_preserve;
+	unsigned long fw_blc1;
+	unsigned long fw_blc2;
+	unsigned long fw_blc3;
+	unsigned long fw_self;
+	unsigned long mem_mode;
+	unsigned long dsp_arb;
+}mode_data_plb_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/tnc/clocks_tnc.c b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/clocks_tnc.c
new file mode 100644
index 0000000..8e58723
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/clocks_tnc.c
@@ -0,0 +1,1184 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: clocks_tnc.c
+ * $Revision: 1.18 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Clock programming for Atom E6xx
+ *  program clocks used for LVDS port based on Lincroft
+ *  program clocks used for SDVO port based on Atom E6xx overlay
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <io.h>
+#include <sched.h>
+
+#include <igd_init.h>
+#include <igd_mode.h>
+
+#include <context.h>
+#include <dsp.h>
+#include <utils.h>
+#include <mode.h>
+#include <pi.h>
+#include <intelpci.h>
+#include "drm_emgd_private.h"
+
+#include <tnc/regs.h>
+
+#include <tnc/igd_tnc_wa.h> /* needed for vbios for register defines */
+
+#ifdef CONFIG_DEBUG
+#define FLAG(a) a
+/* Debug flag to turn off CDVO reset sequence */
+static int flag_enable_cdvo_reset = 1;
+#else
+/* Turn all workaround for release driver */
+#define FLAG(a) 1
+#endif
+
+extern unsigned long get_port_type(int crtc_id);
+static void wait_dpll(void);
+
+int program_cdvo_tnc(void);
+static int kms_program_cdvo(emgd_crtc_t *emgd_crtc);
+
+static cdvo_regs_t cdvo_reset[] = {
+           /*turn off the SDVO port*/
+           {0x61140, 0},
+           /*turn off the stall register*/
+           {0x6102C, 0},
+           /*disable SDVO*/
+           {0x61170, 0},
+           /*programmable cdvo stall*/
+           {0x6102c, 0xf},
+           {0x7000, 0x40},
+           /*reset*/
+           {0x7000, 0x51},
+           {0x7000, 0x50},
+           /*High*/
+           {0x7014, 0x00004800},
+           /*strobe data tuning default*/
+           {0x700c, 0x000BB4FF},
+           /*enable sdvo*/
+           {0x61170, 0x20022160},
+           {0x7010, 0x06000200},
+           /* -- Begin Sequence of 3 pixel signal --*/
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*reset*/
+           {0x7000, 0x51},
+           {0x7000, 0x50},
+           /*High*/
+           {0x7014, 0x00004800},
+           /*strobe data tuning default*/
+           {0x700c, 0x000BB4FF},
+           /*enable sdvo*/
+           {0x61170, 0x20022160},
+           {0x7010, 0x06000200},
+           /* -- Begin Sequence of 3 pixel signal --*/
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Low*/
+           {0x700c, 0x00008000},
+           {0x700c, 0x0008B400},
+           /*High*/
+           {0x700c, 0x0008B4FF},
+           /*Reset*/
+           {0x7000, 0x51},
+           {0x7000, 0x50},
+           {0x7010, 0x02000200},    /*gtl gtl*/
+           {0x7014, 0x00004000},    /*ODT*/
+           {0x61170, 0x20022160},    /*enable SDVO*/
+           {0x7010, 0x02000200},    /*gtl gtl*/
+           {0x7014, 0x00000800},
+           {0x7014, 0x00004800},    /*rcomp*/
+           {0x7014, 0x00000000},
+           {0x7000, 0x40},            /*enable cdvo*/
+           {0x7000, 0x50},            /*enable cdvo*/
+           {0x7014, 0x00004000},    /*ODT*/
+           {0x61170, 0x20022160},    /*enable SDVO*/
+           {0,0}
+};
+
+
+
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/* Enable this if calculated values are wrong and required fixed table */
+#define CONFIG_FIXED_TABLE  0
+
+#ifdef CONFIG_TNC
+
+extern int program_clock_sdvo_tnc(igd_display_context_t *display,
+	igd_clock_t *clock, unsigned long dclk);
+
+/*===========================================================================
+; File Global Data
+;--------------------------------------------------------------------------*/
+#if CONFIG_FIXED_TABLE
+typedef struct _fixed_clock {
+	unsigned long dclk;
+	unsigned long n;
+	unsigned long m;
+	unsigned long p1;
+} fixed_clock_t;
+
+/* set mmio register required values */
+static fixed_clock_t lvds_fixed_clock_table[] =  {
+	/* Clock       N     M     P1 */
+	{ 65000,      0x01, 0x00, 0x00 },
+	{ 0xffffffff, 0x01, 0x00, 0x00}
+};
+
+/* set mmio register required values */
+static fixed_clock_t sdvo_fixed_clock_table[] =  {
+	/* Clock       N     M    P1 */
+	{ 65000,      0x00, 0x00, 0x00 },
+	{ 0xffffffff, 0x00, 0x00, 0x00}
+};
+#endif
+
+#define LVDS_M_MIN 10
+
+/* This table is also used by mode_tnc.c */
+const unsigned long lvds_m_converts[] = {
+	0x2B, 0x15, 0x2A, 0x35, 0x1A, 0x0D, 0x26, 0x33, 0x19, 0x2C,
+	0x36, 0x3B, 0x1D, 0x2E, 0x37, 0x1B, 0x2D, 0x16, 0x0B, 0x25,
+	0x12, 0x09, 0x24, 0x32, 0x39, 0x1c,
+};
+
+const unsigned long LVDS_M_CONVERTS_LEN=26;   /* Length of the array above*/
+
+#define TARGET_ERROR 46
+
+typedef const struct _tnc_limits {
+	unsigned long ref_freq;
+	unsigned long min_m;
+	unsigned long max_m;
+
+	unsigned long min_n;
+	unsigned long max_n;
+
+	unsigned long min_p1;
+	unsigned long max_p1;
+
+	unsigned long min_p2;
+	unsigned long max_p2;
+
+	unsigned long min_vco;
+	unsigned long max_vco;
+} tnc_limits_t;
+
+/* m, n, p value limits:
+ * source: http://moss.amr.ith.intel.com/sites/LCD/LNC/HAS/Secured
+ * %20Documents/LNC%20HAS%20work%20area/Lincroft%20DPLL_1.2_ww31_08.docx
+ *
+ * Note:
+ * VCO range for 100L is not given: so put a range big enough to go through
+ * calculations for the for loop.
+ */
+static tnc_limits_t tnc_lvds_limits[] =
+{
+	/* reffreq  m      n     p1     p2          vco */
+	{ 200000, 10, 17, 1, 1, 2, 8, 14, 14, 2000000, 3400000},  /* SKU 100 */
+	{ 100000, 20, 34, 1, 1, 2, 8, 14, 14, 1000000, 3400000},  /* SKU 100L */
+	{ 166000, 12, 20, 1, 1, 2, 7, 14, 14, 2000000, 3333333},  /* SKU 83 */
+};
+
+static tnc_limits_t tnc_sdvo_limits[] =
+{
+	/* reffreq  m      n     p1     p2          vco */
+	{  96000, 80,137, 3, 7, 1, 2, 10, 10, 1400000, 2800000},  /* SDVO */
+};
+
+/*!
+ *
+ * @param dclk
+ * @param ref_freq
+ * @param limits
+ * @param m
+ * @param n
+ * @param p
+ * @param target_error
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int calculate_clock(unsigned long dclk,
+	unsigned long ref_freq,
+	tnc_limits_t *l,
+	unsigned long *m,
+	unsigned long *n,
+	unsigned long *p1,
+	unsigned long target_error,
+	unsigned long *actual_dclk,
+	unsigned long port_type,
+	unsigned long pd_type)
+{
+	unsigned long  pdiv;
+	unsigned long  target_vco, actual_freq;
+	long freq_error, min_error;
+	unsigned long dclk_10000;
+
+	unsigned long current_m, current_n, current_p1;
+
+	EMGD_TRACE_ENTER;
+
+	min_error = 100000;
+
+	*m = 0;
+	*n = 0;
+	*p1 = 0;
+
+	/* dclk * 10000, so it does not have to be calculated within the
+	 * loop */
+	dclk_10000 = dclk * 10000;
+
+	for(current_m = l->min_m; current_m <= l->max_m; current_m++) {
+		for(current_n = l->min_n; current_n <= l->max_n; current_n++) {
+			for(current_p1 = l->min_p1; current_p1 <= l->max_p1; current_p1++) {
+
+				/* For both LVDS/SDVO ports min_p2 and max_p2 are same,
+				 * so assign use either min/max p2 */
+				pdiv = current_p1 * l->min_p2;
+				target_vco = dclk * pdiv;
+
+				if (target_vco > l->max_vco) {
+					/* target_vco continues to increase, so start with
+					 * next current_n */
+					break;
+				}
+
+				if (target_vco >= l->min_vco) {
+					/* Frequency calculations for SDVO and LVDS are different
+					 * SDVO Dotclock_frequency = (Reference Frequency * (M+2)) / (N*(P1*P2))
+					 * LVDS DotClk_Frequency = (ReferenceFrequency * M)/ (P1* P2)
+					 * need to find a way to differentiate SDVO and LVDS
+					 * Works for now but signal generated will be different */
+					actual_freq = (ref_freq * current_m) / (current_n*pdiv);
+					freq_error = 10000 - (dclk_10000 / actual_freq);
+
+					if (freq_error < -min_error) {
+						/* freq_error continues to decrease once this
+						 * point is reached, so start with next
+						 * current_n */
+						break;
+					}
+
+					if (freq_error < 0) {
+						freq_error = -freq_error;
+					}
+					if (freq_error < min_error)  {
+						*n = current_n;
+						*m = current_m;
+						*p1 = current_p1;
+						min_error = freq_error;
+						*actual_dclk = actual_freq;
+					}
+				}
+			}
+		}
+		if (min_error == 0) {
+			break;
+		}
+	}
+
+	if (pd_type == PD_DISPLAY_TVOUT) {
+		EMGD_TRACE_EXIT;
+		return 0;
+	}
+	/*
+	 * No clock found that meets error requirement
+	 */
+	if (min_error > (long)target_error) {
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	EMGD_DEBUG("dclk = (ref*m)/(n*p1*p2) => %lu = (%lu*%lu)/(%lu*%lu*%lu) = %lu",
+		dclk, ref_freq, *m, *n, *p1, l->min_p2,
+		(ref_freq*(*m))/((*n)*(*p1)*l->min_p2));
+	EMGD_DEBUG("min_error:%ld", min_error);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param dclk
+ * @param ref_freq
+ * @param limits
+ * @param m
+ * @param n
+ * @param p
+ * @param target_error
+ * @param port_type
+ * @param pd_type
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int get_clock(unsigned long dclk,
+	unsigned long ref_freq,
+	tnc_limits_t *l,
+	unsigned long *m,
+	unsigned long *n,
+	unsigned long *p1,
+	unsigned long target_error,
+	unsigned long port_type,
+	unsigned long *actual_dclk,
+	unsigned long pd_type)
+{
+
+#if CONFIG_FIXED_TABLE
+	fixed_clock_t *fixed;
+	EMGD_TRACE_ENTER;
+    /* Enable this if calculated values are wrong and required fixed table */
+	if (port_type == IGD_PORT_LVDS) {
+		fixed = lvds_fixed_clock_table;
+	} else {
+		fixed = sdvo_fixed_clock_table;
+	}
+
+	/* First check for a fixed clock from the table. These are ones that
+	 * can't be calculated correctly. */
+	while (fixed->dclk != 0xffffffff) {
+		if (fixed->dclk == dclk) {
+			EMGD_DEBUG("Using Fixed Clock From table for clock %ld", dclk);
+			*m = fixed->m;
+			*n = fixed->n;
+			*p1 = fixed->p1;
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+		fixed++;
+	}
+#endif
+
+	EMGD_TRACE_ENTER;
+	/* No fixed clock found so calculate one. */
+	EMGD_DEBUG("Calculating dynamic clock for clock %ld", dclk);
+
+	if (calculate_clock(dclk, ref_freq, l, m, n, p1, target_error, actual_dclk, port_type, pd_type)) {
+		/* No usable clock.  Cannot use 640x480@60 as default, because
+		 * there are several vcos and several reference clocks. */
+		EMGD_ERROR("Could not calculate clock %ld, returning default.", dclk);
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	if (pd_type == PD_DISPLAY_TVOUT) {
+		*p1 = 2;
+		*m = 100;
+		*n = 5;
+	}
+
+	/* Translate returned values to m,n,p1 register values */
+	/* No change required for *n value */
+	if (port_type == IGD_PORT_SDVO) {
+		*p1 = (1L << (*p1 - 1));
+		*m -= 2;
+		*n = (1L << (*n -1));
+	} else {
+		*p1 = (1L << (*p1 - 2));
+		*m = lvds_m_converts[*m - LVDS_M_MIN];
+	}
+
+	EMGD_DEBUG("reg m=%lu n=%lu p1=%lu p2=%lu", *m, *n, *p1, l->min_p2);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param emgd_crtc
+ * @param clock
+ * @param dclk
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int kms_program_clock_lvds_tnc(emgd_crtc_t *emgd_crtc,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	struct drm_device  *dev = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	igd_context_t      *context = NULL;
+	igd_display_port_t *port = NULL;
+	struct drm_encoder *encoder = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	unsigned long pt;
+	int ret;
+	unsigned long m, n, p1;
+	unsigned long control;
+	unsigned long ref_freq;
+	tnc_limits_t *l = NULL;
+	unsigned long count;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	pt = get_port_type(emgd_crtc->crtc_id);
+
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			break;
+		}
+	}
+
+
+	/* LNC ref_freq is determined by SKU, convert to KHz */
+#if 0
+	ref_freq = display->context->device_context.gfx_freq * 1000L;
+#else
+	ref_freq = context->device_context.core_freq;
+	ref_freq = ref_freq * 1000;
+#endif
+
+#if 0
+	/* Find m,n,p,vco limits */
+	if (ref_freq == 200000) {
+		l = &tnc_lvds_limits[0];
+	} else if (ref_freq == 100000) {
+		l = &tnc_lvds_limits[1];
+	} else if (ref_freq == 166000) {
+		l = &tnc_lvds_limits[2];
+	}
+#endif
+
+	for(count=0; count<3; count++){
+		if (tnc_lvds_limits[count].ref_freq == ref_freq){
+			l = &tnc_lvds_limits[count];
+			break;
+		}
+	}
+
+/* PO Debug */
+#if 0
+	/* WRITE_PORT80(0xED); */
+
+	if(ref_freq == 166000){
+	WRITE_PORT80(0xEF);
+	}
+
+	if (!l){
+		/* FATAL ERROR */
+		DEAD_LOOP(0xDD);
+	}
+#endif
+
+	/* Per UMG, there is no defined target_error for LVDS, it supposed to
+	 * work for all expected dclks. */
+#if 1
+	if (port) {
+		ret = get_clock(dclk, ref_freq, l, &m, &n, &p1, dclk /* target_error */,
+			port->port_type, &clock->actual_dclk, port->pd_driver->type);
+	} else {
+		ret = 1;
+	}
+	if (ret) {
+		EMGD_ERROR_EXIT("Clock %ld could not be programmed", dclk);
+		/* DEAD_LOOP(0xFF); */
+		return ret;
+	}
+#else
+	/* Hard code the values for now */
+	m = 0x2D;  // ITP uses 0x2E, should change
+	p1 = 2;
+
+#endif
+
+#if 0
+	/* If clocks are already running at required clocks, just return */
+	if (PIPE(display)->dclk == dclk) {
+		return 0;
+	}
+#endif
+
+	/* Disable DPLL */
+#if 0
+	control = BIT28 | (1<<clock->p_shift); //Why are we shifting 1 to P1, GMA carry over???
+#else
+	control = BIT28;
+#endif
+	EMGD_WRITE32(control, context->device_context.virt_mmadr + clock->dpll_control);
+	EMGD_DEBUG("lvds: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* Program M */
+	EMGD_WRITE32((m<<8), context->device_context.virt_mmadr + clock->mnp);
+
+	EMGD_WRITE32((m<<8), context->device_context.virt_mmadr + 0xF044); //BUGBUG
+
+	EMGD_DEBUG("lvds: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->mnp, (m<<8));
+
+	/* Enable DPLL */
+	control = (BIT31 | BIT28 | BIT27) | (p1<<clock->p_shift);
+
+/* PO Debug..*/
+#if 0
+	/* set VCO select */
+	if (ref_freq == 166000) {
+		control |= BIT16;
+	}
+#endif
+
+
+	EMGD_WRITE32(control, context->device_context.virt_mmadr + clock->dpll_control);
+	EMGD_DEBUG("lvds: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* Wait 150us for the DPLL to stabilize */
+	OS_SLEEP(150);
+	pipe->dclk = dclk;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+/*!
+ *
+ * @param display
+ * @param clock
+ * @param dclk
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int program_clock_lvds_tnc(igd_display_context_t *display,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	int ret;
+	unsigned long m, n, p1;
+	unsigned long control;
+	unsigned long ref_freq;
+	igd_display_port_t *port;
+	tnc_limits_t *l = NULL;
+	unsigned long count;
+
+	EMGD_TRACE_ENTER;
+
+	port = PORT_OWNER(display);
+
+	/* LNC ref_freq is determined by SKU, convert to KHz */
+#if 0
+	ref_freq = display->context->device_context.gfx_freq * 1000L;
+#else
+	ref_freq = display->context->device_context.core_freq;
+	ref_freq = ref_freq * 1000;
+#endif
+
+#if 0
+	/* Find m,n,p,vco limits */
+	if (ref_freq == 200000) {
+		l = &tnc_lvds_limits[0];
+	} else if (ref_freq == 100000) {
+		l = &tnc_lvds_limits[1];
+	} else if (ref_freq == 166000) {
+		l = &tnc_lvds_limits[2];
+	}
+#endif
+
+	for(count=0; count<3; count++){
+		if (tnc_lvds_limits[count].ref_freq == ref_freq){
+			l = &tnc_lvds_limits[count];
+			break;
+		}
+	}
+
+/* PO Debug */
+#if 0
+	/* WRITE_PORT80(0xED); */
+
+	if(ref_freq == 166000){
+	WRITE_PORT80(0xEF);
+	}
+
+	if (!l){
+		/* FATAL ERROR */
+		DEAD_LOOP(0xDD);
+	}
+#endif
+
+	/* Per UMG, there is no defined target_error for LVDS, it supposed to
+	 * work for all expected dclks. */
+#if 1
+	ret = get_clock(dclk, ref_freq, l, &m, &n, &p1, dclk /* target_error */,
+		port->port_type, &clock->actual_dclk, port->pd_driver->type);
+	if (ret) {
+		EMGD_ERROR_EXIT("Clock %ld could not be programmed", dclk);
+		/* DEAD_LOOP(0xFF); */
+		return ret;
+	}
+#else
+	/* Hard code the values for now */
+	m = 0x2D;  // ITP uses 0x2E, should change
+	p1 = 2;
+
+#endif
+
+#if 0
+	/* If clocks are already running at required clocks, just return */
+	if (PIPE(display)->dclk == dclk) {
+		return 0;
+	}
+#endif
+
+	/* Disable DPLL */
+#if 0
+	control = BIT28 | (1<<clock->p_shift); //Why are we shifting 1 to P1, GMA carry over???
+#else
+	control = BIT28;
+#endif
+	WRITE_MMIO_REG(display, clock->dpll_control, control);
+	EMGD_DEBUG("lvds: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* Program M */
+	WRITE_MMIO_REG(display, clock->mnp, (m<<8));
+
+	WRITE_MMIO_REG(display, 0xF044, (m<<8)); //BUGBUG
+
+	EMGD_DEBUG("lvds: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->mnp, (m<<8));
+
+	/* Enable DPLL */
+	control = (BIT31 | BIT28 | BIT27) | (p1<<clock->p_shift);
+
+/* PO Debug..*/
+#if 0
+	/* set VCO select */
+	if (ref_freq == 166000) {
+		control |= BIT16;
+	}
+#endif
+
+
+	WRITE_MMIO_REG(display, clock->dpll_control, control);
+	EMGD_DEBUG("lvds: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* Wait 150us for the DPLL to stabilize */
+	OS_SLEEP(150);
+	PIPE(display)->dclk = dclk;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param emgd_crtc
+ * @param clock
+ * @param dclk
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int kms_program_clock_sdvo_tnc(emgd_crtc_t *emgd_crtc,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	struct drm_device  *dev          = NULL;
+	igd_display_pipe_t *pipe         = NULL;
+	igd_context_t      *context      = NULL;
+	igd_display_port_t *port         = NULL;
+	struct drm_encoder *encoder      = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	unsigned long pt;
+	int ret;
+	unsigned long m, n, p1;
+	unsigned long control;
+	unsigned long ref_freq;
+	unsigned long port_mult, vga_mult;
+	unsigned long target_error, actual_dclk;
+	tnc_limits_t *l;
+
+	EMGD_DEBUG("Enter program_clock");
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	pt = get_port_type(emgd_crtc->crtc_id);
+
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			break;
+		}
+	}
+	if (!port) {
+		EMGD_ERROR_EXIT("No port");
+		return -1;
+	}
+
+	/* FIXME: No info available in EAS and waiting for info. */
+	if (dclk > 100000) {          /* 100-200 MHz */
+		port_mult = 1;
+	} else if (dclk > 50000) {    /* 50-100 Mhz */
+		port_mult = 2;
+	} else {                      /* 25-50 Mhz */
+		port_mult = 4;
+	}
+
+	dclk *= port_mult;
+
+	l = &tnc_sdvo_limits[0];
+	ref_freq = l->ref_freq;
+
+	vga_mult = READ_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control) & 0x3;
+
+	target_error = TARGET_ERROR;
+
+	/* For external clock sources always use ref_clock == dclk */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		ref_freq = dclk;
+		/* When clock source sdvo device, allowed error is 0. */
+		target_error = 0;
+	}
+
+	if (port) {
+		ret = get_clock(dclk, ref_freq, l, &m, &n, &p1, target_error,
+			IGD_PORT_SDVO, &actual_dclk, port->pd_driver->type);
+	} else {
+		ret = 1;
+	}
+
+	clock->actual_dclk = actual_dclk/port_mult;
+
+	if (ret) {
+		EMGD_ERROR("Clock %ld could not be programmed", dclk);
+		return ret;
+	}
+
+	/* Disable DPLL, Write 2 into P for saftey */
+	control = BIT28 | (2<<clock->p_shift) | vga_mult;
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control, control);
+	EMGD_DEBUG("sdvo: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* Program N, M */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->mnp, (n<<16) | m);
+	EMGD_DEBUG("sdvo: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->mnp, (n<<16)|m);
+
+	/* Enable DPLL, Disable VGA mode and sitck in new P values */
+	control = BIT31 | BIT30 | BIT28 | (p1<<clock->p_shift) | vga_mult;
+
+	/* Set the clock source correctly based on PD settings */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		control |= port->clock_bits;
+	}
+
+	/* sDVO Multiplier bits[7:0] */
+	if (port_mult == 2) {
+		control |= (1 << 4);
+	} else if (port_mult == 3) {
+		control |= (2 << 4);
+	} else if (port_mult == 4) {
+		control |= (3 << 4);
+	}
+
+	/* Double buffered */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control, control);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control, control);
+	EMGD_DEBUG("sdvo: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* We must wait for 150 us for the dpll clock to warm up */
+	//OS_SLEEP(150);
+	wait_dpll();
+
+	kms_program_cdvo(emgd_crtc);
+
+	return 0;
+}
+/*!
+ *
+ * @param display
+ * @param clock
+ * @param dclk
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int program_clock_sdvo_tnc(igd_display_context_t *display,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	int ret;
+	unsigned long m, n, p1;
+	unsigned long control;
+	unsigned long ref_freq;
+	unsigned long port_mult, vga_mult;
+	unsigned long target_error, actual_dclk;
+	igd_display_port_t *port;
+	tnc_limits_t *l;
+
+	EMGD_DEBUG("Enter program_clock");
+
+	port = PORT_OWNER(display);
+
+	/* FIXME: No info available in EAS and waiting for info. */
+	if (dclk > 100000) {          /* 100-200 MHz */
+		port_mult = 1;
+	} else if (dclk > 50000) {    /* 50-100 Mhz */
+		port_mult = 2;
+	} else {                      /* 25-50 Mhz */
+		port_mult = 4;
+	}
+
+	dclk *= port_mult;
+
+	l = &tnc_sdvo_limits[0];
+	ref_freq = l->ref_freq;
+
+	vga_mult = READ_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control) & 0x3;
+
+	target_error = TARGET_ERROR;
+
+	/* For external clock sources always use ref_clock == dclk */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		ref_freq = dclk;
+		/* When clock source sdvo device, allowed error is 0. */
+		target_error = 0;
+	}
+
+	ret = get_clock(dclk, ref_freq, l, &m, &n, &p1, target_error,
+		IGD_PORT_SDVO, &actual_dclk, port->pd_driver->type);
+
+	clock->actual_dclk = actual_dclk/port_mult;
+
+	if (ret) {
+		EMGD_ERROR("Clock %ld could not be programmed", dclk);
+		return ret;
+	}
+
+	/* Disable DPLL, Write 2 into P for saftey */
+	control = BIT28 | (2<<clock->p_shift) | vga_mult;
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control, control);
+	EMGD_DEBUG("sdvo: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* Program N, M */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->mnp, (n<<16) | m);
+	EMGD_DEBUG("sdvo: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->mnp, (n<<16)|m);
+
+	/* Enable DPLL, Disable VGA mode and sitck in new P values */
+	control = BIT31 | BIT30 | BIT28 | (p1<<clock->p_shift) | vga_mult;
+
+	/* Set the clock source correctly based on PD settings */
+	if(port->pd_flags & PD_FLAG_CLK_SOURCE) {
+		control |= port->clock_bits;
+	}
+
+	/* sDVO Multiplier bits[7:0] */
+	if (port_mult == 2) {
+		control |= (1 << 4);
+	} else if (port_mult == 3) {
+		control |= (2 << 4);
+	} else if (port_mult == 4) {
+		control |= (3 << 4);
+	}
+
+	/* Double buffered */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control, control);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, clock->dpll_control, control);
+	EMGD_DEBUG("sdvo: EMGD_WRITE32: 0x%lx = 0x%lx",
+		clock->dpll_control, control);
+
+	/* We must wait for 150 us for the dpll clock to warm up */
+	//OS_SLEEP(150);
+	wait_dpll();
+
+	program_cdvo_tnc();
+
+	return 0;
+}
+
+int kms_program_clock_tnc(emgd_crtc_t *emgd_crtc,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	EMGD_TRACE_ENTER;
+
+	if (get_port_type(emgd_crtc->crtc_id) == IGD_PORT_LVDS) {
+		EMGD_TRACE_EXIT;
+		return kms_program_clock_lvds_tnc(emgd_crtc, clock, dclk);
+	} else {
+		EMGD_TRACE_EXIT;
+		return kms_program_clock_sdvo_tnc(emgd_crtc, clock, dclk);
+	}
+}
+
+int program_clock_tnc(igd_display_context_t *display,
+	igd_clock_t *clock,
+	unsigned long dclk)
+{
+	EMGD_TRACE_ENTER;
+
+	if (PORT_TYPE(display) == IGD_PORT_LVDS) {
+		EMGD_TRACE_EXIT;
+		return program_clock_lvds_tnc(display, clock, dclk);
+	} else {
+		EMGD_TRACE_EXIT;
+		return program_clock_sdvo_tnc(display, clock, dclk);
+	}
+}
+
+
+/*!
+ * Poll for DPLL register lock. This is only valid for DPLLB
+ *
+ * @return void
+ */
+static void wait_dpll(void)
+{
+#ifndef CONFIG_MICRO
+	unsigned long temp;
+	os_alarm_t timeout;
+
+	EMGD_TRACE_ENTER;
+
+	/* Wait for DPLL lock, about 50 msec (20Hz). */
+	timeout = OS_SET_ALARM(50);
+	do {
+		OS_SCHEDULE();
+		/* Check for Bit16, 1 means DPLL is locked (TNC EAS Ver 2.0) */
+		temp = (READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x606C) & 0x10000) >> 16;
+
+		/* Check for timeout */
+	} while ((!temp) && (!OS_TEST_ALARM(timeout)));
+
+	if (!temp) {
+		EMGD_ERROR_EXIT("Timeout waiting for pipe enable/disable");
+	}
+
+	EMGD_TRACE_EXIT;
+#else
+	/* to save space in VBIOS, we use to old method of waiting for the DPLL
+	* to warm up */
+	OS_SLEEP(150);
+#endif
+	return;
+}
+
+
+/* This is the initialization code for B0 stepping */
+static int kms_program_cdvo(emgd_crtc_t *emgd_crtc)
+{
+	int counter = 0;
+	igd_display_pipe_t *pipe = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+
+    #ifndef CONFIG_MICRO
+	/*
+	 * CDVO reset has been done. Check that offset 0x7000 has the value 0x50
+	 * and this would mean that reset has been done. We only need to do cdvo
+	 * reset once per warm reset
+	 */
+	if((READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x7000) == 0x50) ||
+		!FLAG(flag_enable_cdvo_reset)){
+		EMGD_TRACE_EXIT;
+		return TRUE;
+	}
+
+    #endif
+
+	/* pipe_temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPE(display)->pipe_reg); */
+
+	/* Disable pipe */
+	WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, pipe->pipe_reg, 0);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, pipe->pipe_reg, 0);
+
+	/* Sleep for a while to wait for the pipe to disable. There are no
+	* status bits to check if pipe B has been enable */
+	OS_SLEEP(25);
+
+    #if 0
+       /* Disable DPLL */
+       vga_mult = READ_MMIO_REG_TNC(IGD_PORT_SDVO,
+           PIPE(display)->clock_reg->dpll_control) & 0x3;
+       control = BIT28 | (2<<PIPE(display)->clock_reg->p_shift) | vga_mult;
+
+       WRITE_MMIO_REG_TNC(IGD_PORT_SDVO,
+           PIPE(display)->clock_reg->dpll_control, control);
+       WRITE_MMIO_REG_TNC(IGD_PORT_SDVO,
+           PIPE(display)->clock_reg->dpll_control, control);
+    #endif
+
+	/* the checking is needed for VBIOS but not needed for driver */
+	do{
+		WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, cdvo_reset[counter].reg,
+			cdvo_reset[counter].value);
+			counter++;
+	}while(cdvo_reset[counter].reg != 0);
+
+	/* Enable sDVOB port */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, 0x61140,
+	READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x61140) | 0xC0000018 );
+
+
+	EMGD_TRACE_EXIT;
+
+	return TRUE;
+}
+
+/* This is the initialization code for B0 stepping */
+int program_cdvo_tnc(void)
+{
+	int 		  counter = 0;
+	unsigned long pipe_temp;
+
+	EMGD_TRACE_ENTER;
+
+    #ifndef CONFIG_MICRO
+	/*
+	 * CDVO reset has been done. Check that offset 0x7000 has the value 0x50
+	 * and this would mean that reset has been done. We only need to do cdvo
+	 * reset once per warm reset
+	 */
+	if((READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x7000) == 0x50) ||
+		!FLAG(flag_enable_cdvo_reset)){
+		EMGD_TRACE_EXIT;
+		return TRUE;
+	}
+
+    #endif
+
+	pipe_temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_CONF);
+
+	/* Disable pipe */
+	WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, PIPEB_CONF, 0);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_CONF, 0);
+
+	/* Sleep for a while to wait for the pipe to disable. There are no
+	* status bits to check if pipe B has been enable */
+	OS_SLEEP(25);
+
+#if 0
+       /* Disable DPLL */
+       vga_mult = READ_MMIO_REG_TNC(IGD_PORT_SDVO,
+           PIPE(display)->clock_reg->dpll_control) & 0x3;
+       control = BIT28 | (2<<PIPE(display)->clock_reg->p_shift) | vga_mult;
+
+       WRITE_MMIO_REG_TNC(IGD_PORT_SDVO,
+           PIPE(display)->clock_reg->dpll_control, control);
+       WRITE_MMIO_REG_TNC(IGD_PORT_SDVO,
+           PIPE(display)->clock_reg->dpll_control, control);
+#endif
+
+	/* the checking is needed for VBIOS but not needed for driver */
+	do{
+		WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, cdvo_reset[counter].reg,
+			cdvo_reset[counter].value);
+			counter++;
+	}while(cdvo_reset[counter].reg != 0);
+
+	/* Enable sDVOB port */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, 0x61140,
+	READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x61140) | 0xC0000018 );
+
+	/* Restore Pipe B configuration value */
+	WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, PIPEB_CONF, pipe_temp);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_CONF, pipe_temp);
+
+	EMGD_TRACE_EXIT;
+
+	return TRUE;
+}
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/tnc/kms_mode_tnc.c b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/kms_mode_tnc.c
new file mode 100644
index 0000000..30696ac
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/kms_mode_tnc.c
@@ -0,0 +1,1746 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: kms_mode_tnc.c
+ * $Revision: 1.5 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <igd_pwr.h>
+#include <utils.h>
+#include <vga.h>
+#include <pi.h>
+
+#include <tnc/regs.h>
+#include <tnc/context.h>
+#include <tnc/mi.h>
+#include <tnc/instr.h>
+#include <tnc/igd_tnc_wa.h>
+#include "drm_emgd_private.h"
+
+#include "../cmn/mode_dispatch.h"
+#include "../cmn/match.h"
+#include "mode_tnc.h"
+#include <math_fix.h>
+#include <memory.h>
+
+
+/*------------------------------------------------------------------------------
+ * Function Prototypes
+ *----------------------------------------------------------------------------*/
+int kms_mode_get_stride_stereo_tnc(igd_display_pipe_t *pipe,
+	igd_display_plane_t *plane,
+	unsigned long *stride,
+	unsigned long *stereo,
+	unsigned long flags);
+void kms_program_pipe_vga_tnc(emgd_crtc_t *emgd_crtc);
+static void kms_program_pipe_tnc(emgd_crtc_t *emgd_crtc);
+static void kms_set_pipe_pwr_tnc(emgd_crtc_t *emgd_crtc, unsigned long enable);
+static void kms_program_plane_tnc(emgd_crtc_t *emgd_crtc, unsigned long status);
+static void kms_set_plane_pwr_tnc(emgd_crtc_t *emgd_crtc, unsigned long enable);
+static int  kms_program_port_tnc(emgd_encoder_t *emgd_encoder,
+	unsigned long status);
+static int  kms_program_port_lvds_tnc(emgd_encoder_t *emgd_encoder,
+	unsigned long status);
+static int  kms_program_port_sdvo_tnc(emgd_encoder_t *emgd_encoder,
+	unsigned long status);
+static int  kms_post_program_port_tnc(emgd_encoder_t *emgd_encoder,
+	unsigned long status);
+static u32  kms_get_vblank_counter_tnc(emgd_crtc_t *emgd_crtc);
+
+
+static int set_color_correct_tnc(emgd_crtc_t *emgd_crtc);
+
+extern unsigned long get_port_type(int crtc_id);
+extern void wait_pipe(unsigned long pt, unsigned long pipe_reg, unsigned long check_on_off);
+extern void disable_vga_tnc (unsigned char *mmio);
+extern int write_bunit_tnc(unsigned long reg, unsigned long val);
+extern int read_bunit_tnc(unsigned long reg, unsigned long *val);
+extern int kms_program_clock_tnc(emgd_crtc_t *emgd_crtc,
+	igd_clock_t *clock, unsigned long dclk);
+extern int wait_for_vblank_tnc(unsigned long pipe_reg);
+extern void initialize_ST_bridge(igd_context_t *context,
+	igd_display_port_t *port);
+
+
+/*------------------------------------------------------------------------------
+ * Global Variables
+ *----------------------------------------------------------------------------*/
+extern int flag_basic_htotal_formula_wa;
+extern int flag_turn_off_port_wa;
+extern int flag_enable_tuning_wa;
+extern unsigned long ports_tnc[2];
+extern int vphase;
+extern tnc_wa_timing_t tune;
+extern mode_data_tnc_t device_data_tnc[1];
+
+
+/*------------------------------------------------------------------------------
+ * KMS Dispatch Table
+ *----------------------------------------------------------------------------*/
+mode_kms_dispatch_t mode_kms_dispatch_tnc = {
+	kms_program_pipe_tnc,
+	kms_set_pipe_pwr_tnc,
+	kms_program_plane_tnc,
+	kms_set_plane_pwr_tnc,
+	kms_program_port_tnc,
+	kms_post_program_port_tnc,
+	kms_get_vblank_counter_tnc,
+	kms_match_mode,
+};
+
+
+
+/*!
+ * Get the stride and stereo values based on the display.
+ *
+ * @param plane Pointer to hardware device instance data
+ * @param pipe  Pointer to hardware device instance data
+ * @param flags Should the stereo be for the frontbuffer or backbuffer?
+ *
+ * @return stride - Stride of the display
+ * @return stereo - Stereo address of the display
+ */
+int kms_mode_get_stride_stereo_tnc(igd_display_pipe_t *pipe,
+		igd_display_plane_t *plane,
+		unsigned long *stride,
+		unsigned long *stereo,
+		unsigned long flags)
+{
+	unsigned long pitch = plane->fb_info->screen_pitch;
+	igd_timing_info_t *timing = pipe->timing;
+	unsigned long base_offset;
+
+	EMGD_TRACE_ENTER;
+
+	base_offset = plane->fb_info->visible_offset;
+	*stride = pitch;
+	*stereo = 0;
+
+	/* For field replication, valid for interlaced modes only
+	 *     set stereo = fb_base
+	 *         stride = pitch
+	 */
+	if (timing->mode_info_flags & IGD_SCAN_INTERLACE) {
+
+		if(timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* Interlaced + Line double flags means field replication.
+			 * same lines are sent for both fields. Program the
+			 * second eye to be same as the first
+			 */
+			*stereo = base_offset;
+		} else {
+			/* Regular interlaced. Second eye starts on line 2.
+			 * Skip every other line.
+			 */
+			*stereo = base_offset + pitch;
+			*stride = pitch << 1;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*!
+ *
+ * @param emgd_crtc
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL if color attributes not found
+ */
+static int set_color_correct_tnc(emgd_crtc_t *emgd_crtc)
+{
+	const int        MID_PIXEL_VAL    = 125;
+	const int        MAX_PIXEL_VAL    = 255;
+	const int        NUM_PALETTE_ENTRIES = 256;
+
+	igd_display_port_t *port = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	struct drm_encoder *encoder = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+
+	unsigned int     gamma_r_max_24i_8f, gamma_r_min_24i_8f;
+	unsigned int     gamma_g_max_24i_8f, gamma_g_min_24i_8f;
+	unsigned int     gamma_b_max_24i_8f, gamma_b_min_24i_8f;
+	unsigned int     new_gamma_r_24i_8f, new_gamma_g_24i_8f;
+	unsigned int     new_gamma_b_24i_8f;
+	unsigned int     gamma_normal_r_24i_8f, gamma_normal_g_24i_8f;
+	unsigned int     gamma_normal_b_24i_8f;
+	int              brightness_factor_r, brightness_factor_g;
+	int              brightness_factor_b;
+	int              contrast_factor_r, contrast_factor_g;
+	int              contrast_factor_b;
+
+	unsigned int      *palette;
+	unsigned int       i;
+	struct drm_device *dev = NULL;
+
+	igd_range_attr_t *gamma_attr      = NULL, *contrast_attr = NULL;
+	igd_range_attr_t *brightness_attr = NULL;
+	igd_attr_t       *hal_attr_list   = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			break;
+		}
+	}
+	if (!port) {
+		EMGD_ERROR_EXIT("No port being used.");
+		return -IGD_ERROR_INVAL;
+	}
+	hal_attr_list  = port->attributes;
+
+	/* Using OS_ALLOC to avoid using > 1024 on stack (frame size warning ) */
+	palette = OS_ALLOC(sizeof (unsigned int) * NUM_PALETTE_ENTRIES);
+
+	/* start with a fresh palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		palette[i] = (i << 16) | (i << 8) | i;
+	}
+
+	/* get a pointer to gamma, contrast, and brightness attr */
+	i = 0;
+
+	while (PD_ATTR_LIST_END != hal_attr_list[i].id) {
+		switch (hal_attr_list[i].id) {
+		case PD_ATTR_ID_FB_GAMMA:
+			gamma_attr      = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_BRIGHTNESS:
+			brightness_attr = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_CONTRAST:
+			contrast_attr   = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		default:
+			break;
+		}
+
+		i++;
+	}
+
+	if(!gamma_attr || !brightness_attr || !contrast_attr) {
+		EMGD_ERROR_EXIT("Color Correction Atrributes not found!");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Get the max and min */
+	gamma_r_max_24i_8f = ((gamma_attr->max >> 16) & 0xFF) << 3;
+	gamma_g_max_24i_8f = ((gamma_attr->max >>  8) & 0xFF) << 3;
+	gamma_b_max_24i_8f =  (gamma_attr->max        & 0xFF) << 3;
+
+	gamma_r_min_24i_8f = ((gamma_attr->min >> 16) & 0xFF) << 3;
+	gamma_g_min_24i_8f = ((gamma_attr->min >>  8) & 0xFF) << 3;
+	gamma_b_min_24i_8f =  (gamma_attr->min        & 0xFF) << 3;
+
+	/* The new gamma values are in 3i.5f format, but we must convert it
+	 * to 24i.8f format before passing it to OS_POW_FIX
+	 */
+	new_gamma_r_24i_8f = ((gamma_attr->current_value >> 16) & 0xFF) << 3;
+	new_gamma_g_24i_8f = ((gamma_attr->current_value >> 8) & 0xFF) << 3;
+	new_gamma_b_24i_8f = (gamma_attr->current_value & 0xFF) << 3;
+
+	/* make sure the new gamma is within range */
+	new_gamma_r_24i_8f = OS_MIN(gamma_r_max_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_r_24i_8f = OS_MAX(gamma_r_min_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_g_24i_8f = OS_MIN(gamma_g_max_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_g_24i_8f = OS_MAX(gamma_g_min_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_b_24i_8f = OS_MIN(gamma_b_max_24i_8f, new_gamma_b_24i_8f);
+	new_gamma_b_24i_8f = OS_MAX(gamma_b_min_24i_8f, new_gamma_b_24i_8f);
+
+
+	gamma_normal_r_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_r_24i_8f);
+
+	gamma_normal_g_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_g_24i_8f);
+
+	gamma_normal_b_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_b_24i_8f);
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		unsigned int new_gamma;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* Note that we do not try to calculate the gamma if it
+		 * is 1.0, e.g. 0x100.  This is to avoid round-off errors
+		 */
+
+		/* red: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_r_24i_8f) {
+			cur_color  = (cur_palette >> 16) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_r_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_r_24i_8f;
+			palette[i] &= 0x00FFFF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_g_24i_8f) {
+			cur_color  = (cur_palette >> 8) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_g_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_g_24i_8f;
+			palette[i] &= 0xFF00FF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_b_24i_8f) {
+			cur_color  = cur_palette & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_b_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_b_24i_8f;
+			palette[i] &= 0xFFFF00;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF);
+		}
+	}
+
+
+	/* Brightness correction */
+	brightness_factor_r = (brightness_attr->current_value >> 16) & 0xFF;
+	brightness_factor_g = (brightness_attr->current_value >> 8) & 0xFF;
+	brightness_factor_b = brightness_attr->current_value & 0xFF;
+
+	/* The factors are offset by 0x80 because 0x80 is 0 correction */
+	brightness_factor_r -= 0x80;
+	brightness_factor_g -= 0x80;
+	brightness_factor_b -= 0x80;
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int          new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 16) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_r;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0x00FFFF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+
+		/* green: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 8) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_g;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFF00FF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+
+		/* blue: calculate and make sure the result is within range */
+		cur_color     =  cur_palette & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_b;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFFFF00;
+		palette[i]    |= OS_MAX(new_pixel_val, 0) & 0xFF;
+	}
+
+
+	/* contrast correction */
+	contrast_factor_r = (contrast_attr->current_value >> 16) & 0xFF;
+	contrast_factor_g = (contrast_attr->current_value >> 8) & 0xFF;
+	contrast_factor_b = contrast_attr->current_value & 0xFF;
+
+	/* make sure values are within range */
+	contrast_factor_r -= 0x80;
+	contrast_factor_g -= 0x80;
+	contrast_factor_b -= 0x80;
+
+
+	/* We're doing integer division in this loop using 16i.16f
+	 * integers.  The result will then be converted back into a
+	 * regular, 32-bit integer
+	 */
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		if (0 != contrast_factor_r ) {
+			cur_color     = (cur_palette >> 16) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_r);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0x00FFFF;  /* clear out the R color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0 != contrast_factor_g ) {
+			cur_color     = (cur_palette >> 8) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_g);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFF00FF;  /* clear out the G color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0 != contrast_factor_b) {
+			cur_color     = cur_palette & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_b);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFFFF00;  /* clear out the B color */
+			palette[i]    |=   OS_MAX(new_pixel_val, 0) & 0xFF;
+		}
+	}
+
+
+	/* write the new values in the palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		/* SDVO palette register is not accesible */
+		EMGD_WRITE32(palette[i],
+			MMIO_TNC(IGD_PORT_LVDS) +
+			pipe->palette_reg + i*4);
+	}
+
+	OS_FREE(palette);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*!
+ * kms_program_pipe_vga_tnc
+ *
+ * @param emgd_crtc Pointer to our private kms crtc structure
+ *
+ * @return void
+ */
+void kms_program_pipe_vga_tnc(
+	emgd_crtc_t *emgd_crtc)
+{
+	struct drm_device  *dev = NULL;
+	igd_timing_info_t  *timing = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	igd_context_t      *context = NULL;
+	struct drm_encoder *encoder = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	igd_display_port_t *port = NULL;
+	unsigned long       vga_control;
+	unsigned long       upscale = 0;
+	int                 centering = 1;
+	unsigned long       pt;
+
+	EMGD_TRACE_ENTER;
+
+	pipe = emgd_crtc->igd_pipe;
+	dev = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	/*
+	 * VGA Plane can attach to only one pipe at a time. LVDS can
+	 * only attach to pipe B. We need to use the display passed to
+	 * determine the pipe number to use. (Napa is same as Alm).
+	 */
+
+	/*
+	 * We can come here with following cases:
+	 *   1. magic->vga    CRT, DVI type displays
+	 *   2. native->vga   int-lvds, and up-scaling lvds displays
+	 *   3. pipe->vga     TV and other unscaled-lvds displays
+	 */
+	vga_control = EMGD_READ32(context->device_context.virt_mmadr + 0x71400);
+	vga_control &= 0x18e3ff00;
+	vga_control |= 0x8e;
+
+	timing = pipe->timing;
+	if(!timing->extn_ptr) {
+		EMGD_ERROR_EXIT("No Extension pointer in program_pipe_vga_tnc");
+		return;
+	}
+
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+
+		    /* Find UPSCALING attr value*/
+		    pi_pd_find_attr_and_value(port,
+	            PD_ATTR_ID_PANEL_FIT,
+	            0,/*no PD_FLAG for UPSCALING */
+	            NULL, /* dont need the attr ptr*/
+	            &upscale);
+		    /* this PI func will not modify value
+			 * of upscale if attr does not exist
+			 */
+			break;
+		}
+	}
+
+	/* magic->vga || native->vga cases, centering isn't required */
+	if ((timing->width == 720 && timing->height == 400) || upscale) {
+		EMGD_DEBUG("Centering = 0");
+		centering = 0;
+	}
+
+	/* Enable border */
+	if((timing->width >= 800) && !upscale) {
+		EMGD_DEBUG("Enable VGA Border");
+		vga_control |= (1L<<26);
+	}
+
+	if(timing->width == 640) {
+		EMGD_DEBUG("Enable Nine Dot Disable");
+		vga_control |= (1L<<18);
+	}
+
+	if(centering) {
+		EMGD_DEBUG("Enable VGA Center Centering");
+		vga_control |= 1L<<24;
+
+		if(timing->height >= 960) {
+			if(timing->width >= 1280) {
+				EMGD_DEBUG("Enable VGA 2x (Nine Dot Disable)");
+				vga_control |= (1L<<30) | (1L<<18);
+			}
+		}
+	} else {
+		pt = get_port_type(emgd_crtc->crtc_id);
+		if(pt == IGD_PORT_LVDS) {
+			EMGD_DEBUG("Enable VGA Upper-Left Centering & Nine Dot Disable");
+			vga_control |= (1L<<25 | (1L<<18));
+		} else if (upscale) {
+			EMGD_DEBUG("Enable VGA Center Upper-left for upscale ports");
+			vga_control |= 1L<<25;
+		}
+	}
+
+	if(pipe->pipe_num) {
+		vga_control |= 1L<<29;
+	}
+
+	kms_program_pipe_vga(emgd_crtc, (igd_timing_info_t *)timing->extn_ptr);
+	EMGD_WRITE32(vga_control, context->device_context.virt_mmadr + 0x71400);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+/**
+ * kms_set_pipe_pwr_tnc
+ *
+ * Turns the pipe ON or OFF depending on the input
+ *
+ * @param emgd_crtc (IN) specifies the pipe to change
+ * @param enable    (IN) TRUE to enable pipe, FALSE to disable
+ *
+ * @return
+ */
+static void kms_set_pipe_pwr_tnc(emgd_crtc_t *emgd_crtc, unsigned long enable)
+{
+	unsigned long       pipe_conf;
+	unsigned long       port_type;
+	unsigned long 		temp;
+	struct drm_device  *dev;
+	igd_display_pipe_t *pipe;
+	igd_context_t      *context;
+
+
+	EMGD_TRACE_ENTER;
+
+
+	dev       = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context   = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	pipe      = emgd_crtc->igd_pipe;
+	port_type = get_port_type(emgd_crtc->crtc_id);
+	pipe_conf = READ_MMIO_REG_TNC(port_type, pipe->pipe_reg);
+
+
+	/* Do nothing if current power state is same as what we want to set */
+	/* The PIPE_ENABLE bit is at bit-position 31 */
+	if ( (enable << 31) == (pipe_conf & PIPE_ENABLE) ){
+
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+
+	if (!enable) {
+
+		/* For SDVO, disable both pipe Bs in 0:2:0 and 0:3:0 */
+		if (IGD_PORT_SDVO == port_type) {
+			WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, pipe->pipe_reg,
+				pipe_conf & ~PIPE_ENABLE);
+		}
+
+		/* Disable pipe */
+		WRITE_MMIO_REG_TNC(port_type, pipe->pipe_reg, pipe_conf & ~PIPE_ENABLE);
+
+		/* check when the pipe is disabled. */
+		wait_pipe(port_type, pipe->pipe_reg, 0);
+
+		/* Make sure the associated DPLL is turned off. */
+		temp = READ_MMIO_REG_TNC(port_type, pipe->clock_reg->dpll_control);
+		if (temp & BIT31) {
+			/* Double buffered */
+			WRITE_MMIO_REG_TNC(port_type, pipe->clock_reg->dpll_control,
+				temp & ~BIT31);
+			WRITE_MMIO_REG_TNC(port_type, pipe->clock_reg->dpll_control,
+				temp & ~BIT31);
+		}
+
+
+		EMGD_DEBUG("Set Pipe Power: OFF");
+
+	} else {
+
+		if (IGD_PORT_SDVO == port_type) {
+			WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, pipe->pipe_reg,
+				pipe_conf | PIPE_ENABLE);
+		}
+
+		/* Enable pipe */
+		WRITE_MMIO_REG_TNC(port_type, pipe->pipe_reg, pipe_conf | PIPE_ENABLE);
+
+		EMGD_DEBUG("Set Pipe Power: ON");
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+/**
+ * kms_program_pipe_tnc
+ *
+ * Called before a mode set, takes the input "mode", matches it to the closest
+ * supported mode, then put the supported mode into "adjusted_mode" to let the
+ * caller know.
+ *
+ * @param encoder (IN) Encoder being prepared
+ *
+ * @return true, false (details TBD)
+ */
+static void kms_program_pipe_tnc(emgd_crtc_t *emgd_crtc)
+{
+	unsigned long       timing_reg;
+	unsigned long       pipe_conf;
+	unsigned long       hactive, vactive;
+	short               hactive_tmp, vactive_tmp;
+	igd_timing_info_t  *current_timings;
+	igd_timing_info_t   pTimings_tmp;
+	struct drm_device  *dev          = NULL;
+	igd_display_pipe_t *pipe         = NULL;
+	igd_display_port_t *port         = NULL;
+	igd_context_t      *context      = NULL;
+	struct drm_encoder *encoder      = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	pd_timing_t        *vga_timing   = NULL;
+
+	unsigned long flag_clip_fix;
+	unsigned long pipe_reg;
+	unsigned long temp;
+	unsigned long pt;
+	unsigned long dc;
+	unsigned long calc;
+	int           i;
+	tnc_wa_timing_t *wa;
+	/* igd_framebuffer_info_t *fb_info = PLANE(display)->fb_info; */
+	platform_context_tnc_t *platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	pipe            = emgd_crtc->igd_pipe;
+	current_timings = pipe->timing;
+	vga_timing      = (pd_timing_t *) current_timings->extn_ptr;
+	pipe_reg        = pipe->pipe_reg;
+
+	pt       = get_port_type(emgd_crtc->crtc_id);
+	dev      = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context  = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	platform_context = (platform_context_tnc_t *) mode_context->context->platform_context;
+	flag_clip_fix    = mode_context->clip_hw_fix;
+
+	EMGD_DEBUG("Device power state: D%ld", context->device_context.power_state);
+
+	pipe_conf = READ_MMIO_REG_TNC(pt, pipe->pipe_reg);
+
+	/* Preserving bits 0:17, bit 20, bit 24, bit 26, bit 29:30.  The spec on
+	 * which bits to preserve has been updated since we've originally written
+	 * the code for the non-KMS path.  However, to minimize risk of this fix,
+	 * we are only updating them here.  This is why we are not using
+	 * device_data_tnc->pipe_preserve.  */
+	pipe_conf &= 0x6513FFFF;
+
+	/* For TNC B1, enable hardware cliping fix*/
+	if((platform_context->tnc_dev3_rid == TNC_B1_DEV3_RID)&&
+		(flag_clip_fix & IGD_CLIP_FIX_GLOBAL_ENABLE)) {
+
+		/* Disable SDVO Pipe in Device 2 and Device 3 */
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, pipe->pipe_reg,
+			pipe_conf & (~0x80000000L));
+
+		WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, pipe->pipe_reg,
+			pipe_conf & (~0x80000000L));
+
+		/* check when the pipe is disabled. */
+		wait_pipe(IGD_PORT_LVDS, pipe->pipe_reg, 0);
+
+		/* Enable clipping hardware fix */
+		temp = READ_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS);
+		if(flag_clip_fix & IGD_CLIP_FIX_REPLACE_STALL) {
+			temp |= BIT18;
+		} else {
+			temp &= ~BIT18;
+		}
+		if(flag_clip_fix & IGD_CLIP_FIX_DISABLE_THROTTLE) {
+			temp |= BIT15;
+		} else {
+			temp &= ~BIT15;
+		}
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS, temp);
+	}
+
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (((struct drm_crtc *)(&emgd_crtc->base)) == encoder->crtc) {
+			emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+			port = emgd_encoder->igd_port;
+			break;
+		}
+	}
+
+	if (NULL == emgd_encoder) {
+		EMGD_ERROR_EXIT("Invalid CRTC selected.");
+		return;
+	}
+
+	/* Debug messages */
+	EMGD_DEBUG("Current timings %ux%u mode_number = %u "
+			   "mode_info_flags = 0x%lx, dclk = %lu",
+			   current_timings->width,
+			   current_timings->height,
+			   current_timings->mode_number,
+			   current_timings->mode_info_flags,
+			   current_timings->dclk);
+
+	if (vga_timing) {
+	  EMGD_DEBUG("ext_timing %ux%u mode_number = %u "
+				 "mode_info_flags= 0x%lx, dclk = %lu",
+				 vga_timing->width,
+				 vga_timing->height,
+				 vga_timing->mode_number,
+				 vga_timing->mode_info_flags,
+				 vga_timing->dclk);
+	}
+
+
+	/*
+	 * If the mode is VGA and the PD says it handles all VGA modes without
+	 * reprogramming then just set the mode and leave centering off.
+	 */
+	if(current_timings->mode_info_flags & IGD_MODE_VESA) {
+		EMGD_DEBUG("IGD_MODE_VESA");
+
+		if (current_timings->mode_number <= VGA_MODE_NUM_MAX) {
+			EMGD_DEBUG("current_timings->mode_number <= VGA_MODE_NUM_MAX");
+			kms_program_pipe_vga_tnc(emgd_crtc);
+
+			EMGD_TRACE_EXIT;
+			return;
+		}
+	}
+
+	/* Program dot clock divisors. */
+	kms_program_clock_tnc(emgd_crtc, pipe->clock_reg, current_timings->dclk);
+
+	/* Program timing registers for the pipe */
+	timing_reg = pipe->timing_reg;
+	if (current_timings->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		hactive = (unsigned long)current_timings->width*2 - 1;
+	} else {
+		hactive = (unsigned long)current_timings->width - 1;
+	}
+
+	if (current_timings->mode_info_flags & IGD_LINE_DOUBLE) {
+		vactive = (unsigned long)current_timings->height*2 - 1;
+	} else {
+		/* For Atom E6xx Hardware will automatically divide by 2 to
+		   get the number of line in each field */
+		vactive = (unsigned long)current_timings->height - 1;
+	}
+
+
+	/* Reset the Palette */
+	for (i = 0; i < 256; i++) {
+		/* Program each of the 256 4-byte palette entries */
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, pipe->palette_reg + (i << 2),
+			((i<<16) | (i<<8) | i));
+	}
+
+	if (port) {
+		/* apply color correction */
+		for( i = 0; PD_ATTR_LIST_END != port->attributes[i].id; i++ ) {
+
+			if ((PD_ATTR_ID_FB_GAMMA      == (port->attributes[i].id)) ||
+				(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)) ||
+				(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)))  {
+
+				set_color_correct_tnc(emgd_crtc);
+			}
+		}
+	}
+
+
+	/*
+	 * NOTE: For size reasons the timng table contains unsigned short
+	 * values. Don't shift them past 16. Use a temp instead.
+	 * All register offsets and bit shift are verified for Gen4
+	 *
+	 * For SDVO display:
+	 * Write values into pipe B registers in both 0:2:0 and 0:3:0
+	 */
+
+	dc = *(context->mod_dispatch.dsp_current_dc);
+
+	wa = WA_TUNE;
+
+	for (i=0; i<2; i++) {
+		/* Temp variable */
+		pTimings_tmp = *current_timings;
+		hactive_tmp  = (short) hactive;
+		vactive_tmp  = (short) vactive;
+
+		/* This work around is only for Atom E6xx B0 */
+		if((IGD_DC_CLONE(dc) || IGD_DC_EXTENDED(dc))
+			&& pt == IGD_PORT_SDVO &&
+			(context->device_context.rid == TNC_B0_RID)
+			&& FLAG(flag_basic_htotal_formula_wa)){
+
+			if(!(current_timings->reserved_dd & TNC_HTOTAL_TUNED)){
+				/* Modify blanks so it always begin after active pixel and ends
+				 * at the end. Do not change it if we are already tuned to
+				 * maintain original timing specification
+				 */
+				pTimings_tmp.hblank_start =
+								(short) (current_timings->width - 1);
+				pTimings_tmp.vblank_start =
+								(short) (current_timings->height - 1);
+				pTimings_tmp.hblank_end = (short) (current_timings->htotal);
+				pTimings_tmp.vblank_end = (short) (current_timings->vtotal);
+			}
+
+			if(i==0){
+				if(current_timings->reserved_dd == 0 ||
+					(wa->counter > LIMIT_TOTAL_CHECK_DISPLAY)){
+					/* First time tuning */
+					if ((mode_context->ref_freq != 0) &&
+							(mode_context->ref_freq >= 190000) &&
+							(mode_context->ref_freq <= 210000)){
+						calc = (current_timings->htotal *
+									mode_context->ref_freq);
+					} else {
+						calc = (current_timings->htotal * LNC_CLOCK);
+					}
+
+					calc = (calc / (pipe->clock_reg->actual_dclk));
+					calc *= (current_timings->vtotal -1);
+					calc /= current_timings->vtotal;
+					pTimings_tmp.htotal = (short)calc;
+					current_timings->reserved_dd = wa->htotal =
+						pTimings_tmp.htotal;
+					EMGD_DEBUG("Delta = %d", wa->htotal);
+				}else if (current_timings->reserved_dd & TNC_HTOTAL_TUNED){
+					pTimings_tmp.htotal =
+						(short)(current_timings->reserved_dd &
+								(~TNC_HTOTAL_TUNED));
+				}else{
+					if(wa->htotal == 0)
+						wa->htotal = (short)current_timings->reserved_dd;
+					pTimings_tmp.htotal = wa->htotal;
+				}
+				/* Use vphase formula if available */
+				if(vphase){
+					pTimings_tmp.vtotal -= (short)vphase;
+					pTimings_tmp.vsync_start -= (short)vphase;
+					pTimings_tmp.vsync_end -= (short)vphase;
+					pTimings_tmp.vblank_end -= (short)vphase;
+				}else{
+					pTimings_tmp.hblank_end +=
+						(short) (pTimings_tmp.htotal - current_timings->htotal);
+				}
+			}
+		}
+
+
+		/* Program Timings */
+		temp = (unsigned long)(pTimings_tmp.htotal) << 16 | hactive_tmp;
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg, temp);
+
+		temp = ((unsigned long) pTimings_tmp.hblank_end << 16) |
+			(unsigned long)(pTimings_tmp.hblank_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x04, temp);
+
+		temp = ((unsigned long)(current_timings->hsync_end) << 16) |
+			(unsigned long)(current_timings->hsync_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x08, temp);
+
+		temp = ((unsigned long)(pTimings_tmp.vtotal) << 16) | vactive_tmp;
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x0C, temp);
+
+		temp = ((unsigned long)(pTimings_tmp.vblank_end) << 16) |
+			(unsigned long)(pTimings_tmp.vblank_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x10, temp);
+
+		temp = ((unsigned long)(pTimings_tmp.vsync_end)<< 16) |
+			(unsigned long)(pTimings_tmp.vsync_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x14, temp);
+
+		/*
+		 * If there is a linked mode it is either the VGA or a scaled
+		 * mode. If it is scaled then we need to use it as the source size.
+		 */
+		if (current_timings->extn_ptr) {
+			igd_timing_info_t *scaled_timings =
+				(igd_timing_info_t *)current_timings->extn_ptr;
+
+			if((scaled_timings->mode_info_flags & IGD_MODE_VESA) &&
+				(scaled_timings->mode_number <= VGA_MODE_NUM_MAX)) {
+				temp = (hactive << 16) | vactive;
+			} else {
+				EMGD_DEBUG("scaled_timings->width [%d], "
+						   "scaled_timings->height [%d]\n",
+					scaled_timings->width, scaled_timings->height);
+				temp = (unsigned long)scaled_timings->width  - 1;
+				temp = (temp << 16) |
+					(unsigned long)(scaled_timings->height - 1);
+			}
+		} else {
+			temp = (hactive_tmp << 16) | vactive_tmp;
+		}
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x1C, temp);
+
+		/* Put pipe in interlaced mode if requested:
+		 *     should only happen for LVDS display if at all. */
+		if (current_timings->mode_info_flags & IGD_SCAN_INTERLACE) {
+			pipe_conf |= (INTERLACE_EN);
+		} else {
+			pipe_conf &= ~(INTERLACE_EN);
+		}
+
+		pipe_conf |= PIPE_ENABLE;
+		WRITE_MMIO_REG_TNC(ports_tnc[i], pipe->pipe_reg, pipe_conf);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], pipe->pipe_reg, pipe_conf);
+
+		/* For LVDS port, don't touch dev3 registers stop after 1st iteration */
+		if (pt == IGD_PORT_LVDS) {
+			break;
+		}
+	}
+
+
+	/* Gen4 can check when the pipe is enabled. */
+	wait_pipe(IGD_PORT_LVDS, pipe->pipe_reg, 0x40000000);
+
+	/*
+	 * Set the VGA address range to 0xa0000 so that a normal (not VGA)
+	 * mode can be accessed through 0xa0000 in a 16bit world.
+	 */
+	WRITE_AR(context->device_context.virt_mmadr, 0x10, 0xb);
+	WRITE_VGA(context->device_context.virt_mmadr, GR_PORT, 0x06, 0x5);
+	WRITE_VGA(context->device_context.virt_mmadr, GR_PORT, 0x10, 0x1);
+
+	if(current_timings->extn_ptr) {
+		/* This means either internal scaling (LVDS) or centered VGA */
+		current_timings = current_timings->extn_ptr;
+		if(current_timings->extn_ptr) {
+			/* This is both the scaled and centered VGA */
+			current_timings = current_timings->extn_ptr;
+		}
+		if (current_timings->mode_info_flags & IGD_MODE_VESA) {
+			if (current_timings->mode_number <= VGA_MODE_NUM_MAX) {
+				kms_program_pipe_vga_tnc(emgd_crtc);
+			}
+		}
+	}
+
+	if (pt == IGD_PORT_SDVO) {
+		/*  Enable Chicken Bit */
+		/*  Setting BIT6 enable Pipe B Palette Write
+         *  to prevent hang during palette write */
+		temp = READ_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS);
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS, temp | BIT6);
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+static void kms_set_plane_pwr_tnc(emgd_crtc_t *emgd_crtc, unsigned long enable)
+{
+	unsigned long           plane_control;
+	unsigned long           plane_reg;
+	struct drm_device      *dev         = NULL;
+	igd_display_plane_t    *plane       = NULL;
+	igd_display_pipe_t     *pipe        = NULL;
+	igd_context_t          *context     = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program Plane: %s", enable?"ENABLE":"DISABLE");
+
+	pipe    = emgd_crtc->igd_pipe;
+	dev     = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	plane   = PLANE(pipe->owner);
+
+	if (!plane) {
+		EMGD_ERROR_EXIT("Trying to set power to a plane that is not tied "
+			" to a crtc.");
+		return;
+	}
+
+	/* In case a plane update is already in progress */
+	wait_for_vblank_tnc(pipe->pipe_reg);
+
+	/* Get the current value of the plane control register */
+	plane_reg     = plane->plane_reg;
+	plane_control = EMGD_READ32(context->device_context.virt_mmadr + plane_reg);
+
+	if((enable == FALSE) ||
+		(context->device_context.power_state != IGD_POWERSTATE_D0)) {
+		/*
+		 * Note: The vga programming code does not have an "off". So
+		 * when programming the plane to off we make sure VGA is off
+		 * as well.
+		 */
+		disable_vga_tnc(context->device_context.virt_mmadr);
+
+		/* The Sprite and Cursor planes need to turned off for the modeset
+		   to succeed. */
+       	EMGD_WRITE32(0x0, context->device_context.virt_mmadr + DSPCCNTR);
+       	EMGD_WRITE32(0x0, context->device_context.virt_mmadr + DSPCCNTR + DSP_START_OFFSET);
+
+       	EMGD_WRITE32(0x0, context->device_context.virt_mmadr + CUR_B_CNTR);
+       	EMGD_WRITE32(0x0, context->device_context.virt_mmadr + CUR_B_CNTR + CUR_BASE_OFFSET);
+
+       	EMGD_WRITE32(0x0, context->device_context.virt_mmadr + CUR_A_CNTR);
+       	EMGD_WRITE32(0x0, context->device_context.virt_mmadr + CUR_B_CNTR + CUR_BASE_OFFSET);
+
+		/*
+		 * To turn off plane A or B, the program have to trigger the plane A
+		 * or B start register.  Or else, it will not work.
+		 */
+		plane_control &= ~BIT31;
+
+		EMGD_WRITE32(plane_control,
+						context->device_context.virt_mmadr + plane_reg);
+
+		EMGD_WRITE32(EMGD_READ32(context->device_context.virt_mmadr +
+			plane_reg + DSP_START_OFFSET),
+			context->device_context.virt_mmadr + plane_reg + DSP_START_OFFSET);
+	} else {
+		/* Enable Plane */
+		plane_control |= BIT31;
+
+		EMGD_WRITE32(plane_control,
+			context->device_context.virt_mmadr + plane_reg);
+
+		EMGD_WRITE32(EMGD_READ32(context->device_context.virt_mmadr +
+			plane_reg + DSP_START_OFFSET),
+			context->device_context.virt_mmadr + plane_reg + DSP_START_OFFSET);
+	}
+
+
+	wait_for_vblank_tnc(pipe->pipe_reg);
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+
+static void kms_program_plane_tnc(emgd_crtc_t *emgd_crtc, unsigned long status)
+{
+	unsigned long stride;
+	unsigned long stereo;
+	unsigned long plane_control;
+	unsigned long other_plane_reg;
+	igd_timing_info_t      *timing      = NULL;
+	igd_framebuffer_info_t *fb_info     = NULL;
+	unsigned long           plane_reg;
+	struct drm_device      *dev         = NULL;
+	igd_display_plane_t    *plane       = NULL;
+	igd_display_pipe_t     *pipe        = NULL;
+	igd_context_t          *context     = NULL;
+	igd_timing_info_t      *pipe_timing = NULL;
+	unsigned long           ulreg       = 0;
+	unsigned long           b_reg       = 0;
+	/* tnc_wa_timing_t *wa = WA_TUNE; */
+
+	EMGD_TRACE_ENTER;
+
+
+	pipe    = emgd_crtc->igd_pipe;
+	dev     = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	plane     = PLANE(pipe->owner);
+	if (!plane) {
+		EMGD_ERROR("Trying to program a plane that is not tied to a crtc.");
+		return;
+	}
+	fb_info   = plane->fb_info;
+	plane_reg = plane->plane_reg;
+
+
+	EMGD_DEBUG("Device power state: D%ld", context->device_context.power_state);
+
+	wait_for_vblank_tnc(pipe->pipe_reg);
+
+	plane_control = EMGD_READ32(context->device_context.virt_mmadr + plane_reg);
+	if(plane->plane_reg == DSPACNTR) {
+		plane_control &= device_data_tnc->plane_a_preserve;
+	} else { /* if it's plane b or plane c */
+		plane_control &= device_data_tnc->plane_b_c_preserve;
+	}
+
+
+	/*
+	 * Note: The very first pass through this function will be with
+	 * status false and timings == NULL. Don't use the timings before
+	 * the check above.
+	 */
+	timing = pipe->timing;
+	pipe_timing = timing;
+	/* There is a special case code for legacy VGA modes */
+	while (timing->extn_ptr) {
+		timing = (igd_timing_info_t *)timing->extn_ptr;
+	}
+	if(MODE_IS_VGA(timing) && CHECK_VGA(pipe_timing)) {
+		kms_program_plane_vga(context->device_context.virt_mmadr, timing);
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	disable_vga_tnc(context->device_context.virt_mmadr);
+
+	/* enable plane, select pipe, enable gamma correction logic */
+	plane_control |= (pipe->pipe_num<<24);
+	pipe->plane = plane;
+	plane_control |= (1<<30);
+
+
+	/* Here the settings:
+	 *   If line + pixel dbling, set 21,20 to 01b, and set Horz Multiply
+	 *   If line dbling only,    set 21,20 to 11b
+	 *   If pixel dbling only,   set 21,20 to 00b, but set Horz Multiply
+	 *   If no doubling,         set 21,20 to 00b (no Horz Multiply)
+	 * For pixel doubling
+	 *           --> both progressive/interlaced modes
+	 * For Line doubling
+	 *           --> progressive modes only
+	 */
+
+	if (!(timing->mode_info_flags & IGD_SCAN_INTERLACE)) {
+		/* Line doubling in progressive mode requires special bits */
+		if (timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* BIT 20 for line & pixel doubling*/
+			plane_control |= BIT20;
+			/* check later, if no pixel doubling, set bit 21 too*/
+		}
+	}
+	if (timing->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		/* For line ONLY doubling, set bit 21 also '1' */
+		plane_control |= BIT21;
+	}
+
+	kms_mode_get_stride_stereo_tnc(pipe, plane, &stride, &stereo, 0);
+
+	/* set color depth */
+	switch (IGD_PF_DEPTH(fb_info->pixel_format)) {
+	case PF_DEPTH_8:
+		plane_control |= BIT27 | BIT30;
+		break;
+	case PF_DEPTH_16:
+		plane_control |= BIT28 | BIT26;
+		break;
+	default:
+	case PF_DEPTH_32:
+		plane_control |= BIT28 | BIT27;
+		break;
+	}
+
+	if(fb_info->flags & IGD_ENABLE_DISPLAY_GAMMA) {
+		plane_control |= (BIT30);
+	}
+
+	if(fb_info->flags & IGD_SURFACE_TILED) {
+		plane_control |= (BIT10);
+	}
+
+	/* Set watermark for Atom E6xx */
+	if (!mode_context->en_reg_override) {
+		if (plane_reg == DSPACNTR) {
+			other_plane_reg = DSPBCNTR;
+		} else {
+			other_plane_reg = DSPACNTR;
+		}
+
+		if (EMGD_READ32(context->device_context.virt_mmadr + other_plane_reg) &
+				0x80000000) {
+
+			EMGD_WRITE32(device_data_tnc->dsp_arb,
+				context->device_context.virt_mmadr + PIPEA_DISP_ARB_CTRL);
+		} else if (plane_reg == DSPACNTR) {
+			EMGD_WRITE32(0x00003fff,
+				context->device_context.virt_mmadr + PIPEA_DISP_ARB_CTRL);
+		} else {
+			EMGD_WRITE32(0x00003f80,
+				context->device_context.virt_mmadr + PIPEA_DISP_ARB_CTRL);
+		}
+
+		/*
+		 * Setting WM priority level to 11
+		 * to workaround display bouncing issues
+		 * TODO: Mode set from Clone->Single(Primary)
+		 * PIPEA_DISP_ARB_CTRL does not get updated
+		 */
+		ulreg = EMGD_READ32(context->device_context.virt_mmadr + 0x00020f8);
+		EMGD_WRITE32(ulreg | 0x000003f0,
+		context->device_context.virt_mmadr + 0x00020f8);
+
+		EMGD_WRITE32(device_data_tnc->fifo_watermark1,
+			context->device_context.virt_mmadr + FW_1);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark2,
+			context->device_context.virt_mmadr + FW_2);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark3,
+			context->device_context.virt_mmadr + FW_3);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark4,
+			context->device_context.virt_mmadr + FW_4);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark5,
+			context->device_context.virt_mmadr + FW_5);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark6,
+			context->device_context.virt_mmadr + FW_6);
+	} else {
+		/* en_reg_override=1 */
+		/* Override display registers */
+		EMGD_WRITE32(mode_context->gvd_hp_control,
+			context->device_context.virt_mmadr + 0x00020f8);
+		EMGD_WRITE32(mode_context->disp_arb,
+			context->device_context.virt_mmadr + PIPEA_DISP_ARB_CTRL);
+		EMGD_WRITE32(mode_context->fifo_watermark1,
+			context->device_context.virt_mmadr + FW_1);
+		EMGD_WRITE32(mode_context->fifo_watermark2,
+			context->device_context.virt_mmadr + FW_2);
+		EMGD_WRITE32(mode_context->fifo_watermark3,
+			context->device_context.virt_mmadr + FW_3);
+		EMGD_WRITE32(mode_context->fifo_watermark4,
+			context->device_context.virt_mmadr + FW_4);
+		EMGD_WRITE32(mode_context->fifo_watermark5,
+			context->device_context.virt_mmadr + FW_5);
+		EMGD_WRITE32(mode_context->fifo_watermark6,
+			context->device_context.virt_mmadr + FW_6);
+		EMGD_WRITE32(mode_context->disp_chicken_bits,
+			context->device_context.virt_mmadr + 0x0070400);
+
+		/* Override Bunit Chickenbits and BWFLUSH register */
+		write_bunit_tnc(0x30, mode_context->bunit_chicken_bits);
+		write_bunit_tnc(0x02, mode_context->bunit_write_flush);
+	}
+
+	EMGD_DEBUG(" GVD HP_CONTROL: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr +
+				PIPEA_DISP_ARB_CTRL);
+	EMGD_DEBUG(" Display Arbitration register: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + FW_1);
+	EMGD_DEBUG(" FIFO Watermark Control Register 1: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + FW_2);
+	EMGD_DEBUG(" FIFO Watermark Control Register 2: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + FW_3);
+	EMGD_DEBUG(" FIFO Watermark Control Register 3: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + FW_4);
+	EMGD_DEBUG(" FIFO Watermark Control Register 4: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + FW_5);
+	EMGD_DEBUG(" FIFO Watermark Control Register 5: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + FW_6);
+	EMGD_DEBUG(" FIFO Watermark Control Register 6: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(context->device_context.virt_mmadr + DSP_CHICKENBITS);
+	EMGD_DEBUG(" Display Chicken bits: 0x%lx", ulreg);
+	read_bunit_tnc(0x30, &b_reg);
+	EMGD_DEBUG(" Bunit Chicken bits: 0x%lx", b_reg);
+	read_bunit_tnc(0x02, &b_reg);
+	EMGD_DEBUG(" Bunit Write Flush: 0x%lx", b_reg);
+
+
+	/* FIXME: Not required for TNC.
+	 * The B-Spec states that rendering will be slower if the fences are not
+	 * a power of 2.  So for now, always use a power of 2. */
+	/* EMGD_WRITE32(0x04000400, context->device_context.virt_mmadr + 0x209c); */
+
+	EMGD_DEBUG(" Plane Control:  0x%lx", plane_control);
+	EMGD_DEBUG(" Plane Base:     0x%lx", fb_info->fb_base_offset);
+	EMGD_DEBUG(" Visible Offset: 0x%lx", fb_info->visible_offset);
+	EMGD_DEBUG(" Plane Pitch:    0x%lx", stride);
+
+	EMGD_WRITE32(plane_control, context->device_context.virt_mmadr + plane_reg);
+	EMGD_WRITE32(stride,
+		context->device_context.virt_mmadr + plane_reg + DSP_STRIDE_OFFSET);
+
+	EMGD_WRITE32(fb_info->visible_offset,
+		context->device_context.virt_mmadr + plane_reg + DSP_LINEAR_OFFSET);
+	EMGD_WRITE32(0, context->device_context.virt_mmadr + plane_reg + 0x24);
+	EMGD_WRITE32(fb_info->fb_base_offset,
+		context->device_context.virt_mmadr + plane_reg + DSP_START_OFFSET);
+
+	wait_for_vblank_tnc(pipe->pipe_reg);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+/*!
+ *
+ * @param emgd_encoder
+ * @param status
+ *
+ * @return program_port_lvds_gen4()
+ * @return program_port_sdvo_gen4()
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int kms_program_port_tnc(emgd_encoder_t *emgd_encoder,
+		unsigned long status)
+{
+	EMGD_TRACE_ENTER;
+
+	if (emgd_encoder->igd_port->port_type == IGD_PORT_LVDS) {
+		EMGD_TRACE_EXIT;
+		return kms_program_port_lvds_tnc(emgd_encoder, status);
+	} else {
+		EMGD_TRACE_EXIT;
+		return kms_program_port_sdvo_tnc(emgd_encoder, status);
+	}
+}
+
+
+/*!
+ *
+ * @param emgd_encoder
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+int kms_program_port_lvds_tnc(emgd_encoder_t *emgd_encoder,
+		unsigned long status)
+{
+	int ret = 0;
+	unsigned long pd_powerstate = PD_POWER_MODE_D3;
+	pd_timing_t        *timing    = NULL;
+	igd_display_port_t *port      = NULL;
+	struct drm_encoder *encoder   = NULL;
+	struct drm_device  *dev       = NULL;
+	igd_context_t      *context   = NULL;
+	struct drm_crtc    *crtc      = NULL;
+	emgd_crtc_t        *emgd_crtc = NULL;
+	igd_display_pipe_t *pipe      = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	encoder = &emgd_encoder->base;
+	port = emgd_encoder->igd_port;
+	dev = encoder->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	EMGD_DEBUG("Program LVDS: (%s)", status?"ENABLE":"DISABLE");
+
+	if (status == TRUE) {
+
+		if(!(port->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+
+		pd_powerstate = (context->device_context.power_state > port->power_state) ?
+			context->device_context.power_state : port->power_state;
+
+		if (pd_powerstate == IGD_POWERSTATE_D0) {
+			EMGD_DEBUG("Power State: D0");
+
+			list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+				if (crtc == encoder->crtc) {
+					emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+					pipe = emgd_crtc->igd_pipe;
+					break;
+				}
+			}
+
+			timing = (pd_timing_t *)pipe->timing;
+			/* Reach end timing to get user resolution and pass it to pd */
+			if(timing->extn_ptr) {
+				timing = (pd_timing_t *)timing->extn_ptr;
+			}
+			/* set mode will take care of port control */
+			ret = port->pd_driver->set_mode(
+					port->pd_context,
+					timing,
+					1<<pipe->pipe_num);
+		}
+	}
+
+	/* either status == FALSE, or status == TRUE, but powerstate is D1/D2/D3 */
+	if (pd_powerstate != IGD_POWERSTATE_D0) {
+		ret = port->pd_driver->set_power(
+			port->pd_context, PD_POWER_MODE_D3);
+	}
+
+	if (ret) {
+		EMGD_ERROR_EXIT("PD set_%s returned: 0x%x",
+			(pd_powerstate == IGD_POWERSTATE_D0)?"mode":"power", ret);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+/*!
+ *
+ * @param emgd_encoder
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int kms_program_port_sdvo_tnc(emgd_encoder_t *emgd_encoder,
+		unsigned long status)
+{
+	unsigned long port_control;
+	unsigned long pd_powerstate = PD_POWER_MODE_D3;
+	unsigned long upscale = 0;
+	igd_timing_info_t  local_timing;
+	igd_timing_info_t  *timing    = NULL;
+	igd_display_port_t *port      = NULL;
+	struct drm_encoder *encoder   = NULL;
+	struct drm_device  *dev       = NULL;
+	igd_context_t      *context   = NULL;
+	struct drm_crtc    *crtc      = NULL;
+	emgd_crtc_t        *emgd_crtc = NULL;
+	igd_display_pipe_t *pipe      = NULL;
+	unsigned long temp;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	encoder = &emgd_encoder->base;
+	port = emgd_encoder->igd_port;
+	dev = encoder->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	EMGD_DEBUG("Program Port: (%s)", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("pd_flags: 0x%lx", port->pd_flags);
+
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		if (crtc == encoder->crtc) {
+			emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+			pipe = emgd_crtc->igd_pipe;
+			break;
+		}
+	}
+
+	timing = pipe->timing;
+
+	port_control = READ_MMIO_REG_TNC(IGD_PORT_SDVO, port->port_reg);
+
+	if (status == TRUE) {
+		if (!(port->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+
+		/* Enable VGA syncs for native vga modes */
+		if (port->vga_sync == 1) {
+			EMGD_DEBUG("VGA sync true, is width x height 720 x 400?");
+			if((timing->width == 720) && (timing->height == 400)) {
+				EMGD_DEBUG("Modify port control and multi_port_control");
+				port_control |= (1L<<15);
+			}
+		}
+
+		/* Fact that both IGD_ powerstates and PD_ powermodes have
+		 * same definitions */
+		pd_powerstate =
+			(context->device_context.power_state > port->power_state) ?
+			context->device_context.power_state : port->power_state;
+
+		if (pd_powerstate == IGD_POWERSTATE_D0) {
+			EMGD_DEBUG("Power State: D0");
+			/* Upscale */
+			pi_pd_find_attr_and_value(port,
+				PD_ATTR_ID_PANEL_FIT,
+				0, /*no PD_FLAG for UPSCALING */
+				NULL, /* dont need the attr ptr*/
+				&upscale);
+
+			/* Reach the end timing if upscaling is enabled */
+			if (timing->extn_ptr && upscale) {
+				timing = (pd_timing_t *)timing->extn_ptr;
+			}
+
+			local_timing = *timing;
+			if (upscale) {
+				/* For timings smaller than width 360 and height 200,
+				 * double the size. This is because the active area of the mode
+				 * is double the size of the resolution for these modes
+				 *  - Very tricky huh */
+				if (local_timing.width <= 360) {
+					local_timing.width <<= 1;
+				}
+				if (local_timing.height <= 200) {
+					local_timing.height <<= 1;
+				}
+			}
+
+			/* BIT31 - Enable
+			 * BIT30 - PIPE B
+			 * BIT29 - Stall
+			 * BIT7  - Border
+			 */
+			port_control |= BIT31|BIT30|BIT29|BIT7;
+
+			/* Program cDVO registers:
+			 * Keep default values for
+			 *     7000h - cDVO control register
+			 *     7004h - cDVO slew rate register
+			 *     7008h - cDVO strength register
+			 *     700Ch - cDVO RCOMP update register
+			 *     6102Ch - cDVO stall register = 0xA.
+			 * Note: Though EAS says 6102Ch default value is 6, it is a typo
+			 *     in the spec, based on Si DE hw default value is 10 (0xA),
+			 *     so no need to program explicitly. This saves few bytes for
+			 *     micro.
+			 */
+
+			/* Enable Current Source */
+			temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG);
+			temp |= 0x2000;
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG, temp);
+		}
+	}
+
+	if (pd_powerstate == PD_POWER_MODE_D0) {
+		ret = port->pd_driver->set_mode(port->pd_context, &local_timing, 0);
+	} else {
+		ret = port->pd_driver->set_power(port->pd_context, pd_powerstate);
+	}
+
+	if (ret) {
+		EMGD_ERROR_EXIT("PD set_%s returned: 0x%x",
+			(pd_powerstate == PD_POWER_MODE_D0)?"mode":"power", ret);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_DEBUG("Port_control: 0x%lx", port_control);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, port->port_reg, port_control);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*!
+ * @param emgd_encoder
+ * @param status
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int kms_post_program_port_tnc(emgd_encoder_t * emgd_encoder,
+	unsigned long status)
+{
+	int ret;
+	struct drm_device  *dev       = NULL;
+	igd_context_t      *context   = NULL;
+	igd_display_port_t *port      = NULL;
+	igd_display_pipe_t *pipe      = NULL;
+	struct drm_encoder *encoder   = NULL;
+	struct drm_crtc    *crtc      = NULL;
+	emgd_crtc_t        *emgd_crtc = NULL;
+	igd_timing_info_t  *timings   = NULL;
+	/* unsigned long portreg; */
+
+	EMGD_TRACE_ENTER;
+
+	encoder = &emgd_encoder->base;
+	dev = encoder->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	port = emgd_encoder->igd_port;
+
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		if (crtc == encoder->crtc) {
+			emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+			pipe = emgd_crtc->igd_pipe;
+			timings = pipe->timing;
+			break;
+		}
+	}
+
+	/*
+	 * The programming found in the common code for all chipsets
+	 * has the device programming sequence as follows:
+	 *  Port
+	 *  Pipe
+	 *  Post Port
+	 *  Plane
+	 * On Gen4, if the port is enabled before the pipe, there is a 10%
+	 * chance that the port will not turn on properly.
+	 * Due to compatability requires with other chipsets, this workaround
+	 * fixes this issue
+	 *
+	 * This is handled by set_power.  This also only seems to apply to a
+	 * chipset that is no longer supported.  Not sure if this will get
+	 * re-introduced with ValleyView and beyond, so I left it here.
+	 *
+	 * portreg = READ_MMIO_REG_TNC(port->port_type, port->port_reg);
+	 * WRITE_MMIO_REG_TNC(port->port_type, port->port_reg, portreg & ~BIT31);
+	 * WRITE_MMIO_REG_TNC(port->port_type, port->port_reg, portreg);
+	 */
+
+	if (port->port_type == IGD_PORT_SDVO) {
+		initialize_ST_bridge(context, port);
+	}
+
+	ret = 0;
+	/* call post_set_mode() if exists */
+	if (port->pd_driver->post_set_mode) {
+		if (port->port_type == IGD_PORT_SDVO){
+			ret = port->pd_driver->post_set_mode(port->pd_context, timings,
+				status); /*Needed for OKI*/
+		} else {
+			ret = port->pd_driver->post_set_mode(port->pd_context, timings,
+				1<<pipe->pipe_num);
+		}
+		if (ret) {
+			EMGD_ERROR_EXIT("PD post_set_mode returned: 0x%x", ret);
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+/*!
+ * kms_get_vblank_counter_tnc
+ *
+ * This function returns the vblank counter number back to the caller.
+ *
+ * @param emgd_crtc [IN] The pipe to get frame value from
+ *
+ * @return 0 frame number, which can also be used as a vblank counter number
+ */
+static u32 kms_get_vblank_counter_tnc(emgd_crtc_t *emgd_crtc)
+{
+	unsigned long      high1, high2, low;
+	struct drm_device *dev;
+	unsigned long      frame_high_reg, frame_low_reg;
+	igd_context_t     *context;
+
+
+	dev     = ((struct drm_crtc *)(&emgd_crtc->base))->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+
+	switch (emgd_crtc->igd_pipe->pipe_features & IGD_PORT_MASK) {
+		case IGD_PORT_SHARE_DIGITAL:
+			frame_high_reg = PIPEB_FRAME_HIGH;
+			frame_low_reg  = PIPEB_FRAME_PIXEL;
+			break;
+
+		case IGD_PORT_SHARE_LVDS:
+		default:
+			frame_high_reg = PIPEA_FRAME_HIGH;
+			frame_low_reg  = PIPEA_FRAME_PIXEL;
+			break;
+	}
+
+
+	/*
+	 * High and low register fields are not synchronized so it is possible
+	 * that our low value is actually not from the same high value, e.g.
+	 * going from "99" to "00" when transitioning 499 to 500.  To get
+	 * around this, we will read until the two reads of high values stay
+	 * the same.
+	 */
+	do {
+		high1 = EMGD_READ32(context->device_context.virt_mmadr +
+					frame_high_reg);
+		high1 &= PIPE_FRAME_HIGH_MASK;
+
+		low   = EMGD_READ32(context->device_context.virt_mmadr +
+					frame_low_reg);
+		low   &= PIPE_FRAME_LOW_MASK;
+
+		high2 = EMGD_READ32(context->device_context.virt_mmadr +
+					frame_high_reg);
+		high2 &= PIPE_FRAME_HIGH_MASK;
+	} while (high1 != high2);
+
+
+	/* Frame count low is located at bits 24-31 */
+	low >>= PIPE_FRAME_LOW_SHIFT;
+
+	/* Low value is 8 bits long, so shift high by 8 bits */
+	return (high1 << 8) | low;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/tnc/micro_mode_tnc.c b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/micro_mode_tnc.c
new file mode 100644
index 0000000..f96b710
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/micro_mode_tnc.c
@@ -0,0 +1,2646 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_mode_tnc.c
+ * $Revision: 1.47 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  1) Atom E6xx Core implementations for the mode dispatch functions.
+ *  2) 0:2:0 = Device2 = LNC VGA
+ *  0:3:0 = Device3 = Atom E6xx overlay
+ *  3) For MMIO access:
+ *  Dev2 only: Use EMGD_READ32(), EMGD_WRITE32(): less code
+ *  Dev2/3: Use READ_MMIO_REG_TNC(), WRITE_MMIO_REG_TNC(): more code
+ *  For code optimization sake use the right macro.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <io.h>
+
+#include <igd.h>
+#include <igd_pwr.h>
+
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+#include <dsp.h>
+#include <vga.h>
+#include <intelpci.h>
+#include <math_fix.h>
+#include <mode_access.h>
+#include <rb.h>
+#include <pi.h>
+#include <dispatch.h>
+
+#include <tnc/regs.h>
+#include <tnc/context.h>
+#include <tnc/mi.h>
+#include <tnc/instr.h>
+#include <tnc/igd_tnc_wa.h>
+#include "drm_emgd_private.h"
+
+#include "../cmn/match.h"
+#include "../cmn/mode_dispatch.h"
+#include "mode_tnc.h"
+
+/*
+	Turning on FIB part workaround for all IALs, for vBIOS this will limit
+    the port drivers to one at a time (SDVO or LVDS)due to code size
+ */
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*
+ * Exports from the other components of this module.
+ */
+
+#ifdef CONFIG_TNC
+
+extern int program_clock_tnc(igd_display_context_t *display,
+	igd_clock_t *clock, unsigned long dclk);
+
+int program_cdvo_tnc(void);
+
+extern unsigned long get_port_type(int crtc_id);
+
+extern mode_full_dispatch_t mode_full_dispatch_tnc;
+
+int wait_for_vblank_tnc(unsigned long pipe_reg);
+void wait_pipe(unsigned long pt, unsigned long pipe_reg, unsigned long check_on_off);
+
+void shutdown_ST_bridge(igd_context_t *context);
+void initialize_ST_bridge(igd_context_t *context, igd_display_port_t *port);
+
+#if 0
+static void cdvo_signal(void);
+#endif
+
+static unsigned long flag_clip_fix = IGD_CLIP_FIX_GLOBAL_ENABLE |
+	IGD_CLIP_FIX_DISABLE_THROTTLE;
+
+/* Extern defines for Device2, device3 and device31 iobases.
+ * For Atom E6xx all devices are always io_mapped. */
+extern unsigned char io_mapped;
+extern unsigned short io_base;
+extern unsigned char io_mapped_lvds;
+extern unsigned short io_base_lvds;
+extern unsigned char io_mapped_sdvo;
+extern unsigned short io_base_sdvo;
+extern unsigned char io_mapped_lpc;
+extern unsigned short io_base_lpc;
+extern unsigned char io_mapped_sdvo_st;
+extern unsigned short io_base_sdvo_st;
+extern unsigned char io_mapped_sdvo_st_gpio;
+extern unsigned short io_base_sdvo_st_gpio;
+
+#ifdef DEBUG_BUILD_TYPE
+/* Debug configuration flag for B0 workaround. Workaround is turned on by default. */
+int flag_basic_htotal_formula_wa = 1;    /* Enable formula workaround flag */
+int flag_turn_off_port_wa = 1;           /* Turn off port when sweeping */
+int flag_enable_tuning_wa = 1;           /* Disable sweeping flag */
+#endif
+
+/* Do not change the order */
+const unsigned long ports_tnc[2] = {IGD_PORT_LVDS, IGD_PORT_SDVO};
+
+tnc_wa_timing_t tune = {0, 0, 0, 0, 0, 0, 0,};
+
+/* Temporary frame buffer */
+static igd_framebuffer_info_t fb_info_tmp = {0,0,0,0,0,0,0};
+
+/* VPhase variable */
+int vphase = 0;	/* The current value to enable  vphase is 5 */
+
+mode_data_tnc_t device_data_tnc[1] = {
+	{
+		0x000b0000, /* plane a preservation */
+		0x00000000, /* plane b c preservation */
+		0x60000000, /* pipe preservation */
+		0x00003232, /* DSP FIFO Size A=50 B=50 C=28 May require fine tuning*/
+		0x3f8f0404, /* FIFO watermark control1 */
+		0x04040f04, /* FIFO watermark control2 */
+		0x00000000, /* FIFO watermark control3 */
+		0x04040404, /* FIFO watermark1 control4 */
+		0x04040404, /* FIFO watermark1 control5 */
+		0x00000078, /* FIFO watermark1 control6 */
+	}
+};
+
+pixel_crc_t compute_pixel_crc( pixel_crc_t  pixel, pixel_crc_t  new_pixel)
+{
+	pixel_crc_t  pixel_crc_new;
+
+    pixel_crc_new.pixel = 0x0;
+
+    pixel_crc_new.bit.bit22 = pixel.bit.bit14;
+    pixel_crc_new.bit.bit21 = pixel.bit.bit13;
+    pixel_crc_new.bit.bit20 = pixel.bit.bit12;
+    pixel_crc_new.bit.bit19 = pixel.bit.bit11;
+    pixel_crc_new.bit.bit18 = pixel.bit.bit10;
+    pixel_crc_new.bit.bit17 = pixel.bit.bit9;
+    pixel_crc_new.bit.bit16 = pixel.bit.bit22 ^ pixel.bit.bit8;
+    pixel_crc_new.bit.bit15 = pixel.bit.bit21 ^ pixel.bit.bit7;
+    pixel_crc_new.bit.bit14 = pixel.bit.bit20 ^ pixel.bit.bit6;
+    pixel_crc_new.bit.bit13 = pixel.bit.bit19 ^ pixel.bit.bit5;
+    pixel_crc_new.bit.bit12 = pixel.bit.bit18 ^ pixel.bit.bit4;
+    pixel_crc_new.bit.bit11 = pixel.bit.bit17 ^ pixel.bit.bit3;
+    pixel_crc_new.bit.bit10 = pixel.bit.bit16 ^ pixel.bit.bit2;
+    pixel_crc_new.bit.bit9  = pixel.bit.bit15 ^ pixel.bit.bit1;
+    pixel_crc_new.bit.bit8  = pixel.bit.bit0;
+    pixel_crc_new.bit.bit7  = pixel.bit.bit22 ^ new_pixel.bit.bit0;
+    pixel_crc_new.bit.bit6  = pixel.bit.bit21 ^ new_pixel.bit.bit1;
+    pixel_crc_new.bit.bit5  = pixel.bit.bit20 ^ new_pixel.bit.bit2;
+    pixel_crc_new.bit.bit4  = pixel.bit.bit19 ^ new_pixel.bit.bit3;
+    pixel_crc_new.bit.bit3  = pixel.bit.bit18 ^ new_pixel.bit.bit4;
+    pixel_crc_new.bit.bit2  = pixel.bit.bit17 ^ new_pixel.bit.bit5;
+    pixel_crc_new.bit.bit1  = pixel.bit.bit16 ^ new_pixel.bit.bit6;
+    pixel_crc_new.bit.bit0  = pixel.bit.bit15 ^ new_pixel.bit.bit7;
+
+	return (pixel_crc_new);
+}
+
+#ifndef CONFIG_MICRO
+int check_display_tnc(igd_display_context_t *display,
+					  unsigned short port_number,unsigned long status)
+{
+	pixel_crc_t red_val, green_val, blue_val;
+	pixel_crc_t blue_crc, green_crc, red_crc;
+	int i, j, bps = 4, ret_val = 0;
+	unsigned long *curr_pixel, sync = 0, size = 0, dc;
+	unsigned long tnc_crc_red, tnc_crc_green, tnc_crc_blue, temp;
+	unsigned long temp_plane, temp_pitch, temp_control, plane_control;
+	unsigned long pt = PORT_TYPE(display);
+	igd_framebuffer_info_t *fb_info = PLANE(display)->fb_info;
+	igd_timing_info_t pTimings_tmp, *pTimings_ext,*pTimings = PIPE(display)->timing;
+	/* igd_display_port_t *port = PORT_OWNER(display); */
+	platform_context_tnc_t *platform_context = display->context->platform_context;
+	igd_rect_t dst_rect;
+	igd_surface_t front_buffer;
+	tnc_wa_timing_t *wa = WA_TUNE;
+	os_alarm_t timeout;
+
+	EMGD_TRACE_ENTER;
+
+	dc = *(display->context->mod_dispatch.dsp_current_dc);
+	pTimings_tmp = *pTimings;
+	pTimings_ext = pTimings;
+
+	/* Check to see if VGA plane was requested */
+	while (pTimings_ext->extn_ptr) {
+		pTimings_ext = (igd_timing_info_t *)pTimings_ext->extn_ptr;
+	}
+	if((pt != IGD_PORT_SDVO) || (display->context->device_context.rid != TNC_B0_RID)
+		|| (platform_context->stbridgedev)
+		|| (pTimings->reserved_dd & TNC_HTOTAL_TUNED) || (!FLAG(flag_enable_tuning_wa))
+		|| (mode_context->tuning_wa == 0) /*if 0 don't tune*/
+		|| wa->counter > LIMIT_TOTAL_CHECK_DISPLAY
+		|| (MODE_IS_VGA(pTimings_ext))
+		|| (!IGD_DC_CLONE(dc) && !IGD_DC_EXTENDED(dc))
+		|| (platform_context->tnc_dev3_rid != TNC_B0_DEV3_RID)){
+		/* Workaround is specific for SDVO display
+		 * Dont tuned if it isnt B0 silicon
+		 * Dont tuned if it is already tuned
+		 * Dont tuned if bypass is turned on (debug)
+		 * No tuning for VGA modes
+		 * Stop Tuning if above limit LIMIT_TOTAL_CHECK_DISPLAY
+		 * Dont tuned if B1 silicon (based on SDVO Device 3 RID)
+		 */
+
+		if(pt != IGD_PORT_SDVO ||
+			(MODE_IS_VGA(pTimings_ext))){
+			EMGD_DEBUG("Unsupported tuning");
+		}else if((wa->counter > LIMIT_TOTAL_CHECK_DISPLAY) &&
+			!(pTimings->reserved_dd & TNC_HTOTAL_TUNED)){
+			pTimings->reserved_dd = 0;
+			EMGD_ERROR("Unable to get tuned value!");
+		}
+
+		if(FLAG(flag_turn_off_port_wa)){
+			/* Turn on SDVO source */
+			temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG);
+			temp |= BIT13;
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG, temp);
+		}
+
+		if(fb_info_tmp.width){
+			/* Free up the temporary frame buffer */
+			display->context->dispatch.gmm_free(fb_info_tmp.fb_base_offset);
+			OS_MEMSET(&fb_info_tmp,0,sizeof (igd_framebuffer_info_t));
+		}
+		OS_MEMSET(wa, 0, sizeof(tnc_wa_timing_t));
+		EMGD_TRACE_EXIT;
+		return TRUE;
+	}
+
+	/* Increment checking counter */
+	wa->counter++;
+
+	/* CRC starting value */
+	blue_crc.pixel = 0x007FFFFF;
+	green_crc.pixel = 0x007FFFFF;
+	red_crc.pixel = 0x007FFFFF;
+
+	if(FLAG(flag_turn_off_port_wa)){
+		/* Turn of port source.
+		 * */
+		temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG);
+		temp &= ~BIT13;
+		WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG, temp);
+	}
+
+	/* Allocate temporary 32 bpp frame buffer for CRC calculation.
+	 * SV only provided codes for 32bpp CRC calculation so we would
+	 * only calculate CRC for 32bpp. Once CRC is calculated EMGD
+	 * would revert back to original plane. A temporary place is also
+	 * neede so no corruption is seen on lvds for clone display
+	 */
+	if(fb_info_tmp.width != (unsigned short)pTimings_tmp.width &&
+		fb_info_tmp.height != (unsigned short)pTimings_tmp.height){
+		if(fb_info_tmp.width){
+			EMGD_ERROR("Memory leak! Previous framebuffer is not freed!");
+		}
+		fb_info_tmp.width  = pTimings_tmp.width;
+		fb_info_tmp.height = pTimings_tmp.height;
+		fb_info_tmp.screen_pitch  = fb_info->screen_pitch;
+		fb_info_tmp.fb_base_offset = 0;
+		fb_info_tmp.pixel_format = IGD_PF_xRGB32;
+		fb_info_tmp.flags = IGD_SURFACE_DISPLAY | IGD_SURFACE_RENDER;
+
+		ret_val = display->context->dispatch.gmm_alloc_surface(
+				&fb_info_tmp.fb_base_offset,
+				fb_info_tmp.pixel_format,
+				&fb_info_tmp.width,
+				&fb_info_tmp.height,
+				&fb_info_tmp.screen_pitch,
+				&size,
+				IGD_GMM_ALLOC_TYPE_NORMAL,
+				&fb_info_tmp.flags);
+	}
+	/* Temp plane is always 32 bpp */
+	temp_control = EMGD_READ32(MMIO(display)
+		+ PLANE(display)->plane_reg);
+	plane_control = temp_control;
+	plane_control &= ~(BIT30 | BIT29 | BIT28 | BIT27);
+	plane_control |= BIT28 | BIT27;
+	EMGD_WRITE32(plane_control, MMIO(display)
+		+ PLANE(display)->plane_reg);
+	temp_pitch = EMGD_READ32(MMIO(display)
+		+ PLANE(display)->plane_reg + DSP_STRIDE_OFFSET);
+	EMGD_WRITE32(fb_info_tmp.screen_pitch, MMIO(display)
+		+ PLANE(display)->plane_reg + DSP_STRIDE_OFFSET);
+	temp_plane = EMGD_READ32(MMIO(display)
+		+ PLANE(display)->plane_reg + DSP_START_OFFSET);
+	EMGD_WRITE32(fb_info_tmp.fb_base_offset,
+		MMIO(display) + PLANE(display)->plane_reg + DSP_START_OFFSET);
+	/* Setup front buffer */
+	OS_MEMSET(&front_buffer,0,sizeof (igd_surface_t));
+	front_buffer.width  = fb_info_tmp.width;
+	front_buffer.height = fb_info_tmp.height;
+	front_buffer.pitch  = fb_info_tmp.screen_pitch;
+	front_buffer.offset = fb_info_tmp.fb_base_offset;
+	front_buffer.pixel_format = fb_info_tmp.pixel_format;
+	front_buffer.flags = fb_info_tmp.flags;
+	/* Draw 2 color bars for CRC calculation. This is simple and sufficient
+	 * to detect clipping. Will consider complicated color bar if an issue
+	 * is found for now go for quickest methodColor bar. Red and Blue */
+	dst_rect.x1 = dst_rect.y1 = 0;
+	dst_rect.x2 = fb_info_tmp.width/2;
+	dst_rect.y2 = fb_info_tmp.height;
+
+	/*
+	// NOT supported in koheo, using software for now
+	display->context->dispatch.color_blt((igd_display_h)display,
+				IGD_PRIORITY_NORMAL,
+				&front_buffer,
+				&dst_rect,
+				IGD_ALPHA_CHANNEL | IGD_RGB_CHANNEL,
+				0x00FF0000,
+				0xF0,
+				(igd_appcontext_h)0, IGD_RENDER_BLOCK);
+	*/
+
+	{
+		unsigned long row, col;
+		unsigned char *buf = NULL;
+		unsigned long height = fb_info_tmp.height;
+		unsigned long width = fb_info_tmp.width/2;
+		unsigned long pitch = fb_info_tmp.screen_pitch;
+
+		buf = display->context->dispatch.gmm_map(fb_info_tmp.fb_base_offset);
+		for (row = 0; row < (height); row++) {
+			for (col = 0; col < (width); col++) {
+				EMGD_WRITE32(0x00FF0000, (buf + (col*4)));
+			}
+			buf = buf + pitch;
+		}
+		display->context->dispatch.gmm_unmap(buf);
+	}
+
+	dst_rect.x1 = fb_info_tmp.width/2;
+	dst_rect.y1 = 0;
+	dst_rect.x2 = fb_info_tmp.width;
+	dst_rect.y2 = fb_info_tmp.height;
+
+	/*
+	// NOT supported in koheo, using software for now
+	display->context->dispatch.color_blt((igd_display_h)display,
+				IGD_PRIORITY_NORMAL,
+				&front_buffer,
+				&dst_rect,
+				IGD_ALPHA_CHANNEL | IGD_RGB_CHANNEL,
+				0x000000FF,
+				0xF0,
+				(igd_appcontext_h)0, IGD_RENDER_BLOCK);
+		wa->flag |= TNC_HTOTAL_TUNED;
+		pTimings->reserved_dd = (wa->htotal | TNC_HTOTAL_TUNED);
+		return FALSE;
+
+	display->context->dispatch.sync((igd_display_h)display, IGD_PRIORITY_NORMAL,
+		&sync,
+		IGD_SYNC_NONBLOCK);
+	*/
+
+	{
+		unsigned long row, col;
+		unsigned char *buf = NULL;
+		unsigned long height = fb_info_tmp.height;
+		unsigned long width = fb_info_tmp.width;
+		unsigned long pitch = fb_info_tmp.screen_pitch;
+
+		buf = display->context->dispatch.gmm_map(fb_info_tmp.fb_base_offset);
+		for (row = 0; row < (height); row++) {
+			for (col = fb_info_tmp.width/2; col < (width); col++) {
+				EMGD_WRITE32(0x000000FF, (buf + (col*4)));
+			}
+			buf = buf + pitch;
+		}
+		display->context->dispatch.gmm_unmap(buf);
+	}
+
+
+	ret_val = -IGD_ERROR_BUSY;
+	timeout = OS_SET_ALARM(500);
+	while (-IGD_ERROR_BUSY == ret_val && (!OS_TEST_ALARM(timeout))){
+		ret_val = display->context->dispatch.sync(
+			(igd_display_h)display,
+			IGD_PRIORITY_NORMAL,
+			&sync,
+			IGD_SYNC_BLOCK);
+		OS_SCHEDULE();
+		if (0 == ret_val) {
+			break;
+		}
+	}
+
+	/* Turn on HW CRC */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_CRC_CTRL_REG, 0x80000000);
+	/* Reset Status Bit */
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LVDS, 0x71024);
+	temp |= 0x00001000;
+	WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, 0x71024, temp);
+	temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x71024);
+	temp |= 0x00001000;
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, 0x71024, temp);
+
+	/* Calculate CRC pixel by reading every single pixel.
+	 * Pixel that is more than the timing range is ignore which happens
+	 * during panning. To save time CRC is only calculated once per timing
+	 * change and kept inside a scratch global variable wa.
+	 */
+	if(wa->crc_red == 0 || wa->crc_green == 0 ||
+		wa->crc_blue == 0 ){
+		unsigned char *fb_adr;
+
+		fb_adr = display->context->dispatch.gmm_map(fb_info_tmp.fb_base_offset);
+
+		/* Calculate CRC for this timing */
+		for(i=0; i<(int)pTimings_tmp.height; i++){
+			for(j=0; j<(int)pTimings_tmp.width; j++){
+				if(i < (int)fb_info_tmp.height && j < (int)fb_info_tmp.width){
+					/* virt_fb_adr not available anymore	*/
+					/*
+					curr_pixel = (unsigned long*)(
+						mode_context->context->device_context.virt_fb_adr +
+						fb_info_tmp.fb_base_offset + j*bps + (i*fb_info_tmp.screen_pitch));
+					*/
+
+					curr_pixel = (unsigned long*)(fb_adr + (j*bps) + (i*fb_info_tmp.screen_pitch));
+
+					if(curr_pixel == NULL){
+						EMGD_DEBUG("FB not valid return true for now util FB is allocated");
+						return TRUE;
+					}
+
+
+
+					/*
+					blue_val.pixel =  (0x0000ff & *curr_pixel) >> 0;
+					green_val.pixel = (0x00ff00 & *curr_pixel) >> 8;
+					red_val.pixel = (0xff0000 & *curr_pixel) >> 16;
+					*/
+
+					blue_val.pixel =  (0x0000ff & EMGD_READ32(curr_pixel)) >> 0;
+					green_val.pixel = (0x00ff00 & EMGD_READ32(curr_pixel)) >> 8;
+					red_val.pixel = (0xff0000 & EMGD_READ32(curr_pixel)) >> 16;
+
+				}else{
+					red_val.pixel = green_val.pixel = blue_val.pixel = 0;
+				}
+
+				blue_crc = compute_pixel_crc(blue_crc, blue_val);
+				green_crc = compute_pixel_crc(green_crc, green_val);
+				red_crc = compute_pixel_crc(red_crc, red_val);
+			}
+		}
+		display->context->dispatch.gmm_unmap(fb_adr);
+
+		/* Save calculated value */
+		wa->crc_red = red_crc.pixel;
+		wa->crc_green = green_crc.pixel;
+		wa->crc_blue = blue_crc.pixel;
+	}else{
+		/* Reuse CRC value calculated before */
+		red_crc.pixel = wa->crc_red;
+		green_crc.pixel = wa->crc_green;
+		blue_crc.pixel = wa->crc_blue;
+	}
+
+	timeout = OS_SET_ALARM(70);
+	do {
+		temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, 0x71024);
+		if(temp & 0x00002000){
+			break;
+		}
+		OS_SCHEDULE();
+	} while ((!OS_TEST_ALARM(timeout)));
+	/* Give some time for CRC to be stable */
+	OS_SLEEP(500);
+	/* Read CRC on LNC and OVL */
+	tnc_crc_red = READ_MMIO_REG_TNC(IGD_PORT_SDVO,0x61060);
+	tnc_crc_green = READ_MMIO_REG_TNC(IGD_PORT_SDVO,0x61064);
+	tnc_crc_blue = READ_MMIO_REG_TNC(IGD_PORT_SDVO,0x61068);
+
+	/* Revert back to original plane */
+	EMGD_WRITE32(temp_control, MMIO(display)
+		+ PLANE(display)->plane_reg);
+	EMGD_WRITE32(temp_pitch, MMIO(display)
+		+ PLANE(display)->plane_reg + DSP_STRIDE_OFFSET);
+	EMGD_WRITE32(temp_plane, MMIO(display)
+		+ PLANE(display)->plane_reg + DSP_START_OFFSET);
+
+	/* Turn off CRC */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_CRC_CTRL_REG, 0x00000000);
+	/* Compare CRC calculation with Atom E6xx CRC, return false if a mismatch and
+	 * tuning continues. If it matches indicate the flag as tuned and return
+	 * false anyway so pipe programming would program based on the correct
+	 * timing spec.
+	 */
+	if((red_crc.pixel != (unsigned int)tnc_crc_red) ||
+		(green_crc.pixel != (unsigned int)tnc_crc_green) ||
+		(blue_crc.pixel != (unsigned int)tnc_crc_blue)){
+
+			/* Step by step tuning */
+		wa->delta = 1;
+
+		/* Calculate the delta value */
+		wa->htotal = (wa->htotal + wa->delta);
+
+		EMGD_DEBUG("Counter %d",wa->counter);
+		EMGD_DEBUG("Red CRC: %d Red TNC: %lu",red_crc.pixel,tnc_crc_red);
+		EMGD_DEBUG("Grn CRC: %d Grn TNC: %lu",green_crc.pixel,tnc_crc_green);
+		EMGD_DEBUG("Blu CRC: %d Blu TNC: %lu",blue_crc.pixel,tnc_crc_blue);
+		EMGD_DEBUG("CRC does not match, tuning....");
+		EMGD_TRACE_EXIT;
+		return FALSE;
+	}
+	EMGD_ERROR("Tuned value found.");
+	EMGD_ERROR("Total tuning required %d",wa->counter);
+	EMGD_DEBUG("Red CRC: %d Red TNC: %lu",red_crc.pixel,tnc_crc_red);
+	EMGD_DEBUG("Grn CRC: %d Grn TNC: %lu",green_crc.pixel,tnc_crc_green);
+	EMGD_DEBUG("Blu CRC: %d Blu TNC: %lu",blue_crc.pixel,tnc_crc_blue);
+	wa->flag |= TNC_HTOTAL_TUNED;
+	pTimings->reserved_dd = (wa->htotal | TNC_HTOTAL_TUNED);
+
+	EMGD_TRACE_EXIT;
+	/* We are tuned but lets do another sequence since we may have changed
+	 * the blanks to get a valid CRC. This happens on certain modes that has
+	 * weird blanks value like VESA's 640x480@60
+	 */
+	return FALSE;
+}
+#endif
+
+/*!
+ *
+ * @param mmio dev2 mmio
+ *
+ * @return void
+ */
+void disable_vga_tnc (unsigned char *mmio)
+{
+	unsigned long temp;
+	unsigned char sr01;
+
+	EMGD_TRACE_ENTER;
+
+	/* Disable VGA plane if it is enabled. */
+	temp = EMGD_READ32(EMGD_MMIO(mmio) + VGACNTRL);
+	if ((temp & BIT31) == 0) {
+		/* Read SR01 */
+		READ_VGA(mmio, SR_PORT, SR01, sr01);
+
+		/* Turn on SR01 bit 5 */
+		WRITE_VGA(mmio, SR_PORT, SR01, sr01|BIT(5));
+
+		/* Wait for 30us */
+		OS_SLEEP(30);
+
+		temp |= BIT31;     /* set bit 31 to disable */
+		temp &= ~BIT30;    /* clear bit 30 to get VGA display in normal size */
+		EMGD_WRITE32(temp, EMGD_MMIO(mmio) + VGACNTRL);
+
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param palette_entry
+ * @param palette_color
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+int igd_set_palette_entry_tnc(
+	igd_display_h display_handle,
+	unsigned long palette_entry,
+	unsigned long palette_color)
+{
+	/* Return if Pipe is not on */
+	if(!((1L<<31) & READ_MMIO_REG_TNC(PORT_TYPE_DH(display_handle),
+			PIPE(display_handle)->pipe_reg))) {
+		return -IGD_INVAL;
+	}
+	/* Palette can is only on 0:2:0 so use _TNC IGD_PORT_LVDS */
+	WRITE_MMIO_REG_TNC(IGD_PORT_LVDS,
+		PIPE(display_handle)->palette_reg + palette_entry * 4,
+		palette_color);
+
+	return 0;
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param palette_entry
+ * @param palette_color
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+int igd_get_palette_entry_tnc(
+	igd_display_h display_handle,
+	unsigned long palette_entry,
+	unsigned long *palette_color)
+{
+	/* Return if Pipe is not on */
+	if(!((1L<<31) & READ_MMIO_REG_TNC(
+			PORT_TYPE_DH(display_handle),
+			PIPE(display_handle)->pipe_reg))) {
+		return -IGD_INVAL;
+	}
+	/* Palette can is only on 0:2:0 so use _TNC IGD_PORT_LVDS */
+	*palette_color = 0xffffff & READ_MMIO_REG_TNC(
+		IGD_PORT_LVDS,
+		PIPE(display_handle)->palette_reg + palette_entry * 4);
+	return 0;
+}
+
+/*!
+ *
+ * @param mmio
+ * @param pipe_reg
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int wait_for_vblank_tnc(unsigned long pipe_reg)
+{
+	unsigned long pipe_status_reg = pipe_reg + PIPE_STATUS_OFFSET;
+	unsigned long tmp;
+	unsigned long port_type = IGD_PORT_LVDS;
+	os_alarm_t timeout;
+	int ret;
+	unsigned long request_for;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Parameter: pipe_reg = %lx", pipe_reg);
+
+	/* If pipe is off then just return */
+	if(!((1L<<31) & READ_MMIO_REG_TNC(port_type, pipe_reg))) {
+		EMGD_DEBUG("Pipe disabled/Off");
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	/*
+	 * When VGA plane is on the normal wait for vblank won't work
+	 * so just skip it. VGA plane is on 0:2:0 so no need to use _TNC macros.
+	 */
+	if(!(EMGD_READ32(MMIO_TNC(port_type) + 0x71400) & 0x80000000)) {
+		EMGD_DEBUG("VGA Plane On");
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	/* 1. Request the interrupt handler to record the next VBlank: */
+	request_for = VBINT_REQUEST(VBINT_WAIT,
+		(pipe_status_reg == PIPEA_STAT) ? VBINT_PORT4 : VBINT_PORT2);
+	mode_context->dispatch->full->request_vblanks(request_for,
+		MMIO_TNC(port_type));
+
+	/* 2. Wait (about 50 msec, 20Hz) & poll for the next VBlank: */
+	timeout = OS_SET_ALARM(50);
+	do {
+		OS_SCHEDULE();
+		tmp = mode_context->dispatch->full->vblank_occured(request_for);
+	} while ((tmp == 0x00) && (!OS_TEST_ALARM(timeout)));
+
+
+	if (tmp == 0) {
+		EMGD_ERROR_EXIT("Timeout waiting for VBLANK");
+		ret = 0;
+	} else {
+		ret = 1;
+	}
+
+	/* 3. End our request for the next VBlank: */
+	mode_context->dispatch->full->end_request(request_for,
+		MMIO_TNC(port_type));
+
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* wait_for_vblank_tnc */
+
+
+
+/*!
+ * This procedure waits for the next vertical blanking (vertical retrace)
+ * period. If the display is already in a vertical blanking period, this
+ * procedure exits.
+ *
+ * Note: A timeout is included to prevent an endless loop.
+ *
+ * @param display_handle
+ *
+ * @return FALSE if timed out
+ */
+int igd_wait_vblank_tnc(igd_display_h display_handle)
+{
+
+	return wait_for_vblank_tnc(PIPE(display_handle)->pipe_reg);
+
+}
+
+
+/*!
+ * Get the stride and stereo values based on the display.  This is also used
+ * by the MI instructions.
+ *
+ * @param display Pointer to hardware device instance data
+ * @param flags Should the stereo be for the frontbuffer or backbuffer?
+ *
+ * @return stride - Stride of the display
+ * @return stereo - Stereo address of the display
+ */
+int mode_get_stride_stereo_tnc(igd_display_context_t *display,
+	unsigned long *stride,
+	unsigned long *stereo,
+	unsigned long flags)
+{
+	unsigned long pitch = PLANE(display)->fb_info->screen_pitch;
+	igd_timing_info_t *timing = PIPE(display)->timing;
+	unsigned long base_offset;
+
+	EMGD_TRACE_ENTER;
+
+	base_offset = PLANE(display)->fb_info->visible_offset;
+	*stride = pitch;
+	*stereo = 0;
+
+	/* For field replication, valid for interlaced modes only
+	 *     set stereo = fb_base
+	 *         stride = pitch
+	 */
+	if (timing->mode_info_flags & IGD_SCAN_INTERLACE) {
+
+		if(timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* Interlaced + Line double flags means field replication.
+			 * same lines are sent for both fields. Program the
+			 * second eye to be same as the first
+			 */
+			*stereo = base_offset;
+		} else {
+			/* Regular interlaced. Second eye starts on line 2.
+			 * Skip every other line.
+			 */
+			*stereo = base_offset + pitch;
+			*stride = pitch * 2;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*
+ * Atom E6xx LVDS display: Mode switch sequence
+ *
+ * 1. Enable sequence
+ *
+ * Program power on delay, power off delay, power cycle delay registers
+ * Program backlight control register to set appropriate backlight value
+ * Pipe A must be completely off at this point
+ * Write PIPEACONF bits[19:18] = 00
+ * Write DSPACNTR bit[31] = 1
+ * Write DSPASURF = 0x00000000
+ * Write DSPACNTR bit[31] = 0
+ * Write DSPASURF = 0x00000000
+ * Restore PIPEACONF bits[19:18]  to original value
+ * Program DPLL
+ * Enable DPLL
+ * Wait for DPLL warm up 10us and check for DPLL lock bit in Pipe A config reg
+ * (Wait ensures clock is running smoothly before enabling pipe)
+ * Program pipe timings (Can be done before DPLL programming)
+ * Enable panel fitter as needed (Can be done before DPLL and/or
+ *     pipe timing programming)
+ * Enable pipe
+ * Enable planes (VGA or HiRes)
+ * Enable ports
+ * Enable panel power (Can be done before DPLL programming)
+ *
+ * 2. Disable sequence
+ *
+ * Disable panel backlight
+ * Disable panel power (for AOAC standby)
+ * Disable ports
+ * Disable planes (VGA or hires)
+ * Disable pipe
+ * Disable VGA display in 0x71400 bit 31
+ * (Disable VGA display done after disable pipe to allow pipe to turn off
+ *     when no vblank is available in native VGA mode)
+ * Wait for pipe off status
+ * (Wait ensures planes and pipe have completely turned off prior to
+ *     disabling panelfitter then DPLL)
+ * Disable panelfitter
+ * Disable DPLL
+ * Pipe timings change or change between VGA native or VGA center/upperleft
+ *     or HiRes
+ * Use complete disable sequence followed by complete enable sequence with
+ *     new mode programmings.
+ *
+ *
+ * Atom E6xx SDVO display: Display Pipe B Enable/Disable sequence:
+ *
+ * 1. Enable sequence
+ *
+ * Program LNC and LNW DPLL
+ * Write all planes, port, power control registers on both LNC and LNW
+ * 1st Enable LNC pipe
+ * 2nd Enable LNW pipe
+ *
+ * 2. Disable Sequence
+ *
+ * Disable panel power
+ * Disable ports
+ * Disable planes
+ * 1st Disable LNC pipe
+ * 2nd Disable TNC_SDVO pipe
+ * Disable panel fitter
+ * Disable DPLL
+ */
+
+
+/*!
+ *
+ * @param display Pointer to hardware device instance data
+ *
+ * @return void
+ */
+void program_pipe_vga_tnc(
+	igd_display_context_t *display)
+{
+	igd_timing_info_t *timing;
+	unsigned long vga_control;
+	unsigned long upscale = 0;
+	int centering = 1;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * VGA Plane can attach to only one pipe at a time. LVDS can
+	 * only attach to pipe B. We need to use the display passed to
+	 * determine the pipe number to use. (Napa is same as Alm).
+	 */
+
+	/*
+	 * We can come here with following cases:
+	 *   1. magic->vga    CRT, DVI type displays
+	 *   2. native->vga   int-lvds, and up-scaling lvds displays
+	 *   3. pipe->vga     TV and other unscaled-lvds displays
+	 */
+	vga_control = EMGD_READ32(MMIO(display) + 0x71400);
+	vga_control &= 0x18e3ff00;
+	vga_control |= 0x8e;
+
+	timing = PIPE(display)->timing;
+	if(!timing->extn_ptr) {
+		EMGD_ERROR_EXIT("No Extension pointer in program_pipe_vga_tnc");
+		return;
+	}
+
+	/* Find UPSCALING attr value*/
+	pi_pd_find_attr_and_value(PORT_OWNER(display),
+			PD_ATTR_ID_PANEL_FIT,
+			0,/*no PD_FLAG for UPSCALING */
+			NULL, /* dont need the attr ptr*/
+			&upscale);
+	/* this PI func will not modify value of upscale if attr does not exist */
+
+	/* magic->vga || native->vga cases, centering isn't required */
+	if ((timing->width == 720 && timing->height == 400) || upscale) {
+		EMGD_DEBUG("Centering = 0");
+		centering = 0;
+	}
+
+	/* Enable border */
+	if((timing->width >= 800) && !upscale) {
+		EMGD_DEBUG("Enable VGA Border");
+		vga_control |= (1L<<26);
+	}
+
+	if(timing->width == 640) {
+		EMGD_DEBUG("Enable Nine Dot Disable");
+		vga_control |= (1L<<18);
+	}
+
+	if(centering) {
+		EMGD_DEBUG("Enable VGA Center Centering");
+		vga_control |= 1L<<24;
+
+		if(timing->height >= 960) {
+			if(timing->width >= 1280) {
+				EMGD_DEBUG("Enable VGA 2x (Nine Dot Disable)");
+				vga_control |= (1L<<30) | (1L<<18);
+			}
+		}
+	} else {
+		if(PORT_OWNER(display)->port_type == IGD_PORT_LVDS) {
+			EMGD_DEBUG("Enable VGA Upper-Left Centering & Nine Dot Disable");
+			vga_control |= (1L<<25 | (1L<<18));
+		} else if (upscale) {
+			EMGD_DEBUG("Enable VGA Center Upper-left for upscale ports");
+			vga_control |= 1L<<25;
+		}
+	}
+
+	if(PIPE(display)->pipe_num) {
+		vga_control |= 1L<<29;
+	}
+
+	program_pipe_vga(display, (igd_timing_info_t *)timing->extn_ptr);
+	EMGD_WRITE32(vga_control, MMIO(display) + 0x71400);
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+
+#ifndef CONFIG_MICRO
+extern os_pci_dev_t bridge_dev;
+int read_bunit_tnc(unsigned long reg, unsigned long *val)
+{
+	reg <<= 8;
+	reg &= 0x0000ff00;
+	reg |= 0xD00300F0;
+  	 
+	EMGD_TRACE_ENTER;
+  	 
+	if(OS_PCI_WRITE_CONFIG_32(bridge_dev, 0xD0, reg)) {
+		EMGD_ERROR_EXIT("Writing into the MCR Failed\n");
+		return -IGD_ERROR_INVAL;
+	}
+  	 
+	if(OS_PCI_READ_CONFIG_32(bridge_dev, 0xD4, val)) {
+		EMGD_ERROR_EXIT("Reading from MDR Failed\n");
+		return -IGD_ERROR_INVAL;
+	}
+  	 
+	EMGD_TRACE_EXIT;
+  	 
+	return 0;
+}
+  	 
+int write_bunit_tnc(unsigned long reg, unsigned long val)
+{
+	reg <<= 8;
+	reg &= 0x0000ff00;
+	reg |= 0xE00300F0;
+  	 
+	EMGD_TRACE_ENTER;
+  	 
+	if(OS_PCI_WRITE_CONFIG_32(bridge_dev, 0xD4, val)) {
+			EMGD_ERROR_EXIT("Writing into the MDR Failed\n");
+		return -IGD_ERROR_INVAL;
+	}
+  	 
+	if(OS_PCI_WRITE_CONFIG_32(bridge_dev, 0xD0, reg)) {
+		EMGD_ERROR_EXIT("Writing into the MCR Failed\n");
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+#endif
+
+/*!
+ * Program Display Plane Values.
+ *
+ * @param display Pointer to hardware device instance data
+ * @param status
+ *
+ * @return void
+ */
+void program_plane_tnc(igd_display_context_t *display,
+	unsigned long status)
+{
+	unsigned long stride;
+	unsigned long stereo;
+	unsigned long plane_control;
+	unsigned long other_plane_reg;
+	igd_timing_info_t *timing;
+	igd_framebuffer_info_t *fb_info = PLANE(display)->fb_info;
+	unsigned long plane_reg = PLANE(display)->plane_reg;
+	igd_timing_info_t *pipe_timing;
+#ifndef  CONFIG_MICRO
+	unsigned long ulreg = 0;
+#endif
+	unsigned long b_reg;
+	/* tnc_wa_timing_t *wa = WA_TUNE; */
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program Plane: %s", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("Device power state: D%ld", GET_DEVICE_POWER_STATE(display));
+
+	igd_wait_vblank_tnc((igd_display_h)display);
+
+	plane_control = EMGD_READ32(MMIO(display) + plane_reg);
+	if(PLANE(display)->plane_reg == DSPACNTR) {
+		plane_control &= device_data_tnc->plane_a_preserve;
+	} else { /* if it's plane b or plane c */
+		plane_control &= device_data_tnc->plane_b_c_preserve;
+	}
+
+	/* TODO: Bspec: For EagleLake this Trickle Feed must always disable */
+
+	if((status == FALSE) ||
+		(GET_DEVICE_POWER_STATE(display) != IGD_POWERSTATE_D0)) {
+
+		/*
+		 * Note: The vga programming code does not have an "off". So
+		 * when programming the plane to off we make sure VGA is off
+		 * as well.
+		 */
+		disable_vga_tnc(MMIO(display));
+
+		/*
+		 * To turn off plane A or B, the program have to triger the plane A or B
+		 * start register.  Or else, it will not work.
+		 */
+		EMGD_WRITE32(plane_control, MMIO(display) + plane_reg);
+		EMGD_WRITE32(EMGD_READ32(MMIO(display) + plane_reg + DSP_START_OFFSET),
+			MMIO(display) + plane_reg + DSP_START_OFFSET);
+
+		igd_wait_vblank_tnc((igd_display_h)display);
+		EMGD_TRACE_EXIT;
+		return;
+	}
+	/*
+	 * Note: The very first pass through this function will be with
+	 * status false and timings == NULL. Don't use the timings before
+	 * the check above.
+	 */
+	timing = PIPE(display)->timing;
+	pipe_timing = timing;
+	/* There is a special case code for legacy VGA modes */
+	while (timing->extn_ptr) {
+		timing = (igd_timing_info_t *)timing->extn_ptr;
+	}
+	if(MODE_IS_VGA(timing) && CHECK_VGA(pipe_timing)) {
+		program_plane_vga(display, timing);
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	disable_vga_tnc(MMIO(display));
+
+	/* enable plane, select pipe, enable gamma correction logic */
+	plane_control |= 0x80000000 | (PIPE(display)->pipe_num<<24);
+	PIPE(display)->plane = PLANE(display);
+#ifndef CONFIG_MICRO
+	plane_control |= (1<<30);
+#endif
+
+	/* Here the settings:
+	 *   If line + pixel dbling, set 21,20 to 01b, and set Horz Multiply
+	 *   If line dbling only,    set 21,20 to 11b
+	 *   If pixel dbling only,   set 21,20 to 00b, but set Horz Multiply
+	 *   If no doubling,         set 21,20 to 00b (no Horz Multiply)
+	 * For pixel doubling
+	 *           --> both progressive/interlaced modes
+	 * For Line doubling
+	 *           --> progressive modes only
+	 */
+
+	if (!(timing->mode_info_flags & IGD_SCAN_INTERLACE)) {
+		/* Line doubling in progressive mode requires special bits */
+		if (timing->mode_info_flags & IGD_LINE_DOUBLE) {
+			/* BIT 20 for line & pixel doubling*/
+			plane_control |= BIT20;
+			/* check later, if no pixel doubling, set bit 21 too*/
+		}
+	}
+	if (timing->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		/* For line ONLY doubling, set bit 21 also '1' */
+		plane_control |= BIT21;
+	}
+
+	mode_get_stride_stereo_tnc(display, &stride, &stereo, 0);
+
+	/* set color depth */
+	switch (IGD_PF_DEPTH(fb_info->pixel_format)) {
+	case PF_DEPTH_8:
+		plane_control |= BIT27 | BIT30;
+		break;
+	case PF_DEPTH_16:
+		plane_control |= BIT28 | BIT26;
+		break;
+	default:
+	case PF_DEPTH_32:
+		plane_control |= BIT28 | BIT27;
+		break;
+	}
+
+	if(fb_info->flags & IGD_ENABLE_DISPLAY_GAMMA) {
+		plane_control |= (BIT30);
+	}
+
+	if(fb_info->flags & IGD_SURFACE_TILED) {
+		plane_control |= (BIT10);
+	}
+
+	/* Set watermark for Atom E6xx */
+#ifndef  CONFIG_MICRO
+	if (!mode_context->en_reg_override) {
+		if (plane_reg == DSPACNTR) {
+			other_plane_reg = DSPBCNTR;
+		} else {
+			other_plane_reg = DSPACNTR;
+		}
+
+		if (EMGD_READ32(MMIO(display) + other_plane_reg) & 0x80000000) {
+			EMGD_WRITE32(device_data_tnc->dsp_arb, MMIO(display) +
+				PIPEA_DISP_ARB_CTRL);
+		} else if (plane_reg == DSPACNTR) {
+			EMGD_WRITE32(0x00003fff, MMIO(display) + PIPEA_DISP_ARB_CTRL);
+		} else {
+			EMGD_WRITE32(0x00003f80, MMIO(display) + PIPEA_DISP_ARB_CTRL);
+		}
+
+		/*
+		 * Setting WM priority level to 11
+		 * to workaround display bouncing issues
+		 * TODO: Mode set from Clone->Single(Primary)
+		 * PIPEA_DISP_ARB_CTRL does not get updated
+		 */
+		ulreg = EMGD_READ32(MMIO(display) + 0x00020f8);
+		EMGD_WRITE32(ulreg | 0x000003f0, MMIO(display) + 0x00020f8);
+
+		EMGD_WRITE32(device_data_tnc->fifo_watermark1, MMIO(display) + FW_1);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark2, MMIO(display) + FW_2);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark3, MMIO(display) + FW_3);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark4, MMIO(display) + FW_4);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark5, MMIO(display) + FW_5);
+		EMGD_WRITE32(device_data_tnc->fifo_watermark6, MMIO(display) + FW_6);
+	} else {
+		/* en_reg_override=1 */
+		/* Override display registers */
+		EMGD_WRITE32(mode_context->gvd_hp_control, MMIO(display) + 0x00020f8);
+		EMGD_WRITE32(mode_context->disp_arb, MMIO(display) + PIPEA_DISP_ARB_CTRL);
+		EMGD_WRITE32(mode_context->fifo_watermark1, MMIO(display) + FW_1);
+		EMGD_WRITE32(mode_context->fifo_watermark2, MMIO(display) + FW_2);
+		EMGD_WRITE32(mode_context->fifo_watermark3, MMIO(display) + FW_3);
+		EMGD_WRITE32(mode_context->fifo_watermark4, MMIO(display) + FW_4);
+		EMGD_WRITE32(mode_context->fifo_watermark5, MMIO(display) + FW_5);
+		EMGD_WRITE32(mode_context->fifo_watermark6, MMIO(display) + FW_6);
+		EMGD_WRITE32(mode_context->disp_chicken_bits, MMIO(display) + 0x0070400);
+
+		/* Override Bunit Chickenbits and BWFLUSH register */
+		write_bunit_tnc(0x30, mode_context->bunit_chicken_bits);
+		write_bunit_tnc(0x02, mode_context->bunit_write_flush);
+	}
+
+	EMGD_DEBUG(" GVD HP_CONTROL: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + PIPEA_DISP_ARB_CTRL);
+	EMGD_DEBUG(" Display Arbitration register: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + FW_1);
+	EMGD_DEBUG(" FIFO Watermark Control Register 1: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + FW_2);
+	EMGD_DEBUG(" FIFO Watermark Control Register 2: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + FW_3);
+	EMGD_DEBUG(" FIFO Watermark Control Register 3: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + FW_4);
+	EMGD_DEBUG(" FIFO Watermark Control Register 4: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + FW_5);
+	EMGD_DEBUG(" FIFO Watermark Control Register 5: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + FW_6);
+	EMGD_DEBUG(" FIFO Watermark Control Register 6: 0x%lx", ulreg);
+	ulreg = EMGD_READ32(MMIO(display) + DSP_CHICKENBITS);
+	EMGD_DEBUG(" Display Chicken bits: 0x%lx", ulreg);
+	read_bunit_tnc(0x30, &b_reg);
+	EMGD_DEBUG(" Bunit Chicken bits: 0x%lx", b_reg);
+	read_bunit_tnc(0x02, &b_reg);
+	EMGD_DEBUG(" Bunit Write Flush: 0x%lx", b_reg);
+#else
+	/* ITP Script is doing this and so go ahead */
+	/* The PIPEA_DISP_ARB_CTRL set fixed the issue with 32bit vesa modes */
+	EMGD_WRITE32(0x00001FBF, MMIO(display) + PIPEA_DISP_ARB_CTRL);
+	EMGD_WRITE32(0x3F8F0F18, MMIO(display) + FW_1);
+#endif
+	/* FIXME: Not required for TNC.
+	 * The B-Spec states that rendering will be slower if the fences are not
+	 * a power of 2.  So for now, always use a power of 2. */
+	/* EMGD_WRITE32(0x04000400, MMIO(display) + 0x209c); */
+
+	EMGD_DEBUG(" Plane Control: 0x%lx", plane_control);
+	EMGD_DEBUG(" Plane Base:    0x%lx", fb_info->visible_offset);
+	EMGD_DEBUG(" Plane Pitch:   0x%lx", stride);
+
+	EMGD_WRITE32(plane_control, MMIO(display) + plane_reg);
+	EMGD_WRITE32(stride, MMIO(display) + plane_reg + DSP_STRIDE_OFFSET);
+	/* Both of these registers are Reserved on Gen4 */
+	/*EMGD_WRITE32(size, MMIO(display) + plane_reg + DSP_SIZE_OFFSET);*/
+	/*EMGD_WRITE32(stereo, MMIO(display) + plane_reg + DSP_STEREO_OFFSET);*/
+	EMGD_WRITE32(0, MMIO(display) + plane_reg + DSP_LINEAR_OFFSET);
+	EMGD_WRITE32(0, MMIO(display) + plane_reg + 0x24);
+	EMGD_WRITE32(fb_info->visible_offset,
+		MMIO(display) + plane_reg + DSP_START_OFFSET);
+
+	igd_wait_vblank_tnc((igd_display_h)display);
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ * PGen4 can check when the pipe is enabled or disabled.
+ * This function waits for the pipe to be enabled or disabled.
+ * check_on_off = 0 to wait for the pipe to disable.
+ * check_on_off = 0x40000000 to wait for the pipe to enable.
+ *
+ * @param mmio
+ * @param pipe_reg
+ * @param check_on_off
+ *
+ * @return void
+ */
+void wait_pipe(unsigned long pt, unsigned long pipe_reg, unsigned long check_on_off)
+{
+	unsigned long temp;
+	os_alarm_t timeout;
+
+	EMGD_TRACE_ENTER;
+
+	/* 0:3:0 doesn't wait pipe, only LNC device does. */
+	if (pt == IGD_PORT_SDVO) {
+		return;
+	}
+
+	/* Wait for Pipe enable/disable, about 50 msec (20Hz). */
+	timeout = OS_SET_ALARM(50);
+	do {
+		OS_SCHEDULE();
+		temp = EMGD_READ32(MMIO_TNC(IGD_PORT_LVDS) + pipe_reg) & 0x40000000;
+		/* Check for timeout */
+	} while ((temp != check_on_off) && (!OS_TEST_ALARM(timeout)));
+
+	if (temp != check_on_off) {
+		EMGD_ERROR_EXIT("Timeout waiting for pipe enable/disable");
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+unsigned long get_port_type(int crtc_id) {
+	if (crtc_id == IGD_KMS_PIPEA) {
+		return IGD_PORT_LVDS;
+	}
+	if (crtc_id == IGD_KMS_PIPEB) {
+		return IGD_PORT_SDVO;
+	}
+	EMGD_ERROR("Unrecognized port type based on crtc_id of %d", crtc_id);
+	return 0;
+}
+
+/*!
+ * This function programs the Timing registers and clock registers and
+ * other control registers for PIPE.
+ *
+ * @param display
+ * @param status
+ *
+ * @return void
+ */
+void program_pipe_tnc(igd_display_context_t *display,
+	unsigned long status)
+{
+	unsigned long   timing_reg;
+	unsigned long   pipe_conf;
+	unsigned long   hactive, vactive;
+	igd_timing_info_t  *pTimings;
+	igd_timing_info_t  pTimings_tmp;
+	igd_display_port_t *port;
+	unsigned long temp;
+	unsigned long pt = PORT_TYPE(display);
+	unsigned long dc;
+	unsigned long calc;
+	short hactive_tmp, vactive_tmp;
+	int i;
+	tnc_wa_timing_t *wa;
+	/* igd_framebuffer_info_t *fb_info = PLANE(display)->fb_info; */
+	platform_context_tnc_t *platform_context;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program Pipe: %s", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("Device power state: D%ld", GET_DEVICE_POWER_STATE(display));
+
+	platform_context = (platform_context_tnc_t *)display->context->platform_context;
+
+	/* For Windows OS, flag_clip_fix will be overridden by configurable parameter in registry.
+	* This section of code is excluded in VBIOS, VBIOS will always use the pre-assigned value.
+	*/
+	flag_clip_fix = mode_context->clip_hw_fix;
+
+	pipe_conf = device_data_tnc->pipe_preserve &
+		READ_MMIO_REG_TNC(pt, PIPE(display)->pipe_reg);
+
+	if((status == FALSE) ||
+		(GET_DEVICE_POWER_STATE(display) == IGD_POWERSTATE_D3)) {
+		/* For SDVO disable both pipe Bs in 0:2:0 and 0:3:0 */
+		if (pt == IGD_PORT_SDVO) {
+			WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, PIPE(display)->pipe_reg,
+				pipe_conf & (~0x80000000L));
+			/* After disable pipe B in 0:2:0, wait_pipe must be
+ 			 * called to ensure the pipe B to be turned off */
+			wait_pipe(IGD_PORT_LVDS, PIPE(display)->pipe_reg, 0);
+		}
+		/* Disable pipe */
+		WRITE_MMIO_REG_TNC(pt, PIPE(display)->pipe_reg,
+			pipe_conf & (~0x80000000L));
+
+		/* check when the pipe is disabled. */
+		wait_pipe(pt, PIPE(display)->pipe_reg, 0);
+
+		/* Disable DPLL */
+		//WRITE_MMIO_REG_TNC(pt, PIPE(display)->clock_reg->dpll_control,
+		//	READ_MMIO_REG_TNC(pt,
+		//		PIPE(display)->clock_reg->dpll_control) & ~0x80000000L);
+
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	/* For TNC B1, enable hardware cliping fix*/
+	if((platform_context->tnc_dev3_rid == TNC_B1_DEV3_RID)&&
+		(flag_clip_fix & IGD_CLIP_FIX_GLOBAL_ENABLE)) {
+			/* Disable SDVO Pipe in Device 2 and Device 3 */
+			WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, PIPE(display)->pipe_reg,
+				pipe_conf & (~0x80000000L));
+  	 
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, PIPE(display)->pipe_reg,
+				pipe_conf & (~0x80000000L));
+ 
+			/* check when the pipe is disabled. */
+			wait_pipe(IGD_PORT_LVDS, PIPE(display)->pipe_reg, 0);
+  	 
+			/* Enable clipping hardware fix */
+			temp = READ_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS);
+			if(flag_clip_fix & IGD_CLIP_FIX_REPLACE_STALL) {
+				temp |= BIT18;
+			} else {
+				temp &= ~BIT18;
+			}
+			if(flag_clip_fix & IGD_CLIP_FIX_DISABLE_THROTTLE) {
+				temp |= BIT15;
+			} else {
+				temp &= ~BIT15; 
+			}
+			WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS, temp);
+	}
+
+
+	port     = PORT_OWNER(display);
+	pTimings = PIPE(display)->timing;
+
+	{
+		/* Debug messages */
+		pd_timing_t *vga_timing = (pd_timing_t *)pTimings->extn_ptr;
+		EMGD_DEBUG("pTimings %ux%u mode_number = %u mode_info_flags = 0x%lx, dclk = %lu",
+			pTimings->width,
+			pTimings->height,
+			pTimings->mode_number,
+			pTimings->mode_info_flags,
+			pTimings->dclk);
+		if (vga_timing) {
+			EMGD_DEBUG("ext_timing %ux%u mode_number = %u mode_info_flags= 0x%lx, dclk = %lu",
+				vga_timing->width,
+				vga_timing->height,
+				vga_timing->mode_number,
+				vga_timing->mode_info_flags,
+				vga_timing->dclk);
+		}
+	}
+
+	/*
+	 * If the mode is VGA and the PD says it handles all VGA modes without
+	 * reprogramming then just set the mode and leave centering off.
+	 */
+	if(pTimings->mode_info_flags & IGD_MODE_VESA) {
+			EMGD_DEBUG("IGD_MODE_VESA");
+		if (pTimings->mode_number <= VGA_MODE_NUM_MAX) {
+			/* Pipe timings and clocks no longer need to be set since
+			 * the VGA timings will be used.
+			WRITE_MMIO_REG_TNC(pt, PIPE(display)->pipe_reg,
+				pipe_conf | 0x80000000); */
+
+			/* Gen4 can check when the pipe is enabled. No longer needed
+			 * since pipe not enabled and VGA timings are used.
+			wait_pipe(PIPE(display)->pipe_reg, 0x40000000);*/
+
+			EMGD_DEBUG("pTimings->mode_number <= VGA_MODE_NUM_MAX");
+			program_pipe_vga_tnc(display);
+			EMGD_TRACE_EXIT;
+			return;
+		} else {
+#ifdef CONFIG_MICRO
+			set_256_palette(
+				MMIO_TNC(PORT(display, display->port_number)->port_type));
+#endif
+		}
+	}
+
+	/* Program dot clock divisors. */
+	program_clock_tnc(display, PIPE(display)->clock_reg, pTimings->dclk);
+
+	/* Program timing registers for the pipe */
+	timing_reg = PIPE(display)->timing_reg;
+	if (pTimings->mode_info_flags & IGD_PIXEL_DOUBLE) {
+		hactive = (unsigned long)pTimings->width*2 - 1;
+	} else {
+		hactive = (unsigned long)pTimings->width - 1;
+	}
+
+	if (pTimings->mode_info_flags & IGD_LINE_DOUBLE) {
+		vactive = (unsigned long)pTimings->height*2 - 1;
+	} else {
+		/* For Atom E6xx Hardware will automatically divide by 2 to
+		   get the number of line in each field */
+		vactive = (unsigned long)pTimings->height - 1;
+	}
+
+#ifndef CONFIG_MICRO
+	/* reset the palette */
+	for (i = 0; i < 256; i++) {
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, PIPE(display)->palette_reg,
+			((i<<16) | (i<<8) | i));
+	}
+
+
+	/* apply color correction */
+	for( i = 0; PD_ATTR_LIST_END != port->attributes[i].id; i++ ) {
+
+		if ((PD_ATTR_ID_FB_GAMMA      == (port->attributes[i].id)) ||
+			(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)) ||
+			(PD_ATTR_ID_FB_BRIGHTNESS == (port->attributes[i].id)))  {
+
+			mode_context->dispatch->full->set_color_correct(display);
+		}
+	}
+#endif
+
+	/*
+	 * NOTE: For size reasons the timng table contains unsigned short
+	 * values. Don't shift them past 16. Use a temp instead.
+	 * All register offsets and bit shift are verified for Gen4
+	 *
+	 * For SDVO display:
+	 * Write values into pipe B registers in both 0:2:0 and 0:3:0
+	 */
+
+	dc = *(display->context->mod_dispatch.dsp_current_dc);
+
+	wa = WA_TUNE;
+
+	for (i=0; i<2; i++) {
+		/* Temp variable */
+		pTimings_tmp = *pTimings;
+		hactive_tmp = (short) hactive;
+		vactive_tmp = (short) vactive;
+#ifndef CONFIG_MICRO
+
+		/* Htotal and tuning wa is not supported in VBIOS for TNC				
+		 * TNC B0: Enable Htotal formula and CRC Tuning to fix clipping issue and
+		 * async flip flikering on single display
+		 * TNC B1: Enable Htotal formula without CRC Tuning to fix async flip flikering
+		 * For B0, TVOUT is excluded for htotal workaround to avoid image shifting 
+		 * issue happen. Image shifting will be fixed on B1 while ref_freq is lower down
+		 * to 198Mhz
+		 */
+
+
+		if(	(pt == IGD_PORT_SDVO) && FLAG(flag_basic_htotal_formula_wa) &&
+			(   ((platform_context->tnc_dev3_rid == TNC_B1_DEV3_RID) && 
+					(mode_context->async_flip_wa)	)||
+				(   (platform_context->tnc_dev3_rid == TNC_B0_DEV3_RID) &&
+				    (!IGD_DC_SINGLE(dc) || (port->pd_driver->type != PD_DISPLAY_TVOUT))
+				)
+			)){
+
+
+	 		if((!(pTimings->reserved_dd & TNC_HTOTAL_TUNED)) &&
+	 				(wa->counter < LIMIT_TOTAL_CHECK_DISPLAY) &&
+					(platform_context->tnc_dev3_rid == TNC_B0_DEV3_RID) &&
+ 					(FLAG(flag_enable_tuning_wa))) {
+
+				/* Modify blanks so it always begin after active pixel and ends at the
+				 * end. Do not change it if we are already tuned to maintain
+				 * original timing specification
+				 */
+				pTimings_tmp.hblank_start = (short) (pTimings->width - 1);
+				pTimings_tmp.vblank_start = (short) (pTimings->height - 1);
+				pTimings_tmp.hblank_end = (short) (pTimings->htotal);
+				pTimings_tmp.vblank_end = (short) (pTimings->vtotal);
+			}
+
+#ifndef CONFIG_MICRO
+			/* Delay Frame start while Tuning*/
+			/* Frame start delay is added as plane corruption occurs without delay. 
+			 * Pipe would begin fetching data from random memory location causing the delay. 
+			 * Silicon could not determine why this is happening but agree that adding a delay 
+			 * would not cause any issue unless for a DTD with single vsync line which is 
+			 * super unlikely used.
+			 */
+
+			/* B1 stepping does not need to set this bit because it does not need tuning*/
+			if((platform_context->tnc_dev3_rid == TNC_B0_DEV3_RID) && 
+				(IGD_DC_CLONE(dc) || IGD_DC_EXTENDED(dc))){
+				pipe_conf |= BIT27;
+			}
+#endif
+
+
+			if(i==0){
+				if(pTimings->reserved_dd == 0 || (wa->counter > LIMIT_TOTAL_CHECK_DISPLAY)){
+					/* First time tuning */
+					if ((mode_context->ref_freq != 0) && 
+							(mode_context->ref_freq >= 190000) && 
+							(mode_context->ref_freq <= 210000))
+					{
+						if ((platform_context->tnc_dev3_rid == TNC_B1_DEV3_RID)){
+							/* The optimum reference frequency used for 3DMark flickering workaround formula
+							is the LNC clock minus the optimum margin found from experiment */
+							calc = (pTimings->htotal * (mode_context->ref_freq - LNC_B1_OPTIMUM_MARGIN));
+						} else {
+						calc = (pTimings->htotal * mode_context->ref_freq);
+						}
+					} else {
+						if((platform_context->tnc_dev3_rid == TNC_B1_DEV3_RID)){
+							calc = (pTimings->htotal * (LNC_CLOCK - LNC_B1_OPTIMUM_MARGIN));
+					} else {
+						calc = (pTimings->htotal * LNC_CLOCK);
+						}
+
+					}
+
+					calc = (calc / (PIPE(display)->clock_reg->actual_dclk));
+					calc *= (pTimings->vtotal -1);
+					calc /= pTimings->vtotal;
+					pTimings_tmp.htotal = (short)calc;
+					pTimings->reserved_dd = wa->htotal = pTimings_tmp.htotal;
+					EMGD_DEBUG("Delta = %d", wa->htotal);
+				}else if (pTimings->reserved_dd & TNC_HTOTAL_TUNED){
+					pTimings_tmp.htotal = (short)(pTimings->reserved_dd & (~TNC_HTOTAL_TUNED));
+				}else{
+					if(wa->htotal == 0)
+						wa->htotal = (short)pTimings->reserved_dd;
+					pTimings_tmp.htotal = wa->htotal;
+				}
+				/* Use vphase formula if available */
+				if(vphase){
+					pTimings_tmp.vtotal -= (short)vphase;
+					pTimings_tmp.vsync_start -= (short)vphase;
+					pTimings_tmp.vsync_end -= (short)vphase;
+					pTimings_tmp.vblank_end -= (short)vphase;
+				}else{
+					//pTimings_tmp.hblank_start += (short) (pTimings_tmp.htotal - pTimings->htotal);
+					pTimings_tmp.hblank_end += (short) (pTimings_tmp.htotal - pTimings->htotal);
+				}
+			}
+		}
+#endif
+
+		temp = (unsigned long)(pTimings_tmp.htotal) << 16 | hactive_tmp;
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg, temp);
+
+		temp = ((unsigned long) pTimings_tmp.hblank_end << 16) |
+			(unsigned long)(pTimings_tmp.hblank_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x04, temp);
+
+		temp = ((unsigned long)(pTimings->hsync_end) << 16) |
+			(unsigned long)(pTimings->hsync_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x08, temp);
+
+		temp = ((unsigned long)(pTimings_tmp.vtotal) << 16) | vactive_tmp;
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x0C, temp);
+
+		temp = ((unsigned long)(pTimings_tmp.vblank_end) << 16) |
+			(unsigned long)(pTimings_tmp.vblank_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x10, temp);
+
+		temp = ((unsigned long)(pTimings_tmp.vsync_end)<< 16) |
+			(unsigned long)(pTimings_tmp.vsync_start);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x14, temp);
+
+		/*
+		 * If there is a linked mode it is either the VGA or a scaled
+		 * mode. If it is scaled then we need to use it as the source size.
+		 */
+		if(pTimings->extn_ptr) {
+			igd_timing_info_t *scaled_timings =
+				(igd_timing_info_t *)pTimings->extn_ptr;
+			if((scaled_timings->mode_info_flags & IGD_MODE_VESA) &&
+				(scaled_timings->mode_number <= VGA_MODE_NUM_MAX)) {
+				temp = (hactive << 16) | vactive;
+			} else {
+				EMGD_DEBUG("scaled_timings->width [%d], scaled_timings->height [%d]\n", scaled_timings->width, scaled_timings->height);
+				temp = (unsigned long)scaled_timings->width  - 1;
+				temp = (temp << 16) |
+					(unsigned long)(scaled_timings->height - 1);
+			}
+		} else {
+			temp = (hactive_tmp << 16) | vactive_tmp;
+		}
+		WRITE_MMIO_REG_TNC(ports_tnc[i], timing_reg + 0x1C, temp);
+
+		/* Enable pipe */
+		pipe_conf |= PIPE_ENABLE;
+
+		/* Put pipe in interlaced mode if requested:
+		 *     should only happen for LVDS display if at all. */
+		if (pTimings->mode_info_flags & IGD_SCAN_INTERLACE) {
+			pipe_conf |= (INTERLACE_EN);
+		} else {
+			pipe_conf &= ~(INTERLACE_EN);
+		}
+
+#ifdef CONFIG_MICRO
+		if (pt == IGD_PORT_SDVO) {
+			/*
+			   Enable the panel fitter as VGA controller in Lincroft
+			   is a Panel Fitted VGA controller.
+			   LNC only supports panel fitted VGA mode
+			   (upper left VGA mode). You need to enable the panel fitter.
+			   The timing control will be from the pipe timing generator
+			   but not from the VGA timing generator CRTC registers
+			   as in the centering mode.
+			*/
+			WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, PFIT_CONTROL, 0xA2000000);
+		}
+#endif
+
+		WRITE_MMIO_REG_TNC(ports_tnc[i], PIPE(display)->pipe_reg, pipe_conf);
+		WRITE_MMIO_REG_TNC(ports_tnc[i], PIPE(display)->pipe_reg, pipe_conf);
+		/* For LVDS port, don't touch dev3 registers stop after 1st iteration */
+		if (pt == IGD_PORT_LVDS) {
+			break;
+		}
+	}
+
+
+	/* Gen4 can check when the pipe is enabled. */
+	wait_pipe(IGD_PORT_LVDS, PIPE(display)->pipe_reg, 0x40000000);
+
+	/*
+	 * Set the VGA address range to 0xa0000 so that a normal (not VGA)
+	 * mode can be accessed through 0xa0000 in a 16bit world.
+	 */
+	WRITE_AR(MMIO(display), 0x10, 0xb);
+	WRITE_VGA(MMIO(display), GR_PORT, 0x06, 0x5);
+	WRITE_VGA(MMIO(display), GR_PORT, 0x10, 0x1);
+
+	if(pTimings->extn_ptr) {
+		/* This means either internal scaling (LVDS) or centered VGA */
+		pTimings = pTimings->extn_ptr;
+		if(pTimings->extn_ptr) {
+			/* This is both the scaled and centered VGA */
+			pTimings = pTimings->extn_ptr;
+		}
+		if (pTimings->mode_info_flags & IGD_MODE_VESA) {
+			if (pTimings->mode_number <= VGA_MODE_NUM_MAX) {
+				program_pipe_vga_tnc(display);
+			} else {
+#ifdef CONFIG_MICRO
+				/*
+				 * FIXME: This is not appropriate. This assumes that
+				 * CONFIG_MICRO means "This is vBIOS" and programs
+				 * the palette. vBIOS IAL should probably just set the
+				 * palette itself??
+				 */
+				set_256_palette(
+					MMIO_TNC(PORT(display, display->port_number)->port_type));
+#endif
+			}
+		}
+	}
+
+	if (pt == IGD_PORT_SDVO) {
+		/*  Enable Chicken Bit */
+		/*  Setting BIT6 enable Pipe B Palette Write
+         *  to prevent hang during palette write */
+		/*  Enable Chicken Bit */
+		temp = READ_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS);
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS, DSP_CHICKENBITS, temp | BIT6);
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+void reset_plane_pipe_ports_tnc(igd_context_t *context)
+{
+	igd_plane_t *plane;
+	igd_display_pipe_t *pipe;
+	igd_display_port_t *port,*tv_port=NULL;
+	unsigned long temp;
+	unsigned long i, j;
+	unsigned char *mmio;
+	inter_module_dispatch_t *md;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Disable all plane, pipe and port registers because the
+	 * bios may have been using a different set. Only unset the
+	 * enable bit.
+ */
+	mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+	md = &context->mod_dispatch;
+	/* Turn off LVDS and SDVO ports */
+	port = NULL;
+	while((port = md->dsp_get_next_port(context, port, 0)) != NULL) {
+		/* if the port is TV, then don't set the power to S3 as this causes
+		 * blank screen on analog port after killx or cosole mode,
+		 * probably because the external clock needs to be on till the pipes and
+		 * DPLLs are off
+		 */
+		if (port->pd_driver) {
+			if(port->pd_type == PD_DISPLAY_TVOUT) {
+				tv_port = port;
+			}else {
+				port->pd_driver->set_power(port->pd_context, IGD_POWERSTATE_D3);
+			}
+		}
+
+		if (port->port_type == IGD_PORT_SDVO) {
+			shutdown_ST_bridge(context);
+		}
+
+		if (port->pd_driver) {
+			temp = READ_MMIO_REG_TNC(port->port_type, port->port_reg);
+			WRITE_MMIO_REG_TNC(port->port_type, port->port_reg, (temp & ~BIT31));
+		}
+	}
+
+	/* disable plane C */
+	temp = EMGD_READ32(EMGD_MMIO(mmio) + DSPCCNTR);
+	if(temp & BIT31) {
+		EMGD_WRITE32(0x0, EMGD_MMIO(mmio) + DSPCCNTR);
+		EMGD_WRITE32(0x0, EMGD_MMIO(mmio) + DSPCCNTR + DSP_START_OFFSET);
+	}
+
+	/*
+	 * Gen4 appears to require that plane B be shut off prior to
+	 * shutting off plane A.  The normal get_next_plane returns them
+	 * in order.  We need to read the list backwards.
+	 */
+	plane = NULL;
+	while ((plane = md->dsp_get_next_plane(context, plane, 1)) != NULL) {
+		/*  This section only deals with display planes.
+		 *  Leave cursor, VGA, overlay, sprite planes alone since they will
+		 *  need a different disable bit/sequence.
+		 */
+		temp = EMGD_READ32(EMGD_MMIO(mmio) + plane->plane_reg);
+		if ((plane->plane_features & IGD_PLANE_DISPLAY)) {
+			i = 0x71008;  /* PIPE B */
+			if (temp & BIT31) {
+				if(plane->plane_reg == DSPACNTR) {
+					temp = temp & device_data_tnc->plane_a_preserve;
+					i = 0x70008;  /* use i as pipe_reg */
+				}
+				EMGD_WRITE32((temp & ~BIT31), EMGD_MMIO(mmio) + plane->plane_reg);
+
+				/* The B-Spec is ambiguous on which register is the trigger.
+				 * Testing has shown the the surface start address is the
+				 * correct trigger to disable the plane.
+				 */
+				EMGD_WRITE32(0, EMGD_MMIO(mmio)+plane->plane_reg+DSP_START_OFFSET);
+
+				/* Wait for VBLANK to ensure that the plane is really off */
+				wait_for_vblank_tnc(i);
+
+				EMGD_DEBUG("Plane disabled 0x%lx", plane->plane_reg);
+			}
+		} else if ((plane->plane_features & IGD_PLANE_CURSOR)) {
+			EMGD_WRITE32((temp & 0xffffffe8),
+				EMGD_MMIO(mmio) + plane->plane_reg);
+			EMGD_WRITE32(0, EMGD_MMIO(mmio) + plane->plane_reg+4);
+		}
+
+	}
+
+	/* Turn off pipes */
+	pipe = NULL;
+	while ((pipe = md->dsp_get_next_pipe(context, pipe, 0)) != NULL) {
+		j = 0;
+
+		/* Is this really required? Just waited for vblank above 2 times */
+		wait_for_vblank_tnc(pipe->pipe_reg);
+
+		for (i = 0; i < 2; i++) {
+			temp = READ_MMIO_REG_TNC(ports_tnc[i], pipe->pipe_reg);
+
+			if (temp & BIT31) {
+				/* Do not turn off Pipe B when shutting down */
+				if((context->device_context.power_state
+						== IGD_POWERSTATE_UNDEFINED) &&
+					(pipe->pipe_reg == PIPEB_CONF)){
+					continue;
+				}
+				WRITE_MMIO_REG_TNC(ports_tnc[i], pipe->pipe_reg,
+					(temp & device_data_tnc->pipe_preserve));
+
+				/* Gen4 can check when the pipe is disabled. */
+				wait_pipe(ports_tnc[i], pipe->pipe_reg, 0);
+
+				/* Disable VGA display */
+				disable_vga_tnc(EMGD_MMIO(mmio));
+
+			}
+
+			/* If current pipe is for sDVO, then iterate for PIPE B in
+			 * both 0:2:0 LNC and 0:3:0 Atom E6xx devices */
+			if (pipe->pipe_reg == 0x70008L) {
+				break;
+			}
+		}
+		/* Disable DPLL:
+		 *  LVDS: LNC 0xF014
+		 *  SDVO: Atom E6xx 0x6018 */
+		temp = READ_MMIO_REG_TNC(ports_tnc[j], pipe->clock_reg->dpll_control);
+
+		if (temp & BIT31) {
+			WRITE_MMIO_REG_TNC(ports_tnc[j], pipe->clock_reg->dpll_control,
+				temp & ~BIT31);
+		}
+		j++;
+	}
+	/* pipes and DPLLs are off, now set the power for TV */
+	if(tv_port && tv_port->pd_driver) {
+		tv_port->pd_driver->set_power(tv_port->pd_context, IGD_POWERSTATE_D3);
+	}
+
+	EMGD_TRACE_EXIT;
+} /* end reset_plane_pipe_ports */
+
+
+void shutdown_ST_bridge(igd_context_t *context) {
+	platform_context_tnc_t *platform_context = context->platform_context;
+
+	if (!platform_context->stbridgedev) {
+		return;
+	}
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18,   0x0106007E);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2000,   0x80);
+
+	pd_usleep(20);
+}
+
+void initialize_ST_bridge(igd_context_t *context, igd_display_port_t *port) {
+	platform_context_tnc_t *platform_context = context->platform_context;
+	pd_port_status_t        port_status;
+	unsigned long           tempreg;
+	unsigned char           cmdreg;
+	unsigned long           val;
+	unsigned long           port_output = 0x04000000;
+	int                     ret;
+
+	if (!platform_context->stbridgedev) {
+		return;
+	}
+
+	/* call pd_get_port_status() if exists */
+	if (port && port->pd_driver && port->pd_driver->pd_get_port_status) {
+		ret = port->pd_driver->pd_get_port_status(port->pd_context, &port_status);
+		if (ret == PD_SUCCESS) {
+			if (port_status.display_type == PD_DISPLAY_LVDS_EXT) {
+				port_output = 0x02000000;
+			}
+		}
+	}
+
+	tempreg = READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x20);
+	tempreg |= 0xFFFFFF00;
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x20, tempreg);
+
+	tempreg = READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x1020);
+	tempreg |= 0x7;
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x1020, tempreg);
+
+	tempreg = READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x10);
+	tempreg |= 0x6;
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x10, tempreg);
+
+	tempreg = READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x4);
+	tempreg |= 0x6;
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST_GPIO,0x4, tempreg);
+
+	OS_PCI_READ_CONFIG_8((os_pci_dev_t)platform_context->stbridgedev, 0x4, &cmdreg);
+	cmdreg |= 0x7;
+	OS_PCI_WRITE_CONFIG_8((os_pci_dev_t)platform_context->stbridgedev, 0x4, cmdreg);
+
+	OS_PCI_READ_CONFIG_8((os_pci_dev_t)platform_context->stgpiodev, 0x4, &cmdreg);
+	cmdreg |= 0x7;
+	OS_PCI_WRITE_CONFIG_8((os_pci_dev_t)platform_context->stgpiodev, 0x4, cmdreg);
+
+	OS_PCI_READ_CONFIG_8((os_pci_dev_t)platform_context->pcidev1, 0x4, &cmdreg);
+	cmdreg |= 0x7;
+	OS_PCI_WRITE_CONFIG_8((os_pci_dev_t)platform_context->pcidev1, 0x4, cmdreg);
+
+	/* Write DB control values */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18,   0x0106007E);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2000, 0x00000000);
+	pd_usleep(20);
+
+	/* Put Display Bridge in Reset */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18,   0x00F8003E | port_output);
+	pd_usleep(20);
+
+	/* SDVO PHY startup sequence */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2800, READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2800) | 0x00000080);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2900, READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2900) | 0x00000013);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2400, READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2400) | 0x00000080);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2500, READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2500) | 0x00000013);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2054, 0x00000063);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2454, 0x00000063);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2854, 0x00000063);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2044, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2444, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2844, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2040, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2440, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2840, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2080, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2480, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2880, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2148, 0x00000050);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2144, 0x00000002);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2000, 0x00000086);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2104, 0x00000020);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2108, 0x0000002B);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2100, 0x00000013);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x20C0, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x24C0, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x28C0, 0x00000000);
+	pd_usleep(20);
+
+	do {
+		pd_usleep(20);
+		val = READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2100);
+	} while ((val & 0xC) != 0xC);
+
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2000, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2140, 0x0000008D);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2400, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2800, 0x00000000);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x200C, 0x00000013);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2010, 0x0000007F);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x240C, 0x00000013);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2410, 0x0000007F);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x280C, 0x00000013);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2810, 0x0000007F);
+	pd_usleep(20);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2080, 0x00000001);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2480, 0x00000001);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2880, 0x00000001);
+	pd_usleep(20);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2084, 0x0000000F);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2484, 0x0000000F);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2884, 0x0000000F);
+	pd_usleep(20);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2040, 0x00000004);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2440, 0x00000004);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2840, 0x00000004);
+	pd_usleep(20);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2044, 0x00000014);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2444, 0x00000014);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2844, 0x00000014);
+	pd_usleep(20);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2054, 0x0000002B);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2454, 0x0000002B);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x2854, 0x0000002B);
+	pd_usleep(20);
+
+	/*
+	 * Enable interrupts. It is optional per STM. Include this line only
+	 * when interrupt handler with functionality is implemented for Display
+	 * Bridge.
+	 */
+	//WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x10, 0x00FC0000);
+	//pd_usleep(20);
+
+	/* Release Reset from Control register */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18, 0x00F80030 | port_output);
+	pd_usleep(20);
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18, 0x00F80000 | port_output);
+	pd_usleep(20);
+
+	/* Increase driver strength in case OLDI output is enabled */
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18, READ_MMIO_REG_TNC(IGD_PORT_SDVO_ST, 0x18) | 0x00001800);
+	pd_usleep(20);
+}
+
+/*!
+ * Status is currently not used
+ *
+ * @param display
+ * @param port_number
+ * @param status
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int post_program_port_tnc(igd_display_context_t *display,
+		unsigned short port_number,
+		unsigned long status)
+{
+	int ret;
+	igd_display_port_t *port;
+	igd_timing_info_t  *timings;
+	unsigned long portreg; /* temp; */
+#ifndef CONFIG_MICRO
+	unsigned long pt = PORT_TYPE(display);
+#endif
+
+	EMGD_TRACE_ENTER;
+
+	port = PORT(display, port_number);
+	timings = PIPE(display)->timing;
+
+	/*
+	 * The programming found in the common code for all chipsets
+	 * has the device programming sequence as follows:
+	 *  Port
+	 *  Pipe
+	 *  Post Port
+	 *  Plane
+	 * On Gen4, if the port is enabled before the pipe, there is a 10%
+	 * chance that the port will not turn on properly.
+	 * Due to compatability requires with other chipsets, this workaround
+	 * fixes this issue
+	 */
+	portreg = READ_MMIO_REG_TNC(port->port_type, port->port_reg);
+	WRITE_MMIO_REG_TNC(port->port_type, port->port_reg, portreg & ~BIT31);
+	WRITE_MMIO_REG_TNC(port->port_type, port->port_reg, portreg);
+
+	if (port->port_type == IGD_PORT_SDVO) {
+		initialize_ST_bridge(display->context, port);
+	}
+
+	ret = 0;
+	/* call post_set_mode() if exists */
+	if (port->pd_driver->post_set_mode) {
+		#ifndef CONFIG_MICRO
+		if (pt == IGD_PORT_SDVO){
+			ret = port->pd_driver->post_set_mode(port->pd_context, timings,
+				status); /*Needed for OKI*/
+		} else {
+			ret = port->pd_driver->post_set_mode(port->pd_context, timings,
+				1<<PIPE(display)->pipe_num);
+		}
+		#else
+			ret = port->pd_driver->post_set_mode(port->pd_context, timings,
+				1<<PIPE(display)->pipe_num);
+		#endif
+		if (ret) {
+			EMGD_ERROR_EXIT("PD post_set_mode returned: 0x%x", ret);
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ *
+ * @param display
+ * @param port_number
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+int program_port_sdvo_tnc(igd_display_context_t *display,
+		unsigned short port_number,
+		unsigned long status)
+{
+	unsigned long port_control;
+	unsigned long pd_powerstate = PD_POWER_MODE_D3;
+	unsigned long preserve = 0;
+	unsigned long upscale = 0;
+	igd_timing_info_t  local_timing;
+	igd_timing_info_t  *timing;
+	unsigned long temp;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program Port: (%s)", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("pd_flags: 0x%lx", PORT(display, port_number)->pd_flags);
+
+	timing = PIPE(display)->timing;
+
+	port_control = preserve & READ_MMIO_REG_TNC(IGD_PORT_SDVO,
+			PORT(display, port_number)->port_reg);
+
+	if (status == TRUE) {
+		if (!(PORT(display, port_number)->pt_info->flags &
+			IGD_DISPLAY_ENABLE)) {
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+
+		/* Enable VGA syncs for native vga modes */
+		if (PORT(display, port_number)->vga_sync == 1) {
+			EMGD_DEBUG("VGA sync true, is width x height 720 x 400?");
+			if((timing->width == 720) && (timing->height == 400)) {
+				EMGD_DEBUG("Modify port control and multi_port_control");
+				port_control |= (1L<<15);
+			}
+		}
+
+		/* Fact that both IGD_ powerstates and PD_ powermodes have
+		 * same definitions */
+		pd_powerstate = GET_DISPLAY_POWER_STATE(display, port_number);
+
+		if (pd_powerstate == IGD_POWERSTATE_D0) {
+			EMGD_DEBUG("Power State: D0");
+			/* Upscale */
+			pi_pd_find_attr_and_value(PORT(display, port_number),
+				PD_ATTR_ID_PANEL_FIT,
+				0, /*no PD_FLAG for UPSCALING */
+				NULL, /* dont need the attr ptr*/
+				&upscale);
+
+			/* Reach the end timing if upscaling is enabled */
+			if (timing->extn_ptr && upscale) {
+				timing = (pd_timing_t *)timing->extn_ptr;
+			}
+
+			local_timing = *timing;
+			if (upscale) {
+				/* For timings smaller than width 360 and height 200,
+				 * double the size. This is because the active area of the mode
+				 * is double the size of the resolution for these modes
+				 *  - Very tricky huh */
+				if (local_timing.width <= 360) {
+					local_timing.width <<= 1;
+				}
+				if (local_timing.height <= 200) {
+					local_timing.height <<= 1;
+				}
+			}
+
+			/* BIT31 - Enable
+			 * BIT30 - PIPE B
+			 * BIT29 - Stall
+			 * BIT7  - Border
+			 */
+			port_control |= BIT31|BIT30|BIT29|BIT7;
+
+			/* Program cDVO registers:
+			 * Keep default values for
+			 *     7000h - cDVO control register
+			 *     7004h - cDVO slew rate register
+			 *     7008h - cDVO strength register
+			 *     700Ch - cDVO RCOMP update register
+			 *     6102Ch - cDVO stall register = 0xA.
+			 * Note: Though EAS says 6102Ch default value is 6, it is a typo
+			 *     in the spec, based on Si DE hw default value is 10 (0xA),
+			 *     so no need to program explicitly. This saves few bytes for
+			 *     micro.
+			 */
+
+			/* Enable Current Source */
+			temp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG);
+			temp |= 0x2000;
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, SDVO_BUFF_CTRL_REG, temp);
+		}
+	}
+
+	if (pd_powerstate == PD_POWER_MODE_D0) {
+		ret = PORT(display, port_number)->pd_driver->set_mode(
+			PORT(display, port_number)->pd_context, &local_timing, 0);
+	} else {
+		ret = PORT(display, port_number)->pd_driver->set_power(
+			PORT(display, port_number)->pd_context, pd_powerstate);
+	}
+
+	if (ret) {
+		EMGD_ERROR_EXIT("PD set_%s returned: 0x%x",
+			(pd_powerstate == PD_POWER_MODE_D0)?"mode":"power", ret);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_DEBUG("Port_control: 0x%lx", port_control);
+
+	WRITE_MMIO_REG_TNC(IGD_PORT_SDVO,
+		PORT(display, port_number)->port_reg, port_control);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param display
+ * @param port_number
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+int program_port_lvds_tnc(igd_display_context_t *display,
+		unsigned short port_number,
+		unsigned long status)
+{
+	int ret = 0;
+	unsigned long powerstate = PD_POWER_MODE_D3;
+	pd_timing_t *timing;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Program LVDS: (%s)", status?"ENABLE":"DISABLE");
+
+	if (status == TRUE) {
+		if(!(PORT(display, port_number)->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+
+		powerstate = GET_DISPLAY_POWER_STATE(display,port_number);
+		if (powerstate == IGD_POWERSTATE_D0) {
+			EMGD_DEBUG("Power State: D0");
+			timing = (pd_timing_t *)PIPE(display)->timing;
+			/* Reach end timing to get user resolution and pass it to pd */
+			if(timing->extn_ptr) {
+				timing = (pd_timing_t *)timing->extn_ptr;
+			}
+			/* set mode will take care of port control */
+			ret = PORT(display, port_number)->pd_driver->set_mode(
+					PORT(display, port_number)->pd_context,
+					timing,
+					1<<PIPE(display)->pipe_num);
+		}
+	}
+
+	/* either status == FALSE, or status == TRUE, but powerstate is D1/D2/D3 */
+	if (powerstate != IGD_POWERSTATE_D0) {
+		ret = PORT(display, port_number)->pd_driver->set_power(
+			PORT(display, port_number)->pd_context,
+			PD_POWER_MODE_D3);
+	}
+
+	if (ret) {
+		EMGD_ERROR_EXIT("PD set_%s returned: 0x%x",
+			(powerstate == IGD_POWERSTATE_D0)?"mode":"power", ret);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+/*!
+ *
+ * @param display
+ * @param port_number
+ * @param status
+ *
+ * @return program_port_lvds_gen4()
+ * @return program_port_sdvo_gen4()
+ * @return -IGD_ERROR_INVAL on failure
+ */
+int program_port_tnc(igd_display_context_t *display,
+		unsigned short port_number,
+		unsigned long status)
+{
+	EMGD_TRACE_ENTER;
+
+	if (PORT(display, port_number)->port_type == IGD_PORT_LVDS) {
+		EMGD_TRACE_EXIT;
+		return program_port_lvds_tnc(display, port_number, status);
+	} else {
+		EMGD_TRACE_EXIT;
+		return program_port_sdvo_tnc(display, port_number, status);
+	}
+}
+
+/*!
+ *
+ * @param gpio
+ *
+ * @return size
+ */
+unsigned long get_gpio_sets_tnc(unsigned long **gpio)
+{
+	/* To small to trace */
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param in_list
+ *
+ * @return void - To small to trace
+ */
+void filter_modes_tnc(igd_context_t *context, igd_display_port_t *port,
+	pd_timing_t *in_list)
+{
+	while (in_list->width != IGD_TIMING_TABLE_END) {
+		/* TC LVDS:
+		 *    supports from 19.75MHz to 79.MHz
+		 * TC SDVO:
+		 *    supports from 25 MHz to 160 MHz and progressive only.
+		 */
+		if (port->port_type == IGD_PORT_SDVO) {
+			if (in_list->mode_info_flags & IGD_SCAN_INTERLACE ||
+				in_list->dclk < 25000 || in_list->dclk > 160000) {
+				in_list->mode_info_flags &= ~IGD_MODE_SUPPORTED;
+			}
+		}
+		/* No clock check is required for LVDS port as LVDS port driver
+		 * already taken care of this.
+		if (port->port_type == IGD_PORT_LVDS) {
+			if (in_list->dclk < 19750 || in_list->dclk > 79500) {
+				in_list->mode_info_flags &= ~IGD_MODE_SUPPORTED;
+			}
+		}*/
+		in_list++;
+		if (in_list->width == IGD_TIMING_TABLE_END && in_list->extn_ptr) {
+			in_list = in_list->extn_ptr;
+		}
+	}
+	return;
+}
+#ifndef CONFIG_MICRO
+int get_timing_tnc(igd_display_context_t *display, pd_timing_t *in_list)
+{
+	//int ret = TRUE;
+	igd_timing_info_t *pTimings_ori;
+	pd_timing_t *timing = NULL;
+	timing = in_list;
+
+	EMGD_TRACE_ENTER;
+	pTimings_ori = PIPE(display)->timing;
+
+	if(display->port_number != 2){
+		EMGD_DEBUG("Port does not need tuning");
+		EMGD_TRACE_EXIT;
+		return FALSE;
+	}
+
+	while(timing->width != PD_TIMING_LIST_END) {
+
+		if((timing->width == pTimings_ori->width) &&
+		   (timing->height == pTimings_ori->height) &&
+		   (timing->refresh == pTimings_ori->refresh)){
+			EMGD_DEBUG("Timing found");
+			timing->reserved_dd = pTimings_ori->reserved_dd;
+			timing->mode_info_flags = pTimings_ori->mode_info_flags;
+
+           /* also return the hblank_end so that ioctl can calculate
+            * the xblank_length.
+            */
+            timing->hblank_end = pTimings_ori->hblank_end;
+            timing->vblank_end = pTimings_ori->vblank_end;
+
+
+		}
+		/* Go through the table list */
+		timing++;
+		if ((timing->width == PD_TIMING_LIST_END) && timing->extn_ptr) {
+				timing = timing->extn_ptr;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return TRUE;
+}
+
+int check_port_supported(void *port_tmp)
+{
+	//igd_display_port_t *port = (igd_display_port_t *)port_tmp;
+	/* Determine which port driver is supported by Atom E6xx */
+#if 0 //Pendng confirmation from LNC architect
+	if(pd_driver->type & PD_DISPLAY_TVOUT){
+		return 1;
+	}
+#endif
+	return 0;
+}
+#endif
+
+/* Function checks if the incoming user DTD is one of the timings
+ * in the crt_timing_table that has border. Meaning that the
+ * h_blank_end is not the same as htotal.
+ * We only see this issue when reading a user DTD generated for
+ * harmonic which is derived from crt_timing_table. pi module
+ * only handles user DTD which does not have borders.
+ */
+int get_refresh_in_border(pd_timing_t *in_list)
+{
+       /* return 1 if refresh is obtained */
+
+       if(in_list->width == 640 &&
+               in_list->height == 480 &&
+               in_list->dclk == 25175 &&
+               in_list->hsync_start == 655 &&
+               in_list->hsync_end == 751 &&
+               in_list->vsync_start == 489 &&
+               in_list->vsync_end == 491){
+                       in_list->refresh = 60;
+                       in_list->htotal = 799;
+                       in_list->vtotal = 524;
+                       in_list->hblank_start = 646;
+                       in_list->vblank_start = 486;
+                       return 1;
+       }
+
+       if(in_list->width == 640 &&
+               in_list->height == 480 &&
+               in_list->dclk == 31500 &&
+               in_list->hsync_start == 663 &&
+               in_list->hsync_end == 703 &&
+               in_list->vsync_start == 488 &&
+               in_list->vsync_end == 491){
+
+                       in_list->refresh = 72;
+                       in_list->htotal = 831;
+                       in_list->vtotal = 519;
+                       in_list->hblank_start = 646;
+                       in_list->vblank_start = 486;
+                       return 1;
+       }
+
+
+       return 0;
+}
+
+/*
+ * Function to check if dc is switching from
+ * SDVO single to clone/extended mode
+ * Tunnel Creek B0  needs tuning when there are 2 planes,
+ * if true, it will force alter to run tuning
+ */
+bool dsp_is_force_alter_required_tnc(igd_display_context_t *display,
+	unsigned long current_dc, unsigned long dc_to_set){
+
+	if ((IGD_DC_SINGLE(current_dc) &&
+			(IGD_DC_PRIMARY(current_dc) == IGD_PORT_TYPE_SDVOB )) &&
+			(IGD_DC_CLONE(dc_to_set) || IGD_DC_EXTENDED(dc_to_set)) &&
+			(display->context->device_context.rid == TNC_B0_RID)){
+
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
+mode_dispatch_t mode_dispatch_tnc = {
+	igd_set_palette_entry_tnc,
+	igd_get_palette_entry_tnc,
+	igd_wait_vblank_tnc,
+	program_plane_tnc,
+	program_pipe_tnc,
+	program_port_tnc,
+	post_program_port_tnc,
+	program_clock_tnc,
+	program_cdvo_tnc,
+	reset_plane_pipe_ports_tnc,
+	get_gpio_sets_tnc,
+	filter_modes_tnc,
+	OPT_MICRO_VALUE(check_display_tnc, NULL),
+	OPT_MICRO_VALUE(get_timing_tnc, NULL),
+	OPT_MICRO_VALUE(check_port_supported, NULL),
+	OPT_MICRO_VALUE(get_refresh_in_border, NULL),
+	OPT_MICRO_VALUE(dsp_is_force_alter_required_tnc, NULL),
+	OPT_MICRO_VALUE(&mode_full_dispatch_tnc, NULL),
+};
+
+/* VBIOS does not use the virtual mapped address
+ * This function will return 0 for VBIOS anyway */
+#ifndef CONFIG_MICRO
+unsigned char *get_mmio_tnc(unsigned long port_type)
+{
+	unsigned char *virt_mmio;
+	if (port_type == IGD_PORT_LVDS) {
+		virt_mmio = mode_context->context->device_context.virt_mmadr;
+	} else if (port_type == IGD_PORT_SDVO) {
+		virt_mmio = mode_context->context->device_context.virt_mmadr_sdvo;
+	} else if (port_type == IGD_PORT_SDVO_ST) {
+		virt_mmio = mode_context->context->device_context.virt_mmadr_sdvo_st;
+	} else if (port_type == IGD_PORT_SDVO_ST_GPIO) {
+		virt_mmio = mode_context->context->device_context.virt_mmadr_sdvo_st_gpio;
+	} else {
+		/* i2c_bitbash will use the else condition by passing in port_type
+		 * as '0' */
+		virt_mmio = mode_context->context->device_context.virt_gpio_bar;
+	}
+	return virt_mmio;
+}
+#endif
+
+/* Function to read Atom E6xx 0:2:0, 0:3:0 and 0:31:0 mmio registers */
+unsigned long read_mmio_reg_tnc(unsigned long port_type, unsigned long reg)
+{
+	unsigned long value;
+#ifndef CONFIG_MICRO
+	unsigned char *mmio;
+#endif
+
+	/* to avoid updating or having another set of read/write macros for
+	 * vbios, overwrite io_base to properly read from 0:2:0/0:3:0/0:31:0
+	 * io_base and set it back after reading */
+	if (port_type == IGD_PORT_LVDS) {
+		io_base = io_base_lvds;
+	} else if (port_type == IGD_PORT_SDVO) {
+		io_base = io_base_sdvo;
+	} else if (port_type == IGD_PORT_LPC) {
+		io_base = io_base_lpc;
+	} else if (port_type == IGD_PORT_SDVO_ST) {
+		io_base = io_base_sdvo_st;
+	}
+	OPT_MICRO_CALL_RET(mmio, get_mmio_tnc(port_type));
+	if (port_type == IGD_PORT_LPC) {
+		value = EMGD_READ_PORT32(io_base_lpc + reg);
+	} else {
+		value = EMGD_READ32(EMGD_MMIO(mmio) + reg);
+	}
+
+	io_base = io_base_lvds;
+	return value;
+}
+
+/* Function to write Atom E6xx 0:2:0 and 0:3:0 mmio registers */
+void write_mmio_reg_tnc(unsigned long port_type, unsigned long reg,
+	unsigned long value)
+{
+#ifndef CONFIG_MICRO
+	unsigned char *mmio;
+#endif
+
+	/* to avoid updating or having another set of read/write macros for
+	 * vbios, overwrite io_base to properly read from 0:2:0/0:3:0 io_base
+	 * and set it back after writing */
+	if (port_type == IGD_PORT_LVDS) {
+		io_base = io_base_lvds;
+	} else if (port_type == IGD_PORT_SDVO) {
+		io_base = io_base_sdvo;
+	} else if (port_type == IGD_PORT_LPC) {
+		io_base = io_base_lpc;
+	} else if (port_type == IGD_PORT_SDVO_ST) {
+		io_base = io_base_sdvo_st;
+	}
+
+	OPT_MICRO_CALL_RET(mmio, get_mmio_tnc(port_type));
+	if (port_type == IGD_PORT_LPC) {
+		EMGD_WRITE_PORT32(io_base_lpc + reg, value);
+	} else {
+		EMGD_WRITE32(value, EMGD_MMIO(mmio) + reg);
+	}
+	io_base = io_base_lvds;
+	return;
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.c b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.c
new file mode 100644
index 0000000..2f22ef6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.c
@@ -0,0 +1,2074 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_tnc.c
+ * $Revision: 1.35 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Atom E6xx implementations for the mode dispatch functions.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+#include <io.h>
+
+#include <igd.h>
+#include <igd_pwr.h>
+
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+#include <dsp.h>
+#include <intelpci.h>
+#include <mode_access.h>
+#include <rb.h>
+#include <pi.h>
+#include <math_fix.h>
+
+#include <tnc/regs.h>
+#include <tnc/context.h>
+#include <tnc/mi.h>
+#include <tnc/instr.h>
+#include <tnc/cmd.h>
+
+#include "drm_emgd_private.h"
+#include "../cmn/match.h"
+#include "../cmn/mode_dispatch.h"
+
+#include "emgd_shared.h"
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <drm/drmP.h>
+
+
+/* Get this table from clocks_tnc.c, use this in get_pipe_info */
+extern unsigned long lvds_m_converts[];
+extern unsigned long LVDS_M_CONVERTS_LEN;
+
+/* This is a duplicate define of the same constant in clocks_tnc.c.
+ * Redefined it here because there's no good TNC-specific common header
+ * file to put this in  */
+#define LVDS_M_MIN 10
+
+
+/* Registered VBlank interrupt callbacks (one-per-pipe): */
+static emgd_vblank_callback_t interrupt_callbacks_tnc[IGD_MAX_PORTS] =
+	{{NULL,NULL,0},{NULL,NULL,1},{NULL,NULL,2},{NULL,NULL,3},{NULL,NULL,4}};
+
+/* This variable contains a number of bits, which tell whether the interrupt
+ * code has enabled interrupts, and if so, for what software and for what
+ * port(s); and whether certain events have happened.
+ */
+static unsigned long vblank_interrupt_state = 0;
+
+/* This variables keeps track of the number of clients currently using
+ * the vblank interrupt
+ */
+static int vblank_interrupt_ref_cnt_port2 = 0;
+static int vblank_interrupt_ref_cnt_port4 = 0;
+
+/* Spin lock for synchronization of the vblank_interrupt_state variable,
+ * between the VBlank interrupt handler and the non-interrupt handler code:
+ */
+DEFINE_SPINLOCK(vblank_lock_tnc);
+
+
+
+int set_flip_pending_tnc(unsigned char *mmio, unsigned long pipe_status_reg);
+int check_flip_pending_tnc(unsigned char *mmio, unsigned long pipe_status_reg);
+
+/* KMS callback from emgd_crtc.c */
+int crtc_pageflip_handler(struct drm_device *dev, int port);
+void notify_userspace_vblank(struct drm_device *dev, int port);
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+int mode_get_stride_stereo_tnc(igd_display_context_t *display,
+	unsigned long *stride, unsigned long *stereo, unsigned long flags);
+
+
+
+/*!
+ *
+ * @param display
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL if color attributes not found
+ */
+static int set_color_correct_tnc(igd_display_context_t *display)
+{
+	const int        MID_PIXEL_VAL    = 125;
+	const int        MAX_PIXEL_VAL    = 255;
+	const int        NUM_PALETTE_ENTRIES = 256;
+
+	unsigned int     gamma_r_max_24i_8f, gamma_r_min_24i_8f;
+	unsigned int     gamma_g_max_24i_8f, gamma_g_min_24i_8f;
+	unsigned int     gamma_b_max_24i_8f, gamma_b_min_24i_8f;
+	unsigned int     new_gamma_r_24i_8f, new_gamma_g_24i_8f;
+	unsigned int     new_gamma_b_24i_8f;
+	unsigned int     gamma_normal_r_24i_8f, gamma_normal_g_24i_8f;
+	unsigned int     gamma_normal_b_24i_8f;
+	int              brightness_factor_r, brightness_factor_g;
+	int              brightness_factor_b;
+	int              contrast_factor_r, contrast_factor_g;
+	int              contrast_factor_b;
+
+	unsigned int      *palette;
+	unsigned int      i;
+
+	igd_range_attr_t *gamma_attr      = NULL, *contrast_attr = NULL;
+	igd_range_attr_t *brightness_attr = NULL;
+	igd_attr_t       *hal_attr_list  = PORT_OWNER(display)->attributes;
+
+	EMGD_TRACE_ENTER;
+
+	/* Using OS_ALLOC to avoid using > 1024 on stack (frame size warning ) */
+	palette = OS_ALLOC(sizeof (unsigned int) * NUM_PALETTE_ENTRIES);
+
+	/* start with a fresh palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		palette[i] = (i << 16) | (i << 8) | i;
+	}
+
+	/* get a pointer to gamma, contrast, and brightness attr */
+	i = 0;
+
+	while (PD_ATTR_LIST_END != hal_attr_list[i].id) {
+		switch (hal_attr_list[i].id) {
+		case PD_ATTR_ID_FB_GAMMA:
+			gamma_attr      = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_BRIGHTNESS:
+			brightness_attr = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		case PD_ATTR_ID_FB_CONTRAST:
+			contrast_attr   = (igd_range_attr_t *) &hal_attr_list[i];
+			break;
+
+		default:
+			break;
+		}
+
+		i++;
+	}
+
+	if(!gamma_attr || !brightness_attr || !contrast_attr) {
+		EMGD_ERROR_EXIT("Color Correction Atrributes not found!");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Get the max and min */
+	gamma_r_max_24i_8f = ((gamma_attr->max >> 16) & 0xFF) << 3;
+	gamma_g_max_24i_8f = ((gamma_attr->max >>  8) & 0xFF) << 3;
+	gamma_b_max_24i_8f =  (gamma_attr->max        & 0xFF) << 3;
+
+	gamma_r_min_24i_8f = ((gamma_attr->min >> 16) & 0xFF) << 3;
+	gamma_g_min_24i_8f = ((gamma_attr->min >>  8) & 0xFF) << 3;
+	gamma_b_min_24i_8f =  (gamma_attr->min        & 0xFF) << 3;
+
+	/* The new gamma values are in 3i.5f format, but we must convert it
+	 * to 24i.8f format before passing it to OS_POW_FIX
+	 */
+	new_gamma_r_24i_8f = ((gamma_attr->current_value >> 16) & 0xFF) << 3;
+	new_gamma_g_24i_8f = ((gamma_attr->current_value >> 8) & 0xFF) << 3;
+	new_gamma_b_24i_8f = (gamma_attr->current_value & 0xFF) << 3;
+
+	/* make sure the new gamma is within range */
+	new_gamma_r_24i_8f = OS_MIN(gamma_r_max_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_r_24i_8f = OS_MAX(gamma_r_min_24i_8f, new_gamma_r_24i_8f);
+	new_gamma_g_24i_8f = OS_MIN(gamma_g_max_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_g_24i_8f = OS_MAX(gamma_g_min_24i_8f, new_gamma_g_24i_8f);
+	new_gamma_b_24i_8f = OS_MIN(gamma_b_max_24i_8f, new_gamma_b_24i_8f);
+	new_gamma_b_24i_8f = OS_MAX(gamma_b_min_24i_8f, new_gamma_b_24i_8f);
+
+
+	gamma_normal_r_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_r_24i_8f);
+
+	gamma_normal_g_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_g_24i_8f);
+
+	gamma_normal_b_24i_8f =
+		OS_POW_FIX(MAX_PIXEL_VAL, (1<<16)/new_gamma_b_24i_8f);
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		unsigned int new_gamma;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* Note that we do not try to calculate the gamma if it
+		 * is 1.0, e.g. 0x100.  This is to avoid round-off errors
+		 */
+
+		/* red: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_r_24i_8f) {
+			cur_color  = (cur_palette >> 16) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_r_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_r_24i_8f;
+			palette[i] &= 0x00FFFF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_g_24i_8f) {
+			cur_color  = (cur_palette >> 8) & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_g_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_g_24i_8f;
+			palette[i] &= 0xFF00FF;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0x100 != new_gamma_b_24i_8f) {
+			cur_color  = cur_palette & 0xFF;
+			new_gamma  = OS_POW_FIX(cur_color, (1<<16)/new_gamma_b_24i_8f);
+			new_gamma  = (MAX_PIXEL_VAL * new_gamma)/gamma_normal_b_24i_8f;
+			palette[i] &= 0xFFFF00;
+			palette[i] |=
+				(OS_MIN(new_gamma, (unsigned) MAX_PIXEL_VAL) & 0xFF);
+		}
+	}
+
+
+	/* Brightness correction */
+	brightness_factor_r = (brightness_attr->current_value >> 16) & 0xFF;
+	brightness_factor_g = (brightness_attr->current_value >> 8) & 0xFF;
+	brightness_factor_b = brightness_attr->current_value & 0xFF;
+
+	/* The factors are offset by 0x80 because 0x80 is 0 correction */
+	brightness_factor_r -= 0x80;
+	brightness_factor_g -= 0x80;
+	brightness_factor_b -= 0x80;
+
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int          new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 16) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_r;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0x00FFFF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+
+		/* green: calculate and make sure the result is within range */
+		cur_color     =  (cur_palette >> 8) & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_g;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFF00FF;
+		palette[i]    |= (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+
+		/* blue: calculate and make sure the result is within range */
+		cur_color     =  cur_palette & 0xFF;
+		new_pixel_val =  cur_color + brightness_factor_b;
+		new_pixel_val =  OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+		palette[i]    &= 0xFFFF00;
+		palette[i]    |= OS_MAX(new_pixel_val, 0) & 0xFF;
+	}
+
+
+	/* contrast correction */
+	contrast_factor_r = (contrast_attr->current_value >> 16) & 0xFF;
+	contrast_factor_g = (contrast_attr->current_value >> 8) & 0xFF;
+	contrast_factor_b = contrast_attr->current_value & 0xFF;
+
+	/* make sure values are within range */
+	contrast_factor_r -= 0x80;
+	contrast_factor_g -= 0x80;
+	contrast_factor_b -= 0x80;
+
+
+	/* We're doing integer division in this loop using 16i.16f
+	 * integers.  The result will then be converted back into a
+	 * regular, 32-bit integer
+	 */
+	for( i = 0; i < NUM_PALETTE_ENTRIES; i++ ) {
+		int new_pixel_val;
+		unsigned int cur_color;
+		unsigned int cur_palette = palette[i];
+
+		/* red: calculate and make sure the result is within range */
+		if (0 != contrast_factor_r ) {
+			cur_color     = (cur_palette >> 16) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_r);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0x00FFFF;  /* clear out the R color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 16;
+		}
+
+		/* green: calculate and make sure the result is within range */
+		if (0 != contrast_factor_g ) {
+			cur_color     = (cur_palette >> 8) & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_g);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFF00FF;  /* clear out the G color */
+			palette[i]    |=  (OS_MAX(new_pixel_val, 0) & 0xFF) << 8;
+		}
+
+		/* blue: calculate and make sure the result is within range */
+		if (0 != contrast_factor_b) {
+			cur_color     = cur_palette & 0xFF;
+			new_pixel_val =
+				(MAX_PIXEL_VAL << 16) / (MAX_PIXEL_VAL - contrast_factor_b);
+			new_pixel_val =   new_pixel_val * (cur_color - MID_PIXEL_VAL);
+			new_pixel_val >>= 16;  /* convert back to 32i format */
+			new_pixel_val +=  MID_PIXEL_VAL;
+			new_pixel_val =   OS_MIN(new_pixel_val, MAX_PIXEL_VAL);
+			palette[i]    &=  0xFFFF00;  /* clear out the B color */
+			palette[i]    |=   OS_MAX(new_pixel_val, 0) & 0xFF;
+		}
+	}
+
+
+	/* write the new values in the palette */
+	for (i = 0; i < NUM_PALETTE_ENTRIES; i++) {
+		/* SDVO palette register is not accesible */
+		EMGD_WRITE32(palette[i],
+			MMIO_TNC(IGD_PORT_LVDS) +
+			PIPE(display)->palette_reg + i*4);
+	}
+
+	OS_FREE(palette);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param display
+ * @param fb
+ * @param x
+ * @param y
+ *
+ * @return 0
+ */
+static int set_display_base_tnc(igd_display_context_t *display,
+	igd_framebuffer_info_t *fb, unsigned long *x, unsigned long *y)
+{
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG ("Pan linear to (%lu,%lu)", *x, *y);
+
+	/* Update framebuffer's visible offset */
+	PLANE(display)->fb_info->visible_offset =
+		((*y * fb->screen_pitch) + (*x * IGD_PF_BYPP(fb->pixel_format)));
+
+	/* Plane registers are always on 0:2:0 */
+	WRITE_MMIO_REG(display, PLANE(display)->plane_reg + DSP_LINEAR_OFFSET,
+		PLANE(display)->fb_info->visible_offset);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This function alters the position parameters associated with a cursor.
+ *
+ * @param display_handle
+ * @param cursor_info
+ *
+ * @return 0
+ */
+static int igd_alter_cursor_pos_tnc(igd_display_h display_handle,
+	igd_cursor_info_t *cursor_info)
+{
+	unsigned long cursor_reg;
+	unsigned long new_pos;
+	unsigned long cursor_base;
+
+	igd_display_context_t *display = (igd_display_context_t *) display_handle;
+	cursor_reg = PIPE(display)->cursor->cursor_reg;
+
+	/* To Fast For Tracing */
+
+	/* Plane registers are always on 0:2:0 device */
+	if (0x27 & READ_MMIO_REG(display, cursor_reg)) {
+		/* unlike almador, for Gen4, u must program the base offset
+		to trigger the position update. However, this also means we
+		accidentally enable an invalid cursor surface if the cursor
+		was not enabled already. So do this check first */
+
+		/*
+		 * Encode the cursor position in the format required for the
+		 * cursor position register.
+		 */
+		if(cursor_info->y_offset >= 0) {
+			new_pos = (cursor_info->y_offset << 16);
+		} else {
+			new_pos = ((-(cursor_info->y_offset)) << 16) | 0x80000000;
+		}
+		if(cursor_info->x_offset >= 0) {
+			new_pos |= (cursor_info->x_offset);
+		} else {
+			new_pos |= (-(cursor_info->x_offset)) | 0x00008000;
+		}
+
+		cursor_base = READ_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET);
+
+		WRITE_MMIO_REG(display, cursor_reg + CUR_POS_OFFSET, new_pos);
+		WRITE_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET, cursor_base);
+	}
+
+	return 0;
+}
+
+/*!
+ * The assumption here is that palette_colors points to index 0 and
+ * this function indexes into the palette_colors array by start_index
+ *
+ * @param display_handle
+ * @param palette_colors
+ * @param start_index
+ * @param count
+ *
+ * @return 0
+ */
+static int igd_set_palette_entries_tnc(
+		igd_display_h display_handle,
+		unsigned long *palette_colors,
+		unsigned int start_index,
+		unsigned int count)
+{
+	unsigned int i;
+
+	EMGD_TRACE_ENTER;
+
+	for(i=start_index; i<start_index+count; i++) {
+		/* Palette can is only on 0:2:0 so use _TNC IGD_PORT_LVDS */
+		WRITE_MMIO_REG_TNC(IGD_PORT_LVDS,
+			PIPE(display_handle)->palette_reg + i*4, palette_colors[i]);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * This procedure waits for the next vertical sync
+ * period. If the display is already in a vertical sync period, this
+ * procedure exits.
+ *
+ * Note: A timeout is included to prevent an endless loop.
+ *
+ * @param display_handle
+ * @param palette_colors
+ * @param start_index
+ * @param count
+ *
+ * @return FALSE - if timed out
+ */
+static int igd_wait_vsync_tnc(igd_display_h display_handle)
+{
+	unsigned long tmp;
+	unsigned long status_reg;
+	os_alarm_t timeout;
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	status_reg = PIPE(display)->pipe_reg + PIPE_STATUS_OFFSET;
+
+	/* If pipe is off then just return */
+	if(!((1<<31) & READ_MMIO_REG_TNC(PORT_TYPE(display),
+		PIPE(display)->pipe_reg))) {
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	/* 1. Disable VSync interrupt */
+	tmp = READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	WRITE_MMIO_REG_TNC(PORT_TYPE(display), status_reg, tmp & ~(1<<25));
+
+	/* 2. Clear interrupt status (by writing a 1) */
+	tmp = READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	WRITE_MMIO_REG_TNC(PORT_TYPE(display), status_reg, tmp | (1<<9));
+
+	/* 3. Enable VSync interrupt */
+	tmp = READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	WRITE_MMIO_REG_TNC(PORT_TYPE(display), status_reg, tmp | (1<<25));
+	READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+
+	/* 4. Wait for VSync about 50 msec (20Hz) */
+	timeout = OS_SET_ALARM(50);
+	do {
+		OS_SCHEDULE();
+		/* Check for timeout */
+	} while (((READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg) & (1<<9)) ==
+			0x00) &&
+		(!OS_TEST_ALARM(timeout)));
+
+	if (((READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg) & (1<<9)) ==
+			0x00)) {
+		EMGD_ERROR_EXIT("Timeout waiting for VSYNC");
+		ret = 0;
+	} else {
+		ret = 1;
+	}
+
+	/* 5. Disable VSync interrupt */
+	tmp = READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	WRITE_MMIO_REG_TNC(PORT_TYPE(display), status_reg, tmp & ~(1<<25));
+
+	/* 6. Clear interrupt status (by writing a 1) */
+	tmp = READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+	tmp = tmp & (~PIPESTAT_STS_BITS);
+	WRITE_MMIO_REG_TNC(PORT_TYPE(display), status_reg, tmp | (1<<9));
+	READ_MMIO_REG_TNC(PORT_TYPE(display), status_reg);
+
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}  /* igd_wait_vsync*/
+
+/*!
+ *
+ * @param display_handle
+ * @param scanline
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_get_scanline_tnc(igd_display_h display_handle, int *scanline)
+{
+	unsigned int tmp;
+	unsigned long reg;
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	unsigned long fb_height = PLANE(display)->fb_info->height;
+	unsigned long dp_height = PIPE(display)->timing->height;
+
+	EMGD_TRACE_ENTER;
+
+	/* Scanline reg is -8 from control reg */
+	reg = PIPE(display)->pipe_reg - 0x8;
+
+	tmp = READ_MMIO_REG_TNC(PORT_TYPE_DH(display_handle), reg);
+
+	if(!(PORT_OWNER(display)->pt_info->flags & IGD_DISPLAY_ENABLE)) {
+		EMGD_DEBUG("Port off, can not get scanline");
+		EMGD_TRACE_EXIT;
+		return -IGD_ERROR_INVAL;
+	}
+
+	tmp = (tmp * fb_height) / dp_height;
+
+	if(tmp >= fb_height) {
+		*scanline = IGD_IN_VBLANK;
+	} else {
+		*scanline = (int)tmp;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end igd_get_scanline() */
+
+/*!
+ *
+ * @param display_handle
+ *
+ * @return 1 if TRUE
+ * @return 0 if FALSE
+ */
+static int igd_query_in_vblank_tnc(igd_display_h display_handle)
+{
+	int sl;
+
+	/* To small for Tracing */
+
+	igd_get_scanline_tnc(display_handle, &sl);
+	if (sl == IGD_IN_VBLANK) {
+		return 1; /*TRUE*/
+	} else {
+		return 0; /*FALSE*/
+	}
+}
+
+/*!
+ * This function programs the cursor registers for Grantsdale
+ *
+ * @param display
+ * @param status
+ *
+ * @return void
+ */
+static void program_cursor_tnc(igd_display_context_t *display,
+	unsigned long status)
+{
+	unsigned long cursor_reg;
+	unsigned long cursor_control = 0x00000000;
+	unsigned long cursor_pos;
+	unsigned long cursor_base;
+	igd_cursor_info_t *cursor_info;
+	int i;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Program Cursor: %s", status?"ENABLE":"DISABLE");
+	EMGD_DEBUG("Device power state: D%ld", GET_DEVICE_POWER_STATE(display));
+
+	cursor_reg = PIPE(display)->cursor->cursor_reg;
+	cursor_info = PIPE(display)->cursor->cursor_info;
+
+	/* Turn off cursor before changing anything */
+	/* planes are always on 0:2:0 device, so no need to use _TNC macros */
+	cursor_base = READ_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET);
+
+	WRITE_MMIO_REG(display, cursor_reg, cursor_control);
+	WRITE_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET, cursor_base);
+
+	if(cursor_info->flags & IGD_CURSOR_GAMMA) {
+		cursor_control |= BIT26;
+	}
+
+	cursor_info->argb_pitch = 64*4;
+	cursor_info->xor_pitch = 16;
+
+	/* Setting the cursor format/pitch */
+	switch(cursor_info->pixel_format) {
+	case IGD_PF_ARGB32:
+		cursor_control |= BIT5 | 0x7;
+		break;
+	case IGD_PF_RGB_XOR_2:
+		cursor_control |= 0x5;
+		break;
+	case IGD_PF_RGB_T_2:
+		cursor_control |= 0x4;
+		break;
+	case IGD_PF_RGB_2:
+		cursor_control |= 0x6;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid Pixel Format");
+		return;
+	}
+
+	switch(cursor_info->pixel_format) {
+	case IGD_PF_ARGB32:
+		cursor_base = cursor_info->argb_offset;
+		break;
+	default:
+		cursor_base = cursor_info->xor_offset;
+		break;
+	}
+
+	/* If status is FALSE return with the cursor off */
+	if((!status) || (GET_DEVICE_POWER_STATE(display) == IGD_POWERSTATE_D3)) {
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	if(cursor_info->y_offset >= 0) {
+		cursor_pos = cursor_info->y_offset << 16;
+	} else {
+		cursor_pos = ((-(cursor_info->y_offset)) << 16) | 0x80000000;
+	}
+	if(cursor_info->x_offset >= 0) {
+		cursor_pos |= cursor_info->x_offset;
+	} else {
+		cursor_pos |= (-(cursor_info->x_offset)) | 0x00008000;
+	}
+
+	WRITE_MMIO_REG(display, cursor_reg + CUR_POS_OFFSET,
+		cursor_pos);
+
+	for(i=0; i<4; i++) {
+		WRITE_MMIO_REG(display, cursor_reg + CUR_PAL0_OFFSET + i*4,
+			cursor_info->palette[i]);
+	}
+
+	cursor_control = cursor_control | (PIPE(display)->pipe_num<<28);
+
+	WRITE_MMIO_REG(display, cursor_reg, cursor_control);
+	WRITE_MMIO_REG(display, cursor_reg + CUR_BASE_OFFSET, cursor_base);
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param type
+ * @param surface
+ * @param appcontext
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_get_surface_tnc(igd_display_h display_handle,
+	igd_buffertype_t type,
+	igd_surface_t *surface,
+	igd_appcontext_h appcontext)
+{
+#if 0
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	state3d_tnc_t *state = STATE3D_TNC(appcontext);
+
+	EMGD_TRACE_ENTER;
+
+	if(!surface) {
+		EMGD_ERROR_EXIT("Surface is NULL");
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch(type) {
+	case IGD_BUFFER_DISPLAY:
+		surface->offset = PLANE(display)->fb_info->visible_offset;
+		surface->pitch = PLANE(display)->fb_info->screen_pitch;
+		surface->width = PLANE(display)->fb_info->width;
+		surface->height = PLANE(display)->fb_info->height;
+		surface->u_offset = 0;
+		surface->u_pitch = 0;
+		surface->v_offset = 0;
+		surface->v_pitch = 0;
+		surface->pixel_format = PLANE(display)->fb_info->pixel_format;
+		surface->palette_info = 0;
+		surface->flags = PLANE(display)->fb_info->flags;
+		surface->logic_ops = 0;
+		surface->render_ops = 0;
+		surface->alpha = 0;
+		surface->diffuse = 0;
+		surface->chroma_high = 0;
+		surface->chroma_low = 0;
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_COLOR:
+		OS_MEMCPY(surface, &state->color_buffer,
+			sizeof(igd_surface_t));
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_DEPTH:
+		OS_MEMCPY(surface, &state->depth_buffer,
+			sizeof(igd_surface_t));
+		EMGD_TRACE_EXIT;
+		return 0;
+	default:
+		EMGD_ERROR("Invalid type in get_surface");
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return -IGD_ERROR_INVAL;
+#else
+	return 0;
+#endif
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param type
+ * @param surface
+ * @param appcontext
+ * @param flags
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_set_surface_tnc(igd_display_h display_handle,
+	int priority,
+	igd_buffertype_t type,
+	igd_surface_t *surface,
+	igd_appcontext_h appcontext,
+	unsigned long flags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_handle;
+	unsigned int dsp_current;
+	unsigned long plane_reg;
+	unsigned long plane_control;
+	unsigned long visible_offset;
+
+	EMGD_TRACE_ENTER;
+	if(!surface) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch(type) {
+	case IGD_BUFFER_DISPLAY:
+		if(! (surface->flags & IGD_SURFACE_DISPLAY)) {
+			EMGD_ERROR_EXIT("Surface is not a display surface");
+			return -IGD_ERROR_INVAL;
+		}
+
+		if (PLANE(display)->plane_reg == DSPACNTR) {
+			EMGD_DEBUG("About to flip a buffer for display/pipe A");
+			dsp_current = 0;
+		} else {
+			EMGD_DEBUG("About to flip a buffer for display/pipe B");
+			dsp_current = 1;
+		}
+
+		if(flags & IGD_BUFFER_WAIT) {
+			/* If this is just a wait for flip, so return */
+			return 0;
+		}
+
+		/*
+		 * Async flips only work when the offset is on a 256kb boundary.
+		 */
+		if(surface->offset & 0x3ffff) {
+			EMGD_ERROR("Display surface offset %lu is not 256kb aligned", surface->offset);
+		}
+		
+		if (flags & IGD_BUFFER_NO_PAN) {
+			/* Do not pan. Set visible_offset to zero */
+			visible_offset = 0;
+		} else {
+			/* calculate the visible offset, taking panning into account */
+			visible_offset =
+				(PORT_OWNER(display)->pt_info->y_offset * surface->pitch) +
+				(PORT_OWNER(display)->pt_info->x_offset *
+					IGD_PF_BYPP(surface->pixel_format));
+		}
+		EMGD_DEBUG("visible surface_offset = 0x%08lx", visible_offset);
+
+		/* Save new fb_info */
+		PLANE(display)->fb_info->fb_base_offset = surface->offset;
+		PLANE(display)->fb_info->visible_offset = visible_offset;
+		PLANE(display)->fb_info->screen_pitch = surface->pitch;
+		PLANE(display)->fb_info->width = surface->width;
+		PLANE(display)->fb_info->height = surface->height;
+		PLANE(display)->fb_info->pixel_format = surface->pixel_format;
+		PLANE(display)->fb_info->flags = surface->flags;
+
+		/* Get the correct stride and stereo */
+		/* TODO - Does Atom E6xx flip need to handle stereo mode? */
+		/*mode_get_stride_stereo_tnc(display, &stride, &stereo, 0);*/
+
+		/* plane registers are always on 0:2:0, so no need to use _TNC macros */
+		plane_reg = PLANE(display)->plane_reg;
+		plane_control = EMGD_READ32(MMIO(display) + plane_reg);
+
+		/* Perform the flip by doing the following:
+		 *
+		 *   Write the current plane_control value to the plane_reg
+		 *   Write the surface stride to DSP_STRIDE_OFFSET
+		 *   Write the visible from start of plane to DSP_LINEAR_OFFSET
+		 *   Write the base surface offset to either:
+		 *     1) the plane_reg - 4  if async
+		 *     2) plane_reg + DSP_START_OFFSET (+0x1C) if not async
+		 */
+		EMGD_WRITE32(plane_control, MMIO(display) + plane_reg);
+		EMGD_WRITE32(surface->pitch,
+				MMIO(display) + plane_reg + DSP_STRIDE_OFFSET);
+		EMGD_WRITE32(visible_offset,
+			MMIO(display) + plane_reg + DSP_LINEAR_OFFSET);
+		EMGD_WRITE32(surface->offset,
+			MMIO(display) + plane_reg + DSP_START_OFFSET);
+
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_COLOR:
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_DEPTH:
+		EMGD_TRACE_EXIT;
+		return 0;
+	case IGD_BUFFER_SAVE:
+		PLANE(display)->fb_info->saved_offset = surface->offset;
+		EMGD_DEBUG("saving surface_offset = 0x%08lx", surface->offset);
+		EMGD_TRACE_EXIT;
+		return 0;
+	default:
+		EMGD_ERROR("Invalid type in set_surface");
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/* Atom E6xx does not support a flip pending, since there is no
+ * Display Buffer Info instruction.  So this must be done with vBlank.
+ * However, a wait_for_vblank can be given while a flip is also in
+ * progress, so a semaphore is required when changing flip_pending or
+ * when modifying the vBlank interrupt bits.
+ *
+ * This function should only be called with a flip_mutex around it */
+int set_flip_pending_tnc(unsigned char *mmio, unsigned long pipe_status_reg)
+{
+	platform_context_tnc_t *tnc_context =
+		(platform_context_tnc_t *)mode_context->context->platform_context;
+	unsigned long request_for;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT((pipe_status_reg == PIPEB_STAT) ||
+		(pipe_status_reg == PIPEA_STAT), "Invalid pipe_status_reg", 0);
+
+	if (pipe_status_reg == PIPEB_STAT) {
+		tnc_context->flip_pending |= PLB_FLIP_PIPE_B_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT2);
+	} else {
+		tnc_context->flip_pending |= PLB_FLIP_PIPE_A_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT4);
+	}
+	mode_context->dispatch->full->request_vblanks(request_for, mmio);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/* This function should only be called with a flip_mutex around it */
+int check_flip_pending_tnc(unsigned char *mmio,
+	unsigned long pipe_status_reg)
+{
+	platform_context_tnc_t *tnc_context =
+		(platform_context_tnc_t *)mode_context->context->platform_context;
+	unsigned long request_for;
+	unsigned int flip_pending;
+
+	EMGD_TRACE_ENTER;
+	EMGD_ASSERT((pipe_status_reg == PIPEB_STAT) ||
+		(pipe_status_reg == PIPEA_STAT), "Invalid pipe_status_reg", 0);
+
+	if (pipe_status_reg == PIPEB_STAT) {
+		flip_pending = tnc_context->flip_pending & PLB_FLIP_PIPE_B_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT2);
+	} else {
+		flip_pending = tnc_context->flip_pending & PLB_FLIP_PIPE_A_PENDING;
+		request_for = VBINT_REQUEST(VBINT_FLIP, VBINT_PORT4);
+	}
+
+	if (flip_pending) {
+		if (mode_context->dispatch->full->vblank_occured(request_for)) {
+			/* VBlank occured, flip complete */
+			tnc_context->flip_pending &= ~flip_pending;
+			mode_context->dispatch->full->end_request(request_for, mmio);
+			EMGD_DEBUG("VBlank occured--returning 0");
+			return 0;
+		} else {
+			/* VBlank not done, flip still in progress */
+			EMGD_DEBUG("VBlank hasn't yet occured--returning 1");
+			return 1;
+		}
+	} else {
+		/* No flip pending, so it must have completed */
+		EMGD_DEBUG("returning 0");
+		return 0;
+	}
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param event
+ * @param status
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int igd_query_event_tnc(igd_display_h display_handle,
+	igd_event_t event, unsigned long *status)
+{
+	platform_context_tnc_t *tnc_context =
+		(platform_context_tnc_t *)mode_context->context->platform_context;
+	unsigned char *mmio = MMIO(display_handle);
+	unsigned long pipe_status_reg =
+		(PLANE(display_handle)->plane_reg == DSPACNTR) ? 0x70024 : 0x71024;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("mmio=0x%p, pipe_status_reg=0x%08lx", mmio, pipe_status_reg);
+
+	switch (event) {
+	case IGD_EVENT_FLIP_PENDING:
+		ret = OS_PTHREAD_MUTEX_LOCK(&tnc_context->flip_mutex);
+		*status = check_flip_pending_tnc(mmio, pipe_status_reg);
+		OS_PTHREAD_MUTEX_UNLOCK(&tnc_context->flip_mutex);
+		break;
+	default:
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_DEBUG("Returning status=%lu", *status);
+	return IGD_SUCCESS;
+}
+
+/*!
+ *
+ * @param void
+ *
+ * @return 0
+ */
+static int get_plane_info_tnc(void)
+{
+	igd_framebuffer_info_t *buffer_info;
+	unsigned char* mmio = NULL;
+	unsigned long plane_control = 0;
+	unsigned long reg = 0;
+
+	EMGD_TRACE_ENTER;
+
+	mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	/* Check that plane A is active and process it */
+	plane_control = EMGD_READ32(mmio + DSPACNTR);
+	if(plane_control & PLANE_ENABLE){
+		buffer_info = &mode_context->fw_info->fb_info[0];
+
+		/* get the DSPASIZE register value */
+		reg = (unsigned long)EMGD_READ32(mmio + DSPACNTR + DSP_SIZE_OFFSET);
+		buffer_info[0].height = (reg >> 16) & 0xFFF;
+		buffer_info[0].width =	reg & 0xFFF;
+
+		/* get the DSPASTRIDE register value */
+		buffer_info[0].screen_pitch =
+			(unsigned int)EMGD_READ32(mmio + DSPACNTR + DSP_STRIDE_OFFSET);
+
+		/* Following are NOT offset by 1 in fb info */
+		buffer_info[0].width++;
+		buffer_info[0].height++;
+	}
+
+	/* Check that plane B is active and process it */
+	plane_control = EMGD_READ32(mmio + DSPBCNTR);
+	if(plane_control & PLANE_ENABLE){
+		buffer_info = &mode_context->fw_info->fb_info[0];
+
+		/* get the DSPBSIZE register value */
+		reg = (unsigned long)EMGD_READ32(mmio + DSPBCNTR + DSP_SIZE_OFFSET);
+		buffer_info[1].height = (reg >> 16) & 0xFFF;
+		buffer_info[1].width  =	reg & 0xFFF;
+
+		/* get the DSPBSTRIDE register value */
+		buffer_info[1].screen_pitch =
+			(unsigned int)EMGD_READ32(mmio + DSPBCNTR + DSP_STRIDE_OFFSET);
+
+		/* Following are NOT offset by 1 in fb info */
+		buffer_info[1].width++;
+		buffer_info[1].height++;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param void
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int get_pipe_info_tnc(igd_display_h *display)
+{
+	unsigned char *mmio = NULL;
+	unsigned char *mmio_sdvo = NULL;
+	unsigned long pipe_conf = 0;
+	igd_display_info_t *timing;
+	unsigned long reg = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	mmio_sdvo =
+		EMGD_MMIO(mode_context->context->device_context.virt_mmadr_sdvo);
+
+	/*
+	 * On Atom E6xx only
+	 *    PIPE A + LVDS and
+	 *    PIPE B + SDVO are valid.
+	 *
+	 * Read both VGA_TNC(0:2:0) and SDVO_TNC(0:3:0) mmio reg values in
+	 * determining the current timing
+	 *
+	 * [FIXME]
+	 * 1) Reading both device mmio regs in determining FW timings
+	 * 2) Update dclk equation as it changed for TNC.
+	 */
+	pipe_conf = EMGD_READ32(mmio + PIPEA_CONF);
+	if(pipe_conf & BIT(31)) { /* pipe A is active */
+		timing = &mode_context->fw_info->timing_arr[0];
+
+		reg = EMGD_READ32(mmio + HTOTAL_A);
+		timing[0].htotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[0].width = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + HBLANK_A);
+		timing[0].hblank_start = (unsigned short)reg & 0x1FFF;
+		timing[0].hblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + HSYNC_A);
+		timing[0].hsync_start = (unsigned short)reg & 0x1FFF;
+		timing[0].hsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + VTOTAL_A);
+		timing[0].vtotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[0].height = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + VBLANK_A);
+		timing[0].vblank_start = (unsigned short)reg & 0x1FFF;
+		timing[0].vblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + VSYNC_A);
+		timing[0].vsync_start = (unsigned short)reg & 0x1FFF;
+		timing[0].vsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		/* Following are not offset by 1 in ptinfo */
+		timing[0].width++;
+		timing[0].height++;
+
+		EMGD_DEBUG("Pipe A timing width = %d", timing[0].width);
+		EMGD_DEBUG("Pipe A timing width = %d", timing[0].height);
+
+		{
+			/* Calculate the firmware programmed dot clock */
+			unsigned long dplla, fpa0, fpa1;
+			unsigned long m_select, m, p1_select, p1, p2;
+			unsigned long ref_freq, dclk;
+			int j;
+			unsigned long lvds_port;
+
+			dplla = EMGD_READ32(mmio + DPLLACNTR);
+			fpa0  = EMGD_READ32(mmio + FPA0);
+			fpa1  = EMGD_READ32(mmio + FPA1);
+			/* Note: Only Pipe A supports LVDS */
+			lvds_port = EMGD_READ32(mmio + LVDSCNTR);
+
+
+			if(dplla & BIT(31)) {
+
+				/*************************************************************
+				 * Find M:  read m_select from the register, then do a reverse
+				 *          lookup in the lvds_m_converts table
+				 ************************************************************/
+
+				/* Bits 0:8 determines where the divisor is coming from */
+				if (dplla & 0xFF) {
+					EMGD_DEBUG("Using fpa1");
+					m_select = (fpa1 >> 8) & 0x3F; /* M is at bits 13:8 */
+				} else {
+					EMGD_DEBUG("Using fpa0");
+					m_select = (fpa0 >> 8) & 0x3F; /* M is at bits 13:8 */
+				}
+
+				EMGD_DEBUG("m_select %ld", m_select);
+
+				for( j=0; j < LVDS_M_CONVERTS_LEN; j++) {
+					if (m_select == lvds_m_converts[j]) {
+						break;
+					}
+				}
+
+				if (j == LVDS_M_CONVERTS_LEN) {
+					EMGD_ERROR_EXIT("Invalid M select value");
+					return -IGD_ERROR_INVAL;
+				} else {
+					m = j + LVDS_M_MIN;
+					EMGD_DEBUG("m value %ld", m);
+				}
+
+
+				/*************************************************************
+				 * Find P1 and P2: read p1_select from the register, figure
+				 *                 out its bit position, then add 2.
+				 *                 P2 is always 14 for TNC.
+				 ************************************************************/
+				p2 = 14;  /* P2 is always 14 for TNC */
+
+				/* P1 select is bits 23:17 */
+				if (0 != (p1_select = (dplla >> 17) & 0x7F)) {
+					EMGD_DEBUG("p1_select %ld", p1_select);
+
+					/* p1 = 1 << (p1_select -2), so do the reverse to get p1 */
+					for (j = 0; p1_select > 1; j++) {
+						p1_select >>= 1;
+					}
+
+					/* According to the spec, only 7 bit positions are defined
+					 * and so we should not have shifted more than 6 times */
+					if (j <= 6) {
+						p1 = j + 2;
+						EMGD_DEBUG("p1 %ld", p1);
+					} else {
+						EMGD_ERROR_EXIT("Invalid P1 select value");
+						return -IGD_ERROR_INVAL;
+					}
+				} else {
+					EMGD_ERROR_EXIT("Invalid P1 select value (zero)");
+					return -IGD_ERROR_INVAL;
+				}
+
+
+				/* Equation that calculates the dot clk:
+				 * -------------------------------------
+				 * dot clock = ref_freq * M / (P1 * P2)
+				 */
+				ref_freq = 200000000;  /* 200 MHz */
+
+				dclk = ref_freq * m / (p1*p2);
+
+				/*  FIXME:  This is a workaround to get dclk.  We are supposed
+				 *  to be calculating this based on the formula, but DPLL
+				 *  is somehow locked and does not return the programmed
+				 *  p1 value.  Once this is fixed, we no longer need to have
+				 *  igd_display_handle in the parameter of get_pipe_info
+				 *
+				 *  Update:
+				 *  Now that we initialize the driver before X starts,
+				 *  we want to do a seamless mode-set from firmware to
+				 *  our kernel mode driver. At this point we do not have
+				 *  the igd_display_context_t setup.
+				 *
+				 *	if (NULL != display) {
+				 *		igd_display_context_t *display_context =
+				 *		(igd_display_context_t *) display;
+				 *
+				 *		dclk = ref_freq * m / (p1 * p2);
+				 *
+				 *		PIPE(display)->dclk is in KHhz
+				 *		dclk = PIPE(display)->dclk * 1000;
+				 *	} else {
+				 *		dclk = 0;
+				 *	}
+				 *
+				 *	if( dclk == 0 ) {
+				 *		EMGD_ERROR_EXIT(" Dot Clock/Ref Frequency is Zero!!!");
+				 *		return -IGD_ERROR_INVAL;
+				 *	}
+				 */
+
+				EMGD_DEBUG("Ref frequency = %lu", ref_freq);
+				EMGD_DEBUG("Pipe A constructed Dot clock is = %lu", dclk);
+				timing[0].dclk = dclk/1000; /* Make it to KHz */
+				EMGD_DEBUG("Pipe A Dot clock in KHz = %lu", timing[0].dclk);
+
+				timing[0].refresh = (unsigned short)(dclk/
+					((timing[0].htotal)*(timing[0].vtotal)));
+
+				EMGD_DEBUG("Pipe A refresh = %u", timing[0].refresh);
+			} /* if  DPLL A active */
+		} /* dot clock code block */
+	} /* if Pipe A active */
+
+	/* For 2nd display pipe, pipe b registers in both 0:2:0 and 0:3:0
+	 * supposed to be programmed to same values. So these values can be
+	 * read from either devices */
+	pipe_conf = EMGD_READ32(mmio_sdvo + PIPEB_CONF);
+
+	if(pipe_conf & BIT(31)) { /* pipe B is active */
+		timing = &mode_context->fw_info->timing_arr[0];
+
+		reg = EMGD_READ32(mmio_sdvo + HTOTAL_B);
+		timing[1].htotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[1].width = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + HBLANK_B);
+		timing[1].hblank_start = (unsigned short)reg & 0x1FFF;
+		timing[1].hblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + HSYNC_B);
+		timing[1].hsync_start = (unsigned short)reg & 0x1FFF;
+		timing[1].hsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		reg = EMGD_READ32(mmio + VTOTAL_B);
+		timing[1].vtotal = (unsigned short)(reg >> 16) & 0x1FFF;
+		timing[1].height = (unsigned short)reg & 0xFFF;
+
+		reg = EMGD_READ32(mmio + VBLANK_B);
+		timing[1].vblank_start = (unsigned short)reg & 0x1FFF;
+		timing[1].vblank_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		EMGD_READ32(mmio + VSYNC_B);
+		timing[1].vsync_start = (unsigned short)reg & 0x1FFF;
+		timing[1].vsync_end = (unsigned short)(reg >> 16) & 0x1FFF;
+
+		/* Following are not offset by 1 in ptinfo */
+		timing[1].width++;
+		timing[1].height++;
+
+		{
+			/* Calculate the firmware programmed dot clock */
+			unsigned long dpllb, fpb0, fpb1;
+			unsigned long mb1, mb2, nb, pb1, pb2, pllb_select;
+			unsigned long ref_freq = 0, dclk;
+			unsigned long temp; /* To store intermediate values b4 dclk */
+			int j;
+
+			dpllb = EMGD_READ32(mmio_sdvo + DPLLBCNTR);
+			fpb0  = EMGD_READ32(mmio_sdvo + FPB0);
+			fpb1  = EMGD_READ32(mmio_sdvo + FPB1);
+
+			if(dpllb & BIT(31)) {
+
+				mb1 = (fpb0 >> 8) & 0x3F;    /* M1 is bits 13:8 */
+				mb2 = (fpb0) & 0x1F;         /* M1 is bits 5:0 */
+				nb = (fpb0 >> 16) & 0x3F;    /* N is bits 21:16 */
+				pb1 = (dpllb >> 16) & 0xFF; /* P1 is bits 23:16 */
+
+				/* Check for illegal values of P1
+				 * The bit representation MUST be power of 2
+				 * All other values are illegal including zero.
+				 */
+				if( (pb1 == 0) ||
+					( (pb1 & (pb1-1)) != 0 ) ) {
+					EMGD_ERROR_EXIT("Invalid P1 bits set");
+					return -IGD_ERROR_INVAL;
+				}
+
+				for(j = 0; j < 8; j++) {
+					if(pb1 & BIT(j)) {  /* P1 is divide by 1 to 8 */
+						pb1 = j+1;
+						break;
+					}
+				}
+
+				pb2 = (dpllb >> 24) & 0x3; /* P2 is bits 25:24 */
+
+				/* Since Pipe B does not support internal LVDS, we just
+				 * follow the normal divisor values
+				 */
+				if(pb2 == 0) {
+					pb2 = 10;
+				} else if(pb2 == 1) {
+					pb2 = 5;
+				} else {
+					EMGD_ERROR("Invalid P2 bits set = 0x%lx", pb2);
+				}
+
+				pllb_select = (dpllb >> 13) & 0x3; /* PLL Ref I/P Select */
+
+				/* Equation that calculates the dot clk
+				 * -------------------------------------
+                 *
+				 * pll_freq_factor = ((float)(5 * (mb1+2)+(mb2+2))/(nb+2))/
+				 *	((pb1*pb2));
+				 *
+				 * fdclk = pll_freq_factor * ref_freq * 1000000;
+				 *
+				 * Support for FPU in Kernel Code is not straightforward
+				 * we will just stick to int operations. We will just re-
+                 * arrange the factors.
+				 */
+				if(pllb_select == 0) {
+					ref_freq = 96; /* 96MHz */
+
+				} else if( (pllb_select == 1) || (pllb_select == 3) ) {
+
+					EMGD_ERROR_EXIT("PLL Reference Input Select Reserved");
+					return -IGD_ERROR_INVAL;
+
+				} else if(pllb_select  == 2) {
+					EMGD_ERROR_EXIT("PLL ref is SDVO TV CLK");
+					/* TODO: How to handle this value? */
+					return -IGD_ERROR_INVAL;
+				}
+
+				/* First let's multiply by 1000 * 1000
+				 * so that we don't end up in zero during
+				 * integer division
+				 */
+				temp = 1000 * 1000;
+				temp = temp * (5 * (mb1+2) + (mb2+2));
+				temp = temp /(nb+2);
+				dclk = temp/(pb1*pb2);
+
+				dclk = temp * ref_freq;
+
+				if( (dclk == 0) || (ref_freq == 0) ) {
+					EMGD_ERROR_EXIT("Dot Clock/Ref Frequency is Zero!!!");
+					return -IGD_ERROR_INVAL;
+				}
+
+				EMGD_DEBUG("Ref frequency = %lu", ref_freq);
+				EMGD_DEBUG("Pipe B constructed Dot clock is = %lu", dclk);
+				timing[1].dclk = dclk/1000; /* Make it to KHz */
+				EMGD_DEBUG("Pipe B Dot clock in KHz = %lu", timing[1].dclk);
+
+				timing[1].refresh = (unsigned short) (dclk/
+					((timing[1].htotal+1)*(timing[1].vtotal+1)));
+
+				EMGD_DEBUG("Pipe B refresh = %u", timing[1].refresh);
+			} /* if  DPLL B  active */
+		} /* dot clock code block */
+	} /* if Pipe B is active */
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*!
+ *
+ * @param void
+ *
+ * @return 0
+ */
+static int get_port_info_tnc(void)
+{
+	/* TODO: Any port related info that needs to be populated ? */
+	EMGD_TRACE_ENTER;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+
+}
+
+
+/*******************************************************************************
+ *
+ * The following code provides VBlank interrupt support both within and without
+ * the EMGD HAL.
+ *
+ ******************************************************************************/
+
+/*!
+ * Interrupt handler for VBlanks.
+ *
+ * @param irq (IN).  Unused.
+ *
+ * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+ * registers.
+ *
+ * @return A value to tell the kernel whether or not we handled the interrupt.
+ */
+static irqreturn_t interrupt_handler_tnc(int irq, void* mmio)
+{
+	unsigned long iir, iir_dev3;
+	unsigned long lock_flags;
+	unsigned long tmp;
+	unsigned long port2_interrupt = 0;
+	unsigned long port4_interrupt = 0;
+	emgd_vblank_callback_t *cb;
+
+	EMGD_TRACE_ENTER;
+
+	iir = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+	iir_dev3 = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IIR);
+
+
+	/* Detect whether a vblank interrupt occured, and if so, what type of
+	 * processing is needed (do the simple processing now):
+	 */
+	spin_lock_irqsave(&vblank_lock_tnc, lock_flags);
+	if ((port2_interrupt = iir_dev3 & BIT5 /* Port 2/Pipe A/SDVO-B */) != 0) {
+		if ((tmp = vblank_interrupt_state & VBLANK_INT4_PORT2) != 0) {
+			/* Record "answers" for all requestors: */
+			vblank_interrupt_state |= VBINT_ANSWER4_REQUEST(tmp);
+		}
+	}
+	if ((port4_interrupt = iir & BIT7 /* Port 4/Pipe B/Int-LVDS */) != 0) {
+		if ((tmp = vblank_interrupt_state & VBLANK_INT4_PORT4) != 0) {
+			/* Record "answers" for all requestors: */
+			vblank_interrupt_state |= VBINT_ANSWER4_REQUEST(tmp);
+		}
+	}
+	spin_unlock_irqrestore(&vblank_lock_tnc, lock_flags);
+
+	/*
+	 * Call the KMS 'flip complete' handler if we're waiting for a flip to
+	 * complete on this port.  Note that we should do this before the
+	 * drm_handle_vblank() calls below since we need to clear the
+	 * 'flip pending' bit in the CRTC before the vblank waitqueue gets
+	 * woken up.
+	 */
+	if (port4_interrupt) {
+		crtc_pageflip_handler(mode_context->context->drm_dev,
+			IGD_PORT_TYPE_LVDS);
+
+		if (mode_context->batch_blits[IGD_PORT_TYPE_LVDS - 1]) {
+			notify_userspace_vblank(mode_context->context->drm_dev,
+				IGD_PORT_TYPE_LVDS);
+		}
+	} else if (port2_interrupt) {
+		crtc_pageflip_handler(mode_context->context->drm_dev,
+			IGD_PORT_TYPE_SDVOB);
+
+		if (mode_context->batch_blits[IGD_PORT_TYPE_SDVOB - 1]) {
+			notify_userspace_vblank(mode_context->context->drm_dev,
+				IGD_PORT_TYPE_SDVOB);
+		}
+	}
+
+
+	/* Notify KMS Hander:  The assignment of CRTC=0 for PIPE A and
+	 * CRTC=1 for PIPE B is not correct if somehow PIPE A is disabled.
+	 */
+	if (port2_interrupt) {
+		drm_handle_vblank(mode_context->context->drm_dev, 1);
+	}
+
+	if (port4_interrupt) {
+		drm_handle_vblank(mode_context->context->drm_dev ,0);
+	}
+
+	/* Call any registered/enabled callbacks for this interrupt: */
+	cb = &interrupt_callbacks_tnc[2];
+	if (port2_interrupt && cb->callback &&
+		(vblank_interrupt_state & VBINT_ANSWER(VBINT_CB, VBINT_PORT2))) {
+		/* Clear the state to indicate the vblank has occured prior to
+		 * invoking the callback.
+		 */
+		vblank_interrupt_state &= ~VBINT_ANSWER(VBINT_CB, VBINT_PORT2);
+		cb->callback(cb->priv);
+	}
+	cb = &interrupt_callbacks_tnc[4];
+	if (port4_interrupt && cb->callback &&
+		(vblank_interrupt_state & VBINT_ANSWER(VBINT_CB, VBINT_PORT4))) {
+		/* Clear the state to indicate the vblank has occured prior to
+		 * invoking the callback.
+		 */
+		vblank_interrupt_state &= ~VBINT_ANSWER(VBINT_CB, VBINT_PORT4);
+		cb->callback(cb->priv);
+	}
+
+	/* Clear interrupt status registers: */
+	if (port2_interrupt || port4_interrupt) {
+		/* Clear the corresponding bits in the PIPE{A|B}_STAT register(s): */
+		if (port2_interrupt) {
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS), EMGD_MMIO(mmio) + PIPEB_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+
+			tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT);
+			/* Clear bits that are written by a 1, so we don't clear them: */
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT,
+					(tmp | VBLANK_STS));
+			READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT);
+			tmp = READ_MMIO_REG_TNC(IGD_PORT_LVDS, PIPEB_STAT);
+		}
+		if (port4_interrupt) {
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS), EMGD_MMIO(mmio) + PIPEA_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+		}
+
+		/* Clear the corresponding bits in the IIR register: */
+		EMGD_WRITE32((port2_interrupt | port4_interrupt), EMGD_MMIO(mmio)+IIR);
+		WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IIR, port2_interrupt);
+
+
+		EMGD_DEBUG("EXIT--IRQ_HANDLED");
+		return IRQ_HANDLED;
+	} else {
+		EMGD_DEBUG("EXIT--IRQ_NONE");
+		return IRQ_NONE;
+	}
+}
+
+/*!
+ * Implementation of "protected" function (i.e. for use within the mode
+ * module) to request VBlank interrupts for a particular purpose & port.
+ * Based upon the outstanding requests, this function decides whether to
+ * touch registers, register the interrupt handler, etc.
+ *
+ * @param request_for (IN).  A bit that identifies a who and what (e.g.
+ *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+ *
+ * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+ * registers.
+ *
+ * @return Zero for success, non-zero for failure.
+ */
+int request_vblanks_tnc(unsigned long request_for, unsigned char *mmio)
+{
+	unsigned long lock_flags;
+	unsigned long tmp;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: request_for=0x%lx, mmio=0x%p", request_for, mmio);
+
+	/* Perform error checking--ensure a valid bit was set: */
+	if (!(request_for & (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4))) {
+		EMGD_ERROR_EXIT("Invalid parameter, request_for=0x%lx",
+			request_for);
+		return -1;
+	}
+
+	/* If we're just enabling interrupts, register the interrupt handler: */
+	if (!VBLANK_INTERRUPTS_ENABLED) {
+		struct drm_device *drm_device = mode_context->context->drm_dev;
+
+		EMGD_DEBUG("Registering interrupt_handler_tnc()");
+		if (request_irq(drm_device->pdev->irq, interrupt_handler_tnc,
+			IRQF_SHARED, EMGD_DRIVER_NAME, mmio)) {
+			EMGD_ERROR_EXIT("Failed to register interrupt_handler_tnc()");
+			return -1;
+		} else {
+			EMGD_DEBUG("Successfully registered interrupt_handler_tnc()");
+		}
+	}
+
+	/* Lock here to stop the interrupt handler until after changing bits: */
+	spin_lock_irqsave(&vblank_lock_tnc, lock_flags);
+
+	/* Enable interrupts for the requested purpose/port, actually touching the
+	 * hardware registers if newly enabling interrupts for the given port/pipe:
+	 */
+	if (request_for & VBLANK_INT4_PORT2) {
+		if (!VBLANK_INTERRUPTS_ENABLED4_PORT2) {
+			/* 1. Change Pipe Display Status Register for this pipe: set the
+			 *    Vertical Blank Interrupt Enable bit & clear (by setting) the
+			 *    Vertical Blank Interrupt Status bit:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+			/* Clear bits that are written by a 1, so we don't clear them: */
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS_EN | VBLANK_STS),
+				EMGD_MMIO(mmio) + PIPEB_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+
+			/* 2. Just in case, clear (by setting) the Interrupt Identity
+			 *    Register bit for this pipe:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+			EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IIR);
+
+			/* 3. Clear the Interrupt Mask Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+			EMGD_WRITE32((tmp & (~BIT5)), EMGD_MMIO(mmio) + IMR);
+
+			/* 4. Set the Interrupt Enable Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+			EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IER);
+
+			/* NOW LET'S PROGRAM DEVICE 3 */
+
+			tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT);
+			/* Clear bits that are written by a 1, so we don't clear them: */
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT,
+					(tmp | VBLANK_STS_EN | VBLANK_STS));
+			READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT);
+
+			/* 2. Just in case, clear (by setting) the Interrupt Identity
+			 *    Register bit for this pipe:
+			 */
+			tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IIR);
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IIR, (tmp | BIT5));
+
+			/* 3. Clear the Interrupt Mask Register bit for this pipe: */
+			tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IMR);
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IMR, (tmp & (~BIT5)));
+
+			/* 4. Set the Interrupt Enable Register bit for this pipe: */
+			tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IER);
+			WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IER, (tmp | BIT5));
+
+		}
+		vblank_interrupt_state |= request_for;
+		/* Since there is only  one vblank interrupt per request for now,
+		   add counter have been remove for now and disable it when end request.
+		   The counter may required after there is more than one request in future.
+		*/
+		vblank_interrupt_ref_cnt_port2 = 1;
+	} else /* if (request_for & VBLANK_INT4_PORT4) */ {
+		if (!VBLANK_INTERRUPTS_ENABLED4_PORT4) {
+			/* 1. Change Pipe Display Status Register for this pipe: set the
+			 *    Vertical Blank Interrupt Enable bit & clear (by setting) the
+			 *    Vertical Blank Interrupt Status bit:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+			/* Clear bits that are written by a 1, so we don't clear them: */
+			tmp = tmp & (~PIPESTAT_STS_BITS);
+			EMGD_WRITE32((tmp | VBLANK_STS_EN | VBLANK_STS),
+				EMGD_MMIO(mmio) + PIPEA_STAT);
+			EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+
+			/* 2. Just in case, clear (by setting) the Interrupt Identity
+			 *    Register bit for this pipe:
+			 */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+			EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IIR);
+
+			/* 3. Clear the Interrupt Mask Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+			EMGD_WRITE32((tmp & (~BIT7)), EMGD_MMIO(mmio) + IMR);
+
+			/* 4. Set the Interrupt Enable Register bit for this pipe: */
+			tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+			EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IER);
+		}
+		vblank_interrupt_state |= request_for;
+		vblank_interrupt_ref_cnt_port4++;
+	}
+
+
+	/* Unlock to allow the interrupt handler to proceed: */
+	spin_unlock_irqrestore(&vblank_lock_tnc, lock_flags);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*!
+ * Implementation of "protected" function (i.e. for use within the mode
+ * module) to end a previous request VBlank interrupts for a particular
+ * purpose & port.  Based upon the outstanding requests, this function
+ * decides whether to touch registers, unregister the interrupt handler,
+ * etc.
+ *
+ * @param request_for (IN).  A bit that identifies a who and what (e.g.
+ *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+ *
+ * @param mmio (IN).  A pointer to the start of memory-mapped IO, to access
+ * registers.
+ *
+ * @return Zero for success, non-zero for failure.
+ */
+int end_request_tnc(unsigned long request_for, unsigned char *mmio)
+{
+	unsigned long lock_flags;
+	unsigned long tmp;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: request_for=0x%lx, mmio=0x%p", request_for, mmio);
+
+	/* Perform error checking--ensure a valid bit was set: */
+	if (!(request_for & (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4)) ||
+		!(vblank_interrupt_state & request_for)) {
+		EMGD_ERROR_EXIT("Invalid parameter, request_for=0x%lx", request_for);
+		return -1;
+	}
+
+	/* Lock here to stop the interrupt handler until after changing bits: */
+	spin_lock_irqsave(&vblank_lock_tnc, lock_flags);
+
+	/* Disable interrupts for the requested purpose/port, actually touching the
+	 * hardware registers no software wants interrupts for the given port/pipe:
+	 */
+	if (request_for & VBLANK_INT4_PORT2) {
+		/* Decrement reference count */
+		vblank_interrupt_ref_cnt_port2 = 0;
+		if (0 > vblank_interrupt_ref_cnt_port2) {
+			EMGD_DEBUG("WARNING:  Disabled vblank INT too many times.");
+			vblank_interrupt_ref_cnt_port2 = 0;
+		}
+
+		if (0 == vblank_interrupt_ref_cnt_port2) {
+			/* Turn off both the request and the answer bits: */
+			tmp = request_for & VBLANK_INT4_PORT2;
+			vblank_interrupt_state &= ~(tmp | VBINT_ANSWER4_REQUEST(tmp));
+			if (!VBLANK_INTERRUPTS_ENABLED4_PORT2) {
+				/* 1. Change Pipe Display Status Register for this pipe: clear
+				 *    the Vertical Blank Interrupt Enable bit & clear (by
+				 *    setting) the Vertical Blank Interrupt Status bit:
+				 */
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+				/* Clear bits that are written by a 1, so don't clear them: */
+				tmp = tmp & (~PIPESTAT_STS_BITS);
+				EMGD_WRITE32(((tmp & (~VBLANK_STS_EN)) | VBLANK_STS),
+					EMGD_MMIO(mmio) + PIPEB_STAT);
+				EMGD_READ32(EMGD_MMIO(mmio) + PIPEB_STAT);
+
+				/* 2. Clear the Interrupt Enable Register bit for this pipe: */
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+				EMGD_WRITE32((tmp & (~BIT5)), EMGD_MMIO(mmio) + IER);
+
+				/* 3. Set the Interrupt Mask Register bit for this pipe: */
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+				EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IMR);
+
+				/* 4. Just in case, clear (by setting) the Interrupt Identity
+				*    Register bit for this pipe:
+				*/
+				tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+				EMGD_WRITE32((tmp | BIT5), EMGD_MMIO(mmio) + IIR);
+
+				/* NOW LET'S PROGRAM DEVICE 3 */
+
+				tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT);
+					/* Clear bits that are written by a 1, so we don't clear them: */
+				tmp = tmp & (~PIPESTAT_STS_BITS);
+				WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT,
+						((tmp & (~VBLANK_STS_EN)) | VBLANK_STS));
+				READ_MMIO_REG_TNC(IGD_PORT_SDVO, PIPEB_STAT);
+
+				/* 2. Clear the Interrupt Enable Register bit for this pipe: */
+				tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IER);
+				WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IER, (tmp & (~BIT5)));
+
+				/* 3. Set the Interrupt Mask Register bit for this pipe: */
+				tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IMR);
+				WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IMR, (tmp | BIT5));
+
+				/* 4. Just in case, clear (by setting) the Interrupt Identity
+				 *    Register bit for this pipe:
+				 */
+				tmp = READ_MMIO_REG_TNC(IGD_PORT_SDVO, IIR);
+				WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, IIR, (tmp | BIT5));
+
+			}
+		}
+	}
+
+	if (request_for & VBLANK_INT4_PORT4) {
+		/* Decrement reference count */
+		vblank_interrupt_ref_cnt_port4--;
+		if (0 > vblank_interrupt_ref_cnt_port4) {
+			EMGD_DEBUG("WARNING:  Disabled vblank INT too many times.");
+			vblank_interrupt_ref_cnt_port4 = 0;
+		}
+
+		if (0 == vblank_interrupt_ref_cnt_port4) {
+			/* Turn off both the request and the answer bits: */
+			tmp = request_for & VBLANK_INT4_PORT4;
+			vblank_interrupt_state &= ~(tmp | VBINT_ANSWER4_REQUEST(tmp));
+			if (!VBLANK_INTERRUPTS_ENABLED4_PORT4) {
+				/* 1. Change Pipe Display Status Register for this pipe: clear
+				 *    the Vertical Blank Interrupt Enable bit & clear (by
+				 *    setting the Vertical Blank Interrupt Status bit:
+				 */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+				 /* Clear bits that are written by a 1, so don't clear them: */
+				 tmp = tmp & (~PIPESTAT_STS_BITS);
+				 EMGD_WRITE32(((tmp & (~VBLANK_STS_EN)) | VBLANK_STS),
+					 EMGD_MMIO(mmio) + PIPEA_STAT);
+				 EMGD_READ32(EMGD_MMIO(mmio) + PIPEA_STAT);
+
+				 /* 2. Clear the Interrupt Enable Register bit for this pipe: */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + IER);
+				 EMGD_WRITE32((tmp & (~BIT7)), EMGD_MMIO(mmio) + IER);
+
+				 /* 3. Set the Interrupt Mask Register bit for this pipe: */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + IMR);
+				 EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IMR);
+
+				 /* 4. Just in case, clear (by setting) the Interrupt Identity
+				 *    Register bit for this pipe:
+				 */
+				 tmp = EMGD_READ32(EMGD_MMIO(mmio) + IIR);
+				 EMGD_WRITE32((tmp | BIT7), EMGD_MMIO(mmio) + IIR);
+			}
+		}
+	}
+
+	/* Unlock to allow the interrupt handler to proceed: */
+	spin_unlock_irqrestore(&vblank_lock_tnc, lock_flags);
+
+	/* If we've completely disabled all causes for interrupts, unregister the
+	 * interrupt handler:
+	 */
+	if (!VBLANK_INTERRUPTS_ENABLED) {
+		struct drm_device *drm_device = mode_context->context->drm_dev;
+
+		EMGD_DEBUG("Unregistering interrupt_handler_tnc()");
+		free_irq(drm_device->pdev->irq, mmio);
+		EMGD_DEBUG("Successfully unregistered interrupt_handler_tnc()");
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Implementation of "protected" function (i.e. for use within the mode
+ * module) to check whether a requested VBlank interrupt occured.
+ *
+ * @param request_for (IN).  A bit that identifies a who and what (e.g.
+ *  VBINT_REQUEST(VBINT_WAIT, VBINT_PORT4)).
+ *
+ * @return Non-zero if the requested VBlank occured, zero if not.
+ */
+int vblank_occured_tnc(unsigned long request_for)
+{
+	return (vblank_interrupt_state & VBINT_ANSWER4_REQUEST(request_for));
+}
+
+/*!
+ *  Registers a VBlank interrupt callback function (and its parameter) to
+ *  call when a VBlank interrupt occurs for a given port.
+ *
+ * @param callback (IN).  A callback (function pointer) to a non-HAL
+ * function that processes a VBlank interrupt.
+ *
+ * @param priv (IN).  An opaque pointer to a non-HAL data structure.
+ *  This pointer is passed as a parameter of the callback function.
+ *
+ * @param port_number (IN).  The EMGD port number to register a VBlank
+ *  interrupt callback for.
+ *
+ * @return A handle that uniquely identifies this callback/port
+ *  combination, or NULL if a failure.
+ */
+emgd_vblank_callback_h register_vblank_callback_tnc(
+	emgd_process_vblank_interrupt_t callback,
+	void *priv,
+	unsigned long port_number)
+{
+	EMGD_TRACE_ENTER;
+
+	if (!callback || !priv ||
+		!((port_number == 2) || (port_number == 4))) {
+		EMGD_ERROR_EXIT("Invalid parameter amongst the following:\n"
+			"  process_interrupt=0x%p, priv=0x%p, port_number=%lu",
+			callback, priv, port_number);
+		return NULL;
+	}
+
+	interrupt_callbacks_tnc[port_number].callback = callback;
+	interrupt_callbacks_tnc[port_number].priv = priv;
+
+	EMGD_TRACE_EXIT;
+	return &interrupt_callbacks_tnc[port_number];
+}
+
+/*!
+ *  Unregisters a previously-registered VBlank interrupt callback function
+ *  for a given port.
+ *
+ * @param callback_h (IN).  The handle that uniquely identifies the VBlank
+ *  interrupt callback to unregister.
+ */
+void unregister_vblank_callback_tnc(emgd_vblank_callback_h callback_h)
+{
+	emgd_vblank_callback_t *cb = (emgd_vblank_callback_t *) callback_h;
+
+	EMGD_TRACE_ENTER;
+
+	cb->callback = NULL;
+	cb->priv = NULL;
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ *  Enable delivering VBlank interrupts to the callback function for the
+ *  registered callback/port combination.
+ *
+ * @param callback_h (IN).  The handle that uniquely identifies which
+ *  VBlank interrupt callback/port combination to enable.
+ *
+ * @return Zero if successful, non-zero if a failure.
+ */
+int enable_vblank_callback_tnc(emgd_vblank_callback_h callback_h)
+{
+	emgd_vblank_callback_t *cb = (emgd_vblank_callback_t *) callback_h;
+	unsigned long enable_for = VBINT_REQUEST(VBINT_CB,
+		(cb->port_number == 2) ? VBINT_PORT2 : VBINT_PORT4);
+	unsigned char* mmio =
+		EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameter: callback_h=0x%p", callback_h);
+
+	ret = request_vblanks_tnc(enable_for, mmio);
+
+	EMGD_DEBUG("Return %d", ret);
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*!
+ *  Disable delivering VBlank interrupts to the callback function for the
+ *  registered function/port combination.
+ *
+ * @param callback_h (IN).  The handle that uniquely identifies which
+ *  VBlank interrupt callback/port combination to disable.
+ */
+void disable_vblank_callback_tnc(emgd_vblank_callback_h callback_h)
+{
+	unsigned long lock_flags;
+	unsigned long enable_for;
+	unsigned char* mmio =
+		EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameter: callback_h=0x%p", callback_h);
+
+	if (callback_h == ALL_PORT_CALLBACKS) {
+		/* Need to do some push-ups in order to get interrupts disabled: */
+		spin_lock_irqsave(&vblank_lock_tnc, lock_flags);
+		enable_for = (VBLANK_INT4_PORT2 | VBLANK_INT4_PORT4);
+		if (!VBLANK_INTERRUPTS_ENABLED) {
+			/* Nothing has enabled interrupts, so there's no interrupt handler
+			 * to unregister.  Therefore, use a special value to prevent that:
+			 */
+			vblank_interrupt_state = (VBLANK_INT4_PORT2 |
+				VBLANK_INT4_PORT4 | VBLANK_DISABLE_HW_ONLY);
+		} else {
+			vblank_interrupt_state = (VBLANK_INT4_PORT2 |
+				VBLANK_INT4_PORT4);
+		}
+		spin_unlock_irqrestore(&vblank_lock_tnc, lock_flags);
+
+		end_request_tnc(enable_for, mmio);
+
+		spin_lock_irqsave(&vblank_lock_tnc, lock_flags);
+		vblank_interrupt_state = 0;
+		spin_unlock_irqrestore(&vblank_lock_tnc, lock_flags);
+	} else {
+		emgd_vblank_callback_t *cb =
+			(emgd_vblank_callback_t *) callback_h;
+		enable_for = VBINT_REQUEST(VBINT_CB,
+			(cb->port_number == 2) ? VBINT_PORT2 : VBINT_PORT4);
+
+		end_request_tnc(enable_for, mmio);
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+mode_full_dispatch_t mode_full_dispatch_tnc = {
+	igd_alter_cursor_pos_tnc,
+	igd_set_palette_entries_tnc,
+	igd_wait_vsync_tnc,
+	igd_query_in_vblank_tnc,
+	igd_get_scanline_tnc,
+	set_display_base_tnc,
+	program_cursor_tnc,
+	set_color_correct_tnc,
+	igd_get_surface_tnc,
+	igd_set_surface_tnc,
+	igd_query_event_tnc,
+	set_flip_pending_tnc,
+	check_flip_pending_tnc,
+	get_plane_info_tnc,
+	get_pipe_info_tnc,
+	get_port_info_tnc,
+	register_vblank_callback_tnc,
+	unregister_vblank_callback_tnc,
+	enable_vblank_callback_tnc,
+	disable_vblank_callback_tnc,
+	request_vblanks_tnc,
+	end_request_tnc,
+	vblank_occured_tnc,
+};
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.h b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.h
new file mode 100644
index 0000000..98d667e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/mode/tnc/mode_tnc.h
@@ -0,0 +1,52 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_tnc.h
+ * $Revision: 1.2 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.mode
+
+typedef struct _mode_data_tnc {
+	unsigned long plane_a_preserve;
+	unsigned long plane_b_c_preserve;
+	unsigned long pipe_preserve;
+	unsigned long dsp_arb;
+	unsigned long fifo_watermark1;
+	unsigned long fifo_watermark2;
+	unsigned long fifo_watermark3;
+	unsigned long fifo_watermark4;
+	unsigned long fifo_watermark5;
+	unsigned long fifo_watermark6;
+} mode_data_tnc_t;
+
+#define CHECK_VGA(a) MODE_IS_VGA(a)
+
+#ifdef DEBUG_BUILD_TYPE
+#define FLAG(a) a
+#else
+/* Turn all workaround for release driver */
+#define FLAG(a) 1
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pd/cmn/pd.c b/drivers/gpu/drm/emgd/emgd/display/pd/cmn/pd.c
new file mode 100755
index 0000000..0ac0e9c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pd/cmn/pd.c
@@ -0,0 +1,516 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pd.c
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains all the necessary common functions for port driver
+ *  module. These functions are called only from hardware specific port
+ *  drivers. These are exported via pd.h.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+
+#include <memory.h>
+#include <sched.h>
+
+#include <mode.h>
+#include <pd.h>
+#include <pi.h>
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*!
+ *
+ * @param size
+ *
+ * @return void
+ */
+void *pd_malloc(unsigned long size)
+{
+	return OS_ALLOC(size);
+} /* end pd_malloc */
+
+/*!
+ *
+ * @param address
+ * @param c
+ * @size
+ *
+ * @return void
+ */
+void *pd_memset(void *address, int c, unsigned long size)
+{
+	return OS_MEMSET(address, c, size);
+} /* end pd_memset */
+
+/*!
+ *
+ * @param dst
+ * @param src
+ * @param size
+ *
+ * @return void
+ */
+void *pd_memcpy(void *dst, void *src, unsigned long size)
+{
+	return OS_MEMCPY(dst, src, size);
+} /* end pd_memcpy */
+
+/*!
+ *
+ * @param size
+ *
+ * @return void
+ */
+void pd_free(void *ptr)
+{
+	OS_FREE(ptr);
+} /* end pd_free */
+
+/*!
+ *
+ * @param usec
+ *
+ * @return void
+ */
+void pd_usleep(unsigned long usec)
+{
+
+	if (usec <= 1000) {
+		OS_SLEEP(usec);
+	} else {
+		os_alarm_t alarm = OS_SET_ALARM((usec+999)/1000);
+		do {
+			OS_SCHEDULE();
+		} while (!OS_TEST_ALARM(alarm));
+	}
+
+}
+
+/*!
+ *
+ * @param usec
+ *
+ * @return void
+ */
+void pd_ui_usleep(unsigned long usec)
+{
+	OS_UISLEEP(usec);
+
+}
+
+/*!
+ *
+ * @param dest
+ * @param src
+ *
+ * @return dest
+ */
+char *pd_strcpy(char *dest, const char *src)
+{
+	int i = 0;
+	/* This can be optimized by assigning 32 bit quantities instead
+	 * of 8 bit quantities. This requires knowing the length first then
+	 * move the quantities. For now, this is OK. */
+	while (src[i] != '\0') {
+		dest[i] = src[i];
+		i++;
+	}
+	dest[i] = '\0';
+	return (dest);
+}
+
+/*!
+ *
+ * @param handle
+ * @param driver
+ *
+ * @return pi_pd_register()
+ */
+int pd_register(void *handle, pd_driver_t *driver)
+{
+	return (pi_pd_register(driver));
+} /* end pd_register */
+
+/*!
+ *
+ * @param curr
+ * @param in
+ *
+ * @return PD_SUCCESS on success
+ * @return PD_ERR_INVALID_ATTR or PD_ERR_INCORR_ATTR_VALUE on failure
+ */
+int pd_check_attr(pd_attr_t *curr, pd_attr_t *in)
+{
+	if (!curr || !in) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	if (curr->id != in->id) {
+		return PD_ERR_INVALID_ATTR;
+	}
+
+	switch (curr->type) {
+		case PD_ATTR_TYPE_RANGE:
+			if ((in->current_value < RATTR(curr)->min) ||
+				(in->current_value > RATTR(curr)->max)) {
+				return PD_ERR_INCORR_ATTR_VALUE;
+			}
+			break;
+		case PD_ATTR_TYPE_LIST:
+			if ((in->current_value < 1) ||
+				(in->current_value > LHATTR(curr)->num_entries)) {
+				return PD_ERR_INCORR_ATTR_VALUE;
+			}
+			break;
+		case PD_ATTR_TYPE_BOOL:
+			if ((in->current_value != TRUE) &&
+				(in->current_value != FALSE)) {
+				return PD_ERR_INCORR_ATTR_VALUE;
+			}
+			break;
+		default:
+			return PD_ERR_INVALID_ATTR;
+	}
+	return PD_SUCCESS;
+}
+
+/*!
+ * This function searches for the requested attr_id in the attribute list
+ * and returns the pointer.
+ *
+ * In case of LIST attribute, it will return the proper list entry.
+ *
+ * @param attr_list
+ * @param num_attr
+ * @param attr_id
+ * @param flag
+ *
+ * @return pd_attr_t on success
+ * @return NULL on failure
+ */
+pd_attr_t *pd_get_attr(pd_attr_t *attr_list, unsigned long num_attrs,
+		unsigned long attr_id, unsigned long flag)
+{
+	unsigned long i;
+
+	if (!attr_list) {
+		return NULL;
+	}
+
+	for (i = 0; i < num_attrs; i++) {
+		if (attr_list[i].id == attr_id) {
+			if (attr_list[i].type == PD_ATTR_TYPE_LIST) {
+				if (flag == PD_GET_ATTR_LIST_ENTRY) {
+					return (&(attr_list[i+attr_list[i].current_value]));
+				} else {
+					return (&(attr_list[i]));
+				}
+			}
+			return (&(attr_list[i]));
+		}
+	}
+	return NULL;
+} /* end pd_get_attr() */
+
+/*!
+ * Common mode filter algorithm for all port drivers
+ *
+ * @param call_back
+ * @param in_list
+ * @param out_list
+ * @param dvo_info
+ * @param display_info
+ *
+ * @return PD_SUCCESS on success
+ * @return PD_ERR_NULL_PTR or PD_ERR_NOMEM on failure
+ */
+int pd_filter_timings(
+	void *callback_context,
+	pd_timing_t *in_list,
+	pd_timing_t **out_list,
+	pd_dvo_info_t *dvo_info,
+	pd_display_info_t *display_info)
+{
+	igd_display_port_t *port = (igd_display_port_t *)callback_context;
+	pd_timing_t    *timing   = NULL, *native_dtd = NULL;
+	int            j;
+	int            count     = 0;
+	unsigned short fp_refresh = 60;
+	pd_timing_t    *olist = NULL;
+	unsigned long  fixed_timing = 0;
+	int i, ret;
+
+	if (!port || !in_list || !out_list) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	/* Start with fixed_res = 0 */
+	display_info->fixed_res = 0;
+
+	/* DisplayID FP width are specified */
+	if (port->firmware_type == PI_FIRMWARE_DISPLAYID) {
+		display_info->width = port->displayid->display_params.horz_pixels;
+		display_info->height = port->displayid->display_params.vert_pixels;
+		fixed_timing = port->displayid->display_params.fixed_timing;
+		display_info->fixed_res = port->displayid->display_params.fixed_res;
+	}
+
+	/* Overwrite DisplayID FP values with config fpinfo, later
+	 * these will be overwritten by native DTD width, height */
+	if (port->fp_info) {
+		/* This is done for backward compatibility:
+		 * Set width, height from display port fp_info
+		 * This also required dropping fp_info width and height attributes
+		 * from all port drivers */
+		display_info->width = (unsigned short) port->fp_info->fp_width;
+		display_info->height = (unsigned short) port->fp_info->fp_height;
+		/* Backward compatibility: If width and height are specified,
+		 * that means it is a fixed resolution panel */
+		if (port->fp_info->fp_width && port->fp_info->fp_height) {
+			display_info->fixed_res = 1;
+			fp_refresh = 60;
+		}
+	}
+
+	/* If fixed timing also comes from user attributes then override DisplayID
+	 * fixed timing and fpinfo values */
+	ret = pi_get_port_init_attr(port, PD_ATTR_ID_FIXED_TIMING, &fixed_timing);
+	if (fixed_timing) {
+		display_info->fixed_res = 1;
+		fp_refresh = 60;
+	}
+
+	/* Do gmch filtering:
+	 * There is no way to get the mode context inorder to reach the
+	 * gmch filtering function */
+
+	/* First find the native resolution */
+	get_native_dtd(in_list, PI_SUPPORTED_TIMINGS,
+		&display_info->native_dtd, PD_MODE_DTD_FP_NATIVE);
+
+	/* If no FP Native DTD provided, then get the native DTD
+	 *   either user DTD or edid DTD */
+	if (!display_info->native_dtd) {
+		get_native_dtd(in_list, PI_SUPPORTED_TIMINGS,
+			&display_info->native_dtd, PD_MODE_DTD_USER);
+	}
+	if (!display_info->native_dtd) {
+		get_native_dtd(in_list, PI_SUPPORTED_TIMINGS,
+			&display_info->native_dtd, PD_MODE_DTD);
+	}
+
+	/* Set up the fp width, height and refresh for the comparison */
+	if (display_info->native_dtd) {
+#ifndef CONFIG_MICRO
+		/* If fp width, height doesn' match with native width and height,
+		 * Configuration isn't correct */
+		if ((display_info->width &&
+				(display_info->width != display_info->native_dtd->width)) &&
+			(display_info->height &&
+				(display_info->height != display_info->native_dtd->height))) {
+			EMGD_DEBUG("FP Width Height doesn't match with Native DTD.");
+		}
+#endif
+		/* Overwrite native width height as panel width and height */
+		display_info->width = display_info->native_dtd->width;
+		display_info->height = display_info->native_dtd->height;
+		fp_refresh = display_info->native_dtd->refresh;
+	} else if (ret &&
+		port->firmware_type != PI_FIRMWARE_DISPLAYID &&
+		dvo_info->upscale) {
+		/* TODO:
+		 * For time being this function has to assume all upscaling encoders
+		 * are connected to a fixed timing panel if no fixed_timing is
+		 * specified.
+		 *
+		 * Once fixed_timing init-time attribute becomes mandatory, below check
+		 * can be removed.
+		 *
+		 * If customer uses old config system and didn't specified init time
+		 * fixed_timing attribute, then driver assumes it is a fixed-resolution
+		 * panel. If user do specifies fixed_timing attribute, then above check
+		 * will fail, and algorithm continues with whatever value set to
+		 * fixed_res attr.
+		 *
+		 * ret != 0 means no fixed_timing user attribute
+		 * firmware_type == DISPLAYID means firmware provided fixed_timing,
+		 * so don't change it.
+		 */
+		display_info->fixed_res = 1;
+	}
+
+	/* Check native_dtd for fixed_res display */
+	if (display_info->fixed_res && !display_info->native_dtd) {
+		/* This happens if user provides fp_width, fp_height and didn't set
+		 * fixed res parameter. In this case native_dtd will be set as part
+		 * of the while loop while filtering the modes */
+		EMGD_DEBUG("pd_filter_timings: No native dtd for fixed_resolution");
+	}
+
+	if (!display_info->width && !display_info->height) {
+		/* If fp width and height isn't known, then enable all modes as
+		 * non-fixed res display */
+		display_info->fixed_res = 0;
+	}
+
+	EMGD_DEBUG("fixed_res = %u fixed_timing = %lu",
+		display_info->fixed_res, fixed_timing);
+	EMGD_DEBUG("fp_width = %u, fp_height = %u, fp_refresh = %u",
+		display_info->width, display_info->height, fp_refresh);
+	EMGD_DEBUG("min_dclk = %lu, max_dclk = %lu",
+		dvo_info->min_dclk, dvo_info->max_dclk);
+
+	/* This function can be called with following
+	 * ---------------------------------------------------------------
+	 *  DVO device PanelFit Display AvailableModes
+	 *  DOWN  UP            Fixed?
+	 * --------------------------------------------------------
+	 *   0     0      0       0     All supported modes
+	 *   1     0      0       0     All supported modes
+	 *   0     1      0       0     All supported modes
+	 *   1     1      0       0     All supported modes
+	 *   0     0      1       0     All supported modes
+	 *   1     0      1       0     All supported modes
+	 *   0     1      1       0     All supported modes
+	 *   1     1      1       0     All supported modes
+	 *
+	 *   0     0      0       Y     Only one mode
+	 *   1     0      0       Y     Only one mode
+	 *   0     1      0       Y     Only one mode
+	 *   1     1      0       Y     Only one mode
+	 *   0     0      1       Y     Only one mode
+	 *
+	 *   0     1      1       Y     All upscalable modes
+	 *   1     0      1       Y     All downscalable modes
+	 *
+	 *   1     1      1       Y     All UP & DOWN scalable modes
+	 * -------------------------------------------------------------------------
+	 */
+
+	for (i = 0; i < 2; ++i) {
+		j = 0;
+		timing = in_list;
+		while(timing->width != PD_TIMING_LIST_END) {
+			/* If mode supported and dclk is within the range */
+			if ((timing->mode_info_flags & PD_MODE_SUPPORTED) &&
+				((!dvo_info->max_dclk || (timing->dclk <= dvo_info->max_dclk))&&
+				(!dvo_info->min_dclk || (timing->dclk >= dvo_info->min_dclk)))){
+
+				if (
+					/* fixed_res = 0 */
+					!display_info->fixed_res ||
+
+					/* no panel fit */
+					(!display_info->panel_fit &&
+					timing->refresh == fp_refresh &&
+					timing->width   == display_info->width &&
+					timing->height  == display_info->height) ||
+
+					/* panel fit and upscale or downscale */
+					(display_info->panel_fit &&
+					timing->refresh == fp_refresh &&
+					((dvo_info->upscale &&
+					timing->width  <= display_info->width &&
+					timing->height <= display_info->height &&
+					(!dvo_info->upscale_min_width ||
+						timing->width  >= dvo_info->upscale_min_width) &&
+					(!dvo_info->upscale_min_height ||
+					timing->height >= dvo_info->upscale_min_height))
+#if 0
+					||
+					(dvo_info->downscale &&
+					timing->width  >= display_info->width &&
+					timing->height >= display_info->height &&
+					(!dvo_info->downscale_max_width ||
+						timing->width  <= dvo_info->downscale_max_width) &&
+					(!dvo_info->downscale_max_height ||
+						timing->height >= dvo_info->downscale_max_height))
+#endif
+					))) {
+
+					if(!i){
+						count++;
+					} else {
+						/* copy timing */
+						olist[j] = *timing;
+
+						/* save the native_dtd timing */
+						if ((timing->width   == display_info->width) &&
+							(timing->height  == display_info->height) &&
+							(timing->refresh == fp_refresh)) {
+							native_dtd = &(olist[j]);
+						}
+
+						/* The native DTD pointer is pointing in the in_list,
+						 * reset this pointer to point in the out_list */
+						if (timing == display_info->native_dtd) {
+							display_info->native_dtd = &olist[j];
+						}
+
+						j++;
+					}
+				}
+			}
+			timing++;
+			if ((timing->width == PD_TIMING_LIST_END) && timing->extn_ptr) {
+				timing = timing->extn_ptr;
+			}
+		}
+		if(!i) {
+			count++;
+			olist = (pd_timing_t *) pd_malloc(count * sizeof(pd_timing_t));
+			if(!olist) {
+				return PD_ERR_NOMEM;
+			}
+		} else {
+			/* Copy the END of LIST entry */
+			olist[j] = *timing;
+		}
+	}
+
+	/* If there is no native_dtd, then use the first matching
+	 * resolution with fp width and height as native dtd */
+	if (!display_info->native_dtd) {
+		display_info->native_dtd = native_dtd;
+	}
+	if (display_info->native_dtd) {
+		EMGD_DEBUG("pd_filter_timings: NativeDTD: %ux%u@%u",
+			display_info->native_dtd->width,
+			display_info->native_dtd->height,
+			display_info->native_dtd->refresh);
+		display_info->native_dtd->mode_info_flags |= PD_MODE_DTD_FP_NATIVE;
+	}
+
+	*out_list = olist;
+	return PD_SUCCESS;
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/displayid.c b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/displayid.c
new file mode 100644
index 0000000..186ce7d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/displayid.c
@@ -0,0 +1,1109 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: displayid.c
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains functions to parse DisplayID into a data strucuture.
+ *  Supported DisplayID versions:
+ *  VESA DisplayID Standard Verion 1 12/13/2007
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+#include <memory.h>
+
+#include <igd_errno.h>
+
+#include <displayid.h>
+#include <pi.h>
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+#ifndef CONFIG_NO_DISPLAYID
+
+/* IMP NOTE:
+ *     Keep the order of datablocks as it is.
+ *     DisplayID parser directly access the offset using tag as an index.
+ */
+unsigned short db_offset[] = {
+#ifdef CONFIG_MICRO
+	OS_OFFSETOF(displayid_t, dummy_db),        /* PRODUCTID        0x00 */
+	OS_OFFSETOF(displayid_t, display_params),  /* DISPLAY_PARAMS   0x01 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* COLOR_CHARS      0x02 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_1_DETAIL  0x03 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_2_DETAIL  0x04 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_3_SHORT   0x05 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_4_DMTID   0x06 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* VESA_TIMING_STD  0x07 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* CEA_TIMING_STD   0x08 */
+	OS_OFFSETOF(displayid_t, timing_range),    /* VIDEO_RANGE      0x09 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* SERIAL_NUMBER    0x0A */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* ASCII_STRING     0x0B */
+	OS_OFFSETOF(displayid_t, display_dev),     /* DISPLAY_DEVICE   0x0C */
+	OS_OFFSETOF(displayid_t, lvds),            /* LVDS_INTERFACE   0x0D */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TRANSFER_CHAR    0x0E */
+	OS_OFFSETOF(displayid_t, display_intf),    /* DISPLAY_INTF     0x0F */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* STEREO_INTF      0x10 */
+#else
+	OS_OFFSETOF(displayid_t, productid),       /* PRODUCTID        0x00 */
+	OS_OFFSETOF(displayid_t, display_params),  /* DISPLAY_PARAMS   0x01 */
+	OS_OFFSETOF(displayid_t, color_char),      /* COLOR_CHARS      0x02 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_1_DETAIL  0x03 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_2_DETAIL  0x04 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_3_SHORT   0x05 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* TIMING_4_DMTID   0x06 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* VESA_TIMING_STD  0x07 */
+	OS_OFFSETOF(displayid_t, dummy_db),        /* CEA_TIMING_STD   0x08 */
+	OS_OFFSETOF(displayid_t, timing_range),    /* VIDEO_RANGE      0x09 */
+	OS_OFFSETOF(displayid_t, serial_num),      /* SERIAL_NUMBER    0x0A */
+	OS_OFFSETOF(displayid_t, general_string),  /* ASCII_STRING     0x0B */
+	OS_OFFSETOF(displayid_t, display_dev),     /* DISPLAY_DEVICE   0x0C */
+	OS_OFFSETOF(displayid_t, lvds),            /* LVDS_INTERFACE   0x0D */
+	OS_OFFSETOF(displayid_t, transfer_char),   /* TRANSFER_CHAR    0x0E */
+	OS_OFFSETOF(displayid_t, display_intf),    /* DISPLAY_INTF     0x0F */
+	OS_OFFSETOF(displayid_t, stereo_intf),     /* STEREO_INTF      0x10 */
+#endif
+
+
+#if 0
+	/* Vendor specific tag is out of order, so it cannot be here. See
+	 * case DATABLOCK_VENDOR_SPECIFIC for implementation. */
+	OS_OFFSETOF(displayid_t, vendor),          /* VENDOR_SPECIFIC  0x7F */
+#endif
+};
+
+type_std_t vesa_std_lookup[] =
+{
+	/* width  height refresh flags */
+	/* byte 0 bit 0->7 */
+	{  640,    350,    85,   0                 },   /* bit 0 */
+	{  640,    400,    85,   0                 },   /* bit 1 */
+	{  720,    400,    85,   0                 },   /* bit 2 */
+	{  640,    480,    60,   0                 },   /* bit 3 */
+	{  640,    480,    72,   0                 },   /* bit 4 */
+	{  640,    480,    75,   0                 },   /* bit 5 */
+	{  640,    480,    85,   0                 },   /* bit 6 */
+	{  800,    600,    56,   0                 },   /* bit 7 */
+
+	/* byte 1 bit 0->7 */
+	{  800,    600,    60,   0                 },   /* bit 0 */
+	{  800,    600,    72,   0                 },   /* bit 1 */
+	{  800,    600,    75,   0                 },   /* bit 2 */
+	{  800,    600,    85,   0                 },   /* bit 3 */
+	{  800,    600,   120,   PD_MODE_RB        },   /* bit 4 */
+	{  848,    480,    60,   0                 },   /* bit 5 */
+	{ 1024,    768,    43,   PD_SCAN_INTERLACE },   /* bit 6 */
+	{ 1024,    768,    60,   0                 },   /* bit 7 */
+
+	/* byte 2 bit 0->7 */
+	{ 1024,    768,    70,   0                 },   /* bit 0 */
+	{ 1024,    768,    75,   0                 },   /* bit 1 */
+	{ 1024,    768,    85,   0                 },   /* bit 2 */
+	{ 1024,    768,   120,   PD_MODE_RB        },   /* bit 3 */
+	{ 1152,    864,    75,   0                 },   /* bit 4 */
+	{ 1280,    768,    60,   PD_MODE_RB        },   /* bit 5 */
+	{ 1280,    768,    60,   0                 },   /* bit 6 */
+	{ 1280,    768,    75,   0                 },   /* bit 7 */
+
+	/* byte 3 bit 0->7 */
+	{ 1280,    768,    85,   0                 },   /* bit 0 */
+	{ 1280,    768,   120,   PD_MODE_RB        },   /* bit 1 */
+	{ 1280,    800,    60,   PD_MODE_RB        },   /* bit 2 */
+	{ 1280,    800,    60,   0                 },   /* bit 3 */
+	{ 1280,    800,    75,   0                 },   /* bit 4 */
+	{ 1280,    800,    85,   0                 },   /* bit 5 */
+	{ 1280,    800,   120,   PD_MODE_RB        },   /* bit 6 */
+	{ 1280,    960,    60,   0                 },   /* bit 7 */
+
+	/* byte 4 bit 0->7 */
+	{ 1280,    960,    85,   0                 },   /* bit 0 */
+	{ 1280,    960,   120,   PD_MODE_RB        },   /* bit 1 */
+	{ 1280,   1024,    60,   0                 },   /* bit 2 */
+	{ 1280,   1024,    75,   0                 },   /* bit 3 */
+	{ 1280,   1024,    85,   0                 },   /* bit 4 */
+	{ 1280,   1024,   120,   PD_MODE_RB        },   /* bit 5 */
+	{ 1360,    768,    60,   0                 },   /* bit 6 */
+	{ 1360,    768,   120,   PD_MODE_RB        },   /* bit 7 */
+
+	/* byte 5 bit 0->7 */
+	{ 1400,   1050,    60,   PD_MODE_RB        },   /* bit 0 */
+	{ 1400,   1050,    60,   0                 },   /* bit 1 */
+	{ 1400,   1050,    75,   0                 },   /* bit 2 */
+	{ 1400,   1050,    85,   0                 },   /* bit 3 */
+	{ 1400,   1050,   120,   PD_MODE_RB        },   /* bit 4 */
+	{ 1440,    900,    60,   PD_MODE_RB        },   /* bit 5 */
+	{ 1440,    900,    60,   0                 },   /* bit 6 */
+	{ 1440,    900,    75,   0                 },   /* bit 7 */
+
+	/* byte 6 bit 0->7 */
+	{ 1440,    900,    85,   0                 },   /* bit 0 */
+	{ 1440,    900,   120,   PD_MODE_RB        },   /* bit 1 */
+	{ 1600,   1200,    60,   0                 },   /* bit 2 */
+	{ 1600,   1200,    65,   0                 },   /* bit 3 */
+	{ 1600,   1200,    70,   0                 },   /* bit 4 */
+	{ 1600,   1200,    75,   0                 },   /* bit 5 */
+	{ 1600,   1200,    85,   0                 },   /* bit 6 */
+	{ 1600,   1200,   120,   PD_MODE_RB        },   /* bit 7 */
+
+	/* byte 7 bit 0->7 */
+	{ 1680,   1050,    60,   PD_MODE_RB        },   /* bit 0 */
+	{ 1680,   1050,    60,   0                 },   /* bit 1 */
+	{ 1680,   1050,    75,   0                 },   /* bit 2 */
+	{ 1680,   1050,    85,   0                 },   /* bit 3 */
+	{ 1680,   1050,   120,   PD_MODE_RB        },   /* bit 4 */
+	{ 1792,   1344,    60,   0                 },   /* bit 5 */
+	{ 1792,   1344,    75,   0                 },   /* bit 6 */
+	{ 1792,   1344,   120,   PD_MODE_RB        },   /* bit 7 */
+
+	/* byte 8 bit 0->7 */
+	{ 1856,   1392,    60,   0                 },   /* bit 0 */
+	{ 1856,   1392,    75,   0                 },   /* bit 1 */
+	{ 1856,   1392,   120,   PD_MODE_RB        },   /* bit 2 */
+	{ 1920,   1200,    60,   PD_MODE_RB        },   /* bit 3 */
+	{ 1920,   1200,    60,   0                 },   /* bit 4 */
+	{ 1920,   1200,    75,   0                 },   /* bit 5 */
+	{ 1920,   1200,    85,   0                 },   /* bit 6 */
+	{ 1920,   1200,   120,   PD_MODE_RB        },   /* bit 7 */
+
+	/* byte 9 bit 0->7 */
+	{ 1920,   1440,    60,   0                 },   /* bit 0 */
+	{ 1920,   1440,    75,   0                 },   /* bit 1 */
+	{ 1920,   1440,   120,   PD_MODE_RB        },   /* bit 2 */
+	{ 2560,   1600,    60,   PD_MODE_RB        },   /* bit 3 */
+	{ 2560,   1600,    60,   0                 },   /* bit 4 */
+	{ 2560,   1600,    75,   0                 },   /* bit 5 */
+	{ 2560,   1600,    85,   0                 },   /* bit 6 */
+	{ 2560,   1600,   120,   PD_MODE_RB        },   /* bit 7 */
+};
+
+#ifndef CONFIG_MICRO
+/*!
+ * Function to replace common timings in 1st list with 2nd list, 2nd list
+ * is unchanged.
+ *
+ * @param dtds1
+ * @param dtds2
+ *
+ * @return void
+ */
+void replace_vesa_dtds_with_cea_dtds(igd_timing_info_t *dtds1,
+	igd_timing_info_t *dtds2)
+{
+	igd_timing_info_t *temp;
+
+	if (!dtds2 || !dtds1) {
+		return;
+	}
+
+	while (dtds1->width != IGD_TIMING_TABLE_END) {
+		temp = dtds2;
+
+		while (temp->width != IGD_TIMING_TABLE_END) {
+			if ((temp->width   == dtds1->width) &&
+				(temp->height  == dtds1->height) &&
+				(temp->refresh == dtds1->refresh)) {
+				dtds1->mode_info_flags &= ~PD_MODE_SUPPORTED;
+			}
+			temp++;
+		}
+		dtds1++;
+	}
+}
+#endif
+
+#ifdef DEBUG_FIRMWARE
+/*!
+ *
+ * @param db
+ *
+ * @return void
+ */
+void displayid_print_datablock(datablock_t *db)
+{
+	unsigned char payload_string[800];
+	unsigned char i, j;
+
+	/* Get the payload data into a string */
+	OS_MEMSET(payload_string, 0, sizeof(payload_string));
+	for (i=0, j=0; i<db->payload; i++,j+=5) {
+		payload_string[j] = '0';
+		payload_string[j+1] = 'x';
+		if ((db->payload_data[i]>>4) <= 0x9) {
+			payload_string[j+2] = '0' + (db->payload_data[i]>>4);
+		} else {
+			payload_string[j+2] = 'A' + (db->payload_data[i]>>4) - 0xA;
+		}
+		if ((db->payload_data[i] & 0x0F) <= 0x9) {
+			payload_string[j+3] = '0' + (db->payload_data[i]&0x0F);
+		} else {
+			payload_string[j+3] = 'A' + (db->payload_data[i]&0x0F) - 0xA;
+		}
+		payload_string[j+4] = ' ';
+	}
+	payload_string[j] = '\0';
+
+	EMGD_DEBUG("Tag = %u", db->tag);
+	EMGD_DEBUG("Version = %u", db->revision);
+	EMGD_DEBUG("Payload = %u", db->payload);
+	EMGD_DEBUG("Payload data = %s", payload_string);
+}
+
+/*!
+ *
+ * @param buffer
+ * @param did
+ *
+ * @return void
+ */
+void displayid_print(unsigned char *buffer, displayid_t *did)
+{
+	unsigned short i;
+	display_params_t *dp = &did->display_params;
+	timing_range_t   *tr = &did->timing_range;
+	lvds_display_t   *ld = &did->lvds;
+	display_dev_t    *dd = &did->display_dev;
+	display_intf_t   *di = &did->display_intf;
+#ifndef CONFIG_MICRO
+	productid_t      *pi = &did->productid;
+	color_char_t     *cc = &did->color_char;
+	serial_number_t  *sn = &did->serial_num;
+	general_string_t *gs = &did->general_string;
+	transfer_char_t  *tc = &did->transfer_char;
+	stereo_intf_t    *si = &did->stereo_intf;
+	vendor_t         *vi = &did->vendor;
+#endif
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("DisplayID Version: %d", did->version);
+	EMGD_DEBUG("DisplayID Revision: %d", did->revision);
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("Size of different structures:");
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("     displayid_t = %d", sizeof(displayid_t));
+	EMGD_DEBUG("display_params_t = %d", sizeof(display_params_t));
+	EMGD_DEBUG("     type1_dtd_t = %d", sizeof(type1_dtd_t));
+	EMGD_DEBUG("     type2_dtd_t = %d", sizeof(type2_dtd_t));
+	EMGD_DEBUG("     type3_cvt_t = %d", sizeof(type3_cvt_t));
+	EMGD_DEBUG("      type_std_t = %d", sizeof(type_std_t));
+	EMGD_DEBUG("  timing_range_t = %d", sizeof(timing_range_t));
+	EMGD_DEBUG("   display_dev_t = %d", sizeof(display_dev_t));
+	EMGD_DEBUG("  lvds_display_t = %d", sizeof(lvds_display_t));
+	EMGD_DEBUG("  display_intf_t = %d", sizeof(display_intf_t));
+	EMGD_DEBUG("        dummy_db = %d", 256);
+	EMGD_DEBUG("         timings = %d",
+		sizeof(pd_timing_t)*DISPLAYID_MAX_NUM_TIMINGS);
+	EMGD_DEBUG("           attrs = %d",
+		sizeof(pd_attr_t)*DISPLAYID_MAX_ATTRS);
+
+#ifndef CONFIG_MICRO
+	EMGD_DEBUG("     productid_t = %d", sizeof(productid_t));
+	EMGD_DEBUG("    color_char_t = %d", sizeof(color_char_t));
+	EMGD_DEBUG(" serial_number_t = %d", sizeof(serial_number_t));
+	EMGD_DEBUG("general_string_t = %d", sizeof(general_string_t));
+	EMGD_DEBUG(" transfer_char_t = %d", sizeof(transfer_char_t));
+	EMGD_DEBUG("   stereo_intf_t = %d", sizeof(stereo_intf_t));
+	EMGD_DEBUG("        vendor_t = %d", sizeof(vendor_t));
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("PRODUCT ID DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)pi);
+	EMGD_DEBUG("       vendor = %c%c%c",
+		pi->vendor[0], pi->vendor[1], pi->vendor[2]);
+	EMGD_DEBUG(" product_code = %u", pi->product_code);
+	EMGD_DEBUG("serial_number = %lu", pi->serial_number);
+	EMGD_DEBUG("    manf_week = %u", pi->manf_week);
+	EMGD_DEBUG("    manf_year = %u", pi->manf_year+2000);
+	EMGD_DEBUG("   string_len = %u", pi->string_size);
+	EMGD_DEBUG("       string = %s", pi->string);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("COLOR CHARACTERISTICS DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)cc);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("SERIAL NUMBER DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)sn);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("GENERAL PURPOSE ASCII STRING DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)gs);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("TRANSFER CHARACTERISTICS DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)tc);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("STEREO INTERFACE DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)si);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("VENDOR SPECIFIC DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)vi);
+#endif
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("DISPLAY PARAMETERS DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)dp);
+	EMGD_DEBUG("horz_image_size = %u", dp->horz_image_size);
+	EMGD_DEBUG("vert_image_size = %u", dp->vert_image_size);
+	EMGD_DEBUG("    horz_pixels = %u", dp->horz_pixels);
+	EMGD_DEBUG("    vert_pixels = %u", dp->vert_pixels);
+	EMGD_DEBUG(" deinterlacable = %u", dp->deinterlacing);
+	EMGD_DEBUG("   fixed_timing = %u", dp->fixed_timing);
+	EMGD_DEBUG("      fixed_res = %u", dp->fixed_res);
+	EMGD_DEBUG("   aspect_ratio = %u", dp->aspect_ratio);
+	EMGD_DEBUG(" native_color_depth(bppc) = %u", dp->native_color_depth+1);
+	EMGD_DEBUG("overall_color_depth(bppc) = %u", dp->overall_color_depth+1);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("VIDEO TIMING RANGESS DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)tr);
+	EMGD_DEBUG("  min_dclk = %lu KHz", tr->min_dclk);
+	EMGD_DEBUG("  max_dclk = %lu KHz", tr->max_dclk);
+	EMGD_DEBUG(" min_hrate = %u KHz", tr->min_hrate);
+	EMGD_DEBUG(" max_hrate = %u KHz", tr->max_hrate);
+	EMGD_DEBUG("min_hblank = %u pixels", tr->min_hblank);
+	EMGD_DEBUG(" min_vrate = %u Hz", tr->min_vrate);
+	EMGD_DEBUG(" max_vrate = %u Hz", tr->max_vrate);
+	EMGD_DEBUG("min_vblank = %u lines", tr->min_vblank);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("LVDS DISPLAY DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)ld);
+	EMGD_DEBUG("min_T1 = %u ms", ld->min_t1/10);
+	EMGD_DEBUG("max_T1 = %u ms", ld->max_t1*2);
+	EMGD_DEBUG("max_T2 = %u ms", ld->max_t2*2);
+	EMGD_DEBUG("max_T3 = %u ms", ld->max_t3*2);
+	EMGD_DEBUG("min_T4 = %u ms", ld->min_t4*10);
+	EMGD_DEBUG("min_T5 = %u ms", ld->min_t5*10);
+	EMGD_DEBUG("min_T6 = %u ms", ld->min_t6*10);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("DISPLAY DEVICE DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)dd);
+	EMGD_DEBUG("   horz_pixel_count = %u", dd->horz_pixel_count);
+	EMGD_DEBUG("   vert_pixel_count = %u", dd->vert_pixel_count);
+	EMGD_DEBUG("display_color_depth = %u", dd->display_color_depth);
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("DISPLAY INTERFACE DATA BLOCK");
+	DISPLAYID_PRINT_LINE();
+	displayid_print_datablock((datablock_t *)di);
+	EMGD_DEBUG("         num_channels = %u", di->num_channels);
+	EMGD_DEBUG("            intf_type = %u", di->intf_type);
+	EMGD_DEBUG("      RGB_color_depth = %u", di->rgb_color_depth);
+	EMGD_DEBUG("YCrCb_444_color_depth = %u", di->ycbcr_444_color_depth);
+	EMGD_DEBUG("YCrCb_422_color_depth = %u", di->ycbcr_422_color_depth);
+	if(di->intf_type == INTERFACE_LVDS) {
+		EMGD_DEBUG("         openldi = %u", di->lvds.openldi);
+	}
+
+	DISPLAYID_PRINT_LINE();
+	EMGD_DEBUG("Detailed Timing Descriptors");
+	DISPLAYID_PRINT_LINE();
+
+	for (i=0; i<did->num_timings; i++) {
+		EMGD_DEBUG("DTD: %u", i+1);
+		EMGD_DEBUG("          dclk = %lu", did->timings[i].dclk);
+		EMGD_DEBUG("       hactive = %u", did->timings[i].width);
+		EMGD_DEBUG("        htotal = %u", did->timings[i].htotal);
+		EMGD_DEBUG("  hblank_start = %u", did->timings[i].hblank_start);
+		EMGD_DEBUG("   hsync_start = %u", did->timings[i].hsync_start);
+		EMGD_DEBUG("     hsync_end = %u", did->timings[i].hsync_end);
+		EMGD_DEBUG("    hblank_end = %u", did->timings[i].hblank_end);
+		EMGD_DEBUG("       vactive = %u", did->timings[i].height);
+		EMGD_DEBUG("        vtotal = %u", did->timings[i].vtotal);
+		EMGD_DEBUG("  vblank_start = %u", did->timings[i].vblank_start);
+		EMGD_DEBUG("   vsync_start = %u", did->timings[i].vsync_start);
+		EMGD_DEBUG("     vsync_end = %u", did->timings[i].vsync_end);
+		EMGD_DEBUG("    vblank_end = %u", did->timings[i].vblank_end);
+		EMGD_DEBUG("        native = %u",
+			(did->timings[i].mode_info_flags&PD_MODE_DTD_FP_NATIVE)?1:0);
+		EMGD_DEBUG("     interlace = %u",
+			(did->timings[i].mode_info_flags&PD_SCAN_INTERLACE)?1:0);
+		EMGD_DEBUG("hsync_polarity = %s",
+			(did->timings[i].mode_info_flags & PD_HSYNC_HIGH)?
+			"ACTIVE HIGH":"ACTIVE LOW");
+		EMGD_DEBUG("vsync_polarity = %s",
+			(did->timings[i].mode_info_flags & PD_VSYNC_HIGH)?
+			"ACTIVE HIGH":"ACTIVE LOW");
+		DISPLAYID_PRINT_LINE();
+	}
+
+	/* Print the attributes */
+	if (did->num_attrs) {
+		EMGD_DEBUG("\tAttr\tID\tVALUE");
+		EMGD_DEBUG("----------------------");
+		for (i=0; i<did->num_attrs; i++) {
+			EMGD_DEBUG("\t%u\t%lu\t%lu", i+1, did->attr_list[i].id,
+				did->attr_list[i].current_value);
+		}
+		EMGD_DEBUG("----------------------");
+	}
+}
+#endif
+
+/*!
+ * Function to convert Type I - Detailed to pd_timing_t
+ *
+ * @param timing
+ * @param dtd
+ *
+ * @return void
+ */
+void convert_type1_to_pd(pd_timing_t *timing, type1_dtd_t *dtd)
+{
+	unsigned long refresh;
+	timing->dclk =                            /* change to KHz */
+		((unsigned long)dtd->dclk.lsb_dclk|
+		((unsigned long)dtd->dclk.msb_dclk<<16))*10;
+
+	/* DisplayID fields are 0 based but should be interpreted as 1-based.
+	 * For example hsync_width value can be read as 0-65,535 pixels but
+	 * interpreted as 1-65,536. So, to get the right value add 1.
+	 * But pd_timing_t values are 0 based except width and height,
+	 * so care should be taken while converting DisplayID fields into
+	 * pd_timing_t values */
+	timing->hblank_start = dtd->hactive;
+	timing->width = dtd->hactive + 1;
+	timing->hblank_end = timing->hblank_start + dtd->hblank + 1;
+	timing->hsync_start = timing->hblank_start + dtd->hsync_offset + 1;
+	timing->hsync_end = timing->hsync_start + dtd->hsync_width + 1;
+	timing->htotal = timing->hblank_end;
+
+	timing->vblank_start = dtd->vactive;
+	timing->height = dtd->vactive + 1;
+	timing->vblank_end = timing->vblank_start + dtd->vblank + 1;
+	timing->vsync_start = timing->vblank_start + dtd->vsync_offset + 1;
+	timing->vsync_end = timing->vsync_start + dtd->vsync_width + 1;
+	timing->vtotal = timing->vblank_end;
+
+	refresh = ((timing->dclk * 1000L)/timing->htotal)/timing->vtotal;
+	timing->refresh = (unsigned short) refresh;
+
+	timing->mode_info_flags = PD_MODE_DTD|PD_MODE_SUPPORTED;
+	if (dtd->hsync_polarity) {
+		timing->mode_info_flags |= PD_HSYNC_HIGH;
+	}
+	if (dtd->vsync_polarity) {
+		timing->mode_info_flags |= PD_VSYNC_HIGH;
+	}
+	if (dtd->interlaced) {
+		timing->mode_info_flags |= PD_SCAN_INTERLACE;
+	}
+	if (dtd->preferred) {
+		timing->mode_info_flags |= PD_MODE_DTD_FP_NATIVE;
+	}
+}
+
+/*!
+ * Function to convert Type II - Detailed to pd_timing_t
+ *
+ * @param timing
+ * @param dtd
+ *
+ * @return void
+ */
+void convert_type2_to_pd(pd_timing_t *timing, type2_dtd_t *dtd)
+{
+	unsigned long refresh;
+	timing->dclk =                            /* change to KHz */
+		((unsigned long)dtd->dclk.lsb_dclk|
+		((unsigned long)dtd->dclk.msb_dclk<<16))*10;
+
+	/* DisplayID fields are 0 based but should be interpreted as 1-based.
+	 * For example hsync_width value can be read as 0-15 OCTETs but
+	 * interpreted as 1-16 OCTETs. So, to get the right value add 1.
+	 * But pd_timing_t values are 0 based except width and height,
+	 * so care should be taken while converting DisplayID fields into
+	 * pd_timing_t values */
+	timing->width = (dtd->hactive + 1) * 8;    /* change to pixels */
+	timing->hblank_start = timing->width - 1;
+	timing->hblank_end = timing->hblank_start + (dtd->hblank + 1) * 8;
+	timing->hsync_start = timing->hblank_start + (dtd->hsync_offset + 1) * 8;
+	timing->hsync_end = timing->hsync_start + (dtd->hsync_width + 1) * 8;
+	timing->htotal = timing->hblank_end;
+
+	timing->vblank_start = dtd->vactive;
+	timing->height = dtd->vactive + 1;
+	timing->vblank_end = timing->vblank_start + dtd->vblank + 1;
+	timing->vsync_start = timing->vblank_start + dtd->vsync_offset + 1;
+	timing->vsync_end = timing->vsync_start + dtd->vsync_width + 1;
+	timing->vtotal = timing->vblank_end;
+
+	refresh = ((timing->dclk * 1000L)/timing->htotal)/timing->vtotal;
+	timing->refresh = (unsigned short) refresh;
+
+	timing->mode_info_flags = PD_MODE_DTD|PD_MODE_SUPPORTED;
+	if (dtd->interlaced) {
+		timing->mode_info_flags |= PD_SCAN_INTERLACE;
+	}
+	if (dtd->preferred) {
+		timing->mode_info_flags |= PD_MODE_DTD_FP_NATIVE;
+	}
+}
+
+/*!
+ * Function to filter timing table based on range block
+ *
+ * @param tt
+ * @param range
+ * @param firmware_type
+ *
+ * @return void
+ */
+void displayid_filter_range_timings(pd_timing_t *tt, timing_range_t *range,
+	unsigned char firmware_type)
+{
+	unsigned short hfreq;
+
+#define _HUNDRETHS(_n, _d)  ((100*_n)/_d)-((100*_n_d)/100),
+
+	#ifdef DEBUG_FIRMWARE
+	char result_str[60];
+	unsigned char pass_count = 0;
+	unsigned char fail_count = 0;
+
+	EMGD_DEBUG("Range limits:");
+	EMGD_DEBUG("\tmin_dclk = %lu KHz max_dclk = %lu KHz",
+		range->min_dclk, range->max_dclk);
+	EMGD_DEBUG("\t   h_min = %u h_max = %u KHz",
+		range->min_hrate, range->max_hrate);
+	EMGD_DEBUG("\t   v_min = %u v_max = %u",
+		range->min_vrate,range->max_vrate);
+	EMGD_DEBUG("WIDTH\tHEIGHT\tREFRESH\tH-FREQ\tDOTCLOCK\tRESULT");
+	EMGD_DEBUG("     \t      \t (Hz)  \t (KHz)\t (MHz)  \t      ");
+	EMGD_DEBUG("=====\t======\t=======\t======\t========\t======");
+#endif
+
+	/* If the display is a discreate frequency display, don't enable any
+	 * intermediate timings. Only continuous frequency displays requires
+	 * enabling range timings */
+	if (range->discrete_display) {
+		EMGD_DEBUG("Discrete display: Ranges aren't used.");
+		return;
+	}
+
+	/* If no timing table return */
+	if (tt == NULL) {
+		return;
+	}
+
+	/* Mark the timings that fall in the ranges */
+	/* Compare
+	 *     dclk in KHz
+	 *     hfreq in KHz
+	 *     vfreq in Hz */
+	while(tt->width != IGD_TIMING_TABLE_END) {
+		hfreq = (unsigned short)(tt->dclk/(unsigned long)tt->htotal); /* KHz */
+		if ((tt->dclk    >= (unsigned long)range->min_dclk)&&  /* compare KHz */
+			(tt->dclk    <= (unsigned long)range->max_dclk)&&  /* compare KHz */
+			(tt->refresh >= range->min_vrate) &&   /* compare Hz */
+			(tt->refresh <= range->max_vrate) &&   /* compare Hz */
+			(hfreq       >= range->min_hrate) &&   /* compare KHz */
+			(hfreq       <= range->max_hrate) &&   /* compare KHz */
+			(tt->hblank_end - tt->hblank_start) > range->min_hblank &&
+			(tt->vblank_end - tt->vblank_start) > range->min_vblank) {
+			tt->mode_info_flags |= PD_MODE_SUPPORTED;
+#ifdef DEBUG_FIRMWARE
+			if (tt->mode_info_flags & PD_MODE_SUPPORTED) {
+				EMGD_DEBUG("%5u\t%6u\t%7u\t%6u.%2u\t%8u.%2u\tPASSED",
+					tt->width, tt->height, tt->refresh,
+					tt->dclk/tt->htotal,
+					_HUNDRETHS(tt->dclk,tt->htotal),
+					tt->dclk/1000,
+					_HUNDRETHS(tt->dclk,1000);
+
+				pass_count++;
+			} else {
+			}
+#endif
+		} else {
+			/* Unmark the mode that falls out of range */
+			/* DTD, FACTORY and NATIVE timings are "GOLD" even if they
+			 * fall outside the range limits */
+			if (!(tt->mode_info_flags &
+				(PD_MODE_DTD|PD_MODE_FACTORY|PD_MODE_DTD_FP_NATIVE))) {
+#ifdef DEBUG_FIRMWARE
+				if ((tt->dclk <            /* compare KHz */
+						(unsigned long)range->min_dclk)||
+					(tt->dclk >
+						(unsigned long)range->max_dclk)) {
+					OS_MEMCPY(result_str, "FAILED DCLK    \0", 16);
+					fail_count++;
+				} else if ((tt->refresh > range->max_vrate) ||
+					(tt->refresh < range->min_vrate)) {
+					OS_MEMCPY(result_str, "FAILED REFRESH \0", 16);
+					fail_count++;
+				} else if ((hfreq < range->min_hrate) ||
+					(hfreq > range->max_hrate)) {
+					OS_MEMCPY(result_str, "FAILED H-FREQ  \0", 16);
+					fail_count++;
+				} else if ((tt->hblank_end-tt->hblank_start) <
+					range->min_hblank){
+					OS_MEMCPY(result_str, "FAILED MIN_HBLK\0", 16);
+				} else if ((tt->vblank_end-tt->vblank_start) <
+					range->min_vblank){
+					OS_MEMCPY(result_str, "FAILED MIN_VBLK\0", 16);
+				}
+				EMGD_DEBUG("%5u\t%6u\t%7u\t%6u.%2u\t%8u.%2u\t%s",
+					tt->width, tt->height, tt->refresh,
+					tt->dclk/tt->htotal,
+					_HUNDRETHS(tt->dclk,tt->htotal),
+					tt->dclk/1000,
+					_HUNDRETHS(tt->dclk,1000),
+					result_str);
+					((float) tt->dclk)/1000, result_str);
+
+				/* TODO: For multiple range blocks, don't disable the modes
+				 * that are outside the range. We already started with
+				 * an "empty supported table" */
+
+				/* But above assertion of "empty supported table" broke
+				 * if EDID ETF rules were met to enable all timings.
+				 * See edid.c for ETF conditions. So below line
+				 * cannot be commented out to support multiple range
+				 * blocks for DisplayID. */
+#endif
+				tt->mode_info_flags &= ~PD_MODE_SUPPORTED;
+			}
+		}
+		tt++;
+		if (tt->width == IGD_TIMING_TABLE_END && tt->extn_ptr) {
+			tt = tt->extn_ptr;
+		}
+	}
+#ifdef DEBUG_FIRMWARE
+	EMGD_DEBUG("pass count = %u, fail count = %u total = %u",
+		pass_count, fail_count, pass_count+fail_count);
+#endif
+} /* end displayid_filter_range_timings() */
+
+#define VESA_STD    1
+#define CEA_STD     2
+
+/*!
+ * Function to enable std timings: VESA STD or CEA STD
+ *
+ * @param tt1
+ * @param db_data
+ * @param lookup
+ * @param num_lookup
+ * @param std_type
+ *
+ * @return void
+ */
+void displayid_enable_std_timings(pd_timing_t *tt1, unsigned char *db_data,
+	type_std_t *lookup, unsigned short num_lookup, unsigned char std_type)
+{
+	unsigned short i;
+	pd_timing_t    *tt;
+	/* If no timing table return. This can happen if no edid_avail set not to
+	 * use std timings */
+	if (!tt1) {
+		return;
+	}
+
+	/* For every factory supported mode, enable it in the timing table */
+	for (i = 0; i < num_lookup; i++) {
+		tt = tt1;
+		/* i>>3 is nothing but dividing by 8, that gives the byte number,
+		 * i&0x7 is nothing but getting the bit position in that byte */
+		if (db_data[i>>3] & 1<<(i&0x7)) {
+			while(tt->width != IGD_TIMING_TABLE_END) {
+				if (lookup[i].width == tt->width &&
+					lookup[i].height == tt->height &&
+#if 0
+					(!((lookup[i].flags & (PD_SCAN_INTERLACE|PD_MODE_RB)) ^
+					(tt->mode_info_flags & (PD_SCAN_INTERLACE|PD_MODE_RB)))) &&
+#endif
+					(!((lookup[i].flags & PD_SCAN_INTERLACE) ^
+					(tt->mode_info_flags & PD_SCAN_INTERLACE))) &&
+					(!((lookup[i].flags & PD_ASPECT_16_9) ^
+					(tt->mode_info_flags & PD_ASPECT_16_9))) &&
+					lookup[i].refresh == tt->refresh) {
+					tt->mode_info_flags |= (PD_MODE_FACTORY|PD_MODE_SUPPORTED);
+					break;
+				}
+				tt++;
+				if (tt->width == IGD_TIMING_TABLE_END && tt->extn_ptr) {
+					tt = tt->extn_ptr;
+				}
+			}
+		}
+	}
+}
+
+static void displayid_parse_orientation_info (unsigned char orientation_blob,
+	igd_DID_rotation_info_t * rotation_info) {
+
+	unsigned int def_orientation = DEFAULT_ORIENTATION(orientation_blob);
+	unsigned int zero_pixel = ZERO_PIXEL(orientation_blob);
+	unsigned int scan_dir = SCAN_DIRECTION(orientation_blob);
+
+	/* Start with no rotation */
+	rotation_info->rotation = 0;
+	rotation_info->flip = 0;
+
+
+	if (def_orientation >= DEF_ORIENTATION_UNKNOWN) {
+		/* Display ID blob is corrupted or unknown configuration set */
+		EMGD_DEBUG("DisplayID: Unknown default orientation value: %d",
+			def_orientation);
+		return;
+	}
+
+	if (scan_dir == SCAN_DIRECTION_LONG) {
+		if (def_orientation == DEF_ORIENTATION_PORTRAIT) {
+			rotation_info->flip =
+				(zero_pixel == ZP_UPPER_LEFT || zero_pixel == ZP_LOWER_RIGHT);
+			rotation_info->rotation += 90;
+		} else { /* Landscape */
+			rotation_info->flip =
+				(zero_pixel == ZP_UPPER_RIGHT || zero_pixel == ZP_LOWER_LEFT);
+		}
+
+	} else if (scan_dir == SCAN_DIRECTION_SHORT) {
+		if (def_orientation == DEF_ORIENTATION_PORTRAIT) {
+			rotation_info->flip =
+				(zero_pixel == ZP_UPPER_RIGHT || zero_pixel == ZP_LOWER_LEFT);
+		}
+		else { /* Landscape */
+			rotation_info->flip =
+				(zero_pixel == ZP_UPPER_LEFT || zero_pixel == ZP_LOWER_RIGHT);
+			rotation_info->rotation += 90;
+		}
+	} else { /* Unknown scan direction */
+		EMGD_DEBUG("DisplayID: Unknown scan direction value: %d", scan_dir);
+		return;
+	}
+
+	/* zero pixel is in the lower-half: need to rotate by 180 degs */
+	if (zero_pixel == ZP_LOWER_LEFT || zero_pixel == ZP_LOWER_RIGHT)
+		rotation_info->rotation += 180;
+}
+
+/*!
+ * Function to parse DisplayID
+ *
+ * @param buffer
+ * @param did
+ * @param timing_table
+ * @param count
+ * @param upscale
+ *
+ * @return void
+ */
+int displayid_parse(
+		unsigned char *buffer,
+		displayid_t   *did,
+		pd_timing_t   *timing_table,
+		int           count,
+		unsigned char upscale)
+{
+	//unsigned char e = 0;
+	unsigned char checksum = 0, bytes_left;
+	unsigned short i;
+	unsigned short did_size;
+#ifndef CONFIG_MICRO
+	pd_timing_t   *cea_tmg_table;
+#endif
+	/* Read 4 bytes: (DisplayID Header)
+	 *       version, revision
+	 *       payload
+	 *       display product type identifier
+	 *       number of extensions */
+	*(unsigned long *) did = *(unsigned long *)buffer;
+
+	/* Check for version and revision */
+	if (did->version != 1 && did->revision != 0) {
+		EMGD_DEBUG("DisplayID Version %d.%d Unknown. Will Ignore.",
+			did->version, did->revision);
+		return DISPLAYID_NOT_SUPPORTED;
+	}
+
+	if (did->payload > 251) {
+		EMGD_DEBUG("DispID: Error: payload = %u not in [0..251]", did->payload);
+		return DISPLAYID_ERROR_PARSE;
+	}
+
+	/* Check sum check */
+	/* +5 is for 5 mandatory bytes */
+	did_size = (unsigned short) (did->payload + 5);
+	EMGD_DEBUG("DisplayID size = %u", did_size);
+	for (i = 0; i < did_size; i++) {
+		checksum += buffer[i];
+	}
+
+	/* bytes_left starts without DisplayID header */
+	bytes_left = did->payload;
+	/* current pointer is at 4 not at 5, because checksum byte is at the end */
+	buffer += 4;
+
+	if (checksum) {
+		EMGD_DEBUG("DisplayID checksum is incorrect! Will ignore.");
+		return DISPLAYID_ERROR_PARSE;
+	}
+
+	/* DisplayID parsing should start by disabling all modes.
+	 * Based on DisplayID data blocks modes will be enabled. */
+	enable_disable_timings(timing_table, 0);
+
+	/* Repeat for all extensions */
+	//e = did->num_extensions;
+	//while (e) {
+	{
+		//if (e != did->num_extensions) {
+			/* TODO: If there aren't enough bytes left in the buffer,
+			 * call I2C read function to read next DisplayID section */
+
+			/* Skip next section header 4 bytes */
+			//bytes_left -= 4;
+			//break;
+		//}
+
+		/* Parse Data Blocks */
+		/* Check minimum number of bytes required for Data Block were left */
+		while ((bytes_left > 3) && (bytes_left >= (buffer[2]+3))) {
+			unsigned char *db_data;
+			unsigned char payload = buffer[2] + 3;
+
+			/* displayid->datablock = buffer (for payload bytes) */
+			if (buffer[0] < sizeof(db_offset)/sizeof(unsigned short)) {
+				OS_MEMCPY(((unsigned char*)did) + db_offset[buffer[0]],
+					buffer, payload);
+			}
+
+			/* db_data points to payload data after db header (3 bytes),
+			 * Note: dummy_db offset is used for some DATA BLOCKS. See
+			 *       db_offset table above. */
+			db_data = (unsigned char *) &did->dummy_db[3];
+
+			switch (buffer[0]) {
+			/* Supported in Driver and VBIOS */
+			case DATABLOCK_DISPLAY_PARAMS:
+				/* Use following fields for fp_info:
+				 *     embedded use:      fixed timing
+				 *     horizontal pixels: fp_width
+				 *     vertical pixels:   fp_height */
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_PANEL_DEPTH;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					(did->display_params.overall_color_depth+1)*3;
+				break;
+
+			case DATABLOCK_TIMING_1_DETAIL:
+				/* One Type I block can have multiple DTDs */
+				while (payload>=20&&did->num_timings<DISPLAYID_MAX_NUM_TIMINGS){
+					convert_type1_to_pd(&did->timings[did->num_timings++],
+						(type1_dtd_t *)db_data);
+					db_data += 20;
+					payload -= 20;
+					bytes_left -= 20;
+					buffer += 20;
+				}
+				/* Mark the end of the list */
+				did->timings[did->num_timings].width = IGD_TIMING_TABLE_END;
+				break;
+
+			case DATABLOCK_TIMING_2_DETAIL:
+				/* One Type II block can have multiple DTDs */
+				while (payload>=11&&did->num_timings<DISPLAYID_MAX_NUM_TIMINGS){
+					convert_type2_to_pd(&did->timings[did->num_timings++],
+						(type2_dtd_t *)db_data);
+					db_data += 11;
+					payload -= 11;
+					bytes_left -= 11;
+					buffer += 11;
+				}
+				did->timings[did->num_timings].width = IGD_TIMING_TABLE_END;
+				break;
+
+			case DATABLOCK_VESA_TIMING_STD:
+				/* VESA Standard Timings */
+				displayid_enable_std_timings(
+					timing_table,
+					db_data,
+					vesa_std_lookup,
+					sizeof(vesa_std_lookup)/sizeof(type_std_t),
+					VESA_STD);
+				break;
+
+			case DATABLOCK_VIDEO_RANGE:
+				/* convert from Hz/10,000 -> KHz by multiplying by 10 */
+				did->timing_range.min_dclk =
+					((unsigned long)did->timing_range.mindclk.lsb_min_dclk|
+					((unsigned long)did->timing_range.mindclk.msb_min_dclk
+						<<16))*10;
+
+				did->timing_range.max_dclk =
+					((unsigned long)did->timing_range.maxdclk.lsb_max_dclk|
+					((unsigned long)did->timing_range.maxdclk.msb_max_dclk
+						<<16))*10;
+				displayid_filter_range_timings(timing_table,&did->timing_range,
+					PI_FIRMWARE_DISPLAYID);
+				break;
+
+			case DATABLOCK_DISPLAY_DEVICE:
+				/* Get panel color depth */
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_PANEL_DEPTH;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					(did->display_dev.display_color_depth+1)*3;
+				displayid_parse_orientation_info(did->display_dev.orientation,
+												&(did->rotation_info));
+				break;
+
+			case DATABLOCK_LVDS_INTERFACE:
+				/* Get T1-T5 values */
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_FP_PWR_T1;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					did->lvds.max_t1*2 + did->lvds.max_t2*2;
+
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_FP_PWR_T2;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					did->lvds.min_t5*10;
+
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_FP_PWR_T3;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					did->lvds.min_t6*10;
+
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_FP_PWR_T4;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					did->lvds.max_t3*2;
+
+				did->attr_list[did->num_attrs].id = PD_ATTR_ID_FP_PWR_T5;
+				did->attr_list[did->num_attrs].flags=PD_ATTR_FLAG_VALUE_CHANGED;
+				did->attr_list[did->num_attrs++].current_value =
+					did->lvds.min_t4*10 + did->lvds.max_t1*2;
+				break;
+
+			case DATABLOCK_DISPLAY_INTF:
+				if (did->display_intf.intf_type == INTERFACE_LVDS) {
+					/* Get number of channels: 0=singlechannel 1=dualchannel */
+					did->attr_list[did->num_attrs].id =
+						PD_ATTR_ID_2_CHANNEL_PANEL;
+					did->attr_list[did->num_attrs].flags =
+						PD_ATTR_FLAG_VALUE_CHANGED;
+					if (did->display_intf.num_channels == 2) {
+						did->attr_list[did->num_attrs++].current_value = 1;
+					}
+
+					/* Get panel type value: 0=normal 1=OpenLDI */
+					did->attr_list[did->num_attrs].id =
+						PD_ATTR_ID_LVDS_PANEL_TYPE;
+					did->attr_list[did->num_attrs].flags =
+						PD_ATTR_FLAG_VALUE_CHANGED;
+					did->attr_list[did->num_attrs++].current_value =
+						did->display_intf.lvds.openldi;
+				}
+
+				break;
+
+#ifndef CONFIG_MICRO
+			/* Support in Driver only */
+			case DATABLOCK_PRODUCTID:
+				break;
+
+			case DATABLOCK_SERIAL_NUMBER:
+				break;
+
+			case DATABLOCK_ASCII_STRING:
+				break;
+
+			case DATABLOCK_VENDOR_SPECIFIC:
+				/* Because vendor specific datablock tag is out-of-order,
+				 * copy data from buffer to vendor structure */
+				OS_MEMCPY(&did->vendor, buffer, buffer[2] + 3);
+				break;
+
+			/* Future support in Driver and VBIOS */
+			case DATABLOCK_TIMING_3_SHORT:
+				break;
+
+			case DATABLOCK_TIMING_4_DMTID:
+				break;
+
+			/* Future support in Driver */
+			case DATABLOCK_COLOR_CHARS:
+				break;
+
+			case DATABLOCK_CEA_TIMING_STD:
+				cea_tmg_table = (igd_timing_info_t *)
+					OS_ALLOC(cea_timing_table_size);
+				OS_MEMCPY(cea_tmg_table, cea_timing_table,
+					cea_timing_table_size);
+				/* Disable the CEA timings */
+				enable_disable_timings(cea_tmg_table, 0);
+				displayid_enable_std_timings(
+					cea_tmg_table,
+					db_data,
+					cea_std_lookup,
+					(unsigned short)cea_std_lookup_size,
+					CEA_STD);
+
+				replace_vesa_dtds_with_cea_dtds(timing_table, cea_tmg_table);
+				cea_tmg_table[cea_timing_table_size-1].extn_ptr =
+					(void *)timing_table;
+				timing_table = cea_tmg_table;
+				break;
+#endif
+			case DATABLOCK_TRANSFER_CHAR:
+				break;
+			}
+
+			/* Subtract data block payload */
+			bytes_left -= payload;
+			buffer += payload;
+		}
+		/* Extension count */
+		//e--;
+	}
+
+	return 0;
+}
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/edid.c b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/edid.c
new file mode 100644
index 0000000..d8a3c36
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/edid.c
@@ -0,0 +1,1187 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: edid.c
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is contains all necessary functions for EDID reading and
+ *  parsing into a data strucutures.
+ *  Supported EDID versions:
+ *  EDID 1.3 (backward compatible with 1.1, 1.2)
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+
+#include <igd_errno.h>
+
+#include <memory.h>
+#include <displayid.h>
+#include <edid.h>
+#include <pi.h>
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/* Function to read EDID */
+/* Local functions */
+static void edid_mark_standard_timings(
+	unsigned char *buffer,
+	pd_timing_t   *timings,
+	int           established);
+
+static void edid_mark_detailed_timings(
+	unsigned char *buffer,
+	edid_t        *edid,
+	pd_timing_t   *timings,
+	unsigned char upscale);
+
+static void edid_parse_monitor_name(
+	unsigned char *buffer,
+	char          *name);
+
+/* Edid Releated Constants */
+
+static const unsigned char name_blockid[] = {0x00, 0x00, 0x00, 0xfc};
+static const unsigned char range_blockid[] = {0x00, 0x00, 0x00, 0xfd};
+static const unsigned char st_blockid[] = {0x00, 0x00, 0x00, 0xfa};
+static const unsigned char timings_mask[] = {0xff, 0xff, 0xff, 0x00};
+extern igd_timing_info_t cea_timing_table[];
+/*
+ * Translation from the "Established" bit field position to a
+ * hactive, vactive, refresh that can be searched for in the
+ * timings list.
+ */
+static unsigned char established_table[] = {
+	/* 800x600 @ 60Hz */
+	0x20, 0x03, 0x58, 0x02, 0x3c, 0x00,
+	/* 800x600 @ 56Hz */
+	0x20, 0x03, 0x58, 0x02, 0x38, 0x00,
+	/* 640x480 @ 75Hz */
+	0x80, 0x02, 0xe0, 0x01, 0x4b, 0x00,
+	/* 640x480 @ 72Hz */
+	0x80, 0x02, 0xe0, 0x01, 0x48, 0x00,
+	/* 640x480 @ 67Hz */
+	0x80, 0x02, 0xe0, 0x01, 0x43, 0x00,
+	/* 640x480 @ 60Hz */
+	0x80, 0x02, 0xe0, 0x01, 0x3c, 0x00,
+	/* 720x400 @ 88Hz */
+	0xd0, 0x02, 0x90, 0x01, 0x58, 0x00,
+	/* 720x400 @ 70Hz */
+	0xd0, 0x02, 0x90, 0x01, 0x46, 0x00,
+
+	/* 1280x1024 @ 75Hz */
+	0x00, 0x05, 0x00, 0x04, 0x4b, 0x00,
+	/* 1024x768 @ 75Hz */
+	0x00, 0x04, 0x00, 0x03, 0x4b, 0x00,
+	/* 1024x768 @ 70Hz */
+	0x00, 0x04, 0x00, 0x03, 0x46, 0x00,
+	/* 1024x768 @ 60Hz */
+	0x00, 0x04, 0x00, 0x03, 0x3c, 0x00,
+	/* 1024x768 @ 43Hz (i) */
+	0x00, 0x04, 0x00, 0x03, 0x2b, 0x00,
+	/* 832x624 @ 75Hz */
+	0x40, 0x03, 0x70, 0x02, 0x4b, 0x00,
+	/* 800x600 @ 75Hz */
+	0x20, 0x03, 0x58, 0x02, 0x4b, 0x00,
+	/* 800x600 @ 72Hz */
+	0x20, 0x03, 0x58, 0x02, 0x48, 0x00,
+
+	/* 1152x870 @ 75Hz */
+	0x80, 0x04, 0x66, 0x03, 0x4b, 0x00
+};
+
+#ifdef DEBUG_FIRMWARE
+/*!
+ *
+ * @param timing
+ * @param edid
+ *
+ * @return void
+ */
+static void print_supported_timings(
+	pd_timing_t *timing, edid_t *edid)
+{
+	unsigned short i, count = 0;
+	if (timing != NULL) {
+		while (timing->width != IGD_TIMING_TABLE_END) {
+			if (timing->mode_info_flags & PD_MODE_SUPPORTED) {
+				EMGD_DEBUG("\t%4u\t%4u\t%4u", timing->width,
+					timing->height, timing->refresh);
+				count++;
+			}
+
+			timing++;
+			if (timing->width == IGD_TIMING_TABLE_END && timing->extn_ptr) {
+				timing = timing->extn_ptr;
+			}
+		}
+	}
+
+	if (edid && edid->num_timings) {
+		for (i=0; i<edid->num_timings; i++) {
+			if (edid->timings[i].mode_info_flags & PD_MODE_SUPPORTED) {
+				EMGD_DEBUG("\t%4u\t%4u\t%4u", edid->timings[i].width,
+					edid->timings[i].height, edid->timings[i].refresh);
+				count++;
+			}
+		}
+	}
+	EMGD_DEBUG("Total supported timings = %u", count);
+}
+
+/*!
+ * Dump the EDID to the kernel messages for debug
+ *
+ * @param buffer
+ * @param size
+ *
+ * @return void
+ */
+void firmware_dump(unsigned char *buffer, unsigned short size)
+{
+	unsigned short i;
+	EMGD_DEBUG("---------------------------------------------------------");
+	if (*(unsigned long *) &buffer[0] == 0xFFFFFF00 &&
+		*(unsigned long *) &buffer[4] == 0x00FFFFFF) {
+		size = 128;
+		EMGD_DEBUG("EDID DUMP (size = %u):", size);
+	} else {
+		/* For DisplayID payload is at byte 2 */
+		/* +5 is for 5 mandatory bytes */
+		size = buffer[1]+5;
+		EMGD_DEBUG("DisplayID DUMP (size = %u):", size);
+	}
+	EMGD_DEBUG("-------+-------------------------------------------------");
+	EMGD_DEBUG("Offset | Data.....");
+	EMGD_DEBUG("-------+-------------------------------------------------");
+
+	for (i=0; i<size; i+=8) {
+		EMGD_DEBUG("  0x%2.2x | 0x%2.2x, 0x%2.2x, 0x%2.2x, 0x%2.2x, 0x%2.2x, 0x%2.2x, 0x%2.2x, 0x%2.2x",
+			i, buffer[i], buffer[i+1], buffer[i+2], buffer[i+3],
+			buffer[i+4], buffer[i+5], buffer[i+6], buffer[i+7]);
+	}
+	if (i>size) {
+		EMGD_DEBUG("Note: Ignore last row last %u bytes.", i-size);
+	}
+	EMGD_DEBUG("-------+-------------------------------------------------");
+	EMGD_DEBUG(" ");
+} /* edid_dump() */
+
+/*!
+ * Print the EDID Structure
+ *
+ * @param buffer
+ * @param size
+ *
+ * @return void
+ */
+void edid_print(edid_t *edid)
+{
+	int i;
+
+	EMGD_DEBUG("EDID Version: %d", edid->version);
+	EMGD_DEBUG("EDID Revision: %d", edid->revision);
+	EMGD_DEBUG("Vendor %s", edid->vendor);
+	EMGD_DEBUG("Model: %s", edid->name);
+	EMGD_DEBUG("Product Code: %ld", edid->product_code);
+	EMGD_DEBUG("Serial Number: %lu", edid->serial_number);
+	EMGD_DEBUG("Manufactored week: %d", edid->manf_week);
+	EMGD_DEBUG("Manufactored year: %ld", edid->manf_year);
+	EMGD_DEBUG("DPMS Flags: 0x%u", edid->dpms);
+	EMGD_DEBUG("Max Pixel Clock: %lu KHz", edid->range.max_dclk);
+	EMGD_DEBUG("H Range: %u - %u KHz",
+		edid->range.min_hrate, edid->range.max_hrate);
+	EMGD_DEBUG("V Range: %u - %u Hz",
+		edid->range.min_vrate, edid->range.max_vrate);
+
+	for (i=0; i<edid->num_timings; i++) {
+		EMGD_DEBUG("Detailied Timing Descriptor(DTD) %u", i+1);
+		EMGD_DEBUG("          dclk = %lu", edid->timings[i].dclk);
+		EMGD_DEBUG("       hactive = %u", edid->timings[i].width);
+		EMGD_DEBUG("        htotal = %u", edid->timings[i].htotal);
+		EMGD_DEBUG("  hblank_start = %u", edid->timings[i].hblank_start);
+		EMGD_DEBUG("   hsync_start = %u", edid->timings[i].hsync_start);
+		EMGD_DEBUG("     hsync_end = %u", edid->timings[i].hsync_end);
+		EMGD_DEBUG("    hblank_end = %u", edid->timings[i].hblank_end);
+		EMGD_DEBUG("       vactive = %u", edid->timings[i].height);
+		EMGD_DEBUG("        vtotal = %u", edid->timings[i].vtotal);
+		EMGD_DEBUG("  vblank_start = %u", edid->timings[i].vblank_start);
+		EMGD_DEBUG("   vsync_start = %u", edid->timings[i].vsync_start);
+		EMGD_DEBUG("     vsync_end = %u", edid->timings[i].vsync_end);
+		EMGD_DEBUG("    vblank_end = %u", edid->timings[i].vblank_end);
+		EMGD_DEBUG("     interlace = %u",
+			(edid->timings[i].mode_info_flags&PD_SCAN_INTERLACE)?1:0);
+		EMGD_DEBUG("hsync_polarity = %s",
+			(edid->timings[i].mode_info_flags & PD_HSYNC_HIGH)?
+			"ACTIVE HIGH":"ACTIVE LOW");
+		EMGD_DEBUG("vsync_polarity = %s",
+			(edid->timings[i].mode_info_flags & PD_VSYNC_HIGH)?
+			"ACTIVE HIGH":"ACTIVE LOW");
+	}
+} /* edid_print() */
+#endif
+
+/*!
+ * Function to disable all timings before marking supportecd timings
+ *
+ * @param timing
+ * @param enable
+ *
+ * @return void
+ */
+void enable_disable_timings(pd_timing_t *timing, unsigned char enable)
+{
+	if (!timing) {
+		return;
+	}
+
+	while(timing->width != IGD_TIMING_TABLE_END) {
+		if (enable) {
+			timing->mode_info_flags |= PD_MODE_SUPPORTED;
+		} else {
+			timing->mode_info_flags &= ~PD_MODE_SUPPORTED;
+		}
+		timing++;
+		if (timing->width == IGD_TIMING_TABLE_END && timing->extn_ptr) {
+			timing = timing->extn_ptr;
+		}
+	}
+}
+
+/*!
+ * Function to enable timings based on port driver flags
+ *
+ * @param timing
+ * @param enable
+ *
+ * @return void
+ */
+void enable_scaled_timings(pd_timing_t *timing, pd_timing_t *dtd,
+	unsigned char upscale)
+{
+
+	if (!upscale || !dtd || !timing) {
+		return;
+	}
+
+	/* If port driver supports up scaling, enable all smaller modes. */
+	while(timing->width != IGD_TIMING_TABLE_END) {
+		if ((timing->width <= dtd->width) &&
+			(timing->height <= dtd->height) &&
+			(timing->refresh <= dtd->refresh)) {
+			timing->mode_info_flags |= IGD_MODE_SUPPORTED;
+		}
+		timing++;
+
+		/* If reached the first table END,
+		 * then check for the added modes */
+		if (timing->width == IGD_TIMING_TABLE_END &&
+			timing->extn_ptr) {
+			timing = timing->extn_ptr;
+		}
+	}
+}
+
+/*!
+ * Parse an Edid Structure from a provided 128 byte buffer of data.
+ * This function will return EDID_READ_AGAIN if the driver should
+ * read another 128 bytes and call this function again. This will
+ * happen with EDID structures with one or more extended 128 byte regions.
+ * count is 0 on the first call and incremented with each extra 128
+ * byte buffer.
+ *
+ * This parser skips information that is not important to the driver.
+ * There should be an interface to provide the whole EDID to userspace
+ * where more advanced parsing can happen. If you need something exotic
+ * out of the EDID consider having a user app do it instead of making
+ * This in-kernel EDID parser large.
+ *
+ * Timings found in the "Established" and "Standard" bitfields or
+ * extension blocks will be looked up in the provided timings list.
+ * If a match is found the timings will be marked with
+ * PD_MODE_SUPPORTED.
+ *
+ * If the EDID provides frequency ranges all timings within the range will
+ * be marked with PD_MODE_SUPPORTED in timing_table.
+ *
+ * If a driver uses a single timings list for multiple displays the driver
+ * will have to copy the flags mentioned above into a bit range specific
+ * to each display (in the driver specific bit range).
+ *
+ * @param buffer
+ * @param edid
+ * @param timing_table
+ * @param count
+ * @param upscale
+ *
+ * @return 0 on success
+ * @return EDID_ERROR_PARSE on failure
+ */
+int edid_parse(
+		unsigned char *buffer,
+		edid_t        *edid,
+		pd_timing_t   *timing_table,
+		int           count,
+		unsigned char upscale)
+{
+	int           chksum = 0, i, j;
+	unsigned long version, vendor, temp32;
+	pd_timing_t   *timing;
+
+	if (!edid || !buffer) {
+		return EDID_ERROR_PARSE;
+	}
+
+	/* No count means, edid_parse() called for first time */
+	if (!count) {
+		OS_MEMSET(edid, 0, sizeof(edid_t));
+	}
+
+	/* Check the checksum */
+	for (i=0; i<128; i++) {
+		chksum += buffer[i];
+	}
+
+	if ((chksum & 0xff) != 0) {
+		EMGD_DEBUG("EDID checksum is incorrect! Will ignore.");
+		return EDID_ERROR_PARSE;
+	}
+
+	/* FIXME: Verify this, I think extra EDID blocks have no header */
+	if (count) {
+		/*	goto BLOCKS; */
+		return EDID_ERROR_PARSE;
+	}
+
+	/* Header already checked. Skip 8 bytes */
+	buffer+=8;
+
+	/* Vendor Name */
+	vendor = (buffer[0]<<8) | buffer[1];
+	*(unsigned long *)edid->vendor = ((vendor>>10) + 0x40) +
+		((((vendor>>5) & 0x1f) + 0x40)<<8) +
+		((unsigned long)((vendor & 0x1f) + 0x40)<<16);
+	buffer+=2;
+
+	/* Product Code */
+	edid->product_code = (buffer[1]<<8) | buffer[0];
+	buffer+=2;
+
+	/* Serial Number 4 bytes (SKIP)*/
+	edid->serial_number = buffer[0] |
+				(buffer[1]<<8) |
+				((unsigned long)(buffer[2])<<16) |
+				((unsigned long)(buffer[3])<<24);
+	buffer += 4;
+
+	/* Manufactured Week 1 byte (SKIP) */
+	edid->manf_week = buffer[0];
+	buffer++;
+
+	/* Manufactured Year 1 byte (SKIP) */
+	edid->manf_year = 1990 + buffer[0];
+	buffer++;
+
+	/* EDID Version/Revision 0x12-0x13 */
+	version = (buffer[0]<<8) | buffer[1];
+	edid->version = buffer[0];
+	edid->revision = buffer[1];
+	buffer += 2;
+
+	switch(version) {
+	case 0x0:
+	case 0x0101:
+	case 0x0102:
+	case 0x0103:
+		/* Possibly do something based on this */
+		break;
+	default:
+		EMGD_DEBUG("EDID Version %d.%d Unknown. Will Ignore.",
+			edid->version, edid->revision);
+		return EDID_ERROR_PARSE;
+		break;
+	}
+
+	/* Basic Display Params (SKIP) */
+	buffer+=4;
+
+	/* DPMS and Features 1 byte  */
+	edid->dpms = (buffer[0]>>5);
+	edid->display_type = (buffer[0]>>3) & 0x3;
+	edid->standard_color = (buffer[0]>>2) & 0x1;
+	edid->preferred_timing = (buffer[0]>>1) & 0x1;
+	edid->gtf = (buffer[0] & 0x1);
+	buffer++;
+
+	/* Color 10 bytes (SKIP)*/
+	buffer+=10;
+
+	/* EDID parsing should start by disabling all modes.
+	 * Based on EDID data, modes will be enabled. */
+	enable_disable_timings(timing_table, 0);
+
+	/* Based on the established timings provided in the EDID, mark the
+	 * timings in the table as below (as per meeting with MikeD on 7/11/03):
+	 *
+	 * 1. If all VESA ETF and 640x480 modes are supported,
+	 *     then mark all modes in the table as supported + DTD modes
+	 * 2. If all VESA ETF 60Hz modes are supported,
+	 *     then mark all 60Hz modes in the table as supported + DTD modes
+	 * 3. Default:
+	 *     Enable only ETF modes in the table + DTD modes
+	 *
+	 * Established Timings 3 bytes (SKIP bits 6:0 of byte 3 which contains
+	 * manufactorer's proprietary timings), so there are 17 established
+	 * timings. */
+
+	/* Move byte2 bit7 to byte2 bit0. This is done to maintain
+	 * continuation along with other 16 established timings */
+	temp32 = buffer[0] | ((unsigned long)(buffer[1])<<8) |
+		((unsigned long)(buffer[2]>>7)<<16);
+
+	/* Here are the bit definitions:
+	 *  Byte 0
+	 *       bit 0 - 800 x 600 @ 60Hz    VESA
+	 *       bit 1 - 800 x 600 @ 56Hz    VESA
+	 *       bit 2 - 640 x 480 @ 75Hz    VESA
+	 *       bit 3 - 640 x 480 @ 72Hz    VESA
+	 *       bit 4 - 640 x 480 @ 67Hz    Apple, Mac II
+	 *       bit 5 - 640 x 480 @ 60Hz    IBM, VGA
+	 *       bit 6 - 720 x 400 @ 88Hz    IBM, XGA2
+	 *       bit 7 - 720 x 400 @ 70Hz    IBM, VGA
+	 *  Byte 1
+	 *       bit 0 - 1280 x 1024 @ 75Hz  VESA
+	 *       bit 1 - 1024 x  768 @ 75Hz  VESA
+	 *       bit 2 - 1024 x  768 @ 70Hz  VESA
+	 *       bit 3 - 1024 x  768 @ 60Hz  VESA
+	 *       bit 4 - 1024 x  768 @ 87Hz  IBM (Interlaced)
+	 *       bit 5 -  832 x  624 @ 75Hz  Apple, Mac II
+	 *       bit 6 -  800 x  600 @ 75Hz  VESA
+	 *       bit 7 -  800 x  600 @ 72Hz  VESA
+	 *  Byte 2
+	 *       bit 0 - 1152 x  870 @ 75Hz  Apple, Mac II
+	 *
+	 * Note:
+	 *  Byte2 bit 0 used to be at Byte 2 bit 7 but moved to bit 0 for sake of
+	 *  continuality.
+	 */
+	for (i=0; i<=16; i++) {
+		if (temp32 & (1L<<i)) {
+			edid_mark_standard_timings(&established_table[i*6],
+				timing_table, 1);
+		}
+	}
+
+#ifdef DEBUG_FIRMWARE
+	EMGD_DEBUG("Supported timings after FIRST established timings.");
+	print_supported_timings(timing_table, edid);
+#endif
+
+	if (timing_table != NULL) {
+		/* Mark the other timings in the table, as per above notes. */
+		timing = timing_table;
+		while (timing->width != IGD_TIMING_TABLE_END) {
+			if ((temp32 & 0xCF2F) == 0xCF2F) {            /* Case 1 */
+				timing->mode_info_flags |= PD_MODE_SUPPORTED;
+			} else if (((temp32 & 0xCF2F) == 0x821) &&    /* Case 2 */
+				(timing->refresh == 60)) {
+				timing->mode_info_flags |= PD_MODE_SUPPORTED;
+			}
+
+			timing++;
+			if (timing->width == IGD_TIMING_TABLE_END && timing->extn_ptr) {
+				timing = timing->extn_ptr;
+			}
+		}
+	}
+
+	buffer+=3;
+
+	/* Standard Timings 16 bytes, Look these up in the standard
+	 * timings table to find the match. Skip any that are not found
+	 * in the table.
+	 */
+	for (i=0; i<8; i++) {
+		/* First 8 standard timings */
+		edid_mark_standard_timings(buffer, timing_table, 0);
+		buffer+=2;
+	}
+
+#ifdef DEBUG_FIRMWARE
+	EMGD_DEBUG("Supported timings after SECOND established timings.");
+	print_supported_timings(timing_table, edid);
+#endif
+
+	/* BLOCKS: */
+	/* Blocks of Data */
+	for (i=0; i<4; i++) {
+		if (*(unsigned long *)buffer == *(unsigned long *)name_blockid) {
+			/* Monitor Name */
+			edid_parse_monitor_name(&buffer[5], edid->name);
+			buffer+=18;
+			continue;
+		}
+		if (*(unsigned long *)buffer == *(unsigned long *)range_blockid) {
+			/* Monitor Limits */
+			edid->range_set = 1;
+			edid->range.min_vrate = buffer[5];      /* Hz */
+			edid->range.max_vrate = buffer[6];      /* Hz */
+			edid->range.min_hrate = buffer[7];      /* KHz */
+			edid->range.max_hrate = buffer[8];      /* KHz */
+			edid->range.max_dclk  =
+				(unsigned long)buffer[9]*10000L; /* convert from MHz/10->KHz */
+			/* Following are always zero:
+			 * edid->range.min_dclk
+			 * edid->range->min_hblank
+			 * edid->range->min_vblank */
+
+			/* Call common function to filter timings based on range limits */
+			displayid_filter_range_timings(timing_table, &edid->range,
+				PI_FIRMWARE_EDID);
+
+			/* Fixme GTF */
+			buffer+=18;
+			continue;
+		}
+		if (*(unsigned long *)buffer == *(unsigned long *)st_blockid) {
+			/* Additional 6 Standard Timings */
+			buffer+=5;
+			for (j=0; j<12; j+=2) {
+				edid_mark_standard_timings(&buffer[j], timing_table, 0);
+			}
+			buffer+=13;
+			continue;
+		}
+		if (*(unsigned long *)buffer & *(unsigned long *)timings_mask) {
+			/* Detailed Timings */
+			if (edid->num_timings >= NUM_TIMINGS-1) {
+				continue;
+			}
+			edid_mark_detailed_timings(buffer, edid, timing_table, upscale);
+			buffer+=18;
+			continue;
+		}
+		/* Don't handle Block types that don't matter */
+		buffer+=18;
+	} /* end for loop */
+
+#ifdef DEBUG_FIRMWARE
+	EMGD_DEBUG("FINAL supported timings .");
+	print_supported_timings(timing_table, edid);
+#endif
+
+	/* Extensions */
+	if (*buffer) {
+		return EDID_READ_AGAIN;
+	}
+
+	return 0;
+} /* end edid_parse() */
+
+/*!
+ * Given the two byte Standard Timings Identifier, Mark the timing
+ * that matches in the timings array.
+ * If "established" is set, the two byte "EDID id" will be used,
+ * otherwise the buffer should contain the hactive, followed by
+ * the vactive followed by the refresh, all in 16 bit LSB format.
+ *
+ * @param buffer
+ * @param timings
+ * @param established
+ *
+ * @return void
+ */
+static void edid_mark_standard_timings(
+		unsigned char *buffer,
+		pd_timing_t   *timings,
+		int           established)
+{
+	int hactive, vactive=0, refresh, aspect;
+
+	if ((!established) && (buffer[0] == 0x01) && (buffer[1] == 0x01)) {
+		return;
+	}
+	if (established) {
+		hactive = buffer[0] | (buffer[1]<<8);
+		vactive = buffer[2] | (buffer[3]<<8);
+		refresh = buffer[4] | (buffer[5]<<8);
+	}
+	else {
+		hactive = (buffer[0] + 31)<<3;
+		refresh = (buffer[1] & 0x1f) + 60;
+		aspect = buffer[1]>>6;
+		switch(aspect) {
+		case 0x0:
+			vactive = (hactive*10)>>4;
+			break;
+		case 0x1:
+			vactive = (hactive*3)>>2;
+			break;
+		case 0x2:
+			vactive = (hactive<<2)/5;
+			break;
+		case 0x3:
+			vactive = (hactive*9)>>4;
+			break;
+		default:
+			EMGD_ERROR("Invalid aspect ratio in EDID.");
+			return;
+		}
+	}
+
+	if (timings != NULL) {
+		/* Look for mode in table */
+		while (timings->width != IGD_TIMING_TABLE_END) {
+			/* By default no mode is supported. */
+			if ((timings->refresh == refresh) &&
+				(timings->width == hactive) &&
+				(timings->height == vactive)) {
+				timings->mode_info_flags |= PD_MODE_SUPPORTED;
+				return;
+			}
+
+			timings++;
+
+			if (timings->width == IGD_TIMING_TABLE_END && timings->extn_ptr) {
+				timings = timings->extn_ptr;
+			}
+		}
+	}
+
+	return;
+} /* end edid_mark_standard_timings() */
+
+/*!
+ * Parse a detailed timing block from the buffer provided and set the
+ * data in the pd_timing_t structure provided.
+ *
+ * @param buffer
+ * @param edid
+ * @param timing_table
+ * @param upscale
+ *
+ * @return void
+ */
+static void edid_mark_detailed_timings(
+		unsigned char *buffer,
+		edid_t        *edid,
+		pd_timing_t   *timing_table,
+		unsigned char upscale)
+{
+	unsigned long temp;
+	pd_timing_t *timings = &(edid->timings[edid->num_timings]);
+
+	timings->dclk = ((unsigned long)(buffer[1]<<8) | buffer[0]) * 10;  /* KHz */
+	timings->width = ((buffer[4] & 0xf0)<<4) | buffer[2];
+	if (timings->width == 0) {
+		/* Bail out as DTD is invalid. This happens as we are estimating the
+		 * amount of availabel DTD since manufacturer does not follow CEA
+		 * format */
+		timings->width = IGD_TIMING_TABLE_END;
+		return;
+	}
+	timings->hblank_start = timings->width - 1;
+	timings->hblank_end = timings->width +
+		(((buffer[4]& 0xf)<<8) | buffer[3]) - 1;
+	timings->height = ((buffer[7] & 0xf0)<<4) | buffer[5];
+	timings->vblank_start = timings->height - 1;
+	timings->vblank_end = timings->height +
+		(((buffer[7]& 0xf)<<8)| buffer[6]) - 1;
+	timings->hsync_start = timings->hblank_start +
+		(((buffer[11] & 0xc0)<<2) | buffer[8]);
+	timings->hsync_end = timings->hsync_start +
+		(((buffer[11] & 0x30)<<4) | buffer[9]);
+	timings->vsync_start = timings->vblank_start +
+		(((buffer[11] & 0xc)<<2) | (buffer[10]>>4));
+	timings->vsync_end = timings->vsync_start +
+		(((buffer[11] & 0x3)<<4) | (buffer[10] & 0xf));
+	timings->htotal = timings->hblank_end;
+	timings->vtotal = timings->vblank_end;
+
+	/* SKIP BORDER */
+	timings->mode_info_flags |= (buffer[17] & 0x80)?PD_SCAN_INTERLACE:0;
+
+	/* Need to divide width by 2 and set PIXEL_DOUBLE flag in order to properly */
+	/* handle clock doubled modes like 1440(720)x480i and 1440(720)x576i */
+	if( (timings->mode_info_flags & PD_SCAN_INTERLACE) && (timings->width == 1440) &&
+	    ((timings->height == 480) || (timings->height == 576)) )
+	{
+		timings->width /= 2;
+		timings->mode_info_flags |= IGD_PIXEL_DOUBLE;
+	}else if((timings->mode_info_flags & PD_SCAN_INTERLACE)){
+		/* The height needs to be multiplied by 2 here so OS would prepare the
+		   correct frame buffer size. Pipe programming needs to be aware of this */
+		timings->height			*= 2;
+		timings->vtotal			*= 2;
+		timings->vblank_start	*= 2;
+		timings->vblank_end		*= 2;
+		timings->vsync_start	*= 2;
+		timings->vsync_end		*= 2;
+	}
+
+	/* Set sync polarities */
+	if((buffer[17] & 0x18)==0x18) { /* Bit 4 and 3 above = 11*/
+		/* if Bit 2 = 1, then vsync polarity = positive */
+		timings->mode_info_flags |= (buffer[17] & 0x04)? IGD_VSYNC_HIGH: 0;
+		/* if Bit 1 = 1, then hsync polarity = positive */
+		timings->mode_info_flags |= (buffer[17] & 0x02)? IGD_HSYNC_HIGH: 0;
+	}
+
+	if((buffer[17] & 0x18)==0x10) { /* Bit 4 and 3 above = 10*/
+		/* if Bit 1 = 1, then hsync polarity = positive */
+		timings->mode_info_flags |= (buffer[17] & 0x02)? IGD_HSYNC_HIGH: 0;
+	}
+
+	if (timings->dclk == 0) {
+		timings->dclk = 1;
+	}
+	if (timings->htotal == 0) {
+		timings->htotal = 1;
+	}
+	if (timings->vtotal == 0) {
+		timings->vtotal = 1;
+	}
+
+	temp = (unsigned long)timings->htotal * (unsigned long)timings->vtotal;
+	timings->refresh = (unsigned short)((timings->dclk * 1000)/temp);
+	timings->mode_info_flags |= (PD_MODE_SUPPORTED | PD_MODE_DTD);
+
+	/* Save in the detailed timings of EDID */
+	edid->num_timings++;
+	/* Mark the end of the list */
+	edid->timings[edid->num_timings].width = IGD_TIMING_TABLE_END;
+
+	/* Enable scale timings */
+	enable_scaled_timings(timing_table, timings, upscale);
+} /* end edid_mark_detailed_timings() */
+
+/*!
+ * Get the 13 (or less) character Monitor name from the buffer and
+ * pad with nulls.
+ *
+ * @param buffer
+ * @param name
+ *
+ * @return void
+ */
+static void edid_parse_monitor_name(unsigned char *buffer, char *name)
+{
+	int i;
+	for (i=0; i<13; i++) {
+		if (buffer[i] == 0x0a) {
+			name[i] = 0;
+			break;
+		}
+		name[i] = buffer[i];
+	}
+	while (i<14) {
+		name[i] = 0;
+		i++;
+	}
+} /* edid_parse_monitor_name() */
+
+#ifndef CONFIG_MICRO
+/*!
+ *
+ * @param edid
+ *
+ * @return 0
+ */
+int parse_audio_block(edid_t *edid)
+{
+	int i;
+	cea_audio_format_t *tmp_audio;
+
+	for(i=0; i<(edid->cea->total_short_audio_desc); i++){
+		tmp_audio = edid->cea->short_audio_desc + i;
+		if(tmp_audio->audio_format_code == CEA_AUDIO_LPCM){
+			if(tmp_audio->_48khz){
+				edid->cea->audio_cap[0].max_channels = tmp_audio->max_channels;
+				edid->cea->audio_cap[0]._20bit = tmp_audio->_20bit;
+				edid->cea->audio_cap[0]._24bit = tmp_audio->_24bit;
+			}
+			if(tmp_audio->_96khz){
+				edid->cea->audio_cap[1].max_channels = tmp_audio->max_channels;
+				edid->cea->audio_cap[1]._20bit = tmp_audio->_20bit;
+				edid->cea->audio_cap[1]._24bit = tmp_audio->_24bit;
+				if(tmp_audio->_88khz){
+					edid->cea->_44ms = 1;
+				}
+			}
+			if(tmp_audio->_192khz){
+				edid->cea->audio_cap[2].max_channels = tmp_audio->max_channels;
+				edid->cea->audio_cap[2]._20bit = tmp_audio->_20bit;
+				edid->cea->audio_cap[2]._24bit = tmp_audio->_24bit;
+				if(tmp_audio->_176khz){
+					edid->cea->_44ms = 1;
+				}
+			}
+		}
+	}
+	return 0;
+}
+
+/*!
+ *
+ * @param buffer
+ * @param list
+ * @param block_size
+ * @param total_bytes
+ *
+ * @return 0 no read will happen
+ * @return total_blocks
+ */
+int read_data_block_collection(unsigned char *buffer,
+							   char **list,
+							   unsigned int block_size,/* Block size in byte */
+							   unsigned int total_bytes)
+{
+	unsigned int total_blocks;
+	unsigned int i;
+
+	total_blocks = total_bytes/block_size;
+	if(total_bytes <= 0 ){
+		/* No read will happen */
+		return 0;
+	}
+
+	if(*list !=NULL){
+		OS_FREE(*list);
+	}
+
+	*list = OS_ALLOC((sizeof(char)*total_bytes));
+	for(i=0;i<total_blocks;i++){
+		OS_MEMCPY(*list+i, buffer, block_size);
+		buffer+=block_size;
+	}
+
+	return total_blocks;
+}
+
+/*!
+ *
+ * @param buffer
+ * @param edid
+ * @param timings
+ * @param count
+ * @param upscale
+ *
+ * @return 0 on success
+ * @return EDID_ERROR_PARSE on failure
+ */
+int edid_parse_cea (unsigned char *buffer,
+					edid_t        *edid,
+					pd_timing_t   *timings,
+					int           count,
+					unsigned char upscale)
+{
+	unsigned int offset,i = 4,timing_in_edid = 0, j;
+	unsigned int tag_code, chksum=0;
+	unsigned char *data_block_buffer;
+	pd_timing_t     *cea_timings = NULL;
+	pd_timing_t		*temp_timings;
+	unsigned short header;
+
+	if (!buffer) {
+		return EDID_ERROR_PARSE;
+	}
+	/* This holds the buffer */
+	offset = buffer[2];
+	/* Check the checksum */
+	for( i=0; i<128; i++) {
+		chksum += buffer[i];
+	}
+	if ((chksum & 0xff) != 0) {
+		EMGD_ERROR("EDID checksum is incorrect! Will ignore.");
+		return EDID_ERROR_PARSE;
+	}
+
+	header = buffer[0];
+
+	/* DI-Extension(header == 0x40) Unsupported for now */
+	if(header != 0x02){	/* CEA-Extension */
+		EMGD_ERROR("Unknown Extension");
+		return EDID_ERROR_PARSE;
+	}
+
+	if(edid->cea == NULL){
+		edid->cea = (cea_extension_t *) OS_ALLOC(sizeof(cea_extension_t));
+	}
+	if(edid->cea == NULL){
+		EMGD_ERROR("Run out of memory");
+		return 1;
+	}
+	OS_MEMSET(edid->cea, 0 , (sizeof(cea_extension_t)));
+	edid->cea->rev_number	= buffer [1];
+	edid->cea->caps			= buffer [3];
+
+	// no data block found or data block exceeds maximum buffer size
+	if(offset == 0 || offset > 124) {
+		EMGD_ERROR("No data block and no DTD ");
+		return 1;
+	}
+
+	/* General Format of CEA Data Block Collection
+	// -----------+--------------------+--------------------------------------+
+	//            |Byte#   |Bits5-7    |       Bits 0-4                       |
+	// -----------|--------------------+--------------------------------------+
+	//            |  1     | Video Tag |Length = total #of video			  |
+	//            |        |    Code   |bytes following this byte (L1)        |
+	//            |--------------------+--------------------------------------+
+	//  Video     |  2     | CEA Short Video Descriptor 1                     |
+	//  Data      |--------+--------------------------------------------------|
+	//  Block     |  3     | CEA Short Video Descriptor 2                     |
+	//            |--------+--------------------------------------------------|
+	//            | ...    | ...                                              |
+	//            |-----------------------------------------------------------+
+	//            | 1+L1   | CEA Short Video Descriptor L1                    |
+	// -----------+--------------------+--------------------------------------+
+	//            | 2+L1   | Audio Tag |Length = total #of audio bytes		  |
+	//            |        |    Code   |following this byte (L2)              |
+	//            |--------------------+--------------------------------------+
+	//  Audio     | 3+L1   |                                                  |
+	//  Data      |--------+                                                  |
+	//  Block     | 4+L1   | CEA Short Audio Descriptor 1                     |
+	//            |--------+                                                  |
+	//            | 5+L1   |                                                  |
+	//            |-----------------------------------------------------------+
+	//            | ...    |                                                  |
+	//            |        |                                                  |
+	//            |        |                                                  |
+	//            | ...    |                                                  |
+	//            |------------------------------------------------------------
+	//            |L1+L2   |                                                  |
+	//            |--------|                                                  |
+	//            |1+L1+L2 | CEA Short Audio Descriptor L2/3                  |
+	//            |--------|                                                  |
+	//            |2+L1+L2 |                                                  |
+	// -----------+-----------------------------------------------------------+
+	//            |3+L1+L2 |  Speaker  |Length = total #of SA bytes following |
+	//            |        | Tag Code  |this byte (L1)                        |
+	//  Speaker   |-----------------------------------------------------------+
+	//  Allocation|4+L1+L2 |                                                  |
+	//  Data      |--------|                                                  |
+	//  Block     |5+L1+L2 | Speaker Allocation Data Block Payload(3 bytes)   |
+	//            |--------|                                                  |
+	//            |6+L1+L2 |                                                  |
+	// -----------+-----------------------------------------------------------+
+	//            |7+L1+L2 | VSDB  Tag |Length = total #of VSDB bytes		  |
+	//            |        |    Code   |following this byte (L1)              |
+	//  Vendor    |-----------------------------------------------------------+
+	//  Specific  |8+L1+L2 |                                                  |
+	//  Data      |--------|                                                  |
+	//  Block     |9+L1+L2 | 24-bit IEEE Registration Identifier (LSB first)  |
+	//            |--------|                                                  |
+	//            |10+L1+L2|                                                  |
+	//            |-----------------------------------------------------------+
+	//            | ...    | Vendor Specific Data block Payload               |
+	// -----------+-----------------------------------------------------------+*/
+	data_block_buffer=&buffer[4];
+	/* Start reading data block collection */
+	i = 0;
+	while(i<offset){
+		unsigned int total_bytes;
+		tag_code = (unsigned int)*data_block_buffer & 0xE0;
+		total_bytes = (unsigned int)*data_block_buffer & 0x1F;
+		data_block_buffer++;
+		i++;
+		switch(tag_code){
+			case CEA_VIDEO_DATA_BLOCK:
+				/* Reading Short Video Descriptor block */
+				edid->cea->total_short_video_desc =read_data_block_collection
+				   (data_block_buffer,
+					(char **)&edid->cea->short_video_desc,
+					1,/* Block size in byte */
+					total_bytes);
+				break;
+			case CEA_AUDIO_DATA_BLOCK:
+				/* Reading Short Audio Descriptor block */
+				edid->cea->total_short_audio_desc = read_data_block_collection
+					(data_block_buffer,
+					(char **)&edid->cea->short_audio_desc,
+					3,/* Block size in byte */
+					total_bytes);
+				/* uncomment this codes if this info is requred in the future */
+				/*for(j=0; j<total_bytes; j+=3){
+					OS_MEMCPY
+						(&edid->cea->misc_data[j],
+						(edid->cea->short_audio_desc + (j/3)),
+						sizeof(cea_audio_format_t));
+				}
+				edid->cea->sadc = (unsigned char)
+					edid->cea->total_short_audio_desc; */
+				break;
+			case CEA_VENDOR_DATA_BLOCK:
+				/* Reading Vendor Specific Descriptor block */
+				edid->cea->vendor_block.vendor_block_size = (unsigned char)total_bytes;
+				OS_MEMCPY(edid->cea->vendor_block.vendor_ieee_id, data_block_buffer, 3);
+				read_data_block_collection
+					(data_block_buffer,
+					(char **)&edid->cea->vendor_data_block,
+					3,
+					total_bytes);
+				break;
+			case CEA_SPEAKER_DATA_BLOCK:
+				/* Reading Speaker Data Descriptor block */
+				for(j=0; j<3; j++){
+					edid->cea->speaker_alloc_block[j] = data_block_buffer[j];
+				}
+				break;
+			default:
+				/* Ignore unknown block? */
+				EMGD_ERROR("Unknown Tag Code! : CEA extended data block");
+				break;
+		}
+		data_block_buffer+=(total_bytes);
+		i+=(total_bytes);
+	};
+
+	/* DPG codes show that they are manufacturer who doesn't follow the spec
+	   Calculate the maximum possible DTD from whatever empty space left.
+	   The Maximum DTD is 6*/
+	 i=(unsigned int)(128-offset-1)/18;
+	 i = (i>6)?6:i;
+
+	buffer+=offset;
+	while(i>0){
+		if (edid->num_timings >= NUM_TIMINGS-1) {
+				break;
+		}
+		edid_mark_detailed_timings(buffer, edid, timings, upscale);
+		buffer+=18;
+		i--;
+	};
+
+	/* Parse audio block to know panel audio capability */
+	parse_audio_block(edid);
+
+	cea_timings = (igd_timing_info_t *) OS_ALLOC(cea_timing_table_size);
+	OS_MEMCPY(cea_timings, cea_timing_table, cea_timing_table_size);
+
+	for(i=0; i<(unsigned int)edid->cea->total_short_video_desc; i++){
+		EMGD_DEBUG("Video Code %d",(int)(edid->cea->short_video_desc + i)->code);
+		temp_timings = cea_timings;
+		timing_in_edid = 0;
+		while (temp_timings->width != IGD_TIMING_TABLE_END){
+			/* Look for matching tag code */
+			if((edid->cea->short_video_desc + i)->code == temp_timings->mode_number){
+				j=0;
+				while(j<edid->num_timings){
+					/* Ignore if we have already read the timng through DTD
+					   dclk would be sufficient to differentiate between progressive
+					   and interlace */
+					if(temp_timings->width == edid->timings[j].width &&
+					   temp_timings->height == edid->timings[j].height &&
+					   temp_timings->refresh == edid->timings[j].refresh &&
+					   temp_timings->dclk == edid->timings[j].dclk){
+						timing_in_edid = 1;
+						break;
+					}
+					j++;
+				}
+				/* Add the timing if we have not done so through DTD */
+				if(!(timing_in_edid)){
+					if (edid->num_timings >= NUM_TIMINGS-1) {
+							break;
+					}
+					OS_MEMCPY(&edid->timings[edid->num_timings],
+							  temp_timings, sizeof(pd_timing_t));
+					edid->timings[edid->num_timings].mode_number = 0;
+					edid->timings[edid->num_timings].mode_info_flags
+						|= (PD_MODE_SUPPORTED | PD_MODE_DTD);
+					edid->num_timings++;
+					edid->timings[edid->num_timings].width = IGD_TIMING_TABLE_END;
+					/*enable_scaled_timings(timings,
+						&edid->timings[edid->num_timings], upscale);*/
+				}
+				break;
+			}
+			temp_timings++;
+		}
+	}
+
+	OS_FREE(cea_timings);
+
+#ifdef DEBUG_FIRMWARE
+	EMGD_ERROR("FINAL supported timings after CEA .");
+	print_supported_timings(timings, edid);
+#endif
+	return 0;
+}
+#endif
+
+/*!
+ *
+ * @param buffer
+ * @param edid
+ * @param timings
+ * @param count
+ * @param upscale
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int edid_ext_parse(
+		unsigned char *buffer,
+		edid_t        *edid,
+		pd_timing_t   *timings,
+		int           count,
+		unsigned char upscale)
+{
+	unsigned int timing_in_edid = 0, j;
+#ifndef CONFIG_MICRO
+	if(edid_parse_cea(buffer, edid, timings, count, upscale)){
+		EMGD_ERROR("Reading cea extension ERROR!");
+		return 1;
+	}
+#endif
+
+	/* Check to see if 640x480 is part of EDID timing if not add it in.
+	   CEA panels does not support standard timing 640x480 */
+	for(j=0; j<edid->num_timings; j++){
+		if(edid->timings[j].width == 640 &&
+		   edid->timings[j].height == 480 &&
+		   edid->timings[j].refresh == 60 ){
+			timing_in_edid = 1;
+			break;
+		}
+	}
+	if((!timing_in_edid) && (edid->num_timings < NUM_TIMINGS-1)){
+		OS_MEMCPY(&edid->timings[edid->num_timings],
+							  cea_timing_table, sizeof(pd_timing_t));
+		edid->timings[edid->num_timings].mode_number = 0x101;
+		edid->timings[edid->num_timings].mode_info_flags
+			|= (PD_MODE_SUPPORTED | PD_MODE_DTD);
+		edid->num_timings++;
+		edid->timings[edid->num_timings].width = IGD_TIMING_TABLE_END;
+	}
+
+	return 0;
+}/* end edid_parse_ext() */
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/i2c_dispatch.h b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/i2c_dispatch.h
new file mode 100755
index 0000000..ae98630
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/i2c_dispatch.h
@@ -0,0 +1,78 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: i2c_dispatch.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _I2C_DISPATCH_H
+#define _I2C_DISPATCH_H
+
+/*
+ * IO.h is needed to resolve the FAR define
+ * context.h is needed for the igd_display_context_t
+ */
+
+#include <io.h>
+#include <context.h>
+#include <mode.h>
+
+#include <pd.h>
+
+#define I2C_DEFAULT_SPEED 100  /* Default I2C bus speed in KHz */
+#define DDC_DEFAULT_SPEED  10  /* Default DDC bus speed in KHz */
+
+/*
+ * Flags for Write Reg List
+ *
+ * Serial Write: Write a Reg Value, Data Value repeatedly within one
+ *  write cycle.
+ */
+#define IGD_I2C_SERIAL_WRITE 0x1
+#define IGD_I2C_WRITE_FW 0x2
+
+typedef struct _i2c_dispatch {
+	int (*i2c_read_regs)(
+		igd_context_t *context,
+		unsigned long i2c_bus,
+		unsigned long i2c_speed,
+		unsigned long dab,
+		unsigned char reg,
+		unsigned char FAR *buffer,
+		unsigned long num_bytes,
+		unsigned long flags);
+	int (*i2c_write_reg_list)(
+		igd_context_t *context,
+		unsigned long i2c_bus,
+		unsigned long i2c_speed,
+		unsigned long dab,
+		pd_reg_t *reg_list,
+		unsigned long flags);
+} i2c_dispatch_t;
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/igd_pi.c b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/igd_pi.c
new file mode 100644
index 0000000..1cf54ce
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/igd_pi.c
@@ -0,0 +1,260 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_pi.c
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Callback functions to give to port drivers. All functions are provided
+ *  to port driver as callback functions. Only port driver should call
+ *  these functions.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <context.h>
+
+#include <memory.h>
+#include <sched.h>
+
+/* #include <igd_pi.h> */
+#include <igd_debug.h>
+
+#include <pi.h>
+#include <pd.h>
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*!
+ * Function to register with main driver.
+ *
+ * @param handle
+ * @param pd_driver
+ *
+ * @return pi_pd_register()
+ */
+int igd_pd_register(void *handle, void *pd_driver)
+{
+	return pi_pd_register(pd_driver);
+} /* igd_pd_register */
+
+/*!
+ * Function to allocate memory
+ *
+ * @param handle
+ * @param pd_driver
+ *
+ * @return void
+ */
+void *igd_pd_malloc(unsigned long size)
+{
+	return OS_ALLOC(size);
+} /* end igd_pd_malloc */
+
+/*!
+ * Function to set the memory
+ *
+ * @param address
+ * @param c
+ * @param size
+ *
+ * @return void
+ */
+void *igd_pd_memset(void *address, int c, unsigned long size)
+{
+	return OS_MEMSET(address, c, size);
+} /* end igd_pd_memset */
+
+/*!
+ * Function to copy block of memory
+ *
+ * @param dst
+ * @param src
+ * @param size
+ *
+ * @return void
+ */
+void *igd_pd_memcpy(void *dst, void *src, unsigned long size)
+{
+	return OS_MEMCPY(dst, src, size);
+} /* end igd_pd_memcpy */
+
+/* Functions to free memory */
+void igd_pd_free(void *ptr)
+{
+	OS_FREE(ptr);
+} /* end igd_pd_free */
+
+/*!
+ * Function to sleep in micro seconds. This can be called with millisecond
+ * ranges.
+ *
+ * @param usec
+ *
+ * @return void
+ */
+void igd_pd_usleep(unsigned long usec)
+{
+	if (usec <= 1000) {
+		OS_SLEEP(usec);
+	} else {
+		os_alarm_t alarm = OS_SET_ALARM((usec+999)/1000);
+		do {
+			OS_SCHEDULE();
+		} while (!OS_TEST_ALARM(alarm));
+	}
+} /* end igd_pd_usleep() */
+
+/*!
+ * Function to do a string copy
+ *
+ * @param dest
+ * @param src
+ *
+ * @return dest
+ */
+char *igd_pd_strcpy(char *dest, char const *src)
+{
+	int i = 0;
+	/* This can be optimized by assigning 32 bit quantities instead
+	 * of 8 bit quantities. This requires knowing the length first then
+	 * move the quantities. For now, this is OK. */
+	while (src[i] != '\0') {
+		dest[i] = src[i];
+		i++;
+	}
+	dest[i] = '\0';
+	return (dest);
+} /* end igd_pd_strcpy() */
+
+/*!
+ * Function to check value of an attribute
+ *
+ * @param curr
+ * @param in
+ *
+ * @return PD_SUCCESS on success
+ * @return PD_ERR_NULL_PTR,PD_ERR_INVALID_ATTR, or PD_ERR_INCORR_ATTR_VALUE
+ * 	on failure
+ */
+int igd_pd_check_attr(pd_attr_t *curr, pd_attr_t *in)
+{
+	if (!curr || !in) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	if (curr->id != in->id) {
+		return PD_ERR_INVALID_ATTR;
+	}
+
+	switch (curr->type) {
+		case PD_ATTR_TYPE_RANGE:
+			if ((in->current_value < RATTR(curr)->min) ||
+				(in->current_value > RATTR(curr)->max)) {
+				return PD_ERR_INCORR_ATTR_VALUE;
+			}
+			break;
+		case PD_ATTR_TYPE_LIST:
+			if ((in->current_value < 1) ||
+				(in->current_value > LHATTR(curr)->num_entries)) {
+				return PD_ERR_INCORR_ATTR_VALUE;
+			}
+			break;
+		case PD_ATTR_TYPE_BOOL:
+			if ((in->current_value != TRUE) &&
+				(in->current_value != FALSE)) {
+				return PD_ERR_INCORR_ATTR_VALUE;
+			}
+			break;
+		default:
+			return PD_ERR_INVALID_ATTR;
+	}
+	return PD_SUCCESS;
+} /* end igd_pd_check_attr() */
+
+/*!
+ * This function searches for the requested attr_id in the attribute list
+ * and returns the pointer.
+ *
+ * In case of LIST attribute, it will return the proper list entry.
+ *
+ * @param attr_list
+ * @param num_attrs
+ * @param attr_id
+ * @param flag
+ *
+ * @return attr_list on success
+ * @return NULL on failure
+ */
+pd_attr_t *igd_pd_get_attr(pd_attr_t *attr_list, unsigned long num_attrs,
+		unsigned long attr_id, unsigned long flag)
+{
+	unsigned long i;
+
+	if (!attr_list) {
+		return NULL;
+	}
+
+	for (i = 0; i < num_attrs; i++) {
+		if (attr_list[i].id == attr_id) {
+			if (attr_list[i].type == PD_ATTR_TYPE_LIST) {
+				if (flag == PD_GET_ATTR_LIST_ENTRY) {
+					return (&(attr_list[i+attr_list[i].current_value]));
+				} else {
+					return (&(attr_list[i]));
+				}
+			}
+			return (&(attr_list[i]));
+		}
+	}
+	return NULL;
+} /* end igd_pd_get_attr() */
+
+/*!
+ * Common mode filter algorithm for all port drivers
+ *
+ * @param context
+ * @param ilist
+ * @param olist
+ * @param dvo
+ * @param display
+ *
+ * @return pd_filter_timings()
+ */
+int igd_pd_filter_timings(
+	void *context,
+	pd_timing_t *ilist,
+	pd_timing_t **olist,
+	pd_dvo_info_t *dvo,
+	pd_display_info_t *display)
+{
+	return pd_filter_timings(context, ilist, olist, dvo, display);
+}
+
+igd_debug_t *igd_pd_get_igd_debug( void )
+{
+	return emgd_debug;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/mode_table.c b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/mode_table.c
new file mode 100644
index 0000000..d698875
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/mode_table.c
@@ -0,0 +1,2545 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_table.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the mode resolution parameters.
+ *-----------------------------------------------------------------------------
+ */
+
+#define TRUE 1
+#define FALSE 0
+
+#include <config.h>
+#include <mode.h>
+
+/*
+ * If CONFIG_LIMIT_MODES is not defined then ALL modes should be included.
+ * Keep the defines here so that when adding new modes it is apparent that
+ * you should add it to the list below.
+ */
+#ifndef CONFIG_LIMIT_MODES
+#define CONFIG_MODE_640x480x60
+#define CONFIG_MODE_640x480x70
+#define CONFIG_MODE_640x480x72
+#define CONFIG_MODE_640x480x75
+#define CONFIG_MODE_640x480x85
+#define CONFIG_MODE_640x480x100
+#define CONFIG_MODE_640x480x120
+#define CONFIG_MODE_720x480x60
+#define CONFIG_MODE_720x576x50
+#define CONFIG_MODE_800x480x60
+#define CONFIG_MODE_800x600x60
+#define CONFIG_MODE_800x600x70
+#define CONFIG_MODE_800x600x72
+#define CONFIG_MODE_800x600x75
+#define CONFIG_MODE_800x600x85
+#define CONFIG_MODE_800x600x100
+#define CONFIG_MODE_800x600x120
+#define CONFIG_MODE_960x540x60
+#define CONFIG_MODE_1024x768x60
+#define CONFIG_MODE_1024x768x70
+#define CONFIG_MODE_1024x768x75
+#define CONFIG_MODE_1024x768x85
+#define CONFIG_MODE_1024x768x100
+#define CONFIG_MODE_1024x768x120
+#define CONFIG_MODE_1152x864x60
+#define CONFIG_MODE_1152x864x70
+#define CONFIG_MODE_1152x864x72
+#define CONFIG_MODE_1152x864x75
+#define CONFIG_MODE_1152x864x85
+#define CONFIG_MODE_1152x864x100
+#define CONFIG_MODE_1280x720x60
+#define CONFIG_MODE_1280x720x75
+#define CONFIG_MODE_1280x720x85
+#define CONFIG_MODE_1280x720x100
+#define CONFIG_MODE_1280x768x60
+#define CONFIG_MODE_1280x768x75
+#define CONFIG_MODE_1280x768x85
+#define CONFIG_MODE_1280x960x60
+#define CONFIG_MODE_1280x960x75
+#define CONFIG_MODE_1280x960x85
+#define CONFIG_MODE_1280x1024x60
+#define CONFIG_MODE_1280x1024x70
+#define CONFIG_MODE_1280x1024x72
+#define CONFIG_MODE_1280x1024x75
+#define CONFIG_MODE_1280x1024x85
+#define CONFIG_MODE_1280x1024x100
+#define CONFIG_MODE_1280x1024x120
+#define CONFIG_MODE_1366x768x60
+#define CONFIG_MODE_1400x1050x60
+#define CONFIG_MODE_1400x1050x75
+#define CONFIG_MODE_1400x1050x85
+#define CONFIG_MODE_1600x900x60
+#define CONFIG_MODE_1600x900x75
+#define CONFIG_MODE_1600x900x85
+#define CONFIG_MODE_1600x900x100
+#define CONFIG_MODE_1600x900x120
+#define CONFIG_MODE_1600x1200x60
+#define CONFIG_MODE_1600x1200x65
+#define CONFIG_MODE_1600x1200x70
+#define CONFIG_MODE_1600x1200x72
+#define CONFIG_MODE_1600x1200x75
+#define CONFIG_MODE_1600x1200x85
+#define CONFIG_MODE_1600x1200x100
+#define CONFIG_MODE_1600x1200x120
+#define CONFIG_MODE_1856x1392x60
+#define CONFIG_MODE_1856x1392x75
+#define CONFIG_MODE_1920x1080x50
+#define CONFIG_MODE_1920x1080x60
+#define CONFIG_MODE_1920x1080x75
+#define CONFIG_MODE_1920x1080x85
+#define CONFIG_MODE_1920x1080x100
+#define CONFIG_MODE_1920x1200x60
+#define CONFIG_MODE_1920x1200x75
+#define CONFIG_MODE_1920x1440x60
+#define CONFIG_MODE_1920x1440x75
+#define CONFIG_MODE_1920x1440x85
+#define CONFIG_MODE_2048x1536x60
+#define CONFIG_MODE_2048x1536x75
+/* CEA timings */
+#define CONFIG_CEA_MODE_640x480px60
+#ifndef CONFIG_MICRO
+/* These are timings that has duplicate except the difference in aspect
+   ratio. Since IEGD does not support diffrent aspect ratio removing
+   these CEA timings for now */
+/*
+#define CONFIG_CEA_MODE_720x480px60
+#define CONFIG_CEA_MODE_720x576px50*/
+#define CONFIG_CEA_MODE_720x480p_ax60
+#define CONFIG_CEA_MODE_720x576p_ax50
+#define CONFIG_CEA_MODE_1280x720p_ax50
+#define CONFIG_CEA_MODE_1280x720p_ax60
+#define CONFIG_CEA_MODE_1920x1080p_ax50
+#define CONFIG_CEA_MODE_1920x1080p_ax60
+#define CONFIG_CEA_MODE_1920x1080i_ax50
+#define CONFIG_CEA_MODE_1920x1080i_ax60
+#endif /* CONFIG_MICRO */
+
+#endif
+
+/*---------------------------------------------------------------------------
+ * Timing tables for CRT modes.
+ *---------------------------------------------------------------------------
+ */
+
+igd_timing_info_t crt_timing_table[] =
+{
+/* TODO: Add VESA standard REDUCED BLANKING (RB) timings to the table */
+#ifdef CONFIG_MODE_640x480x60
+	{
+		640, 480,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		647, 791,              /* hblank_start, hblank_end */
+		655, 751,              /* hsync_start, hsync_end */
+		524,                   /* vtotal */
+		487, 516,              /* vblank_start, vblank_end */
+		489, 491,              /* vsync_start, vsync_end */
+		0x101,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_640x480x70
+	{
+		640, 480,              /* width, height */
+		70, 28560,             /* refresh, dot clock */
+		815,                   /* htotal */
+		639, 815,              /* hblank_start, hblank_end */
+		663, 727,              /* hsync_start, hsync_end */
+		499,                   /* vtotal */
+		479, 499,              /* vblank_start, vblank_end */
+		480, 483,              /* vsync_start, vsync_end */
+		0x101,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_640x480x72
+	{
+		640, 480,              /* width, height */
+		72, 31500,             /* refresh, dot clock */
+		831,                   /* htotal */
+		647, 823,              /* hblank_start, hblank_end */
+		663, 703,              /* hsync_start, hsync_end */
+		519,                   /* vtotal */
+		487, 511,              /* vblank_start, vblank_end */
+		488, 491,              /* vsync_start, vsync_end */
+		0x101,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_640x480x75
+	{
+		640, 480,              /* width, height */
+		75, 31500,             /* refresh, dot clock */
+		839,                   /* htotal */
+		639, 839,              /* hblank_start, hblank_end */
+		655, 719,              /* hsync_start, hsync_end */
+		499,                   /* vtotal */
+		479, 499,              /* vblank_start, vblank_end */
+		480, 483,              /* vsync_start, vsync_end */
+		0x101,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_640x480x85
+	{
+		640, 480,              /* width, height */
+		85, 36000,             /* refresh, dot clock */
+		831,                   /* htotal */
+		639, 831,              /* hblank_start, hblank_end */
+		695, 751,              /* hsync_start, hsync_end */
+		508,                   /* vtotal */
+		479, 508,              /* vblank_start, vblank_end */
+		480, 483,              /* vsync_start, vsync_end */
+		0x101,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_640x480x100
+	{
+		640, 480,              /* width, height */
+		100, 43163,            /* refresh, dot clock */
+		847,                   /* htotal */
+		639, 847,              /* hblank_start, hblank_end */
+		679, 743,              /* hsync_start, hsync_end */
+		508,                   /* vtotal */
+		479, 508,              /* vblank_start, vblank_end */
+		480, 483,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_640x480x120
+	{
+		640, 480,              /* width, height */
+		120, 52406,            /* refresh, dot clock */
+		847,                   /* htotal */
+		639, 847,              /* hblank_start, hblank_end */
+		679, 743,              /* hsync_start, hsync_end */
+		514,                   /* vtotal */
+		479, 514,              /* vblank_start, vblank_end */
+		480, 483,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_720x400x70
+	/* NOTE: The above define is not auto-enabled. This is the VGA magic mode */
+	{   /* VGA Mode 2+,3+ */
+		720, 400,              /* width, height */
+		70, 28322,             /* refresh, dot clock */
+		899,                   /* htotal */
+		719, 899,              /* hblank_start, hblank_end */
+		773, 881,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		399, 448,              /* vblank_start, vblank_end */
+		412, 414,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_VSYNC_HIGH |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_720x480x60
+	{
+		720, 480,              /* width, height */
+		60, 27000,             /* refresh, dot clock */
+		857,                   /* htotal */
+		719, 857,              /* hblank_start, hblank_end */
+		735, 797,              /* hsync_start, hsync_end */
+		524,                   /* vtotal */
+		479, 524,              /* vblank_start, vblank_end */
+		488, 494,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_720x576x50
+	{
+		720, 576,              /* width, height */
+		50, 27500,             /* refresh, dot clock */
+		864,                   /* htotal */
+		719, 864,              /* hblank_start, hblank_end */
+		732, 795,              /* hsync_start, hsync_end */
+		625,                   /* vtotal */
+		575, 625,              /* vblank_start, vblank_end */
+		581, 587,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+	/* OFF BY Default, enable when PLB is merged */
+#ifdef CONFIG_MODE_800x480x60
+	{
+		800, 480,              /* width, height */
+		60, 33231,             /* refresh, dot clock */
+		1055,                  /* htotal */
+		799, 1055,             /* hblank_start, hblank_end */
+		863, 991,              /* hsync_start, hsync_end */
+		524,                   /* vtotal */
+		479, 524,              /* vblank_start, vblank_end */
+		500, 502,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x60
+	{
+		800, 600,              /* width, height */
+		60, 40000,             /* refresh, dot clock */
+		1055,                  /* htotal */
+		799, 1055,             /* hblank_start, hblank_end */
+		839, 967,              /* hsync_start, hsync_end */
+		627,                   /* vtotal */
+		599, 627,              /* vblank_start, vblank_end */
+		600, 604,              /* vsync_start, vsync_end */
+		0x103,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x70
+	{
+		800, 600,              /* width, height */
+		70, 45500,             /* refresh, dot clock */
+		1039,                  /* htotal */
+		799, 1039,             /* hblank_start, hblank_end */
+		839, 919,              /* hsync_start, hsync_end */
+		624,                   /* vtotal */
+		599, 624,              /* vblank_start, vblank_end */
+		600, 603,              /* vsync_start, vsync_end */
+		0x103,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x72
+	{
+		800, 600,              /* width, height */
+		72, 50000,             /* refresh, dot clock */
+		1039,                  /* htotal */
+		799, 1039,             /* hblank_start, hblank_end */
+		855, 975,              /* hsync_start, hsync_end */
+		665,                   /* vtotal */
+		599, 665,              /* vblank_start, vblank_end */
+		636, 642,              /* vsync_start, vsync_end */
+		0x103,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x75
+	{
+		800, 600,              /* width, height */
+		75, 49500,             /* refresh, dot clock */
+		1055,                  /* htotal */
+		799, 1055,             /* hblank_start, hblank_end */
+		815, 895,              /* hsync_start, hsync_end */
+		624,                   /* vtotal */
+		599, 624,              /* vblank_start, vblank_end */
+		600, 603,              /* vsync_start, vsync_end */
+		0x103,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x85
+	{
+		800, 600,              /* width, height */
+		85, 56250,             /* refresh, dot clock */
+		1047,                  /* htotal */
+		799, 1047,             /* hblank_start, hblank_end */
+		831, 895,              /* hsync_start, hsync_end */
+		630,                   /* vtotal */
+		599, 630,              /* vblank_start, vblank_end */
+		600, 603,              /* vsync_start, vsync_end */
+		0x103,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x100
+	{
+		800, 600,              /* width, height */
+		100, 68179,            /* refresh, dot clock */
+		1071,                  /* htotal */
+		799, 1071,             /* hblank_start, hblank_end */
+		847, 935,              /* hsync_start, hsync_end */
+		635,                   /* vtotal */
+		599, 635,              /* vblank_start, vblank_end */
+		600, 603,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_800x600x120
+	{
+		800, 600,              /* width, height */
+		120, 83950,            /* refresh, dot clock */
+		1087,                  /* htotal */
+		799, 1087,             /* hblank_start, hblank_end */
+		855, 943,              /* hsync_start, hsync_end */
+		642,                   /* vtotal */
+		599, 642,              /* vblank_start, vblank_end */
+		600, 603,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_960x540x60
+	{
+		960, 540,              /* width, height */
+		60, 40785,             /* refresh, dot clock */
+		1215,                  /* htotal */
+		959, 1215,             /* hblank_start, hblank_end */
+		991, 1087,             /* hsync_start, hsync_end */
+		558,                   /* vtotal */
+		539, 558,              /* vblank_start, vblank_end */
+		540, 543,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1024x768x60
+	{
+		1024, 768,             /* width, height */
+		60, 65000,             /* refresh, dot clock */
+		1343,                  /* htotal */
+		1023, 1343,            /* hblank_start, hblank_end */
+		1047, 1183,            /* hsync_start, hsync_end */
+		805,                   /* vtotal */
+		767, 805,              /* vblank_start, vblank_end */
+		770, 776,              /* vsync_start, vsync_end */
+		0x105,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1024x768x70
+	{
+		1024, 768,             /* width, height */
+		70, 75000,             /* refresh, dot clock */
+		1327,                  /* htotal */
+		1023, 1327,            /* hblank_start, hblank_end */
+		1047, 1183,            /* hsync_start, hsync_end */
+		805,                   /* vtotal */
+		767, 805,              /* vblank_start, vblank_end */
+		770, 776,              /* vsync_start, vsync_end */
+		0x105,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1024x768x75
+	{
+		1024, 768,             /* width, height */
+		75, 78750,             /* refresh, dot clock */
+		1311,                  /* htotal */
+		1023, 1311,            /* hblank_start, hblank_end */
+		1039, 1135,            /* hsync_start, hsync_end */
+		799,                   /* vtotal */
+		767, 799,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x105,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1024x768x85
+	{
+		1024, 768,             /* width, height */
+		85, 94500,             /* refresh, dot clock */
+		1375,                  /* htotal */
+		1023, 1375,            /* hblank_start, hblank_end */
+		1071, 1167,            /* hsync_start, hsync_end */
+		807,                   /* vtotal */
+		767, 807,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x105,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1024x768x100
+	{
+		1024, 768,             /* width, height */
+		100, 113310,           /* refresh, dot clock */
+		1391,                  /* htotal */
+		1023, 1391,            /* hblank_start, hblank_end */
+		1095, 1207,            /* hsync_start, hsync_end */
+		813,                   /* vtotal */
+		767, 813,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1024x768x120
+	{
+		1024, 768,             /* width, height */
+		120, 139050,           /* refresh, dot clock */
+		1407,                  /* htotal */
+		1023, 1407,            /* hblank_start, hblank_end */
+		1103, 1215,            /* hsync_start, hsync_end */
+		822,                   /* vtotal */
+		767, 822,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity H+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1152x864x60
+	{
+		1152, 864,             /* width, height */
+		60, 81624,             /* refresh, dot clock */
+		1519,                  /* htotal */
+		1151, 1519,            /* hblank_start, hblank_end */
+		1215, 1335,            /* hsync_start, hsync_end */
+		894,                   /* vtotal */
+		863, 894,              /* vblank_start, vblank_end */
+		864, 867,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1152x864x70
+	{
+		1152, 864,             /* width, height */
+		70, 97000,             /* refresh, dot clock */
+		1535,                  /* htotal */
+		1151, 1535,            /* hblank_start, hblank_end */
+		1223, 1343,            /* hsync_start, hsync_end */
+		899,                   /* vtotal */
+		863, 899,              /* vblank_start, vblank_end */
+		864, 867,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1152x864x72
+	{
+		1152, 864,             /* width, height */
+		72, 100000,            /* refresh, dot clock */
+		1535,                  /* htotal */
+		1151, 1535,            /* hblank_start, hblank_end */
+		1223, 1343,            /* hsync_start, hsync_end */
+		900,                   /* vtotal */
+		863, 900,              /* vblank_start, vblank_end */
+		864, 867,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1152x864x75
+	{
+		1152, 864,             /* width, height */
+		75, 108000,            /* refresh, dot clock */
+		1599,                  /* htotal */
+		1151, 1599,            /* hblank_start, hblank_end */
+		1215, 1343,            /* hsync_start, hsync_end */
+		899,                   /* vtotal */
+		863, 899,              /* vblank_start, vblank_end */
+		864, 867,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1152x864x85
+	{
+		1152, 864,             /* width, height */
+		85, 120000,            /* refresh, dot clock */
+		1551,                  /* htotal */
+		1151, 1551,            /* hblank_start, hblank_end */
+		1223, 1351,            /* hsync_start, hsync_end */
+		906,                   /* vtotal */
+		863, 906,              /* vblank_start, vblank_end */
+		864, 867,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1152x864x100
+	{
+		1152, 864,             /* width, height */
+		100, 144000,           /* refresh, dot clock */
+		1567,                  /* htotal */
+		1151, 1567,            /* hblank_start, hblank_end */
+		1231, 1359,            /* hsync_start, hsync_end */
+		914,                   /* vtotal */
+		863, 914,              /* vblank_start, vblank_end */
+		864, 867,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x720x60
+	{
+		1280, 720,             /* width, height */
+		60, 74481,             /* refresh, dot clock */
+		1663,                  /* htotal */
+		1279, 1663,            /* hblank_start, hblank_end */
+		1335, 1471,            /* hsync_start, hsync_end */
+		745,                   /* vtotal */
+		719, 745,              /* vblank_start, vblank_end */
+		720, 723,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x720x75
+	{
+		1280, 720,             /* width, height */
+		75, 96000,             /* refresh, dot clock */
+		1695,                  /* htotal */
+		1279, 1695,            /* hblank_start, hblank_end */
+		1351, 1487,            /* hsync_start, hsync_end */
+		751,                   /* vtotal */
+		719, 751,              /* vblank_start, vblank_end */
+		720, 723,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x720x85
+	{
+		1280, 720,             /* width, height */
+		85, 110000,            /* refresh, dot clock */
+		1711,                  /* htotal */
+		1279, 1711,            /* hblank_start, hblank_end */
+		1359, 1495,            /* hsync_start, hsync_end */
+		755,                   /* vtotal */
+		719, 755,              /* vblank_start, vblank_end */
+		720, 723,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x720x100
+	{
+		1280, 720,             /* width, height */
+		100, 131850,           /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1367, 1503,            /* hsync_start, hsync_end */
+		762,                   /* vtotal */
+		719, 762,              /* vblank_start, vblank_end */
+		720, 723,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x768x60
+	{
+		1280, 768,             /* width, height */
+		60, 80136,             /* refresh, dot clock */
+		1679,                  /* htotal */
+		1279, 1679,            /* hblank_start, hblank_end */
+		1343, 1479,            /* hsync_start, hsync_end */
+		794,                   /* vtotal */
+		767, 794,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x768x75
+	{
+		1280, 768,             /* width, height */
+		75, 102977,            /* refresh, dot clock */
+		1711,                  /* htotal */
+		1279, 1711,            /* hblank_start, hblank_end */
+		1359, 1495,            /* hsync_start, hsync_end */
+		801,                   /* vtotal */
+		767, 801,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x768x85
+	{
+		1280, 768,             /* width, height */
+		85, 118532,            /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1367, 1503,            /* hsync_start, hsync_end */
+		806,                   /* vtotal */
+		767, 806,              /* vblank_start, vblank_end */
+		768, 771,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x960x60
+	{
+		1280, 960,             /* width, height */
+		60, 108000,            /* refresh, dot clock */
+		1799,                  /* htotal */
+		1279, 1799,            /* hblank_start, hblank_end */
+		1375, 1487,            /* hsync_start, hsync_end */
+		999,                   /* vtotal */
+		959, 999,              /* vblank_start, vblank_end */
+		960, 963,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x960x75
+	{
+		1280, 960,             /* width, height */
+		75, 129859,            /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1367, 1503,            /* hsync_start, hsync_end */
+		1001,                  /* vtotal */
+		959, 1001,             /* vblank_start, vblank_end */
+		960, 963,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x960x85
+	{
+		1280, 960,             /* width, height */
+		85, 148500,            /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1343, 1503,            /* hsync_start, hsync_end */
+		1010,                  /* vtotal */
+		959, 1010,             /* vblank_start, vblank_end */
+		960, 963,              /* vsync_start, vsync_end */
+		0x00,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x60
+	{
+		1280, 1024,            /* width, height */
+		60, 108000,            /* refresh, dot clock */
+		1687,                  /* htotal */
+		1279, 1687,            /* hblank_start, hblank_end */
+		1327, 1439,            /* hsync_start, hsync_end */
+		1065,                  /* vtotal */
+		1023, 1065,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x107,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x70
+	{
+		1280, 1024,            /* width, height */
+		70, 129000,            /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1367, 1503,            /* hsync_start, hsync_end */
+		1065,                  /* vtotal */
+		1023, 1065,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x107,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x72
+	{
+		1280, 1024,            /* width, height */
+		72, 133000,            /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1367, 1503,            /* hsync_start, hsync_end */
+		1066,                  /* vtotal */
+		1023, 1066,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x107,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x75
+	{
+		1280, 1024,            /* width, height */
+		75, 135000,            /* refresh, dot clock */
+		1687,                  /* htotal */
+		1279, 1687,            /* hblank_start, hblank_end */
+		1295, 1439,            /* hsync_start, hsync_end */
+		1065,                  /* vtotal */
+		1023, 1065,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x107,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x85
+	{
+		1280, 1024,            /* width, height */
+		85, 157500,            /* refresh, dot clock */
+		1727,                  /* htotal */
+		1279, 1727,            /* hblank_start, hblank_end */
+		1343, 1503,            /* hsync_start, hsync_end */
+		1071,                  /* vtotal */
+		1023, 1071,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x107,                 /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x100
+	{
+		1280, 1024,            /* width, height */
+		100, 190960,           /* refresh, dot clock */
+		1759,                  /* htotal */
+		1279, 1759,            /* hblank_start, hblank_end */
+		1375, 1519,            /* hsync_start, hsync_end */
+		1084,                  /* vtotal */
+		1023, 1084,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1280x1024x120
+	{
+		1280, 1024,            /* width, height */
+		120, 233790,           /* refresh, dot clock */
+		1775,                  /* htotal */
+		1279, 1775,            /* hblank_start, hblank_end */
+		1383, 1527,            /* hsync_start, hsync_end */
+		1096,                  /* vtotal */
+		1023, 1096,            /* vblank_start, vblank_end */
+		1024, 1027,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1366x768x60
+	{
+		1366, 768,             /* width, height */
+		60, 74057,             /* refresh, dot clock */
+		1663,                  /* htotal */
+		1365, 1663,            /* hblank_start, hblank_end */
+		1429, 1557,            /* hsync_start, hsync_end */
+		775,                   /* vtotal */
+		767, 775,              /* vblank_start, vblank_end */
+		768, 769,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH |       /* polarity V+ */
+		IGD_HSYNC_HIGH,        /* polarity H+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+
+#ifdef CONFIG_MODE_1400x1050x60
+	{
+		1400, 1050,            /* width, height */
+		60, 122000,            /* refresh, dot clock */
+		1879,                  /* htotal */
+		1399, 1879,            /* hblank_start, hblank_end */
+		1487, 1639,            /* hsync_start, hsync_end */
+		1086,                  /* vtotal */
+		1049, 1086,            /* vblank_start, vblank_end */
+		1050, 1053,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1400x1050x75
+	{
+		1400, 1050,            /* width, height */
+		75, 155851,            /* refresh, dot clock */
+		1895,                  /* htotal */
+		1399, 1895,            /* hblank_start, hblank_end */
+		1495, 1647,            /* hsync_start, hsync_end */
+		1095,                  /* vtotal */
+		1049, 1095,            /* vblank_start, vblank_end */
+		1050, 1053,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1400x1050x85
+	{
+		1400, 1050,            /* width, height */
+		85, 179260,            /* refresh, dot clock */
+		1911,                  /* htotal */
+		1399, 1911,            /* hblank_start, hblank_end */
+		1503, 1655,            /* hsync_start, hsync_end */
+		1102,                  /* vtotal */
+		1049, 1102,            /* vblank_start, vblank_end */
+		1050, 1053,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x900x60
+	{
+		1600, 900,             /* width, height */
+		60, 119000,            /* refresh, dot clock */
+		2127,                  /* htotal */
+		1599, 2127,            /* hblank_start, hblank_end */
+		1695, 1863,            /* hsync_start, hsync_end */
+		931,                   /* vtotal */
+		899, 931,              /* vblank_start, vblank_end */
+		900, 903,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x900x75
+	{
+		1600, 900,             /* width, height */
+		75, 152000,            /* refresh, dot clock */
+		2159,                  /* htotal */
+		1599, 2159,            /* hblank_start, hblank_end */
+		1703, 1879,            /* hsync_start, hsync_end */
+		939,                   /* vtotal */
+		899, 939,              /* vblank_start, vblank_end */
+		900, 903,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x900x85
+	{
+		1600, 900,             /* width, height */
+		85, 175000,            /* refresh, dot clock */
+		2175,                  /* htotal */
+		1599, 2175,            /* hblank_start, hblank_end */
+		1711, 1887,            /* hsync_start, hsync_end */
+		944,                   /* vtotal */
+		899, 944,              /* vblank_start, vblank_end */
+		900, 903,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x900x100
+	{
+		1600, 900,             /* width, height */
+		100, 208900,           /* refresh, dot clock */
+		2191,                  /* htotal */
+		1599, 2191,            /* hblank_start, hblank_end */
+		1719, 1895,            /* hsync_start, hsync_end */
+		952,                   /* vtotal */
+		899, 952,              /* vblank_start, vblank_end */
+		900, 903,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x900x120
+	{
+		1600, 900,             /* width, height */
+		120, 255686,           /* refresh, dot clock */
+		2207,                  /* htotal */
+		1599, 2207,            /* hblank_start, hblank_end */
+		1727, 1903,            /* hsync_start, hsync_end */
+		964,                   /* vtotal */
+		899, 964,              /* vblank_start, vblank_end */
+		900, 903,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x60
+	{
+		1600, 1200,            /* width, height */
+		60, 162000,            /* refresh, dot clock */
+		2159,                  /* htotal */
+		1599, 2159,            /* hblank_start, hblank_end */
+		1663, 1855,            /* hsync_start, hsync_end */
+		1249,                  /* vtotal */
+		1199, 1249,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x65
+	{
+		1600, 1200,            /* width, height */
+		65, 175500,            /* refresh, dot clock */
+		2159,                  /* htotal */
+		1599, 2159,            /* hblank_start, hblank_end */
+		1663, 1855,            /* hsync_start, hsync_end */
+		1249,                  /* vtotal */
+		1199, 1249,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x70
+	{
+		1600, 1200,            /* width, height */
+		70, 189000,            /* refresh, dot clock */
+		2159,                  /* htotal */
+		1599, 2159,            /* hblank_start, hblank_end */
+		1663, 1855,            /* hsync_start, hsync_end */
+		1249,                  /* vtotal */
+		1199, 1249,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x72
+	{
+		1600, 1200,            /* width, height */
+		72, 195000,            /* refresh, dot clock */
+		2175,                  /* htotal */
+		1599, 2175,            /* hblank_start, hblank_end */
+		1711, 1887,            /* hsync_start, hsync_end */
+		1250,                  /* vtotal */
+		1199, 1250,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x75
+	{
+		1600, 1200,            /* width, height */
+		75, 202500,            /* refresh, dot clock */
+		2159,                  /* htotal */
+		1599, 2159,            /* hblank_start, hblank_end */
+		1663, 1855,            /* hsync_start, hsync_end */
+		1249,                  /* vtotal */
+		1199, 1249,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x85
+	{
+		1600, 1200,            /* width, height */
+		85, 229500,            /* refresh, dot clock */
+		2159,                  /* htotal */
+		1599, 2159,            /* hblank_start, hblank_end */
+		1663, 1855,            /* hsync_start, hsync_end */
+		1249,                  /* vtotal */
+		1199, 1249,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_HSYNC_HIGH|        /* polarity H+ */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x100
+	{
+		1600, 1200,            /* width, height */
+		100, 280640,           /* refresh, dot clock */
+		2207,                  /* htotal */
+		1599, 2207,            /* hblank_start, hblank_end */
+		1727, 1903,            /* hsync_start, hsync_end */
+		1270,                  /* vtotal */
+		1199, 1270,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1600x1200x120
+	{
+		1600, 1200,            /* width, height */
+		120, 343210,           /* refresh, dot clock */
+		2223,                  /* htotal */
+		1599, 2223,            /* hblank_start, hblank_end */
+		1735, 1911,            /* hsync_start, hsync_end */
+		1285,                  /* vtotal */
+		1199, 1285,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1856x1392x60
+	{
+		1856, 1392,            /* width, height */
+		60, 218250,            /* refresh, dot clock */
+		2527,                  /* htotal */
+		1855, 2527,            /* hblank_start, hblank_end */
+		1951, 2175,            /* hsync_start, hsync_end */
+		1438,                  /* vtotal */
+		1391, 1438,            /* vblank_start, vblank_end */
+		1392, 1395,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1856x1392x75
+	{
+		1856, 1392,            /* width, height */
+		75, 288000,            /* refresh, dot clock */
+		2559,                  /* htotal */
+		1855, 2559,            /* hblank_start, hblank_end */
+		1983, 2207,            /* hsync_start, hsync_end */
+		1499,                  /* vtotal */
+		1391, 1499,            /* vblank_start, vblank_end */
+		1392, 1395,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1080x50
+	{
+		1920, 1080,            /* width, height */
+		50, 141446,            /* refresh, dot clock */
+		2543,                  /* htotal */
+		1919, 2543,            /* hblank_start, hblank_end */
+		2031, 2231,            /* hsync_start, hsync_end */
+		1111,                  /* vtotal */
+		1079, 1111,            /* vblank_start, vblank_end */
+		1080, 1083,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1080x60
+	{
+		1920, 1080,            /* width, height */
+		60, 172800,            /* refresh, dot clock */
+		2575,                  /* htotal */
+		1919, 2575,            /* hblank_start, hblank_end */
+		2039, 2247,            /* hsync_start, hsync_end */
+		1117,                  /* vtotal */
+		1079, 1117,            /* vblank_start, vblank_end */
+		1080, 1083,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1080x75
+	{
+		1920, 1080,            /* width, height */
+		75, 220640,            /* refresh, dot clock */
+		2607,                  /* htotal */
+		1919, 2607,            /* hblank_start, hblank_end */
+		2055, 2263,            /* hsync_start, hsync_end */
+		1127,                  /* vtotal */
+		1079, 1127,            /* vblank_start, vblank_end */
+		1080, 1083,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1080x85
+	{
+		1920, 1080,            /* width, height */
+		85, 252930,            /* refresh, dot clock */
+		2623,                  /* htotal */
+		1919, 2623,            /* hblank_start, hblank_end */
+		2063, 2271,            /* hsync_start, hsync_end */
+		1133,                  /* vtotal */
+		1079, 1133,            /* vblank_start, vblank_end */
+		1080, 1083,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1080x100
+	{
+		1920, 1080,            /* width, height */
+		100, 302020,           /* refresh, dot clock */
+		2639,                  /* htotal */
+		1919, 2639,            /* hblank_start, hblank_end */
+		2071, 2279,            /* hsync_start, hsync_end */
+		1143,                  /* vtotal */
+		1079, 1143,            /* vblank_start, vblank_end */
+		1080, 1083,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1200x60
+	{
+		1920, 1200,            /* width, height */
+		60, 193156,            /* refresh, dot clock */
+		2591,                  /* htotal */
+		1919, 2591,            /* hblank_start, hblank_end */
+		2047, 2255,            /* hsync_start, hsync_end */
+		1241,                  /* vtotal */
+		1199, 1241,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1200x75
+	{
+		1920, 1200,            /* width, height */
+		75, 246590,            /* refresh, dot clock */
+		2623,                  /* htotal */
+		1919, 2623,            /* hblank_start, hblank_end */
+		2063, 2271,            /* hsync_start, hsync_end */
+		1252,                  /* vtotal */
+		1199, 1252,            /* vblank_start, vblank_end */
+		1200, 1203,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1440x60
+	{
+		1920, 1440,            /* width, height */
+		60, 234000,            /* refresh, dot clock */
+		2599,                  /* htotal */
+		1919, 2599,            /* hblank_start, hblank_end */
+		2047, 2255,            /* hsync_start, hsync_end */
+		1499,                  /* vtotal */
+		1439, 1499,            /* vblank_start, vblank_end */
+		1440, 1443,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1440x75
+	{
+		1920, 1440,            /* width, height */
+		75, 297000,            /* refresh, dot clock */
+		2639,                  /* htotal */
+		1919, 2639,            /* hblank_start, hblank_end */
+		2063, 2287,            /* hsync_start, hsync_end */
+		1499,                  /* vtotal */
+		1439, 1499,            /* vblank_start, vblank_end */
+		1440, 1443,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_1920x1440x85
+	{
+		1920, 1440,            /* width, height */
+		85, 341350,            /* refresh, dot clock */
+		2655,                  /* htotal */
+		1919, 2655,            /* hblank_start, hblank_end */
+		2071, 2287,            /* hsync_start, hsync_end */
+		1511,                  /* vtotal */
+		1439, 1511,            /* vblank_start, vblank_end */
+		1440, 1443,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_2048x1536x60
+	{
+		2048, 1536,            /* width, height */
+		60, 266950,            /* refresh, dot clock */
+		2799,                  /* htotal */
+		2047, 2799,            /* hblank_start, hblank_end */
+		2199, 2423,            /* hsync_start, hsync_end */
+		1588,                  /* vtotal */
+		1535, 1588,            /* vblank_start, vblank_end */
+		1536, 1539,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_MODE_2048x1536x75
+	{
+		2048, 1536,            /* width, height */
+		75, 340480,            /* refresh, dot clock */
+		2831,                  /* htotal */
+		2047, 2831,            /* hblank_start, hblank_end */
+		2215, 2439,            /* hsync_start, hsync_end */
+		1602,                  /* vtotal */
+		1535, 1602,            /* vblank_start, vblank_end */
+		1536, 1539,            /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE|  /* mode info flags */
+		IGD_VSYNC_HIGH,        /* polarity V+ */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+	PD_TIMING_TABLE_END
+};
+
+int crt_timing_table_size = sizeof(crt_timing_table);
+
+/*---------------------------------------------------------------------------
+ * Timing tables for VGA modes
+ *---------------------------------------------------------------------------
+ */
+igd_timing_info_t vga_timing_table[] =
+{
+	{   /* VGA Mode 0 */
+		320, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x00,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_LINE_DOUBLE |
+		IGD_PIXEL_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 1 */
+		320, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x01,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_LINE_DOUBLE |
+		IGD_PIXEL_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 2 */
+		640, 200,              /* width, height */
+		70, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		679, 775,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x02,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_LINE_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 3 */
+		640, 200,              /* width, height */
+		70, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		679, 775,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x03,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_LINE_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 4 */
+		320, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x04,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_LINE_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 5 */
+		320, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x05,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_LINE_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 6 */
+		640, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 784,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x06,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_LINE_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 7 */
+		720, 350,              /* width, height */
+		60, 28322,             /* refresh, dot clock */
+		899,                   /* htotal */
+		719, 881,              /* hblank_start, hblank_end */
+		764, 872,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x07,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode d */
+		320, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x0d,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_LINE_DOUBLE |
+		IGD_PIXEL_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode e */
+		640, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x0e,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_LINE_DOUBLE |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode f */
+		640, 350,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x11,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 10 */
+		640, 350,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x12,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 0* */
+		320, 350,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x13,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_PIXEL_DOUBLE |
+		IGD_MODE_TEXT |
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 1* */
+		320, 350,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		399,                   /* htotal */
+		319, 383,              /* hblank_start, hblank_end */
+		343, 383,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x14,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_PIXEL_DOUBLE |
+		IGD_MODE_TEXT |
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 2* */
+		640, 350,              /* width, height */
+		70, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		679, 775,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x15,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 3* */
+		640, 350,              /* width, height */
+		70, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		679, 775,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		354, 441,              /* vblank_start, vblank_end */
+		386, 388,              /* vsync_start, vsync_end */
+		0x16,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_HSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 0+,1+ */
+		360, 400,              /* width, height */
+		60, 28322,             /* refresh, dot clock */
+		449,                   /* htotal */
+		359, 431,              /* hblank_start, hblank_end */
+		386, 431,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x17,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_PIXEL_DOUBLE |
+		IGD_MODE_TEXT |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 2+,3+ */
+		720, 400,              /* width, height */
+		70, 28322,             /* refresh, dot clock */
+		899,                   /* htotal */
+		719, 881,              /* hblank_start, hblank_end */
+		764, 872,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x18,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 7+ */
+		720, 400,              /* width, height */
+		60, 28322,             /* refresh, dot clock */
+		899,                   /* htotal */
+		719, 881,              /* hblank_start, hblank_end */
+		764, 872,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x19,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_TEXT |
+		IGD_VSYNC_HIGH |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 11 */
+		640, 480,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		524,                   /* vtotal */
+		486, 515,              /* vblank_start, vblank_end */
+		489, 491,              /* vsync_start, vsync_end */
+		0x1A,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 12 */
+		640, 480,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		524,                   /* vtotal */
+		486, 515,              /* vblank_start, vblank_end */
+		489, 491,              /* vsync_start, vsync_end */
+		0x1B,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+	{   /* VGA Mode 13 */
+		320, 200,              /* width, height */
+		60, 25175,             /* refresh, dot clock */
+		799,                   /* htotal */
+		639, 783,              /* hblank_start, hblank_end */
+		671, 767,              /* hsync_start, hsync_end */
+		448,                   /* vtotal */
+		405, 440,              /* vblank_start, vblank_end */
+		411, 413,              /* vsync_start, vsync_end */
+		0x1C,                  /* mode number */
+		IGD_MODE_VESA |        /* VESA/VGA mode */
+		IGD_LINE_DOUBLE |
+		IGD_PIXEL_DOUBLE |
+		IGD_MODE_SUPPORTED |
+		IGD_SCAN_PROGRESSIVE,  /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+
+	PD_TIMING_TABLE_END
+};
+
+/* CEA standard timings: Get them from CEA-861. This is following DPG.
+   We will replace the mode number to CEA*/
+igd_timing_info_t cea_timing_table[] =
+{
+#ifdef CONFIG_CEA_MODE_640x480px60
+	{
+		640, 480,              /* width, height */
+		60, 25200,             /* refresh, dot clock */
+		800,                   /* htotal */
+		640, 799,              /* hblank_start, hblank_end */
+		656, 751,              /* hsync_start, hsync_end */
+		525,                   /* vtotal */
+		480, 524,              /* vblank_start, vblank_end */
+		490, 491,              /* vsync_start, vsync_end */
+		1,					   /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE |
+		PD_MODE_CEA,		   /* mode info flags */
+		0, 0,                  /* x, y offset */
+		0,				   /* device dependant */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_720x480px60
+	{
+		720, 480,              /* width, height */
+		60, 27000,             /* refresh, dot clock */
+		858,                   /* htotal */
+		720, 857,              /* hblank_start, hblank_end */
+		736, 797,              /* hsync_start, hsync_end */
+		525,                   /* vtotal */
+		480, 524,              /* vblank_start, vblank_end */
+		489, 494,              /* vsync_start, vsync_end */
+		2,					   /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE |
+		PD_MODE_CEA,		   /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_720x480p_ax60
+	{
+		720, 480,              /* width, height */
+		60, 27000,             /* refresh, dot clock */
+		858,                   /* htotal */
+		720, 857,              /* hblank_start, hblank_end */
+		736, 797,              /* hsync_start, hsync_end */
+		525,                   /* vtotal */
+		480, 524,              /* vblank_start, vblank_end */
+		489, 494,              /* vsync_start, vsync_end */
+		3,                     /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE |
+		PD_MODE_CEA |          /* mode info flags */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_720x576px50
+	{
+		720, 576,              /* width, height */
+		50, 27000,             /* refresh, dot clock */
+		864,                   /* htotal */
+		720, 863,              /* hblank_start, hblank_end */
+		732, 795,              /* hsync_start, hsync_end */
+		625,                   /* vtotal */
+		575, 624,              /* vblank_start, vblank_end */
+		581, 585,              /* vsync_start, vsync_end */
+		17,                    /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE |
+		PD_MODE_CEA,		   /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_720x576p_ax50
+	{
+		720, 576,              /* width, height */
+		50, 27000,             /* refresh, dot clock */
+		864,                   /* htotal */
+		720, 863,              /* hblank_start, hblank_end */
+		732, 795,              /* hsync_start, hsync_end */
+		625,                   /* vtotal */
+		575, 624,              /* vblank_start, vblank_end */
+		581, 585,              /* vsync_start, vsync_end */
+		18,                    /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE |
+		PD_MODE_CEA |		   /* mode info flags */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_1280x720p_ax50
+	{
+		1280, 720,             /* width, height */
+		50, 74250,             /* refresh, dot clock */
+		1980,                  /* htotal */
+		1280, 1979,            /* hblank_start, hblank_end */
+		1720, 1759,            /* hsync_start, hsync_end */
+		750,                   /* vtotal */
+		720, 749,              /* vblank_start, vblank_end */
+		725, 729,              /* vsync_start, vsync_end */
+		19,                    /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE |
+		PD_MODE_CEA |		   /* mode info flags */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_1280x720p_ax60
+	{
+		1280, 720,             /* width, height */
+		60, 74250,             /* refresh, dot clock */
+		1650,                  /* htotal */
+		1280, 1649,            /* hblank_start, hblank_end */
+		1390, 1429,            /* hsync_start, hsync_end */
+		750,                   /* vtotal */
+		720, 749,              /* vblank_start, vblank_end */
+		725, 729,              /* vsync_start, vsync_end */
+		4,                     /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE | /* mode info flags */
+		IGD_VSYNC_HIGH |
+		PD_MODE_CEA |		   /* polarity V+ */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+/* KIV. Not proper timimgs, rerefer to DPG updates */
+#ifdef CONFIG_CEA_MODE_1920x1080p_ax50
+	{
+		1920, 1080,            /* width, height */
+		50, 148500,            /* refresh, dot clock */
+		2640,                  /* htotal */
+		1920, 2639,            /* hblank_start, hblank_end */
+		2448, 2491,            /* hsync_start, hsync_end */
+		1125,                  /* vtotal */
+		1080, 1124,            /* vblank_start, vblank_end */
+		1084, 1088,            /* vsync_start, vsync_end */
+		31,                    /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE | /* mode info flags */
+		IGD_VSYNC_HIGH |
+		PD_MODE_CEA |		   /* polarity V+ */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_1920x1080p_ax60
+	{
+		1920, 1080,            /* width, height */
+		60, 148500,            /* refresh, dot clock */
+		2200,                  /* htotal */
+		1920, 2199,            /* hblank_start, hblank_end */
+		2008, 2051,            /* hsync_start, hsync_end */
+		1125,                  /* vtotal */
+		1080, 1124,            /* vblank_start, vblank_end */
+		1084, 1088,            /* vsync_start, vsync_end */
+		16,                     /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_PROGRESSIVE | /* mode info flags */
+		IGD_VSYNC_HIGH |
+		PD_MODE_CEA |	       /* polarity V+ */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_1920x1080i_ax50
+	{
+		1920, 1080,            /* width, height */
+		25, 74250,             /* refresh, dot clock */
+		2640,                  /* htotal */
+		1920, 2639,            /* hblank_start, hblank_end */
+		2448, 2491,            /* hsync_start, hsync_end */
+		1124,                  /* vtotal */
+		1080, 1123,            /* vblank_start, vblank_end */
+		1084, 1093,            /* vsync_start, vsync_end */
+		20,					   /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_INTERLACE |   /* mode info flags */
+		IGD_VSYNC_HIGH |
+		PD_MODE_CEA |		   /* polarity V+ */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+#ifdef CONFIG_CEA_MODE_1920x1080i_ax60
+	{
+		1920, 1080,            /* width, height */
+		30, 74250,             /* refresh, dot clock */
+		2200,                  /* htotal */
+		1920, 2199,            /* hblank_start, hblank_end */
+		2008, 2051,            /* hsync_start, hsync_end */
+		1124,                  /* vtotal */
+		1080, 1123,            /* vblank_start, vblank_end */
+		1084, 1093,            /* vsync_start, vsync_end */
+		5,					   /* mode number */
+		PD_MODE_SUPPORTED |    /* enable the mode */
+		IGD_SCAN_INTERLACE |   /* mode info flags */
+		IGD_VSYNC_HIGH |
+		PD_MODE_CEA |		   /* polarity V+ */
+		PD_ASPECT_16_9,        /* Aspect ratio 16:9 */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extension pointer */
+		0,				   /* device dependant */
+		0				   /* device dependant */
+	},
+#endif
+	PD_TIMING_TABLE_END
+};
+
+#ifndef CONFIG_MICRO
+int cea_timing_table_size = sizeof(cea_timing_table);
+
+/* CEA timings for parsing purpose */
+type_std_t cea_std_lookup[] =
+{
+	{  640,    400,   60,    0 },
+	{  720,    480,   60,    0 },
+	{  720,    480,   60,    PD_ASPECT_16_9 },
+	{ 1280,    720,   60,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   60,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{  720,    480,   60,    PD_SCAN_INTERLACE },
+	{  720,    480,   60,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{  720,    240,   60,    0 },
+
+	{  720,    240,   60,    PD_ASPECT_16_9 },
+	{ 2880,    480,   60,    PD_SCAN_INTERLACE },
+	{ 2880,    480,   60,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{ 2880,    240,   60,    0 },
+	{ 2880,    240,   60,    PD_ASPECT_16_9 },
+	{ 1440,    480,   60,    0 },
+	{ 1440,    480,   60,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   60,    PD_ASPECT_16_9 },
+
+	{  720,    576,   50,    0 },
+	{  720,    576,   50,    PD_ASPECT_16_9 },
+	{ 1280,    720,   50,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   50,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{  720,    576,   50,    PD_SCAN_INTERLACE },
+	{  720,    576,   50,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{  720,    288,   50,    0 },
+	{  720,    288,   50,    PD_ASPECT_16_9 },
+
+	{ 2880,    576,   50,    PD_SCAN_INTERLACE },
+	{ 2880,    576,   50,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{ 2880,    288,   50,    0 },
+	{ 2880,    288,   50,    PD_ASPECT_16_9 },
+	{ 1440,    576,   50,    0 },
+	{ 1440,    576,   50,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   50,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   24,    PD_ASPECT_16_9 },
+
+	{ 1920,   1080,   25,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   30,    PD_ASPECT_16_9 },
+	{ 2880,    480,   60,    0 },
+	{ 2880,    480,   60,    PD_ASPECT_16_9 },
+	{ 2880,    576,   50,    0 },
+	{ 2880,    576,   50,    PD_ASPECT_16_9 },
+	{ 1920,   1080,   50,    PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{ 1920,   1080,   100,   PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+
+	{ 1280,    720,   100,   PD_ASPECT_16_9 },
+	{  720,    576,   100,   0 },
+	{  720,    576,   100,   PD_ASPECT_16_9 },
+	{  720,    576,   100,   PD_SCAN_INTERLACE },
+	{  720,    576,   100,   PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{ 1920,   1080,   120,   PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{ 1280,    720,   120,   PD_ASPECT_16_9 },
+	{  720,    480,   120,   0 },
+
+	{  720,    480,   120,   PD_ASPECT_16_9 },
+	{  720,    480,   120,   PD_SCAN_INTERLACE },
+	{  720,    480,   120,   PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{  720,    576,   200,   0 },
+	{  720,    576,   200,   PD_ASPECT_16_9 },
+	{  720,    576,   200,   PD_SCAN_INTERLACE },
+	{  720,    576,   200,   PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{  720,    480,   240,   0 },
+
+	{  720,    480,   240,   PD_ASPECT_16_9 },
+	{  720,    480,   240,   PD_SCAN_INTERLACE },
+	{  720,    480,   240,   PD_SCAN_INTERLACE | PD_ASPECT_16_9 },
+	{ 1280,    720,   24,    PD_ASPECT_16_9 },
+	{ 1280,    720,   25,    PD_ASPECT_16_9 },
+	{ 1280,    720,   30,    PD_ASPECT_16_9 },
+};
+
+int cea_std_lookup_size = sizeof(cea_std_lookup)/sizeof(type_std_t);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/pd_init_all.c b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/pd_init_all.c
new file mode 100644
index 0000000..ba70fc8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/pd_init_all.c
@@ -0,0 +1,215 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pd_init_all.c
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file controls the port drivers that are initialized as part of
+ *  the HAL init. These must be statically linked with the HAL.
+ *  NOTE: This file is visible to customers. It is used in source form as
+ *  part of the vBIOS build procedure.
+ *-----------------------------------------------------------------------------
+ */
+
+#include "config.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/* Enable Analog (CRT) port driver */
+#ifdef CONFIG_LINK_PD_ANALOG
+extern int analog_init(void *handle);
+#define ANALOG_INIT(handle)  analog_init(handle)
+#else
+#define ANALOG_INIT(handle)  0
+#endif
+
+/* Enable Silicon Image 154/164 FP port driver */
+#ifdef CONFIG_LINK_PD_SII164
+extern int sii164_init(void *handle);
+#define SII164_INIT(handle)  sii164_init(handle)
+#else
+#define SII164_INIT(handle)  0
+#endif
+
+/* Enable Texas Instruments 410 FP port driver */
+#ifdef CONFIG_LINK_PD_TI410
+extern int ti410_init(void *handle);
+#define TI410_INIT(handle)  ti410_init(handle)
+#else
+#define TI410_INIT(handle)  0
+#endif
+
+/* Enable Chrontel 7009 TMDS & TVOut port driver */
+#ifdef CONFIG_LINK_PD_CH7009
+extern int ch7009_init(void *handle);
+#define CH7009_INIT(handle)  ch7009_init(handle)
+#else
+#define CH7009_INIT(handle)  0
+#endif
+
+/* Enable National Semiconductor 2501 LVDS port driver */
+#ifdef CONFIG_LINK_PD_NS2501
+extern int ns2501_init(void *handle);
+#define NS2501_INIT(handle)  ns2501_init(handle)
+#else
+#define NS2501_INIT(handle)  0
+#endif
+
+/* Enable TL955 port driver */
+#ifdef CONFIG_LINK_PD_TL955
+extern int tl955_init(void *handle);
+#define TL955_INIT(handle)  tl955_init(handle)
+#else
+#define TL955_INIT(handle)  0
+#endif
+
+/* Enable Th164 port driver */
+#ifdef CONFIG_LINK_PD_TH164
+extern int th164_init(void *handle);
+#define TH164_INIT(handle)  th164_init(handle)
+#else
+#define TH164_INIT(handle)  0
+#endif
+
+/* Enable FS454 port driver */
+#ifdef CONFIG_LINK_PD_FS454
+extern int fs454_init(void *handle);
+#define FS454_INIT(handle)  fs454_init(handle)
+#else
+#define FS454_INIT(handle)  0
+#endif
+
+/* Enable NS387/389 port driver */
+#ifdef CONFIG_LINK_PD_NS387
+extern int ns387_init(void *handle);
+#define NS387_INIT(handle)  ns387_init(handle)
+#else
+#define NS387_INIT(handle)  0
+#endif
+
+/* Enable connexant port driver */
+#ifdef CONFIG_LINK_PD_CX873
+extern int cx873_init(void *handle);
+#define CX873_INIT(handle)  cx873_init(handle)
+#else
+#define CX873_INIT(handle)  0
+#endif
+
+/* Enable Internal LVDS port driver */
+#ifdef CONFIG_LINK_PD_LVDS
+extern int lvds_init(void *handle);
+#define LVDS_INIT(handle)  lvds_init(handle)
+#else
+#define LVDS_INIT(handle)  0
+#endif
+
+/* Enable SDVO port driver */
+#ifdef CONFIG_LINK_PD_SDVO
+extern int sdvo_init(void *handle);
+#define SDVO_INIT(handle)  sdvo_init(handle)
+#else
+#define SDVO_INIT(handle)  0
+#endif
+
+/* Enable Integrated TV port driver for NAPA */
+#ifdef CONFIG_LINK_PD_TV
+extern int tv_init(void *handle);
+#define TV_INIT(handle)  tv_init(handle)
+#else
+#define TV_INIT(handle)  0
+#endif
+
+/* Enable FS460 port driver */
+#ifdef CONFIG_LINK_PD_FS460
+extern int fs460_init(void *handle);
+#define FS460_INIT(handle)  fs460_init(handle)
+#else
+#define FS460_INIT(handle)  0
+#endif
+
+/* Enable FS450 port driver */
+#ifdef CONFIG_LINK_PD_FS450
+extern int fs450_init(void *handle);
+#define FS450_INIT(handle)  fs450_init(handle)
+#else
+#define FS450_INIT(handle)  0
+#endif
+
+/* Enable Chrontel 7017 LVDS & TVOut port driver */
+#ifdef CONFIG_LINK_PD_CH7017
+extern int ch7017_init(void *handle);
+#define CH7017_INIT(handle)  ch7017_init(handle)
+#else
+#define CH7017_INIT(handle)  0
+#endif
+
+/* Enable internal HDMI port driver */
+#ifdef CONFIG_LINK_PD_HDMI
+extern int hdmi_init(void *handle);
+#define HDMI_INIT(handle)  hdmi_init(handle)
+#else
+#define HDMI_INIT(handle)  0
+#endif
+
+/* Enable Chrontel CH7036  port driver */
+#ifdef CONFIG_LINK_PD_CH7036
+extern int ch7036_init(void *handle);
+#define CH7036_INIT(handle)  ch7036_init(handle)
+#else
+#define CH7036_INIT(handle)  0
+#endif
+
+
+/*!
+ *
+ * @param handle
+ *
+ * @return 0
+ */
+int pi_init_all(void *handle)
+{
+	int ret;
+
+	ret = ANALOG_INIT(handle);
+	ret = CH7009_INIT(handle);
+	ret = SII164_INIT(handle);
+	ret = NS2501_INIT(handle);
+	ret = TH164_INIT(handle);
+	ret = NS387_INIT(handle);
+	ret = FS454_INIT(handle);
+	ret = CH7017_INIT(handle);
+	ret = SDVO_INIT(handle);
+	ret = TI410_INIT(handle);
+	ret = TV_INIT(handle);
+	ret = HDMI_INIT(handle);
+	ret = CH7036_INIT(handle);
+	ret = LVDS_INIT(handle); /*LVDS need to be initiate only after CH7036.*/
+	/* ret = PD000_INIT(handle); */
+
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/cmn/pi.c b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/pi.c
new file mode 100755
index 0000000..8eca6c7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/cmn/pi.c
@@ -0,0 +1,1919 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pi.c
+ * $Revision: 1.25 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains all the necessary functions for port interface
+ *  module. This module abstracts all hardware port interfaces and
+ *  manages them.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <config.h>
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+#include <igd_pwr.h>
+
+#include <io.h>
+#include <pci.h>
+#include <sched.h>
+#include <memory.h>
+
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+#include <dsp.h>
+#include <debug.h>
+#include <pi.h>
+#include <pd.h>
+#include <pd_init.h>
+#include <intelpci.h>
+#include <dispatch.h>
+#include <mode_access.h>
+#include <edid.h>
+#include <displayid.h>
+#include <emgd_drv.h>
+
+#include "i2c_dispatch.h"
+#include <igd_vga.h>
+#include <context.h>
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+typedef struct _pi_context {
+	igd_context_t *igd_context;
+	i2c_dispatch_t *i2c_dispatch;
+	unsigned long num_pi_drivers;
+} pi_context_t;
+
+/* Function to filter the modes using EDID or DisplayID*/
+int get_firmware_timings(igd_display_port_t *port,
+	unsigned char  *firmware_data, pd_timing_t *timing_table);
+
+int pi_pd_init(igd_display_port_t *port, unsigned long port_feature,
+	unsigned long second_port_feature, int drm_load_time);
+#ifndef CONFIG_MICRO
+unsigned long get_magic_cookie(pd_driver_t *pd_driver);
+#endif
+void assign_dynamic_numbers(igd_timing_info_t *timing_table);
+int update_attrs(igd_display_port_t *port);
+pd_timing_t *get_user_timings(igd_param_dtd_list_t *in_list);
+
+
+extern int pi_init_all(void *handle);
+
+extern emgd_drm_config_t config_drm;
+extern i2c_dispatch_t i2c_dispatch_plb;
+extern i2c_dispatch_t i2c_dispatch_tnc;
+
+int null_func( void )
+{
+	return -IGD_ERROR_NODEV;
+}
+/* Not currently used
+static i2c_dispatch_t i2c_dispatch_null = {
+	(void *)null_func,
+	(void *)null_func,
+	(void *)null_func,
+	(void *)null_func
+}; */
+
+
+static dispatch_table_t i2c_dispatch_list[] = {
+
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &i2c_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &i2c_dispatch_tnc},
+#endif
+
+	{0, NULL}
+};
+
+static unsigned char firmware_data[256];
+
+static pi_context_t pi_context[1];
+
+/*----------------------------------------------------------------------
+ *                        FUNCTION DEFINITIONS
+ *----------------------------------------------------------------------*/
+#ifndef CONFIG_MICRO
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void pi_shutdown(igd_context_t *context)
+{
+	igd_display_port_t *port;
+
+	EMGD_TRACE_ENTER;
+
+	if (pi_context->igd_context == NULL) {
+		return;
+	}
+
+	/* Close the port drivers */
+	port = NULL;
+	while ((port = context->mod_dispatch.dsp_get_next_port(context, port, 0)) != NULL) {
+		if (port->pd_driver) {
+			port->pd_driver->pd_close(port->pd_context);
+			port->pd_driver = NULL;
+			/* pd_context is freed by port driver */
+			port->pd_context = NULL;
+			/* timing_table is freed by port driver */
+			port->timing_table = NULL;
+			port->num_timing = 0;
+			if (port->fp_info) {
+				OS_FREE(port->fp_info);
+				port->fp_info = NULL;
+			}
+			if (port->callback) {
+				OS_FREE(port->callback);
+				port->callback = NULL;
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return 0
+ */
+int pi_full_init(igd_context_t *context)
+{
+	/* Optional Inter-module interfaces */
+	context->mod_dispatch.pi_shutdown = pi_shutdown;
+	return 0;
+}
+
+#endif
+
+/*!
+ *
+ * @param context
+ * @param config_info
+ *
+ * @return 0
+ */
+static int pi_get_config_info(igd_context_t *context,
+	igd_config_info_t *config_info)
+{
+
+	igd_display_port_t   *port = NULL;
+	igd_param_t          *init_params = NULL;
+	igd_display_params_t *display_params = NULL;
+	int                   i;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_ASSERT(context, "Null context", -IGD_ERROR_INVAL);
+	EMGD_ASSERT(config_info, "Null config_info", -IGD_ERROR_INVAL);
+
+	config_info->num_act_dsp_ports = pi_context->num_pi_drivers;
+
+	init_params = pi_context->igd_context->mod_dispatch.init_params;
+
+	while ((port = pi_context->igd_context->mod_dispatch.
+		dsp_get_next_port(pi_context->igd_context, port, 0)) != NULL) {
+
+		/* Get the display params to check if the user enabled EDID */
+		for (i = 0; i < IGD_MAX_PORTS; i++) {
+			if (init_params->display_params[i].port_number == port->port_number) {
+				display_params = &init_params->display_params[i];
+				break;
+			}
+		}
+
+		/* If DID rotation info is available, pass it to user-space through the
+		 * config_info struct */
+		if ((!display_params || (display_params->flags & IGD_DISPLAY_READ_EDID))
+			&& (port->firmware_type == PI_FIRMWARE_DISPLAYID
+			&&	port->displayid != NULL)) {
+
+			config_info->displayid_rotation[port->port_number - 1].rotation =
+				port->displayid->rotation_info.rotation;
+			config_info->displayid_rotation[port->port_number - 1].flip =
+				port->displayid->rotation_info.flip;
+
+		} else {
+			config_info->displayid_rotation[port->port_number - 1].rotation = 0;
+			config_info->displayid_rotation[port->port_number - 1].flip = 0;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return 0
+ */
+int pi_init(igd_context_t *context)
+{
+	i2c_dispatch_t *i2c_dispatch;
+
+	EMGD_TRACE_ENTER;
+
+	OS_MEMSET(pi_context, 0, sizeof(pi_context_t));
+
+	/* Save igd_context in local_igd_context. */
+	pi_context->igd_context = context;
+
+	/* Get I2C dispatch table */
+	i2c_dispatch = (i2c_dispatch_t *)dispatch_acquire(context,
+		i2c_dispatch_list);
+	if(!i2c_dispatch) {
+		EMGD_DEBUG("No i2c Dispatch available for PI module");
+	}
+	pi_context->i2c_dispatch = i2c_dispatch;
+
+	/*
+	 * If Dynamic Port drivers are not used then init the static drivers
+	 * now.
+	 */
+#ifndef IGD_DPD_ENABLED
+	{
+		void *handle = NULL;
+		int ret;
+		ret = pi_init_all(handle);
+	}
+#endif
+
+	/* Inter-module dispatch functions */
+	context->mod_dispatch.i2c_read_regs = i2c_dispatch->i2c_read_regs;
+	context->mod_dispatch.i2c_write_reg_list =
+		i2c_dispatch->i2c_write_reg_list;
+	context->mod_dispatch.pi_get_config_info = pi_get_config_info;
+
+	OPT_MICRO_CALL(pi_full_init(context));
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * Get a port with the requested feature set from the list. Don't allocate
+ * it just return it.  Only consider ports that aren't already in use.
+ *
+ * @param feature
+ * @param last
+ *
+ * @return port on success
+ * @return NULL on failure
+ */
+igd_display_port_t *pi_get_feature_port(unsigned long feature,
+	igd_display_port_t *last)
+{
+	igd_display_port_t *port;
+	inter_module_dispatch_t *md = &pi_context->igd_context->mod_dispatch;
+
+	while ((port = md->dsp_get_next_port(pi_context->igd_context, last, 0))) {
+		if (!port->inuse) {
+			if (feature) {
+				if (port->port_features & feature) {
+					return port;
+				}
+			} else {
+				return port;
+			}
+		}
+		last = port;
+	}
+
+	return NULL;
+}
+
+/*!
+ * Function to register port driver with display driver
+ *
+ * @param pd_driver
+ *
+ * @return PD_SUCCESS on success
+ * @return PD_ERR_NULL_PTR, PD_ERR_VER_MISMATCH, PD_ERR_DISPLAY_TYPE,
+ * 	PD_ERR_NOMEM on failure
+ */
+int pi_pd_register(pd_driver_t *pd_driver)
+{
+	igd_display_port_t *port;
+	igd_param_t *init_params;
+	unsigned long      prev_dab = 0, prev_i2c_speed = 0;
+	unsigned long      port_type, port_feature, second_port_feature = 0;
+	unsigned long      cookie_sent, cookie_rcvd;
+	unsigned long      num_instances = 0;
+	unsigned long      prev_instance_dab = 0, prev_instance_i2c_reg = 0;
+	unsigned long      dab_index = 0;
+	int                ret = PD_SUCCESS;
+
+	EMGD_TRACE_ENTER;
+
+	if (!pd_driver) {
+		EMGD_ERROR_EXIT("Null pd_driver received.");
+		return PD_ERR_NULL_PTR;
+	}
+
+	/* Check the PD SDK version (interface version between main and
+	 * port drivers Rightnow this check is useful only for XFree86.
+	 * XP and CE already done this checking. */
+	if (pd_driver->pd_sdk_version != PD_SDK_VERSION) {
+		EMGD_ERROR("PD SDK version mismatch between main driver"
+			"and %s. %u.%u != %u.%u", pd_driver->name,
+			(unsigned short) PD_SDK_VERSION>>8,
+			(unsigned short) PD_SDK_VERSION & 0xFF,
+			(unsigned short) pd_driver->pd_sdk_version>>8,
+			(unsigned short) pd_driver->pd_sdk_version & 0xFF);
+		return PD_ERR_VER_MISMATCH;
+	}
+
+	/* Do magic cookie hand shaking */
+#ifndef CONFIG_MICRO
+	cookie_sent = get_magic_cookie(pd_driver);
+#else
+	cookie_sent = 0;
+#endif
+	cookie_rcvd = pd_driver->validate(cookie_sent);
+	if (cookie_sent != cookie_rcvd) {
+		/* TODO: Do this check once we comeup with handshake algorithm. */
+		/*
+		EMGD_ERROR("Error, magic cookie handshaking failed.");
+		return PD_ERR_HAND_SHAKE;
+		*/
+	}
+
+	init_params = pi_context->igd_context->mod_dispatch.init_params;
+
+	port_feature = 0;
+	/* Get the port features based on the display types */
+	if (pd_driver->type == PD_DISPLAY_CRT) {
+		/* Allocate GMCH onboard CRT port */
+		port_type = IGD_PORT_ANALOG;
+	} else if (pd_driver->type == PD_DISPLAY_LVDS_INT) {
+		/* Allocate GMCH onboard LVDS port */
+		port_type = IGD_PORT_LVDS;
+	} else if (pd_driver->type == PD_DISPLAY_TVOUT_INT) {
+		/* Allocate GMCH onboard TV port */
+		port_type = IGD_PORT_TV;
+	} else if (pd_driver->type &
+			(PD_DISPLAY_TVOUT |    PD_DISPLAY_FP |      PD_DISPLAY_CRT_EXT |
+			 PD_DISPLAY_LVDS_EXT | PD_DISPLAY_HDMI_EXT| PD_DISPLAY_HDMI_INT|
+			 PD_DISPLAY_DRGB)) {
+
+		/* Allocate DVO port which is the only kind of port exported to
+		 * 3rd party encoders */
+		port_type = IGD_PORT_DIGITAL;
+
+		if (pd_driver->flags & PD_FLAG_GANG_MODE) {
+			igd_display_port_t *portb;
+			unsigned long user_gang = 0;
+			/* Get DVO Port B */
+			pi_context->igd_context->mod_dispatch.dsp_get_display(2,
+				NULL, &portb, 0);
+			if(portb) {
+				if (portb->attr_list && portb->attr_list->num_attrs != 0) {
+					unsigned long i;
+					for (i = 0; i < portb->attr_list->num_attrs; i++) {
+						if (portb->attr_list->attr[i].id==PD_ATTR_ID_GANG_MODE){
+							user_gang = portb->attr_list->attr[i].value;
+						}
+					}
+				}
+			}
+			/* If both user attribute and port driver flag are set to GANG MODE,
+			 * then allocate a gang display port */
+			if (user_gang) {
+				port_feature = IGD_PORT_GANG;
+				second_port_feature = IGD_PORT_GANG;
+			}
+		}
+	} else if (pd_driver->type == PD_DISPLAY_RGBA) {
+		port_type = IGD_PORT_DIGITAL;
+		port_feature = IGD_RGBA_COLOR;
+		second_port_feature = IGD_RGBA_ALPHA;
+	} else {
+		EMGD_ERROR_EXIT("Invalid display type.");
+		return PD_ERR_DISPLAY_TYPE;
+	}
+
+	/* Get the port entry */
+	port = NULL;
+	while((port = pi_get_feature_port(port_feature, port))) {
+		dab_index = 0;
+
+
+		/* This port already has a port driver,
+		 * don't search device on this port. */
+		if (port->pd_driver || (port->port_type != port_type)) {
+			continue;
+		}
+
+		/* allocate memory for callback */
+		port->callback = (pd_callback_t *)OS_ALLOC(sizeof(pd_callback_t));
+		if (port->callback == NULL) {
+			EMGD_ERROR_EXIT("Unable to alloc memory for callback context.");
+			return PD_ERR_NOMEM;
+		}
+		/* Fill entries in pd_callback_t */
+		port->callback->callback_context = port;
+		port->callback->read_regs = pi_read_regs;
+		port->callback->write_regs = pi_write_regs;
+		port->callback->eld = NULL; /* Insert when edid is initialize */
+
+		/*	SDVO port driver needs the port number */
+		port->callback->port_num = port->port_number;
+
+		/* now save the pd_driver in port entry */
+		port->pd_driver = pd_driver;
+
+		/* preference is to user specified i2c_speed */
+		prev_dab = port->dab;
+		prev_i2c_speed = port->i2c_speed;
+		if (!port->i2c_speed) {
+			port->i2c_speed = pd_driver->i2c_speed?pd_driver->i2c_speed:
+				I2C_DEFAULT_SPEED;
+		}
+
+		/* Try detecting the encoder by calling port driver open() */
+		if (port->dab ||
+			((port->dab == 0) && (pd_driver->dab_list[0] == PD_DAB_LIST_END))) {
+
+			/* Workaround for not to detect 2 encoders if only 1 encoder
+			 * is present and both DVOB and DVOC are using same I2C bus */
+			if ((init_params->display_flags & IGD_DISPLAY_MULTI_DVO) &&
+				(num_instances > 0) &&
+				(prev_instance_dab == port->dab) &&
+				(prev_instance_i2c_reg == port->i2c_reg)) {
+				/* Print this msg, because user explicitly mentioned DAB/I2C
+				 * bus details which are same as previous encoder's DAB/I2C bus
+				 * details. */
+				EMGD_DEBUG("1+ encoders have same I2C bus and DAB");
+				ret = -1;
+			} else {
+				/* Call open() only once if either user provides a DAB
+				 *                      or
+				 * no required to open an encoder. ex: analog, rgba, lvds etc.
+				 */
+				EMGD_DEBUG("Looking for \"%s\" on port 0x%lx with DAB 0x%lx",
+					pd_driver->name, port->port_reg, port->dab);
+				ret = pd_driver->open(port->callback, &(port->pd_context));
+			}
+		} else {
+
+			/* Call open() for each DAB */
+			while (pd_driver->dab_list[dab_index] != PD_DAB_LIST_END) {
+				if(pd_driver->type == PD_DISPLAY_LVDS_INT) {
+                	port->ddc_dab = pd_driver->dab_list[dab_index];
+					printk ("NUHAIRI: port->ddc_dab\n" );
+                } else {
+					port->dab = pd_driver->dab_list[dab_index];
+                }
+
+				/* Workaround for not to detect 2 encoders if only 1 encoder
+				 * is present and both DVOB and DVOC are using same I2C bus */
+				if ((init_params->display_flags & IGD_DISPLAY_MULTI_DVO) &&
+					(num_instances > 0) &&
+					(prev_instance_dab == port->dab) &&
+					(prev_instance_i2c_reg == port->i2c_reg)) {
+					/* Don't print the debug msg, because this is a valid case.
+					 * Example,
+					 *    Algorithm is detecting for multiple encoders with
+					 *    same DAB and same I2C bus on different ports.
+					 * If this case arises, simply continue
+					 */
+					/* EMGD_DEBUG("1+ encoders have same I2C bus and DAB"); */
+					if (pd_driver->flags & PD_FLAG_DUAL_DVO) {
+						/* If this flag is set, that means port driver is
+						 * explicityly requesting to be loaded on
+						 * both DVO B & DVO C with same DAB. Example: CH7017.
+						 *
+						 * In this case open the port driver again. */
+					} else {
+						ret = -1;
+						port->pd_context = NULL;
+						dab_index++;
+						continue;
+					}
+				}
+
+				EMGD_DEBUG("Looking for \"%s\" on port 0x%lx with DAB 0x%lx",
+					pd_driver->name, port->port_reg, port->dab);
+				ret = pd_driver->open(port->callback, &(port->pd_context));
+				if (ret == 0) {
+					break;
+				} else {
+
+					dab_index++;
+				}
+			}
+		}
+#ifndef CONFIG_MICRO
+		if(pi_context->igd_context->mod_dispatch.check_port_supported && ret == 0){
+			ret = pi_context->igd_context->mod_dispatch.check_port_supported(port);
+		}
+#endif
+		if (ret == 0) {
+
+			/* Initialize our port entry */
+			ret = pi_pd_init(port, port_feature, second_port_feature, TRUE);
+			if (ret) {
+				port->pd_driver = NULL;
+				port->pd_context = NULL;
+				port->dab = prev_dab;
+				port->i2c_speed = prev_i2c_speed;
+				port->mult_port = NULL;
+				port->timing_table = NULL;
+				port->num_timing = 0;
+				if (port->callback) {
+					OS_FREE(port->callback);
+					port->callback = NULL;
+				}
+			} else {
+				EMGD_DEBUG("Device found on %s port for \"%s\"", port->port_name,
+					pd_driver->name);
+				num_instances++;
+				prev_instance_dab = port->dab;
+				prev_instance_i2c_reg = port->i2c_reg;
+			}
+
+			/* If Multi-DVO support is enabled then detect next encoder of
+			 * same kind */
+			if (init_params->display_flags & IGD_DISPLAY_MULTI_DVO){
+				/* Continue to find next encoder */
+				continue;
+			} else {
+				/* Found one encoder and return to port driver */
+				break;
+			}
+		} else {
+			port->pd_driver = NULL;
+			port->pd_context = NULL;
+			port->dab = prev_dab;
+			port->i2c_speed = prev_i2c_speed;;
+			if (port->callback) {
+				OS_FREE(port->callback);
+				port->callback = NULL;
+			}
+		}
+	} /* end while(port == feature_port()) */
+
+	if (num_instances == 0) {
+		EMGD_DEBUG("No device found for \"%s\"", pd_driver->name);
+		return PD_ERR_NOPORT_AVAIL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return PD_SUCCESS;
+} /* end pi_pd_register() */
+
+/* Function to replace common timings in 1st list with 2nd list, 2nd list
+ * is unchanged. */
+void replace_common_dtds(igd_timing_info_t *dtds1,
+	igd_timing_info_t *dtds2)
+{
+	igd_timing_info_t *temp;
+	int index;
+
+	if (!dtds2 || !dtds1) {
+		return;
+	}
+
+	while (dtds1->width != IGD_TIMING_TABLE_END) {
+		temp = dtds2;
+		index = 0;
+
+		while (temp->width != IGD_TIMING_TABLE_END && index < NUM_TIMINGS) {
+			/* Replace modes that have common width, height and
+			   refresh rate. Removed Dot clock comparison since
+			   EDID(CEA modes) may differ in dot clock value.
+			   causing duplicate mode.
+			   All ial would do a match  mode by height, width
+			   and refresh rate.
+			*/
+			if ((temp->width   == dtds1->width) &&
+				(temp->height  == dtds1->height) &&
+				(temp->refresh == dtds1->refresh) &&
+				((temp->mode_info_flags & PD_SCAN_INTERLACE) ==
+				 (dtds1->mode_info_flags & PD_SCAN_INTERLACE))) {
+				dtds1->mode_info_flags &= ~PD_MODE_SUPPORTED;
+			}
+			temp++;
+			index++;
+		}
+		dtds1++;
+	}
+}
+
+/*!
+ *
+ * @param port
+ *
+ * @return 0
+ */
+int check_port_attrs(igd_display_port_t *port)
+{
+	int          ret;
+	unsigned long         attr_value = 0;
+#ifndef CONFIG_MICRO
+	pd_attr_t             out_list,*temp_list;
+	temp_list = &out_list;
+#endif
+	/* Attempt to see if the port driver has this attibutes so it can update
+	  the port driver value. For now this is required for internal HDMI which
+	  has a different i2c bus and port name from the standard SDVO port driver.
+	  DP would most likely use this attribute assuming it uese the same port
+	  number as well*/
+	ret = pi_pd_find_attr_and_value(port, PD_ATTR_ID_PORT_DDC_REG,
+		PD_ATTR_FLAG_GENERAL, NULL, &attr_value);
+	if(!ret){
+		EMGD_DEBUG("ddr_reg value unique = %ld.", attr_value);
+		port->ddc_reg = attr_value;
+	}
+#ifndef CONFIG_MICRO
+	ret = pi_pd_find_attr_and_value(port, PD_ATTR_ID_PORT_NAME,
+		PD_ATTR_FLAG_GENERAL, &(temp_list), &attr_value);
+	if(!ret){
+		EMGD_DEBUG("ddr_reg value unique = %ld.", attr_value);
+		pd_strcpy(port->port_name, temp_list->name);
+	}
+#endif
+	return 0;
+
+}
+
+/*!
+ * Function to initialize port driver related members in port table entry
+ *
+ * @param port
+ * @param port_feature
+ * @param second_port_feature
+ * @param drm_load_time
+ *
+ * @return PD_SUCCESS on success
+ * @return 1 on failure
+ */
+int pi_pd_init(igd_display_port_t *port,
+	unsigned long port_feature,
+	unsigned long second_port_feature,
+	int drm_load_time)
+{
+	igd_display_port_t *second_port;
+	pd_timing_t        *user_timings = NULL;
+	pd_timing_t        *std_timings = NULL;
+	pd_timing_t        *firmware_timings = NULL;
+	pd_timing_t        *final_timings = NULL;
+	pd_timing_t        *pd_timing_table = NULL;
+	mode_state_t       *mstate;
+	int                i, ret = PD_SUCCESS;
+	unsigned long      edid_flags;
+	unsigned char      num_firmware_timings = 0;
+	igd_display_params_t *display_params = NULL;
+	igd_param_t *init_params;
+
+	EMGD_TRACE_ENTER;
+
+	mstate = NULL;
+
+	/* If the display device is a ganged mode device or RGBA mode, then hook
+	 * up second port pointer in first port */
+	if (second_port_feature) {
+		second_port = pi_get_feature_port(second_port_feature, port);
+
+		/* If second_port is N/A, or second port was already taken by
+		 * other port driver, then release main port and return error */
+		if (second_port == NULL || second_port->pd_driver) {
+			EMGD_ERROR_EXIT("Second ganged/RGBA port N/A or already allocated.");
+			return PD_ERR_NOPORT_AVAIL;
+		}
+		/* now link second port to first one */
+		port->mult_port = second_port;
+	}
+
+	/* Check port attributes to overwrite port value is any available */
+	check_port_attrs(port);
+	/* Implementation notes to get the timing list:
+	 *   Any port timing table consists of
+	 *          EDID DTDS
+	 *          USER DTDS
+	 *          STD TIMINGS
+	 *   based on edid_flags.
+	 *
+	 * If there are no flags, then it defaults to use STD TIMINGS + EDID DTDs
+	 */
+
+	/* Get the display params for this port */
+	init_params = pi_context->igd_context->mod_dispatch.init_params;
+
+	for (i = 0; i < 5; i++) {
+		if (port->port_number == init_params->display_params[i].port_number) {
+			display_params = &init_params->display_params[i];
+			break;
+		}
+	}
+
+	/* Start with STD TIMINGS */
+	edid_flags = IGD_DISPLAY_USE_STD_TIMINGS;
+
+	/* If there is EDID, then default to use EDID */
+	if (!display_params || (display_params->flags & IGD_DISPLAY_READ_EDID)) {
+		/* Read firmware (EDID/DisplayID) on I2C */
+		ret = pi_context->i2c_dispatch->i2c_read_regs(
+			pi_context->igd_context,
+			port->ddc_reg,      /* DDC register */
+			port->ddc_speed,    /* DDC speed */
+			port->ddc_dab,      /* Data Addr Byte*/
+			0,                  /* Register */
+			firmware_data,      /* Values */
+			128,               /* Num bytes to read */
+			0);
+
+		/* If EDID is present then use EDID.
+		 * edid_flags will be corrected later if display_params are present */
+		if (ret == 0) {
+			edid_flags |= IGD_DISPLAY_USE_EDID;
+		}
+	}
+
+	/* Check for display params */
+	if (display_params) {
+		if (edid_flags & IGD_DISPLAY_USE_EDID) {
+			/* Adjust edid_flags to use edid_avail
+			 * if both edid is present and edid_avail is not 0 */
+			if (display_params->edid_avail) {
+				edid_flags = display_params->edid_avail;
+				EMGD_DEBUG("EDID_Avail: 0x%lx", edid_flags);
+			}
+		} else {
+			/* Adjust edid_flags to use edid_not_avail
+			 * if edid is not present and edid_not_avail is not 0 */
+			if (display_params->edid_not_avail) {
+				edid_flags =
+					display_params->edid_not_avail & ~IGD_DISPLAY_USE_EDID;
+				EMGD_DEBUG("EDID_Not_Avail: 0x%lx", edid_flags);
+			}
+		}
+	}
+
+	/* Make a copy of crt_timing_table */
+	/* All crt timings are already enabled in mode_table.c */
+	std_timings = (igd_timing_info_t *) OS_ALLOC(crt_timing_table_size);
+	OS_MEMCPY(std_timings, crt_timing_table, crt_timing_table_size);
+
+	/* Include Standard built-in modes */
+	if (edid_flags & IGD_DISPLAY_USE_STD_TIMINGS) {
+		EMGD_DEBUG("Using STD TIMINGS ");
+		final_timings = std_timings;
+	}
+
+	/* Include user DTDs */
+	if (edid_flags & IGD_DISPLAY_USE_USERDTDS) {
+		EMGD_DEBUG("Using USER-DTDs ");
+		user_timings = get_user_timings(port->dtd_list);
+
+		if (user_timings) {
+			/* Add user DTDs at the begining of the final timings */
+			user_timings[port->dtd_list->num_dtds].extn_ptr = final_timings;
+			final_timings = user_timings;
+		}
+	}
+
+	/* Include EDID timings and filter modes */
+	if (edid_flags & IGD_DISPLAY_USE_EDID) {
+		EMGD_DEBUG("Using EDID-DTDs ");
+		ret = get_firmware_timings(port, firmware_data, final_timings);
+		if (port->firmware_type == PI_FIRMWARE_EDID) {
+			firmware_timings = port->edid->timings;
+			num_firmware_timings = port->edid->num_timings;
+		} else if (port->firmware_type == PI_FIRMWARE_DISPLAYID) {
+			firmware_timings = port->displayid->timings;
+			num_firmware_timings = port->displayid->num_timings;
+		}
+		if (ret == 0 && num_firmware_timings) {
+			/* Add EDID DTDs at the begining of the final timings */
+			firmware_timings[num_firmware_timings].extn_ptr =
+				(void *)final_timings;
+			final_timings = firmware_timings;
+		}
+	}
+
+	/* Replace any common timings */
+	replace_common_dtds(std_timings, firmware_timings);
+	replace_common_dtds(std_timings, user_timings);
+	replace_common_dtds(firmware_timings, user_timings);
+
+	/* Count the number of timings in final_timings.  If the above functions
+	 * result in an empty timing list, then use std_timings as default.
+	 */
+	if (!get_native_dtd(final_timings, PI_SUPPORTED_TIMINGS, NULL, 0)) {
+		EMGD_DEBUG("User options resulted in 0 timings; using std timings.");
+		final_timings = std_timings;
+		enable_disable_timings(final_timings, 1);
+	}
+
+	/* Update port driver attributes */
+	update_attrs(port);
+
+	/* Now get the timing list filtered by PORT DRIVER */
+	ret = port->pd_driver->get_timing_list(port->pd_context,
+			final_timings, &pd_timing_table);
+
+	if (ret || !pd_timing_table) {
+		EMGD_ERROR_EXIT("port driver: get timing list error.");
+		return PD_ERR_NO_TIMINGS;
+	}
+
+	/* Delete temporary lists and buffers */
+	if (user_timings) {
+		OS_FREE(user_timings);
+	}
+	if (std_timings) {
+		OS_FREE(std_timings);
+	}
+
+	/* Filter modes based on chipset type */
+	pi_context->igd_context->mod_dispatch.filter_modes(pi_context->igd_context,
+		port, pd_timing_table);
+
+	/* Now save the timings in port */
+	port->timing_table = pd_timing_table;
+	port->num_timing = get_native_dtd(pd_timing_table,
+			PI_SUPPORTED_TIMINGS, &port->fp_native_dtd, PD_MODE_DTD_FP_NATIVE);
+
+	assign_dynamic_numbers(port->timing_table);
+
+#ifdef DEBUG_FIRMWARE
+	{
+		int ti;
+		EMGD_DEBUG("Supported timings for \"%s\" (%lu)",
+			port->pd_driver->name, port->num_timing);
+		ti = 0;
+		while (port->timing_table[ti].width != PD_TIMING_LIST_END) {
+			if (port->timing_table[ti].mode_info_flags & PD_MODE_SUPPORTED) {
+				EMGD_DEBUG("\t%ux%u@%u dclk=%lu mode_num=%d hsync=%luKHz "
+							"vsync=%luHz flags=0x%lx",
+					port->timing_table[ti].width,
+					port->timing_table[ti].height,
+					port->timing_table[ti].refresh,
+					port->timing_table[ti].dclk,
+					port->timing_table[ti].mode_number,
+					port->timing_table[ti].dclk/port->timing_table[ti].htotal,
+					((port->timing_table[ti].dclk * 1000)/
+					port->timing_table[ti].htotal)/
+					port->timing_table[ti].vtotal,
+					port->timing_table[ti].mode_info_flags);
+			}
+			ti++;
+		}
+	}
+#endif
+
+
+	/*
+	 * Exit early when called by emgd_driver_pre_init to poke the X driver's
+	 * (i.e. "xorg.conf") DTDs and attr's into the port drivers (done above).
+	 */
+	if (!drm_load_time) {
+		EMGD_TRACE_EXIT;
+		return PD_SUCCESS;
+	}
+
+
+#ifndef CONFIG_MICRO
+	/*
+	 * There is only two states that need to be saved; one is the regular state
+	 * and the other is for the console.
+	 */
+	ret = pi_save_mode_state(port, REG_MODE_STATE_REG);
+	if (config_drm.init) {
+		ret = pi_save_mode_state(port, REG_MODE_STATE_CON);
+	}
+#endif
+
+	if(port->displayid != NULL){
+		/* Driver to init audio if cea extension available */
+		if(port->firmware_type == PI_FIRMWARE_EDID){
+			port->callback->eld = &(port->edid->cea);
+		}
+		/* Displayid unsupported for now. Uncomment this code when audio
+		information is available for Display ID
+		else if(port->firmware_type == PI_FIRMWARE_EDID){
+			port->callback->eld = &(port->displayid->cea);
+		}
+		*/
+		else{
+			port->callback->eld = NULL;
+		}
+	}
+	ret = port->pd_driver->init_device(port->pd_context);
+	if (ret) {
+#ifndef CONFIG_MICRO
+		/* TODO: Restore the pd state? */
+		EMGD_ERROR_EXIT("port driver: init_device error. ret = %d", ret);
+		if (mstate) {
+			mstate->pd_state[pi_context->num_pi_drivers].port = NULL;
+			mstate->pd_state[pi_context->num_pi_drivers].state = NULL;
+		}
+#endif
+		return ret;
+	}
+
+	/* Increment the number of port drivers */
+	pi_context->num_pi_drivers++;
+
+	/* save the port driver display type & flags in port. These additions are
+	 * required to support different types displays by same port driver. */
+	port->pd_type = port->pd_driver->type;
+	port->pd_flags = port->pd_driver->flags;
+
+	EMGD_TRACE_EXIT;
+	return PD_SUCCESS;
+} /* end pi_pd_init */
+
+/*!
+ * Function to read registers
+ *
+ * @param context
+ * @param list
+ * @param type
+ *
+ * @return PD_SUCCESS on success
+ * @return PD_ERR_NULL_PTR, PD_ERR_I2C_READ, PD_ERR_UNSUCCESSFUL on failure
+ */
+int pi_read_regs(void *callback_context, pd_reg_t *list, unsigned long type)
+{
+	int ret;
+	igd_display_port_t *port = callback_context;
+	unsigned char      *mmio;
+
+	/*EMGD_TRACE_EXIT;*/
+
+	if (!port) {
+		EMGD_ERROR_EXIT("Null callback context passed.");
+		return PD_ERR_NULL_PTR;
+	}
+
+	if (!port->pd_driver) {
+		EMGD_ERROR_EXIT("Null pd_driver in port entry.");
+		return PD_ERR_NULL_PTR;
+	}
+
+	mmio = EMGD_MMIO(pi_context->igd_context->device_context.virt_mmadr);
+
+	/* Based on the port type either read GMCH registers or I2C registers */
+	switch (type) {
+	case PD_REG_I2C:
+		ret = 0;
+		while (list->reg != PD_REG_LIST_END) {
+			ret = pi_context->i2c_dispatch->i2c_read_regs(
+				pi_context->igd_context,
+				port->i2c_reg,
+				port->i2c_speed,
+				port->dab,
+				(unsigned char)list->reg,
+				(unsigned char *)&list->value, 1, 0);
+			if (ret) {
+				EMGD_DEBUG("i2c_read_reg: 0x%lx failed.", list->reg);
+				break;
+			}
+			list++;
+		}
+		if (ret) {
+			return PD_ERR_I2C_READ;
+		}
+		break;
+	case PD_REG_DDC_FW:
+		ret = 0;
+		while (list->reg != PD_REG_LIST_END) {
+			ret = pi_context->i2c_dispatch->i2c_read_regs(
+				pi_context->igd_context,
+				port->ddc_reg,
+				port->ddc_speed,
+				port->ddc_dab,
+				(unsigned char)list->reg,
+				(unsigned char *)&list->value, 1,
+				IGD_I2C_WRITE_FW);
+			if (ret) {
+				EMGD_DEBUG("i2c_read_reg: 0x%lx failed.", list->reg);
+				break;
+			}
+			list++;
+		}
+		if (ret) {
+			return PD_ERR_I2C_READ;
+		}
+		break;
+	case PD_REG_DDC:
+		ret = 0;
+		while (list->reg != PD_REG_LIST_END) {
+			ret = pi_context->i2c_dispatch->i2c_read_regs(
+				pi_context->igd_context,
+				port->ddc_reg,
+				port->ddc_speed,
+				port->ddc_dab,
+				(unsigned char)list->reg,
+				(unsigned char *)&list->value, 1,
+				0);
+			if (ret) {
+				EMGD_DEBUG("i2c_read_reg: 0x%lx failed.", list->reg);
+				break;
+			}
+			list++;
+		}
+		if (ret) {
+			return PD_ERR_I2C_READ;
+		}
+		break;
+	case PD_REG_PIO8:
+		while (list->reg != PD_REG_LIST_END) {
+			list->value = EMGD_READ_PORT8(list->reg);
+			list++;
+		}
+		break;
+	case PD_REG_PIO16:
+		while (list->reg != PD_REG_LIST_END) {
+			list->value = EMGD_READ_PORT16(list->reg);
+			list++;
+		}
+		break;
+	case PD_REG_PIO32:
+		while (list->reg != PD_REG_LIST_END) {
+			list->value = EMGD_READ_PORT32(list->reg);
+			list++;
+		}
+		break;
+	case PD_REG_MIO :
+	case PD_REG_MIO8 :
+		if ((port->port_type == IGD_PORT_ANALOG) ||
+			(port->port_type == IGD_PORT_TV)     ||  /* For Integrated TV */
+			(port->port_type == IGD_PORT_LVDS)	 ||
+			(port->port_type == IGD_PORT_DIGITAL)) {
+			while (list->reg != PD_REG_LIST_END) {
+				if (type == PD_REG_MIO) {
+					if (BIT31 & list->reg) {
+#ifdef CONFIG_TNC
+						/* Atom E6xx si hack: Si folks defined LVDS (0:2:0)
+						 * related register in 0:3:0 (sdvo device) as they
+						 * are afraid to touch Lincroft hardmacro.
+						 * This triggered LVDS port driver to touch 0:3:0
+						 * registers for its operation. As this is done for
+						 * LVDS operation and LVDS port driver is internal,
+						 * BIT31 is defined to access 0:3:0 device. */
+						list->value = READ_MMIO_REG_TNC(IGD_PORT_SDVO,
+							list->reg);
+#endif
+					} else {
+						list->value = EMGD_READ32(EMGD_MMIO(mmio) + list->reg);
+					}
+				} else {
+					list->value = EMGD_READ8(EMGD_MMIO(mmio) + list->reg);
+				}
+				list++;
+			}
+		}
+		break;
+#ifdef CONFIG_TNC
+	case PD_REG_LPC:
+		if (port->port_type == IGD_PORT_LVDS) {
+			while (list->reg != PD_REG_LIST_END) {
+				list->value = READ_MMIO_REG_TNC(IGD_PORT_LPC, list->reg);
+				list++;
+			}
+		}
+		break;
+#endif
+	case PD_REG_PCI:
+		/* Rightnow this is only to provide the device id */
+		while (list->reg != PD_REG_LIST_END) {
+#if 0
+			/* Assume IGD at bus=0, dev=2, func=0 */
+			EMGD_WRITE_PORT32(0xCF8,
+				(0x80000000 | (0L << 16) | (2L << 11) | (0L << 8) |
+					(list->reg & 0xFC)));
+			list->value = EMGD_READ_PORT32(0xCFC + (list->reg & 0x03));
+#endif
+			list->value = pi_context->igd_context->device_context.did;
+			list++;
+		}
+		break;
+	case PD_REG_BRIDGE_OPCODE:
+		/* right now, we only return the graphics frequency to calculate the
+		 * PWM Backlight modulation frequency. This is only available for pouslbo */
+		while (list->reg != PD_REG_LIST_END) {
+			list->value = pi_context->igd_context->device_context.gfx_freq;
+			list++;
+		}
+
+		break;
+	default:
+		EMGD_ERROR_EXIT("Unknown reg type (0x%lx).", type);
+		return PD_ERR_UNSUCCESSFUL;
+		break;
+	}
+
+	/*EMGD_TRACE_EXIT;*/
+	return PD_SUCCESS;
+} /* end pi_read_regs */
+
+/*!
+ * Function to write registers
+ *
+ * @param context
+ * @param list
+ * @param type
+ *
+ * @return 0 on success
+ * @return PD_ERR_NULL_PTR, PD_ERR_I2C_WRITE, PD_ERR_UNSUCCESSFUL on failure
+ */
+extern unsigned short io_base_sdvo;
+extern unsigned short io_base;
+extern unsigned short io_base_sdvo_st;
+extern unsigned short io_base_sdvo_st_gpio;
+
+int pi_write_regs(void *callback_context, pd_reg_t *list, unsigned long type)
+{
+	igd_display_port_t *port = callback_context;
+	int           ret;
+	unsigned char *mmio;
+
+	EMGD_TRACE_ENTER;
+
+	if (!port) {
+		EMGD_ERROR_EXIT("Null callback context passed.");
+		return PD_ERR_NULL_PTR;
+	}
+
+	if (!port->pd_driver) {
+		EMGD_ERROR_EXIT("Null pd_driver.");
+		return PD_ERR_NULL_PTR;
+	}
+
+	EMGD_DEBUG("Getting mmio");
+	mmio = EMGD_MMIO(pi_context->igd_context->device_context.virt_mmadr);
+	EMGD_DEBUG("mmio = 0x%lx", (unsigned long)mmio);
+
+	/* Based on the port type either write GMCH registers or I2C registers */
+	switch (type) {
+	case PD_REG_DDC_FW:
+		/*This will use shorter delay than PD_REG_DDC*/
+		ret = pi_context->i2c_dispatch->i2c_write_reg_list(
+			pi_context->igd_context,
+			port->ddc_reg,
+			port->ddc_speed,
+			port->ddc_dab,
+			list,
+			IGD_I2C_WRITE_FW);
+		if (ret) {
+        	EMGD_DEBUG("i2c_write_reg: 0x%lx = 0x%lx failed.",
+       		list->reg, list->value);
+        	return PD_ERR_I2C_WRITE;
+        }
+    	break;
+	case PD_REG_DDC:
+		ret = pi_context->i2c_dispatch->i2c_write_reg_list(
+			pi_context->igd_context,
+			port->ddc_reg,
+			port->ddc_speed,
+			port->ddc_dab,
+			list,
+			0);
+		if (ret) {
+        	EMGD_DEBUG("i2c_write_reg: 0x%lx = 0x%lx failed.",
+       		list->reg, list->value);
+        	return PD_ERR_I2C_WRITE;
+        }
+    	break;
+	case PD_REG_I2C:
+		ret = pi_context->i2c_dispatch->i2c_write_reg_list(
+			pi_context->igd_context,
+			port->i2c_reg,
+			port->i2c_speed,
+			port->dab,
+			list,
+			0);
+		if (ret) {
+			EMGD_DEBUG("i2c_write_reg: 0x%lx = 0x%lx failed.",
+				list->reg, list->value);
+			return PD_ERR_I2C_WRITE;
+		}
+		EMGD_DEBUG("i2c_write_reg success");
+		break;
+	case PD_REG_PIO8:
+		while (list->reg != PD_REG_LIST_END) {
+			EMGD_WRITE_PORT8(list->reg, list->value);
+			list++;
+		}
+		EMGD_DEBUG("EMGD_WRITE_PORT8 seemed successful");
+		break;
+	case PD_REG_PIO16:
+		while (list->reg != PD_REG_LIST_END) {
+			EMGD_WRITE_PORT16(list->reg, list->value);
+			list++;
+		}
+		EMGD_DEBUG("EMGD_WRITE_PORT16 seemed successful");
+		break;
+	case PD_REG_PIO32:
+		while (list->reg != PD_REG_LIST_END) {
+			EMGD_WRITE_PORT32(list->reg, list->value);
+			list++;
+		}
+		EMGD_DEBUG("EMGD_WRITE_PORT32 seemed successful");
+		break;
+	case PD_REG_MIO :
+	case PD_REG_MIO8 :
+		if ((port->port_type == IGD_PORT_ANALOG) ||
+			(port->port_type == IGD_PORT_TV)     ||  /* For Integrated TV */
+			(port->port_type == IGD_PORT_LVDS)	 ||
+			(port->port_type == IGD_PORT_DIGITAL)) {
+			while (list->reg != PD_REG_LIST_END) {
+				if (type == PD_REG_MIO) {
+					if (BIT31 & list->reg) {
+#ifdef CONFIG_TNC
+						/* BIT31 indicates write to 0:3:0 SDVO device */
+						WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, list->reg,
+							list->value);
+#endif
+					} else {
+						EMGD_WRITE32(list->value, EMGD_MMIO(mmio) + list->reg);
+					}
+				} else {
+					EMGD_WRITE8(list->value, EMGD_MMIO(mmio) + list->reg);
+				}
+				list++;
+			}
+			EMGD_DEBUG("complicated write seemed successful");
+		}
+		break;
+#ifdef CONFIG_TNC
+	case PD_REG_LPC:
+		if (port->port_type == IGD_PORT_LVDS) {
+			while (list->reg != PD_REG_LIST_END) {
+				WRITE_MMIO_REG_TNC(IGD_PORT_LPC, list->reg, list->value);
+				list++;
+			}
+			EMGD_DEBUG("Write to IGD_PORT_LPC seemed successful");
+		}
+		break;
+#endif
+	default:
+		EMGD_ERROR_EXIT("Unknown reg type (0x%lx).", type);
+		return PD_ERR_UNSUCCESSFUL;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* end pi_write_regs */
+
+/*!
+ * Depending on the parameters, this function does multiple things.  It always
+ * counts and returns the number of [supported] timings.  If desired, it also
+ * finds the timing with a desired "mode_info_flags" that has the largest value
+ * of width, height, OR refresh rate, which it sets to the "native_dtd"
+ * parameter.
+ *
+ * @param timing
+ * @param flags
+ * @param native_dtd
+ * @param nflags
+ *
+ * @return 0 on failure
+ * @return native dtd on success
+ */
+unsigned long get_native_dtd(igd_timing_info_t *timing,
+	unsigned long flags, pd_timing_t **native_dtd, unsigned long nflags)
+{
+	unsigned long entries = 0;
+
+	EMGD_TRACE_EXIT;
+
+	if (!timing) {
+		return 0;
+	}
+	if (native_dtd) {
+		*native_dtd = NULL;
+	}
+	while (timing->width != IGD_TIMING_TABLE_END) {
+		if (flags & PI_SUPPORTED_TIMINGS) {
+			if (timing->mode_info_flags & PD_MODE_SUPPORTED) {
+				entries++;
+			}
+			if ((native_dtd) &&
+				(timing->mode_info_flags & nflags)) {
+				/* Native Resolution is defined as the largest resolution the
+				 * panel can display. However, some panels contain more than one
+				 * DTD in its EDID. We will choose the largest resolution
+				 * available from EDID */
+				if(((*native_dtd) && (nflags == PD_MODE_DTD)) &&
+					(((pd_timing_t*)(*native_dtd))->width > timing->width ||
+					((pd_timing_t*)(*native_dtd))->height > timing->height ||
+					((pd_timing_t*)(*native_dtd))->refresh > timing->refresh)){
+						/* do nothing */
+				} else {
+					*native_dtd = timing;
+				}
+			}
+		} else {
+			entries++;
+		}
+
+		timing++;
+		if ((timing->width == PD_TIMING_LIST_END) && timing->extn_ptr) {
+			timing = timing->extn_ptr;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return entries;
+}
+
+#ifndef CONFIG_MICRO
+unsigned long get_magic_cookie(pd_driver_t *pd_driver)
+{
+	/* FIXME: Implement cookie checking */
+	return 0;
+}
+#endif
+
+/*!
+ * Function to filter modes based on EDID or DisplayID
+ *
+ * @param port
+ * @param firmware_data
+ * @param timing_table
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_EDID, -IGD_ERROR_NOMEM on failure
+ */
+int get_firmware_timings(igd_display_port_t *port,
+	unsigned char *firmware_data, igd_timing_info_t *timing_table)
+{
+	edid_t         *edid;
+	displayid_t    *displayid;
+	int            ret = -1;
+
+	EMGD_TRACE_ENTER;
+
+	if (!firmware_data) {
+		return -IGD_ERROR_EDID;
+	}
+
+	if (!port->displayid) {
+		/* EDID and DisplayID use same memory */
+		displayid = (displayid_t *) OS_ALLOC(sizeof(displayid_t));
+		if (!displayid) {
+			return -IGD_ERROR_NOMEM;
+		}
+		edid = (edid_t *) displayid;
+	} else {
+		displayid = port->displayid;
+		edid = (edid_t *) displayid;
+	}
+	OS_MEMSET(displayid, 0, sizeof(displayid_t));
+
+	/* Now parse the EDID or DisplayID */
+	/* Check the header to determine whether the data is EDID or DisplayID */
+	/* EDID header first 8 bytes =
+	 *     byte 0, 1, 2, 3: 00 ff ff ff = unsigned long 0xFFFFFF00
+	 *     byte 4, 5, 6, 7: ff ff ff 00 = unsigned long 0x00FFFFFF */
+	if (*(unsigned long *) &firmware_data[0] == 0xFFFFFF00 &&
+		*(unsigned long *) &firmware_data[4] == 0x00FFFFFF) {
+#ifdef DEBUG_FIRMWARE
+		firmware_dump(firmware_data, 256);
+#endif
+		/* This is EDID data */
+		ret = edid_parse(firmware_data, edid, timing_table, 0,
+			(unsigned char) (port->pd_driver->flags&PD_FLAG_UP_SCALING?1:0));
+		if (ret == EDID_READ_AGAIN) {
+			/* Check to see if there is an extension block */
+			if((firmware_data[0x7e] == 0x1)){
+				ret = pi_context->i2c_dispatch->i2c_read_regs(
+					pi_context->igd_context,
+					port->ddc_reg,      /* DDC register */
+					port->ddc_speed,    /* DDC speed */
+					port->ddc_dab,      /* Data Addr Byte*/
+					0x80,                /* Register */
+					&firmware_data[128], /* Values */
+					128,
+					0);				 /* next 128 bytes include extension */
+				ret = edid_ext_parse(&firmware_data[128], edid, timing_table,0,
+					(unsigned char)(port->pd_driver->flags&
+					PD_FLAG_UP_SCALING?1:0));
+				/* Parse next 128 bytes of EDID block */
+			}else{
+				ret = 0;
+			}
+		} else if (ret) {
+			OS_FREE(edid);
+			return -IGD_ERROR_EDID;
+		}
+
+		port->firmware_type = PI_FIRMWARE_EDID;
+#ifdef DEBUG_FIRMWARE
+		edid_print(edid);
+#endif
+
+#ifndef CONFIG_NO_DISPLAYID
+	} else {
+		/* size  = payload + 5 */
+		/* +5 is for the 5 mandatory bytes not included in payload */
+		unsigned short displayid_size = firmware_data[1] + 5;
+		if (displayid_size > 256) {
+			EMGD_DEBUG("Invalid DisplayID size = %u (incl 5 mand bytes) > 256",
+				displayid_size);
+			return -IGD_ERROR_EDID;
+		}
+
+		/* If the DisplayID is greater than 128 bytes */
+		if (displayid_size > 128) {
+			ret = pi_context->i2c_dispatch->i2c_read_regs(
+				pi_context->igd_context,
+				port->ddc_reg,      /* DDC register */
+				port->ddc_speed,    /* DDC speed */
+				port->ddc_dab,      /* Data Addr Byte*/
+				0,                  /* Register */
+				firmware_data,      /* Values */
+				displayid_size,    /* Num bytes to read */
+				0);
+		}
+
+#ifdef DEBUG_FIRMWARE
+		firmware_dump(firmware_data, 256);
+#endif
+		/* This is DisplayID data */
+		ret = displayid_parse(firmware_data, displayid, timing_table, 0,
+			(unsigned char) (port->pd_driver->flags&PD_FLAG_UP_SCALING?1:0));
+		if (!ret) {
+			port->firmware_type = PI_FIRMWARE_DISPLAYID;
+		} else {
+			OS_FREE(displayid);
+		}
+#ifdef DEBUG_FIRMWARE
+		displayid_print(firmware_data, displayid);
+#endif
+#else
+	/* If DisplayID isn't enabled then print a debug message and return error */
+	} else {
+		EMGD_ERROR_EXIT("EDID header is wrong! Will ignore");
+		return -IGD_ERROR_EDID;
+#endif
+	}
+
+	port->edid = edid;
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* end get_firmware_timings() */
+
+/*!
+ * Update port driver attributes with incoming values from IAL
+ *
+ * @param in_list attributes in this list are used to update corresponding
+ *	attributes in out_list
+ * @param fp_info flat panel attributes used to update ocrresponding
+ *	attributes in out_list
+ * @param out_num_attrs size of out_list
+ * @param out_list contains a list of attributes to be updated
+ *
+ * @return 0
+ */
+int update_attrs(igd_display_port_t *port)
+{
+	int          ret;
+	unsigned int i = 0;
+
+	igd_param_attr_list_t *in_list = port->attr_list;
+	igd_param_fp_info_t   *fp_info = port->fp_info;
+
+	/* Initial 5 attributes are for fp_info,
+	 * Note: If both igd_param_fp_info_t and fp_info attributes
+	 * were specified then fp_info values takes the precedence */
+	unsigned long         out_num_attrs = 5;
+	pd_attr_t             *out_list;
+
+	EMGD_TRACE_ENTER;
+
+	/* if: there's something in in_list */
+	if (in_list) {
+		out_num_attrs += in_list->num_attrs;
+	}
+
+	out_list = OS_ALLOC(sizeof(pd_attr_t) * out_num_attrs);
+	if (!out_list) {
+		EMGD_DEBUG("No memory to make attr_list.");
+		return 0;
+	}
+	OS_MEMSET(out_list, 0, sizeof(pd_attr_t) * out_num_attrs);
+
+	/* Pass user specified attributes */
+	if (in_list) {
+		/* For every incoming attr, make a pd_attr_t */
+		for (i = 0; i < in_list->num_attrs; i++) {
+			/* This will work for all kinds of attributes:
+			 * in_list->attr[i].value =
+			 *      actual value for range attributes
+			 *      index for list  attributes
+			 *      value for boolean attributes.
+			 * This works because
+			 *   pd_attr_t, pd_range_attr_t, pd_list_attr_t,
+			 *   pd_boolean_attr_t all have save offsets for
+			 *   default values. */
+			out_list[i].id = in_list->attr[i].id;
+			out_list[i].flags = PD_ATTR_FLAG_VALUE_CHANGED;
+			out_list[i].current_value = in_list->attr[i].value;
+		}
+	}  /* if: there's something in in_list */
+
+	/* Pass flat panel attributes to the port driver, if necessary */
+	if (fp_info) {
+		/* Initialize flat panel attributes */
+		/* Update FP attributes */
+		if (fp_info->fp_pwr_method == IGD_PARAM_FP_PWR_METHOD_PD) {
+			/* The only thing remaining is the FP_PWR_Tx, so check
+			 * to ensure it is for an FP_PWR_METHOD_PD */
+			out_list[i].id = PD_ATTR_ID_FP_PWR_T1;
+			out_list[i].flags = PD_ATTR_FLAG_VALUE_CHANGED;
+			out_list[i++].current_value = fp_info->fp_pwr_t1;
+
+			out_list[i].id = PD_ATTR_ID_FP_PWR_T2;
+			out_list[i].flags = PD_ATTR_FLAG_VALUE_CHANGED;
+			out_list[i++].current_value = fp_info->fp_pwr_t2;
+
+			out_list[i].id = PD_ATTR_ID_FP_PWR_T3;
+			out_list[i].flags = PD_ATTR_FLAG_VALUE_CHANGED;
+			out_list[i++].current_value = fp_info->fp_pwr_t3;
+
+			out_list[i].id = PD_ATTR_ID_FP_PWR_T4;
+			out_list[i].flags = PD_ATTR_FLAG_VALUE_CHANGED;
+			out_list[i++].current_value = fp_info->fp_pwr_t4;
+
+			out_list[i].id = PD_ATTR_ID_FP_PWR_T5;
+			out_list[i].flags = PD_ATTR_FLAG_VALUE_CHANGED;
+			out_list[i++].current_value = fp_info->fp_pwr_t5;
+		}
+	}
+
+#ifndef CONFIG_NO_DISPLAYID
+	/* Based on our architecture, any user-defined config option will override
+	 * firmware options. i.e., First send the DisplayID attributes
+	 * then send the config attributes to port drivers. */
+	if (port->firmware_type == PI_FIRMWARE_DISPLAYID) {
+		ret = port->pd_driver->set_attrs(port->pd_context,
+			port->displayid->num_attrs,
+			port->displayid->attr_list);
+	}
+#endif
+	ret = port->pd_driver->set_attrs(port->pd_context, i, out_list);
+	if (ret) {
+		EMGD_DEBUG("Attribute update failed. ret = %d.", ret);
+	}
+	OS_FREE(out_list);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* update_attrs */
+
+/* Add user defined timings to big timing table */
+pd_timing_t *get_user_timings(igd_param_dtd_list_t *in_list)
+{
+	pd_timing_t        *t = NULL, *timing = NULL;
+	igd_display_info_t *dtd;
+	unsigned long i;
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	if (!in_list || !(in_list->num_dtds) || !(in_list->dtd)) {
+		return NULL;
+	}
+
+	t = (pd_timing_t *)OS_ALLOC((in_list->num_dtds + 1)
+			* sizeof(pd_timing_t));
+	if (!t) {
+		return NULL;
+	}
+
+	OS_MEMSET(t, 0, (in_list->num_dtds + 1) * sizeof(pd_timing_t));
+	timing = t;
+	dtd = in_list->dtd;
+
+	/*
+	 * OPTIMIZEME: When igd_display_info_t goes away there will be no reason
+	 * to copy this data. A Rule can be imposed that anything passed to
+	 * the HAL during init must remain in scope until the HAL is shut
+	 * down. The HAL can then just use this directly.
+	 */
+	for (i = 0; i < in_list->num_dtds; i++) {
+		OS_MEMCPY(t, dtd, sizeof(igd_display_info_t));
+		t->mode_info_flags = dtd->flags | PD_MODE_DTD_USER | PD_MODE_SUPPORTED;
+#if 0
+		/* Assume there is no border, then htotal and vtotal are the same as
+		 * hblank_end and vblank_end */
+		t->htotal = t->hblank_end;
+		t->vtotal = t->vblank_end;
+		if (dtd->refresh) {
+			t->refresh = (unsigned short)dtd->refresh;
+		} else if (t->htotal && t->vtotal) {
+			t->refresh = (unsigned short)
+				((unsigned long)((unsigned long)(t->dclk) * 1000) /
+					((unsigned long)(t->htotal) *
+						(unsigned long)(t->vtotal)));
+		}
+#endif
+
+#ifndef CONFIG_MICRO
+               /*
+                * If the VESA flag is set, set the mode mode number to VESA.
+                * DTD may not contain mode number but the VESA flag is set
+                * This will cause program pipe VGA to be executed and fail
+                * Need to make sure that if USER wants to use VGA mode, that the
+                * mode_number is entered in the user DTD
+                */
+               if((t->mode_info_flags & PD_MODE_VESA) && (!t->mode_number)){
+                       t->mode_number = VGA_MODE_NUM_MAX + 1;
+               }
+
+               /*
+                * Handle the corner case where user DTD is derived from std timing.
+                * Two std timings have border (htotal != h_blank_end).
+                * Compare the timing attribute with the std timing and use the
+                * total and refresh rate from std timing.
+                * Only happens when using Harmonic tool so the change is limited
+                * to Atom E6xx through the dispatch function
+                */
+                if(pi_context->igd_context->mod_dispatch.get_refresh_in_border){
+                       /* returns 1 if a refresh was obtained */
+                       ret = pi_context->igd_context->mod_dispatch.get_refresh_in_border(t);
+               }
+#endif
+               if(!ret){
+                       /* Assume there is no border, then htotal and vtotal are the same as
+                        * hblank_end and vblank_end */
+                       t->htotal = t->hblank_end;
+                       t->vtotal = t->vblank_end;
+                       if (dtd->refresh) {
+                               t->refresh = (unsigned short)dtd->refresh;
+                       } else if (t->htotal && t->vtotal) {
+                               /*
+                                * Refresh is used mainly for esthetic, mainly in GUI.
+                                * The compiler will truncate the decimals, not rounding
+                                * UP the value (in this case, 59.7 will be 59Hz and not 60Hz
+                                * Need to manually handle the rounding.
+                                *
+                                * We multiply the dclk by 10, therefore shifting the final
+                                * decimal place by one, then check if the last digit is
+                                * >4 to round up the refresh by 1 after dividing by 10.
+                                */
+                               unsigned short temp_refresh = (unsigned short)
+                                       ((unsigned long)((unsigned long)(t->dclk) * 10000) /
+                                               ((unsigned long)(t->htotal) *
+                                                       (unsigned long)(t->vtotal)));
+                               if((temp_refresh % 10) > 4){
+                                       t->refresh = (temp_refresh / 10 ) + 1;
+                               } else {
+                                       t->refresh = (temp_refresh / 10);
+                               }
+                       }
+               }
+
+		/* t->pd_extn_ptr = NULL; */
+		t->extn_ptr = NULL;
+
+		t++;
+		dtd++;
+	}
+
+	/* End the table with end marker */
+	t->width = IGD_TIMING_TABLE_END;
+
+	EMGD_TRACE_EXIT;
+
+	return timing;
+}
+
+/*!
+ * Assign dynamic VBE numbers to the modes that do not already have
+ * VESA defined numbers.
+ *
+ * @param timing_table
+ *
+ * @return void
+ */
+#define FIRST_DYNAMIC_MODE_NUMBER 0x120
+void assign_dynamic_numbers(igd_timing_info_t *timing_table)
+{
+	unsigned short next_number = FIRST_DYNAMIC_MODE_NUMBER;
+	unsigned int i;
+	unsigned short vesa_mode_table[] = {
+		640, 480, 0x101,
+		800, 600, 0x103,
+		1024, 768, 0x105,
+		1280, 1024, 0x107,
+		0xffff, 0xffff, 0xffff,
+	};
+
+	EMGD_TRACE_ENTER;
+
+	while(timing_table->width != IGD_TIMING_TABLE_END) {
+		if((timing_table->mode_info_flags & IGD_MODE_SUPPORTED) &&
+			!(timing_table->mode_info_flags & IGD_MODE_VESA)) {
+
+			for (i=0; vesa_mode_table[i] != 0xffff; i+=3) {
+				if ((timing_table->width == vesa_mode_table[i]) &&
+					(timing_table->height == vesa_mode_table[i+1])) {
+					/* This is a VESA Standard mode, so assign it to the
+					 * correct VESA mode number.  This can occur with
+					 * modes added either through User Defined DTDs or
+					 * potentially EDID. */
+					timing_table->mode_number = vesa_mode_table[i+2];
+					timing_table->mode_info_flags |= IGD_MODE_VESA;
+					break;
+				}
+			}
+			if (vesa_mode_table[i] == 0xffff) {
+				/* Assign this mode a Dynamic number, if it is not
+				 * a VESA Standard mode. */
+				timing_table->mode_number = next_number;
+				/* VBE modes use lower 2 bits for depth so next mode is += 4 */
+				next_number += 4;
+				timing_table->mode_info_flags |= IGD_MODE_VESA;
+			}
+		}
+		timing_table++;
+
+		/* If reached the first table END,
+		 * then check for the added modes */
+		if (timing_table->width == IGD_TIMING_TABLE_END &&
+			timing_table->extn_ptr) {
+			timing_table = timing_table->extn_ptr;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+/*!
+ * This is a utility function that can be used througout the HAL.
+ * It can be used to get a ptr to an attr structure and/or the
+ * actual current_value of that attribute.
+ * According to usage modal at time of creation of this function:
+ *	- the *caller_pd_attr must be NULL if the attr was not found.
+ *	- the *attr_value is only changed if the attr was found
+ *
+ * @param port
+ * @param attr_id
+ * @param flag
+ * @param caller_pd_attr
+ * @param attr_value
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+int pi_pd_find_attr_and_value(igd_display_port_t *port,
+		unsigned long attr_id,
+		unsigned long flag,
+		pd_attr_t   **caller_pd_attr,
+		unsigned long *attr_value)
+{
+	unsigned long pd_attr_length   = 0;
+	pd_attr_t    *pd_attr_list     = NULL;
+	pd_attr_t    *found_pd_attr    = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	if(!port || !(port->pd_driver)) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	if(flag == PD_ATTR_FLAG_GENERAL){
+		pd_attr_length = PD_QUERY_GENERAL_ATTR;
+	}
+	port->pd_driver->get_attrs(port->pd_context, &pd_attr_length,
+		&pd_attr_list);
+	if(!pd_attr_length) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	found_pd_attr = pd_get_attr(pd_attr_list, pd_attr_length, attr_id, flag);
+
+	if (!found_pd_attr) {
+		if(caller_pd_attr) {
+			*caller_pd_attr = NULL;
+		}
+		return -IGD_INVAL;
+	}
+	if(caller_pd_attr) {
+		*caller_pd_attr = found_pd_attr;
+	}
+	if(attr_value) {
+		*attr_value = found_pd_attr->current_value;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ *
+ * @param port
+ * @param id
+ * @param value
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+int pi_get_port_init_attr(igd_display_port_t *port,
+		unsigned long id,
+		unsigned long *value)
+{
+	unsigned short i;
+
+	EMGD_TRACE_ENTER;
+
+	if (!port || !port->attr_list) {
+		return -IGD_ERROR_INVAL;
+	}
+
+	for (i = 0; i < (unsigned short) port->attr_list->num_attrs; i++) {
+		if (port->attr_list->attr[i].id == id) {
+			*value = port->attr_list->attr[i].value;
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+	}
+
+	EMGD_DEBUG("Attribute (0x%ld) Not Found", id);
+	EMGD_TRACE_EXIT;
+	return -IGD_ERROR_INVAL;
+}
+
+
+
+int pi_save_mode_state(igd_display_port_t *port, reg_state_id_t reg_state_id)
+{
+	int ret = PD_SUCCESS;
+	mode_state_t  *mstate = NULL;
+	if(pi_context->igd_context->mod_dispatch.reg_get_mod_state) {
+		module_state_h     *state = NULL;
+		unsigned long *flags = NULL;
+		pi_context->igd_context->mod_dispatch.reg_get_mod_state(
+			reg_state_id,
+			&state,
+			&flags);
+
+		if (state) {
+			mstate = (mode_state_t *)(*state);
+		}
+	}
+
+	/* If mode state is present in register context,
+	 * then call save() function to save the port driver's state */
+	if (mstate) {
+		ret = port->pd_driver->pd_save(port->pd_context,
+				&(mstate->pd_state[pi_context->num_pi_drivers].state), 0);
+		if (ret) {
+			EMGD_ERROR_EXIT("port driver: reg saving error. ret = %d", ret);
+			return ret;
+		}
+		mstate->pd_state[pi_context->num_pi_drivers].port = port;
+	}
+
+	return ret;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/plb/i2c_plb.c b/drivers/gpu/drm/emgd/emgd/display/pi/plb/i2c_plb.c
new file mode 100644
index 0000000..7021e34
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/plb/i2c_plb.c
@@ -0,0 +1,943 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: i2c_plb.c
+ * $Revision: 1.14 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd_pwr.h>
+
+#include <general.h>
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+
+#include <plb/regs.h>
+
+#include "../cmn/i2c_dispatch.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*......................................................................... */
+extern igd_display_port_t dvob_port_plb;
+
+/*......................................................................... */
+static int i2c_read_regs_plb(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags);
+
+static int i2c_write_reg_list_plb(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags);
+
+i2c_dispatch_t i2c_dispatch_plb = {
+	i2c_read_regs_plb,
+	i2c_write_reg_list_plb,
+};
+
+
+/*.......................................................................... */
+typedef enum {
+
+	GMBUS_SPEED_50K     = 0x0100,
+	GMBUS_SPEED_100K	= 0x0000,
+	GMBUS_SPEED_400K	= 0x0200,
+	GMBUS_SPEED_1000K	= 0x0300,
+
+} gmbus_speed_t;
+
+typedef enum {
+
+	SDVOB_ADDR	= 0x70,
+	SDVOC_ADDR	= 0x72,
+
+} sdvo_dev_addr_t;
+
+typedef enum {
+
+	DDC1_ADDR = 0xA0,
+	DDC2_ADDR = 0xA2,
+
+} gmbus_ddc_addr_t;
+
+
+typedef enum {
+
+	GMBUS_PINS_DEDICATED = 1, /* Dedicated Control/GMBUS Pins */
+	/* LCTRCLKA, LCTRLCLKB SSC Clock Device */
+	GMBUS_PINS_ANALOG	= 2,  /* Analog DDC */
+	GMBUS_PINS_INT_LVDS = 3,  /* Alviso : Integrated Digital Panel */
+	GMBUS_PINS_SDVO     = 5,  /* SDVO Registers, DDC, PROM */
+
+} gmbus_pins_pair_t;
+
+
+typedef enum {
+
+	I2C_WRITE = 0,
+	I2C_READ  = 1,
+
+} i2c_bus_dir_t;
+
+/*.......................................................................... */
+typedef enum {
+
+	SDVO_BUS_PROM = BIT(0),
+	SDVO_BUS_DDC1 = BIT(1),
+	SDVO_BUS_DDC2 = BIT(2),
+
+} sdvo_bus_switch_t;
+
+#define SDVO_OPCODE_BUS_SWITCH	0x7A
+
+#define SDVO_INDEX_PARAM_1		0x07
+#define SDVO_INDEX_OPCODE		0x08
+#define SDVO_INDEX_STATUS		0x09
+
+#define SDVO_STATUS_SUCCESS     0x01
+#define SDVO_STATUS_PENDING     0x04
+
+/*.......................................................................... */
+/*
+ * In 16-bit, the mmio is a 16-bit pointer, the watcom 1.2 compiler will have
+ * error if directly convert it to unsigned long.  Normally, have to cast it to
+ * unsigned short first then cast again to unsigned long; then, it will be
+ * correct.  But this type of casting may cause some error in the 32 and 64 bit
+ * code.  Since mmio will be equal to zero for 16-bit code.  Add the checking
+ * for MICRO definition code to correct the macro by remove mmio.
+ */
+#define READ_GMCH_REG(reg)			EMGD_READ32(EMGD_MMIO(mmio) + reg)
+#define WRITE_GMCH_REG(reg, data)	EMGD_WRITE32(data, EMGD_MMIO(mmio) + reg)
+
+static int gmbus_init(unsigned char *mmio, unsigned long i2c_bus,
+	unsigned long i2c_speed);
+
+static int gmbus_read_edid(unsigned char *mmio,
+	unsigned long ddc_addr,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned long num_bytes,
+	unsigned char FAR *buffer);
+
+static int gmbus_read_reg(unsigned char *mmio,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned char FAR *data);
+
+static int gmbus_write_reg(unsigned char *mmio,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned char data);
+
+static int gmbus_set_control_bus_switch(unsigned char *mmio,
+	unsigned long slave_addr,
+	gmbus_ddc_addr_t ddc_addr);
+
+static int gmbus_wait_event_one(unsigned char *mmio, unsigned long bit);
+static int gmbus_wait_event_zero(unsigned char *mmio, unsigned long bit);
+static int gmbus_error_handler(unsigned char *mmio);
+
+/*!
+ * i2c_read_regs_plb is called to read Edid or a single sDVO register
+ *
+ * @param context
+ * @param i2c_bus port->ddc_reg, port->i2c_reg
+ * @param i2c_speed 50, 100, 400, 1000 (Khz)
+ * @param dab 0x70/0x72 (sDVO Regs), 0xA0/0xA2 (sDVO/Analog DDC)
+ * @param reg I2C Reg Index
+ * @param num_bytes <= 508
+ * @param buffer Data read
+ * @param flags unused, put in to match TNC func signature
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_read_regs_plb(igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags)
+{
+	unsigned char *mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+	unsigned long slave_addr;
+
+	if (! gmbus_init(mmio, i2c_bus, i2c_speed)) {
+		EMGD_DEBUG("Error ! i2c_read_regs_plb : gmbus_init() failed");
+		return 1;
+	}
+
+	/*	If the request is to read Edid from sDVO display, find out the */
+	/*	i2c addres of the sDVO device */
+	if (i2c_bus == GMBUS_DVOB_DDC) {
+		slave_addr = dvob_port_plb.dab;
+
+	} else {
+		slave_addr = 0;
+
+	}
+
+	switch (i2c_bus) {
+	case GMBUS_ANALOG_DDC :
+	case GMBUS_INT_LVDS_DDC :
+	case GMBUS_DVOB_DDC :
+	case GMBUS_DVOC_DDC :
+		if (! gmbus_read_edid(mmio, dab, slave_addr, reg, num_bytes, buffer)) {
+
+			EMGD_DEBUG("Error ! i2c_read_regs_plb : gmbus_read_edid() failed");
+			return 1;
+		}
+		break;
+
+	case GMBUS_DVO_REG :
+		if (! gmbus_read_reg(mmio, dab, reg, buffer)) {
+
+			EMGD_DEBUG("Error ! i2c_read_regs_plb : gmbus_read_reg() failed");
+			return 1;
+		}
+		break;
+
+	default :
+		EMGD_ERROR("Error ! i2c_read_regs_plb : Invalid i2c_bus=0x%lx",
+			i2c_bus);
+		return 1;
+	}
+
+	return 0;
+}
+
+/*!
+ * i2c_write_reg_list_plb is called to write a list of i2c registers to sDVO
+ * device
+ *
+ * @param context
+ * @param i2c_bus NAP_GMBUS_DVOB_DDC/NAP_GMBUS_DVOC_DDC
+ * @param i2c_speed 1000 Khz
+ * @param dab 0x70/0x72
+ * @param reg_list List of i2c indexes and data, terminated with register index
+ *  set to PD_REG_LIST_END
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_write_reg_list_plb(igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags)
+{
+	unsigned char *mmio = EMGD_MMIO(context->device_context.virt_mmadr);
+	unsigned long reg_num = 0, ddc_addr = 0, slave_addr = 0;
+
+	if (! gmbus_init(mmio, i2c_bus, i2c_speed)) {
+
+		EMGD_DEBUG("Error ! i2c_write_reg_list_plb : gmbus_init() failed");
+		return 1;
+	}
+	/*If it is SDVO Make sure we issue SDVO command to enable DDC access*/
+	if ((i2c_bus == GMBUS_DVOB_DDC) || (i2c_bus == GMBUS_DVOC_DDC)) {
+		if (i2c_bus == GMBUS_DVOB_DDC) {
+			slave_addr = dvob_port_plb.dab;
+		}else if (i2c_bus == GMBUS_DVOC_DDC) {
+			/* Is DVOC available in PLB? */
+			//slave_addr = dvoc_port_plb.dab;
+			slave_addr = 0;
+		}
+		ddc_addr = 0;
+		if (! gmbus_set_control_bus_switch(mmio, slave_addr, ddc_addr)) {
+			EMGD_DEBUG("Error ! i2c_write_reg_list_plb : gmbus_set_control_bus_switch()"
+					 " failed");
+			return 1;
+		}
+		while (reg_list[reg_num].reg != PD_REG_LIST_END) {
+
+			if (! gmbus_write_reg(mmio, dab, reg_list[reg_num].reg,
+					  (unsigned char)reg_list[reg_num].value)) {
+
+				EMGD_DEBUG("Error ! i2c_write_reg_list_plb : gmbus_write_reg() failed, reg_num=%lu",
+					reg_num);
+
+				return 1;
+			}
+		reg_num++;
+		}
+		/*...................................................................... */
+		/* Issue a Stop Command */
+		gmbus_wait_event_one(mmio, HW_WAIT);
+		WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | ddc_addr);
+		gmbus_wait_event_one(mmio, HW_RDY);
+		gmbus_wait_event_zero(mmio, GA);
+		gmbus_error_handler(mmio);
+		WRITE_GMCH_REG(GMBUS1, SW_RDY);
+		WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+		WRITE_GMCH_REG(GMBUS1, 0);
+		WRITE_GMCH_REG(GMBUS5, 0);
+		WRITE_GMCH_REG(GMBUS0, 0);
+		/*...................................................................... */
+		return 0;
+	}
+	while (reg_list[reg_num].reg != PD_REG_LIST_END) {
+
+		if (! gmbus_write_reg(mmio, dab, reg_list[reg_num].reg,
+				(unsigned char)reg_list[reg_num].value)) {
+
+			EMGD_DEBUG("Error ! i2c_write_reg_list_plb : gmbus_write_reg() failed, reg_num=%lu",
+				reg_num);
+
+			return 1;
+		}
+
+		reg_num++;
+	}
+
+	return 0;
+}
+
+/*!
+ * gmbus_init initializes the GMBUS controller with specified bus and speed
+ *
+ * @param mmio
+ * @param i2c_bus sDVO B/C Reg/DDC or Analog DDC
+ * @param i2c_speed 50/100/400/1000 Khz
+ *
+ * @return TRUE(1) on success
+ * @return FALSE(0) on failure
+ */
+static int gmbus_init(unsigned char *mmio, unsigned long i2c_bus,
+	unsigned long i2c_speed)
+{
+	gmbus_pins_pair_t pin_pair;
+	gmbus_speed_t bus_speed;
+
+	switch (i2c_bus) {
+
+	case GMBUS_DVO_REG :
+	case GMBUS_DVOB_DDC :
+	case GMBUS_DVOC_DDC :
+		pin_pair = GMBUS_PINS_SDVO;
+		break;
+
+	case GMBUS_ANALOG_DDC :
+		pin_pair = GMBUS_PINS_ANALOG;
+		break;
+
+	case GMBUS_INT_LVDS_DDC :
+		pin_pair = GMBUS_PINS_INT_LVDS;
+		break;
+
+	default :
+		EMGD_ERROR("Error ! gmbus_init : Invalid i2c_bus=0x%lx", i2c_bus);
+		return 0;
+	}
+
+	switch (i2c_speed) {
+
+	case 50 :		/* Slow speed */
+		bus_speed = GMBUS_SPEED_50K;
+		break;
+
+	case 400 :		/* SPD */
+		bus_speed = GMBUS_SPEED_400K;
+		break;
+
+	case 1000 :     /* sDVO Registers */
+		bus_speed = GMBUS_SPEED_1000K;
+		break;
+
+	case 100 :      /* DDC */
+	default :
+		bus_speed = GMBUS_SPEED_100K;
+		break;
+	}
+
+	WRITE_GMCH_REG(GMBUS5, 0);   /* Clear the word index reg */
+	WRITE_GMCH_REG(GMBUS0, pin_pair | bus_speed);
+
+	return 1;
+}
+
+/*!
+ * gmbus_wait_event_zero waits for specified GMBUS2 register bit to be deasserted
+ *
+ * @param mmio
+ * @param bit
+ *
+ * @return TRUE(1) on success. The bit was deasserted in the specified timeout period
+ * @return FALSE(0) on failure
+ */
+static int gmbus_wait_event_zero(unsigned char *mmio, unsigned long bit)
+{
+	unsigned long i;
+	unsigned long status;
+
+	for (i = 0; i < 0x1000; i++) {
+
+		status = READ_GMCH_REG(GMBUS2);
+
+		if ((status & bit) == 0) {
+
+			return 1;
+		}
+	}
+
+	EMGD_DEBUG("Error ! gmbus_wait_event_zero : Failed : bit=0x%lx, status=0x%lx, forcing reset",
+		bit, status);
+
+	/* If we are here, that means that the GBMUS is busy or in a bad
+	 * state, the situation was observed
+	 * that the GMBUS never becomes available (idle state) after S3.
+	 * This results in the driver never being able to set the display.
+	 *
+	 * To fix this, we force force a reset of the GMBUS.
+	 */
+	WRITE_GMCH_REG(GMBUS1, SW_RDY);
+	WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+	WRITE_GMCH_REG(GMBUS1, 0);
+
+	return 1;
+}
+
+/*!
+ * gmbus_wait_event_one wait for specified GMBUS2 register bits to be asserted
+ *
+ * @param mmio
+ * @param bit
+ *
+ * @return TRUE(1) on success. The bit was asserted in the specified timeout period
+ * @return FALSE(0) on failure
+ */
+static int gmbus_wait_event_one(unsigned char *mmio, unsigned long bit)
+{
+	unsigned long i;
+	unsigned long status;
+
+	for (i = 0; i < 0x10000; i++) {
+
+		status = READ_GMCH_REG(GMBUS2);
+		if ((status & bit) != 0) {
+
+			return 1;
+		}
+	}
+
+	EMGD_DEBUG("Error ! gmbus_wait_event_one : Failed : bit=0x%lx, status=0x%lx",
+		bit, status);
+
+	return 0;
+}
+
+/*!
+ * gmbus_error_handler attempts to recover from timeout error
+ *
+ * @param mmio
+ *
+ * @return TRUE(1) error was detected and handled
+ * @return FALSE(0) there was no error
+ */
+static int gmbus_error_handler(unsigned char *mmio)
+{
+	unsigned long status = READ_GMCH_REG(GMBUS2);
+
+	/* Clear the SW_INT, wait for HWRDY and GMBus active (GA) */
+	if ((status & HW_BUS_ERR) || (status & HW_TMOUT)) {
+
+		EMGD_DEBUG("Error ! gmbus_error_handler : Resolving error=0x%lx",
+			status);
+
+		WRITE_GMCH_REG(GMBUS1, SW_RDY);
+		WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+		WRITE_GMCH_REG(GMBUS1, 0);
+
+		gmbus_wait_event_zero(mmio, GA);
+
+		return 1;	/* Handled the error */
+	}
+
+	return 0;	/* There was no error */
+}
+
+/*!
+ * Assemble 32 bit GMBUS1 command
+ *
+ * @param slave_addr 0x70/0x72
+ * @param index 0 - 256
+ * @param num_bytes Bytes to transfer
+ * @param flags Bits 25-31 of GMBUS1
+ * @param i2c_dir I2C_READ / I2C_WRITE
+ *
+ * @return The assembled command
+ */
+static unsigned long gmbus_assemble_command(unsigned long slave_addr, unsigned long index,
+	unsigned long num_bytes, unsigned long flags,
+	i2c_bus_dir_t i2c_dir)
+{
+	unsigned long cmd = flags | ENIDX | ENT | (num_bytes << 16) | (index << 8) |
+						slave_addr | i2c_dir;
+
+	return cmd;
+}
+
+/*!
+ * gmbus_send_pkt transmits a block a data to specified i2c slave device
+ *
+ * @param mmio
+ * @param slave_addr I2C device address
+ * @param index Starting i2c register index
+ * @param pkt_size 1 - 508 bytes
+ * @param pkt Bytes to send
+ *
+ * @return TRUE(1) if successful in sending the specified number of bytes
+ * @return FALSE(0) on failure
+ */
+static int gmbus_send_pkt(unsigned char *mmio,
+	unsigned long slave_addr, unsigned long index,
+	unsigned long pkt_size, void *pkt)
+{
+	unsigned long gmbus1_cmd;
+	unsigned long bytes_sent;
+	unsigned long *data;
+
+	if ((pkt_size == 0) || (pkt == NULL) || (pkt_size > 508)) {
+
+		return 0;
+	}
+
+	data = (unsigned long *)pkt;
+
+	/*...................................................................... */
+	gmbus_error_handler(mmio);
+
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, pkt_size,
+										STA, I2C_WRITE);
+	if (pkt_size <= 4) {
+
+		gmbus1_cmd |= SW_RDY;
+	}
+
+	/*...................................................................... */
+	bytes_sent = 0;
+
+	do {
+
+		WRITE_GMCH_REG(GMBUS3, *data);
+
+		if (bytes_sent == 0) {
+
+			WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+		}
+
+		if (! gmbus_wait_event_one(mmio, HW_RDY)) {
+
+			EMGD_DEBUG("Error ! gmbus_send_pkt : Failed to get HW_RDY, bytes_sent=%ld",
+				bytes_sent);
+
+			return 0;
+		}
+
+		if (gmbus_error_handler(mmio)) {
+
+			EMGD_DEBUG("Error ! gmbus_send_pkt : gmbus error, bytes_sent=%ld",
+				bytes_sent);
+
+			return 0;
+		}
+
+		data++;
+
+		if (pkt_size >= 4) {
+			bytes_sent += 4;
+
+		} else {
+			bytes_sent += pkt_size;
+		}
+
+	} while (bytes_sent < pkt_size);
+
+	/*...................................................................... */
+	if (bytes_sent != pkt_size) {
+
+		return 0;
+
+	} else {
+
+		return 1;
+	}
+}
+
+/*!
+ * gmbus_recv_pkt reads a block of data from specified i2c slave device
+ *
+ * @param mmio
+ * @param slave_addr I2C device address
+ * @param index Starting i2c register index
+ * @param pkt_size 1 - 508 bytes
+ * @param pkt Bytes to send
+ *
+ * @return TRUE(1) if successful in receiving specified number of bytes
+ * @return FALSE(0) on failure
+ */
+static int gmbus_recv_pkt(unsigned char *mmio,
+	unsigned long slave_addr, unsigned long index,
+	unsigned long pkt_size, void FAR *pkt)
+{
+	unsigned long gmbus1_cmd;
+	unsigned long bytes_rcvd;
+	unsigned long FAR *data;
+
+	if ((pkt_size == 0) || (pkt == NULL) || (pkt_size > 508)) {
+
+		return 0;
+	}
+
+	data = (unsigned long FAR *)pkt;
+
+	/*...................................................................... */
+	gmbus_error_handler(mmio);
+
+	/* Program the command */
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, pkt_size,
+										STA | SW_RDY, I2C_READ);
+	WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+
+	/*...................................................................... */
+	bytes_rcvd = 0;
+	do {
+
+		unsigned long gmbus3_data;
+		unsigned long bytes_left = pkt_size - bytes_rcvd;
+
+		if (! gmbus_wait_event_one(mmio, HW_RDY)) {
+
+			EMGD_DEBUG("Error ! gmbus_recv_pkt : Failed to get HW_RDY, "
+				"bytes_rcvd=%ld", bytes_rcvd);
+			break;
+		}
+
+		if (gmbus_error_handler(mmio)) {
+
+			EMGD_DEBUG("Error ! gmbus_recv_pkt : gmbus error, bytes_rcvd=%ld",
+				bytes_rcvd);
+			break;
+		}
+
+		gmbus3_data = READ_GMCH_REG(GMBUS3);
+
+		switch (bytes_left) {
+
+		case 1 :
+			*(unsigned char *)data = (unsigned char)gmbus3_data;
+			break;
+
+		case 2 :
+			*(unsigned short *)data = (unsigned short)gmbus3_data;
+			break;
+
+		case 3 :
+		{
+			unsigned char *dest = (unsigned char *)data;
+			 unsigned char *src  = (unsigned char *)&(gmbus3_data);
+			dest[0] = src[0];
+			dest[1] = src[1];
+			dest[2] = src[2];
+
+			break;
+		}
+
+		default :	/* >= 4 */
+			*data = gmbus3_data;
+			break;
+		}
+
+		if (bytes_left > 4) {
+			bytes_rcvd += 4;
+			data++;
+
+		} else {
+			bytes_rcvd += bytes_left;
+
+		}
+
+	} while (bytes_rcvd < pkt_size);
+
+	/*...................................................................... */
+	if (bytes_rcvd < pkt_size) {
+		return 0;
+
+	} else {
+		return 1;
+	}
+}
+
+/*!
+ * gmbus_set_control_bus_switch sends sDVO command to switch i2c bus to read EDID
+ * or SPD data
+ *
+ * @param mmio
+ * @param slave_addr sDVO device address (0x70/0x72)
+ * @param ddc_addr DDC1_ADDR/DDC2_ADDR
+ *
+ * @return TRUE(1) if successful in sending the opcode
+ * @return FALSE(0) on failure
+ */
+static int gmbus_set_control_bus_switch(unsigned char *mmio,
+	unsigned long slave_addr,
+	gmbus_ddc_addr_t ddc_addr)
+{
+	unsigned char data;
+	sdvo_bus_switch_t bus_switch;
+	int retry;
+
+	bus_switch = SDVO_BUS_DDC1;
+
+	/*...................................................................... */
+	/*	Transmit the Arguments */
+	if (! gmbus_send_pkt(mmio, slave_addr, SDVO_INDEX_PARAM_1, 1, &bus_switch)) {
+
+		EMGD_DEBUG("Error ! gmbus_set_control_bus_switch : gmbus_send_pkt() failed");
+
+		return 0;
+	}
+
+	/*...................................................................... */
+	/* Generate I2C stop cycle */
+	gmbus_wait_event_one(mmio, HW_WAIT);
+	WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | slave_addr);
+	gmbus_wait_event_one(mmio, HW_RDY);
+	gmbus_wait_event_zero(mmio, GA);
+
+	/*...................................................................... */
+	/* Transmit the Opcode */
+	data = SDVO_OPCODE_BUS_SWITCH;
+	if (! gmbus_send_pkt(mmio, slave_addr, SDVO_INDEX_OPCODE, 1, &data)) {
+
+		EMGD_DEBUG("Error ! gmbus_set_control_bus_switch : gmbus_send_pkt(Opcode)"
+				 " failed");
+
+		return 0;
+	}
+
+	/*...................................................................... */
+	/* Read Status */
+	for (retry = 0; retry < 3; retry++) {
+		if (! gmbus_recv_pkt(mmio, slave_addr, SDVO_INDEX_STATUS, 1, &data)) {
+
+			continue;
+		}
+
+		if (data != SDVO_STATUS_PENDING) {
+
+			break;
+		}
+	}
+
+	/*...................................................................... */
+	/* Send Stop */
+	gmbus_wait_event_one(mmio, HW_WAIT);
+	WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | slave_addr);
+	gmbus_wait_event_one(mmio, HW_RDY);
+	gmbus_wait_event_zero(mmio, GA);
+
+	/*...................................................................... */
+	if (data != SDVO_STATUS_SUCCESS) {
+
+		EMGD_DEBUG("Error ! gmbus_set_control_bus_switch : Opcode Bus Switch failed");
+
+		return 0;
+	}
+
+	return 1;
+}
+
+/*!
+ * gmbus_read_edid reads specified number of Edid data bytes
+ *
+ * @param mmio
+ * @param ddc_addr 0xA0/0xA2 (DDC1/DDC2)
+ * @param slave_addr 0x70/0x72 (sDVOB, sDVOC), 0 Analog
+ * @param index i2c register index
+ * @param num_bytes <= 508
+ * @param buffer Edid data read from the display
+ *
+ * @return TRUE(1) if successful in reading Edid
+ * @return FALSE(0) on failure
+ */
+static int gmbus_read_edid(unsigned char *mmio,
+	unsigned long ddc_addr,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned long num_bytes,
+	unsigned char FAR *buffer)
+{
+	int status;
+
+	if ((slave_addr == SDVOB_ADDR) || (slave_addr == SDVOC_ADDR)) {
+
+		if (! gmbus_set_control_bus_switch(mmio, slave_addr, ddc_addr)) {
+
+			EMGD_DEBUG("Error ! gmbus_read_edid : gmbus_set_control_bus_switch()"
+					 " failed");
+
+			return 0;
+		}
+	} else {
+		/*	Reset the bus */
+		gmbus_recv_pkt(mmio, ddc_addr, 0, 1, buffer);
+	}
+
+	status = gmbus_recv_pkt(mmio, ddc_addr, index, num_bytes, buffer);
+	if (! status) {
+
+		EMGD_DEBUG("Error ! gmbus_read_edid : gmbus_recv_pkt() failed");
+	}
+
+	/*...................................................................... */
+	/* Issue a Stop Command */
+
+	gmbus_wait_event_one(mmio, HW_WAIT);
+	WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | ddc_addr);
+	gmbus_wait_event_one(mmio, HW_RDY);
+
+	gmbus_wait_event_zero(mmio, GA);
+
+	gmbus_error_handler(mmio);
+	WRITE_GMCH_REG(GMBUS1, SW_RDY);
+	WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+	WRITE_GMCH_REG(GMBUS1, 0);
+	WRITE_GMCH_REG(GMBUS5, 0);
+	WRITE_GMCH_REG(GMBUS0, 0);
+
+	/*...................................................................... */
+	return status;
+}
+
+/*!
+ * gmbus_read_reg reads one i2c register
+ *
+ * @param mmio
+ * @param slave_addr 0x70/0x72 (sDVOB, sDVOC)
+ * @param index i2c register index
+ * @param data register data
+ *
+ * @return TRUE(1) if successful in reading the i2c register
+ * @return FALSE(0) on failure
+ */
+static int gmbus_read_reg(unsigned char *mmio,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned char FAR *data)
+{
+	unsigned long gmbus1_cmd;
+
+	WRITE_GMCH_REG(GMBUS5, 0x0);		/* Clear Word Index register */
+
+	if (! gmbus_wait_event_zero(mmio, GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_read_reg : Failed to get GA(1)");
+
+		return 0;
+	}
+
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, 1,
+										STO | STA, I2C_READ);
+	WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+
+	if (! gmbus_wait_event_zero(mmio, GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_read_reg : Failed to get GA(2)");
+
+		return 0;
+	}
+
+	*data = (unsigned char)READ_GMCH_REG(GMBUS3);
+
+	return 1;
+}
+
+/*!
+ * gmbus_write_reg writes one i2c register
+ *
+ * @param mmio
+ * @param slave_addr 0x70/0x72 (sDVOB, sDVOC)
+ * @param index i2c register index
+ * @param data register data
+ *
+ * @return TRUE(1) if successful in updating the i2c register
+ * @return FALSE(0) if failed to update the register
+ */
+static int gmbus_write_reg(unsigned char *mmio,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned char data)
+{
+	unsigned long gmbus1_cmd;
+
+	WRITE_GMCH_REG(GMBUS5, 0x0);		/* Clear Word Index register */
+
+	if (! gmbus_wait_event_zero(mmio, GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_write_reg : Failed to get GA(1)");
+
+		return 0;
+	}
+
+	WRITE_GMCH_REG(GMBUS3, data);
+
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, 1,
+										STO | STA, I2C_WRITE);
+	WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+
+	if (! gmbus_wait_event_zero(mmio, GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_write_reg : Failed to get GA(2)");
+		return 0;
+	}
+
+	return 1;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_bitbash_tnc.c b/drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_bitbash_tnc.c
new file mode 100644
index 0000000..427aa29
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_bitbash_tnc.c
@@ -0,0 +1,599 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: i2c_bitbash_tnc.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Bitbash to read GPIO pins are done on LPC device 0:31:0. So do not
+ *  use EMGD_READ32 and EMGD_WRITE32 macros for read/write to device 31. These
+ *  macros do mmio only on device2.
+ *  To properly read/write mmio on device 31, use
+ *  READ_MMIO_REG_TNC(port_type, reg) and
+ *  WRITE_MMIO_REG_TNC(port_type, reg, data).
+ *  These macros properly set to work for all OSes including VBIOS, but
+ *  generate more code compared to EMGD_READ32 and EMGD_WRITE32. So use as
+ *  necessary.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd_pwr.h>
+
+#include <general.h>
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+
+#include <tnc/regs.h>
+
+#include <intelpci.h>
+
+#include "../cmn/i2c_dispatch.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+static int i2c_error_recovery_tnc(
+	unsigned long hold_time);
+
+static int i2c_write_byte_tnc(
+	unsigned char value,
+	unsigned long hold_time);
+
+static int i2c_read_byte_tnc(
+	unsigned char *value,
+	unsigned char ack,
+	unsigned long hold_time);
+
+int i2c_read_regs_gpio(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags);
+
+int i2c_write_reg_list_gpio(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags);
+
+/* The LVDS GPIO clock lines are GPIOSUS[3]
+ * The LVDS GPIO data lines are GPIOSUS[4]
+ */
+#define GPIO_CLOCK	0x08
+#define GPIO_DATA	0x10
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+#if 0
+static void enable_gpio_tnc(igd_context_t *context)
+{
+	/*
+	 * NOTE: This really should be a system BIOS job.
+	 * The driver would not touch these register anymore since
+	 * it would cause the 13x7 panel fail to start.
+	 */
+
+	/* Enabling LVDS Data and LVDS Clock */
+	unsigned long temp;
+
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGEN);
+	temp |= (GPIO_DATA | GPIO_CLOCK);
+	WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGEN, temp);
+
+	return;
+}
+#endif
+
+/*!
+ *
+ * @param clock
+ * @param data
+ *
+ * @return void
+ */
+static void i2c_get(unsigned long *clock,
+	unsigned long *data)
+{
+	unsigned long temp;
+	/* Set Data as Input */
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
+	temp |= (GPIO_DATA);
+	WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
+	/* Read Data */
+	*data = (READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL) & GPIO_DATA) ? 1:0;
+	*clock = (READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL) & GPIO_CLOCK) ? 1:0;
+
+#if 0
+	EMGD_WRITE32(0, EMGD_MMIO(mmio) + i2c_bus);
+	c = EMGD_READ32(EMGD_MMIO(mmio) + i2c_bus)>>4;
+	*data = (c>>8) & 1;
+	*clock &= 1;
+#endif
+}
+
+/*!
+ *
+ * @param data
+ * @param hold_time
+ *
+ * @return void
+ */
+static void i2c_set_data(int data,
+	unsigned long hold_time)
+{
+	unsigned long temp;
+	/* The LVDS GPIO data lines are GPIOSUS[4] */
+	/* Set as Output */
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
+	temp &= ~GPIO_DATA;
+	WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
+	/* Read status register */
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL);
+
+	if(data){
+		/* Set level to High */
+		temp |= GPIO_DATA;
+	} else {
+		/* Set level to low */
+		temp &= ~GPIO_DATA;
+	}
+	WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL, temp);
+
+	OS_DELAY(hold_time);
+
+#if 0
+	/* Implementation using Display GPIO
+	 * For alm, the default data value "could" be 0
+	 */
+	/*
+	 * Simplified definition for the bits
+	 * 11: GPIO data Value
+	 * 10: GPIO Data Mask
+	 * 9: GPIO Data Direction Value
+	 * 8: GPIO Data Direction Mask
+	 */
+	EMGD_WRITE32(data ? 0x500 : 0x700, EMGD_MMIO(mmio) + i2c_bus);
+	EMGD_WRITE32(data ? 0x400 : 0x600, EMGD_MMIO(mmio) + i2c_bus);
+	OS_DELAY(hold_time);
+#endif
+}
+
+/*!
+ *
+ * @param clock
+ * @param hold_time
+ *
+ * @return void
+ */
+static void i2c_set_clock(int clock,
+	unsigned long hold_time)
+{
+	unsigned long temp;
+	/* The LVDS GPIO clock lines are GPIOSUS[3] */
+	/* Set as Output */
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
+	temp &= ~GPIO_CLOCK;
+	WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
+	/* Read Status Register */
+	temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL);
+
+	if(clock){
+		/* Set level to High */
+		temp |= GPIO_CLOCK;
+
+	} else {
+		/* Set level to low */
+		temp &= ~GPIO_CLOCK;
+	}
+	WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL, temp);
+
+	OS_DELAY(hold_time);
+
+#if 0
+	/*
+	 * Simplified definition for the bits
+	 * 3: GPIO Clock Value
+	 * 2: GPIO Clock Mask
+	 * 1: GPIO Clock Direction Value
+	 * 0: GPIO Clock Direction Mask
+	 */
+
+	EMGD_WRITE32(clock ? 0x5 : 0x7, EMGD_MMIO(mmio) + i2c_bus);
+	EMGD_WRITE32(clock ? 0x4 : 0x6, EMGD_MMIO(mmio) + i2c_bus);
+	OS_DELAY(hold_time);
+#endif
+}
+
+/*!
+ *
+ * @param hold_time
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_start_tnc(unsigned long hold_time)
+{
+	unsigned long sc, sd;
+
+	/* set sd high */
+	i2c_set_data(1, hold_time);
+
+	/* set clock high */
+	i2c_set_clock(1, hold_time);
+
+	/* Start condition happens when sd goes high to low when sc is high */
+	i2c_get(&sc, &sd);
+
+	if( 0 == sc ) {
+		// Data must be high
+		i2c_error_recovery_tnc(hold_time);
+		return 1;
+	}
+
+	i2c_set_data(0, hold_time);
+	i2c_set_clock(0, hold_time);
+
+	return 0;
+} /* end i2c_start */
+
+/*!
+ *
+ * @param hold_time
+ *
+ * @return 0
+ */
+static int i2c_stop_tnc(unsigned long hold_time)
+{
+	/* Stop condition happens when sd goes low to high when sc is high */
+	unsigned long sc,sd;
+
+	i2c_set_clock(0, hold_time);
+	i2c_set_data(0, hold_time);
+
+	i2c_set_clock(1, hold_time);
+
+	i2c_get(&sc, &sd);
+	/* Try another time */
+	if (sc == 0) {
+		i2c_set_clock(1, hold_time);
+	}
+	i2c_set_data(1, hold_time);
+
+	return 0;
+} /* end i2c_stop */
+
+/*!
+ *
+ * @param hold_time
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_error_recovery_tnc(unsigned long hold_time)
+{
+	unsigned char max_retries = 9;
+	unsigned long sc, sd;
+
+	while (max_retries--) {
+		i2c_get(&sc, &sd);
+		if (sd == 1 && sc == 1) {
+			return 0;
+		} else {
+			i2c_stop_tnc(hold_time);
+		}
+	}
+	EMGD_ERROR("Cannot recover I2C error.");
+
+	return 1;
+}
+
+/*!
+ *
+ * @param value
+ * @param hold_time
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_write_byte_tnc(unsigned char value,
+	unsigned long hold_time)
+{
+	int i;
+	unsigned long sc,sd;
+
+	/* I2C_DEBUG("i2c_write_byte"); */
+	for(i=7; i>=0; i--) {
+		i2c_set_clock(0, hold_time);
+		i2c_set_data(value>>i & 1, hold_time);
+
+		i2c_set_clock(1, hold_time);
+	}
+
+	/* Get ACK */
+	i2c_set_clock(0, hold_time);
+	/* Set data low. Possible inteference in some lvds panel */
+	i2c_set_data(0, hold_time);
+	i2c_set_clock(1, hold_time);
+	OS_DELAY(hold_time);
+
+	i2c_get(&sc, &sd);
+
+	i2c_set_clock(0, hold_time);
+
+	if (sd != 0) {
+		EMGD_ERROR("No ACK for byte 0x%x", value);
+		i2c_error_recovery_tnc(hold_time);
+		return 1;
+	}
+
+	return 0;
+
+} /* end i2c_write_byte */
+
+/*!
+ *
+ * @param value
+ * @param ack
+ * @param hold_time
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_read_byte_tnc(unsigned char *value,
+	unsigned char ack,
+	unsigned long hold_time)
+{
+	int i;
+	unsigned long sc, sd, temp;
+
+	*value = 0;
+	for(i=7; i>=0; i--) {
+		i2c_set_clock(1, hold_time);
+		i2c_get(&sc, &sd);
+		OS_DELAY(hold_time);
+		if(!sc) {
+			EMGD_DEBUG("Clock low on read %d", i);
+			i2c_error_recovery_tnc(hold_time);
+			return 1;
+		}
+		*value |= (sd & 1)<<i;
+		i2c_set_clock(0, hold_time);
+	}
+
+	if (ack) {
+		i2c_set_data(0, hold_time);
+	}
+
+	/* Master does not ACK */
+	i2c_set_clock(1, hold_time);
+	i2c_set_clock(0, hold_time);
+
+	if (ack) {
+		/* Set data as input as we continue to read */
+		temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
+		temp |= GPIO_DATA;
+		WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
+	}
+
+	return 0;
+} /* end i2c_read_byte */
+
+/*!
+ *
+ * @param context
+ * @param i2c_bus
+ * @param i2c_speed
+ * @param dab
+ * @param reg
+ * @param buffer
+ * @param num_bytes
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int i2c_read_regs_gpio(igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags	
+	)
+{
+	unsigned long hold_time;
+	unsigned char temp;
+	int i;
+	if (!i2c_speed) {
+		EMGD_DEBUG("i2c Speed failed.");
+		return 1;
+	}
+
+	/*
+	 * We are holding the clock LOW for "hold_time" and then HIGH for
+	 * "hold_time". Therefore, we double the clock speed in this calculation.
+	 */
+	if (flags & IGD_I2C_WRITE_FW){
+		hold_time = 1;
+	} else {
+	hold_time = 1000/(i2c_speed * 2);
+	}
+
+	/* enable_gpio_tnc(context); */
+
+	if (i2c_start_tnc(hold_time)) {
+		EMGD_DEBUG("i2c Start failed.");
+		return 1;
+	}
+
+	if (i2c_write_byte_tnc((unsigned char)dab & 0xFE,
+			hold_time)) {
+		EMGD_DEBUG("i2c DAB(W) failed.");
+		return 1;
+	}
+
+	if (i2c_write_byte_tnc(reg, hold_time)) {
+		EMGD_DEBUG("RAB failed.");
+		return 1;
+	}
+
+	if (i2c_start_tnc(hold_time)) {
+		EMGD_DEBUG("i2c ReStart failed");
+		return 1;
+	}
+
+	if (i2c_write_byte_tnc((unsigned char)dab | 0x01,
+			hold_time)) {
+		EMGD_ERROR("i2c DAB(R) failed");
+		return 1;
+	}
+
+
+	/* Read the requested number of bytes */
+	for(i=0; i<(int)(num_bytes-1); i++) {
+		/*
+		 * Use a local temp so that the FAR pointer doesn't have to
+		 * get passed down.
+		 */
+		if (i2c_read_byte_tnc(&temp, 1, hold_time)) {
+			EMGD_DEBUG("Read data byte %d failed", i);
+			EMGD_DEBUG("Exit i2c_read_regs_tnc with error");
+			return 1;
+		}
+		buffer[i] = temp;
+	}
+
+	/* No ACK on the last read */
+	if(i2c_read_byte_tnc(&temp, 0, hold_time)) {
+		EMGD_DEBUG("Read Data %d Failed", i);
+		EMGD_DEBUG("Exit i2c_read_regs_tnc with error");
+		return 1;
+	}
+	buffer[i] = temp;
+
+	i2c_stop_tnc(hold_time);
+	i2c_stop_tnc(hold_time);
+
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ * @param i2c_bus
+ * @param i2c_speed
+ * @param dab
+ * @param reg_list
+ * @param flags
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int i2c_write_reg_list_gpio(igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags)
+{
+	unsigned long hold_time;
+
+	if (!i2c_speed) {
+		return 1;
+	}
+
+	/*
+	 * We are holding the clock LOW for "hold_time" and then HIGH for
+	 * "hold_time". Therefore, we double the clock speed in this calculation.
+	 */
+	if (flags & IGD_I2C_WRITE_FW){
+		hold_time = 1;	
+	} else {
+	hold_time = 1000/(i2c_speed * 2);
+	}
+
+	/* enable_gpio_tnc(context); */
+
+	while(reg_list->reg != PD_REG_LIST_END) {
+		if (i2c_start_tnc(hold_time)) {
+			EMGD_DEBUG("Start failed");
+			return 1;
+		}
+
+		if (i2c_write_byte_tnc((unsigned char)dab & 0xFE,
+				hold_time)) {
+			EMGD_DEBUG("DAB(W) failed");
+			return 1;
+		}
+
+		/* Register Address */
+		if (i2c_write_byte_tnc((unsigned char)reg_list->reg, hold_time)) {
+			EMGD_DEBUG("RAB failed");
+			return 1;
+		}
+
+		do {
+			/*  New Value */
+			if (i2c_write_byte_tnc((unsigned char)reg_list->value, hold_time)) {
+				EMGD_DEBUG("Data failed");
+				return 1;
+			}
+
+			if(reg_list[1].reg != (reg_list[0].reg + 1)) {
+				reg_list++;
+				break;
+			}
+
+			EMGD_DEBUG("I2C Multi-Write Reg[%x] = 0x%x",
+				(unsigned short)reg_list->reg,
+				(unsigned short)reg_list->value);
+			reg_list++;
+		} while(flags & IGD_I2C_SERIAL_WRITE);
+
+
+		i2c_stop_tnc(hold_time);
+		i2c_stop_tnc(hold_time);
+	}
+
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_gmbus_tnc.c b/drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_gmbus_tnc.c
new file mode 100644
index 0000000..ed8cbb9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/display/pi/tnc/i2c_gmbus_tnc.c
@@ -0,0 +1,929 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: i2c_gmbus_tnc.c
+ * $Revision: 1.15 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd_pwr.h>
+
+#include <general.h>
+#include <context.h>
+#include <mode.h>
+#include <utils.h>
+
+#include <tnc/regs.h>
+
+#include "../cmn/i2c_dispatch.h"
+
+/*!
+ * @addtogroup display_group
+ * @{
+ */
+
+/*......................................................................... */
+extern igd_display_port_t dvob_port_tnc;
+
+/*......................................................................... */
+static int i2c_read_regs_tnc(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags);
+
+static int i2c_write_reg_list_tnc(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags);
+
+i2c_dispatch_t i2c_dispatch_tnc = {
+	i2c_read_regs_tnc,
+	i2c_write_reg_list_tnc,
+};
+
+
+/*.......................................................................... */
+typedef enum {
+
+	GMBUS_SPEED_50K     = 0x0100,
+	GMBUS_SPEED_100K	= 0x0000,
+	GMBUS_SPEED_400K	= 0x0200,
+	GMBUS_SPEED_1000K	= 0x0300,
+
+} gmbus_speed_t;
+
+typedef enum {
+
+	SDVOB_ADDR	= 0x70,
+
+} sdvo_dev_addr_t;
+
+typedef enum {
+
+	DDC1_ADDR = 0xA0,
+	DDC2_ADDR = 0xA2,
+
+} gmbus_ddc_addr_t;
+
+
+typedef enum {
+
+	GMBUS_PINS_DEDICATED = 1, /* Dedicated Control/GMBUS Pins */
+	GMBUS_PINS_SDVO     = 5,  /* SDVO Registers, DDC, PROM */
+
+} gmbus_pins_pair_t;
+
+
+typedef enum {
+
+	I2C_WRITE = 0,
+	I2C_READ  = 1,
+
+} i2c_bus_dir_t;
+
+/*.......................................................................... */
+typedef enum {
+
+	SDVO_BUS_PROM = BIT(0),
+	SDVO_BUS_DDC1 = BIT(1),
+	SDVO_BUS_DDC2 = BIT(2),
+
+} sdvo_bus_switch_t;
+
+#define SDVO_OPCODE_BUS_SWITCH	0x7A
+
+#define SDVO_INDEX_PARAM_1		0x07
+#define SDVO_INDEX_OPCODE		0x08
+#define SDVO_INDEX_STATUS		0x09
+
+#define SDVO_STATUS_SUCCESS     0x01
+#define SDVO_STATUS_PENDING     0x04
+
+/*.......................................................................... */
+/*
+ * In 16-bit, the mmio is a 16-bit pointer, the watcom 1.2 compiler will have
+ * error if directly convert it to unsigned long.  Normally, have to cast it to
+ * unsigned short first then cast again to unsigned long; then, it will be
+ * correct.  But this type of casting may cause some error in the 32 and 64 bit
+ * code.  Since mmio will be equal to zero for 16-bit code.  Add the checking
+ * for MICRO definition code to correct the macro by remove mmio.
+ */
+#define READ_GMCH_REG(reg)        READ_MMIO_REG_TNC(IGD_PORT_SDVO, reg)
+#define WRITE_GMCH_REG(reg, data) WRITE_MMIO_REG_TNC(IGD_PORT_SDVO, reg, data)
+
+static int gmbus_init(unsigned long i2c_bus,
+	unsigned long i2c_speed);
+
+static int gmbus_read_edid(unsigned long ddc_addr,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned long num_bytes,
+	unsigned char FAR *buffer);
+
+static int gmbus_read_reg(unsigned long slave_addr,
+	unsigned long index,
+	unsigned char FAR *data);
+
+static int gmbus_write_reg(unsigned long slave_addr,
+	unsigned long index,
+	unsigned char data);
+
+static int gmbus_set_control_bus_switch(unsigned long slave_addr,
+	gmbus_ddc_addr_t ddc_addr);
+
+static int gmbus_wait_event_one(unsigned long bit);
+static int gmbus_wait_event_zero(unsigned long bit);
+static int gmbus_error_handler(void);
+
+/*.......................................................................... */
+extern int i2c_read_regs_gpio(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags);
+
+extern int i2c_write_reg_list_gpio(
+	igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags);
+
+
+/*!
+ * i2c_read_regs_tnc is called to read Edid or a single sDVO register
+ *
+ * @param context
+ * @param i2c_bus port->ddc_reg, port->i2c_reg
+ * @param i2c_speed 50, 100, 400, 1000 (Khz)
+ * @param dab 0x70/0x72 (sDVO Regs), 0xA0/0xA2 (sDVO/Analog DDC)
+ * @param reg I2C Reg Index
+ * @param num_bytes <= 508
+ * @param buffer Data read
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_read_regs_tnc(igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	unsigned char reg,
+	unsigned char FAR *buffer,
+	unsigned long num_bytes,
+	unsigned long flags)
+{
+	unsigned long slave_addr = 0;
+
+	if(i2c_bus == I2C_INT_LVDS_DDC){
+		/*
+		 * Atom E6xx LVDS does not have GMBUS support. To read DDC register, we bit bash.
+		 * The i2c_speed is necessary to calculate the hold time. But i2c_bus is not needed.
+		 */
+
+		EMGD_DEBUG("i2c_read_regs_tnc : Using GPIO to read DDC");
+		return i2c_read_regs_gpio(context, i2c_bus, i2c_speed,	dab, reg, buffer, num_bytes, flags);
+
+	} else {
+
+		if (! gmbus_init(i2c_bus, i2c_speed)) {
+			EMGD_DEBUG("Error ! i2c_read_regs_tnc : gmbus_init() failed");
+			return 1;
+		}
+
+		/*	If the request is to read Edid from sDVO display, find out the */
+		/*	i2c addres of the sDVO device */
+		if (i2c_bus == GMBUS_DVOB_DDC) {
+			slave_addr = dvob_port_tnc.dab;
+		}
+
+		switch (i2c_bus) {
+		case GMBUS_DVOB_DDC :
+			if (! gmbus_read_edid(dab, slave_addr, reg, num_bytes, buffer)) {
+
+				EMGD_DEBUG("Error ! i2c_read_regs_tnc : gmbus_read_edid() failed");
+				return 1;
+			}
+			break;
+
+		case GMBUS_DVO_REG :
+			if (! gmbus_read_reg(dab, reg, buffer)) {
+
+				EMGD_DEBUG("Error ! i2c_read_regs_tnc : gmbus_read_reg() failed");
+				return 1;
+			}
+			break;
+
+		default :
+			EMGD_ERROR("Error ! i2c_read_regs_tnc : Invalid i2c_bus=0x%lx",
+				i2c_bus);
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
+/*!
+ * i2c_write_reg_list_tnc is called to write a list of i2c registers to sDVO
+ * device
+ *
+ * @param context
+ * @param i2c_bus NAP_GMBUS_DVOB_DDC/NAP_GMBUS_DVOC_DDC
+ * @param i2c_speed 1000 Khz
+ * @param dab 0x70/0x72
+ * @param reg_list List of i2c indexes and data, terminated with register index
+ *  set to PD_REG_LIST_END
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+static int i2c_write_reg_list_tnc(igd_context_t *context,
+	unsigned long i2c_bus,
+	unsigned long i2c_speed,
+	unsigned long dab,
+	pd_reg_t *reg_list,
+	unsigned long flags)
+{
+	unsigned long reg_num = 0, ddc_addr = 0, slave_addr = 0;
+
+	if(i2c_bus == I2C_INT_LVDS_DDC){
+		/* There are no GMBUS pins for internal LVDS on Atom E6xx.
+		 * Forcing us to use bit bashing */
+		/* FIXME: determine which GPIO pin is used for bit bashing
+		 *		i2c_bus is not defined
+		 */
+		EMGD_DEBUG("i2c_write_reg_list_tnc : Using GPIO to write DDC");
+		return i2c_write_reg_list_gpio(context, i2c_bus, i2c_speed, dab, reg_list, flags);
+
+	} else {
+
+		if (! gmbus_init(i2c_bus, i2c_speed)) {
+
+			EMGD_DEBUG("Error ! i2c_write_reg_list_tnc : gmbus_init() failed");
+			return 1;
+		}
+		/*If it is SDVO Make sure we issue SDVO command to enable DDC access*/
+		if (i2c_bus == GMBUS_DVOB_DDC) {
+			slave_addr = dvob_port_tnc.dab;
+			ddc_addr = 0;
+
+			if (! gmbus_set_control_bus_switch(slave_addr, ddc_addr)) {
+				EMGD_DEBUG("Error ! i2c_write_reg_list_tnc : gmbus_set_control_bus_switch()"
+						 " failed");
+				return 1;
+			}
+			while (reg_list[reg_num].reg != PD_REG_LIST_END) {
+
+				if (! gmbus_write_reg(dab, reg_list[reg_num].reg,
+						  (unsigned char)reg_list[reg_num].value)) {
+
+					EMGD_DEBUG("Error ! i2c_write_reg_list_tnc : gmbus_write_reg() failed, reg_num=%lu",
+						reg_num);
+
+					return 1;
+				}
+			reg_num++;
+			}
+			/*...................................................................... */
+			/* Issue a Stop Command */
+			gmbus_wait_event_one(HW_WAIT);
+			WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | ddc_addr);
+			gmbus_wait_event_one(HW_RDY);
+			gmbus_wait_event_zero(GA);
+			gmbus_error_handler();
+			WRITE_GMCH_REG(GMBUS1, SW_RDY);
+			WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+			WRITE_GMCH_REG(GMBUS1, 0);
+			WRITE_GMCH_REG(GMBUS5, 0);
+			WRITE_GMCH_REG(GMBUS0, 0);
+			/*...................................................................... */
+			return 0;
+		}
+		while (reg_list[reg_num].reg != PD_REG_LIST_END) {
+
+			if (! gmbus_write_reg(dab, reg_list[reg_num].reg,
+					(unsigned char)reg_list[reg_num].value)) {
+
+				EMGD_DEBUG("Error ! i2c_write_reg_list_tnc : gmbus_write_reg() failed, reg_num=%lu",
+					reg_num);
+
+				return 1;
+			}
+
+			reg_num++;
+		}
+	}
+
+	return 0;
+}
+
+/*!
+ * gmbus_init initializes the GMBUS controller with specified bus and speed
+ *
+ * @param i2c_bus sDVO B/C Reg/DDC or Analog DDC
+ * @param i2c_speed 50/100/400/1000 Khz
+ *
+ * @return TRUE(1) on success
+ * @return FALSE(0) on failure
+ */
+static int gmbus_init(unsigned long i2c_bus,
+	unsigned long i2c_speed)
+{
+	gmbus_pins_pair_t pin_pair;
+	gmbus_speed_t bus_speed;
+
+	switch (i2c_bus) {
+
+	case GMBUS_DVO_REG :
+	case GMBUS_DVOB_DDC :
+		pin_pair = GMBUS_PINS_SDVO;
+		break;
+
+	default :
+		EMGD_ERROR("Error ! gmbus_init : Invalid i2c_bus=0x%lx", i2c_bus);
+		return 0;
+	}
+
+	switch (i2c_speed) {
+
+	case 50 :		/* Slow speed */
+		bus_speed = GMBUS_SPEED_50K;
+		break;
+
+	case 400 :		/* SPD */
+		bus_speed = GMBUS_SPEED_400K;
+		break;
+
+	case 1000 :     /* sDVO Registers */
+		//bus_speed = GMBUS_SPEED_1000K;
+		bus_speed = GMBUS_SPEED_400K;
+		break;
+
+	case 100 :      /* DDC */
+	default :
+		bus_speed = GMBUS_SPEED_100K;
+		break;
+	}
+
+	WRITE_GMCH_REG(GMBUS5, 0);   /* Clear the word index reg */
+	WRITE_GMCH_REG(GMBUS0, pin_pair | bus_speed);
+
+	return 1;
+}
+
+/*!
+ * gmbus_wait_event_zero waits for specified GMBUS2 register bit to be deasserted
+ *
+ * @param bit
+ *
+ * @return TRUE(1) on success. The bit was deasserted in the specified timeout period
+ * @return FALSE(0) on failure
+ */
+static int gmbus_wait_event_zero(unsigned long bit)
+{
+	unsigned long i;
+	unsigned long status;
+
+	for (i = 0; i < 0x1000; i++) {
+
+		status = READ_GMCH_REG(GMBUS2);
+
+		if ((status & bit) == 0) {
+
+			return 1;
+		}
+	}
+
+	EMGD_DEBUG("Error ! gmbus_wait_event_zero : Failed : bit=0x%lx, status=0x%lx",
+		bit, status);
+
+	return 0;
+}
+
+/*!
+ * gmbus_wait_event_one wait for specified GMBUS2 register bits to be asserted
+ *
+ * @param bit
+ *
+ * @return TRUE(1) on success. The bit was asserted in the specified timeout period
+ * @return FALSE(0) on failure
+ */
+static int gmbus_wait_event_one(unsigned long bit)
+{
+	unsigned long i;
+	unsigned long status;
+
+	for (i = 0; i < 0x10000; i++) {
+
+		status = READ_GMCH_REG(GMBUS2);
+		if ((status & bit) != 0) {
+
+			return 1;
+		}
+	}
+
+	EMGD_DEBUG("Error ! gmbus_wait_event_one : Failed : bit=0x%lx, status=0x%lx",
+		bit, status);
+
+	return 0;
+}
+
+/*!
+ * gmbus_error_handler attempts to recover from timeout error
+ *
+ *
+ * @return TRUE(1) error was detected and handled
+ * @return FALSE(0) there was no error
+ */
+static int gmbus_error_handler(void)
+{
+	unsigned long status = READ_GMCH_REG(GMBUS2);
+
+	/* Clear the SW_INT, wait for HWRDY and GMBus active (GA) */
+	if ((status & HW_BUS_ERR) || (status & HW_TMOUT)) {
+
+		EMGD_DEBUG("Error ! gmbus_error_handler : Resolving error=0x%lx",
+			status);
+
+		WRITE_GMCH_REG(GMBUS1, SW_RDY);
+		WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+		WRITE_GMCH_REG(GMBUS1, 0);
+
+		gmbus_wait_event_zero(GA);
+
+		return 1;	/* Handled the error */
+	}
+
+	return 0;	/* There was no error */
+}
+
+/*!
+ * Assemble 32 bit GMBUS1 command
+ *
+ * @param slave_addr 0x70/0x72
+ * @param index 0 - 256
+ * @param num_bytes Bytes to transfer
+ * @param flags Bits 25-31 of GMBUS1
+ * @param i2c_dir I2C_READ / I2C_WRITE
+ *
+ * @return The assembled command
+ */
+static unsigned long gmbus_assemble_command(unsigned long slave_addr, unsigned long index,
+	unsigned long num_bytes, unsigned long flags,
+	i2c_bus_dir_t i2c_dir)
+{
+	unsigned long cmd = flags | ENIDX | ENT | (num_bytes << 16) | (index << 8) |
+						slave_addr | i2c_dir;
+
+	return cmd;
+}
+
+/*!
+ * gmbus_send_pkt transmits a block a data to specified i2c slave device
+ *
+ * @param slave_addr I2C device address
+ * @param index Starting i2c register index
+ * @param pkt_size 1 - 508 bytes
+ * @param pkt Bytes to send
+ *
+ * @return TRUE(1) if successful in sending the specified number of bytes
+ * @return FALSE(0) on failure
+ */
+static int gmbus_send_pkt(unsigned long slave_addr, unsigned long index,
+	unsigned long pkt_size, void *pkt)
+{
+	unsigned long gmbus1_cmd;
+	unsigned long bytes_sent;
+	unsigned long *data;
+
+	if ((pkt_size == 0) || (pkt == NULL) || (pkt_size > 508)) {
+
+		return 0;
+	}
+
+	data = (unsigned long *)pkt;
+
+	/*...................................................................... */
+	gmbus_error_handler();
+
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, pkt_size,
+										STA, I2C_WRITE);
+	if (pkt_size <= 4) {
+
+		gmbus1_cmd |= SW_RDY;
+	}
+
+	/*...................................................................... */
+	bytes_sent = 0;
+
+	do {
+
+		WRITE_GMCH_REG(GMBUS3, *data);
+
+		if (bytes_sent == 0) {
+
+			WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+		}
+
+		if (! gmbus_wait_event_one(HW_RDY)) {
+
+			EMGD_DEBUG("Error ! gmbus_send_pkt : Failed to get HW_RDY, bytes_sent=%ld",
+				bytes_sent);
+
+			return 0;
+		}
+
+		if (gmbus_error_handler()) {
+
+			EMGD_DEBUG("Error ! gmbus_send_pkt : gmbus error, bytes_sent=%ld",
+				bytes_sent);
+
+			return 0;
+		}
+
+		data++;
+
+		if (pkt_size >= 4) {
+			bytes_sent += 4;
+
+		} else {
+			bytes_sent += pkt_size;
+		}
+
+	} while (bytes_sent < pkt_size);
+
+	/*...................................................................... */
+	if (bytes_sent != pkt_size) {
+
+		return 0;
+
+	} else {
+
+		return 1;
+	}
+}
+
+/*!
+ * gmbus_recv_pkt reads a block of data from specified i2c slave device
+ *
+ * @param slave_addr I2C device address
+ * @param index Starting i2c register index
+ * @param pkt_size 1 - 508 bytes
+ * @param pkt Bytes to send
+ *
+ * @return TRUE(1) if successful in receiving specified number of bytes
+ * @return FALSE(0) on failure
+ */
+static int gmbus_recv_pkt(unsigned long slave_addr, unsigned long index,
+	unsigned long pkt_size, void FAR *pkt)
+{
+	unsigned long gmbus1_cmd;
+	unsigned long bytes_rcvd;
+	unsigned long FAR *data;
+
+	if ((pkt_size == 0) || (pkt == NULL) || (pkt_size > 508)) {
+
+		return 0;
+	}
+
+	data = (unsigned long FAR *)pkt;
+
+	/*...................................................................... */
+	gmbus_error_handler();
+
+	/* Program the command */
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, pkt_size,
+										STA | SW_RDY, I2C_READ);
+	WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+
+	/*...................................................................... */
+	bytes_rcvd = 0;
+	do {
+
+		unsigned long gmbus3_data;
+		unsigned long bytes_left = pkt_size - bytes_rcvd;
+
+		if (! gmbus_wait_event_one(HW_RDY)) {
+
+			EMGD_DEBUG("Error ! gmbus_recv_pkt : Failed to get HW_RDY, "
+				"bytes_rcvd=%ld", bytes_rcvd);
+			break;
+		}
+
+		if (gmbus_error_handler()) {
+
+			EMGD_DEBUG("Error ! gmbus_recv_pkt : gmbus error, bytes_rcvd=%ld",
+				bytes_rcvd);
+			break;
+		}
+
+		gmbus3_data = READ_GMCH_REG(GMBUS3);
+
+		switch (bytes_left) {
+
+		case 1 :
+			*(unsigned char *)data = (unsigned char)gmbus3_data;
+			break;
+
+		case 2 :
+			*(unsigned short *)data = (unsigned short)gmbus3_data;
+			break;
+
+		case 3 :
+		{
+			unsigned char *dest = (unsigned char *)data;
+			 unsigned char *src  = (unsigned char *)&(gmbus3_data);
+			dest[0] = src[0];
+			dest[1] = src[1];
+			dest[2] = src[2];
+
+			break;
+		}
+
+		default :	/* >= 4 */
+			*data = gmbus3_data;
+			break;
+		}
+
+		if (bytes_left > 4) {
+			bytes_rcvd += 4;
+			data++;
+
+		} else {
+			bytes_rcvd += bytes_left;
+
+		}
+
+	} while (bytes_rcvd < pkt_size);
+
+	/*...................................................................... */
+	if (bytes_rcvd < pkt_size) {
+		return 0;
+
+	} else {
+		return 1;
+	}
+}
+
+/*!
+ * gmbus_set_control_bus_switch sends sDVO command to switch i2c bus to read EDID
+ * or SPD data
+ *
+ * @param slave_addr sDVO device address (0x70/0x72)
+ * @param ddc_addr DDC1_ADDR/DDC2_ADDR
+ *
+ * @return TRUE(1) if successful in sending the opcode
+ * @return FALSE(0) on failure
+ */
+static int gmbus_set_control_bus_switch(unsigned long slave_addr,
+	gmbus_ddc_addr_t ddc_addr)
+{
+	unsigned char data;
+	sdvo_bus_switch_t bus_switch;
+	int retry;
+
+	bus_switch = SDVO_BUS_DDC1;
+
+	/*...................................................................... */
+	/*	Transmit the Arguments */
+	if (! gmbus_send_pkt(slave_addr, SDVO_INDEX_PARAM_1, 1, &bus_switch)) {
+
+		EMGD_DEBUG("Error ! gmbus_set_control_bus_switch : gmbus_send_pkt() failed");
+
+		return 0;
+	}
+
+	/*...................................................................... */
+	/* Generate I2C stop cycle */
+	gmbus_wait_event_one(HW_WAIT);
+	WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | slave_addr);
+	gmbus_wait_event_one(HW_RDY);
+	gmbus_wait_event_zero(GA);
+
+	/*...................................................................... */
+	/* Transmit the Opcode */
+	data = SDVO_OPCODE_BUS_SWITCH;
+	if (! gmbus_send_pkt(slave_addr, SDVO_INDEX_OPCODE, 1, &data)) {
+
+		EMGD_DEBUG("Error ! gmbus_set_control_bus_switch : gmbus_send_pkt(Opcode)"
+				 " failed");
+
+		return 0;
+	}
+
+	/*...................................................................... */
+	/* Read Status */
+	for (retry = 0; retry < 3; retry++) {
+		if (! gmbus_recv_pkt(slave_addr, SDVO_INDEX_STATUS, 1, &data)) {
+
+			continue;
+		}
+
+		if (data != SDVO_STATUS_PENDING) {
+
+			break;
+		}
+	}
+
+	/*...................................................................... */
+	/* Send Stop */
+	gmbus_wait_event_one(HW_WAIT);
+	WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | slave_addr);
+	gmbus_wait_event_one(HW_RDY);
+	gmbus_wait_event_zero(GA);
+
+	/*...................................................................... */
+	if (data != SDVO_STATUS_SUCCESS) {
+
+		EMGD_DEBUG("Error ! gmbus_set_control_bus_switch : Opcode Bus Switch failed");
+
+		return 0;
+	}
+
+	return 1;
+}
+
+/*!
+ * gmbus_read_edid reads specified number of Edid data bytes
+ *
+ * @param ddc_addr 0xA0/0xA2 (DDC1/DDC2)
+ * @param slave_addr 0x70/0x72 (sDVOB, sDVOC), 0 Analog
+ * @param index i2c register index
+ * @param num_bytes <= 508
+ * @param buffer Edid data read from the display
+ *
+ * @return TRUE(1) if successful in reading Edid
+ * @return FALSE(0) on failure
+ */
+static int gmbus_read_edid(unsigned long ddc_addr,
+	unsigned long slave_addr,
+	unsigned long index,
+	unsigned long num_bytes,
+	unsigned char FAR *buffer)
+{
+	int status;
+
+	if (slave_addr == SDVOB_ADDR) {
+
+		if (! gmbus_set_control_bus_switch(slave_addr, ddc_addr)) {
+
+			EMGD_DEBUG("Error ! gmbus_read_edid : gmbus_set_control_bus_switch()"
+					 " failed");
+
+			return 0;
+		}
+	} else {
+		/*	Reset the bus */
+		gmbus_recv_pkt(ddc_addr, 0, 1, buffer);
+	}
+
+	status = gmbus_recv_pkt(ddc_addr, index, num_bytes, buffer);
+	if (! status) {
+
+		EMGD_DEBUG("Error ! gmbus_read_edid : gmbus_recv_pkt() failed");
+	}
+
+	/*...................................................................... */
+	/* Issue a Stop Command */
+
+	gmbus_wait_event_one(HW_WAIT);
+	WRITE_GMCH_REG(GMBUS1, STO | SW_RDY | ddc_addr);
+	gmbus_wait_event_one(HW_RDY);
+
+	gmbus_wait_event_zero(GA);
+
+	gmbus_error_handler();
+	WRITE_GMCH_REG(GMBUS1, SW_RDY);
+	WRITE_GMCH_REG(GMBUS1, SW_CLR_INT);
+	WRITE_GMCH_REG(GMBUS1, 0);
+	WRITE_GMCH_REG(GMBUS5, 0);
+	WRITE_GMCH_REG(GMBUS0, 0);
+
+	/*...................................................................... */
+	return status;
+}
+
+/*!
+ * gmbus_read_reg reads one i2c register
+ *
+ * @param slave_addr 0x70/0x72 (sDVOB, sDVOC)
+ * @param index i2c register index
+ * @param data register data
+ *
+ * @return TRUE(1) if successful in reading the i2c register
+ * @return FALSE(0) on failure
+ */
+static int gmbus_read_reg(unsigned long slave_addr,
+	unsigned long index,
+	unsigned char FAR *data)
+{
+	unsigned long gmbus1_cmd;
+
+	WRITE_GMCH_REG(GMBUS5, 0x0);		/* Clear Word Index register */
+
+	if (! gmbus_wait_event_zero(GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_read_reg : Failed to get GA(1)");
+
+		return 0;
+	}
+
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, 1,
+										STO | STA, I2C_READ);
+	WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+
+	if (! gmbus_wait_event_zero(GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_read_reg : Failed to get GA(2)");
+
+		return 0;
+	}
+
+	*data = (unsigned char)READ_GMCH_REG(GMBUS3);
+
+	return 1;
+}
+
+/*!
+ * gmbus_write_reg writes one i2c register
+ *
+ * @param slave_addr 0x70/0x72 (sDVOB, sDVOC)
+ * @param index i2c register index
+ * @param data register data
+ *
+ * @return TRUE(1) if successful in updating the i2c register
+ * @return FALSE(0) if failed to update the register
+ */
+static int gmbus_write_reg(unsigned long slave_addr,
+	unsigned long index,
+	unsigned char data)
+{
+	unsigned long gmbus1_cmd;
+
+	WRITE_GMCH_REG(GMBUS5, 0x0);		/* Clear Word Index register */
+
+	if (! gmbus_wait_event_zero(GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_write_reg : Failed to get GA(1)");
+
+		return 0;
+	}
+
+	WRITE_GMCH_REG(GMBUS3, data);
+
+	gmbus1_cmd = gmbus_assemble_command(slave_addr, index, 1,
+										STO | STA, I2C_WRITE);
+	WRITE_GMCH_REG(GMBUS1, gmbus1_cmd);
+
+	if (! gmbus_wait_event_zero(GA)) {
+
+		EMGD_DEBUG("Error ! gmbus_write_reg : Failed to get GA(2)");
+		return 0;
+	}
+
+	return 1;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/drm/drm_emgd_private.h b/drivers/gpu/drm/emgd/emgd/drm/drm_emgd_private.h
new file mode 100644
index 0000000..d6d6819
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/drm_emgd_private.h
@@ -0,0 +1,176 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: drm_emgd_private.h
+ * $Revision: 1.22 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the EMGD-specific drm_device.dev_private structure,
+ *  which is used to share data with the EMGD-specific, IMG 3rd-Party Display
+ *  Driver (3DD, implimented in the
+ *  "egd_drm/pvr/services4/3rdparty/emgd_displayclass" directory).
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _DRM_EMGD_PRIVATE_H_
+#define _DRM_EMGD_PRIVATE_H_
+
+#include <drm/drmP.h>
+#include <drm/drm.h>
+#include <drm/drm_fb_helper.h>
+#include "mode.h"
+#include "context.h"
+#include "emgd_drv.h"
+
+struct _drm_emgd_private;
+
+
+/* FIXME: This define should not be here.  Find a better place to put it */
+#define PSB_GATT_RESOURCE   2
+
+/**
+ * This enum is used to record the currently-saved register state (e.g. for VT
+ * switching)--either the Linux console's state is saved (i.e. the X server is
+ * active), or the X server's state is saved (i.e. the Linux console is
+ * active).
+ */
+typedef enum _drm_emgd_saved_state {
+	CONSOLE_STATE_SAVED = 0,
+	X_SERVER_STATE_SAVED = 1,
+} drm_emgd_saved_state;
+
+
+/**
+ * This structure allows the EMGD-proper code to communicate information and
+ * function pointers to the IMG 3rd-Party Display Driver (mrstlfb).
+ */
+typedef struct _drm_emgd_private {
+	/** Non-zero if the HAL is running */
+	int hal_running;
+
+	/**
+	 * Which register state is currently saved for VT switches (X or the
+	 * console; note: the opposite is what's actively installed in the
+	 * hardware)
+	 */
+	drm_emgd_saved_state saved_registers;
+
+	/**
+	 * Saved state of the console, when suspending (or hibernating) the system.
+	 * emgd_driver_suspend() allocates this, and emgd_driver_resume() frees
+	 * this.
+	 */
+	void *suspended_state;
+
+	/**
+	 * A flag which emgd_driver_pre_init() sets and emgd_alter_displays()
+	 * clears.  If set (i.e. to 1), this indicates that emgd_alter_displays()
+	 * must power on the port drivers' hardware.
+	 */
+	int must_power_on_ports;
+
+	/** Non-zero if the X server is running (i.e. PVR can't do mode changes) */
+	int xserver_running;
+
+	/**
+	 * Function to re-initialize the 3DD's data structures, after calls to
+	 * alter_displays()
+	 */
+	int (*reinit_3dd)(struct drm_device *dev);
+
+	/**
+	 * Function to invalidate the flip-chains assosciated with a display (or all
+	 * displays). Called from igd_alter_displays before a possible mode_change
+	 */
+	int (*invalidate_flip_chains)(int display);
+
+
+	/** The context is set during the DRM module load function. */
+	igd_context_t *context;
+
+	/* The selected DC is copied to here each time alter_displays() is called
+	 * via an ioctl.
+	 */
+	unsigned long dc;
+
+	/**
+	 * The port number of the primary display handle is copied to here each
+	 * time alter_displays() is called via an ioctl.
+	 */
+	unsigned short primary_port_number;
+
+	/**
+	 * The primary display handle is copied to here each time alter_displays()
+	 * is called via an ioctl.
+	 */
+	igd_display_h primary;
+
+	/**
+	 * The port number of the secondary display handle is copied to here each
+	 * time alter_displays() is called via an ioctl.
+	 */
+	unsigned short secondary_port_number;
+
+	/**
+	 * The secondary display handle is copied to here each time alter_displays()
+	 * is called via an ioctl.
+	 */
+	igd_display_h secondary;
+
+	/**
+	 * Store the device information so it can be passed back to userspace
+	 * callers via an ioctl.
+	 */
+	igd_init_info_t *init_info;
+
+	/** TODO:  May use this in future.
+	 * MSVDX
+	 */
+	void *msvdx_private;
+
+	/*
+	 * GTT offset of initial framebuffer.  The initial framebuffer is a special
+	 * case in that it is allocated directly by the GMM at system startup
+	 * before the PVR services are initialized.  As such, it has no PVR
+	 * meminfo that can be used as a handle to refer to it; functions that take
+	 * a framebuffer will treat a handle of 0 as referring to the initial
+	 * framebuffer and then pull the GTT offset from here.
+	 */
+	//unsigned long initfb_offset;
+	igd_framebuffer_info_t initfb_info;
+
+	/* fbdev is removed from drm_framebuffer in 2.6.35 so access it here */
+	struct fb_info    *fbdev;
+	emgd_crtc_t       *crtcs[IGD_MAX_PIPES];
+	int                num_crtc;
+	emgd_fbdev_t      *emgd_fbdev;
+	bool               mode_config_initialized;
+	struct drm_device *ddev;
+	bool               kms_enabled;
+	int qb_seamless; /* Store the state of seamless/quickboot */
+
+	/* The fd of the drm master - used to send vblank events to userspace */
+	struct drm_file   *drm_master_fd;  
+} drm_emgd_priv_t;
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_connector.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_connector.c
new file mode 100644
index 0000000..280f839
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_connector.c
@@ -0,0 +1,512 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_connector.c
+ * $Revision: 1.2 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Connector / kenrel mode setting functions.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.oal
+
+
+#include <drmP.h>
+#include <drm_crtc_helper.h>
+#include <linux/version.h>
+
+
+#include "drm_emgd_private.h"
+
+
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
+static enum drm_connector_status emgd_connector_detect(
+									struct drm_connector *connector,
+									bool force);
+#else
+static enum drm_connector_status emgd_connector_detect(
+									struct drm_connector *connector);
+#endif
+
+static int  emgd_connector_set_property(struct drm_connector *connector,
+				struct drm_property *property,
+	   			uint64_t value);
+static void emgd_connector_destroy(struct drm_connector *connector);
+static int  emgd_connector_get_modes(struct drm_connector *connector);
+static int  emgd_connector_mode_valid(struct drm_connector *connector,
+				struct drm_display_mode *mode);
+static struct drm_encoder *emgd_connector_best_encoder(
+								struct drm_connector *connector);
+static void emgd_connector_save (struct drm_connector *connector);
+static void emgd_connector_restore (struct drm_connector *connector);
+
+
+
+const struct drm_connector_funcs emgd_connector_funcs = {
+	.save         = emgd_connector_save,
+	.restore      = emgd_connector_restore,
+	.dpms         = drm_helper_connector_dpms,
+	.detect       = emgd_connector_detect,
+	.fill_modes   = drm_helper_probe_single_connector_modes,
+	.set_property = emgd_connector_set_property,
+	.destroy      = emgd_connector_destroy,
+};
+
+const struct drm_connector_helper_funcs emgd_connector_helper_funcs = {
+	.get_modes    = emgd_connector_get_modes,
+	.mode_valid   = emgd_connector_mode_valid,
+	.best_encoder = emgd_connector_best_encoder,
+};
+
+
+
+/**
+ * emgd_mode_to_kms
+ *
+ * Converts an EMGD mode to a DRM KMS mode
+ *
+ * @param emgd_mode (IN)  emgd_mode timing information
+ * @param drm_mode  (OUT) DRM mode
+ */
+static void emgd_mode_to_kms(igd_display_info_t *emgd_mode,
+				struct drm_display_mode *drm_mode)
+{
+	drm_mode->status      = MODE_OK;
+	drm_mode->type        = DRM_MODE_TYPE_DRIVER;
+	drm_mode->clock       = emgd_mode->dclk;
+	drm_mode->hdisplay    = emgd_mode->width;
+	drm_mode->hsync_start = emgd_mode->hsync_start;
+	drm_mode->hsync_end   = emgd_mode->hsync_end;
+	drm_mode->htotal      = emgd_mode->htotal;
+	drm_mode->vdisplay    = emgd_mode->height;
+	drm_mode->vsync_start = emgd_mode->vsync_start;
+	drm_mode->vsync_end   = emgd_mode->vsync_end;
+	drm_mode->vtotal      = emgd_mode->vtotal;
+	drm_mode->flags       = 0;
+	drm_mode->vrefresh    = emgd_mode->refresh;
+
+	drm_mode_set_name(drm_mode);
+}
+
+
+
+/**
+ * emgd_connector_save
+ *
+ * Saves a connector state before a power event.
+ *
+ * @param encoder (IN) Encoder
+ * @param mode    (IN) power mode
+ *
+ * @return None
+ */
+static void emgd_connector_save (struct drm_connector *connector)
+{
+#if 0
+	emgd_connector_t   *emgd_connector;
+	emgd_encoder_t     *emgd_encoder;
+	igd_display_port_t *igd_port;
+
+
+	/* Currently there is no need to do anything in this function because
+	 * CRTC save calls igd_driver_save() which eventually calls mode_save().
+	 * Inside of mode_save() is where encoder state is saved.  In order for
+	 * this function to work properly, we need to take the pd_save() call
+	 * out of mode_save().  Let's wait until Phase 2 to do this.
+	 */
+
+	EMGD_TRACE_ENTER;
+
+	emgd_connector = container_of(connector, emgd_connector_t, base);
+	emgd_encoder   = emgd_connector->encoder;
+	igd_port       = emgd_encoder->igd_port;
+
+	igd_port->pd_driver->pd_save(igd_port->pd_context,
+							&emgd_encoder->state.state, 0);
+
+	EMGD_TRACE_EXIT;
+#else
+	EMGD_TRACE_ENTER;
+	EMGD_TRACE_EXIT;
+#endif
+}
+
+
+
+/**
+ * emgd_connector_restore
+ *
+ * Restores a connector state after a power event
+ *
+ * @param encoder (IN) Encoder
+ * @param mode    (IN) power mode
+ *
+ * @return None
+ */
+static void emgd_connector_restore (struct drm_connector *connector)
+{
+#if 0
+	emgd_connector_t   *emgd_connector;
+	emgd_encoder_t     *emgd_encoder;
+	igd_display_port_t *igd_port;
+
+
+	/* Currently there is no need to do anything in this function because
+	 * CRTC save calls igd_driver_save() which eventually calls mode_save().
+	 * Inside of mode_save() is where encoder state is saved.  In order for
+	 * this function to work properly, we need to take the pd_save() call
+	 * out of mode_save().  Let's wait until Phase 2 to do this.
+	 */
+
+	EMGD_TRACE_ENTER;
+
+	emgd_connector = container_of(connector, emgd_connector_t, base);
+	emgd_encoder   = emgd_connector->encoder;
+	igd_port       = emgd_encoder->igd_port;
+
+	igd_port->pd_driver->pd_restore(igd_port->pd_context,
+							emgd_encoder->state.state, 0);
+	emgd_encoder->state.state = NULL;
+
+	EMGD_TRACE_EXIT;
+#else
+	EMGD_TRACE_ENTER;
+	EMGD_TRACE_EXIT;
+#endif
+}
+
+
+
+/**
+ * emgd_connector_detect
+ *
+ * Checks to see if a display device is attached to the connector.  EMGD
+ * does not currently support hot-plug.
+ *
+ * FIXME: The prototype for this function seemed to change sometime
+ * around the 2.6.35 timeframe however, different distributions
+ * cherrypicked it earlier.
+ *
+ * Fedora 14's 2.6.35.11 kernel has the patch (needs bool force)
+ * MeeGo's 2.6.35.10 kernel doesn't.
+ *
+ * @param encoder (IN) Encoder
+ * @param mode    (IN) power mode
+ *
+ * @return None
+ */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
+
+static enum drm_connector_status emgd_connector_detect(
+		struct drm_connector *connector, bool force) {
+#else
+static enum drm_connector_status emgd_connector_detect(
+		struct drm_connector *connector) {
+#endif
+	emgd_connector_t          *emgd_connector;
+	enum drm_connector_status  connector_status;
+	igd_display_port_t        *igd_port;
+	pd_port_status_t           port_status;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_connector = container_of(connector, emgd_connector_t, base);
+	igd_port       = emgd_connector->encoder->igd_port;
+
+	memset(&port_status, 0, sizeof(pd_port_status_t));
+
+	/* Get current status from the port driver */
+	igd_port->pd_driver->pd_get_port_status(igd_port->pd_context, &port_status);
+
+	switch (port_status.connected) {
+		case PD_DISP_STATUS_ATTACHED:
+			connector_status = connector_status_connected;
+			break;
+
+		case PD_DISP_STATUS_DETACHED:
+			connector_status = connector_status_disconnected;
+			break;
+
+		case PD_DISP_STATUS_UNKNOWN:
+		default:
+			/*
+             * Technically "unknown" is correct here, but that isn't actually
+			 * what we want to pass back to userspace via KMS.  LVDS panels
+			 * always have unknown connection status, so they'll always be
+			 * ignored by userspace apps that only operate on connected outputs.
+			 * If the driver is configured to use a port, then we should just
+			 * assume that its actually connected when we report back to
+			 * userspace.
+			 */
+
+			connector_status = connector_status_connected;
+			break;
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return connector_status;
+}
+
+
+
+/**
+ * emgd_connector_set_property
+ *
+ * Sets a port attribute.
+ *
+ * @param connector (IN) connector
+ * @param property  (IN)
+ * @param value     (IN)
+ *
+ * @return TBD
+ */
+static int emgd_connector_set_property(struct drm_connector *connector,
+			struct drm_property *property,
+			uint64_t value)
+{
+	emgd_connector_t   *emgd_connector;
+	drm_emgd_priv_t    *priv;
+	int                 ret;
+	igd_attr_t		   *attributes, selected_attr;
+	unsigned short      port_number;
+	unsigned long       num_of_attributes, i;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Set the property value to the new one.  This doesn't actually change
+     * anything on the HW. */
+	ret = drm_connector_property_set_value(connector, property, value);
+	if (ret) {
+		return ret;
+	}
+
+	/* Take care of the HW changes associated with the value change */
+	emgd_connector = container_of(connector, emgd_connector_t, base);
+	port_number    = emgd_connector->encoder->igd_port->port_number;
+	priv           = emgd_connector->priv;
+
+	/* Search port attributes to find the one associated with the property */
+	ret = priv->context->dispatch.get_attrs(priv->context,
+									port_number,
+									&num_of_attributes,
+									&attributes);
+
+	if (ret) {
+		return ret;
+	}
+
+
+	for (i = 0; i < num_of_attributes; i++) {
+
+		if (attributes[i].id == property->values[0]) {
+			/* Copy the content of the attribute */
+			memcpy(&selected_attr, &attributes[i], sizeof(igd_attr_t));
+
+			switch (selected_attr.type) {
+				case PD_ATTR_TYPE_RANGE:
+				case PD_ATTR_TYPE_BOOL:
+					selected_attr.current_value = (unsigned long) value;
+					ret = priv->context->dispatch.set_attrs(priv->context,
+													port_number,
+													1, /* Setting 1 attribute */
+													&selected_attr);
+					break;
+
+				case PD_ATTR_TYPE_LIST:
+				case PD_ATTR_TYPE_LIST_ENTRY:
+				case PD_ATTR_TYPE_BUFFER:
+				default:
+					EMGD_DEBUG("Unsupported PD Attribute type");
+					continue;
+			}
+
+			break;
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+
+
+/**
+ * emgd_connector_destroy
+ *
+ * Cleans up the emgd_connector object.
+ *
+ * @param connector (IN) connector to clean up
+ *
+ * @return None
+ */
+static void emgd_connector_destroy(struct drm_connector *connector)
+{
+	struct drm_device *dev = connector->dev;
+	emgd_connector_t  *emgd_connector;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("connector=0x%lx",(unsigned long)connector);
+
+	emgd_connector = container_of(connector, emgd_connector_t, base);
+
+	drm_sysfs_connector_remove(connector);
+	drm_connector_cleanup(connector);
+
+	/* Remove the properties */
+	if (emgd_connector->properties) {
+		int i;
+
+		for(i = 0; i < emgd_connector->num_of_properties; i++) {
+			drm_property_destroy(dev, emgd_connector->properties[i]);
+		}
+
+		kfree(emgd_connector->properties);
+		emgd_connector->properties = NULL;
+	}
+
+	kfree(emgd_connector);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_connector_get_modes
+ *
+ * Get the list of supported modes for the given connector
+ *
+ * @param connector (IN) connector to query.
+ *
+ * @return None
+ */
+static int emgd_connector_get_modes(struct drm_connector *connector)
+{
+        emgd_connector_t        *emgd_connector;
+        igd_display_port_t      *igd_port;
+        unsigned long            i;
+        struct drm_display_mode *drm_mode;
+
+
+        EMGD_TRACE_ENTER;
+
+        emgd_connector = container_of(connector, emgd_connector_t, base);
+        igd_port       = emgd_connector->encoder->igd_port;
+
+        EMGD_DEBUG("[EMGD] emgd_connector_get_modes for port %ld\n",
+                igd_port->port_number);
+
+        for (i = 0; i < igd_port->num_timing; i++) {
+                drm_mode = drm_mode_create(emgd_connector->priv->ddev);
+
+                emgd_mode_to_kms((igd_display_info_t *)&igd_port->timing_table[i],
+                        drm_mode);
+
+                /* Add current mode to the connector */
+                drm_mode_probed_add(connector, drm_mode);
+        }
+
+
+        EMGD_TRACE_EXIT;
+
+        return igd_port->num_timing;
+}
+
+
+
+/**
+ * emgd_connector_mode_valid
+ *
+ * Examines the mode given and see if the connector can support it.
+ * Note:  the ModeStatus enum is defined in xorg/hw/xfree86/common/xf86str.h
+ *
+ * @param connector (IN) the connector to be analyzed.
+ * @param mode      (IN) mode to check
+ *
+ * @return MODE_OK if supported, other ModeStatus enum if not
+ */
+static int emgd_connector_mode_valid(struct drm_connector *connector,
+                struct drm_display_mode *mode)
+{
+        emgd_connector_t *emgd_connector;
+
+        EMGD_TRACE_ENTER;
+
+        emgd_connector = container_of(connector, emgd_connector_t, base);
+
+
+        /* Basic checks */
+        if ((0 >= mode->hdisplay) || (mode->hdisplay > mode->hsync_start) ||
+                (mode->hsync_start >= mode->hsync_end) ||
+                (mode->hsync_end >= mode->htotal)) {
+                return MODE_H_ILLEGAL;
+        }
+
+        if ((0 >= mode->vdisplay) || (mode->vdisplay > mode->vsync_start) ||
+                (mode->vsync_start >= mode->vsync_end) ||
+                (mode->vsync_end >= mode->vtotal)) {
+                return MODE_V_ILLEGAL;
+        }
+
+
+        /* We can do some basic checks here, but it is better to call
+     * match_mode() in the display/mode module because that function does
+     * a much more thorough check */
+
+
+
+        EMGD_TRACE_EXIT;
+
+        return MODE_OK;
+}
+
+
+
+/**
+ * emgd_connector_best_encoder
+ *
+ * Returns the best encoder for the given connector.  In EMGD the connector is
+ * fixed to the encoder.
+ *
+ * @param connector (IN) the connector to be analyzed.
+ *
+ * @return Encoder onto which the connector is fixed on.
+ */
+static struct drm_encoder *emgd_connector_best_encoder(
+                struct drm_connector *connector)
+{
+        emgd_connector_t *emgd_connector;
+
+        EMGD_TRACE_ENTER;
+
+        emgd_connector = container_of(connector, emgd_connector_t, base);
+
+        EMGD_TRACE_EXIT;
+
+        return &emgd_connector->encoder->base;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_crtc.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_crtc.c
new file mode 100644
index 0000000..76e0235
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_crtc.c
@@ -0,0 +1,997 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_crtc.c
+ * $Revision: 1.4 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  CRTC / kernel mode setting functions.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.oal
+
+
+#include <drmP.h>
+#include <drm_crtc_helper.h>
+#include <linux/version.h>
+
+
+#include "drm_emgd_private.h"
+
+#include <memory.h>
+#include <mode_dispatch.h>
+#include <igd_pwr.h>
+
+/* Necessary to cursor memory from PVR buffer */
+#include "pvr_bridge_km.h"
+
+/* Maximum cursor size supported by our HAL: 64x64 in ARGB */
+#define MAX_CURSOR_SIZE (64*64*4)
+
+static void emgd_crtc_dpms(struct drm_crtc *crtc, int mode);
+static bool emgd_crtc_mode_fixup(struct drm_crtc *crtc,
+		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode);
+static int emgd_crtc_mode_set(struct drm_crtc *crtc,
+		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode,
+		int x, int y, struct drm_framebuffer *old_fb);
+static int emgd_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
+		struct drm_framebuffer *old_fb);
+static void emgd_crtc_prepare(struct drm_crtc *crtc);
+static void emgd_crtc_commit(struct drm_crtc *crtc);
+
+static void emgd_crtc_save(struct drm_crtc *crtc);
+static void emgd_crtc_restore(struct drm_crtc *crtc);
+static int emgd_crtc_cursor_set(struct drm_crtc *crtc,
+		struct drm_file *file_priv, uint32_t handle,
+		uint32_t width, uint32_t height);
+static int emgd_crtc_cursor_move(struct drm_crtc *crtc, int x, int y);
+static void emgd_crtc_gamma_set(struct drm_crtc *crtc,
+		unsigned short *red, unsigned short *green, unsigned short *blue,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
+		uint32_t start,
+#endif
+		uint32_t size);
+static void emgd_crtc_destroy(struct drm_crtc *crtc);
+static void emgd_crtc_load_lut(struct drm_crtc *crtc);
+static int emgd_crtc_page_flip(struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_pending_vblank_event *event);
+static int emgd_crtc_set_config(struct drm_mode_set *set);
+
+
+
+const struct drm_crtc_helper_funcs emgd_crtc_helper_funcs = {
+	.dpms          = emgd_crtc_dpms,
+	.mode_fixup    = emgd_crtc_mode_fixup,
+	.mode_set      = emgd_crtc_mode_set,
+	.mode_set_base = emgd_crtc_mode_set_base,
+	.prepare       = emgd_crtc_prepare,
+	.commit        = emgd_crtc_commit,
+	.load_lut      = emgd_crtc_load_lut,
+};
+
+const struct drm_crtc_funcs emgd_crtc_funcs = {
+	.save        = emgd_crtc_save,
+	.restore     = emgd_crtc_restore,
+	.cursor_set  = emgd_crtc_cursor_set,
+	.cursor_move = emgd_crtc_cursor_move,
+	.gamma_set   = emgd_crtc_gamma_set,
+	.set_config  = emgd_crtc_set_config,
+	.destroy     = emgd_crtc_destroy,
+	.page_flip   = emgd_crtc_page_flip,
+};
+
+
+
+static int emgd_crtc_set_config(struct drm_mode_set *set)
+{
+	int ret;
+	struct drm_device  *dev;
+	struct drm_encoder *encoder;
+	struct drm_encoder_helper_funcs *encoder_funcs;
+
+	EMGD_TRACE_ENTER;
+
+	ret = drm_crtc_helper_set_config(set);
+
+	if (ret) {
+		EMGD_ERROR_EXIT("Failed to set config\n");
+		return ret;
+	}
+
+	/*  FIXME: This is a temporary fix for the resume problem:  Screen goes
+	 *  blank after an idling period, then doesn't come back.  At this point
+	 *  it is unclear why KMS is not calling our DPMS functions to re-enable
+	 *  the display.  KMS does call this function on resume, so we will
+	 *  enable the displays here
+	 */
+	emgd_crtc_dpms(set->crtc, DRM_MODE_DPMS_ON);
+
+	dev = set->crtc->dev;
+
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		if (encoder->crtc == set->crtc) {
+			encoder_funcs = encoder->helper_private;
+			encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+/*
+ * Sets the power management mode of the pipe.
+ */
+static void emgd_crtc_dpms(struct drm_crtc *crtc, int mode)
+{
+	emgd_crtc_t           *emgd_crtc = NULL;
+	igd_display_pipe_t    *pipe = NULL;
+
+	EMGD_TRACE_ENTER;
+
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	EMGD_DEBUG("pipe=%d, mode=%d", emgd_crtc->crtc_id, mode);
+	pipe = emgd_crtc->igd_pipe;
+
+
+	switch(mode) {
+	case DRM_MODE_DPMS_ON:
+		EMGD_DEBUG("Checking if we have pipe timings");
+		if (!pipe->timing) {
+			/* If there is no pipe timing, we cannot enable */
+			EMGD_ERROR("No pipe timing, can't enable pipe");
+		} else {
+			EMGD_DEBUG("Calling program pipe");
+			mode_context->kms_dispatch->kms_program_pipe(emgd_crtc);
+
+			EMGD_DEBUG("Calling program plane");
+			mode_context->kms_dispatch->kms_set_plane_pwr(emgd_crtc, TRUE);
+
+			crtc->enabled = true;
+		}
+		break;
+
+	case DRM_MODE_DPMS_STANDBY:
+	case DRM_MODE_DPMS_SUSPEND:
+	case DRM_MODE_DPMS_OFF:
+		if (emgd_crtc->igd_pipe->inuse && crtc->enabled) {
+			EMGD_DEBUG("Calling program plane");
+			mode_context->kms_dispatch->kms_set_plane_pwr(emgd_crtc, FALSE);
+
+			EMGD_DEBUG("Calling program pipe");
+			mode_context->kms_dispatch->kms_set_pipe_pwr(emgd_crtc, FALSE);
+			crtc->enabled = false;
+		} else {
+			EMGD_ERROR("pipe is already off");
+		}
+		break;
+	default:
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+static bool emgd_crtc_mode_fixup(struct drm_crtc *crtc,
+				struct drm_display_mode *mode,
+				struct drm_display_mode *adjusted_mode)
+{
+	EMGD_TRACE_ENTER;
+
+	/* Check ajusted mode to see if it's valid.  If not, populate it */
+	if (adjusted_mode->crtc_htotal == 0) {
+		drm_mode_set_crtcinfo(adjusted_mode, 0);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 1;
+}
+
+
+
+/**
+ * emgd_crtc_mode_set
+ *
+ * Sets mode for the selected CRTC.  This function only sets the timings
+ * into the CRTC, but doesn't actually program the timing values into the
+ * registers.  The actual programming is done in emgd_crtc_commit.
+ *
+ * @param crtc   (IN) CRTC to configure
+ * @param x      (IN) starting X position in the frame buffer
+ * @param y      (IN) starting Y position in the frame buffer
+ * @param old_fb (IN) Not used
+ *
+ * @return 0
+ */
+static int emgd_crtc_mode_set(struct drm_crtc *crtc,
+		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode,
+		int x, int y, struct drm_framebuffer *old_fb)
+{
+	emgd_crtc_t            *emgd_crtc = NULL;
+	struct drm_device      *dev = NULL;
+	igd_context_t          *context = NULL;
+	igd_display_pipe_t     *pipe = NULL;
+	igd_timing_info_t      *timing = NULL;
+
+	EMGD_TRACE_ENTER;
+
+
+	dev = crtc->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+	/* Which pipe are we using */
+	EMGD_DEBUG("Getting PIPE");
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+	EMGD_DEBUG("\t\tpipe=%d, ->(%dx%d@%d)", emgd_crtc->crtc_id,
+		adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay,
+		adjusted_mode->vrefresh);
+
+	pipe  = emgd_crtc->igd_pipe;
+
+
+	if (old_fb) {
+		EMGD_DEBUG("Handling old framebuffer?");
+		/* What do we do with the old framebuffer? */
+	}
+
+	timing = kzalloc(sizeof(igd_timing_info_t), GFP_KERNEL);
+	if (!timing) {
+		EMGD_ERROR_EXIT("unable to allocate a igd_timing_info struct.");
+		return 1;
+	}
+
+	timing->width = adjusted_mode->crtc_hdisplay;
+	timing->height = adjusted_mode->crtc_vdisplay;
+	timing->refresh = adjusted_mode->vrefresh;
+	timing->dclk = adjusted_mode->synth_clock; /* Is this the right variable? */
+	timing->htotal = adjusted_mode->crtc_htotal;
+	timing->hblank_start = adjusted_mode->crtc_hblank_start;
+	timing->hblank_end = adjusted_mode->crtc_hblank_end;
+	timing->hsync_start = adjusted_mode->crtc_hsync_start;
+	timing->hsync_end = adjusted_mode->crtc_hsync_end;
+	timing->vtotal = adjusted_mode->crtc_vtotal;
+	timing->vblank_start = adjusted_mode->crtc_vblank_start;
+	timing->vblank_end = adjusted_mode->crtc_vblank_end;
+	timing->vsync_start = adjusted_mode->crtc_vsync_start;
+	timing->vsync_end = adjusted_mode->crtc_vsync_end;
+	timing->mode_number = adjusted_mode->clock_index;
+	timing->mode_info_flags = adjusted_mode->private_flags;
+	timing->x_offset = x;
+	timing->y_offset = y;
+	timing->mode_info_flags |= IGD_DISPLAY_ENABLE;
+
+	if (pipe->timing) {
+		OS_MEMCPY(pipe->timing, timing, sizeof(igd_timing_info_t));
+		kfree(timing);
+	} else {
+		pipe->timing = timing;
+	}
+
+	/* The code above only sets the CRTC timing, not the plane */
+	emgd_crtc_mode_set_base(crtc, x, y, old_fb);
+
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/**
+ * emgd_crtc_mode_set_base
+ *
+ * Sets the starting position in the framebuffer for the given CRTC.
+ *
+ * @param crtc   (IN) CRTC to configure
+ * @param x      (IN) starting X position in the frame buffer
+ * @param y      (IN) starting Y position in the frame buffer
+ * @param old_fb (IN) Not used
+ *
+ * @return 0
+ */
+static int emgd_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
+		struct drm_framebuffer *old_fb)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_display_context_t *display = NULL;
+	emgd_framebuffer_t *emgd_fb;
+	igd_framebuffer_info_t *plane_fb_info;
+	struct drm_framebuffer *fb = NULL;
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	fb        = crtc->fb;
+	emgd_fb   = container_of(fb, emgd_framebuffer_t, base);
+	display   = emgd_crtc->igd_pipe->owner;
+
+
+	if (old_fb) {
+		/* We should not need to do anything here. */
+	}
+
+	plane_fb_info = PLANE(display)->fb_info;
+
+	plane_fb_info->width          = fb->width;
+	plane_fb_info->height         = fb->height;
+	plane_fb_info->screen_pitch   = fb->pitch;
+	plane_fb_info->flags          = 0;
+	plane_fb_info->allocated      = 1;
+	plane_fb_info->fb_base_offset = emgd_fb->gtt_offset;
+	plane_fb_info->visible_offset = (y * fb->pitch) +
+		(x * (fb->bits_per_pixel / 8));
+
+
+	PLANE(display)->inuse = 1;
+	PLANE(display)->ref_cnt++;
+
+	mode_context->kms_dispatch->kms_program_plane(emgd_crtc, TRUE);
+
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+static void emgd_crtc_prepare(struct drm_crtc *crtc)
+{
+	EMGD_TRACE_ENTER;
+	emgd_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
+	EMGD_TRACE_EXIT;
+}
+
+static void emgd_crtc_commit(struct drm_crtc *crtc)
+{
+	EMGD_TRACE_ENTER;
+	emgd_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+static void emgd_crtc_save(struct drm_crtc *crtc)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_context_t *context = NULL;
+	unsigned long save_flags;
+
+	/* We should probably break out register save and restore
+	 * so that its specific to a crtc
+	 */
+	EMGD_TRACE_ENTER;
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	context = ((drm_emgd_priv_t *)crtc->dev->dev_private)->context;
+	EMGD_DEBUG("\t\tpipe=%d", emgd_crtc->crtc_id);
+
+	save_flags = (IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT & ~IGD_REG_SAVE_RB) |
+		IGD_REG_SAVE_TYPE_MISC;
+	context->dispatch.driver_save((igd_driver_h)context, save_flags);
+
+	EMGD_TRACE_EXIT;
+}
+
+static void emgd_crtc_restore(struct drm_crtc *crtc)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_context_t *context = NULL;
+	unsigned long restore_flags;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	context = ((drm_emgd_priv_t *)crtc->dev->dev_private)->context;
+	EMGD_DEBUG("\t\tpipe=%d", emgd_crtc->crtc_id);
+
+	restore_flags = IGD_REG_SAVE_TYPE_MISC;
+	context->dispatch.driver_restore((igd_driver_h)context, restore_flags);
+
+	EMGD_TRACE_EXIT;
+}
+
+static int emgd_crtc_cursor_set(struct drm_crtc *crtc,
+		struct drm_file *file_priv, uint32_t handle,
+		uint32_t width, uint32_t height)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_context_t *context = NULL;
+	igd_display_context_t *display = NULL;
+	igd_cursor_info_t *cursor_info = NULL;
+	PVRSRV_PER_PROCESS_DATA *pvr_perproc;
+	PVRSRV_KERNEL_MEM_INFO *pvr_meminfo;
+	PVRSRV_ERROR ret;
+	unsigned char *tempcurs;
+	unsigned long pid;
+	struct page **pagelist;
+	unsigned long numpages, page_offset, cursor_size = 0;
+	void *pageaddr;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	if (!emgd_crtc->igd_pipe->inuse) {
+		EMGD_ERROR("\t\tpipe %d is not available", emgd_crtc->crtc_id);
+		return 1;
+	}
+	context = ((drm_emgd_priv_t *)crtc->dev->dev_private)->context;
+	display = emgd_crtc->igd_pipe->owner;
+
+	if (!handle) {
+		/* If handle is 0, turn off the cursor */
+		EMGD_DEBUG("Turning off cursor");
+		mode_context->dispatch->full->program_cursor(display, FALSE);
+	} else {
+		/* Fetch PVR services 'per-process' data structure */
+		pid = OSGetCurrentProcessIDKM();
+		pvr_perproc = PVRSRVPerProcessData(pid);
+
+		/* Look up the kernel-side meminfo for the handle passed in*/
+		ret = PVRSRVLookupHandle(pvr_perproc->psHandleBase,
+				(void**)&pvr_meminfo,
+			(IMG_HANDLE)handle, PVRSRV_HANDLE_TYPE_MEM_INFO);
+		if (ret != PVRSRV_OK) {
+			EMGD_ERROR("Buffer handle is not a valid PVR surface.");
+			return -EINVAL;
+		}
+
+		/*
+		 * Now fetch the page list, number of pages, and offset into the first
+		 * page for this buffer.
+		 */
+		ret = PVRSRVGetPageListKM(pvr_meminfo, &pagelist, &numpages,
+				&page_offset);
+		if (ret != PVRSRV_OK) {
+			EMGD_ERROR("Failed to get pagelist for PVR surface.");
+			return -EINVAL;
+		}
+
+		/* Allocate a temporary buffer to hold the cursor image */
+		tempcurs = OS_ALLOC(MAX_CURSOR_SIZE);
+		if (!tempcurs) {
+			return -ENOMEM;
+		}
+
+		/* Map the page list into kernel virtual address space */
+		pageaddr = vmap(pagelist, numpages, VM_MAP, PAGE_KERNEL_UC_MINUS);
+
+		/*
+		 * Our HAL only accepts 64x64 x 4byte cursors.  Only copy the first
+		 * 64x64x4 of the provided buffer if the user tries to pass us
+		 * something too big.
+		 */
+		if (numpages * PAGE_SIZE > MAX_CURSOR_SIZE) {
+			cursor_size = MAX_CURSOR_SIZE;
+		} else {
+			cursor_size = numpages * PAGE_SIZE;
+		}
+
+		OS_MEMCPY(tempcurs, pageaddr, cursor_size);
+
+		vunmap(pageaddr);
+
+		/* Pass the cursor image to the HAL to program the cursor plane */
+		cursor_info = emgd_crtc->igd_pipe->cursor->cursor_info;
+		cursor_info->flags = IGD_CURSOR_LOAD_ARGB_IMAGE | IGD_CURSOR_ON;
+		cursor_info->width = width;
+		cursor_info->height = height;
+		context->dispatch.alter_cursor(display, cursor_info, tempcurs);
+
+		/* Free temporary cursor image */
+		OS_FREE(tempcurs);
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+static int emgd_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_context_t *context = NULL;
+	igd_display_context_t *display = NULL;
+	igd_cursor_info_t *emgd_cursor;
+
+	/* Too spammy; commenting out */
+#if 0
+	EMGD_TRACE_ENTER;
+#endif
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	if (!emgd_crtc->igd_pipe->inuse) {
+		EMGD_ERROR("\t\tpipe %d is not available", emgd_crtc->crtc_id);
+		return 1;
+	}
+	context = ((drm_emgd_priv_t *)crtc->dev->dev_private)->context;
+	display = emgd_crtc->igd_pipe->owner;
+
+    emgd_cursor = emgd_crtc->igd_pipe->cursor->cursor_info;
+	emgd_cursor->x_offset = x;
+	emgd_cursor->y_offset = y;
+	context->dispatch.alter_cursor_pos(display, emgd_cursor);
+
+	/* Too spammy; commenting out */
+#if 0
+	EMGD_TRACE_EXIT;
+#endif
+	return 0;
+}
+
+static void emgd_crtc_gamma_set(struct drm_crtc *crtc,
+		unsigned short *red, unsigned short *green, unsigned short *blue,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
+		uint32_t start,
+#endif
+		uint32_t size)
+{
+	int end, i;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
+	int start = 0;
+#endif
+	emgd_crtc_t *emgd_crtc = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	end = (start + size > 256) ? 256 : start + size;
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+	for (i=start; i < end; i++) {
+		emgd_crtc->lut_r[i] = red[i] >> 8;
+		emgd_crtc->lut_g[i] = green[i] >> 8;
+		emgd_crtc->lut_b[i] = blue[i] >> 8;
+	}
+
+	emgd_crtc_load_lut(crtc);
+
+	EMGD_TRACE_EXIT;
+}
+
+static void emgd_crtc_destroy(struct drm_crtc *crtc)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_context_t *context = NULL;
+	igd_display_pipe_t *igd_pipe = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	igd_pipe = emgd_crtc->igd_pipe;
+	if (!igd_pipe) {
+		EMGD_ERROR("\t\tpipe %d is not available", emgd_crtc->crtc_id);
+		return;
+	}
+	context = ((drm_emgd_priv_t *)crtc->dev->dev_private)->context;
+
+	EMGD_DEBUG("\t\tpipe=%d", emgd_crtc->crtc_id);
+
+	drm_crtc_cleanup(crtc);
+
+	/* Free our private crtc structure */
+	kfree(emgd_crtc);
+
+	igd_pipe->inuse = 0;
+	igd_pipe->plane = NULL;
+	igd_pipe->timing = NULL;
+	igd_pipe->owner = NULL;
+
+	EMGD_TRACE_EXIT;
+}
+
+static void emgd_crtc_load_lut(struct drm_crtc *crtc)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	igd_context_t *context = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+	if (!emgd_crtc->igd_pipe) {
+		EMGD_ERROR("\t\tpipe %d is not available", emgd_crtc->crtc_id);
+		return;
+	}
+	context = ((drm_emgd_priv_t *)crtc->dev->dev_private)->context;
+	EMGD_DEBUG("\t\tpipe=%d", emgd_crtc->crtc_id);
+	/* Call into hal function to set color map. */
+	context->mod_dispatch.reg_crtc_lut_set(context, emgd_crtc);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+/*
+ * crtc_pageflip_handler()
+ *
+ * VBlank handler to be called when a pageflip is complete.  This will send
+ * the vblank event to userspace.
+ *
+ * State upon entry (assuming vblank_expected is set):
+ *  * newfb is non-NULL
+ *  * flip_event is non-NULL
+ *  * flip_work_queued is FALSE
+ *  * vblank_expected is TRUE (based on assumption)
+ *
+ * State upon exit (assuming entered with vblank_expected):
+ *  * newfb is NULL
+ *  * flip_event is NULL
+ *  * flip_work_queued is FALSE
+ *  * vblank_expected is FALSE
+ */
+int crtc_pageflip_handler(struct drm_device *dev, int port)
+{
+	drm_emgd_priv_t *devpriv = dev->dev_private;
+	emgd_crtc_t *emgd_crtc;
+	struct drm_pending_vblank_event *e;
+	struct timeval now;
+	igd_context_t *context = NULL;
+	int crtcnum;
+	unsigned long flags;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * This handler should only be executed if KMS modesetting is
+	 * enabled.  However, the device dependent mode code always hooks
+	 * this up. Abort early if KMS modesetting is disabled.
+	 */
+	if (!devpriv->kms_enabled) {
+		return 0;
+	}
+
+	context = devpriv->context;
+
+	/* Look up which CRTC that this flip is for. */
+	if (port == devpriv->primary_port_number) {
+		crtcnum = 0;
+	} else if (port == devpriv->secondary_port_number) {
+		crtcnum = 1;
+	} else {
+		return 1;
+	}
+
+	emgd_crtc = devpriv->crtcs[crtcnum];
+
+	/* Protect access to CRTC */
+	spin_lock_irqsave(&emgd_crtc->crtc_lock, flags);
+
+	/*
+	 * Were we waiting for a vblank to do flip cleanup?  If not, we
+	 * should just bail out.
+	 */
+	if (!emgd_crtc->vblank_expected) {
+		spin_unlock_irqrestore(&emgd_crtc->crtc_lock, flags);
+		return 1;
+	}
+
+	/* Sanity check: shouldn't be here if we still have flip work queued */
+	if (emgd_crtc->flip_work_queued) {
+		EMGD_ERROR("Flip vblank handler while work queued!");
+		spin_unlock_irqrestore(&emgd_crtc->crtc_lock, flags);
+		return 0;
+	}
+
+	/*
+	 * Sanity check: shouldn't be possible to get to this point without a
+	 * userspace event to send.
+	 */
+	if (!emgd_crtc->flip_event) {
+		EMGD_ERROR("Pageflip vblank handler has no userspace event");
+		spin_unlock_irqrestore(&emgd_crtc->crtc_lock, flags);
+		return 1;
+	}
+
+	/* Release vblank refcount */
+	drm_vblank_put(dev, crtcnum);
+	emgd_crtc->vblank_expected = 0;
+
+	/* Flip is now complete; send userspace event, if requested */
+	e = emgd_crtc->flip_event;
+	do_gettimeofday(&now);
+	e->event.sequence = 0;
+	e->event.tv_sec = now.tv_sec;
+	e->event.tv_usec = now.tv_usec;
+	list_add_tail(&e->base.link, &e->base.file_priv->event_list);
+	wake_up_interruptible(&e->base.file_priv->event_wait);
+
+	/*
+	 * Cleanup; not in process of switching to new FB, no outstanding
+	 * userspace event awaiting our attention.
+	 */
+	emgd_crtc->newfb = NULL;
+	emgd_crtc->flip_event = NULL;
+
+	spin_unlock_irqrestore(&emgd_crtc->crtc_lock, flags);
+
+	EMGD_TRACE_EXIT;
+	return 1;
+}
+
+
+/**
+ * emgd_flip_worker
+ *
+ * Workqueue task to schedule a flip when rendering to the new framebuffer
+ * is complete.  Should check the "cancel" flag and just give up,
+ * regardless of whether rendering is complete or not if set.
+ *
+ * State upon entry:
+ *  * newfb is non-NULL
+ *  * no constraint on flip_event
+ *  * flip_work_queued is TRUE
+ *  * vblank_expected is FALSE
+ *
+ * State upon exit:
+ *  * no constraint on newfb
+ *  * no constraint on flip_event
+ *  * no constraint on flip_work_queued
+ *  * no constraint on vblank_expected
+ */
+void emgd_flip_worker(struct work_struct *w)
+{
+	drm_emgd_priv_t *dev_priv;
+	igd_context_t *igd_context;
+	PVRSRV_KERNEL_MEM_INFO *meminfo;
+	PVRSRV_SYNC_DATA *syncdata = NULL;
+	emgd_crtc_t *crtc;
+	igd_surface_t igd_surface = { 0 };
+	unsigned long flags;
+	unsigned int crtcnum;
+	int ret;
+
+	/* Which CRTC does this work task belong to? */
+	crtc = container_of(w, emgd_crtc_t, flip_work);
+
+	/* Protect updates to the CRTC structure */
+	spin_lock_irqsave(&crtc->crtc_lock, flags);
+
+	/* Sanity check:  flip_work_queued must be TRUE */
+	if (!crtc->flip_work_queued) {
+		EMGD_ERROR("Flip worker running without being queued");
+		spin_unlock_irqrestore(&crtc->crtc_lock, flags);
+		return;
+	}
+
+	/* Sanity check:  newfb must not be NULL */
+	if (!crtc->newfb) {
+		EMGD_ERROR("No newfb in flip worker");
+		spin_unlock_irqrestore(&crtc->crtc_lock, flags);
+		return;
+	}
+
+	/*
+	 * Sanity check: we shouldn't be expecting vblank/cleanup while we're
+	 * still doing flip work (prior flips that may have scheduled a vblank
+	 * were cancelled already.
+	 */
+	if (crtc->vblank_expected) {
+		EMGD_ERROR("Expecting vblank cleanup while flip work in progress");
+		spin_unlock_irqrestore(&crtc->crtc_lock, flags);
+		return;
+	}
+
+	/*
+	 * Grab the PVR meminfo and syncinfo for the surface we're waiting for
+	 * rendering completion on.
+	 */
+	if (crtc->newfb->type == PVR_FRAMEBUFFER) {
+		meminfo = (PVRSRV_KERNEL_MEM_INFO *)crtc->newfb->pvr_meminfo;
+		syncdata = meminfo->psKernelSyncInfo->psSyncData;
+	}
+
+	/*
+	 * Have we completed all the operations that were pending when the flip
+	 * ioctl was called?  If so, proceed with issuing the actual flip.  If
+	 * we're flipping to a GMM framebuffer (i.e., the initial system fb),
+	 * then we don't need to wait for any kind of rendering).
+	 *
+	 * It seems like we should test for wraparound here, but I don't see
+	 * anywhere in PVR's code where they handle wraparound.  It's probably safe
+	 * to ignore for now since even if we perform 60 ops per second against the
+	 * framebuffer, it would still take over two years to overflow the 32-bit
+	 * unsigned int for the operation counter.
+	 */
+	if (crtc->newfb->type == GMM_FRAMEBUFFER ||
+		syncdata->ui32WriteOpsComplete >= crtc->render_complete_at)
+	{
+		dev_priv = (drm_emgd_priv_t *) crtc->base.dev->dev_private;
+		igd_context = dev_priv->context;
+		crtcnum = (crtc == dev_priv->crtcs[0]) ? 0 : 1;
+
+		/* Rendering complete; program the plane registers */
+		igd_surface.flags        = IGD_SURFACE_DISPLAY;
+		igd_surface.offset       = crtc->newfb->gtt_offset;
+		igd_surface.pitch        = crtc->newfb->base.pitch;
+		igd_surface.width        = crtc->newfb->base.width;
+		igd_surface.height       = crtc->newfb->base.height;
+		igd_surface.pixel_format = IGD_PF_ARGB32;
+
+		igd_context->dispatch.set_surface(
+			crtc->igd_pipe->owner,
+			IGD_PRIORITY_NORMAL,
+			IGD_BUFFER_DISPLAY,
+			&igd_surface,
+			NULL, /* Not used */
+			0);
+
+		/* Flip issued.  No need to requeue the work. */
+		crtc->flip_work_queued = 0;
+
+		/*
+		 * If a userspace event was requested for this flip, request vblank
+		 * interrupts if they aren't already on.
+		 */
+		if (crtc->flip_event) {
+			/* Request vblank events (or inc the refcount if they're already on) */
+			ret = drm_vblank_get(crtc->base.dev, crtcnum);
+			if (ret) {
+				EMGD_ERROR("Failed enable vblanks");
+				return;
+			}
+			crtc->vblank_expected = 1;
+		} else {
+			/*
+			 * No userspace event; the flip is complete as far as we're
+			 * concerned.  Clear the "in progress of switching to fb" field.
+			 */
+			crtc->newfb = NULL;
+		}
+
+	} else {
+		/* Rendering not complete.  Requeue the work task. */
+		schedule_work(w);
+	}
+
+	spin_unlock_irqrestore(&crtc->crtc_lock, flags);
+}
+
+
+/**
+ * emgd_crtc_page_flip
+ *
+ * Page flip ioctl handler.  The ioctl simply dispatches a workqueue task
+ * which will wait until current rendering against the new framebuffer
+ * is complete, then issue the actual flip.  This ioctl should return
+ * immediately, allowing pipelining of subsequent CPU execution with
+ * the outstanding rendering happening against this framebuffer.
+ *
+ * @param crtc  (INOUT) The pipe to put the new framebuffer on
+ * @param fb    (IN)    Framebuffer to flip to
+ * @param event (IN)    Event to signal when flip has been completed
+ *
+ * @return
+ *
+ * State upon entry:
+ *  * No constraint on newfb
+ *  * No constraint on flip_event
+ *  * No constraint on flip_work_queued
+ *  * No constraint on vblank_expected
+ *
+ * State upon exit:
+ *  * newfb is non-NULL
+ *  * no constraint on flip_event
+ *  * flip_work_queued is TRUE
+ *  * vblank_expected is FALSE
+ */
+static int emgd_crtc_page_flip(struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_pending_vblank_event *event)
+{
+	emgd_crtc_t        *emgd_crtc;
+	emgd_framebuffer_t *emgd_fb;
+	drm_emgd_priv_t    *dev_priv;
+	igd_context_t      *igd_context;
+	unsigned int crtcnum;
+	unsigned long flags;
+	struct drm_pending_vblank_event *e;
+	struct timeval now;
+	PVRSRV_KERNEL_MEM_INFO *meminfo;
+	PVRSRV_SYNC_DATA *syncdata;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_crtc   = container_of(crtc, emgd_crtc_t, base);
+	dev_priv    = (drm_emgd_priv_t *) crtc->dev->dev_private;
+	igd_context = dev_priv->context;
+	emgd_fb     = container_of(fb, emgd_framebuffer_t, base);
+	crtcnum = (emgd_crtc == dev_priv->crtcs[0]) ? 0 : 1;
+
+	/*
+	 * Protect updates to the CRTC structure. We don't want this code to
+	 * overlap with either the workqueue task or the vblank handler.
+	 */
+	spin_lock_irqsave(&emgd_crtc->crtc_lock, flags);
+
+	/*
+	 * Was there a pending userspace event for a previous flip request?  If
+	 * so, just send the completion back now.  We're essentially cancelling
+	 * the previous flip (it will never show up on the display), but we don't
+	 * want userspace to get confused by not receiving notification.  We'll
+	 * also decrement our vblank request if we'd already scheduled on for
+	 * cleanup.
+	 */
+	if ((e = emgd_crtc->flip_event) != NULL) {
+		do_gettimeofday(&now);
+		e->event.sequence = 0;
+		e->event.tv_sec = now.tv_sec;
+		e->event.tv_usec = now.tv_usec;
+		list_add_tail(&e->base.link, &e->base.file_priv->event_list);
+		wake_up_interruptible(&e->base.file_priv->event_wait);
+		emgd_crtc->flip_event = NULL;
+
+		/*
+		 * If the work task had completed and actually programmed the
+		 * registers, it had also requested a vblank callback.  Cancel
+		 * that as well since we've already taken care of the necessary
+		 * cleanup.  Note that vblanks are only requested when we actually
+		 * have a userspace event to send back.
+		 */
+		if (emgd_crtc->vblank_expected) {
+			drm_vblank_put(crtc->dev, crtcnum);
+			emgd_crtc->vblank_expected = 0;
+		}
+	}
+
+	/* Update the CRTC's "target framebuffer" field. */
+	emgd_crtc->newfb = emgd_fb;
+	emgd_crtc->flip_event = event;
+
+	/*
+	 * Set the number of rendering operations that need to complete before we
+	 * can flip to this buffer.  I.e., we don't need render to completely
+	 * quiesce, we can flip as soon as any operations that are outstanding
+	 * right now complete, even if more rendering ops get added to the pipeline
+	 * after we return.
+	 */
+	meminfo = (PVRSRV_KERNEL_MEM_INFO *)emgd_fb->pvr_meminfo;
+	syncdata = meminfo->psKernelSyncInfo->psSyncData;
+	emgd_crtc->render_complete_at = syncdata->ui32WriteOpsPending;
+
+	/*
+	 * If work is already scheduled, nothing more to do here; the
+	 * already-scheduled work will see the new values we set next time it wakes
+	 * up and will act upon them.  However if work is not scheduled, then
+	 * we need to schedule it now.
+	 */
+	if (!emgd_crtc->flip_work_queued) {
+		schedule_work(&emgd_crtc->flip_work);
+		emgd_crtc->flip_work_queued = 1;
+	}
+
+	/* Move the FB currently associated with the CRTC to the new FB */
+	crtc->fb = fb;
+
+	/* Done updating CRTC structure */
+	spin_unlock_irqrestore(&emgd_crtc->crtc_lock, flags);
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_drv.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_drv.c
new file mode 100644
index 0000000..92f70a7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_drv.c
@@ -0,0 +1,2472 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_drv.c
+ * $Revision: 1.145 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  The main part of the kernel module.  This part gets everything going and
+ *  connected, and then the rest can function.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.oal
+
+#include <drm/drmP.h>
+#include <drm/drm.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_crtc_helper.h>
+#include <linux/version.h>
+#include <linux/device.h>
+#include <drm/drm_pciids.h>
+#include <intelpci.h>
+#include "drm_emgd_private.h"
+#include "user_config.h"
+#include "emgd_drv.h"
+#include "emgd_drm.h"
+#include "memory.h"
+#include "io.h"
+#include "mode_dispatch.h"
+#include "igd_debug.h"
+#include "splash_screen.h"
+/*
+ * Imagination includes.
+ */
+#include <img_types.h>
+#include <pvr_drm.h>
+#include <pvr_drm_shared.h>
+#include <pvr_bridge.h>
+#include <linkage.h>
+#include <sysconfig.h>
+
+/* For Buffer Class of Texture Stream*/
+/* pvr/services4/3rdparty/emgd_bufferclass/emgd_bc_linux.c */
+extern int emgd_bc_ts_init(void);
+extern int emgd_bc_ts_uninit(void);
+
+/*------------------------------------------------------------------------------
+ * Formal Declaration
+ *------------------------------------------------------------------------------
+ */
+extern void emgd_set_real_handle(igd_driver_h drm_handle);
+extern void emgd_set_real_dispatch(igd_dispatch_t *drm_dispatch);
+extern void emgd_modeset_init(struct drm_device *dev);
+extern void emgd_modeset_destroy(struct drm_device *dev);
+extern int  msvdx_pre_init_plb(struct drm_device *dev);
+extern int msvdx_shutdown_plb(igd_context_t *context);
+extern emgd_drm_config_t config_drm;
+extern int context_count;
+
+/* This must be defined whether debug or release build */
+igd_debug_t emgd_debug_flag = {
+	{
+		CONFIG_DEBUG_FLAGS
+	}
+};
+igd_debug_t *emgd_debug = &emgd_debug_flag;
+
+#ifdef DEBUG_BUILD_TYPE
+
+MODULE_PARM_DESC(debug_cmd, "Debug: cmd");
+module_param_named(debug_cmd, emgd_debug_flag.hal.cmd, short, 0600);
+
+MODULE_PARM_DESC(debug_dsp, "Debug: dsp");
+module_param_named(debug_dsp, emgd_debug_flag.hal.dsp, short, 0600);
+
+MODULE_PARM_DESC(debug_mode, "Debug: mode");
+module_param_named(debug_mode, emgd_debug_flag.hal.mode, short, 0600);
+
+MODULE_PARM_DESC(debug_init, "Debug: init");
+module_param_named(debug_init, emgd_debug_flag.hal.init, short, 0600);
+
+MODULE_PARM_DESC(debug_overlay, "Debug: overlay");
+module_param_named(debug_overlay, emgd_debug_flag.hal.overlay, short, 0600);
+
+MODULE_PARM_DESC(debug_power, "Debug: power");
+module_param_named(debug_power, emgd_debug_flag.hal.power, short, 0600);
+
+MODULE_PARM_DESC(debug_2D, "Debug: 2D");
+module_param_named(debug_2D, emgd_debug_flag.hal._2d, short, 0600);
+
+MODULE_PARM_DESC(debug_blend, "Debug: blend");
+module_param_named(debug_blend, emgd_debug_flag.hal.blend, short, 0600);
+
+MODULE_PARM_DESC(debug_state, "Debug: state");
+module_param_named(debug_state, emgd_debug_flag.hal.state, short, 0600);
+
+MODULE_PARM_DESC(debug_gmm, "Debug: GMM");
+module_param_named(debug_gmm, emgd_debug_flag.hal.gmm, short, 0600);
+
+MODULE_PARM_DESC(debug_gart, "Debug: GART");
+module_param_named(debug_gart, emgd_debug_flag.hal.gart, short, 0600);
+
+MODULE_PARM_DESC(debug_oal, "Debug: OAL");
+module_param_named(debug_oal, emgd_debug_flag.hal.oal, short, 0600);
+
+MODULE_PARM_DESC(debug_intr, "Debug: intr");
+module_param_named(debug_intr, emgd_debug_flag.hal.intr, short, 0600);
+
+MODULE_PARM_DESC(debug_dpd, "Debug: dpd");
+module_param_named(debug_dpd, emgd_debug_flag.hal.dpd, short, 0600);
+
+MODULE_PARM_DESC(debug_video, "Debug: video");
+module_param_named(debug_video, emgd_debug_flag.hal.video, short, 0600);
+
+MODULE_PARM_DESC(debug_pvr3dd, "Debug: PVR3DD");
+module_param_named(debug_pvr3dd, emgd_debug_flag.hal.pvr3dd, short, 0600);
+
+MODULE_PARM_DESC(debug_trace, "Global Debug: trace");
+module_param_named(debug_trace, emgd_debug_flag.hal.trace, short, 0600);
+
+MODULE_PARM_DESC(debug_instr, "Global Debug: instr");
+module_param_named(debug_instr, emgd_debug_flag.hal.instr, short, 0600);
+
+MODULE_PARM_DESC(debug_debug, "Global Debug: Debug with no associated module ");
+module_param_named(debug_debug, emgd_debug_flag.hal.debug, short, 0600);
+
+MODULE_PARM_DESC(debug_blend_stats, "Verbose Debug: Blend stats");
+module_param_named(debug_blend_stats, emgd_debug_flag.hal.blend_stats, short, 0600);
+
+MODULE_PARM_DESC(debug_dump_overlay_regs, "Verbose Debug: Dump overlay regs");
+module_param_named(debug_dump_overlay_regs, emgd_debug_flag.hal.dump_overlay_regs, short, 0600);
+
+MODULE_PARM_DESC(debug_dump_command_queue, "Verbose Debug: dump command queue");
+module_param_named(debug_dump_command_queue, emgd_debug_flag.hal.dump_command_queue, short, 0600);
+
+MODULE_PARM_DESC(debug_dump_gmm_on_fail, "Verbose Debug: dump gmm on fail");
+module_param_named(debug_dump_gmm_on_fail, emgd_debug_flag.hal.dump_gmm_on_fail, short, 0600);
+
+MODULE_PARM_DESC(debug_dump_shaders, "Verbose Debug: dump shaders");
+module_param_named(debug_dump_shaders, emgd_debug_flag.hal.dump_shaders, short, 0600);
+
+MODULE_PARM_DESC(debug_bc_ts, "Debug: Texture Stream");
+module_param_named(debug_bc_ts, emgd_debug_flag.hal.buf_class, short, 0600);
+#endif
+
+
+static struct drm_driver driver;  /* TODO: what? */
+
+/* Note: The following module paramter values are advertised to the root user,
+ * via the files in the /sys/module/emgd/parameters directory (e.g. the "init"
+ * file contains the value of the "init" module parameter), and so we keep
+ * these values up to date.
+ *
+ * Note: The initial values are all set to -1, so that we can tell if the user
+ * set them.
+ */
+int drm_emgd_portorder[IGD_MAX_PORTS] = {-1, -1, -1, -1, -1};
+int drm_emgd_numports;
+int drm_emgd_configid = -1;
+int drm_emgd_init = -1;
+int drm_emgd_dc = -1;
+int drm_emgd_width = -1;
+int drm_emgd_height = -1;
+int drm_emgd_refresh = -1;
+MODULE_PARM_DESC(portorder, "Display port order (e.g. \"4,2,0,0,0\")");
+MODULE_PARM_DESC(configid, "Which defined configuration number to use (e.g. "
+	"\"1\")");
+MODULE_PARM_DESC(init, "Whether to initialize the display at startup (1=yes, "
+	"0=no)");
+MODULE_PARM_DESC(dc, "Display configuration (i.e. 1=single, 2=clone)");
+MODULE_PARM_DESC(width, "Display resolution's width (e.g. \"1024\")");
+MODULE_PARM_DESC(height, "Display resolution's height (e.g. \"768\")");
+MODULE_PARM_DESC(refresh, "Monitor refresh rate (e.g. 60, as in 60Hz)");
+module_param_array_named(portorder, drm_emgd_portorder, int, &drm_emgd_numports,
+	0600);
+module_param_named(configid, drm_emgd_configid, int, 0600);
+module_param_named(init, drm_emgd_init, int, 0600);
+module_param_named(dc, drm_emgd_dc, int, 0600);
+module_param_named(width, drm_emgd_width, int, 0600);
+module_param_named(height, drm_emgd_height, int, 0600);
+module_param_named(refresh, drm_emgd_refresh, int, 0600);
+
+
+/** The DC to use when the DRM module [re-]initializes the display. */
+static unsigned long *desired_dc = NULL;
+/** The DC to use when the DRM module [re-]initializes the display. */
+static unsigned short port_number = 0;
+/** The mode to use when the DRM module [re-]initializes the display. */
+static igd_display_info_t *desired_mode = NULL;
+/** Set to true when we know X is initialized. This flag should be set
+ ** earlier, but right now we're setting it in emgd_driver_preclose() */
+unsigned x_started = false;
+
+/**
+ * The primary fb_info to use when the DRM module [re-]initializes the display.
+ */
+igd_framebuffer_info_t *primary_fb_info;
+/**
+ * The secondary fb_info to use when the DRM module [re-]initializes the
+ * display.
+ */
+igd_framebuffer_info_t *secondary_fb_info;
+/**
+ * The primary display structure (filled in by alter_displays()) to use when
+ * the DRM module [re-]initializes the display.
+ */
+igd_display_h primary;
+/**
+ * The secondary display structure (filled in by alter_displays()) to use when
+ * the DRM module [re-]initializes the display.
+ */
+igd_display_h secondary;
+/**
+ * The display information to use when the DRM module [re-]initializes the
+ * display.
+ */
+igd_display_info_t pt_info;
+extern mode_context_t mode_context[1];
+
+/* Note: This macro is #define'd in "oal/os/memory.h" */
+#ifdef INSTRUMENT_KERNEL_ALLOCS
+os_allocd_mem *list_head = NULL;
+os_allocd_mem *list_tail = NULL;
+#endif
+
+
+#define emgd_PCI_IDS \
+	{0x8086, 0x8108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CHIP_PSB_8108}, \
+    {0x8086, 0x8109, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CHIP_PSB_8109}, \
+    {0x8086, 0x4108, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CHIP_TC_4108}, \
+    {0, 0, 0}
+
+static struct pci_device_id pciidlist[] = {
+	    emgd_PCI_IDS
+};
+
+/*
+ * To use DRM_IOCTL_DEF, the first arg should be the local (zero based)
+ * IOCTL number, not the global number.
+ */
+#define EMGD_IOCTL_DEF(ioctl, _func, _flags) \
+	[DRM_IOCTL_NR(ioctl) - DRM_COMMAND_BASE] = \
+		{.cmd = ioctl, .func = _func, .flags = _flags}
+
+static struct drm_ioctl_desc emgd_ioctl[] = {
+	/*
+	 * NOTE: The flag "DRM_MASTER" for the final parameter indicates an ioctl
+	 * can only be used by the DRM master process.  In an X environment, the
+	 * X server will be the master, in a Wayland environment, the Wayland
+	 * compositor will be master, and if just running standalone GBM apps,
+	 * they'll gain master.  For ioctl's that we want to run from an X
+	 * client app or a Wayland client app, we instead use DRM_AUTH; these
+	 * clients will get the DRM master to authenticate them, after which
+	 * they'll be able to call the ioctl's.  Random programs that haven't
+	 * authenticated with the DRM master won't be able to call them.
+	 */
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_ALTER_CURSOR, emgd_alter_cursor,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_ALTER_CURSOR_POS, emgd_alter_cursor_pos,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_ALTER_DISPLAYS, emgd_alter_displays,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_ALTER_OVL, emgd_alter_ovl, DRM_MASTER),
+	/* Making DRM_IOCTL_IGD_ALTER_OVL2 DRM_AUTH so that libva wayland can 
+	 * call alter_ovl without going through X server.
+	 */
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_ALTER_OVL2, emgd_alter_ovl2, DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_APPCTX_ALLOC, emgd_appcontext_alloc,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_APPCTX_FREE, emgd_appcontext_free,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_DRIVER_SAVE_RESTORE, emgd_driver_save_restore,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_ENABLE_PORT, emgd_enable_port, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_ATTRS, emgd_get_attrs, DRM_MASTER),
+	/* Making DRM_IOCTL_IGD_GET_DISPLAY DRM_AUTH so that libva wayland can
+	 * obtain the display handle without going through x server.
+	 */ 
+    EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_DISPLAY, emgd_get_display, DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_DRM_CONFIG, emgd_get_drm_config,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_EDID_BLOCK, emgd_get_EDID_block,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_EDID_INFO, emgd_get_EDID_info,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_PIXELFORMATS, emgd_get_pixelformats,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_PORT_INFO, emgd_get_port_info,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GMM_ALLOC_REGION, emgd_gmm_alloc_region,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GMM_ALLOC_SURFACE, emgd_gmm_alloc_surface,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GMM_GET_NUM_SURFACE, emgd_gmm_get_num_surface,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GMM_GET_SURFACE_LIST,
+		emgd_gmm_get_surface_list,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GMM_FREE, emgd_gmm_free,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GMM_FLUSH_CACHE, emgd_gmm_flush_cache,
+		DRM_MASTER),
+	/*
+	 * Externally handled IOCTL's. These are routed to the Imagination Tech
+	 * kernel services.
+	 *   function prototypes in services4/srvkm/env/linux/pvr_drm.h
+	 */
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_1, PVRSRV_BridgeDispatchKM, 0),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_2, PVRDRM_Dummy_ioctl, 0),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_3, PVRDRM_Dummy_ioctl, 0),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_4, PVRDRMIsMaster, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_5, PVRDRMUnprivCmd, 0),
+
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_PAN_DISPLAY, emgd_pan_display,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_POWER_DISPLAY, emgd_power_display,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_PWR_ALTER, emgd_pwr_alter, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_QUERY_DC, emgd_query_dc,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_QUERY_MAX_SIZE_OVL, emgd_query_max_size_ovl,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_QUERY_OVL, emgd_query_ovl, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_QUERY_MODE_LIST, emgd_query_mode_list,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_GOLDEN_HTOTAL, emgd_get_golden_htotal,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_CONTROL_PLANE_FORMAT, emgd_control_plane_format,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_SET_OVERLAY_DISPLAY, emgd_set_overlay_display,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_QUERY_2D_CAPS_HWHINT, emgd_query_2d_caps_hwhint,
+		DRM_MASTER),
+	/*
+	 * For PDUMP
+	 */
+#if defined(PDUMP)
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_6, dbgdrv_ioctl, 0),
+#else
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_RESERVED_6, NULL, 0),
+#endif
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_SET_ATTRS, emgd_set_attrs, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_SET_PALETTE_ENTRY, emgd_set_palette_entry,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_SET_SURFACE, emgd_set_surface, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_SYNC, emgd_sync, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_DRIVER_PRE_INIT, emgd_driver_pre_init,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_DRIVER_GET_PORTS, emgd_driver_get_ports,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_OVL_INIT_PARAMS, emgd_get_ovl_init_params,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_PAGE_LIST, emgd_get_page_list,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_START_PVRSRV, emgd_start_pvrsrv,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_TEST_PVRSRV, emgd_test_pvrsrv,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_CHIPSET_INFO, emgd_get_chipset_info,
+		DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_DIHCLONE_SET_SURFACE, emgd_dihclone_set_surface, DRM_MASTER),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_PREINIT_MMU, emgd_preinit_mmu, DRM_MASTER),
+
+	/*
+	 * For VIDEO (MSVDX/TOPAZ
+	 */
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_VIDEO_CMD_BUF, emgd_video_cmd_buf,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_INIT_VIDEO, emgd_init_video,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_GET_DEVICE_INFO, emgd_get_device_info,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_VIDEO_GET_INFO, emgd_video_get_info,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_VIDEO_FLUSH_TLB, emgd_video_flush_tlb,
+		DRM_AUTH),
+
+	/* For Buffer Class of Texture Stream */
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_INIT, emgd_bc_ts_cmd_init,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_UNINIT, emgd_bc_ts_cmd_uninit,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_REQUEST_BUFFERS, emgd_bc_ts_cmd_request_buffers,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_RELEASE_BUFFERS, emgd_bc_ts_cmd_release_buffers,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_SET_BUFFER_INFO, emgd_bc_ts_set_buffer_info,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_GET_BUFFERS_COUNT, emgd_bc_ts_get_buffers_count,
+		DRM_AUTH),
+	EMGD_IOCTL_DEF(DRM_IOCTL_IGD_BC_TS_GET_BUFFER_INDEX, emgd_bc_ts_get_buffer_index,
+		DRM_AUTH),
+};
+
+static int emgd_max_ioctl = DRM_ARRAY_SIZE(emgd_ioctl);
+
+
+
+/*
+ * NOTE: The next part of this file are EMGD-specific DRM functions, exported to
+ * the generic DRM code via the drm_driver struct:
+ */
+
+
+extern igd_driver_h handle;
+/** This is the dispatch table for the HAL.  It is cached for quick access. */
+extern igd_dispatch_t *dispatch;
+
+
+/**
+ * The driver handle for talking with the HAL, within the DRM/kernel code.
+ *
+ * This is a "real handle" as opposed to the "fake handle" in user-space.
+ * Notice that there's only one handle, as the secondary device shares this
+ * handle.
+ */
+static igd_driver_h drm_HAL_handle = NULL;
+
+/**
+ * This is the drm_HAL_handle cast to an igd_context_t.  It is cached for quick
+ * access.
+ */
+static igd_context_t *drm_HAL_context = NULL;
+
+/**
+ * This is the dispatch table for the HAL.  It is cached for quick access.
+ */
+static igd_dispatch_t *drm_HAL_dispatch = NULL;
+
+
+
+/*!
+ * get_pre_driver_info
+ *
+ * Gets the mode information before the user-mode driver changes it.
+ * This information can either come from the firmware or the DRM.
+ * Note:  Prior to EMGD this information can only come from the firmware
+ *        thus the field "fw_info."  This should really be changed
+ *        to "pre_drv_info"
+ *
+ * @param mode_context This is where fw_info is stored
+ *
+ * @return -IGD_INVAL on failure
+ * @return 0 on success
+ */
+static int get_pre_driver_info(mode_context_t *mode_context)
+{
+	int ret = 0;
+	int seamless = FALSE;
+	EMGD_TRACE_ENTER;
+
+	if(mode_context->fw_info != NULL) {
+		seamless = TRUE;
+
+		ret = mode_context->dispatch->full->get_plane_info();
+		if(ret) {
+			seamless = FALSE;
+		}
+
+		ret = mode_context->dispatch->full->get_pipe_info(primary);
+		if(ret) {
+			seamless = FALSE;
+		}
+
+		ret = mode_context->dispatch->full->get_port_info();
+		if(ret) {
+			seamless = FALSE;
+		}
+
+	}
+
+	if(seamless == FALSE) {
+		/* If one of these plane/pipe/port functions
+		 *  returns an error, we explicitly
+		 *  turn-off seamless.
+		 */
+		 mode_context->seamless = FALSE;
+
+	}
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/**
+ * A helper function that prints the igd_param_t struct.
+ *
+ * @param params (IN) The the igd_param_t struct to print
+ */
+void emgd_print_params(igd_param_t *params)
+{
+	int i;
+
+	EMGD_DEBUG("Values of params:");
+	EMGD_DEBUG(" page_request = %lu = 0x%lx", params->page_request,
+		params->page_request);
+	EMGD_DEBUG(" max_fb_size = %lu = 0x%lx", params->max_fb_size,
+		params->max_fb_size);
+	EMGD_DEBUG(" preserve_regs = %u", params->preserve_regs);
+	EMGD_DEBUG(" display_flags = %lu = 0x%lx", params->display_flags,
+		params->display_flags);
+	EMGD_DEBUG(" port_order:");
+	for (i = 0 ; i < IGD_MAX_PORTS; i++) {
+		EMGD_DEBUG("  port number %d = %lu", i, params->port_order[i]);
+	}
+	EMGD_DEBUG(" display_params:");
+	for (i = 0 ; i < IGD_MAX_PORTS; i++) {
+		int j;
+
+		EMGD_DEBUG("  port_number = %lu",
+			params->display_params[i].port_number);
+		EMGD_DEBUG("   present_params = %lu = 0x%lx",
+			params->display_params[i].present_params,
+			params->display_params[i].present_params);
+		EMGD_DEBUG("   flags = %lu = 0x%lx",
+			params->display_params[i].flags,
+			params->display_params[i].flags);
+		EMGD_DEBUG("   edid_avail = %u = 0x%x",
+			params->display_params[i].edid_avail,
+			params->display_params[i].edid_avail);
+		EMGD_DEBUG("   edid_not_avail = %u = 0x%x",
+			params->display_params[i].edid_not_avail,
+			params->display_params[i].edid_not_avail);
+		EMGD_DEBUG("   ddc_gpio = %lu", params->display_params[i].ddc_gpio);
+		EMGD_DEBUG("   ddc_speed = %lu", params->display_params[i].ddc_speed);
+		EMGD_DEBUG("   ddc_dab = %lu", params->display_params[i].ddc_dab);
+		EMGD_DEBUG("   i2c_gpio = %lu", params->display_params[i].i2c_gpio);
+		EMGD_DEBUG("   i2c_speed = %lu", params->display_params[i].i2c_speed);
+		EMGD_DEBUG("   i2c_dab = %lu", params->display_params[i].i2c_dab);
+		EMGD_DEBUG("   fp_info.fp_width = %lu",
+			params->display_params[i].fp_info.fp_width);
+		EMGD_DEBUG("   fp_info.fp_height = %lu",
+			params->display_params[i].fp_info.fp_height);
+		EMGD_DEBUG("   fp_info.fp_pwr_method = %lu",
+			params->display_params[i].fp_info.fp_pwr_method);
+		EMGD_DEBUG("   fp_info.fp_pwr_t1 = %lu",
+			params->display_params[i].fp_info.fp_pwr_t1);
+		EMGD_DEBUG("   fp_info.fp_pwr_t2 = %lu",
+			params->display_params[i].fp_info.fp_pwr_t2);
+		EMGD_DEBUG("   fp_info.fp_pwr_t3 = %lu",
+			params->display_params[i].fp_info.fp_pwr_t3);
+		EMGD_DEBUG("   fp_info.fp_pwr_t4 = %lu",
+			params->display_params[i].fp_info.fp_pwr_t4);
+		EMGD_DEBUG("   fp_info.fp_pwr_t5 = %lu",
+			params->display_params[i].fp_info.fp_pwr_t5);
+		EMGD_DEBUG("   dtd_list:");
+		EMGD_DEBUG("    num_dtds = %lu",
+			params->display_params[i].dtd_list.num_dtds);
+		for (j = 0 ; j < params->display_params[i].dtd_list.num_dtds; j++) {
+			EMGD_DEBUG("   *dtd[%d].width = %u", j,
+				params->display_params[i].dtd_list.dtd[j].width);
+			EMGD_DEBUG("    dtd[%d].height = %u", j,
+				params->display_params[i].dtd_list.dtd[j].height);
+			EMGD_DEBUG("    dtd[%d].refresh = %u", j,
+				params->display_params[i].dtd_list.dtd[j].refresh);
+			EMGD_DEBUG("    dtd[%d].dclk = %lu = 0x%lx", j,
+				params->display_params[i].dtd_list.dtd[j].dclk,
+				params->display_params[i].dtd_list.dtd[j].dclk);
+			EMGD_DEBUG("    dtd[%d].htotal = %u", j,
+				params->display_params[i].dtd_list.dtd[j].htotal);
+			EMGD_DEBUG("    dtd[%d].hblank_start = %u", j,
+				params->display_params[i].dtd_list.dtd[j].hblank_start);
+			EMGD_DEBUG("    dtd[%d].hblank_end = %u", j,
+				params->display_params[i].dtd_list.dtd[j].hblank_end);
+			EMGD_DEBUG("    dtd[%d].hsync_start = %u", j,
+				params->display_params[i].dtd_list.dtd[j].hsync_start);
+			EMGD_DEBUG("    dtd[%d].hsync_end = %u", j,
+				params->display_params[i].dtd_list.dtd[j].hsync_end);
+			EMGD_DEBUG("    dtd[%d].vtotal = %u", j,
+				params->display_params[i].dtd_list.dtd[j].vtotal);
+			EMGD_DEBUG("    dtd[%d].vblank_start = %u", j,
+				params->display_params[i].dtd_list.dtd[j].vblank_start);
+			EMGD_DEBUG("    dtd[%d].vblank_end = %u", j,
+				params->display_params[i].dtd_list.dtd[j].vblank_end);
+			EMGD_DEBUG("    dtd[%d].vsync_start = %u", j,
+				params->display_params[i].dtd_list.dtd[j].vsync_start);
+			EMGD_DEBUG("    dtd[%d].vsync_end = %u", j,
+				params->display_params[i].dtd_list.dtd[j].vsync_end);
+			EMGD_DEBUG("    dtd[%d].mode_number = %d", j,
+				params->display_params[i].dtd_list.dtd[j].mode_number);
+			EMGD_DEBUG("    dtd[%d].flags = %lu = 0x%lx", j,
+				params->display_params[i].dtd_list.dtd[j].flags,
+				params->display_params[i].dtd_list.dtd[j].flags);
+			EMGD_DEBUG("    dtd[%d].x_offset = %u", j,
+				params->display_params[i].dtd_list.dtd[j].x_offset);
+			EMGD_DEBUG("    dtd[%d].y_offset = %u", j,
+				params->display_params[i].dtd_list.dtd[j].y_offset);
+			/*EMGD_DEBUG("    dtd[%d].pd_private_ptr = 0x%p", j,
+				params->display_params[i].dtd_list.dtd[j].pd_private_ptr); */
+			EMGD_DEBUG("    dtd[%d].private_ptr = 0x%p", j,
+				params->display_params[i].dtd_list.dtd[j].private_ptr);
+		}
+		EMGD_DEBUG("   attr_list:");
+		EMGD_DEBUG("    num_attrs = %lu",
+			params->display_params[i].attr_list.num_attrs);
+		for (j = 0 ; j < params->display_params[i].attr_list.num_attrs; j++) {
+			EMGD_DEBUG("    attr[%d].id = %lu = 0x%lx", j,
+				params->display_params[i].attr_list.attr[j].id,
+				params->display_params[i].attr_list.attr[j].id);
+			EMGD_DEBUG("    attr[%d].value = %lu = 0x%lx", j,
+				params->display_params[i].attr_list.attr[j].value,
+				params->display_params[i].attr_list.attr[j].value);
+		}
+	}
+	EMGD_DEBUG("   display_color = %lu = 0x%lx", params->display_color,
+		params->display_color);
+	EMGD_DEBUG("   quickboot = %lu", params->quickboot);
+	EMGD_DEBUG("   qb_seamless = %d", params->qb_seamless);
+	EMGD_DEBUG("   qb_video_input = %lu", params->qb_video_input);
+	EMGD_DEBUG("   qb_splash = %d", params->qb_splash);
+	EMGD_DEBUG("   polling = %d", params->polling);
+} /* emgd_print_params() */
+
+
+/**
+ * A helper function that starts, initializes and configures the HAL.  This
+ * will be called during emgd_driver_load() if the display is to be initialized
+ * at module load time.  Otherwise, this is deferred till the X driver loads
+ * and calls emgd_driver_pre_init().
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h").
+ * @param params (IN) The the igd_param_t struct to give to the HAL.
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int emgd_startup_hal(struct drm_device *dev, igd_param_t *params)
+{
+	drm_emgd_priv_t *priv = dev->dev_private;
+	int err = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Initialize the various HAL modules: */
+	EMGD_DEBUG("Calling igd_module_init()");
+
+	err = igd_module_init(drm_HAL_handle, &drm_HAL_dispatch, params);
+	if (err != 0) {
+		return -EIO;
+	}
+
+	/* Give the dispatch table to the ioctl-handling "bridge" code: */
+	emgd_set_real_dispatch(drm_HAL_dispatch);
+
+	/* Record that the HAL is running now: */
+	priv->hal_running = 1;
+	/* Record that the console's state is saved: */
+	priv->saved_registers = CONSOLE_STATE_SAVED;
+
+	/* Since PVR services is running, we're restarting the HAL, which
+	 * disabled the SGX & MSVDX interrupts.  Need to re-enable those
+	 * interrupts:
+	 */
+	SysReEnableInterrupts();
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+} /* emgd_startup_hal() */
+
+
+
+/**
+ * A helper function that initializes the display, and potentially merges the
+ * module parameters with the pre-compiled parameters.
+ *
+ * @param merge_mod_params (IN) non-zero if the module parameters should be
+ * merged with the pre-compiled parameters.
+ *
+ */
+void emgd_init_display(int merge_mod_params, drm_emgd_priv_t *priv)
+{
+	int                     err         = 0;
+	igd_display_info_t     *mode_list   = NULL;
+	igd_display_info_t     *mode        = NULL;
+	struct drm_framebuffer *framebuffer = NULL;
+	emgd_framebuffer_t     *emgd_fb     = NULL;
+	unsigned char          *fb          = NULL;
+	int                     mode_flags = IGD_QUERY_LIVE_MODES;
+	unsigned long           temp_bg_color;
+	int temp_dc;
+	EMGD_TRACE_ENTER;
+
+
+	if (merge_mod_params) {
+		EMGD_DEBUG("Checking other module parameters before initializing the "
+				"display");
+
+		/*************************************
+		 * Get the desired display display config:
+		 *************************************/
+		if (-1 != drm_emgd_dc) {
+			/* Validate and potentially use the module parameter: */
+			EMGD_DEBUG("Value of module parameter \"dc\" = \"%d\"", drm_emgd_dc);
+			if (IGD_DC_SINGLE(drm_emgd_dc) || IGD_DC_CLONE(drm_emgd_dc) ||
+				IGD_DC_VEXT(drm_emgd_dc) || IGD_DC_EXTENDED(drm_emgd_dc)) {
+				/* Use validated value to override compile-time value: */
+				config_drm.dc = drm_emgd_dc;
+			} else if (IGD_DC_TWIN(drm_emgd_dc)) {
+				/* Use validated value to override compile-time value: */
+				EMGD_DEBUG("Module parameter \"dc\" contains unsupported value "
+						"%d.", drm_emgd_dc);
+				EMGD_DEBUG("Overriding and making it 1 (single display).");
+				drm_emgd_dc = 1;
+				config_drm.dc = drm_emgd_dc;
+			} else {
+				/* Use compile-time value: */
+				EMGD_ERROR("Module parameter \"dc\" contains invalid value "
+					"%d (must be 1, 2, 5 or 8).", drm_emgd_dc);
+				if (config_drm.dc == 4) {
+					EMGD_DEBUG("Compile-time setting for module parameter "
+						"\"dc\" contains unsupported value %d.", config_drm.dc);
+					EMGD_DEBUG("Overriding and making it 1 (single display).");
+
+					config_drm.dc = 1;
+				} else {
+					EMGD_ERROR("Will use compile-time setting %d instead "
+						"of invalid value %d.\n", config_drm.dc, drm_emgd_dc);
+				}
+				drm_emgd_dc = config_drm.dc;
+			}
+		} else {
+			/* Check and potentially use the compile-time value: */
+			if (IGD_DC_SINGLE(config_drm.dc) || IGD_DC_CLONE(config_drm.dc) ||
+				IGD_DC_VEXT(config_drm.dc) ||
+				IGD_DC_EXTENDED(config_drm.dc)) {
+				/* Report the compile-time value: */
+				EMGD_DEBUG("Using compile-time setting for the module parameter"
+						" \"dc\" = \"%d\"", config_drm.dc);
+			} else if (IGD_DC_TWIN(config_drm.dc)) {
+				EMGD_DEBUG("Compile-time setting for module parameter "
+						"\"dc\" contains unsupported value %d.", config_drm.dc);
+				EMGD_DEBUG("Overriding and making it 1 (single display).");
+				config_drm.dc = 1;
+			} else {
+				EMGD_DEBUG("Compile-time setting for module parameter "
+						"\"dc\" contains invalid value %d.", config_drm.dc);
+				EMGD_DEBUG("Must be 1, 2, 5 or 8.  Making it 1 (single"
+						" display).");
+				config_drm.dc = 1;
+			}
+			drm_emgd_dc = config_drm.dc;
+		}
+
+		/*************************************
+		 * Get the desired display "width":
+		 *************************************/
+		if (-1 != drm_emgd_width) {
+			/* Override the compile-time value with the module parameter: */
+			EMGD_DEBUG("Using the \"width\" module parameter: \"%d\"",
+					drm_emgd_width);
+			config_drm.width = drm_emgd_width;
+		} else {
+			/* Use the compile-time value: */
+			drm_emgd_width = config_drm.width;
+			EMGD_DEBUG("Using the compile-time \"width\" value: \"%d\"",
+					drm_emgd_width);
+		}
+
+		/*************************************
+		 * Get the desired display "height":
+		 *************************************/
+		if (-1 != drm_emgd_height) {
+			/* Override the compile-time value with the module parameter: */
+			EMGD_DEBUG("Using the \"height\" module parameter: \"%d\"",
+					drm_emgd_height);
+			config_drm.height = drm_emgd_height;
+		} else {
+			/* Use the compile-time value: */
+			drm_emgd_height = config_drm.height;
+			EMGD_DEBUG("Using the compile-time \"height\" value: \"%d\"",
+					drm_emgd_height);
+		}
+
+		/*************************************
+		 * Get the desired display "refresh":
+		 *************************************/
+		if (-1 != drm_emgd_refresh) {
+			/* Override the compile-time value with the module parameter: */
+			EMGD_DEBUG("Using the \"refresh\" module parameter: \"%d\"",
+					drm_emgd_refresh);
+			config_drm.refresh = drm_emgd_refresh;
+		} else {
+			/* Use the compile-time value: */
+			drm_emgd_refresh = config_drm.refresh;
+			EMGD_DEBUG("Using the compile-time \"refresh\" value: \"%d\"",
+					drm_emgd_refresh);
+		}
+	}
+
+
+	if (config_drm.kms) {
+		priv->num_crtc = 2;
+
+
+		/*************************************
+		 * Initialize kernel mode setting functionality
+		 *************************************/
+		emgd_modeset_init(priv->ddev);
+
+		/* Get Display context */
+		drm_HAL_context->mod_dispatch.dsp_get_dc(NULL,
+							(igd_display_context_t **) &primary,
+							(igd_display_context_t **) &secondary);
+
+
+		/*************************************
+		 * Initialize primary_fb_info
+		 *************************************/
+		framebuffer = list_entry(priv->ddev->mode_config.fb_list.next,
+				struct drm_framebuffer, head);
+		if (!framebuffer) {
+			EMGD_ERROR("Can't display splash screen/video as there is no fb.");
+		} else {
+			emgd_fb = container_of(framebuffer, emgd_framebuffer_t, base);
+			primary_fb_info = &priv->initfb_info;
+			if (priv->fbdev) {
+				fb = priv->fbdev->screen_base;
+			}
+		}
+
+
+		/*
+		 * Update the private data structure
+		 */
+		priv->primary               = primary;
+		priv->secondary             = secondary;
+		priv->primary_port_number   = IGD_DC_PRIMARY(priv->dc);
+		priv->secondary_port_number = IGD_DC_SECONDARY(priv->dc);
+	} else {
+
+		/**************************************************************************
+		 * Special case handling: Since HAL doesn't know anything about Vertical
+		 * extended mode, if we are in Vertical Extended (5), send HAL asking for
+		 * (2)
+		 *************************************************************************/
+		temp_dc = drm_emgd_dc;
+		if(IGD_DC_VEXT(drm_emgd_dc)) {
+			temp_dc = IGD_DISPLAY_CONFIG_CLONE;
+		}
+
+		/*************************************
+		 * Query the DC list (use first one):
+		 *************************************/
+		EMGD_DEBUG("Calling query_dc()");
+		err = drm_HAL_dispatch->query_dc(drm_HAL_handle, temp_dc,
+				&desired_dc, IGD_QUERY_DC_INIT);
+		EMGD_DEBUG("query_dc() returned %d", err);
+		if (err) {
+			EMGD_ERROR_EXIT("Cannot initialize the display as requested.\n"
+					"The query_dc() function returned %d.", err);
+			return;
+		}
+		port_number = (*desired_dc & 0xf0) >> 4;
+		EMGD_DEBUG("Using DC 0x%lx with port number %d",
+				*desired_dc, port_number);
+		if(IGD_DC_VEXT(drm_emgd_dc)) {
+			drm_emgd_dc = (*desired_dc & ~IGD_DISPLAY_CONFIG_CLONE) |
+					IGD_DISPLAY_CONFIG_VEXT;
+		}
+
+		/*************************************
+		 * Query the mode list:
+		 *************************************/
+		EMGD_DEBUG("Calling query_mode_list()");
+		err = drm_HAL_dispatch->query_mode_list(drm_HAL_handle, *desired_dc,
+				&mode_list, mode_flags);
+		EMGD_DEBUG("query_mode_list() returned %d", err);
+		if (err) {
+			EMGD_ERROR_EXIT("Cannot initialize the display as requested\n"
+					"The query_mode_list() function returned %d.", err);
+			return;
+		}
+
+
+		/*************************************
+		 * Find the desired mode from the list:
+		 *************************************/
+		EMGD_DEBUG("Comparing the mode list with the desired width, height, and"
+			" refresh rate...");
+
+		mode = mode_list;
+		while (mode && (mode->width != IGD_TIMING_TABLE_END)) {
+			EMGD_DEBUG(" ...Found a mode with width=%d, height=%d, refresh=%d;",
+					mode->width, mode->height, mode->refresh);
+			if ((mode->width == drm_emgd_width) &&
+					(mode->height == drm_emgd_height) &&
+					(mode->refresh == drm_emgd_refresh)) {
+				EMGD_DEBUG("     ... This mode is a match!");
+				desired_mode = mode;
+				break;
+			}
+			mode++;
+		}
+		if (NULL == desired_mode) {
+			EMGD_ERROR("Cannot initialize the display as requested.");
+			EMGD_ERROR("No mode matching the desired width (%d), height "
+					"(%d), and refresh rate (%d) was found.",
+					drm_emgd_width, drm_emgd_height, drm_emgd_refresh);
+			return;
+		} else {
+			/* Must set this in order to get the timings setup: */
+			desired_mode->flags |= IGD_DISPLAY_ENABLE;
+		}
+
+		/*************************************
+		 * Call alter_displays():
+		 *************************************/
+		primary_fb_info   = kzalloc(sizeof(igd_framebuffer_info_t), GFP_KERNEL);
+		secondary_fb_info = kzalloc(sizeof(igd_framebuffer_info_t), GFP_KERNEL);
+		primary_fb_info->width = desired_mode->width;
+
+		/*************************************
+		 * Special for Vertical Extended, double the height
+		 *************************************/
+		if(IGD_DC_VEXT(drm_emgd_dc)) {
+			primary_fb_info->height = desired_mode->height * 2;
+		} else {
+			primary_fb_info->height = desired_mode->height;
+		}
+		primary_fb_info->pixel_format = IGD_PF_ARGB32;
+		primary_fb_info->flags = 0;
+		primary_fb_info->allocated = 0;
+		memcpy(secondary_fb_info, primary_fb_info,
+				sizeof(igd_framebuffer_info_t));
+
+		EMGD_DEBUG("Calling alter_displays()");
+		err = drm_HAL_dispatch->alter_displays(drm_HAL_handle,
+				&primary, desired_mode, primary_fb_info,
+				&secondary, desired_mode, secondary_fb_info,
+				*desired_dc, 0);
+		EMGD_DEBUG("alter_displays() returned %d", err);
+		if (err) {
+			EMGD_ERROR_EXIT("Cannot initialize the display as requested.\n"
+					"The alter_displays() function returned %d.", err);
+			return;
+		}
+
+		/*
+		 * Update the private data structure with the values we get
+		 * back from alter displays.
+		 */
+		priv->dc                    = *desired_dc;
+		priv->primary               = primary;
+		priv->secondary             = secondary;
+		priv->primary_port_number   = IGD_DC_PRIMARY(*desired_dc);
+		priv->secondary_port_number = IGD_DC_SECONDARY(*desired_dc);
+
+		/*************************************
+		 * Special for Vertical Extended, pan the second display
+		 *************************************/
+		if(IGD_DC_VEXT(drm_emgd_dc)) {
+			drm_HAL_dispatch->pan_display(secondary, 0,
+					secondary_fb_info->height / 2);
+		}
+
+		/*************************************
+		 * Call get_display():
+		 *************************************/
+		EMGD_DEBUG("Calling get_display()");
+		err = drm_HAL_dispatch->get_display(primary, port_number,
+				primary_fb_info, &pt_info, 0);
+		EMGD_DEBUG("get_display() returned %d", err);
+		if (err) {
+			EMGD_ERROR_EXIT("Cannot initialize the display as requested\n"
+					"The get_display() function returned %d.", err);
+			return;
+		}
+
+		/*************************************
+		 * Get FB virtual address
+		 *************************************/
+		EMGD_DEBUG("Calling full_clear_fb()");
+		fb = mode_context->context->dispatch.gmm_map(
+				primary_fb_info->fb_base_offset);
+	}
+
+	if (fb) {
+
+		/*************************************
+		 * Set the framebuffer to the background color:
+		 *************************************/
+		temp_bg_color = mode_context->display_color;
+		mode_context->display_color = config_drm.ss_data->bg_color;
+		full_clear_fb(mode_context, primary_fb_info, fb);
+		mode_context->display_color = temp_bg_color;
+
+		/*************************************
+		 * Display a splash screen if requested by user
+		 *************************************/
+		if(config_drm.ss_data->width &&
+				config_drm.ss_data->height) {
+
+			/* Display a splash screen */
+			printk(KERN_ERR "[EMGD] Display splash screen image.\n");
+			EMGD_DEBUG("Calling disp_splash_screen()");
+			display_splash_screen(primary_fb_info, fb, config_drm.ss_data);
+		}
+
+		/*************************************
+		 * Display a splash video if requested by user
+		 *************************************/
+		if(config_drm.sv_data->pixel_format &&
+				config_drm.sv_data->src_width &&
+				config_drm.sv_data->src_height &&
+				config_drm.sv_data->src_pitch &&
+				config_drm.sv_data->dst_width &&
+				config_drm.sv_data->dst_height) {
+
+			/* Display a splash video */
+			EMGD_DEBUG("Calling disp_splash_video()");
+			disp_splash_video(config_drm.sv_data);
+		}
+	} else {
+		EMGD_ERROR("framebuffer base address is 0");
+	}
+
+	if (!config_drm.kms) {
+		mode_context->context->dispatch.gmm_unmap(fb);
+	}
+	EMGD_TRACE_EXIT;
+} /* emgd_init_display() */
+
+
+
+/**
+ * Function to display a splash video to the user. The splash video data must be
+ * provided to the kernel mode driver by another entity (like a driver or FPGA
+ * HW). Splash video will be display after setting the mode (if requested by
+ * the user through config options).
+ *
+ * @param sv_data (IN) a non null pointer to splash video information like
+ * width, height etc.
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int disp_splash_video(emgd_drm_splash_video_t *sv_data)
+{
+	igd_surface_t surface;
+	igd_rect_t ovl_rect, surf_rect;
+	igd_ovl_info_t ovl_info;
+	unsigned int tmp;
+
+	surface.offset       = sv_data->offset;
+	surface.pitch        = sv_data->src_pitch;
+	surface.width        = sv_data->src_width;
+	surface.height       = sv_data->src_height;
+	surface.pixel_format = sv_data->pixel_format;
+	surface.flags        = IGD_SURFACE_OVERLAY;
+
+	/* Set the surface rect as big as the video frame size */
+	surf_rect.x1 = 0;
+	surf_rect.y1 = 0;
+	surf_rect.x2 = sv_data->src_width;
+	surf_rect.y2 = sv_data->src_height;
+
+
+	/* The x,y postion of the sprite c */
+	ovl_rect.x1 = sv_data->dst_x;
+	ovl_rect.y1 = sv_data->dst_y;
+
+	/*
+	 *  NOTE: This for scaling if the hardware supports it.
+	 *  If no dest w x h values are set,set it the the
+	 *  src w x h
+	 */
+	tmp = sv_data->dst_width ? sv_data->dst_width : sv_data->src_width;
+
+	ovl_rect.x2 = ovl_rect.x1 + tmp;
+
+	tmp = sv_data->dst_height?
+		sv_data->dst_height:
+	sv_data->src_height;
+
+	ovl_rect.y2 = ovl_rect.y1 + tmp;
+
+	/*
+	 *  If no values are set, set it to default
+	 */
+	ovl_info.video_quality.brightness =
+		config_drm.ovl_brightness ?
+		config_drm.ovl_brightness : 0x8000;
+	ovl_info.video_quality.contrast =
+		config_drm.ovl_contrast ?
+		config_drm.ovl_contrast : 0x8000;
+	ovl_info.video_quality.saturation =
+		config_drm.ovl_saturation ?
+		config_drm.ovl_saturation : 0x8000;
+
+	/*
+	 * If any values are set for gamma, turn on the gamma flags
+	 */
+	ovl_info.gamma.red   = config_drm.ovl_gamma_red;
+	ovl_info.gamma.green = config_drm.ovl_gamma_green;
+	ovl_info.gamma.blue  = config_drm.ovl_gamma_blue;
+
+	if(ovl_info.gamma.red || ovl_info.gamma.green || ovl_info.gamma.blue) {
+
+		ovl_info.gamma.flags = IGD_OVL_GAMMA_ENABLE;
+	}
+
+	drm_HAL_dispatch->alter_ovl(drm_HAL_handle, NULL,
+		&surface,
+		&surf_rect,
+		&ovl_rect,
+		&ovl_info, IGD_OVL_ALTER_ON);
+
+	return 0;
+}
+
+
+
+/**
+ * This is the drm_driver.load() function.  It is called when the DRM "loads"
+ * (i.e. when our driver loads, it calls drm_init(), which eventually causes
+ * this driver function to be called).
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param flags (IN) The last member of the pci_device_id struct that we
+ * fill-in at the top of this file (e.g. CHIP_PSB_8108).
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int emgd_driver_load(struct drm_device *dev, unsigned long flags)
+{
+	int i, err = 0;
+	igd_param_t *params;
+	drm_emgd_priv_t *priv = NULL;
+	igd_init_info_t *init_info;
+	int num_hal_params =
+		sizeof(config_drm.hal_params) / sizeof(config_drm.hal_params[0]);
+
+
+	EMGD_TRACE_ENTER;
+
+	mutex_lock(&dev->struct_mutex);
+
+	/**************************************************************************
+	 *
+	 * Get the compile-time/module-parameter "params" before initializing the
+	 * HAL:
+	 *
+	 **************************************************************************/
+
+	/*************************************
+	 * Take into account the "configid" module parameter:
+	 *************************************/
+	if (num_hal_params <= 0) {
+		EMGD_ERROR("The compile-time configuration (in \"user_config.c\")\n"
+			"contains no igd_param_t structures.  Please fix and recompile.");
+		mutex_unlock(&dev->struct_mutex);
+		return -EINVAL;
+	}
+	if ((drm_emgd_configid == 0) ||
+		(drm_emgd_configid > num_hal_params)) {
+		EMGD_ERROR("Module parameter \"configid\" contains invalid value "
+			"%d.\nMust specify a compile-time configuration (in "
+			"\"user_config.c\"),\nnumbered between 1 and %d.\n",
+			drm_emgd_configid, num_hal_params);
+		mutex_unlock(&dev->struct_mutex);
+		return -EINVAL;
+	}
+
+	/* Obtain the user-configurable set of parameter values: */
+	if (drm_emgd_configid < 0) {
+		params = config_drm.hal_params[0];
+	} else {
+		params = config_drm.hal_params[drm_emgd_configid-1];
+	}
+
+
+	/*************************************
+	 * Take into account the "portorder" module parameter:
+	 *************************************/
+	err = 0;
+	EMGD_DEBUG("Determining desired port order:");
+	if (0 == drm_emgd_numports) {
+		/* Set this to 1 so we use the compile-time value below: */
+		err = 1;
+		drm_emgd_numports = IGD_MAX_PORTS;
+	} else if (drm_emgd_numports != IGD_MAX_PORTS) {
+		EMGD_ERROR("Module parameter \"portorder\" specifies %d ports "
+			"(must specify %d in a comma-separated list).",
+			drm_emgd_numports, IGD_MAX_PORTS);
+		drm_emgd_numports = IGD_MAX_PORTS;
+		err = -EINVAL;
+	}
+	if (!err) {
+		/* Validate each port within the module parameter: */
+		for (i = 0 ; i < drm_emgd_numports ; i++) {
+			if ((drm_emgd_portorder[i] < 0) ||
+				(drm_emgd_portorder[i] > 5)) {
+				EMGD_ERROR("Item %d in module parameter \"portorder\" "
+					"contains invalid value %d (must be between 0 and 5).",
+					i, drm_emgd_portorder[i]);
+				err = -EINVAL;
+			}
+		}
+	}
+	if (!err) {
+		/* Override the compile-time value with the module parameter: */
+		for (i = 0 ; i < drm_emgd_numports ; i++) {
+			params->port_order[i] = drm_emgd_portorder[i];
+		}
+		EMGD_DEBUG("Using the \"portorder\" module parameter: \"%d, %d, %d, "
+			"%d, %d\"", drm_emgd_portorder[0], drm_emgd_portorder[1],
+			drm_emgd_portorder[2], drm_emgd_portorder[3],
+			drm_emgd_portorder[4]);
+	} else {
+		/* Use the compile-time value: */
+		for (i = 0 ; i < drm_emgd_numports ; i++) {
+			drm_emgd_portorder[i] = params->port_order[i];
+		}
+		EMGD_DEBUG("Using the compile-time \"portorder\" value: \"%d, %d, "
+			"%d, %d, %d\"", drm_emgd_portorder[0], drm_emgd_portorder[1],
+			drm_emgd_portorder[2], drm_emgd_portorder[3],
+			drm_emgd_portorder[4]);
+		err = 0;
+	}
+
+
+	emgd_print_params(params);
+
+
+	/**************************************************************************
+	 *
+	 * Minimally initialize and configure the driver, deferring as much as
+	 * possible until the X driver starts.:
+	 *
+	 **************************************************************************/
+
+	/* Determine whether display initialization is desired: */
+	if (-1 != drm_emgd_init) {
+		/* Validate and potentially use the module parameter: */
+		if (!((drm_emgd_init == 1) || (drm_emgd_init == 0))) {
+			EMGD_ERROR("Module parameter \"init\" contains invalid "
+				"value %d (must be 0 or 1).", drm_emgd_init);
+			drm_emgd_init = config_drm.init;
+			EMGD_ERROR("Using the compile-time \"init\" value: \"%d\"",
+				drm_emgd_init);
+		} else {
+			/* Override the compile-time value with the module parameter: */
+			config_drm.init = drm_emgd_init;
+			EMGD_DEBUG("Using the \"init\" module parameter: \"%d\"",
+				drm_emgd_init);
+		}
+	} else {
+		/* Use the compile-time value: */
+		drm_emgd_init = config_drm.init;
+		EMGD_DEBUG("Using the compile-time \"init\" value: \"%d\"",
+			drm_emgd_init);
+	}
+
+
+	/*
+	 * In order for some early ioctls (e.g. emgd_get_chipset_info()) to work,
+	 * we must do the following minimal initialization.
+	 */
+	EMGD_DEBUG("Calling igd_driver_init()");
+	init_info = (igd_init_info_t *)OS_ALLOC(sizeof(igd_init_info_t));
+	drm_HAL_handle = igd_driver_init(init_info);
+	if (drm_HAL_handle == NULL) {
+		OS_FREE(init_info);
+		mutex_unlock(&dev->struct_mutex);
+		return -ENOMEM;
+	}
+
+	/* Get the HAL context and give it to the ioctl-handling "bridge" code: */
+	drm_HAL_context = (igd_context_t *) drm_HAL_handle;
+	emgd_set_real_handle(drm_HAL_handle);
+
+	/* Save the drm dev pointer, it's needed by igd_module_init */
+	drm_HAL_context->drm_dev = dev;
+
+	/* Create the private structure used to communicate to the IMG 3rd-party
+	 * display driver:
+	 */
+	priv = OS_ALLOC(sizeof(drm_emgd_priv_t));
+	if (NULL == priv) {
+		OS_FREE(init_info);
+		mutex_unlock(&dev->struct_mutex);
+		return -ENOMEM;
+	}
+
+
+	OS_MEMSET(priv, 0, sizeof(drm_emgd_priv_t));
+	priv->hal_running = 0;
+	priv->context     = drm_HAL_context;
+	priv->init_info   = init_info;
+	priv->qb_seamless = params->qb_seamless;
+	dev->dev_private  = priv;
+	priv->ddev        = dev;
+	priv->kms_enabled = 0;
+
+
+	/* Do basic driver initialization & configuration: */
+	EMGD_DEBUG("Calling igd_driver_config()");
+	err = igd_driver_config(drm_HAL_handle);
+	if (err != 0) {
+		OS_FREE(init_info);
+		OS_FREE(priv);
+		OS_FREE(drm_HAL_handle);
+		mutex_unlock(&dev->struct_mutex);
+		return -EIO;
+	}
+
+	// PVRSRVDrmLoad() sets up an ISR routine with a pointer to drm_device to be passed every time.  This variable (gpDrmDevice) is initialized in msvdx_pre_init_plb only.    
+	// Due to this reason, msvdx_pre_init_plb() is moved before PVRSRVDrmLoad().
+
+	/* Init MSVDX and load firmware */
+	msvdx_pre_init_plb(dev);
+
+	/* Initialize the PVR services if not already initialized */
+	printk(KERN_INFO "Initializing PVR Services.\n");
+	PVRSRVDrmLoad(dev, 0);
+
+	/* Decide if we can defer the rest of the initialization */
+	if (config_drm.init) {
+
+		if (config_drm.kms) {
+			params->preserve_regs = 1;
+			priv->kms_enabled = 1;
+		}
+
+		/* Initialize and configure the driver now */
+		err = emgd_startup_hal(dev, params);
+		if (err != 0) {
+			OS_FREE(init_info);
+			OS_FREE(priv);
+			OS_FREE(drm_HAL_handle);
+			mutex_unlock(&dev->struct_mutex);
+			return err;
+		}
+
+		/* This will get the plane, pipe and port register values and fill up the
+		 * fw_info data structure. This needs to be done at INIT time before the
+		 * user-mode driver loads
+		 */
+		get_pre_driver_info(mode_context);
+
+		/* Per the user's request, initialize the display: */
+		emgd_init_display(TRUE, priv);
+	}
+
+
+
+#ifdef DEBUG_BUILD_TYPE
+	/* Turn on KMS debug messages in the general DRM module if our OAL
+	 * messages are on and it's a debug driver.
+	 */
+	if (emgd_debug->hal.oal) {
+		drm_debug |= DRM_UT_KMS;
+	}
+#endif
+
+	/* can not work out how to start PVRSRV */
+	/* Load Buffer Class Module*/
+	emgd_bc_ts_init();
+
+	mutex_unlock(&dev->struct_mutex);
+	EMGD_TRACE_EXIT;
+
+	return 0;
+} /* emgd_driver_load() */
+
+
+/**
+ * This is the drm_driver.unload() function.  It is called when the DRM
+ * "unloads."  That is, drm_put_dev() (in "drm_stub.c"), which is called by
+ * drm_exit() (in "drm_drv.c"), calls this function.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int emgd_driver_unload(struct drm_device *dev)
+{
+	drm_emgd_priv_t *priv = dev->dev_private;
+	unsigned long save_flags = 0;
+
+	EMGD_TRACE_ENTER;
+
+	mutex_lock(&dev->struct_mutex);
+
+	/* Unload Buffer Class Module*/
+	emgd_bc_ts_uninit();
+
+	PVRSRVDrmUnload(dev);
+
+	/* KMS cleanup */
+	if (config_drm.init && config_drm.kms) {
+		emgd_modeset_destroy(dev);
+	}
+
+	if (priv->hal_running) {
+		/* igd_driver_shutdown() will restore the then-currently-saved register
+		 * state.  We can't rely on any save_restore() calls before that time,
+		 * because igd_driver_shutdown() does things that mess up the register
+		 * state.  Thus, we must allow it to do a restore.  The only way to
+		 * control the final state of the hardware is to potentially do a
+		 * save_restore now.  Thus, if the X server's state is currently saved
+		 * (i.e. the console's state is currently active), we must do a
+		 * save_restore now, so that this state will still exist after
+		 * igd_driver_shutdown().
+		 */
+		if (priv->saved_registers == CONSOLE_STATE_SAVED) {
+			EMGD_DEBUG("Need to restore the console's saved register state");
+
+			save_flags = IGD_REG_SAVE_ALL;
+			drm_HAL_dispatch->driver_save_restore(drm_HAL_handle, save_flags);
+			EMGD_DEBUG("State of saved registers is X_SERVER_STATE_SAVED");
+			priv->saved_registers = X_SERVER_STATE_SAVED;
+		}
+		igd_driver_shutdown_hal(drm_HAL_handle);
+		igd_driver_shutdown(drm_HAL_handle);
+	} else {
+		/* Do safe cleanup that would've been done by igd_driver_shutdown() */
+		igd_driver_shutdown(drm_HAL_handle);
+	}
+
+	if (!config_drm.kms) {
+		kfree(primary_fb_info);
+		kfree(secondary_fb_info);
+	}
+
+	OS_FREE(priv->init_info);
+	OS_FREE(priv);
+
+	/* Note: This macro is #define'd in "oal/os/memory.h" */
+#ifdef INSTRUMENT_KERNEL_ALLOCS
+	emgd_report_unfreed_memory();
+#endif
+
+	mutex_unlock(&dev->struct_mutex);
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+} /* emgd_driver_unload() */
+
+
+/**
+ * This is the drm_driver.open() function.  It is called when a user-space
+ * process opens the DRM device file.  The DRM drm_open() (in "drm_fops.c")
+ * calls drm_open_helper(), which calls this function.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param priv (IN) DRM's file private data struct (in "drmP.h")
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int emgd_driver_open(struct drm_device *dev, struct drm_file *priv)
+{
+	int ret = 0;
+	drm_emgd_priv_t *emgd_priv = dev->dev_private;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Under the latest MeeGo images, something is trying to open the DRM device
+	 * while we're still inside the the load() function (possibly an updated
+	 * copy of udev?).  Don't let open() calls through here until
+	 * initialization is finished.  If userspace can overlap load() and open(),
+	 * then it stands to reason that we might also wind up racing with close()
+	 * and unload() as well, so let's protect all of those operations by
+	 * grabbing the mutex.
+	 *
+	 * FIXME: Should we do the same for other operations like suspend/resume/etc?
+	 */
+	mutex_lock(&dev->struct_mutex);
+
+	/* The is_master flag is set after the call to this function, so there needs
+	 * to be manual check to determine the DRM master */
+	if(priv->is_master || (!priv->minor->master && !emgd_priv->drm_master_fd)) {
+		emgd_priv->drm_master_fd = priv;
+	}
+
+	ret = PVRSRVOpen(dev, priv);
+
+	mutex_unlock(&dev->struct_mutex);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* emgd_driver_open() */
+
+
+/**
+ * This is the drm_driver.lastclose() function.  It is called when the last
+ * user-space process closes/releases the DRM device file.  At end of DRM
+ * drm_release() (in "drm_fops.c"), it calls drm_lastclose() (in "drm_drv.c"),
+ * which calls this function.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ */
+void emgd_driver_lastclose(struct drm_device *dev)
+{
+	drm_emgd_priv_t *priv = dev->dev_private;
+	igd_init_info_t *init_info = priv->init_info;
+	int err = 0;
+	unsigned long restore_flags = 0;
+
+	EMGD_TRACE_ENTER;
+
+	mutex_lock(&dev->struct_mutex);
+
+
+	if (priv->hal_running) {
+		if (config_drm.init) {
+			if( x_started ) {
+
+				restore_flags = (IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT)
+									| IGD_REG_SAVE_TYPE_REG;
+
+				if (!config_drm.kms) {
+					restore_flags &= ~IGD_REG_SAVE_RB;
+				}
+
+
+				if (priv->saved_registers == CONSOLE_STATE_SAVED) {
+					EMGD_DEBUG("Need to restore the console's saved "
+						"register state");
+					drm_HAL_dispatch->driver_save_restore(drm_HAL_handle,
+															restore_flags);
+					EMGD_DEBUG("State of saved registers is "
+						"X_SERVER_STATE_SAVED");
+					priv->saved_registers = X_SERVER_STATE_SAVED;
+				}
+
+				if (priv->saved_registers == X_SERVER_STATE_SAVED &&
+					!config_drm.kms && !priv->qb_seamless) {
+					emgd_init_display(FALSE, priv);
+				}
+
+				if (priv->saved_registers == CONSOLE_STATE_SAVED) {
+					EMGD_DEBUG("Need to restore the console's saved register "
+						"state");
+					drm_HAL_dispatch->driver_save_restore(drm_HAL_handle,
+															restore_flags);
+					EMGD_DEBUG("State of saved registers is X_SERVER_STATE_SAVED");
+					priv->saved_registers = X_SERVER_STATE_SAVED;
+				}
+
+				/* Since an alter_displays() was done, re-init the 3DD: */
+				if (priv->reinit_3dd) {
+					priv->reinit_3dd(dev);
+				}
+
+				x_started = false;
+			}
+		} else {
+			/* The X server has quit/crashed.  If the console's state is
+			 * currently saved (likely) restore that state, so that the console
+			 * can be seen and work.
+			 */
+			context_count = 0;
+			priv->dc = 0; /* Don't let the 3DD re-init too early: */
+			if (priv->saved_registers == CONSOLE_STATE_SAVED) {
+				EMGD_DEBUG("Need to restore the console's saved register "
+					"state");
+				restore_flags = IGD_REG_SAVE_ALL;
+
+				drm_HAL_dispatch->driver_save_restore(drm_HAL_handle,
+														restore_flags);
+				EMGD_DEBUG("State of saved registers is X_SERVER_STATE_SAVED");
+				priv->saved_registers = X_SERVER_STATE_SAVED;
+			}
+
+			/******************************************************************
+			 * Shutdown and minimally-restart the HAL, so that if/when the X
+			 * server starts again, the HAL will be started again.  This will
+			 * allow the necessary port drivers to be loaded, all configuration
+			 * parameters to be used, the EDID to be read again (e.g. because
+			 * the user may have switched monitors), etc.
+			 *
+			 * However, keeep the DRM driver state around, so that the driver
+			 * can continue to work (i.e. re-enter the state when the DRM
+			 * driver was first loaded).
+			 *
+			 * Also, keep the PVR services up and going, since it wasn't
+			 * designed to be shutdown and restarted (without doing an
+			 * rmmod/insmod).
+			 ******************************************************************/
+			EMGD_DEBUG("Shutting down the HAL");
+
+			/* igd_driver_shutdown() will restore the then-currently-saved
+			 * register state.  We can't rely on any save_restore() calls
+			 * before that time, because igd_driver_shutdown() does things that
+			 * mess up the register state.  Thus, we must allow it to do a
+			 * restore.  The only way to control the final state of the
+			 * hardware is to potentially do a save_restore now.  Thus, if the
+			 * X server's state is currently saved (i.e. the console's state is
+			 * currently active), we must do a save_restore now, so that this
+			 * state will still exist after igd_driver_shutdown().
+			 */
+			if (priv->saved_registers == X_SERVER_STATE_SAVED) {
+				EMGD_DEBUG("Need to restore the console's saved register "
+					"state");
+				restore_flags = IGD_REG_SAVE_ALL;
+				drm_HAL_dispatch->driver_save_restore(drm_HAL_handle,
+														restore_flags);
+				EMGD_DEBUG("State of saved registers is X_SERVER_STATE_SAVED");
+				priv->saved_registers = CONSOLE_STATE_SAVED;
+			}
+			msvdx_shutdown_plb(drm_HAL_handle);
+			igd_driver_shutdown_hal(drm_HAL_handle);
+			igd_driver_shutdown(drm_HAL_handle);
+
+			EMGD_DEBUG("Minimally restarting the HAL--like load-time");
+			/*
+			 * In order for some early ioctls (e.g. emgd_get_chipset_info()) to
+			 * work, we must do the following minimal initialization.
+			 */
+			EMGD_DEBUG("Calling igd_driver_init()");
+			if(init_info == NULL){
+				init_info = (igd_init_info_t *)OS_ALLOC(sizeof(igd_init_info_t));
+			}
+			drm_HAL_handle = igd_driver_init(init_info);
+			if (drm_HAL_handle == NULL) {
+				/* This shouldn't happen, but if it does, alert the user, as
+				 * the only thing to do is to rmmod/insmod emgd.ko, or to
+				 * reboot:
+				 */
+				OS_FREE(init_info);
+				printk(KERN_ALERT "[EMGD] Failed to restart the EMGD graphics "
+						"HAL\n");
+				mutex_unlock(&dev->struct_mutex);
+				return;
+			}
+
+			/* Get the HAL context and give it to the ioctl-handling "bridge"
+			 * code:
+			 */
+			drm_HAL_context = (igd_context_t *) drm_HAL_handle;
+			emgd_set_real_handle(drm_HAL_handle);
+
+			/* Save the drm dev pointer, it's needed by igd_module_init */
+			drm_HAL_context->drm_dev = dev;
+
+			/* Reset part of the private structure used to communicate to the
+			 * IMG 3rd-party display driver:
+			 */
+			priv->saved_registers = 0;
+			priv->suspended_state = NULL;
+			priv->must_power_on_ports = 0;
+			priv->xserver_running = 0;
+			priv->primary_port_number = 0;
+			priv->primary = NULL;
+			priv->secondary_port_number = 0;
+			priv->secondary = NULL;
+			priv->msvdx_private = NULL;
+			priv->hal_running = 0;
+			priv->context = drm_HAL_context;
+			priv->init_info = init_info;
+
+
+			/* Do basic driver initialization & configuration: */
+			EMGD_DEBUG("Calling igd_driver_config()");
+			err = igd_driver_config(drm_HAL_handle);
+			if (err != 0) {
+				/* This shouldn't happen, but if it does, alert the user, as
+				 * the only thing to do is to rmmod/insmod emgd.ko, or to
+				 * reboot:
+				 */
+				OS_FREE(init_info);
+				printk(KERN_ALERT "[EMGD] Failed to restart the EMGD graphics "
+						"HAL.\n");
+				mutex_unlock(&dev->struct_mutex);
+				return;
+			}
+
+			/* To ensure the devinfo->interrupt_h is NULL, call the
+			 * following:
+			 */
+			if (priv->reinit_3dd) {
+				priv->reinit_3dd(dev);
+			}
+		}
+	}
+
+	mutex_unlock(&dev->struct_mutex );
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_driver_lastclose() */
+
+
+/**
+ * This is the drm_driver.preclose() function.  It is called when a user-space
+ * process closes/releases the DRM device file.  At the very start of DRM
+ * drm_release() (in "drm_fops.c"), it calls this function, before it does any
+ * real work (other than get the lock).
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param priv (IN) DRM's file private data struct (in "drmP.h")
+ */
+void emgd_driver_preclose(struct drm_device *dev, struct drm_file *priv)
+{
+	drm_emgd_priv_t *emgd_priv = dev->dev_private;
+
+	/* Notes on what to implement in this function.  What this
+	 * function does is largely influenced by when/why this can be called:
+	 *
+	 * - We can determine whether the connection was for the X driver vs. for a
+	 *   3D application, because the X driver should be the master.
+	 *
+	 *   - If there's any state management we need to take care of (e.g. if the
+	 *     X server crashes while there are still 3D app's running), this is
+	 *     probably a good place to do it.
+	 *
+	 *   - Normal 3D app shutdown is probably best done here too (simply
+	 *     because we can tell it's a 3D connection, and do our work before the
+	 *     general drm_release() code does its work.
+	 *
+	 *   - We don't yet know what the IMG 3D code is going to need.
+	 *
+	 * We don't yet know what we need to do for the IMG code.  Thus, I'm
+	 * inclined to leave this function a stub for now.
+	 */
+
+	EMGD_TRACE_ENTER;
+	mutex_lock(&dev->struct_mutex);
+	if ((emgd_priv->hal_running) && priv->is_master && drm_HAL_dispatch) {
+		/* The X server can't call gmm_cache_flush() nor igd_driver_shutdown()
+		 * after DRICloseScreen() closes the connection with the DRM.  However,
+		 * we can tell on this side of the connection (because the X server is
+		 * the master) that it is closing down.  There is no need to truly shut
+		 * down the HAL, but we can flush the GMM cache and reset the display
+		 * hardware to a known state/mode:
+		 */
+		drm_HAL_dispatch->gmm_flush_cache();
+
+		/* TODO - WRITE CODE THAT PUTS THE DISPLAY HW IN A KNOWN STATE/MODE */
+	}
+
+	if (emgd_priv->drm_master_fd == priv)
+		emgd_priv->drm_master_fd = NULL;
+
+	mutex_unlock(&dev->struct_mutex);
+	EMGD_TRACE_EXIT;
+
+	/* TODO -- ADD ANYTHING WE DISCOVER WE NEED AFTER THE IMG TRAINING */
+
+} /* emgd_driver_preclose() */
+
+
+/**
+ * This is the drm_driver.postclose() function.  It is called when a user-space
+ * process closes/releases the DRM device file.  At the end of DRM
+ * drm_release() (in "drm_fops.c"), but before drm_lastclose is optionally
+ * called, it calls this function.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param priv (IN) DRM's file private data struct (in "drmP.h")
+ */
+void emgd_driver_postclose(struct drm_device *dev, struct drm_file *priv)
+{
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+	mutex_lock(&dev->struct_mutex);
+
+	ret = PVRSRVRelease(dev, priv);
+
+	mutex_unlock(&dev->struct_mutex);
+	EMGD_TRACE_EXIT;
+} /* emgd_driver_postclose() */
+
+
+/**
+ * This is the drm_driver.suspend() function.  It appears to support what the
+ * Linux "pm.h" file calls "the legacy suspend framework."  The DRM
+ * drm_class_suspend() (in "drm_sysfs.c", which implements the Linux
+ * class.suspend() function defined in "device.h") calls this function if
+ * conditions are met, such as drm_minor->type is DRM_MINOR_LEGACY and the
+ * driver doesn't have the DRIVER_MODESET (i.e. KMS--Kernel Mode Setting)
+ * feature set.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param state (IN) What power state to put the device in (in "pm.h")
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int emgd_driver_suspend(struct drm_device *dev, pm_message_t state)
+{
+	int ret;
+	unsigned int pwr_state;
+	drm_emgd_priv_t *priv = dev->dev_private;
+
+	EMGD_TRACE_ENTER;
+
+
+	mutex_lock(&dev->struct_mutex);
+
+	/* When the system is suspended, the X server does a VT switch, which saves
+	 * the register state of the X server, and restores the console's register
+	 * state.  This code saves the console's register state, so that after the
+	 * system resumes, VT switches to the console can occur.
+	 */
+	if (priv->hal_running) {
+		EMGD_DEBUG("Saving the console's register state");
+		priv->suspended_state =
+			drm_HAL_context->mod_dispatch.reg_alloc(drm_HAL_context,
+				IGD_REG_SAVE_ALL);
+		if (priv->suspended_state) {
+			/*
+			//Flag CDVO is not needed
+			if (state.event == PM_EVENT_SUSPEND){
+				if (drm_HAL_context->mod_dispatch.flag_cdvo!= NULL){
+					drm_HAL_context->mod_dispatch.flag_cdvo(drm_HAL_context);
+				}
+			}
+			*/
+			drm_HAL_context->mod_dispatch.reg_save(drm_HAL_context,
+				priv->suspended_state);
+		}
+	}
+
+	/* Map the pm_message_t event states to HAL states: */
+	switch (state.event) {
+	case PM_EVENT_PRETHAW:
+	case PM_EVENT_FREEZE:
+		pwr_state = IGD_POWERSTATE_D1;
+		break;
+	case PM_EVENT_HIBERNATE:
+		pwr_state = IGD_POWERSTATE_D3;
+		break;
+	case PM_EVENT_SUSPEND:
+	default:
+		pwr_state = IGD_POWERSTATE_D2;
+		break;
+	} /* switch (state) */
+
+	EMGD_DEBUG("Calling pwr_alter()");
+	ret = drm_HAL_dispatch->pwr_alter(drm_HAL_handle, pwr_state);
+	EMGD_DEBUG("pwr_alter() returned %d", ret);
+
+	if (0 == ret) {
+		EMGD_DEBUG("Calling PVRSRVDriverSuspend()");
+		ret = PVRSRVDriverSuspend(dev, state);
+		EMGD_DEBUG("PVRSRVDriverSuspend() returned %d", ret);
+	}
+
+	EMGD_DEBUG("Returning %d", ret);
+	mutex_unlock(&dev->struct_mutex);
+	EMGD_TRACE_EXIT;
+	return ret;
+
+} /* emgd_driver_suspend() */
+
+
+/**
+ * This is the drm_driver.resume() function.  It appears to support what the
+ * Linux "pm.h" file calls "the legacy suspend framework."  The DRM
+ * drm_class_resume() (in "drm_sysfs.c", which implements the Linux
+ * class.suspend() function defined in "device.h") calls this function if
+ * conditions are met, such as drm_minor->type is DRM_MINOR_LEGACY and the
+ * driver doesn't have the DRIVER_MODESET feature set.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return 0 on Success
+ * @return <0 on Error
+ */
+int emgd_driver_resume(struct drm_device *dev)
+{
+	int ret;
+	drm_emgd_priv_t *priv = dev->dev_private;
+
+	EMGD_TRACE_ENTER;
+
+	mutex_lock(&dev->struct_mutex);
+
+	EMGD_DEBUG("Calling pwr_alter()");
+	ret = drm_HAL_dispatch->pwr_alter(drm_HAL_handle, IGD_POWERSTATE_D0);
+	EMGD_DEBUG("pwr_alter() returned %d", ret);
+
+	if (0 == ret) {
+		EMGD_DEBUG("Calling PVRSRVDriverResume()");
+		ret = PVRSRVDriverResume(dev);
+		EMGD_DEBUG("PVRSRVDriverResume() returned %d", ret);
+	}
+
+	/* Restore the register state of the console, so that after the X server is
+	 * back up, VT switches to the console can occur.
+	 */
+	if (priv->hal_running) {
+		EMGD_DEBUG("Restoring the console's register state");
+		if (priv->suspended_state) {
+			drm_HAL_context->mod_dispatch.reg_restore(drm_HAL_context,
+				priv->suspended_state);
+			drm_HAL_context->mod_dispatch.reg_free(drm_HAL_context,
+				priv->suspended_state);
+			priv->suspended_state = NULL;
+		}
+	}
+
+	EMGD_DEBUG("Returning %d", ret);
+	mutex_unlock(&dev->struct_mutex);
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* emgd_driver_resume() */
+
+
+/**
+ * This is the drm_driver.device_is_agp() function.  It is called as a part of
+ * drm_init() (before the drm_driver.load() function), and is "typically used
+ * to determine if a card is really attached to AGP or not" (see the Doxygen
+ * comment for this function in "drmP.h").  It is actually called by the
+ * inline'd DRM procedure, drm_device_is_agp() (in "drmP.h").
+ *
+ * The Intel open source driver states that all Intel graphics devices are
+ * treated as AGP.  However, the EMGD driver provides its own management of the
+ * GTT tables, and so doesn't need AGP GART driver support.  Thus, this
+ * function always returns 0.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return 0 the graphics "card" is absolutely not AGP.
+ * @return 1 the graphics "card" is AGP.
+ * @return 2 the graphics "card" may or may not be AGP.
+ */
+int emgd_driver_device_is_agp(struct drm_device *dev)
+{
+	EMGD_DEBUG("[EMGD] Returning 0 from emgd_driver_device_is_agp()\n");
+	return 0;
+} /* emgd_driver_device_is_agp() */
+
+
+
+/**
+ * This is the drm_driver.get_vblank_counter() function.  It is called to get
+ * the raw hardware vblank counter.  There are 4 places within "drm_irq.c" that
+ * call this function.
+ *
+ * @param dev         (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param crtc_select (IN) Exactly how this is derived is unclear, but
+ *                     for now we are assuming that it is the order
+ *                     in which the CRTCs were creatd.  So 0 for
+ *                     the first CRTC, 1 for the second, and so on.
+ *
+ * @return raw vblank counter value
+ */
+u32 emgd_driver_get_vblank_counter(struct drm_device *dev, int crtc_select)
+{
+	struct drm_crtc *crtc;
+	emgd_crtc_t     *cur_emgd_crtc, *selected_emgd_crtc = NULL;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Only supported for KMS-enabled driver */
+	if (!config_drm.kms) {
+		/* Since we have previously returned 0 for our non-KMS driver,
+		 * this is left in to prevent any unforeseen problems. */
+		EMGD_TRACE_EXIT;
+		return 0;
+	}
+
+
+	/* Find the CRTC associated with the  */
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		cur_emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+		if ((1 << crtc_select) == cur_emgd_crtc->crtc_id) {
+			selected_emgd_crtc = cur_emgd_crtc;
+			break;
+		}		
+	}
+
+	if (NULL == selected_emgd_crtc) {
+		EMGD_ERROR_EXIT("Invalid CRTC selected.");
+		return -EINVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return mode_context->kms_dispatch->kms_get_vblank_counter(
+										selected_emgd_crtc);
+} /* emgd_driver_get_vblank_counter() */
+
+
+
+/**
+ * This is the drm_driver.enable_vblank() function.  It is called by
+ * drm_vblank_get() (in "drm_irq.c") to enable vblank interrupt events.
+ * This function is only available for KMS
+ *
+ * @param dev         (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param crtc_select (IN) Exactly how this is derived is unclear, but
+ *                     for now we are assuming that it is the order
+ *                     in which the CRTCs were creatd.  So 0 for
+ *                     the first CRTC, 1 for the second, and so on.
+ *
+ * @return 0 on Success
+ * @return <0 if the given crtc's vblank interrupt cannot be enabled
+ */
+int emgd_driver_enable_vblank(struct drm_device *dev, int crtc_select)
+{
+	struct drm_crtc *crtc;
+	unsigned char   *mmio;
+	emgd_crtc_t     *cur_emgd_crtc, *selected_emgd_crtc = NULL;
+	unsigned long    request_for;
+	int              ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Only supported for KMS-enabled driver */
+	if (!config_drm.kms) {
+		/* We should return an error here since this is not
+		 * supported.  However, since we have previously returned 0
+		 * for our non-KMS driver, this is left in to prevent any
+		 * unforeseen problems. */
+		EMGD_TRACE_EXIT;
+		return 0;
+	}
+
+
+	/* Find the CRTC associated with the  */
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		cur_emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+		if ((1 << crtc_select) == cur_emgd_crtc->crtc_id) {
+			selected_emgd_crtc = cur_emgd_crtc;
+			break;
+		}		
+	}
+
+	if (NULL == selected_emgd_crtc) {
+		EMGD_ERROR_EXIT("Invalid CRTC selected.");
+		return -EINVAL;
+	}
+
+
+	switch (selected_emgd_crtc->igd_pipe->pipe_features & IGD_PORT_MASK) {
+		case IGD_PORT_SHARE_LVDS:
+			request_for = VBLANK_INT4_PORT4;
+			break;
+
+		case IGD_PORT_SHARE_DIGITAL:
+			request_for = VBLANK_INT4_PORT2;
+			break;
+
+		default:
+			EMGD_DEBUG("Unsupported port type");
+			request_for = 0;
+			ret         = -EINVAL;
+			break;
+	}
+
+	if (0 == ret) {
+		mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+		ret  = mode_context->dispatch->full->request_vblanks(request_for, mmio);
+
+		if (ret) {
+			EMGD_DEBUG("Failed to enable vblank");
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+} /* emgd_driver_enable_vblank() */
+
+
+
+/**
+ * This is the drm_driver.disable_vblank() function.  It is called by
+ * vblank_disable_fn() (in "drm_irq.c") to disable vblank interrupt events.
+ *
+ * @param dev         (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param crtc_select (IN) Exactly how this is derived is unclear, but
+ *                     for now we are assuming that it is the order
+ *                     in which the CRTCs were creatd.  So 0 for
+ *                     the first CRTC, 1 for the second, and so on.
+ */
+void emgd_driver_disable_vblank(struct drm_device *dev, int crtc_select)
+{
+	struct drm_crtc *crtc;
+	unsigned char   *mmio;
+	emgd_crtc_t     *cur_emgd_crtc, *selected_emgd_crtc = NULL;
+	unsigned long    request_for;
+	int              ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Only supported for KMS-enabled driver */
+	if (!config_drm.kms) {
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+
+	/* Find the CRTC associated with the  */
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		cur_emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+		if ((1 << crtc_select) == cur_emgd_crtc->crtc_id) {
+			selected_emgd_crtc = cur_emgd_crtc;
+			break;
+		}		
+	}
+
+	if (NULL == selected_emgd_crtc) {
+		EMGD_ERROR_EXIT("Invalid CRTC selected.");
+		return;
+	}
+
+
+	switch (selected_emgd_crtc->igd_pipe->pipe_features & IGD_PORT_MASK) {
+		case IGD_PORT_SHARE_LVDS:
+			request_for = VBLANK_INT4_PORT4;
+			break;
+
+		case IGD_PORT_SHARE_DIGITAL:
+			request_for = VBLANK_INT4_PORT2;
+			break;
+
+		default:
+			EMGD_DEBUG("Unsupported port type");
+			request_for = 0;
+			ret         = -EINVAL;
+			break;
+	}
+
+	if (0 == ret) {
+		mmio = EMGD_MMIO(mode_context->context->device_context.virt_mmadr);
+		ret  = mode_context->dispatch->full->end_request(request_for, mmio);
+
+		if (ret) {
+			EMGD_DEBUG("Failed to disable vblank");
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+} /* emgd_driver_disable_vblank() */
+
+
+
+/**
+ * This is the drm_driver.irq_preinstall() function.  It is called by
+ * drm_irq_install() (in "drm_irq.c") before it installs this driver's IRQ
+ * handler (i.e. emgd_driver_irq_handler()).
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ */
+void emgd_driver_irq_preinstall(struct drm_device *dev)
+{
+	/* Notes on what to implement in this function:
+	 *
+	 * - Ditto the notes in emgd_driver_enable_vblank().
+	 *
+	 * - In addition, I'll note that I see a HAL interface that roughly
+	 *   corresponds to this functionality--the interrupt_install() and
+	 *   interrupt_uninstall() entry points in igd_dispatch_t (in "igd.h" and
+	 *   better defined in "igd_interrupt.h").  However:
+	 *
+	 *    - There are no "plb" versions of this code, only "alm", "nap" & "wht".
+	 *
+	 *    - These install a passed-in interrupt handler.  As far as I can see
+	 *      in the ssigd tree, these routines are never called, and no
+	 *      interrupt handlers exist.
+	 *
+	 *    - Koheo doesn't contain the hal/core/interrupt code.  I'm not sure
+	 *      what that does, but grep'ing through the source shows a number of
+	 *      lines about irq's.
+	 *
+	 * - The open source Intel driver has some code that implements this.  It's
+	 *   not small, so I haven't really studied it yet.  Can/should we go with
+	 *   a similar approach, or keep like the current HAL approach and
+	 *   structure some new entrypoints that allow future hardware to be
+	 *   different?
+	 */
+
+	/* TODO -- REPLACE THIS STUB WITH A REAL IMPLEMENTATION  */
+	printk(KERN_INFO "[EMGD] Inside of STUBBED %s()", __FUNCTION__);
+
+} /* emgd_driver_irq_preinstall() */
+
+
+/**
+ * This is the drm_driver.irq_postinstall() function.  It is called by
+ * drm_irq_install() (in "drm_irq.c") after it installs this driver's IRQ
+ * handler (i.e. emgd_driver_irq_handler()).
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return 0 on Success
+ * @return <0 if the given crtc's vblank interrupt cannot be enabled
+ */
+int emgd_driver_irq_postinstall(struct drm_device *dev)
+{
+	return 0;
+} /* emgd_driver_irq_postinstall() */
+
+
+/**
+ * This is the drm_driver.irq_uninstall() function.  It is called by
+ * drm_irq_install() (in "drm_irq.c") as a part of uninstalling this driver's
+ * IRQ handler (i.e. emgd_driver_irq_handler()).
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ */
+void emgd_driver_irq_uninstall(struct drm_device *dev)
+{
+	/* TODO -- REPLACE THIS STUB WITH A REAL IMPLEMENTATION THE IMG TRAINING */
+	printk(KERN_INFO "[EMGD] Inside of STUBBED %s()", __FUNCTION__);
+
+} /* emgd_driver_irq_uninstall() */
+
+
+/**
+ * This is the drm_driver.irq_handler() function, and is a Linux IRQ interrupt
+ * handler (see the irq_handler_t in the Linux header "interrupt.h").  It is
+ * installed by drm_irq_install() (in "drm_irq.c") by calling request_irq()
+ * (implemented in "interrupt.h") with this function as the 2nd parameter.  The
+ * return type is an enum (see the Linux header "irqreturn.h").
+ *
+ * Our HAL will have already installed an IRQ handler, so we do nothing here.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return IRQ_NONE if the interrupt was not from this device
+ * @return IRQ_HANDLED if the interrupt was handled by this device
+ * @return IRQ_WAKE_THREAD if this handler requests to wake the handler thread
+ */
+irqreturn_t emgd_driver_irq_handler(int irq, void *arg)
+{
+	/* TODO -- REPLACE THIS STUB WITH A REAL IMPLEMENTATION THE IMG TRAINING */
+	printk(KERN_INFO "[EMGD] Inside of STUBBED %s()", __FUNCTION__);
+
+	return IRQ_NONE;
+} /* emgd_driver_irq_handler() */
+
+
+static int __devinit emgd_pci_probe(struct pci_dev *pdev,
+		const struct pci_device_id *ent)
+{
+	if (PCI_FUNC(pdev->devfn)) {
+		return -ENODEV;
+	}
+
+	/*
+	 * Name changed at some point in time.  2.6.35 uses drm_get_dev
+	 * and 2.6.38 uses drm_get_pci_dev  Need to figure what kernel
+	 * version this changed.
+	 */
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35)
+	return drm_get_pci_dev(pdev, ent, &driver);
+#else
+	return drm_get_dev(pdev, ent, &driver);
+#endif
+}
+
+static void emgd_pci_remove(struct pci_dev *pdev)
+{
+	struct drm_device *dev = pci_get_drvdata(pdev);
+
+	drm_put_dev(dev);
+}
+
+static int emgd_pm_suspend(struct device *dev)
+{
+	return 0;
+}
+
+static int emgd_pm_resume(struct device *dev)
+{
+	return 0;
+}
+
+static int emgd_pm_freeze(struct device *dev)
+{
+	return 0;
+}
+
+static int emgd_pm_thaw(struct device *dev)
+{
+	return 0;
+}
+
+static int emgd_pm_poweroff(struct device *dev)
+{
+	return 0;
+}
+
+static int emgd_pm_restore(struct device *dev)
+{
+	return 0;
+}
+
+static const struct dev_pm_ops emgd_pm_ops = {
+	.suspend = emgd_pm_suspend,
+	.resume = emgd_pm_resume,
+	.freeze = emgd_pm_freeze,
+	.thaw = emgd_pm_thaw,
+	.poweroff = emgd_pm_poweroff,
+	.restore = emgd_pm_restore,
+};
+
+/*
+ * NOTE: The remainder of this file is standard kernel module initialization
+ * code:
+ */
+
+
+/*
+ * Older kernels kept the PCI driver information directly in the
+ * DRM driver structure.  Newer kernels (2.6.38-rc3 and beyond)
+ * move it outside of the DRM driver structure and pass it to
+ * drm_pci_init instead in order to help pave the way for
+ * USB graphics devices.
+ */
+#define EMGD_PCI_DRIVER {    \
+	.name     = DRIVER_NAME, \
+	.id_table = pciidlist,   \
+	.probe = emgd_pci_probe, \
+	.remove = emgd_pci_remove, \
+	.driver.pm = &emgd_pm_ops, \
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)
+static struct pci_driver emgd_pci_driver = EMGD_PCI_DRIVER;
+#endif
+
+/**
+ * DRM Sub driver entry points
+ */
+static struct drm_driver driver = {
+	.driver_features    = DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
+	.load               = emgd_driver_load,
+	.unload             = emgd_driver_unload,
+	.open               = emgd_driver_open,
+	.lastclose          = emgd_driver_lastclose,
+	.preclose           = emgd_driver_preclose,
+	.postclose          = emgd_driver_postclose,
+	.suspend            = emgd_driver_suspend,
+	.resume             = emgd_driver_resume,
+	.device_is_agp      = emgd_driver_device_is_agp,
+	.get_vblank_counter = emgd_driver_get_vblank_counter,
+	.enable_vblank      = emgd_driver_enable_vblank,
+	.disable_vblank     = emgd_driver_disable_vblank,
+	.irq_preinstall     = emgd_driver_irq_preinstall,
+	.irq_postinstall    = emgd_driver_irq_postinstall,
+	.irq_uninstall      = emgd_driver_irq_uninstall,
+	.irq_handler        = emgd_driver_irq_handler,
+	.reclaim_buffers    = drm_core_reclaim_buffers,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
+	.get_map_ofs        = drm_core_get_map_ofs,
+	.get_reg_ofs        = drm_core_get_reg_ofs,
+#endif
+	.ioctls             = emgd_ioctl,
+	.fops = {
+		.owner   = THIS_MODULE,
+		.open    = drm_open,
+		.release = drm_release,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
+		.unlocked_ioctl   = drm_ioctl,
+#else
+		.ioctl   = drm_ioctl,
+#endif
+		.mmap    = emgd_mmap,
+		.poll    = drm_poll,
+		.fasync  = drm_fasync,
+		.read    = drm_read,
+	},
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
+	.pci_driver          = EMGD_PCI_DRIVER,
+#endif
+	.name                = DRIVER_NAME,
+	.desc                = DRIVER_DESC,
+	.date                = DRIVER_DATE,
+	.major               = DRIVER_MAJOR,
+	.minor               = DRIVER_MINOR,
+	.patchlevel          = DRIVER_PATCHLEVEL,
+};
+
+/**
+ * Standard procedure to initialize this kernel module when it is loaded.
+ */
+static int __init emgd_init(void) {
+	int ret;
+	struct pci_dev *our_device;
+
+	printk(KERN_INFO "[EMGD] Initializing Driver.\n");
+	driver.num_ioctls = emgd_max_ioctl;
+
+	/* If init == 1 then we should always set KMS to 0 for US15 */
+
+	if(config_drm.init || drm_emgd_init == 1){
+		
+		/*  Detecting device */
+
+		/* 
+		 * 0x8086 is the intel vendor id and 0x8108 is the 
+		 * US15 device id.
+		 * pci_get_device returns NULL if it is not a PLB.
+		 */
+
+		our_device = pci_get_device(PCI_VENDOR_ID_INTEL, 
+					PCI_DEVICE_ID_VGA_PLB, NULL);
+		
+		if(our_device){
+			EMGD_ERROR("US15 detected. Setting KMS to 0 "
+				"config_drm.kms = %d ", config_drm.kms);
+			config_drm.kms = 0;
+		}
+	}
+
+	if (config_drm.kms && (config_drm.init || drm_emgd_init == 1)) {
+		driver.driver_features |= DRIVER_MODESET;
+	}
+
+	PVRDPFInit();
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)
+	ret = drm_pci_init(&driver, &emgd_pci_driver);
+#else
+	ret = drm_init(&driver);
+#endif
+	printk(KERN_INFO "[EMGD] Driver Initialized.\n");
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/**
+ * Standard procedure to clean-up this kernel module before it exits & unloads.
+ */
+static void __exit emgd_exit(void) {
+	EMGD_TRACE_ENTER;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)
+	drm_pci_exit(&driver, &emgd_pci_driver);
+#else
+	drm_exit(&driver);
+#endif
+	EMGD_TRACE_EXIT;
+}
+
+
+module_init(emgd_init);
+module_exit(emgd_exit);
+
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL and additional rights");
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_drv.h b/drivers/gpu/drm/emgd/emgd/drm/emgd_drv.h
new file mode 100644
index 0000000..c7e334a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_drv.h
@@ -0,0 +1,209 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_drv.h
+ * $Revision: 1.76 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _EMGD_DRV_H_
+#define _EMGD_DRV_H_
+
+#include <linux/io-mapping.h>
+#include <emgd_shared.h>
+#include <igd_version.h>
+#include "user_config.h"
+
+#define DRIVER_AUTHOR     "Intel Corporation."
+#define DRIVER_NAME       EMGD_MODULE_NAME
+#define DRIVER_DESC       "Intel Embedded Media and Grahics Driver"
+#define DRIVER_DATE       PVR_BUILD_DATE
+#define DRIVER_MAJOR      IGD_MAJOR_NUM
+#define DRIVER_MINOR      IGD_MINOR_NUM
+#define DRIVER_PATCHLEVEL IGD_BUILD_NUM
+
+#define INTELFB_CONN_LIMIT 4
+
+/*
+ *  * Special "handle" that indicates the framebuffer being referred to is the
+ *   * EMGD initial framebuffer (which does not have a PVR meminfo handle that
+ *    * can be passed.
+ *     */
+#define EMGD_INITIAL_FRAMEBUFFER 0
+
+
+/* Function prototypes */
+extern int emgd_driver_load(struct drm_device *dev, unsigned long flags);
+extern int emgd_driver_unload(struct drm_device *dev);
+extern int emgd_driver_open(struct drm_device *dev,
+		struct drm_file *file_priv);
+extern void emgd_driver_lastclose(struct drm_device *dev);
+extern void emgd_driver_preclose(struct drm_device *dev,
+		struct drm_file *file_priv);
+extern void emgd_driver_postclose(struct drm_device *dev,
+		struct drm_file *file_priv);
+extern int emgd_driver_device_is_agp(struct drm_device * dev);
+extern long egd(struct file *filp, unsigned int cmd, unsigned long arg);
+
+extern int emgd_startup_hal(struct drm_device *dev, igd_param_t *params);
+int disp_splash_screen(emgd_drm_splash_screen_t *ss_data);
+int disp_splash_video(emgd_drm_splash_video_t *sv_data);
+extern irqreturn_t emgd_driver_irq_handler(DRM_IRQ_ARGS);
+extern void emgd_driver_irq_preinstall(struct drm_device * dev);
+extern int emgd_driver_irq_postinstall(struct drm_device *dev);
+extern void emgd_driver_irq_uninstall(struct drm_device * dev);
+extern int emgd_driver_enable_vblank(struct drm_device *dev, int crtc);
+extern void emgd_driver_disable_vblank(struct drm_device *dev, int crtc);
+extern u32 emgd_driver_get_vblank_counter(struct drm_device *dev, int crtc);
+
+extern int emgd_driver_suspend(struct drm_device *dev, pm_message_t state);
+extern int emgd_driver_resume(struct drm_device *dev);
+extern int emgd_mmap(struct file *filp, struct vm_area_struct *vma);
+
+
+/* Module parameters: */
+extern int drm_emgd_configid;
+
+
+
+typedef struct drm_device drm_device_t;
+
+
+/*
+ * IOCTL handler function prototypes:
+ */
+int emgd_alter_cursor(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_alter_cursor_pos(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_alter_displays(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_display_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_alter_ovl(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_appcontext_alloc(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_appcontext_free(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_driver_save_restore(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_enable_port(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_attrs(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+extern int emgd_alter_ovl2(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+extern int emgd_get_ovl_init_params(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_drm_config(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_EDID_block(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_EDID_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_pixelformats(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_port_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_gmm_alloc_region(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_gmm_alloc_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_gmm_get_num_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_gmm_get_surface_list(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_gmm_free(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_gmm_flush_cache(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_pan_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_power_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_pwr_alter(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_query_dc(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_query_max_size_ovl(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_query_ovl(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_query_mode_list(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_set_attrs(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_set_palette_entry(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_set_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_dihclone_set_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_sync(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_driver_pre_init(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_driver_get_ports(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_start_pvrsrv(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_test_pvrsrv(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_chipset_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_video_cmd_buf(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_device_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_page_list(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_init_video(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_video_get_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_video_flush_tlb(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_preinit_mmu(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_get_golden_htotal(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_control_plane_format(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_set_overlay_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+int emgd_query_2d_caps_hwhint(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+/* For Buffer Class of Texture Stream */
+int emgd_bc_ts_cmd_init(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+int emgd_bc_ts_cmd_uninit(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+int emgd_bc_ts_cmd_request_buffers(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+int emgd_bc_ts_cmd_release_buffers(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+int emgd_bc_ts_set_buffer_info(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+int emgd_bc_ts_get_buffers_count(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+int emgd_bc_ts_get_buffer_index(struct drm_device *dev, void *arg, struct drm_file *file_priv);
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_encoder.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_encoder.c
new file mode 100644
index 0000000..72b3d3e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_encoder.c
@@ -0,0 +1,477 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_encoder.c
+ * $Revision: 1.3 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Encoder / kenrel mode setting functions.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.oal
+
+#include <drmP.h>
+#include <drm_crtc_helper.h>
+#include <linux/version.h>
+
+#include <mode_dispatch.h>
+#include "drm_emgd_private.h"
+
+#include <igd_pwr.h>
+
+
+
+/*------------------------------------------------------------------------------
+ * External Functions
+ *------------------------------------------------------------------------------
+ */
+extern int calculate_eld(igd_display_port_t *port,
+				igd_timing_info_t *timing_info);
+
+
+
+static void emgd_encoder_destroy(struct drm_encoder *encoder);
+static void emgd_encoder_dpms(struct drm_encoder *encoder, int mode);
+static bool emgd_encoder_mode_fixup(struct drm_encoder *encoder,
+				struct drm_display_mode *mode,
+				struct drm_display_mode *adjusted_mode);
+static void emgd_encoder_prepare(struct drm_encoder *encoder);
+static void emgd_encoder_mode_set(struct drm_encoder *encoder,
+				struct drm_display_mode *mode,
+				struct drm_display_mode *adjusted_mode);
+static void emgd_encoder_commit(struct drm_encoder *encoder);
+
+
+const struct drm_encoder_funcs emgd_encoder_funcs = {
+	.destroy = emgd_encoder_destroy,
+};
+
+const struct drm_encoder_helper_funcs emgd_encoder_helper_funcs = {
+	.dpms       = emgd_encoder_dpms,
+	.mode_fixup = emgd_encoder_mode_fixup,
+	.prepare    = emgd_encoder_prepare,
+	.mode_set   = emgd_encoder_mode_set,
+	.commit     = emgd_encoder_commit,
+};
+
+
+/**
+ * emgd_encoder_dpms
+ *
+ * This function will put the encoder to either an ON or OFF state.  Anything
+ * that is not DRM_MODE_DPMS_ON is treated as an off-state.
+ *
+ * @param encoder (IN) Encoder
+ * @param mode    (IN) power mode
+ *
+ * @return None
+ */
+static void emgd_encoder_dpms(struct drm_encoder *encoder, int mode)
+{
+	emgd_encoder_t *emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+	igd_display_port_t *igd_port = emgd_encoder->igd_port;
+	unsigned long       new_port_state;
+
+	EMGD_TRACE_ENTER;
+
+
+	switch(mode) {
+		case DRM_MODE_DPMS_ON:
+			new_port_state = IGD_POWERSTATE_D0;
+			break;
+
+		case DRM_MODE_DPMS_STANDBY:
+		case DRM_MODE_DPMS_SUSPEND:
+		case DRM_MODE_DPMS_OFF:
+			new_port_state = IGD_POWERSTATE_D3;
+			break;
+
+		default:
+			EMGD_ERROR_EXIT("Unsupported DPMS mode");
+			return;
+	}
+
+
+	if (new_port_state == IGD_POWERSTATE_D3 &&
+		emgd_encoder->flags & ENCODER_FLAG_FIRST_DPMS) {
+
+		EMGD_DEBUG("Not setting initial port %lx power to %lx",
+			igd_port->port_number, new_port_state);
+		emgd_encoder->flags &= ~ENCODER_FLAG_FIRST_DPMS;
+	} else {
+		EMGD_DEBUG("Setting port %lx power to %lx",
+			igd_port->port_number, new_port_state);
+		igd_port->pd_driver->set_power(igd_port->pd_context,
+			new_port_state);
+
+		if (new_port_state == IGD_POWERSTATE_D0) {
+			mode_context->kms_dispatch->kms_post_program_port(emgd_encoder,
+				TRUE);
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_encoder_mode_fixup
+ *
+ * Called before a mode set, takes the input "mode", matches it to the closest
+ * supported mode, then put the supported mode into "adjusted_mode" to let the
+ * caller know.
+ *
+ * Note: We cannot handle centered and scaled mode with this.  To handle this
+ *       we need to program the pipe and the port to different sets of timings.
+ *       The CRTC Helper does not allow this.  It wants to send adjusted_mode
+ *       to both the CRTC and the Encoder.  We can maybe get around this by
+ *       modifying the "mode" parameter, but that is not the right approach.
+ *
+ * @param encoder (IN) Encoder being prepared
+ * @param mode    (IN) Requested mode
+ * @param adjusted_mode (IN) Encoder supported mode
+ *
+ * @return true, false (details TBD)
+ */
+static bool emgd_encoder_mode_fixup(struct drm_encoder *encoder,
+		struct drm_display_mode *mode,
+		struct drm_display_mode *adjusted_mode)
+{
+	struct drm_device      *dev          = NULL;
+	igd_context_t          *context      = NULL;
+	igd_display_port_t     *port         = NULL;
+	igd_framebuffer_info_t *fb_info      = NULL;
+	emgd_encoder_t         *emgd_encoder = NULL;
+	igd_timing_info_t      *timing       = NULL;
+	igd_display_info_t     *pt_info      = NULL;
+	emgd_crtc_t            *emgd_crtc    = NULL;
+	igd_display_pipe_t     *pipe         = NULL;
+	unsigned long           existing_height  = 0;
+	unsigned long           existing_width   = 0;
+	unsigned long           existing_refresh = 0;
+	int                     ret;
+
+	EMGD_TRACE_ENTER;
+
+	/* Check ajusted mode to see if it's valid.  If not, populate it */
+	if (adjusted_mode->crtc_htotal == 0) {
+		EMGD_DEBUG("No valid mode in adjusted mode, setting valid mode");
+		drm_mode_set_crtcinfo(adjusted_mode, 0);
+	}
+
+	dev = encoder->dev;
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+	emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+	port = emgd_encoder->igd_port;
+	if (!port->pt_info) {
+		port->pt_info = kzalloc(sizeof(igd_display_info_t), GFP_KERNEL);
+		if (!port->pt_info) {
+			EMGD_DEBUG("Cannot allocate igd_display_into_t");
+			return false;
+		}
+	}
+	existing_height  = port->pt_info->height;
+	existing_width   = port->pt_info->width;
+	existing_refresh = port->pt_info->refresh;
+	pt_info          = port->pt_info;
+
+	fb_info = kzalloc(sizeof(igd_framebuffer_info_t), GFP_KERNEL);
+	if (!fb_info) {
+		EMGD_DEBUG("Cannot allocate framebuffer info");
+		return false;
+	}
+
+	/* Get the dimension of the framebuffer linked to the CRTC.  If it is
+	 * smaller than the resolution, kms_match_mode will either center it
+	 * or let the encoder hardware scale it */
+	fb_info->width  = encoder->crtc->fb->width;
+	fb_info->height = encoder->crtc->fb->height;
+	EMGD_DEBUG("Setting fb_info to: %dx%d", fb_info->width, fb_info->height);
+
+	pt_info->width        = mode->crtc_hdisplay;
+	pt_info->height       = mode->crtc_vdisplay;
+	pt_info->refresh      = mode->vrefresh;
+	pt_info->dclk         = mode->synth_clock;
+	pt_info->htotal       = mode->crtc_htotal;
+	pt_info->hblank_start = mode->crtc_hblank_start;
+	pt_info->hblank_end   = mode->crtc_hblank_end;
+	pt_info->hsync_start  = mode->crtc_hsync_start;
+	pt_info->hsync_end    = mode->crtc_hsync_end;
+	pt_info->vtotal       = mode->crtc_vtotal;
+	pt_info->vblank_start = mode->crtc_vblank_start;
+	pt_info->vblank_end   = mode->crtc_vblank_end;
+	pt_info->vsync_start  = mode->crtc_vsync_start;
+	pt_info->vsync_end    = mode->crtc_vsync_end;
+	pt_info->mode_number  = mode->clock_index;
+	pt_info->flags        = 0;
+	EMGD_DEBUG("Setting pt_info to: %dx%d", pt_info->width, pt_info->height);
+
+	ret = mode_context->kms_dispatch->kms_match_mode((void *)emgd_encoder,
+		(void *)fb_info, &timing);
+
+	if (!ret) {
+		adjusted_mode->crtc_hdisplay     = timing->width;
+		adjusted_mode->crtc_vdisplay     = timing->height;
+		adjusted_mode->vrefresh          = timing->refresh;
+		adjusted_mode->synth_clock       = timing->dclk;
+		adjusted_mode->crtc_htotal       = timing->htotal;
+		adjusted_mode->crtc_hblank_start = timing->hblank_start;
+		adjusted_mode->crtc_hblank_end   = timing->hblank_end;
+		adjusted_mode->crtc_hsync_start  = timing->hsync_start;
+		adjusted_mode->crtc_hsync_end    = timing->hsync_end;
+		adjusted_mode->crtc_vtotal       = timing->vtotal;
+		adjusted_mode->crtc_vblank_start = timing->vblank_start;
+		adjusted_mode->crtc_vblank_end   = timing->vblank_end;
+		adjusted_mode->crtc_vsync_start  = timing->vsync_start;
+		adjusted_mode->crtc_vsync_end    = timing->vsync_end;
+		adjusted_mode->clock_index       = timing->mode_number;
+		adjusted_mode->private_flags     = timing->mode_info_flags;
+
+		EMGD_DEBUG("(%dx%d@%d)->(%dx%d@%d)",
+			mode->crtc_hdisplay, mode->crtc_vdisplay, mode->vrefresh,
+			adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay,
+			adjusted_mode->vrefresh);
+
+		/* Check our new mode against what currently there
+		 * to see if we can do a seamless mode-set
+		 */
+		if (emgd_encoder->flags & ENCODER_FLAG_FIRST_ALTER) {
+			if (mode_context->fw_info) {
+
+				emgd_crtc = container_of(encoder->crtc, emgd_crtc_t, base);
+				pipe = emgd_crtc->igd_pipe;
+
+				existing_width = mode_context->fw_info->
+					timing_arr[pipe->pipe_num].width;
+				existing_height = mode_context->fw_info->
+					timing_arr[pipe->pipe_num].height;
+				existing_refresh = mode_context->fw_info->
+					timing_arr[pipe->pipe_num].refresh;
+
+			}
+		}
+
+		if (adjusted_mode->crtc_hdisplay == existing_width &&
+			adjusted_mode->crtc_vdisplay == existing_height) {
+
+			if (abs(adjusted_mode->vrefresh - existing_refresh) <= 1) {
+				emgd_encoder->flags |= ENCODER_FLAG_SEAMLESS;
+			}
+		}
+	}
+
+	kfree(fb_info);
+
+	EMGD_TRACE_EXIT;
+	return (!ret);
+}
+
+
+
+/**
+ * emgd_encoder_prepare
+ *
+ * Based on the available documentation at the moment, this function gets
+ * called right before a mode change.  Its job is to turn off the display.
+ *
+ * @param encoder (IN) Encoder being prepared
+ *
+ * @return None
+ */
+static void emgd_encoder_prepare(struct drm_encoder *encoder)
+{
+	struct drm_encoder_helper_funcs *encoder_funcs;
+	emgd_encoder_t *emgd_encoder;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+
+	if (!(emgd_encoder->flags & ENCODER_FLAG_SEAMLESS)) {
+		encoder_funcs = encoder->helper_private;
+		encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
+	}
+
+	emgd_encoder->flags &= ~ENCODER_FLAG_FIRST_ALTER;
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_encoder_commit
+ *
+ * This function commits the mode change sequence by actually programming
+ * the registers.
+ *
+ * @param encoder (IN) Encoder being prepared
+ *
+ * @return None
+ */
+static void emgd_encoder_commit(struct drm_encoder *encoder)
+{
+	struct drm_encoder_helper_funcs *encoder_funcs;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	emgd_crtc_t        *emgd_crtc;
+	igd_display_port_t *port;
+	igd_display_pipe_t *pipe;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+
+
+	if (!(emgd_encoder->flags & ENCODER_FLAG_SEAMLESS)) {
+
+		port      = emgd_encoder->igd_port;
+		emgd_crtc = container_of(encoder->crtc, emgd_crtc_t, base);
+		pipe      = emgd_crtc->igd_pipe;
+
+		mode_context->kms_dispatch->kms_program_port(emgd_encoder,
+	    		IGD_DISPLAY_ENABLE);
+/*
+		port->pd_driver->set_mode(port->pd_context, pipe->timing,
+								1<<pipe->pipe_num);
+*/
+		encoder_funcs = encoder->helper_private;
+		encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
+
+	}
+	/* Reset our seamless variable */
+	emgd_encoder->flags &= ~ENCODER_FLAG_SEAMLESS;
+
+	/* TODO: Add call to check_display */
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_encoder_mode_set
+ *
+ * This function saves the requested timings into the Port Timing Info
+ * structure.  At emgd_encoder_commit() time we should be using these
+ * timings to program the port, but currently we are using timings from
+ * the pipe.  This is fine for now, but at one point we should investigate
+ * the centering case in which the port timings may not match the pipe timings.
+ *
+ * @param encoder (IN) Encoder being prepared
+ * @param mode    (IN)
+ * @param adjusted_mode (IN)
+ *
+ * @return None
+ */
+static void emgd_encoder_mode_set(struct drm_encoder *encoder,
+		struct drm_display_mode *mode,
+		struct drm_display_mode *adjusted_mode)
+{
+	emgd_crtc_t        *emgd_crtc    = NULL;
+	emgd_encoder_t     *emgd_encoder = NULL;
+	igd_display_pipe_t *pipe         = NULL;
+	igd_display_port_t *port         = NULL;
+	pd_timing_t        *timing       = NULL;
+
+	EMGD_TRACE_ENTER;
+
+
+	emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+
+	if (!(emgd_encoder->flags & ENCODER_FLAG_SEAMLESS)) {
+		port = emgd_encoder->igd_port;
+		emgd_crtc = container_of(encoder->crtc, emgd_crtc_t, base);
+		pipe = emgd_crtc->igd_pipe;
+
+		if (pipe) {
+			timing = (pd_timing_t *)pipe->timing;
+
+			if (NULL == port->pt_info) {
+				port->pt_info = kzalloc(sizeof(igd_display_info_t), GFP_KERNEL);
+
+				if (!port->pt_info) {
+					EMGD_ERROR_EXIT("Unable to allocate pt_info.");
+					return;
+				}
+			}
+
+			port->pt_info->width        = adjusted_mode->crtc_hdisplay;
+			port->pt_info->height       = adjusted_mode->crtc_vdisplay;
+			port->pt_info->refresh      = adjusted_mode->vrefresh;
+			port->pt_info->dclk         = adjusted_mode->synth_clock;
+			port->pt_info->htotal       = adjusted_mode->crtc_htotal;
+			port->pt_info->hblank_start = adjusted_mode->crtc_hblank_start;
+			port->pt_info->hblank_end   = adjusted_mode->crtc_hblank_end;
+			port->pt_info->hsync_start  = adjusted_mode->crtc_hsync_start;
+			port->pt_info->hsync_end    = adjusted_mode->crtc_hsync_end;
+			port->pt_info->vtotal       = adjusted_mode->crtc_vtotal;
+			port->pt_info->vblank_start = adjusted_mode->crtc_vblank_start;
+			port->pt_info->vblank_end   = adjusted_mode->crtc_vblank_end;
+			port->pt_info->vsync_start  = adjusted_mode->crtc_vsync_start;
+			port->pt_info->vsync_end    = adjusted_mode->crtc_vsync_end;
+			port->pt_info->mode_number  = adjusted_mode->clock_index;
+			port->pt_info->flags        = adjusted_mode->private_flags;
+
+			port->pt_info->x_offset     = timing->x_offset;
+			port->pt_info->y_offset     = timing->y_offset;
+			port->pt_info->flags       |= IGD_DISPLAY_ENABLE;
+
+
+			EMGD_DEBUG("Calculate ELD");
+			if (calculate_eld(port, timing)) {
+				EMGD_DEBUG("Fail to calculate ELD");
+			}
+
+		} else {
+			EMGD_ERROR("Trying to set the mode without a pipe attached.");
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_encoder_destroy
+ *
+ * Frees the resources allocated for this encoder during "create_encoder()"
+ *
+ * @param encoder (IN) Encoder to be freed
+ *
+ * @return None
+ */
+static void emgd_encoder_destroy(struct drm_encoder *encoder)
+{
+	emgd_encoder_t *emgd_encoder;
+
+	EMGD_TRACE_ENTER;
+	emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+
+	drm_encoder_cleanup(encoder);
+
+	kfree(emgd_encoder);
+
+	EMGD_TRACE_EXIT;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_fb.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_fb.c
new file mode 100644
index 0000000..6e00b27
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_fb.c
@@ -0,0 +1,1413 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_fb.c
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Framebuffer / kernel mode setting functions.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.oal
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/fb.h>
+#include <drmP.h>
+#include <drm.h>
+#include <drm_crtc.h>
+#include <drm_crtc_helper.h>
+#include <drm_fb_helper.h>
+#include <linux/version.h>
+#include <linux/vga_switcheroo.h>
+
+#include "drm_emgd_private.h"
+#include "emgd_drv.h"
+#include "emgd_drm.h"
+#include "memory.h"
+#include "io.h"
+#include "mode_dispatch.h"
+
+/* Necessary to import page list for a PVR-allocated surface */
+#include "pvr_bridge_km.h"
+
+
+
+#if  (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35))
+#define RETURN_PROBE_TYPE int
+#define RETURN_PROBE return 0
+#define PROBE_FUNC fb_changed
+#else
+#define RETURN_PROBE_TYPE void
+#define RETURN_PROBE
+#define PROBE_FUNC output_poll_changed
+#endif
+
+
+
+/*------------------------------------------------------------------------------
+ * Global Variables
+ *------------------------------------------------------------------------------
+ */
+extern igd_framebuffer_info_t primary_fb_info;
+extern mode_context_t         mode_context[1];
+static struct drm_mode_set    panic_mode;
+extern int                    drm_emgd_init;
+extern emgd_drm_config_t      config_drm;
+
+/* CRTC Dispatch Tables */
+extern const struct drm_crtc_funcs emgd_crtc_funcs;
+extern const struct drm_crtc_helper_funcs emgd_crtc_helper_funcs;
+
+/* Encoder Dispatch Tables */
+extern const struct drm_encoder_funcs emgd_encoder_funcs;
+extern const struct drm_encoder_helper_funcs emgd_encoder_helper_funcs;
+
+/* Connector Dispatch Tables */
+extern const struct drm_connector_funcs emgd_connector_funcs;
+extern const struct drm_connector_helper_funcs emgd_connector_helper_funcs;
+
+/* FBCON Dispatch Table */
+extern const struct fb_ops emgd_fb_ops;
+
+
+
+/*------------------------------------------------------------------------------
+ * Formal Declaration
+ *------------------------------------------------------------------------------
+ */
+extern int emgd_fbcon_initial_config(emgd_fbdev_t *emgd_fbdev);
+
+static int emgd_fbdev_init(drm_emgd_priv_t *priv);
+static void emgd_fbdev_destroy(drm_emgd_priv_t *priv);
+
+
+
+/*------------------------------------------------------------------------------
+ * FB Functions
+ *------------------------------------------------------------------------------
+ */
+int emgd_framebuffer_init(struct drm_device *dev,
+			emgd_framebuffer_t *emgd_fb,
+			struct drm_mode_fb_cmd *mode_cmd,
+			unsigned long offset);
+static struct drm_framebuffer *emgd_user_framebuffer_create(
+								struct drm_device *dev,
+								struct drm_file *filp,
+								struct drm_mode_fb_cmd *r);
+RETURN_PROBE_TYPE  emgd_fb_probe(struct drm_device *dev);
+static int emgd_fb_panic(struct notifier_block *n,
+			unsigned long res,
+			void *panic_str);
+static void emgd_user_framebuffer_destroy (struct drm_framebuffer *fb);
+static int  emgd_user_framebuffer_create_handle(struct drm_framebuffer *fb,
+				struct drm_file *file_priv, unsigned int *handle);
+static void emgd_fb_restore(void);
+
+
+static const struct drm_mode_config_funcs emgd_mode_funcs = {
+	.fb_create  = emgd_user_framebuffer_create,
+	.PROBE_FUNC = emgd_fb_probe,
+	/*.output_poll_changed:  we don't support hotplug */
+};
+
+
+static struct notifier_block paniced = {
+	.notifier_call = emgd_fb_panic,
+};
+
+
+static const struct drm_framebuffer_funcs emgd_fb_funcs = {
+	.destroy       = emgd_user_framebuffer_destroy,
+	.create_handle = emgd_user_framebuffer_create_handle,
+};
+
+
+
+/*
+ * Called if something fails while trying to set up framebuffer based
+ * console.
+ */
+static int emgd_fb_panic(struct notifier_block *n,
+		unsigned long res,
+		void *panic_str)
+{
+	EMGD_DEBUG("Panic occurred, switch back to text console.");
+
+	emgd_fb_restore();
+
+	return 0;
+}
+
+
+/*
+ * emgd_fb_restore
+ *
+ * Restore the kernel's fbcon mode.
+ */
+void emgd_fb_restore(void)
+{
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * FIXME: Need to have the real crtc saved so it can be restored.
+	 */
+	if ((ret = drm_crtc_helper_set_config(&panic_mode)) != 0) {
+		EMGD_ERROR("Failed to restore crtc configuration: %d", ret);
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+/* Workqueue task function; needed during CRTC init */
+void emgd_flip_worker(struct work_struct *w);
+
+/*
+ * create_crtcs
+ *
+ * Creates crtcs. This function loops through the valid pipes, creating
+ * one crtc for each pipe.  This function assumes that dsp_alloc() has
+ * been run.
+ *
+ * TODO:  Move this to emgd_crtc.c?
+ *
+ * @param dev     (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return None
+ */
+void create_crtcs(struct drm_device *dev)
+{
+	emgd_crtc_t *emgd_crtc = NULL;
+	drm_emgd_priv_t *dev_priv = NULL;
+	igd_context_t *context = NULL;
+	igd_display_pipe_t *pipe = NULL;
+	int i, index = 0;
+	unsigned short *r, *g, *b;
+
+	EMGD_TRACE_ENTER;
+
+	dev_priv = ((drm_emgd_priv_t *)dev->dev_private);
+	context = dev_priv->context;
+
+	while ((pipe = context->mod_dispatch.dsp_get_next_pipe(context, pipe, 0))) {
+
+		/* FIXME: Why are we allocating the extra space for the
+		 * connectors here? */
+		emgd_crtc = kzalloc(sizeof(emgd_crtc_t) +
+			(INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
+			GFP_KERNEL);
+
+		if (emgd_crtc == NULL) {
+			EMGD_ERROR("create_crtcs: Failed to allocate CRTC structure.");
+			return;
+		}
+
+		spin_lock_init(&emgd_crtc->crtc_lock);
+
+		emgd_crtc->crtc_id     = 1 << index;
+		emgd_crtc->igd_pipe    = pipe;
+		dev_priv->crtcs[index] = emgd_crtc;
+		EMGD_DEBUG("Creating CRTC with ID: 0x%x, igd_pipe=0x%lx",
+			emgd_crtc->crtc_id, (unsigned long)pipe);
+
+
+		/* Hook up crtc functions */
+		drm_crtc_init(dev, &emgd_crtc->base, &emgd_crtc_funcs);
+		EMGD_DEBUG("  Created CRTC [%d]", emgd_crtc->base.base.id);
+
+		/* gamma */
+		drm_mode_crtc_set_gamma_size(&emgd_crtc->base, 256);
+
+		/* Set initial gamma values */
+		r = emgd_crtc->base.gamma_store;
+		g = emgd_crtc->base.gamma_store + 256;
+		b = emgd_crtc->base.gamma_store + 512;
+		for (i = 0; i < 256; i++) {
+			emgd_crtc->lut_r[i] = i;
+			emgd_crtc->lut_g[i] = i;
+			emgd_crtc->lut_b[i] = i;
+			emgd_crtc->lut_a[i] = 0;
+			r[i] = (i << 8);
+			g[i] = (i << 8);
+			b[i] = (i << 8);
+		}
+
+		/* Initialize workqueue task to wait for render completion on flips */
+		INIT_WORK(&emgd_crtc->flip_work, emgd_flip_worker);
+		emgd_crtc->flip_work_queued = 0;
+
+		/* Target FB that we're in the process of changing to */
+		emgd_crtc->newfb = NULL;
+
+		/* Target "ops complete" watermark to allow flip to proceed */
+		emgd_crtc->render_complete_at = 0;
+
+		/*
+		 * Are we expected to perform flip cleanup (sending userspace event
+		 * and such) on next vblank event?
+		 */
+		emgd_crtc->vblank_expected = 0;
+
+		/* Userspace 'flip done' event */
+		emgd_crtc->flip_event = NULL;
+
+		/* TODO: Create connector list */
+		emgd_crtc->mode_set.crtc       = &emgd_crtc->base;
+		emgd_crtc->mode_set.connectors =
+								(struct drm_connector **)(emgd_crtc + 1);
+		emgd_crtc->mode_set.num_connectors = 0;
+
+		/* Hook up crtc helper functions */
+		drm_crtc_helper_add(&emgd_crtc->base, &emgd_crtc_helper_funcs);
+		index++;
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * create_encoder
+ *
+ * Creates an encoder for the igd_port in the parameter.
+ *
+ * @param dev      (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param igd_port (IN) pointer to IGD display port.  (pd_driver must be valid)
+ *
+ * @return None
+ */
+static void create_encoder(struct drm_device *dev, igd_display_port_t *igd_port)
+{
+	emgd_encoder_t  *emgd_encoder;
+	emgd_crtc_t     *emgd_crtc;
+	struct drm_crtc *crtc;
+	unsigned long    drm_encoder_type;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Find the corresponding DRM encoder type */
+	switch(igd_port->port_type) {
+		case IGD_PORT_LVDS:
+			drm_encoder_type = DRM_MODE_ENCODER_LVDS;
+			break;
+
+		case IGD_PORT_DIGITAL:
+			drm_encoder_type = DRM_MODE_ENCODER_TMDS;
+			break;
+
+		case IGD_PORT_ANALOG:
+		case IGD_PORT_RGBA:
+		case IGD_PORT_TV:
+		case IGD_PORT_SDVO_ST:
+		case IGD_PORT_SDVO_ST_GPIO:
+		default:
+			EMGD_ERROR("Invalid Port Type");
+			return;
+	}
+
+
+	emgd_encoder = kzalloc(sizeof(emgd_encoder_t), GFP_KERNEL);
+	if (!emgd_encoder) {
+		EMGD_ERROR("Out of memory!");
+		return;
+	}
+
+
+	/* What we call "TWIN" is what KMS calls "CLONE".  None of the
+	 * platforms we currently support allow TWIN, so just set
+	 * the bits equal to the port type since a port can always
+	 * "TWIN" with itself */
+    emgd_encoder->clone_mask = igd_port->port_type;
+	/*
+	 * crtc's are identified by a 1 that is shifted over:
+	 * 11
+	 * ||_ pipe 0
+	 * |__ pipe 1
+	 * The order of the pipes is based on how they are initialized
+	 * in the drm.
+	 */
+	emgd_encoder->crtc_mask  = KMS_PIPE_ID(igd_port->port_features);
+	emgd_encoder->igd_port   = igd_port;
+	emgd_encoder->state.port = igd_port;
+	emgd_encoder->flags |= ENCODER_FLAG_FIRST_DPMS | ENCODER_FLAG_FIRST_ALTER;
+
+
+	/* Since we do not support TWIN and pipe assignment is fixed, the
+	 * current configuration is the only possible configuration */
+	emgd_encoder->base.possible_crtcs  = emgd_encoder->crtc_mask;
+	emgd_encoder->base.possible_clones = emgd_encoder->clone_mask;
+
+	/* Initialize the CRTC associated with this encoder.  We should
+	 * probably use the crtc_mask here to do the matching, but we
+	 * have the same info in our internal structures. */
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+		if ((emgd_crtc->igd_pipe->pipe_features & IGD_PORT_MASK) ==
+			emgd_encoder->igd_port->port_type) {
+			emgd_encoder->base.crtc = crtc;
+			break;
+		}
+	}
+
+
+	EMGD_DEBUG("Creating encoder=0x%lx on pipe=0x%lx, of port=0x%lx, "
+		"port_type=0x%lx",
+		(unsigned long)&(emgd_encoder->base), emgd_encoder->crtc_mask,
+		igd_port->port_number, igd_port->port_type);
+
+	drm_encoder_init(dev, &emgd_encoder->base, &emgd_encoder_funcs,
+						drm_encoder_type);
+	drm_encoder_helper_add(&emgd_encoder->base, &emgd_encoder_helper_funcs);
+}
+
+
+
+/**
+ * create_connector_properties
+ *
+ * Creates properties associated with the input connector.  Connector properties
+ * are what EMGD calls "port attributes."  The only difference is EMGD's port
+ * attributes are per-encoder, not per-connector.  For this implementation, we
+ * are assuming one connector per encoder.  With this assumption, we can draw
+ * a direct connection between "port attributes" and "connector properties."
+ *
+ * @param dev            (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param emgd_connector (IN) Selected connector
+ *
+ * @return None
+ */
+static void create_connector_properties(struct drm_device *dev,
+				emgd_connector_t *emgd_connector)
+{
+	igd_display_port_t *igd_port = emgd_connector->encoder->igd_port;
+	drm_emgd_priv_t    *priv     = emgd_connector->priv;
+	unsigned long       num_of_attributes = 0, num_of_properties = 0;
+	unsigned long       i, current_value;
+	pd_attr_t          *attributes;
+
+	struct drm_connector *drm_connector = &emgd_connector->base;
+	struct drm_property  *new_prop;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Get port attributes from the port driver */
+	priv->context->dispatch.get_attrs(priv->context,
+								igd_port->port_number,
+								&num_of_attributes,
+								&attributes);
+
+
+	/* If the connector has no attributes, then return */
+	if (0 >= num_of_attributes) {
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	emgd_connector->properties =
+		(struct drm_property **) kzalloc(sizeof(struct drm_property *) *
+									num_of_attributes, GFP_KERNEL);
+
+	if (NULL == emgd_connector->properties) {
+		EMGD_ERROR_EXIT("Failed to allocate emgd_connector->properties");
+	}
+
+
+	/* Convert port attributes to connector properties and attach them */
+	for(i = 0; i < num_of_attributes; i++) {
+
+		/* Invisible attributes are not settable so don't even report
+		 * it was a property */
+		if (attributes[i].flags & PD_ATTR_FLAG_USER_INVISIBLE) {
+			continue;
+		}
+
+		/* Instead of using DRM_MODE_PROP_xxxx types, it may be possible
+         * to use the PD_ATTR_TYPE_xxxx types, since the 2nd parameter
+		 * to drm_property_create is a flag */
+		switch (attributes[i].type) {
+			case PD_ATTR_TYPE_RANGE:
+			{
+				igd_range_attr_t *attr = (igd_range_attr_t *) &attributes[i];
+
+
+				new_prop = drm_property_create(dev, DRM_MODE_PROP_RANGE,
+									attr->name, 3);
+
+				if (NULL == new_prop) {
+					EMGD_ERROR("Failed to allocate new property");
+					continue;
+				}
+
+				new_prop->values[0] = attr->id;
+				new_prop->values[1] = attr->min;
+				new_prop->values[2] = attr->max;
+				current_value       = attr->current_value;
+
+				break;
+			}
+
+			case PD_ATTR_TYPE_BOOL:
+			{
+				igd_bool_attr_t *attr = (igd_bool_attr_t *) &attributes[i];
+
+
+				new_prop = drm_property_create(dev, DRM_MODE_PROP_RANGE,
+									attr->name, 2);
+
+				if (NULL == new_prop) {
+					EMGD_ERROR("Failed to allocate new property");
+					continue;
+				}
+
+				new_prop->values[0] = false;
+				new_prop->values[1] = true;
+				current_value       = attr->current_value;
+
+				break;
+			}
+
+			case PD_ATTR_TYPE_LIST:
+			case PD_ATTR_TYPE_LIST_ENTRY:
+			case PD_ATTR_TYPE_BUFFER:
+			default:
+				EMGD_ERROR("Unsupported PD Attribute type");
+				continue;
+		}
+
+		drm_connector_attach_property(drm_connector, new_prop, current_value);
+		emgd_connector->properties[num_of_properties++] = new_prop;
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * create_connectors
+ *
+ * Creates connectors associated with the encoder.
+ *
+ * This function currently supports one connector per encoder.  Further
+ * development required in the future to support encoders that have more
+ * than one connector.
+ *
+ * @param dev          (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param emgd_encoder (IN) Encoder to be examined.
+ *
+ * @return None
+ */
+static void create_connectors(struct drm_device *dev,
+				emgd_encoder_t *emgd_encoder)
+{
+	igd_display_port_t    *port = emgd_encoder->igd_port;
+	pd_driver_t           *pd   = port->pd_driver;
+	unsigned long          connector_type = DRM_MODE_CONNECTOR_LVDS;
+	emgd_connector_t      *emgd_connector;
+
+
+	EMGD_TRACE_ENTER;
+
+	switch (pd->type) {
+		case PD_DISPLAY_LVDS_EXT:
+		case PD_DISPLAY_LVDS_INT:
+			connector_type = DRM_MODE_CONNECTOR_LVDS;
+			break;
+
+		case PD_DISPLAY_FP:
+			connector_type = DRM_MODE_CONNECTOR_DVID;
+			break;
+
+		case PD_DISPLAY_CRT_EXT:
+		case PD_DISPLAY_CRT:
+			connector_type = DRM_MODE_CONNECTOR_VGA;
+			break;
+
+		case PD_DISPLAY_HDMI_INT:
+			connector_type = DRM_MODE_CONNECTOR_HDMIA;
+			break;
+
+		case PD_DISPLAY_HDMI_EXT:
+			connector_type = DRM_MODE_CONNECTOR_HDMIA;
+			break;
+
+		case PD_DISPLAY_DP_INT:
+			connector_type = DRM_MODE_CONNECTOR_DisplayPort;
+			break;
+
+		case PD_DISPLAY_TVOUT:
+		case PD_DISPLAY_TVFP:
+		case PD_DISPLAY_RGBA:
+		case PD_DISPLAY_TVOUT_INT:
+		case PD_DISPLAY_DRGB:
+			EMGD_ERROR("Unsupported connector type");
+			return;
+
+		default:
+			EMGD_ERROR("Unknown connector type");
+			return;
+	}
+
+
+	/* Allocate a new connector */
+	emgd_connector = kzalloc(sizeof(emgd_connector_t), GFP_KERNEL);
+	if (!emgd_connector) {
+		EMGD_ERROR("Out of memory!");
+		return;
+	}
+
+	drm_connector_init(dev, &emgd_connector->base, &emgd_connector_funcs,
+						connector_type);
+
+	drm_mode_connector_attach_encoder(&emgd_connector->base,
+		&emgd_encoder->base);
+
+	drm_connector_helper_add(&emgd_connector->base,
+		&emgd_connector_helper_funcs);
+
+	EMGD_DEBUG("Creating connector=0x%lx, encoder=0x%lx, type=0x%lx",
+		(unsigned long)&(emgd_connector->base),
+		(unsigned long)&(emgd_encoder->base), connector_type);
+
+	emgd_connector->encoder                          = emgd_encoder;
+	emgd_connector->priv                             = dev->dev_private;
+	emgd_connector->base.display_info.subpixel_order = SubPixelHorizontalRGB;
+	emgd_connector->base.interlace_allowed           = false;
+	emgd_connector->base.doublescan_allowed          = false;
+	emgd_connector->base.encoder                     = &emgd_encoder->base;
+
+	/* Create and attach connector properties */
+	create_connector_properties(dev, emgd_connector);
+
+
+#if 0
+        drm_connector_attach_property(&connector->base,
+                        dev->mode_config.scaling_mode_property,
+                        DRM_MODE_SCALE_FULLSCREEN);
+        drm_connector_attach_property(&connector->base,
+                       dev->mode_config.edid_property, 0);
+        drm_connector_attach_property(&connector->base,
+                       dev->mode_config.dpms_property, 0);
+#endif
+
+
+	drm_sysfs_connector_add(&emgd_connector->base);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_setup_outputs
+ *
+ * This function enumerates all the available outputs (physical connectors) by
+ * first initializing all the encoders in the system, and then querying
+ * the encoders for the connectors.
+ *
+ * Because we are adapting from EMGD, the real work behind detecting encoders
+ * has already been done by the time we get to this function.  Therefore,
+ * all we need to do is using existing EMGD HAL dispatch functions to complete
+ * the task.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return
+ */
+void emgd_setup_outputs(struct drm_device *dev)
+{
+	drm_emgd_priv_t         *priv            = dev->dev_private;
+	igd_context_t           *igd_context     = priv->context;
+	inter_module_dispatch_t *module_dispatch = &igd_context->mod_dispatch;
+	igd_display_port_t      *port            = NULL;
+	struct drm_encoder      *encoder;
+
+	EMGD_TRACE_ENTER;
+
+	/* Loop through all available ports.  What KMS calls "encoder" is a
+     * subset of what EMGD calls "port."
+     */
+	while ((port = module_dispatch->dsp_get_next_port(igd_context, port, 0))) {
+
+		/* If there is a port driver, then there's an encoder */
+		if (port->pd_driver) {
+			create_encoder(dev, port);
+		}
+	}
+
+
+	/* For each encoder, create the connectors on the encoder */
+	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+		emgd_encoder_t *emgd_encoder;
+
+		emgd_encoder = container_of(encoder, emgd_encoder_t, base);
+		create_connectors(dev, emgd_encoder);
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_modeset_init
+ *
+ * This is the main initialization entry point.  Called during driver load
+ * and does basic setup.
+ *
+ * @param dev      (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param full_kms (IN) If we are running with KMS enabled, then set this
+ *                      to true to do the full KMS initialization.  If not,
+ *                      then set this to false.
+ *
+ * @return None
+ */
+void emgd_modeset_init(struct drm_device *dev)
+{
+	drm_emgd_priv_t *devpriv = (drm_emgd_priv_t *)dev->dev_private;
+	int ret;
+	struct drm_encoder *encoder;
+
+	EMGD_TRACE_ENTER;
+	drm_mode_config_init(dev);  /* drm helper function */
+
+	dev->mode_config.min_width  = 0;
+	dev->mode_config.max_width  = 4096;
+	dev->mode_config.min_height = 0;
+	dev->mode_config.max_height = 2048;
+	dev->mode_config.funcs      = (void *)&emgd_mode_funcs;
+
+
+	/* OTC uses dev->agp->base for fb_base */
+	dev->mode_config.fb_base = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
+
+	/* Initialize VBLANK handling */
+	dev->irq_enabled      = true;
+	dev->max_vblank_count = 0xffffff; /* 24-bit frame counter */
+
+	ret = drm_vblank_init(dev, devpriv->num_crtc);
+	if (ret) {
+		EMGD_ERROR("Call to drm_vblank_init() failed.  drmWaitVBlank() "
+			"will not work.");
+		dev->irq_enabled = false;
+	}
+
+
+	/* Create the crtc's */
+	create_crtcs(dev);
+
+	drm_mode_create_scaling_mode_property(dev);
+	emgd_setup_outputs(dev);
+
+	/* The encoders need to be turned off to prevent the locking of some
+	   of the registers - before doing a modeset */
+    list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {                  
+        struct drm_encoder_helper_funcs *e_funcs = encoder->helper_private;               
+        (*e_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);                                     
+    }
+
+	drm_helper_disable_unused_functions(dev);
+
+	/* Initialize the framebuffer device */
+	emgd_fbdev_init(devpriv);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_modeset_destroy
+ *
+ * Clean up resources allocated in emgd_modeset_init.  Called during driver
+ * unload
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return None
+ */
+void emgd_modeset_destroy(struct drm_device *dev)
+{
+	drm_emgd_priv_t *priv = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	priv = dev->dev_private;
+	emgd_fbdev_destroy(priv);
+	kfree(priv->emgd_fbdev);
+	priv->emgd_fbdev = NULL;
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/*
+ * emgd_fb_probe
+ *
+ * Registers panic mode for the DRM to switch to.
+ *
+ * @param dev (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ *
+ * @return TBD
+ */
+RETURN_PROBE_TYPE emgd_fb_probe(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+	/* Set Panic Mode to 1024x768 because it is an acceptably generic mode */
+	//memset(&panic_mode, 0, sizeof(struct drm_mode_set));
+
+	/* In later kernels this function changes to "output_poll_changed", as
+	 * such, it is unclear if we need to implement it at all since we do not
+	 * support hot-plug
+	 */
+
+
+	/* Register a notifier to switch back to kernel console on panic */
+	atomic_notifier_chain_register(&panic_notifier_list, &paniced);
+
+	EMGD_TRACE_EXIT;
+	RETURN_PROBE;
+}
+EXPORT_SYMBOL(emgd_fb_probe);
+
+
+/**
+ * emgd_user_framebuffer_create
+ *
+ * Creates an instance for the framebuffer.
+ *
+ * @param dev      (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param filp     (IN)
+ * @param mode_cmd (IN) Input from the DRM_IOCTL_MODE_ADDFB
+ *
+ * @return pointer to allocated FB instance
+ */
+static struct drm_framebuffer *emgd_user_framebuffer_create(
+								struct drm_device *dev,
+								struct drm_file *filp,
+								struct drm_mode_fb_cmd *mode_cmd)
+{
+	emgd_framebuffer_t *emgd_fb;
+	int                 ret;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_fb = kzalloc(sizeof(emgd_framebuffer_t), GFP_KERNEL);
+	if (!emgd_fb) {
+		EMGD_ERROR("Failed to allocate an emgd_framebuffer_t");
+		return NULL;
+	}
+
+	/* Create a framebuffer instance */
+	ret = emgd_framebuffer_init(dev, emgd_fb, mode_cmd,
+			(unsigned long)mode_cmd->handle);
+	if (ret) {
+		EMGD_ERROR("Failed to create framebuffer instance.");
+		/* TODO: Free the allocation at mode_cmd->handle */
+		kfree(emgd_fb);
+		emgd_fb = NULL;
+		return NULL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return &emgd_fb->base;
+}
+
+
+/**
+ * emgd_framebuffer_init
+ *
+ * Creates an instance for the framebuffer.
+ *
+ * Maps an existing surface, which has been allocated via the PVR services,
+ * into the GTT to make it displayable and returns a 'drm_framebuffer'
+ * struct that can be used to refer to this surface in future KMS
+ * operations.  This function is called as a result of drmModeAddFB()
+ * being called in userspace; the handle passed to drmModeAddFB() should
+ * be the kernel meminfo handle (i.e., meminfo->hKernelMemInfo in userspace).
+ *
+ * @param dev      (IN) DRM per-device (e.g. one GMA) struct (in "drmP.h")
+ * @param emgd_fb  (IN)
+ * @param mode_cmd (IN) Input from the DRM_IOCTL_MODE_ADDFB
+ * @param handle   (IN) Handle of allocation.  The handle will be the
+ *                      PVR meminfo handle.  The initial framebuffer,
+ *                      which is allocated directly by the GMM before
+ *                      the PVR services starts up is a special case;
+ *                      a handle of 0 will refer to the initial framebuffer.
+ *
+ * @return int return value
+ */
+int emgd_framebuffer_init(struct drm_device *dev,
+		emgd_framebuffer_t *emgd_fb,
+		struct drm_mode_fb_cmd *mode_cmd,
+		unsigned long handle)
+{
+	drm_emgd_priv_t *dev_priv = NULL;
+	igd_context_t *context = NULL;
+	PVRSRV_PER_PROCESS_DATA *pvr_perproc;
+	PVRSRV_KERNEL_MEM_INFO *pvr_meminfo;
+	struct page **pagelist;
+	unsigned long numpages, gtt_offset, page_offset;
+	int ret, pid;
+
+	EMGD_TRACE_ENTER;
+
+	dev_priv = ((drm_emgd_priv_t *)dev->dev_private);
+	context = dev_priv->context;
+
+	if (handle == EMGD_INITIAL_FRAMEBUFFER) {
+		/*
+		 * Special case:  initial EMGD framebuffer is allocated via GTT instead
+		 * of through the PVR services.  It has no PVR meminfo handle.
+		 */
+		ret = drm_framebuffer_init(dev, &emgd_fb->base, &emgd_fb_funcs);
+		if (ret) {
+		    EMGD_ERROR("Failed to create framebuffer instance.");
+		    return -ENOMEM;
+		}
+
+		drm_helper_mode_fill_fb_struct(&emgd_fb->base, mode_cmd);
+
+		/*
+		 * Initial framebuffer offset is stored in the device
+		 * private structure.
+		 */
+		emgd_fb->type       = GMM_FRAMEBUFFER;
+		emgd_fb->gtt_offset = dev_priv->initfb_info.fb_base_offset;
+		emgd_fb->handle     = handle;
+
+	} else {
+		/* Fetch PVR services 'per-process' data structure */
+		pid = OSGetCurrentProcessIDKM();
+		pvr_perproc = PVRSRVPerProcessData(pid);
+
+		/* Look up the kernel-side meminfo for the handle passed in*/
+		ret = PVRSRVLookupHandle(pvr_perproc->psHandleBase,
+				(void**)&pvr_meminfo,
+			(IMG_HANDLE)handle, PVRSRV_HANDLE_TYPE_MEM_INFO);
+		if (ret != PVRSRV_OK) {
+			EMGD_ERROR("Buffer handle is not a valid PVR surface.");
+			return -EINVAL;
+		}
+
+		/*
+		 * Now fetch the page list, number of pages, and offset into the first
+		 * page for this buffer.
+		 */
+		ret = PVRSRVGetPageListKM(pvr_meminfo, &pagelist, &numpages,
+				&page_offset);
+		if (ret != PVRSRV_OK) {
+			EMGD_ERROR("Failed to get pagelist for PVR surface.");
+			return -EINVAL;
+		}
+
+		/* Map this page list into the GTT */
+		ret = context->dispatch.gmm_import_pages((void**)pagelist, &gtt_offset,
+			numpages);
+		if (ret < 0) {
+			EMGD_ERROR("Failed to map pagelist into GTT.");
+			return -ENOMEM;
+		}
+
+		/* Create a framebuffer instance */
+		ret = drm_framebuffer_init(dev, &emgd_fb->base, &emgd_fb_funcs);
+		if (ret) {
+			EMGD_ERROR("Failed to create framebuffer instance.");
+			return -ENOMEM;
+		}
+
+		/* Fill the EMGD framebuffer structure */
+		drm_helper_mode_fill_fb_struct(&emgd_fb->base, mode_cmd);
+
+		emgd_fb->type = PVR_FRAMEBUFFER;
+		emgd_fb->pvr_meminfo = pvr_meminfo;
+		emgd_fb->pagelist = pagelist;
+		emgd_fb->gtt_offset = gtt_offset + page_offset;
+		emgd_fb->handle     = handle;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*
+ * emgd_user_framebuffer_destroy
+ *
+ * clean up and remove a framebuffer instance.
+ *
+ */
+static void emgd_user_framebuffer_destroy (struct drm_framebuffer *fb)
+{
+	drm_emgd_priv_t *dev_priv = NULL;
+	emgd_framebuffer_t *emgd_fb = NULL;
+	igd_context_t *context = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	dev_priv = ((drm_emgd_priv_t *)fb->dev->dev_private);
+	context = dev_priv->context;
+	emgd_fb = container_of(fb, emgd_framebuffer_t, base);
+
+	/* Unmap pages from GTT */
+	if (emgd_fb->type == PVR_FRAMEBUFFER) {
+		/* PVR-allocated pages imported into GTT:  just unmap */
+		EMGD_DEBUG("Unmapping imported PVR framebuffer pages at %lu.", emgd_fb->gtt_offset);
+		context->dispatch.gmm_release_import(emgd_fb->gtt_offset);
+	} else {
+		/* GMM-allocated pages (initial framebuffer): unmap and free pages */
+		EMGD_DEBUG("Unmapping and freeing GMM framebuffer pages at %lu.", emgd_fb->gtt_offset);
+		context->dispatch.gmm_free(emgd_fb->gtt_offset);
+	}
+
+	drm_framebuffer_cleanup(fb);
+	kfree(emgd_fb);
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/*
+ * emgd_user_framebuffer_create_handle
+ *
+ * Returns 'a' handle associated with the drm_framebuffer given in the
+ * parameter.  In our case, we return 'the' handle because our handles
+ * are already cross-process compatible, and PVR is keeping track of
+ * the reference count so buffers don't get destroyed unless it is not
+ * used.
+ *
+ * @param fb (IN)        DRM framebuffer to look up
+ * @param file_priv (IN) Not used
+ * @param handle (OUT)   
+ *
+ * @return 0 on success
+ * @return -EINVAL on failure
+ */
+static int emgd_user_framebuffer_create_handle(struct drm_framebuffer *fb,
+		struct drm_file *file_priv,
+		unsigned int *handle)
+{
+	emgd_framebuffer_t *emgd_fb;
+	int ret = -EINVAL;
+
+	EMGD_TRACE_ENTER;
+
+	/* To avoid compiler warning. */
+	(void)file_priv;
+
+	emgd_fb = container_of(fb, emgd_framebuffer_t, base);
+
+	if (NULL != emgd_fb) {
+		*handle = emgd_fb->handle;
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+
+
+/**
+ * emgd_fbdev_init
+ *
+ * Allocates and initializes a framebuffer device.  Through
+ * drm_fb_helper_initial_config(), this function will set a sane mode and
+ * allocate a framebuffer surface to go with the framebuffer device.
+ *
+ * @param priv (IN) EMGD private DRM data structure
+ *
+ * @return 0 on success
+ */
+
+static int emgd_fbdev_init(drm_emgd_priv_t *priv)
+{
+	emgd_fbdev_t *emgd_fbdev;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_fbdev = kzalloc(sizeof(emgd_fbdev_t), GFP_KERNEL);
+	if (!emgd_fbdev) {
+		return -ENOMEM;
+	}
+
+	emgd_fbdev->priv         = priv;
+	priv->emgd_fbdev         = emgd_fbdev;
+
+	emgd_fbcon_initial_config(emgd_fbdev);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/**
+ * emgd_fbdev_destroy
+ *
+ * Cleans up resources allocated during emgd_fbdev_init.
+ * Since emgd_fbdev_init() calls drm_fb_helper_initial_config() which leads
+ * to a call to emgd_fb_find_or_create_single(), we are also cleaning up
+ * resources allocated in that function as well.  It is unclear if this
+ * clean up is necessary because KMS APIs maybe smart enough to call
+ * the corresponding destory function.
+ *
+ * @param priv (IN) EMGD private DRM data structure
+ *
+ * @return None
+ */
+static void emgd_fbdev_destroy(drm_emgd_priv_t *priv)
+{
+	emgd_fbdev_t           *emgd_fbdev = NULL;
+	struct drm_framebuffer *fb         = NULL;
+	struct fb_info         *info       = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_fbdev = priv->emgd_fbdev;
+	fb         = &emgd_fbdev->emgd_fb->base;
+
+	if (priv->fbdev) {
+		info = priv->fbdev;
+
+		unregister_framebuffer(info);
+
+		if (info->cmap.len) {
+			fb_dealloc_cmap(&info->cmap);
+		}
+
+		priv->context->dispatch.gmm_unmap(info->screen_base);
+		framebuffer_release(info);	
+	}
+
+	drm_framebuffer_cleanup(fb); /* Is this the right place to call this? */
+	priv->context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+
+	kfree(emgd_fbdev->emgd_fb);
+	emgd_fbdev->emgd_fb = NULL;
+
+	EMGD_TRACE_EXIT;
+}
+
+
+
+#if 0
+/*------------------------------------------------------------------------------
+ * drm_fb_helper related functions.  These will be removed once we have our
+ * own version of drm_fb_helper (in emgd_fbcon.c)
+ *------------------------------------------------------------------------------
+ */
+static int emgd_fb_find_or_create_single(struct drm_fb_helper *helper,
+			struct drm_fb_helper_surface_size *sizes);
+static void emgd_fb_gamma_set(struct drm_crtc *crtc,
+				u16 red, u16 green, u16 blue, int regno);
+static void emgd_fb_gamma_get(struct drm_crtc *crtc,
+				u16 *red, u16 *green, u16 *blue, int regno);
+
+
+
+/* FIXME:  This should be removed. */
+static struct drm_fb_helper_funcs emgd_fb_helper_funcs = {
+	.gamma_set = emgd_fb_gamma_set,
+	.gamma_get = emgd_fb_gamma_get,
+	.fb_probe  = emgd_fb_find_or_create_single,
+};
+
+
+
+/**
+ * emgd_fb_create
+ *
+ * This function creates a frame buffer using the surface information contained
+ * in surface_info.  The working assumption is this function will only be
+ * called once at initialization time.  So the buffer allocated here is
+ * for the console.
+ *
+ * @param emgd_fbdev   (IN) Framebuffer devices to attach the new FB to
+ * @param surface_info (IN) Information about the surface to be allocated
+ */
+static int emgd_fb_create(emgd_fbdev_t *emgd_fbdev,
+		struct drm_fb_helper_surface_size *surface_info)
+{
+	drm_emgd_priv_t        *priv    = emgd_fbdev->priv;
+	struct drm_device      *dev     = priv->ddev;
+	struct fb_info         *info    = NULL;
+	struct drm_mode_fb_cmd  mode_cmd;
+	struct drm_framebuffer *fb       = NULL;
+	igd_context_t          *context  = NULL;
+	int                     ret;
+	unsigned long           size;
+	unsigned long           offset = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	context = priv->context;
+
+	priv->initfb_info.width        = surface_info->surface_width;
+	priv->initfb_info.height       = surface_info->surface_height;
+	priv->initfb_info.screen_pitch = 0;
+	priv->initfb_info.pixel_format = IGD_PF_ARGB32;
+
+	ret = context->dispatch.gmm_alloc_surface(
+								&priv->initfb_info.fb_base_offset,
+								 priv->initfb_info.pixel_format,
+								&priv->initfb_info.width,
+								&priv->initfb_info.height,
+								&priv->initfb_info.screen_pitch,
+								&size,
+								IGD_GMM_ALLOC_TYPE_RESERVATION,
+								&priv->initfb_info.flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Allocation of buffer failed: %d", ret);
+		return -ENOMEM;
+	}
+
+	priv->initfb_info.allocated = 1;
+	priv->initfb_info.visible_offset = 0;
+
+	mode_cmd.handle = EMGD_INITIAL_FRAMEBUFFER;
+	mode_cmd.pitch  = priv->initfb_info.screen_pitch;
+	mode_cmd.width  = surface_info->surface_width;
+	mode_cmd.height = surface_info->surface_height;
+	mode_cmd.bpp    = surface_info->surface_bpp;
+	mode_cmd.depth  = surface_info->surface_depth;
+
+	/* Allocate fb_info */
+	info = kzalloc(sizeof(struct fb_info), GFP_KERNEL);
+	if (info == NULL) {
+		EMGD_ERROR_EXIT("Allocation of fb_info failed");
+		context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+		return -ENOMEM;
+	}
+	info->par   = emgd_fbdev;
+
+	/* Allocate emgd_framebuffer_t */
+	emgd_fbdev->emgd_fb = kzalloc(sizeof(emgd_framebuffer_t), GFP_KERNEL);
+	if (!emgd_fbdev->emgd_fb) {
+		EMGD_ERROR_EXIT("Allocation of emgd_framebuffer_t failed");
+		context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+		kfree(info);
+		info = NULL;
+		return -ENOMEM;
+	}
+
+	/* Initialize the framebuffer */
+	ret = emgd_framebuffer_init(dev, emgd_fbdev->emgd_fb, &mode_cmd,
+			EMGD_INITIAL_FRAMEBUFFER);
+	if (ret) {
+		context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+		kfree(info);
+		info = NULL;
+		kfree(emgd_fbdev->emgd_fb);
+		emgd_fbdev->emgd_fb = NULL;
+		return ret;
+	}
+
+
+
+	info->flags = FBINFO_DEFAULT /* | FBINFO_CAN_FORCE_OUTPUT */;
+	info->fbops = (struct fb_ops*) &emgd_fb_ops;
+	strcpy(info->fix.id, "emgdfb"); /* fix.id is 16 bytes long */
+	fb = &emgd_fbdev->emgd_fb->base;
+	emgd_fbdev->helper.fb    = fb;
+	emgd_fbdev->helper.fbdev = info;
+	priv->fbdev              = info;
+
+	EMGD_DEBUG("EMGD: Call fb_alloc_cmap()");
+	ret = fb_alloc_cmap(&info->cmap, 256, 0);
+	if (ret) {
+		EMGD_ERROR("%s: Can't allocate color map", info->fix.id);
+		//mutex_unlock(&dev->struct_mutex);
+		return -ENOMEM;
+	}
+
+	/*
+	 * Does this need to be filled in and if so, with what?  Right now
+	 * I'm trying to reuse the framebuffer that was already configured by
+	 * the EMGD code.
+	 *
+	 * setup aperture base/size for vesafb takeover
+	 */
+	info->apertures = alloc_apertures(1);
+	if (!info->apertures) {
+		EMGD_ERROR("%s: Can't allocate apertures", info->fix.id);
+		//mutex_unlock(&dev->struct_mutex);
+		return -ENOMEM;
+	}
+
+	info->apertures->ranges[0].base =
+		(unsigned long)priv->context->device_context.virt_gttadr;
+	info->apertures->ranges[0].size =
+		priv->context->device_context.gatt_pages << PAGE_SHIFT;
+
+	//mutex_unlock(&dev->struct_mutex);
+	/*
+	 * FIXME: What is fix.smem_start vs screen_base?
+	 *
+	 * smem_start is the start of frame buffer mem (physical address), does
+	 * that mean GTT or that it expects a physical contigous block in real
+	 * memory?
+	 *
+	 * screen_base is a virtual address
+	 *
+	 */
+	/* Set up framebuffer surface */
+	EMGD_DEBUG("EMGD: Call pci_resource_start()");
+	info->fix.smem_start =  pci_resource_start(dev->pdev, PSB_GATT_RESOURCE) +
+		priv->initfb_info.fb_base_offset;
+	info->fix.smem_len = size;
+
+	/* Get kernel virtual memory address of framebuffer */
+	EMGD_DEBUG("EMGD: Call gmm_map()");
+	info->screen_base =
+		priv->context->dispatch.gmm_map(priv->initfb_info.fb_base_offset);
+	if (!info->screen_base) {
+		EMGD_ERROR("%s: Can't map framebuffer surface", info->fix.id);
+		return -ENOSPC;
+	}
+	info->screen_size = size;
+
+	info->pixmap.size         = 64 * 1024;
+	info->pixmap.buf_align    = 8;
+	info->pixmap.access_align = 32;
+	info->pixmap.flags        = FB_PIXMAP_SYSTEM;
+	info->pixmap.scan_align   = 1;
+
+	EMGD_DEBUG("Frame buffer %dx%d @ 0x%08lx",
+			fb->width, fb->height, offset);
+
+	EMGD_DEBUG("EMGD: Call vga_switcheroo_client_fb_set()");
+	vga_switcheroo_client_fb_set(dev->pdev, info);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/*
+ * emgd_fb_gamma_set
+ *
+ * Set the gamma values for a framebuffer for a particular
+ * index.
+ *
+ * @param crtc     (IN) the drm pipe
+ * @param red      (IN) red gamma value
+ * @param green    (IN) green gamma value
+ * @param blue     (IN) blue gamma value
+ * @param regno    (IN) index value which we are setting
+ *
+ * @return None
+ */
+static void emgd_fb_gamma_set(struct drm_crtc *crtc,
+		u16 red, u16 green, u16 blue,
+		int regno)
+{
+	emgd_crtc_t *emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+	EMGD_TRACE_ENTER;
+	emgd_crtc->lut_r[regno] = red >> 8;
+	emgd_crtc->lut_g[regno] = green >> 8;
+	emgd_crtc->lut_b[regno] = blue >> 8;
+	EMGD_TRACE_EXIT;
+}
+
+
+/*
+ * emgd_fb_gamma_get
+ *
+ * Set the gamma values for a framebuffer for a particular
+ * index.
+ *
+ * @param crtc     (IN) the drm pipe
+ * @param red      (OUT) red gamma value
+ * @param green    (OUT) green gamma value
+ * @param blue     (OUT) blue gamma value
+ * @param regno    (IN) index value which we are getting
+ *
+ * @return None
+ */
+static void emgd_fb_gamma_get(struct drm_crtc *crtc,
+		u16 *red, u16 *green, u16 *blue,
+		int regno)
+{
+	emgd_crtc_t *emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+	EMGD_TRACE_ENTER;
+	*red = (emgd_crtc->lut_r[regno] << 8);
+	*blue = (emgd_crtc->lut_b[regno] << 8);
+	*green = (emgd_crtc->lut_g[regno] << 8);
+	EMGD_TRACE_EXIT;
+}
+
+
+
+/**
+ * emgd_fb_find_or_create_single
+ *
+ * If there is no frame buffer associated with "helper", then allocate a new
+ * one.  Otherwise, reuse the existing one.
+ *
+ * @param helper       (IN) TBD
+ * @param surface_info (IN) Surface info
+ *
+ * @return None
+ */
+static int emgd_fb_find_or_create_single(struct drm_fb_helper *helper,
+			struct drm_fb_helper_surface_size *surface_info)
+{
+	emgd_fbdev_t *fbdev  = container_of(helper, emgd_fbdev_t, helper);
+	int           new_fb = false;
+
+
+	EMGD_TRACE_ENTER;
+
+	if (!helper->fb) {
+		int ret = emgd_fb_create(fbdev, surface_info);
+		if (ret) {
+			return ret;
+		}
+
+		new_fb = true;
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return new_fb;
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_fbcon.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_fbcon.c
new file mode 100644
index 0000000..5132d33
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_fbcon.c
@@ -0,0 +1,805 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_fbcon.c
+ * $Revision: 1.4 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2011, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Framebuffer Console related functions.  This is the equivalent of
+ *  what is in drm_fb_helper.c, a set of functions for configuring the
+ *  framebuffer console. 
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.oal
+
+#include <drmP.h>
+#include <drm_crtc_helper.h>
+#include <linux/vga_switcheroo.h>
+
+
+#include "user_config.h"
+#include "drm_emgd_private.h"
+#include "mode_dispatch.h"
+
+
+/*------------------------------------------------------------------------------
+ * Global Variables
+ *------------------------------------------------------------------------------
+ */
+extern emgd_drm_config_t config_drm;
+
+
+
+/*------------------------------------------------------------------------------
+ * Formal Declaration
+ *------------------------------------------------------------------------------
+ */
+extern int emgd_framebuffer_init(struct drm_device *dev,
+			emgd_framebuffer_t *emgd_fb,
+			struct drm_mode_fb_cmd *mode_cmd,
+			unsigned long handle);
+
+
+/* Sets up initial display configuration */
+int  emgd_fbcon_initial_config(emgd_fbdev_t *emgd_fbdev);
+
+
+/*------------------------------------------------------------------------------
+ * These are called by the framebuffer console
+ *------------------------------------------------------------------------------
+ */
+static int  alloc_initial_fb(emgd_fbdev_t *emgd_fbdev);
+static void fill_fix(emgd_fbdev_t *emgd_fbdev, struct fb_info *info);
+static void fill_var(emgd_fbdev_t *emgd_fbdev, struct fb_info *info);
+
+
+
+/*------------------------------------------------------------------------------
+ * FBCON Functions
+ * These are called by the framebuffer console
+ *------------------------------------------------------------------------------
+ */
+static int emgd_fbcon_setcolreg(unsigned int regno,
+			unsigned int red, unsigned int green, unsigned int blue,
+			unsigned int transp, struct fb_info *info);
+static int emgd_fbcon_pan_display(struct fb_var_screeninfo *var,
+			struct fb_info *info);
+static int emgd_fbcon_set_par(struct fb_info *info);
+static int emgd_fbcon_check_var(struct fb_var_screeninfo *var,
+			struct fb_info *info);
+static int emgd_fbcon_blank(int blank, struct fb_info *info);
+static int emgd_fbcon_setcmap(struct fb_cmap *cmap, struct fb_info *info);
+
+
+/* This is called from within FBCON, the framebuffer console */
+const struct fb_ops emgd_fb_ops = {
+	.owner          = THIS_MODULE,
+	.fb_check_var   = emgd_fbcon_check_var,
+	.fb_set_par     = emgd_fbcon_set_par,
+	.fb_setcolreg   = emgd_fbcon_setcolreg,
+	.fb_fillrect    = cfb_fillrect,
+	.fb_copyarea    = cfb_copyarea,
+	.fb_imageblit   = cfb_imageblit,
+	.fb_pan_display = emgd_fbcon_pan_display,
+	.fb_blank       = emgd_fbcon_blank,
+	.fb_setcmap     = emgd_fbcon_setcmap,
+	/* .fb_debug_enter = drm_fb_helper_debug_enter, */
+	/* .fb_debug_leave = drm_fb_helper_debug_leave, */
+};
+
+
+
+static void fill_fix(emgd_fbdev_t *emgd_fbdev, struct fb_info *info)
+{
+	struct drm_framebuffer *fb = &emgd_fbdev->emgd_fb->base;
+
+	info->fix.type        = FB_TYPE_PACKED_PIXELS;
+	info->fix.visual      = FB_VISUAL_TRUECOLOR;
+	info->fix.mmio_start  = 0;
+	info->fix.mmio_len    = 0;
+	info->fix.type_aux    = 0;
+	info->fix.xpanstep    = 1; /* doing it in hw */
+	info->fix.ypanstep    = 1; /* doing it in hw */
+	info->fix.ywrapstep   = 0;
+	info->fix.accel       = FB_ACCEL_NONE;
+	info->fix.type_aux    = 0;
+	info->fix.line_length = fb->pitch;
+}
+
+
+
+/**
+ * fill_var
+ *
+ * Fills in the fb_info structure.  This function is called by alloc_init_fb,
+ * and as such an actual mode would not have been set yet.  This means we
+ * don't really know what "var.xres" and "var.yres" will be, so we have to
+ * make the assumption that "config_drm->width" and "config_drm->height"
+ * sepcify the resolution for the eventual mode.
+ *
+ * @param emgd_fbdev [IN]    FB device that contains the relevant information
+ * @param info       [INOUT] fb_info structure to fill in
+ *
+ * @return 0 on success, an error code otherwise
+ */
+static void fill_var(emgd_fbdev_t *emgd_fbdev, struct fb_info *info)
+{
+	struct drm_framebuffer *fb = &emgd_fbdev->emgd_fb->base;
+
+
+	/* Actual resolution for the mode.  We are assuming here that the
+	 * mode requested through config_drm->width and config_drm->height can
+	 * be set successfully */
+	info->var.xres           = config_drm.width;
+	info->var.yres           = config_drm.height;
+
+	/* Size of the framebuffer */
+	info->var.xres_virtual   = fb->width;
+	info->var.yres_virtual   = fb->height;
+
+	info->pseudo_palette     = emgd_fbdev->pseudo_palette;
+	info->var.bits_per_pixel = fb->bits_per_pixel;
+	info->var.accel_flags    = FB_ACCELF_TEXT;
+	info->var.xoffset        = 0;
+	info->var.yoffset        = 0;
+	info->var.activate       = FB_ACTIVATE_NOW;
+	info->var.height         = -1;
+	info->var.width          = -1;
+
+	switch (fb->depth) {
+	case 8:
+		info->var.red.offset = 0;
+		info->var.green.offset = 0;
+		info->var.blue.offset = 0;
+		info->var.red.length = 8; /* 8bit DAC */
+		info->var.green.length = 8;
+		info->var.blue.length = 8;
+		info->var.transp.offset = 0;
+		info->var.transp.length = 0;
+		break;
+	case 15:
+		info->var.red.offset = 10;
+		info->var.green.offset = 5;
+		info->var.blue.offset = 0;
+		info->var.red.length = 5;
+		info->var.green.length = 5;
+		info->var.blue.length = 5;
+		info->var.transp.offset = 15;
+		info->var.transp.length = 1;
+		break;
+	case 16:
+		info->var.red.offset = 11;
+		info->var.green.offset = 5;
+		info->var.blue.offset = 0;
+		info->var.red.length = 5;
+		info->var.green.length = 6;
+		info->var.blue.length = 5;
+		info->var.transp.offset = 0;
+		break;
+	case 24:
+		info->var.red.offset = 16;
+		info->var.green.offset = 8;
+		info->var.blue.offset = 0;
+		info->var.red.length = 8;
+		info->var.green.length = 8;
+		info->var.blue.length = 8;
+		info->var.transp.offset = 0;
+		info->var.transp.length = 0;
+		break;
+	case 32:
+		info->var.red.offset = 16;
+		info->var.green.offset = 8;
+		info->var.blue.offset = 0;
+		info->var.red.length = 8;
+		info->var.green.length = 8;
+		info->var.blue.length = 8;
+		info->var.transp.offset = 24;
+		info->var.transp.length = 8;
+		break;
+	default:
+		break;
+	}
+}
+
+
+
+/**
+ * alloc_initial_fb
+ *
+ * This function creates a frame buffer using the config_drm.width and
+ * config_drm.height.  The working assumption is this function will only be
+ * called once at initialization time.  So the buffer allocated here is
+ * for the console.
+ * FIXME:  need to make sure resources are freed properly for all the
+ *         failed cases.
+ *
+ * @param emgd_fbdev [IN] Framebuffer device to allocate a buffer for.
+ *
+ * @return 0 on success, an error code otherwise
+ */
+static int alloc_initial_fb(emgd_fbdev_t *emgd_fbdev)
+{
+	struct fb_info         *info     = NULL;
+	struct drm_device      *dev      = emgd_fbdev->priv->ddev;
+	drm_emgd_priv_t        *priv     = emgd_fbdev->priv;
+	igd_context_t          *context;
+	int                     ret;
+	unsigned long           size;
+	struct drm_mode_fb_cmd  mode_cmd;
+
+	EMGD_TRACE_ENTER;
+
+
+	context = priv->context;
+
+	memset(&priv->initfb_info, 0, sizeof(priv->initfb_info));
+   	priv->initfb_info.width        = config_drm.width;
+	priv->initfb_info.height       = config_drm.height;
+	priv->initfb_info.pixel_format = IGD_PF_ARGB32;
+
+	/* The initial framebuffer is a displayable surface. */
+	priv->initfb_info.flags = (priv->initfb_info.flags & IGD_FB_FLAGS_MASK) |
+                							IGD_SURFACE_DISPLAY;
+
+	ret = context->dispatch.gmm_alloc_surface(
+								&priv->initfb_info.fb_base_offset,
+								 priv->initfb_info.pixel_format,
+								&priv->initfb_info.width,
+								&priv->initfb_info.height,
+								&priv->initfb_info.screen_pitch,
+								&size,
+								IGD_GMM_ALLOC_TYPE_RESERVATION,
+								&priv->initfb_info.flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Allocation of buffer failed: %d", ret);
+		return -ENOMEM;
+	}
+
+	priv->initfb_info.allocated      = 1;
+	priv->initfb_info.visible_offset = 0;
+
+
+	/* Allocate emgd_framebuffer_t */
+	emgd_fbdev->emgd_fb = kzalloc(sizeof(emgd_framebuffer_t), GFP_KERNEL);
+
+	if (!emgd_fbdev->emgd_fb) {
+		EMGD_ERROR_EXIT("Allocation of emgd_framebuffer_t failed");
+		context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+		return -ENOMEM;
+	}
+
+
+	/* Initialize emgd_framebuffer_t */
+	mode_cmd.handle = EMGD_INITIAL_FRAMEBUFFER;
+	mode_cmd.pitch  = priv->initfb_info.screen_pitch;
+	mode_cmd.width  = priv->initfb_info.width;
+	mode_cmd.height = priv->initfb_info.height;
+	mode_cmd.bpp    = IGD_PF_BPP(priv->initfb_info.pixel_format);
+	mode_cmd.depth  = mode_cmd.bpp;  /* Ok for 32bpp, may not work for 16bpp */
+
+	ret = emgd_framebuffer_init(dev, emgd_fbdev->emgd_fb, &mode_cmd,
+			EMGD_INITIAL_FRAMEBUFFER);
+	if (ret) {
+		kfree(emgd_fbdev->emgd_fb);
+		emgd_fbdev->emgd_fb = NULL;
+
+		context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+
+		return ret;
+	}
+
+
+	/* Allocate fb_info */
+	info = kzalloc(sizeof(struct fb_info), GFP_KERNEL);
+	if (NULL == info) {
+		EMGD_ERROR_EXIT("Allocation of fb_info failed");
+		drm_framebuffer_cleanup(&emgd_fbdev->emgd_fb->base);
+
+		kfree(emgd_fbdev->emgd_fb);
+		emgd_fbdev->emgd_fb = NULL;
+
+		context->dispatch.gmm_free(priv->initfb_info.fb_base_offset);
+
+		return -ENOMEM;
+	}
+
+
+	info->par   = emgd_fbdev; /* Private data for all FBCON functions  */
+	info->flags = FBINFO_DEFAULT; /* | FBINFO_CAN_FORCE_OUTPUT */
+	info->fbops = (struct fb_ops*) &emgd_fb_ops;
+	strcpy(info->fix.id, "emgdfb"); /* fix.id is 16 bytes long */
+
+	priv->fbdev = info;
+
+	EMGD_DEBUG("EMGD: Call fb_alloc_cmap()");
+	ret = fb_alloc_cmap(&info->cmap, 256, 0);
+	if (ret) {
+		EMGD_ERROR("%s: Can't allocate color map", info->fix.id);
+
+		return -ENOMEM;
+	}
+
+	/*
+	 * Does this need to be filled in and if so, with what?  Right now
+	 * I'm trying to reuse the framebuffer that was already configured by
+	 * the EMGD code.
+	 *
+	 * setup aperture base/size for vesafb takeover
+	 */
+	info->apertures = alloc_apertures(1);
+	if (!info->apertures) {
+		EMGD_ERROR("%s: Can't allocate apertures", info->fix.id);
+		return -ENOMEM;
+	}
+
+	info->apertures->ranges[0].base =
+		(unsigned long)context->device_context.virt_gttadr;
+	info->apertures->ranges[0].size =
+		context->device_context.gatt_pages << PAGE_SHIFT;
+
+
+	/*
+	 * FIXME: What is fix.smem_start vs screen_base?
+	 *
+	 * smem_start is the start of frame buffer mem (physical address), does
+	 * that mean GTT or that it expects a physical contigous block in real
+	 * memory?
+	 *
+	 * screen_base is a virtual address
+	 *
+	 */
+	/* Set up framebuffer surface */
+	EMGD_DEBUG("EMGD: Call pci_resource_start()");
+	info->fix.smem_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE) +
+						   priv->initfb_info.fb_base_offset;
+	info->fix.smem_len   = size;
+
+	/* Get kernel virtual memory address of framebuffer */
+	EMGD_DEBUG("EMGD: Call gmm_map()");
+	info->screen_base =
+			context->dispatch.gmm_map(priv->initfb_info.fb_base_offset);
+	if (!info->screen_base) {
+		EMGD_ERROR("%s: Can't map framebuffer surface", info->fix.id);
+		return -ENOSPC;
+	}
+
+	info->screen_size         = size;
+	info->pixmap.size         = 64 * 1024;
+	info->pixmap.buf_align    = 8;
+	info->pixmap.access_align = 32;
+	info->pixmap.flags        = FB_PIXMAP_SYSTEM;
+	info->pixmap.scan_align   = 1;
+
+	/* Initialize info->fix and info->var */
+	fill_fix(emgd_fbdev, info);
+	fill_var(emgd_fbdev, info);
+
+	EMGD_DEBUG("Frame buffer %dx%d @ 0x%08lx",
+			priv->initfb_info.width,
+			priv->initfb_info.height, priv->initfb_info.fb_base_offset);
+
+	EMGD_DEBUG("EMGD: Call vga_switcheroo_client_fb_set()");
+	vga_switcheroo_client_fb_set(dev->pdev, info);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+/**
+ * emgd_fbcon_setcmap
+ *
+ * Sets color map for the framebuffer console device.  For now, we will set
+ * both CRTC to the same color map, regardless of which display configuration
+ * we are in.  There may be a case in the future where we will have to set
+ * the color map for both CRTCs differently.
+ *
+ * We will also assume that we are dealing with FB_VISUAL_TRUECOLOR because
+ * our alloc_initial_fb() function will only allocate framebuffer of this
+ * type.
+ *
+ * @param cmap [IN] Input color map
+ * @param info [IN] framebuffer to set
+ *
+ * @return 0 on success
+ */
+static int emgd_fbcon_setcmap(struct fb_cmap *cmap, struct fb_info *info)
+{
+	emgd_fbdev_t      *emgd_fbdev = info->par;
+	struct drm_device *dev        = emgd_fbdev->priv->ddev;
+	int                ret = 0;
+	u16               *red, *green, *blue, *transp;
+	u16                hred, hgreen, hblue, htransp;
+	u32                new_value, mask;
+	int                i, start_index;
+	struct drm_crtc   *crtc;
+
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Set all the CRTCs to the same color map */
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		red         = cmap->red;
+		green       = cmap->green;
+		blue        = cmap->blue;
+		transp      = cmap->transp;
+		start_index = cmap->start;
+
+		for(i = 0; i < cmap->len; i++) {
+			htransp = 0xffff;
+			hred    = *red++;
+			hgreen  = *green++;
+			hblue   = *blue++;
+
+			if (transp) {
+				htransp = *transp++;
+			}
+
+			/* The palette only has 17 entries */
+			if (16 < start_index ) {
+				ret = -IGD_ERROR_INVAL_PARAM;
+				break;
+			}
+
+			hred   >>= (16 - info->var.red.length);
+			hgreen >>= (16 - info->var.green.length);
+			hblue  >>= (16 - info->var.blue.length);
+
+			new_value = (hred   << info->var.red.offset)   |
+						(hgreen << info->var.green.offset) |
+						(hblue  << info->var.blue.offset);
+
+			if (info->var.transp.length > 0) {
+				mask = (1 << info->var.transp.length) - 1;
+				mask <<= info->var.transp.offset;
+				new_value |= mask;
+			}
+
+			((u32 *) info->pseudo_palette)[start_index] = new_value;
+
+			start_index++;
+		}
+
+		if (ret) {
+			EMGD_ERROR("Invalid parameter.");
+			break;
+		}
+
+		((struct drm_crtc_helper_funcs *)crtc->helper_private)->load_lut(crtc);
+	}	
+
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+
+
+/*
+ * Currently, we are using the device indpendent DRM functions
+ * for these.  If we need to do special processing, then uncomment
+ * these functions and hook them into the function table.
+ */
+static int emgd_fbcon_check_var(struct fb_var_screeninfo *var,
+		struct fb_info *info)
+{
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+
+
+static int emgd_fbcon_set_par(struct fb_info *info)
+{
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	// set mode here:  crtc->set_config
+    // look at drm_fb_helper_set_par for reference
+
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+
+
+static int emgd_fbcon_setcolreg(unsigned int regno,
+		unsigned int red, unsigned int green, unsigned int blue,
+		unsigned int transp, struct fb_info *info)
+{
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("STUBED emgd_fbcon_setcolreg");
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+
+static int emgd_fbcon_pan_display(struct fb_var_screeninfo *var,
+		struct fb_info *info)
+{
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+/*
+ * Currently, we are using the device indpendent DRM functions
+ * for these.  If we need to do special processing, then uncomment
+ * these functions and hook them into the function table.
+ */
+static int emgd_fbcon_blank(int blank, struct fb_info *info)
+{
+	int ret = 0;
+	struct drm_device *dev;
+	struct drm_crtc *crtc = NULL;
+	struct drm_encoder *encoder = NULL;
+	struct drm_encoder_helper_funcs *encoder_funcs;
+	emgd_fbdev_t *emgd_fbdev;
+
+	EMGD_TRACE_ENTER;
+
+	emgd_fbdev = info->par;
+	dev	       = emgd_fbdev->priv->ddev;
+
+	switch(blank) {
+	case FB_BLANK_UNBLANK:
+		EMGD_DEBUG("Turn on Display");
+
+		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+
+			((struct drm_crtc_helper_funcs *)crtc->helper_private)->dpms(crtc,
+				DRM_MODE_DPMS_ON);
+
+			list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+				if(encoder->crtc == crtc) {
+					encoder_funcs = encoder->helper_private;
+					encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
+				}
+			}
+		}
+		break;
+	case FB_BLANK_NORMAL:
+	case FB_BLANK_HSYNC_SUSPEND:
+	case FB_BLANK_VSYNC_SUSPEND:
+	case FB_BLANK_POWERDOWN:
+		EMGD_DEBUG("Turn off Display");
+
+		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+			list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+				if(encoder->crtc == crtc) {
+					encoder_funcs = encoder->helper_private;
+					encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
+				}
+			}
+
+			((struct drm_crtc_helper_funcs *)crtc->helper_private)->dpms(crtc,
+				DRM_MODE_DPMS_OFF);
+		}
+		break;
+	default:
+		EMGD_DEBUG("ERROR: Incorrect FB_BLANK value passed");
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return ret;
+}
+
+
+
+/*
+ * emgd_fbcon_initial_config
+ *
+ * Uses configurations in config_drm to set the start-up configuration.
+ * We will not support configurations that require more than one framebuffer
+ * at DRM boot time.  The fallback configuration is SINGLE.
+ *
+ * @param emgd_fbdev (IN) Framebuffer device to initialize
+ *
+ * @return 0 on success, an error code otherwise
+ */
+int emgd_fbcon_initial_config(emgd_fbdev_t *emgd_fbdev)
+{
+	int                    err;
+	unsigned long         *dc_assigned;
+	igd_context_t         *context;
+	unsigned short         port_number;
+	emgd_crtc_t           *emgd_crtc;
+	struct drm_crtc       *crtc         = NULL;
+	igd_display_context_t *primary      = NULL, *secondary = NULL;
+	bool                   mode_set_ret = FALSE;
+	struct drm_device     *dev          = emgd_fbdev->priv->ddev;
+
+	struct drm_display_mode primary_mode, secondary_mode;
+
+	EMGD_TRACE_ENTER;
+
+
+	context = ((drm_emgd_priv_t *)dev->dev_private)->context;
+
+
+	/* Basic error checking.  SINGLE is the fallback configuration */
+	if( IGD_DISPLAY_CONFIG_CLONE  != config_drm.dc &&
+		IGD_DISPLAY_CONFIG_SINGLE != config_drm.dc ) {
+		config_drm.dc = IGD_DISPLAY_CONFIG_SINGLE;
+	}
+
+
+	/*******************************************************
+	 * Allocate DSP for the desired DC.  This is to make sure the
+	 * display context is valid for subsequent operations.  The
+	 * dc_assigned coming out will contain port info in addition to
+	 * display configuration.
+	 ******************************************************/
+	/* Query for the best DC that matches the request */
+	err = context->dispatch.query_dc(context, config_drm.dc,
+								&dc_assigned, IGD_QUERY_DC_INIT);
+	if (err) {
+		EMGD_ERROR_EXIT("Cannot initialize the display as requested.\n"
+				"The query_dc() function returned %d.", err);
+		return -IGD_ERROR_INVAL;
+	}
+
+	emgd_fbdev->priv->dc = *dc_assigned;
+
+
+	/* Allocate and initialize igd_display_context */
+	err = context->mod_dispatch.dsp_alloc(context, *dc_assigned, 0);
+	if (err) {
+		EMGD_ERROR_EXIT("Cannot initialize display context.\n"
+			"The dsp_alloc() function returned %d.", err);
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Initialize pipe->owner for PRIMARY and SECONDARY */
+	port_number = IGD_DC_PRIMARY(*dc_assigned);
+
+	primary = context->mod_dispatch.dsp_display_list[port_number];
+	PIPE(primary)->owner = primary;
+
+	if (IGD_DC_SECONDARY(*dc_assigned)) {
+		port_number = IGD_DC_SECONDARY(*dc_assigned);
+		secondary = context->mod_dispatch.dsp_display_list[port_number];
+		PIPE(secondary)->owner = secondary;
+	}
+
+
+
+	/*******************************************************
+	 * Allocate framebuffer
+	 ******************************************************/
+	if (0 == (err = alloc_initial_fb(emgd_fbdev))) {
+
+		/* Attach the frame buffer to the CRTC(s)  */
+		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+			emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+			if (PIPE(primary) == emgd_crtc->igd_pipe) {
+				crtc->fb = &emgd_fbdev->emgd_fb->base;
+			}
+
+			/* Attach frame buffer to the secondary CRTC, if necessary */
+			if (IGD_DC_SECONDARY(*dc_assigned) &&
+				PIPE(secondary) == emgd_crtc->igd_pipe) {
+				crtc->fb = &emgd_fbdev->emgd_fb->base;
+			}
+		}
+
+
+		err = register_framebuffer(emgd_fbdev->priv->fbdev);
+
+		EMGD_DEBUG("fb%d: %s framebuffer device",
+			emgd_fbdev->priv->fbdev->node,
+			emgd_fbdev->priv->fbdev->fix.id);
+	}
+
+
+	/*******************************************************
+	 * Set Mode for Primary
+	 ******************************************************/
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+		emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+		if (PIPE(primary) == emgd_crtc->igd_pipe) {
+
+			memset(&primary_mode, 0, sizeof(primary_mode));
+			primary_mode.crtc_hdisplay = config_drm.width;
+			primary_mode.crtc_vdisplay = config_drm.height;
+			primary_mode.vrefresh      = config_drm.refresh;
+
+			mode_set_ret = drm_crtc_helper_set_mode(crtc,
+								&primary_mode,
+								0, 0,
+								NULL);
+			break;
+		}
+	}
+
+	if (FALSE == mode_set_ret) {
+		EMGD_ERROR_EXIT("Failed to set mode on primary CRTC.  Abort.");
+		return -IGD_ERROR_INVAL;
+	}
+
+
+	/*******************************************************
+	 * Set Mode for Secondary, if necessary
+	 ******************************************************/
+	if (IGD_DC_SECONDARY(*dc_assigned)) {
+
+		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
+			emgd_crtc = container_of(crtc, emgd_crtc_t, base);
+
+			if (PIPE(secondary) == emgd_crtc->igd_pipe) {
+
+				memset(&secondary_mode, 0, sizeof(secondary_mode));
+				secondary_mode.crtc_hdisplay = config_drm.width;
+				secondary_mode.crtc_vdisplay = config_drm.height;
+				secondary_mode.vrefresh      = config_drm.refresh;
+
+				mode_set_ret = drm_crtc_helper_set_mode(crtc,
+								&secondary_mode,
+								0, 0,
+								NULL);
+								break;
+			}
+		}
+
+	}
+
+	if (FALSE == mode_set_ret) {
+		EMGD_ERROR_EXIT("Failed to set mode on secondary CRTC.  Abort.");
+		return -IGD_ERROR_INVAL;
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_interface.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_interface.c
new file mode 100644
index 0000000..9a5a4bc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_interface.c
@@ -0,0 +1,2730 @@
+/*-----------------------------------------------------------------------------
+ * Filename: emgd_interface.c
+ * $Revision: 1.190 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file implements the kernel-space bridge between the IAL and the HAL.
+ *  Each of the igd_dispatch_t functions are called by an ioctl's handling
+ *  function, that's implemented in this file.  All parameters coming from user
+ *  space are copied and sent to the HAL.  All return parameters and the
+ *  overall return value is copied back to the user-space bridge code.
+ *  See the description in the file "emgd_hal2drm.c" for more details.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.oal
+
+#include "drmP.h"
+#include "drm.h"
+
+#include "drm_emgd_private.h"
+#include "emgd_drm.h"
+#include "emgd_drv.h"
+#include "memory.h"
+
+#include "module_init.h"
+#include "mode_dispatch.h"
+#include "ovl_dispatch.h"
+#include "ovl_virt.h"
+#include "msvdx.h"
+#include "topaz.h"
+
+#include "memmap.h"
+#include "sched.h"
+
+#include "services.h"
+#include "perproc.h"
+#include "pvr_bridge_km.h"
+#include "syscommon.h"
+#include "pvr_drm.h"
+
+#include <linkage.h>
+
+#include "pvrversion.h"
+
+/* Turn on tracing for this file only */
+/*
+#undef EMGD_TRACE_ENTER
+#define EMGD_TRACE_ENTER printk( KERN_ERR "%s Entry. ",__FUNCTION__)
+*/
+
+/* The compile-time configuration found in "user_config.c": */
+extern emgd_drm_config_t config_drm;
+/* Module parameters from "emgd_drv.c": */
+extern int drm_emgd_dc;
+extern unsigned x_started;
+
+extern void emgd_init_display(int merge_mod_params, drm_emgd_priv_t *priv);
+
+/**
+ * The driver handle for talking with the HAL, within the DRM/kernel code.
+ * This is a "real handle" as opposed to the "fake handle" in user-space.
+ * Notice that there's only one handle, as the secondary device shares this
+ * handle, and so it is cached here to simplify the ioctl-handling procedures.
+ */
+static igd_driver_h handle = NULL;
+/** This is the dispatch table for the HAL.  It is cached for quick access. */
+static igd_dispatch_t *dispatch = NULL;
+
+
+/*!
+ * This is called by emgd_driver_load() once it has a "real" driver handle, to
+ * allow the values to be cached in this file.
+ *
+ * @param drm_handle (IN) the kernel-space HAL's "real" driver handle
+ */
+void emgd_set_real_handle(igd_driver_h drm_handle)
+{
+	handle = drm_handle;
+} /* emgd_set_real_handle() */
+
+/*!
+ * This is called by emgd_driver_load() once it has a "real" dispatch table, to
+ * allow the values to be cached in this file.
+ *
+ * @param drm_dispatch (OUT) the kernel-space HAL's dispatch table
+ */
+void emgd_set_real_dispatch(igd_dispatch_t *drm_dispatch)
+{
+	dispatch = drm_dispatch;
+} /* emgd_set_real_dispatch() */
+
+
+/*
+ * Externally-accessible global variables and functions, for PreInit()-time
+ * configuration:
+ */
+extern mode_context_t mode_context[1];
+extern void dsp_shutdown(igd_context_t *context);
+extern int pi_pd_init(igd_display_port_t *port, unsigned long port_feature,
+	unsigned long second_port_feature, int drm_load_time);
+#ifdef DEBUG_BUILD_TYPE
+extern void emgd_print_params(igd_param_t *params);
+#endif
+extern void emgd_modeset_destroy(struct drm_device *dev);
+
+
+/*
+ * NOTE: The rest of this file contains implementations of the HAL-to-DRM
+ * ioctl-handling procedures.
+ *
+ * The naming convention is:  emgd_<HAL-procedure-pointer-name>()
+ */
+
+
+/*!
+ * IOCTL to get chipset information from DRM.
+ */
+int emgd_get_chipset_info(struct drm_device *dev, void *arg,
+		struct drm_file *file_priv)
+{
+	emgd_drm_driver_get_chipset_info_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+
+	drm_data->device_id = priv->init_info->device_id;
+	drm_data->revision_id = priv->init_info->vendor_id;
+
+	/*
+	 * Copy over the name and description fields.  Currently these
+	 * are hard coded to a maximum of 40 and 20 characters respectively.
+	 */
+	if (strlen(priv->init_info->name) > 39) {
+		strncpy(drm_data->name, priv->init_info->name, 39);
+		drm_data->name[39] = '\0';
+	} else {
+		strcpy(drm_data->name, priv->init_info->name);
+	}
+
+	if (strlen(priv->init_info->chipset) > 19) {
+		strncpy(drm_data->description, priv->init_info->chipset, 19);
+		drm_data->description[19] = '\0';
+	} else {
+		strcpy(drm_data->description, priv->init_info->chipset);
+	}
+
+	return 0;
+}
+
+
+int do_wait_vblank(void *display, int headline, int footline)
+{
+	int rtn;
+	int scnlne;
+	os_alarm_t timeout;
+	int ext = -99999;
+	unsigned long height = PIPE((igd_display_context_t *)display)->timing->height;
+
+	/*EMGD_TRACE_ENTER;*/
+
+	if (footline + 50 > height)
+		ext = IGD_IN_VBLANK;
+
+	timeout = OS_SET_ALARM(50);
+	do {
+		/* Call the HAL: */
+		rtn = dispatch->get_scanline((igd_display_context_t *)display, &scnlne);
+		if (rtn || (scnlne >= footline && scnlne <= footline + 50) || ext == scnlne) {
+			break;
+		}
+		OS_SCHEDULE();
+	} while (!OS_TEST_ALARM(timeout));
+
+	/*EMGD_DEBUG("rtn = %d", rtn);*/
+	/*EMGD_TRACE_EXIT;*/
+	return 0;
+}
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's wait_vblank() procedure.
+ */
+int emgd_wait_vblank(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	int rtn;
+	emgd_drm_driver_set_sync_refresh_t *refresh = arg;
+	igd_display_h *display_handle = &(refresh->display_handle);
+
+	/*EMGD_TRACE_ENTER;*/
+	rtn = do_wait_vblank((igd_display_context_t *)(*display_handle), refresh->start_line, refresh->bottom_line);
+
+	/*EMGD_DEBUG("rtn = %d", rtn);*/
+	/*EMGD_TRACE_EXIT;*/
+	return 0;
+} /* emgd_wait_vblank() */
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's alter_cursor() procedure.
+ */
+int emgd_alter_cursor(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_alter_cursor_t *drm_data = arg;
+
+	/*EMGD_TRACE_ENTER;*/
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->alter_cursor(drm_data->display_handle,
+		&(drm_data->cursor_info),
+		drm_data->image);
+
+
+	/*EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);*/
+	/*EMGD_TRACE_EXIT;*/
+	return 0;
+} /* emgd_alter_cursor() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's alter_cursor_pos() procedure.
+ */
+int emgd_alter_cursor_pos(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_alter_cursor_pos_t *drm_data = arg;
+
+	/*EMGD_TRACE_ENTER;*/
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->alter_cursor_pos(drm_data->display_handle,
+		&(drm_data->cursor_info));
+
+
+	/*EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);*/
+	/*EMGD_TRACE_EXIT;*/
+	return 0;
+} /* emgd_alter_cursor_pos() */
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's emgd_get_display_info() procedure.
+ */
+int emgd_get_display_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+  /*
+	emgd_drm_get_display_info_t *drm_data = arg;
+
+	igd_context_t *context = (igd_context_t *) handle;
+	drm_emgd_priv_t *priv = (drm_emgd_priv_t *)dev->dev_private;
+*/
+	EMGD_TRACE_ENTER;
+/*
+	EMGD_DEBUG("drm_data->primary = 0x%lx",  (unsigned long) drm_data->primary);
+	EMGD_DEBUG("drm_data->secondary = 0x%lx",(unsigned long) drm_data->secondary);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn); */
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_display_info() */
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's alter_displays() procedure.
+ */
+int emgd_alter_displays(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_alter_displays_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	int temp_dc = drm_data->dc;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * If we are in Vertical Extended mode and the caller's dc is also
+	 * Vertical Extended, then set it to Clone instead as our HAL doesn't
+	 * know anything about Vertical Extended mode.
+	 */
+	if(IGD_DC_VEXT(drm_data->dc)) {
+		temp_dc = (drm_data->dc & ~IGD_DISPLAY_CONFIG_MASK) |
+				IGD_DISPLAY_CONFIG_CLONE;
+		if (drm_data->primary_fb_info.height == 
+			drm_data->secondary_fb_info.height) {
+			if(drm_data->primary_pt_info.height ==
+					drm_data->primary_fb_info.height) {
+				drm_data->primary_fb_info.height *= 2;
+			}
+
+			if(drm_data->secondary_pt_info.height ==
+					drm_data->secondary_fb_info.height) {
+				drm_data->secondary_fb_info.height *= 2;
+			}
+		}
+	}
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->alter_displays(handle,
+		/* Note: Since a pointer is passed to drm_data->primary/secondary,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->primary),
+		&(drm_data->primary_pt_info),
+		&(drm_data->primary_fb_info),
+		&(drm_data->secondary),
+		&(drm_data->secondary_pt_info),
+		&(drm_data->secondary_fb_info),
+		temp_dc,
+		drm_data->flags);
+
+	if (!drm_data->rtn) {
+		/*
+		 * Special for Vertical Extended, pan the second display
+		 */
+		if(IGD_DC_VEXT(drm_emgd_dc) && IGD_DC_VEXT(drm_data->dc)) {
+			dispatch->pan_display(drm_data->secondary, 0,
+					drm_data->secondary_fb_info.height / 2);
+		}
+		/* Communicate the new info to the IMG 3rd-party display driver: */
+		priv->dc = drm_data->dc;
+		priv->primary = drm_data->primary;
+		priv->secondary = drm_data->secondary;
+		priv->primary_port_number = (drm_data->dc & 0xf0) >> 4;
+		priv->secondary_port_number = (drm_data->dc & 0xf00000) >> 20;
+
+		if (priv->must_power_on_ports) {
+			/* The HAL was re-initialized during emgd_driver_pre_init()
+			 * (i.e. because the X server was restarted).  At that time, it
+			 * turned off the port drivers' hardware (so that it could poke new
+			 * values into the port drivers).  It couldn't turn the hardware
+			 * back on then.  Do so now.
+			 */
+			igd_context_t *context = (igd_context_t *) handle;
+			igd_display_port_t *port = NULL;
+
+			while ((port = context->mod_dispatch.dsp_get_next_port(context,
+						port, 0)) != NULL) {
+				/* only LVDS was turned off, so turn it back on */
+				if (port->pd_driver &&  (port->port_number == IGD_PORT_TYPE_LVDS)) {
+					EMGD_DEBUG("Turning on power for port %lu\n", port->port_number);
+
+					dispatch->power_display(context, port->port_number,
+						IGD_POWERSTATE_D0);
+				}
+			}
+			priv->must_power_on_ports = 0;
+		}
+
+		if (priv->reinit_3dd) {
+			priv->reinit_3dd(dev);
+		}
+	}
+
+	EMGD_DEBUG("drm_data->primary = 0x%lx", (unsigned long) drm_data->primary);
+	EMGD_DEBUG("drm_data->secondary = 0x%lx",(unsigned long) drm_data->secondary);
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_alter_displays() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's alter_ovl() procedure.
+ */
+int emgd_alter_ovl(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_alter_ovl_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Deprecated version of alter_ovl. FIX ME.");
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->alter_ovl2(drm_data->display_handle,
+		(igd_surface_t *) &(drm_data->src_surf),
+		(igd_rect_t *) &(drm_data->src_rect),
+		(igd_rect_t *) &(drm_data->dst_rect),
+		(igd_ovl_info_t *) &(drm_data->ovl_info),
+		drm_data->flags);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_alter_ovl() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's alter_ovl() procedure.
+ */
+int emgd_alter_ovl2(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_alter_ovl2_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+	/* Dump the overlay parameters for debugging */
+	/*
+	printk (KERN_ERR "emgd_alter_ovl2 Entry."
+		"offset=0x%X "
+		"pitch=0x%X "
+		"width=0x%X "
+		"height=0x%X "
+		"pixel_format=0x%X "
+		"flags=0x%X "
+		"virt_addr=0x%X "
+		"pvr2d_mem_info=0x%X "
+		"pvr2d_context_h=0x%X "
+		"hPVR2DFlipChain=0x%X "
+		"src_x1=0x%X "
+		"src_x2=0x%X "
+		"src_y1=0x%X "
+		"src_y2=0x%X "
+		"dest_x1=0x%X "
+		"dest_x2=0x%X "
+		"dest_y1=0x%X "
+		"dest_y2=0x%X "
+		"color_key.src_lo=0x%X "
+		"color_key.src_hi=0x%X "
+		"color_key.dest=0x%X "
+		"color_key.flags=0x%X "
+		"flags=0x%X "
+		,
+		(unsigned int)drm_data->src_surf.offset ,
+		(unsigned int)drm_data->src_surf.pitch ,
+		(unsigned int)drm_data->src_surf.width ,
+		(unsigned int)drm_data->src_surf.height ,
+		(unsigned int)drm_data->src_surf.pixel_format ,
+		(unsigned int)drm_data->src_surf.flags ,
+		(unsigned int)drm_data->src_surf.virt_addr ,
+		(unsigned int)drm_data->src_surf.pvr2d_mem_info ,
+		(unsigned int)drm_data->src_surf.pvr2d_context_h ,
+		(unsigned int)drm_data->src_surf.hPVR2DFlipChain ,
+		(unsigned int)drm_data->src_rect.x1,
+		(unsigned int)drm_data->src_rect.x2,
+		(unsigned int)drm_data->src_rect.y1,
+		(unsigned int)drm_data->src_rect.y2,
+		(unsigned int)drm_data->dst_rect.x1,
+		(unsigned int)drm_data->dst_rect.x2,
+		(unsigned int)drm_data->dst_rect.y1,
+		(unsigned int)drm_data->dst_rect.y2,
+		(unsigned int)drm_data->ovl_info.color_key.src_lo,
+		(unsigned int)drm_data->ovl_info.color_key.src_hi,
+		(unsigned int)drm_data->ovl_info.color_key.dest,
+		(unsigned int)drm_data->ovl_info.color_key.flags,
+		(unsigned int)drm_data->flags
+		);
+	*/
+	switch (drm_data->cmd) {
+	case CMD_ALTER_OVL2:
+		drm_data->rtn = dispatch->alter_ovl2(drm_data->display_handle,
+											 &(drm_data->src_surf),
+											 &(drm_data->src_rect),
+											 &(drm_data->dst_rect),
+					                         &(drm_data->ovl_info),
+						                     drm_data->flags);
+		break;
+	case CMD_ALTER_OVL2_OSD:
+		drm_data->rtn = dispatch->alter_ovl2_osd(drm_data->display_handle,
+                                             &(drm_data->src_surf),
+                                             &(drm_data->src_rect),
+                                             &(drm_data->dst_rect),
+                                             &(drm_data->ovl_info),
+                                             drm_data->flags);
+   		break;
+	default:
+		break;
+	}
+ 
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+
+	return 0;
+} /* emgd_alter_ovl2() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_ovl_init_params() procedure.
+ */
+int emgd_get_ovl_init_params(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_ovl_init_params_t *drm_data = arg;
+	ovl_um_context_t *ovl_um_context;
+
+	EMGD_TRACE_ENTER;
+
+	/* Call the HAL: */
+	/* there may not be a need to call the hal for this, since we have
+	 * access to the overlay context.  We can probably just copy what we need
+	 * from the context to the drm_data
+	 */
+
+	if (!drm_data) {
+		EMGD_DEBUG("Invalid drm_data in emgd_get_ovl_init_params.\n");
+		return 0;
+	}
+
+	ovl_um_context = drm_data->ovl_um_context;
+
+	if (!ovl_um_context) {
+		drm_data->rtn = -1;
+		return 0;
+	}
+
+	drm_data->rtn = dispatch->get_ovl_init_params(handle, ovl_um_context);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_ovl_init_params() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's appcontext_alloc() procedure.
+ */
+int emgd_appcontext_alloc(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_appcontext_alloc_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	/* NOTE: the return value is different than normal: */
+	drm_data->appcontext_h =
+		dispatch->appcontext_alloc(drm_data->display_handle,
+			drm_data->priority,
+			drm_data->flags);
+
+
+	EMGD_DEBUG("drm_data->appcontext_h = %p", drm_data->appcontext_h);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_appcontext_alloc() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's appcontext_free() procedure.
+ */
+int emgd_appcontext_free(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_appcontext_free_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	/* NOTE: no return value is desired: */
+	dispatch->appcontext_free(drm_data->display_handle,
+		drm_data->priority,
+		drm_data->appcontext_h);
+
+
+	EMGD_DEBUG("This function has no drm_data->rtn value");
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_appcontext_free() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's driver_save_restore() procedure.
+ */
+int emgd_driver_save_restore(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	drm_emgd_priv_t *priv = dev->dev_private;
+	emgd_drm_driver_save_restore_t *drm_data = arg;
+	unsigned long save_flags  = 0;
+
+	EMGD_TRACE_ENTER;
+
+	if (config_drm.init) {
+		save_flags = IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT;
+	}
+	else {
+		save_flags = IGD_REG_SAVE_ALL;
+	}
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->driver_save_restore(handle, save_flags);
+
+	/* Change the state of what's saved: */
+	if (priv->saved_registers == CONSOLE_STATE_SAVED) {
+		priv->saved_registers = X_SERVER_STATE_SAVED;
+		EMGD_DEBUG("State of saved registers is X_SERVER_STATE_SAVED");
+	} else {
+		priv->saved_registers = CONSOLE_STATE_SAVED;
+		EMGD_DEBUG("State of saved registers is CONSOLE_STATE_SAVED");
+	}
+
+	if (priv->saved_registers == X_SERVER_STATE_SAVED &&
+		!priv->qb_seamless && !config_drm.kms && config_drm.init) {
+		emgd_init_display(FALSE, priv);
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_driver_save_restore() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's enable_port() procedure.
+ */
+int emgd_enable_port(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_enable_port_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->enable_port(drm_data->display_handle,
+		drm_data->port_number,
+		drm_data->flag,
+		drm_data->test);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_enable_port() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_attrs() procedure.
+ */
+int emgd_get_attrs(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_attrs_t *drm_data = arg;
+	igd_attr_t *attr_list = NULL;
+	igd_attr_t *ext_list = NULL;
+	igd_extension_attr_t *tmp = NULL;
+	int i = 0;
+	int extended = 0;
+	int core_cnt = 0;
+	int ext_cnt = 0;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->get_attrs(handle,
+		drm_data->port_number,
+		&(drm_data->list_size),
+		/* Note: get_attrs() returns a pointer to live data, that the caller
+		 * (i.e. this procedure) is not supposed to free/alter.  Therefore,
+		 * this data must be copied into the space pointed to by
+		 * drm_data->attr_list (assuming enough memory was allocated for it--if
+		 * not, the hal2drm code will allocate drm_data->list_size and call
+		 * this ioctl again).
+		 */
+		&(attr_list));
+
+	/*
+	 * The attribute list may have an extension list in addition to
+	 * the core attributes.  The caller may want the core list or
+	 * the extenstion list. Use the extended flag in drm_data to
+	 * determine this.  If the caller is looking for the count, then
+	 * return either core or extended size.
+	 */
+
+	while ((i < drm_data->list_size) && (attr_list[i].id != PD_ATTR_LIST_END)){
+		if (attr_list[i].id == PD_ATTR_ID_EXTENSION) {
+			extended = 1;
+			tmp = (igd_extension_attr_t *)&attr_list[i];
+			ext_list = tmp->extension;
+
+			/* Count extension attributes */
+			while ((ext_cnt < drm_data->list_size) &&
+					(ext_list[ext_cnt].id != PD_ATTR_LIST_END)){
+				ext_cnt++;
+			}
+		}
+		core_cnt++;
+		i++;
+	}
+
+	if(drm_data->port_number == IGD_PORT_TYPE_LVDS) {
+		/* LVDS port driver returns +1 is to include the end attribute */
+		ext_cnt++;
+	}
+	if (drm_data->extended) {
+		drm_data->list_size = ext_cnt; /* size of extension list */
+
+		/* Copy the attribute array back to user-space, if enough room: */
+		if ((drm_data->allocated_size > 0) &&
+			(drm_data->list_size <= drm_data->allocated_size)) {
+			OS_MEMCPY(drm_data->attr_list, ext_list,
+				(drm_data->list_size * sizeof(igd_attr_t)));
+		}
+	} else {
+		drm_data->list_size = core_cnt;
+
+		/* Copy the attribute array back to user-space, if enough room: */
+		if ((drm_data->allocated_size > 0) &&
+			(drm_data->list_size <= drm_data->allocated_size)) {
+			OS_MEMCPY(drm_data->attr_list, attr_list,
+				(drm_data->list_size * sizeof(igd_attr_t)));
+		}
+	}
+
+
+	drm_data->extended = extended;
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_attrs() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_display() procedure.
+ */
+int emgd_get_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	igd_context_t *context = (igd_context_t *) handle;
+	emgd_drm_get_display_t *drm_data = arg;
+	igd_display_context_t *display =
+		(igd_display_context_t *) drm_data->display_handle;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	int dc, port_num;
+	int do_reinit_3dd = 1;
+
+	EMGD_TRACE_ENTER;
+
+	/* Can we overload this to get the display handle if it's null? */
+	if (display == NULL) {
+		display = context->mod_dispatch.dsp_display_list[drm_data->port_number];
+		drm_data->display_handle = display;
+	}
+
+	/* To prevent a kernel OOPS, ensure the following value is non-NULL: */
+	if ((display == NULL) || (PORT(display, drm_data->port_number) == NULL)) {
+		EMGD_ERROR_EXIT("emgd_get_display() given invalid display "
+			"handle (0x%x) or port number (%d)", (unsigned int) display,
+			drm_data->port_number);
+		return -IGD_ERROR_INVAL;
+	}
+
+	dc = *(context->mod_dispatch.dsp_current_dc);
+	port_num = drm_data->port_number;
+
+	if (drm_data->flags & IGD_GET_DISPLAY_NO_3DD_REINIT){
+		do_reinit_3dd = 0;
+		drm_data->flags &= (~IGD_GET_DISPLAY_NO_3DD_REINIT);
+	}
+	
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->get_display(drm_data->display_handle,
+		drm_data->port_number,
+		/* Note: Since a pointer is passed to drm_data->fb_info/pt_info,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->fb_info),
+		&(drm_data->pt_info),
+		drm_data->flags);
+
+	/* In seamless mode this gets called instead of alter_displays */
+	/* The reason why we do not want to call reinit_3dd() if the clone mode is
+	 * set and if this function was called to get the secondary handle is because
+	 * reinit_3dd() calls init_display() which in turn calls get_display() and passes
+	 * the handle for secondary, and primary port number hence leading to an incorrect
+	 * combination. Also, we may not have to call reinit_3dd() as this is already done
+	 * as a part of configuring the primary.
+	 */
+	if (do_reinit_3dd) {
+		if(mode_context->seamless && !(IGD_DC_CLONE(dc) && port_num == priv->secondary_port_number)) {
+			if (priv->reinit_3dd) {
+				if(IGD_DC_VEXT(drm_emgd_dc)) {
+					priv->dc = drm_emgd_dc;
+				} else {
+					priv->dc = *(context->mod_dispatch.dsp_current_dc);
+				}
+				EMGD_DEBUG("priv->dc = 0x%lX", priv->dc);
+				priv->primary = drm_data->display_handle;
+				priv->secondary = NULL;
+				priv->primary_port_number = (priv->dc & 0xf0) >> 4;
+				priv->secondary_port_number = (priv->dc & 0xf00000) >> 20;
+				priv->reinit_3dd(dev);
+			}
+		}
+	}
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_display() */
+
+
+/*!
+ * IOCTL to support emgd_hal2drm_drm_config(), which returns to the X driver
+ * the igd_param_t that was used at load time, as well as the "config ID" that
+ * was either specified by the "configid" module parameter of stored in the
+ * VBOIS (by calling igd_get_param(...,IGD_PARAM_PANEL_ID,...)).
+ */
+int emgd_get_drm_config(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_drm_config_t *drm_data = arg;
+	igd_param_t *params;
+	int i;
+
+
+	EMGD_TRACE_ENTER;
+
+
+	/*
+	 * Return the igd_param_t parameter values used at module load time:
+	 */
+	if (drm_emgd_configid < 0) {
+		params = config_drm.hal_params[0];
+	} else {
+		params = config_drm.hal_params[drm_emgd_configid-1];
+	}
+	if (config_drm.init) {
+		drm_data->display_config = drm_emgd_dc;
+	} else {
+		drm_data->display_config = config_drm.dc;
+	}
+	drm_data->params.page_request = params->page_request;
+	drm_data->params.max_fb_size = params->max_fb_size;
+	drm_data->params.preserve_regs = params->preserve_regs;
+	drm_data->params.display_flags = params->display_flags;
+	drm_data->params.display_color = params->display_color;
+	drm_data->params.quickboot = params->quickboot;
+	drm_data->params.qb_seamless = params->qb_seamless;
+	drm_data->params.qb_video_input = params->qb_video_input;
+	drm_data->params.qb_splash = params->qb_splash;
+	drm_data->params.polling = params->polling;
+	drm_data->params.ref_freq = params->ref_freq;
+	drm_data->params.tuning_wa = params->tuning_wa;
+	drm_data->params.clip_hw_fix = params->clip_hw_fix;
+	drm_data->params.async_flip_wa = params->async_flip_wa;
+	drm_data->params.en_reg_override = params->en_reg_override;
+	drm_data->params.disp_arb = params->disp_arb;
+	drm_data->params.fifo_watermark1 = params->fifo_watermark1;
+	drm_data->params.fifo_watermark2 = params->fifo_watermark2;
+	drm_data->params.fifo_watermark3 = params->fifo_watermark3;
+	drm_data->params.fifo_watermark4 = params->fifo_watermark4;
+	drm_data->params.fifo_watermark5 = params->fifo_watermark5;
+	drm_data->params.fifo_watermark6 = params->fifo_watermark6;
+	drm_data->params.gvd_hp_control = params->gvd_hp_control;
+	drm_data->params.bunit_chicken_bits = params->bunit_chicken_bits;
+	drm_data->params.bunit_write_flush = params->bunit_write_flush;
+	drm_data->params.disp_chicken_bits = params->disp_chicken_bits;
+	drm_data->params.punt_to_3dblit = params->punt_to_3dblit;
+
+	for (i = 0 ; i < IGD_MAX_PORTS ; i++) {
+		drm_data->params.port_order[i] = params->port_order[i];
+		drm_data->params.display_params[i].port_number =
+			params->display_params[i].port_number;
+		drm_data->params.display_params[i].present_params =
+			(params->display_params[i].present_params &
+				~(IGD_PARAM_DTD_LIST | IGD_PARAM_ATTR_LIST));
+		drm_data->params.display_params[i].flags =
+			params->display_params[i].flags;
+		drm_data->params.display_params[i].edid_avail =
+			params->display_params[i].edid_avail;
+		drm_data->params.display_params[i].edid_not_avail =
+			params->display_params[i].edid_not_avail;
+		drm_data->params.display_params[i].ddc_gpio =
+			params->display_params[i].ddc_gpio;
+		drm_data->params.display_params[i].ddc_speed =
+			params->display_params[i].ddc_speed;
+		drm_data->params.display_params[i].ddc_dab =
+			params->display_params[i].ddc_dab;
+		drm_data->params.display_params[i].i2c_gpio =
+			params->display_params[i].i2c_gpio;
+		drm_data->params.display_params[i].i2c_speed =
+			params->display_params[i].i2c_speed;
+		drm_data->params.display_params[i].i2c_dab =
+			params->display_params[i].i2c_dab;
+		drm_data->params.display_params[i].fp_info =
+			params->display_params[i].fp_info;
+		/* No DTDs nor attrs are sent up.  If "xorg.conf" lists none, the
+		 * defaults (in "user_config.c") are still in force.  If anything
+		 * is listed in "xorg.conf", it overrides the defaults.  Thus,
+		 * there's no need to send them up.
+		 */
+	}
+
+	/*
+	 * Return the "ConfigID" to return (either the module parameter or the
+	 * value stored in VBIOS:
+	 */
+	if (drm_emgd_configid > 0) {
+		drm_data->config_id = drm_emgd_configid;
+	} else {
+		/* Call the HAL: */
+		int err = igd_get_param(handle, IGD_PARAM_PANEL_ID,
+			/* Note: Since a pointer is passed to drm_data->value,
+			 * there's no need to copy anything back into drm_data, except
+			 * for the return value.
+			 */
+			&drm_data->config_id);
+		if (err) {
+			/* Either the vBIOS doesn't exist, or there was a problem reading
+			 * it.  Either way, don't error; just set the config_id to an
+			 * invalid value:
+			 */
+			drm_data->config_id = -1;
+		}
+	}
+
+	/* Fill in DDK & EMGD version numbers and debug status */
+	drm_data->build_config.ddk_version = PVRVERSION_BUILD;
+	drm_data->build_config.emgd_version = IGD_BUILD_NUM;
+#ifdef DEBUG
+	drm_data->build_config.debug = 1;
+#else
+	drm_data->build_config.debug = 0;
+#endif
+
+	drm_data->rtn = 0;
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_drm_config() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_EDID_block() procedure.
+ */
+int emgd_get_EDID_block(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_EDID_block_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->get_EDID_block(handle,
+		drm_data->port_number,
+		/* Note: Since a pointer is passed to drm_data->edid_block,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		drm_data->edid_block,
+		drm_data->block_number);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_EDID_block() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_EDID_info() procedure.
+ */
+int emgd_get_EDID_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_EDID_info_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->get_EDID_info(handle,
+		drm_data->port_number,
+		/* Note: Since a pointer is passed to drm_data->edid_*,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->edid_version),
+		&(drm_data->edid_revision),
+		&(drm_data->edid_size));
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_EDID_info() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_pixelformats() procedure.
+ */
+int emgd_get_pixelformats(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_pixelformats_t *drm_data = arg;
+	unsigned long *format_list = NULL;
+	unsigned long *fb_list_pfs = NULL;
+	unsigned long *cu_list_pfs = NULL;
+	unsigned long *overlay_pfs = NULL;
+	unsigned long *render_pfs = NULL;
+	unsigned long *texture_pfs = NULL;
+	unsigned long *pf;
+	int count = 1;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* The HAL's get_pixelformats() procedure determines which formats are
+	 * wanted based on whether a pointer is NULL or not, and so we need to make
+	 * the desired-format's pointer non-NULL (setting it to 1 works, though it
+	 * is kinda cheating:-):
+	 */
+	if (drm_data->format == PIXEL_FORMAT_FRAMEBUFFER) {
+		fb_list_pfs = (unsigned long *) 1;
+	} else if (drm_data->format == PIXEL_FORMAT_CURSOR) {
+		cu_list_pfs = (unsigned long *) 1;
+	} else if (drm_data->format == PIXEL_FORMAT_OVERLAY) {
+		overlay_pfs = (unsigned long *) 1;
+	} else if (drm_data->format == PIXEL_FORMAT_RENDER) {
+		render_pfs = (unsigned long *) 1;
+	} else if (drm_data->format == PIXEL_FORMAT_TEXTURE) {
+		texture_pfs = (unsigned long *) 1;
+	}
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->get_pixelformats(drm_data->display_handle,
+		&fb_list_pfs, &cu_list_pfs, &overlay_pfs, &render_pfs, &texture_pfs);
+
+
+	/* Next, point format_list at the correct list: */
+	if (drm_data->format == PIXEL_FORMAT_FRAMEBUFFER) {
+		format_list = fb_list_pfs;
+	} else if (drm_data->format == PIXEL_FORMAT_CURSOR) {
+		format_list = cu_list_pfs;
+	} else if (drm_data->format == PIXEL_FORMAT_OVERLAY) {
+		format_list = overlay_pfs;
+	} else if (drm_data->format == PIXEL_FORMAT_RENDER) {
+		format_list = render_pfs;
+	} else if (drm_data->format == PIXEL_FORMAT_TEXTURE) {
+		format_list = texture_pfs;
+	}
+
+
+	/* Count how many pixelformats were returned: */
+	pf = format_list;
+	while (*pf) {
+		pf++;
+		count++;
+	}
+	drm_data->list_size = count;
+
+
+	/* Copy the pixel format list/array back to user-space, if enough room.  If
+	 * there isn't enough memory allocated, the hal2drm code will allocate
+	 * drm_data->list_size and call this ioctl again).
+	 */
+	if ((drm_data->allocated_size > 0) && (count <= drm_data->allocated_size)) {
+		OS_MEMCPY(drm_data->format_list, format_list,
+			(count * sizeof(unsigned long)));
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_pixelformats() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_port_info() procedure.
+ */
+int emgd_get_port_info(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_port_info_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->get_port_info(handle,
+		drm_data->port_number,
+		/* Note: Since a pointer is passed to drm_data->port_info,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->port_info));
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_get_port_info() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's gmm_alloc_region() procedure.
+ */
+int emgd_gmm_alloc_region(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_gmm_alloc_region_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->gmm_alloc_region(
+		/* Note: Since a pointer is passed to drm_data->offset/size,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->offset),
+		&(drm_data->size),
+		drm_data->type,
+		drm_data->flags);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_gmm_alloc_region() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's gmm_alloc_surface() procedure.
+ */
+int emgd_gmm_alloc_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_gmm_alloc_surface_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->gmm_alloc_surface(
+		/* Note: Since a pointer is passed to drm_data->*,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->offset),
+		drm_data->pixel_format,
+		&(drm_data->width),
+		&(drm_data->height),
+		&(drm_data->pitch),
+		&(drm_data->size),
+		drm_data->type,
+		&(drm_data->flags));
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_gmm_alloc_surface() */
+
+int emgd_gmm_get_num_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_gmm_get_num_surface_t *drm_data = arg;
+	EMGD_TRACE_ENTER;
+
+	drm_data->rtn = dispatch->gmm_get_num_surface(
+		&(drm_data->count));
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int emgd_gmm_get_surface_list(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_gmm_get_surface_list_t *drm_data = arg;
+	igd_surface_list_t *surface_list = NULL;
+	EMGD_TRACE_ENTER;
+
+	drm_data->rtn = dispatch->gmm_get_surface_list(
+		drm_data->allocated_size,
+		&(drm_data->list_size),
+		&(surface_list));
+
+	/* Copy the surface array back to user-space, if enough room */
+	if ((drm_data->allocated_size > 0) &&
+		(drm_data->list_size <= drm_data->allocated_size)) {
+		OS_MEMCPY(*(drm_data->surface_list), surface_list,
+			(drm_data->list_size * sizeof(igd_surface_list_t)));
+	}
+
+	vfree(surface_list);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's gmm_free() procedure.
+ */
+int emgd_gmm_free(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_gmm_free_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	/* NOTE: no return value is desired: */
+	dispatch->gmm_free(drm_data->offset);
+
+
+	EMGD_DEBUG("This function has no drm_data->rtn value");
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_gmm_free() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's gmm_flush_cache() procedure.
+ */
+int emgd_gmm_flush_cache(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_gmm_flush_cache_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->gmm_flush_cache();
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_gmm_flush_cache() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's pan_display() procedure.
+ */
+int emgd_pan_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_pan_display_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	/* NOTE: the return value is different than normal: */
+	drm_data->rtn = dispatch->pan_display(drm_data->display_handle,
+		drm_data->x_offset,
+		drm_data->y_offset);
+
+
+	EMGD_DEBUG("drm_data->rtn = %ld", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_pan_display() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's power_display() procedure.
+ */
+int emgd_power_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_power_display_t *drm_data = arg;
+	igd_context_t *context = (igd_context_t *) handle;
+	igd_display_context_t *display;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* To prevent a kernel OOPS, ensure the following value is non-NULL: */
+	display = context->mod_dispatch.dsp_display_list[drm_data->port_number];
+	if ((display == NULL) || (PORT(display, drm_data->port_number) == NULL)) {
+		EMGD_ERROR_EXIT("emgd_power_display() given an invalid port "
+			"number (%d)\n", drm_data->port_number);
+		return -IGD_ERROR_INVAL;
+	}
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->power_display(handle,
+		drm_data->port_number,
+		drm_data->power_state);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_power_display() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's pwr_alter() procedure.
+ */
+int emgd_pwr_alter(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_pwr_alter_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->pwr_alter(handle, drm_data->power_state);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_pwr_alter() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's query_dc() procedure.
+ */
+int emgd_query_dc(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_query_dc_t *drm_data = arg;
+	unsigned long *dc_list = NULL;
+	unsigned long *dc;
+	int count = 1;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Next, We don't need to copy the DC list from user-space, since
+	 * the HAL is going to point us to its internal list.
+	 */
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->query_dc(handle,
+		drm_data->request,
+		/* Note: query_dc() returns a pointer to its live internal list, that
+		 * the caller (i.e. this procedure) is not supposed to free/alter.
+		 * Therefore, this data must be copied into the space pointed to by
+		 * drm_data->dc_list (assuming enough memory was allocated for it--if
+		 * not, the hal2drm code will allocate drm_data->list_size and call
+		 * this ioctl again).
+		 */
+		&dc_list,
+		drm_data->flags);
+
+
+	/* Count how many DCs were returned: */
+	if(dc_list == NULL){
+		EMGD_ERROR("query_dc() returned NULL !");
+		return 1;
+	}
+	dc = dc_list;
+	while (*dc) {
+		dc++;
+		count++;
+	}
+
+	drm_data->list_size = count;
+	EMGD_DEBUG("drm_data->list_size = %d", drm_data->list_size);
+
+
+	/* Copy the DC list back to user-space, if enough room: */
+	if ((drm_data->allocated_size > 0) && (count <= drm_data->allocated_size)) {
+		EMGD_DEBUG("Copying list");
+		OS_MEMCPY(drm_data->dc_list, dc_list, (count * sizeof(unsigned long)));
+		EMGD_DEBUG("Copied list");
+	}
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_query_dc() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's query_max_size_ovl() procedure.
+ */
+int emgd_query_max_size_ovl(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_query_max_size_ovl_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->query_max_size_ovl(drm_data->display_handle,
+		drm_data->pf,
+		/* Note: Since a pointer is passed to drm_data->*,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->max_width),
+		&(drm_data->max_height));
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_query_max_size_ovl() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's query_ovl() procedure.
+ */
+int emgd_query_ovl(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_query_ovl_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->query_ovl(drm_data->display_handle,
+		drm_data->flags);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_query_ovl() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's query_mode_list() procedure.
+ */
+int emgd_query_mode_list(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_query_mode_list_t *drm_data = arg;
+	igd_display_info_t *mode_list = NULL;
+	igd_display_info_t *mode;
+	int count = 1;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->query_mode_list(handle,
+		drm_data->dc,
+		/* Note: query_mode_list() **MAY** return a pointer to live data, that
+		 * the caller (i.e. this procedure) is not supposed to free/alter.
+		 * Therefore, this data is copied into the space pointed to by
+		 * drm_data->mode_list (assuming enough memory was allocated for it--if
+		 * not, the hal2drm code will allocate drm_data->list_size and call
+		 * this ioctl again).
+		 */
+		&mode_list,
+		drm_data->flags);
+
+
+	/* Count how many modes were returned: */
+	mode = mode_list;
+	while (mode && (mode->width != IGD_TIMING_TABLE_END)) {
+		mode++;
+		count++;
+	}
+	drm_data->list_size = count;
+	EMGD_DEBUG("drm_data->list_size = %d", drm_data->list_size);
+
+
+	/* Next, copy the mode list back to user-space: */
+	if ((drm_data->allocated_size > 0) && (count <= drm_data->allocated_size)) {
+		EMGD_DEBUG("Copying list");
+		OS_MEMCPY(drm_data->mode_list, mode_list,
+			(count * sizeof(igd_display_info_t)));
+		EMGD_DEBUG("Copied list");
+	}
+
+	/* Finally, if a non-live list was requested, free the kernel memory: */
+	if (!(drm_data->flags & IGD_QUERY_LIVE_MODES)) {
+		dispatch->free_mode_list(mode_list);
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_query_mode_list() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's set_attrs() procedure.
+ */
+int emgd_set_attrs(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_set_attrs_t *drm_data = arg;
+	igd_attr_t *attr_list = drm_data->attr_list;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->set_attrs(handle,
+		drm_data->port_number,
+		/* Note: Since a pointer is passed to drm_data->num_attrs and
+		 * drm_data->attr_list, there's no need to copy anything back into
+		 * drm_data, except for the return value.
+		 */
+		drm_data->list_size,
+		attr_list);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_set_attrs() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's set_palette_entry() procedure.
+ */
+int emgd_set_palette_entry(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_set_palette_entry_t *drm_data = arg;
+
+	/*EMGD_TRACE_ENTER;*/
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->set_palette_entry(drm_data->display_handle,
+		drm_data->palette_entry,
+		drm_data->palette_color);
+
+
+	/*EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);*/
+	/*EMGD_TRACE_EXIT;*/
+	return 0;
+} /* emgd_set_palette_entry() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's set_surface() procedure.
+ */
+int emgd_set_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_set_surface_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->set_surface(drm_data->display_handle,
+		drm_data->priority,
+		drm_data->type,
+		&(drm_data->surface),
+		drm_data->appcontext,
+		drm_data->flags);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_set_surface() */
+
+
+
+/*!
+ * IOCTL to set surface to dih_clone or back to dih.
+ * drm_data->mode = CLONE - sets fake clone clone
+ * drm_data->mode = DIH - reverts back to DIH
+ * drm_data->dih_clone_display = CLONE_PRIMARY, primary cloned
+ * drm_data->dih_clone_display = CLONE_SECONDARY, secondary cloned
+ *
+ */
+int emgd_dihclone_set_surface(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_dihclone_set_surface_t *drm_data = arg;
+	igd_context_t *context = (igd_context_t *) handle;
+	igd_plane_t *display_plane1, *display_plane2;
+	igd_display_pipe_t *pipe1, *pipe2;
+	igd_display_context_t *display;
+	igd_surface_t surf;
+	unsigned long dc ;
+	unsigned long x_offset, y_offset;
+
+	EMGD_TRACE_ENTER;
+
+	memset(&surf, 0, sizeof(igd_surface_t));
+	dc = *(context->mod_dispatch.dsp_current_dc);
+
+	context->mod_dispatch.dsp_get_planes_pipes(
+			&display_plane1, &display_plane2,
+			&pipe1, &pipe2);
+
+	/* check if the resolutions match */
+	if((pipe1->plane->fb_info->width != pipe2->plane->fb_info->width) ||
+		(pipe1->plane->fb_info->height != pipe2->plane->fb_info->height)){
+			EMGD_ERROR(" emgd_dihclone_set_surface: resolutions don't match");
+		return -IGD_ERROR_INVAL;
+	}
+
+
+	if( drm_data->dih_clone_display != CLONE_PRIMARY && drm_data->dih_clone_display != CLONE_SECONDARY){
+
+		EMGD_ERROR(" emgd_dihclone_set_surface: Invalid Clone Display number");
+		return -IGD_ERROR_INVAL;
+	}
+
+	surf.pitch = pipe1->plane->fb_info->screen_pitch;
+	surf.width =  pipe1->plane->fb_info->width;
+	surf.height =  pipe1->plane->fb_info->height;
+	surf.pixel_format =  pipe1->plane->fb_info->pixel_format;
+	surf.flags = IGD_SURFACE_RENDER | IGD_SURFACE_DISPLAY;
+
+	display = context->mod_dispatch.dsp_display_list[IGD_DC_PRIMARY(dc)];
+
+	/*reverting back to DIH from fake clone */
+	if( drm_data->mode == DIH){
+		if (IGD_DC_VEXT(drm_emgd_dc)) {
+			/* Clone to VEXT */
+			x_offset = 0;
+
+			y_offset = 0;
+			display = context->mod_dispatch.dsp_display_list[IGD_DC_PRIMARY(dc)];
+			/* Call pan display to revert Primary to VEXT */
+			drm_data->rtn = dispatch->pan_display(display, x_offset, y_offset);
+
+			y_offset = (pipe1->plane->fb_info->height / 2);
+			display = context->mod_dispatch.dsp_display_list[IGD_DC_SECONDARY(dc)];
+			/* Call pan_display to revert Secondary to VEXT */
+			drm_data->rtn = dispatch->pan_display(display, x_offset, y_offset);
+		}else {/* in DIH mode */
+
+			if(context->mod_dispatch.dih_clone_display == CLONE_PRIMARY) {
+
+				EMGD_DEBUG(" emgd_dihclone_set_surface: setting DIH1");
+				surf.offset = pipe1->plane->fb_info->fb_base_offset;
+				/* Call the HAL: */
+				drm_data->rtn = dispatch->set_surface(display,
+						IGD_PRIORITY_NORMAL,
+						IGD_BUFFER_DISPLAY,
+						&surf,
+						NULL,
+						0);
+
+				if(drm_data->rtn) {
+					EMGD_ERROR(" emgd_dihclone_set_surface1: failed");
+					return -IGD_ERROR_INVAL;
+				}
+				display = context->mod_dispatch.dsp_display_list[IGD_DC_SECONDARY(dc)];
+				surf.offset = pipe2->plane->fb_info->saved_offset;
+				/* Call the HAL: */
+				drm_data->rtn = dispatch->set_surface(display,
+						IGD_PRIORITY_NORMAL,
+						IGD_BUFFER_DISPLAY,
+						&surf,
+						NULL,
+						0);
+				if(drm_data->rtn) {
+					EMGD_ERROR(" emgd_dihclone_set_surface2: failed");
+					return -IGD_ERROR_INVAL;
+				}
+
+
+			} else { // if secondary clone
+
+				EMGD_DEBUG(" emgd_dihclone_set_surface: setting DIH2");
+				surf.offset = pipe1->plane->fb_info->saved_offset;
+				/* Call the HAL: */
+				drm_data->rtn = dispatch->set_surface(display,
+						IGD_PRIORITY_NORMAL,
+						IGD_BUFFER_DISPLAY,
+						&surf,
+						NULL,
+						0);
+				display = context->mod_dispatch.dsp_display_list[IGD_DC_SECONDARY(dc)];
+				surf.offset = pipe2->plane->fb_info->fb_base_offset;
+				/* Call the HAL: */
+				drm_data->rtn = dispatch->set_surface(display,
+						IGD_PRIORITY_NORMAL,
+						IGD_BUFFER_DISPLAY,
+						&surf,
+						NULL,
+						0);
+
+			}
+		}
+		if (drm_data->rtn == 0) {
+			context->mod_dispatch.in_dih_clone_mode = false;
+		}
+	}
+	/* setting fake clone (dih clone) mode */
+	if (drm_data->mode == CLONE) {
+		if (IGD_DC_VEXT(drm_emgd_dc)) {
+			x_offset = 0;
+
+			if( drm_data->dih_clone_display == CLONE_PRIMARY){
+				y_offset = 0;
+				display = context->mod_dispatch.dsp_display_list[IGD_DC_SECONDARY(dc)];
+			} else {
+				y_offset = (pipe1->plane->fb_info->height / 2);
+				display = context->mod_dispatch.dsp_display_list[IGD_DC_PRIMARY(dc)];
+			}
+
+			/* Call Pan display to affect pt_info offsets */
+			drm_data->rtn = dispatch->pan_display(display, x_offset, y_offset);
+		} else { /* in DIH mode */
+
+			/*first save the display's original offset  */
+			surf.offset = pipe1->plane->fb_info->fb_base_offset;
+			drm_data->rtn = dispatch->set_surface(display,
+					IGD_PRIORITY_NORMAL,
+					IGD_BUFFER_SAVE,
+					&surf,
+					NULL,
+					0);
+
+			display = context->mod_dispatch.dsp_display_list[IGD_DC_SECONDARY(dc)];
+			surf.offset = pipe2->plane->fb_info->fb_base_offset;
+			drm_data->rtn = dispatch->set_surface(display,
+					IGD_PRIORITY_NORMAL,
+					IGD_BUFFER_SAVE,
+					&surf,
+					NULL,
+					0);
+
+
+			/* primary display */
+			if( drm_data->dih_clone_display == CLONE_PRIMARY){
+				surf.offset = pipe1->plane->fb_info->fb_base_offset;
+			} else {
+				surf.offset = pipe2->plane->fb_info->fb_base_offset;
+			}
+
+			display = context->mod_dispatch.dsp_display_list[IGD_DC_PRIMARY(dc)];
+
+			/* Call the HAL: */
+			drm_data->rtn = dispatch->set_surface(display,
+					IGD_PRIORITY_NORMAL,
+					IGD_BUFFER_DISPLAY,
+					&surf,
+					NULL,
+					0);
+
+			/* secondary display */
+			display = context->mod_dispatch.dsp_display_list[IGD_DC_SECONDARY(dc)];
+
+			drm_data->rtn = dispatch->set_surface(display,
+					IGD_PRIORITY_NORMAL,
+					IGD_BUFFER_DISPLAY,
+					&surf,
+					NULL,
+					0);
+
+		}
+		if(drm_data->rtn == 0){
+			context->mod_dispatch.in_dih_clone_mode = true;
+			context->mod_dispatch.dih_clone_display = drm_data->dih_clone_display;
+		}
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_dihclone_set_surface() */
+
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's sync() procedure.
+ */
+int emgd_sync(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_sync_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = dispatch->sync(drm_data->display_handle,
+		drm_data->priority,
+		/* Note: Since a pointer is passed to drm_data->in_sync,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		&(drm_data->in_sync),
+		drm_data->flags);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_sync() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's driver_pre_init() procedure.
+ */
+int emgd_driver_pre_init(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	drm_emgd_priv_t *priv = dev->dev_private;
+	igd_context_t *context = NULL;
+	emgd_drm_driver_pre_init_t *drm_data = (emgd_drm_driver_pre_init_t *) arg;
+	igd_param_t *x_params = NULL;
+	igd_param_t *uc_params = NULL;
+	igd_fb_caps_t *pf_caps;
+	igd_display_port_t *port = NULL;
+	int i, err = 0, need_to_startup_hal = (!priv->hal_running) ? 1 : 0;
+	unsigned long save_flags;
+
+	EMGD_TRACE_ENTER;
+
+	/* This flag will cause a call to emgd_init_display() in
+	 * emgd_driver_lastclose() if config_drm.init is true */
+	x_started = true;
+
+	/*
+	 * Need to apply the "x_params" (e.g. "xorg.conf" values, from user-space)
+	 * in the same order as igd_module_init():
+	 * - Nothing to do for REG_INIT(), gmm_init(), nor CMD_INIT().
+	 * - mode_init() has several steps (see below).
+	 * - Nothing to do for APPCONTEXT_INIT().
+	 * - OVERLAY_INIT() is simple (see below).
+	 * - Nothing to do for PWR_INIT(), RESET_INIT(), OS_INIT_INTERRUPT(),
+	 *   BLEND_INIT(), nor INIT_2D().
+	 */
+	x_params = &(drm_data->params);
+
+
+	/* Before applying them, first see if the X driver sent any DTD or attr
+	 * list(s) for the given ports (i.e. from "xorg.conf").  If not, the values
+	 * from "user_config.c" are supposed to be used (and so they need to be
+	 * looked up and copied).
+	 */
+	if (drm_emgd_configid < 0) {
+		uc_params = config_drm.hal_params[0];
+	} else {
+		uc_params = config_drm.hal_params[drm_emgd_configid-1];
+	}
+	for (i = 0 ; i < IGD_MAX_PORTS ; i++) {
+		igd_display_params_t *x_dp = &x_params->display_params[i];
+		igd_display_params_t *uc_dp = NULL;
+		int j;
+
+		if ((x_dp->port_number > 0) && (x_dp->dtd_list.num_dtds == 0)) {
+			/* X didn't have any DTDs for this port.  Look up the corresponding
+			 * display params from "user_config.c"
+			 */
+			for (j = 0 ; j < IGD_MAX_PORTS ; j++) {
+				uc_dp = &uc_params->display_params[j];
+				if (uc_dp->port_number == x_dp->port_number) {
+					break;
+				}
+			}
+			if ((x_dp->port_number == uc_dp->port_number) ||
+				(uc_dp->dtd_list.num_dtds > 0)) {
+				/* Have X's DTD list point to "user_config.c"'s DTD list */
+				x_dp->dtd_list.num_dtds = uc_dp->dtd_list.num_dtds;
+				x_dp->dtd_list.dtd = uc_dp->dtd_list.dtd;
+				x_dp->present_params |= IGD_PARAM_DTD_LIST;
+			}
+		}
+
+		if ((x_dp->port_number > 0) && (x_dp->attr_list.num_attrs == 0)) {
+			/* X didn't have any ATTRs for this port.  Look up the corresponding
+			 * display params from "user_config.c"
+			 */
+			for (j = 0 ; j < IGD_MAX_PORTS ; j++) {
+				uc_dp = &uc_params->display_params[j];
+				if (uc_dp->port_number == x_dp->port_number) {
+					break;
+				}
+			}
+			if ((x_dp->port_number == uc_dp->port_number) ||
+				(uc_dp->attr_list.num_attrs > 0)) {
+				/* Have X's ATTR list point to "user_config.c"'s attr list */
+				x_dp->attr_list.num_attrs = uc_dp->attr_list.num_attrs;
+				x_dp->attr_list.attr = uc_dp->attr_list.attr;
+				x_dp->present_params |= IGD_PARAM_ATTR_LIST;
+			}
+		}
+	}
+#ifdef DEBUG_BUILD_TYPE
+	if(emgd_debug && emgd_debug->MODULE_NAME)
+		emgd_print_params(x_params);
+#endif
+
+
+	if (need_to_startup_hal) {
+		/* emgd_driver_load() deferred driver initialization and configuration.
+		 * Therefore, do it now.
+		 *
+		 * Before doing so, ensure both ports are turned on, even if the user
+		 * only requested one port.
+		 *
+		 * TODO/FIXME - This assumes only two ports; is that wise long-term?
+		 */
+		if (x_params->port_order[1] == 0) {
+			if (x_params->port_order[0] == 2) {
+				x_params->port_order[1] = 4;
+			} else {
+				x_params->port_order[1] = 2;
+			}
+		}
+
+		EMGD_DEBUG("Starting the HAL");
+		err = emgd_startup_hal(dev, x_params);
+		if (err != 0) {
+			mutex_unlock(&dev->struct_mutex);
+			return err;
+		} else {
+			priv = dev->dev_private;
+			priv->hal_running = 1;
+		}
+	}
+	/* Now that we know the driver is initialized, we can do the following: */
+	context = (igd_context_t *) handle;
+	context->mod_dispatch.init_params = x_params;
+
+	if (config_drm.init) {
+		if (config_drm.kms) {
+			save_flags = (IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT)| IGD_REG_SAVE_TYPE_REG;
+		} else {
+			save_flags = (IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT &
+				~IGD_REG_SAVE_RB) | IGD_REG_SAVE_TYPE_REG;
+		}
+		dispatch->driver_save(handle, save_flags);
+		EMGD_DEBUG("State of saved registers is CONSOLE_STATE_SAVED");
+		priv->saved_registers = CONSOLE_STATE_SAVED;
+	}
+
+	if (!need_to_startup_hal) {
+		/* emgd_driver_load() initialized and configured the driver.
+		 * Therefore, we must now re-initialize and poke x_params values into
+		 * various parts of the driver.
+		 */
+		EMGD_DEBUG("Re-initializing the HAL with X driver values");
+
+		/* mode_init() (in "micro_mode.c") only sets the following value, which
+		 * affects future alter_displays() calls.  Note that no code seems to
+		 * set x_params->display_color in the X driver.
+		 */
+		mode_context->display_color = x_params->display_color;
+
+		/* mode_init() calls full_mode_init() (in "igd_mode.c"), which set the
+		 * following values, of which, only seamless is used in the HAL.
+		 *
+		 * If seamless is TRUE, full_mode_init() will call the static procedure
+		 * get_fw_info(), which will get firmware-programmed info before the
+		 * driver starts re-programming it.  get_fw_info will set seamless to
+		 * FALSE if there's any errors.  The get_fw_info() procedure can't be
+		 * called here because it's static.  It may also be too late to call it
+		 * at this point (i.e. if the HAL has already touched anything, and
+		 * that may have something to do with how many times the X server
+		 * process starts).
+		 */
+		mode_context->quickboot = x_params->quickboot;
+		mode_context->seamless = x_params->qb_seamless;
+		mode_context->video_input = x_params->qb_video_input;
+		mode_context->splash = x_params->qb_splash;
+		mode_context->first_alter = TRUE;
+
+		if (config_drm.init) {
+			for (i=0; i < IGD_MAX_PORTS; i++) {
+				mode_context->batch_blits[x_params->display_params[i].port_number - 1]
+					= x_params->display_params[i].flags & IGD_DISPLAY_BATCH_BLITS;
+			}
+			toggle_vblank_interrupts(TRUE);
+		}
+
+		/* In case the X server ran, exited, and is starting again, we may need
+		 * to put the X server's state back:
+		 */
+		if (priv->saved_registers == X_SERVER_STATE_SAVED) {
+			EMGD_DEBUG("Need to restore the X server's saved register state");
+
+			if (config_drm.init) {
+				save_flags = IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT;
+			}
+			else {
+				save_flags = IGD_REG_SAVE_ALL;
+			}
+			err = dispatch->driver_save_restore(handle, save_flags);
+			EMGD_DEBUG("State of saved registers is CONSOLE_STATE_SAVED");
+			priv->saved_registers = CONSOLE_STATE_SAVED;
+		}
+
+		if(!mode_context->seamless) {
+			/* NOTE: In order for some new values to be poked into the port
+			 * drivers' hardware (e.g. the LVDS panel depth), the power must be
+			 * turned off on those devices.  This used to be done during the
+			 * emgd_driver_lastclose() function, but that prevents the console from
+			 * being seen after X quits.
+			 */
+			while ((port = context->mod_dispatch.dsp_get_next_port(context,
+						port, 0)) != NULL) {
+				/* power off LVDS only */
+				if (port->pd_driver &&  (port->port_number == IGD_PORT_TYPE_LVDS) &&
+					!mode_context->seamless) {
+					EMGD_DEBUG("Turning off power for port %lu\n", port->port_number);
+					dispatch->power_display(context, port->port_number,
+						IGD_POWERSTATE_D3);
+				}
+			}
+
+			/* mode_init() calls dsp_init() (in "dsp.c"), which uses the
+			 * display_flags, display_params & port_order params.  In the case of
+			 * display_params, each element of the array contains dtd_list and
+			 * attr_list, both of which point to memory that must be separately
+			 * copied to kernel-space.  The display_flags, display_params &
+			 * port_order params affect the initialization of the DSP module, and
+			 * as such, it may not be easy to simply poke values here.
+			 *
+			 * Thus, it appears that the DSP module must somehow be re-initialized.
+			 *
+			 * Note: dsp_init() also calls full_dsp_init() and the device-specific
+			 * init procedures, none of which uses any params.
+			 */
+			EMGD_DEBUG("Calling dsp_shutdown()");
+			dsp_shutdown(handle);
+
+			EMGD_DEBUG("Calling dsp_init()");
+			dsp_init(handle);
+
+
+			/* Poke any new port attributes & DTDs into the port drivers: */
+			while ((port = context->mod_dispatch.dsp_get_next_port(context,
+						port, 0)) != NULL) {
+					if (port->pd_driver && !mode_context->seamless) {
+						EMGD_DEBUG("Insert new port attrs/DTDs for port %lu",
+						port->port_number);
+					pi_pd_init(port, 0, 0, FALSE);
+				}
+			}
+
+			/*
+			 * Because dsp_init was called above, a lot of the configuration
+			 * performed by the driver at initializat is now invalid.
+			 *
+			 * Setting the dc to 0 makes sure we don't try to use other
+			 * data structures before alter_displays has been called again.
+			 */
+			priv->dc = 0;
+		}
+
+
+		/* Set a flag so that emgd_alter_displays() will turn back on the port
+		 * drivers' hardware:
+		 */
+		if(!mode_context->seamless) {
+			priv->must_power_on_ports = 1;
+		}
+
+		/* OVERLAY_INIT() translates to the _overlay_init() procedure (in
+		 * "micro_ovl.c").  It calls ovl_full_init() (in "igd_ovl.c").  If the
+		 * IGD_DISPLAY_FB_BLEND_OVL bit in "display_flags" is set, it sets
+		 * ovl_context->fb_blend_ovl to 1, which affects future atler_ovl()
+		 * calls.
+		 */
+		if (x_params->display_flags & IGD_DISPLAY_FB_BLEND_OVL) {
+			ovl_context->fb_blend_ovl = 1;
+		}
+	} /* if (!need_to_startup_hal) */
+
+/* NOTE -- Below is our original analysis of what values/modules need
+ * to be dealt with (above):
+ *
+ * - mode_init() (in "micro_mode.c") uses display_color.
+ *
+ *   - Note: the value is stored in a global variable:
+ *     mode_context->display_color.  This value is used only once--in
+ *     full_clear_fb() (igd_mode.c), which is only called by
+ *     configure_display() (micro_mode.c), which is only called by
+ *     igd_alter_displays() (micro_mode.c).
+ *
+ *   [ ] Implemented proposal: set the value.  Correct approach?
+ *
+ * - full_mode_init() (in "igd_mode.c", called by mode_init(); lives in
+ *   "micro_mode.c", which is called by init_modules()) uses the quickboot &
+ *   qb_* params.  The values are stored in a global variable: mode_context->*.
+ *   Here's what I learned in terms of what is used (same is true for IEGD
+ *   source):
+ *
+ *     - The mode_context->quickboot value is only set (here) and is never used.
+ *
+ *     - mode_context->seamless (corresponding to params->qb_seamless) is
+ *       used in full_mode_init() (igd_mode.c) to decide whether (if TRUE) to
+ *       call get_fw_info(), which may set it to FALSE depending on whether
+ *       there is firmware info that should affect it.  It is also read by
+ *       configure_display() (micro_mode.c), which is only called by
+ *       igd_alter_displays() (micro_mode.c).  It is also read & used to decide
+ *       something by igd_alter_displays() (micro_mode.c).
+ *
+ *     - mode_context->video_input (corresponding to params->qb_video_input) is
+ *       only set (here) and is never used.
+ *
+ *     - mode_context->splash (corresponding to params->qb_splash) is only set
+ *       (here) and is never used.
+ *
+ *   [ ] New Proposal: potentially set the values and do nothing else.  Is this
+ *       the correct approach?
+ *
+ *   [-] Old Proposal: set the values, and if seamless is TRUE, call
+ *       get_fw_info().  Can't actually do so, as get_fw_info() is a static
+ *       procedure.
+ *
+ * - dsp_init() (in "dsp.c", called by mode_init(); lives in "micro_mode.c",
+ *   which is called by init_modules()) uses the display_flags, display_params
+ *   & port_order params.  It also calls full_dsp_init() and the
+ *   device-specific init procedures, neither of which uses any params.  Within
+ *   display_params, there are several params looked at: port_number,
+ *   present_params, ddc_gpio, ddc_speed, ddc_dab, i2c_gpio, i2c_speed,
+ *   i2c_dab, fp_info, dtd_list, and attr_list.  Right now, none of these are
+ *   set.
+ *
+ * - Notes: Here's what I learned in terms of what is used:
+ *
+ *     - params->display_flags is stored in dsp_context->display_flags.  The
+ *       value is used by dsp_dc_init() (dsp.c, called by igd_query_dc() and
+ *       mode_init()--called after dsp_init()) to determine if
+ *       IGD_DISPLAY_DETECT is set (used frequently in that procedure).  The
+ *       value is used by igd_query_dc() (dsp.c, called by igd_query_dc() and
+ *       mode_init()--called after dsp_init())--after calling dsp_dc_init()
+ *       once, if there's only 1 DC, it uses this value to determine if
+ *       IGD_DISPLAY_DETECT is set, and if so the bit is immediately turned off
+ *       and dsp_dc_init() is called again.
+ *
+ *     - params->port_order is used as a parameter to do_port_order() (in
+ *       "dsp.c"), which is only called this once via dsp_init().  Based on
+ *       this, it seems that we should try to figure out how to re-init this
+ *       code.  Correct approach?
+ *
+ *     - params->display_params is iterated over ... OUCH!  display_params IS A
+ *       STRUCT OF TYPE igd_param_attr_list_t, AND SOME OF ITS MEMBERS HAVE
+ *       POINTERS WITHIN THEM (i.e. HARDER TO GET ACROSS THE IOCTL BOUNDARY).
+ *
+ *       Based on the above, it seems that the idea of
+ *       re-initializing the DSP module seems like the correct approach.  I'm
+ *       not quite sure how to do that, but it seems like the right direction
+ *       to both keep the HAL code unmodified, and do the user/kernel-space
+ *       split.
+ *
+ *       [X] Implemented proposal: copy all user-space memory to kernel-space,
+ *           and later free any temporarily allocated memory.
+ *       [ ] Proposal: re-initialize the DSP module.  Correct approach?
+ *
+ * - OVERLAY_INIT() translates to _overlay_init() (in "micro_ovl.c") takes
+ *   params as a parameter, which it passes to ovl_full_init() (in
+ *   "igd_ovl.c").  If the IGD_DISPLAY_FB_BLEND_OVL bit in "display_flags" is
+ *   set, it sets ovl_context->fb_blend_ovl to 1.  This flag is used in the
+ *   following procedures:
+ *
+ *   - ovl_send_instr_plb() (in "emgd_drm/video/overlay/plb/ovl_plb.c") is
+ *     called by alter_ovl_plb(), which is called through the ovl_dispatch_plb
+ *     table (of type ovl_dispatch_t).
+ *
+ *   - ovl2_send_instr_plb() (in "emgd_drm/video/overlay/plb/ovl2_plb.c") is
+ *     called by alter_ovl2_plb(), which is called through the ovl_dispatch_plb
+ *     table (of type ovl_dispatch_t).
+ *
+ *   - ovl_send_instr_tnc() (in "emgd_drm/video/overlay/tnc/ovl_tnc.c") is
+ *     called by alter_ovl_tnc(), which is called through the ovl_dispatch_tnc
+ *     table (of type ovl_dispatch_t).
+ *
+ *   [ ] Implemented proposal: If the IGD_DISPLAY_FB_BLEND_OVL bit in
+ *   "display_flags" is set, set ovl_context->fb_blend_ovl to 1.  Correct
+ *   approach?
+ *
+ * - pi_pd_init() (in the file "emgd_drm/display/pi/cmd/pi.c") accesses
+ *   params->display_params.  It is called by pi_pd_register() ("pi.c"), which
+ *   is called by both igd_pd_register() (in "igd_pi.c") and pd_register() (in
+ *   "emgd_drm/display/pd/pd.c").  igd_pd_register() is called by
+ *   load_port_drivers() (in "emgd_drv/emgd_dpdloader.c") which is no longer
+ *   called (was called during PreInit()).
+ *   pd_register() is called by lvds_init() and sdvo_init(), both of which are
+ *   called by pi_init_all(), which is not being called.
+ *
+ *   [ ] TODO - LEARN ABOUT HOW THE STATICALLY-LOADED PORT DRIVERS WILL GET
+ *       THEIR params INFO, AND WHEN THEY WILL WANT IT (i.e. IN RELATION TO X
+ *       DRIVER PreInit())
+ *
+ * None of the other modules use and/or need any params at this point:
+ *
+ * - REG_INIT translates to _reg_init() and uses preserve_reg.  It's too late
+ *   to undo what we chose at kernel init time.
+ * - gmm_init() ignores page_request and max_fb_size.
+ * - CMD_INIT() translates to 0 (i.e. Koheo doesn't use this module).
+ * - pi_init() (in "pi.c", called by mode_init(); lives in "micro_mode.c",
+ *   which is called by init_modules()) uses no params.
+ * - APPCONTEXT_INIT() translates to appcontext_init() doesn't use any params
+ * - PWR_INIT() translates to _pwr_init() (in "igd_pwr.c") doesn't look a any
+ *   params in context, nor does pwr_init_plb() (in "pwr_plb.c").
+ * - RESET_INIT() translates to _reset_init() IS MISSING.  IS IT NEEDED???
+ *   Just in case, I looked at the IEGD version (in "igd_reset.c"), which
+ *   doesn't look at any params.
+ * - OS_INIT_INTERRUPT() doesn't translate into anything real (i.e. IT IS
+ *   MISSING).  IS IT NEEDED???  Just in case, I looked at the IEGD version (in
+ *   "oal/xfree86/src/interrupt/interrupt.c"), and it is a no-op.
+ * - BLEND_INIT() translates to 0 (i.e. Koheo doesn't use this module).
+ * - INIT_2D() translates to 0 (i.e. Koheo doesn't use this module).
+ */
+
+
+	/* Call the HAL's igd_get_config_info() procedure: */
+	drm_data->rtn = igd_get_config_info(handle,
+		/* Note: Since a pointer is passed to drm_data->config_info, there's no
+		 * need to copy anything back into drm_data, except for the return
+		 * value.
+		 */
+		&(drm_data->config_info));
+
+	/* Check for a bit depth we support */
+	pf_caps = drm_data->config_info.fb_caps;
+
+	while (pf_caps && pf_caps->pixel_format != 0) {
+		if (drm_data->bpp == IGD_PF_DEPTH(pf_caps->pixel_format)) {
+			EMGD_DEBUG("Found matching PF: %lx for depth %lx bpp.",
+				pf_caps->pixel_format, drm_data->bpp);
+			drm_data->config_info.pixel_format = pf_caps->pixel_format;
+			break;
+		}
+		pf_caps++;
+	}
+	if (!drm_data->config_info.pixel_format) {
+		EMGD_DEBUG("Given depth (%lx) is not supported.", drm_data->bpp);
+	}
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_driver_pre_init() */
+
+
+/*!
+ * IOCTL to support emgd_hal2drm_get_ports(), which replaces the X driver's
+ * usage of igd_get_param(...,IGD_PARAM_PORT_LIST,...)
+ */
+int emgd_driver_get_ports(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_driver_get_ports_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Call the HAL: */
+	drm_data->rtn = igd_get_param(handle, IGD_PARAM_PORT_LIST,
+		/* Note: Since a pointer is passed to drm_data->ports,
+		 * there's no need to copy anything back into drm_data, except
+		 * for the return value.
+		 */
+		drm_data->ports);
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_driver_get_ports() */
+
+/*!
+ * IOCTL to support emgd_hal2drm_get_page_list(), which get's the list
+ * of page address that make up a memory allocation.
+ */
+int emgd_get_page_list(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_get_page_list_t *drm_data = arg;
+	unsigned long cnt;
+	unsigned long *list;
+	unsigned long *new_list;
+	int i;
+
+	EMGD_TRACE_ENTER;
+
+	dispatch->gmm_get_page_list(drm_data->offset, &list, &cnt);
+	if (cnt > drm_data->addr_count) {
+		/*
+		 * The user didn't allocate enough space for the
+		 * page list, this is an error.
+		 */
+		drm_data->addr_count = cnt;
+		drm_data->rtn = -IGD_NO_MEM;
+	} else {
+		/* Copy the address list to the caller */
+		new_list = (unsigned long *)(arg + sizeof(emgd_drm_get_page_list_t));
+		for (i = 0; i < cnt; i++) {
+			new_list[i] = list[i];
+		}
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_driver_get_page_list() */
+
+/*!
+ * IOCTL to allow the X driver (in "egd_driver.c") to start the Imagination
+ * Technologies PVR services DRM/kernel module, which will start our 3rd-party
+ * display driver (3DD).
+ */
+int emgd_start_pvrsrv(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_start_pvrsrv_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+
+	EMGD_TRACE_ENTER;
+
+	/* Tell the 3DD the status of whether the X server is running: */
+	if (!priv->xserver_running && drm_data->xserver) {
+		priv->xserver_running = 1;
+		if (priv->reinit_3dd) {
+			priv->reinit_3dd(dev);
+		}
+	}
+
+	drm_data->rtn = 0;
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_start_pvrsrv() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's video_cmd_buf() procedure.
+ */
+int emgd_video_cmd_buf(struct drm_device *dev, void *arg,
+		struct drm_file *file_priv)
+{
+	emgd_drm_video_cmd_buf_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	igd_context_t *context = priv->context;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Call the HAL: */
+	switch (drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = process_video_decode_plb(context, drm_data->offset,
+								drm_data->kernel_virt_addr,
+								&(drm_data->fence_id));
+			break;
+		case TNC_ENGINE_ENCODE:
+			drm_data->rtn = process_video_encode_tnc(context, drm_data->offset,
+								drm_data->kernel_virt_addr,
+								&(drm_data->fence_id));
+			break;
+		default:
+			break;
+	}
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_video_cmd_buf() */
+
+
+/*!
+ * IOCTL to bridge the IAL to the HAL's get_device_info() procedure.
+ */
+int emgd_get_device_info(struct drm_device *dev, void *arg,
+    struct drm_file *file_priv)
+{
+    emgd_drm_device_info_t *drm_data = arg;
+    drm_emgd_priv_t *priv = dev->dev_private;
+    igd_context_t *context = priv->context;
+
+    EMGD_TRACE_ENTER;
+
+    drm_data->display_memory_offset = context->device_context.fb_adr;
+    drm_data->display_memory_size = context->device_context.mem_size;
+    drm_data->device_id = context->device_context.did;
+    drm_data->revision_id = context->device_context.rid;
+    drm_data->bridge_id = context->device_context.bid;
+
+    EMGD_TRACE_EXIT;
+    return 0;
+} /* emgd_get_device_info() */
+
+
+int emgd_init_video(struct drm_device *dev, void *arg,
+		struct drm_file *file_priv)
+{
+	emgd_drm_init_video_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	igd_context_t *context = priv->context;
+
+	EMGD_TRACE_ENTER;
+
+	switch (drm_data->cmd) {
+	case CMD_VIDEO_STATE :
+		switch (drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_query_plb(context, &drm_data->status);
+			break;
+		default:
+			drm_data->rtn = 1;
+			break;
+		}
+		break;
+	case CMD_VIDEO_INITIALIZE :
+		/* Call the HAL: */
+		switch (drm_data->engine) {
+		case PSB_ENGINE_COMPOSITOR_MMU:
+			drm_data->rtn = msvdx_init_compositor_mmu(drm_data->base0);
+			break;
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_init_plb(drm_data->base0, drm_data->base1,
+						       drm_data->fw_priv, drm_data->fw_size, 0);
+			break;
+		case TNC_ENGINE_ENCODE:
+			drm_data->rtn = topaz_init_tnc(drm_data->wb_offset, drm_data->wb_addr, drm_data->firm_addr);
+			break;
+		default:
+			break;
+		}
+		break;
+	case CMD_VIDEO_UNINITIALIZE :
+		switch (drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_uninit_plb(context);
+			break;
+		default:
+			break;
+		}
+		break;
+	case CMD_VIDEO_CLOSE_CONTEXT :
+		switch (drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_close_context(context);
+			break;
+		default:
+			break;
+		}
+		break;
+	case CMD_VIDEO_CREATE_CONTEXT:
+		switch (drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_create_context(context);
+			break;
+		default:
+			break;
+		}
+		break;
+	case CMD_VIDEO_SHUTDOWN:
+		switch (drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_shutdown_plb(context);
+			break;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_init_video() */
+
+int emgd_video_get_info(struct drm_device *dev, void *arg,
+			struct drm_file *file_priv)
+{
+	emgd_drm_video_get_info_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	igd_context_t *context = priv->context;
+
+	EMGD_TRACE_ENTER;
+
+	switch(drm_data->cmd) {
+		case CMD_VIDEO_GET_FENCE_ID:
+			switch(drm_data->engine){
+				case PSB_ENGINE_VIDEO:
+					drm_data->rtn = msvdx_get_fence_id(context, &(drm_data->fence_id));
+					break;
+				case TNC_ENGINE_ENCODE:
+					drm_data->rtn = topaz_get_fence_id(context, &(drm_data->fence_id));
+					break;
+				default:
+					break;
+			}
+			break;
+		case CMD_VIDOE_GET_FRAME_SKIP:
+			switch(drm_data->engine){
+				case PSB_ENGINE_VIDEO:
+					break;
+				case TNC_ENGINE_ENCODE:
+					drm_data->rtn = topaz_get_frame_skip(context, &(drm_data->frame_skip));
+					break;
+				default:
+					break;
+			}
+			break;
+		case CMD_VIDEO_GET_MSVDX_STATUS:
+			if ((&context->mod_dispatch) && context->mod_dispatch.msvdx_status)
+				drm_data->rtn = context->mod_dispatch.msvdx_status(context, &drm_data->queue_status, &drm_data->mtx_msg_status);
+			break;
+		default:
+			break;
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+int emgd_video_flush_tlb(struct drm_device *dev, void *arg,
+			struct drm_file *file_priv)
+{
+	emgd_drm_video_flush_tlb_t *drm_data = arg;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	igd_context_t *context = priv->context;
+
+	EMGD_TRACE_ENTER;
+
+	switch(drm_data->engine) {
+		case PSB_ENGINE_VIDEO:
+			drm_data->rtn = msvdx_flush_tlb(context);
+			break;
+		case TNC_ENGINE_ENCODE:
+			drm_data->rtn = topaz_flush_tnc(context);
+			break;
+		default:
+			break;
+	}
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int emgd_preinit_mmu(struct drm_device *dev, void *arg,
+			struct drm_file *file_priv)
+{
+	emgd_drm_preinit_mmu_t *drm_data = arg;
+
+	EMGD_TRACE_ENTER;
+
+	drm_data->rtn = msvdx_preinit_mmu(drm_data->memcontext);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int emgd_get_golden_htotal(struct drm_device *dev, void *arg,
+		struct drm_file *file_priv){
+
+
+	emgd_drm_get_golden_htotal_t *drm_data = arg;
+	igd_context_t *context = (igd_context_t *) handle;
+	igd_display_context_t *display;
+	pd_timing_t igd_mode_table_in[2];
+	pd_timing_t igd_mode_table_out;
+
+	igd_display_info_t *out_mode = (igd_display_info_t *) drm_data->out_mode;
+	igd_display_info_t *in_mode = (igd_display_info_t *) drm_data->in_mode;
+
+
+	EMGD_DEBUG("emgd_get_golden_htotal : Entry");
+
+	/* parameters sanity check */
+	if (out_mode == NULL) {
+		EMGD_ERROR("emgd_get_golden_htotal : NO Output Buffer");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (in_mode == NULL) {
+		EMGD_ERROR("emgD_get_golden_htotal : NO Input Buffer");
+		return -IGD_ERROR_INVAL;
+
+	}
+
+	/* Zero out the data structures so that we can check for error later */
+	memset(igd_mode_table_in, 0, 2 * sizeof(pd_timing_t));
+	memset(&igd_mode_table_out, 0, sizeof(pd_timing_t));
+
+	/* To prevent a kernel OOPS, ensure the following value is non-NULL: */
+	display = context->mod_dispatch.dsp_display_list[2];
+	if ((display == NULL) || (PORT(display, 2) == NULL)) {
+		EMGD_ERROR_EXIT("emgd_get_golden_htotal() given an invalid port "
+			"number (%d)\n", 2);
+		return -IGD_ERROR_INVAL;
+	}
+
+    /* convert the data from escape structure to pd_timing */
+    igd_mode_table_in[0].width = in_mode->width;
+    igd_mode_table_in[0].height = in_mode->height;
+    igd_mode_table_in[0].refresh = in_mode->refresh;
+
+    /* Set the end of list */
+    igd_mode_table_in[1].width = PD_TIMING_LIST_END;
+    igd_mode_table_in[1].extn_ptr = NULL;
+
+    /************************************************************/
+    /* call dispatch function that will generate the golden htotal
+     * for all the modes. The pd_timing_t has the same structure as
+     * igd_display_t so we can typecast without issue.
+     */
+    /************************************************************/
+    context->mod_dispatch.get_dd_timing(display,
+                            (pd_timing_t*)&igd_mode_table_in);
+
+    /************************************************************/
+    /* Send back the modified mode list that contains the delta */
+    /************************************************************/
+
+    memcpy (out_mode, in_mode, sizeof(igd_display_info_t));
+    out_mode->reserved_dd = igd_mode_table_in[0].reserved_dd;
+
+	out_mode->hblank_end = igd_mode_table_in[0].hblank_end;
+	out_mode->vblank_end = igd_mode_table_in[0].vblank_end;
+
+
+    return 0;
+}
+
+int emgd_control_plane_format(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_control_plane_format_t *drm_data = arg;
+	igd_context_t *context = (igd_context_t *) handle;
+	igd_plane_t *plane_override;
+
+	EMGD_DEBUG("emgd_control_plane_format : Entry");
+
+	if (drm_data->use_plane == FALSE)
+	{
+		/* Do some error checking first */
+		if((drm_data->enable != 0 && drm_data->enable != 1) ||
+			(drm_data->primary_secondary_dsp == NULL)) {
+			EMGD_ERROR("emgd_control_plane_format: Invalid parameters");
+			return -IGD_ERROR_INVAL;
+		}
+
+		plane_override = ((igd_plane_t *)(PLANE(drm_data->primary_secondary_dsp)));
+	} else {
+		/* Do some error checking first */
+		if((drm_data->enable != 0 && drm_data->enable != 1) ||
+			(drm_data->display_plane != 0 && drm_data->display_plane != 1)) {
+			EMGD_ERROR("emgd_control_plane_format: Invalid parameters");
+			return -IGD_ERROR_INVAL;
+		}
+	
+		/* Set plane_override to NULL */
+		plane_override = NULL;
+	}
+
+	/* Call the DSP modules function if no errors */
+	context->mod_dispatch.dsp_control_plane_format(drm_data->enable,
+			drm_data->display_plane, plane_override);
+
+	return 0;
+}
+
+int emgd_set_overlay_display(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	emgd_drm_set_overlay_display_t *drm_data = arg;
+	igd_context_t *context = (igd_context_t *) handle;
+
+	EMGD_DEBUG("emgd_set_overlay_display : Entry");
+
+	/* Do some error checking first */
+	if((drm_data->ovl_display[OVL_PRIMARY] == 0) || 
+		(drm_data->ovl_display[OVL_SECONDARY] == 0)) {
+		/* Both Ovl displays need to have valid handle
+		 * (i.e. Single mode is not supported) */
+		EMGD_ERROR("emgd_set_overlay_display: Invalid parameters");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Call the set_ovl_display if no errors */
+	context->dispatch.set_ovl_display(drm_data->ovl_display);
+
+	return 0;
+}
+
+int emgd_query_2d_caps_hwhint(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv){
+
+
+	emgd_drm_query_2d_caps_hwhint_t *drm_data;
+	unsigned long caps_val;
+	unsigned long *status;
+
+	EMGD_DEBUG("emgd_query_2d_caps_hwhint : Entry");
+
+    /* parameters sanity check */
+	if (arg == NULL) {
+          EMGD_ERROR("emgd_query_2d_caps_hwhint : invalid argument");
+        return -IGD_ERROR_INVAL;
+    }
+
+
+	drm_data = arg;
+	caps_val = (unsigned long) drm_data->caps_val;
+	status = (unsigned long *) drm_data->status;
+
+    igd_query_2d_caps_hwhint(handle, caps_val, status);
+
+    return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_mmap.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_mmap.c
new file mode 100644
index 0000000..c1d13a2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_mmap.c
@@ -0,0 +1,186 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_mmap.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Memory mapping functions.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.gart
+
+#include <linux/mm.h>
+#include <drm_emgd_private.h>
+#include <emgd_drv.h>
+#include <emgd_drm.h>
+#include <memory.h>
+#include <memlist.h>
+#include <io.h>
+
+/*
+ * Bottom 256MB reserved for display
+ */
+#define DRM_PSB_FILE_PAGE_OFFSET (0x10000000UL >> PAGE_SHIFT)
+
+extern gmm_chunk_t *gmm_get_chunk(igd_context_t *context,
+		unsigned long vm_pgoff);
+extern int PVRMMap(struct file *pFile, struct vm_area_struct *ps_vma);
+
+static int emgd_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
+static void emgd_vm_open(struct vm_area_struct *vma);
+static void emgd_vm_close(struct vm_area_struct *vma);
+
+static struct vm_operations_struct emgd_vm_ops = {
+	.fault = emgd_vm_fault,
+	.open = emgd_vm_open,
+	.close = emgd_vm_close
+};
+
+/*
+ * Create a virtual address mapping for physical pages of memory.
+ *
+ * This needs to handle requrests for both the EMGD display driver
+ * and the IMG 2D/3D drivers.
+ *
+ * If the page offset falls below the 256MB limit for display,
+ * then map display memory. If above, route to the IMG handler.
+ */
+int emgd_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct drm_file *file_priv;
+	drm_emgd_priv_t *emgd_priv;
+	gmm_chunk_t *chunk;
+	unsigned long offset;
+
+	/*
+	 * re-direct offsets beyond the 256MB display range to PVRMMap
+	 */
+	if (vma->vm_pgoff > DRM_PSB_FILE_PAGE_OFFSET) {
+		EMGD_DEBUG("emgd_mmap: Calling PVRMMap().");
+		return PVRMMap(filp, vma);
+	}
+
+	file_priv = (struct drm_file *) filp->private_data;
+	emgd_priv = (drm_emgd_priv_t *)file_priv->minor->dev->dev_private;
+	offset = vma->vm_pgoff << PAGE_SHIFT;
+
+	/*
+	 * Look up the buffer in the gmm chunk list based on offset
+	 * and size.
+	 */
+	/* chunk = emgd_priv->context->dispatch->gmm_get_chunk(vma->vm_pgoff);*/
+	chunk = gmm_get_chunk(emgd_priv->context, offset);
+	if (chunk == NULL) {
+		printk(KERN_ERR "emgd_mmap: Failed to find memory at 0x%lx.", offset);
+	}
+
+	/*
+	 * Fill in the vma
+	 */
+	vma->vm_ops = &emgd_vm_ops;
+	vma->vm_private_data = chunk;
+	vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
+	pgprot_val(vma->vm_page_prot) =
+		pgprot_val(vma->vm_page_prot) | _PAGE_CACHE_UC_MINUS;
+
+	return 0;
+}
+
+
+static int emgd_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
+{
+	unsigned long offset;
+	unsigned long pg_offset;
+	gmm_chunk_t *chunk;
+	struct page *page;
+
+	offset = (unsigned long)vmf->virtual_address - vma->vm_start;
+	pg_offset = offset >> PAGE_SHIFT;
+
+	chunk = (gmm_chunk_t *)vma->vm_private_data;
+
+	if (chunk == NULL) {
+		printk(KERN_ERR "emgd_vm_fault: Chunk is NULL.\n");
+		vmf->page = NULL;
+		return VM_FAULT_SIGBUS;
+	}
+
+	if (pg_offset > chunk->gtt_mem->page_count) {
+		printk(KERN_ERR "emgd_vm_fault: page offet (%lu) > page count (%d)\n",
+				pg_offset, chunk->gtt_mem->page_count);
+		return VM_FAULT_SIGBUS;
+	}
+
+	page = chunk->gtt_mem->pages[pg_offset];
+	get_page(page);
+	vmf->page = page;
+
+	return 0;
+}
+
+/*
+ * Increase the refrence count on the memory object
+ */
+static void emgd_vm_open(struct vm_area_struct *vma)
+{
+	struct drm_file *priv = vma->vm_file->private_data;
+	struct drm_device *dev = priv->minor->dev;
+
+
+	/*
+	 * Does the DRM really need to keep track of the count if we're managing
+	 * everything?
+	 */
+	atomic_inc(&dev->vma_count);
+
+	/*
+	 * DRM code maintains a list of vma's and when open is
+	 * called, the vma is added to the list.
+	 */
+
+}
+
+/*
+ * Decrease the reference count on the memory object and
+ * remove the refrence to the memory object.
+ */
+static void emgd_vm_close(struct vm_area_struct *vma)
+{
+	struct drm_file *priv = vma->vm_file->private_data;
+	struct drm_device *dev = priv->minor->dev;
+
+	/*
+	 * Does the DRM really need to keep track of the count if we're managing
+	 * everything?
+	 */
+	atomic_dec(&dev->vma_count);
+
+	/*
+	 * DRM code maintains a list of vma's and when close is
+	 * called, the vma is removed to the list.
+	 */
+
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/drm/emgd_test_pvrsrv.c b/drivers/gpu/drm/emgd/emgd/drm/emgd_test_pvrsrv.c
new file mode 100644
index 0000000..6ba4567
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/emgd_test_pvrsrv.c
@@ -0,0 +1,1365 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_test_pvrsrv.c
+ * $Revision: 1.23 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file implements an ioctl devoted to the "ult/drmtest_surface.c" test.
+ *  It tests the 3rd-Party Display Driver (and as such, it tests some of the
+ *  PVR services) within kernel-space.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.oal
+
+#include "drmP.h"
+#include "drm.h"
+
+#include "drm_emgd_private.h"
+#include "emgd_drm.h"
+#include "emgd_drv.h"
+#include "memory.h"
+
+#include "module_init.h"
+#include "mode_dispatch.h"
+#include "ovl_dispatch.h"
+#include "ovl_virt.h"
+#include "msvdx.h"
+
+#include "memmap.h"
+#include "sched.h"
+
+#include "services.h"
+#include "perproc.h"
+#include "pvr_bridge_km.h"
+#include "syscommon.h"
+#include "pvr_drm.h"
+/* Pull in whether HAL & 3rd-party display driver (3DD) should use interrupts */
+#include "emgd_shared.h"
+
+
+
+/* Note: The following "TEST_ERROR" macro is only for this function.  It prints
+ * the message to the kernel log, and it formats and inserts the same message
+ * into the drm_data->rtn_msg, so that the user-space code can print it.
+ */
+#define TEST_ERROR(format, arg...) \
+	printk(KERN_ERR format, ##arg);\
+	snprintf(drm_data->rtn_msg, 1024, format, ##arg);
+
+
+#define NUM_FRONT_BUFFERS 1
+#define NUM_BACK_BUFFERS 2
+#define NUM_BUFFERS (NUM_FRONT_BUFFERS + NUM_BACK_BUFFERS)
+
+
+#define DEVICE1 0
+#define DEVICE2 1
+
+
+/* Common colors: */
+#define BLACK   0xff000000
+#define WHITE   0xffffffff
+#define RED     0xffff0000
+#define GREEN   0xff00ff00
+#define BLUE    0xff0000ff
+#define CYAN    0xff00ffff
+#define YELLOW  0xffffff00
+#define MAGENTA 0xffff00ff
+
+
+/*
+ * Note: Am using the following global variables as a sort-of "poor man's OO"
+ * class members:
+ */
+static emgd_drm_test_pvrsrv_t *drm_data;
+static drm_emgd_priv_t *priv;
+
+static PVRSRV_PER_PROCESS_DATA *pp_data = NULL;
+static void *sgx_cookie = NULL;
+
+static void *dev_mem_context = NULL;
+static unsigned long heap_count = 0;
+static IMG_BOOL mem_created, dummy, mem_destroyed;
+
+static unsigned long dev_ids[2] = {0, 0};
+static void *dev_handle[2] = {NULL, NULL};
+static DISPLAY_INFO display_info;
+static DISPLAY_FORMAT *pf = NULL, *pixel_formats = NULL;
+static unsigned long num_dims = 0;
+static DISPLAY_DIMS *dim = NULL, *dimensions = NULL;
+
+static void *sys_buffer = NULL;
+static PVRSRV_KERNEL_MEM_INFO *sysbuf_mem_info = NULL;
+static void *sysbuf_map_info = NULL;
+
+#if 0
+static unsigned long fb_offset = 0;
+#endif
+static unsigned long mapped_fb_addr = 0;
+
+static DISPLAY_SURF_ATTRIBUTES	dst_surf_attrib;
+static DISPLAY_SURF_ATTRIBUTES	src_surf_attrib;
+static void *swap_chain = NULL;
+static unsigned long swap_chain_id = 0;
+
+static void *buffers[NUM_BUFFERS] = {NULL, NULL, NULL};
+static PVRSRV_KERNEL_MEM_INFO *buffers_mem_info[NUM_BUFFERS] = {NULL, NULL, NULL};
+static void *buffers_map_info[NUM_BUFFERS] = {NULL, NULL, NULL};
+
+
+
+
+
+/*!
+ * Initialize PVR services (including the 3DD), and get general info:
+ */
+static void init_pvrsrv(void)
+{
+	int i;
+	unsigned long num = 10;
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (!drm_data->rtn) {
+		/* This is similar to PVRSRVConnect(): */
+		EMGD_DEBUG("Calling PVRSRVPerProcessDataConnect()");
+		err = PVRSRVPerProcessDataConnect(/*Use a dummy PID*/ 200);
+		EMGD_DEBUG("Calling PVRSRVPerProcessData()");
+		pp_data = PVRSRVPerProcessData(/*Use a dummy PID*/ 200);
+		EMGD_DEBUG("  TEST: pp_data = 0x%p", pp_data);
+		if ((err != PVRSRV_OK) || (NULL == pp_data)) {
+			TEST_ERROR("[EMGD] can not find per-process data\n");
+			drm_data->rtn = -1;
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Enumerate the devices and acquire the SGX device: */
+		PVRSRV_DEVICE_IDENTIFIER dev_id_list[10];
+
+		EMGD_DEBUG("Calling PVRSRVEnumerateDevicesKM()");
+		err = PVRSRVEnumerateDevicesKM(&num, dev_id_list);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVEnumerateDevicesKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else {
+			EMGD_DEBUG("  PVRSRVEnumerateDevicesKM() found %ld devices", num);
+			for (i = 0 ; i < num ; i++) {
+				PVRSRV_DEVICE_IDENTIFIER *id = dev_id_list + i;
+				unsigned long cookie = 0;
+				EMGD_DEBUG("    Device %d has type %d, class %d & index %ld", i,
+					id->eDeviceType, id->eDeviceClass, id->ui32DeviceIndex);
+				if (PVRSRV_DEVICE_TYPE_EXT != id->eDeviceType) {
+					/* Call PVRSRVAcquireDeviceDataKM(): */
+					EMGD_DEBUG("Calling PVRSRVAcquireDeviceDataKM()");
+					err = PVRSRVAcquireDeviceDataKM(id->ui32DeviceIndex,
+						PVRSRV_DEVICE_TYPE_UNKNOWN, (void *) &cookie);
+					if (err != PVRSRV_OK) {
+						TEST_ERROR("[EMGD] PVRSRVAcquireDeviceDataKM() "
+							"returned %d\n", err);
+						drm_data->rtn = -1;
+						break;
+					}
+					if (PVRSRV_DEVICE_TYPE_SGX == id->eDeviceType) {
+						EMGD_DEBUG("  Found cookie = 0x%lx", cookie);
+						/* Save this away for later: */
+						sgx_cookie = (void *) cookie;
+					}
+				}
+			}
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Enumerate the display class devices to be able to find the 3DD: */
+		EMGD_DEBUG("Calling PVRSRVEnumerateDCKM()");
+EMGD_DEBUG("dev_ids=0x%p", dev_ids);
+		err = PVRSRVEnumerateDCKM(PVRSRV_DEVICE_CLASS_DISPLAY,
+			&num, dev_ids);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVEnumerateDCKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else {
+			/* Find the 3DD: */
+			EMGD_DEBUG("  PVRSRVEnumerateDCKM() found %ld devices", num);
+			for (i = 0 ; i < num ; i++) {
+				EMGD_DEBUG("    device %d has ID %ld", i, dev_ids[i]);
+			}
+			if (0 == dev_ids[0]) {
+				TEST_ERROR("[EMGD] Did not find 3rd-party display driver ID\n");
+				drm_data->rtn = -1;
+			}
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Call PVRSRVCreateDeviceMemContextKM(): */
+		PVRSRV_HEAP_INFO heap_info[PVRSRV_MAX_CLIENT_HEAPS];
+
+		EMGD_DEBUG("Calling PVRSRVCreateDeviceMemContextKM()");
+		err = PVRSRVCreateDeviceMemContextKM(sgx_cookie, pp_data,
+			&dev_mem_context, &heap_count, heap_info, &mem_created, &dummy);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVCreateDeviceMemContextKM() "
+				"returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* init_pvrsrv() */
+
+
+
+
+
+/*!
+ * IOCTL to allow a ult test ("drmtest_surface.c") to test our 3rd-party
+ * display driver (3DD) without the need for the user-mode IMG service code.
+ */
+static void deinit_pvrsrv(void)
+{
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (NULL != dev_mem_context) {
+		EMGD_DEBUG("Calling PVRSRVDestroyDeviceMemContextKM()");
+		err = PVRSRVDestroyDeviceMemContextKM(sgx_cookie, dev_mem_context,
+			&mem_destroyed);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVDestroyDeviceMemContext() "
+				"returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+	}
+
+	if (NULL != pp_data) {
+		EMGD_DEBUG("Calling PVRSRVPerProcessDataDisconnect()");
+		PVRSRVPerProcessDataDisconnect(/*Use a dummy PID*/ 200);
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* deinit_pvrsrv() */
+
+
+
+
+
+/*!
+ * Initialize PVR services (including the 3DD), and get general info:
+ */
+static void open_device(int which_device)
+{
+	int i;
+	unsigned long num = 10;
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (!drm_data->rtn) {
+		/* Finally, open a connection to the 3DD: */
+		EMGD_DEBUG("Calling PVRSRVOpenDCDeviceKM()");
+		err = PVRSRVOpenDCDeviceKM(pp_data, dev_ids[which_device], sgx_cookie,
+			&dev_handle[which_device]);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVOpenDCDeviceKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+		if (NULL == dev_handle[which_device]) {
+			TEST_ERROR("[EMGD] PVRSRVOpenDCDeviceKM() returned NULL handle\n");
+			drm_data->rtn = -1;
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Get information about the display: */
+		EMGD_DEBUG("Calling PVRSRVGetDCInfoKM()");
+		err = PVRSRVGetDCInfoKM(dev_handle[which_device], &display_info);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVGetDCInfoKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else {
+			EMGD_DEBUG("PVRSRVGetDCInfoKM() returned the following info:");
+			EMGD_DEBUG("  name = \"%s\"", display_info.szDisplayName);
+			EMGD_DEBUG("  MaxSwapChains = %lu", display_info.ui32MaxSwapChains);
+			EMGD_DEBUG("  MaxSwapChainBuffers = %lu",
+				display_info.ui32MaxSwapChainBuffers);
+			EMGD_DEBUG("  MinSwapInterval = %lu",
+				display_info.ui32MinSwapInterval);
+			EMGD_DEBUG("  MaxSwapInterval = %lu",
+				display_info.ui32MaxSwapInterval);
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Get the the number of pixel formats: */
+		EMGD_DEBUG("Calling PVRSRVEnumDCFormatsKM()");
+		err = PVRSRVEnumDCFormatsKM(dev_handle[which_device], &num, NULL);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVEnumDCFormatsKM() returned %d\n",err);
+			drm_data->rtn = -1;
+		}
+		if (0 == num) {
+			TEST_ERROR("[EMGD] PVRSRVEnumDCFormatsKM() says that there "
+				"are %lu pixel formats\n", num);
+			drm_data->rtn = -1;
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Get the pixel formats: */
+		EMGD_DEBUG("Allocating space for %lu pixel formats", num);
+		pixel_formats = OS_ALLOC(sizeof(DISPLAY_FORMAT) * num);
+		EMGD_DEBUG("Calling PVRSRVEnumDCFormatsKM()");
+		err = PVRSRVEnumDCFormatsKM(dev_handle[which_device], &num,
+			pixel_formats);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVEnumDCFormatsKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else {
+			pf = pixel_formats;
+			for (i = 0 ; i < num ; i++) {
+				EMGD_DEBUG("  pixel format %d is %d", i, pf->pixelformat);
+				pf++;
+			}
+			/* Save the first pixel format (the one being used): */
+			pf = pixel_formats;
+		}
+	}
+
+
+	if (!drm_data->rtn) {
+		/* Get the number of display dimensions: */
+		EMGD_DEBUG("Calling PVRSRVEnumDCDimsKM()");
+		err = PVRSRVEnumDCDimsKM(dev_handle[which_device], pf, &num_dims, NULL);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVEnumDCDimsKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+		if (0 == num_dims) {
+			TEST_ERROR("[EMGD] PVRSRVEnumDCDimsKM() says that there "
+				"are %lu dimensions\n", num_dims);
+			drm_data->rtn = -1;
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Get the display dimensions: */
+		EMGD_DEBUG("Allocating space for %lu dimensions", num_dims);
+		dimensions = OS_ALLOC(sizeof(DISPLAY_DIMS) * num_dims);
+		EMGD_DEBUG("Calling PVRSRVEnumDCDimsKM()");
+		err = PVRSRVEnumDCDimsKM(dev_handle[which_device], pf, &num_dims,
+			dimensions);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVEnumDCDimsKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else {
+			dim = dimensions;
+			for (i = 0 ; i < num_dims ; i++) {
+				EMGD_DEBUG("  dimension %d is width = %lu, height = %lu, "
+					"stride = %lu", i, dim->ui32Width, dim->ui32Height,
+					dim->ui32ByteStride);
+				dim++;
+			}
+			/* Save the first dimension (the one being used): */
+			dim = dimensions;
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* open_device() */
+
+
+
+
+
+/*!
+ * IOCTL to allow a ult test ("drmtest_surface.c") to test our 3rd-party
+ * display driver (3DD) without the need for the user-mode IMG service code.
+ */
+static void close_device(int which_device)
+{
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (NULL != sysbuf_mem_info) {
+		EMGD_DEBUG("Calling PVRSRVUnmapDeviceClassMemoryKM()");
+		err = PVRSRVUnmapDeviceClassMemoryKM(sysbuf_mem_info);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVUnmapDeviceClassMemoryKM() "
+				"returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+	}
+	if (NULL != dimensions) {
+		OS_FREE(dimensions);
+	}
+	if (NULL != pixel_formats) {
+		OS_FREE(pixel_formats);
+	}
+	if (NULL != dev_handle[which_device]) {
+		EMGD_DEBUG("Calling PVRSRVCloseDCDeviceKM()");
+		err = PVRSRVCloseDCDeviceKM(dev_handle[which_device], dummy);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVCloseDCDeviceKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+	}
+
+	if (NULL != dev_mem_context) {
+		EMGD_DEBUG("Calling PVRSRVDestroyDeviceMemContextKM()");
+		err = PVRSRVDestroyDeviceMemContextKM(sgx_cookie, dev_mem_context,
+			&mem_destroyed);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVDestroyDeviceMemContext() "
+				"returned %d\n", err);
+			drm_data->rtn = -1;
+		}
+	}
+
+	if (NULL != pp_data) {
+		EMGD_DEBUG("Calling PVRSRVPerProcessDataDisconnect()");
+		PVRSRVPerProcessDataDisconnect(/*Use a dummy PID*/ 200);
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* close_device() */
+
+
+
+
+/*!
+ * Maps the "system buffer" and paints it a solid color.
+ */
+static void map_and_paint_fb(int which_device, unsigned long color)
+{
+	int i;
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Get a handle to the primary surface (a.k.a. "system buffer"): */
+	EMGD_DEBUG("Calling PVRSRVGetDCSystemBufferKM()");
+	err = PVRSRVGetDCSystemBufferKM(dev_handle[which_device], &sys_buffer);
+	if (err != PVRSRV_OK) {
+		TEST_ERROR("[EMGD] PVRSRVGetDCSystemBufferKM() returned %d\n", err);
+		drm_data->rtn = -1;
+	} else if (NULL == sys_buffer) {
+		TEST_ERROR("[EMGD] PVRSRVGetDCSystemBufferKM() returned a "
+			"NULL handle to the system buffer\n");
+		drm_data->rtn = -1;
+	}
+
+	if (!drm_data->rtn) {
+		/* Map the memory of the "system buffer": */
+		EMGD_DEBUG("Calling PVRSRVMapDeviceClassMemoryKM()");
+		err = PVRSRVMapDeviceClassMemoryKM(pp_data, dev_mem_context,
+			sys_buffer, &sysbuf_mem_info, &sysbuf_map_info);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVMapDeviceClassMemoryKM() "
+				"returned %d\n", err);
+			drm_data->rtn = -1;
+		} else if (NULL == sysbuf_mem_info) {
+			TEST_ERROR("[EMGD] PVRSRVMapDeviceClassMemoryKM() returned a "
+				"NULL pointer to the system buffer's PVRSRV_KERNEL_MEM_INFO\n");
+			drm_data->rtn = -1;
+		} else {
+			EMGD_DEBUG("  pvLinAddrKM = 0x%p", sysbuf_mem_info->pvLinAddrKM);
+			EMGD_DEBUG("  sDevVAddr = 0x%lx",sysbuf_mem_info->sDevVAddr.uiAddr);
+			EMGD_DEBUG("  ui32Flags = 0x%lx", sysbuf_mem_info->ui32Flags);
+			EMGD_DEBUG("  ui32AllocSize = %lu = 0x%lx",
+				sysbuf_mem_info->ui32AllocSize, sysbuf_mem_info->ui32AllocSize);
+			EMGD_DEBUG("  sMemBlk.sDevVirtAddr = %lu = 0x%lx",
+				sysbuf_mem_info->sMemBlk.sDevVirtAddr.uiAddr,
+				sysbuf_mem_info->sMemBlk.sDevVirtAddr.uiAddr);
+			EMGD_DEBUG("  sMemBlk.psIntSysPAddr = 0x%p",
+				sysbuf_mem_info->sMemBlk.psIntSysPAddr);
+			EMGD_DEBUG("  pvSysBackupBuffer = 0x%p",
+				sysbuf_mem_info->pvSysBackupBuffer);
+			EMGD_DEBUG("  ui32RefCount = %lu", sysbuf_mem_info->ui32RefCount);
+			EMGD_DEBUG("  bPendingFree = 0x%d", sysbuf_mem_info->bPendingFree);
+			EMGD_DEBUG("  psKernelSyncInfo = 0x%p",
+				sysbuf_mem_info->psKernelSyncInfo);
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Draw the "system buffer" the desired color: */
+		mapped_fb_addr = (unsigned long) (sysbuf_mem_info->pvLinAddrKM);
+		if (0 == mapped_fb_addr) {
+			TEST_ERROR("[EMGD] Failed to map the framebuffer\n");
+			drm_data->rtn = -1;
+		} else {
+			unsigned long *fb = (unsigned long *) (mapped_fb_addr);
+			unsigned int pixels = sysbuf_mem_info->ui32AllocSize / 4;
+			EMGD_DEBUG("  Mapped %luMB @ virtual addr= 0x%lx",
+				sysbuf_mem_info->ui32AllocSize/(1024*1024), mapped_fb_addr);
+			EMGD_DEBUG("     About to write to addr = 0x%p", fb);
+			EMGD_DEBUG("     About to write %d (0x%x) pixels",
+				pixels, pixels);
+			for (i = 0 ; i < pixels ; i++) {
+#if 0
+				if (0 == (i % 1024)) {
+					printk("row %3d: fb=0x%p\n", (i / 1024), fb);
+				}
+#endif
+				*fb++ = color;
+			}
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* map_and_paint_fb() */
+
+
+
+
+/*!
+ * Creates a swap chain, maps the buffers, and paints each a solid color.
+ */
+static void create_swap_chain(int which_device, int which_dimension,
+	unsigned long fb_color,
+	unsigned long bb1_color,
+	unsigned long bb2_color)
+{
+	int i, j;
+	unsigned long num = 10;
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	/**************************************************************************
+	 *
+	 * Create a swap chain, draw green to one back buffer and red to the
+	 * other; and then flip between the frame buffer and the 2 back buffers:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		/* Create a swap chain: */
+		dst_surf_attrib.pixelformat = pf->pixelformat;
+		dst_surf_attrib.sDims = dimensions[which_dimension];
+		src_surf_attrib.pixelformat = pf->pixelformat;
+		src_surf_attrib.sDims = dimensions[which_dimension];
+		EMGD_DEBUG("Calling PVRSRVCreateDCSwapChainKM()");
+		err = PVRSRVCreateDCSwapChainKM(pp_data, dev_handle[which_device], 0,
+			&dst_surf_attrib, &src_surf_attrib, NUM_BUFFERS, 0, &swap_chain,
+			&swap_chain_id);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVCreateDCSwapChainKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else {
+			EMGD_DEBUG("PVRSRVCreateDCSwapChainKM() returned swap_chain=0x%p, "
+				"ID=%lu", swap_chain, swap_chain_id);
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Get the DC Buffers: */
+		num = NUM_BUFFERS;
+		EMGD_DEBUG("Calling PVRSRVGetDCBuffersKM()");
+		err = PVRSRVGetDCBuffersKM(dev_handle[which_device], swap_chain, &num,
+			buffers);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVGetDCBuffersKM() returned %d\n", err);
+			drm_data->rtn = -1;
+		} else if (NUM_BUFFERS != num) {
+			TEST_ERROR("[EMGD] PVRSRVGetDCBuffersKM() changed number of "
+				"buffers to %lu\n", num);
+			drm_data->rtn = -1;
+		} else {
+			EMGD_DEBUG("PVRSRVGetDCBuffersKM() returned buffers: 0x%p, 0x%p and "
+				"0x%p", buffers[0], buffers[1], buffers[2]);
+		}
+	}
+	if (!drm_data->rtn) {
+		/* Map the memory of swap chain buffers: */
+		for (j = 0 ; j < NUM_BUFFERS ; j++) {
+			EMGD_DEBUG("Calling PVRSRVMapDeviceClassMemoryKM(buffers[%d])", j);
+			err = PVRSRVMapDeviceClassMemoryKM(pp_data, dev_mem_context,
+				buffers[j], &buffers_mem_info[j], &buffers_map_info[j]);
+			if (err != PVRSRV_OK) {
+				TEST_ERROR("[EMGD] PVRSRVMapDeviceClassMemoryKM() "
+					"returned %d\n", err);
+				drm_data->rtn = -1;
+			} else if (NULL == buffers_mem_info[j]) {
+				TEST_ERROR("[EMGD] PVRSRVMapDeviceClassMemoryKM() "
+					"returned a NULL pointer for buffer %d's "
+					"PVRSRV_KERNEL_MEM_INFO\n", j);
+				drm_data->rtn = -1;
+			} else {
+				EMGD_DEBUG("  pvLinAddrKM = 0x%p",
+					buffers_mem_info[j]->pvLinAddrKM);
+				EMGD_DEBUG("  sDevVAddr = 0x%lx",
+					buffers_mem_info[j]->sDevVAddr.uiAddr);
+				EMGD_DEBUG("  ui32Flags = 0x%lx",
+					buffers_mem_info[j]->ui32Flags);
+				EMGD_DEBUG("  ui32AllocSize = %lu = 0x%lx",
+					buffers_mem_info[j]->ui32AllocSize,
+					buffers_mem_info[j]->ui32AllocSize);
+				EMGD_DEBUG("  sMemBlk.sDevVirtAddr = %lu = 0x%lx",
+					buffers_mem_info[j]->sMemBlk.sDevVirtAddr.uiAddr,
+					buffers_mem_info[j]->sMemBlk.sDevVirtAddr.uiAddr);
+				EMGD_DEBUG("  sMemBlk.psIntSysPAddr = 0x%p",
+					buffers_mem_info[j]->sMemBlk.psIntSysPAddr);
+				EMGD_DEBUG("  pvSysBackupBuffer = 0x%p",
+					buffers_mem_info[j]->pvSysBackupBuffer);
+				EMGD_DEBUG("  ui32RefCount = %lu",
+					buffers_mem_info[j]->ui32RefCount);
+				EMGD_DEBUG("  bPendingFree = 0x%d",
+					buffers_mem_info[j]->bPendingFree);
+				EMGD_DEBUG("  psKernelSyncInfo = 0x%p",
+					buffers_mem_info[j]->psKernelSyncInfo);
+			}
+		}
+	}
+
+	if (!drm_data->rtn) {
+		/* Draw green in buffer 1 and red in buffer 2: */
+		for (j = 0 ; j < NUM_BUFFERS ; j++) {
+			unsigned long color;
+			unsigned long *fb =
+				(unsigned long *)buffers_mem_info[j]->pvLinAddrKM;
+			unsigned int pixels = buffers_mem_info[j]->ui32AllocSize / 4;
+
+			switch(j) {
+			case 0:
+				color = fb_color;
+				break;
+			case 1:
+				color = bb1_color;
+				break;
+			case 2:
+				color = bb2_color;
+				break;
+			default:
+				color = WHITE;
+				break;
+			}
+
+			EMGD_DEBUG("   Drawing the color 0x%lx to buffer %u", color, j);
+			EMGD_DEBUG("     About to write to addr = 0x%p", fb);
+			EMGD_DEBUG("     About to write %d (0x%x) pixels",
+				pixels, pixels);
+			for (i = 0 ; i < pixels ; i++) {
+#if 0
+				if (0 == (i % 1024)) {
+					printk("row %3d: fb=0x%p\n", (i / 1024), fb);
+				}
+#endif
+				*fb++ = color;
+			}
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* create_swap_chain() */
+
+
+
+
+/*!
+ * Flips each of the swap chain buffers, pausing a specified amount of time in
+ * between, so that the color can be seen.
+ */
+static void flip_swap_chain_buffers(int which_device, int which_dimension,
+	unsigned long sleep_time)
+{
+	int j;
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (!drm_data->rtn) {
+		/* Display each of the buffers: */
+#ifndef TURN_ON_INTERRUPTS
+		igd_context_t *context = priv->context;
+		igd_display_h primary = priv->primary;
+		int ret;
+		unsigned long status;
+#endif /* TURN_ON_INTERRUPTS */
+		IMG_RECT clip_rect = {0, 0, dimensions[which_dimension].ui32Width,
+			dimensions[which_dimension].ui32Height};
+
+		/* Sleep for half a second, to give a chance to see the original system
+		 * buffer:
+		 */
+		OS_SLEEP(sleep_time);
+
+		/* Show each of the swap-chain buffers twice: */
+		for (j = 1 ; j < (NUM_BUFFERS+2) ; j++) {
+#ifndef TURN_ON_INTERRUPTS
+			int bailout;
+#endif /* TURN_ON_INTERRUPTS */
+			int buf = j % NUM_BUFFERS;
+
+			EMGD_DEBUG("Calling PVRSRVSwapToDCBufferKM(%d)", buf);
+			err = PVRSRVSwapToDCBufferKM(dev_handle[which_device], buffers[buf],
+#ifdef TURN_ON_INTERRUPTS
+				/* Note: it is interesting to test with swap interval values of
+				 * 0, 63, and 130, which cause (respectively) no queueing,
+				 * immediate flipping but queueing to count-down the swap
+				 * interval, and the 3DD not getting some flips from PVR
+				 * services before the swap chain is destoryed:
+				 */
+				1, NULL, 1, &clip_rect);
+#else /* TURN_ON_INTERRUPTS */
+				0, NULL, 1, &clip_rect);
+#endif /* TURN_ON_INTERRUPTS */
+			if (err != PVRSRV_OK) {
+				TEST_ERROR("[EMGD] PVRSRVSwapToDCBufferKM() returned %d\n",err);
+				drm_data->rtn = -1;
+			} else {
+				TEST_ERROR("[EMGD] PVRSRVSwapToDCBufferKM() swapped to "
+					"buffer %d\n", buf);
+			}
+
+#ifndef TURN_ON_INTERRUPTS
+			/* Test that we can query whether the flip occured: */
+			for (bailout = 18 ; bailout > 0 ; bailout--) {
+				ret = context->dispatch.query_event(primary,
+					IGD_EVENT_FLIP_PENDING, &status);
+				if (0 == status) {
+					break;
+				} else {
+					OS_SLEEP(1000);
+				}
+			}
+			if (0 == bailout) {
+				TEST_ERROR("Buffer flip didn't complete after 17msec\n");
+				drm_data->rtn = -1;
+			}
+#endif /* TURN_ON_INTERRUPTS */
+
+			/* Sleep for half a second, to give a chance to verify the flip
+			 * occured:
+			 */
+			OS_SLEEP(sleep_time);
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* flip_swap_chain_buffers() */
+
+
+
+
+/*!
+ * Unmaps each swap chain buffer, and then destroys the swap chain.
+ */
+static void destroy_swap_chain(void)
+{
+	int j;
+	PVRSRV_ERROR err;
+
+	EMGD_TRACE_ENTER;
+
+
+	for (j = 0 ; j < NUM_BUFFERS ; j++) {
+		if (NULL != buffers_mem_info[j]) {
+			EMGD_DEBUG("Calling PVRSRVUnmapDeviceClassMemoryKM(buffers[%d])", j);
+			err = PVRSRVUnmapDeviceClassMemoryKM(buffers_mem_info[j]);
+			if (err != PVRSRV_OK) {
+				TEST_ERROR("[EMGD] PVRSRVUnmapDeviceClassMemoryKM(%d) "
+					"returned %d\n", j, err);
+				drm_data->rtn = -1;
+			}
+		}
+	}
+	if (NULL != swap_chain) {
+		EMGD_DEBUG("Calling PVRSRVDestroyDCSwapChainKM()");
+		err = PVRSRVDestroyDCSwapChainKM(swap_chain);
+		if (err != PVRSRV_OK) {
+			TEST_ERROR("[EMGD] PVRSRVDestroyDCSwapChainKM() returned %d\n",err);
+			drm_data->rtn = -1;
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* destroy_swap_chain() */
+
+
+
+
+/*!
+ * Change the mode, via EMGD HAL functions.
+ */
+static void change_mode(struct drm_device *dev, int which_dim)
+{
+	EMGD_TRACE_ENTER;
+
+
+	/**************************************************************************
+	 *
+	 * Do a mode change (with a new background color of white), external to the
+	 * 3DD (which will invalidate the swap chain) and see that the flips are no
+	 * longer visible:
+	 *
+	 **************************************************************************/
+	if ((!drm_data->rtn) && (num_dims != 0)) {
+		igd_context_t *context = priv->context;
+		unsigned long emgd_pf = IGD_PF_ARGB32;
+		igd_display_info_t *mode_list = NULL;
+		igd_display_info_t *mode = NULL;
+		igd_display_info_t *desired_mode = NULL;
+		int mode_flags = IGD_QUERY_LIVE_MODES;
+		unsigned long byte_stride;
+		igd_framebuffer_info_t primary_fb_info;
+		igd_framebuffer_info_t secondary_fb_info;
+		igd_display_h primary;
+		igd_display_h secondary;
+		int err = 0;
+
+		EMGD_DEBUG("Will attempt to do a mode change...");
+		EMGD_DEBUG("  Target width=%lu, height=%lu",
+			dimensions[which_dim].ui32Width,
+			dimensions[which_dim].ui32Height);
+
+		EMGD_DEBUG("  Calling query_mode_list()");
+		err = context->dispatch.query_mode_list(context, priv->dc,
+			&mode_list, mode_flags);
+		if (err) {
+			TEST_ERROR("The query_mode_list() function returned %d.", err);
+			drm_data->rtn = -1;
+		}
+		if (!drm_data->rtn) {
+			EMGD_DEBUG("  Comparing mode list with target width and height");
+			mode = mode_list;
+			while (mode && (mode->width != IGD_TIMING_TABLE_END)) {
+				byte_stride =  IGD_PF_PIXEL_BYTES(emgd_pf, mode->width);
+				EMGD_DEBUG("    Found a mode w/ width=%d, height=%d, refresh=%d;",
+					mode->width, mode->height, mode->refresh);
+				if ((mode->width == dimensions[which_dim].ui32Width) &&
+					(mode->height == dimensions[which_dim].ui32Height)) {
+					EMGD_DEBUG("      This mode is a match!");
+					desired_mode = mode;
+					break;
+				}
+				mode++;
+			}
+			if (NULL == desired_mode) {
+				TEST_ERROR("  No mode matching the desired width (%lu), "
+					"height (%lu) was found.", dimensions[which_dim].ui32Width,
+					dimensions[which_dim].ui32Height);
+				drm_data->rtn = -1;
+			} else {
+				/* Must set this in order to get the timings setup: */
+				desired_mode->flags |= IGD_DISPLAY_ENABLE;
+			}
+		}
+		if (!drm_data->rtn) {
+			/* Make the mode change by calling alter_displays(): */
+			primary_fb_info.width = desired_mode->width;
+			primary_fb_info.height = desired_mode->height;
+			primary_fb_info.pixel_format = emgd_pf;
+			primary_fb_info.flags = 0;
+			primary_fb_info.allocated = 0;
+			memcpy(&secondary_fb_info, &primary_fb_info,
+				sizeof(igd_framebuffer_info_t));
+
+			EMGD_DEBUG("Calling alter_displays()");
+			err = context->dispatch.alter_displays(context,
+				&primary, desired_mode, &primary_fb_info,
+				&secondary, desired_mode, &secondary_fb_info, priv->dc, 0);
+			if (err) {
+				TEST_ERROR("The alter_displays() function returned %d.", err);
+				drm_data->rtn = -1;
+			}
+		}
+		if (!drm_data->rtn) {
+			/* Update the private copy, like emgd_alter_displays() would do: */
+			priv->primary = primary;
+			priv->secondary = secondary;
+			priv->primary_port_number = (priv->dc & 0xf0) >> 4;
+			priv->secondary_port_number = (priv->dc & 0xf00000) >> 20;
+
+			/* Re-initialize the display values: */
+			err = priv->reinit_3dd(dev);
+			if (err != 0) {
+				TEST_ERROR("The reinit_3dd() function returned %d.", err);
+				drm_data->rtn = -1;
+			} else {
+				EMGD_DEBUG("The MODE CHANGE was SUCCESSFUL");
+			}
+		}
+	}
+
+
+
+	EMGD_TRACE_EXIT;
+
+} /* change_mode() */
+
+
+
+
+
+
+
+
+
+/*!
+ * Test a single/clone DC with lots of buffer flipping and mode changes.
+ */
+static void emgd_test_pvrsrv_single_dc(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	/**************************************************************************
+	 *
+	 * Initialize PVR services (including the 3DD):
+	 *
+	 **************************************************************************/
+	init_pvrsrv();
+	if (!drm_data->rtn) {
+		open_device(DEVICE1);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Map the frame buffer and paint it blue:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		map_and_paint_fb(DEVICE1, BLUE);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Create a swap chain, draw red to one back buffer and green to the
+	 * other; and then flip between the frame buffer and the 2 back buffers:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE1, 0, BLUE, RED, GREEN);
+	}
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE1, 0, 500*1000);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Do a mode change, external to the 3DD (which will invalidate the swap
+	 * chain) and see that the flips are no longer visible:
+	 *
+	 **************************************************************************/
+	if ((!drm_data->rtn) && (num_dims != 0)) {
+		change_mode(dev, 1);
+	}
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE1, 0, 500*1000);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Destroy the old swap chain, and create a new one that has the new
+	 * dimensions; then draw cyan to the frame buffer, yellow to one back
+	 * buffer and magenta to the other; and then flip between the frame buffer
+	 * and the 2 back buffers:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		destroy_swap_chain();
+	}
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE1, 1, CYAN, YELLOW, MAGENTA);
+	}
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE1, 1, 500*1000);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Destroy the old swap chain, and create a new one that has different
+	 * dimensions (the original dimensions), which will cause the 3DD to do a
+	 * mode change; then draw blue to the frame buffer, red to one back
+	 * buffer and green to the other; and then flip between the frame buffer
+	 * and the 2 back buffers:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		destroy_swap_chain();
+	}
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE1, 0, BLUE, RED, GREEN);
+	}
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE1, 0, 500*1000);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Clean up to avoid an Oops and/or memory leak:
+	 *
+	 **************************************************************************/
+	destroy_swap_chain();
+
+	close_device(DEVICE1);
+	deinit_pvrsrv();
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_single_dc() */
+
+
+
+
+/*!
+ * Test a DIH/extended DC by opening/mapping the first device and drawing blue
+ * on it, and then by creating a swap chain, and flipping between all buffers.
+ */
+static void emgd_test_pvrsrv_dih_open_dev1(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	/**************************************************************************
+	 *
+	 * Initialize PVR services (including the 3DD):
+	 *
+	 **************************************************************************/
+	init_pvrsrv();
+	if (!drm_data->rtn) {
+		open_device(DEVICE1);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Map the frame buffer and paint it blue:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		map_and_paint_fb(DEVICE1, BLUE);
+	}
+	OS_SLEEP(3*1000*1000);
+
+
+	/**************************************************************************
+	 *
+	 * Create a swap chain and flip between all buffers:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE1, 0, BLUE, RED, GREEN);
+	}
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE1, 0, 1000*1000);
+	}
+	if (!drm_data->rtn) {
+		destroy_swap_chain();
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_open_dev1() */
+
+
+
+
+/*!
+ * Continue testing a DIH/extended DC by having EMGD do a mode change on the
+ * first device.
+ */
+static void emgd_test_pvrsrv_dih_change1_dev1(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	if ((!drm_data->rtn) && (num_dims != 0)) {
+		change_mode(dev, 1);
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_change1_dev1() */
+
+
+
+
+/*!
+ * Continue testing a DIH/extended DC by having the 3DD (via PVRSRV) do a mode
+ * change, via the creation of a swap chain, on the first device, and then flip
+ * between all buffers.
+ */
+static void emgd_test_pvrsrv_dih_change2_dev1(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE1, 0, BLUE, RED, GREEN);
+	}
+	OS_SLEEP(3*1000*1000);
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE1, 0, 500*1000);
+	}
+	if (!drm_data->rtn) {
+		destroy_swap_chain();
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_change2_dev1() */
+
+
+
+
+/*!
+ * Close the first device and close PVR services to avoid an Oops and/or
+ * memory leak:
+ */
+static void emgd_test_pvrsrv_dih_close_dev1(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	close_device(DEVICE1);
+	deinit_pvrsrv();
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_close_dev1() */
+
+
+
+
+/*!
+ * Continue testing a DIH/extended DC by opening/mapping the second device and
+ * drawing red on it.
+ */
+static void emgd_test_pvrsrv_dih_open_dev2(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	/**************************************************************************
+	 *
+	 * Initialize PVR services (including the 3DD):
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		init_pvrsrv();
+	}
+	if (!drm_data->rtn) {
+		open_device(DEVICE2);
+	}
+
+
+	/**************************************************************************
+	 *
+	 * Map the frame buffer and paint it cyan:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		map_and_paint_fb(DEVICE2, CYAN);
+	}
+	OS_SLEEP(3*1000*1000);
+
+
+	/**************************************************************************
+	 *
+	 * Create a swap chain and flip between all buffers:
+	 *
+	 **************************************************************************/
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE2, 0, CYAN, YELLOW, MAGENTA);
+	}
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE2, 0, 1000*1000);
+	}
+	if (!drm_data->rtn) {
+		destroy_swap_chain();
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_open_dev2() */
+
+
+
+
+/*!
+ * Continue testing a DIH/extended DC by having EMGD do a mode change on the
+ * second device.
+ */
+static void emgd_test_pvrsrv_dih_change1_dev2(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	if ((!drm_data->rtn) && (num_dims != 0)) {
+		change_mode(dev, 1);
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_change1_dev2() */
+
+
+
+
+/*!
+ * Continue testing a DIH/extended DC by having the 3DD (via PVRSRV) do a mode
+ * change, via the creation of a swap chain, on the second device, and then flip
+ * between all buffers.
+ */
+static void emgd_test_pvrsrv_dih_change2_dev2(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	if (!drm_data->rtn) {
+		create_swap_chain(DEVICE2, 0, CYAN, YELLOW, MAGENTA);
+	}
+	OS_SLEEP(3*1000*1000);
+	if (!drm_data->rtn) {
+		flip_swap_chain_buffers(DEVICE2, 0, 500*1000);
+	}
+	if (!drm_data->rtn) {
+		destroy_swap_chain();
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_change2_dev2() */
+
+
+
+
+/*!
+ * Close the second device and close PVR services to avoid an Oops and/or
+ * memory leak:
+ */
+static void emgd_test_pvrsrv_dih_close_dev2(struct drm_device *dev)
+{
+	EMGD_TRACE_ENTER;
+
+
+	close_device(DEVICE2);
+	deinit_pvrsrv();
+
+
+	EMGD_TRACE_EXIT;
+
+} /* emgd_test_pvrsrv_dih_close_dev2() */
+
+
+
+
+
+
+
+
+
+
+/*!
+ * IOCTL to allow a ult test ("drmtest_surface.c") to test our 3rd-party
+ * display driver (3DD) without the need for the user-mode IMG service code.
+ */
+int emgd_test_pvrsrv(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv)
+{
+	EMGD_TRACE_ENTER;
+
+
+	drm_data = arg;
+	priv = dev->dev_private;
+	drm_data->rtn = 0;
+
+
+	/**************************************************************************
+	 *
+	 * Depending on the opcode, call the appropriate function that does the
+	 * test:
+	 *
+	 **************************************************************************/
+	switch (drm_data->opcode) {
+	case SINGLE_DC_SWAPING:
+		emgd_test_pvrsrv_single_dc(dev);
+		break;
+	case DIH_DC_DRAW_OPEN_DEV1:
+		emgd_test_pvrsrv_dih_open_dev1(dev);
+		break;
+	case DIH_DC_MODE_CHANGE1_DEV1:
+		emgd_test_pvrsrv_dih_change1_dev1(dev);
+		break;
+	case DIH_DC_MODE_CHANGE2_DEV1:
+		emgd_test_pvrsrv_dih_change2_dev1(dev);
+		break;
+	case DIH_DC_CLOSE_DEV1:
+		emgd_test_pvrsrv_dih_close_dev1(dev);
+		break;
+	case DIH_DC_DRAW_OPEN_DEV2:
+		emgd_test_pvrsrv_dih_open_dev2(dev);
+		break;
+	case DIH_DC_MODE_CHANGE1_DEV2:
+		emgd_test_pvrsrv_dih_change1_dev2(dev);
+		break;
+	case DIH_DC_MODE_CHANGE2_DEV2:
+		emgd_test_pvrsrv_dih_change2_dev2(dev);
+		break;
+	case DIH_DC_CLOSE_DEV2:
+		emgd_test_pvrsrv_dih_close_dev2(dev);
+		break;
+	}
+
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	return 0;
+} /* emgd_test_pvrsrv() */
diff --git a/drivers/gpu/drm/emgd/emgd/drm/image_data.h b/drivers/gpu/drm/emgd/emgd/drm/image_data.h
new file mode 100755
index 0000000..b48fc0c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/image_data.h
@@ -0,0 +1,33 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: bitmap_data.h
+ * $Revision: 1.4 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+#define DECODE_PNG
+static unsigned char image_data[] = {
+};
diff --git a/drivers/gpu/drm/emgd/emgd/drm/splash_screen.c b/drivers/gpu/drm/emgd/emgd/drm/splash_screen.c
new file mode 100755
index 0000000..f429894
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/splash_screen.c
@@ -0,0 +1,2221 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: splash_screen.c
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the Intel Embedded Graphics EFI Driver Splash Screen implementation
+ *  file. This code shows a splash screen with a customizable icon.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.oal
+
+#include <drm/drmP.h>
+#include <drm/drm.h>
+#include <memory.h>
+#include "sched.h"
+#include "image_data.h"
+#include "splash_screen.h"
+#include "io.h"
+#include "igd_debug.h"
+
+
+/**
+ * Function to display a splash screen to the user. The splash screen must be
+ * accessible to the kernel mode driver as it has to be displayed immediately
+ * after setting the mode (if requested by the user through config options).
+ *
+ * @param ss_data (IN) a non null pointer to splash screen information like
+ * width, height etc.
+ */
+void display_splash_screen(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	emgd_drm_splash_screen_t *ss_data)
+{
+	if (image_data[0] == 0x89) {
+		display_png_splash_screen(fb_info, fb, ss_data);
+	} else {
+		display_bmp_splash_screen(fb_info, fb, ss_data);
+	}
+}
+
+/*
+ * This is the function to display the bmp splash screen.
+ *
+ * @param ss_data (IN) a non null pointer to splash screen information like
+ * width, height etc.
+ */
+void display_bmp_splash_screen(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	emgd_drm_splash_screen_t *ss_data)
+{
+	unsigned char *fb_addr, *icon_temp;
+	unsigned long *fb_addr_long, icon_long;
+	unsigned long bitmap_pitch;
+	short x, y;
+	unsigned long init_x_shift, init_y_shift;
+	unsigned long row, col, fb_index;
+	unsigned long bytecount, temp;
+	unsigned long limit_width, limit_height;
+
+	EMGD_TRACE_ENTER;
+
+	x = (short) ss_data->x;
+	y = (short) ss_data->y;
+
+	if(x < 0) {
+		init_x_shift = (fb_info->width + x) * 4;
+		init_y_shift = (fb_info->height + y) * fb_info->screen_pitch;
+	} else {
+		init_x_shift = x * 4;
+		init_y_shift = y * fb_info->screen_pitch;
+	}
+
+	fb_addr = fb + init_y_shift;
+	bytecount = (unsigned long) image_data[1];
+	bitmap_pitch = ss_data->width * bytecount;
+
+	limit_width = ss_data->width;
+	limit_height = ss_data->height;
+
+	if (x+ss_data->width > fb_info->width) {
+		limit_width = fb_info->width - x;
+	}
+	if (y+ss_data->height > fb_info->height) {
+		limit_height = fb_info->height - y;
+	}
+
+	for(row = 0; row < limit_height; row++) {
+		fb_addr_long =
+			(unsigned long *) &fb_addr[fb_info->screen_pitch * row +
+			init_x_shift];
+		/*
+		 * We are adding 3 bytes here, the first byte indicates BMP or PNG,
+		 * the second byte is the bytecount
+		 * and the third byte is the palette count
+		 */
+		icon_temp = &image_data[3 + (row * bitmap_pitch)];
+		fb_index = 0;
+
+		for(col = 0; col < limit_width; col++) {
+
+			icon_long = *((unsigned long *) &icon_temp[col*bytecount]);
+			switch(bytecount) {
+				case 1:
+					/* 8 bit */
+					temp = (icon_long & 0xFF);
+					icon_long = ((temp & 0xE0)<<16) | ((temp & 0x1C)<<11) |
+						((temp & 0x3)<<6);
+					break;
+				case 2:
+					/* 16 bit */
+					temp = (icon_long & 0xFFFF);
+					icon_long = CONV_16_TO_32_BIT(temp);
+					break;
+			}
+			/*
+			 * For 24 bit we don't really have to do anything as it is
+			 * already in RGB 888 format
+			 */
+			fb_addr_long[fb_index++] = icon_long & 0x00FFFFFF;
+		}
+	}
+	EMGD_TRACE_EXIT;
+}
+
+
+/*
+ * This is the function to display the png splash screen.
+ *
+ * @param ss_data (IN) a non null pointer to splash screen information like
+ * width, height etc.
+ */
+void display_png_splash_screen(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	emgd_drm_splash_screen_t *ss_data)
+{
+	unsigned long image_size;
+	unsigned long i;
+	unsigned long chunk_size;
+	unsigned long chunk_type;
+	unsigned long iter = PNG_HEADER_SIZE;
+	png_header image_header;
+	png_frame *frames = NULL;
+	png_frame *default_image = NULL;
+	unsigned long gama = 0;
+	unsigned long palette_size = 0;
+	unsigned char *input_data = NULL;
+	unsigned long input_iter = 0;
+	unsigned long apng_num_frames = 0;
+	unsigned long apng_num_plays = 0;
+	unsigned long sequence_number = 0;
+	unsigned long cur_seq_num = 0;
+	unsigned long orig_x = 0;
+	unsigned long orig_y = 0;
+	unsigned long apng_file = 0;
+	unsigned long cur_frame = 0;
+	unsigned char trans_p = 0;
+	unsigned long prev_dispose_op = 0;
+	unsigned short delay_num, delay_den;
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Just incase there is no background and we have alpha values, lets
+	 * use the background specified in ss_data.
+	 */
+	image_header.background = 0xFF000000 | ss_data->bg_color;
+	image_header.background_r = (image_header.background >> 16) & 0xFF;
+	image_header.background_g = (image_header.background >> 8) & 0xFF;
+	image_header.background_b = image_header.background & 0xFF;
+
+	image_size = sizeof(image_data)/sizeof(unsigned char);
+	input_data = (unsigned char *)vmalloc(sizeof(image_data));
+	if (!input_data) {
+		EMGD_ERROR("Out of memory");
+		return;
+	}
+	OS_MEMSET(input_data, 0, sizeof(image_data));
+
+	orig_x = (short) ss_data->x;
+	orig_y = (short) ss_data->y;
+
+	/*
+	 * Lets get the information for the first chunk, which should be
+	 * the header chunk: IHDR.
+	 */
+	read_int_from_stream(image_data, &iter, &chunk_size);
+	read_int_from_stream(image_data, &iter, &chunk_type);
+
+	/*
+	 * Initialize image_header
+	 */
+	image_header.width = 0;
+	image_header.height = 0;
+	image_header.bit_depth = 0;
+	image_header.colour_type = 0;
+	image_header.compression_method = 0;
+	image_header.filter_method = 0;
+	image_header.interlace_method = 0;
+	image_header.bpp = 0;
+	image_header.bytes_pp = 0;
+	image_header.bytes_pl = 0;
+
+	/* Loop through the PNG chunks */
+	while (iter <= image_size) {
+		switch (chunk_type) {
+		case CHUNK_IHDR:
+			read_int_from_stream(image_data, &iter, &image_header.width);
+			read_int_from_stream(image_data, &iter, &image_header.height);
+			image_header.bit_depth = (unsigned char)image_data[iter++];
+			image_header.colour_type = (unsigned char)image_data[iter++];
+			image_header.compression_method = (unsigned char)image_data[iter++];
+			image_header.filter_method = (unsigned char)image_data[iter++];
+			image_header.interlace_method = (unsigned char)image_data[iter++];
+			image_header.x_offset = orig_x;
+			image_header.y_offset = orig_y;
+
+			/* store bits per pixel based on PNG spec */
+			switch (image_header.colour_type) {
+			case COLOR_GREY:
+				image_header.bpp = image_header.bit_depth;
+				break;
+			case COLOR_TRUE:
+				image_header.bpp = 3 * image_header.bit_depth;
+				break;
+			case COLOR_INDEXED:
+				image_header.bpp = image_header.bit_depth;
+				break;
+			case COLOR_GREY_ALPHA:
+				image_header.bpp = 2 * image_header.bit_depth;
+				break;
+			case COLOR_TRUE_ALPHA:
+				image_header.bpp = 4 * image_header.bit_depth;
+				break;
+			}
+			/*
+			 * Adding 7 to the bits per pixel before we divide by 8
+			 * gives us the ceiling of bytes per pixel instead of the floor.
+			 */
+			image_header.bytes_pp = (image_header.bpp + 7) / 8;
+			image_header.bytes_pl =
+				((image_header.width * image_header.bpp) + 7) / 8;
+			break;
+
+		case CHUNK_TRNS:
+			image_header.using_transparency = 1;
+			switch (image_header.colour_type) {
+				case COLOR_GREY:
+					read_short_from_stream(image_data, &iter,
+						&image_header.transparency_r);
+					break;
+				case COLOR_TRUE:
+					read_short_from_stream(image_data, &iter,
+						&image_header.transparency_r);
+					read_short_from_stream(image_data, &iter,
+						&image_header.transparency_g);
+					read_short_from_stream(image_data, &iter,
+						&image_header.transparency_b);
+					break;
+				case COLOR_INDEXED:
+					if (image_header.image_palette) {
+						if (chunk_size > palette_size) {
+							EMGD_ERROR("Palette size is smaller than "
+								"transparency values for the palette");
+						}
+						for (i=0; i<chunk_size; i++) {
+							read_char_from_stream(image_data, &iter, &trans_p);
+							image_header.image_palette[i] &= 0xFFFFFF |
+								((trans_p << 28) | (trans_p << 24));
+						}
+					} else {
+						EMGD_ERROR("Palette has not been initialized yet");
+					}
+					break;
+			}
+			break;
+
+		case CHUNK_BKGD:
+			/* Truecolor */
+			if (image_header.colour_type == COLOR_TRUE_ALPHA ||
+				image_header.colour_type == COLOR_TRUE) {
+
+				switch (image_header.bit_depth) {
+				case 16:
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_g);
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_b);
+					iter++;
+					break;
+				case 8:
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_g);
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_b);
+					break;
+				}
+			}
+
+			/* Grayscale */
+			if (image_header.colour_type == COLOR_GREY_ALPHA ||
+				image_header.colour_type == COLOR_GREY) {
+
+				switch (image_header.bit_depth) {
+				case 16:
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					iter++;
+					break;
+				case 8:
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					break;
+				case 4:
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					image_header.background_r =
+						((image_header.background_r >> 4) << 4) |
+						(image_header.background_r >> 4);
+					break;
+				case 2:
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					image_header.background_r =
+						((image_header.background_r >> 2) << 6) |
+						((image_header.background_r >> 2) << 4) |
+						((image_header.background_r >> 2) << 2) |
+						(image_header.background_r >> 2);
+					break;
+				case 1:
+					iter++;
+					read_char_from_stream(image_data, &iter,
+						&image_header.background_r);
+					image_header.background_r =
+						(image_header.background_r << 7) |
+						(image_header.background_r << 6) |
+						(image_header.background_r << 5) |
+						(image_header.background_r << 4) |
+						(image_header.background_r << 3) |
+						(image_header.background_r << 2) |
+						(image_header.background_r << 1) |
+						image_header.background_r;
+					break;
+				}
+				image_header.background_g = image_header.background_r;
+				image_header.background_b = image_header.background_r;
+			}
+
+			image_header.background = 0xFF000000 |
+				image_header.background_r<<16 |
+				image_header.background_g<<8 |
+				image_header.background_b;
+			break;
+
+		case CHUNK_GAMA:
+			read_int_from_stream(image_data, &iter, &gama);
+			break;
+
+		case CHUNK_PLTE:
+			palette_size = chunk_size/3;
+			image_header.image_palette =
+				vmalloc(sizeof(unsigned long) * palette_size);
+			if (!image_header.image_palette) {
+				EMGD_ERROR("Out of memory");
+				return;
+			}
+			OS_MEMSET(image_header.image_palette, 0,
+				sizeof(unsigned long) * palette_size);
+
+			for (i=0; i<palette_size; i++) {
+				image_header.image_palette[i] = (
+					0xFF000000 |
+					((unsigned char)image_data[iter] << 16) |
+					((unsigned char)image_data[iter+1] << 8) |
+					(unsigned char)image_data[iter+2]);
+				iter += 3;
+			}
+			break;
+
+		case CHUNK_IDAT:
+			if (!default_image) {
+				default_image = vmalloc(sizeof(png_header));
+				if (!default_image) {
+					EMGD_ERROR("Out of memory");
+					return;
+				}
+				OS_MEMSET(default_image, 0, sizeof(png_header));
+				default_image->width = image_header.width;
+				default_image->height = image_header.height;
+				default_image->x_offset = 0;
+				default_image->y_offset = 0;
+				default_image->bytes_pp = image_header.bytes_pp;
+				default_image->bytes_pl = image_header.bytes_pl;
+				default_image->blend_op = APNG_BLEND_OP_SOURCE;
+				default_image->dispose_op = APNG_DISPOSE_OP_NONE;
+			}
+			for (i=0; i<chunk_size; i++) {
+				input_data[input_iter++] = image_data[iter++];
+			}
+			break;
+
+		case CHUNK_ACTL:
+			apng_file = 1;
+			read_int_from_stream(image_data, &iter, &apng_num_frames);
+			read_int_from_stream(image_data, &iter, &apng_num_plays);
+			frames = vmalloc(apng_num_frames * sizeof(png_frame));
+			if (!frames) {
+				EMGD_ERROR("Out of memory.");
+				return;
+			}
+			OS_MEMSET(frames, 0, apng_num_frames * sizeof(png_frame));
+			break;
+
+		case CHUNK_FCTL:
+			if (cur_seq_num > 0) {
+				decode_png_data(&image_header,input_data,&frames[cur_frame-1]);
+			} else {
+				if (default_image) {
+					decode_png_data(&image_header, input_data, default_image);
+				}
+			}
+
+			/* Should we wipe out the input_data buffer? */
+			input_iter = 0;
+
+			read_int_from_stream(image_data, &iter, &sequence_number);
+			read_int_from_stream(image_data, &iter, &frames[cur_frame].width);
+			read_int_from_stream(image_data, &iter, &frames[cur_frame].height);
+			read_int_from_stream(image_data, &iter, &frames[cur_frame].x_offset);
+			read_int_from_stream(image_data, &iter, &frames[cur_frame].y_offset);
+			read_short_from_stream(image_data, &iter, &delay_num);
+			read_short_from_stream(image_data, &iter, &delay_den);
+			read_char_from_stream(image_data, &iter, &frames[cur_frame].dispose_op);
+			read_char_from_stream(image_data, &iter, &frames[cur_frame].blend_op);
+
+			if (delay_num) {
+				if (!delay_den) {
+					frames[cur_frame].delay = 10000 * (unsigned long)delay_num;
+				} else {
+					frames[cur_frame].delay = (1000000 *
+						(unsigned long)delay_num) / (unsigned long)delay_den;
+				}
+			}
+
+			/*
+			 * Adding 7 to the bits per pixel before we divide by 8
+			 * gives us the ceiling of bytes per pixel instead of the floor.
+			 */
+			frames[cur_frame].bytes_pp = (image_header.bpp + 7) / 8;
+			frames[cur_frame].bytes_pl =
+				((frames[cur_frame].width * image_header.bpp) + 7) / 8;
+
+			cur_frame++;
+
+			if (sequence_number != cur_seq_num++) {
+				EMGD_ERROR("Sequence numbers do not match!");
+				return;
+			}
+			break;
+
+		case CHUNK_FDAT:
+			read_int_from_stream(image_data, &iter, &sequence_number);
+			if (sequence_number != cur_seq_num++) {
+				EMGD_ERROR("Sequence numbers do not match!");
+				return;
+			}
+			for (i=4; i<chunk_size; i++) {
+				input_data[input_iter++] = image_data[iter++];
+			}
+			break;
+
+		case CHUNK_IEND:
+			if (!frames && default_image) {
+				decode_png_data(&image_header, input_data,
+					default_image);
+			} else {
+				decode_png_data(&image_header, input_data,
+					&frames[cur_frame-1]);
+			}
+			break;
+
+		default:
+			iter += chunk_size;
+			break;
+		}
+
+		/*
+		 * Skip over the CRC for now, do we actually want to spend
+		 * time checking this? Per the spec, unless there is an a corrupted
+		 * header, the only possible outcome is a corrupted image.  It's
+		 * either that, or we don't display any image, maybe in this case we
+		 * should display a blue screen. :)
+		 */
+		iter += 4;
+
+		/* Get the next chunk */
+		read_int_from_stream(image_data, &iter, &chunk_size);
+		read_int_from_stream(image_data, &iter, &chunk_type);
+	}
+
+	if (input_data) {
+		vfree(input_data);
+		input_data = NULL;
+	}
+
+	if (apng_file && !apng_num_plays) {
+		apng_num_plays = 20;
+	}
+	if (apng_num_frames > 0) {
+		frames[apng_num_frames-1].dispose_op = APNG_DISPOSE_OP_NONE;
+		if (frames[0].dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
+			frames[0].dispose_op = APNG_DISPOSE_OP_BACKGROUND;
+		}
+	}
+	for (i=0; i<apng_num_plays; i++) {
+		for (cur_frame=0; cur_frame<apng_num_frames; cur_frame++) {
+			if (cur_frame > 0) {
+				prev_dispose_op = frames[cur_frame-1].dispose_op;
+			}
+			display_png_frame(fb_info, fb, image_header, &frames[cur_frame],
+				prev_dispose_op);
+		}
+	}
+	if (!apng_file) {
+		display_png_frame(fb_info, fb, image_header, default_image,
+			APNG_DISPOSE_OP_NONE);
+	}
+
+	for (cur_frame=0; cur_frame<apng_num_frames; cur_frame++) {
+		if (frames[cur_frame].output) {
+			vfree(frames[cur_frame].output);
+			frames[cur_frame].output = NULL;
+		}
+	}
+	if (frames) {
+		vfree(frames);
+		frames = NULL;
+	}
+
+	if (default_image->output) {
+		vfree(default_image->output);
+		default_image->output = NULL;
+	}
+
+	if (default_image) {
+		vfree(default_image);
+		default_image = NULL;
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+void display_png_frame(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	png_header image_header,
+	png_frame *frame,
+	unsigned long prev_dispose_op)
+{
+	unsigned char *fb_addr = NULL;
+	unsigned long *fb_addr_long = NULL;
+	unsigned long init_x_shift, init_y_shift, row, col, j;
+	unsigned char image_alpha;
+	unsigned char background_alpha;
+	unsigned long *previous = NULL;
+
+	if (frame->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
+		previous = vmalloc(frame->width*frame->height*sizeof(unsigned long));
+		if (!previous) {
+			EMGD_ERROR("Out of memory.");
+			return;
+		}
+	}
+
+	/* Lets position our image at the supplied offsets on the screen */
+	/* TODO: Need to account for negative offset */
+	init_x_shift = (image_header.x_offset + frame->x_offset) *
+		sizeof(unsigned long);
+	init_y_shift = (image_header.y_offset + frame->y_offset) *
+		fb_info->screen_pitch;
+	fb_addr = fb + init_y_shift;
+	fb_addr_long = (unsigned long *) &fb_addr[init_x_shift];
+
+	row = 0;
+	j = 0;
+
+	switch (frame->blend_op) {
+
+	/* Blending against our background color */
+	case APNG_BLEND_OP_SOURCE:
+		while (row < frame->height){
+			col = 0;
+			fb_addr_long = (unsigned long *)
+				&fb_addr[fb_info->screen_pitch * row + init_x_shift];
+
+			if (frame->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
+				/* Save the previous since we need to dispose to it */
+				OS_MEMCPY((void *)&previous[j], (void *)fb_addr_long,
+					frame->width * sizeof(unsigned long));
+			}
+
+			/* Put together the pixel and output to framebuffer */
+			while (col < frame->width) {
+				image_alpha = frame->output[j]>>24;
+				if (image_alpha){
+					if (image_alpha != 0xFF){
+						background_alpha = (0xFF - image_alpha) & 0xFF;
+
+						frame->output[j] = 0xFF000000 |
+							((((((frame->output[j]&0xFF0000)>>16) *
+								image_alpha)/0xFF) +
+							((((image_header.background&0xFF0000)>>16) *
+								background_alpha)/0xFF))<<16) |
+							((((((frame->output[j]&0x00FF00)>>8) *
+								image_alpha)/0xFF) +
+							((((image_header.background&0x00FF00)>>8) *
+								background_alpha)/0xFF))<<8) |
+							((((((frame->output[j]&0x0000FF)) *
+								image_alpha)/0xFF) +
+							((((image_header.background&0x0000FF)) *
+								background_alpha)/0xFF)));
+					}
+				} else {
+					frame->output[j] = image_header.background;
+				}
+				fb_addr_long[col] = frame->output[j];
+				col++;
+				j++;
+			}
+			row++;
+		}
+		break;
+
+	/* Blending against previous frame */
+	case APNG_BLEND_OP_OVER:
+		while (row < frame->height){
+			col = 0;
+			fb_addr_long = (unsigned long *)
+				&fb_addr[fb_info->screen_pitch * row + init_x_shift];
+
+			if (frame->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
+				/* Save the previous isince we need to dispose to it */
+				OS_MEMCPY((void *)&previous[j], (void *)fb_addr_long,
+						frame->width * sizeof(unsigned long));
+			}
+
+			/* Blend the pixel with existing framebuffer pixel */
+			while (col < frame->width) {
+				image_alpha = frame->output[j]>>24;
+
+				if (image_alpha){
+					if (image_alpha != 0xFF){
+						background_alpha = (0xFF - image_alpha) & 0xFF;
+
+						frame->output[j] = 0xFF000000 |
+							((((((frame->output[j]&0xFF0000)>>16) *
+								image_alpha)/0xFF) +
+							  ((((fb_addr_long[col]&0xFF0000)>>16) *
+								background_alpha)/0xFF))<<16) |
+							((((((frame->output[j]&0x00FF00)>>8) *
+								image_alpha)/0xFF) +
+							  ((((fb_addr_long[col]&0x00FF00)>>8) *
+								background_alpha)/0xFF))<<8) |
+							((((((frame->output[j]&0x0000FF)) *
+								image_alpha)/0xFF) +
+							  ((((fb_addr_long[col]&0x0000FF)) *
+								background_alpha)/0xFF)));
+					}
+					fb_addr_long[col] = frame->output[j];
+				}
+				col++;
+				j++;
+			}
+			row++;
+		}
+		break;
+	}
+
+	if (frame->delay) {
+		OS_SLEEP(frame->delay);
+	}
+
+	fb_addr = fb + init_y_shift;
+	fb_addr_long = (unsigned long *) &fb_addr[init_x_shift];
+	row = 0;
+
+	/* TODO: It would be better to only do this to the portions of the
+	 * frame that will not get overwritten by the next frame.
+	 */
+	switch (frame->dispose_op) {
+	case APNG_DISPOSE_OP_PREVIOUS:
+		j = 0;
+		while (row < frame->height){
+			fb_addr_long = (unsigned long *)
+				&fb_addr[fb_info->screen_pitch * row + init_x_shift];
+
+			OS_MEMCPY((void *)fb_addr_long, (void *)&previous[j],
+				frame->width * sizeof(unsigned long));
+
+			j+= frame->width;
+			row++;
+		}
+		if (previous) {
+			vfree(previous);
+			previous = NULL;
+		}
+		break;
+	case APNG_DISPOSE_OP_BACKGROUND:
+		while (row < frame->height){
+			fb_addr_long = (unsigned long *)
+				&fb_addr[fb_info->screen_pitch * row + init_x_shift];
+
+			OS_MEMSET((void *)fb_addr_long, image_header.background,
+				frame->width * sizeof(unsigned long));
+
+			row++;
+		}
+		break;
+	}
+}
+
+
+void decode_png_data(
+	png_header *image_header,
+	unsigned char *input_data,
+	png_frame *frame)
+{
+	unsigned char *output;
+	unsigned long output_iter = 0;
+
+	unsigned long iter = 0;
+	unsigned char bit_iter = 0;
+	unsigned long row = 0, col = 0;
+	unsigned long end_of_row;
+	unsigned long j,k,l;
+
+	unsigned char zlib_cmf = 0;
+	unsigned char zlib_flg = 0;
+	unsigned char zlib_cm = 0;
+	unsigned char zlib_cinfo = 0;
+	unsigned char zlib_fcheck = 0;
+	unsigned char zlib_fdict = 0;
+	unsigned char zlib_flevel = 0;
+	unsigned long zlib_dictid = 0;
+
+	huffman_node *length_tree = NULL;
+	huffman_node *distance_tree = NULL;
+
+	unsigned char paeth_a, paeth_b, paeth_c;
+	unsigned long paeth_p, paeth_pa, paeth_pb, paeth_pc;
+
+	unsigned long filter_type = 0;
+	unsigned long bfinal = 0;
+	unsigned long btype = 0;
+	unsigned char compr_len = 0;
+	unsigned char compr_nlen = 0;
+	unsigned int small_color;
+
+	/* Allocate space for out output buffer */
+	output = (unsigned char *)vmalloc(
+		frame->height * frame->bytes_pl + frame->height);
+	if (!output) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(output, 0, frame->height * frame->bytes_pl + frame->height);
+
+	frame->size = frame->height * frame->width * sizeof(unsigned long);
+	frame->output = vmalloc(frame->size);
+	if (!frame->output) {
+		frame->size = 0;
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(frame->output, 0, frame->size);
+
+	/* Data, this needs to be decompressed per zlib spec */
+	if (!zlib_cmf) {
+		zlib_cmf = (unsigned char)input_data[iter++];
+		zlib_flg = (unsigned char)input_data[iter++];
+		zlib_cm = zlib_cmf & 0xF;
+		zlib_cinfo = (zlib_cmf >> 4) & 0xF;
+		zlib_fcheck = zlib_flg & 0x1F;
+		zlib_fdict = (zlib_flg & 0x20) >> 5;
+		zlib_flevel = (zlib_flg >> 6) & 0x3;
+
+		if (zlib_fdict) {
+			read_int_from_stream(input_data, &iter, &zlib_dictid);
+		}
+	}
+
+	/* Here is where we need to process data as a bit stream */
+	bfinal = 0;
+	while (!bfinal) {
+		read_bits_from_stream(input_data, &iter, &bit_iter, 1, &bfinal);
+		read_bits_from_stream(input_data, &iter, &bit_iter, 2, &btype);
+
+		if (btype == 0){
+
+			if (bit_iter) {
+				iter++;
+				bit_iter = 0;
+			}
+
+			/* No Compression */
+			read_char_from_stream(input_data, &iter, &compr_len);
+			read_char_from_stream(input_data, &iter, &compr_nlen);
+
+			for (j = 0;j < compr_len; j++) {
+				read_char_from_stream(input_data, &iter, &output[j]);
+			}
+
+		} else {
+
+			if (btype == 2){
+
+				/* Compressed with dynamnic Huffman codes */
+				build_dynamic_huffman_tree(
+						input_data,
+						&iter,
+						&bit_iter,
+						&length_tree,
+						&distance_tree);
+			} else {
+
+				/* Compressed with static Huffman codes */
+				build_static_huffman_tree(&length_tree,	&distance_tree);
+			}
+
+			/* Decompress huffman code */
+			decompress_huffman(
+					input_data,
+					&iter,
+					&bit_iter,
+					&length_tree,
+					&distance_tree,
+					output,
+					&output_iter);
+
+			free_node(length_tree);
+			free_node(distance_tree);
+		}
+	}
+
+	row = 0;
+	j = 0;
+	l = 0;
+
+	/*
+	 * Process the scanline filtering
+	 * This filtering works by using a difference from a previous pixel
+	 * instead of full pixel data.
+	 */
+	while (row < frame->height){
+		j = row * frame->bytes_pl + row;
+		end_of_row = j + frame->bytes_pl;
+		filter_type = output[j++];
+
+		switch (filter_type) {
+		case 1:
+			/* Filter type of 1 uses the previous pixel */
+			for (k=j+frame->bytes_pp; k<=end_of_row; k++) {
+				output[k] += output[k-frame->bytes_pp];
+			}
+			break;
+		case 2:
+			/* Filter type of 2 uses the previous row's pixel */
+			if (row) {
+				for (k=j; k<=end_of_row; k++) {
+					output[k] += output[k-frame->bytes_pl-1];
+				}
+			}
+			break;
+		case 3:
+			/*
+			 * Filter type of 3 uses the average of the
+			 * previous pixel and the previous row's pixel
+			 */
+			if (row) {
+				for (k=j; k<j+frame->bytes_pp; k++) {
+					output[k] += output[k-frame->bytes_pl-1]/2;
+				}
+				for (k=j+frame->bytes_pp; k<=end_of_row; k++) {
+					output[k] += (output[k-frame->bytes_pp] +
+						output[k-frame->bytes_pl-1])/2;
+				}
+			} else {
+				for (k=j+frame->bytes_pp; k<=end_of_row; k++) {
+					output[k] = output[k] +
+						output[k-frame->bytes_pp]/2;
+				}
+			}
+			break;
+		case 4:
+			/*
+			 * Filter type of 4 uses this algorithm to
+			 * determine if it should use the previous pixel,
+			 * the previous row's pixel, or the pixel immediately
+			 * before the previous row's pixel.
+			 */
+			for (k=j; k<=end_of_row; k++) {
+
+				if (k >= j + frame->bytes_pp) {
+					paeth_a = output[k-frame->bytes_pp];
+				} else {
+					paeth_a = 0;
+				}
+
+				if (row) {
+					paeth_b = output[k-frame->bytes_pl-1];
+				} else {
+					paeth_b = 0;
+				}
+
+				if (row && k >= j + frame->bytes_pp) {
+					paeth_c = output[k-frame->bytes_pp-frame->bytes_pl-1];
+				} else {
+					paeth_c = 0;
+				}
+
+				paeth_p = paeth_a + paeth_b - paeth_c;
+				paeth_pa = abs(paeth_p - paeth_a);
+				paeth_pb = abs(paeth_p - paeth_b);
+				paeth_pc = abs(paeth_p - paeth_c);
+
+				if (paeth_pa <= paeth_pb && paeth_pa <= paeth_pc) {
+					output[k] += paeth_a;
+				} else if (paeth_pb <= paeth_pc) {
+					output[k] += paeth_b;
+				} else {
+					output[k] += paeth_c;
+				}
+			}
+			break;
+		}
+
+		col = 0;
+
+		/* Put together the pixel and output to framebuffer */
+		while (col < frame->width) {
+
+			/* Truecolor with alpha, 16 bits per component */
+			if (image_header->colour_type == COLOR_TRUE_ALPHA &&
+				image_header->bit_depth == 16) {
+
+				frame->output[l] = (output[j+6]<<24 | output[j]<<16 |
+					output[j+2]<<8 | output[j+4]);
+			}
+
+
+			/* Truecolor with alpha, 8 bits per component */
+			if (image_header->colour_type == COLOR_TRUE_ALPHA &&
+				image_header->bit_depth == 8) {
+
+				frame->output[l] = (output[j+3]<<24 | output[j]<<16 |
+					output[j+1]<<8 | output[j+2]);
+			}
+
+			/* Grayscale with alpha, 16 bits per component */
+			if (image_header->colour_type == COLOR_GREY_ALPHA &&
+				image_header->bit_depth == 16) {
+
+				frame->output[l] = (output[j+2]<<24 | output[j]<<16 |
+					output[j]<<8 | output[j]);
+			}
+
+			/* Grayscale with alpha, 8 bits per component */
+			if (image_header->colour_type == COLOR_GREY_ALPHA &&
+				image_header->bit_depth == 8) {
+
+				frame->output[l] = (output[j+1]<<24 | output[j]<<16 |
+					output[j]<<8 | output[j]);
+
+			}
+
+			/* Truecolor, 16 bits per component */
+			if (image_header->colour_type == COLOR_TRUE &&
+				image_header->bit_depth == 16) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r !=
+					(output[j] | output[j+1]) ||
+					image_header->transparency_g !=
+					(output[j+2] | output[j+3]) ||
+					image_header->transparency_b !=
+					(output[j+4] | output[j+5])) {
+
+					frame->output[l] = (0xFF000000 | output[j]<<16 |
+						output[j+2]<<8 | output[j+4]);
+				}
+			}
+
+			/* Truecolor, 8 bits per component */
+			if (image_header->colour_type == COLOR_TRUE &&
+				image_header->bit_depth == 8) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r != output[j] ||
+					image_header->transparency_g != output[j+1] ||
+					image_header->transparency_b != output[j+2]) {
+
+					frame->output[l] = (0xFF000000 | (output[j]<<16) |
+						(output[j+1]<<8) | (output[j+2]));
+				}
+			}
+
+			/* Grayscale, 16 bits per component */
+			if (image_header->colour_type == COLOR_GREY &&
+				image_header->bit_depth == 16) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r !=
+					(output[j] | output[j+1])) {
+
+					frame->output[l] = (0xFF000000 |(output[j]<<16) |
+						(output[j]<<8) | output[j]);
+				}
+			}
+
+			/* Grayscale, 8 bits per component */
+			if (image_header->colour_type == COLOR_GREY &&
+				 image_header->bit_depth == 8) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r != output[j]) {
+					frame->output[l] = (0xFF000000 | (output[j]<<16) |
+						(output[j]<<8) | output[j]);
+				}
+			}
+
+			/* Grayscale, 4 bits per component */
+			if (image_header->colour_type == COLOR_GREY &&
+				image_header->bit_depth == 4) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r != ((output[j] & 0xF0)>>4)) {
+
+					frame->output[l] =
+						CONV_GS_4_TO_32((output[j] & 0xF0)>>4);
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != (output[j] & 0x0F)) {
+
+						frame->output[l] =
+							CONV_GS_4_TO_32(output[j] & 0x0F);
+					}
+				}
+			}
+
+			/* Grayscale, 2 bits per component */
+			if (image_header->colour_type == COLOR_GREY &&
+				image_header->bit_depth == 2) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r != ((output[j] & 0xC0)>>6)) {
+
+					frame->output[l] =
+						CONV_GS_2_TO_32((output[j] & 0xC0) >> 6);
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x30)>>4)) {
+
+						frame->output[l] =
+							CONV_GS_2_TO_32((output[j] & 0x30) >> 4);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x0C)>>2)) {
+
+						frame->output[l] =
+							CONV_GS_2_TO_32((output[j] & 0x0C) >> 2);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != (output[j] & 0x03)) {
+
+						frame->output[l] =
+							CONV_GS_2_TO_32(output[j] & 0x03);
+					}
+				}
+			}
+
+			/* Grayscale, 1 bit per component */
+			if (image_header->colour_type == COLOR_GREY &&
+				image_header->bit_depth == 1) {
+
+				if (!image_header->using_transparency ||
+					image_header->transparency_r != ((output[j] & 0x80)>>7)) {
+
+					frame->output[l] =
+						CONV_GS_1_TO_32((output[j] & 0x80) >> 7);
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x40)>>6)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32((output[j] & 0x40) >> 6);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x20)>>5)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32((output[j] & 0x20) >> 5);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x10)>>4)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32((output[j] & 0x10) >> 4);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x08)>>3)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32((output[j] & 0x08) >> 3);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x04)>>2)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32((output[j] & 0x04) >> 2);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != ((output[j] & 0x02)>>1)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32((output[j] & 0x02) >> 1);
+					}
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					if (!image_header->using_transparency ||
+						image_header->transparency_r != (output[j] & 0x01)) {
+
+						frame->output[l] =
+							CONV_GS_1_TO_32(output[j] & 0x01);
+					}
+				}
+			}
+
+			/* Palette, 8 bit per component */
+			if (image_header->colour_type == COLOR_INDEXED &&
+				image_header->bit_depth == 8) {
+
+				small_color = output[j];
+				frame->output[l] = 0xFF000000 |
+					image_header->image_palette[small_color];
+			}
+
+			/* Palette, 4 bit per component */
+			if (image_header->colour_type == COLOR_INDEXED &&
+				image_header->bit_depth == 4) {
+
+				small_color = (output[j] & 0xF0) >> 4;
+				frame->output[l] = 0xFF000000 |
+					image_header->image_palette[small_color];
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = output[j] & 0x0F;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+			}
+
+			/* Palette, 2 bit per component */
+			if (image_header->colour_type == COLOR_INDEXED &&
+				image_header->bit_depth == 2) {
+
+				small_color = (output[j] & 0xC0) >> 6;
+				frame->output[l] = 0xFF000000 |
+					image_header->image_palette[small_color];
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = output[j] & 0x30 >> 4;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = output[j] & 0x0C >> 2;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = output[j] & 0x03;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+			}
+
+			/* Palette, 1 bit per component */
+			if (image_header->colour_type == COLOR_INDEXED &&
+				image_header->bit_depth == 1) {
+
+				small_color = (output[j] & 0x80) >> 7;
+				frame->output[l] = 0xFF000000 |
+					image_header->image_palette[small_color];
+
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x40) >> 6;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x20) >> 5;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x10) >> 4;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x08) >> 3;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x04) >> 2;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x02) >> 1;
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+				if (col + 1 < frame->width) {
+					l++;
+					col++;
+					small_color = (output[j] & 0x01);
+					frame->output[l] = 0xFF000000 |
+						image_header->image_palette[small_color];
+				}
+			}
+
+			j += image_header->bytes_pp;
+			l++;
+			col++;
+			if (l > frame->height * frame->width) {
+				EMGD_ERROR("l is larger than frame output size!");
+				return;
+			}
+		}
+		row++;
+	}
+
+	vfree(output);
+}
+
+/*
+ * This is the function to decompress a huffman tree.
+ *
+ * @param stream (IN) This is the input data stream from which we are reading.
+ * @param iter (IN/OUT) This is the input data stream's char iterator
+ * @param bit_iter (IN/OUT) This is the bit iterator for the particular char we
+ *                          are reading.
+ * @param length_tree (IN) This is the huffman code's length tree.
+ * @param distance_tree (IN) This is the huffman code's distance tree.
+ * @param output (IN/OUT) This is an output stream to which we write out the
+ *                        decompressed huffman data.
+ * @param output_iter (IN/OUT) This is the output iterator.
+ */
+void decompress_huffman(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *bit_iter,
+	huffman_node **length_tree,
+	huffman_node **distance_tree,
+	unsigned char *output,
+	unsigned long *output_iter)
+{
+
+	unsigned long j,k;
+	huffman_node *final_node;
+	unsigned long extra_value = 0;
+	unsigned long length_value = 0;
+	unsigned long distance_value = 0;
+
+	/* Start going along the bitstream and traversing the tree
+	 * until you get to a leaf
+	 */
+	get_huffman_code(stream, iter, bit_iter, length_tree, &final_node);
+
+	while (final_node->value != 256) {
+
+		if (final_node->value < 256){
+			/* literal value */
+			output[*output_iter] = final_node->value;
+			(*output_iter)++;
+		}
+		if (final_node->value > 256){
+			/* We have the initial length value,
+			 * now get the extra length bits, if any
+			 */
+			extra_value = 0;
+			length_value = 0;
+			for (j=0; j<final_node->extra_bits; j++){
+				extra_value = read_bit_from_stream(stream, iter, bit_iter);
+				length_value += extra_value << j;
+			}
+			length_value += final_node->real;
+
+			/* Now its time to get the distance value */
+			get_huffman_code(stream, iter, bit_iter, distance_tree, &final_node);
+
+			/* Get any extra bits for the distance value */
+			extra_value = 0;
+			distance_value = 0;
+			for (j=0; j<final_node->extra_bits; j++){
+				extra_value = read_bit_from_stream(stream, iter, bit_iter);
+				distance_value += extra_value << j;
+			}
+			distance_value += final_node->real;
+
+			/*
+			 * Now we need to use the distance and length values
+			 * to copy previously existing values
+			 */
+			distance_value = (*output_iter) - distance_value;
+			for (k=0; k<length_value; k++){
+				output[*output_iter] = output[distance_value];
+				(*output_iter)++;
+				distance_value++;
+			}
+		}
+
+		/* Get the next code */
+		get_huffman_code(stream, iter, bit_iter, length_tree, &final_node);
+	}
+}
+
+
+/*
+ * This is the function to build a static huffman tree.
+ *
+ * @param length_tree (OUT) This is the huffman code's length tree.
+ * @param distance_tree (OUT) This is the huffman code's distance tree.
+ */
+void build_static_huffman_tree(
+	huffman_node **length_tree,
+	huffman_node **distance_tree) {
+
+	huffman_node *new_node = NULL;
+	huffman_node *cur_node = NULL;
+	unsigned long j,k;
+	unsigned long running_literal_value = 0;
+	unsigned long running_real_value = 0;
+
+	unsigned long ltree_literal_value[10] =
+		{256,265,269,273,277,  0,280,281,285,144};
+	unsigned long ltree_real_value[10] =
+		{  2, 11, 19, 35, 67,  0,115,131,258,144};
+	unsigned long ltree_literal_length[10] =
+		{  9,  4,  4,  4,  3,144,  1,  4,  3,112};
+	unsigned long ltree_code_start[10] =
+		{  0,  9, 13, 17, 21, 48,192,193,197,400};
+	unsigned long ltree_code_length[10] =
+		{  7,  7,  7,  7,  7,  8,  8,  8,  8,  9};
+	unsigned long ltree_extra_bits[10] =
+		{  0,  1,  2,  3,  4,  0,  4,  5,  0,  0};
+
+	unsigned long dtree_literal_value[15] =
+		{0,4,6, 8,10,12, 14, 16, 18,  20,  22,  24,  26,   28,30};
+	unsigned long dtree_real_value[15] =
+		{1,5,9,17,33,65,129,257,513,1025,2049,4097,8193,16385, 0};
+	unsigned long dtree_literal_length[15] =
+		{4,2,2, 2, 2, 2,  2,  2,  2,   2,   2,   2,   2,    2, 2};
+	unsigned long dtree_code_start[15] =
+		{0,4,6, 8,10,12, 14, 16, 18,  20,  22,  24,  26,   28,30};
+	unsigned long dtree_code_length[15] =
+		{5,5,5, 5, 5, 5,  5,  5,  5,   5,   5,   5,   5,    5, 5};
+	unsigned long dtree_extra_bits[15] =
+		{0,1,2, 3, 4, 5,  6,  7,  8,   9,   10,  11, 12,   13, 0};
+
+	/* Build our Huffman length tree using the fixed codes */
+	new_node = (huffman_node *)kzalloc(sizeof(huffman_node), GFP_KERNEL);
+	if (!new_node) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+
+	*length_tree = new_node;
+
+	for (k=0; k<10; k++){
+		running_literal_value = ltree_literal_value[k];
+		running_real_value = ltree_real_value[k];
+		for (j=0; j<ltree_literal_length[k]; j++) {
+			new_node = (huffman_node *)kzalloc(sizeof(huffman_node),GFP_KERNEL);
+			if (!new_node) {
+				EMGD_ERROR("Out of memory.");
+				return;
+			}
+
+			new_node->extra_bits = (unsigned char)ltree_extra_bits[k];
+			new_node->value = running_literal_value;
+			new_node->real = running_real_value;
+			cur_node = *length_tree;
+			add_node(&cur_node,
+					new_node,
+					ltree_code_start[k] + j,
+					ltree_code_length[k]);
+			running_literal_value++;
+			if (ltree_extra_bits[k]){
+				running_real_value += (1<<ltree_extra_bits[k]);
+			}else{
+				running_real_value++;
+			}
+		}
+	}
+
+	/* Build our Huffman distance tree using the fixed codes */
+	new_node = (huffman_node *)kzalloc(sizeof(huffman_node), GFP_KERNEL);
+	if (!new_node) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	*distance_tree = new_node;
+
+	for (k=0; k<15; k++){
+		running_literal_value = dtree_literal_value[k];
+		running_real_value = dtree_real_value[k];
+		for (j=0; j<dtree_literal_length[k]; j++) {
+			new_node = (huffman_node *)kzalloc(sizeof(huffman_node),GFP_KERNEL);
+			if (!new_node) {
+				EMGD_ERROR("Out of memory.");
+				return;
+			}
+			new_node->extra_bits = (unsigned char)dtree_extra_bits[k];
+			new_node->value = running_literal_value;
+			new_node->real = running_real_value;
+			cur_node = *distance_tree;
+			add_node(&cur_node,
+				new_node,
+				dtree_code_start[k] + j,
+				dtree_code_length[k]);
+			running_literal_value++;
+			if (dtree_extra_bits[k]){
+				running_real_value += (1<<dtree_extra_bits[k]);
+			}else{
+				running_real_value++;
+			}
+		}
+	}
+}
+
+
+/*
+ * This is the function to build a dynamic huffman tree.
+ *
+ * @param stream (IN) This is the input data stream from which we are reading.
+ * @param iter (IN/OUT) This is the input data stream's char iterator
+ * @param bit_iter (IN/OUT) This is the bit iterator for the particular char we
+ *                          are reading.
+ * @param length_tree (OUT) This is the huffman code's length tree.
+ * @param distance_tree (OUT) This is the huffman code's distance tree.
+ */
+void build_dynamic_huffman_tree(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *bit_iter,
+	huffman_node **length_tree,
+	huffman_node **distance_tree) {
+
+	unsigned long j,k;
+	unsigned long clc_order[19] =
+	{16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
+	unsigned long clc_lengths[19] =
+	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+	unsigned long clc_extra_bits[19] =
+	{2,3,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+	unsigned long clc_values[19] =
+	{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
+	huffman_node *code_length_tree = NULL;
+	unsigned long dynamic_hlit = 0;
+	unsigned long dynamic_hdist = 0;
+	unsigned long dynamic_hclen = 0;
+
+	unsigned long lit_extra_bits_num[LEN_NUM_DISTINCT_EXTRA_BITS] =
+	{265,4,4,4,4,4,1};
+	unsigned long lit_extra_bits[LEN_NUM_DISTINCT_EXTRA_BITS] =
+	{0,1,2,3,4,5,0};
+	unsigned long *dynamic_lit_code = NULL;
+	unsigned long *dynamic_lit_length = NULL;
+	unsigned long *dynamic_lit_extra_bits = NULL;
+	unsigned long *dynamic_lit_values = NULL;
+	unsigned long *dynamic_lit_real_values = NULL;
+
+	unsigned long dist_extra_bits_num[DIST_NUM_DISTINCT_EXTRA_BITS] =
+	{4,2,2,2,2,2,2,2,2,2,2,2,2,2};
+	unsigned long dist_extra_bits[DIST_NUM_DISTINCT_EXTRA_BITS] =
+	{0,1,2,3,4,5,6,7,8,9,10,11,12,13};
+	unsigned long *dynamic_dist_code = NULL;
+	unsigned long *dynamic_dist_length = NULL;
+	unsigned long *dynamic_dist_extra_bits = NULL;
+	unsigned long *dynamic_dist_values = NULL;
+	unsigned long *dynamic_dist_real_values = NULL;
+
+	unsigned long prev_real = 0;
+	unsigned long code_index;
+	huffman_node *new_node = NULL;
+
+	/* Read some initial information about our dynamic huffman tree */
+	read_bits_from_stream(stream, iter, bit_iter, 5, &dynamic_hlit);
+	read_bits_from_stream(stream, iter, bit_iter, 5, &dynamic_hdist);
+	read_bits_from_stream(stream, iter, bit_iter, 4, &dynamic_hclen);
+
+	dynamic_hlit += 257;
+	dynamic_hdist++;
+	dynamic_hclen += 4;
+
+	/* Build our Huffman length tree using the fixed codes */
+	new_node = (huffman_node *)kzalloc(sizeof(huffman_node), GFP_KERNEL);
+	if (!new_node) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	code_length_tree = new_node;
+
+	/* Get the code lengths */
+	for (k=0; k<19 && k<dynamic_hclen; k++){
+		read_bits_from_stream(stream,
+				iter, bit_iter,	3, &clc_lengths[clc_order[k]]);
+	}
+
+	/* build the code_length tree */
+	if (create_tree(CLC_MAX_BITS, CLC_NUM_CODES,
+				&clc_lengths[0],
+				&clc_extra_bits[0],
+				&clc_values[0],
+				&clc_values[0],
+				&code_length_tree) == 1) {
+		EMGD_ERROR("ERROR: create tree failed\n");
+		return;
+	}
+
+	/* Build the literal/length alphabet */
+	dynamic_lit_code = (unsigned long *)vmalloc(
+			sizeof(unsigned long) * LEN_NUM_CODES);
+	if (!dynamic_lit_code) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_lit_code, 0, sizeof(unsigned long) * LEN_NUM_CODES);
+
+	dynamic_lit_length = (unsigned long *)vmalloc(
+			sizeof(unsigned long) * LEN_NUM_CODES);
+	if (!dynamic_lit_length) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_lit_length, 0, sizeof(unsigned long) * LEN_NUM_CODES);
+
+	dynamic_lit_extra_bits = (unsigned long *)vmalloc(
+			sizeof(unsigned long) * LEN_NUM_CODES);
+	if (!dynamic_lit_extra_bits) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_lit_extra_bits, 0, sizeof(unsigned long)*LEN_NUM_CODES);
+
+	dynamic_lit_values = (unsigned long *)vmalloc(
+			sizeof(unsigned long) * LEN_NUM_CODES);
+	if (!dynamic_lit_values) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_lit_values, 0, sizeof(unsigned long)*LEN_NUM_CODES);
+
+	dynamic_lit_real_values = (unsigned long *)vmalloc(
+			sizeof(unsigned long) * LEN_NUM_CODES);
+	if (!dynamic_lit_real_values) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_lit_real_values, 0, sizeof(unsigned long)*LEN_NUM_CODES);
+
+	/* build extra information, such as extra bits, values and real_values */
+	prev_real = 2;
+	code_index = 0;
+	for (k=0; k<LEN_NUM_DISTINCT_EXTRA_BITS; k++) {
+		for (j=0; j<lit_extra_bits_num[k]; j++) {
+			dynamic_lit_extra_bits[code_index] = lit_extra_bits[k];
+			dynamic_lit_values[code_index] = code_index;
+
+			if (code_index >= LEN_START_REAL_VALUES){
+				dynamic_lit_real_values[code_index] =
+					prev_real += (1<<dynamic_lit_extra_bits[code_index-1]);
+			} else {
+				dynamic_lit_real_values[code_index] = code_index;
+			}
+			code_index++;
+		}
+	}
+
+	/* Doesn't seem to follow the pattern? */
+	dynamic_lit_real_values[285] = 258;
+
+	/* get code lengths for the literal/length alphabet */
+	get_code_lengths(stream, iter, bit_iter, &code_length_tree,
+			dynamic_hlit, dynamic_lit_length);
+
+	/* allocate tree for literal/length codes */
+	new_node = (huffman_node *)kzalloc(sizeof(huffman_node), GFP_KERNEL);
+	if (!new_node) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	*length_tree = new_node;
+
+	/* build the literal/length tree */
+	if (create_tree(LEN_MAX_BITS, LEN_NUM_CODES,
+				dynamic_lit_length,
+				dynamic_lit_extra_bits,
+				dynamic_lit_values,
+				dynamic_lit_real_values,
+				length_tree) == 1) {
+		EMGD_ERROR("ERROR: create tree failed\n");
+		return;
+	}
+
+	/* free all the literal/length data we are no longer using */
+	vfree(dynamic_lit_code);
+	vfree(dynamic_lit_length);
+	vfree(dynamic_lit_extra_bits);
+	vfree(dynamic_lit_values);
+	vfree(dynamic_lit_real_values);
+
+
+	/* Build the distance alphabet */
+	dynamic_dist_code = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * DIST_NUM_CODES);
+	if (!dynamic_dist_code) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_dist_code, 0, sizeof(unsigned long) * DIST_NUM_CODES);
+
+	dynamic_dist_length = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * DIST_NUM_CODES);
+	if (!dynamic_dist_length) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_dist_length, 0, sizeof(unsigned long) * DIST_NUM_CODES);
+
+	dynamic_dist_extra_bits = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * DIST_NUM_CODES);
+	if (!dynamic_dist_extra_bits) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_dist_extra_bits, 0,
+		sizeof(unsigned long) * DIST_NUM_CODES);
+
+	dynamic_dist_values = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * DIST_NUM_CODES);
+	if (!dynamic_dist_values) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_dist_values, 0, sizeof(unsigned long) * DIST_NUM_CODES);
+
+	dynamic_dist_real_values = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * DIST_NUM_CODES);
+	if (!dynamic_dist_real_values) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	OS_MEMSET(dynamic_dist_real_values, 0,
+		sizeof(unsigned long) * DIST_NUM_CODES);
+
+	/* build extra information, such as extra bits, values and real_values */
+	prev_real = 1;
+	code_index = 0;
+	for (k=0; k<DIST_NUM_DISTINCT_EXTRA_BITS; k++) {
+		for (j=0; j<dist_extra_bits_num[k]; j++) {
+			dynamic_dist_extra_bits[code_index] = dist_extra_bits[k];
+			dynamic_dist_values[code_index] = code_index;
+
+			if (code_index >= DIST_START_REAL_VALUES){
+				dynamic_dist_real_values[code_index] =
+					prev_real += (1<<dynamic_dist_extra_bits[code_index-1]);
+			} else {
+				dynamic_dist_real_values[code_index] = code_index+1;
+			}
+			code_index++;
+		}
+	}
+
+	/* get code lengths for the distance alphabet */
+	get_code_lengths(stream, iter, bit_iter, &code_length_tree,
+			dynamic_hdist, dynamic_dist_length);
+
+	/* allocate tree for distance codes */
+	new_node = (huffman_node *)kzalloc(sizeof(huffman_node), GFP_KERNEL);
+	if (!new_node) {
+		EMGD_ERROR("Out of memory.");
+		return;
+	}
+	*distance_tree = new_node;
+
+	/* build the distance tree */
+	if (create_tree(DIST_MAX_BITS, DIST_NUM_CODES,
+				&dynamic_dist_length[0],
+				&dynamic_dist_extra_bits[0],
+				&dynamic_dist_values[0],
+				&dynamic_dist_real_values[0],
+				distance_tree) == 1) {
+		EMGD_ERROR("ERROR: create tree failed.\n");
+		return;
+	}
+
+	/* free all the distance data we are no longer using */
+	vfree(dynamic_dist_code);
+	vfree(dynamic_dist_length);
+	vfree(dynamic_dist_extra_bits);
+	vfree(dynamic_dist_values);
+	vfree(dynamic_dist_real_values);
+
+	/* All done with the code length tree, lets free this memory */
+	free_node(code_length_tree);
+}
+
+
+/*
+ * This is the function to get the dynamic code lengths for a specified
+ * number of code lenths. There is some overuse of the word code and code
+ * lengths, but thats sort of the way the PNG spec is.  This is because
+ * we use codes to decode compressed codes.
+ *
+ * @param stream (IN) This is the input data stream from which we are reading.
+ * @param iter (IN/OUT) This is the input data stream's char iterator
+ * @param bit_iter (IN/OUT) This is the bit iterator for the particular char we
+ *                          are reading.
+ * @param code_length_tree (IN) This is the huffman code length tree, which is
+ *                              used to get the code lengths.
+ * @param num_lengths (IN) The number of code lengths.
+ * @param dynamic_lengths (OUT) Gets the dynamic length for the different
+ *                              code lengths
+ */
+void get_code_lengths(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *bit_iter,
+	huffman_node **code_length_tree,
+	unsigned long num_lengths,
+	unsigned long *dynamic_lengths) {
+
+	unsigned long j,k;
+	huffman_node *final_node;
+	unsigned long dynamic_repeat_length = 0;
+
+	/* get code lengths for the literal/length alphabet */
+	for (k=0; k<num_lengths; k++) {
+		get_huffman_code(stream, iter, bit_iter,
+			code_length_tree, &final_node);
+
+		if (final_node->value < 16){
+			dynamic_lengths[k] = final_node->value;
+		} else {
+			switch (final_node->value) {
+			case 16:
+				/* get repeat length */
+				read_bits_from_stream(stream,
+					iter, bit_iter, 2, &dynamic_repeat_length);
+				dynamic_repeat_length += 3;
+				for (j=0; j<dynamic_repeat_length; j++){
+					dynamic_lengths[k+j] = dynamic_lengths[k-1];
+				}
+				k += j-1;
+				break;
+			case 17:
+				/* get repeat length */
+				read_bits_from_stream(stream,
+					iter, bit_iter, 3, &dynamic_repeat_length);
+				dynamic_repeat_length += 3;
+				for (j=0; j<dynamic_repeat_length; j++){
+					dynamic_lengths[k+j] = 0;
+				}
+				k += j-1;
+				break;
+			case 18:
+				/* get repeat length */
+				read_bits_from_stream(stream,
+					iter, bit_iter, 7, &dynamic_repeat_length);
+				dynamic_repeat_length += 11;
+				for (j=0; j<dynamic_repeat_length; j++){
+					dynamic_lengths[k+j] = 0;
+				}
+				k += j-1;
+				break;
+			}
+		}
+	}
+}
+
+
+/*
+ * This function creates a tree given the necessary tree information.
+ *
+ * @param max_bits (IN) The maximum number of bits for any code
+ * @param num_codes (IN) The number of codes
+ * @param code_lengths (IN) The code lengths
+ * @param extra_bits (IN) The number of extra bits for each huffman code
+ * @param values (IN) The values for the huffman code
+ * @param real_values (IN) The real values for the huffman code
+ * @param tree (OUT) The resulting huffman tree.
+ *
+ * @return 0 on Success
+ * @return >0 on Error
+ */
+int create_tree(
+	unsigned long max_bits,
+	unsigned long num_codes,
+	unsigned long *code_lengths,
+	unsigned long *extra_bits,
+	unsigned long *values,
+	unsigned long *real_values,
+	huffman_node **tree) {
+
+	unsigned long *clc_count;
+	unsigned long *clc_next_code;
+	unsigned long *codes;
+	unsigned long clc_code;
+	unsigned long k;
+	huffman_node *cur_node;
+	huffman_node *new_node;
+
+	if (!tree) {
+		EMGD_ERROR("Bad tree pointer.");
+		return 1;
+	}
+
+	/* Step 1: Count the number of codes for each code length */
+	clc_count = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * (max_bits+1));
+    if (!clc_count) {
+		EMGD_ERROR("Out of memory.");
+		return 1;
+    }
+    OS_MEMSET(clc_count, 0, sizeof(unsigned long) * (max_bits+1));
+
+	for (k=0; k<num_codes; k++){
+		clc_count[code_lengths[k]]++;
+	}
+
+	/* Step 2: Get numerical value of smallest code for each code length */
+	clc_next_code = (unsigned long *)vmalloc(
+		sizeof(unsigned long) * (max_bits+1));
+    if (!clc_next_code) {
+		EMGD_ERROR("Out of memory.");
+		return 1;
+    }
+    OS_MEMSET(clc_next_code, 0, sizeof(unsigned long) * (max_bits+1));
+
+	clc_code = 0;
+	clc_next_code[0] = 2;
+	for (k=1; k<=max_bits; k++){
+	    clc_code = (clc_code + clc_count[k-1]) << 1;
+	    clc_next_code[k] = clc_code;
+	}
+
+	/* Step 3: Assign numerical values to all codes */
+	codes = (unsigned long *)vmalloc(sizeof(unsigned long) * num_codes);
+    if (!codes) {
+		EMGD_ERROR("Out of memory.");
+		return 1;
+    }
+    OS_MEMSET(codes, 0, sizeof(unsigned long) * num_codes);
+
+	for (k=0; k<num_codes; k++){
+	    if (code_lengths[k] > 0){
+			codes[k] = clc_next_code[code_lengths[k]]++;
+
+			/* Add this node to the code length tree */
+			new_node = (huffman_node *)kzalloc(sizeof(huffman_node),GFP_KERNEL);
+			if (!new_node) {
+				EMGD_ERROR("Out of memory.");
+				return 1;
+			}
+
+			new_node->extra_bits = (unsigned char)extra_bits[k];
+			new_node->value = values[k];
+			new_node->real = real_values[k];
+			cur_node = *tree;
+			add_node(&cur_node, new_node, codes[k], code_lengths[k]);
+	    }
+	}
+
+	vfree(clc_count);
+	vfree(clc_next_code);
+	vfree(codes);
+	return 0;
+}
+
+
+/*
+ * This function recursively frees a huffman node and all its sub nodes.
+ * First we free any sub nodes, then we free itself.
+ *
+ * @param node (IN) The huffman node to free.
+ */
+void free_node(huffman_node *node) {
+	if (node->leaf[0]) {
+		free_node((huffman_node *)(node->leaf[0]));
+	}
+	if (node->leaf[1]) {
+		free_node((huffman_node *)(node->leaf[1]));
+	}
+	kfree(node);
+}
+
+
+/*
+ * This function gets a huffman code by traversing through a bit
+ * stream as if those are directions for traversling through
+ * a binary tree.   When we hit a leaf node, we have our value.
+ *
+ * @param stream (IN) This is the input data stream from which we are reading.
+ * @param iter (IN/OUT) This is the input data stream's char iterator
+ * @param bit_iter (IN/OUT) This is the bit iterator for the particular char we
+ *                          are reading.
+ * @param tree (IN) This is the huffman tree.
+ * @param final_node (OUT) The final leaf node we have reached.
+ */
+void get_huffman_code(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *bit_iter,
+	huffman_node **tree,
+	huffman_node **final_node){
+
+	*final_node = *tree;
+	while ((*final_node)->leaf[0] || (*final_node)->leaf[1]) {
+		(*final_node) = (huffman_node *)(*final_node)->
+			leaf[((stream[*iter] >> *bit_iter) & 1)];
+
+		if (++(*bit_iter) == 8) {
+			(*iter)++;
+			(*bit_iter) = 0;
+		}
+	}
+}
+
+
+/*
+ * This function adds a node into a tree.
+ *
+ * @param tree (IN/OUT) This is the tree's root to which we'll be adding a
+ *                      node.
+ * @param node (IN) This is the node we'll be adding.
+ * @param code (IN) This is the code which will be used as a map to determine
+ *                  where the new node goes on the tree.
+ * @param code_length (IN) This is the code length for the code passed in.
+ *
+ * @return 0 on Success
+ * @return >0 on Error
+ */
+int add_node(
+	huffman_node **tree,
+	huffman_node *node,
+	unsigned long code,
+	unsigned long code_length){
+
+	huffman_node *new_node;
+
+	if (!(*tree)) {
+		EMGD_ERROR("Invalid tree pointer.");
+		return 1;
+	}
+
+	if (code_length > 1){
+
+		/* Build a leaf node if it doesn't exist */
+		if (!(*tree)->leaf[(code >> (code_length-1)) & 1]){
+			new_node = (huffman_node *)kzalloc(sizeof(huffman_node),GFP_KERNEL);
+			if (!new_node) {
+				EMGD_ERROR("Out of memory");
+				return 1;
+			}
+
+			(*tree)->leaf[(code >> (code_length-1)) & 1] =
+				(struct huffman_node *)new_node;
+			(*tree) = new_node;
+		} else {
+			(*tree) =
+				(huffman_node *)(*tree)->leaf[(code >> (code_length-1)) & 1];
+		}
+
+		/* Recursively add the tree node */
+		add_node(&(*tree), node, code, --code_length);
+
+	} else {
+		/* This is where our leaf node belongs */
+		(*tree)->leaf[code & 1] = (struct huffman_node *)node;
+	}
+	return 0;
+}
+
+
+/*
+ * This function reads a 4 byte value from a given stream.
+ * This assumes the passed in stream is byte aligned.
+ *
+ * @param stream (IN) The stream from which we are reading.
+ * @param iter (IN/OUT) The stream iterator.
+ * @param value (OUT) The value read from the stream.
+ *
+ * @return 0 on Success
+ * @return >0 on Error
+ */
+int read_int_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned long *value){
+
+	*value = stream[*iter] << 24 |
+		stream[(*iter)+1] << 16 |
+		stream[(*iter)+2] << 8 |
+		stream[(*iter)+3];
+	*iter += 4;
+	return 0;
+}
+
+
+/*
+ * This function reads a 2 byte value from a given stream.
+ * This assumes the passed in stream is byte aligned.
+ *
+ * @param stream (IN) The stream from which we are reading.
+ * @param iter (IN/OUT) The stream iterator.
+ * @param value (OUT) The value read from the stream.
+ *
+ * @return 0 on Success
+ * @return >0 on Error
+ */
+int read_short_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned short *value){
+
+	*value = stream[(*iter)] << 8 |
+		stream[(*iter)+1];
+	*iter += 2;
+	return 0;
+}
+
+
+/*
+ * This function reads a 1 byte value from a given stream.
+ * This assumes the passed in stream is byte aligned.
+ *
+ * @param stream (IN) The stream from which we are reading.
+ * @param iter (IN/OUT) The stream iterator.
+ * @param value (OUT) The value read from the stream.
+ *
+ * @return 0 on Success
+ * @return >0 on Error
+ */
+int read_char_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *value){
+
+	*value = stream[*iter];
+	(*iter)++;
+
+	return 0;
+}
+
+
+/*
+ * This function reads a given number of bits from a given stream.
+ * This does not assume the passed in stream is byte aligned.
+ *
+ * @param stream (IN) The stream from which we are reading.
+ * @param iter (IN/OUT) The stream iterator.
+ * @param bit_iter (IN/OUT) The stream's bit iterator.
+ * @param num_bits (IN) The number of bits to read.
+ * @param value (OUT) The value read from the stream.
+ *
+ * @return 0 on Success
+ * @return >0 on Error
+ */
+int read_bits_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *bit_iter,
+	unsigned long num_bits,
+	unsigned long *value){
+
+	unsigned long i;
+	*value = 0;
+
+	for (i=0; i<num_bits; i++){
+		*value += read_bit_from_stream(stream, iter, bit_iter) << i;
+	}
+
+	return 0;
+}
+
+
+/*
+ * This function reads a single bit from a given stream.
+ * This does not assume the passed in stream is byte aligned.
+ *
+ * @param stream (IN) The stream from which we are reading.
+ * @param iter (IN/OUT) The stream iterator.
+ * @param bit_iter (IN/OUT) The stream's bit iterator.
+ *
+ * @return The bit value read.
+ */
+unsigned int read_bit_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *bit_iter){
+
+	unsigned int result = 0;
+
+	/* get our bit */
+	result = (stream[*iter] >> *bit_iter) & 1;
+
+	/* This is faster than above */
+	if (++(*bit_iter) == 8) {
+		(*iter)++;
+		(*bit_iter) = 0;
+	}
+
+	return result;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/drm/splash_screen.h b/drivers/gpu/drm/emgd/emgd/drm/splash_screen.h
new file mode 100755
index 0000000..5fac725
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/splash_screen.h
@@ -0,0 +1,280 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: splash_screen.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the Intel Embedded Graphics EFI Driver Splash Screen header file.
+ *  This file contains data structures pertinent to showing a splash screen
+ *  with a customizable icon.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _SPLASH_SCREEN_H
+#define _SPLASH_SCREEN_H
+
+#include <user_config.h>
+
+#define CONV_16_TO_32_BIT(a) (0xFF000000 | ((a & 0xF800)<<8) |\
+						((a & 0x7E0)<<5) | (a & 0x1F)<<3)
+#define CONV_GS_4_TO_32(a) (0xFF000000 | ((a)<<20) | ((a)<<16) |\
+						((a)<<12) | ((a)<<8) | ((a)<<4) | ((a)))
+
+#define CONV_GS_2_TO_32(a) (0xFF000000 | ((a)<<22) | ((a)<<20) | ((a)<<18) |\
+						((a)<<16) | ((a)<<14) | ((a)<<12) | ((a)<<10) |\
+						((a)<<8) | ((a)<<6) | ((a)<<4) | ((a)<<2) | ((a)))
+
+#define CONV_GS_1_TO_32(a) (0xFF000000 | ((a)<<23) | ((a)<<22) | ((a)<<21) |\
+						((a)<<20) | ((a)<<19) | ((a)<<18) | ((a)<<17) |\
+						((a)<<16) |	((a)<<15) | ((a)<<14) | ((a)<<13) |\
+						((a)<<12) |	((a)<<11) | ((a)<<10) | ((a)<<9) |\
+						((a)<<8) | ((a)<<7) | ((a)<<6) | ((a)<<5) | ((a)<<4) |\
+						((a)<<3) | ((a)<<2) | ((a)<<1) | ((a)))
+
+/* Colour_type options */
+#define COLOR_GREY       0
+#define COLOR_TRUE       2
+#define COLOR_INDEXED    3
+#define COLOR_GREY_ALPHA 4
+#define COLOR_TRUE_ALPHA 6
+
+#define CONV_GS_4_TO_32(a) (0xFF000000 | ((a)<<20) | ((a)<<16) | ((a)<<12) |\
+						((a)<<8) | ((a)<<4) | ((a)))
+
+#define CONV_GS_2_TO_32(a) (0xFF000000 | ((a)<<22) | ((a)<<20) | ((a)<<18) |\
+						((a)<<16) | ((a)<<14) | ((a)<<12) | ((a)<<10) |\
+						((a)<<8) | ((a)<<6) | ((a)<<4) | ((a)<<2) | ((a)))
+
+#define CONV_GS_1_TO_32(a) (0xFF000000 | ((a)<<23) | ((a)<<22) | ((a)<<21) |\
+						((a)<<20) |	((a)<<19) | ((a)<<18) | ((a)<<17) |\
+						((a)<<16) |	((a)<<15) | ((a)<<14) | ((a)<<13) |\
+						((a)<<12) |	((a)<<11) | ((a)<<10) | ((a)<<9) |\
+						((a)<<8) | ((a)<<7) | ((a)<<6) | ((a)<<5) | ((a)<<4) |\
+						((a)<<3) | ((a)<<2) | ((a)<<1) | ((a)))
+
+#define PNG_HEADER_SIZE                   8
+#define PNG_CRC_SIZE                      4
+#define CLC_MAX_BITS                      7
+#define CLC_NUM_CODES                    19
+#define LEN_MAX_BITS                     15
+#define LEN_NUM_CODES                   288
+#define DIST_MAX_BITS                    15
+#define DIST_NUM_CODES                   32
+#define LEN_NUM_DISTINCT_EXTRA_BITS       7
+#define DIST_NUM_DISTINCT_EXTRA_BITS     14
+#define LEN_START_REAL_VALUES           257
+#define DIST_START_REAL_VALUES            1
+#define DISPLAY_START                  8365
+#define DISPLAY_MAX                    8372
+#define DISPLAY_MAX2                   8372
+
+/* Chunk types */
+#define CHUNK_IHDR 0x49484452
+#define CHUNK_SRGB 0x73524742
+#define CHUNK_PHYS 0x70485973
+#define CHUNK_TIME 0x74494D45
+#define CHUNK_BKGD 0x624B4744
+#define CHUNK_TRNS 0x74524E53
+#define CHUNK_CHRM 0x6348524D
+#define CHUNK_GAMA 0x67414D41
+#define CHUNK_ICCP 0x69434350
+#define CHUNK_SBIT 0x73424954
+#define CHUNK_TEXT 0x74455874
+#define CHUNK_ZTXT 0x7A545874
+#define CHUNK_ITXT 0x69545874
+#define CHUNK_HIST 0x68495354
+#define CHUNK_SPLT 0x73504C54
+#define CHUNK_PLTE 0x504C5445
+#define CHUNK_IDAT 0x49444154
+#define CHUNK_IEND 0x49454E44
+
+/* APNG Chunks */
+#define CHUNK_ACTL 0x6163544C
+#define CHUNK_FCTL 0x6663544C
+#define CHUNK_FDAT 0x66644154
+
+/* Colour_type options */
+#define COLOR_GREY       0
+#define COLOR_TRUE       2
+#define COLOR_INDEXED    3
+#define COLOR_GREY_ALPHA 4
+#define COLOR_TRUE_ALPHA 6
+
+/* APNG dispose_op codes */
+#define APNG_DISPOSE_OP_NONE       0
+#define APNG_DISPOSE_OP_BACKGROUND 1
+#define APNG_DISPOSE_OP_PREVIOUS   2
+
+/* APNG blend_op codes */
+#define APNG_BLEND_OP_SOURCE 0
+#define APNG_BLEND_OP_OVER   1
+
+typedef struct _bitmap_header {
+	/* What is the widht and height of the bitmap */
+	unsigned short width;
+	unsigned short height;
+	/* If Negative, from bottom right, how much to go left by */
+	/* If Positive, from top left, how much to go right by */
+	short x_coord;
+	/* If Negative, from bottom right, how much to go up by */
+	/* If Positive, from top left, how much to go down by */
+	short y_coord;
+} bitmap_header;
+
+typedef struct _png_header {
+	unsigned long width;
+	unsigned long height;
+	unsigned long x_offset;
+	unsigned long y_offset;
+	unsigned char bit_depth;
+	unsigned char colour_type;
+	unsigned char compression_method;
+	unsigned char filter_method;
+	unsigned char interlace_method;
+	unsigned long bpp;
+	unsigned long bytes_pp;
+	unsigned long bytes_pl;
+	unsigned long background;
+	unsigned char background_r;
+	unsigned char background_g;
+	unsigned char background_b;
+	unsigned long *image_palette;
+	unsigned long using_transparency;
+	unsigned short transparency_r;
+	unsigned short transparency_g;
+	unsigned short transparency_b;
+} png_header;
+
+typedef struct _png_frame {
+	unsigned long *output;
+	unsigned long size;
+	unsigned long width;
+	unsigned long height;
+	unsigned long x_offset;
+	unsigned long y_offset;
+	unsigned long bytes_pp;
+	unsigned long bytes_pl;
+	unsigned long delay;
+	unsigned char dispose_op;
+	unsigned char blend_op;
+} png_frame;
+
+typedef struct _huffman_node {
+	unsigned long value;
+	unsigned long real;
+	unsigned char extra_bits;
+	struct huffman_node *leaf[2];
+} huffman_node;
+
+void display_png_frame(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	png_header image_header,
+	png_frame *frame,
+	unsigned long prev_dispose_op);
+void decode_png_data(
+	png_header *image_header,
+	unsigned char *input_data,
+	png_frame *frame);
+int create_tree(
+    unsigned long max_bits,
+    unsigned long num_codes,
+    unsigned long *code_lengths,
+    unsigned long *extra_bits,
+    unsigned long *values,
+    unsigned long *real_values,
+    huffman_node **tree);
+void free_node(huffman_node *node);
+void display_splash_screen(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	emgd_drm_splash_screen_t *ss_data);
+void display_bmp_splash_screen(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	emgd_drm_splash_screen_t *ss_data);
+void display_png_splash_screen(
+	igd_framebuffer_info_t *fb_info,
+	unsigned char *fb,
+	emgd_drm_splash_screen_t *ss_data);
+void decompress_huffman(
+    unsigned char *stream,
+    unsigned long *iter,
+    unsigned char *bit_iter,
+    huffman_node **length_tree,
+    huffman_node **distance_tree,
+	unsigned char *output,
+	unsigned long *output_iter);
+void build_static_huffman_tree(
+    huffman_node **length_tree,
+    huffman_node **distance_tree);
+void build_dynamic_huffman_tree(
+    unsigned char *stream,
+    unsigned long *iter,
+    unsigned char *bit_iter,
+    huffman_node **length_tree,
+    huffman_node **distance_tree);
+void get_code_lengths(
+    unsigned char *stream,
+    unsigned long *iter,
+    unsigned char *bit_iter,
+    huffman_node **code_length_tree,
+    unsigned long num_lengths,
+    unsigned long *dynamic_lengths);
+void get_huffman_code(
+    unsigned char *stream,
+    unsigned long *iter,
+    unsigned char *bit_iter,
+    huffman_node **tree,
+    huffman_node **final_node);
+int add_node(
+    huffman_node **tree,
+    huffman_node *node,
+    unsigned long code,
+    unsigned long code_length);
+int read_int_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned long *value);
+int read_short_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned short *value);
+int read_char_from_stream(
+	unsigned char *stream,
+	unsigned long *iter,
+	unsigned char *value);
+int read_bits_from_stream(
+    unsigned char *stream,
+    unsigned long *iter,
+    unsigned char *bit_iter,
+    unsigned long num_bits,
+    unsigned long *value);
+unsigned int read_bit_from_stream(
+    unsigned char *stream,
+    unsigned long *iter,
+    unsigned char *bit_iter);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/drm/user_config.c b/drivers/gpu/drm/emgd/emgd/drm/user_config.c
new file mode 100644
index 0000000..cea78e1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/user_config.c
@@ -0,0 +1,252 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: user_config.c
+ * $Revision: 1.24 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  A file that contains the initial display configuration information of the
+ *  EMGD kernel module.  A user can edit this file in order to affect the way
+ *  that the kernel initially configures the displays.  This file is compiled
+ *  into the EMGD kernel module.
+ *-----------------------------------------------------------------------------
+ */
+#include "user_config.h"
+
+#warning ****
+#warning **** This driver has NOT been configured for your system.  You are
+#warning **** building with a sample user_config.c configuration that may or
+#warning **** may not meet your needs.  It is recommended that you run CED to
+#warning **** generate an appropriate configuration or see the User Guide
+#warning **** for more information about driver configuration.
+#warning ****
+
+
+/*
+ * One array of igd_display_info_t structures should exist for each port that
+ * needs to provide a DTD list.  Each igd_display_info_t contains the DTD
+ * information for a given resolution/refresh-rate.  This is especially needed
+ * for analog/VGA ports.
+ */
+
+#define TUNNELCREEK 1
+#define POULSBO 0
+
+#if TUNNELCREEK
+static igd_display_info_t dtd_config1_port4_dtdlist[] = {
+	{
+		1366,   /* Width */
+		768,    /* Height */
+		60,     /* Refresh Rate */
+		72300,  /* Dot Clock */
+		1525,   /* Horizontal Total (horizontal synch end) */
+		1365,   /* Horizontal Blank Start (h_active-1) */
+		1525,   /* Horizontal Blank End (start + h_blank) */
+		1413,   /* Horizontal Sync Start (h_active+h_synch-1) */
+		1445,   /* Horizontal Sync End (start + h_syncp) */
+		789,    /* Vertical Total (Vertical synch end) */
+		767,    /* Vertical Blank Start (v_active-1) */
+		789,    /* Vertical Blank End (start + v_blank) */
+		770,    /* Vertical Sync Start (v_active+v_synch-1) */
+		775,    /* Vertical Sync End (start + v_synchp) */
+		0,  /* Mode Number */
+		0x20000,    /* Flags */
+		0,  /* X Offset */
+		0,  /* Y Offset */
+		NULL,   /* pd extension pointer */
+		0, 0   /* mode extension pointer */
+	},
+};
+#endif
+
+#if POULSBO
+static igd_display_info_t dtd_config1_port4_dtdlist[] = {
+	{
+		1024,			/* Width */
+		768,				/* Height */
+		60,				/* Refresh Rate */
+		65000,			/* Dot Clock (in KHz) */
+		1343,			/* Horizontal Total (horizontal synch end) */
+		1023,			/* Horizontal Blank Start (h_active-1) */
+		1343,			/* Horizontal Blank End (start + h_blank) */
+		1047,			/* Horizontal Sync Start (h_active+h_synch-1) */
+		1183,			/* Horizontal Sync End (start + h_syncp) */
+		805,				/* Vertical Total (Vertical synch end) */
+		767,				/* Vertical Blank Start (v_active-1) */
+		805,				/* Vertical Blank End (start + v_blank) */
+		770,				/* Vertical Sync Start (v_active+v_synch-1) */
+		776,				/* Vertical Sync End (start + v_synchp) */
+		0,				/* Mode Number */
+		0x20000,			/* Flags */
+		0,				/* X Offset */
+		0,				/* Y Offset */
+		NULL,			/* pd extension pointer */
+		0, 0				/* mode extension pointer */
+	},
+};
+#endif
+
+static igd_param_attr_t attrs_config1_port4[] = {
+	{0x46, 100},
+#if POULSBO
+	{0x1a, 24},
+#endif
+#if TUNNELCREEK
+	{0x47, 20300},
+#endif
+};
+
+static emgd_drm_splash_screen_t splash_screen_data = {
+	0x000000,		/* bg_color */
+	0,			/* x */
+	0,			/* y */
+	0,			/* width */
+	0,			/* height */
+};
+
+static emgd_drm_splash_video_t splash_video_data = {
+	0,			/* offset */
+	0,			/* pixel_format */
+	0,			/* src_width */
+	0,			/* src_height */
+	0,			/* src_pitch */
+	0,			/* dst_x */
+	0,			/* dst_y */
+	0,			/* dst_width */
+	0,			/* dst_height */
+};
+
+static igd_param_t config_params_config1 = {
+	1*256*1024,		/* Page request */
+	0,		/* Max frame buffer size */
+	1,		/* Preserve registers */
+	0x6,		/* Display flags */
+	{ 4, 2, 0, 0, 0 },		/* Display port order */
+	{		/* Display Params */
+		{		/* Port */
+			4,		/* Display port number */
+			0x180,		/* Parameters present */
+			0x0,		/* EDID flag */
+			0x5,		/* Flags when EDID is available */
+			0x5,		/* Flags when EDID is not available */
+			0,		/* DDC GPIO pins */
+			0,		/* DDC speed */
+			0,		/* DDC DAB */
+			0,		/* I2C GPIO pins */
+			0,		/* I2C speed */
+			0,		/* I2C DAB */
+			{		/* Flat Panel Info */
+				0,		/* Flat Panel width */
+				0,		/* Flat Panel height */
+				0,		/* Flat Panel power method */
+				0,		/* VDD active & DVO clock/data active */
+				0,		/* DVO clock/data active & backlight enable */
+				0,		/* backlight disable & DVO clock/data inactive */
+				0,		/* DVO clock/data inactive & VDD inactive */
+				0			/* VDD inactive & VDD active */
+			},
+			{		/* DTD Info */
+				sizeof(dtd_config1_port4_dtdlist)/sizeof(igd_display_info_t),		/* Number of DTDs */
+				dtd_config1_port4_dtdlist		/* DTD name */
+			},
+			{		/* Attribute Info */
+				sizeof(attrs_config1_port4)/sizeof(igd_param_attr_t),		/* Number of attributes */
+				attrs_config1_port4		/* Attr name */
+			}
+		},
+		{		/* Port */
+			2,		/* Display port number */
+			0x0,		/* Parameters present */
+			0x0,		/* EDID flag */
+			0x5,		/* Flags when EDID is available */
+			0x5,		/* Flags when EDID is not available */
+			0,		/* DDC GPIO pins */
+			0,		/* DDC speed */
+			0,		/* DDC DAB */
+			0,		/* I2C GPIO pins */
+			0,		/* I2C speed */
+			0,		/* I2C DAB */
+			{		/* Flat Panel Info */
+				0,		/* Flat Panel width */
+				0,		/* Flat Panel height */
+				0,		/* Flat Panel power method */
+				0,		/* VDD active & DVO clock/data active */
+				0,		/* DVO clock/data active & backlight enable */
+				0,		/* backlight disable & DVO clock/data inactive */
+				0,		/* DVO clock/data inactive & VDD inactive */
+				0			/* VDD inactive & VDD active */
+			},
+			{		/* DTD Info */
+			0, NULL
+			},
+			{		/* Attribute Info */
+			0, NULL
+			}
+		},
+	},
+	0,			/* 24-bit RGB color that framebuffer is cleared to */
+	1,			/* Quickboot (1 = enabled) */
+	0,			/* Quickboot seamless (1 = enabled) */
+	0,			/* Quickboot video input (1 = enabled) */
+	0			/* Polling (1 = override interrupt support and use polling) */
+};
+
+igd_param_t *config_params = {&config_params_config1};
+
+/*
+ * The emgd_drm_config_t structure is the main configuration structure
+ * for the EMGD kernel module.
+ */
+emgd_drm_config_t config_drm = {
+	0,	/* Whether to initialize the display at EMGD module startup time
+		 * (corresponds to the "init" module parameter)
+		 */
+	1,	/* The display configuration to use if initializing the display
+		 * (corresponds to the "init" module parameter), where:
+		 * - 1 = Single port/display
+		 * - 2 = Cloned port/display (e.g. LVDS + CRT with different timings)
+		 * - 4 = Twin ports/displays (e.g. LVDS + CRT with same timings)
+		 *       Note: Twin is NOT CURRENTLY SUPPORTED
+		 * - 8 = Extended displays (e.g. LVDS + CRT displaying different images)
+		 */
+	1366,/* Display width to use if initializing the display
+		 * (corresponds to the "width" module parameter)
+		 */
+	768,/* Display height to use if initializing the display
+		 * (corresponds to the "height" module parameter)
+		 */
+	60,	/* Display refresh rate to use if initializing the display
+		 * (corresponds to the "refresh" module parameter)
+		 */
+	0,  /* KMS */
+	0,	/* ovl_brightness */
+	0,	/* ovl_contrast */
+	0,	/* ovl_saturation */
+	0,	/* ovl_gamma_red */
+	0,	/* ovl_gamma_green */
+	0,	/* ovl_gamma_blue */
+	&splash_screen_data,
+	&splash_video_data,
+	&config_params	/* driver parameters from above */
+};
+
diff --git a/drivers/gpu/drm/emgd/emgd/drm/user_config.h b/drivers/gpu/drm/emgd/emgd/drm/user_config.h
new file mode 100644
index 0000000..5aca4d8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/drm/user_config.h
@@ -0,0 +1,113 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: user_config.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Data structure containing the initial display configuration information of
+ *  the EMGD kernel module.
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _USER_CONFIG_H_
+#define _USER_CONFIG_H_
+
+#include "igd_init.h"
+#include "igd_mode.h"
+
+/*
+ * Splash Screen data provided by the user.
+ */
+typedef struct _emgd_drm_splash_screen {
+	unsigned long bg_color;
+	unsigned long x;
+	unsigned long y;
+	unsigned long width;
+	unsigned long height;
+} emgd_drm_splash_screen_t;
+
+/*
+ * Splash Video data provided by the user.
+ */
+typedef struct _emgd_drm_splash_video {
+	unsigned long offset;
+	unsigned long pixel_format;
+	unsigned long src_width;
+	unsigned long src_height;
+	unsigned long src_pitch;
+	unsigned long dst_x;
+	unsigned long dst_y;
+	unsigned long dst_width;
+	unsigned long dst_height;
+} emgd_drm_splash_video_t;
+
+/**
+ * User-configurable parameters.  This structure is the basis for the
+ * user_config.c" file, which allows compile-time customization of the EMGD DRM
+ * module.
+ *
+ * Besides the igd_param_t values, the other options in this structure
+ * correspond to EMGD module parameters of the same name.  Most are only
+ * applicable if the init option is non-zero.  There is one additional module
+ * parameter ("portorder") that corresponds to the port_order member of the
+ * igd_param_t structure.
+ */
+typedef struct _emgd_drm_config {
+	/**
+	 * Whether the EMGD kernel/DRM module should initialize the display at
+	 * startup time (1=yes, 0=no).
+	 */
+	int init;
+	/** The display configuration to use if initializing the display. */
+	int dc;
+	/** The width to use if initializing the display. */
+	int width;
+	/** The height to use if initializing the display. */
+	int height;
+	/** The refresh rate to use if initializing the display. */
+	int refresh;
+	/** Enable Kernel Mode Set */
+	int kms;
+	/** Overlay Brightness */
+	unsigned long ovl_brightness;
+	/** Overlay Contrast */
+	unsigned long ovl_contrast;
+	/** Overlay Saturation */
+	unsigned long ovl_saturation;
+	/** Overlay Gamma Correction - Red */
+	unsigned long ovl_gamma_red;
+	/** Overlay Gamma Correction - Green */
+	unsigned long ovl_gamma_green;
+	/** Overlay Gamma Correction - Blue */
+	unsigned long ovl_gamma_blue;
+	/** The splash screen data if specified by the user. */
+	emgd_drm_splash_screen_t *ss_data;
+	/** The splash video data if specified by the user. */
+	emgd_drm_splash_video_t *sv_data;
+	/** Array of other parameters (one per configid), used by the hardware
+	 * abstraction layer code.
+	 */
+	igd_param_t **hal_params;
+} emgd_drm_config_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/gmm/gmm.c b/drivers/gpu/drm/emgd/emgd/gmm/gmm.c
new file mode 100644
index 0000000..cb51f93
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/gmm/gmm.c
@@ -0,0 +1,1382 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: gmm.c
+ * $Revision: 1.52 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Very basic video memory managment functions required by HAL.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.gmm
+
+#include <igd_debug.h>
+#include <drmP.h>
+#include <memlist.h>
+#include <io.h>
+#include <memory.h>
+
+#include <linux/module.h>
+#include <linux/init.h>
+
+#include <asm/agp.h>
+
+#define AGP_PHYS_MEMORY 2 /* Physical contigous memory */
+struct emgd_ci_surface_t{
+	unsigned int used;
+	unsigned int v4l2_offset;
+	unsigned int virt;
+	unsigned long size;
+	unsigned long gtt_offset;
+	};
+#define MAX_CI_LIST_SIZE 14
+struct emgd_ci_surface_t ci_surfaces[MAX_CI_LIST_SIZE];
+
+
+gmm_context_t gmm_context;
+
+gmm_chunk_t *gmm_get_chunk(igd_context_t *context, unsigned long offset);
+static int gmm_flush_cache(void);
+static int gmm_alloc_linear_surface(unsigned long *offset,
+		unsigned long pixel_format,
+		unsigned int *width,
+		unsigned int *height,
+		unsigned int *pitch,
+		unsigned long *size,
+		unsigned long type,
+		unsigned long flags,
+		unsigned long phys);
+
+static int gmm_alloc_chunk_space(gmm_context_t *gmm_context,
+		unsigned long *offset,
+		unsigned long size,
+		unsigned long phys,
+		unsigned long flags);
+
+static int gmm_import_pages(void **pagelist,
+		unsigned long *gtt_offset,
+		unsigned long numpages);
+
+static int gmm_get_page_list(unsigned long offset,
+		unsigned long **pages,
+		unsigned long *page_cnt);
+
+gmm_mem_buffer_t *emgd_alloc_pages(unsigned long num_pages, int type);
+void emgd_free_pages(gmm_mem_buffer_t *mem);
+void emgd_gtt_remove(igd_context_t *context, gmm_mem_buffer_t *mem,
+		unsigned long offset);
+void emgd_gtt_insert(igd_context_t *context, gmm_mem_buffer_t *mem,
+		unsigned long offset);
+
+
+static int gmm_map_ci(unsigned long *gtt_offset,
+			unsigned long ci_param,	
+			unsigned long *virt_addr,
+			unsigned int map_method,
+			unsigned long size);
+
+
+static int gmm_unmap_ci(unsigned long virt_addr);
+
+static void gmm_free(unsigned long offset)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_DEBUG("Enter gmm_free(0x%lx)", offset);
+
+	/* Walk the chunk list */
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		if (chunk->offset == offset) {
+			switch (chunk->usage) {
+			case FREE_ALLOCATED:
+				EMGD_DEBUG("WARNING: The chunk 0x%lx is already freed", offset);
+				break;
+			case INUSE_IMPORTED:
+			case FREE_IMPORTED:
+				EMGD_DEBUG("WARNING: The chunk 0x%lx was allocated externally", offset);
+				return;
+			case INUSE_ALLOCATED:
+				EMGD_DEBUG("Freeing the chunk 0x%lx", offset);
+				break;
+			default:
+				EMGD_DEBUG("Unknown usage %d for chunk 0x%lx.  Memory manager corrupt?",
+					chunk->usage, offset);
+				break;
+			}
+
+			/*
+			 * What to do if the ref count is > 0?  Unmapping is
+			 * probably the right thing since nothing should try
+			 * to use this. If something does, it should probably
+			 * fail.
+			 */
+			if (chunk->ref_cnt > 0 && chunk->addr) {
+				EMGD_DEBUG("WARNING: The chunk 0x%lx is mapped", offset);
+				/* chunk->addr will be freed by gmm_shutdown */
+			}
+
+
+			/* Free the array of page address, if applicable: */
+			if (chunk->page_addresses != NULL) {
+				EMGD_DEBUG("About to free chunk->page_addresses = 0x%p",
+					chunk->page_addresses);
+				OS_FREE(chunk->page_addresses);
+				chunk->page_addresses = NULL;
+			}
+
+			chunk->usage = FREE_ALLOCATED;  /* mark as free */
+			return;
+		}
+		chunk = chunk->next;
+	}
+
+	EMGD_ERROR("gmm_free() did not find the chunk 0x%lx to free", offset);
+	return;
+}
+
+static void gmm_release_import(unsigned long offset)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_DEBUG("Enter gmm_release_import(0x%lx)", offset);
+
+	/* Walk the chunk list */
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		if (chunk->offset == offset) {
+			switch (chunk->usage) {
+			case FREE_ALLOCATED:
+			case INUSE_ALLOCATED:
+				EMGD_DEBUG("WARNING: The chunk 0x%lx was not an imported chunk", offset);
+				break;
+			case INUSE_IMPORTED:
+				EMGD_DEBUG("Releasing the chunk 0x%lx", offset);
+				break;
+			case FREE_IMPORTED:
+				EMGD_DEBUG("WARNING: The chunk 0x%lx has already been released", offset);
+				return;
+			default:
+				EMGD_DEBUG("Unknown usage %d for chunk 0x%lx.  Memory manager corrupt?",
+					chunk->usage, offset);
+				break;
+			}
+
+			/*
+			 * What to do if the ref count is > 0?  Unmapping is
+			 * probably the right thing since nothing should try
+			 * to use this. If something does, it should probably
+			 * fail.
+			 */
+			if (chunk->ref_cnt > 0) {
+				EMGD_DEBUG("WARNING: The chunk 0x%lx is mapped", offset);
+				chunk->ref_cnt = 0;
+				vunmap(chunk->addr);
+				chunk->addr = NULL;
+			}
+			/* Free the array of page address, if applicable: */
+			if (chunk->page_addresses != NULL) {
+				EMGD_DEBUG("About to free chunk->page_addresses = 0x%p",
+					chunk->page_addresses);
+				OS_FREE(chunk->page_addresses);
+				chunk->page_addresses = NULL;
+			}
+
+			/* Zero out the gmm_mem_buffer_t */
+			OS_MEMSET(chunk->gtt_mem, 0, sizeof(gmm_mem_buffer_t));
+
+			/* Mark address space as free */
+			chunk->usage = FREE_IMPORTED;
+			return;
+		}
+		chunk = chunk->next;
+	}
+
+	EMGD_ERROR("gmm_free() did not find the chunk 0x%lx to free", offset);
+	return;
+}
+
+static int gmm_alloc_region(unsigned long *offset,
+	unsigned long *size,
+	unsigned int type,
+	unsigned long flags)
+{
+	int ret;
+	unsigned long aligned_size;
+	unsigned long phys = 0;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: size=%lu, type=%d, flags=0x%lx", *size, type, flags);
+
+	*offset = 0;
+
+	switch(type) {
+	case IGD_GMM_REGION_TYPE_OVLREG:
+		flags |= IGD_GMM_REGION_ALIGN_MMAP;
+		phys = 1;
+		break;
+	case IGD_GMM_REGION_TYPE_OVLREG64:
+		flags |= IGD_GMM_REGION_ALIGN_64K;
+		phys = 1;
+		break;
+	case IGD_GMM_REGION_TYPE_HWSTATUS:
+		flags |= IGD_GMM_REGION_ALIGN_MMAP;
+		*size = 4096;
+		phys = 1;
+		break;
+	case IGD_GMM_REGION_TYPE_DMA:
+		flags |= IGD_GMM_REGION_ALIGN_MMAP;
+		break;
+	case IGD_GMM_REGION_TYPE_PERSISTENT:
+		flags |= IGD_GMM_REGION_ALIGN_MMAP;
+		break;
+	case IGD_GMM_REGION_TYPE_BPL:
+		flags |= IGD_GMM_REGION_ALIGN_MMAP;
+		phys = 1;
+		break;
+	case IGD_GMM_REGION_TYPE_CONTEXT:
+		flags |= IGD_GMM_REGION_ALIGN_CONTEXT | IGD_GMM_REGION_ALIGN_MMAP;
+		*size = 4096;
+		phys = 1;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid Region type requested: 0x%8.8x", type);
+		return -IGD_INVAL;
+	}
+
+	aligned_size = (*size + 4095) & ~4095;
+	EMGD_DEBUG("aligned_size=%lu", aligned_size);
+
+	do {
+		ret = gmm_alloc_chunk_space(&gmm_context, offset, aligned_size, phys,
+				flags);
+	} while ((ret == -IGD_ERROR_NOMEM) && gmm_flush_cache());
+
+	EMGD_DEBUG("EXIT  Returning %d", ret);
+	return ret;
+}
+
+static int gmm_get_num_surface(unsigned long *count)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_TRACE_ENTER;
+
+	/* Walk the chunk list */
+	chunk = gmm_context.head_chunk;
+	*count = 0;
+	while (chunk) {
+		(*count)++;
+		chunk = chunk->next;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+static int gmm_get_surface_list(unsigned long allocated_size,
+	unsigned long *list_size,
+	igd_surface_list_t **surface_list)
+{
+	gmm_chunk_t *chunk;
+	igd_surface_list_t *tmp_list;
+
+	EMGD_TRACE_ENTER;
+	gmm_get_num_surface(list_size);
+
+	if (*list_size > 0){
+		*surface_list = vmalloc(*list_size * sizeof(igd_surface_list_t));
+
+		/* Walk the chunk list */
+		chunk = gmm_context.head_chunk;
+		tmp_list = *surface_list;
+
+		while (chunk){
+			tmp_list->offset = chunk->offset;
+			tmp_list->size = chunk->size;
+
+			chunk = chunk->next;
+			tmp_list++;
+		}
+	}
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+static int gmm_alloc_surface(unsigned long *offset,
+	unsigned long pixel_format,
+	unsigned int *width,
+	unsigned int *height,
+	unsigned int *pitch,
+	unsigned long *size,
+	unsigned int type,
+	unsigned long *flags)
+{
+	int ret;
+	unsigned long phys;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: pixel_format=0x%08lx,", pixel_format);
+	EMGD_DEBUG("  width=%u, height=%u", *width, *height);
+	EMGD_DEBUG("  pitch=%u, type=%d, flags=0x%08lx", *pitch, type, *flags);
+
+	*offset = 0;
+	*size = 0;
+	if (! (*flags & IGD_MIN_PITCH)) {
+		*pitch = 0;
+	}
+
+	if (*flags & IGD_SURFACE_CURSOR) {
+		phys = 1;
+	} else {
+		phys = 0;
+	}
+
+	ret = gmm_alloc_linear_surface(offset, pixel_format, width, height, pitch,
+			size, type, *flags, phys);
+
+	EMGD_DEBUG("EXIT  Returning %d", ret);
+	return ret;
+}
+
+
+/*
+ * Given an offset, find the chunk and return the physical address.
+ */
+static int gmm_virt_to_phys(unsigned long offset,
+	unsigned long *physical)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Looking for offset=0x%lx", offset);
+
+	/* Walk the chunk list */
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		if (chunk->offset == offset) {
+			*physical = chunk->gtt_mem->physical;
+			EMGD_DEBUG("Physical address = 0x%08lx", *physical);
+			EMGD_TRACE_EXIT;
+			return 0;
+		}
+		chunk = chunk->next;
+	}
+
+	/* offset not found */
+	EMGD_ERROR_EXIT("Did not find offset (0x%lx); returning %d",
+		offset, -IGD_ERROR_NOMEM);
+	return -IGD_ERROR_NOMEM;
+}
+
+
+static int gmm_flush_cache(void)
+{
+	EMGD_DEBUG("Enter gmm_flush_cache(), which is stubbed");
+	return 0;
+}
+
+static void gmm_save(igd_context_t *context, void **state)
+{
+	EMGD_DEBUG("Enter gmm_save(), which is stubbed");
+	return;
+}
+
+static void gmm_restore(igd_context_t *context, void *state)
+{
+	EMGD_DEBUG("Enter gmm_restore(), which is stubbed");
+	return;
+}
+
+/*
+ * Create a virtual address mapping for a block of video memory.
+ */
+static void *gmm_map(unsigned long offset)
+{
+	gmm_chunk_t *chunk;
+	struct page **page_map;
+	int i;
+	void *addr = NULL;
+	unsigned long num_pages;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameter: offset=0x%lx", offset);
+
+	chunk = gmm_get_chunk(gmm_context.context, offset);
+
+	if (chunk == NULL) {
+		printk(KERN_ERR"[EMGD] gmm_map: Failed to find chunk: 0x%lx\n", offset);
+		return NULL;
+	}
+
+	/*
+	 * Check if this as been mapped already and return that map instead
+	 * of remapping it.
+	 */
+	chunk->ref_cnt++;
+	if (chunk->addr) {
+		EMGD_DEBUG("This chunk is already mapped!");
+		return chunk->addr;
+	}
+
+	/*
+	 * Read the physical addresses of the allocation from the GTT
+	 * and convert that to a page list.
+	 */
+
+	num_pages = chunk->gtt_mem->page_count;
+	page_map = vmalloc(num_pages * sizeof(struct page *));
+	if (page_map == NULL) {
+		printk(KERN_ERR"[EMGD] gmm_map: vmalloc failed.\n");
+		return NULL;
+	}
+
+	for (i = 0; i < num_pages; i++) {
+		page_map[i] = chunk->gtt_mem->pages[i];
+	}
+
+	addr = vmap(page_map, num_pages, VM_MAP, PAGE_KERNEL_UC_MINUS);
+
+	vfree(page_map);
+	chunk->addr = addr;
+
+	EMGD_DEBUG("Mapped address = 0x%p", addr);
+	EMGD_TRACE_EXIT;
+
+	return addr;
+}
+
+
+static void gmm_unmap(void *addr)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameter: addr=0x%p", addr);
+
+	/* Look up the chunk that was mapped to this address */
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		if (chunk->addr == addr) {
+			EMGD_DEBUG("The chunk with addr=0x%p has the offset = 0x%08lx", addr,
+				chunk->offset);
+			chunk->ref_cnt--;
+			if (chunk->ref_cnt == 0) {
+				EMGD_DEBUG("About to call vunmap(0x%p)", addr);
+				vunmap(addr);
+				chunk->addr = NULL;
+			}
+			return;
+		}
+		chunk = chunk->next;
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+int gmm_init(igd_context_t *context,
+	unsigned long scratch_mem,
+	unsigned long max_fb_mem)
+{
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: scratch_mem=0x%lx, max_fb_mem=%lu",
+		scratch_mem, max_fb_mem);
+
+	context->dispatch.gmm_alloc_surface = gmm_alloc_surface;
+	context->dispatch.gmm_alloc_region = gmm_alloc_region;
+	context->dispatch.gmm_import_pages = gmm_import_pages;
+	context->dispatch.gmm_virt_to_phys = gmm_virt_to_phys;
+	context->dispatch.gmm_free = gmm_free;
+	context->dispatch.gmm_release_import = gmm_release_import;
+	context->dispatch.gmm_memstat = NULL;
+	context->dispatch.gmm_alloc_cached = NULL;
+	context->dispatch.gmm_free_cached = NULL;
+	context->dispatch.gmm_alloc_cached_region = NULL;
+	context->dispatch.gmm_free_cached_region = NULL;
+	context->dispatch.gmm_flush_cache = gmm_flush_cache;
+	context->dispatch.gmm_alloc_reservation = NULL;
+	context->dispatch.gmm_alloc_heap = NULL;
+	context->dispatch.gmm_alloc_heap_block = NULL;
+	context->dispatch.gmm_free_heap_block = NULL;
+	context->dispatch.gmm_get_heap_from_block = NULL;
+	context->dispatch.gmm_get_pvtheap_size = NULL;
+	context->dispatch.gmm_get_cache_mem = NULL;
+	context->dispatch.gmm_alloc_persistent_region = NULL;
+	context->dispatch.gmm_free_persistent_region = NULL;
+	context->dispatch.gmm_map = gmm_map;
+	context->dispatch.gmm_unmap = gmm_unmap;
+	context->dispatch.gmm_get_page_list = gmm_get_page_list;
+	context->dispatch.gmm_get_num_surface = gmm_get_num_surface;
+	context->dispatch.gmm_get_surface_list = gmm_get_surface_list;
+	context->dispatch.gmm_map_ci = gmm_map_ci;
+	context->dispatch.gmm_unmap_ci = gmm_unmap_ci;
+
+	context->mod_dispatch.gmm_save = gmm_save;
+	context->mod_dispatch.gmm_restore = gmm_restore;
+
+	gmm_context.context = context;
+	gmm_context.head_chunk = NULL;
+	gmm_context.tail_chunk = NULL;
+
+	/* Reserve memory for framebuffer ??? */
+
+	EMGD_DEBUG("EXIT  Returning %d", 0);
+	return 0;
+}
+
+
+void gmm_shutdown(igd_context_t *context)
+{
+	gmm_chunk_t *chunk, *del;
+	struct drm_device *dev;
+
+	EMGD_TRACE_ENTER;
+
+	dev = (struct drm_device *)context->drm_dev;
+
+	/* Walk the chunk list */
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		EMGD_DEBUG("process chunk at 0x%lx", chunk->offset);
+		if (chunk->usage == INUSE_ALLOCATED || chunk->usage == INUSE_IMPORTED) {
+			EMGD_ERROR("Chunk at 0x%lx not properly freed", chunk->offset);
+		}
+
+		if (chunk->addr != NULL) {
+			vunmap(chunk->addr);
+		}
+
+		if (chunk->bound) {
+			emgd_gtt_remove(context, chunk->gtt_mem, chunk->offset);
+		}
+
+		if (chunk->usage == INUSE_ALLOCATED || chunk->usage == FREE_ALLOCATED) {
+			emgd_free_pages(chunk->gtt_mem);
+		}
+
+		/* Free the array of page address, if applicable: */
+		if (chunk->page_addresses != NULL) {
+			EMGD_DEBUG("About to free chunk->page_addresses = 0x%p",
+				chunk->page_addresses);
+			OS_FREE(chunk->page_addresses);
+		}
+
+		/* Free the chunk */
+		del = chunk;
+		chunk = chunk->next;
+		OS_FREE(del);
+	}
+
+	EMGD_TRACE_EXIT;
+	return;
+}
+
+gmm_chunk_t *gmm_get_chunk(igd_context_t *context, unsigned long offset)
+{
+	gmm_chunk_t *chunk;
+
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		if (chunk->offset == offset) {
+			return chunk;
+		}
+		chunk = chunk->next;
+	}
+
+	printk(KERN_ERR "[EMGD] gmm_get_chunk: Failed to find chunk 0x%lx\n",
+		offset);
+	return NULL;
+}
+
+
+
+static int gmm_alloc_linear_surface(unsigned long *offset,
+	unsigned long pixel_format,
+	unsigned int *width,
+	unsigned int *height,
+	unsigned int *pitch,
+	unsigned long *size,
+	unsigned long type,
+	unsigned long flags,
+	unsigned long phys)
+{
+	int ret;
+	unsigned long align;
+	unsigned long min_pitch;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: pixel_format=0x%08lx,", pixel_format);
+	EMGD_DEBUG("  width=%u, height=%u", *width, *height);
+	EMGD_DEBUG("  pitch=%u, size=%lu, type=%lu", *pitch, *size, type);
+	EMGD_DEBUG("  flags=0x%08lx; phys=%lu", flags, phys);
+
+	/* Validate surface */
+	if (! *width) {
+		*width = 1;
+	}
+
+	if (! *height) {
+		*height = 1;
+	}
+
+	/* Set the minimum surface pitch */
+	min_pitch = (IGD_PF_DEPTH(pixel_format) * *width) >> 3;
+	if (min_pitch < *pitch) {
+		min_pitch = *pitch;
+	}
+
+	/* Pitch for both PLB and TNC requires 64-byte alignment */
+	min_pitch = ALIGN(min_pitch, 64);
+
+	/*
+	 * Size should be based on pixel format and pitch, not just pitch.
+	 * For YUV surfaces, it is smaller than RGB surfaces.
+	 */
+	switch (IGD_PF_TYPE(pixel_format)) {
+	case PF_TYPE_YUV_PLANAR:
+		*size = min_pitch * (*height + (*height>>1));
+		break;
+	case PF_TYPE_YUV_PACKED:
+		/* FIXME: What should this really be? */
+		*size = min_pitch * *height;
+		break;
+	default:
+		*size = min_pitch * *height;
+		break;
+	}
+
+	*pitch = min_pitch;
+
+	/* Page align size */
+	align = (*size + 4095) & ~4095;
+
+	/*
+	 * Flags provide information on the type of surface being requested
+	 *   0x04 = cursor surface
+	 *   0x08 = overlay surface
+	 *   0x10 = display surface
+	 *   0x40 = video surface
+	 */
+	do {
+		ret = gmm_alloc_chunk_space(&gmm_context, offset, *size, phys, flags);
+	} while ((ret == -IGD_ERROR_NOMEM) && gmm_flush_cache());
+
+	EMGD_DEBUG("EXIT  Returning %d", ret);
+	return ret;
+}
+
+
+
+
+/*
+ * gmm_contig_page_list(): Create the page list for a previously-allocated 
+ * block of contiguous memory. (This is needed for GTT insertion, and normally
+ * created by the emgd_alloc_pages() function.)
+ */
+static gmm_mem_buffer_t *gmm_contig_page_list(unsigned long num_pages,
+			unsigned long phys_addr)
+{
+	gmm_mem_buffer_t *mem;
+	size_t list_size;
+	int i;
+	void *virt_addr = phys_to_virt(phys_addr);
+
+	mem = (gmm_mem_buffer_t *)kzalloc(sizeof(gmm_mem_buffer_t), GFP_KERNEL);
+	if (mem == NULL) {
+		printk(KERN_ERR "[EMGD] Cannot allocate gmm_mem_buffer_t ");
+		EMGD_ERROR_EXIT("Returning NULL\n");
+		return NULL;
+	}
+
+	/* First allocate page array */
+	list_size = num_pages * sizeof(struct page *);
+	mem->vmalloc_flag = false;
+
+	if (list_size <= (2 * PAGE_SIZE)) {
+		mem->pages = kmalloc(list_size, GFP_KERNEL | __GFP_NORETRY);
+	}
+
+	if (mem->pages == NULL) {
+		mem->pages = vmalloc(list_size);
+		mem->vmalloc_flag = true;
+	}
+
+	if (mem->pages == NULL) {
+		kfree(mem);
+		printk(KERN_ERR "Failed to allocate memory info struct.\n");
+		EMGD_ERROR_EXIT("Returning NULL\n");
+		return NULL;
+	}
+
+	mem->pages[0] = virt_to_page(virt_addr);
+	if (num_pages > 1) {
+		for (i = 1; i < num_pages; i++) {
+			mem->pages[i] = mem->pages[i-1] + 1;
+		}
+	}
+	mem->physical = page_to_phys(mem->pages[0]);
+	mem->page_count = num_pages;
+	
+	return mem;
+}
+
+/*
+ * gmm_map_contig_buffer(): Map a previously-allocated contiguous SDRAM memory
+ * block into a graphics-accessible memory.
+ */
+
+static int gmm_map_contig_buffer(gmm_context_t *gmm_context,
+		unsigned long phys_addr,
+		unsigned long size,
+		unsigned long *offset)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Check for a free contiguous chunk of sufficent size */
+	chunk = gmm_context->head_chunk;
+
+	/* Allocate a new chunk list element */
+	chunk = (gmm_chunk_t *)OS_ALLOC(sizeof(gmm_chunk_t));
+	if (!chunk) {
+		printk(KERN_ERR "[EMGD] Cannot allocate gmm_chunk_t element");
+		EMGD_ERROR_EXIT("Returning %d", -IGD_ERROR_NOMEM);
+		return -IGD_ERROR_NOMEM;
+	}
+	OS_MEMSET(chunk, 0, sizeof(gmm_chunk_t));
+
+	/* Contiguous memory is needed, so set the type to AGP_PHYS_MEMORY */
+	chunk->size = size;
+	chunk->pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
+	chunk->type = AGP_PHYS_MEMORY;
+
+	/* Create the GTT page list for this contiguous memory block */
+	chunk->gtt_mem = gmm_contig_page_list(chunk->pages, phys_addr); 
+	if (chunk->gtt_mem == NULL) {
+		printk(KERN_ERR "[EMGD] Cannot allocate gmm_chunk_t element");
+		EMGD_ERROR_EXIT("Returning %d", -IGD_ERROR_NOMEM);
+		return -IGD_ERROR_NOMEM;
+	}
+
+	/* Assign the specified memory block to this chunk */
+	chunk->used = 1;
+	chunk->ref_cnt = 0;
+	chunk->page_addresses = NULL;
+
+	/* Determine the offset value for this chunk */
+	if (gmm_context->tail_chunk == NULL) {
+		
+		chunk->offset = 0;
+		
+	} else {
+		chunk->offset = gmm_context->tail_chunk->offset +
+			gmm_context->tail_chunk->size;
+		
+		
+	}
+
+
+	/* Adjust the offset since display surfaces require 256KB alignment */
+	chunk->offset = (chunk->offset + 0x3ffff) & ~0x3ffff;
+	
+	/* Insert this chunk in the list */
+	chunk->next = NULL;
+	if (gmm_context->head_chunk == NULL) {
+		
+		gmm_context->head_chunk = chunk;
+	} else {
+		gmm_context->tail_chunk->next = chunk;
+	}
+	gmm_context->tail_chunk = chunk;
+
+	/* Now update the GTT so the display HW can access this memory */
+	emgd_gtt_insert(gmm_context->context, chunk->gtt_mem, chunk->offset);
+
+	/* Bind the gart memory to the offset */
+	chunk->bound = 1;
+
+	/* For contiguous pages, physical is the address of the first allocated page */
+	if (chunk->gtt_mem->physical == 0x0) {
+		chunk->gtt_mem->physical = page_to_phys(chunk->gtt_mem->pages[0]);
+	}
+
+	/* Return the offset associated with this contiguous block */	
+	*offset = chunk->offset;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+/*
+ * gmm_map_to_graphics(): Facilitates direct display of contiguous video input
+ * buffers by mapping the specified block into the "graphics aperture" via the 
+ * GTT.
+ */
+int gmm_map_to_graphics(unsigned long phys_addr,	
+	unsigned long size,		
+	unsigned long *offset)	
+{
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	if (phys_addr && size) {
+		ret = gmm_map_contig_buffer(&gmm_context, phys_addr, size,
+			offset);
+		
+	} else {
+		printk(KERN_ERR "Invalid address (0x%lx) and/or size (0x%lx) !",
+			phys_addr, size);
+		printk(KERN_ERR "EXIT  Returning %d", -EINVAL);
+		ret = -EINVAL;
+	}
+	
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+/*
+ * find gtt_offset and virtual address from ci_surface list according to the same v4l2_offset
+ */
+
+static int gmm_map_ci(unsigned long *gtt_offset,
+			unsigned long ci_param,	/* virtaddr or v4l2_offset */
+			unsigned long *virt_addr,
+			unsigned int map_method,
+			unsigned long size)
+
+{
+	unsigned char i;
+	int ret;
+
+	if(map_method){		
+		ret = gmm_map_to_graphics(virt_to_phys((unsigned long *)ci_param),size,gtt_offset);
+		if(ret)
+			return ret;
+		else{
+			for(i=0;i<MAX_CI_LIST_SIZE;i++){
+				
+				if(!ci_surfaces[i].used){
+					
+					ci_surfaces[i].used = 1;
+					ci_surfaces[i].virt = ci_param;
+					ci_surfaces[i].size = size;
+					ci_surfaces[i].gtt_offset = *gtt_offset;
+					*virt_addr = ci_param;				
+					break;
+				}		
+			}
+		}
+	}
+	else{
+		
+		for(i=0;i<MAX_CI_LIST_SIZE;i++){
+			if(ci_surfaces[i].used && (ci_surfaces[i].v4l2_offset ==ci_param)){
+				
+				*gtt_offset = ci_surfaces[i].gtt_offset;
+				*virt_addr = ci_surfaces[i].virt;			
+				break;
+			}
+		}
+	}
+	return 0;
+}
+
+
+/*
+ * gmm_unmap_contig_buffer(): Un-map a previously-allocated contiguous SDRAM 
+ * memory block into graphics memory.
+ */
+
+static int gmm_unmap_contig_buffer(gmm_context_t *gmm_context,
+		unsigned long offset,
+		unsigned long size)
+{
+	
+	gmm_chunk_t *chunk;
+#ifdef GMM_CI_CLEANUP
+ 	gmm_chunk_t *del;
+#endif
+	EMGD_TRACE_ENTER;
+
+	/* Locate the specified chunk and mark it as unused */
+	chunk = gmm_context->head_chunk;
+	while (chunk) {
+		if ((chunk->used == 1) && (chunk->size >= size) &&
+			(chunk->type == AGP_PHYS_MEMORY) &&
+			chunk->offset == offset) {			
+
+			emgd_gtt_remove(gmm_context->context, chunk->gtt_mem, chunk->offset);
+
+			chunk->used =0;	
+#ifdef GMM_CI_CLEANUP			
+			kfree(chunk->gtt_mem);
+
+			/* Free the array of page address, if applicable: */
+			if (chunk->page_addresses != NULL) {
+				EMGD_DEBUG("About to free chunk->page_addresses = 0x%p",
+					chunk->page_addresses);
+				OS_FREE(chunk->page_addresses);
+			}
+
+			/* Free the chunk */
+			del = chunk;
+			chunk = chunk->next;
+			OS_FREE(del);
+#endif				
+			
+			EMGD_DEBUG("EXIT  Returning %d", 0);
+			return 0;
+		}
+		chunk = chunk->next;
+	}
+	printk(KERN_ERR "Buffer @ 0x%lx (size 0x%lu) not found !", offset, size);
+	printk(KERN_ERR "EXIT  Returning %d", -EINVAL);
+	EMGD_TRACE_EXIT;
+	return -EINVAL;
+}
+
+
+/*
+ * gmm_unmap_from_graphics(): Disables direct display of DMA video input buffers
+ * by unmapping the specified block from the "graphics aperture" via the GTT.
+ */
+int gmm_unmap_from_graphics(unsigned long offset, unsigned long size)
+{
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	if (offset && size) {
+		/* Mark the GTT chunk as currently unused */
+		ret = gmm_unmap_contig_buffer(&gmm_context, offset, size);
+	} else {
+		printk(KERN_ERR "Invalid offset (0x%lx) and/or size (0x%lx) !",
+			offset, size);
+		printk(KERN_ERR "EXIT  Returning %d", -EINVAL);
+		ret = -EINVAL;
+	}
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+EXPORT_SYMBOL(gmm_unmap_from_graphics);
+
+
+/*
+ * Maintain a very simple linear linked list of memory allocations. Try
+ * to re-use freed blocks.  No error checking is done and alignment is
+ * hard codeded.
+ */
+
+static int gmm_alloc_chunk_space(gmm_context_t *gmm_context,
+		unsigned long *offset,
+		unsigned long size,
+		unsigned long phys,
+		unsigned long flags)
+{
+	gmm_chunk_t *chunk;
+	struct drm_device *dev;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: size=%lu; phys=%lu", size, phys);
+	EMGD_DEBUG("  flags=0x%08lx", flags);
+
+	/* Check for a free chunk of sufficent size */
+	chunk = gmm_context->head_chunk;
+	while (chunk) {
+		if ((chunk->usage == FREE_ALLOCATED) && (chunk->size >= size) &&
+			(chunk->type == (phys ? AGP_PHYS_MEMORY : AGP_NORMAL_MEMORY))) {
+			chunk->usage = INUSE_ALLOCATED;
+			*offset = chunk->offset;
+			EMGD_DEBUG("Re-using old chunk with offset=0x%lx", chunk->offset);
+			EMGD_DEBUG("EXIT  Returning %d", 0);
+			return 0;
+		}
+		chunk = chunk->next;
+	}
+
+	/* Allocate a new chunk */
+	chunk = (gmm_chunk_t *)OS_ALLOC(sizeof(gmm_chunk_t));
+	if (!chunk) {
+		printk(KERN_ALERT "[EMGD] Cannot allocate gmm_chunk_t");
+		EMGD_ERROR_EXIT("Returning %d", -IGD_ERROR_NOMEM);
+		return -IGD_ERROR_NOMEM;
+	}
+	OS_MEMSET(chunk, 0, sizeof(gmm_chunk_t));
+
+	/*
+	 * First allocate the memory from the gart driver. If this failes,
+	 * don't bother allocating a new chunk.
+	 */
+	dev = (struct drm_device *)gmm_context->context->drm_dev;
+	chunk->size = size;
+	chunk->pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
+	/*
+	 * If we need phyical contiguous memory, then we need to
+	 * set the type to AGP_PHYS_MEMORY, otherwise use AGP_NORMAL_MEMORY
+	 */
+	if (phys) {
+		chunk->type = AGP_PHYS_MEMORY;
+		EMGD_DEBUG("Allocate AGP_PHYS; size = 0x%08lx", chunk->size);
+	} else {
+		chunk->type = AGP_NORMAL_MEMORY;
+		EMGD_DEBUG("Allocate AGP_NORMAL; size = 0x%08lx", chunk->size);
+	}
+
+	if (dev == NULL) {
+		OS_FREE(chunk);
+		EMGD_ERROR_EXIT("drm device is NULL; Returning %d", -IGD_ERROR_NOMEM);
+		return -IGD_ERROR_NOMEM;
+	}
+
+
+	/* Allocate memory from the AGPGART */
+	chunk->gtt_mem = emgd_alloc_pages(chunk->pages, chunk->type);
+	if (!chunk->gtt_mem) {
+		OS_FREE(chunk);
+		printk(KERN_ALERT "[EMGD] Failed to allocated AGP memory.\n");
+		EMGD_DEBUG("gmm_alloc_chunk_space() returning %d", -IGD_ERROR_NOMEM);
+		return -IGD_ERROR_NOMEM;
+	}
+
+
+	chunk->usage = INUSE_ALLOCATED;
+	chunk->ref_cnt = 0;
+	chunk->page_addresses = NULL;
+
+	/*
+	 * Get the next available offset that works for this allocation.
+	 * Currently this just uses the next linear offset available. No
+	 * attempt is made to keep track of or utilize gaps introduced
+	 * because of alignments.
+	 *
+	 * Eventually, this should mainting different "heaps" of offsets
+	 * for different types of allocations.  For example, display vs.
+	 * video.
+	 *
+	 * See igd_gmm.h for the different surface types supported. Below
+	 * are the ones of interest
+	 *
+	 * #define IGD_SURFACE_RENDER           0x00000001
+	 * #define IGD_SURFACE_CURSOR           0x00000004
+	 * #define IGD_SURFACE_OVERLAY          0x00000008
+	 * #define IGD_SURFACE_DISPLAY          0x00000010
+	 * #define IGD_SURFACE_VIDEO            0x00000040
+	 * #define IGD_SURFACE_VIDEO_ENCODE     0x00000080
+	 * #define IGD_SURFACE_SYS_MEM          0x00008000
+	 * #define IGD_SURFACE_PHYS_PTR         0x00010000
+	 *
+	 */
+	if (gmm_context->tail_chunk == NULL) {
+		chunk->offset = 0;
+	} else {
+		chunk->offset = gmm_context->tail_chunk->offset +
+			gmm_context->tail_chunk->size;
+	}
+	EMGD_DEBUG("- Before alignment: offset=0x%lx", chunk->offset);
+
+	/*
+	 * Alignment varies depending on the type of surface being allocated.
+	 */
+	if (flags & IGD_SURFACE_DISPLAY) {
+		/* 256KB aligned */
+		chunk->offset = (chunk->offset + 0x3ffff) & ~0x3ffff;
+	} else {
+		/* 4KB aligned */
+		chunk->offset = (chunk->offset + 0x0fff) & ~0x0fff;
+	}
+
+	EMGD_DEBUG("-  After alignment: offset=0x%lx", chunk->offset);
+	chunk->next = NULL;
+
+	if (gmm_context->head_chunk == NULL) {
+		gmm_context->head_chunk = chunk;
+	} else {
+		gmm_context->tail_chunk->next = chunk;
+	}
+	gmm_context->tail_chunk = chunk;
+
+	/* Bind the gart memory to the offset */
+	/*
+	 * This updates the GTT table with the actual allocated pages
+	 * so the display hardware can access the memory.
+	 *
+	 * TODO: Add ability to use other MMU's depending on the
+	 * type of memory requested.
+	 */
+	emgd_gtt_insert(gmm_context->context, chunk->gtt_mem, chunk->offset);
+	chunk->bound = 1;
+
+	/*
+	 * Physical is only meaningfull for single page or contiguous pages.
+	 * It represents the physical address of the first allocated page.
+	 */
+	if (chunk->gtt_mem->physical == 0x0) {
+		chunk->gtt_mem->physical = page_to_phys(chunk->gtt_mem->pages[0]);
+	}
+
+	*offset = chunk->offset;
+
+	EMGD_DEBUG("Allocated chunk @ 0x%lx (0x%lx)", chunk->offset,
+		(unsigned long)chunk->gtt_mem->physical);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+/*
+ * Imports a list of pages allocated by an external source (i.e., the PVR
+ * services) into the GMM and maps the pages into the GTT.  Note that
+ * this function is as dumb as gmm_alloc_chunk_space about reusing
+ * previous allocations that have been freed; it will happily use a large
+ * hole in the GTT for a tiny allocation if it's the first hole it finds.
+ *
+ * pagelist is a live page list; it should not be modified or freed by
+ *    the GMM.
+ * gtt_offset is an output only; this is the offset of the beginning of
+ *    the first page from the start of the GTT.  If the actual surface
+ *    data starts partway through a page, the caller may need to add an
+ *    addition offset to where the surface data starts.
+ */
+static int gmm_import_pages(void **pagelist,
+		unsigned long *gtt_offset,
+		unsigned long numpages)
+{
+	gmm_chunk_t *chunk;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Importing %lu pages into GTT\n", numpages);
+
+	/*
+	 * Check for a free chunk of sufficent size that does not have allocated
+	 * pages attached to it (i.e., a chunk from a previous import region that's
+	 * been freed.
+	 */
+	chunk = gmm_context.head_chunk;
+	while (chunk) {
+		if ((chunk->usage == FREE_IMPORTED) && (chunk->pages >= numpages))
+		{
+			chunk->usage = INUSE_ALLOCATED;
+			EMGD_DEBUG("Re-using old chunk with offset=0x%lx", chunk->offset);
+			break;
+		}
+		chunk = chunk->next;
+	}
+
+	/* Allocate a new chunk if we didn't find any that we could reuse */
+	if (!chunk) {
+		chunk = (gmm_chunk_t *)OS_ALLOC(sizeof(gmm_chunk_t));
+		if (!chunk) {
+			printk(KERN_ALERT "[EMGD] Cannot allocate gmm_chunk_t");
+			EMGD_ERROR_EXIT("Returning %d", -IGD_ERROR_NOMEM);
+			return -IGD_ERROR_NOMEM;
+		}
+		OS_MEMSET(chunk, 0, sizeof(gmm_chunk_t));
+
+		chunk->pages = numpages;
+		chunk->size = numpages * PAGE_SIZE;
+		chunk->next = NULL;
+
+		/* Create a gmm_mem_buffer_t for the imported memory */
+		chunk->gtt_mem = OS_ALLOC(sizeof(gmm_mem_buffer_t));
+		if (chunk->gtt_mem == NULL) {
+			OS_FREE(chunk);
+			return -IGD_ERROR_NOMEM;
+		}
+
+		/* Stick this chunk after all other GTT chunks */
+		if (gmm_context.tail_chunk == NULL) {
+			/* First chunk ever! */
+			gmm_context.head_chunk = chunk;
+			chunk->offset = 0;
+		} else {
+			chunk->offset = gmm_context.tail_chunk->offset +
+				gmm_context.tail_chunk->size;
+			gmm_context.tail_chunk->next = chunk;
+		}
+		gmm_context.tail_chunk = chunk;
+
+		/*
+		 * Since we're making this a displayable surface, we need to make sure
+		 * it's 256k-aligned.
+		 */
+		chunk->offset = (chunk->offset + 0x3ffff) & ~0x3ffff;
+
+		EMGD_DEBUG("Setting up a new GMM chunk for imported pages");
+	}
+
+	*gtt_offset = chunk->offset;
+
+	chunk->usage = INUSE_IMPORTED;
+	chunk->ref_cnt = 0;
+	chunk->page_addresses = NULL;
+
+	/*
+	 * Note that the underlying gmm_mem_buffer may have a smaller size and
+	 * number of pages if we're reusing a larger chunk than we really needed.
+	 */
+	chunk->gtt_mem->size = numpages * PAGE_SIZE;
+	chunk->gtt_mem->pages = (struct page**)pagelist;
+	chunk->gtt_mem->page_count = numpages;
+
+	/*
+	 * These fields should never be needed since responsibility for actually
+	 * freeing these pages and the page list itself lies with the external
+	 * code that allocated the pages.
+	 */
+	chunk->type = AGP_NORMAL_MEMORY;
+	chunk->gtt_mem->type = AGP_NORMAL_MEMORY;
+	chunk->gtt_mem->vmalloc_flag = 0;
+
+	/*
+	 * This updates the GTT table with the actual imported pages
+	 * so the display hardware can access the memory.
+	 */
+	emgd_gtt_insert(gmm_context.context, chunk->gtt_mem, chunk->offset);
+	chunk->bound = 1;
+
+	/*
+	 * Physical is only meaningfull for single page or contiguous pages.
+	 * It represents the physical address of the first allocated page.
+	 */
+	if (chunk->gtt_mem->physical == 0x0) {
+		chunk->gtt_mem->physical = page_to_phys(chunk->gtt_mem->pages[0]);
+	}
+
+	EMGD_DEBUG("Imported chunk @ 0x%lx (0x%lx)", chunk->offset,
+		(unsigned long)chunk->gtt_mem->physical);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+static int gmm_get_page_list(unsigned long offset,
+		unsigned long **pages,
+		unsigned long *page_cnt)
+{
+	gmm_chunk_t *chunk;
+	int i;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: offset=0x%08lx", offset);
+	EMGD_DEBUG("  pages=0x%p, *pages=0x%p", pages, *pages);
+	chunk = gmm_get_chunk(gmm_context.context, offset);
+
+	if (chunk == NULL) {
+		printk(KERN_ERR"[EMGD] gmm_get_page_list: Failed to find chunk: "
+			"0x%lx\n", offset);
+		return -IGD_ERROR_NOMEM;
+	}
+
+	*page_cnt = chunk->gtt_mem->page_count;
+
+	/* Allocate an array of page addresses: */
+	if (chunk->page_addresses == NULL) {
+		chunk->page_addresses = OS_ALLOC(sizeof(unsigned long) * *page_cnt);
+		EMGD_DEBUG("Allocated chunk->page_addresses = 0x%p",
+			chunk->page_addresses);
+		if (chunk->page_addresses == NULL) {
+			printk(KERN_ERR "[EMGD] gmm_get_page_list: failed to allocate the "
+				"array of page addresses for chunk: 0x%lx\n", offset);
+			return -IGD_ERROR_NOMEM;
+		}
+	} else {
+		EMGD_DEBUG("Re-using chunk->page_addresses = 0x%p",
+			chunk->page_addresses);
+	}
+
+	/* Populate the array with the starting addresses of the pages: */
+	for (i = 0; i < *page_cnt; i++) {
+		chunk->page_addresses[i] = page_to_phys(chunk->gtt_mem->pages[i]);
+	}
+
+	*pages = chunk->page_addresses;
+
+	EMGD_DEBUG("*pages=0x%p", *pages);
+	EMGD_DEBUG("page_count=%lu", *page_cnt);
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+struct emgd_ci_meminfo_t {
+	unsigned long v4l2_offset;
+	unsigned long virt;
+	unsigned long  size;
+	};
+
+int emgd_map_ci_buf(struct emgd_ci_meminfo_t * ci_meminfo)
+{
+	int ret;
+	unsigned long gtt_offset;
+	unsigned char i;
+	ret = gmm_map_to_graphics(virt_to_phys((unsigned long *)ci_meminfo->virt), ci_meminfo->size, &gtt_offset);
+	if(ret)
+	{
+		return ret;/*error handling*/
+	}
+	/* save meminfo into our context */
+	for(i=0;i<MAX_CI_LIST_SIZE;i++){
+		if(!ci_surfaces[i].used){
+			ci_surfaces[i].used = 1;			
+			ci_surfaces[i].virt = virt_to_phys((unsigned long *)ci_meminfo->virt);
+			ci_surfaces[i].size =  ci_meminfo->size;
+			ci_surfaces[i].gtt_offset =  gtt_offset;
+			return 0;
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL(emgd_map_ci_buf);
+int emgd_unmap_ci_buf(unsigned long virt_addr)
+{
+	unsigned char i;
+	int ret;
+	for(i=0;i<MAX_CI_LIST_SIZE;i++)
+	{
+		if(ci_surfaces[i].used && (ci_surfaces[i].virt == virt_addr))
+			{
+				ret = gmm_unmap_from_graphics(ci_surfaces[i].gtt_offset, ci_surfaces[i].size);
+				ci_surfaces[i].used = 0;
+				ci_surfaces[i].gtt_offset = 0;
+				return 0;
+			}
+	}
+	printk(KERN_ERR"[gmm]ci unmap failed\n");
+	return 1;
+}
+
+EXPORT_SYMBOL(emgd_unmap_ci_buf);
+
+
+static int gmm_unmap_ci(unsigned long virt_addr)
+{
+	int ret;
+	ret =emgd_unmap_ci_buf(virt_addr);
+	return ret;
+}
+
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/gmm/gtt.c b/drivers/gpu/drm/emgd/emgd/gmm/gtt.c
new file mode 100644
index 0000000..f530ca6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/gmm/gtt.c
@@ -0,0 +1,436 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: gtt.c
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Manage the GTT.
+ *-----------------------------------------------------------------------------
+ */
+#include <igd_debug.h>
+#include <drmP.h>
+#include <memlist.h>
+#include <io.h>
+#include <memory.h>
+#include <asm/cacheflush.h>
+#include <linux/version.h>
+
+#define PFX "EMGD: "
+
+#define SCR1	0x71410 /* scratch register set by vbios indicating status*/
+#define SCR2	0x71418 /* scratch register set by vbios indicating amount of stolen memory */
+#define FW_ID	0xE1DF0000 /* firmware identifier */
+#define ST_BIT	0x00000004 /* bit2- stolen memory bit */
+#define PSB_PTE_VALID  0x0001
+
+void emgd_free_pages(gmm_mem_buffer_t *mem);
+
+static DEFINE_MUTEX(client_sem);
+static DEFINE_MUTEX(gtt_sem);
+
+struct client_list_struct {
+	struct list_head list;
+	struct vm_area_struct *vma;
+	pid_t pid;
+};
+
+static LIST_HEAD(client_list);
+
+static void ipi_handler(void *null) {
+	//flush_agp_cache(); /* from agp.h */
+	wbinvd();
+}
+
+static void emgd_cache_flush(void) {
+	if (on_each_cpu(ipi_handler, NULL, 1) != 0)
+		panic(PFX "timed out waiting for the other CPUs!\n");
+}
+
+static void tlb_flush(void) {
+	/* If needed, this would flush the SGX/MSVDX mmu TLB's */
+}
+
+/* FIXME - TURNS OUT THAT THIS FUNCTION IS CURRENTLY A NOOP, BECAUSE
+ * client_list NEVER HAS ANYTHING ADDED TO IT.
+ */
+static void invalidate_vma(unsigned long pg_offset, unsigned long bus_addr) {
+	int zap;
+	struct list_head *tmp;
+	struct client_list_struct *entry;
+	unsigned long addr_start=0;
+	unsigned long addr_end=0;
+	unsigned long addr_offset=0;
+	unsigned long vaddr;
+	pgd_t *pgd;
+	pud_t *pud;
+	pmd_t *pmd;
+	pte_t *pte;
+
+	mutex_lock(&client_sem);
+	list_for_each(tmp, &client_list) {
+		entry = list_entry(tmp, struct client_list_struct, list);
+
+		/*
+		 * We need to handle invalidating VMA's that are only mapping
+		 * a portion of the virtual aperture.  Calculate what if
+		 * any invalidated pages need to be zapped
+		 */
+		addr_start = (entry->vma->vm_pgoff << PAGE_SHIFT)
+			- bus_addr;
+		addr_end = addr_start + (entry->vma->vm_end - entry->vma->vm_start);
+		addr_offset = pg_offset << PAGE_SHIFT;
+
+		vaddr = entry->vma->vm_start + (addr_offset - addr_start);
+
+		zap=0;
+		pgd=NULL;
+		pud=NULL;
+		pmd=NULL;
+		pte=NULL;
+
+		/*
+		 * Look up page table entries for all VMAs that currently
+		 * have the virtual aperture mapped -- to see if the page
+		 * has ever faulted
+		 */
+		pgd = pgd_offset(entry->vma->vm_mm, vaddr);
+		if (!pgd_none(*pgd)) {
+			pud = pud_offset(pgd, vaddr);
+			if (!pud_none(*pud)) {
+				pmd = pmd_offset(pud, vaddr);
+				if (!pmd_none(*pmd)) {
+					pte = pte_offset_map(pmd, vaddr);
+					if (!pte_none(*pte)) {
+						zap=1;
+					}
+				}
+			}
+		}
+		/*
+		 * Only zap a page if it falls within the mapped region
+		 * and it has previously faulted
+		 */
+		if (zap && (addr_offset >= addr_start) &&
+				(addr_offset < addr_end)) {
+
+
+			if (!page_mapcount(pte_page(*pte))) {
+				printk(KERN_ERR "[EMGD] ERROR No mapcount\n");
+				printk(KERN_ALERT "[EMGD] ZR %p %08lX %d %d %p\n",
+						pte_page(*pte),
+						pte_page(*pte)->flags, page_count(pte_page(*pte)),
+						page_mapcount(pte_page(*pte)), pte_page(*pte)->mapping);
+			} else {
+				atomic_add_negative(-1, &pte_page(*pte)->_mapcount);
+				put_page(pte_page(*pte));
+#if  (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34))
+				dec_mm_counter(entry->vma->vm_mm, file_rss);
+#else
+				dec_mm_counter(entry->vma->vm_mm, MM_FILEPAGES);
+#endif
+			}
+
+			pte_clear(entry->vma->vm_mm, vaddr, pte);
+		}
+
+		if(pte) {
+			pte_unmap(pte);
+		}
+	}
+	mutex_unlock(&client_sem);
+}
+
+
+
+/*
+ * Allocate pages from the kernel and store in a page list.
+ */
+gmm_mem_buffer_t *emgd_alloc_pages(unsigned long num_pages, int type) {
+	gmm_mem_buffer_t *mem;
+	size_t list_size;
+	struct page *page;
+	int i;
+	int order;
+
+	mem = (gmm_mem_buffer_t *)kzalloc(sizeof(gmm_mem_buffer_t), GFP_KERNEL);
+	if (mem == NULL) {
+		return NULL;
+	}
+
+	/* First allocate page array */
+	list_size = num_pages * sizeof(struct page *);
+	mem->vmalloc_flag = false;
+
+	if (list_size <= (2 * PAGE_SIZE)) {
+		mem->pages = kmalloc(list_size, GFP_KERNEL | __GFP_NORETRY);
+	}
+
+	if (mem->pages == NULL) {
+		mem->pages = vmalloc(list_size);
+		mem->vmalloc_flag = true;
+	}
+
+	if (mem->pages == NULL) {
+		printk(KERN_ERR "[EMGD] Failed to allocate memory info struct.\n");
+		kfree(mem);
+		return NULL;
+	}
+
+	/*
+	 * If we need phyical contiguous memory, then do things differently.
+	 *   Call alloc_pages(GFP_KERNEL, pages) to allocate all the pages.
+	 *   The page structure returned is just the first page of the group.
+	 *   ? is it a virtual address ?
+	 *
+	 *   mem->pages[0] = virt_to_phys(page)
+	 *   mem->pages[1] = mem->pages[0] + PAGE_SIZE
+	 */
+
+	if ((type == 1) || (type == 0)) {
+	/* Next allocate the pages */
+		for (i = 0; i < num_pages; i++) {
+			page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
+			if (page == NULL) {
+				/* Error! */
+				printk(KERN_ERR "[EMGD] Memory allocation failure!\n");
+				if (mem->vmalloc_flag) {
+					vfree(mem->pages);
+				} else {
+					kfree(mem->pages);
+				}
+				kfree(mem);
+				return NULL;
+			}
+
+			/* Make sure this page isn't cached */
+			if (set_memory_uc((unsigned long) page_address(page), 1) < 0) {
+				printk(KERN_ERR "[EMGD] Unable to set page attributes for newly"
+					" allocated graphics memory.\n");
+				/* Rely on the fact that we've kept up the data structures: */
+				emgd_free_pages(mem);
+				/* XXX - THIS IS WHAT SOME OLD IEGD CODE DID--A GOOD IDEA??? */
+				set_memory_wb((unsigned long) page_address(page), 1);
+				__free_page(page);
+				return NULL;
+			}
+
+			get_page(page);
+			mem->pages[i] = page;
+			mem->page_count++;
+		}
+	} else {
+		if (num_pages == 1) {
+			order = 0;
+		} else if (num_pages == 4) {
+			order = 2;
+		} else if (num_pages == 8) {
+			order = 3;
+		} else {
+			printk(KERN_ERR "[EMGD] Page count is not valid for physical "
+				"allocation.\n");
+			if (mem->vmalloc_flag) {
+				vfree(mem->pages);
+			} else {
+				kfree(mem->pages);
+			}
+			kfree(mem);
+			return NULL;
+		}
+
+		page = alloc_pages(GFP_KERNEL, order);
+		if (page == NULL) {
+			/* Error! */
+			printk(KERN_ERR "[EMGD] Memory allocation failure!\n");
+			if (mem->vmalloc_flag) {
+				vfree(mem->pages);
+			} else {
+				kfree(mem->pages);
+			}
+			kfree(mem);
+			return NULL;
+		} else {
+			/* Make sure these pages aren't cached */
+			if (set_memory_uc((unsigned long) page_address(page),
+				num_pages) < 0) {
+				printk(KERN_ERR "[EMGD] Unable to set page attributes for newly"
+					" allocated physical graphics memory.\n");
+				/* XXX - THIS IS WHAT SOME OLD IEGD CODE DID--A GOOD IDEA??? */
+				set_memory_wb((unsigned long) page_address(page), num_pages);
+				__free_pages(page, num_pages);
+				if (mem->vmalloc_flag) {
+					vfree(mem->pages);
+				} else {
+					kfree(mem->pages);
+				}
+				kfree(mem);
+				return NULL;
+			}
+
+			get_page(page);
+			mem->pages[0] = page;
+			if (num_pages > 1) {
+				for (i = 1; i < num_pages; i++) {
+					mem->pages[i] = mem->pages[i-1] + 1;
+				}
+			}
+			mem->physical = page_to_phys(mem->pages[0]);
+			mem->page_count = num_pages;
+		}
+	}
+
+	return mem;
+}
+
+/*
+ * Free memory pages.
+ */
+void emgd_free_pages(gmm_mem_buffer_t *mem) {
+	int i;
+	struct page *page;
+
+	for (i = 0; i < mem->page_count; i++) {
+		page = mem->pages[i];
+		/* XXX - THIS IS WHAT SOME OLD IEGD CODE DID--A GOOD IDEA??? */
+		set_memory_wb((unsigned long) page_address(page), 1);
+		put_page(page);
+		__free_page(page);
+		mem->pages[i] = NULL;
+	}
+
+	if (mem->vmalloc_flag) {
+		vfree(mem->pages);
+	} else {
+		kfree(mem->pages);
+	}
+
+	kfree(mem);
+}
+
+
+/*
+ * Need a function to populate the GTT with the pages.
+ *
+ * The caller provides the offset into the GTT where the memory needs
+ * to go.  This simply needs to allocate the pages and insert them
+ * into the GTT.
+ */
+void emgd_gtt_insert(igd_context_t *context,
+		gmm_mem_buffer_t *mem,
+		unsigned long offset)
+{
+	struct page *page;
+	unsigned long pte;
+	unsigned long pg_off;
+	int i, j;
+
+	pg_off = offset >> PAGE_SHIFT;
+
+	/* Check that the offset is within the gtt's range */
+	if ((pg_off + mem->page_count) > context->device_context.gatt_pages) {
+		printk(KERN_ERR "[EMGD] Attempt to insert a offset beyond of GTT range.\n");
+		return;
+	}
+
+	/* Flush before inserting pages into the GTT */
+	emgd_cache_flush();
+	tlb_flush();
+
+
+	/* Insert the pages into the GTT */
+	mutex_lock(&gtt_sem);
+	for (i = 0, j = pg_off; i < mem->page_count; i++, j++) {
+		page = mem->pages[i];
+
+		/* Mark the page as valid */
+		pte = page_to_phys(page) | PSB_PTE_VALID;
+		writel(pte, (context->device_context.virt_gttadr + j));
+		readl(context->device_context.virt_gttadr + j);
+
+	}
+
+	mutex_unlock(&gtt_sem);
+
+	(void)readl(context->device_context.virt_gttadr + j - 1);
+
+	/* Invalidate VMA's */
+	invalidate_vma(j,
+		(context->device_context.gmch_ctl | PCI_BASE_ADDRESS_MEM_MASK));
+
+	/* Flush */
+	emgd_cache_flush();
+	tlb_flush();
+
+	return;
+}
+
+/*
+ * Need a function to remove pages from the GTT (and replace with the
+ * scratch page?) and free the pages.
+ */
+
+void emgd_gtt_remove(igd_context_t *context,
+		gmm_mem_buffer_t *mem,
+		unsigned long offset)
+{
+	struct page *page;
+	unsigned long pte;
+	int i;
+	unsigned long pg_start;
+
+	pg_start = offset >> PAGE_SHIFT;
+
+	/* Flush before inserting pages into the GTT */
+	emgd_cache_flush();
+	tlb_flush();
+
+	mutex_lock(&gtt_sem);
+
+	page = context->device_context.scratch_page;
+	pte = page_to_phys(page) | PSB_PTE_VALID;
+
+	/* Insert the scratch page into the GTT */
+	for (i = pg_start; i < (mem->page_count + pg_start); i++) {
+		/* FIXME: Apparently we don't really need to copy stolen memory pages.
+		 * If so, what should we do about the following code?  Is it correct to
+		 * do nothing?
+		 */
+		if (i < context->device_context.stolen_pages) {
+			/* This is stolen memory.... */
+		} else {
+			writel(pte, context->device_context.virt_gttadr + i);
+			(void)readl(context->device_context.virt_gttadr + i);
+		}
+
+	}
+
+	mutex_unlock(&gtt_sem);
+
+	/* Invaidate VMA's */
+	invalidate_vma(i,
+			(context->device_context.gmch_ctl | PCI_BASE_ADDRESS_MEM_MASK));
+
+	/* Flush */
+	emgd_cache_flush();
+	tlb_flush();
+}
diff --git a/drivers/gpu/drm/emgd/emgd/include/cmd.h b/drivers/gpu/drm/emgd/emgd/include/cmd.h
new file mode 100644
index 0000000..e1787ec
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/cmd.h
@@ -0,0 +1,47 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: cmd.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+
+#ifndef _CMD_H
+#define _CMD_H
+
+
+typedef struct _cmd_queue cmd_queue_t;
+
+int cmd_init(igd_context_t *context);
+
+
+#define CMD_CONTROL_ON   0x1
+#define CMD_CONTROL_OFF  0x2
+
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/context.h b/drivers/gpu/drm/emgd/emgd/include/context.h
new file mode 100644
index 0000000..9d54e6d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/context.h
@@ -0,0 +1,255 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: context.h
+ * $Revision: 1.22 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_CONTEXT_H
+#define _HAL_CONTEXT_H
+
+#include <pci.h>
+
+#include <igd.h>
+#include <igd_init.h>
+
+#define GET_DEVICE_POWER_STATE(d_h) \
+(((igd_display_context_t *)(d_h))->context->device_context.power_state)
+
+typedef void * platform_context_t;
+
+typedef struct _igd_context igd_context_t;
+
+struct _pd_reg;
+struct _igd_display_context;
+struct _igd_display_port;
+struct _pd_timing;
+struct _cmd_queue;
+
+/*
+ * Flags for reg_set_mod_state and reg_get_mode_state
+ */
+typedef enum _reg_state_id {
+	REG_MODE_STATE_REG = 1,
+	REG_MODE_STATE_CON = 2
+} reg_state_id_t;
+
+/* module_state handle */
+typedef struct _module_state *module_state_h;
+
+/*
+ * Intermodule-dispatch is for calling from one module to another. All
+ * functions should be considered optional and the caller should check
+ * for null before using.
+ */
+typedef struct _inter_module_dispatch {
+	struct _igd_param *init_params;
+
+	void (*gmm_save)(igd_context_t *context, void **state);
+	void (*gmm_restore)(igd_context_t *context, void *state);
+
+	void (*mode_reset_plane_pipe_ports)(igd_context_t *context);
+	void (*filter_modes)(igd_context_t *context,
+		struct _igd_display_port *port,
+		struct _pd_timing *in_list);
+
+	/* FIXME: This should be a dsp function */
+	unsigned long (*mode_get_gpio_sets)(unsigned long **gpio);
+
+	/* For Poulsbo, there is no flip pending hardware, so the
+	 * mode module and command module need to communicate */
+	int (*set_flip_pending)(unsigned char *mmio,
+		unsigned long pipe_status_reg);
+	int (*check_flip_pending)(unsigned char *mmio,
+		unsigned long pipe_status_reg);
+
+	int (*dsp_get_config_info)(igd_context_t *context,
+		igd_config_info_t *config_info);
+	int (*pi_get_config_info)(igd_context_t *context,
+		igd_config_info_t *config_info);
+
+	/* Widely used DSP interfaces */
+	struct _igd_display_port *(*dsp_get_next_port)(igd_context_t *context,
+		struct _igd_display_port *port, int reverse);
+	struct _igd_plane *(*dsp_get_next_plane)(igd_context_t *context,
+		struct _igd_plane *plane, int reverse);
+	struct _igd_display_pipe *(*dsp_get_next_pipe)(igd_context_t *context,
+		struct _igd_display_pipe *pipe, int reverse);
+	void (*dsp_get_display)(unsigned short port_number,
+		struct _igd_display_context **display,
+		struct _igd_display_port **port, int display_detect);
+	void (*dsp_get_dc)(unsigned long *dc,
+		struct _igd_display_context **primary,
+		struct _igd_display_context **secondary);
+	void (*dsp_get_planes_pipes)(struct _igd_plane **primary_display_plane,
+		struct _igd_plane **secondary_display_plane,
+		struct _igd_display_pipe **primary_pipe,
+		struct _igd_display_pipe **secondary_pipe);
+	int (*dsp_alloc)(igd_context_t *context, unsigned long dc,
+		unsigned long flags);
+	void (*dsp_control_plane_format)(int enable, int display_plane, struct _igd_plane *plane_override);
+
+	/* Flag to indicate FB Blend + Overlay override */
+	unsigned int fb_blend_ovl_override;
+
+	unsigned long *dsp_current_dc;
+	/* Firmware  programmed DC. This information needs to be
+     * accessible by the mode module.
+     */
+	unsigned long dsp_fw_dc;
+	
+	/* flag to tell if in dih_clone mode */
+	unsigned long in_dih_clone_mode;
+	unsigned long dih_clone_display;
+
+	struct _igd_display_port **dsp_port_list;
+	struct _igd_display_context **dsp_display_list;
+
+	/* i2c and GMBus interfaces */
+	int (*i2c_read_regs)(igd_context_t *context, unsigned long i2c_bus,
+		unsigned long i2c_speed, unsigned long dab, unsigned char reg,
+		unsigned char FAR *buffer, unsigned long num_bytes,
+		unsigned long flags);
+	int (*i2c_write_reg_list)(igd_context_t *context,
+		unsigned long i2c_bus, unsigned long i2c_speed, unsigned long dab,
+		struct _pd_reg *reg_list, unsigned long flags);
+	int (*gpio_write_regs)(igd_context_t *context,
+		struct _igd_display_port *port, unsigned long gpio_pin_pair,
+		unsigned long power_state);
+	int (*gpio_read_regs)(igd_context_t *context, unsigned long gpio_pin_pair,
+		unsigned long *data);
+
+	int (*reg_get_mod_state)(reg_state_id_t id, module_state_h **state,
+		unsigned long **flags);
+
+	/* Power Module Calls to Reg module */
+	void *(*reg_alloc)(igd_context_t *context, unsigned long flags);
+	void (*reg_free)(igd_context_t *context, void *reg_set);
+	int (*reg_save)(igd_context_t *context, void *reg_set);
+	int (*reg_restore)(igd_context_t *context, void *reg_set);
+	void (*reg_crtc_lut_get)(igd_context_t *context, void *emgd_crtc);
+	void (*reg_crtc_lut_set)(igd_context_t *context, void *emgd_crtc);
+
+	/* Reg Module callbacks */
+	int (*mode_save)(igd_context_t *context, module_state_h *state,
+		unsigned long *flags);
+	int (*mode_restore)(igd_context_t *context, module_state_h *state,
+		unsigned long *flags);
+
+	/* Power Module Callbacks */
+	int (*mode_pwr)(igd_context_t *context, unsigned long powerstate);
+	int (*overlay_pwr)(igd_context_t *context, unsigned long powerstate);
+	int (*msvdx_pwr)(igd_context_t *context, unsigned long powerstate);
+	int (*msvdx_status)(igd_context_t *context, unsigned long *queue_status, unsigned long *mtx_msg_status);
+
+	/* Shutdown functions for use by init module only */
+	void (*mode_shutdown)(igd_context_t *context);
+	void (*dsp_shutdown)(igd_context_t *context);
+	void (*pi_shutdown)(igd_context_t *context);
+	void (*shutdown_2d)(igd_context_t *context);
+	void (*blend_shutdown)(igd_context_t *context);
+	void (*interrupt_shutdown)(igd_context_t *context);
+	void (*appcontext_shutdown)(igd_context_t *context);
+	void (*reset_shutdown)(igd_context_t *context);
+	void (*pwr_shutdown)(igd_context_t *context);
+	void (*overlay_shutdown)(igd_context_t *context);
+	void (*cmd_shutdown)(igd_context_t *context);
+	void (*reg_shutdown)(igd_context_t *context);
+
+	/* Mode module Callbacks */
+	int (*get_dd_timing)(struct _igd_display_context *display,
+		struct _pd_timing *in_list);
+	int (*check_port_supported)(void *port_tmp);
+	int (*get_refresh_in_border)(struct _pd_timing *in_list);
+
+} inter_module_dispatch_t;
+
+/*
+ * IGD device context contains device information that should exist in
+ * some form across all supported platforms.
+ *
+ * Device information that is specific to any individual platform/core
+ * etc should be in the "platform_context" which is hardware specific.
+ *
+ * In the event that any given core has a large number of marketing
+ * SKUs that change the PCI device ID, the device ID in this structure
+ * may be overwritten with a single common ID and not truly reflect
+ * the actual ID.  This prevents polluting the function tables with
+ * dozens of logically identical chipsets.
+ *
+ */
+typedef struct _device_context {
+	unsigned long power_state;
+	unsigned long mmadr;		/* Primary mmio address */
+	unsigned long mmadr_sdvo;	/* sdvo mmio address */
+	unsigned long mmadr_sdvo_st;	/* STMicro sdvo mmio address */
+	unsigned long mmadr_sdvo_st_gpio;	/* STMicro sdvo mmio address */
+	unsigned long gpio_bar;		/* LPC GPIO address */
+	unsigned char *virt_mmadr;
+	unsigned char *virt_mmadr_sdvo;
+	unsigned char *virt_mmadr_sdvo_st;
+	unsigned char *virt_mmadr_sdvo_st_gpio;
+	unsigned char *virt_gpio_bar;
+	unsigned long *virt_gttadr; /* was gtt_mmap */
+	unsigned long gatt_pages;   /* Number of pages addressable by GTT */
+	unsigned long stolen_pages; /* Number of pages of stolen memory */
+	unsigned long gmch_ctl;     /* GMCH control value */
+	void *scratch_page;         /* Empty page to fill unused GTT entries */
+	unsigned long fb_adr;       /* Video Memory address */
+	unsigned short did;         /* Device ID for main video device */
+	unsigned long rid;          /* Device revision ID for main video device */
+	unsigned short bid;			/* Device ID for Bridge */
+	unsigned long max_dclk;     /* maximum dotclock of all the chipset */
+	unsigned long mem_size;     /* Total memory space managed by GMM */
+	unsigned long reserved_mem; /* Size in pages of firmware populated mem */
+	unsigned long hw_config;    /* HW Config parameter bits (see igd_init.h) */
+	unsigned long hw_status_offset; /* Hw status page offset */
+	unsigned short gfx_freq;	/* Graphics Frequency, used to calculate PWM */
+	unsigned short core_freq;	/* Core Frequency, used to calculate DPLL freq */
+} device_context_t;
+
+struct _igd_context {
+	int igd_device_error_no; /* Values are already negative */
+	igd_dispatch_t dispatch;
+	inter_module_dispatch_t mod_dispatch;
+	device_context_t device_context;     /* Hardware independent */
+	platform_context_t platform_context; /* Hardware dependent   */
+	void *drm_dev;
+};
+
+typedef struct _igd_display_context {
+	igd_context_t *context;
+	void *plane;
+	void *pipe;
+	void *port[IGD_MAX_PORTS];
+	void *cursor;
+	unsigned long port_number;
+	unsigned long allocated;
+} igd_display_context_t;
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/debug.h b/drivers/gpu/drm/emgd/emgd/include/debug.h
new file mode 100644
index 0000000..3a12fc1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/debug.h
@@ -0,0 +1,169 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: debug.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Contains debug macros
+ *-----------------------------------------------------------------------------
+ */
+#ifndef __DEBUG_H
+#define __DEBUG_H
+
+
+
+#define IGD_PRINTK_PALETTE(p) \
+{ \
+	EMGD_DEBUG("Palette Struct:"); \
+	EMGD_DEBUG_S("    [%4d].id",    (unsigned int) p->palette_id); \
+	EMGD_DEBUG_S("    [%4d].type",  (unsigned int) p->palette_type); \
+	EMGD_DEBUG_S("    [%4d].size",  (unsigned int) p->size); \
+}
+
+#define IGD_PRINTK_3DSTRETCH(p, s, d, f) \
+{ \
+	EMGD_DEBUG("Ring Buffer Struct:"); \
+	EMGD_DEBUG_S("    Priority = [%8d]",       (unsigned int) p); \
+	EMGD_DEBUG_S("    Flags    = [%08x]",      (unsigned int) f); \
+	EMGD_DEBUG_S("    --------------"); \
+	EMGD_DEBUG_S("    Source surface"); \
+	EMGD_DEBUG_S("    --------------"); \
+	EMGD_DEBUG_S("    [%08x].addr",            (unsigned int) s->addr); \
+	EMGD_DEBUG_S("    [%8d].height",           (unsigned int) s->height); \
+	EMGD_DEBUG_S("    [%8d].width",            (unsigned int) s->width); \
+	EMGD_DEBUG_S("    [%8d].pitch",            (unsigned int) s->pitch); \
+	EMGD_DEBUG_S("    [%08x].pixel_format",    (unsigned int) s->pixel_format); \
+	EMGD_DEBUG_S("    -------------------"); \
+	EMGD_DEBUG_S("    Destination surface"); \
+	EMGD_DEBUG_S("    -------------------"); \
+	EMGD_DEBUG_S("    [%8d].offset",           (unsigned int) d->offset); \
+	EMGD_DEBUG_S("    [%8d].pitch",            (unsigned int) d->pitch); \
+	EMGD_DEBUG_S("    [%8d].x1",               (unsigned int) d->x1); \
+	EMGD_DEBUG_S("    [%8d].y1",               (unsigned int) d->y1); \
+	EMGD_DEBUG_S("    [%8d].x2",               (unsigned int) d->x2); \
+	EMGD_DEBUG_S("    [%8d].y2",               (unsigned int) d->y2); \
+	EMGD_DEBUG_S("    [%08x].pixel_format",    (unsigned int) d->pixel_format); \
+	EMGD_DEBUG_S("    [%08x].byte_mask",       (unsigned int) d->byte_mask); \
+}
+
+#define IGD_PRINT_RB_BUFFER(a) \
+{ \
+	EMGD_DEBUG("Ring Buffer Struct:"); \
+	EMGD_DEBUG_S("    [%08x].id",              (unsigned int) a->id); \
+	EMGD_DEBUG_S("    [%08x].size",            (unsigned int) a->size); \
+	EMGD_DEBUG_S("    [%08x].addr",            (unsigned int) a->addr); \
+	EMGD_DEBUG_S("    [%p].virt",             a->virt); \
+	EMGD_DEBUG_S("    [%p].start  = [%08x]",  a->start, (unsigned int) EMGD_READ32(a->start)); \
+	EMGD_DEBUG_S("    [%p].head   = [%08x]",  a->head,  (0x001ffffc & ((unsigned int) EMGD_READ32(a->head)))); \
+	EMGD_DEBUG_S("    [%p].tail   = [%08x]",  a->tail,  (0x001ffff8 & (unsigned int) EMGD_READ32(a->tail))); \
+	EMGD_DEBUG_S("    [%p].ctrl   = [%08x]",  a->ctrl,  (unsigned int) EMGD_READ32(a->ctrl)); \
+	EMGD_DEBUG_S("    [%08x].res",             (unsigned int) a->reservation); \
+}
+
+#define IGD_PRINTK_DISPLAY(display_context) \
+{ \
+	EMGD_DEBUG("Display Context:"); \
+	EMGD_DEBUG_S("    [%8d].plane",  (int) display_context->plane); \
+	EMGD_DEBUG_S("    [%8d].blend",  (int) display_context->blend_operation); \
+	EMGD_DEBUG_S("    [%8d].pipe",   (int) display_context->pipe); \
+	EMGD_DEBUG_S("    [%8d].port",   (int) display_context->port); \
+	EMGD_DEBUG_S("    [%8d].graph",  (int) display_context->alloc_type); \
+	EMGD_DEBUG_S("    [%8d].mode_n", (int) display_context->mode_number); \
+	EMGD_DEBUG_S("    [%8d].ref",    (int) display_context->refresh); \
+	EMGD_DEBUG_S("    [%8d].enable", (int) display_context->enable); \
+	EMGD_DEBUG_S("    [%8d].ulPipe", (int) display_context->ulPipe); \
+	EMGD_DEBUG_S("    [%8d].ulMode", (int) display_context->ulModeNumber); \
+	EMGD_DEBUG_S("    [%8d].ulRef",  (int) display_context->ulRefresh); \
+}
+
+#define IGD_PRINTK_FBINFO(fb_info) \
+{ \
+	EMGD_DEBUG("FrameBuffer Info:"); \
+	EMGD_DEBUG_S("    [%8d].width",  (int) fb_info->width); \
+	EMGD_DEBUG_S("    [%8d].height",  (int) fb_info->height); \
+	EMGD_DEBUG_S("    [%8d].screen_pitch",  (int) fb_info->screen_pitch); \
+	EMGD_DEBUG_S("    [%8x].fb_base_offset",  (unsigned int) fb_info->fb_base_offset); \
+	EMGD_DEBUG_S("    [%8x].pixel_format",  (unsigned int) fb_info->pixel_format); \
+}
+
+#define IGD_PRINTK_PTINFO(pt_info) \
+{ \
+	EMGD_DEBUG("Port Info:"); \
+	EMGD_DEBUG_S("    [%8d].width",  (int) pt_info->width); \
+	EMGD_DEBUG_S("    [%8d].height",  (int) pt_info->height); \
+	EMGD_DEBUG_S("    [%8d].x_offset",  (int) pt_info->x_offset); \
+	EMGD_DEBUG_S("    [%8d].y_offset",  (int) pt_info->y_offset); \
+	EMGD_DEBUG_S("    [%8d].refresh",  (int) pt_info->refresh); \
+	EMGD_DEBUG_S("    [%8d].hsync_start",  (unsigned int) pt_info->hsync_start); \
+	EMGD_DEBUG_S("    [%8d].hsync_end",  (unsigned int) pt_info->hsync_end); \
+	EMGD_DEBUG_S("    [%8d].hblank_start",  (unsigned int) pt_info->hblank_start); \
+	EMGD_DEBUG_S("    [%8d].hblank_end",  (unsigned int) pt_info->hblank_end); \
+	EMGD_DEBUG_S("    [%8d].vsync_start",  (unsigned int) pt_info->vsync_start); \
+	EMGD_DEBUG_S("    [%8d].vsync_end",  (unsigned int) pt_info->vsync_end); \
+	EMGD_DEBUG_S("    [%8d].vblank_start",  (unsigned int) pt_info->vblank_start); \
+	EMGD_DEBUG_S("    [%8d].vblank_end",  (unsigned int) pt_info->vblank_end); \
+	EMGD_DEBUG_S("    [%8x].flags",  (unsigned int) pt_info->flags); \
+}
+
+#define IGD_PRINTK_FBINFO_2(a,b) \
+{ \
+	EMGD_DEBUG("FrameBuffer Info:"); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].width",  (int) (a)->width, (int) (b)->width); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].height",  (int) (a)->height, (int) (b)->height); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].screen_pitch",  (int) (a)->screen_pitch, (int) (b)->screen_pitch); \
+	EMGD_DEBUG_S("    a=[%8x] b=[%8x].fb_base_offset",  (unsigned int) (a)->fb_base_offset, (unsigned int) (b)->fb_base_offset); \
+	EMGD_DEBUG_S("    a=[%8x] b=[%8x].pixel_format",  (unsigned int) (a)->pixel_format, (unsigned int) (b)->pixel_format); \
+}
+
+#define IGD_PRINTK_PTINFO_2(a,b) \
+{ \
+	EMGD_DEBUG("Port Info:"); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].width",  (int) a->width, (int) b->width); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].height",  (int) a->height, (int) b->height); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].x_offset",  (int) a->x_offset, (int) b->x_offset); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].y_offset",  (int) a->y_offset, (int) b->y_offset); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].refresh",  (int) a->refresh, (int) b->refresh); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].hsync_start",  (unsigned int) a->hsync_start, (unsigned int) b->hsync_start); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].hsync_end",  (unsigned int) a->hsync_end, (unsigned int) b->hsync_end); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].hblank_start",  (unsigned int) a->hblank_start, (unsigned int) b->hblank_start); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].hblank_end",  (unsigned int) a->hblank_end, (unsigned int) b->hblank_end); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].vsync_start",  (unsigned int) a->vsync_start, (unsigned int) b->vsync_start); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].vsync_end",  (unsigned int) a->vsync_end, (unsigned int) b->vsync_end); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].vblank_start",  (unsigned int) a->vblank_start, (unsigned int) b->vblank_start); \
+	EMGD_DEBUG_S("    a=[%8d] b=[%8d].vblank_end",  (unsigned int) a->vblank_end, (unsigned int) b->vblank_end); \
+	EMGD_DEBUG_S("    a=[%8x] b=[%8x].flags",  (unsigned int) a->flags, (unsigned int) b->flags); \
+}
+
+#define IGD_PRINT_ICH_GPIO(ich_gpio_base) \
+{ \
+	EMGD_DEBUG("GPIO_USE_SEL = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0x0)); \
+	EMGD_DEBUG("GPIO_IO_SEL = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0x4)); \
+	EMGD_DEBUG("GPIO_LVL = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0xc)); \
+	EMGD_DEBUG("GPIO_INV = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0x2c)); \
+	EMGD_DEBUG("GPIO_USE_SEL2 = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0x30));\
+	EMGD_DEBUG("GPIO_IO_SEL2 = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0x34)); \
+	EMGD_DEBUG("GPIO_LVL2 = 0x%lx", EMGD_READ_PORT32(ich_gpio_base + 0x38)); \
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/decode.h b/drivers/gpu/drm/emgd/emgd/include/decode.h
new file mode 100644
index 0000000..ac2415a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/decode.h
@@ -0,0 +1,76 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: decode.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for the video decode module.
+ *-----------------------------------------------------------------------------
+ */
+
+
+#ifndef _IGD_DECODE_H
+#define _IGD_DECODE_H
+
+#include <igd_render.h>  /* For surface definition */
+
+#define IGD_VD_MAX_SURFACES 2048
+
+#define VID_SURFACE(ctx, id) \
+	(((ctx->context_info.surfmap == NULL) ? &surface_list[id] : &surface_list[ctx->context_info.surfmap->slist_index[id]]))
+
+typedef struct _igd_vd_surfmap_table {
+	int active_entries;
+	int total_surfaces;
+	int *slist_index;
+} igd_vd_surfmap_table_t;
+
+typedef struct _igd_vd_surface_list {
+	int allocated;
+	igd_surface_t *surface;
+	int chroma_offset;
+	int stride_mode;
+    /* Used to store driver private data, e.g. decoder specific intermediate status data
+     * extra_info[0-3]: used for decode
+     * extra_info[4]: surface fourcc
+     * extra_info[5]: surface skippeded or not for encode
+     */
+	int extra_info[6];
+	igd_region_t buf;
+	igd_vd_surfmap_table_t* surfmap;
+} igd_vd_surface_list_t;
+
+struct _igd_video_context {
+	igd_vp_name_t profile;
+	igd_ve_name_t entrypoint;
+	igd_vd_surface_list_t *current_render_target;
+	igd_vd_surfmap_table_t *surfmap;
+
+	/*
+	 * The actual hardware/profile context is much larger. The above
+	 * members are device indpendent.
+	 */
+};
+
+#endif /*_IGD_DECODE_H*/
diff --git a/drivers/gpu/drm/emgd/emgd/include/dispatch.h b/drivers/gpu/drm/emgd/emgd/include/dispatch.h
new file mode 100644
index 0000000..c5a371f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/dispatch.h
@@ -0,0 +1,59 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dispatch.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains helper functions for creating a dispatch table
+ *  for IGD modules.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_DISPATCH_H
+#define _HAL_DISPATCH_H
+
+#include <context.h>
+
+typedef struct _dispatch_table {
+	unsigned long did;
+	void *table;
+}dispatch_table_t;
+
+static __inline void *dispatch_acquire(
+	igd_context_t *context,
+	dispatch_table_t *list)
+{
+	dispatch_table_t *curr = list;
+	unsigned long did = context->device_context.did;
+
+	while(curr->did != 0) {
+		if(curr->did == did) {
+			return curr->table;
+		}
+		curr++;
+	}
+	return curr->table;
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/dispatch_utils.h b/drivers/gpu/drm/emgd/emgd/include/dispatch_utils.h
new file mode 100644
index 0000000..b6e4f73
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/dispatch_utils.h
@@ -0,0 +1,78 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dispatch_utils.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file implements common macros that are used throughout the HAL
+ *  for Device Dependent dispatching. This file is used in both the HAL and
+ *  HAL helper libraries, as such it cannot include any HAL data structures
+ *  that are not available to the helper libraries.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _DISPATCH_UTILS_H_
+#define _DISPATCH_UTILS_H_
+
+#include <config.h>
+#include <intelpci.h>
+
+typedef struct _dispatch_table {
+	unsigned long did;
+	void *table;
+}dispatch_table_t;
+
+static __inline void *dispatch_acquire(
+	unsigned long did,
+	dispatch_table_t *list)
+{
+	dispatch_table_t *curr = list;
+
+	while(curr->did != 0) {
+		if(curr->did == did) {
+			return curr->table;
+		}
+		curr++;
+	}
+	return curr->table;
+}
+
+
+#ifdef CONFIG_PLB
+#define DISPATCH_PLB(p) {PCI_DEVICE_ID_VGA_PLB, p},
+#else
+#define DISPATCH_PLB(p)
+#endif
+#ifdef CONFIG_TNC
+#define DISPATCH_TNC(p) {PCI_DEVICE_ID_VGA_TNC, p},
+#define DISPATCH_TNC_A0(p) {PCI_DEVICE_ID_VGA_TNC_A0, p},
+#define DISPATCH_LNC(p) {PCI_DEVICE_ID_VGA_LNC, p},
+#else
+#define DISPATCH_TNC(p)
+#define DISPATCH_TNC_A0(p)
+#define DISPATCH_LNC(p)
+#endif
+#define DISPATCH_END {0, NULL}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/displayid.h b/drivers/gpu/drm/emgd/emgd/include/displayid.h
new file mode 100644
index 0000000..82a995f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/displayid.h
@@ -0,0 +1,663 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: displayid.h
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Header file for DisplayID.
+ *  Supported DisplayID versions:
+ *  VESA DisplayID Standard Verion 1 12/13/2007
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _DISPLAYID_H
+#define _DISPLAYID_H
+
+#include <pd.h>
+
+/* Max limits for DisplayID structure members */
+#define DISPLAYID_MAX_RANGES        5
+#define DISPLAYID_MAX_NUM_TIMINGS   5
+/* Possible attributes from DisplayID:
+ *     T1, T2, T3, T4, T5
+ *     dual_channel
+ *     panel_depth
+ *     panel_type */
+#define DISPLAYID_MAX_ATTRS        10
+
+/* DisplayID return values */
+#define DISPLAYID_ERROR_PARSE      1
+#define DISPLAYID_READ_AGAIN       2
+#define DISPLAYID_NOT_SUPPORTED    3
+
+/* Print line macro */
+#define DISPLAYID_PRINT_LINE() \
+	EMGD_DEBUG("----------------------------------------------------------")
+
+/* DisplayID Product Type Identifiers */
+#define PRODUCTID_EXTENSION        0x00
+#define PRODUCTID_TEST             0x01
+#define PRODUCTID_PANEL            0x02
+#define PRODUCTID_STANDALONE       0x03
+#define PRODUCTID_TV_RECEIVER      0x04
+#define PRODUCTID_REPEATER         0x05
+
+/* DisplayID Data Block Tags */
+#define DATABLOCK_PRODUCTID        0x00
+#define DATABLOCK_DISPLAY_PARAMS   0x01
+#define DATABLOCK_COLOR_CHARS      0x02
+#define DATABLOCK_TIMING_1_DETAIL  0x03
+#define DATABLOCK_TIMING_2_DETAIL  0x04
+#define DATABLOCK_TIMING_3_SHORT   0x05
+#define DATABLOCK_TIMING_4_DMTID   0x06
+#define DATABLOCK_VESA_TIMING_STD  0x07
+#define DATABLOCK_CEA_TIMING_STD   0x08
+#define DATABLOCK_VIDEO_RANGE      0x09
+#define DATABLOCK_SERIAL_NUMBER    0x0A
+#define DATABLOCK_ASCII_STRING     0x0B
+#define DATABLOCK_DISPLAY_DEVICE   0x0C
+#define DATABLOCK_LVDS_INTERFACE   0x0D
+#define DATABLOCK_TRANSFER_CHAR    0x0E
+#define DATABLOCK_DISPLAY_INTF     0x0F
+#define DATABLOCK_STEREO_INTF      0x10
+#define DATABLOCK_VENDOR_SPECIFIC  0x7F
+#define DATABLOCK_RESERVED         0x80
+
+/*
+ * typedef struct __attribute__((packed)) {
+ * } foo_t;
+ * This isn't available with Microsoft C compiler
+ * So use pragma to enable bytewise packing and at the end
+ * put it back to default.
+ */
+#pragma pack(1)
+/* General Data Block header - Max 251 bytes */
+typedef struct _datablock {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+	unsigned char  payload_data[248];
+} datablock_t;
+
+/* Video Timing Range Limits Data Block.
+ * Note:
+ *   It is used to convey to a source the supported range of vertical and
+ *   horizontal frequencies along with the maximum supported pixel clock.
+ *   For a continuous frequency device any timing that lies within the
+ *   specified range will ensure a displayable image. The picture may not
+ *   be properly sized or centered, as guaranteed with explicitly reported
+ *   timing, but however the display should synchronize to the timing.
+ */
+
+typedef struct _timing_range {
+	unsigned char  tag;                 /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	struct {
+		unsigned short lsb_min_dclk;
+		unsigned char  msb_min_dclk;
+	} mindclk;
+	struct {
+		unsigned short lsb_max_dclk;
+		unsigned char  msb_max_dclk;
+	} maxdclk;
+	unsigned char  min_hrate;           /* KHz */
+	unsigned char  max_hrate;           /* KHz */
+	unsigned short min_hblank;          /* Pixels */
+	unsigned char  min_vrate;           /* Hz */
+	unsigned char  max_vrate;           /* Hz */
+	unsigned short min_vblank;          /* Lines */
+
+	/* Timing support flags */
+	unsigned char  reserved1:4;
+	unsigned char  discrete_display:1;
+	unsigned char  vesa_cvt_rb:1;
+	unsigned char  vesa_cvt_std:1;
+	unsigned char  interlaced:1;
+
+	/* Because there is no way to define a 3-byte quantity it is not possible
+	 * to directly read the dclks into member variables automatically.
+	 * Therefore 1st read into above dclk bytes and then copy values into
+	 * below fields at the time of parsing. */
+	/* Below members are outside Data Block definition */
+	unsigned long  min_dclk;            /* KHz */
+	unsigned long  max_dclk;            /* KHz */
+} timing_range_t;
+
+/* Data Block Display params - Video input id */
+#define VIDEO_INPUT_LVDS    0x01
+#define VIDEO_INPUT_TMDS    0x02
+#define VIDEO_INPUT_RSDS    0x03
+#define VIDEO_INPUT_DVID    0x04
+#define VIDEO_INPUT_DVII    0x05
+#define VIDEO_INPUT_VESA_M1 0x06
+#define VIDEO_INPUT_HDMI_A  0x07
+#define VIDEO_INPUT_HDMI_B  0x08
+#define VIDEO_INPUT_MDDI    0x09
+#define VIDEO_INPUT_PORT    0x0A
+#define VIDEO_INPUT_1394    0x0B
+#define VIDEO_INPUT_USB     0x0C
+
+/* Aspect Ratios */
+#define TIMING_AR_1_1       0x0    /* 3-bits */
+#define TIMING_AR_5_4       0x1
+#define TIMING_AR_4_3       0x2
+#define TIMING_AR_15_9      0x3
+#define TIMING_AR_16_9      0x4
+#define TIMING_AR_16_10     0x5
+
+/* Data Block Display params - feature support flags */
+typedef struct _display_params {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned short horz_image_size;    /* horizontal image size in 0.1 mm */
+	unsigned short vert_image_size;    /* vertical image size in 0.1 mm */
+	unsigned short horz_pixels;        /* horizontal pixels */
+	unsigned short vert_pixels;        /* vertical pixels */
+
+	/* Display feature support flags */
+	unsigned char  deinterlacing:1;    /* de-interlacing capable display */
+	unsigned char  reserved1:1;
+	unsigned char  fixed_res:1;        /* fixed resolution display */
+	unsigned char  fixed_timing:1;     /* fixed timing display */
+	unsigned char  dpm_capable:1;      /* VESA DPM capable display */
+	unsigned char  audio_override:1;   /* override audio on video interface */
+	unsigned char  seperate_audio:1;   /* seperate audio input is available */
+	unsigned char  audio_on_video:1;   /* audio available on video interface */
+
+	unsigned char  transfer_gamma;     /* transfer characteristic gamma */
+	unsigned char  aspect_ratio;       /* aspect ratio */
+	unsigned char  native_color_depth:4; /* color bit depth */
+	unsigned char  overall_color_depth:4;/* color bit depth */
+} display_params_t;
+
+
+/* Product Identification Data Block */
+typedef struct _productid {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	char           vendor[3];          /* vendor name code, no NULL char */
+	unsigned short product_code;       /* product code */
+	unsigned long  serial_number;      /* 32-bit serial number */
+	unsigned char  manf_week;          /* manufactured week number */
+	unsigned char  manf_year;          /* # of years after 2000 */
+	unsigned char  string_size;        /* product id string size */
+	char           string[237];        /* product id string max 236 + NULL */
+} productid_t;
+
+/* Color Characteristices Data Block */
+typedef struct _color_char {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  white_points:4;
+	unsigned char  primaries:3;
+	unsigned char  temporal_color:1;
+
+	unsigned short color_x_value:12;   /* 1.5 bytes */
+	unsigned short color_y_value:12;   /* 1.5 bytes: 0.5 from above */
+} color_char_t;
+
+/* Type 1 - DTD - 20 bytes
+ * Notes:
+ * 1. In a Data Block, maximum of 12DTDs x 20bytes = 240bytes can fit.
+ * 2. As part of parsing, DTD data will be read into type1_dtd_t then
+ *    converted into pd_timing_t for IEGD consumption.
+ * 3. There is no limit on maximum number of type 1 timings.
+ *    They can be present not only in basic section but also in the
+ *    extension section as well.
+ */
+typedef struct _type1_dtd {
+	struct {
+		unsigned short lsb_dclk;
+		unsigned char  msb_dclk;
+	} dclk;                                  /*  02-00 [07:00] */
+
+	/* timing options - 1 byte */
+	unsigned char  aspect_ratio:3;
+	unsigned char  reserved:1;
+	unsigned char  interlaced:1;
+	unsigned char  stereo_3d_support:2;
+	unsigned char  preferred:1;
+
+	unsigned short hactive;
+	unsigned short hblank;
+	unsigned short hsync_offset:15;
+	unsigned short hsync_polarity:1;
+	unsigned short hsync_width;
+	unsigned short vactive;
+	unsigned short vblank;
+	unsigned short vsync_offset:15;
+	unsigned short vsync_polarity:1;
+	unsigned short vsync_width;
+} type1_dtd_t;
+
+/* Type 2 - DTD - 11 bytes
+ * Notes:
+ * 1. In a Data Block, maximum of 22DTDs x 11bytes = 242bytes can fit.
+ * 2. As part of parsing, DTD data will be read into type2_dtd_t then
+ *    converted into pd_timing_t for IEGD consumption.
+ * 3. There is no limit on maximum number of type 2 timings.
+ *    They can be present not only in basic section but also in the
+ *    extension section as well.
+ */
+typedef struct _type2_dtd {                   /* bytes: [bits] */
+	struct {
+		unsigned short lsb_dclk;
+		unsigned char  msb_dclk;
+	} dclk;                                  /*  02-00 [07:00] */
+
+	/* timing options - 1 byte */
+	unsigned char  reserved:4;                /*     03 [03:00] */
+	unsigned char  interlaced:1;              /*     03 [04:04] */
+	unsigned char  stereo_3d_support:2;       /*     03 [06:05] */
+	unsigned char  preferred:1;               /*     03 [07:07] */
+
+	unsigned short hactive:9;                 /*  05-04 [08:00] */
+	unsigned short hblank:7;                  /*  05-04 [15:09] */
+	unsigned char  hsync_width:4;             /*     06 [03:00] */
+	unsigned char  hsync_offset:4;            /*     06 [07:04] */
+	unsigned short vactive:12;                /*  08-07 [11:00] */
+	unsigned short reserved1:4;               /*     08 [15:12] */
+
+	unsigned char  vblank;                    /*     09 [07:00] */
+	unsigned char  vsync_width:4;             /*     10 [03:00] */
+	unsigned char  vsync_offset:4;            /*     10 [07:04] */
+} type2_dtd_t;
+
+
+/* Type 3 - DTD - CVT - 3 bytes
+ * Notes:
+ * 1. In a Data Block, maximum of 82timings x 3bytes = 246bytes can fit.
+ * 2. As part of parsing, data will be read into type3_cvt_t then
+ *    it will be made available for IEGD consumption
+ *    either
+ *         by marking in standard timing table
+ *    or
+ *         creating and inserting in pd_timing_t
+ *    based on CVT formula.
+ * 3. There is no limit on maximum number of type 3 timings.
+ *    They can be present not only in basic section but also in the
+ *    extension section as well.
+ */
+typedef struct _type3_cvt {                   /* bytes: [bits] */
+	/* byte 0 - timing options */
+	unsigned char  aspect_ratio:4;
+	unsigned char  cvt_formula:3;
+	unsigned char  preferred:1;
+
+	/* byte 1 - horizontal active */
+	unsigned char  hactive;
+
+	/* byte 2 - other info */
+	unsigned char  refresh:7;
+	unsigned char  interlaced:1;
+} type3_cvt_t;
+
+/* Structure to emulate bit fields for Type1 Std or Type2 CEA timings */
+typedef struct _type_std {
+	unsigned short width;
+	unsigned short height;
+	unsigned short refresh;
+	unsigned long  flags;         /* currently interlaced, rb, aspect ratio */
+} type_std_t;
+
+/* Display Device Data Block */
+/* Analog interface subtype codes */
+#define ANALOG_15HD_VGA     0x0
+#define ANALOG_VESA_NAVI_V  0x1
+#define ANALOG_VESA_NAVI_D  0x2
+
+/* Device technology types */
+#define TECH_CRT            0x0
+#define TECH_LVDS           0x1
+#define TECH_PLASMA         0x2
+#define TECH_EL             0x3   /* Electroluminescent */
+#define TECH_INORG          0x4
+#define TECH_ORG            0x5
+#define TECH_FED            0x6   /* FED or sim. "cold cathode" */
+#define TECH_EP             0x7   /* Electrophoretic */
+#define TECH_EC             0x8   /* Electrochromic */
+#define TECH_EM             0x9   /* Electromechanical */
+#define TECH_EW             0xA   /* Electrowetting */
+#define TECH_OTHER          0xF   /* Othertype not defined here */
+
+/* Device technoloty sub-types */
+#define TECH_CRT_MONO       0x0   /* Mono chrome CRT */
+#define TECH_CRT_STD        0x1   /* Standard tricolor CRT */
+#define TECH_CRT_OTHER      0x2   /* other or undefined */
+
+#define TECH_LVDS_PASSIVE_TN 0x0  /* Passive matrix TN */
+#define TECH_LVDS_PASSIVE_CL 0x1  /* Passive matrix cholesteric LC */
+#define TECH_LVDS_PASSIVE_FE 0x2  /* Passive matrix ferroelectric LC */
+#define TECH_LVDS_PASSIVE_OT 0x3  /* Passive matrix other LC */
+#define TECH_LVDS_ACTIVE_TN  0x4  /* Active matrix TN */
+#define TECH_LVDS_ACTIVE_IPS 0x5  /* Active matrix IPS */
+#define TECH_LVDS_ACTIVE_VA  0x6  /* Active matrix VA */
+#define TECH_LVDS_ACTIVE_OCB 0x7  /* Active matrix OCB */
+#define TECH_LVDS_ACTIVE_FL  0x8  /* Active matrix ferroelectric */
+#define TECH_LVDS_OTHER      0xF  /* Other LVDS technology not defined above */
+
+#define TECH_PLASMA_DC       0x0  /* DC plasma */
+#define TECH_PLASMA_AC       0x1  /* AC plasma */
+
+/* Content protection support */
+#define CP_NOCP              0x0
+#define CP_DTCP              0x2
+#define CP_DPCP              0x3  /* Display port content protection */
+
+/* Display physical orientation information */
+#define DEFAULT_ORIENTATION(a) 		((a & 0xC0) >> 6)
+#define ZERO_PIXEL(a) 				((a & 0x0C) >> 2)
+#define SCAN_DIRECTION(a) 			(a & 0x03)
+
+#define SCAN_DIRECTION_LONG 		0x1 /* Display scan direction (2 bits) */
+#define SCAN_DIRECTION_SHORT 		0x2
+
+#define ZP_UPPER_LEFT 				0x0 /* Zero pixel position (2 bits) */
+#define ZP_UPPER_RIGHT 				0x1
+#define ZP_LOWER_LEFT 				0x2
+#define ZP_LOWER_RIGHT 				0x3
+
+#define DEF_ORIENTATION_LANDSCAPE 	0x0 /* Default orientation (2 bits) */
+#define DEF_ORIENTATION_PORTRAIT 	0x1
+#define DEF_ORIENTATION_UNKNOWN 	0x2
+
+
+typedef struct _display_device {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  tech_subtype:4;
+	unsigned char  tech_type:4;
+	unsigned char  reserved1:2;
+	unsigned char  can_on_off_bklt:1;  /* This bit tells backlight can be
+										* switched on or off using the control
+										* method defined */
+	unsigned char  can_change_bklt:1;  /* This bit tells backlight intensity
+										* can be changed using the control
+										* method defined */
+	unsigned char  op_mode:4;
+
+	unsigned short horz_pixel_count;
+	unsigned short vert_pixel_count;
+	unsigned char  aspect_ratio;
+	unsigned char  orientation;
+	unsigned char  subpixel_info;
+	unsigned char  horz_pitch;
+	unsigned char  vert_pitch;
+
+	unsigned char  display_color_depth:4;
+	unsigned char  reserved2:4;
+	unsigned char  response_time;
+} display_dev_t;
+
+/* LVDS Interface Timing and Power Sequencing */
+typedef struct _lvds_display {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  max_t1:4;
+	unsigned char  min_t1:4;
+
+	unsigned char  max_t2:6;
+	unsigned char  reserved2:2;
+
+	unsigned char  max_t3:6;
+	unsigned char  reserved3:2;
+
+	unsigned char  min_t4:7;
+	unsigned char  reserved4:1;
+
+	unsigned char  min_t5:6;
+	unsigned char  reserved5:2;
+
+	unsigned char  min_t6:6;
+	unsigned char  reserved6:2;
+} lvds_display_t;
+
+/* Transfer Characteristics Data Block */
+typedef struct _transfer_char {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+	unsigned char  luminance;
+} transfer_char_t;
+
+/* Vendor specific Data Block */
+typedef struct _vendor {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+	unsigned char  vendor_id[3];                /* no NULL char */
+	unsigned char  vendor_specific_data[246];   /* 245 + NULL */
+} vendor_t;
+
+/* Stereo methods */
+#define STEREO_FIELD_SEQUENTIAL      0x00
+#define STEREO_SIDE_BY_SIDE          0x01
+#define STEREO_PIXEL_INTERLEAVED     0x02
+#define STEREO_DUAL_INTERFACE        0x03
+#define STEREO_MULTIVIEW             0x04
+#define STEREO_PROPRIETARY           0xFF
+
+/* Stereo Display Interface Data Block */
+typedef struct _stereo_intf {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  stereo_intf_payload;
+	unsigned char  stereo_intf_method;
+	union {
+		unsigned char  stereo_polarity;     /* Field Sequential Stereo */
+		unsigned char  view_identity;       /* Side-by-side Stereo */
+		unsigned char  pattern8x8[8];       /* Pixel Interleaved Stereo */
+		unsigned char  polarity_mirroring;  /* Dual Interface Stereo */
+		struct {                            /* Multiview stereo */
+			unsigned char  num_views;
+			unsigned char  interleaving_method;
+		} multiview;
+		/* Note: In case of proprietary stereo method, the parameters can be
+		 * somewhere in the Vendor Specific Block */
+		unsigned char  stereo_params[8];
+	};
+} stereo_intf_t;
+
+/* Display Interface Data Block */
+#define INTERFACE_ANALOG    0x0
+#define INTERFACE_LVDS      0x1
+#define INTERFACE_TMDS      0x2
+#define INTERFACE_RSDS      0x3
+#define INTERFACE_DVID      0x4
+#define INTERFACE_DVII_A    0x5
+#define INTERFACE_DVII_D    0x6
+#define INTERFACE_HDMI_A    0x7
+#define INTERFACE_HDMI_B    0x8
+#define INTERFACE_MDDI      0x9
+#define INTERFACE_DISP_PORT 0xA
+#define INTERFACE_PROP_DIG  0xB
+
+/* Interface color depth */
+#define INTERFACE_6BPPC            0x01   /* 6 bits per primary color */
+#define INTERFACE_8BPPC            0x02   /* 8 bits per primary color */
+#define INTERFACE_10BPPC           0x04   /* 10 bits per primary color */
+#define INTERFACE_12BPPC           0x08   /* 12 bits per primary color */
+#define INTERFACE_14BPPC           0x10   /* 14 bits per primary color */
+#define INTERFACE_16BPPC           0x20   /* 16 bits per primary color */
+
+typedef struct _display_intf {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  num_channels:4;
+	unsigned char  intf_type:4;
+
+	unsigned char  intf_revision:4;
+	unsigned char  intf_version:4;
+
+	unsigned char  rgb_color_depth:6;
+	unsigned char  reserved2:2;
+
+	unsigned char  ycbcr_444_color_depth:6;
+	unsigned char  reserved3:2;
+
+	unsigned char  ycbcr_422_color_depth:6;
+	unsigned char  reserved4:2;
+
+	unsigned char  cp_type:3;
+	unsigned char  reserved5:2;
+	unsigned char  color_encoding:3;
+
+	unsigned char  cp_revision:4;
+	unsigned char  cp_version:4;
+
+	unsigned char  ss_percent:4;
+	unsigned char  reserved6:2;
+	unsigned char  ss_type:2;
+
+	union {
+		struct {
+			/* Interface type: LVDS */
+			unsigned char  support_3_3v:1;
+			unsigned char  support_5v:1;
+			unsigned char  support_12v:1;
+			unsigned char  support_2_8v:1;
+			unsigned char  openldi:1;
+			unsigned char  reserved5:3;
+
+			unsigned char  data_strobe:1;
+			unsigned char  de_polarity:1;
+			unsigned char  de_mode:1;
+			unsigned char  reserved6:5;
+		} lvds;
+		struct {
+			/* Interface type: Proprietary Digital Interface */
+			unsigned char  data_strobe:1;
+			unsigned char  de_polarity:1;
+			unsigned char  de_mode:1;
+			unsigned char  reserved7:5;
+
+			unsigned char  reserved8;
+		} pdi;
+		unsigned short bytes9_10;
+	};
+} display_intf_t;
+
+/* Serial Number String */
+typedef struct _serial_number {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  serial_num[249];    /* 248 + NULL */
+} serial_number_t;
+
+/* General Purpose ASCII String */
+typedef struct _general_string {
+	unsigned char  tag;                /* data block type */
+	unsigned char  revision:3;
+	unsigned char  reserved:5;
+	unsigned char  payload;
+
+	unsigned char  string[249];        /* 248 + NULL */
+} general_string_t;
+
+/* This structure holds all of the parsed DisplayID information.*/
+/* 5-required bytes:
+ *  1-byte  - version, revision
+ *  1-byte  - payload
+ *  1-byte  - number of extensions
+ *  1-byte  - product type identifier
+ *  1-byte  - checksum
+ */
+typedef struct _displayid {
+	unsigned char    revision:4;         /* HEADER: revision */
+	unsigned char    version:4;          /* version */
+	unsigned char    payload;            /* payload excluding 5 required bytes*/
+	unsigned char    ptype_id;           /* DisplayID product type identifier */
+	unsigned char    num_extensions;     /* number of extension sections */
+	unsigned char    check_sum;          /* FOOTER: last byte of the section */
+
+	display_params_t display_params;     /* display params block */
+	timing_range_t   timing_range;       /* timing range block */
+	lvds_display_t   lvds;               /* LVDS interface & display block */
+	display_dev_t    display_dev;        /* display device block */
+	display_intf_t   display_intf;       /* display interface block */
+	unsigned char    dummy_db[256];      /* dummy datablock */
+
+	/* Video Timing Modes Type I, II DTDs into pd_timing_t */
+	unsigned char   num_timings;
+	pd_timing_t     timings[DISPLAYID_MAX_NUM_TIMINGS];
+
+	/* DisplayID attributes */
+	unsigned char   num_attrs;
+	pd_attr_t       attr_list[DISPLAYID_MAX_ATTRS];
+
+#ifndef CONFIG_MICRO
+	productid_t      		productid;       /* product id block */
+	color_char_t     		color_char;      /* color characteristics block */
+	serial_number_t  		serial_num;      /* serial number ASCII string */
+	general_string_t 		general_string;  /* general purpose ASCII string */
+	transfer_char_t  		transfer_char;   /* transfer characteristics block */
+	stereo_intf_t    		stereo_intf;     /* stereo display interface */
+	vendor_t 				vendor;          /* vendor specific data */
+	igd_DID_rotation_info_t rotation_info;   /* display orientation data */
+#endif
+} displayid_t;
+
+/* Pack back to default */
+#pragma pack()
+
+/* Functions */
+void displayid_filter_range_timings(pd_timing_t *tt, timing_range_t *range,
+	unsigned char firmware_type);
+int displayid_parse(unsigned char *buffer, displayid_t   *displayid,
+		pd_timing_t *timings, int count, unsigned char upscale);
+
+void displayid_dump(unsigned char *buffer, unsigned short size);
+void displayid_print(unsigned char *buffer, displayid_t *displayid);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/dsp.h b/drivers/gpu/drm/emgd/emgd/include/dsp.h
new file mode 100644
index 0000000..8a803bf
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/dsp.h
@@ -0,0 +1,46 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: dsp.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _DSP_H_
+#define _DSP_H_
+
+#include <mode.h>
+
+/* FIXME: Illegal exports */
+void dsp_dc_init(igd_context_t *context);
+int dsp_alloc(igd_context_t *context, unsigned long dc, unsigned long flags);
+int dsp_wait_rb(igd_context_t *context);
+unsigned long dsp_valid_dc(unsigned long dc, unsigned long flags);
+int dsp_display_connected(igd_context_t *context, igd_display_port_t *port);
+
+
+#endif /* _DSP_H_ */
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/edid.h b/drivers/gpu/drm/emgd/emgd/include/edid.h
new file mode 100644
index 0000000..829e272
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/edid.h
@@ -0,0 +1,130 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: edid.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Header file for EDID.
+ *  Supported EDID versions:
+ *  EDID 1.3 (backward compatible with 1.1, 1.2)
+ *-----------------------------------------------------------------------------
+ */
+
+/*
+ * This is a basic EDID parser. It only parses the parts of the EDID that
+ * are useful for developing timings and identifying the monitor. Items
+ * like monitor serial numbers, chromaticity settings etc. are not useful
+ * in the display drivers. There should be a simple interface to provide
+ * the complete unparsed EDID to IAL, so that such information can be
+ * used by a IAL driver if need be.
+ *
+ * timings_t: This is a structure that contains one set of display timings.
+ *  It is complete in that it contains all 12 timings fields instead of
+ *  the popular 8. Some of the "Established" EDID timings require all 12
+ *  fields. It also allows for both established flags and driver specific
+ *  flags to make it flexible enough to work for most drivers. The
+ *  EDID parser will mark the flags with FB_TIMINGS_PREFERRED if they are
+ *  found to be listed in the EDID. This does not include modes within
+ *  the the sync ranges (if provided), only "Established" "Standard" and
+ *  "Detailed" timings.
+ *
+ * edid_t: This structure will be populated by the EDID parser. There
+ *  are three important areas of information that impact display timings.
+ *  The h_max,h_min,v_max,v_min represent the range of timings that the
+ *  display is capable of accepting. No timings outside this range should
+ *  be used by the driver if a range is provided.
+ *
+ *  Any "Detailed" timings will be fully parsed and returned in
+ *  the timings array (only the first 12 are parsed, it is very unlikely
+ *  that any EDID exist that contain this many).
+ *
+ *  Suggested driver behavior when setting display timings would be to
+ *  first attempt to use "Detailed" timings provided by the monitor. Then
+ *  attempt to use the "Standard" timings that were marked as PREFERRED,
+ *  then as a last alternative, use a nonstandard set of timings within
+ *  the provided range.
+ *
+ */
+
+#ifndef _EDID_H
+#define _EDID_H
+
+#include <displayid.h>
+#include <pd.h>
+
+/* This structure holds all of the parsed EDID information.*/
+#define NUM_TIMINGS 12
+
+/* EDID return values */
+#define EDID_ERROR_PARSE  1
+#define EDID_READ_AGAIN   2
+
+typedef struct _established_timing {
+	unsigned long width;
+	unsigned long height;
+	unsigned long refresh;
+} established_timing_t;
+
+typedef struct _edid {
+	unsigned char     version;            /* Edid Version */
+	unsigned char     revision;           /* Edid Revision */
+	char              vendor[4];          /* Vendor Name code */
+	unsigned long     product_code;       /* Vendor assigned code */
+	unsigned long     serial_number;      /* 32-bit serial number */
+	unsigned char     manf_week;          /* Manufactored week number */
+	unsigned long     manf_year;          /* Manufactored year */
+	unsigned char     standard_color;
+	unsigned char     preferred_timing;   /* Use first timing Provided */
+	unsigned char     dpms;
+	unsigned char     display_type;
+	unsigned char     gtf;
+	unsigned char     range_set;          /* EDID Contains Valid Range Data */
+	timing_range_t    range;
+	unsigned char     num_timings;
+	pd_timing_t       timings[NUM_TIMINGS];
+	char              name[14];
+	cea_extension_t	  *cea;				  /* CEA extension based on 861-B */
+} edid_t;
+
+/* Functions */
+int edid_parse(
+		unsigned char *buffer,
+		edid_t        *edid,
+		pd_timing_t   *timings,
+		int           count,
+		unsigned char upscale);
+
+void enable_disable_timings(pd_timing_t *timing, unsigned char enable);
+void enable_scaled_timings(pd_timing_t *timing, pd_timing_t *dtd,
+	unsigned char upscale);
+void firmware_dump(unsigned char *buffer, unsigned short size);
+void edid_print(edid_t *edid);
+
+int edid_ext_parse(
+		unsigned char *buffer,
+		edid_t        *edid,
+		pd_timing_t   *timings,
+		int           count,
+		unsigned char upscale);
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/general.h b/drivers/gpu/drm/emgd/emgd/include/general.h
new file mode 100644
index 0000000..159bf23
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/general.h
@@ -0,0 +1,84 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: general.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file includes general equates.
+ *  Compiler:
+ *  This file contains general constants, structures and should be included
+ *  in all noninclude files.
+ *-----------------------------------------------------------------------------
+ */
+
+/*===========================================================================
+; General Constants
+;--------------------------------------------------------------------------*/
+
+#ifndef _GENERAL_H_
+#define _GENERAL_H_
+
+#define BIT0  0x00000001L
+#define BIT1  0x00000002L
+#define BIT2  0x00000004L
+#define BIT3  0x00000008L
+
+#define BIT4  0x00000010L
+#define BIT5  0x00000020L
+#define BIT6  0x00000040L
+#define BIT7  0x00000080L
+
+#define BIT8  0x00000100L
+#define BIT9  0x00000200L
+#define BIT10 0x00000400L
+#define BIT11 0x00000800L
+
+#define BIT12 0x00001000L
+#define BIT13 0x00002000L
+#define BIT14 0x00004000L
+#define BIT15 0x00008000L
+
+#define BIT16 0x00010000L
+#define BIT17 0x00020000L
+#define BIT18 0x00040000L
+#define BIT19 0x00080000L
+
+#define BIT20 0x00100000L
+#define BIT21 0x00200000L
+#define BIT22 0x00400000L
+#define BIT23 0x00800000L
+
+#define BIT24 0x01000000L
+#define BIT25 0x02000000L
+#define BIT26 0x04000000L
+#define BIT27 0x08000000L
+
+#define BIT28 0x10000000L
+#define BIT29 0x20000000L
+#define BIT30 0x40000000L
+#define BIT31 0x80000000L
+
+
+#endif // _GENERAL_H_
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/instr_common.h b/drivers/gpu/drm/emgd/emgd/include/instr_common.h
new file mode 100644
index 0000000..95c7e38
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/instr_common.h
@@ -0,0 +1,54 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: instr_common.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Common tools for hardware that uses instruction engines.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_INSTR_COMMON_H
+#define _IGD_INSTR_COMMON_H
+
+/* Write instructions macro. */
+#ifdef DEBUG_BUILD_TYPE
+#define INSTR_WRITE(data, addr)											\
+    if(igd_debug->hal.instr) {											\
+		EMGD_DEBUG_S("    addr:%p   data:0x%8.8x", ((void *)(addr)),	\
+			(unsigned int)(data));										\
+    }																	\
+	EMGD_WRITE32 (data, (addr));											\
+	addr++;
+
+#define IGD_PRINT_INSTR(name)                                    \
+    if(igd_debug->hal.instr) {									 \
+		EMGD_DEBUG_S("GMCH Instruction: %s", name);				 \
+    }
+#else
+#define INSTR_WRITE(data, addr) EMGD_WRITE32(data, (addr)++);
+#define IGD_PRINT_INSTR(name)
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/intelpci.h b/drivers/gpu/drm/emgd/emgd/include/intelpci.h
new file mode 100644
index 0000000..b389cc7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/intelpci.h
@@ -0,0 +1,98 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: intelpci.h
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Contains PCI bus transaction definitions
+ *-----------------------------------------------------------------------------
+ */
+
+/* PCI */
+#define PCI_VENDOR_ID_INTEL             0x8086
+#ifndef PCI_VENDOR_ID_STMICRO
+#define PCI_VENDOR_ID_STMICRO           0x104A
+#endif
+
+/* PLB Family Chips */
+#define PCI_DEVICE_ID_BRIDGE_PLB        0x8100
+#define PCI_DEVICE_ID_VGA_PLB           0x8108
+
+/* Atom E6xx */
+#define PCI_DEVICE_ID_BRIDGE_TNC        0x4114
+#define PCI_DEVICE_ID_VGA_TNC           0x4108
+
+/* Atom E6xx ULP */
+#define PCI_DEVICE_ID_BRIDGE_TNC_ULP    0x4115
+
+/* Atom E6xx Device 3 */
+#define PCI_DEVICE_ID_SDVO_TNC 	        0x8182
+
+/* Atom E6xx Device 31 (LPC) */
+#define PCI_DEVICE_ID_LPC_TNC 	        0x8186
+
+/* Atom E6xx ST Micro SDVO PCI device */
+#define PCI_DEVICE_ID_SDVO_TNC_ST       0xcc13
+
+/* Atom E6xx ST Micro GPIO SDVO PCI device */
+#define PCI_DEVICE_ID_SDVO_TNC_ST_GPIO  0xcc0c
+
+/* Support for MSRT and Pre-Release PCI ID for Atom E6xx
+ * Can be removed in future */
+#if 0
+/* Atom E6xx A0 Stepping */
+#define PCI_DEVICE_ID_BRIDGE_TNC_A0     0x4110
+#define PCI_DEVICE_ID_VGA_TNC_A0        0x4100
+
+/* Moorestown */
+#define PCI_DEVICE_ID_BRIDGE_LNC        0x4110
+#define PCI_DEVICE_ID_VGA_LNC           0x4102
+#endif
+
+/* Start: Southbridge specific */
+#define PCI_DEVICE_ID_LPC_82801AA       0x2410
+#define PCI_DEVICE_ID_LPC_82801AB       0x2420
+#define PCI_DEVICE_ID_LPC_82801BA       0x2440
+#define PCI_DEVICE_ID_LPC_82801BAM      0x244c
+#define PCI_DEVICE_ID_LPC_82801E        0x2450
+#define PCI_DEVICE_ID_LPC_82801CA       0x2480
+#define PCI_DEVICE_ID_LPC_82801DB       0x24c0
+#define PCI_DEVICE_ID_LPC_82801DBM      0x24cc
+#define PCI_DEVICE_ID_LPC_82801EB       0x24d0
+#define PCI_DEVICE_ID_LPC_82801EBM      0x24dc
+#define PCI_DEVICE_ID_LPC_80001ESB      0x25a1  /* LPC on HanceRapids ICH */
+#define PCI_DEVICE_ID_LPC_82801FB       0x2640  /* ICH6/ICH6R */
+#define PCI_DEVICE_ID_LPC_82801FBM      0x2641  /* ICH6M/ICH6MR */
+#define PCI_DEVICE_ID_LPC_82801FW       0x2642  /* ICH6W/ICH6WR */
+#define PCI_DEVICE_ID_LPC_82801FWM      0x2643  /* ICH6MW/ICH6MWR */
+#define PCI_DEVICE_ID_LPC_Q35DES        0x2910  /* ICH9 */
+#define PCI_DEVICE_ID_LPC_Q35DHES       0x2912  /* ICH9 */
+#define PCI_DEVICE_ID_LPC_Q35DOES       0x2914  /* ICH9 */
+#define PCI_DEVICE_ID_LPC_Q35RES        0x2916  /* ICH9 */
+#define PCI_DEVICE_ID_LPC_Q35BES        0x2918  /* ICH9 */
+
+
+
+#define INTEL_PTE_ALLIGNMENT                0xFFFFF000
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/math_fix.h b/drivers/gpu/drm/emgd/emgd/include/math_fix.h
new file mode 100644
index 0000000..91306fe
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/math_fix.h
@@ -0,0 +1,59 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: math_fix.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains OS abstracted interfaces to fixed point math functions
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OAL_MATH_FIX_H
+#define _OAL_MATH_FIX_H
+
+
+#define OS_POW_FIX(b, p)         os_pow_fix(b, p)
+
+
+#define OS_MAX(a,b)              (((a) > (b)) ? (a) : (b))
+#define OS_MIN(a,b)              (((a) < (b)) ? (a) : (b))
+
+/*****************************************************************************
+ * Function: os_pow_fix
+ *
+ * Parameters:
+ *    [IN] base:  the base, should be between 0 and 255.
+ *    [IN] power:  this must be in 24i.8f format.
+ *
+ * Description:
+ *    This function uses the Taylor Series to approximate the power function
+ *    using fixed-point math.
+ *
+ ****************************************************************************/
+
+unsigned int os_pow_fix( const int base, const int power );
+
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/memlist.h b/drivers/gpu/drm/emgd/emgd/include/memlist.h
new file mode 100644
index 0000000..7d9fa22
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/memlist.h
@@ -0,0 +1,154 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: memlist.h
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the header information for the chunk layer of gmm.
+ *  It should not be included outside gmm.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _MEMLIST_H
+#define _MEMLIST_H
+
+#include <gart.h>
+#include <igd_gmm.h>
+#include <context.h>
+#include <drmP.h>
+
+#define GMM_NODE_FREE  0x0
+#define GMM_NODE_USED  0x1
+#define GMM_NODE_FIXED 0x2
+
+/****************************************************************************
+ * gmm_node_t
+ *  This structure is an individual node representing an range of memory.
+ *  It may be allocated or free.
+ ****************************************************************************/
+typedef struct _gmm_node {
+	struct _gmm_node *next;
+	struct _gmm_node *previous;
+	unsigned long start;
+	unsigned long end;
+	unsigned long size;
+	unsigned long status;
+	os_gart_allocation_t gart_handle;
+	unsigned long phys;
+#ifdef DEBUG_BUILD_TYPE
+	char desc[GMM_DEBUG_DESC_SIZE];
+	unsigned long pixel_format; /* Pixel Format == 0 for Regions */
+	unsigned int width;
+	unsigned int height;
+	unsigned int pitch;
+	unsigned int type;
+	unsigned long flags;
+#endif
+}gmm_node_t;
+
+#ifdef DEBUG_BUILD_TYPE
+extern unsigned long _gmm_debug_pixel_format;
+extern unsigned int _gmm_debug_width;
+extern unsigned int _gmm_debug_height;
+extern unsigned int _gmm_debug_pitch;
+extern unsigned int _gmm_debug_type;
+extern unsigned long _gmm_debug_flags;
+
+#define GMM_SET_DEBUG_INFO(pf, w, h, p, t, f) \
+	_gmm_debug_pixel_format = pf;    \
+	_gmm_debug_width = w;			 \
+	_gmm_debug_height = h;			 \
+	_gmm_debug_pitch = p;			 \
+	_gmm_debug_type = t;			 \
+	_gmm_debug_flags = f;
+
+#else
+#define GMM_SET_DEBUG_INFO(pf, w, h, p, t, f)
+#endif
+
+/*
+ * Flags used when creating chunks or when requesting space from existing
+ * chunks.
+ */
+#define GMM_CHUNK_FREE      0x0
+#define GMM_CHUNK_LINEAR    0x1
+#define GMM_CHUNK_FENCED    0x2
+#define GMM_CHUNK_YMAJOR    0x4
+/*
+ * Reserved chunks are marked with this flag. Only a single surface may
+ * be allocated in reserved chunks. Reserved chunks may or may not have
+ * a fence pre-allocated.
+ */
+#define GMM_CHUNK_RESERVED  0x8
+#define GMM_CHUNK_TYPE_MASK 0xf
+
+typedef struct _gmm_mem_buffer  {
+	unsigned long size;
+	unsigned long type;
+	unsigned long physical;
+	struct page **pages;
+	int page_count;
+	int vmalloc_flag;
+} gmm_mem_buffer_t;
+
+typedef struct _gmm_chunk {
+	/* Next chunk in the list */
+	struct _gmm_chunk *next;
+	/* Previous chunk in the list */
+	struct _gmm_chunk *previous;
+	/* Current chunk usage state */
+	enum {
+		INUSE_ALLOCATED,
+		FREE_ALLOCATED,  /* gmm-allocated pages attached */
+		INUSE_IMPORTED,
+		FREE_IMPORTED    /* address space only; no pages attached */
+	} usage;
+	/* The offset of the allocated memory, after alignment */
+	unsigned long offset;
+	unsigned long used;
+	/* The number of bytes of memory requested/allocated for this chunk */
+	unsigned long size;
+	/* The number of pages of memory required for "size" bytes */
+	unsigned long pages;
+	/* A boolean flag indicating if the chunk's memory has been bound by AGP */
+	unsigned long bound;
+	/* Either AGP_PHYS_MEMORY or AGP_NORMAL_MEMORY */
+	unsigned long type;
+	gmm_mem_buffer_t *gtt_mem;
+	void *addr;
+	unsigned long ref_cnt;
+	/* An array of page addresses (created by gmm_get_page_list() */
+	unsigned long *page_addresses;
+} gmm_chunk_t;
+
+typedef struct _gmm_context_t {
+	unsigned long max_mem;
+	gmm_chunk_t *head_chunk;
+	gmm_chunk_t *tail_chunk;
+	igd_memstat_t memstat;
+	igd_context_t *context;
+	igd_gtt_info_t *gtt;
+} gmm_context_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/memory.h b/drivers/gpu/drm/emgd/emgd/include/memory.h
new file mode 100644
index 0000000..f85477c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/memory.h
@@ -0,0 +1,421 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: memory.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains OS abstracted interfaces to common memory operations.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OAL_MEMORY_H
+#define _OAL_MEMORY_H
+
+#include <linux/slab.h>
+
+unsigned long os_gart_alloc_page( void );
+unsigned long os_gart_virt_to_phys( unsigned char *a );
+void os_gart_free_page( unsigned char *a );
+
+/* #define INSTRUMENT_KERNEL_ALLOCS */
+#ifdef INSTRUMENT_KERNEL_ALLOCS
+#define MAX_FUNC_NAME 64
+
+typedef struct _os_allocd_mem {
+	void *ptr;
+	unsigned int size;
+	char function[MAX_FUNC_NAME];
+	struct _os_allocd_mem *next;
+} os_allocd_mem;
+
+extern os_allocd_mem *list_head;
+extern os_allocd_mem *list_tail;
+
+static inline void *_os_alloc(unsigned int size, const char *function) {
+	os_allocd_mem *mem;
+	void *ptr = kmalloc(size, GFP_KERNEL);
+	printk(KERN_DEBUG "%s OS_ALLOC(size=%u)=0x%p\n", function, size, ptr);
+	mem = kmalloc(sizeof(os_allocd_mem), GFP_KERNEL);
+	if (!ZERO_OR_NULL_PTR(mem)) {
+		mem->ptr = ptr;
+		mem->size = size;
+		strncpy(mem->function, function, MAX_FUNC_NAME);
+		mem->function[MAX_FUNC_NAME-1] = '\0';
+		mem->next = NULL;
+		if (NULL == list_tail) {
+			list_head = mem;
+		} else {
+			list_tail->next = mem;
+		}
+		list_tail = mem;
+	}
+	return ptr;
+}
+
+static inline void _os_free(void *ptr, const char *function) {
+	printk(KERN_DEBUG "%s OS_FREE(0x%p)\n", function, ptr);
+	if (NULL != list_head) {
+		os_allocd_mem *mem = list_head;
+		os_allocd_mem *prev = NULL;
+		while (NULL != mem) {
+			if (mem->ptr == ptr) {
+				if (mem == list_head) {
+					list_head = mem->next;
+					if (mem == list_tail) {
+						list_tail = NULL;
+					}
+				} else {
+					prev->next = mem->next;
+					if (mem == list_tail) {
+						list_tail = prev;
+					}
+				}
+				kfree(mem);
+				break;
+			}
+			prev = mem;
+			mem = mem->next;
+		}
+	}
+	kfree(ptr);
+}
+
+static inline void emgd_report_unfreed_memory(void) {
+	os_allocd_mem *mem = list_head;
+	os_allocd_mem *prev;
+
+	printk(KERN_DEBUG "%s() REPORT ON NON-FREED MEMORY:\n", __FUNCTION__);
+	while (NULL != mem) {
+		printk(KERN_DEBUG "  addr=0x%p, size=%u, function=\"%s\"\n",
+				mem->ptr, mem->size, mem->function);
+		prev = mem;
+		mem = mem->next;
+		kfree(prev);
+	}
+}
+
+
+/*!
+ * void *OS_ALLOC(size_t size)
+ *
+ * OS_ALLOC is used by OS independent code to allocate system memory and
+ * return a CPU writeable address to the allocated memory (Virtual address)
+ * The returned address has no guarenteed alignment.
+ * size should be <= 4k for larger sizes use OS_ALLOC_LARGE().
+ *
+ * Allocations returned from OS_ALLOC() should be freed with OS_FREE().
+ *
+ * All Full OAL implementations must implement the _OS_ALLOC entry point
+ * to enable use of this function.
+ *
+ * @return NULL on Failure
+ * @return Virtual or Flat address on Success
+ */
+#define OS_ALLOC(a) _os_alloc(a, __FUNCTION__)
+
+/*!
+ * void OS_FREE(void *p)
+ * OS_FREE should be used to free allocations returned from OS_ALLOC()
+ *
+ * All Full OAL implementations must implement the _OS_FREE entry point
+ * to enable use of this function.
+ */
+#define OS_FREE(a) _os_free(a, __FUNCTION__)
+
+/*!
+ * void *OS_ALLOC_LARGE(size_t size)
+ *
+ * OS_ALLOC_LARGE is used by OS independent code to allocate system memory
+ * in the same manner as OS_ALLOC except that size must be > 4k.
+ *
+ * Allocations returned from OS_ALLOC_LARGE() should be freed with
+ * OS_FREE_LARGE().
+ *
+ * All Full OAL implementations must implement the _OS_ALLOC_LARGE entry point
+ * to enable use of this function. This entry point may be implemented
+ * exactly the same as _OS_ALLOC is no diferentiation is required.
+ *
+ * @return NULL on Failure
+ * @return Virtual or Flat address on Success
+ */
+#define OS_ALLOC_LARGE(a) _os_alloc(a, __FUNCTION__)
+
+/*!
+ * void OS_FREE_LARGE(void *p)
+ * OS_FREE_LARGE should be used to free allocations returned from
+ * OS_ALLOC_LARGE()
+ *
+ * All Full OAL implementations must implement the _OS_FREE_LARGE entry point
+ * to enable use of this function. This entry point may be implemented
+ * exactly the same as _OS_FREE is no diferentiation is required.
+ *
+ */
+#define OS_FREE_LARGE(a) _os_free(a, __FUNCTION__)
+
+#else /* INSTRUMENT_KERNEL_ALLOCS */
+
+#define OS_ALLOC(a) kmalloc((a), GFP_KERNEL)
+#define OS_FREE(a) kfree(a)
+#define OS_ALLOC_LARGE(a) kmalloc((a), GFP_KERNEL)
+#define OS_FREE_LARGE(a) kfree(a)
+
+#endif /* INSTRUMENT_KERNEL_ALLOCS */
+
+#define OS_ALLOC_PAGE() NULL
+/*!
+ * void *OS_VIRT_TO_PHYS( void *p )
+ *
+ * OS_VIRT_TO_PHYS is used by OS independent code to obtain the physical
+ * address referenced by the virtual address p. The virtual address must be
+ * one returned by OS_ALLOC_PAGE or OS_ALLOC_CONTIGUOUS.
+ *
+ * This entry point is OPTIONAL. Only OAL implementations that have
+ * implemented the _OS_ALLOC_PAGE or _OS_ALLOC_CONTIGUOUS macros need
+ * implement the _OS_VIRT_TO_PHYS macro. OS independent code that must
+ * function on all implementation may not use this entry point.
+ *
+ * @returns Physical Address
+ */
+#define OS_VIRT_TO_PHYS(a) os_gart_virt_to_phys(a)
+
+/*!
+ * void OS_FREE_PAGE(void *p)
+ * OS_FREE_PAGE should be used to free allocations returned from
+ * OS_ALLOC_PAGE()
+ *
+ * This entry point is OPTIONAL. Only OAL implementations that have implemented
+ * the _OS_ALLOC_PAGE macro need implement the _OS_FREE_PAGE macro.
+ */
+#define OS_FREE_PAGE(a) os_gart_free_page(a)
+
+#define OS_MEMSET(a,b,c) memset(a,b,c)
+#define OS_MEMCPY(a,b,c) memcpy(a,b,c)
+#define OS_MEMCMP(a,b,c) memcmp(a,b,c)
+
+#define OS_OFFSETOF(t,m) offsetof(t,m)
+
+
+/*
+ * void *OS_MEMSET(void *s, int c, size_t n)
+ *
+ * OS_MEMSET sets all bytes of the memory area referenced by address s and
+ * size n to the char value c.
+ *
+ * ALL Full OAL implementations must implement the entry point _OS_MEMSET
+ * to enable use of this function.
+ *
+ * @returns Address s
+ */
+#ifndef OS_MEMSET
+#define OS_MEMSET(a,b,c) _oal_memset(a,b,c)
+#endif
+
+/*
+ * void *OS_MEMCPY(void *dest, void *src, size_t n)
+ *
+ * OS_MEMCPY copies n bytes from the memory referenced by src to the
+ * memory referenced by dest. The areas may not overlap.
+ *
+ * ALL Full OAL implementations must implement the entry point _OS_MEMCPY
+ * to enable use of this function.
+ *
+ * @returns Address dest
+ */
+#ifndef OS_MEMCPY
+#define OS_MEMCPY(a,b,c) _oal_memcpy(a,b,c)
+#endif
+
+/*
+ * void *OS_MEMCMP(void *s1, void *s2, size_t n)
+ *
+ * OS_MEMCMP compares n bytes from the memory referenced by s1 to the
+ * corresponding bytes referenced by s2.
+ *
+ * This entry point is available in all full OAL implementations. An OAL
+ * may implement _OS_MEMCMP macro or the built-in version will be used.
+ *
+ * @returns < 0 if the s1 value is less than s2
+ * @returns > 0 if the s1 value is greater than s2
+ * @returns 0 if the values are equal
+ */
+#ifndef OS_MEMCMP
+#define OS_MEMCMP(a,b,c) _oal_memcmp(a,b,c)
+#endif
+
+
+/*
+ * void *OS_MEMZERO(void *s, size_t n)
+ *
+ * OS_MEMZERO sets all bytes of the memory area referenced by address s and
+ * size n to 0.
+ *
+ * This entry point is available in all full OAL implementations. An OAL
+ * may implement _OS_MEMZERO macro or the built-in version making use of
+ * of _OS_MEMSET will be used.
+ *
+ * @returns Address s
+ */
+#ifndef OS_MEMZERO
+#define OS_MEMZERO(a,b)  OS_MEMSET(a, 0, b)
+#endif
+
+/*
+ * void *OS_MEMZERO(void *s, size_t n)
+ *
+ * OS_MEMZERO sets all bytes of the memory area referenced by address s and
+ * size n to 0.
+ *
+ * This entry point is available in all full OAL implementations. An OAL
+ * may implement _OS_MEMZERO macro or the built-in version making use of
+ * of _OS_MEMSET will be used.
+ *
+ * @returns Address s
+ */
+#ifndef OS_STRNCPY
+#define OS_STRNCPY(d, s, n)  _oal_strncpy(d, s, n)
+#endif
+
+
+
+
+
+/*!
+ * void *OS_ALLOC_CONTIGUOUS( size_t n, size_t align )
+ *
+ * OS_ALLOC_CONTIGUOUS is used by OS independent code to allocate a number
+ * of aligned system memory pages and return a CPU writeable address to the
+ * allocated memory (Virtual address) The returned address must point to
+ * physically contiguous memory aligned to the requested alignment.
+ *
+ * Allocations returned from OS_ALLOC_CONTIGUOUS() should be freed with
+ * OS_FREE_CONTIGUOUS().
+ *
+ * This entry point is OPTIONAL. Only OAL implementations that have the
+ * ability to allocate such pages need implement the _OS_ALLOC_CONTIGUOUS
+ * macro. OS independent code that must function on all implementation may
+ * not use this entry point.
+ *
+ * @return NULL on Failure
+ * @return Virtual or Flat address on Success
+ */
+#define OS_ALLOC_CONTIGUOUS(a,b) _OS_ALLOC_CONTIGUOUS(a,b)
+/*!
+ * void OS_FREE_CONTIGUOUS(void *p)
+ * OS_FREE_CONTIGUOUS should be used to free allocations returned from
+ * OS_ALLOC_CONTIGUOUS()
+ *
+ * This entry point is OPTIONAL. Only OAL implementations that have implemented
+ * the _OS_ALLOC_CONTIGUOUS macro need implement the _OS_FREE_CONTIGUOUS macro.
+ */
+#define OS_FREE_CONTIGUOUS(a) _OS_FREE_CONTIGUOUS(a)
+
+/*!
+ * size_t OS_OFFSETOF(type, member)
+ *
+ * OS_OFFSETOF is used by OS independent code to obtain the offset of a
+ * given member within a type.
+ *
+ * @returns size_t of the offset of member m within type t
+ */
+#ifndef OS_OFFSETOF
+#define OS_OFFSETOF(t,m) ((size_t)&(((t *)0)->m))
+#endif
+
+/*
+ * This is a OS independent helper is case the operating environment
+ * does not supply a memcmp() function. The OAL may use this implementation.
+ */
+static __inline int _oal_memcmp(const void *s1, const void *s2, unsigned long n)
+{
+	const unsigned char *cs1 = (const unsigned char *) s1;
+	const unsigned char *cs2 = (const unsigned char *)s2;
+
+	for ( ; n-- > 0; cs1++, cs2++) {
+		if (*cs1 != *cs2) {
+			return *cs1 - *cs2;
+		}
+	}
+	return 0;
+}
+
+static __inline void *_oal_memcpy(void *dest, const void *src, size_t n)
+{
+	size_t i;
+
+	i=0;
+	while( i < n ) {
+		((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
+		i++;
+	}
+	return dest;
+}
+
+static __inline char *_oal_strncpy(char *dest, const char *src, size_t n)
+{
+	size_t i;
+
+	for(i=0; i<n; i++) {
+		if(!(dest[i] = src[i])) {
+			for(i=i; i<n; i++) {
+				dest[i] = '\0';
+			}
+			return dest;
+		}
+	}
+	return dest;
+}
+
+static __inline void *_oal_memset(void *s, int c, size_t n)
+{
+	unsigned int i;
+	for(i=0; i<n; i++) {
+		((unsigned char *)s)[i] = (unsigned char)c;
+	}
+	return s;
+}
+
+/*
+ * These macros are optional for the OAL port. They are used to do memory
+ * management for virtual apterture space process.
+ */
+
+#ifdef _OS_MAP_STOLEN_MEM
+#define OS_MAP_STOLEN_MEM(a, b, c)        _OS_MAP_STOLEN_MEM(a, b, c)
+#else
+#define OS_MAP_STOLEN_MEM(a, b, c)        0
+#endif
+
+#ifdef _OS_VIRT_APERT_AVAILABLE
+#define OS_VIRT_APERT_AVAILABLE()         _OS_VIRT_APERT_AVAILABLE()
+#else
+#define OS_VIRT_APERT_AVAILABLE()         0
+#endif
+
+#ifdef _OS_GET_VIRT_APERT_BASE
+#define OS_GET_VIRT_APERT_BASE()          _OS_GET_VIRT_APERT_BASE()
+#else
+#define OS_GET_VIRT_APERT_BASE()          NULL
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/mode.h b/drivers/gpu/drm/emgd/emgd/include/mode.h
new file mode 100644
index 0000000..1cb8a2c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/mode.h
@@ -0,0 +1,444 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Contain header information for set mode support
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _MODE_H_
+#define _MODE_H_
+
+/* IO.h is needed for the FAR define */
+#include <io.h>
+
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <general.h>
+#include <context.h>
+#include <cmd.h>
+#include <pd.h>
+#include <edid.h>
+#include <displayid.h>
+#include <igd_render.h>
+
+/* KMS-related Headers */
+#include <drm/drmP.h>
+#include <drm/drm.h>
+#include <drm/drm_fb_helper.h>
+
+
+#ifndef TRUE
+#define TRUE  1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#define IGD_INVALID_MODE 0
+
+/*****************/
+/* Plane Features */
+/*****************/
+#define IGD_PLANE_FEATURES_MASK      0x000000FF
+#define IGD_PLANE_DISPLAY            0x00000001
+#define IGD_PLANE_OVERLAY            0x00000002
+#define IGD_PLANE_SPRITE             0x00000004
+#define IGD_PLANE_CURSOR             0x00000008
+#define IGD_PLANE_VGA                0x00000010
+#define IGD_PLANE_DOUBLE             0x00000020
+/*#define IGD_PLANE_CLONE            0x00000040 currently unused */
+#define IGD_PLANE_DIH                0x00000080
+#define IGD_PLANE_USE_PIPEA          0x00000100
+#define IGD_PLANE_USE_PIPEB          0x00000200
+
+/*****************/
+/* Pipe Features */
+/*****************/
+/* pipe's supported features */
+#define IGD_PIPE_FEATURES_MASK       0x000000FF
+#define IGD_PIPE_DOUBLE              0x00000001
+/* the following 2 bits are not pipe features but
+ * pipe identification bits that share the same
+ * pipe_features variable of the pipe structure
+ * NOTE that these bit-wise OR flags in nibble-2
+ * are also used in the port_features variable
+ * of the port structure (same locations) but
+ * called IGD_PORT_USE_PIPEX
+ */
+#define IGD_PIPE_IS_PIPEA            0x00000100
+#define IGD_PIPE_IS_PIPEB            0x00000200
+
+/*******************/
+/* Cursor Features */
+/*******************/
+/*
+ * Cursor's supported features
+ */
+/*
+ * cursor's pipe usage regulations. These are copies of the PIPE_IS_ bits so
+ * that a quick (pipe_features & plane_features & MASK) will tell you if
+ * the pipe can be used.
+ */
+#define IGD_CURSOR_USE_PIPE_MASK       0x00000F00
+#define IGD_CURSOR_USE_PIPEA           IGD_PIPE_IS_PIPEA
+#define IGD_CURSOR_USE_PIPEB           IGD_PIPE_IS_PIPEB
+
+/*****************/
+/* Port features */
+/*****************/
+/*
+ * Port's supported features
+ *
+ * port features also uses IGD_PORT_SHARE_MASK thus port feature bits
+ * cannot collide with IGD_PORT_SHARE_MASK
+ */
+#define IGD_PORT_FEATURES_MASK       0x000000FF
+#define IGD_RGBA_COLOR               0x00000001
+#define IGD_RGBA_ALPHA               0x00000002
+#define IGD_VGA_COMPRESS             0x00000004 /* Compress VGA to 640x480 */
+#define IGD_PORT_GANG                0x00000008
+/*
+ * port's pipe usage regulations. These are copies of the PIPE_IS_ bits so
+ * that a quick (pipe_features & port_features & MASK) will tell you if
+ * the pipe can be used.
+ */
+#define IGD_PORT_USE_PIPE_MASK       0x00000F00
+#define IGD_PORT_USE_PIPE_MASK_SHIFT 8
+#define IGD_PORT_USE_PIPEA           IGD_PIPE_IS_PIPEA
+#define IGD_PORT_USE_PIPEB           IGD_PIPE_IS_PIPEB
+/*
+ * Ports Sharing information. The port in question can share a pipe with the
+ * listed ports. If a shares with b, b must share with a too.
+ * Must be the same as IGD_PORT_MASK = 0x3f000
+ */
+#define IGD_PORT_SHARE_MASK          IGD_PORT_MASK
+#define IGD_PORT_SHARE_ANALOG        IGD_PORT_ANALOG
+#define IGD_PORT_SHARE_DIGITAL       IGD_PORT_DIGITAL
+#define IGD_PORT_SHARE_LVDS          IGD_PORT_LVDS
+#define IGD_PORT_SHARE_TV            IGD_PORT_TV
+
+/* MAX rings, planes and ports connected to a pipe */
+#define IGD_MAX_PIPE_QUEUES      4
+#define IGD_MAX_PIPE_PLANES      5
+#define IGD_MAX_PIPE_DISPLAYS    4
+#define IGD_MAX_PIPES            2
+#define MAX_DISPLAYS             IGD_MAX_DISPLAYS /* From igd_mode.h */
+
+/* Parameters to mode_update_plane_pipe_ports */
+#define MODE_UPDATE_PLANE  0x1
+#define MODE_UPDATE_PIPE   0x2
+#define MODE_UPDATE_PORT   0x4
+#define MODE_UPDATE_NONE   0x0
+
+#define PLANE(display) \
+	((igd_display_plane_t *)(((igd_display_context_t *)display)->plane))
+#define PIPE(display)  \
+	((igd_display_pipe_t *)(((igd_display_context_t *)display)->pipe))
+#define PORT(display, pn)  \
+	((igd_display_port_t *)(((igd_display_context_t *)display)->port[pn-1]))
+#define PORT_OWNER(display)  \
+	((igd_display_port_t *)(((igd_display_context_t *)display)->port[display->port_number-1]))
+
+#define MODE_IS_SUPPORTED(t) (t->mode_info_flags & IGD_MODE_SUPPORTED)
+#define MODE_IS_VGA(t) \
+	((((pd_timing_t *)t)->mode_info_flags & IGD_MODE_VESA) &&	\
+		(t->mode_number < 0x1D))
+
+#define IGD_KMS_PIPEA (IGD_PIPE_IS_PIPEA >> IGD_PORT_USE_PIPE_MASK_SHIFT)
+#define IGD_KMS_PIPEB (IGD_PIPE_IS_PIPEB >> IGD_PORT_USE_PIPE_MASK_SHIFT)
+
+#define KMS_PIPE_FEATURES(display) \
+	(((igd_display_pipe_t *)((igd_display_context_t *)display)->pipe)->pipe_features)
+
+#define KMS_PIPE_ID(pipe_features) \
+	((pipe_features & IGD_PORT_USE_PIPE_MASK) >> IGD_PORT_USE_PIPE_MASK_SHIFT)
+
+
+/* #define DC_PORT_NUMBER(dc, i) ((dc >> (i * 4)) & 0x0f) */
+#define DC_PORT_NUMBER IGD_DC_PORT_NUMBER
+
+/* This structure is used for the mode table which is a list of all
+ * supported modes. */
+
+typedef pd_timing_t igd_timing_info_t, *pigd_timing_info_t;
+
+
+/*
+ * NOTE: The plane typedef is a generic type. Each plane type has an
+ * equivalent typedef that is more specific to the type of plane. They
+ * MUST remain equivalent. If you change one you must change them all.
+ */
+typedef struct _igd_plane {
+	unsigned long plane_reg;       /* plane control register */
+	unsigned long plane_features;  /* plane feature list */
+	int           inuse;           /* plane inuse ? */
+	int           ref_cnt;         /* # of displays using this plane */
+	unsigned long *pixel_formats;  /* supported pixel formats */
+	void *plane_info;              /* ptr to plane_info */
+	struct _igd_plane *mirror;     /* pointer to mirror plane */
+} igd_plane_t;
+
+typedef struct _igd_display_plane {
+	unsigned long plane_reg;         /* plane contron register */
+	unsigned long plane_features;    /* list of plane features */
+	int           inuse;             /* plane inuse ? */
+	int           ref_cnt;           /* # of displays using this plane */
+	unsigned long *pixel_formats;    /* list of pixel formats supported */
+	igd_framebuffer_info_t *fb_info; /* attached fb to this plane */
+	struct _igd_display_plane *mirror;  /* pointer to mirror plane */
+} igd_display_plane_t, *pigd_display_plane_t;
+
+typedef struct _igd_cursor {
+	unsigned long cursor_reg;        /* cursor control register */
+	unsigned long plane_features;    /* cursor plane features */
+	int           inuse;             /* is this cursor in use? */
+	int           ref_cnt;           /* # of displays using this plane */
+	unsigned long *pixel_formats;    /* list of pixel_formats supported */
+	igd_cursor_info_t *cursor_info;
+	struct _igd_cursor *mirror;  /* pointer to mirror plane */
+} igd_cursor_t;
+
+typedef struct _igd_clock {
+	unsigned long dpll_control;     /* DPLL control register */
+	unsigned long mnp;              /* FPx0 register */
+	unsigned long p_shift;          /* Bit location of P within control */
+	unsigned long actual_dclk;		/* Actual dotclock after calculating dpll */
+}igd_clock_t;
+
+typedef struct _igd_display_pipe {
+	unsigned long pipe_num;             /* 0 Based index */
+	unsigned long pipe_reg;             /* pipe configuration register */
+	unsigned long timing_reg;           /* timing register(htotal) */
+	unsigned long palette_reg;          /* palette register */
+	igd_clock_t   *clock_reg;           /* DPLL clock registers */
+	unsigned long pipe_features;        /* pipe features */
+	int           inuse;                /* pipe allocated? TRUE/FALSE */
+	int           ref_cnt;              /* # of displays using this pipe */
+	cmd_queue_t *queue[IGD_MAX_PIPE_QUEUES]; /* Queues for this pipe */
+	igd_display_plane_t   *plane;      /* dsp plane connected to pipe */
+	igd_cursor_t          *cursor;     /* cursor connected to this pipe */
+	void                  *sprite;     /* sprite connected to this pipe */
+	igd_timing_info_t     *timing;     /* current timings on the port */
+	igd_display_context_t *owner;      /* owner display of this pipe */
+	unsigned long dclk;                /* current dclk running on this pipe */
+}igd_display_pipe_t, *pigd_display_pipe_t;
+
+typedef struct _igd_display_port {
+	unsigned long port_type;            /* port type */
+	unsigned long port_number;          /* port number */
+	char          port_name[8];         /* port name DVO A, B, C, LVDS, ANALOG */
+	unsigned long port_reg;             /* port control register */
+	unsigned long i2c_reg;              /* GPIO pins for i2c on this port */
+	unsigned long dab;                  /* i2c Device Address Byte */
+	unsigned long ddc_reg;              /* GPIO pins for DDC on this port */
+	unsigned long ddc_dab;
+	unsigned long port_features;        /* port features */
+	unsigned long clock_bits;           /* Clock input to use */
+	int           inuse;                /* port is in use */
+	unsigned long  power_state;         /* D Power state for the display/port */
+	unsigned long bl_power_state;       /* D Power state for the FP backlight */
+	struct _igd_display_port *mult_port;/* pointer to multiplexed port,
+										 * if it is used in that way */
+	igd_display_info_t    *pt_info;     /* port timing info */
+	pd_driver_t           *pd_driver;
+	void                  *pd_context;  /* Context returned from PD */
+	pd_callback_t         *callback;    /* DD Callback to passed to PD */
+	unsigned long         num_timing;   /* number of timings available */
+	igd_timing_info_t     *timing_table; /* static/dynamic PD timings list */
+	unsigned long         i2c_speed;    /* Connected encoder's I2C bus speed */
+	unsigned long         ddc_speed;    /* DDC speed in KHz */
+	igd_param_fp_info_t   *fp_info;     /* Flat panel parameter info if any */
+	igd_param_dtd_list_t  *dtd_list;    /* EDID-less DTD info if any */
+	igd_param_attr_list_t *attr_list;   /* Saved attributes if any */
+	igd_attr_t            *tmp_attr;    /* Temp attr array, for copying */
+	unsigned int          tmp_attr_num; /* Number of attr in temp array */
+	igd_timing_info_t     *fp_native_dtd; /* FP native DTD */
+	unsigned long         pd_type;      /* Display type given by port driver */
+	unsigned long         pd_flags;     /* port driver flags */
+	unsigned long         saved_bl_power_state;
+
+	/* This attribute list is designed to eventually suck in things above
+	 * such as fb_info.  For now, it only has color correction attributes */
+	igd_attr_t            *attributes;
+
+	unsigned char         firmware_type;
+	union {
+		displayid_t       *displayid;
+		edid_t            *edid;         /* EDID information */
+	};
+
+    /* Added for VBIOS size Reduction */
+	unsigned long         preserve;
+	unsigned long         mult_preserve;
+	unsigned long         vga_sync;
+
+}igd_display_port_t, *pigd_display_port_t;
+
+/* This structure is used to save mode state.
+ * Rightnow, it is saving state of current port and its port driver state.
+ * This information is used while restoring to a previously saved mode
+ * state.
+ * TODO: This can be extended to save all display modules (mode, dsp, pi) reg
+ * information along with port driver's state information. This requires
+ * changes to exiting reg module. */
+#define MAX_PORT_DRIVERS     20
+typedef struct _mode_pd_state {
+		igd_display_port_t *port;       /* display port */
+		void               *state;      /* and its port driver state */
+} mode_pd_state_t;
+
+typedef struct _mode_state_t {
+	mode_pd_state_t pd_state[MAX_PORT_DRIVERS];
+} mode_state_t;
+
+
+/**
+ * This holds information about a framebuffer
+ */
+typedef struct _emgd_framebuffer {
+	struct drm_framebuffer base;
+	enum {
+		GMM_FRAMEBUFFER,
+		PVR_FRAMEBUFFER
+	} type;
+	void *pvr_meminfo;
+	struct page **pagelist;
+	unsigned long gtt_offset;
+	unsigned long handle;
+} emgd_framebuffer_t;
+
+typedef struct _emgdfb_par {
+        struct drm_device  *dev;
+        emgd_framebuffer_t *emgd_fb;
+} emgdfb_par_t;
+
+
+/**
+ * This holds information about a CRTC.
+ */
+typedef struct _emgd_crtc {
+        struct drm_crtc         base;
+
+		/* Spinlock to protect access to this structure */
+		spinlock_t              crtc_lock;
+
+        int                     crtc_id;
+        igd_display_pipe_t     *igd_pipe;
+        emgd_framebuffer_t     *fbdev_fb;
+        struct drm_mode_set     mode_set;
+        struct drm_display_mode saved_mode;
+        struct drm_display_mode saved_adjusted_mode;
+        unsigned char           lut_r[256];
+        unsigned char           lut_g[256];
+        unsigned char           lut_b[256];
+        unsigned char           lut_a[256];
+
+		/* Flip request work task */
+		struct work_struct      flip_work;
+		unsigned char           flip_work_queued;
+
+		/*
+		 * Framebuffer that we're in the process of flipping to (may not
+		 * actually show up until rendering is complete, the actual
+		 * registers are programmed, and a vblank happens).
+		 */
+		emgd_framebuffer_t *newfb;
+
+		/*
+		 * Target for rendering completion to allow flip to proceed.
+		 * Rendering operations may continue to be dispatched against this
+		 * FB after the flip ioctl is called, so we need to track the
+		 * number of pending operations at the point the ioctl was called
+		 * and use that as our target to flip on.
+		 */
+		unsigned long render_complete_at;
+
+		/*
+		 * Are we waiting for the next vblank event to perform flip cleanup
+		 * on this CRTC?  Flip cleanup primarily involves sending a
+		 * notification event back to userspace.
+		 */
+		unsigned char vblank_expected;
+
+		/* Userspace event to send back upon flip completion. */
+		struct drm_pending_vblank_event *flip_event;
+} emgd_crtc_t;
+
+
+/**
+ * This holds information about an individual encoder
+ *  */
+typedef struct _emgd_encoder {
+        struct drm_encoder base;
+
+        unsigned long       crtc_mask;
+        unsigned long       clone_mask;
+        igd_display_port_t *igd_port;
+        mode_pd_state_t     state;
+        /* flags is a bit mask. For information
+         * on the different masks, see ENCODER_FLAG_xx
+         */
+		unsigned long       flags;
+} emgd_encoder_t;
+
+#define ENCODER_FLAG_FIRST_DPMS  0x1
+#define ENCODER_FLAG_FIRST_ALTER 0x2
+#define ENCODER_FLAG_SEAMLESS    0x4
+
+/**
+ *  * This holds information about an individual output
+ *   */
+typedef struct _emgd_connector {
+        struct drm_connector  base;
+
+        emgd_encoder_t       *encoder;
+        unsigned long         type;
+        struct drm_property **properties;
+        unsigned long         num_of_properties;
+
+        struct _drm_emgd_private *priv;
+} emgd_connector_t;
+
+/**
+ *  * This holds information on our framebuffer device.
+ *   */
+typedef struct _emgd_fbdev {
+        struct drm_fb_helper      helper;
+
+        emgd_framebuffer_t       *emgd_fb;
+        u32                       pseudo_palette[17];
+        struct list_head          fbdev_list;
+        struct _drm_emgd_private *priv;
+} emgd_fbdev_t;
+
+
+
+
+#endif // _IGD_MODE_H_
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/mode_access.h b/drivers/gpu/drm/emgd/emgd/include/mode_access.h
new file mode 100644
index 0000000..307739a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/mode_access.h
@@ -0,0 +1,52 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mode_access.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Contains functional support to set video mode
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_MODEACCESS_H
+#define _IGD_MODEACCESS_H
+
+#include <mode.h>
+#include <context.h>
+
+extern int mode_getresolution(igd_display_h display_h,
+	unsigned long *width,
+	unsigned long *height);
+
+/*
+ * The power state for the display is the lower(power) of the display
+ * state and the device state. A larger power_state number means a
+ * lower power state.
+ */
+#define GET_DISPLAY_POWER_STATE(d_h, pn) \
+(GET_DEVICE_POWER_STATE(d_h) > ((PORT(d_h, pn))->power_state) ? \
+GET_DEVICE_POWER_STATE(d_h) : ((PORT(d_h, pn))->power_state))
+
+#endif /* _IGD_MODEACCESS_H */
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/module_init.h b/drivers/gpu/drm/emgd/emgd/include/module_init.h
new file mode 100644
index 0000000..8feafea
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/module_init.h
@@ -0,0 +1,109 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: module_init.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the init/power/shutdown prototypes for all optional
+ *  (and possibly other) modules.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _MODULE_INIT_H
+#define _MODULE_INIT_H
+
+#include <config.h>
+#include <context.h>
+
+extern int mode_init(igd_context_t *context);
+extern int dsp_init(igd_context_t *context);
+extern int pi_init(igd_context_t *context);
+extern int _init_2d(igd_context_t *context);
+extern int _blend_init(igd_context_t *context);
+extern int _overlay_init(igd_context_t *context, igd_param_t *params);
+extern int _cmd_init(igd_context_t *context);
+extern int _pwr_init(igd_context_t *context);
+extern int _reset_init(igd_context_t *context);
+extern int _reg_init(igd_context_t *context, unsigned long flags);
+extern int appcontext_init(igd_context_t *context);
+extern int gmm_init(igd_context_t *context, unsigned long scratch_mem,
+		unsigned long max_fb_size);
+extern void gmm_shutdown(igd_context_t *context);
+
+
+#ifdef CONFIG_2D
+#define INIT_2D(c) _init_2d(c);
+#else
+#define INIT_2D(c) 0
+#endif
+
+#ifdef CONFIG_BLEND
+#define BLEND_INIT(c) _blend_init(c);
+#else
+#define BLEND_INIT(c) 0
+#endif
+
+#ifdef CONFIG_OVERLAY
+#define OVERLAY_INIT(c, p) _overlay_init(c, p)
+#else
+#define OVERLAY_INIT(a, p) 0
+#endif
+
+#ifdef CONFIG_CMD
+#define CMD_INIT(a) _cmd_init(a);
+#else
+#define CMD_INIT(a) 0
+#endif
+
+#ifdef CONFIG_POWER
+#define PWR_INIT(a) _pwr_init(a);
+#else
+#define PWR_INIT(a) 0
+#endif
+
+#ifdef CONFIG_REG
+#define REG_INIT(a, b) _reg_init(a, b);
+#else
+#define REG_INIT(a, b) 0
+#endif
+
+#ifdef CONFIG_RESET
+#define RESET_INIT(a) _reset_init(a);
+#else
+#define RESET_INIT(a) 0
+#endif
+
+#ifdef CONFIG_INTERRUPT
+#define INTERRUPT_INIT(did, mmadr) OS_INIT_INTERRUPT(did, mmadr);
+#else
+#define INTERRUPT_INIT(did, mmadr) 0
+#endif
+
+#ifdef CONFIG_APPCONTEXT
+#define APPCONTEXT_INIT(a) appcontext_init(a)
+#else
+#define APPCONTEXT_INIT(a) 0
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/msvdx.h b/drivers/gpu/drm/emgd/emgd/include/msvdx.h
new file mode 100644
index 0000000..19ec924
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/msvdx.h
@@ -0,0 +1,257 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: msvdx.h
+ * $Revision: 1.20 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  These are the defines specific to the MSDVX engine code.
+ *-----------------------------------------------------------------------------
+ */
+#include <linux/list.h>
+#include <context.h>
+
+
+extern unsigned long _msvdx_base;
+#define MSVDX_BASE _msvdx_base
+
+#ifndef _MSVDX_H
+#define _MSVDX_H
+
+/* MTX registers */
+#define PSB_MSVDX_MTX_ENABLE                      (MSVDX_BASE + 0x0000)
+#define PSB_MSVDX_MTX_STATUS                      (MSVDX_BASE + 0x0008)
+#define PSB_MSVDX_MTX_KICK                        (MSVDX_BASE + 0x0080)
+#define PSB_MSVDX_MTX_KICKI                       (MSVDX_BASE + 0x0088)
+#define PSB_MSVDX_MTX_FAULT0                      (MSVDX_BASE + 0x0090)
+#define PSB_MSVDX_MTX_REGISTER_READ_WRITE_DATA    (MSVDX_BASE + 0x00f8)
+#define PSB_MSVDX_MTX_REGISTER_READ_WRITE_REQUEST (MSVDX_BASE + 0x00fc)
+#define PSB_MSVDX_MTX_RAM_ACCESS_DATA_EXCHANGE    (MSVDX_BASE + 0x0100)
+#define PSB_MSVDX_MTX_RAM_ACCESS_DATA_TRANSFER    (MSVDX_BASE + 0x0104)
+#define PSB_MSVDX_MTX_RAM_ACCESS_CONTROL          (MSVDX_BASE + 0x0108)
+#define PSB_MSVDX_MTX_RAM_ACCESS_STATUS           (MSVDX_BASE + 0x010c)
+#define PSB_MSVDX_MTX_SOFT_RESET                  (MSVDX_BASE + 0x0200)
+#define PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_TIMERDIV_OFFSET (MSVDX_BASE + 0x0208)
+
+#define PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAC      (MSVDX_BASE + 0x0340)
+#define PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAA      (MSVDX_BASE + 0x0344)
+#define PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAS0     (MSVDX_BASE + 0x0348)
+#define PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAT      (MSVDX_BASE + 0x0350)
+
+#define PSB_MSVDX_DMAC_SETUP                      (MSVDX_BASE + 0x0500)
+#define PSB_MSVDX_DMAC_COUNT                      (MSVDX_BASE + 0x0504)
+#define PSB_MSVDX_DMAC_PERIPH                     (MSVDX_BASE + 0x0508)
+#define PSB_MSVDX_DMAC_IRQ_STAT                   (MSVDX_BASE + 0x050c)
+#define PSB_MSVDX_DMAC_PERIPHERAL_ADDR            (MSVDX_BASE + 0x0514)
+/* MSVDX registers */
+#define PSB_MSVDX_CONTROL                         (MSVDX_BASE + 0x0600)
+#define PSB_MSVDX_INTERRUPT_STATUS                (MSVDX_BASE + 0x0608)
+#define PSB_MSVDX_INTERRUPT_CLEAR                 (MSVDX_BASE + 0x060c)
+#define PSB_MSVDX_HOST_INTERRUPT_ENABLE           (MSVDX_BASE + 0x0610)
+#define PSB_MSVDX_MAN_CLK_ENABLE                  (MSVDX_BASE + 0x0620)
+#define PSB_MSVDX_CORE_REV                        (MSVDX_BASE + 0x0640)
+#define PSB_MSVDX_MMU_CONTROL0                    (MSVDX_BASE + 0x0680)
+#define PSB_MSVDX_MMU_CONTROL1                    (MSVDX_BASE + 0x0684)
+#define PSB_MSVDX_MMU_BANK_INDEX                  (MSVDX_BASE + 0x0688)
+#define PSB_MSVDX_MMU_STATUS                      (MSVDX_BASE + 0x068c)
+#define PSB_MSVDX_MMU_DIR_LIST_BASE0              (MSVDX_BASE + 0x0694)
+#define PSB_MSVDX_MMU_DIR_LIST_BASE1              (MSVDX_BASE + 0x0698)
+#define PSB_MSVDX_MMU_DIR_LIST_BASE2              (MSVDX_BASE + 0x069c)
+#define PSB_MSVDX_MMU_DIR_LIST_BASE3              (MSVDX_BASE + 0x06a0)
+#define PSB_MSVDX_MMU_MEM_REQ                     (MSVDX_BASE + 0x06d0)
+#define PSB_MSVDX_MTX_RAM_BANK                    (MSVDX_BASE + 0x06f0)
+/* RENDEC registers */
+#define PSB_MSVDX_RENDEC_CONTROL0                 (MSVDX_BASE + 0x0868)
+#define PSB_MSVDX_RENDEC_CONTROL1                 (MSVDX_BASE + 0x086C)
+#define PSB_MSVDX_RENDEC_BUFFER_SIZE              (MSVDX_BASE + 0x0870)
+#define PSB_MSVDX_RENDEC_BASE_ADDR0               (MSVDX_BASE + 0x0874)
+#define PSB_MSVDX_RENDEC_BASE_ADDR1               (MSVDX_BASE + 0x0878)
+#define PSB_MSVDX_RENDEC_READ_DATA                (MSVDX_BASE + 0x0898)
+#define PSB_MSVDX_RENDEC_CONTEXT0                 (MSVDX_BASE + 0x0950)
+#define PSB_MSVDX_RENDEC_CONTEXT1                 (MSVDX_BASE + 0x0954)
+#define PSB_MSVDX_RENDEC_CONTEXT2                 (MSVDX_BASE + 0x0958)
+#define PSB_MSVDX_RENDEC_CONTEXT3                 (MSVDX_BASE + 0x095C)
+#define PSB_MSVDX_RENDEC_CONTEXT4                 (MSVDX_BASE + 0x0960)
+#define PSB_MSVDX_RENDEC_CONTEXT5                 (MSVDX_BASE + 0x0964)
+
+#define MSVDX_COMMS_AREA_ADDR                     (MSVDX_BASE + 0x02fd0)
+#define PSB_MSVDX_COMMS_FW_STATUS                 (MSVDX_COMMS_AREA_ADDR + 0x00)
+#define PSB_MSVDX_COMMS_VLR_RES                   (MSVDX_COMMS_AREA_ADDR + 0x04)
+#define PSB_MSVDX_COMMS_SCRATCH                   (MSVDX_COMMS_AREA_ADDR + 0x08)
+#define PSB_MSVDX_COMMS_MSG_COUNTER               (MSVDX_COMMS_AREA_ADDR + 0x0c)
+#define PSB_MSVDX_COMMS_SIGNATURE                 (MSVDX_COMMS_AREA_ADDR + 0x10)
+#define PSB_MSVDX_COMMS_TO_HOST_BUF_SIZE          (MSVDX_COMMS_AREA_ADDR + 0x14)
+#define PSB_MSVDX_COMMS_TO_HOST_RD_INDEX          (MSVDX_COMMS_AREA_ADDR + 0x18)
+#define PSB_MSVDX_COMMS_TO_HOST_WRT_INDEX         (MSVDX_COMMS_AREA_ADDR + 0x1c)
+#define PSB_MSVDX_COMMS_TO_MTX_BUF_SIZE           (MSVDX_COMMS_AREA_ADDR + 0x20)
+#define PSB_MSVDX_COMMS_TO_MTX_RD_INDEX           (MSVDX_COMMS_AREA_ADDR + 0x24)
+#define PSB_MSVDX_COMMS_OFFSET_FLAGS              (MSVDX_COMMS_AREA_ADDR + 0x28)
+#define PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX          (MSVDX_COMMS_AREA_ADDR + 0x2c)
+
+#define MTX_CORE_CODE_MEM                         (0x10)
+#define MTX_CORE_DATA_MEM                         (0x18)
+#define MTX_CODE_BASE                             (0x80900000)
+#define MTX_DATA_BASE                             (0x82880000)
+#define PC_START_ADDRESS                          (0x80900000)
+#define MSVDX_MTX_ENABLE_MTX_ENABLE_MASK          (0x00000001)
+#define MTX_PC                                    (5)
+#define RENDEC_A_SIZE                             (1024 * 1024 * 2)
+#define RENDEC_B_SIZE                             (RENDEC_A_SIZE / 4)
+#define FWRK_PADMSG_SIZE                          (2)
+#define FWRK_MSGID_PADDING                        (0)
+#define FWRK_MSGID_START_PSR_HOSTMTX_MSG          (0x80)
+#define FWRK_MSGID_START_PSR_MTXHOST_MSG          (0xc0)
+#define MSVDX_CLK_ENABLE_CR_CORE_MASK             (0x00000001)
+#define MSVDX_CLK_ENABLE_CR_VDEB_PROCESS_MASK     (0x00000002)
+#define MSVDX_CLK_ENABLE_CR_VDEB_ACCESS_MASK      (0x00000004)
+#define MSVDX_CLK_ENABLE_CR_VDMC_MASK             (0x00000008)
+#define MSVDX_CLK_ENABLE_CR_VEC_ENTDEC_MASK       (0x00000010)
+#define MSVDX_CLK_ENABLE_CR_VEC_ITRANS_MASK       (0x00000020)
+#define MSVDX_CLK_ENABLE_CR_MTX_MASK              (0x00000040)
+#define MSVDX_CLK_ENABLE_CR_VDEB_ACCESS_AUTO_MASK (0x00040000)
+#define MSVDX_CLK_ENABLE_CR_VDMC_AUTO_MASK        (0x00080000)
+#define MSVDX_CLK_ENABLE_CR_VEC_ENTDEC_AUTO_MASK  (0x00100000)
+#define MSVDX_CLK_ENABLE_CR_VEC_ITRANS_AUTO_MASK  (0x00200000)
+
+
+#define PSB_CLK_ENABLE_ALL \
+	MSVDX_CLK_ENABLE_CR_CORE_MASK |\
+	MSVDX_CLK_ENABLE_CR_VDEB_PROCESS_MASK |\
+	MSVDX_CLK_ENABLE_CR_VDEB_ACCESS_MASK |\
+	MSVDX_CLK_ENABLE_CR_VDMC_MASK |\
+	MSVDX_CLK_ENABLE_CR_VEC_ENTDEC_MASK |\
+	MSVDX_CLK_ENABLE_CR_VEC_ITRANS_MASK |\
+	MSVDX_CLK_ENABLE_CR_MTX_MASK
+
+#define PSB_CLK_ENABLE_MIN    MSVDX_CLK_ENABLE_CR_CORE_MASK
+#define PSB_MSVDX_FW_STATUS_HW_IDLE                 (0x00000001)
+
+#define MSVDX_DEVICE_NODE_FLAGS_MMU_NONOPT_INV      (0x00000002)
+#define MSVDX_DEVICE_NODE_FLAGS_MMU_HW_INVALIDATION (0x00000020)
+#define MSVDX_DEVICE_NODE_FLAG_BRN23154_BLOCK_ON_FE (0x00000200)
+
+#define MSVDX_DEVICE_NODE_FLAGS_DEFAULT \
+	MSVDX_DEVICE_NODE_FLAGS_MMU_HW_INVALIDATION
+
+
+#define FW_VA_RENDER_HOST_INT		0x00004000
+
+#ifndef list_first_entry
+#define list_first_entry(ptr, type, member) \
+	list_entry((ptr)->next, type, member)
+#endif
+
+enum {
+	/*! Sent by the video driver on the host to the mtx firmware. */
+	IGD_MSGID_INIT               = FWRK_MSGID_START_PSR_HOSTMTX_MSG,
+	IGD_MSGID_RENDER,
+	IGD_MSGID_DEBLOCK,
+	IGD_MSGID_BUBBLE,
+
+	/* Test Messages */
+	IGD_MSGID_TEST1,
+	IGD_MSGID_TEST2,
+
+	/*! Sent by the mtx firmware to itself. */
+	IGD_MSGID_RENDER_MC_INTERRUPT,
+
+	/*! Sent by the DXVA firmware on the MTX to the host. */
+	IGD_MSGID_CMD_COMPLETED  = FWRK_MSGID_START_PSR_MTXHOST_MSG,
+	IGD_MSGID_CMD_COMPLETED_BATCH,
+	IGD_MSGID_DEBLOCK_REQUIRED,
+	IGD_MSGID_TEST_RESPONCE,
+	IGD_MSGID_ACK,
+
+	IGD_MSGID_CMD_FAILED,
+	IGD_MSGID_CMD_UNSUPPORTED,
+	IGD_MSGID_CMD_HW_PANIC,
+};
+
+struct msvdx_cmd_queue {
+	struct list_head head;
+	void *cmd;
+	unsigned long cmd_size;
+};
+
+/* TODO:  From UMG, temporary put here first, may need to use this
+ * MSVDX private structure
+ */
+
+struct msvdx_private {
+	int msvdx_needs_reset;
+
+	unsigned int pmstate;
+
+	struct sysfs_dirent *sysfs_pmstate;
+
+	uint32_t msvdx_current_sequence;
+	uint32_t msvdx_last_sequence;
+
+	/*
+	 * MSVDX Rendec Memory
+	 */
+	uint32_t base_addr0;
+	uint32_t base_addr1;
+
+	/*
+	 * msvdx command queue
+	 */
+	/* spinlock_t msvdx_lock; */
+	/* struct mutex msvdx_mutex; */
+	struct list_head msvdx_queue;
+	int msvdx_busy;
+	int msvdx_fw_loaded;
+	void *msvdx_fw;
+	int msvdx_fw_size;
+
+	struct list_head deblock_queue; /* deblock parameter list */
+
+	uint32_t msvdx_hw_busy;
+};
+
+typedef struct msvdx_fw_ {
+	unsigned long fw_text_size;
+	unsigned long *fw_text;
+	unsigned long fw_data_location;
+	unsigned long fw_data_size;
+	unsigned long *fw_data;
+	unsigned long fw_version_size;
+	char *fw_version;
+} msvdx_fw_t;
+
+
+int process_video_decode_plb(igd_context_t *context, unsigned long offset, void *virt_addr, unsigned long *fence_id);
+int msvdx_query_plb(igd_context_t *context, unsigned long *status);
+int msvdx_preinit_mmu(unsigned long hmemcxt);
+int msvdx_init_plb(unsigned long base0, unsigned long base1,
+           void *msvdx_fw, unsigned long msvdx_fw_size, int reset_flag);
+int msvdx_init_compositor_mmu(unsigned long mmu_base);
+int msvdx_uninit_plb(igd_context_t *context);
+int msvdx_close_context(igd_context_t *context);
+int msvdx_create_context(igd_context_t *context);
+int msvdx_shutdown_plb(igd_context_t *context);
+int msvdx_get_fence_id(igd_context_t *context, unsigned long *fence_id);
+int msvdx_flush_tlb(igd_context_t *context);
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/pci.h b/drivers/gpu/drm/emgd/emgd/include/pci.h
new file mode 100644
index 0000000..9a5fc5c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/pci.h
@@ -0,0 +1,257 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pci.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains OS abstractions for PCI function calls.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OAL_PCI_H
+#define _OAL_PCI_H
+
+
+/*
+ * Standard PCI register definitions.
+ * Only the first 64 bytes are standardized so thiese must all be
+ * defined to numbers less than 0x40
+ */
+#define PCI_RID            0x08
+
+#define PCI_BAR_0           0x10
+#define PCI_BAR_1           0x14
+#define PCI_BAR_2           0x18
+#define PCI_BAR_3           0x1c
+#define PCI_BAR_4           0x20
+#define PCI_BAR_5           0x24
+
+#define PCI_INTERRUPT_LINE  0x3c
+
+/*
+ * This macro _may_ be defined by an OAL port to enable the PCI device
+ * Commonly this is used to enable the device on EFI
+ * The prototype of the function looks like this:
+ * int os_enable_pci(os_pci_dev dev);
+ * The return value from this function should be 0 to indicate success
+ * or non zero to indicate failure.
+ */
+#ifdef _OS_ENABLE_PCI
+#define OS_ENABLE_PCI(a) _OS_ENABLE_PCI(a)
+#else
+#define OS_ENABLE_PCI(a) 0
+#endif
+
+#define OS_PCI_GET_SLOT_ADDRESS(p, b, s, f) os_pci_get_slot_address(p, b, s, f)
+#define OS_PCI_READ_CONFIG_8(p, o, v)       os_pci_read_config_8(p, o, (v))
+#define OS_PCI_READ_CONFIG_16(p, o, v)      os_pci_read_config_16(p, o, (v))
+#define OS_PCI_READ_CONFIG_32(p, o, v)      os_pci_read_config_32(p, o, (v))
+#define OS_PCI_WRITE_CONFIG_8(p, o, v)      os_pci_write_config_8(p, o, v)
+#define OS_PCI_WRITE_CONFIG_16(p, o, v)     os_pci_write_config_16(p, o, v)
+#define OS_PCI_WRITE_CONFIG_32(p, o, v)     os_pci_write_config_32(p, o, v)
+#define OS_PCI_FREE_DEVICE(p)               os_pci_free_device(p)
+
+#define OS_PCI_FIND_DEVICE(v, d, p, bus, dev, func) \
+	os_pci_find_device(v, d, p, bus, dev, func)
+
+/*****************************************************************************
+ * Variable: os_pci_dev_t
+ *
+ * Description:
+ *  This is a data type that serves as a handle for allocated PCI device.
+ *
+ ****************************************************************************/
+typedef unsigned char *os_pci_dev_t;
+
+
+/*****************************************************************************
+ * Function: os_pci_find_device
+ *
+ * Parameters:
+ *  vendor_id : The vendor ID for the device to be found.
+ *  device_id : The vendor ID for the device to be found.
+ *  bus       : The bus number of the device in the PCI topology
+ *  dev       : The device number of the device in the PCI topology
+ *  func      : The function number of the device in the PCI topology
+ *  pci_device: The last found os_pci_dev_t or NULL.
+ *
+ * Description:
+ *  This function will find the PCI device for the paticular vendor and device,
+ *  bus, device, and function number.The pci_device parameter should be NULL when
+ *  calling the first time
+ *  and the last returned value when searching for multiple devices of
+ *  the same ID.
+ *
+ *  Notes: If the bus number is 0xFFFF, then the function searches for that
+ *  vendor_id, device_id pair in the whole PCI topology of the system i.e
+ *  it goes through all the buses, devices, functions in the system
+ *
+ ****************************************************************************/
+os_pci_dev_t os_pci_find_device(
+		unsigned short vendor_id,
+		unsigned short device_id,
+		unsigned short bus,
+		unsigned short dev,
+		unsigned short func,
+		os_pci_dev_t pci_dev);
+
+/*****************************************************************************
+ * Function: os_get_slot_address
+ *
+ * Parameters:
+ *  pci_device: The os_pci_dev_t to query.
+ *  bus: The returned bus or NULL if the bus is not needed.
+ *  slot: The returned slot or NULL if the bus is not needed.
+ *  func: The returned func or NULL if the bus is not needed.
+ *
+ * Description:
+ *  This function will return the bus slot and function for an os_pci_dev_t
+ *  previously obtained from os_pci_find_device(). Any of the bus/slot/func
+ *  parameters may be null if the information is not needed.
+ *
+ ****************************************************************************/
+int os_pci_get_slot_address(
+	os_pci_dev_t pci_dev,
+	unsigned int *bus,
+	unsigned int *slot,
+	unsigned int *func);
+
+/*****************************************************************************
+ * Function: os_pci_read_config_8
+ *
+ * Description:
+ *  This function retrieves a byte of information, starting at the specified
+ *  offset, from the PCI configuration space on a particular PCI device.
+ *
+ ****************************************************************************/
+int os_pci_read_config_8(
+		os_pci_dev_t pci_dev,
+		unsigned long offset,
+		unsigned char* val
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_read_config_16
+ *
+ * Description:
+ *  This function retrieves a word of information, starting at the specified
+ *  offset, from the PCI configuration space on a particular PCI device.
+ *
+ ****************************************************************************/
+int os_pci_read_config_16(
+		os_pci_dev_t pci_dev,
+		unsigned long offset,
+		unsigned short* val
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_read_config_32
+ *
+ * Description:
+ *  This function retrieves double word of information, starting at the
+ *  specified offset, from the PCI configuration space on a particular PCI
+ *  device.
+ *
+ ****************************************************************************/
+int os_pci_read_config_32(
+		os_pci_dev_t pci_dev,
+		unsigned long offset,
+		unsigned long* val
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_write_config_8
+ *
+ * Description:
+ *  This function sets a byte of data, starting at the specified offset, to
+ *  the PCI configuration space for a particular PCI device.
+ *
+ ****************************************************************************/
+int os_pci_write_config_8(
+		os_pci_dev_t pci_dev,
+		unsigned long offset,
+		unsigned char val
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_write_config_16
+ *
+ * Description:
+ *  This function sets a word of data, starting at the specified offset, to
+ *  the PCI configuration space for a particular PCI device.
+ *
+ ****************************************************************************/
+int os_pci_write_config_16(
+		os_pci_dev_t pci_dev,
+		unsigned long offset,
+		unsigned short val
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_write_config_32
+ *
+ * Description:
+ *  This function sets double word of data, starting at the specified offset,
+ *  to the PCI configuration space for a particular PCI device.
+ *
+ ****************************************************************************/
+int os_pci_write_config_32(
+		os_pci_dev_t pci_dev,
+		unsigned long offset,
+		unsigned long val
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_disable_legacy_vga_decoding
+ *
+ * Description:
+ *  Disabled legacy VGA decoding on a specific PCI device if the kernel is
+ *  compiled with support for the VGA arbiter.  If the VGA arbiter is not
+ *  compiled in, this function is a noop.
+ *
+ ****************************************************************************/
+int os_pci_disable_legacy_vga_decoding(
+		os_pci_dev_t pci_dev
+		);
+
+
+/*****************************************************************************
+ * Function: os_pci_free_device
+ *
+ * Description:
+ *  This function free the os_pci_dev_t * that previously allocated with the
+ *  os_pci_find_device.
+ *
+ ****************************************************************************/
+void os_pci_free_device(
+		os_pci_dev_t pci_dev
+		);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/pd.h b/drivers/gpu/drm/emgd/emgd/include/pd.h
new file mode 100644
index 0000000..388a2af
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/pd.h
@@ -0,0 +1,766 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pd.h
+ * $Revision: 1.12 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PD_H_
+#define _PD_H_
+
+#include <igd_pd.h>
+#include <igd_debug.h>
+
+/* PD SDK version 2 bytes,
+ *    1st byte is major version,
+ *    second byte is minor version.
+ */
+
+/* Update the version number each time there is an
+ * API Change or Change in data structures
+ */
+#define PD_SDK_VERSION    0x0300             /* PD SDK Version */
+
+#ifndef NULL
+#ifdef __cplusplus
+#define NULL    0
+#else
+#define NULL    ((void *)0)
+#endif
+#endif
+
+#ifndef TRUE
+#define TRUE   1
+#endif
+
+#ifndef FALSE
+#define FALSE  0
+#endif
+
+/* This needs to contain the unsigned long for vBIOS. */
+#ifndef BIT
+#define BIT(x) (1L<<x)
+#endif
+
+/* Macro to return:
+ *    minimum, if the value < min value
+ *    maximum, if the value > max value
+ *    value, otherwise
+ */
+#define MINMAX(val, min, max) ((val < min) ? min : ((val > max) ? max : val))
+
+#define PD_REG_LIST_END      0xFFFFFFFF
+#define PD_DAB_LIST_END      0xFFFFFFFF
+#define PD_TIMING_LIST_END   0xFFFF
+
+/* Error codes used for return values */
+#define  PD_SUCCESS           0x00    /* Success */
+#define  PD_ERR_NOMEM         0x01    /* Unable to allocated requested mem */
+#define  PD_ERR_NODEV         0x02    /* No Device/Codec found */
+#define  PD_ERR_NODIS         0x03    /* No display found */
+#define  PD_ERR_INVALID_PTR   0x04    /* Invalid pointer passed */
+#define  PD_ERR_NULL_PTR      0x05    /* Null ptr passed */
+#define  PD_ERR_DISPLAY_TYPE  0x06    /* Unsupported display type specified */
+#define  PD_ERR_NOPORT_AVAIL  0x07    /* No port available */
+#define  PD_ERR_INTERNAL      0x08    /* Critical internal error */
+#define  PD_ERR_INVALID_POWER 0x09    /* Invalid power state specified */
+#define  PD_ERR_HAND_SHAKE    0x0A    /* Magic cookie hand shake failed */
+#define  PD_ERR_MODE_NOTSUPP  0x0B    /* Unsupported mode */
+#define  PD_ERR_I2C_READ      0x0C    /* I2C read error */
+#define  PD_ERR_I2C_WRITE     0x0D    /* I2C write error */
+#define  PD_ERR_NULL_STATE    0x0E    /* Saved state is NULL */
+/* PD_ERR_NOATTR removed. Return success with 0 count instead */
+#define  PD_ERR_NO_TIMINGS    0x10    /* No timing list */
+#define  PD_ERR_INVALID_ATTR  0x11    /* Invalid attribute */
+#define  PD_ERR_INCORR_ATTR_VALUE 0x12 /* Incorrect attr value */
+#define  PD_ERR_ATTR_CANT_CHANGE  0x13 /* Attribute value cannot be changed */
+#define  PD_ERR_VER_MISMATCH      0x14 /* PD SDK version mismatch */
+#define  PD_ERR_UNSUCCESSFUL      0x15 /* Operation unsuccessful */
+
+typedef struct _pd_reg {
+	unsigned long reg;
+	unsigned long value;
+}pd_reg_t;
+
+
+/* The following structures holds CEA EDID Extension data
+ * Edid and possibly display id would use this structure as
+ * well. PD callback would have a callback function to one the
+ * structure to retrieve EDID like data (ELD)
+ */
+
+
+#define CEA_IEEE_HDMI_ID	0x000C03
+/* Aspect Ratio */
+#define PD_ASPECT_RATIO_NO_DATA 0
+#define PD_ASPECT_RATIO_4_3 	1
+#define PD_ASPECT_RATIO_16_9	2
+/* ELD status */
+#define ELD_AVAIL	0x01
+#define ELD_CANNED	0x02
+/* Transmitter audio charactheristic */
+#define PD_AUDIO_CHAR_AVAIL	0x10
+/* CEA data block type */
+#define CEA_VIDEO_DATA_BLOCK	0x40
+#define CEA_AUDIO_DATA_BLOCK	0x20
+#define CEA_VENDOR_DATA_BLOCK	0x60
+#define CEA_SPEAKER_DATA_BLOCK	0x80
+/* Audio block Tag Code */
+#define CEA_AUDIO_LPCM		1
+#define CEA_AUDIO_AC3		2
+#define CEA_AUDIO_MPG1		3
+#define CEA_AUDIO_MP3		4
+#define CEA_AUDIO_MPG2		5
+#define CEA_AUDIO_AAC		6
+#define CEA_AUDIO_DTS		7
+#define CEA_AUDIO_ATRAC		8
+#define CEA_AUDIO_SACD		9
+#define CEA_AUDIO_DD_PLUS	10
+#define CEA_AUDIO_DTS_HD	11
+#define CEA_AUDIO_MLP		12
+/* Pixel Replication */
+#define PIX_REPLICATION_0	1
+#define PIX_REPLICATION_1	2
+#define PIX_REPLICATION_3	4
+/* Quantization */
+#define HDMI_QUANTIZATION_RGB_256	0x00
+#define HDMI_QUANTIZATION_RGB_220	0x01
+#define HDMI_QUANTIZATION_YUV_422	0x02
+#define HDMI_QUANTIZATION_YUV_44	0x03
+/* Colorimetry */
+#define HDMI_COLORIMETRY_NODATA		0x00
+#define HDMI_COLORIMETRY_ITU601		0x01 /* SMPTE 170M, ITU601 */
+#define HDMI_COLORIMETRY_ITU709		0x02
+/* Audio CAP(48,96,192)Khz refer to audio_cap in CEA ELD extension*/
+#define CAP_48_KHZ	0
+#define CAP_96_KHZ	1
+#define CAP_192_KHZ	2
+/* Vendor Specific Data Block */
+#define VSBD_LATENCY_FIELD			8
+typedef struct _cea_audio_format_t{
+	union{
+		unsigned char byte1;
+		struct{
+			unsigned char max_channels		: 3;
+			unsigned char audio_format_code : 4;
+			unsigned char reserve_byte1		: 1;
+		};
+	};
+	union{
+		unsigned char byte2;
+		struct{
+			unsigned char _32khz			: 1;
+			unsigned char _44khz 			: 1;
+			unsigned char _48khz			: 1;
+			unsigned char _88khz			: 1;
+			unsigned char _96khz			: 1;
+			unsigned char _176khz			: 1;
+			unsigned char _192khz			: 1;
+			unsigned char reserve_byte2		: 1;
+		};
+	};
+	/* 3rd byte differs between compressed & uncompressed audio */
+	union{
+		unsigned char max_bitrate;
+		struct{
+			unsigned char _16bit		: 1;
+			unsigned char _20bit		: 1;
+			unsigned char _24bit		: 1;
+			unsigned char reserve_byte3	: 5;
+		};
+	};
+
+}cea_audio_format_t;
+
+typedef struct _speaker_allocation_data_t{
+	struct{
+		unsigned char	rsvd	: 1;
+		unsigned char	rlc_rrc : 1;
+		unsigned char	flc_frc : 1;
+		unsigned char	rc		: 1;
+		unsigned char	rl_rr	: 1;
+		unsigned char	fc		: 1;
+		unsigned char	lfe		: 1;
+		unsigned char	fl_fr	: 1;
+	};
+	unsigned char reserved[2];
+}speaker_allocation_data_t;
+
+typedef union {
+	unsigned char value;
+	struct {
+		unsigned char code: 7;
+		unsigned char native: 1;
+	};
+} cea_video_blk_t;
+
+typedef struct _audio_capability_t{
+	unsigned char max_channels;
+	unsigned char _20bit;
+	unsigned char _24bit;
+}audio_capability_t;
+
+typedef struct _vsdb_t{
+	union{
+		unsigned char header;
+		struct{
+			unsigned char vendor_block_size: 5;
+			unsigned char tag : 3;
+		};
+	};
+	unsigned char	vendor_ieee_id[3];
+	unsigned char	src_phy_add[2];
+	unsigned char	support_ai;
+	unsigned char	max_tmds_clck;
+	union{
+		unsigned char latency_fields;
+		struct{
+			unsigned char reserve	: 6;
+			unsigned char i_latency	: 1;
+			unsigned char p_latency	: 1;
+		};
+	};
+	unsigned char p_video_latency;
+	unsigned char p_audio_latency;
+	unsigned char i_video_latency;
+	unsigned char i_audio_latency;
+
+}vsdb_t;
+
+typedef struct _cea_extension{
+	unsigned char	rev_number;
+	unsigned char	canned_eld;
+	union{
+		unsigned char	caps;
+		struct{
+			unsigned char	total_native_format : 4;
+			unsigned char	YCC_422				: 1;
+			unsigned char	YCC_444				: 1;
+			unsigned char	audio_support		: 1;
+			unsigned char	underscan_support	: 1;
+		};
+	};
+
+	/* Short Video Descriptor */
+	int						total_short_video_desc;
+	cea_video_blk_t			*short_video_desc;
+	/* Short Audio Descriptor */
+	int						total_short_audio_desc;
+	cea_audio_format_t		*short_audio_desc;
+	/* Vendor Descriptor Block */
+	unsigned char			*vendor_data_block;
+
+	unsigned char		audio_flag;		/* Define is ELD status */
+	unsigned char		NPL;
+	unsigned char		K0;
+	unsigned char		K1;
+	audio_capability_t	audio_cap[3];	/* Panel audio capability (48,96,192)Khz*/
+
+	/* AVI Info frames data */
+	int					pixel_rep;		/* Pixel replication */
+	int					quantization;	/* Quantization */
+	int					aspect_ratio;	/* Aspect Ratio */
+	int					colorimetry;	/* Colorimetry */
+	int					video_code;
+	/* Consolidate ELD information here */
+	union{
+		unsigned char eld_ptr[256];
+		struct {
+			union {
+				unsigned char version;
+				struct {
+					unsigned char cea_ver: 3;
+					unsigned char eld_ver: 5;
+				};
+			};
+			union {
+				unsigned char capability;
+				struct {
+					unsigned char hdcp: 1;
+					unsigned char repeater: 1;
+					unsigned char _44ms: 1;
+					unsigned char reserved1: 5;
+				};
+			};
+			union {
+				unsigned short length;
+				struct {
+					unsigned short mnl: 3;
+					unsigned short vsdbl: 3;
+					unsigned short sadc: 4;
+					unsigned short reserved2: 6;
+				};
+			};
+			unsigned short	manu_id;
+			unsigned short	prod_id;
+			unsigned char	LPCM_CAD[3];	/* LPCM for ELD */
+			unsigned char	speaker_alloc_block[3];
+			vsdb_t			vendor_block;	/* 13 byte */
+			unsigned char	misc_data[229];
+		};
+	};
+}cea_extension_t;
+
+/* Following are the callback functions provided to port driver. It also
+ * provides a call back context. Every time port driver has to pass this
+ * callback context when calling a callback function.
+ *
+ * read_regs:     Port driver has to pass a list of pd_reg_t's which
+ *                it want to read. The list has to end with PD_I2C_LIST_END.
+ *                The values read will be set to the for every list->reg,
+ *                value will be set in list->value.
+ *
+ *                Return value == 0 on success
+ *                             != 0 on failure
+ *
+ * write_regs:    Port driver has to pass a list of pd_reg_t's which
+ *                it want to write. The list has to end with PD_LIST_END.
+ *                Caller has to provide already masked values. This callback
+ *                function doesn't do any masking. If, port driver wants to
+ *                write only some bits of a reg, first it has to read the
+ *                register and then set bits and write the register.
+ *
+ *                Return value == 0 on success
+ *                             != 0 on failure
+ */
+
+#define PD_REG_I2C    0x00000001
+#define PD_REG_LPC    0x00000002
+#define PD_REG_DMA    0x00000003
+#define PD_REG_PCI    0x00000004
+#define PD_REG_MIO    0x00000005   /* MMIO 32 bits    */
+#define PD_REG_PIO8   0x00000006   /* Port IO 1 byte */
+#define PD_REG_PIO16  0x00000007   /* Port IO 2 bytes */
+#define PD_REG_PIO32  0x00000008   /* Port IO 4 bytes */
+#define PD_REG_MIO8   0x00000009   /* MMIO 8 bits     */
+#define PD_REG_BRIDGE_OPCODE  0x0000000D /* Host bridge read and write */
+#define PD_REG_DDC            0x0000000E /* Read the ddc */
+#define PD_REG_DDC_FW            0x0000000F /* Read the ddc */
+
+typedef int (*pd_read_regs_p)(void *callback_context, pd_reg_t *list,
+		unsigned long type);
+typedef int (*pd_write_regs_p)(void *callback_context, pd_reg_t *list,
+		unsigned long type);
+
+typedef struct _pd_callback {
+	void *callback_context;
+	pd_read_regs_p  read_regs;
+	pd_write_regs_p write_regs;
+	unsigned long port_num;			/*	Added for SDVO port driver */
+	cea_extension_t	**eld;			/* EDID like Data */
+}pd_callback_t;
+
+/* IMP NOTE: All below structures should be with same size.
+ *         pd_attr_t            : General attribute structure
+ *         pd_range_attr_t      : Range type attribute structure
+ *         pd_list_attr_t       : List type attribute
+ *         pd_list_entry_attr_t : Entry for a list
+ *         pd_bool_attr_t       : Boolean type attribute
+ *         pd_buffer_attr_t     : Buffer type attribute
+ */
+typedef igd_attr_t            pd_attr_t;
+typedef igd_range_attr_t      pd_range_attr_t;
+typedef igd_list_attr_t       pd_list_attr_t;
+typedef igd_list_entry_attr_t pd_list_entry_attr_t;
+typedef igd_bool_attr_t       pd_bool_attr_t;
+typedef igd_buffer_attr_t     pd_buffer_attr_t;
+
+#define ATTR(a)   ((pd_attr_t *)a)               /* General attr */
+#define RATTR(a)  ((pd_range_attr_t *)a)         /* Range attr */
+#define LHATTR(a) ((pd_list_attr_t *)a)          /* List head attr */
+#define LEATTR(a) ((pd_list_entry_attr_t *)a)    /* List entry attr */
+#define BATTR(a)  ((pd_bool_attr_t *)a)          /* Bool attr */
+#define BUATTR(a) ((pd_buffer_attr_t *)a)        /* Buffer attr */
+
+/* Timing structure flag defines */
+#define PD_SCAN_INTERLACE        0x80000000
+#define PD_LINE_DOUBLE           0x40000000
+#define PD_PIXEL_DOUBLE          0x20000000
+#define PD_MODE_TEXT             0x10000000  /* VGA Text mode */
+
+#define PD_HSYNC_HIGH            0x08000000
+#define PD_VSYNC_HIGH            0x04000000
+#define PD_BLANK_LOW             0x02000000
+#define PD_MODE_VESA             0x01000000 /* VGA/VESA mode number is valid */
+
+#define PD_MODE_STALL            0x00800000   /* Flag to enable stall signal */
+#define PD_MODE_SCALE            0x00400000   /* Request NATIVE timings */
+
+#define PD_ASPECT_16_9           0x00200000   /* 16:9 aspect ratio, otherwise it is 4:3 */
+#define PD_MODE_CEA				 0x00100000
+
+#define PD_MODE_DTD              0x00080000   /* Read from EDID */
+#define PD_MODE_DTD_USER         0x00040000   /* User defined timing */
+#define PD_MODE_DTD_FP_NATIVE    0x00020000   /* Native fp timing */
+#define PD_MODE_SUPPORTED        0x00010000
+
+#define PD_MODE_FACTORY          0x00008000   /* Factory supported mode */
+#define PD_MODE_RB               0x00004000   /* Reduced blanking mode */
+
+/* Macro to put at the end timing table */
+#define  PD_TIMING_TABLE_END \
+{\
+	PD_TIMING_LIST_END, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
+	NULL, 0, 0\
+}\
+
+/* Timing structure */
+typedef struct _pd_timing {
+	unsigned short width;           /* width */
+	unsigned short height;          /* height */
+	unsigned short refresh;         /* refresh rate */
+	unsigned long  dclk;            /* refresh rate dot clock in kHz */
+	unsigned short htotal;          /* horizontal total */
+	unsigned short hblank_start;    /* horizontal blank start */
+	unsigned short hblank_end;      /* horizontal blank end */
+	unsigned short hsync_start;     /* horizontal sync start */
+	unsigned short hsync_end;       /* horizontal sync end */
+	unsigned short vtotal;          /* vertical total */
+	unsigned short vblank_start;    /* vertical blank start */
+	unsigned short vblank_end;      /* vertical blank end */
+	unsigned short vsync_start;     /* vertical sync start */
+	unsigned short vsync_end;       /* vertical sync end */
+	short mode_number; /* VGA or VESA mode number */
+	unsigned long mode_info_flags; /* Valid Flags
+									   - PD_SCAN_INTERLACE
+									   - PD_LINE_DOUBLE
+									   - PD_PIXEL_DOUBLE
+									   - PD_HSYNC_HIGH
+									   - PD_VSYNC_HIGH
+									   - PD_MODE_SUPPORTED
+									   - PD_MODE_DTD */
+	unsigned short x_offset;
+	unsigned short y_offset;
+	void *extn_ptr;  /* INTERNAL pointer for use by main driver only */
+	unsigned short reserved_dd; /* Reserved for device dependant layer */
+	unsigned short reserved_dd_ext; /* Reserved for device dependant layer */
+} pd_timing_t;
+
+/* Bit field flags for pd_driver_t->driver_flags */
+#define PD_FLAG_GANG_MODE       0x00000001   /* driver is running in gang mode*/
+#define PD_FLAG_CLK_SOURCE      0x00000002   /* clk source is port driver */
+#define PD_FLAG_PIPE_MASTER     0x00000004   /* master port driver */
+#define PD_FLAG_REV_DATA_ORDER  0x00000008   /* reverse data order requested */
+#define PD_FLAG_I740_DATA_ORDER 0x00000010   /* For I740 Data ordering. If this
+												flag isn't set, then it is
+												Flat panel data ordering */
+#define PD_FLAG_DUAL_DVO        0x00000020   /* Same port driver will be loaded
+											  * on both DVOB & DVOC with same
+											  * DAB */
+#define PD_FLAG_GANG_MODE_EVEN_ODD 0x00000040   /* pd wants gang mode in even
+												 * pixels on one DVO port &
+												 * odd pixels on other DVO port.
+												 * Default is upper half pixel
+												 * on one DVO port and lower
+												 * half on the other DVO port.
+												 */
+#define PD_FLAG_UP_SCALING          0x00000080 /* pd supports up-scaling */
+#define PD_FLAG_DOWN_SCALING        0x00000100 /* pd supports down-scaling */
+#define PD_FLAG_CLOCK_MASTER        0x00000200 /* clock master port driver */
+#define PD_FLAG_GANG_MODE_DVOCLKINV 0x00000400 /* GangMode DVO Clk inversion */
+#define PD_FLAG_NO_VGA_2X_IMAGE     0x00000800 /* Gang Mode operation might
+												* request this flag */
+
+/* Flag for set_mode function */
+/* Though these are bit fields, both cannot be used at same time */
+#define PD_SET_MODE_PIPE_A    0x1
+#define PD_SET_MODE_PIPE_B    0x2
+#define PD_SET_MODE_FLAG_TEST 0x4
+
+/* Flags for save and restore */
+#define PD_NO_RESTORE_FREE_STATE 0x1
+
+/* Power Modes */
+#define PD_POWER_MODE_D0  0x0
+#define PD_POWER_MODE_D1  0x1
+#define PD_POWER_MODE_D2  0x2
+#define PD_POWER_MODE_D3  0x3
+
+#define PD_FILE_SIZE       8   /* File name size for port driver excluding
+								* extension. Extension can be max of 3 chars. */
+
+/* Following is the abstraction of port driver. Port driver passes this
+ * driver information to display driver as part of registering itself with
+ * display driver. pd_register(pd_driver_t *) is called from port driver
+ * in its init function which is called whenever port driver is loaded.
+ *
+ * Port driver provides following list of functions to display driver.
+ * Display driver calls these functions to do the required operations.
+ *
+ * type         - supporting display type for the port driver
+ *
+ * flags        - flags for the driver. And cannot be changed at runtime.
+ *                Once these flags are passed to main driver, they will be
+ *                used while setting the mode.
+ *
+ * context      - Driver's contextual information. The driver can save
+ *                all needed state information in this opaque pointer
+ *                such that the driver is entirely reentrant. No writeable
+ *                data may be saved in any non-stack variables by a driver
+ *                therefore all necessary data must be saved in a private
+ *                structure and stored in this pointer. This pointer is
+ *                passed to the driver with each function call.
+ *
+ * validate     - Display driver calls this function to validate the port
+ *                driver by passing a magic cookie. Port driver does some
+ *                magic operation and returns another cookie back to caller.
+ *                If the returned cookie has correct value then the driver
+ *                passes the validation, otherwise driver cannot communicate
+ *                with display driver.
+ *
+ * open         - This function is to detect for any port device (codec).
+ *                pd_callback_t has all the required callback functions
+ *                to do this operation.
+ *
+ * init_device  - This function detects and initializes any display devices
+ *                attached to the codec.
+ *
+ *                For ex: a multi function encoder can support a FlatPanel or
+ *                TVout display. It has to detect and check for any
+ *                Flatpanel or TVout display device attached, then it
+ *                initializes the display devices.
+ *
+ * pd_close     - closes the display device and frees any memory allocated.
+ *
+ * set_mode     - sets/tests a mode. Display (or main) driver calls this
+ *                function to set a mode when the encoder in D3 power state.
+ *                At this time, DVO timings are off.
+ *                If encoder wants to run setmode in any other power state,
+ *                then first thing it needs to do is enter into that power
+ *                state and then do setmode.
+ *
+ *                When this function returns it is expected that:
+ *                   1. Either encoder is in D0 power state or
+ *                      it defers until post_set_mode().
+ *                   2. In case of external encoder (example: TVOut encoder)
+ *                      driving the reference clock, the clock is running.
+ *
+ * post_set_mode- This is called after GMCH DVO timings are on.
+ *                This function can be null if there is nothing to do.
+ *                This is used to do any post processing on the encoder
+ *                after GMCH starts driving the timings. When this function
+ *                returns it is expected that the encoder is in D0 power state.
+ *                Parameters:
+ *                       context - port driver context
+ *                       mode    - current timing
+ *                       flags   - not used (for future use).
+ *
+ * set_attrs    - to set provided list of attributes.
+ *
+ * get_attrs    - to get list of attributes.
+ *
+ * get_timing_list - returns the list of supported modes for the current
+ *                   attributes. For example if the TVFormat is NTSC, this
+ *                   function will return only the modes supported for NTSC.
+ *
+ * set_power    - to set the power state of the display device.
+ *
+ * get_power    - to get the current power state of the display device.
+ *
+ * pd_save      - to save the current state of the registers.
+ *                  - returns the current state in the double pointer 'state'.
+ *
+ * pd_restore   - to restore to the passed state. This functions frees the
+ *                resources allocated that state.
+ *                  - pass the previously saved state in 'state'.
+ *
+ *                If PD_NO_RESTORE_FREE_STATE is set, then it just frees
+ *                the resources without restoring the state.
+ *
+ * pd_get_port_status - to get the status of port/display
+ *
+ *
+ * All functions return value:
+ *         == 0 - if success
+ *         != 0 - if failure
+ */
+
+
+/* Note on version AA.BB.CC.DD
+ * AA - Major version
+ * BB - Minor version
+ * CC - Patch version if any
+ * DD - Bug fixes if any
+ *
+ * 01.00.00.00 is the first initial major version */
+typedef struct _pd_version {
+	unsigned char major;
+	unsigned char minor;
+	unsigned char patch;
+	unsigned char bug_fix;
+} pd_version_t;
+
+
+typedef enum {
+	PD_DISP_STATUS_DETACHED = 0,        /* Display is not connected */
+	PD_DISP_STATUS_ATTACHED,            /* Display is connected     */
+	PD_DISP_STATUS_UNKNOWN              /* Unable to determine      */
+} pd_display_status_t;
+
+/* Port/Display information */
+typedef struct _pd_port_status {
+	pd_display_status_t connected;      /* Display status                */
+	unsigned long display_type;         /* Type of display, if connected */
+} pd_port_status_t;
+
+typedef struct _pd_driver {
+	unsigned long pd_sdk_version;        /* PD SDK version - interface ver */
+	char          name[PD_NAME_SIZE];    /* Descriptive name of port driver */
+	char          num_devices;           /* Number of devices it is driving */
+	pd_version_t  *version;              /* Driver version */
+	unsigned long type;
+	unsigned long flags;
+	unsigned long *dab_list;             /* PD_DAB_LIST_END terminated list */
+	unsigned long i2c_speed;             /* Encoder I2C speed in KHz */
+	unsigned long (*validate)(unsigned long cookie);
+	int (*open)       (pd_callback_t *callback, void **context);
+	int (*init_device)(void *context);
+	int (*pd_close)   (void *context);
+	int (*set_mode)   (void *context, pd_timing_t *mode, unsigned long flags);
+	int (*post_set_mode)(void *context, pd_timing_t *mode, unsigned long flags);
+	int (*set_attrs)  (void *context, unsigned long num, pd_attr_t *list);
+	int (*get_attrs)  (void *context, unsigned long *num, pd_attr_t **list);
+	int (*get_timing_list)(void*context,pd_timing_t*in_list,pd_timing_t**list);
+	int (*set_power)  (void *context, unsigned long state);
+	int (*get_power)  (void *context, unsigned long *state);
+	int (*pd_save)    (void *context, void **state, unsigned long flags);
+	int (*pd_restore) (void *context, void *state, unsigned long flags);
+	int (*pd_get_port_status) (void *context, pd_port_status_t *port_status);
+} pd_driver_t;
+
+/* Mode filter helper function for port drivers */
+/* Structure representing encoder capabilities for mode filtering */
+typedef struct _pd_dvo_info {
+	unsigned long min_dclk;               /* Min clock */
+	unsigned long max_dclk;               /* Max clock */
+	unsigned char upscale;                /* Is upscalable? */
+	unsigned char downscale;              /* Is downscalable? */
+	unsigned short upscale_min_width;     /* Min upscale width */
+	unsigned short upscale_min_height;    /* Min upscale height */
+	unsigned short downscale_max_width;   /* Max downscale width */
+	unsigned short downscale_max_height;  /* Max downscale height */
+} pd_dvo_info_t;
+
+typedef struct _pd_display_info {
+	unsigned char panel_fit;   /* Incoming */
+	unsigned char fixed_res;   /* Outgoing */
+	unsigned short width;      /* Outgoing */
+	unsigned short height;     /* Outgoing */
+	pd_timing_t   *native_dtd; /* Outgoing */
+} pd_display_info_t;
+
+/* All following functions are provided to port drivers as a binary file.
+ *
+ * pd_register: This function is called by port driver to register with
+ *              display driver. It passes its driver structure to display
+ *              driver.
+ *
+ * pd_malloc:   This function is used to allocate any dynamic memory required
+ *              in port driver. Port driver will pass the size of the memory
+ *              it is requesting in bytes.
+ *
+ *              Return value == pointer to the allocated memory on success
+ *                           == NULL if failed to allocated the memory
+ *
+ * pd_memset:   This function is used to set the passed byte in memory
+ *              for size bytes.
+ *
+ * pd_memcpy:   This function is used to copy size bytes from src pointer to
+ *              dest pointer.
+ *
+ * free:        This function is used to free the memory previously allocated
+ *              using malloc callback function.
+ *
+ * usleep:      This function is used to get any delay in port driver.
+ *              'usec' is specified in micro seconds.
+ *
+ *              No return value.
+ * ui_usleep:   This function is used to get any delay in port driver uniterrupted.
+ *              'usec' is specified in micro seconds.
+ *
+ *              No return value.
+ *
+ * printf:      This function is used to do any debug prints for port driver.
+ *              This has exactly the same syntax and usage of standard
+ *              printf in 'C'.
+ *
+ * strcpy:      This function is used to copy src string into dest string.
+ *              This has exactly the same syntax and usage of standard
+ *              strcpy in 'C'.
+ *
+ * filter:      This function is to filter the incoming mode list based on
+ *              dvo_info. Memory is allocated for outgoing list. No changes
+ *              to in_list.
+ */
+int   pd_register(void *handle, pd_driver_t *driver);
+void *pd_malloc(unsigned long size);
+void *pd_memset(void *address, int c, unsigned long size);
+void *pd_memcpy(void *dst, void *src, unsigned long size);
+void  pd_free  (void *address);
+
+void  pd_usleep(unsigned long usec);
+void  pd_ui_usleep(unsigned long usec);
+char *pd_strcpy(char *dest, char const *src);
+int  *pd_printf(const char *format, ...);
+int   pd_check_attr(pd_attr_t *curr, pd_attr_t *in);
+/* Mode filter helper function for port drivers */
+int   pd_filter_timings(void *context, pd_timing_t *inlist, pd_timing_t **olist,
+	pd_dvo_info_t *dvo_info, pd_display_info_t *display_info);
+
+/* pd_get_attr() :  To return the request attr from the list.
+ * In case of 'attr_id' is a list type attribute, then caller
+ * can request either LIST head itself or list entry for that
+ * attribute. */
+#define PD_GET_ATTR_LIST            0x0001
+#define PD_GET_ATTR_LIST_ENTRY      0x0002
+pd_attr_t *pd_get_attr(pd_attr_t *attr_list, unsigned long num_attrs,
+		unsigned long attr_id, unsigned long flag);
+
+/*
+// Typedefinitions
+typedef int        (*pd_register_p)  (void *handle, pd_driver_t *driver);
+typedef void      *(*pd_malloc_p)(unsigned long size);
+typedef void      *(*pd_memset_p)(void *address, int c, unsigned long size);
+typedef void      *(*pd_memcpy_p)(void *dst, void *src, unsigned long size);
+typedef void       (*pd_free_p)  (void *address);
+typedef void       (*pd_usleep_p)(unsigned long usec);
+typedef char      *(*pd_strcpy_p)(char *dest, char const *src);
+typedef int       *(*pd_printf_p)(const char *format, ...);
+typedef int        (*pd_check_attr_p)(pd_attr_t *curr, pd_attr_t *in);
+typedef pd_attr_t *(*pd_get_attr_p)(pd_attr_t *attr_list,
+		unsigned long num_attrs, unsigned long attr_id, unsigned long flag);
+typedef emgd_debug_t *(*pd_get_emgd_debug_p)( void );
+typedef unsigned long *(*pd_get_dropped_debug_messages_p)( void );
+typedef void *(*pd_get_debug_log_mutex_p)( void );
+
+// Mode filter helper function for port drivers
+typedef int        (*pd_filter_timings_p)(void *context, pd_timing_t *inlist,
+		pd_timing_t **olist, pd_dvo_info_t *dvo_info,
+		pd_display_info_t *display_info);
+*/
+#endif /* _PD_H_ */
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/pd_init.h b/drivers/gpu/drm/emgd/emgd/include/pd_init.h
new file mode 100644
index 0000000..eb78285
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/pd_init.h
@@ -0,0 +1,191 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pd_init.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains configurable definitions to statically link port
+ *  drivers with display driver.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PD_INIT_H
+#define _PD_INIT_H
+
+#include <config.h>
+
+/* Enable Analog (CRT) port driver */
+#ifdef CONFIG_PD_ANALOG
+extern int analog_init(void *handle);
+#define ANALOG_INIT(handle)  analog_init(handle)
+#else
+#define ANALOG_INIT(handle)  0
+#endif
+
+/* Enable RGBA port driver */
+#ifdef CONFIG_PD_RGBA
+extern int rgba_init(void *handle);
+#define RGBA_INIT(handle)  rgba_init(handle)
+#else
+#define RGBA_INIT(handle)  0
+#endif
+
+/* Enable Silicon Image 154/164 FP port driver */
+#ifdef CONFIG_PD_SII164
+extern int sii164_init(void *handle);
+#define SII164_INIT(handle)  sii164_init(handle)
+#else
+#define SII164_INIT(handle)  0
+#endif
+
+/* Enable Texas Instruments 410 FP port driver */
+#ifdef CONFIG_PD_TI410
+extern int ti410_init(void *handle);
+#define TI410_INIT(handle)  ti410_init(handle)
+#else
+#define TI410_INIT(handle)  0
+#endif
+
+/* Enable Chrontel 7009 TMDS & TVOut port driver */
+#ifdef CONFIG_PD_CH7009
+extern int ch7009_init(void *handle);
+#define CH7009_INIT(handle)  ch7009_init(handle)
+#else
+#define CH7009_INIT(handle)  0
+#endif
+
+/* Enable National Semiconductor 2501 LVDS port driver */
+#ifdef CONFIG_PD_NS2501
+extern int ns2501_init(void *handle);
+#define NS2501_INIT(handle)  ns2501_init(handle)
+#else
+#define NS2501_INIT(handle)  0
+#endif
+
+/* Enable TL955 port driver */
+#ifdef CONFIG_PD_TL955
+extern int tl955_init(void *handle);
+#define TL955_INIT(handle)  tl955_init(handle)
+#else
+#define TL955_INIT(handle)  0
+#endif
+
+/* Enable Th164 port driver */
+#ifdef CONFIG_PD_TH164
+extern int th164_init(void *handle);
+#define TH164_INIT(handle)  th164_init(handle)
+#else
+#define TH164_INIT(handle)  0
+#endif
+
+/* Enable FS454 port driver */
+#ifdef CONFIG_PD_FS454
+extern int fs454_init(void *handle);
+#define FS454_INIT(handle)  fs454_init(handle)
+#else
+#define FS454_INIT(handle)  0
+#endif
+
+/* Enable NS387/389 port driver */
+#ifdef CONFIG_PD_NS387
+extern int ns387_init(void *handle);
+#define NS387_INIT(handle)  ns387_init(handle)
+#else
+#define NS387_INIT(handle)  0
+#endif
+
+/* Enable connexant port driver */
+#ifdef CONFIG_PD_CX873
+extern int cx873_init(void *handle);
+#define CX873_INIT(handle)  cx873_init(handle)
+#else
+#define CX873_INIT(handle)  0
+#endif
+
+/* Enable Internal LVDS port driver */
+#ifdef CONFIG_PD_LVDS
+extern int lvds_init(void *handle);
+#define LVDS_INIT(handle)  lvds_init(handle)
+#else
+#define LVDS_INIT(handle)  0
+#endif
+
+/* Enable SDVO port driver */
+#ifdef CONFIG_PD_SDVO
+extern int sdvo_init(void *handle);
+#define SDVO_INIT(handle)  sdvo_init(handle)
+#else
+#define SDVO_INIT(handle)  0
+#endif
+
+/* Enable Integrated TV port driver for NAPA*/
+#ifdef CONFIG_PD_TV
+extern int tv_init(void *handle);
+#define TV_INIT(handle)  tv_init(handle)
+#else
+#define TV_INIT(handle)  0
+#endif
+
+/* Enable FS460 port driver */
+#ifdef CONFIG_PD_FS460
+extern int fs460_init(void *handle);
+#define FS460_INIT(handle)  fs460_init(handle)
+#else
+#define FS460_INIT(handle)  0
+#endif
+
+/* Enable FS450 port driver */
+#ifdef CONFIG_PD_FS450
+extern int fs450_init(void *handle);
+#define FS450_INIT(handle)  fs450_init(handle)
+#else
+#define FS450_INIT(handle)  0
+#endif
+
+/* Enable Chrontel 7017 LVDS & TVOut port driver */
+#ifdef CONFIG_PD_CH7017
+extern int ch7017_init(void *handle);
+#define CH7017_INIT(handle)  ch7017_init(handle)
+#else
+#define CH7017_INIT(handle)  0
+#endif
+
+/* Enable internal HDMI port driver */
+#ifdef CONFIG_PD_HDMI
+extern int hdmi_init(void *handle);
+#define HDMI_INIT(handle)  hdmi_init(handle)
+#else
+#define HDMI_INIT(handle)  0
+#endif
+
+/* Enable Dummy Port Driver*/
+#ifdef CONFIG_PD_PD000
+extern int pd000_init(void *handle);
+#define PD000_INIT(handle)  pd000_init(handle)
+#else
+#define PD000_INIT(handle)  0
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/pi.h b/drivers/gpu/drm/emgd/emgd/include/pi.h
new file mode 100644
index 0000000..d14d39e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/pi.h
@@ -0,0 +1,86 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pi.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PI_H_
+#define _PI_H_
+
+#include <context.h>
+#include <mode.h>
+#include <pd.h>
+
+/* power states */
+#define IGD_DEVICE_ON 1
+#define IGD_DEVICE_OFF 0
+
+#define PI_FIRMWARE_EDID          0x1
+#define PI_FIRMWARE_DISPLAYID     0x2
+
+/* get_native_dtd() flags */
+#define PI_ALL_TIMINGS            0x0001
+#define PI_SUPPORTED_TIMINGS      0x0002
+extern unsigned long get_native_dtd(igd_timing_info_t *timing_table,
+	unsigned long flags, pd_timing_t **native_dtd, unsigned long native_flags);
+
+extern igd_timing_info_t crt_timing_table[];
+extern int crt_timing_table_size;
+
+#ifndef CONFIG_MICRO
+extern igd_timing_info_t cea_timing_table[];
+extern int cea_timing_table_size;
+extern type_std_t cea_std_lookup[];
+extern int cea_std_lookup_size;
+#endif
+
+extern int pi_pd_register(pd_driver_t *pd_driver);
+extern int pi_pd_register(pd_driver_t *pd_driver);
+extern int pi_read_regs(void *callback_context, pd_reg_t *list,
+		unsigned long reg_type);
+extern int pi_write_regs(void *callback_context, pd_reg_t *list,
+		unsigned long reg_type);
+extern int pi_program_port_dvo(igd_display_context_t *, unsigned long);
+extern int pi_program_port_analog(igd_display_context_t *, unsigned long);
+extern int pi_program_port_rgba(igd_display_context_t *, unsigned long);
+extern int pi_program_port_lvds(igd_display_context_t *, unsigned long);
+
+/* Function to get attr value from port driver attr list */
+extern int pi_pd_find_attr_and_value(igd_display_port_t *port,
+						  unsigned long attr_id,
+						  unsigned long flag,
+						  pd_attr_t   **caller_pd_attr,
+						  unsigned long * attr_value);
+
+/* Function to get attr value from user provided init attribute list */
+extern int pi_get_port_init_attr(igd_display_port_t *port,
+		unsigned long id,
+		unsigned long *value);
+extern int pi_save_mode_state(igd_display_port_t *port,
+		reg_state_id_t reg_state_id);
+#endif /* _PI_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/appcontext.h b/drivers/gpu/drm/emgd/emgd/include/plb/appcontext.h
new file mode 100644
index 0000000..3a1417c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/appcontext.h
@@ -0,0 +1,67 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: appcontext.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the inter-module header file for the client context module.
+ *  It contains data structures needed for modules to use and manipulate
+ *  the device state including hardware context switching.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _APPCONTEXT_PLB_H
+#define _APPCONTEXT_PLB_H
+
+#include <context.h>
+#include <igd_mode.h>
+
+/*
+ * This data structure contains a copy of all needed state variables and
+ * the logical context used by hardware for context switching. An IGD
+ * client driver can allocate a client context for each client or allocate
+ * just one to be shared by all clients.
+ */
+/*
+ * TODO:
+ * Add or modify the struct and other definitions for Poulsbo
+ */
+typedef struct _appcontext {
+	void *state3d;
+	void *state2d;
+	unsigned char *hw_context_virt;
+	unsigned long hw_context_phys;
+	unsigned long hw_context_offset;
+}appcontext_t, appcontext_plb_t;
+
+int appcontext_set_plb(igd_display_h display,
+	int priority,
+	appcontext_t *context,
+	int extstate_save_enable,
+	int extstate_restore_enable,
+	int force_restore,
+	int restore_inhibit);
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/cmd.h b/drivers/gpu/drm/emgd/emgd/include/plb/cmd.h
new file mode 100644
index 0000000..c4252cb
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/cmd.h
@@ -0,0 +1,43 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: cmd.h
+ * $Revision: 1.13 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Device dependent header file for the command interface for poulsbo
+ *  devices.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PLB_CMD_H
+#define _PLB_CMD_H
+
+#include <io.h>
+#include <sched.h>
+
+#include <instr_common.h>
+#include <utils.h>
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/context.h b/drivers/gpu/drm/emgd/emgd/include/plb/context.h
new file mode 100644
index 0000000..563c232
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/context.h
@@ -0,0 +1,210 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: context.h
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_PLB_CONTEXT_H
+#define _HAL_PLB_CONTEXT_H
+
+#include <sched.h>
+
+#include <pci.h>
+#include <igd_render.h>
+#include <plb/sgx.h>
+#include <servicesint.h>
+/*
+ * FIXME: Promote io_mapped/io_base to DI layer
+ *
+ * Note: This define is for the vBIOS OAL only. Do not use
+ * it anywhere else, use the actual type name.
+ */
+#define PLATFORM_CONTEXT_T platform_context_plb_t
+
+typedef struct psb_use_reg {
+	unsigned long reg_seq;
+	unsigned long base;
+	unsigned long size;
+	unsigned long data_master;
+	unsigned char * virt;
+} psb_use_reg_t;
+
+typedef struct drmBO {
+	unsigned long offset;
+} drmBO_t;
+
+typedef struct psb_closed_dpm {
+	drmBO_t *page_table_bo;
+	drmBO_t *parameter_bo;
+	unsigned int num_pages;
+	unsigned int context_id;
+	unsigned int ta_global_list;
+	unsigned int ta_threshold;
+	unsigned int zls_threshold;
+} psb_closed_dpm_t;
+
+typedef struct _psb_sgx_priv {
+
+	/* HW workaround table */
+	/* ***********************************************/
+    struct pclosed_vopt   vopt;
+
+	/* stuff required to be setup by sgx_init in cmd */
+	/* ***********************************************/
+	struct psb_use_reg    use_code[SGX_MAX_USSE_THRDS];
+	igd_dma_t             drm_bo[DRM_BO_MEM_TYPES];
+
+	igd_dma_t             commBO;
+	igd_dma_t             codeBO;
+	igd_dma_t             sProg;
+	igd_dma_t             geom;
+	igd_dma_t             local;
+
+	unsigned long         usse_reg_dm;
+	unsigned long         num_use_attribute_registers;
+
+	psb_closed_dpm_t      dpms[2];
+	/* What is an igd_command variable doing here?!
+	 * should we move this into an appcontext_plb
+	 * structure and let psb_sgx_priv_t have a ptr
+	 * to the active appcontext_plb pointer? i.e. an
+	 * appcontext created for 3d context?
+	 */
+	igd_command_t         context_select;
+
+	/* stuff required to be setup by sgx_init in gart */
+	/* ***********************************************/
+	unsigned int          cache_ctrl;
+
+	/* state required to be setup by sgx_init in pwr */
+	/* ***********************************************/
+} psb_sgx_priv_t;
+
+/* Values used in platform_context_plb_t->flip_pending
+ * This corresponds to the pipe, which is a bit strange,
+ * but since the flip must wait for a vBlank, it is
+ * based off the PIPE */
+#define PLB_FLIP_PIPE_A_PENDING 1
+#define PLB_FLIP_PIPE_B_PENDING 2
+
+typedef struct _tnc_topaz_priv {
+
+	/* current video task */
+	unsigned long topaz_cur_codec;
+	unsigned long cur_mtx_data_size;
+	int topaz_needs_reset;
+	int topaz_start_idle;
+	unsigned long topaz_idle_start_jiffies;
+	/* used by topaz_lockup */
+	unsigned long topaz_current_sequence;
+	unsigned long topaz_last_sequence;
+	unsigned long topaz_finished_sequence;
+
+	/*
+	 * topaz command queueu
+	 */
+	int topaz_busy;		/* 0 means topaz is free */
+	int topaz_fw_loaded;
+
+	/* topaz ccb data */
+	unsigned long topaz_ccb_buffer_addr;
+	unsigned long topaz_ccb_ctrl_addr;
+	unsigned long topaz_ccb_size;
+	unsigned long topaz_cmd_windex;
+	unsigned short topaz_cmd_seq;
+
+	unsigned long stored_initial_qp;
+	unsigned long topaz_frame_skip;
+	unsigned long topaz_dash_access_ctrl;
+
+	unsigned char *topaz_ccb_wb;
+	unsigned long topaz_wb_offset;
+	unsigned long *topaz_sync_addr;
+	unsigned long topaz_sync_offset;
+	unsigned long topaz_sync_cmd_seq;
+	unsigned long topaz_sync_id;
+	/**
+	 * Virtual address to writeback memory in the aperture space.
+	 */
+	unsigned char *virt_wb;
+	/**
+	 * Offset in gmm space for write back memory.
+	 */
+	unsigned long wb_offset;
+} tnc_topaz_priv_t;
+
+struct msvdx_pvr_info;
+
+typedef struct _platform_context_plb {
+	int irq;
+	unsigned short did;
+	os_pci_dev_t pcidev0;
+	os_pci_dev_t pcidev1;
+	os_pci_dev_t lpc_dev;
+	os_pci_dev_t bridgedev;
+	unsigned char tnc_dev3_rid;             /* TNC Device 3 RID*/
+	os_pci_dev_t stbridgedev;
+	os_pci_dev_t stgpiodev;
+	unsigned long rendec_base0;
+	unsigned long rendec_base1;
+	/*
+	 * Cached value of the SGX's PSB_CR_BIF_DIR_LIST_BASE1, which is
+	 * used to configure MSVDX MMU base 0.
+	 */
+	unsigned long psb_cr_bif_dir_list_base1;
+	int msvdx_needs_reset;
+    spinlock_t msvdx_lock;
+    spinlock_t msvdx_init_plb;
+    unsigned long msvdx_status;
+    int msvdx_busy;
+    struct list_head msvdx_queue;
+	unsigned long msvdx_dash_access_ctrl;
+	struct msvdx_pvr_info *msvdx_pvr;
+	psb_sgx_priv_t sgx_priv_data;
+	tnc_topaz_priv_t tpz_private_data;
+    unsigned long msvdx_fence;
+	int topaz_busy;
+	unsigned long src_pat_data_offset;
+	unsigned long glyph_data_offset;
+	unsigned long sequence;
+	unsigned long mtx_submitted;
+	unsigned long mtx_completed;
+	unsigned long mtx_buf_size;
+	unsigned long host_buf_size;
+	unsigned long mtx_buf_offset;
+	unsigned long host_buf_offset;
+	/* Flip pending. This is used in the mode
+	 * module, but it is intialized in the cmd
+	 * module along with the other mutex-es */
+	unsigned int flip_pending;
+        os_pthread_mutex_t flip_mutex;
+	int force_polling;
+	int irq_enabled;
+} platform_context_plb_t, platform_context_tnc_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/instr.h b/drivers/gpu/drm/emgd/emgd/include/plb/instr.h
new file mode 100644
index 0000000..791808a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/instr.h
@@ -0,0 +1,224 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: instr.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+/*
+ * TODO:
+ * Add or modify the struct and other definitions for Poulsbo
+ */
+#ifndef _INSTR_PLB_H
+#define _INSTR_PLB_H
+
+#include <instr_common.h>
+
+typedef struct _igd_vertex {
+	unsigned long x;
+	unsigned long x_ieee_fp;
+	unsigned long y;
+	unsigned long y_ieee_fp;
+	unsigned long z;
+	unsigned long z_ieee_fp;
+	unsigned long argb;
+} igd_vertex_t, *pigd_vertex_t;
+
+/* Common FP numbers */
+#define IEEE_FP_0 0x00000000
+#define IEEE_FP_1 0x3f800000
+
+/* For EMGD_WRITE32 */
+#include <io.h>
+
+
+/* All instruction sizes must be Double DWORD aligned for the current
+ * platform.  The exception to this is the NOOP, which may be useful
+ * not being Double DWORD aligned. */
+
+/* color depth */
+#define PLB_PF_1BIT_PALETTE      0x0
+#define PLB_PF_2BIT_PALETTE      0x1
+#define PLB_PF_4BIT_PALETTE      0x2
+#define PLB_PF_8BIT_PALETTE      0x3
+#define PLB_PF_8BIT_ALPHA        0x4
+#define PLB_PF_4BIT_ALPHA        0x5
+#define PLB_PF_8BPP              0x6
+#define PLB_PF_16BPP4444         0x7
+#define PLB_PF_16BPP555          0x8
+#define PLB_PF_16BPP1555         0x9
+#define PLB_PF_16BPP565          0xA
+#define PLB_PF_24BPP             0xB
+#define PLB_PF_32BPP             0xC
+#define PLB_PF_32BPP_UYVY        0xD
+#define PLB_PF_32BPP_AYUV        0xE
+
+/*-----------------*/
+/* 2D Instructions */
+/*-----------------*/
+/* Size of 2D Instructions */
+#define PLB_2D_PAT_CONTROL_SIZE    1
+#define PLB_2D_CONTROL_SIZE        3
+#define PLB_2D_SRC_SURF_SIZE       2
+#define PLB_2D_DEST_SURF_SIZE      2
+#define PLB_2D_PAT_SURF_SIZE       2
+#define PLB_2D_MASK_SURF_SIZE      2
+#define PLB_2D_SRC_OFFSET_SIZE     1
+#define PLB_2D_MASK_OFFSET_SIZE    1
+#define PLB_2D_SRC_PAL_SIZE        1
+#define PLB_2D_PAT_PAL_SIZE        1
+/* The 2D_BLT_SIZE is different when doing a Fill */
+#define PLB_2D_BLT_SIZE            3
+#define PLB_2D_BLT_FILL_SIZE       4
+#define PLB_2D_CLIP_SIZE           2
+#define PLB_2D_FENCE_SIZE          1
+#define PLB_2D_FLUSH_SIZE          1
+#define PLB_2D_BLT_SRC_COPY_SIZE   3
+
+/* 2D instruction (BR0) */
+#define _PLB_2D_CLIP        (0x0 << 28)
+#define _PLB_2D_PAT_CONTROL (0x1 << 28)
+#define _PLB_2D_CONTROL     (0x2 << 28)
+#define _PLB_2D_SRC_OFFSET  (0x3 << 28)
+#define _PLB_2D_MASK_OFFSET (0x4 << 28)
+#define _PLB_2D_FENCE       (0x7 << 28)
+#define _PLB_2D_BLT         (0x8 << 28)
+#define _PLB_2D_SRC_SURF    (0x9 << 28)
+#define _PLB_2D_DEST_SURF   (0xa << 28)
+#define _PLB_2D_PAT_SURF    (0xb << 28)
+#define _PLB_2D_SRC_PAL     (0xc << 28)
+#define _PLB_2D_PAT_PAL     (0xd << 28)
+#define _PLB_2D_MASK_SURF   (0xe << 28)
+#define _PLB_2D_FLUSH       (0xf << 28)
+
+/* In 2D Control */
+#define PSB_2D_SRCCK_CTRL     1
+#define PSB_2D_DSTCK_CTRL     2
+#define PSB_2D_ALPHA_CTRL     4
+
+#define PLB_2D_BLT_CTRL_ROT_0      0
+#define PLB_2D_BLT_CTRL_ROT_90     (1<<25)
+#define PLB_2D_BLT_CTRL_ROT_180    (2<<25)
+#define PLB_2D_BLT_CTRL_ROT_270    (3<<25)
+
+/* Top Left or Bottom Right */
+#define PLB_2D_BLT_CTRL_ORDER_TL2BR   0
+#define PLB_2D_BLT_CTRL_ORDER_BR2TL   (1<<23)
+#define PLB_2D_BLT_CTRL_ORDER_TR2BL   (2<<23)
+#define PLB_2D_BLT_CTRL_ORDER_BL2TR   (3<<23)
+
+#define PLB_2D_BLT_CTRL_DEST_CK_DISABLE     0
+#define PLB_2D_BLT_CTRL_DEST_CK_PASS_MATCH  (1<<21)
+#define PLB_2D_BLT_CTRL_DEST_CK_KILL_MATCH  (2<<21)
+
+#define PLB_2D_BLT_CTRL_SRC_CK_DISABLE     0
+#define PLB_2D_BLT_CTRL_SRC_CK_PASS_MATCH  (1<<19)
+#define PLB_2D_BLT_CTRL_SRC_CK_KILL_MATCH  (2<<19)
+
+#define PLB_2D_BLT_CTRL_CLIP_DISABLE  0
+#define PLB_2D_BLT_CTRL_CLIP_ENABLE   (1<<18)
+
+#define PLB_2D_BLT_CTRL_ALPHA_BLEND_DISABLE  0
+#define PLB_2D_BLT_CTRL_ALPHA_BLEND_ENABLE   (1<<17)
+
+#define PLB_2D_BLT_CTRL_FILL      0
+#define PLB_2D_BLT_CTRL_PATTERN   (1<<16)
+
+/*----------------*/
+/* 2D Abstraction */
+/*----------------*/
+#define PLB_2D_CLIP(queue, xmin, xmax, ymin, ymax)               \
+    CMD_2D_WRITE_PLB(queue, _PLB_2D_CLIP | (xmax << 12) | xmin); \
+    CMD_2D_WRITE_PLB(queue, (ymax << 12) | ymin);
+
+#define PLB_2D_PAT_CONTROL(queue, pt, width, height)                       \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_PAT_CONTROL | (pt.x << 15) |           \
+                     (pt.y << 10) | (width << 5) | height);
+
+#define PLB_2D_CONTROL(queue, ctrl_flag, ck_color, ck_mask)                \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_CONTROL | ctrl_flag);                  \
+	CMD_2D_WRITE_PLB(queue, ck_color);                                     \
+	CMD_2D_WRITE_PLB(queue, ck_mask);
+
+#define PLB_2D_FENCE(queue)                                                \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_FENCE);
+
+#define PLB_2D_FLUSH(queue)                                                \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_FLUSH);
+
+#define PLB_2D_DEST_SURF(queue, pf, pitch, offset)                         \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_DEST_SURF | (pf<<15) | pitch);         \
+	CMD_2D_WRITE_PLB(queue, offset);
+
+#define PLB_2D_SRC_SURF(queue, pf, pitch, offset)                          \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_SRC_SURF | (pf<<15) | pitch);          \
+	CMD_2D_WRITE_PLB(queue, offset);
+
+#define PLB_2D_PAT_SURF(queue, pf, pitch, offset)                          \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_PAT_SURF | (pf<<15) | pitch);          \
+	CMD_2D_WRITE_PLB(queue, offset);
+
+#define PLB_2D_MASK_SURF(queue, pitch, offset)                             \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_MASK_SURF | pitch);                    \
+	CMD_2D_WRITE_PLB(queue, offset);
+
+#define PLB_2D_SRC_OFFSET(queue, pt)                                       \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_SRC_OFFSET | (pt.x<<12) | pt.y);
+
+#define PLB_2D_MASK_OFFSET(queue, pt)                                      \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_MASK_OFFSET | (pt.x<<12) | pt.y);
+
+#define PLB_2D_SRC_PAL(queue, offset)                                      \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_SRC_PAL | offset);
+
+#define PLB_2D_PAT_PAL(queue, offset)                                      \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_PAT_PAL | offset);
+
+/* Color fill from Top Left to Bottom Right */
+#define PLB_2D_BLT_FILL_TL2BR(queue, control, rop, fill, rect)             \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_BLT | control | (rop<<8) | rop);       \
+	CMD_2D_WRITE_PLB(queue, fill);                                         \
+	CMD_2D_WRITE_PLB(queue, (rect->x1<<12) | rect->y1);                    \
+	CMD_2D_WRITE_PLB(queue, ((rect->x2-rect->x1)<<12) |                    \
+                     (rect->y2-rect->y1));
+
+/* Color fill from Bottom Right to Top Left */
+#define PLB_2D_BLT_FILL_BR2TL(queue, control, rop, fill, rect)             \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_BLT | control | (rop<<8) | rop);       \
+	CMD_2D_WRITE_PLB(queue, fill);                                         \
+	CMD_2D_WRITE_PLB(queue, ((rect->x2-1)<<12) | (rect->y2-1));            \
+	CMD_2D_WRITE_PLB(queue, ((rect->x2-rect->x1)<<12) |                    \
+                     (rect->y2-rect->y1));
+
+#define PLB_2D_BLT_SRC_COPY(queue, control, rop, pt, w, h)                 \
+	CMD_2D_WRITE_PLB(queue, _PLB_2D_BLT | control | (rop<<8) | rop);       \
+	CMD_2D_WRITE_PLB(queue, (pt.x<<12) | pt.y);                            \
+	CMD_2D_WRITE_PLB(queue, (w<<12) | h);
+
+#define PLB_2D_BLT_CHROMA(queue, chroma_color)                             \
+	CMD_2D_WRITE_PLB(queue, chroma_color);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/mi.h b/drivers/gpu/drm/emgd/emgd/include/plb/mi.h
new file mode 100644
index 0000000..fcbc706
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/mi.h
@@ -0,0 +1,77 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mi.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is a header file for the Intel GFX commands.
+ *  This includes commands specific to Intel hardware and structures specific
+ *  to Intel hardware.  All other commands and structures are available
+ *  through GFX.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _MI_H
+#define _MI_H
+
+#include <igd_mode.h>
+#include <igd_render.h>
+
+/* FIXME: This file has not been checked for PLB. */
+
+#define MI_FLUSH_OPTIONS_MASK 0x0000000f
+#define MI_FLUSH_MAP          0x00000001
+#define MI_FLUSH_RENDER       0x00000004
+/* #define MI_FLUSH_END_SCENE    0x00000008  Not available on PLB? */
+/* #define MI_FLUSH_WRITE_DIRTY  0x00000010  Not available on PLB? */
+
+extern int mi_display_buffer_info_plb(igd_display_h display_h,
+	int priority,
+	unsigned long flags);
+
+extern int mi_wait_scan_priority_arb_on_off_plb(igd_command_t *addr,
+	int priority, int enable);
+
+extern int mi_wait_scan_lines_excl_plb(igd_display_h display_h,
+	int priority,
+	unsigned long start,
+	unsigned long end);
+
+extern int mi_wait_scan_lines_incl_plb(igd_display_h display_h,
+	int priority,
+	unsigned long start,
+	unsigned long end);
+
+extern int mi_wait_vblank_plb(igd_display_h display_h,
+	int priority);
+
+extern int mi_flush_plb(igd_display_h display_h, int priority,
+	unsigned int flush_options, unsigned int flags);
+
+extern int mi_wait_for_scan_plb(igd_display_h display_h, int priority,
+	igd_rect_t *dest_rect);
+
+extern int mi_user_interrupt_plb(igd_display_h display_h, int priority);
+
+#endif /* _MI_H */
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/regs.h b/drivers/gpu/drm/emgd/emgd/include/plb/regs.h
new file mode 100644
index 0000000..95d880f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/regs.h
@@ -0,0 +1,747 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: regs.h
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the register definition file for the PLB platform. This should
+ *  contain device dependent register definitions. Standard register
+ *  definitions (VGA, PCI, etc) should not be put in this file. For those
+ *  see pci.h or vga.h.
+ *-----------------------------------------------------------------------------
+ */
+
+/*
+ * Note: Use _REGS_H_  instead of _PLB_REGS_H_ to insure that no file can
+ * include 2 device dependent register files.
+ */
+#ifndef _REGS_H_
+#define _REGS_H_
+
+#include <pci.h>
+#include <psb_regs.h>
+#include <msvdx.h>
+
+/*-----------------------------------------------------------------------------
+ * PCI Register Definitions
+ *---------------------------------------------------------------------------*/
+#define PLB_PCI_MMADR      PCI_BAR_0
+#define PLB_PCI_IOBAR      PCI_BAR_1
+#define PLB_PCI_GMADR      PCI_BAR_2
+#define PLB_PCI_GTTADR     PCI_BAR_3
+
+#define PLB_PCI_GC         0x52
+#define PLB_PCI_BSM        0x5C
+
+#define PLB_MMIO_SIZE  (512*1024)
+#define PLB_GTT_SIZE   (128*1024)
+
+
+/*-----------------------------------------------------------------------------
+ * Display Plane A Register Definitions (70180h - 70188h)
+ *---------------------------------------------------------------------------*/
+#define DSPAAFLIP    0x7017C
+#define DSPACNTR     0x70180
+#define DSPAADDR     0x70184
+#define DSPASTRIDE   0x70188
+#define DSPASIZE     0x70190
+#define DSPAKEYVAL   0x70194
+#define DSPAKEYMASK  0x70198
+
+/*
+ * FIXME: Review and clean up this file. Everything below this point needs
+ * to be reviewed for accuracy or removed.
+ */
+
+
+
+/*-----------------------------------------------------------------------------
+ * General VGA Register Definitions
+ *---------------------------------------------------------------------------*/
+#define FEATURE_CONT_REG      0x3DA  /* Feature Control Register */
+#define FEATURE_CONT_REG_MONO 0x3BA  /* Feature Control Register (Mono) */
+#define FEATURE_CONT_REG_READ 0x3CA  /* Feature Control Register (Read) */
+#define MSR_PORT              0x3C2  /* Miscellaneous Output Port */
+#define MSR_PORT_LSB          0xC2   /* Miscellaneous Output Port LSB */
+#define MSR_READ_PORT         0x3CC  /* Miscellaneous Output Reg (Read) */
+#define STATUS_REG_00         0x3C2  /* Input Status Register 0 */
+#define STATUS_REG_01         0x3DA  /* Input Status Register 1 */
+#define STATUS_REG_01_MONO    0x3BA  /* Input Status Register 1 (Mono) */
+
+/* DAC Register Definitions. */
+#define DAC_PEL_MASK          0x3C6  /* Color Palette Pixel Mask Register */
+#define DAC_READ_INDEX        0x3C7  /* Color Palette Read-Mode Index Reg */
+#define DAC_STATE             0x3C7  /* Color Palette State Register */
+#define DAC_WRITE_INDEX       0x3C8  /* Color Palette Index Register */
+#define DAC_DATA_REG          0x3C9  /* Color Palette Data Register */
+
+
+/*-----------------------------------------------------------------------------
+ * Attribute Controller Register Definitions
+ *---------------------------------------------------------------------------*/
+#define AR_PORT_LSB  0xC0  /*Attribute Controller Index Port LSB */
+
+#define AR00  0x00  /* Color Data Register */
+#define AR01  0x01	/* Color Data Register */
+#define AR02  0x02	/* Color Data Register */
+#define AR03  0x03	/* Color Data Register */
+#define AR04  0x04	/* Color Data Register */
+#define AR05  0x05	/* Color Data Register */
+#define AR06  0x06	/* Color Data Register */
+#define AR07  0x07	/* Color Data Register */
+#define AR08  0x08	/* Color Data Register */
+#define AR09  0x09	/* Color Data Register */
+#define AR0A  0x0A	/* Color Data Register */
+#define AR0B  0x0B	/* Color Data Register */
+#define AR0C  0x0C	/* Color Data Register */
+#define AR0D  0x0D	/* Color Data Register */
+#define AR0E  0x0E	/* Color Data Register */
+#define AR0F  0x0F	/* Color Data Register */
+#define AR10  0x10	/* Mode Control Register */
+#define AR11  0x11	/* Overscan Color Register */
+#define AR12  0x12	/* Color Plane Enable Register */
+#define AR13  0x13	/* Horizontal Pixel Panning Register */
+#define AR14  0x14	/* Pixel Pad Register */
+
+
+/*-----------------------------------------------------------------------------
+ * CRT Controller Register Definitions
+ *---------------------------------------------------------------------------*/
+#define CR_PORT_LSB     0xD4  /* CRT Controller Index Port LSB */
+#define CRT_3D4	        0x3D4 /* Color CRTC Index Port */
+#define CRT_3B4	        0x3B4 /* Monochrome CRTC Index Port */
+
+#define CR00            0x00  /* Horizontal Total Register */
+#define CR01            0x01  /* Horizontal Display Enable End Reg */
+#define CR02            0x02  /* Horizontal Blank Start Register */
+#define CR03            0x03  /* Horizontal Blank End Register */
+#define CR04            0x04  /* Horizontal Sync Start Register */
+#define CR05            0x05  /* Horizontal Sync End Register */
+#define CR06            0x06  /* Vertical Total Register */
+#define CR07            0x07  /* Overflow Register */
+#define CR08            0x08  /* Preset Row Scan Register */
+#define CR09            0x09  /* Maximum Scan Line Register */
+#define DOUBLE_SCANLINE	BIT7  /* Double scan ( 1 = Enable ) */
+#define LCOMP_BIT9      BIT6  /* Bit 9 of line compare register */
+#define VBLANK_BIT9     BIT5  /* Bit 9 of vertical blank start */
+#define CR0A            0x0A  /* Cursor Start Scan Line Register */
+#define CR0B            0x0B  /* Cursor End Scan Line Register */
+#define CR0C            0x0C  /* Start Address High Register */
+#define CR0D            0x0D  /* Start Address Low Register */
+#define CR0E            0x0E  /* Cursor Location High Register */
+#define CR0F            0x0F  /* Cursor Location Low Register */
+#define CR10            0x10  /* Vertical Sync Start Register */
+#define CR11            0x11  /* Vertical Sync End Register */
+#define CR12            0x12  /* Vertical Display Enable End Reg */
+#define CR13            0x13  /* Offset Register */
+#define CR14            0x14  /* Underline Row Register */
+#define CR15            0x15  /* Vertical Blank Start Register */
+#define CR16            0x16  /* Vertical Blank End Register */
+#define CR17            0x17  /* CRT Mode Control Register */
+#define CR18            0x18  /* Line Compare Register */
+#define CR22            0x22  /* Memory Data Latches Register */
+#define CR24            0x24  /* Attribute Controller Toggle Reg */
+
+
+/*-----------------------------------------------------------------------------
+ * Graphics Controller Register Definitions
+ *---------------------------------------------------------------------------*/
+#define GR_PORT_LSB    0xCE   /* Graphics Controller Index Port LSB */
+
+#define GR00           0x00   /* Set/Reset Register */
+#define GR01           0x01   /* Enable Set/Reset Register */
+#define GR02           0x02   /* Color Compare Register */
+#define GR03           0x03   /* Data Rotate Register */
+#define GR04           0x04   /* Read Map Select Register */
+#define GR05           0x05   /* Graphics Mode Register */
+#define GR06           0x06   /* Micsellaneous Register */
+#define RANGE_MAP_MASK BIT3 + BIT2  /* Address range to map mask */
+#define A0_BF_RANGE    000h   /* Map to A0000h-BFFFFh range */
+#define GRAF_MODE      BIT0   /* 1 = Grahics mode, 0 = Text mode */
+#define GR07           0x07   /* Color Don't Care Register */
+#define GR08           0x08   /* Bit Mask Register */
+#define GR10           0x10   /* Address Mapping */
+#define PAGING_TARGET  BIT2 + BIT1 /* 00 = Local/Stolen, 01 = Memory mapped regs */
+#define PAGE_MODE      BIT0   /* Page Map allow access to all FB mem */
+#define GR11           0x11   /* Page Selector */
+#define	GR18           0x18   /* Software Flag */
+
+
+/*-----------------------------------------------------------------------------
+ * Sequencer Register Definitions
+ *---------------------------------------------------------------------------*/
+#define SR_PORT_DATA      0x3C5 /* Sequencer Data Port */
+#define SR_PORT_LSB       0xC4  /* Sequencer Index Port LSB */
+
+#define SR00              0x00  /* Reset Register */
+#define SR01              0x01  /* Clocking Mode Register */
+#define DOT_CLOCK_DIVIDE  BIT3	/* Divide pixel clock by 2 */
+#define SR02              0x02  /* Plane/Map Mask Register */
+#define SR03              0x03  /* Character Font Register */
+#define SR04              0x04  /* Memory Mode Register */
+#define SR07              0x07  /* Horizontal Character Counter Reset */
+
+
+
+
+
+#define PLB_OFFSET_VGA_MSAC         0x62
+#define INTEL_OFFSET_VGA_CORECLK    0xF0
+
+#define INTEL_OFFSET_BRIDGE_CAPREG  0xE0
+
+#define PCI_CAPREG_4      0x44 /* Capability Identification Reg[39:31] */
+#define PCI_MOBILE_BIT    BIT0
+
+#define PCI_GMS_MASK      BIT6 + BIT5 + BIT4 /* GFX Mode Select Bits Mask */
+#define PCI_LOCAL		  BIT4 /* Local memory enabled */
+#define PCI_DVMT_512K     BIT5 /* 512KB DVMT */
+#define PCI_DVMT_1M       BIT5 + BIT4 /* 1MB DVMT */
+#define PCI_DVMT_8M       BIT6 /* 8MB DVMT */
+
+#define PCI_DRB_REG       0x60 /* DRAM row boundary Register */
+#define PCI_DRC_REG       0x7C /* DRAM Controller Mode Register */
+#define PCI_DT_MASK       BIT0 + BIT1  /* Select SDRAM types.
+                                        *  = 00:  Single data rate SDRAM
+                                        *  = 01:  Dual data rate SDRAM
+                                        *  = Other:  Reserved
+                                        */
+#define DT_SDR_SDRAM      00   /* Single data rate SDRAM */
+#define DT_DDR_SDRAM      01   /* Dual data rate SDRAM */
+
+#define PCI_ESMRAMC_REG   0x91 /* Extended System Management RAM Reg */
+#define PCI_TSEG_SZ		  BIT1 /* TSEG size bit */
+#define PCI_TSEG_512K	  0    /* 512K TSEG */
+#define PCI_TSEG_1M       BIT1 /* 1MB TSEG */
+
+#define PCI_GCLKIO_REG    0xC0 /* GMCH Clock and IO Control Register */
+#define PCI_AGP_Bit       BIT9 /* AGP/DVO Mux Select:
+								*  = 0, DVO/ZV
+								*  = 1, AGP
+								*/
+#define PCI_GMCHCFG_REG   0xC6 /* GMCH Configuration Register */
+#define PCI_SMFREQ_MASK   BIT10 + BIT11
+						   /* System Mem Frequency Select
+                            * = 00:  Intel Reserved
+                            * = 01:  System Memory Frequency is 166Mhz (DDR333) - Intel Reserved
+                            * = 10:  System Memory Frequency is 133Mhz (SDR133, DDR266)
+                            * = 11:  System Memory Frequency is 100Mhz (DDR200)
+                            */
+#define SYS_MEM_FREQ_166  1  /* System Memory Frequency is 166Mhz */
+#define SYS_MEM_FREQ_133  2  /* System Memory Frequency is 133Mhz */
+#define SYS_MEM_FREQ_100  3  /* System Memory Frequency is 100Mhz */
+#define PCI_SMFREQ_POS    10 /* System Memory Frequency position  */
+
+#define PCI_CONFIG_LMINT  0xE0
+#define PREALLOCATED_SIZE (8 * 1024 * 1024)
+
+
+/*-----------------------------------------------------------------------------
+ * General VGA Register Definitions
+ *---------------------------------------------------------------------------*/
+#define FEATURE_CONT_REG      0x3DA  /* Feature Control Register */
+#define FEATURE_CONT_REG_MONO 0x3BA  /* Feature Control Register (Mono) */
+#define FEATURE_CONT_REG_READ 0x3CA  /* Feature Control Register (Read) */
+#define MSR_PORT              0x3C2  /* Miscellaneous Output Port */
+#define MSR_PORT_LSB          0xC2   /* Miscellaneous Output Port LSB */
+#define MSR_READ_PORT         0x3CC  /* Miscellaneous Output Reg (Read) */
+#define STATUS_REG_00         0x3C2  /* Input Status Register 0 */
+#define STATUS_REG_01         0x3DA  /* Input Status Register 1 */
+#define STATUS_REG_01_MONO    0x3BA  /* Input Status Register 1 (Mono) */
+
+/* DAC Register Definitions. */
+#define DAC_PEL_MASK          0x3C6  /* Color Palette Pixel Mask Register */
+#define DAC_READ_INDEX        0x3C7  /* Color Palette Read-Mode Index Reg */
+#define DAC_STATE             0x3C7  /* Color Palette State Register */
+#define DAC_WRITE_INDEX       0x3C8  /* Color Palette Index Register */
+#define DAC_DATA_REG          0x3C9  /* Color Palette Data Register */
+
+
+/*-----------------------------------------------------------------------------
+ * Memory mapped I/O Registers Definitions
+ *---------------------------------------------------------------------------*/
+
+/*-----------------------------------------------------------------------------
+ * Instruction and Interrupt Control Registers (01000h - 02FFFh)
+ *---------------------------------------------------------------------------*/
+#define PGTBL_CTL         0x02020  /* Page Table Control Register */
+#define HWS_PGA           0x02080  /* Hardware Status Page Address register */
+#define HWSTAM            0x02098  /* Hardware Status Mask */
+#define SCPD0             0x0209C  /* Scratch Pad 0 (Debug) */
+#define IER               0x020A0  /* Interrupt Enable */
+#define IIR               0x020A4  /* Interrupt Identity */
+#define IMR               0x020A8  /* Interrupt Mask */
+#define ISR               0x020AC  /* Interrupt Status */
+#define EIR               0x020B0  /* Error Identity */
+#define EMR               0x020B4  /* Error Mask */
+#define ESR               0x020B8  /* Error Status */
+#define FW_BLC1           0x020D8  /* FIFO Watermark Burst Length Control */
+#define FW_BLC2           0x020DC  /* FIFO Watermark Burst Length Control */
+#define FW_BLC_SELF       0x020E0  /* Display FIFO Watermark */
+#define MI_ARB_STATE      0x020E4  /* Memory Interface Arbitration State */
+#define FW_BLC3           0x020EC  /* FIFO Watermark Burst Length Control */
+#define G_DEBUG           0x020FC  /* G-UNIT Debug enable register */
+
+
+/*-----------------------------------------------------------------------------
+ * FENCE and Per Process GTT Control Registers (02000h - 031FFh)
+ * --------------------------------------------------------------------------*/
+#define FENCE0            0x02000 /* Fence table registers */
+#define FENCE1            0x02004
+#define FENCE2            0x02008
+#define FENCE3            0x0200C
+#define FENCE4            0x02010
+#define FENCE5            0x02014
+#define FENCE6            0x02018
+#define FENCE7            0x0201C
+#define FENCE8            0x03000
+#define FENCE9            0x03004
+#define FENCE10           0x03008
+#define FENCE11           0x0300C
+#define FENCE12           0x03010
+#define FENCE13           0x03014
+#define FENCE14           0x03018
+#define FENCE15           0x0301C
+
+/*-----------------------------------------------------------------------------
+ * MISC I/0 Contol Register ( 05000h - 05FFFh )
+ *---------------------------------------------------------------------------*/
+#define IO_OFF          0x05000         /* Register group offset */
+
+#define IO00            0x05000         /* Hsync / Vsync control register */
+#define GPIO0           0x05010         /* GPIO register 0 (DDC1) */
+#define	DDC1_SCL_PIN    GPIO0_SCL_PIN   /* DDC1 SCL GPIO pin # */
+#define	DDC1_SDA_PIN    GPIO0_SDA_PIN   /* DDC1 SDA CPIO pin # */
+#define GPIO1           0x05014         /* GPIO register 1 (I2C) */
+#define	I2C_SCL_PIN     GPIO1_SCL_PIN   /* I2C SCL GPIO pin # */
+#define	I2C_SDA_PIN     GPIO1_SDA_PIN   /* I2C SDA CPIO pin # */
+#define GPIO2           0x05018         /* GPIO register 2 (DDC2) */
+#define	DDC2_SCL_PIN    GPIO2_SCL_PIN   /* DDC2 SCL GPIO pin # */
+#define	DDC2_SDA_PIN    GPIO2_SDA_PIN   /* DDC2 SDA CPIO pin # */
+#define GPIO3           0x0501C         /* GPIO register 3 (AGP mux DVI DDC) */
+#define GPIO4           0x05020         /* GPIO register 4 (AGP mux I2C) */
+#define GPIO5           0x05024         /* GPIO register 5 (AGP mux DDC2/I2C) */
+
+#define GPIOPIN0        GPIO0
+#define GPIOPIN1        GPIO0+1
+#define GPIOPIN2        GPIO1
+#define GPIOPIN3        GPIO1+1
+#define GPIOPIN4        GPIO2
+#define GPIOPIN5        GPIO2+1
+#define GPIOPIN6        GPIO3
+#define GPIOPIN7        GPIO3+1
+#define GPIOPIN8        GPIO4
+#define GPIOPIN9        GPIO4+1
+#define GPIOPIN10       GPIO5
+#define GPIOPIN11       GPIO5+1
+#define GPIOPINMAX      12
+
+#define GMBUS0          0x5100 /* GMBUS clock/device select register */
+#define GMBUS1          0x5104 /* GMBUS command/status register */
+#define GMBUS2          0x5108 /* GMBUS status register */
+#define GMBUS3          0x510C /* GMBUS data buffer register */
+#define GMBUS4          0x5110 /* GMBUS REQUEST_INUSE register */
+#define GMBUS5          0x5120 /* GMBUS 2-Byte Index register */
+#define GMBUS6          0x5124 /* GMBUS Clock divider */
+
+/*  GMBUS1 Bits */
+#define SW_CLR_INT      BIT31
+#define SW_RDY          BIT30
+#define ENT             BIT29
+#define STO             BIT27
+#define ENIDX           BIT26
+#define STA             BIT25
+
+/*  GMBUS2 Bits */
+#define INUSE           BIT15
+#define HW_WAIT         BIT14
+#define HW_TMOUT        BIT13
+#define HW_INT          BIT12
+#define HW_RDY          BIT11
+#define HW_BUS_ERR      BIT10
+#define GA              BIT9
+
+/*-----------------------------------------------------------------------------
+ * Clock Control and PM Register ( 06000h - 06FFFh )
+ *---------------------------------------------------------------------------*/
+#define VGA0_DIVISOR    0x06000  /* VGA 0  Divisor */
+#define VGA1_DIVISOR    0x06004  /* VGA 1 Divisor */
+#define VGA_PD          0x06010  /* VGA Post Divisor Select */
+#define DPLLACNTR       0x06014  /* Display PLL A Control */
+#define DPLLBCNTR       0x06018  /* Display PLL B Control */
+#define FPA0            0x06040  /* DPLL A Divisor 0 */
+#define FPA1            0x06044  /* DPLL A Divisor 1 */
+#define FPB0            0x06048  /* DPLL B Divisor 0 */
+#define FPB1            0x0604C  /* DPLL B Divisor 1 */
+
+#define DREFCLK         0x0
+#define TVCLKINBC       0x4000
+#define CLOCK_2X        0x40000000
+
+#define P2D_CG_DIS      0x06200  /* Clock Gating Disable */
+#define P3D_CG_DIS      0x06204  /* Clock Gating Disable */
+
+
+/*-----------------------------------------------------------------------------
+ * Display Palette Register Definitions (0A000h - 0AFFFh)
+ *---------------------------------------------------------------------------*/
+#define DPALETTE_A      0x0A000  /* Display Pipe A Palette */
+#define DPALETTE_B      0x0A800  /* Display Pipe B Palette */
+
+
+/*-----------------------------------------------------------------------------
+ * Display Pipeline / Port Register Definitions (60000h - 6FFFFh)
+ *---------------------------------------------------------------------------*/
+#define DP_OFFSET       0x60000  /* register group offset */
+#define PIPEA_TIMINGS   0x60000
+#define HTOTAL_A        0x60000  /* Pipe A Horizontal Total Register */
+#define ACTIVE_DISPLAY  0x7FF    /* bit [ 10:0 ] */
+#define HBLANK_A        0x60004  /* Pipe A Horizontal Blank Register */
+#define HSYNC_A         0x60008  /* Pipe A Horizontal Sync Register */
+#define VTOTAL_A        0x6000C  /* Pipe A Vertical Total Register */
+#define VBLANK_A        0x60010  /* Pipe A Vertical Blank Register */
+#define VSYNC_A         0x60014  /* Pipe A Vertical Sync Register */
+#define PIPEASRC        0x6001C  /* Pipe A Source Image Size Register */
+#define BCLRPAT_A       0x60020  /* Pipe A Border Color Pattern Register */
+#define CRCCTRLREDA     0x60050  /* Pipe A CRC Red Control Register */
+#define CRCCTRLGREENA   0x60054  /* Pipe A CRC Green Control Register */
+#define CRCCTRLBLUEA    0x60058  /* Pipe A CRC Blue Control Register */
+#define CRCCTRLRESA     0x6005C  /* Pipe A CRC Alpha Control Register */
+
+#define PIPEB_TIMINGS   0x61000
+#define HTOTAL_B        0x61000  /* Pipe B Horizontal Total Register */
+#define HBLANK_B        0x61004  /* Pipe B Horizontal Blank Register */
+#define HSYNC_B         0x61008  /* Pipe B Horizontal Sync Register */
+#define VTOTAL_B        0x6100C  /* Pipe B Vertical Total Register */
+#define VBLANK_B        0x61010  /* Pipe B Vertical Blank Register */
+#define VSYNC_B         0x61014  /* Pipe B Vertical Sync Register */
+#define PIPEBSRC        0x6101C  /* Pipe B Source Image Size Register */
+#define BCLRPAT_B       0x61020  /* Pipe B Border Color Pattern Register */
+#define CRCCTRLREDB     0x61050  /* Pipe B CRC Red Control Register */
+#define CRCCTRLGREENB   0x61054  /* Pipe B CRC Green Control Register */
+#define CRCCTRLBLUEB    0x61058  /* Pipe B CRC Blue Control Register */
+#define CRCCTRLRESB     0x6105C  /* Pipe B CRC Alpha Control Register */
+
+#define PORT_HPLUG_EN	0x61110  /* Port Hot Plug Enable */
+#define PORT_HPLUG_STAT 0x61114  /* Port Hot Plug Status */
+#define SDVOBCNTR       0x61140  /* Digital Display Port B Control */
+#define SDVOCCNTR       0x61160  /* Digital Display Port B Control */
+#define LVDSCNTR        0x61180  /* Digital Display Port Control */
+
+/* Panel Power Sequencing */
+#define LVDS_PNL_PWR_STS  0x61200  /* LVDS Panel Power Status Register */
+#define LVDS_PNL_PWR_CTL  0x61204  /* LVDS Panel Power Control Register */
+#define PP_ON_DELAYS      0x61208  /* Panel Power On Sequencing Delays */
+#define PP_OFF_DELAYS     0x6120C  /* Panel Power Off Sequencing Delays */
+#define PP_DIVISOR        0x61210  /* Panel Power Cycle Delay and Reference */
+
+/* Panel Fitting */
+#define PFIT_CONTROL      0x61230  /* Panel Fitting Control */
+#define PFIT_PGM_RATIOS   0x61234  /* Programmed Panel Fitting Ratios */
+#define PFIT_AUTO_RATIOS  0x61238  /* Programmed Panel Fitting Ratios */
+#define PFIT_INIT_PHASE   0x6123C  /* Programmed Panel Fitting Ratios */
+
+/* Backlight control */
+#define BLC_PWM_CTL       0x61254  /* Backlight PWM Control */
+#define BLM_HIST_CTL      0x61260  /* Image BLM Histogram Control */
+
+#define PORT_EN           BIT31
+#define PORT_PIPE_SEL     BIT30
+#define PORT_PIPE_SEL_POS 30
+#define PORT_PIPE_A       0      /* 0 = Pipe A */
+#define PORT_PIPE_B       BIT30  /* 1 = Pipe B */
+#define STALL_MASK        BIT29 + BIT28
+#define STALL_ENABLE      BIT28
+#define SYNC_MASK         BIT15 + BIT11 + BIT10 + BIT4 + BIT3
+#define SYNC_POLARITY     BIT15  /* 1 = Use VGA register */
+#define VSYNC_OUTPUT      BIT11
+#define HSYNC_OUTPUT      BIT10
+#define VSYNC_POLARITY    BIT4
+#define HSYNC_POLARITY    BIT3
+#define FP_DATA_ORDER     BIT14
+#define SUBDATA_ORDER     BIT6
+#define BORDER_EN         BIT7
+#define DISPLAY_EN        BIT2
+#define INTERLACED_BIT    0x00100000
+#define RGBA_BITS         0x00030000
+
+
+
+/*-----------------------------------------------------------------------------
+ * Display Pipeline A Register ( 70000h - 70024h )
+ *---------------------------------------------------------------------------*/
+#define PIPEA_SCANLINE_COUNT   0x70000  /* Pipe A Scan Line Count (RO) */
+#define PIPEA_SCANLINE_COMPARE 0x70004  /* Pipe A SLC Range Compare (RO) */
+#define PIPEA_CONF             0x70008  /* Pipe A Configuration */
+#define PIPE_STATUS_OFFSET     0x1C
+#define PIPEA_STAT             0x70024  /* Pipe A Display Status */
+#define PIPEA_DISP_ARB_CTRL    0x70030  /* Display Arbitration Control */
+#define FW_BLC_AB              0x70034
+#define FW_BLC_C               0x70038
+#define PIPEA_FRAME_HIGH       0x70040  /* Pipe A Frame Count High */
+#define PIPEA_FRAME_PIXEL      0x70044  /* Pipe A Frame Cnt Low & pixel count */
+
+#define PIPE_PIXEL_MASK        0x00ffffff
+#define PIPE_FRAME_HIGH_MASK   0x0000ffff
+#define PIPE_FRAME_LOW_MASK    0xff000000
+#define PIPE_FRAME_LOW_SHIFT   24
+
+
+/* following bit flag defs can be re-used for Pipe-B */
+#define PIPE_ENABLE       BIT31
+#define PIPE_LOCK         BIT25
+#define GAMMA_MODE        BIT24
+#define HOT_PLUG_EN       BIT26
+/* Setting this bit to a 1 in the PIPE{A|B}_STAT register, enables the
+ * consideration of vertical sync interrupts.
+ */
+#define VSYNC_STS_EN      BIT25
+/* Setting this bit to a 1 in the PIPE{A|B}_STAT register, enables the
+ * consideration of vertical blank interrupts.
+ */
+#define VBLANK_STS_EN     BIT17
+#define HOT_PLUG_STS      BIT10
+#define VSYNC_STS         BIT9
+#define VBLANK_STS        BIT1
+/* The following are "sticky" status bits in the PIPE{A|B}_STAT register.  They
+ * are cleared by writing a 1 to them.  Theres is code that reads a
+ * PIPE{A|B}_STAT register into a variable, then modifies the variable, and
+ * writes it back to the register.  These bits should be treated specially, so
+ * as not to inadvertantly clear the status bits, so that other code looking
+ * for those bits to be set won't miss it.
+ */
+#define PIPESTAT_STS_BITS ((unsigned long) BIT31 | BIT13 | BIT12 | BIT11 | \
+	VSYNC_STS | BIT8 | BIT5 | BIT4 | VBLANK_STS | BIT0)
+
+/*-----------------------------------------------------------------------------
+ * Hardware Cursor Register Definitions (70080h - 7009Ch)
+ *---------------------------------------------------------------------------*/
+#define CUR_A_CNTR        0x70080  /*Cursor A Control */
+#define CUR_B_CNTR        0x700C0
+#define CUR_BASE_OFFSET   0x4
+#define CUR_POS_OFFSET    0x8
+#define CUR_PAL0_OFFSET   0x10
+#define CUR_PAL1_OFFSET   0x14
+#define CUR_PAL2_OFFSET   0x18
+#define CUR_PAL3_OFFSET   0x1C
+
+/* Define these for ease of reference */
+#define CURSOR_A_BASE     CUR_A_CNTR + CUR_BASE_OFFSET
+#define CURSOR_A_POS      CUR_A_CNTR + CUR_POS_OFFSET
+#define CURSOR_A_PAL0     CUR_A_CNTR + CUR_PAL0_OFFSET
+#define CURSOR_A_PAL1     CUR_A_CNTR + CUR_PAL1_OFFSET
+#define CURSOR_A_PAL2     CUR_A_CNTR + CUR_PAL2_OFFSET
+#define CURSOR_A_PAL3     CUR_A_CNTR + CUR_PAL3_OFFSET
+#define CURSOR_B_BASE     CUR_B_CNTR + CUR_BASE_OFFSET
+#define CURSOR_B_POS      CUR_B_CNTR + CUR_POS_OFFSET
+#define CURSOR_B_PAL0     CUR_B_CNTR + CUR_PAL0_OFFSET
+#define CURSOR_B_PAL1     CUR_B_CNTR + CUR_PAL1_OFFSET
+#define CURSOR_B_PAL2     CUR_B_CNTR + CUR_PAL2_OFFSET
+#define CURSOR_B_PAL3     CUR_B_CNTR + CUR_PAL3_OFFSET
+
+
+/*-----------------------------------------------------------------------------
+ * VBIOS Software flags  00h - 0Fh
+ *---------------------------------------------------------------------------*/
+#define DSP_CHICKENBITS 0x70400  /* Chicken Bit */
+#define SWFABASE        0x70410  /* Software flags A Base Addr */
+#define SWF00           0x70410
+#define SWF01           0x70414
+#define SWF02           0x70418
+#define SWF03           0x7041C
+#define SWF04           0x70420
+#define SWF05           0x70424
+#define SWF06           0x70428
+#define SWF07           0x7042C
+#define SWF08           0x70430
+#define SWF09           0x70434
+#define SWF0A           0x70438
+#define SWF0B           0x7043C
+#define SWF0C           0x70440
+#define SWF0D           0x70444
+#define SWF0E           0x70448
+#define SWF0F           0x7044C
+
+
+/*-----------------------------------------------------------------------------
+ * Display Pipeline B Register ( 71000h - 71024h )
+ *---------------------------------------------------------------------------*/
+#define PIPEB_SCANLINE_COUNT   0x71000 /* Pipe B Disp Scan Line Count Reg */
+#define PIPEB_SCANLINE_COMPARE 0x71004 /* Pipe B Disp Scan Line Cnt Range Cmp */
+#define PIPEB_CONF             0x71008 /* Pipe B Pixel Pipeline Config Reg */
+#define PIPEB_STAT             0x71024 /* Display Status Select Register */
+#define PIPEB_FRAME_HIGH       0x71040 /* Pipe B Frame Count High */
+#define PIPEB_FRAME_PIXEL      0x71044 /* Pipe B Frame Cnt Low and pixel cnt */
+
+
+#define VBLANK_EVN_STS_EN   BIT20
+#define VBLANK_ODD_STS_EN   BIT21
+#define VBLANK_EVN_STS      BIT4
+#define VBLANK_ODD_STS      BIT5
+
+
+/*-----------------------------------------------------------------------------
+ * Display Plane B Register Definitions (71180h - 71188h)
+ *---------------------------------------------------------------------------*/
+#define DSPBFLIP        0x7117C  /* Display B Async flip */
+#define DSPBCNTR        0x71180  /* Display Plane B */
+#define DSPBADDR        0x71184  /* Display B Start Address */
+#define DSPBSTRIDE      0x71188  /* Display B Stride */
+#define DSPBPOS         0x7118C  /* Display B Sprite Offset */
+#define DSPBSIZE        0x71190  /* Display B Sprite Size */
+#define DSPBKEYVAL      0x71194  /* Sprite color key value */
+#define DSPBKEYMASK     0x71198  /* Sprite color key mask */
+
+
+
+/*-----------------------------------------------------------------------------
+ * Source Format Definition for DSPxCNTR
+ *---------------------------------------------------------------------------*/
+#define DSPxCNTR_ARGB_8888			0x1C000000
+#define DSPxCNTR_RGB_8888			0x18000000
+#define DSPxCNTR_RGB_565			0x14000000
+#define DSPxCNTR_RGB_555			0x10000000
+#define DSPxCNTR_RGB_8				0x08000000
+#define DSPxCNTR_SRC_FMT_MASK			0x3C000000 /*mask for above*/
+
+
+/*-----------------------------------------------------------------------------
+ * VBIOS Software flags  10h - 1Fh
+ *---------------------------------------------------------------------------*/
+#define SWFBBASE        0x71410  /* Software flags B Base Addr */
+#define SWF10           0x71410
+#define SWF11           0x71414
+#define SWF12           0x71418
+#define SWF13           0x7141C
+#define SWF14           0x71420
+#define SWF15           0x71424
+#define SWF16           0x71428
+#define SWF17           0x7142C
+#define SWF18           0x71430
+#define SWF19           0x71434
+#define SWF1A           0x71438
+#define SWF1B           0x7143C
+#define SWF1C           0x71440
+#define SWF1D           0x71444
+#define SWF1E           0x71448
+#define SWF1F           0x7144C
+
+
+/*-----------------------------------------------------------------------------
+ * Display Plane C Register Definitions (72180h - 72188h)
+ *---------------------------------------------------------------------------*/
+#define DSPCCNTR        0x72180  /* Display Plane C */
+#define DSPCADDR        0x72184  /* Display C Start Address */
+#define DSPCSTRIDE      0x72188  /* Display C Stride */
+#define DSPCPOS         0x7218C  /* Display C Position */
+#define DSPCSIZE        0x72190  /* Display C Size */
+#define DSPCKEYMINVAL   0x72194  /* Sprite color key Min */
+#define DSPCKEYMASK     0x72198  /* Sprite color key mask */
+#define DSPCKEYMAXVAL   0x721A0  /* Display C Sprint color key Max */
+
+#define DCLRC0          0x721D0  /* Display C Color Correction 0 */
+#define DCLRC1          0x721D4  /* Display C Color Correction 1 */
+#define DPYC_GAMC5      0x721E0  /* Display C Gamma Correction 5 */
+#define DPYC_GAMC4      0x721E4  /* Display C Gamma Correction 4 */
+#define DPYC_GAMC3      0x721E8  /* Display C Gamma Correction 3 */
+#define DPYC_GAMC2      0x721EC  /* Display C Gamma Correction 2 */
+#define DPYC_GAMC1      0x721F0  /* Display C Gamma Correction 1 */
+#define DPYC_GAMC0      0x721F4  /* Display C Gamma Correction 0 */
+
+#define PLANE_ENABLE    BIT31
+#define GAMMA_ENABLE    BIT30
+#define BPP_MASK        BIT29 + BIT28 + BIT27 + BIT26
+#define BPP_POS         26
+#define STEREO_ENABLE   BIT25
+#define PIPE_SEL        BIT24
+#define PIPE_SEL_POS    24
+#define PIXEL_MULTIPLY  BIT21 + BIT20
+#define STEREO_POLARITY BIT18
+
+/* Common offsets for all Display Pipeline registers */
+#define DSP_START_OFFSET 0x04  /* Offset from the control reg */
+#define DSP_STRIDE_OFFSET 0x08  /* Offset from the control reg */
+#define DSP_SIZE_OFFSET   0x10  /* Offset from the control reg */
+
+
+
+/*-----------------------------------------------------------------------------
+ * VGA Display Plane Control Register Definitions (71400h)
+ *---------------------------------------------------------------------------*/
+#define VP00             0x71400
+#define VGACNTRL         0x71400  /* VGA Display Plane Control Register */
+#define VGA_DOUBLE       BIT30
+#define VGA_PIPE         BIT29
+#define VGA_CENTER_MASK  BIT25 + BIT24
+#define VGA_CENTER_1     BIT25
+#define VGA_CENTER_0     BIT24
+#define VGA_PAL_READ     BIT23
+#define VGA_PAL_MASK     BIT22 + BIT21
+#define VGA_PALA_DISABLE BIT22
+#define VGA_PALB_DISABLE BIT21
+#define DAC_8_BIT	     BIT20
+#define VGA_8_DOT	     BIT18
+
+#define ADD_ID           0x71408  /* ADD Card ID Register*/
+
+#define OVADD            0x30000  /* Overlay Control */
+
+/*-----------------------------------------------------------------------------
+ * Software Flag Registers (71410h - 71428h)
+ *---------------------------------------------------------------------------*/
+
+/* Map old software flag names to their new Gen4 names */
+#define SF00  SWF10
+#define SF01  SWF11
+#define SF02  SWF12
+#define SF03  SWF13
+#define SF04  SWF14
+#define SF05  SWF15
+#define SF06  SWF16
+
+
+
+/*-----------------------------------------------------------------------------
+ * Scratch register to be used as additional parameter in SMI
+ *---------------------------------------------------------------------------*/
+#define SCRATCH_SWF6         0x71428
+
+/*-----------------------------------------------------------------------------
+ * Miscellaneous registers
+ ----------------------------------------------------------------------------*/
+#define HW_ST_PAGE_ADDR      0x02080
+
+
+/*-----------------------------------------------------------------------------
+ * GMBUS : I2C Bus Types
+ ----------------------------------------------------------------------------*/
+#define GMBUS_ANALOG_DDC    1
+#define GMBUS_INT_LVDS_DDC  2
+
+#define GMBUS_DVO_REG       3
+
+#define GMBUS_DVOB_DDC      4
+#define GMBUS_DVOC_DDC      5
+
+#endif /* _REGS_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/sgx.h b/drivers/gpu/drm/emgd/emgd/include/plb/sgx.h
new file mode 100644
index 0000000..b882efc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/sgx.h
@@ -0,0 +1,217 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sgx.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  These are the defines specific to the SGX engine code.
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _SGX_H
+#define _SGX_H
+
+
+/* ---------------------------------------------------------*/
+/* MACROS */
+/* ---------------------------------------------------------*/
+#ifndef BUG_ON
+#define BUG_ON(_cond)
+#endif
+
+#ifndef BUG
+#define BUG()
+#endif
+
+
+/* ---------------------------------------------------------*/
+/* DEFINES*/
+/* ---------------------------------------------------------*/
+/* PSB SGX Memory buffer types */
+/* FIXME! - rename 'DRM' prefix to 'SGX'
+ * if we start using this */
+#define DRM_BO_MEM_LOCAL                        0
+#define DRM_BO_MEM_TT                           1
+#define DRM_BO_MEM_VRAM                         2
+#define DRM_BO_MEM_PRIV0                        3
+#define DRM_BO_MEM_PRIV1                        4
+#define DRM_BO_MEM_PRIV2                        5
+#define DRM_BO_MEM_PRIV3                        6
+#define DRM_BO_MEM_PRIV4                        7
+#define DRM_BO_MEM_TYPES                        8
+		/* For now. */
+#define DRM_BO_LOCK_UNLOCK_BM                   (1 << 0)
+#define DRM_BO_LOCK_IGNORE_NO_EVICT             (1 << 1)
+
+/* GMM APERTURE FUNCTIONAL SEGMENTATION
+ * - BEWARE!!!! we are not using this kind
+ *   of pre-allocated slots in IEGD arch
+ *   so this should remain commented!!??
+ *
+#define PSB_VDC_OFFSET                          0x00000000
+#define PSB_VDC_SIZE                            0x000080000
+#define PSB_SGX_SIZE                            0x8000
+#define PSB_SGX_OFFSET                          0x00040000
+#define PSB_MMIO_RESOURCE                       0
+#define PSB_GATT_RESOURCE                       2
+#define PSB_GTT_RESOURCE                        3
+#define PSB_GMCH_CTRL                           0x52
+#define PSB_BSM                                 0x5C
+#define _PSB_GMCH_ENABLED                       0x4
+#define PSB_PGETBL_CTL                          0x2020
+#define _PSB_PGETBL_ENABLED                     0x00000001
+#define PSB_SGX_2D_SLAVE_PORT                   0x4000
+#define PSB_TT_PRIV0_LIMIT                      (256*1024*1024)
+#define PSB_TT_PRIV0_PLIMIT                     (PSB_TT_PRIV0_LIMIT >> PAGE_SHIFT)
+#define PSB_NUM_VALIDATE_BUFFERS                512
+#define PSB_MEM_KERNEL_START                    0x10000000
+#define PSB_MEM_PDS_START                       0x20000000
+#define PSB_MEM_RASTGEOM_START                  0x30000000
+#define PSB_MEM_MMU_START                       0x40000000
+*/
+
+#define XPSB_LOCAL_SIZE                         4096
+#define XPSB_SPROG_SIZE                         1024
+#define XPSB_GEOM_SIZE                          8192
+
+#define PSB_HW_COOKIE_SIZE                      16
+#define PSB_HW_FEEDBACK_SIZE                    8
+
+#define PAGE_SHIFT                              12
+#define PSB_DPM_BUFFER_PAGES                    ((10*1024*1024) >> PAGE_SHIFT)
+#define PSB_DPM_TABLE_SIZE                      0x40000
+#define PSB_PARAM_PAGE_SHIFT                    PAGE_SHIFT
+#define PSB_PARAM_PAGE_SIZE                     PAGE_SIZE
+#define PSB_MIN_NONGLOBAL_PAGES                 1024
+#define PSB_PLM(_val, _base)			\
+		(((_val) << (_base ## _SHIFT)) & (_base ## _MASK))
+
+/*
+ * Number of registers in a PDS attribute chunk.
+ */
+#define PSB_PDS_CHUNK_SIZE		                (32)
+#define PSB_PDS_CHUNK_SIZE_SHIFT	            (5)
+
+/*
+ * Number of registers reserved for output registers.
+ */
+#define PSB_USE_NUM_OUTPUT_REGISTERS            (384)
+
+/*
+ * Total number of USE registers.
+ */
+
+#define PSB_USE_NUM_UNIFIED_REGISTERS	        (2048)
+/*
+ *
+ */
+
+#define PSB_USE_DEFAULT_TEMP_REG_COUNT	        (384)
+#define PSB_USE_DEFAULT_TEMP_GRAN	            (4)
+#define PSB_USE_DEFAULT_TEMP_REG_INIT	        (24)
+#define PSB_USE_DEFAULT_ATTRIB_REG_COUNT        (2048 - 384 * 2) /* 1280 */
+
+
+/* ---------------------------------------------------------*/
+/* STRUCTURES */
+/* ---------------------------------------------------------*/
+struct drm_psb_xhw_arg {
+	unsigned long op;
+	int ret;
+	unsigned long irq_op;
+	unsigned long issue_irq;
+	unsigned long cookie[PSB_HW_COOKIE_SIZE];
+	union {
+		struct {
+			unsigned long w;
+			unsigned long h;
+			unsigned long size;
+			unsigned long clear_p_start;
+			unsigned long clear_num_pages;
+		} si;
+		struct {
+			unsigned long fire_flags;
+			unsigned long hw_context;
+			unsigned long offset;
+			unsigned long engine;
+			unsigned long flags;
+			unsigned long feedback[PSB_HW_FEEDBACK_SIZE];
+		} sb;
+		struct {
+			unsigned long pages;
+			unsigned long size;
+		} bi;
+		struct {
+			unsigned long bca;
+			unsigned long rca;
+			unsigned long flags;
+		} oom;
+	} arg;
+};
+struct pclosed_vopt
+{
+    int fix_hw_brn_20267;
+    int fix_hw_brn_20696;
+    int fix_hw_brn_20852;
+    int fix_hw_brn_21024;
+    int fix_hw_brn_21049;
+    int fix_hw_brn_21117;
+    int fix_hw_brn_21158;
+    int fix_hw_brn_21183;
+    int fix_hw_brn_21226;
+    int fix_hw_brn_21227;
+    int fix_hw_brn_21301;
+    int fix_hw_brn_21329;
+    int fix_hw_brn_21351;
+    int fix_hw_brn_21369;
+    int fix_hw_brn_21387;
+    int fix_hw_brn_21246;
+    int fix_hw_brn_21500;
+    int fix_hw_brn_21551;
+    int fix_hw_brn_21592;
+    int fix_hw_brn_21652;
+    int fix_hw_brn_21788;
+    int fix_hw_brn_21826;
+    int fix_hw_brn_21878;
+    int fix_hw_brn_21893;
+    int fix_hw_brn_21934;
+    int fix_hw_brn_21986;
+    int fix_hw_brn_22048;
+    int fix_hw_brn_22107;
+    int fix_hw_brn_22111;
+    int fix_hw_brn_22136;
+    int fix_hw_brn_22162;
+    int fix_hw_brn_22329;
+    int fix_hw_brn_22336;
+    int fix_hw_brn_22364;
+    int fix_hw_brn_22117;
+    int fix_hw_brn_22380;
+    int fix_hw_brn_22391;
+    int fix_hw_brn_22393;
+    int fix_hw_brn_22462;
+    int fix_hw_brn_22563;
+    int fix_hw_brn_22666;
+    int fix_hw_brn_23281;
+};
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/state3d.h b/drivers/gpu/drm/emgd/emgd/include/plb/state3d.h
new file mode 100644
index 0000000..68f4a80
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/state3d.h
@@ -0,0 +1,398 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: state3d.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is an inter-module header file for manipulating the 3D
+ *  State variables.
+ *-----------------------------------------------------------------------------
+ */
+/*
+ * This is referenced from Napa code. For Poulsbo, the HW spec may be
+ * different. Be ready for the new change.
+ */
+
+#ifndef _STATE3D_H
+#define _STATE3D_H
+
+#include <plb/appcontext.h>
+
+/* The dispatch table */
+typedef struct _state3d_dispatch_plb_t {
+	int lsi1_s2_needs_s3;     /* Does LSI1 S2 also require S3? */
+	int lsi2_s3_830_845;
+} state3d_dispatch_plb_t;
+
+/* The array of buffer infos are used for these purposes */
+#define COLOR_BUFFER_INDEX 0
+#define DEPTH_BUFFER_INDEX 1
+#define AUX0_BUFFER_INDEX  2
+#define AUX1_BUFFER_INDEX  3
+#define INTRA_BUFFER_INDEX 4
+
+typedef struct _state3d_buffer_info_plb {
+	union {
+		struct {
+			unsigned long pitch :14;
+			unsigned long :7;
+			unsigned long tile_walk :1;
+			unsigned long tiled :1;
+			unsigned long fenced :1;
+			unsigned long buffer_id :4;
+			unsigned long aux_id :1;
+			unsigned long :3;
+		};
+		unsigned long dw1;
+	};
+	union {
+		unsigned long base;
+		unsigned long dw2;
+	};
+	unsigned long mod;
+} state3d_buffer_info_plb_t;
+
+typedef struct _state3d_dest_buffer_vars_plb {
+	union {
+		struct {
+			unsigned long vert_offset  :1;
+			unsigned long vert_stride  :1;
+			unsigned long depth_format :3;
+			unsigned long :1;
+			unsigned long depth_component :1;
+			unsigned long :1;
+			unsigned long color_format :4;
+			unsigned long write_select :3;
+			unsigned long :1;
+			unsigned long vert_bias  :4;
+			unsigned long horiz_bias :4;
+			unsigned long dither_disable :1;
+			unsigned long gamma_blend_enable :1;
+			unsigned long dither_patten :2;
+			unsigned long :4;
+		};
+		unsigned long dw1;
+	};
+	unsigned long mod;
+} state3d_dest_buffer_vars_plb_t;
+
+typedef struct _state3d_vertex_buffer {
+	unsigned long addr;
+	unsigned long width;
+	unsigned long pitch;
+	unsigned long enable;
+	unsigned long mod;
+} state3d_vertex_buffer_t;
+
+#define VERTEX_BUFFER_ADDR_MODIFIED    0x1
+#define VERTEX_BUFFER_WIDTH_MODIFIED   0x2
+#define VERTEX_BUFFER_PITCH_MODIFIED   0x4
+#define VERTEX_BUFFER_ENABLE_MODIFIED  0x8
+
+typedef struct _state3d_texture_coord_set {
+	unsigned long format;
+	unsigned long enable;
+	unsigned long mod;
+} state3d_texture_coord_set_t;
+
+typedef struct _state3d_coord_set {
+	unsigned long transform_enable;
+	unsigned long normalized_coords;
+	unsigned long source;
+	unsigned long type;
+	unsigned long addr_v_control_mode;
+	unsigned long addr_u_control_mode;
+	unsigned long mod;
+} state3d_coord_set_t;
+
+typedef struct _state3d_texel_stream {
+	unsigned long modification_enable;
+	unsigned long modifier_unit_index;
+	unsigned long coord_select;
+	unsigned long map_select;
+	unsigned long mod;
+} state3d_texel_stream_t;
+
+#define MODIFICATION_ENABLE_MODIFIED 0x1
+#define MODIFIER_UNIT_INDEX_MODIFIED 0x2
+#define COORD_SELECT_MODIFIED        0x4
+#define MAP_SELECT_MODIFIED          0x8
+
+typedef struct _state3d_texel_modifier {
+	unsigned long texel_stream_index;
+	unsigned long bump_param_table_index;
+	unsigned long enable;
+	unsigned long mod;
+} state3d_texel_modifier_t;
+
+#define TEXEL_STREAM_INDEX_MODIFIED     0x1
+#define BUMP_PARAM_TABLE_INDEX_MODIFIED 0x2
+#define ENABLE_MODIFIED                 0x4
+
+/*
+ * These can be changed with the LOAD_STATE_IMMEDIATE_1 S3 Instruction.
+ */
+typedef struct _state3d_imm1_state3 {
+	unsigned long point_width;
+	unsigned long line_width;
+	unsigned long alpha_shade_mode;
+	unsigned long fog_shade_mode;
+	unsigned long specular_shade_mode;
+	unsigned long color_shade_mode;
+	unsigned long cull_mode;
+	unsigned long point_width_present;
+	unsigned long specular_color_present; /* And Fog Factor */
+	unsigned long diffuse_color_present;
+	unsigned long depth_offset_present;
+	unsigned long position_mask;
+	unsigned long specular_add_enable;
+	unsigned long fog_enable;
+	unsigned long local_depth_bias_enable;
+	unsigned long sprite_point_enable;
+	unsigned long antialiasing_enable;
+	unsigned long mod;
+} state3d_imm1_state3_t;
+
+#define POINT_WIDTH_MODIFIED             0x1
+#define LINE_WIDTH_MODIFIED              0x2
+#define ALPHA_SHADE_MODE_MODIFIED        0x4
+#define FOG_SHADE_MODE_MODIFIED          0x8
+#define SPECULAR_SHADE_MODE_MODIFIED     0x10
+#define COLOR_SHADE_MODE_MODIFIED        0x20
+#define CULL_MODE_MODIFIED               0x40
+#define POINT_WIDTH_PRESENT_MODIFIED     0x80
+#define SPECULAR_COLOR_PRESENT_MODIFIED  0x100
+#define DIFFUSE_COLOR_PRESENT_MODIFIED   0x200
+#define DEPTH_OFFSET_PRESENT_MODIFIED    0x400
+#define POSITION_MASK_MODIFIED           0x800
+#define SPECULAR_ADD_ENABLE_MODIFIED     0x1000
+#define FOG_ENABLE_MODIFIED              0x2000
+#define LOCAL_DEPTH_BIAS_ENABLE_MODIFIED 0x4000
+#define SPRITE_POINT_ENABLE_MODIFIED     0x8000
+#define ANTIALAISING_ENABLE_MODIFIED     0x10000
+
+/*
+ * These can be changed with the LOAD_STATE_IMMEDIATE_1 S7 Instruction.
+ */
+typedef struct _state3d_imm1_state7 {
+	unsigned long global_depth_bias;
+	unsigned long stencil_reference_value;
+	unsigned long stencil_test_function;
+	unsigned long stencil_fail_op;
+	unsigned long stencil_pass_depth_fail_op;
+	unsigned long stencil_pass_depth_pass_op;
+	unsigned long stencil_buffer_write_enable;
+	unsigned long stencil_test_enable;
+	unsigned long color_dither_enable;
+	unsigned long logic_op_enable;
+	unsigned long mod;
+} state3d_imm1_state7_t;
+
+#define GLOBAL_DEPTH_BIAS_MODIFIED           0x1
+#define STENCIL_REFERENCE_VALUE_MODIFIED     0x2
+#define STENCIL_TEST_FUNCTION_MODIFIED       0x4
+#define STENCIL_FAIL_OP_MODIFIED             0x8
+#define STENCIL_PASS_DEPTH_FAIL_OP_MODIFIED  0x10
+#define STENCIL_PASS_DEPTH_PASS_OP_MODIFIED  0x20
+#define STENCIL_BUFFER_WRITE_ENABLE_MODIFIED 0x40
+#define STENCIL_TEST_ENABLE_MODIFIED         0x80
+#define COLOR_DITHER_ENABLE_MODIFIED         0x100
+#define LOGIC_OP_ENABLE_MODIFIED             0x200
+
+/*
+ * These can be changed with the LOAD_STATE_IMMEDIATE_1 S8 Instruction.
+ */
+typedef struct _state3d_imm1_state8 {
+	unsigned long alpha_test_enable;
+	unsigned long alpha_test_function;
+	unsigned long alpha_reference_value;
+	unsigned long depth_test_enable;
+	unsigned long depth_test_function;
+	unsigned long color_buffer_blend_enable;
+	unsigned long color_blend_function;
+	unsigned long source_blend_factor;
+	unsigned long destination_blend_factor;
+	unsigned long depth_buffer_write_enable;
+	unsigned long color_buffer_write_enable;
+	unsigned long triangle_provoking_vertex_select;
+} state3d_imm1_state8_t;
+
+typedef struct _state3d_imm2_tms0 {
+	unsigned long tm_map_base_addr;
+	unsigned long tm_utilize_fence_regs;
+	unsigned long reverse_gamma_enable;
+} state3d_imm2_tms0_t;
+
+typedef struct _state3d_imm2_tms1 {
+	unsigned long tm_height;
+	unsigned long tm_width;
+	unsigned long tm_palette_select;
+	unsigned long tm_surface_format;
+	unsigned long tm_texel_format;
+	unsigned long tm_color_space_conversion_enable;
+	unsigned long tm_tiled_surface;
+	unsigned long tm_tile_walk;
+} state3d_imm2_tms1_t;
+
+typedef struct _state3d_imm2_tms2 {
+	unsigned long tm_dword_pitch;
+	unsigned long tm_cube_face_enables;
+	unsigned long tm_map_format;
+	unsigned long tm_vertical_line_stride;
+	unsigned long tm_vertical_line_stride_offset;
+	unsigned long tm_output_channel_selection;
+	unsigned long tm_base_mip_level;
+	unsigned long tm_lod_preclamp_enable;
+} state3d_imm2_tms2_t;
+
+typedef struct _state3d_imm2_tms3 {
+	unsigned long tm_mip_mode_filter;
+	unsigned long tm_mag_mode_filter;
+	unsigned long tm_min_mode_filter;
+	unsigned long tm_texture_lod_bias;
+	unsigned long tm_colorkey_enable;
+	unsigned long tm_chromakey_enable;
+	unsigned long tm_maximum_mip_level;
+	unsigned long tm_minimum_mip_level;
+	unsigned long tm_kill_pixel_enable;
+	unsigned long tm_keyed_texture_filter_mode;
+} state3d_imm2_tms3_t;
+
+typedef struct _state3d_imm2_tms4 {
+	unsigned long tm_default_color;
+} state3d_imm2_tms4_t;
+
+/*
+ * All pointers may be NULL. If so defaults should be assumed.
+ */
+typedef struct _state3d {
+	igd_surface_t color_buffer;
+	igd_surface_t depth_buffer;
+	state3d_vertex_buffer_t vertex_buffer[2];
+	state3d_texture_coord_set_t texture_coord_set[8];
+	unsigned long texture_coord_count;
+	state3d_imm1_state3_t imm1_s3;
+	state3d_coord_set_t coord_set[4];
+	state3d_texel_modifier_t texel_modifier[2];
+	state3d_texel_stream_t texel_stream[4];
+	state3d_imm1_state7_t imm1_s7;
+	state3d_imm1_state8_t imm1_s8;
+	state3d_imm2_tms0_t imm2_tms0[4];
+	state3d_imm2_tms1_t imm2_tms1[4];
+	state3d_imm2_tms2_t imm2_tms2[4];
+	state3d_imm2_tms3_t imm2_tms3[4];
+	state3d_imm2_tms4_t imm2_tms4[4];
+	state3d_buffer_info_plb_t buffer_info[5];
+	state3d_dest_buffer_vars_plb_t buffer_vars;
+	unsigned long mod;
+	state3d_dispatch_plb_t *dispatch;
+} state3d_t, state3d_plb_t;
+
+#define IMM1_MODIFIED    0x1ff
+#define IMM1_S0_MODIFIED 0x1
+#define IMM1_S1_MODIFIED 0x2
+#define IMM1_S2_MODIFIED 0x4
+#define IMM1_S3_MODIFIED 0x8
+#define IMM1_S4_MODIFIED 0x10
+#define IMM1_S5_MODIFIED 0x20
+#define IMM1_S6_MODIFIED 0x40
+#define IMM1_S7_MODIFIED 0x80
+#define IMM1_S8_MODIFIED 0x100
+#define IMM2_MODIFIED     0xf0000
+#define IMM2_TM0_MODIFIED 0x10000
+#define IMM2_TM1_MODIFIED 0x20000
+#define IMM2_TM2_MODIFIED 0x40000
+#define IMM2_TM3_MODIFIED 0x80000
+
+#define LOAD_S0 0x1
+#define LOAD_S1 0x2
+#define LOAD_S2 0x4
+#define LOAD_S3 0x8
+#define LOAD_S4 0x10
+#define LOAD_S5 0x20
+#define LOAD_S6 0x40
+#define LOAD_S7 0x80
+#define LOAD_S8 0x100
+#define LOAD_TM  0xf0000
+#define LOAD_TM0 0x10000
+#define LOAD_TM1 0x20000
+#define LOAD_TM2 0x40000
+#define LOAD_TM3 0x80000
+
+#define STATE3D_SET_ALPHA_TEST_ENABLE(s, v) \
+    s->imm1_s8.alpha_test_enable = (v & 1); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_ALPHA_TEST_FUNCTION(s, v) \
+    s->imm1_s8.alpha_test_function = (v & 7); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_ALPHA_REFERENCE_VALUE(s, v) \
+    s->imm1_s8.alpha_reference_value = (v & 0xf); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_DEPTH_TEST_ENABLE(s, v) \
+    s->imm1_s8.depth_test_enable = (v & 1); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_DEPTH_TEST_FUNCTION(s, v) \
+    s->imm1_s8.depth_test_function = (v & 7); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_COLOR_BUFFER_BLEND_ENABLE(s, v) \
+    s->imm1_s8.color_buffer_blend_enable = (v & 1); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_COLOR_BLEND_FUNCTION(s, v) \
+    s->imm1_s8.color_blend_function = (v & 7); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_SOURCE_BLEND_FACTOR(s, v) \
+    s->imm1_s8.source_blend_factor = (v & 0xf); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_DESTINATION_BLEND_FACTOR(s, v) \
+    s->imm1_s8.destination_blend_factor = (v & 0xf); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_DEPTH_BUFFER_WRITE_ENABLE(s, v) \
+    s->imm1_s8.depth_buffer_write_enable = (v & 1); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_COLOR_BUFFER_WRITE_ENABLE(s, v) \
+    s->imm1_s8.color_buffer_write_enable = (v & 1); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+#define STATE3D_SET_TRIANGLE_PROVOKING_VERTEX_SELECT(s, v) \
+    s->imm1_s8.triangle_provoking_vertex_select = (v & 3); \
+    s->mod |= IMM1_S8_MODIFIED;
+
+
+#define STATE3D(ac) ((state3d_t *)((appcontext_plb_t *)ac)->state3d)
+
+int state3d_update_plb(igd_command_t **in, appcontext_t *appcontext);
+int state3d_update_size(appcontext_t *appcontext);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/plb/state3d_plb.h b/drivers/gpu/drm/emgd/emgd/include/plb/state3d_plb.h
new file mode 100644
index 0000000..37327bc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/plb/state3d_plb.h
@@ -0,0 +1,1299 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: state3d_plb.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is an inter-module header file for manipulating the 3D
+ *  State variables.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _STATE3D_PLB_H
+#define _STATE3D_PLB_H
+
+#include <plb/appcontext.h>
+
+typedef struct _state3d_anti_aliasing_plb {
+	union {
+		struct {
+			unsigned int :6;
+			unsigned int line_aa_region_width :2;
+			unsigned int line_aa_region_width_mod_en :1;
+			unsigned int :5;
+			unsigned int line_end_cap_aa_region_width :2;
+			unsigned int line_end_cap_aa_region_width_mod_en :1;
+			unsigned int :15;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+
+} state3d_anti_aliasing_plb_t;
+
+#define STATE3D_ANTI_ALIASING_UPDATED_PLB	1
+#define STATE3D_ANTI_ALIASING_DWORD_COUNT_PLB 	1
+
+typedef struct _state3d_backface_stencil_ops_plb {
+	union {
+		struct {
+			unsigned int double_sided_en :1;
+			unsigned int double_sided_en_mod_en :1;
+			unsigned int pass_depth_pass_op :3;
+			unsigned int pass_depth_fail_op :3;
+			unsigned int fail_op :3;
+			unsigned int test_function :3;
+			unsigned int multiple_mod_en :1;
+			unsigned int ref_val :8;
+			unsigned int ref_val_mod_en :1;
+			unsigned int :8;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_backface_stencil_ops_plb_t;
+#define STATE3D_BACKFACE_STENCIL_OPS_UPDATED_PLB	1
+#define STATE3D_BACKFACE_STENCIL_OPS_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_backface_stencil_masks_plb {
+	union {
+		struct {
+			unsigned int write_mask :8;
+			unsigned int test_mask :8;
+			unsigned int write_mask_mod_en :1;
+			unsigned int test_mask_mod_en :1;
+			unsigned int :14;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_backface_stencil_masks_plb_t;
+#define STATE3D_BACKFACE_STENCIL_MASK_UPDATED_PLB	1
+#define STATE3D_BACKFACE_STENCIL_MASK_DWORD_COUNT_PLB	1
+
+/* FIXME: Details are ignored in this struct */
+typedef struct _state3d_bin_control_plb {
+	unsigned int dw0;
+	unsigned int dw1;
+	unsigned int dw2;
+	unsigned int dw3;
+	unsigned int dw4;
+	unsigned int dw5;
+	unsigned int updated;
+} state3d_bin_control_plb_t;
+#define STATE3D_BIN_CONTROL_UPDATED_PLB		1
+#define STATE3D_BIN_CONTROL_DWORD_COUNT_PLB	6
+
+typedef struct _state3d_buffer_info_plb {
+	unsigned int dw0;
+	union {
+		struct {
+			unsigned int :2;
+			unsigned int buffer_pitch :12;
+			unsigned int :7;
+			unsigned int tile_walk :1;
+			unsigned int tiled_surface :1;
+			unsigned int util_fence_regs :1;
+			unsigned int buffer_id :4;
+			unsigned int aux_buffer_id :1;
+			unsigned int :3;
+		};
+		unsigned int dw1;
+	};
+	union {
+		struct {
+			unsigned int :2;
+			unsigned int buffer_base_addr :26;
+		};
+		unsigned int dw2;
+	};
+	unsigned int updated;
+} state3d_buffer_info_plb_t;
+#define STATE3D_BUFFER_INFO_UPDATED_PLB		1
+#define STATE3D_BUFFER_INFO_DWORD_COUNT_PLB	3
+#define STATE3D_BUFFER_INFO_BUFFERID_COLOR_BACK			0x03
+#define STATE3D_BUFFER_INFO_BUFFERID_COLOR_AUX			0x04
+#define STATE3D_BUFFER_INFO_BUFFERID_COLOR_MC_INTRA_CORR	0x05
+#define STATE3D_BUFFER_INFO_BUFFERID_DEPTH			0x07
+
+typedef struct _state3d_chroma_key_plb {
+	union {
+		struct {
+			unsigned int :30;
+			unsigned int table_index :2;
+		};
+		unsigned int dw1;
+	};
+	union {
+		unsigned int lo_val;
+		unsigned int dw2;
+	};
+	union {
+		unsigned int hi_val;
+		unsigned int dw3;
+	};
+	unsigned int updated;
+} state3d_chroma_key_plb_t;
+#define STATE3D_CHROMA_KEY_UPDATED_PLB		1
+#define STATE3D_CHROMA_KEY_DWORD_COUNT_PLB	4
+
+typedef struct _state3d_clear_parameters_plb {
+	union {
+		struct {
+		unsigned int stencil_write_en :1;
+		unsigned int depth_buffer_write_en :1;
+		unsigned int color_buffer_write_en :1;
+			unsigned int :13;
+			unsigned int clear_primitive_type :1;
+			unsigned int :15;
+		};
+		unsigned int dw1;
+        };
+	union {
+		unsigned int	clear_color_buffer_val;
+		unsigned int dw2;
+	};
+	union {
+	unsigned int	clear_depth_buffer_val;
+		unsigned int dw3;
+	};
+	union {
+	unsigned int	clear_color;
+		unsigned int dw4;
+	};
+	union {
+	unsigned int	clear_depth;
+		unsigned int dw5;
+	};
+	union {
+		struct {
+		unsigned int clear_stencil :8;
+			unsigned int :24;
+		};
+		unsigned int dw6;
+	};
+	unsigned int updated;
+} state3d_clear_parameters_plb_t;
+#define STATE3D_CLEAR_PARAMETERS_UPDATED_PLB		1
+#define STATE3D_CLEAR_PARAMETERS_DWORD_COUNT_PLB	7
+
+typedef struct _state3d_constant_blend_color_plb {
+	union {
+		unsigned int color;
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_constant_blend_color_plb_t;
+#define STATE3D_CONSTANT_BLEND_COLOR_UPDATED_PLB	1
+#define STATE3D_CONSTANT_BLEND_COLOR_DWORD_COUNT_PLB	2
+
+typedef struct _state3d_coord_set_bindings_plb {
+	union {
+		struct {
+			unsigned int internal_tc_set_0_src :3;
+			unsigned int internal_tc_set_1_src :3;
+			unsigned int internal_tc_set_2_src :3;
+			unsigned int internal_tc_set_3_src :3;
+			unsigned int internal_tc_set_4_src :3;
+			unsigned int internal_tc_set_5_src :3;
+			unsigned int internal_tc_set_6_src :3;
+			unsigned int internal_tc_set_7_src :3;
+			unsigned int :8;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_coord_set_bindings_plb_t;
+#define STATE3D_COORD_SET_BINDINGS_UPDATED_PLB		1
+#define STATE3D_COORD_SET_BINDINGS_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_default_diffuse_plb {
+	union {
+		unsigned int color;
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_default_diffuse_plb_t;
+#define STATE3D_DEFAULT_DIFFUSE_UPDATED_PLB		1
+#define STATE3D_DEFAULT_DIFFUSE_DWORD_COUNT_PLB		2
+
+typedef struct _state3d_default_specular_plb {
+	union {
+		unsigned int color;
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_default_specular_plb_t;
+#define STATE3D_DEFAULT_SPECULAR_UPDATED_PLB		1
+#define STATE3D_DEFAULT_SPECULAR_DWORD_COUNT_PLB	2
+
+typedef struct _state3d_default_z_plb {
+	union {
+		unsigned int depth;
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_default_z_plb_t;
+#define STATE3D_DEFAULT_Z_UPDATED_PLB			1
+#define STATE3D_DEFAULT_Z_DWORD_COUNT_PLB		2
+
+typedef struct _state3d_depth_offset_scale_plb {
+	union {
+		unsigned int global_val;
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_depth_offset_scale_plb_t;
+#define STATE3D_DEPTH_OFFSET_SCALE_UPDATED_PLB		1
+#define STATE3D_DEPTH_OFFSET_SCALE_DWORD_COUNT_PLB	2
+
+typedef struct _state3d_depth_subrectangle_enable_plb {
+	union {
+		struct {
+			unsigned int enable :1;	/* B-spec says this is difeatured, MUST BE DISABLE */
+			unsigned int mod_en :1;
+			unsigned int :30;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_depth_subrectangle_enable_plb_t;
+#define STATE3D_DEPTH_SUBRECTANGLE_ENABLE_UPDATED_PLB		1
+#define STATE3D_DEPTH_SUBRECTANGLE_ENABLE_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_dest_buffer_variables_plb {
+	union {
+		struct {
+			unsigned int vert_line_stride_offset :1;
+			unsigned int vert_line_stride :1;
+			unsigned int depth_buffer_format :2;
+			unsigned int :4;
+			unsigned int color_buffer_format :4;
+			unsigned int write_select_422_channel :3;
+			unsigned int :1;
+			unsigned int dest_origin_vert_bias :4;
+			unsigned int dest_origin_horiz_bias :4;
+			unsigned int dither_enhance_dis :1;
+			unsigned int linear_gamma_blend_en :1;
+			unsigned int dither_pattern_select :2;
+			unsigned int lod_preclamp_en :1;
+			unsigned int early_depth_test_en :1;
+			unsigned int texture_default_color_mode :1;
+			unsigned int :1;
+		};
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_dest_buffer_variables_plb_t;
+#define STATE3D_DEST_BUFFER_VARIABLES_UPDATED_PLB	      1
+#define STATE3D_DEST_BUFFER_VARIABLES_DWORD_COUNT_PLB	  2
+
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_8BIT          0
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_X1R5G6B5      1
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_R5G6B5        2
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_A8R8G8B8      3
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_YCRCB_SWAPY   4
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_YCRCB_NORMAL  5
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_YCRCB_SWAPUV  6
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_YCRCB_SWAPUVY 7
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_A4R4G4B4      8
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_A1R5G5B5      9
+#define STATE3D_DEST_BUFF_COLOR_BUFF_FORMAT_A2R10G10B10   0xA
+
+typedef struct _state3d_drawing_rectangle_plb {
+	union {
+		struct {
+			unsigned int :24;
+		unsigned int y_dither_offset :2;
+		unsigned int x_dither_offset :2;
+			unsigned int :2;
+		unsigned int depth_buffer_coord_offset_dis :1;
+		unsigned int fast_scissor_clip_dis :1;
+		};
+		unsigned int dw1;
+	};
+	union {
+		struct {
+		unsigned int clip_draw_rect_x_min :16;
+		unsigned int clip_draw_rect_y_min :16;
+		};
+		unsigned int dw2;
+	};
+	union {
+		struct {
+		unsigned int clip_draw_rect_x_max :16;
+		unsigned int clip_draw_rect_y_max :16;
+		};
+		unsigned int dw3;
+	};
+	union {
+		struct {
+		unsigned int draw_rect_origin_x :16;
+		unsigned int draw_rect_origin_y :16;
+		};
+		unsigned int dw4;
+	};
+	unsigned int updated;
+} state3d_drawing_rectangle_plb_t;
+#define STATE3D_DRAWING_RECTANGLE_UPDATED_PLB		1
+#define STATE3D_DRAWING_RECTANGLE_DWORD_COUNT_PLB	5
+
+typedef struct _state3d_filter_coefficients_4x4_plb {
+	/* FIXME: This might be an over simplified abstraction
+	 * and may subject to change to meet its semantic later */
+	unsigned int filter1[32];
+	unsigned int filter2[32];
+	unsigned int updated;
+} state3d_filter_coefficients_4x4_plb_t;
+#define STATE3D_FILTER_COEFFICIENT_4X4_UPDATED_PLB	1
+#define STATE3D_FILTER_COEFFICIENT_4X4_DWORD_COUNT_PLB	65
+
+typedef struct _state3d_filter_coefficients_6x5_plb {
+	union {
+		struct {
+			unsigned int k1 :16;
+			unsigned int k2 :16;
+		};
+		unsigned int dw1;
+	};
+	union {
+		struct {
+			unsigned int k3 :16;
+			unsigned int k4 :16;
+		};
+		unsigned int dw2;
+	};
+	union {
+		struct {
+			unsigned int k5 :16;
+			unsigned int k6 :16;
+		};
+		unsigned int dw3;
+	};
+	unsigned int updated;
+} state3d_filter_coefficients_6x5_plb_t;
+#define STATE3D_FILTER_COEFFICIENT_6X5_UPDATED_PLB	1
+#define STATE3D_FILTER_COEFFICIENT_6X5_DWORD_COUNT_PLB	4
+
+typedef struct _state3d_fog_color_plb {
+	union {
+		struct {
+			unsigned int blue :8;
+			unsigned int green :8;
+			unsigned int red :8;
+			unsigned int :8;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_fog_color_plb_t;
+#define STATE3D_FOG_COLOR_UPDATED_PLB		1
+#define STATE3D_FOG_COLOR_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_fog_mode_plb {
+	union {
+		struct {
+			unsigned int :4;
+			unsigned int linear_fog_c1_const:16;
+			unsigned int :3;
+			unsigned int fog_density_mod_en: 1;
+			unsigned int linear_fog_c1c2_const_mod_en :1;
+			unsigned int fog_source :1;
+			unsigned int :1;
+			unsigned int fog_source_mod_en :1;
+			unsigned int fog_func :2;
+			unsigned int :1;
+			unsigned int fog_func_mod_en :1;
+		};
+		unsigned int dw1;
+	};
+	union {
+		unsigned int linear_fog_c2_const;
+		unsigned int dw2;
+	};
+	union {
+		unsigned int fog_density;
+		unsigned int dw3;
+	};
+	unsigned int updated;
+} state3d_fog_mode_plb_t;
+
+#define STATE3D_FOG_MODE_UPDATED_PLB		1
+#define STATE3D_FOG_MODE_DWORD_COUNT_PLB	4
+
+typedef struct _state3d_independent_alpha_blend_plb {
+	union {
+		struct {
+			unsigned int dest_factor :4;
+			unsigned int :1;
+			unsigned int dest_factor_mod_en :1;
+			unsigned int src_factor :4;
+			unsigned int :1;
+			unsigned int src_factor_mod_en :1;
+			unsigned int :4;
+			unsigned int function :3;
+			unsigned int :2;
+			unsigned int function_mod_en :1;
+			unsigned int enable :1;
+			unsigned int enable_mod_en :1;
+			unsigned int :8;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_independent_alpha_blend_plb_t;
+#define STATE3D_INDEPENDENT_ALPHA_BLEND_UPDATED_PLB	1
+#define STATE3D_INDEPENDENT_ALPHA_BLEND_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_load_indirect_plb {
+	union {
+		struct {
+			unsigned int block_num :8;
+			unsigned int block_mask :6;
+			unsigned int mem_space_select :1;
+			unsigned int :17;
+		};
+		unsigned int dw0;
+	};
+	union {
+		struct {
+			unsigned int static_buffer_valid :1;
+			unsigned int force_static_load :1;
+			unsigned int static_buffer_addr :30;
+		};
+		unsigned int dw_sis0;
+	};
+	union {
+		struct {
+			unsigned int static_buffer_length :9;
+			unsigned int :23;
+		};
+		unsigned int dw_sis1;
+	};
+	union {
+		struct {
+			unsigned int dynamic_buffer_valid :1;
+			unsigned int dynamic_buffer_reset :1;
+			unsigned int dynamic_buffer_addr :30;
+		};
+		unsigned int dw_dis0;
+	};
+	union {
+		struct {
+			unsigned int sampler_buffer_valid :1;
+			unsigned int force_sampler_load :1;
+			unsigned int sampler_buffer_addr :30;
+		};
+		unsigned int dw_ssb0;
+	};
+	union {
+		struct {
+			unsigned int sampler_buffer_length :9;
+			unsigned int :23;
+		};
+		unsigned int dw_ssb1;
+	};
+	union {
+		struct {
+			unsigned int map_buffer_valid :1;
+			unsigned int force_map_load :1;
+			unsigned int map_buffer_addr :30;
+		};
+		unsigned int dw_msb0;
+	};
+	union {
+		struct {
+			unsigned int map_buffer_length :9;
+			unsigned int :23;
+		};
+		unsigned int dw_msb1;
+	};
+	union {
+		struct {
+			unsigned int psp_buffer_valid :1;
+			unsigned int force_psp_load :1;
+			unsigned int psp_buffer_addr :30;
+		};
+		unsigned int dw_psp0;
+	};
+	union {
+		struct {
+			unsigned int psp_buffer_length :9;
+			unsigned int :23;
+		};
+		unsigned int dw_psp1;
+	};
+	union {
+		struct {
+			unsigned int psc_buffer_valid :1;
+			unsigned int force_psc_load :1;
+			unsigned int psc_buffer_addr :30;
+		};
+		unsigned int dw_psc0;
+	};
+	union {
+		struct {
+			unsigned int psc_buffer_length :9;
+			unsigned int :23;
+		};
+		unsigned int dw_psc1;
+	};
+	unsigned int updated;
+} state3d_load_indirect_plb_t;
+#define STATE3D_LOAD_INDIRECT_UPDATED_PLB		1
+#define STATE3D_LOAD_INDIRECT_DWORD_COUNT_PLB		12
+#define STATE3D_LOAD_INDIRECT_HEADER_DWORD_COUNT_PLB	1
+#define STATE3D_LOAD_INDIRECT_PSP_DWORD_COUNT_PLB	2
+
+typedef struct _state3d_load_state_immediate_1_plb {
+	union {
+		struct {
+			unsigned int state_num :4;
+			unsigned int load_dw_s0 :1;
+			unsigned int load_dw_s1 :1;
+			unsigned int load_dw_s2 :1;
+			unsigned int load_dw_s3 :1;
+			unsigned int load_dw_s4 :1;
+			unsigned int load_dw_s5 :1;
+			unsigned int load_dw_s6 :1;
+			unsigned int load_dw_s7 :1;
+			unsigned int :20;
+		};
+		unsigned int dw0;
+	};
+	union {
+		struct {
+			unsigned int vtx_cache_invalid_dis :1;
+			unsigned int :1;
+			unsigned int vtx_buf_addr :26;
+			unsigned int :4;
+		};
+		unsigned int dw_s0;
+	};
+	union {
+		struct {
+			unsigned int :16;
+			unsigned int vtx_buf_pitch :6;
+			unsigned int :2;
+			unsigned int vtx_buf_width :6;
+			unsigned int :2;
+		};
+		unsigned int dw_s1;
+	};
+	union {
+		/*
+		struct {
+			unsigned int :4;
+		} tex_coord_set_fmt[8];
+		*/
+		struct {
+			unsigned int tex_coord_set_0_fmt :4;
+			unsigned int tex_coord_set_1_fmt :4;
+			unsigned int tex_coord_set_2_fmt :4;
+			unsigned int tex_coord_set_3_fmt :4;
+			unsigned int tex_coord_set_4_fmt :4;
+			unsigned int tex_coord_set_5_fmt :4;
+			unsigned int tex_coord_set_6_fmt :4;
+			unsigned int tex_coord_set_7_fmt :4;
+		};
+		unsigned int dw_s2;
+	};
+	union {
+		/*
+		struct {
+			unsigned int pspec_crtn_dis :1;
+			unsigned int wrap_short_tcz :1;
+			unsigned int wrap_short_tcy :1;
+			unsigned int wrap_short_tcx :1;
+		} tex_coor_set[8];
+		*/
+		struct {
+			unsigned int tex_coord_set_0_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_0_wrap_short_tcz :1;
+			unsigned int tex_coord_set_0_wrap_short_tcy :1;
+			unsigned int tex_coord_set_0_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_1_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_1_wrap_short_tcz :1;
+			unsigned int tex_coord_set_1_wrap_short_tcy :1;
+			unsigned int tex_coord_set_1_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_2_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_2_wrap_short_tcz :1;
+			unsigned int tex_coord_set_2_wrap_short_tcy :1;
+			unsigned int tex_coord_set_2_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_3_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_3_wrap_short_tcz :1;
+			unsigned int tex_coord_set_3_wrap_short_tcy :1;
+			unsigned int tex_coord_set_3_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_4_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_4_wrap_short_tcz :1;
+			unsigned int tex_coord_set_4_wrap_short_tcy :1;
+			unsigned int tex_coord_set_4_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_5_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_5_wrap_short_tcz :1;
+			unsigned int tex_coord_set_5_wrap_short_tcy :1;
+			unsigned int tex_coord_set_5_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_6_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_6_wrap_short_tcz :1;
+			unsigned int tex_coord_set_6_wrap_short_tcy :1;
+			unsigned int tex_coord_set_6_wrap_short_tcx :1;
+
+			unsigned int tex_coord_set_7_pspec_crtn_dis :1;
+			unsigned int tex_coord_set_7_wrap_short_tcz :1;
+			unsigned int tex_coord_set_7_wrap_short_tcy :1;
+			unsigned int tex_coord_set_7_wrap_short_tcx :1;
+
+		};
+		unsigned int dw_s3;
+	};
+	union {
+		struct {
+			unsigned int anti_alias_en :1;
+			unsigned int sprite_point_en :1;
+			unsigned int fog_param_present :1;
+			unsigned int local_depth_offset_en :1;
+			unsigned int force_specular_diffuse_color :1;
+			unsigned int force_default_diffuse_color :1;
+			unsigned int position_mask :3;
+			unsigned int local_depth_offset_present: 1;
+			unsigned int diffuse_color_present :1;
+			unsigned int specular_color_fog_factor_present :1;
+			unsigned int point_width_present :1;
+			unsigned int cull_mode :2;
+			unsigned int color_shade_mode :1;
+			unsigned int specular_shade_mode :1;
+			unsigned int fog_shade_mode :1;
+			unsigned int alpha_shade_mode :1;
+			unsigned int line_width :4;
+			unsigned int point_width :9;
+		};
+		unsigned int dw_s4;
+	};
+	union {
+		struct {
+			unsigned int logic_op_en :1;
+			unsigned int color_dither_en :1;
+			unsigned int stencil_test_en :1;
+			unsigned int stencil_buffer_write_en :1;
+			unsigned int stencil_pass_depth_pass_op :3;
+			unsigned int stencil_pass_depth_fail_op :3;
+			unsigned int stencil_fail_op :3;
+			unsigned int stencil_test_func :3;
+			unsigned int stencil_ref_val :8;
+			unsigned int fog_enable :1;
+			unsigned int global_depth_offset_en :1;
+			unsigned int last_pixel_en :1;
+			unsigned int force_default_point_width :1;
+			unsigned int color_buffer_component_write_dis :4;
+		};
+		unsigned int dw_s5;
+	};
+	union {
+		struct {
+			unsigned int triang_list_provoke_vtx_sel :2;
+			unsigned int color_buffer_write_en :1;
+			unsigned int depth_buffer_write_en :1;
+			unsigned int dest_blend_factor :4;
+			unsigned int src_blend_factor :4;
+			unsigned int color_blend_func :3;
+			unsigned int color_buffer_blend_en :1;
+			unsigned int depth_test_func :3;
+			unsigned int depth_test_en :1;
+			unsigned int alpha_ref_val :8;
+			unsigned int alpha_test_func :3;
+			unsigned int alpha_test_en :1;
+		};
+		unsigned int dw_s6;
+	};
+	union {
+		unsigned int global_depth_offset_const;
+		unsigned int dw_s7;
+	};
+	unsigned int updated;
+} state3d_load_state_immediate_1_plb_t;
+/* FIXME: Need to refine to smaller enable flags */
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_UPDATED_PLB			1
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_HEADER_DWORD_COUNT_PLB		1
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_STATES_ALL_DWORD_COUNT_PLB	8
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_ZERO			1
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_ONE			2
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_SRC_ALPHA		5
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_INV_SRC_ALPHA	6
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_DST_ALPHA		7
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_INV_DST_ALPHA	8
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_CONST_ALPHA		0xe
+#define STATE3D_LOAD_STATE_IMMEDIATE_1_BLENDFACTOR_INV_CONST_ALPHA	0xf
+
+typedef struct _state3d_map_deinterlacer_parameters_plb {
+	union {
+		struct {
+			unsigned int da_en_y :1;
+			unsigned int da_en_uv :1;
+			unsigned int :30;
+		};
+		unsigned int dw1;
+	};
+	union {
+		struct {
+			unsigned int edge_threshold_low_y :8;
+			unsigned int edge_threshold_high_y :8;
+			unsigned int edge_threshold_low_uv :8;
+			unsigned int edge_threshold_high_uv :8;
+		};
+		unsigned int dw2;
+	};
+	unsigned int updated;
+} state3d_map_deinterlacer_parameters_plb_t;
+#define STATE3D_MAP_DEINTERLACER_PARAMETERS_UPDATED_PLB		1
+#define STATE3D_MAP_DEINTERLACER_PARAMETERS_DWORD_COUNT_PLB	3
+
+typedef struct _state3d_map_palette_load_32_plb {
+	union {
+		struct {
+			unsigned int color_num :4;
+			unsigned int :12;
+			unsigned int :16;
+		};
+		unsigned int dw0;
+	};
+	unsigned int color[16];
+	unsigned int updated;
+} state3d_map_palette_load_32_plb_t;
+#define STATE3D_MAP_PALETTE_LOAD_32_UPDATED_PLB		1
+#define STATE3D_MAP_PALETTE_LOAD_32_DWORD_COUNT_PLB	17
+
+typedef	struct _state3d_map_state_texmap_plb {
+		union {
+			struct {
+				unsigned int vert_stride_offset :1;
+				unsigned int vert_stride :1;
+				unsigned int base_addr :26;
+				unsigned int :3;
+			};
+			unsigned int tm_dw0;
+		};
+		union {
+			struct {
+				unsigned int tile_walk :1;
+				unsigned int tiled_surface :1;
+				unsigned int util_fence_regs :1;
+				unsigned int texel_format :4;
+				unsigned int surface_format :3;
+				unsigned int width :11;
+				unsigned int height :11;
+			};
+			unsigned int tm_dw1;
+		};
+		union {
+			struct {
+				unsigned int depth :8;
+				unsigned int :1;
+				unsigned int max_lod :6;
+				unsigned int cube_face_en :6;
+				unsigned int dword_pitch :11;
+			};
+			unsigned int tm_dw2;
+		};
+} state3d_map_state_texmap_plb_t;
+#define STATE3D_MAP_STATE_MAPSURF_8BIT_PLB		1
+  #define STATE3D_MAP_STATE_MAPSURF_8BIT_MAPTEXEL_I8_PLB		0
+  #define STATE3D_MAP_STATE_MAPSURF_8BIT_MAPTEXEL_L8_PLB		1
+  #define STATE3D_MAP_STATE_MAPSURF_8BIT_MAPTEXEL_A4P4_PLB		2
+  #define STATE3D_MAP_STATE_MAPSURF_8BIT_MAPTEXEL_P4A4_PLB		3
+  #define STATE3D_MAP_STATE_MAPSURF_8BIT_MAPTEXEL_A8_PLB		4
+  #define STATE3D_MAP_STATE_MAPSURF_8BIT_MAPTEXEL_MONO8_PLB		5
+#define STATE3D_MAP_STATE_MAPSURF_16BIT_PLB		2
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_R5G6B5_PLB		0
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_A1R5G5B5_PLB		1
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_A4R4G4B4_PLB		2
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_A8L8_PLB		3
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_V8U8_PLB		5
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_L6V5U5_PLB		6
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_I16_PLB		7
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_L16_PLB		8
+  #define STATE3D_MAP_STATE_MAPSURF_16BIT_MAPTEXEL_A16_PLB		9
+#define STATE3D_MAP_STATE_MAPSURF_32BIT_PLB		3
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_A8R8G8B8_PLB		0
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_A8B8G8R8_PLB		1
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_X8R8G8B8_PLB		2
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_X8B8G8R8_PLB		3
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_Q8W8V8U8_PLB		4
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_A8X8V8U8_PLB		5
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_L8X8V8U8_PLB		6
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_X8L8V8U8_PLB		7
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_A2R10G10B10_PLB	8
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_A2B10G10R10_PLB	9
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_A2W10V10U10_PLB	0xA
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_G16R16_PLB		0xB
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_V16U16_PLB		0xC
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_X8I24_PLB		0xD
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_X8L24_PLB		0xE
+  #define STATE3D_MAP_STATE_MAPSURF_32BIT_MAPTEXEL_X8A24_PLB		0xF
+  #define STATE3D_MAP_STATE_MAPSURF_422_PLB		5
+  #define STATE3D_MAP_STATE_MAPSURF_422_MAPTEXEL_YCRCB_SWAPY_PLB	0
+  #define STATE3D_MAP_STATE_MAPSURF_422_MAPTEXEL_YCRCB_NORMAL_PLB	1
+  #define STATE3D_MAP_STATE_MAPSURF_422_MAPTEXEL_YCRCB_SWAPUV_PLB	2
+  #define STATE3D_MAP_STATE_MAPSURF_422_MAPTEXEL_YCRCB_SWAPUVY_PLB	3
+  #define STATE3D_MAP_STATE_MAPSURF_COMPRESSED_PLB	6
+  #define STATE3D_MAP_STATE_MAPSURF_COMPRESSED_MAPTEXEL_DX1_PLB		0
+  #define STATE3D_MAP_STATE_MAPSURF_COMPRESSED_MAPTEXEL_DX2_3_PLB	1
+  #define STATE3D_MAP_STATE_MAPSURF_COMPRESSED_MAPTEXEL_DX4_5_PLB	2
+  #define STATE3D_MAP_STATE_MAPSURF_COMPRESSED_MAPTEXEL_FX1_PLB		3
+  #define STATE3D_MAP_STATE_MAPSURF_COMPRESSED_MAPTEXEL_DX1_RGB_PLB	4
+#define STATE3D_MAP_STATE_MAPSURF_4BIT_INDEXED_PLB	7
+  #define STATE3D_MAP_STATE_MAPSURF_4BIT_INDEXED_MAPTEXEL_A8R8G8B8_PLB	7
+typedef struct _state3d_map_state_plb {
+	union {
+		struct {
+			unsigned int map_num :6;
+			unsigned int :9;
+			unsigned int retain :1;
+			unsigned int :16;
+		};
+		unsigned int dw0;
+	};
+	union {
+		struct {
+			unsigned int map_mask :16;
+			unsigned int :16;
+		};
+		unsigned int dw1;
+	};
+	state3d_map_state_texmap_plb_t texmap[16]; /* Napa support 16 texture maps, B-spec-1a pg198 */
+	unsigned int updated;
+} state3d_map_state_plb_t;
+/* FIXME: Need to refine to smaller enable flags */
+#define STATE3D_MAP_STATE_UPDATED_PLB			1
+#define STATE3D_MAP_STATE_MAX_DWORD_COUNT_PLB		50
+#define STATE3D_MAP_STATE_HEADER_DWORD_COUNT_PLB	2
+#define STATE3D_MAP_STATE_SINGLE_TM_DWORD_COUNT_PLB	3
+
+typedef struct _state3d_modes_4_plb {
+	union {
+		struct {
+			unsigned int stencil_write_mask :8;
+			unsigned int stencil_test_mask :8;
+			unsigned int stencil_write_mask_mod_en :1;
+			unsigned int stencil_test_mask_mod_en :1;
+			unsigned int logic_op_func :4;
+			unsigned int :1;
+			unsigned int logic_op_func_mod_en :1;
+			unsigned int :8;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_modes_4_plb_t;
+#define STATE3D_MODES_4_UPDATED_PLB		1
+#define STATE3D_MODES_4_DWORD_COUNT_PLB		1
+
+typedef struct _state3d_modes_5_plb {
+	union {
+		struct {
+			unsigned int :16;
+			unsigned int pipelined_tex_cache_op :1;
+			unsigned int tex_cache_dis :1;
+			unsigned int pipelined_render_cache_op :1;
+			unsigned int :13;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_modes_5_plb_t;
+
+#define STATE3D_MODES_5_UPDATED_PLB		1
+#define STATE3D_MODES_5_DWORD_COUNT_PLB		1
+
+typedef struct _state3d_pixel_shader_const_elem_plb {
+	unsigned int const_x;
+	unsigned int const_y;
+	unsigned int const_z;
+	unsigned int const_w;
+} state3d_pixel_shader_const_elem_plb_t;
+
+typedef struct _state3d_pixel_shader_constants_plb {
+	union {
+		struct {
+			unsigned int const_num :8;
+			unsigned int :24;
+		};
+		unsigned int dw0;
+	};
+	union {
+		unsigned int reg_mask;
+		unsigned int dw1;
+	};
+	state3d_pixel_shader_const_elem_plb_t elem[32];
+	unsigned int updated;
+} state3d_pixel_shader_constants_plb_t;
+#define STATE3D_PIXEL_SHADER_CONSTANTS_UPDATED_PLB		1
+#define STATE3D_PIXEL_SHADER_CONSTANTS_HEADER_DWORD_COUNT_PLB	2
+#define STATE3D_PIXEL_SHADER_CONSTANTS_SINGLE_ELEM_DWORD_COUNT_PLB	4
+
+typedef struct _state3d_pixel_shader_program_ari_instr_plb {
+	union {
+		struct {
+			unsigned int :2;
+			unsigned int src0_reg_num :5;
+			unsigned int src0_reg_type :3;
+			unsigned int dest_channel_mask :4;
+			unsigned int dest_reg_num :4;
+			unsigned int :1;
+			unsigned int dest_reg_type :3;
+			unsigned int dest_saturate :1;
+			unsigned int :1;
+			unsigned int opcode :5;
+			unsigned int :3;
+		};
+		unsigned int dw0;
+	};
+	union {
+		struct {
+			unsigned int src1_y_channel_select :3;
+			unsigned int src1_y_channel_negate :1;
+			unsigned int src1_x_channel_select :3;
+			unsigned int src1_x_channel_negate :1;
+			unsigned int src1_reg_num :5;
+			unsigned int src1_reg_type :3;
+			unsigned int src0_w_channel_select :3;
+			unsigned int src0_w_channel_negate :1;
+			unsigned int src0_z_channel_select :3;
+			unsigned int src0_z_channel_negate :1;
+			unsigned int src0_y_channel_select :3;
+			unsigned int src0_y_channel_negate :1;
+			unsigned int src0_x_channel_select :3;
+			unsigned int src0_x_channel_negate :1;
+		};
+		unsigned int dw1;
+	};
+	union {
+		struct {
+			unsigned int src2_w_channel_select :3;
+			unsigned int src2_w_channel_negate :1;
+			unsigned int src2_z_channel_select :3;
+			unsigned int src2_z_channel_negate :1;
+			unsigned int src2_y_channel_select :3;
+			unsigned int src2_y_channel_negate :1;
+			unsigned int src2_x_channel_select :3;
+			unsigned int src2_x_channel_negate :1;
+			unsigned int src2_reg_num :5;
+			unsigned int src2_reg_type :3;
+			unsigned int src1_w_channel_select :3;
+			unsigned int src1_w_channel_negate :1;
+			unsigned int src1_z_channel_select :3;
+			unsigned int src1_z_channel_negate :1;
+		};
+		unsigned int dw2;
+	};
+
+	void * next_instr;
+	unsigned char ps_id;
+} state3d_pixel_shader_program_ari_instr_plb_t;
+
+typedef struct _state3d_pixel_shader_program_tex_instr_plb {
+	union {
+		struct {
+			unsigned int samp_reg_num :4;
+			unsigned int :10;
+			unsigned int dest_reg_num :4;
+			unsigned int :1;
+			unsigned int dest_reg_type :3;
+			unsigned int :2;
+			unsigned int opcode :5;
+			unsigned int :3;
+		};
+		unsigned int dw0;
+	};
+	union {
+		struct {
+			unsigned int :17;
+			unsigned int addr_reg_num :4;
+			unsigned int :3;
+			unsigned int addr_reg_type :3;
+			unsigned int :5;
+		};
+		unsigned int dw1;
+	};
+	unsigned int dw2;
+
+	void * next_instr;
+	unsigned char ps_id;
+} state3d_pixel_shader_program_tex_instr_plb_t;
+
+typedef struct _state3d_pixel_shader_program_dcl_instr_plb {
+	union {
+		struct {
+			unsigned int :10;
+			unsigned int dcl_channel_mask :4;
+			unsigned int dcl_reg_num :4;
+			unsigned int :1;
+			unsigned int dcl_reg_type :2;
+			unsigned int :1;
+			unsigned int samp_type :2;
+			unsigned int opcode :5;
+			unsigned int :3;
+		};
+		unsigned int dw0;
+	};
+	unsigned int dw1;
+	unsigned int dw2;
+
+	void * next_instr;
+	unsigned char ps_id;
+} state3d_pixel_shader_program_dcl_instr_plb_t;
+
+typedef struct _state3d_pixel_shader_program_instr_plb {
+	unsigned int idw0;	//instruction's first dword
+	unsigned int idw1;	//instruction's second dword
+	unsigned int idw2;	//instruction's third dword
+} state3d_pixel_shader_program_instr_plb_t;
+
+typedef struct _state3d_pixel_shader_program_plb {
+	union {
+		struct {
+			unsigned int instr_num :9;
+			unsigned int :6;
+			unsigned int retain :1;
+			unsigned int :16;
+		};
+		unsigned int dw0;
+	};
+	state3d_pixel_shader_program_instr_plb_t instr[123];
+	unsigned int updated;
+} state3d_pixel_shader_program_plb_t;
+
+/*
+#define STATE3D_PIXEL_SHADER_PROGRAM_ARI_INSTR_UPDATED_PLB	1
+#define STATE3D_PIXEL_SHADER_PROGRAM_TEX_INSTR_UPDATED_PLB	1
+#define STATE3D_PIXEL_SHADER_PROGRAM_DCL_INSTR_UPDATED_PLB	1
+*/
+#define STATE3D_PIXEL_SHADER_PROGRAM_HEADER_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_rasterization_rules_plb {
+	union {
+		struct {
+			unsigned int :3;
+			unsigned int tri_fan_provok_vtx_sel :2;
+			unsigned int tri_fan_provok_vtx_sel_mod_en :1;
+			unsigned int line_list_provok_vtx_sel :2;
+			unsigned int line_list_provok_vtx_sel_mod_en :1;
+			unsigned int texkill_3d4d :1;
+			unsigned int texkill_3d4d_mod_en :1;
+			unsigned int :1;
+			unsigned int snake_walk_dis :1;
+			unsigned int point_raster_rule :2;
+			unsigned int point_raster_rule_mod_en :1;
+			unsigned int zero_pixel_tri_filter_dis :1;
+			unsigned int zero_pixel_tri_filter_dis_mod_en :1;
+			unsigned int tri_2x2_filter_dis :1;
+			unsigned int tri_2x2_filter_dis_mod_en :1;
+			unsigned int :12;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_rasterization_rules_plb_t;
+#define STATE3D_RASTERIZATION_RULES_UPDATED_PLB		1
+#define STATE3D_RASTERIZATION_RULES_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_sampler_state_samp_plb {
+	union {
+		struct {
+			unsigned int shadow_func :3;
+			unsigned int max_anisotropy :1;
+			unsigned int shadow_en :1;
+			unsigned int tex_lod_bias :9;
+			unsigned int min_mode_filter :3;
+			unsigned int mag_mode_filter :3;
+			unsigned int mip_mode_filter :2;
+			unsigned int base_mip_level :5;
+			unsigned int chroma_index :2;
+			unsigned int color_space_conv_en :1;
+			unsigned int planar_to_packed_en :1;
+			unsigned int reverse_gamma_en :1;
+		};
+		unsigned int ts_dw0;
+	};
+	union {
+		struct {
+			unsigned int east_deinterlace_en:1;
+			unsigned int map_index:4;
+			unsigned int normalized_coord:1;
+			unsigned int tcz_addr_ctrl_mode:3;
+			unsigned int tcy_addr_ctrl_mode:3;
+			unsigned int tcx_addr_ctrl_mode:3;
+			unsigned int chroma_en:1;
+			unsigned int keyed_tex_filter_mode:1;
+			unsigned int kill_pixel_en:1;
+			unsigned int :6;
+			unsigned int min_lod:8;
+		};
+		unsigned int ts_dw1;
+	};
+	union {
+		unsigned int default_color;
+		unsigned int ts_dw2;
+	};
+} state3d_sampler_state_samp_plb_t;
+
+typedef struct _state3d_sampler_state_plb {
+	union {
+		struct {
+			unsigned int samp_num :6;
+			unsigned int :26;
+		};
+		unsigned int dw0;
+	};
+	union {
+		struct {
+			unsigned int samp_mask :16;
+			unsigned int :16;
+		};
+		unsigned int dw1;
+	};
+	state3d_sampler_state_samp_plb_t samp[16]; /* Napa support up to 16 sampler */
+	unsigned int updated;
+} state3d_sampler_state_plb_t;
+/* FIXME: Need to refine smaller enable flags */
+#define STATE3D_SAMPLER_STATE_UPDATED_PLB			1
+#define STATE3D_SAMPLER_STATE_MAX_DWORD_COUNT_PLB		50
+#define STATE3D_SAMPLER_STATE_HEADER_DWORD_COUNT_PLB		2
+#define STATE3D_SAMPLER_STATE_SINGLE_TS_DWORD_COUNT_PLB		3
+
+typedef struct _state3d_scissor_enable_plb {
+	union {
+		struct {
+			unsigned int enable :1;
+			unsigned int enable_mod_en :1;
+			unsigned int :30;
+		};
+		unsigned int dw0;
+	};
+	unsigned int updated;
+} state3d_scissor_enable_plb_t;
+#define STATE3D_SCISSOR_ENABLE_UPDATED_PLB	1
+#define STATE3D_SCISSOR_ENABLE_DWORD_COUNT_PLB	1
+
+typedef struct _state3d_scissor_rectangle_plb {
+	union {
+		struct {
+			unsigned int x_min :16;
+			unsigned int y_min :16;
+		};
+		unsigned int dw1;
+	};
+	union {
+		struct {
+			unsigned int x_max :16;
+			unsigned int y_max :16;
+		};
+		unsigned int dw2;
+	};
+	unsigned int updated;
+
+} state3d_scissor_rectangle_plb_t;
+#define STATE3D_SCISSOR_RECTANGLE_UPDATED_PLB		1
+#define STATE3D_SCISSOR_RECTANGLE_DWORD_COUNT_PLB	3
+
+typedef struct _state3d_span_stipple_plb {
+	union {
+		struct {
+			unsigned int pattern :16;
+			unsigned int enable :1;
+			unsigned int :15;
+		};
+		unsigned int dw1;
+	};
+	unsigned int updated;
+} state3d_span_stipple_plb_t;
+#define STATE3D_SPAN_STIPPLE_UPDATED_PLB	1
+#define STATE3D_SPAN_STIPPLE_DWORD_COUNT_PLB	2
+
+#define COLOR_BUFFER_INDEX	0
+#define DEPTH_BUFFER_INDEX	1
+#define AUX0_BUFFER_INDEX	2
+#define AUX1_BUFFER_INDEX	3
+#define INTRA_BUFFER_INDEX	4
+
+#define ZONE_INIT_CLEARPARAM_INDEX  0
+#define CLEAR_RECT_CLEARPARAM_INDEX 1
+
+typedef struct _state3d_plb {
+	igd_surface_t					color_buffer;
+	igd_surface_t					depth_buffer;
+	state3d_anti_aliasing_plb_t 			anti_alias;
+	state3d_backface_stencil_ops_plb_t 		bface_stencil_ops;
+	state3d_backface_stencil_masks_plb_t 		bface_stencil_masks;
+	state3d_bin_control_plb_t 			bin_control;
+/*
+	state3d_buffer_info_plb_t 			dest_buffer_info;
+	state3d_buffer_info_plb_t 			depth_buffer_info;
+*/
+	state3d_buffer_info_plb_t			buffer_info[5];
+	state3d_chroma_key_plb_t 			chroma_key[4];
+	state3d_clear_parameters_plb_t 			clear_param[2];
+	state3d_constant_blend_color_plb_t 		const_blend_color;
+	state3d_coord_set_bindings_plb_t 		coord_set_bind;
+	state3d_default_diffuse_plb_t 			default_diffuse;
+	state3d_default_specular_plb_t 			default_specular;
+	state3d_default_z_plb_t 			default_z;
+	state3d_depth_offset_scale_plb_t 		depth_offset_scale;
+	state3d_depth_subrectangle_enable_plb_t 	depth_subrect_en;
+	state3d_dest_buffer_variables_plb_t		dest_buf_vars;
+	state3d_drawing_rectangle_plb_t			draw_rect;
+	state3d_filter_coefficients_4x4_plb_t		filter_coef_4x4;
+	state3d_filter_coefficients_6x5_plb_t		filter_coef_6x5;
+	state3d_fog_color_plb_t				fog_color;
+	state3d_fog_mode_plb_t				fog_mode;
+	state3d_independent_alpha_blend_plb_t		indep_alpha_blend;
+	state3d_load_indirect_plb_t			load_indirect;
+	state3d_load_state_immediate_1_plb_t		lsi1;
+	state3d_map_deinterlacer_parameters_plb_t	map_deinterlace_param;
+	state3d_map_palette_load_32_plb_t		map_pal_load_32;
+	state3d_map_state_plb_t				map_state;
+	state3d_modes_4_plb_t				modes_4;
+	state3d_modes_5_plb_t				modes_5;
+	state3d_pixel_shader_constants_plb_t		pixel_shader_const;
+	state3d_pixel_shader_program_plb_t		pixel_shader_program;
+/*	state3d_pixel_shader_program_dcl_instr_plb_t	pixel_shader_dcl;
+	state3d_pixel_shader_program_ari_instr_plb_t	pixel_shader_ari;
+	state3d_pixel_shader_program_tex_instr_plb_t	pixel_shader_tex;*/
+	state3d_rasterization_rules_plb_t		raster_rules;
+	state3d_sampler_state_plb_t			sampler_state;
+	state3d_scissor_enable_plb_t			scissor_en;
+	state3d_scissor_rectangle_plb_t			scissor_rect;
+	state3d_span_stipple_plb_t			span_stipple;
+} state3d_plb_t;
+
+int state3d_update_plb(igd_command_t ** in, state3d_plb_t *state, int load_all);
+int state3d_update_size_plb(state3d_plb_t *state);
+
+#define STATE3D_PLB(ac) ((state3d_plb_t*)((appcontext_plb_t*)ac)->state3d)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/psb_regs.h b/drivers/gpu/drm/emgd/emgd/include/psb_regs.h
new file mode 100644
index 0000000..01edb09
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/psb_regs.h
@@ -0,0 +1,658 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: psb_regs.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the register definition file for the SGX and MSVDX cores.
+ *  These registers were formerly part of the plb/regs.h. They are taken out
+ *  because Atom E6xx shares the same core as PLB. This file is included in tnc/regs.h
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PSB_REGS_H_
+#define _PSB_REGS_H_
+
+extern unsigned long _sgx_base;
+/*-----------------------------------------------------------------------------
+ * SGX and MSVDX registers
+ ----------------------------------------------------------------------------*/
+#define SGX_BASE _sgx_base
+
+/* bits in PSB_EUR_CR_EVENT_STATUS */
+#define PSB_DPM_3D_MEM_FREE    (1<<0)
+#define PSB_OUT_OF_MEM_MT      (1<<1)
+#define PSB_OUT_OF_MEM_GBL     (1<<2)
+#define PSB_REACHED_MEM_THRESH (1<<3)
+#define PSB_TA_TERMINATE       (1<<12)
+#define PSB_TA_FINISHED        (1<<13)
+#define PSB_PIXELBE_END_RENDER (1<<18)
+#define PSB_DPM_TA_MEM_FREE    (1<<24)
+#define PSB_DPM_OUT_OF_MEM_ZLS (1<<25)
+#define PSB_TWOD_COMPLETE      (1<<27)
+#define PSB_TA_DPM_FAULT       (1<<28)
+
+/* PSB Registers */
+#define PSB_SGX_2D_CMD_SLAVE_PORT               (SGX_BASE + 0x4000)
+
+/* 0x000-0x0ff */
+/* CORE CLOCK GATING register definitions */
+#define PSB_CR_CLKGATECTL                       (SGX_BASE + 0x0000)
+#define _PSB_C_CLKGATECTL_CLKG_ENABLED          0x0
+#define _PSB_C_CLKGATECTL_CLKG_DISABLED         0x1
+#define _PSB_C_CLKGATECTL_CLKG_AUTO             0x2
+
+#define _PSB_C_CLKGATECTL_USE_CLKG_SHIFT        (20)
+#define _PSB_C_CLKGATECTL_DPM_CLKG_SHIFT        (16)
+#define _PSB_C_CLKGATECTL_TA_CLKG_SHIFT         (12)
+#define _PSB_C_CLKGATECTL_TSP_CLKG_SHIFT        (8)
+#define _PSB_C_CLKGATECTL_ISP_CLKG_SHIFT        (4)
+#define _PSB_C_CLKGATECTL_2D_CLKG_SHIFT         (0)
+
+#define _PSB_C_CLKGATECTL_USE_CLKG_MASK         (0x3 << 20)
+#define _PSB_C_CLKGATECTL_DPM_CLKG_MASK         (0x3 << 16)
+#define _PSB_C_CLKGATECTL_TA_CLKG_MASK          (0x3 << 12)
+#define _PSB_C_CLKGATECTL_TSP_CLKG_MASK         (0x3 << 8)
+#define _PSB_C_CLKGATECTL_ISP_CLKG_MASK         (0x3 << 4)
+#define _PSB_C_CLKGATECTL_2D_CLKG_MASK          (0x3 << 0)
+
+#define PSB_CR_CORE_ID                          (SGX_BASE + 0x0010)
+#define _PSB_CC_ID_ID_SHIFT                     (16)
+#define _PSB_CC_ID_ID_MASK                      (0xFFFF << 16)
+#define _PSB_CC_ID_CONFIG_SHIFT                 (0)
+#define _PSB_CC_ID_CONFIG_MASK                  (0xFFFF << 0)
+
+#define PSB_CR_CORE_REVISION                    (SGX_BASE + 0x0014)
+#define _PSB_CC_REVISION_DESIGNER_SHIFT         (24)
+#define _PSB_CC_REVISION_DESIGNER_MASK          (0xFF << 24)
+#define _PSB_CC_REVISION_MAJOR_SHIFT            (16)
+#define _PSB_CC_REVISION_MAJOR_MASK             (0xFF << 16)
+#define _PSB_CC_REVISION_MINOR_SHIFT            (8)
+#define _PSB_CC_REVISION_MINOR_MASK             (0xFF << 8)
+#define _PSB_CC_REVISION_MAINTENANCE_SHIFT      (0)
+#define _PSB_CC_REVISION_MAINTENANCE_MASK       (0xFF << 0)
+
+#define PSB_CR_SOFT_RESET						(SGX_BASE + 0x0080)
+#define _PSB_CS_RESET_TSP_RESET					(1 << 6)
+#define _PSB_CS_RESET_ISP_RESET					(1 << 5)
+#define _PSB_CS_RESET_USE_RESET					(1 << 4)
+#define _PSB_CS_RESET_TA_RESET					(1 << 3)
+#define _PSB_CS_RESET_DPM_RESET					(1 << 2)
+#define _PSB_CS_RESET_TWOD_RESET				(1 << 1)
+#define _PSB_CS_RESET_BIF_RESET					(1 << 0)
+
+/* 0x100-0x200 */
+
+#define PSB_CR_EVENT_HOST_ENABLE2               (SGX_BASE + 0x0110)
+#define PSB_CR_EVENT_HOST_CLEAR2                (SGX_BASE + 0x0114)
+#define PSB_CR_EVENT_STATUS2                    (SGX_BASE + 0x0118)
+#define PSB_TRIG_TA                             (1 << 7)
+#define PSB_TRIG_3D                             (1 << 6)
+#define PSB_TRIG_DL                             (1 << 5)
+#define PSB_BIF_REQ_FAULT                       (1 << 4)
+#define _PSB_CE_BIF_REQUESTER_FAULT             (1 << 4)
+#define _PSB_CE_DPM_DHOST_FREE_LOAD             (1 << 3)
+#define _PSB_CE_DPM_HOST_FREE_LOAD              (1 << 2)
+#define _PSB_CE_DPM_3D_FREE_LOAD                (1 << 1)
+#define _PSB_CE_DPM_TA_FREE_LOAD                (1 << 0)
+
+#define PSB_EUR_CR_EVENT_STATUS                 (SGX_BASE + 0x012c)
+
+#define PSB_CR_EVENT_STATUS                     (SGX_BASE + 0x012C)
+
+#define PSB_EUR_CR_EVENT_HOST_ENABLE            (SGX_BASE + 0x0130)
+
+#define PSB_CR_EVENT_HOST_CLEAR                 (SGX_BASE + 0x0134)
+#define _PSB_CE_MASTER_INTERRUPT                (1 << 31)
+#define _PSB_CE_TIMER                           (1 << 29)
+#define _PSB_CE_TA_DPM_FAULT                    (1 << 28)
+#define _PSB_CE_TWOD_COMPLETE                   (1 << 27)
+#define _PSB_CE_MADD_CACHE_INVALCOMPLETE        (1 << 26)
+#define _PSB_CE_DPM_OUT_OF_MEMORY_ZLS           (1 << 25)
+#define _PSB_CE_DPM_TA_MEM_FREE                 (1 << 24)
+#define _PSB_CE_ISP_END_TILE                    (1 << 23)
+#define _PSB_CE_DPM_INITEND                     (1 << 22)
+#define _PSB_CE_OTPM_LOADED                     (1 << 21)
+#define _PSB_CE_OTPM_INV                        (1 << 20)
+#define _PSB_CE_OTPM_FLUSHED                    (1 << 19)
+#define _PSB_CE_PIXELBE_END_RENDER              (1 << 18)
+#define _PSB_CE_ISP_HALT                        (1 << 17)
+#define _PSB_CE_ISP_VISIBILITY_FAIL             (1 << 16)
+#define _PSB_CE_BREAKPOINT                      (1 << 15)
+#define _PSB_CE_SW_EVENT                        (1 << 14)
+#define _PSB_CE_TA_FINISHED                     (1 << 13)
+#define _PSB_CE_TA_TERMINATE                    (1 << 12)
+#define _PSB_CE_TPC_CLEAR                       (1 << 11)
+#define _PSB_CE_TPC_FLUSH                       (1 << 10)
+#define _PSB_CE_DPM_CONTROL_CLEAR               (1 << 9)
+#define _PSB_CE_DPM_CONTROL_LOAD                (1 << 8)
+#define _PSB_CE_DPM_CONTROL_STORE               (1 << 7)
+#define _PSB_CE_DPM_STATE_CLEAR                 (1 << 6)
+#define _PSB_CE_DPM_STATE_LOAD                  (1 << 5)
+#define _PSB_CE_DPM_STATE_STORE                 (1 << 4)
+#define _PSB_CE_DPM_REACHED_MEM_THRESH          (1 << 3)
+#define _PSB_CE_DPM_OUT_OF_MEMORY_GBL           (1 << 2)
+#define _PSB_CE_DPM_OUT_OF_MEMORY_MT            (1 << 1)
+#define _PSB_CE_DPM_3D_MEM_FREE                 (1 << 0)
+
+#define PSB_CR_PDS_CACHE_STATUS                 (SGX_BASE + 0x0138)
+
+#define PSB_CR_PDS_CACHE_HOST_ENABLE            (SGX_BASE + 0x013C)
+
+#define PSB_CR_PDS_CACHE_HOST_CLEAR             (SGX_BASE + 0x0140)
+#define _PSB_CPC_DSC1_INV3                      (1 << 8)
+#define _PSB_CPC_DSC1_INV2                      (1 << 7)
+#define _PSB_CPC_DSC1_INV1                      (1 << 6)
+#define _PSB_CPC_DSC1_INV0                      (1 << 5)
+#define _PSB_CPC_DSC0_INV3                      (1 << 4)
+#define _PSB_CPC_DSC0_INV2                      (1 << 3)
+#define _PSB_CPC_DSC0_INV1                      (1 << 2)
+#define _PSB_CPC_DSC0_INV0                      (1 << 1)
+#define _PSB_CPC_CSC_INV                        (1 << 0)
+
+/* 0x200 - 0x300 */
+#define PSB_CR_VDM_START                        (SGX_BASE + 0x0200)
+#define _PSB_CV_START_PULSE                     (1 << 0)
+
+#define PSB_CR_TE_TPCCONTROL                    (SGX_BASE + 0x0224)
+#define _PSB_CT_TPCCONTROL_CLEAR                (1 << 31)
+#define _PSB_CT_TPCCONTROL_FLUSH                (1 << 30)
+
+#define PSB_CR_TE_RGNBBOX_X                     (SGX_BASE + 0x0228)
+#define _PSB_CT_RGNBOX_X_MAX_SHIFT              (16)
+#define _PSB_CT_RGNBOX_X_MAX_MASK               (0x1FF << 16)
+#define _PSB_CT_RGNBOX_X_MIN_SHIFT              (0)
+#define _PSB_CT_RGNBOX_X_MIN_MASK               (0x1FF << 0)
+
+#define PSB_CR_TE_RGNBBOX_Y                     (SGX_BASE + 0x022C)
+#define _PSB_CT_RGNBOX_Y_MAX_SHIFT              (16)
+#define _PSB_CT_RGNBOX_Y_MAX_MASK               (0x1FF << 16)
+#define _PSB_CT_RGNBOX_Y_MIN_SHIFT              (0)
+#define _PSB_CT_RGNBOX_Y_MIN_MASK               (0x1FF << 0)
+
+#define PSB_CR_MTE_OTPM_OP                      (SGX_BASE + 0x024C)
+#define _PSB_CMO_OP_CSM_FLUSH                   (1 << 2)
+#define _PSB_CMO_OP_CSM_LOAD                    (1 << 1)
+#define _PSB_CMO_OP_CSM_INV                     (1 << 0)
+
+#define PSB_CR_TE_DIAG1                         (SGX_BASE + 0x0280)
+#define _PSB_CT_DIAG1_PRERGNPRITILE_COUNT_MASK  (0xFFFFFFFF)
+
+#define PSB_CR_TE_DIAG2                         (SGX_BASE + 0x0284)
+#define _PSB_CT_DIAG2_POSTRGNPRITILE_COUNT_MASK (0xFFFFFFFF)
+
+#define PSB_CR_TE_DIAG3                         (SGX_BASE + 0x0288)
+#define _PSB_CT_DIAG3_PREPTEPRITILE_COUNT_MASK  (0xFFFFFFFF)
+
+#define PSB_CR_TE_DIAG4                         (SGX_BASE + 0x028C)
+#define _PSB_CT_DIAG4_POSTPTEPRITILE_COUNT_MASK (0xFFFFFFFF)
+
+#define PSB_CR_TE_DIAG5                         (SGX_BASE + 0x0290)
+#define _PSB_CT_DIAG5_PSGCTLDWORD_COUNT_MASK    (0xFFFFFFFF)
+
+#define PSB_CR_TE_DIAG6                         (SGX_BASE + 0x0294)
+#define _PSB_CT_DIAG6_PSGRGNHDR_COUNT_MASK      (0xFFFF << 0)
+
+#define PSB_CR_TE_DIAG7                         (SGX_BASE + 0x0298)
+#define _PSB_CT_DIAG7_TPCREADREQ_COUNT_SHIFT    (16)
+#define _PSB_CT_DIAG7_TPCREADREQ_COUNT_MASK     (0xFFFF << 16)
+#define _PSB_CT_DIAG7_TPCREADHIT_COUNT_SHIFT    (0)
+#define _PSB_CT_DIAG7_TPCREADHIT_COUNT_MASK     (0xFFFF << 0)
+
+#define PSB_CR_TE_DIAG8                         (SGX_BASE + 0x029C)
+#define _PSB_CT_DIAG8_TPCSTATEINV_COUNT_SHIFT   (16)
+#define _PSB_CT_DIAG8_TPCSTATEINV_COUNT_MASK    (0xFFFF << 16)
+#define _PSB_CT_DIAG8_TPCWRITEHIT_COUNT_SHIFT   (0)
+#define _PSB_CT_DIAG8_TPCWRITEHIT_COUNT_MASK    (0xFFFF << 0)
+
+/* 0x400 - 0x500 */
+#define PSB_CR_ISP_START_RENDER                 (SGX_BASE + 0x0428)
+#define _PSB_CI_START_RENDER_PULSE              (1 << 0)
+
+#define PSB_CR_TSP_PARAMETER_CACHE              (SGX_BASE + 0x043C)
+#define _PSB_CTP_CACHE_INVALIDATE               (1 << 0)
+
+/* 0x600 - 0x700 */
+
+#define PSB_CR_DPM_3D_PAGE_TABLE_BASE           (SGX_BASE + 0x0600)
+#define _PSB_CD3PT_BASE_ADDR_SHIFT              (4)//128-bit aligned address
+
+#define PSB_CR_DPM_3D_FREE_LIST                 (SGX_BASE + 0x0604)
+#define _PSB_CD3F_LIST_TAIL_SHIFT               (16)
+#define _PSB_CD3F_LIST_TAIL_MASK                (0xFFFF << 16)
+#define _PSB_CD3F_LIST_HEAD_SHIFT               (0)
+#define _PSB_CD3F_LIST_HEAD_MASK                (0xFFFF << 0)
+
+#define PSB_CR_DPM_HOST_DALLOC_PAGE_TABLE_BASE  (SGX_BASE + 0x0608)
+#define _PSB_CDHDPT_BASE_ADDR_SHIFT             (4)//128-bit aligned address
+
+#define PSB_CR_DPM_HOST_DALLOC_FREE_LIST        (SGX_BASE + 0x060C)
+#define _PSB_CDHDF_LIST_TAIL_SHIFT              (16)
+#define _PSB_CDHDF_LIST_TAIL_MASK               (0xFFFF << 16)
+#define _PSB_CDHDF_LIST_HEAD_SHIFT              (0)
+#define _PSB_CDHDF_LIST_HEAD_MASK               (0xFFFF << 0)
+
+#define PSB_CR_DPM_HOST_ALLOC_PAGE_TABLE_BASE   (SGX_BASE + 0x0610)
+#define _PSB_CDHAPT_BASE_ADDR_SHIFT             (4)//128-bit aligned address
+
+#define PSB_CR_DPM_HOST_ALLOC_FREE_LIST         (SGX_BASE + 0x0614)
+#define _PSB_CDHAF_LIST_TAIL_SHIFT              (16)
+#define _PSB_CDHAF_LIST_TAIL_MASK               (0xFFFF << 16)
+#define _PSB_CDHAF_LIST_HEAD_SHIFT              (0)
+#define _PSB_CDHAF_LIST_HEAD_MASK               (0xFFFF << 0)
+
+#define PSB_CR_DPM_TA_ALLOC_PAGE_TABLE_BASE     (SGX_BASE + 0x0618)
+#define _PSB_CDTAPT_BASE_ADDR_SHIFT             (4)//128-bit aligned address
+
+#define PSB_CR_DPM_TA_ALLOC_FREE_LIST           (SGX_BASE + 0x061C)
+#define _PSB_CDTAF_LIST_TAIL_SHIFT              (16)
+#define _PSB_CDTAF_LIST_TAIL_MASK               (0xFFFF << 16)
+#define _PSB_CDTAF_LIST_HEAD_SHIFT              (0)
+#define _PSB_CDTAF_LIST_HEAD_MASK               (0xFFFF << 0)
+
+
+#define PSB_CR_DPM_TA_PAGE_THRESHOLD            (SGX_BASE + 0x0620)
+#define _PSB_CDTP_THRESHOLD_VALUE_SHIFT         (0)
+#define _PSB_CDTP_THRESHOLD_VALUE_MASK          (0xFFFF << 0)
+
+#define PSB_CR_DPM_ZLS_PAGE_THRESHOLD           (SGX_BASE + 0x0624)
+#define _PSB_CDZP_THRESHOLD_VALUE_SHIFT         (0)
+#define _PSB_CDZP_THRESHOLD_VALUE_MASK          (0xFFFF << 0)
+
+#define PSB_CR_DPM_TA_GLOBAL_LIST               (SGX_BASE + 0x0628)
+#define _PSB_CDTG_LIST_POLICY                   (1 << 16)
+#define _PSB_CDTG_LIST_SIZE_SHIFT               (0)
+#define _PSB_CDTG_LIST_SIZE_MASK                (0xFFFF << 0)
+
+#define PSB_CR_DPM_STATE_CONTEXT_ID             (SGX_BASE + 0x0630)
+#define _PSB_CDSC_ID_ALLOC                      (1 << 2)
+#define _PSB_CDSC_ID_DALLOC                     (1 << 1)
+#define _PSB_CDSC_ID_LS                         (1 << 0)
+
+#define PSB_CR_DPM_3D_DEALLOCATE                (SGX_BASE + 0x063C)
+#define _PSB_CD3_DEALLOCATE_ENABLE              (1 << 1)
+#define _PSB_CD3_DEALLOCATE_GLOBAL              (1 << 0)
+
+#define PSB_CR_DPM_ALLOC                        (SGX_BASE + 0x0640)
+#define _PSB_CD_ALLOC_PAGE_OUTOFMEMORY          (1 << 17)
+#define _PSB_CD_ALLOC_PAGE_VALID                (1 << 16)
+#define _PSB_CD_ALLOC_PAGE_SHIFT                (0)
+#define _PSB_CD_ALLOC_PAGE_MASK                 (0xFFFF << 0)
+
+#define PSB_CR_DPM_DALLOC                       (SGX_BASE + 0x0644)
+#define _PSB_CD_DALLOC_PAGE_FREE                (1 << 16)
+#define _PSB_CD_DALLOC_PAGE_SHIFT               (0)
+#define _PSB_CD_DALLOC_PAGE_MASK                (0xFFFF << 0)
+
+#define PSB_CR_DPM_TA_ALLOC                     (SGX_BASE + 0x0648)
+#define _PSB_CDT_ALLOC_FREE_LIST_PREVIOUS_SHIFT (16)
+#define _PSB_CDT_ALLOC_FREE_LIST_PREVIOUS_MASK  (0xFFFF << 16)
+
+#define PSB_CR_DPM_3D                           (SGX_BASE + 0x064C)
+#define _PSB_CD_3D_FREE_LIST_PREVIOUS_SHIFT     (16)
+#define _PSB_CD_3D_FREE_LIST_PREVIOUS_MASK      (0xFFFF << 16)
+
+#define PSB_CR_DPM_HOST_DALLOC                  (SGX_BASE + 0x0650)
+#define _PSB_CDH_DALLOC_FREE_LIST_PREVIOUS_SHIFT (16)
+#define _PSB_CDH_DALLOC_FREE_LIST_PREVIOUS_MASK (0xFFFF << 16)
+
+#define PSB_CR_DPM_HOST_ALLOC                   (SGX_BASE + 0x0654)
+#define _PSB_CDH_ALLOC_FREE_LIST_PREVIOUS_SHIFT (16)
+#define _PSB_CDH_ALLOC_FREE_LIST_PREVIOUS_MASK  (0xFFFF << 16)
+
+#define PSB_CR_DPM_TASK_3D_FREE                 (SGX_BASE + 0x0680)
+#define _PSB_CDT3_FREE_LOAD                     (1 << 0)
+
+#define PSB_CR_DPM_TASK_TA_FREE                 (SGX_BASE + 0x0684)
+#define _PSB_CDTT_FREE_LOAD                     (1 << 0)
+
+#define PSB_CR_DPM_TASK_HOST_FREE               (SGX_BASE + 0x0688)
+#define _PSB_CDTH_FREE_LOAD                     (1 << 0)
+
+#define PSB_CR_DPM_TASK_DHOST_FREE              (SGX_BASE + 0x0690)
+#define _PSB_CDTD_FREE_LOAD                     (1 << 0)
+
+#define PSB_CR_DPM_TASK_STATE                   (SGX_BASE + 0x0694)
+#define _PSB_CDT_STATE_LOAD                     (1 << 2)
+#define _PSB_CDT_STATE_CLEAR                    (1 << 1)
+#define _PSB_CDT_STATE_STORE                    (1 << 0)
+
+#define PSB_CR_DPM_TASK_CONTROL                 (SGX_BASE + 0x0698)
+#define _PSB_CDT_CONTROL_LOAD                   (1 << 2)
+#define _PSB_CDT_CONTROL_CLEAR                  (1 << 1)
+#define _PSB_CDT_CONTROL_STORE                  (1 << 0)
+
+#define PSB_CR_DPM_OUTOFMEM                     (SGX_BASE + 0x069C)
+#define _PSB_CD_OUTOFMEM_ABORTALL               (1 << 2)
+#define _PSB_CD_OUTOFMEM_ABORT                  (1 << 1)
+#define _PSB_CD_OUTOFMEM_RESTART                (1 << 0)
+
+#define PSB_CR_DPM_FREE_CONTEXT                 (SGX_BASE + 0x06A0)
+#define _PSB_CDF_CONTEXT_NOW                    (1 << 0)
+
+#define PSB_CR_DPM_3D_TIMEOUT                   (SGX_BASE + 0x06A4)
+#define _PSB_CD3_TIMEOUT_NOW                    (1 << 0)
+
+#define PSB_CR_DPM_TA_EVM                       (SGX_BASE + 0x06A8)
+#define _PSB_CDT_EVM_INIT                       (1 << 0)
+
+/* 0x700 - 0x800 */
+
+#define PSB_CR_DPM_HOST_DALLOC_FREE_LIST_STATUS1 (SGX_BASE + 0x0708)
+#define _PSB_CDHDFL_STATUS1_TAIL_SHIFT          (16)
+#define _PSB_CDHDFL_STATUS1_TAIL_MASK           (0xFFFF << 16)
+#define _PSB_CDHDFL_STATUS1_HEAD_SHIFT          (0)
+#define _PSB_CDHDFL_STATUS1_HEAD_MASK           (0xFFFF << 0)
+
+#define PSB_CR_DPM_PAGE_STATUS                  (SGX_BASE + 0x0724)
+#define _PSB_CDP_STATUS_TA_SHIFT                (16)
+#define _PSB_CDP_STATUS_TA_MASK                 (0xFFFF << 16)
+#define _PSB_CDP_STATUS_TOTAL_SHIFT             (0)
+#define _PSB_CDP_STATUS_TOTAL_MASK              (0xFFFF << 0)
+
+#define PSB_CR_DPM_GLOBAL_PAGE_STATUS           (SGX_BASE + 0x072C)
+#define _PSB_CDGP_STATUS_TA_SHIFT               (16)
+#define _PSB_CDGP_STATUS_TA_MASK                (0xFFFF << 16)
+#define _PSB_CDGP_STATUS_TOTAL_SHIFT            (0)
+#define _PSB_CDGP_STATUS_TOTAL_MASK             (0xFFFF << 0)
+
+#define PSB_CR_CACHE_CTRL                       (SGX_BASE + 0x0804)
+#define _PSB_CC_CTRL_L0P1OFF                    (1 << 30)
+#define _PSB_CC_CTRL_L0P0OFF                    (1 << 29)
+#define _PSB_CC_CTRL_INVALIDATE                 (1 << 28)
+#define _PSB_CC_CTRL_L1P1OFF                    (1 << 26)
+#define _PSB_CC_CTRL_L1P0OFF                    (1 << 25)
+#define _PSB_CC_CTRL_L2OFF                      (1 << 24)
+#define _PSB_CC_CTRL_PARTDM3_SHIFT              (12)
+#define _PSB_CC_CTRL_PARTDM3_MASK               (0xF << 12)
+#define _PSB_CC_CTRL_PARTDM2_SHIFT              (8)
+#define _PSB_CC_CTRL_PARTDM2_MASK               (0xF << 8)
+#define _PSB_CC_CTRL_PARTDM1_SHIFT              (4)
+#define _PSB_CC_CTRL_PARTDM1_MASK               (0xF << 4)
+#define _PSB_CC_CTRL_PARTDM0_SHIFT              (0)
+#define _PSB_CC_CTRL_PARTDM0_MASK               (0xF << 0)
+
+/* 0xa00 - 0xb00 */
+#define PSB_CR_USE_CACHE                        (SGX_BASE + 0x0A08)
+#define _PSB_CU_CACHE_INVALIDATE                (1 << 0)
+
+/* USSE register definitions */
+
+/* USSE/PDS - 0xA00-0xBFF */
+
+#define PSB_CR_USE_CTRL                         (SGX_BASE + 0x0A00)
+#define _PSB_CU_CTRL_REGBOUND_ZERO              (1 << 19)
+#define _PSB_CU_CTRL_REGBOUND_ZERO_R1           (0 << 19)
+#define _PSB_CU_CTRL_INSTLIMIT_SHIFT            (14)
+#define _PSB_CU_CTRL_INSTLIMIT_MASK             (0x1F << 14)
+#define _PSB_CU_CTRL_ST_TTE_SCALE_SHIFT         (12)
+#define _PSB_CU_CTRL_ST_TTE_SCALE_MASK          (0x3 << 12)
+#define _PSB_CU_CTRL_CACHE_TTE_SHIFT            (4)
+#define _PSB_CU_CTRL_CACHE_TTE_MASK             (0xFF << 4)
+#define _PSB_CU_CTRL_KILL_MODE_SHIFT            (2)
+#define _PSB_CU_CTRL_KILL_MODE_MASK             (0x3 << 2)
+#define _PSB_CU_CTRL_KILL_MODE_OFF              (0 << 2)
+#define _PSB_CU_CTRL_KILL_MODE_FLUSH            (1 << 2)
+#define _PSB_CU_CTRL_KILL_MODE_KILL             (2 << 2)
+#define _PSB_CU_CTRL_KILL_DM_SHIFT              (0)
+#define _PSB_CU_CTRL_KILL_DM_MASK               (0x3 << 0)
+
+
+#define PSB_CR_USE_CODE_BASE(i)                 (SGX_BASE + (0x0A0C + ((i) << 2)))
+#define _PSB_CUC_BASE_DM_SHIFT                  (25)
+#define _PSB_CUC_BASE_DM_MASK                   (0x3 << 25)
+#define _PSB_CUC_BASE_ADDR_SHIFT                (0) // 1024-bit aligned address?
+#define _PSB_CUC_BASE_ADDR_ALIGNSHIFT           (7)
+#define _PSB_CUC_BASE_ADDR_MASK                 (0x1FFFFFF << 0)
+#define _PSB_CUC_DM_VERTEX                      (0)
+#define _PSB_CUC_DM_PIXEL                       (1)
+#define _PSB_CUC_DM_RESERVED                    (2)
+#define _PSB_CUC_DM_EDM                         (3)
+
+#define SGX_MAX_USSE_THRDS                      16
+#define PSB_USE_OFFSET_MASK                     0x0007FFFF
+#define PSB_USE_OFFSET_SIZE                     (SGX_USE_OFFSET_MASK + 1)
+#define PSB_CR_USE_CODE_BASE0                   (SGX_BASE + 0x0A0C)
+#define PSB_CR_USE_CODE_BASE1                   (SGX_BASE + 0x0A10)
+#define PSB_CR_USE_CODE_BASE2                   (SGX_BASE + 0x0A14)
+#define PSB_CR_USE_CODE_BASE3                   (SGX_BASE + 0x0A18)
+#define PSB_CR_USE_CODE_BASE4                   (SGX_BASE + 0x0A1C)
+#define PSB_CR_USE_CODE_BASE5                   (SGX_BASE + 0x0A20)
+#define PSB_CR_USE_CODE_BASE6                   (SGX_BASE + 0x0A24)
+#define PSB_CR_USE_CODE_BASE7                   (SGX_BASE + 0x0A28)
+#define PSB_CR_USE_CODE_BASE8                   (SGX_BASE + 0x0A2C)
+#define PSB_CR_USE_CODE_BASE9                   (SGX_BASE + 0x0A30)
+#define PSB_CR_USE_CODE_BASE10                  (SGX_BASE + 0x0A34)
+#define PSB_CR_USE_CODE_BASE11                  (SGX_BASE + 0x0A38)
+#define PSB_CR_USE_CODE_BASE12                  (SGX_BASE + 0x0A3C)
+#define PSB_CR_USE_CODE_BASE13                  (SGX_BASE + 0x0A40)
+#define PSB_CR_USE_CODE_BASE14                  (SGX_BASE + 0x0A44)
+#define PSB_CR_USE_CODE_BASE15                  (SGX_BASE + 0x0A48)
+
+#define PSB_CR_EVENT_PDS_ENABLE                 (SGX_BASE + 0x0A58)
+#define _PSB_CEP_ENABLE_TIMER                   (1 << 29)
+#define _PSB_CEP_ENABLE_TA_DPM_FAULT            (1 << 28)
+#define _PSB_CEP_ENABLE_TWOD_COMPLETE           (1 << 27)
+#define _PSB_CEP_ENABLE_MADD_CACHE_INVALCOMPLETE (1 << 26)
+#define _PSB_CEP_ENABLE_DPM_OUT_OF_MEMORY_ZLS   (1 << 25)
+#define _PSB_CEP_ENABLE_DPM_TA_MEM_FREE         (1 << 24)
+#define _PSB_CEP_ENABLE_ISP_END_PASS            (1 << 23)
+#define _PSB_CEP_ENABLE_DPM_INITEND             (1 << 22)
+#define _PSB_CEP_ENABLE_OTPM_LOADED             (1 << 21)
+#define _PSB_CEP_ENABLE_OTPM_INV                (1 << 20)
+#define _PSB_CEP_ENABLE_OTPM_FLUSHED            (1 << 19)
+#define _PSB_CEP_ENABLE_PIXELBE_END_RENDER      (1 << 18)
+#define _PSB_CEP_ENABLE_ISP_HALT                (1 << 17)
+#define _PSB_CEP_ENABLE_ISP_VISIBILITY_FAIL     (1 << 16)
+#define _PSB_CEP_ENABLE_BREAKPOINT              (1 << 15)
+#define _PSB_CEP_ENABLE_SW_EVENT                (1 << 14)
+#define _PSB_CEP_ENABLE_TA_FINISHED             (1 << 13)
+#define _PSB_CEP_ENABLE_TA_TERMINATE            (1 << 12)
+#define _PSB_CEP_ENABLE_TPC_CLEAR               (1 << 11)
+#define _PSB_CEP_ENABLE_TPC_FLUSH               (1 << 10)
+#define _PSB_CEP_ENABLE_DPM_CONTROL_CLEAR       (1 << 9)
+#define _PSB_CEP_ENABLE_DPM_CONTROL_LOAD        (1 << 8)
+#define _PSB_CEP_ENABLE_DPM_CONTROL_STORE       (1 << 7)
+#define _PSB_CEP_ENABLE_DPM_STATE_CLEAR         (1 << 6)
+#define _PSB_CEP_ENABLE_DPM_STATE_LOAD          (1 << 5)
+#define _PSB_CEP_ENABLE_DPM_STATE_STORE         (1 << 4)
+#define _PSB_CEP_ENABLE_DPM_REACHED_MEM_THRESH  (1 << 3)
+#define _PSB_CEP_ENABLE_DPM_OUT_OF_MEMORY_GBL   (1 << 2)
+#define _PSB_CEP_ENABLE_DPM_OUT_OF_MEMORY_MT    (1 << 1)
+#define _PSB_CEP_ENABLE_DPM_3D_MEM_FREE         (1 << 0)
+
+#define PSB_CR_DMS_CTRL                         (SGX_BASE + 0x0A74)
+#define _PSB_CD_CTRL_MAX_NUM_VERTEX_PARTITIONS_SHIFT (25)
+#define _PSB_CD_CTRL_MAX_NUM_VERTEX_PARTITIONS_MASK  (0x7 << 25)
+#define _PSB_CD_CTRL_MAX_NUM_PIXEL_PARTITIONS_SHIFT  (22)
+#define _PSB_CD_CTRL_MAX_NUM_PIXEL_PARTITIONS_MASK   (0x7 << 22)
+#define _PSB_CD_CTRL_MAX_NUM_EDM_TASKS_SHIFT    (16)
+#define _PSB_CD_CTRL_MAX_NUM_EDM_TASKS_MASK     (0x3F << 16)
+#define _PSB_CD_CTRL_MAX_NUM_VERTEX_TASKS_SHIFT (10)
+#define _PSB_CD_CTRL_MAX_NUM_VERTEX_TASKS_MASK  (0x3F << 10)
+#define _PSB_CD_CTRL_MAX_NUM_PIXEL_TASKS_SHIFT  (4)
+#define _PSB_CD_CTRL_MAX_NUM_PIXEL_TASKS_MASK   (0x3F << 4)
+#define _PSB_CD_CTRL_DISABLE_DM_SHIFT           (0)
+#define _PSB_CD_CTRL_DISABLE_DM_MASK            (0xF << 0)
+#define _PSB_CD_CTRL_DISABLE_DM_VERTEX          (1 << 0)
+#define _PSB_CD_CTRL_DISABLE_DM_PIXEL           (1 << 1)
+#define _PSB_CD_CTRL_DISABLE_DM_EVENT           (1 << 2)
+#define _PSB_CD_CTRL_DISABLE_DM_LOOPBACK        (1 << 3)
+
+#define PSB_CR_DMS_AGE                          (SGX_BASE + 0x0A78)
+#define _PSB_CD_AGE_ENABLE                      (1 << 16)
+#define _PSB_CD_AGE_VALUE_SHIFT                 (0)
+#define _PSB_CD_AGE_VALUE_MASK                  (0xFFFF << 0)
+
+#define PSB_CR_USE_G0                           (SGX_BASE + 0x0A7C)
+#define _PSB_CU_G0_VALUE_SHIFT                  (0)
+#define _PSB_CU_G0_VALUE_MASK                   (0xFF << 0)
+
+#define PSB_CR_USE_G1                           (SGX_BASE + 0x0A80)
+#define _PSB_CU_G1_VALUE_SHIFT                  (0)
+#define _PSB_CU_G1_VALUE_MASK                   (0xFF << 0)
+
+#define PSB_CR_PDS_EXEC_BASE                    (SGX_BASE + 0x0AB8)
+#define _PSB_CR_PDS_EXEC_BASE_ADDR_SHIFT        (20)/* 1MB aligned address */
+#define _PSB_CR_PDS_EXEC_BASE_ADDR_ALIGNSHIFT   (20)
+
+#define PSB_CR_USE0_DM_SLOT                     (SGX_BASE + 0x0AA4)
+
+#define PSB_CR_USE1_DM_SLOT                     (SGX_BASE + 0x0AA8)
+
+#define PSB_CR_USE_TMPREG                       (SGX_BASE + 0x0AAC)
+#define _PSB_CU_TMPREG_INIT_SHIFT               (3)
+#define _PSB_CU_TMPREG_INIT_MASK                (0x1F << 3)
+#define _PSB_CU_TMPREG_SIZE_SHIFT               (0)
+#define _PSB_CU_TMPREG_SIZE_MASK                (0x7 << 0)
+
+#define PSB_CR_EVENT_TIMER                      (SGX_BASE + 0x0ACC)
+#define _PSB_CE_TIMER_ENABLE                    (1 << 24)
+#define _PSB_CE_TIMER_VALUE_SHIFT               (0)
+#define _PSB_CE_TIMER_VALUE_MASK                (0xFFFFFF << 0)
+
+#define PSB_CR_PDS                              (SGX_BASE + 0x0ABC)
+#define _PSB_C_PDS_DOUT_TIMEOUT_DISABLE         (1 << 6)
+#define _PSB_C_PDS_ATTRIBUTE_CHUNK_START_SHIFT  (0)
+#define _PSB_C_PDS_ATTRIBUTE_CHUNK_START_MASK   (0x3F << 0)
+
+#define PSB_CR_PDS_INV0                         (SGX_BASE + 0x0AD0)
+
+#define PSB_CR_PDS_INV1                         (SGX_BASE + 0x0AD4)
+
+#define PSB_CR_PDS_INV2                         (SGX_BASE + 0x0AD8)
+
+#define PSB_CR_PDS_INV3                         (SGX_BASE + 0x0ADC)
+#define _PSB_CP_INV_DSC                         (1 << 0)
+
+#define PSB_CR_PDS_INV_CSC                      (SGX_BASE + 0x0AE0)
+#define _PSB_CPI_CSC_KICK                       (1 << 0)
+
+/* 0xb00 - 0xc00 */
+#define PSB_CR_USE0_SERV_PIXEL                  (SGX_BASE + 0x0B08)
+#define _PSB_CUS_PIXEL_EMPTY                    (1 << 16)
+#define _PSB_CUS_PIXEL_COUNT_SHIFT              (0)
+#define _PSB_CUS_PIXEL_COUNT_MASK               (0xFFFF << 0)
+
+#define PSB_CR_USE0_SERV_VERTEX                 (SGX_BASE + 0x0B0C)
+#define _PSB_CUS_VERTEX_EMPTY                   (1 << 16)
+#define _PSB_CUS_VERTEX_COUNT_SHIFT             (0)
+#define _PSB_CUS_VERTEX_COUNT_MASK              (0xFFFF << 0)
+
+#define PSB_CR_USE0_SERV_EVENT                  (SGX_BASE + 0x0B10)
+#define _PSB_CUS_EVENT_EMPTY                    (1 << 16)
+#define _PSB_CUS_EVENT_COUNT_SHIFT              (0)
+#define _PSB_CUS_EVENT_COUNT_MASK               (0xFFFF << 0)
+
+#define PSB_CR_USE1_SERV_PIXEL                  (SGX_BASE + 0x0B14)
+
+#define PSB_CR_USE1_SERV_VERTEX                 (SGX_BASE + 0x0B18)
+
+#define PSB_CR_USE1_SERV_EVENT                  (SGX_BASE + 0x0B1C)
+
+/* 0xc00 - 0xd00 */
+/* BIF register definitions */
+#define PSB_CR_BIF_CTRL                         (SGX_BASE + 0x0C00)
+#define _PSB_CB_CTRL_CLEAR_FAULT                (1 << 4)
+#define _PSB_CB_CTRL_INVALDC                    (1 << 3)
+#define _PSB_CB_CTRL_FLUSH                      (1 << 2)
+#define _PSB_MMU_ER_MASK                        0x0001FF00
+#define _PSB_MMU_ER_HOST                        (1 << 16)
+
+#define PSB_CR_BIF_INT_STAT                     (SGX_BASE + 0x0C04)
+
+#define PSB_CR_BIF_FAULT                        (SGX_BASE + 0x0C08)
+#define _PSB_CBI_STAT_PF_N_RW                   (1 << 14)
+#define _PSB_CBI_STAT_FAULT_SHIFT               (0)
+#define _PSB_CBI_STAT_FAULT_MASK                (0x3FFF << 0)
+#define _PSB_CBI_STAT_FAULT_CACHE               (1 << 1)
+#define _PSB_CBI_STAT_FAULT_TA                  (1 << 2)
+#define _PSB_CBI_STAT_FAULT_VDM                 (1 << 3)
+#define _PSB_CBI_STAT_FAULT_2D                  (1 << 4)
+#define _PSB_CBI_STAT_FAULT_PBE                 (1 << 5)
+#define _PSB_CBI_STAT_FAULT_TSP                 (1 << 6)
+#define _PSB_CBI_STAT_FAULT_ISP                 (1 << 7)
+#define _PSB_CBI_STAT_FAULT_USSEPDS             (1 << 8)
+#define _PSB_CBI_STAT_FAULT_HOST                (1 << 9)
+
+#define PSB_CR_BIF_BANK_SET                     (SGX_BASE + 0x0C74)
+#define _PSB_CBB_SET_SELECT_SHIFT               (0)
+#define _PSB_CBB_SET_SELECT_MASK                (0x3FF << 0)
+#define _PSB_CBB_SET_SELECT_DPM_TA              (0 << 9)
+#define _PSB_CBB_SET_SELECT_DPM_3D              (1 << 9)
+#define _PSB_CBB_SET_SELECT_EDM_B0              (0 << 8)
+#define _PSB_CBB_SET_SELECT_EDM_B1              (1 << 8)
+#define _PSB_CBB_SET_SELECT_TA_B0               (0 << 6)
+#define _PSB_CBB_SET_SELECT_TA_B0B1             (1 << 6)
+#define _PSB_CBB_SET_SELECT_TA_B1B0             (2 << 6)
+#define _PSB_CBB_SET_SELECT_TA_B1               (3 << 6)
+#define _PSB_CBB_SET_SELECT_HOST_B0             (0 << 4)
+#define _PSB_CBB_SET_SELECT_HOST_B1             (3 << 4)
+#define _PSB_CBB_SET_SELECT_3D_B0               (0 << 2)
+#define _PSB_CBB_SET_SELECT_3D_B0B1             (1 << 2)
+#define _PSB_CBB_SET_SELECT_3D_B1B0             (2 << 2)
+#define _PSB_CBB_SET_SELECT_3D_B1               (3 << 2)
+#define _PSB_CBB_SET_SELECT_2D_B0               (0 << 0)
+#define _PSB_CBB_SET_SELECT_2D_B1               (3 << 0)
+
+#define PSB_CR_BIF_BANK0                        (SGX_BASE + 0x0C78)
+#define PSB_CR_BIF_BANK1                        (SGX_BASE + 0x0C7C)
+
+#define _PSB_CB_BANK_INDEX_2D_SHIFT             (16)
+#define _PSB_CB_BANK_INDEX_2D_MASK              (0xF << 16)
+#define _PSB_CB_BANK_INDEX_3D_SHIFT             (12)
+#define _PSB_CB_BANK_INDEX_3D_MASK              (0xF << 12)
+#define _PSB_CB_BANK_INDEX_HOST_SHIFT           (8)
+#define _PSB_CB_BANK_INDEX_HOST_MASK            (0xF << 8)
+#define _PSB_CB_BANK_INDEX_TA_SHIFT             (4)
+#define _PSB_CB_BANK_INDEX_TA_MASK              (0xF << 4)
+#define _PSB_CB_BANK_INDEX_EDM_SHIFT            (0)
+#define _PSB_CB_BANK_INDEX_EDM_MASK             (0xF << 0)
+
+#define PSB_CR_BIF_DIR_LIST_BASE0               (SGX_BASE + 0x0C84)
+#define PSB_CR_BIF_DIR_LIST_BASE1               (SGX_BASE + 0x0C38)
+#define PSB_CR_BIF_TWOD_REQ_BASE                (SGX_BASE + 0x0C88)
+
+#define PSB_CR_BIF_TA_REQ_BASE                  (SGX_BASE + 0x0C90)
+#define _PSB_CBTR_BASE_ADDR_SHIFT               (20) // 1MB aligned address
+
+#define PSB_CR_BIF_MEM_ARB_CONFIG               (SGX_BASE + 0x0CA0)
+#define _PSB_CBMA_CONFIG_TTE_THRESH_SHIFT       (12)
+#define _PSB_CBMA_CONFIG_TTE_THRESH_MASK        (0xFFF << 12)
+#define _PSB_CBMA_CONFIG_BEST_CNT_SHIFT         (4)
+#define _PSB_CBMA_CONFIG_BEST_CNT_MASK          (0xFF << 4)
+#define _PSB_CBMA_CONFIG_PAGE_SIZE_SHIFT        (0)
+#define _PSB_CBMA_CONFIG_PAGE_SIZE_MASK         (0xF << 0)
+
+#define PSB_CR_BIF_3D_REQ_BASE                  (SGX_BASE + 0x0CAC)
+#define _PSB_CB3R_BASE_ADDR_ALIGNSHIFT          (20)
+#define _PSB_CB3R_BASE_ADDR_SHIFT               (20) // 1MB aligned address
+
+/* 0xe00 - 0xf00 */
+#define PSB_EUR_CR_2D_SOCIF                     (SGX_BASE + 0x0e18)
+
+#define PSB_EUR_CR_2D_BLIT_STATUS               (SGX_BASE + 0x0e04)
+
+#endif /* _REGS_H_ */
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/rb.h b/drivers/gpu/drm/emgd/emgd/include/rb.h
new file mode 100644
index 0000000..8e0c166
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/rb.h
@@ -0,0 +1,186 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: rb.h
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This header file describes the inter-module interface for using the
+ *  ring buffer. This is a legacy interface that is not strictly
+ *  device-independent. It is kept here because many legacy chipsets use
+ *  this interface.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _RB_H
+#define _RB_H
+
+#include <igd_rb.h>
+#include <igd_errno.h>
+
+#include <context.h>
+#include <module_init.h>
+#include <mode.h>
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+
+/*
+ * The rb_buffer contents should be treated as opaque but are defined
+ * here for the use in the inline functions below.
+ */
+typedef struct _rb_buffer {
+	unsigned int id;       /* ring index */
+	unsigned long size;    /* size of rb in bytes */
+	unsigned long tail_off;
+	unsigned long dead_off;
+	unsigned long avail;
+	unsigned long addr;    /* starting address of rb */
+	unsigned char *virt;    /* virt starting address of rb */
+	unsigned char *start;   /* ring buffer start register offset */
+	unsigned char *head;    /* ring buffer head register offset */
+	unsigned char *tail;    /* ring buffer tail register offset */
+	unsigned char *ctrl;    /* ring buffer ctrl register offset */
+	unsigned char *sync;    /* used to synchronize s/w interface
+								   * code via rb_sync() */
+	unsigned long next_sync;
+	int state;             /* on or off state */
+	unsigned long last_head_value; /* used for checking ring buf lockup,
+									* keeps last head value*/
+	os_alarm_t last_head_time; /* used for checking ring buf lockup,
+								* keeps last head time*/
+	unsigned long reservation; /* increments upon each rb_reserver(),
+								* reset to zero upon rb_update() */
+	unsigned long sync_wrap;   /* Sync number to wrap at */
+	igd_context_t * context_ptr; /*back pointer to the main driver context */
+	igd_appcontext_h appcontext; /* Appcontext that is current on the ring */
+        unsigned int skip_timeout;  /* skip the timeout check */
+	unsigned int force_reset_rb;  /* force a rb reset */
+} rb_buffer_t;
+
+
+#define MODE_GET_RING(d, p) \
+	((rb_buffer_t *)((igd_display_pipe_t *)((igd_display_context_t *)d)->pipe)->queue[p])
+
+/*
+ * Flags used exclusively by rb_reserve()
+ */
+#define RB_RESERVE_BLOCK     0x00000000
+#define RB_RESERVE_NONBLOCK  0x00000001
+
+
+/*
+ * Determine how much space is available in the ring buffer.
+ */
+unsigned long rb_avail(rb_buffer_t *buffer);
+
+
+#define MODE_PIPE_ALLOCED(d) \
+	((igd_display_pipe_t *)((igd_display_context_t *)d)->pipe)
+
+extern igd_command_t *rb_slow_reserve(rb_buffer_t *buffer,
+	unsigned long size,
+	unsigned long flags);
+/*
+ * For Debug do not use the inline ring functions. It makes it hard
+ * to add breakpoints.
+ */
+#ifdef DEBUG_BUILD_TYPE
+extern igd_command_t *rb_reserve(rb_buffer_t *buffer,
+	unsigned long size,
+	unsigned long flags);
+
+extern int rb_update(rb_buffer_t *buffer,
+	igd_command_t *addr);
+
+#else
+#ifdef CONFIG_CMD
+static __inline igd_command_t *rb_reserve(rb_buffer_t *buffer,
+	unsigned long size,
+	unsigned long flags)
+{
+	if (buffer->state == CMD_CONTROL_OFF) {
+		/* ring buffer is turned off, so don't allow a rb_reserve() */
+		buffer->context_ptr->igd_device_error_no = -IGD_ERROR_PWRDOWN;
+		return(NULL);
+	}
+
+	/*
+	 * Change size to bytes for efficiency.
+	 */
+	size = ((size<<2) + 7) & ~7;
+
+	if(buffer->reservation) {
+		buffer->avail += buffer->reservation;
+	}
+	buffer->reservation = size;
+
+	if(buffer->avail > size) {
+		buffer->avail -= size;
+		return (igd_command_t *)(buffer->virt + buffer->tail_off);
+	}
+
+	return rb_slow_reserve(buffer, size, flags);
+}
+
+
+static __inline int rb_update(rb_buffer_t *buffer,
+	igd_command_t *addr)
+{
+	unsigned long tail_off;
+/*	tail_off = (addr - buffer->virt); */
+	tail_off = (unsigned long)((unsigned char *)(addr) - buffer->virt);
+
+	buffer->reservation = 0;
+	buffer->tail_off = tail_off;
+
+	if(buffer->tail_off & 0x7) {
+		EMGD_WRITE32(0, buffer->virt + buffer->tail_off);
+		buffer->tail_off += 4;
+		buffer->avail -= 4;
+	}
+
+	EMGD_WRITE32(buffer->tail_off, buffer->tail);
+
+	return 0;
+}
+#else
+static __inline igd_command_t *rb_reserve(rb_buffer_t *buffer,
+	unsigned long size,
+	unsigned long flags)
+{
+	return NULL;
+}
+
+static __inline int rb_update(rb_buffer_t *buffer,
+	igd_command_t *addr)
+{
+	return 0;
+}
+#endif /* CONFIG_CMD */
+#endif
+
+#endif /* _RB_H */
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/reset.h b/drivers/gpu/drm/emgd/emgd/include/reset.h
new file mode 100644
index 0000000..809cb52
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/reset.h
@@ -0,0 +1,43 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: reset.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for the reset module. It should be included
+ *  by ssigd HAL modules that need to access the reset module for initialization
+ *  and shutdown
+ *-----------------------------------------------------------------------------
+ */
+
+
+#ifndef _IGD_RESET_INIT_H
+#define _IGD_RESET_INIT_H
+
+#define RESET_FLAG_CAPTURE_SNAPSHOT         0x00000001
+#define RESET_FLAG_EXECUTE_SOFT_RESET       0x00000002
+#define RESET_FLAG_EXECUTE_HARD_RESET       0x00000003
+
+
+#endif /*_IGD_RESET_H*/
diff --git a/drivers/gpu/drm/emgd/emgd/include/sched.h b/drivers/gpu/drm/emgd/emgd/include/sched.h
new file mode 100644
index 0000000..9b33a1a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/sched.h
@@ -0,0 +1,197 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sched.h
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains Linux User-space implementations for the OAL io.h
+ *  abstractions.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OAL_LINUX_USER_SCHED_H
+#define _OAL_LINUX_USER_SCHED_H
+
+#include <config.h>
+
+#include <linux/sched.h>
+#include <linux/delay.h>
+
+typedef unsigned long os_alarm_t;
+
+static __inline os_alarm_t _linux_kernel_set_alarm(unsigned long t)
+{
+	return (msecs_to_jiffies(t) + jiffies);
+}
+
+static __inline int _linux_kernel_test_alarm(os_alarm_t t)
+{
+	return (jiffies >= t) ? 1 : 0;
+}
+
+#define OS_SET_ALARM(t) _linux_kernel_set_alarm(t)
+#define OS_TEST_ALARM(t) _linux_kernel_test_alarm(t)
+
+
+/*----------------------------------------------------------------------------
+ *  Function: OS_INIT_INTERRUPT
+ *
+ *  Description:
+ *  This function establishes the communication for sending requests for interrupt
+ *
+ *  int OS_INIT_INTERRUPT( did, mmadr )
+ *---------------------------------------------------------------------------*/
+#ifdef CONFIG_INTERRUPT
+#define OS_INIT_INTERRUPT(did, mmadr) _OS_INIT_INTERRUPT(did, mmadr)
+#else
+#define OS_INIT_INTERRUPT(did, mmadr) 0
+#endif
+
+
+
+/*----------------------------------------------------------------------------
+ * Function: OS_SCHEDULE
+ *
+ * Description:
+ *  This function can be called with the caller wished to give up processor
+ *  control until the next available timeslice. This will allow other OS tasks
+ *  to run before returning to the current context. This function should be
+ *  used with caution and observation of reentrant coding principals.
+ *
+ *  void OS_SCHEDULE( void );
+ *---------------------------------------------------------------------------*/
+
+#define OS_SCHEDULE() {									\
+	__set_current_state(TASK_INTERRUPTIBLE);					\
+	schedule_timeout(1); }
+
+#if 0
+#define OS_DELAY(usec) {					\
+    unsigned long j_timeout = usecs_to_jiffies(usec);		\
+    unsigned long j_start = jiffies;				\
+    unsigned long j_current;					\
+    do {							\
+      j_current = jiffies;					\
+    }while((j_current - j_start) < j_timeout);			\
+}
+#else 
+#define OS_DELAY(usec)	udelay(usec);
+#endif
+
+#if 0
+// Implement _OS_SLEEP at a wait-loop
+#define _OS_SLEEP(usec) {					\
+	unsigned long j_timeout = usecs_to_jiffies(usec);		\
+	unsigned long j_start = jiffies;				\
+	unsigned long j_current;					\
+	do {							\
+		j_current = jiffies;					\
+	}while((j_current - j_start) < j_timeout);			\
+}
+#else
+// Implement _OS_SLEEP as a true sleep
+
+/* We could also try TASK_UNINTERRUPTIBLE here */
+/*----------------------------------------------------------------------------
+ * Function: OS_SLEEP
+ *
+ * Parameters:
+ *  time_val: Unsigned long time value in Micro-seconds (1/1000000 of a second)
+ *            for the task to sleep.
+ *
+ * Description:
+ *  This function causes the caller to delay further processing
+ *  for the number of micro-seconds ( 1/1000000 or a second ) requested.
+ *  This function should only be used with small time values
+ *  ( < 1/100 of a second ) as lengthy sleeps could degrade the kernel
+ *  response time.
+ *
+ *  void OS_SLEEP( unsigned long time_val );
+ *---------------------------------------------------------------------------*/
+
+#define OS_SLEEP(usec) {									\
+	__set_current_state(TASK_KILLABLE);						\
+	schedule_timeout(usecs_to_jiffies(usec)); }
+#endif
+
+/*----------------------------------------------------------------------------
+ * Function: OS_UISLEEP
+ *
+ * Parameters:
+ *  time_val: Unsigned long time value in Micro-seconds (1/1000000 of a second)
+ *            for the task to sleep.
+ *
+ * Description:
+ *  This function causes the caller to delay further processing
+ *  for the number of micro-seconds ( 1/1000000 or a second ) requested.
+ *  This sleep is uninterrruptable.
+ *  This function should only be used with small time values
+ *  ( < 1/100 of a second ) as lengthy sleeps could degrade the kernel
+ *  response time.
+ *
+ *  void OS_UISLEEP( unsigned long time_val );
+ *---------------------------------------------------------------------------*/
+
+/* un interuuptable sleep */
+#define OS_UISLEEP(usec) {					\
+	__set_current_state(TASK_UNINTERRUPTIBLE);			\
+	schedule_timeout(usecs_to_jiffies(usec)); 			\
+}
+
+#ifndef OS_PTHREAD_CREATE
+typedef int os_pthread_t;
+// This function is not being used.
+// #define OS_PTHREAD_CREATE(t, a, f, p) (-1)
+#endif
+
+#ifndef OS_PTHREAD_JOIN
+// This function is not being used.
+// #define OS_PTHREAD_JOIN(t, p) (0)
+#endif
+
+/*
+ * Note, the POSIX pthread implementation does not take the thread as a
+ * parameter but it makes porting easier.
+ */
+#define UNREFERENCED_PARAMETER(parm) ((parm)=(parm))
+
+#ifndef OS_PTHREAD_EXIT
+// This function is not being used.
+// #define OS_PTHREAD_EXIT(t, r)
+#endif
+
+#ifndef OS_PTHREAD_MUTEX_INIT
+typedef struct mutex os_pthread_mutex_t;
+#define OS_PTHREAD_MUTEX_INIT(m, a) mutex_init(m)
+#endif
+
+#ifndef OS_PTHREAD_MUTEX_LOCK
+#define OS_PTHREAD_MUTEX_LOCK(m) mutex_lock_interruptible(m)
+#endif
+
+#ifndef OS_PTHREAD_MUTEX_UNLOCK
+#define OS_PTHREAD_MUTEX_UNLOCK(m) mutex_unlock(m)
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/state2d.h b/drivers/gpu/drm/emgd/emgd/include/state2d.h
new file mode 100644
index 0000000..af04e47
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/state2d.h
@@ -0,0 +1,69 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: state2d.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is the header file for manipulating the 2D State variables.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _STATE2D_H
+#define _STATE2D_H
+
+/* IGD */
+#include <igd_render.h>
+
+typedef struct _state2d {
+    igd_rect_t dest_rect;
+	unsigned int clip_status;
+} state2d_t;
+
+#define STATE2D(ac) ((state2d_t *)ac->state2d)
+
+#define STATE2D_SET_CLIP(ac, rect)                            \
+if(ac) {                                                      \
+    OS_MEMCPY(&((state2d_t *)(ac)->state2d)->dest_rect, rect, \
+		sizeof(igd_rect_t));                                  \
+}
+ /*
+  * Checking for the 2D appcontext instead of the appcontext
+  * pointer because the state2d could be null during botched
+  * allocation.
+  */
+#define STATE2D_GET_CLIP(ac) \
+	(((ac) && ((state2d_t *)(ac)->state2d))? \
+	(&((state2d_t *)(ac)->state2d)->dest_rect):NULL)
+
+#define STATE2D_GET_CLIP_STATUS(ac) \
+	(((ac) && ((state2d_t *)(ac)->state2d))? \
+	(((state2d_t *)(ac)->state2d)->clip_status):0)
+
+#define STATE2D_SET_CLIP_STATUS(ac, s) \
+	(((ac) && ((state2d_t *)(ac)->state2d))? \
+	(((state2d_t *)(ac)->state2d)->clip_status = s):0)
+
+#endif /*_STATE2D_H */
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/appcontext.h b/drivers/gpu/drm/emgd/emgd/include/tnc/appcontext.h
new file mode 100644
index 0000000..0c76cf1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/appcontext.h
@@ -0,0 +1,40 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: appcontext.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the inter-module header file for the client context module.
+ *  It contains data structures needed for modules to use and manipulate
+ *  the device state including hardware context switching.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _APPCONTEXT_TNC_H
+#define _APPCONTEXT_TNC_H
+
+/* reusing pouslbo structures */
+#include<plb/appcontext.h>
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/cmd.h b/drivers/gpu/drm/emgd/emgd/include/tnc/cmd.h
new file mode 100644
index 0000000..731c86a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/cmd.h
@@ -0,0 +1,39 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: cmd.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Device dependent header file for the command interface for poulsbo
+ *  devices.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _TNC_CMD_H
+#define _TNC_CMD_H
+
+/* reusing pouslbo codes */
+#include<plb/cmd.h>
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/context.h b/drivers/gpu/drm/emgd/emgd/include/tnc/context.h
new file mode 100644
index 0000000..bcd73ee
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/context.h
@@ -0,0 +1,37 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: context.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_TNC_CONTEXT_H
+#define _HAL_TNC_CONTEXT_H
+
+#include<plb/context.h>
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/igd_tnc_wa.h b/drivers/gpu/drm/emgd/emgd/include/tnc/igd_tnc_wa.h
new file mode 100644
index 0000000..7cd4aa7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/igd_tnc_wa.h
@@ -0,0 +1,133 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_tnc_wa.h
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This header file contains the Device Dependent information used with
+ *  the IEGD HAL for the Atom E6xx family of supported chips.
+ *  Note: This file should be included into a C file as <tnc/igd_tnc_wa.h> and
+ *  not <igd_tnc_wa.h> this will insure that multiple device dependent
+ *  igd_cmd.h files can be included into a single C source.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_TNC_WA_H
+#define _IGD_TNC_WA_H
+
+#define LNC_CLOCK 199500
+#define LNC_B1_OPTIMUM_MARGIN 2000
+#define LIMIT_TOTAL_CHECK_DISPLAY 10
+#define T0 0
+#define T1 1
+#define WA_TUNE \
+	((tnc_wa_timing_t *)&tune)
+/* Graphic core Revision ID for Atom E6xx stepping */
+#define TNC_A0_RID			0x2
+#define TNC_B0_RID			0x3
+#define TNC_B0_DEV3_RID		0x1
+#define TNC_B1_DEV3_RID		0x2
+#define SDVO_CRC_CTRL_REG	0x61050
+#define SDVO_BUFF_CTRL_REG	0x61170
+#define TNC_HTOTAL_TUNED	0x8000
+
+/* flag to enable clipping hardware fix in TNC B1 */
+#define IGD_CLIP_FIX_DISABLE            0x0000
+ /* Completely disable throttling */
+#define IGD_CLIP_FIX_DISABLE_THROTTLE   0x0001          
+/* Replaces the original stall_pixel from FSM with external stall from cDVO */
+#define IGD_CLIP_FIX_REPLACE_STALL      0x0002    
+#define IGD_CLIP_FIX_GLOBAL_ENABLE      0x8000
+#define CLIP_HW_FIX_DEFAULT             0x8001
+
+/*
+ * These are the register default settings for TC B1
+ * They are used for the undocumented register override feature
+ * when en_reg_override=1
+ */
+#define DISP_ARB                0x00003232
+#define FIFO_WATERMARK_1        0x3f8f0404
+#define FIFO_WATERMARK_2        0x04040f04
+#define FIFO_WATERMARK_3        0x00000000
+#define FIFO_WATERMARK_4        0x04040404
+#define FIFO_WATERMARK_5        0x04040404
+#define FIFO_WATERMARK_6        0x00000078
+#define GVD_HP_CONTROL          0x102003f2
+#define DISP_CHICKENBITS        0x0000c040
+#define BCHICKENBITS            0xffe32004
+#define BWFLUSH                 0x00000408
+
+typedef struct _tnc_wa_timing_t{
+	short htotal;
+	short delta;
+	short flag;
+	short counter;
+	unsigned int crc_red;
+	unsigned int crc_green;
+	unsigned int crc_blue;
+}tnc_wa_timing_t;
+
+typedef union
+{
+       unsigned int pixel;
+       struct
+	   {
+			   unsigned int bit0             :1;
+               unsigned int bit1             :1;
+               unsigned int bit2             :1;
+               unsigned int bit3             :1;
+               unsigned int bit4             :1;
+               unsigned int bit5             :1;
+               unsigned int bit6             :1;
+               unsigned int bit7             :1;
+               unsigned int bit8             :1;
+               unsigned int bit9             :1;
+               unsigned int bit10             :1;
+               unsigned int bit11             :1;
+               unsigned int bit12             :1;
+               unsigned int bit13             :1;
+               unsigned int bit14             :1;
+               unsigned int bit15             :1;
+               unsigned int bit16             :1;
+               unsigned int bit17             :1;
+               unsigned int bit18             :1;
+               unsigned int bit19             :1;
+               unsigned int bit20             :1;
+               unsigned int bit21             :1;
+               unsigned int bit22             :1;
+               unsigned int bitExtra          :1;
+               unsigned int bitRest           :8;
+       } bit;
+} pixel_crc_t;
+
+
+/*
+* CDVO regs structure. This is mainly used to move the CDVO reset sequence
+* to the data segment for VBIOS.
+*/
+typedef struct _cdvo_regs_t{
+	unsigned long reg;
+	unsigned long value;
+}cdvo_regs_t;
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/instr.h b/drivers/gpu/drm/emgd/emgd/include/tnc/instr.h
new file mode 100644
index 0000000..14bf85f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/instr.h
@@ -0,0 +1,40 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: instr.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+/*
+ * TODO:
+ * Add or modify the struct and other definitions for Poulsbo
+ */
+#ifndef _INSTR_TNC_H
+#define _INSTR_TNC_H
+
+#include<plb/instr.h>
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/mi.h b/drivers/gpu/drm/emgd/emgd/include/tnc/mi.h
new file mode 100644
index 0000000..2bb8c23
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/mi.h
@@ -0,0 +1,41 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: mi.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is a header file for the Intel GFX commands.
+ *  This includes commands specific to Intel hardware and structures specific
+ *  to Intel hardware.  All other commands and structures are available
+ *  through GFX.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _TNC_MI_H
+#define _TNC_MI_H
+
+#include<plb/mi.h>
+
+
+#endif /* _MI_H */
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/regs.h b/drivers/gpu/drm/emgd/emgd/include/tnc/regs.h
new file mode 100644
index 0000000..218722c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/regs.h
@@ -0,0 +1,889 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: regs.h
+ * $Revision: 1.16 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the register definition file for the Atom E6xx platform. This should
+ *  contain device dependent register definitions. Standard register
+ *  definitions (VGA, PCI, etc) should not be put in this file. For those
+ *  see pci.h or vga.h.
+ *-----------------------------------------------------------------------------
+ */
+
+/*
+ * Note: Use _REGS_H_  instead of _TNC_REGS_H_ to insure that no file can
+ * include 2 device dependent register files.
+ */
+#ifndef _REGS_H_
+#define _REGS_H_
+
+#include <pci.h>
+#include <psb_regs.h>
+#include <msvdx.h>
+#include <topaz.h>
+
+/*-----------------------------------------------------------------------------
+ * PCI Register Definitions
+ *---------------------------------------------------------------------------*/
+#define TNC_PCI_MMADR      PCI_BAR_0
+#define TNC_PCI_IOBAR      PCI_BAR_1
+#define TNC_PCI_GMADR      PCI_BAR_2
+#define TNC_PCI_GTTADR     PCI_BAR_3
+
+/*
+ * GVD.MGGC
+ * Device: 2 Offset:50h
+ * [22:20] Graphics mode select
+ * [17] VGA Disable
+ */
+#define TNC_PCI_GC         0x52
+#define TNC_PCI_BSM        0x5C
+
+/* Atom E6xx Device 31 GPIO base address offset */
+#define TNC_PCI_GBA			0x44
+
+#define TNC_D2_MMIO_SIZE  (1024*1024)
+#define TNC_D3_MMIO_SIZE  (512*1024)
+#define TNC_ST_SDVO_MMIO_SIZE  (10*1024)
+
+#define TNC_GTT_SIZE   (128*1024)
+
+
+
+/*-----------------------------------------------------------------------------
+ * General VGA Register Definitions
+ *---------------------------------------------------------------------------*/
+#define FEATURE_CONT_REG      0x3DA  /* Feature Control Register */
+#define FEATURE_CONT_REG_MONO 0x3BA  /* Feature Control Register (Mono) */
+#define FEATURE_CONT_REG_READ 0x3CA  /* Feature Control Register (Read) */
+#define MSR_PORT              0x3C2  /* Miscellaneous Output Port */
+#define MSR_PORT_LSB          0xC2   /* Miscellaneous Output Port LSB */
+#define MSR_READ_PORT         0x3CC  /* Miscellaneous Output Reg (Read) */
+#define STATUS_REG_00         0x3C2  /* Input Status Register 0 */
+#define STATUS_REG_01         0x3DA  /* Input Status Register 1 */
+#define STATUS_REG_01_MONO    0x3BA  /* Input Status Register 1 (Mono) */
+
+/* DAC Register Definitions. */
+#define DAC_PEL_MASK          0x3C6  /* Color Palette Pixel Mask Register */
+#define DAC_READ_INDEX        0x3C7  /* Color Palette Read-Mode Index Reg */
+#define DAC_STATE             0x3C7  /* Color Palette State Register */
+#define DAC_WRITE_INDEX       0x3C8  /* Color Palette Index Register */
+#define DAC_DATA_REG          0x3C9  /* Color Palette Data Register */
+
+
+/*-----------------------------------------------------------------------------
+ * Attribute Controller Register Definitions
+ *---------------------------------------------------------------------------*/
+#define AR_PORT_LSB  0xC0  /*Attribute Controller Index Port LSB */
+
+#define AR00  0x00  /* Color Data Register */
+#define AR01  0x01	/* Color Data Register */
+#define AR02  0x02	/* Color Data Register */
+#define AR03  0x03	/* Color Data Register */
+#define AR04  0x04	/* Color Data Register */
+#define AR05  0x05	/* Color Data Register */
+#define AR06  0x06	/* Color Data Register */
+#define AR07  0x07	/* Color Data Register */
+#define AR08  0x08	/* Color Data Register */
+#define AR09  0x09	/* Color Data Register */
+#define AR0A  0x0A	/* Color Data Register */
+#define AR0B  0x0B	/* Color Data Register */
+#define AR0C  0x0C	/* Color Data Register */
+#define AR0D  0x0D	/* Color Data Register */
+#define AR0E  0x0E	/* Color Data Register */
+#define AR0F  0x0F	/* Color Data Register */
+#define AR10  0x10	/* Mode Control Register */
+#define AR11  0x11	/* Overscan Color Register */
+#define AR12  0x12	/* Color Plane Enable Register */
+#define AR13  0x13	/* Horizontal Pixel Panning Register */
+#define AR14  0x14	/* Pixel Pad Register */
+
+
+/*-----------------------------------------------------------------------------
+ * CRT Controller Register Definitions
+ *---------------------------------------------------------------------------*/
+#define CR_PORT_LSB     0xD4  /* CRT Controller Index Port LSB */
+#define CRT_3D4	        0x3D4 /* Color CRTC Index Port */
+#define CRT_3B4	        0x3B4 /* Monochrome CRTC Index Port */
+
+#define CR00            0x00  /* Horizontal Total Register */
+#define CR01            0x01  /* Horizontal Display Enable End Reg */
+#define CR02            0x02  /* Horizontal Blank Start Register */
+#define CR03            0x03  /* Horizontal Blank End Register */
+#define CR04            0x04  /* Horizontal Sync Start Register */
+#define CR05            0x05  /* Horizontal Sync End Register */
+#define CR06            0x06  /* Vertical Total Register */
+#define CR07            0x07  /* Overflow Register */
+#define CR08            0x08  /* Preset Row Scan Register */
+#define CR09            0x09  /* Maximum Scan Line Register */
+#define DOUBLE_SCANLINE	BIT7  /* Double scan ( 1 = Enable ) */
+#define LCOMP_BIT9      BIT6  /* Bit 9 of line compare register */
+#define VBLANK_BIT9     BIT5  /* Bit 9 of vertical blank start */
+#define CR0A            0x0A  /* Cursor Start Scan Line Register */
+#define CR0B            0x0B  /* Cursor End Scan Line Register */
+#define CR0C            0x0C  /* Start Address High Register */
+#define CR0D            0x0D  /* Start Address Low Register */
+#define CR0E            0x0E  /* Cursor Location High Register */
+#define CR0F            0x0F  /* Cursor Location Low Register */
+#define CR10            0x10  /* Vertical Sync Start Register */
+#define CR11            0x11  /* Vertical Sync End Register */
+#define CR12            0x12  /* Vertical Display Enable End Reg */
+#define CR13            0x13  /* Offset Register */
+#define CR14            0x14  /* Underline Row Register */
+#define CR15            0x15  /* Vertical Blank Start Register */
+#define CR16            0x16  /* Vertical Blank End Register */
+#define CR17            0x17  /* CRT Mode Control Register */
+#define CR18            0x18  /* Line Compare Register */
+#define CR22            0x22  /* Memory Data Latches Register */
+#define CR24            0x24  /* Attribute Controller Toggle Reg */
+
+
+/*-----------------------------------------------------------------------------
+ * Graphics Controller Register Definitions
+ *---------------------------------------------------------------------------*/
+#define GR_PORT_LSB    0xCE   /* Graphics Controller Index Port LSB */
+
+#define GR00           0x00   /* Set/Reset Register */
+#define GR01           0x01   /* Enable Set/Reset Register */
+#define GR02           0x02   /* Color Compare Register */
+#define GR03           0x03   /* Data Rotate Register */
+#define GR04           0x04   /* Read Map Select Register */
+#define GR05           0x05   /* Graphics Mode Register */
+#define GR06           0x06   /* Micsellaneous Register */
+#define RANGE_MAP_MASK BIT3 + BIT2  /* Address range to map mask */
+#define A0_BF_RANGE    000h   /* Map to A0000h-BFFFFh range */
+#define GRAF_MODE      BIT0   /* 1 = Grahics mode, 0 = Text mode */
+#define GR07           0x07   /* Color Don't Care Register */
+#define GR08           0x08   /* Bit Mask Register */
+#define GR10           0x10   /* Address Mapping */
+#define PAGING_TARGET  BIT2 + BIT1 /* 00 = Local/Stolen, 01 = Mem mapped regs */
+#define PAGE_MODE      BIT0   /* Page Map allow access to all FB mem */
+#define GR11           0x11   /* Page Selector */
+#define	GR18           0x18   /* Software Flag */
+
+
+/*-----------------------------------------------------------------------------
+ * Sequencer Register Definitions
+ *---------------------------------------------------------------------------*/
+#define SR_PORT_DATA      0x3C5 /* Sequencer Data Port */
+#define SR_PORT_LSB       0xC4  /* Sequencer Index Port LSB */
+
+#define SR00              0x00  /* Reset Register */
+#define SR01              0x01  /* Clocking Mode Register */
+#define DOT_CLOCK_DIVIDE  BIT3	/* Divide pixel clock by 2 */
+#define SR02              0x02  /* Plane/Map Mask Register */
+#define SR03              0x03  /* Character Font Register */
+#define SR04              0x04  /* Memory Mode Register */
+#define SR07              0x07  /* Horizontal Character Counter Reset */
+
+/*-----------------------------------------------------------------------------
+ * Platform specific PCI Config Register
+ *---------------------------------------------------------------------------*/
+#define TNC_OFFSET_VGA_MSAC         0x62
+
+/* FIXME: Core Clock should be obtained from the Message Control Register*/
+#define INTEL_OFFSET_VGA_CORECLK    0xF0
+
+/* #define INTEL_OFFSET_BRIDGE_CAPREG  0xE0 Not needed for Atom E6xx */
+
+#define PCI_GMS_MASK      BIT6 + BIT5 + BIT4 /* GFX Mode Select Bits Mask */
+#define PCI_LOCAL		  BIT4 /* Local memory enabled */
+#define PCI_DVMT_512K     BIT5 /* 512KB DVMT */
+#define PCI_DVMT_1M       BIT5 + BIT4 /* 1MB DVMT */
+#define PCI_DVMT_8M       BIT6 /* 8MB DVMT */
+
+#define PCI_DRB_REG       0x60 /* DRAM row boundary Register */
+#define PCI_DRC_REG       0x7C /* DRAM Controller Mode Register */
+#define PCI_DT_MASK       BIT0 + BIT1  /* Select SDRAM types.
+                                        *  = 00:  Single data rate SDRAM
+                                        *  = 01:  Dual data rate SDRAM
+                                        *  = Other:  Reserved
+                                        */
+#define DT_SDR_SDRAM      00   /* Single data rate SDRAM */
+#define DT_DDR_SDRAM      01   /* Dual data rate SDRAM */
+
+#define PCI_ESMRAMC_REG   0x91 /* Extended System Management RAM Reg */
+#define PCI_TSEG_SZ		  BIT1 /* TSEG size bit */
+#define PCI_TSEG_512K	  0    /* 512K TSEG */
+#define PCI_TSEG_1M       BIT1 /* 1MB TSEG */
+
+#define PCI_GCLKIO_REG    0xC0 /* GMCH Clock and IO Control Register */
+#define PCI_AGP_Bit       BIT9 /* AGP/DVO Mux Select:
+								*  = 0, DVO/ZV
+								*  = 1, AGP
+								*/
+#define PCI_GMCHCFG_REG   0xC6 /* GMCH Configuration Register */
+#define PCI_SMFREQ_MASK   BIT10 + BIT11
+						   /* System Mem Frequency Select
+                            * = 00:  Intel Reserved
+                            * = 01:  System Memory Frequency is 166Mhz (DDR333) - Intel Reserved
+                            * = 10:  System Memory Frequency is 133Mhz (SDR133, DDR266)
+                            * = 11:  System Memory Frequency is 100Mhz (DDR200)
+                            */
+#define SYS_MEM_FREQ_166  1  /* System Memory Frequency is 166Mhz */
+#define SYS_MEM_FREQ_133  2  /* System Memory Frequency is 133Mhz */
+#define SYS_MEM_FREQ_100  3  /* System Memory Frequency is 100Mhz */
+#define PCI_SMFREQ_POS    10 /* System Memory Frequency position  */
+
+#define PCI_CONFIG_LMINT  0xE0
+#define PREALLOCATED_SIZE (8 * 1024 * 1024)
+
+
+/*-----------------------------------------------------------------------------
+ * General VGA Register Definitions
+ *---------------------------------------------------------------------------*/
+#define FEATURE_CONT_REG      0x3DA  /* Feature Control Register */
+#define FEATURE_CONT_REG_MONO 0x3BA  /* Feature Control Register (Mono) */
+#define FEATURE_CONT_REG_READ 0x3CA  /* Feature Control Register (Read) */
+#define MSR_PORT              0x3C2  /* Miscellaneous Output Port */
+#define MSR_PORT_LSB          0xC2   /* Miscellaneous Output Port LSB */
+#define MSR_READ_PORT         0x3CC  /* Miscellaneous Output Reg (Read) */
+#define STATUS_REG_00         0x3C2  /* Input Status Register 0 */
+#define STATUS_REG_01         0x3DA  /* Input Status Register 1 */
+#define STATUS_REG_01_MONO    0x3BA  /* Input Status Register 1 (Mono) */
+
+/* DAC Register Definitions. */
+#define DAC_PEL_MASK          0x3C6  /* Color Palette Pixel Mask Register */
+#define DAC_READ_INDEX        0x3C7  /* Color Palette Read-Mode Index Reg */
+#define DAC_STATE             0x3C7  /* Color Palette State Register */
+#define DAC_WRITE_INDEX       0x3C8  /* Color Palette Index Register */
+#define DAC_DATA_REG          0x3C9  /* Color Palette Data Register */
+
+
+/*-----------------------------------------------------------------------------
+ * Memory mapped I/O Registers Definitions
+ *---------------------------------------------------------------------------*/
+
+/*-----------------------------------------------------------------------------
+ * Instruction and Interrupt Control Registers (01000h - 02FFFh)
+ *---------------------------------------------------------------------------*/
+#define PGTBL_CTL         0x02020  /* Page Table Control Register */
+#define HWS_PGA           0x02080  /* Hardware Status Page Address register */
+#define HWSTAM            0x02098  /* Hardware Status Mask */
+#define SCPD0             0x0209C  /* Scratch Pad 0 (Debug) */
+#define IER               0x020A0  /* Interrupt Enable */
+#define IIR               0x020A4  /* Interrupt Identity */
+#define IMR               0x020A8  /* Interrupt Mask */
+#define ISR               0x020AC  /* Interrupt Status */
+#define EIR               0x020B0  /* Error Identity */
+#define EMR               0x020B4  /* Error Mask */
+#define ESR               0x020B8  /* Error Status */
+#define FW_BLC_SELF       0x020E0  /* Display FIFO Watermark */
+#define MI_ARB_STATE      0x020E4  /* Memory Interface Arbitration State */
+#define G_DEBUG		  0x020FC  /* G-UNIT Debug enable register */
+
+#if 0
+/*-----------------------------------------------------------------------------
+ * FENCE and Per Process GTT Control Registers (02000h - 031FFh)
+ * --------------------------------------------------------------------------*/
+#define FENCE0            0x02000 /* Fence table registers */
+#define FENCE1            0x02004
+#define FENCE2            0x02008
+#define FENCE3            0x0200C
+#define FENCE4            0x02010
+#define FENCE5            0x02014
+#define FENCE6            0x02018
+#define FENCE7            0x0201C
+#define FENCE8            0x03000
+#define FENCE9            0x03004
+#define FENCE10           0x03008
+#define FENCE11           0x0300C
+#define FENCE12           0x03010
+#define FENCE13           0x03014
+#define FENCE14           0x03018
+#define FENCE15           0x0301C
+#endif
+/*-----------------------------------------------------------------------------
+ * FIXME: For TNC, we were not able to find specs detailing the FENCE registers
+ * Use the GN4 FENCE Registers for the time being as a placeholder till we can
+ * nail down the FENCE registers
+ * --------------------------------------------------------------------------*/
+/*-----------------------------------------------------------------------------
+ * FENCE and Per Process GTT Control Registers (03000h - 031FFh)
+ *
+ * The FENCE registers are now 64-bits but we can only read/write 32-bits
+ * at a time.  As a result, each register has aliases for the whole 64-bits,
+ * the low DWORD, and the high DWORD.
+ *
+ * This is important for restoring the registers, since we must always write
+ * the high DWORD first.
+ * --------------------------------------------------------------------------*/
+#define FENCE0            0x03000 /* Fence table registers */
+#define FENCE0l           0x03000
+#define FENCE0h           0x03004
+#define FENCE1            0x03008
+#define FENCE1l           0x03008
+#define FENCE1h           0x0300C
+#define FENCE2            0x03010
+#define FENCE2l           0x03010
+#define FENCE2h           0x03014
+#define FENCE3            0x03018
+#define FENCE3l           0x03018
+#define FENCE3h           0x0301C
+#define FENCE4            0x03020
+#define FENCE4l           0x03020
+#define FENCE4h           0x03024
+#define FENCE5            0x03028
+#define FENCE5l           0x03028
+#define FENCE5h           0x0302C
+#define FENCE6            0x03030
+#define FENCE6l           0x03030
+#define FENCE6h           0x03034
+#define FENCE7            0x03038
+#define FENCE7l           0x03038
+#define FENCE7h           0x0303C
+#define FENCE8            0x03040
+#define FENCE8l           0x03040
+#define FENCE8h           0x03044
+#define FENCE9            0x03048
+#define FENCE9l           0x03048
+#define FENCE9h           0x0304C
+#define FENCE10           0x03050
+#define FENCE10l          0x03050
+#define FENCE10h          0x03054
+#define FENCE11           0x03058
+#define FENCE11l          0x03058
+#define FENCE11h          0x0305C
+#define FENCE12           0x03060
+#define FENCE12l          0x03060
+#define FENCE12h          0x03064
+#define FENCE13           0x03068
+#define FENCE13l          0x03068
+#define FENCE13h          0x0306C
+#define FENCE14           0x03070
+#define FENCE14l          0x03070
+#define FENCE14h          0x03074
+#define FENCE15           0x03078
+#define FENCE15l          0x03078
+#define FENCE15h          0x0307C
+
+/*-----------------------------------------------------------------------------
+ * MISC I/0 Contol Register ( 05000h - 05FFFh )
+ *---------------------------------------------------------------------------*/
+#define IO_OFF          0x05000         /* Register group offset */
+
+#define IO00            0x05000         /* Hsync / Vsync control register */
+#define GPIO0           0x05010         /* GPIO register 0 (DDC1) */
+#define	DDC1_SCL_PIN    GPIO0_SCL_PIN   /* DDC1 SCL GPIO pin # */
+#define	DDC1_SDA_PIN    GPIO0_SDA_PIN   /* DDC1 SDA CPIO pin # */
+#define GPIO1           0x05014         /* GPIO register 1 (I2C) */
+#define	I2C_SCL_PIN     GPIO1_SCL_PIN   /* I2C SCL GPIO pin # */
+#define	I2C_SDA_PIN     GPIO1_SDA_PIN   /* I2C SDA CPIO pin # */
+#define GPIO2           0x05018         /* GPIO register 2 (DDC2) */
+#define	DDC2_SCL_PIN    GPIO2_SCL_PIN   /* DDC2 SCL GPIO pin # */
+#define	DDC2_SDA_PIN    GPIO2_SDA_PIN   /* DDC2 SDA CPIO pin # */
+#define GPIO3           0x0501C         /* GPIO register 3 (AGP mux DVI DDC) */
+#define GPIO4           0x05020         /* GPIO register 4 (AGP mux I2C) */
+#define GPIO5           0x05024         /* GPIO register 5 (AGP mux DDC2/I2C) */
+
+#define GPIOPIN0        GPIO0
+#define GPIOPIN1        GPIO0+1
+#define GPIOPIN2        GPIO1
+#define GPIOPIN3        GPIO1+1
+#define GPIOPIN4        GPIO2
+#define GPIOPIN5        GPIO2+1
+#define GPIOPIN6        GPIO3
+#define GPIOPIN7        GPIO3+1
+#define GPIOPIN8        GPIO4
+#define GPIOPIN9        GPIO4+1
+#define GPIOPIN10       GPIO5
+#define GPIOPIN11       GPIO5+1
+#define GPIOPINMAX      12
+
+#define GMBUS0          0x5100 /* GMBUS clock/device select register */
+#define GMBUS1          0x5104 /* GMBUS command/status register */
+#define GMBUS2          0x5108 /* GMBUS status register */
+#define GMBUS3          0x510c /* GMBUS data buffer register */
+#define GMBUS4          0x5110 /* GMBUS REQUEST_INUSE register */
+#define GMBUS5          0x5120 /* GMBUS 2-Byte Index register */
+#define GMBUS6          0x5124 /* GMBUS Clock divider */
+
+/*  GMBUS1 Bits */
+#define SW_CLR_INT      BIT31
+#define SW_RDY          BIT30
+#define ENT             BIT29
+#define STO             BIT27
+#define ENIDX           BIT26
+#define STA             BIT25
+
+/*  GMBUS2 Bits */
+#define INUSE           BIT15
+#define HW_WAIT         BIT14
+#define HW_TMOUT        BIT13
+#define HW_INT          BIT12
+#define HW_RDY          BIT11
+#define HW_BUS_ERR      BIT10
+#define GA              BIT9
+
+/*-----------------------------------------------------------------------------
+ * Clock Control and PM Register ( 06000h - 06FFFh )
+ *---------------------------------------------------------------------------*/
+#define VGA0_DIVISOR    0x06000  /* VGA 0  Divisor */
+#define VGA1_DIVISOR    0x06004  /* VGA 1 Divisor */
+#define VGA_PD          0x06010  /* VGA Post Divisor Select */
+#define DPLLAMD         0x0601C  /* Display PLL A SDVO Multiplier/Divisor */
+#define DPLLBMD         0x06020  /* Display PLL B SDVO Multiplier/Divisor */
+
+/*-----------------------------------------------------------------------------
+ * DPLL A Control Register ( 0F000h - 0FFFFh )
+ *---------------------------------------------------------------------------*/
+#define DPLLACNTR       0x0F014  /* Display PLL A Control */
+#define FPA0            0x0F040  /* DPLL A Divisor 0 */
+#define FPA1            0x0F044  /* DPLL A Divisor 1 */
+#define DPLL_TEST		0x0F06C  /* DPLLA Test Register */
+
+/*-----------------------------------------------------------------------------
+ * DPLL B Control Register ( 06000h - 06FFFh )
+ *---------------------------------------------------------------------------*/
+#define DPLLBCNTR       0x06018  /* Display PLL B Control */
+#define FPB0            0x06048  /* DPLL B Divisor 0 */
+#define FPB1            0x0604C  /* DPLL B Divisor 1 */
+#define TNC_DPLL_TEST	0x0606C	 /* DPLL A and DPLL B test register */
+
+#define DREFCLK         0x0
+#define TVCLKINBC       0x4000
+#define CLOCK_2X        0x40000000
+
+#define P2D_CG_DIS      0x06200  /* Clock Gating Disable */
+/* Not available in Atom E6xx */
+/* #define P3D_CG_DIS      0x06204 */ /* Clock Gating Disable */
+
+
+/*-----------------------------------------------------------------------------
+ * Display Palette Register Definitions (0A000h - 0AFFFh)
+ *---------------------------------------------------------------------------*/
+#define DPALETTE_A      0x0A000  /* Display Pipe A Palette */
+#define DPALETTE_B      0x0A800  /* Display Pipe B Palette */
+
+
+/*-----------------------------------------------------------------------------
+ * Display Pipeline / Port Register Definitions (60000h - 6FFFFh)
+ *---------------------------------------------------------------------------*/
+#define DP_OFFSET       0x60000  /* register group offset */
+#define PIPEA_TIMINGS   0x60000
+#define HTOTAL_A        0x60000  /* Pipe A Horizontal Total Register */
+#define ACTIVE_DISPLAY  0x7FF    /* bit [ 10:0 ] */
+#define HBLANK_A        0x60004  /* Pipe A Horizontal Blank Register */
+#define HSYNC_A         0x60008  /* Pipe A Horizontal Sync Register */
+#define VTOTAL_A        0x6000C  /* Pipe A Vertical Total Register */
+#define VBLANK_A        0x60010  /* Pipe A Vertical Blank Register */
+#define VSYNC_A         0x60014  /* Pipe A Vertical Sync Register */
+#define PIPEASRC        0x6001C  /* Pipe A Source Image Size Register */
+#define BCLRPAT_A       0x60020  /* Pipe A Border Color Pattern Register */
+#define CRCCTRLREDA     0x60050  /* Pipe A CRC Red Control Register */
+#define CRCCTRLGREENA   0x60054  /* Pipe A CRC Green Control Register */
+#define CRCCTRLBLUEA    0x60058  /* Pipe A CRC Blue Control Register */
+#define CRCCTRLRESA     0x6005C  /* Pipe A CRC Alpha Control Register */
+
+/*-----------------------------------------------------------------------------
+ * Pipe B registers are shared between Device 2 and Device 3
+ * Atom E6xx silicon design specifies that both device 2 and device 3
+ * Pipe B registers need to be set sequencially. Device 2, followed by
+ * Device 3
+ *---------------------------------------------------------------------------*/
+#define PIPEB_TIMINGS   0x61000
+#define HTOTAL_B        0x61000  /* Pipe B Horizontal Total Register */
+#define HBLANK_B        0x61004  /* Pipe B Horizontal Blank Register */
+#define HSYNC_B         0x61008  /* Pipe B Horizontal Sync Register */
+#define VTOTAL_B        0x6100C  /* Pipe B Vertical Total Register */
+#define VBLANK_B        0x61010  /* Pipe B Vertical Blank Register */
+#define VSYNC_B         0x61014  /* Pipe B Vertical Sync Register */
+#define PIPEBSRC        0x6101C  /* Pipe B Source Image Size Register */
+#define BCLRPAT_B       0x61020  /* Pipe B Border Color Pattern Register */
+#define VSYNCSHIFT_B	0x61028  /* Pipe B Vertical Sync Shift Register */
+#define CRCCTRLREDB     0x61050  /* Pipe B CRC Red Control Register */
+#define CRCCTRLGREENB   0x61054  /* Pipe B CRC Green Control Register */
+#define CRCCTRLBLUEB    0x61058  /* Pipe B CRC Blue Control Register */
+#define CRCCTRLRESB     0x6105C  /* Pipe B CRC Alpha Control Register */
+
+/* These registers are in Device 3 */
+#define PORT_HPLUG_EN		0x61110  /* Port Hot Plug Enable */
+#define PORT_HPLUG_STAT 	0x61114  /* Port Hot Plug Status */
+#define SDVOBCNTR       	0x61140  /* Digital Display Port B Control */
+#define LVDSCNTR        	0x61180  /* Digital Display Port Control */
+#define SDVO_BUFF_CTRL_REG	0x61170  /* SDVO Buffer Control */
+
+/* Panel Power Sequencing */
+#define LVDS_PNL_PWR_STS  0x61200  /* LVDS Panel Power Status Register */
+#define LVDS_PNL_PWR_CTL  0x61204  /* LVDS Panel Power Control Register */
+#define PP_ON_DELAYS      0x61208  /* Panel Power On Sequencing Delays */
+#define PP_OFF_DELAYS     0x6120C  /* Panel Power Off Sequencing Delays */
+#define PP_DIVISOR        0x61210  /* Panel Power Cycle Delay and Reference */
+
+/* Panel Fitting */
+#define PFIT_CONTROL      0x61230  /* Panel Fitting Control */
+#define PFIT_PGM_RATIOS   0x61234  /* Programmed Panel Fitting Ratios */
+
+/* Backlight control */
+#define BLC_PWM_CTL2      0x61250  /* Backlight PWM Control 2 */
+#define BLC_PWM_CTL       0x61254  /* Backlight PWM Control */
+#define BLM_HIST_CTL      0x61260  /* Image BLM Histogram Control */
+
+#define PORT_EN           BIT31
+#define PORT_PIPE_SEL     BIT30
+#define PORT_PIPE_SEL_POS 30
+#define PORT_PIPE_A       0      /* 0 = Pipe A */
+#define PORT_PIPE_B       BIT30  /* 1 = Pipe B */
+#define STALL_MASK        BIT29 + BIT28
+#define STALL_ENABLE      BIT28
+#define SYNC_MASK         BIT15 + BIT11 + BIT10 + BIT4 + BIT3
+#define SYNC_POLARITY     BIT15  /* 1 = Use VGA register */
+#define VSYNC_OUTPUT      BIT11
+#define HSYNC_OUTPUT      BIT10
+#define VSYNC_POLARITY    BIT4
+#define HSYNC_POLARITY    BIT3
+#define FP_DATA_ORDER     BIT14
+#define SUBDATA_ORDER     BIT6
+#define BORDER_EN         BIT7
+#define DISPLAY_EN        BIT2
+#define INTERLACED_BIT    0x00100000
+#define RGBA_BITS         0x00030000
+
+
+
+/*-----------------------------------------------------------------------------
+ * Display Pipeline A Register ( 70000h - 70024h )
+ *---------------------------------------------------------------------------*/
+#define PIPEA_SCANLINE_COUNT   0x70000  /* Pipe A Scan Line Count (RO) */
+#define PIPEA_SCANLINE_COMPARE 0x70004  /* Pipe A SLC Range Compare (RO) */
+#define PIPEA_CONF             0x70008  /* Pipe A Configuration */
+#define PIPE_STATUS_OFFSET     0x1C
+#define PIPEAGCMAXRED          0x70010  /* Pipe A Gamma Correct. Max Red */
+#define PIPEAGCMAXGRN          0x70014  /* Pipe A Gamma Correct. Max Green */
+#define PIPEAGCMAXBLU          0x70018  /* Pipe A Gamma Correct. Max Blue */
+#define PIPEA_STAT             0x70024  /* Pipe A Display Status */
+#define PIPEA_DISP_ARB_CTRL    0x70030  /* Display Arbitration Control */
+#define FW_1                   0x70034
+#define FW_2                   0x70038
+#define FW_3                   0x7003C
+#define PIPEA_FRAME_HIGH       0x70040  /* Pipe A Frame Count High */
+#define PIPEA_FRAME_PIXEL      0x70044  /* Pipe A Frame Cnt Low & pixel count */
+#define FW_4                   0x70050
+#define FW_5                   0x70054
+#define FW_6                   0x70058
+
+#define PIPE_PIXEL_MASK        0x00ffffff
+#define PIPE_FRAME_HIGH_MASK   0x0000ffff
+#define PIPE_FRAME_LOW_MASK    0xff000000
+#define PIPE_FRAME_LOW_SHIFT   24
+
+
+/*-----------------------------------------------------------------------------
+ * Display Pipeline B Register ( 71000h - 71024h )
+ *---------------------------------------------------------------------------*/
+#define PIPEB_SCANLINE_COUNT   0x71000 /* Pipe B Disp Scan Line Count Reg */
+#define PIPEB_SCANLINE_COMPARE 0x71004 /* Pipe B Disp Scan Line Cnt Range Cmp */
+#define PIPEB_CONF             0x71008 /* Pipe B Pixel Pipeline Config Reg */
+#define PIPEBGCMAXRED          0x71010 /* Pipe B Gamma Correct. Max Red */
+#define PIPEBGCMAXGRN          0x71014 /* Pipe B Gamma Correct. Max Green */
+#define PIPEBGCMAXBLU          0x71018 /* Pipe B Gamma Correct. Max Blue */
+#define PIPEB_STAT             0x71024 /* Display Status Select Register */
+#define PROG_STALL             0x6102C /* Programmable cDVO Stall Register */
+#define PIPEB_FRAME_HIGH       0x71040 /* Pipe B Frame Count High */
+#define PIPEB_FRAME_PIXEL      0x71044 /* Pipe B Frame Cnt Low and pixel cnt */
+
+
+#define VBLANK_EVN_STS_EN   BIT20
+#define VBLANK_ODD_STS_EN   BIT21
+#define VBLANK_EVN_STS      BIT4
+#define VBLANK_ODD_STS      BIT5
+
+
+/* following bit flag defs can be re-used for Pipe-B */
+#define PIPE_ENABLE       BIT31
+#define PIPE_LOCK         BIT29
+#define GAMMA_MODE        BIT24
+#define HOT_PLUG_EN       BIT26
+/* Setting this bit to a 1 in the PIPE{A|B}_STAT register, enables the
+ * consideration of vertical sync interrupts.
+ */
+#define VSYNC_STS_EN      BIT25
+#define INTERLACE_EN	  BIT23|BIT22
+/* Setting this bit to a 1 in the PIPE{A|B}_STAT register, enables the
+ * consideration of vertical blank interrupts.
+ */
+#define VBLANK_STS_EN     BIT17
+#define HOT_PLUG_STS      BIT10
+#define VSYNC_STS         BIT9
+#define VBLANK_STS        BIT1
+/* The following are "sticky" status bits in the PIPE{A|B}_STAT register.  They
+ * are cleared by writing a 1 to them.  Theres is code that reads a
+ * PIPE{A|B}_STAT register into a variable, then modifies the variable, and
+ * writes it back to the register.  These bits should be treated specially, so
+ * as not to inadvertantly clear the status bits, so that other code looking
+ * for those bits to be set won't miss it.
+ */
+#define PIPESTAT_STS_BITS ((unsigned long) BIT31 | BIT13 | BIT12 | BIT11 | \
+	VSYNC_STS | BIT8 | BIT5 | BIT4 | VBLANK_STS | BIT0)
+
+/*-----------------------------------------------------------------------------
+ * cDVO Registers
+ * --------------------------------------------------------------------------*/
+#define CDVO_CTRL	  0x07000  /* cDVO Control Register */
+#define CDVO_SLEW_RATE	  0x07004  /* cDVO Slew Rate Register */
+#define CDVO_STRENGTH	  0x07008  /* cDVO Strength Register */
+#define CDVO_RCOMP_UPDATE 0x0700C  /* cDVO RCOMP update Register */
+
+/*-----------------------------------------------------------------------------
+ * TNC_SDVOFAM Registers
+ * --------------------------------------------------------------------------*/
+#define DPLL_ANACORE_CTRL 	  0x06300
+#define DPLL_MISC	  	  0x06304
+#define DPLL_MONITOR	  	  0x06308
+#define DPLL_MONITOR2	  	  0x0630C
+#define DPLL_TEST_COMPARATOR      0x06310
+#define SDVOB_TX_LSKEW	          0x06314
+
+/*-----------------------------------------------------------------------------
+ * Hardware Cursor Register Definitions (70080h - 7009Ch)
+ *---------------------------------------------------------------------------*/
+#define CUR_A_CNTR        0x70080  /*Cursor A Control */
+#define CUR_B_CNTR        0x700C0
+#define CUR_BASE_OFFSET   0x4
+#define CUR_POS_OFFSET    0x8
+#define CUR_PAL0_OFFSET   0x10
+#define CUR_PAL1_OFFSET   0x14
+#define CUR_PAL2_OFFSET   0x18
+#define CUR_PAL3_OFFSET   0x1C
+
+/* Define these for ease of reference */
+#define CURSOR_A_BASE     CUR_A_CNTR + CUR_BASE_OFFSET
+#define CURSOR_A_POS      CUR_A_CNTR + CUR_POS_OFFSET
+#define CURSOR_A_PAL0     CUR_A_CNTR + CUR_PAL0_OFFSET
+#define CURSOR_A_PAL1     CUR_A_CNTR + CUR_PAL1_OFFSET
+#define CURSOR_A_PAL2     CUR_A_CNTR + CUR_PAL2_OFFSET
+#define CURSOR_A_PAL3     CUR_A_CNTR + CUR_PAL3_OFFSET
+#define CURSOR_B_BASE     CUR_B_CNTR + CUR_BASE_OFFSET
+#define CURSOR_B_POS      CUR_B_CNTR + CUR_POS_OFFSET
+#define CURSOR_B_PAL0     CUR_B_CNTR + CUR_PAL0_OFFSET
+#define CURSOR_B_PAL1     CUR_B_CNTR + CUR_PAL1_OFFSET
+#define CURSOR_B_PAL2     CUR_B_CNTR + CUR_PAL2_OFFSET
+#define CURSOR_B_PAL3     CUR_B_CNTR + CUR_PAL3_OFFSET
+
+/*-----------------------------------------------------------------------------
+ * Display Plane A Register Definitions (70180h - 70188h)
+ *---------------------------------------------------------------------------*/
+#define DSPAAFLIP    	0x7017C
+#define DSPACNTR        0x70180  /* Display Plane A */
+#define DSPALINOFF      0x70184  /* Display A Linear Offset */
+#define DSPASTRIDE      0x70188  /* Display A Stride */
+#define DSPAKEYVAL      0x70194  /* Sprite color key value */
+#define DSPAKEYMASK     0x70198  /* Sprite color key mask */
+#define DSPASURF        0x7019C  /* Display A Suface base address */
+#define DSPATILEOFF     0x701A4  /* Display A Tiled Offset */
+
+/*-----------------------------------------------------------------------------
+ * Display Plane B Register Definitions (71180h - 71188h)
+ *---------------------------------------------------------------------------*/
+#define DSPBFLIP        0x7117C  /* Display B Async flip */
+#define DSPBCNTR        0x71180  /* Display Plane B */
+#define DSPBLINOFF      0x71184  /* Display B Linear Offset */
+#define DSPBSTRIDE      0x71188  /* Display B Stride */
+#define DSPBKEYVAL      0x71194  /* Sprite color key value */
+#define DSPBKEYMASK     0x71198  /* Sprite color key mask */
+#define DSPBSURF        0x7119C  /* Display B Suface base address */
+#define DSPBTILEOFF     0x711A4  /* Display B Tiled Offset */
+
+/*-----------------------------------------------------------------------------
+ * Source Format Definition for DSPxCNTR
+ *---------------------------------------------------------------------------*/
+#define DSPxCNTR_ARGB_8888			0x1C000000
+#define DSPxCNTR_RGB_8888			0x18000000
+#define DSPxCNTR_RGB_565			0x14000000
+#define DSPxCNTR_RGB_555			0x10000000
+#define DSPxCNTR_RGB_8				0x08000000
+#define DSPxCNTR_SRC_FMT_MASK			0x3C000000 /*mask for above*/
+
+/*-----------------------------------------------------------------------------
+ * Display Plane C Register Definitions (72180h - 72188h)
+ *---------------------------------------------------------------------------*/
+#define DSPCCNTR        0x72180  /* Display Plane C */
+#define DSPCLINOFF      0x72184  /* Display C Linear Offset */
+#define DSPCSTRIDE      0x72188  /* Display C Stride */
+#define DSPCPOS         0x7218C  /* Display C Position */
+#define DSPCSIZE        0x72190  /* Display C Size */
+#define DSPCKEYMINVAL   0x72194  /* Sprite color key Min */
+#define DSPCKEYMASK     0x72198  /* Sprite color key mask */
+#define DSPCSURF        0x7219C  /* Display C Suface base address */
+#define DSPCKEYMAXVAL   0x721A0  /* Display C Sprint color key Max */
+#define DSPCTILEOFF     0x721A4  /* Display C Tiled Offset */
+#define DSPCCONTALPHA	0x721A8	 /* Display C Constant Alpha */
+
+#define DCLRC0          0x721D0  /* Display C Color Correction 0 */
+#define DCLRC1          0x721D4  /* Display C Color Correction 1 */
+#define DPYC_GAMC5      0x721E0  /* Display C Gamma Correction 5 */
+#define DPYC_GAMC4      0x721E4  /* Display C Gamma Correction 4 */
+#define DPYC_GAMC3      0x721E8  /* Display C Gamma Correction 3 */
+#define DPYC_GAMC2      0x721EC  /* Display C Gamma Correction 2 */
+#define DPYC_GAMC1      0x721F0  /* Display C Gamma Correction 1 */
+#define DPYC_GAMC0      0x721F4  /* Display C Gamma Correction 0 */
+
+#define PLANE_ENABLE    BIT31
+#define GAMMA_ENABLE    BIT30
+#define BPP_MASK        BIT29 + BIT28 + BIT27 + BIT26
+#define BPP_POS         26
+#define STEREO_ENABLE   BIT25
+#define PIPE_SEL        BIT24
+#define PIPE_SEL_POS    24
+#define PIXEL_MULTIPLY  BIT21 + BIT20
+#define STEREO_POLARITY BIT18
+
+/* Common offsets for all Display Pipeline registers */
+#define DSP_LINEAR_OFFSET 0x04  /* Offset from the control reg */
+#define DSP_STRIDE_OFFSET 0x08  /* Offset from the control reg */
+#define DSP_SIZE_OFFSET   0x10  /* Offset from the control reg */
+#define DSP_START_OFFSET  0x1c  /* Offset from the control reg */
+#define DSP_TOFF_OFFSET   0x24  /* Offset from the control reg */
+
+/*-----------------------------------------------------------------------------
+ * VGA Display Plane Control Register Definitions (71400h)
+ *---------------------------------------------------------------------------*/
+#define VP00             0x71400
+#define VGACNTRL         0x71400  /* VGA Display Plane Control Register */
+#define VGA_DOUBLE       BIT30
+#define VGA_PIPE         BIT29
+#define VGA_CENTER_MASK  BIT25 + BIT24
+#define VGA_CENTER_1     BIT25
+#define VGA_CENTER_0     BIT24
+#define VGA_PAL_READ     BIT23
+#define VGA_PAL_MASK     BIT22 + BIT21
+#define VGA_PALA_DISABLE BIT22
+#define VGA_PALB_DISABLE BIT21
+#define DAC_8_BIT	     BIT20
+#define VGA_8_DOT	     BIT18
+
+#define ADD_ID           0x71408  /* ADD Card ID Register*/
+
+/*-----------------------------------------------------------------------------
+ * Overlay Plane Control Register Definitions (30000h)
+ *---------------------------------------------------------------------------*/
+#define OVADD            0x30000  /* Overlay Control */
+
+/*-----------------------------------------------------------------------------
+ * VBIOS Software flags  00h - 0Fh
+ *---------------------------------------------------------------------------*/
+#define DSP_CHICKENBITS	0x70400  /* Chicken Bit */
+#define SWFABASE        0x70410  /* Software flags A Base Addr */
+#define SWF00           0x70410
+#define SWF01           0x70414
+#define SWF02           0x70418
+#define SWF03           0x7041C
+#define SWF04           0x70420
+#define SWF05           0x70424
+#define SWF06           0x70428
+#define SWF07           0x7042C
+#define SWF08           0x70430
+#define SWF09           0x70434
+#define SWF0A           0x70438
+#define SWF0B           0x7043C
+#define SWF0C           0x70440
+#define SWF0D           0x70444
+#define SWF0E           0x70448
+#define SWF0F           0x7044C
+
+/*-----------------------------------------------------------------------------
+ * VBIOS Software flags  10h - 1Fh
+ *---------------------------------------------------------------------------*/
+#define SWFBBASE        0x71410  /* Software flags B Base Addr */
+#define SWF10           0x71410
+#define SWF11           0x71414
+#define SWF12           0x71418
+#define SWF13           0x7141C
+#define SWF14           0x71420
+#define SWF15           0x71424
+#define SWF16           0x71428
+#define SWF17           0x7142C
+#define SWF18           0x71430
+#define SWF19           0x71434
+#define SWF1A           0x71438
+#define SWF1B           0x7143C
+#define SWF1C           0x71440
+#define SWF1D           0x71444
+#define SWF1E           0x71448
+#define SWF1F           0x7144C
+
+#define	SWF30			0x72414
+#define SWF31			0x72418
+#define SWF32			0x7241C
+
+/*-----------------------------------------------------------------------------
+ * Software Flag Registers (71410h - 71428h)
+ *---------------------------------------------------------------------------*/
+/* Map old software flag names to their new Gen4 names */
+#define SF00  SWF10
+#define SF01  SWF11
+#define SF02  SWF12
+#define SF03  SWF13
+#define SF04  SWF14
+#define SF05  SWF15
+#define SF06  SWF16
+
+/*-----------------------------------------------------------------------------
+ * GPIO Control Registers(05000h - 05023h)
+ *---------------------------------------------------------------------------*/
+/* GPIO registers 0x0500 - 0x500C is reserved */
+#define GPIOCTL_1	0x05014
+#define GPIOCTL_2	0x05018
+#define GPIOCTL_4	0x05020
+
+/*-----------------------------------------------------------------------------
+ * Scratch register to be used as additional parameter in SMI
+ *---------------------------------------------------------------------------*/
+#define SCRATCH_SWF6         0x71428
+
+/*-----------------------------------------------------------------------------
+ * Miscellaneous registers
+ ----------------------------------------------------------------------------*/
+#define HW_ST_PAGE_ADDR      0x02080
+
+/*-----------------------------------------------------------------------------
+ * GMBUS : I2C Bus Types
+ ----------------------------------------------------------------------------*/
+//#define GMBUS_ANALOG_DDC    1
+
+//#define GMBUS_INT_LVDS_DDC  2
+#define I2C_INT_LVDS_DDC  2
+
+#define GMBUS_DVO_REG       3
+
+#define GMBUS_DVOB_DDC      4
+//#define GMBUS_DVOC_DDC      5
+
+/*-----------------------------------------------------------------------------
+ * LPC Register Offsets. Used for LVDS GPIO Bit Bashing. Registers are part
+ * Atom E6xx [D31:F0]
+ ----------------------------------------------------------------------------*/
+#define RGEN    0x20
+#define RGIO    0x24
+#define RGLVL   0x28
+#define RGTPE   0x2C
+#define RGTNE   0x30
+#define RGGPE   0x34
+#define RGSMI   0x38
+#define RGTS    0x3C
+
+
+#endif /* _REGS_H_ */
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/sgx.h b/drivers/gpu/drm/emgd/emgd/include/tnc/sgx.h
new file mode 100644
index 0000000..c43d490
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/sgx.h
@@ -0,0 +1,36 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sgx.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  These are the defines specific to the SGX engine code.
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _TNC_SGX_H
+#define _TNC_SGX_H
+
+#include<plb/sgx.h>
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/state3d.h b/drivers/gpu/drm/emgd/emgd/include/tnc/state3d.h
new file mode 100644
index 0000000..b32aea2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/state3d.h
@@ -0,0 +1,43 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: state3d.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is an inter-module header file for manipulating the 3D
+ *  State variables.
+ *-----------------------------------------------------------------------------
+ */
+/*
+ * This is referenced from Napa code. For Poulsbo, the HW spec may be
+ * different. Be ready for the new change.
+ */
+
+#ifndef _TNC_STATE3D_H
+#define _TNC_STATE3D_H
+
+#include <plb/state3d.h>
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/tnc/state3d_plb.h b/drivers/gpu/drm/emgd/emgd/include/tnc/state3d_plb.h
new file mode 100644
index 0000000..fd5e628
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/tnc/state3d_plb.h
@@ -0,0 +1,38 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: state3d_plb.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is an inter-module header file for manipulating the 3D
+ *  State variables.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _STATE3D_TNC_H
+#define _STATE3D_TNC_H
+
+#include <plb/state3d_plb.h>
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/topaz.h b/drivers/gpu/drm/emgd/emgd/include/topaz.h
new file mode 100755
index 0000000..e14b46c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/topaz.h
@@ -0,0 +1,209 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: topaz.h
+ * $Revision: 1.16 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  These are the defines specific to the Topaz engine code.
+ *-----------------------------------------------------------------------------
+ */
+extern unsigned long _topaz_base;
+#define TOPAZ_BASE _topaz_base
+
+#ifndef _TOPAZ_H
+#define _TOPAZ_H
+
+/* MTX registers */
+#define TNC_TOPAZ_MTX_ENABLE				(TOPAZ_BASE + 0x0000)
+#define TNC_TOPAZ_MTX_STATUS				(TOPAZ_BASE + 0x0008)
+#define TNC_TOPAZ_MTX_KICK				(TOPAZ_BASE + 0x0080)
+#define TNC_TOPAZ_MTX_KICKI				(TOPAZ_BASE + 0x0088)
+#define TNC_TOPAZ_MTX_FAULT0				(TOPAZ_BASE + 0x0090)
+#define TNC_TOPAZ_MTX_REGISTER_READ_WRITE_DATA		(TOPAZ_BASE + 0x00f8)
+#define TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST	(TOPAZ_BASE + 0x00fc)
+#define TNC_TOPAZ_MTX_RAM_ACCESS_DATA_EXCHANGE		(TOPAZ_BASE + 0x0100)
+#define TNC_TOPAZ_MTX_RAM_ACCESS_DATA_TRANSFER		(TOPAZ_BASE + 0x0104)
+#define TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL		(TOPAZ_BASE + 0x0108)
+#define TNC_TOPAZ_MTX_RAM_ACCESS_STATUS			(TOPAZ_BASE + 0x010c)
+#define TNC_TOPAZ_MTX_SOFT_RESET			(TOPAZ_BASE + 0x0200)
+#define TNC_TOPAZ_MTX_SYSC_CDMAC			(TOPAZ_BASE + 0x0340)
+#define TNC_TOPAZ_MTX_SYSC_CDMAA			(TOPAZ_BASE + 0x0344)
+#define TNC_TOPAZ_MTX_SYSC_CDMAS0			(TOPAZ_BASE + 0x0348)
+#define TNC_TOPAZ_MTX_SYSC_CDMAS1			(TOPAZ_BASE + 0x034c)
+#define TNC_TOPAZ_MTX_SYSC_CDMAT			(TOPAZ_BASE + 0x0350)
+/* Topaz Registers */
+#define TNC_START_OFFSET			(0x02000)
+#define TNC_IMG_TOPAZ_REG_BASE			(TOPAZ_BASE + TNC_START_OFFSET)
+#define TNC_TOPAZ_IMG_TOPAZ_SRST		(TNC_IMG_TOPAZ_REG_BASE + 0x0000)
+#define TNC_TOPAZ_IMG_TOPAZ_INTSTAT		(TNC_IMG_TOPAZ_REG_BASE + 0x0004)
+#define TNC_TOPAZ_IMG_TOPAZ_INTENAB		(TNC_IMG_TOPAZ_REG_BASE + 0x0008)
+#define TNC_TOPAZ_IMG_TOPAZ_INTCLEAR		(TNC_IMG_TOPAZ_REG_BASE + 0x000c)
+#define TNC_TOPAZ_IMG_TOPAZ_MAN_CLK_GATE	(TNC_IMG_TOPAZ_REG_BASE + 0x0010)
+#define TNC_TOPAZ_IMG_TOPAZ_AUTO_CLK_GATE	(TNC_IMG_TOPAZ_REG_BASE + 0x0014)
+#define TNC_TOPAZ_IMG_TOPAZ_RSVD0		(TNC_IMG_TOPAZ_REG_BASE + 0x03b0)
+#define TNC_TOPAZ_TOPAZ_MTX_C_RATIO		(TNC_IMG_TOPAZ_REG_BASE + 0x0018)
+#define TNC_TOPAZ_IMG_TOPAZ_CORE_ID		(TNC_IMG_TOPAZ_REG_BASE + 0x03c0)
+#define TNC_TOPAZ_IMG_TOPAZ_CORE_REV		(TNC_IMG_TOPAZ_REG_BASE + 0x03d0)
+#define TNC_TOPAZ_IMG_TOPAZ_CORE_DES1		(TNC_IMG_TOPAZ_REG_BASE + 0x03e0)
+#define TNC_TOPAZ_IMG_TOPAZ_CORE_DES2		(TNC_IMG_TOPAZ_REG_BASE + 0x03f0)
+#define TNC_TOPAZ_MMU_STATUS			(TNC_IMG_TOPAZ_REG_BASE + 0x001c)
+#define TNC_TOPAZ_MMU_MEM_REQ			(TNC_IMG_TOPAZ_REG_BASE + 0x0020)
+#define TNC_TOPAZ_MMU_CONTROL0			(TNC_IMG_TOPAZ_REG_BASE + 0x0024)
+#define TNC_TOPAZ_MMU_CONTROL1			(TNC_IMG_TOPAZ_REG_BASE + 0x0028)
+#define TNC_TOPAZ_MMU_DIR_LIST_BASE0		(TNC_IMG_TOPAZ_REG_BASE + 0x0030)
+#define TNC_TOPAZ_MMU_TILE0			(TNC_IMG_TOPAZ_REG_BASE + 0x0034)
+#define TNC_TOPAZ_MMU_BANK_INDEX		(TNC_IMG_TOPAZ_REG_BASE + 0x0038)
+#define TNC_TOPAZ_MTX_DEBUG			(TNC_IMG_TOPAZ_REG_BASE + 0x003c)
+#define TNC_TOPAZ_IMG_TOPAZ_DMAC_MODE		(TNC_IMG_TOPAZ_REG_BASE + 0x0040)
+#define TNC_TOPAZ_RTM				(TNC_IMG_TOPAZ_REG_BASE + 0x0044)
+#define TNC_TOPAZ_RTM_VALUE			(TNC_IMG_TOPAZ_REG_BASE + 0x0048)
+/* MVEA Registers */
+#define TNC_MVEA_START_OFFSET			(0x03000)
+#define TNC_IMG_MVEA_REG_BASE			(TOPAZ_BASE + TNC_MVEA_START_OFFSET)
+#define TNC_TOPAZ_IMG_MVEA_SRST			(TNC_IMG_MVEA_REG_BASE + 0x0000)
+#define TNC_TOPAZ_IMG_MVEA_INTSTAT		(TNC_IMG_MVEA_REG_BASE + 0x0004)
+#define TNC_TOPAZ_IMG_MVEA_INTENAB		(TNC_IMG_MVEA_REG_BASE + 0x0008)
+#define TNC_TOPAZ_IMG_MVEA_INTCLEAR		(TNC_IMG_MVEA_REG_BASE + 0x000c)
+#define TNC_TOPAZ_IMG_MVEA_INT_COMB_SEL		(TNC_IMG_MVEA_REG_BASE + 0x0010)
+#define TNC_TOPAZ_IMG_MVEA_RSVD0		(TNC_IMG_MVEA_REG_BASE + 0x03b0)
+#define TNC_TOPAZ_IMG_MVEA_CORE_ID		(TNC_IMG_MVEA_REG_BASE + 0x03c0)
+#define TNC_TOPAZ_IMG_MVEA_CORE_REV		(TNC_IMG_MVEA_REG_BASE + 0x03d0)
+#define TNC_TOPAZ_MVEA_START			(TNC_IMG_MVEA_REG_BASE + 0x0014)
+#define TNC_TOPAZ_MVEA_BUSY			(TNC_IMG_MVEA_REG_BASE + 0x0018)
+#define TNC_TOPAZ_MVEA_DMACMDFIFO_WAIT		(TNC_IMG_MVEA_REG_BASE + 0x001c)
+#define TNC_TOPAZ_MVEA_DMACMDFIFO_STATUS	(TNC_IMG_MVEA_REG_BASE + 0x0020)
+#define TNC_TOPAZ_MVEA_AUTO_CLOCK_GATING	(TNC_IMG_MVEA_REG_BASE + 0x0024)
+#define TNC_TOPAZ_MVEA_MAN_CLOCK_GATING		(TNC_IMG_MVEA_REG_BASE + 0x0028)
+#define TNC_TOPAZ_MB_PERFORMANCE_RESULT		(TNC_IMG_MVEA_REG_BASE + 0x002c)
+#define TNC_TOPAZ_MB_PERFORMANCE_MB_NUMBER	(TNC_IMG_MVEA_REG_BASE + 0x0030)
+#define TNC_TOPAZ_HW_MB_PERFORMANCE_RESULT	(TNC_IMG_MVEA_REG_BASE + 0x0034)
+#define TNC_TOPAZ_HW_MB_PERFORMANCE_MB_NUMBER	(TNC_IMG_MVEA_REG_BASE + 0x0038)
+/* VLC Registers */
+#define TNC_VLC_START_OFFSET			(0x05000)
+#define TNC_IMG_VLC_REG_BASE			(TOPAZ_BASE + TNC_VLC_START_OFFSET)
+#define TNC_TOPAZ_VLC_CONTROL			(TNC_IMG_VLC_REG_BASE + 0x0000)
+#define TNC_TOPAZ_VLC_STATUS			(TNC_IMG_VLC_REG_BASE + 0x0004)
+#define TNC_TOPAZ_VLC_INFO_0			(TNC_IMG_VLC_REG_BASE + 0x0008)
+#define TNC_TOPAZ_VLC_INFO_1			(TNC_IMG_VLC_REG_BASE + 0x000c)
+#define TNC_TOPAZ_VLC_INFO_2			(TNC_IMG_VLC_REG_BASE + 0x0010)
+#define TNC_TOPAZ_VLC_STUFF_HEAD_CTRL		(TNC_IMG_VLC_REG_BASE + 0x0014)
+#define TNC_TOPAZ_VLC_HEADER_FIFO		(TNC_IMG_VLC_REG_BASE + 0x0018)
+#define TNC_TOPAZ_VLC_HEADER_CTRL		(TNC_IMG_VLC_REG_BASE + 0x001c)
+#define TNC_TOPAZ_VLC_HEADER_STATUS		(TNC_IMG_VLC_REG_BASE + 0x0020)
+#define TNC_TOPAZ_VLC_RATE_CTRL_0		(TNC_IMG_VLC_REG_BASE + 0x0024)
+#define TNC_TOPAZ_VLC_RATE_CTRL_1		(TNC_IMG_VLC_REG_BASE + 0x002c)
+#define TNC_TOPAZ_VLC_BUFFER_SIZE		(TNC_IMG_VLC_REG_BASE + 0x0034)
+#define TNC_TOPAZ_VLC_SIGNATURE_0		(TNC_IMG_VLC_REG_BASE + 0x0038)
+#define TNC_TOPAZ_VLC_SIGNATURE_1		(TNC_IMG_VLC_REG_BASE + 0x003c)
+#define TNC_TOPAZ_VLC_SIGNATURE_2		(TNC_IMG_VLC_REG_BASE + 0x0040)
+#define TNC_TOPAZ_VLC_SIGNATURE_3		(TNC_IMG_VLC_REG_BASE + 0x0044)
+#define TNC_TOPAZ_VLC_SIGNATURE_4		(TNC_IMG_VLC_REG_BASE + 0x0048)
+#define TNC_TOPAZ_VLC_JPEG_CFG			(TNC_IMG_VLC_REG_BASE + 0x004c)
+#define TNC_TOPAZ_VLC_PERFORMANCE_0		(TNC_IMG_VLC_REG_BASE + 0x0050)
+#define TNC_TOPAZ_VLC_PERFORMANCE_1		(TNC_IMG_VLC_REG_BASE + 0x0054)
+#define TNC_TOPAZ_VLC_PERFORMANCE_2		(TNC_IMG_VLC_REG_BASE + 0x0058)
+#define TNC_TOPAZ_VLC_IPCM_0			(TNC_IMG_VLC_REG_BASE + 0x005c)
+#define TNC_TOPAZ_VLC_IPCM_1			(TNC_IMG_VLC_REG_BASE + 0x0060)
+#define TNC_TOPAZ_VLC_MPEG4_CFG			(TNC_IMG_VLC_REG_BASE + 0x0064)
+#define TNC_TOPAZ_VLC_MB_PARAMS			(TNC_IMG_VLC_REG_BASE + 0x0068)
+#define TNC_TOPAZ_VLC_RESET			(TNC_IMG_VLC_REG_BASE + 0x006c)
+
+#define WRITEBACK_MEM_SIZE			(4 * 1024)
+
+#define TOPAZ_MTX_PC				(0x00000005)
+#define PC_START_ADDRESS			(0x80900000)
+#define MTX_CODE_BASE				(0x80900000)
+#define MTX_DATA_BASE				(0x82880000)
+#define MTX_CORE_CODE_MEM			(0x10)
+#define MTX_CORE_DATA_MEM			(0x18)
+
+#define MTX_CCB_CTRL_CCB_SIZE			(8)
+#define MTX_CCB_CTRL_INIT_QP			(24)
+
+#define MVEA_BASE				0xA3000
+#define VLC_BASE				0xA5000
+
+#define TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_RNW_MASK 0x00010000
+#define TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_DREADY_MASK 0x80000000
+
+enum tnc_topaz_encode_fw {
+	FW_JPEG = 0,
+	FW_H264_NO_RC,
+	FW_H264_VBR,
+	FW_H264_CBR,
+	FW_H263_NO_RC,
+	FW_H263_VBR,
+	FW_H263_CBR,
+	FW_MPEG4_NO_RC,
+	FW_MPEG4_VBR,
+	FW_MPEG4_CBR,
+	FW_NUM
+};
+
+typedef struct _enc_fw_info {
+	enum tnc_topaz_encode_fw idx;
+	unsigned long *text_size;
+	unsigned long *data_size;
+	unsigned long *data_offset;
+	unsigned long *text;
+	unsigned long *data;
+	char *fw_version;
+}enc_fw_info_t;
+
+struct topaz_cmd_header {
+	union {
+		struct {
+			unsigned long enable_interrupt:1;
+			unsigned long id:7;
+			unsigned long size:8;
+			unsigned long seq:16;
+		};
+		unsigned int val;
+	};
+};
+
+/* commands for topaz,shared with user space driver */
+enum drm_lnc_topaz_cmd {
+	MTX_CMDID_NULL = 0,
+	MTX_CMDID_DO_HEADER = 1,
+	MTX_CMDID_ENCODE_SLICE = 2,
+	MTX_CMDID_WRITEREG = 3,
+	MTX_CMDID_START_PIC = 4,
+	MTX_CMDID_END_PIC = 5,
+	MTX_CMDID_SYNC = 6,
+	MTX_CMDID_ENCODE_ONE_ROW = 7,
+	MTX_CMDID_FLUSH = 8,
+	MTX_CMDID_SW_LEAVE_LOWPOWER = 0x7c,
+	MTX_CMDID_SW_ENTER_LOWPOWER = 0x7e,
+	MTX_CMDID_SW_NEW_CODEC = 0x7f
+};
+
+extern int topaz_setup_fw(igd_context_t *context, enum tnc_topaz_encode_fw codec);
+int process_video_encode_tnc(igd_context_t *context, unsigned long offset, void *virt_addr, unsigned long *fence_id);
+int topaz_init_tnc(unsigned long wb_offset, void *wb_addr, void *firm_addr);
+int topaz_get_fence_id(igd_context_t *context, unsigned long *fence_id);
+int topaz_flush_tnc(igd_context_t *context);
+int topaz_get_frame_skip(igd_context_t *context, unsigned long *frame_skip);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/include/utils.h b/drivers/gpu/drm/emgd/emgd/include/utils.h
new file mode 100644
index 0000000..18fe9c3
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/utils.h
@@ -0,0 +1,174 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: utils.h
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _UTILS_H_
+#define _UTILS_H_
+
+#include <config.h>
+#include <igd_mode.h>
+#include <general.h>
+#include <mode.h>
+
+/* Get the MMIO pointer for a display context */
+#define MMIO(dsp) \
+ EMGD_MMIO((((igd_display_context_t *)dsp)->context->device_context.virt_mmadr))
+
+/* Get the 2nd MMIO pointer for a display context */
+#define MMIO_SDVO(dsp) \
+ EMGD_MMIO(((igd_display_context_t *)dsp)->context->device_context.virt_mmadr_sdvo)
+
+/* Get the STMicro SDVO MMIO pointer for a display context */
+#define MMIO_SDVO_ST(dsp) \
+ EMGD_MMIO(((igd_display_context_t *)dsp)->context->device_context.virt_mmadr_sdvo_st)
+#define MMIO_SDVO_ST_GPIO(dsp) \
+ EMGD_MMIO(((igd_display_context_t *)dsp)->context->device_context.virt_mmadr_sdvo_st_gpio)
+
+/* Definition: value = READ_MMIO_REG(igd_display_context_t *, reg) */
+#define READ_MMIO_REG(dsp, reg) \
+	EMGD_READ32( MMIO(dsp) +(reg) )
+
+/* Definition: WRITE_MMIO_REG(igd_display_context_t *, reg, value) */
+#define WRITE_MMIO_REG(dsp, reg, value) \
+	EMGD_WRITE32(value, ( MMIO(dsp) + (reg) ) )
+
+/* Definition:WRITE_MMIO_REG_BITS(igd_display_context_t *, reg, value, mask) */
+#define WRITE_MMIO_REG_BITS(dsp, reg, data, mask) \
+{                                                 \
+	unsigned long tmp;                            \
+	tmp = READ_MMIO_REG((dsp), (reg));            \
+	tmp &= (~(mask));                             \
+	tmp |= ((data) & (mask));                     \
+	WRITE_MMIO_REG((dsp), (reg), (tmp));          \
+}
+
+#define PORT_TYPE(d) (PORT(d, (d->port_number))->port_type)
+#define PORT_TYPE_DH(dh) \
+	(PORT(dh, (((igd_display_context_t *)dh)->port_number))->port_type)
+
+#ifdef CONFIG_TNC
+/* Based on display port determine which mmio base to use:
+ *       port_type == SDVO ==> use 0:3:0 device mmio
+ *       port_type == LVDS ==> use 0:2:0 device mmio
+ *       port_type == LPC  ==> use 0:31:0 device mmio
+ */
+#define MMIO_TNC(port_type) EMGD_MMIO(get_mmio_tnc(port_type))
+
+#define READ_MMIO_REG_TNC(pt, reg) read_mmio_reg_tnc(pt, reg)
+#define WRITE_MMIO_REG_TNC(pt, reg, value) write_mmio_reg_tnc(pt, reg, value)
+
+/* Defined in micro_mode_tnc.c */
+extern unsigned char *get_mmio_tnc(unsigned long port_type);
+extern unsigned long read_mmio_reg_tnc(unsigned long port_type,
+	unsigned long reg);
+extern void write_mmio_reg_tnc(unsigned long port_type, unsigned long reg,
+	unsigned long value);
+#endif
+
+
+/*
+ * These are temporary macros used only within this header.
+ * Individual config options use these macros to generate macros that look
+ * like this:
+ *
+ * If CONFIG_FOO is defined
+ *  OPT_FOO_SYMBOL(a)
+ *  OPT_FOO_VALUE(a, b)
+ *  OPT_FOO_VOID_CALL(fn)
+ *  OPT_FOO_CALL(fn)
+ */
+#define OPTIONAL_VOID_CALL(fn) fn
+#define OPTIONAL_CALL(fn)			\
+	{								\
+		int __ret;					\
+		__ret = fn;					\
+		if(__ret) {					\
+			EMGD_ERROR_EXIT("EXIT");	\
+			return __ret;			\
+		}							\
+	}
+
+#define OPTIONAL_CALL_RET(ret, fn)      (ret) = (fn)
+
+/*
+ * Debug call macro should be used to call debug printing functions
+ * that will only exist in debug builds.
+ */
+#ifdef DEBUG_BUILD_TYPE
+#define OPT_DEBUG_SYMBOL(sym)      sym
+#define OPT_DEBUG_VALUE(val, alt)  val
+#define OPT_DEBUG_VOID_CALL(fn)    OPTIONAL_VOID_CALL(fn)
+#define OPT_DEBUG_CALL(fn)         OPTIONAL_CALL(fn)
+#define OPT_DEBUG_INLINE
+#else
+#define OPT_DEBUG_SYMBOL(sym)
+#define OPT_DEBUG_VALUE(val, alt)  alt
+#define OPT_DEBUG_VOID_CALL(fn)
+#define OPT_DEBUG_CALL(fn)
+#define OPT_DEBUG_INLINE           static __inline
+#endif
+
+/*
+ * Micro Symbols are only used when CONFIG_MICRO is not defined.
+ */
+#ifndef CONFIG_MICRO
+#define OPT_MICRO_SYMBOL(sym)         sym
+#define OPT_MICRO_VALUE(val, alt)     val
+#define OPT_MICRO_VOID_CALL(fn)       OPTIONAL_VOID_CALL(fn)
+#define OPT_MICRO_CALL(fn)            OPTIONAL_CALL(fn)
+#define OPT_MICRO_CALL_RET(ret, fn)   OPTIONAL_CALL_RET(ret, fn)
+#else
+#define OPT_MICRO_SYMBOL(sym)
+#define OPT_MICRO_VALUE(val, alt)  alt
+#define OPT_MICRO_VOID_CALL(fn)
+#define OPT_MICRO_CALL(fn)
+#define OPT_MICRO_CALL_RET(ret, fn)
+#endif
+
+
+#ifdef CONFIG_PLB
+#define DISPATCH_PLB(p) {PCI_DEVICE_ID_VGA_PLB, p},
+#else
+#define DISPATCH_PLB(p)
+#endif
+#ifdef CONFIG_TNC
+#define DISPATCH_TNC(p) {PCI_DEVICE_ID_VGA_TNC, p},
+#define DISPATCH_TNC_A0(p) {PCI_DEVICE_ID_VGA_TNC_A0, p},
+#define DISPATCH_LNC(p) {PCI_DEVICE_ID_VGA_LNC, p},
+#else
+#define DISPATCH_TNC(p)
+#define DISPATCH_TNC_A0(p)
+#define DISPATCH_LNC(p)
+#endif
+#define DISPATCH_END {0, NULL}
+
+
+#endif // _UTILS_H_
+
diff --git a/drivers/gpu/drm/emgd/emgd/include/vga.h b/drivers/gpu/drm/emgd/emgd/include/vga.h
new file mode 100644
index 0000000..1c9b782
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/include/vga.h
@@ -0,0 +1,116 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: vga.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Header file for common VGA registers and macros.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _HAL_VGA_H
+#define _HAL_VGA_H
+
+#include <igd_vga.h>
+#include <io.h>
+#include "drm_emgd_private.h"
+
+extern void program_pipe_vga(
+	igd_display_context_t *display,
+	igd_timing_info_t *timings);
+
+extern void kms_program_pipe_vga(
+	emgd_crtc_t *emgd_crtc,
+	igd_timing_info_t *timings);
+
+extern void program_plane_vga(
+	igd_display_context_t *display,
+	igd_timing_info_t *timings);
+
+extern void kms_program_plane_vga(
+	unsigned char *mmio,
+	igd_timing_info_t *timings);
+
+extern unsigned long get_vga_color(int i);
+
+extern void set_256_palette(unsigned char *mmio);
+
+extern unsigned char vga_port_offset;
+
+#define READ_VGA(mmio, port, index, data) \
+  EMGD_WRITE8((unsigned char)index, EMGD_MMIO(mmio) + port); \
+  data = EMGD_READ8(EMGD_MMIO(mmio) + port + 1);
+
+#define WRITE_VGA(mmio, port, index, data) \
+  EMGD_WRITE8(index, EMGD_MMIO(mmio) + port); \
+  EMGD_WRITE8(data, EMGD_MMIO(mmio) + port + 1); \
+  EMGD_DEBUG("0x%lx (0x%lx): 0x%lx", (unsigned long) port, \
+		  (unsigned long) index, (unsigned long) data);
+
+#define READ_AR(mmio, index, data) \
+ { \
+	 unsigned char tmp; \
+	 tmp = EMGD_READ8(EMGD_MMIO(mmio) + ST01); \
+     EMGD_WRITE8(index, EMGD_MMIO(mmio) + AR_PORT); \
+     data = EMGD_READ8(EMGD_MMIO(mmio) + ARX_DATA_R); \
+	 tmp = EMGD_READ8(EMGD_MMIO(mmio) + ST01); \
+ }
+
+#define WRITE_AR(mmio, index, data) \
+ { \
+    unsigned char tmp; \
+	tmp = EMGD_READ8(EMGD_MMIO(mmio) + ST01); \
+	EMGD_WRITE8(index, EMGD_MMIO(mmio) + AR_PORT); \
+    EMGD_WRITE8(data, EMGD_MMIO(mmio) + AR_PORT); \
+	EMGD_WRITE8(0x20, EMGD_MMIO(mmio) + AR_PORT); \
+	tmp = EMGD_READ8(EMGD_MMIO(mmio) + ST01); \
+}
+
+/* Status registers */
+#define ST00 0x3C2
+#define ST01 (0x3BA + vga_port_offset)
+
+/* Sequencer Registers */
+#define SR_PORT   0x3c4
+#define SRX_INDEX 0x3C4   /* Index port */
+#define SRX_DATA  0x3C5   /* Data port */
+
+/* Graphics Registers */
+#define GR_PORT   0x3ce
+#define GRX_INDEX 0x3CE   /* Index port */
+#define GRX_DATA  0x3CF   /* Data port */
+
+/* Attribute Controlller Registers */
+#define AR_PORT    0x3c0
+#define ARX_INDEX  0x3C0   /* Index port */
+#define ARX_DATA_R 0x3C1   /* Data port read */
+#define ARX_DATA_W 0x3C0   /* Data port write */
+
+/* CRTC Registers */
+#define CR_PORT   (0x3b4 + vga_port_offset)
+#define CRX_INDEX (0x3b4 + vga_port_offset)  /* Index port */
+#define CRX_DATA (0x3b5 + vga_port_offset)  /* Data port */
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/oal/src/math_fix.c b/drivers/gpu/drm/emgd/emgd/oal/src/math_fix.c
new file mode 100644
index 0000000..84e5811
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/oal/src/math_fix.c
@@ -0,0 +1,138 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: math_fix.c
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains fixed point implementations of math functions
+ *-----------------------------------------------------------------------------
+ */
+
+
+/******************************************************************************
+ * This is a look-up table for natural log for integer values 1 - 255 in
+ * 24i.8f fixed point format.
+ *
+ * This table returns 0 or ln(0), but it is technically undefined.
+ *****************************************************************************/
+static const int ln_table[] =
+  {
+    0,    0,  177,  281,  355,  412,  459,  498,  532,  562,  589,  614,
+    636,  657,  676,  693,  710,  725,  740,  754,  767,  779,  791,  803,
+    814,  824,  834,  844,  853,  862,  871,  879,  887,  895,  903,  910,
+    917,  924,  931,  938,  944,  951,  957,  963,  969,  975,  980,  986,
+    991,  996, 1001, 1007, 1012, 1016, 1021, 1026, 1030, 1035, 1039, 1044,
+    1048, 1052, 1057, 1061, 1065, 1069, 1073, 1076, 1080, 1084, 1088, 1091,
+    1095, 1098, 1102, 1105, 1109, 1112, 1115, 1119, 1122, 1125, 1128, 1131,
+    1134, 1137, 1140, 1143, 1146, 1149, 1152, 1155, 1158, 1160, 1163, 1166,
+    1168, 1171, 1174, 1176, 1179, 1181, 1184, 1186, 1189, 1191, 1194, 1196,
+    1199, 1201, 1203, 1206, 1208, 1210, 1212, 1215, 1217, 1219, 1221, 1223,
+    1226, 1228, 1230, 1232, 1234, 1236, 1238, 1240, 1242, 1244, 1246, 1248,
+    1250, 1252, 1254, 1256, 1258, 1260, 1261, 1263, 1265, 1267, 1269, 1270,
+    1272, 1274, 1276, 1278, 1279, 1281, 1283, 1284, 1286, 1288, 1289, 1291,
+    1293, 1294, 1296, 1298, 1299, 1301, 1302, 1304, 1306, 1307, 1309, 1310,
+    1312, 1313, 1315, 1316, 1318, 1319, 1321, 1322, 1324, 1325, 1327, 1328,
+    1329, 1331, 1332, 1334, 1335, 1336, 1338, 1339, 1341, 1342, 1343, 1345,
+    1346, 1347, 1349, 1350, 1351, 1353, 1354, 1355, 1356, 1358, 1359, 1360,
+    1361, 1363, 1364, 1365, 1366, 1368, 1369, 1370, 1371, 1372, 1374, 1375,
+    1376, 1377, 1378, 1380, 1381, 1382, 1383, 1384, 1385, 1387, 1388, 1389,
+    1390, 1391, 1392, 1393, 1394, 1395, 1397, 1398, 1399, 1400, 1401, 1402,
+    1403, 1404, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1412, 1413, 1415,
+    1416, 1417, 1418, 1419
+  };
+
+
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: os_pow_fix()
+ *
+ * Parameters:
+ *    [IN] base:  the base, should be between 0 and 255.
+ *    [IN] power:  this must be in 24i.8f format.
+ *
+ * Description:
+ *    This function uses the Taylor Series to approximate the power function
+ *    using fixed-point math.  See this website for the math background
+ *    http://www.efunda.com/math/taylor_series/exponential.cfm
+ *
+ *    This function was originally designed for gamma correction.  When the
+ *    base value is between 1 - 255 and the power is between 0.1 and 2, this
+ *    function will produce an approximation that is within 2% of the "real"
+ *    function.
+ *
+ * Returns:
+ *    The result in 24i.8f format
+ *
+ *----------------------------------------------------------------------------
+ */
+
+unsigned int os_pow_fix( const int base, const int power )
+{
+  /* For some reason using "const unsigned int" causes a compiler error
+   * when I use it below when I declare "result[APPROXIMATE_TERMS]".  So
+   * switched these from "const unsigned .." to #define
+   */
+  #define APPROXIMATE_TERMS     40
+#define FRACTION_BITS         8    /* num of bits for fraction */
+
+  unsigned int       result[APPROXIMATE_TERMS];
+  unsigned int       i, power_loop;
+  unsigned int       total  = 0;
+  int                ln_x   = ln_table[base];  /* ln_x is in 24i.8f format */
+
+
+  /* nothing to do if we get 0 */
+  if (0 == base) {
+    return 0;
+  } else {
+    ln_x = ln_table[base];
+  }
+
+  /* We approximate the result using APPROXIMATE_TERMS terms */
+  for (i = 0; i < APPROXIMATE_TERMS; i++)    {
+
+    result[i] = 1 << FRACTION_BITS;
+
+    /* Need to be very careful about the order in which we are multiplying
+     * multiplying and dividing because we don't want any overflow.  In
+     * addition, every time we multiply 2 fixed point numbers, we need to
+     * shift the result by FRACTION_BITS to maintain the radix point.
+     */
+    for( power_loop = 0; power_loop < i; power_loop++ )    {
+      result[i] *= ln_x;
+      result[i] /= (power_loop + 1);
+      result[i] >>=FRACTION_BITS;
+      result[i] *= power;
+      result[i] >>=FRACTION_BITS;
+
+    }
+
+    total += result[i];
+
+  }
+
+  return total;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/oal/src/memmap.c b/drivers/gpu/drm/emgd/emgd/oal/src/memmap.c
new file mode 100644
index 0000000..2ee0812
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/oal/src/memmap.c
@@ -0,0 +1,55 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: memmap.c
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the XFree86 implementations of the OAL
+ *  memory.h abstractions.
+ *-----------------------------------------------------------------------------
+ */
+#include <io.h>
+
+#ifndef _OAL_LINUX_KERNEL_IO_MEMMAP_H
+#define _OAL_LINUX_KERNEL_IO_MEMMAP_H
+
+
+void * os_map_io_to_mem_nocache(
+				unsigned long base_address,
+				unsigned long size
+				)
+{
+  return ioremap(base_address, size);
+}
+
+void os_unmap_io_from_mem(
+			  void * virt_addr,
+			  unsigned long size
+			  )
+{
+  iounmap(virt_addr);
+}
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/oal/src/pci.c b/drivers/gpu/drm/emgd/emgd/oal/src/pci.c
new file mode 100644
index 0000000..ff8a28c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/oal/src/pci.c
@@ -0,0 +1,277 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pci.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains Linux kernel space PCI API for the OAL abstractions.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <memory.h>
+#include <pci.h>
+#include <linux/pci.h>
+#include <io.h>
+
+#if defined(CONFIG_VGA_ARB)
+#include <linux/vgaarb.h>
+#endif
+
+
+//
+// This is our local representation of a PCI device.
+// This struct is private to this file, and exposed to calling functions
+// only through an opaque "os_pci_dev_t" handle.
+//
+typedef struct _linuxkernel_pci {
+  struct pci_dev *dev;
+  unsigned int bus;
+  unsigned int slot;
+  unsigned int func;
+}linuxkernel_pci_t;
+
+/*----------------------------------------------------------------------
+ * Function:
+ *pci_find_device_generic
+ * Parameters:
+ *unsigned short vendor,
+ *unsigned short device,
+ *os_pci_dev_t os_pci_dev
+ * Description:
+ *This function finds a PCI device by going through 255 buses, 32 devices
+ *and 8 functions and tries to match each ones vendor and device ids with
+ *the ones given to it as parameters. Stops for the first device it finds
+ *with a matching vendor and device, so it will not find multiple devices.
+ *This is _NOT_ an exported OAL
+ *function.
+ * Returns:
+ *os_pci_dev_t
+ *---------------------------------------------------------------------
+ */
+static os_pci_dev_t pci_find_device_generic(
+					    unsigned short vendor_id,
+					    unsigned short device_id,
+					    os_pci_dev_t pci_dev_handle
+					    )
+{
+  struct pci_dev *our_device; // Kernel struct for a PCI device
+  linuxkernel_pci_t *pdev; // Our struct for a PCI device.
+
+    // Locate the device, and lock it. Start search at the start of the list.
+    our_device = pci_get_device(vendor_id, device_id, NULL);
+    // If we didn't find it, return an error.
+    if (!our_device){
+      return (os_pci_dev_t)NULL;
+    }
+
+    // Get the pointer to the destination for the data.
+    // pci_dev_handle is an opaque pointer to a linuxkernel_pci_t struct
+    // If there isn't one, allocate it.
+    pdev = (linuxkernel_pci_t *)pci_dev_handle;
+    if(!pdev) {
+
+      // Caller did not supply a handle to a PCI device struct.
+      // Allocate one.
+      pdev = (linuxkernel_pci_t *)OS_ALLOC(sizeof(linuxkernel_pci_t));
+      if(!pdev) {
+	return (os_pci_dev_t)NULL;
+      }
+      // Zero the destination memory
+      memset(pdev, 0, sizeof(linuxkernel_pci_t));
+    }
+
+    // Copy over from the kernel struct to our struct.
+    // It is safe to copy a pointer to the pci_dev, since we
+    // have a lock on it.
+    pdev->dev = our_device;
+    pdev->bus = our_device->bus->number;
+    pdev->slot = PCI_SLOT(our_device->devfn);
+    pdev->func = PCI_FUNC(our_device->devfn);
+
+    return (os_pci_dev_t)pdev;
+}
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *os_pci_find_device
+ *
+ *  Parameters:
+ *unsigned short vendor,
+ *unsigned short device,
+ *unsigned short bus,
+ *unsigned short dev,
+ *unsigned short func,
+ *os_pci_dev_t os_pci_dev
+ *
+ *  Description:
+ *  This function finds a PCI device by scanning the specified bus, dev, func
+ *and tries to match vendor and device ids with the ones given to it as
+ *parameters.
+ *
+ *  Notes: If the bus number is 0xFFFF, then the function searches for that
+ *  vendor_id, device_id pair in the whole PCI topology of the system i.e
+ *  it goes through all the buses, devices, functions in the system
+ *
+ * Returns:
+ *os_pci_dev_t
+ *---------------------------------------------------------------------
+ */
+os_pci_dev_t os_pci_find_device(
+				unsigned short vendor_id,
+				unsigned short device_id,
+				unsigned short bus,
+				unsigned short dev,
+				unsigned short func,
+				os_pci_dev_t pci_dev)
+{
+  /* TODO: Right now, Just fallback to pci_find_device_generic
+   * But we need to implement this
+   */
+  return pci_find_device_generic(vendor_id, device_id, pci_dev);
+}
+
+int os_pci_get_slot_address(
+			    os_pci_dev_t pci_dev,
+			    unsigned int *bus,
+			    unsigned int *slot,
+			    unsigned int *func)
+{
+
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+
+  if(bus) {
+    *bus = pdev->bus;
+  }
+  if(slot) {
+    *slot = pdev->slot;
+  }
+  if(func) {
+    *func = pdev->func;
+  }
+  return 0;
+}
+
+int os_pci_read_config_8(
+			 os_pci_dev_t pci_dev,
+			 unsigned long offset,
+			 unsigned char* val
+			 )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  EMGD_ASSERT(val, "Invalid pointer", 0);
+  return pci_read_config_byte(pdev->dev, offset, val);
+}
+
+int os_pci_read_config_16(
+			  os_pci_dev_t pci_dev,
+			  unsigned long offset,
+			  unsigned short* val
+			  )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  EMGD_ASSERT(val, "Invalid pointer", 0);
+  return pci_read_config_word(pdev->dev, offset,val);
+}
+
+int os_pci_read_config_32(
+			  os_pci_dev_t pci_dev,
+			  unsigned long offset,
+			  unsigned long* val
+			  )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  EMGD_ASSERT(val, "Invalid pointer", 0);
+  return pci_read_config_dword(pdev->dev, offset, (u32*)val);
+}
+
+int os_pci_write_config_8(
+			  os_pci_dev_t pci_dev,
+			  unsigned long offset,
+			  unsigned char val
+			  )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  return pci_write_config_byte(pdev->dev, offset, val);
+}
+
+int os_pci_write_config_16(
+			   os_pci_dev_t pci_dev,
+			   unsigned long offset,
+			   unsigned short val
+			   )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  return pci_write_config_word(pdev->dev, offset, val);
+}
+
+int os_pci_write_config_32(
+			   os_pci_dev_t pci_dev,
+			   unsigned long offset,
+			   unsigned long val
+			   )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  return pci_write_config_dword(pdev->dev, offset, val);
+}
+
+int os_pci_disable_legacy_vga_decoding(
+			os_pci_dev_t pci_dev
+		)
+{
+#if defined(CONFIG_VGA_ARB)
+	linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+	EMGD_ASSERT(pdev, "Invalid pci device", 0);
+
+	vga_set_legacy_decoding(pdev->dev, VGA_RSRC_NONE);
+#else
+	/* Noop if the VGA arbiter isn't compiled into the kernel */
+#endif
+
+	return 0;
+}
+
+
+void os_pci_free_device(
+			os_pci_dev_t pci_dev
+			)
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+
+  // Release the lock on our PCI device struct
+  pci_dev_put(pdev->dev);
+
+  // Free our local structure.
+  OS_FREE(pdev);
+
+  return;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/Makefile.include b/drivers/gpu/drm/emgd/emgd/pal/Makefile.include
new file mode 100644
index 0000000..c312c51
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/Makefile.include
@@ -0,0 +1,50 @@
+#----------------------------------------------------------------------------
+# Filename: Makefile.include
+# $Revision: 1.3 $
+#----------------------------------------------------------------------------
+# Copyright (c) 2002-2010, Intel Corporation.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#----------------------------------------------------------------------------
+
+ifeq ($(origin EGD_CFG), undefined)
+$(error EGD_CFG environment variable is required for compilation)
+endif
+
+EGD_ROOT := $(subst  \,/,$(EGD_ROOT))
+
+#
+# PAL overrides the include paths because the HAL get access to more
+# headers that the port drivers cannot use.
+#
+PROJECT_INCLUDES := \
+	$(EGD_ROOT)/hal/include \
+	$(EGD_ROOT)/$(EGD_CFG)/include \
+	$(EGD_ROOT)/$(EGD_CFG)/oal/include \
+	$(EGD_ROOT)/$(EGD_CFG)/oal \
+	$(EGD_ROOT)/$(EGD_CFG)/pal/lpd
+
+PROJECT_LIBS=\
+	$(EGD_ROOT)/$(EGD_CFG)/cfg/lib/liblpd
+
+PROJECT_BUILD_OPTS = -DMODULE_NAME=hal.dpd
+
+PROJECT_OUTPUT_PATH = $(EGD_ROOT)/$(EGD_CFG)/cfg/dpd
+
+include $(EGD_ROOT)/$(EGD_CFG)/build/Makefile.include
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/Makefile.gnu b/drivers/gpu/drm/emgd/emgd/pal/ch7036/Makefile.gnu
new file mode 100644
index 0000000..cb82fd6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/Makefile.gnu
@@ -0,0 +1,44 @@
+#----------------------------------------------------------------------------
+# Filename: Makefile.gnu
+# $Revision: 1.1.2.1 $
+#----------------------------------------------------------------------------
+# INTEL CONFIDENTIAL
+# Copyright (2002-2008) Intel Corporation All Rights Reserved.
+# The source code contained or described herein and all documents related to
+# the source code ("Material") are owned by Intel Corporation or its suppliers
+# or licensors. Title to the Material remains with Intel Corporation or its
+# suppliers and licensors. The Material contains trade secrets and proprietary
+# and confidential information of Intel or its suppliers and licensors. The
+# Material is protected by worldwide copyright and trade secret laws and
+# treaty provisions. No part of the Material may be used, copied, reproduced,
+# modified, published, uploaded, posted, transmitted, distributed, or
+# disclosed in any way without Intel's prior express written permission.
+# 
+# No license under any patent, copyright, trade secret or other intellectual
+# property right is granted to or conferred upon you by disclosure or
+# delivery of the Materials, either expressly, by implication, inducement,
+# estoppel or otherwise. Any license under such intellectual property rights
+# must be express and approved by Intel in writing.
+# 
+#----------------------------------------------------------------------------
+DRV     = ch7036
+SOURCES = \
+            ch7036_port.c \
+            ch7036_intf.c \
+            ch7036_attr.c \
+			ch7036_fw.c \
+            ch7036.c \
+            ch7036_iic.c \
+            ch7036_pm.c \
+            ch7036_reg_table.c \
+            lvds.c
+
+
+include ../Makefile.include
+
+#----------------------------------------------------------------------------
+# File Revision History
+# $Id: Makefile.gnu,v 1.1.2.1 2011/09/13 08:50:22 nanuar Exp $
+# $Source: /nfs/fm/proj/eia/cvsroot/koheo/linux.nonredistributable/ch7036/ch7036pd_src/Attic/Makefile.gnu,v $
+#----------------------------------------------------------------------------
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.c
new file mode 100755
index 0000000..3b588a9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.c
@@ -0,0 +1,2366 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036.c
+* @version 1.2.2.1
+*-----------------------------------------------------------------------------
+*/
+
+
+
+#include "ch7036_intf.h"
+
+
+static uint32 g_nLastError;
+
+
+
+static ch_bool convert_pll1n1_div(ch_bool pll1n1_to_div, uint8* pll1n1_addr, uint8* div_addr); 
+static ch_bool convert_pll1n2_div(ch_bool pll1n2_to_div, uint8* pll1n2_addr, uint8* div_addr);
+static ch_bool convert_pll1n3_div(ch_bool pll1n3_to_div, uint8* pll1n3_addr, uint8* div_addr);
+static ch_bool convert_pll2n5_div(ch_bool pll2n5_to_div, uint8* pll2n5_addr, uint8* div_addr);
+static ch_bool convert_pll2n6_div(ch_bool pll2n6_to_div, uint8* pll2n6_addr, uint8* div_addr);
+static ch_bool convert_pll2n7_div(ch_bool pll2n7_to_div, uint8* pll2n7_addr, uint8* div_addr);
+static ch_bool convert_pll3n8_div(ch_bool pll3n8_to_div, uint8* pll3n8_addr, uint8* div_addr);
+static ch_bool convert_dmxtal_div(ch_bool dmxtal_to_div, uint8* dmxtal_addr, uint8* div_addr);
+
+
+static ch_bool set_input_info(INPUT_INFO* pInput_Info);
+
+
+static ch_bool set_output_info(OUTPUT_INFO* pOutput_Info);
+
+
+static ch_bool set_prefer_info(PREFER_INFO* pPrefer_Info);
+
+
+static ch_bool cal_and_set_clk_pll(DEV_CONTEXT* pDevContext);
+
+
+static ch_bool cal_and_set_scaler(DEV_CONTEXT* pDevContext);
+
+
+static ch_bool cal_and_set_power(DEV_CONTEXT* pDevContext);
+
+
+static ch_bool post_cal_and_set(DEV_CONTEXT* pDevContext);
+
+
+extern void ch7036_set_power_lvds(DEV_CONTEXT* pDevContext);
+extern void ch7036_set_power_hdmi(DEV_CONTEXT* pDevContext);
+extern void ch7036_set_power_crt(DEV_CONTEXT* pDevContext);
+
+
+#define  DOUBLE_TO_INT		1000
+
+
+
+
+
+
+
+ch_bool DeviceSetPower(DEV_CONTEXT* pDevContext, unsigned long flags)
+{
+
+	switch (flags) {
+		default:
+			ch7036_set_power_crt(pDevContext);
+			ch7036_set_power_hdmi(pDevContext);
+			ch7036_set_power_lvds(pDevContext);
+			break;
+	}
+
+	return ch_true;
+
+}
+
+ch_bool DevicePrepare(DEV_CONTEXT* pDevContext)
+{
+	uint32 C;
+	uint32 hao_t, vao_t, vai_t, val_t;
+	uint32 hai_down;
+	uint32 hai_sdram;
+	uint32 lnsel;
+	uint32 intlc = 0;
+	uint32 blk_h;
+	uint32 fba_inc;
+	uint32 bandwidth;
+
+	uint32 frame_rate_in, frame_rate_out;  
+	uint32  r;                             
+
+	INPUT_INFO* pInput_Info = pDevContext->pInput_Info;
+	OUTPUT_INFO* pOutput_Info = pDevContext->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = pDevContext->pPrefer_Info;
+
+	
+	g_nLastError = ERR_NO_ERROR;
+
+	
+	I2CWrite(pDevContext,0x03, 0x04);
+	pDevContext->DeviceID = I2CRead(pDevContext,0x50);
+	if(pDevContext->DeviceID != 0x56)
+	{
+		g_nLastError = ERR_DEVICE_NO_EXIST;
+		return ch_false;
+	}
+
+	
+	
+	if(pInput_Info->timing.ha % 2)
+	{
+		pInput_Info->timing.ha--;	
+	}
+	if(pInput_Info->timing.va % 2)
+	{
+		pInput_Info->timing.va--;	
+	}
+
+	
+	
+	if(pPrefer_Info->dat16_32b)
+	{
+		C = 100;
+	}
+	else if(pPrefer_Info->true24)
+	{
+		C = 100;
+	}
+	else
+	{
+		C = (pPrefer_Info->true_com) ? 75 : 50;
+	}
+	if(pOutput_Info->ds_percent_h)
+	{
+		hao_t = pOutput_Info->timing.ha * (100 - pOutput_Info->ds_percent_h) / 100;
+	}
+	else
+	{
+		hao_t = pOutput_Info->timing.ha;
+	}
+	if(pOutput_Info->ds_percent_v)
+	{
+		vao_t = pOutput_Info->timing.va * (100 - pOutput_Info->ds_percent_v) / 100;
+	}
+	else
+	{
+		vao_t = pOutput_Info->timing.va;
+	}
+	hao_t = hao_t + (hao_t % 2);
+	vao_t = vao_t + (vao_t % 2);
+	hai_down = pInput_Info->timing.ha;
+	if(hai_down > hao_t && pOutput_Info->rotate != ROTATE_90 && pOutput_Info->rotate != ROTATE_270)
+	{
+		hai_down = hao_t;
+	}
+
+	if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+	{
+		val_t = 720;
+	}
+	else
+	{
+		if(pPrefer_Info->dat16_32b)
+		{
+			val_t = 720;
+		}
+		else
+		{
+			val_t = pPrefer_Info->true24 ? 720 : 1440;
+		}
+	}
+	if(hai_down > val_t && (pOutput_Info->rotate != ROTATE_NO || pOutput_Info->h_flip || pOutput_Info->v_flip))
+	{
+		hai_down = val_t;
+	}
+
+	if(pOutput_Info->channel & CHANNEL_HDMI)
+	{
+		
+		switch(pOutput_Info->hdmi_fmt.format_index)
+		{
+		case  5:
+		case  6:
+		case  7:
+		case 10:
+		case 11:
+		case 20:
+		case 21:
+		case 22:
+		case 25:
+		case 26:
+			intlc = 1;
+			break;
+		default:
+			intlc = 0;
+			break;		
+		}
+	}
+
+	
+	if(pOutput_Info->channel & CHANNEL_VGA)
+	{
+       intlc = 0;
+	}
+
+	val_t = intlc ? (vao_t / 2) : vao_t;
+	if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+	{
+		if(hai_down <= val_t)
+			lnsel = 3;
+		else
+			lnsel = 1;
+	}
+	else
+	{
+		if(pInput_Info->timing.va <= val_t)
+			lnsel = 3;
+		else
+			lnsel = 1;
+	}
+	bandwidth = (pInput_Info->rx_clk_khz + pOutput_Info->uclk_khz * pInput_Info->timing.ha / hao_t * (4 - lnsel)) / 85 * C;
+	if(bandwidth > MEM_CLK_FREQ_MAX)
+	{
+		if(lnsel >= 2)
+		{
+			g_nLastError = ERR_BANDWIDTH_OVERFLOW;
+			return ch_false;
+		}
+		
+		lnsel++;
+
+		
+		
+		
+		
+		
+		if(pInput_Info->rx_clk_khz >= 75428 && pOutput_Info->uclk_khz >= 94500)
+			lnsel++;
+
+		bandwidth = (pInput_Info->rx_clk_khz + pOutput_Info->uclk_khz * pInput_Info->timing.ha / hao_t * (4 - lnsel)) / 85 * C;
+		if(bandwidth > MEM_CLK_FREQ_MAX)
+		{
+			g_nLastError = ERR_BANDWIDTH_OVERFLOW;
+			return ch_false;
+		}
+		pPrefer_Info->scale_line_adjust = 1;
+	}
+
+	
+	if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+	{
+		val_t = 100;
+	}
+	else
+	{
+		if(pPrefer_Info->dat16_32b)
+		{
+			val_t = 100;
+		}
+		else if(pPrefer_Info->true24)
+		{
+			val_t = 100;
+		}
+		else
+		{
+			val_t = pPrefer_Info->true_com ? 75 : 50;
+		}
+	}
+	if(val_t == 75)
+	{
+		hai_sdram = (hai_down / 4) * 3 + (hai_down % 4);
+	}
+	else
+	{
+		hai_sdram = hai_down * val_t / 100;
+	}
+	if(pOutput_Info->rotate != ROTATE_NO || pOutput_Info->h_flip || pOutput_Info->v_flip)
+	{
+		blk_h = 45;
+	}
+	else if(hai_sdram <= 720 && pInput_Info->timing.va <= 720)
+	{
+		blk_h = 45;
+	}
+	else
+	{
+		blk_h = hai_sdram / 16;
+		blk_h = (hai_sdram % 16) ? (blk_h + 1) : blk_h;
+	}
+
+	
+	if((pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270) && pPrefer_Info->true24 == 0 && pPrefer_Info->true_com == 0 && pPrefer_Info->dat16_32b == 0)
+	{
+		vai_t = pInput_Info->timing.va / 2;
+	}
+	else
+	{
+		vai_t = pInput_Info->timing.va;
+	}
+	val_t = (vai_t % 16) ? (vai_t / 16 + 1) : (vai_t / 16);
+	fba_inc = blk_h * val_t;
+
+	
+	frame_rate_in = ((pInput_Info->rx_clk_khz * 10000) / pInput_Info->timing.ht) * 10 / pInput_Info->timing.vt;
+	frame_rate_out = ((pOutput_Info->uclk_khz * 10000) / pOutput_Info->timing.ht) * 10 / pOutput_Info->timing.vt;
+	r = DOUBLE_TO_INT*frame_rate_in / frame_rate_out;
+
+	
+	if(pPrefer_Info->dat16_32b)
+	{
+		if(pPrefer_Info->true24)
+		{
+			g_nLastError = ERR_NO_SUPPORT_TRUE24;
+			return ch_false;
+		}
+		if(pPrefer_Info->true_com)
+		{
+			g_nLastError = ERR_NO_SUPPORT_TRUECOM;
+			return ch_false;
+		}
+		if(pOutput_Info->rotate == ROTATE_NO && pOutput_Info->h_flip == 0 && pOutput_Info->v_flip == 0)
+		{
+			if(fba_inc > FBA_INC_MAX)
+			{
+				
+				
+				
+               if (( (DOUBLE_TO_INT/2) <= r) && ( r <= DOUBLE_TO_INT))
+			   {
+					if( ((4096*DOUBLE_TO_INT) / fba_inc) < (2*DOUBLE_TO_INT-r) )
+					{
+						g_nLastError = ERR_RESOLUTION_OVERFLOW;
+						return ch_false;
+					}
+			   }
+  
+               if (( DOUBLE_TO_INT < r) && ( r <= (2*DOUBLE_TO_INT)))
+			   {
+					if( ((4096*DOUBLE_TO_INT) / fba_inc)  <
+						(2*DOUBLE_TO_INT - (DOUBLE_TO_INT*DOUBLE_TO_INT)/r) )
+					{
+						g_nLastError = ERR_RESOLUTION_OVERFLOW;
+						return ch_false;
+					}
+			   }		
+			   
+			}
+		}
+		else if(pOutput_Info->rotate != ROTATE_NO)	
+		{
+			if(pInput_Info->timing.va > 720)
+			{
+				g_nLastError = ERR_ROTATION_WITH_VAI;
+				return ch_false;
+			}
+		}
+		else 
+		{
+			if(pInput_Info->timing.va > 720)
+			{
+				g_nLastError = ERR_FLIP_WITH_VAI;
+				return ch_false;
+			}
+		}
+	}
+
+	
+	if(pPrefer_Info->dat16_32b == 0)
+	{
+		if(pOutput_Info->rotate == ROTATE_NO && pOutput_Info->h_flip == 0 && pOutput_Info->v_flip == 0)
+		{	
+			if(fba_inc > FBA_INC_MAX)	
+			{
+				
+                if (( (DOUBLE_TO_INT/2) <= r) && ( r <= DOUBLE_TO_INT))
+			    {
+					if( ((4096*DOUBLE_TO_INT) / fba_inc) < (2*DOUBLE_TO_INT-r) )
+					{
+						g_nLastError = ERR_RESOLUTION_OVERFLOW;
+						return ch_false;
+					}
+			    }
+  
+                if (( DOUBLE_TO_INT < r) && ( r <= (2*DOUBLE_TO_INT)))
+			    {
+					if( ((4096*DOUBLE_TO_INT) / fba_inc)  <
+						(2*DOUBLE_TO_INT - (DOUBLE_TO_INT*DOUBLE_TO_INT)/r) )
+					{
+						g_nLastError = ERR_RESOLUTION_OVERFLOW;
+						return ch_false;
+					}
+			    }		
+			    
+
+				if(pPrefer_Info->true24)
+				{
+					g_nLastError = ERR_NO_SUPPORT_TRUE24;
+					return ch_false;
+				}
+				if(pPrefer_Info->true_com)
+				{
+					g_nLastError = ERR_NO_SUPPORT_TRUECOM;
+					return ch_false;
+				}
+				g_nLastError = ERR_RESOLUTION_OVERFLOW;
+				return ch_false;
+			}
+		}
+		if(pOutput_Info->rotate == ROTATE_180)
+		{
+			if(pPrefer_Info->true_com)
+			{
+				g_nLastError = ERR_NO_SUPPORT_TRUECOM;
+				return ch_false;
+			}
+			if(pPrefer_Info->true24)
+			{
+				if(pInput_Info->timing.va > 720)
+				{
+					g_nLastError = ERR_NO_SUPPORT_TRUE24;
+					return ch_false;
+				}
+			}
+			else
+			{
+				if(pInput_Info->timing.va > 720)
+				{
+					g_nLastError = ERR_ROTATION_WITH_VAI;
+					return ch_false;
+				}
+			}
+		}
+		if(pOutput_Info->h_flip || pOutput_Info->v_flip)
+		{
+			if(pPrefer_Info->true_com)
+			{
+				g_nLastError = ERR_NO_SUPPORT_TRUECOM;
+				return ch_false;
+			}
+			if(pInput_Info->timing.va > 720)
+			{
+				g_nLastError = ERR_FLIP_WITH_VAI;
+				return ch_false;
+			}
+		}
+		if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+		{
+			if(pPrefer_Info->true_com)
+			{
+				g_nLastError = ERR_NO_SUPPORT_TRUECOM;
+				return ch_false;
+			}
+			if(pPrefer_Info->true24)
+			{
+				if(pInput_Info->timing.va > 720)
+				{
+					g_nLastError = ERR_NO_SUPPORT_TRUE24;
+					return ch_false;
+				}
+			}
+			else
+			{
+				if(pInput_Info->timing.va > 1440)
+				{
+					g_nLastError = ERR_ROTATION_WITH_VAI;
+					return ch_false;
+				}
+			}
+		}
+	}
+
+	
+	
+	if(pPrefer_Info->true24 || pPrefer_Info->true_com)
+	{
+		pPrefer_Info->dither_filter_enable = 0;
+	}
+
+	
+
+	
+	iic_reset();
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI || pOutput_Info->channel & CHANNEL_VGA) 
+	{
+	
+		iic_write(0x03, 0x01);  
+		iic_write(0x07, 0x7E);
+		iic_write(0x09, 0x0B);
+		iic_write(0x03, 0x00);
+		iic_write_ex(STOP, 1);
+	}
+
+	return ch_true;
+}
+
+ch_bool DeviceConfig(DEV_CONTEXT* pDevContext)
+{
+	if(!set_input_info(pDevContext->pInput_Info))
+		return ch_false;
+
+	if(!set_output_info(pDevContext->pOutput_Info))
+		return ch_false;
+
+	if(!set_prefer_info(pDevContext->pPrefer_Info))
+		return ch_false;
+
+	if(!cal_and_set_clk_pll(pDevContext))
+		return ch_false;
+
+	if(!cal_and_set_scaler(pDevContext))
+		return ch_false;
+
+	if(!cal_and_set_power(pDevContext))
+		return ch_false;
+
+	return ch_true;
+}
+
+ch_bool DeviceRunning(DEV_CONTEXT* pDevContext)
+{
+	uint8 val_t;
+	uint8 reg = 0x00;
+	OUTPUT_INFO* pOutput_Info = pDevContext->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = pDevContext->pPrefer_Info;
+
+	uint8 REFDLY = pPrefer_Info->pll_ref_dly;
+	uint8 FBDLY = pPrefer_Info->pll_ref_fbdly;
+	uint8 TXDRV_IT = pPrefer_Info->lvds_txdrv_ctrl;
+
+	
+	DeviceReset(pDevContext);
+
+	
+	
+	DeviceSetup(pDevContext);
+	
+	
+	I2CWrite(pDevContext,0x03, 0x01);
+	
+	
+	
+	
+	reg = I2CRead(pDevContext,0x24) & 0xFD; 
+	I2CWrite(pDevContext,0x24, reg);
+
+	I2CWrite(pDevContext,0x03, 0x04);
+	reg = I2CRead(pDevContext,0x66);
+    reg = reg & 0x1F;
+	reg = reg | ((uint8)REFDLY << 5);
+	I2CWrite(pDevContext,0x66, reg); 
+
+	I2CWrite(pDevContext,0x03, 0x04);
+	reg = I2CRead(pDevContext,0x66);
+    reg = reg & 0xE3;
+	reg = reg | ((uint8)FBDLY << 2);
+	I2CWrite(pDevContext,0x66, reg);
+
+    I2CWrite(pDevContext,0x03, 0x04);
+	reg = I2CRead(pDevContext,0x68) & 0xF0;
+    reg = reg | ((uint8)TXDRV_IT);
+	I2CWrite(pDevContext,0x68, reg); 
+
+	
+
+
+
+	
+        
+       I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x0D);
+	   if((pPrefer_Info->eye_dri_pll_rlf >= 0) && (pPrefer_Info->eye_dri_pll_rlf <= 3))
+ 	      reg = (reg & 0x9F) | (pPrefer_Info->eye_dri_pll_rlf << 5);
+	   I2CWrite(pDevContext,0x0D, reg);
+
+	   I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x0C);
+	   if((pPrefer_Info->eye_dri_pll_cp >= 0) && (pPrefer_Info->eye_dri_pll_cp <= 3))
+ 	      reg = (reg & 0x3F) | (pPrefer_Info->eye_dri_pll_cp << 6);
+	   I2CWrite(pDevContext,0x0C, reg);
+
+	   I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x0B);
+	   if((pPrefer_Info->eye_dri_damp >= 0) && (pPrefer_Info->eye_dri_damp <= 7))
+ 	      reg = (reg & 0x1F) | (pPrefer_Info->eye_dri_damp << 5);
+	   I2CWrite(pDevContext,0x0B, reg);
+
+       I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x0A);
+	   if((reg & 0x01)==0x00)
+ 	      reg = (reg | 0x01);
+	   I2CWrite(pDevContext,0x0A, reg);
+
+       I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x0A);
+	   if((pPrefer_Info->eye_dri_demp >= 0) && (pPrefer_Info->eye_dri_demp <= 15))
+ 	      reg = (reg & 0xE1) | (pPrefer_Info->eye_dri_demp << 1);
+	   I2CWrite(pDevContext,0x0A, reg);
+
+	   I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x08);
+	   if((pPrefer_Info->eye_dacg >= 0) && (pPrefer_Info->eye_dacg <= 3))
+ 	      reg = (reg & 0x3F) | (pPrefer_Info->eye_dacg << 6);
+	   I2CWrite(pDevContext,0x08, reg);
+
+	   I2CWrite(pDevContext,0x03, 0x01);  
+	   reg = I2CRead(pDevContext,0x0F);
+	   if((pPrefer_Info->eye_rdac >= 0) && (pPrefer_Info->eye_rdac <= 7))
+ 	      reg = (reg & 0xC7) | (pPrefer_Info->eye_rdac << 3);
+	   I2CWrite(pDevContext,0x0F, reg);
+
+	   I2CWrite(pDevContext,0x03, 0x04);  
+	   reg = I2CRead(pDevContext,0x54);
+	   if((pPrefer_Info->eye_bgtrim >= 0) && (pPrefer_Info->eye_bgtrim <= 3))
+ 	      reg = (reg & 0xF3) | (pPrefer_Info->eye_bgtrim << 2);
+	   I2CWrite(pDevContext,0x54, reg);
+
+
+	
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI || pOutput_Info->channel & CHANNEL_VGA) 
+	{
+		
+		if(!post_cal_and_set(pDevContext)){
+		   return ch_false;
+	    }
+
+		
+		I2CWrite(pDevContext,0x03, 0x00);
+		val_t = I2CRead(pDevContext,0x0A);
+		I2CWrite(pDevContext,0x0A, val_t | 0x80); 
+		I2CWrite(pDevContext,0x0A, val_t & 0x7F); 
+
+		
+
+
+		
+		
+		
+		
+		
+		
+	    
+	    
+		
+		
+		I2CWrite(pDevContext,0x03, 0x00);
+		val_t = I2CRead(pDevContext,0x0A);
+		I2CWrite(pDevContext,0x0A, val_t & 0xEF); 
+		
+		
+
+	}
+
+	return ch_true;
+}
+
+uint32 GetLastErrorMessage()
+{
+	return g_nLastError;
+}
+
+
+
+ch_bool set_input_info(INPUT_INFO* pInput_Info)
+{
+	
+	iic_write_ex(HTI, pInput_Info->timing.ht);
+	iic_write_ex(HAI, pInput_Info->timing.ha);
+	iic_write_ex(HOI, pInput_Info->timing.ho);
+	iic_write_ex(HWI, pInput_Info->timing.hw);
+	iic_write_ex(VTI, pInput_Info->timing.vt);
+	iic_write_ex(VAI, pInput_Info->timing.va);
+	iic_write_ex(VOI, pInput_Info->timing.vo);
+	iic_write_ex(VWI, pInput_Info->timing.vw);
+	
+	iic_write_ex(RCLK, pInput_Info->rx_clk_khz);
+	
+	
+	switch(pInput_Info->pixel_fmt)
+	{
+		  case 0:
+			 iic_write_ex(DITHER_SEL0_SPP, 0);
+		     iic_write_ex(DITHER_SEL1_SPP, 0);
+			 break;
+		  case 1:
+			 iic_write_ex(DITHER_SEL0_SPP, 1);
+		     iic_write_ex(DITHER_SEL1_SPP, 0);
+			 break;
+		  case 2:
+			 iic_write_ex(DITHER_SEL0_SPP, 0);
+		     iic_write_ex(DITHER_SEL1_SPP, 1);
+			 break;
+		  case 3:
+			 iic_write_ex(DITHER_SEL0_SPP, 1);
+		     iic_write_ex(DITHER_SEL1_SPP, 1);
+			 break;
+		  default:
+			iic_write_ex(DITHER_SEL0_SPP, 0);
+		    iic_write_ex(DITHER_SEL1_SPP, 0);
+			break;
+	}
+
+	
+	
+	iic_write_ex(HSYNCP_SPP, pInput_Info->hs_pol);
+	iic_write_ex(VSYNCP_SPP, pInput_Info->vs_pol);
+	
+	iic_write_ex(HPO_I, pInput_Info->hs_pol);
+	iic_write_ex(VPO_I, pInput_Info->vs_pol);
+	iic_write_ex(DEPO_I, pInput_Info->de_pol);
+	
+	iic_write_ex(NP_INV0_SPP, (pInput_Info->data_ch_pol & (1 << 0)) ? 1 : 0);
+	iic_write_ex(NP_INV1_SPP, (pInput_Info->data_ch_pol & (1 << 1)) ? 1 : 0);
+	iic_write_ex(NP_INV2_SPP, (pInput_Info->data_ch_pol & (1 << 2)) ? 1 : 0);
+	iic_write_ex(NP_INV3_SPP, (pInput_Info->data_ch_pol & (1 << 3)) ? 1 : 0);
+	
+	iic_write_ex(LVDS_IN_ORDER, pInput_Info->data_ch_invert);
+	
+	iic_write_ex(I2S_SPDIFB, pInput_Info->audio_type);
+	if(pInput_Info->audio_type == AUDIO_I2S) {
+		iic_write_ex(I2SPOL, pInput_Info->i2s_pol);
+		iic_write_ex(I2S_LENGTH, pInput_Info->i2s_len);
+		iic_write_ex(I2SFMT, pInput_Info->i2s_fmt);
+	}
+
+	return ch_true;
+}
+
+ch_bool set_output_info(OUTPUT_INFO* pOutput_Info)
+{
+	uint8 lvds0_seq, lvds1_seq, lvds2_seq, lvds3_seq, lvdsclk_seq;
+	uint8 lvds0_pol, lvds1_pol, lvds2_pol, lvds3_pol, lvdsclk_pol;
+	uint8 hd_dvib, intlc = 0, copy, hd_lv_pol, hd_lv_seq, hdmi_lvds_sel, hsp, vsp, m1m0, c1c0, vic; 
+	uint32 hao_down, vao_down;
+
+	
+	LVDS_FMT* pLvdsFmt = &pOutput_Info->lvds_fmt;
+	HDMI_FMT* pHdmiFmt = &pOutput_Info->hdmi_fmt;
+	
+	
+	
+
+
+	
+		
+
+
+
+	if(pOutput_Info->channel & CHANNEL_VGA || pOutput_Info->channel & CHANNEL_HDMI) {
+		iic_write_ex(VP,pOutput_Info->v_position);
+		iic_write_ex(HP,pOutput_Info->h_position);
+	}
+	
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI || pOutput_Info->channel & CHANNEL_VGA)
+	{
+		iic_write_ex(HTO, pOutput_Info->timing.ht);
+		iic_write_ex(HAO, pOutput_Info->timing.ha);
+		iic_write_ex(HOO_HDMI, pOutput_Info->timing.ho);
+		iic_write_ex(HWO_HDMI, pOutput_Info->timing.hw);
+		iic_write_ex(VTO, pOutput_Info->timing.vt);
+		iic_write_ex(VAO, pOutput_Info->timing.va);
+		iic_write_ex(VOO_HDMI, pOutput_Info->timing.vo);
+		iic_write_ex(VWO_HDMI, pOutput_Info->timing.vw);
+
+		iic_write_ex(UCLK, pOutput_Info->uclk_khz);
+		iic_write_ex(ROTATE, pOutput_Info->rotate);
+		iic_write_ex(HFLIP, pOutput_Info->h_flip);
+		iic_write_ex(VFLIP, pOutput_Info->v_flip);
+		
+		hao_down = pOutput_Info->timing.ha * (100 - pOutput_Info->ds_percent_h) / 100;
+		hao_down = hao_down + (hao_down % 2);
+		vao_down = pOutput_Info->timing.va * (100 - pOutput_Info->ds_percent_v) / 100;
+		vao_down = vao_down + (vao_down % 2);
+		if(pOutput_Info->ds_percent_h || pOutput_Info->ds_percent_v)
+		{
+			iic_write_ex(SCAN_EN, 1);
+			iic_write_ex(HAO_SCL, hao_down);
+			iic_write_ex(VAO_SCL, vao_down);
+		}
+		
+		iic_write_ex(ROTATE, pOutput_Info->rotate);
+		iic_write_ex(HFLIP, pOutput_Info->h_flip);
+		iic_write_ex(VFLIP, pOutput_Info->v_flip);
+	}
+	
+	if(pOutput_Info->channel & CHANNEL_LVDS)
+	{
+		
+		lvds0_seq = (pLvdsFmt->channel_swap >>  0) & 0xF;
+		lvds1_seq = (pLvdsFmt->channel_swap >>  4) & 0xF;
+		lvds2_seq = (pLvdsFmt->channel_swap >>  8) & 0xF;
+		lvds3_seq = (pLvdsFmt->channel_swap >> 12) & 0xF;
+		lvdsclk_seq = (pLvdsFmt->channel_swap >>  16) & 0xF;
+		iic_write_ex(LVDS0_SEQ_SPP, lvds0_seq);
+		iic_write_ex(LVDS1_SEQ_SPP, lvds1_seq);
+		iic_write_ex(LVDS2_SEQ_SPP, lvds2_seq);
+		iic_write_ex(LVDS3_SEQ_SPP, lvds3_seq);
+		iic_write_ex(LVDSCLK_SEQ_SPP, lvdsclk_seq);
+		
+		lvds0_pol = (pLvdsFmt->channel_pol >> 0) & 0x1;
+		lvds1_pol = (pLvdsFmt->channel_pol >> 1) & 0x1;
+		lvds2_pol = (pLvdsFmt->channel_pol >> 2) & 0x1;
+		lvds3_pol = (pLvdsFmt->channel_pol >> 3) & 0x1;
+		lvdsclk_pol = (pLvdsFmt->channel_pol >> 4) & 0x1;
+		iic_write_ex(LVDS0_POL_SPP, lvds0_pol);
+		iic_write_ex(LVDS1_POL_SPP, lvds1_pol);
+		iic_write_ex(LVDS2_POL_SPP, lvds2_pol);
+		iic_write_ex(LVDS3_POL_SPP, lvds3_pol);
+		iic_write_ex(LVDSCLK_POL_SPP, lvdsclk_pol);
+		
+		
+		switch(pLvdsFmt->pixel_fmt){
+		  case 0:
+			 iic_write_ex(DITHER_SEL0_SPP, 0);
+		     iic_write_ex(DITHER_SEL1_SPP, 0);
+			 break;
+		  case 1:
+			 iic_write_ex(DITHER_SEL0_SPP, 1);
+		     iic_write_ex(DITHER_SEL1_SPP, 0);
+			 break;
+		  case 2:
+			 iic_write_ex(DITHER_SEL0_SPP, 0);
+		     iic_write_ex(DITHER_SEL1_SPP, 1);
+			 break;
+		  case 3:
+			 iic_write_ex(DITHER_SEL0_SPP, 1);
+		     iic_write_ex(DITHER_SEL1_SPP, 1);
+			 break;
+		  default:
+			iic_write_ex(DITHER_SEL0_SPP, 0);
+		    iic_write_ex(DITHER_SEL1_SPP, 0);
+			break;
+	    }
+
+	}
+	
+	if((pOutput_Info->channel & CHANNEL_VGA)&&((pOutput_Info->channel & CHANNEL_HDMI)==0x00))
+	{
+		
+
+		iic_write_ex(HPO_O, 1);
+		iic_write_ex(VPO_O, 1);
+		iic_write_ex(DEPO_O, 0);
+	}
+	else if((pOutput_Info->channel & CHANNEL_VGA)&&(pOutput_Info->channel & CHANNEL_HDMI))
+	{
+
+        iic_write_ex(HPO_O, 1);
+		iic_write_ex(VPO_O, 0);
+		iic_write_ex(DEPO_O, 0);
+	}
+
+	if((pOutput_Info->channel & CHANNEL_HDMI) && ((pOutput_Info->channel & CHANNEL_VGA) == 0x00))
+	{
+		 iic_write_ex(HDMIIN_HSP, pHdmiFmt->hs_pol ? 1:0);
+		 iic_write_ex(HDMIIN_VSP, pHdmiFmt->vs_pol ? 1:0);
+		 iic_write_ex(HDMIIN_DEP, 0);
+	}
+	if((pOutput_Info->channel & CHANNEL_HDMI)&&(pOutput_Info->channel & CHANNEL_VGA))
+	{
+		 iic_write_ex(HDMIIN_HSP, 0);
+		 iic_write_ex(HDMIIN_VSP, 1);
+		 iic_write_ex(HDMIIN_DEP, 1);
+	}
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI)
+	{
+		
+		hd_dvib = pHdmiFmt->is_dvi_mode ? 0 : 1;
+		iic_write_ex(HD_DVIB, hd_dvib);
+		
+		switch(pHdmiFmt->format_index)
+		{
+		case  5:
+		case  6:
+		case  7:
+		case 10:
+		case 11:
+		case 20:
+		case 21:
+		case 22:
+		case 25:
+		case 26:
+			intlc = 1;
+			break;
+		default:
+			intlc = 0;
+			break;		
+		}
+		iic_write_ex(INTLC, intlc);
+		
+		copy = 1;
+		iic_write_ex(COPY, copy);
+		
+		hd_lv_pol = pHdmiFmt->data_pol_invert;
+		iic_write_ex(HD_LV_POL, hd_lv_pol);
+		
+		hd_lv_seq = pHdmiFmt->channel_swap;
+		iic_write_ex(HD_LV_SEQ, hd_lv_seq);
+		
+		hdmi_lvds_sel = 1;
+		iic_write_ex(HDMI_LVDS_SEL, hdmi_lvds_sel);
+		
+
+		if (pOutput_Info->timing.ha > 720) {
+			pOutput_Info->hdmi_fmt.hs_pol = POL_HIGH;
+			pOutput_Info->hdmi_fmt.vs_pol = POL_HIGH;
+			if(pOutput_Info->timing.ha == 1440)
+			{
+              pOutput_Info->hdmi_fmt.hs_pol = POL_LOW;
+			  pOutput_Info->hdmi_fmt.vs_pol = POL_LOW;
+			}
+		}
+		else {
+			
+			pOutput_Info->hdmi_fmt.hs_pol = POL_LOW;
+			pOutput_Info->hdmi_fmt.vs_pol = POL_LOW;
+		}
+
+		hsp = pHdmiFmt->hs_pol;
+		vsp = pHdmiFmt->vs_pol;
+		iic_write_ex(HSP, hsp);
+		iic_write_ex(VSP, vsp);
+		
+		
+		switch(pHdmiFmt->format_index)
+		{
+		case 1:
+		case 2:
+		case 17:
+			{
+				if(pHdmiFmt->aspect_ratio == AS_RATIO_16_9) 
+				{
+					g_nLastError = ERR_ASPECT_RATIO_NOMATCH;
+					return ch_false;
+				}
+			}
+			break;
+		case 4:
+		case 5:
+		case 16:
+		case 19:
+		case 20:
+		case 31:
+		case 32:
+		case 33:
+		case 34:
+			{
+				if(pHdmiFmt->aspect_ratio == AS_RATIO_4_3)	
+				{
+					g_nLastError = ERR_ASPECT_RATIO_NOMATCH;
+					return ch_false;
+				}
+			}
+			break;
+		default:
+			break;
+		}
+		m1m0 = pHdmiFmt->aspect_ratio;
+		iic_write_ex(M1M0, m1m0);
+		
+		c1c0 = 2;
+		iic_write_ex(C1C0, c1c0);
+		
+		vic = pHdmiFmt->format_index;
+		iic_write_ex(VIC, vic);
+	}
+
+	if(pOutput_Info->channel & CHANNEL_VGA){
+       intlc = 0;
+	}
+
+	iic_write_ex(INTLC, intlc);
+
+	
+	iic_write( 0x3,  0x1); 
+	iic_write( 0x23, 0x63); 
+	iic_write( 0x3,  0x0); 
+
+	return ch_true;
+}
+
+ch_bool set_prefer_info(PREFER_INFO* pPrefer_Info)
+{
+	uint8 hsync_cnt_th, prbs_set_sel;
+	uint8 dbp ;	
+	
+	iic_write_ex(MCLK, pPrefer_Info->mclk_khz);
+	
+	iic_write_ex(UCLKOD_SEL, pPrefer_Info->uclkod_sel);
+	iic_write_ex(DAT16_32B, pPrefer_Info->dat16_32b);
+	iic_write_ex(TRUE24, pPrefer_Info->true24);
+	iic_write_ex(TRUE_COM, pPrefer_Info->true_com);
+	
+	hsync_cnt_th = pPrefer_Info->lvds_out_hs_tolerance;
+	iic_write_ex(HSYNC_CNT_TH_SPP, hsync_cnt_th);
+	prbs_set_sel = pPrefer_Info->lvds_out_reset_bit_sel;
+	iic_write_ex(PRBS_SET_SEL_SPP, prbs_set_sel);
+	
+	dbp = pPrefer_Info->dither_filter_enable ? 0 : 1;
+	iic_write_ex(DBP, dbp);
+	
+
+
+	iic_write_ex(TXTEN, pPrefer_Info->text_enhancement);
+
+
+	
+
+
+
+	return ch_true;
+}
+
+
+ch_bool cal_and_set_clk_pll(DEV_CONTEXT* pDevContext)
+{
+	uint8 pll1n2_reg, pll1n2_div;
+	uint8 pll1n3_reg, pll1n3_div;
+	uint8 pll1n1_reg = 0, pll1n1_div = 0;
+	uint8 pll3n8_reg, pll3n8_div;
+	uint8 pll2n5_reg, pll2n5_div;
+	uint8 pll2n6_reg, pll2n6_div;
+	uint8 pll2n7_reg, pll2n7_div;
+	uint8 dmxtal_reg, dmxtal_div;
+	
+	uint32 a2_reg = 0;
+	uint32 a1_reg = 0;
+	uint32 a3_reg = 0;
+	uint32 uclk2d_reg = 0;
+	uint8 uclksec_reg = 0;
+	uint8 dri_pll_n1_reg;
+	uint8 dri_pll_n3_reg;
+	
+	uint32 val_t;
+	uint32 val_t1, val_t2;
+
+	uint8 gcksel = 0;
+	uint8 tsten1 = 0; 
+   	uint8 REV_ID;
+	uint64 temp1;
+
+	
+	INPUT_INFO* pInput_Info = pDevContext->pInput_Info;
+	OUTPUT_INFO* pOutput_Info = pDevContext->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = pDevContext->pPrefer_Info;
+
+	I2CWrite(pDevContext,0x03, 0x04);
+	REV_ID = I2CRead(pDevContext,0x51) & 0x0F;
+
+	
+
+	if(pPrefer_Info->reset == 1) 
+	{
+      I2CWrite(pDevContext,0x03, 0x01);
+	  I2CWrite(pDevContext,0x14, I2CRead(pDevContext,0x14) | 0x40); 
+	  I2CWrite(pDevContext,0x03, 0x04);
+	  I2CWrite(pDevContext,0x54, I2CRead(pDevContext,0x54) | 0x40); 
+	  tsten1 = 0x40;
+	  gcksel = 0x40;
+	  iic_write_ex(GCKSEL, 1);
+	  iic_write_ex(TSTEN1, 1);
+	}
+	
+	
+	else if((pPrefer_Info->reset == 0) || (REV_ID >= 0x01)) 
+	{
+	  I2CWrite(pDevContext,0x03, 0x01);
+	  I2CWrite(pDevContext,0x14, I2CRead(pDevContext,0x14) & 0xBF); 
+	  I2CWrite(pDevContext,0x03, 0x04);
+	  I2CWrite(pDevContext,0x54, 0x40 | (I2CRead(pDevContext,0x54) & 0xBF)); 
+	  tsten1=0x00;
+	  gcksel=0x00;
+	  iic_write_ex(GCKSEL, 1);
+	  iic_write_ex(TSTEN1, 0);
+	}
+
+	
+	pll1n2_div = 4;
+	pll1n3_div = 8;
+	if(!convert_pll1n2_div(ch_false, &pll1n2_reg, &pll1n2_div))
+	{
+		g_nLastError = ERR_PLL1N2_WRONG;
+		return ch_false;
+	}
+	if(!convert_pll1n3_div(ch_false, &pll1n3_reg, &pll1n3_div))
+	{
+		g_nLastError = ERR_PLL1N3_WRONG;
+		return ch_false;
+	}
+	iic_write_ex(PLL1N2, pll1n2_reg);
+	iic_write_ex(PLL1N3, pll1n3_reg);
+
+	
+
+	if(((gcksel == 0x40)&&(tsten1 == 0x00)) || ((gcksel == 0x00)&&(tsten1 == 0x00)))
+	{
+       
+		
+		
+		
+			pll1n1_div = 8;
+		    pll1n1_reg = 3;
+		
+		
+        
+        
+		
+		
+	  
+	}
+	else if((gcksel == 0x40)&&(tsten1 == 0x40)){
+	  for(pll1n1_div=1; pll1n1_div<=64; pll1n1_div<<=1)
+	  {
+	 	val_t = pInput_Info->rx_clk_khz / pll1n1_div;
+	    if(val_t >= 2300 && val_t <= 4600)
+			break;
+	  }
+	  if(pll1n1_div > 64)
+	  {
+		g_nLastError = ERR_PLL1N1_WRONG;
+		return ch_false;
+	  }
+	  if(!convert_pll1n1_div(ch_false, &pll1n1_reg, &pll1n1_div))
+	  {
+		g_nLastError = ERR_PLL1N1_WRONG;
+		return ch_false;
+	  }
+	}
+
+	iic_write_ex(PLL1N1, pll1n1_reg);
+
+	
+	for(pll3n8_div=1; pll3n8_div<=8; pll3n8_div<<=1)
+	{
+		val_t = pPrefer_Info->mclk_khz * pll3n8_div / 64;
+		if(val_t >= 2300 && val_t <= 2600)
+			break;
+	}
+	if(pll3n8_div > 8)
+	{
+		g_nLastError = ERR_PLL3N8_WRONG;
+		return ch_false;
+	}
+	if(!convert_pll3n8_div(ch_false, &pll3n8_reg, &pll3n8_div))
+	{
+		g_nLastError = ERR_PLL3N8_WRONG;
+		return ch_false;
+	}
+	iic_write_ex(PLL3N8, pll3n8_reg);
+
+	
+
+	if((gcksel == 0x40)&&(tsten1 == 0x40)){
+		 val_t1 = (pInput_Info->rx_clk_khz << 12);
+		 val_t2 = (pll1n1_div * pll3n8_div * pPrefer_Info->mclk_khz);
+		 a2_reg = (val_t1 % val_t2) ? (val_t1 / val_t2 + 1) : (val_t1 / val_t2); 
+		  
+	}else if(((gcksel == 0x40)&&(tsten1 == 0x00))||((gcksel == 0x00)&&(tsten1 == 0x00))){
+	  val_t1 = (27000 << 12);
+	  val_t2 = (pll1n1_div * pll3n8_div * pPrefer_Info->mclk_khz);
+	  
+	 
+	
+	  
+	  a2_reg = (val_t1 % val_t2) ? (val_t1 / val_t2 + 1) : (val_t1 / val_t2); 
+	  if(pOutput_Info->uclk_khz == 148500){
+         a2_reg = 0x73;      
+	  }
+	}	
+
+	iic_write_ex(A2, a2_reg);
+
+	
+	if(pInput_Info->audio_type) 
+	{
+		uclksec_reg = pOutput_Info->uclk_khz / 10;
+	}
+	else 
+	{
+		
+
+		
+
+		 
+		if( (0x40==gcksel)&&(0x40==tsten1) )  
+		{
+			uclk2d_reg = (pInput_Info->rx_clk_khz * 2 * pll1n2_div * pll1n3_div) / (pll1n1_div * 10);
+		}
+		if(0x00==tsten1 )   
+		{
+			uclk2d_reg = (27000 * 2 * pll1n2_div * pll1n3_div) / (pll1n1_div * 10);
+		}
+
+	}
+	iic_write_ex(PCLK_NUM, uclk2d_reg);
+
+	
+
+	
+	
+	if((pOutput_Info->uclk_khz == 54000	||
+												  pOutput_Info->uclk_khz == 72000	||
+												  pOutput_Info->uclk_khz == 74250	||
+												  pOutput_Info->uclk_khz == 108000	||
+												  pOutput_Info->uclk_khz == 148500  ))
+	{
+		uclksec_reg = 1;
+	}
+	else
+	{
+		uclksec_reg = 0;
+	}
+	iic_write_ex(UCLKSEC, uclksec_reg);
+
+	
+	if(uclksec_reg == 0)
+	{
+		pll2n6_div = 8;
+		pll2n7_div = 8;
+		for(pll2n5_div=1; pll2n5_div<=8; ++pll2n5_div)
+		{
+			val_t = pOutput_Info->uclk_khz * pll2n5_div / 64;
+			if(val_t >= 2300 && val_t <= 4600)
+				break;
+		}
+		if(pll2n5_div > 8)
+		{
+			g_nLastError = ERR_PLL2N5_WRONG;
+			return ch_false;
+		}
+		dmxtal_div = 1; 
+		
+		if(pPrefer_Info->uclkod_sel == 1)
+		{
+			if((gcksel == 0x40)&&(tsten1 == 0x40)){
+				temp1 = (((uint64)pOutput_Info->uclk_khz) * pll1n1_div * pll2n5_div * (1 << 20));
+#ifdef T_LINUX
+	        	do_div(temp1 , pInput_Info->rx_clk_khz);
+				a1_reg = (uint32)temp1;
+#else
+				a1_reg = (uint32)((uint64)temp1 / pInput_Info->rx_clk_khz);
+#endif
+				PD_DEBUG ("#1 a1_reg = 0x%.8X\n", a1_reg);
+	      	} else if((gcksel == 0x40)&&(tsten1 == 0x00)){
+				temp1 = (((uint64)pOutput_Info->uclk_khz) * pll1n1_div * pll2n5_div * (1 << 20));
+#ifdef T_LINUX
+	        	do_div(temp1 , (uint32)27000);
+				a1_reg = (uint32)temp1;
+#else
+				a1_reg = (uint32)((uint64)temp1 / (uint32)27000);
+#endif
+				PD_DEBUG ("#2 a1_reg = 0x%.8X\n", a1_reg);
+	       	}else if((gcksel == 0x00)&&(tsten1 == 0x00)){
+				temp1 = (((uint64)pOutput_Info->uclk_khz) * pll1n1_div * pll2n5_div * (1 << 20));
+#ifdef T_LINUX
+	        	do_div(temp1 , (uint32)27000);
+				a1_reg = (uint32)temp1;
+#else
+				a1_reg = (uint32)((uint64)temp1 / (uint32)27000);
+#endif
+				PD_DEBUG ("#3 a1_reg = 0x%.8X\n", a1_reg);
+			}
+		   iic_write_ex(A1, a1_reg);
+		}
+		else
+		{
+			if((gcksel == 0x40)&&(tsten1 == 0x40))
+	       a3_reg = pInput_Info->rx_clk_khz * (1 << 12) / (pOutput_Info->uclk_khz * pll1n1_div * pll2n5_div);
+          else if((gcksel == 0x40)&&(tsten1 == 0x00))
+	       a3_reg = 27000 * (1 << 12) / (pOutput_Info->uclk_khz * pll1n1_div * pll2n5_div);
+          else if((gcksel == 0x00)&&(tsten1 == 0x00))
+	       a3_reg = 27000 * (1 << 12) / (pOutput_Info->uclk_khz * pll1n1_div * pll2n5_div);
+
+			iic_write_ex(A3, a3_reg);
+		}
+	}
+	else 
+	{
+		switch(pOutput_Info->uclk_khz)
+		{
+		case 54000:
+			dmxtal_div = 6;
+			pll2n7_div = 8;
+			pll2n6_div = 6;
+			pll2n5_div = 4;
+			break;
+		case 72000:
+			dmxtal_div = 6;
+			pll2n7_div = 8;
+			pll2n6_div = 6;
+			pll2n5_div = 3;
+			break;
+		case 74250:
+			dmxtal_div = 6;    
+			pll2n7_div = 11;
+			pll2n6_div = 6;    
+			pll2n5_div = 4;
+			break;
+		case 108000:
+			dmxtal_div = 6;
+			pll2n7_div = 8;
+			pll2n6_div = 6;
+			pll2n5_div = 2;
+			break;
+		case 148500:
+			
+			
+			
+			
+			dmxtal_div = 6;  
+			pll2n6_div = 6;  
+			
+            
+			
+			
+
+			pll2n7_div = 11;
+			pll2n5_div = 2;
+			break;
+		default:
+			return ch_false;
+		}
+	}
+	if(!convert_pll2n5_div(ch_false, &pll2n5_reg, &pll2n5_div))
+	{
+		g_nLastError = ERR_PLL2N5_WRONG;
+		return ch_false;
+	}
+	iic_write_ex(PLL2N5, pll2n5_reg & 0x07);
+	iic_write_ex(PLL2N53, pll2n5_reg >> 3 & 0x01);
+	if(!convert_pll2n6_div(ch_false, &pll2n6_reg, &pll2n6_div))
+	{
+		g_nLastError = ERR_PLL2N6_WRONG;
+		return ch_false;
+	}
+	iic_write_ex(PLL2N6, pll2n6_reg);
+	if(!convert_pll2n7_div(ch_false, &pll2n7_reg, &pll2n7_div))
+	{
+		g_nLastError = ERR_PLL2N7_WRONG;
+		return ch_false;
+	}
+	iic_write_ex(PLL2N7, pll2n7_reg);
+	if(!convert_dmxtal_div(ch_false, &dmxtal_reg, &dmxtal_div))
+	{
+		g_nLastError = ERR_DMXTAL_WRONG;
+		return ch_false;
+	}
+	iic_write_ex(DIVXTAL, dmxtal_reg);
+
+	
+	if(pInput_Info->rx_clk_khz > 40000)
+	{
+		iic_write_ex(TXPLL_FFD, 1);
+	}
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI)
+	{
+		val_t = pOutput_Info->uclk_khz;
+		if(val_t < 25000 || val_t > 165000)
+		{
+			g_nLastError = ERR_HDMI_CLOCK_NO_SUPPORT;
+			return ch_false;
+		}
+		if(val_t <= 40000)
+		{
+			dri_pll_n1_reg = 0;
+			dri_pll_n3_reg = 0;
+		}
+		else if(val_t < 80000)
+		{
+			dri_pll_n1_reg = 1;
+			dri_pll_n3_reg = 1;
+		}
+		else
+		{
+			dri_pll_n1_reg = 2;
+			dri_pll_n3_reg = 2;
+		}
+		iic_write_ex(DRI_PLL_N1, dri_pll_n1_reg);
+		iic_write_ex(DRI_PLL_N3, dri_pll_n3_reg);
+	}
+
+	
+	iic_write_ex(CK_TVINV, 1);
+	
+	return ch_true;
+}
+
+ch_bool cal_and_set_scaler(DEV_CONTEXT* pDevContext)
+{
+	uint8 wrlen_reg;
+	uint32 frame_rate_in, frame_rate_out, field_rate_out;
+	uint32 hai_down, hai_sdram;
+	uint8 fltbp2_reg, fltbp1_reg;
+	uint8 dnsmpen_reg;
+	uint32 hadwn_reg = 0;
+	uint8 blk_h_reg;
+	uint32 fba_inc_reg;
+	uint8 sfm_reg;
+	uint8 thren_reg;
+	uint32 thrrl_reg;
+	uint8 lnsel_reg;
+	uint8 wrfast_reg;
+	uint8 chg_hl_reg;
+	uint8 vsmst_reg;
+
+	uint32 val_t;
+	uint32 hao_t;
+	uint32 vao_t;
+	uint32 vai_t;
+
+	
+	INPUT_INFO* pInput_Info = pDevContext->pInput_Info;
+	OUTPUT_INFO* pOutput_Info = pDevContext->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = pDevContext->pPrefer_Info;
+
+	
+	wrlen_reg = 0;
+	iic_write_ex(WRLEN, wrlen_reg);
+
+	
+	frame_rate_in = ((pInput_Info->rx_clk_khz * 10000) / pInput_Info->timing.ht) * 10 / pInput_Info->timing.vt;
+	frame_rate_out = ((pOutput_Info->uclk_khz * 10000) / pOutput_Info->timing.ht) * 10 / pOutput_Info->timing.vt;
+	field_rate_out = iic_read_ex(INTLC) ? (frame_rate_out * 2) : (frame_rate_out);
+
+	
+	hai_down = pInput_Info->timing.ha;
+	if(pOutput_Info->ds_percent_h)
+	{
+		hao_t = pOutput_Info->timing.ha * (100 - pOutput_Info->ds_percent_h) / 100;
+	}
+	else
+	{
+		hao_t = pOutput_Info->timing.ha;
+	}
+	hao_t = hao_t + (hao_t % 2);
+	if(hai_down > hao_t)
+	{
+		dnsmpen_reg = 1;
+		hadwn_reg = hao_t;
+		hai_down = hadwn_reg;
+		fltbp2_reg = 1;
+		fltbp1_reg = 1;
+	}
+	else
+	{
+		dnsmpen_reg = 0;
+		fltbp2_reg = 1;
+		fltbp1_reg = 1;
+	}
+	if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+	{
+		val_t = 720;
+	}
+	else
+	{
+		if(pPrefer_Info->dat16_32b)
+		{
+			val_t = 720;
+		}
+		else
+		{
+			val_t = pPrefer_Info->true24 ? 720 : 1440;
+		}
+	}
+	if(hai_down > val_t && (pOutput_Info->rotate != ROTATE_NO || pOutput_Info->h_flip || pOutput_Info->v_flip))
+	{
+		dnsmpen_reg = 1;
+		hadwn_reg = val_t;
+		hai_down = hadwn_reg;
+		fltbp2_reg = 1;
+		fltbp1_reg = 1;
+	}
+	iic_write_ex(DNSMPEN, dnsmpen_reg);
+	iic_write_ex(HADWSPP, hadwn_reg);
+	
+	if(pInput_Info->timing.ha * 100 / hao_t > pPrefer_Info->hscale_ratio_gate)
+	{
+		fltbp1_reg = 0;
+	}
+	iic_write_ex(FLTBP2, fltbp2_reg);
+	iic_write_ex(FLTBP1, fltbp1_reg);
+
+	
+	if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+	{
+		val_t = 100;
+	}
+	else
+	{
+		if(pPrefer_Info->dat16_32b)
+		{
+			val_t = 100;
+		}
+		else if(pPrefer_Info->true24)
+		{
+			val_t = 100;
+		}
+		else
+		{
+			val_t = pPrefer_Info->true_com ? 75 : 50;
+		}
+	}
+	if(val_t == 75)
+	{
+		hai_sdram = (hai_down / 4) * 3 + (hai_down % 4);
+	}
+	else
+	{
+		hai_sdram = hai_down * val_t / 100;
+	}
+	if(pOutput_Info->rotate != ROTATE_NO || pOutput_Info->h_flip || pOutput_Info->v_flip)
+	{
+		blk_h_reg = 45;
+	}
+	else if(hai_sdram <= 720 && pInput_Info->timing.va <= 720)
+	{
+		blk_h_reg = 45;
+	}
+	else
+	{
+		blk_h_reg = hai_sdram / 16;
+		blk_h_reg = (hai_sdram % 16) ? (blk_h_reg + 1) : blk_h_reg;
+	}
+	iic_write_ex(BLK_H, blk_h_reg);
+
+	
+	if((pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270) && pPrefer_Info->true24 == 0 && pPrefer_Info->true_com == 0 && pPrefer_Info->dat16_32b == 0)
+	{
+		vai_t = pInput_Info->timing.va / 2;
+	}
+	else
+	{
+		vai_t = pInput_Info->timing.va;
+	}
+	val_t = (vai_t % 16) ? (vai_t / 16 + 1) : (vai_t / 16);
+	fba_inc_reg = blk_h_reg * val_t;
+	if(fba_inc_reg > 2048)
+	{
+		sfm_reg = 1;
+		thren_reg = 1;
+		val_t = (frame_rate_in * 100) / field_rate_out;
+		if(val_t > 100)
+		{
+			
+			
+			thrrl_reg = pInput_Info->timing.vt * (100 - 10000 / val_t) / 100 + THRRL_ADJUST_DEF;
+		}
+		else
+		{
+			thrrl_reg = pInput_Info->timing.vt * (100 - val_t) / 100 + THRRL_ADJUST_DEF;
+		}
+	}
+	else
+	{
+		sfm_reg = 0;
+		thren_reg = 0;
+		thrrl_reg = 0;
+	}
+	iic_write_ex(FBA_INC, fba_inc_reg);
+	iic_write_ex(SFM, sfm_reg);
+	iic_write_ex(THREN, thren_reg);
+	iic_write_ex(THRRL, thrrl_reg);
+
+	
+	if(pOutput_Info->ds_percent_v)
+	{
+		vao_t = pOutput_Info->timing.va * (100 - pOutput_Info->ds_percent_v) / 100;
+	}
+	else
+	{
+		vao_t = pOutput_Info->timing.va;
+	}
+	vao_t = vao_t + (vao_t % 2);
+	val_t = iic_read_ex(INTLC) ? (vao_t / 2) : vao_t;
+	if(pOutput_Info->rotate == ROTATE_90 || pOutput_Info->rotate == ROTATE_270)
+	{
+		if(hai_down <= val_t)
+			lnsel_reg = 3;
+		else
+			lnsel_reg = 1;
+	}
+	else
+	{
+		if(pInput_Info->timing.va <= val_t)
+			lnsel_reg = 3;
+		else
+			lnsel_reg = 1;
+	}
+	if(pPrefer_Info->scale_line_adjust)
+	{
+		lnsel_reg++;
+		if((pOutput_Info->ds_percent_v >= 2) && (pOutput_Info->ds_percent_v <= 12) &&
+            (pOutput_Info->ds_percent_h <= 12) &&
+
+			(pOutput_Info->channel & CHANNEL_VGA) &&
+			(pOutput_Info->timing.ha == 1280) && (pOutput_Info->timing.va == 960) &&
+			(pOutput_Info->timing.hz == 60) && (pInput_Info->rx_clk_khz >= 75428)&&
+			(pInput_Info->timing.ha == 1280) && (pInput_Info->timing.va == 1024)&&
+			(pInput_Info->timing.hz >= 49)){
+			
+				lnsel_reg++;		
+
+		}
+
+		
+
+
+		if((pOutput_Info->ds_percent_v >= 2) && (pOutput_Info->ds_percent_v <= 12) &&
+            (pOutput_Info->ds_percent_h >= 0) && (pOutput_Info->ds_percent_h <= 12) &&
+			(pOutput_Info->channel & CHANNEL_VGA) &&
+			(pOutput_Info->timing.ha == 1280) && (pOutput_Info->timing.va == 1024) &&
+			(pOutput_Info->timing.hz == 60) && (pInput_Info->rx_clk_khz >= 75428)){
+			
+				lnsel_reg++;		
+
+		}
+
+		if((pOutput_Info->ds_percent_v >= 2 ) && 
+            (pOutput_Info->ds_percent_h >= 2 ) && (pOutput_Info->channel & CHANNEL_VGA) &&
+			(pOutput_Info->timing.ha == 1280) && (pOutput_Info->timing.va == 1024) &&
+			(pOutput_Info->timing.hz == 75) && (pInput_Info->rx_clk_khz >= 75428)){
+			
+				lnsel_reg++;
+		}
+
+		if((pOutput_Info->ds_percent_v >= 6 ) && 
+            (pOutput_Info->ds_percent_h >= 6 ) && (pOutput_Info->channel & CHANNEL_HDMI) &&
+			(pOutput_Info->timing.ha == 1920) && (pOutput_Info->timing.va == 1080) &&
+			(pOutput_Info->timing.hz == 50) && (pInput_Info->rx_clk_khz >= 75428)){
+			
+				lnsel_reg++;
+		}
+		if((pOutput_Info->ds_percent_v >= 6 ) && 
+            (pOutput_Info->ds_percent_h >= 6 ) && (pOutput_Info->channel & CHANNEL_HDMI) &&
+			(pOutput_Info->timing.ha == 1920) && (pOutput_Info->timing.va == 1080) &&
+			(pOutput_Info->timing.hz == 59) && (pInput_Info->rx_clk_khz >= 75428)){
+			
+				lnsel_reg++;
+		}
+		if((pOutput_Info->ds_percent_v >= 6 ) && 
+            (pOutput_Info->ds_percent_h >= 6 ) && (pOutput_Info->channel & CHANNEL_HDMI) &&
+			(pOutput_Info->timing.ha == 1920) && (pOutput_Info->timing.va == 1080) &&
+			(pOutput_Info->timing.hz == 60) && (pInput_Info->rx_clk_khz >= 75428)){
+			
+				lnsel_reg++;
+		}
+	}
+	iic_write_ex(LNSEL, lnsel_reg);
+
+	
+	if(frame_rate_in >= field_rate_out)
+		wrfast_reg = 1;
+	else
+		wrfast_reg = 0;
+	iic_write_ex(WRFAST, wrfast_reg);
+
+	
+	chg_hl_reg = (	( 		(pPrefer_Info->dat16_32b == 0)		&&
+					(pPrefer_Info->true24 == 0)		&&
+					(pPrefer_Info->true_com == 0)		&&
+				(	(pOutput_Info->rotate == 0)		&&
+					(pOutput_Info->h_flip == 1)	) )	||
+				(	(pOutput_Info->rotate == 1)		&&
+					(pOutput_Info->h_flip == 0)	)	||
+				(	(pOutput_Info->rotate == 3) 		&&
+					(pOutput_Info->h_flip == 1) 	)	||
+				(	(pOutput_Info->rotate == 2) 		&&
+					(pOutput_Info->h_flip == 0)  )   ) 	? 1 : 0;
+	iic_write_ex(CHG_HL, chg_hl_reg);
+
+	
+	vsmst_reg = 2;
+	iic_write_ex(VSMST, vsmst_reg);
+
+	return ch_true;	
+}
+
+ch_bool post_cal_and_set(DEV_CONTEXT* pDevContext)
+{
+	uint32 hdinc_reg, hdinca_reg, hdincb_reg;
+	uint32 hinc_reg, hinca_reg, hincb_reg;
+	uint32 vinc_reg, vinca_reg, vincb_reg;
+
+	uint32 val_t;
+	uint64 temp1;
+	
+
+	
+	I2CWrite(pDevContext,0x03, 0x04);
+	
+	val_t = I2CRead(pDevContext,0x2A);
+	hinca_reg = (val_t << 3) | (I2CRead(pDevContext,0x2B) & 0x07);
+	
+	val_t = I2CRead(pDevContext,0x2C);
+	hincb_reg = (val_t << 3) | (I2CRead(pDevContext,0x2D) & 0x07);
+	
+	val_t = I2CRead(pDevContext,0x2E);
+	vinca_reg = (val_t << 3) | (I2CRead(pDevContext,0x2F) & 0x07);
+	
+	val_t = I2CRead(pDevContext,0x30);
+	vincb_reg = (val_t << 3) | (I2CRead(pDevContext,0x31) & 0x07);
+	
+	val_t = I2CRead(pDevContext,0x32); 
+	hdinca_reg = (val_t << 3) | (I2CRead(pDevContext,0x33) & 0x07);
+	
+	val_t = I2CRead(pDevContext,0x34);
+	hdincb_reg = (val_t << 3) | (I2CRead(pDevContext,0x35) & 0x07);
+
+	
+	I2CWrite(pDevContext,0x03, 0x04);
+
+	
+	if(iic_read_ex(DNSMPEN))
+	{
+		if(hdincb_reg == 0)
+		{
+			g_nLastError = ERR_CAL_INC_DIV_ZERO;
+			return ch_false;
+		}
+		temp1 = ((uint64)hdinca_reg) * (1 << 20);
+#ifdef T_LINUX
+		do_div(temp1 , hdincb_reg);
+		hdinc_reg = (uint32)temp1;
+#else
+		hdinc_reg = (uint32)((uint64)temp1  / hdincb_reg);
+#endif
+		
+
+		PD_DEBUG ("#1 hdinc_reg = 0x%.8X\n", hdinc_reg);
+		I2CWrite(pDevContext,0x3C, (hdinc_reg >> 16) & 0xFF);
+		I2CWrite(pDevContext,0x3D, (hdinc_reg >>  8) & 0xFF);
+		I2CWrite(pDevContext,0x3E, (hdinc_reg >>  0) & 0xFF);
+	}
+	if(hincb_reg == 0 || vincb_reg == 0)
+	{
+		g_nLastError = ERR_CAL_INC_DIV_ZERO;
+		return ch_false;
+	}
+	
+	if(hinca_reg > hincb_reg)
+	{
+		g_nLastError = ERR_CAL_INC_NO_MATH;
+		return ch_false;
+	}
+
+	temp1 = (uint64)hinca_reg * (1 << 20);
+#ifdef T_LINUX
+	do_div(temp1 , hincb_reg);
+	hinc_reg = (uint32)temp1;
+#else
+	hinc_reg = (uint32)((uint64)temp1 / hincb_reg);
+#endif
+	
+PD_DEBUG ("#2 hinc_reg = 0x%.8X\n", hinc_reg);
+
+	temp1 = (uint64)vinca_reg * (1 << 20);
+#ifdef T_LINUX
+	do_div( temp1 , vincb_reg);
+	vinc_reg = (uint32)temp1;
+#else
+	vinc_reg = (uint32)((uint64)temp1 / vincb_reg);
+#endif
+	
+	PD_DEBUG ("#1 vinc_reg = 0x%.8X\n", vinc_reg);
+	
+	I2CWrite(pDevContext,0x36, (hinc_reg >> 16) & 0xFF);
+	I2CWrite(pDevContext,0x37, (hinc_reg >>  8) & 0xFF);
+	I2CWrite(pDevContext,0x38, (hinc_reg >>  0) & 0xFF);
+
+	I2CWrite(pDevContext,0x39, (vinc_reg >> 16) & 0xFF);
+	I2CWrite(pDevContext,0x3A, (vinc_reg >>  8) & 0xFF);
+	I2CWrite(pDevContext,0x3B, (vinc_reg >>  0) & 0xFF);
+
+	return ch_true;
+}
+
+
+
+
+
+
+ch_bool cal_and_set_power(DEV_CONTEXT* pDevContext)
+{
+	
+	uint8 hdmi_pd = 1;
+	uint8 i2s_pd = 1;
+	uint8 spdif_pd = 1;
+	uint8 auddac=1;
+	uint8 dri_pd = 1;
+	uint8 dri_pd_ser = 1;
+	uint8 dri_pd_pll = 1;
+	uint8 dri_pddri = 7;
+	uint8 cec_pd = 1; 
+	uint8 pd_ddc = 1;
+
+	
+	uint8 lvds_pd = 1;
+	uint8 rx_pd = 63;
+	uint8 rxpll_pd = 1;
+	uint8 txpll_pd = 1;
+	uint8 txdrv_pd = 1;
+	uint8 txser_pd = 1;
+
+	
+	
+
+
+	uint8 icen0 =1;		
+	
+
+	
+	uint8 vga_pd = 1;
+	uint8 pddac = 7;
+
+	uint8 pdpll1 = 1;
+	uint8 pdpll0 = 1;
+
+
+	uint8 pd_prom = 0; 
+	uint8 pdmio = 1;
+	uint8 pdio = 1;
+	uint8 hpd_pd = 0; 
+
+	
+	uint8 sclpd = 1;
+	uint8 sdpd = 1;
+	uint8 mempd = 1;
+	uint8 dacsence = 1;
+
+	uint8 gckoff = 0; 
+
+	
+	uint8 dispon = 0;
+
+
+
+
+	
+	INPUT_INFO* pInput_Info = pDevContext->pInput_Info;
+	OUTPUT_INFO* pOutput_Info = pDevContext->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = pDevContext->pPrefer_Info;
+
+	
+	iic_write_ex(CRYS_FREQ_SPP, 27000);
+	iic_write_ex(I2SCK_SEC, 1);
+	iic_write_ex(SP_EN, 0);
+	iic_write_ex(HARD_SOFTB, 1);
+	iic_write_ex(MULT_I2CEN, 0);
+
+	
+	
+	if(pOutput_Info->channel & CHANNEL_LVDS)
+	{
+		lvds_pd = 0;
+		rx_pd = 0;
+		rxpll_pd = 0;
+		txpll_pd = 0;
+		txdrv_pd = 0;
+		txser_pd = 0;
+		pdio = 0; 
+	
+
+	}
+	if(pOutput_Info->channel & CHANNEL_HDMI)
+	{
+		hdmi_pd = 0;
+		if(pInput_Info->audio_type == 1)
+			i2s_pd = 0;
+		else {
+			spdif_pd = 0;
+			auddac=0;
+		}
+		dri_pd = 0;
+		dri_pddri = 0;
+		dri_pd_ser = 0;
+		dri_pd_pll = 0;		
+		pd_ddc = 0;
+		
+		vga_pd = 0;
+		pdmio = 0;
+		sclpd = 0;
+		sdpd = 0;
+		gckoff = 0;
+		mempd = 0;
+
+		rx_pd = 0; 
+		rxpll_pd = 0;
+		
+		pdpll0 = 0;
+		pdpll1 = 0;
+		icen0 =0;		
+		pdio = 0;
+
+		hpd_pd = 0;
+		
+	}
+	if(pOutput_Info->channel & CHANNEL_VGA)
+	{
+		vga_pd = 0;
+		pdmio = 0;	
+		sclpd = 0;
+		sdpd = 0;
+		gckoff = 0;
+		mempd = 0;
+		
+		rx_pd = 0;
+		rxpll_pd = 0;	
+		
+		pdpll0 = 0;
+		pdpll1 = 0;
+		icen0 =0;
+		pdio = 0;
+
+		dispon = 1;
+		dri_pd_pll = 0;
+				
+		dacsence=1;		
+		pddac = 0;
+	}else{
+		
+		
+
+		if(pPrefer_Info->vga_enable)
+           dispon = 0;
+	}
+
+	
+	iic_write_ex(HDMI_PD,		hdmi_pd);
+	iic_write_ex(I2S_PD,		i2s_pd);
+	iic_write_ex(SPDIF_PD,		spdif_pd);
+
+	iic_write_ex(AUDDAC,        auddac);
+
+	iic_write_ex(DRI_PD,		dri_pd);
+	iic_write_ex(DRI_PD_SER,	dri_pd_ser);
+	iic_write_ex(DRI_PD_PLL,	dri_pd_pll);
+	iic_write_ex(DRI_PDDRI,		dri_pddri);
+	iic_write_ex(CEC_PD,		cec_pd);
+	iic_write_ex(PD_DDC,		pd_ddc);
+	iic_write_ex(LVDS_PD,		lvds_pd);
+	iic_write_ex(RX_PD,			rx_pd);
+	iic_write_ex(RXPLL_PD,		rxpll_pd);
+	iic_write_ex(TXPLL_PD,		txpll_pd);
+	iic_write_ex(TXDRV_PD,		txdrv_pd);
+	iic_write_ex(TXSER_PD,		txser_pd);
+	iic_write_ex(VGA_PD,		vga_pd);
+	iic_write_ex(PDDAC,			pddac);
+	iic_write_ex(PDPLL1,		pdpll1);
+	iic_write_ex(PDPLL0,		pdpll0);
+	iic_write_ex(ICEN0,			icen0);
+	iic_write_ex(PD_PROM,		pd_prom);
+	iic_write_ex(PDMIO,			pdmio);
+	iic_write_ex(PDIO,			pdio);
+	iic_write_ex(HPD_PD,		hpd_pd);
+	iic_write_ex(SCLPD,			sclpd);
+	iic_write_ex(SDPD,			sdpd);
+	iic_write_ex(MEMPD,			mempd);
+	iic_write_ex(DISPON,		dispon);
+	
+	iic_write_ex(DACSENCE,			dacsence);	
+	iic_write_ex(GCKOFF,			gckoff);
+	iic_write_ex(ZRCTS, 0);
+
+	return ch_true;
+}
+
+ch_bool convert_pll1n1_div(ch_bool pll1n1_to_div, uint8* pll1n1_addr, uint8* div_addr)
+{
+	uint8 val_t;
+
+	if(pll1n1_to_div)	
+	{
+		if((*pll1n1_addr) <= 5)
+		{
+			(*div_addr)= 1<<(*pll1n1_addr);
+		}
+		else if((*pll1n1_addr) == 6 || (*pll1n1_addr) == 7) 
+		{
+			(*div_addr) = 64;
+		}
+		else
+		{
+			return ch_false;
+		}
+	}
+	else 
+	{
+		for(val_t=1; val_t<=6; ++val_t)
+		{
+			if(*div_addr == (1 << val_t))
+			{
+				*pll1n1_addr = val_t;
+				return ch_true;
+			}
+		}
+		return ch_false;
+	}
+	return ch_true;
+}
+
+ch_bool convert_pll1n2_div(ch_bool pll1n2_to_div, uint8* pll1n2_addr, uint8* div_addr)
+{
+	uint8 val_t;
+
+	if(pll1n2_to_div)	
+	{
+		if((*pll1n2_addr) <= 3)
+		{
+			*div_addr = 1 << (*pll1n2_addr);
+		}
+		else if((4 == (*pll1n2_addr)) || (6 == (*pll1n2_addr)))
+		{
+			(*div_addr) = 16;
+		}
+		else if((5 == (*pll1n2_addr)) || (7 == (*pll1n2_addr)))
+		{
+			(*div_addr) = 32;
+		} 
+		else 
+		{
+			return ch_false;
+		}
+	}
+	else	
+	{
+		for(val_t=0; val_t<=5; ++val_t)
+		{
+			if(*div_addr == (1 << val_t))
+			{
+				*pll1n2_addr = val_t;
+				return ch_true;
+			}
+		}
+		return ch_false;
+	}
+	return ch_true;
+}
+
+ch_bool convert_pll1n3_div(ch_bool pll1n3_to_div, uint8* pll1n3_addr, uint8* div_addr)
+{
+	uint8 val_t;
+	
+	if(pll1n3_to_div)	
+	{
+		if((*pll1n3_addr) <= 3)
+		{
+			*div_addr = 1 << (*pll1n3_addr);
+		}
+		else if((4 == (*pll1n3_addr)) || (6 == (*pll1n3_addr)))
+		{
+			(*div_addr) = 16;
+		}
+		else if((5 == (*pll1n3_addr)) || (7 == (*pll1n3_addr)))
+		{
+			(*div_addr) = 32;
+		} 
+		else 
+		{
+			return ch_false;
+		}
+	}
+	else	
+	{
+		for(val_t=0; val_t<=5; ++val_t)
+		{
+			if(*div_addr == (1 << val_t))
+			{
+				*pll1n3_addr = val_t;
+				return ch_true;
+			}
+		}
+		return ch_false;
+	}
+	return ch_true;
+}
+
+ch_bool convert_pll2n5_div(ch_bool pll2n5_to_div, uint8* pll2n5_addr, uint8* div_addr)
+{
+	if(pll2n5_to_div)	
+	{
+		switch(*pll2n5_addr)
+		{
+		case 0:		
+			*div_addr = 1;	
+			break;
+		case 4:		
+			*div_addr = 2;	
+			break;
+		case 1:    
+			*div_addr = 3;
+			break;
+		case 8:    
+			*div_addr = 4;
+			break;
+		case 2:    
+			*div_addr = 5;
+			break;
+		case 5:    
+			*div_addr = 6;
+			break;
+		case 3:    
+			*div_addr = 7;
+			break;
+		case 12:
+			*div_addr = 8;
+			break;
+		default :
+			return ch_false;
+		}
+	}
+	else	
+	{
+		switch(*div_addr)
+		{
+		case 1:    
+			*pll2n5_addr = 0;
+			break;
+		case 2:    
+			*pll2n5_addr = 4;
+			break;
+		case 3:    
+			*pll2n5_addr = 1;
+			break;
+		case 4:    
+			*pll2n5_addr = 8;
+			break;
+		case 5:    
+			*pll2n5_addr = 2;
+			break;
+		case 6:    
+			*pll2n5_addr = 5;
+			break;
+		case 7:    
+			*pll2n5_addr = 3;
+			break;
+		case 8:    
+			*pll2n5_addr = 0x0C;
+			break;
+		default : 
+			return ch_false;
+		}
+	}
+	return ch_true;
+}
+
+ch_bool convert_pll2n6_div(ch_bool pll2n6_to_div, uint8* pll2n6_addr, uint8* div_addr)
+{
+	if(pll2n6_to_div)
+	{
+		if(*pll2n6_addr <= 3)
+		{
+			(*div_addr)= (*pll2n6_addr) + 5;
+			return ch_true;
+		}
+		return ch_false;
+	}
+	else
+	{
+		if(*div_addr >= 5 && *div_addr <= 8)
+		{
+			*pll2n6_addr = (*div_addr) - 5;
+			return ch_true;
+		}
+		return ch_false;
+	}
+	return ch_true;
+}
+
+ch_bool convert_pll2n7_div(ch_bool pll2n7_to_div, uint8* pll2n7_addr, uint8* div_addr)
+{
+	if(pll2n7_to_div)
+	{
+		if(*pll2n7_addr <= 3)
+		{
+			(*div_addr)= (*pll2n7_addr) + 8;
+			return ch_true;
+		}
+		return ch_false;
+	}
+	else
+	{
+		if(*div_addr >= 8 && *div_addr <= 11)
+		{
+			*pll2n7_addr = (*div_addr) - 8;
+			return ch_true;
+		}
+		return ch_false;
+	}
+	return ch_true;
+}
+
+ch_bool convert_pll3n8_div(ch_bool pll3n8_to_div, uint8* pll3n8_addr, uint8* div_addr)
+{
+	uint8 val_t;
+
+	if(pll3n8_to_div)	
+	{
+		if(*pll3n8_addr <= 3)
+		{
+			*div_addr= 1 << (*pll3n8_addr);
+			return ch_true;
+		}
+		return ch_false;
+	}
+	else	
+	{
+		for(val_t=0; val_t<=3; ++val_t)
+		{
+			if(*div_addr == (1 << val_t))
+			{
+				*pll3n8_addr = val_t;
+				return ch_true;
+			}
+		}
+        return ch_false;
+	}
+
+	return ch_true;
+}
+
+ch_bool convert_dmxtal_div(ch_bool dmxtal_to_div, uint8* dmxtal_addr, uint8* div_addr)
+{
+	if(dmxtal_to_div)	
+	{
+		switch(*dmxtal_addr)
+		{
+		case 0x00:  
+			*div_addr = 1;
+			break;
+		case 0x04: 
+			*div_addr = 2;
+			break;
+		case 0x08: 
+			*div_addr = 3;
+			break;
+		case 0x0C:  
+			*div_addr = 4;
+			break;
+		case 0x10: 
+			*div_addr = 5;
+			break;
+		case 0x11: 
+			*div_addr = 6;
+			break;
+		case 0x12: 
+			*div_addr = 7;
+			break;
+		case 0x13: 
+			*div_addr = 8;
+			break;
+		case 0x01: 
+			*div_addr = 9;
+			break;
+		case 0x14: 
+			*div_addr = 10;
+			break;
+		case 0x02: 
+			*div_addr = 11;
+			break;
+		case 0x15: 
+			*div_addr = 12;
+			break;
+		case 0x03: 
+			*div_addr = 13;
+			break;
+		case 0x16: 
+			*div_addr = 14;
+			break;
+		case 0x18: 
+			*div_addr = 15;
+			break;
+		case 0x17: 
+			*div_addr = 16;
+			break;
+		default:  
+			return ch_false;
+		}
+	}
+	else	
+	{
+		switch(*div_addr)
+		{
+		case 1:  
+			*dmxtal_addr = 0x00;
+			break;
+		case 2: 
+			*dmxtal_addr = 0x04;
+			break;
+		case 3: 
+			*dmxtal_addr = 0x08;
+			break;
+		case 4: 
+			*dmxtal_addr = 0x0C;
+			break;
+		case 5:
+			*dmxtal_addr = 0x10;
+			break;
+		case  6: 
+			*dmxtal_addr = 0x11;
+			break;
+		case 7: 
+			*dmxtal_addr = 0x12;
+			break;
+		case 8: 
+			*dmxtal_addr = 0x13;
+			break;
+		case 9: 
+			*dmxtal_addr = 0x01;
+			break;
+		case 10: 
+			*dmxtal_addr = 0x14;
+			break;
+		case 11: 
+			*dmxtal_addr = 0x02;
+			break;
+		case 12: 
+			*dmxtal_addr = 0x15;
+			break;
+		case 13: 
+			*dmxtal_addr = 0x03;
+			break;
+		case 14: 
+			*dmxtal_addr = 0x16;
+			break;
+		case 15:
+			*dmxtal_addr = 0x18;
+			break;
+		case 16: 
+			*dmxtal_addr = 0x17;
+			break;
+		default: 
+			return ch_false;
+		}
+	}
+	return ch_true;
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.def b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.def
new file mode 100644
index 0000000..49af6a5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.def
@@ -0,0 +1,5 @@
+LIBRARY ch7036
+
+EXPORTS
+    get_version
+    dpd_init
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.h
new file mode 100755
index 0000000..9452854
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036.h
@@ -0,0 +1,51 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036.h
+* @version 1.2.2
+*-----------------------------------------------------------------------------
+*/
+
+#ifndef _CH7036_H
+#define _CH7036_H
+
+#include "ch7036_typedef.h"
+
+
+
+ch_bool DevicePrepare(DEV_CONTEXT* pDevContext);
+	
+
+ch_bool DeviceConfig(DEV_CONTEXT* pDevContext);
+
+
+ch_bool DeviceRunning(DEV_CONTEXT* pDevContext);
+
+
+ch_bool DeviceSetPower(DEV_CONTEXT* pDevContext, unsigned long flags);
+
+
+uint32 GetLastErrorMessage(void);
+
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.c
new file mode 100644
index 0000000..fb5cdfe
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.c
@@ -0,0 +1,1061 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_attr.c
+* @version 1.2.5
+*-----------------------------------------------------------------------------
+*/
+ 
+
+
+
+#include "ch7036_intf.h"
+#include "ch7036_attr.h"
+
+
+#ifdef T_LINUX
+	#include "lvds/lvds.h"
+#else
+	#include "lvds.h"
+#endif
+
+
+
+static pd_attr_t g_ch7036_attrs[] =
+{
+	
+
+	
+	PD_MAKE_ATTR (PD_ATTR_ID_HPOSITION,   PD_ATTR_TYPE_RANGE, "H Pos.",  0, DEFAULT_POSITION,	DEFAULT_POSITION,                    DEFAULT_POSITION - 40,  DEFAULT_POSITION + 40,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_VPOSITION,   PD_ATTR_TYPE_RANGE, "V Pos.",  0, DEFAULT_POSITION,	DEFAULT_POSITION,                    DEFAULT_POSITION - 30,  DEFAULT_POSITION + 30,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_HSCALE,   PD_ATTR_TYPE_RANGE, "H_Scale",  0, HDMI_DEFAULT_UNDERSCAN,   HDMI_DEFAULT_UNDERSCAN,  10,  20,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_VSCALE,   PD_ATTR_TYPE_RANGE, "V_Scale",  0, HDMI_DEFAULT_UNDERSCAN,     HDMI_DEFAULT_UNDERSCAN,   10,  20,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_HSCALE_CRT,   PD_ATTR_TYPE_RANGE, "H_Scale",  0, CRT_DEFAULT_UNDERSCAN,   CRT_DEFAULT_UNDERSCAN,  10,  20,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_VSCALE_CRT,   PD_ATTR_TYPE_RANGE, "V_Scale",  0, CRT_DEFAULT_UNDERSCAN,   CRT_DEFAULT_UNDERSCAN,  10,  20,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_DITHER,   PD_ATTR_TYPE_RANGE, "Dither", PD_ATTR_FLAG_USER_INVISIBLE, 0,   0,  0,  3,  1),
+	
+	PD_MAKE_ATTR (PD_ATTR_ID_TEXT_FILTER,   PD_ATTR_TYPE_RANGE, "Text Tuning", PD_ATTR_FLAG_USER_INVISIBLE, 4,   DEFAULT_TEXT_ENHANCE,  0,  7,  1),
+	
+
+	PD_MAKE_ATTR (PD_ATTR_ID_DITHER_BYPASS, PD_ATTR_TYPE_BOOL, "Quality Enhance",  0, 0,                 0,                 0, 0, 0),
+	
+	PD_MAKE_ATTR (PD_ATTR_ID_PLL_REF_DLY, PD_ATTR_TYPE_BOOL, "Pll Reference Delay",PD_ATTR_FLAG_USER_INVISIBLE, 0,0,0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_PLL_REF_FBDLY,PD_ATTR_TYPE_BOOL, "Pll Reference FBDelay",PD_ATTR_FLAG_USER_INVISIBLE, 1,   1, 0, 0, 0),
+	
+	PD_MAKE_ATTR (PD_ATTR_ID_LOAD_FIRMWARE,PD_ATTR_TYPE_BOOL, "Load Firmware",PD_ATTR_FLAG_USER_INVISIBLE, 0, 1, 0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_REFRESH,PD_ATTR_TYPE_BOOL, "Refresh",0, 0,   0, 0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_DWNSCAL_BYPASS,PD_ATTR_TYPE_BOOL, "Remove_DS",PD_ATTR_FLAG_USER_INVISIBLE, 0, 1, 0, 0, 0),
+	
+};
+
+
+static ch7036_attr_list_entry_t g_list_entry_hdmi[] =
+{
+	{OUT_HDMI_640x480P_59,		"640x480p_59",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_HDMI_640x480P_60,		"640x480p_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_HDMI_720x480P_59,		"720x480p_59",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_HDMI_720x480P_60,		"720x480p_60",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_HDMI_720x576P_50,     "720x576p_50",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_HDMI_1280x720P_59,    "1280x720p_59",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_HDMI_1280x720P_60,    "1280x720p_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_HDMI_1920x1080I_59,   "1920x1080i_59",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_HDMI_1920x1080I_60,   "1920x1080i_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_HDMI_1920x1080P_59,   "1920x1080p_59",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_HDMI_1920x1080P_60,   "1920x1080p_60",PD_ATTR_FLAG_DYNAMIC}, 
+	
+	{OUT_HDMI_1280x720P_50,    "1280x720p_50",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_HDMI_1920x1080I_50,   "1920x1080i_50",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_HDMI_1920x1080P_50,   "1920x1080p_50",PD_ATTR_FLAG_DYNAMIC},
+	{0, NULL,0}
+};
+
+
+
+static ch7036_attr_list_entry_t g_list_entry_dvi[] =
+{
+
+	{OUT_DVI_640x480_60,	"640x480_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_DVI_640x480_72,	"640x480_72",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_640x480_75,	"640x480_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_720x400_70,	"720x400_70",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_800x600_56,	"800x600_56",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_800x600_60,	"800x600_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_DVI_800x600_72,	"800x600_72",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_800x600_75,	"800x600_75",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_1024x768_60,	"1024x768_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_DVI_1024x768_70,	"1024x768_70",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1024x768_75,	"1024x768_75",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_1152x864_60,	"1152x864_60",PD_ATTR_FLAG_DYNAMIC}, 
+
+	{OUT_DVI_1280x720_60,   "1280x720_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_DVI_1280x800_60,   "1280x800_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1280x960_60,    "1280x960_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1280x1024_60,   "1280x1024_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_DVI_1280x1024_75,   "1280x1024_75",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_1360x768_60,    "1360x768_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1366x768_60,    "1366x768_60",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_1400x1050_60,   "1400x1050_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1400x1050_75,   "1400x1050_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1440x900_60,    "1440x900_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1440x1050_60,   "1440x1050_60",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_1600x900_60,    "1600x900_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_DVI_1600x1200_60,   "1600x1200_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_DVI_1680x1050_60,   "1680x1050_60",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_DVI_1920x1080_60,   "1920x1080_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{0, NULL,0}
+};
+
+
+
+static ch7036_attr_list_entry_t g_list_entry_crt[] =
+{
+	{OUT_CRT_640x400_85, "640x400_85",PD_ATTR_FLAG_DYNAMIC}, 
+
+	{OUT_CRT_640x480_60, "640x480_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_640x480_72, "640x480_72",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_640x480_75, "640x480_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_640x480_85, "640x480_85",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_720x400_85, "720x400_85",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_800x600_56, "800x600_56",PD_ATTR_FLAG_DYNAMIC},
+	
+	{OUT_CRT_800x600_60, "800x600_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_CRT_800x600_72, "800x600_72",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_800x600_75, "800x600_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_800x600_85, "800x600_85",PD_ATTR_FLAG_DYNAMIC}, 
+	
+	{OUT_CRT_1024x768_60,"1024x768_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_CRT_1024x768_70,"1024x768_70",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1024x768_75,"1024x768_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1024x768_85,"1024x768_85",PD_ATTR_FLAG_DYNAMIC},
+	
+
+	{OUT_CRT_1152x864_75,"1152x864_75",PD_ATTR_FLAG_DYNAMIC}, 
+
+	{OUT_CRT_1280x768_60,"1280x768_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1280x768_75,"1280x768_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1280x768_85,"1280x768_85",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_1280x960_60,"1280x960_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1280x960_85,"1280x960_85",PD_ATTR_FLAG_DYNAMIC},
+
+
+	{OUT_CRT_1280x1024_60,"1280x1024_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_CRT_1280x1024_75,"1280x1024_75",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1280x1024_85,"1280x1024_85",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_1360x768_60,"1360x768_60",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_1400x1050_60,"1400x1050_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1400x1050_75,"1400x1050_75",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_1440x900_60,"1440x900_60",PD_ATTR_FLAG_DYNAMIC},
+	{OUT_CRT_1440x1050_60,"1440x1050_60",PD_ATTR_FLAG_DYNAMIC},
+
+	{OUT_CRT_1600x900_60,"1600x900_60",PD_ATTR_FLAG_DYNAMIC},
+	
+	{OUT_CRT_1600x1200_60,"1600x1200_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{OUT_CRT_1920x1080_60,"1920x1080_60",PD_ATTR_FLAG_DYNAMIC}, 
+	{0, NULL,0}
+};
+
+
+static ch7036_attr_list_entry_t g_list_entry_channel[] =
+{
+	{CHANNEL_AUTO_DETECT,"Auto Detect",PD_ATTR_FLAG_DYNAMIC},
+	{CHANNEL_LVDS_HDMI, "LVDS_HDMI",PD_ATTR_FLAG_DYNAMIC}, 
+	{CHANNEL_LVDS_DVI, "LVDS_DVI",PD_ATTR_FLAG_DYNAMIC},
+	{CHANNEL_LVDS_VGA,  "LVDS_VGA",PD_ATTR_FLAG_DYNAMIC},
+	{CHANNEL_HDMI, "HDMI",PD_ATTR_FLAG_DYNAMIC}, 
+	{CHANNEL_DVI, "DVI",PD_ATTR_FLAG_DYNAMIC},
+	{CHANNEL_VGA,  "VGA",PD_ATTR_FLAG_DYNAMIC},
+	{0, NULL,0}	
+};
+
+static ch7036_attr_list_header_t g_list_header[] =
+{
+	{7, PD_ATTR_ID_DISPLAY,"Display Channel", 1,1, 
+	g_list_entry_channel},
+
+	{14, PD_ATTR_ID_HDMI_OUT_MODE, "HDMI",7,7,
+	 g_list_entry_hdmi},
+
+	{27, PD_ATTR_ID_DVI_OUT_MODE,"DVI", 9,9,
+	 g_list_entry_dvi},
+
+	{32, PD_ATTR_ID_CRT_OUT_MODE, "VGA", 12,12,
+	 g_list_entry_crt},
+
+	{ 0 }
+};
+
+
+
+ch7036_status_t ch7036_init_attribute_table(ch7036_device_context_t *p_ctx, ch7036_attr_list_header_t* p_list_header)
+{
+	unsigned long ch7036_num_attrs, num_attrs;
+	unsigned char *p_table;
+	
+
+	
+	ch7036_num_attrs = ch7036_enumerate_attr_table(p_ctx, NULL, p_list_header);
+	PD_DEBUG("ch7036_init_attribute_table- ch7036 num_attr = [%lu]\n",ch7036_num_attrs);
+
+	
+	p_ctx->ch7036_num_attrs =  ch7036_num_attrs ;  
+
+
+	if(p_ctx->p_ch7036_attr_table) {
+		pd_free(p_ctx->p_ch7036_attr_table);
+		p_ctx->p_ch7036_attr_table = NULL;
+	}
+
+	
+	if(p_ctx->p_ch7036_attr_table == NULL) {
+	
+		p_ctx->p_ch7036_attr_table = pd_malloc((p_ctx->ch7036_num_attrs + 1) * sizeof(pd_attr_t)); 
+
+		if (p_ctx->p_ch7036_attr_table == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_init_attribute_table: pd_malloc() failed allocating ch7036_attr_table");
+		
+			return SS_MEM_ALLOC_ERR;
+		}
+
+		pd_memset(p_ctx->p_ch7036_attr_table, 0, (p_ctx->ch7036_num_attrs + 1) *
+			sizeof(pd_attr_t));
+
+	}
+
+
+	p_table = (unsigned char *)p_ctx->p_ch7036_attr_table;
+
+
+	if (p_ctx->ch7036_num_attrs  > 0) {
+
+		num_attrs = ch7036_enumerate_attr_table(p_ctx, (pd_attr_t *)p_table, p_list_header);
+
+
+		PD_DEBUG("ch7036_init_attribute_table: ch7036_enumerate_attr_table()- returned %lu entries, expected %lu\n",
+					  num_attrs, p_ctx->ch7036_num_attrs) ;
+
+	}
+
+	
+	return SS_SUCCESS;
+
+}
+
+
+
+unsigned long ch7036_enumerate_attr_table (ch7036_device_context_t *p_ctx,
+	pd_attr_t *p_attr, ch7036_attr_list_header_t* p_list_header)
+{
+
+	
+	unsigned long num_attrs, i, j, num_attrs_static;
+	ch7036_attr_list_entry_t *list_item;
+	int ret;
+	pd_attr_t *p_table;
+
+	list_item = NULL;
+	num_attrs = 0;
+	i = j = 0;
+
+	
+	PD_DEBUG("ch7036_enumerate_attr_table- enter\n");
+
+
+	
+	if ( p_list_header == NULL )
+			p_list_header = g_list_header; 
+		
+	
+	if (p_attr == NULL) {
+
+		if(p_ctx->p_ch7036_attr_table == NULL) { 
+			ret = PD_INTERNAL_LVDS_MODULE_GET_ATTRIBUTES(ch7036_lvds_get_attrs,(p_ctx->internal_lvds,&(p_ctx->lvds_num_attrs),&(p_ctx->p_lvds_attr_table))); 
+
+			if(ret != PD_SUCCESS)
+				PD_DEBUG("ch7036_enumerate_attr_table: lvds_get_attributes() return ERROR! check this routine\n");
+			else
+				PD_DEBUG("ch7036_enumerate_attr_table: lvds_get_attributes()- lvds num_attr = [%lu]\n",p_ctx->lvds_num_attrs);
+		}
+		
+				
+
+		while (p_list_header[i].num_entries) {
+
+
+			num_attrs += (p_list_header[i].num_entries + 1); 
+			++i;	 
+		}
+
+		num_attrs += ((sizeof(g_ch7036_attrs)/sizeof(pd_attr_t)) + p_ctx->lvds_num_attrs) ;
+
+		return num_attrs; 
+	}
+
+	
+	if(p_ctx->lvds_num_attrs > 0) {
+
+		pd_attr_t *p_lvds_des, *p_lvds_src ;
+
+		
+		p_lvds_src=  (pd_attr_t *)p_ctx->p_lvds_attr_table;
+		p_lvds_des = (pd_attr_t *)p_attr;
+
+		pd_memcpy(p_lvds_des, p_lvds_src, (p_ctx->lvds_num_attrs)*sizeof(pd_attr_t));
+		num_attrs += p_ctx->lvds_num_attrs;
+
+		for (i=0 ; i < p_ctx->lvds_num_attrs; i++, p_lvds_des++) {
+
+			PD_DEBUG("ch7036 : ch7036_enumerate_attr_table : \n"
+				  "adding lvds attr='%s', id=%lu, default=%lu, current=%lu, \n",
+				  p_lvds_des->name, p_lvds_des->id, p_lvds_des->default_value, p_lvds_des->current_value);
+		}
+
+	}
+
+	PD_DEBUG("ch7036 : ch7036_enumerate_attr_table adding lvds num attrs = %lu \n", num_attrs);
+
+	
+
+	i=0;
+
+
+	while (p_list_header[i].num_entries) {
+
+		pd_list_attr_t *p_hdr = (pd_list_attr_t *) &p_attr[num_attrs];
+
+	
+		p_hdr->type	= PD_ATTR_TYPE_LIST;
+		p_hdr->id = p_list_header[i].id;
+		p_hdr->num_entries = p_list_header[i].num_entries;
+		p_hdr->flags |= PD_ATTR_FLAG_DYNAMIC;
+
+		p_hdr->current_index = p_list_header[i].current_index;
+		pd_strcpy(p_hdr->name, p_list_header[i].p_name);
+
+		p_hdr->default_index = p_list_header[i].default_index;
+
+		PD_DEBUG("ch7036 : ch7036_enumerate_attr_table : \n"
+				  "adding attr - list header='%s', id=%lu, default=%lu, current=%lu, \n"
+				  "num_entries=%lu\n",
+				  p_hdr->name, p_hdr->id, p_hdr->default_index, p_hdr->current_index,
+				  p_hdr->num_entries);
+
+		++num_attrs;
+
+
+
+		list_item = p_list_header[i].attr_list;
+		for (j = 0; j < p_list_header[i].num_entries; ++j, ++num_attrs) {
+
+			pd_list_entry_attr_t *p_entry =
+				(pd_list_entry_attr_t *)&p_attr[num_attrs];
+
+			p_entry->id		= p_list_header[i].id;  
+			p_entry->type	= PD_ATTR_TYPE_LIST_ENTRY;
+			p_entry->value	= list_item[j].id;     
+			
+
+			p_entry->flags  = list_item[j].flags; 
+
+			
+			
+			
+			
+
+			
+
+			pd_strcpy(p_entry->name, list_item[j].p_name);
+
+			
+
+			PD_DEBUG("ch7036 : ch7036_enumerate_attr_table : list entry[%lu]=%s, id=[%lu], "
+					  "value=%lu, flags=0x%x \n",
+					  j, p_entry->name, p_entry->id,
+					  p_entry->value, (unsigned char)p_entry->flags);
+					  
+		}
+
+		++i;   
+	}
+
+	
+	p_table = (pd_attr_t *)&p_attr[num_attrs];
+	num_attrs_static = sizeof(g_ch7036_attrs)/sizeof(pd_attr_t);
+	pd_memcpy(p_table, g_ch7036_attrs,num_attrs_static*sizeof(pd_attr_t));
+
+	for (i=0 ; i < num_attrs_static; i++, p_table++) {
+
+		PD_DEBUG("ch7036 : ch7036_enumerate_attr_table : \n"
+				  "adding ch7036 static attr='%s', id=%lu, default=%lu, current=%lu, \n",
+				  p_table->name, p_table->id, p_table->default_value, p_table->current_value);
+
+	}
+
+	num_attrs += num_attrs_static;
+
+
+	return num_attrs;
+
+}
+
+void ch7036_set_dither(ch7036_device_context_t* p_ctx)
+{
+	uint8 reg=0x00;
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+	
+	I2CWrite(p_ch_ctx,0x03, 0x04);
+	reg = I2CRead(p_ch_ctx,0x57);
+    reg = reg & 0x3F;
+
+
+	switch(p_ctx->dither_select)
+	{
+		  case DITHER_18_TO_18: 
+			  reg &= 0x3F;
+			  break;
+		  case DITHER_18_TO_24: 
+			  reg = reg | 0x40;
+			  break;
+		  case DITHER_24_TO_18: 
+			  reg = reg | 0x80;		    
+			  break;
+		  case DITHER_24_TO_24: 
+			  reg = reg | 0xC0;
+			  break;
+		  default:
+			 break;
+    }
+	I2CWrite(p_ch_ctx,0x57, reg);
+
+	return;
+}
+
+
+
+void ch7036_set_audio_type(INPUT_INFO* pInput_Info, uint8 id)
+{
+	
+	pInput_Info->audio_type = id; 
+	
+	if(pInput_Info->audio_type == AUDIO_I2S) {
+		
+		
+		
+		
+		pInput_Info->i2s_pol = 0; 
+		pInput_Info->i2s_len = 0; 
+		pInput_Info->i2s_fmt = 0; 
+		
+	}
+}
+
+
+
+ch7036_status_t ch7036_set_output_channel(void* p_context, uint32 channel)
+{
+	ch7036_device_context_t * p_ctx= (ch7036_device_context_t *)p_context;
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	pd_attr_t *p_attr_hdmi, *p_attr_crt, *p_attr_dvi;
+	pd_attr_t *p_attr_hscale, *p_attr_vscale, *p_attr_hscale_crt, *p_attr_vscale_crt, *p_attr_hp, *p_attr_vp;
+	pd_attr_t* p_attr_dither;
+
+
+	
+	PD_DEBUG("ch7036_set_output_channel- channel [%x]\n", channel);
+	
+	pOutput_Info->channel = channel;
+
+
+
+	p_attr_dvi = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+				PD_ATTR_ID_DVI_OUT_MODE, PD_GET_ATTR_LIST);
+
+	p_attr_hdmi = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+				PD_ATTR_ID_HDMI_OUT_MODE, PD_GET_ATTR_LIST);
+	
+
+	p_attr_crt = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+			PD_ATTR_ID_CRT_OUT_MODE, PD_GET_ATTR_LIST);
+
+	
+	p_attr_hscale_crt = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+			PD_ATTR_ID_HSCALE_CRT, 0);
+	p_attr_vscale_crt = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+			PD_ATTR_ID_VSCALE_CRT, 0);
+	
+	p_attr_hscale = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+			PD_ATTR_ID_HSCALE, 0);
+	p_attr_vscale = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+			PD_ATTR_ID_VSCALE, 0);
+
+	p_attr_hp = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+				PD_ATTR_ID_HPOSITION, 0);
+	p_attr_vp = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+				PD_ATTR_ID_VPOSITION, 0);
+
+	p_attr_dither = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+				PD_ATTR_ID_DITHER_BYPASS, 0);
+			
+	
+
+	if(pOutput_Info->channel & CHANNEL_HDMI) {
+
+		if(pOutput_Info->hdmi_fmt.is_dvi_mode) {
+
+			if(p_attr_hdmi) 
+				p_attr_hdmi->flags |= PD_ATTR_FLAG_USER_INVISIBLE;  
+
+			p_attr_dvi->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+
+			
+			p_attr_dvi += p_attr_dvi->current_value; 
+			p_ctx->dvi_mode_index = ((pd_list_entry_attr_t *)p_attr_dvi)->value;
+
+			PD_DEBUG("ch7036_set_output_channel- dvi mode index [%hu]\n",p_ctx->dvi_mode_index);
+
+
+
+		}
+		else { 
+			
+			p_attr_hdmi->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+
+			if(p_attr_dvi) 
+				p_attr_dvi->flags |= PD_ATTR_FLAG_USER_INVISIBLE;  
+
+			
+			p_attr_hdmi += p_attr_hdmi->current_value; 
+			p_ctx->hdmi_mode_index = ((pd_list_entry_attr_t *)p_attr_hdmi)->value;
+
+			PD_DEBUG("ch7036_set_output_channel- hdmi mode index [%hu]\n",p_ctx->hdmi_mode_index);
+
+		}
+
+		if(p_attr_crt)
+			p_attr_crt->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		
+		//hdmi/dvi- hide it from user
+		p_attr_hscale_crt->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vscale_crt->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		//h/v pos scale- hide it
+		p_attr_hp->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vp->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		
+		//show it
+		p_attr_hscale->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vscale->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+
+
+		p_attr_dither->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+
+
+		PD_DEBUG("ch7036_set_output_channel-current hscale value [%d]\n",(uint8)p_attr_hscale->current_value);
+		PD_DEBUG("ch7036_set_output_channel-current vscale value [%d]\n",(uint8)p_attr_vscale->current_value);
+		
+		ch7036_set_scaling(pOutput_Info,PD_ATTR_ID_HSCALE,(uint8)p_attr_hscale->current_value);
+		ch7036_set_scaling(pOutput_Info,PD_ATTR_ID_VSCALE,(uint8)p_attr_vscale->current_value);
+
+
+	} else if (pOutput_Info->channel & CHANNEL_VGA) {
+		
+		p_attr_crt->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+		
+		if(p_attr_hdmi)
+			p_attr_hdmi->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+		if(p_attr_dvi)
+			p_attr_dvi->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+		p_attr_crt += p_attr_crt->current_value; 
+		p_ctx->crt_mode_index = ((pd_list_entry_attr_t *)p_attr_crt)->value;
+	
+		PD_DEBUG("ch7036_set_output_channel- crt mode index [%hu]\n",p_ctx->crt_mode_index);
+
+		
+		p_attr_hscale_crt->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vscale_crt->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+		
+		p_attr_hp->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vp->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+
+		p_attr_dither->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE;
+
+
+		p_attr_hscale->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vscale->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+		PD_DEBUG("ch7036_set_output_channel-current hscale_crt value [%d]\n",(uint8)p_attr_hscale_crt->current_value);
+		PD_DEBUG("ch7036_set_output_channel-current vscale_crt value [%d]\n",(uint8)p_attr_vscale_crt->current_value);
+
+		
+		ch7036_set_scaling(pOutput_Info,PD_ATTR_ID_HSCALE_CRT,(uint8)p_attr_hscale_crt->current_value);
+		ch7036_set_scaling(pOutput_Info,PD_ATTR_ID_VSCALE_CRT,(uint8)p_attr_vscale_crt->current_value);
+
+		
+	} else {
+
+		if(p_attr_crt)
+			p_attr_crt->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		if(p_attr_hdmi)
+			p_attr_hdmi->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		if(p_attr_dvi)
+			p_attr_dvi->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+		
+		p_attr_hscale->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vscale->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+		p_attr_hscale_crt->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+		p_attr_vscale_crt->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+	
+		p_attr_hp->flags |= PD_ATTR_FLAG_USER_INVISIBLE;		
+		p_attr_vp->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+		p_attr_dither->flags |= PD_ATTR_FLAG_USER_INVISIBLE;
+
+
+	}
+	
+
+	return SS_SUCCESS;
+}
+unsigned long ch7036_get_output_channel(void* p_context)
+{
+	ch7036_device_context_t * p_ctx= (ch7036_device_context_t *)p_context;
+	pd_list_entry_attr_t *p_attr;
+	uint32 channel = 0;
+
+	PD_DEBUG("ch7036_get_output_channel- enter\n");
+
+	p_attr = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,
+			PD_ATTR_ID_DISPLAY, PD_GET_ATTR_LIST_ENTRY);
+
+	if(p_attr->value & CHANNEL_DVI)
+		channel = (p_attr->value & 0xF7) | CHANNEL_HDMI;
+
+	return channel;
+}
+
+ch7036_status_t ch7036_set_position(ch7036_device_context_t *p_ctx, uint8 attr_id, uint16 pos)
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+
+	uint8 reg;
+
+	PD_DEBUG("ch7036_set_position- enter\n");
+	I2CWrite(p_ch7xxx_context,0x03, 0x00);
+	reg = I2CRead(p_ch7xxx_context,0x39);
+	
+	if(attr_id == PD_ATTR_ID_HPOSITION) {
+		reg = reg & 0xF0; 
+		I2CWrite(p_ch7xxx_context,0x3A, (pos & 0x00FF));
+		reg = ((pos >> 8) & 0x0F) | reg;	
+		iic_write_ex(HP,pos);
+	} 
+	else { 
+		reg = reg & 0x0F;
+		I2CWrite(p_ch7xxx_context,0x3B, (pos & 0x00FF));
+		reg = (((pos >> 8) & 0x0F) << 4) | reg;	
+		iic_write_ex(VP,pos);
+	}
+
+	I2CWrite(p_ch7xxx_context,0x39, reg);
+	
+	return SS_SUCCESS;
+}
+
+
+void ch7036_set_hdmi_sync_polarity(OUTPUT_INFO* pOutput_Info)
+{
+	
+	if((pOutput_Info->channel & CHANNEL_HDMI)&&(pOutput_Info->channel & CHANNEL_VGA))
+	{
+		if (pOutput_Info->timing.ha > 720) {
+			pOutput_Info->hdmi_fmt.hs_pol = POL_HIGH;
+			pOutput_Info->hdmi_fmt.vs_pol = POL_HIGH;
+			if(pOutput_Info->timing.ha == 1440 )
+			{
+              pOutput_Info->hdmi_fmt.hs_pol = POL_LOW;
+			  pOutput_Info->hdmi_fmt.vs_pol = POL_LOW;
+			}
+		}
+		else if(pOutput_Info->timing.ha <= 720){
+			
+			pOutput_Info->hdmi_fmt.hs_pol = POL_LOW;
+			pOutput_Info->hdmi_fmt.vs_pol = POL_LOW;
+		}
+	}
+	
+	
+	if((pOutput_Info->channel & CHANNEL_HDMI) && ((pOutput_Info->channel & CHANNEL_VGA) == 0x00))
+	{	
+		pOutput_Info->hdmi_fmt.hs_pol = POL_LOW;
+		pOutput_Info->hdmi_fmt.vs_pol = POL_LOW;
+	}
+
+}
+
+void ch7036_set_quality_enhancement(ch7036_device_context_t *p_ctx, uint8 checked)
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	PREFER_INFO* pPrefer_Info = p_ch7xxx_context->pPrefer_Info;
+	uint8 reg;
+
+	PD_DEBUG("ch7036_set_quality_enhancement- enter\n");
+	I2CWrite(p_ch7xxx_context,0x03, 0x00);	
+	reg = I2CRead(p_ch7xxx_context,0x19); 
+	
+	if(checked) 
+		reg = reg & 0xBF; 
+	else
+		reg = reg | 0x40; 
+	
+	I2CWrite(p_ch7xxx_context,0x19, reg);
+
+	
+	pPrefer_Info->dither_filter_enable = checked?DITHER_ENABLE:DITHER_BYPASS;
+
+	
+	iic_write_ex(DBP, checked?DITHER_ENABLE:DITHER_BYPASS);
+	
+}
+
+void ch7036_set_scaling (OUTPUT_INFO* pOutput_Info, unsigned long id, uint8 value)
+{
+	uint8* p_scale;
+
+	PD_DEBUG("ch7036_set_scaling- enter- value [%d]\n", value);
+		
+	if(id == PD_ATTR_ID_HSCALE || id == PD_ATTR_ID_HSCALE_CRT)
+		p_scale= &(pOutput_Info->ds_percent_h);
+	else 
+		p_scale= &(pOutput_Info->ds_percent_v);
+		
+	switch(value) {
+		case 20: *p_scale = 0; break; 
+		case 19: *p_scale = 1; break; 
+		case 18: *p_scale = 2; break;
+		case 17: *p_scale = 3; break;
+		case 16: *p_scale = 4; break;
+		case 15: *p_scale = 5; break;
+		case 14: *p_scale = 6; break;
+		case 13: *p_scale = 7; break;
+		case 12: *p_scale = 8; break;
+		case 11: *p_scale = 9; break;
+		case 10: *p_scale = 10; break;
+		
+		case 9: *p_scale = 11; break;
+		case 8: *p_scale = 12; break;
+		case 7: *p_scale = 13; break;
+		case 6: *p_scale = 14; break;
+		case 5: *p_scale = 15; break;
+		case 4: *p_scale = 16; break;
+		case 3: *p_scale = 17; break;
+		case 2: *p_scale = 18; break;
+		case 1: *p_scale = 19; break;
+		case 0: *p_scale = 20; break;
+		default: break;
+			
+	}
+
+	return;
+
+}
+
+void ch7036_set_rotate (OUTPUT_INFO* pOutput_Info)
+{
+	
+	
+	pOutput_Info->rotate = DEFAULT_ROTATE; 
+}
+
+void ch7036_set_hflip (OUTPUT_INFO* pOutput_Info)
+{
+	
+	
+	pOutput_Info->h_flip = DEFAULT_HFLIP; 
+}
+void ch7036_set_vflip (OUTPUT_INFO* pOutput_Info)
+{
+	
+	
+	pOutput_Info->v_flip = DEFAULT_VFLIP; 
+}
+
+void ch7036_set_text_enhancement (ch7036_device_context_t *p_ctx, uint8 val)
+{
+	
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	PREFER_INFO* pPrefer_Info = p_ch7xxx_context->pPrefer_Info;
+	
+
+	PD_DEBUG("ch7036_set_text_enhancement- enter\n");
+
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x00);
+	I2CWrite(p_ch7xxx_context,0x2E, (I2CRead(p_ch7xxx_context,0x2E) & 0xF8) |  val);
+	
+	pPrefer_Info->text_enhancement = val; 
+	iic_write_ex(TXTEN, val);
+}
+
+
+void ch7036_set_pll_refdly(PREFER_INFO* pPrefer_Info)
+{
+	pPrefer_Info->pll_ref_dly = DEF_PLL_REF_DLY;
+}
+
+void ch7036_set_pll_fbdly(PREFER_INFO* pPrefer_Info)
+{
+	pPrefer_Info->pll_ref_fbdly = DEF_PLL_REF_FBDLY;
+}
+
+void ch7036_set_lvds_txdrv_ctrl(PREFER_INFO* pPrefer_Info)
+{
+	pPrefer_Info->lvds_txdrv_ctrl = DEF_LVDS_TXDRV_CTRL;
+}
+
+
+
+ch7036_status_t ch7036_alter_display_channel (ch7036_device_context_t *p_ctx) 
+{
+
+
+	ch7036_status_t status;
+
+
+	pd_list_entry_attr_t  *list_item;
+
+	ch7036_edid_blk_t* p_hedid = (ch7036_edid_blk_t *)p_ctx->hedid;
+	ch7036_edid_blk_t* p_cedid = (ch7036_edid_blk_t *)p_ctx->cedid;
+
+
+	list_item = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_DISPLAY,
+					PD_GET_ATTR_LIST_ENTRY); 
+	
+	PD_DEBUG("ch7036_alter_display_channel=> enter- list_item->value [0x%lu]\n",list_item->value);
+	
+	switch (p_ctx->hpd & 0x22) {
+	
+		case 0x22: 
+		case 0x20:
+			PD_DEBUG("ch7036_alter_display_channel=> hdmi-dvi hpd status: attached... \n");
+			if(p_ctx->hpd & 0x41 ) { 
+
+				status = ch7036_read_edid(p_ctx, CHANNEL_LVDS_HDMI);
+
+		
+				if (status == SS_SUCCESS) {
+
+					if( p_ctx->man_sel_out==1 ) 
+						p_ctx->hpd &= 0x3F; //need parsing but dont do edid again
+					else													
+						p_ctx->hpd &= 0x7F; 
+					
+					
+					PD_DEBUG("ch7036_alter_display_channel=> hdmi-dvi, status changed,edid read is a success- proceed to alter attr table...\n");
+					
+				}
+				else { 
+
+					PD_DEBUG("ch7036_alter_display_channel=> hdmi-dvi, status changed,edid read failed- use default attr table- show all modes..\n");
+					
+
+				}
+
+					
+
+			}
+			else {
+
+				PD_DEBUG("ch7036_alter_display_channel=> hdmi/dvi, status unchanged,check if edid read needed...\n"); 
+				ch7036_redo_edid_if_needed(p_ctx, (void *)p_hedid, CHANNEL_LVDS_HDMI);
+			}
+
+	
+
+
+			if(list_item->value == CHANNEL_AUTO_DETECT) {
+				if(p_ctx->prev_outchannel != CHANNEL_LVDS_HDMI)	
+					ch7036_set_output_channel((void *)p_ctx, CHANNEL_LVDS_HDMI);
+			}
+			else { 
+
+	
+
+				
+				if(list_item->value & CHANNEL_DVI) 
+					//	map DVI to HDMI output channel;										
+					ch7036_set_output_channel((void *)p_ctx, (list_item->value & 0x01) | CHANNEL_HDMI);
+				
+				else 
+					
+					ch7036_set_output_channel((void *)p_ctx, (uint32)list_item->value );
+				
+
+			}
+	
+
+			break;
+
+		case 0x02: 
+			
+			PD_DEBUG("ch7036_alter_display_channel=> crt hpd status: attached... \n");
+		
+			if(p_ctx->hpd & 0x05) { 
+				
+				status = ch7036_read_edid(p_ctx, CHANNEL_LVDS_VGA);
+
+			
+				if (status == SS_SUCCESS) {
+
+					PD_DEBUG("ch7036_alter_display_channel=> crt only, status changed, edid is a success, proceed ..\n");
+					if( p_ctx->man_sel_out==1 ) 
+						p_ctx->hpd &= 0xF3; //need parsing,hpd manually updated to 'unchanged',dont do edid again
+					else
+						p_ctx->hpd &= 0xF7; 
+										
+				
+				}
+				else {
+
+					PD_DEBUG("ch7036_alter_display_channel=> crt only, status changed, edid failed- use default attr table- show all modes..\n");
+
+				} 
+							
+
+			} 
+			
+			else  {
+				PD_DEBUG("ch7036_alter_display_channel=> crt only, status unchange,check if edid read needed...\n"); 
+				ch7036_redo_edid_if_needed(p_ctx, (void *)p_cedid, CHANNEL_LVDS_VGA);
+			}
+				
+			if(list_item->value == CHANNEL_AUTO_DETECT)
+					ch7036_set_output_channel((void *)p_ctx, CHANNEL_LVDS_VGA);
+			else {
+
+				//set output channel, correct display choice if needed 				
+				ch7036_set_output_channel((void *)p_ctx, (uint32)list_item->value );
+			}
+			
+
+			break;
+
+		default: 
+
+			if(list_item->value == CHANNEL_AUTO_DETECT) {
+				if (!p_ctx->init_done) { 					
+					ch7036_set_output_channel((void *)p_ctx, CHANNEL_LVDS_VGA); 
+				}
+				else
+					ch7036_set_output_channel((void *)p_ctx, CHANNEL_LVDS);
+			}
+			else 		
+				ch7036_set_output_channel((void *)p_ctx, (uint32)list_item->value );
+			
+
+		break;
+
+
+	} 
+
+	PD_DEBUG("ch7036_alter_display_channel=> exit- list_item->value [0x%x]\n",list_item->value);
+	return SS_SUCCESS;
+
+}
+
+
+pd_list_entry_attr_t *ch7036_get_updated_display_ouput_entry(ch7036_device_context_t *p_ctx, uint32 channel)
+{
+		
+	pd_list_entry_attr_t  *list_item;
+
+	list_item = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_DISPLAY,
+					PD_GET_ATTR_LIST_ENTRY); 
+
+	list_item->value = (list_item->value & 0x01) | channel; 
+
+	return list_item;			
+}
+
+
+void ch7036_redo_edid_if_needed(ch7036_device_context_t *p_ctx, void* p_edidblk, uint32 channel)
+{
+	ch7036_edid_blk_t* p_edid = (ch7036_edid_blk_t *)p_edidblk;
+	ch7036_status_t status = SS_UNSUCCESSFUL;;
+	
+	
+	
+	if (!p_ctx->init_done) { 
+					
+			PD_DEBUG("ch7036_alter_list: ch7036_check_if_edid_read_needed=> attached, status unchanged at init, read edid NOW...\n"); 
+			status = ch7036_read_edid(p_ctx, channel); 
+			
+	}
+	else {
+				
+		if(!p_edid->is_edid) { 
+			PD_DEBUG("ch7036_alter_list: ch7036_check_if_edid_read_needed=>attached, status unchanged, never read edid or edid failed, read it NOW..\n");
+			status= ch7036_read_edid(p_ctx, channel);
+
+
+		}
+		else 
+			PD_DEBUG("ch7036_alter_list: ch7036_check_if_edid_read_needed=> attached, status unchanged, edid read was done, edid read is NOT needed...\n");
+	}
+
+	if(status == SS_SUCCESS) { 
+
+		if(channel & CHANNEL_HDMI) 
+			if( p_ctx->man_sel_out==1 ) 
+				p_ctx->hpd &= 0x3F; 
+			else
+				p_ctx->hpd &= 0x7F;
+		
+		else 
+			if( p_ctx->man_sel_out==1 ) 
+				p_ctx->hpd &= 0xF3; 
+			else
+				p_ctx->hpd &= 0xF7;
+		
+	} 
+
+
+
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.h
new file mode 100644
index 0000000..4917394
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_attr.h
@@ -0,0 +1,85 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_attr.h
+* @version 1.2.5
+*-----------------------------------------------------------------------------
+*/
+
+
+
+//#include <linux/kernel.h>
+
+#ifndef _CH7036_ATTR_H_
+#define _CH7036_ATTR_H_
+
+typedef struct {
+	unsigned long id;
+	char *p_name;
+	unsigned long flags; 
+
+} ch7036_attr_list_entry_t;
+
+typedef struct {
+	unsigned char num_entries;
+	unsigned long id;
+	char *p_name;
+	unsigned long default_index; 
+	unsigned long current_index;
+	ch7036_attr_list_entry_t *attr_list;
+	
+} ch7036_attr_list_header_t;
+
+
+unsigned long ch7036_enumerate_attr_table(ch7036_device_context_t *p_ctx,pd_attr_t *p_attr,ch7036_attr_list_header_t* p_list_header);
+ch7036_status_t ch7036_init_attribute_table(ch7036_device_context_t *p_ctx,ch7036_attr_list_header_t* p_list_header);
+
+
+void ch7036_set_hdmi_sync_polarity(OUTPUT_INFO* pOutput_Info);
+void ch7036_set_audio_type(INPUT_INFO* pInput_Info, uint8 id);
+void ch7036_set_scaling (OUTPUT_INFO* pOutput_Info, unsigned long id, uint8 value );
+ch7036_status_t ch7036_set_position(ch7036_device_context_t *p_ctx, uint8 attr_id, uint16 pos);
+
+
+void ch7036_set_rotate (OUTPUT_INFO* pOutput_Info);
+void ch7036_set_hflip (OUTPUT_INFO* pOutput_Info);
+void ch7036_set_vflip (OUTPUT_INFO* pOutput_Info);
+
+ch7036_status_t ch7036_set_output_channel(void* p_context, uint32 channel);
+unsigned long ch7036_get_output_channel(void* p_context);
+ch7036_status_t ch7036_alter_display_channel (ch7036_device_context_t *p_ctx);
+
+pd_list_entry_attr_t * ch7036_get_updated_display_ouput_entry(ch7036_device_context_t *p_ctx, uint32 channel);
+
+void ch7036_set_text_enhancement (ch7036_device_context_t *p_ctx, uint8 val);
+void ch7036_set_quality_enhancement(ch7036_device_context_t *p_ctx, uint8 val);
+
+void ch7036_set_pll_refdly(PREFER_INFO* pPrefer_Info);
+void ch7036_set_pll_fbdly(PREFER_INFO* pPrefer_Info);
+void ch7036_set_lvds_txdrv_ctrl(PREFER_INFO* pPrefer_Info);
+
+void ch7036_set_dither(ch7036_device_context_t* p_ctx);
+
+void ch7036_redo_edid_if_needed(ch7036_device_context_t *p_ctx, void* p_edidblk, uint32 channel);
+
+
+#endif 
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_def_regmap.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_def_regmap.h
new file mode 100755
index 0000000..d2e82fc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_def_regmap.h
@@ -0,0 +1,700 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_def_regmap.h
+* @version 1.2.2
+*-----------------------------------------------------------------------------
+*/
+
+#ifndef _CH7036_DEF_REGMAP_H
+#define _CH7036_DEF_REGMAP_H
+
+#include "ch7036_typedef.h"
+
+
+
+#define REG_PAGE_NUM		5
+#define REG_NUM_PER_PAGE	0x80
+
+static uint8 g_DefRegMap[5][0x80] = {
+	
+	{
+	0x56, 
+	0xF0, 
+	0xF3, 
+	0x00, 
+	0x36, 
+	0x58, 
+	0xAC, 
+	0xDD, 
+	0x0F, 
+	0x1F, 
+	0xB4, 
+	0x1A, 
+	0x80, 
+	0x20, 
+	0x00, 
+	0x10, 
+	0x60, 
+	0x11, 
+	0xE0, 
+	0x0D, 
+	0x00, 
+	0x0A, 
+	0x02, 
+	0x00, 
+	0x00, 
+	0xF8, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x1A, 
+	0x80, 
+	0x20, 
+	0x00, 
+	0x10, 
+	0x60, 
+	0x11, 
+	0xE0, 
+	0x0D, 
+	0x00, 
+	0x0A, 
+	0x02, 
+	0x08, 
+	0x00, 
+	0x00, 
+	0x3C, 
+	0x00, 
+	0x01, 
+	0x01, 
+	0xC0, 
+	0x01, 
+	0x01, 
+	0x80, 
+	0x40, 
+	0x40, 
+	0x47, 
+	0x88, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x86, 
+	0x00, 
+	0x11, 
+	0x0E, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x40, 
+	0x40, 
+	0x80, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x1F, 
+	0xFF, 
+	0x00, 
+	0x80, 
+	0x10, 
+	0x60, 
+	0x00, 
+	0x0A, 
+	0x02, 
+	0x08, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x40, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x01, 
+	0x2D, 
+	0x90, 
+	0x20, 
+	0x22, 
+	0x44, 
+	0x24, 
+	0x40, 
+	0x00, 
+	0x10, 
+	0x00, 
+	0xA0, 
+	0x4B, 
+	0x18, 
+	0x01, 
+	0x00, 
+	0x00, 
+	0x20, 
+	0x80, 
+	0x18, 
+	0x00, 
+	0x00, 
+	0x01, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0x0F, 
+	0x00, 
+	},
+
+	
+	{
+	0x56, 
+	0xF0, 
+	0xF3, 
+	0x01, 
+	0x36, 
+	0x58, 
+	0xAC, 
+	0x20, 
+	0x00, 
+	0x4B, 
+	0x00, 
+	0x6D, 
+	0x6A, 
+	0x51, 
+	0x93, 
+	0x1C, 
+	0x00, 
+	0x08, 
+	0xC5, 
+	0xA8, 
+	0x91, 
+	0x68, 
+	0x29, 
+	0x0E, 
+	0xC8, 
+	0x42, 
+	0x6C, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x10, 
+	0x07, 
+	0xFF, 
+	0xB6, 
+	0x10, 
+	0x00, 
+	0x00, 
+	0x15, 
+	0x18, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x0B, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x08, 
+	0x60, 
+	0x14, 
+	0x20, 
+	0x00, 
+	0x00, 
+	0x20, 
+	0x00, 
+	0x49, 
+	0x10, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x00, 
+	0x08, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xA0, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x7A, 
+	0x5E, 
+	0x6E, 
+	0x1F, 
+	0x1F, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x20, 
+	0x40, 
+	0x40, 
+	0x40, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x50, 
+	0x00, 
+	0x00, 
+	0x09, 
+	0x00, 
+	0x00, 
+	0x70, 
+	0x00, 
+	0x50, 
+	0x00, 
+	0x98, 
+	0x00, 
+	0x98, 
+	0xFF, 
+	0x00, 
+	0x00, 
+	0x00, 
+	},
+
+	
+	{
+	0x56, 
+	0xF0, 
+	0xF3, 
+	0xFF, 
+	0x36, 
+	0x58, 
+	0xAC, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	},
+
+	
+	{
+	0x56, 
+	0xF0, 
+	0xF3, 
+	0x03, 
+	0x36, 
+	0x58, 
+	0xAC, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0x00, 
+	0x03, 
+	0x19, 
+	0x40, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x11, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x09, 
+	0x1D, 
+	0x0F, 
+	0x00, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0xF8, 
+	0xFF, 
+	0x00, 
+	0x02, 
+	0xFF, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	},
+
+	
+	{
+	0x56, 
+	0xF0, 
+	0xF3, 
+	0x04, 
+	0x36, 
+	0x58, 
+	0xAC, 
+	0xFF, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xC0, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x00, 
+	0x00, 
+	0x4F, 
+	0x07, 
+	0x4F, 
+	0x07, 
+	0x3B, 
+	0x07, 
+	0x3B, 
+	0x07, 
+	0x50, 
+	0x00, 
+	0x50, 
+	0x00, 
+	0x10, 
+	0x00, 
+	0x00, 
+	0x10, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x00, 
+	0x03, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x24, 
+	0x00, 
+	0x40, 
+	0xFF, 
+	0xFF, 
+	0x7F, 
+	0x56, 
+	0x80, 
+	0x2F, 
+	0x00, 
+	0x84, 
+	0x60, 
+	0xAF, 
+	0x00, 
+	0x00, 
+	0x80, 
+	0x94, 
+	0x00, 
+	0x80, 
+	0x00, 
+	0x05, 
+	0x38, 
+	0x01, 
+	0x83, 
+	0x11, 
+	0x01, 
+	0x80, 
+	0x90, 
+	0x00, 
+	0x40, 
+	0x40, 
+	0xE4, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x00, 
+	0x89, 
+	0x10, 
+	0xAC, 
+	0x00, 
+	0xE0, 
+	0x80, 
+	0x80, 
+	0x19, 
+	0x7D, 
+	0x00, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0xFF, 
+	0x00, 
+	0x0C, 
+	},
+};
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.c
new file mode 100755
index 0000000..23490d7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.c
@@ -0,0 +1,468 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_fw.c
+* @version 1.2.5
+*-----------------------------------------------------------------------------
+*/
+
+
+
+#include "ch7036_intf.h"
+
+#include "edid7036.car"
+
+
+#define EDID_RETRY_MAX_TIMES 5
+#define HPD_LOOP_MAX 10
+
+
+#define SLEEP_TIME 200 
+
+
+#define lhfm_size  sizeof(lhfm_array)
+
+#define LHFM_TIMEOUT	10  
+
+static unsigned char edid_header[8]={0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00};
+
+static unsigned char es_map[16] = {   
+	0x26,0x27,0x42,0x43,0x44,0x45,0x46,0x47,
+	0x6A,0x51,0x52,0x53,0x57,0x58,0x59,0x5A	
+};
+
+
+
+
+
+established_timings_t et_I[8] = {
+	{0,"800x600_60", {OUT_DVI_800x600_60,OUT_HDMI_END, OUT_CRT_800x600_60} },
+	{0,"800x600_56", {OUT_DVI_800x600_56,OUT_HDMI_END, OUT_CRT_800x600_56} },
+	{0,"640x480_75", {OUT_DVI_640x480_75, OUT_HDMI_END, OUT_CRT_640x480_75} },
+	{0,"640x480_72", {OUT_DVI_640x480_72, OUT_HDMI_END, OUT_CRT_640x480_72} },
+	{0,"640x480_67", {OUT_DVI_END, OUT_HDMI_END, OUT_CRT_END } },
+	{0,"640x480_60", {OUT_DVI_640x480_60, OUT_HDMI_640x480P_60, OUT_CRT_640x480_60} },
+	{0,"720x400_88", {OUT_DVI_END, OUT_HDMI_END, OUT_CRT_END } },
+	{0,"720x400_70", {OUT_DVI_720x400_70, OUT_HDMI_END, OUT_CRT_END} },
+};
+
+established_timings_t et_II[8] ={
+	{0, "1280x1024_75", {OUT_DVI_1280x1024_75, OUT_HDMI_END, OUT_CRT_1280x1024_75} },
+	{0, "1024x768_75", {OUT_DVI_1024x768_75, OUT_HDMI_END, OUT_CRT_1024x768_75} },
+	{0, "1024x768_70", {OUT_DVI_1024x768_70, OUT_HDMI_END, OUT_CRT_1024x768_70} },
+	{0, "1024x768_60", {OUT_DVI_1024x768_60, OUT_HDMI_END, OUT_CRT_1024x768_60} },
+	{0, "1024x768_87", {OUT_DVI_END, OUT_HDMI_END, OUT_CRT_END} },
+	{0, "832x624_75", {OUT_DVI_END, OUT_HDMI_END, OUT_CRT_END} },
+	{0, "800x600_75", {OUT_DVI_800x600_75, OUT_HDMI_END, OUT_CRT_800x600_75} },
+	{0, "800x600_72", {OUT_DVI_800x600_72, OUT_HDMI_END, OUT_CRT_800x600_72} }
+};
+
+established_timings_t et_man = {
+	0, "1152x870_75", {OUT_DVI_END, OUT_HDMI_END, OUT_CRT_END} 
+};
+
+
+int LHFM_get_version(DEV_CONTEXT* p_ch7xxx_context,struct _FW7036_CFG* cfg)
+{
+
+	unsigned char reg;
+	unsigned wj;
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+		 reg = I2CRead(p_ch7xxx_context,0x4F);
+		 
+
+		 if (0==(LHFM_REQUEST & reg)) { 
+			 I2CWrite(p_ch7xxx_context,0x4F, (LHFM_REQUEST+LHFM_GET_VERSION)); 
+			 wj = 0;
+			 while (wj++< LHFM_TIMEOUT) {
+			 
+			 				
+				 I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+				 pd_usleep(SLEEP_TIME );
+				 PD_DEBUG("LHFM_get_version [%x]\r\n", wj);
+
+				 reg = I2CRead(p_ch7xxx_context,0x4F);
+				 if(reg == LHFM_GET_VERSION) {
+					 reg = I2CRead(p_ch7xxx_context,0x50);			
+					 if (!(LHFM_RET_ERROR & reg)) {
+						    I2CWrite(p_ch7xxx_context,0x03, 0x01); 
+						    cfg->size = I2CRead(p_ch7xxx_context,es_map[0]);	 
+							cfg->ver_major = I2CRead(p_ch7xxx_context,es_map[1]);
+							cfg->ver_minor = I2CRead(p_ch7xxx_context,es_map[2]);
+							cfg->did = I2CRead(p_ch7xxx_context,es_map[3]);
+							cfg->rid = I2CRead(p_ch7xxx_context,es_map[4]);
+							cfg->capbility = I2CRead(p_ch7xxx_context,es_map[5]);
+							cfg->reserved = I2CRead(p_ch7xxx_context,es_map[6]);
+						    I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+							return 0;
+					 } else return -2;
+
+				 } 
+				 				 				
+			 }  
+
+		 } 
+
+	return -1; 
+}
+
+
+void LHFM_enable_crt_hpd(DEV_CONTEXT* p_ch7xxx_context)
+{
+	uint8 reg;
+	
+	PD_DEBUG("ch7036: LHFM_enable_crt_hpd-enter\n");
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x04); 
+	reg = I2CRead(p_ch7xxx_context,0x57);
+	I2CWrite(p_ch7xxx_context,0x57, reg | 0x02); 
+	   
+	pd_usleep(SLEEP_TIME);
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+	I2CWrite(p_ch7xxx_context,0x08, I2CRead(p_ch7xxx_context,0x08) & 0xF1);
+	
+	pd_usleep(SLEEP_TIME);
+}
+
+
+ch7036_status_t LHFM_get_crt_hpd(DEV_CONTEXT* p_ch7xxx_context)
+{
+
+	unsigned char reg, count =0;
+	ch7036_status_t status = SS_CRT_HPD_NOTCONNECTED;
+
+	do {
+		
+		I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+		reg = I2CRead(p_ch7xxx_context,0x74); 
+		I2CWrite(p_ch7xxx_context,0x74, reg & 0xFD); 
+		pd_usleep(SLEEP_TIME);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+		reg = I2CRead(p_ch7xxx_context,0x74);
+		I2CWrite(p_ch7xxx_context,0x74, reg | 0x02); 		
+		pd_usleep(SLEEP_TIME);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01); 
+		reg = I2CRead(p_ch7xxx_context,0x7C); 		
+		
+		if( (reg & 0xF0) == 0x50  || (reg & 0xF4) == 0x14 || (reg & 0xF4) == 0x44) 
+			status= SS_SUCCESS;
+					      	   	    
+	} while ( (status == SS_CRT_HPD_NOTCONNECTED) && ( (++count) < HPD_LOOP_MAX ) );
+
+          
+	I2CWrite(p_ch7xxx_context,0x03, 0x00); 	        
+	reg = I2CRead(p_ch7xxx_context,0x74);	        
+	I2CWrite(p_ch7xxx_context,0x74, reg & 0xFD);	 
+
+	I2CWrite(p_ch7xxx_context,0x08, I2CRead(p_ch7xxx_context,0x08) | 0x0E);
+		
+	return status;
+
+}
+
+
+ch7036_status_t LHFM_get_hdmi_modeinfo(DEV_CONTEXT* p_ch7xxx_context,unsigned char *minfo)
+{
+	unsigned char reg;
+	unsigned wj;
+	unsigned i;
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+		 reg = I2CRead(p_ch7xxx_context,0x4F);
+		 
+		 if (0==(LHFM_REQUEST & reg)) {
+			 I2CWrite(p_ch7xxx_context,0x4F, (LHFM_REQUEST+ LHFM_GET_MODEINFO)); 
+			 wj = 0;
+			 while (wj++< LHFM_TIMEOUT) {
+				 I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+				 pd_usleep(SLEEP_TIME);
+
+				 reg = I2CRead(p_ch7xxx_context,0x4F);
+				 if(reg == LHFM_GET_MODEINFO) {					 
+					 reg = I2CRead(p_ch7xxx_context,0x50);			
+					 if (!(LHFM_RET_ERROR & reg)) {
+						 if (minfo) {
+						    I2CWrite(p_ch7xxx_context,0x03, 0x01); 
+							for (i=0; i<15; i++) minfo[i] = I2CRead(p_ch7xxx_context,es_map[i]);	 
+						}
+						return SS_SUCCESS;
+					 } else return SS_FIRMWARE_ERR;
+
+				 } 
+				 			
+			 } 
+
+		 } 
+
+	return SS_FIRMWARE_TIMEOUT;
+
+}
+
+
+ch7036_status_t LHFM_get_edid(DEV_CONTEXT* p_ch7xxx_context,unsigned char* edid, unsigned char* ebn, unsigned char flag)
+{
+
+	unsigned i,j, ie;
+	unsigned k1, k2;
+	unsigned char reg;
+	unsigned wj;
+
+	ch7036_status_t status;
+
+
+
+	status = SS_SUCCESS;
+	*ebn = 0;    
+
+	pd_memset(edid, 0, EDID_SIZE); 
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+	reg = I2CRead(p_ch7xxx_context,0x4F);
+
+	if( (reg & LHFM_REQUEST) != 0 )
+		return SS_FIRMWARE_ERR; //mcu is busy
+	else { //mcu is ready to serve host request
+       
+		for(i=0,j=0; i < 8; i++,j+=16) {
+
+		    I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+			I2CWrite(p_ch7xxx_context,0x50, i | flag); 
+			I2CWrite(p_ch7xxx_context,0x4F, (LHFM_REQUEST+LHFM_GET_EDID)); 
+			wj = 0;
+
+			status = SS_FIRMWARE_TIMEOUT;
+			PD_DEBUG("ch7036: LHFM_get_edid- block [%d] - getting [%d]- 16 bytes - wj=[0x%x]\n",*ebn,i+1,wj);
+			while (wj++< (2*LHFM_TIMEOUT) ) { 
+				 I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+				 pd_usleep(SLEEP_TIME);
+				 
+				 reg = I2CRead(p_ch7xxx_context,0x4F);
+				 
+				 if(reg == LHFM_GET_EDID) { 
+					 
+					 reg = I2CRead(p_ch7xxx_context,0x50);	
+					 
+					 if (!(LHFM_RET_ERROR & reg)) { 
+							I2CWrite(p_ch7xxx_context,0x03, 0x01); 
+							for (ie=0; ie<16; ie++) edid[j+ie] = I2CRead(p_ch7xxx_context,es_map[ie] );								
+							break; 
+					 }
+					 else 						
+							return SS_FIRMWARE_ERR; 															 
+
+				 } 
+				 
+				 				 
+			} //while block
+			
+			status = SS_SUCCESS; 
+
+		} //for
+
+		for(i=0;i<8;i++)
+			if (edid_header[i] != edid[i]) {
+				return SS_FIRMWARE_ERR;
+			}
+		
+		(*ebn)++;
+		
+		if(flag & 0x80 && (edid[0x7E] > 0) ) { 
+			PD_DEBUG("ch7036: LHFM_get_edid- incorrect # of VGA EDID blocks read [%x]\n",edid[0x7E]); 
+			return SS_FIRMWARE_ERR;
+		}
+		
+		k2 = edid[0x7E];
+
+		k2 = (k2 > 3)? 3 : k2;  
+		
+		if (k2>0) { //case: there are more than 1 blocks
+			for (k1=1; k1<=k2; k1++) {
+
+				for(i=k1*8; i < 8+k1*8; i++,j+=16){
+					I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+					I2CWrite(p_ch7xxx_context,0x50, i + flag);  
+					I2CWrite(p_ch7xxx_context,0x4F, (LHFM_REQUEST+LHFM_GET_EDID)); 
+					wj = 0;
+				
+					status = SS_FIRMWARE_TIMEOUT;
+					PD_DEBUG("ch7036: LHFM_get_edid- block [%d] - getting [%d]- 16 bytes - wj=[0x%x]\n",k1,i+1,wj);
+					while (wj++< (2*LHFM_TIMEOUT)) {
+						I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+						pd_usleep(SLEEP_TIME);
+					 
+						reg = I2CRead(p_ch7xxx_context,0x4F);
+					 
+						if(reg == LHFM_GET_EDID) {						 
+							reg = I2CRead(p_ch7xxx_context,0x50);			
+							if (!(LHFM_RET_ERROR & reg)) {
+								I2CWrite(p_ch7xxx_context,0x03, 0x01); 
+								for (ie=0; ie<16; ie++) edid[j+ie] = I2CRead(p_ch7xxx_context,es_map[ie] );			
+								break;
+							}
+							else 
+								return SS_FIRMWARE_ERR;
+	
+
+						} 
+					 
+					} //while
+
+					status = SS_SUCCESS;
+
+				}//nested for 
+
+				(*ebn)++;
+
+			} //outermost for
+
+
+		} //if- case: there are more than 1 blocks
+
+	} //else - case:  mcu is ready to serve host request
+	
+	return status; 
+				
+}
+
+
+int LHFM_load_firmware(DEV_CONTEXT* p_ch7xxx_context)
+{
+
+
+	unsigned fs1;
+	unsigned char ch;
+
+	PD_DEBUG("ch7036: LHFM_load_firmware- firmware size [0x%.8X]\n",lhfm_size);
+	
+	I2CWrite(p_ch7xxx_context,0x03, 0x04);
+	ch = 0x29 | I2CRead(p_ch7xxx_context,0x52);
+	I2CWrite(p_ch7xxx_context,0x52, ch & 0xFB); 
+   
+
+	I2CWrite(p_ch7xxx_context,0x5B, 0x9E); 
+	I2CWrite(p_ch7xxx_context,0x5B, 0xB3);
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x04);
+	I2CWrite(p_ch7xxx_context,0x03, 0x07);
+
+	for (fs1=0; fs1<lhfm_size; fs1++) 
+		I2CWrite(p_ch7xxx_context, 0x07, lhfm_array[fs1]);  
+
+
+
+	I2CWrite(p_ch7xxx_context, 0x03, 0x03); 
+	ch = I2CRead(p_ch7xxx_context,0x74); 
+	if (ch & 0x1) { 
+		
+		return -1;
+	}
+	else {
+		
+		I2CWrite( p_ch7xxx_context,0x03, 0x04); 
+		ch = I2CRead(p_ch7xxx_context,0x52);
+		I2CWrite(p_ch7xxx_context, 0x52, ch | 0x24);  
+	}
+
+	I2CWrite(p_ch7xxx_context, 0x03, 0x00);
+
+	return 0;
+}
+#if 0
+void ch7036_dump( char *s, int size, unsigned char *regdata)
+{
+	char temp[18];
+    int ch;
+
+    int i,j, size1;
+    
+    temp[16] = '?'; 
+    temp[17] = '\n';
+
+	PD_DEBUG("\n");
+    PD_DEBUG("+--------------------------------------------------------------------+\n");
+    PD_DEBUG("|%s |\n",s);
+    PD_DEBUG("+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-----------------+\n");
+    PD_DEBUG("|  | 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| A| B| C| D| E| F|                 |\n");
+    PD_DEBUG("++++--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-----------------+\n");
+    
+    size1 = size % 16; 
+    size = size/16; 
+
+    for (i=0; i<size; i++) {
+
+       
+       PD_DEBUG("|%02x>", 0xFF & i*16);
+
+       
+	   for (j=0; j<16; j++) {
+           ch = regdata[i*16+j];
+           PD_DEBUG("%02X|", ch);
+
+#if 0
+		   
+          
+		   if( (ch >= 0x30 && ch <= 0x39) || (ch >= 0x61 && ch <= 0x7A) || (ch >= 0x41 && ch <= 0x5C) ) 
+				temp[j]='.';
+		   else
+			   temp[j]=(char)ch; 
+#endif
+		   
+        }
+             
+	   PD_DEBUG("\n");
+	}
+
+	if (size1) {
+	
+        PD_DEBUG( "|%02x>", 0xFF & size*16);
+
+		for (j=0; j<size1; j++) {
+           ch = regdata[size*16+j];
+           PD_DEBUG("%02X|", ch);
+#if 0
+         
+			if( (ch >= 0x30 && ch <= 0x39) || (ch >= 0x61 && ch <= 0x7A) || (ch >= 0x41 && ch <= 0x5C) ) 
+				temp[j]='.';
+		   else
+			   temp[j]=(char)ch; 
+#endif
+
+        }
+		for (j=size1; j<16; j++) {
+     
+           PD_DEBUG("   ");
+      
+        }
+    
+	}
+   
+	PD_DEBUG("\n");
+
+    return;
+}
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.h
new file mode 100755
index 0000000..cc4a321
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_fw.h
@@ -0,0 +1,213 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_fw.h
+* @version 1.2.2
+*-----------------------------------------------------------------------------
+*/
+
+
+#ifndef H_FW_OPER_H
+#define H_FW_OPER_H
+
+#include "ch7036_typedef.h"
+
+
+//#include <linux/kernel.h>
+
+#define	CFG_CAP_EDID	0x01
+#define CFG_CAP_HDCP	0x02
+
+typedef unsigned char ch7036_attr_table_index_t;
+
+
+typedef struct _FW7036_CFG {
+    unsigned char size;	 
+	unsigned char ver_major;
+	unsigned char ver_minor;
+	unsigned char did;
+	unsigned char rid;
+	unsigned char capbility;
+	unsigned char reserved;
+} FW7036_CFG;
+
+
+#define LHFM_GET_VERSION		0x1F
+#define LHFM_GET_HPD			0x06
+#define LHFM_GET_EDID			0x01
+#define LHFM_GET_MODEINFO		0x03
+#define LHFM_GET_VGA_MODEINFO	0x33
+#define LHFM_HDMI_ONOFF		0x04 
+#define LHFM_REQUEST		0x40
+#define LHFM_RET_ERROR		0x80
+
+int LHFM_get_version(DEV_CONTEXT* p_ch7xxx_context,struct _FW7036_CFG* cfg);
+
+void LHFM_enable_crt_hpd(DEV_CONTEXT* p_ch7xxx_context);
+ch7036_status_t LHFM_get_crt_hpd(DEV_CONTEXT* p_ch7xxx_context);
+int LHFM_hdmi_onoff(int turnon);
+ch7036_status_t LHFM_get_hdmi_modeinfo(DEV_CONTEXT* p_ch7xxx_context,unsigned char *minfo);
+ch7036_status_t LHFM_get_edid(DEV_CONTEXT* p_ch7xxx_context,unsigned char*edid, unsigned char* ebn, unsigned char flag);
+int LHFM_load_firmware(DEV_CONTEXT* pDevContext);
+void ch7036_dump( char *s, int size, unsigned char *regdata);
+
+#define GET_VER					0x01
+#define GET_HPD					0x02
+#define GET_EDID				0x04
+#define GET_HDMI_DISPLAY_MODES	0x08
+
+#define GET_CRT_HPD				0x10
+#define GET_CRT_EDID			0x20
+
+#define CH7036_HDMI_DDC			0
+#define CH7036_VGA_DDC			0x80
+
+#define MAX_EDID_BLOCKS			2
+
+#define MAX_I2C_BLOCK_SIZE		32
+
+
+
+
+#define NUM_OF_BLOCKS                         0x0F  
+#define DTD_SIZE                              0x12  
+#define EDID_SIZE                             0x0100
+#define EDID_STRUCTURE_VERSION                0x12
+#define EDID_STRUCTURE_REVISION               0x13
+#define EDID_FEATURE_SUPPORT                  0x18
+#define EDID_ESTABLISHED_TIMINGS_1            0x23  
+#define EDID_ESTABLISHED_TIMINGS_2            0x24  
+#define EDID_MANUFACTURERS_RESERVED_TIMINGS   0x25  
+#define EDID_STANDARD_TIMINGS                 0x26  
+#define EDID_STANDARD_TIMINGS_BIT_DESCRIPTION 0x27  
+#define EDID_DETAILED_TIMING_DESCRIPTION_1    0x36  
+#define EDID_DETAILED_TIMING_DESCRIPTION_2    0x48  
+#define EDID_DETAILED_TIMING_DESCRIPTION_3    0x5A  
+
+#define EDID_EXTENSION_FLAG                   0x7E  
+
+#define EDID_CEA_TAG                          0x80  
+#define EDID_CEA_REVISION                     0x81  
+#define EDID_CEA_DETAILED_TIMING_DATA_OFFSET  0x82  
+#define EDID_CEA_VIDEO_DATA_BLOCK             0x84  
+#define EDID_CEA_DATA_BLOCK					  0x84
+
+#define EDID_CEA_VIDEO_DATA_BLOCK_2           0x96  
+#define EDID_CEA_VIDEO_DATA_BLOCK_3           0xA8  
+#define EDID_CEA_VIDEO_DATA_BLOCK_4           0xBA  
+#define EDID_CEA_VIDEO_DATA_BLOCK_5           0xCC  
+#define EDID_CEA_VIDEO_DATA_BLOCK_6           0xDE  
+
+#define EDID_SPACE_INDEX_START                0x18  
+
+
+
+
+
+typedef struct {
+    unsigned char  is_supported;
+    char mode_name[13];
+	unsigned long index[3]; 
+
+}established_timings_t, *p_established_timings;
+
+typedef struct {
+	unsigned int ha; 
+	unsigned int va; 
+	unsigned char refresh_rate;
+}standard_timings_t, *p_standard_timings;
+
+
+
+typedef struct {
+	unsigned char pclk_lobyte;
+	unsigned char pclk_hibyte;
+	unsigned char hactive_lobyte;
+	unsigned char hblank_lobyte;
+	unsigned char hactive_blank;			
+	unsigned char vactive_lobyte;
+	unsigned char vblank_lobyte;
+	unsigned char vactive_blank;			
+	unsigned char hsync_off_lobyte;			
+	unsigned char hsync_pulse_width;         
+	unsigned char vsync_off_pulse_width;	
+											
+	unsigned char hsopw_vsopw;				
+											
+											 
+											
+	unsigned char himage_size_lobyte;		
+	unsigned char vimage_size_lobyte;       
+	unsigned char hvimage_size;				
+											
+	unsigned char hborder;
+	unsigned char vborder; 
+	unsigned char flags;
+}dtd_t, *p_dtd;
+
+
+typedef struct monitor_range {
+
+	unsigned char  hrate_min;           
+	unsigned char  hrate_max;            
+	unsigned char  vrate_min;           
+	unsigned char  vrate_max;            
+
+	unsigned long  pclk_max;           
+
+}monitor_range_t;
+
+
+typedef struct detailed_timing_block {
+	unsigned long data_tag; 
+	OUT_FMT		dtiming; 
+} dtblks_t;
+
+
+typedef struct edid_blk {
+	
+
+	unsigned char						is_edid; 
+	unsigned char						is_preferred_timing;  
+
+	unsigned char						ebn; 
+	unsigned char						edidblk[EDID_SIZE]; 
+
+	established_timings_t				*etiming_I;
+	established_timings_t				*etiming_II;
+	established_timings_t				*etiming_man;
+
+	standard_timings_t					stiming[8];
+
+	standard_timings_t					stiming_x[6]; 
+	monitor_range_t						rtiming; 
+
+	dtblks_t							dtblk[4];
+	
+	
+	unsigned char						supported_modes[15]; 
+	ch7036_attr_table_index_t			modes[MAX_ATTR_LIST_SIZE]; 
+
+} ch7036_edid_blk_t;
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.c
new file mode 100755
index 0000000..5a842ab
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.c
@@ -0,0 +1,296 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_iic.c
+* @version 1.2.4
+*-----------------------------------------------------------------------------
+*/
+
+
+
+
+
+#include "ch7036_iic.h"
+#include "ch7036_def_regmap.h"
+
+
+static uint8 g_nCurPage = 0;
+static uint8 g_nCurRegmap[REG_PAGE_NUM][REG_NUM_PER_PAGE];
+
+int g_verbose = 0; 
+
+void DeviceReset(DEV_CONTEXT* pDevContext)
+{
+	
+	uint8 reg=0x00;
+
+   I2CWrite(pDevContext,0x03, 0x04);
+   
+   I2CWrite(pDevContext,0x52, 0x2B); 
+   I2CWrite(pDevContext,0x5a, 0x06);
+   I2CWrite(pDevContext,0x5a, 0x04);
+   I2CWrite(pDevContext,0x5a, 0x06);
+
+   I2CWrite(pDevContext,0x52, 0x29);
+   I2CWrite(pDevContext,0x52, 0x2b);
+
+   I2CWrite(pDevContext,0x5a, 0x04);
+
+  
+   I2CWrite(pDevContext,0x03, 0x01); 
+   reg = I2CRead(pDevContext,0x16);
+
+   reg = reg & 0xF7;
+   I2CWrite(pDevContext,0x16, reg);
+   
+
+
+}
+
+
+void DeviceSetup(DEV_CONTEXT* pDevContext)
+{
+	if (g_verbose) {
+			uint8 page, index;
+			uint16 k =0;       
+			uint16 rc[5];
+			for(page=0; page<REG_PAGE_NUM; ++page)
+			{
+				rc[page]=0;
+				if(page == 0x02) continue; 
+				I2CWrite(pDevContext,0x03, page); 
+				for(index=0x07; index<REG_NUM_PER_PAGE; ++index)
+				{
+					if(g_nCurRegmap[page][index] != g_DefRegMap[page][index])
+					{   
+			
+						k++;  
+						rc[page]++;
+						I2CWrite(pDevContext,index, g_nCurRegmap[page][index]);
+						
+					}
+				}
+			}
+	
+			for(page=0; page<REG_PAGE_NUM; ++page) 
+				
+				;
+
+	}
+	else {
+			uint8 page, index;
+			for(page=0; page<REG_PAGE_NUM; ++page)
+			{
+				if(page == 0x02) continue; 
+				I2CWrite(pDevContext,0x03, page);
+				for(index=0x07; index<REG_NUM_PER_PAGE; ++index)
+				{
+					if(g_nCurRegmap[page][index] != g_DefRegMap[page][index])
+					{
+						I2CWrite(pDevContext,index, g_nCurRegmap[page][index]);
+					}
+				}
+			}
+	}
+}
+
+
+uint8 iic_read(uint8 index)
+{
+	return g_nCurRegmap[g_nCurPage][index];
+}
+
+void iic_write(uint8 index, uint8 value)
+{
+	if(index == 0x03)
+	{
+		g_nCurPage = value;
+	}
+	else
+	{
+		g_nCurRegmap[g_nCurPage][index] = value;
+	}
+}
+
+void iic_reset()
+{
+	uint8 page, index;
+	for(page=0; page<REG_PAGE_NUM; ++page)
+	{
+		for(index=0; index<REG_NUM_PER_PAGE; ++index)
+		{
+			g_nCurRegmap[page][index] = g_DefRegMap[page][index];
+		}
+	}
+	g_nCurPage = 0x00; 
+}
+
+uint32 iic_read_ex(MULTI_REG_ID reg_id)
+{
+	uint8 oldpage;
+	uint32 n0, n1, n2, n3, mask, i, value;
+
+	
+	n0 = n1 = n2 = n3 = value = mask = 0;
+
+	
+	i = (uint32)reg_id;
+
+	
+	oldpage = iic_read(0x03);
+	iic_write(0x03, g_MultiRegTable[i].PageIndex);
+
+	if(NOOP_INDEX != g_MultiRegTable[i].TopRegIndex)
+		n0 = iic_read(g_MultiRegTable[i].TopRegIndex);
+
+	if(NOOP_INDEX != g_MultiRegTable[i].HighRegIndex) 
+		n1 = iic_read(g_MultiRegTable[i].HighRegIndex);
+
+	if(NOOP_INDEX != g_MultiRegTable[i].MiddleRegIndex)
+		n2 = iic_read(g_MultiRegTable[i].MiddleRegIndex);
+
+	n3 = iic_read(g_MultiRegTable[i].LowRegIndex);
+
+	iic_write(0x03, oldpage);
+
+	if(0 != n0)
+	{
+		mask = 1;
+		mask <<= g_MultiRegTable[i].TopEndBit - g_MultiRegTable[i].TopStartBit + 1;
+		mask -= 1;
+		mask <<= g_MultiRegTable[i].TopStartBit;
+		n0 &= mask;
+		n0 >>= g_MultiRegTable[i].TopStartBit;
+		n0 <<= (g_MultiRegTable[i].HighEndBit - g_MultiRegTable[i].HighStartBit + 1)+(g_MultiRegTable[i].MiddleEndBit - g_MultiRegTable[i].MiddleStartBit + 1) + (g_MultiRegTable[i].LowEndBit - g_MultiRegTable[i].LowStartBit + 1);
+	}
+	if(0 != n1)
+	{
+		mask = 1;
+		mask <<= g_MultiRegTable[i].HighEndBit - g_MultiRegTable[i].HighStartBit + 1;
+		mask -= 1;
+		mask <<= g_MultiRegTable[i].HighStartBit;
+		n1 &= mask;
+		n1 >>= g_MultiRegTable[i].HighStartBit;
+		n1 <<= (g_MultiRegTable[i].MiddleEndBit - g_MultiRegTable[i].MiddleStartBit + 1) + (g_MultiRegTable[i].LowEndBit - g_MultiRegTable[i].LowStartBit + 1);
+	}
+
+	if(0 != n2)
+	{
+		mask = 1;
+		mask <<= g_MultiRegTable[i].MiddleEndBit - g_MultiRegTable[i].MiddleStartBit + 1;
+		mask -= 1;
+		mask <<= g_MultiRegTable[i].MiddleStartBit;
+		n2 &= mask;
+		n2 >>= g_MultiRegTable[i].MiddleStartBit;	
+		n2 <<= g_MultiRegTable[i].LowEndBit - g_MultiRegTable[i].LowStartBit + 1;
+	}
+
+	if(0 != n3)
+	{
+		mask = 1;
+		mask <<= g_MultiRegTable[i].LowEndBit - g_MultiRegTable[i].LowStartBit + 1;
+		mask -= 1;
+		mask <<= g_MultiRegTable[i].LowStartBit;
+		n3 &= mask;
+		n3 >>= g_MultiRegTable[i].LowStartBit;
+	}
+
+	value = n0 | n1 | n2 | n3;
+	return value;
+}
+
+void iic_write_ex(MULTI_REG_ID reg_id, uint32 value)
+{
+	uint8 oldpage;
+	uint32 n1, n2, n3, n4, mask, nOrigin, i, value_local;
+
+	
+	n1 = n2 = n3 = n4 = mask = nOrigin = i = 0;
+	value_local = value;
+
+	
+	i = (uint32)reg_id;
+
+	
+	oldpage = iic_read(0x03);
+	iic_write(0x03, g_MultiRegTable[i].PageIndex);
+
+	mask = 1;
+	mask <<= g_MultiRegTable[i].LowEndBit - g_MultiRegTable[i].LowStartBit + 1;
+	mask -= 1;
+	n1 = value_local & mask;
+	n1 <<= g_MultiRegTable[i].LowStartBit;
+	nOrigin = iic_read(g_MultiRegTable[i].LowRegIndex);
+	mask <<= g_MultiRegTable[i].LowStartBit;
+	nOrigin &= ~mask;
+	n1 |= nOrigin;
+	iic_write(g_MultiRegTable[i].LowRegIndex, (uint8)n1);
+	value_local >>= g_MultiRegTable[i].LowEndBit - g_MultiRegTable[i].LowStartBit + 1;
+
+	if(NOOP_INDEX != g_MultiRegTable[i].MiddleRegIndex)
+	{
+		mask = 1;
+		mask <<= g_MultiRegTable[i].MiddleEndBit - g_MultiRegTable[i].MiddleStartBit + 1;
+		mask -= 1;
+		n2 = value_local & mask;
+		n2 <<= g_MultiRegTable[i].MiddleStartBit;
+		nOrigin = iic_read(g_MultiRegTable[i].MiddleRegIndex);
+		mask <<= g_MultiRegTable[i].MiddleStartBit;
+		nOrigin &= ~mask;
+		n2 |= nOrigin;
+		iic_write(g_MultiRegTable[i].MiddleRegIndex, (uint8)n2);
+		value_local >>= g_MultiRegTable[i].MiddleEndBit - g_MultiRegTable[i].MiddleStartBit + 1;
+
+		if(NOOP_INDEX != g_MultiRegTable[i].HighRegIndex)
+		{
+			mask = 1;
+			mask <<= g_MultiRegTable[i].HighEndBit - g_MultiRegTable[i].HighStartBit + 1;
+			mask -= 1;
+			n3 = value_local & mask;
+			n3 <<= g_MultiRegTable[i].HighStartBit; 
+			nOrigin = iic_read(g_MultiRegTable[i].HighRegIndex);
+			mask <<= g_MultiRegTable[i].HighStartBit;
+			nOrigin &= ~mask;
+			n3 |= nOrigin;
+			iic_write(g_MultiRegTable[i].HighRegIndex, (uint8)n3);
+			value_local >>= g_MultiRegTable[i].HighEndBit - g_MultiRegTable[i].HighStartBit + 1;
+
+			if(NOOP_INDEX != g_MultiRegTable[i].TopRegIndex)
+			{
+				mask = 1;
+				mask <<= g_MultiRegTable[i].TopEndBit - g_MultiRegTable[i].TopStartBit +1;
+				mask -= 1;
+				n4 = value_local & mask;
+				n4 <<= g_MultiRegTable[i].TopStartBit;
+				nOrigin = iic_read(g_MultiRegTable[i].TopRegIndex);
+				mask <<= g_MultiRegTable[i].TopStartBit;
+				nOrigin &= ~mask;
+				n4 |= nOrigin;
+				iic_write(g_MultiRegTable[i].TopRegIndex,(uint8)n4);
+			}
+		}		
+	}
+
+	iic_write(0x03, oldpage);
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.h
new file mode 100755
index 0000000..593aee8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_iic.h
@@ -0,0 +1,52 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_iic.h
+* @version 1.2.2
+*-----------------------------------------------------------------------------
+*/
+
+#ifndef _CH7036_IIC_H
+#define _CH7036_IIC_H
+
+#include "ch7036_typedef.h"
+#include "ch7036_reg_table.h"
+
+
+
+
+uint8 I2CRead(DEV_CONTEXT* pDevContext, uint8 index);
+void I2CWrite(DEV_CONTEXT* pDevContext, uint8 index, uint8 value);
+
+void I2CBlockWrite(DEV_CONTEXT* pDevContext,uint8 index, uint8* value, uint16 len);
+void DeviceReset(DEV_CONTEXT* pDevContext);
+void DeviceSetup(DEV_CONTEXT* pDevContext);
+
+uint8 iic_read(uint8 index);
+void iic_write(uint8 index, uint8 value);
+void iic_reset(void);
+uint32 iic_read_ex(MULTI_REG_ID reg_id);
+void iic_write_ex(MULTI_REG_ID reg_id, uint32 reg_val);
+
+extern int g_verbose;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.c
new file mode 100644
index 0000000..bc7b8a9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.c
@@ -0,0 +1,1994 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_intf.c
+* @version 1.2.5
+*-----------------------------------------------------------------------------
+*/
+
+
+#include "ch7036_intf.h"
+#include "ch7036_attr.h"
+
+
+#ifdef T_LINUX
+	#include "lvds/lvds.h"
+#else
+	#include "lvds.h"
+#endif
+
+
+ch7036_status_t ch7036_get_hdmi_hpd(ch7036_device_context_t* p_ctx, uint8 *hpd);
+
+unsigned long ch7036_invis_6x4_modes_table_size[3] = {2,3,5}; 
+unsigned long ch7036_invis_8x6_7x4_table_size[3] = {3,5,6};
+
+OUT_FMT hdmi_timing_table[OUT_HDMI_END] = {
+	
+		                    
+	{1,     25175,  1, {800,   640, 16,  96, 525,  480, 10,  2, 59, SCANTYPE_PROGRESSIVE} }, 
+    {1,     25250,  1, {800,   640, 16,  96, 525,  480, 10,  2, 60, SCANTYPE_PROGRESSIVE} },
+    {2,     27000,  1, {858,   720, 16,  62, 525,  480,  9,  6, 59, SCANTYPE_PROGRESSIVE} },
+    {2,     27027,  1, {858,   720, 16,  62, 525,  480,  9,  6, 60, SCANTYPE_PROGRESSIVE} },
+	{17,     27000,  1, {864,  720,  12,  64, 625,  576,  5,  5, 50, SCANTYPE_PROGRESSIVE} },
+
+    {4,     74176,  2, {1650, 1280, 110, 40, 750,  720,  5,  5, 59, SCANTYPE_PROGRESSIVE} },
+    {4,     74250,  2, {1650, 1280, 110, 40, 750,  720,  5,  5, 60, SCANTYPE_PROGRESSIVE} },
+    {5,     74176,  2, {2200, 1920, 88,  44, 1125, 1080, 2,  5, 59, SCANTYPE_INTERLACED} },
+    {5,     74250,  2, {2200, 1920, 88,  44, 1125, 1080, 2,  5, 60, SCANTYPE_INTERLACED} },
+	{16,    148350,  2, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 59, SCANTYPE_PROGRESSIVE} },
+//   {16,    148352,  2, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 59, SCANTYPE_PROGRESSIVE} }, 
+   {16,    148500,  2, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 60, SCANTYPE_PROGRESSIVE} }, 
+ 
+//   {17,     27000,  1, {864,  720,  12,  64, 625,  576,  5,  5, 50, SCANTYPE_PROGRESSIVE} }, 
+   {19,     74250,  2, {1980, 1280, 440, 40, 750,  720,  5,  5, 50, SCANTYPE_PROGRESSIVE} }, 
+   
+   {20,     74250,  2, {2640, 1920, 528, 44, 1125, 1080, 2, 5, 50, SCANTYPE_INTERLACED} },
+
+
+   {31,    148500,  2, {2640, 1920, 528, 44, 1125, 1080, 4,  5, 50, SCANTYPE_PROGRESSIVE} },
+   {32,     74175,  2, {2750, 1920, 638, 44, 1125, 1080, 4,  5, 23, SCANTYPE_PROGRESSIVE} },
+   {32,     74250,  2, {2750, 1920, 638, 44, 1125, 1080, 4,  5, 24, SCANTYPE_PROGRESSIVE} },
+   {33,     74250,  2, {2640, 1920, 528, 44, 1125, 1080, 4,  5, 25, SCANTYPE_PROGRESSIVE} },
+   {34,     74175,  2, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 29, SCANTYPE_PROGRESSIVE} },
+   {34,     74250,  2, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 30, SCANTYPE_PROGRESSIVE} }, 
+
+   {40,    148500,  2, {2640, 1920, 528, 44, 1124, 1080, 4, 10, 100, SCANTYPE_INTERLACED} },
+   {41,    148500,  2, {1980, 1280, 440, 40, 750,  720,  5,  5, 100, SCANTYPE_PROGRESSIVE} },
+   {42,     54000,  1, {864,   720, 12,  64, 625,  576,  5,  5, 100, SCANTYPE_PROGRESSIVE} },
+
+
+   {46,    148352,  2, {2200, 1920, 88,  44, 1124, 1080, 4, 10, 119, SCANTYPE_INTERLACED} },
+   {46,    148500,  2, {2200, 1920, 88,  44, 1124, 1080, 4, 10, 120, SCANTYPE_INTERLACED} },
+
+   {47,    148352,  2, {1650, 1280, 110, 40, 750,  720,  5,  5, 119, SCANTYPE_PROGRESSIVE} },
+   {47,    148500,  2, {1650, 1280, 110, 40, 750,  720,  5,  5, 120, SCANTYPE_PROGRESSIVE} },
+   
+   {48,     54000,  1, {858,   720, 16,  62, 525,  480,  9,  6, 119, SCANTYPE_PROGRESSIVE} },
+   {48,     54054,  1, {858,   720, 16,  62, 525,  480,  9,  6, 120, SCANTYPE_PROGRESSIVE} }, 
+  
+   {52,    108000,  1, {864,   720, 12,  64, 625,  576,  5,  5, 200, SCANTYPE_PROGRESSIVE} },
+
+   {56,    108000,  1, {858,   720, 16,  62, 525,  480,  9,  6, 239, SCANTYPE_PROGRESSIVE} },
+   {56,    108108,  1, {858,   720, 16,  62, 525,  480,  9,  6, 240, SCANTYPE_PROGRESSIVE} }, 
+  
+};
+
+OUT_FMT dvi_timing_table[OUT_DVI_END] = {
+	
+
+
+	{0,     25170,  0, { 800,  640, 16,  96, 525,  480, 10,  2,  60, SCANTYPE_PROGRESSIVE}	}, 
+	{0,     31500,  0, { 832,  640, 24,  40, 520,  480,  9,  3,  72, SCANTYPE_PROGRESSIVE}	},  
+    {0,     31500,  0, { 800,  640, 16,  96, 525,  480, 11,  2,  75, SCANTYPE_PROGRESSIVE}	},  
+
+	{0,     28322,  0, { 900,  720, 15, 108, 449,  400, 11,  2,  70, SCANTYPE_PROGRESSIVE}	},  
+	
+	{0,		38100,	0, {1088,  800,	32, 128, 619,  600,  1,  4,  56, SCANTYPE_PROGRESSIVE}	},
+	
+
+	{0,		40000,	0, {1056,  800,	40, 128, 628,  600,  1,  4,  60, SCANTYPE_PROGRESSIVE}	},
+	{0,		50000,	0, {1040,  800,	56, 120, 666,  600, 37,  6,  72, SCANTYPE_PROGRESSIVE}	},
+	{0,		49500,	0, {1056,  800,	16,  80, 624,  600,  1,  2,  75, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		65000,	0, {1344, 1024,	24,	136, 806,  768,	 3,	 6,  60, SCANTYPE_PROGRESSIVE}	}, 
+    {0,		75000,	0, {1328, 1024,	24,	136, 806,  768,	 3,	 6,  70, SCANTYPE_PROGRESSIVE}	},
+	{0,		78750,	0, {1312, 1024,	16,	 96, 800,  768,	 1,	 3,  75, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		81517,	0, {1688, 1152,	48,112,1066,  864,	 1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		83460,	0, {1680, 1280,	64,	136, 828,  720,  1,	 3,  60, SCANTYPE_PROGRESSIVE}	}, 
+	
+	{0,		83865,	0, {1680, 1280,	64,	136, 828,  800,  1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+	
+	{0,		100638,	0, {1688, 1280,	48,	112, 1066, 960,  1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		108000,	0, {1688, 1280,	48,	112, 1066, 1024, 1,	 3,  60, SCANTYPE_PROGRESSIVE}	}, 
+	{0,		135000,	0, {1688, 1280,	16,	144, 1066, 1024, 1,	 3,  75, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		85543,	0, {2256, 1360,104, 184, 1087, 768,  1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+	{0,		85920,	0, {2256, 1366,104, 184, 1087, 768,  1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		121750,	0, {1864, 1400,	88,144,1089,  1050,	 3,	 4,  60, SCANTYPE_PROGRESSIVE}	}, 
+    {0,		156000,	0, {1896, 1400,104,144,1099,  1050,	 3,	 4,  75, SCANTYPE_PROGRESSIVE}	},
+
+
+	{0,		88750,	0, {1600, 1440,	48,	32,	 926,  900,	 3,	 6,  60, SCANTYPE_PROGRESSIVE}	},
+	{0,		119000,	0, {1840, 1440,	48,	32,	 1080, 1050, 3,	 6,  60, SCANTYPE_PROGRESSIVE}	},
+ 	
+	{0,		117936,	0, {2256, 1600,104,	184, 1250,  900, 1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+	{0,		162000,	0, {2160, 1600,	64,	192, 1250, 1200, 1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+
+	{0,		147130,	0, {2256, 1680,104,	184, 1087, 1050, 1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+	{0,     148500, 0, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 60, SCANTYPE_PROGRESSIVE} },
+	{0,		154000,	0, {2080, 1920,	48,	 32, 1235, 1200, 3,	 6,  60, SCANTYPE_PROGRESSIVE}	},
+
+};
+
+
+
+
+OUT_FMT ch7036_crt_timing_table[OUT_CRT_END] = {
+   
+   
+   {100,    31500,  1, {832,   640, 32,  64,  445,  400, 1,  3,  85, SCANTYPE_PROGRESSIVE}  },
+	 
+   {100,    25175,  1, {800,   640,  8,  96,  525,  480, 2,  2,  60, SCANTYPE_PROGRESSIVE}  },
+   {100,    31500,  1, {832,   640, 16,  40,  520,  480, 1,  3,  72, SCANTYPE_PROGRESSIVE}  },
+   {100,    31500,  1, {840,   640, 16,  64,  500,  480, 1,  3,  75, SCANTYPE_PROGRESSIVE}  },
+   {100,    36000,  1, {832,   640, 56,  56,  509,  480, 1,  3,  85, SCANTYPE_PROGRESSIVE}  },
+   
+   {100,    35500,  1, {936,   720, 36,  72,  446,  400, 1,  3,  85, SCANTYPE_PROGRESSIVE}  },
+   
+
+   
+   {100,    36000,  1, {1024,  800, 24,  72,  625,  600, 1,  2,  56, SCANTYPE_PROGRESSIVE}  },
+   
+
+   {100,    38250,  1, {1024,  800, 32,  80,  624,  600, 3,  4,  60, SCANTYPE_PROGRESSIVE}  },
+   {100,    50000,  1, {1040,  800, 56, 120,  666,  600, 37, 6,  72, SCANTYPE_PROGRESSIVE}  },
+   {100,    49000,  1, {1040,  800, 40,  80,  629,  600, 3,  4,  75, SCANTYPE_PROGRESSIVE}  },
+   {100,    56750,  1, {1056,  800, 48,  80,  633,  600, 3,  4,  85, SCANTYPE_PROGRESSIVE}  },
+
+    
+   {100,    65000,  1, {1344, 1024, 24, 136,  806,  768, 3,  6,  60, SCANTYPE_PROGRESSIVE}  },
+   {100,    75000,  1, {1328, 1024, 24, 136,  806,  768, 3,  6,  70, SCANTYPE_PROGRESSIVE}  },
+   {100,    78750,  1, {1312, 1024, 16,  96,  800,  768, 1,  3,  75, SCANTYPE_PROGRESSIVE}  }, 
+   
+   {100,    94500,  1, {1376, 1024, 48,  96,  808,  768, 1,  3,  85, SCANTYPE_PROGRESSIVE}  },
+   
+    
+   {100,   108000,  1, {1600, 1152, 64, 128,  900,  864, 1,  2,  75, SCANTYPE_PROGRESSIVE}  },
+
+   
+   {100,    79500,  1, {1664, 1280, 64, 128,  798,  768, 3,  7,  60, SCANTYPE_PROGRESSIVE}  },
+   {100,   102250,  1, {1696, 1280, 80, 128,  805,  768, 3,  7,  75, SCANTYPE_PROGRESSIVE}  },
+   {100,   117500,  1, {1712, 1280, 80, 136,  809,  768, 3,  7,  85, SCANTYPE_PROGRESSIVE}  },
+  
+   
+   {100,   108000,  1, {1800, 1280, 96, 112, 1000,  960, 1,  3,  60, SCANTYPE_PROGRESSIVE}  },
+   {100,   148500,  1, {1728, 1280, 64, 160, 1011,  960, 1,  3,  85, SCANTYPE_PROGRESSIVE}  },
+
+    
+   {100,   108000,  1, {1688, 1280, 48, 112, 1066, 1024, 1,  3,  60, SCANTYPE_PROGRESSIVE}  },
+   {100,   135000,  1, {1688, 1280, 16, 144, 1066, 1024, 1,  3,  75, SCANTYPE_PROGRESSIVE}  },
+   {100,   157500,  1, {1728, 1280, 64, 160, 1072, 1024, 1,  3,  85, SCANTYPE_PROGRESSIVE}  },
+
+   
+   {100,    85500,	1, {1792, 1360,	64, 112,  795,  768, 3,	 6,  60, SCANTYPE_PROGRESSIVE}	},
+
+   
+   {100,   121750,	1, {1864, 1400,	88,144,1089,  1050,	 3,	 4,  60, SCANTYPE_PROGRESSIVE}	},
+   {100,   156000,	1, {1896, 1400,104,144,1099,  1050,	 3,	 4,  75, SCANTYPE_PROGRESSIVE}	},
+
+   
+   {100,	88750,	1, {1600, 1440,	48,	32,	 926,  900,	 3,	 6,  60, SCANTYPE_PROGRESSIVE}	},
+
+   
+   {100,   119000,	1, {1840, 1440,	48,	32,	 1080, 1050, 3,	 6,  60, SCANTYPE_PROGRESSIVE}	},
+
+   
+   {100,   117936,	1, {2256, 1600,104,	184, 1250,  900, 1,	 3,  60, SCANTYPE_PROGRESSIVE}	},
+   
+   
+   {100,   162000,  1, {2160, 1600, 64, 192, 1250, 1200, 1,  3,  60, SCANTYPE_PROGRESSIVE}  },
+   
+   
+   
+   {100,   148500, 1, {2200, 1920, 88,  44, 1125, 1080, 4,  5, 60, SCANTYPE_PROGRESSIVE} },
+   
+   
+   
+};
+
+
+
+
+
+uint8 I2CRead(DEV_CONTEXT* pDevContext,uint8 index)
+{	
+	
+	ch7036_device_context_t *p_ctx= pDevContext->pd_context;
+	pd_reg_t reg_list[2];
+
+	reg_list[0].reg = (i2c_reg_t)index;
+	reg_list[1].reg = PD_REG_LIST_END;
+
+	p_ctx->p_callback->read_regs(p_ctx->p_callback->callback_context, reg_list,PD_REG_DDC);
+
+	return (uint8)(reg_list[0].value);
+}
+
+void I2CWrite(DEV_CONTEXT* pDevContext,uint8 index, uint8 value)
+{
+	ch7036_device_context_t *p_ctx= pDevContext->pd_context;
+	pd_reg_t reg_list[2];
+
+	
+
+	reg_list[0].reg = (i2c_reg_t)index;
+	reg_list[0].value = (i2c_reg_t)value;
+
+	reg_list[1].reg = PD_REG_LIST_END;
+
+	p_ctx->p_callback->write_regs(p_ctx->p_callback->callback_context, reg_list,PD_REG_DDC);
+
+	return;
+}
+
+
+void I2CBlockWrite(DEV_CONTEXT* pDevContext,uint8 index, uint8* value, uint16 len)
+{
+	ch7036_device_context_t *p_ctx= pDevContext->pd_context;
+	pd_reg_t reg_list[MAX_I2C_BLOCK_SIZE +1]; 
+	uint16 i=0;
+	
+
+	for(i=0;i<len;i++) {
+		reg_list[i].reg = (i2c_reg_t)index;
+		reg_list[i].value = (i2c_reg_t)value[i];
+	}
+
+	reg_list[len].reg = PD_REG_LIST_END;
+
+	p_ctx->p_callback->write_regs(p_ctx->p_callback->callback_context, reg_list,PD_REG_DDC);
+
+	return;
+}
+
+ch7036_status_t ch7036_device_prepare(ch7036_device_context_t* p_ctx)
+{
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+	ch7036_status_t status = SS_SUCCESS;
+
+	PD_DEBUG("ch7036_intf: ch7036_device_prepare()\n");
+
+
+	if(!DevicePrepare(p_ch_ctx))
+	{
+		p_ctx->last_emsg = GetLastErrorMessage(); 
+		status = SS_UNSUCCESSFUL; 
+	}
+	
+	return status;
+}
+
+ch7036_status_t ch7036_device_config(ch7036_device_context_t* p_ctx)
+{
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+	ch7036_status_t status = SS_SUCCESS;
+
+	PD_DEBUG("ch7036_intf: ch7036_device_config()\n");
+
+	
+	if(!DeviceConfig(p_ch_ctx))
+	{
+		p_ctx->last_emsg = GetLastErrorMessage();
+		status = SS_UNSUCCESSFUL;
+	}
+	
+	return status;
+	
+}
+
+ch7036_status_t ch7036_device_start(ch7036_device_context_t* p_ctx)
+{
+	
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+	ch7036_status_t status = SS_SUCCESS;
+
+	PD_DEBUG("ch7036_intf: ch7036_device_start()\n");
+	
+	if(!DeviceRunning(p_ch_ctx))
+	{
+		p_ctx->last_emsg = GetLastErrorMessage();
+		status = SS_UNSUCCESSFUL;
+	}
+	
+	return status;
+	
+}
+
+ch7036_status_t ch7036_device_set_power(ch7036_device_context_t* p_ctx, unsigned long channel)
+{
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+	ch7036_status_t status = SS_SUCCESS;
+
+	PD_DEBUG("ch7036_intf: ch7036_device_set_power()- channel [%x]\n", channel);
+	
+	if(!DeviceSetPower(p_ch_ctx,channel))
+	{
+		p_ctx->last_emsg = GetLastErrorMessage();
+		status = SS_UNSUCCESSFUL;
+	}
+	
+	return status;
+}
+
+
+
+ch7036_status_t ch7036_load_firmware(ch7036_device_context_t* p_ctx)
+{
+	
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+
+	ch7036_status_t status = SS_UNSUCCESSFUL;
+
+	PD_DEBUG("ch7036: ch7036_load_firmware()\n");
+
+	
+	if(LHFM_load_firmware(p_ch_ctx) == -1) {
+		PD_DEBUG("ch7036_load_firmware: LHFM_load_firmware()- firmware loading FAILED...\n");
+		
+	}
+	else  {
+		PD_DEBUG("ch7036_load_firmware: LHFM_load_firmware()- firmware loading is a SUCCESS\n");
+		status = SS_SUCCESS;
+	}
+	
+
+	return status;
+
+}
+
+
+
+
+
+
+ch7036_status_t ch7036_get_hdvi_display_modes_supported(ch7036_device_context_t* p_ctx) 
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	ch7036_status_t status;
+	ch7036_edid_blk_t* p_hedid = (ch7036_edid_blk_t *)p_ctx->hedid;
+
+
+	PD_DEBUG("ch7036_get_hdvi_display_modes_supported()- enter\n");
+
+	status = LHFM_get_hdmi_modeinfo(p_ch7xxx_context,p_hedid->supported_modes);
+
+	if (status == SS_SUCCESS) {
+
+		PD_DEBUG("HDMI_Modes=%02X. Vesa_Modes=%02x\r\n", p_hedid->supported_modes[13], p_hedid->supported_modes[14]);
+
+						
+	}
+	else {
+		PD_DEBUG("ch7036_get_hdvi_display_modes_supported()-- failed!\r\n");
+		PD_DEBUG("status: [%s]\n",status == SS_FIRMWARE_TIMEOUT?"timeout!":"firmware_error!");
+
+		
+	}
+	
+	return status;
+}
+
+
+
+ch7036_status_t ch7036_read_edid(ch7036_device_context_t* p_ctx, uint32 channel)
+{
+	
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	ch7036_status_t status = SS_UNSUCCESSFUL; 
+	
+
#ifdef T_CH7036_EDID_DUMP
+	uint8 ebn;
+	int i;
+#endif
+
+	ch7036_edid_blk_t* p_hedid = (ch7036_edid_blk_t *)p_ctx->hedid;
+	ch7036_edid_blk_t* p_cedid = (ch7036_edid_blk_t *)p_ctx->cedid;
+
+
+	unsigned char* hedidblk = p_hedid->edidblk;
+	unsigned char* cedidblk = p_cedid->edidblk;
+
+
+
+	switch (channel) {
+
+		case CHANNEL_LVDS_HDMI:
+		case CHANNEL_HDMI:
+
+			
+			status = LHFM_get_edid(p_ch7xxx_context,hedidblk, &(p_hedid->ebn), CH7036_HDMI_DDC);
+
+			if(status == SS_SUCCESS) {	
+			
+				PD_DEBUG("ch7036_read_edid()-hdmi-dvi hpd status- attached, hdmi-dvi edid read is a SUCCESS\n");
+				PD_DEBUG("ch7036_read_edid()- number of blocks read [%x]\n", p_hedid->ebn);
+			
+				p_hedid->is_edid = 1;
+				if(p_hedid->ebn == 1)  
+					pOutput_Info->hdmi_fmt.is_dvi_mode =1;
+							
+				else 
+					pOutput_Info->hdmi_fmt.is_dvi_mode =0;
+				
+
+		
+#ifdef T_CH7036_EDID_DUMP
+	
+				if (p_hedid->ebn <= MAX_EDID_BLOCKS) { 
+					for (i=0; i<p_hedid->ebn; i++) 
+						ch7036_dump("HDMI-DVI EDID Data", 128, &hedidblk[i*128]);
+								
+				}
+
+#endif
+
+			} 
+
+			else { 
+				p_hedid->is_edid = 0;
+				
+				status = SS_UNSUCCESSFUL;
+
+			}	
+
+			break;
+
+		case CHANNEL_LVDS_VGA:
+		case CHANNEL_VGA:
+			
+			I2CWrite(p_ch7xxx_context,0x03, 0x01);			
+			I2CWrite(p_ch7xxx_context,0x0F, I2CRead(p_ch7xxx_context,0x0F) & 0x7F);		
+			pd_usleep(200);
+
+			status = LHFM_get_edid(p_ch7xxx_context,cedidblk, &(p_cedid->ebn), CH7036_VGA_DDC);
+			if (status== SS_SUCCESS) {
+				p_cedid->is_edid =1;
+				PD_DEBUG("ch7036_read_edid()- crt hpd status- attached, crt edid read is a SUCCESS\n");
+				PD_DEBUG("ch7036_read_edid()- number of blocks read [%x]\n", p_cedid->ebn);
+
+#ifdef T_CH7036_EDID_DUMP
+				if (p_cedid->ebn <= MAX_EDID_BLOCKS) 
+					for (i=0; i<p_cedid->ebn; i++) 
+						ch7036_dump("VGA EDID Data", 128, &cedidblk[i*128]);
+
+
+#endif
+
+			}
+			else {
+				p_cedid->is_edid =0;
+					
+				status = SS_UNSUCCESSFUL;
+			}
+
+	
+			break;
+
+		default:
+			break;
+
+
+	} 
+
+
+	return status;
+}
+
+ch7036_status_t ch7036_get_hdmi_hpd(ch7036_device_context_t* p_ctx, uint8 *hpd)
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	ch7036_status_t status= SS_SUCCESS;
+	unsigned char reg, reg_hpdpw;
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x03);
+	reg = I2CRead(p_ch7xxx_context,0x25);
+
+	I2CWrite(p_ch7xxx_context,0x03, 0x04);
+	reg_hpdpw = I2CRead(p_ch7xxx_context,0x51);
+
+#ifdef T_CONFIG_PLB	
+	if(!(reg & 0x10) && (reg_hpdpw & 0x80) ){
+		reg= 0x10;
+	}
+#endif
+
+	switch (reg & 0x10) {
+		case 0x10:
+			if(p_ctx->hpd & CH7036HPD_HDVI_ATTACHED)
+				*hpd = 0x01;
+			else
+				*hpd = 0x81;
+			break;
+		case 0x00:
+			if(p_ctx->hpd & CH7036HPD_HDVI_ATTACHED)
+				*hpd = 0x80;
+			else
+				*hpd = 0x00;
+			break;
+	}
+	
+
+	PD_DEBUG("ch7036: ch7036_get_hdmi_hpd- exit...*hpd [0x%x]\n",*hpd);
+
+	return status;
+
+}
+ch7036_status_t ch7036_get_attached_device(ch7036_device_context_t* p_ctx)
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	uint8 reg;
+	ch7036_status_t status = SS_SUCCESS;
+	ch7036_status_t status_hdmi, status_crt;
+
+	ch7036_edid_blk_t * p_hedid = (ch7036_edid_blk_t *)(p_ctx->hedid);
+
+	uint8 hpd = 0;
+
+
+	
+	I2CWrite(p_ch7xxx_context,0x03, 0x04);
+	reg = I2CRead(p_ch7xxx_context,0x52);
+	
+	reg = reg & 0xEF; 
+	I2CWrite(p_ch7xxx_context,0x52, reg);
+
+	
+	I2CWrite(p_ch7xxx_context,0x03, 0x0);
+	reg = I2CRead(p_ch7xxx_context,0x07);
+	
+	reg = reg & 0x70; 
+	I2CWrite(p_ch7xxx_context,0x07, reg);
+	
+	I2CWrite(p_ch7xxx_context,0x03, 0x01);
+	reg = I2CRead(p_ch7xxx_context,0x0F);
+	
+	reg = reg & 0x7F; 	
+	I2CWrite(p_ch7xxx_context,0x0F, reg); 
+
+	I2CWrite(p_ch7xxx_context,0x0E, I2CRead(p_ch7xxx_context,0x0E) & 0x7F);
+		
+	PD_DEBUG("ch7036_get_attached_device()- enter- p_ctx->hpd= [0x%x]\n", p_ctx->hpd);
+
+	status_hdmi = ch7036_get_hdmi_hpd(p_ctx,&hpd);
+	LHFM_enable_crt_hpd(p_ch7xxx_context);  
+	status_crt = LHFM_get_crt_hpd(p_ch7xxx_context);
+
+	PD_DEBUG("ch7036_get_attached_device()- enter- pOutput_Info->channel = [0x%x]\n", pOutput_Info->channel);
+	
+	if(p_ctx->man_sel_out==1) { //manual selection
+
+		
+		if( (status_crt == SS_SUCCESS) || ( (hpd & 0x01)== 1 ) ) { 
+
+			switch (pOutput_Info->channel) {				
+				case CHANNEL_LVDS_DVI: 
+				case CHANNEL_DVI: 
+				case CHANNEL_LVDS_HDMI:
+				case CHANNEL_HDMI: 
+
+					 
+					if( (status_crt == SS_SUCCESS) && ( (hpd & 0x01)== 0 ) ) { 
+
+						p_ctx->hpd = 0x16; //bit 4=1 to indicate SS_DISPLAY_CHOICE_NOT_ALLOWED,  check edid and parse it												
+						status = SS_DISPLAY_CHOICE_NOT_ALLOWED;
+
+					}
+					else {
+
+						if (!p_ctx->init_done || ((p_ctx->prev_outchannel & pOutput_Info->channel) & 0x02) != 0x02 ) 
+							p_ctx->hpd = 0x60;
+						else 
+							p_ctx->hpd = ( ((p_ctx->prev_outchannel & pOutput_Info->channel) & 0x02) == 0x02)?0xA0:0x60;							
+											
+						status = SS_SUCCESS;
+					}
+										
+					break;
+
+				case CHANNEL_LVDS_VGA: 
+				case CHANNEL_VGA:
+					
+					
+					if((status_crt != SS_SUCCESS) && ((hpd & 0x01)==1 ) ) { 
+						
+						if(!p_ctx->init_done) { 
+							p_ctx->hpd = 0x06; 
+							status = SS_SUCCESS;
+							
+						}else {					
+							p_ctx->hpd = (hpd & 0x80)?0x70:0x30; 
+							status = SS_DISPLAY_CHOICE_NOT_ALLOWED;
+						}
+						
+					}
+					else { 
+
+						if ( (!p_ctx->init_done) || ((p_ctx->prev_outchannel & pOutput_Info->channel) & 0x04) != 0x04 )//at init, need to read edid, and parse it
+							p_ctx->hpd = 0x06;
+						else {							
+							p_ctx->hpd = ( ((p_ctx->prev_outchannel & pOutput_Info->channel) & 0x04) == 0x04)?0x0A:0x06;
+													
+						}
+
+						status = SS_SUCCESS;
+					}
+															
+					break;
+			} //switch
+
+			if(status == SS_DISPLAY_CHOICE_NOT_ALLOWED)
+				PD_DEBUG("ch7036_get_attached_device()- manual selection- display choice is not allowed...\n");
+
+		}
+		else {
+			
+			if (!p_ctx->init_done) { 
+				p_ctx->hpd = 0x06; 
+				status = SS_SUCCESS;
+				
+			}else {
+
+				PD_DEBUG("ch7036_get_attached_device()- manual selection- none is connected- not allowed...\n");
+				
+				status = SS_DISPLAY_CHOICE_NOT_ALLOWED;
+				p_ctx->hpd = 0x50;
+
+			}
+
+
+		}
+
+		PD_DEBUG("ch7036_get_attached_device()- manual selection- exit- p_ctx->hpd= [0x%x]\n", p_ctx->hpd);
+		PD_DEBUG("ch7036_get_attached_device()- exit- pOutput_Info->channel = [0x%x]\n", pOutput_Info->channel);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x03);
+		reg = I2CRead(p_ch7xxx_context,0x25);
+
+		PD_DEBUG("ch7036_get_attached_device()- manual selection- exit...HPD_MCU [0x%x]\n",reg);
+
+		return status;
+	}
+
+	//auto detection 
+
+	if(status_hdmi == SS_SUCCESS) { 
+			
+			if( hpd == 0x81 ) { 
+				if(p_ctx->hpd & CH7036HPD_HDVI_ATTACHED)  {
+
+					p_ctx->hpd = (p_ctx->hpd  & 0x9F) & ((~CH7036HPD_HDVI_STATUS_CHANGED) | CH7036HPD_HDVI_ATTACHED); 
+										
+					ch7036_get_hdvi_display_modes_supported(p_ctx);
+				
+					if ( 
+						((pOutput_Info->hdmi_fmt.is_dvi_mode== 0) && (p_hedid->supported_modes[13] == 0) )||
+						((pOutput_Info->hdmi_fmt.is_dvi_mode== 1) && (p_hedid->supported_modes[13] > 0))
+					) {
+
+						p_ctx->hpd = (p_ctx->hpd  & 0x9F) | (CH7036HPD_HDVI_STATUS_CHANGED | CH7036HPD_HDVI_ATTACHED);
+						PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status changed [HDMI<->DVI] since last query and it's HIGH\n");
+
+					}
+					else					
+						PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status has not changed since last query and it's HIGH\n"); 
+				}
+				else {
+
+
+					p_ctx->hpd = (p_ctx->hpd  & 0x9F) | (CH7036HPD_HDVI_STATUS_CHANGED | CH7036HPD_HDVI_ATTACHED);
+					PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status changed since last query and it's HIGH\n");
+				}
+			}
+			else if (hpd ==0x80) {	
+				if( (p_ctx->hpd & CH7036HPD_HDVI_ATTACHED) ==0 )  {
+
+					p_ctx->hpd = (p_ctx->hpd  & 0x9F) & ((~ CH7036HPD_HDVI_STATUS_CHANGED) & (~CH7036HPD_HDVI_ATTACHED)) ;
+					PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status not changed since last query and it's LOW\n");
+				}
+				else {
+
+					p_ctx->hpd = (p_ctx->hpd  & 0x9F) | (CH7036HPD_HDVI_STATUS_CHANGED & (~CH7036HPD_HDVI_ATTACHED)) ;
+					PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status changed since last query and it's LOW\n");
+				}
+		
+			}
+			else if (hpd == 0x01) {
+
+				p_ctx->hpd = (p_ctx->hpd  & 0x9F) & ((~CH7036HPD_HDVI_STATUS_CHANGED) | CH7036HPD_HDVI_ATTACHED);
+				PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status has not changed since last query and it's HIGH\n");
+			}
+			else { 
+
+				p_ctx->hpd &= 0x9F; 
+				PD_DEBUG("ch7036_get_attached_device()- hdvi HPD status has not changed since last query and it's LOW\n");
+			}
+			
+
+			
+			PD_DEBUG("ch7036: ch7036_get_attached_device()- SUCCESS- hdmi hpd [0x%x]\n", hpd);
+			
+	}
+	else {
+
+		
+		if ( (!p_ctx->init_done) &&  (hpd == 0x86) )  {
+
+			p_ctx->hpd |= CH7036HPD_HDVI_ATTACHED;
+							
+		}
+
+		else {
+			p_ctx->hpd &= ~CH7036HPD_HDVI_ATTACHED;
+			
+		}
+		
+	}
+
+	if(status_crt == SS_SUCCESS) {  
+		
+		if( (p_ctx->hpd & CH7036HPD_CRT_ATTACHED )== CH7036HPD_CRT_ATTACHED)  
+			p_ctx->hpd = (p_ctx->hpd  & 0xF9) & ((~CH7036HPD_CRT_STATUS_CHANGED)  | CH7036HPD_CRT_ATTACHED);
+					
+		else 
+			p_ctx->hpd = ((p_ctx->hpd  & 0xF9) | ( CH7036HPD_CRT_STATUS_CHANGED | CH7036HPD_CRT_ATTACHED));
+									
+	} 
+	else  {  
+		
+		if( (p_ctx->hpd & CH7036HPD_CRT_ATTACHED ) == 0 ) { 
+			p_ctx->hpd &= 0xF9; 
+
+			if( (p_ctx->hpd & CH7036HPD_HDVI_ATTACHED)== 0  ) 
+				p_ctx->hpd |= 0x06; 
+										
+		}
+		else  {
+			
+			if ( (p_ctx->hpd & CH7036HPD_HDVI_ATTACHED) ==0 )
+				p_ctx->hpd &= 0xFB; 
+			else
+				p_ctx->hpd = (p_ctx->hpd & 0xF9) |  CH7036HPD_CRT_STATUS_CHANGED;		
+		}
+			
+	}
+
+	PD_DEBUG("ch7036_get_attached_device()- auto detection - exit- p_ctx->hpd= [0x%x]\n", p_ctx->hpd);	
+	return SS_SUCCESS;
+}
+
+
+void ch7036_reset(ch7036_device_context_t* p_ctx)
+{
+	DEV_CONTEXT* p_ch_ctx = p_ctx->p_ch7xxx_context;
+
+	ch7036_reset_mcu(p_ch_ctx); 
+	ch7036_reset_datapath(p_ch_ctx);
+
+	return;
+}
+
+void ch7036_reset_datapath(DEV_CONTEXT* p_ch_ctx)
+{
+
+	PD_DEBUG("ch7036: ch7036_reset_datapath()-enter\n");
+    I2CWrite(p_ch_ctx,0x03, 0x04); 
+	I2CWrite(p_ch_ctx,0x52, 0x2E);
+	
+	pd_usleep(50); 
+	I2CWrite(p_ch_ctx,0x52, 0x2F);
+	
+	return;
+}
+
+void ch7036_reset_mcu(DEV_CONTEXT* p_ch_ctx)
+{
+
+	PD_DEBUG("ch7036: ch7036_reset_mcu()-enter\n");
+
+    I2CWrite(p_ch_ctx,0x03, 0x04);
+	I2CWrite(p_ch_ctx,0x52, 0x2B);
+	
+	pd_usleep(50); 	
+	I2CWrite(p_ch_ctx,0x52, 0x2F);
+	
+	
+	PD_DEBUG("ch7036: ch7036_reset_mcu()-exit-\n");
+	return;
+}
+
+
+
+
+
+void ch7036_set_input_timing_info(ch7036_device_context_t *p_ctx, INPUT_INFO* pInput_Info)
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+
+	pd_timing_t * p_current_mode = &(p_ctx->native_dtd);
+	uint8 audio_id = AUDIO_SPDIF;
+	PD_DEBUG("ch7036_intf: ch7036_set_input_timing_info()-\n");
+
+	
+	if (p_ctx->init_done) { 
+
+	
+		pInput_Info->timing.ht = p_current_mode->htotal+1;
+	
+		pInput_Info->timing.ha = p_current_mode->width;
+		pInput_Info->timing.ho = p_current_mode->hsync_start - p_current_mode->hblank_start;
+		pInput_Info->timing.hw = p_current_mode->hsync_end - p_current_mode->hsync_start;
+		pInput_Info->timing.vt = p_current_mode->vtotal+1;
+	
+		pInput_Info->timing.va = p_current_mode->height;
+		pInput_Info->timing.vo = p_current_mode->vsync_start - p_current_mode->vblank_start;
+		pInput_Info->timing.vw = p_current_mode->vsync_end - p_current_mode->vsync_start;
+
+	
+		pInput_Info->rx_clk_khz = p_current_mode->dclk; 
+
+		
+		pInput_Info->hs_pol = ((uint8)(((p_current_mode)->mode_info_flags & PD_HSYNC_HIGH) >> 24 ))?1:0;  
+		pInput_Info->vs_pol = ((uint8)(((p_current_mode)->mode_info_flags & PD_VSYNC_HIGH) >> 24))?1:0; 
+
+	}
+
+
+	
+	
+	
+	pInput_Info->pixel_fmt = ((lvds_context_t *)p_ctx->internal_lvds)->panel_depth == 18?PIXEL_FMT_18BIT:3; 
+
+	
+	pInput_Info->data_ch_pol = POL_NO_INV;
+
+	
+	pInput_Info->data_ch_invert = POL_NO_INV;
+
+	
+
+	
+
+	pInput_Info->de_pol = POL_HIGH;  
+	
+	
+	
+	
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI)
+		ch7036_set_audio_type(pInput_Info, audio_id);
+	
+}
+
+void ch7036_set_output_timing_info(ch7036_device_context_t *p_ctx, OUTPUT_INFO* pOutput_Info)
+{
+
+
+	PD_DEBUG("ch7036: ch7036_set_output_timing_info()\n");
+		
+	
+	PD_DEBUG("ch7036_set_output_timing_info()- output channel from pd context[%u]\n",pOutput_Info->channel);
+
+	
+	pOutput_Info->lvds_fmt.channel_swap = LVDS_CHANNEL_SWAP_DEF; 
+	pOutput_Info->lvds_fmt.channel_pol = (POL_LOW << 4) | (POL_LOW << 3) | (POL_LOW << 2) | (POL_LOW << 1) | (POL_LOW << 0);
+	pOutput_Info->lvds_fmt.pixel_fmt = p_ctx->dither_select;
+
+	
+	
+	pOutput_Info->hdmi_fmt.channel_swap = 0;
+	pOutput_Info->hdmi_fmt.data_pol_invert = POL_NO_INV;
+	pOutput_Info->hdmi_fmt.protect_enable = 0;
+
+
+	
+	
+	if (pOutput_Info->channel & CHANNEL_HDMI) 
+	{
+		
+		if(!(pOutput_Info->hdmi_fmt.is_dvi_mode))
+		{
+			PD_DEBUG("ch7036_set_output_timing_info- hdmi mode index is [0x%x]\n",p_ctx->hdmi_mode_index);
+			pOutput_Info->hdmi_fmt.format_index = (uint8)hdmi_timing_table[p_ctx->hdmi_mode_index].fmt_index; 
+			pOutput_Info->hdmi_fmt.aspect_ratio = (uint8)hdmi_timing_table[p_ctx->hdmi_mode_index].aspect;
+
+			pOutput_Info->timing.ht = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.ht;
+			pOutput_Info->timing.ha = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.ha;
+			pOutput_Info->timing.ho = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.ho;
+			pOutput_Info->timing.hw = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.hw;
+			pOutput_Info->timing.vt = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.vt;
+			pOutput_Info->timing.va = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.va;
+			pOutput_Info->timing.vo = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.vo;
+			pOutput_Info->timing.vw = hdmi_timing_table[p_ctx->hdmi_mode_index].timing.vw;
+			pOutput_Info->uclk_khz = hdmi_timing_table[p_ctx->hdmi_mode_index].clk_freq;
+		}
+		else 
+		{
+			pOutput_Info->hdmi_fmt.format_index = (uint8)dvi_timing_table[p_ctx->dvi_mode_index].fmt_index; 
+			pOutput_Info->hdmi_fmt.aspect_ratio = (uint8)dvi_timing_table[p_ctx->dvi_mode_index].aspect;
+
+			pOutput_Info->timing.ht = dvi_timing_table[p_ctx->dvi_mode_index].timing.ht;
+			pOutput_Info->timing.ha = dvi_timing_table[p_ctx->dvi_mode_index].timing.ha;
+			pOutput_Info->timing.ho = dvi_timing_table[p_ctx->dvi_mode_index].timing.ho;
+			pOutput_Info->timing.hw = dvi_timing_table[p_ctx->dvi_mode_index].timing.hw;
+			pOutput_Info->timing.vt = dvi_timing_table[p_ctx->dvi_mode_index].timing.vt;
+			pOutput_Info->timing.va = dvi_timing_table[p_ctx->dvi_mode_index].timing.va;
+			pOutput_Info->timing.vo = dvi_timing_table[p_ctx->dvi_mode_index].timing.vo;
+			pOutput_Info->timing.vw = dvi_timing_table[p_ctx->dvi_mode_index].timing.vw;
+			pOutput_Info->uclk_khz = dvi_timing_table[p_ctx->dvi_mode_index].clk_freq;
+
+		}
+
+
+
+	} else if((pOutput_Info->channel & CHANNEL_VGA) && ((pOutput_Info->channel & CHANNEL_HDMI)==0x00) ) 
+	{
+		
+		
+		
+		PD_DEBUG("ch7036_set_output_timing_info- crt mode index is [0x%x]\n",p_ctx->crt_mode_index);
+		pOutput_Info->timing.ht = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.ht;
+		pOutput_Info->timing.ha = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.ha;
+		pOutput_Info->timing.ho = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.ho;
+		pOutput_Info->timing.hw = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.hw;
+		pOutput_Info->timing.vt = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.vt;
+		pOutput_Info->timing.va = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.va;
+		pOutput_Info->timing.vo = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.vo;
+		pOutput_Info->timing.vw = ch7036_crt_timing_table[p_ctx->crt_mode_index].timing.vw;
+		pOutput_Info->uclk_khz = ch7036_crt_timing_table[p_ctx->crt_mode_index].clk_freq;
+
+		
+	} else
+		; 
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI)
+		ch7036_set_hdmi_sync_polarity(pOutput_Info);
+
+	if(pOutput_Info->channel & CHANNEL_HDMI || pOutput_Info->channel & CHANNEL_VGA) {
+		
+		ch7036_set_rotate (pOutput_Info);
+		ch7036_set_hflip (pOutput_Info);
+		ch7036_set_vflip (pOutput_Info);
+	}
+}
+
+void ch7036_set_prefer_timing_info(ch7036_device_context_t *p_ctx, PREFER_INFO* pPrefer_Info)
+{
+	
+	
+	
+
+	PD_DEBUG("ch7036_intf: ch7036_set_prefer_timing_info()\n");
+
+	
+	if (!p_ctx->init_done) {
+
+	pPrefer_Info->mclk_khz = 166000; 
+	pPrefer_Info->uclkod_sel = 1;
+	pPrefer_Info->dat16_32b = 0;
+	pPrefer_Info->true24 = 0;
+	pPrefer_Info->true_com = 0;
+	pPrefer_Info->lvds_out_hs_tolerance = HS_TOLERANCE_LEVEL0;
+	pPrefer_Info->lvds_out_reset_bit_sel = RST_BIT_VSYNC;
+	pPrefer_Info->dither_filter_enable = DITHER_ENABLE;  
+		
+	pPrefer_Info->hscale_ratio_gate = 130;	
+	pPrefer_Info->reset=0; 
+	pPrefer_Info->vga_enable=0;
+
+	pPrefer_Info->text_enhancement = DEFAULT_TEXT_ENHANCE; 
+	pPrefer_Info->pll_ref_dly = DEF_PLL_REF_DLY;
+	pPrefer_Info->pll_ref_fbdly = DEF_PLL_REF_FBDLY;
+	pPrefer_Info->lvds_txdrv_ctrl = DEF_LVDS_TXDRV_CTRL;
+
+	pPrefer_Info->eye_bgtrim=0;
+	pPrefer_Info->eye_dacg=0;
+	pPrefer_Info->eye_dri_demp=0;
+	pPrefer_Info->eye_dri_pll_cp=0;
+	pPrefer_Info->eye_dri_damp=0;
+	pPrefer_Info->eye_dri_pll_rlf=0;
+	pPrefer_Info->eye_rdac=0;
+
+	}
+
+	pPrefer_Info->scale_line_adjust = 0;
+
+
+	
+}
+
+
+ch7036_status_t ch7036_parse_standard_edid(ch7036_device_context_t* p_ctx, uint32 channel)
+{
+	uint8 i, index = 0;
+	
+	ch7036_edid_blk_t* p_edid = (ch7036_edid_blk_t *)p_ctx->cedid;
+	unsigned char* p_edidblk = p_edid->edidblk;
+
+	
+	OUT_FMT* p_table = ch7036_crt_timing_table;
+	TIMING* p_timing;
+
+	unsigned char j=0;
+
+
+
+
+	
+	established_timings_t *p_etiming_I = p_edid->etiming_I;
+	established_timings_t *p_etiming_II = p_edid->etiming_II;
+
+
+	
+	standard_timings_t	*stiming = p_edid->stiming; 
+	ch7036_attr_table_index_t* p_modes =  p_edid->modes;
+
+	unsigned long idx = 2; 
+
+
+	PD_DEBUG("ch7036_parse_stardard_edid() channel [0x%x] - enter...\n", channel);
+
+
+	if(channel == CHANNEL_LVDS_HDMI) {
+
+		p_edid = (ch7036_edid_blk_t *)p_ctx->hedid;
+		p_edidblk = p_edid->edidblk;
+		p_etiming_I = p_edid->etiming_I;
+		p_etiming_II = p_edid->etiming_II;
+
+		stiming = p_edid->stiming; 
+		p_modes =  p_edid->modes;
+
+		
+		if (p_edid->ebn > 1) { 
+			idx = 1;
+			p_table = hdmi_timing_table;
+		}
+		else { 
+			idx = 0;
+			p_table = dvi_timing_table;
+			channel = 7; 
+		}
+
+
+	}
+
+	
+	while (index < MAX_ATTR_LIST_SIZE)
+		p_modes[index++] = FALSE; 
+
+
+	if (p_edidblk[EDID_EXTENSION_FLAG] == 0x00 ) {
+			
+
+		ch7036_parse_standard_timing(p_edid,0);
+
+		
+		ch7036_parse_established_timing(p_ctx, p_edid);
+		
+
+		
+		ch7036_parse_detailed_descriptor_blocks(p_ctx, p_edid);
+				
+
+		
+		for(i=0; i<8;i++) { 
+
+
+			index=0;
+			while (index < MAX_ATTR_LIST_SIZE ) {
+				if(p_modes[index]== TRUE) {
+					index++; 
+					continue;
+				}
+				p_timing = &(p_table[index].timing);
+
+				if ( (p_edid->dtblk[j]).data_tag & 0x00FFFFFF ) { 
+					OUT_FMT* p_dtd = &((p_edid->dtblk[j]).dtiming);
+					
+					if( (p_dtd->timing.ha == p_timing->ha) && (p_dtd->timing.va == p_timing->va) && (p_dtd->timing.hz >= p_timing->hz) ) {
+						p_modes[index] = TRUE;
+						j= j> 3?3:j+1;
+
+						PD_DEBUG("ch7036_parse_standard_edid()- detailed timing mode supported- index [%d] name [%s]...\n",index,ch7036_get_mode_name(channel,index) );
+
+					}
+
+				}
+							
+				
+				if( (stiming[i].ha == p_timing->ha) && (stiming[i].va == p_timing->va) && (stiming[i].refresh_rate >= p_timing->hz) ) {
+					p_modes[index] = TRUE;
+					PD_DEBUG("ch7036_parse_standard_edid()- std_timing mode supported- index [%d] name [%s]...\n",index,ch7036_get_mode_name(channel,index) );
+				}
+				
+				index++;
+
+			}
+
+		}
+
+		
+		for(i=0;i<4;i++) {
+			if ( (p_edid->dtblk[i]).data_tag == 0xFA000000) {
+				; 
+				continue;
+			}
+		}
+
+		
+
+		for(i=0; i<8;i++) { 
+
+			
+			
+			if( (p_etiming_I[i].is_supported == TRUE) && (p_etiming_I[i].index[idx] != OUT_CRT_END) ) {
+				p_modes[p_etiming_I[i].index[idx]] = TRUE; 
+				PD_DEBUG("ch7036_parse_standard_edid()- et1 mode supported- index [%d] name [%s]...\n",p_etiming_I[i].index[idx],p_etiming_I[i].mode_name);
+				continue;
+
+			}
+
+			
+			if( (i==7) && (channel ==7) && (p_etiming_I[i].is_supported == TRUE) ) {
+				p_modes[p_etiming_I[i].index[idx]] = TRUE; 
+				PD_DEBUG("ch7036_parse_standard_edid()- et1 mode supported- index [%d] name [%s]...\n",p_etiming_I[i].index[idx],p_etiming_I[i].mode_name);
+			}
+
+			
+		}
+
+		for(i=0; i<8;i++) { 
+		
+			if( (p_etiming_II[i].is_supported == TRUE) && (p_etiming_II[i].index[idx] != OUT_CRT_END) ) {
+				p_modes[p_etiming_II[i].index[idx]] = TRUE; 
+				PD_DEBUG("ch7036_parse_standard_edid()- et2 mode supported- index [%d] name [%s]...\n",p_etiming_II[i].index[idx],p_etiming_II[i].mode_name);
+				continue;
+
+			}
+
+		}
+
+		
+
+	}
+	else { 
+		PD_DEBUG("ch7036_parse_standard_edid()- vga/dvi has more than one 128 byte block\n");
+	}
+
+
+	PD_DEBUG("ch7036_parse_stardard_edid()-channel [0x%x] - exit...\n", channel);
+
+	return SS_SUCCESS;
+
+}
+
+void ch7036_parse_detailed_descriptor_blocks(ch7036_device_context_t* p_ctx, ch7036_edid_blk_t* p_edid)
+{
+	unsigned long *monitor_descriptor;
+	unsigned char* p_ebuf, *p_st;
+	unsigned char* p_edidblk = p_edid->edidblk;
+	unsigned char i;
+	OUT_FMT* p_dt;
+	
+
+	p_ebuf = &(p_edidblk[EDID_DETAILED_TIMING_DESCRIPTION_1]);
+
+	PD_DEBUG("parse_detailed_descriptor_blocks()- enter...\n");
+
+	for(i=0;i<4;i++) {
+
+		monitor_descriptor = (unsigned long *) p_ebuf;
+
+		if((*monitor_descriptor) & 0x00FFFFFF ) { 
+			
+			
+			p_dt = &(p_edid->dtblk[i].dtiming);				
+			ch7036_parse_detailed_timing(p_dt, p_ebuf);
+			
+		}
+			
+		else { 
+
+			p_st = p_ebuf;
+			p_st +=5;
+
+			switch (*monitor_descriptor) {
+				case 0xFA000000: 
+					ch7036_parse_standard_timing(p_edid,p_st); 
+					break;
+
+				case 0xFD000000: 
+					(p_edid->rtiming).vrate_min = *p_st; 
+					(p_edid->rtiming).vrate_max = *(p_st+1);
+					(p_edid->rtiming).hrate_min = *(p_st+2); 
+					(p_edid->rtiming).hrate_max = *(p_st+3); 
+					(p_edid->rtiming).pclk_max = (unsigned long)(*(p_st+4))*10000L;	
+					
+					
+					break;
+
+				case 0xFC000000: 
+				case 0xFF000000: 
+				default: 
+					break;
+			}
+		}
+
+		(p_edid->dtblk[i]).data_tag = *monitor_descriptor;
+
+		p_ebuf += 18;
+	}
+	
+	return;
+}
+
+void ch7036_parse_detailed_timing(OUT_FMT *p_dt, unsigned char* p_ebuf)
+{
+
+	unsigned short blanking;
+
+	PD_DEBUG("ch7036_parse_detailed_descriptor_timing()- enter...\n");
+
+
+	p_dt->clk_freq = ((uint32)(p_ebuf[1]<<8) | p_ebuf[0]) * 10; 
+
+	p_dt->timing.ha = ((uint16)(p_ebuf[4] & 0xF0) << 4) | p_ebuf[2];
+
+	PD_DEBUG("ch7036_parse_detailed_timing() byte 3 [%x] byte 5 [%x]\n",p_ebuf[2],p_ebuf[4]);
+
+	blanking = ((uint16)(p_ebuf[4] & 0x0F) << 8) | p_ebuf[3]; 
+	p_dt->timing.ht = p_dt->timing.ha + blanking;
+
+	p_dt->timing.va = ((uint16)(p_ebuf[7] & 0xF0) << 4) | p_ebuf[5];
+
+	PD_DEBUG("ch7036_parse_detailed_timing() byte 6 [%x] byte 8 [%x]\n",p_ebuf[5],p_ebuf[7]);
+
+	blanking = ((uint16)(p_ebuf[7] & 0x0F) << 8) | p_ebuf[6]; 
+	p_dt->timing.vt = p_dt->timing.va + blanking;
+
+	PD_DEBUG("ch7036_parse_detailed_timing()- pclk [%d] Khz ha [%d] va [%d] ht [%d] vt [%d]\n",p_dt->clk_freq,p_dt->timing.ha,p_dt->timing.va,p_dt->timing.ht,p_dt->timing.vt);
+
+	p_dt->timing.hz = 	(((p_dt->clk_freq / p_dt->timing.ht) + 1 ) * 1000) / p_dt->timing.vt; 
+
+	p_dt->timing.stype = (p_ebuf[17] & 0x80)?0:1; 
+
+	PD_DEBUG("ch7036_parse_detailed_timing()- refresh [%d] scantype [%d]\n",
+		p_dt->timing.hz,p_dt->timing.stype);
+
+	return;	
+}
+
+
+
+void ch7036_parse_standard_timing(ch7036_edid_blk_t* p_edid, unsigned char* p_addtional_st)
+{
+	standard_timings_t *stiming = p_edid->stiming;
+	unsigned char i, max=8;
+
+	unsigned char* p_edidblk = &(p_edid->edidblk[EDID_STANDARD_TIMINGS]);
+
+	
+	PD_DEBUG("ch7036_parse_standard_timing()- enter\n");
+	
+	if(p_addtional_st) {
+		p_edidblk = p_addtional_st;
+		stiming = p_edid->stiming_x;
+		max = 6;
+	}
+
+	for(i = 0; i < max; stiming++,i++) {
+			
+	
+		if( (*p_edidblk) == 0x01 &&  *(p_edidblk+1) == 0x01) 
+			continue;
+	
+		stiming->ha = ((*p_edidblk) + 31) << 3 ;
+		 
+		stiming->refresh_rate = (*(p_edidblk + 1) & 0x3F) + 60;
+
+		switch(*(p_edidblk +1) >> 6) {
+	
+		   case 0: 
+			   stiming->va =  (stiming->ha *10) >> 4;
+			   break;
+		   case 1: 
+			   stiming->va =  (stiming->ha *3) >> 2;
+			   break;
+		   case 2: 
+			   stiming->va =  (stiming->ha << 2) / 5;			       
+			   break;
+		   case 3: 
+			   stiming->va =  (stiming->ha* 9) >>4;			       
+			   break;
+		   default:
+			   break;
+		} 
+
+		p_edidblk +=2;
+
+		PD_DEBUG("ch7036_parse_standard_timing()- ha [%d] va [%d] refresh [%d]\n",stiming->ha,stiming->va,stiming->refresh_rate);
+
+	} 
+	
+	return;
+}
+
+
+
+
+void ch7036_parse_established_timing(ch7036_device_context_t* p_ctx, ch7036_edid_blk_t* p_edid) 
+{
+
+	unsigned char* p_edidblk = p_edid->edidblk;
+
+	
+
+	established_timings_t *p_etiming_I = p_edid->etiming_I;
+	established_timings_t *p_etiming_II = p_edid->etiming_II;
+	established_timings_t *p_etiming_man = p_edid->etiming_man;
+
+	unsigned char i=0;
+	unsigned char et1, et2;
+
+	PD_DEBUG("ch7036_parse_established_timing()- enter...\n");
+
+	et1 = p_edidblk[EDID_ESTABLISHED_TIMINGS_1];
+	et2 = p_edidblk[EDID_ESTABLISHED_TIMINGS_2];
+
+
+
+	for(i=0; i<8;i++) {
+	  p_etiming_I[i].is_supported = FALSE;
+	  p_etiming_II[i].is_supported = FALSE;
+	}
+
+
+
+	p_etiming_man->is_supported = FALSE;
+ 
+
+	for(i=0;i<8;i++) {
+
+
+		
+		if(et1 & 0x01) {   
+			p_etiming_I[i].is_supported = TRUE;
+	
+		}
+		
+		
+		if(et2 & 0x01) {   
+			p_etiming_II[i].is_supported = TRUE;
+	
+		}
+
+		et1 >>=  1;
+		et2 >>=  1;
+
+	}
+
+
+	
+	if (p_edidblk[EDID_MANUFACTURERS_RESERVED_TIMINGS] & 0x80) {
+
+		p_etiming_man->is_supported = TRUE;
+	 
+	}
+
+
+	return;
+
+}
+
+
+ch7036_status_t ch7036_parse_cea_edid(ch7036_device_context_t* p_ctx)
+{
+	uint8 tag, blk_size =0; 
+	uint8 index = 0;
+	
+	uint16 count;
+
+	uint8*  p_buff;
+
+
+	ch7036_edid_blk_t* p_hedid = (ch7036_edid_blk_t *)p_ctx->hedid;
+
+	unsigned char* p_edidblk = p_hedid->edidblk;
+	ch7036_attr_table_index_t* p_modes =  p_hedid->modes;
+
+	
+	PD_DEBUG("ch7036_parse_cea_edid()- enter...\n");
+
+	while (index < MAX_ATTR_LIST_SIZE )
+		p_modes[index++] = FALSE; 
+	
+		
+	if(p_edidblk[EDID_CEA_DETAILED_TIMING_DATA_OFFSET] == 0 ||  p_edidblk[EDID_CEA_DETAILED_TIMING_DATA_OFFSET] > 124) {
+		PD_DEBUG("ch7036_parse_cea_edid()- invalid data block size [%d]\n", p_edidblk[EDID_CEA_DETAILED_TIMING_DATA_OFFSET]);
+		return SS_UNSUCCESSFUL;
+	}
+
+	
+
+	PD_DEBUG("ch7036_parse_cea_edid()- CEA revision [0x%x]...\n",p_edidblk[EDID_CEA_REVISION]);
+	
+	if(p_edidblk[EDID_CEA_TAG] == 0x02 && p_edidblk[EDID_CEA_REVISION] == 0x03)
+	{
+		
+		p_buff = &(p_edidblk[EDID_CEA_DATA_BLOCK]);
+
+		
+
+		count=4 ; 
+		for(; count < p_edidblk[EDID_CEA_DETAILED_TIMING_DATA_OFFSET]; p_buff += blk_size) {
+
+			blk_size = (*p_buff) & 0x1F; 
+			tag = (*p_buff) & 0xE0; 
+
+			PD_DEBUG("ch7036_parse_cea_edid()- data type [0x%x] block_size [%d]\n", tag, blk_size);
+
+			p_buff++;
+			switch (tag) { 
+			
+				case 0x20: 
+				
+					break;
+
+				case 0x40: 
+					ch7036_parse_cea_video_data_block(blk_size, p_buff, p_hedid);
+					break;
+
+				case 0x60: 
+					break;
+
+				case 0x80: 
+					break;
+
+				default:
+					break;
+
+			}
+
+			count += (blk_size + 1); 
+					
+
+		} 
+	
+	} 
+	
+
+	PD_DEBUG("ch7036_parse_cea_edid()- exit...\n");
+
+	return SS_SUCCESS;
+
+	
+}
+
+
+void ch7036_parse_cea_video_data_block(uint8 blk_size, uint8* p_buff, ch7036_edid_blk_t* p_edid)
+{
+	uint8 i, index;
+
+	ch7036_attr_table_index_t* p_modes =  p_edid->modes;
+
+	for(i=1; i <= blk_size; i++, p_buff++ ) {
+		
+			index=0;
+
+			PD_DEBUG("ch7036_parse_cea_video_data_block()- HDMI display video code [0x%x]\n",(*p_buff) & 0x7F);
+			
+			while (index < OUT_HDMI_END) { 
+			
+				
+				if( p_modes[index] == TRUE  ) {
+					index++;
+					continue;
+				}
+				
+
+				if( ((*p_buff) & 0x7F) == hdmi_timing_table[index].fmt_index  ) {				
+					
+					p_modes[index] = TRUE;
+					PD_DEBUG("ch7036_parse_cea_video_data_block()- mode supported: global table index [%d] name [%s]...\n",index, ch7036_get_mode_name(CHANNEL_LVDS_HDMI,index) );
+					
+					if( hdmi_timing_table[index-1].fmt_index == hdmi_timing_table[index].fmt_index) {						 
+		
+						break; 
+					}
+
+				}  
+				
+				index++;
+								   
+			} 
+	} 
+
+	return;
+}
+
+
+ch7036_status_t ch7036_parse_edid(ch7036_device_context_t* p_ctx)
+{
+
+
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+
+	ch7036_edid_blk_t* p_hedid = (ch7036_edid_blk_t *)p_ctx->hedid;
+	ch7036_edid_blk_t* p_cedid = (ch7036_edid_blk_t *)p_ctx->cedid;
+
+	unsigned char* p_edidblk=0; 
+	ch7036_attr_table_index_t* p_modes = 0;
+	pd_attr_t  *p_attr = NULL ;
+	int RESET =1;
+
+	PD_DEBUG("ch7036_parse_edid()- enter...\n");
+
+	
+	//hpd checking schemes
+	if(p_ctx->hpd == 0) 
+		return SS_SUCCESS;
+
+
+	if ( 
+		( (pOutput_Info->channel & 0x02) &&  (p_ctx->hpd & 0x80) )||
+		( (pOutput_Info->channel & 0x04) &&  (p_ctx->hpd & 0x08) )
+		)
+		//parsing is already done
+		return SS_SUCCESS;
+
+	
+	switch (pOutput_Info->channel) {
+		case CHANNEL_LVDS_HDMI:
+		case CHANNEL_HDMI:
+			if(p_hedid->is_edid) { 
+				p_edidblk = p_hedid->edidblk;	
+
+				p_hedid->is_preferred_timing = (p_edidblk[EDID_FEATURE_SUPPORT] >> 1) & 0x1;
+
+				
+				if(pOutput_Info->hdmi_fmt.is_dvi_mode == 0) {
+					p_attr = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs, PD_ATTR_ID_HDMI_OUT_MODE, 0);
+					ch7036_parse_cea_edid(p_ctx);
+				}
+				else {
+					p_attr = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs, PD_ATTR_ID_DVI_OUT_MODE, 0);
+					ch7036_parse_standard_edid(p_ctx, CHANNEL_LVDS_HDMI);
+				}
+
+								
+				
+				p_ctx->hpd |= 0x80; //don't parse again next time
+							
+			}
+			else { 
+
+				PD_DEBUG("ch7036_parse_edid()- hdmi edid read failed or never done...use default...\n");
+				ch7036_set_edid_display_supported_attr( (void *)p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,p_ctx->downscaled,p_hedid->modes,RESET);
+				p_ctx->hpd &= 0x7F; //allow to enter parsing block again
+
+			}
+
+			p_modes = p_hedid->modes;
+			
+			break;
+
+		case CHANNEL_LVDS_VGA:
+		case CHANNEL_VGA:
+			if(p_cedid->is_edid) { 
+				p_edidblk = p_cedid->edidblk;
+
+				p_cedid->is_preferred_timing = (p_edidblk[EDID_FEATURE_SUPPORT] >> 1) & 0x1;
+				p_attr = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs, PD_ATTR_ID_CRT_OUT_MODE, 0);
+								
+				ch7036_parse_standard_edid(p_ctx, CHANNEL_LVDS_VGA);
+				p_ctx->hpd |= 0x08;
+			}
+			else {
+				PD_DEBUG("ch7036_parse_edid()- vga edid read failed or never done...use default...\n");
+				ch7036_set_edid_display_supported_attr( (void *)p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,p_ctx->downscaled,p_cedid->modes,RESET);
+				p_ctx->hpd &= 0x7F; //allow to enter parsing block again
+			}
+			p_modes = p_cedid->modes;
+			
+			break;
+		default:
+			//there is no separate DVI display channel, or LVDS edid reading at this time
+			//DVI display option should already be mapped to HDMI channel
+			p_edidblk = 0;
+
+	}
+
+	if(p_edidblk) {
+
+		ch7036_set_edid_display_supported_attr( (void *)p_attr,0,p_ctx->downscaled,p_modes,0);
+
+	}
+
+	PD_DEBUG("ch7036_parse_edid()- exit...\n");
+
+	return SS_SUCCESS;
+}
+
+
+void ch7036_alter_display_table(int all, void *p_table,unsigned char* p_modes, void* val,unsigned long* p_invis,unsigned char is_invis,unsigned char is_6x4)
+{
+	pd_attr_t  *p_attr;
+	unsigned long i,j;
+	
+
+	PD_DEBUG("ch7036_alter_display_table()-enter...\n");
+
+	if(all) { 
+		unsigned long* num_attrs = (unsigned long *)val;
+		for(i=0;i<3;i++) { //all 3 tables: hdmi, dvi, vga
+			
+			p_attr = pd_get_attr((pd_attr_t *)p_table, *num_attrs, PD_ATTR_ID_HDMI_OUT_MODE+i, 0);
+			if(is_6x4) 
+				p_attr++;  		
+			else 
+				p_attr= p_attr + ch7036_invis_6x4_modes_table_size[i] + 1; //1 is to skip header
+
+					
+			for(j=0; j < *p_invis;j++) { 
+				if(is_invis)
+					p_attr->flags  |= PD_ATTR_FLAG_USER_INVISIBLE;  
+				else 
+					p_attr->flags  &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+
+
+				p_attr++;
+			}
+			p_invis++; //now, grab dvi, then vga table size
+			
+
+		}
+
+	}
+	else { 
+		unsigned long id= 0,k;
+		p_attr = (pd_attr_t *)p_table;
+		id = p_attr->id;
+
+		if(is_6x4) {
+			p_attr++;  
+			j=0;
+		}
+		else {
+			p_attr= p_attr + ch7036_invis_6x4_modes_table_size[id - PD_ATTR_ID_HDMI_OUT_MODE] + 1; //point to the first 7x4 entry			
+			j= ch7036_invis_6x4_modes_table_size[id - PD_ATTR_ID_HDMI_OUT_MODE];
+		}
+		p_invis = p_invis + (id - PD_ATTR_ID_HDMI_OUT_MODE);
+		k= *p_invis + j;
+
+		for(; j < k;j++) { 
+			if(is_invis) {
+				p_attr->flags  |= PD_ATTR_FLAG_USER_INVISIBLE;  
+				p_modes[j]=FALSE;
+			} 
+	
+			p_attr++;
+		}
+			
+	}
+}
+
+
+ch7036_status_t ch7036_set_edid_display_supported_attr(void *p_table, unsigned long num_attrs, unsigned char* p_downscaled, unsigned char* p_modes, int is_reset)
+{
+
+	pd_list_attr_t *p_hdr = (pd_list_attr_t *)p_table;
+	pd_attr_t *p_entry = (pd_attr_t *)p_table; 
+	
+	uint8 i=0;
+
+#ifdef T_SHOW_EDID_DISPLAY_ATTR
+	pd_list_entry_attr_t *list_entry;
+#endif
+	
+	PD_DEBUG("ch7036_set_edid_display_supported_attr()-enter... is_reset status = [%d]\n", is_reset);
+
+	if(is_reset) {
+		while (i < MAX_ATTR_LIST_SIZE ) {
+			p_modes[i++] = TRUE; 
+		}
+
+		
+		
+		//special handling for 1080i/p @ 59Hz
+		p_entry = pd_get_attr((pd_attr_t *)p_table, num_attrs, PD_ATTR_ID_HDMI_OUT_MODE, 0);
+		p_entry++;
+		p_entry = p_entry + OUT_HDMI_1920x1080I_59;
+		p_entry->flags  |= PD_ATTR_FLAG_USER_INVISIBLE;  
+		p_entry += 2;
+		p_entry->flags  |= PD_ATTR_FLAG_USER_INVISIBLE;  
+
+	}
+	else { 
+
+		PD_DEBUG("ch7036_set_edid_display_supported_attr()-start to build edid display mode list...\n");
+	
+		p_entry++; 
+
+		for (i = 0; i < p_hdr->num_entries ; ++i,++p_entry) { 
+			if( 
+				(p_modes[i] == FALSE) ||
+				//1080 i/p @ 59 Hz
+				((p_hdr->id == PD_ATTR_ID_HDMI_OUT_MODE) && (i == OUT_HDMI_1920x1080I_59 || i== OUT_HDMI_1920x1080P_59) ) ){
+
+				p_entry->flags  |= PD_ATTR_FLAG_USER_INVISIBLE;  
+				continue;
+			}
+		
+
+			p_entry->flags  &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+
+		}
+
+
+	}
+	
+	//special handling for 8x6,7x4,6x4- 
+	ch7036_alter_display_table(is_reset,p_table,p_modes,(void *)&num_attrs,ch7036_invis_6x4_modes_table_size,*p_downscaled,1);			
+	p_downscaled++;
+	ch7036_alter_display_table(is_reset,p_table,p_modes,(void *)&num_attrs,ch7036_invis_8x6_7x4_table_size,*p_downscaled,0);
+
+#ifdef T_SHOW_EDID_DISPLAY_ATTR
+
+	list_entry = (pd_list_entry_attr_t *)(p_table);
+
+	for (i = 0,++list_entry; i < p_hdr->num_entries; ++i, ++list_entry) 
+		PD_DEBUG("ch7036 : ch7036_set_edid_display_supported_attr : \n"
+				  "list entry[%hhu]=%s, id=%lu, "
+					  "value=%lu, flags=0x%x \n",
+					  i, list_entry->name, list_entry->id,
+					  list_entry->value, (unsigned char)list_entry->flags);
+#endif
+
+
+	return SS_SUCCESS;
+}
+
+
+uint8 * ch7036_get_mode_name(uint32 channel, uint8 index)
+{
+	uint8* str = "Name String Is Not Yet Converted";
+
+	switch (channel) {
+
+		case CHANNEL_LVDS_HDMI:
+			switch (index) {
+				case OUT_HDMI_640x480P_59:
+					return ("OUT_HDMI_640x480P_59");					
+				case OUT_HDMI_640x480P_60:
+					return ("OUT_HDMI_640x480P_60");
+				case OUT_HDMI_720x480P_59:
+					return ("OUT_HDMI_720x480P_59");
+				case OUT_HDMI_720x480P_60:
+					return ("OUT_HDMI_720x480P_60");
+				case OUT_HDMI_1280x720P_59:
+					return ("OUT_HDMI_1280x720P_59");					
+				case OUT_HDMI_1280x720P_60:
+					return ("OUT_HDMI_1280x720P_60");
+				case OUT_HDMI_1920x1080I_59:
+					return ("OUT_HDMI_1920x1080I_59");					
+				case OUT_HDMI_1920x1080I_60:
+					return ("OUT_HDMI_1920x1080I_60");
+				case OUT_HDMI_1920x1080P_59:
+					return ("OUT_HDMI_1920x1080P_59");
+				case OUT_HDMI_1920x1080P_60:
+					return ("OUT_HDMI_1920x1080P_60");
+				
+
+			}
+			break;
+
+		case CHANNEL_LVDS_VGA:
+
+			switch (index) {
+
+				case OUT_CRT_640x400_85:
+					return ("OUT_CRT_640x400_85");
+
+				case OUT_CRT_640x480_60:
+					return ("OUT_CRT_640x480_60");
+				case OUT_CRT_640x480_72:
+					return ("OUT_CRT_640x480_72");
+				case OUT_CRT_640x480_75:
+					return ("OUT_CRT_640x480_75");
+				case OUT_CRT_640x480_85:
+					return ("OUT_CRT_640x480_85");
+
+				case OUT_CRT_720x400_85:
+					return ("OUT_CRT_720x400_85");
+
+				case OUT_CRT_800x600_56:
+					return ("OUT_CRT_800x600_56");
+				case OUT_CRT_800x600_60:
+					return ("OUT_CRT_800x600_60");
+				case OUT_CRT_800x600_72:
+					return ("OUT_CRT_800x600_72");
+				case OUT_CRT_800x600_75:
+					return ("OUT_CRT_800x600_75");
+					
+				case OUT_CRT_800x600_85:
+					return ("OUT_CRT_800x600_85");
+
+				case OUT_CRT_1024x768_60:
+					return ("OUT_CRT_1024x768_60");
+				case OUT_CRT_1024x768_70:
+					return ("OUT_CRT_1024x768_70");
+				case OUT_CRT_1024x768_75:
+					return ("OUT_CRT_1024x768_75");
+				case OUT_CRT_1024x768_85:
+					return ("OUT_CRT_1024x768_85");
+
+				case OUT_CRT_1152x864_75:
+					return ("OUT_CRT_1152x864_75");
+
+				case OUT_CRT_1280x768_60:
+					return ("OUT_CRT_1280x768_60");
+				case OUT_CRT_1280x768_75:
+					return ("OUT_CRT_1280x768_75");
+				case OUT_CRT_1280x768_85:
+					return ("OUT_CRT_1280x768_85");
+
+				case OUT_CRT_1280x960_60:
+					return ("OUT_CRT_1280x960_60");
+				case OUT_CRT_1280x960_85:
+					return ("OUT_CRT_1280x960_85");	
+					
+				case OUT_CRT_1280x1024_60:
+					return ("OUT_CRT_1280x1024_60");
+				case OUT_CRT_1280x1024_75:
+					return ("OUT_CRT_1280x1024_75");
+				case OUT_CRT_1280x1024_85:
+					return ("OUT_CRT_1280x1024_85");
+
+				case OUT_CRT_1360x768_60:
+					return ("OUT_CRT_1360x768_60");
+
+				case OUT_CRT_1400x1050_60:
+					return ("OUT_CRT_1400x1050_60");
+				case OUT_CRT_1400x1050_75:
+					return ("OUT_CRT_1400x1050_75");
+
+				case OUT_CRT_1440x900_60:
+					return ("OUT_CRT_1440x900_60");
+
+				case OUT_CRT_1440x1050_60:
+					return ("OUT_CRT_1440x1050_60");
+
+				case OUT_CRT_1600x900_60:
+					return ("OUT_CRT_1600x900_60");
+
+				case OUT_CRT_1600x1200_60:
+					return ("OUT_CRT_1600x1200_60");	
+					
+				case OUT_CRT_1920x1080_60:
+					return ("OUT_CRT_1920x1080_60");
+
+			}
+			break;
+
+		default: 
+			switch (index) {
+				case OUT_DVI_640x480_60:
+					return ("OUT_DVI_640x480_60"); 
+				case OUT_DVI_640x480_72:
+					return ("OUT_DVI_640x480_72"); 
+
+				case OUT_DVI_720x400_70:
+					return ("OUT_DVI_720x400_70"); 
+
+				case OUT_DVI_800x600_56:
+					return ("OUT_DVI_800x600_56");
+				case OUT_DVI_800x600_60:
+					return ("OUT_DVI_800x600_60"); 
+				case OUT_DVI_800x600_72:
+					return ("OUT_DVI_800x600_72");
+				case OUT_DVI_800x600_75:
+					return ("OUT_DVI_800x600_75");
+
+				case OUT_DVI_1024x768_60:
+					return ("OUT_DVI_1024x768_60");
+				case OUT_DVI_1024x768_70:
+					return ("OUT_DVI_1024x768_70");
+				case OUT_DVI_1024x768_75:
+					return ("OUT_DVI_1024x768_75");
+
+				case OUT_DVI_1152x864_60:
+					return ("OUT_CRT_1152x864_60");
+
+				case OUT_DVI_1280x720_60:
+					return ("OUT_DVI_1280x720_60");
+
+				case OUT_DVI_1280x800_60:
+					return ("OUT_DVI_1280x800_60");
+
+				case OUT_DVI_1280x960_60:
+					return ("OUT_DVI_1280x960_60");
+
+				case OUT_DVI_1280x1024_60:
+					return ("OUT_DVI_1280x1024_60");
+				case OUT_DVI_1280x1024_75:
+					return ("OUT_DVI_1280x1024_75");
+
+				case OUT_DVI_1360x768_60:
+					return ("OUT_DVI_1360x768_60");
+
+				case OUT_DVI_1366x768_60:
+					return ("OUT_DVI_1366x768_60");
+
+				case OUT_DVI_1400x1050_60:
+					return ("OUT_DVI_1400x1050_60");
+				case OUT_DVI_1400x1050_75:
+					return ("OUT_DVI_1400x1050_75");
+
+				case OUT_DVI_1440x900_60:
+					return ("OUT_DVI_1440x900_60");
+
+				case OUT_DVI_1440x1050_60:
+					return ("OUT_DVI_1440x1050_60");
+
+				case OUT_DVI_1600x900_60:
+					return ("OUT_DVI_1600x900_60");
+
+				case OUT_DVI_1600x1200_60:
+					return ("OUT_DVI_1600x1200_60");
+					
+				case OUT_DVI_1680x1050_60:
+					return ("OUT_DVI_1680x1050_60");
+					
+				case OUT_DVI_1920x1080_60:
+					return ("OUT_DVI_1920x1080_60");
+			}
+			break;
+			
+
+	} 
+
+
+	return str;
+
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.h
new file mode 100644
index 0000000..d031396
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_intf.h
@@ -0,0 +1,243 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_intf.h
+* @version 1.2.5
+*-----------------------------------------------------------------------------
+*/
+
+
+
+#ifndef _CH7036_INTF_H_
+#define _CH7036_INTF_H_
+
+
+#include <config.h>
+#include <igd_pd.h>
+#include <pd.h>
+#include <pd_print.h>
+
+
+#include "ch7036_typedef.h"
+#include "ch7036_iic.h"
+#include "ch7036.h"
+
+#include "ch7036_fw.h"
+
+
+#include "config_.h"
+
+#ifdef T_LINUX
+#include "asm/div64.h"
+#endif
+
+
+#define internal_lvds_context_t lvds_context_t
+
+typedef unsigned char i2c_reg_t;
+
+
+
+#define DITHER_18_TO_18					0
+#define DITHER_18_TO_24					1
+#define DITHER_24_TO_18					2
+#define DITHER_24_TO_24					3
+
+
+#define DEFAULT_POSITION				2048
+#define DEFAULT_H_1080P_POSITION		2137
+#define HDMI_DEFAULT_UNDERSCAN			15
+#define CRT_DEFAULT_UNDERSCAN			18
+#define DEFAULT_ROTATE					0
+#define DEFAULT_HFLIP					0
+#define DEFAULT_VFLIP					0
+#define DEFAULT_TEXT_ENHANCE			7
+#define DEF_PLL_REF_DLY					0
+#define DEF_PLL_REF_FBDLY				1
+#define DEF_LVDS_TXDRV_CTRL				0
+#define DITHER_ENABLE					0
+#define DITHER_BYPASS					1
+#define MODE_6x4_BYPASS					0
+#define MODE_8x6_7x4_BYPASS				1
+
+
+typedef struct {
+    i2c_reg_t dev_D0 :1; 
+    i2c_reg_t dev_D1 :1;
+    i2c_reg_t dev_D2 :1;
+    i2c_reg_t dev_D3 :1; 
+    i2c_reg_t disp_D0:1; 
+    i2c_reg_t disp_D1:1; 
+    i2c_reg_t disp_D2:1; 
+    i2c_reg_t disp_D3:1; 
+} ch7036_power_states_t;
+
+
+
+typedef unsigned char ch7036_hpd_t;
+
+#define CH7036HPD_RESERVED1						0x01  //force port status inquiry bit
+#define CH7036HPD_CRT_ATTACHED					0x02 
+#define CH7036HPD_CRT_STATUS_CHANGED			0x04 
+#define CH7036HPD_CRT_EDID_PARSING_STATUS		0x08 
+
+#define CH7036HPD_RESERVED2						0x10 //incorrect display choice bit
+#define CH7036HPD_HDVI_ATTACHED					0x20 
+#define CH7036HPD_HDVI_STATUS_CHANGED			0x40 
+#define CH7036HPD_HDVI_EDID_PARSING_STATUS		0x80 
+
+
+typedef struct _ch7036_device_context {
+
+	
+	void						*internal_lvds;	
+	DEV_CONTEXT					*p_ch7xxx_context;  
+    pd_callback_t				*p_callback;
+
+	
+	pd_attr_t					*p_ch7036_attr_table;	
+	pd_attr_t					*p_lvds_attr_table;		
+
+	unsigned long				ch7036_num_attrs; 
+	unsigned long				lvds_num_attrs;	
+	
+	
+	HDMI_OUT_MODE 				hdmi_mode_index;
+	DVI_OUT_MODE 				dvi_mode_index;
+	CRT_OUT_MODE				crt_mode_index;
+
+	
+	pd_timing_t					*p_lvds_table; 
+	
+	
+	
+	
+	pd_timing_t					native_dtd; 
+
+	
+	unsigned short				fp_width;		
+	unsigned short				fp_height;		
+
+	unsigned char				dither_select; 
+
+	
+	unsigned long				pwr_state;		
+	
+	unsigned char				init_done;		
+	
+	
+
+
+	uint8						use_firmware; 
+
+	ch7036_hpd_t				hpd;
+	uint8						man_sel_out;  //auto or manually select display output channel
+
+	uint32						prev_outchannel;  
+	
+	void*						fw;
+	void*						cedid; 
+	void*						hedid; 
+	uint8						downscaled[2]; 
+	uint8						dwnscal_bypass; //1: remove 8x6,&7x4 when downscaling, 0: keep them
+	
+	uint32						last_emsg;	
+	
+} ch7036_device_context_t;
+
+
+
+
+#if 0
+#define PD_INTERNAL_LVDS_MODULE_OPEN(name, params) lvds_open params
+#define PD_INTERNAL_LVDS_MODULE_POST_SET_MODE(name, params) lvds_post_set_mode params
+#define PD_INTERNAL_LVDS_MODULE_SET_MODE(name, params) lvds_set_mode params
+#define PD_INTERNAL_LVDS_MODULE_SET_POWER(name, params) lvds_set_power params
+#define PD_INTERNAL_LVDS_MODULE_INIT_DEVICE(name, params) lvds_init_device params
+#define PD_INTERNAL_LVDS_MODULE_CLOSE(name, params) lvds_close params
+#define PD_INTERNAL_LVDS_MODULE_GET_ATTRIBUTES(name, params) lvds_get_attrs params
+#define PD_INTERNAL_LVDS_MODULE_SET_ATTRIBUTES(name, params) lvds_set_attrs params
+#define PD_INTERNAL_LVDS_MODULE_GET_TIMING_LIST(name, params) lvds_get_timing_list params
+
+#endif
+
+
+
+#define PD_INTERNAL_LVDS_MODULE_OPEN(name, params) internal_lvds_open params
+#define PD_INTERNAL_LVDS_MODULE_POST_SET_MODE(name, params) internal_lvds_post_set_mode params
+#define PD_INTERNAL_LVDS_MODULE_SET_MODE(name, params) internal_lvds_set_mode params
+#define PD_INTERNAL_LVDS_MODULE_SET_POWER(name, params) internal_lvds_set_power params
+#define PD_INTERNAL_LVDS_MODULE_GET_POWER(name, params) internal_lvds_get_power params
+#define PD_INTERNAL_LVDS_MODULE_INIT_DEVICE(name, params) internal_lvds_init_device params
+#define PD_INTERNAL_LVDS_MODULE_CLOSE(name, params) internal_lvds_close params
+#define PD_INTERNAL_LVDS_MODULE_GET_ATTRIBUTES(name, params) internal_lvds_get_attrs params
+#define PD_INTERNAL_LVDS_MODULE_SET_ATTRIBUTES(name, params) internal_lvds_set_attrs params
+#define PD_INTERNAL_LVDS_MODULE_GET_TIMING_LIST(name, params) internal_lvds_get_timing_list params
+#define PD_INTERNAL_LVDS_MODULE_SAVE(name, params) internal_lvds_save params
+#define PD_INTERNAL_LVDS_MODULE_RESTORE(name, params) internal_lvds_restore params
+#define PD_INTERNAL_LVDS_MODULE_GET_PORT_STATUS(name, params) internal_lvds_get_port_status params
+
+
+
+
+ch7036_status_t ch7036_device_prepare(ch7036_device_context_t* p_ctx);
+ch7036_status_t ch7036_device_config(ch7036_device_context_t* p_ctx);
+ch7036_status_t ch7036_device_start(ch7036_device_context_t* p_ctx);
+ch7036_status_t ch7036_device_set_power(ch7036_device_context_t* p_ctx,unsigned long channel);
+
+
+void ch7036_set_input_timing_info(ch7036_device_context_t *p_ctx,INPUT_INFO* pInput_Info);
+void ch7036_set_output_timing_info (ch7036_device_context_t *p_ctx, OUTPUT_INFO* pOutput_Info);
+void ch7036_set_prefer_timing_info(ch7036_device_context_t *p_ctx,PREFER_INFO* pPrefer_Info);
+
+
+
+ch7036_status_t ch7036_load_firmware(ch7036_device_context_t* p_ctx);
+ch7036_status_t ch7036_get_attached_device(ch7036_device_context_t* p_ctx);
+ch7036_status_t ch7036_read_edid(ch7036_device_context_t* p_ctx, uint32 channel);
+ch7036_status_t ch7036_get_hdvi_display_modes_supported(ch7036_device_context_t* p_ctx);
+
+
+uint8 * ch7036_get_mode_name(uint32 channel, uint8 index);
+//void ch7036_reset_edid_supported_modes(unsigned char *p_modes);
+ch7036_status_t ch7036_parse_edid(ch7036_device_context_t* p_ctx);
+ch7036_status_t ch7036_parse_cea_edid(ch7036_device_context_t* p_ctx);
+void ch7036_parse_cea_video_data_block(uint8 blk_size, uint8* p_buff, ch7036_edid_blk_t* p_edid);
+ch7036_status_t ch7036_set_edid_display_supported_attr(void *p_table, unsigned long num_attrs, unsigned char* p_downscaled, unsigned char* p_modes,int is_reset);
+void ch7036_alter_display_table(int all, void *p_table,unsigned char* p_modes, void* val,unsigned long* p_invis,unsigned char is_invis,unsigned char is_6x4);
+
+ch7036_status_t ch7036_parse_standard_edid(ch7036_device_context_t* p_ctx, uint32 channel);
+
+void ch7036_parse_standard_timing(ch7036_edid_blk_t* p_edid, unsigned char* p_addtional_st);
+
+void ch7036_parse_established_timing(ch7036_device_context_t* p_ctx, ch7036_edid_blk_t* p_edid);
+void ch7036_parse_detailed_descriptor_blocks(ch7036_device_context_t* p_ctx, ch7036_edid_blk_t* p_edid);
+void ch7036_parse_detailed_timing(OUT_FMT *p_dt, unsigned char* p_ebuf);
+
+
+void ch7036_reset_mcu(DEV_CONTEXT* p_ch_ctx);
+void ch7036_reset_datapath(DEV_CONTEXT* p_ch_ctx);
+void ch7036_reset(ch7036_device_context_t* p_ctx);
+
+extern uint32 GetLastErrorMessage(void);
+
+#endif  
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_pm.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_pm.c
new file mode 100755
index 0000000..d9f51bb
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_pm.c
@@ -0,0 +1,492 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_pm.c
+* @version 1.2.4
+*-----------------------------------------------------------------------------
+*/
+
+
+
+#include "ch7036_intf.h"
+
+
+
+void ch7036_set_power_lvds(DEV_CONTEXT* pDevContext);
+void ch7036_set_power_hdmi(DEV_CONTEXT* pDevContext);
+void ch7036_set_power_crt(DEV_CONTEXT* pDevContext);
+
+void ch7036_set_power_lvds(DEV_CONTEXT* p_ch7xxx_context)
+{
+
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	uint8 reg=0x00;
+
+	PD_DEBUG("ch7036: ch7036_set_power_lvds()-enter...channel [%lu]\n", pOutput_Info->channel);
+	if(pOutput_Info->channel & CHANNEL_LVDS)
+	{
+		
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01);               
+		reg = I2CRead(p_ch7xxx_context,0x1A);
+		reg = reg & 0xEF;                   
+		I2CWrite(p_ch7xxx_context,0x1A, reg); 
+		
+		reg = I2CRead(p_ch7xxx_context,0x11);
+		reg = reg & 0xEF;                   
+	  if(((pOutput_Info->channel & CHANNEL_HDMI)==0x00)&&
+		 ((pOutput_Info->channel & CHANNEL_VGA) == 0x00))
+	    {
+			
+			reg = reg | 0x80;                   
+	    }
+		 I2CWrite(p_ch7xxx_context,0x11, reg);		
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x04);               
+		reg = I2CRead(p_ch7xxx_context,0x66);
+		reg = reg & 0xFD;                   
+		I2CWrite(p_ch7xxx_context,0x66, reg);
+
+        reg = I2CRead(p_ch7xxx_context,0x64);
+		reg = reg & 0xFE;                   
+		I2CWrite(p_ch7xxx_context,0x64, reg);
+
+        reg = I2CRead(p_ch7xxx_context,0x63);
+		reg = reg & 0x03;                   
+		I2CWrite(p_ch7xxx_context,0x63, reg);
+
+        I2CWrite(p_ch7xxx_context,0x03, 0x00);               
+		reg = I2CRead(p_ch7xxx_context,0x0A);
+		reg = reg & 0xF7;                   
+        I2CWrite(p_ch7xxx_context,0x0A, reg);		
+
+		
+		I2CWrite(p_ch7xxx_context,0x4E, I2CRead(p_ch7xxx_context,0x4E) | 0x80 );
+
+		
+		if((pOutput_Info->channel & CHANNEL_HDMI) == 0x00 && (pOutput_Info->channel & CHANNEL_VGA) == 0x00){
+
+			
+		 I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		 reg = I2CRead(p_ch7xxx_context,0x07);
+		 reg = reg | 0x02;                  
+		 I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x04);
+		 reg = I2CRead(p_ch7xxx_context,0x54);
+		 reg = reg | 0x10;                 
+		 I2CWrite(p_ch7xxx_context,0x54, reg);
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x02);             
+		 reg = I2CRead(p_ch7xxx_context,0x16);
+		 reg = reg | 0x08; 
+		
+	
+		 I2CWrite(p_ch7xxx_context,0x16, reg);
+
+		}
+		
+		
+				
+	}
+	
+	if((pOutput_Info->channel & CHANNEL_LVDS) == 0x00)
+	{
+		
+		
+
+        I2CWrite(p_ch7xxx_context,0x03, 0x00);   
+
+		I2CWrite(p_ch7xxx_context,0x4E, I2CRead(p_ch7xxx_context,0x4E) & 0x7F );
+
+		reg = I2CRead(p_ch7xxx_context,0x0A);
+		reg = reg | 0x08;                   
+        I2CWrite(p_ch7xxx_context,0x0A, reg);
+
+		if((pOutput_Info->channel & CHANNEL_HDMI) == 0x00 && (pOutput_Info->channel & CHANNEL_VGA)==0x00)
+		{
+			
+			I2CWrite(p_ch7xxx_context,0x03, 0x04);               
+			reg = I2CRead(p_ch7xxx_context,0x63);
+			reg = reg | 0xFC;                   
+			I2CWrite(p_ch7xxx_context,0x63, reg);
+
+			reg = I2CRead(p_ch7xxx_context,0x64);
+			reg = reg | 0x01;                   
+			I2CWrite(p_ch7xxx_context,0x64, reg);
+		}
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x04); 
+		reg = I2CRead(p_ch7xxx_context,0x66);
+		reg = reg | 0x02;                   
+		I2CWrite(p_ch7xxx_context,0x66, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01);               
+		reg = I2CRead(p_ch7xxx_context,0x11);
+		reg = reg | 0x10;                   
+		I2CWrite(p_ch7xxx_context,0x11, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x1A);
+		reg = reg | 0x10;                   
+		I2CWrite(p_ch7xxx_context,0x1A, reg);
+
+	}
+
+
+}
+
+void ch7036_set_power_hdmi(DEV_CONTEXT* p_ch7xxx_context)
+{
+
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	INPUT_INFO* pInput_Info = p_ch7xxx_context->pInput_Info;
+	uint8 reg=0x00;
+
+	PD_DEBUG("ch7036: ch7036_set_power_hdmi()-enter...channel [%lu]\n", pOutput_Info->channel);
+   if(pOutput_Info->channel  & CHANNEL_HDMI)
+   {
+	   
+		
+        I2CWrite(p_ch7xxx_context,0x03, 0x04);  
+		reg = I2CRead(p_ch7xxx_context,0x52);   
+		reg = reg & 0xEF;      
+		I2CWrite(p_ch7xxx_context,0x52, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x0A);
+		reg = reg & 0xDF;      
+		I2CWrite(p_ch7xxx_context,0x0A, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xBF;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xF7;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xEF;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+
+		 
+	   I2CWrite(p_ch7xxx_context,0x03, 0x01);               
+		reg = I2CRead(p_ch7xxx_context,0x11);
+		reg = reg & 0x7F;                   
+	    I2CWrite(p_ch7xxx_context,0x11, reg);
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		 reg = I2CRead(p_ch7xxx_context,0x07);
+		 reg = reg & 0xFD;                  
+		 I2CWrite(p_ch7xxx_context,0x07, reg);
+		  
+		 I2CWrite(p_ch7xxx_context,0x03, 0x04);  
+		reg = I2CRead(p_ch7xxx_context,0x54);
+		reg = reg & 0xEF;
+		I2CWrite(p_ch7xxx_context,0x54, reg);
+
+	  
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg & 0xFB;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xFB;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01);  
+		reg = I2CRead(p_ch7xxx_context,0x0E);
+		reg = reg & 0x7F;      
+		I2CWrite(p_ch7xxx_context,0x0E, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		
+		if(pInput_Info->audio_type == AUDIO_I2S){
+          
+		   reg = reg & 0xBF;   
+		   reg = reg | 0x01;   
+		}else{
+		  
+           reg = reg | 0x40;   
+		   reg = reg & 0xFE;   
+		}
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01);  
+		reg = I2CRead(p_ch7xxx_context,0x16);
+		reg = reg & 0xF7;      
+		I2CWrite(p_ch7xxx_context,0x16, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x16);
+		reg = reg & 0xFE;      
+		I2CWrite(p_ch7xxx_context,0x16, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x08);  
+		reg = reg & 0x0F;      
+		I2CWrite(p_ch7xxx_context,0x08, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg & 0xF7;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg | 0x10;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xFE;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+				
+
+   }else {
+
+	   
+	   if((pOutput_Info->channel  & CHANNEL_VGA) == 0x00){
+
+
+        I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x0A);
+		reg = reg | 0x20;      
+		I2CWrite(p_ch7xxx_context,0x0A, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x40;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x08;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x10;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg | 0x04;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01);               
+		 reg = I2CRead(p_ch7xxx_context,0x11);
+		 reg = reg | 0x80;                   
+		 I2CWrite(p_ch7xxx_context,0x11, reg);	
+
+		
+		 I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		 reg = I2CRead(p_ch7xxx_context,0x07);
+		 reg = reg | 0x02;                  
+		 I2CWrite(p_ch7xxx_context,0x07, reg);
+		  
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x04);
+		 reg = I2CRead(p_ch7xxx_context,0x54);
+		 reg = reg | 0x10;                 
+		 I2CWrite(p_ch7xxx_context,0x54, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		 reg = I2CRead(p_ch7xxx_context,0x09);
+		 reg = reg | 0x04;      
+		 I2CWrite(p_ch7xxx_context,0x09, reg);
+    
+		}
+		
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg | 0x40;   
+		reg = reg | 0x01;   
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+				
+
+		if((pOutput_Info->channel  & CHANNEL_LVDS) == 0x00)
+		{
+		 I2CWrite(p_ch7xxx_context,0x03, 0x01);  
+		 reg = I2CRead(p_ch7xxx_context,0x16);
+		 reg = reg | 0x08;      
+		 I2CWrite(p_ch7xxx_context,0x16, reg);
+		}
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x01); 
+		reg = I2CRead(p_ch7xxx_context,0x16);
+		reg = reg | 0x01;      
+		I2CWrite(p_ch7xxx_context,0x16, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x08);  
+		reg = reg | 0xF0;      
+		I2CWrite(p_ch7xxx_context,0x08, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg | 0x08;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg | 0x10;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x01;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+	
+
+   }
+		   
+}
+
+void ch7036_set_power_crt(DEV_CONTEXT* p_ch7xxx_context)
+{
+	
+
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	uint8 reg=0x00; 
+
+	PD_DEBUG("ch7036: ch7036_set_power_crt()-enter...channel [%lu]\n", pOutput_Info->channel);
+	if(pOutput_Info->channel & CHANNEL_VGA)
+	 {
+		 
+	     
+	   I2CWrite(p_ch7xxx_context,0x03, 0x01);               
+		reg = I2CRead(p_ch7xxx_context,0x11);
+		reg = reg & 0x7F;                   
+	    I2CWrite(p_ch7xxx_context,0x11, reg);
+  
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		reg = I2CRead(p_ch7xxx_context,0x0A);
+		reg = reg & 0xDF;      
+		I2CWrite(p_ch7xxx_context,0x0A, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xBF;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xF7;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xEF;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg & 0xF9;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x04);  
+		reg = I2CRead(p_ch7xxx_context,0x54);
+		reg = reg & 0xEF;
+		I2CWrite(p_ch7xxx_context,0x54, reg);
+
+		
+		I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+		reg = I2CRead(p_ch7xxx_context,0x08);
+		reg = reg & 0xF1;      
+		I2CWrite(p_ch7xxx_context,0x08, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg & 0xFB;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+	}else if((pOutput_Info->channel & CHANNEL_VGA) == 0x00){ 
+
+		
+       
+		I2CWrite(p_ch7xxx_context,0x03, 0x00); 
+	   
+		 reg = I2CRead(p_ch7xxx_context,0x08);
+		reg = reg | 0x0E;     
+		I2CWrite(p_ch7xxx_context,0x08, reg);
+
+
+
+		if((pOutput_Info->channel & CHANNEL_HDMI)==0x00){
+
+			
+
+	     I2CWrite(p_ch7xxx_context,0x03, 0x01);               
+		 reg = I2CRead(p_ch7xxx_context,0x11);
+		 reg = reg | 0x80;                   
+	     I2CWrite(p_ch7xxx_context,0x11, reg);
+
+		  I2CWrite(p_ch7xxx_context,0x03, 0x00);
+		 reg = I2CRead(p_ch7xxx_context,0x07);
+		 reg = reg | 0x02;                  
+		 I2CWrite(p_ch7xxx_context,0x07, reg);
+		  
+
+		 I2CWrite(p_ch7xxx_context,0x03, 0x04);
+		 reg = I2CRead(p_ch7xxx_context,0x54);
+		 reg = reg | 0x10;                 
+		 I2CWrite(p_ch7xxx_context,0x54, reg);
+
+		 
+        I2CWrite(p_ch7xxx_context,0x03, 0x00);  
+		reg = I2CRead(p_ch7xxx_context,0x0A);
+		reg = reg | 0x20;      
+		I2CWrite(p_ch7xxx_context,0x0A, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x40;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x08;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x09);
+		reg = reg | 0x10;      
+		I2CWrite(p_ch7xxx_context,0x09, reg);
+
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg | 0x04;      
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		
+		 reg = I2CRead(p_ch7xxx_context,0x09);
+		 reg = reg | 0x04;      
+		 I2CWrite(p_ch7xxx_context,0x09, reg);
+	   }
+	} 
+
+	return;
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.c
new file mode 100644
index 0000000..7d4e496
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.c
@@ -0,0 +1,1548 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_port.c
+* @version 1.2.5
+*-----------------------------------------------------------------------------
+*/
+
+
+#include <linux/kernel.h>
+
+#include "ch7036_port.h"
+
+
+#ifdef T_LINUX
+	#include "lvds/lvds.h"
+#else
+	#include "lvds.h"
+#endif
+
+
+
+static pd_version_t  g_ch7036_version = {1, 2, 5, 0};
+static unsigned long g_ch7036_dab_list[] = {0xEC,PD_DAB_LIST_END};
+
+
+void ch7036_update_position(ch7036_device_context_t *p_ctx, OUTPUT_INFO* pOutput_Info);
+int ch7036_initialize_device(ch7036_device_context_t *p_ctx);
+
+
+static pd_driver_t	 g_ch7036_drv = {
+	PD_SDK_VERSION,
+	"Chrontel CH7036 Port Driver",
+	0,
+	&g_ch7036_version,
+	PD_DISPLAY_LVDS_INT,
+	PD_FLAG_UP_SCALING,
+	g_ch7036_dab_list,
+	100,
+	ch7036_validate,
+	ch7036_open,
+	ch7036_init_device,
+	ch7036_close,
+	ch7036_set_mode,
+	ch7036_post_set_mode,
+	ch7036_set_attributes,
+	ch7036_get_attributes,
+	ch7036_get_timing_list,
+	ch7036_set_power,
+	ch7036_get_power,
+	ch7036_save,
+	ch7036_restore,
+	ch7036_get_port_status
+};
+
+
+extern established_timings_t et_I[8];
+extern established_timings_t et_II[8];
+extern established_timings_t et_man;
+
+
+int PD_MODULE_INIT(ch7036_init, (void *handle))
+{
+	int status;
+
+	PD_DEBUG("ch7036: ch7036_init()\n");
+
+	status = pd_register(handle, &g_ch7036_drv);
+	if (status != PD_SUCCESS) {
+		PD_DEBUG("ch7036: Error ! ch7036_init: pd_register() failed with "
+				  "status=%#x\n", status);  
+	}
+	return status;
+}
+
+
+
+int PD_MODULE_EXIT(ch7036_exit, (void))
+{
+	PD_DEBUG("ch7036: ch7036_exit()\n");
+
+	return (PD_SUCCESS);
+} 
+
+
+unsigned long ch7036_validate(unsigned long cookie)
+{
+	PD_DEBUG("ch7036: ch7036_validate()\n");
+	
+	return cookie;
+}
+
+
+int ch7036_open(pd_callback_t *p_callback, void **pp_context)
+{
+	uint8 device_ID;
+	ch7036_device_context_t* p_ctx;
+	DEV_CONTEXT* p_ch7xxx_context;
+
+	ch7036_edid_blk_t *p_edid1, *p_edid2;
+
+		
+	int ret;
+
+
+	
+	PD_DEBUG("ch7036: ch7036_open()- enter- ch7036 pd release- major [%x] minor [%x] patch [%x]\n",
+		g_ch7036_version.major,g_ch7036_version.minor,g_ch7036_version.patch);
+	
+	ret = PD_INTERNAL_LVDS_MODULE_OPEN(ch7036_lvds_open,(p_callback, pp_context));
+	if ( ret != PD_SUCCESS)
+	{
+
+
+		PD_ERROR("ch7036: ch7036_open: EXIT#1\n");
+		return ret;	
+	}
+	
+	p_ctx = pd_malloc(sizeof(ch7036_device_context_t));
+	if (p_ctx == NULL) {
+		PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed\n");
+		goto exit6;
+	}
+
+	pd_memset(p_ctx, 0, sizeof(ch7036_device_context_t));
+	
+	/* per EMGD request */
+	p_edid1 = (ch7036_edid_blk_t *)p_ctx->cedid;
+	p_edid2 = (ch7036_edid_blk_t *)p_ctx->hedid;
+
+
+	p_ctx->fw = (FW7036_CFG *)(pd_malloc(sizeof(FW7036_CFG)));
+	
+	if (p_ctx->fw == NULL) {
+		PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating FW7036_CFG struct\n");
+		goto exit5;
+	}
+
+#if 0	
+	ret = PD_INTERNAL_LVDS_MODULE_OPEN(ch7036_lvds_open,(p_callback, pp_context));
+	if ( ret != PD_SUCCESS)
+	{
+		pd_free(p_ctx->fw);
+		pd_free(p_ctx);		
+		return ret;	
+	}
+#endif
+	
+	p_ctx->internal_lvds = *pp_context; 
+
+	p_ctx->p_callback = p_callback;
+
+#ifdef LVDS_ONLY
+	*pp_context = (void *)p_ctx;
+	return (PD_SUCCESS);
+#endif
+
+	
+	
+	p_ctx->p_ch7xxx_context = pd_malloc(sizeof(DEV_CONTEXT));
+	if (p_ctx->p_ch7xxx_context == NULL) {
+		PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating DEV_CONTEXT struct");
+		goto exit4;
+	}
+	
+	p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	
+	p_ch7xxx_context->pd_context = (void *)p_ctx;
+
+
+	
+	I2CWrite(p_ch7xxx_context,0x03, 0x04); 
+	
+	device_ID = I2CRead(p_ch7xxx_context,0x50); 	
+  
+	PD_DEBUG("ch7036: ch7036_open()- read device ID= 0x%.2X\n", device_ID);
+
+
+	if(device_ID != 0x56)
+	{
+		PD_DEBUG("ch7036: ch7036_open()- device is NOT found...\n");
+
+		if(p_ch7xxx_context->pd_context)
+			p_ch7xxx_context->pd_context=NULL;
+		pd_free(p_ch7xxx_context);
+		if(p_ctx->internal_lvds) {
+			p_ctx->internal_lvds=NULL;
+
+		}
+		pd_free(p_ctx->fw);
+		pd_free(p_ctx);
+		return PD_ERR_NODEV;
+		
+	}
+	else
+	{
+		PD_DEBUG("ch7036: ch7036_open()- ch7036 device is found...\n");
+
+		
+	}
+
+	p_ch7xxx_context->DeviceID = device_ID;
+
+	ch7036_reset(p_ctx);
+	pd_usleep(50); 	
+		
+	if (ch7036_load_firmware(p_ctx) != SS_SUCCESS)   { 
+		p_ctx->use_firmware =0;
+		p_ctx->cedid = NULL;
+		p_ctx->hedid = NULL;
+		
+	}
+	else {
+		p_ctx->use_firmware =1;
+
+
+		p_ctx->cedid = (ch7036_edid_blk_t *)pd_malloc(sizeof(ch7036_edid_blk_t));
+
+		if (p_ctx->cedid == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating ch7036_edid_blk_t struct for crt");
+			goto exit3;
+		}
+
+		p_edid1 = (ch7036_edid_blk_t *)p_ctx->cedid ;
+
+		p_edid1->etiming_I = (established_timings_t *)pd_malloc(8 * sizeof(established_timings_t));
+		if (p_edid1->etiming_I == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating established_timings_t struct I for crt");
+			pd_free(p_ctx->cedid);
+			goto exit3;
+		}
+		p_edid1->etiming_II = (established_timings_t *)pd_malloc(8 * sizeof(established_timings_t));
+		if (p_edid1->etiming_II == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating established_timings_t struct II for crt");
+			pd_free(p_edid1->etiming_I);
+			pd_free(p_ctx->cedid);
+			goto exit3;
+		}
+
+		p_ctx->hedid = (ch7036_edid_blk_t *)pd_malloc(sizeof(ch7036_edid_blk_t));
+
+		if (p_ctx->hedid == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating ch7036_edid_blk_t struct for hdvi");
+			pd_free(p_edid1->etiming_I);
+			pd_free(p_edid1->etiming_II);
+			pd_free(p_ctx->cedid);
+
+			goto exit3;
+		}
+
+		p_edid2 = (ch7036_edid_blk_t *)p_ctx->hedid ;
+
+		p_edid2->etiming_I = (established_timings_t *)pd_malloc(8 * sizeof(established_timings_t));
+		if (p_edid2->etiming_I == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating established_timings_t struct I for crt");
+			pd_free(p_edid1->etiming_I);
+			pd_free(p_edid1->etiming_II);
+			pd_free(p_ctx->cedid);
+			pd_free(p_ctx->hedid);
+
+			goto exit3;
+		}
+
+		p_edid2->etiming_II = (established_timings_t *)pd_malloc(8 * sizeof(established_timings_t));
+
+		if (p_edid2->etiming_II == NULL) {
+			PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating established_timings_t struct for hdvi");
+			pd_free(p_edid1->etiming_I);
+			pd_free(p_edid1->etiming_II);
+			pd_free(p_ctx->cedid);
+			pd_free(p_edid2->etiming_I);
+
+			pd_free(p_ctx->hedid);
+
+			goto exit3;
+		}
+
+
+	}
+
+	
+	pd_memset(p_edid1->etiming_I, 0, 8 *
+			sizeof(established_timings_t));
+	pd_memset(p_edid1->etiming_II, 0, 8 *
+			sizeof(established_timings_t));
+
+	pd_memset(p_edid2->etiming_I, 0, 8 *
+			sizeof(established_timings_t));
+	pd_memset(p_edid2->etiming_II, 0, 8 *
+			sizeof(established_timings_t));
+
+
+
+
+
+
+	
+	p_ch7xxx_context->pInput_Info = pd_malloc(sizeof(INPUT_INFO));
+	if (p_ch7xxx_context->pInput_Info == NULL) {
+		PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating INPUT_INFO struct");
+
+		goto exit23; 
+	}
+
+	p_ch7xxx_context->pOutput_Info = pd_malloc(sizeof(OUTPUT_INFO));
+	if (p_ch7xxx_context->pOutput_Info == NULL) {
+		PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating OUTPUT_INFO struct");
+
+		goto exit2;
+	}
+
+	p_ch7xxx_context->pPrefer_Info = pd_malloc(sizeof(PREFER_INFO));
+	if (p_ch7xxx_context->pPrefer_Info == NULL) {
+		PD_ERROR("ch7036: Error ! ch7036_open: pd_malloc() failed allocating PREFER_INFO struct");
+		
+		goto exit1;
+	}
+
+	
+
+
+	p_ctx->p_ch7036_attr_table = NULL;
+	
+	if( ch7036_init_attribute_table(p_ctx, NULL) == SS_MEM_ALLOC_ERR)
+	{
+		pd_free(p_ch7xxx_context->pPrefer_Info);
+		goto exit1;
+	}
+
+	
+	ch7036_initialize_device(p_ctx);
+	
+	g_ch7036_drv.type = PD_DISPLAY_LVDS_INT; 
+
+	
+	*pp_context = (void *)p_ctx;
+
+	PD_DEBUG("ch7036: ch7036_open: EXIT w/ SUCCESS...ch7036 pd release- major [%x] minor [%x] patch [%x]\n",
+		g_ch7036_version.major,g_ch7036_version.minor,g_ch7036_version.patch);
+	return (PD_SUCCESS);
+
+exit1:
+	pd_free(p_ch7xxx_context->pOutput_Info);
+exit2:
+	pd_free(p_ch7xxx_context->pInput_Info);
+
+exit23:
+	pd_free(p_edid1->etiming_I);
+	pd_free(p_edid1->etiming_II);
+	pd_free(p_ctx->cedid);
+	pd_free(p_edid2->etiming_I);
+	pd_free(p_edid2->etiming_II);
+	pd_free(p_ctx->hedid);
+
+exit3:
+	p_ch7xxx_context->pd_context=NULL; 
+	pd_free(p_ch7xxx_context);
+exit4:
+
+	p_ctx->internal_lvds = NULL;
+	pd_free(p_ctx->fw);
+exit5:
+	pd_free(p_ctx);
+exit6:
+	PD_ERROR("ch7036: ch7036_open: EXIT- PD_ERR_NOMEM\n");
+	return PD_ERR_NOMEM;
+}
+
+
+int ch7036_init_device(void *p_context)
+{
+	ch7036_device_context_t* p_ctx  = (ch7036_device_context_t*)p_context;
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	int ret;
+
+	PD_DEBUG("ch7036: ch7036_init_device()-enter\n");
+
+	p_ctx->init_done = 1;
+	ret= PD_INTERNAL_LVDS_MODULE_INIT_DEVICE(ch7036_lvds_init_device, (p_ctx->internal_lvds));
+
+	p_ctx->prev_outchannel = pOutput_Info->channel;
+	PD_DEBUG("ch7036: ch7036_init_device()-p_ctx->prev_outchannel = pOutput_Info->channel = [0x%x]\n",pOutput_Info->channel);
+
+	PD_DEBUG("ch7036: ch7036_init_device()-exit\n");
+	return ret;
+	
+}
+
+
+
+int ch7036_set_mode(void *p_context, pd_timing_t *p_mode, unsigned long flags)
+{
+
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t*)p_context;
+	
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	INPUT_INFO* pInput_Info = p_ch7xxx_context->pInput_Info;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = p_ch7xxx_context->pPrefer_Info;
+	int ret, channel_on=0;
+
+	
+	PD_DEBUG("ch7036: ch7036_set_mode()-enter\n");
+
+
+	if (!p_ctx || !p_mode) {
+		return (PD_ERR_NULL_PTR);
+	}
+
+	if (p_ch7xxx_context->DeviceID != 0x56)
+	{
+		return (PD_ERR_NULL_PTR);
+	}
+
+	PD_DEBUG("ch7036_set_mode: requested width = %u height = %u\n",
+		p_mode->width, p_mode->height);
+
+#ifndef LVDS_ONLY	
+	if (
+		(p_ctx->fp_width && (p_mode->width > p_ctx->fp_width)) ||
+		(p_ctx->fp_height && (p_mode->height > p_ctx->fp_height))
+		) {
+		return PD_ERR_MODE_NOTSUPP;
+	}
+	
+	if( (p_ctx->fp_width == 640) && (p_ctx->fp_height == 480) ) {
+		p_ctx->downscaled[MODE_6x4_BYPASS] = 0;
+	}
+	else { 
+		p_ctx->downscaled[MODE_6x4_BYPASS] = 1;
+		if( (!p_ctx->dwnscal_bypass) || ((p_ctx->fp_width <= 800) && (p_ctx->fp_height <= 600) )  ) 
+			p_ctx->downscaled[MODE_8x6_7x4_BYPASS] = 0;
+		else
+			p_ctx->downscaled[MODE_8x6_7x4_BYPASS] = 1;
+
+	}
+
+	if(pOutput_Info->channel  == CHANNEL_LVDS_HDMI_VGA_OFF) { 
+		
+		pOutput_Info->channel = p_ctx->prev_outchannel; //restore output channel before temp. power down
+		channel_on =1;
+	}
+	
+	
+	if (pOutput_Info->channel == CHANNEL_LVDS) {  
+
+		pOutput_Info->channel = CHANNEL_LVDS_HDMI;	//force both channel on before setting in/out timing
+
+	}
+
+
+	ch7036_set_input_timing_info(p_ctx,pInput_Info);
+	ch7036_set_output_timing_info(p_ctx, pOutput_Info);
+	ch7036_set_prefer_timing_info(p_ctx,pPrefer_Info);
+
+	
+	if(ch7036_device_prepare(p_ctx)== SS_UNSUCCESSFUL)
+	{
+		PD_DEBUG("ch7036_set_mode: ch7036_device_prepare()- NOT SUCCESS... ERROR CODE [%lu]\n", p_ctx->last_emsg);		
+		return PD_ERR_UNSUCCESSFUL;  
+	}
+
+	
+	ch7036_update_position(p_ctx, pOutput_Info);
+
+	if(ch7036_device_config(p_ctx) == SS_UNSUCCESSFUL)
+	{
+		PD_DEBUG("ch7036_set_mode: ch7036_device_config()- NOT SUCCESS...ERROR CODE [%lu]\n", p_ctx->last_emsg);
+		return PD_ERR_UNSUCCESSFUL; 
+	}
+
+	if(channel_on) {
+		
+		pOutput_Info->channel = CHANNEL_LVDS_HDMI_VGA_OFF; //now, power down if it's tmp. powered up
+		
+	}
+
+#endif
+
+	ret = PD_INTERNAL_LVDS_MODULE_SET_MODE(ch7036_lvds_set_mode,(p_ctx->internal_lvds,p_mode,flags));
+	if(ret != PD_SUCCESS)
+		return ret; 
+
+	
+	return PD_SUCCESS;
+}
+//thua- 10/24/11- when user switches back and forth from VGA to HDMI or vice versa
+//need to set h/v pos. accordingly
+void ch7036_update_position(ch7036_device_context_t *p_ctx, OUTPUT_INFO* pOutput_Info)
+{
+	
+	PD_DEBUG("ch7036: ch7036_update_position()- enter\n");
+
+	
+	if(pOutput_Info->channel & CHANNEL_HDMI) {	 	
+		pOutput_Info->h_position = DEFAULT_POSITION; 
+		pOutput_Info->v_position = DEFAULT_POSITION;
+				
+	}
+
+	else { //pOutput_Info->channel & CHANNEL_VGA; note that CHANNEL_LVDS would never come here
+		
+			pOutput_Info->h_position = (uint16)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_HPOSITION,
+					PD_GET_ATTR_LIST)->current_value;
+
+			pOutput_Info->v_position = (uint16)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_VPOSITION,
+					PD_GET_ATTR_LIST)->current_value;
+		
+	}
+
+	PD_DEBUG("ch7036: ch7036_update_position()- h pos [%d]\n",pOutput_Info->h_position);
+	PD_DEBUG("ch7036: ch7036_update_position()- v pos [%d]\n",pOutput_Info->v_position);
+
+	return;
+}
+
+
+int ch7036_post_set_mode(void *p_context, pd_timing_t *p_mode,
+						  unsigned long flags)
+{
+	
+
+	ch7036_device_context_t* p_ctx  = (ch7036_device_context_t*)p_context;
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	int ret;
+	
+	
+	
+	
+
+	if (!p_ctx || !p_mode ) {
+		return (PD_ERR_NULL_PTR);
+	}
+
+	if (p_ch7xxx_context->DeviceID != 0x56)
+	{
+		return (PD_ERR_NULL_PTR);
+	}
+
+#ifndef LVDS_ONLY
+
+	
+		
+	ch7036_set_output_channel(p_ctx, p_ctx->prev_outchannel);	
+	PD_DEBUG("ch7036_post_set_mode- now, current pOutput_Info->channel is [%x]\n",pOutput_Info->channel);
+
+	if(ch7036_device_start(p_ctx) == SS_UNSUCCESSFUL)
+	{
+		PD_DEBUG("ch7036_post_set_mode: ch7036_device_start()- NOT SUCCESS\n");
+		return PD_ERR_UNSUCCESSFUL; 
+	}
+
+	
+	
+
+
+#endif
+
+	
+	ret = PD_INTERNAL_LVDS_MODULE_POST_SET_MODE(ch7036_lvds_post_set_mode,(p_ctx->internal_lvds,p_mode,flags));
+
+#ifndef LVDS_ONLY	
+	if(ret != PD_SUCCESS)
+		return ret; 
+	else
+	{
+		
+		ch7036_reset_datapath(p_ch7xxx_context);
+		pd_usleep(50); 
+
+			
+		ch7036_device_set_power(p_ctx, pOutput_Info->channel);	
+		
+		p_ctx->prev_outchannel = pOutput_Info->channel; 
+	}
+
+#endif
+
+	return PD_SUCCESS;
+}
+
+
+
+
+
+int ch7036_close(void *p_context)
+{
+	
+	ch7036_device_context_t* p_ctx  = (ch7036_device_context_t*)p_context;
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	ch7036_edid_blk_t *p_edid1, *p_edid2;
+
+	p_edid1 = (ch7036_edid_blk_t *)p_ctx->cedid;
+	p_edid2 = (ch7036_edid_blk_t *)p_ctx->hedid;
+
+
+	PD_DEBUG("ch7036: ch7036_close()\n");
+
+#ifndef LVDS_ONLY
+	
+	ch7036_set_power(p_context, PD_POWER_MODE_D3);	
+#endif
+
+	PD_INTERNAL_LVDS_MODULE_CLOSE(ch7036_lvds_close, (p_ctx->internal_lvds));
+
+	if (p_ctx!= NULL) 
+	{
+		
+		if(p_ctx->p_ch7xxx_context) {
+
+			pd_free(p_ch7xxx_context->pInput_Info);
+			p_ch7xxx_context->pInput_Info = NULL;
+
+			pd_free(p_ch7xxx_context->pOutput_Info);
+			p_ch7xxx_context->pOutput_Info = NULL;
+
+			pd_free(p_ch7xxx_context->pPrefer_Info);
+			p_ch7xxx_context->pPrefer_Info = NULL;
+
+			pd_free(p_ctx->p_ch7xxx_context);
+			p_ch7xxx_context = NULL;
+		}
+
+		
+		if(p_edid1) {
+			pd_free(p_edid1->etiming_I);
+			pd_free(p_edid1->etiming_II);
+			pd_free(p_ctx->cedid);
+
+			p_edid1->etiming_I=NULL;
+			p_edid1->etiming_II=NULL;
+			p_ctx->cedid = NULL;
+
+		}
+		if(p_edid2) {
+			pd_free(p_edid2->etiming_I);
+			pd_free(p_edid2->etiming_II);
+			pd_free(p_ctx->hedid);
+
+			p_edid2->etiming_I=NULL;
+			p_edid2->etiming_II=NULL;
+			p_ctx->hedid = NULL;
+
+		}
+
+		if ( p_ctx->p_ch7036_attr_table) {
+			pd_free(p_ctx->p_ch7036_attr_table);
+			p_ctx->p_ch7036_attr_table = NULL;
+			p_ctx->ch7036_num_attrs = 0;
+		}
+
+		if(p_ctx->fw) {
+			pd_free(p_ctx->fw);
+			p_ctx->fw = NULL;
+		}
+
+
+		pd_free(p_ctx);
+		p_ctx = NULL;
+	}
+	
+	return PD_SUCCESS;
+}
+
+
+int ch7036_get_timing_list(void *p_context, pd_timing_t *p_in_list,
+	pd_timing_t **pp_out_list)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+		
+	int ret = 0;
+	
+
+#ifndef T_PANEL_NATIVE_DTD
+	int i;
+	pd_timing_t * p_table;
+	lvds_context_t * p_lvds = (lvds_context_t *)(p_ctx->internal_lvds);
+
+#endif
+
+	pd_port_status_t port_status;
+
+
+
+	PD_DEBUG("ch7036: ch7036_get_timing_list()-enter\n");
+
+	if (p_ch7xxx_context->DeviceID != 0x56)
+	{
+		return (PD_ERR_NODEV);
+	}
+
+#ifdef LVDS_ONLY
+	PD_DEBUG("NUHAIRI: p_ctx->internal_lvds = %lu\n", p_ctx->internal_lvds);
+	if (p_ctx->internal_lvds != NULL){
+		PD_DEBUG("NUHAIRI: p_ctx->internal_lvds = %lu\n", p_ctx->internal_lvds);
+		return PD_INTERNAL_LVDS_MODULE_GET_TIMING_LIST(ch7036_get_timing_list, (p_ctx->internal_lvds,p_in_list,pp_out_list));
+	}
+	return PD_SUCCESS;
+#endif
+
+
+	
+	if ( (g_ch7036_drv.type & PD_DISPLAY_LVDS_INT) || (g_ch7036_drv.type & PD_DISPLAY_LVDS_LHDV) )
+	{
+		ret = PD_INTERNAL_LVDS_MODULE_GET_TIMING_LIST(ch7036_lvds_get_timing_list, (p_ctx->internal_lvds,p_in_list,pp_out_list));
+
+		if(((lvds_context_t *)(p_ctx->internal_lvds))->native_dtd == 0 )
+			return PD_ERR_NO_TIMINGS;
+
+		p_ctx->p_lvds_table = *pp_out_list; 
+
+#ifdef T_PANEL_NATIVE_DTD	
+
+		
+		pd_memcpy(&(p_ctx->native_dtd),((lvds_context_t *)(p_ctx->internal_lvds))->native_dtd,sizeof(pd_timing_t));
+
+		p_ctx->fp_width = ((lvds_context_t *)(p_ctx->internal_lvds))->fp_width;
+		p_ctx->fp_height = ((lvds_context_t *)(p_ctx->internal_lvds))->fp_height;	
+		
+		if( (p_ctx->fp_width == 0) || (p_ctx->fp_height == 0) ) 
+			return PD_ERR_NO_TIMINGS ;
+			
+#else
+		
+		for(i=0,p_table = *pp_out_list;i< 30;i++) 
+			{
+				
+				if((p_table->width == 1024) && (p_table->height == 768 )&& 
+					(p_table->refresh == 60) )
+				{
+				
+				
+
+					pd_memcpy(&(p_ctx->native_dtd),p_table,sizeof(pd_timing_t));
+					p_ctx->fp_width = p_table->width;
+					p_ctx->fp_height = p_table->height;
+					
+					break;
+				}
+				
+				p_table= (pd_timing_t*)((uint8*)p_table + (sizeof(pd_timing_t)+4)); 
+							
+			}
+
+	
+			pd_memcpy(p_lvds->native_dtd,&(p_ctx->native_dtd),sizeof(pd_timing_t));
+			p_lvds->fp_width = p_ctx->fp_width;
+			p_lvds->fp_height = p_ctx->fp_height;
+
+#endif
+			
+	} 
+	
+
+
+	if ( !p_ctx->init_done) {  
+		PD_DEBUG("ch7036: ch7036_get_timing_list()-init is not done- inquire port status...\n");		
+		ch7036_get_port_status((void *)p_ctx, &port_status);
+
+	}
+
+	
+	ch7036_parse_edid(p_ctx);
+
+	return ret;
+
+}
+
+
+
+int ch7036_get_attributes(void *p_context, unsigned long *p_num_attr,
+	pd_attr_t **pp_list)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+		
+	
+
+	PD_DEBUG("ch7036: ch7036_get_attributes()-enter\n");
+
+	
+	
+	if (!p_ctx || !p_num_attr || !pp_list) {
+		return PD_ERR_NULL_PTR;
+	}
+	
+	*pp_list = p_ctx->p_ch7036_attr_table;
+
+	
+	*p_num_attr  = p_ctx->ch7036_num_attrs;
+
+	PD_DEBUG("ch7036: ch7036_get_attributes()- total num_attrs = [%u]\n",*p_num_attr);
+
+	return PD_SUCCESS;
+}
+
+
+int ch7036_set_attributes(void *p_context, unsigned long num_attrs,
+	pd_attr_t *p_list)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = p_ch7xxx_context->pOutput_Info;
+	pd_list_entry_attr_t* list_item;
+
+	pd_port_status_t port_status;
+
+	pd_attr_t        *p_curr, *p_attr;
+	int ret;
+	unsigned long i,temp=0;
+	uint32 temp_chan;
+	ch7036_status_t status;
+
+	
+	PD_DEBUG("ch7036: ch7036_set_attributes()-enter: num_attrs=%u\n", num_attrs);
+	
+	
+	ret = PD_INTERNAL_LVDS_MODULE_SET_ATTRIBUTES(ch7036_lvds_set_attrs, (p_ctx->internal_lvds,num_attrs,p_list));
+
+	
+	
+	if(ret != PD_SUCCESS)
+		return ret;
+
+#ifdef LVDS_ONLY
+		return PD_SUCCESS;
+#endif	
+		
+	
+	if (!p_ctx->init_done) {
+
+		PD_DEBUG("ch7036: ch7036_set_attributes()- at bootup...\n");
+
+		PD_DEBUG("ch7036: ch7036_set_attributes()- p_ctx->hpd [%x]\n", p_ctx->hpd);
+
+		p_attr = pd_get_attr(p_list, num_attrs, PD_ATTR_ID_DISPLAY, 0);
+		if (p_attr && (p_attr->flags & PD_ATTR_FLAG_VALUE_CHANGED) ) {
+			
+		
+			pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_DISPLAY,
+				PD_GET_ATTR_LIST)->current_value
+			= p_attr->current_value; 
+
+
+		}
+
+		
+		p_attr = pd_get_attr(p_list, num_attrs, PD_ATTR_ID_HDMI_OUT_MODE, 0);
+		p_curr = pd_get_attr(p_ctx->p_ch7036_attr_table, num_attrs, PD_ATTR_ID_HDMI_OUT_MODE, 0);
+
+		if (p_attr && (p_attr->flags & PD_ATTR_FLAG_VALUE_CHANGED) ) {
+					
+			if(p_curr) {
+				pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_HDMI_OUT_MODE,
+					PD_GET_ATTR_LIST)->current_value
+				= p_attr->current_value;
+				p_curr->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+			}
+
+
+		}
+		
+						
+		p_attr = pd_get_attr(p_list, num_attrs, PD_ATTR_ID_DVI_OUT_MODE, 0);
+		p_curr = pd_get_attr(p_ctx->p_ch7036_attr_table, num_attrs, PD_ATTR_ID_DVI_OUT_MODE, 0);
+
+		if (p_attr && (p_attr->flags & PD_ATTR_FLAG_VALUE_CHANGED) ) {
+			
+
+			
+			if(p_curr) {
+				pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_DVI_OUT_MODE,
+					PD_GET_ATTR_LIST)->current_value
+				= p_attr->current_value;
+				p_curr->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+			}
+
+
+		}
+	
+		
+		
+		
+		p_attr = pd_get_attr(p_list, num_attrs, PD_ATTR_ID_CRT_OUT_MODE, 0);
+		p_curr = pd_get_attr(p_ctx->p_ch7036_attr_table, num_attrs, PD_ATTR_ID_CRT_OUT_MODE, 0);
+
+		if (p_attr && (p_attr->flags & PD_ATTR_FLAG_VALUE_CHANGED) ) {
+			
+			if(p_curr) {
+				pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_CRT_OUT_MODE,
+						PD_GET_ATTR_LIST)->current_value
+					= p_attr->current_value;
+
+				p_curr->flags &= ~PD_ATTR_FLAG_USER_INVISIBLE; 
+			}
+
+
+		}
+		
+	
+		
+	} 
+
+	
+	for (i = 0, p_attr = p_list; i < num_attrs; i++,p_attr++) 
+	{
+		
+		
+		if (!(p_attr->flags & PD_ATTR_FLAG_VALUE_CHANGED)) {
+
+			continue;	
+		}
+
+		
+		p_attr->flags &= ~PD_ATTR_FLAG_VALUE_CHANGED;
+	
+		if (p_attr->flags & PD_ATTR_FLAG_USER_INVISIBLE)
+			continue;
+
+		
+		if( (p_attr->id == 0x1A) || (p_attr->id == 0x1B) ||
+			(p_attr->id == 0x3C) || (p_attr->id == 0x46) ||
+			(p_attr->id == 0x47) )
+			continue;
+		
+#if 0		
+		
+		p_curr = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,p_attr->id,
+				PD_GET_ATTR_LIST); 
+#endif 
+		
+		
+		p_curr = pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,p_attr->id,
+				0); 
+		PD_DEBUG("ch7036_set_attributes(): attribute changed is of type [%ld] name [%s] id [%ld]\n",p_attr->type, p_attr->name, p_attr->id);
+
+		PD_DEBUG("ch7036_set_attributes():current value [%ld] requested value [%ld]\n",p_curr->current_value, p_attr->current_value); 	
+			
+		
+		temp = p_curr->current_value; 
+		p_curr->current_value = p_attr->current_value;
+		switch (p_attr->id) {
+			case PD_ATTR_ID_DISPLAY:
+				
+				list_item = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_DISPLAY,
+							PD_GET_ATTR_LIST_ENTRY); 			
+				
+				p_attr->flags |= PD_ATTR_FLAG_SETMODE;  
+
+				if(list_item->value == CHANNEL_AUTO_DETECT) { 
+					p_ctx->man_sel_out = 0;
+
+					if(p_ctx->init_done) { 
+						p_ctx->hpd |= CH7036HPD_RESERVED1; //force port status inquiry
+						ch7036_get_port_status((void *)p_ctx, &port_status);
+	
+					}
+					break; 
+				}
+				//end of CHANNEL_AUTO_DETECT, manual selection of display output begins
+				
+				
+				p_ctx->man_sel_out = 1; 
+
+				//1- save a copy
+				temp_chan = p_ctx->prev_outchannel;
+				p_ctx->prev_outchannel = pOutput_Info->channel;
+
+				PD_DEBUG("ch7036_set_attributes():current list item value [0x%x]\n",list_item->value);
+
+				//2- get requested output channel- assume it's allowed		
+
+				if(list_item->value & CHANNEL_DVI) { 					
+					pOutput_Info->channel = (list_item->value & 0x01) | CHANNEL_HDMI;
+				}
+				else	 	
+					pOutput_Info->channel = list_item->value;
+
+				PD_DEBUG("ch7036_set_attributes():current output channel value [0x%x]\n",pOutput_Info->channel);
+
+				//3- check requested selection vs what is available
+				status = ch7036_get_attached_device(p_ctx); //manual mode- verify its selection and correct if needed
+				
+				//4- NOT allowed display choice- also include when none is connected
+				if( status == SS_DISPLAY_CHOICE_NOT_ALLOWED) {  //restore previous states
+					PD_DEBUG("ch7036_set_attributes(): display choice is not allowed- restore prev. states...\n");
+							
+					p_ctx->hpd &= 0xEF;	 //reset												
+					
+					if(p_ctx->init_done){ 																		
+						pOutput_Info->channel = p_ctx->prev_outchannel;
+						p_ctx->prev_outchannel = temp_chan; 
+						p_curr->current_value = temp; 
+						//when system is restarted right after this point, need to provide main driver w/ a valid 
+						//restored choice
+						p_attr->current_value = p_curr->current_value;
+						if(p_curr->current_value==1)
+							p_ctx->man_sel_out = 0; 
+
+					}
+					else { //at installation, when init is not done, if manual choice is not valid, revert to auto
+					
+						p_curr->current_value = 1; 
+						p_ctx->man_sel_out = 0; 
+						
+						if (pOutput_Info->channel & CHANNEL_HDMI) //DVI mapped to CHANNEL_HDMI
+						{
+							pOutput_Info->channel = (pOutput_Info->channel & 0x01) | CHANNEL_VGA;
+							p_ctx->prev_outchannel = pOutput_Info->channel;
+						
+						} 
+						else { //VGA
+							pOutput_Info->channel = (pOutput_Info->channel & 0x01) | CHANNEL_HDMI;
+							p_ctx->prev_outchannel = pOutput_Info->channel;
+						}
+						
+						
+					}
+
+					PD_DEBUG("ch7036_set_attributes(): p_curr->current_value is: [%x]\n",p_curr->current_value);
+					if(p_ctx->hpd == 0x50) {//none is attached
+						pOutput_Info->channel &= CHANNEL_LVDS; 
+						p_ctx->prev_outchannel = pOutput_Info->channel;
+					}
+
+										
+				}
+				PD_DEBUG("ch7036_set_attributes(): now- output display channel is: value [%ld]\n",pOutput_Info->channel);
+
+
+				//special case channel CHANNEL_xxx_HDMI: 
+				//incoming dvi format => convert to hdmi format- or- incoming hdmi format => convert to dvi format 
+				if( ( (pOutput_Info->hdmi_fmt.is_dvi_mode==1) && ((list_item->value & 0x02) == CHANNEL_HDMI) ) ||
+					( (pOutput_Info->hdmi_fmt.is_dvi_mode==0) && ((list_item->value & 0x08) == CHANNEL_DVI) )
+					)
+					p_ctx->hpd |= 0x40; //request edid read again to update hdmi/dvi format accordingly
+
+
+				//base on 'new' attached info, read edid and set proper display output channel, including DVI
+				//case: manual selection w/ hpd change
+				if(p_ctx->hpd & 0x44) 	{		
+					ch7036_alter_display_channel(p_ctx); 
+				}
+				
+				if( (pOutput_Info->channel & 0x04) == CHANNEL_VGA || p_curr->current_value==1  /* Coerced Auto Detect */ ) 
+					break;
+
+				//case: 
+				//channel CHANNEL_xxx_HDMI- update list item xxx_DVI or xxx_HDMI accordingly
+				if((pOutput_Info->channel & CHANNEL_HDMI) && (pOutput_Info->hdmi_fmt.is_dvi_mode==1) )
+					p_curr->current_value = (pOutput_Info->channel & 0x01) == CHANNEL_LVDS?3:6;//lvds-dvi:dvi
+				
+				else 
+					if((pOutput_Info->channel & CHANNEL_HDMI) && (pOutput_Info->hdmi_fmt.is_dvi_mode==0) )
+						p_curr->current_value = (pOutput_Info->channel & 0x01) == CHANNEL_LVDS?2:5;//lvds-hdmi:hdmi
+
+				PD_DEBUG("ch7036_set_attributes(): after alter channel- output display channel is: value [%ld]\n",pOutput_Info->channel);
+
+				break;
+
+			case PD_ATTR_ID_HDMI_OUT_MODE:
+				list_item = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_HDMI_OUT_MODE,
+							PD_GET_ATTR_LIST_ENTRY); 
+				p_ctx->hdmi_mode_index = list_item->value;
+				PD_DEBUG("ch7036_set_attributes(): updated hdmi_mode_index is: value [%ld]\n",p_ctx->hdmi_mode_index);
+				p_attr->flags |= PD_ATTR_FLAG_SETMODE;
+				break;
+				
+			case PD_ATTR_ID_DVI_OUT_MODE:
+				list_item = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_DVI_OUT_MODE,
+							PD_GET_ATTR_LIST_ENTRY); 
+				p_ctx->dvi_mode_index = list_item->value;
+				PD_DEBUG("ch7036_set_attributes(): updated dvi_mode_index is: value [%ld]\n",p_ctx->dvi_mode_index);
+				p_attr->flags |= PD_ATTR_FLAG_SETMODE;
+				break;
+
+			case PD_ATTR_ID_CRT_OUT_MODE:
+				list_item = (pd_list_entry_attr_t *)pd_get_attr(p_ctx->p_ch7036_attr_table, p_ctx->ch7036_num_attrs,PD_ATTR_ID_CRT_OUT_MODE,
+							PD_GET_ATTR_LIST_ENTRY); 
+				p_ctx->crt_mode_index = list_item->value;
+				PD_DEBUG("ch7036_set_attributes(): updated crt_mode_index is: value [%ld]\n",p_ctx->crt_mode_index);
+				p_attr->flags |= PD_ATTR_FLAG_SETMODE;
+				break;
+
+			case PD_ATTR_ID_HPOSITION: //these attributes are for vga only
+			case PD_ATTR_ID_VPOSITION:
+			
+				PD_DEBUG("ch7036_set_attributes(): set vga h/v position...\n");
+				 
+				//save vga h/v attribute context
+				if(p_curr->current_value > ((pd_range_attr_t *)p_curr)->max)
+					p_curr->current_value = ((pd_range_attr_t *)p_curr)->max;
+				else if (p_curr->current_value < ((pd_range_attr_t *)p_curr)->min)
+					p_curr->current_value = ((pd_range_attr_t *)p_curr)->min;
+	
+				
+				if( pOutput_Info->channel & CHANNEL_HDMI)
+					break;
+
+				if (p_attr->id == PD_ATTR_ID_HPOSITION) 
+					pOutput_Info->h_position = (uint16)(p_curr->current_value);
+				else
+					pOutput_Info->v_position = (uint16)(p_curr->current_value);
+					
+				ch7036_set_position(p_ctx, (uint8)p_attr->id, (uint16)(p_curr->current_value));
+				
+				PD_DEBUG("ch7036_set_attributes(): updated and set vga position: value [%d]\n",p_curr->current_value);
+		
+				break;
+			case PD_ATTR_ID_HSCALE:
+			case PD_ATTR_ID_VSCALE:
+			case PD_ATTR_ID_HSCALE_CRT:
+			case PD_ATTR_ID_VSCALE_CRT:
+				
+				if(p_curr->current_value > ((pd_range_attr_t *)p_curr)->max)
+					p_curr->current_value = ((pd_range_attr_t *)p_curr)->max;
+				else if (p_curr->current_value < ((pd_range_attr_t *)p_curr)->min)
+					p_curr->current_value = ((pd_range_attr_t *)p_curr)->min;
+				
+			
+				if(p_attr->id== PD_ATTR_ID_HSCALE || p_attr->id== PD_ATTR_ID_HSCALE_CRT)
+					ch7036_set_scaling (pOutput_Info,PD_ATTR_ID_HSCALE, (uint8)p_curr->current_value);
+				else 
+					ch7036_set_scaling (pOutput_Info,PD_ATTR_ID_VSCALE, (uint8)p_curr->current_value);				
+				PD_DEBUG("ch7036_set_attributes(): updated scale value is: value [%lu]\n",p_curr->current_value);
+			
+				p_attr->flags |= PD_ATTR_FLAG_SETMODE; 
+
+				break;
+			case PD_ATTR_ID_DITHER_BYPASS: 
+				
+			
+				PD_DEBUG("ch7036_set_attributes(): updated quality enhance value is: value [%lu]\n",p_curr->current_value);
+				ch7036_set_quality_enhancement(p_ctx,(uint8)p_curr->current_value);
+				break;
+			
+			case PD_ATTR_ID_DITHER: 
+				p_ctx->dither_select = (uint8)p_curr->current_value;
+				PD_DEBUG("ch7036_set_attributes(): updated dither select value is: value [%lu]\n",p_curr->current_value);
+				ch7036_set_dither(p_ctx);
+
+				break;
+
+			case PD_ATTR_ID_TEXT_FILTER:
+				
+				if(p_curr->current_value > ((pd_range_attr_t *)p_curr)->max)
+					p_curr->current_value = ((pd_range_attr_t *)p_curr)->max;
+				else if (p_curr->current_value < ((pd_range_attr_t *)p_curr)->min)
+					p_curr->current_value = ((pd_range_attr_t *)p_curr)->min;
+				
+				PD_DEBUG("ch7036_set_attributes(): update text tuning value...\n");
+				ch7036_set_text_enhancement (p_ctx, (uint8) p_curr->current_value);
+
+				break;
+
+			case PD_ATTR_ID_LOAD_FIRMWARE:
+				PD_DEBUG("ch7036_set_attributes(): updated [reload-firmware] value is: value [%lu]\n",p_curr->current_value);
+
+				break;
+
+			case PD_ATTR_ID_DWNSCAL_BYPASS:
+				PD_DEBUG("ch7036_set_attributes(): updated [dwnscal_bypass] value is: value [%lu]\n",p_curr->current_value);
+				if(p_curr->current_value ) { 
+					p_ctx->dwnscal_bypass = 1; 
+					p_ctx->downscaled[MODE_8x6_7x4_BYPASS]=1;
+				}
+				else {
+					p_ctx->dwnscal_bypass = 0; 
+					p_ctx->downscaled[MODE_8x6_7x4_BYPASS]=0;
+				}
+
+				break;
+
+			case PD_ATTR_ID_REFRESH:
+				PD_DEBUG("ch7036_set_attributes(): refresh value is: value [%lu]\n",p_curr->current_value);
+				
+				if(p_curr->current_value ) 
+						p_curr->current_value= 0; 
+				
+				if(p_ctx->init_done && !p_ctx->man_sel_out ) {
+
+						p_ctx->hpd |= CH7036HPD_RESERVED1; //force port status inquiry- edid read bit
+
+						ch7036_get_port_status((void *)p_ctx, &port_status);
+
+						p_attr->flags |= PD_ATTR_FLAG_SETMODE; 					
+					
+				}
+				break;
+
+			default:
+				
+				PD_DEBUG("ch7036_set_attr(): unhandled attr name[%s]id[%ld]curr_index[%lu]\n",p_attr->name, p_attr->id,p_attr->current_value);
+				break;
+
+		}
+		
+	}
+
+	PD_DEBUG("ch7036: ch7036_set_attributes()-p_ctx->man_sel_out at exit [0x%x]\n",p_ctx->man_sel_out);
+	
+	PD_DEBUG("ch7036: ch7036_set_attributes()-p_ctx->hpd at exit [0x%x]\n",p_ctx->hpd);
+
+	PD_DEBUG("ch7036: ch7036_set_attributes()-exit\n");
+
+	return ret;
+}
+
+int ch7036_set_power(void *p_context, unsigned long state)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+	OUTPUT_INFO* pOutput_Info = (p_ctx->p_ch7xxx_context)->pOutput_Info;
+
+
+	int ret;
+
+
+	PD_DEBUG("ch7036: ch7036_set_power()-enter: requested state=%x\n", state);
+
+#ifdef LVDS_ONLY
+	return	PD_INTERNAL_LVDS_MODULE_SET_POWER(ch7036_set_power, (p_ctx->internal_lvds,state));
+#endif
+		
+	if (!p_ctx) 
+		return PD_ERR_NULL_PTR;
+	
+	if (state > PD_POWER_MODE_D3)
+		return PD_ERR_INVALID_POWER;
+	
+	if (state != PD_POWER_MODE_D0) {
+	
+		if(pOutput_Info->channel != CHANNEL_LVDS_HDMI_VGA_OFF)  
+			p_ctx->prev_outchannel = pOutput_Info->channel; //store current output channel before temporarily powered down
+		pOutput_Info->channel = CHANNEL_LVDS_HDMI_VGA_OFF; 
+
+		
+		ch7036_device_set_power(p_ctx,pOutput_Info->channel);
+				
+		ret = PD_INTERNAL_LVDS_MODULE_SET_POWER(ch7036_lvds_set_power, (p_ctx->internal_lvds,state));
+		
+	
+	}
+	else {
+		
+
+		ch7036_set_output_channel(p_ctx,p_ctx->prev_outchannel); //restore previous output channel
+
+		PD_DEBUG("ch7036: ch7036_set_power()- p->ctx-hpd [0x%x]\n",p_ctx->hpd);
+		PD_DEBUG("ch7036: ch7036_set_power()- requested output channel- [%x]\n", pOutput_Info->channel);
+
+
+		PD_INTERNAL_LVDS_MODULE_SET_POWER(ch7036_lvds_set_power, (p_ctx->internal_lvds,state));
+		
+		ch7036_device_set_power(p_ctx,pOutput_Info->channel);
+						
+	}
+
+	
+	p_ctx->pwr_state = state;
+
+	return PD_SUCCESS;
+}
+
+
+int ch7036_get_power(void *p_context, unsigned long *p_state)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+	
+
+	PD_DEBUG("ch7036: ch7036_get_power()\n");
+
+#ifdef LVDS_ONLY
+	return PD_INTERNAL_LVDS_MODULE_GET_POWER(ch7036_get_power, (p_ctx->internal_lvds,p_state));
+#endif
+
+	*p_state = p_ctx->pwr_state;
+
+	return PD_SUCCESS;
+}
+
+int ch7036_save(void *p_context, void **state, unsigned long flags)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+	OUTPUT_INFO* pOutput_Info = (p_ctx->p_ch7xxx_context)->pOutput_Info;
+
+	PD_DEBUG("ch7036: ch7036_save()\n"); 
+
+#ifdef LVDS_ONLY
+	
+	return PD_INTERNAL_LVDS_MODULE_SAVE(ch7036_save,(p_ctx->internal_lvds, state, flags));
+#endif
+
+	//in Linux, when being called @ init, it incorrectly assigned unintialized global attribute value to prev outchannel	
+	//p_ctx->prev_outchannel = ch7036_get_output_channel(p_context); 
+	//fixed
+	p_ctx->prev_outchannel = pOutput_Info->channel; 
+	
+
+	*state = NULL;
+
+	return PD_SUCCESS;
+}
+
+
+
+int ch7036_restore(void *p_context, void *state, unsigned long flags)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)p_context;
+
+	unsigned long i;
+
+	
+
+	PD_DEBUG("ch7036: ch7036_restore()\n");
+
+#ifdef LVDS_ONLY
+	
+	return PD_INTERNAL_LVDS_MODULE_RESTORE(ch7036_restore,(p_ctx->internal_lvds, state, flags));
+#endif
+
+	
+	if (ch7036_load_firmware(p_ctx) != SS_SUCCESS)   { 
+		PD_DEBUG("ch7036: ch7036_restore()-  load fw is NOT a SUCCESS\n");
+		return PD_ERR_UNSUCCESSFUL;
+		
+	}
+	else {
+		PD_DEBUG("ch7036: ch7036_restore()-  load fw is a SUCCESS\n");
+	}
+
+
+
+	if(p_ctx->prev_outchannel == CHANNEL_LVDS_HDMI) {
+
+		ch7036_set_output_channel(p_context, p_ctx->prev_outchannel);
+		ch7036_set_mode(p_context, &(p_ctx->native_dtd), 0);  
+		ch7036_post_set_mode(p_context, &(p_ctx->native_dtd), 0);
+	}
+
+	
+	for(i=0;i<p_ctx->ch7036_num_attrs;i++) {
+
+		if( p_ctx->p_ch7036_attr_table[i].id == PD_ATTR_ID_REFRESH ) {
+
+			
+			p_ctx->p_ch7036_attr_table[i].flags |= PD_ATTR_FLAG_VALUE_CHANGED;
+			p_ctx->p_ch7036_attr_table[i].current_value = 1;
+
+			ch7036_set_attributes(p_context, 1, &p_ctx->p_ch7036_attr_table[i]);
+			break;
+		}
+
+	}
+
+	
+	return PD_SUCCESS;
+
+}
+
+
+int ch7036_get_port_status(void *context, pd_port_status_t *port_status)
+{
+	ch7036_device_context_t *p_ctx = (ch7036_device_context_t *)context;
+	OUTPUT_INFO* pOutput_Info = (p_ctx->p_ch7xxx_context)->pOutput_Info;
+
+#if 0
+	FW7036_CFG* fv = (FW7036_CFG*) p_ctx->fw;
+	int ret;
+#endif
+	
+	port_status->display_type = PD_DISPLAY_LVDS_INT;
+	port_status->connected    = PD_DISP_STATUS_UNKNOWN;
+	
+	
+	PD_DEBUG("ch7036: ch7036_get_port_status()-enter... p_ctx->hpd [%x]\n", p_ctx->hpd);
+
+#if 0
+	PD_DEBUG("Get LHFM Version Information.\r\n");
+	ret = LHFM_get_version(p_ch7xxx_context, fv);
+	if (0==ret) {
+		PD_DEBUG("Ma_ver=%d, mi_ver=%d, did=%02X, rid= %02X, capability=%s\r\n",
+			fv->ver_major, fv->ver_minor, fv->did, fv->rid, (fv->capbility & 0x2) ? "EDID+HDCP" : "EDID"); 
+	}
+	else {
+		PD_DEBUG("--- failed!\r\n");
+		PD_DEBUG("status: [%s]\n",ret ==-1?"timeout!":"firmware_error!");
+	}
+#endif
+
+	
+#ifdef LVDS_ONLY
+	
+	return PD_INTERNAL_LVDS_MODULE_GET_PORT_STATUS(ch7036_get_port_status,(p_ctx->internal_lvds, port_status));
+#endif
+	
+	if(! (p_ctx->hpd & CH7036HPD_RESERVED1) ) {  
+		//note: main driver check port status several times, this block is to speed things up a little 
+		if ((p_ctx->init_done) && (pOutput_Info->channel == p_ctx->prev_outchannel ) ) {
+			if(p_ctx ->hpd & 0x22)
+				port_status->connected = PD_DISP_STATUS_ATTACHED;
+			PD_DEBUG("ch7036: ch7036_get_port_status()-output channel UNCHANGED- exit. p_ctx->hpd [%x]\n", p_ctx->hpd);
+			return PD_SUCCESS;
+		}
+
+	}
+
+	if(p_ctx->use_firmware) {
+
+		ch7036_get_attached_device(p_ctx);		
+		if(p_ctx ->hpd & 0x22)
+			port_status->connected = PD_DISP_STATUS_ATTACHED;
+
+		ch7036_alter_display_channel(p_ctx); 
+			
+		}
+
+	
+	p_ctx->hpd &= 0xEE; 
+	
+	PD_DEBUG("ch7036: ch7036_get_port_status()-exit. p_ctx->hpd [%x]\n", p_ctx->hpd);
+	return PD_SUCCESS;
+}
+
+
+
+
+int ch7036_initialize_device(ch7036_device_context_t *p_ctx)
+{
+	DEV_CONTEXT* p_ch7xxx_context = p_ctx->p_ch7xxx_context;
+	OUTPUT_INFO* pOutput_Info = (p_ctx->p_ch7xxx_context)->pOutput_Info;
+	PREFER_INFO* pPrefer_Info = p_ch7xxx_context->pPrefer_Info;
+	uint8 reg; //,i=0;
+
+	ch7036_edid_blk_t* p_hedid; 
+	ch7036_edid_blk_t* p_cedid ;
+
+
+	PD_DEBUG("ch7036: ch7036_initialize_device()- ENTER...\n");
+	
+
+	p_ctx->init_done = 0;
+	p_ctx->hpd = 0;	
+
+	p_ctx->downscaled[MODE_6x4_BYPASS] = 1; 
+	p_ctx->downscaled[MODE_8x6_7x4_BYPASS] = 1;
+	p_ctx->dwnscal_bypass = 1; 
+
+	if(p_ctx->use_firmware) {
+
+		p_hedid = (ch7036_edid_blk_t *)p_ctx->hedid;
+		p_cedid = (ch7036_edid_blk_t *)p_ctx->cedid;
+
+		
+		
+		pd_memcpy(p_cedid->etiming_I,et_I,8* sizeof(established_timings_t));  
+		pd_memcpy(p_cedid->etiming_II,et_II,8* sizeof(established_timings_t)); 
+		
+		
+		p_cedid->etiming_man = &et_man;
+
+
+		
+		pd_memcpy(p_hedid->etiming_I,et_I,8* sizeof(established_timings_t));  
+		pd_memcpy(p_hedid->etiming_II,et_II,8* sizeof(established_timings_t)); 
+
+		p_hedid->etiming_man = &et_man;
+
+		
+		p_cedid->is_edid = 0;
+		p_hedid->is_edid = 0;
+		p_cedid->ebn = 0;
+		p_hedid->ebn = 0;
+
+		
+		I2CWrite(p_ch7xxx_context,0x03, 0x04);
+		reg = I2CRead(p_ch7xxx_context,0x52);
+		reg = reg & 0xEF; 
+		I2CWrite(p_ch7xxx_context,0x52, reg);
+
+
+		I2CWrite(p_ch7xxx_context,0x03, 0x0);
+		reg = I2CRead(p_ch7xxx_context,0x07);
+		reg = reg & 0x70; 
+		I2CWrite(p_ch7xxx_context,0x07, reg);
+
+		I2CWrite(p_ch7xxx_context,0x4E, I2CRead(p_ch7xxx_context,0x4E) & 0x7F); 
+		
+		I2CWrite(p_ch7xxx_context,0x03, 0x01);
+		reg = I2CRead(p_ch7xxx_context,0x0F); 
+		reg = reg & 0x7F; 
+		I2CWrite(p_ch7xxx_context,0x0F, reg);
+	
+		I2CWrite(p_ch7xxx_context,0x03, 0x03);		
+		reg = I2CRead(p_ch7xxx_context,0x6E);
+		reg = reg & 0xBF; 
+		I2CWrite(p_ch7xxx_context,0x6E, reg | 0x40);
+
+
+	}
+
+	
+	pOutput_Info->hdmi_fmt.is_dvi_mode = 0;  
+	
+	pOutput_Info->channel = CHANNEL_LVDS | CHANNEL_HDMI;
+
+
+
+	PD_DEBUG("ch7036: ch7036_initialize_device()- default output channel is [%u]\n",pOutput_Info->channel);
+
+
+	
+	pOutput_Info->h_position = DEFAULT_POSITION;
+	pOutput_Info->v_position = DEFAULT_POSITION;
+
+
+	p_ctx->dither_select = DITHER_18_TO_18; 
+	p_ctx->man_sel_out= 0; 
+
+	ch7036_set_scaling (pOutput_Info,PD_ATTR_ID_HSCALE, HDMI_DEFAULT_UNDERSCAN);
+	ch7036_set_scaling (pOutput_Info,PD_ATTR_ID_VSCALE, HDMI_DEFAULT_UNDERSCAN);
+
+	ch7036_set_prefer_timing_info(p_ctx,pPrefer_Info); 
+	
+	return PD_SUCCESS;
+}
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.h
new file mode 100644
index 0000000..6798815
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_port.h
@@ -0,0 +1,59 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_port.h
+* @version 1.2.2
+*-----------------------------------------------------------------------------
+*/
+
+
+#ifndef _CH7036_PORT_H_
+#define _CH7036_PORT_H_
+
+
+
+#include "ch7036_intf.h"
+#include "ch7036_attr.h"
+
+int ch7036_open(pd_callback_t *p_callback, void **p_context);
+int ch7036_init_device(void *p_context);
+int ch7036_get_timing_list(void *p_context, pd_timing_t *p_in_list,
+	pd_timing_t **pp_out_list);
+int ch7036_set_mode(void *p_context, pd_timing_t *p_mode, unsigned long flags);
+int ch7036_post_set_mode(void *p_context, pd_timing_t *p_mode,
+	unsigned long flags);
+
+int ch7036_get_attributes(void *p_context, unsigned long *p_num_attr,
+	pd_attr_t **pp_list);
+int ch7036_set_attributes(void *p_context, unsigned long num_attr,
+	pd_attr_t *p_list);
+unsigned long ch7036_validate(unsigned long cookie);
+int ch7036_close(void *p_context);
+
+int ch7036_set_power(void *p_context, unsigned long state);
+int ch7036_get_power(void *p_context, unsigned long *p_state);
+int ch7036_save(void *p_context, void **pp_state, unsigned long flags);
+int ch7036_restore(void *p_context, void *p_state, unsigned long flags);
+int ch7036_get_port_status(void *context, pd_port_status_t *port_status);
+
+
+#endif 
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.c
new file mode 100755
index 0000000..126480f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.c
@@ -0,0 +1,224 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_reg_table.c
+* @version 1.2.2
+*-----------------------------------------------------------------------------
+*/
+
+
+
+#include "ch7036_reg_table.h"
+
+
+
+
+MULTI_REG g_MultiRegTable[MUL_ID_END] = 
+{
+	{BASE_FLAG,       NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  3, 6,	NOOP_INDEX,   0, 7,		0},
+	
+	{HTI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0B,3, 6,	0x0D, 0, 7,		0},
+	{VTI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x11,3, 5,	0x13, 0, 7,		0},
+	{HAI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0B,0, 2,	0x0C, 0, 7,		0},
+	{VAI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x11,0, 2,	0x12, 0, 7,		0},
+	{HOI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0E,0, 2,	0x0F, 0, 7,		0},
+	{VOI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x14,0, 2,	0x15, 0, 7,		0},
+	{HWI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0E,3, 5,	0x10, 0, 7,		0},
+	{VWI,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x14,3, 5,	0x16, 0, 7,		0},
+	{RCLK,            NOOP_INDEX,  0, 0,	0x0F,0, 1,  0x14,0, 7,  0x15, 0, 7,     4},
+	{DITHER_SEL1_SPP, NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x57, 7, 7,		4},	
+	{DITHER_SEL0_SPP, NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x57, 6, 6,		4},
+	{HSYNCP_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x41, 6, 6,		1},
+	{VSYNCP_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x41, 5, 5,		1},
+	{NP_INV0_SPP,     NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x58, 4, 4,		4},	
+	{NP_INV1_SPP,     NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x58, 3, 3,		4},	
+	{NP_INV2_SPP,     NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x58, 2, 2,		4},	
+	{NP_INV3_SPP,     NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x58, 1, 1,		4},	
+	{LVDS_IN_ORDER,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,	 0, 0,  NOOP_INDEX,  0, 0,  0x5F, 0, 0,		4},
+	
+	{LVDS0_SEQ_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x5E, 5, 7,		4},	
+	{LVDS1_SEQ_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x5E, 2, 4,		4},	
+	{LVDS2_SEQ_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,	 0, 0,  0x5F, 7, 7,		4},	
+	{LVDS3_SEQ_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x5F, 4, 6,		4},	
+	{LVDSCLK_SEQ_SPP, NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x5F, 1, 3,		4},	
+	{HSYNC_CNT_TH_SPP,NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x59, 5, 6,		4},	
+	{PRBS_SET_SEL_SPP,NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x61, 1, 1,		4},
+	{LVDS0_POL_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x59, 4, 4,		4},	
+    {LVDS1_POL_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x59, 3, 3,		4},	
+	{LVDS2_POL_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x59, 2, 2,		4},	
+	{LVDS3_POL_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x59, 1, 1,		4},	
+	{LVDSCLK_POL_SPP, NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x59, 0, 0,		4},	
+	
+	{HTO,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1F,3, 6,	0x21, 0, 7,		0},
+	{VTO,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x25,3, 5,	0x27, 0, 7,		0},
+	{HAO,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1F,0, 2,	0x20, 0, 7,		0},
+	{VAO,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x25,0, 2,	0x26, 0, 7,		0},
+	{HOO_HDMI,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x54,0, 2,	0x55, 0, 7,		0},
+	{VOO_HDMI,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x57,0, 2,	0x58, 0, 7,		0},
+	{HWO_HDMI,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x54,3, 5,	0x56, 0, 7,		0},
+	{VWO_HDMI,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x57,3, 5,	0x59, 0, 7,		0},
+	{HPO_I,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x19, 5, 5,		0},
+	{VPO_I,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x19, 4, 4,		0},
+	{DEPO_I,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x19, 3, 3,		0},
+	{HPO_O,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E, 4, 4,		0},
+	{VPO_O,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E, 3, 3,		0},
+	{DEPO_O,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E, 5, 5,		0},
+	
+	{HD_DVIB,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0A, 2, 2,		0},
+	{INTLC,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1E, 5, 5,		0},
+	{HD_LV_POL,       NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x4C, 5, 5,		0},
+	{HD_LV_SEQ,       NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x4C, 0, 4,		0},
+	{HDMI_LVDS_SEL,   NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x7E, 7, 7,		0},
+	{VSP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x24, 5, 5,		1},
+	{HSP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x24, 4, 4,		1},
+	{HDMIIN_HSP,	  NOOP_INDEX,  0, 0,    NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x61, 6, 6,		4},
+	{HDMIIN_VSP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x61, 5, 5,		4},
+	{HDMIIN_DEP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x61, 4, 4,		4},
+	{M1M0,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x3D, 2, 3,		0},
+	{C1C0,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x3D, 0, 1,		0},
+	{VIC,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x40, 0, 5,		0},
+	{COPY,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x4B, 6, 6,		0},
+	{SCAN_EN,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6C, 0, 0,		4},
+	{HAO_SCL,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6A,0, 7,	0x6B, 5, 7,		4},
+	{VAO_SCL,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6B,0, 4,	0x6C, 2, 7,		4},
+	
+	{I2SPOL,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1E, 7, 7,		0},
+	{I2S_SPDIFB,      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1E, 6, 6,		0},
+	{I2S_LENGTH,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1E, 2, 3,		0},
+	{I2SFMT,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1E, 0, 1,		0},
+	{UCLK,            NOOP_INDEX,  0, 0,	0x0F,4, 5,  0x10,0, 7,  0x11, 0, 7 ,    4},
+	
+	{PCLK_NUM,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x28,  0, 7,0x29, 0, 7,		1},	
+	
+	{UCLKSEC,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x12, 4, 4,		1},
+	{PLL1N1,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x55, 3, 5,		4},	
+	{PLL1N2,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x55, 0, 2,		4},	
+	{PLL1N3,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x56, 5, 7,		4},	
+	{PLL3N8,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x13, 1, 2,		1},
+	{A2,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x5E, 0, 7,		0},
+	{MCLK,            NOOP_INDEX,  0, 0,	0x0F,2, 3,  0x12,0, 7,  0x13, 0, 7,     4},
+	{PLL2N5,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,  0x12,0, 1,	0x13, 7, 7,		1},
+	{PLL2N54,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6E, 2, 2,		4},
+	{PLL2N53,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6E, 1, 1,		4},
+	{PLL2N6,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x69, 5, 6,		4},
+	{PLL2N7,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x69, 3, 4,		4},
+	{DIVXTAL,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6E, 3, 7,		4},
+	{A1,			  0x5A,0, 7,	0x5B,0, 7,	0x5C,0, 7,	0x5D, 0, 7,		0},
+	{A3,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x60, 0, 7,		4},
+	{TXPLL_FFD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x67, 7, 7,		4},
+	{DRI_PLL_N1,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0C, 3, 4,		1},
+	{DRI_PLL_N3,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0C, 1, 2,		1},
+	{UCLKOD_SEL,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x61, 7, 7,		4},
+
+	{GCKSEL,          NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x54, 6, 6,		4},
+	{TSTEN1,          NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x14, 6, 6,		1},
+	
+	{LNSEL,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x68, 1, 2,		0},
+	{DAT16_32B,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x74, 7, 7,		0},
+	{TRUE24,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x74, 3, 3,		0},
+	{TRUE_COM,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x74, 4, 4,		0},
+	{WRLEN,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2D, 6, 7,		0},
+	{ROTATE,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2D, 4, 5,		0},
+	{HFLIP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E, 7, 7,		0},
+	{VFLIP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E, 6, 6,		0},
+	{DNSMPEN,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x25, 6, 6,		0},
+	{HADWSPP,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x60,0, 7,	0x61, 0, 2,		0},
+	{FLTBP2,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x32, 7, 7,		0},
+	{FLTBP1,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x32, 6, 6,		0},
+	{BLK_H,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x64, 0, 6,		0},
+	{FBA_INC,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6B,0, 7,	0x6C, 0, 3,		0},
+	{SFM,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6A, 0, 0,		0},
+	{THREN,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6A, 2, 2,		0},
+	{THRRL,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6D,0, 7,	0x6E, 0, 2,		0},
+	{WRFAST,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x6E, 3, 3,		0},
+	{CHG_HL,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2F, 7, 7,		0},
+	{HINCA,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2A,0, 7,	0x2B, 0, 2,		4},
+	{HINCB,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2C,0, 7,	0x2D, 0, 2,		4},
+	{VINCA,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E,0, 7,	0x2F, 0, 2,		4},
+	{VINCB,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x30,0, 7,	0x31, 0, 2,		4},
+	{HDINCA,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x32,0, 7,	0x33, 0, 2,		4},
+	{HDINCB,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x34,0, 7,	0x35, 0, 2,		4},
+	{HINC,			  NOOP_INDEX,  0, 0,	0x36,0, 4,	0x37,0, 7,	0x38, 0, 7,		4},
+	{VINC,			  NOOP_INDEX,  0, 0,	0x39,0, 7,	0x3A,0, 7,  0x3B, 0, 7,		4},
+	{HDINC,		      NOOP_INDEX,  0, 0,	0x3C,0, 7,	0x3D,0, 7,	0x3E, 0, 7,		4},
+	{VSMST,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x70, 6, 7,		0},
+	{MEMINIT,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0A, 7, 7,		0},
+	{STOP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0A, 4, 4,		0},
+	
+	{HDMI_PD,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x09, 0, 0,		0},
+	{I2S_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 6, 6,		0},
+	{SPDIF_PD,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 0, 0,		0},
+	{DRI_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 3, 3,		0},
+	{DRI_PD_SER,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x16, 0, 0,		1},
+	{DRI_PD_PLL,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x16, 3, 3,		1}, 
+	{DRI_PDDRI,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x08, 4, 7,		0},
+	{CEC_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 4, 4,		0},
+	{PD_DDC,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0E, 7, 7,		1},
+	
+	{LVDS_PD,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0A, 3, 3,		0},
+	{RX_PD,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x63, 4, 7,		4},
+	{RXPLL_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x64, 0, 0,		4},
+    {TXPLL_PD,		  NOOP_INDEX,  0, 0,    NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x66, 1, 1,		4},
+	{TXDRV_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x11, 4, 4,		1},
+	{TXSER_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x1A, 4, 4,		1}, 
+	
+	{VGA_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x09, 2, 2,		0},
+	{PDDAC,		      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x08, 1, 3,		0},
+	{DACSENCE,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x57, 1, 1,		4},
+	{GCKOFF,          NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x09, 6, 6,		0},
+	{TV_BP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x09, 5, 5,		0},
+	{PDPLL1,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 1, 1,		0},
+	{PDPLL0,	      NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x54, 4, 4,		4},
+
+	{ICEN0,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x11, 7, 7,		1},
+
+	{PD_PROM,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0F, 7, 7,		1},
+	{PDMIO,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 2, 2,		0},
+	{PDIO,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 5, 5,		0},
+	{HPD_PD,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 7, 7,		0},
+	{SCLPD,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x09, 4, 4,		0},
+	{SDPD,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x09, 3, 3,		0},
+	{MEMPD,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x0A, 5, 5,		0},
+	{AUDDAC,          NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x5C, 2, 2,		4},
+	
+	{CRYS_FREQ_SPP,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,0x1C,	 0,	7,  0x1D, 0, 7,		0},
+	{I2SCK_SEC, 	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 2, 2,		1},
+	{SP_EN,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x21, 7, 7,		1},
+	{HARD_SOFTB,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x58, 5, 5,		4},
+	{MULT_I2CEN,	  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x4E, 0, 0,		1},
+	{RXPLL_REFDLY,    NOOP_INDEX,  0, 0,    NOOP_INDEX,  0, 0,  NOOP_INDEX,  0, 0,  0x66, 5, 7,     4},
+	{RXPLL_FBDLY,     NOOP_INDEX,  0, 0,    NOOP_INDEX,  0, 0,  NOOP_INDEX,  0, 0,  0x66, 2, 4,     4},
+	{VFMT,            NOOP_INDEX,  0, 0,    NOOP_INDEX,  0, 0,  NOOP_INDEX,  0, 0,  0x2B, 0, 3,     0},
+
+	{DBP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x19, 6, 6,		0},
+	{CK_TVINV,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x07, 1, 1,		1},
+	{DISPON,		  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x08, 0, 0,		1},
+
+	{VP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x39,4, 7,	0x3B, 0, 7,		0},
+	{HP,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x39,0, 3,	0x3A, 0, 7,		0},
+
+	{TXTEN,			  NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x2E, 0, 2,		0},
+	{ZRCTS,           NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	NOOP_INDEX,  0, 0,	0x24, 1, 1,		1},
+	
+};
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.h
new file mode 100755
index 0000000..8e0c18d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_reg_table.h
@@ -0,0 +1,125 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_reg_table.h
+* @version 1.2
+*-----------------------------------------------------------------------------
+*/
+
+#ifndef _CH7036_REG_TABLE_H
+#define _CH7036_REG_TABLE_H
+
+#include "ch7036_typedef.h"
+
+typedef enum{
+	BASE_FLAG = 0,
+
+	
+	HTI,	VTI,	HAI,	VAI,	HOI,	VOI,	HWI,	VWI,	RCLK, 
+	DITHER_SEL1_SPP,		DITHER_SEL0_SPP,		HSYNCP_SPP,		VSYNCP_SPP,
+	NP_INV0_SPP,	NP_INV1_SPP,	NP_INV2_SPP,	NP_INV3_SPP,	LVDS_IN_ORDER,
+
+	
+	LVDS0_SEQ_SPP,	LVDS1_SEQ_SPP,	LVDS2_SEQ_SPP,	LVDS3_SEQ_SPP,	LVDSCLK_SEQ_SPP,
+	HSYNC_CNT_TH_SPP,				PRBS_SET_SEL_SPP,				
+	LVDS0_POL_SPP,	LVDS1_POL_SPP,	LVDS2_POL_SPP,	LVDS3_POL_SPP,	LVDSCLK_POL_SPP,		
+
+	
+	HTO,	VTO,	HAO,	VAO,	HOO_HDMI,		VOO_HDMI,		HWO_HDMI,	VWO_HDMI,	
+	HPO_I,	VPO_I,	DEPO_I,	HPO_O,	VPO_O,			DEPO_O,	
+
+	
+	HD_DVIB,		INTLC,			HD_LV_POL,		HD_LV_SEQ,		HDMI_LVDS_SEL, 
+	VSP,			HSP,			HDMIIN_HSP,		HDMIIN_VSP,		HDMIIN_DEP,
+	M1M0,			C1C0,			VIC,
+	COPY,			SCAN_EN,		HAO_SCL,		VAO_SCL,
+
+	
+	I2SPOL,			I2S_SPDIFB,		I2S_LENGTH,		I2SFMT,		
+	UCLK,			PCLK_NUM,
+
+	
+	UCLKSEC,		PLL1N1,			PLL1N2,			PLL1N3,			PLL3N8,		A2,			MCLK, 
+	PLL2N5,			PLL2N54,		PLL2N53,		PLL2N6,			PLL2N7,		DIVXTAL,
+	A1,				A3,				TXPLL_FFD,		DRI_PLL_N1,		DRI_PLL_N3, UCLKOD_SEL,
+	GCKSEL,         TSTEN1, 
+
+	
+	LNSEL,			DAT16_32B,		TRUE24,			TRUE_COM,		WRLEN,	
+	ROTATE,			HFLIP,			VFLIP,
+	DNSMPEN,		HADWSPP,		FLTBP2,			FLTBP1,			BLK_H,
+	FBA_INC,		SFM,			THREN,			THRRL,			WRFAST,		CHG_HL,
+	HINCA,			HINCB,			VINCA,			VINCB,			HDINCA,		HDINCB,
+	HINC,			VINC,			HDINC,
+	VSMST,			MEMINIT,		STOP,						
+	
+	
+	HDMI_PD,		I2S_PD,			SPDIF_PD,		DRI_PD,			DRI_PD_SER,
+	DRI_PD_PLL,		DRI_PDDRI,		CEC_PD,			PD_DDC,	
+	LVDS_PD,		RX_PD,			RXPLL_PD,		TXPLL_PD,		TXDRV_PD,	TXSER_PD,	
+	VGA_PD,			PDDAC,			DACSENCE,       GCKOFF, 
+	TV_BP,			PDPLL1,			PDPLL0,			ICEN0,			PD_PROM,		PDMIO,		PDIO,		HPD_PD,
+	SCLPD,			SDPD,			MEMPD,			AUDDAC,
+
+	
+	CRYS_FREQ_SPP,	I2SCK_SEC,		SP_EN,			HARD_SOFTB,		MULT_I2CEN,	RXPLL_REFDLY,
+	RXPLL_FBDLY, VFMT,
+
+	
+	DBP,			CK_TVINV,		DISPON,
+	
+	VP,	HP,
+	
+	TXTEN,
+	ZRCTS,
+	
+	MUL_ID_END,
+	
+}MULTI_REG_ID;
+
+typedef struct{
+	MULTI_REG_ID RegId;
+	uint8 TopRegIndex;
+	uint8 TopStartBit;
+	uint8 TopEndBit;
+	uint8 HighRegIndex;
+	uint8 HighStartBit;
+	uint8 HighEndBit;
+	uint8 MiddleRegIndex;
+	uint8 MiddleStartBit;
+	uint8 MiddleEndBit;
+	uint8 LowRegIndex;
+	uint8 LowStartBit;
+	uint8 LowEndBit;
+	uint8 PageIndex;
+}MULTI_REG, *PMULTI_REG;
+
+extern MULTI_REG g_MultiRegTable[];
+
+//issue- 11/15/11- thua 
+//Intel C compiler complained negative constant (-1) comparison to unsigned char for checked build verion
+//for free build, these warnings are treated as compiling error
+//solution:
+//define NOOP_REGINDEX as 2-complement of -1
+#define NOOP_INDEX 0xFF
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_typedef.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_typedef.h
new file mode 100755
index 0000000..57c987b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/ch7036_typedef.h
@@ -0,0 +1,472 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  ch7036_typedef.h
+* @version 1.2
+*-----------------------------------------------------------------------------
+*/
+
+
+#ifndef _CH7036_TYPEDEF_H
+#define _CH7036_TYPEDEF_H
+
+
+
+typedef unsigned char				uint8;
+typedef unsigned short				uint16;
+typedef unsigned int				uint32;
+
+
+
+typedef unsigned long long int 		uint64;
+
+typedef signed char					int8;
+typedef signed short				int16;
+typedef signed int					int32;
+
+typedef signed long long int		int64;
+
+
+
+
+
+
+
+typedef uint32					ch_bool;
+#define ch_true					1
+#define ch_false				0
+
+
+
+typedef struct{
+	uint16 ht;
+	uint16 ha;
+	uint16 ho;
+	uint16 hw;
+	uint16 vt;
+	uint16 va;
+	uint16 vo;
+	uint16 vw;
+	uint16 hz;  
+	uint16 stype; 
+
+}TIMING, *PTIMING;
+
+
+#define PIXEL_FMT_18BIT		0
+#define PIXEL_FMT_24BIT		1
+
+#define POL_HIGH			1
+#define POL_LOW				0
+#define POL_INVERT			1
+#define POL_NO_INV			0
+
+#define AUDIO_I2S			1
+#define AUDIO_SPDIF			0
+
+#define SCANTYPE_INTERLACED		0
+#define SCANTYPE_PROGRESSIVE	1
+#define TIMING_LIST_END			2
+
+typedef struct{
+	
+	TIMING timing;
+	uint32 rx_clk_khz;
+	uint8  pixel_fmt;	
+	uint8  hs_pol;		
+	uint8  vs_pol;
+	uint8  de_pol;
+	uint8  data_ch_pol;	
+	uint8  data_ch_invert;
+	
+	uint8  audio_type;	
+	uint8  i2s_pol;
+	uint8  i2s_len;
+	uint8  i2s_fmt;
+}INPUT_INFO, *PINPUT_INFO;
+
+
+
+
+#define LVDS_DATA0_SEL			0
+#define LVDS_DATA1_SEL			1
+#define LVDS_DATA2_SEL			2
+#define LVDS_DATA3_SEL			3
+#define LVDS_CLOCK_SEL			4
+
+
+#define LVDS_CHANNEL_SWAP_DEF		(LVDS_DATA0_SEL << 0) | (LVDS_DATA1_SEL << 4) | (LVDS_DATA2_SEL << 8) | (LVDS_DATA3_SEL << 12) | (LVDS_CLOCK_SEL << 16)
+#define LVDS_CHANNEL_SWAP_OP1		(LVDS_DATA1_SEL << 0) | (LVDS_DATA2_SEL << 4) | (LVDS_DATA3_SEL << 8) | (LVDS_CLOCK_SEL << 12) | (LVDS_DATA0_SEL << 16)
+#define LVDS_CHANNEL_SWAP_OP2		(LVDS_DATA2_SEL << 0) | (LVDS_DATA3_SEL << 4) | (LVDS_CLOCK_SEL << 8) | (LVDS_DATA0_SEL << 12) | (LVDS_DATA1_SEL << 16)
+#define LVDS_CHANNEL_SWAP_OP3		(LVDS_DATA3_SEL << 0) | (LVDS_CLOCK_SEL << 4) | (LVDS_DATA0_SEL << 8) | (LVDS_DATA1_SEL << 12) | (LVDS_DATA2_SEL << 16)
+#define LVDS_CHANNEL_SWAP_OP4		(LVDS_CLOCK_SEL << 0) | (LVDS_DATA0_SEL << 4) | (LVDS_DATA1_SEL << 8) | (LVDS_DATA2_SEL << 12) | (LVDS_DATA3_SEL << 16)
+#define LVDS_CHANNAL_SWAP_OP5		(LVDS_DATA3_SEL << 0) | (LVDS_CLOCK_SEL << 4) | (LVDS_DATA2_SEL << 8) | (LVDS_DATA1_SEL << 12) | (LVDS_DATA0_SEL << 16)
+
+typedef struct{
+	uint32 channel_swap;	
+	uint32 channel_pol;		
+	uint32 pixel_fmt;		
+}LVDS_FMT, *PLVDS_FMT;
+
+#define AS_RATIO_4_3		1
+#define AS_RATIO_16_9		2
+
+typedef struct{
+	uint8 is_dvi_mode;		
+	uint8 format_index;		
+	uint8 aspect_ratio;		
+	uint8 channel_swap;		
+	uint8 data_pol_invert;	
+	uint8 hs_pol;			
+	uint8 vs_pol;			
+	uint8 protect_enable;	
+}HDMI_FMT, *PHDMI_FMT;
+
+typedef struct{
+	uint8 channel_swap;		
+	uint8 hs_pol;			
+	uint8 vs_pol;			
+	uint8 de_pol;			
+}VGA_FMT, *PVGA_FMT;
+
+
+#define CHANNEL_LVDS		(1 << 0)
+#define CHANNEL_HDMI		(1 << 1)
+#define CHANNEL_VGA			(1 << 2)
+#define CHANNEL_DVI			(1 << 3)
+#define CHANNEL_LVDS_HDMI 	(CHANNEL_LVDS | CHANNEL_HDMI)
+#define CHANNEL_LVDS_VGA 	(CHANNEL_LVDS | CHANNEL_VGA)
+#define CHANNEL_LVDS_DVI 	(CHANNEL_LVDS | CHANNEL_DVI)  
+#define CHANNEL_AUTO_DETECT	0x10 
+
+
+#define CHANNEL_LVDS_HDMI_VGA_OFF	0x0000  
+
+
+
+#define ROTATE_0			0
+#define ROTATE_NO			0
+#define ROTATE_90			1
+#define ROTATE_180			2
+#define ROTATE_270			3
+
+typedef struct{
+	uint32 channel;			
+	LVDS_FMT lvds_fmt;		
+	HDMI_FMT hdmi_fmt;		
+	VGA_FMT vga_fmt;		
+	TIMING timing;			
+	uint32 uclk_khz;		
+	uint8  ds_percent_h;	
+	uint8  ds_percent_v;	
+	uint8  rotate;			
+	uint8  h_flip;			
+	uint8  v_flip;			
+	uint16  h_position;			
+	uint16  v_position;			
+}OUTPUT_INFO, *POUTPUT_INFO;
+
+
+#define HS_TOLERANCE_LEVEL0			0
+#define HS_TOLERANCE_LEVEL1			1
+#define HS_TOLERANCE_LEVEL2			3
+#define HS_TOLERANCE_LEVEL3			7
+
+#define RST_BIT_HSYNC				0
+#define RST_BIT_VSYNC				1
+
+#define MEM_CLK_FREQ_MAX			166000	
+#define FBA_INC_MAX					3000	
+
+#define THRRL_ADJUST_DEF			200	
+
+#define THRRL_ADJUST_OP1			250		
+#define THRRL_ADJUST_OP2			300		
+
+typedef struct{
+	uint32 mclk_khz;		
+	
+	uint8 uclkod_sel;		
+	uint8 dat16_32b;		
+	uint8 true24;			
+	uint8 true_com;			
+	
+	uint8 lvds_out_hs_tolerance;	
+	uint8 lvds_out_reset_bit_sel;	
+	
+	uint8 dither_filter_enable;		
+	uint8 hscale_ratio_gate;		
+	
+	uint8 scale_line_adjust;		
+	uint8  text_enhancement;	
+			
+	uint8 pll_ref_dly;
+	uint8 pll_ref_fbdly;
+	uint8 lvds_txdrv_ctrl;
+
+	uint8 eye_bgtrim ;
+	uint8 eye_dacg;
+	uint8 eye_dri_demp;
+	uint8 eye_dri_pll_cp;
+	uint8 eye_dri_damp;
+	uint8 eye_dri_pll_rlf;
+	uint8 eye_rdac;
+	
+	uint8 reset; 
+	uint8 vga_enable;
+
+}PREFER_INFO, *PPREFER_INFO;
+
+
+
+typedef struct{
+	uint32 DeviceID;
+	INPUT_INFO* pInput_Info;
+	OUTPUT_INFO* pOutput_Info;
+	PREFER_INFO* pPrefer_Info;
+	void*	pd_context; 
+}DEV_CONTEXT, *PDEV_CONTEXT;
+
+typedef struct{
+	uint32 fmt_index;
+	uint32 clk_freq;
+	uint32 aspect;
+	TIMING timing;
+}OUT_FMT, *POUT_FMT;
+
+typedef enum hdmi{
+
+	OUT_HDMI_640x480P_59 = 0,
+	OUT_HDMI_640x480P_60,
+
+	OUT_HDMI_720x480P_59,
+	OUT_HDMI_720x480P_60, 
+
+	OUT_HDMI_720x576P_50,
+
+	OUT_HDMI_1280x720P_59,
+	OUT_HDMI_1280x720P_60, 
+
+	OUT_HDMI_1920x1080I_59, 
+	OUT_HDMI_1920x1080I_60, 
+
+	OUT_HDMI_1920x1080P_59,
+	OUT_HDMI_1920x1080P_60, 
+
+//	OUT_HDMI_720x576P_50, //2/2/12 grouped to downscaling modes < 13x7
+	OUT_HDMI_1280x720P_50,
+	OUT_HDMI_1920x1080I_50,
+	
+	OUT_HDMI_1920x1080P_50,
+
+	OUT_HDMI_1920x1080P_23,
+	OUT_HDMI_1920x1080P_24,
+	OUT_HDMI_1920x1080P_25,
+	OUT_HDMI_1920x1080P_29,
+	OUT_HDMI_1920x1080P_30,
+
+	OUT_HDMI_1920x1080I_100,
+	OUT_HDMI_1280x720P_100,
+	OUT_HDMI_720x576P_100,
+	
+	OUT_HDMI_1920x1080I_119,
+	OUT_HDMI_1920x1080I_120,
+    
+	OUT_HDMI_1280x720P_119,
+	OUT_HDMI_1280x720P_120,
+
+	OUT_HDMI_720x480P_119,
+	OUT_HDMI_720x480P_120,
+
+	OUT_HDMI_720x576P_200,
+    OUT_HDMI_1280x720P_239,
+	OUT_HDMI_1280x720P_240, 
+	OUT_HDMI_END,
+}HDMI_OUT_MODE;
+
+typedef enum dvi {
+
+	OUT_DVI_640x480_60 = 0,
+
+	OUT_DVI_640x480_72,
+	OUT_DVI_640x480_75,
+	OUT_DVI_720x400_70,
+
+	OUT_DVI_800x600_56,
+    OUT_DVI_800x600_60,
+	OUT_DVI_800x600_72,
+	OUT_DVI_800x600_75,
+
+	OUT_DVI_1024x768_60,
+	OUT_DVI_1024x768_70,
+	OUT_DVI_1024x768_75,
+
+    OUT_DVI_1152x864_60,
+    OUT_DVI_1280x720_60,
+	OUT_DVI_1280x800_60,
+	OUT_DVI_1280x960_60,
+	OUT_DVI_1280x1024_60,
+	OUT_DVI_1280x1024_75,
+	OUT_DVI_1360x768_60,
+	OUT_DVI_1366x768_60,
+
+	OUT_DVI_1400x1050_60,
+	OUT_DVI_1400x1050_75, 
+	
+
+	OUT_DVI_1440x900_60,
+	OUT_DVI_1440x1050_60,
+	OUT_DVI_1600x900_60,
+	OUT_DVI_1600x1200_60,
+	OUT_DVI_1680x1050_60,
+
+	OUT_DVI_1920x1080_60, 
+
+	OUT_DVI_1920x1200_60, 
+
+	OUT_DVI_END,
+} DVI_OUT_MODE;
+
+typedef enum crt{
+
+	OUT_CRT_640x400_85 = 0,
+
+	OUT_CRT_640x480_60,
+	
+	OUT_CRT_640x480_72,
+	OUT_CRT_640x480_75,
+	OUT_CRT_640x480_85,
+	
+	OUT_CRT_720x400_85,
+
+	OUT_CRT_800x600_56,
+
+	    
+	OUT_CRT_800x600_60, 
+	OUT_CRT_800x600_72,
+	OUT_CRT_800x600_75,
+	OUT_CRT_800x600_85, 
+
+	OUT_CRT_1024x768_60,
+	OUT_CRT_1024x768_70,
+	OUT_CRT_1024x768_75, 
+	OUT_CRT_1024x768_85, 
+	
+
+	
+	OUT_CRT_1152x864_75,
+	
+	OUT_CRT_1280x768_60,  
+	OUT_CRT_1280x768_75,  
+	OUT_CRT_1280x768_85,  
+	
+	OUT_CRT_1280x960_60,
+	OUT_CRT_1280x960_85, 
+
+	OUT_CRT_1280x1024_60,
+	OUT_CRT_1280x1024_75,
+	OUT_CRT_1280x1024_85, 
+
+	OUT_CRT_1360x768_60,   
+
+	OUT_CRT_1400x1050_60,
+	OUT_CRT_1400x1050_75,
+	
+
+
+	OUT_CRT_1440x900_60,
+	OUT_CRT_1440x1050_60,
+
+	OUT_CRT_1600x900_60, 
+	OUT_CRT_1600x1200_60,  
+	
+	OUT_CRT_1920x1080_60, 
+
+	
+
+	OUT_CRT_END,
+}CRT_OUT_MODE;
+
+#define MAX_ATTR_LIST_SIZE 32
+
+
+
+
+#define ERR_NO_ERROR						0
+
+#define ERR_DEVICE_PREPARE					100
+#define ERR_RESOLUTION_H_ODD				101
+#define ERR_RESOLUTION_V_ODD				102
+#define ERR_BANDWIDTH_OVERFLOW				103
+#define ERR_RESOLUTION_OVERFLOW				104
+#define ERR_DEVICE_NO_EXIST					105
+#define ERR_NO_SUPPORT_TRUE24				106
+#define ERR_NO_SUPPORT_TRUECOM				107
+#define ERR_ROTATION_WITH_VAI				108 
+#define ERR_FLIP_WITH_VAI					109
+
+#define ERR_DEVICE_CONFIG					200
+#define ERR_ASPECT_RATIO_NOMATCH			201
+#define ERR_PLL1N2_WRONG					202
+#define ERR_PLL1N3_WRONG					203
+#define ERR_PLL1N1_WRONG					204
+#define ERR_PLL3N8_WRONG					205
+#define ERR_PLL2N5_WRONG					206
+#define ERR_PLL2N6_WRONG					207
+#define ERR_PLL2N7_WRONG					208
+#define ERR_DMXTAL_WRONG					209
+#define ERR_HDMI_CLOCK_NO_SUPPORT			210	
+
+#define ERR_DEVICE_RUNNING					300
+#define ERR_CAL_INC_DIV_ZERO				301		
+#define	ERR_CAL_INC_NO_MATH					302
+
+
+
+typedef enum {
+    SS_POWER_ON_STATE,       
+    SS_SUCCESS,              
+    SS_NOT_SUPPORTED,        
+    SS_INVALID_ARGUMENT,     
+    SS_PENDING,              
+    SS_TARGET_UNSPECIFIED,   
+    SS_SCALING_UNSUPPORTED,  
+
+	SS_MEM_ALLOC_ERR,
+
+    
+    SS_UNSUCCESSFUL = 10,    
+    SS_INVALID_RETURN,       
+    SS_WRITE_FAILED,         
+    SS_READ_FAILED,          
+
+	SS_FIRMWARE_ERR,
+	SS_FIRMWARE_TIMEOUT,
+
+	SS_CRT_HPD_NOTCONNECTED,
+	SS_CRT_HPD_CONNECTED_TO_GRD,
+	SS_DISPLAY_CHOICE_NOT_ALLOWED,
+
+
+} ch7036_status_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/config_.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/config_.h
new file mode 100644
index 0000000..99949e8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/config_.h
@@ -0,0 +1,73 @@
+/*-----------------------------------------------------------------------------
+* Copyright (c) Chrontel Inc.
+*
+* 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 shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+*-----------------------------------------------------------------------------
+* @file  config_.h
+* @version 1.2.3
+*-----------------------------------------------------------------------------
+*/
+
+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+
+#define T_LVDS_OPEN
+#define T_LVDS_INIT
+#define T_POST_SETMODE
+#define T_LVDS_CLOSE
+
+#define T_SET_TIMING
+
+#define T_PD_DISPLAY_LVDS_INT 
+
+#define T_DEVICE_DETECTION 
+#define T_RES_DEALLOC
+
+#define T_TEST_HW
+#define T_RESET
+
+
+#define T_HDMI_TIMING_TABLE
+#define T_VGA_TIMING_TABLE
+#define T_PANEL_NATIVE_DTD
+
+
+#define T_LH_POWER_ON_OFF
+
+#define T_CH7036_USE_LVDS_ATTR_TABLE
+
+#define T_CH7036_INIT_ATTR_TABLE
+
+
+#define T_CH7036_USE_FIRMWARE
+
+#define T_CH7036_EDID_DUMP
+#undef T_CH7036_EDID_DUMP
+
+#define T_SHOW_EDID_DISPLAY_ATTR
+#undef T_SHOW_EDID_DISPLAY_ATTR
+
+#define T_LINUX
+//#undef T_LINUX
+
+#define T_CONFIG_PLB
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/edid7036.car b/drivers/gpu/drm/emgd/emgd/pal/ch7036/edid7036.car
new file mode 100644
index 0000000..b7da904
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/edid7036.car
@@ -0,0 +1,3362 @@
+unsigned char lhfm_array[]= {
+	0x02,
+	0x0D,
+	0x14,
+	0x02,
+	0x0A,
+	0xBC,
+	0xFF,
+	0xFF,
+	0xFF,
+	0xFF,
+	0xFF,
+	0x02,
+	0x09,
+	0x83,
+	0x75,
+	0x50,
+	0x66,
+	0xE4,
+	0xF5,
+	0x26,
+	0xF5,
+	0x27,
+	0xC2,
+	0x01,
+	0xF5,
+	0x25,
+	0xF5,
+	0x28,
+	0xFF,
+	0x12,
+	0x09,
+	0xF4,
+	0x40,
+	0x01,
+	0x22,
+	0xE5,
+	0x50,
+	0x24,
+	0x3E,
+	0xF8,
+	0xE6,
+	0xF5,
+	0x4A,
+	0x7B,
+	0x02,
+	0x7D,
+	0x23,
+	0xE4,
+	0xFF,
+	0x12,
+	0x09,
+	0x15,
+	0x40,
+	0x01,
+	0x22,
+	0xA8,
+	0x50,
+	0xE6,
+	0x30,
+	0xE0,
+	0x03,
+	0x43,
+	0x28,
+	0x01,
+	0xE8,
+	0x04,
+	0xF8,
+	0xE6,
+	0xFF,
+	0x54,
+	0x0E,
+	0x60,
+	0x03,
+	0x43,
+	0x28,
+	0x02,
+	0xEF,
+	0x30,
+	0xE0,
+	0x03,
+	0x43,
+	0x28,
+	0x04,
+	0x75,
+	0x4B,
+	0x26,
+	0xE4,
+	0xF5,
+	0x4F,
+	0x7B,
+	0x02,
+	0xAD,
+	0x4B,
+	0xE4,
+	0xFF,
+	0x12,
+	0x09,
+	0x15,
+	0x50,
+	0x36,
+	0x05,
+	0x4B,
+	0x05,
+	0x4B,
+	0xA8,
+	0x50,
+	0xE6,
+	0xFF,
+	0xB4,
+	0x01,
+	0x08,
+	0xE8,
+	0x04,
+	0xF8,
+	0xE6,
+	0x64,
+	0x01,
+	0x60,
+	0x23,
+	0xE5,
+	0x50,
+	0x04,
+	0xF8,
+	0x74,
+	0xC0,
+	0x56,
+	0xF6,
+	0xEF,
+	0xB4,
+	0x81,
+	0x09,
+	0xE5,
+	0x50,
+	0x04,
+	0xF8,
+	0x76,
+	0x80,
+	0x43,
+	0x28,
+	0x04,
+	0xA8,
+	0x50,
+	0xE6,
+	0xB4,
+	0xA9,
+	0x08,
+	0xE8,
+	0x04,
+	0xF8,
+	0x76,
+	0x40,
+	0x43,
+	0x28,
+	0x10,
+	0x05,
+	0x4F,
+	0xE5,
+	0x4F,
+	0xC3,
+	0x94,
+	0x08,
+	0x40,
+	0xB6,
+	0x7B,
+	0x06,
+	0x7D,
+	0x38,
+	0xE4,
+	0xFF,
+	0x12,
+	0x09,
+	0x15,
+	0x40,
+	0x03,
+	0x02,
+	0x01,
+	0x30,
+	0xE5,
+	0x50,
+	0x24,
+	0x02,
+	0xF8,
+	0xE6,
+	0x54,
+	0xF0,
+	0xFF,
+	0xE5,
+	0x50,
+	0x04,
+	0xF8,
+	0xA6,
+	0x07,
+	0xE5,
+	0x50,
+	0x24,
+	0x03,
+	0xF8,
+	0xE6,
+	0xFF,
+	0xE5,
+	0x50,
+	0x24,
+	0x02,
+	0xF8,
+	0xA6,
+	0x07,
+	0xE5,
+	0x50,
+	0x24,
+	0x05,
+	0xF8,
+	0xE6,
+	0x54,
+	0xF0,
+	0xFF,
+	0xE5,
+	0x50,
+	0x24,
+	0x03,
+	0xF8,
+	0xA6,
+	0x07,
+	0xE4,
+	0xF5,
+	0x4F,
+	0xE4,
+	0xF5,
+	0x4C,
+	0xE5,
+	0x4F,
+	0x75,
+	0xF0,
+	0x05,
+	0xA4,
+	0x24,
+	0xBA,
+	0xF5,
+	0x82,
+	0xE4,
+	0x34,
+	0x05,
+	0xF5,
+	0x83,
+	0xE5,
+	0x82,
+	0x25,
+	0x4C,
+	0xF5,
+	0x82,
+	0xE4,
+	0x35,
+	0x83,
+	0xF5,
+	0x83,
+	0xE4,
+	0x93,
+	0xFF,
+	0xE5,
+	0x50,
+	0x25,
+	0x4C,
+	0xF8,
+	0xE6,
+	0xB5,
+	0x07,
+	0x07,
+	0x05,
+	0x4C,
+	0xE5,
+	0x4C,
+	0xB4,
+	0x04,
+	0xD3,
+	0xE5,
+	0x4C,
+	0xB4,
+	0x04,
+	0x13,
+	0xE5,
+	0x4F,
+	0x75,
+	0xF0,
+	0x05,
+	0xA4,
+	0x24,
+	0xBF,
+	0xF5,
+	0x82,
+	0xE4,
+	0x34,
+	0x05,
+	0xF5,
+	0x83,
+	0xE4,
+	0x93,
+	0x42,
+	0x28,
+	0x05,
+	0x4F,
+	0xE5,
+	0x4F,
+	0xB4,
+	0x03,
+	0xB1,
+	0xD2,
+	0x04,
+	0xE5,
+	0x4A,
+	0x70,
+	0x02,
+	0xC3,
+	0x22,
+	0x75,
+	0x4F,
+	0x01,
+	0xE5,
+	0x4F,
+	0xD3,
+	0x95,
+	0x4A,
+	0x40,
+	0x03,
+	0x02,
+	0x02,
+	0x61,
+	0xE5,
+	0x4F,
+	0x30,
+	0xE0,
+	0x04,
+	0x7F,
+	0x80,
+	0x80,
+	0x02,
+	0x7F,
+	0x00,
+	0x8F,
+	0x51,
+	0xAF,
+	0x4F,
+	0x12,
+	0x09,
+	0xF4,
+	0x40,
+	0x01,
+	0x22,
+	0xE5,
+	0x4F,
+	0xC3,
+	0x13,
+	0xF5,
+	0x4E,
+	0x7B,
+	0x05,
+	0xAD,
+	0x51,
+	0xFF,
+	0x12,
+	0x09,
+	0x15,
+	0x40,
+	0x01,
+	0x22,
+	0xA8,
+	0x50,
+	0xE6,
+	0x64,
+	0x02,
+	0x60,
+	0x03,
+	0x02,
+	0x02,
+	0x5C,
+	0xE8,
+	0x04,
+	0xF8,
+	0xE6,
+	0x64,
+	0x03,
+	0x60,
+	0x03,
+	0x02,
+	0x02,
+	0x5C,
+	0xE5,
+	0x50,
+	0x24,
+	0x02,
+	0xF8,
+	0xE6,
+	0xFF,
+	0xD3,
+	0x94,
+	0x04,
+	0x50,
+	0x03,
+	0x02,
+	0x02,
+	0x5C,
+	0x8F,
+	0x4B,
+	0xE5,
+	0x50,
+	0x24,
+	0x04,
+	0xF8,
+	0xE6,
+	0xF5,
+	0x4C,
+	0x75,
+	0x4D,
+	0x04,
+	0xE5,
+	0x4C,
+	0x54,
+	0xE0,
+	0x64,
+	0x60,
+	0x70,
+	0x37,
+	0xE5,
+	0x4D,
+	0x7B,
+	0x06,
+	0x12,
+	0x09,
+	0x10,
+	0x40,
+	0x01,
+	0x22,
+	0xE5,
+	0x50,
+	0x04,
+	0xF8,
+	0xE6,
+	0x64,
+	0x03,
+	0x70,
+	0x24,
+	0xE5,
+	0x50,
+	0x24,
+	0x02,
+	0xF8,
+	0xE6,
+	0x64,
+	0x0C,
+	0x70,
+	0x1A,
+	0xE5,
+	0x50,
+	0x24,
+	0x03,
+	0xF8,
+	0xE6,
+	0x70,
+	0x12,
+	0xE5,
+	0x50,
+	0x24,
+	0x04,
+	0xF8,
+	0xE6,
+	0xF5,
+	0x26,
+	0xE5,
+	0x50,
+	0x24,
+	0x05,
+	0xF8,
+	0xE6,
+	0xF5,
+	0x27,
+	0xD2,
+	0x01,
+	0xE5,
+	0x4C,
+	0x54,
+	0xE0,
+	0x64,
+	0x40,
+	0x70,
+	0x59,
+	0xE5,
+	0x4C,
+	0x54,
+	0x1F,
+	0xF5,
+	0x52,
+	0xE5,
+	0x4D,
+	0x25,
+	0x51,
+	0x04,
+	0xFD,
+	0xAB,
+	0x52,
+	0xAF,
+	0x4E,
+	0x12,
+	0x09,
+	0x15,
+	0x50,
+	0x64,
+	0xE4,
+	0xF5,
+	0x53,
+	0xE5,
+	0x53,
+	0xC3,
+	0x95,
+	0x52,
+	0x50,
+	0x3A,
+	0xE4,
+	0xF5,
+	0x54,
+	0xE5,
+	0x50,
+	0x25,
+	0x53,
+	0xF8,
+	0xE6,
+	0x54,
+	0x7F,
+	0xFF,
+	0xE5,
+	0x54,
+	0x25,
+	0xE0,
+	0x24,
+	0xA6,
+	0xF5,
+	0x82,
+	0xE4,
+	0x34,
+	0x05,
+	0xF5,
+	0x83,
+	0xE4,
+	0x93,
+	0xB5,
+	0x07,
+	0x11,
+	0xE5,
+	0x54,
+	0x25,
+	0xE0,
+	0x24,
+	0xA7,
+	0xF5,
+	0x82,
+	0xE4,
+	0x34,
+	0x05,
+	0xF5,
+	0x83,
+	0xE4,
+	0x93,
+	0x42,
+	0x25,
+	0x05,
+	0x54,
+	0xE5,
+	0x54,
+	0xB4,
+	0x0A,
+	0xCD,
+	0x05,
+	0x53,
+	0x80,
+	0xBF,
+	0xE5,
+	0x4C,
+	0x54,
+	0x1F,
+	0x25,
+	0x4D,
+	0x04,
+	0xF5,
+	0x4D,
+	0x7B,
+	0x01,
+	0x12,
+	0x09,
+	0x10,
+	0x40,
+	0x01,
+	0x22,
+	0xA8,
+	0x50,
+	0xE6,
+	0xF5,
+	0x4C,
+	0xE5,
+	0x4D,
+	0xC3,
+	0x95,
+	0x4B,
+	0x50,
+	0x03,
+	0x02,
+	0x01,
+	0x9C,
+	0x05,
+	0x4F,
+	0x02,
+	0x01,
+	0x3B,
+	0xA2,
+	0x01,
+	0x22,
+	0xE4,
+	0xF5,
+	0xA8,
+	0xF5,
+	0xA0,
+	0x75,
+	0x90,
+	0xC0,
+	0xD2,
+	0x8A,
+	0xD2,
+	0x88,
+	0xD2,
+	0xA8,
+	0xD2,
+	0xB8,
+	0x7D,
+	0x40,
+	0x7F,
+	0x4F,
+	0x12,
+	0x06,
+	0x63,
+	0xC2,
+	0x0D,
+	0xC2,
+	0x04,
+	0xC2,
+	0x07,
+	0xC2,
+	0x0F,
+	0xC2,
+	0x02,
+	0xE4,
+	0xF5,
+	0x47,
+	0xD2,
+	0x0A,
+	0xD2,
+	0x00,
+	0xD2,
+	0x09,
+	0xD2,
+	0x0C,
+	0xC2,
+	0x05,
+	0xC2,
+	0x08,
+	0xC2,
+	0x0B,
+	0x7E,
+	0x05,
+	0x7F,
+	0x7B,
+	0x12,
+	0x06,
+	0x8A,
+	0x7F,
+	0x0A,
+	0x12,
+	0x0C,
+	0xB4,
+	0x12,
+	0x07,
+	0x69,
+	0x75,
+	0x15,
+	0x19,
+	0x75,
+	0x16,
+	0x19,
+	0x75,
+	0x17,
+	0x0C,
+	0x12,
+	0x0C,
+	0x23,
+	0x7F,
+	0x0A,
+	0x12,
+	0x0C,
+	0x92,
+	0xE5,
+	0x47,
+	0x24,
+	0xFF,
+	0x92,
+	0x05,
+	0x7D,
+	0xC0,
+	0x7F,
+	0x6E,
+	0x12,
+	0x06,
+	0x75,
+	0xAF,
+	0x47,
+	0x12,
+	0x0B,
+	0xF9,
+	0xC2,
+	0x06,
+	0x7D,
+	0x40,
+	0x7F,
+	0x4F,
+	0x12,
+	0x06,
+	0x4E,
+	0x75,
+	0x23,
+	0x00,
+	0x75,
+	0x24,
+	0x01,
+	0xE4,
+	0xF5,
+	0x46,
+	0x12,
+	0x06,
+	0xC6,
+	0x30,
+	0x0D,
+	0x05,
+	0x7F,
+	0xFF,
+	0x12,
+	0x08,
+	0x97,
+	0x30,
+	0x07,
+	0x30,
+	0x20,
+	0x04,
+	0x0C,
+	0x7F,
+	0x25,
+	0x12,
+	0x0C,
+	0x77,
+	0xEF,
+	0x30,
+	0xE4,
+	0x03,
+	0x12,
+	0x00,
+	0x0E,
+	0x7B,
+	0x12,
+	0x7D,
+	0x36,
+	0x7C,
+	0x00,
+	0x7F,
+	0x50,
+	0x12,
+	0x0B,
+	0x9F,
+	0x92,
+	0x10,
+	0x85,
+	0x3A,
+	0x35,
+	0x85,
+	0x25,
+	0x36,
+	0x85,
+	0x28,
+	0x37,
+	0xA2,
+	0x10,
+	0x92,
+	0x13,
+	0x7F,
+	0x0F,
+	0x12,
+	0x0A,
+	0xFF,
+	0xC2,
+	0x07,
+	0xE5,
+	0x16,
+	0x60,
+	0x03,
+	0x02,
+	0x03,
+	0xA3,
+	0x30,
+	0x02,
+	0x2D,
+	0xC2,
+	0xA8,
+	0x7F,
+	0x0A,
+	0x12,
+	0x0C,
+	0x92,
+	0xC2,
+	0x02,
+	0x30,
+	0x05,
+	0x0C,
+	0xE5,
+	0x47,
+	0x60,
+	0x04,
+	0xD2,
+	0x03,
+	0x80,
+	0x0C,
+	0xAF,
+	0x47,
+	0x80,
+	0x05,
+	0xE5,
+	0x47,
+	0x60,
+	0x04,
+	0xFF,
+	0x12,
+	0x0B,
+	0xF9,
+	0xE5,
+	0x47,
+	0x24,
+	0xFF,
+	0x92,
+	0x05,
+	0x75,
+	0x16,
+	0x19,
+	0xD2,
+	0xA8,
+	0x80,
+	0x54,
+	0x20,
+	0x0B,
+	0x1A,
+	0x30,
+	0x05,
+	0x09,
+	0x7F,
+	0x09,
+	0x12,
+	0x0C,
+	0x6F,
+	0xEF,
+	0x13,
+	0x92,
+	0x09,
+	0xD2,
+	0x0C,
+	0x7F,
+	0x08,
+	0x12,
+	0x0C,
+	0x6F,
+	0xEF,
+	0x54,
+	0x0E,
+	0x70,
+	0x02,
+	0xC2,
+	0x0C,
+	0xE5,
+	0x47,
+	0x60,
+	0x22,
+	0x7F,
+	0x09,
+	0x12,
+	0x0C,
+	0x6F,
+	0xEF,
+	0x20,
+	0xE0,
+	0x19,
+	0x7F,
+	0x0A,
+	0x12,
+	0x0C,
+	0x6F,
+	0xEF,
+	0x30,
+	0xE2,
+	0x10,
+	0x7D,
+	0x04,
+	0x7F,
+	0x5C,
+	0x12,
+	0x06,
+	0x6F,
+	0x7D,
+	0x01,
+	0x7F,
+	0x07,
+	0x12,
+	0x06,
+	0x4E,
+	0x80,
+	0x0E,
+	0x7D,
+	0x04,
+	0x7F,
+	0x5C,
+	0x12,
+	0x06,
+	0x5A,
+	0x7D,
+	0x01,
+	0x7F,
+	0x07,
+	0x12,
+	0x06,
+	0x63,
+	0x75,
+	0x16,
+	0x4B,
+	0xE5,
+	0x17,
+	0x70,
+	0x2C,
+	0x7F,
+	0x7F,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x54,
+	0x04,
+	0xF5,
+	0x49,
+	0x65,
+	0x3D,
+	0x60,
+	0x1B,
+	0x85,
+	0x3D,
+	0x49,
+	0x7F,
+	0x0A,
+	0x12,
+	0x0C,
+	0xB4,
+	0xE5,
+	0x3D,
+	0x65,
+	0x49,
+	0x60,
+	0x0D,
+	0xE5,
+	0x3D,
+	0x70,
+	0x03,
+	0xD3,
+	0x80,
+	0x01,
+	0xC3,
+	0x92,
+	0x11,
+	0x12,
+	0x0B,
+	0xCD,
+	0x75,
+	0x17,
+	0x19,
+	0x05,
+	0x24,
+	0xE5,
+	0x24,
+	0xAE,
+	0x23,
+	0x70,
+	0x02,
+	0x05,
+	0x23,
+	0x14,
+	0x4E,
+	0x60,
+	0x03,
+	0x02,
+	0x02,
+	0xDA,
+	0x75,
+	0x13,
+	0x07,
+	0x75,
+	0x14,
+	0xD0,
+	0x05,
+	0x46,
+	0xE5,
+	0x46,
+	0x20,
+	0xE0,
+	0x03,
+	0x02,
+	0x02,
+	0xDA,
+	0x02,
+	0x02,
+	0xDA,
+	0x02,
+	0x05,
+	0x4B,
+	0xEC,
+	0x5D,
+	0x04,
+	0x60,
+	0x05,
+	0xE8,
+	0x59,
+	0x04,
+	0x70,
+	0x03,
+	0x02,
+	0x05,
+	0x41,
+	0x12,
+	0x05,
+	0x0C,
+	0x58,
+	0x04,
+	0x60,
+	0xF6,
+	0xEC,
+	0x48,
+	0x60,
+	0xF2,
+	0xEC,
+	0x70,
+	0x04,
+	0xFD,
+	0xFE,
+	0xFF,
+	0x22,
+	0xC8,
+	0x60,
+	0xDB,
+	0x24,
+	0x81,
+	0xC8,
+	0x50,
+	0x09,
+	0xC3,
+	0x98,
+	0x60,
+	0x02,
+	0x50,
+	0x06,
+	0x02,
+	0x05,
+	0x48,
+	0x98,
+	0x50,
+	0xCA,
+	0xF5,
+	0x82,
+	0xE9,
+	0x29,
+	0x4B,
+	0x4A,
+	0x70,
+	0x05,
+	0xAB,
+	0x82,
+	0x02,
+	0x05,
+	0x37,
+	0x75,
+	0xF0,
+	0x00,
+	0x7C,
+	0x1A,
+	0x78,
+	0x80,
+	0xC3,
+	0xEF,
+	0x9B,
+	0xEE,
+	0x9A,
+	0xED,
+	0x99,
+	0x40,
+	0x0D,
+	0xC3,
+	0xEF,
+	0x9B,
+	0xFF,
+	0xEE,
+	0x9A,
+	0xFE,
+	0xED,
+	0x99,
+	0xFD,
+	0xE8,
+	0x42,
+	0xF0,
+	0xDC,
+	0x23,
+	0xAC,
+	0xF0,
+	0xD0,
+	0xE0,
+	0xFF,
+	0xD0,
+	0xE0,
+	0xFE,
+	0xD0,
+	0xE0,
+	0xFD,
+	0xAB,
+	0x82,
+	0x20,
+	0xE7,
+	0x10,
+	0x1B,
+	0xEB,
+	0x60,
+	0xBA,
+	0xEC,
+	0x2C,
+	0xFC,
+	0xEF,
+	0x33,
+	0xFF,
+	0xEE,
+	0x33,
+	0xFE,
+	0xED,
+	0x33,
+	0xFD,
+	0x02,
+	0x05,
+	0x23,
+	0xE8,
+	0x03,
+	0xF8,
+	0x30,
+	0xE7,
+	0x05,
+	0xC0,
+	0xF0,
+	0x75,
+	0xF0,
+	0x00,
+	0xEF,
+	0x2F,
+	0xFF,
+	0xEE,
+	0x33,
+	0xFE,
+	0xED,
+	0x33,
+	0xFD,
+	0x40,
+	0xB8,
+	0x30,
+	0xE7,
+	0xC2,
+	0x80,
+	0xAA,
+	0x75,
+	0xF0,
+	0x20,
+	0x80,
+	0x0E,
+	0x75,
+	0xF0,
+	0x10,
+	0x80,
+	0x05,
+	0x75,
+	0xF0,
+	0x08,
+	0x7D,
+	0x00,
+	0x7E,
+	0x00,
+	0x7F,
+	0x00,
+	0x33,
+	0x92,
+	0xD5,
+	0x30,
+	0xD5,
+	0x03,
+	0x12,
+	0x05,
+	0x56,
+	0xEC,
+	0x33,
+	0x40,
+	0x10,
+	0xEF,
+	0x33,
+	0xFF,
+	0xEE,
+	0x33,
+	0xFE,
+	0xED,
+	0x33,
+	0xFD,
+	0xEC,
+	0x33,
+	0xFC,
+	0xD5,
+	0xF0,
+	0xED,
+	0x22,
+	0xE5,
+	0xF0,
+	0x24,
+	0x7E,
+	0xA2,
+	0xD5,
+	0x13,
+	0xCC,
+	0x92,
+	0xE7,
+	0xCD,
+	0xCE,
+	0xFF,
+	0x22,
+	0xED,
+	0xD2,
+	0xE7,
+	0xCD,
+	0x33,
+	0xEC,
+	0x33,
+	0x92,
+	0xD5,
+	0x24,
+	0x81,
+	0x40,
+	0x06,
+	0xE4,
+	0xFF,
+	0xFE,
+	0xFD,
+	0xFC,
+	0x22,
+	0xFC,
+	0xE4,
+	0xCF,
+	0xCE,
+	0xCD,
+	0xCC,
+	0x24,
+	0xE0,
+	0x50,
+	0x11,
+	0x74,
+	0xFF,
+	0x80,
+	0xED,
+	0xC3,
+	0xCC,
+	0x13,
+	0xCC,
+	0xCD,
+	0x13,
+	0xCD,
+	0xCE,
+	0x13,
+	0xCE,
+	0xCF,
+	0x13,
+	0xCF,
+	0x04,
+	0x70,
+	0xF0,
+	0x30,
+	0xD5,
+	0xDE,
+	0x02,
+	0x05,
+	0x56,
+	0xE9,
+	0xD2,
+	0xE7,
+	0xC9,
+	0x33,
+	0xE8,
+	0x33,
+	0xF8,
+	0x92,
+	0xD5,
+	0xED,
+	0xD2,
+	0xE7,
+	0xCD,
+	0x33,
+	0xEC,
+	0x33,
+	0xFC,
+	0x50,
+	0x02,
+	0xB2,
+	0xD5,
+	0x22,
+	0xEC,
+	0x30,
+	0xE7,
+	0x10,
+	0x0F,
+	0xBF,
+	0x00,
+	0x0C,
+	0x0E,
+	0xBE,
+	0x00,
+	0x08,
+	0x0D,
+	0xBD,
+	0x00,
+	0x04,
+	0x0B,
+	0xEB,
+	0x60,
+	0x14,
+	0xA2,
+	0xD5,
+	0xEB,
+	0x13,
+	0xFC,
+	0xED,
+	0x92,
+	0xE7,
+	0xFD,
+	0x22,
+	0x74,
+	0xFF,
+	0xFC,
+	0xFD,
+	0xFE,
+	0xFF,
+	0x22,
+	0xE4,
+	0x80,
+	0xF8,
+	0xA2,
+	0xD5,
+	0x74,
+	0xFF,
+	0x13,
+	0xFC,
+	0x7D,
+	0x80,
+	0xE4,
+	0x80,
+	0xEF,
+	0xC3,
+	0xE4,
+	0x9F,
+	0xFF,
+	0xE4,
+	0x9E,
+	0xFE,
+	0xE4,
+	0x9D,
+	0xFD,
+	0xE4,
+	0x9C,
+	0xFC,
+	0x22,
+	0x26,
+	0x27,
+	0x42,
+	0x43,
+	0x44,
+	0x45,
+	0x46,
+	0x47,
+	0x6A,
+	0x51,
+	0x52,
+	0x53,
+	0x57,
+	0x58,
+	0x59,
+	0x5A,
+	0x07,
+	0x01,
+	0x2B,
+	0x00,
+	0x00,
+	0x01,
+	0x00,
+	0x14,
+	0x5A,
+	0x00,
+	0x20,
+	0x07,
+	0x10,
+	0x11,
+	0x0E,
+	0x80,
+	0x14,
+	0x59,
+	0xE0,
+	0x24,
+	0x59,
+	0x60,
+	0x10,
+	0x19,
+	0x04,
+	0x00,
+	0x1C,
+	0x69,
+	0x00,
+	0x1D,
+	0x78,
+	0x04,
+	0x76,
+	0x00,
+	0x04,
+	0x77,
+	0x20,
+	0x04,
+	0x75,
+	0x08,
+	0x14,
+	0x52,
+	0x10,
+	0x23,
+	0x10,
+	0x01,
+	0x13,
+	0x0F,
+	0x20,
+	0xF0,
+	0x10,
+	0x80,
+	0x1F,
+	0x40,
+	0x04,
+	0x20,
+	0x13,
+	0x10,
+	0x05,
+	0x08,
+	0x14,
+	0x04,
+	0x03,
+	0x02,
+	0x02,
+	0x02,
+	0x12,
+	0x01,
+	0x11,
+	0x01,
+	0x00,
+	0x50,
+	0x00,
+	0x40,
+	0x04,
+	0x66,
+	0x50,
+	0x00,
+	0x30,
+	0x08,
+	0x40,
+	0x60,
+	0xB0,
+	0x40,
+	0x10,
+	0x20,
+	0x09,
+	0x01,
+	0x20,
+	0x08,
+	0xF0,
+	0x20,
+	0x07,
+	0x19,
+	0x21,
+	0x0D,
+	0x80,
+	0x21,
+	0x16,
+	0x01,
+	0xF0,
+	0x10,
+	0x08,
+	0x0E,
+	0x10,
+	0x09,
+	0x04,
+	0xF0,
+	0x20,
+	0x08,
+	0x0E,
+	0x20,
+	0x09,
+	0x04,
+	0xF0,
+	0x20,
+	0x09,
+	0x5D,
+	0x20,
+	0x08,
+	0xFE,
+	0x20,
+	0x07,
+	0x1D,
+	0x20,
+	0x0A,
+	0x20,
+	0x20,
+	0x07,
+	0x02,
+	0x24,
+	0x54,
+	0x10,
+	0x21,
+	0x11,
+	0x80,
+	0x10,
+	0x0A,
+	0x04,
+	0xF0,
+	0x11,
+	0x11,
+	0x80,
+	0x10,
+	0x07,
+	0x02,
+	0x14,
+	0x54,
+	0x10,
+	0x10,
+	0x07,
+	0x1D,
+	0x10,
+	0x0A,
+	0x20,
+	0x11,
+	0x16,
+	0x01,
+	0x11,
+	0x0D,
+	0x80,
+	0x10,
+	0x08,
+	0xF0,
+	0x10,
+	0x09,
+	0x5D,
+	0x14,
+	0x52,
+	0x01,
+	0x24,
+	0x52,
+	0x01,
+	0xF0,
+	0x7B,
+	0x00,
+	0x80,
+	0x0A,
+	0x7B,
+	0x01,
+	0x80,
+	0x06,
+	0x7B,
+	0x03,
+	0x80,
+	0x02,
+	0x7B,
+	0x04,
+	0x7C,
+	0x00,
+	0x80,
+	0x00,
+	0x12,
+	0x06,
+	0x42,
+	0xE2,
+	0x5C,
+	0x4D,
+	0xF2,
+	0x78,
+	0x03,
+	0xEB,
+	0xF2,
+	0xD2,
+	0xAF,
+	0x22,
+	0xC2,
+	0xAF,
+	0x78,
+	0x83,
+	0xE2,
+	0xCB,
+	0xF2,
+	0x74,
+	0x80,
+	0x2F,
+	0xF8,
+	0x22,
+	0x7B,
+	0x00,
+	0x80,
+	0x0A,
+	0x7B,
+	0x01,
+	0x80,
+	0x06,
+	0x7B,
+	0x03,
+	0x80,
+	0x02,
+	0x7B,
+	0x04,
+	0xED,
+	0xF4,
+	0xFC,
+	0x7D,
+	0x00,
+	0x80,
+	0xD1,
+	0x7B,
+	0x00,
+	0x80,
+	0x0A,
+	0x7B,
+	0x01,
+	0x80,
+	0x06,
+	0x7B,
+	0x03,
+	0x80,
+	0x02,
+	0x7B,
+	0x04,
+	0x7C,
+	0xFF,
+	0x80,
+	0xBF,
+	0x7B,
+	0x03,
+	0x80,
+	0x02,
+	0x7B,
+	0x04,
+	0x12,
+	0x06,
+	0x42,
+	0xED,
+	0xF4,
+	0xFF,
+	0xE2,
+	0x5F,
+	0xF2,
+	0x4D,
+	0xF2,
+	0x5F,
+	0xF2,
+	0x80,
+	0xB1,
+	0x8E,
+	0x83,
+	0x8F,
+	0x82,
+	0xE4,
+	0x93,
+	0xA3,
+	0xFB,
+	0x54,
+	0x0F,
+	0xCB,
+	0x54,
+	0xF0,
+	0xB4,
+	0xF0,
+	0x01,
+	0x22,
+	0xFD,
+	0xE4,
+	0x93,
+	0xA3,
+	0xFF,
+	0xE4,
+	0x93,
+	0xA3,
+	0xCD,
+	0xB4,
+	0x00,
+	0x05,
+	0x12,
+	0x06,
+	0x30,
+	0x80,
+	0xE2,
+	0xB4,
+	0x10,
+	0x05,
+	0x12,
+	0x06,
+	0x5C,
+	0x80,
+	0xDA,
+	0xB4,
+	0x20,
+	0x05,
+	0x12,
+	0x06,
+	0x71,
+	0x80,
+	0xD2,
+	0xB4,
+	0x30,
+	0x05,
+	0x12,
+	0x06,
+	0x7B,
+	0x80,
+	0xCA,
+	0x80,
+	0xC8,
+	0xD2,
+	0x11,
+	0xD2,
+	0x12,
+	0x30,
+	0x06,
+	0x03,
+	0x02,
+	0x07,
+	0x66,
+	0x7F,
+	0x4F,
+	0x12,
+	0x0C,
+	0x6F,
+	0x8F,
+	0x4A,
+	0xE5,
+	0x4A,
+	0x20,
+	0xE6,
+	0x03,
+	0x02,
+	0x07,
+	0x66,
+	0xD2,
+	0x06,
+	0x53,
+	0x4A,
+	0x3F,
+	0xE5,
+	0x4A,
+	0x24,
+	0xFD,
+	0x60,
+	0x12,
+	0x24,
+	0xFD,
+	0x60,
+	0x14,
+	0x24,
+	0xE7,
+	0x60,
+	0x2E,
+	0x24,
+	0x1E,
+	0x70,
+	0x5F,
+	0xD2,
+	0x0D,
+	0xC2,
+	0x11,
+	0x80,
+	0x5E,
+	0xD2,
+	0x07,
+	0xC2,
+	0x11,
+	0x80,
+	0x58,
+	0x30,
+	0x0A,
+	0x04,
+	0x7F,
+	0x80,
+	0x80,
+	0x02,
+	0x7F,
+	0x00,
+	0x8F,
+	0x29,
+	0x7F,
+	0x25,
+	0x12,
+	0x0C,
+	0x77,
+	0xEF,
+	0x30,
+	0xE4,
+	0x03,
+	0x43,
+	0x29,
+	0x01,
+	0xC2,
+	0x0A,
+	0x75,
+	0x4A,
+	0x01,
+	0x80,
+	0x3A,
+	0x75,
+	0x4B,
+	0x05,
+	0x75,
+	0x4C,
+	0x74,
+	0xE4,
+	0xFF,
+	0xE5,
+	0x4C,
+	0x2F,
+	0xF5,
+	0x82,
+	0xE4,
+	0x35,
+	0x4B,
+	0xF5,
+	0x83,
+	0xE4,
+	0x93,
+	0xFE,
+	0x74,
+	0x29,
+	0x2F,
+	0xF8,
+	0xA6,
+	0x06,
+	0x0F,
+	0xBF,
+	0x07,
+	0xE9,
+	0x7F,
+	0x50,
+	0x12,
+	0x0C,
+	0x7B,
+	0x8F,
+	0x2C,
+	0x7F,
+	0x51,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x54,
+	0x0F,
+	0xF5,
+	0x2D,
+	0x75,
+	0x4A,
+	0x07,
+	0x80,
+	0x05,
+	0xE4,
+	0xF5,
+	0x4A,
+	0xC2,
+	0x12,
+	0x30,
+	0x11,
+	0x09,
+	0xA2,
+	0x12,
+	0x92,
+	0x13,
+	0xAF,
+	0x4A,
+	0x12,
+	0x0A,
+	0xFF,
+	0xA2,
+	0x06,
+	0x22,
+	0xC2,
+	0x96,
+	0xD2,
+	0x97,
+	0xD2,
+	0x96,
+	0x22,
+	0x22,
+	0xC2,
+	0x97,
+	0xD2,
+	0x96,
+	0x74,
+	0x28,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xD2,
+	0x97,
+	0x74,
+	0x1E,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0x22,
+	0xC2,
+	0x97,
+	0xD2,
+	0x97,
+	0x74,
+	0x1E,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xD2,
+	0x96,
+	0x74,
+	0x28,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0x22,
+	0xD2,
+	0x97,
+	0xD2,
+	0x96,
+	0x74,
+	0x28,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xC2,
+	0x97,
+	0x74,
+	0x1E,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xC2,
+	0x96,
+	0x74,
+	0x1E,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0x22,
+	0xEF,
+	0x7E,
+	0x08,
+	0x33,
+	0xC0,
+	0xE0,
+	0x40,
+	0x04,
+	0xC2,
+	0x97,
+	0x80,
+	0x02,
+	0xD2,
+	0x97,
+	0x74,
+	0x11,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xD2,
+	0x96,
+	0x74,
+	0x11,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xC2,
+	0x96,
+	0x74,
+	0x11,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xD0,
+	0xE0,
+	0xDE,
+	0xD8,
+	0xD2,
+	0x97,
+	0x74,
+	0x11,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xD2,
+	0x96,
+	0x74,
+	0x11,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xA2,
+	0x97,
+	0xE4,
+	0x92,
+	0xE0,
+	0xFE,
+	0xC2,
+	0x96,
+	0x74,
+	0x11,
+	0x00,
+	0x00,
+	0xD5,
+	0xE0,
+	0xFB,
+	0xEE,
+	0x22,
+	0x8F,
+	0x5E,
+	0x8D,
+	0x5F,
+	0x7F,
+	0x5A,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x54,
+	0x80,
+	0xF5,
+	0x62,
+	0xE5,
+	0x5E,
+	0xB4,
+	0x01,
+	0x04,
+	0x7F,
+	0x40,
+	0x80,
+	0x02,
+	0x7F,
+	0x00,
+	0x8F,
+	0x60,
+	0x43,
+	0x60,
+	0x80,
+	0xAD,
+	0x60,
+	0x7F,
+	0x0A,
+	0x12,
+	0x06,
+	0x2E,
+	0xE5,
+	0x60,
+	0x44,
+	0x20,
+	0xFD,
+	0x7F,
+	0x0A,
+	0x12,
+	0x06,
+	0x2E,
+	0xAD,
+	0x60,
+	0x7F,
+	0x0A,
+	0x12,
+	0x06,
+	0x2E,
+	0x7F,
+	0x0E,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x20,
+	0xE7,
+	0x4E,
+	0x7F,
+	0x5A,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x54,
+	0x80,
+	0x65,
+	0x62,
+	0x60,
+	0x03,
+	0x7F,
+	0xFF,
+	0x22,
+	0x7F,
+	0x0E,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x30,
+	0xE6,
+	0x03,
+	0x7F,
+	0x01,
+	0x22,
+	0x85,
+	0x60,
+	0x61,
+	0x7F,
+	0x0C,
+	0x12,
+	0x0C,
+	0x7B,
+	0x8F,
+	0x60,
+	0xE5,
+	0x61,
+	0x65,
+	0x60,
+	0x60,
+	0x04,
+	0x7F,
+	0x01,
+	0x80,
+	0x02,
+	0x7F,
+	0x00,
+	0xE5,
+	0x60,
+	0x60,
+	0x04,
+	0x7E,
+	0x01,
+	0x80,
+	0x02,
+	0x7E,
+	0x00,
+	0xEE,
+	0x5F,
+	0x60,
+	0xB8,
+	0x7F,
+	0x0D,
+	0x12,
+	0x0C,
+	0x7B,
+	0xE5,
+	0x60,
+	0x14,
+	0x25,
+	0x5F,
+	0xF8,
+	0xA6,
+	0x07,
+	0x80,
+	0xA9,
+	0x7F,
+	0x00,
+	0x22,
+	0xAD,
+	0x07,
+	0x8D,
+	0x4A,
+	0xBD,
+	0xFF,
+	0x07,
+	0x7F,
+	0x50,
+	0x12,
+	0x0C,
+	0x6F,
+	0xAD,
+	0x07,
+	0xED,
+	0x54,
+	0x80,
+	0xFF,
+	0x53,
+	0x05,
+	0x7F,
+	0xED,
+	0xC4,
+	0x54,
+	0x0F,
+	0xFE,
+	0xED,
+	0x54,
+	0x0F,
+	0xC4,
+	0x54,
+	0xF0,
+	0xFD,
+	0xEF,
+	0x4E,
+	0xFF,
+	0x7B,
+	0x10,
+	0x12,
+	0x09,
+	0x15,
+	0x92,
+	0x11,
+	0xAD,
+	0x4A,
+	0x7F,
+	0x66,
+	0xE4,
+	0xF5,
+	0x4A,
+	0x30,
+	0x11,
+	0x08,
+	0xEF,
+	0x25,
+	0x4A,
+	0xF8,
+	0xE6,
+	0xFE,
+	0x80,
+	0x02,
+	0x7E,
+	0x00,
+	0x74,
+	0x29,
+	0x25,
+	0x4A,
+	0xF8,
+	0xA6,
+	0x06,
+	0x05,
+	0x4A,
+	0xE5,
+	0x4A,
+	0xB4,
+	0x10,
+	0xE5,
+	0xED,
+	0xF4,
+	0x70,
+	0x25,
+	0xA2,
+	0x11,
+	0x92,
+	0x13,
+	0x7F,
+	0x10,
+	0x12,
+	0x0A,
+	0xFF,
+	0xC2,
+	0x0D,
+	0x7D,
+	0x40,
+	0x7F,
+	0x4F,
+	0x12,
+	0x06,
+	0x4E,
+	0x30,
+	0x11,
+	0x09,
+	0x7D,
+	0x80,
+	0x7F,
+	0x50,
+	0x12,
+	0x06,
+	0x4E,
+	0x80,
+	0x07,
+	0x7D,
+	0x40,
+	0x7F,
+	0x50,
+	0x12,
+	0x06,
+	0x63,
+	0xA2,
+	0x11,
+	0x22,
+	0x25,
+	0x51,
+	0xFD,
+	0xAF,
+	0x4E,
+	0x8F,
+	0x59,
+	0x8D,
+	0x5A,
+	0x8B,
+	0x5B,
+	0x75,
+	0x5C,
+	0x03,
+	0xE5,
+	0x59,
+	0x30,
+	0xE7,
+	0x04,
+	0x7F,
+	0x01,
+	0x80,
+	0x02,
+	0x7F,
+	0x00,
+	0x8F,
+	0x5D,
+	0x53,
+	0x59,
+	0x7F,
+	0xAF,
+	0x5C,
+	0x15,
+	0x5C,
+	0xEF,
+	0x60,
+	0x4C,
+	0xE5,
+	0x5D,
+	0x70,
+	0x1D,
+	0xE5,
+	0x59,
+	0xD3,
+	0x94,
+	0x00,
+	0x40,
+	0x16,
+	0x12,
+	0x07,
+	0x70,
+	0x12,
+	0x07,
+	0x9B,
+	0x7F,
+	0x60,
+	0x12,
+	0x07,
+	0xB9,
+	0xAF,
+	0x59,
+	0x12,
+	0x07,
+	0xB9,
+	0x12,
+	0x07,
+	0x86,
+	0x12,
+	0x07,
+	0x70,
+	0xE5,
+	0x5D,
+	0x60,
+	0x0D,
+	0xE5,
+	0x5A,
+	0xFD,
+	0x7C,
+	0x00,
+	0xAB,
+	0x5B,
+	0x7F,
+	0x50,
+	0x7E,
+	0x02,
+	0x80,
+	0x0B,
+	0xE5,
+	0x5A,
+	0xFD,
+	0x7C,
+	0x00,
+	0xAB,
+	0x5B,
+	0x7F,
+	0x50,
+	0x7E,
+	0x00,
+	0x12,
+	0x0A,
+	0x61,
+	0x7D,
+	0x66,
+	0xE4,
+	0xFF,
+	0x12,
+	0x08,
+	0x07,
+	0xEF,
+	0x70,
+	0xAF,
+	0xD3,
+	0x22,
+	0xC3,
+	0x22,
+	0xC0,
+	0xE0,
+	0xC0,
+	0xD0,
+	0xC2,
+	0x8C,
+	0x75,
+	0x8A,
+	0x40,
+	0x75,
+	0x8C,
+	0x7C,
+	0xD2,
+	0x8C,
+	0x05,
+	0x18,
+	0xE5,
+	0x18,
+	0x70,
+	0x19,
+	0x05,
+	0x10,
+	0xE5,
+	0x10,
+	0xD3,
+	0x94,
+	0x02,
+	0x40,
+	0x05,
+	0x75,
+	0x10,
+	0x00,
+	0xD2,
+	0x0E,
+	0xE5,
+	0x10,
+	0xB4,
+	0x01,
+	0x03,
+	0xD3,
+	0x80,
+	0x01,
+	0xC3,
+	0x92,
+	0x03,
+	0xE5,
+	0x11,
+	0x60,
+	0x02,
+	0x15,
+	0x11,
+	0xE5,
+	0x12,
+	0x60,
+	0x02,
+	0x15,
+	0x12,
+	0xE5,
+	0x15,
+	0x60,
+	0x02,
+	0x15,
+	0x15,
+	0xE5,
+	0x16,
+	0x60,
+	0x02,
+	0x15,
+	0x16,
+	0xE5,
+	0x17,
+	0x60,
+	0x02,
+	0x15,
+	0x17,
+	0xE5,
+	0x14,
+	0x45,
+	0x13,
+	0x60,
+	0x0A,
+	0xE5,
+	0x14,
+	0x15,
+	0x14,
+	0x70,
+	0x0C,
+	0x15,
+	0x13,
+	0x80,
+	0x08,
+	0xC2,
+	0xAF,
+	0x12,
+	0x0C,
+	0x91,
+	0x02,
+	0x00,
+	0x00,
+	0x74,
+	0x81,
+	0xB5,
+	0x81,
+	0x00,
+	0x40,
+	0x02,
+	0x80,
+	0xEF,
+	0xD0,
+	0xD0,
+	0xD0,
+	0xE0,
+	0x32,
+	0x8F,
+	0x55,
+	0x75,
+	0x58,
+	0x66,
+	0xE4,
+	0xF5,
+	0x56,
+	0xE5,
+	0x55,
+	0xC3,
+	0x13,
+	0xFF,
+	0xE5,
+	0x55,
+	0x30,
+	0xE0,
+	0x04,
+	0x7D,
+	0x80,
+	0x80,
+	0x02,
+	0x7D,
+	0x00,
+	0x7B,
+	0x40,
+	0x12,
+	0x09,
+	0x15,
+	0x40,
+	0x01,
+	0x22,
+	0xE4,
+	0xF5,
+	0x57,
+	0xE5,
+	0x58,
+	0x25,
+	0x57,
+	0xF8,
+	0xE6,
+	0x25,
+	0x56,
+	0xF5,
+	0x56,
+	0x05,
+	0x57,
+	0xE5,
+	0x57,
+	0xB4,
+	0x40,
+	0xEF,
+	0xE5,
+	0x55,
+	0xC3,
+	0x13,
+	0xFF,
+	0xE5,
+	0x55,
+	0x30,
+	0xE0,
+	0x04,
+	0x7E,
+	0x80,
+	0x80,
+	0x02,
+	0x7E,
+	0x00,
+	0xEE,
+	0x24,
+	0x40,
+	0xFD,
+	0x7B,
+	0x40,
+	0x12,
+	0x09,
+	0x15,
+	0x40,
+	0x01,
+	0x22,
+	0xE4,
+	0xF5,
+	0x57,
+	0xE5,
+	0x58,
+	0x25,
+	0x57,
+	0xF8,
+	0xE6,
+	0x25,
+	0x56,
+	0xF5,
+	0x56,
+	0x05,
+	0x57,
+	0xE5,
+	0x57,
+	0xB4,
+	0x40,
+	0xEF,
+	0xE5,
+	0x56,
+	0x70,
+	0x03,
+	0xD3,
+	0x80,
+	0x01,
+	0xC3,
+	0x22,
+	0x8E,
+	0x5E,
+	0x8F,
+	0x5F,
+	0x8C,
+	0x60,
+	0x8D,
+	0x61,
+	0x8B,
+	0x62,
+	0xE5,
+	0x5E,
+	0xF5,
+	0x63,
+	0xEF,
+	0xF5,
+	0x64,
+	0xE5,
+	0x63,
+	0x7D,
+	0x80,
+	0x7F,
+	0x5A,
+	0x30,
+	0xE1,
+	0x05,
+	0x12,
+	0x06,
+	0x6F,
+	0x80,
+	0x03,
+	0x12,
+	0x06,
+	0x5A,
+	0x53,
+	0x63,
+	0x01,
+	0xE5,
+	0x63,
+	0x70,
+	0x0B,
+	0xE5,
+	0x60,
+	0x25,
+	0x64,
+	0xF5,
+	0x64,
+	0xE4,
+	0xF5,
+	0x65,
+	0x80,
+	0x04,
+	0xE5,
+	0x60,
+	0xF5,
+	0x65,
+	0xAD,
+	0x64,
+	0x7F,
+	0x28,
+	0x12,
+	0x06,
+	0x2E,
+	0xAD,
+	0x65,
+	0x7F,
+	0x40,
+	0x12,
+	0x06,
+	0x2E,
+	0xE5,
+	0x61,
+	0xF5,
+	0x65,
+	0xAD,
+	0x65,
+	0x7F,
+	0x29,
+	0x12,
+	0x06,
+	0x2E,
+	0xAD,
+	0x62,
+	0x7F,
+	0x08,
+	0x12,
+	0x06,
+	0x2E,
+	0xAF,
+	0x63,
+	0x22,
+	0xC0,
+	0xE0,
+	0xC0,
+	0xF0,
+	0xC0,
+	0x83,
+	0xC0,
+	0x82,
+	0xC0,
+	0xD0,
+	0x75,
+	0xD0,
+	0x08,
+	0x7F,
+	0x25,
+	0x12,
+	0x0C,
+	0x77,
+	0xEF,
+	0x54,
+	0x10,
+	0xF5,
+	0x48,
+	0x7D,
+	0x40,
+	0x7F,
+	0x6E,
+	0x12,
+	0x06,
+	0x75,
+	0x30,
+	0x00,
+	0x17,
+	0xE5,
+	0x48,
+	0x65,
+	0x47,
+	0x60,
+	0x11,
+	0x85,
+	0x48,
+	0x47,
+	0xD2,
+	0x02,
+	0xD2,
+	0x0A,
+	0xE5,
+	0x16,
+	0xD3,
+	0x94,
+	0x03,
+	0x40,
+	0x03,
+	0x75,
+	0x16,
+	0x03,
+	0xD0,
+	0xD0,
+	0xD0,
+	0x82,
+	0xD0,
+	0x83,
+	0xD0,
+	0xF0,
+	0xD0,
+	0xE0,
+	0x32,
+	0x8F,
+	0x4D,
+	0xE4,
+	0xF5,
+	0x4E,
+	0xE5,
+	0x4E,
+	0xC3,
+	0x95,
+	0x4D,
+	0x50,
+	0x15,
+	0xE5,
+	0x4E,
+	0x90,
+	0x05,
+	0x64,
+	0x93,
+	0xFF,
+	0x74,
+	0x29,
+	0x25,
+	0x4E,
+	0xF8,
+	0xE6,
+	0xFD,
+	0x12,
+	0x06,
+	0x26,
+	0x05,
+	0x4E,
+	0x80,
+	0xE4,
+	0x20,
+	0x13,
+	0x03,
+	0x43,
+	0x4D,
+	0x80,
+	0xAD,
+	0x4D,
+	0x7F,
+	0x50,
+	0x12,
+	0x06,
+	0x22,
+	0x7D,
+	0x40,
+	0x7F,
+	0x4F,
+	0x12,
+	0x06,
+	0x4E,
+	0xC2,
+	0x06,
+	0x22,
+	0x20,
+	0x12,
+	0x15,
+	0x7D,
+	0x08,
+	0x7F,
+	0x0A,
+	0x12,
+	0x06,
+	0x63,
+	0x7D,
+	0xFC,
+	0x7F,
+	0x63,
+	0x12,
+	0x06,
+	0x6F,
+	0x7D,
+	0x01,
+	0x7F,
+	0x64,
+	0x12,
+	0x06,
+	0x6F,
+	0x7D,
+	0x02,
+	0x7F,
+	0x66,
+	0x12,
+	0x06,
+	0x6F,
+	0x7D,
+	0x10,
+	0x7F,
+	0x11,
+	0x12,
+	0x06,
+	0x67,
+	0x7D,
+	0x10,
+	0x7F,
+	0x1A,
+	0x12,
+	0x06,
+	0x67,
+	0x7D,
+	0x08,
+	0x7F,
+	0x16,
+	0x02,
+	0x06,
+	0x67,
+	0x20,
+	0x12,
+	0x15,
+	0x7D,
+	0x08,
+	0x7F,
+	0x0A,
+	0x12,
+	0x06,
+	0x4E,
+	0x7D,
+	0xFC,
+	0x7F,
+	0x63,
+	0x12,
+	0x06,
+	0x5A,
+	0x7D,
+	0x01,
+	0x7F,
+	0x64,
+	0x12,
+	0x06,
+	0x5A,
+	0x7D,
+	0x02,
+	0x7F,
+	0x66,
+	0x12,
+	0x06,
+	0x5A,
+	0x7D,
+	0x10,
+	0x7F,
+	0x11,
+	0x12,
+	0x06,
+	0x52,
+	0x7D,
+	0x10,
+	0x7F,
+	0x1A,
+	0x12,
+	0x06,
+	0x52,
+	0x7D,
+	0x08,
+	0x7F,
+	0x16,
+	0x02,
+	0x06,
+	0x52,
+	0x8F,
+	0x4A,
+	0x8C,
+	0x4B,
+	0x8D,
+	0x4C,
+	0x8B,
+	0x4D,
+	0xE4,
+	0xF5,
+	0x4E,
+	0xAF,
+	0x4A,
+	0x7E,
+	0x00,
+	0xAB,
+	0x4D,
+	0xAD,
+	0x4C,
+	0xAC,
+	0x4B,
+	0x12,
+	0x0A,
+	0x61,
+	0x7D,
+	0x29,
+	0xE4,
+	0xFF,
+	0x12,
+	0x08,
+	0x07,
+	0xEF,
+	0x70,
+	0x02,
+	0xD3,
+	0x22,
+	0x05,
+	0x4E,
+	0xE5,
+	0x4E,
+	0xD3,
+	0x94,
+	0x02,
+	0x40,
+	0xDE,
+	0x22,
+	0xA2,
+	0x11,
+	0x92,
+	0x0B,
+	0x30,
+	0x11,
+	0x0F,
+	0x12,
+	0x0C,
+	0x49,
+	0x7E,
+	0x05,
+	0x7F,
+	0xE0,
+	0x12,
+	0x06,
+	0x8A,
+	0xD2,
+	0x12,
+	0x02,
+	0x0B,
+	0x37,
+	0xD2,
+	0x12,
+	0x12,
+	0x0B,
+	0x6B,
+	0x20,
+	0x09,
+	0x03,
+	0x12,
+	0x0C,
+	0xD6,
+	0x20,
+	0x0C,
+	0x07,
+	0x7E,
+	0x05,
+	0x7F,
+	0xD9,
+	0x12,
+	0x06,
+	0x8A,
+	0x22,
+	0xC2,
+	0x04,
+	0xEF,
+	0x60,
+	0x1A,
+	0x7D,
+	0x80,
+	0x7F,
+	0x0E,
+	0x12,
+	0x06,
+	0x52,
+	0x7F,
+	0x64,
+	0x7E,
+	0x00,
+	0x12,
+	0x0C,
+	0xF8,
+	0x12,
+	0x00,
+	0x0E,
+	0x20,
+	0x09,
+	0x03,
+	0x12,
+	0x0C,
+	0xD6,
+	0xD2,
+	0x03,
+	0x22,
+	0x12,
+	0x0C,
+	0x49,
+	0x7D,
+	0x80,
+	0x7F,
+	0x0E,
+	0x12,
+	0x06,
+	0x67,
+	0x22,
+	0xC2,
+	0xAF,
+	0xE4,
+	0xF5,
+	0x11,
+	0xF5,
+	0x12,
+	0x75,
+	0x13,
+	0x07,
+	0x75,
+	0x14,
+	0xD0,
+	0xF5,
+	0x18,
+	0xF5,
+	0x10,
+	0xC2,
+	0x8C,
+	0x53,
+	0x89,
+	0xF0,
+	0x43,
+	0x89,
+	0x01,
+	0x75,
+	0x8A,
+	0x40,
+	0x75,
+	0x8C,
+	0x7C,
+	0xD2,
+	0xA9,
+	0xD2,
+	0x8C,
+	0xD2,
+	0xAF,
+	0x22,
+	0x7E,
+	0x05,
+	0x7F,
+	0xC9,
+	0x12,
+	0x06,
+	0x8A,
+	0x7F,
+	0x08,
+	0x12,
+	0x0C,
+	0x6F,
+	0xEF,
+	0x54,
+	0x0E,
+	0x60,
+	0x07,
+	0x7E,
+	0x05,
+	0x7F,
+	0xE0,
+	0x12,
+	0x06,
+	0x8A,
+	0x7D,
+	0x04,
+	0x7F,
+	0x5C,
+	0x12,
+	0x06,
+	0x5A,
+	0x7D,
+	0x01,
+	0x7F,
+	0x07,
+	0x02,
+	0x06,
+	0x63,
+	0x7D,
+	0x00,
+	0x80,
+	0x0A,
+	0x7D,
+	0x01,
+	0x80,
+	0x06,
+	0x7D,
+	0x03,
+	0x80,
+	0x02,
+	0x7D,
+	0x04,
+	0xC2,
+	0xAF,
+	0x78,
+	0x83,
+	0xE2,
+	0xCD,
+	0xF2,
+	0x74,
+	0x80,
+	0x2F,
+	0xF8,
+	0xE2,
+	0x78,
+	0x03,
+	0xFF,
+	0xED,
+	0xF2,
+	0xD2,
+	0xAF,
+	0x22,
+	0x32,
+	0x8F,
+	0x4A,
+	0x80,
+	0x18,
+	0xE5,
+	0x4C,
+	0x60,
+	0x19,
+	0x7F,
+	0x25,
+	0x12,
+	0x0C,
+	0x77,
+	0xEF,
+	0x54,
+	0x10,
+	0xF5,
+	0x4B,
+	0xB5,
+	0x47,
+	0x04,
+	0x15,
+	0x4C,
+	0x80,
+	0xEB,
+	0x85,
+	0x4B,
+	0x47,
+	0x85,
+	0x4A,
+	0x4C,
+	0x80,
+	0xE3,
+	0x22,
+	0x8F,
+	0x4A,
+	0x80,
+	0x18,
+	0xE5,
+	0x4C,
+	0x60,
+	0x19,
+	0x7F,
+	0x7F,
+	0x12,
+	0x0C,
+	0x7B,
+	0xEF,
+	0x54,
+	0x04,
+	0xF5,
+	0x4B,
+	0xB5,
+	0x3D,
+	0x04,
+	0x15,
+	0x4C,
+	0x80,
+	0xEB,
+	0x85,
+	0x4B,
+	0x3D,
+	0x85,
+	0x4A,
+	0x4C,
+	0x80,
+	0xE3,
+	0x22,
+	0x7E,
+	0x06,
+	0x7F,
+	0x00,
+	0x12,
+	0x06,
+	0x8A,
+	0x7D,
+	0x04,
+	0x7F,
+	0x0A,
+	0x30,
+	0x01,
+	0x10,
+	0x12,
+	0x06,
+	0x63,
+	0x7D,
+	0x04,
+	0x7F,
+	0x5C,
+	0x12,
+	0x06,
+	0x6F,
+	0x7D,
+	0x01,
+	0x7F,
+	0x07,
+	0x80,
+	0x00,
+	0x12,
+	0x06,
+	0x4E,
+	0x22,
+	0xAD,
+	0x07,
+	0xAC,
+	0x06,
+	0xE4,
+	0x12,
+	0x04,
+	0x9C,
+	0xE4,
+	0xFB,
+	0xFA,
+	0x79,
+	0x20,
+	0x78,
+	0x40,
+	0x12,
+	0x03,
+	0xFA,
+	0x12,
+	0x04,
+	0xD5,
+	0x8F,
+	0x11,
+	0xE5,
+	0x11,
+	0x70,
+	0xFC,
+	0x22,
+	0x78,
+	0x7F,
+	0xE4,
+	0xF6,
+	0xD8,
+	0xFD,
+	0x75,
+	0x81,
+	0xA5,
+	0x02,
+	0x02,
+	0x64 
+};
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.c b/drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.c
new file mode 100644
index 0000000..2b92556
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.c
@@ -0,0 +1,1587 @@
+/* -*- pse-c -*-
+ *-----------------------------------------------------------------------------
+ * Filename: lvds.c
+ * $Revision: 1.3 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is contains all necessary functions for Internal
+ *  LVDS PORT DRIVER.
+ *  This is written according to the port interface defined in pd.h.
+ *-----------------------------------------------------------------------------
+ */
+
+#pragma optimize ("",off)
+
+#include <config.h>
+#include <igd_pd.h>
+#include <pd.h>
+#include <pd_print.h>
+
+#include "lvds.h"
+
+
+#ifdef T_LINUX
+	#include <linux/kernel.h>
+
+	#define PD_DEBUG printk
+	#define PD_ERROR printk
+#else
+	#define CONFIG_PLB
+//	#define CONFIG_TNC
+#endif
+
+/* One space between the #define and the backslash,else compilers complain */
+#define PTR_OFFSET_UCHAR(ptr,offset)   (*((unsigned char *)ptr + offset))
+#define PTR_OFFSET_USHORT(ptr, offset) (*(unsigned short *)((unsigned char *)ptr + offset))
+
+#define PTR_OFFSET_ULONG(ptr, offset) (*(unsigned long *)((unsigned char *)ptr + offset))
+/* END OF OPTIMIZATION MACROS */
+
+/* This constant = 10,000,000.  The value is used to
+ * get effective results from the integer math, and
+ * to not divide by 0. */
+#define PWM_FREQ_CALC_CONSTANT_1        0x00989680
+/* This constant is 1,000,000 - to multiply to get
+ * the Display Clock Frequency to the order of Mhz */
+#define PWM_FREQ_CALC_CONSTANT_2        0x000F4240
+
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(p) (sizeof(p)/sizeof((p)[0]))
+#endif
+
+static pd_version_t  internal_lvds_version = {11, 0, 0, 0}; /* driver version */
+static unsigned long internal_lvds_dab_list[] = {
+	PD_DAB_LIST_END
+}; /* dab list */
+
+static unsigned long supported_chipset[] =
+{
+#ifdef CONFIG_855
+	PCI_DEVICE_ID_VGA_855,
+#endif
+#ifdef CONFIG_915AL
+	PCI_DEVICE_ID_VGA_915AL,
+#endif
+#ifdef CONFIG_945GM
+	PCI_DEVICE_ID_VGA_945GM,
+	PCI_DEVICE_ID_VGA_945GME,
+#endif
+#ifdef CONFIG_965GM
+	PCI_DEVICE_ID_VGA_GM965,
+	PCI_DEVICE_ID_VGA_GME965,
+#endif
+#ifdef CONFIG_CTG
+	PCI_DEVICE_ID_VGA_CTG,
+#endif
+#ifdef CONFIG_PLB
+	PCI_DEVICE_ID_VGA_PLB,
+#endif
+#ifdef CONFIG_TNC
+	PCI_DEVICE_ID_VGA_TNC,
+	PCI_DEVICE_ID_VGA_TNC_A0,
+	PCI_DEVICE_ID_VGA_LNC,
+#endif
+};
+
+static pd_driver_t  internal_lvds_driver  = {
+	PD_SDK_VERSION,
+	"Internal LVDS Port Driver",
+	0,
+	&internal_lvds_version,
+	PD_DISPLAY_LVDS_INT,
+	PD_FLAG_UP_SCALING,
+	internal_lvds_dab_list,
+	100,
+	internal_lvds_validate,
+	internal_lvds_open,
+	internal_lvds_init_device,
+	internal_lvds_close,
+	internal_lvds_set_mode,
+	internal_lvds_post_set_mode,
+	internal_lvds_set_attrs,
+	internal_lvds_get_attrs,
+	internal_lvds_get_timing_list,
+	internal_lvds_set_power,
+	internal_lvds_get_power,
+	internal_lvds_save,
+	internal_lvds_restore,
+	internal_lvds_get_port_status
+};
+
+/* This is a common attribute table for all chipsets. At the end of the table
+ * there are multiple end entries to add chipset specific attributes.
+ * Chipset specific attributes:
+ *     965GM/GM45- Maintain aspect ratio
+ * Note:
+ *    1. Make sure to update the chipset_attr_index whenever adding a
+ *       chipset specific new attr.
+ */
+static pd_attr_t internal_lvds_attrs[] =
+{
+	/* Range attributes */
+
+	/*<-------ID----------->              <----TYPE-------->  <---NAME----->  <----flag---->               <---DEF_VAL---->       <--CURR_VALUE-->      min max  st */
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T1,   PD_ATTR_TYPE_RANGE, "FP Power T1",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T2,   PD_ATTR_TYPE_RANGE, "FP Power T2",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T3,   PD_ATTR_TYPE_RANGE, "FP Power T3",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T4,   PD_ATTR_TYPE_RANGE, "FP Power T4",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T5,   PD_ATTR_TYPE_RANGE, "FP Power T5",  PD_ATTR_FLAG_USER_INVISIBLE, 400,                 400,                    0,  3000, 1),
+	PD_MAKE_ATTR (PD_ATTR_ID_PANEL_DEPTH, PD_ATTR_TYPE_RANGE, "Panel Depth",  PD_ATTR_FLAG_USER_INVISIBLE, LVDS_DEF_PANEL_DEPTH,  LVDS_DEF_PANEL_DEPTH, 18, 24,   6),
+
+	PD_MAKE_ATTR (PD_ATTR_ID_PWM_INTENSITY,	  PD_ATTR_TYPE_RANGE, "PWM cycle",			  PD_ATTR_FLAG_USER_INVISIBLE,		100,		0,				  0,  100,  0),
+	PD_MAKE_ATTR (PD_ATTR_ID_INVERTER_FREQ,	  PD_ATTR_TYPE_RANGE, "Inverter Frequency",	  PD_ATTR_FLAG_USER_INVISIBLE,		100,		0,				  0,  0,  0),
+	PD_MAKE_ATTR (PD_ATTR_ID_BLM_LEGACY_MODE, PD_ATTR_TYPE_BOOL,  "Backlight Legacy mode",	  PD_ATTR_FLAG_USER_INVISIBLE,		0,		0,					  0,  0,  0),
+
+	/*<-------ID------------>                 <----TYPE--------> <---NAME----->        <------flag--------------->  <---DEF_VAL---->   <--CURR_VALUE-->   <---pad--> */
+	PD_MAKE_ATTR (PD_ATTR_ID_2_CHANNEL_PANEL, PD_ATTR_TYPE_BOOL, "Dual-channel panel", PD_ATTR_FLAG_USER_INVISIBLE, 0,                 0,                 0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_LVDS_PANEL_TYPE, PD_ATTR_TYPE_BOOL, "Panel Type",         PD_ATTR_FLAG_USER_INVISIBLE, 0,                 0,                 0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_PANEL_FIT,       PD_ATTR_TYPE_BOOL, "Panel Upscale",      PD_ATTR_FLAG_USER_INVISIBLE, LVDS_DEF_PANEL_FIT,LVDS_DEF_PANEL_FIT,0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_DITHER,          PD_ATTR_TYPE_BOOL, "Dither",             PD_ATTR_FLAG_USER_INVISIBLE, LVDS_DEF_DITHER,   LVDS_DEF_DITHER,   0, 0, 0),
+	PD_MAKE_ATTR (LVDS_ATTR_ID_TC_LVDS_CLK,   PD_ATTR_TYPE_BOOL, "TC LVDS CLK 110MHz", PD_ATTR_FLAG_USER_INVISIBLE, 0,                 0,                 0, 0, 0),
+
+	/* Start of chipset specific attributes */
+	/* Maintain aspect ratio */
+	PD_MAKE_ATTR (PD_ATTR_LIST_END,       0,                   "",            0,                           0,                0,               0,     0, 0),
+	/* Text tuning */
+	PD_MAKE_ATTR (PD_ATTR_LIST_END,       0,                   "",            0,                           0,                0,               0,     0, 0),
+
+	/* Attribute list end */
+	PD_MAKE_ATTR (PD_ATTR_LIST_END,       0,                   "",            0,                           0,                0,               0,     0, 0)
+};
+
+/* Rightnow it is -3 to reach starting of chipset specific attributes */
+static unsigned short chipset_attr_index = (unsigned short)
+	sizeof(internal_lvds_attrs)/sizeof(pd_attr_t) - 3;
+
+#if 0
+static pd_attr_t internal_lvds_965gm_attrs[] = {
+	PD_MAKE_ATTR (PD_ATTR_ID_MAINTAIN_ASPECT_RATIO,PD_ATTR_TYPE_BOOL,      "Maintain Aspect Ratio",0,             0,               0,               0,     0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_TEXT_TUNING,          PD_ATTR_TYPE_RANGE,     "Text Enhancement",     0,             0,               0,               0,     2, 1),
+};
+#endif
+
+static void internal_lvds_write_reg(internal_lvds_context_t *pd_context, unsigned long reg,
+		unsigned long value, unsigned long change_bits, unsigned long reg_type);
+
+static unsigned long internal_lvds_read_reg(internal_lvds_context_t *pd_context,
+		unsigned long reg, unsigned long reg_type);
+static void internal_lvds_panel_fit(internal_lvds_context_t *pd_context);
+
+static void internal_lvds_get_dclk(internal_lvds_context_t *pd_context, pd_dvo_info_t *internal_lvds_info);
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: PD_MODULE_INIT(internal_lvds_init)
+ *
+ * Description:
+ *    This is the entry function into LVDS port driver when
+ *    it first loads. This will call pd_register() to register
+ *    with Display driver.  Only the driver object is initialized in this
+ *    function, similar to DrverEntry() in a WDM driver.
+ *
+ * Parameters:
+ *    [OUT] *handle:  Not used.  Place holder for supporting dynamic pd
+ *
+ * Return:
+ *    PD_SUCCESS(0)  success
+ *    PD_ERR_XXXXXX  otherwise
+ *
+ *----------------------------------------------------------------------------
+ */
+#ifdef T_LINUX //thua- 9/16/11
+
+int PD_MODULE_INIT(internal_lvds_init, (void *handle))
+{
+	/* register the LVDS driver */
+	return pd_register(handle, &internal_lvds_driver);
+}
+
+#endif
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: PD_MODULE_EXIT(internal_lvds_exit, (void))
+ *
+ * Description:
+ *    This function cleans up resources used by the LVDS driver
+ *
+ * Parameters:
+ *    None
+ *
+ * Return:
+ *    PD_SUCCESS(0):  always returns this
+ *
+ *----------------------------------------------------------------------------
+ */
+#ifdef T_LINUX //thua- 9/16/11
+int PD_MODULE_EXIT(internal_lvds_exit, (void))
+{
+	return PD_SUCCESS;
+}
+#endif
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_validate
+ *
+ * Description:
+ *    Place holder for a future function
+ *
+ * Parameters:
+ *    TBD
+ *
+ * Return:
+ *    TBD
+ *
+ *----------------------------------------------------------------------------
+ */
+
+unsigned long internal_lvds_validate (unsigned long cookie)
+{                                                          /* internal_lvds_validate */
+	/* Validate magic cookie */
+	/* TODO: Implement the magic cookie algorithm */
+	return cookie;
+}                                                          /* internal_lvds_validate */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_open
+ *
+ * Description:
+ *    This function creates an LVDS context and intialize it with LVDS
+ *    attributes.
+ *
+ *    Internal LVDS port is available only on MGM platform, this port driver
+ *    reads the GMCH device ID with pd_read_regs(PD_REG_PCI) to verify that
+ *    it is supported on the current platform.
+ *
+ * Parameters:
+ *    [IN] callback:    callback context
+ *    [INOUT] context:  Device context.  This function will set the attributes
+ *                      for this context, provided it is already allocated,
+ *                      i.e. not NULL.
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:  if either of the parameters is NULL
+ *    PD_ERR_NODEV:     if an LVDS device is already up and running
+ *    PD_ERR_NOMEM:     if a memory allocation failed
+ *    PD_SUCCESS:       if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+
+/* lvds context structure being declared and initialized */
+
+static internal_lvds_context_t internal_lvds_context = {  /* lvds context structure */
+	0,                   /* fp_width     */
+	0,                   /* fp_height    */
+	0,                   /* dual_channel: Default single channel */
+	0,                   /* panel_type : 0-SPWG 1-OpenLDI*/
+	LVDS_DEF_PANEL_FIT,  /* panel_fit    */
+	LVDS_DEF_PANEL_DEPTH,/* panel_depth  */
+	0xFFFF,              /* dither       */
+	0,                   /* Main aspect ratio, default no */
+	0,                   /* panel filter: Default fuzzy filtering */
+	100,                 /* PWM Intensity */
+	0xFFFF,              /* Inverter Frequency*/
+	0,                   /* BLM Legacy Mode */
+
+	PD_POWER_MODE_D0,    /* power_state   */
+	0,                   /* chipset       */
+	0,                   /* init_done     */
+	0,                   /* num_attrs */
+	0,                   /* PIPE flags    */
+	0,                   /* Graphics Frequency */
+	0,                   /* is gn4 based LVDS controller? */
+	0,                   /* is pwm_done? */
+	0,                   /* is tc_110MHz_clk? i.e., TC max LVDS to 110MHz*/
+
+	NULL,                /* ptr to callback       */
+	NULL,                /* ptr to timing table   */
+	internal_lvds_attrs,          /* ptr to attribute list */
+	NULL,                /* ptr to native timing  */
+	NULL,                /* current mode */
+};
+
+int internal_lvds_open(pd_callback_t *callback, void **context)
+{                                                              /* internal_lvds_open */
+	internal_lvds_context_t *pd_context = (internal_lvds_context_t*) &(internal_lvds_context); /* static Global */
+	pd_reg_t reg_list[2];
+	int ret, i, valid_chipset = 0;
+	unsigned short chipset;
+
+	PD_TRACE_ENTER;
+
+
+
+	/* make sure parameters are valid */
+	if (!callback || !context) {
+		PD_ERROR("invalid parameter");
+		return (PD_ERR_NULL_PTR);
+	}
+	/* GMCH cannot support more than one device */
+	if (internal_lvds_driver.num_devices > 0) {
+		return (PD_ERR_NODEV);
+	}
+	/* Verify that this is an GMCH with Internal LVDS available */
+	reg_list[0].reg = 2;
+	reg_list[1].reg = PD_REG_LIST_END;
+	ret = callback->read_regs(callback->callback_context, reg_list, PD_REG_PCI);
+	if(ret != PD_SUCCESS) {
+		return ret;
+	}
+	chipset = (unsigned short)(reg_list[0].value & 0xffff);
+	for (i = 0; i < ARRAY_SIZE(supported_chipset); i++) {
+		if (chipset == supported_chipset[i]){
+			valid_chipset = 1;
+			break;
+		}
+	}
+	if(!valid_chipset){
+		return PD_ERR_NODEV;
+	}
+
+	/* Special handling for gn4 and beyond chipsets */
+	if (chipset == PCI_DEVICE_ID_VGA_GM965 ||
+		chipset == PCI_DEVICE_ID_VGA_GME965 ||
+		chipset == PCI_DEVICE_ID_VGA_CTG ||
+		chipset == PCI_DEVICE_ID_VGA_TNC ||
+		chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+		chipset == PCI_DEVICE_ID_VGA_LNC) {
+		internal_lvds_context.gn4_plus = 1;
+	}
+
+	/* Initialize number of attributes */
+	/* +1 is to include the end attribute */
+	internal_lvds_context.num_attrs = (unsigned char)chipset_attr_index;
+//	internal_lvds_context.num_attrs = (unsigned char)chipset_attr_index + 1;
+
+#if 0
+	/* Add chipset specific attrbutes.
+	 * This can be expanded into a switch statement in future if required. */
+	if (internal_lvds_context.gn4_plus) {
+		internal_lvds_context.num_attrs += sizeof(internal_lvds_965gm_attrs)/sizeof(pd_attr_t);
+		for (i=0; i < sizeof(internal_lvds_965gm_attrs)/sizeof(pd_attr_t); i++) {
+			internal_lvds_attrs[chipset_attr_index+i] = internal_lvds_965gm_attrs[i];
+		}
+	}
+#endif
+
+	/* Make this a compile time so that size of vBIOS doesn't become > 64KB */
+#if defined(CONFIG_PLB) || defined(CONFIG_TNC)
+	/* pwm backlight control needs graphics frequency. we currently implement
+	 * pwm backlight control for pouslbo only to limit the vbios lvds size. Each
+	 * chipset have a different method of getting this value and chipset has a
+	 * different multiplier. */
+	if (chipset == PCI_DEVICE_ID_VGA_PLB || 
+	    chipset == PCI_DEVICE_ID_VGA_TNC ||
+	    chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+	    chipset == PCI_DEVICE_ID_VGA_LNC) {
+
+		/* For plb/tnc, graphics frequency is obtained by sending an opcode to
+		 * port 5 in the SCH Message Network. We call the read_regs with
+		 * PD_REG_BRIDGE_OPCODE specifically for this purpose only.
+		 *
+		 * The input for this read_reg is the opcode that register data that
+		 * we send into the Message control register*/
+		reg_list[1].reg = PD_REG_LIST_END;
+		ret = callback->read_regs(callback->callback_context,
+			reg_list, PD_REG_BRIDGE_OPCODE);
+		if(ret != PD_SUCCESS) {
+			return ret;
+		}
+
+		/*set the graphics frequency*/
+		pd_context->gfx_freq = (unsigned short) reg_list[0].value;
+	}
+#endif
+
+	pd_context->callback = callback;           /* Save callback context */
+	pd_context->chipset = chipset;            /* save the chipset ID   */
+	*context = (void *) pd_context;
+
+	PD_TRACE_EXIT;
+	return PD_SUCCESS;
+}                                                              /* internal_lvds_open */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_init_device
+ *
+ * Description:
+ *    Initializes the LVDS device, using the device context from the parameter
+ *
+ * Parameters:
+ *    [INOUT] context:  device context
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:  if parameter is invalid
+ *    PD_SUCCESS:       if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int internal_lvds_init_device (void *context)
+{                                                       /* internal_lvds_init_device */
+	if (!context) {
+		return (PD_ERR_NULL_PTR);
+	}
+
+	/* Don't need to do much to initialize this device */
+	((internal_lvds_context_t *)context)->init_done = 1;
+	internal_lvds_driver.num_devices++;
+
+	return (PD_SUCCESS);
+}                                                       /* internal_lvds_init_device */
+
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_close
+ *
+ * Description:
+ *    Releases resources allocated during internal_lvds_open().  This function
+ *    essentially frees a device object.
+ *
+ * Parameters:
+ *    [INOUT] device_context:  device to be freed
+ *
+ * Return:
+ *    PD_SUCCESS: always returns this
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int internal_lvds_close(void *device_context)
+{
+#ifndef CONFIG_MICRO
+
+	internal_lvds_context_t *pd_context = (internal_lvds_context_t *)device_context;
+
+	/* internal_lvds_close */
+	
+	PD_TRACE_ENTER;
+
+	/* Deallocate memory occupied by this device */
+	if (device_context) {
+		if ( NULL != pd_context->timing_table) {
+			pd_free(pd_context->timing_table);
+		}
+
+		/* Free attribute list, if necessary */
+		/* FIXME -- The following test will never call pd_free(), because the
+		 * expression "!internal_lvds_driver.num_devices" will yield 0 or 1, and that
+		 * will never be greater than 1.  This is a potentially small memory
+		 * leak, unless some other code frees it.
+		 */
+		if (!internal_lvds_driver.num_devices > 1) {
+			pd_free(pd_context->attr_list);
+		}
+		/* This allocated statically no need to free it */
+		/* pd_free(device_context); */
+		internal_lvds_driver.num_devices--;
+		pd_context->init_done = 0;
+	}
+
+	PD_TRACE_EXIT;
+#endif
+	return (PD_SUCCESS);
+}                                                             /* internal_lvds_close */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_set_mode
+ *
+ * Description:
+ *    Sets LVDS to a new mode, specified by "mode".
+ *
+ * Parameters:
+ *    [INOUT] context:  device context
+ *    [IN]    mode:     information about the mode to switch to
+ *    [IN]    flags:    can contain the following value
+ *                PD_SET_MODE_FLAG_TEST:  only testing to see if mode is supported
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:     if invalid parameter detected
+ *    PD_ERR_MODE_NOTSUPP: if mode specified is not supported
+ *    PD_SUCCESS:          if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+int internal_lvds_set_mode(void *context, pd_timing_t *mode, unsigned long flags)
+{
+	internal_lvds_context_t *pd_context  = NULL;
+
+	PD_DEBUG("internal_lvds_set_mode)\n");
+	PD_TRACE_ENTER;
+
+	pd_context = (internal_lvds_context_t *)context;
+
+	/* Make sure these parameters are valid */
+	if (!pd_context || !mode) {
+		return (PD_ERR_NULL_PTR);
+	}
+	PD_DEBUG("internal_lvds_set_mode: %ux%u", mode->width, mode->height);
+
+	/* Make sure specified mode is supported */
+	if ((pd_context->fp_width && (mode->width > pd_context->fp_width)) ||
+		(pd_context->fp_height && (mode->height > pd_context->fp_height))) {
+		PD_ERROR("internal_lvds_set_mode: ERROR #1\n");
+		return PD_ERR_MODE_NOTSUPP;
+	}
+
+	/* Do nothing if we are only want to know if a mode is supported */
+	if (flags & PD_SET_MODE_FLAG_TEST) {
+		PD_ERROR("internal_lvds_set_mode: SUPPORTED #2\n");
+		return PD_SUCCESS;
+	}
+
+	pd_context->current_mode = mode;
+	pd_context->pipe = flags;
+	/* Enable panel fitting and return */
+	internal_lvds_panel_fit(pd_context);
+  
+	PD_ERROR("internal_lvds_set_mode: EXIT #3\n");
+	PD_TRACE_EXIT;
+
+	return PD_SUCCESS;
+}
+
+int internal_lvds_post_set_mode(void *context, pd_timing_t *mode, unsigned long flags)
+{                                                          /* internal_lvds_set_mode */
+	internal_lvds_context_t *pd_context  = NULL;
+	unsigned long  port_control = 0;
+	unsigned long  preserve     = 0;
+	int            ret          = 0;
+
+	PD_TRACE_ENTER;
+	pd_context = (internal_lvds_context_t *)context;
+
+	PD_DEBUG("internal_lvds_post_set_mode)\n");
+	/* Make sure these parameters are valide */
+	if (!pd_context || !mode) {
+		PD_DEBUG("internal_lvds_post_set_mode: EXIT #1\n");
+		return (PD_ERR_NULL_PTR);
+	}
+
+	/* Before enabling the LVDS port, make sure that display PLL for this pipe
+	 * is enabled and the port is power sequenced on using the panel power
+	 * sequencing logic. */
+
+	preserve = 0x3E007803;
+	port_control = preserve & internal_lvds_read_reg(pd_context, 0x61180, PD_REG_MIO);
+	port_control |= BIT(31);          /* enable LVDS port */
+	if (flags & PD_SET_MODE_PIPE_B) {
+		port_control |= BIT(30);
+	}
+	port_control |= (BIT(9)|BIT(8));  /* power up */
+	port_control &= ~(BIT(21)|BIT(20)); /* Default sync polarites active high */
+
+	/* For gn4+, dither moved to port_control from panel_fit reg */
+	if (pd_context->gn4_plus) {
+		if (pd_context->panel_depth == 18) {
+			port_control |= BIT(25);
+		}
+
+		if (pd_context->dither != 0xFFFF) {
+			if (pd_context->dither) {
+				port_control |= BIT(25);
+			} else {
+				port_control &= ~BIT(25);
+			}
+		}
+	}
+
+	if((!pd_context->panel_fit) &&
+	   ( mode->width < pd_context->fp_width ||
+	     mode->height < pd_context->fp_height ) ){
+		port_control |= BIT(15); /* enable border in active for centering */
+	}
+
+	/*
+	 * Bit 24 for OpenLDI should be set to a 0 (1x18.0, 2x18.0, 1x24.0, 2x24.0).
+	 * From (B-Spec, Ref# 22316, section 1.15.3.8.3)
+	 *
+	 * Bit 24 for SPWG should be set to a 1 (1x24.1 or 2x24.1).
+	 *
+	 * This was verified by comparing the timing diagram for 1x24.0 in
+	 * OpenLDI spec (5.4.2.2 24-bit Single Pixel Mode, Unbalanced) with the
+	 * same diagram in the Display BSpec for Napa or Gen4.
+	 * A0 � A3 signals match the 1x24.0.
+	 *
+	 * From the OpenLDI spec (bit mappings are different):
+	 *
+	 * Table 5-2, Bit Number Mappings
+	 * 18 bpp bit# 24 bpp bit# OpenLDI bit#
+	 * 5           7           5
+	 * 4           6           4
+	 * 3           5           3
+	 * 2           4           2
+	 * 1           3           1
+	 * 0           2           0
+	 *             1           7
+	 *             0           6
+	 */
+
+	/* Attribute panel_type description:
+	 *  Attr ID    Attr Value        IntLVDS dataformat
+	 *  =======    ===============   ==================
+	 *    49       0  (SPWG)           1 (value of Bit 24)
+	 *    49       1  (OpenLDI)        0 (value of Bit 24)
+	 */
+	if (pd_context->panel_type == 0) {
+		port_control |= BIT(24); /* Dataformat 0 = SPWG, 1 = OpenLDI */
+	}
+
+	/* If the dual-channel is required, then power up second channel
+	 * ClkB and B0, B1, B2, (B3) */
+	if (pd_context->dual_channel) {
+		port_control |= (BIT(5)|BIT(4));  /* ClkB */
+		port_control |= (BIT(3)|BIT(2));  /* B0, B1, B2, (B3) */
+	}
+
+	/* Check for 18 or 24 bit panel */
+	if (pd_context->panel_depth == 24) {
+		/* If the panel is 24-bit (8-bpp), enable A3, (B3) pair. */
+		port_control |= (BIT(7)|BIT(6));
+	}
+
+	/* Set the sync polarities correctly if there is a native dtd */
+	if (pd_context->native_dtd) {
+		/* Set bit 20 for hsync active low */
+		if ((pd_context->native_dtd->mode_info_flags & PD_HSYNC_HIGH) == 0) {
+			port_control |= BIT(20);
+		}
+		/* Set bit 21 for vsync active low */
+		if ((pd_context->native_dtd->mode_info_flags & PD_VSYNC_HIGH) == 0) {
+			port_control |= BIT(21);
+		}
+	}
+
+    internal_lvds_write_reg(pd_context, 0x61204, 0xABCD0000, 0xFFFFFFFF, PD_REG_MIO);
+
+	internal_lvds_write_reg(pd_context, 0x61180, port_control, 0xFFFFFFFF, PD_REG_MIO);
+	ret = internal_lvds_set_power(pd_context, PD_POWER_MODE_D0);
+	if (ret) {
+		PD_ERROR("PD set_power (D0) returned: 0x%x", ret);
+		return ret;
+	}
+
+    internal_lvds_write_reg(pd_context, 0x61204, BIT(1), BIT(1), PD_REG_MIO);
+
+	PD_DEBUG("internal_lvds_post_set_mode: EXIT SUCCESS\n");
+
+	PD_TRACE_EXIT;
+	/* Set the mode as per given timings */
+	return PD_SUCCESS;
+}                                                          /* internal_lvds_set_mode */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_set_attrs
+ *
+ * Description:
+ *    Incorporate attributes in "list" into the device context.  This function
+ *    will override the initial attributes set by init_attrs.
+ *
+ * Parameters:
+ *    [INOUT] context:  device context
+ *    [IN] num:         not used, but must not be 0.
+ *    [IN] list:        list of attributes to incorporate into device context
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:          if one of the parameters is invalid
+ *    PD_ERR_ATTR_CANT_CHANGE:  attributes cannot be modified
+ *    PD_SUCCESS: if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+/* Tables required by Optimization Code. internal_lvds_set_attrs() */
+typedef struct _opt_table_data {
+	unsigned long id;
+	unsigned short block;
+	unsigned short offset;
+} internal_lvds_opt_table_data_t;
+static internal_lvds_opt_table_data_t table_opt_data1[] = {
+
+	/*<--- id ---------->  <---block---> <------ offset -------------------> */
+	{PD_ATTR_ID_PANEL_DEPTH,     1, PD_OFFSETOF(internal_lvds_context_t, panel_depth) },
+
+	{PD_ATTR_ID_2_CHANNEL_PANEL, 1, PD_OFFSETOF(internal_lvds_context_t, dual_channel)},
+	{PD_ATTR_ID_LVDS_PANEL_TYPE, 1, PD_OFFSETOF(internal_lvds_context_t, panel_type) },
+	{PD_ATTR_ID_PANEL_FIT,       1, PD_OFFSETOF(internal_lvds_context_t, panel_fit)   },
+	{PD_ATTR_ID_DITHER,          1, PD_OFFSETOF(internal_lvds_context_t, dither)      },
+	{PD_ATTR_ID_MAINTAIN_ASPECT_RATIO,1,PD_OFFSETOF(internal_lvds_context_t,aspect_ratio)},
+	{PD_ATTR_ID_TEXT_TUNING,     1, PD_OFFSETOF(internal_lvds_context_t, text_tune)},
+	{PD_ATTR_ID_PWM_INTENSITY,   1, PD_OFFSETOF(internal_lvds_context_t, pwm_intensity)},
+	{PD_ATTR_ID_INVERTER_FREQ,   1, PD_OFFSETOF(internal_lvds_context_t, inverter_freq)},
+	{PD_ATTR_ID_BLM_LEGACY_MODE, 1, PD_OFFSETOF(internal_lvds_context_t, blm_legacy_mode)},
+	{LVDS_ATTR_ID_TC_LVDS_CLK,   1, PD_OFFSETOF(internal_lvds_context_t, tc_110MHz_clk)},
+
+	/*<--- id ---------->  <---block---> <------ offset -------------------> */
+	{PD_ATTR_ID_FP_PWR_T1,       2,     0 }, /* 6 */
+	{PD_ATTR_ID_FP_PWR_T2,       2,     0 }, /* 7 */
+	{PD_ATTR_ID_FP_PWR_T3,       2,     0 }, /* 8 */
+	{PD_ATTR_ID_FP_PWR_T4,       2,     0 }, /* 9 */
+	{PD_ATTR_ID_FP_PWR_T5,       2,     0 }  /* 10 */
+};
+/* End of Tables required by Optimization Code. internal_lvds_set_attrs() */
+
+int internal_lvds_set_attrs (void *context, unsigned long num, pd_attr_t *list)
+{
+	internal_lvds_context_t *pd_context = (internal_lvds_context_t *) context;
+	pd_attr_t      *attr       = NULL;
+	unsigned short i           = 0;
+	unsigned short j           = 0;
+	int            ret         = PD_SUCCESS;
+	/* no of case IDs in the global table */
+	int num_case_ids = sizeof(table_opt_data1)/sizeof(internal_lvds_opt_table_data_t);
+
+	/* basic parameter check */
+	if (!context || !num || !list) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	PD_DEBUG("internal_lvds_set_attrs()\n");
+	for (i = 0; i < num; i++, list++) {
+		/* do nothing if the attribute has not been changed */
+		if (!(list->flags & PD_ATTR_FLAG_VALUE_CHANGED)) {
+			continue;
+		}
+
+		//thua- need to reset flag to 'not changed' - 9/19/11
+//		list->flags &= ~PD_ATTR_FLAG_VALUE_CHANGED;
+
+		/* attributes can't be changed after init has been completed */
+		if (list->flags & PD_ATTR_FLAG_USER_INVISIBLE &&
+			pd_context->init_done) {
+			return PD_ERR_ATTR_CANT_CHANGE;
+		}
+
+		/* Set the internal attributes' list.  Note that although get_attr() can
+		 * return NULL theortically, it will not do so here because all the
+		 * attribute IDs in this switch statement comes from internal_lvds_attrs[],
+		 * a list that is automatically initialized into pd_context.  This is
+		 * why we are not checking for NULL after calling get_attr().
+		 */
+#if 0	/* ORIGINAL SWITCH STATEMENT */
+		switch (list->id) {
+			case PD_ATTR_ID_FP_PWR_T1:
+			case PD_ATTR_ID_FP_PWR_T2:
+			case PD_ATTR_ID_FP_PWR_T3:
+			case PD_ATTR_ID_FP_PWR_T4:
+			case PD_ATTR_ID_FP_PWR_T5:
+				/* current_value should not exceed the predefined max value */
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = LVDS_MIN(
+						((pd_range_attr_t *)list)->current_value,
+						attr->_pad1);
+				break;
+
+			case PD_ATTR_ID_PANEL_DEPTH:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = list->current_value;
+				pd_context->panel_depth = (unsigned char) attr->current_value;
+				break;
+
+			case PD_ATTR_ID_2_CHANNEL_PANEL:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = ((pd_bool_attr_t *)list)->current_value;
+				pd_context->dual_channel = (attr->current_value?1:0);
+				break;
+
+			case PD_ATTR_ID_LVDS_PANEL_TYPE:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = ((pd_bool_attr_t *)list)->current_value;
+				pd_context->panel_type = (attr->current_value?1:0);
+				break;
+
+			case PD_ATTR_ID_PANEL_FIT:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = ((pd_bool_attr_t *)list)->current_value;
+				pd_context->panel_fit = (attr->current_value?1:0);
+				break;
+
+			default:
+				/* do nothing if we have an unknown ID */
+				break;
+		}
+#endif
+		/* OPTIMIZATION CODE BEGINS FOR THE ABOVE SWITCH
+		 *----------------------------------------------
+		 * Step 1: First identify the code common to all case blocks.
+		 *         This we call it "common code block" Since this is to be
+		 *         executed by all the case blocks.
+		 *
+		 * Step 2: Group the cases into blocks based on how we can combine them
+		 *
+		 *         Eg: case 0: ptr->x = 1; break; // similar code
+		 *        case 1: ptr->y = 1; break; // similar code
+		 *  ------Combined block ----
+		 *        case 0:
+		 *        case 1: PTR_OFFSET_TYPE(ptr, table[i].offset) = 1; break;
+		 *        This is an important step because we save code space by
+		 *        mapping many cases to smaller number of blocks. In the above
+		 *        we use index to get the right offset of the ptr.
+		 *
+		 * Step 3: Assign block IDs to each block. Put the case ID, block ID
+		 *         and other information such as offsets of ptr in a global
+		 *         table.
+		 *
+		 * Step 4: During run-time , search through the global table, find the
+		 *         matching case ID, and execute the common code. Next get the
+		 *         block ID and execute the block specific code. For the index,
+		 *         retrieve it from the table for the corresponding case ID.
+		 *
+		 * Step 5: If no matching case ID is found, error it out.
+		 *
+		 * Let's look at the optimization code for the above switch.
+		 * The code below searches for the list->id in a global table where
+		 * we store all the case values along with block numbers and other
+		 * information. After we find a valid id in the table , we execute the
+		 * common code for all cases first and then we retrieve the block id.
+		 * The block id is necesary to determine which block does the id belong
+		 * to. This is used to execute block specific code. Similar to switch
+		 * cases ONLY here we try to minimize the no of blocks.
+		 *
+		 * In this example , we store the offsets of field names of a ptr in the
+		 * global table.This is necessary to combine cases with "similar" but
+		 * not "same" codes.
+		 *
+		 * Eg: case 0: ptr->x = 1; break; // similar code
+		 *     case 1: ptr->y = 1; break; // similar code
+		 *  -- Combined block ----
+		 *    case 0:
+		 *    case 1: PTR_OFFSET_TYPE(ptr, table[i].offset) = 1; break;
+		 *
+		 * By reducing the number of blocks we save on Code space. After we
+		 * finish our work , we exit the for loop and check for invalid ID
+		 * passed by the upper layer. This is akin to default in the
+		 * switch block
+		 *
+		 * CAUTION: If there is any change in the switch above, this code
+		 * along with the tables have to be re-written and changed according
+		 * to the new behaviour of the switch. Examples include adding a new
+		 * case in the switch. The reason for all this mumbo-jumbo is to
+		 * reduce code size in VBIOS, where we are running out of code space.
+		 */
+		for(j = 0; j < num_case_ids; j++) {
+			/* Search for the attribute ID in the global table */
+			if(list->id == table_opt_data1[j].id) {
+				/* Run the common code for all the blocks */
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				/* Once we get a valid ID, need to find out which block it
+				 *  belongs so we can execute block specific code.
+				 */
+				if(table_opt_data1[j].block == 1) { /* block 1 */
+					/* Got the block. Need the offset to the struct for that ID
+					* so we can store value at corresponding offset to get the
+					* desired behaviour for that ID.This is the code that makes
+					* the whole optimization work because we are combining the
+					* case IDs into a single block which saves code.
+					*/
+					attr->current_value = list->current_value;
+					PTR_OFFSET_USHORT(pd_context, table_opt_data1[j].offset) =
+					(unsigned short) attr->current_value;
+
+				} else { /* block 2. We only have two blocks. */
+					attr->current_value = LVDS_MIN(
+						list->current_value, ((pd_range_attr_t *)attr)->max);
+				}
+				break; /* We found a valid ID, so break inner for loop */
+			}
+		}
+	}
+	/* panel_type 0 (SPWG) isn't available for 18-bit depth */
+	PD_DEBUG("in LVDS_set_attributes()\n");
+	if (pd_context->panel_depth == 18) {
+		pd_context->panel_type = 1;
+	}
+	PD_DEBUG("IntLVDS: dual_channel=%u", pd_context->dual_channel);
+	PD_DEBUG("IntLVDS: panel_type=%u panel_fit=%u panel_dep=%u dither=%u",
+		pd_context->panel_type, pd_context->panel_fit,
+		pd_context->panel_depth, pd_context->dither);
+	PD_DEBUG("IntLVDS: keep_aspect_ratio=%u text_tune=%lu",
+		pd_context->aspect_ratio, pd_context->text_tune);
+	PD_DEBUG("IntLVDS: PWM Intensity=%u Inverter Freq=%u, BLM legacy mode =%u",
+		pd_context->pwm_intensity, pd_context->inverter_freq,
+		pd_context->blm_legacy_mode);
+	PD_DEBUG("IntLVDS: tc_110MHz_clk = %u", pd_context->tc_110MHz_clk);
+
+	if (pd_context->init_done) {
+		/* When emgd_driver_pre_init() pokes new attrs into this port driver,
+		 * pd_context->current_mode must be set before calling
+		 * internal_lvds_panel_fit(), so set it to the first entry in the timing table:
+		 */
+		if (pd_context->current_mode == NULL) {
+			pd_context->current_mode = pd_context->timing_table;
+		}
+		internal_lvds_panel_fit(pd_context); 
+	}
+
+	return ret;
+} /* internal_lvds_set_attrs */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_get_attrs
+ *
+ * Description:
+ *    Extracts the attribute list and the number of elements in the list
+ *    from the device context.
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] num:    number of elements in list
+ *    [OUT] list:   list of attributes from the device context
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR: if one of the parameters is invalid
+ *    PD_SUCCESS:      if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+int internal_lvds_get_attrs (void *context, unsigned long *num, pd_attr_t **list)
+{                                                         /* internal_lvds_get_attrs */
+	/* basic parameter check */
+	if (!context || !num || !list) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	PD_DEBUG("internal_lvds_get_attrs()\n");
+	/* Nothing fancy, just extracting the elements from the list */
+	*list = ((internal_lvds_context_t *)context)->attr_list;
+	*num  = ((internal_lvds_context_t *)context)->num_attrs;
+
+	return PD_SUCCESS;
+}                                                         /* internal_lvds_get_attrs */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_get_timing_list
+ *
+ * Description:
+ *
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] in_list:
+ *    [OUT] list:
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR: if one of the parameters is invalid
+ *    PD_ERR_NOMEM:    if internal memory allocate failed
+ *    PD_SUCCESS:      if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+int internal_lvds_get_timing_list (void *context, pd_timing_t *in_list,
+	pd_timing_t **list)
+{                                                   /* internal_lvds_get_timing_list */
+	internal_lvds_context_t *pd_context = (internal_lvds_context_t *)context;
+	pd_dvo_info_t internal_lvds_info = {0, 0, 1, 0, 0, 0, 0, 0};
+	pd_display_info_t internal_lvds_display_info = {0, 0, 0, 0, NULL};
+	int ret;
+
+	PD_DEBUG("internal_lvds_get_timing_list()\n");
+	
+	PD_DEBUG ("NUHAIRI : context = %lu,  pd_context = %lu, &internal_lvds_info = %lu\n",context,  pd_context, &internal_lvds_info);
+	
+	internal_lvds_get_dclk( pd_context, &internal_lvds_info );
+
+	PD_DEBUG("chipset = 0x%x", pd_context->chipset);
+	internal_lvds_display_info.panel_fit = (unsigned char) pd_context->panel_fit;
+	ret = pd_filter_timings(pd_context->callback->callback_context,
+		in_list, &pd_context->timing_table, &internal_lvds_info, &internal_lvds_display_info);
+
+	/* Helper function will return the below values */
+	pd_context->native_dtd = internal_lvds_display_info.native_dtd;
+	pd_context->fp_width = internal_lvds_display_info.width;
+	pd_context->fp_height = internal_lvds_display_info.height;
+
+	*list = pd_context->timing_table;
+	return ret;
+}                                                   /* internal_lvds_get_timing_list */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_set_power
+ *
+ * Description:
+ *  Sets LVDS to the specified power state
+ *
+ *  Conversion between IEGD timer values to LVDS port timer values
+ *
+ *  SEPG(?) IEGD    LVDS Port    Program bits     min     max
+ *  ----    ------- ------------ ---------------- ------ ---------
+ *  T1+T2   T1 ms   T1+T2 100us  0x61208 [28:16]  0 ms   819.2 ms
+ *  T5      T2 ms   T5    100us  0x61208 [12:00]  0 ms   819.2 ms
+ *  T6      T3 ms   Tx    100us  0x6120C [12:00]  0 ms   819.2 ms
+ *  T3      T4 ms   T3    100us  0x6120C [28:16]  0 ms   819.2 ms
+ *  T4      T5 ms   T4    100ms  0x61210 [04:00]  0 ms   3200  ms
+ *
+ *  Reg     = Value
+ *  ------    -------
+ *  0x61208 = [T1 T2]
+ *  0x6120C = [T4 T3]
+ *  0x61210 = [   T5]
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] in_list:
+ *    [OUT] list:
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:      if context is NULL
+ *    PD_ERR_INVALID_POWER: if "state" is invalid
+ *    PD_SUCCESS:           if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+/* Tables required by the optimization codes in internal_lvds_set_power() */
+typedef struct {
+	unsigned char id1;
+	unsigned char id2;
+	unsigned char bit;
+	unsigned long reg;
+} opt_set_power_t;
+
+static opt_set_power_t table_set_power[] = {
+	/* id1                  id2                   bit    reg */
+	{ PD_ATTR_ID_FP_PWR_T1, PD_ATTR_ID_FP_PWR_T2, 1,     0x61208 },  /* D0 */
+	{ PD_ATTR_ID_FP_PWR_T4, PD_ATTR_ID_FP_PWR_T3, 0,     0x6120C },  /* Dx */
+};
+
+int internal_lvds_set_power(void *context, unsigned long state)
+{                                                         /* internal_lvds_set_power */
+	unsigned long  i           = 0;
+	internal_lvds_context_t *pd_context = (internal_lvds_context_t *)context;
+	pd_attr_t      *tattr      = NULL;  /* holds time delay b/ pwr transition */
+	unsigned long            delay = 0, delay1;
+
+	PD_DEBUG("state = %lu", state);
+
+	PD_DEBUG("internal_lvds_set_power() to state = %lu\n",state);
+	/* Basic parameter check */
+	if (!context) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	/* Check for invalid state */
+	if (state > PD_POWER_MODE_D3) {
+		return PD_ERR_INVALID_POWER;
+	}
+
+	/* Get the index into above table */
+	if (state == PD_POWER_MODE_D0) {
+		i = 0;
+	} else {
+		i = 1;
+	}
+
+	internal_lvds_write_reg(pd_context, 0x61204, 0xABCD0000, 0xFFFFFFFF, PD_REG_MIO);
+
+	/* Program panel power up/down delays: Either T1/T2 or T3/T4*/
+	tattr = pd_get_attr(pd_context->attr_list,
+		pd_context->num_attrs, table_set_power[i].id1, 0);
+	/* Convert ms to 100us */
+	delay1 = tattr->current_value;
+	delay = (tattr->current_value * 10) << 16;
+	tattr = pd_get_attr(pd_context->attr_list,
+		pd_context->num_attrs, table_set_power[i].id2, 0);
+	delay1 += tattr->current_value;
+	delay |= tattr->current_value * 10;
+
+	internal_lvds_write_reg(pd_context, table_set_power[i].reg, delay, 0x1FFF1FFF, PD_REG_MIO);
+
+	/* Program power cycle delay: convert ms to 100ms */
+	delay = pd_get_attr(pd_context->attr_list,
+		pd_context->num_attrs, PD_ATTR_ID_FP_PWR_T5, 0)->current_value;
+	delay1 += delay;
+	/* TODO: Write reference divider [31:8] */
+	delay = ((delay/100+1) & 0xFF) | ((pd_context->gfx_freq*100/2-1)<<8);
+	internal_lvds_write_reg(pd_context, 0x61210, delay, 0xFFFFFFFF, PD_REG_MIO);
+
+	/* Power state target */
+	internal_lvds_write_reg(pd_context, 0x61204, table_set_power[i].bit, BIT(0), PD_REG_MIO);
+
+	/* Power down on reset available on crestline onwards */
+		internal_lvds_write_reg(pd_context, 0x61204, BIT(1), BIT(1), PD_REG_MIO);
+
+/* Make this a compile time so that size of vBIOS doesn't become > 64KB */
+#if defined(CONFIG_PLB) || defined(CONFIG_TNC)
+	/* PWM is a method of controlling the backlight intensity.
+	 * It is not method to turn on baclkight.
+	 * We still need the PD method to turn on the backlight.
+	 *
+	 * This feature is for Pouslbo Only. We check that the user has set the
+	 * inverter frequency. Default intensity, if not set, is 100%
+	 *
+	 * Due to the high amount of calculation, we want to only set this register
+	 * if it has not been ser previously. The register could be
+	 * "brought forward" from VBIOS.
+	 */
+	if(pd_context->inverter_freq != 0xFFFF &&  /* Overwritten by set_attr */
+		(pd_context->chipset == PCI_DEVICE_ID_VGA_PLB ||
+		 pd_context->chipset == PCI_DEVICE_ID_VGA_TNC ||
+		 pd_context->chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+		 pd_context->chipset == PCI_DEVICE_ID_VGA_LNC) &&
+		 !pd_context->pwm_done) {
+		unsigned long reg_value = 0;
+		unsigned long percentage = 0;
+		unsigned long calculation = 0;
+		unsigned long blc_pwm_ctl2 = 0;
+
+		/* We first need to get the graphics frequency, which will be used to
+		 * calculate Backlight Modulation Frequency[BMF]. BMF will be used to
+		 * fill up the 15 MSB in the 0x61254 register
+		 *
+		 * The calculation for the Modulation Frequency field in the
+		 * BLC_PWM_CTL Register is:
+		 *
+         *     Reference Clock Freq               1
+         *     -----------------------   x    ------------------
+         *            Divider                   PWM Freq in Hz
+		 *
+		 */
+#if 0
+		/* GMA accurate calculation that requires "calculation" to be an
+		 * unsigned long long typedef */
+		calculation = pd_context->gfx_freq * PWM_FREQ_CALC_CONSTANT_2;
+		calculation = calculation / 0x20; /*pouslbo specific divider*/
+		calculation = calculation * PWM_FREQ_CALC_CONSTANT_1;
+		calculation = calculation / pd_context->inverter_freq;
+		calculation = calculation / PWM_FREQ_CALC_CONSTANT_1;
+#endif
+		/* Some system bios cannot take 64 bit data type. Using a more
+		 * simplified calculation that is not too accurate if the inputs
+		 * are not round numbers */
+		calculation = pd_context->inverter_freq * 0x20; /* plb/tnc divider */
+		calculation = (pd_context->gfx_freq * PWM_FREQ_CALC_CONSTANT_2) /
+			calculation;
+
+		/* Writing the register: 15 MSB is the max lvds clock / 32.
+		* Bit 16 can either be legacy or non legacy depending upon Attr #72. */
+		if (pd_context->gn4_plus) {
+			blc_pwm_ctl2 = (1L << 31) |
+				(pd_context->blm_legacy_mode << 30) |
+				((pd_context->pipe == PD_SET_MODE_PIPE_B)?1L:0L << 29);
+			internal_lvds_write_reg(pd_context, 0x61250, blc_pwm_ctl2, 0xFFFFFFFF, PD_REG_MIO);
+			reg_value = (calculation & 0xFFFF)<<16;
+		} else {
+			reg_value = ((calculation & 0xFFFE) |
+				pd_context->blm_legacy_mode)<<16;
+		}
+
+		/* The 16 LSB is a value that the user sets in configuration.
+		 * user sets the value in percentage.
+		 * We convert it into the clock speed */
+		percentage = ( pd_context->pwm_intensity * (unsigned long)calculation);
+		reg_value |= (unsigned long)( percentage / (int)100 ) & 0xFFFE;
+		internal_lvds_write_reg(pd_context, 0x61254, reg_value, 0xFFFFFFFF, PD_REG_MIO);
+
+		/* set the flag so that we only do this function once */
+		pd_context->pwm_done = 1;
+	}
+#endif
+
+	if (state != PD_POWER_MODE_D0) {
+		/* Wait until the current power up/down sequence is complete */
+		i = 0;
+		while(internal_lvds_read_reg(pd_context, 0x61200, PD_REG_MIO) & 0x80000000L) {
+			i++;
+			if(i > 0x100000L) {
+				break;
+			}
+		}
+		internal_lvds_write_reg(pd_context, 0x61180, 0, BIT(31), PD_REG_MIO);
+		//pd_usleep(delay1*1000);	
+	}
+
+#ifdef CONFIG_TNC
+#if 0
+
+/*-----------------------------------------------------------------------------
+ * LPC Register Offsets. Used for LVDS BKLT control. Registers are part
+ * Atom E6xx [D31:F0]
+ ----------------------------------------------------------------------------*/
+#define RGEN    0x20
+#define RGIO    0x24
+#define RGLVL   0x28
+#define TNC_LVDS_VDDEN	 BIT(0)
+#define TNC_LVDS_BKLTEN  BIT(1)
+#define TNC_LVDS_BKLTCTL BIT(2) 
+
+	if (pd_context->chipset == PCI_DEVICE_ID_VGA_TNC ||
+		pd_context->chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+		pd_context->chipset == PCI_DEVICE_ID_VGA_LNC) {
+		unsigned long value;
+
+		/* Enable backlight for LVDS: based on observed si behavior:
+		 * Subject to change based on si DE feedback */
+		if (state == PD_POWER_MODE_D0) {
+			value = TNC_LVDS_BKLTCTL|TNC_LVDS_BKLTEN|TNC_LVDS_BKLTCTL;
+		} else {
+			value = 0;
+		}
+		internal_lvds_write_reg(pd_context, RGEN, value,			 
+			TNC_LVDS_BKLTCTL|TNC_LVDS_BKLTEN|TNC_LVDS_BKLTCTL, PD_REG_LPC);
+		internal_lvds_write_reg(pd_context, RGIO, value,
+			TNC_LVDS_BKLTCTL|TNC_LVDS_BKLTEN|TNC_LVDS_BKLTCTL, PD_REG_LPC);
+	}
+#endif
+#endif
+
+	/* update power state */
+	pd_context->power_state = state;
+	return PD_SUCCESS;
+}                                                         /* internal_lvds_set_power */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_get_power
+ *
+ * Description:
+ *   Returns the current LVDS power state back to the caller.
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] state:  current power state
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:      if one of the parameters is invalid
+ *    PD_SUCCESS:           if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int internal_lvds_get_power (void *context, unsigned long *state)
+{                                                         /* internal_lvds_get_power */
+	if ((NULL == context) || (NULL == state)) {
+		return PD_ERR_NULL_PTR;
+	}
+	PD_DEBUG("internal_lvds_get_power()\n");
+	/* The caller should be able to do this himself, but whatever */
+	*state = ((internal_lvds_context_t *) context)->power_state;
+	return PD_SUCCESS;
+}                                                         /* internal_lvds_get_power */
+
+int internal_lvds_save(void *context, void **state, unsigned long flags)
+{
+	*state = NULL;
+	return PD_SUCCESS;
+}
+
+int internal_lvds_restore(void *context, void *state, unsigned long flags)
+{
+	int ret = PD_SUCCESS;
+	return ret;
+}
+
+/*----------------------------------------------------------------------
+ * Function: internal_lvds_get_port_status()
+ *
+ * Description:  It is called to get the information about the display
+ *
+ * Parameters:  context - Port driver's context
+ *				port_status - Returns the display type and connection state
+ *
+ * Return:      PD_SUCCESS(0)  success
+ *              PD_ERR_XXXXXX  otherwise
+ *----------------------------------------------------------------------*/
+int internal_lvds_get_port_status(void *context, pd_port_status_t *port_status)
+{
+	/* Display connection cannot be determined */
+	port_status->display_type = PD_DISPLAY_LVDS_INT;
+	port_status->connected    = PD_DISP_STATUS_UNKNOWN;
+	return PD_SUCCESS;
+}
+
+static unsigned long internal_lvds_read_reg(internal_lvds_context_t *pd_context,unsigned long reg,
+	unsigned long reg_type)
+{
+	pd_reg_t list[2];
+	int ret;
+
+	list[0].reg = reg;
+	list[0].value = 0;
+	list[1].reg = PD_REG_LIST_END;
+	ret = pd_context->callback->read_regs(
+		pd_context->callback->callback_context, list, reg_type);
+	if (ret) {
+		PD_ERROR("LVDS read regs: Failed.");
+	}
+	return list[0].value;
+}
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_write_reg
+ *
+ * Description:
+ *    Writes bits in "value" into a register.  Bits written are dictated by
+ *    the "change_bits" mask.
+ *
+ * Parameters:
+ *    [IN] pd_context:  device context, dispatcher to the actual write_reg
+ *                      function
+ *    [IN] reg:  register to write value to
+ *    [IN] value:  value to change the register to
+ *    [IN] change_bits:  bit mask, the bits set to "1" will be modified by
+ *                       the corresponding bits in "value"
+ *
+ * Return:
+ *    None
+ *
+ *----------------------------------------------------------------------------
+ */
+static void internal_lvds_write_reg(internal_lvds_context_t *pd_context, unsigned long reg,
+		unsigned long value,
+		unsigned long change_bits,
+		unsigned long reg_type)
+{                                                         /* internal_lvds_write_reg */
+	pd_reg_t list[2];
+	int ret;
+
+	list[0].reg = reg;
+	list[0].value = (internal_lvds_read_reg(pd_context, reg, PD_REG_MIO) & ~change_bits) | value;
+	list[1].reg = PD_REG_LIST_END;
+	ret = pd_context->callback->write_regs(
+		pd_context->callback->callback_context, list, reg_type);
+	if (ret) {
+		PD_ERROR("LVDS write regs: Failed.");
+	}
+	return;
+}                                                         /* internal_lvds_write_reg */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_panel_fit
+ *
+ * Description:
+ *    Enables panel fitting
+ *
+ * Parameters:
+ *    [IN] pd_context:  device context
+ *
+ * Return:
+ *    None
+ *
+ *----------------------------------------------------------------------------
+ */
+static void internal_lvds_panel_fit(internal_lvds_context_t *pd_context)
+{
+	/* enable auto vertical ratio */
+	/* enable auto horizantal ratio */
+	/* no dither */
+	unsigned long panel_fit_reg = 0x00000220;
+	PD_DEBUG("internal_lvds_panel_fit() \n");
+
+	PD_TRACE_ENTER;
+
+	if (pd_context->current_mode->width != pd_context->fp_width ||
+		pd_context->current_mode->height != pd_context->fp_height) {
+		/* Enable panel fitting */
+		/* vertical interpolation = bilinear */
+		/* horizontal interpolation = bilinear */
+		panel_fit_reg |= 0x80000440;
+	}
+	/* Enable dither based on default/user-set value:
+	 *   By default
+	 *        dither = 1 for 18-bit panels
+	 *               = 0 for 24-bit panels.
+	 *   But this behavior can be changed by setting the DITHER attribute.
+	 *        When user sets the attribute, dither will be updated
+	 *        as part of attribute processing in set attributes. */
+	/* For gn4 based chipsets dither is controlled in port_control register */
+	if (!pd_context->gn4_plus) {
+		/* Default behavior */
+		if (pd_context->panel_depth == 18) {
+			panel_fit_reg |= BIT(3);
+		}
+
+		/* Overwritten by set attribute */
+		if (pd_context->dither != 0xFFFF) {
+			if (pd_context->dither) {
+				panel_fit_reg |= BIT(3);
+			} else {
+				panel_fit_reg &= ~BIT(3);
+			}
+		}
+	}
+
+	if (pd_context->gn4_plus) {
+		unsigned long src_ratio, dest_ratio;
+		panel_fit_reg = 0;
+		if (pd_context->native_dtd &&
+			(pd_context->current_mode->width != pd_context->native_dtd->width ||
+			pd_context->current_mode->height !=
+				pd_context->native_dtd->height)) {
+			/* Enable panel fitter */
+			panel_fit_reg = 0x80000000;
+
+			/* Select the pipe */
+			if (pd_context->pipe & PD_SET_MODE_PIPE_B) {
+				panel_fit_reg |= BIT(29);   /* bits[30:29] = 01 for pipe B */
+			}
+
+			/* Scaling mode:
+			 *    Default - Auto scaling src_ratio == dest_ratio
+			 *    Piller box scaling - src_ratio < dest_ratio
+			 *    Letter box scaling - src_ratio > dest_ratio */
+
+			/* To make this work correctly, port driver shall know the
+			 * size of the framebuffer, not the src mode. Most of the
+			 * times the src mode is fb, but not all the cases.
+			 * User has an attribute to change
+			 *    1. Between Pillerbox and auto, and vice versa
+			 *                and
+			 *    2. Between Letterbox and auto, and vice versa.
+			 */
+			if (pd_context->aspect_ratio) {
+				src_ratio = (pd_context->current_mode->width << 10)/
+					(pd_context->current_mode->height);
+				dest_ratio = (pd_context->native_dtd->width << 10)/
+					(pd_context->native_dtd->height);
+
+				if (dest_ratio > src_ratio) {
+					/* Pillarbox scaling */
+					panel_fit_reg |= BIT(27);
+				} else if (dest_ratio < src_ratio) {
+					/* Letterbox scaling */
+					panel_fit_reg |= BIT(27) | BIT(26);
+				}
+			}
+
+			/* Filter coefficient select: pd_context->text_tune = 0,1,2 */
+			panel_fit_reg |= (pd_context->text_tune << 24);
+		}
+	}
+
+	internal_lvds_write_reg(pd_context, 0x61230, panel_fit_reg, 0xFFFFFFFF, PD_REG_MIO);
+	PD_DEBUG("panel_fit_reg 0x61230 = 0x%lx", panel_fit_reg);
+}
+
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: internal_lvds_get_dclk
+ *
+ * Description:
+ *    Gets the Dclk for LVDS
+ *
+ * Parameters:
+ *    [IN] pd_context:  device context
+ *	  [OUT]internal_lvds_info:   Structure that contains the min and max dclk
+ *
+ * Return:
+ *    None
+ *
+ *----------------------------------------------------------------------------
+ */
+static void internal_lvds_get_dclk(internal_lvds_context_t *pd_context, pd_dvo_info_t *internal_lvds_info )
+{
+	PD_DEBUG("internal_lvds_get_dclk()\n");
+	/* Get the min and max dclks for lvds */
+	if (pd_context->dual_channel) {
+		internal_lvds_info->min_dclk = LVDS_MIN_DCLK * 2 ;
+		internal_lvds_info->max_dclk = LVDS_MAX_DCLK * 2;
+	} else {
+		internal_lvds_info->min_dclk = LVDS_MIN_DCLK;
+		internal_lvds_info->max_dclk = LVDS_MAX_DCLK;
+	}
+	PD_DEBUG("internal_lvds_dclk: #1\n");
+/* This #define is the result of code size reduction effort. */
+#ifdef CONFIG_CTG
+	/* Set dclk for GM965 */
+	if(pd_context->chipset==PCI_DEVICE_ID_VGA_CTG){
+		/* Set dclk for GM965/CTG */
+		if (pd_context->dual_channel) {
+			internal_lvds_info->min_dclk = LVDS_GM965_DUAL_MIN_DCLK ;
+			internal_lvds_info->max_dclk = LVDS_GM965_DUAL_MAX_DCLK;
+		} else {
+			internal_lvds_info->min_dclk = LVDS_GM965_SINGLE_MIN_DCLK;
+			internal_lvds_info->max_dclk = LVDS_GM965_SINGLE_MAX_DCLK;
+		}
+	}
+#else
+
+	/* Set dclk for 915GM */
+	if(pd_context->chipset==PCI_DEVICE_ID_VGA_915AL){
+		if (pd_context->dual_channel) {
+			internal_lvds_info->min_dclk = LVDS_915GM_DUAL_MIN_DCLK ;
+			internal_lvds_info->max_dclk = LVDS_915GM_DUAL_MAX_DCLK;
+		} else {
+			internal_lvds_info->min_dclk = LVDS_915GM_SINGLE_MIN_DCLK;
+			internal_lvds_info->max_dclk = LVDS_915GM_SINGLE_MAX_DCLK;
+		}
+	} else if(pd_context->chipset==PCI_DEVICE_ID_VGA_945GM ||
+				pd_context->chipset==PCI_DEVICE_ID_VGA_945GME){
+		/* Set dclk for 945GM */
+		if (pd_context->dual_channel) {
+			internal_lvds_info->min_dclk = LVDS_945GM_DUAL_MIN_DCLK ;
+			internal_lvds_info->max_dclk = LVDS_945GM_DUAL_MAX_DCLK;
+		} else {
+			internal_lvds_info->min_dclk = LVDS_945GM_SINGLE_MIN_DCLK;
+			internal_lvds_info->max_dclk = LVDS_945GM_SINGLE_MAX_DCLK;
+		}
+	} else if(pd_context->chipset==PCI_DEVICE_ID_VGA_GM965 ||
+				pd_context->chipset==PCI_DEVICE_ID_VGA_GME965){
+		/* Set dclk for GM965 */
+		if (pd_context->dual_channel) {
+			internal_lvds_info->min_dclk = LVDS_GM965_DUAL_MIN_DCLK ;
+			internal_lvds_info->max_dclk = LVDS_GM965_DUAL_MAX_DCLK;
+		} else {
+			internal_lvds_info->min_dclk = LVDS_GM965_SINGLE_MIN_DCLK;
+			internal_lvds_info->max_dclk = LVDS_GM965_SINGLE_MAX_DCLK;
+		}
+	}
+#endif
+
+#ifdef CONFIG_TNC
+	/* Get the min and max dclks for Atom E6xx lvds */
+	if ((pd_context->chipset == PCI_DEVICE_ID_VGA_TNC) ||
+	    (pd_context->chipset == PCI_DEVICE_ID_VGA_TNC_A0) ||
+	    (pd_context->chipset == PCI_DEVICE_ID_VGA_LNC)) {
+		internal_lvds_info->min_dclk = LVDS_TNC_SINGLE_MIN_DCLK;
+		/* Experimental feature to raise TC LVDS clk to 110MHz. */
+		if (pd_context->tc_110MHz_clk) {
+			internal_lvds_info->max_dclk = 110000L;
+		} else {
+			internal_lvds_info->max_dclk = LVDS_TNC_SINGLE_MAX_DCLK;
+		}
+		PD_DEBUG("internal_lvds_dclk: #2\n");
+	}
+#endif
+	PD_DEBUG("internal_lvds_dclk: at the end\n");
+}
+
+#pragma optimize ("",on)
diff --git a/drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.h b/drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.h
new file mode 100644
index 0000000..f691d1a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/ch7036/lvds/lvds.h
@@ -0,0 +1,170 @@
+/* -*- pse-c -*-
+ *-----------------------------------------------------------------------------
+ * Filename: lvds.h
+ * $Revision: 1.3 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is header file for Internal LVDS PORT DRIVER.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PD_LVDS_H
+#define _PD_LVDS_H
+
+#include <pd.h>
+
+#ifdef T_LINUX 
+#include <pci.h>
+#endif
+
+/* Values received from DPG hardware engineer. These are for single channel.  */
+
+#ifndef PCI_DEVICE_ID_VGA_855
+#define PCI_DEVICE_ID_VGA_855      0x3582
+#endif
+#ifndef PCI_DEVICE_ID_VGA_915AL
+#define PCI_DEVICE_ID_VGA_915AL    0x2592
+#endif
+#ifndef PCI_DEVICE_ID_VGA_945GM
+#define PCI_DEVICE_ID_VGA_945GM    0x27A2
+#endif
+#ifndef PCI_DEVICE_ID_VGA_945GME
+#define PCI_DEVICE_ID_VGA_945GME   0x27AE
+#endif
+#ifndef PCI_DEVICE_ID_VGA_GM965
+#define PCI_DEVICE_ID_VGA_GM965    0x2A02
+#endif
+#ifndef PCI_DEVICE_ID_VGA_GME965
+#define PCI_DEVICE_ID_VGA_GME965   0x2A12
+#endif
+#ifndef PCI_DEVICE_ID_VGA_CTG
+#define PCI_DEVICE_ID_VGA_CTG      0x2A42
+#endif
+#ifndef PCI_DEVICE_ID_VGA_PLB
+#define PCI_DEVICE_ID_VGA_PLB      0x8108
+#endif
+#ifndef PCI_DEVICE_ID_VGA_PNVM
+#define PCI_DEVICE_ID_VGA_PNVM     0xA011
+#endif
+#ifndef PCI_DEVICE_ID_VGA_TNC
+#define PCI_DEVICE_ID_VGA_TNC      0x4108
+#endif
+#ifndef PCI_DEVICE_ID_VGA_TNC_A0
+#define PCI_DEVICE_ID_VGA_TNC_A0   0x4100
+#endif
+#ifndef PCI_DEVICE_ID_VGA_LNC
+#define PCI_DEVICE_ID_VGA_LNC      0x4102
+#endif
+
+
+#define LVDS_MIN_DCLK 12000L    /* in KHz */
+#define LVDS_MAX_DCLK 112000L   /* in KHz */
+/* definition for 915GM */
+#define LVDS_915GM_SINGLE_MIN_DCLK   20000L
+#define LVDS_915GM_SINGLE_MAX_DCLK  112000L
+#define LVDS_915GM_DUAL_MIN_DCLK     25000L
+#define LVDS_915GM_DUAL_MAX_DCLK    224000L
+
+/* definition for 945GM */
+#define LVDS_945GM_SINGLE_MIN_DCLK   25000L
+#define LVDS_945GM_SINGLE_MAX_DCLK  112000L
+#define LVDS_945GM_DUAL_MIN_DCLK     25000L
+#define LVDS_945GM_DUAL_MAX_DCLK    224000L
+
+/* definition for GM965 */
+#define LVDS_GM965_SINGLE_MIN_DCLK   25000L
+#define LVDS_GM965_SINGLE_MAX_DCLK  112000L
+#define LVDS_GM965_DUAL_MIN_DCLK     25000L
+#define LVDS_GM965_DUAL_MAX_DCLK    224000L
+
+/* definition for Atom E6xx */
+#define LVDS_TNC_SINGLE_MIN_DCLK     19750L
+#define LVDS_TNC_SINGLE_MAX_DCLK     79500L
+
+#define LVDS_MIN(a, b)       ((a)<(b)?(a):(b))
+
+/* Local attributes
+ * BIT15 of attribute Id specifies that it is a local attribute */
+#define LVDS_ATTR_ID_DATA_FORMAT   (BIT(15)|1)
+#define LVDS_ATTR_ID_TC_LVDS_CLK   (BIT(15)|2)
+
+/* Default values */
+#define LVDS_DEF_PANEL_DEPTH    18      /* Default panel depth */
+#define LVDS_DEF_DITHER          1      /* default=1 as default panel depth=18*/
+#define LVDS_DEF_SCALE_ON        1      /* Default scaling is on */
+#define LVDS_DEF_PANEL_FIT       1      /* Default panel fit in on */
+
+typedef struct _internal_lvds_context {
+	unsigned short fp_width;
+	unsigned short fp_height;
+	unsigned short dual_channel;
+	unsigned short panel_type;
+	unsigned short panel_fit;
+	unsigned short panel_depth;
+	unsigned short dither;
+	unsigned short aspect_ratio;
+	unsigned long  text_tune;
+	unsigned long  pwm_intensity;
+	unsigned long  inverter_freq;
+	unsigned long  blm_legacy_mode;
+
+	unsigned long power_state;
+	unsigned short chipset;
+	unsigned char init_done;
+	unsigned char num_attrs;
+	unsigned long pipe;
+	unsigned short gfx_freq;
+	unsigned char gn4_plus;
+	unsigned char pwm_done;
+	unsigned char tc_110MHz_clk;
+
+	pd_callback_t *callback;
+	pd_timing_t   *timing_table;
+	pd_attr_t     *attr_list;
+	pd_timing_t   *native_dtd;
+	pd_timing_t   *current_mode;
+} internal_lvds_context_t;
+
+extern int PD_MODULE_INIT(internal_lvds_init, (void *handle));
+extern int PD_MODULE_EXIT(internal_lvds_exit, (void));
+extern unsigned long internal_lvds_validate(unsigned long cookie);
+extern int internal_lvds_open(pd_callback_t *callback, void **context);
+extern int internal_lvds_init_device(void *context);
+extern int internal_lvds_close(void *context);
+extern int internal_lvds_set_mode(void *context, pd_timing_t *mode,
+			unsigned long flags);
+extern int internal_lvds_post_set_mode(void *context, pd_timing_t *mode,
+			unsigned long flags);
+extern int internal_lvds_set_attrs(void *context, unsigned long num, pd_attr_t *list);
+extern int internal_lvds_get_attrs(void *context, unsigned long*num, pd_attr_t **list);
+extern int internal_lvds_get_timing_list(void *context, pd_timing_t *in_list,
+			pd_timing_t **list);
+extern int internal_lvds_set_power(void *context, unsigned long state);
+extern int internal_lvds_get_power(void *context, unsigned long *state);
+extern int internal_lvds_save(void *context, void **state, unsigned long flags);
+extern int internal_lvds_restore(void *context, void *state, unsigned long flags);
+extern int internal_lvds_get_port_status(void *context, pd_port_status_t *port_status);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/lpd/lpd.c b/drivers/gpu/drm/emgd/emgd/pal/lpd/lpd.c
new file mode 100644
index 0000000..a180772
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/lpd/lpd.c
@@ -0,0 +1,114 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: lpd.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Functions necessary to allow debug printing from the port drivers.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.dpd
+
+#include <igd_pd.h>
+#include <igd_pi.h>
+#include "pd_print.h"
+#include <memory.h>
+
+
+/*
+ * This file implements the pd debug printing functions using the OAL.
+ * In order to keep the OAL debug printing macro definitions in the OAL
+ * the DEBUG_BUILD_TYPE option must be set, even in a production build. The
+ * port drivers themselves must not call the pd debug printing functions
+ * in a release build. This enables the PD SDK to have debug printing from
+ * port drivers, even when using a release driver.
+ */
+#ifndef DEBUG_BUILD_TYPE
+#define DEBUG_BUILD_TYPE
+#endif
+#define MODULE_NAME hal.dpd
+
+#include <io.h>
+#include <igd_debug.h>
+
+unsigned long *dropped_debug_messages;
+
+int pd_set_funcname( const char *name )
+{
+	os_set_funcname(name);
+	return 1;
+}
+
+int pd_print(const char *funcname, const int error,
+	const int error_exit, const char *format, ...)
+{
+#ifdef DEBUG_BUILD_TYPE
+	va_list ap;
+	unsigned int *blah;
+	char *priority = error ? KERN_ERR : EMGD_DEBUG_MSG_PRIORITY;
+	char *fmt = NULL;
+
+	/* Can't directly use the EMGD_DEBUG_S macro (because "format" is a string
+	 * variable), so duplicate some of it here:
+	 */
+	if (!(emgd_debug && emgd_debug-> MODULE_NAME)) {
+		return 0;
+	}
+
+	va_start(ap, format);
+	blah = (unsigned int *)ap;
+
+	if (error_exit) {
+		EMGD_DEBUG("EXIT With Error...");
+	}
+
+	/* Create a new format string, with all of the correct parts: */
+	fmt = OS_ALLOC(strlen(priority) + strlen(funcname) +
+		strlen(format) + 2);
+	if (fmt == NULL) {
+		printk(format, blah[0], blah[1], blah[2], blah[3], blah[4], blah[5],
+			blah[6], blah[7], blah[8], blah[9]);
+	} else {
+		sprintf(fmt, "%s%s %s", priority, funcname, format);
+		printk(fmt, blah[0], blah[1], blah[2], blah[3], blah[4], blah[5],
+			blah[6], blah[7], blah[8], blah[9]);
+		OS_FREE(fmt);
+	}
+	printk("\n");
+	va_end(ap);
+
+	return 0;
+#endif
+}
+
+void pd_trace_enter(const char *funcname)
+{
+	EMGD_DEBUG_S("%s ENTER", funcname);
+}
+
+void pd_trace_exit(const char *funcname)
+{
+	EMGD_DEBUG_S("%s EXIT", funcname);
+}
diff --git a/drivers/gpu/drm/emgd/emgd/pal/lpd/pd_print.h b/drivers/gpu/drm/emgd/emgd/pal/lpd/pd_print.h
new file mode 100644
index 0000000..8893e1b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/lpd/pd_print.h
@@ -0,0 +1,65 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pd_print.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Header file to get printing macros for port drivers
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PD_PRINT_H
+#define _PD_PRINT_H
+
+#include <config.h>
+
+extern int pd_print(const char *funcname, const int error,
+	const int error_exit, const char *format, ...);
+extern void pd_trace_enter(const char *funcname);
+extern void pd_trace_exit(const char *funcname);
+
+#ifdef DEBUG_BUILD_TYPE
+#define PD_DEBUG(arg...) pd_print(__FUNCTION__, 0, 0, arg)
+#define PD_ERROR(arg...) pd_print(__FUNCTION__, 1, 0, arg)
+#define PD_TRACE_ENTER   pd_trace_enter(__FUNCTION__)
+#define PD_TRACE_EXIT    pd_trace_exit(__FUNCTION__)
+#define PD_ERROR_EXIT(arg...) pd_print(__FUNCTION__, 1, 1, arg)
+#else
+#define PD_DEBUG(arg...)
+#define PD_ERROR(arg...)
+#define PD_TRACE_ENTER
+#define PD_TRACE_EXIT
+#define PD_ERROR_EXIT(arg...)
+#endif
+
+#define PD_OFFSETOF(t,m) ((unsigned long)&(((t *)0)->m))
+#if 0
+#define PD_OFFSETOF(t,m) offsetof(t,m)
+#endif
+
+#define PD_MODULE_INIT(func, params) func params
+#define PD_MODULE_EXIT(func, params) func params
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.c b/drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.c
new file mode 100644
index 0000000..b67a992
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.c
@@ -0,0 +1,1555 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: lvds.c
+ * $Revision: 1.18 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file is contains all necessary functions for Internal
+ *  LVDS PORT DRIVER.
+ *  This is written according to the port interface defined in pd.h.
+ *-----------------------------------------------------------------------------
+ */
+#include <linux/kernel.h>
+
+#include <config.h>
+#include <igd_pd.h>
+#include <pd.h>
+#include <pd_print.h>
+
+#include "lvds.h"
+
+/* One space between the #define and the backslash,else compilers complain */
+#define PTR_OFFSET_UCHAR(ptr,offset)   (*((unsigned char *)ptr + offset))
+#define PTR_OFFSET_USHORT(ptr, offset) (*(unsigned short *)((unsigned char *)ptr + offset))
+
+#define PTR_OFFSET_ULONG(ptr, offset) (*(unsigned long *)((unsigned char *)ptr + offset))
+/* END OF OPTIMIZATION MACROS */
+
+/* This constant = 10,000,000.  The value is used to
+ * get effective results from the integer math, and
+ * to not divide by 0. */
+#define PWM_FREQ_CALC_CONSTANT_1        0x00989680
+/* This constant is 1,000,000 - to multiply to get
+ * the Display Clock Frequency to the order of Mhz */
+#define PWM_FREQ_CALC_CONSTANT_2        0x000F4240
+
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(p) (sizeof(p)/sizeof((p)[0]))
+#endif
+
+static pd_version_t  lvds_version = {11, 0, 0, 0}; /* driver version */
+static unsigned long lvds_dab_list[] = {
+	PD_DAB_LIST_END
+}; /* dab list */
+
+static unsigned long supported_chipset[] =
+{
+#ifdef CONFIG_855
+	PCI_DEVICE_ID_VGA_855,
+#endif
+#ifdef CONFIG_915AL
+	PCI_DEVICE_ID_VGA_915AL,
+#endif
+#ifdef CONFIG_945GM
+	PCI_DEVICE_ID_VGA_945GM,
+	PCI_DEVICE_ID_VGA_945GME,
+#endif
+#ifdef CONFIG_965GM
+	PCI_DEVICE_ID_VGA_GM965,
+	PCI_DEVICE_ID_VGA_GME965,
+#endif
+#ifdef CONFIG_CTG
+	PCI_DEVICE_ID_VGA_CTG,
+#endif
+#ifdef CONFIG_PLB
+	PCI_DEVICE_ID_VGA_PLB,
+#endif
+#ifdef CONFIG_TNC
+	PCI_DEVICE_ID_VGA_TNC,
+	PCI_DEVICE_ID_VGA_TNC_A0,
+	PCI_DEVICE_ID_VGA_LNC,
+#endif
+};
+
+static pd_driver_t  lvds_driver  = {
+	PD_SDK_VERSION,
+	"Internal LVDS Port Driver",
+	0,
+	&lvds_version,
+	PD_DISPLAY_LVDS_INT,
+	PD_FLAG_UP_SCALING,
+	lvds_dab_list,
+	100,
+	lvds_validate,
+	lvds_open,
+	lvds_init_device,
+	lvds_close,
+	lvds_set_mode,
+	lvds_post_set_mode,
+	lvds_set_attrs,
+	lvds_get_attrs,
+	lvds_get_timing_list,
+	lvds_set_power,
+	lvds_get_power,
+	lvds_save,
+	lvds_restore,
+	lvds_get_port_status
+};
+
+/* This is a common attribute table for all chipsets. At the end of the table
+ * there are multiple end entries to add chipset specific attributes.
+ * Chipset specific attributes:
+ *     965GM/GM45- Maintain aspect ratio
+ * Note:
+ *    1. Make sure to update the chipset_attr_index whenever adding a
+ *       chipset specific new attr.
+ */
+static pd_attr_t lvds_attrs[] =
+{
+	/* Range attributes */
+
+	/*<-------ID----------->              <----TYPE-------->  <---NAME----->  <----flag---->               <---DEF_VAL---->       <--CURR_VALUE-->      min max  st */
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T1,   PD_ATTR_TYPE_RANGE, "FP Power T1",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T2,   PD_ATTR_TYPE_RANGE, "FP Power T2",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T3,   PD_ATTR_TYPE_RANGE, "FP Power T3",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T4,   PD_ATTR_TYPE_RANGE, "FP Power T4",  PD_ATTR_FLAG_USER_INVISIBLE, 0,                     0,                    0,  819,  1),
+	PD_MAKE_ATTR (PD_ATTR_ID_FP_PWR_T5,   PD_ATTR_TYPE_RANGE, "FP Power T5",  PD_ATTR_FLAG_USER_INVISIBLE, 400,                 400,                    0,  3000, 1),
+	PD_MAKE_ATTR (PD_ATTR_ID_PANEL_DEPTH, PD_ATTR_TYPE_RANGE, "Panel Depth",  PD_ATTR_FLAG_USER_INVISIBLE, LVDS_DEF_PANEL_DEPTH,  LVDS_DEF_PANEL_DEPTH, 18, 24,   6),
+
+	PD_MAKE_ATTR (PD_ATTR_ID_PWM_INTENSITY,	  PD_ATTR_TYPE_RANGE, "PWM cycle",			  PD_ATTR_FLAG_USER_INVISIBLE,		100,		0,				  0,  100,  0),
+	PD_MAKE_ATTR (PD_ATTR_ID_INVERTER_FREQ,	  PD_ATTR_TYPE_RANGE, "Inverter Frequency",	  PD_ATTR_FLAG_USER_INVISIBLE,		100,		0,				  0,  0,  0),
+	PD_MAKE_ATTR (PD_ATTR_ID_BLM_LEGACY_MODE, PD_ATTR_TYPE_BOOL,  "Backlight Legacy mode",	  PD_ATTR_FLAG_USER_INVISIBLE,		0,		0,					  0,  0,  0),
+
+	/*<-------ID------------>                 <----TYPE--------> <---NAME----->        <------flag--------------->  <---DEF_VAL---->   <--CURR_VALUE-->   <---pad--> */
+	PD_MAKE_ATTR (PD_ATTR_ID_2_CHANNEL_PANEL, PD_ATTR_TYPE_BOOL, "Dual-channel panel", PD_ATTR_FLAG_USER_INVISIBLE, 0,                 0,                 0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_LVDS_PANEL_TYPE, PD_ATTR_TYPE_BOOL, "Panel Type",         PD_ATTR_FLAG_USER_INVISIBLE, 0,                 0,                 0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_PANEL_FIT,       PD_ATTR_TYPE_BOOL, "Panel Upscale",      PD_ATTR_FLAG_USER_INVISIBLE, LVDS_DEF_PANEL_FIT,LVDS_DEF_PANEL_FIT,0, 0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_DITHER,          PD_ATTR_TYPE_BOOL, "Dither",             PD_ATTR_FLAG_USER_INVISIBLE, LVDS_DEF_DITHER,   LVDS_DEF_DITHER,   0, 0, 0),
+	PD_MAKE_ATTR (LVDS_ATTR_ID_TC_LVDS_CLK,   PD_ATTR_TYPE_BOOL, "TC LVDS CLK 110MHz", PD_ATTR_FLAG_USER_INVISIBLE, 0,                 0,                 0, 0, 0),
+
+	/* Start of chipset specific attributes */
+	/* Maintain aspect ratio */
+	PD_MAKE_ATTR (PD_ATTR_LIST_END,       0,                   "",            0,                           0,                0,               0,     0, 0),
+	/* Text tuning */
+	PD_MAKE_ATTR (PD_ATTR_LIST_END,       0,                   "",            0,                           0,                0,               0,     0, 0),
+
+	/* Attribute list end */
+	PD_MAKE_ATTR (PD_ATTR_LIST_END,       0,                   "",            0,                           0,                0,               0,     0, 0)
+};
+
+/* Rightnow it is -3 to reach starting of chipset specific attributes */
+static unsigned short chipset_attr_index = (unsigned short)
+	sizeof(lvds_attrs)/sizeof(pd_attr_t) - 3;
+
+static pd_attr_t lvds_965gm_attrs[] = {
+	PD_MAKE_ATTR (PD_ATTR_ID_MAINTAIN_ASPECT_RATIO,PD_ATTR_TYPE_BOOL,      "Maintain Aspect Ratio",0,             0,               0,               0,     0, 0),
+	PD_MAKE_ATTR (PD_ATTR_ID_TEXT_TUNING,          PD_ATTR_TYPE_RANGE,     "Text Enhancement",     0,             0,               0,               0,     2, 1),
+};
+
+static void lvds_write_reg(lvds_context_t *pd_context, unsigned long reg,
+		unsigned long value, unsigned long change_bits, unsigned long reg_type);
+
+static unsigned long lvds_read_reg(lvds_context_t *pd_context,
+		unsigned long reg, unsigned long reg_type);
+static void lvds_panel_fit(lvds_context_t *pd_context);
+
+static void lvds_get_dclk(lvds_context_t *pd_context, pd_dvo_info_t *lvds_info);
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: PD_MODULE_INIT(lvds_init)
+ *
+ * Description:
+ *    This is the entry function into LVDS port driver when
+ *    it first loads. This will call pd_register() to register
+ *    with Display driver.  Only the driver object is initialized in this
+ *    function, similar to DrverEntry() in a WDM driver.
+ *
+ * Parameters:
+ *    [OUT] *handle:  Not used.  Place holder for supporting dynamic pd
+ *
+ * Return:
+ *    PD_SUCCESS(0)  success
+ *    PD_ERR_XXXXXX  otherwise
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int PD_MODULE_INIT(lvds_init, (void *handle))
+{
+	/* register the LVDS driver */
+	return pd_register(handle, &lvds_driver);
+}
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: PD_MODULE_EXIT(lvds_exit, (void))
+ *
+ * Description:
+ *    This function cleans up resources used by the LVDS driver
+ *
+ * Parameters:
+ *    None
+ *
+ * Return:
+ *    PD_SUCCESS(0):  always returns this
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int PD_MODULE_EXIT(lvds_exit, (void))
+{
+	return PD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_validate
+ *
+ * Description:
+ *    Place holder for a future function
+ *
+ * Parameters:
+ *    TBD
+ *
+ * Return:
+ *    TBD
+ *
+ *----------------------------------------------------------------------------
+ */
+
+unsigned long lvds_validate (unsigned long cookie)
+{                                                          /* lvds_validate */
+	/* Validate magic cookie */
+	/* TODO: Implement the magic cookie algorithm */
+	return cookie;
+}                                                          /* lvds_validate */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_open
+ *
+ * Description:
+ *    This function creates an LVDS context and intialize it with LVDS
+ *    attributes.
+ *
+ *    Internal LVDS port is available only on MGM platform, this port driver
+ *    reads the GMCH device ID with pd_read_regs(PD_REG_PCI) to verify that
+ *    it is supported on the current platform.
+ *
+ * Parameters:
+ *    [IN] callback:    callback context
+ *    [INOUT] context:  Device context.  This function will set the attributes
+ *                      for this context, provided it is already allocated,
+ *                      i.e. not NULL.
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:  if either of the parameters is NULL
+ *    PD_ERR_NODEV:     if an LVDS device is already up and running
+ *    PD_ERR_NOMEM:     if a memory allocation failed
+ *    PD_SUCCESS:       if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+
+/* lvds context structure being declared and initialized */
+
+static lvds_context_t lvds_context = {  /* lvds context structure */
+	0,                   /* fp_width     */
+	0,                   /* fp_height    */
+	0,                   /* dual_channel: Default single channel */
+	0,                   /* panel_type : 0-SPWG 1-OpenLDI*/
+	LVDS_DEF_PANEL_FIT,  /* panel_fit    */
+	LVDS_DEF_PANEL_DEPTH,/* panel_depth  */
+	0xFFFF,              /* dither       */
+	0,                   /* Main aspect ratio, default no */
+	0,                   /* panel filter: Default fuzzy filtering */
+	100,                 /* PWM Intensity */
+	0xFFFF,              /* Inverter Frequency*/
+	0,                   /* BLM Legacy Mode */
+
+	PD_POWER_MODE_D0,    /* power_state   */
+	0,                   /* chipset       */
+	0,                   /* init_done     */
+	0,                   /* num_attrs */
+	0,                   /* PIPE flags    */
+	0,                   /* Graphics Frequency */
+	0,                   /* is gn4 based LVDS controller? */
+	0,                   /* is pwm_done? */
+	0,                   /* is tc_110MHz_clk? i.e., TC max LVDS to 110MHz*/
+
+	NULL,                /* ptr to callback       */
+	NULL,                /* ptr to timing table   */
+	lvds_attrs,          /* ptr to attribute list */
+	NULL,                /* ptr to native timing  */
+	NULL,                /* current mode */
+};
+
+int lvds_open(pd_callback_t *callback, void **context)
+{                                                              /* lvds_open */
+	lvds_context_t *pd_context = (lvds_context_t*) &(lvds_context); /* static Global */
+	pd_reg_t reg_list[2];
+	int ret, i, valid_chipset = 0;
+	unsigned short chipset;
+
+	PD_TRACE_ENTER;
+
+
+
+	/* make sure parameters are valid */
+	if (!callback || !context) {
+		PD_ERROR("invalid parameter");
+		return (PD_ERR_NULL_PTR);
+	}
+	/* GMCH cannot support more than one device */
+	if (lvds_driver.num_devices > 0) {
+		return (PD_ERR_NODEV);
+	}
+	/* Verify that this is an GMCH with Internal LVDS available */
+	reg_list[0].reg = 2;
+	reg_list[1].reg = PD_REG_LIST_END;
+	ret = callback->read_regs(callback->callback_context, reg_list, PD_REG_PCI);
+	if(ret != PD_SUCCESS) {
+		return ret;
+	}
+	chipset = (unsigned short)(reg_list[0].value & 0xffff);
+	for (i = 0; i < ARRAY_SIZE(supported_chipset); i++) {
+		if (chipset == supported_chipset[i]){
+			valid_chipset = 1;
+			break;
+		}
+	}
+	if(!valid_chipset){
+		return PD_ERR_NODEV;
+	}
+
+	/* Special handling for gn4 and beyond chipsets */
+	if (chipset == PCI_DEVICE_ID_VGA_GM965 ||
+		chipset == PCI_DEVICE_ID_VGA_GME965 ||
+		chipset == PCI_DEVICE_ID_VGA_CTG ||
+		chipset == PCI_DEVICE_ID_VGA_TNC ||
+		chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+		chipset == PCI_DEVICE_ID_VGA_LNC) {
+		lvds_context.gn4_plus = 1;
+	}
+
+	/* Initialize number of attributes */
+	/* +1 is to include the end attribute */
+	lvds_context.num_attrs = (unsigned char)chipset_attr_index + 1;
+
+	/* Add chipset specific attrbutes.
+	 * This can be expanded into a switch statement in future if required. */
+	if (lvds_context.gn4_plus) {
+		lvds_context.num_attrs += sizeof(lvds_965gm_attrs)/sizeof(pd_attr_t);
+		for (i=0; i < sizeof(lvds_965gm_attrs)/sizeof(pd_attr_t); i++) {
+			lvds_attrs[chipset_attr_index+i] = lvds_965gm_attrs[i];
+		}
+	}
+	/* Make this a compile time so that size of vBIOS doesn't become > 64KB */
+#if defined(CONFIG_PLB) || defined(CONFIG_TNC)
+	/* pwm backlight control needs graphics frequency. we currently implement
+	 * pwm backlight control for pouslbo only to limit the vbios lvds size. Each
+	 * chipset have a different method of getting this value and chipset has a
+	 * different multiplier. */
+	if (chipset == PCI_DEVICE_ID_VGA_PLB ||
+	    chipset == PCI_DEVICE_ID_VGA_TNC ||
+	    chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+	    chipset == PCI_DEVICE_ID_VGA_LNC) {
+
+		/* For plb/tnc, graphics frequency is obtained by sending an opcode to
+		 * port 5 in the SCH Message Network. We call the read_regs with
+		 * PD_REG_BRIDGE_OPCODE specifically for this purpose only.
+		 *
+		 * The input for this read_reg is the opcode that register data that
+		 * we send into the Message control register*/
+		reg_list[1].reg = PD_REG_LIST_END;
+		ret = callback->read_regs(callback->callback_context,
+			reg_list, PD_REG_BRIDGE_OPCODE);
+		if(ret != PD_SUCCESS) {
+			return ret;
+		}
+
+		/*set the graphics frequency*/
+		pd_context->gfx_freq = (unsigned short) reg_list[0].value;
+	}
+#endif
+
+	pd_context->callback = callback;           /* Save callback context */
+	pd_context->chipset = chipset;            /* save the chipset ID   */
+	*context = (void *) pd_context;
+
+	PD_TRACE_EXIT;
+	return PD_SUCCESS;
+}                                                              /* lvds_open */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_init_device
+ *
+ * Description:
+ *    Initializes the LVDS device, using the device context from the parameter
+ *
+ * Parameters:
+ *    [INOUT] context:  device context
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:  if parameter is invalid
+ *    PD_SUCCESS:       if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int lvds_init_device (void *context)
+{                                                       /* lvds_init_device */
+	if (!context) {
+		return (PD_ERR_NULL_PTR);
+	}
+
+	/* Don't need to do much to initialize this device */
+	((lvds_context_t *)context)->init_done = 1;
+	lvds_driver.num_devices++;
+
+	return (PD_SUCCESS);
+}                                                       /* lvds_init_device */
+
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_close
+ *
+ * Description:
+ *    Releases resources allocated during lvds_open().  This function
+ *    essentially frees a device object.
+ *
+ * Parameters:
+ *    [INOUT] device_context:  device to be freed
+ *
+ * Return:
+ *    PD_SUCCESS: always returns this
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int lvds_close(void *device_context)
+{
+#ifndef CONFIG_MICRO
+
+	lvds_context_t *pd_context = (lvds_context_t *)device_context;
+
+	/* lvds_close */
+
+	PD_TRACE_ENTER;
+
+	/* Deallocate memory occupied by this device */
+	if (device_context) {
+		if ( NULL != pd_context->timing_table) {
+			pd_free(pd_context->timing_table);
+			pd_context->timing_table = NULL;
+		}
+
+		/* Free attribute list, if necessary */
+		/* FIXME -- The following test will never call pd_free(), because the
+		 * expression "!lvds_driver.num_devices" will yield 0 or 1, and that
+		 * will never be greater than 1.  This is a potentially small memory
+		 * leak, unless some other code frees it.
+		 */
+		if (!lvds_driver.num_devices > 1) {
+			pd_free(pd_context->attr_list);
+		}
+		/* This allocated statically no need to free it */
+		/* pd_free(device_context); */
+		lvds_driver.num_devices--;
+		pd_context->init_done = 0;
+	}
+
+	PD_TRACE_EXIT;
+#endif
+	return (PD_SUCCESS);
+}                                                             /* lvds_close */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_set_mode
+ *
+ * Description:
+ *    Sets LVDS to a new mode, specified by "mode".
+ *
+ * Parameters:
+ *    [INOUT] context:  device context
+ *    [IN]    mode:     information about the mode to switch to
+ *    [IN]    flags:    can contain the following value
+ *                PD_SET_MODE_FLAG_TEST:  only testing to see if mode is supported
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:     if invalid parameter detected
+ *    PD_ERR_MODE_NOTSUPP: if mode specified is not supported
+ *    PD_SUCCESS:          if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+int lvds_set_mode(void *context, pd_timing_t *mode, unsigned long flags)
+{
+	lvds_context_t *pd_context  = NULL;
+
+	PD_DEBUG("lvds_set_mode)\n");
+	PD_TRACE_ENTER;
+
+	pd_context = (lvds_context_t *)context;
+
+	/* Make sure these parameters are valid */
+	if (!pd_context || !mode) {
+		return (PD_ERR_NULL_PTR);
+	}
+	PD_DEBUG("lvds_set_mode: %ux%u", mode->width, mode->height);
+
+	/* Make sure specified mode is supported */
+	if ((pd_context->fp_width && (mode->width > pd_context->fp_width)) ||
+		(pd_context->fp_height && (mode->height > pd_context->fp_height))) {
+		return PD_ERR_MODE_NOTSUPP;
+	}
+
+	/* Do nothing if we are only want to know if a mode is supported */
+	if (flags & PD_SET_MODE_FLAG_TEST) {
+		return PD_SUCCESS;
+	}
+
+	pd_context->current_mode = mode;
+	pd_context->pipe = flags;
+	/* Enable panel fitting and return */
+	lvds_panel_fit(pd_context);
+
+	PD_TRACE_EXIT;
+
+	return PD_SUCCESS;
+}
+
+int lvds_post_set_mode(void *context, pd_timing_t *mode, unsigned long flags)
+{                                                          /* lvds_set_mode */
+	lvds_context_t *pd_context  = NULL;
+	unsigned long  port_control = 0;
+	unsigned long  preserve     = 0;
+	int            ret          = 0;
+
+	PD_TRACE_ENTER;
+	pd_context = (lvds_context_t *)context;
+
+	PD_DEBUG("lvds_post_set_mode)\n");
+	/* Make sure these parameters are valide */
+	if (!pd_context || !mode) {
+		return (PD_ERR_NULL_PTR);
+	}
+
+	/* Before enabling the LVDS port, make sure that display PLL for this pipe
+	 * is enabled and the port is power sequenced on using the panel power
+	 * sequencing logic. */
+
+	preserve = 0x3E007803;
+	port_control = preserve & lvds_read_reg(pd_context, 0x61180, PD_REG_MIO);
+	port_control |= BIT(31);          /* enable LVDS port */
+	if (flags & PD_SET_MODE_PIPE_B) {
+		port_control |= BIT(30);
+	}
+	port_control |= (BIT(9)|BIT(8));  /* power up */
+	port_control &= ~(BIT(21)|BIT(20)); /* Default sync polarites active high */
+
+	/* For gn4+, dither moved to port_control from panel_fit reg */
+	if (pd_context->gn4_plus) {
+		if (pd_context->panel_depth == 18) {
+			port_control |= BIT(25);
+		}
+
+		if (pd_context->dither != 0xFFFF) {
+			if (pd_context->dither) {
+				port_control |= BIT(25);
+			} else {
+				port_control &= ~BIT(25);
+			}
+		}
+	}
+
+	if((!pd_context->panel_fit) &&
+	   ( mode->width < pd_context->fp_width ||
+	     mode->height < pd_context->fp_height ) ){
+		port_control |= BIT(15); /* enable border in active for centering */
+	}
+
+	/*
+	 * Bit 24 for OpenLDI should be set to a 0 (1x18.0, 2x18.0, 1x24.0, 2x24.0).
+	 * From (B-Spec, Ref# 22316, section 1.15.3.8.3)
+	 *
+	 * Bit 24 for SPWG should be set to a 1 (1x24.1 or 2x24.1).
+	 *
+	 * This was verified by comparing the timing diagram for 1x24.0 in
+	 * OpenLDI spec (5.4.2.2 24-bit Single Pixel Mode, Unbalanced) with the
+	 * same diagram in the Display BSpec for Napa or Gen4.
+	 * A0 � A3 signals match the 1x24.0.
+	 *
+	 * From the OpenLDI spec (bit mappings are different):
+	 *
+	 * Table 5-2, Bit Number Mappings
+	 * 18 bpp bit# 24 bpp bit# OpenLDI bit#
+	 * 5           7           5
+	 * 4           6           4
+	 * 3           5           3
+	 * 2           4           2
+	 * 1           3           1
+	 * 0           2           0
+	 *             1           7
+	 *             0           6
+	 */
+
+	/* Attribute panel_type description:
+	 *  Attr ID    Attr Value        IntLVDS dataformat
+	 *  =======    ===============   ==================
+	 *    49       0  (SPWG)           1 (value of Bit 24)
+	 *    49       1  (OpenLDI)        0 (value of Bit 24)
+	 */
+	if (pd_context->panel_type == 0) {
+		port_control |= BIT(24); /* Dataformat 0 = SPWG, 1 = OpenLDI */
+	}
+
+	/* If the dual-channel is required, then power up second channel
+	 * ClkB and B0, B1, B2, (B3) */
+	if (pd_context->dual_channel) {
+		port_control |= (BIT(5)|BIT(4));  /* ClkB */
+		port_control |= (BIT(3)|BIT(2));  /* B0, B1, B2, (B3) */
+	}
+
+	/* Check for 18 or 24 bit panel */
+	if (pd_context->panel_depth == 24) {
+		/* If the panel is 24-bit (8-bpp), enable A3, (B3) pair. */
+		port_control |= (BIT(7)|BIT(6));
+	}
+
+	/* Set the sync polarities correctly if there is a native dtd */
+	if (pd_context->native_dtd) {
+		/* Set bit 20 for hsync active low */
+		if ((pd_context->native_dtd->mode_info_flags & PD_HSYNC_HIGH) == 0) {
+			port_control |= BIT(20);
+		}
+		/* Set bit 21 for vsync active low */
+		if ((pd_context->native_dtd->mode_info_flags & PD_VSYNC_HIGH) == 0) {
+			port_control |= BIT(21);
+		}
+	}
+
+    lvds_write_reg(pd_context, 0x61204, 0xABCD0000, 0xFFFFFFFF, PD_REG_MIO);
+
+	lvds_write_reg(pd_context, 0x61180, port_control, 0xFFFFFFFF, PD_REG_MIO);
+	ret = lvds_set_power(pd_context, PD_POWER_MODE_D0);
+	if (ret) {
+		PD_ERROR("PD set_power (D0) returned: 0x%x", ret);
+		return ret;
+	}
+
+    lvds_write_reg(pd_context, 0x61204, BIT(1), BIT(1), PD_REG_MIO);
+
+
+	PD_TRACE_EXIT;
+	/* Set the mode as per given timings */
+	return PD_SUCCESS;
+}                                                          /* lvds_set_mode */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_set_attrs
+ *
+ * Description:
+ *    Incorporate attributes in "list" into the device context.  This function
+ *    will override the initial attributes set by init_attrs.
+ *
+ * Parameters:
+ *    [INOUT] context:  device context
+ *    [IN] num:         not used, but must not be 0.
+ *    [IN] list:        list of attributes to incorporate into device context
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:          if one of the parameters is invalid
+ *    PD_ERR_ATTR_CANT_CHANGE:  attributes cannot be modified
+ *    PD_SUCCESS: if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+/* Tables required by Optimization Code. lvds_set_attrs() */
+typedef struct _opt_table_data {
+	unsigned long id;
+	unsigned short block;
+	unsigned short offset;
+} lvds_opt_table_data_t;
+static lvds_opt_table_data_t table_opt_data1[] = {
+
+	/*<--- id ---------->  <---block---> <------ offset -------------------> */
+	{PD_ATTR_ID_PANEL_DEPTH,     1, PD_OFFSETOF(lvds_context_t, panel_depth) },
+
+	{PD_ATTR_ID_2_CHANNEL_PANEL, 1, PD_OFFSETOF(lvds_context_t, dual_channel)},
+	{PD_ATTR_ID_LVDS_PANEL_TYPE, 1, PD_OFFSETOF(lvds_context_t, panel_type) },
+	{PD_ATTR_ID_PANEL_FIT,       1, PD_OFFSETOF(lvds_context_t, panel_fit)   },
+	{PD_ATTR_ID_DITHER,          1, PD_OFFSETOF(lvds_context_t, dither)      },
+	{PD_ATTR_ID_MAINTAIN_ASPECT_RATIO,1,PD_OFFSETOF(lvds_context_t,aspect_ratio)},
+	{PD_ATTR_ID_TEXT_TUNING,     1, PD_OFFSETOF(lvds_context_t, text_tune)},
+	{PD_ATTR_ID_PWM_INTENSITY,   1, PD_OFFSETOF(lvds_context_t, pwm_intensity)},
+	{PD_ATTR_ID_INVERTER_FREQ,   1, PD_OFFSETOF(lvds_context_t, inverter_freq)},
+	{PD_ATTR_ID_BLM_LEGACY_MODE, 1, PD_OFFSETOF(lvds_context_t, blm_legacy_mode)},
+	{LVDS_ATTR_ID_TC_LVDS_CLK,   1, PD_OFFSETOF(lvds_context_t, tc_110MHz_clk)},
+
+	/*<--- id ---------->  <---block---> <------ offset -------------------> */
+	{PD_ATTR_ID_FP_PWR_T1,       2,     0 }, /* 6 */
+	{PD_ATTR_ID_FP_PWR_T2,       2,     0 }, /* 7 */
+	{PD_ATTR_ID_FP_PWR_T3,       2,     0 }, /* 8 */
+	{PD_ATTR_ID_FP_PWR_T4,       2,     0 }, /* 9 */
+	{PD_ATTR_ID_FP_PWR_T5,       2,     0 }  /* 10 */
+};
+/* End of Tables required by Optimization Code. lvds_set_attrs() */
+
+int lvds_set_attrs (void *context, unsigned long num, pd_attr_t *list)
+{
+	lvds_context_t *pd_context = (lvds_context_t *) context;
+	pd_attr_t      *attr       = NULL;
+	unsigned short i           = 0;
+	unsigned short j           = 0;
+	int            ret         = PD_SUCCESS;
+	/* no of case IDs in the global table */
+	int num_case_ids = sizeof(table_opt_data1)/sizeof(lvds_opt_table_data_t);
+
+	/* basic parameter check */
+	if (!context || !num || !list) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	PD_DEBUG("lvds_set_attrs()\n");
+	for (i = 0; i < num; i++, list++) {
+		/* do nothing if the attribute has not been changed */
+		if (!(list->flags & PD_ATTR_FLAG_VALUE_CHANGED)) {
+			continue;
+		}
+
+		/* attributes can't be changed after init has been completed */
+		if (list->flags & PD_ATTR_FLAG_USER_INVISIBLE &&
+			pd_context->init_done) {
+			return PD_ERR_ATTR_CANT_CHANGE;
+		}
+
+		/* Set the internal attributes' list.  Note that although get_attr() can
+		 * return NULL theortically, it will not do so here because all the
+		 * attribute IDs in this switch statement comes from lvds_attrs[],
+		 * a list that is automatically initialized into pd_context.  This is
+		 * why we are not checking for NULL after calling get_attr().
+		 */
+#if 0	/* ORIGINAL SWITCH STATEMENT */
+		switch (list->id) {
+			case PD_ATTR_ID_FP_PWR_T1:
+			case PD_ATTR_ID_FP_PWR_T2:
+			case PD_ATTR_ID_FP_PWR_T3:
+			case PD_ATTR_ID_FP_PWR_T4:
+			case PD_ATTR_ID_FP_PWR_T5:
+				/* current_value should not exceed the predefined max value */
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = LVDS_MIN(
+						((pd_range_attr_t *)list)->current_value,
+						attr->_pad1);
+				break;
+
+			case PD_ATTR_ID_PANEL_DEPTH:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = list->current_value;
+				pd_context->panel_depth = (unsigned char) attr->current_value;
+				break;
+
+			case PD_ATTR_ID_2_CHANNEL_PANEL:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = ((pd_bool_attr_t *)list)->current_value;
+				pd_context->dual_channel = (attr->current_value?1:0);
+				break;
+
+			case PD_ATTR_ID_LVDS_PANEL_TYPE:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = ((pd_bool_attr_t *)list)->current_value;
+				pd_context->panel_type = (attr->current_value?1:0);
+				break;
+
+			case PD_ATTR_ID_PANEL_FIT:
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				attr->current_value = ((pd_bool_attr_t *)list)->current_value;
+				pd_context->panel_fit = (attr->current_value?1:0);
+				break;
+
+			default:
+				/* do nothing if we have an unknown ID */
+				break;
+		}
+#endif
+		/* OPTIMIZATION CODE BEGINS FOR THE ABOVE SWITCH
+		 *----------------------------------------------
+		 * Step 1: First identify the code common to all case blocks.
+		 *         This we call it "common code block" Since this is to be
+		 *         executed by all the case blocks.
+		 *
+		 * Step 2: Group the cases into blocks based on how we can combine them
+		 *
+		 *         Eg: case 0: ptr->x = 1; break; // similar code
+		 *        case 1: ptr->y = 1; break; // similar code
+		 *  ------Combined block ----
+		 *        case 0:
+		 *        case 1: PTR_OFFSET_TYPE(ptr, table[i].offset) = 1; break;
+		 *        This is an important step because we save code space by
+		 *        mapping many cases to smaller number of blocks. In the above
+		 *        we use index to get the right offset of the ptr.
+		 *
+		 * Step 3: Assign block IDs to each block. Put the case ID, block ID
+		 *         and other information such as offsets of ptr in a global
+		 *         table.
+		 *
+		 * Step 4: During run-time , search through the global table, find the
+		 *         matching case ID, and execute the common code. Next get the
+		 *         block ID and execute the block specific code. For the index,
+		 *         retrieve it from the table for the corresponding case ID.
+		 *
+		 * Step 5: If no matching case ID is found, error it out.
+		 *
+		 * Let's look at the optimization code for the above switch.
+		 * The code below searches for the list->id in a global table where
+		 * we store all the case values along with block numbers and other
+		 * information. After we find a valid id in the table , we execute the
+		 * common code for all cases first and then we retrieve the block id.
+		 * The block id is necesary to determine which block does the id belong
+		 * to. This is used to execute block specific code. Similar to switch
+		 * cases ONLY here we try to minimize the no of blocks.
+		 *
+		 * In this example , we store the offsets of field names of a ptr in the
+		 * global table.This is necessary to combine cases with "similar" but
+		 * not "same" codes.
+		 *
+		 * Eg: case 0: ptr->x = 1; break; // similar code
+		 *     case 1: ptr->y = 1; break; // similar code
+		 *  -- Combined block ----
+		 *    case 0:
+		 *    case 1: PTR_OFFSET_TYPE(ptr, table[i].offset) = 1; break;
+		 *
+		 * By reducing the number of blocks we save on Code space. After we
+		 * finish our work , we exit the for loop and check for invalid ID
+		 * passed by the upper layer. This is akin to default in the
+		 * switch block
+		 *
+		 * CAUTION: If there is any change in the switch above, this code
+		 * along with the tables have to be re-written and changed according
+		 * to the new behaviour of the switch. Examples include adding a new
+		 * case in the switch. The reason for all this mumbo-jumbo is to
+		 * reduce code size in VBIOS, where we are running out of code space.
+		 */
+		for(j = 0; j < num_case_ids; j++) {
+			/* Search for the attribute ID in the global table */
+			if(list->id == table_opt_data1[j].id) {
+				/* Run the common code for all the blocks */
+				attr = pd_get_attr(pd_context->attr_list, pd_context->num_attrs,
+					list->id, 0);
+				/* Once we get a valid ID, need to find out which block it
+				 *  belongs so we can execute block specific code.
+				 */
+				if(table_opt_data1[j].block == 1) { /* block 1 */
+					/* Got the block. Need the offset to the struct for that ID
+					* so we can store value at corresponding offset to get the
+					* desired behaviour for that ID.This is the code that makes
+					* the whole optimization work because we are combining the
+					* case IDs into a single block which saves code.
+					*/
+					attr->current_value = list->current_value;
+					PTR_OFFSET_USHORT(pd_context, table_opt_data1[j].offset) =
+					(unsigned short) attr->current_value;
+
+				} else { /* block 2. We only have two blocks. */
+					attr->current_value = LVDS_MIN(
+						list->current_value, ((pd_range_attr_t *)attr)->max);
+				}
+				break; /* We found a valid ID, so break inner for loop */
+			}
+		}
+	}
+	/* panel_type 0 (SPWG) isn't available for 18-bit depth */
+	PD_DEBUG("in LVDS_set_attributes()\n");
+	if (pd_context->panel_depth == 18) {
+		pd_context->panel_type = 1;
+	}
+	PD_DEBUG("IntLVDS: dual_channel=%u", pd_context->dual_channel);
+	PD_DEBUG("IntLVDS: panel_type=%u panel_fit=%u panel_dep=%u dither=%u",
+		pd_context->panel_type, pd_context->panel_fit,
+		pd_context->panel_depth, pd_context->dither);
+	PD_DEBUG("IntLVDS: keep_aspect_ratio=%u text_tune=%lu",
+		pd_context->aspect_ratio, pd_context->text_tune);
+	PD_DEBUG("IntLVDS: PWM Intensity=%u Inverter Freq=%u, BLM legacy mode =%u",
+		pd_context->pwm_intensity, pd_context->inverter_freq,
+		pd_context->blm_legacy_mode);
+	PD_DEBUG("IntLVDS: tc_110MHz_clk = %u", pd_context->tc_110MHz_clk);
+
+	if (pd_context->init_done) {
+		/* When emgd_driver_pre_init() pokes new attrs into this port driver,
+		 * pd_context->current_mode must be set before calling
+		 * lvds_panel_fit(), so set it to the first entry in the timing table:
+		 */
+		if (pd_context->current_mode == NULL) {
+			pd_context->current_mode = pd_context->timing_table;
+		}
+		lvds_panel_fit(pd_context);
+	}
+
+	return ret;
+} /* lvds_set_attrs */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_get_attrs
+ *
+ * Description:
+ *    Extracts the attribute list and the number of elements in the list
+ *    from the device context.
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] num:    number of elements in list
+ *    [OUT] list:   list of attributes from the device context
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR: if one of the parameters is invalid
+ *    PD_SUCCESS:      if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+int lvds_get_attrs (void *context, unsigned long *num, pd_attr_t **list)
+{                                                         /* lvds_get_attrs */
+	/* basic parameter check */
+	if (!context || !num || !list) {
+		return PD_ERR_NULL_PTR;
+	}
+
+	PD_DEBUG("lvds_get_attrs()\n");
+	/* Nothing fancy, just extracting the elements from the list */
+	*list = ((lvds_context_t *)context)->attr_list;
+	*num  = ((lvds_context_t *)context)->num_attrs;
+
+	return PD_SUCCESS;
+}                                                         /* lvds_get_attrs */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_get_timing_list
+ *
+ * Description:
+ *
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] in_list:
+ *    [OUT] list:
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR: if one of the parameters is invalid
+ *    PD_ERR_NOMEM:    if internal memory allocate failed
+ *    PD_SUCCESS:      if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+int lvds_get_timing_list (void *context, pd_timing_t *in_list,
+	pd_timing_t **list)
+{                                                   /* lvds_get_timing_list */
+	lvds_context_t *pd_context = (lvds_context_t *)context;
+	pd_dvo_info_t lvds_info = {0, 0, 1, 0, 0, 0, 0, 0};
+	pd_display_info_t lvds_display_info = {0, 0, 0, 0, NULL};
+	int ret;
+
+	PD_DEBUG("lvds_get_timing_list()\n");
+	lvds_get_dclk( pd_context, &lvds_info );
+
+	PD_DEBUG("chipset = 0x%x", pd_context->chipset);
+	lvds_display_info.panel_fit = (unsigned char) pd_context->panel_fit;
+	ret = pd_filter_timings(pd_context->callback->callback_context,
+		in_list, &pd_context->timing_table, &lvds_info, &lvds_display_info);
+
+	/* Helper function will return the below values */
+	pd_context->native_dtd = lvds_display_info.native_dtd;
+	pd_context->fp_width = lvds_display_info.width;
+	pd_context->fp_height = lvds_display_info.height;
+
+	*list = pd_context->timing_table;
+	return ret;
+}                                                   /* lvds_get_timing_list */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_set_power
+ *
+ * Description:
+ *  Sets LVDS to the specified power state
+ *
+ *  Conversion between IEGD timer values to LVDS port timer values
+ *
+ *  SEPG(?) IEGD    LVDS Port    Program bits     min     max
+ *  ----    ------- ------------ ---------------- ------ ---------
+ *  T1+T2   T1 ms   T1+T2 100us  0x61208 [28:16]  0 ms   819.2 ms
+ *  T5      T2 ms   T5    100us  0x61208 [12:00]  0 ms   819.2 ms
+ *  T6      T3 ms   Tx    100us  0x6120C [12:00]  0 ms   819.2 ms
+ *  T3      T4 ms   T3    100us  0x6120C [28:16]  0 ms   819.2 ms
+ *  T4      T5 ms   T4    100ms  0x61210 [04:00]  0 ms   3200  ms
+ *
+ *  Reg     = Value
+ *  ------    -------
+ *  0x61208 = [T1 T2]
+ *  0x6120C = [T4 T3]
+ *  0x61210 = [   T5]
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] in_list:
+ *    [OUT] list:
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:      if context is NULL
+ *    PD_ERR_INVALID_POWER: if "state" is invalid
+ *    PD_SUCCESS:           if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+/* Tables required by the optimization codes in lvds_set_power() */
+typedef struct {
+	unsigned char id1;
+	unsigned char id2;
+	unsigned char bit;
+	unsigned long reg;
+} opt_set_power_t;
+
+static opt_set_power_t table_set_power[] = {
+	/* id1                  id2                   bit    reg */
+	{ PD_ATTR_ID_FP_PWR_T1, PD_ATTR_ID_FP_PWR_T2, 1,     0x61208 },  /* D0 */
+	{ PD_ATTR_ID_FP_PWR_T4, PD_ATTR_ID_FP_PWR_T3, 0,     0x6120C },  /* Dx */
+};
+
+int lvds_set_power(void *context, unsigned long state)
+{                                                         /* lvds_set_power */
+	unsigned long  i           = 0;
+	lvds_context_t *pd_context = (lvds_context_t *)context;
+	pd_attr_t      *tattr      = NULL;  /* holds time delay b/ pwr transition */
+	unsigned long            delay = 0, delay1;
+
+	PD_DEBUG("state = %lu", state);
+
+	PD_DEBUG("lvds_set_power() to state = %lu\n",state);
+	/* Basic parameter check */
+	if (!context) {
+		PD_DEBUG("No context");
+		return PD_ERR_NULL_PTR;
+	}
+	PD_DEBUG("pd_context=0x%lx", (unsigned long)pd_context);
+
+	/* Check for invalid state */
+	if (state > PD_POWER_MODE_D3) {
+		PD_DEBUG("Invalid power state");
+		return PD_ERR_INVALID_POWER;
+	}
+
+	/* Get the index into above table */
+	if (state == PD_POWER_MODE_D0) {
+		i = 0;
+	} else {
+		i = 1;
+	}
+
+	lvds_write_reg(pd_context, 0x61204, 0xABCD0000, 0xFFFFFFFF, PD_REG_MIO);
+
+	/* Program panel power up/down delays: Either T1/T2 or T3/T4*/
+	tattr = pd_get_attr(pd_context->attr_list,
+		pd_context->num_attrs, table_set_power[i].id1, 0);
+	/* Convert ms to 100us */
+	delay1 = tattr->current_value;
+	delay = (tattr->current_value * 10) << 16;
+	tattr = pd_get_attr(pd_context->attr_list,
+		pd_context->num_attrs, table_set_power[i].id2, 0);
+	delay1 += tattr->current_value;
+	delay |= tattr->current_value * 10;
+
+	lvds_write_reg(pd_context, table_set_power[i].reg, delay, 0x1FFF1FFF, PD_REG_MIO);
+
+	/* Program power cycle delay: convert ms to 100ms */
+	delay = pd_get_attr(pd_context->attr_list,
+		pd_context->num_attrs, PD_ATTR_ID_FP_PWR_T5, 0)->current_value;
+	delay1 += delay;
+	/* TODO: Write reference divider [31:8] */
+	delay = ((delay/100+1) & 0xFF) | ((pd_context->gfx_freq*100/2-1)<<8);
+	lvds_write_reg(pd_context, 0x61210, delay, 0xFFFFFFFF, PD_REG_MIO);
+
+	/* Power state target */
+	lvds_write_reg(pd_context, 0x61204, table_set_power[i].bit, BIT(0), PD_REG_MIO);
+
+	/* Power down on reset available on crestline onwards */
+	lvds_write_reg(pd_context, 0x61204, BIT(1), BIT(1), PD_REG_MIO);
+
+/* Make this a compile time so that size of vBIOS doesn't become > 64KB */
+#if defined(CONFIG_PLB) || defined(CONFIG_TNC)
+	/* PWM is a method of controlling the backlight intensity.
+	 * It is not method to turn on baclkight.
+	 * We still need the PD method to turn on the backlight.
+	 *
+	 * This feature is for Pouslbo Only. We check that the user has set the
+	 * inverter frequency. Default intensity, if not set, is 100%
+	 *
+	 * Due to the high amount of calculation, we want to only set this register
+	 * if it has not been ser previously. The register could be
+	 * "brought forward" from VBIOS.
+	 */
+	if(pd_context->inverter_freq != 0xFFFF &&  /* Overwritten by set_attr */
+		(pd_context->chipset == PCI_DEVICE_ID_VGA_PLB ||
+		 pd_context->chipset == PCI_DEVICE_ID_VGA_TNC ||
+		 pd_context->chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+		 pd_context->chipset == PCI_DEVICE_ID_VGA_LNC) &&
+		 !pd_context->pwm_done) {
+		unsigned long reg_value = 0;
+		unsigned long percentage = 0;
+		unsigned long calculation = 0;
+		unsigned long blc_pwm_ctl2 = 0;
+
+		/* We first need to get the graphics frequency, which will be used to
+		 * calculate Backlight Modulation Frequency[BMF]. BMF will be used to
+		 * fill up the 15 MSB in the 0x61254 register
+		 *
+		 * The calculation for the Modulation Frequency field in the
+		 * BLC_PWM_CTL Register is:
+		 *
+         *     Reference Clock Freq               1
+         *     -----------------------   x    ------------------
+         *            Divider                   PWM Freq in Hz
+		 *
+		 */
+#if 0
+		/* GMA accurate calculation that requires "calculation" to be an
+		 * unsigned long long typedef */
+		calculation = pd_context->gfx_freq * PWM_FREQ_CALC_CONSTANT_2;
+		calculation = calculation / 0x20; /*pouslbo specific divider*/
+		calculation = calculation * PWM_FREQ_CALC_CONSTANT_1;
+		calculation = calculation / pd_context->inverter_freq;
+		calculation = calculation / PWM_FREQ_CALC_CONSTANT_1;
+#endif
+		/* Some system bios cannot take 64 bit data type. Using a more
+		 * simplified calculation that is not too accurate if the inputs
+		 * are not round numbers */
+		calculation = pd_context->inverter_freq * 0x20; /* plb/tnc divider */
+		calculation = (pd_context->gfx_freq * PWM_FREQ_CALC_CONSTANT_2) /
+			calculation;
+
+		/* Writing the register: 15 MSB is the max lvds clock / 32.
+		* Bit 16 can either be legacy or non legacy depending upon Attr #72. */
+		if (pd_context->gn4_plus) {
+			blc_pwm_ctl2 = (1L << 31) |
+				(pd_context->blm_legacy_mode << 30) |
+				((pd_context->pipe == PD_SET_MODE_PIPE_B)?1L:0L << 29);
+			lvds_write_reg(pd_context, 0x61250, blc_pwm_ctl2, 0xFFFFFFFF, PD_REG_MIO);
+			reg_value = (calculation & 0xFFFF)<<16;
+		} else {
+			reg_value = ((calculation & 0xFFFE) |
+				pd_context->blm_legacy_mode)<<16;
+		}
+
+		/* The 16 LSB is a value that the user sets in configuration.
+		 * user sets the value in percentage.
+		 * We convert it into the clock speed */
+		percentage = ( pd_context->pwm_intensity * (unsigned long)calculation);
+		reg_value |= (unsigned long)( percentage / (int)100 ) & 0xFFFE;
+		lvds_write_reg(pd_context, 0x61254, reg_value, 0xFFFFFFFF, PD_REG_MIO);
+
+		/* set the flag so that we only do this function once */
+		pd_context->pwm_done = 1;
+	}
+#endif
+
+	if (state != PD_POWER_MODE_D0) {
+		/* Wait until the current power up/down sequence is complete */
+		i = 0;
+		while(lvds_read_reg(pd_context, 0x61200, PD_REG_MIO) & 0x80000000L) {
+			i++;
+			if(i > 0x100000L) {
+				break;
+			}
+		}
+		lvds_write_reg(pd_context, 0x61180, 0, BIT(31), PD_REG_MIO);
+	}
+
+#ifdef CONFIG_TNC
+#if 0
+
+/*-----------------------------------------------------------------------------
+ * LPC Register Offsets. Used for LVDS BKLT control. Registers are part
+ * Atom E6xx [D31:F0]
+ ----------------------------------------------------------------------------*/
+#define RGEN    0x20
+#define RGIO    0x24
+#define RGLVL   0x28
+#define TNC_LVDS_VDDEN	 BIT(0)
+#define TNC_LVDS_BKLTEN  BIT(1)
+#define TNC_LVDS_BKLTCTL BIT(2)
+
+	if (pd_context->chipset == PCI_DEVICE_ID_VGA_TNC ||
+		pd_context->chipset == PCI_DEVICE_ID_VGA_TNC_A0 ||
+		pd_context->chipset == PCI_DEVICE_ID_VGA_LNC) {
+		unsigned long value;
+
+		/* Enable backlight for LVDS: based on observed si behavior:
+		 * Subject to change based on si DE feedback */
+		if (state == PD_POWER_MODE_D0) {
+			value = TNC_LVDS_BKLTCTL|TNC_LVDS_BKLTEN|TNC_LVDS_BKLTCTL;
+		} else {
+			value = 0;
+		}
+		lvds_write_reg(pd_context, RGEN, value,
+			TNC_LVDS_BKLTCTL|TNC_LVDS_BKLTEN|TNC_LVDS_BKLTCTL, PD_REG_LPC);
+		lvds_write_reg(pd_context, RGIO, value,
+			TNC_LVDS_BKLTCTL|TNC_LVDS_BKLTEN|TNC_LVDS_BKLTCTL, PD_REG_LPC);
+	}
+#endif
+#endif
+
+	/* update power state */
+	pd_context->power_state = state;
+	return PD_SUCCESS;
+}                                                         /* lvds_set_power */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_get_power
+ *
+ * Description:
+ *   Returns the current LVDS power state back to the caller.
+ *
+ * Parameters:
+ *    [IN] context: device context to extract information from
+ *    [OUT] state:  current power state
+ *
+ * Return:
+ *    PD_ERR_NULL_PTR:      if one of the parameters is invalid
+ *    PD_SUCCESS:           if successful
+ *
+ *----------------------------------------------------------------------------
+ */
+
+int lvds_get_power (void *context, unsigned long *state)
+{                                                         /* lvds_get_power */
+	if ((NULL == context) || (NULL == state)) {
+		return PD_ERR_NULL_PTR;
+	}
+	PD_DEBUG("lvds_get_power()\n");
+	/* The caller should be able to do this himself, but whatever */
+	*state = ((lvds_context_t *) context)->power_state;
+	return PD_SUCCESS;
+}                                                         /* lvds_get_power */
+
+int lvds_save(void *context, void **state, unsigned long flags)
+{
+	*state = NULL;
+	return PD_SUCCESS;
+}
+
+int lvds_restore(void *context, void *state, unsigned long flags)
+{
+	int ret = PD_SUCCESS;
+	return ret;
+}
+
+/*----------------------------------------------------------------------
+ * Function: lvds_get_port_status()
+ *
+ * Description:  It is called to get the information about the display
+ *
+ * Parameters:  context - Port driver's context
+ *				port_status - Returns the display type and connection state
+ *
+ * Return:      PD_SUCCESS(0)  success
+ *              PD_ERR_XXXXXX  otherwise
+ *----------------------------------------------------------------------*/
+int lvds_get_port_status(void *context, pd_port_status_t *port_status)
+{
+	/* Display connection cannot be determined */
+	port_status->display_type = PD_DISPLAY_LVDS_INT;
+	port_status->connected    = PD_DISP_STATUS_UNKNOWN;
+	return PD_SUCCESS;
+}
+
+static unsigned long lvds_read_reg(lvds_context_t *pd_context,unsigned long reg,
+	unsigned long reg_type)
+{
+	pd_reg_t list[2];
+	int ret;
+
+	list[0].reg = reg;
+	list[0].value = 0;
+	list[1].reg = PD_REG_LIST_END;
+	ret = pd_context->callback->read_regs(
+		pd_context->callback->callback_context, list, reg_type);
+	if (ret) {
+		PD_ERROR("LVDS read regs: Failed.");
+	}
+	return list[0].value;
+}
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_write_reg
+ *
+ * Description:
+ *    Writes bits in "value" into a register.  Bits written are dictated by
+ *    the "change_bits" mask.
+ *
+ * Parameters:
+ *    [IN] pd_context:  device context, dispatcher to the actual write_reg
+ *                      function
+ *    [IN] reg:  register to write value to
+ *    [IN] value:  value to change the register to
+ *    [IN] change_bits:  bit mask, the bits set to "1" will be modified by
+ *                       the corresponding bits in "value"
+ *
+ * Return:
+ *    None
+ *
+ *----------------------------------------------------------------------------
+ */
+static void lvds_write_reg(lvds_context_t *pd_context, unsigned long reg,
+		unsigned long value,
+		unsigned long change_bits,
+		unsigned long reg_type)
+{                                                         /* lvds_write_reg */
+	pd_reg_t list[2];
+	int ret;
+
+	PD_DEBUG("ENTER");
+
+	list[0].reg = reg;
+	list[0].value = (lvds_read_reg(pd_context, reg, PD_REG_MIO) & ~change_bits) | value;
+	list[1].reg = PD_REG_LIST_END;
+	ret = pd_context->callback->write_regs(
+		pd_context->callback->callback_context, list, reg_type);
+	if (ret) {
+		PD_ERROR("LVDS write regs: Failed.");
+	}
+	PD_DEBUG("EXIT");
+	return;
+}                                                         /* lvds_write_reg */
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_panel_fit
+ *
+ * Description:
+ *    Enables panel fitting
+ *
+ * Parameters:
+ *    [IN] pd_context:  device context
+ *
+ * Return:
+ *    None
+ *
+ *----------------------------------------------------------------------------
+ */
+static void lvds_panel_fit(lvds_context_t *pd_context)
+{
+	/* enable auto vertical ratio */
+	/* enable auto horizantal ratio */
+	/* no dither */
+	unsigned long panel_fit_reg = 0x00000220;
+	PD_DEBUG("lvds_panel_fit() \n");
+
+	PD_TRACE_ENTER;
+
+	if (pd_context->current_mode->width != pd_context->fp_width ||
+		pd_context->current_mode->height != pd_context->fp_height) {
+		/* Enable panel fitting */
+		/* vertical interpolation = bilinear */
+		/* horizontal interpolation = bilinear */
+		panel_fit_reg |= 0x80000440;
+	}
+	/* Enable dither based on default/user-set value:
+	 *   By default
+	 *        dither = 1 for 18-bit panels
+	 *               = 0 for 24-bit panels.
+	 *   But this behavior can be changed by setting the DITHER attribute.
+	 *        When user sets the attribute, dither will be updated
+	 *        as part of attribute processing in set attributes. */
+	/* For gn4 based chipsets dither is controlled in port_control register */
+	if (!pd_context->gn4_plus) {
+		/* Default behavior */
+		if (pd_context->panel_depth == 18) {
+			panel_fit_reg |= BIT(3);
+		}
+
+		/* Overwritten by set attribute */
+		if (pd_context->dither != 0xFFFF) {
+			if (pd_context->dither) {
+				panel_fit_reg |= BIT(3);
+			} else {
+				panel_fit_reg &= ~BIT(3);
+			}
+		}
+	}
+
+	if (pd_context->gn4_plus) {
+		unsigned long src_ratio, dest_ratio;
+		panel_fit_reg = 0;
+		if (pd_context->native_dtd &&
+			(pd_context->current_mode->width != pd_context->native_dtd->width ||
+			pd_context->current_mode->height !=
+				pd_context->native_dtd->height)) {
+			/* Enable panel fitter */
+			panel_fit_reg = 0x80000000;
+
+			/* Select the pipe */
+			if (pd_context->pipe & PD_SET_MODE_PIPE_B) {
+				panel_fit_reg |= BIT(29);   /* bits[30:29] = 01 for pipe B */
+			}
+
+			/* Scaling mode:
+			 *    Default - Auto scaling src_ratio == dest_ratio
+			 *    Piller box scaling - src_ratio < dest_ratio
+			 *    Letter box scaling - src_ratio > dest_ratio */
+
+			/* To make this work correctly, port driver shall know the
+			 * size of the framebuffer, not the src mode. Most of the
+			 * times the src mode is fb, but not all the cases.
+			 * User has an attribute to change
+			 *    1. Between Pillerbox and auto, and vice versa
+			 *                and
+			 *    2. Between Letterbox and auto, and vice versa.
+			 */
+			if (pd_context->aspect_ratio) {
+				src_ratio = (pd_context->current_mode->width << 10)/
+					(pd_context->current_mode->height);
+				dest_ratio = (pd_context->native_dtd->width << 10)/
+					(pd_context->native_dtd->height);
+
+				if (dest_ratio > src_ratio) {
+					/* Pillarbox scaling */
+					panel_fit_reg |= BIT(27);
+				} else if (dest_ratio < src_ratio) {
+					/* Letterbox scaling */
+					panel_fit_reg |= BIT(27) | BIT(26);
+				}
+			}
+
+			/* Filter coefficient select: pd_context->text_tune = 0,1,2 */
+			panel_fit_reg |= (pd_context->text_tune << 24);
+		}
+	}
+
+	lvds_write_reg(pd_context, 0x61230, panel_fit_reg, 0xFFFFFFFF, PD_REG_MIO);
+	PD_DEBUG("panel_fit_reg 0x61230 = 0x%lx", panel_fit_reg);
+}
+
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: lvds_get_dclk
+ *
+ * Description:
+ *    Gets the Dclk for LVDS
+ *
+ * Parameters:
+ *    [IN] pd_context:  device context
+ *	  [OUT]lvds_info:   Structure that contains the min and max dclk
+ *
+ * Return:
+ *    None
+ *
+ *----------------------------------------------------------------------------
+ */
+static void lvds_get_dclk(lvds_context_t *pd_context, pd_dvo_info_t *lvds_info )
+{
+	PD_DEBUG("lvds_get_dclk()\n");
+	/* Get the min and max dclks for lvds */
+	if (pd_context->dual_channel) {
+		lvds_info->min_dclk = LVDS_MIN_DCLK * 2 ;
+		lvds_info->max_dclk = LVDS_MAX_DCLK * 2;
+	} else {
+		lvds_info->min_dclk = LVDS_MIN_DCLK;
+		lvds_info->max_dclk = LVDS_MAX_DCLK;
+	}
+/* This #define is the result of code size reduction effort. */
+#ifdef CONFIG_CTG
+	/* Set dclk for GM965 */
+	if(pd_context->chipset==PCI_DEVICE_ID_VGA_CTG){
+		/* Set dclk for GM965/CTG */
+		if (pd_context->dual_channel) {
+			lvds_info->min_dclk = LVDS_GM965_DUAL_MIN_DCLK ;
+			lvds_info->max_dclk = LVDS_GM965_DUAL_MAX_DCLK;
+		} else {
+			lvds_info->min_dclk = LVDS_GM965_SINGLE_MIN_DCLK;
+			lvds_info->max_dclk = LVDS_GM965_SINGLE_MAX_DCLK;
+		}
+	}
+#else
+
+	/* Set dclk for 915GM */
+	if(pd_context->chipset==PCI_DEVICE_ID_VGA_915AL){
+		if (pd_context->dual_channel) {
+			lvds_info->min_dclk = LVDS_915GM_DUAL_MIN_DCLK ;
+			lvds_info->max_dclk = LVDS_915GM_DUAL_MAX_DCLK;
+		} else {
+			lvds_info->min_dclk = LVDS_915GM_SINGLE_MIN_DCLK;
+			lvds_info->max_dclk = LVDS_915GM_SINGLE_MAX_DCLK;
+		}
+	} else if(pd_context->chipset==PCI_DEVICE_ID_VGA_945GM ||
+				pd_context->chipset==PCI_DEVICE_ID_VGA_945GME){
+		/* Set dclk for 945GM */
+		if (pd_context->dual_channel) {
+			lvds_info->min_dclk = LVDS_945GM_DUAL_MIN_DCLK ;
+			lvds_info->max_dclk = LVDS_945GM_DUAL_MAX_DCLK;
+		} else {
+			lvds_info->min_dclk = LVDS_945GM_SINGLE_MIN_DCLK;
+			lvds_info->max_dclk = LVDS_945GM_SINGLE_MAX_DCLK;
+		}
+	} else if(pd_context->chipset==PCI_DEVICE_ID_VGA_GM965 ||
+				pd_context->chipset==PCI_DEVICE_ID_VGA_GME965){
+		/* Set dclk for GM965 */
+		if (pd_context->dual_channel) {
+			lvds_info->min_dclk = LVDS_GM965_DUAL_MIN_DCLK ;
+			lvds_info->max_dclk = LVDS_GM965_DUAL_MAX_DCLK;
+		} else {
+			lvds_info->min_dclk = LVDS_GM965_SINGLE_MIN_DCLK;
+			lvds_info->max_dclk = LVDS_GM965_SINGLE_MAX_DCLK;
+		}
+	}
+#endif
+
+#ifdef CONFIG_TNC
+	/* Get the min and max dclks for Atom E6xx lvds */
+	if ((pd_context->chipset == PCI_DEVICE_ID_VGA_TNC) ||
+	    (pd_context->chipset == PCI_DEVICE_ID_VGA_TNC_A0) ||
+	    (pd_context->chipset == PCI_DEVICE_ID_VGA_LNC)) {
+		lvds_info->min_dclk = LVDS_TNC_SINGLE_MIN_DCLK;
+		/* Experimental feature to raise TC LVDS clk to 110MHz. */
+		if (pd_context->tc_110MHz_clk) {
+			lvds_info->max_dclk = 110000L;
+		} else {
+			lvds_info->max_dclk = LVDS_TNC_SINGLE_MAX_DCLK;
+		}
+	}
+#endif
+}
diff --git a/drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.h b/drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.h
new file mode 100644
index 0000000..dd9cc87
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/lvds/lvds.h
@@ -0,0 +1,164 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: lvds.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is header file for Internal LVDS PORT DRIVER.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PD_LVDS_H
+#define _PD_LVDS_H
+
+#include <pd.h>
+#include <pci.h>
+
+/* Values received from DPG hardware engineer. These are for single channel.  */
+
+#ifndef PCI_DEVICE_ID_VGA_855
+#define PCI_DEVICE_ID_VGA_855      0x3582
+#endif
+#ifndef PCI_DEVICE_ID_VGA_915AL
+#define PCI_DEVICE_ID_VGA_915AL    0x2592
+#endif
+#ifndef PCI_DEVICE_ID_VGA_945GM
+#define PCI_DEVICE_ID_VGA_945GM    0x27A2
+#endif
+#ifndef PCI_DEVICE_ID_VGA_945GME
+#define PCI_DEVICE_ID_VGA_945GME   0x27AE
+#endif
+#ifndef PCI_DEVICE_ID_VGA_GM965
+#define PCI_DEVICE_ID_VGA_GM965    0x2A02
+#endif
+#ifndef PCI_DEVICE_ID_VGA_GME965
+#define PCI_DEVICE_ID_VGA_GME965   0x2A12
+#endif
+#ifndef PCI_DEVICE_ID_VGA_CTG
+#define PCI_DEVICE_ID_VGA_CTG      0x2A42
+#endif
+#ifndef PCI_DEVICE_ID_VGA_PLB
+#define PCI_DEVICE_ID_VGA_PLB      0x8108
+#endif
+#ifndef PCI_DEVICE_ID_VGA_TNC
+#define PCI_DEVICE_ID_VGA_TNC      0x4108
+#endif
+#ifndef PCI_DEVICE_ID_VGA_TNC_A0
+#define PCI_DEVICE_ID_VGA_TNC_A0   0x4100
+#endif
+#ifndef PCI_DEVICE_ID_VGA_LNC
+#define PCI_DEVICE_ID_VGA_LNC      0x4102
+#endif
+
+
+#define LVDS_MIN_DCLK 12000L    /* in KHz */
+#define LVDS_MAX_DCLK 112000L   /* in KHz */
+/* definition for 915GM */
+#define LVDS_915GM_SINGLE_MIN_DCLK   20000L
+#define LVDS_915GM_SINGLE_MAX_DCLK  112000L
+#define LVDS_915GM_DUAL_MIN_DCLK     25000L
+#define LVDS_915GM_DUAL_MAX_DCLK    224000L
+
+/* definition for 945GM */
+#define LVDS_945GM_SINGLE_MIN_DCLK   25000L
+#define LVDS_945GM_SINGLE_MAX_DCLK  112000L
+#define LVDS_945GM_DUAL_MIN_DCLK     25000L
+#define LVDS_945GM_DUAL_MAX_DCLK    224000L
+
+/* definition for GM965 */
+#define LVDS_GM965_SINGLE_MIN_DCLK   25000L
+#define LVDS_GM965_SINGLE_MAX_DCLK  112000L
+#define LVDS_GM965_DUAL_MIN_DCLK     25000L
+#define LVDS_GM965_DUAL_MAX_DCLK    224000L
+
+/* definition for Atom E6xx */
+#define LVDS_TNC_SINGLE_MIN_DCLK     19750L
+#define LVDS_TNC_SINGLE_MAX_DCLK     79500L
+
+#define LVDS_MIN(a, b)       ((a)<(b)?(a):(b))
+
+/* Local attributes
+ * BIT15 of attribute Id specifies that it is a local attribute */
+#define LVDS_ATTR_ID_DATA_FORMAT   (BIT(15)|1)
+#define LVDS_ATTR_ID_TC_LVDS_CLK   (BIT(15)|2)
+
+/* Default values */
+#define LVDS_DEF_PANEL_DEPTH    18      /* Default panel depth */
+#define LVDS_DEF_DITHER          1      /* default=1 as default panel depth=18*/
+#define LVDS_DEF_SCALE_ON        1      /* Default scaling is on */
+#define LVDS_DEF_PANEL_FIT       1      /* Default panel fit in on */
+
+typedef struct _lvds_context {
+	unsigned short fp_width;
+	unsigned short fp_height;
+	unsigned short dual_channel;
+	unsigned short panel_type;
+	unsigned short panel_fit;
+	unsigned short panel_depth;
+	unsigned short dither;
+	unsigned short aspect_ratio;
+	unsigned long  text_tune;
+	unsigned long  pwm_intensity;
+	unsigned long  inverter_freq;
+	unsigned long  blm_legacy_mode;
+
+	unsigned long power_state;
+	unsigned short chipset;
+	unsigned char init_done;
+	unsigned char num_attrs;
+	unsigned long pipe;
+	unsigned short gfx_freq;
+	unsigned char gn4_plus;
+	unsigned char pwm_done;
+	unsigned char tc_110MHz_clk;
+
+	pd_callback_t *callback;
+	pd_timing_t   *timing_table;
+	pd_attr_t     *attr_list;
+	pd_timing_t   *native_dtd;
+	pd_timing_t   *current_mode;
+} lvds_context_t;
+
+extern int PD_MODULE_INIT(lvds_init, (void *handle));
+extern int PD_MODULE_EXIT(lvds_exit, (void));
+extern unsigned long lvds_validate(unsigned long cookie);
+extern int lvds_open(pd_callback_t *callback, void **context);
+extern int lvds_init_device(void *context);
+extern int lvds_close(void *context);
+extern int lvds_set_mode(void *context, pd_timing_t *mode,
+			unsigned long flags);
+extern int lvds_post_set_mode(void *context, pd_timing_t *mode,
+			unsigned long flags);
+extern int lvds_set_attrs(void *context, unsigned long num, pd_attr_t *list);
+extern int lvds_get_attrs(void *context, unsigned long*num, pd_attr_t **list);
+extern int lvds_get_timing_list(void *context, pd_timing_t *in_list,
+			pd_timing_t **list);
+extern int lvds_set_power(void *context, unsigned long state);
+extern int lvds_get_power(void *context, unsigned long *state);
+extern int lvds_save(void *context, void **state, unsigned long flags);
+extern int lvds_restore(void *context, void *state, unsigned long flags);
+extern int lvds_get_port_status(void *context, pd_port_status_t *port_status);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo.def b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo.def
new file mode 100644
index 0000000..f271b16
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo.def
@@ -0,0 +1,25 @@
+# Copyright (c) 2002-2011, Intel Corporation.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+LIBRARY sdvo
+
+EXPORTS
+    get_version
+    dpd_init
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.c b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.c
new file mode 100644
index 0000000..6d2d3c8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.c
@@ -0,0 +1,1484 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_attr.c
+ * $Revision: 1.16 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Functions to
+ *  Create Display Attribute table
+ *  Query and Alter attributes
+ *-----------------------------------------------------------------------------
+ */
+
+#include "sdvo_port.h"
+
+/* .......................................................................... */
+#ifndef CONFIG_MICRO
+#define MAKE_NAME(x)    x
+#else
+#define MAKE_NAME(x)    NULL
+#endif
+
+
+/* .......................................................................... */
+/*    Panel Power Sequencing Parameters */
+static pd_range_attr_t g_panel_power_attr[] =
+{
+		/*       ID                      Type                 Name */
+		/*       Flags   Default Value   Current Value   Min  Max Step */
+	PD_MAKE_ATTR(PD_ATTR_ID_FP_PWR_T1,   PD_ATTR_TYPE_RANGE, "FP Power T1",
+				PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0, 1),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_FP_PWR_T2,   PD_ATTR_TYPE_RANGE, "FP Power T2",
+				PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0, 1),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_FP_PWR_T3,   PD_ATTR_TYPE_RANGE, "FP Power T3",
+				PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0, 1),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_FP_PWR_T4,   PD_ATTR_TYPE_RANGE, "FP Power T4",
+				PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0, 1),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_FP_PWR_T5,   PD_ATTR_TYPE_RANGE, "FP Power T5",
+				PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0, 1),
+};
+
+static pd_range_attr_t g_fp_attr[] = {
+	/* Do not change the order of entries */
+		/*       ID                     Type                Name */
+		/* Flags  Default Value   Current Value   Min  Max Step */
+	PD_MAKE_ATTR(PD_ATTR_ID_DISPLAY,    PD_ATTR_TYPE_RANGE, "Display Type",
+		 PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0x4000, 1),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_PANEL_FIT,  PD_ATTR_TYPE_BOOL,  "PanelFit",
+		 PD_ATTR_FLAG_USER_INVISIBLE, 0, 0, 0, 0, 0),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_TEXT_TUNING,PD_ATTR_TYPE_RANGE, "Text Enhancement",
+		0,            0,              0,           0,  4,  1),
+
+	PD_MAKE_ATTR(PD_ATTR_ID_RB_SWAP_MODE, PD_ATTR_TYPE_BOOL,"RB Swap", 0, 0x00,	0x00, 0, 0, 0),
+};
+#ifndef CONFIG_MICRO
+static sdvo_tv_optimal_settings_t g_sdtv_op_settings[] =
+/*	chron_tv_code [7:0]
+ *		bits[7:2] = tv_format
+ *			480i = 0x00 ; 576i = 0x24 ; other TV line formats = 0xFF => for higher
+ *       resolution, YPbPr is the implied connector
+ *		bits [1:0] = output type
+ *			CVBS = 0x00, SVIDEO = 0x01, YPbPr = 0x02, SCART = 0x03
+ *	chron_tv_code,		hue,		saturation,		brightness,		contrast */
+{
+	/* SDTV [480i, 576i] - analog format w/
+	 * CVBS / SVIDEO / SCART / YPbPr connector */
+	/* NTSC_M Composite */
+	{0x00,	0x3F,		0x35,			0x81,			0x2F},
+	/* NTSC_M Svideo */
+	{0x01,	0x3D,		0x30,			0x81,			0x2F},
+	/* PAL_B/D/G/H/I/K/L Composite */
+	{0x24,	0x41,		0x38,			0x74,			0x3B},
+	/* PAL_B/D/G/H/I/K/L Svideo */
+	{0x25,	0x3F,		0x30,			0x74,			0x3B},
+	/* SECAM xxx- */
+
+	/* EDTV [ 480p, 576p] + HDTV [720p, 1080i/p] ) -
+	 * analog format w/ YPbPr connector */
+	{0xFF,	0x40,		0x45,			0x80,			0x2C},
+};
+#endif
+/* .......................................................................... */
+static sdvo_range_attr_data_t g_range_data[] =
+/*  ID                          Name                              Bytes*/
+/*  opcode_max                  opcode_get              opcode_set */
+{
+ {PD_ATTR_ID_BRIGHTNESS,        MAKE_NAME("Brightness"),            2,
+  GET_MAX_BRIGHTNESS,           GET_BRIGHTNESS,         SET_BRIGHTNESS},
+
+ {PD_ATTR_ID_PANEL_DEPTH,       MAKE_NAME("Panel Depth"),           1,
+  GET_LVDS_PANEL_INFORMATION,   GET_LVDS_PANEL_INFORMATION,
+													SET_LVDS_PANEL_INFORMATION},
+#ifndef CONFIG_MICRO
+ {PD_ATTR_ID_FP_BACKLIGHT_EN,   MAKE_NAME("Backlight Level"),       1,
+  GET_MAX_BACKLIGHT_LEVEL,      GET_BACKLIGHT_LEVEL,    SET_BACKLIGHT_LEVEL},
+
+ {PD_ATTR_ID_FLICKER,			MAKE_NAME("Flicker Filter"),		2,
+  GET_MAX_FLICKER_FILTER,		GET_FLICKER_FILTER, 	SET_FLICKER_FILTER},
+
+ {PD_ATTR_ID_2D_FLICKER,		MAKE_NAME("2D Flicker Filter"),		2,
+  GET_MAX_2D_FLICKER_FILTER,	GET_2D_FLICKER_FILTER,	SET_2D_FLICKER_FILTER},
+
+ {PD_ATTR_ID_SATURATION,		MAKE_NAME("Saturation"),				2,
+  GET_MAX_SATURATION,			GET_SATURATION, 		SET_SATURATION},
+
+ {PD_ATTR_ID_HUE,				MAKE_NAME("Hue"),					2,
+  GET_MAX_HUE,					GET_HUE,				SET_HUE},
+
+ {PD_ATTR_ID_CONTRAST,			MAKE_NAME("Contrast"), 			2,
+  GET_MAX_CONTRAST, 			GET_CONTRAST,			SET_CONTRAST},
+
+ {PD_ATTR_ID_HORZ_OVERSCAN, 	MAKE_NAME("Horz Overscan"),			2,
+  GET_MAX_HORIZONTAL_OVERSCAN,  GET_HORIZONTAL_OVERSCAN,SET_HORIZONTAL_OVERSCAN},
+
+ {PD_ATTR_ID_VERT_OVERSCAN, 	MAKE_NAME("Vert Overscan"),			2,
+  GET_MAX_VERTICAL_OVERSCAN,	GET_VERTICAL_OVERSCAN,	SET_VERTICAL_OVERSCAN},
+
+ {PD_ATTR_ID_HPOSITION, 		MAKE_NAME("Horz Position"),			2,
+  GET_MAX_HORIZONTAL_POSITION,  GET_HORIZONTAL_POSITION,SET_HORIZONTAL_POSITION},
+
+ {PD_ATTR_ID_VPOSITION, 		MAKE_NAME("Vert Position"),			2,
+  GET_MAX_VERTICAL_POSITION,	GET_VERTICAL_POSITION,	SET_VERTICAL_POSITION},
+
+ {PD_ATTR_ID_SHARPNESS, 		MAKE_NAME("Sharpness"),				2,
+  GET_MAX_SHARPNESS,			GET_SHARPNESS,			SET_SHARPNESS},
+
+ {PD_ATTR_ID_CHROMA_FILTER, 	MAKE_NAME("TV Chroma Filter"), 		2,
+  GET_MAX_TV_CHROMA_FILTER, 	GET_TV_CHROMA_FILTER,	SET_TV_CHROMA_FILTER},
+
+ {PD_ATTR_ID_LUMA_FILTER,		MAKE_NAME("TV Luma Filter"),		2,
+  GET_MAX_TV_LUMA_FILTER,		GET_TV_LUMA_FILTER, 	SET_TV_LUMA_FILTER},
+
+ {PD_ATTR_ID_ADAPTIVE_FLICKER,	MAKE_NAME("Adaptive Flicker Filter"),	2,
+  GET_MAX_ADAPTIVE_FLICKER_FILTER, GET_ADAPTIVE_FLICKER_FILTER, SET_ADAPTIVE_FLICKER_FILTER},
+
+ {PD_ATTR_ID_SSC,				MAKE_NAME("Spread Spectrum Clocking"), 1,
+  GET_MAX_SSC,					GET_SSC,				SET_SSC},
+#endif
+};
+
+
+/* .......................................................................... */
+static sdvo_bool_attr_data_t g_bool_data[] =
+/*	ID								Name					Bit_Mask_Cur	Bit_Mask_Def	 */
+/*	opcode_get				opcode_set */
+{
+ {PD_ATTR_ID_2_CHANNEL_PANEL, MAKE_NAME("Dual Channel"),    0x10,           0,
+  GET_LVDS_PANEL_INFORMATION, SET_LVDS_PANEL_INFORMATION},
+
+ /* 0-spwg, 1-openldi */
+ {PD_ATTR_ID_LVDS_PANEL_TYPE, MAKE_NAME("LVDS Panel Type"), 0x04,	        0,
+  GET_LVDS_PANEL_INFORMATION, SET_LVDS_PANEL_INFORMATION},
+
+ {PD_ATTR_ID_DITHER,          MAKE_NAME("Dither"),          0x01,        0x02,
+  GET_DITHER,                 SET_DITHER},
+
+#ifndef CONFIG_MICRO
+ {PD_ATTR_ID_DOT_CRAWL,     MAKE_NAME("Dot Crawl"),							0x01,	0x02,
+  GET_DOT_CRAWL,			SET_DOT_CRAWL},
+
+ {PD_ATTR_ID_PANEL_PROTECT_HSYNC,	MAKE_NAME("HSync Panel Protection"),	0x01,	0,
+  GET_LVDS_PANEL_PROTECTION, SET_LVDS_PANEL_PROTECTION},
+
+ {PD_ATTR_ID_PANEL_PROTECT_VSYNC,	MAKE_NAME("VSync Panel Protection"),	0x02,	0,
+  GET_LVDS_PANEL_PROTECTION, SET_LVDS_PANEL_PROTECTION},
+
+ {PD_ATTR_ID_PANEL_PROTECT_PIXCLK,	MAKE_NAME("Pixel Clock Protection"),	0x04,	0,
+  GET_LVDS_PANEL_PROTECTION, SET_LVDS_PANEL_PROTECTION},
+#endif
+};
+
+/* .......................................................................... */
+#ifndef CONFIG_MICRO
+/* List entries for the TV format, the header are in g_list_header[] */
+static sdvo_attr_list_entry_t g_list_entry_tv_format[] =
+{
+	{PD_TV_STD_NTSC_M,   MAKE_NAME("NTSC_M"),   0, 0x01},
+	{PD_TV_STD_NTSC_M_J, MAKE_NAME("NTSC_J"),   0, 0x02},
+	{PD_TV_STD_NTSC_433, MAKE_NAME("NTSC_433"),	0, 0x04},
+	{PD_TV_STD_PAL_B,    MAKE_NAME("PAL_B"),    0, 0x08},
+	{PD_TV_STD_PAL_D,    MAKE_NAME("PAL_D"),    0, 0x10},
+	{PD_TV_STD_PAL_G,    MAKE_NAME("PAL_G"),    0, 0x20},
+	{PD_TV_STD_PAL_H,    MAKE_NAME("PAL_H"),    0, 0x40},
+	{PD_TV_STD_PAL_I,    MAKE_NAME("PAL_I"),    0, 0x80},
+
+	{PD_TV_STD_PAL_M,   MAKE_NAME("PAL_M"),     1, 0x01},
+	{PD_TV_STD_PAL_N,   MAKE_NAME("PAL_N"),     1, 0x02},
+	{PD_TV_STD_PAL_NC,  MAKE_NAME("PAL_NC"),    1, 0x04},
+	{PD_TV_STD_PAL_60,  MAKE_NAME("PAL_60"),    1, 0x08},
+	{PD_TV_STD_SECAM_B,	MAKE_NAME("SECAM_B"),   1, 0x10},
+	{PD_TV_STD_SECAM_D,	MAKE_NAME("SECAM_D"),   1, 0x20},
+	{PD_TV_STD_SECAM_G,	MAKE_NAME("SECAM_G"),   1, 0x40},
+	{PD_TV_STD_SECAM_K,	MAKE_NAME("SECAM_K"),   1, 0x80},
+
+	{PD_TV_STD_SECAM_K1, MAKE_NAME("SECAM_K1"), 2, 0x01},
+	{PD_TV_STD_SECAM_L,  MAKE_NAME("SECAM_L"),  2, 0x02},
+	{PD_TV_STD_SECAM_60, MAKE_NAME("SECAM_60"), 2, 0x04},
+
+	{PD_HDTV_STD_SMPTE_240M_1080i59, MAKE_NAME("SMPTE_240M_1080i59"), 2, 0x08},
+	{PD_HDTV_STD_SMPTE_240M_1080i60, MAKE_NAME("SMPTE_240M_1080i60"), 2, 0x10},
+	{PD_HDTV_STD_SMPTE_260M_1080i59, MAKE_NAME("SMPTE_260M_1080i59"), 2, 0x20},
+	{PD_HDTV_STD_SMPTE_260M_1080i60, MAKE_NAME("SMPTE_260M_1080i60"), 2, 0x40},
+	{PD_HDTV_STD_SMPTE_274M_1080i50, MAKE_NAME("SMPTE_274M_1080i50"), 2, 0x80},
+
+	{PD_HDTV_STD_SMPTE_274M_1080i59, MAKE_NAME("SMPTE_274M_1080i59"), 3, 0x01},
+	{PD_HDTV_STD_SMPTE_274M_1080i60, MAKE_NAME("SMPTE_274M_1080i60"), 3, 0x02},
+	{PD_HDTV_STD_SMPTE_274M_1080p23, MAKE_NAME("SMPTE_274M_1080p23"), 3, 0x04},
+	{PD_HDTV_STD_SMPTE_274M_1080p24, MAKE_NAME("SMPTE_274M_1080p24"), 3, 0x08},
+	{PD_HDTV_STD_SMPTE_274M_1080p25, MAKE_NAME("SMPTE_274M_1080p25"), 3, 0x10},
+	{PD_HDTV_STD_SMPTE_274M_1080p29, MAKE_NAME("SMPTE_274M_1080p29"), 3, 0x20},
+	{PD_HDTV_STD_SMPTE_274M_1080p30, MAKE_NAME("SMPTE_274M_1080p30"), 3, 0x40},
+	{PD_HDTV_STD_SMPTE_274M_1080p50, MAKE_NAME("SMPTE_274M_1080p50"), 3, 0x80},
+
+	{PD_HDTV_STD_SMPTE_274M_1080p59, MAKE_NAME("SMPTE_274M_1080p59"), 4, 0x01},
+	{PD_HDTV_STD_SMPTE_274M_1080p60, MAKE_NAME("SMPTE_274M_1080p60"), 4, 0x02},
+	{PD_HDTV_STD_SMPTE_295M_1080i50, MAKE_NAME("SMPTE_295M_1080i50"), 4, 0x04},
+	{PD_HDTV_STD_SMPTE_295M_1080p50, MAKE_NAME("SMPTE_295M_1080p50"), 4, 0x08},
+	{PD_HDTV_STD_SMPTE_296M_720p59,  MAKE_NAME("SMPTE_296M_720p59"),  4, 0x10},
+	{PD_HDTV_STD_SMPTE_296M_720p60,  MAKE_NAME("SMPTE_296M_720p60"),  4, 0x20},
+	{PD_HDTV_STD_SMPTE_296M_720p50,  MAKE_NAME("SMPTE_296M_720p50"),  4, 0x40},
+	{PD_HDTV_STD_SMPTE_293M_480p59,  MAKE_NAME("SMPTE_293M_480p59"),  4, 0x80},
+
+	{PD_HDTV_STD_SMPTE_170M_480i59,  MAKE_NAME("MPTE_170M_480i59"),   5, 0x01},
+	{PD_HDTV_STD_ITURBT601_576i50,   MAKE_NAME("ITURBT601_576i50"),   5, 0x02},
+	{PD_HDTV_STD_ITURBT601_576p50,   MAKE_NAME("TURBT601_576p50"),    5, 0x04},
+	{PD_HDTV_STD_EIA_7702A_480i60,   MAKE_NAME("EIA_7702A_480i60"),   5, 0x08},
+	{PD_HDTV_STD_EIA_7702A_480p60,   MAKE_NAME("EIA_7702A_480p60"),   5, 0x10},
+	{0, NULL, 0, 0}
+};
+
+/* Enable below attributes once upper layers hooked up copyprotection API */
+
+/* List entries for the analog source, the header are in g_list_header[] */
+static sdvo_attr_list_entry_t g_list_entry_analog_src[] =
+{
+	{PD_ANALOG_SRC_NO_DATA,          MAKE_NAME("No Data"),                 0, 0x00},
+	{PD_ANALOG_SRC_PRE_RECORDED,     MAKE_NAME("Pre-recorded Packaged"),   0, 0x20},
+	{PD_ANALOG_SRC_NOT_PRE_RECORDED, MAKE_NAME("Not Analog Pre-recorded"), 0, 0x30},
+	{0, NULL, 0, 0}
+};
+
+/* List entries for the scan information, the header are in g_list_header[] */
+static sdvo_attr_list_entry_t g_list_entry_scan_info[] =
+{
+	{PD_SCAN_NO_DATA,      MAKE_NAME("No Data"),      0, 0x00},
+	{PD_SCAN_OVERSCANNED,  MAKE_NAME("Overscanned"),  0, 0x40},
+	{PD_SCAN_UNDERSCANNED, MAKE_NAME("Underscanned"), 0, 0x80},
+	{0, NULL, 0, 0}
+};
+
+/* List entries for the aspect ratio, the header are in g_list_header[] */
+static sdvo_attr_list_entry_t g_list_entry_aspect_ratio[] =
+{
+	{PD_SCAN_NO_DATA,      MAKE_NAME("No Data"), 1, 0x00},
+	{PD_SCAN_OVERSCANNED,  MAKE_NAME("4:3"),     1, 0x01},
+	{PD_SCAN_UNDERSCANNED, MAKE_NAME("16:9"),    1, 0x02},
+	{0, NULL, 0, 0}
+};
+
+/* List entries for the active format, the header are in g_list_header[] */
+static sdvo_attr_list_entry_t g_list_entry_active_format[] =
+{
+	{PD_ACTIVE_FORMAT_NO_DATA,
+	 MAKE_NAME("No Data"),                               1, 0x00},
+	{PD_ACTIVE_FORMAT_SQUARE_PIXELS,
+	 MAKE_NAME("Square Pixels"),                         1, 0x20},
+	{PD_ACTIVE_FORMAT_4_3_CENTER,
+	MAKE_NAME("4:3 Center"),                             1, 0x24},
+	{PD_ACTIVE_FORMAT_16_9_CENTER,
+	MAKE_NAME("16:9 Center"),                            1, 0x28},
+	{PD_ACTIVE_FORMAT_14_9_CENTER,
+	MAKE_NAME("14:9 Center"),                            1, 0x2C},
+	{PD_ACTIVE_FORMAT_16_9_LETTERBOX_TOP,
+	MAKE_NAME("16:9 Letterbox(Top)"),                    1, 0x08},
+	{PD_ACTIVE_FORMAT_14_9_LETTERBOX_TOP,
+	MAKE_NAME("14:9 Letterbox(Top)"),                    1, 0x0C},
+	{PD_ACTIVE_FORMAT_16_9_LETTERBOX_CENTER,
+	MAKE_NAME("16:9 Letterbox(Center)"),                 1, 0x10},
+	{PD_ACTIVE_FORMAT_4_3_SHOOT_PROTECT_14_9_CENTER,
+	MAKE_NAME("4:3(with shoot & protect 14:9 center)"),	 1, 0x34},
+	{PD_ACTIVE_FORMAT_16_9_SHOOT_PROTECT_14_9_CENTER,
+	MAKE_NAME("16:9(with shoot & protect 14:9 center)"), 1, 0x38},
+	{PD_ACTIVE_FORMAT_16_9_SHOOT_PROTECT_4_3_CENTER,
+	MAKE_NAME("16:9(with shoot & protect 4:3 center)"),	 1, 0x3C},
+	{0, NULL, 0, 0}
+};
+
+static sdvo_attr_list_header_t g_list_header[] =
+{
+	{45, PD_ATTR_ID_TVFORMAT, MAKE_NAME("TV Out Format"),
+	 GET_TV_OUTPUT_FORMAT, SET_TV_OUTPUT_FORMAT,
+	 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
+	 g_list_entry_tv_format},
+
+/* Enable below attributes once upper layers hooked up copyprotection API */
+
+    { 3, PD_ATTR_ID_ANALOG_SRC, MAKE_NAME("Analog Source"),
+	 GET_ANCILLARY_VIDEO_INFORMATION, SET_ANCILLARY_VIDEO_INFORMATION,
+	 {0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+	 g_list_entry_analog_src},
+
+	{ 3, PD_ATTR_ID_SCAN_INFO, MAKE_NAME("Scan Information"),
+	 GET_ANCILLARY_VIDEO_INFORMATION, SET_ANCILLARY_VIDEO_INFORMATION,
+	 {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+	 g_list_entry_scan_info},
+
+	{ 3, PD_ATTR_ID_PICTURE_ASPECT_RATIO, MAKE_NAME("Picture Aspect Ratio"),
+	 GET_ANCILLARY_VIDEO_INFORMATION, SET_ANCILLARY_VIDEO_INFORMATION,
+	 {0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+	 g_list_entry_aspect_ratio},
+
+	{11, PD_ATTR_ID_ACTIVE_FORMAT, MAKE_NAME("Active Format Aspect Ratio"),
+	 GET_ANCILLARY_VIDEO_INFORMATION, SET_ANCILLARY_VIDEO_INFORMATION,
+	 {0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+	 g_list_entry_active_format},
+
+	{ 0 }
+};
+
+#endif
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_set_tv_optimal_settings(sdvo_device_context_t *p_ctx,
+	unsigned char chron_tv_code)
+{
+
+	sdvo_status_t status = SS_SUCCESS;
+	pd_attr_t *p_attr;
+	int i;
+
+
+	for (i= 0;i <5;i ++)
+		if (chron_tv_code == g_sdtv_op_settings[i].tv_code) {
+			/* set hue */
+			p_attr = pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+					PD_ATTR_ID_HUE, 0);
+			if (NULL != p_attr)
+			{
+				status = sdvo_alter_range_attr(p_ctx, (pd_range_attr_t *)p_attr,
+						g_sdtv_op_settings[i].hue);
+				p_attr->current_value = g_sdtv_op_settings[i].hue; //update table
+			}
+
+			/* set saturation */
+			p_attr = pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+					PD_ATTR_ID_SATURATION, 0);
+			if (NULL != p_attr)
+			{
+				status = sdvo_alter_range_attr(p_ctx, (pd_range_attr_t *)p_attr,
+						g_sdtv_op_settings[i].saturation);
+
+				p_attr->current_value = g_sdtv_op_settings[i].saturation;
+			}
+
+			/* set brightness */
+			p_attr = pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+					PD_ATTR_ID_BRIGHTNESS, 0);
+			if (p_attr)
+			{
+				status = sdvo_alter_range_attr(p_ctx, (pd_range_attr_t *)p_attr,
+						g_sdtv_op_settings[i].brightness);
+
+				p_attr->current_value = g_sdtv_op_settings[i].brightness;
+			}
+
+			/* set contrast */
+			p_attr = pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+					PD_ATTR_ID_CONTRAST, 0);
+			if (p_attr)
+			{
+				status = sdvo_alter_range_attr(p_ctx, (pd_range_attr_t *)p_attr,
+						g_sdtv_op_settings[i].contrast);
+
+				p_attr->current_value = g_sdtv_op_settings[i].contrast;
+			}
+		}
+
+	return status;
+}
+#endif
+
+/* .......................................................................... */
+/* .......................................................................... */
+/*	============================================================================
+	Function	:	sdvo_get_static_attrs
+
+	Parameters	:	p_attr : Returns the list of avaliable static attributes
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+unsigned long sdvo_get_static_attrs(sdvo_device_context_t *p_ctx,
+	pd_attr_t *p_attr_list1)
+{
+	int num_attrs = 0;
+	pd_range_attr_t *p_attr_list = (pd_range_attr_t *)p_attr_list1;
+
+	if (p_attr_list == NULL) {
+
+		/*	Just return the number of available attributes */
+
+		if (p_ctx->out_type.flags & FP_DISP_MASK) {
+			num_attrs ++;
+
+			/* Enable Text Tuning */
+			if ((p_ctx->out_type.flags & LVDS_DISP_MASK) &&
+				(p_ctx->dev_cap.up_scaling)) {
+				num_attrs++;
+			}
+		}
+
+		/*	Add Display Attribute only if sDVO returned more than one
+			display type from GetDeviceCapabilities */
+		if (sdvo_is_multi_display_device(p_ctx)) {
+			num_attrs++;
+		}
+
+		/*  Red Blue Swap for DRGB output */
+		if((p_ctx->out_type.flags & DRGB_DISP_MASK) && (p_ctx->dev_cap.vendor_id == 0x81)){
+			num_attrs++;
+		}
+		return num_attrs;
+	}
+
+	/* ...................................................................... */
+	/*	Fill in the available attributes */
+
+	/*	Width and Height Attributes are available for LVDS and TMDS only */
+	if (p_ctx->out_type.flags & FP_DISP_MASK) {
+		p_attr_list[num_attrs] = g_fp_attr[1];   /* PANELFIT */
+		/* There are initial values */
+		p_attr_list[num_attrs].current_value = p_ctx->dev_cap.up_scaling;
+		p_ctx->up_scaling = p_ctx->dev_cap.up_scaling;
+		num_attrs++;
+
+		/* Enable Text Tuning */
+		if ((p_ctx->out_type.flags & LVDS_DISP_MASK) &&
+			(p_ctx->dev_cap.up_scaling)) {
+			p_attr_list[num_attrs] = g_fp_attr[2];   /* Text tuning */
+			num_attrs++;
+		}
+	}
+
+	/*	Add Display Attribute only if sDVO returned more than one
+		display type from GetDeviceCapabilities */
+	if (sdvo_is_multi_display_device(p_ctx)) {
+		p_attr_list[num_attrs] = g_fp_attr[0];
+		p_attr_list[num_attrs].default_value = p_ctx->dev_cap.output.flags;
+		p_attr_list[num_attrs].current_value = p_ctx->out_type.flags;
+		num_attrs++;
+	}
+
+	/*  Red Blue Swap for DRGB output */
+	if((p_ctx->out_type.flags & DRGB_DISP_MASK) && (p_ctx->dev_cap.vendor_id == 0x81)){
+		p_attr_list[num_attrs] = g_fp_attr[3];
+		num_attrs++;
+	}
+
+
+#ifdef DEBUG_BUILD_TYPE
+	/* ...................................................................... */
+	{
+		int i;
+		for (i = 0; i < num_attrs; i++) {
+			pd_attr_t *p_attr_cur = (pd_attr_t *)&p_attr_list[i];
+			PD_DEBUG("sdvo : sdvo_get_static_attrs : "
+			  "Adding attr='%s', id=%ld, type=%ld, default=%ld, current=%ld",
+			  SDVO_GET_ATTR_NAME(p_attr_cur), p_attr_cur->id, p_attr_cur->type,
+			  p_attr_cur->default_value, p_attr_cur->current_value);
+		}
+	}
+#endif
+
+	/* ...................................................................... */
+	return num_attrs;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_query_static_attr
+
+	Parameters	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_query_static_attr(sdvo_device_context_t *p_ctx,
+	pd_attr_t *p_attr)
+{
+	int b_error = 1;
+	i2c_reg_t temp_reg;
+
+	switch (p_attr->id) {
+		case PD_ATTR_ID_DISPLAY :
+			p_attr->current_value = p_ctx->out_type.flags;
+			b_error = 0;
+			break;
+
+		case PD_ATTR_ID_PANEL_FIT:
+			if (p_ctx->out_type.flags & FP_DISP_MASK) {
+				p_attr->current_value = p_ctx->up_scaling;
+				b_error = 0;
+			}
+			break;
+
+		case PD_ATTR_ID_TEXT_TUNING:
+			p_attr->current_value = p_ctx->text_tune;
+			b_error = 0;
+			break;
+		case PD_ATTR_ID_RB_SWAP_MODE:
+			{
+				sdvo_read_i2c_reg(p_ctx, 0x30, &temp_reg);
+				if (!(p_attr->flags & PD_ATTR_FLAG_NEED_RESTORE)){
+					p_attr->current_value = (temp_reg & 0x2)?1:0;
+				}
+				/*Impossible to return correct value since the encoder
+				has been reset.*/
+
+				b_error = 0;
+			}
+			break;
+	}
+
+	/*	..................................................................... */
+	if (b_error) {
+
+		PD_ERROR("sdvo : Error ! sdvo_query_static_attr : "
+			"Invalid attr='%s, id=%ld",
+			SDVO_GET_ATTR_NAME(p_attr), p_attr->id);
+
+		return SS_INVALID_ARGUMENT;
+	}
+
+	return SS_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_alter_static_attr
+
+	Parameters	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_alter_static_attr(sdvo_device_context_t *p_ctx,
+	pd_attr_t *p_attr, unsigned long new_value)
+{
+	int b_error = 1;
+	i2c_reg_t temp_reg;
+
+	switch (p_attr->id) {
+		case PD_ATTR_ID_PANEL_FIT:
+			if (p_ctx->out_type.flags & FP_DISP_MASK) {
+				p_ctx->up_scaling = (unsigned char)
+				((p_ctx->dev_cap.up_scaling)?new_value:0);
+				b_error = 0;
+			}
+			break;
+
+		case PD_ATTR_ID_TEXT_TUNING:
+			if (sdvo_set_upscalar_coefficient(p_ctx,(i2c_reg_t)new_value) ==
+				SS_SUCCESS) {
+				p_ctx->text_tune = (i2c_reg_t)new_value;
+			}
+			b_error = 0;
+			break;
+		case PD_ATTR_ID_RB_SWAP_MODE:
+			sdvo_read_i2c_reg(p_ctx, 0x30, &temp_reg);
+			if(new_value){
+				temp_reg |= 0x2;
+				p_attr->current_value = 1;
+			}else{
+				temp_reg &= ~(0x2);
+				p_attr->current_value = 0;
+			}
+			p_attr->flags |= PD_ATTR_FLAG_SETMODE;
+			p_attr->flags &= ~PD_ATTR_FLAG_NEED_RESTORE;
+			sdvo_write_i2c_reg(p_ctx, 0x30, temp_reg);
+			b_error = 0;
+			break;
+	}
+
+	/*	..................................................................... */
+	if (b_error) {
+		PD_ERROR("sdvo : Error ! sdvo_alter_static_attr : "
+			"Invalid attr='%s, id=%ld",
+			SDVO_GET_ATTR_NAME(p_attr), p_attr->id);
+		return SS_INVALID_ARGUMENT;
+	}
+	return SS_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_panel_pwr_seq_attrs
+
+	Parameters	:	pp_Attr : Returns the list of avaliable attributes
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+unsigned long sdvo_get_panel_pwr_seq_attrs(sdvo_device_context_t *p_ctx,
+	pd_range_attr_t *p_attr)
+{
+	sdvo_status_t status;
+	i2c_reg_t ret_val[8];
+	int i;
+
+	status = sdvo_execute_command(p_ctx,
+		GET_MAX_PANEL_POWER_SEQUENCING_PARAMETER,
+		0, NULL,
+		7, ret_val);
+	if (status != SS_SUCCESS) {
+		return 0;
+	}
+
+	if (p_attr == NULL) {
+		/*	Just return the number of available attributes */
+		return ARRAY_SIZE(g_panel_power_attr);
+	}
+
+	/* ...................................................................... */
+	g_panel_power_attr[0].max=((unsigned long)(ret_val[5]&0x03)<<8)|ret_val[0];
+	g_panel_power_attr[1].max=((unsigned long)(ret_val[5]&0x0C)<<6)|ret_val[1];
+	g_panel_power_attr[2].max=((unsigned long)(ret_val[5]&0x30)<<4)|ret_val[2];
+	g_panel_power_attr[3].max=((unsigned long)(ret_val[5]&0xC0)<<2)|ret_val[3];
+	g_panel_power_attr[4].max=((unsigned long)(ret_val[6]&0x03)<<8)|ret_val[4];
+
+	for (i = 0; i < 5; i++)	{
+		pd_range_attr_t *p_attr_cur = &g_panel_power_attr[i];
+		p_attr_cur->flags |= PD_ATTR_FLAG_DYNAMIC;
+#ifndef CONFIG_MICRO
+		status = sdvo_query_panel_pwr_seq_attr(p_ctx, p_attr_cur);
+		if (status != SS_SUCCESS) {
+			return 0;
+		}
+		p_attr_cur->default_value = p_attr_cur->current_value;
+#endif
+		PD_DEBUG("sdvo : sdvo_get_panel_pwr_seq_attrs : "
+		  "Adding attr='%s', id=%ld, default=%ld, current=%ld, min=%ld, "
+		  "max=%ld, step=%ld",
+		  SDVO_GET_ATTR_NAME(p_attr_cur), p_attr_cur->id,
+		  p_attr_cur->default_value, p_attr_cur->current_value,
+		  p_attr_cur->min, p_attr_cur->max, p_attr_cur->step);
+
+	}
+
+	pd_memcpy(p_attr, g_panel_power_attr, sizeof(g_panel_power_attr));
+	return ARRAY_SIZE(g_panel_power_attr);
+}
+
+
+/*	============================================================================
+	Function	:
+
+	Parameters	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_query_panel_pwr_seq_attr(sdvo_device_context_t *p_ctx,
+	pd_range_attr_t *p_attr)
+{
+#ifndef CONFIG_MICRO
+	sdvo_status_t status;
+	i2c_reg_t ret_val[8];
+
+	status = sdvo_execute_command(p_ctx, GET_PANEL_POWER_SEQUENCING_PARAMETER,
+								 0, NULL,
+								 7, ret_val);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	switch (p_attr->id) {
+
+		case PD_ATTR_ID_FP_PWR_T1 :
+			p_attr->current_value = p_ctx->t1 =
+				((unsigned short)(ret_val[5] & 0x03) << 8) | ret_val[0];
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T2 :
+			p_attr->current_value = p_ctx->t2 =
+				((unsigned short)(ret_val[5] & 0x0C) << 6) | ret_val[1];
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T3 :
+			p_attr->current_value = p_ctx->t3 =
+				((unsigned short)(ret_val[5] & 0x30) << 4) | ret_val[2];
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T4 :
+			p_attr->current_value = p_ctx->t4 =
+				((unsigned short)(ret_val[5] & 0xC0) << 2) | ret_val[3];
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T5 :
+			p_attr->current_value = p_ctx->t5 =
+				((unsigned short)(ret_val[6] & 0x03) << 8) | ret_val[4];
+			break;
+
+		default :
+			return SS_INVALID_ARGUMENT;
+	}
+#endif
+	return SS_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_alter_panel_pwr_seq_attr
+
+	Parameters	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_alter_panel_pwr_seq_attr(sdvo_device_context_t *p_ctx,
+	pd_range_attr_t *p_attr,
+	unsigned long new_value)
+{
+	sdvo_status_t status;
+	i2c_reg_t arg[8];
+	i2c_reg_t new_val_low  = (i2c_reg_t)(new_value & 0xFF);
+	i2c_reg_t new_val_high = (i2c_reg_t)((new_value & 0x0300) >> 8);
+
+	if ((new_value < p_attr->min) || (new_value > p_attr->max)) {
+		PD_DEBUG("Requested values out of range, new_value = %lu.",
+			new_value);
+		return SS_INVALID_ARGUMENT;
+	}
+
+	status = sdvo_execute_command(p_ctx, GET_PANEL_POWER_SEQUENCING_PARAMETER,
+		0, NULL, 7, arg);
+	if (status != SS_SUCCESS) {
+		PD_DEBUG("GET_PANEL_POWER_SEQUENCING_PARAMETER failed status = %u",
+			status);
+		return status;
+	}
+
+	switch (p_attr->id) {
+		case PD_ATTR_ID_FP_PWR_T1 :
+			p_ctx->t1 = (unsigned short)new_value;
+			arg[0] = new_val_low;
+			arg[5] &= 0xFC;
+			arg[5] |= new_val_high;
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T2 :
+			p_ctx->t2 = (unsigned short)new_value;
+			arg[1] = new_val_low;
+			arg[5] &= 0xF3;
+			arg[5] |= (new_val_high << 2);
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T3 :
+			p_ctx->t3 = (unsigned short)new_value;
+			arg[2] = new_val_low;
+			arg[5] &= 0xCF;
+			arg[5] |= (new_val_high << 4);
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T4 :
+			p_ctx->t4 = (unsigned short)new_value;
+			arg[3] = new_val_low;
+			arg[5] &= 0x3F;
+			arg[5] |= (new_val_high << 6);
+			break;
+
+		case PD_ATTR_ID_FP_PWR_T5 :
+			p_ctx->t5 = (unsigned short)new_value;
+			arg[4] = new_val_low;
+			arg[6] &= 0xFC;
+			arg[6] |= new_val_high;
+			break;
+
+		default :
+			return SS_INVALID_ARGUMENT;
+	}
+
+	status = sdvo_execute_command(p_ctx, SET_PANEL_POWER_SEQUENCING_PARAMETER,
+								 7, arg,
+								 0, NULL);
+	return status;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_range_attrs returns all the available range
+					attributes
+
+	Parameters	:	pp_Attr : Returns the list of avaliable range attributes
+
+	Remarks     :	sdvo_get_range_attrs returns all the range attributes
+					from g_range_data table that are supported by the SDVO device.
+					It allocates memory for all possible range attributes.
+					The caller is responsible for adding the range attributes to
+					port driver attribute table and freeing the memory allocated
+					for pp_Attr.
+
+	Returns     :	Number of range attributes returned in pp_Attr list.
+	------------------------------------------------------------------------- */
+unsigned long sdvo_get_range_attrs(sdvo_device_context_t *p_ctx,
+	pd_range_attr_t *p_attr)
+{
+	unsigned long num_attrs, i;
+	sdvo_status_t status;
+	unsigned char byte_ret[4];
+
+	/*	..................................................................... */
+	if (p_attr == NULL) {
+		/*	Just return the number of available attributes */
+		num_attrs = 0;
+
+		for (i = 0; i < ARRAY_SIZE(g_range_data); i++) {
+			status = sdvo_execute_command(p_ctx, g_range_data[i].opcode_max,
+							0, NULL,
+							(i2c_reg_t)(g_range_data[i].num_bytes * 2),
+							byte_ret);
+			if (status != SS_SUCCESS) {
+				continue;
+			}
+
+			status = sdvo_execute_command(p_ctx, g_range_data[i].opcode_get,
+										 0, NULL,
+										 (i2c_reg_t)(g_range_data[i].num_bytes),
+										 byte_ret);
+			if (status != SS_SUCCESS) {
+				continue;
+			}
+
+			num_attrs++;
+		}
+
+		return num_attrs;
+	}
+
+	/*	..................................................................... */
+	num_attrs = 0;
+
+	for (i = 0; i < ARRAY_SIZE(g_range_data); i++) {
+
+		pd_range_attr_t *p_attr_cur = &p_attr[num_attrs];
+
+		status = sdvo_execute_command(p_ctx, g_range_data[i].opcode_max,
+									 0, NULL,
+									 (i2c_reg_t)(g_range_data[i].num_bytes * 2),
+									 byte_ret);
+		if (status != SS_SUCCESS) {
+
+			continue;
+		}
+
+		p_attr_cur->id = g_range_data[i].id;
+		p_attr_cur->type = PD_ATTR_TYPE_RANGE;
+		p_attr_cur->flags |= PD_ATTR_FLAG_DYNAMIC;
+
+#ifndef CONFIG_MICRO
+		pd_strcpy(p_attr_cur->name, g_range_data[i].p_name);
+#endif
+
+		p_attr_cur->default_value = g_range_data[i].num_bytes == 1 ?
+									byte_ret[1] : byte_ret[2] + (byte_ret[3] << 8);
+		p_attr_cur->min	= 0;
+		p_attr_cur->step = 1;
+		p_attr_cur->max	= g_range_data[i].num_bytes == 1 ?
+			byte_ret[0] : byte_ret[0] + (byte_ret[1] << 8);
+
+		status = sdvo_query_range_attr(p_ctx, p_attr_cur);
+		if (status != SS_SUCCESS) {
+			continue;
+		}
+
+		PD_DEBUG("sdvo : sdvo_get_range_attrs : "
+				  "Adding attr='%s', id=%ld, default=%ld, current=%ld, min=%ld, "
+				  "max=%ld, step=%ld",
+				  SDVO_GET_ATTR_NAME(p_attr_cur), p_attr_cur->id,
+				  p_attr_cur->default_value, p_attr_cur->current_value,
+				  p_attr_cur->min, p_attr_cur->max, p_attr_cur->step);
+
+		num_attrs++;
+	}
+
+	/*	..................................................................... */
+	return num_attrs;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_query_range_attr is called to query the current value
+					of a specific range attribute
+
+	Parameters	:	p_attr : Pointer to range attribute
+
+	Remarks     :	sdvo_query_range_attr scans the g_range_data list to find
+					the range attribute of id specified in p_attr.
+					If found it executes the opcode_get command to get the current
+					value of the attribute.
+
+	Returns     :	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_query_range_attr(sdvo_device_context_t *p_ctx,
+	pd_range_attr_t *p_attr)
+{
+	int i;
+	sdvo_status_t status;
+
+	for (i = 0; i < ARRAY_SIZE(g_range_data); i++) {
+
+		if (g_range_data[i].id == p_attr->id) {
+
+			status = sdvo_execute_command(p_ctx, g_range_data[i].opcode_get,
+									   0, NULL,
+									   g_range_data[i].num_bytes,
+									   (unsigned char *)&p_attr->current_value);
+
+			if (status == SS_SUCCESS) {
+				/* Special case for Panel depth */
+				if (g_range_data[i].id == PD_ATTR_ID_PANEL_DEPTH) {
+					p_attr->min = 18;
+					p_attr->step = 6;
+					p_attr->max = 24;
+					p_attr->default_value = 18;
+					if (p_attr->current_value & 0x01) {
+						p_attr->current_value = 24;
+					} else {
+						p_attr->current_value = 18;
+					}
+				}
+			}
+			return status;
+		}
+	}
+
+	return SS_INVALID_ARGUMENT;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_alter_range_attr is called to set the new value of a
+					specific range attribute
+
+	Parameters	:	p_attr : Pointer to range attribute
+
+	Remarks     :	sdvo_query_range_attr scans the g_range_data list to find
+					the range attribute of id specified in p_attr.
+					If found it executes the opcode_set command to set the new
+					value of the attribute.
+
+	Returns     :	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_alter_range_attr(sdvo_device_context_t *p_ctx,
+	pd_range_attr_t *p_attr, unsigned long new_value)
+{
+	int i;
+	sdvo_status_t status;
+
+
+	if ((new_value < p_attr->min) || (new_value > p_attr->max)) {
+
+		return SS_INVALID_ARGUMENT;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(g_range_data); i++) {
+
+		if (g_range_data[i].id == p_attr->id) {
+			/* Special case handling for panel depth attribute */
+			if (p_attr->id == PD_ATTR_ID_PANEL_DEPTH) {
+				i2c_reg_t cur_value;
+				status = sdvo_execute_command(p_ctx,
+					g_range_data[i].opcode_get,
+					0, NULL,
+					1, &cur_value);
+				if (status != SS_SUCCESS) {
+					return status;
+				}
+
+				if (new_value == 24) {
+					/* 24-bit display */
+					new_value = cur_value | 0x01; /* set bits [1:0] = 01 */
+				} else {
+					/* 18-bit display */
+					new_value = cur_value & 0xFC; /* set bits [1:0] = 00 */
+				}
+			}
+
+			/*Special case handling for CH7308 EMI on/off operation */
+
+			if (p_attr->id == PD_ATTR_ID_SSC) {
+
+				i2c_reg_t reg;
+				if( new_value > 0) /* turn on EMI for value range from 1 - 15 */
+				{
+
+					sdvo_read_i2c_reg(p_ctx, 0x3C, &reg);
+					sdvo_write_i2c_reg(p_ctx, 0x3C, (reg & 0xFC));
+					sdvo_read_i2c_reg(p_ctx, 0x3E, &reg);
+					sdvo_write_i2c_reg(p_ctx, 0x3E, (reg | 0x80));
+				}
+				else /* turn off EMI */
+				{
+					sdvo_read_i2c_reg(p_ctx, 0x3C, &reg);
+					sdvo_write_i2c_reg(p_ctx, 0x3C, (reg | 0x01));
+					sdvo_read_i2c_reg(p_ctx, 0x3E, &reg);
+					sdvo_write_i2c_reg(p_ctx, 0x3E, (reg & 0x7F));
+
+				}
+
+
+			}
+
+
+			return sdvo_execute_command(p_ctx, g_range_data[i].opcode_set,
+								   g_range_data[i].num_bytes,
+								   (unsigned char *)&new_value,
+								   0, NULL);
+		}
+	}
+
+	return SS_INVALID_ARGUMENT;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_bool_attrs returns all the available boolean
+					attributes
+
+	Parameters	:	pp_Attr : Returns the list of avaliable boolean attributes
+
+	Remarks     :	sdvo_get_bool_attrs returns all the boolean attributes
+					from g_bool_data table that are supported by the SDVO device.
+					It allocates memory for all possible boolean attributes.
+					The caller is responsible for adding the booelan attributes
+					to the port driver attribute table and freeing the memory
+					allocated for pp_Attr.
+
+	Returns     :	Number of boolean attributes returned in pp_Attr list.
+	------------------------------------------------------------------------- */
+unsigned long sdvo_get_bool_attrs(sdvo_device_context_t *p_ctx,
+	pd_bool_attr_t *p_attr)
+{
+	unsigned long num_attrs, i;
+	sdvo_status_t status;
+
+	/*	..................................................................... */
+	if (p_attr == NULL) {
+
+		/*	Just return the number of available attributes */
+		i2c_reg_t ret_value;
+
+		num_attrs = 0;
+
+		for (i = 0; i < ARRAY_SIZE(g_bool_data); i++) {
+			if((p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI) &&
+				(p_ctx->dev_cap.device_id == 0x1) &&
+	            ((g_bool_data[i].id == PD_ATTR_ID_PANEL_PROTECT_HSYNC) ||
+	            (g_bool_data[i].id == PD_ATTR_ID_PANEL_PROTECT_VSYNC))) {
+
+	            continue;
+	        }
+			status = sdvo_execute_command(p_ctx, g_bool_data[i].opcode_get,
+						 0, NULL,
+						 1, &ret_value);
+			if (status != SS_SUCCESS) {
+
+				continue;
+			}
+
+			num_attrs++;
+		}
+
+		return num_attrs;
+	}
+
+	/*	..................................................................... */
+	num_attrs = 0;
+
+	for (i = 0; i < ARRAY_SIZE(g_bool_data); i++) {
+
+		pd_bool_attr_t *p_attr_cur = &p_attr[num_attrs];
+
+		p_attr_cur->id   = g_bool_data[i].id;
+		p_attr_cur->type = PD_ATTR_TYPE_BOOL;
+		p_attr_cur->flags |= PD_ATTR_FLAG_DYNAMIC;
+
+		if((p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI) &&
+			(p_ctx->dev_cap.device_id == 0x1) &&
+	        ((g_bool_data[i].id == PD_ATTR_ID_PANEL_PROTECT_HSYNC) ||
+	        (g_bool_data[i].id == PD_ATTR_ID_PANEL_PROTECT_VSYNC))) {
+
+	        continue;
+	    }
+
+
+		status = sdvo_query_bool_attr(p_ctx, p_attr_cur);
+		if (status != SS_SUCCESS)
+			continue;
+
+		p_attr_cur->default_value = p_attr_cur->current_value;
+
+#ifndef CONFIG_MICRO
+		pd_strcpy(p_attr_cur->name, g_bool_data[i].p_name);
+#endif
+
+		PD_DEBUG("sdvo : sdvo_get_bool_attrs : "
+				  "Adding attr='%s', id=%ld, default=%ld, current=%ld",
+				  SDVO_GET_ATTR_NAME(p_attr_cur), p_attr_cur->id,
+				  p_attr_cur->default_value, p_attr_cur->current_value);
+
+		num_attrs++;
+	}
+
+	/*	..................................................................... */
+	return num_attrs;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_query_bool_attr is called to query the current value
+					of a specific boolean attribute
+
+	Parameters	:	p_attr : Pointer to boolean attribute
+
+
+	Remarks     :	sdvo_query_bool_attr scans the g_bool_data list to find
+					the boolean attribute of id specified in p_attr.
+					If found it executes the opcode_get command to get the current
+					value of the attribute.
+
+	Returns     :	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_query_bool_attr(sdvo_device_context_t *p_ctx,
+	pd_bool_attr_t *p_attr)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(g_bool_data); i++) {
+
+		if (g_bool_data[i].id == p_attr->id) {
+
+			sdvo_status_t status;
+			i2c_reg_t ret_value;
+
+			status = sdvo_execute_command(p_ctx, g_bool_data[i].opcode_get,
+										 0, NULL,
+										 1, &ret_value);
+
+			if (status == SS_SUCCESS) {
+				if (ret_value & g_bool_data[i].bit_mask_cur) {
+					p_attr->current_value = 1;
+				} else {
+					p_attr->current_value = 0;
+				}
+
+				if (g_bool_data[i].bit_mask_def != 0) {
+					p_attr->default_value = ret_value & g_bool_data[i].bit_mask_def;
+				}
+			}
+
+			return status;
+		}
+	}
+
+	return SS_INVALID_ARGUMENT;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_alter_bool_attr is called to set the new value of a
+					specific boolean attribute
+
+	Parameters	:	p_attr : Pointer to boolean attribute
+
+	Remarks     :	sdvo_alter_range_attr scans the g_bool_data list to find
+					the bool attribute of id specified in p_attr.
+					If found it executes the opcode_set command to set the new
+					value of the attribute.
+
+	Returns     :	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_alter_bool_attr(sdvo_device_context_t *p_ctx,
+	pd_bool_attr_t *p_attr, unsigned long new_value)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(g_bool_data); i++) {
+		if (g_bool_data[i].id == p_attr->id) {
+			sdvo_status_t status;
+			i2c_reg_t cur_value;
+
+			status = sdvo_execute_command(p_ctx, g_bool_data[i].opcode_get,
+										 0, NULL,
+										 1, &cur_value);
+			if (status != SS_SUCCESS) {
+				return status;
+			}
+#ifndef CONFIG_MICRO
+			if(p_attr->id == PD_ATTR_ID_DITHER){
+				cur_value &= 0;
+			}
+#endif
+			if (new_value) {
+				cur_value |= g_bool_data[i].bit_mask_cur;
+			} else {
+				cur_value &= ~g_bool_data[i].bit_mask_cur;
+			}
+
+			status = sdvo_execute_command(p_ctx, g_bool_data[i].opcode_set,
+										 1, &cur_value,
+										 0, NULL);
+			return status;
+		}
+	}
+
+	return SS_INVALID_ARGUMENT;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_list_attrs returns all the available list
+					attributes
+
+	Parameters	:	p_attr : Returns the list of avaliable list attributes
+
+	Remarks     :	sdvo_get_list_attrs returns all the list attributes
+					from g_list_data table that are supported by the SDVO device.
+					It allocates memory for all possible list attributes.
+					The caller is responsible for adding the list attributes
+					to the port driver attribute table and freeing the memory
+					allocated for p_attr.
+
+	Returns     :	Number of list attributes returned in pp_Attr list.
+	------------------------------------------------------------------------- */
+unsigned long sdvo_get_list_attrs(sdvo_device_context_t *p_ctx,
+	pd_list_attr_t *p_attr)
+{
+#ifndef CONFIG_MICRO
+	unsigned long num_attrs, i, j;
+	sdvo_status_t status;
+	sdvo_attr_list_entry_t *list_item;
+
+	list_item = NULL;
+	num_attrs = 0;
+	i = j = 0;
+
+	/*	..................................................................... */
+	if (p_attr == NULL) {
+
+		/*	Just return the number of available attributes */
+		i2c_reg_t ret_value[8];
+		while (g_list_header[i].num_entries) {
+
+			status = sdvo_execute_command(p_ctx, g_list_header[i].opcode_get,
+										 0, NULL, 8, ret_value);
+
+			if (status == SS_SUCCESS) {
+				num_attrs += (g_list_header[i].num_entries + 1);
+			}
+
+			++i;	/*	Skip to the next list hdr */
+		}
+
+		return num_attrs;
+	}
+
+	while (g_list_header[i].num_entries) {
+
+		pd_list_attr_t *p_hdr = &p_attr[num_attrs];
+
+		p_hdr->type	= PD_ATTR_TYPE_LIST;
+		p_hdr->id = g_list_header[i].id;
+		p_hdr->num_entries = g_list_header[i].num_entries;
+		p_hdr->flags |= PD_ATTR_FLAG_DYNAMIC;
+
+		status = sdvo_query_list_attr(p_ctx, p_hdr);
+		if (status != SS_SUCCESS) {
+			++i; /* Skip to the next list header */
+			continue;
+		}
+
+		pd_strcpy(p_hdr->name, g_list_header[i].p_name);
+		p_hdr->default_index = p_hdr->current_index;
+
+		PD_DEBUG("sdvo : sdvo_get_list_attrs : "
+				  "Adding attr='%s', id=%ld, default=%ld, current=%ld, "
+				  "num_entries=%ld",
+				  SDVO_GET_ATTR_NAME(p_hdr), p_hdr->id, p_hdr->default_index, p_hdr->current_index,
+				  p_hdr->num_entries);
+
+		++num_attrs;
+		/*	Copy the list entries */
+		list_item = g_list_header[i].attr_list;
+		for (j = 0; j < g_list_header[i].num_entries; ++j, ++num_attrs) {
+
+			pd_list_entry_attr_t *p_entry =
+				(pd_list_entry_attr_t *)&p_attr[num_attrs];
+
+			p_entry->id		= g_list_header[i].id;  /* Header ID */
+			p_entry->type	= PD_ATTR_TYPE_LIST_ENTRY;
+			p_entry->value	= list_item[j].id;      /* Entry ID */
+			p_entry->flags  |= PD_ATTR_FLAG_DYNAMIC;
+
+			pd_strcpy(p_entry->name, list_item[j].p_name);
+			PD_DEBUG("sdvo : sdvo_get_list_attrs : Entry[%ld]=%s, id=%ld, "
+					  "value=%ld",
+					  j, SDVO_GET_ATTR_NAME(p_entry), p_entry->id,
+					  p_entry->value);
+		}
+
+		++i;   /*	Skip to the next list header */
+	}
+
+	/*	..................................................................... */
+	return num_attrs;
+#else
+	return 0;
+#endif
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_query_list_attr is called to query the current value
+					of a specific list attribute
+
+	Parameters	:	p_attr : Pointer to list header attribute
+
+	Remarks     :	sdvo_query_list_attr scans the g_list_data list to find
+					the list attribute of id specified in p_attr.
+					If found it executes the opcode_get command to get the current
+					value of the attribute.
+
+	Returns     :	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_query_list_attr(sdvo_device_context_t *p_ctx,
+	pd_list_attr_t *p_attr)
+{
+#ifndef CONFIG_MICRO
+	i2c_reg_t ret_value[8];
+	int i, j;
+	sdvo_status_t status;
+	sdvo_attr_list_entry_t *list_item;
+
+	i = 0;
+	list_item = NULL;
+	while (g_list_header[i].num_entries) {
+
+		if (g_list_header[i].id != p_attr->id) {
+			++i;	/*	Skip to the next list hdr */
+			continue;
+		}
+
+		status = sdvo_execute_command(p_ctx, g_list_header[i].opcode_get,
+									 0, NULL,
+									 8, ret_value);
+		if (status != SS_SUCCESS) {
+
+			return status;
+		}
+
+		for (j = 0; j < 8; ++j) {
+			ret_value[j] &= g_list_header[i].bit_masks[j];
+		}
+
+		list_item = g_list_header[i].attr_list;
+		for (j = 0; j < g_list_header[i].num_entries; ++j) {
+
+			if (ret_value[list_item[j].reg_offset] == list_item[j].value) {
+				p_attr->current_index = j + 1;
+
+				return SS_SUCCESS;
+			}
+		}
+
+		/* If current_index isn't found then the default hardware state is
+		 * not in one of the known states, then set it to the first one */
+		p_attr->current_index = 1;
+		return sdvo_alter_list_attr(p_ctx, p_attr, 1);
+	}
+
+	PD_ERROR("sdvo : Warning ! sdvo_query_list_attr : Unable to find list "
+	    "header");
+#endif
+	return SS_INVALID_ARGUMENT;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_alter_list_attr is called to set the new value of a
+					specific list attribute
+
+	Parameters	:	p_attr : Pointer to list attribute
+
+	Remarks     :	sdvo_alter_list_attr scans the g_list_data list to find
+					the list attribute of id specified in p_attr.
+					If found it executes the opcode_set command to set the new
+					value of the attribute.
+
+	Returns     :	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_alter_list_attr(sdvo_device_context_t *p_ctx,
+	pd_list_attr_t *p_attr, unsigned long new_index)
+{
+#ifndef CONFIG_MICRO
+	unsigned short i, j;
+	sdvo_status_t status;
+	i2c_reg_t ret_value[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+	sdvo_attr_list_entry_t *list_item;
+
+	if (new_index > p_attr->num_entries) {
+		return SS_INVALID_ARGUMENT;
+	}
+
+	list_item = NULL;
+	i = 0;
+	while (g_list_header[i].num_entries) {
+		if (g_list_header[i].id != p_attr->id) {
+			++i;   /*	Skip to the next list hdr */
+			continue;
+		}
+
+		/*	Found the list header */
+		/*	Get the current value of the attribute(s) */
+		status = sdvo_execute_command(p_ctx, g_list_header[i].opcode_get,
+			0, NULL, 8, ret_value);
+		if (status != SS_SUCCESS) {
+			return status;
+		}
+
+		/* Mask out the parameter bits */
+		for (j = 0; j < 8; ++j) {
+			ret_value[j] &= ~g_list_header[i].bit_masks[j];
+		}
+
+		list_item = g_list_header[i].attr_list;  /* Get the list entries */
+		/* Set new index bits */
+		ret_value[list_item[new_index - 1].reg_offset] |=
+			list_item[new_index - 1].value;
+
+		status = sdvo_execute_command(p_ctx, g_list_header[i].opcode_set,
+			8, ret_value, 0, NULL);
+		/* If opcode_set(new_value) is failed, assuming it will stay at old value.
+		 * If this isn't the case, then opcode_set(old_value) is required. */
+		if (status != SS_SUCCESS) {
+			/* Set old value */
+		}
+		return status;
+	}
+#endif
+	return SS_INVALID_ARGUMENT;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.h b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.h
new file mode 100644
index 0000000..4c58315
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_attr.h
@@ -0,0 +1,141 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_attr.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Definitions for display attribute
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _SDVO_ATTR_H_
+#define _SDVO_ATTR_H_
+
+unsigned long sdvo_get_static_attrs(sdvo_device_context_t *p_ctx,   pd_attr_t *p_attr);
+sdvo_status_t sdvo_query_static_attr(sdvo_device_context_t *p_ctx,  pd_attr_t *p_attr);
+sdvo_status_t sdvo_alter_static_attr(sdvo_device_context_t *p_ctx,  pd_attr_t *p_attr,
+	unsigned long new_value);
+
+unsigned long sdvo_get_panel_pwr_seq_attrs(sdvo_device_context_t *p_ctx,   pd_range_attr_t *p_attr);
+sdvo_status_t sdvo_query_panel_pwr_seq_attr(sdvo_device_context_t *p_ctx,  pd_range_attr_t *p_attr);
+sdvo_status_t sdvo_alter_panel_pwr_seq_attr(sdvo_device_context_t *p_ctx,  pd_range_attr_t *p_attr,
+	unsigned long new_value);
+
+unsigned long sdvo_get_range_attrs(sdvo_device_context_t *p_ctx,   pd_range_attr_t *p_attr);
+sdvo_status_t sdvo_query_range_attr(sdvo_device_context_t *p_ctx,  pd_range_attr_t *p_attr);
+sdvo_status_t sdvo_alter_range_attr(sdvo_device_context_t *p_ctx,  pd_range_attr_t *p_attr,
+	unsigned long new_value);
+
+unsigned long sdvo_get_bool_attrs(sdvo_device_context_t *p_ctx,   pd_bool_attr_t *p_attr);
+sdvo_status_t sdvo_query_bool_attr(sdvo_device_context_t *p_ctx,  pd_bool_attr_t *p_attr);
+sdvo_status_t sdvo_alter_bool_attr(sdvo_device_context_t *p_ctx,  pd_bool_attr_t *p_attr,
+	unsigned long new_value);
+
+unsigned long sdvo_get_list_attrs(sdvo_device_context_t *p_ctx,   pd_list_attr_t *p_attr);
+sdvo_status_t sdvo_query_list_attr(sdvo_device_context_t *p_ctx,  pd_list_attr_t *p_attr);
+sdvo_status_t sdvo_alter_list_attr(sdvo_device_context_t *p_ctx,  pd_list_attr_t *p_attr,
+	unsigned long new_value);
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_set_tv_optimal_settings(sdvo_device_context_t *p_ctx, unsigned char chron_tv_code);
+#endif
+
+/*	.......................................................................... */
+/*	Attribute Definitions */
+
+typedef struct {
+
+	unsigned long id;
+	char *p_name;
+	unsigned char num_bytes;
+	unsigned char opcode_max, opcode_get, opcode_set;
+
+} sdvo_range_attr_data_t;
+
+typedef struct {
+
+	unsigned long id;
+	char *p_name;
+	unsigned char bit_mask_cur, bit_mask_def;
+	unsigned char opcode_get, opcode_set;
+
+} sdvo_bool_attr_data_t;
+
+typedef struct {
+	unsigned long id;
+	char *p_name;
+	unsigned char reg_offset;
+	unsigned char value;
+} sdvo_attr_list_entry_t;
+
+typedef struct {
+	unsigned char num_entries;
+	unsigned long id;
+	char *p_name;
+	unsigned char opcode_get, opcode_set;
+	unsigned char bit_masks[8];
+	sdvo_attr_list_entry_t *attr_list;
+} sdvo_attr_list_header_t;
+
+#ifndef CONFIG_MICRO
+typedef struct {
+	unsigned char tv_code;
+	unsigned char hue;
+	unsigned char saturation;
+	unsigned char brightness;
+	unsigned char contrast;
+} sdvo_tv_optimal_settings_t;
+#endif
+
+#define PD_LVDS_COLOR_DEPTH_18	0
+#define PD_LVDS_COLOR_DEPTH_24	1
+
+#define PD_LVDS_CONNECTOR_SPWG		0
+#define PD_LVDS_CONNECTOR_OPEN_LDI	1
+
+#define PD_ANALOG_SRC_NO_DATA			0
+#define PD_ANALOG_SRC_PRE_RECORDED		2
+#define PD_ANALOG_SRC_NOT_PRE_RECORDED	3
+
+#define PD_SCAN_NO_DATA 		0
+#define PD_SCAN_OVERSCANNED 	1
+#define PD_SCAN_UNDERSCANNED	2
+
+#define PD_ASPECT_RATIO_NO_DATA 0
+#define PD_ASPECT_RATIO_4_3 	1
+#define PD_ASPECT_RATIO_16_9	2
+
+#define PD_ACTIVE_FORMAT_NO_DATA							0
+#define PD_ACTIVE_FORMAT_SQUARE_PIXELS						8
+#define PD_ACTIVE_FORMAT_4_3_CENTER 						9
+#define PD_ACTIVE_FORMAT_16_9_CENTER						10
+#define PD_ACTIVE_FORMAT_14_9_CENTER						11
+#define PD_ACTIVE_FORMAT_16_9_LETTERBOX_TOP 				2
+#define PD_ACTIVE_FORMAT_14_9_LETTERBOX_TOP 				3
+#define PD_ACTIVE_FORMAT_16_9_LETTERBOX_CENTER				4
+#define PD_ACTIVE_FORMAT_4_3_SHOOT_PROTECT_14_9_CENTER		13
+#define PD_ACTIVE_FORMAT_16_9_SHOOT_PROTECT_14_9_CENTER 	14
+#define PD_ACTIVE_FORMAT_16_9_SHOOT_PROTECT_4_3_CENTER		15
+
+#endif	/*	_SDVO_ATTR_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.c b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.c
new file mode 100644
index 0000000..81dd8f7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.c
@@ -0,0 +1,524 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_hdmi.c
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Port driver HDMI interface functions
+ *-----------------------------------------------------------------------------
+ */
+
+#include "sdvo_hdmi.h"
+
+#if 0
+ /*	HDMI Attributes
+	Currently if omitting out this attributes as this should NOT be avalable
+	to user
+	TODO: Integrate this attributes into HDMI  */
+/* HDMI attributes */
+#define PD_HDMI_COLORIMETRY_256_RGB					0
+#define PD_HDMI_COLORIMETRY_220_RGB					1
+#define PD_HDMI_COLORIMETRY_422_YCRCB				2
+#define PD_HDMI_COLORIMETRY_444_YCRCB				3
+
+#define PD_HDMI_PIXEL_REPLICATION_1X_REPITION				1
+#define PD_HDMI_PIXEL_REPLICATION_2X_REPITION				2
+#define PD_HDMI_PIXEL_REPLICATION_4X_REPITION				4
+/-----------------------------------------------------------------------------/
+ {PD_ATTR_ID_COLORIMETRY, MAKE_NAME("Colorimetry"), {4}, {0x0F},
+  GET_CURRENT_COLORIMETRY, SET_CURRENT_COLORIMETRY},
+
+  /* 256 RGB is passthrough, real value is 0x01 but 0x00 works as well
+	and upon boot up the value is 0x00 */
+ {PD_HDMI_COLORIMETRY_256_RGB,
+  MAKE_NAME("RGB 256 level"),							{0}, {0x00}, 0, 0},
+ {PD_HDMI_COLORIMETRY_220_RGB,
+  MAKE_NAME("RGB 220 level"),							{0}, {0x02}, 0, 0},
+ {PD_HDMI_COLORIMETRY_422_YCRCB,
+  MAKE_NAME("4:2:2 YcrCb"),							{0}, {0x04}, 0, 0},
+ {PD_HDMI_COLORIMETRY_444_YCRCB,
+  MAKE_NAME("4:4:4 YcrCb"),							{0}, {0x08}, 0, 0},
+
+ {PD_ATTR_ID_PIXEL_REPLICATION, MAKE_NAME("Pixel Replication"), {3}, {0x1F},
+  GET_PIXEL_REPLICATION, SET_PIXEL_REPLICATION},
+
+ {PD_HDMI_PIXEL_REPLICATION_1X_REPITION,
+  MAKE_NAME("No Repetition"),							{0}, {0x00}, 0, 0},
+ {PD_HDMI_PIXEL_REPLICATION_2X_REPITION,
+  MAKE_NAME("1X Repetition"),							{0}, {0x01}, 0, 0},
+ {PD_HDMI_PIXEL_REPLICATION_4X_REPITION,
+  MAKE_NAME("3X Repetition"),							{0}, {0x03}, 0, 0},
+#endif
+#if defined(SDVO_HDMI)
+
+/*	============================================================================
+	Function	:	sdvo_hdmi_transmitter
+
+	Parameters	:
+
+	Remarks 	:	Detect if transmitter is a HDMI transmitter.
+
+	Returns 	:
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_transmitter(sdvo_device_context_t *p_ctx)
+{
+	return sdvo_get_supported_encoding_modes(p_ctx,&(p_ctx->hdmi.version));
+}
+
+/*	============================================================================
+	Function	:	sdvo_hdmi_support is called to see if the transmitter
+					and the monitor device support HDMI.
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+								structure
+
+	Remarks 	:
+
+	Returns 	:	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_support(void *p_context)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status = SS_SUCCESS;
+
+	/* Monitor support is determined by valid CEA 861 extension and */
+	/* vendor block with 0xC03 IEEE regstration ID */
+	/*
+	if((p_ctx->p_callback->eld) == NULL){
+		return SS_NOT_SUPPORTED;
+	}
+
+	if((*p_ctx->p_callback->eld)->vendor_block_size) {
+		return SS_NOT_SUPPORTED;
+	}
+
+	if((*p_ctx->p_callback->eld)->vendor_ieee_id != CEA_IEEE_HDMI_ID) {
+		return SS_NOT_SUPPORTED;
+	}
+	*/
+	/* Transmitter support is determined by SDVO GetSupportDigitalEncodingModes */
+	status = sdvo_hdmi_transmitter(p_ctx);
+	if (status != SS_SUCCESS) {
+		return status;
+	}
+
+	return SS_SUCCESS;
+}
+
+
+
+/*	============================================================================
+	Function	:	sdvo_hdmi_audio_characteristic
+
+	Parameters	:
+
+	Remarks 	:	Set audio characteristic to be used in ELD calcilation.
+					TODO: Make sure this is recalled if hotplug is detected
+
+	Returns 	:
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_audio_characteristic(sdvo_device_context_t *p_ctx)
+{
+	sdvo_status_t status;
+	i2c_reg_t hdmi_audio_char[3];
+
+	/* Skip since no CEA data available */
+	if((!p_ctx->p_callback) || ((p_ctx->p_callback->eld) == NULL)){
+		return SS_NOT_SUPPORTED;
+	}
+	if(*(p_ctx->p_callback->eld) == NULL){
+		return SS_NOT_SUPPORTED;
+	}
+
+	status = sdvo_get_hdmi_audio_transmission_char(p_ctx, hdmi_audio_char);
+	if(status == SS_SUCCESS){
+		(*p_ctx->p_callback->eld)->audio_flag |= PD_AUDIO_CHAR_AVAIL;
+		(*p_ctx->p_callback->eld)->NPL = hdmi_audio_char[0];
+		(*p_ctx->p_callback->eld)->K0	= hdmi_audio_char[1];
+		(*p_ctx->p_callback->eld)->K1	= hdmi_audio_char[2];
+	}else{
+		(*p_ctx->p_callback->eld)->audio_flag &= ~PD_AUDIO_CHAR_AVAIL;
+	}
+
+	return SS_SUCCESS;
+}
+
+/*	============================================================================
+	Function	:	sdvo_hdmi_configure is called to program AVI infoframes, SPD
+					infoframes, ELD data for audio, colorimetry and pixel replication.
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+								structure
+					p_mode: Point to current video output display mode
+
+	Remarks 	:
+
+	Returns 	:	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_configure(sdvo_device_context_t *p_ctx)
+{
+	i2c_reg_t data;
+	sdvo_status_t status = SS_SUCCESS;
+	i2c_reg_t encoding_mode = DVI_MODE;
+
+	if((p_ctx->p_callback->eld) == NULL){
+		sdvo_set_digital_encoding_mode(p_ctx, encoding_mode);
+		return SS_NOT_SUPPORTED;
+	}
+	if(*(p_ctx->p_callback->eld) == NULL){
+		sdvo_set_digital_encoding_mode(p_ctx, encoding_mode);
+		return SS_NOT_SUPPORTED;
+	}
+
+	/* Prepare buffer for video information. Video only requires a total of 3
+	 * buffers. (0)ELD (1)AVI (2)SPD. SO we will set it to 2 indexbased buffer */
+	status = sdvo_execute_command(p_ctx, GET_HDMI_BUFFER_AUDIO_VIDEO_SPLIT,
+				0, NULL,
+				1, &data);
+	data = 2;
+	status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_AUDIO_VIDEO_SPLIT,
+				1, &data,
+				0, NULL);
+
+	/* Send pixel replication and colorimetry */
+	if (sdvo_hdmi_pr_and_color(p_ctx) != SS_SUCCESS) {
+		PD_ERROR("Fail toset pixel replication and colorimetry");
+	}
+
+	/* Build and send ELD Info Frames */
+	if(sdvo_hdmi_send_eld(p_ctx) != SS_SUCCESS){
+		PD_ERROR("Fail to write ELD to transmitter");
+	}
+
+	/* Build and send AVI Info Frames */
+	if (sdvo_hdmi_avi_info_frame(p_ctx) != SS_SUCCESS) {
+		PD_ERROR("Fail to write AVI infoframes to transmitter");
+	}
+
+	/* Build and send SPD Info Frames */
+#ifndef CONFIG_MICRO
+	if (sdvo_hdmi_spd_info_frame(p_ctx) != SS_SUCCESS) {
+		PD_ERROR("Fail to write SPD Infoframes to transmitter");
+	}
+#endif
+	/* Set to HDMI mode */
+	if((*p_ctx->p_callback->eld)->audio_support){
+		encoding_mode = HDMI_MODE;
+	}
+	status = sdvo_set_digital_encoding_mode(p_ctx, encoding_mode);
+	if (status != SS_SUCCESS) {
+		PD_DEBUG("sdvo: Fail to set HDMI mode ");
+	}
+
+	return SS_SUCCESS;
+}
+/*	============================================================================
+	Function	:	sdvo_hdmi_pr_and_color is called to send pixel replication
+					and colorimetry data to SDVO device.
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+								structure
+
+	Remarks 	:
+
+	Returns 	:	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_pr_and_color(sdvo_device_context_t *p_context)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status = SS_SUCCESS;
+	i2c_reg_t pixel_rep = (i2c_reg_t)(*p_ctx->p_callback->eld)->pixel_rep - 1;
+	i2c_reg_t quantization = (i2c_reg_t)(*p_ctx->p_callback->eld)->quantization;
+
+	/* Set pixel replication */
+	status = sdvo_execute_command(p_ctx, SET_PIXEL_REPLICATION,
+				1, &pixel_rep,
+				0, NULL);
+	if (status != SS_SUCCESS) {
+		return status;
+	}
+
+	/* Set colorimetry */
+	status = sdvo_execute_command(p_ctx, SET_CURRENT_COLORIMETRY,
+				1, &quantization,
+				0, NULL);
+	if (status != SS_SUCCESS) {
+		return status;
+	}
+	return status;
+};
+/*	============================================================================
+	Function	:	sdvo_hdmi_send_eld
+
+	Parameters	:
+
+	Remarks 	:	Builds eld structure and write it into SDVO ELD buffers
+
+	Returns 	:
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_send_eld(sdvo_device_context_t *p_ctx)
+{
+	sdvo_status_t status;
+	sdvo_audio_state_t audio_state;
+
+	if((p_ctx->p_callback->eld) == NULL){
+		return SS_NOT_SUPPORTED;
+	}
+	if(*(p_ctx->p_callback->eld) == NULL){
+		return SS_NOT_SUPPORTED;
+	}
+	if(!((*p_ctx->p_callback->eld)->audio_flag & ELD_AVAIL)){
+		PD_DEBUG("Eld not available");
+		return SS_NOT_SUPPORTED;
+	}
+
+	/* ELD 1.0, CEA version retrieve from callback */
+	(*p_ctx->p_callback->eld)->cea_ver = 0;
+	(*p_ctx->p_callback->eld)->eld_ver = 1;
+
+	/* Capability Flags */
+	(*p_ctx->p_callback->eld)->repeater = 0;
+	(*p_ctx->p_callback->eld)->hdcp = 0;
+#ifdef CONFIG_MICRO
+	(*p_ctx->p_callback->eld)->_44ms = 0;
+#endif
+
+	/* We do not provide Monitor length */
+	(*p_ctx->p_callback->eld)->mnl = 0;
+
+#ifdef CONFIG_MICRO
+	(*p_ctx->p_callback->eld)->sadc = 0;
+#endif
+
+	audio_state.value = 0;
+	audio_state.eld_valid = FALSE;
+	audio_state.presense_detect = TRUE;
+	status = sdvo_execute_command(p_ctx, SET_AUDIO_STATE,
+				1, (i2c_reg_t *)&audio_state,
+				0, NULL);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("Set Audio eld_valid  fail (FALSE)");
+		return status;
+	}
+
+	/* Write ELD to SDVO buffers */
+	status = sdvo_hdmi_write_buffer(p_ctx, SDVO_HDMI_ELD_BUFFER ,
+		0,  (*p_ctx->p_callback->eld)->eld_ptr, SDVO_HDMI_ELD_BUFFER_SIZE);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("Write ELD Failed !");
+		return status;
+	}
+
+	/* Send audio state change by toggling eld_valid bit */
+	audio_state.eld_valid = TRUE;
+	status = sdvo_execute_command(p_ctx, SET_AUDIO_STATE,
+				1, (i2c_reg_t *)&audio_state,
+				0, NULL);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("Set Audio eld_valid  fail (TRUE)");
+		return status;
+	}
+
+	return SS_SUCCESS;
+}
+/*	============================================================================
+	Function	:	sdvo_hdmi_avi_info_frame is called to program AVI infoframes
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+								structure
+					p_mode: Point to current video output display mode
+
+	Remarks 	:
+
+	Returns 	:	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_avi_info_frame(sdvo_device_context_t *p_context)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status = SS_SUCCESS;
+	unsigned long sum = 0;
+	int i;
+	sdvo_avi_info_t		avi;
+
+	/* Initialize AVI InfoFrame to no scan info, no bar info, no active format */
+	/* mode rgb, active format same as picture format */
+	/* no aspect ratio, no colorimetry, no scaling */
+	pd_memset(&avi, 0, sizeof(sdvo_avi_info_t));
+	avi.header.type = SDVO_HDMI_AVI_INFO_TYPE;
+	avi.header.version = 2;
+	avi.header.length = SDVO_HDMI_AVI_BUFFER_SIZE - sizeof(sdvo_info_header_t);
+
+	/* Fill in VIC, colorimetry, aspect ratio */
+
+	/* Set Video ID Code */
+	avi.video_id_code = (unsigned char)(*p_ctx->p_callback->eld)->video_code;
+
+	/* Set aspect ratio */
+	avi.pic_aspect_ratio = (unsigned char)(*p_ctx->p_callback->eld)->aspect_ratio;
+
+	/* Set Colorimetry */
+	avi.colorimetry = (unsigned char)(*p_ctx->p_callback->eld)->colorimetry;
+
+	/* Calc checksum */
+	for (i= 0; i < SDVO_HDMI_AVI_BUFFER_SIZE; i++) {
+		sum += avi.data[i];
+	}
+	avi.header.chksum = (unsigned char)(0x100 - (sum & 0xFF));
+
+	/* Send data to SDVO device */
+	status = sdvo_hdmi_write_buffer(p_ctx, SDVO_HDMI_AVI_BUFFER, 0, avi.data,
+		SDVO_HDMI_AVI_BUFFER_SIZE);
+	return status;
+};
+/*	============================================================================
+	Function	:	sdvo_hdmi_spd_info_frame is called to program SPD infoframes
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+								structure
+
+	Remarks 	:
+
+	Returns 	:	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_spd_info_frame(sdvo_device_context_t *p_context)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status = SS_SUCCESS;
+	sdvo_spd_info_t		spd;
+	unsigned long sum = 0;
+	int i;
+
+	/* Initialize SPD InfoFrame to zero */
+	pd_memset(&spd, 0, sizeof(sdvo_spd_info_t));
+	spd.header.type = SDVO_HDMI_SPD_INFO_TYPE;
+	spd.header.version = 1;
+	spd.header.length = SDVO_HDMI_SPD_BUFFER_SIZE - sizeof(sdvo_info_header_t);
+
+	pd_memcpy(spd.vendor_name, SDVO_HDMI_VENDOR_NAME, SDVO_HDMI_INTEL_VENDOR_NAME_SIZE);
+	pd_memcpy(spd.product_desc,
+		SDVO_HDMI_VENDOR_DESCRIPTION, SDVO_HDMI_IEGD_VENDOR_DESCRIPTION_SIZE);
+	spd.source_device = SDVO_HDMI_SPD_SOURCE_PC;
+
+	/* Calc checksum */
+	for (i= 0; i < SDVO_HDMI_SPD_BUFFER_SIZE; i++) {
+		sum += spd.data[i];
+	}
+	spd.header.chksum = (unsigned char)(0x100 - (sum & 0xFF));
+
+	/* Send data to SDVO device */
+	status = sdvo_hdmi_write_buffer
+		(p_ctx, SDVO_HDMI_SPD_BUFFER, 0, spd.data, SDVO_HDMI_SPD_BUFFER_SIZE);
+	return status;
+};
+/*	============================================================================
+	Function	:	sdvo_hdmi_write_buffer
+
+	Parameters	:
+
+	Remarks 	:	Write into SDVO buffers
+
+	Returns 	:
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_hdmi_write_buffer
+	(sdvo_device_context_t *p_ctx,unsigned char index,
+	unsigned char offset, unsigned char *input, int len)
+{
+	sdvo_status_t status;
+	i2c_reg_t hdmi_index[2],data[8];
+	int buffer_size = 0;
+	unsigned char total_bytes, *p_sdvo_buffer, *ori_p_sdvo_buffer;
+
+	/* Set Buffer index and offset */
+	hdmi_index[0] = index;
+	hdmi_index[1] = offset;
+	status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_INDEX,
+				2, hdmi_index,
+				0, NULL);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("HDMI wirte buffer fail : Set index");
+		return status;
+	}
+
+	/* Setup buffer transmit rate */
+	if(index != SDVO_HDMI_ELD_BUFFER){
+		data[0] = SDVO_HDMI_TRANSMIT_EVERY;
+		status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_TRANSMIT_RATE,
+					1, data,
+					0, NULL);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("HDMI wirte buffer fail : Transmite rate");
+			return status;
+		}
+	}
+	else {
+		status = sdvo_execute_command(p_ctx, GET_HDMI_BUFFER_TRANSMIT_RATE,
+					0, NULL,
+					1, data);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("HDMI wirte buffer fail : Transmite rate");
+			return status;
+		}
+	}
+
+	/* Get buffer size info */
+	status = sdvo_execute_command(p_ctx, GET_HDMI_BUFFER_INFO,
+			0, NULL,
+			1, (i2c_reg_t *)&data);
+	buffer_size = data[0] + 1;
+	/* Minimum buffer size is 3 then something is wrong */
+	if (status != SS_SUCCESS || buffer_size <0x03) {
+		PD_ERROR("HDMI wirte buffer fail : Buffer size");
+		return status;
+	}
+
+	/* Make sure to query size of buffer from SDVO and use it, not size of source infoframe buffers */
+	/* copy structs into cleared memory the size of SDVO buffer */
+	p_sdvo_buffer = pd_malloc(buffer_size);
+	ori_p_sdvo_buffer = p_sdvo_buffer;
+	pd_memset(p_sdvo_buffer, 0, buffer_size);
+	pd_memcpy(p_sdvo_buffer, input, (len < buffer_size) ? len : buffer_size);
+
+	while(buffer_size) {
+		//total_bytes = (buffer_size >= 8) ? 8:buffer_size;
+		if(buffer_size >= 8)	total_bytes = 8;
+		else					total_bytes = (unsigned char)buffer_size;
+
+		status = sdvo_execute_command(p_ctx, SET_HDMI_BUFFER_DATA,
+				total_bytes, (i2c_reg_t *)p_sdvo_buffer,
+				0, NULL);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("HDMI wirte buffer fail : Set buffer data");
+			return status;
+		}
+		p_sdvo_buffer +=total_bytes;
+		buffer_size -= total_bytes;
+	};
+
+	pd_free(ori_p_sdvo_buffer);
+	return SS_SUCCESS;
+}
+
+#endif/* SDVO_HDMI */
+
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.h b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.h
new file mode 100644
index 0000000..64c06c1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_hdmi.h
@@ -0,0 +1,182 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_hdmi.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Main include file for SDVO port driver HDMI interface function
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _SDVO_PORT_HDMI_H_
+#define _SDVO_PORT_HDMI_H_
+
+#include <config.h>
+#include <igd_pd.h>
+#include <pd.h>
+#include <pd_print.h>
+#include "sdvo_port.h"
+#include "sdvo_intf.h"
+
+/* VBios abstraction macro */
+#if !defined(CONFIG_MICRO)
+#define SDVO_HDMI
+#endif
+
+
+#if defined(SDVO_HDMI)
+
+#define SDVO_HDMI_ELD_BUFFER_SIZE			256
+#define SDVO_HDMI_AVI_BUFFER_SIZE			17
+#define SDVO_HDMI_SPD_BUFFER_SIZE			29
+#define	SDVO_HDMI_ELD_BUFFER				0			/* ELD buffer index */
+#define SDVO_HDMI_AVI_BUFFER				1			/* AVI buffer index */
+#define SDVO_HDMI_SPD_BUFFER				2			/* SPD buffer index */
+#define SDVO_HDMI_ELD_INFO_TYPE				0x00		/* ELD buffer type */
+#define SDVO_HDMI_AVI_INFO_TYPE				0x82		/* AVI buffer type */
+#define SDVO_HDMI_SPD_INFO_TYPE				0x83		/* SPD buffer type */
+#define SDVO_HDMI_AVI_BUFFER_SIZE			17
+#define SDVO_HDMI_TRANSMIT_DISABLE			0x00
+#define SDVO_HDMI_TRANSMIT_ONCE				0x80
+#define SDVO_HDMI_TRANSMIT_EVERY			0xC0
+
+#define SDVO_HDMI_VENDOR_NAME				"Intel"
+#define SDVO_HDMI_VENDOR_DESCRIPTION		"IEGD Driver"
+#define SDVO_HDMI_INTEL_VENDOR_NAME_SIZE		6
+#define SDVO_HDMI_IEGD_VENDOR_DESCRIPTION_SIZE	12
+
+#define SDVO_HDMI_VENDOR_NAME_SIZE			8
+#define SDVO_HDMI_VENDOR_DESCRIPTION_SIZE	16
+#define SDVO_HDMI_SPD_SOURCE_PC				0x09
+
+#define DVI_MODE	0
+#define HDMI_MODE	1
+sdvo_status_t sdvo_hdmi_transmitter(sdvo_device_context_t *p_context);
+sdvo_status_t sdvo_hdmi_support(void *p_context);
+sdvo_status_t sdvo_hdmi_audio_characteristic(sdvo_device_context_t *p_context);
+sdvo_status_t sdvo_hdmi_configure(sdvo_device_context_t *p_ctx);
+sdvo_status_t sdvo_hdmi_pr_and_color(sdvo_device_context_t *p_context);
+sdvo_status_t sdvo_hdmi_send_eld(sdvo_device_context_t *p_ctx);
+sdvo_status_t sdvo_hdmi_avi_info_frame(sdvo_device_context_t *p_context);
+sdvo_status_t sdvo_hdmi_spd_info_frame(sdvo_device_context_t *p_context);
+sdvo_status_t sdvo_hdmi_write_buffer
+	(sdvo_device_context_t *p_ctx,unsigned char index,
+	unsigned char offset, unsigned char *input, int len);
+
+typedef union
+{
+	unsigned char value;
+	struct {
+		unsigned char eld_valid: 1;
+		unsigned char presense_detect: 1;
+		unsigned char cp_ready: 1;
+		unsigned char reserved: 5;
+	};
+} sdvo_audio_state_t;
+/* AVI Info Frames version 2 */
+typedef struct
+{
+	unsigned char type;
+	unsigned char version;
+	unsigned char length;
+	unsigned char chksum;
+} sdvo_info_header_t;
+
+typedef union
+{
+	unsigned char data[SDVO_HDMI_AVI_BUFFER_SIZE];
+	struct {
+		sdvo_info_header_t	header;
+		union {
+			unsigned char data1;
+			struct {
+				unsigned char scan_info: 2;
+				unsigned char bar_info: 2;
+				unsigned char active_format: 1;
+				unsigned char color_space: 2;
+				unsigned char reserved1: 1;
+			};
+		};
+		union {
+			unsigned char data2;
+			struct {
+				unsigned char format_aspect_ratio: 4;
+				unsigned char pic_aspect_ratio: 2;
+				unsigned char colorimetry: 2;
+			};
+		};
+		union {
+			unsigned char data3;
+			struct {
+				unsigned char pic_scaling: 2;
+				unsigned char reserved2: 6;
+			};
+		};
+		union {
+			unsigned char data4;
+			struct {
+				unsigned char video_id_code: 7;
+				unsigned char reserved3: 1;
+			};
+		};
+		union {
+			unsigned char data5;
+			struct {
+				unsigned char pixel_rep: 4;
+				unsigned char reserved4: 4;
+			};
+		};
+		unsigned char bar1;
+		unsigned char bar2;
+		unsigned char bar3;
+		unsigned char bar4;
+		unsigned char bar5;
+		unsigned char bar6;
+		unsigned char bar7;
+		unsigned char bar8;
+	};
+} sdvo_avi_info_t;
+
+typedef union
+{
+	unsigned char data[SDVO_HDMI_SPD_BUFFER_SIZE];
+	struct {
+		sdvo_info_header_t	header;
+		unsigned char vendor_name[SDVO_HDMI_VENDOR_NAME_SIZE];
+		unsigned char product_desc[SDVO_HDMI_VENDOR_DESCRIPTION_SIZE];
+		unsigned char source_device;
+	};
+} sdvo_spd_info_t;
+
+
+#define SDVO_INIT_HDMI(a)			sdvo_hdmi_audio_characteristic(a)
+#define SDVO_IS_HDMI_SUPPORTED(a)	sdvo_hdmi_support(a)
+#define SDVO_CONFIGURE_HDMI(a)		sdvo_hdmi_configure(a)
+#else
+#define SDVO_INIT_HDMI(a)
+#define SDVO_IS_HDMI_SUPPORTED(a)		SS_NOT_SUPPORTED
+#define SDVO_CONFIGURE_HDMI(a)
+#endif  /* SDVO_HDMI */
+
+#endif  /*  _SDVO_PORT_HDMI_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.c b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.c
new file mode 100644
index 0000000..e2fb741
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.c
@@ -0,0 +1,725 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_intf.c
+ * $Revision: 1.12 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Functions that provide interface to SDVO device
+ *-----------------------------------------------------------------------------
+ */
+
+#include "sdvo_port.h"
+
+/* .......................................................................... */
+void sdvo_convert_parm_to_dtd(i2c_reg_t parm[16], sdvo_dtd_t *p_dtd);
+void sdvo_convert_dtd_to_parm(
+	sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd,
+	i2c_reg_t parm[16]);
+
+
+/* .......................................................................... */
+/* .......................................................................... */
+/*	============================================================================
+	Function	:	sdvo_execute_command transfers a command and argument to
+					the SDVO device and returns the status of execution
+
+	Parameters	:	opcode		: One of the sdvo_opcode_t opcodes
+					num_args 	: Number of arguments 0-8
+					p_arg	    : List of arguments
+					num_returns	: Expected number of returns 0-8
+					p_ret_value	: List of return values
+
+	Remarks 	:	sdvo_execute_command is core function that is called by other
+					SDVO interface functions to execute a command.
+					The arguments of the command are transferred to the argument
+					I2C registers of the SDVO device
+					The opcode is then transferred to the opcode I2C register
+					It then waits for the command to complete by reading the
+					status I2C register up to 3 times
+
+	Returns 	:	sdvo_status_t : Status of command execution
+	------------------------------------------------------------------------- */
+sdvo_status_t sdvo_execute_command(sdvo_device_context_t *p_ctx,
+	sdvo_opcode_t opcode,
+	i2c_reg_t num_args,	   i2c_reg_t *p_arg,
+	i2c_reg_t num_returns, i2c_reg_t *p_ret_value)
+{
+	i2c_reg_t status, i;
+
+	/*	..................................................................... */
+	/*	Error checking */
+	if ((num_args > SDVO_MAX_ARGS) || (num_returns > SDVO_MAX_RETURNS)) {
+
+		return SS_UNSUCCESSFUL;
+	}
+
+	if ((num_args > 0) && (p_arg == NULL)) {
+
+		return SS_UNSUCCESSFUL;
+	}
+
+	if ((num_returns > 0) && (p_ret_value == NULL)) {
+
+		return SS_UNSUCCESSFUL;
+	}
+
+	/*	..................................................................... */
+	/*	Write the arguments and the opcode */
+	for (i = 0; i < num_args; i++) {
+
+		if (! sdvo_write_i2c_reg(p_ctx, (i2c_reg_t)(SDVO_REG_ARG_START - i),
+				p_arg[i])) {
+
+			return SS_WRITE_FAILED;
+		}
+	}
+
+	if (! sdvo_write_i2c_reg(p_ctx, (i2c_reg_t)SDVO_REG_OPCODE,
+			(i2c_reg_t)opcode)) {
+
+		return SS_WRITE_FAILED;
+	}
+
+	/*	..................................................................... */
+	/*	Wait for command to complete */
+	for (i = 0; i < SDVO_MAX_RETRIES; i++) {
+
+		if (! sdvo_read_i2c_reg(p_ctx, SDVO_REG_STATUS, &status)) {
+
+			return SS_READ_FAILED;
+		}
+
+		if (status != SS_PENDING) {
+
+			break;
+		}
+	}
+
+	/*	..................................................................... */
+	/*	Read the return parameters if command succeeded */
+	if (status == SS_SUCCESS) {
+
+		for (i = 0; i < num_returns; i++) {
+
+			if (! sdvo_read_i2c_reg(p_ctx, SDVO_REG_RETURN_START + i,
+					&p_ret_value[i])) {
+
+				 return SS_READ_FAILED;
+			}
+		}
+	}
+
+	return status;
+}
+
+sdvo_status_t sdvo_execute_command_read (sdvo_device_context_t *p_ctx,
+	i2c_reg_t num_returns, i2c_reg_t *p_ret_value)
+{
+	i2c_reg_t status, i;
+
+	for (i = 0; i < SDVO_MAX_RETRIES; i++) {
+		if (!sdvo_read_i2c_reg(p_ctx, SDVO_REG_STATUS, &status)) {
+			return SS_READ_FAILED;
+		}
+
+		if (status != SS_PENDING) {
+			break;
+		}
+	}
+
+	/*	Read the return parameters if command succeeded */
+	if (status == SS_SUCCESS) {
+		for (i = 0; i < num_returns; i++) {
+			if (! sdvo_read_i2c_reg(p_ctx, SDVO_REG_RETURN_START + i,
+					&p_ret_value[i])) {
+				 return SS_READ_FAILED;
+			}
+		}
+	}
+	return status;
+}
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_reset(sdvo_device_context_t *p_ctx)
+{
+	return sdvo_execute_command(p_ctx, RESET,
+							   0, NULL,
+							   0, NULL);
+}
+#endif
+
+sdvo_status_t sdvo_get_device_capabilities(sdvo_device_context_t *p_ctx,
+	sdvo_device_capabilities_t *p_Dev_Cap)
+{
+	return sdvo_execute_command(p_ctx, GET_DEVICE_CAPABILITIES,
+							    0, NULL,
+							    8, (i2c_reg_t *)p_Dev_Cap);
+}
+
+
+sdvo_status_t sdvo_get_trained_inputs(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_Out)
+{
+	return sdvo_execute_command(p_ctx, GET_TRAINED_INPUTS,
+							   0, NULL,
+							   1, p_Out);
+}
+
+
+sdvo_status_t sdvo_get_active_outputs(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t *p_Out_Flags)
+{
+	return sdvo_execute_command(p_ctx, GET_ACTIVE_OUTPUTS,
+				0, NULL,
+				2, (i2c_reg_t *)p_Out_Flags);
+}
+
+
+sdvo_status_t sdvo_set_active_outputs(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags)
+{
+	return sdvo_execute_command(p_ctx, SET_ACTIVE_OUTPUTS,
+			   2, (i2c_reg_t *)&out_flags,
+			   0, NULL);
+}
+
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_get_in_out_map(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags[2])
+{
+	return sdvo_execute_command(p_ctx, GET_IN_OUT_MAP,
+				0, NULL,
+				4, (i2c_reg_t *)out_flags);
+}
+#endif
+
+sdvo_status_t sdvo_set_in_out_map(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags[2])
+{
+	return sdvo_execute_command(p_ctx, SET_IN_OUT_MAP,
+				4, (i2c_reg_t *)out_flags,
+				0, NULL);
+}
+
+
+sdvo_status_t sdvo_get_attached_displays(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t *p_Out_Flags)
+{
+	i2c_reg_t status, i;
+
+	status = sdvo_execute_command(p_ctx, GET_ATTACHED_DISPLAYS,
+				0, NULL,
+				2, (i2c_reg_t *)p_Out_Flags);
+
+
+	PD_DEBUG("sdvo: GET_ATTACHED_DISPLAYS status = %d]",  status);
+	for (i=0; i<5; i++) {
+		if (status == SS_SUCCESS)
+			break;
+		else {
+			pd_ui_usleep(50000); /* wait for 50ms */
+			status = sdvo_execute_command_read(p_ctx, 2, (i2c_reg_t *)p_Out_Flags);
+		}
+	}
+
+	PD_DEBUG("sdvo: sdvo_execute_command_read- no. of tries = [%d]",  i);
+	return status;
+}
+
+
+sdvo_status_t sdvo_set_target_input(sdvo_device_context_t *p_ctx,
+	sdvo_target_input_t input)
+{
+	i2c_reg_t arg_value = input == SDVO0 ? 0 : 1;
+
+	return sdvo_execute_command(p_ctx, SET_TARGET_INPUT,
+				1, (i2c_reg_t *)&arg_value,
+				0, NULL);
+}
+
+
+sdvo_status_t sdvo_set_target_output(sdvo_device_context_t *p_ctx,
+		sdvo_output_flags_t out_flags)
+{
+	return sdvo_execute_command(p_ctx, SET_TARGET_OUTUT,
+			   2, (i2c_reg_t *)&out_flags,
+			   0, NULL);
+}
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_get_input_timings(sdvo_device_context_t *p_ctx,
+								 sdvo_dtd_t *p_dtd)
+{
+	sdvo_status_t status;
+	i2c_reg_t ret[16];
+
+	status = sdvo_execute_command(p_ctx, GET_INPUT_TIMINGS_PART1,
+				0, NULL,
+				8, ret);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	status = sdvo_execute_command(p_ctx, GET_INPUT_TIMINGS_PART2,
+				 0, NULL,
+				 8, &ret[8]);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	sdvo_convert_parm_to_dtd(ret, p_dtd);
+
+	return status;
+}
+#endif
+
+sdvo_status_t sdvo_set_input_timings(sdvo_device_context_t *p_ctx,
+								 sdvo_dtd_t *p_dtd)
+{
+	sdvo_status_t status;
+	i2c_reg_t parm[16];
+
+	sdvo_convert_dtd_to_parm(p_ctx, p_dtd, parm);
+
+	status = sdvo_execute_command(p_ctx, SET_INPUT_TIMINGS_PART1,
+				 8, parm,
+				 0, NULL);
+
+	if ((status != SS_SUCCESS) && (status != SS_NOT_SUPPORTED)) {
+
+		return status;
+	}
+
+	status = sdvo_execute_command(p_ctx, SET_INPUT_TIMINGS_PART2,
+				 8, &parm[8],
+				 0, NULL);
+
+	return status;
+}
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_get_output_timings(sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd)
+{
+	sdvo_status_t status;
+	i2c_reg_t ret[16];
+
+	status = sdvo_execute_command(p_ctx, GET_OUTPUT_TIMINGS_PART1,
+				 0, NULL,
+				 8, ret);
+	if ((status != SS_SUCCESS) && (status != SS_NOT_SUPPORTED)) {
+
+		return status;
+	}
+
+	status = sdvo_execute_command(p_ctx, GET_OUTPUT_TIMINGS_PART2,
+				 0, NULL,
+				 8, &ret[8]);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	sdvo_convert_parm_to_dtd(ret, p_dtd);
+
+	return status;
+}
+#endif
+
+sdvo_status_t sdvo_set_output_timings(sdvo_device_context_t *p_ctx,
+  sdvo_dtd_t *p_dtd)
+{
+	sdvo_status_t status;
+	i2c_reg_t parm[16];
+
+	sdvo_convert_dtd_to_parm(p_ctx, p_dtd, parm);
+
+	status = sdvo_execute_command(p_ctx, SET_OUTPUT_TIMINGS_PART1,
+				 8, parm,
+				 0, NULL);
+
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	status = sdvo_execute_command(p_ctx, SET_OUTPUT_TIMINGS_PART2,
+				 8, &parm[8],
+				 0, NULL);
+
+	return status;
+}
+
+
+sdvo_status_t sdvo_get_preferred_input_timings(sdvo_device_context_t *p_ctx,
+				  sdvo_create_preferred_timings_t *p_Timings,
+				  sdvo_dtd_t *p_dtd)
+{
+	sdvo_status_t status;
+	i2c_reg_t ret[16];
+
+	/*	..................................................................... */
+	status = sdvo_execute_command(p_ctx, CREATE_PREFERRED_INPUT_TIMINGS,
+				 7, (i2c_reg_t *)p_Timings,
+				 0, NULL);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	/*	..................................................................... */
+	status =  sdvo_execute_command(p_ctx, GET_PREFERRED_INPUT_TIMINGS_PART1,
+				  0, NULL,
+				  8, ret);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	status = sdvo_execute_command(p_ctx, GET_PREFERRED_INPUT_TIMINGS_PART2,
+				 0, NULL,
+				 8, &ret[8]);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	/*	..................................................................... */
+	sdvo_convert_parm_to_dtd(ret, p_dtd);
+
+	return status;
+}
+
+
+/*	in_clock[0] = min, in_clock[1] = max */
+sdvo_status_t sdvo_get_input_pixel_clock_range(sdvo_device_context_t *p_ctx,
+	unsigned short in_clock[2])
+{
+	return sdvo_execute_command(p_ctx, GET_INPUT_PIXEL_CLOCK_RANGE,
+			   0, NULL,
+			   4, (i2c_reg_t *)in_clock);
+}
+
+#ifndef CONFIG_MICRO
+/*	out_Clock[0] = min, out_Clock[1] = max */
+sdvo_status_t sdvo_get_output_pixel_clock_range(sdvo_device_context_t *p_ctx,
+				  unsigned short out_Clock[2])
+{
+	return sdvo_execute_command(p_ctx, GET_OUTPUT_PIXEL_CLOCK_RANGE,
+			   0, NULL,
+			   4, (i2c_reg_t *)out_Clock);
+}
+
+sdvo_status_t sdvo_get_clock_rate_multiplier(sdvo_device_context_t *p_ctx,
+	sdvo_clock_rate_mult_t *p_mult)
+{
+	*p_mult = 0;
+
+	return sdvo_execute_command(p_ctx, GET_SDVO_CLOCK_RATE_MULTIPLIER,
+			   0, NULL,
+			   1, (i2c_reg_t *)p_mult);
+}
+#endif
+
+
+sdvo_status_t sdvo_set_clock_rate_multiplier(sdvo_device_context_t *p_ctx,
+	sdvo_clock_rate_mult_t mult)
+{
+	return sdvo_execute_command(p_ctx, SET_SDVO_CLOCK_RATE_MULTIPLIER,
+			   1, (i2c_reg_t *)&mult,
+			   0, NULL);
+}
+
+
+sdvo_status_t sdvo_get_supported_power_states(sdvo_device_context_t *p_ctx,
+	 i2c_reg_t *p_pwr_state)
+{
+	return sdvo_execute_command(p_ctx, GET_SUPPORTED_POWER_STATES,
+			   0, NULL,
+			   1, p_pwr_state);
+}
+
+
+sdvo_status_t sdvo_get_power_state(sdvo_device_context_t *p_ctx,
+	   i2c_reg_t *p_pwr_state)
+{
+	return sdvo_execute_command(p_ctx, GET_POWER_STATE,
+			   0, NULL,
+			   1, p_pwr_state);
+}
+
+
+sdvo_status_t sdvo_set_power_state(sdvo_device_context_t *p_ctx,
+   i2c_reg_t pwr_state)
+{
+	return sdvo_execute_command(p_ctx, SET_POWER_STATE,
+			   1, &pwr_state,
+			   0, NULL);
+}
+
+
+sdvo_status_t sdvo_set_display_power_state(sdvo_device_context_t *p_ctx,
+  i2c_reg_t disp_pwr_state)
+{
+	return sdvo_execute_command(p_ctx, SET_DISPLAY_POWER_STATE,
+			   1, &disp_pwr_state,
+			   0, NULL);
+}
+#if !defined(CONFIG_MICRO)
+sdvo_status_t sdvo_set_digital_encoding_mode(sdvo_device_context_t *p_ctx,
+  i2c_reg_t digital_encoding_mode)
+{
+	return sdvo_execute_command(p_ctx, SET_DIGITAL_ENCODING_MODE,
+			   1, &digital_encoding_mode,
+			   0, NULL);
+}
+sdvo_status_t sdvo_get_hdmi_audio_transmission_char(sdvo_device_context_t *p_ctx,
+  i2c_reg_t *hdmi_reg)
+{
+	return sdvo_execute_command(p_ctx, GET_HDMI_AUDIO_TRANSMISSION_CHARACTERISTICS,
+			   0, NULL,
+			   3, (i2c_reg_t*)hdmi_reg);
+}
+sdvo_status_t sdvo_get_supported_encoding_modes(sdvo_device_context_t *p_ctx,
+  sdvo_digital_encode_modes_t *digital_encoding_modes)
+{
+	return sdvo_execute_command(p_ctx, GET_SUPPORTED_DIGITAL_ENCODING_MODES,
+			   0, NULL,
+			   8, (i2c_reg_t*)digital_encoding_modes);
+}
+
+sdvo_status_t sdvo_get_digital_encoding_mode(sdvo_device_context_t *p_ctx,
+  i2c_reg_t *digital_encoding_mode)
+{
+	return sdvo_execute_command(p_ctx, GET_DIGITAL_ENCODING_MODE,
+			   0, NULL,
+			   1, (i2c_reg_t*)digital_encoding_mode);
+}
+/* ------------------------------------------------------------------------- */
+#endif
+sdvo_status_t sdvo_set_upscalar_coefficient(sdvo_device_context_t *p_ctx,
+  i2c_reg_t upscalar_coefficient)
+{
+	return sdvo_execute_command(p_ctx, SET_UPSCALAR_COEFFICIENTS,
+			   1, &upscalar_coefficient,
+			   0, NULL);
+}
+
+sdvo_status_t sdvo_get_tv_output_format(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_TV_Output_Format_Flag)
+{
+	return sdvo_execute_command(p_ctx, GET_TV_OUTPUT_FORMAT,
+				0, NULL,
+				6, p_TV_Output_Format_Flag);
+}
+
+sdvo_status_t sdvo_get_sdtv_resolution_support(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_Requested_SDTV_Format_Flag,
+	i2c_reg_t *p_Resolution_Support_Flags)
+{
+	return sdvo_execute_command(p_ctx, GET_SDTV_RESOLUTION_SUPPORT,
+				3, p_Requested_SDTV_Format_Flag,
+				3, p_Resolution_Support_Flags);
+}
+
+sdvo_status_t sdvo_get_scaled_hdtv_resolution_support(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_Requested_HDTV_Format_Flag,
+	i2c_reg_t *p_Resolution_Support_Flags)
+{
+	return sdvo_execute_command(p_ctx, GET_SCALED_HDTV_RESOLUTION_SUPPORT,
+				4, p_Requested_HDTV_Format_Flag,
+				8, p_Resolution_Support_Flags);
+}
+
+void sdvo_convert_parm_to_dtd(i2c_reg_t parm[16], sdvo_dtd_t *p_dtd)
+{
+	pd_memset(p_dtd, 0, sizeof(sdvo_dtd_t));
+
+	p_dtd->pixel_clock			= (parm[1] << 8) | parm[0];
+	p_dtd->horz_active			= ((parm[4] & 0xF0) << 4) | parm[2];
+	p_dtd->horz_blanking		= ((parm[4] & 0x0F) << 8) | parm[3];
+	p_dtd->vert_active			= ((parm[7] & 0xF0) << 4) | parm[5];
+	p_dtd->vert_blanking		= ((parm[7] & 0x0F) << 8) | parm[6];
+	p_dtd->horz_sync_offset		= ((parm[11] & 0xC0) << 2) | parm[8];
+	p_dtd->horz_sync_pulse_width= ((parm[11] & 0x30) << 4) | parm[9];
+	p_dtd->vert_sync_offset		= (parm[14] & 0xC0) | ((parm[11] & 0x0C) << 2) |
+								  ((parm[10] & 0xF0) >> 4);
+	p_dtd->vert_sync_pulse_width= ((parm[11] & 0x03) << 4) | (parm[10] & 0x0F);
+	p_dtd->dtd_flags			= parm[12];
+	p_dtd->sdvo_flags			= parm[13];
+}
+
+
+void sdvo_convert_dtd_to_parm(
+	sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd,
+	i2c_reg_t parm[16])
+{
+	unsigned int multp;
+
+	parm[0]  = (p_dtd->pixel_clock & 0xFF);
+	parm[1]  = ((p_dtd->pixel_clock & 0xFF00) >> 8);
+
+	if (p_ctx->st_sdvo) {
+		if (p_dtd->pixel_clock > 10000) {			/*	100-200 MHz */
+			multp = 1;
+		} else if (p_dtd->pixel_clock > 5000) {     /*	50-100 Mhz */
+			multp = 2;
+		} else {							        /*	25-50 Mhz */
+			multp = 4;
+		}
+
+		parm[2]  = ((p_dtd->horz_active * multp) & 0xFF);
+		parm[3]  = ((p_dtd->horz_blanking * multp) & 0xFF);
+		parm[4]  = ((((p_dtd->horz_active * multp) & 0xF00) >> 4) |
+					(((p_dtd->horz_blanking * multp) & 0xF00) >> 8));
+	} else {
+		parm[2]  = (p_dtd->horz_active & 0xFF);
+		parm[3]  = (p_dtd->horz_blanking & 0xFF);
+		parm[4]  = ((p_dtd->horz_active & 0xF00) >> 4) |
+					((p_dtd->horz_blanking & 0xF00) >> 8);
+	}
+	parm[5]  = (p_dtd->vert_active & 0xFF);
+	parm[6]  = (p_dtd->vert_blanking & 0xFF);
+	parm[7]  = ((p_dtd->vert_active & 0xF00) >> 4) |
+				((p_dtd->vert_blanking & 0xF00) >> 8);
+	parm[8]  = (p_dtd->horz_sync_offset & 0xFF);
+	parm[9]  = (p_dtd->horz_sync_pulse_width & 0xFF);
+	parm[10] = ((p_dtd->vert_sync_offset & 0x0F) << 4) |
+				(p_dtd->vert_sync_pulse_width & 0x0F);
+	parm[11] = ((p_dtd->horz_sync_offset & 0x300) >> 2) |
+			   ((p_dtd->horz_sync_pulse_width & 0x300) >> 4) |
+			   ((p_dtd->vert_sync_offset & 0x30) >> 2) |
+			   ((p_dtd->vert_sync_pulse_width & 0x30) >> 4);
+	parm[12] = p_dtd->dtd_flags;
+	parm[13] = p_dtd->sdvo_flags;
+	parm[14] = p_dtd->vert_sync_offset & 0xC0;
+}
+
+#ifndef CONFIG_MICRO
+sdvo_status_t sdvo_get_min_max_pixel_clock(sdvo_device_context_t *p_ctx,
+	unsigned long *p_min_clock,
+	unsigned long *p_max_clock)
+{
+	sdvo_status_t status;
+	sdvo_clock_rate_mult_t clock_mult;
+	unsigned short in_range[2], out_range[2];
+
+	status = sdvo_get_clock_rate_multiplier(p_ctx, &clock_mult);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	status = sdvo_get_input_pixel_clock_range(p_ctx, in_range);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	status = sdvo_get_output_pixel_clock_range(p_ctx, out_range);
+	if (status != SS_SUCCESS) {
+
+		return status;
+	}
+
+	switch (clock_mult)
+	{
+		case CRM_1X :
+			break;
+
+		case CRM_2X :
+			in_range[0] *= 2;
+			break;
+
+		case CRM_4X :
+			in_range[0] *= 4;
+			break;
+
+		default :
+			return SS_INVALID_RETURN;
+	}
+
+	/*	Minimum pixel clock supported is the maximum of min input/output */
+	/*  pixel clock */
+	*p_min_clock = in_range[0] > out_range[0] ? in_range[0] : out_range[0];
+
+	/*	Maximum pixel clock supported is minimum of max pixel clock of */
+	/*	encoder and SDVO bus clock rate */
+	*p_max_clock = in_range[1] < out_range[1] ? in_range[1] : out_range[1];
+
+	return SS_SUCCESS;
+}
+#endif
+
+unsigned char sdvo_write_i2c_reg(sdvo_device_context_t *p_ctx, i2c_reg_t offset,
+	i2c_reg_t value)
+{
+	pd_reg_t reg_list[2];
+
+/*	PD_DEBUG("sdvo : W : 0x%02x, 0x%02x", offset, value); */
+
+	reg_list[0].reg = offset;
+	reg_list[0].value = value;
+
+	reg_list[1].reg = PD_REG_LIST_END;
+
+	if (p_ctx->p_callback->write_regs(p_ctx->p_callback->callback_context, reg_list,
+			PD_REG_I2C)) {
+
+		return FALSE;
+
+	}else {
+
+		return TRUE;
+	}
+}
+
+
+unsigned char sdvo_read_i2c_reg(sdvo_device_context_t *p_ctx, unsigned char offset,
+						i2c_reg_t *p_Value)
+{
+	pd_reg_t reg_list[2];
+
+	reg_list[0].reg = offset;
+	reg_list[1].reg = PD_REG_LIST_END;
+
+	if (p_ctx->p_callback->read_regs(p_ctx->p_callback->callback_context, reg_list,
+								   PD_REG_I2C)) {
+
+		return FALSE;
+
+	} else {
+
+		*p_Value = (i2c_reg_t)reg_list[0].value;
+
+/*		PD_DEBUG("sdvo : R : 0x%02x, 0x%02x", offset, *p_Value); */
+
+		return TRUE;
+	}
+}
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.h b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.h
new file mode 100644
index 0000000..e861af4
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_intf.h
@@ -0,0 +1,482 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_intf.h
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Definitions for SDVO interface
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _SDVO_INTF_H_
+#define _SDVO_INTF_H_
+
+/*  ......................................................................... */
+/*  Opcode, Status Code, Register definitions */
+
+typedef enum {
+    /*  General */
+    RESET = 0x1,
+    GET_DEVICE_CAPABILITIES,
+    GET_TRAINED_INPUTS,
+    GET_ACTIVE_OUTPUTS,
+    SET_ACTIVE_OUTPUTS,
+
+    /*  Picture Routing  */
+    GET_IN_OUT_MAP = 0x6,
+    SET_IN_OUT_MAP,
+
+    GET_ATTACHED_DISPLAYS = 0x0B,
+
+    /* Hot Plug */
+    GET_HOT_PLUG_SUPPORT = 0x0C,
+    SET_ACTIVE_HOT_PLUG,
+    GET_ACTIVE_HOT_PLUG,
+    GET_INTERRUPT_EVENT_SOURCE,
+
+    /*  Input/Output Timings */
+    SET_TARGET_INPUT = 0x10,
+    SET_TARGET_OUTUT,
+    GET_INPUT_TIMINGS_PART1,
+    GET_INPUT_TIMINGS_PART2,
+    SET_INPUT_TIMINGS_PART1,
+    SET_INPUT_TIMINGS_PART2,
+    SET_OUTPUT_TIMINGS_PART1,
+    SET_OUTPUT_TIMINGS_PART2,
+    GET_OUTPUT_TIMINGS_PART1,
+    GET_OUTPUT_TIMINGS_PART2,
+    CREATE_PREFERRED_INPUT_TIMINGS,
+    GET_PREFERRED_INPUT_TIMINGS_PART1,
+    GET_PREFERRED_INPUT_TIMINGS_PART2,
+    GET_INPUT_PIXEL_CLOCK_RANGE,
+    GET_OUTPUT_PIXEL_CLOCK_RANGE,
+    GET_SUPPORTED_SDVO_CLOCK_RATE_MULTIPLIERS,
+    GET_SDVO_CLOCK_RATE_MULTIPLIER,
+    SET_SDVO_CLOCK_RATE_MULTIPLIER,
+
+    GET_SUPPORTED_TV_OUTPUT_FORMATS = 0x27,
+    GET_TV_OUTPUT_FORMAT,
+    SET_TV_OUTPUT_FORMAT,
+
+    /*  Power Management */
+    GET_SUPPORTED_POWER_STATES = 0x2A,
+    GET_POWER_STATE,
+    SET_POWER_STATE,
+
+    /*  Panel Power Sequencing */
+    GET_MAX_PANEL_POWER_SEQUENCING_PARAMETER = 0x2D,
+    GET_PANEL_POWER_SEQUENCING_PARAMETER,
+    SET_PANEL_POWER_SEQUENCING_PARAMETER,
+
+    GET_MAX_BACKLIGHT_LEVEL,
+    GET_BACKLIGHT_LEVEL,
+    SET_BACKLIGHT_LEVEL,
+
+    GET_AMBIENT_LIGHT,
+    SET_AMBIENT_LIGHT,
+
+    SET_UPSCALAR_COEFFICIENTS = 0x36,
+
+    GET_SSC,
+    SET_SSC,
+
+    /*  Content Protection */
+    GET_ANCILLARY_VIDEO_INFORMATION,
+    SET_ANCILLARY_VIDEO_INFORMATION,
+
+    /*  Picture Enhancements */
+    GET_MAX_FLICKER_FILTER = 0x4D,
+    GET_FLICKER_FILTER,
+    SET_FLICKER_FILTER,
+
+    GET_ADAPTIVE_FLICKER_FILTER,
+    SET_ADAPTIVE_FLICKER_FILTER,
+
+    GET_MAX_2D_FLICKER_FILTER,
+    GET_2D_FLICKER_FILTER,
+    SET_2D_FLICKER_FILTER,
+
+    GET_MAX_SATURATION,
+    GET_SATURATION,
+    SET_SATURATION,
+
+    GET_MAX_HUE,
+    GET_HUE,
+    SET_HUE,
+
+    GET_MAX_BRIGHTNESS,
+    GET_BRIGHTNESS,
+    SET_BRIGHTNESS,
+
+    GET_MAX_CONTRAST,
+    GET_CONTRAST,
+    SET_CONTRAST,
+
+    GET_MAX_HORIZONTAL_OVERSCAN,
+    GET_HORIZONTAL_OVERSCAN,
+    SET_HORIZONTAL_OVERSCAN,
+
+    GET_MAX_VERTICAL_OVERSCAN,
+    GET_VERTICAL_OVERSCAN,
+    SET_VERTICAL_OVERSCAN,
+
+    GET_MAX_HORIZONTAL_POSITION,
+    GET_HORIZONTAL_POSITION,
+    SET_HORIZONTAL_POSITION,
+
+    GET_MAX_VERTICAL_POSITION,
+    GET_VERTICAL_POSITION,
+    SET_VERTICAL_POSITION,
+
+    GET_MAX_SHARPNESS,
+    GET_SHARPNESS,
+    SET_SHARPNESS,
+
+    GET_DOT_CRAWL,
+    SET_DOT_CRAWL,
+
+    GET_DITHER,
+    SET_DITHER,
+
+    GET_MAX_TV_CHROMA_FILTER,
+    GET_TV_CHROMA_FILTER,
+    SET_TV_CHROMA_FILTER,
+
+    GET_MAX_TV_LUMA_FILTER,
+    GET_TV_LUMA_FILTER,
+    SET_TV_LUMA_FILTER,
+
+    /*  Control Bus Switch */
+    SET_CONTROL_BUS_SWITCH = 0x7A,
+
+    /*  Picture Enhancements */
+    GET_MAX_ADAPTIVE_FLICKER_FILTER = 0x7B,
+
+    /*  Power Management */
+    SET_DISPLAY_POWER_STATE = 0x7D,
+
+    /*  Panel Power Sequencing */
+    GET_MAX_SSC = 0x7E,
+    GET_LVDS_PANEL_INFORMATION,
+    SET_LVDS_PANEL_INFORMATION,
+    GET_LVDS_PANEL_PROTECTION,
+    SET_LVDS_PANEL_PROTECTION,
+
+	GET_SDTV_RESOLUTION_SUPPORT = 0x83,
+	GET_SUPPORTED_ENHANCEMENTS,
+	GET_SCALED_HDTV_RESOLUTION_SUPPORT,
+
+	/*  General */
+	GET_FIRMWARE_VERSION = 0x86,
+	DETECT_LEGACY_VGA = 0x87,
+
+	/* HDMI */
+	PREPARE_TO_GENERATE_HDCPAN = 0x8A,
+	SET_PIXEL_REPLICATION = 0x8B,
+	GET_PIXEL_REPLICATION,
+	GET_COLORIMETRY_CAPABILITIES,
+	SET_CURRENT_COLORIMETRY,
+	GET_CURRENT_COLORIMETRY,
+	GET_AUDIO_ENCRYPTION_PREFERENCE,
+	SET_AUDIO_STATE,
+	GET_AUDIO_STATE,
+	SET_HDMI_BUFFER_INDEX,
+	GET_HDMI_BUFFER_INDEX,
+	GET_HDMI_BUFFER_INFO,
+	SET_HDMI_BUFFER_AUDIO_VIDEO_SPLIT,
+	GET_HDMI_BUFFER_AUDIO_VIDEO_SPLIT,
+	SET_HDMI_BUFFER_DATA,
+	GET_HDMI_BUFFER_DATA,
+	SET_HDMI_BUFFER_TRANSMIT_RATE,
+	GET_HDMI_BUFFER_TRANSMIT_RATE,
+	GET_HDMI_AUDIO_TRANSMISSION_CHARACTERISTICS,
+	GET_SUPPORTED_DIGITAL_ENCODING_MODES, /* 0x9D, */
+	GET_DIGITAL_ENCODING_MODE,
+	SET_DIGITAL_ENCODING_MODE,
+} sdvo_opcode_t;
+
+typedef enum {
+    SS_POWER_ON_STATE,       /*  00: */
+    SS_SUCCESS,              /*  01: Command successfully completed */
+    SS_NOT_SUPPORTED,        /*  02: Invalid Opcode */
+    SS_INVALID_ARGUMENT,     /*  03: Opcode is valid but arguments are not */
+    SS_PENDING,              /*  04: Opcode is not completed yet */
+    SS_TARGET_UNSPECIFIED,   /*  05: SetTargetInput/Output not called */
+    SS_SCALING_UNSUPPORTED,  /*  06: Device does not support scaling */
+
+    /*  New Errors */
+    SS_UNSUCCESSFUL = 10,    /*  10: General Failure */
+    SS_INVALID_RETURN,       /*  11: Unexpected return value */
+    SS_WRITE_FAILED,         /*  12: Write Operation failed */
+    SS_READ_FAILED,          /*  13: Read Operation failed */
+} sdvo_status_t;
+
+#define SDVO_MAX_ARGS           8
+#define SDVO_MAX_RETURNS        8
+
+#define SDVO_REG_ARG_START      0x07
+#define SDVO_REG_ARG_END        0x00
+#define SDVO_REG_OPCODE         0x08
+#define SDVO_REG_STATUS         0x09
+#define SDVO_REG_RETURN_START   0x0A
+#define SDVO_REG_RETURN_END     0x11
+
+#define SDVO_MAX_RETRIES        3
+
+/*  ........................................................................ */
+/*  Structure definitions for Commands */
+
+typedef unsigned char i2c_reg_t;
+
+/* SDVOOutputFlagStructure Byte 0 */
+typedef struct {
+    i2c_reg_t tmds  : 1;
+    i2c_reg_t rgb   : 1;
+    i2c_reg_t cvbs  : 1;
+    i2c_reg_t svid  : 1;
+    i2c_reg_t yprpb : 1;
+    i2c_reg_t scart : 1;
+    i2c_reg_t lvds  : 1;
+    i2c_reg_t drgb  : 1;
+} sdvo_display_output_t;
+
+/* Display masks */
+#define FP_DISP_MASK (BIT(0)|BIT(6)|BIT(7)|BIT(8)|BIT(14)) /*TMDS/LVDS/DRGB*/
+#define TV_DISP_MASK (BIT(2)|BIT(3)|BIT(4)|BIT(5)) /*CVBS0/SVID0/YPRPB0/SCART0*/
+#define CRT_EXT_DISP_MASK (BIT(1)|BIT(9))   /* External CRT */
+#define LVDS_DISP_MASK    (BIT(6)|BIT(14))  /* LVDS */
+#define TMDS_DISP_MASK (BIT(0)|BIT(8))
+#define TV_SVIDEO_DISP_MASK (BIT(3))   /* S-VIDEO */
+#define TV_YPBPR_DISP_MASK (BIT(4))    /* YPBPR/Yipper */
+#define DRGB_DISP_MASK  (BIT(7))       /* DRGB0 */
+typedef struct {
+	union {
+		struct {
+			sdvo_display_output_t out0;
+			sdvo_display_output_t out1;
+		};
+		unsigned short flags;
+	};
+} sdvo_output_flags_t;
+
+typedef struct {
+    i2c_reg_t vendor_id;         /* 00: Vendor ID */
+    i2c_reg_t device_id;         /* 01: Device ID */
+    i2c_reg_t revision_id;       /* 02: Revision ID */
+    i2c_reg_t version_minor;     /*  03 : sDVO Minor Version (00) */
+    i2c_reg_t version_major;     /*  04 : sDVO Major Version (01) */
+                                 /*  05 : Device capabilities */
+    i2c_reg_t num_inputs    :2;  /*  10 : Number of sDVO inputs (1 or 2) */
+    i2c_reg_t smooth_scaling:1;  /*   2 : Smooth (Graphics) Scaling */
+    i2c_reg_t sharp_scaling :1;  /*   3 : Sharp (Text) scaling */
+    i2c_reg_t up_scaling    :1;  /*   4 : Up Scaling */
+    i2c_reg_t down_scaling  :1;  /*   5 : Down Scaling */
+    i2c_reg_t stall         :1;  /*   6 : Stall */
+    i2c_reg_t reserved      :1;  /*   7 : Reserved */
+    sdvo_output_flags_t output;  /* 06&07: Ouput supported by sDVO device */
+} sdvo_device_capabilities_t;
+
+typedef struct {
+    unsigned short pixel_clock;         /*  Pixel clock / 10,000 */
+    unsigned short active_horz_pixels;  /*  12 bits of horizontal active */
+    unsigned short active_vert_pixels;  /*  12 bits of vertical active */
+    unsigned char  b_interlaced :1;     /*  1:Interlaced, 0: Non-Interlaced */
+    unsigned char  b_scaled     :1;     /*  1:Image to be scaled, 0:Otherwise*/
+    unsigned char  resv_bits    :6;     /*  Reserved bits */
+    unsigned char  resv_byte;
+} sdvo_create_preferred_timings_t;
+
+typedef enum {
+    CRM_1X = BIT(0),
+    CRM_2X = BIT(1),
+    CRM_4X = BIT(3),
+} sdvo_clock_rate_mult_t;
+
+/*  Detailed Timing Descriptor */
+typedef struct {
+    unsigned short pixel_clock;      /*  Pixel Clock / 10,000 */
+    unsigned short horz_active;      /*  Horizontal Active */
+    unsigned short horz_blanking;    /*  Horizontal Blanking */
+    unsigned short vert_active;      /*  Vertical Active */
+    unsigned short vert_blanking;    /*  Vertical Blanking */
+    unsigned short horz_sync_offset;  /*  Pixels from blanking start */
+    unsigned short horz_sync_pulse_width;
+    unsigned char  vert_sync_offset;
+    unsigned char  vert_sync_pulse_width;
+    unsigned char  dtd_flags;
+    unsigned char  sdvo_flags;
+} sdvo_dtd_t;
+
+typedef enum {
+    SDVO0,
+    SDVO1,
+} sdvo_target_input_t;
+
+typedef struct {
+    i2c_reg_t dev_D0 :1; /*  SDVO On */
+    i2c_reg_t dev_D1 :1;
+    i2c_reg_t dev_D2 :1;
+    i2c_reg_t dev_D3 :1; /*  SDVO Off */
+    i2c_reg_t disp_D0:1; /*  Display On      : Vsync/Hsync/Video Active */
+    i2c_reg_t disp_D1:1; /*  Display Standby : Vsync Active, Hsync/Video Off */
+    i2c_reg_t disp_D2:1; /*  Display Suspend : Hsync Active, Vsync/Video Off */
+    i2c_reg_t disp_D3:1; /*  Display Off     : All signals Off */
+} sdvo_power_states_t;
+
+typedef struct {
+	struct{
+		i2c_reg_t minor_DVI_rev :4;
+		i2c_reg_t major_DVI_rev :4;
+	};
+	struct{
+		i2c_reg_t minor_HDMI_rev:4;
+		i2c_reg_t major_HDMI_rev:4;
+	};
+
+    i2c_reg_t reserved[6]; 		/* reserved for future use */
+} sdvo_digital_encode_modes_t;
+
+typedef struct{
+	sdvo_digital_encode_modes_t	version;
+}sdvo_hdmi_context_t;
+
+
+typedef struct _sdvo_device_context {
+    sdvo_target_input_t        inp_dev;
+    sdvo_output_flags_t        out_type;
+	sdvo_device_capabilities_t dev_cap;
+    pd_callback_t             *p_callback;
+	pd_attr_t                 *p_attr_table;
+	unsigned long              num_attrs;
+	pd_timing_t               *p_mode_table;
+	pd_timing_t               *native_dtd;
+	unsigned short             fp_width;
+	unsigned short             fp_height;
+	unsigned char              supp_pwr_states;
+	unsigned char              device_pwr_state, display_pwr_state;
+	unsigned char              up_scaling;
+	unsigned short             t1;   /* power state delays */
+	unsigned short             t2;   /* power state delays */
+	unsigned short             t3;   /* power state delays */
+	unsigned short             t4;
+	unsigned short             t5;
+	unsigned short             text_tune;
+	sdvo_hdmi_context_t		   hdmi;
+	unsigned short             st_sdvo;
+} sdvo_device_context_t;
+
+typedef struct sdvo_state {
+	unsigned long power_state;
+	pd_timing_t timing;
+} sdvo_state_t;
+
+/*  ......................................................................... */
+sdvo_status_t sdvo_execute_command(sdvo_device_context_t *p_ctx,
+	sdvo_opcode_t opcode,
+	i2c_reg_t num_args, i2c_reg_t *p_arg,
+	i2c_reg_t num_returns, i2c_reg_t *p_ret_value);
+
+sdvo_status_t sdvo_reset(sdvo_device_context_t *p_ctx);
+sdvo_status_t sdvo_get_device_capabilities(sdvo_device_context_t *p_ctx,
+	sdvo_device_capabilities_t *p_Dev_Cap);
+sdvo_status_t sdvo_get_trained_inputs(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_Out);
+sdvo_status_t sdvo_get_active_outputs(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t *p_Out_Flags);
+sdvo_status_t sdvo_set_active_outputs(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags);
+
+sdvo_status_t sdvo_get_in_out_map(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags[2]);
+sdvo_status_t sdvo_set_in_out_map(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags[2]);
+sdvo_status_t sdvo_set_upscalar_coefficient(sdvo_device_context_t *p_ctx,
+	i2c_reg_t upscalar_coefficient);
+
+sdvo_status_t sdvo_get_attached_displays(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t *p_Out_Flags);
+
+sdvo_status_t sdvo_set_target_input(sdvo_device_context_t *p_ctx,
+	sdvo_target_input_t input);
+sdvo_status_t sdvo_set_target_output(sdvo_device_context_t *p_ctx,
+	sdvo_output_flags_t out_flags);
+
+sdvo_status_t sdvo_get_input_timings(sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd);
+sdvo_status_t sdvo_set_input_timings(sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd);
+
+sdvo_status_t sdvo_get_output_timings(sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd);
+sdvo_status_t sdvo_set_output_timings(sdvo_device_context_t *p_ctx,
+	sdvo_dtd_t *p_dtd);
+
+sdvo_status_t sdvo_get_input_pixel_clock_range(sdvo_device_context_t *p_ctx,
+	unsigned short p_Pixel[2]);
+
+sdvo_status_t sdvo_get_preferred_input_timings(sdvo_device_context_t *p_ctx,
+	sdvo_create_preferred_timings_t *p_Timings,
+	sdvo_dtd_t *p_dtd);
+sdvo_status_t sdvo_get_clock_rate_multiplier(sdvo_device_context_t *p_ctx,
+	sdvo_clock_rate_mult_t *p_mult);
+sdvo_status_t sdvo_set_clock_rate_multiplier(sdvo_device_context_t *p_ctx,
+	sdvo_clock_rate_mult_t mult);
+
+sdvo_status_t sdvo_get_supported_power_states(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_pwr_state);
+sdvo_status_t sdvo_get_power_state(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_Power_State);
+sdvo_status_t sdvo_set_power_state(sdvo_device_context_t *p_ctx,
+	i2c_reg_t pwr_state);
+sdvo_status_t sdvo_set_display_power_state(sdvo_device_context_t *p_ctx,
+	i2c_reg_t display_state);
+#if !defined(CONFIG_MICRO) /* These are not used in vBIOS */
+sdvo_status_t sdvo_set_digital_encoding_mode(sdvo_device_context_t *p_ctx,
+	i2c_reg_t digital_encoding_mode);
+sdvo_status_t sdvo_get_hdmi_audio_transmission_char(
+	sdvo_device_context_t *p_ctx, i2c_reg_t *hdmi_reg);
+sdvo_status_t sdvo_get_supported_encoding_modes(sdvo_device_context_t *p_ctx,
+    sdvo_digital_encode_modes_t *digital_encoding_modes);
+sdvo_status_t sdvo_get_digital_encoding_mode(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *digital_encoding_mode);
+/* ------------------------------------------------------------------------- */
+#endif
+sdvo_status_t sdvo_get_min_max_pixel_clock(sdvo_device_context_t *p_ctx,
+	unsigned long *p_min_clock, unsigned long *p_max_clock);
+
+sdvo_status_t sdvo_get_tv_output_format(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_TV_Output_Format_Flag);
+
+sdvo_status_t sdvo_get_sdtv_resolution_support(sdvo_device_context_t *p_ctx,
+	i2c_reg_t *p_Requested_SDTV_Format_Flag, i2c_reg_t *p_Resolution_Support_Flags);
+
+sdvo_status_t sdvo_get_scaled_hdtv_resolution_support(
+	sdvo_device_context_t *p_ctx, i2c_reg_t *p_Requested_HDTV_Format_Flag,
+	i2c_reg_t *p_Resolution_Support_Flags);
+
+sdvo_status_t sdvo_execute_command_read(sdvo_device_context_t *p_ctx,
+	i2c_reg_t num_returns, i2c_reg_t *p_ret_value);
+
+#endif  /*  _SDVO_INTF_H_ */
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.c b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.c
new file mode 100644
index 0000000..7651199
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.c
@@ -0,0 +1,3898 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_port.c
+ * $Revision: 1.31 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Port driver interface functions
+ *-----------------------------------------------------------------------------
+ */
+
+
+#include <linux/kernel.h>
+
+
+#include "sdvo_port.h"
+
+#define PFIT_CONTROL		0x61230  /* Panel Fitting Control Register Address*/
+#define PFIT_PIPEB_ENABLED	0xA0000000
+#define CH7308_VENDOR_ID	2
+#define CH7308_DEVICE_ID	0x41
+#define CH7308_REVISION_ID	1
+
+/* .......................................................................... */
+
+int sdvo_open(pd_callback_t *p_callback, void **p_context);
+int sdvo_init_device(void *p_context);
+int sdvo_get_timing_list(void *p_context, pd_timing_t *p_in_list,
+	pd_timing_t **pp_out_list);
+int sdvo_set_mode(void *p_context, pd_timing_t *p_mode, unsigned long flags);
+int sdvo_post_set_mode(void *p_context, pd_timing_t *p_mode,
+	unsigned long flags);
+int sdvo_get_attributes(void *p_context, unsigned long *p_num_attr,
+	pd_attr_t **pp_list);
+int sdvo_set_attributes(void *p_context, unsigned long num_attr,
+	pd_attr_t *p_list);
+unsigned long sdvo_validate(unsigned long cookie);
+int sdvo_close(void *p_context);
+int sdvo_set_power(void *p_context, unsigned long state);
+int sdvo_get_power(void *p_context, unsigned long *p_state);
+int sdvo_save(void *p_context, void **pp_state, unsigned long flags);
+int sdvo_restore(void *p_context, void *p_state, unsigned long flags);
+int sdvo_get_port_status(void *p_context, pd_port_status_t *port_status);
+#ifndef CONFIG_MICRO
+int sdvo_set_tv_settings(void *p_context, i2c_reg_t tv_format);
+#endif
+char *sdvo_get_output_type(sdvo_display_output_t out, unsigned char byte);
+int sdvo_init_attribute_table(sdvo_device_context_t *p_ctx);
+void sdvo_convert_pd_timing_to_dtd(pd_timing_t *p_mode, sdvo_dtd_t *p_dtd,
+	unsigned long dclk);
+void sdvo_convert_dtd_to_pd_timing(sdvo_dtd_t *p_dtd, pd_timing_t *p_mode);
+
+#ifndef CONFIG_MICRO
+static sdvo_status_t sdvo_reset_encoder(sdvo_device_context_t *p_ctx);
+#endif
+
+static pd_version_t  g_sdvo_version = {3, 4, 0, 0};
+static unsigned long g_sdvo_dab_list[] = {0x70, 0x72, PD_DAB_LIST_END};
+
+static pd_driver_t	 g_sdvo_drv = {
+	PD_SDK_VERSION,
+	"SDVO Port Driver",
+	0,
+	&g_sdvo_version,
+	PD_DISPLAY_FP,
+	PD_FLAG_CLOCK_MASTER,
+	g_sdvo_dab_list,
+	1000,
+	sdvo_validate,
+	sdvo_open,
+	sdvo_init_device,
+	sdvo_close,
+	sdvo_set_mode,
+	sdvo_post_set_mode,
+	sdvo_set_attributes,
+	sdvo_get_attributes,
+	sdvo_get_timing_list,
+	sdvo_set_power,
+	sdvo_get_power,
+	sdvo_save,
+	sdvo_restore,
+	sdvo_get_port_status
+};
+
+pd_timing_t sdvo_sdtv_timing_table[] =
+{
+#ifndef CONFIG_MICRO
+#if 0
+	/* byte 0 */
+	{
+		320, 200,              /* width, height */
+		60, 19140,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		319, 1286,             /* hblank_start, hblank_end (will be set) */
+		561, 593,              /* hsync_start, hsync_end (will be set) */
+		249,                   /* vtotal */
+		199, 249,              /* vblank_start, vblank_end (will be set) */
+		216, 218,              /* vsync_start, vsync_end (will be set) */
+		648,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		320, 240,              /* width, height */
+		60, 22970,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		319, 1286,             /* hblank_start, hblank_end (will be set) */
+		561, 593,              /* hsync_start, hsync_end (will be set) */
+		299,                   /* vtotal */
+		239, 299,              /* vblank_start, vblank_end (will be set) */
+		260, 262,              /* vsync_start, vsync_end (will be set) */
+		658,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		400, 300,              /* width, height */
+		60, 28330,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		399, 1286,             /* hblank_start, hblank_end (will be set) */
+		621, 653,              /* hsync_start, hsync_end (will be set) */
+		369,                   /* vtotal */
+		299, 369,              /* vblank_start, vblank_end (will be set) */
+		322, 324,              /* vsync_start, vsync_end (will be set) */
+		662,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		640, 350,              /* width, height */
+		60, 32930,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		639, 1286,             /* hblank_start, hblank_end (will be set) */
+		801, 833,              /* hsync_start, hsync_end (will be set) */
+		429,                   /* vtotal */
+		349, 429,              /* vblank_start, vblank_end (will be set) */
+		376, 378,              /* vsync_start, vsync_end (will be set) */
+		768,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		640, 400,              /* width, height */
+		60, 37520,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		639, 1286,             /* hblank_start, hblank_end (will be set) */
+		801, 833,              /* hsync_start, hsync_end (will be set) */
+		489,                   /* vtotal */
+		399, 489,              /* vblank_start, vblank_end (will be set) */
+		429, 431,              /* vsync_start, vsync_end (will be set) */
+		772,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#else
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                   /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                   /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+#endif
+	{
+		640, 480,              /* width, height */
+		60, 45180,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		639, 1286,             /* hblank_start, hblank_end (will be set) */
+		801, 833,              /* hsync_start, hsync_end (will be set) */
+		589,                   /* vtotal */
+		479, 589,              /* vblank_start, vblank_end (will be set) */
+		516, 518,              /* vsync_start, vsync_end (will be set) */
+		599,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#ifndef CONFIG_MICRO
+#if 0
+	{
+		704, 480,              /* width, height */
+		60, 45180,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		703, 1286,             /* hblank_start, hblank_end (will be set) */
+		849, 881,              /* hsync_start, hsync_end (will be set) */
+		589,                   /* vtotal */
+		479, 589,              /* vblank_start, vblank_end (will be set) */
+		516, 518,              /* vsync_start, vsync_end (will be set) */
+		776,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		704, 576,              /* width, height */
+		60, 54370,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		703, 1286,             /* hblank_start, hblank_end (will be set) */
+		849, 881,              /* hsync_start, hsync_end (will be set) */
+		709,                   /* vtotal */
+		575, 709,              /* vblank_start, vblank_end (will be set) */
+		620, 622,              /* vsync_start, vsync_end (will be set) */
+		786,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 1 */
+	{
+		720, 350,              /* width, height */
+		60, 32930,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		719, 1286,             /* hblank_start, hblank_end (will be set) */
+		861, 893,              /* hsync_start, hsync_end (will be set) */
+		429,                   /* vtotal */
+		349, 429,              /* vblank_start, vblank_end (will be set) */
+		376, 378,              /* vsync_start, vsync_end (will be set) */
+		790,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		720, 400,              /* width, height */
+		60, 37520,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		719, 1286,             /* hblank_start, hblank_end (will be set) */
+		861, 893,              /* hsync_start, hsync_end (will be set) */
+		489,                   /* vtotal */
+		399, 489,              /* vblank_start, vblank_end (will be set) */
+		429, 431,              /* vsync_start, vsync_end (will be set) */
+		800,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#else
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+
+	{
+		720, 480,              /* width, height */
+		60, 45180,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		719, 1286,             /* hblank_start, hblank_end (will be set) */
+		861, 893,              /* hsync_start, hsync_end (will be set) */
+		589,                   /* vtotal */
+		479, 589,              /* vblank_start, vblank_end (will be set) */
+		516, 518,              /* vsync_start, vsync_end (will be set) */
+		804,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+#if 0
+	{
+		720, 540,              /* width, height */
+		60, 51310,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		719, 1286,             /* hblank_start, hblank_end (will be set) */
+		861, 893,              /* hsync_start, hsync_end (will be set) */
+		669,                   /* vtotal */
+		539, 669,              /* vblank_start, vblank_end (will be set) */
+		583, 585,              /* vsync_start, vsync_end (will be set) */
+		808,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#else
+	{
+		0, 0,                  /* width, height */
+		0, 0,                  /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+
+	{
+		720, 576,              /* width, height */
+		60, 36250,             /* refresh (will be set), dot clock */
+		857,                   /* htotal */
+		719, 857,              /* hblank_start, hblank_end (will be set) */
+		754, 786,              /* hsync_start, hsync_end (will be set) */
+		709,                   /* vtotal */
+		575, 709,              /* vblank_start, vblank_end (will be set) */
+		620, 622,              /* vsync_start, vsync_end (will be set) */
+		818,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+	    0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+#if 0
+	{
+		768, 576,              /* width, height */
+		60, 54370,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		767, 1286,             /* hblank_start, hblank_end (will be set) */
+		897, 929,              /* hsync_start, hsync_end (will be set) */
+		709,                   /* vtotal */
+		575, 709,              /* vblank_start, vblank_end (will be set) */
+		620, 622,              /* vsync_start, vsync_end (will be set) */
+		822,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#else
+	{
+		0, 0,                  /* width, height */
+		0, 0,                  /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+#endif
+	{
+		800, 600,              /* width, height */
+		60, 56670,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		799, 1286,             /* hblank_start, hblank_end (will be set) */
+		921, 953,              /* hsync_start, hsync_end (will be set) */
+		739,                   /* vtotal */
+		599, 739,              /* vblank_start, vblank_end (will be set) */
+		646, 648,              /* vsync_start, vsync_end (will be set) */
+		601,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#ifndef CONFIG_MICRO
+#if 0
+	{
+		832, 624,              /* width, height */
+		60, 58970,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		831, 1286,             /* hblank_start, hblank_end (will be set) */
+		945, 977,              /* hsync_start, hsync_end (will be set) */
+		769,                   /* vtotal */
+		623, 769,              /* vblank_start, vblank_end (will be set) */
+		672, 674,              /* vsync_start, vsync_end (will be set) */
+		832,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 2 */
+	{
+		920, 766,              /* width, height */
+		60, 71990,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		919, 1286,             /* hblank_start, hblank_end (will be set) */
+		1011, 1043,            /* hsync_start, hsync_end (will be set) */
+		939,                   /* vtotal */
+		765, 939,              /* vblank_start, vblank_end (will be set) */
+		823, 825,              /* vsync_start, vsync_end (will be set) */
+		836,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#else
+	{
+		0, 0,                  /* width, height */
+		0, 0,                  /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,                  /* width, height */
+		0, 0,                  /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+
+	{
+		1024, 768,             /* width, height */
+		60, 72750,             /* refresh (will be set), dot clock */
+		1286,                  /* htotal */
+		1023, 1286,            /* hblank_start, hblank_end (will be set) */
+		1089, 1121,            /* hsync_start, hsync_end (will be set) */
+		949,                   /* vtotal */
+		767, 949,              /* vblank_start, vblank_end (will be set) */
+		828, 830,              /* vsync_start, vsync_end (will be set) */
+		609,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1280, 1024,            /* width, height */
+		60, 128660,            /* refresh (will be set), dot clock */
+		1715,                  /* htotal */
+		1279, 1715,            /* hblank_start, hblank_end (will be set) */
+		1388, 1420,            /* hsync_start, hsync_end (will be set) */
+		1259,                  /* vtotal */
+		1023, 1259,            /* vblank_start, vblank_end (will be set) */
+		1086, 1088,            /* vsync_start, vsync_end (will be set) */
+		611,                   /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+	PD_TIMING_TABLE_END
+};
+pd_timing_t sdvo_hdtv_timing_table[] =
+{
+	/* byte 0 */
+	{
+		640, 480,              /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+
+	{
+		800, 600,              /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#ifndef CONFIG_MICRO
+	{
+		1024, 768,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1280, 960,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1400, 1050,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1600, 1200,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1920, 1440,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		2048, 1536,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 1 */
+	{
+		2560, 1920,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL ,                 /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 2 */
+	{
+		848, 480,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1064, 600,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1280, 720,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1360, 768,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1704, 960,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1864, 1050,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1920, 1080,            /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		2128, 1200,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 3 */
+	{
+		2560, 1400,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		2728, 1536,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,             /* width, height */
+		0, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 4 */
+	{
+		768, 480,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		960, 600,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1152, 720,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1124, 768,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1536, 960,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1680, 1050,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1728, 1080,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		1920, 1200,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 5 */
+	{
+		2304, 1440,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		2456, 1536,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 6 */
+	{
+		1280, 1024,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					 /* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+
+	/* byte 7 */
+	{
+		1280, 768,             /* width, height */
+		60, 0,                 /* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		PD_MODE_SUPPORTED|     /* this mode is supported */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+	{
+		0, 0,					/* width, height */
+		0, 0,					/* refresh (will be set), dot clock */
+		0,                     /* htotal */
+		0, 0,                  /* hblank_start, hblank_end (will be set) */
+		0, 0,                  /* hsync_start, hsync_end (will be set) */
+		0,                     /* vtotal */
+		0, 0,                  /* vblank_start, vblank_end (will be set) */
+		0, 0,                  /* vsync_start, vsync_end (will be set) */
+		0,                     /* mode number */
+		0,                     /* mode info flags */
+		0, 0,                  /* x, y offset */
+		NULL,                  /* mode extenstion pointer (will bet set) */
+		0, 0				   /* Reserved */
+	},
+#endif
+	PD_TIMING_TABLE_END
+};
+
+/* .......................................................................... */
+/* .......................................................................... */
+/*============================================================================
+	Function	:	pd_init is the first function that is invoked by IEG driver.
+
+	Parameters	:	handle : not used
+
+	Remarks     :   pd_init initializes pd_driver_t structure and registers the
+					port driver with IEG driver by calling pd_register function
+
+	Returns     :	Status returned by pd_register function.
+	------------------------------------------------------------------------- */
+int PD_MODULE_INIT(sdvo_init, (void *handle))
+{
+	int status;
+
+	PD_DEBUG("sdvo: pd_init()");
+
+	status = pd_register(handle, &g_sdvo_drv);
+	if (status != PD_SUCCESS) {
+		PD_DEBUG("sdvo: Error ! pd_init: pd_register() failed with "
+				  "status=%#x", status);
+	}
+	return status;
+}
+
+
+/*----------------------------------------------------------------------
+ * Function: sdvo_exit()
+ *
+ * Description: This is the exit function for SDVO port driver to unload
+ *              the driver.
+ *
+ * Parameters:  None.
+ *
+ * Return:      PD_SUCCESS(0)  success
+ *              PD_ERR_XXXXXX  otherwise
+ *----------------------------------------------------------------------*/
+int PD_MODULE_EXIT(sdvo_exit, (void))
+{
+	return (PD_SUCCESS);
+} /* end sdvo_exit() */
+
+
+/*	============================================================================
+	Function	:	sdvo_open is called for each combination of port and dab
+					registers to detect the SDVO device.
+
+	Parameters	:	p_callback : Contains pointers to read_regs/write_regs
+								functions to access I2C registes.
+
+					pp_context	  : Pointer to port driver allocated context
+								structure is returned in this argument
+
+	Remarks     :	sdvo_open detects the presence of SDVO device for specified
+					port.
+
+	Returns     :	PD_SUCCESS If SDVO device is detected
+					PD_ERR_xxx On Failure
+	------------------------------------------------------------------------- */
+int sdvo_open(pd_callback_t *p_callback, void **pp_context)
+{
+	sdvo_device_context_t *p_ctx;
+	sdvo_status_t status;
+	sdvo_output_flags_t output_flags;
+	unsigned long power_state;
+#ifndef CONFIG_MICRO
+	i2c_reg_t reg;
+#endif
+
+	PD_DEBUG("sdvo: sdvo_open(): port_num=%ld", p_callback->port_num);
+
+	p_ctx = pd_malloc(sizeof(sdvo_device_context_t));
+	if (p_ctx == NULL) {
+		PD_ERROR("sdvo: Error ! sdvo_open: pd_malloc() failed");
+		return PD_ERR_NOMEM;
+	}
+
+	pd_memset(p_ctx, 0, sizeof(sdvo_device_context_t));
+
+	*pp_context = p_ctx;
+	p_ctx->p_callback = p_callback;
+
+#ifndef CONFIG_MICRO
+	/* reset context to avoid wrong timing list */
+	if ((p_ctx->dev_cap.vendor_id != VENDOR_ID_OKI)
+		&& (p_ctx->dev_cap.device_id != 0x1)){
+		sdvo_reset_encoder(p_ctx);
+	}
+#endif
+
+	status = sdvo_get_device_capabilities(p_ctx, &p_ctx->dev_cap);
+	if (status != SS_SUCCESS) {
+
+		PD_DEBUG("sdvo: Error ! sdvo_open: sdvo_get_device_capabilities() "
+				  "failed with status=%d",  status);
+		pd_free(p_ctx);
+		return PD_ERR_NODEV;
+	}
+
+	PD_DEBUG("sdvo: Device Capabilities:");
+	PD_DEBUG("sdvo: vendor=%d, device=%d, revision=%d,",
+			  p_ctx->dev_cap.vendor_id, p_ctx->dev_cap.device_id,
+			  p_ctx->dev_cap.revision_id);
+	PD_DEBUG("sdvo: version_major=%d, version_minor=%d, num sDVO inputs=%d",
+			  p_ctx->dev_cap.version_major, p_ctx->dev_cap.version_minor,
+			  p_ctx->dev_cap.num_inputs);
+	PD_DEBUG("sdvo: Scaling support: smooth=%d, sharp=%d, up=%d, down=%d,",
+			  p_ctx->dev_cap.smooth_scaling, p_ctx->dev_cap.sharp_scaling,
+			  p_ctx->dev_cap.up_scaling,
+			  p_ctx->dev_cap.down_scaling);
+	PD_DEBUG("sdvo: Stall support=%d", p_ctx->dev_cap.stall);
+
+	PD_DEBUG("sdvo: out0:%s",
+			  sdvo_get_output_type(p_ctx->dev_cap.output.out0, 0));
+	PD_DEBUG("sdvo: out1:%s",
+			  sdvo_get_output_type(p_ctx->dev_cap.output.out1, 1));
+
+	/* Detecting STMicro device */
+	if ((p_ctx->dev_cap.vendor_id == 0x82) &&
+		(p_ctx->dev_cap.device_id==0x01)) {
+
+		p_ctx->st_sdvo = 1;
+	}
+
+	/* ...................................................................... */
+	p_ctx->out_type = p_ctx->dev_cap.output;
+
+	/*Workaround:
+	 * st_sdvo has two potential modes lvds and rgba
+	 * As we dont have the relevant information from xorg (attr/9(
+	 * at this point to correctly identify which one the user wants
+	 * and as set target will fail if sent 2 output types
+	 * This if has been added
+	 */
+	if(!p_ctx->st_sdvo)
+	{
+		status = sdvo_set_target_output(p_ctx, p_ctx->out_type);
+
+	}
+
+	status = sdvo_get_supported_power_states(p_ctx, &p_ctx->supp_pwr_states);
+	if (status == SS_SUCCESS) {
+		PD_DEBUG("sdvo: Supported Power States = %#x",
+			p_ctx->supp_pwr_states);
+	} else {
+		PD_DEBUG("sdvo: GetSupported Power States FAILED with status = %d",status);
+	}
+
+	/* ...................................................................... */
+	/*	Assume SDVO0 connected to Port B and SDVO1 connected to Port C		  */
+	/*	However the correct way to determine SDVO port to device mapping at   */
+	/*	run-time is via Get_Trained_Inputs opcode                             */
+	p_ctx->inp_dev = SDVO0;
+
+
+#ifndef CONFIG_MICRO
+	/*We want this in (Windows XP driver) AND
+	(in VBIOS when LVDS is not linked.)*/
+	if ((p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI)
+		&& (p_ctx->dev_cap.device_id == 0x1)){
+		status = sdvo_set_target_input(p_ctx, p_ctx->inp_dev);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_target_input()"
+				"failed with status=%d", status);
+			return PD_ERR_INTERNAL;
+		}
+
+
+
+		/*Workaround:
+			 * st_sdvo has two potential modes lvds and rgba
+			 * As we dont have the relevant information from xorg (attr/9(
+			 * at this point to correctly identify which one the user wants
+			 * and as set target will fail if sent 2 output types
+			 * This if has been added
+			 */
+		if(!p_ctx->st_sdvo)
+			status = sdvo_set_target_output(p_ctx, p_ctx->out_type);
+
+		if (status != SS_SUCCESS) {
+			PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_target_output()"
+				"failed with status=%d", status);
+			return PD_ERR_INTERNAL;
+		}
+
+
+		/* ...................................................................... */
+		status = sdvo_get_supported_power_states(p_ctx, &p_ctx->supp_pwr_states);
+		if (status == SS_SUCCESS) {
+			PD_DEBUG("sdvo: Supported Power States = %#x",
+			p_ctx->supp_pwr_states);
+		}
+	}
+#endif
+
+
+	/* This code is to find out whether a TV encoder is used to get VGA display
+	 * or TVout display */
+
+	/* Read the power state */
+	sdvo_get_power(p_ctx, &power_state);
+
+	/* set device to DO before performing connection detect attempt */
+	sdvo_set_power(p_ctx, PD_POWER_MODE_D0);
+
+	status = sdvo_get_attached_displays(p_ctx, &output_flags);
+	/*
+	 * sdvo_get_attached_displays returns "0" when sdvo cable is
+	 *  disconnected. Need to make sure out_type flags not
+	 *  overridden.
+	 */
+	if (status == SS_SUCCESS && output_flags.flags != 0) {
+		p_ctx->out_type.flags &= output_flags.flags;
+	} else {
+		/* If display detection fails, reset the hardware and try again: */
+		PD_ERROR("get_attached_display() did not succeed; trying HW reset");
+		status = sdvo_reset(p_ctx);
+		PD_DEBUG("sdvo_reset returned %u", status);
+		status = sdvo_get_attached_displays(p_ctx, &output_flags);
+		if (status == SS_SUCCESS && output_flags.flags != 0) {
+			p_ctx->out_type.flags &= output_flags.flags;
+		} else {
+			PD_ERROR("get_attached_display() did not succeed again; giving up");
+			return PD_ERR_NODEV;
+		}
+	}
+
+	/* If component video (YPbPr) is connected, for some reason, both ypbpr
+	 * and s_video are 1 when sdvo_get_attached_displays is called. We need
+	 * to set only ypbpr to 1 because, apparently, the attribute table
+	 * would not be created if multiple outputs are on (as in this case).
+	 */
+	if (p_ctx->out_type.flags & TV_YPBPR_DISP_MASK) {
+		p_ctx->out_type.flags &= TV_YPBPR_DISP_MASK;
+	}
+
+	/* restore the previous power state */
+	sdvo_set_power(p_ctx, power_state);
+	g_sdvo_drv.flags &= ~(PD_FLAG_CLK_SOURCE|PD_FLAG_PIPE_MASTER);
+
+	/* Set the display type */
+	if (p_ctx->out_type.flags & CRT_EXT_DISP_MASK) {
+		g_sdvo_drv.type	= PD_DISPLAY_CRT_EXT;
+	} else if (p_ctx->out_type.flags & DRGB_DISP_MASK) {
+		g_sdvo_drv.type = PD_DISPLAY_DRGB;
+	} else if (p_ctx->out_type.flags & FP_DISP_MASK) {
+		status = SDVO_IS_HDMI_SUPPORTED(p_ctx);
+		if (status == SS_SUCCESS) {
+			PD_DEBUG("sdvo: HDMI transmiter ");
+			g_sdvo_drv.type = PD_DISPLAY_HDMI_EXT;
+		} else{
+			g_sdvo_drv.type	= PD_DISPLAY_FP;
+		}
+	} else if (p_ctx->out_type.flags & TV_DISP_MASK) {
+		g_sdvo_drv.type = PD_DISPLAY_TVOUT;
+
+		/* TV display is PIPE master and also generates clock */
+		g_sdvo_drv.flags |= (PD_FLAG_CLK_SOURCE|PD_FLAG_PIPE_MASTER);
+
+		/* CH7021: Initialize H/V Overscan */
+		sdvo_write_i2c_reg(p_ctx, 0x38, 0x1F);
+		sdvo_write_i2c_reg(p_ctx, 0x39, 0x1F);
+
+#ifndef CONFIG_MICRO
+		/*Set optimal TV settings*/
+
+		/*identify tv output systems in terms of line, and frame rate*/
+		sdvo_read_i2c_reg(p_ctx, 0x36, &reg);
+
+		if (reg != 0 && reg != 0x24) //not 480i, and 576i system
+			reg = 0xFF;
+
+		status = sdvo_set_tv_settings(p_ctx,reg);
+
+		if (status != SS_SUCCESS) {
+			PD_DEBUG("sdvo: Error ! sdvo_open(): sdvo_set_tv_settings "
+				"failed with status=%d",  status);
+		}
+#endif
+
+	}
+
+	/* Indicate main driver about upscaling */
+	if (p_ctx->dev_cap.up_scaling && g_sdvo_drv.type != PD_DISPLAY_CRT_EXT) {
+		g_sdvo_drv.flags |= PD_FLAG_UP_SCALING;
+	} else {
+		/* Remove the upscaling dev caps for VGA bypass. There are functions
+		 * that decides on upscaling based on the caps and not the flags. */
+		p_ctx->dev_cap.up_scaling = 0;
+	}
+
+	/* ...................................................................... */
+	/*	CH7308 up-scaling workaround */
+	if ((p_ctx->dev_cap.vendor_id == 2) && (p_ctx->dev_cap.device_id == 0x41) &&
+		(p_ctx->dev_cap.revision_id == 1)) {
+		i2c_reg_t reg;
+		sdvo_read_i2c_reg(p_ctx, 0x5D, &reg);
+		reg |= 1;
+		sdvo_write_i2c_reg(p_ctx, 0x5D, reg);
+	}
+	/* ...................................................................... */
+
+	/* CH7021A XUD VGAbypass */
+	if (((p_ctx->dev_cap.vendor_id == 2) &&
+		((p_ctx->dev_cap.device_id==0xC2)||(p_ctx->dev_cap.device_id==0x42)))&&
+		(p_ctx->dev_cap.revision_id == 2) &&
+		(g_sdvo_drv.type == PD_DISPLAY_CRT_EXT)) {
+		i2c_reg_t reg;
+		sdvo_read_i2c_reg(p_ctx, 0x7F, &reg);
+		reg |= 1;
+		sdvo_write_i2c_reg(p_ctx, 0x7F, reg);
+
+		sdvo_read_i2c_reg(p_ctx, 0x6C, &reg);
+		reg |= 0x03;
+		sdvo_write_i2c_reg(p_ctx, 0x6C, reg);
+
+		sdvo_read_i2c_reg(p_ctx, 0x7F, &reg);
+		reg &= 0xFE;
+		sdvo_write_i2c_reg(p_ctx, 0x7F, reg);
+	}
+
+	return PD_SUCCESS;
+}
+
+/*	============================================================================
+	Function	:	sdvo_init_device is called to initialize a SDVO device
+
+	Parameters	:	p_context : Pointer to port driver allocated context
+					structure
+
+	Remarks     :
+
+	Returns     :	PD_SUCCESS	If initialization is successful
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_init_device(void *p_context)
+{
+	PD_DEBUG("sdvo: sdvo_init_device()");
+#ifndef CONFIG_MICRO
+	if(g_sdvo_drv.type == PD_DISPLAY_HDMI_EXT){
+		/* Initialize transmitter audio capability if available */
+		SDVO_INIT_HDMI(p_context);
+	}
+#endif
+	return PD_SUCCESS;
+}
+
+/*	============================================================================
+	Function	:	sdvo_get_timing_list is called to get the list of display
+					modes supported by the SDVO device and the display.
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+								structure
+					p_in_list: List of display modes supported by the IEG driver
+					pp_out_list: List of modes supported by the SDVO device
+
+	Remarks     :
+
+	Returns     :	PD_SUCCESS	On Success
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_get_timing_list(void *p_context, pd_timing_t *p_in_list,
+	pd_timing_t **pp_out_list)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status;
+	unsigned short pixel_range[2];
+	pd_dvo_info_t sdvo_info = {0, 0, 0, 0, 0, 0, 0, 0};
+	pd_display_info_t sdvo_display_info = {0, 0, 0, 0, NULL};
+	int ret = PD_SUCCESS;
+	pd_timing_t *tv_timing;
+	unsigned char hdtv = 0; /* 0: standard ; 1: high definition */
+
+	PD_DEBUG("sdvo: sdvo_get_timing_list()");
+
+	/* ...................................................................... */
+	status = sdvo_set_target_input(p_ctx, p_ctx->inp_dev);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error ! sdvo_get_timing_list: sdvo_set_target_input()"
+				  "failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+	status = sdvo_get_input_pixel_clock_range(p_ctx, pixel_range);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error ! sdvo_get_timing_list: "
+				  "sdvo_get_input_pixel_clock_range() failed with status=%d",
+				  status);
+		sdvo_info.min_dclk = 0;
+		sdvo_info.max_dclk = 0;
+	} else {
+		sdvo_info.min_dclk = (unsigned long)pixel_range[0] * 10;
+		if (p_ctx->st_sdvo) {
+			sdvo_info.min_dclk = 25000;
+		}
+		sdvo_info.max_dclk = (unsigned long)pixel_range[1] * 10;
+	}
+
+	sdvo_info.upscale   = p_ctx->dev_cap.up_scaling;
+	sdvo_info.downscale = p_ctx->dev_cap.down_scaling;
+	sdvo_display_info.panel_fit = p_ctx->up_scaling;
+
+	if (!(p_ctx->out_type.flags & TV_DISP_MASK)) {
+		/* For VGA, DVI, LVDS and HDMI displays,
+		 * call helper function to filter the modes */
+		ret = pd_filter_timings(p_ctx->p_callback->callback_context,
+			p_in_list, &p_ctx->p_mode_table, &sdvo_info, &sdvo_display_info);
+
+		/* Helper function returns the below values */
+		p_ctx->native_dtd = sdvo_display_info.native_dtd;
+		p_ctx->fp_width = sdvo_display_info.width;
+		p_ctx->fp_height = sdvo_display_info.height;
+		*pp_out_list = p_ctx->p_mode_table;
+	} else {
+		sdvo_create_preferred_timings_t pref_timings;
+		sdvo_dtd_t dtd_in;
+		unsigned char params[6] = {0,0,0,0,0,0};
+		unsigned char ret_value[8] = {0,0,0,0,0,0,0,0};
+		unsigned long *temp = (unsigned long *)&params[2];
+		unsigned char i=0, j=0;
+
+		status = sdvo_get_tv_output_format(p_ctx,params);
+		if (params[0] > 0 || params[1] > 0 || (params[2] > 0 &&
+			params[2] < 0x08)) {
+			/* SDTV support */
+			status = sdvo_get_sdtv_resolution_support(p_ctx,params,ret_value);
+			tv_timing = sdvo_sdtv_timing_table;
+
+		} else {
+			/* HDTV support */
+			(*temp) >>= 3;
+			status = sdvo_get_scaled_hdtv_resolution_support(
+				p_ctx, &params[2], ret_value);
+			tv_timing = sdvo_hdtv_timing_table;
+			hdtv = 1;
+		}
+
+
+		/* For TV displays generate supported timing table */
+
+		while (tv_timing->width != PD_TIMING_LIST_END) {
+			/* Fill-in pref timings */
+			/* Go through bits 0 to 7 of the returned byte and check to see if
+			 * it is on. If yes, calculate the preferred timing */
+
+			for (j=0; j<8; j++) {
+				tv_timing->mode_info_flags &= ~PD_MODE_SUPPORTED;
+				/* Cannot request preferred input timing for width/height = 0 */
+				if (!tv_timing->width || !tv_timing->height) {
+					tv_timing++;
+					continue;
+				}
+				if (ret_value[i] & BIT(j)) {
+					pd_memset(&pref_timings, 0, sizeof(pref_timings));
+					/* pref_timings.pixel_clock=(unsigned short)(t1->dclk/10);*/
+					pref_timings.active_horz_pixels = tv_timing->width;
+					pref_timings.active_vert_pixels = tv_timing->height;
+					status = sdvo_get_preferred_input_timings(p_ctx,
+						&pref_timings, &dtd_in);
+
+					if (status != SS_SUCCESS) {
+						PD_ERROR("sdvo_get_timing_list: %ux%u cannot be "
+							"supported status = %u",
+							tv_timing->width, tv_timing->height, status);
+					} else {
+						PD_DEBUG("sdvo_get_timing_list: "
+							"device returned values = %ux%u dclk=%u",
+							dtd_in.horz_active, dtd_in.vert_active,
+							dtd_in.pixel_clock);
+					}
+					if (dtd_in.horz_active && dtd_in.vert_active) {
+						sdvo_convert_dtd_to_pd_timing(&dtd_in, tv_timing);
+					}
+					PD_DEBUG("sdvo_get_timing_list: after conversion: "
+						"device returned values = %ux%u dclk=%lu",
+						tv_timing->width, tv_timing->height,
+						tv_timing->dclk);
+					tv_timing->refresh = 60;     /* This is for OS to see */
+					tv_timing->mode_info_flags |= PD_MODE_SUPPORTED;
+				} /* end checking ret_value[i] */
+				tv_timing++;
+			} /* end for loop */
+			i++; /* grab next returned byte */
+		} /* end while timing list */
+
+		if (hdtv) {
+			*pp_out_list = sdvo_hdtv_timing_table;
+		}
+		else {
+			*pp_out_list = sdvo_sdtv_timing_table;
+		}
+	}
+	 /* end tv if */
+	return ret;
+}
+
+/*	============================================================================
+	Function	:	sdvo_set_mode is called to test if specified mode can be
+					supported or to set it.
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+					p_mode	: New mode
+					flags	: In test mode it is set to PD_SET_MODE_FLAG_TEST
+
+	Remarks     :	sdvo_set_mode first verifies that the new mode is
+					supportable.
+					If not it returns an error status
+					If the flags is not set to PD_SET_MODE_FLAG_TEST it sets the
+					new mode.
+
+	Returns     :	PD_SUCCESS	On Success
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_set_mode(void *p_context, pd_timing_t *p_mode, unsigned long flags)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status;
+	sdvo_dtd_t dtd_out, dtd_in;
+	sdvo_clock_rate_mult_t clock_mult;
+	unsigned char b_use_scaled_timing;
+	pd_timing_t *p_native_mode;
+	unsigned long dclk;
+
+	PD_DEBUG("sdvo: sdvo_set_mode() mode=%dx%d@%d",
+			  p_mode->width, p_mode->height, p_mode->refresh);
+
+	p_native_mode = p_ctx->native_dtd;
+
+	if (p_ctx->out_type.flags & TV_DISP_MASK) {
+		p_native_mode = p_mode;
+	}
+
+	/* ...................................................................... */
+	status = sdvo_set_target_input(p_ctx, p_ctx->inp_dev);
+	if (status != SS_SUCCESS) {
+
+		PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_target_input()"
+				  "failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+	/*Workaround:
+		 * st_sdvo has two potential modes lvds and rgba
+		 * As we dont have the relevant information from xorg (attr/9(
+		 * at this point to correctly identify which one the user wants
+		 * and as set target will fail if sent 2 output types
+		 * This if has been added
+		 */
+	if(!p_ctx->st_sdvo)
+		status = sdvo_set_target_output(p_ctx, p_ctx->out_type);
+
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_target_output()"
+				  "failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+	/* ...................................................................... */
+	/*	Test mode.	Specified mode is supported by this device return success */
+	if (flags & PD_SET_MODE_FLAG_TEST) {
+		return PD_SUCCESS;
+	}
+
+	/* ...................................................................... */
+	/* Set power state to D0 */
+	sdvo_set_power(p_context, PD_POWER_MODE_D0);
+	/* ...................................................................... */
+	/*	Initialize the DTD structure and set output and input timings */
+	b_use_scaled_timing = FALSE;
+	/* p_ctx->unscaled_Mode.width = 0; */
+
+	dclk = p_mode->dclk;
+
+	/*	If this sDVO device supports scaling and if native mode was specified */
+	/*	get the preferred input timings for native output timings */
+	if (((p_native_mode) &&
+		((p_native_mode->width != p_mode->width) ||
+		 (p_native_mode->height != p_mode->height)) &&
+
+		((p_ctx->up_scaling) || (p_ctx->dev_cap.down_scaling))) ||
+
+		(p_ctx->out_type.flags & TV_DISP_MASK)) {
+
+		sdvo_create_preferred_timings_t pref_timings;
+
+		sdvo_convert_pd_timing_to_dtd(p_native_mode, &dtd_out,
+			p_native_mode->dclk);
+
+		status = sdvo_set_output_timings(p_ctx, &dtd_out);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_output_timings() "
+					  "failed with status=%d", status);
+			return PD_ERR_INTERNAL;
+		}
+
+		pd_memset(&pref_timings, 0, sizeof(pref_timings));
+		pref_timings.pixel_clock = (unsigned short)(p_native_mode->dclk / 10);
+		pref_timings.active_horz_pixels	= p_mode->width;
+		pref_timings.active_vert_pixels	= p_mode->height;
+
+		if (!(p_ctx->out_type.flags & TV_DISP_MASK)) {
+			pref_timings.b_scaled = 1;
+			if (p_native_mode->mode_info_flags & PD_SCAN_INTERLACE) {
+				pref_timings.b_interlaced = 1;
+			}
+		}
+
+		status = sdvo_get_preferred_input_timings(p_ctx, &pref_timings,&dtd_in);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("sdvo: Error ! sdvo_set_mode: "
+				"sdvo_get_preferred_input_timings() failed with status=%d",
+				status);
+		} else {
+			PD_DEBUG("sdvo: sdvo_set_mode: pref_in_timings = %ux%u dclk=%u",
+				dtd_in.horz_active, dtd_in.vert_active, dtd_in.pixel_clock);
+			b_use_scaled_timing = TRUE;
+			dclk = p_native_mode->dclk;
+		}
+	}
+
+	/* ...................................................................... */
+	/*	Determine clock rate multiplier */
+	if (dclk > 100000) {			/*	100-200 MHz */
+		clock_mult = CRM_1X;
+	} else if (dclk > 50000) {	/*	50-100 Mhz */
+		clock_mult = CRM_2X;
+	} else {							/*	25-50 Mhz */
+		clock_mult = CRM_4X;
+	}
+
+	PD_DEBUG("sdvo: Clock Multiplier = %d dclk = %lu", clock_mult, dclk);
+
+	/* ...................................................................... */
+	/*	SI 1364 Autozone switch workaround */
+	if ((p_ctx->dev_cap.vendor_id == 4) && (p_ctx->dev_cap.device_id == 0xAA)) {
+		i2c_reg_t data;
+		if (clock_mult == CRM_1X) {
+			data = 0x4D;
+		} else {
+			data = 0x49;
+		}
+		sdvo_write_i2c_reg(p_ctx, 0x51, data);
+	}
+
+	/* ...................................................................... */
+	if (! b_use_scaled_timing) {
+		sdvo_convert_pd_timing_to_dtd(p_mode, &dtd_out, p_mode->dclk);
+		status = sdvo_set_output_timings(p_ctx, &dtd_out);
+		if ((status != SS_SUCCESS) && (status != SS_NOT_SUPPORTED)) {
+			PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_output_timings() "
+					  "failed with status=%d", status);
+			return PD_ERR_INTERNAL;
+		}
+		dtd_in = dtd_out;
+	}
+
+#ifndef CONFIG_MICRO
+	/*We want this in (Windows XP driver) AND
+	(in VBIOS when LVDS is not linked.)*/
+	/* This workaround needed for OKI solution only.
+	It would probably give problem if the SDVO is connected to
+	analog display, but that is not the usage model for OKI.
+	*/
+	if ((p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI)
+		&& (p_ctx->dev_cap.device_id == 0x1)){
+		/* The OKI SDVO receiver to return “Invalid Argument” when:
+		(1) Horizontal Active   < 600
+		(2) Horizontal Blanking < 16
+		(3) HSync pulse width  < 2
+		(4) (HSync Offset + HSync pulse width) > Horizontal Blanking
+		(5) Vertical Active      < 480
+		(6) Vertical Blanking    < 3
+		(7) VSync pulse width  < 1
+		(8) (VSync Offset + VSync pulse width) > Vertical Blanking
+		(9) Interlace mode or unacceptable sync ( Part2/4 must be "00011xxx" )
+		(10) SDVO STALL mode
+		(11) SDVO Scaling mode
+
+		We have seen that (4) and (8) are problematic. The work around is
+		to moved Horizontal VBlank and Vertical VBlank so it is always
+		at least 1 pixel bigger than offset + pulse width.
+
+		There is no problem with Chrontel 7307 even with
+		horz_vblank <  (HSync Offset + HSync pulse width)
+		and I assume it is the same with other SDVO card.
+		*/
+		if ((dtd_in.horz_sync_offset + dtd_in.horz_sync_pulse_width) >
+					dtd_in.horz_blanking){
+			dtd_in.horz_blanking = dtd_in.horz_sync_offset +
+					dtd_in.horz_sync_pulse_width + 1;
+		}
+
+		if ((dtd_in.vert_sync_offset + dtd_in.vert_sync_pulse_width) >
+					dtd_in.vert_blanking){
+			dtd_in.vert_blanking = dtd_in.vert_sync_offset +
+					dtd_in.vert_sync_pulse_width + 1;
+		}
+
+	}
+#endif
+	/*
+	 * Disable panel fitting for CH7308 card on Pipe B if enabled, because it
+	 * handles its own upscaling
+	 */
+	if ((p_ctx->dev_cap.vendor_id == CH7308_VENDOR_ID) &&
+			(p_ctx->dev_cap.device_id == CH7308_DEVICE_ID) &&
+			(p_ctx->dev_cap.revision_id == CH7308_REVISION_ID)) {
+		pd_reg_t reg_list[2];
+
+		reg_list[0].reg = PFIT_CONTROL;
+		reg_list[1].reg = PD_REG_LIST_END;
+
+		/*Read the current value of the panel fitting control register*/
+		p_ctx->p_callback->read_regs(p_ctx->p_callback->callback_context,
+				reg_list, PD_REG_MIO);
+
+		PD_DEBUG("PFIT_CONTROL 0x%lx = 0x%lx", PFIT_CONTROL,reg_list[0].value);
+
+		if((reg_list[0].value & PFIT_PIPEB_ENABLED) == PFIT_PIPEB_ENABLED ){
+			/* Disable panel fitting */
+			reg_list[0].value = 0x0;
+			p_ctx->p_callback->write_regs(p_ctx->p_callback->callback_context,
+					reg_list, PD_REG_MIO);
+			PD_DEBUG(" Value 0x%lx written to PFIT_CONTROL 0x%lx",
+					reg_list[0].value,PFIT_CONTROL);
+		}
+	}
+
+	status = sdvo_set_input_timings(p_ctx, &dtd_in);
+	if ((status != SS_SUCCESS) && (status != SS_NOT_SUPPORTED)) {
+		PD_ERROR("sdvo: Error ! sdvo_set_mode: sdvo_set_input_timings() "
+				  "failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+	status = sdvo_set_clock_rate_multiplier(p_ctx, clock_mult);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error!sdvo_set_mode: sdvo_set_clock_rate_multiplier() "
+				  "failed with status=%d",  status);
+		return PD_ERR_INTERNAL;
+	}
+
+	return PD_SUCCESS;
+}
+
+/*	============================================================================
+	Function	:	sdvo_post_set_mode
+
+	Parameters	:	p_context: Pointer to port driver allocated context
+					p_mode	:
+					flags	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+int sdvo_post_set_mode(void *p_context, pd_timing_t *p_mode,
+						  unsigned long flags)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status;
+	i2c_reg_t trained = 0;
+	sdvo_output_flags_t out_flags[2];
+	int pd_status = PD_SUCCESS;
+	sdvo_output_flags_t output_flags;
+	/*sdvo_output_flags_t is_attached display;*/
+
+	PD_DEBUG("sdvo: sdvo_post_set_mode()");
+
+#ifndef CONFIG_MICRO
+	/* This is a workaround specific to OKI */
+	if((p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI)
+		&& (p_ctx->dev_cap.device_id == 0x1)){
+
+		pd_timing_t local_p_mode;
+		/* pd_reg_t reg_list[2]; */
+		/* unsigned long temp, portreg; */
+		local_p_mode = *p_mode;
+
+		sdvo_reset_encoder(p_context);
+		/* sdvo_reset(p_context);*/ /* THIS is workaround for OKI SDVO flashing issue.*/
+		sdvo_set_power(p_context, 0);
+		if (p_ctx->display_pwr_state == 0x0)
+		{
+
+			pd_attr_t *p_attr_temp =
+						pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+						PD_ATTR_ID_RB_SWAP_MODE, 0 );
+
+			/* RB SWAP */
+			sdvo_alter_static_attr(p_ctx, p_attr_temp, p_attr_temp->current_value);
+	
+			sdvo_set_mode(p_context, &local_p_mode,0);
+		} else {
+			sdvo_set_power(p_context, p_ctx->display_pwr_state);
+		}
+
+	}
+#endif
+
+	status = sdvo_get_trained_inputs(p_ctx, &trained);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error ! sdvo_get_trained_inputs() failed with "
+				  "status=%d", status);
+	} else {
+		if (trained == 0) {
+			PD_DEBUG("sdvo: Error ! sdvo_post_set_mode: "
+					  "No inputs are trained");
+		} else {
+			PD_DEBUG("sdvo: Success ! sdvo_post_set_mode: "
+					  "Inputs are trained");
+		}
+	}
+
+	status = sdvo_get_attached_displays(p_ctx, &output_flags);
+	if (status == SS_SUCCESS) {
+		PD_DEBUG("sdvo: get_attached_display returned => [0x%x]",
+			output_flags.flags);
+	} else {
+		/* TODO: What if display detection fails? */
+		/* pd_status = PD_ERR_NODEV; */
+		output_flags.flags = 0;
+	}
+
+	/* To enable YPbPr, the attribute 9 must be set to 16 in xorg.conf;
+	 * if it set, then p_ctx->out_type.flags would be changed to 16 in,
+	 * sdvo_set_attributes. Here, we are just making sure that if YPbPr
+	 * is enabled, then output_flags must be changed to 16. This needs to
+	 * be done because, when component video (ypbpr) is connected, both ypbpr
+	 * and s_video are 1 when read from the registers (sdvo_get_attached_displays).
+	 */
+	if (p_ctx->out_type.flags & TV_YPBPR_DISP_MASK) {
+		output_flags.flags &= TV_YPBPR_DISP_MASK;
+	}
+
+	if (output_flags.flags == 0) {
+		output_flags.flags = p_ctx->out_type.flags;
+	}else if(output_flags.flags != p_ctx->out_type.flags){
+		/* enable switching tv output type on-the-fly;
+		* also to enable cvbs+svideo simul display */
+		/* NOTE: Removed on-the-fly support with PRD change.
+		p_ctx->out_type.flags will always retain the registry
+		settings. Setting multiple bits out_type.flags for
+		sdvo_set_target_output will caused INVALID_ARGUMENT error
+		which in turn causes un-trained output. */
+		/*p_ctx->out_type.flags = output_flags.flags; */
+	}
+
+	/* set inout map */
+	PD_DEBUG("sdvo_post_set_mode: out_type.flags:[0x%x]",
+		output_flags.flags);
+
+	/* For ST SDVO device, when the jumper is set to LVDS or
+	 * DRGB, output flags get set to 0xC0.  We need to then
+	 * set the output flags to only DRGB.
+	 */
+	if (p_ctx->st_sdvo) {
+		PD_DEBUG("sdvo_post_set_mode: out_type.flags [0x%x]->[0x%x]",
+			output_flags.flags, p_ctx->out_type.flags);
+		output_flags.flags = p_ctx->out_type.flags;
+	}
+
+	PD_DEBUG("sdvo_post_set_mode: out0:%s, out 1:%s",
+		sdvo_get_output_type(p_ctx->out_type.out0, 0),
+		sdvo_get_output_type(p_ctx->out_type.out1, 1));
+
+	/* set inout map */
+	if(output_flags.flags & TV_DISP_MASK){
+		/* Ensure only one output (SVIDEO) is used since in out map
+		* command can only handle one active port. SDVO specs also
+		* states that any tv setting would be applied to all connectors */
+		out_flags[0].flags = TV_SVIDEO_DISP_MASK;
+	}else{
+		out_flags[0].flags = output_flags.flags;
+		/* in0 = required output type */
+	}
+	out_flags[1].flags = 0;                     /* in1 = 0 */
+	status = sdvo_set_in_out_map(p_ctx, out_flags);
+	/* Some encoders doesn't support INOUT Map */
+	if (status != SS_SUCCESS && status != SS_NOT_SUPPORTED) {
+		PD_ERROR("sdvo: Error ! sdvo_post_set_mode: sdvo_set_in_out_map() "
+			"failed with status=%d",  status);
+		pd_status = PD_ERR_INTERNAL;
+	}
+
+	/* set active outputs */
+	status = sdvo_set_active_outputs(p_ctx, output_flags);
+	if (status != SS_SUCCESS) {
+
+		PD_ERROR("sdvo: Error ! sdvo_post_set_mode: sdvo_set_active_outputs() "
+			"failed with status=%d",  status);
+		pd_status = PD_ERR_INTERNAL;
+	}
+#ifndef CONFIG_MICRO
+	status = SDVO_IS_HDMI_SUPPORTED(p_context);
+	if(status == SS_SUCCESS){
+		SDVO_CONFIGURE_HDMI(p_ctx);
+	}
+#endif
+	/* Wait for panel power up sequence to complete */
+	pd_usleep((unsigned long)(p_ctx->t1+p_ctx->t2)*1000 + 10);
+
+	return pd_status;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_attributes is called to get the list of all the
+					available attributes
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					p_Num	: Return the total number of attributes
+					pp_list	: Return the list of port driver attributes
+
+	Remarks     :	sdvo_get_attributes calls SDVO interface functions to get all
+					available range,bool and list attributes supported by the
+					SDVO device
+
+	Returns     :	PD_SUCCESS	On Success
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_get_attributes(void *p_context, unsigned long *p_num_attr,
+	pd_attr_t **pp_list)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status;
+	unsigned long i;
+
+	PD_DEBUG("sdvo: sdvo_get_attributes()");
+
+	/* No attributes for external CRT */
+	if (p_ctx->out_type.flags & CRT_EXT_DISP_MASK) {
+		PD_DEBUG("No attributes available for SDVO CRT.");
+		*p_num_attr = 0;
+		return 0;
+	}
+#ifndef CONFIG_MICRO
+	/* This is a workaround specific to OKI */
+	if(p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI
+		&& p_ctx->dev_cap.device_id == 0x1){
+		/* 	TODO: sdvo_set_target_output return pending when the power state is D3 on
+		* 	ML7213 A0. other sdvo cards don't have this problem and OKI don't see
+		* 	this problem on their site. verify this on A1 and remove the code if
+		* 	it return success */
+		sdvo_set_power(p_ctx, PD_POWER_MODE_D0);
+	}
+#endif
+
+	status = sdvo_set_target_output(p_ctx, p_ctx->out_type);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error ! sdvo_get_attributes: sdvo_set_target_output()"
+			"failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+	if (p_ctx->p_attr_table == NULL) {
+		PD_DEBUG("sdvo: sdvo_get_attributes: "
+			"Attribute Table not initialized");
+		return PD_ERR_INTERNAL;
+	}
+	/* ...................................................................... */
+	for (i = 0; i < p_ctx->num_attrs ; i++) {
+		pd_attr_t *p_attr_cur = &p_ctx->p_attr_table[i];
+
+		if ((p_attr_cur->flags & PD_ATTR_FLAG_DYNAMIC) == 0) {
+			status = sdvo_query_static_attr(p_ctx, p_attr_cur);
+		} else {
+			if ((p_attr_cur->id >= PD_ATTR_ID_FP_PWR_T1) &&
+				(p_attr_cur->id <= PD_ATTR_ID_FP_PWR_T5)) {
+				status = sdvo_query_panel_pwr_seq_attr(p_ctx,
+					(pd_range_attr_t *)p_attr_cur);
+			} else if (p_attr_cur->type == PD_ATTR_TYPE_RANGE) {
+				status = sdvo_query_range_attr(p_ctx,
+					(pd_range_attr_t *)p_attr_cur);
+			} else if (p_attr_cur->type == PD_ATTR_TYPE_BOOL) {
+				status = sdvo_query_bool_attr(p_ctx,
+					(pd_bool_attr_t *)p_attr_cur);
+			} else if (p_attr_cur->type == PD_ATTR_TYPE_LIST) {
+				status = sdvo_query_list_attr(p_ctx,
+					(pd_list_attr_t *)p_attr_cur);
+				i += ((pd_list_attr_t *)p_attr_cur)->num_entries;
+			}
+
+			if (status != SS_SUCCESS) {
+				PD_ERROR("sdvo: Error ! sdvo_get_attributes: "
+					"Failed to query attr[%ld]='%s', id=%ld",
+					i, SDVO_GET_ATTR_NAME(p_attr_cur), p_attr_cur->id);
+			}
+		}
+	}
+
+	/* ...................................................................... */
+	*pp_list	= p_ctx->p_attr_table;
+	*p_num_attr	= p_ctx->num_attrs ;
+	return PD_SUCCESS;
+}
+
+#ifndef CONFIG_MICRO
+int sdvo_set_tv_settings(void *p_context, i2c_reg_t tv_format)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+
+	unsigned char chron_tv_code;
+
+	PD_DEBUG("sdvo: sdvo_set_tv_settings: tv_format [0x%x]\n",tv_format);
+
+
+	if ((tv_format == 0xFF) || (p_ctx->out_type.flags & BIT(2)) ){
+		chron_tv_code = tv_format; /*HDTV or SDTV and composite type*/
+	} else {
+		chron_tv_code = tv_format | 0x01; /*include s-video and everything else*/
+	}
+
+	PD_DEBUG("sdvo: sdvo_set_tv_settings: chron_tv_code [0x%x]\n",chron_tv_code);
+
+	return sdvo_set_tv_optimal_settings(p_ctx,chron_tv_code);
+
+}
+#endif
+
+/*	============================================================================
+	Function	:	sdvo_set_attributes is called to modify one or more display
+					attributes
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					num     : Number of attributes
+					p_list	: List of attributes
+
+	Remarks     :	sdvo_set_attributes scans the attribute list to find the ones
+					that are to be modified by checking flags field in each
+					attribute for PD_ATTR_FLAG_VALUE_CHANGED bit. If this bit is
+					set it will call SDVO interface functions to set the new
+					value for the attribute.
+
+	Returns     :	PD_SUCCESS	On Success
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_set_attributes(void *p_context, unsigned long num_attrs,
+	pd_attr_t *p_list)
+{
+
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status;
+	sdvo_output_flags_t out_flags;
+	unsigned long i;
+	static unsigned char set_tvformat = 1;
+	unsigned short tvformat = 1;      /* Index 1 is for NTSC */
+	int found_display_setting = 0;
+
+	PD_DEBUG("sdvo: sdvo_set_attributes(): num_attrs=%ld", num_attrs);
+	/* Search for PD_ATTR_ID_DISPLAY in the incoming attribute table, if it   */
+	/* present and if sDVO supports > 1 displays use it to set TargetOutput   */
+	for (i = 0; i < num_attrs; i++) {
+		if ((p_list[i].flags & PD_ATTR_FLAG_VALUE_CHANGED) == 0) {
+			continue;
+		}
+
+		/* overwrite the connected display with requested display */
+		if (p_list[i].id == PD_ATTR_ID_DISPLAY) {
+			p_ctx->out_type.flags = (unsigned short)p_list[i].current_value;
+
+			PD_DEBUG("sdvo: Success ! sdvo_set_attributes: "
+				"attr='%s', id=%ld, current_value=%ld",
+				SDVO_GET_ATTR_NAME((&p_list[i])), p_list[i].id,
+				p_list[i].current_value);
+			/*	Clear attribute changed flag */
+			p_list[i].flags &= ~PD_ATTR_FLAG_VALUE_CHANGED;
+			found_display_setting = 1;
+			break;
+		}
+	}
+
+
+	/*in the situation where we have 2 potential outputs (e.g. lvds and drgb_
+	 * and a preference has not been set in the xorg
+	 * then we try and set an appropriate default, while outputting an error
+	 * message
+	 */
+	if(!found_display_setting && p_ctx->st_sdvo){
+			int defaultFound = 0;
+			printk("\n[EMGD] SDVO: ERROR !\n"
+					"\n\tALL/1/Port/2/Attr/9 is NOT set!"
+					"\n\tYou may want to set the value to either"
+					"\n\t\t0x40: for lvds "
+					"\n\tor"
+					"\n\t\t0x00: for VGA"
+					"\n\tCurrent Value: %x ",
+					 p_ctx->out_type.flags);
+
+			/*attempt default setting*/
+			out_flags.flags = p_ctx->out_type.flags;
+			if(out_flags.out0.lvds){
+				/*clear*/
+				out_flags.flags = 0x0;
+				/*set just lvds*/
+				out_flags.out0.lvds = 1;
+				defaultFound = 1;
+
+			}
+			else if(out_flags.out0.drgb){
+				/*clear*/
+				out_flags.flags = 0x0;
+				/*set just lvds*/
+				out_flags.out0.drgb = 1;
+				defaultFound = 1;
+			}
+			/*Should out1 be allowed a seperate set up? */
+			else if(out_flags.out1.drgb){
+				/*clear*/
+				out_flags.flags = 0x0;
+				/*set just lvds*/
+				out_flags.out1.drgb = 1;
+				defaultFound = 1;
+
+			}
+			else if(out_flags.out1.drgb){
+				/*clear*/
+				out_flags.flags = 0x0;
+				/*set just lvds*/
+				out_flags.out1.drgb = 1;
+				defaultFound = 1;
+
+			}
+
+			if(defaultFound)
+			{
+				printk("\n[EMGD] SDVO: Defaulting To: %x\n", out_flags.flags);
+				p_ctx->out_type.flags = out_flags.flags;
+			}
+			else
+			{
+				printk("\n[EMGD] SDVO: Error ! no appropriate default for p_ctx->out_type.flags!\n");
+			}
+
+	}
+
+	/* ...................................................................... */
+	status = sdvo_set_target_output(p_ctx, p_ctx->out_type);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error ! sdvo_set_attributes: sdvo_set_target_output()"
+				  "failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+
+	/* Here will try to get the supported power states again.  For multiple
+	 * displays functionality card, the supported power state will only can get
+      * succesfully after the output display type been seted.
+	 */
+	status = sdvo_get_supported_power_states(p_ctx, &p_ctx->supp_pwr_states);
+
+	/* ...................................................................... */
+	/*	Create attribute table if not already created                         */
+	if (p_ctx->p_attr_table == NULL) {
+		sdvo_init_attribute_table(p_ctx);
+	}
+
+	for (i = 0; i < num_attrs; i++) {
+		pd_attr_t *p_attr;
+		unsigned long new_value;
+
+		if ((p_list[i].flags & PD_ATTR_FLAG_VALUE_CHANGED) == 0) {
+			continue;
+		}
+
+		/* Special handling for TVFormat in vbios */
+		if (p_list[i].id == PD_ATTR_ID_TVFORMAT) {
+			set_tvformat = 1;
+			tvformat = (unsigned short) p_list[i].current_value;
+		}
+
+		/*	Clear attribute changed flag */
+		p_list[i].flags &= ~PD_ATTR_FLAG_VALUE_CHANGED;
+
+		new_value = p_list[i].current_value;
+
+		p_attr = pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+						p_list[i].id, 0);
+		if (p_attr == NULL) {
+			PD_DEBUG("sdvo: Error ! pd_get_attr() failed for attr "
+					  "id=%ld", p_list[i].id);
+			continue;
+		}
+
+		/* .................................................................. */
+		if(!(p_attr->flags & PD_ATTR_FLAG_DYNAMIC)) {
+			status = sdvo_alter_static_attr(p_ctx, p_attr, new_value);
+		} else {
+			if ((p_attr->id >= PD_ATTR_ID_FP_PWR_T1) &&
+				(p_attr->id <= PD_ATTR_ID_FP_PWR_T5)) {
+				status = sdvo_alter_panel_pwr_seq_attr(p_ctx,
+					(pd_range_attr_t *)p_attr, new_value);
+			} else if (p_attr->type == PD_ATTR_TYPE_RANGE) {
+				status = sdvo_alter_range_attr(p_ctx, (pd_range_attr_t *)p_attr,
+					  new_value);
+				if (p_attr->id == PD_ATTR_ID_VERT_OVERSCAN) {
+					p_attr->flags |= PD_ATTR_FLAG_SETMODE;
+				}
+			} else if (p_attr->type == PD_ATTR_TYPE_BOOL) {
+				status = sdvo_alter_bool_attr(p_ctx, (pd_bool_attr_t *)p_attr,
+					new_value);
+			} else if (p_attr->type == PD_ATTR_TYPE_LIST) {
+				status = sdvo_alter_list_attr(p_ctx, (pd_list_attr_t *)p_attr,
+					new_value);
+				if (p_attr->id == PD_ATTR_ID_TVFORMAT) {
+					p_attr->flags |= PD_ATTR_FLAG_SETMODE;
+				}
+				/* Note: Incoming attribute list may or maynot have the
+				 * list entries, so don't skip.
+				i += ((pd_list_attr_t *)p_attr)->num_entries;
+				p_attr += ((pd_list_attr_t *)p_attr)->num_entries; */
+
+			} else if (p_attr->type == PD_ATTR_TYPE_LIST_ENTRY) {
+				/* Skip the list entries */
+			} else {
+				PD_ERROR("sdvo: Error ! sdvo_set_attributes: "
+					  "Unknown attr='%s', id=%ld, type=%ld",
+					  SDVO_GET_ATTR_NAME(p_attr), p_attr->id, p_attr->type);
+				status = SS_UNSUCCESSFUL;
+			}
+		}
+
+		/* .................................................................. */
+		if (status == SS_SUCCESS) {
+			p_attr->current_value = new_value;
+			PD_DEBUG("sdvo: Success ! sdvo_set_attributes: "
+				"attr='%s', id=%ld, current_value=%ld",
+				SDVO_GET_ATTR_NAME(p_attr), p_attr->id, p_attr->current_value);
+
+		} else {
+			PD_ERROR("sdvo: Error ! sdvo_set_attributes: Failed to set "
+				"attr='%s', id=%ld", SDVO_GET_ATTR_NAME(p_attr), p_attr->id);
+		}
+	}
+
+#ifdef CONFIG_MICRO
+	if ((p_ctx->out_type.flags & TV_DISP_MASK) && set_tvformat) {
+		unsigned char params[8] = {0,0,0,0,0,0,0,0};
+		/* Special handling of TVFormat attribute in VBIOS, because
+		 * it wasn't enabled in the list attributes. */
+		/* TVFormat    Byte       Byte
+		 *             Number     Value
+		 * --------    ------     -----
+		 *     1       0          0x01
+		 *     2       0          0x02
+		 *     3       0          0x04
+		 *     4       0          0x08
+		 *     5       0          0x10
+		 *     6       0          0x20
+		 *     7       0          0x40
+		 *     8       0          0x80
+		 *     9       1          0x01
+		 *     10      1          0x02
+		 *     11      1          0x04
+		 *     12      1          0x08
+		 *     13      1          0x10
+		 *     ...
+		 *
+		 *     40      4          0x80
+		 *     41      5          0x01
+		 *     42      5          0x02
+		 *     43      5          0x04
+		 *     44      5          0x08
+		 *     45      5          0x10
+		 */
+		tvformat %= 46;      /* TVformat <= 45 */
+		params[tvformat/8] = 1<<((tvformat%8)-1);
+		status = sdvo_execute_command(p_ctx, SET_TV_OUTPUT_FORMAT, 8,
+			params, 0, NULL);
+		PD_DEBUG("sdvo_set_attributes(): Set TVFormat = %u:"
+			"params=0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x status=%u",
+			tvformat,
+			params[0], params[1], params[2],
+			params[3], params[4], params[5],
+			params[6], params[7], status);
+		set_tvformat = 0;
+	}
+#endif
+	return PD_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_set_power is called to change the power state of the
+					device
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					state	: New power state
+
+	Remarks     :
+
+	Returns     :	PD_SUCCESS	On Success
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_set_power(void *p_context, unsigned long state)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_output_flags_t active_outputs;
+	sdvo_status_t status = SS_SUCCESS;
+	unsigned short power_state;
+
+	PD_DEBUG("sdvo: sdvo_set_power(): requested state=%#lx", state);
+
+	/* Bits 7-4 in supp_pwr_states are display power states:
+	 * Find next supported low power state if requested state isn't
+	 * supported. */
+	power_state = 0x1 << state;
+	for (;power_state<=0x8; power_state<<=1) {
+		if ((p_ctx->supp_pwr_states>>4) & power_state) {
+			break;
+		}
+	}
+
+	/* Set display power state if it is a supported power state */
+	if (power_state <= 0x80) {
+		if (!p_ctx->st_sdvo) {
+			status = sdvo_set_display_power_state(p_ctx, (i2c_reg_t) power_state);
+		}
+		PD_DEBUG("sdvo: sdvo_set_power: "
+			"display_power_state = %#x status = %d",
+			(i2c_reg_t)power_state, status);
+	} else {
+		PD_DEBUG("sdvo: sdvo_set_power: No supported display power state");
+	}
+
+	/* Bits 3-0 in supp_pwr_states are device power states:
+	 * Find next supported low power state if requested state isn't
+	 * supported. */
+	power_state = 0x1 << state;
+	for (;power_state<=0x8; power_state<<=1) {
+		if (p_ctx->supp_pwr_states & power_state) {
+			break;
+		}
+	}
+
+	/* To avoid LCD flickering avoid setting the current state */
+	if (sdvo_get_power_state(p_ctx, &p_ctx->device_pwr_state) == SS_SUCCESS) {
+		if ((p_ctx->device_pwr_state & 0x0f) == power_state) {
+			PD_DEBUG("sdvo: sdvo_set_power: Device is already"
+				"in %u power state", power_state);
+			return PD_SUCCESS;
+		}
+	}
+
+
+	/*	Disable the output if status is not POWER ON */
+	if (state != PD_POWER_MODE_D0) {
+		status = sdvo_get_active_outputs(p_ctx, &active_outputs);
+		if (status != SS_SUCCESS) {
+			PD_ERROR("sdvo: Error ! sdvo_set_power: "
+				"sdvo_get_active_outputs() failed with status=%d",
+				status);
+		} else {
+			active_outputs.flags = active_outputs.flags &
+				(~p_ctx->out_type.flags);
+			status = sdvo_set_active_outputs(p_ctx, active_outputs);
+			if (status != SS_SUCCESS) {
+				PD_ERROR("sdvo: Error ! sdvo_set_power: "
+					"sdvo_set_active_outputs() failed with status=%d",
+					status);
+			}
+			/* Wait for panel power down sequence to complete */
+			pd_usleep((unsigned long)(p_ctx->t3+p_ctx->t4+p_ctx->t5)*1000 + 10);
+		}
+	}
+
+	/* Set device power state if it is a supported power state */
+	if (power_state <= 0x8) {
+		if (!p_ctx->st_sdvo) {
+			status = sdvo_set_power_state(p_ctx, (i2c_reg_t)power_state);
+		}
+		PD_DEBUG("sdvo: sdvo_set_power: "
+			"device_power_state = %#x status = %d",
+			(i2c_reg_t)power_state, status);
+		if (status == SS_SUCCESS || status == SS_POWER_ON_STATE) {
+			p_ctx->device_pwr_state = (unsigned char) power_state;
+		}
+	} else {
+		PD_DEBUG("sdvo: sdvo_set_power: No supported device power state");
+	}
+
+#ifdef DEBUG
+	{
+		unsigned char pstate;
+		status = sdvo_get_power_state(p_ctx, &pstate);
+		PD_DEBUG("sdvo: sdvo_set_power: new sdvo pwr_state=%#x", power_state);
+	}
+#endif
+	return PD_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_power is called to get the current power state
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					p_state	: Returns the current power state
+
+	Remarks     :
+
+	Returns     :	PD_SUCCESS	On Success
+					PD_ERR_xxx	On Failure
+	------------------------------------------------------------------------- */
+int sdvo_get_power(void *p_context, unsigned long *p_state)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_status_t status;
+	unsigned char power_state;
+
+	PD_DEBUG("sdvo: sdvo_get_power()");
+
+	status = sdvo_get_power_state(p_ctx, &power_state);
+	if (status != SS_SUCCESS) {
+		PD_ERROR("sdvo: Error! sdvo_get_power_state() failed with status=%d",
+			status);
+	}
+
+	/* ...................................................................... */
+	PD_DEBUG("sdvo: Current Power state=%#x", power_state);
+
+	/*
+	 * power_state has both the display and the encoder power state.
+	 * The display state is in the upper nybble and the encoder state
+	 * is in the lower nybble.  Currently we only care about the
+	 * encoder state so mask off the upper mybble.
+	 */
+	switch ((power_state & 0x0f)) {
+		case 0x1:        /* Bit 0 */
+			*p_state = PD_POWER_MODE_D0;
+			break;
+		case 0x2:        /* Bit 1 */
+			*p_state = PD_POWER_MODE_D1;
+			break;
+		case 0x4:        /* Bit 2 */
+			*p_state = PD_POWER_MODE_D2;
+			break;
+		case 0x8:        /* Bit 3 */
+		default:
+			*p_state = PD_POWER_MODE_D3;
+			break;
+	}
+
+	return PD_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_save is called to save the default state of registers
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					pp_state : Returs a pointer to list of SDVO registers
+					terminated with PD_REG_LIST_END.
+					flags	: Not used
+
+	Remarks		:	sdvo_save does not save any registers.
+
+	Returns		:	PD_SUCCESS
+	------------------------------------------------------------------------- */
+int sdvo_save(void *p_context, void **pp_state, unsigned long flags)
+{
+#ifndef CONFIG_MICRO
+	sdvo_device_context_t *pd_context = (sdvo_device_context_t *)p_context;
+	sdvo_state_t   *reg_state;
+	int            ret, status;
+	sdvo_dtd_t p_dtd;
+
+	PD_DEBUG("sdvo: sdvo_save()");
+	/* Allocate memory to save the current regs */
+	reg_state = (sdvo_state_t *) pd_malloc(sizeof(sdvo_state_t));
+	if (!reg_state) {
+		return (PD_ERR_NOMEM);
+	}
+	pd_memset(reg_state, 0, sizeof(sdvo_state_t));
+
+	/* Save the current state */
+	ret = sdvo_get_power(pd_context, &(reg_state->power_state));
+	if (ret) {
+		pd_free(reg_state);
+		return ret;
+	}
+
+	/* Save current timings */
+	ret = sdvo_set_power(p_context, PD_POWER_MODE_D0);
+	if ((status = sdvo_get_input_timings(pd_context, &p_dtd)) != SS_SUCCESS) {
+		if (status != SS_SUCCESS) {
+			/* Standard VGA mode 3+ timings */
+			p_dtd.pixel_clock = 2832;
+			p_dtd.horz_active = 720;
+			p_dtd.horz_blanking = 162;
+			p_dtd.vert_active = 400;
+			p_dtd.vert_blanking = 35;
+			p_dtd.horz_sync_offset = 45;
+			p_dtd.horz_sync_pulse_width = 108;
+			p_dtd.vert_sync_offset = 12;
+			p_dtd.vert_sync_pulse_width = 2;
+			p_dtd.dtd_flags = 0x1c;
+			p_dtd.sdvo_flags = 0;
+			PD_ERROR("sdvo: Failed to save current timings - command not "
+						"supported.");
+			PD_ERROR("sdvo: Standard 720x400 VGA timings will be used.");
+		}
+	}
+	sdvo_convert_dtd_to_pd_timing(&p_dtd, &(reg_state->timing));
+
+	/* In case someone calls save again without restore, free the previous
+     * state */
+	if (NULL != *pp_state) {
+		pd_free(*pp_state);
+	}
+
+	*pp_state = (void *)reg_state;
+
+	/* restore power state, just in case */
+	ret = sdvo_set_power(pd_context, reg_state->power_state);
+#endif
+	return PD_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_restore is called to restore the registers which were
+					save previously via a call to sdvo_save
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					p_state	: List of SDVO registers
+					flags	: Not used
+
+	Remarks	:
+
+	Returns	:	PD_SUCCESS
+	------------------------------------------------------------------------- */
+int sdvo_restore(void *p_context, void *p_state, unsigned long flags)
+{
+#ifndef CONFIG_MICRO
+	sdvo_device_context_t *pd_context = (sdvo_device_context_t *)p_context;
+	sdvo_state_t *reg_state = (sdvo_state_t *)p_state;
+	pd_attr_t attr_tmp;
+	int ret;
+	unsigned long i;
+	/*i2c_reg_t reg;*/
+
+
+
+	PD_DEBUG("sdvo: sdvo_restore()");
+	if (!reg_state) {
+		PD_ERROR("restore got a NULL saved state.");
+		return PD_ERR_NULL_STATE;
+	}
+
+	sdvo_reset_encoder(p_context);    /* Reset the sdvo device to known state for good
+							   * start. */
+	/* Add the code to process the CH7022 card */
+	if ((pd_context->dev_cap.vendor_id == 0x02) &&
+		((pd_context->dev_cap.device_id  == 0xc2) ||
+		 (pd_context->dev_cap.device_id  == 0x42)) &&
+		((pd_context->p_attr_table[0].current_value == 0x04) ||
+		 (pd_context->p_attr_table[0].current_value == 0x08))) {
+		ret = sdvo_set_target_input(p_context, pd_context->inp_dev);
+		ret = sdvo_set_target_output(p_context, pd_context->out_type);
+		ret = sdvo_set_power(p_context, PD_POWER_MODE_D0);
+		ret = sdvo_set_power(p_context, PD_POWER_MODE_D3);
+
+		/* CH7021: Initialize H/V Overscan */
+		if (pd_context->out_type.flags & TV_DISP_MASK) {
+			sdvo_write_i2c_reg(p_context, 0x38, 0x1F);
+			sdvo_write_i2c_reg(p_context, 0x39, 0x1F);
+		}
+	} else {
+		/*
+		 * 0X5Acircuitry enable register for 7308
+		 * BIT0=1 triggers power up sequence
+		 * BIT0=0 triggers power down sequence
+		 * BIT3=1 enable bit for circuitry access
+		 */
+		/* removing this as this seems to cause screen corruption after killx */
+		/*if ((pd_context->dev_cap.vendor_id == 2) && (pd_context->dev_cap.device_id == 0x41)){
+			sdvo_read_i2c_reg(pd_context, 0x5A, &reg);
+			reg |= 9;
+			sdvo_write_i2c_reg(pd_context, 0x5A, reg);
+		}*/
+#endif
+		/* Restore the saved mode */
+		ret = sdvo_set_power(p_context, PD_POWER_MODE_D0);
+		reg_state->timing.refresh = 70;  /* Is refresh needed? */
+		sdvo_set_mode(p_context, &(reg_state->timing), 0);
+		sdvo_post_set_mode(p_context, &(reg_state->timing), 0);
+		if (!(flags & PD_NO_RESTORE_FREE_STATE)) {
+			/* Restore the power state */
+			ret = sdvo_set_power(p_context, reg_state->power_state);
+			if (ret) {
+				return ret;
+			}
+		}
+	}
+
+	/* restore the sdvo attributes. */
+	/* The below should be sufficient for general sdvo + ch7308 */
+	/* TODO, add more attributes as neccessary for other cards */
+	for(i=0;i<pd_context->num_attrs;i++) {
+		switch (pd_context->p_attr_table[i].id) {
+		case PD_ATTR_ID_2_CHANNEL_PANEL:
+		case PD_ATTR_ID_LVDS_PANEL_TYPE:
+		case PD_ATTR_ID_DITHER:
+		case PD_ATTR_ID_PANEL_PROTECT_HSYNC:
+		case PD_ATTR_ID_PANEL_PROTECT_VSYNC:
+		case PD_ATTR_ID_TEXT_TUNING:
+		case PD_ATTR_ID_PANEL_DEPTH:
+		case PD_ATTR_ID_SSC:
+		case PD_ATTR_ID_DISPLAY:
+		case PD_ATTR_ID_FP_PWR_T1:
+		case PD_ATTR_ID_FP_PWR_T2:
+		case PD_ATTR_ID_FP_PWR_T3:
+		case PD_ATTR_ID_FP_PWR_T4:
+		case PD_ATTR_ID_FP_PWR_T5:
+			pd_context->p_attr_table[i].flags |= PD_ATTR_FLAG_VALUE_CHANGED;
+			sdvo_set_attributes(p_context, 1, &pd_context->p_attr_table[i]);
+			break;
+
+		/* TV format is a list attribute, need to alter in different way */
+		case PD_ATTR_ID_TVFORMAT:
+			if (pd_context->p_attr_table[i].type == PD_ATTR_TYPE_LIST) {
+				pd_memcpy(&attr_tmp, &pd_context->p_attr_table[i],
+							sizeof(pd_attr_t));
+				attr_tmp.flags |= PD_ATTR_FLAG_VALUE_CHANGED;
+				sdvo_set_attributes(p_context, 1, &attr_tmp);
+			}
+		default:
+			break;
+		}
+	}
+
+	pd_free(p_state);
+	p_state = NULL;
+
+	return PD_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_validate
+
+	Parameters	:	cookie
+
+	Remarks     :	sdvo_Valite returns the cookie it received as an argument
+
+	Returns     :	cookie
+	------------------------------------------------------------------------- */
+unsigned long sdvo_validate(unsigned long cookie)
+{
+	PD_DEBUG("sdvo: sdvo_validate()");
+	return cookie;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_close is the last function to be called in the port
+					driver
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+
+	Remarks     :
+
+	Returns     :	PD_SUCCESS
+	------------------------------------------------------------------------- */
+int sdvo_close(void *p_context)
+{
+#ifndef CONFIG_MICRO
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	/* Shutdown device if it is tv to avoid flickering */
+	if (p_ctx->out_type.flags & TV_DISP_MASK) {
+		sdvo_set_power(p_context, PD_POWER_MODE_D3);
+	}
+	PD_DEBUG("sdvo: sdvo_close()");
+
+	if (p_ctx->p_attr_table != NULL) {
+
+		pd_free(p_ctx->p_attr_table);
+		p_ctx->p_attr_table = NULL;
+		p_ctx->num_attrs	 = 0;
+	}
+
+	if (p_ctx->p_mode_table) {
+		pd_free(p_ctx->p_mode_table);
+		p_ctx->p_mode_table = NULL;
+	}
+	pd_free(p_ctx);
+#endif
+	return PD_SUCCESS;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_get_port_status is called to get the status of the
+					display
+
+	Parameters	:	p_context: Pointer to port driver allocated context structure
+					port_status : Returns display type and connection state
+
+	Returns     :	PD_SUCCESS or PD_ERR_XXX
+	------------------------------------------------------------------------- */
+int sdvo_get_port_status(void *p_context, pd_port_status_t *port_status)
+{
+	sdvo_device_context_t *p_ctx = (sdvo_device_context_t *)p_context;
+	sdvo_output_flags_t out_flags;
+	sdvo_display_output_t disp_out;
+	sdvo_status_t status;
+	unsigned long  pwr_state;
+
+	port_status->display_type = 0;
+	port_status->connected	  = PD_DISP_STATUS_UNKNOWN;
+
+
+	/* Save the current power state and turn on the port device's power
+	 * so we can query it
+	 */
+	sdvo_get_power(p_context, &pwr_state);
+	sdvo_set_power(p_context, PD_POWER_MODE_D0);
+
+	status = sdvo_get_attached_displays(p_ctx, &out_flags);
+
+	sdvo_set_power(p_context, pwr_state);
+
+	if (status != SS_SUCCESS) {
+		/* TODO: Certain SDVO cards (CH7022) would return SS_PENDING. Need to
+		 * Identify correct setting so the card behaves correctly. For now
+		 * we would fallback to out_type kept in the context.
+		 */
+		if(status == SS_PENDING){
+			out_flags.flags = p_ctx->out_type.flags;
+		}else{
+			PD_ERROR("sdvo: Error ! sdvo_get_port_status: "
+				"sdvo_get_attached_displays() failed with status=%d", status);
+			return PD_ERR_INTERNAL;
+		}
+	}
+
+	if (p_ctx->st_sdvo) {
+		out_flags.flags = p_ctx->out_type.flags;
+
+		if (p_ctx->out_type.flags == 0x40) {
+			out_flags.out0.lvds = 1;
+			out_flags.out0.drgb = 0;
+			out_flags.out1.lvds = 0;
+			out_flags.out1.drgb = 0;
+		} else {
+			out_flags.out0.lvds = 0;
+			out_flags.out0.drgb = 1;
+			out_flags.out1.lvds = 0;
+			out_flags.out1.drgb = 0;
+		}
+	}
+
+	if (p_ctx->inp_dev == SDVO0) {
+		disp_out = out_flags.out0;
+	} else {
+		disp_out = out_flags.out1;
+	}
+
+	port_status->connected = PD_DISP_STATUS_ATTACHED;
+
+	if (disp_out.tmds) {
+		status = SDVO_IS_HDMI_SUPPORTED(p_ctx);
+		if (status == SS_SUCCESS) {
+			PD_DEBUG("sdvo: HDMI transmiter ");
+			g_sdvo_drv.type = PD_DISPLAY_HDMI_EXT;
+			port_status->display_type = PD_DISPLAY_HDMI_EXT;
+		} else{
+			g_sdvo_drv.type	= PD_DISPLAY_FP;
+			port_status->display_type = PD_DISPLAY_FP;
+		}
+	} else if (disp_out.lvds) {
+		port_status->display_type = PD_DISPLAY_LVDS_EXT;
+	} else if (disp_out.rgb) {
+		port_status->display_type = PD_DISPLAY_CRT_EXT;
+	} else if (disp_out.drgb) {
+		port_status->display_type = PD_DISPLAY_DRGB;
+	} else if (disp_out.cvbs || disp_out.scart ||
+			   disp_out.svid || disp_out.yprpb) {
+		port_status->display_type = PD_DISPLAY_TVOUT;
+	} else {
+		port_status->connected = PD_DISP_STATUS_DETACHED;
+	}
+	return PD_SUCCESS;
+}
+
+char *sdvo_get_output_type(sdvo_display_output_t out, unsigned char byte)
+{
+	static char buff[40] = "";
+	int indx = 0;
+
+	if (out.tmds) {
+
+		pd_strcpy(&buff[indx], " tmds");
+		indx += 5;
+	}
+
+	if (out.rgb) {
+
+		pd_strcpy(&buff[indx], " rgb");
+		indx += 4;
+	}
+
+	if (out.cvbs) {
+
+		pd_strcpy(&buff[indx], " cvbs");
+		indx += 5;
+	}
+
+	if (out.svid) {
+
+		pd_strcpy(&buff[indx], " svid");
+		indx += 5;
+	}
+
+	if (out.yprpb) {
+
+		pd_strcpy(&buff[indx], " yprpb");
+		indx += 6;
+	}
+
+	if (out.scart) {
+
+		pd_strcpy(&buff[indx], " scart");
+		indx += 6;
+	}
+
+	if (out.lvds) {
+
+		pd_strcpy(&buff[indx], " lvds");
+		indx += 5;
+	}
+
+	/* DRGB defined in byte0 of SDVOOutputFlagStructure */
+	if (byte == 0 && out.drgb) {
+		pd_strcpy(&buff[indx], " drgb");
+		indx += 5;
+	}
+
+	if (indx == 0) {
+
+		pd_strcpy(buff, " None");
+	}
+
+	return buff;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_convert_pd_timing_to_dtd
+
+	Parameters	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+void sdvo_convert_pd_timing_to_dtd(pd_timing_t *p_mode, sdvo_dtd_t *p_dtd,
+							   unsigned long dclk)
+{
+	pd_memset(p_dtd, 0, sizeof(sdvo_dtd_t));
+
+	p_dtd->pixel_clock			= (unsigned short)(dclk / 10);
+
+	p_dtd->horz_active			= p_mode->width;
+	p_dtd->horz_blanking		= p_mode->hblank_end - p_mode->hblank_start;
+	p_dtd->horz_sync_offset		= p_mode->hsync_start - p_mode->width + 1;
+	p_dtd->horz_sync_pulse_width= p_mode->hsync_end - p_mode->hsync_start;
+
+	p_dtd->vert_active			= p_mode->height;
+	p_dtd->vert_blanking		= p_mode->vblank_end - p_mode->vblank_start;
+	p_dtd->vert_sync_offset		= p_mode->vsync_start - p_mode->height + 1;
+	p_dtd->vert_sync_pulse_width= p_mode->vsync_end - p_mode->vsync_start;
+
+	/* ...................................................................... */
+	p_dtd->dtd_flags = (BIT(4) | BIT(3));     /*	Digital Seperate */
+
+	if (p_mode->mode_info_flags & PD_SCAN_INTERLACE) {
+
+		p_dtd->dtd_flags |= BIT(7);
+	}
+
+	if (p_mode->mode_info_flags & PD_HSYNC_HIGH) {	/*	HSync + */
+
+		p_dtd->dtd_flags |= BIT(1);
+	}
+
+	if (p_mode->mode_info_flags & PD_VSYNC_HIGH) {	/*	VSync + */
+
+		p_dtd->dtd_flags |= BIT(2);
+	}
+
+	/*	....................................................................  */
+	p_dtd->sdvo_flags = 0;
+}
+
+
+/*	============================================================================
+	Function	:	sdvo_convert_dtd_to_pd_timing
+
+	Parameters	:
+
+	Remarks     :
+
+	Returns     :
+	------------------------------------------------------------------------- */
+void sdvo_convert_dtd_to_pd_timing(sdvo_dtd_t *p_dtd, pd_timing_t *p_mode)
+{
+	pd_memset(p_mode, 0, sizeof(pd_timing_t));
+
+	p_mode->dclk		= p_dtd->pixel_clock * 10L;
+
+	p_mode->width		= p_dtd->horz_active;
+	p_mode->hsync_start	= p_dtd->horz_sync_offset + p_dtd->horz_active - 1;
+	p_mode->hsync_end	= p_dtd->horz_sync_pulse_width + p_mode->hsync_start;
+	p_mode->hblank_start= p_dtd->horz_active - 1;
+	p_mode->hblank_end	= p_dtd->horz_blanking + p_mode->hblank_start;
+	p_mode->htotal		= p_dtd->horz_active + p_dtd->horz_blanking - 1;
+
+	p_mode->height		= p_dtd->vert_active;
+	p_mode->vsync_start	= p_dtd->vert_sync_offset + p_dtd->vert_active - 1;
+	p_mode->vsync_end	= p_dtd->vert_sync_pulse_width + p_mode->vsync_start;
+	p_mode->vblank_start= p_dtd->vert_active - 1;
+	p_mode->vblank_end	= p_dtd->vert_blanking + p_mode->vblank_start;
+	p_mode->vtotal		= p_dtd->vert_active + p_dtd->vert_blanking - 1;
+
+	/*	....................................................................  */
+	if (p_dtd->dtd_flags & BIT(7)) {
+		p_mode->mode_info_flags |= PD_SCAN_INTERLACE;
+	}
+
+	if (p_dtd->dtd_flags & BIT(1)) {
+		p_mode->mode_info_flags |= PD_HSYNC_HIGH;
+	}
+
+	if (p_dtd->dtd_flags & BIT(2)) {
+		p_mode->mode_info_flags |= PD_VSYNC_HIGH;
+	}
+}
+
+/*	============================================================================
+	Function	:	sdvo_init_attribute_table
+
+	Parameters	:	p_ctx
+
+	Remarks     :
+
+	Returns     :	PD_SUCCESS or PD_ERR_XXX
+	------------------------------------------------------------------------- */
+int sdvo_init_attribute_table(sdvo_device_context_t *p_ctx)
+{
+	unsigned long num_attrs, num_static_attrs, num_pwr_seq_attrs;
+	unsigned long num_range_attrs, num_bool_attrs, num_list_attrs;
+	unsigned char *p_table;
+	sdvo_status_t status;
+
+	/* .................................................................. */
+	status = sdvo_set_target_input(p_ctx, p_ctx->inp_dev);
+	if (status != SS_SUCCESS) {
+
+		PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: sdvo_set_target_input()"
+				  "failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+	status = sdvo_set_target_output(p_ctx, p_ctx->out_type);
+	if (status != SS_SUCCESS) {
+
+		PD_ERROR("sdvo: Error ! sdvo_init_attribute_table:"
+			"sdvo_set_target_output()failed with status=%d", status);
+		return PD_ERR_INTERNAL;
+	}
+
+
+	/* .................................................................. */
+	/*	Get the number of available attributes from the SDVO interface    */
+	num_static_attrs	= sdvo_get_static_attrs(p_ctx, NULL);
+	num_pwr_seq_attrs	= sdvo_get_panel_pwr_seq_attrs(p_ctx, NULL);
+	num_range_attrs		= sdvo_get_range_attrs(p_ctx, NULL);
+	num_bool_attrs      = sdvo_get_bool_attrs(p_ctx, NULL);
+	num_list_attrs      = sdvo_get_list_attrs(p_ctx, NULL);
+	p_ctx->num_attrs = num_static_attrs + num_pwr_seq_attrs +
+					   num_range_attrs + num_bool_attrs + num_list_attrs;
+
+	if (p_ctx->num_attrs == 0) {
+
+		PD_ERROR("sdvo: Warning ! sdvo_init_attribute_table: "
+				  "No attributes found");
+
+		return PD_SUCCESS;
+	}
+
+	/* .................................................................. */
+	/*	Allocate memory to save all available port attributes.            */
+	/*	Allocate space for extra attribute so Query_Attr function can be   */
+	/*  called using that space for the last+1 attribute.                 */
+	/*  Query and save all the available attributes.                      */
+	p_ctx->p_attr_table = pd_malloc((p_ctx->num_attrs + 1) * sizeof(pd_attr_t));
+	if (p_ctx->p_attr_table == NULL) {
+
+		PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: "
+				  "pd_malloc(p_attr_table) failed");
+
+		p_ctx->num_attrs = 0;
+		return PD_ERR_NOMEM;
+	}
+
+	pd_memset(p_ctx->p_attr_table, 0, (p_ctx->num_attrs + 1) *
+		sizeof(pd_attr_t));
+
+	p_table = (unsigned char *)p_ctx->p_attr_table;
+
+	/* .................................................................. */
+	if (num_static_attrs > 0) {
+
+		num_attrs = sdvo_get_static_attrs(p_ctx, (pd_attr_t *)p_table);
+		if (num_attrs != num_static_attrs) {
+
+			PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: "
+					  "sdvo_get_static_attrs() returned %ld entries, expected %ld",
+					  num_attrs, num_static_attrs);
+
+		} else {
+
+			p_table += (num_static_attrs * sizeof(pd_attr_t));
+		}
+	}
+
+	if (num_pwr_seq_attrs > 0) {
+
+		num_attrs = sdvo_get_panel_pwr_seq_attrs(p_ctx,
+			(pd_range_attr_t *)p_table);
+		if (num_attrs != num_pwr_seq_attrs) {
+
+			PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: "
+					  "sdvo_get_panel_pwr_seq_attrs() returned %ld entries,"
+					  " expected %ld", num_attrs, num_pwr_seq_attrs);
+
+		} else {
+
+			p_table += (num_pwr_seq_attrs * sizeof(pd_attr_t));
+		}
+	}
+
+	if (num_range_attrs > 0) {
+
+		num_attrs = sdvo_get_range_attrs(p_ctx, (pd_range_attr_t *)p_table);
+		if (num_attrs != num_range_attrs) {
+
+			PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: "
+					  "sdvo_get_range_attrs() returned %ld entries,"
+					  " expected %ld", num_attrs, num_range_attrs);
+
+		} else {
+
+			p_table += (num_range_attrs * sizeof(pd_attr_t));
+		}
+	}
+
+	if (num_bool_attrs > 0) {
+
+		num_attrs = sdvo_get_bool_attrs(p_ctx, (pd_bool_attr_t *)p_table);
+		if (num_attrs != num_bool_attrs) {
+
+			PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: "
+					  "sdvo_get_bool_attrs() returned %ld entries, expected %ld",
+					  num_attrs, num_bool_attrs);
+
+		} else {
+
+			p_table += (num_bool_attrs * sizeof(pd_attr_t));
+		}
+	}
+
+	if (num_list_attrs > 0) {
+
+		num_attrs = sdvo_get_list_attrs(p_ctx, (pd_list_attr_t *)p_table);
+		if (num_attrs != num_list_attrs) {
+
+			PD_ERROR("sdvo: Error ! sdvo_init_attribute_table: "
+					  "sdvo_get_list_attrs() returned %ld entries, expected %ld",
+					  num_attrs, num_list_attrs);
+		} else {
+
+			p_table += (num_list_attrs * sizeof(pd_attr_t));
+		}
+	}
+	return PD_SUCCESS;
+}
+
+int sdvo_is_multi_display_device(sdvo_device_context_t *p_ctx)
+{
+	unsigned short i;
+	int b_multi_display = 0;
+
+	for (i = 0; i < 16; i++) {
+		if (p_ctx->dev_cap.output.flags & (1 << i)) {
+			if (!b_multi_display) {
+				b_multi_display = 1;
+			} else {
+				return TRUE;
+			}
+		}
+	}
+	return 0;
+}
+
+#ifndef CONFIG_MICRO
+static sdvo_status_t sdvo_reset_encoder(sdvo_device_context_t *p_ctx)
+{
+	sdvo_status_t ret_stat;
+	if ((p_ctx->dev_cap.vendor_id == VENDOR_ID_OKI)
+		&& (p_ctx->dev_cap.device_id == 0x1)){
+
+		pd_attr_t *p_attr_temp =
+			pd_get_attr(p_ctx->p_attr_table, p_ctx->num_attrs,
+				PD_ATTR_ID_RB_SWAP_MODE, 0 );
+
+		if (!(p_attr_temp->flags & PD_ATTR_FLAG_NEED_RESTORE)){
+			sdvo_query_static_attr(p_ctx, p_attr_temp);
+			p_attr_temp->flags |= PD_ATTR_FLAG_NEED_RESTORE;
+		}
+
+
+
+		sdvo_reset (p_ctx);
+		pd_usleep(10);
+		sdvo_reset (p_ctx);
+
+		ret_stat = sdvo_reset (p_ctx);
+
+	} else {
+		ret_stat = sdvo_reset (p_ctx);
+	}
+
+	return ret_stat;
+}
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.h b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.h
new file mode 100644
index 0000000..b06fc73b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/pal/sdvo/sdvo_port.h
@@ -0,0 +1,71 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: sdvo_port.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Main include file for SDVO port driver
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _SDVO_PORT_H_
+#define _SDVO_PORT_H_
+
+#include <config.h>
+#include <igd_pd.h>
+#include <pd.h>
+#include <pd_print.h>
+
+/* #define DEBUG 0 */
+
+/*  ............................................................................ */
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(p) (sizeof(p)/sizeof((p)[0]))
+#endif
+
+#include "sdvo_intf.h"
+#include "sdvo_attr.h"
+#include "sdvo_hdmi.h"
+
+/*  ............................................................................ */
+#ifndef CONFIG_MICRO
+#define	SDVO_GET_ATTR_NAME(p_attr)	p_attr->name
+#else
+#define SDVO_GET_ATTR_NAME(p_attr)	""
+#endif
+
+/*  ............................................................................ */
+unsigned char sdvo_write_i2c_reg(sdvo_device_context_t *p_Ctx, i2c_reg_t offset,
+	i2c_reg_t value);
+unsigned char sdvo_read_i2c_reg(sdvo_device_context_t *p_Ctx, unsigned char offset,
+	i2c_reg_t *p_Value);
+
+
+int sdvo_is_multi_display_device(sdvo_device_context_t *p_ctx);
+
+/*OKI Workaround*/
+#define	VENDOR_ID_OKI 0x81
+
+
+#endif  /*  _SDVO_PORT_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/appcontext_dispatch.h b/drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/appcontext_dispatch.h
new file mode 100644
index 0000000..3da03e0
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/appcontext_dispatch.h
@@ -0,0 +1,56 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: appcontext_dispatch.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+
+#ifndef _APPCONTEXT_DISPATCH_H
+#define _APPCONTEXT_DISPATCH_H
+
+typedef struct _appcontext_table_t {
+	unsigned long appcontext_id;
+	unsigned long logical_context_offset;
+} appcontext_table_t;
+
+typedef struct _appcontext_dispatch_t {
+	int (*appcontext_init)(igd_context_t *context);
+	void (*appcontext_shutdown)(igd_context_t *context);
+	igd_appcontext_h (*appcontext_alloc)(igd_display_h display_handle,
+		int priority, unsigned int flags);
+	void (*appcontext_free)(igd_display_h display_handle,
+		int priority, igd_appcontext_h context_handle);
+	appcontext_table_t *table;
+} appcontext_dispatch_t;
+
+
+extern appcontext_dispatch_t appcontext_dispatch_plb;
+
+extern appcontext_dispatch_t *appcontext_dispatch;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/igd_appcontext.c b/drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/igd_appcontext.c
new file mode 100755
index 0000000..c5ee392
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/appcontext/cmn/igd_appcontext.c
@@ -0,0 +1,148 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_appcontext.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file inplements the IGD interface for the context module. This
+ *  includes functions that handle hardware context swapping and state
+ *  variable management.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.state
+
+#include <config.h>
+
+#include <igd_appcontext.h>
+#include <igd_gmm.h>
+#include <igd_mode.h>
+#include <igd_errno.h>
+
+#include <intelpci.h>
+#include <context.h>
+#include <mode.h>
+#include <dispatch.h>
+
+#include <io.h>
+
+#include "appcontext_dispatch.h"
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+static dispatch_table_t appcontext_dispatch_list[] = {
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &appcontext_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &appcontext_dispatch_plb},
+#endif
+	{0, NULL}
+};
+
+/* Not static: cores may need it */
+appcontext_dispatch_t *appcontext_dispatch = NULL;
+
+/*!
+ *
+ * @param display_handle
+ * @param priority
+ * @param flags
+ *
+ * @return appcontext_h
+ */
+igd_appcontext_h igd_appcontext_alloc(
+	igd_display_h display_handle,
+	int priority,
+	unsigned int flags)
+{
+
+	return appcontext_dispatch->appcontext_alloc(
+		display_handle, priority, flags);
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param priority
+ * @param appcontext_handle
+ *
+ * @return void
+ */
+void igd_appcontext_free(
+	igd_display_h display_handle,
+	int priority,
+	igd_appcontext_h appcontext_handle)
+{
+	appcontext_dispatch->appcontext_free(
+		display_handle, priority, appcontext_handle);
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+void appcontext_shutdown(igd_context_t *context)
+{
+
+	EMGD_DEBUG("appcontext_shutdown Entry");
+
+	if (appcontext_dispatch != NULL)
+	{
+		appcontext_dispatch->appcontext_shutdown(context);
+	}
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+int appcontext_init(igd_context_t *context)
+{
+
+	EMGD_DEBUG("appcontext_init Entry");
+
+	appcontext_dispatch = (appcontext_dispatch_t *)dispatch_acquire(context,
+		appcontext_dispatch_list);
+	if(!appcontext_dispatch) {
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Hook up top level dispatch table */
+	context->dispatch.appcontext_alloc = appcontext_dispatch->appcontext_alloc;
+	context->dispatch.appcontext_free = appcontext_dispatch->appcontext_free;
+
+	/* Hook up optional inter-module functions */
+	context->mod_dispatch.appcontext_shutdown = appcontext_shutdown;
+
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/state/appcontext/plb/appcontext_plb.c b/drivers/gpu/drm/emgd/emgd/state/appcontext/plb/appcontext_plb.c
new file mode 100644
index 0000000..568e24f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/appcontext/plb/appcontext_plb.c
@@ -0,0 +1,207 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: appcontext_plb.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file inplements the IGD interface for the context module. This
+ *  includes functions that handle hardware context swapping and state
+ *  variable management.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <io.h>
+
+#include <igd_appcontext.h>
+#include <igd_gmm.h>
+#include <igd_mode.h>
+#include <igd_render.h>
+#include <igd_errno.h>
+
+#include <intelpci.h>
+#include <memory.h>
+#include <mode_access.h>
+#include <context.h>
+#include <state2d.h>
+#include <mode.h>
+#include <dispatch.h>
+#include <utils.h>
+
+#include <plb/appcontext.h>
+#include <plb/instr.h>
+#include <plb/state3d_plb.h>
+
+#include "../cmn/appcontext_dispatch.h"
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+/*
+ * Prototype these here because they are only needed by appcontext.c
+ * and putting them in state2d.h would cause a circular dependency.
+ */
+
+static igd_appcontext_h igd_appcontext_alloc_plb(
+	igd_display_h display_handle,
+	int priority,
+	unsigned int flags);
+static void igd_appcontext_free_plb(
+	igd_display_h display_h,
+	int priority,
+	igd_appcontext_h appcontext_handle);
+static int appcontext_init_plb(igd_context_t *context);
+static void appcontext_shutdown_plb(igd_context_t *context);
+
+static appcontext_table_t appcontext_table_plb[] = {
+	{0, 0}
+};
+
+appcontext_dispatch_t appcontext_dispatch_plb = {
+	appcontext_init_plb,
+	appcontext_shutdown_plb,
+	igd_appcontext_alloc_plb,
+	igd_appcontext_free_plb,
+	appcontext_table_plb
+};
+
+/*!
+ *
+ * @param appcontext
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_NOMEM on failure
+ */
+static int state2d_init_plb(appcontext_t *appcontext)
+{
+
+	appcontext->state2d = OS_ALLOC(sizeof(state2d_t));
+	if(!appcontext->state2d) {
+		return -IGD_ERROR_NOMEM;
+	}
+
+	STATE2D(appcontext)->clip_status = IGD_CLIP_DISABLED;
+	STATE2D(appcontext)->dest_rect.x1 = 0;
+	STATE2D(appcontext)->dest_rect.y1 = 0;
+	STATE2D(appcontext)->dest_rect.x2 = 0;
+	STATE2D(appcontext)->dest_rect.y2 = 0;
+
+	return 0;
+}
+
+/*!
+ *
+ * @param appcontext
+ *
+ * @return void
+ */
+static int state2d_shutdown_plb(appcontext_t *appcontext)
+{
+	if(appcontext->state2d) {
+		OS_FREE(appcontext->state2d);
+	}
+
+	appcontext->state2d = NULL;
+
+	return 0;
+}
+
+/*!
+ *
+ * @param display_handle
+ * @param priority
+ * @param flags
+ *
+ * @return appcontext_h on success
+ * @return NULL on failure
+ */
+static igd_appcontext_h igd_appcontext_alloc_plb(
+	igd_display_h display_handle,
+	int priority,
+	unsigned int flags)
+{
+	appcontext_t *appcontext;
+
+	/* A proper appcontext is not required for Poulsbo
+	 * as a result, we'll allocate it and have it set to NULL
+	 */
+	appcontext = OS_ALLOC(sizeof(appcontext_t));
+	if (!appcontext) {
+		EMGD_ERROR ("OS_ALLOC appcontext failed\n");
+		return NULL;
+	}
+
+	OS_MEMSET(appcontext, 0, sizeof(appcontext_t));
+	appcontext->state2d = NULL;
+
+	if (flags & IGD_CONTEXT_STATE_2D) {
+		if (state2d_init_plb(appcontext)) {
+			igd_appcontext_free_plb(display_handle, priority, appcontext);
+			return NULL;
+		}
+	}
+
+	return appcontext;
+
+}
+
+static void igd_appcontext_free_plb(
+	igd_display_h display_h,
+	int priority,
+	igd_appcontext_h appcontext_handle)
+{
+
+	appcontext_t *appcontext = (appcontext_t *)appcontext_handle;
+
+	/* Free the appcontext struct */
+	if (appcontext) {
+		state2d_shutdown_plb(appcontext);
+
+		OS_FREE(appcontext_handle);
+	}
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return 0
+ */
+static int appcontext_init_plb(igd_context_t *context)
+{
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void appcontext_shutdown_plb(igd_context_t *context)
+{
+	return;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/state/power/cmn/igd_pwr.c b/drivers/gpu/drm/emgd/emgd/state/power/cmn/igd_pwr.c
new file mode 100644
index 0000000..2cfd775
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/power/cmn/igd_pwr.c
@@ -0,0 +1,305 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_pwr.c
+ * $Revision: 1.15 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.power
+
+#include <config.h>
+#include <io.h>
+
+#include <igd_init.h>
+#include <igd_errno.h>
+#include <igd_pwr.h>
+#include <igd_mode.h>
+
+#include <module_init.h>
+#include <mode.h>
+#include <dsp.h>
+#include <intelpci.h>
+#include <dispatch.h>
+#include "pwr_dispatch.h"           /* contains the power dispatch functions */
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+static dispatch_table_t pwr_dispatch_list[] = {
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &pwr_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &pwr_dispatch_plb},
+#endif
+	{0, NULL}
+};
+
+typedef struct _power_context {
+	pwr_dispatch_t *dispatch;
+	void *power_state;
+} power_context_t;
+
+static power_context_t power_context;
+
+static int igd_pwr_query(igd_driver_h driver_handle, unsigned int power_state)
+{                                                           /* igd_pwr_query */
+	return power_context.dispatch->pwr_query(power_state);
+}                                                           /* igd_pwr_query */
+
+/*!
+ * This function alters the power state of the graphics device.  This
+ * function saves and restores registers as necessary and changes the
+ * power states of the mode and overlay module.
+ *
+ * This function will convert a request for D2 to a request for D1.
+ *
+ * @param driver_handle driver context
+ * @param dwPowerState power state to set the device to.
+ *
+ * @return < 0 on Error
+ * @return 0 on Success
+ */
+static int igd_pwr_alter(igd_driver_h driver_handle, unsigned int dwPowerState)
+{                                                           /* igd_pwr_alter */
+	unsigned char *mmio;
+	int           retval   = 0;
+	igd_context_t *context = (igd_context_t *) driver_handle;
+
+	EMGD_DEBUG("in igd_pwr_alter");
+
+	EMGD_DEBUG("Power State requested: 0x%x", dwPowerState);
+
+	if(context == NULL) {
+		EMGD_ERROR("In igd_pwr_alter:-Device context is null");
+		return -IGD_ERROR_INVAL;
+	}
+
+	if(power_context.power_state == NULL) {
+		EMGD_DEBUG("In igd_pwr_alter:- Memory not allocated yet."
+				" Power Module Not Initialised");
+		return -IGD_ERROR_INVAL;
+	}
+
+	mmio = context->device_context.virt_mmadr;
+
+	if(context->device_context.power_state == dwPowerState ) {
+		EMGD_DEBUG("Already in the present state");
+		return -IGD_ERROR_INVAL;
+	}
+
+	switch(dwPowerState) {
+	case IGD_POWERSTATE_D0:
+		/* Do any chipset specific power management */
+		retval = power_context.dispatch->pwr_set(context, dwPowerState);
+
+		/* restore the registers */
+		retval = context->mod_dispatch.reg_restore(context,
+			power_context.power_state);
+		if (retval) {
+			return retval;
+		}
+
+		/* Officially change the power state after registers are restored */
+		context->device_context.power_state = IGD_POWERSTATE_D0;
+
+		/* Power up mode module */
+		if(context->mod_dispatch.mode_pwr) {
+			retval = context->mod_dispatch.mode_pwr(context, dwPowerState);
+			if (retval) {
+				return retval;
+			}
+		}
+
+		/* enable overlay */
+		if(context->mod_dispatch.overlay_pwr) {
+			context->mod_dispatch.overlay_pwr(context, IGD_POWERSTATE_D0);
+		}
+
+		/* enable msvdx */
+		if(context->mod_dispatch.msvdx_pwr) {
+			context->mod_dispatch.msvdx_pwr(context, IGD_POWERSTATE_D0);
+		}
+		break;
+
+	case IGD_POWERSTATE_D1:
+	case IGD_POWERSTATE_D2:
+		/* Standby - ACPI S1 */
+
+		if (IGD_POWERSTATE_D2 == dwPowerState) {
+			dwPowerState = IGD_POWERSTATE_D1;
+		}
+
+
+		/* Turn off the msvdx */
+		if(context->mod_dispatch.msvdx_pwr) {
+			context->mod_dispatch.msvdx_pwr(context, dwPowerState);
+		}
+
+		/* Turn off the overlay */
+		if(context->mod_dispatch.overlay_pwr) {
+			context->mod_dispatch.overlay_pwr(context, dwPowerState);
+		}
+
+		/* save registers */
+		retval = context->mod_dispatch.reg_save(context,
+			power_context.power_state);
+		if (retval) {
+			return retval;
+		}
+
+		/* Change the state of the device to Dx.  This is required so the
+		 * plane/pipe/port all use this.  This needs to happen after "reg_save"
+		 * because igd_sync will timeout if power state is not D0
+		 */
+		context->device_context.power_state = dwPowerState;
+
+		if(context->mod_dispatch.mode_pwr) {
+			retval = context->mod_dispatch.mode_pwr(context, dwPowerState);
+			if (retval) {
+				return retval;
+			}
+		}
+
+		/* Do any chipset specific power management */
+		retval = power_context.dispatch->pwr_set(context, dwPowerState);
+
+		break;
+
+	case IGD_POWERSTATE_D3:
+		/* Suspend to memory - ACPI S3 */
+
+		/* diable msvdx */
+		if(context->mod_dispatch.msvdx_pwr) {
+			context->mod_dispatch.msvdx_pwr(context, dwPowerState);
+		}
+
+		/* disable overlay */
+		if(context->mod_dispatch.overlay_pwr) {
+			context->mod_dispatch.overlay_pwr(context, dwPowerState);
+		}
+
+		/* save registers */
+		retval= context->mod_dispatch.reg_save(context,
+			power_context.power_state);
+		if (retval) {
+			return retval;
+		}
+
+		/* Change the state of the device to Dx.  This is required so the
+		 * plane/pipe/port all use this.  This needs to happen after "reg_save"
+		 * because igd_sync will timeout if power state is not D0
+		 */
+		context->device_context.power_state = dwPowerState;
+
+		/* Mode module Power */
+		if(context->mod_dispatch.mode_pwr) {
+			retval = context->mod_dispatch.mode_pwr(context, dwPowerState);
+			if (retval) {
+				return retval;
+			}
+		}
+
+
+		/* Do any chipset specific power management */
+		retval = power_context.dispatch->pwr_set(context, dwPowerState);
+
+		break;
+
+	default:
+		/* state undefined */
+		EMGD_ERROR("In igd_pwr_alter:-Undefined Power State");
+		break;
+
+	}
+
+	return retval;
+}                                                           /* igd_pwr_alter */
+
+/*!
+ * This function should be called from context manager.
+ * To delete the memory allocated ealier.
+ *
+ * @param context
+ *
+ * @return void
+ */
+static void _pwr_shutdown(igd_context_t *context)
+{
+
+	if(power_context.power_state) {
+		context->mod_dispatch.reg_free(context, power_context.power_state);
+	}
+	power_context.power_state = NULL;
+	context->device_context.power_state = IGD_POWERSTATE_UNDEFINED;
+	return;
+}
+
+/*!
+ * This function should only be called only once from context manager .
+ * It initializes the power module.
+ *
+ * @param context
+ *
+ * @return < 0 on Error
+ * @return 0 on Success
+ */
+int _pwr_init(igd_context_t *context)
+{                                                               /* _pwr_init */
+	EMGD_ASSERT(context, "Null Context", -IGD_ERROR_INVAL);
+
+	/* Hook up the IGD dispatch table entires for power */
+	context->dispatch.pwr_alter = igd_pwr_alter;
+	context->dispatch.pwr_query = igd_pwr_query;
+
+	/* Inter-module dispatch */
+	context->mod_dispatch.pwr_shutdown = _pwr_shutdown;
+
+	power_context.dispatch = (pwr_dispatch_t *)dispatch_acquire(context,
+		pwr_dispatch_list);
+
+	if(!power_context.dispatch) {
+		return -IGD_ERROR_NODEV;
+	}
+
+	/* Initialize chipset specific default power behavior */
+	power_context.dispatch->pwr_init(context);
+
+	power_context.power_state = context->mod_dispatch.reg_alloc(context,
+		IGD_REG_SAVE_VGA |
+		IGD_REG_SAVE_DAC | IGD_REG_SAVE_MMIO | IGD_REG_SAVE_RB |
+		IGD_REG_SAVE_MODE | IGD_REG_SAVE_3D );
+
+	if(!power_context.power_state) {
+		return -IGD_ERROR_NOMEM;
+	}
+
+	return 0;
+}                                                               /* _pwr_init */
+
diff --git a/drivers/gpu/drm/emgd/emgd/state/power/cmn/pwr_dispatch.h b/drivers/gpu/drm/emgd/emgd/state/power/cmn/pwr_dispatch.h
new file mode 100644
index 0000000..34043ba
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/power/cmn/pwr_dispatch.h
@@ -0,0 +1,52 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pwr_dispatch.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the protocols for the plaform-specific dispatch
+ *  functions that the power module uses.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _PWR_DISPATCH_H
+#define _PWR_DISPATCH_H
+
+#include <context.h>
+
+typedef struct _pwr_dispatch {
+	/* Queries support for power_state
+	 * ret:  0 if power_state is supported, <0 otherwise
+	 */
+	int (*pwr_query)(unsigned int power_state);
+	/* Dispatch function to set device dependent part of set power */
+	int (*pwr_set)(igd_context_t *context, unsigned int power_state);
+	/* Dispatch function to set init time device dependent power settings */
+	int (*pwr_init)(igd_context_t *context);
+} pwr_dispatch_t;
+
+extern pwr_dispatch_t pwr_dispatch_plb;
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/emgd/state/power/plb/pwr_plb.c b/drivers/gpu/drm/emgd/emgd/state/power/plb/pwr_plb.c
new file mode 100644
index 0000000..22d3280
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/power/plb/pwr_plb.c
@@ -0,0 +1,123 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pwr_plb.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+/* copied from napa */
+
+#include <io.h>
+#include "igd_pwr.h"
+#include "../cmn/pwr_dispatch.h"
+#include <plb/regs.h>
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+static int pwr_query_full_plb(unsigned int power_state);
+static int pwr_set_plb(igd_context_t *context, unsigned int power_state);
+static int pwr_init_plb(igd_context_t *context);
+
+pwr_dispatch_t pwr_dispatch_plb = {
+	pwr_query_full_plb,
+	pwr_set_plb,
+	pwr_init_plb,
+};
+
+/*!
+ * This function returns "0" for all ACPI system states.
+ *
+ * @param power_state the requested power state
+ *
+ * @return 0
+ */
+static int pwr_query_full_plb(unsigned int power_state)
+{                                                          /* pwr_query_full */
+	switch( power_state ) {
+
+	case IGD_ADAPTERPOWERSTATE_ON:
+	case IGD_ADAPTERPOWERSTATE_STANDBY:
+	case IGD_ADAPTERPOWERSTATE_OFF:
+	case IGD_ADAPTERPOWERSTATE_SUSPEND:
+	case IGD_ADAPTERPOWERSTATE_HIBERNATE:
+	default:
+		return 0;
+		break;
+	}
+}                                                          /* pwr_query_full */
+
+/*!
+ *
+ * @patam context
+ * @param power_state the requested power state
+ *
+ * @return 0
+ */
+static int pwr_set_plb(igd_context_t *context, unsigned int power_state)
+{
+	unsigned char *mmio;
+
+	mmio = context->device_context.virt_mmadr;
+
+	switch (power_state) {
+	case IGD_POWERSTATE_D0:
+		break;
+	case IGD_POWERSTATE_D1:
+		break;
+	case IGD_POWERSTATE_D2:
+		break;
+	case IGD_POWERSTATE_D3:
+		break;
+
+	default:
+		break;
+	}
+	return 0;
+}
+
+/*!
+ *
+ * @param context
+ *
+ * @return 0
+ */
+static int pwr_init_plb(igd_context_t *context)
+{
+	unsigned char *mmio;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* Reset CLKGATECTL and CLKGATECTLOVR */
+	EMGD_WRITE32(0x1111111, mmio + PSB_CR_CLKGATECTL);
+	EMGD_WRITE32(0, mmio + PSB_CR_CLKGATECTL + 8);
+
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg.c b/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg.c
new file mode 100644
index 0000000..e030f0d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg.c
@@ -0,0 +1,451 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: reg.c
+ * $Revision: 1.16 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the implementation file for the register module's main API's called
+ *  by external devices.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.state
+
+#include <io.h>
+
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_errno.h>
+
+#include <context.h>
+#include <utils.h>
+#include <dsp.h>
+#include <mode.h>
+#include <memory.h>
+#include <utils.h>
+
+#include <intelpci.h>
+#include <dispatch.h>
+
+#include "reg_dispatch.h"
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+typedef struct _reg_context {
+	unsigned long flags;     /* IGD_DRIVER_SAVE_RESTORE */
+	reg_dispatch_t *dispatch;
+	reg_buffer_t *device_state;
+	void *gmm_state;
+} reg_context_t;
+
+extern emgd_drm_config_t config_drm;
+static reg_context_t reg_ctx[1];
+static reg_buffer_t *console_state;
+void *console_gmm_state;
+static reg_buffer_t *misc_state;
+void *misc_gmm_state;
+
+static dispatch_table_t reg_dispatch_list[] = {
+
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &reg_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &reg_dispatch_tnc},
+#endif
+	{0, NULL}
+};
+
+void *reg_alloc(igd_context_t *context,
+	unsigned long flags)
+{
+	return (void *)reg_ctx->dispatch->reg_alloc(context, flags,
+		reg_ctx->dispatch->platform_context);
+}
+
+void reg_free(igd_context_t *context,
+	void *reg_set)
+{
+	reg_ctx->dispatch->reg_free(context, (reg_buffer_t *)reg_set,
+		reg_ctx->dispatch->platform_context);
+}
+
+int reg_save(igd_context_t *context,
+	void *reg_set)
+{
+	return reg_ctx->dispatch->reg_save(context, (reg_buffer_t *)reg_set,
+		reg_ctx->dispatch->platform_context);
+}
+
+int reg_restore(igd_context_t *context,
+	void *reg_set)
+{
+	return reg_ctx->dispatch->reg_restore(context, (reg_buffer_t *)reg_set,
+		reg_ctx->dispatch->platform_context);
+}
+
+void reg_crtc_lut_get(igd_context_t *context,
+	void *emgd_crtc)
+{
+	reg_ctx->dispatch->reg_crtc_lut_get(context, (emgd_crtc_t *)emgd_crtc);
+}
+
+void reg_crtc_lut_set(igd_context_t *context,
+	void *emgd_crtc)
+{
+	reg_ctx->dispatch->reg_crtc_lut_set(context, (emgd_crtc_t *)emgd_crtc);
+}
+
+/*!
+ * This function calls reg_save() to save the state of the graphics engine
+ * and then reg_restore to restore the previous state.
+ *
+ * @param driver_handle  our driver context
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int igd_driver_save_restore(igd_driver_h driver_handle,
+	unsigned long flags)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	reg_buffer_t *reg_state;
+	short restored = 0;
+
+	EMGD_ASSERT(context, "Null driver_handle!\n", -IGD_ERROR_INVAL);
+
+	/*
+	 * If the gmm_state exists, the registers are not setup, so we should not
+	 * try to save the GTT, the ring buffer and appcontext, or it will cause a
+	 * sync which will not be fulfilled, so it will wait forever.  We can
+	 * however save everything else.  If the gmm_state is null, then everything
+	 * is still setup and we can save everything including the GTT, ring buffer
+	 * and appcontext.
+	 */
+	if (reg_ctx->gmm_state) {
+		flags = flags & ~IGD_REG_SAVE_GTT & ~IGD_REG_SAVE_RB;
+	}
+
+	/* Save current state. We have to save the current state first before
+	 * restoring the GART bindings. Otherwise we will end up restoring wrong
+	 * pages when return back to the graphic mode console */
+	reg_state = reg_ctx->dispatch->reg_alloc(context, flags,
+		reg_ctx->dispatch->platform_context);
+	reg_ctx->dispatch->reg_save(context, reg_state,
+		reg_ctx->dispatch->platform_context);
+
+	/* Restore GART bindings, if saved */
+	if (reg_ctx->gmm_state && context->mod_dispatch.gmm_restore) {
+		context->mod_dispatch.gmm_restore(context, reg_ctx->gmm_state);
+		reg_ctx->gmm_state = NULL;
+		restored = 1;
+	}
+
+	/* restore previously saved state */
+
+	/*
+	 * NAPA class seems to work much better all the display stuff is
+	 * turned off prior to restoring the registers.
+	 */
+	context->mod_dispatch.mode_reset_plane_pipe_ports(context);
+
+
+	reg_ctx->dispatch->reg_restore(context, reg_ctx->device_state,
+		reg_ctx->dispatch->platform_context);
+	reg_ctx->dispatch->reg_free(context, reg_ctx->device_state,
+		reg_ctx->dispatch->platform_context);
+
+	reg_ctx->device_state = reg_state;
+
+	/* Free GART bindings, if not already saved */
+	if (!restored && !reg_ctx->gmm_state &&
+		context->mod_dispatch.gmm_save) {
+		context->mod_dispatch.gmm_save(context, &(reg_ctx->gmm_state));
+	}
+
+	return 0;
+}
+
+/*!
+ * This function calls reg_save() to save the state of the register
+ *
+ * @param driver_handle our driver context
+ * @param flags specifies which states to save
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int igd_driver_save(igd_driver_h driver_handle,
+	const unsigned long flags)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	reg_buffer_t *reg_state;
+	reg_buffer_t **reg_location = NULL;
+
+	EMGD_ASSERT(context, "Null driver_handle!\n", -IGD_ERROR_INVAL);
+
+	switch (flags & IGD_REG_SAVE_TYPE_MASK) {
+	case IGD_REG_SAVE_TYPE_REG:
+		reg_location = &reg_ctx->device_state;
+		break;
+	case IGD_REG_SAVE_TYPE_CON:
+		reg_location = &console_state;
+		break;
+	case IGD_REG_SAVE_TYPE_MISC:
+		reg_location = &misc_state;
+		break;
+	default:
+		EMGD_ERROR("Called igd_driver_save without a valid save flag.");
+		return 0;
+	}
+
+	if (*reg_location) {
+		reg_ctx->dispatch->reg_free(context, *reg_location,
+			reg_ctx->dispatch->platform_context);
+	}
+
+	/* Save current state */
+	reg_state = reg_ctx->dispatch->reg_alloc(context, flags,
+		reg_ctx->dispatch->platform_context);
+	reg_ctx->dispatch->reg_save(context, reg_state,
+		reg_ctx->dispatch->platform_context);
+
+	*reg_location = reg_state;
+
+	/* Free GART bindings, if not already saved */
+	if ((flags & IGD_REG_SAVE_TYPE_REG) &&
+		!reg_ctx->gmm_state &&
+		context->mod_dispatch.gmm_save) {
+		context->mod_dispatch.gmm_save(context, &(reg_ctx->gmm_state));
+	}
+	return 0;
+}
+/* igd_driver_save */
+
+/*!
+ * This function calls reg_restore() to restore previously saved
+ *
+ * @param driver_handle our driver context
+ *
+ * @return 0 on success
+ * @return -IGD_INVAL on failure
+ */
+static int igd_driver_restore(igd_driver_h driver_handle,
+	const unsigned long flags)
+{
+	igd_context_t *context;
+	reg_buffer_t *reg_state = NULL;
+
+	EMGD_ASSERT(driver_handle, "Null driver_handle!\n", -IGD_ERROR_INVAL);
+
+	context = (igd_context_t *)driver_handle;
+	switch (flags & IGD_REG_SAVE_TYPE_MASK) {
+	case IGD_REG_SAVE_TYPE_REG:
+		reg_state = reg_ctx->device_state;
+		break;
+	case IGD_REG_SAVE_TYPE_CON:
+		reg_state = console_state;
+		break;
+	case IGD_REG_SAVE_TYPE_MISC:
+		reg_state = misc_state;
+		break;
+	default:
+		EMGD_ERROR("Not a valida restore flag specified.");
+		return 0;
+	}
+
+	/*
+	 * NAPA class seems to work much better all the display stuff is
+	 * turned off prior to restoring the registers.
+	 * context->mod_dispatch.mode_reset_plane_pipe_ports(context);
+	 */
+	context->mod_dispatch.mode_reset_plane_pipe_ports(context);
+
+	/* restore previously saved state */
+	if (reg_state) {
+		reg_ctx->dispatch->reg_restore(context, reg_state,
+			reg_ctx->dispatch->platform_context);
+		reg_ctx->dispatch->reg_free(context, reg_state,
+			reg_ctx->dispatch->platform_context);
+
+		/* Restore GART bindings, if saved */
+		if ((flags & IGD_REG_SAVE_TYPE_REG) &&
+			reg_ctx->gmm_state &&
+			context->mod_dispatch.gmm_restore) {
+			context->mod_dispatch.gmm_restore(context, reg_ctx->gmm_state);
+			reg_ctx->gmm_state = NULL;
+		}
+	} else {
+		EMGD_ERROR("Previous state was not saved, so can't restore.");
+	}
+
+	return 0;
+}
+
+/*!
+ *
+ * @param id
+ * @param state
+ * @param flags
+ *
+ * @return 0 on success
+ * @return -IGD_ERROR_INVAL on failure
+ */
+static int reg_get_mod_state(reg_state_id_t id, module_state_h **state,
+	unsigned long **flags)
+{
+	if(!reg_ctx->device_state && !console_state) {
+		return 0;
+	}
+
+	switch(id) {
+	case REG_MODE_STATE_REG:
+		*flags = &reg_ctx->device_state->flags;
+			if((**flags & IGD_REG_SAVE_MODE)){
+				*state = &reg_ctx->device_state->mode_buffer;
+			} else {
+				state = NULL;
+			}
+		break;
+	case REG_MODE_STATE_CON:
+		*flags = &console_state->flags;
+			if((**flags & IGD_REG_SAVE_MODE)){
+				*state = &console_state->mode_buffer;
+			} else {
+				state = NULL;
+			}
+		break;
+	default:
+		state = NULL;
+		return -IGD_ERROR_INVAL;
+	}
+
+	return 0;
+}
+
+/*!
+ * Free any memory allocated for registry state info.
+ *
+ * @param context
+ *
+ * @return void
+ */
+void _reg_shutdown(igd_context_t *context)
+{
+	reg_buffer_t *restore_state;
+	EMGD_TRACE_ENTER;
+
+	if (!config_drm.init) {
+		restore_state = reg_ctx[0].device_state;
+	} else {
+		if (config_drm.kms) {
+			restore_state = console_state;
+		} else {
+			restore_state = console_state;
+			//restore_state = reg_ctx[0].device_state;
+		}
+	}
+
+	if(restore_state) {
+		context->mod_dispatch.mode_reset_plane_pipe_ports(context);
+
+		EMGD_DEBUG("Restoring register values prior to exit...");
+		reg_ctx->dispatch->reg_restore(context, restore_state,
+			reg_ctx->dispatch->platform_context);
+		reg_ctx->dispatch->reg_free(context, restore_state,
+			reg_ctx->dispatch->platform_context);
+
+	}
+	OS_MEMSET(reg_ctx, 0, sizeof(reg_context_t));
+
+	EMGD_TRACE_EXIT;
+}
+
+/*!
+ * Initialize the registry state context for registry save
+ * /restore operations.
+ *
+ * @param context
+ * @param flags
+ *
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int _reg_init(igd_context_t *context, unsigned long flags)
+{
+	reg_dispatch_t *dispatch;
+
+	EMGD_TRACE_ENTER;
+
+	dispatch = (reg_dispatch_t *)dispatch_acquire(context, reg_dispatch_list);
+	if(!dispatch) {
+		return -IGD_ERROR_NODEV;
+	}
+
+	OS_MEMSET(reg_ctx, 0, sizeof(reg_context_t));
+
+	reg_ctx->dispatch = dispatch;
+
+	/*
+	 * Hook up functions in dispatch table.
+	 */
+	context->dispatch.driver_save_restore   = igd_driver_save_restore;
+	context->dispatch.driver_save           = igd_driver_save;
+	context->dispatch.driver_restore        = igd_driver_restore;
+	context->mod_dispatch.reg_get_mod_state = reg_get_mod_state;
+	context->mod_dispatch.reg_alloc         = reg_alloc;
+	context->mod_dispatch.reg_free          = reg_free;
+	context->mod_dispatch.reg_save          = reg_save;
+	context->mod_dispatch.reg_restore       = reg_restore;
+	context->mod_dispatch.reg_shutdown      = _reg_shutdown;
+	context->mod_dispatch.reg_crtc_lut_get  = reg_crtc_lut_get;
+	context->mod_dispatch.reg_crtc_lut_set  = reg_crtc_lut_set;
+
+	if(flags & IGD_DRIVER_SAVE_RESTORE) {
+		reg_ctx->flags |= IGD_DRIVER_SAVE_RESTORE;
+		EMGD_DEBUG("Saving Device State");
+
+		reg_ctx->device_state = reg_ctx->dispatch->reg_alloc(context,
+			IGD_REG_SAVE_STATE|IGD_REG_SAVE_MODE|IGD_REG_SAVE_TYPE_REG,
+			reg_ctx->dispatch->platform_context);
+		reg_ctx->dispatch->reg_save(context, reg_ctx->device_state,
+			reg_ctx->dispatch->platform_context);
+	}
+
+	if (config_drm.init) {
+		console_state = reg_ctx->dispatch->reg_alloc(context,
+			IGD_REG_SAVE_STATE|IGD_REG_SAVE_MODE|IGD_REG_SAVE_TYPE_CON,
+			reg_ctx->dispatch->platform_context);
+		reg_ctx->dispatch->reg_save(context, console_state,
+			reg_ctx->dispatch->platform_context);
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg_dispatch.h b/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg_dispatch.h
new file mode 100644
index 0000000..985fa44
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg_dispatch.h
@@ -0,0 +1,73 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: reg_dispatch.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the implementation file for the register module's main API's called
+ *  by external devices.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _REG_DISPATCH_H
+#define _REG_DISPATCH_H
+
+#include <context.h>
+#include <drm_emgd_private.h>
+
+typedef struct _reg_buffer {
+	unsigned long flags;
+	module_state_h mode_buffer;
+	void *platform_buffer;
+}reg_buffer_t;
+
+/*
+ * This needs to be available for inter-module use so that the
+ * mode module can save state during it's init which happens after
+ * the reg module init.
+ */
+typedef struct _reg_dispatch {
+	reg_buffer_t *(*reg_alloc)(igd_context_t *context, unsigned long flags,
+		void *_platform_context);
+	void (*reg_free)(igd_context_t *context, reg_buffer_t *reg_set,
+		void *_platform_context);
+	int (*reg_save)(igd_context_t *context, reg_buffer_t *reg_set,
+		void *_platform_context);
+	int (*reg_restore)(igd_context_t *context, reg_buffer_t *reg_set,
+		void *_platform_context);
+	void (*reg_crtc_lut_get)(igd_context_t *context, emgd_crtc_t *emgd_crtc);
+	void (*reg_crtc_lut_set)(igd_context_t *context, emgd_crtc_t *emgd_crtc);
+	void *platform_context;
+} reg_dispatch_t;
+
+/*
+ * Reg dispatch data structure is in reg.h because it and the reg context
+ * needs to be available for initial reg save during mode module init.
+ */
+
+
+extern reg_dispatch_t reg_dispatch_plb;
+extern reg_dispatch_t reg_dispatch_tnc;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/state/reg/plb/reg_plb.c b/drivers/gpu/drm/emgd/emgd/state/reg/plb/reg_plb.c
new file mode 100644
index 0000000..d262ad6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/reg/plb/reg_plb.c
@@ -0,0 +1,1126 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: reg_plb.c
+ * $Revision: 1.14 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the implementation file for the register module's main API's called
+ *  by external devices.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.state
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_pwr.h>
+#include <igd_errno.h>
+
+#include <context.h>
+#include <utils.h>
+#include <dsp.h>
+#include <mode.h>
+#include <utils.h>
+#include <vga.h>
+#include <intelpci.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+
+#include "../cmn/reg_dispatch.h"
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+#define PLANE_LATCH_COUNT  4
+
+#define RING_BUFFER        0
+#define MMIO_MISC          1
+
+static reg_buffer_t *reg_alloc_plb(igd_context_t *context,
+	unsigned long flags, void *_platform_context);
+static void reg_free_plb(igd_context_t *context, reg_buffer_t *reg_buffer,
+	void *_platform_context);
+static int reg_save_plb(igd_context_t *context, reg_buffer_t *reg_buffer,
+	void *_platform_context);
+static int reg_restore_plb(igd_context_t *context, reg_buffer_t *reg_buffer,
+	void *_platform_context);
+static void reg_crtc_lut_get_plb(igd_context_t *context,
+    emgd_crtc_t *emgd_crtc);
+static void reg_crtc_lut_set_plb(igd_context_t *context,
+    emgd_crtc_t *emgd_crtc);
+
+/* GR registers being saved */
+static unsigned char gr_regs_plb[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x05, 0x06, 0x07, 0x08,
+	0x10, 0x11,
+	0x18,
+	0xff
+};
+#define GR_REG_COUNT sizeof(gr_regs_plb)
+
+/* SR registers being saved */
+static unsigned char sr_regs_plb[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x07,
+	0xff
+};
+#define SR_REG_COUNT sizeof(sr_regs_plb)
+
+/* AR registers being saved */
+static unsigned char ar_regs_plb[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x05, 0x06, 0x07, 0x08, 0x09,
+	0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+	0x0f, 0x10, 0x11, 0x12, 0x13,
+	0x14,
+	0xff,
+};
+#define AR_REG_COUNT sizeof(ar_regs_plb)
+
+/* CR registers being saved */
+static unsigned char cr_regs_plb[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x05, 0x06, 0x07, 0x08, 0x09,
+	0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+	0x0f, 0x10, 0x11, 0x12, 0x13,
+	0x14, 0x15, 0x16, 0x17, 0x18,
+	0xff
+};
+#define CR_REG_COUNT sizeof(cr_regs_plb)
+
+
+/* MMIO states register to be saved */
+static unsigned long mmio_regs_plb[] = {
+#if  MMIO_MISC
+	/* Fence Registers */
+	FENCE0, FENCE1, FENCE2, FENCE3, FENCE4, FENCE5, FENCE6, FENCE7,  // Fence
+	FENCE8, FENCE9, FENCE10, FENCE11, FENCE12, FENCE13, FENCE14, FENCE15,
+
+	/* GTT Control */
+	PGTBL_CTL,
+#endif
+
+	/* Memory Arbitration */
+	FW_BLC1, FW_BLC2, FW_BLC_SELF, MI_ARB_STATE, FW_BLC3, G_DEBUG,
+
+	/* Program Clocks */
+	VGA0_DIVISOR, VGA1_DIVISOR, VGA_PD,
+
+	FPA0, FPA1, FPB0, FPB1, P2D_CG_DIS, P3D_CG_DIS,
+
+	DPLLACNTR, DPLLBCNTR,
+
+	/* Enable Ports */
+	/* SDVO Port Enable */
+	SDVOCCNTR,
+	SDVOBCNTR,
+
+	/* Panel Power */
+	LVDS_PNL_PWR_CTL, PP_ON_DELAYS, PP_OFF_DELAYS, PP_DIVISOR,
+
+	/* Panel Fitting */
+	PFIT_CONTROL, PFIT_PGM_RATIOS, PFIT_AUTO_RATIOS, PFIT_INIT_PHASE,
+
+	/* Backlight Control */
+	BLC_PWM_CTL, BLM_HIST_CTL,
+
+	/* LVDS Port Enable */
+	LVDSCNTR,
+
+
+	/* Program Pipe A */
+	PIPEA_STAT, PIPEA_DISP_ARB_CTRL, HTOTAL_A, HBLANK_A, HSYNC_A, VTOTAL_A,
+	VBLANK_A, VSYNC_A, PIPEASRC, BCLRPAT_A,
+
+	/* Program Pipe B */
+	PIPEB_STAT, HTOTAL_B, HBLANK_B, HSYNC_B, VTOTAL_B,
+	VBLANK_B, VSYNC_B, PIPEBSRC, BCLRPAT_B,
+
+
+	/* Enable Pipes */
+	PIPEA_CONF, PIPEB_CONF,
+
+	/* Plane A */
+	DSPASTRIDE, DSPAKEYVAL, DSPAKEYMASK,
+
+	/* Plane B */
+	DSPBSTRIDE, DSPBPOS, DSPBSIZE, DSPBKEYVAL, DSPBKEYMASK,
+
+	/* Plane C */
+	DSPCSTRIDE, DSPCPOS, DSPCSIZE, DSPCKEYMASK,
+	DSPCKEYMINVAL, DSPCKEYMAXVAL, DCLRC0, DCLRC1, DPYC_GAMC5,
+	DPYC_GAMC4, DPYC_GAMC3, DPYC_GAMC2, DPYC_GAMC1, DPYC_GAMC0,
+
+	/* Enable Plane C */
+	DSPCCNTR, DSPCADDR,
+
+	/* Enable Plane B */
+	DSPBCNTR, DSPBADDR,
+
+	/* Enable Plane A */
+	DSPACNTR, DSPAADDR,
+
+	/* Enable VGA Plane */
+	VGACNTRL,
+
+	/* Software Flags A */
+	SWF00, SWF01, SWF02, SWF03, SWF04, SWF05, SWF06, SWF07,
+	SWF08, SWF09, SWF0A, SWF0B, SWF0C, SWF0D, SWF0E, SWF0F,
+
+	/* Software Flags B */
+	SWF10, SWF11, SWF12, SWF13, SWF14, SWF15, SWF16, SWF17,
+	SWF18, SWF19, SWF1A, SWF1B, SWF1C, SWF1D, SWF1E, SWF1F,
+
+	/* Cursor A */
+	CURSOR_A_BASE, CURSOR_A_POS, CURSOR_A_PAL0, CURSOR_A_PAL1,
+	CURSOR_A_PAL2, CURSOR_A_PAL3,
+
+	/* Cursor B */
+	CURSOR_B_BASE, CURSOR_B_POS, CURSOR_B_PAL0, CURSOR_B_PAL1,
+	CURSOR_B_PAL2, CURSOR_B_PAL3,
+
+	/* Interrupt and hardware status */
+	HWS_PGA, HWSTAM, SCPD0, IER, IMR, EIR, EMR,
+
+	0xffffffff
+};
+#define MMIO_REG_COUNT(x) (sizeof(mmio_regs_plb)/4)
+
+typedef struct _reg_platform_context_plb {
+	unsigned long *mmio_regs;
+	unsigned char *ar;
+	unsigned char *cr;
+	unsigned char *sr;
+	unsigned char *gr;
+	unsigned long pipes;
+} reg_platform_context_plb_t;
+
+typedef struct _vga_state_plb  {
+	unsigned char fcr; // Feature Control register
+	unsigned char msr; // Miscellaneous Output register
+	unsigned char sr_index; // Sequencer index register
+	unsigned char cr_index; // CRT Controller index register
+	unsigned char ar_index; // Attribute Controller index register
+	unsigned char gr_index; // Graphics Controller index register
+	unsigned char ar[AR_REG_COUNT];  // Attribute Contr regs (AR00-AR14)
+	unsigned char sr[SR_REG_COUNT];  // Sequencer registers (SR01-SR04)
+	unsigned char cr[CR_REG_COUNT];  // CRT Controller regs (CR00-CR18)
+	unsigned char gr[GR_REG_COUNT];  // Graphics Contr regs
+	unsigned char plane_latches[PLANE_LATCH_COUNT];
+} vga_state_plb_t;
+
+#define DAC_DATA_COUNT  256  /* 256 sets of (R, G, B) data */
+typedef struct _dac_state_plb {
+	unsigned long palette_a[DAC_DATA_COUNT]; /* Pipe A palette data */
+	unsigned long palette_b[DAC_DATA_COUNT]; /* Pipe B palette data */
+	unsigned char mode;  // DAC state register
+	unsigned char index; // DAC index register
+	unsigned char mask;  // 0x3C6, Palette Pixel Mask Register
+} dac_state_plb_t;
+
+/* Structure to store the 3D registers during power management
+ * These are 3D specific registers, but named as D3D because
+ * variabl names cannot start with a number*/
+typedef struct _d3d_state_plb {
+	unsigned long cin_ctl;
+	unsigned long bin_scene;
+	unsigned long bmp_buffer;
+	unsigned long bmp_get;
+	unsigned long bmp_put;
+} d3d_state_plb_t;
+
+typedef struct _reg_buffer_plb {
+	unsigned long *mmio_state;
+	//unsigned long mem_mode; /* Reg 0x20CC */
+	//unsigned long instpm;   /* Reg 0x20C0 */
+	unsigned long gtt[128*1024]; /* PLB is 128 */
+	unsigned long sgx_phyaddr;
+	void *vga_mem;
+	vga_state_plb_t vga_state;
+	dac_state_plb_t dac_state;
+	d3d_state_plb_t d3d_state;
+	void *rb_state;
+	unsigned long clk_gating_ctl;
+} reg_buffer_plb_t;
+
+static reg_platform_context_plb_t reg_platform_context_plb = {
+	mmio_regs_plb,
+	ar_regs_plb,
+	cr_regs_plb,
+	sr_regs_plb,
+	gr_regs_plb,
+	2
+};
+
+reg_dispatch_t reg_dispatch_plb = {
+	reg_alloc_plb,
+	reg_free_plb,
+	reg_save_plb,
+	reg_restore_plb,
+	reg_crtc_lut_get_plb,
+	reg_crtc_lut_set_plb,
+	&reg_platform_context_plb
+};
+
+/******************************************************************************
+ *  Local Functions
+ *****************************************************************************/
+static int reg_save_gtt_plb(igd_context_t *context, unsigned char *mmio,
+	reg_buffer_plb_t *reg_args);
+static int reg_restore_gtt_plb(igd_context_t *context,
+	reg_buffer_plb_t *reg_args);
+
+/*!
+ * This procedure simply waits for the next vertical syncing (vertical retrace)
+ * period. If the display is already in a vertical syncing period, this
+ * procedure exits unlike "util_Wait_VSync_Start()" function that waits for
+ * the beginning of the next vertical sync period.
+ *
+ * Note: A timeout is included to prevent an endless loop.
+ *
+ * @param context
+ *
+ * @return FALSE if timed out
+ * @return TRUE otherwise
+ */
+int reg_wait_vsync_plb(igd_context_t *context)
+{
+	unsigned long i = 0;
+	unsigned char *mmio;
+
+	mmio = context->device_context.virt_mmadr;
+
+	if ((EMGD_READ32(mmio + VP00) & BIT31)) {
+		return 1;
+	}
+
+	while ((i++ < 0x100000) &&  /* Check for timeout */
+		((EMGD_READ8(mmio + STATUS_REG_01) & BIT3) == 0x00)) {
+		;
+	}
+
+	if (i >= 0x100000) {
+		return 0;
+	}
+	return 1;
+}
+
+/*!
+ * Saves the current VGA register state of the video chipset into the
+ * given state buffer.
+ *
+ * This function first saves the 4 plane latches, and then it saves
+ * the SR, GR, AR, CR registers.
+ *
+ * @param context the current device context
+ * @param vga_buffer this is where the VGA register state is saved
+ * @param ar_regs AR registers to save
+ * @param cr_regs CR registers to save
+ * @param sr_regs SR registers to save
+ * @param gr_regs GR registers to save
+ *
+ * @return 0
+ */
+static int reg_save_vga_plb(
+	igd_context_t *context,
+	vga_state_plb_t *vga_buffer,
+	unsigned char *ar_regs,
+	unsigned char *cr_regs,
+	unsigned char *sr_regs,
+	unsigned char *gr_regs)
+{
+	unsigned char *mmio;
+	int i;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* First, save a few registers that will be modified to read the latches.
+	 * We need to use GR04 to go through all the latches.  Therefore, we must
+	 * first save GR04 before continuing.  However, GR04 will only behave the
+	 * way we want it to if we are not in Odd/Even mode and not in Chain 4
+	 * mode.  Therefore, we have to make sure GR05 (GFX mode reg),
+	 * and SR04 (Mem Mode reg) are set properly.
+	 * According to B-spec, we are not supposed to program GR06 via MMIO.
+	 */
+	READ_VGA(mmio, GR_PORT, GR04, vga_buffer->gr[0x04]);
+	READ_VGA(mmio, SR_PORT, SR04, vga_buffer->sr[0x04]); /* Memory Mode */
+	READ_VGA(mmio, GR_PORT, GR05, vga_buffer->gr[0x05]); /* GFX Mode Reg */
+	READ_VGA(mmio, GR_PORT, GR06, vga_buffer->gr[0x06]); /* Misc Reg */
+
+	WRITE_VGA(mmio, SR_PORT, SR04, 0x06);
+	WRITE_VGA(mmio, GR_PORT, GR05, 0x00);
+	WRITE_VGA(mmio, GR_PORT, GR06, 0x05);
+
+	/* Save Memory Latch Data latches */
+	for (i=0; i<PLANE_LATCH_COUNT; i++) {
+		WRITE_VGA(mmio, GR_PORT, GR04, (unsigned char)i);
+		READ_VGA(mmio, CR_PORT, CR22, vga_buffer->plane_latches[i]);
+	}
+
+	/* Restore the modified regs */
+	WRITE_VGA(mmio, GR_PORT, GR06, vga_buffer->gr[0x06]);
+	WRITE_VGA(mmio, GR_PORT, GR05, vga_buffer->gr[0x05]);
+	WRITE_VGA(mmio, GR_PORT, GR04, vga_buffer->gr[0x04]);
+	WRITE_VGA(mmio, SR_PORT, SR04, vga_buffer->sr[0x04]);
+
+
+	/* Save Feature Controller register. */
+	vga_buffer->fcr = EMGD_READ8(mmio + FEATURE_CONT_REG_READ);
+
+	/* Save Miscellaneous Output Register. */
+	vga_buffer->msr = EMGD_READ8(mmio + MSR_READ_PORT);
+
+	/* Save index registers. */
+	vga_buffer->sr_index = EMGD_READ8(mmio + SR_PORT);
+	vga_buffer->cr_index = EMGD_READ8(mmio + CR_PORT);
+	vga_buffer->gr_index = EMGD_READ8(mmio + GR_PORT);
+
+#if 0
+	/*
+	 * Save the AR index register and last write status. Not sure that
+	 * this is really necessary so skip it for now.
+	 */
+	READ_VGA(mmio, CR_PORT, CR24, isARData);  // Next write to AR index reg
+	isARData &= 0x80;
+
+	// Save AR index and last write status
+	vga_buffer->ar_index = EMGD_READ8(mmio + AR_PORT) | isARData;
+#endif
+
+	/* Save CRT Controller registers. */
+	for (i=0; cr_regs[i] != 0xff; i++) {
+		READ_VGA(mmio, CR_PORT, cr_regs[i], vga_buffer->cr[i]);
+	}
+
+	/* Save Attribute Controller registers. */
+	for (i=0; ar_regs[i] != 0xff; i++) {
+		READ_AR(mmio, (unsigned char)i, vga_buffer->ar[i]);
+	}
+
+	/* Save Graphics Controller registers. */
+	for (i=0; gr_regs[i] != 0xff; i++) {
+		READ_VGA(mmio, GR_PORT, gr_regs[i], vga_buffer->gr[i]);
+	}
+
+	/* Save Sequencer registers. */
+	for (i=0; sr_regs[i] != 0xff; i++) {
+		READ_VGA(mmio, SR_PORT, sr_regs[i], vga_buffer->sr[i]);
+	}
+
+	return 0;
+}
+
+/*!
+ * Restore the VGA Register state from the specifed state buffer.
+ *
+ * @param context the current device context
+ * @param vga_buffer this is where the VGA register state is saved
+ * @param ar_regs AR registers to save
+ * @param cr_regs CR registers to save
+ * @param sr_regs SR registers to save
+ * @param gr_regs GR registers to save
+ *
+ * @return 0
+ */
+static int reg_restore_vga_plb(
+	igd_context_t *context,
+	vga_state_plb_t *vga_buffer,
+	unsigned char *ar_regs,
+	unsigned char *cr_regs,
+	unsigned char *sr_regs,
+	unsigned char *gr_regs)
+{
+	unsigned long i;
+	unsigned char *mmio;
+	unsigned long bit_mask;
+	mmio = context->device_context.virt_mmadr;
+
+	/* Restore the plane latches.
+	 *
+	 * BP: I don't understand what this block is doing and it doesn't
+	 * seem necessary. Should check this against the spec and figure
+	 * out what it really does.
+	 */
+
+	/* Memory Mode Register */
+	WRITE_VGA(mmio, SR_PORT, SR04, 0x06);
+	/* GR05, Graphics Mode Register */
+	WRITE_VGA(mmio, GR_PORT, GR05, 0x00);
+	/* GR06, Micsellaneous Register */
+	WRITE_VGA(mmio, GR_PORT, GR06, 0x05);
+	/* GR08, Bit Mask Register */
+	WRITE_VGA(mmio, GR_PORT, GR08, 0xFF);
+
+	for (i=0, bit_mask=1; i<PLANE_LATCH_COUNT; i++, bit_mask<<= 1)  {
+		/* Set plane select register */
+		WRITE_VGA(mmio, GR_PORT, GR04, i);
+		/* Plane/Map mask register */
+		WRITE_VGA(mmio, SR_PORT, SR02, bit_mask);
+	}
+
+	for (i=0, bit_mask=1; i<PLANE_LATCH_COUNT; i++, bit_mask<<= 1)  {
+		/* Plane/Map mask register again?? */
+		WRITE_VGA(mmio, SR_PORT, SR02, bit_mask);
+	}
+
+	/* Restore standard VGA registers.
+	 * 2) Sequence registers
+	 * 1) MSR register
+	 * 3) CRTC registers
+	 * 4) Graphics registers
+	 * 5) Attribute registers
+	 * 6) VGA Feature register
+	 * 7) Index restisters
+	 */
+
+
+	WRITE_VGA(mmio, SR_PORT, SR00, 01); /* Do sync reset */
+
+	for (i=0; sr_regs[i] != 0xff; i++) {
+		WRITE_VGA(mmio, SR_PORT, sr_regs[i], vga_buffer->sr[i]);
+	}
+
+
+	EMGD_WRITE8(vga_buffer->msr, mmio + MSR_PORT);
+	WRITE_VGA(mmio, SR_PORT, SR00, 0x03); /* Set to normal operation */
+
+	/* Unlock CRTC */
+	WRITE_VGA(mmio, CR_PORT, CR11, vga_buffer->cr[0x11] & 0x7F);
+	for (i=0; cr_regs[i] != 0xff; i++) {
+		WRITE_VGA(mmio, CR_PORT, cr_regs[i], vga_buffer->cr[i]);
+	}
+
+	for (i=0; gr_regs[i] != 0xff; i++) {
+		WRITE_VGA(mmio, GR_PORT, gr_regs[i], vga_buffer->gr[i]);
+	}
+
+	for (i=0; ar_regs[i] != 0xff; i++) {
+		WRITE_AR(mmio, ar_regs[i], vga_buffer->ar[i]);
+	}
+
+	EMGD_WRITE8(vga_buffer->fcr, mmio + FEATURE_CONT_REG);
+
+	/* Restore index registers. Is this necessary?  */
+	EMGD_WRITE8(vga_buffer->sr_index, mmio + SR_PORT);
+	EMGD_WRITE8(vga_buffer->cr_index, mmio + CR_PORT);
+	EMGD_WRITE8(vga_buffer->gr_index, mmio + GR_PORT);
+
+	/* Lock CRTC */
+	WRITE_VGA(mmio, CR_PORT, CR11, vga_buffer->cr[0x11] | 0x80);
+
+	return 0;
+}
+
+/*!
+ * Saves the DAC registers and lookup table.
+ *
+ * @param context
+ * @param dac_state
+ * @param platform_context
+ *
+ * @return 0
+ */
+static int reg_save_dac_plb(
+	igd_context_t *context,
+	dac_state_plb_t *dac_state,
+	reg_platform_context_plb_t *platform_context)
+{
+	unsigned char *mmio;
+	int	i;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* Save DACMASK register */
+	dac_state->mask = EMGD_READ8(mmio + DAC_PEL_MASK);
+
+	/* Save DAC State register */
+	dac_state->mode = EMGD_READ8(mmio + DAC_STATE);
+
+	/* Save DAC Index register */
+	dac_state->index = EMGD_READ8(mmio + DAC_WRITE_INDEX);
+
+	/*
+	 * Save DAC data
+	 * Start from first DAC location
+	 */
+	EMGD_WRITE8(0, mmio + DAC_WRITE_INDEX);
+
+	/* Save Pipe A Palette */
+	/* Or if Pipe A is used for VGA */
+	if (((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+		(EMGD_READ32(mmio + PIPEASRC))) ||
+		((!(EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+		(!(EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+		for (i=0; i<DAC_DATA_COUNT; i++)  {
+			dac_state->palette_a[i] = EMGD_READ32(mmio + i*4 + DPALETTE_A);
+		}
+	}
+
+	if (platform_context->pipes == 2) {
+		/* If Pipe B is enabled, save Pipe B Palette */
+		/* Or if Pipe B is used for VGA */
+		if (((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+			(EMGD_READ32(mmio + PIPEBSRC)))  ||
+			((! (EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+			((EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+
+			for (i=0; i<DAC_DATA_COUNT; i++)  {
+				dac_state->palette_b[i] = EMGD_READ32(mmio + i*4 + DPALETTE_B);
+			}
+		}
+	}
+
+	return 0;
+}
+
+static void reg_crtc_lut_set_plb(
+    igd_context_t *context,
+    emgd_crtc_t *emgd_crtc)
+{
+    int i;
+	unsigned long pal_reg;
+    unsigned char *mmio;
+
+    EMGD_TRACE_ENTER;
+
+    mmio = context->device_context.virt_mmadr;
+
+    if (emgd_crtc->crtc_id == 0) {
+        /* If Pipe is enabled, restore Palette */
+        if ((emgd_crtc->crtc_id == 0 &&
+            ((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+            (EMGD_READ32(mmio + PIPEASRC)))) ||
+            (emgd_crtc->crtc_id == 1 &&
+            ((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+            (EMGD_READ32(mmio + PIPEBSRC))))) {
+
+            pal_reg = (unsigned long)(mmio + DPALETTE_A +
+                (emgd_crtc->crtc_id * (DPALETTE_B - DPALETTE_A)));
+
+            /* Restore Pipe A Palette */
+            for (i=0; i<DAC_DATA_COUNT; i++)  {
+                EMGD_WRITE32((emgd_crtc->lut_r[i] << 16) |
+                    (emgd_crtc->lut_g[i] << 8) |
+                    emgd_crtc->lut_b[i],
+                    pal_reg + i*4);
+            }
+        }
+    }
+
+    EMGD_TRACE_EXIT;
+}
+
+static void reg_crtc_lut_get_plb(
+    igd_context_t *context,
+    emgd_crtc_t *emgd_crtc)
+{
+    int i;
+	unsigned long pal_reg;
+    unsigned char *mmio;
+    unsigned long lut_value;
+
+    EMGD_TRACE_ENTER;
+
+    mmio = context->device_context.virt_mmadr;
+
+    if (emgd_crtc->crtc_id == 0) {
+        /* If Pipe is enabled, restore Palette */
+        if ((emgd_crtc->crtc_id == 0 &&
+            ((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+            (EMGD_READ32(mmio + PIPEASRC)))) ||
+            (emgd_crtc->crtc_id == 1 &&
+            ((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+            (EMGD_READ32(mmio + PIPEBSRC))))) {
+
+            pal_reg = (unsigned long)(mmio + DPALETTE_A +
+                (emgd_crtc->crtc_id * (DPALETTE_B - DPALETTE_A)));
+
+            /* Restore Pipe A Palette */
+            for (i=0; i<DAC_DATA_COUNT; i++)  {
+                lut_value = EMGD_READ32(pal_reg + i*4);
+                emgd_crtc->lut_r[i] = (lut_value & 0xFF0000) >> 16;
+                emgd_crtc->lut_g[i] = (lut_value & 0x00FF00) >> 8;
+                emgd_crtc->lut_b[i] = (lut_value & 0x0000FF);
+            }
+        }
+    }
+
+    EMGD_TRACE_EXIT;
+}
+
+
+/*!
+ * Restore previously saved DAC palette from the specifed state buffer.
+ *
+ * @param context
+ * @param dac_state
+ * @param platform_context
+ *
+ * @return 0
+ */
+static int reg_restore_dac_plb(
+	igd_context_t *context,
+	dac_state_plb_t *dac_state,
+	reg_platform_context_plb_t *platform_context)
+{
+	int i;
+	unsigned char *mmio;
+	unsigned char temp;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* If Pipe A is enabled, restore Palette */
+	/* Or if Pipe A is used for VGA */
+	if (((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+		(EMGD_READ32(mmio + PIPEASRC))) ||
+		((! (EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+		(! (EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+
+		/*
+		 * Restore DAC data
+		 * Start from first DAC location
+		 */
+		EMGD_WRITE8(0, mmio + DAC_WRITE_INDEX);
+
+		/* Restore Pipe A Palette */
+		for (i=0; i<DAC_DATA_COUNT; i++)  {
+			EMGD_WRITE32(dac_state->palette_a[i], mmio + i*4 + DPALETTE_A);
+		}
+	}
+
+	/* If this is a single pipe device. */
+	if (platform_context->pipes == 2) {
+		/* If Pipe B is enabled, restore Palette */
+		/* Or if Pipe B is used for VGA */
+		if (((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+			(EMGD_READ32(mmio + PIPEBSRC)))  ||
+			((! (EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+			((EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+
+			for (i=0; i<DAC_DATA_COUNT; i++)  {
+				EMGD_WRITE32(dac_state->palette_b[i], mmio + i*4 + DPALETTE_B);
+			}
+		}
+	}
+
+	/* Restore DACMASK register */
+	EMGD_WRITE8(dac_state->mask, mmio + DAC_PEL_MASK);
+
+	/* Restore DAC Index register */
+	if (dac_state->mode & 1) {  /* Last write was to "write index register" */
+		EMGD_WRITE8(dac_state->index, mmio + DAC_WRITE_INDEX);
+	} else {  /* Last index write was to "read index register" */
+		EMGD_WRITE8(dac_state->index - 1, mmio + DAC_READ_INDEX);
+
+		/* Dummy read to set DACSTATE register and to increment read index to
+		 * last saved.
+		 */
+		temp = EMGD_READ8(mmio + DAC_DATA_REG);
+	}
+
+	return 0;
+}
+
+/*!
+ * Initializes memory to store register values
+ *
+ * @param context pointer to igd_context
+ * @param flags indicate which register type to save
+ * @param _platform_context
+ *
+ * @return pointer to structure which will eventually be saved data defining
+ * 	register state
+ * @return NULL on failure
+ */
+static reg_buffer_t *reg_alloc_plb(igd_context_t *context,
+	unsigned long flags,
+	void *_platform_context)
+{
+	reg_buffer_plb_t* reg_args;
+	reg_buffer_t *reg_buffer;
+
+	EMGD_DEBUG("Entry - reg_alloc");
+
+	reg_buffer = (reg_buffer_t*)OS_ALLOC(sizeof(reg_buffer_t));
+	if (reg_buffer == NULL) {
+		return NULL;
+	}
+
+	reg_args = (reg_buffer_plb_t*)OS_ALLOC(sizeof(reg_buffer_plb_t));
+	if (reg_args == NULL) {
+		OS_FREE(reg_buffer);
+		return NULL;
+	}
+	reg_buffer->mode_buffer = NULL;
+	reg_buffer->platform_buffer = reg_args;
+
+	OS_MEMSET(reg_args, 0, sizeof(reg_buffer_plb_t));
+	reg_buffer->flags = flags;
+
+	reg_args->mmio_state = (void *)OS_ALLOC(sizeof(mmio_regs_plb));
+
+	if (!reg_args->mmio_state) {
+		EMGD_DEBUG("Failed Allocating mmio memory");
+		OS_FREE(reg_buffer);
+		OS_FREE(reg_args);
+		return NULL;
+	}
+
+	return reg_buffer;
+}
+
+/*!
+ * This function calculates the size of memory to store data
+ *
+ * @param context void pointer to main igd context
+ * @param reg_buffer pointer to register structure returned from a reg_alloc
+ * @param _platform_context
+ *
+ * @return 0
+ */
+static void reg_free_plb(igd_context_t *context,
+	reg_buffer_t *reg_buffer,
+	void *_platform_context)
+{
+	reg_buffer_plb_t* reg_args;
+
+	EMGD_DEBUG("Entry - reg_free");
+
+	if (reg_buffer) {
+		reg_args = (reg_buffer_plb_t*)reg_buffer->platform_buffer;
+
+		if (reg_args) {
+			if (NULL != reg_args->mmio_state) {
+				OS_FREE(reg_args->mmio_state);
+			}
+
+			if (NULL != reg_args->vga_mem) {
+				OS_FREE(reg_args->vga_mem);
+			}
+
+			if (NULL != reg_args->rb_state) {
+				OS_FREE(reg_args->rb_state);
+			}
+			OS_FREE(reg_args);
+		}
+		OS_FREE(reg_buffer);
+	}
+
+	return;
+}
+
+/*!
+ * This functions save the regs
+ *
+ * @param context void pointer to main igd context
+ * @param reg_buffer pointer to register structure returned from a reg_alloc
+ * @param _platform_context
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+static int reg_save_plb(igd_context_t *context,
+	reg_buffer_t *reg_buffer,
+	void *_platform_context)
+{
+	reg_buffer_plb_t           *reg_args;
+	unsigned long              *buffer;
+	reg_platform_context_plb_t *platform_context =
+		(reg_platform_context_plb_t *)_platform_context;
+	int                        i;
+	unsigned char              *mmio;
+
+	EMGD_TRACE_ENTER;
+
+	if (reg_buffer == NULL) {
+		return 0;
+	}
+
+	/* Before save need to idle the ring buffer */
+	context->dispatch.idle((igd_driver_h)context);
+
+	reg_args = (reg_buffer_plb_t *)reg_buffer->platform_buffer;
+	if (reg_args == NULL) {
+		return 0;
+	}
+
+	mmio = context->device_context.virt_mmadr;
+
+
+	/* Save vga registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA) {
+		EMGD_DEBUG("Saving VGA registers");
+		reg_save_vga_plb(context, &reg_args->vga_state,
+			platform_context->ar, platform_context->cr, platform_context->sr,
+			platform_context->gr);
+	}
+
+	/* Save VGA memory */
+#if 0
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA_MEM) {
+		EMGD_DEBUG("Saving VGA memory");
+		if (!reg_args->vga_mem) {
+			reg_args->vga_mem = (void *)OS_ALLOC_LARGE(64 * 1024);
+			if (reg_args->vga_mem != NULL) {
+				OS_MEMCPY(reg_args->vga_mem,
+		            (void *)context->device_context.virt_fb_adr, 64*1024);
+			} else {
+				EMGD_ERROR("Unable to allocate memory for vga memory");
+			}
+		} else {
+			OS_MEMCPY(reg_args->vga_mem,
+	            (void *)context->device_context.virt_fb_adr, 64*1024);
+		}
+	}
+#endif
+
+	/* Save mmio registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_MMIO) {
+		EMGD_DEBUG("Saving MMIO registers");
+		buffer = reg_args->mmio_state;
+		for (i=0; platform_context->mmio_regs[i] != 0xffffffff; i++) {
+			*buffer++ = EMGD_READ32(mmio + platform_context->mmio_regs[i]);
+		}
+
+		if (reg_buffer->flags & IGD_REG_SAVE_GTT) {
+			reg_save_gtt_plb(context, mmio, reg_args);
+		}
+	}
+
+	/* Save DAC registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_DAC) {
+		EMGD_DEBUG("Saving DAC registers");
+		reg_save_dac_plb(context, &reg_args->dac_state, platform_context);
+	}
+
+	/* Save Mode state */
+	if(reg_buffer->flags & IGD_REG_SAVE_MODE) {
+		EMGD_DEBUG("Saving mode state");
+		/* At driver initialization though mode_save is requested, mode isn't
+		 * initialized. So skip the mode_save if dispatch function isn't
+		 * available. In this case, mode_save() will be done as part of
+		 * mode_init(). */
+		if (context->mod_dispatch.mode_save && !reg_buffer->mode_buffer) {
+			context->mod_dispatch.mode_save(context,
+				&reg_buffer->mode_buffer,
+				&reg_buffer->flags);
+		} else {
+			EMGD_DEBUG("mode_save() is skipped as mode_init() isn't done.");
+		}
+	}
+
+	/* Save the SGX clock gating settings */
+	reg_args->clk_gating_ctl = EMGD_READ32(mmio + PSB_CR_CLKGATECTL);
+
+	return 0;
+} /* reg_save */
+
+/*!
+ * This function restores the regs states
+ *
+ * @param context void pointer to main igd context
+ * @param reg_buffer pointer to register structure returned from a reg_alloc
+ * @param _platform_context
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+int reg_restore_plb(igd_context_t *context,
+	reg_buffer_t *reg_buffer,
+	void *_platform_context)
+{
+	reg_buffer_plb_t* reg_args;
+	reg_platform_context_plb_t *platform_context =
+		(reg_platform_context_plb_t *)_platform_context;
+	unsigned char *mmio;
+	unsigned long *buffer;
+	int i;
+
+	EMGD_DEBUG("Entry - reg_restore");
+
+	if (reg_buffer == NULL) {
+		return 0;
+	}
+
+	reg_args = (reg_buffer_plb_t *)reg_buffer->platform_buffer;
+	if (reg_args == NULL) {
+		return 0;
+	}
+
+	EMGD_DEBUG(" flags = 0x%lx", reg_buffer->flags);
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* Restore Mode state */
+	if (reg_buffer->flags & IGD_REG_SAVE_MODE) {
+		if (reg_buffer->mode_buffer) {
+			context->mod_dispatch.mode_restore(context,
+				&reg_buffer->mode_buffer,
+				&reg_buffer->flags);
+		} else {
+			EMGD_DEBUG("mode_restore() already happened in mode_shutdown().");
+		}
+	}
+
+	/* Restore MMIO registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_MMIO) {
+		EMGD_DEBUG("Restoring MMIO registers");
+		buffer = reg_args->mmio_state;
+		for (i=0; platform_context->mmio_regs[i] != 0xffffffff; i++) {
+			if (platform_context->mmio_regs[i] == MI_ARB_STATE) {
+				EMGD_DEBUG("Handle special masked register case");
+				EMGD_WRITE32(0x04100000 | *buffer++, mmio +
+					platform_context->mmio_regs[i]);
+			} else if (platform_context->mmio_regs[i] == DPLLACNTR ||
+				platform_context->mmio_regs[i] == DPLLBCNTR) {
+
+					EMGD_DEBUG("Handle special DPLL settling");
+					EMGD_WRITE32(*buffer++,
+						mmio + platform_context->mmio_regs[i]);
+					OS_SLEEP(150);
+			} else {
+				EMGD_WRITE32(*buffer++, mmio + platform_context->mmio_regs[i]);
+			}
+		}
+
+		if (reg_buffer->flags & IGD_REG_SAVE_GTT) {
+			reg_restore_gtt_plb(context, reg_args);
+		}
+	}
+
+#if 0
+	/* Restore VGA memory */
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA_MEM) {
+		if (reg_args->vga_mem) {
+			OS_MEMCPY((void *)context->device_context.virt_fb_adr,
+					reg_args->vga_mem, 64*1024);
+		} else {
+			EMGD_ERROR("Trying to restore NULL VGA memory");
+		}
+	}
+#endif
+
+	/* Restore DAC registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_DAC) {
+		reg_restore_dac_plb(context, &reg_args->dac_state, platform_context);
+	}
+
+	/* Restore VGA registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA) {
+		reg_restore_vga_plb(context, &reg_args->vga_state,
+			platform_context->ar, platform_context->cr, platform_context->sr,
+			platform_context->gr);
+	}
+
+	/* Restore the SGX clock gating settings */
+	EMGD_WRITE32(reg_args->clk_gating_ctl, mmio + PSB_CR_CLKGATECTL);
+
+	return 0;
+}
+
+/*!
+ * This function saves the GTT table entries into a buffer so that the GTT
+ * can be restored later.
+ *
+ * @param context needs this to get the GTT table size and to get the
+ *	virtual address to the GTT table
+ * @param mmio virtual address to MMIO
+ * @param reg_args a pre-allocated context where GTT entries are saved
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+static int reg_save_gtt_plb(igd_context_t *context, unsigned char *mmio,
+	reg_buffer_plb_t *reg_args)
+{
+	unsigned int  i;
+
+	/* Read the GTT entries from GTT ADR and save it in the array. */
+	for (i = 0; i < (context->device_context.gatt_pages); i++) {
+		reg_args->gtt[i] = EMGD_READ32(
+			context->device_context.virt_gttadr + i);
+	}
+
+	reg_args->sgx_phyaddr = EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+
+	return 0;
+}
+
+/*!
+ *  This function restores the GTT table entries.
+ *
+ * @param context the context contains the GTT size and address
+ * @param reg_args this has the saved GTT entries
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+static int reg_restore_gtt_plb(igd_context_t *context,
+	reg_buffer_plb_t *reg_args)
+{
+	unsigned int i;
+	unsigned char *mmio;
+	unsigned long sgx_mmu;
+
+	/* If the first element is 0, then nothing was saved */
+	if (0 == reg_args->gtt[0]) {
+		return 0;
+	}
+
+	/* Restore the GTT entries */
+	for (i=0; i < context->device_context.gatt_pages; i++) {
+		EMGD_WRITE32(reg_args->gtt[i],
+			context->device_context.virt_gttadr + i);
+	}
+
+	mmio = context->device_context.virt_mmadr;
+
+	/*
+	 * FIXME: This is probably not necessary and may be incorrect
+	 */
+	/* Invalidate directory cache */
+	sgx_mmu = EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+	sgx_mmu |= 0x1E;
+	EMGD_WRITE32(sgx_mmu, mmio + PSB_CR_BIF_CTRL);
+	EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+
+	EMGD_WRITE32(reg_args->sgx_phyaddr, mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+	EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+
+	/* Turn on host access to aperture via the MMU */
+	sgx_mmu = EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+	sgx_mmu &= 0xFFFF0000;
+	EMGD_WRITE32(sgx_mmu, mmio + PSB_CR_BIF_CTRL);
+	EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/state/reg/tnc/reg_tnc.c b/drivers/gpu/drm/emgd/emgd/state/reg/tnc/reg_tnc.c
new file mode 100644
index 0000000..969155e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/state/reg/tnc/reg_tnc.c
@@ -0,0 +1,1205 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: reg_tnc.c
+ * $Revision: 1.18 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the implementation file for the register module's main API's called
+ *  by external devices.
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.state
+
+#include <io.h>
+#include <memory.h>
+#include <sched.h>
+
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_pwr.h>
+#include <igd_errno.h>
+
+#include <context.h>
+#include <utils.h>
+#include <dsp.h>
+#include <mode.h>
+#include <utils.h>
+#include <vga.h>
+#include <intelpci.h>
+
+#include <tnc/regs.h>
+#include <plb/context.h>
+
+#include "../cmn/reg_dispatch.h"
+
+/*!
+ * @addtogroup state_group
+ * @{
+ */
+
+#define PLANE_LATCH_COUNT  4
+
+#define RING_BUFFER        0
+#define MMIO_MISC          1
+
+static reg_buffer_t *reg_alloc_tnc(igd_context_t *context,
+	unsigned long flags, void *_platform_context);
+static void reg_free_tnc(igd_context_t *context, reg_buffer_t *reg_buffer,
+	void *_platform_context);
+static int reg_save_tnc(igd_context_t *context, reg_buffer_t *reg_buffer,
+	void *_platform_context);
+static int reg_restore_tnc(igd_context_t *context, reg_buffer_t *reg_buffer,
+	void *_platform_context);
+static void reg_crtc_lut_get_tnc(igd_context_t *context,
+    emgd_crtc_t *emgd_crtc);
+static void reg_crtc_lut_set_tnc(igd_context_t *context,
+    emgd_crtc_t *emgd_crtc);
+
+/* GR registers being saved */
+static unsigned char gr_regs_tnc[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x05, 0x06, 0x07, 0x08,
+	0x10, 0x11,
+	0x18,
+	0xff
+};
+#define GR_REG_COUNT sizeof(gr_regs_tnc)
+
+/* SR registers being saved */
+static unsigned char sr_regs_tnc[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x07,
+	0xff
+};
+#define SR_REG_COUNT sizeof(sr_regs_tnc)
+
+/* AR registers being saved */
+static unsigned char ar_regs_tnc[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x05, 0x06, 0x07, 0x08, 0x09,
+	0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+	0x0f, 0x10, 0x11, 0x12, 0x13,
+	0x14,
+	0xff,
+};
+#define AR_REG_COUNT sizeof(ar_regs_tnc)
+
+/* CR registers being saved */
+static unsigned char cr_regs_tnc[] = {
+	0x00, 0x01, 0x02, 0x03, 0x04,
+	0x05, 0x06, 0x07, 0x08, 0x09,
+	0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+	0x0f, 0x10, 0x11, 0x12, 0x13,
+	0x14, 0x15, 0x16, 0x17, 0x18,
+	0xff
+};
+#define CR_REG_COUNT sizeof(cr_regs_tnc)
+
+
+/* MMIO states register to be saved */
+static unsigned long mmio_regs_tnc[] = {
+#if  MMIO_MISC
+	/* Fence Registers */
+	FENCE0, FENCE1, FENCE2, FENCE3, FENCE4, FENCE5, FENCE6, FENCE7,  // Fence
+	FENCE8, FENCE9, FENCE10, FENCE11, FENCE12, FENCE13, FENCE14, FENCE15,
+
+	/* GTT Control */
+	PGTBL_CTL,
+#endif
+
+	/* Memory Arbitration */
+	FW_BLC_SELF, MI_ARB_STATE, G_DEBUG,
+
+	/* Program Clocks */
+	VGA0_DIVISOR, VGA1_DIVISOR, VGA_PD,
+
+	FPA0, FPA1, FPB0, FPB1, P2D_CG_DIS,
+
+	DPLLACNTR, DPLLBCNTR,
+
+	/* Enable Ports */
+	/* SDVO Port Enable */
+	SDVOBCNTR,
+
+	/* Panel Power */
+	LVDS_PNL_PWR_CTL, PP_ON_DELAYS, PP_OFF_DELAYS, PP_DIVISOR,
+
+	/* Panel Fitting */
+	PFIT_CONTROL, PFIT_PGM_RATIOS,
+
+	/* Backlight Control */
+	BLC_PWM_CTL2, BLC_PWM_CTL, BLM_HIST_CTL,
+
+	/* LVDS Port Enable */
+	LVDSCNTR,
+
+	/* Program Pipe A */
+	PIPEA_STAT, PIPEA_DISP_ARB_CTRL, HTOTAL_A, HBLANK_A, HSYNC_A, VTOTAL_A,
+	VBLANK_A, VSYNC_A, PIPEASRC, BCLRPAT_A,
+
+	/* Program Pipe B */
+	PIPEB_STAT, HTOTAL_B, HBLANK_B, HSYNC_B, VTOTAL_B,
+	VBLANK_B, VSYNC_B, PIPEBSRC, BCLRPAT_B,
+
+
+	/* Enable Pipes */
+	PIPEA_CONF, PIPEB_CONF,
+
+	/* Plane A */
+	DSPASTRIDE, DSPAKEYVAL, DSPAKEYMASK,
+
+	/* Plane B */
+	DSPBSTRIDE, DSPBKEYVAL, DSPBKEYMASK,
+
+	/* Plane C */
+	DSPCSTRIDE, DSPCPOS, DSPCSIZE, DSPCKEYMASK,
+	DSPCKEYMINVAL, DSPCKEYMAXVAL, DCLRC0, DCLRC1, DPYC_GAMC5,
+	DPYC_GAMC4, DPYC_GAMC3, DPYC_GAMC2, DPYC_GAMC1, DPYC_GAMC0,
+
+	/* Enable Plane C */
+	DSPCCNTR, DSPCLINOFF, DSPCSURF,
+
+	/* Enable Plane B */
+	DSPBCNTR, DSPBLINOFF, DSPBSURF,
+
+	/* Enable Plane A */
+	DSPACNTR, DSPALINOFF, DSPASURF,
+
+	/* Enable VGA Plane */
+	VGACNTRL,
+
+	/* Software Flags A */
+	SWF00, SWF01, SWF02, SWF03, SWF04, SWF05, SWF06, SWF07,
+	SWF08, SWF09, SWF0A, SWF0B, SWF0C, SWF0D, SWF0E, SWF0F,
+
+	/* Software Flags B */
+	SWF10, SWF11, SWF12, SWF13, SWF14, SWF15, SWF16, SWF17,
+	SWF18, SWF19, SWF1A, SWF1B, SWF1C, SWF1D, SWF1E, SWF1F,
+
+	/* Cursor A */
+	CURSOR_A_BASE, CURSOR_A_POS, CURSOR_A_PAL0, CURSOR_A_PAL1,
+	CURSOR_A_PAL2, CURSOR_A_PAL3,
+
+	/* Cursor B */
+	CURSOR_B_BASE, CURSOR_B_POS, CURSOR_B_PAL0, CURSOR_B_PAL1,
+	CURSOR_B_PAL2, CURSOR_B_PAL3,
+
+	/* Interrupt and hardware status */
+	HWS_PGA, HWSTAM, SCPD0, IER, IMR, EIR, EMR,
+
+	0xffffffff
+};
+#define MMIO_REG_COUNT(x) (sizeof(mmio_regs_tnc)/4)
+
+/* MMIO states register to be saved */
+static unsigned long mmio_regs_sdvo_tnc[] = {
+
+	/* Program Clocks */
+	VGA0_DIVISOR,
+
+	SDVO_BUFF_CTRL_REG,
+
+	FPB0, P2D_CG_DIS,
+
+	DPLLBCNTR,
+
+	/* Enable Ports */
+	/* SDVO Port Enable */
+	SDVOBCNTR,
+
+	/* Program Pipe B */
+	PIPEB_STAT, HTOTAL_B, HBLANK_B, HSYNC_B, VTOTAL_B,
+	VBLANK_B, VSYNC_B, PIPEBSRC, BCLRPAT_B, PROG_STALL,
+
+	/* CDVO */
+	/* CDVO_CTRL,*/
+	CDVO_SLEW_RATE, CDVO_STRENGTH, CDVO_RCOMP_UPDATE,
+
+	/* TNC_SDVORAM */
+	DPLL_ANACORE_CTRL, DPLL_MISC, DPLL_MONITOR, DPLL_MONITOR2,
+	DPLL_TEST_COMPARATOR,
+
+	/* Enable Pipes */
+	PIPEB_CONF,
+
+	/* Interrupt and hardware status */
+	IER, IMR,
+
+	0xffffffff
+};
+#define MMIO_REG_SDVO_COUNT(x) (sizeof(mmio_regs_sdvo_tnc)/4)
+
+typedef struct _reg_platform_context_tnc {
+	unsigned long *mmio_regs;
+	unsigned long *mmio_regs_sdvo;
+	unsigned char *ar;
+	unsigned char *cr;
+	unsigned char *sr;
+	unsigned char *gr;
+	unsigned long pipes;
+} reg_platform_context_tnc_t;
+
+typedef struct _vga_state_tnc  {
+	unsigned char fcr; // Feature Control register
+	unsigned char msr; // Miscellaneous Output register
+	unsigned char sr_index; // Sequencer index register
+	unsigned char cr_index; // CRT Controller index register
+	unsigned char ar_index; // Attribute Controller index register
+	unsigned char gr_index; // Graphics Controller index register
+	unsigned char ar[AR_REG_COUNT];  // Attribute Contr regs (AR00-AR14)
+	unsigned char sr[SR_REG_COUNT];  // Sequencer registers (SR01-SR04)
+	unsigned char cr[CR_REG_COUNT];  // CRT Controller regs (CR00-CR18)
+	unsigned char gr[GR_REG_COUNT];  // Graphics Contr regs
+	unsigned char plane_latches[PLANE_LATCH_COUNT];
+} vga_state_tnc_t;
+
+#define DAC_DATA_COUNT  256  /* 256 sets of (R, G, B) data */
+typedef struct _dac_state_tnc {
+	unsigned long palette_a[DAC_DATA_COUNT]; /* Pipe A palette data */
+	unsigned long palette_b[DAC_DATA_COUNT]; /* Pipe B palette data */
+	unsigned char mode;  // DAC state register
+	unsigned char index; // DAC index register
+	unsigned char mask;  // 0x3C6, Palette Pixel Mask Register
+} dac_state_tnc_t;
+
+/* Structure to store the 3D registers during power management
+ * These are 3D specific registers, but named as D3D because
+ * variabl names cannot start with a number*/
+typedef struct _d3d_state_tnc {
+	unsigned long cin_ctl;
+	unsigned long bin_scene;
+	unsigned long bmp_buffer;
+	unsigned long bmp_get;
+	unsigned long bmp_put;
+} d3d_state_tnc_t;
+
+typedef struct _reg_buffer_tnc {
+	unsigned long *mmio_state;
+	unsigned long *mmio_state_sdvo;
+	//unsigned long mem_mode; /* Reg 0x20CC */
+	//unsigned long instpm;   /* Reg 0x20C0 */
+	unsigned long gtt[128*1024]; /* PLB is 128 */
+	unsigned long sgx_phyaddr;
+	void *vga_mem;
+	vga_state_tnc_t vga_state;
+	dac_state_tnc_t dac_state;
+	d3d_state_tnc_t d3d_state;
+	void *rb_state;
+	unsigned long clk_gating_ctl;
+} reg_buffer_tnc_t;
+
+static reg_platform_context_tnc_t reg_platform_context_tnc = {
+	mmio_regs_tnc,
+	mmio_regs_sdvo_tnc,
+	ar_regs_tnc,
+	cr_regs_tnc,
+	sr_regs_tnc,
+	gr_regs_tnc,
+	2
+};
+
+reg_dispatch_t reg_dispatch_tnc = {
+	reg_alloc_tnc,
+	reg_free_tnc,
+	reg_save_tnc,
+	reg_restore_tnc,
+	reg_crtc_lut_get_tnc,
+	reg_crtc_lut_set_tnc,
+	&reg_platform_context_tnc
+};
+
+/******************************************************************************
+ *  Local Functions
+ *****************************************************************************/
+static int reg_save_gtt_tnc(igd_context_t *context, unsigned char *mmio,
+	reg_buffer_tnc_t *reg_args);
+static int reg_restore_gtt_tnc(igd_context_t *context,
+	reg_buffer_tnc_t *reg_args);
+
+/*!
+ * This procedure simply waits for the next vertical syncing (vertical retrace)
+ * period. If the display is already in a vertical syncing period, this
+ * procedure exits unlike "util_Wait_VSync_Start()" function that waits for
+ * the beginning of the next vertical sync period.
+ *
+ * Note: A timeout is included to prevent an endless loop.
+ *
+ * @param context
+ *
+ * @return FALSE if timed out
+ * @return TRUE otherwise
+ */
+int reg_wait_vsync_tnc(igd_context_t *context)
+{
+	unsigned long i = 0;
+	unsigned char *mmio;
+
+	mmio = context->device_context.virt_mmadr;
+
+	if ((EMGD_READ32(mmio + VP00) & BIT31)) {
+		return 1;
+	}
+
+	while ((i++ < 0x100000) &&  /* Check for timeout */
+		((EMGD_READ8(mmio + STATUS_REG_01) & BIT3) == 0x00)) {
+		;
+	}
+
+	if (i >= 0x100000) {
+		return 0;
+	}
+	return 1;
+}
+
+/*!
+ * Saves the current VGA register state of the video chipset into the
+ * given state buffer.
+ *
+ * This function first saves the 4 plane latches, and then it saves
+ * the SR, GR, AR, CR registers.
+ *
+ * @param context the current device context
+ * @param vga_buffer this is where the VGA register state is saved
+ * @param ar_regs AR registers to save
+ * @param cr_regs CR registers to save
+ * @param sr_regs SR registers to save
+ * @param gr_regs GR registers to save
+ *
+ * @return 0
+ */
+static int reg_save_vga_tnc(
+	igd_context_t *context,
+	vga_state_tnc_t *vga_buffer,
+	unsigned char *ar_regs,
+	unsigned char *cr_regs,
+	unsigned char *sr_regs,
+	unsigned char *gr_regs)
+{
+	unsigned char *mmio;
+	int i;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* First, save a few registers that will be modified to read the latches.
+	 * We need to use GR04 to go through all the latches.  Therefore, we must
+	 * first save GR04 before continuing.  However, GR04 will only behave the
+	 * way we want it to if we are not in Odd/Even mode and not in Chain 4
+	 * mode.  Therefore, we have to make sure GR05 (GFX mode reg),
+	 * and SR04 (Mem Mode reg) are set properly.
+	 * According to B-spec, we are not supposed to program GR06 via MMIO.
+	 */
+	READ_VGA(mmio, GR_PORT, GR04, vga_buffer->gr[0x04]);
+	READ_VGA(mmio, SR_PORT, SR04, vga_buffer->sr[0x04]); /* Memory Mode */
+	READ_VGA(mmio, GR_PORT, GR05, vga_buffer->gr[0x05]); /* GFX Mode Reg */
+	READ_VGA(mmio, GR_PORT, GR06, vga_buffer->gr[0x06]); /* Misc Reg */
+
+	WRITE_VGA(mmio, SR_PORT, SR04, 0x06);
+	WRITE_VGA(mmio, GR_PORT, GR05, 0x00);
+	WRITE_VGA(mmio, GR_PORT, GR06, 0x05);
+
+	/* Save Memory Latch Data latches */
+	for (i=0; i<PLANE_LATCH_COUNT; i++) {
+		WRITE_VGA(mmio, GR_PORT, GR04, (unsigned char)i);
+		READ_VGA(mmio, CR_PORT, CR22, vga_buffer->plane_latches[i]);
+	}
+
+	/* Restore the modified regs */
+	WRITE_VGA(mmio, GR_PORT, GR06, vga_buffer->gr[0x06]);
+	WRITE_VGA(mmio, GR_PORT, GR05, vga_buffer->gr[0x05]);
+	WRITE_VGA(mmio, GR_PORT, GR04, vga_buffer->gr[0x04]);
+	WRITE_VGA(mmio, SR_PORT, SR04, vga_buffer->sr[0x04]);
+
+
+	/* Save Feature Controller register. */
+	vga_buffer->fcr = EMGD_READ8(mmio + FEATURE_CONT_REG_READ);
+
+	/* Save Miscellaneous Output Register. */
+	vga_buffer->msr = EMGD_READ8(mmio + MSR_READ_PORT);
+
+	/* Save index registers. */
+	vga_buffer->sr_index = EMGD_READ8(mmio + SR_PORT);
+	vga_buffer->cr_index = EMGD_READ8(mmio + CR_PORT);
+	vga_buffer->gr_index = EMGD_READ8(mmio + GR_PORT);
+
+#if 0
+	/*
+	 * Save the AR index register and last write status. Not sure that
+	 * this is really necessary so skip it for now.
+	 */
+	READ_VGA(mmio, CR_PORT, CR24, isARData);  // Next write to AR index reg
+	isARData &= 0x80;
+
+	// Save AR index and last write status
+	vga_buffer->ar_index = EMGD_READ8(mmio + AR_PORT) | isARData;
+#endif
+
+	/* Save CRT Controller registers. */
+	for (i=0; cr_regs[i] != 0xff; i++) {
+		READ_VGA(mmio, CR_PORT, cr_regs[i], vga_buffer->cr[i]);
+	}
+
+	/* Save Attribute Controller registers. */
+	for (i=0; ar_regs[i] != 0xff; i++) {
+		reg_wait_vsync_tnc(context);
+		READ_AR(mmio, (unsigned char)i, vga_buffer->ar[i]);
+	}
+
+	/* Save Graphics Controller registers. */
+	for (i=0; gr_regs[i] != 0xff; i++) {
+		READ_VGA(mmio, GR_PORT, gr_regs[i], vga_buffer->gr[i]);
+	}
+
+	/* Save Sequencer registers. */
+	for (i=0; sr_regs[i] != 0xff; i++) {
+		READ_VGA(mmio, SR_PORT, sr_regs[i], vga_buffer->sr[i]);
+	}
+
+	return 0;
+}
+
+/*!
+ * Restore the VGA Register state from the specifed state buffer.
+ *
+ * @param context the current device context
+ * @param vga_buffer this is where the VGA register state is saved
+ * @param ar_regs AR registers to save
+ * @param cr_regs CR registers to save
+ * @param sr_regs SR registers to save
+ * @param gr_regs GR registers to save
+ *
+ * @return 0
+ */
+static int reg_restore_vga_tnc(
+	igd_context_t *context,
+	vga_state_tnc_t *vga_buffer,
+	unsigned char *ar_regs,
+	unsigned char *cr_regs,
+	unsigned char *sr_regs,
+	unsigned char *gr_regs)
+{
+	unsigned long i;
+	unsigned char *mmio;
+	unsigned long bit_mask;
+	mmio = context->device_context.virt_mmadr;
+
+	/* Restore the plane latches.
+	 *
+	 * BP: I don't understand what this block is doing and it doesn't
+	 * seem necessary. Should check this against the spec and figure
+	 * out what it really does.
+	 */
+
+	/* Memory Mode Register */
+	WRITE_VGA(mmio, SR_PORT, SR04, 0x06);
+	/* GR05, Graphics Mode Register */
+	WRITE_VGA(mmio, GR_PORT, GR05, 0x00);
+	/* GR06, Micsellaneous Register */
+	WRITE_VGA(mmio, GR_PORT, GR06, 0x05);
+	/* GR08, Bit Mask Register */
+	WRITE_VGA(mmio, GR_PORT, GR08, 0xFF);
+
+	for (i=0, bit_mask=1; i<PLANE_LATCH_COUNT; i++, bit_mask<<= 1)  {
+		/* Set plane select register */
+		WRITE_VGA(mmio, GR_PORT, GR04, i);
+		/* Plane/Map mask register */
+		WRITE_VGA(mmio, SR_PORT, SR02, bit_mask);
+	}
+
+	for (i=0, bit_mask=1; i<PLANE_LATCH_COUNT; i++, bit_mask<<= 1)  {
+		/* Plane/Map mask register again?? */
+		WRITE_VGA(mmio, SR_PORT, SR02, bit_mask);
+	}
+
+	/* Restore standard VGA registers.
+	 * 2) Sequence registers
+	 * 1) MSR register
+	 * 3) CRTC registers
+	 * 4) Graphics registers
+	 * 5) Attribute registers
+	 * 6) VGA Feature register
+	 * 7) Index restisters
+	 */
+
+
+	WRITE_VGA(mmio, SR_PORT, SR00, 01); /* Do sync reset */
+
+	for (i=0; sr_regs[i] != 0xff; i++) {
+		 WRITE_VGA(mmio, SR_PORT, sr_regs[i], vga_buffer->sr[i]);
+	}
+
+
+	EMGD_WRITE8(vga_buffer->msr, mmio + MSR_PORT);
+	WRITE_VGA(mmio, SR_PORT, SR00, 0x03); /* Set to normal operation */
+
+	/* Unlock CRTC */
+	WRITE_VGA(mmio, CR_PORT, CR11, vga_buffer->cr[0x11] & 0x7F);
+	for (i=0; cr_regs[i] != 0xff; i++) {
+		 WRITE_VGA(mmio, CR_PORT, cr_regs[i], vga_buffer->cr[i]);
+	}
+
+	for (i=0; gr_regs[i] != 0xff; i++) {
+		 WRITE_VGA(mmio, GR_PORT, gr_regs[i], vga_buffer->gr[i]);
+	}
+
+	for (i=0; ar_regs[i] != 0xff; i++) {
+		 WRITE_AR(mmio, ar_regs[i], vga_buffer->ar[i]);
+	}
+
+	EMGD_WRITE8(vga_buffer->fcr, mmio + FEATURE_CONT_REG);
+
+	/* Restore index registers. Is this necessary?  */
+	EMGD_WRITE8(vga_buffer->sr_index, mmio + SR_PORT);
+	EMGD_WRITE8(vga_buffer->cr_index, mmio + CR_PORT);
+	EMGD_WRITE8(vga_buffer->gr_index, mmio + GR_PORT);
+
+	/* Lock CRTC */
+	WRITE_VGA(mmio, CR_PORT, CR11, vga_buffer->cr[0x11] | 0x80);
+
+	return 0;
+}
+
+/*!
+ * Saves the DAC registers and lookup table.
+ *
+ * @param context
+ * @param dac_state
+ * @param platform_context
+ *
+ * @return 0
+ */
+static int reg_save_dac_tnc(
+	igd_context_t *context,
+	dac_state_tnc_t *dac_state,
+	reg_platform_context_tnc_t *platform_context)
+{
+	unsigned char *mmio;
+	int	i;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* Save DACMASK register */
+	dac_state->mask = EMGD_READ8(mmio + DAC_PEL_MASK);
+
+	/* Save DAC State register */
+	dac_state->mode = EMGD_READ8(mmio + DAC_STATE);
+
+	/* Save DAC Index register */
+	dac_state->index = EMGD_READ8(mmio + DAC_WRITE_INDEX);
+
+	/*
+	 * Save DAC data
+	 * Start from first DAC location
+	 */
+	EMGD_WRITE8(0, mmio + DAC_WRITE_INDEX);
+
+	/* Save Pipe A Palette */
+	/* Or if Pipe A is used for VGA */
+	if (((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+		(EMGD_READ32(mmio + PIPEASRC))) ||
+		((!(EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+		(!(EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+		for (i=0; i<DAC_DATA_COUNT; i++)  {
+			dac_state->palette_a[i] = EMGD_READ32(mmio + i*4 + DPALETTE_A);
+		}
+	}
+
+	if (platform_context->pipes == 2) {
+		/* If Pipe B is enabled, save Pipe B Palette */
+		/* Or if Pipe B is used for VGA */
+		if (((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+			(EMGD_READ32(mmio + PIPEBSRC)))  ||
+			((! (EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+			((EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+
+			for (i=0; i<DAC_DATA_COUNT; i++)  {
+				dac_state->palette_b[i] = EMGD_READ32(mmio + i*4 + DPALETTE_B);
+			}
+		}
+	}
+
+	return 0;
+}
+
+static void reg_crtc_lut_set_tnc(
+	igd_context_t *context,
+	emgd_crtc_t *emgd_crtc)
+{
+	int i;
+	unsigned long pal_reg;
+	unsigned char *mmio;
+
+	EMGD_TRACE_ENTER;
+
+	mmio = context->device_context.virt_mmadr;
+
+    /* Check if the pipe is enabled */
+    if ((emgd_crtc->crtc_id == IGD_KMS_PIPEA &&
+		((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+        (EMGD_READ32(mmio + PIPEASRC)))) ||
+		(emgd_crtc->crtc_id == IGD_KMS_PIPEB &&
+		((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+        (EMGD_READ32(mmio + PIPEBSRC))))) {
+
+		pal_reg = (unsigned long)(mmio + DPALETTE_A +
+			(emgd_crtc->crtc_id * (DPALETTE_B - DPALETTE_A)));
+
+        for (i=0; i<DAC_DATA_COUNT; i++)  {
+            EMGD_WRITE32((emgd_crtc->lut_r[i] << 16) |
+				(emgd_crtc->lut_g[i] << 8) |
+				emgd_crtc->lut_b[i],
+				pal_reg + i*4);
+        }
+    }
+
+	EMGD_TRACE_EXIT;
+}
+
+static void reg_crtc_lut_get_tnc(
+	igd_context_t *context,
+	emgd_crtc_t *emgd_crtc)
+{
+	int i;
+	unsigned long pal_reg;
+	unsigned char *mmio;
+	unsigned long lut_value;
+
+	EMGD_TRACE_ENTER;
+
+	mmio = context->device_context.virt_mmadr;
+
+    /* Check if the pipe is enabled, is this necessary? */
+    if ((emgd_crtc->crtc_id == IGD_KMS_PIPEA &&
+		((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+        (EMGD_READ32(mmio + PIPEASRC)))) ||
+		(emgd_crtc->crtc_id == IGD_KMS_PIPEB &&
+		((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+        (EMGD_READ32(mmio + PIPEBSRC))))) {
+
+		pal_reg = (unsigned long)(mmio + DPALETTE_A +
+			(emgd_crtc->crtc_id * (DPALETTE_B - DPALETTE_A)));
+
+        for (i=0; i<DAC_DATA_COUNT; i++)  {
+            lut_value = EMGD_READ32(pal_reg + i*4);
+			emgd_crtc->lut_r[i] = (lut_value & 0xFF0000) >> 16;
+			emgd_crtc->lut_g[i] = (lut_value & 0x00FF00) >> 8;
+			emgd_crtc->lut_b[i] = (lut_value & 0x0000FF);
+		}
+    }
+
+	EMGD_TRACE_EXIT;
+}
+
+
+/*!
+ * Restore previously saved DAC palette from the specifed state buffer.
+ *
+ * @param context
+ * @param dac_state
+ * @param platform_context
+ *
+ * @return 0
+ */
+static int reg_restore_dac_tnc(
+	igd_context_t *context,
+	dac_state_tnc_t *dac_state,
+	reg_platform_context_tnc_t *platform_context)
+{
+	int i;
+	unsigned char *mmio;
+	unsigned char temp;
+
+	mmio = context->device_context.virt_mmadr;
+
+	/* If Pipe A is enabled, restore Palette */
+	/* Or if Pipe A is used for VGA */
+	if (((EMGD_READ32(mmio + PIPEA_CONF) & PIPE_ENABLE) &&
+		(EMGD_READ32(mmio + PIPEASRC))) ||
+		((! (EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+		(! (EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+
+		/*
+		 * Restore DAC data
+		 * Start from first DAC location
+		 */
+		EMGD_WRITE8(0, mmio + DAC_WRITE_INDEX);
+
+		/* Restore Pipe A Palette */
+		for (i=0; i<DAC_DATA_COUNT; i++)  {
+			EMGD_WRITE32(dac_state->palette_a[i], mmio + i*4 + DPALETTE_A);
+		}
+	}
+
+	/* If this is a single pipe device. */
+	if (platform_context->pipes == 2) {
+		/* If Pipe B is enabled, restore Palette */
+		/* Or if Pipe B is used for VGA */
+		if (((EMGD_READ32(mmio + PIPEB_CONF) & PIPE_ENABLE) &&
+			(EMGD_READ32(mmio + PIPEBSRC)))  ||
+			((! (EMGD_READ32(mmio + 0x71400) & 0x80000000)) &&
+			((EMGD_READ32(mmio + 0x71400) & 0x20000000)))) {
+
+			for (i=0; i<DAC_DATA_COUNT; i++)  {
+				EMGD_WRITE32(dac_state->palette_b[i], mmio + i*4 + DPALETTE_B);
+			}
+		}
+	}
+
+	/* Restore DACMASK register */
+	EMGD_WRITE8(dac_state->mask, mmio + DAC_PEL_MASK);
+
+	/* Restore DAC Index register */
+	if (dac_state->mode & 1) {  /* Last write was to "write index register" */
+		EMGD_WRITE8(dac_state->index, mmio + DAC_WRITE_INDEX);
+	} else {  /* Last index write was to "read index register" */
+		EMGD_WRITE8(dac_state->index - 1, mmio + DAC_READ_INDEX);
+
+		/* Dummy read to set DACSTATE register and to increment read index to
+		 * last saved.
+		 */
+		temp = EMGD_READ8(mmio + DAC_DATA_REG);
+	}
+
+	return 0;
+}
+
+/*!
+ * Initializes memory to store register values
+ *
+ * @param context pointer to igd_context
+ * @param flags indicate which register type to save
+ * @param _platform_context
+ *
+ * @return pointer to structure which will eventually be saved data defining
+ * 	register state
+ * @return NULL on failure
+ */
+static reg_buffer_t *reg_alloc_tnc(igd_context_t *context,
+	unsigned long flags,
+	void *_platform_context)
+{
+	reg_buffer_tnc_t* reg_args;
+	reg_buffer_t *reg_buffer;
+
+	EMGD_DEBUG("Entry - reg_alloc");
+
+	reg_buffer = (reg_buffer_t*)OS_ALLOC(sizeof(reg_buffer_t));
+	if (reg_buffer == NULL) {
+		return NULL;
+	}
+
+	reg_args = (reg_buffer_tnc_t*)OS_ALLOC(sizeof(reg_buffer_tnc_t));
+	if (reg_args == NULL) {
+		OS_FREE(reg_buffer);
+		return NULL;
+	}
+	reg_buffer->mode_buffer = NULL;
+	reg_buffer->platform_buffer = reg_args;
+
+	OS_MEMSET(reg_args, 0, sizeof(reg_buffer_tnc_t));
+	reg_buffer->flags = flags;
+
+	reg_args->mmio_state = (void *)OS_ALLOC(sizeof(mmio_regs_tnc));
+	reg_args->mmio_state_sdvo = (void *)OS_ALLOC(sizeof(mmio_regs_sdvo_tnc));
+
+	if (!reg_args->mmio_state) {
+		EMGD_DEBUG("Failed Allocating mmio memory");
+		/* free the "parent" memory*/
+		OS_FREE(reg_args);
+		return NULL;
+	}
+
+	if (!reg_args->mmio_state_sdvo) {
+		EMGD_DEBUG("Failed Allocating sdvo mmio memory");
+		/*Free other dependaant memory*/
+		OS_FREE(reg_args->mmio_state);
+		OS_FREE(reg_args);
+		return NULL;
+	}
+
+	return reg_buffer;
+}
+
+/*!
+ * This function calculates the size of memory to store data
+ *
+ * @param context void pointer to main igd context
+ * @param reg_buffer pointer to register structure returned from a reg_alloc
+ * @param _platform_context
+ *
+ * @return 0
+ */
+static void reg_free_tnc(igd_context_t *context,
+	reg_buffer_t *reg_buffer,
+	void *_platform_context)
+{
+	reg_buffer_tnc_t* reg_args;
+
+	EMGD_DEBUG("Entry - reg_free");
+
+	if (reg_buffer) {
+		reg_args = (reg_buffer_tnc_t*)reg_buffer->platform_buffer;
+
+		if (reg_args) {
+			if (NULL != reg_args->mmio_state) {
+				OS_FREE(reg_args->mmio_state);
+			}
+
+			if (NULL != reg_args->mmio_state_sdvo) {
+				OS_FREE(reg_args->mmio_state_sdvo);
+			}
+
+			if (NULL != reg_args->vga_mem) {
+				OS_FREE(reg_args->vga_mem);
+			}
+
+			if (NULL != reg_args->rb_state) {
+				OS_FREE(reg_args->rb_state);
+			}
+			OS_FREE(reg_args);
+		}
+		OS_FREE(reg_buffer);
+	}
+
+	return;
+}
+
+/*!
+ * This functions save the regs
+ *
+ * @param context void pointer to main igd context
+ * @param reg_buffer pointer to register structure returned from a reg_alloc
+ * @param _platform_context
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+static int reg_save_tnc(igd_context_t *context,
+	reg_buffer_t *reg_buffer,
+	void *_platform_context)
+{
+	reg_buffer_tnc_t           *reg_args;
+	unsigned long              *buffer, *buffer_sdvo;
+	reg_platform_context_tnc_t *platform_context =
+		(reg_platform_context_tnc_t *)_platform_context;
+	int                        i;
+	unsigned char              *mmio, *mmio_sdvo, *mmio_sdvo_st, *mmio_sdvo_st_gpio;
+
+	EMGD_TRACE_ENTER;
+
+	if (reg_buffer == NULL) {
+		return 0;
+	}
+
+	/* Before save need to idle the ring buffer */
+	context->dispatch.idle((igd_driver_h)context);
+
+	reg_args = (reg_buffer_tnc_t *)reg_buffer->platform_buffer;
+	if (reg_args == NULL) {
+		return 0;
+	}
+
+	mmio = context->device_context.virt_mmadr;
+	mmio_sdvo = context->device_context.virt_mmadr_sdvo;
+	mmio_sdvo_st = context->device_context.virt_mmadr_sdvo_st;
+	mmio_sdvo_st_gpio = context->device_context.virt_mmadr_sdvo_st_gpio;
+
+
+	/* Save vga registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA) {
+		EMGD_DEBUG("Saving VGA registers");
+		reg_save_vga_tnc(context, &reg_args->vga_state,
+			platform_context->ar, platform_context->cr, platform_context->sr,
+			platform_context->gr);
+	}
+
+	/* Save VGA memory */
+#if 0
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA_MEM) {
+		EMGD_DEBUG("Saving VGA memory");
+		if (!reg_args->vga_mem) {
+			reg_args->vga_mem = (void *)OS_ALLOC_LARGE(64 * 1024);
+			if (reg_args->vga_mem != NULL) {
+				OS_MEMCPY(reg_args->vga_mem,
+		            (void *)context->device_context.virt_fb_adr, 64*1024);
+			} else {
+				EMGD_ERROR("Unable to allocate memory for vga memory");
+			}
+		} else {
+			OS_MEMCPY(reg_args->vga_mem,
+	            (void *)context->device_context.virt_fb_adr, 64*1024);
+		}
+	}
+#endif
+
+	/* Save mmio registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_MMIO) {
+		EMGD_DEBUG("Saving MMIO registers");
+		buffer = reg_args->mmio_state;
+		for (i=0; platform_context->mmio_regs[i] != 0xffffffff; i++) {
+			*buffer++ = EMGD_READ32(mmio + platform_context->mmio_regs[i]);
+		}
+
+		buffer_sdvo = reg_args->mmio_state_sdvo;
+		for (i=0; platform_context->mmio_regs_sdvo[i] != 0xffffffff; i++) {
+			*buffer_sdvo++ = EMGD_READ32(mmio_sdvo + platform_context->mmio_regs_sdvo[i]);
+		}
+
+		if (reg_buffer->flags & IGD_REG_SAVE_GTT) {
+			reg_save_gtt_tnc(context, mmio, reg_args);
+		}
+	}
+
+	/* Save DAC registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_DAC) {
+		EMGD_DEBUG("Saving DAC registers");
+		reg_save_dac_tnc(context, &reg_args->dac_state, platform_context);
+	}
+
+	/* Save Mode state */
+	if(reg_buffer->flags & IGD_REG_SAVE_MODE) {
+		EMGD_DEBUG("Saving mode state");
+		/* At driver initialization though mode_save is requested, mode isn't
+		 * initialized. So skip the mode_save if dispatch function isn't
+		 * available. In this case, mode_save() will be done as part of
+		 * mode_init(). */
+		if (context->mod_dispatch.mode_save && !reg_buffer->mode_buffer) {
+			context->mod_dispatch.mode_save(context,
+				&reg_buffer->mode_buffer,
+				&reg_buffer->flags);
+		} else {
+			EMGD_DEBUG("mode_save() is skipped as mode_init() isn't done.");
+		}
+	}
+
+	/* Save the SGX clock gating settings */
+	reg_args->clk_gating_ctl = EMGD_READ32(mmio + PSB_CR_CLKGATECTL);
+
+	return 0;
+} /* reg_save */
+
+/*!
+ * This function restores the regs states
+ *
+ * @param context void pointer to main igd context
+ * @param reg_buffer pointer to register structure returned from a reg_alloc
+ * @param _platform_context
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+int reg_restore_tnc(igd_context_t *context,
+	reg_buffer_t *reg_buffer,
+	void *_platform_context)
+{
+	reg_buffer_tnc_t* reg_args;
+	reg_platform_context_tnc_t *platform_context =
+		(reg_platform_context_tnc_t *)_platform_context;
+	unsigned char *mmio, *mmio_sdvo, *mmio_sdvo_st, *mmio_sdvo_st_gpio;
+	unsigned long *buffer, *buffer_sdvo;
+	int i;
+
+	EMGD_DEBUG("Entry - reg_restore");
+
+	if (reg_buffer == NULL) {
+		return 0;
+	}
+
+	reg_args = (reg_buffer_tnc_t *)reg_buffer->platform_buffer;
+	if (reg_args == NULL) {
+		return 0;
+	}
+
+	EMGD_DEBUG(" flags = 0x%lx", reg_buffer->flags);
+
+	mmio = context->device_context.virt_mmadr;
+	mmio_sdvo = context->device_context.virt_mmadr_sdvo;
+	mmio_sdvo_st = context->device_context.virt_mmadr_sdvo_st;
+	mmio_sdvo_st_gpio = context->device_context.virt_mmadr_sdvo_st_gpio;
+
+	/* Restore Mode state */
+	if (reg_buffer->flags & IGD_REG_SAVE_MODE) {
+		if (reg_buffer->mode_buffer) {
+			context->mod_dispatch.mode_restore(context,
+				&reg_buffer->mode_buffer,
+				&reg_buffer->flags);
+		} else {
+			EMGD_DEBUG("mode_restore() already happened in mode_shutdown().");
+		}
+	}
+
+	/* Restore MMIO registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_MMIO) {
+		EMGD_DEBUG("Restoring MMIO registers");
+		buffer = reg_args->mmio_state;
+		for (i=0; platform_context->mmio_regs[i] != 0xffffffff; i++) {
+			if (platform_context->mmio_regs[i] == MI_ARB_STATE) {
+				EMGD_DEBUG("Handle special masked register case");
+				EMGD_WRITE32(0x04100000 | *buffer++, mmio +
+					platform_context->mmio_regs[i]);
+			} else if (platform_context->mmio_regs[i] == DPLLACNTR ||
+				platform_context->mmio_regs[i] == DPLLBCNTR) {
+
+					EMGD_DEBUG("Handle special DPLL settling");
+					EMGD_WRITE32(*buffer++,
+						mmio + platform_context->mmio_regs[i]);
+					OS_SLEEP(150);
+			} else {
+				EMGD_WRITE32(*buffer++, mmio + platform_context->mmio_regs[i]);
+			}
+		}
+//		reg_restore_gtt_tnc(context, reg_args);
+
+		buffer_sdvo = reg_args->mmio_state_sdvo;
+		for (i=0; platform_context->mmio_regs_sdvo[i] != 0xffffffff; i++) {
+			if (platform_context->mmio_regs_sdvo[i] == MI_ARB_STATE) {
+				EMGD_DEBUG("Handle special masked register case");
+				EMGD_WRITE32(0x04100000 | *buffer_sdvo++, mmio_sdvo +
+					platform_context->mmio_regs_sdvo[i]);
+			} else if (platform_context->mmio_regs_sdvo[i] == DPLLACNTR ||
+				platform_context->mmio_regs_sdvo[i] == DPLLBCNTR) {
+
+					EMGD_DEBUG("Handle special DPLL settling");
+					EMGD_WRITE32(*buffer_sdvo++,
+						mmio_sdvo + platform_context->mmio_regs_sdvo[i]);
+					OS_SLEEP(150);
+			} else {
+				EMGD_WRITE32(*buffer_sdvo++, mmio_sdvo + platform_context->mmio_regs_sdvo[i]);
+			}
+		}
+
+		if (reg_buffer->flags & IGD_REG_SAVE_GTT) {
+			reg_restore_gtt_tnc(context, reg_args);
+		}
+	}
+
+#if 0
+	/* Restore VGA memory */
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA_MEM) {
+		if (reg_args->vga_mem) {
+			OS_MEMCPY((void *)context->device_context.virt_fb_adr,
+					reg_args->vga_mem, 64*1024);
+		} else {
+			EMGD_ERROR("Trying to restore NULL VGA memory");
+		}
+	}
+#endif
+
+	/* Restore DAC registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_DAC) {
+		reg_restore_dac_tnc(context, &reg_args->dac_state, platform_context);
+	}
+
+	/* Restore VGA registers */
+	if (reg_buffer->flags & IGD_REG_SAVE_VGA) {
+		reg_restore_vga_tnc(context, &reg_args->vga_state,
+			platform_context->ar, platform_context->cr, platform_context->sr,
+			platform_context->gr);
+	}
+
+	/* Restore the SGX clock gating settings */
+	EMGD_WRITE32(reg_args->clk_gating_ctl, mmio + PSB_CR_CLKGATECTL);
+
+	return 0;
+}
+
+/*!
+ * This function saves the GTT table entries into a buffer so that the GTT
+ * can be restored later.
+ *
+ * @param context needs this to get the GTT table size and to get the
+ * 	virtual address to the GTT table
+ * @param mmio virtual address to MMIO
+ * @param reg_args a pre-allocated context where GTT entries are saved
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+static int reg_save_gtt_tnc(igd_context_t *context, unsigned char *mmio,
+	reg_buffer_tnc_t *reg_args)
+{
+	unsigned int  i;
+
+	/* Read the GTT entries from GTT ADR and save it in the array. */
+	for (i = 0; i < (context->device_context.gatt_pages); i++) {
+		reg_args->gtt[i] = EMGD_READ32(
+			context->device_context.virt_gttadr + i);
+	}
+
+	/* Save the address of the SGX MMU */
+	reg_args->sgx_phyaddr = EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+
+	return 0;
+}
+
+/*!
+ *  This function restores the GTT table entries.
+ *
+ * @param context the context contains the GTT size and address
+ * @param reg_args this has the saved GTT entries
+ *
+ * @return 0 on success
+ * @return -1 on failure
+ */
+static int reg_restore_gtt_tnc(igd_context_t *context,
+	reg_buffer_tnc_t *reg_args)
+{
+	unsigned int i;
+	unsigned char *mmio;
+	unsigned long sgx_mmu;
+
+	/* If the first element is 0, then nothing was saved */
+	if (0 == reg_args->gtt[0]) {
+		return 0;
+	}
+
+	/* Restore the GTT tablep */
+	for (i=0; i < context->device_context.gatt_pages; i++) {
+		EMGD_WRITE32(reg_args->gtt[i],
+			context->device_context.virt_gttadr + i);
+	}
+
+	mmio = context->device_context.virt_mmadr;
+
+	/*
+	 * FIXME: The following code restore the SGX MMU. This
+	 * is probably not necessary and may be incorrect.
+	 */
+	/* Invalidate directory cache */
+	sgx_mmu = EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+	sgx_mmu |= 0x1E;
+	EMGD_WRITE32(sgx_mmu, mmio + PSB_CR_BIF_CTRL);
+	EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+
+	EMGD_WRITE32(reg_args->sgx_phyaddr, mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+	EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+
+	/* Turn on host access to aperture via the MMU */
+	sgx_mmu = EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+	sgx_mmu &= 0xFFFF0000;
+	EMGD_WRITE32(sgx_mmu, mmio + PSB_CR_BIF_CTRL);
+	EMGD_READ32(mmio + PSB_CR_BIF_CTRL);
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/utils/math_fix.c b/drivers/gpu/drm/emgd/emgd/utils/math_fix.c
new file mode 100644
index 0000000..84e5811
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/utils/math_fix.c
@@ -0,0 +1,138 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: math_fix.c
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains fixed point implementations of math functions
+ *-----------------------------------------------------------------------------
+ */
+
+
+/******************************************************************************
+ * This is a look-up table for natural log for integer values 1 - 255 in
+ * 24i.8f fixed point format.
+ *
+ * This table returns 0 or ln(0), but it is technically undefined.
+ *****************************************************************************/
+static const int ln_table[] =
+  {
+    0,    0,  177,  281,  355,  412,  459,  498,  532,  562,  589,  614,
+    636,  657,  676,  693,  710,  725,  740,  754,  767,  779,  791,  803,
+    814,  824,  834,  844,  853,  862,  871,  879,  887,  895,  903,  910,
+    917,  924,  931,  938,  944,  951,  957,  963,  969,  975,  980,  986,
+    991,  996, 1001, 1007, 1012, 1016, 1021, 1026, 1030, 1035, 1039, 1044,
+    1048, 1052, 1057, 1061, 1065, 1069, 1073, 1076, 1080, 1084, 1088, 1091,
+    1095, 1098, 1102, 1105, 1109, 1112, 1115, 1119, 1122, 1125, 1128, 1131,
+    1134, 1137, 1140, 1143, 1146, 1149, 1152, 1155, 1158, 1160, 1163, 1166,
+    1168, 1171, 1174, 1176, 1179, 1181, 1184, 1186, 1189, 1191, 1194, 1196,
+    1199, 1201, 1203, 1206, 1208, 1210, 1212, 1215, 1217, 1219, 1221, 1223,
+    1226, 1228, 1230, 1232, 1234, 1236, 1238, 1240, 1242, 1244, 1246, 1248,
+    1250, 1252, 1254, 1256, 1258, 1260, 1261, 1263, 1265, 1267, 1269, 1270,
+    1272, 1274, 1276, 1278, 1279, 1281, 1283, 1284, 1286, 1288, 1289, 1291,
+    1293, 1294, 1296, 1298, 1299, 1301, 1302, 1304, 1306, 1307, 1309, 1310,
+    1312, 1313, 1315, 1316, 1318, 1319, 1321, 1322, 1324, 1325, 1327, 1328,
+    1329, 1331, 1332, 1334, 1335, 1336, 1338, 1339, 1341, 1342, 1343, 1345,
+    1346, 1347, 1349, 1350, 1351, 1353, 1354, 1355, 1356, 1358, 1359, 1360,
+    1361, 1363, 1364, 1365, 1366, 1368, 1369, 1370, 1371, 1372, 1374, 1375,
+    1376, 1377, 1378, 1380, 1381, 1382, 1383, 1384, 1385, 1387, 1388, 1389,
+    1390, 1391, 1392, 1393, 1394, 1395, 1397, 1398, 1399, 1400, 1401, 1402,
+    1403, 1404, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1412, 1413, 1415,
+    1416, 1417, 1418, 1419
+  };
+
+
+
+/*----------------------------------------------------------------------------
+ *
+ * Function: os_pow_fix()
+ *
+ * Parameters:
+ *    [IN] base:  the base, should be between 0 and 255.
+ *    [IN] power:  this must be in 24i.8f format.
+ *
+ * Description:
+ *    This function uses the Taylor Series to approximate the power function
+ *    using fixed-point math.  See this website for the math background
+ *    http://www.efunda.com/math/taylor_series/exponential.cfm
+ *
+ *    This function was originally designed for gamma correction.  When the
+ *    base value is between 1 - 255 and the power is between 0.1 and 2, this
+ *    function will produce an approximation that is within 2% of the "real"
+ *    function.
+ *
+ * Returns:
+ *    The result in 24i.8f format
+ *
+ *----------------------------------------------------------------------------
+ */
+
+unsigned int os_pow_fix( const int base, const int power )
+{
+  /* For some reason using "const unsigned int" causes a compiler error
+   * when I use it below when I declare "result[APPROXIMATE_TERMS]".  So
+   * switched these from "const unsigned .." to #define
+   */
+  #define APPROXIMATE_TERMS     40
+#define FRACTION_BITS         8    /* num of bits for fraction */
+
+  unsigned int       result[APPROXIMATE_TERMS];
+  unsigned int       i, power_loop;
+  unsigned int       total  = 0;
+  int                ln_x   = ln_table[base];  /* ln_x is in 24i.8f format */
+
+
+  /* nothing to do if we get 0 */
+  if (0 == base) {
+    return 0;
+  } else {
+    ln_x = ln_table[base];
+  }
+
+  /* We approximate the result using APPROXIMATE_TERMS terms */
+  for (i = 0; i < APPROXIMATE_TERMS; i++)    {
+
+    result[i] = 1 << FRACTION_BITS;
+
+    /* Need to be very careful about the order in which we are multiplying
+     * multiplying and dividing because we don't want any overflow.  In
+     * addition, every time we multiply 2 fixed point numbers, we need to
+     * shift the result by FRACTION_BITS to maintain the radix point.
+     */
+    for( power_loop = 0; power_loop < i; power_loop++ )    {
+      result[i] *= ln_x;
+      result[i] /= (power_loop + 1);
+      result[i] >>=FRACTION_BITS;
+      result[i] *= power;
+      result[i] >>=FRACTION_BITS;
+
+    }
+
+    total += result[i];
+
+  }
+
+  return total;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/utils/memmap.c b/drivers/gpu/drm/emgd/emgd/utils/memmap.c
new file mode 100644
index 0000000..2ee0812
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/utils/memmap.c
@@ -0,0 +1,55 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: memmap.c
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the XFree86 implementations of the OAL
+ *  memory.h abstractions.
+ *-----------------------------------------------------------------------------
+ */
+#include <io.h>
+
+#ifndef _OAL_LINUX_KERNEL_IO_MEMMAP_H
+#define _OAL_LINUX_KERNEL_IO_MEMMAP_H
+
+
+void * os_map_io_to_mem_nocache(
+				unsigned long base_address,
+				unsigned long size
+				)
+{
+  return ioremap(base_address, size);
+}
+
+void os_unmap_io_from_mem(
+			  void * virt_addr,
+			  unsigned long size
+			  )
+{
+  iounmap(virt_addr);
+}
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/utils/pci.c b/drivers/gpu/drm/emgd/emgd/utils/pci.c
new file mode 100644
index 0000000..969fc6d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/utils/pci.c
@@ -0,0 +1,277 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: pci.c
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains Linux kernel space PCI API for the OAL abstractions.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <memory.h>
+#include <pci.h>
+#include <linux/pci.h>
+#include <io.h>
+
+#if defined(CONFIG_VGA_ARB)
+#include <linux/vgaarb.h>
+#endif
+
+
+//
+// This is our local representation of a PCI device.
+// This struct is private to this file, and exposed to calling functions
+// only through an opaque "os_pci_dev_t" handle.
+//
+typedef struct _linuxkernel_pci {
+  struct pci_dev *dev;
+  unsigned int bus;
+  unsigned int slot;
+  unsigned int func;
+}linuxkernel_pci_t;
+
+/*----------------------------------------------------------------------
+ * Function:
+ *pci_find_device_generic
+ * Parameters:
+ *unsigned short vendor,
+ *unsigned short device,
+ *os_pci_dev_t os_pci_dev
+ * Description:
+ *This function finds a PCI device by going through 255 buses, 32 devices
+ *and 8 functions and tries to match each ones vendor and device ids with
+ *the ones given to it as parameters. Stops for the first device it finds
+ *with a matching vendor and device, so it will not find multiple devices.
+ *This is _NOT_ an exported OAL
+ *function.
+ * Returns:
+ *os_pci_dev_t
+ *---------------------------------------------------------------------
+ */
+static os_pci_dev_t pci_find_device_generic(
+					    unsigned short vendor_id,
+					    unsigned short device_id,
+					    os_pci_dev_t pci_dev_handle
+					    )
+{
+  struct pci_dev *our_device; // Kernel struct for a PCI device
+  linuxkernel_pci_t *pdev; // Our struct for a PCI device.
+
+    // Locate the device, and lock it. Start search at the start of the list.
+    our_device = pci_get_device(vendor_id, device_id, NULL);
+    // If we didn't find it, return an error.
+    if (!our_device){
+      return (os_pci_dev_t)NULL;
+    }
+
+    // Get the pointer to the destination for the data.
+    // pci_dev_handle is an opaque pointer to a linuxkernel_pci_t struct
+    // If there isn't one, allocate it.
+    pdev = (linuxkernel_pci_t *)pci_dev_handle;
+    if(!pdev) {
+
+      // Caller did not supply a handle to a PCI device struct.
+      // Allocate one.
+      pdev = (linuxkernel_pci_t *)OS_ALLOC(sizeof(linuxkernel_pci_t));
+      if(!pdev) {
+	return (os_pci_dev_t)NULL;
+      }
+      // Zero the destination memory
+      memset(pdev, 0, sizeof(linuxkernel_pci_t));
+    }
+
+    // Copy over from the kernel struct to our struct.
+    // It is safe to copy a pointer to the pci_dev, since we
+    // have a lock on it.
+    pdev->dev = our_device;
+    pdev->bus = our_device->bus->number;
+    pdev->slot = PCI_SLOT(our_device->devfn);
+    pdev->func = PCI_FUNC(our_device->devfn);
+
+    return (os_pci_dev_t)pdev;
+}
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *os_pci_find_device
+ *
+ *  Parameters:
+ *unsigned short vendor,
+ *unsigned short device,
+ *unsigned short bus,
+ *unsigned short dev,
+ *unsigned short func,
+ *os_pci_dev_t os_pci_dev
+ *
+ *  Description:
+ *  This function finds a PCI device by scanning the specified bus, dev, func
+ *and tries to match vendor and device ids with the ones given to it as
+ *parameters.
+ *
+ *  Notes: If the bus number is 0xFFFF, then the function searches for that
+ *  vendor_id, device_id pair in the whole PCI topology of the system i.e
+ *  it goes through all the buses, devices, functions in the system
+ *
+ * Returns:
+ *os_pci_dev_t
+ *---------------------------------------------------------------------
+ */
+os_pci_dev_t os_pci_find_device(
+				unsigned short vendor_id,
+				unsigned short device_id,
+				unsigned short bus,
+				unsigned short dev,
+				unsigned short func,
+				os_pci_dev_t pci_dev)
+{
+  /* TODO: Right now, Just fallback to pci_find_device_generic
+   * But we need to implement this
+   */
+  return pci_find_device_generic(vendor_id, device_id, pci_dev);
+}
+
+int os_pci_get_slot_address(
+			    os_pci_dev_t pci_dev,
+			    unsigned int *bus,
+			    unsigned int *slot,
+			    unsigned int *func)
+{
+
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+
+  if(bus) {
+    *bus = pdev->bus;
+  }
+  if(slot) {
+    *slot = pdev->slot;
+  }
+  if(func) {
+    *func = pdev->func;
+  }
+  return 0;
+}
+
+int os_pci_read_config_8(
+			 os_pci_dev_t pci_dev,
+			 unsigned long offset,
+			 unsigned char* val
+			 )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  EMGD_ASSERT(val, "Invalid pointer", 0);
+  return pci_read_config_byte(pdev->dev, offset, val);
+}
+
+int os_pci_read_config_16(
+			  os_pci_dev_t pci_dev,
+			  unsigned long offset,
+			  unsigned short* val
+			  )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  EMGD_ASSERT(val, "Invalid pointer", 0);
+  return pci_read_config_word(pdev->dev, offset,val);
+}
+
+int os_pci_read_config_32(
+			  os_pci_dev_t pci_dev,
+			  unsigned long offset,
+			  unsigned long* val
+			  )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  EMGD_ASSERT(val, "Invalid pointer", 0);
+  return pci_read_config_dword(pdev->dev, offset, (u32*)val);
+}
+
+int os_pci_write_config_8(
+			  os_pci_dev_t pci_dev,
+			  unsigned long offset,
+			  unsigned char val
+			  )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  return pci_write_config_byte(pdev->dev, offset, val);
+}
+
+int os_pci_write_config_16(
+			   os_pci_dev_t pci_dev,
+			   unsigned long offset,
+			   unsigned short val
+			   )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  return pci_write_config_word(pdev->dev, offset, val);
+}
+
+int os_pci_write_config_32(
+			   os_pci_dev_t pci_dev,
+			   unsigned long offset,
+			   unsigned long val
+			   )
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+  EMGD_ASSERT(pdev, "Invalid pci device", 0);
+  return pci_write_config_dword(pdev->dev, offset, val);
+}
+
+int os_pci_disable_legacy_vga_decoding(
+			os_pci_dev_t pci_dev
+		)
+{
+#if defined(CONFIG_VGA_ARB)
+	linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+	EMGD_ASSERT(pdev, "Invalid pci device", 0);
+
+	vga_set_legacy_decoding(pdev->dev, VGA_RSRC_NONE);
+#else
+	/* Noop if the VGA arbiter isn't compiled into the kernel */
+#endif
+
+	return 0;
+}
+
+
+void os_pci_free_device(
+			os_pci_dev_t pci_dev
+			)
+{
+  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
+
+  // Release the lock on our PCI device struct
+  pci_dev_put(pdev->dev);
+
+  // Free our local structure.
+  OS_FREE(pdev);
+
+  return;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx.c b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx.c
new file mode 100644
index 0000000..644c3cc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx.c
@@ -0,0 +1,913 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: msvdx.c
+ * $Revision: 1.27 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Send commands to the MSVDX video decode engine.
+ *  The host communicates with the firmware via messages. The following
+ *  messages are supported:
+ *  RENDER           -> MTX
+ *  DEBLOCK          -> MTX
+ *  OOLD             -> MTX
+ *  MSG_PADDING      -> MTX
+ *  CMD_COMPLETED    <- MTX
+ *  DEBLOCK_REQUIRED <- MTX
+ *  CMD_FAILED       <- MTX
+ *  HW_PANIC         <- MTX
+ *-----------------------------------------------------------------------------
+ */
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+#include <sched.h>
+
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+#include <utils.h>
+#include <msvdx.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+#include "services_headers.h"
+#include <drm_emgd_private.h>
+
+
+
+//extern int msvdx_init_plb(void);
+extern void msvdx_reset_plb(igd_context_t *context);
+
+int send_to_mtx(igd_context_t *context, unsigned long *msg);
+int msvdx_poll_mtx_irq(igd_context_t *context);
+void msvdx_mtx_interrupt_plb(igd_context_t *context);
+void populate_fence_id(igd_context_t *context, unsigned long *mtx_msgs,
+		unsigned long mtx_msg_cnt);
+int process_mtx_messages(igd_context_t *context,
+		unsigned long *mtx_msgs, unsigned long mtx_msg_cnt,
+		unsigned long fence);
+#ifdef DEBUG_BUILD_TYPE
+static void debug_mesg_info(igd_context_t *context,
+		unsigned long *msg,
+		unsigned long num_words);
+/* To eliminate warnings, am temporarily #ifdef'ing this function.  Please
+ * remove the #ifdef when this function is to be used.
+ */
+#ifdef USE_DEBUG_DUMP
+static void debug_dump(igd_context_t *context);
+#endif
+static void lldma_dump(unsigned long *cmd_base, unsigned long offset);
+static void dump_all_messages(igd_context_t *context);
+#define DEBUG_MESG_INFO(a, b, c) debug_mesg_info(a, b, c)
+#define DEBUG_DUMP(a) debug_dump(a)
+#define LLDMA_DUMP(a, b) lldma_dump(a, b)
+#define DUMP_ALL_MESSAGES(a) dump_all_messages(a)
+#else
+#define DEBUG_MESG_INFO(a, b, c)
+#define DEBUG_DUMP(a)
+#define LLDMA_DUMP(a, b)
+#define DUMP_ALL_MESSAGES(a)
+#endif
+
+/* These are used to debug the message buffer */
+unsigned long *save_msg;
+unsigned long save_msg_cnt;
+
+/* This is used to discover when MSVDX gets in a bad state and stops generating
+ * interrupts.  By keeping track of when the last time a cmd was dequeued, when
+ * cmds continue to be queued, but never dequeued, this bad hardware state can
+ * be detected.
+ */
+unsigned long jiffies_at_last_dequeue = 0;
+
+int msvdx_dequeue_send(igd_context_t *context)
+{
+    platform_context_plb_t *platform;
+    struct msvdx_cmd_queue *msvdx_cmd = NULL;
+    int ret = 0;
+
+    EMGD_TRACE_ENTER;
+
+    platform = (platform_context_plb_t *)context->platform_context;
+
+    if (list_empty(&platform->msvdx_queue)) {
+       	//printk(KERN_ERR "MSVDXQUE: msvdx list empty\n");
+        platform->msvdx_busy = 0;
+        return -EINVAL;
+    }
+
+	//printk(KERN_INFO "MSVDXQUE:  get from the msvdx list\n");
+    msvdx_cmd =  list_first_entry(&platform->msvdx_queue,
+				struct msvdx_cmd_queue, head);
+
+    ret = process_mtx_messages(context, msvdx_cmd->cmd, msvdx_cmd->cmd_size, platform->msvdx_fence);
+
+	jiffies_at_last_dequeue = jiffies;
+
+    if (ret) {
+        printk(KERN_ERR "MSVDXQUE: process_mtx_messages failed\n");
+        ret = -EINVAL;
+    }
+
+    list_del(&msvdx_cmd->head);
+	msvdx_cmd->cmd = NULL;
+	kfree(msvdx_cmd);
+
+    return ret;
+}
+
+void populate_fence_id(igd_context_t *context, unsigned long *mtx_msgs,
+		unsigned long mtx_msg_cnt)
+{
+	platform_context_plb_t *platform;
+	unsigned long submit_size;
+	unsigned long submit_id;
+	unsigned int msg;
+
+	platform = (platform_context_plb_t *)context->platform_context;
+
+	for (msg = 0; msg < mtx_msg_cnt; msg++) {
+		submit_size = (mtx_msgs[0] & 0x000000ff);
+		submit_id = (mtx_msgs[0] & 0x0000ff00) >> 8;
+
+		if (submit_id == IGD_MSGID_RENDER) {
+			mtx_msgs[4] = ++platform->msvdx_fence;
+			//printk(KERN_INFO "Update fence id %lx\n", mtx_msgs[4]);
+		}
+
+		mtx_msgs += (submit_size / sizeof(unsigned long));
+	}
+}
+
+/*
+ * The incoming command buffer holds MTX firmware messages, register pairs,
+ * and the LLDMA linked lists. There is a simple header at the beginning of
+ * the buffer that has the MTX message offset and the number of MTX messages.
+ *
+ * To process this buffer, find the MTX firmware messages and send each
+ * one to the MTX firmware.
+ */
+int process_mtx_messages(igd_context_t *context,
+		unsigned long *mtx_msgs, unsigned long mtx_msg_cnt,
+		unsigned long fence)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	platform_context_plb_t *platform;
+	unsigned long submit_size;
+	unsigned long submit_id;
+	unsigned int msg;
+	unsigned long skipped_msg_cnt;
+    unsigned long msvdx_status;
+
+	EMGD_TRACE_ENTER;
+
+	platform = (platform_context_plb_t *)context->platform_context;
+
+    // message processing is about to start .. set the flag=bit 2
+    spin_lock(&platform->msvdx_init_plb);
+    platform->msvdx_status = platform->msvdx_status | 2;
+    msvdx_status = platform->msvdx_status;
+    spin_unlock(&platform->msvdx_init_plb);
+
+	if (msvdx_status & 1)
+	{
+	    // OOPS: reset/fw load in progress ... return from here
+        spin_lock(&platform->msvdx_init_plb);
+        platform->msvdx_status = platform->msvdx_status & ~2;  // unset message processing status.
+        spin_unlock(&platform->msvdx_init_plb);
+
+        return 0;
+    }
+
+	save_msg = mtx_msgs;
+	save_msg_cnt = mtx_msg_cnt;
+	skipped_msg_cnt = 0;
+
+	for (msg = 0; msg < mtx_msg_cnt; msg++) {
+		submit_size = (mtx_msgs[0] & 0x000000ff);
+		submit_id = (mtx_msgs[0] & 0x0000ff00) >> 8;
+
+		if (submit_id != IGD_MSGID_RENDER) {
+			/* Error, unknown message id, skip it */
+			EMGD_ERROR("Unknown MTX message id 0x%lx", submit_id);
+			skipped_msg_cnt++;
+			continue;
+		}
+
+		/* reuse the sgx phy PD */
+		mtx_msgs[1] = platform->psb_cr_bif_dir_list_base1 | 1;
+
+		/*
+		 * If the send returns busy, then retry sending the message, otherwise
+		 * move to next message in buffer.
+		 */
+		if (send_to_mtx(context, (unsigned long *)mtx_msgs) != -IGD_ERROR_BUSY) {
+			mtx_msgs += (submit_size / sizeof(unsigned long));
+			/*
+			 * msvdx_mtx_interrupt_plb checks the firmware-to-host buffer
+			 * and if there are messages there, processes them.
+			 */
+			 //msvdx_mtx_interrupt_plb(context);
+		} else {
+			msg--; /* Reset count back to unsent message */
+			/* Should this wait a bit? */
+		}
+	}
+
+    // We are done processing messages .. unset the flag
+	spin_lock(&platform->msvdx_init_plb);
+    platform->msvdx_status = platform->msvdx_status & ~2;
+    spin_unlock(&platform->msvdx_init_plb);
+
+	EMGD_TRACE_EXIT;
+	if (skipped_msg_cnt == mtx_msg_cnt) {
+		/* We failed to submit anything; the entire buffer was bad.
+		 * Just return a failure code so we can unmark the video engine
+		 * busy bit. */
+		return -IGD_ERROR_INVAL;
+	} else {
+		return 0;
+	}
+}
+
+
+
+
+/*
+ * This is the function that actually passes the message to the MTX
+ * firmware. The contents of the message are opaque to this function.
+ *
+ * Currently, the only supported message type is RENDER.
+ */
+int send_to_mtx(igd_context_t *context, unsigned long *msg)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long pad_msg;
+	unsigned long num_words;
+	unsigned long words_free;
+	unsigned long read_idx, write_idx;
+	platform_context_plb_t *platform =
+		(platform_context_plb_t *)context->platform_context;
+	int padding_flag = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Enable all clocks before touching VEC local ram */
+	EMGD_WRITE32(PSB_CLK_ENABLE_ALL, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+	/* The first two longs in the msg have the message ID and size */
+	num_words = ((msg[0] & 0xff) + 3) / 4;
+
+	/* Is message too big? */
+	if (num_words > platform->mtx_buf_size) {
+		/* TODO: Error ? */
+		EMGD_ERROR("Message is too large (size=%ld, max=%ld).", num_words,
+				platform->mtx_buf_size);
+		return -IGD_ERROR_INVAL;
+	}
+
+	/*
+	 * Safely increment the "number of messages in flight" counter, but only
+	 * if this isn't a "padding" message.
+	 */
+	/*
+	if (((msg[0] & 0xff00) >> 8) != FWRK_MSGID_PADDING) {
+		platform->mtx_submitted++;
+	}
+	*/
+
+	/*
+	 * Make sure the MTX is enabled
+	 */
+	EMGD_WRITE32(MSVDX_MTX_ENABLE_MTX_ENABLE_MASK, mmio + PSB_MSVDX_MTX_ENABLE);
+
+
+	read_idx  = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_MTX_RD_INDEX);
+	write_idx = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX);
+
+	EMGD_DEBUG("MTX read = 0x%08lx  write = 0x%08lx", read_idx, write_idx);
+
+	/*
+	 * Check to see if there is room for this message, if not send
+	 * a pad message to use up all the space and wait for the next
+	 * slot.
+	 */
+	if ((write_idx + num_words) > platform->mtx_buf_size) {
+		/*
+		 * if the read pointer is at zero, then the engine is probably
+		 * hung and not processing the message. This is bad.
+		 */
+		if (read_idx == 0) {
+			platform->msvdx_needs_reset = 1;
+			EMGD_ERROR("MSVDX Engine is hung? Aborting send.");
+			DUMP_ALL_MESSAGES(context);
+			return -IGD_ERROR_BUSY;
+		}
+
+		/*
+		 * The message id and size are encoded into the first word of
+		 * the message.
+		 * bits  0:7  size (in long words?)
+		 * bits  8:15 message id
+		 */
+		pad_msg = (platform->mtx_buf_size - write_idx) << 2; /* size */
+		pad_msg |= (FWRK_MSGID_PADDING << 8); /* message id */
+		EMGD_DEBUG("Sending a pad_mesg: 0x%x, size = %ld", FWRK_MSGID_PADDING,
+				(pad_msg & 0xff));
+
+		/*
+		 * Maybe just try writing the message directly here, instead of calling
+		 * this fuction recursivly??
+		 */
+		EMGD_WRITE32(pad_msg,
+				mmio + platform->mtx_buf_offset + (write_idx << 2));
+		write_idx = 0;
+
+		/* Update the write index to the next free location */
+		EMGD_WRITE32(write_idx, mmio + PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX);
+		EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX);
+
+		padding_flag = 1;
+	}
+
+	/* Verify free space available */
+	words_free = (write_idx >= read_idx) ?
+		platform->mtx_buf_size - (write_idx - read_idx) : read_idx - write_idx;
+
+	if (num_words > (words_free - 1)) {
+		/* There is no space available, this isn't an error */
+
+		if (padding_flag) {
+			/* Make sure clocks are enabled before we kick */
+			EMGD_WRITE32(PSB_CLK_ENABLE_ALL, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+			/* Send an interrupt to the MTX to let it know about the message */
+			EMGD_WRITE32(1, mmio + PSB_MSVDX_MTX_KICK);
+		}
+
+		return - IGD_ERROR_BUSY;
+	}
+
+	/*
+	 * DEBUGGING info:
+	 *  Call a function to try and output some debugging info about the message
+	 */
+	/* DEBUG_MESG_INFO(context, msg, num_words); */
+
+	/* Write the message to the firmware */
+	while (num_words > 0) {
+		EMGD_WRITE32(*msg++,
+				mmio + platform->mtx_buf_offset + (write_idx << 2));
+		num_words--;
+		write_idx++;
+	}
+
+	/* Check for wrap in the buffer */
+	if (write_idx == platform->mtx_buf_size) {
+		write_idx = 0;
+	}
+
+	/* Update the write index to the next free location */
+	EMGD_WRITE32(write_idx, mmio + PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX);
+
+	/* Check for overwrite */
+	if (write_idx == read_idx) {
+		EMGD_ERROR("Overwrite detected, resetting MSVDX.");
+		platform->msvdx_needs_reset = 1;
+	}
+
+	/* Send an interrupt to the MTX to let it know about the message */
+	EMGD_WRITE32(1, mmio + PSB_MSVDX_MTX_KICK);
+
+	/* Read MSVDX Register several times in case idle signal assert */		
+	EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_STATUS);
+	EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_STATUS);
+	EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_STATUS);
+	EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_STATUS);
+	
+
+#if 0
+	DEBUG_DUMP(context); /* For lots of additional debugging info */
+#endif
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+/*
+ * This function will look at messages sent from the MTX Firmware
+ * and decode them.
+ */
+
+void msvdx_mtx_interrupt_plb(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long msg[128];
+	unsigned long read_idx, write_idx;
+	unsigned long num_words;
+	unsigned long msg_offset;
+	unsigned long fence = 0;
+    unsigned long flags = 0;
+	unsigned long status;
+	platform_context_plb_t *platform =
+		(platform_context_plb_t *)context->platform_context;
+
+	/* Check that the clocks are enabled before trying to read message */
+	EMGD_WRITE32(PSB_CLK_ENABLE_ALL, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+	do {
+		read_idx  = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_HOST_RD_INDEX);
+		write_idx = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_HOST_WRT_INDEX);
+		EMGD_DEBUG("HOST read=0x%lx write=0x%lx", read_idx, write_idx);
+
+		if (read_idx != write_idx) {
+			/* Read the first waiting message from the buffer */
+			msg_offset = 0;
+			msg[msg_offset] = EMGD_READ32(mmio + (platform->host_buf_offset +
+						(read_idx << 2)));
+
+			/* Size of message rounded to nearest number of words */
+			num_words = ((msg[0] & 0x000000ff) + 3) / 4;
+
+			if (++read_idx >= platform->host_buf_size) {
+				read_idx = 0;
+			}
+
+			for (msg_offset++; msg_offset < num_words; msg_offset++) {
+				msg[msg_offset] = EMGD_READ32(mmio + platform->host_buf_offset +
+						(read_idx << 2));
+
+				if (++read_idx >= platform->host_buf_size) {
+					read_idx = 0;
+				}
+			}
+
+			EMGD_WRITE32(read_idx, mmio + PSB_MSVDX_COMMS_TO_HOST_RD_INDEX);
+
+			/* Check message ID */
+			switch ((msg[0] & 0x0000ff00) >> 8) {
+			case IGD_MSGID_CMD_FAILED:
+				fence  = msg[1]; /* Fence value */
+				status = msg[2]; /* Failed IRQ Status */
+				printk(KERN_ERR "MSGID_CMD, status = 0x%lx, "
+					"fence = 0x%lx\n", status, fence);
+
+				/* This message from MTX is for informational purposes and
+				   firmware does not expect hardware to be reset */
+				platform->msvdx_needs_reset = 1;
+
+				if (msg[1] != 0) {
+					platform->mtx_completed = fence;
+				}
+				DUMP_ALL_MESSAGES(context);
+				goto done;
+				break;
+			case IGD_MSGID_CMD_COMPLETED:
+				fence = msg[1];
+                flags = msg[2];
+
+				//printk(KERN_INFO "MSGID_CMD_COMPLETED: fence 0x%lx\n", fence);
+				platform->mtx_completed = fence;
+				jiffies_at_last_dequeue = 0;
+
+                if (flags & FW_VA_RENDER_HOST_INT) {
+                    spin_lock(&platform->msvdx_lock);
+                    msvdx_dequeue_send(context);
+                    spin_unlock(&platform->msvdx_lock);
+                }
+
+				break;
+			case IGD_MSGID_CMD_HW_PANIC:
+				fence  = msg[1]; /* Fence value */
+				status = msg[2]; /* Failed IRQ Status */
+				printk(KERN_ERR "MSGID_CMD_HW_PANIC: fence 0x%08lx, "
+					"Irq 0x%08lx\n", msg[1], msg[2]);
+				platform->msvdx_needs_reset = 1;
+				platform->mtx_completed = fence;
+				DUMP_ALL_MESSAGES(context);
+				goto done;
+				break;
+			default:
+				EMGD_ERROR("Unknown message ID 0x%lx response from firmware.", (msg[0] & 0x0000ff00) >> 8);
+				break;
+			}
+
+		}
+	} while (read_idx != write_idx);
+
+done:
+	return;
+}
+
+
+/*
+ * Simple function to check for a MSVDX interrupt.  Clear the interrupt status
+ * if one occured.  Mostly just ignore them for now.
+ */
+
+#define MSVDX_MMU_FAULT_IRQ_MASK                0x00000F00
+#define MSVDX_MTX_IRQ_MASK                      0x00004000
+#define MSVDX_MMU_CONTROL0_CR_MMU_PAUSE_MASK    0x00000002
+
+int msvdx_poll_mtx_irq(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long status;
+	platform_context_plb_t *platform =
+		(platform_context_plb_t *)context->platform_context;
+	int poll_cnt = 100;
+
+	EMGD_TRACE_ENTER;
+
+	while (poll_cnt) {
+		status = EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_STATUS);
+		if (status & MSVDX_MMU_FAULT_IRQ_MASK) {
+			EMGD_DEBUG("MMU FAULT Interrupt");
+			/* Pause the MMU */
+			EMGD_WRITE32(MSVDX_MMU_CONTROL0_CR_MMU_PAUSE_MASK,
+					mmio + PSB_MSVDX_MMU_CONTROL0);
+
+			/* Clear interrupt bit */
+			EMGD_WRITE32(MSVDX_MMU_FAULT_IRQ_MASK,
+					mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+			EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+
+			platform->msvdx_needs_reset = 1;
+			DUMP_ALL_MESSAGES(context);
+			return 0;
+		} else if (status & MSVDX_MTX_IRQ_MASK) {
+			/* Clear all interrupt bits */
+			EMGD_WRITE32(0xffffffff, mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+			EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+
+			msvdx_mtx_interrupt_plb(context);
+			return 0;
+		} else if (status) {
+			/* Might want to make this debug only. */
+			if (status & 0x00000001) EMGD_DEBUG("IRQ - VEC_END_OF_SLICE");
+			if (status & 0x00000002) EMGD_DEBUG("IRQ - VEC_ERROR_DETECTETED_SR");
+			if (status & 0x00000004) EMGD_DEBUG("IRQ - VEC_ERROR_DETECTETED_ENTDEC");
+			if (status & 0x00000008) EMGD_DEBUG("IRQ - VEC_RENDEC_ERROR");
+			if (status & 0x00000010) EMGD_DEBUG("IRQ - VEC_RENDEC_OVERFLOW");
+			if (status & 0x00000020) EMGD_DEBUG("IRQ - VEC_RENDEC_UNDERFLOW");
+			if (status & 0x00000040) EMGD_DEBUG("IRQ - VEC_RENDEC_MTXBLOCK");
+			if (status & 0x00000080) EMGD_DEBUG("IRQ - VEC_RENDEC_END_OF_SLICE");
+			if (status & 0x00000100) EMGD_DEBUG("IRQ - MMU_FAULT_IRQ");
+			if (status & 0x00000200) EMGD_DEBUG("IRQ - MMU_FAULT_IRQ");
+			if (status & 0x00000400) EMGD_DEBUG("IRQ - MMU_FAULT_IRQ");
+			if (status & 0x00000800) EMGD_DEBUG("IRQ - MMU_FAULT_IRQ");
+			if (status & 0x00001000) EMGD_DEBUG("IRQ - FE_WDT_CM0");
+			if (status & 0x00002000) EMGD_DEBUG("IRQ - FE_WDT_CM1");
+			if (status & 0x00004000) EMGD_DEBUG("IRQ - MTX_IRQ");
+			if (status & 0x00008000) EMGD_DEBUG("IRQ - MTX_GPIO_IRQ");
+			if (status & 0x00010000) EMGD_DEBUG("IRQ - VDMC_IRQ");
+			if (status & 0x00020000) EMGD_DEBUG("IRQ - VDEB_PICTURE_DONE_IRQ");
+			if (status & 0x00040000) EMGD_DEBUG("IRQ - VDEB_SLICE_DONE_IRQ");
+			if (status & 0x00080000) EMGD_DEBUG("IRQ - VDEB_FLUSH_DONE_IRQ");
+			if (status & 0x00100000) EMGD_DEBUG("IRQ - DMAC_IRQ");
+			if (status & 0x00200000) EMGD_DEBUG("IRQ - DMAC_IRQ");
+			if (status & 0x00400000) EMGD_DEBUG("IRQ - DMAC_IRQ");
+			if (status & 0x00800000) EMGD_DEBUG("IRQ - VDEB_FAULT_IRQ");
+			if (status & 0x01000000) EMGD_DEBUG("IRQ - SYS_COMMAND_TIMEOUT_IRQ");
+			if (status & 0x02000000) EMGD_DEBUG("IRQ - SYS_READ_TIMEOUT_IRQ");
+			if (status & 0x04000000) EMGD_DEBUG("IRQ - MTX_COMMAND_TIMEOUT_IRQ");
+			if (status & 0x08000000) EMGD_DEBUG("IRQ - MTX_READ_TIMEOUT_IRQ");
+			if (status & 0x10000000) EMGD_DEBUG("IRQ - SYS_WDT");
+			if (status & 0x20000000) EMGD_DEBUG("IRQ - BE_WDT_CM0");
+			if (status & 0x40000000) EMGD_DEBUG("IRQ - BE_WDT_CM1");
+			if (status & 0x80000000) EMGD_DEBUG("IRQ - VEC_RENDEC_SLICE_SKIPPED");
+
+			EMGD_DEBUG("  Watchdog Control = 0x%x  0x%x  0x%x",
+					EMGD_READ32(mmio + MSVDX_BASE + 0x0670),
+					EMGD_READ32(mmio + MSVDX_BASE + 0x0674),
+					EMGD_READ32(mmio + MSVDX_BASE + 0x0678));
+
+			/* Clear the interrupt */
+			EMGD_WRITE32(status, mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+			EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+			return 0;
+		}
+
+		poll_cnt--;
+		OS_SLEEP(10);
+	}
+
+	EMGD_ERROR("Timout polling interrupt status.");
+	return 1;
+}
+
+
+/*
+ * msvdx_mtx_irq
+ *
+ *  Process a MTX interrupt.  Look at the interrupt status bits
+ *  and do the processing dictated by the bits.  Currently only
+ *  two interrupts are really handled:
+ *     MMU Fault, which pauses the MMU
+ *     MTX IRQ, which then processes any firmware to hosts messages
+ *  Other interrupts will output a debugging message.  These are fault
+ *  conditions that are not handled.
+ *
+ *  return 0 if the IRQ was handled
+ *  return 1 if it wasn't
+ */
+//static void msvdx_mtx_irq(struct drm_device *dev)
+IMG_BOOL msvdx_mtx_isr(IMG_VOID *pvData)
+{
+    struct drm_device *dev;
+    drm_emgd_priv_t *priv;
+    igd_context_t *context;
+	unsigned char *mmio;
+	platform_context_plb_t *platform;
+    unsigned long msvdx_stat,temp;
+
+	//EMGD_TRACE_ENTER;
+    dev = (struct drm_device *)pvData;
+    priv = dev->dev_private;
+    context = priv->context;
+    mmio =  context->device_context.virt_mmadr;
+    platform = context->platform_context;
+
+    msvdx_stat = EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_STATUS);
+
+	//printk(KERN_ALERT "MSVDX_IRQ\n");
+
+	if (msvdx_stat & MSVDX_MMU_FAULT_IRQ_MASK) {
+		EMGD_DEBUG(KERN_ERR "MMU FAULT Interrupt\n");
+		EMGD_DEBUG(KERN_ERR "INTERRUPT_STATUS Register=0x%x\n",msvdx_stat);
+		temp = EMGD_READ32(mmio + PSB_MSVDX_MMU_DIR_LIST_BASE0);
+        EMGD_DEBUG(KERN_ERR "MMU_DIR_LIST_BASE0 = 0x%x\n", temp);
+	    	temp = EMGD_READ32(mmio + PSB_MSVDX_MMU_DIR_LIST_BASE1);
+		EMGD_DEBUG(KERN_ERR "MMU_DIR_LIST_BASE1 = 0x%x\n", temp);
+	    	temp = EMGD_READ32(mmio + PSB_MSVDX_MMU_DIR_LIST_BASE2);
+		EMGD_DEBUG(KERN_ERR "MMU_DIR_LIST_BASE2 = 0x%x\n", temp);
+	    	temp = EMGD_READ32(mmio + PSB_MSVDX_MMU_DIR_LIST_BASE3);
+		EMGD_DEBUG(KERN_ERR "MMU_DIR_LIST_BASE3 = 0x%x\n", temp);
+		/* Pause the MMU */
+		EMGD_WRITE32(MSVDX_MMU_CONTROL0_CR_MMU_PAUSE_MASK,
+				mmio + PSB_MSVDX_MMU_CONTROL0);
+
+
+		EMGD_WRITE32(MSVDX_MMU_FAULT_IRQ_MASK,
+				mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+
+		//printk(KERN_INFO "FAULT ADDR=%x\n", EMGD_READ32(mmio + PSB_MSVDX_MMU_STATUS));
+		platform->msvdx_needs_reset = 1;
+		DUMP_ALL_MESSAGES(context);
+		//return 0;
+	} else if (msvdx_stat & MSVDX_MTX_IRQ_MASK) {
+		/* Read the firmware to host messages */
+
+        EMGD_WRITE32(0xffffffff, mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+		EMGD_READ32(mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+		msvdx_mtx_interrupt_plb(context);
+		//return 0;
+	} else if (msvdx_stat) {
+		/*
+		 * Print a debug message for any other interrupt cases.
+		 */
+		if (msvdx_stat & 0x00000001) printk(KERN_INFO "IRQ - VEC_END_OF_SLICE\n");
+		if (msvdx_stat & 0x00000002) printk(KERN_INFO "IRQ - VEC_ERROR_DETECTETED_SR\n");
+		if (msvdx_stat & 0x00000004) printk(KERN_INFO "IRQ - VEC_ERROR_DETECTETED_ENTDEC\n");
+		if (msvdx_stat & 0x00000008) printk(KERN_INFO "IRQ - VEC_RENDEC_ERROR\n");
+		if (msvdx_stat & 0x00000010) printk(KERN_INFO "IRQ - VEC_RENDEC_OVERFLOW\n");
+		if (msvdx_stat & 0x00000020) printk(KERN_INFO "IRQ - VEC_RENDEC_UNDERFLOW\n");
+		if (msvdx_stat & 0x00000040) printk(KERN_INFO "IRQ - VEC_RENDEC_MTXBLOCK\n");
+		if (msvdx_stat & 0x00000080) printk(KERN_INFO "IRQ - VEC_RENDEC_END_OF_SLICE\n");
+		if (msvdx_stat & 0x00000100) printk(KERN_INFO "IRQ - MMU_FAULT_IRQ\n");
+		if (msvdx_stat & 0x00000200) printk(KERN_INFO "IRQ - MMU_FAULT_IRQ\n");
+		if (msvdx_stat & 0x00000400) printk(KERN_INFO "IRQ - MMU_FAULT_IRQ\n");
+		if (msvdx_stat & 0x00000800) printk(KERN_INFO "IRQ - MMU_FAULT_IRQ\n");
+		if (msvdx_stat & 0x00001000) printk(KERN_INFO "IRQ - FE_WDT_CM0\n");
+		if (msvdx_stat & 0x00002000) printk(KERN_INFO "IRQ - FE_WDT_CM1\n");
+		if (msvdx_stat & 0x00004000) printk(KERN_INFO "IRQ - MTX_IRQ\n");
+		if (msvdx_stat & 0x00008000) printk(KERN_INFO "IRQ - MTX_GPIO_IRQ\n");
+		if (msvdx_stat & 0x00010000) printk(KERN_INFO "IRQ - VDMC_IRQ\n");
+		if (msvdx_stat & 0x00020000) printk(KERN_INFO "IRQ - VDEB_PICTURE_DONE_IRQ\n");
+		if (msvdx_stat & 0x00040000) printk(KERN_INFO "IRQ - VDEB_SLICE_DONE_IRQ\n");
+		if (msvdx_stat & 0x00080000) printk(KERN_INFO "IRQ - VDEB_FLUSH_DONE_IRQ\n");
+		if (msvdx_stat & 0x00100000) printk(KERN_INFO "IRQ - DMAC_IRQ\n");
+		if (msvdx_stat & 0x00200000) printk(KERN_INFO "IRQ - DMAC_IRQ\n");
+		if (msvdx_stat & 0x00400000) printk(KERN_INFO "IRQ - DMAC_IRQ\n");
+		if (msvdx_stat & 0x00800000) printk(KERN_INFO "IRQ - VDEB_FAULT_IRQ\n");
+		if (msvdx_stat & 0x01000000) printk(KERN_INFO "IRQ - SYS_COMMAND_TIMEOUT_IRQ\n");
+		if (msvdx_stat & 0x02000000) printk(KERN_INFO "IRQ - SYS_READ_TIMEOUT_IRQ\n");
+		if (msvdx_stat & 0x04000000) printk(KERN_INFO "IRQ - MTX_COMMAND_TIMEOUT_IRQ\n");
+		if (msvdx_stat & 0x08000000) printk(KERN_INFO "IRQ - MTX_READ_TIMEOUT_IRQ\n");
+		if (msvdx_stat & 0x10000000) printk(KERN_INFO "IRQ - SYS_WDT\n");
+		if (msvdx_stat & 0x20000000) printk(KERN_INFO "IRQ - BE_WDT_CM0\n");
+		if (msvdx_stat & 0x40000000) printk(KERN_INFO "IRQ - BE_WDT_CM1\n");
+		if (msvdx_stat & 0x80000000) printk(KERN_INFO "IRQ - VEC_RENDEC_SLICE_SKIPPED\n");
+
+		printk(KERN_INFO "  Watchdog Control = 0x%x  0x%x  0x%x\n",
+				EMGD_READ32(mmio + MSVDX_BASE + 0x0670),
+				EMGD_READ32(mmio + MSVDX_BASE + 0x0674),
+				EMGD_READ32(mmio + MSVDX_BASE + 0x0678));
+
+		return 0;
+	}
+    /*
+    else {
+		return ;
+	} */
+    return IMG_TRUE;
+}
+
+
+#ifdef DEBUG_BUILD_TYPE
+
+static void dump_all_messages(igd_context_t *context)
+{
+	unsigned long msg;
+	unsigned long submit_size;
+	unsigned long num_words;
+
+	if (save_msg) {
+		for (msg = 0; msg < save_msg_cnt; msg++) {
+			submit_size = (save_msg[0] & 0x000000ff);
+			num_words = ((save_msg[0] & 0xff) + 3) / 4;
+
+			DEBUG_MESG_INFO(context, save_msg, num_words);
+
+			save_msg += (submit_size / sizeof(unsigned long));
+		}
+
+		save_msg = NULL;
+		save_msg_cnt = 0;
+	}
+}
+
+/*
+ * This function is for debugging firmware messages. It should not be part of the
+ * production code.   Make sure it is removed (if def'ed out) before building a
+ * production version.
+ */
+
+static void debug_mesg_info(igd_context_t *context,
+		unsigned long *msg,
+		unsigned long num_words)
+{
+	unsigned long msg_size;
+	unsigned char msg_id;
+	unsigned short buffer_size;
+	unsigned long buffer_address;
+	unsigned short last_mb;
+	unsigned short first_mb;
+	unsigned long *cmd_base;
+
+	msg_size = (msg[0] & 0xff);
+	msg_id = (unsigned char)((msg[0] & 0xff00) >> 8);
+	buffer_size = (unsigned short)((msg[0] & 0xffff0000) >> 16);
+
+	EMGD_DEBUG("MSG: size = %ld, id = 0x%x, buffer_size = %d",
+			msg_size, msg_id, buffer_size);
+	if (msg_size > 4) {
+		EMGD_DEBUG("MSG: MMUPTD         = 0x%08lx", msg[1]);
+	}
+	if (msg_size > 8) {
+		buffer_address = msg[2];
+		/* FIXME - The following line of code is wrong.  The buffer_address is
+		 * not an offset to GMM-allocated memory, and so cmd_base is always
+		 * NULL (see fixme in lldma_dump()).  The buffer_address is an offset
+		 * within a larger buffer allocated through the user-space PVR2D code
+		 * (and thus, allocated by PVRSRV).  Unless the PVRSRV mapping between
+		 * buffer_address and a kernel-visible address can be found, this
+		 * buffer cannot be accessed, as this code is being called from a
+		 * kernel interrupt handler, which has no access to a user-space
+		 * process's memory.
+		 */
+		cmd_base = (unsigned long *)context->dispatch.gmm_map(buffer_address);
+		EMGD_DEBUG("MSG: Buffer Offset  = 0x%08lx", buffer_address);
+		EMGD_DEBUG("MSG: Context id     = 0x%lx", msg[3]);
+		EMGD_DEBUG("MSG: Fence value    = 0x%lx", msg[4]);
+		EMGD_DEBUG("MSG: Operating Mode = 0x%lx", msg[5]);
+		last_mb = (unsigned short)((msg[6] & 0xffff0000) >> 16);
+		first_mb = (unsigned short)(msg[6] & 0x0000ffff);
+		EMGD_DEBUG("MSG: First MB       = %d, last mb = %d", first_mb, last_mb);
+		EMGD_DEBUG("MSG: Flags          = 0x%lx", msg[7]);
+
+		/* Dump some of the lldma contents here */
+		LLDMA_DUMP(cmd_base, buffer_address);
+	}
+}
+
+
+/* To eliminate warnings, am temporarily #ifdef'ing this function.  Please
+ * remove the #ifdef when this function is to be used.
+ */
+#ifdef USE_DEBUG_DUMP
+/*
+ * This function dumps the to firmware message buffer.
+ */
+static void debug_dump(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long read_idx, write_idx;
+	unsigned long i;
+	platform_context_plb_t *platform;
+
+	platform = (platform_context_plb_t *)context->platform_context;
+	read_idx = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_HOST_RD_INDEX);
+	write_idx = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_HOST_WRT_INDEX);
+	EMGD_DEBUG("HOST buffer: RDIDX 0x%08lx   WRIDX 0x%08lx, %lu",
+		read_idx, write_idx, platform->host_buf_size);
+	for (i = 0; (i < read_idx || i < write_idx); i++) {
+		unsigned long value =
+			EMGD_READ32(mmio + platform->host_buf_offset + (i <<2));
+		EMGD_DEBUG("   %02lx: 0x%08lx", i, value);
+	}
+
+	read_idx = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_MTX_RD_INDEX);
+	write_idx = EMGD_READ32(mmio + PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX);
+	EMGD_DEBUG("MTX buffer: RDIDX 0x%08lx   WRIDX 0x%08lx, %lu",
+		read_idx, write_idx, platform->mtx_buf_size);
+	for (i = 0; (i < read_idx || i < write_idx); i++) {
+		unsigned long value =
+			EMGD_READ32(mmio + platform->mtx_buf_offset + (i <<2));
+		EMGD_DEBUG("   %02lx: 0x%08lx", i, value);
+	}
+}
+#endif
+
+unsigned long lldma_decode(unsigned long val,
+		unsigned long to,
+		unsigned long from)
+{
+	if (to < 31) {
+		val &= ~(0xffffffff << (to + 1));
+	}
+	val = val >> from;
+
+	return val;
+}
+
+static void lldma_dump(unsigned long *cmd_base, unsigned long offset)
+{
+	int i;
+
+	/* FIXME - When the code in debug_mesg_info() is fixed (see the fixme in
+	 * that function), the following if-return code can be removed.
+	 */
+	if (cmd_base == NULL) {
+		return;
+	}
+
+	for (i = 0; i < 8; i++) {
+		if (i == 0) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: BSWAP     = %ld", offset, lldma_decode(*cmd_base, 31, 31));
+			EMGD_DEBUG("            LLDMA: DIR       = %ld", lldma_decode(*cmd_base, 30, 30));
+			EMGD_DEBUG("            LLDMA: PW        = %ld", lldma_decode(*cmd_base, 29, 28));
+		} else if (i == 1) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: List_Fin  = %ld", offset+1, lldma_decode(*cmd_base, 31, 31));
+			EMGD_DEBUG("            LLDMA: List_INT  = %ld", lldma_decode(*cmd_base, 30, 30));
+			EMGD_DEBUG("            LLDMA: PI        = %ld", lldma_decode(*cmd_base, 18, 17));
+			EMGD_DEBUG("            LLDMA: INCR      = %ld", lldma_decode(*cmd_base, 16, 16));
+			EMGD_DEBUG("            LLDMA: LEN       = %ld", lldma_decode(*cmd_base, 15, 0));
+		} else if (i == 2) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: ADDR      = 0x%lx", offset+2, lldma_decode(*cmd_base, 22, 0));
+		} else if (i == 3) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: ACC_DEL   = %ld", offset+3, lldma_decode(*cmd_base, 31, 29));
+			EMGD_DEBUG("            LLDMA: BURST     = %ld", lldma_decode(*cmd_base, 28, 26));
+			EMGD_DEBUG("            LLDMA: EXT_SA    = 0x%lx", lldma_decode(*cmd_base, 3, 0));
+		} else if (i == 4) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: 2D_MODE   = %ld", offset+4, lldma_decode(*cmd_base, 16, 16));
+			EMGD_DEBUG("            LLDMA: REP_CNT   = %ld", lldma_decode(*cmd_base, 10, 0));
+		} else if (i == 5) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: LINE_ADD  = 0x%lx", offset+5, lldma_decode(*cmd_base, 25, 16));
+			EMGD_DEBUG("            LLDMA: ROW_LEN   = %ld", lldma_decode(*cmd_base, 9, 0));
+		} else if (i == 6) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: SA        = 0x%lx", offset+6, lldma_decode(*cmd_base, 31, 0));
+		} else if (i == 7) {
+			EMGD_DEBUG(" 0x%08lx LLDMA: LISTPTR   = 0x%lx", offset+7, lldma_decode(*cmd_base, 27, 0));
+		}
+		cmd_base++;
+	}
+}
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_init.c b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_init.c
new file mode 100644
index 0000000..6591224
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_init.c
@@ -0,0 +1,1516 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: msvdx_init.c
+ * $Revision: 1.31 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Initialize the MSVDX video engine.  This loads the MTX firmware and
+ *  starts a MTX thread running the firmware.
+ *  The host communicates with the firmware via messages. The following
+ *  messages are supported:
+ *  INIT               -> MTX
+ *  RENDER             -> MTX
+ *  DEBLOCK            -> MTX
+ *  BUBBLE             -> MTX
+ *  TEST1              -> MTX
+ *  TEST2              -> MTX
+ *  CMD_COMPLETED      <- MTX
+ *  CMD_COMPLTED_BATCH <- MTX
+ *  DEBLOCK_REQUIRED   <- MTX
+ *  TEST_RESPONSE      <- MTX
+ *  ACK                <- MTX
+ *  CMD_FAILED         <- MTX
+ *-----------------------------------------------------------------------------
+ */
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+#include <sched.h>
+
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+#include <utils.h>
+#include <msvdx.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+#include <drm/drm.h>
+#include <drm_emgd_private.h>
+#include <emgd_drm.h>
+#include <osfunc.h>  /* for OSFlushCPUCacheKM() */
+#include "msvdx_pvr.h"
+
+struct drm_device *gpDrmDevice = NULL;
+static int init_msvdx_first_time = 1;
+static unsigned long msvdx_compositor_mmu_base = 0;
+extern void send_to_mtx(igd_context_t *context, unsigned long *init_msg);
+extern int process_mtx_messages(igd_context_t *context,
+		unsigned long *mtx_msgs, unsigned long mtx_msg_cnt,
+		unsigned long fence);
+
+extern void populate_fence_id(igd_context_t *context, unsigned long *mtx_msgs,
+		unsigned long mtx_msg_cnt);
+extern int msvdx_dequeue_send(igd_context_t *context);
+extern int alloc_ramdec_region(unsigned long *base_addr0, unsigned long *base_addr1,
+				unsigned long size0, unsigned long size1);
+
+static int poll_mtx_irq(igd_context_t *context);
+static int reg_ready_psb(igd_context_t *context, unsigned long reg,
+        unsigned long mask, unsigned long value);
+int context_count = 0;
+
+void msvdx_reset_plb(igd_context_t *context);
+
+static msvdx_fw_t *priv_fw = NULL;
+
+extern unsigned long jiffies_at_last_dequeue;
+static int msvdx_fw_dma_upload = 1;
+
+MODULE_PARM_DESC(msvdx_dma_upload, "MSVDX: upload firmware using DMA");
+module_param_named(msvdx_dma_upload, msvdx_fw_dma_upload, int, 0600);
+unsigned long LastClockState;
+
+///// HHP TODO: Move to separate .h file
+#define MSVDX_MTX_DEBUG      PSB_MSVDX_MTX_RAM_BANK
+
+#define MSVDX_MTX_DEBUG_MTX_DBG_IS_SLAVE_MASK            (0x00000004)
+#define MSVDX_MTX_DEBUG_MTX_DBG_IS_SLAVE_LSBMASK         (0x00000001)
+#define MSVDX_MTX_DEBUG_MTX_DBG_IS_SLAVE_SHIFT           (2)
+
+#define MSVDX_MTX_DEBUG_MTX_DBG_GPIO_IN_MASK             (0x00000003)
+#define MSVDX_MTX_DEBUG_MTX_DBG_GPIO_IN_LSBMASK          (0x00000003)
+#define MSVDX_MTX_DEBUG_MTX_DBG_GPIO_IN_SHIFT            (0)
+
+
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_BURSTSIZE_MASK        (0x07000000)
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_BURSTSIZE_SHIFT       (24)
+
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_RNW_MASK              (0x00020000)
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_RNW_SHIFT             (17)
+
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_ENABLE_MASK           (0x00010000)
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_ENABLE_SHIFT          (16)
+
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_LENGTH_MASK           (0x0000FFFF)
+#define MTX_CORE_CR_MTX_SYSC_CDMAC_LENGTH_SHIFT          (0)
+
+//#define MSVDX_CORE_CR_MSVDX_CONTROL_DMAC_CH0_SELECT_MASK   (0x00001000)
+//#define MSVDX_CORE_CR_MSVDX_CONTROL_DMAC_CH0_SELECT_SHIFT  (12)
+
+#define PSB_MSVDX_CONTROL_DMAC_CH0_SELECT_MASK           (0x00001000)
+#define PSB_MSVDX_CONTROL_DMAC_CH0_SELECT_SHIFT          (12)
+
+
+#define DMAC_DMAC_COUNT_BSWAP_LSBMASK                    (0x00000001)
+#define DMAC_DMAC_COUNT_BSWAP_SHIFT                      (30)
+
+#define DMAC_DMAC_COUNT_PW_LSBMASK                       (0x00000003)
+#define DMAC_DMAC_COUNT_PW_SHIFT                         (27)
+
+#define DMAC_DMAC_COUNT_DIR_LSBMASK                      (0x00000001)
+#define DMAC_DMAC_COUNT_DIR_SHIFT                        (26)
+
+#define DMAC_DMAC_COUNT_PI_LSBMASK                       (0x00000003)
+#define DMAC_DMAC_COUNT_PI_SHIFT                         (24)
+
+#define DMAC_DMAC_COUNT_EN_MASK                          (0x00010000)
+#define DMAC_DMAC_COUNT_EN_SHIFT                         (16)
+
+#define DMAC_DMAC_COUNT_CNT_LSBMASK                      (0x0000FFFF)
+#define DMAC_DMAC_COUNT_CNT_SHIFT                        (0)
+
+
+#define DMAC_DMAC_PERIPH_ACC_DEL_LSBMASK                 (0x00000007)
+#define DMAC_DMAC_PERIPH_ACC_DEL_SHIFT                   (29)
+
+#define DMAC_DMAC_PERIPH_INCR_LSBMASK                    (0x00000001)
+#define DMAC_DMAC_PERIPH_INCR_SHIFT                      (27)
+
+#define DMAC_DMAC_PERIPH_BURST_LSBMASK                   (0x00000007)
+#define DMAC_DMAC_PERIPH_BURST_SHIFT                     (24)
+
+#define DMAC_DMAC_PERIPHERAL_ADDR_ADDR_MASK              (0x007FFFFF)
+#define DMAC_DMAC_PERIPHERAL_ADDR_ADDR_LSBMASK           (0x007FFFFF)
+#define DMAC_DMAC_PERIPHERAL_ADDR_ADDR_SHIFT             (0)
+
+
+#define DMAC_DMAC_IRQ_STAT_TRANSFER_FIN_MASK             (0x00020000)
+
+/*watch dog for FE and BE*/
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_OFFSET		(MSVDX_BASE + 0x0064)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_CONTROL, FE_WDT_CNT_CTRL
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CNT_CTRL_MASK		(0x00060000)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CNT_CTRL_LSBMASK		(0x00000003)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CNT_CTRL_SHIFT		(17)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_CONTROL, FE_WDT_ENABLE
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ENABLE_MASK		(0x00010000)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ENABLE_LSBMASK		(0x00000001)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ENABLE_SHIFT		(16)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_CONTROL, FE_WDT_ACTION1
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ACTION1_MASK		(0x00003000)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ACTION1_LSBMASK		(0x00000003)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ACTION1_SHIFT		(12)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_CONTROL, FE_WDT_ACTION0
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ACTION0_MASK		(0x00000100)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ACTION0_LSBMASK		(0x00000001)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_ACTION0_SHIFT		(8)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_CONTROL, FE_WDT_CLEAR_SELECT
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CLEAR_SELECT_MASK		(0x00000030)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CLEAR_SELECT_LSBMASK		(0x00000003)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CLEAR_SELECT_SHIFT		(4)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_CONTROL, FE_WDT_CLKDIV_SELECT
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CLKDIV_SELECT_MASK		(0x00000007)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CLKDIV_SELECT_LSBMASK		(0x00000007)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_FE_WDT_CLKDIV_SELECT_SHIFT		(0)
+
+#define MSVDX_CORE_CR_FE_MSVDX_WDTIMER_OFFSET		(MSVDX_BASE + 0x0068)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDTIMER, FE_WDT_COUNTER
+#define MSVDX_CORE_CR_FE_MSVDX_WDTIMER_FE_WDT_COUNTER_MASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_FE_MSVDX_WDTIMER_FE_WDT_COUNTER_LSBMASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_FE_MSVDX_WDTIMER_FE_WDT_COUNTER_SHIFT		(0)
+
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_OFFSET		(MSVDX_BASE + 0x006C)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_COMPAREMATCH, FE_WDT_CM1
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_FE_WDT_CM1_MASK		(0xFFFF0000)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_FE_WDT_CM1_LSBMASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_FE_WDT_CM1_SHIFT		(16)
+
+// MSVDX_CORE, CR_FE_MSVDX_WDT_COMPAREMATCH, FE_WDT_CM0
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_FE_WDT_CM0_MASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_FE_WDT_CM0_LSBMASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_FE_WDT_CM0_SHIFT		(0)
+
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_OFFSET		(MSVDX_BASE + 0x0070)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDT_CONTROL, BE_WDT_CNT_CTRL
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CNT_CTRL_MASK		(0x001E0000)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CNT_CTRL_LSBMASK		(0x0000000F)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CNT_CTRL_SHIFT		(17)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDT_CONTROL, BE_WDT_ENABLE
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_ENABLE_MASK		(0x00010000)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_ENABLE_LSBMASK		(0x00000001)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_ENABLE_SHIFT		(16)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDT_CONTROL, BE_WDT_ACTION0
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_ACTION0_MASK		(0x00000100)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_ACTION0_LSBMASK		(0x00000001)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_ACTION0_SHIFT		(8)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDT_CONTROL, BE_WDT_CLEAR_SELECT
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CLEAR_SELECT_MASK		(0x000000F0)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CLEAR_SELECT_LSBMASK		(0x0000000F)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CLEAR_SELECT_SHIFT		(4)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDT_CONTROL, BE_WDT_CLKDIV_SELECT
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CLKDIV_SELECT_MASK		(0x00000007)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CLKDIV_SELECT_LSBMASK		(0x00000007)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_BE_WDT_CLKDIV_SELECT_SHIFT		(0)
+
+#define MSVDX_CORE_CR_BE_MSVDX_WDTIMER_OFFSET		(MSVDX_BASE + 0x0074)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDTIMER, BE_WDT_COUNTER
+#define MSVDX_CORE_CR_BE_MSVDX_WDTIMER_BE_WDT_COUNTER_MASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_BE_MSVDX_WDTIMER_BE_WDT_COUNTER_LSBMASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_BE_MSVDX_WDTIMER_BE_WDT_COUNTER_SHIFT		(0)
+
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_COMPAREMATCH_OFFSET		(MSVDX_BASE + 0x0078)
+
+// MSVDX_CORE, CR_BE_MSVDX_WDT_COMPAREMATCH, BE_WDT_CM0
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_COMPAREMATCH_BE_WDT_CM0_MASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_COMPAREMATCH_BE_WDT_CM0_LSBMASK		(0x0000FFFF)
+#define MSVDX_CORE_CR_BE_MSVDX_WDT_COMPAREMATCH_BE_WDT_CM0_SHIFT		(0)
+/*watch dog end*/
+
+enum {
+	MSVDX_DMAC_BSWAP_NO_SWAP = 0x0, /* No byte swapping will be performed */
+	MSVDX_DMAC_BSWAP_REVERSE = 0x1, /* Byte order will be reversed */
+};
+
+enum {
+	MSVDX_DMAC_DIR_MEM_TO_PERIPH = 0x0, /* Data from memory to peripheral */
+	MSVDX_DMAC_DIR_PERIPH_TO_MEM = 0x1, /* Data from peripheral to memory */
+};
+
+enum {
+	MSVDX_DMAC_ACC_DEL_0    = 0x0, /* Access delay zero clock cycles */
+	MSVDX_DMAC_ACC_DEL_256  = 0x1, /* Access delay 256 clock cycles */
+	MSVDX_DMAC_ACC_DEL_512  = 0x2, /* Access delay 512 clock cycles */
+	MSVDX_DMAC_ACC_DEL_768  = 0x3, /* Access delay 768 clock cycles */
+	MSVDX_DMAC_ACC_DEL_1024 = 0x4, /* Access delay 1024 clock cycles */
+	MSVDX_DMAC_ACC_DEL_1280 = 0x5, /* Access delay 1280 clock cycles */
+	MSVDX_DMAC_ACC_DEL_1536 = 0x6, /* Access delay 1536 clock cycles */
+	MSVDX_DMAC_ACC_DEL_1792 = 0x7, /* Access delay 1792 clock cycles */
+};
+
+enum {
+	MSVDX_DMAC_INCR_OFF  = 0x0, /* Static peripheral address */
+	MSVDX_DMAC_INCR_ON   = 0x1, /* Incrementing peripheral address */
+};
+
+enum {
+	MSVDX_DMAC_BURST_0   = 0x0, /* burst size of 0 */
+	MSVDX_DMAC_BURST_1   = 0x1, /* burst size of 1 */
+	MSVDX_DMAC_BURST_2   = 0x2, /* burst size of 2 */
+	MSVDX_DMAC_BURST_3   = 0x3, /* burst size of 3 */
+	MSVDX_DMAC_BURST_4   = 0x4, /* burst size of 4 */
+	MSVDX_DMAC_BURST_5   = 0x5, /* burst size of 5 */
+	MSVDX_DMAC_BURST_6   = 0x6, /* burst size of 6 */
+	MSVDX_DMAC_BURST_7   = 0x7, /* burst size of 7 */
+};
+
+/* DMAC control */
+#define MSVDX_DMAC_VALUE_COUNT(BSWAP,PW,DIR,PERIPH_INCR,COUNT) \
+	(((BSWAP) & DMAC_DMAC_COUNT_BSWAP_LSBMASK) << DMAC_DMAC_COUNT_BSWAP_SHIFT) | \
+	(((PW)    & DMAC_DMAC_COUNT_PW_LSBMASK)    << DMAC_DMAC_COUNT_PW_SHIFT)    | \
+	(((DIR)   & DMAC_DMAC_COUNT_DIR_LSBMASK)   << DMAC_DMAC_COUNT_DIR_SHIFT)   | \
+	(((PERIPH_INCR) & DMAC_DMAC_COUNT_PI_LSBMASK) << DMAC_DMAC_COUNT_PI_SHIFT) | \
+	(((COUNT) & DMAC_DMAC_COUNT_CNT_LSBMASK)   << DMAC_DMAC_COUNT_CNT_SHIFT)
+
+#define MSVDX_DMAC_VALUE_PERIPH_PARAM(ACC_DEL,INCR,BURST) \
+	(((ACC_DEL) & DMAC_DMAC_PERIPH_ACC_DEL_LSBMASK) << DMAC_DMAC_PERIPH_ACC_DEL_SHIFT) | \
+	(((INCR)    & DMAC_DMAC_PERIPH_INCR_LSBMASK)    << DMAC_DMAC_PERIPH_INCR_SHIFT)    | \
+	(((BURST)   & DMAC_DMAC_PERIPH_BURST_LSBMASK)   << DMAC_DMAC_PERIPH_BURST_SHIFT)
+
+
+
+#define REGIO_READ_FIELD(reg_val, reg, field)                           \
+        ((reg_val & reg##_##field##_MASK) >> reg##_##field##_SHIFT)
+
+#define REGIO_WRITE_FIELD(reg_val, reg, field, value)                   \
+        (reg_val) =                                                     \
+                ((reg_val) & ~(reg##_##field##_MASK)) |                 \
+               (((value) << (reg##_##field##_SHIFT)) & (reg##_##field##_MASK));
+
+#define REGIO_WRITE_FIELD_LITE(reg_val, reg, field, value)              \
+        (reg_val) =                                                     \
+                ((reg_val) | ((value) << (reg##_##field##_SHIFT)));
+
+#define STACKGUARDWORD (0x10101010)
+
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_CORE_MAN_CLK_ENABLE_MASK	\
+	(0x00000001)
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_VDEB_PROCESS_MAN_CLK_ENABLE_MASK \
+	(0x00000002)
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_VDEB_ACCESS_MAN_CLK_ENABLE_MASK \
+	(0x00000004)
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_VDMC_MAN_CLK_ENABLE_MASK	\
+	(0x00000008)
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_VEC_ENTDEC_MAN_CLK_ENABLE_MASK \
+	(0x00000010)
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_VEC_ITRANS_MAN_CLK_ENABLE_MASK \
+	(0x00000020)
+#define MSVDX_CORE_CR_MSVDX_MAN_CLK_ENABLE_CR_MTX_MAN_CLK_ENABLE_MASK	\
+	(0x00000040)
+
+
+
+int msvdx_init_compositor_mmu(unsigned long mmu_base) {
+	msvdx_compositor_mmu_base = mmu_base;
+	return 0;
+}
+
+/*
+ * Map and copy the firmware image to the shared SGX heap.
+ */
+static int msvdx_map_fw(uint32_t size)
+{
+	drm_emgd_priv_t       *priv;
+	igd_context_t          *context;
+	platform_context_plb_t *platform;
+	PVRSRV_KERNEL_MEM_INFO *mem_info;
+	unsigned long alloc_size;
+	uint32_t *last_word;
+	void     *mapped_fw_addr;
+
+	priv     = gpDrmDevice->dev_private;
+	context  = priv->context;
+    platform = (platform_context_plb_t *)context->platform_context;
+
+	/* Round up as DMA's can overrun a page */
+	alloc_size = (size + 8192) & ~0x0fff;
+
+	mem_info = platform->msvdx_pvr->fw_mem_info;
+	if (!mem_info) {
+		mem_info = msvdx_pvr_alloc_devmem(alloc_size, "MSVDX firmware");
+		if (!mem_info) {
+			printk(KERN_ERR "[EMGD] MSVDX: Failed to allocate %u "
+				"bytes from SGX heap\n",
+				(unsigned int)alloc_size);
+			return -ENOMEM;
+		}
+		platform->msvdx_pvr->fw_mem_info = mem_info;
+	}
+
+	mapped_fw_addr = (unsigned long *)mem_info->pvLinAddrKM;
+
+	memset(mapped_fw_addr, 0x00, size);
+
+	memcpy(mapped_fw_addr, priv_fw->fw_text, priv_fw->fw_text_size * 4);
+
+	memcpy(mapped_fw_addr + (priv_fw->fw_data_location - MTX_DATA_BASE),
+		priv_fw->fw_data, priv_fw->fw_data_size * 4);
+
+	/*
+	 * Write a known value to the last word in MTX memory. Useful for
+	 * detection of stack overruns.
+	 */
+	last_word = (uint32_t *)(mapped_fw_addr + size - sizeof(uint32_t));
+	*last_word = STACKGUARDWORD;
+
+	OSFlushCPUCacheRangeKM(mapped_fw_addr, mapped_fw_addr + size);
+
+	return 0;
+}
+
+static void msvdx_get_mtx_control_from_dash(igd_context_t *context)
+{
+	platform_context_plb_t *platform;
+	unsigned char *mmio;
+	int count = 0;
+	uint32_t reg_val = 0;
+
+	mmio = context->device_context.virt_mmadr;
+        platform = (platform_context_plb_t *)context->platform_context;
+
+	REGIO_WRITE_FIELD(reg_val, MSVDX_MTX_DEBUG, MTX_DBG_IS_SLAVE, 1);
+	REGIO_WRITE_FIELD(reg_val, MSVDX_MTX_DEBUG, MTX_DBG_GPIO_IN, 0x02);
+	EMGD_WRITE32(reg_val, mmio + MSVDX_MTX_DEBUG);
+
+	do {		reg_val = EMGD_READ32(mmio + MSVDX_MTX_DEBUG);
+		count++;
+	} while (((reg_val & 0x18) != 0) && count < 50000);
+
+	if (count >= 50000)
+		printk(KERN_ERR "[EMGD] MSVDX: timeout in %s\n", __FUNCTION__);
+
+	/* Save the RAM access control register */
+	platform->msvdx_dash_access_ctrl =
+		EMGD_READ32(mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+}
+
+static void msvdx_release_mtx_control_from_dash(igd_context_t *context)
+{
+	platform_context_plb_t *platform;
+	unsigned char *mmio;
+
+	mmio = context->device_context.virt_mmadr;
+        platform = (platform_context_plb_t *)context->platform_context;
+
+	/* Restore access control */
+	EMGD_WRITE32(platform->msvdx_dash_access_ctrl,
+		mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+
+	/* Release the bus */
+	EMGD_WRITE32(0x04, mmio + MSVDX_MTX_DEBUG);
+}
+
+static int msvdx_upload_fw_dma(uint32_t address)
+{
+	drm_emgd_priv_t       *priv;
+	igd_context_t          *context;
+	platform_context_plb_t *platform;
+	unsigned long addr;
+	unsigned char *mmio;
+	uint32_t core_rev;
+	uint32_t size;
+	uint32_t cmd;
+	uint32_t count_reg;
+	uint32_t dma_channel;
+	uint32_t reg_val;
+	int ret;
+
+    printk(KERN_INFO "MSVDX: Upload firmware by DMA\n");
+
+	priv     = gpDrmDevice->dev_private;
+	context  = priv->context;
+	mmio     = context->device_context.virt_mmadr;
+       platform = (platform_context_plb_t *)context->platform_context;
+
+	dma_channel = 0; /* Use DMA channel 0 */
+
+	core_rev = EMGD_READ32(mmio + PSB_MSVDX_CORE_REV);
+	if ((core_rev & 0xffffff) < 0x020000)
+		size = 16 * 1024; /* mtx_mem_size */
+	else
+		size = 40 * 1024;
+
+	if (platform->msvdx_pvr && !platform->msvdx_pvr->fw_mem_info) {
+		ret = msvdx_map_fw(size);
+		if (ret)
+			return ret;
+	}
+
+	msvdx_get_mtx_control_from_dash(context);
+
+	/*
+	 * dma transfers to/from the mtx have to be 32-bit aligned and in
+	 * multiples of 32 bits
+	 */
+	EMGD_WRITE32(address, mmio + PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAA);
+
+	reg_val = 0;
+	/* Burst size in multiples of 64 bytes (allowed values are 2 or 4) */
+	REGIO_WRITE_FIELD_LITE(reg_val, MTX_CORE_CR_MTX_SYSC_CDMAC, BURSTSIZE, 4);
+
+	/* False means write to MTX mem, true means read from MTX mem */
+	REGIO_WRITE_FIELD_LITE(reg_val, MTX_CORE_CR_MTX_SYSC_CDMAC, RNW, 0);
+
+	/* Begin transfer */
+	REGIO_WRITE_FIELD_LITE(reg_val, MTX_CORE_CR_MTX_SYSC_CDMAC, ENABLE, 1);
+
+	/* DMA transfer is in size of 32-bit words */
+	REGIO_WRITE_FIELD_LITE(reg_val, MTX_CORE_CR_MTX_SYSC_CDMAC, LENGTH, (size/4));
+
+	EMGD_WRITE32(reg_val, mmio + PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAC);
+
+	/* Toggle channel 0 usage between MTX and other MSVDX peripherals */
+	reg_val = EMGD_READ32(mmio + PSB_MSVDX_CONTROL);
+	REGIO_WRITE_FIELD(reg_val, PSB_MSVDX_CONTROL, DMAC_CH0_SELECT, 0);
+	EMGD_WRITE32(reg_val, mmio + PSB_MSVDX_CONTROL);
+
+	/* Clear the DMAC Stats */
+	EMGD_WRITE32(0, mmio + PSB_MSVDX_DMAC_IRQ_STAT + (dma_channel * 0x20));
+
+	addr = platform->msvdx_pvr->fw_mem_info->sDevVAddr.uiAddr;
+
+	/* Use bank 0 */
+	EMGD_WRITE32(0, mmio + PSB_MSVDX_MMU_BANK_INDEX);
+
+	/* Use the same MMU PTD as the SGX for MMU base 0 */
+	EMGD_WRITE32(platform->psb_cr_bif_dir_list_base1,
+		mmio + PSB_MSVDX_MMU_DIR_LIST_BASE0);
+
+	/* Invalidate and flush TLB */
+	msvdx_flush_tlb(context);
+
+	EMGD_WRITE32(addr,
+		mmio + PSB_MSVDX_DMAC_SETUP + (dma_channel * 0x20));
+
+	/* Only use single DMA - assert that this is valid */
+	if ((size / 4) >= (1 << 15)) {
+		printk(KERN_ERR "[EMGD] MSVDX: DMA size beyond limit. "
+			"Firmware uploading aborted.\n");
+        msvdx_release_mtx_control_from_dash(context);
+		return -ENODEV;
+	}
+
+        count_reg = MSVDX_DMAC_VALUE_COUNT(MSVDX_DMAC_BSWAP_NO_SWAP,
+					0,  /* 32 bits */
+					MSVDX_DMAC_DIR_MEM_TO_PERIPH,
+					0,
+					(size / 4));
+
+	/* Set the number of bytes to DMA */
+	EMGD_WRITE32(count_reg,
+		mmio + PSB_MSVDX_DMAC_COUNT + (dma_channel * 0x20));
+
+	cmd = MSVDX_DMAC_VALUE_PERIPH_PARAM(MSVDX_DMAC_ACC_DEL_0,
+					MSVDX_DMAC_INCR_OFF,
+					MSVDX_DMAC_BURST_2);
+	EMGD_WRITE32(cmd, mmio + PSB_MSVDX_DMAC_PERIPH + (dma_channel * 0x20));
+
+	/* Set the destination port for DMA */
+	cmd = 0;
+	REGIO_WRITE_FIELD(cmd, DMAC_DMAC_PERIPHERAL_ADDR,
+		ADDR, (PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAT - MSVDX_BASE));
+	EMGD_WRITE32(cmd,
+		mmio + PSB_MSVDX_DMAC_PERIPHERAL_ADDR + (dma_channel * 0x20));
+
+	/* Finally, rewrite the count register with the enable bit set */
+	EMGD_WRITE32(count_reg | DMAC_DMAC_COUNT_EN_MASK,
+		mmio + PSB_MSVDX_DMAC_COUNT + (dma_channel * 0x20));
+
+	/* Wait for DMA to complete */
+	if (reg_ready_psb(context,
+			PSB_MSVDX_DMAC_IRQ_STAT + (dma_channel * 0x20),
+			DMAC_DMAC_IRQ_STAT_TRANSFER_FIN_MASK,
+			DMAC_DMAC_IRQ_STAT_TRANSFER_FIN_MASK)) {
+		msvdx_release_mtx_control_from_dash(context);
+
+		printk(KERN_ERR "[EMGD] MSVDX: DMA firmware upload timed "
+			"out\n");
+
+		return -ENODEV;
+	}
+
+	/* Assert that the MTX DMA port is done */
+	if (reg_ready_psb(context,
+			PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_CDMAS0, 1, 1)) {
+		msvdx_release_mtx_control_from_dash(context);
+		printk(KERN_ERR "[EMGD] MSVDX: MTX DMA port done timed out\n");
+		return -ENODEV;
+	}
+
+	msvdx_release_mtx_control_from_dash(context);
+    printk(KERN_INFO "[EMGD] MSVDX: firmware DMA upload done!\n");
+	return 0;
+}
+
+/*
+ * Upload firmware using PIO
+*/
+static int msvdx_upload_fw(void)
+{
+	drm_emgd_priv_t *priv;
+	igd_context_t *context;
+	unsigned char *mmio;
+	unsigned long ram_bank;
+	unsigned long bank_size;
+	unsigned long current_bank;
+	unsigned long acc_control;
+	unsigned long address;
+	unsigned long fw_size;
+	unsigned long *fw_data;
+	unsigned long ram_id;
+	unsigned long ctrl;
+	unsigned long i;
+
+	priv    = gpDrmDevice->dev_private;
+	context = priv->context;
+	mmio    = context->device_context.virt_mmadr;
+
+	/*
+	 * Get the ram bank size
+	 * The banks size seems to be a 4 bit value in the MTX debug register.
+	 * Where this is documented other than the UMG code is not clear.
+	 */
+	ram_bank = EMGD_READ32(mmio + PSB_MSVDX_MTX_RAM_BANK);
+	bank_size = (ram_bank & 0x000f0000) >> 16;
+	bank_size = (1 << (bank_size + 2));
+
+	/* Save RAM access control register */
+	acc_control = EMGD_READ32(mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+
+	/* Loop writing text/code to core memory */
+	current_bank = ~0L;
+	address = PC_START_ADDRESS - MTX_CODE_BASE;
+
+	fw_data = priv_fw->fw_text;
+	fw_size = priv_fw->fw_text_size;
+
+	for (i = 0; i < fw_size; i++) {
+		/* Wait for MCMSTAT to become be idle 1 */
+		if (reg_ready_psb(context, PSB_MSVDX_MTX_RAM_ACCESS_STATUS,
+					0xffffffff, 0x00000001) == 0) {
+			ram_id = MTX_CORE_CODE_MEM + (address / bank_size);
+			if (ram_id != current_bank) {
+				/*
+				 * bits 20:27 - ram bank (CODE_BASE | DATA_BASE)
+				 * bits  2:19 - address
+				 * bit   1    - enable auto increment
+				 *              addressing mode
+				 */
+				ctrl = (ram_id << 20) | (((address >> 2) & 0x000ffffc) << 2) | 0x02;
+				EMGD_WRITE32(ctrl, mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+
+				current_bank = ram_id;
+				/* Wait for MCMSTAT to become be idle 1 */
+				reg_ready_psb(context,
+					PSB_MSVDX_MTX_RAM_ACCESS_STATUS,
+					0xffffffff, 0x00000001);
+			}
+
+			address +=  4;
+			EMGD_WRITE32(fw_data[i],
+				mmio + PSB_MSVDX_MTX_RAM_ACCESS_DATA_TRANSFER);
+		} else {
+			printk(KERN_ERR
+				"[EMGD] MSVDX: Timeout waiting for MCMSTAT "
+				"to be idle\n");
+		}
+	}
+
+	/* verify firmware upload. */
+	current_bank = ~0L;
+	address = PC_START_ADDRESS - MTX_CODE_BASE;
+
+	for (i = 0; i < fw_size; i++) {
+		if (reg_ready_psb(context, PSB_MSVDX_MTX_RAM_ACCESS_STATUS,
+				0xffffffff, 0x00000001) == 0) {
+			ram_id = MTX_CORE_CODE_MEM + (address / bank_size);
+			if (ram_id != current_bank) {
+				/*
+				 * bits 20:27 - ram bank (CODE_BASE | DATA_BASE)
+				 * bits  2:19 - address
+				 * bit   1    - enable auto increment
+				 *              addressing mode
+				 */
+				ctrl = (ram_id << 20) | (((address >> 2) & 0x000ffffc) << 2) | 0x03;
+				EMGD_WRITE32(ctrl, mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+				current_bank = ram_id;
+				reg_ready_psb(context,
+					PSB_MSVDX_MTX_RAM_ACCESS_STATUS,
+					0xffffffff, 0x00000001);
+			}
+
+			address +=  4;
+			if (EMGD_READ32(mmio + PSB_MSVDX_MTX_RAM_ACCESS_DATA_TRANSFER) !=
+				fw_data[i]) {
+				printk(KERN_ERR "Verify Error at index %ld\n", i);
+			}
+		} else {
+			printk(KERN_ERR "Timeout waiting for MCMSTAT to be idle while verifying\n");
+		}
+	}
+
+	fw_data = priv_fw->fw_data;
+	fw_size = priv_fw->fw_data_size;
+
+	/* Loop writing data to core memory */
+	current_bank = ~0L;
+	address = priv_fw->fw_data_location - MTX_DATA_BASE;
+
+	for (i = 0; i < fw_size; i++) {
+		if (reg_ready_psb(context, PSB_MSVDX_MTX_RAM_ACCESS_STATUS,
+				0xffffffff, 0x00000001) == 0) {
+			ram_id = MTX_CORE_DATA_MEM + (address / bank_size);
+			if (ram_id != current_bank) {
+				/*
+				 * bits 20:27 - ram bank (CODE_BASE | DATA_BASE)
+				 * bits  2:19 - address
+				 * bit   1    - enable auto increment
+				 *              addressing mode
+				 */
+				ctrl = (ram_id << 20) | (((address >> 2) & 0x000ffffc) << 2) | 0x02;
+				EMGD_WRITE32(ctrl, mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+				current_bank = ram_id;
+				reg_ready_psb(context, PSB_MSVDX_MTX_RAM_ACCESS_STATUS,
+					0xffffffff, 0x00000001);
+			}
+
+			address +=  4;
+			EMGD_WRITE32(fw_data[i],
+				mmio + PSB_MSVDX_MTX_RAM_ACCESS_DATA_TRANSFER);
+		} else {
+			printk(KERN_ERR
+				"[EMGD] MSVDX: Timeout waiting for MCMSTAT "
+				"to be idle - data segment\n");
+		}
+	}
+
+	/* Restore the RAM access control register */
+	EMGD_WRITE32(acc_control, mmio + PSB_MSVDX_MTX_RAM_ACCESS_CONTROL);
+	return 0;
+}
+
+int msvdx_query_plb(igd_context_t *context,
+					unsigned long *status)
+{
+	platform_context_plb_t *platform;
+	EMGD_TRACE_ENTER;
+
+	platform = (platform_context_plb_t *)context->platform_context;
+	*status = 0;
+
+	if (priv_fw) {
+		*status |= VIDEO_STATE_FW_LOADED;
+	}
+
+	if(!platform->rendec_base0 || !platform->rendec_base1) {
+		*status |= VIDEO_STATE_RENDEC_FREED;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int msvdx_pwr_plb(
+	igd_context_t *context,
+	unsigned long power_state)
+{
+	platform_context_plb_t *platform = (platform_context_plb_t *)context->platform_context;
+
+	/* NOTE: The MSVDX need to reset after resume */
+	EMGD_TRACE_ENTER;
+	if(power_state != IGD_POWERSTATE_D0){
+		platform->msvdx_needs_reset = 1;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+int msvdx_pre_init_plb(struct drm_device *dev)
+{
+    drm_emgd_priv_t *priv;
+    igd_context_t *context;
+
+	EMGD_TRACE_ENTER;
+
+    gpDrmDevice = dev;
+	priv = gpDrmDevice->dev_private;
+	context = priv->context;
+
+	context->mod_dispatch.msvdx_pwr = msvdx_pwr_plb;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+int msvdx_init_plb(unsigned long base0, unsigned long base1,
+           void *msvdx_fw, unsigned long msvdx_fw_size, int reset_flag)
+{
+    drm_emgd_priv_t *priv;
+    igd_context_t *context;
+    unsigned char *mmio;
+    unsigned long mmu_base_address;
+    unsigned long base_addr0, base_addr1, size0, size1;
+    unsigned long ctrl;
+	int tmp;
+	unsigned long fw_size;
+	unsigned long reg_val;
+	msvdx_fw_t *fw = NULL;
+	platform_context_plb_t *platform = NULL;
+	int ret =0;
+	int msvdx_status;
+
+    priv = gpDrmDevice->dev_private;
+	context = priv->context;
+	mmio = context->device_context.virt_mmadr;
+    platform = (platform_context_plb_t *)context->platform_context;
+
+    // return back if firmware is already loaded
+    if (init_msvdx_first_time) {
+	spin_lock_init(&platform->msvdx_init_plb);
+    } else if(!reset_flag){
+	return ret;
+    }
+
+    // Set the status for firmware loading
+    spin_lock(&platform->msvdx_init_plb);
+    platform->msvdx_status = platform->msvdx_status | 1;
+    spin_unlock(&platform->msvdx_init_plb);
+
+    // now wait for message processing to finish
+    do
+    {
+        spin_lock(&platform->msvdx_init_plb);
+        msvdx_status = platform->msvdx_status ;
+        spin_unlock(&platform->msvdx_init_plb);
+        OS_SLEEP(100);
+    }
+    while((msvdx_status & 2));
+
+	if (!priv_fw && msvdx_fw) {
+		fw = (msvdx_fw_t *) msvdx_fw;
+		priv_fw = kzalloc(sizeof(msvdx_fw_t), GFP_KERNEL);
+		priv_fw->fw_text_size = fw->fw_text_size;
+		priv_fw->fw_data_size = fw->fw_data_size;
+		priv_fw->fw_version_size = fw->fw_version_size;
+		priv_fw->fw_data_location = fw->fw_data_location;
+
+		fw_size = sizeof(unsigned long) * fw->fw_text_size;
+		priv_fw->fw_text = kmalloc(fw_size, GFP_KERNEL);
+		memcpy(priv_fw->fw_text, (void *) ((unsigned long) msvdx_fw) +
+				((unsigned long) fw->fw_text), fw_size);
+
+		fw_size = sizeof(unsigned long) * fw->fw_data_size;
+		priv_fw->fw_data = kmalloc(fw_size, GFP_KERNEL);
+		memcpy(priv_fw->fw_data, (void *) ((unsigned long) msvdx_fw) +
+			((unsigned long) fw->fw_data), fw_size);
+
+		priv_fw->fw_version = kzalloc(priv_fw->fw_version_size, GFP_KERNEL);
+		strcpy(priv_fw->fw_version, (char *) (((unsigned long) msvdx_fw) +
+			((unsigned long) fw->fw_version)));
+	} else if (!priv_fw) {
+		printk(KERN_INFO "Kernel firmware is not loaded");
+		if(init_msvdx_first_time) {
+			printk(KERN_ERR "ALAN!!! !priv_fw at msvdx init 1st");
+		}
+		ret = 1;
+		goto cleanup;
+	}
+
+	if(!context_count || reset_flag) {
+
+    //init_msvdx_first_time = 1;
+    /* Reset MSVDX engine */
+    EMGD_WRITE32(0x00000100, mmio + PSB_MSVDX_CONTROL);
+    reg_ready_psb(context, PSB_MSVDX_CONTROL, 0x00000100, 0);
+
+    /*
+    * Make sure the clock is on.
+    *
+    * Clock enable bits are 0 - 6, with each bit controlling one of the
+    * clocks.  For this, make sure all the clocks are enabled.
+    */
+    EMGD_WRITE32(PSB_CLK_ENABLE_ALL, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+    /* Set default MMU PTD to the same value used by the SGX */
+    if (!msvdx_compositor_mmu_base) {
+		printk(KERN_ERR "XSERVER never sent compositor MMU base!!!");
+		mmu_base_address = EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE1);
+		tmp = 0;
+		while(!mmu_base_address){
+			mmu_base_address = EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE0);
+			++tmp;
+			if(!tmp%100){
+				printk(KERN_ERR "Cant read SGX Base0 count = %d", tmp);
+			}
+			if(tmp > 10000){
+				printk(KERN_ERR "Giving up reading SGX Base0 from register - expect hang!");
+				break;
+			}
+		}
+		platform->psb_cr_bif_dir_list_base1 = mmu_base_address;
+	} else {
+			platform->psb_cr_bif_dir_list_base1 = msvdx_compositor_mmu_base;
+    }
+
+    EMGD_WRITE32(platform->psb_cr_bif_dir_list_base1,
+	mmio + PSB_MSVDX_MMU_DIR_LIST_BASE0);
+
+    /*
+    * MMU Page size = 12
+    * MMU best count = 7
+    * MMU ADT TTE = 0
+    * MMU TTE threshold = 12
+    */
+    EMGD_WRITE32(0xc070000c, mmio + PSB_MSVDX_MMU_CONTROL1);
+
+
+    /* Flush the directory cache */
+    ctrl = EMGD_READ32(mmio + PSB_MSVDX_MMU_CONTROL0) | 0x0C; /* Flush */
+    EMGD_WRITE32(ctrl, mmio + PSB_MSVDX_MMU_CONTROL0);
+
+#if 1 //disable watchdog
+    reg_val = 0;
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL, FE_WDT_CNT_CTRL, 0x3);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL, FE_WDT_ENABLE, 0);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL, FE_WDT_ACTION0, 1);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL, FE_WDT_CLEAR_SELECT, 1);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL, FE_WDT_CLKDIV_SELECT, 7);
+    printk(KERN_INFO "CTL_MSG: WDT Control value = 0x%x", reg_val);
+    EMGD_WRITE32(0, mmio + MSVDX_CORE_CR_FE_MSVDX_WDT_COMPAREMATCH_OFFSET);
+    EMGD_WRITE32(reg_val, mmio + MSVDX_CORE_CR_FE_MSVDX_WDT_CONTROL_OFFSET);
+
+    reg_val = 0;
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL, BE_WDT_CNT_CTRL, 0x7);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL, BE_WDT_ENABLE, 0);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL, BE_WDT_ACTION0, 1);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL, BE_WDT_CLEAR_SELECT, 0xd);
+    REGIO_WRITE_FIELD(reg_val, MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL, BE_WDT_CLKDIV_SELECT, 7);
+    printk(KERN_INFO "CTL_MSG: WDT Control value = 0x%x", reg_val);
+    EMGD_WRITE32(0, mmio + MSVDX_CORE_CR_BE_MSVDX_WDT_COMPAREMATCH_OFFSET);
+    EMGD_WRITE32(reg_val, mmio + MSVDX_CORE_CR_BE_MSVDX_WDT_CONTROL_OFFSET);
+
+
+#endif
+
+    /* Enable MMU by removing all bypass bits */
+    EMGD_WRITE32(0, mmio + PSB_MSVDX_MMU_CONTROL0);
+    }
+
+    /* Set up the RENDEC.
+    *   The RENDEC requires two blocks of virtual address space so those
+    *   must be allocated and then the RENDEC is initialized using those
+    *   address ranges.
+    *
+    *   RENDEC control0:
+    *     bit 3     1 - search MTX_to_MTX header
+    *     bit 2     1 - skip next slice
+    *     bit 1     1 - flush remaining bit stream
+    *     bit 0     1 - initialize RENDEC
+    *
+    *   RENDEC control1:
+    *     bit 24:   1 - enables data to be transferred through ext. memory
+    *     bit 19:18 WR burst size (0 = 32 bytes, 1 = 64 bytes, 2 = 128 bytes)
+    *     bit 17:16 RD burst size (0 = 32 bytes, 1 = 64 bytes, 2 = 128 bytes)
+    *     bit  7: 0 start size (zero)
+    */
+
+    size0 = RENDEC_A_SIZE;
+    size1 = RENDEC_B_SIZE;
+
+    /*
+    * These allocations need to be undone when shutting down.  Where
+    * should they be saved?
+    */
+    if (init_msvdx_first_time) {
+		base_addr0 = base0;
+		base_addr1 = base1;
+
+		//printk(KERN_INFO "get the base_addr=%lx, base_addr1=%lx\n", base_addr0,base_addr1);
+
+        /* Save the offsets so it can be freed and restored later */
+        platform = (platform_context_plb_t *)context->platform_context;
+        platform->rendec_base0 = base_addr0;
+        platform->rendec_base1 = base_addr1;
+
+		init_msvdx_first_time = 0;
+        INIT_LIST_HEAD(&platform->msvdx_queue);
+		spin_lock_init(&platform->msvdx_lock);
+
+    } else {
+        /* restore offsets. */
+        platform = (platform_context_plb_t *)context->platform_context;
+        base_addr0 = platform->rendec_base0;
+        base_addr1 = platform->rendec_base1;
+
+        /* Init link list */
+        if(!context_count) {
+		INIT_LIST_HEAD(&platform->msvdx_queue);
+	} else {
+		if(!reset_flag){
+			EMGD_TRACE_EXIT;
+			ret = 0;
+			goto cleanup;
+		}
+	}
+    }
+
+
+	platform->msvdx_busy = 0;
+    EMGD_WRITE32(base_addr0, mmio + PSB_MSVDX_RENDEC_BASE_ADDR0);
+    EMGD_WRITE32(base_addr1, mmio + PSB_MSVDX_RENDEC_BASE_ADDR1);
+
+    EMGD_WRITE32((((size1 / 4096) << 16) | (size0 / 4096)),
+            mmio + PSB_MSVDX_RENDEC_BUFFER_SIZE);
+
+    /* Rendec setup:
+    *   Start size = 0
+    *   Burst size R = 4 words
+    *   Burst size W = 4 words
+    *   External memory enabled
+    *   Stream End = 0
+    *   Slice mode = 0
+    *   DEC disable = 0
+    */
+    EMGD_WRITE32(0x01050000, mmio + PSB_MSVDX_RENDEC_CONTROL1);
+
+    EMGD_WRITE32(0x00101010, mmio + PSB_MSVDX_RENDEC_CONTEXT0);
+    EMGD_WRITE32(0x00101010, mmio + PSB_MSVDX_RENDEC_CONTEXT1);
+    EMGD_WRITE32(0x00101010, mmio + PSB_MSVDX_RENDEC_CONTEXT2);
+    EMGD_WRITE32(0x00101010, mmio + PSB_MSVDX_RENDEC_CONTEXT3);
+    EMGD_WRITE32(0x00101010, mmio + PSB_MSVDX_RENDEC_CONTEXT4);
+    EMGD_WRITE32(0x00101010, mmio + PSB_MSVDX_RENDEC_CONTEXT5);
+
+    EMGD_WRITE32(0x00000001, mmio + PSB_MSVDX_RENDEC_CONTROL0);
+
+
+    /* Start Firmware Load process */
+
+    /* Reset the MTX */
+    EMGD_WRITE32(0x00000001, mmio + PSB_MSVDX_MTX_SOFT_RESET);
+
+	/* Reset the counter that looks for MSVDX getting into a bad state */
+	jiffies_at_last_dequeue = 0;
+
+    /*
+    * Should this check the core revision and only do this if it is
+    * a specific version or range of versions?
+    *
+    * Stepping prior to D0, need to set COMMS_OFFSET_FLAGS to 0
+    * Stepping D0 should set MSVDX_DEVICE_NODE_FLAGS_DEFAULT_D0 (0x222)
+    * Stepping D1 should set MSVDX_DEVICE_NODE_FLAGS_DEFAULT_D1 (0x220)
+    */
+#if 0
+    /* If POULSBO_D1 or later use MSVDX_DEVICE_NODE_FLAGS_MMU_HW_INVALIDATION */
+    EMGD_WRITE32(MSVDX_DEVICE_NODE_FLAGS_DEFAULT,
+            mmio + PSB_MSVDX_COMMS_OFFSET_FLAGS);
+    /* Else EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_OFFSET_FLAGS); */
+#endif
+#if 1
+{
+#define DISABLE_FW_WDT                          0x0008
+#define ABORT_ON_ERRORS_IMMEDIATE               0x0010
+#define ABORT_FAULTED_SLICE_IMMEDIATE           0x0020
+#define RETURN_VDEB_DATA_IN_COMPLETION          0x0800
+#define DISABLE_Auto_CLOCK_GATING               0x1000
+#define DISABLE_IDLE_GPIO_SIG                   0x2000
+
+    unsigned long msvdx_fw_flag;
+
+    // msvdx_fw_flag = DISABLE_Auto_CLOCK_GATING | RETURN_VDEB_DATA_IN_COMPLETION | DISABLE_FW_WDT;
+    msvdx_fw_flag = DISABLE_FW_WDT; /* Per ImgTec ticket 16892 */
+
+    EMGD_WRITE32(msvdx_fw_flag, mmio + PSB_MSVDX_COMMS_OFFSET_FLAGS);
+
+    /*
+     * Following two setting can't find reg definition in spec, just copy
+     * from IMG DDK 187
+     */
+    /* 1/200th of the clock frequency */
+    EMGD_WRITE32(200 - 1,
+      mmio + PSB_MSVDX_MTX_CORE_CR_MTX_SYSC_TIMERDIV_OFFSET);
+    EMGD_WRITE32(0, mmio + 0x2884); /* EXT_FW_ERROR_STATE */
+}
+#endif
+    /* Initialize communication control */
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_MSG_COUNTER);
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_SIGNATURE);
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_TO_HOST_RD_INDEX);
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_TO_HOST_WRT_INDEX);
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_TO_MTX_RD_INDEX);
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_TO_MTX_WRT_INDEX);
+    EMGD_WRITE32(0x00, mmio + PSB_MSVDX_COMMS_FW_STATUS);
+
+    printk(KERN_INFO "MSVDX: Firmware version is %s\n", priv_fw->fw_version);
+    if (msvdx_fw_dma_upload)
+        msvdx_upload_fw_dma(0 /* Offset of firmware's .text section */);
+    else
+        msvdx_upload_fw();
+
+    /* Start the firmware thread running */
+    EMGD_WRITE32(PC_START_ADDRESS, mmio + PSB_MSVDX_MTX_REGISTER_READ_WRITE_DATA);
+    EMGD_WRITE32(MTX_PC, mmio + PSB_MSVDX_MTX_REGISTER_READ_WRITE_REQUEST);
+    reg_ready_psb(context, PSB_MSVDX_MTX_REGISTER_READ_WRITE_REQUEST,
+            0x80000000, 0x80000000);
+
+    /* Enable the MTX */
+    printk(KERN_INFO "Enabling MTX 0x%x\n", EMGD_READ32(mmio + PSB_MSVDX_MTX_ENABLE));
+    EMGD_WRITE32(MSVDX_MTX_ENABLE_MTX_ENABLE_MASK, mmio + PSB_MSVDX_MTX_ENABLE);
+    printk(KERN_INFO "Enabled MTX 0x%x\n", EMGD_READ32(mmio + PSB_MSVDX_MTX_ENABLE));
+
+    /*
+    * Wait for signature value to be written.
+    *
+    * This is how the firmware thread notifies us that it is running.
+    */
+    if (reg_ready_psb(context, PSB_MSVDX_COMMS_SIGNATURE, 0xffffffff,
+                0xA5A5A5A5)){
+        /* Error initializing firmware.... */
+        EMGD_DEBUG("Error, no MSVDX COMMS Signature");
+	    ret = -1; /* FIXME: return an error code */
+        goto cleanup;
+    }
+    printk(KERN_INFO "MSVDX COMMS Signature OK\n");
+
+    /* Locate message buffers */
+    platform->mtx_buf_size = EMGD_READ32(mmio+PSB_MSVDX_COMMS_TO_MTX_BUF_SIZE) & 0xFFFF;
+    platform->host_buf_size = EMGD_READ32(mmio+PSB_MSVDX_COMMS_TO_HOST_BUF_SIZE) & 0xFFFF;
+    platform->mtx_buf_offset = MSVDX_BASE + (EMGD_READ32(mmio+PSB_MSVDX_COMMS_TO_MTX_BUF_SIZE) >> 16) + 0x2000;
+    platform->host_buf_offset = MSVDX_BASE + (EMGD_READ32(mmio+PSB_MSVDX_COMMS_TO_HOST_BUF_SIZE) >> 16) + 0x2000;
+
+    platform->sequence = 1;
+    platform->mtx_submitted = 0;
+
+    /* Send initialization message to firmware, newer versions don't */
+    if (0) {
+        unsigned long init_msg[2];
+
+        init_msg[0] = 8 | (0x80 << 8);
+
+        /* physical address of the PD shared by SGX/MSVDX */
+        init_msg[1] = EMGD_READ32(mmio + 0x40c84);
+
+        send_to_mtx(context, init_msg);
+
+        /* Check response from MTX firmware */
+        poll_mtx_irq(context);
+    }
+
+    /* Clear the firmware buffer, this is mostly to make debugging easier */
+    if (1) {
+        unsigned long i;
+
+        for (i = 0; i < platform->mtx_buf_size; i++) {
+            EMGD_WRITE32(0, mmio + platform->mtx_buf_offset + (i <<2));
+        }
+        for (i = 0; i < platform->host_buf_size; i++) {
+            EMGD_WRITE32(0, mmio + platform->host_buf_offset + (i <<2));
+        }
+    }
+
+
+    /* Enable minimal clocks */
+    EMGD_WRITE32(PSB_CLK_ENABLE_MIN, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+    /* Enable MTX interrupts to host */
+    EMGD_WRITE32(1<<14, mmio + PSB_MSVDX_HOST_INTERRUPT_ENABLE);
+
+cleanup:
+    // unset fw loading flag
+    spin_lock(&platform->msvdx_init_plb);
+    platform->msvdx_status = platform->msvdx_status & ~1;
+    spin_unlock(&platform->msvdx_init_plb);
+    /* Are we done? */
+    EMGD_TRACE_EXIT;
+    return ret; /* Successfully initialized the MTX firmware */
+}
+
+
+int msvdx_uninit_plb(igd_context_t *context)
+{
+	EMGD_TRACE_ENTER;
+
+	if(!context_count) {
+		//msvdx_reset_plb(context);
+		//msvdx_pvr_deinit();
+ 	}
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+int msvdx_close_context(igd_context_t *context)
+{
+	EMGD_TRACE_ENTER;
+
+	if(context_count) {
+		context_count -= 1;
+	} else {
+		EMGD_TRACE_EXIT;
+		return 1;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int msvdx_create_context(igd_context_t *context)
+{
+	EMGD_TRACE_ENTER;
+
+	context_count +=1;
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+int process_video_decode_plb(igd_context_t *context, unsigned long offset, void *virt_addr, unsigned long *fence_id)
+{
+	unsigned long *mtx_buf;
+    unsigned long *mtx_msgs;
+    unsigned long mtx_offset;
+    unsigned long mtx_msg_cnt;
+    unsigned long irq_flags;
+	int ret = 0;
+    platform_context_plb_t *platform;
+    EMGD_TRACE_ENTER;
+
+
+    platform = (platform_context_plb_t *)context->platform_context;
+
+	mtx_buf = (unsigned long *) virt_addr;
+    mtx_offset = mtx_buf[0];
+    mtx_msg_cnt = mtx_buf[1];
+
+    if (mtx_msg_cnt > 0x20) {
+        printk(KERN_ERR "Message count too big at %ld\n", mtx_msg_cnt);
+        return -EINVAL;
+    }
+
+    mtx_msgs = mtx_buf + (mtx_offset / sizeof (unsigned long));
+	if (mtx_msg_cnt > 0) {
+	//if ((mtx_buf[0] != 0x8) || (mtx_buf[2] != 0x8504)) {
+
+		spin_lock_irqsave(&platform->msvdx_lock, irq_flags);
+
+		if (!platform->msvdx_busy) {
+
+			platform->msvdx_busy = 1;
+			spin_unlock_irqrestore(&platform->msvdx_lock, irq_flags);
+
+
+			if (platform->msvdx_needs_reset) {
+				msvdx_reset_plb(context);
+				msvdx_init_plb(0, 0, NULL, 0, 1);
+				jiffies_at_last_dequeue = 0;
+			}
+			// Send message buffer to MSVDX Firmware
+
+			populate_fence_id(context, mtx_msgs, mtx_msg_cnt);
+			ret = process_mtx_messages(context, mtx_msgs, mtx_msg_cnt, platform->msvdx_fence);
+
+			if (ret) {
+				ret = -EINVAL;
+
+			}
+		} else {
+			struct msvdx_cmd_queue *msvdx_cmd;
+
+			spin_unlock_irqrestore(&platform->msvdx_lock, irq_flags);
+
+			msvdx_cmd = kzalloc(sizeof(struct msvdx_cmd_queue), GFP_KERNEL);
+			if (msvdx_cmd == NULL) {
+				printk(KERN_ERR "MSVDXQUE: Out of memory\n");
+				return -ENOMEM;
+			}
+
+			populate_fence_id(context, mtx_msgs, mtx_msg_cnt);
+			msvdx_cmd->cmd = mtx_msgs;
+			msvdx_cmd->cmd_size = mtx_msg_cnt;
+			/* If more than 1000 msec (1 second or 1000 jiffies) passes since
+			 * the last time a video cmd has been decoded, MSVDX may be hung
+			 * and needing to be reset.
+			 */
+			if ((jiffies_at_last_dequeue != 0) &&
+				((jiffies - jiffies_at_last_dequeue) > 1000)) {
+				printk(KERN_ERR "Video decode hardware appears to be hung; "
+					"resetting\n");
+				platform->msvdx_needs_reset = 1;
+			}
+			if (platform->msvdx_needs_reset) {
+				msvdx_reset_plb(context);
+				msvdx_init_plb(0, 0, NULL, 0, 1);
+				platform->msvdx_busy = 0;
+				jiffies_at_last_dequeue = 0;
+			}
+
+			spin_lock_irqsave(&platform->msvdx_lock, irq_flags);
+			list_add_tail(&msvdx_cmd->head, &platform->msvdx_queue);
+			if (!platform->msvdx_busy) {
+				platform->msvdx_busy = 1;
+				msvdx_dequeue_send(context);
+			}
+
+			spin_unlock_irqrestore(&platform->msvdx_lock, irq_flags);
+
+		}
+		*fence_id = platform->msvdx_fence;
+	} else {
+		/* return the fence id even there is no messages to process.
+		 * Used this for context id.
+		 */
+		*fence_id = platform->msvdx_fence;
+	}
+
+	return ret;
+}
+
+int msvdx_get_fence_id(igd_context_t *context, unsigned long *fence_id)
+{
+	int ret = 0;
+    platform_context_plb_t *platform;
+
+    platform = (platform_context_plb_t *)context->platform_context;
+
+	*fence_id = platform->mtx_completed;
+
+	return ret;
+}
+
+int msvdx_flush_tlb(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long msvdx_mmu;
+	msvdx_mmu = EMGD_READ32(mmio + PSB_MSVDX_MMU_CONTROL0);
+	msvdx_mmu &= 0xFFFFFFF0;
+	msvdx_mmu |= 0x0C; 	/* MMU_INVALDC + MMU_FLUSH */
+	EMGD_WRITE32(msvdx_mmu, mmio + PSB_MSVDX_MMU_CONTROL0);
+
+	msvdx_mmu = EMGD_READ32(mmio + PSB_MSVDX_MMU_CONTROL0);
+	msvdx_mmu &= 0xFFFFFF00;
+	EMGD_WRITE32(msvdx_mmu, mmio + PSB_MSVDX_MMU_CONTROL0);
+	EMGD_READ32(mmio + PSB_MSVDX_MMU_CONTROL0);
+
+	return 0;
+
+}
+
+
+/*
+ * Resets the MSVDX engine via the soft reset control.
+ *
+ * This function is exported.
+ */
+void msvdx_reset_plb(igd_context_t *context)
+{
+    unsigned char *mmio = context->device_context.virt_mmadr;
+    platform_context_plb_t *platform;
+    EMGD_TRACE_ENTER;
+
+    platform = (platform_context_plb_t *)context->platform_context;
+
+    /* Reset MSVDX engine */
+    EMGD_WRITE32(0x11111100, mmio + PSB_MSVDX_CONTROL);
+    reg_ready_psb(context, PSB_MSVDX_CONTROL, 0x00000100, 0);
+
+    /* Clear interrupt and clear pending interrupts */
+    EMGD_WRITE32(0, mmio + PSB_MSVDX_HOST_INTERRUPT_ENABLE);
+    EMGD_WRITE32(0xffffffff, mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+
+    /* Mark the engine as being reset */
+    platform->msvdx_needs_reset = 0;
+}
+
+#if 0
+void MSVDXSetClocksEnable(int ClockState)
+{
+	unsigned long reg_val = 0;
+
+	if(ClockState == 0)
+	{
+		// Turn off clocks procedure
+
+		if(LastClockState)
+		{
+			// Turn off all the clocks except core
+			EMGD_WRITE32(0x00000001, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+			// Make sure all the clocks are off except core
+			reg_ready_psb(context, PSB_MSVDX_MAN_CLK_ENABLE, 0x00000001, 0);
+
+			// Turn off core clock
+			EMGD_WRITE32(0, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+		}
+
+		LastClockState = 0;
+	}
+	else
+	{
+		// ui32ClockState
+		unsigned long ClocksEn = ClockState;
+
+		//Make sure that core clock is not accidentally turned off
+		ClocksEn |= 0x00000001;
+
+		//If all clocks were disable do the bring up procedure
+		if(LastClockState == 0 )
+		{
+			// turn on core clock
+			EMGD_WRITE32(0x00000001, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+			// Make sure it is on
+			reg_ready_psb(context, PSB_MSVDX_MAN_CLK_ENABLE, 0x00000001, 0);
+
+			// turn on the other clocks as well
+			EMGD_WRITE32(ClocksEn, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+			// Make sure that they are on
+			reg_ready_psb(context, PSB_MSVDX_MAN_CLK_ENABLE, ClocksEn, 0);
+		}
+		else
+		{
+			EMGD_WRITE32(ClocksEn, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+			// Make sure that they are on
+			reg_ready_psb(context, PSB_MSVDX_MAN_CLK_ENABLE, ClocksEn, 0);
+		}
+
+		LastClockState = ClocksEn;
+	}
+}
+
+void msvdx_reset_plb_workaround(igd_context_t *context)
+{
+    unsigned char *mmio = context->device_context.virt_mmadr;
+    platform_context_plb_t *platform;
+
+    EMGD_TRACE_ENTER;
+
+    platform = (platform_context_plb_t *)context->platform_context;
+
+    /*
+    * Make sure the clock is on.
+    *
+    * Clock enable bits are 0 - 6, with each bit controlling one of the
+    * clocks.  For this, make sure all the clocks are enabled.
+    */
+
+    EMGD_WRITE32(PSB_CLK_ENABLE_ALL, mmio + PSB_MSVDX_MAN_CLK_ENABLE);
+
+    /* Reset MSVDX engine */
+    EMGD_WRITE32(0x11111100, mmio + PSB_MSVDX_CONTROL);
+    reg_ready_psb(context, PSB_MSVDX_CONTROL, 0x00000100, 0);
+
+    /* Clear interrupt and clear pending interrupts */
+    EMGD_WRITE32(0, mmio + PSB_MSVDX_HOST_INTERRUPT_ENABLE);
+    EMGD_WRITE32(0xffffffff, mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+
+    /* Mark the engine as being reset */
+    platform->msvdx_needs_reset = 0;
+}
+#endif
+/*
+ * When shuting down, need to reset the MSVDX engine too.
+ */
+int msvdx_shutdown_plb(igd_context_t *context)
+{
+	platform_context_plb_t *platform;
+	EMGD_TRACE_ENTER;
+	platform = (platform_context_plb_t *)context->platform_context;
+
+	/* Reset MSVDX engine */
+	msvdx_reset_plb(context);
+	msvdx_pvr_deinit();
+	/* Free RENDEC memory allocations */
+	platform->rendec_base0 = 0;
+	platform->rendec_base1 = 0;
+	init_msvdx_first_time = 1;
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+static int reg_ready_psb(igd_context_t *context,
+        unsigned long reg,
+        unsigned long mask,
+        unsigned long value)
+{
+    unsigned char *mmio = context->device_context.virt_mmadr;
+    unsigned long status;
+    int poll_cnt = 1000;
+
+    while (poll_cnt) {
+        status = EMGD_READ32(mmio + reg);
+        if ((status & mask) == value) {
+            return 0;
+        }
+        poll_cnt--;
+        OS_SLEEP(100);
+    }
+
+    /* Timeout waiting for RAM ACCESS ready */
+    EMGD_DEBUG("TIMEOUT: Got 0x%08lx while waiting for 0x%08lx", status, value);
+    return 1;
+}
+
+
+static int poll_mtx_irq(igd_context_t *context)
+{
+    unsigned char *mmio = context->device_context.virt_mmadr;
+    int ret;
+    unsigned long mtx_int;
+
+    EMGD_TRACE_ENTER;
+    mtx_int = (1 << 14);
+
+    ret = reg_ready_psb(context, PSB_MSVDX_INTERRUPT_STATUS, mtx_int, mtx_int);
+    if (ret) {
+        /* Timeout waiting on interrupt status */
+        return ret;
+    }
+
+    /* Clear the interrupt */
+    EMGD_WRITE32(mtx_int, mmio + PSB_MSVDX_INTERRUPT_CLEAR);
+
+    return ret;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.c b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.c
new file mode 100644
index 0000000..1e36594
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.c
@@ -0,0 +1,583 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: msvdx_pvr.c
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include "msvdx.h"
+#include "msvdx_pvr.h"
+#include "sgxinfokm.h"
+#include "services_headers.h"
+//#include "sgxinfokm.h"
+#include "sgxapi_km.h"
+#include "services.h"
+#include "perproc.h"
+#include "pvr_bridge_km.h"
+#include "syscommon.h"
+#include "memory.h"
+#include "memmap.h"
+#include <drm_emgd_private.h>
+#include "emgd_drm.h"
+#include <linux/kthread.h>
+#include <plb/regs.h>
+#include <plb/context.h>
+
+extern IMG_BOOL msvdx_mtx_isr(IMG_VOID *pvData);
+#if 0
+extern int emgd_test_pvrsrv(struct drm_device *dev, void *arg,
+	struct drm_file *file_priv);
+
+static PVRSRV_PER_PROCESS_DATA *psPerProc = NULL;
+static unsigned long dev_ids[2] = {0, 0};
+static void *dev_mem_context = NULL;
+static IMG_BOOL mem_created, dummy;
+static PVRSRV_DEVICE_IDENTIFIER dev_id_list[10];
+static PVRSRV_HEAP_INFO heap_info[PVRSRV_MAX_CLIENT_HEAPS];
+
+int alloc_ramdec_region(unsigned long *base_addr0, unsigned long *base_addr1,
+				unsigned long size0, unsigned long size1)
+{
+	unsigned long pid = OSGetCurrentProcessIDKM();
+    unsigned long num = 10;
+	unsigned long heap_count = 0;
+    void *sgx_cookie = NULL;
+    unsigned long heapIndex = 0, generalHeapIndex = 0;
+	PVRSRV_ERROR err;
+	PVRSRV_KERNEL_MEM_INFO *rendec0MemInfo;
+	PVRSRV_KERNEL_MEM_INFO *rendec1MemInfo;
+	int i;
+
+	printk(KERN_INFO "Calling PVRSRVPerProcessData()\n");
+	if (PVRSRVPerProcessDataConnect(200) != PVRSRV_OK) {
+		printk(KERN_ERR "msvdx_init: connect to PVR failed\n");
+	}
+
+	psPerProc = PVRSRVPerProcessData(200);
+	if (psPerProc == IMG_NULL)
+	{
+		printk(KERN_ERR "msvdx_init:  Couldn't find per process data for pid=%lx\n", pid);
+	}
+
+	printk(KERN_INFO "  TEST: pp_data = 0x%p\n", psPerProc);
+
+
+	PVRSRVEnumerateDevicesKM(&num, dev_id_list);
+	printk(KERN_INFO "Calling PVRSRVEnumerateDevicesKM()\n");
+	if (PVRSRVEnumerateDevicesKM(&num, dev_id_list) != PVRSRV_OK) {
+		printk(KERN_ERR "msvdx_init: PVRSRVEnumerateDevice failed\n");
+	} else {
+		printk(KERN_INFO "  PVRSRVEnumerateDevicesKM() found %ld devices\n", num);
+		for (i = 0 ; i < num ; i++) {
+			PVRSRV_DEVICE_IDENTIFIER *id = dev_id_list + i;
+			unsigned long cookie = 0;
+			printk(KERN_INFO "    Device %d has type %d, class %d & index %ld\n", i,
+					id->eDeviceType, id->eDeviceClass, id->ui32DeviceIndex);
+			if (PVRSRV_DEVICE_TYPE_EXT != id->eDeviceType) {
+				// Call PVRSRVAcquireDeviceDataKM():
+				printk(KERN_INFO "Calling PVRSRVAcquireDeviceDataKM()\n");
+				err = PVRSRVAcquireDeviceDataKM(id->ui32DeviceIndex,
+						PVRSRV_DEVICE_TYPE_UNKNOWN, (void *) &cookie);
+				if (err != PVRSRV_OK) {
+					printk(KERN_ERR "[EMGD] PVRSRVAcquireDeviceDataKM() "
+							"returned %d\n", err);
+					break;
+				}
+				if (PVRSRV_DEVICE_TYPE_SGX == id->eDeviceType) {
+					printk(KERN_INFO "  Found cookie = 0x%lx\n", cookie);
+					// Save this away for later:
+					sgx_cookie = (void *) cookie;
+				}
+			}
+		}
+	}
+
+	// Enumerate the display class devices to be able to find the 3DD:
+	printk(KERN_INFO "Calling PVRSRVEnumerateDCKM()\n");
+	err = PVRSRVEnumerateDCKM(PVRSRV_DEVICE_CLASS_DISPLAY,
+			&num, dev_ids);
+	if (err != PVRSRV_OK) {
+		printk(KERN_ERR "[EMGD] PVRSRVEnumerateDCKM() returned %d\n", err);
+	} else {
+		// Find the 3DD:
+		printk(KERN_INFO "  PVRSRVEnumerateDCKM() found %ld devices\n", num);
+		for (i = 0 ; i < num ; i++) {
+			printk(KERN_INFO "    device %d has ID %ld\n", i, dev_ids[i]);
+		}
+		if (0 == dev_ids[0]) {
+			printk(KERN_ERR "[EMGD] Did not find 3rd-party display driver ID\n");
+		}
+	}
+
+	// Call PVRSRVCreateDeviceMemContextKM():
+
+	printk(KERN_INFO "Calling PVRSRVCreateDeviceMemContextKM()\n");
+	err = PVRSRVCreateDeviceMemContextKM(sgx_cookie, psPerProc,
+			&dev_mem_context, &heap_count, heap_info, &mem_created, &dummy);
+	if (err != PVRSRV_OK) {
+		printk(KERN_ERR "[EMGD] PVRSRVCreateDeviceMemContextKM() "
+				"returned %d\n", err);
+	}
+
+
+	for (heapIndex=0; heapIndex<heap_count; heapIndex++) {
+		if (HEAP_IDX(heap_info[heapIndex].ui32HeapID) == SGX_GENERAL_HEAP_ID)
+		{
+			generalHeapIndex = heapIndex;
+			break;
+		}
+	}
+
+	if (PVRSRVAllocDeviceMemKM(sgx_cookie, psPerProc, heap_info[generalHeapIndex].hDevMemHeap,
+                        PVRSRV_MEM_READ | PVRSRV_MEM_WRITE,
+                        size0, 0, &rendec0MemInfo, "") != PVRSRV_OK) {
+		printk(KERN_ERR "msvdx: PVRSRVAllocDeviceMemKM failed\n");
+	}
+
+	if (PVRSRVAllocDeviceMemKM(sgx_cookie, psPerProc, heap_info[generalHeapIndex].hDevMemHeap,
+                        PVRSRV_MEM_READ | PVRSRV_MEM_WRITE,
+                        size1, 0, &rendec1MemInfo, "") != PVRSRV_OK) {
+		printk(KERN_ERR "msvdx: PVRSRVAllocDeviceMemKM failed\n");
+	}
+
+	*base_addr0 = rendec0MemInfo->sDevVAddr.uiAddr;
+	*base_addr1 = rendec1MemInfo->sDevVAddr.uiAddr;
+
+	//printk(KERN_INFO "pvr size0=%lx, size1=%lx, heap=%ld\n", size0, size1, generalHeapIndex);
+
+//	return PVRSRV_OK;
+	return 0;
+
+}
+#endif
+
+/*
+ * Dummy kernel thread.  Needed for a persistent per process data for
+ * PVR services.
+ */
+static int msvdx_pvr_kthread(void *data)
+{
+	while (!kthread_should_stop()) {
+		set_current_state(TASK_INTERRUPTIBLE);
+		schedule();
+	}
+	return 0;
+}
+
+int msvdx_preinit_mmu(unsigned long hmemcxt)
+{
+	PVRSRV_ERROR err;
+	PVRSRV_PER_PROCESS_DATA *ps_data = NULL;
+	IMG_HANDLE hmem = (IMG_HANDLE) hmemcxt;
+	IMG_HANDLE hmemkm;
+	IMG_DEV_PHYADDR addr;
+	IMG_UINT32 pid = OSGetCurrentProcessIDKM();
+	int ret;
+	drm_emgd_priv_t *priv = gpDrmDevice->dev_private;
+	igd_context_t *context = priv->context;
+	platform_context_plb_t *platform =
+		(platform_context_plb_t *) context->platform_context;
+
+    ps_data = PVRSRVPerProcessData(pid);
+	if (!ps_data) {
+		printk(KERN_ERR "MSVDX: Cannot get process data information");
+
+		return -1;
+	}
+
+
+	err = PVRSRVLookupHandle(ps_data->psHandleBase, &hmemkm, hmem, PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+
+	if(err != PVRSRV_OK)
+	{
+		printk(KERN_ERR "MSVDX: Cannot get memory context from process data");
+
+		return -1;
+	}
+
+	addr = BM_GetDeviceNode(hmemkm)->pfnMMUGetPDDevPAddr(BM_GetMMUContextFromMemContext(hmemkm));
+	msvdx_init_compositor_mmu(addr.uiAddr);
+
+	if (!platform->msvdx_pvr) {
+		ret = msvdx_pvr_init();
+		if (ret) {
+			printk(KERN_INFO "Failed in msvdx_pvr_init()");
+		} else {
+
+		   	printk(KERN_INFO "Succeed for msvdx_pvr_init()");
+
+		}
+	}
+
+
+	return 0;
+}
+
+/*
+ * Can't call from module initialization, since PVR services are started when
+ * the Xorg server starts.
+ */
+int msvdx_pvr_init(void)
+{
+	drm_emgd_priv_t        *priv;
+	igd_context_t           *context;
+	platform_context_plb_t  *platform;
+	struct msvdx_pvr_info   *pvr;
+	PVRSRV_DEVICE_IDENTIFIER dev_id_list[PVRSRV_MAX_DEVICES];
+	IMG_UINT32   pid;
+	IMG_UINT32   num_devices;
+	IMG_UINT32   i;
+	IMG_BOOL     mem_created;
+	PVRSRV_ERROR err;
+	int ret;
+
+	priv     = gpDrmDevice->dev_private;
+	context  = priv->context;
+	platform = (platform_context_plb_t *)context->platform_context;
+
+	if (platform->msvdx_pvr) {
+		printk(KERN_INFO "[EMGD] MSVDX: PVR services already "
+			"initialized\n");
+		return 0;
+	}
+
+    pvr = kzalloc(sizeof(*pvr), GFP_KERNEL);
+	if (!pvr)
+		return -ENOMEM;
+
+    /*
+	 * Create a dummy kernel thread so that a persistent PVR per process
+	 * data could be created.
+	 */
+	pvr->kthread = kthread_run(msvdx_pvr_kthread, NULL, "msvdx-pvr");
+	if (IS_ERR(pvr->kthread)) {
+		ret = PTR_ERR(pvr->kthread);
+		printk(KERN_ERR "[EMGD] MSVDX: failed to create MSVDX PVR "
+			"kernel tread, error=%i\n", ret);
+		pvr->kthread = NULL;
+		goto out_free;
+	}
+
+	ret = -ENODEV;
+	pid = OSGetCurrentProcessIDKM(); //(IMG_UINT32)pvr->kthread->pid;
+    err = PVRSRVPerProcessDataConnect(pid);
+	if (err != PVRSRV_OK) {
+		printk(KERN_ERR "[EMGD] MSVDX: connect to PVR failed (pid=%u), "
+			"error=%i\n", (unsigned int)err, err);
+		goto out_stop_kthread;
+	}
+	pvr->per_proc = PVRSRVPerProcessData(pid);
+	if (pvr->per_proc == IMG_NULL) {
+		printk(KERN_ERR "[EMGD] MSVDX: Couldn't find per process "
+			"data for pid=%u\n", (unsigned int)pid);
+		goto out_stop_kthread;
+	}
+
+	err = PVRSRVEnumerateDevicesKM(&num_devices, dev_id_list);
+	if (err != PVRSRV_OK) {
+		printk(KERN_ERR "[EMGD] MSVDX: PVRSRVEnumerateDevice() failed, "
+			"error=%u\n", (unsigned int)err);
+		goto out_stop_kthread;
+	}
+
+	for (i = 0 ; i < num_devices ; i++) {
+		PVRSRV_DEVICE_IDENTIFIER *id;
+		IMG_HANDLE cookie;
+
+		cookie = IMG_NULL;
+		id = &dev_id_list[i];
+
+		if (id->eDeviceType != PVRSRV_DEVICE_TYPE_EXT) {
+			err = PVRSRVAcquireDeviceDataKM(id->ui32DeviceIndex,
+				PVRSRV_DEVICE_TYPE_UNKNOWN, &cookie);
+			if (err != PVRSRV_OK) {
+				printk(KERN_ERR "[EMGD] MSVDX: "
+					"PVRSRVAcquireDeviceDataKM() failed, "
+					"error=%u\n", err);
+				break;
+			}
+			if (PVRSRV_DEVICE_TYPE_SGX == id->eDeviceType) {
+				pvr->sgx_cookie = cookie;
+				break;
+			}
+		}
+	}
+
+	if (pvr->sgx_cookie == IMG_NULL)
+		goto out_stop_kthread;
+
+	err = PVRSRVCreateDeviceMemContextKM(pvr->sgx_cookie, pvr->per_proc,
+			&pvr->dev_mem_context, &pvr->heap_count,
+			pvr->heap_info, &mem_created, pvr->heap_shared);
+	if (err != PVRSRV_OK) {
+		printk(KERN_ERR "[EMGD] MSVDX: PVRSRVCreateDeviceMemContextKM()"
+			" failed, error=%u\n", (unsigned int)err);
+		goto out_stop_kthread;
+	}
+
+	for (i = 0; i < pvr->heap_count; i++) {
+		if (HEAP_IDX(pvr->heap_info[i].ui32HeapID) ==
+			SGX_GENERAL_MAPPING_HEAP_ID) {
+			pvr->mapping_heap_index = i;
+			break;
+		}
+	}
+
+	platform->msvdx_pvr = pvr;
+	pvr->pid = pid;
+
+
+	return 0;
+
+out_stop_kthread:
+	kthread_stop(pvr->kthread);
+out_free:
+	kfree(pvr);
+	return ret;
+}
+
+/*
+ * HHP: FIXME:
+ * This should also be called when GMM/GTT is re-initialized, i.e. when
+ * the Xorg server stops.  The PVR backed memory is invalid when this happens,
+ * so if we call this function from msvdx_driver_unload() when the module is
+ * removed, the "fw_mem_info" and "dev_mem_context" are invalid.
+ */
+void msvdx_pvr_deinit(void)
+{
+	drm_emgd_priv_t       *priv;
+	igd_context_t          *context;
+	platform_context_plb_t *platform;
+	struct msvdx_pvr_info  *pvr;
+	IMG_BOOL mem_destroyed;
+	int pid=0;
+	unsigned char *mmio;
+
+	priv     = gpDrmDevice->dev_private;
+	context  = priv->context;
+	platform = (platform_context_plb_t *)context->platform_context;
+    mmio = context->device_context.virt_mmadr;
+	pvr = platform->msvdx_pvr;
+
+    //Reset MTX before unloading firmware
+    EMGD_WRITE32(0x00000001, mmio + PSB_MSVDX_MTX_SOFT_RESET);
+
+
+	if (pvr) {
+        kthread_stop(pvr->kthread);
+		if (pvr->fw_mem_info)
+			PVRSRVFreeDeviceMemKM(pvr->sgx_cookie,
+						pvr->fw_mem_info);
+
+		PVRSRVDestroyDeviceMemContextKM(pvr->sgx_cookie,
+						pvr->dev_mem_context,
+						&mem_destroyed);
+
+	//	PVRSRVPerProcessDataDisconnect((IMG_UINT32)pvr->kthread->pid);
+        PVRSRVPerProcessDataDisconnect((IMG_UINT32)pvr->pid);
+        pid = pvr->pid;
+		kfree(pvr);
+		platform->msvdx_pvr = NULL;
+	}
+
+}
+
+PVRSRV_KERNEL_MEM_INFO *msvdx_pvr_alloc_devmem(unsigned long alloc_size,
+	const char *mame)
+{
+	drm_emgd_priv_t        *priv;
+	igd_context_t           *context;
+	platform_context_plb_t  *platform;
+	struct msvdx_pvr_info   *pvr;
+	PVRSRV_KERNEL_MEM_INFO  *mem_info;
+	PVRSRV_ERROR err;
+
+	priv     = gpDrmDevice->dev_private;
+	context  = priv->context;
+	platform = (platform_context_plb_t *)context->platform_context;
+
+	pvr = platform->msvdx_pvr;
+	if (!pvr)
+		return NULL;
+
+	/* Is PVRSRV_MEM_CACHE_CONSISTENT needed in the allocation flags? */
+	err = PVRSRVAllocDeviceMemKM(pvr->sgx_cookie, pvr->per_proc,
+			pvr->heap_info[pvr->mapping_heap_index].hDevMemHeap,
+			PVRSRV_MEM_READ | PVRSRV_MEM_WRITE,
+			alloc_size, 0, &mem_info, name);
+	if (err != PVRSRV_OK) {
+		printk(KERN_ERR "[EMGD] MSVDX: PVRSRVAllocDeviceMemKM failed, "
+			"error=%u\n", (unsigned int)err);
+		return NULL;
+	}
+
+	return mem_info;
+}
+
+static PVRSRV_ERROR DevInitMSVDXPart1(IMG_VOID *pvDeviceNode)
+{
+    /*
+	PVRSRV_DEVICE_NODE *psDeviceNode = (PVRSRV_DEVICE_NODE *)pvDeviceNode;
+	PVRSRV_ERROR eError;
+	PVRSRV_DEV_POWER_STATE eDefaultPowerState;
+    */
+
+	/* register power operation function */
+	/* FIXME: this should be in part2 init function, but
+	 * currently here only OSPM needs IMG device... */
+    /*
+	eDefaultPowerState = PVRSRV_DEV_POWER_STATE_OFF;
+	eError = PVRSRVRegisterPowerDevice(psDeviceNode->sDevId.ui32DeviceIndex,
+					   MSVDXPrePowerState,
+					   MSVDXPostPowerState,
+					   MSVDXPreClockSpeedChange,
+					   MSVDXPostClockSpeedChange,
+					   (IMG_HANDLE)psDeviceNode,
+					   PVRSRV_DEV_POWER_STATE_ON,
+					   eDefaultPowerState);
+	if (eError != PVRSRV_OK) {
+		PVR_DPF((PVR_DBG_ERROR, "DevInitMSVDXPart1: failed to "
+			 "register device with power manager"));
+		return eError;
+	}
+    */
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR DevDeInitMSVDX(IMG_VOID *pvDeviceNode)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode = (PVRSRV_DEVICE_NODE *)pvDeviceNode;
+	PVRSRV_ERROR eError;
+
+	/* should deinit all resource */
+
+	eError = PVRSRVRemovePowerDevice(psDeviceNode->sDevId.ui32DeviceIndex);
+	if (eError != PVRSRV_OK)
+		return eError;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR MSVDXDevInitCompatCheck(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	/* version check */
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR MSVDXRegisterDevice(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	psDeviceNode->sDevId.eDeviceType	= PVRSRV_DEVICE_TYPE_MSVDX;
+	psDeviceNode->sDevId.eDeviceClass	= PVRSRV_DEVICE_CLASS_VIDEO;
+
+	psDeviceNode->pfnInitDevice		= DevInitMSVDXPart1;
+	psDeviceNode->pfnDeInitDevice		= DevDeInitMSVDX;
+
+	psDeviceNode->pfnInitDeviceCompatCheck	= MSVDXDevInitCompatCheck;
+
+	psDeviceNode->pfnDeviceISR = msvdx_mtx_isr;
+	psDeviceNode->pvISRData = (IMG_VOID *)gpDrmDevice;
+
+	return PVRSRV_OK;
+}
+
+/*
+PVRSRV_ERROR MSVDXPrePowerState(IMG_HANDLE hDevHandle,
+				 PVRSRV_DEV_POWER_STATE	eNewPowerState,
+				 PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	// ask for a change not power on
+	if ((eNewPowerState != eCurrentPowerState) &&
+	    (eNewPowerState != PVRSRV_DEV_POWER_STATE_ON)) {
+		struct drm_psb_private *dev_priv = gpDrmDevice->dev_private;
+		struct msvdx_private *msvdx_priv = dev_priv->msvdx_private;
+		MSVDX_NEW_PMSTATE(gpDrmDevice, msvdx_priv, PSB_PMSTATE_POWERDOWN);
+
+		// context save
+		psb_msvdx_save_context(gpDrmDevice);
+
+		// internally close the device //
+
+		// ask for power off //
+		if (eNewPowerState == PVRSRV_DEV_POWER_STATE_OFF) {
+			// here will deinitialize the driver if needed //
+		} else {
+			PVR_DPF((PVR_DBG_MESSAGE,
+				"%s no action for transform from %d to %d",
+				 __func__,
+				eCurrentPowerState,
+				eNewPowerState));
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR MSVDXPostPowerState(IMG_HANDLE hDevHandle,
+				 PVRSRV_DEV_POWER_STATE	eNewPowerState,
+				 PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	// if ask for change & current status is not on //
+	if ((eNewPowerState != eCurrentPowerState) &&
+	    (eCurrentPowerState != PVRSRV_DEV_POWER_STATE_ON)) {
+		// internally open device //
+		struct drm_psb_private *dev_priv = gpDrmDevice->dev_private;
+		struct msvdx_private *msvdx_priv = dev_priv->msvdx_private;
+		MSVDX_NEW_PMSTATE(gpDrmDevice, msvdx_priv, PSB_PMSTATE_POWERUP);
+
+		// context restore //
+		psb_msvdx_restore_context(gpDrmDevice);
+
+		if (eCurrentPowerState == PVRSRV_DEV_POWER_STATE_OFF) {
+			// here will initialize the driver if needed //
+		} else {
+			PVR_DPF((PVR_DBG_MESSAGE,
+				"%s no action for transform from %d to %d",
+				 __func__,
+				eCurrentPowerState,
+				eNewPowerState));
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR MSVDXPreClockSpeedChange(IMG_HANDLE hDevHandle,
+				      IMG_BOOL bIdleDevice,
+				      PVRSRV_DEV_POWER_STATE eCurrentPowerState)
+{
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR MSVDXPostClockSpeedChange(IMG_HANDLE hDevHandle,
+				      IMG_BOOL bIdleDevice,
+				      PVRSRV_DEV_POWER_STATE eCurrentPowerState)
+{
+	return PVRSRV_OK;
+}
+*/
diff --git a/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.h b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.h
new file mode 100644
index 0000000..070c935
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/msvdx/msvdx_pvr.h
@@ -0,0 +1,78 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: msvdx_pvr.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef MSVDX_POWER_H_
+#define MSVDX_POWER_H_
+#include <linux/sched.h>
+#include "services_headers.h"
+#include "sysconfig.h"
+
+extern struct drm_device *gpDrmDevice;
+
+struct msvdx_pvr_info {
+	/* HHP: FIXME: review if a mutex lock is needed here */
+	IMG_HANDLE                sgx_cookie;
+	IMG_HANDLE                dev_mem_context;
+	PVRSRV_PER_PROCESS_DATA  *per_proc;
+	PVRSRV_HEAP_INFO          heap_info[PVRSRV_MAX_CLIENT_HEAPS];
+	IMG_BOOL                  heap_shared[PVRSRV_MAX_CLIENT_HEAPS];
+	IMG_UINT32                heap_count;
+	/* index of SGX_GENERAL_MAPPING_HEAP_ID in heap_info[] */
+	int                       mapping_heap_index;
+	PVRSRV_KERNEL_MEM_INFO   *fw_mem_info;
+	/* Needed for PVR per process data */
+	struct task_struct       *kthread;
+	IMG_UINT32                pid;
+};
+/* function define */
+PVRSRV_ERROR MSVDXRegisterDevice(PVRSRV_DEVICE_NODE *psDeviceNode);
+PVRSRV_ERROR MSVDXDevInitCompatCheck(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+/* power function define */
+PVRSRV_ERROR MSVDXPrePowerState(IMG_HANDLE	hDevHandle,
+			PVRSRV_DEV_POWER_STATE	eNewPowerState,
+			PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+PVRSRV_ERROR MSVDXPostPowerState(IMG_HANDLE	hDevHandle,
+			 PVRSRV_DEV_POWER_STATE	eNewPowerState,
+			 PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+PVRSRV_ERROR MSVDXPreClockSpeedChange(IMG_HANDLE	hDevHandle,
+			      IMG_BOOL			bIdleDevice,
+			      PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+PVRSRV_ERROR MSVDXPostClockSpeedChange(IMG_HANDLE	hDevHandle,
+			       IMG_BOOL			bIdleDevice,
+			       PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+PVRSRV_ERROR MSVDXInitOSPM(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+extern int  msvdx_pvr_init(void);
+extern void msvdx_pvr_deinit(void);
+extern PVRSRV_KERNEL_MEM_INFO *msvdx_pvr_alloc_devmem(
+	unsigned long alloc_size, const char *name);
+#endif /* !MSVDX_POWER_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/igd_ovl.c b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/igd_ovl.c
new file mode 100644
index 0000000..adf31a6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/igd_ovl.c
@@ -0,0 +1,540 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_ovl.c
+ * $Revision: 1.28 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.overlay
+
+#include <context.h>
+#include <memory.h>
+#include <dsp.h>
+#include <io.h>
+
+#include <igd_mode.h>
+#include <igd_gmm.h>
+#include <igd_ovl.h>
+#include <igd_errno.h>
+#include <igd_pwr.h>
+#include <igd_render.h>
+#include <igd_blend.h>
+
+#include <general.h>
+#include <mode_access.h>
+#include <dispatch.h>
+#include <intelpci.h>
+
+#include "ovl_dispatch.h"
+#include "ovl_virt.h"
+
+void _overlay_shutdown(igd_context_t *context);
+
+
+extern ovl_dispatch_t ovl_dispatch_plb[];
+extern ovl_dispatch_t ovl_dispatch_tnc[];
+
+static dispatch_table_t ovl_dispatch_list[] = {
+
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &ovl_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &ovl_dispatch_tnc},
+#endif
+
+	{0, NULL}
+};
+
+ovl_context_t ovl_context[1];
+
+/* Description: Turns off video plane that was turned by fw/EFI. To be called by
+ * alter_ovl if needed.
+ *
+ * Notes:Upon video splash being turned on, in order to reset it, need to call
+ * secondary ovl dispatch function to turn off the right registers
+ */
+void igd_reset_fw_ovl(igd_display_context_t *display)
+{
+	ovl_dispatch_t	*ovl_dispatch = (ovl_dispatch_t *)ovl_context->dispatch;
+	if(NULL == ovl_dispatch){
+		EMGD_DEBUG("ovl_dispatch_t	*ovl_dispatch == NULL");
+		return ;
+	}
+	ovl_dispatch[OVL_SECONDARY].alter_ovl(display,
+								NULL, NULL, NULL, NULL, IGD_OVL_ALTER_OFF);
+}
+
+static int igd_ovl_set_display(igd_display_h ovl_display[])
+{
+	ovl_context->ovl_display_km[OVL_PRIMARY] = (igd_display_context_t *)ovl_display[OVL_PRIMARY];
+	ovl_context->ovl_display_km[OVL_SECONDARY] = (igd_display_context_t *)ovl_display[OVL_SECONDARY];
+
+	ovl_context->ovl_display_swapped = 1;
+	return 0;
+}
+
+static int ovl_get_display(igd_display_context_t *display,
+	igd_display_context_t *(ovl_displays[]),
+	unsigned long flags)
+{
+	igd_display_context_t *primary, *secondary;
+	unsigned long dc;
+
+	display->context->mod_dispatch.dsp_get_dc(&dc, &primary, &secondary);
+
+	/* This is a special case  to enable Dual Overlay */
+	if(flags & IGD_OVL_FORCE_USE_DISP) {
+
+		/* Use the passed in Display Handle */
+		if(display == primary) {
+
+			ovl_displays[OVL_PRIMARY]   = display;
+			ovl_displays[OVL_SECONDARY] = NULL;
+
+		} else if(display == secondary) {
+
+			ovl_displays[OVL_PRIMARY]   = NULL;
+			ovl_displays[OVL_SECONDARY] = display;
+		}
+
+		return 0;
+	}
+
+	if (dc & IGD_DISPLAY_CONFIG_CLONE || display->context->mod_dispatch.in_dih_clone_mode) {
+		/* CLONE or Vertical Extended
+		 * Primary Overlay uses the display from the primary pipe.
+		 * Secondary Overlay uses the display from the secondary pipe. */
+		/* The above is TRUE on init, but subsequent 
+		 * video plane override calls will change the overlay plane assignment.
+		 * Use ovl_um_context->ovl_display_km[OVL_PRIMARY] & 
+		 * ovl_um_context->ovl_display_km[OVL_SECONDARY] to retrieve
+		 * HW overlay(PRIMARY) & SpriteC(SECONDARY) plane to display 
+		 * handle assignment respectively */
+		ovl_displays[OVL_PRIMARY]   = ovl_context->ovl_display_km[OVL_PRIMARY];
+		ovl_displays[OVL_SECONDARY] = ovl_context->ovl_display_km[OVL_SECONDARY];
+	} else {
+		/* Single, Twin, Extended */
+		if (ovl_context->ovl_display_km[OVL_PRIMARY] == display) {
+			ovl_displays[OVL_PRIMARY]   = display;
+			ovl_displays[OVL_SECONDARY] = NULL;
+		} else {
+			ovl_displays[OVL_PRIMARY]   = NULL;
+			ovl_displays[OVL_SECONDARY] = display;
+		}
+	}
+
+	return 0;
+}
+
+static int igd_alter_ovl2(igd_display_h display_h,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	igd_display_context_t *display =
+		(igd_display_context_t *)display_h;
+	ovl_dispatch_t    *ovl_dispatch =
+		(ovl_dispatch_t *)ovl_context->dispatch;
+	int cur_ovl, ret = 0;
+	igd_display_context_t *primary, *secondary;
+	unsigned long dc;
+	igd_display_context_t *ovl_displays[OVL_MAX_HW];
+
+	EMGD_TRACE_ENTER;
+
+	if(NULL == ovl_dispatch) {
+		EMGD_DEBUG("ovl_dispatch == NULL");
+		return IGD_SUCCESS;
+	}
+
+	if(flags == IGD_FW_VIDEO_OFF)
+	{
+		igd_reset_fw_ovl(display);
+		return IGD_SUCCESS;
+	}
+
+	/* Determine which display this overlay belongs to */
+	if(display->context == NULL){
+		EMGD_DEBUG("display->context == NULL");
+		return -IGD_ERROR_INVAL;
+	}
+	display->context->mod_dispatch.dsp_get_dc(&dc, &primary, &secondary);
+
+	if ((ovl_context->ovl_display_km[OVL_PRIMARY] == NULL) &&
+		(ovl_context->ovl_display_km[OVL_SECONDARY] == NULL)) {
+		/* One time initialization of ovl_display_km */
+		ovl_displays[OVL_PRIMARY] = primary;
+		ovl_displays[OVL_SECONDARY] = secondary;
+		igd_ovl_set_display((igd_display_h)ovl_displays);
+	}
+
+	/* Determine which overlays belong to which displays */
+	ovl_get_display(display, ovl_displays, flags);
+
+	/* Determine which display this overlay belongs to */
+	if(display == ovl_displays[OVL_PRIMARY]) {
+		cur_ovl = 0;
+	} else if (display == ovl_displays[OVL_SECONDARY]) {
+		cur_ovl = 1;
+	} else {
+		/* shouldn't get here. */
+		EMGD_TRACE_EXIT;
+		return  -IGD_ERROR_INVAL;
+	}
+
+
+	/* Is the overlay is being turned off? */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+
+		/* Call family dependent overlay
+		 * function to alter the overlay. */
+		ret = ovl_dispatch[cur_ovl].alter_ovl(display,
+						  src_surf,
+						  src_rect,
+						  dest_rect,
+						  ovl_info,
+						  flags);
+
+		EMGD_TRACE_EXIT;
+		return ret;
+	}
+
+	/* Call family dependent overlay function
+	 * to alter the overlay. */
+	ret = ovl_dispatch[cur_ovl].alter_ovl(display,
+                                                      src_surf,
+                                                      src_rect,
+                                                      dest_rect,
+                                                      ovl_info,
+					      flags);
+
+	if (ret != IGD_SUCCESS) {
+		/* Turn the overlay off when there is an error */
+		ovl_dispatch[cur_ovl].alter_ovl(display,
+						NULL, NULL, NULL, NULL,
+						IGD_OVL_ALTER_OFF);
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+static int igd_query_ovl(igd_display_h display_h,
+	unsigned int flags)
+{
+	igd_display_context_t *display =
+		(igd_display_context_t *)display_h;
+	ovl_dispatch_t    *ovl_dispatch =
+		(ovl_dispatch_t *)ovl_context->dispatch;
+	int           cur_ovl;
+	igd_display_context_t *(ovl_displays[OVL_MAX_HW]); /* Array of pointers */
+	int ret = FALSE;
+
+	ovl_get_display(display, ovl_displays, flags);
+
+	/* This is a bit of a short circuit (since the hardware dependent functions
+	 * are not being called), but to determine if the hardware overlay is on,
+	 * just check the state of the ovl_context. */
+	if (flags == IGD_OVL_QUERY_IS_ON) {
+		if (ovl_context->state == OVL_STATE_ON) {
+			return TRUE;
+		} else {
+			return FALSE;
+		}
+	}
+
+	for (cur_ovl = 0; cur_ovl < OVL_MAX_HW; cur_ovl++) {
+		if (ovl_displays[cur_ovl] != NULL) {
+			ret = ovl_dispatch[cur_ovl].query_ovl(
+				(igd_display_h)(ovl_displays[cur_ovl]),
+				(flags & IGD_OVL_QUERY_MASK));
+			if (ret == FALSE) {
+				/* Can only return TRUE (event has occured and capability
+				 * is available) if it is TRUE for all displays */
+				return FALSE;
+			}
+		}
+	}
+
+	return ret;
+}
+static int igd_query_max_size_ovl(igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height)
+{
+	igd_display_context_t *display =
+		(igd_display_context_t *)display_h;
+	ovl_dispatch_t    *ovl_dispatch =
+		(ovl_dispatch_t *)ovl_context->dispatch;
+	int           cur_ovl;
+	igd_display_context_t *(ovl_displays[OVL_MAX_HW]); /* Array of pointers */
+	unsigned int tmp_max_width, tmp_max_height;
+	int ret = -IGD_ERROR_INVAL;
+
+	*max_width =  0x10000000;
+	*max_height = 0x10000000;
+
+	/* pass a dummy flag */
+	ovl_get_display(display, ovl_displays, 0);
+
+	for (cur_ovl = 0; cur_ovl < OVL_MAX_HW; cur_ovl++) {
+		if (ovl_displays[cur_ovl] != NULL) {
+			ret = ovl_dispatch[cur_ovl].query_max_size_ovl(
+				(igd_display_h)(ovl_displays[cur_ovl]), pf,
+				&tmp_max_width, &tmp_max_height);
+			if (ret != IGD_SUCCESS) {
+				/* Can only return IGD_SUCCESS (no error)
+				 * if there is no error for all displays */
+				return ret;
+			}
+			if (tmp_max_width < *max_width) {
+				*max_width = tmp_max_width;
+			}
+			if (tmp_max_height < *max_height) {
+				*max_height = tmp_max_height;
+			}
+		}
+	}
+
+	return ret;
+}
+
+
+
+/* This is a wrapper function that will call the device specific alter_ovl2_osd 
+ * function. Currently this function is used by video driver to map the subpicture 
+ * surface to second overlay so that it blends with the video surface on first 
+ * overlay. Furthermore, this is only enabled for PLB device only. 
+ */
+static int igd_alter_ovl2_osd(igd_display_h display_h,
+	igd_surface_t *sub_surface,
+	igd_rect_t *sub_src_rect,
+	igd_rect_t *sub_dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl_dispatch_t  *ovl_dispatch =
+		(ovl_dispatch_t *)ovl_context->dispatch;
+	igd_display_context_t *display =
+		(igd_display_context_t *)display_h;
+
+	int ret = 0;
+
+	ret = ovl_dispatch[1].alter_ovl(display,
+			sub_surface, sub_src_rect, sub_dest_rect,
+			ovl_info, flags);
+
+	return ret;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: igd_get_ovl_init_params(ovl_um_context_t *ovl_um_context)
+ * Description:
+ *
+ * Notes in Usage:
+ * the user mode caller fills in the primary and secondary display handles.
+ *
+ *----------------------------------------------------------------------*/
+int igd_get_ovl_init_params(igd_driver_h driver_handle,
+			    ovl_um_context_t *ovl_um_context)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_context_t *primary, *secondary;
+	unsigned long dc;
+
+	context->mod_dispatch.dsp_get_dc(&dc, &primary, &secondary);
+	ovl_um_context->dc = dc;
+	ovl_um_context->primary = primary;
+	ovl_um_context->secondary = secondary;
+    ovl_um_context->chiptype = 0;
+
+    return IGD_SUCCESS;
+}
+
+
+
+int ovl_full_init(igd_context_t *context,
+			igd_param_t *params,
+			ovl_context_t *ovl_context)
+{
+	unsigned long tmp_reg_size;
+	unsigned long region_type = IGD_GMM_REGION_TYPE_OVLREG;
+	void *virt;
+
+	if(context->device_context.did == PCI_DEVICE_ID_VGA_TNC){
+		/* Atom E6xx needs the overlay update register offset
+		 * to be a 64K aligned address. */
+		region_type = IGD_GMM_REGION_TYPE_OVLREG64;
+	}
+
+	/* Allocate a 4K page aligned region from
+	 * memory manager for overlay register update memory */
+
+	tmp_reg_size = 4096;
+	GMM_SET_DEBUG_NAME("Overlay Register Buffer");
+	if(context->dispatch.gmm_alloc_region(
+		   &(ovl_context->reg_update_offset),
+		   &tmp_reg_size,
+		   IGD_GMM_REGION_TYPE_OVLREG,
+		   0)){
+
+		EMGD_ERROR_EXIT("Memory allocation for "
+			"Overlay Register Update failed");
+
+		ovl_context->reg_allocated = 0;
+
+		return -IGD_ERROR_NOMEM;
+	}
+	ovl_context->reg_allocated = 1;
+	ovl_context->ovl_display_swapped = 0;
+
+	/* Get the register update physical address in RAM */
+	if(context->dispatch.gmm_virt_to_phys(
+		   ovl_context->reg_update_offset,
+		   &ovl_context->reg_update_phys)) {
+
+		EMGD_ERROR_EXIT("Virtual to Physical Address translation failed");
+		return -IGD_ERROR_NOMEM;
+	}
+
+	/*
+	 * TODO: Verify that phys_to_virt returns a valid address for
+	 * agp memory
+	 */
+	virt = phys_to_virt(ovl_context->reg_update_phys);
+
+	/* Clear the register update page */
+	OS_MEMSET(virt, 0, tmp_reg_size);
+
+
+	/* Get overlay's dispatch table */
+	ovl_context->dispatch = (ovl_dispatch_t (*)[])dispatch_acquire(context,
+		ovl_dispatch_list);
+	if(!ovl_context->dispatch) {
+		EMGD_ERROR_EXIT("Unsupported Device");
+		return -IGD_ERROR_NODEV;
+	}
+
+
+	/* Hook up the IGD dispatch table entries for overlay
+	 * Alter has a common function, query can call the family function
+	 * directly */
+	context->dispatch.get_ovl_init_params = igd_get_ovl_init_params;
+	context->dispatch.alter_ovl = NULL;
+	context->dispatch.alter_ovl2 = igd_alter_ovl2;
+//	context->dispatch.alter_ovl2_dihclone = igd_alter_ovl2_dihclone;
+	context->dispatch.query_ovl = igd_query_ovl;
+	context->dispatch.query_max_size_ovl = igd_query_max_size_ovl;
+	context->dispatch.alter_ovl2_osd = igd_alter_ovl2_osd;
+	context->dispatch.set_ovl_display = igd_ovl_set_display;
+
+	/* Hook up optional inter-module functions */
+	context->mod_dispatch.overlay_shutdown = _overlay_shutdown;
+
+	/* Initialize any OS / Chipst general HAL params for overlay */
+	if(params->display_flags & IGD_DISPLAY_FB_BLEND_OVL){
+		ovl_context->fb_blend_ovl = 1;
+	}
+	ovl_context->saved_src_surf = NULL;
+	ovl_context->saved_src_rect = NULL;
+	ovl_context->saved_dest_rect = NULL;
+	ovl_context->saved_ovl_info = NULL;
+	ovl_context->saved_flags	=  0;
+	ovl_context->ovl_display_km[OVL_PRIMARY] = NULL;
+	ovl_context->ovl_display_km[OVL_SECONDARY] = NULL;
+
+	
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+void _overlay_shutdown(igd_context_t *context)
+{
+
+	EMGD_TRACE_ENTER;
+
+	if (ovl_context->reg_allocated) {
+		context->dispatch.gmm_free(ovl_context->reg_update_offset);
+		ovl_context->reg_update_offset = 0;
+		ovl_context->reg_allocated = 0;
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return;
+}
+
+/*----------------------------------------------------------------------
+ * Function: igd_overlay_pwr()
+ * Description:
+ *  igd_overlay_pwr will only be called from power module .
+ *  It shuts down / powers up overlay output based on the power
+ *  state transition requested.
+ *
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+int igd_overlay_pwr(
+	igd_driver_h driver_handle,
+	int power_state)
+{
+	igd_context_t *context = (igd_context_t *)driver_handle;
+	igd_display_context_t *primary, *secondary;
+
+	/* NOTE: The overlay will not be turned on by this method, but will be
+	 * turned on with the next call to alter_ovl */
+
+	/* Turn off the overlay for every display.  Most of the displays will
+	 * likely not have the overlay off, but there should be no harm it turning
+	 * it off for a display which does not have the overlay. */
+
+	EMGD_TRACE_ENTER;
+	if(power_state != IGD_POWERSTATE_D0){
+		context->mod_dispatch.dsp_get_dc(NULL, &primary, &secondary);
+
+		if(primary) {
+			/* Turn the overlay off. */
+			igd_alter_ovl2((igd_display_h)primary,
+				 NULL, NULL, NULL, NULL, IGD_OVL_ALTER_OFF);
+		}
+		if(secondary) {
+			igd_alter_ovl2((igd_display_h)secondary,
+				 NULL, NULL, NULL, NULL, IGD_OVL_ALTER_OFF);
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/micro_ovl.c b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/micro_ovl.c
new file mode 100644
index 0000000..7a15220
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/micro_ovl.c
@@ -0,0 +1,165 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_ovl.c
+ * $Revision: 1.13 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.overlay
+
+#include <context.h>
+#include <memory.h>
+#include <dsp.h>
+#include <io.h>
+
+#include <igd_mode.h>
+#include <igd_gmm.h>
+//#include <igd_ovl.h>
+#include <igd_pwr.h>
+#include <igd_render.h>
+#include <igd_blend.h>
+
+#include <general.h>
+#include <mode_access.h>
+#include <dispatch.h>
+#include <intelpci.h>
+
+#include "ovl_dispatch.h"
+#include "ovl_virt.h"
+
+/*
+ * TODO: If there is micro overlay for device dependent layer
+ * modify extern below to the correct micro device dispatch
+ * Only two micro overlay existing is for Plb and Ctg
+ */
+
+extern ovl_dispatch_t ovl_micro_dispatch_plb[];
+
+static dispatch_table_t ovl_micro_dispatch_list[] = {
+/*
+ * TODO: Fix this.
+ *  * This table would point the micro dispatch tables.
+ *  For now, other than Plb ,refer the micro dispatch
+ *  to the original table
+ * */
+
+#ifdef CONFIG_PLB
+	{PCI_DEVICE_ID_VGA_PLB, &ovl_micro_dispatch_plb},
+#endif
+#ifdef CONFIG_TNC
+	{PCI_DEVICE_ID_VGA_TNC, &ovl_micro_dispatch_plb},
+#endif
+	{0, NULL}
+};
+extern int ovl_full_init(igd_context_t *context, igd_param_t *params,ovl_context_t *ovl_context);
+
+#ifndef CONFIG_MICRO_OVERLAY
+#define OVL_FULL_INIT(a, b, c) ovl_full_init(a, b, c)
+#else
+#define OVL_FULL_INIT(a, b, c) 1
+#endif
+
+extern ovl_context_t ovl_context[1];
+static int igd_micro_alter_ovl(igd_display_h display_h,
+	igd_appcontext_h     appcontext_h,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	igd_display_context_t *display =
+		(igd_display_context_t *)display_h;
+	ovl_dispatch_t    *ovl_dispatch =
+		(ovl_dispatch_t *)ovl_context->dispatch;
+
+	igd_display_context_t *ovl_display;
+
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	ovl_display = display;
+
+	/* Call family dependent overlay function to alter the overlay. */
+	ret = ovl_dispatch->alter_ovl(
+				ovl_display, src_surf, src_rect,
+				dest_rect, ovl_info, flags);
+
+	if (ret != IGD_SUCCESS) {
+		EMGD_ERROR("Error micro alter overlay\n");
+		/* Turn the overlay off when there is an error */
+		ovl_dispatch->alter_ovl(ovl_display,
+					NULL, NULL, NULL, NULL, IGD_OVL_ALTER_OFF);
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+int _overlay_init(igd_context_t *context, igd_param_t *params)
+{
+
+	EMGD_TRACE_ENTER;
+
+	/* Ensure the device context pointer is valid */
+	if(!context){
+		EMGD_ERROR_EXIT("Error: Null context");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Clear the allocated memory for overlay context */
+	OS_MEMSET((void *)ovl_context, 0, sizeof(ovl_context_t));
+
+	/* Get overlay's dispatch table */
+	ovl_context->dispatch = (ovl_dispatch_t (*)[])dispatch_acquire(context,
+		ovl_micro_dispatch_list);
+	if(!ovl_context->dispatch) {
+		EMGD_ERROR_EXIT("Error: Unsupported Device");
+		return -IGD_ERROR_NODEV;
+	}
+
+
+	/* Hook up the IGD dispatch table entries for overlay
+	 * Alter has a common function, query can call the family function
+	 * directly */
+	context->dispatch.alter_ovl = igd_micro_alter_ovl;
+	context->dispatch.query_ovl = NULL;
+	context->dispatch.query_max_size_ovl = NULL;
+	context->mod_dispatch.overlay_shutdown = NULL;
+
+	if (OVL_FULL_INIT(context, params, ovl_context)) {
+		/*
+		 * Even if full init failed we can still use the micro-gmm
+		 */
+		EMGD_DEBUG("Full overlay did not initialize, using micro-overlay");
+
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.c b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.c
new file mode 100644
index 0000000..fc08f0e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.c
@@ -0,0 +1,1127 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_coeff.c
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file calculates the coefficient values used by the various
+ *  platform families.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <general.h>
+#include <memory.h>
+
+#include "ovl_coeff.h"
+
+/*****************************************************************
+ *                                                               *
+ *        HARDCODED QUARTER SINE WAVE TABLES                     *
+ *        4096 unsigned shorts MSB is integer                    *
+ *        and 15 LSB's are after the radix                       *
+ *                                                               *
+ *****************************************************************/
+static unsigned short wave_table[4096] = {
+	/* 16 bit value - all 16 bits are fp only */
+	0x0,  0x19,  0x32,  0x4b,  0x65,  0x7e,  0x97,  0xb0,  0xc9,  0xe2,
+	0xfb,  0x114,  0x12e,  0x147,  0x160,  0x179,  0x192,  0x1ab,  0x1c4,  0x1de,
+	0x1f7,  0x210,  0x229,  0x242,  0x25b,  0x274,  0x28d,  0x2a7,  0x2c0,  0x2d9,
+	0x2f2,  0x30b,  0x324,  0x33d,  0x356,  0x370,  0x389,  0x3a2,  0x3bb,  0x3d4,
+	0x3ed,  0x406,  0x420,  0x439,  0x452,  0x46b,  0x484,  0x49d,  0x4b6,  0x4cf,
+	0x4e9,  0x502,  0x51b,  0x534,  0x54d,  0x566,  0x57f,  0x598,  0x5b2,  0x5cb,
+	0x5e4,  0x5fd,  0x616,  0x62f,  0x648,  0x661,  0x67b,  0x694,  0x6ad,  0x6c6,
+	0x6df,  0x6f8,  0x711,  0x72a,  0x744,  0x75d,  0x776,  0x78f,  0x7a8,  0x7c1,
+	0x7da,  0x7f3,  0x80d,  0x826,  0x83f,  0x858,  0x871,  0x88a,  0x8a3,  0x8bc,
+	0x8d5,  0x8ef,  0x908,  0x921,  0x93a,  0x953,  0x96c,  0x985,  0x99e,  0x9b8,
+	0x9d1,  0x9ea,  0xa03,  0xa1c,  0xa35,  0xa4e,  0xa67,  0xa80,  0xa9a,  0xab3,
+	0xacc,  0xae5,  0xafe,  0xb17,  0xb30,  0xb49,  0xb62,  0xb7c,  0xb95,  0xbae,
+	0xbc7,  0xbe0,  0xbf9,  0xc12,  0xc2b,  0xc44,  0xc5d,  0xc77,  0xc90,  0xca9,
+	0xcc2,  0xcdb,  0xcf4,  0xd0d,  0xd26,  0xd3f,  0xd59,  0xd72,  0xd8b,  0xda4,
+	0xdbd,  0xdd6,  0xdef,  0xe08,  0xe21,  0xe3a,  0xe53,  0xe6d,  0xe86,  0xe9f,
+	0xeb8,  0xed1,  0xeea,  0xf03,  0xf1c,  0xf35,  0xf4e,  0xf67,  0xf81,  0xf9a,
+	0xfb3,  0xfcc,  0xfe5,  0xffe,  0x1017,  0x1030,  0x1049,  0x1062,  0x107b,  0x1094,
+	0x10ae,  0x10c7,  0x10e0,  0x10f9,  0x1112,  0x112b,  0x1144,  0x115d,  0x1176,  0x118f,
+	0x11a8,  0x11c1,  0x11da,  0x11f4,  0x120d,  0x1226,  0x123f,  0x1258,  0x1271,  0x128a,
+	0x12a3,  0x12bc,  0x12d5,  0x12ee,  0x1307,  0x1320,  0x1339,  0x1352,  0x136c,  0x1385,
+	0x139e,  0x13b7,  0x13d0,  0x13e9,  0x1402,  0x141b,  0x1434,  0x144d,  0x1466,  0x147f,
+	0x1498,  0x14b1,  0x14ca,  0x14e3,  0x14fc,  0x1515,  0x152e,  0x1548,  0x1561,  0x157a,
+	0x1593,  0x15ac,  0x15c5,  0x15de,  0x15f7,  0x1610,  0x1629,  0x1642,  0x165b,  0x1674,
+	0x168d,  0x16a6,  0x16bf,  0x16d8,  0x16f1,  0x170a,  0x1723,  0x173c,  0x1755,  0x176e,
+	0x1787,  0x17a0,  0x17b9,  0x17d2,  0x17eb,  0x1804,  0x181d,  0x1837,  0x1850,  0x1869,
+	0x1882,  0x189b,  0x18b4,  0x18cd,  0x18e6,  0x18ff,  0x1918,  0x1931,  0x194a,  0x1963,
+	0x197c,  0x1995,  0x19ae,  0x19c7,  0x19e0,  0x19f9,  0x1a12,  0x1a2b,  0x1a44,  0x1a5d,
+	0x1a76,  0x1a8f,  0x1aa8,  0x1ac1,  0x1ada,  0x1af3,  0x1b0c,  0x1b25,  0x1b3e,  0x1b57,
+	0x1b70,  0x1b89,  0x1ba2,  0x1bbb,  0x1bd4,  0x1bed,  0x1c06,  0x1c1f,  0x1c38,  0x1c51,
+	0x1c69,  0x1c82,  0x1c9b,  0x1cb4,  0x1ccd,  0x1ce6,  0x1cff,  0x1d18,  0x1d31,  0x1d4a,
+	0x1d63,  0x1d7c,  0x1d95,  0x1dae,  0x1dc7,  0x1de0,  0x1df9,  0x1e12,  0x1e2b,  0x1e44,
+	0x1e5d,  0x1e76,  0x1e8f,  0x1ea8,  0x1ec1,  0x1eda,  0x1ef3,  0x1f0b,  0x1f24,  0x1f3d,
+	0x1f56,  0x1f6f,  0x1f88,  0x1fa1,  0x1fba,  0x1fd3,  0x1fec,  0x2005,  0x201e,  0x2037,
+	0x2050,  0x2069,  0x2082,  0x209a,  0x20b3,  0x20cc,  0x20e5,  0x20fe,  0x2117,  0x2130,
+	0x2149,  0x2162,  0x217b,  0x2194,  0x21ad,  0x21c6,  0x21de,  0x21f7,  0x2210,  0x2229,
+	0x2242,  0x225b,  0x2274,  0x228d,  0x22a6,  0x22bf,  0x22d7,  0x22f0,  0x2309,  0x2322,
+	0x233b,  0x2354,  0x236d,  0x2386,  0x239f,  0x23b8,  0x23d0,  0x23e9,  0x2402,  0x241b,
+	0x2434,  0x244d,  0x2466,  0x247f,  0x2497,  0x24b0,  0x24c9,  0x24e2,  0x24fb,  0x2514,
+	0x252d,  0x2546,  0x255e,  0x2577,  0x2590,  0x25a9,  0x25c2,  0x25db,  0x25f4,  0x260c,
+	0x2625,  0x263e,  0x2657,  0x2670,  0x2689,  0x26a2,  0x26ba,  0x26d3,  0x26ec,  0x2705,
+	0x271e,  0x2737,  0x274f,  0x2768,  0x2781,  0x279a,  0x27b3,  0x27cc,  0x27e4,  0x27fd,
+	0x2816,  0x282f,  0x2848,  0x2860,  0x2879,  0x2892,  0x28ab,  0x28c4,  0x28dd,  0x28f5,
+	0x290e,  0x2927,  0x2940,  0x2959,  0x2971,  0x298a,  0x29a3,  0x29bc,  0x29d5,  0x29ed,
+	0x2a06,  0x2a1f,  0x2a38,  0x2a51,  0x2a69,  0x2a82,  0x2a9b,  0x2ab4,  0x2acc,  0x2ae5,
+	0x2afe,  0x2b17,  0x2b30,  0x2b48,  0x2b61,  0x2b7a,  0x2b93,  0x2bab,  0x2bc4,  0x2bdd,
+	0x2bf6,  0x2c0e,  0x2c27,  0x2c40,  0x2c59,  0x2c71,  0x2c8a,  0x2ca3,  0x2cbc,  0x2cd4,
+	0x2ced,  0x2d06,  0x2d1f,  0x2d37,  0x2d50,  0x2d69,  0x2d82,  0x2d9a,  0x2db3,  0x2dcc,
+	0x2de5,  0x2dfd,  0x2e16,  0x2e2f,  0x2e47,  0x2e60,  0x2e79,  0x2e92,  0x2eaa,  0x2ec3,
+	0x2edc,  0x2ef4,  0x2f0d,  0x2f26,  0x2f3f,  0x2f57,  0x2f70,  0x2f89,  0x2fa1,  0x2fba,
+	0x2fd3,  0x2feb,  0x3004,  0x301d,  0x3035,  0x304e,  0x3067,  0x307f,  0x3098,  0x30b1,
+	0x30ca,  0x30e2,  0x30fb,  0x3114,  0x312c,  0x3145,  0x315e,  0x3176,  0x318f,  0x31a7,
+	0x31c0,  0x31d9,  0x31f1,  0x320a,  0x3223,  0x323b,  0x3254,  0x326d,  0x3285,  0x329e,
+	0x32b7,  0x32cf,  0x32e8,  0x3300,  0x3319,  0x3332,  0x334a,  0x3363,  0x337c,  0x3394,
+	0x33ad,  0x33c5,  0x33de,  0x33f7,  0x340f,  0x3428,  0x3440,  0x3459,  0x3472,  0x348a,
+	0x34a3,  0x34bb,  0x34d4,  0x34ed,  0x3505,  0x351e,  0x3536,  0x354f,  0x3568,  0x3580,
+	0x3599,  0x35b1,  0x35ca,  0x35e2,  0x35fb,  0x3614,  0x362c,  0x3645,  0x365d,  0x3676,
+	0x368e,  0x36a7,  0x36c0,  0x36d8,  0x36f1,  0x3709,  0x3722,  0x373a,  0x3753,  0x376b,
+	0x3784,  0x379c,  0x37b5,  0x37cd,  0x37e6,  0x37fe,  0x3817,  0x3830,  0x3848,  0x3861,
+	0x3879,  0x3892,  0x38aa,  0x38c3,  0x38db,  0x38f4,  0x390c,  0x3925,  0x393d,  0x3956,
+	0x396e,  0x3987,  0x399f,  0x39b8,  0x39d0,  0x39e9,  0x3a01,  0x3a1a,  0x3a32,  0x3a4a,
+	0x3a63,  0x3a7b,  0x3a94,  0x3aac,  0x3ac5,  0x3add,  0x3af6,  0x3b0e,  0x3b27,  0x3b3f,
+	0x3b58,  0x3b70,  0x3b88,  0x3ba1,  0x3bb9,  0x3bd2,  0x3bea,  0x3c03,  0x3c1b,  0x3c33,
+	0x3c4c,  0x3c64,  0x3c7d,  0x3c95,  0x3cae,  0x3cc6,  0x3cde,  0x3cf7,  0x3d0f,  0x3d28,
+	0x3d40,  0x3d58,  0x3d71,  0x3d89,  0x3da2,  0x3dba,  0x3dd2,  0x3deb,  0x3e03,  0x3e1c,
+	0x3e34,  0x3e4c,  0x3e65,  0x3e7d,  0x3e95,  0x3eae,  0x3ec6,  0x3edf,  0x3ef7,  0x3f0f,
+	0x3f28,  0x3f40,  0x3f58,  0x3f71,  0x3f89,  0x3fa1,  0x3fba,  0x3fd2,  0x3fea,  0x4003,
+	0x401b,  0x4033,  0x404c,  0x4064,  0x407c,  0x4095,  0x40ad,  0x40c5,  0x40de,  0x40f6,
+	0x410e,  0x4127,  0x413f,  0x4157,  0x416f,  0x4188,  0x41a0,  0x41b8,  0x41d1,  0x41e9,
+	0x4201,  0x421a,  0x4232,  0x424a,  0x4262,  0x427b,  0x4293,  0x42ab,  0x42c3,  0x42dc,
+	0x42f4,  0x430c,  0x4324,  0x433d,  0x4355,  0x436d,  0x4385,  0x439e,  0x43b6,  0x43ce,
+	0x43e6,  0x43ff,  0x4417,  0x442f,  0x4447,  0x4460,  0x4478,  0x4490,  0x44a8,  0x44c0,
+	0x44d9,  0x44f1,  0x4509,  0x4521,  0x4539,  0x4552,  0x456a,  0x4582,  0x459a,  0x45b2,
+	0x45cb,  0x45e3,  0x45fb,  0x4613,  0x462b,  0x4643,  0x465c,  0x4674,  0x468c,  0x46a4,
+	0x46bc,  0x46d4,  0x46ec,  0x4705,  0x471d,  0x4735,  0x474d,  0x4765,  0x477d,  0x4795,
+	0x47ae,  0x47c6,  0x47de,  0x47f6,  0x480e,  0x4826,  0x483e,  0x4856,  0x486f,  0x4887,
+	0x489f,  0x48b7,  0x48cf,  0x48e7,  0x48ff,  0x4917,  0x492f,  0x4947,  0x495f,  0x4978,
+	0x4990,  0x49a8,  0x49c0,  0x49d8,  0x49f0,  0x4a08,  0x4a20,  0x4a38,  0x4a50,  0x4a68,
+	0x4a80,  0x4a98,  0x4ab0,  0x4ac8,  0x4ae0,  0x4af8,  0x4b10,  0x4b28,  0x4b40,  0x4b58,
+	0x4b71,  0x4b89,  0x4ba1,  0x4bb9,  0x4bd1,  0x4be9,  0x4c01,  0x4c19,  0x4c31,  0x4c49,
+	0x4c61,  0x4c79,  0x4c90,  0x4ca8,  0x4cc0,  0x4cd8,  0x4cf0,  0x4d08,  0x4d20,  0x4d38,
+	0x4d50,  0x4d68,  0x4d80,  0x4d98,  0x4db0,  0x4dc8,  0x4de0,  0x4df8,  0x4e10,  0x4e28,
+	0x4e40,  0x4e58,  0x4e70,  0x4e87,  0x4e9f,  0x4eb7,  0x4ecf,  0x4ee7,  0x4eff,  0x4f17,
+	0x4f2f,  0x4f47,  0x4f5f,  0x4f77,  0x4f8e,  0x4fa6,  0x4fbe,  0x4fd6,  0x4fee,  0x5006,
+	0x501e,  0x5036,  0x504d,  0x5065,  0x507d,  0x5095,  0x50ad,  0x50c5,  0x50dd,  0x50f4,
+	0x510c,  0x5124,  0x513c,  0x5154,  0x516c,  0x5183,  0x519b,  0x51b3,  0x51cb,  0x51e3,
+	0x51fb,  0x5212,  0x522a,  0x5242,  0x525a,  0x5272,  0x5289,  0x52a1,  0x52b9,  0x52d1,
+	0x52e8,  0x5300,  0x5318,  0x5330,  0x5348,  0x535f,  0x5377,  0x538f,  0x53a7,  0x53be,
+	0x53d6,  0x53ee,  0x5406,  0x541d,  0x5435,  0x544d,  0x5464,  0x547c,  0x5494,  0x54ac,
+	0x54c3,  0x54db,  0x54f3,  0x550b,  0x5522,  0x553a,  0x5552,  0x5569,  0x5581,  0x5599,
+	0x55b0,  0x55c8,  0x55e0,  0x55f7,  0x560f,  0x5627,  0x563e,  0x5656,  0x566e,  0x5685,
+	0x569d,  0x56b5,  0x56cc,  0x56e4,  0x56fc,  0x5713,  0x572b,  0x5743,  0x575a,  0x5772,
+	0x5789,  0x57a1,  0x57b9,  0x57d0,  0x57e8,  0x57ff,  0x5817,  0x582f,  0x5846,  0x585e,
+	0x5875,  0x588d,  0x58a5,  0x58bc,  0x58d4,  0x58eb,  0x5903,  0x591a,  0x5932,  0x594a,
+	0x5961,  0x5979,  0x5990,  0x59a8,  0x59bf,  0x59d7,  0x59ee,  0x5a06,  0x5a1d,  0x5a35,
+	0x5a4c,  0x5a64,  0x5a7b,  0x5a93,  0x5aaa,  0x5ac2,  0x5ad9,  0x5af1,  0x5b08,  0x5b20,
+	0x5b37,  0x5b4f,  0x5b66,  0x5b7e,  0x5b95,  0x5bad,  0x5bc4,  0x5bdc,  0x5bf3,  0x5c0b,
+	0x5c22,  0x5c3a,  0x5c51,  0x5c68,  0x5c80,  0x5c97,  0x5caf,  0x5cc6,  0x5cde,  0x5cf5,
+	0x5d0c,  0x5d24,  0x5d3b,  0x5d53,  0x5d6a,  0x5d81,  0x5d99,  0x5db0,  0x5dc8,  0x5ddf,
+	0x5df6,  0x5e0e,  0x5e25,  0x5e3c,  0x5e54,  0x5e6b,  0x5e83,  0x5e9a,  0x5eb1,  0x5ec9,
+	0x5ee0,  0x5ef7,  0x5f0f,  0x5f26,  0x5f3d,  0x5f55,  0x5f6c,  0x5f83,  0x5f9b,  0x5fb2,
+	0x5fc9,  0x5fe1,  0x5ff8,  0x600f,  0x6026,  0x603e,  0x6055,  0x606c,  0x6084,  0x609b,
+	0x60b2,  0x60c9,  0x60e1,  0x60f8,  0x610f,  0x6126,  0x613e,  0x6155,  0x616c,  0x6183,
+	0x619b,  0x61b2,  0x61c9,  0x61e0,  0x61f8,  0x620f,  0x6226,  0x623d,  0x6254,  0x626c,
+	0x6283,  0x629a,  0x62b1,  0x62c8,  0x62e0,  0x62f7,  0x630e,  0x6325,  0x633c,  0x6353,
+	0x636b,  0x6382,  0x6399,  0x63b0,  0x63c7,  0x63de,  0x63f5,  0x640d,  0x6424,  0x643b,
+	0x6452,  0x6469,  0x6480,  0x6497,  0x64ae,  0x64c6,  0x64dd,  0x64f4,  0x650b,  0x6522,
+	0x6539,  0x6550,  0x6567,  0x657e,  0x6595,  0x65ac,  0x65c3,  0x65db,  0x65f2,  0x6609,
+	0x6620,  0x6637,  0x664e,  0x6665,  0x667c,  0x6693,  0x66aa,  0x66c1,  0x66d8,  0x66ef,
+	0x6706,  0x671d,  0x6734,  0x674b,  0x6762,  0x6779,  0x6790,  0x67a7,  0x67be,  0x67d5,
+	0x67ec,  0x6803,  0x681a,  0x6831,  0x6848,  0x685f,  0x6876,  0x688d,  0x68a3,  0x68ba,
+	0x68d1,  0x68e8,  0x68ff,  0x6916,  0x692d,  0x6944,  0x695b,  0x6972,  0x6989,  0x69a0,
+	0x69b6,  0x69cd,  0x69e4,  0x69fb,  0x6a12,  0x6a29,  0x6a40,  0x6a57,  0x6a6d,  0x6a84,
+	0x6a9b,  0x6ab2,  0x6ac9,  0x6ae0,  0x6af6,  0x6b0d,  0x6b24,  0x6b3b,  0x6b52,  0x6b69,
+	0x6b7f,  0x6b96,  0x6bad,  0x6bc4,  0x6bdb,  0x6bf1,  0x6c08,  0x6c1f,  0x6c36,  0x6c4d,
+	0x6c63,  0x6c7a,  0x6c91,  0x6ca8,  0x6cbe,  0x6cd5,  0x6cec,  0x6d03,  0x6d19,  0x6d30,
+	0x6d47,  0x6d5e,  0x6d74,  0x6d8b,  0x6da2,  0x6db8,  0x6dcf,  0x6de6,  0x6dfc,  0x6e13,
+	0x6e2a,  0x6e41,  0x6e57,  0x6e6e,  0x6e85,  0x6e9b,  0x6eb2,  0x6ec9,  0x6edf,  0x6ef6,
+	0x6f0d,  0x6f23,  0x6f3a,  0x6f50,  0x6f67,  0x6f7e,  0x6f94,  0x6fab,  0x6fc2,  0x6fd8,
+	0x6fef,  0x7005,  0x701c,  0x7033,  0x7049,  0x7060,  0x7076,  0x708d,  0x70a3,  0x70ba,
+	0x70d1,  0x70e7,  0x70fe,  0x7114,  0x712b,  0x7141,  0x7158,  0x716e,  0x7185,  0x719b,
+	0x71b2,  0x71c9,  0x71df,  0x71f6,  0x720c,  0x7223,  0x7239,  0x7250,  0x7266,  0x727c,
+	0x7293,  0x72a9,  0x72c0,  0x72d6,  0x72ed,  0x7303,  0x731a,  0x7330,  0x7347,  0x735d,
+	0x7373,  0x738a,  0x73a0,  0x73b7,  0x73cd,  0x73e4,  0x73fa,  0x7410,  0x7427,  0x743d,
+	0x7454,  0x746a,  0x7480,  0x7497,  0x74ad,  0x74c3,  0x74da,  0x74f0,  0x7507,  0x751d,
+	0x7533,  0x754a,  0x7560,  0x7576,  0x758d,  0x75a3,  0x75b9,  0x75d0,  0x75e6,  0x75fc,
+	0x7612,  0x7629,  0x763f,  0x7655,  0x766c,  0x7682,  0x7698,  0x76ae,  0x76c5,  0x76db,
+	0x76f1,  0x7708,  0x771e,  0x7734,  0x774a,  0x7760,  0x7777,  0x778d,  0x77a3,  0x77b9,
+	0x77d0,  0x77e6,  0x77fc,  0x7812,  0x7828,  0x783f,  0x7855,  0x786b,  0x7881,  0x7897,
+	0x78ad,  0x78c4,  0x78da,  0x78f0,  0x7906,  0x791c,  0x7932,  0x7949,  0x795f,  0x7975,
+	0x798b,  0x79a1,  0x79b7,  0x79cd,  0x79e3,  0x79f9,  0x7a10,  0x7a26,  0x7a3c,  0x7a52,
+	0x7a68,  0x7a7e,  0x7a94,  0x7aaa,  0x7ac0,  0x7ad6,  0x7aec,  0x7b02,  0x7b18,  0x7b2e,
+	0x7b44,  0x7b5a,  0x7b70,  0x7b86,  0x7b9c,  0x7bb2,  0x7bc8,  0x7bde,  0x7bf4,  0x7c0a,
+	0x7c20,  0x7c36,  0x7c4c,  0x7c62,  0x7c78,  0x7c8e,  0x7ca4,  0x7cba,  0x7cd0,  0x7ce6,
+	0x7cfc,  0x7d12,  0x7d28,  0x7d3e,  0x7d54,  0x7d6a,  0x7d7f,  0x7d95,  0x7dab,  0x7dc1,
+	0x7dd7,  0x7ded,  0x7e03,  0x7e19,  0x7e2f,  0x7e44,  0x7e5a,  0x7e70,  0x7e86,  0x7e9c,
+	0x7eb2,  0x7ec8,  0x7edd,  0x7ef3,  0x7f09,  0x7f1f,  0x7f35,  0x7f4a,  0x7f60,  0x7f76,
+	0x7f8c,  0x7fa2,  0x7fb7,  0x7fcd,  0x7fe3,  0x7ff9,  0x800f,  0x8024,  0x803a,  0x8050,
+	0x8066,  0x807b,  0x8091,  0x80a7,  0x80bc,  0x80d2,  0x80e8,  0x80fe,  0x8113,  0x8129,
+	0x813f,  0x8154,  0x816a,  0x8180,  0x8195,  0x81ab,  0x81c1,  0x81d6,  0x81ec,  0x8202,
+	0x8217,  0x822d,  0x8243,  0x8258,  0x826e,  0x8284,  0x8299,  0x82af,  0x82c4,  0x82da,
+	0x82f0,  0x8305,  0x831b,  0x8330,  0x8346,  0x835c,  0x8371,  0x8387,  0x839c,  0x83b2,
+	0x83c7,  0x83dd,  0x83f2,  0x8408,  0x841d,  0x8433,  0x8449,  0x845e,  0x8474,  0x8489,
+	0x849f,  0x84b4,  0x84ca,  0x84df,  0x84f5,  0x850a,  0x851f,  0x8535,  0x854a,  0x8560,
+	0x8575,  0x858b,  0x85a0,  0x85b6,  0x85cb,  0x85e0,  0x85f6,  0x860b,  0x8621,  0x8636,
+	0x864c,  0x8661,  0x8676,  0x868c,  0x86a1,  0x86b6,  0x86cc,  0x86e1,  0x86f7,  0x870c,
+	0x8721,  0x8737,  0x874c,  0x8761,  0x8777,  0x878c,  0x87a1,  0x87b7,  0x87cc,  0x87e1,
+	0x87f6,  0x880c,  0x8821,  0x8836,  0x884c,  0x8861,  0x8876,  0x888b,  0x88a1,  0x88b6,
+	0x88cb,  0x88e0,  0x88f6,  0x890b,  0x8920,  0x8935,  0x894a,  0x8960,  0x8975,  0x898a,
+	0x899f,  0x89b4,  0x89ca,  0x89df,  0x89f4,  0x8a09,  0x8a1e,  0x8a34,  0x8a49,  0x8a5e,
+	0x8a73,  0x8a88,  0x8a9d,  0x8ab2,  0x8ac7,  0x8add,  0x8af2,  0x8b07,  0x8b1c,  0x8b31,
+	0x8b46,  0x8b5b,  0x8b70,  0x8b85,  0x8b9a,  0x8baf,  0x8bc5,  0x8bda,  0x8bef,  0x8c04,
+	0x8c19,  0x8c2e,  0x8c43,  0x8c58,  0x8c6d,  0x8c82,  0x8c97,  0x8cac,  0x8cc1,  0x8cd6,
+	0x8ceb,  0x8d00,  0x8d15,  0x8d2a,  0x8d3f,  0x8d54,  0x8d69,  0x8d7e,  0x8d93,  0x8da7,
+	0x8dbc,  0x8dd1,  0x8de6,  0x8dfb,  0x8e10,  0x8e25,  0x8e3a,  0x8e4f,  0x8e64,  0x8e79,
+	0x8e8d,  0x8ea2,  0x8eb7,  0x8ecc,  0x8ee1,  0x8ef6,  0x8f0b,  0x8f1f,  0x8f34,  0x8f49,
+	0x8f5e,  0x8f73,  0x8f88,  0x8f9c,  0x8fb1,  0x8fc6,  0x8fdb,  0x8ff0,  0x9004,  0x9019,
+	0x902e,  0x9043,  0x9057,  0x906c,  0x9081,  0x9096,  0x90aa,  0x90bf,  0x90d4,  0x90e9,
+	0x90fd,  0x9112,  0x9127,  0x913b,  0x9150,  0x9165,  0x9179,  0x918e,  0x91a3,  0x91b7,
+	0x91cc,  0x91e1,  0x91f5,  0x920a,  0x921f,  0x9233,  0x9248,  0x925d,  0x9271,  0x9286,
+	0x929a,  0x92af,  0x92c4,  0x92d8,  0x92ed,  0x9301,  0x9316,  0x932a,  0x933f,  0x9354,
+	0x9368,  0x937d,  0x9391,  0x93a6,  0x93ba,  0x93cf,  0x93e3,  0x93f8,  0x940c,  0x9421,
+	0x9435,  0x944a,  0x945e,  0x9473,  0x9487,  0x949c,  0x94b0,  0x94c5,  0x94d9,  0x94ee,
+	0x9502,  0x9516,  0x952b,  0x953f,  0x9554,  0x9568,  0x957d,  0x9591,  0x95a5,  0x95ba,
+	0x95ce,  0x95e2,  0x95f7,  0x960b,  0x9620,  0x9634,  0x9648,  0x965d,  0x9671,  0x9685,
+	0x969a,  0x96ae,  0x96c2,  0x96d7,  0x96eb,  0x96ff,  0x9713,  0x9728,  0x973c,  0x9750,
+	0x9765,  0x9779,  0x978d,  0x97a1,  0x97b6,  0x97ca,  0x97de,  0x97f2,  0x9807,  0x981b,
+	0x982f,  0x9843,  0x9857,  0x986c,  0x9880,  0x9894,  0x98a8,  0x98bc,  0x98d0,  0x98e5,
+	0x98f9,  0x990d,  0x9921,  0x9935,  0x9949,  0x995d,  0x9972,  0x9986,  0x999a,  0x99ae,
+	0x99c2,  0x99d6,  0x99ea,  0x99fe,  0x9a12,  0x9a26,  0x9a3a,  0x9a4f,  0x9a63,  0x9a77,
+	0x9a8b,  0x9a9f,  0x9ab3,  0x9ac7,  0x9adb,  0x9aef,  0x9b03,  0x9b17,  0x9b2b,  0x9b3f,
+	0x9b53,  0x9b67,  0x9b7b,  0x9b8f,  0x9ba3,  0x9bb7,  0x9bca,  0x9bde,  0x9bf2,  0x9c06,
+	0x9c1a,  0x9c2e,  0x9c42,  0x9c56,  0x9c6a,  0x9c7e,  0x9c92,  0x9ca6,  0x9cb9,  0x9ccd,
+	0x9ce1,  0x9cf5,  0x9d09,  0x9d1d,  0x9d31,  0x9d44,  0x9d58,  0x9d6c,  0x9d80,  0x9d94,
+	0x9da7,  0x9dbb,  0x9dcf,  0x9de3,  0x9df7,  0x9e0a,  0x9e1e,  0x9e32,  0x9e46,  0x9e59,
+	0x9e6d,  0x9e81,  0x9e95,  0x9ea8,  0x9ebc,  0x9ed0,  0x9ee3,  0x9ef7,  0x9f0b,  0x9f1f,
+	0x9f32,  0x9f46,  0x9f5a,  0x9f6d,  0x9f81,  0x9f95,  0x9fa8,  0x9fbc,  0x9fd0,  0x9fe3,
+	0x9ff7,  0xa00a,  0xa01e,  0xa032,  0xa045,  0xa059,  0xa06c,  0xa080,  0xa094,  0xa0a7,
+	0xa0bb,  0xa0ce,  0xa0e2,  0xa0f5,  0xa109,  0xa11c,  0xa130,  0xa143,  0xa157,  0xa16b,
+	0xa17e,  0xa192,  0xa1a5,  0xa1b9,  0xa1cc,  0xa1df,  0xa1f3,  0xa206,  0xa21a,  0xa22d,
+	0xa241,  0xa254,  0xa268,  0xa27b,  0xa28e,  0xa2a2,  0xa2b5,  0xa2c9,  0xa2dc,  0xa2ef,
+	0xa303,  0xa316,  0xa32a,  0xa33d,  0xa350,  0xa364,  0xa377,  0xa38a,  0xa39e,  0xa3b1,
+	0xa3c4,  0xa3d8,  0xa3eb,  0xa3fe,  0xa412,  0xa425,  0xa438,  0xa44b,  0xa45f,  0xa472,
+	0xa485,  0xa498,  0xa4ac,  0xa4bf,  0xa4d2,  0xa4e5,  0xa4f9,  0xa50c,  0xa51f,  0xa532,
+	0xa545,  0xa559,  0xa56c,  0xa57f,  0xa592,  0xa5a5,  0xa5b8,  0xa5cc,  0xa5df,  0xa5f2,
+	0xa605,  0xa618,  0xa62b,  0xa63e,  0xa652,  0xa665,  0xa678,  0xa68b,  0xa69e,  0xa6b1,
+	0xa6c4,  0xa6d7,  0xa6ea,  0xa6fd,  0xa710,  0xa723,  0xa736,  0xa749,  0xa75c,  0xa76f,
+	0xa782,  0xa795,  0xa7a8,  0xa7bb,  0xa7ce,  0xa7e1,  0xa7f4,  0xa807,  0xa81a,  0xa82d,
+	0xa840,  0xa853,  0xa866,  0xa879,  0xa88c,  0xa89f,  0xa8b2,  0xa8c5,  0xa8d7,  0xa8ea,
+	0xa8fd,  0xa910,  0xa923,  0xa936,  0xa949,  0xa95c,  0xa96e,  0xa981,  0xa994,  0xa9a7,
+	0xa9ba,  0xa9cd,  0xa9df,  0xa9f2,  0xaa05,  0xaa18,  0xaa2a,  0xaa3d,  0xaa50,  0xaa63,
+	0xaa76,  0xaa88,  0xaa9b,  0xaaae,  0xaac1,  0xaad3,  0xaae6,  0xaaf9,  0xab0b,  0xab1e,
+	0xab31,  0xab43,  0xab56,  0xab69,  0xab7b,  0xab8e,  0xaba1,  0xabb3,  0xabc6,  0xabd9,
+	0xabeb,  0xabfe,  0xac11,  0xac23,  0xac36,  0xac48,  0xac5b,  0xac6d,  0xac80,  0xac93,
+	0xaca5,  0xacb8,  0xacca,  0xacdd,  0xacef,  0xad02,  0xad14,  0xad27,  0xad39,  0xad4c,
+	0xad5e,  0xad71,  0xad83,  0xad96,  0xada8,  0xadbb,  0xadcd,  0xade0,  0xadf2,  0xae05,
+	0xae17,  0xae29,  0xae3c,  0xae4e,  0xae61,  0xae73,  0xae85,  0xae98,  0xaeaa,  0xaebd,
+	0xaecf,  0xaee1,  0xaef4,  0xaf06,  0xaf18,  0xaf2b,  0xaf3d,  0xaf4f,  0xaf62,  0xaf74,
+	0xaf86,  0xaf99,  0xafab,  0xafbd,  0xafcf,  0xafe2,  0xaff4,  0xb006,  0xb018,  0xb02b,
+	0xb03d,  0xb04f,  0xb061,  0xb074,  0xb086,  0xb098,  0xb0aa,  0xb0bc,  0xb0ce,  0xb0e1,
+	0xb0f3,  0xb105,  0xb117,  0xb129,  0xb13b,  0xb14e,  0xb160,  0xb172,  0xb184,  0xb196,
+	0xb1a8,  0xb1ba,  0xb1cc,  0xb1de,  0xb1f0,  0xb203,  0xb215,  0xb227,  0xb239,  0xb24b,
+	0xb25d,  0xb26f,  0xb281,  0xb293,  0xb2a5,  0xb2b7,  0xb2c9,  0xb2db,  0xb2ed,  0xb2ff,
+	0xb311,  0xb323,  0xb335,  0xb347,  0xb358,  0xb36a,  0xb37c,  0xb38e,  0xb3a0,  0xb3b2,
+	0xb3c4,  0xb3d6,  0xb3e8,  0xb3fa,  0xb40b,  0xb41d,  0xb42f,  0xb441,  0xb453,  0xb465,
+	0xb477,  0xb488,  0xb49a,  0xb4ac,  0xb4be,  0xb4d0,  0xb4e1,  0xb4f3,  0xb505,  0xb517,
+	0xb528,  0xb53a,  0xb54c,  0xb55e,  0xb56f,  0xb581,  0xb593,  0xb5a5,  0xb5b6,  0xb5c8,
+	0xb5da,  0xb5eb,  0xb5fd,  0xb60f,  0xb620,  0xb632,  0xb644,  0xb655,  0xb667,  0xb679,
+	0xb68a,  0xb69c,  0xb6ad,  0xb6bf,  0xb6d1,  0xb6e2,  0xb6f4,  0xb705,  0xb717,  0xb729,
+	0xb73a,  0xb74c,  0xb75d,  0xb76f,  0xb780,  0xb792,  0xb7a3,  0xb7b5,  0xb7c6,  0xb7d8,
+	0xb7e9,  0xb7fb,  0xb80c,  0xb81e,  0xb82f,  0xb841,  0xb852,  0xb864,  0xb875,  0xb886,
+	0xb898,  0xb8a9,  0xb8bb,  0xb8cc,  0xb8dd,  0xb8ef,  0xb900,  0xb912,  0xb923,  0xb934,
+	0xb946,  0xb957,  0xb968,  0xb97a,  0xb98b,  0xb99c,  0xb9ae,  0xb9bf,  0xb9d0,  0xb9e1,
+	0xb9f3,  0xba04,  0xba15,  0xba26,  0xba38,  0xba49,  0xba5a,  0xba6b,  0xba7d,  0xba8e,
+	0xba9f,  0xbab0,  0xbac1,  0xbad3,  0xbae4,  0xbaf5,  0xbb06,  0xbb17,  0xbb28,  0xbb3a,
+	0xbb4b,  0xbb5c,  0xbb6d,  0xbb7e,  0xbb8f,  0xbba0,  0xbbb1,  0xbbc3,  0xbbd4,  0xbbe5,
+	0xbbf6,  0xbc07,  0xbc18,  0xbc29,  0xbc3a,  0xbc4b,  0xbc5c,  0xbc6d,  0xbc7e,  0xbc8f,
+	0xbca0,  0xbcb1,  0xbcc2,  0xbcd3,  0xbce4,  0xbcf5,  0xbd06,  0xbd17,  0xbd28,  0xbd39,
+	0xbd4a,  0xbd5a,  0xbd6b,  0xbd7c,  0xbd8d,  0xbd9e,  0xbdaf,  0xbdc0,  0xbdd1,  0xbde2,
+	0xbdf2,  0xbe03,  0xbe14,  0xbe25,  0xbe36,  0xbe47,  0xbe57,  0xbe68,  0xbe79,  0xbe8a,
+	0xbe9b,  0xbeab,  0xbebc,  0xbecd,  0xbede,  0xbeee,  0xbeff,  0xbf10,  0xbf21,  0xbf31,
+	0xbf42,  0xbf53,  0xbf63,  0xbf74,  0xbf85,  0xbf95,  0xbfa6,  0xbfb7,  0xbfc7,  0xbfd8,
+	0xbfe9,  0xbff9,  0xc00a,  0xc01b,  0xc02b,  0xc03c,  0xc04c,  0xc05d,  0xc06e,  0xc07e,
+	0xc08f,  0xc09f,  0xc0b0,  0xc0c0,  0xc0d1,  0xc0e1,  0xc0f2,  0xc102,  0xc113,  0xc123,
+	0xc134,  0xc144,  0xc155,  0xc165,  0xc176,  0xc186,  0xc197,  0xc1a7,  0xc1b8,  0xc1c8,
+	0xc1d8,  0xc1e9,  0xc1f9,  0xc20a,  0xc21a,  0xc22a,  0xc23b,  0xc24b,  0xc25c,  0xc26c,
+	0xc27c,  0xc28d,  0xc29d,  0xc2ad,  0xc2be,  0xc2ce,  0xc2de,  0xc2ee,  0xc2ff,  0xc30f,
+	0xc31f,  0xc330,  0xc340,  0xc350,  0xc360,  0xc371,  0xc381,  0xc391,  0xc3a1,  0xc3b1,
+	0xc3c2,  0xc3d2,  0xc3e2,  0xc3f2,  0xc402,  0xc413,  0xc423,  0xc433,  0xc443,  0xc453,
+	0xc463,  0xc473,  0xc483,  0xc494,  0xc4a4,  0xc4b4,  0xc4c4,  0xc4d4,  0xc4e4,  0xc4f4,
+	0xc504,  0xc514,  0xc524,  0xc534,  0xc544,  0xc554,  0xc564,  0xc574,  0xc584,  0xc594,
+	0xc5a4,  0xc5b4,  0xc5c4,  0xc5d4,  0xc5e4,  0xc5f4,  0xc604,  0xc614,  0xc624,  0xc634,
+	0xc644,  0xc653,  0xc663,  0xc673,  0xc683,  0xc693,  0xc6a3,  0xc6b3,  0xc6c2,  0xc6d2,
+	0xc6e2,  0xc6f2,  0xc702,  0xc712,  0xc721,  0xc731,  0xc741,  0xc751,  0xc761,  0xc770,
+	0xc780,  0xc790,  0xc7a0,  0xc7af,  0xc7bf,  0xc7cf,  0xc7de,  0xc7ee,  0xc7fe,  0xc80d,
+	0xc81d,  0xc82d,  0xc83c,  0xc84c,  0xc85c,  0xc86b,  0xc87b,  0xc88b,  0xc89a,  0xc8aa,
+	0xc8ba,  0xc8c9,  0xc8d9,  0xc8e8,  0xc8f8,  0xc907,  0xc917,  0xc927,  0xc936,  0xc946,
+	0xc955,  0xc965,  0xc974,  0xc984,  0xc993,  0xc9a3,  0xc9b2,  0xc9c2,  0xc9d1,  0xc9e1,
+	0xc9f0,  0xc9ff,  0xca0f,  0xca1e,  0xca2e,  0xca3d,  0xca4d,  0xca5c,  0xca6b,  0xca7b,
+	0xca8a,  0xca99,  0xcaa9,  0xcab8,  0xcac7,  0xcad7,  0xcae6,  0xcaf5,  0xcb05,  0xcb14,
+	0xcb23,  0xcb33,  0xcb42,  0xcb51,  0xcb61,  0xcb70,  0xcb7f,  0xcb8e,  0xcb9e,  0xcbad,
+	0xcbbc,  0xcbcb,  0xcbda,  0xcbea,  0xcbf9,  0xcc08,  0xcc17,  0xcc26,  0xcc35,  0xcc45,
+	0xcc54,  0xcc63,  0xcc72,  0xcc81,  0xcc90,  0xcc9f,  0xccae,  0xccbe,  0xcccd,  0xccdc,
+	0xcceb,  0xccfa,  0xcd09,  0xcd18,  0xcd27,  0xcd36,  0xcd45,  0xcd54,  0xcd63,  0xcd72,
+	0xcd81,  0xcd90,  0xcd9f,  0xcdae,  0xcdbd,  0xcdcc,  0xcddb,  0xcdea,  0xcdf9,  0xce08,
+	0xce17,  0xce25,  0xce34,  0xce43,  0xce52,  0xce61,  0xce70,  0xce7f,  0xce8e,  0xce9c,
+	0xceab,  0xceba,  0xcec9,  0xced8,  0xcee7,  0xcef5,  0xcf04,  0xcf13,  0xcf22,  0xcf30,
+	0xcf3f,  0xcf4e,  0xcf5d,  0xcf6b,  0xcf7a,  0xcf89,  0xcf98,  0xcfa6,  0xcfb5,  0xcfc4,
+	0xcfd2,  0xcfe1,  0xcff0,  0xcffe,  0xd00d,  0xd01c,  0xd02a,  0xd039,  0xd047,  0xd056,
+	0xd065,  0xd073,  0xd082,  0xd090,  0xd09f,  0xd0ae,  0xd0bc,  0xd0cb,  0xd0d9,  0xd0e8,
+	0xd0f6,  0xd105,  0xd113,  0xd122,  0xd130,  0xd13f,  0xd14d,  0xd15c,  0xd16a,  0xd179,
+	0xd187,  0xd195,  0xd1a4,  0xd1b2,  0xd1c1,  0xd1cf,  0xd1de,  0xd1ec,  0xd1fa,  0xd209,
+	0xd217,  0xd225,  0xd234,  0xd242,  0xd250,  0xd25f,  0xd26d,  0xd27b,  0xd28a,  0xd298,
+	0xd2a6,  0xd2b5,  0xd2c3,  0xd2d1,  0xd2df,  0xd2ee,  0xd2fc,  0xd30a,  0xd318,  0xd326,
+	0xd335,  0xd343,  0xd351,  0xd35f,  0xd36d,  0xd37c,  0xd38a,  0xd398,  0xd3a6,  0xd3b4,
+	0xd3c2,  0xd3d0,  0xd3df,  0xd3ed,  0xd3fb,  0xd409,  0xd417,  0xd425,  0xd433,  0xd441,
+	0xd44f,  0xd45d,  0xd46b,  0xd479,  0xd487,  0xd495,  0xd4a3,  0xd4b1,  0xd4bf,  0xd4cd,
+	0xd4db,  0xd4e9,  0xd4f7,  0xd505,  0xd513,  0xd521,  0xd52f,  0xd53d,  0xd54b,  0xd559,
+	0xd566,  0xd574,  0xd582,  0xd590,  0xd59e,  0xd5ac,  0xd5ba,  0xd5c7,  0xd5d5,  0xd5e3,
+	0xd5f1,  0xd5ff,  0xd60c,  0xd61a,  0xd628,  0xd636,  0xd644,  0xd651,  0xd65f,  0xd66d,
+	0xd67a,  0xd688,  0xd696,  0xd6a4,  0xd6b1,  0xd6bf,  0xd6cd,  0xd6da,  0xd6e8,  0xd6f6,
+	0xd703,  0xd711,  0xd71f,  0xd72c,  0xd73a,  0xd747,  0xd755,  0xd763,  0xd770,  0xd77e,
+	0xd78b,  0xd799,  0xd7a6,  0xd7b4,  0xd7c1,  0xd7cf,  0xd7dc,  0xd7ea,  0xd7f8,  0xd805,
+	0xd812,  0xd820,  0xd82d,  0xd83b,  0xd848,  0xd856,  0xd863,  0xd871,  0xd87e,  0xd88b,
+	0xd899,  0xd8a6,  0xd8b4,  0xd8c1,  0xd8ce,  0xd8dc,  0xd8e9,  0xd8f6,  0xd904,  0xd911,
+	0xd91e,  0xd92c,  0xd939,  0xd946,  0xd954,  0xd961,  0xd96e,  0xd97b,  0xd989,  0xd996,
+	0xd9a3,  0xd9b0,  0xd9be,  0xd9cb,  0xd9d8,  0xd9e5,  0xd9f2,  0xda00,  0xda0d,  0xda1a,
+	0xda27,  0xda34,  0xda41,  0xda4f,  0xda5c,  0xda69,  0xda76,  0xda83,  0xda90,  0xda9d,
+	0xdaaa,  0xdab7,  0xdac4,  0xdad1,  0xdade,  0xdaeb,  0xdaf8,  0xdb05,  0xdb12,  0xdb1f,
+	0xdb2c,  0xdb39,  0xdb46,  0xdb53,  0xdb60,  0xdb6d,  0xdb7a,  0xdb87,  0xdb94,  0xdba1,
+	0xdbae,  0xdbbb,  0xdbc8,  0xdbd5,  0xdbe1,  0xdbee,  0xdbfb,  0xdc08,  0xdc15,  0xdc22,
+	0xdc2f,  0xdc3b,  0xdc48,  0xdc55,  0xdc62,  0xdc6f,  0xdc7b,  0xdc88,  0xdc95,  0xdca2,
+	0xdcae,  0xdcbb,  0xdcc8,  0xdcd5,  0xdce1,  0xdcee,  0xdcfb,  0xdd07,  0xdd14,  0xdd21,
+	0xdd2d,  0xdd3a,  0xdd47,  0xdd53,  0xdd60,  0xdd6c,  0xdd79,  0xdd86,  0xdd92,  0xdd9f,
+	0xddab,  0xddb8,  0xddc5,  0xddd1,  0xddde,  0xddea,  0xddf7,  0xde03,  0xde10,  0xde1c,
+	0xde29,  0xde35,  0xde42,  0xde4e,  0xde5b,  0xde67,  0xde74,  0xde80,  0xde8c,  0xde99,
+	0xdea5,  0xdeb2,  0xdebe,  0xdeca,  0xded7,  0xdee3,  0xdef0,  0xdefc,  0xdf08,  0xdf15,
+	0xdf21,  0xdf2d,  0xdf39,  0xdf46,  0xdf52,  0xdf5e,  0xdf6b,  0xdf77,  0xdf83,  0xdf8f,
+	0xdf9c,  0xdfa8,  0xdfb4,  0xdfc0,  0xdfcd,  0xdfd9,  0xdfe5,  0xdff1,  0xdffd,  0xe009,
+	0xe016,  0xe022,  0xe02e,  0xe03a,  0xe046,  0xe052,  0xe05e,  0xe06a,  0xe077,  0xe083,
+	0xe08f,  0xe09b,  0xe0a7,  0xe0b3,  0xe0bf,  0xe0cb,  0xe0d7,  0xe0e3,  0xe0ef,  0xe0fb,
+	0xe107,  0xe113,  0xe11f,  0xe12b,  0xe137,  0xe143,  0xe14f,  0xe15b,  0xe167,  0xe172,
+	0xe17e,  0xe18a,  0xe196,  0xe1a2,  0xe1ae,  0xe1ba,  0xe1c6,  0xe1d1,  0xe1dd,  0xe1e9,
+	0xe1f5,  0xe201,  0xe20d,  0xe218,  0xe224,  0xe230,  0xe23c,  0xe247,  0xe253,  0xe25f,
+	0xe26b,  0xe276,  0xe282,  0xe28e,  0xe299,  0xe2a5,  0xe2b1,  0xe2bd,  0xe2c8,  0xe2d4,
+	0xe2df,  0xe2eb,  0xe2f7,  0xe302,  0xe30e,  0xe31a,  0xe325,  0xe331,  0xe33c,  0xe348,
+	0xe353,  0xe35f,  0xe36b,  0xe376,  0xe382,  0xe38d,  0xe399,  0xe3a4,  0xe3b0,  0xe3bb,
+	0xe3c7,  0xe3d2,  0xe3de,  0xe3e9,  0xe3f4,  0xe400,  0xe40b,  0xe417,  0xe422,  0xe42e,
+	0xe439,  0xe444,  0xe450,  0xe45b,  0xe466,  0xe472,  0xe47d,  0xe488,  0xe494,  0xe49f,
+	0xe4aa,  0xe4b6,  0xe4c1,  0xe4cc,  0xe4d7,  0xe4e3,  0xe4ee,  0xe4f9,  0xe504,  0xe510,
+	0xe51b,  0xe526,  0xe531,  0xe53d,  0xe548,  0xe553,  0xe55e,  0xe569,  0xe574,  0xe57f,
+	0xe58b,  0xe596,  0xe5a1,  0xe5ac,  0xe5b7,  0xe5c2,  0xe5cd,  0xe5d8,  0xe5e3,  0xe5ee,
+	0xe5f9,  0xe605,  0xe610,  0xe61b,  0xe626,  0xe631,  0xe63c,  0xe647,  0xe652,  0xe65c,
+	0xe667,  0xe672,  0xe67d,  0xe688,  0xe693,  0xe69e,  0xe6a9,  0xe6b4,  0xe6bf,  0xe6ca,
+	0xe6d5,  0xe6df,  0xe6ea,  0xe6f5,  0xe700,  0xe70b,  0xe716,  0xe720,  0xe72b,  0xe736,
+	0xe741,  0xe74c,  0xe756,  0xe761,  0xe76c,  0xe777,  0xe781,  0xe78c,  0xe797,  0xe7a1,
+	0xe7ac,  0xe7b7,  0xe7c2,  0xe7cc,  0xe7d7,  0xe7e2,  0xe7ec,  0xe7f7,  0xe801,  0xe80c,
+	0xe817,  0xe821,  0xe82c,  0xe836,  0xe841,  0xe84c,  0xe856,  0xe861,  0xe86b,  0xe876,
+	0xe880,  0xe88b,  0xe895,  0xe8a0,  0xe8aa,  0xe8b5,  0xe8bf,  0xe8ca,  0xe8d4,  0xe8df,
+	0xe8e9,  0xe8f3,  0xe8fe,  0xe908,  0xe913,  0xe91d,  0xe927,  0xe932,  0xe93c,  0xe947,
+	0xe951,  0xe95b,  0xe966,  0xe970,  0xe97a,  0xe985,  0xe98f,  0xe999,  0xe9a3,  0xe9ae,
+	0xe9b8,  0xe9c2,  0xe9cc,  0xe9d7,  0xe9e1,  0xe9eb,  0xe9f5,  0xe9ff,  0xea0a,  0xea14,
+	0xea1e,  0xea28,  0xea32,  0xea3c,  0xea47,  0xea51,  0xea5b,  0xea65,  0xea6f,  0xea79,
+	0xea83,  0xea8d,  0xea97,  0xeaa1,  0xeaab,  0xeab6,  0xeac0,  0xeaca,  0xead4,  0xeade,
+	0xeae8,  0xeaf2,  0xeafc,  0xeb06,  0xeb0f,  0xeb19,  0xeb23,  0xeb2d,  0xeb37,  0xeb41,
+	0xeb4b,  0xeb55,  0xeb5f,  0xeb69,  0xeb73,  0xeb7c,  0xeb86,  0xeb90,  0xeb9a,  0xeba4,
+	0xebae,  0xebb7,  0xebc1,  0xebcb,  0xebd5,  0xebdf,  0xebe8,  0xebf2,  0xebfc,  0xec06,
+	0xec0f,  0xec19,  0xec23,  0xec2c,  0xec36,  0xec40,  0xec4a,  0xec53,  0xec5d,  0xec66,
+	0xec70,  0xec7a,  0xec83,  0xec8d,  0xec97,  0xeca0,  0xecaa,  0xecb3,  0xecbd,  0xecc6,
+	0xecd0,  0xecda,  0xece3,  0xeced,  0xecf6,  0xed00,  0xed09,  0xed13,  0xed1c,  0xed26,
+	0xed2f,  0xed38,  0xed42,  0xed4b,  0xed55,  0xed5e,  0xed68,  0xed71,  0xed7a,  0xed84,
+	0xed8d,  0xed97,  0xeda0,  0xeda9,  0xedb3,  0xedbc,  0xedc5,  0xedcf,  0xedd8,  0xede1,
+	0xedea,  0xedf4,  0xedfd,  0xee06,  0xee0f,  0xee19,  0xee22,  0xee2b,  0xee34,  0xee3e,
+	0xee47,  0xee50,  0xee59,  0xee62,  0xee6b,  0xee75,  0xee7e,  0xee87,  0xee90,  0xee99,
+	0xeea2,  0xeeab,  0xeeb4,  0xeebd,  0xeec7,  0xeed0,  0xeed9,  0xeee2,  0xeeeb,  0xeef4,
+	0xeefd,  0xef06,  0xef0f,  0xef18,  0xef21,  0xef2a,  0xef33,  0xef3c,  0xef45,  0xef4d,
+	0xef56,  0xef5f,  0xef68,  0xef71,  0xef7a,  0xef83,  0xef8c,  0xef95,  0xef9d,  0xefa6,
+	0xefaf,  0xefb8,  0xefc1,  0xefca,  0xefd2,  0xefdb,  0xefe4,  0xefed,  0xeff5,  0xeffe,
+	0xf007,  0xf010,  0xf018,  0xf021,  0xf02a,  0xf033,  0xf03b,  0xf044,  0xf04d,  0xf055,
+	0xf05e,  0xf067,  0xf06f,  0xf078,  0xf080,  0xf089,  0xf092,  0xf09a,  0xf0a3,  0xf0ab,
+	0xf0b4,  0xf0bc,  0xf0c5,  0xf0ce,  0xf0d6,  0xf0df,  0xf0e7,  0xf0f0,  0xf0f8,  0xf101,
+	0xf109,  0xf111,  0xf11a,  0xf122,  0xf12b,  0xf133,  0xf13c,  0xf144,  0xf14c,  0xf155,
+	0xf15d,  0xf166,  0xf16e,  0xf176,  0xf17f,  0xf187,  0xf18f,  0xf198,  0xf1a0,  0xf1a8,
+	0xf1b1,  0xf1b9,  0xf1c1,  0xf1c9,  0xf1d2,  0xf1da,  0xf1e2,  0xf1ea,  0xf1f3,  0xf1fb,
+	0xf203,  0xf20b,  0xf213,  0xf21b,  0xf224,  0xf22c,  0xf234,  0xf23c,  0xf244,  0xf24c,
+	0xf254,  0xf25d,  0xf265,  0xf26d,  0xf275,  0xf27d,  0xf285,  0xf28d,  0xf295,  0xf29d,
+	0xf2a5,  0xf2ad,  0xf2b5,  0xf2bd,  0xf2c5,  0xf2cd,  0xf2d5,  0xf2dd,  0xf2e5,  0xf2ed,
+	0xf2f5,  0xf2fd,  0xf304,  0xf30c,  0xf314,  0xf31c,  0xf324,  0xf32c,  0xf334,  0xf33c,
+	0xf343,  0xf34b,  0xf353,  0xf35b,  0xf363,  0xf36a,  0xf372,  0xf37a,  0xf382,  0xf38a,
+	0xf391,  0xf399,  0xf3a1,  0xf3a8,  0xf3b0,  0xf3b8,  0xf3c0,  0xf3c7,  0xf3cf,  0xf3d7,
+	0xf3de,  0xf3e6,  0xf3ed,  0xf3f5,  0xf3fd,  0xf404,  0xf40c,  0xf413,  0xf41b,  0xf423,
+	0xf42a,  0xf432,  0xf439,  0xf441,  0xf448,  0xf450,  0xf457,  0xf45f,  0xf466,  0xf46e,
+	0xf475,  0xf47d,  0xf484,  0xf48c,  0xf493,  0xf49a,  0xf4a2,  0xf4a9,  0xf4b1,  0xf4b8,
+	0xf4bf,  0xf4c7,  0xf4ce,  0xf4d5,  0xf4dd,  0xf4e4,  0xf4eb,  0xf4f3,  0xf4fa,  0xf501,
+	0xf509,  0xf510,  0xf517,  0xf51e,  0xf526,  0xf52d,  0xf534,  0xf53b,  0xf543,  0xf54a,
+	0xf551,  0xf558,  0xf55f,  0xf566,  0xf56e,  0xf575,  0xf57c,  0xf583,  0xf58a,  0xf591,
+	0xf598,  0xf59f,  0xf5a6,  0xf5ae,  0xf5b5,  0xf5bc,  0xf5c3,  0xf5ca,  0xf5d1,  0xf5d8,
+	0xf5df,  0xf5e6,  0xf5ed,  0xf5f4,  0xf5fb,  0xf602,  0xf609,  0xf610,  0xf616,  0xf61d,
+	0xf624,  0xf62b,  0xf632,  0xf639,  0xf640,  0xf647,  0xf64e,  0xf654,  0xf65b,  0xf662,
+	0xf669,  0xf670,  0xf677,  0xf67d,  0xf684,  0xf68b,  0xf692,  0xf698,  0xf69f,  0xf6a6,
+	0xf6ad,  0xf6b3,  0xf6ba,  0xf6c1,  0xf6c7,  0xf6ce,  0xf6d5,  0xf6db,  0xf6e2,  0xf6e9,
+	0xf6ef,  0xf6f6,  0xf6fd,  0xf703,  0xf70a,  0xf710,  0xf717,  0xf71e,  0xf724,  0xf72b,
+	0xf731,  0xf738,  0xf73e,  0xf745,  0xf74b,  0xf752,  0xf758,  0xf75f,  0xf765,  0xf76c,
+	0xf772,  0xf779,  0xf77f,  0xf785,  0xf78c,  0xf792,  0xf799,  0xf79f,  0xf7a5,  0xf7ac,
+	0xf7b2,  0xf7b8,  0xf7bf,  0xf7c5,  0xf7cb,  0xf7d2,  0xf7d8,  0xf7de,  0xf7e5,  0xf7eb,
+	0xf7f1,  0xf7f7,  0xf7fe,  0xf804,  0xf80a,  0xf810,  0xf816,  0xf81d,  0xf823,  0xf829,
+	0xf82f,  0xf835,  0xf83b,  0xf842,  0xf848,  0xf84e,  0xf854,  0xf85a,  0xf860,  0xf866,
+	0xf86c,  0xf872,  0xf878,  0xf87e,  0xf885,  0xf88b,  0xf891,  0xf897,  0xf89d,  0xf8a3,
+	0xf8a9,  0xf8af,  0xf8b4,  0xf8ba,  0xf8c0,  0xf8c6,  0xf8cc,  0xf8d2,  0xf8d8,  0xf8de,
+	0xf8e4,  0xf8ea,  0xf8f0,  0xf8f5,  0xf8fb,  0xf901,  0xf907,  0xf90d,  0xf913,  0xf918,
+	0xf91e,  0xf924,  0xf92a,  0xf930,  0xf935,  0xf93b,  0xf941,  0xf946,  0xf94c,  0xf952,
+	0xf958,  0xf95d,  0xf963,  0xf969,  0xf96e,  0xf974,  0xf97a,  0xf97f,  0xf985,  0xf98a,
+	0xf990,  0xf996,  0xf99b,  0xf9a1,  0xf9a6,  0xf9ac,  0xf9b2,  0xf9b7,  0xf9bd,  0xf9c2,
+	0xf9c8,  0xf9cd,  0xf9d3,  0xf9d8,  0xf9de,  0xf9e3,  0xf9e8,  0xf9ee,  0xf9f3,  0xf9f9,
+	0xf9fe,  0xfa04,  0xfa09,  0xfa0e,  0xfa14,  0xfa19,  0xfa1f,  0xfa24,  0xfa29,  0xfa2f,
+	0xfa34,  0xfa39,  0xfa3e,  0xfa44,  0xfa49,  0xfa4e,  0xfa54,  0xfa59,  0xfa5e,  0xfa63,
+	0xfa69,  0xfa6e,  0xfa73,  0xfa78,  0xfa7d,  0xfa83,  0xfa88,  0xfa8d,  0xfa92,  0xfa97,
+	0xfa9c,  0xfaa1,  0xfaa7,  0xfaac,  0xfab1,  0xfab6,  0xfabb,  0xfac0,  0xfac5,  0xfaca,
+	0xfacf,  0xfad4,  0xfad9,  0xfade,  0xfae3,  0xfae8,  0xfaed,  0xfaf2,  0xfaf7,  0xfafc,
+	0xfb01,  0xfb06,  0xfb0b,  0xfb10,  0xfb15,  0xfb1a,  0xfb1f,  0xfb23,  0xfb28,  0xfb2d,
+	0xfb32,  0xfb37,  0xfb3c,  0xfb40,  0xfb45,  0xfb4a,  0xfb4f,  0xfb54,  0xfb58,  0xfb5d,
+	0xfb62,  0xfb67,  0xfb6b,  0xfb70,  0xfb75,  0xfb7a,  0xfb7e,  0xfb83,  0xfb88,  0xfb8c,
+	0xfb91,  0xfb96,  0xfb9a,  0xfb9f,  0xfba4,  0xfba8,  0xfbad,  0xfbb1,  0xfbb6,  0xfbbb,
+	0xfbbf,  0xfbc4,  0xfbc8,  0xfbcd,  0xfbd1,  0xfbd6,  0xfbda,  0xfbdf,  0xfbe3,  0xfbe8,
+	0xfbec,  0xfbf1,  0xfbf5,  0xfbfa,  0xfbfe,  0xfc02,  0xfc07,  0xfc0b,  0xfc10,  0xfc14,
+	0xfc18,  0xfc1d,  0xfc21,  0xfc26,  0xfc2a,  0xfc2e,  0xfc33,  0xfc37,  0xfc3b,  0xfc3f,
+	0xfc44,  0xfc48,  0xfc4c,  0xfc51,  0xfc55,  0xfc59,  0xfc5d,  0xfc61,  0xfc66,  0xfc6a,
+	0xfc6e,  0xfc72,  0xfc76,  0xfc7b,  0xfc7f,  0xfc83,  0xfc87,  0xfc8b,  0xfc8f,  0xfc93,
+	0xfc97,  0xfc9b,  0xfca0,  0xfca4,  0xfca8,  0xfcac,  0xfcb0,  0xfcb4,  0xfcb8,  0xfcbc,
+	0xfcc0,  0xfcc4,  0xfcc8,  0xfccc,  0xfcd0,  0xfcd4,  0xfcd8,  0xfcdc,  0xfcdf,  0xfce3,
+	0xfce7,  0xfceb,  0xfcef,  0xfcf3,  0xfcf7,  0xfcfb,  0xfcfe,  0xfd02,  0xfd06,  0xfd0a,
+	0xfd0e,  0xfd12,  0xfd15,  0xfd19,  0xfd1d,  0xfd21,  0xfd24,  0xfd28,  0xfd2c,  0xfd30,
+	0xfd33,  0xfd37,  0xfd3b,  0xfd3e,  0xfd42,  0xfd46,  0xfd49,  0xfd4d,  0xfd51,  0xfd54,
+	0xfd58,  0xfd5b,  0xfd5f,  0xfd63,  0xfd66,  0xfd6a,  0xfd6d,  0xfd71,  0xfd74,  0xfd78,
+	0xfd7c,  0xfd7f,  0xfd83,  0xfd86,  0xfd89,  0xfd8d,  0xfd90,  0xfd94,  0xfd97,  0xfd9b,
+	0xfd9e,  0xfda2,  0xfda5,  0xfda8,  0xfdac,  0xfdaf,  0xfdb3,  0xfdb6,  0xfdb9,  0xfdbd,
+	0xfdc0,  0xfdc3,  0xfdc7,  0xfdca,  0xfdcd,  0xfdd0,  0xfdd4,  0xfdd7,  0xfdda,  0xfddd,
+	0xfde1,  0xfde4,  0xfde7,  0xfdea,  0xfdee,  0xfdf1,  0xfdf4,  0xfdf7,  0xfdfa,  0xfdfd,
+	0xfe01,  0xfe04,  0xfe07,  0xfe0a,  0xfe0d,  0xfe10,  0xfe13,  0xfe16,  0xfe19,  0xfe1c,
+	0xfe1f,  0xfe22,  0xfe25,  0xfe28,  0xfe2b,  0xfe2e,  0xfe31,  0xfe34,  0xfe37,  0xfe3a,
+	0xfe3d,  0xfe40,  0xfe43,  0xfe46,  0xfe49,  0xfe4c,  0xfe4f,  0xfe52,  0xfe55,  0xfe57,
+	0xfe5a,  0xfe5d,  0xfe60,  0xfe63,  0xfe66,  0xfe68,  0xfe6b,  0xfe6e,  0xfe71,  0xfe73,
+	0xfe76,  0xfe79,  0xfe7c,  0xfe7e,  0xfe81,  0xfe84,  0xfe87,  0xfe89,  0xfe8c,  0xfe8f,
+	0xfe91,  0xfe94,  0xfe97,  0xfe99,  0xfe9c,  0xfe9e,  0xfea1,  0xfea4,  0xfea6,  0xfea9,
+	0xfeab,  0xfeae,  0xfeb0,  0xfeb3,  0xfeb5,  0xfeb8,  0xfeba,  0xfebd,  0xfebf,  0xfec2,
+	0xfec4,  0xfec7,  0xfec9,  0xfecc,  0xfece,  0xfed1,  0xfed3,  0xfed5,  0xfed8,  0xfeda,
+	0xfedd,  0xfedf,  0xfee1,  0xfee4,  0xfee6,  0xfee8,  0xfeeb,  0xfeed,  0xfeef,  0xfef1,
+	0xfef4,  0xfef6,  0xfef8,  0xfefb,  0xfefd,  0xfeff,  0xff01,  0xff03,  0xff06,  0xff08,
+	0xff0a,  0xff0c,  0xff0e,  0xff10,  0xff13,  0xff15,  0xff17,  0xff19,  0xff1b,  0xff1d,
+	0xff1f,  0xff21,  0xff23,  0xff25,  0xff28,  0xff2a,  0xff2c,  0xff2e,  0xff30,  0xff32,
+	0xff34,  0xff36,  0xff38,  0xff3a,  0xff3b,  0xff3d,  0xff3f,  0xff41,  0xff43,  0xff45,
+	0xff47,  0xff49,  0xff4b,  0xff4d,  0xff4e,  0xff50,  0xff52,  0xff54,  0xff56,  0xff58,
+	0xff59,  0xff5b,  0xff5d,  0xff5f,  0xff60,  0xff62,  0xff64,  0xff66,  0xff67,  0xff69,
+	0xff6b,  0xff6c,  0xff6e,  0xff70,  0xff71,  0xff73,  0xff75,  0xff76,  0xff78,  0xff7a,
+	0xff7b,  0xff7d,  0xff7e,  0xff80,  0xff82,  0xff83,  0xff85,  0xff86,  0xff88,  0xff89,
+	0xff8b,  0xff8c,  0xff8e,  0xff8f,  0xff91,  0xff92,  0xff94,  0xff95,  0xff96,  0xff98,
+	0xff99,  0xff9b,  0xff9c,  0xff9d,  0xff9f,  0xffa0,  0xffa2,  0xffa3,  0xffa4,  0xffa6,
+	0xffa7,  0xffa8,  0xffa9,  0xffab,  0xffac,  0xffad,  0xffaf,  0xffb0,  0xffb1,  0xffb2,
+	0xffb4,  0xffb5,  0xffb6,  0xffb7,  0xffb8,  0xffb9,  0xffbb,  0xffbc,  0xffbd,  0xffbe,
+	0xffbf,  0xffc0,  0xffc1,  0xffc2,  0xffc4,  0xffc5,  0xffc6,  0xffc7,  0xffc8,  0xffc9,
+	0xffca,  0xffcb,  0xffcc,  0xffcd,  0xffce,  0xffcf,  0xffd0,  0xffd1,  0xffd2,  0xffd3,
+	0xffd4,  0xffd5,  0xffd5,  0xffd6,  0xffd7,  0xffd8,  0xffd9,  0xffda,  0xffdb,  0xffdc,
+	0xffdc,  0xffdd,  0xffde,  0xffdf,  0xffe0,  0xffe0,  0xffe1,  0xffe2,  0xffe3,  0xffe3,
+	0xffe4,  0xffe5,  0xffe6,  0xffe6,  0xffe7,  0xffe8,  0xffe8,  0xffe9,  0xffea,  0xffea,
+	0xffeb,  0xffec,  0xffec,  0xffed,  0xffed,  0xffee,  0xffef,  0xffef,  0xfff0,  0xfff0,
+	0xfff1,  0xfff1,  0xfff2,  0xfff2,  0xfff3,  0xfff3,  0xfff4,  0xfff4,  0xfff5,  0xfff5,
+	0xfff6,  0xfff6,  0xfff7,  0xfff7,  0xfff7,  0xfff8,  0xfff8,  0xfff9,  0xfff9,  0xfff9,
+	0xfffa,  0xfffa,  0xfffa,  0xfffb,  0xfffb,  0xfffb,  0xfffc,  0xfffc,  0xfffc,  0xfffc,
+	0xfffd,  0xfffd,  0xfffd,  0xfffd,  0xfffe,  0xfffe,  0xfffe,  0xfffe,  0xfffe,  0xffff,
+	0xffff,  0xffff,  0xffff,  0xffff,  0xffff,  0xffff,  0xffff,  0xffff,  0xffff,  0xffff,
+	0xffff,  0x0000,  0x0000,  0x0000,  0x0000,  0x0000};
+/* in actual sine / cosine functions, the last 5
+ * values will be added with a int '1' */
+
+/* FIXME: The coefficients could use a bit of cleanup */
+/*----------------------------------------------------------------------
+ * Function: ovl_util_sine()
+ * Description: Helper function for filter coefficient programming
+ *
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+static int ovl_util_sine(int radiant_input)
+{
+
+	/*
+	 * the radiant input parameter is a 32 signed number where the LSB
+	 * 16 bits are the FP portion
+	 */
+
+	short sign = 0; /* 0 positive for and 1 for negative */
+	int pi_half_count = 0;
+	int pi_half = 0;
+	int balance = 0;
+	int answer = 0;
+
+	/* lets rule that the input is a signed integer in which:
+	 * the first 16 bits represent the integer
+	 * the last 16 bits represent the floating point portion */
+
+	/* first we have to normalize it to a positive number.*/
+	if(radiant_input<0)
+	{
+		sign = 1;
+		radiant_input = -radiant_input;
+	}
+
+	/* let's get pi/2 variable with 16 LSB FP just like this input data.*/
+	pi_half = 0x3243f; /* = 3.141586 */
+	pi_half += 0x1; /* for rounding off */
+	pi_half = pi_half >> 1; /* divided by 2 */
+
+
+	/* now we have to count how many pi/2 there are in this value
+	 * (since pi/2 = 90' = one quarter sine wave from 0 to 1) */
+	balance = radiant_input;
+	while(balance >= pi_half)
+	{
+		balance = balance - pi_half;
+		++pi_half_count;
+	}
+
+	/* using the pi_half_count, we can figure out how many 90 degrees.
+	 * thru the sine wave we are looking at */
+	pi_half_count = pi_half_count%4;
+
+	/*
+	 * balance is something between '0' and 'pi/2' - exactly what we need
+	 * to use in the table however, this is 16 bits shifted and our table
+	 * is a max of 12 bits - lets not forget to adjust but first we have to
+	 * use the balance according to which 90' of the sine wave we are in
+	 */
+
+	/* now we get the actual sine value of excess that's less than a
+	 * quarter pi if quart_count = 0, then answer is direct from table; */
+	if(pi_half_count == 0){
+
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+		balance = balance << 13;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+		balance = balance / pi_half;
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+	}
+
+	/* if quart_count = 1, then formula is =
+	 * answer = sin[(pi/2) - balance)] */
+	else if(pi_half_count == 1){
+		balance = pi_half - balance;
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+	}
+
+	/*
+	 * if quart_count = 2, then formula is = answer = the negative of the
+	 * case it being 0;
+	 */
+	else if(pi_half_count == 2){
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+		answer = 0 - answer;
+	}
+
+	/*
+	 * if quart_count = 3, then formula is = answer = the negative of the
+	 * case it being 1;
+	 */
+	else if(pi_half_count == 3){
+		balance = pi_half - balance;
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+		answer = 0 - answer;
+	}
+
+	if(sign==1) {
+		answer = 0-answer;
+	}
+
+	return answer;
+	/*
+	 * so return value is a signed short where 16 MSBits are integer and
+	 * 16 LSBits are FP
+	 */
+}
+
+/*----------------------------------------------------------------------
+ * Function: ovl_util_cosine()
+ * Description: Helper function for filter coefficient programming
+ *
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+static int ovl_util_cosine(int radiant_input)
+{
+	/*
+	 * the radiant input parameter is a 32 signed number where the LSB
+	 * 16 bits are the FP portion
+	 */
+
+	short sign = 0; /* 0 positive for and 1 for negative */
+	int pi_half_count = 0;
+	int pi_half = 0;
+	int balance = 0;
+	int answer = 0;
+
+	/* lets rule that the input is a signed integer in which:
+	 * the first 16 bits represent the integer
+	 * the last 16 bits represent the floating point portion */
+
+	/* first we have to normalize it to a positive number.*/
+	if(radiant_input<0) {
+		sign = 1;
+		radiant_input = -radiant_input;
+	}
+
+	/* let's get pi/2 variable with 16 LSB FP just like this input data.*/
+	pi_half = 0x3243f; /* = 3.141586 */
+	pi_half += 0x1; /* for rounding off */
+	pi_half = pi_half >> 1; /* divided by 2 */
+
+
+	/* now we have to count how many pi/2 there are in this value
+	 * (since pi/2 = 90' = one quarter sine wave from 0 to 1) */
+	balance = radiant_input;
+	while(balance >= pi_half) {
+		balance = balance - pi_half;
+		++pi_half_count;
+	}
+
+	/* using the pi_half_count, we can figure out how many 90 degrees.
+	 * thru the sine wave we are looking at */
+	pi_half_count = pi_half_count%4;
+
+	/*
+	 * balance is something between '0' and 'pi/2' - exactly what we need
+	 * to use in the table however, this is 16 bits shifted and our table
+	 * is a max of 12 bits - lets not forget to adjust but first we have
+	 * to use the balance according to which 90' of the sine wave we are in
+	 */
+
+	/* now we get the actual sine value of excess
+	 * that's less than a quarter pi
+	 * if quart_count = 0, then formula is = sine quarter # 1
+	 * then formula is = answer = sin[(pi/2) - balance)] */
+	if(pi_half_count == 0) {
+		balance = pi_half - balance;
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+	}
+
+	/* if quart_count = 1, then formula is = sine quarter # 2
+	 * then formula is = answer = the negative of the case it being 0; */
+	else if(pi_half_count == 1){
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+
+		answer = 0 - answer;
+	}
+
+	/*if quart_count = 2, then formula is = sine quarter # 3
+	 * then formula is = answer = the negative of the case it being 1; */
+	else if(pi_half_count == 2){
+		balance = pi_half - balance;
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+		answer = 0 - answer;
+	}
+
+	/*if quart_count = 3, then formula is = sine quarter # 0
+	 * now we get the actual sine value of excess that's
+	 * less than a quarter pi then answer is direct from table;*/
+	else if(pi_half_count == 3){
+
+		balance = balance << 13;
+		/* balance is now 16+13 = 29 fp portion BUT 20 bit numb */
+
+		balance = balance / pi_half;
+		/* balance is now 29 - 16 = 13 bit fp area BUT 14 bit numb */
+
+		balance = balance + 1; /* for round up */
+		balance = balance >> 1;
+		/* now balance should be a 13 bit number but */
+		/* bit 13 will never be '1' since balance was less than pi_half */
+
+		if(balance>4095) {
+			balance = 4095;
+		}
+
+		answer = wave_table[balance];
+		if(balance>4090) {
+			answer |= BIT16;
+		}
+	}
+
+	if(sign==1) {
+		answer = 0-answer;
+	}
+
+	return answer;
+	/*
+	 * so return value is a signed short where bit_16 = integer and 16
+	 * LSBits are FP
+	 */
+}
+
+/*----------------------------------------------------------------------
+ * Function: ovl_set_coeff_reg()
+ * Description: Helper function for filter coefficient programming
+ *
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+static unsigned int ovl_set_coeff_reg(
+	int * fpint_Coeff,
+	unsigned short wMantSize,
+	unsigned short * pCoeff,
+	unsigned short wPosition)
+{
+
+	/*
+	 * fpint_Coeff is a pointer to signed int that follows
+	 * MS 16 bits =integer, LS 16 bits =floating point for f.p. portion of
+	 * scaling
+	 */
+
+	unsigned short wRes;
+	unsigned short sign;
+	int coeff;
+	int maxVal;
+	int wCoeff_exp3;
+	int wCoeff_exp2;
+	int wCoeff_exp1;
+	int wCoeff_exp0;
+
+	sign = 0;
+	maxVal = 1 << wMantSize;
+	coeff = *fpint_Coeff;
+	if (coeff < 0) {
+		sign = 1;
+		coeff = - coeff;
+	}
+
+	wRes = 12 - wMantSize;
+
+	wCoeff_exp3 = coeff * 4;
+	wCoeff_exp2 = coeff * 2;
+	wCoeff_exp1 = coeff;
+	wCoeff_exp0 = coeff;
+	wCoeff_exp0 += BIT0;
+	wCoeff_exp0 = wCoeff_exp0 >> 1;
+
+	wCoeff_exp3 = wCoeff_exp3 + (1 << (16 - wMantSize -1));
+	/* round coeff to the nearest bit X */
+	/* where X is the last MS bit that will
+	 * be left out of the mantissa*/
+	wCoeff_exp3 = wCoeff_exp3 >> (16 - wMantSize);
+	/* now wCoeff is a unsigned short where */
+	/* FP portion matched maxVal */
+
+	wCoeff_exp2 = wCoeff_exp2 + (1 << (16 - wMantSize -1));
+	/* round coeff to the nearest bit X */
+	/* where X is the last MS bit that will
+	 * be left out of the mantissa */
+
+	wCoeff_exp2 = wCoeff_exp2 >> (16 - wMantSize);
+	/* now wCoeff is a unsigned short where */
+	/* FP portion matched maxVal */
+
+	wCoeff_exp1 = wCoeff_exp1 + (1 << (16 - wMantSize -1));
+	/* round coeff to the nearest bit X */
+	/* where X is the last MS bit that will
+	 * be left out of the mantissa */
+
+	wCoeff_exp1 = wCoeff_exp1 >> (16 - wMantSize);
+	/* now wCoeff is a unsigned short where */
+	/* FP portion matched maxVal */
+
+	wCoeff_exp0 = wCoeff_exp0 + (1 << (16 - wMantSize -1));
+	/* round coeff to the nearest bit X */
+	/* where X is the last MS bit that will
+	 * be left out of the mantissa */
+
+	wCoeff_exp0 = wCoeff_exp0 >> (16 - wMantSize);
+	/* now wCoeff is a unsigned short where */
+	/* FP portion matched maxVal */
+
+	pCoeff[wPosition] = 0;
+
+	if ( wCoeff_exp3 < maxVal ) {
+		pCoeff[wPosition] |= (3 << 12);/*exponent */
+		pCoeff[wPosition] |= (wCoeff_exp3 << wRes);/*mantissa*/
+
+		*fpint_Coeff = wCoeff_exp3 << (16 - wMantSize);
+		*fpint_Coeff += BIT1;
+		*fpint_Coeff = *fpint_Coeff >> 2;
+	} else if ( wCoeff_exp2 < maxVal ) {
+		pCoeff[wPosition] |= (2 << 12);/*.exponent */
+		pCoeff[wPosition] |= (wCoeff_exp2 << wRes);/*mantissa*/
+
+		*fpint_Coeff = wCoeff_exp2 << (16 - wMantSize);
+		*fpint_Coeff += BIT0;
+		*fpint_Coeff = *fpint_Coeff >> 1;
+	} else if ( wCoeff_exp1 < maxVal ) {
+		pCoeff[wPosition] |= (1 << 12);/*.exponent */
+		pCoeff[wPosition] |= (wCoeff_exp1 << wRes);/*mantissa*/
+
+		*fpint_Coeff = wCoeff_exp1 << (16 - wMantSize);
+	} else if ( wCoeff_exp0 < maxVal ) {
+		pCoeff[wPosition] |= (wCoeff_exp0 << wRes);/*mantissa*/
+
+		*fpint_Coeff = wCoeff_exp0 << (16 - wMantSize);
+		*fpint_Coeff = *fpint_Coeff * 2;
+	} else {
+		return 0; /*Coeff out of range*/
+	}
+
+	if(sign) {
+		pCoeff[wPosition] |= BIT15;/*sign */
+	}
+
+	if (sign) {
+		*fpint_Coeff = -(*fpint_Coeff);
+	}
+
+	return 1;
+
+}
+
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_coeff_regs()
+ * Description: Helper function for filter coefficient programming
+ *
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+int ddCoeff[17][5];
+unsigned short wTapAdjust[5];
+void ovl_update_coeff_regs(
+	unsigned short wTaps,
+	int fpint_cutoff,
+	unsigned short bHor,
+	unsigned short bY,
+	unsigned short * pCoeff)
+{
+
+	unsigned short i, j, j1, num, pos, wMantSize;
+	unsigned short bVandC;
+
+	int val, sinc, window, sum, x, y;
+	int dCoeff[5*32];
+	int dDiff;
+	unsigned short wTap2Fix;
+
+	const int pi = 0x3243f; /* = 3.141586 << 16 */
+				  /*+- 3.1415926535; */
+
+	/* H Scale */
+	if (bHor==1)
+		wMantSize = 7;
+	else
+		wMantSize = 6;
+
+	/*vertal & Chroma */
+	if(bHor==0 && bY==0)
+		bVandC = 1;
+	else
+		bVandC = 0;
+
+	/*
+	 * 1 - let's make this fixed point number smaller by 4 bits,
+	 * since it will be a denominator later
+	 */
+	/* 2 - dont forget to round it at the 3rd bit */
+	fpint_cutoff += BIT3;
+	fpint_cutoff = fpint_cutoff >> 4; /* 16 bits of FP (reduced from 20) */
+
+	num = wTaps * 16;
+	for (i = 0; i < num*2; i++) {
+		/* fpint cutoff is 16 bits */
+		val = ((1 << 30)/fpint_cutoff);
+		/*14 bits of FP (after dividing the fpint_cutoff */
+		val += BIT3;
+		val = val >> 4; /* val is now 10 bits of FP */
+		val = val * wTaps * pi;
+		/*now 26 bits of FP (increase from 10 after multiply pi)*/
+		val += BIT5;
+		val = val >> 6; /*20 bits of FP (reduced from 26 bits)*/
+		val = val * (((i - num) << 8)/(2*num)); /*28 bits of FP now*/
+		if(val<0) {
+			val -= BIT11;
+		} else {
+			val += BIT11;
+		}
+		val = val >> 12; /* 16 bits of FP (reduced from 22 bits)*/
+
+		if (val == 0) {
+			sinc = (1 << 16); /*sinc = 1.0;*/
+		} else {
+			sinc = ovl_util_sine(val);
+			/* dont forget val has 14 bits of FP to it */
+			sinc = sinc << 12; /* sinc now has 28 bit FP */
+			if(val<0) {
+				val -= BIT3;
+			} else {
+				val += BIT3;
+			}
+			val = val >> 4; /* val now has 12 bit FP */
+			sinc = sinc / val; /* the new sinc is back to 16 bit FP */
+		}
+
+		/* hanning window */
+		window = (pi << 12)/num; /* 16 bits increased to 28 bits FP */
+		window += BIT11;
+		window = window >> 12;
+		window = i * window;
+		window = ovl_util_cosine(window);
+		/* note cosine param is 16 bits FP to it */
+		window += 1;
+		window = window >> 1; /* divided by 2 */
+		window = ((1 << 15) /*0.5*/ - window);
+
+		if(window<0) {
+			window -= BIT3;
+		} else {
+			window += BIT3;
+		}
+		window = window >> 4; /* now window is with 12 bit FP*/
+		if(sinc<0) {
+			sinc -= BIT3;
+		} else {
+			sinc += BIT3;
+		}
+		sinc = sinc >> 4; /* now sinc is with 12 bit FP */
+
+		dCoeff[i] = sinc * window;
+		/* dCoeff item is with 24 bit FP  --> lets reduce to 16*/
+		if(dCoeff[i]<0) {
+			dCoeff[i] -= BIT7;
+		} else {
+			dCoeff[i] += BIT7;
+		}
+		dCoeff[i] = dCoeff[i] >> 8;
+
+	}
+
+	for (i = 0; i < 17; i++) {
+		/* normalize the coefficient */
+		sum = 0;
+		for (j = 0; j < wTaps; j++) {
+			pos = i + j * 32;
+			sum += dCoeff[pos];
+			/* sum takes from dCeoff so it also has 16bit FP to it */
+		}
+		for (j = 0; j < wTaps; j++) {
+			pos = i + j * 32;
+			x = dCoeff[pos] << 11; /* x now has (16+11)=27 fp to it */
+			y = sum;
+			if(y<0)
+				y -= BIT7;
+			else
+				y += BIT7;
+			y = y >> 8; /* y now has (16-8)=8 fp to it */
+			ddCoeff[i][j] = x/y; /* ddCoeff has 27-8 = 19 fp to it */
+			if(ddCoeff[i][j] < 0)
+				ddCoeff[i][j] -= BIT2;
+			else
+				ddCoeff[i][j] += BIT2;
+			ddCoeff[i][j] = ddCoeff[i][j] >> 3;
+			/*ddCoeff is back to 16 bit fp (19-3) */
+		}
+
+		/*
+		 * set the coefficient registers and get the data in floating
+		 * point format
+		 */
+        for (j = 0; j < wTaps; j++) {
+			pos = j + i * wTaps;
+			if ( (j == (wTaps - 1)/2) && (!bVandC) ) {
+				ovl_set_coeff_reg(&ddCoeff[i][j],
+					(unsigned short)(wMantSize + 2),pCoeff,pos);
+			} else {
+				ovl_set_coeff_reg(&ddCoeff[i][j],
+					(unsigned short)wMantSize,pCoeff,pos);
+			}
+		}
+
+		wTapAdjust[0] = (wTaps - 1)/2;
+		for (j = 1, j1 = 1; j <= wTapAdjust[0]; j++, j1++) {
+			wTapAdjust[j1] = wTapAdjust[0] - j;
+			wTapAdjust[++j1] = wTapAdjust[0] + j;
+		}
+
+		/* adjust the coefficient */
+		sum = 0;
+		for (j = 0; j < wTaps; j++) {
+			sum += ddCoeff[i][j]; /*sum is now 16 bit fp to it*/
+		}
+
+		if (sum != (1 << 16)) {
+			/* if sum != 1.0 */
+			for (j1 = 0; j1 < wTaps; j1++) {
+				wTap2Fix = wTapAdjust[j1];
+				dDiff = (1 << 16) - sum; /*dDiff is also 16 bit FP to it*/
+				ddCoeff[i][wTap2Fix] += dDiff;
+				pos = wTap2Fix + i * wTaps;
+				if ( (wTap2Fix == (wTaps - 1)/2) && (!bVandC) ) {
+					ovl_set_coeff_reg(&ddCoeff[i][wTap2Fix],
+						(unsigned short)(wMantSize + 2),pCoeff,pos);
+				} else {
+					ovl_set_coeff_reg(&ddCoeff[i][wTap2Fix],
+						(unsigned short)wMantSize,pCoeff,pos);
+				}
+				sum = 0;
+				for (j = 0; j < wTaps; j++) {
+					sum += ddCoeff[i][j]; /*sum is now 16 bit FP to it*/
+				}
+				if (sum == (1 << 16 )) {
+					break;
+				}
+			}
+		}
+	}
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.h b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.h
new file mode 100644
index 0000000..60cadb7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_coeff.h
@@ -0,0 +1,45 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_coeff.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for overlay. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized structures and functions internal to
+ *  overlay
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OVL_COEFF_H
+#define _OVL_COEFF_H
+
+extern void ovl_update_coeff_regs(
+	unsigned short wTaps,
+	int fpint_cutoff,
+	unsigned short bHor,
+	unsigned short bY,
+	unsigned short * pCoeff);
+
+#endif  /* _OVL_COEFF_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_dispatch.h b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_dispatch.h
new file mode 100644
index 0000000..4487071
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_dispatch.h
@@ -0,0 +1,57 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_dispatch.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OVERLAY_DISPATCH_H
+#define _OVERLAY_DISPATCH_H
+
+typedef struct _ovl_dispatch {
+	int (*blend_surf_needed)(igd_display_context_t *display,
+		igd_surface_t       *src_surf,
+		igd_rect_t          *src_rect,
+		igd_rect_t          *dest_rect,
+		unsigned int         flags,
+		igd_surface_t       *blend_surf,
+		igd_rect_t          *blend_rect);
+	int (*alter_ovl)(igd_display_context_t *display,
+		igd_surface_t       *src_surf,
+		igd_rect_t          *src_rect,
+		igd_rect_t          *dest_rect,
+		igd_ovl_info_t      *ovl_info,
+		unsigned int         flags);
+	int (*query_ovl)(igd_display_h display_h,
+		unsigned int flags);
+	int (*query_max_size_ovl)(igd_display_h display_h,
+		unsigned long pf,
+		unsigned int *max_width,
+		unsigned int *max_height);
+} ovl_dispatch_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_virt.h b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_virt.h
new file mode 100644
index 0000000..7b0a998
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/cmn/ovl_virt.h
@@ -0,0 +1,107 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_virt.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for overlay. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized structures and functions internal to
+ *  overlay
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OVL_VIRT_H
+#define _OVL_VIRT_H
+
+/* None of the IALs use the OVL_SUPPORT_*.  Exclude it for now. */
+/* None of the IALs use the OVL_RULE_MUST_*.  Exclude it for now. */
+
+/* Overlay HW range of values for color control and gamma correction*/
+#define OVL_HW_DEF_BRIGHT      750L
+#define OVL_HW_MIN_BRIGHT      0L
+#define OVL_HW_MAX_BRIGHT      10000L
+
+#define OVL_HW_DEF_CONT	    10000L
+#define OVL_HW_MIN_CONT	    0L
+#define OVL_HW_MAX_CONT	    20000L
+
+#define OVL_HW_DEF_SAT	        10000L
+#define OVL_HW_MIN_SAT		    0L
+#define OVL_HW_MAX_SAT	        20000L
+
+#define OVL_HW_DEF_HUE			0L
+#define OVL_HW_MIN_HUE			-180L
+#define OVL_MHW_AX_HUE			180L
+
+#define OVL_HW_DEF_GAMMA       1L
+#define OVL_HW_MAX_GAMMA       500L
+#define OVL_HW_MIN_GAMMA       1L
+
+#define OVL_REPROG_OP_NONE            0
+#define OVL_REPROG_OP_TURNON_PRIM     1
+#define OVL_REPROG_OP_TURNON_SECND    2
+#define OVL_REPROG_OP_TURNOFF_PRIM    3
+#define OVL_REPROG_OP_TURNOFF_SECND   4
+#define OVL_REPROG_OP_SWITCH_TO_PRIM  5
+#define OVL_REPROG_OP_SWITCH_TO_SECND 6
+#define OVL_REPROG_OP_REVERT_SECND	  7
+#define OVL_REPROG_OP_REVERT_PRIM	  8
+
+enum {
+	OVL_STATE_OFF = 0,
+	OVL_STATE_ON,
+};
+
+#define OVL_PRIMARY   0
+#define OVL_SECONDARY 1
+#define OVL_MAX_HW    2  /* Maximum number of overlays */
+
+/* Define the maximum number of blend surfaces which can be used */
+#define MAX_BLEND_SURF 2
+
+typedef struct _ovl_context{
+	ovl_dispatch_t (*dispatch)[];  /* Pointer to an array */
+	unsigned int  state;
+	unsigned long reg_update_offset;
+	unsigned long reg_update_phys;
+	unsigned long sync;
+	unsigned long sync2;
+	unsigned int blend_surf_num[OVL_MAX_HW];
+	igd_surface_t blend_surf[OVL_MAX_HW][MAX_BLEND_SURF];
+	unsigned int ovl_buff;
+	unsigned int fb_blend_ovl;
+	unsigned short reg_allocated;
+	igd_surface_t  *saved_src_surf;
+	igd_rect_t     *saved_src_rect;
+	igd_rect_t     *saved_dest_rect;
+	igd_ovl_info_t *saved_ovl_info;
+	unsigned int   saved_flags;
+	igd_display_context_t * ovl_display_km[OVL_MAX_HW];
+	unsigned int ovl_display_swapped;
+} ovl_context_t;
+
+extern ovl_context_t ovl_context[];
+
+#endif /*_OVL_VIRT_H*/
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/micro_ovl_plb.c b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/micro_ovl_plb.c
new file mode 100644
index 0000000..c58e890
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/micro_ovl_plb.c
@@ -0,0 +1,1725 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_ovl_plb.c
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains function that actually programs the second
+ *  overlay with the bits to properly configure the overlay
+ *  Also includes functions to execute the second overlay flip
+ *  instruction, and query the overlay flip status.
+ *  Also contains some hardware capabilities querrying functions
+ *  for upper overlay layer to get this chips second overlay
+ *  capabilities
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.overlay
+
+#include <math_fix.h>
+#include <plb/cmd.h>
+#include "ovl_plb_cache.h"
+#include "ovl2_plb.h"
+#include "../cmn/ovl_dispatch.h"
+#include "../cmn/ovl_virt.h"
+
+extern unsigned long sprite_pixel_formats_plb[];
+
+extern unsigned int ovl2_check_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+
+int micro_alter_ovl2_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+
+#ifndef CONFIG_MICRO_OVERLAY
+#define	OVL2_CHECK_PLB_RET(ret,a, b, c, d, e,f) ret = ovl2_check_plb(a ,b ,c ,d ,e ,f)
+#define QUERY_OVL2_PLB_RET(ret,a, b) ret = query_ovl2_plb(a, b)
+#else
+#define	OVL2_CHECK_PLB_RET(ret,a, b, c, d, e,f) 0
+#define QUERY_OVL2_PLB_RET(ret,a, b) 0
+#endif
+
+ovl_dispatch_t ovl_micro_dispatch_plb[] = {
+	{
+		NULL,
+		micro_alter_ovl2_plb,
+		NULL,
+		NULL,
+	},
+};
+
+#ifndef OVL_PLB_CACHE_QUICK_SWAP /* If caching is NOT enabled */
+unsigned int micro_spritec_update_src_plb(igd_display_context_t *display,
+	ovl2_reg_plb_t *spritec_regs_plb,
+	igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	unsigned int      src_Bpp;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format)/8;
+
+	/* src surface */
+	spritec_regs_plb->start = src_surf->offset +
+		(src_rect->y1 * src_surf->pitch) + (src_rect->x1 * src_Bpp);
+
+	EMGD_WRITE32(src_surf->pitch,
+		MMIO(display) + 0x72188);
+
+	/* src pixel format */
+	switch(src_surf->pixel_format){
+	case IGD_PF_YUV422_PACKED_YUY2:
+		spritec_regs_plb->control |= OVL2_CMD_YUV_422;
+		break;
+	case IGD_PF_YUV422_PACKED_UYVY:
+		spritec_regs_plb->control |= OVL2_CMD_YUV_422 | OVL2_CMD_UYVY;
+		break;
+	case IGD_PF_ARGB32_8888:
+		spritec_regs_plb->control |= OVL2_CMD_ARGB_8888;
+		break;
+	case IGD_PF_xRGB32_8888:
+		spritec_regs_plb->control |= OVL2_CMD_RGB_8888;
+		break;
+	case IGD_PF_RGB16_565:
+		spritec_regs_plb->control |= OVL2_CMD_RGB_565;
+		break;
+	case IGD_PF_xRGB16_555:
+	case IGD_PF_ARGB16_1555:
+		spritec_regs_plb->control |= OVL2_CMD_RGB_555;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		spritec_regs_plb->control |= OVL2_CMD_RGB_8;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid pixel format: 0x%lx", src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Turn off YUV to RGB conversion if the src is RGB */
+	if (!(src_surf->pixel_format & PF_TYPE_YUV)) {
+		spritec_regs_plb->control |= (1<<19);
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------
+ * Function: spritec_update_video_quality_plb()
+ *
+ * Description:
+ *   This function updates the contrast, brightness, and saturation of
+ *   the overlay using the values specified in overlay_info.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static int micro_spritec_update_video_quality_plb(
+	igd_display_context_t        *display,
+	igd_surface_t                *src_surf,
+	igd_ovl_video_quality_info_t *video_quality)
+{
+	int                          calc_brightness_tmp = 0;
+	int                          calc_brightness     = 0;
+	unsigned int                 calc_contrast_tmp   = 0;
+	unsigned int                 calc_contrast       = 0;
+	unsigned int                 calc_saturation_tmp = 0;
+	unsigned int                 calc_saturation     = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* If the src_surf pixel format is RGB, then brightness, contrast,
+	 * and saturation should all be set to the exact default */
+	if (src_surf->pixel_format & PF_TYPE_RGB) {
+		if (video_quality->brightness != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set brightness to default");
+		}
+		if (video_quality->contrast != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set contrast to default");
+		}
+		if (video_quality->saturation != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set saturation to default");
+		}
+
+		EMGD_WRITE32(OVL2_RGB_COLOR_DEF_CONT_BRGHT,
+			MMIO(display) + 0x721D0);
+		EMGD_WRITE32(OVL2_RGB_COLOR_DEF_SATN_HUE,
+			MMIO(display) + 0x721D4);
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Brightness
+	 *************************************************************************/
+	if (0x8000 == video_quality->brightness) {
+		calc_brightness = MID_BRIGHTNESS_YUV;
+	} else if (video_quality->brightness < 0x8000) {
+		/*
+		 * we have here a brightness that is less than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = 0x8000 - video_quality->brightness;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = -128 - MID_BRIGHTNESS_YUV;
+		/*
+		 * more range if the midpoint is positive but less range
+		 * if midpoint is negative
+		 */
+
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness < -128) {
+			calc_brightness = -128;
+		}
+		if (calc_brightness > MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	} else {
+		/*
+		 * we have here a brightness that is more than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = video_quality->brightness - 0x8000;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = 127 - MID_BRIGHTNESS_YUV;
+		/*
+		 * less range if the midpoint is positive but more range
+		 * if midpoint is negative
+		 */
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness > 127) {
+			calc_brightness = 127;
+		}
+		if (calc_brightness < MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	}
+
+	EMGD_WRITE32((EMGD_READ32(MMIO(display) + 0x721D0) & 0xFFFFFF00) |
+		(calc_brightness & 0xFF),
+		MMIO(display) + 0x721D0);
+
+
+	/*************************************************************************
+	 * Contrast
+	 *************************************************************************/
+	if (0x8000 == video_quality->contrast ){
+		calc_contrast = MID_CONTRAST_YUV;
+	} else if (video_quality->contrast < 0x8000) {
+		/* we have here a contrast that is less than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = MID_CONTRAST_YUV;
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		if (calc_contrast > 0x3F) {
+			calc_contrast = 0x3F;
+		}
+	} else {
+		/* we have here a contrast that is more than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast - 0x8000;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = (0x1FF - MID_CONTRAST_YUV);
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		calc_contrast     += MID_CONTRAST_YUV;
+		if (calc_contrast > 0x1FF) {
+			calc_contrast = 0x1FF;
+		}
+	}
+
+	EMGD_WRITE32((EMGD_READ32(MMIO(display) + 0x721D0) & 0xF803FFFF) |
+		((calc_contrast & 0x1FF) << 18),
+		MMIO(display) + 0x721D0);
+
+	/*************************************************************************
+	 * Saturation
+	 *************************************************************************/
+	if (video_quality->saturation == 0x8000) {
+		calc_saturation = MID_SATURATION_YUV;
+	} else if (video_quality->saturation < 0x8000) {
+		/* we have here a saturation that is less than the default
+		 * mid point */
+		calc_saturation_tmp = video_quality->saturation;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = MID_SATURATION_YUV;
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		if (calc_saturation > 0x7F) {
+			calc_saturation = 0x7F;
+		}
+	} else {
+		/* we have here a saturation that is more than the default
+		 * mid point*/
+		calc_saturation_tmp = video_quality->saturation - 0x8000;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = (0x3FF - MID_SATURATION_YUV);
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		calc_saturation     += MID_SATURATION_YUV;
+
+		if (calc_saturation > 0x3FF) {
+			calc_saturation = 0x3FF;
+		}
+	}
+
+	EMGD_WRITE32((EMGD_READ32(MMIO(display) + 0x721D4) & 0xFFFFFC00) |
+		(calc_saturation & 0x3FF),
+		MMIO(display) + 0x721D4);
+
+
+	/*************************************************************************
+	 * Hue
+	 *************************************************************************/
+	/* Hue is always set to the default value.  It is based on the saturation
+	 * value, and having a separate hue is of minimal value. */
+	EMGD_WRITE32(EMGD_READ32(MMIO(display) + 0x721D4) & 0xF800FFFF,
+		MMIO(display) + 0x721D4);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*-----------------------------------------------------------------------------
+ * Function: spritec_update_gamma_plb()
+ *
+ * Description:
+ *    This function sets the gamma correction values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static int micro_spritec_update_gamma_plb(
+	igd_display_context_t *display,
+	ovl2_reg_plb_t *spritec_regs_plb,
+	igd_ovl_gamma_info_t * ovl_gamma)
+{
+	const int gamma_reg_input[OVL2_TOTAL_GAMMA_REG] = {8, 16, 32, 64, 128, 192};
+	const int gamma_reg_offset[OVL2_TOTAL_GAMMA_REG] = {
+		OVL2_REG_ADDR_GAMMA0,
+		OVL2_REG_ADDR_GAMMA1,
+		OVL2_REG_ADDR_GAMMA2,
+		OVL2_REG_ADDR_GAMMA3,
+		OVL2_REG_ADDR_GAMMA4,
+		OVL2_REG_ADDR_GAMMA5
+	};
+	const unsigned int gamma_def[OVL2_TOTAL_GAMMA_REG] = {
+		0x00080808,
+		0x00101010,
+		0x00202020,
+		0x00404040,
+		0x00808080,
+		0x00c0c0c0
+	};
+	unsigned int          new_gamma_red_24i_8f, new_gamma_green_24i_8f;
+	unsigned int          new_gamma_blue_24i_8f;
+	unsigned int          gamma_normal_r_24i_8f;
+	unsigned int          gamma_normal_g_24i_8f;
+	unsigned int          gamma_normal_b_24i_8f;
+	unsigned int          gamma_reg, gamma_reg_24i_8f;
+	unsigned int          i;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The gamma values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* If the overlay gamma is disabled or the display is an 8 bit mode
+	 * (second overay can not support gamma in an 8 bit mode),
+	 * set it to the default */
+	if (((ovl_gamma->flags & IGD_OVL_GAMMA_ENABLE) == IGD_OVL_GAMMA_DISABLE) ||
+		(PLANE(display)->fb_info->pixel_format == IGD_PF_ARGB8_INDEXED)) {
+		for (i = 0; i < OVL2_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			EMGD_WRITE32(gamma_def[i], MMIO(display) + gamma_reg_offset[i]);
+		}
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* It is assumed that the input value is a 24-bit number */
+	new_gamma_red_24i_8f   = ovl_gamma->red;
+	new_gamma_green_24i_8f = ovl_gamma->green;
+	new_gamma_blue_24i_8f  = ovl_gamma->blue;
+
+	/*
+	 * Program RGB for each of the 6 gamma registers
+	 */
+
+	/* Since the OS_POW_FIX function can only take an integer base,
+	 * we need to normalize the result by gamma_normal_x
+	 */
+	gamma_normal_r_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_red_24i_8f);
+	gamma_normal_g_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_green_24i_8f);
+	gamma_normal_b_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_blue_24i_8f);
+
+	for( i = 0; i < OVL2_TOTAL_GAMMA_REG; i++ )
+	{
+		/* red */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+								(1<<16)/new_gamma_red_24i_8f);
+		gamma_reg        =
+			((255 * gamma_reg_24i_8f) / gamma_normal_r_24i_8f) << 16;
+
+		/* green */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_green_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_g_24i_8f) << 8;
+
+		/* blue */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_blue_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_b_24i_8f);
+
+		/* turn overlay off (TBD) */
+
+		/* program register */
+		EMGD_WRITE32(gamma_reg, MMIO(display) + gamma_reg_offset[i]);
+
+		/* turn overlay on (TBD) */
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#endif // End if caching is not enabled.
+
+static unsigned int convert_color_key_to_hw (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	case IGD_PF_ARGB8_INDEXED:
+	default:
+		output = input;
+		break;
+	case IGD_PF_RGB16_565:
+		output =
+			((((input & 0xf800)>>11)<<3)<<16) |
+			((((input & 0x07e0)>>5 )<<2)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	case IGD_PF_ARGB16_1555:
+		output =
+			((((input & 0x7c00)>>10)<<3)<<16) |
+			((((input & 0x03e0)>>5 )<<3)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	}
+
+	return output;
+}
+static unsigned int convert_color_key_to_mask (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	default:
+		output = 0x00ffffff;
+		break;
+	case IGD_PF_RGB16_565:
+		output = 0x00f8fcf8;
+		break;
+	case IGD_PF_ARGB16_1555:
+		output = 0x00f8f8f8;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		output = 0x000000ff;
+		break;
+	}
+
+	return output;
+}
+
+/* Convert YUV to UVY for YUV pixel formats.
+ * Do not convert RGB pixel formats */
+static unsigned int yuv_to_uvy(
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	if (IGD_PF_TYPE(pf) & PF_TYPE_YUV) {
+		output =
+			((input & 0x00ff0000) >> 16) |
+			((input & 0x0000ff00) << 8)  |
+			((input & 0x000000ff) << 8);
+	} else {
+		output = input;
+	}
+
+	return output;
+}
+
+#ifndef OVL_PLB_CACHE_QUICK_SWAP /* If caching is NOT enabled */
+
+static unsigned int micro_spritec_update_regs_plb(
+	igd_display_context_t *display,
+	ovl2_reg_plb_t      *spritec_regs_plb,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	unsigned int plane_control, plane_start;
+	unsigned int pipe_num;
+	unsigned int ckey_low, ckey_high;
+	int ret;
+
+
+	EMGD_TRACE_ENTER;
+
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		/* Turn off the plane control key enable and the second overlay
+		 * control. */
+		plane_control =
+			EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+		if ((plane_control & (3<<22)) != 0) {
+			plane_control &= ~(3<<22);
+			EMGD_WRITE32(plane_control,
+				MMIO(display) + PLANE(display)->plane_reg);
+			plane_start =
+				EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg + 4);
+			EMGD_WRITE32(plane_start,
+				MMIO(display) + PLANE(display)->plane_reg + 4);
+		}
+
+		/* Turn the overlay Off.
+		 * Ensure we are using the correct Pipe. */
+		pipe_num = PIPE(display)->pipe_num ?
+			(1<<24)/*Pipe B*/ :
+			(0<<24)/*Pipe A*/;
+		EMGD_WRITE32(pipe_num, MMIO(display) + 0x72180);
+		EMGD_WRITE32(0, MMIO(display) + 0x72184);
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/***********************************************************************
+	 * Copy the information passed in to the HW overlay structure
+	 **********************************************************************/
+	/* Zero the control, since they will be OR'ed in with data
+	 * below */
+	spritec_regs_plb->control = 0;
+
+	/* Interleaved/progressive and Odd/Even if interleaved */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		EMGD_ERROR("Overlay2 does not support Interleaved");
+	}
+
+	/* Dest rect information */
+	EMGD_WRITE32((dest_rect->y1<<16) | (dest_rect->x1),
+		MMIO(display) + 0x7218C);
+	EMGD_WRITE32(((dest_rect->y2 - dest_rect->y1 - 1)<<16) |
+		(dest_rect->x2 - dest_rect->x1 - 1),
+		MMIO(display) + 0x72190);
+
+	/* Src rect and surface information */
+	ret = micro_spritec_update_src_plb(display, spritec_regs_plb, src_surf, src_rect);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay2 updating src failed");
+		return ret;
+	}
+
+	/* Color control information */
+	if (!(flags & IGD_OVL_OSD_ON_SPRITEC)) {
+		ret = micro_spritec_update_video_quality_plb(display, src_surf,
+			&ovl_info->video_quality);
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 video quality failed");
+			return ret;
+		}
+		ret = micro_spritec_update_gamma_plb(display, spritec_regs_plb, &ovl_info->gamma);
+		if (ret) {
+			EMGD_ERROR("Overlay2 gamma failed");
+			return ret;
+		}
+	}
+
+	/* Destination color key */
+	EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Dest Color Key");
+		/* The mask and color key are different for the different
+		 * pixel formats */
+		EMGD_WRITE32(convert_color_key_to_hw(
+					   PLANE(display)->fb_info->pixel_format,
+					   ovl_info->color_key.dest),
+			MMIO(display) + PLANE(display)->plane_reg + 0x14);
+		EMGD_WRITE32(convert_color_key_to_mask(
+					   PLANE(display)->fb_info->pixel_format,
+					   ovl_info->color_key.dest),
+			MMIO(display) + PLANE(display)->plane_reg + 0x18);
+
+		/* Both the plane control key enable and the second overlay
+		 * control order must be enabled to turn on destination color
+		 * key.  Also rewrite the plane_start which is the trigger for
+		 * Plane A/B */
+		plane_control =
+			EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+		if ((plane_control & (3<<22)) != (3<<22)) {
+			plane_control |= (3<<22);
+			EMGD_WRITE32(plane_control,
+				MMIO(display) + PLANE(display)->plane_reg);
+			plane_start =
+				EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg + 4);
+			EMGD_WRITE32(plane_start,
+				MMIO(display) + PLANE(display)->plane_reg + 4);
+
+			/* Note: On a 915GM (maybe other platforms as well), the dest
+			 * color key is not always enabled when the plane A/B and
+			 * plane C are both modified in the same vblank.  So, ensure
+			 * they occur on a different vblank.  This should be fine,
+			 * since this will only occur the first time when enabling
+			 * the dest color key. */
+			display->context->dispatch.wait_vblank((igd_display_h)display);
+		}
+
+		if (flags & IGD_OVL_OSD_ON_SPRITEC) {
+			/* Sprite C should be below plane and above overlay. */
+			spritec_regs_plb->control |= 1;
+		} else {
+			/* Sprite C should be on the bottom of the Z order.
+			 * Plane B should be above Sprite C */
+			spritec_regs_plb->control |= 6;
+		}
+	} else {
+		EMGD_DEBUG("Overlay Disable Dest Color Key");
+		plane_control =
+			EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+		if ((plane_control & (3<<22)) != 0) {
+			plane_control &= ~(3<<22);
+			EMGD_WRITE32(plane_control,
+				MMIO(display) + PLANE(display)->plane_reg);
+			plane_start =
+				EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg + 4);
+			EMGD_WRITE32(plane_start,
+				MMIO(display) + PLANE(display)->plane_reg + 4);
+		}
+	}
+
+	/* Source Color key */
+	if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Src Color Key");
+
+		ckey_high = convert_color_key_to_hw(src_surf->pixel_format,
+			ovl_info->color_key.src_hi);
+		ckey_high = yuv_to_uvy(src_surf->pixel_format,
+			ckey_high);
+
+		ckey_low = convert_color_key_to_hw(src_surf->pixel_format,
+			ovl_info->color_key.src_lo);
+		ckey_low = yuv_to_uvy(src_surf->pixel_format,
+			ckey_low);
+
+		EMGD_WRITE32(ckey_low,  MMIO(display) + 0x72194);
+		EMGD_WRITE32(ckey_high, MMIO(display) + 0x721a0);
+		EMGD_WRITE32(7,         MMIO(display) + 0x72198);
+		spritec_regs_plb->control |= (1<<22);
+	} else {
+		EMGD_DEBUG("Overlay Disable Src Color Key");
+		EMGD_WRITE32(0,         MMIO(display) + 0x72198);
+	}
+
+	/* General overlay information.  Turn the second overlay on.
+	 * The trigger register is the start register which causes the
+	 * overlay to update.  The trigger register is written in send_instr */
+	spritec_regs_plb->control |= (1<<31);
+    spritec_regs_plb->control |= PIPE(display)->pipe_num ?
+	    (1<<24)/*Pipe B*/ :
+	    (0<<24)/*Pipe A*/;
+    EMGD_WRITE32(spritec_regs_plb->control, MMIO(display) + 0x72180);
+	/*EMGD_WRITE32(ovl2_regs_plb->start,   MMIO(display) + 0x72184);*/
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#else /* OVL_PLB_CACHE_QUICK_SWAP is enabled. */
+
+/* Poulsbo overlay cache structure */
+static ovl_plb_cache_t ovl_cache;
+
+/* Flag to signal the cache is invalid and needs
+ * to be re-initialized */
+static int ovl_cache_needs_init = TRUE;
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_src_ptr_plb()
+ *
+ * Description:
+ *   This function updates the source offset
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+unsigned int micro_spritec_update_src_ptr_plb(igd_surface_t *src_surf,
+					  igd_rect_t    *src_rect)
+{
+	unsigned int      src_Bpp;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format) / 8;
+
+	/* src surface */
+	ovl_cache.ovl2_regs.start =
+		src_surf->offset +
+		(src_rect->y1 * src_surf->pitch) +
+		(src_rect->x1 * src_Bpp);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_src_plb()
+ *
+ * Description:
+ *   This function updates the source pitch and pixel format
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+unsigned int micro_spritec_update_src_plb(igd_surface_t *src_surf,
+					  igd_rect_t    *src_rect)
+{
+	EMGD_TRACE_ENTER;
+
+	ovl_cache.ovl2_regs.pitch = src_surf->pitch;
+
+	/* src pixel format */
+	switch(src_surf->pixel_format){
+	case IGD_PF_YUV422_PACKED_YUY2:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_YUV_422;
+		break;
+	case IGD_PF_YUV422_PACKED_UYVY:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_YUV_422 | OVL2_CMD_UYVY;
+		break;
+	case IGD_PF_ARGB32_8888:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_ARGB_8888;
+		break;
+	case IGD_PF_xRGB32_8888:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_8888;
+		break;
+	case IGD_PF_RGB16_565:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_565;
+		break;
+	case IGD_PF_xRGB16_555:
+	case IGD_PF_ARGB16_1555:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_555;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_8;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid pixel format: 0x%lx", src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Turn off YUV to RGB conversion if the src is RGB */
+	if (!(src_surf->pixel_format & PF_TYPE_YUV)) {
+		ovl_cache.ovl2_regs.control |= (1<<19);
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_video_quality_plb()
+ *
+ * Description:
+ *   This function updates the contrast, brightness, and saturation of
+ *   the overlay using the values specified in overlay_info.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static int micro_spritec_update_video_quality_plb(
+	igd_surface_t                *src_surf,
+	igd_ovl_video_quality_info_t *video_quality)
+{
+	int                          calc_brightness_tmp = 0;
+	int                          calc_brightness     = 0;
+	unsigned int                 calc_contrast_tmp   = 0;
+	unsigned int                 calc_contrast       = 0;
+	unsigned int                 calc_saturation_tmp = 0;
+	unsigned int                 calc_saturation     = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* If the src_surf pixel format is RGB, then brightness, contrast,
+	 * and saturation should all be set to the exact default */
+	if (src_surf->pixel_format & PF_TYPE_RGB) {
+		if (video_quality->brightness != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set brightness to default");
+		}
+		if (video_quality->contrast != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set contrast to default");
+		}
+		if (video_quality->saturation != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set saturation to default");
+		}
+
+		ovl_cache.ovl2_regs.cont_bright = OVL2_RGB_COLOR_DEF_CONT_BRGHT;
+		ovl_cache.ovl2_regs.satn_hue =  OVL2_RGB_COLOR_DEF_SATN_HUE;
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Brightness
+	 *************************************************************************/
+	if (0x8000 == video_quality->brightness) {
+		calc_brightness = MID_BRIGHTNESS_YUV;
+	} else if (video_quality->brightness < 0x8000) {
+		/*
+		 * we have here a brightness that is less than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = 0x8000 - video_quality->brightness;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = -128 - MID_BRIGHTNESS_YUV;
+		/*
+		 * more range if the midpoint is positive but less range
+		 * if midpoint is negative
+		 */
+
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness < -128) {
+			calc_brightness = -128;
+		}
+		if (calc_brightness > MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	} else {
+		/*
+		 * we have here a brightness that is more than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = video_quality->brightness - 0x8000;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = 127 - MID_BRIGHTNESS_YUV;
+		/*
+		 * less range if the midpoint is positive but more range
+		 * if midpoint is negative
+		 */
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness > 127) {
+			calc_brightness = 127;
+		}
+		if (calc_brightness < MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	}
+
+	ovl_cache.ovl2_regs.cont_bright =
+		(ovl_cache.ovl2_regs.cont_bright & 0xFFFFFF00) |
+		(calc_brightness & 0xFF);
+
+
+	/*************************************************************************
+	 * Contrast
+	 *************************************************************************/
+	if (0x8000 == video_quality->contrast ){
+		calc_contrast = MID_CONTRAST_YUV;
+	} else if (video_quality->contrast < 0x8000) {
+		/* we have here a contrast that is less than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = MID_CONTRAST_YUV;
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		if (calc_contrast > 0x3F) {
+			calc_contrast = 0x3F;
+		}
+	} else {
+		/* we have here a contrast that is more than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast - 0x8000;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = (0x1FF - MID_CONTRAST_YUV);
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		calc_contrast     += MID_CONTRAST_YUV;
+		if (calc_contrast > 0x1FF) {
+			calc_contrast = 0x1FF;
+		}
+	}
+
+        ovl_cache.ovl2_regs.cont_bright =
+                (ovl_cache.ovl2_regs.cont_bright & 0xF803FFFF ) |
+                ((calc_contrast & 0x1FF) << 18);
+
+
+	/*************************************************************************
+	 * Saturation
+	 *************************************************************************/
+	if (video_quality->saturation == 0x8000) {
+		calc_saturation = MID_SATURATION_YUV;
+	} else if (video_quality->saturation < 0x8000) {
+		/* we have here a saturation that is less than the default
+		 * mid point */
+		calc_saturation_tmp = video_quality->saturation;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = MID_SATURATION_YUV;
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		if (calc_saturation > 0x7F) {
+			calc_saturation = 0x7F;
+		}
+	} else {
+		/* we have here a saturation that is more than the default
+		 * mid point*/
+		calc_saturation_tmp = video_quality->saturation - 0x8000;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = (0x3FF - MID_SATURATION_YUV);
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		calc_saturation     += MID_SATURATION_YUV;
+
+		if (calc_saturation > 0x3FF) {
+			calc_saturation = 0x3FF;
+		}
+	}
+
+        ovl_cache.ovl2_regs.satn_hue =
+                (ovl_cache.ovl2_regs.satn_hue & 0xFFFFFC00 ) |
+                (calc_saturation & 0x3FF);
+
+	/*************************************************************************
+	 * Hue
+	 *************************************************************************/
+	/* Hue is always set to the default value.  It is based on the saturation
+	 * value, and having a separate hue is of minimal value. */
+        ovl_cache.ovl2_regs.satn_hue =
+		(ovl_cache.ovl2_regs.satn_hue & 0xF800FFFF);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+static const int gamma_reg_input[OVL2_TOTAL_GAMMA_REG] = {8, 16, 32, 64, 128, 192};
+static const int gamma_reg_offset[OVL2_TOTAL_GAMMA_REG] = {
+	OVL2_REG_ADDR_GAMMA0,
+	OVL2_REG_ADDR_GAMMA1,
+	OVL2_REG_ADDR_GAMMA2,
+	OVL2_REG_ADDR_GAMMA3,
+	OVL2_REG_ADDR_GAMMA4,
+	OVL2_REG_ADDR_GAMMA5
+};
+static const unsigned int gamma_def[OVL2_TOTAL_GAMMA_REG] = {
+	0x00080808,
+	0x00101010,
+	0x00202020,
+	0x00404040,
+	0x00808080,
+	0x00c0c0c0
+};
+
+/*-----------------------------------------------------------------------------
+ * Function: spritec_update_gamma_plb()
+ *
+ * Description:
+ *    This function sets the gamma correction values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static int micro_spritec_update_gamma_plb(
+	igd_display_context_t *display,
+	igd_ovl_gamma_info_t *ovl_gamma)
+{
+	unsigned int          new_gamma_red_24i_8f, new_gamma_green_24i_8f;
+	unsigned int          new_gamma_blue_24i_8f;
+	unsigned int          gamma_normal_r_24i_8f;
+	unsigned int          gamma_normal_g_24i_8f;
+	unsigned int          gamma_normal_b_24i_8f;
+	unsigned int          gamma_reg, gamma_reg_24i_8f;
+	unsigned int          i;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The gamma values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* If the overlay gamma is disabled or the display is an 8 bit mode
+	 * (second overay can not support gamma in an 8 bit mode),
+	 * set it to the default */
+	if (((ovl_gamma->flags&IGD_OVL_GAMMA_ENABLE)==IGD_OVL_GAMMA_DISABLE) ||
+	     (PLANE(display)->fb_info->pixel_format == IGD_PF_ARGB8_INDEXED)) {
+		for (i = 0; i < OVL2_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			ovl_cache.ovl2_regs.gamma_regs[i] = gamma_def[i];
+		}
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* It is assumed that the input value is a 24-bit number */
+	new_gamma_red_24i_8f   = ovl_gamma->red;
+	new_gamma_green_24i_8f = ovl_gamma->green;
+	new_gamma_blue_24i_8f  = ovl_gamma->blue;
+
+	/*
+	 * Program RGB for each of the 6 gamma registers
+	 */
+
+	/* Since the OS_POW_FIX function can only take an integer base,
+	 * we need to normalize the result by gamma_normal_x
+	 */
+	gamma_normal_r_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_red_24i_8f);
+	gamma_normal_g_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_green_24i_8f);
+	gamma_normal_b_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_blue_24i_8f);
+
+	for( i = 0; i < OVL2_TOTAL_GAMMA_REG; i++ )
+	{
+		/* red */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+					      (1<<16)/new_gamma_red_24i_8f);
+		gamma_reg =
+			((255 * gamma_reg_24i_8f) / gamma_normal_r_24i_8f) << 16;
+
+		/* green */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+					      (1<<16)/new_gamma_green_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_g_24i_8f) << 8;
+
+		/* blue */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+					      (1<<16)/new_gamma_blue_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_b_24i_8f);
+
+		/* program register */
+		ovl_cache.ovl2_regs.gamma_regs[i] = gamma_reg;
+
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_disable_ovl_plb()
+ *
+ * Description:
+ * Write the registers needed to turn off the overlay.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static unsigned int micro_spritec_disable_ovl_plb(
+	igd_display_context_t *display	)
+{
+	unsigned int pipe_num;
+	unsigned int plane_control, plane_start;
+
+	/* Turn off the plane control key enable and the second overlay
+	 * control. */
+	plane_control =
+		EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+	if ((plane_control & (3<<22)) != 0) {
+		plane_control &= ~(3<<22);
+		EMGD_WRITE32(plane_control,
+			   MMIO(display) + PLANE(display)->plane_reg);
+		plane_start =
+			EMGD_READ32(MMIO(display) +
+				  PLANE(display)->plane_reg + 4);
+		EMGD_WRITE32(plane_start,
+			   MMIO(display) + PLANE(display)->plane_reg + 4);
+	}
+
+	/* Turn the overlay Off.
+	 * Ensure we are using the correct Pipe. */
+	pipe_num = PIPE(display)->pipe_num ?
+		(1<<24)/*Pipe B*/ :
+		(0<<24)/*Pipe A*/;
+	EMGD_WRITE32(pipe_num, MMIO(display) + 0x72180);
+	EMGD_WRITE32(0, MMIO(display) + 0x72184);
+
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_clear_cache_plb()
+ *
+ * Description:
+ *
+ *----------------------------------------------------------------------*/
+
+static void micro_spritec_clear_cache_plb(
+        igd_display_context_t *display,
+	unsigned int           flags)
+{
+	/* Force every cache check to miss */
+	OS_MEMSET(&ovl_cache, 0, sizeof(ovl_plb_cache_t));
+
+	/* We just set our cached flags to 0, which might accidently
+	 * match up with "OFF" for some important incoming flag
+	 * bits, causing us to think we already handled them when
+	 * we didn't.  So set our cached flags to the exact
+	 * opposite of the incoming flags, which will force
+	 * us to test and handle every single bit, regardless
+	 * of whether it is on or off. */
+	ovl_cache.flags = ~flags;
+
+	/* init our cached registers */
+	ovl_cache.ovl2_regs.plane_control =
+		EMGD_READ32(MMIO(display) +
+			  PLANE(display)->plane_reg);
+	ovl_cache.ovl2_regs.plane_start =
+		EMGD_READ32(MMIO(display) +
+			  PLANE(display)->plane_reg + 4);
+
+	/* initialization complete */
+	ovl_cache_needs_init = FALSE;
+
+}
+
+
+/*-----------------------------------------------------------------------------
+ * Function: spritec_update_colorkey_plb()
+ *
+ * Description:
+ *    This function sets the colorkey values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static void micro_spritec_update_colorkey_plb(
+	igd_display_context_t *display,
+	igd_surface_t         *src_surf,
+	igd_ovl_info_t        *ovl_info)
+{
+	unsigned int ckey_low, ckey_high;
+
+	/* Destination color key */
+	EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+
+		EMGD_DEBUG("Overlay Enable Dest Color Key");
+		/* The mask and color key are different for the
+		 * different pixel formats */
+		ovl_cache.ovl2_regs.colorkey_hw =
+			convert_color_key_to_hw
+			(PLANE(display)->fb_info->pixel_format,
+			 ovl_info->color_key.dest);
+
+		ovl_cache.ovl2_regs.colorkey_mask =
+			convert_color_key_to_mask
+			(PLANE(display)->fb_info->pixel_format,
+			 ovl_info->color_key.dest);
+		/*
+		 * Both the plane control key enable and the second
+		 * overlay control order must be enabled to turn on
+		 * destination color key.  Also rewrite the
+		 * plane_start which is the trigger for Plane A/B
+		 */
+		if ( (ovl_cache.ovl2_regs.plane_control & (3<<22)) !=
+		     (3<<22)) {
+			ovl_cache.ovl2_regs.plane_control |= (3<<22);
+		}
+
+	} else {
+		EMGD_DEBUG("Overlay Disable Dest Color Key");
+		if ((ovl_cache.ovl2_regs.plane_control & (3<<22)) != 0) {
+			ovl_cache.ovl2_regs.plane_control &= ~(3<<22);
+		}
+	}
+
+	/* Source Color key */
+	if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Src Color Key");
+
+		ckey_high = convert_color_key_to_hw
+			(src_surf->pixel_format,
+			 ovl_info->color_key.src_hi);
+		ckey_high = yuv_to_uvy(src_surf->pixel_format,
+				       ckey_high);
+
+		ckey_low = convert_color_key_to_hw
+			(src_surf->pixel_format,
+			 ovl_info->color_key.src_lo);
+		ckey_low = yuv_to_uvy(src_surf->pixel_format,
+				      ckey_low);
+
+		ovl_cache.ovl2_regs.ckey_low = ckey_low;
+		ovl_cache.ovl2_regs.ckey_high = ckey_high;
+		ovl_cache.ovl2_regs.ckey_enable = 7;
+		ovl_cache.ovl2_regs.control |= (1<<22);
+	} else {
+		EMGD_DEBUG("Overlay Disable Src Color Key");
+		ovl_cache.ovl2_regs.ckey_enable = 0;
+	}
+
+}
+
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_write_cache_plb()
+ *
+ * Description:
+ *
+ *----------------------------------------------------------------------*/
+
+static void micro_spritec_write_cache_plb(
+        igd_display_context_t *display,
+	ovl2_reg_plb_t        *spritec_regs_plb,
+	igd_ovl_info_t        *ovl_info,
+	int                    cache_changed)
+{
+	int i;
+
+	/*
+	 * Now write all the changed registers to the HW
+	 * TODO: Or should we write all the registers, regardless of
+	 * if they have changed?
+	 * TODO: It may be beneficial to turn off overlay while
+	 * updateing the regs?
+	*/
+
+	/* Write source information */
+        if (cache_changed & (IGD_OVL_PLB_UPDATE_SURF |
+                             IGD_OVL_PLB_UPDATE_SRC  ) ) {
+		EMGD_WRITE32(ovl_cache.ovl2_regs.pitch,
+			   MMIO(display) + MMIO_OFFSET_PLB2_PITCH);
+	}
+
+	/* Write dest rect information */
+        if (cache_changed & IGD_OVL_PLB_UPDATE_DEST) {
+		EMGD_WRITE32( ovl_cache.ovl2_regs.dest_rect_x1y1,
+			    MMIO(display) + MMIO_OFFSET_PLB2_DEST_RECT_X1Y1);
+		EMGD_WRITE32( ovl_cache.ovl2_regs.dest_rect_x2y2,
+			    MMIO(display) + MMIO_OFFSET_PLB2_DEST_RECT_X2Y2);
+	}
+
+	/* write the quality information */
+        if (cache_changed & (IGD_OVL_PLB_UPDATE_VQ   |
+                             IGD_OVL_PLB_UPDATE_SURF ) ) {
+		EMGD_WRITE32(ovl_cache.ovl2_regs.cont_bright,
+			MMIO(display) + MMIO_OFFSET_PLB2_CONT_BRIGHT);
+		EMGD_WRITE32(ovl_cache.ovl2_regs.satn_hue,
+			MMIO(display) + MMIO_OFFSET_PLB2_SATN_HUE);
+	}
+
+	/* Write the gamma */
+        if (cache_changed & IGD_OVL_PLB_UPDATE_GAMMA) {
+		for (i = 0; i < OVL2_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			EMGD_WRITE32(ovl_cache.ovl2_regs.gamma_regs[i],
+				   MMIO(display) + gamma_reg_offset[i]);
+		}
+	}
+
+	/* Write the colorkey data */
+        if (cache_changed & IGD_OVL_PLB_UPDATE_COLORKEY) {
+
+		/* Dest color key */
+		if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+
+			/* Write the regs needed to turn it on */
+			EMGD_WRITE32(ovl_cache.ovl2_regs.colorkey_hw,
+				   MMIO(display) +
+				   PLANE(display)->plane_reg +
+				   MMIO_OFFSET_PLB2_COLORKEY_HW);
+
+			EMGD_WRITE32(ovl_cache.ovl2_regs.colorkey_mask,
+				   MMIO(display) +
+				   PLANE(display)->plane_reg +
+				   MMIO_OFFSET_PLB2_COLORKEY_MASK);
+		}
+
+		EMGD_WRITE32(ovl_cache.ovl2_regs.plane_control,
+			   MMIO(display) + PLANE(display)->plane_reg);
+
+		EMGD_WRITE32(ovl_cache.ovl2_regs.plane_start,
+			   MMIO(display) + PLANE(display)->plane_reg+4);
+
+		/*
+		 * Note: On a 915GM (maybe other platforms as well),
+		 * the dest color key is not always enabled when the
+		 * plane A/B and plane C are both modified in the
+		 * same vblank.  So, ensure they occur on a different
+		 * vblank.  This should be fine, since this will only
+		 * occur the first time when enabling the dest color
+		 * key.
+		 */
+		if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+			display->context->dispatch.
+				wait_vblank((igd_display_h)display);
+		}
+
+		/* Source Color key */
+		if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+			EMGD_WRITE32(ovl_cache.ovl2_regs.ckey_low,
+				   MMIO(display) + MMIO_OFFSET_PLB2_CKEY_LOW);
+			EMGD_WRITE32(ovl_cache.ovl2_regs.ckey_high,
+				   MMIO(display) + MMIO_OFFSET_PLB2_CKEY_HIGH);
+		}
+
+		EMGD_WRITE32(ovl_cache.ovl2_regs.ckey_enable,
+			   MMIO(display) + MMIO_OFFSET_PLB2_CKEY_ENABLE);
+	}
+
+	/* Write the control register, but not the start register.
+	   The trigger register is the start register
+	   which causes the overlay to update.  The trigger
+	   register is written in send_instr */
+
+	EMGD_WRITE32(ovl_cache.ovl2_regs.control,
+		   MMIO(display) + MMIO_OFFSET_PLB2_CONTROL);
+
+	spritec_regs_plb->start = ovl_cache.ovl2_regs.start;
+	spritec_regs_plb->control = ovl_cache.ovl2_regs.control;
+
+}
+
+
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_regs_plb()
+ *
+ * Description:
+ * Examine the incoming overlay parameters, and update the overlay hardware
+ * regs according to what changed.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static unsigned int micro_spritec_update_regs_plb(
+	igd_display_context_t *display,
+	ovl2_reg_plb_t      *spritec_regs_plb,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	// unsigned int ckey_low, ckey_high;
+	// int i,
+	int ret;
+	int cache_changed;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Fast path for turning off overlay. No need for cache */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		ret = micro_spritec_disable_ovl_plb(display);
+
+		/* Reset the cache */
+		ovl_cache_needs_init = TRUE;
+
+		return ret;
+	}
+
+	/* Init the cache if needed */
+	if (ovl_cache_needs_init) {
+		micro_spritec_clear_cache_plb(display, flags);
+	}
+
+	/* See what has changed in the cache */
+        cache_changed = get_cache_changes_plb (src_surf,
+					   src_rect,
+					   dest_rect,
+					   ovl_info,
+					   flags,
+					   &ovl_cache);
+
+	/*
+	 * Perhaps the biggest challenge of caching the overlay
+         * state is what to do with the command and config regs.
+         * Normally we would clear command and config to 0 here,
+         * and let the update process set only the bits that are
+         * needed.  But doing this would invalidate our cache.
+         * Instead we are relying on the above call to
+         * get_cache_changes() to clear those bits in command
+         * and config that will be changing */
+
+
+	/* Normally we would set interleave parameters here,
+	 * but the secondary overlay does not support interleave.*/
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		EMGD_ERROR("Overlay2 does not support Interleaved");
+	}
+
+	/* ----------------------------------------------------------*/
+        /* Has our destination rectangle changed? */
+        if (cache_changed & IGD_OVL_PLB_UPDATE_DEST) {
+
+		ovl_cache.ovl2_regs.dest_rect_x1y1 =
+			( (dest_rect->y1 << 16) | dest_rect->x1 );
+		ovl_cache.ovl2_regs.dest_rect_x2y2 =
+			( (dest_rect->y2 - dest_rect->y1 - 1) << 16) |
+		 	  (dest_rect->x2 - dest_rect->x1 - 1)        ;
+	}
+
+	/* ----------------------------------------------------------*/
+        /* Always update the source pointers every frame. */
+	ret = micro_spritec_update_src_ptr_plb(src_surf,
+					   src_rect);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay2 updating src failed");
+		return ret;
+	}
+
+
+	/* ----------------------------------------------------------*/
+        /* Did either the Src rect or surface change? */
+        if (cache_changed & (IGD_OVL_PLB_UPDATE_SURF |
+                             IGD_OVL_PLB_UPDATE_SRC  ) ) {
+		ret = micro_spritec_update_src_plb(src_surf,
+						   src_rect);
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 updating src failed");
+			return ret;
+		}
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Did the quality information change? */
+        if (cache_changed & (IGD_OVL_PLB_UPDATE_VQ   |
+                             IGD_OVL_PLB_UPDATE_SURF ) ) {
+		ret = micro_spritec_update_video_quality_plb
+			(src_surf,
+			 &ovl_info->video_quality);
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 video quality failed");
+			return ret;
+		}
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Did the gamma change? */
+        if (cache_changed & IGD_OVL_PLB_UPDATE_GAMMA) {
+		ret = micro_spritec_update_gamma_plb(display, &ovl_info->gamma);
+		if (ret) {
+			EMGD_ERROR("Overlay2 gamma failed");
+			return ret;
+		}
+
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Did the color key change? */
+        if (cache_changed & IGD_OVL_PLB_UPDATE_COLORKEY) {
+		micro_spritec_update_colorkey_plb(display,
+						  src_surf,
+						  ovl_info);
+	}
+
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+		if (flags & IGD_OVL_OSD_ON_SPRITEC) {
+			ovl_cache.ovl2_regs.control |= 1;
+		} else {
+			/* Sprite C should be on the bottom of the Z order.
+			 * Plane B should be above Sprite C */
+			ovl_cache.ovl2_regs.control |= 6;
+		}
+	}
+
+	/* General overlay information.  Turn the second overlay on. */
+	ovl_cache.ovl2_regs.control |= (1<<31);
+	ovl_cache.ovl2_regs.control |= PIPE(display)->pipe_num ?
+		(1<<24)/*Pipe B*/ :
+		(0<<24)/*Pipe A*/;
+
+	/*
+	 * Now write all the changes to the part
+	 */
+	micro_spritec_write_cache_plb(display,
+				      spritec_regs_plb,
+				      ovl_info,
+				      cache_changed);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#endif
+
+
+
+unsigned int micro_spritec_send_instr_plb(
+	igd_display_context_t     *display,
+	ovl2_reg_plb_t    *spritec_regs_plb,
+	unsigned int      flags)
+{
+	EMGD_TRACE_ENTER;
+
+	/* Send a load register instruction to write the Plane C sprite address
+	 * which is the trigger register.
+	 * This is an instruction, so it happens after blend, and since it
+	 * is an instruction, we do not have to poll waiting for it. */
+
+#ifdef OVL_PLB_CACHE_QUICK_SWAP /* If caching is enabled */
+	EMGD_WRITE32(ovl_cache.ovl2_regs.start, MMIO(display) +
+		   MMIO_OFFSET_PLB2_START);
+
+#else
+	EMGD_WRITE32(spritec_regs_plb->start, MMIO(display) + 0x72184);
+#endif
+
+	ovl_context->sync2 = 0;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+int micro_prepare_ovl2_plb(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	ovl2_reg_plb_t      *spritec_regs_plb,
+	unsigned int         flags)
+{
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Check to ensure the overlay can be used given the current mode as
+	 * well as what the IAL is asking for.  If not return an error. */
+
+	if( (OVL2_CHECK_PLB_RET(ret,display, src_surf, src_rect,
+				dest_rect, ovl_info,flags)) ) {
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 Check failed");
+			return ret;
+		}
+	}
+
+	/* Check if last flip is still pending.
+	 * This is necessary for the following reasons:
+	 *    - If the previous instructions have not been processed, then the
+	 *      spritec_regs_plb is still in use and can not be overwritten.
+	 */
+	if (PIPE(display)){
+		if( (QUERY_OVL2_PLB_RET(ret,(igd_display_h)display,
+								IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE)) ){
+			if ((FALSE == ret) &&
+				(flags & IGD_OVL_ALTER_ON)) {
+				/* Only return an error if the overlay is on.  If turning it off,
+				* allow it to continue, since something may have failed and we
+				* should try our best to turn the overlay off. */
+				return -IGD_ERROR_HWERROR;
+			}
+		}
+	}
+
+	/* Update all Overlay Update Registers */
+	ret = micro_spritec_update_regs_plb(display, spritec_regs_plb,
+		src_surf, src_rect, dest_rect, ovl_info,
+		flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Sprite C update Registers failed");
+		return ret;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+int micro_alter_ovl2_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl2_reg_plb_t spritec_regs_plb;
+	int ret=0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Dump overlay parameters for debugging */
+	/*
+	if (src_surf && src_rect && dest_rect && ovl_info) {
+		printk (KERN_ERR " micro_alter_ovl2_plb  Entry."
+			"offset=0x%X "
+			"pitch=%d "
+			"width=%d "
+			"height=%d \n"
+			"pixel_format=0x%X "
+			"flags=0x%X "
+			"virt_addr=0x%X "
+			"pvr2d_mem_info=0x%X "
+			"pvr2d_context_h=0x%X "
+			"hPVR2DFlipChain=0x%X \n"
+			"src_x1=%d "
+			"src_x2=%d "
+			"src_y1=%d "
+			"src_y2=%d "
+			"src width=%d "
+			"src height=%d \n"
+			"dest_x1=%d "
+			"dest_x2=%d "
+			"dest_y1=%d "
+			"dest_y2=%d "
+			"dest width=%d "
+			"dest height=%d \n"
+			"color_key.src_lo=0x%X "
+			"color_key.src_hi=0x%X "
+			"color_key.dest=0x%X "
+			"color_key.flags=0x%X "
+			"flags=0x%X "
+			,
+			(unsigned int) src_surf->offset ,
+			(unsigned int) src_surf->pitch ,
+			(unsigned int) src_surf->width ,
+			(unsigned int) src_surf->height ,
+			(unsigned int) src_surf->pixel_format ,
+			(unsigned int) src_surf->flags ,
+			(unsigned int) src_surf->virt_addr ,
+			(unsigned int) src_surf->pvr2d_mem_info ,
+			(unsigned int) src_surf->pvr2d_context_h ,
+			(unsigned int) src_surf->hPVR2DFlipChain ,
+			(unsigned int) src_rect->x1,
+			(unsigned int) src_rect->x2,
+			(unsigned int) src_rect->y1,
+			(unsigned int) src_rect->y2,
+			(unsigned int) (src_rect->x2 - src_rect->x1),
+			(unsigned int) (src_rect->y2 - src_rect->y1),
+			(unsigned int) dest_rect->x1,
+			(unsigned int) dest_rect->x2,
+			(unsigned int) dest_rect->y1,
+			(unsigned int) dest_rect->y2,
+			(unsigned int) (dest_rect->x2 - dest_rect->x1),
+			(unsigned int) (dest_rect->y2 - dest_rect->y1),
+			(unsigned int) ovl_info->color_key.src_lo,
+			(unsigned int) ovl_info->color_key.src_hi,
+			(unsigned int) ovl_info->color_key.dest,
+			(unsigned int) ovl_info->color_key.flags,
+			(unsigned int) flags
+			);
+	}
+	*/
+	/* Initialize structure so compilers don't complain */
+	OS_MEMSET(&spritec_regs_plb, 0, sizeof(ovl2_reg_plb_t));
+
+	if (micro_prepare_ovl2_plb(display, src_surf, src_rect, dest_rect,
+		ovl_info, &spritec_regs_plb, flags)) {
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Directlt write the register to update 2nd overlay */
+	ret = micro_spritec_send_instr_plb(display, &spritec_regs_plb, flags);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.c b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.c
new file mode 100644
index 0000000..816c3ab
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.c
@@ -0,0 +1,542 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl2_plb.c
+ * $Revision: 1.22 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains function that actually programs the second
+ *  overlay with the bits to properly configure the overlay
+ *  Also includes functions to execute the second overlay flip
+ *  instruction, and query the overlay flip status.
+ *  Also contains some hardware capabilities querrying functions
+ *  for upper overlay layer to get this chips second overlay
+ *  capabilities
+ *-----------------------------------------------------------------------------
+ */
+/* Referenced from Napa. Need change later */
+
+#define MODULE_NAME hal.overlay
+
+#include "ovl_plb_cache.h"
+#include <plb/regs.h>
+#include <plb/cmd.h>
+#include <plb/context.h>
+#include "../cmn/ovl_dispatch.h"
+#include "../cmn/ovl_virt.h"
+#include "ovl2_regs_plb.h"
+
+extern unsigned long sprite_pixel_formats_plb[];
+extern int micro_prepare_ovl2_plb(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	ovl2_reg_plb_t      *spritec_regs_plb,
+	unsigned int         flags);
+
+/*----------------------------------------------------------------------
+ * Function: ovl2_check_pf_plb()
+ * Parameters: unsigned int requested_pixel_format -
+ *             according to definitions in igd_mode.h
+ *
+ * Description:
+ *
+ * Returns:
+ *   TRUE on Success
+ *   FALSE on The first pixel format that is supported
+ *----------------------------------------------------------------------*/
+static unsigned int ovl2_check_pf_plb(
+	igd_display_context_t *display,
+	unsigned int requested_pixel_format)
+{
+	unsigned long *spr_pf = sprite_pixel_formats_plb;
+	int temp_loop = 0;
+
+	while(spr_pf[temp_loop]) {
+		if(spr_pf[temp_loop] == requested_pixel_format) {
+			return TRUE;
+		}
+		++temp_loop;
+	}
+
+	return FALSE;
+}
+
+static unsigned int get_uv_shift_x (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV422_PACKED_YUY2:
+	case IGD_PF_YUV422_PACKED_UYVY:
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+static unsigned int get_uv_shift_y (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+unsigned int ovl2_check_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	igd_timing_info_t *timing;
+	unsigned int min_w, min_h;
+
+	EMGD_TRACE_ENTER;
+
+	if (!display){
+		EMGD_ERROR_EXIT("display is null");
+	    return -IGD_ERROR_INVAL;
+	}
+	if (!PIPE(display)){
+	    EMGD_ERROR_EXIT("PIPE(display) is null");
+	    return -IGD_ERROR_INVAL;
+	}
+
+	timing = PIPE(display)->timing;
+
+    if(!timing) {
+        EMGD_ERROR_EXIT("timing is null\n");
+        return -IGD_ERROR_INVAL;
+    }
+
+	/* The following parameters are only valid if the overlay is on, so
+	 * return success if the overlay is being turned off. */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Ensure the framebuffer dotclock does not exceed the board SKU
+	 * max dotclock
+	 **************************************************************************/
+        /* Make it chipset-specific */
+        /* DCT-PC99TA crashes with dotclock > 300MHz */
+	if(timing->dclk >= 340000){
+		EMGD_ERROR_EXIT("Cannot support dotclock > 340MHz for this SKU");
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/*************************************************************************
+	 * Ensure the overlay surface is ok and can be properly displayed.
+	 * This ensures the following is valid:
+	 *    - Ensure x1, x2, y1, y2 are pixel aligned
+	 *    - 2 pixels or greater in width and height
+	 *    - Pixel format is supported by the overlay
+	 *    - Pitch is <= 8KB
+	 *    - Based on the pixel format, the width is supported
+	 *************************************************************************/
+	if (!src_surf){
+		EMGD_ERROR_EXIT("src_surf is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (!src_rect){
+		EMGD_ERROR_EXIT("src_rect is null");
+		return -IGD_ERROR_INVAL;
+	}
+	/* Get the minimum size of 1 pixel in width and height for y, u, and v.
+	 */
+	min_w = 1 << get_uv_shift_x(src_surf->pixel_format);
+	min_h = 1 << get_uv_shift_y(src_surf->pixel_format);
+
+	if (((src_rect->x2 - src_rect->x1) < min_w*2) ||
+		((src_rect->y2 - src_rect->y1) < min_h*2)) {
+		EMGD_ERROR_EXIT(
+			"Overlay2 source width or height is < 2 pixels (%dx%d)\n",
+			src_rect->x2 - src_rect->x1, src_rect->y2 - src_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (FALSE == ovl2_check_pf_plb(display, src_surf->pixel_format)) {
+		EMGD_ERROR_EXIT("Overlay2 source pixel format unsupported (pf:0x%lx)",
+			src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	if (src_surf->pitch > 8192) {
+		EMGD_ERROR_EXIT("Overlay2 source pitch (%d) > 8KB",
+			src_surf->pitch);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/*************************************************************************
+	 * Ensure the location on the framebuffer is ok and can be properly
+	 * displayed
+	 * This ensures the following is valid:
+	 *    - Greater than 1 pixel width and height
+	 *    - Will be displayed on screen (not panned off)
+	 *************************************************************************/
+	if (!dest_rect){
+		EMGD_ERROR_EXIT("dest_rect is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (((dest_rect->x2 - dest_rect->x1) <= 1) ||
+		((dest_rect->y2 - dest_rect->y1) <= 1)) {
+		EMGD_ERROR_EXIT(
+			"Overlay2 dest width or height is single pixel (%dx%d)\n",
+			dest_rect->x2 - dest_rect->x1, dest_rect->y2 - dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((dest_rect->x1 >= timing->width) ||
+		(dest_rect->y1 >= timing->height)) {
+		EMGD_ERROR_EXIT(
+			"Overlay2 dest is panned off the screen (%d,%d)\n",
+			dest_rect->x1, dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+unsigned int ovl2_send_instr_plb(
+	igd_display_context_t     *display,
+	ovl2_reg_plb_t    *spritec_regs_plb,
+	unsigned int      flags)
+{
+	unsigned char * mmio = MMIO(display);
+	unsigned long tmp;
+	inter_module_dispatch_t *md;
+	platform_context_plb_t * platform;
+	unsigned int pipe_num;
+	unsigned long pipe_reg;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	/* We dont need the CMD_WAIT_OVL2_PLB instruction coz
+	 * our alter_ovl code already querried status
+	 * for last flip completion before getting here. See
+	 * micro_prepare_ovl2_plb called by alter_ovl2_plb.
+	 * It calls query overlay before the next flip
+	 */
+
+
+	/*
+	 * If Overlay + FB Blend is requested and the FB is xRGB
+	 * turn on the ARGB format.
+	 */
+	if(ovl_context->fb_blend_ovl) {
+		if ((flags & IGD_OVL_ALTER_ON) != IGD_OVL_ALTER_ON) {
+			tmp = EMGD_READ32(mmio +  PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x1c000000) {
+				tmp = tmp & 0xc3FFFFFF;
+				EMGD_WRITE32(tmp | 0x18000000, mmio +  PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 4);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 4);
+				OS_SLEEP(100);
+			}
+		} else {
+			tmp = EMGD_READ32(mmio +  PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x18000000) {
+				EMGD_WRITE32(tmp | 0x1c000000, mmio +  PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 4);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 4);
+			}
+		}
+	}
+
+
+	/* Send a load register instruction to write the Plane C sprite address
+	 * which is the trigger register.
+	 * This is an instruction, so it happens after blend, and since it
+	 * is an instruction, we do not have to poll waiting for it. */
+	EMGD_WRITE32(spritec_regs_plb->start, mmio + 0x72184);
+
+	md = &display->context->mod_dispatch;
+	platform = (platform_context_plb_t *)display->context->
+			platform_context;
+
+	pipe_num = PIPE(display)->pipe_num;
+
+	if(pipe_num){
+		pipe_reg = 0x71024;
+	} else {
+		pipe_reg = 0x70024;
+	}
+
+        if(md && md->set_flip_pending){
+                /* For second overlay, Poulsbo has no ISR bit
+	         * to reflect the flip pending for Display
+                 * Sprite C. So we use Pipe-B vblank status
+                 * as a substitute
+                 */
+                ret = OS_PTHREAD_MUTEX_LOCK(&platform->flip_mutex);
+                md->set_flip_pending(MMIO(display), pipe_reg);
+                OS_PTHREAD_MUTEX_UNLOCK(&platform->flip_mutex);
+        }
+
+	ovl_context->sync2 = 0;
+
+	display->context->dispatch.sync(display,
+		IGD_PRIORITY_NORMAL,
+		&ovl_context->sync2,
+		IGD_SYNC_NONBLOCK);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+int alter_ovl2_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl2_reg_plb_t spritec_regs_plb;
+	int ret=0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Dump overlay parameters for debugging */
+	/*
+	if (src_surf && src_rect && dest_rect && ovl_info) {
+		printk (KERN_ERR " alter_ovl2_plb  Entry."
+			"offset=0x%X "
+			"pitch=%d "
+			"width=%d "
+			"height=%d \n"
+			"pixel_format=0x%X "
+			"flags=0x%X "
+			"virt_addr=0x%X "
+			"pvr2d_mem_info=0x%X "
+			"pvr2d_context_h=0x%X "
+			"hPVR2DFlipChain=0x%X \n"
+			"src_x1=%d "
+			"src_x2=%d "
+			"src_y1=%d "
+			"src_y2=%d "
+			"src width=%d "
+			"src height=%d \n"
+			"dest_x1=%d "
+			"dest_x2=%d "
+			"dest_y1=%d "
+			"dest_y2=%d "
+			"dest width=%d "
+			"dest height=%d \n"
+			"color_key.src_lo=0x%X "
+			"color_key.src_hi=0x%X "
+			"color_key.dest=0x%X "
+			"color_key.flags=0x%X "
+			"flags=0x%X "
+			,
+			(unsigned int) src_surf->offset ,
+			(unsigned int) src_surf->pitch ,
+			(unsigned int) src_surf->width ,
+			(unsigned int) src_surf->height ,
+			(unsigned int) src_surf->pixel_format ,
+			(unsigned int) src_surf->flags ,
+			(unsigned int) src_surf->virt_addr ,
+			(unsigned int) src_surf->pvr2d_mem_info ,
+			(unsigned int) src_surf->pvr2d_context_h ,
+			(unsigned int) src_surf->hPVR2DFlipChain ,
+			(unsigned int) src_rect->x1,
+			(unsigned int) src_rect->x2,
+			(unsigned int) src_rect->y1,
+			(unsigned int) src_rect->y2,
+			(unsigned int) (src_rect->x2 - src_rect->x1),
+			(unsigned int) (src_rect->y2 - src_rect->y1),
+			(unsigned int) dest_rect->x1,
+			(unsigned int) dest_rect->x2,
+			(unsigned int) dest_rect->y1,
+			(unsigned int) dest_rect->y2,
+			(unsigned int) (dest_rect->x2 - dest_rect->x1),
+			(unsigned int) (dest_rect->y2 - dest_rect->y1),
+			(unsigned int) ovl_info->color_key.src_lo,
+			(unsigned int) ovl_info->color_key.src_hi,
+			(unsigned int) ovl_info->color_key.dest,
+			(unsigned int) ovl_info->color_key.flags,
+			(unsigned int) flags
+			);
+	}
+	*/
+	/* Initialize structure so compilers don't complain */
+	OS_MEMSET(&spritec_regs_plb, 0, sizeof(ovl2_reg_plb_t));
+
+	if (micro_prepare_ovl2_plb(display, src_surf, src_rect, dest_rect,
+		ovl_info, &spritec_regs_plb, flags)) {
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Send the instructions to the command queue */
+	ret = ovl2_send_instr_plb(display, &spritec_regs_plb, flags);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+int query_ovl2_plb(igd_display_h display_h,
+	unsigned int flags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_h;
+	inter_module_dispatch_t *md;
+	platform_context_plb_t * platform;
+	os_alarm_t timeout;
+	int ret;
+	unsigned int pipe_num;
+	unsigned long pipe_reg;
+
+	EMGD_TRACE_ENTER;
+
+	switch (flags) {
+	case IGD_OVL_QUERY_IS_HW_SUPPORTED:
+		/* This is the second overlay, so HW overlay is not supported */
+		break;
+
+	case IGD_OVL_QUERY_IS_LAST_FLIP_DONE:
+		/* If there no sync to wait on, then the last flip is done, and the
+		 * Register Update has occured, simply return TRUE (Flip done).
+		 */
+		/*if (!ovl_context->sync2) {
+			EMGD_DEBUG("Overlay already synced");
+			EMGD_TRACE_EXIT;
+			return TRUE;
+		}*/
+
+		/* Wait for vblank */
+		/* Check to see if the plane C flip is pending.  If is is pending
+		 * return FALSE (Flip not done). */
+		/*if(display->context->dispatch.sync(
+			display,
+			IGD_PRIORITY_NORMAL,
+			&ovl_context->sync2,
+			IGD_SYNC_NONBLOCK)) {
+			EMGD_DEBUG("Overlay Sync Check - Flip not done");
+			return FALSE;
+		}*/
+		/* According to the PBL B-spec, there doesnt seem to exist any bit
+		 * for Sprite C Flip-Pending status. Testing 0x20AC in code during
+		 * virt queue's REG write shows nothing changed for Bit8. Thus, we
+		 * are using state of the VBLANK ISR bit as ovl2 flip status.
+		 * Assumption is that if were running 2nd overlay, its either clone
+		 * display or VEXT in WinCE. In either case, were not doing full screen
+		 * FB flipping, so this check should be 'statefully' accurate
+		 */
+
+		if (PIPE(display)){
+			pipe_num = PIPE(display)->pipe_num;
+
+			if(pipe_num){
+				pipe_reg = 0x71024;
+			} else {
+				pipe_reg = 0x70024;
+			}
+
+			md = &display->context->mod_dispatch;
+			platform = (platform_context_plb_t *)display->context->
+						platform_context;
+			if(md && md->check_flip_pending){
+				ret = OS_PTHREAD_MUTEX_LOCK(&platform->flip_mutex);
+				if(md->check_flip_pending(MMIO(display), pipe_reg)){
+					OS_PTHREAD_MUTEX_UNLOCK(&platform->flip_mutex);
+					EMGD_DEBUG("Overlay2 Sync done but Flip not done");
+					return FALSE;
+				}
+				OS_PTHREAD_MUTEX_UNLOCK(&platform->flip_mutex);
+			}
+
+		} else {
+			EMGD_TRACE_EXIT;
+			return TRUE;
+		}
+		/* Now that we know the last flip is done and the register update is
+		 * complete, set the sync to 0 and return TRUE (Flip done). */
+		ovl_context->sync2 = 0;
+		break;
+	case IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE:
+		/* Wait for 200 milliseconds for the last flip to complete.  If not
+		 * done in that time, there is likely a hardware problem so return
+		 * FALSE. */
+		timeout = OS_SET_ALARM(200);
+		do {
+			if (TRUE ==
+				query_ovl2_plb(display_h, IGD_OVL_QUERY_IS_LAST_FLIP_DONE)) {
+				EMGD_TRACE_EXIT;
+				return TRUE;
+			}
+		} while (!OS_TEST_ALARM(timeout));
+		EMGD_ERROR_EXIT("Timeout waiting for last flip done");
+		return FALSE;
+		break;
+	case IGD_OVL_QUERY_IS_GAMMA_SUPPORTED:
+		return TRUE;
+		break;
+	case IGD_OVL_QUERY_IS_VIDEO_PARAM_SUPPORTED:
+		return TRUE;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return TRUE;
+}
+
+
+
+int query_max_size_ovl2_plb(
+	igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height)
+{
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: Should this be size of FB? */
+	*max_width = 2048;
+	*max_height = 2048;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.h b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.h
new file mode 100644
index 0000000..dca46bc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_plb.h
@@ -0,0 +1,55 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl2_plb.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for napa overlay engine. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized structures and functions internal to
+ *  the napa core's overlay engine
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _OVL2_PLB_H
+#define _OVL2_PLB_H
+extern int blend2_surf_needed_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	unsigned int         flags,
+	igd_surface_t       *blend_surf,
+	igd_rect_t          *blend_rect);
+extern int alter_ovl2_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+extern int query_ovl2_plb(igd_display_h display_h,
+	unsigned int flags);
+extern int query_max_size_ovl2_plb(igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height);
+#endif /* _OVL2_NAP_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_regs_plb.h b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_regs_plb.h
new file mode 100644
index 0000000..6460560
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl2_regs_plb.h
@@ -0,0 +1,76 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl2_regs_plb.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for napa overlay engine. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized structures and functions internal to
+ *  the napa core's overlay engine
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _OVL2_REGS_PLB_H
+#define _OVL2_REGS_PLB_H
+/* Second Overlay Structure.
+ * These registers are the trigger registers and should be written
+ * last. */
+typedef struct _ovl2_reg_plb{
+	unsigned int control;
+	unsigned int start;
+} ovl2_reg_plb_t;
+
+/* Color Correction */
+#define OVL2_RGB_COLOR_DEF_CONT_BRGHT    0x1000000
+#define OVL2_RGB_COLOR_DEF_SATN_HUE      0x0000080
+
+/* Overlay Command Definitions */
+/* 422 Swap */
+#define OVL2_CMD_YVYU				0x00020000
+#define OVL2_CMD_UYVY				0x00010000
+#define OVL2_CMD_VYUY				0x00030000
+
+/* Source Format */
+#define OVL2_CMD_ARGB_8888          0x1C000000
+#define OVL2_CMD_RGB_8888			0x18000000
+#define OVL2_CMD_RGB_565			0x14000000
+#define OVL2_CMD_RGB_555			0x10000000
+#define OVL2_CMD_RGB_8				0x08000000
+#define OVL2_CMD_YUV_422			0x00000000 /*packed YUV422*/
+#define OVL2_CMD_SRC_FMT_MASK		0x3C000000 /*mask for above*/
+
+#define OVL2_REG_ADDR_GAMMA5						0x721E0
+#define OVL2_REG_ADDR_GAMMA4						0x721E4
+#define OVL2_REG_ADDR_GAMMA3						0x721E8
+#define OVL2_REG_ADDR_GAMMA2						0x721EC
+#define OVL2_REG_ADDR_GAMMA1						0x721F0
+#define OVL2_REG_ADDR_GAMMA0						0x721F4
+#define OVL2_TOTAL_GAMMA_REG						6
+
+/* following value are needed because hardware seems to display yuv slightly dimmer
+   than RGB when color data is calculated out to be equal */
+#define MID_CONTRAST_YUV					0x43//4a
+#define MID_SATURATION_YUV					0x91//92
+#define MID_BRIGHTNESS_YUV					-5
+#endif /* _OVL2_REGS_NAP_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb.c b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb.c
new file mode 100644
index 0000000..42d4a01
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb.c
@@ -0,0 +1,2188 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_plb.c
+ * $Revision: 1.30 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains function that actually programs the overlay
+ *  register back buffer with the bits to properly configure
+  *  the overlay
+ *  Also includes functions to execute the overlay flip instruction,
+ *  and query the overlay flip status.
+ *  Also contains some hardware capabilities querrying functions
+ *  for upper overlay layer to get this chips overlay capabilities
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.overlay
+
+#include <math_fix.h>
+#include <plb/cmd.h>
+#include "ovl_plb_cache.h"
+#include "../cmn/ovl_dispatch.h"
+#include "../cmn/ovl_virt.h"
+#include "../cmn/ovl_coeff.h"
+#include "ovl2_plb.h"
+
+/*-----------------------------------------------------------------------------
+ * Common dispatch functions
+ *---------------------------------------------------------------------------*/
+static int alter_ovl_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+static int query_ovl_plb(igd_display_h display_h,
+	unsigned int flags);
+static int query_max_size_ovl_plb(igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height);
+
+
+ovl_dispatch_t ovl_dispatch_plb[] = {
+	/* Dispatch for the hardware overlay */
+	{
+		NULL, /*blend_surf_needed_plb,*/
+		alter_ovl_plb,
+		query_ovl_plb,
+		query_max_size_ovl_plb,
+	},
+	/* Dispatch for the software overlay */
+	{
+		NULL, /*blend2_surf_needed_plb,*/
+		alter_ovl2_plb,
+		query_ovl2_plb,
+		query_max_size_ovl2_plb,
+	},
+};
+
+
+typedef struct _ovl_chipset_plb {
+	unsigned int	num_linebuf;
+	unsigned int	pixel_format;
+	unsigned int	max_width;
+	unsigned int	max_height;
+} ovl_chipset_plb_t;
+
+static ovl_chipset_plb_t ovl_chipset_plb[] = {
+	{OVL_CONFIG_THREE_LINE_BUFF,
+		(PF_DEPTH_16 | PF_TYPE_YUV_PACKED), 1280, 1080},
+	{OVL_CONFIG_THREE_LINE_BUFF,
+		(PF_DEPTH_8 | PF_TYPE_YUV_PLANAR), 1024, 1080},
+	{OVL_CONFIG_THREE_LINE_BUFF,
+		(PF_DEPTH_32 | PF_TYPE_ARGB), 640, 1080},
+	{OVL_CONFIG_THREE_LINE_BUFF,
+		(PF_DEPTH_32 | PF_TYPE_RGB), 640, 1080},
+
+	{OVL_CONFIG_TWO_LINE_BUFF,
+		(PF_DEPTH_16 | PF_TYPE_YUV_PACKED), 1920, 1088},
+	{OVL_CONFIG_TWO_LINE_BUFF,
+		(PF_DEPTH_8 | PF_TYPE_YUV_PLANAR), 1920, 1088},
+	{OVL_CONFIG_TWO_LINE_BUFF,
+		(PF_DEPTH_32 | PF_TYPE_ARGB), 960, 1080},
+	{OVL_CONFIG_TWO_LINE_BUFF,
+		(PF_DEPTH_32 | PF_TYPE_RGB), 960, 1080},
+	{OVL_CONFIG_NO_LINE_BUFF, 0, 0, 0}
+};
+
+#ifdef DEBUG_BUILD_TYPE
+
+static void ovl_dump_regs_plb(
+	ovl_reg_image_plb_t *ovl_regs_plb)
+{
+	return;
+
+	printk (KERN_ERR "************************************************\n");
+	printk (KERN_ERR "OVERLAY REGISTER LISTING\n");
+
+	printk (KERN_ERR "OVL REGS AT  =    0x%x  \n", (unsigned int)ovl_regs_plb);
+	if(ovl_regs_plb->buffer0_yrgb_ptr)
+		printk (KERN_ERR "RGB POINTER 0    =     0x%x  \n",
+			ovl_regs_plb->buffer0_yrgb_ptr);
+	if(ovl_regs_plb->buffer1_yrgb_ptr)
+		printk (KERN_ERR "RGB POINTER 1    =     0x%x  \n",
+			ovl_regs_plb->buffer1_yrgb_ptr);
+	if(ovl_regs_plb->buffer0_u_ptr)
+		printk (KERN_ERR "U POINTER 0      =     0x%x  \n",
+			ovl_regs_plb->buffer0_u_ptr);
+	if(ovl_regs_plb->buffer0_v_ptr)
+		printk (KERN_ERR "V POINTER 0      =     0x%x  \n",
+			ovl_regs_plb->buffer0_v_ptr);
+	if(ovl_regs_plb->buffer1_u_ptr)
+		printk (KERN_ERR "U POINTER 1      =     0x%x  \n",
+			ovl_regs_plb->buffer1_u_ptr);
+	if(ovl_regs_plb->buffer1_v_ptr)
+		printk (KERN_ERR "V POINTER 1      =     0x%x  \n",
+			ovl_regs_plb->buffer1_v_ptr);
+
+	printk (KERN_ERR "RGB STRIDE       =     0x%x  \n",
+		ovl_regs_plb->yrgb_stride);
+	printk (KERN_ERR "UV STRIDE        =     0x%x  \n",
+		ovl_regs_plb->uv_stride);
+	printk (KERN_ERR "DST POS X        =     %d  \n",
+		ovl_regs_plb->dest_pos_x_left);
+	printk (KERN_ERR "DST POS Y        =     %d  \n",
+		ovl_regs_plb->dest_pos_y_top);
+	printk (KERN_ERR "DST WIDTH        =     %d  \n",
+		ovl_regs_plb->dest_width_x);
+	printk (KERN_ERR "DST HEIGHT       =     %d  \n",
+		ovl_regs_plb->dest_height_y);
+	printk (KERN_ERR "SRC WIDTH        =     %d  \n",
+		ovl_regs_plb->source_yrgb_width);
+	printk (KERN_ERR "SRC SWWIDTH      =     0x%x  \n",
+		ovl_regs_plb->source_yrgb_width_swords);
+	printk (KERN_ERR "SRC HEIGHT       =     %d  \n",
+		ovl_regs_plb->source_yrgb_height);
+	printk (KERN_ERR "UV SRC WIDTH     =     %d  \n",
+		ovl_regs_plb->source_uv_width);
+	printk (KERN_ERR "UV SRC SWWIDTH   =     %d  \n",
+		ovl_regs_plb->source_uv_width_swords);
+	printk (KERN_ERR "UV SRC HEIGHT    =     %d  \n",
+		ovl_regs_plb->source_uv_height);
+	printk (KERN_ERR "RGB SCALE        =     0x%x  \n",
+		ovl_regs_plb->yrgb_scale);
+	printk (KERN_ERR "UV SCALE         =     0x%x  \n",
+		ovl_regs_plb->uv_scale);
+	printk (KERN_ERR "COL CTL BRT CON  =     0x%x  \n",
+		ovl_regs_plb->col_ctl_brt_con);
+	printk (KERN_ERR "COL CTL SAT HUE  =     0x%x  \n",
+		ovl_regs_plb->col_ctl_sat_hue);
+	printk (KERN_ERR "DST COLOR KEY    =     0x%x  \n",
+		ovl_regs_plb->dest_ckey_val);
+	printk (KERN_ERR "DST COLOR KEY MASK =   0x%x  \n",
+		ovl_regs_plb->dest_ckey_mask);
+	printk (KERN_ERR "SRC COLOR KEY HI =     0x%x  \n",
+		ovl_regs_plb->source_ckey_high);
+	printk (KERN_ERR "SRC COLOR KEY LO =     0x%x  \n",
+		ovl_regs_plb->source_ckey_low);
+	printk (KERN_ERR "SRC COLOR KEY MASK =   0x%x  \n",
+		ovl_regs_plb->source_ckey_mask);
+	printk (KERN_ERR "OVL CONFIG       =     0x%x  \n", ovl_regs_plb->config);
+	printk (KERN_ERR "OVL CMD          =     0x%x  \n", ovl_regs_plb->command);
+	printk (KERN_ERR "FAST_V_DSCALE    =     0x%x  \n",
+		ovl_regs_plb->vert_downscale);
+	printk (KERN_ERR "************************************************\n");
+}
+#endif
+
+/*----------------------------------------------------------------------
+ * Function: ovl_check_pf_plb()
+ * Parameters: unsigned int requested_pixel_format -
+ *             according to definitions in igd_mode.h
+ *
+ * Description:
+ *
+ * Returns:
+ *   TRUE on Success
+ *   FALSE on The first pixel format that is supported
+ *----------------------------------------------------------------------*/
+static unsigned int ovl_check_pf_plb(
+	igd_display_context_t *display,
+	unsigned int requested_pixel_format)
+{
+	unsigned long *overlay_pfs;
+	int temp_loop = 0;
+
+	display->context->dispatch.get_pixelformats(
+		(igd_display_h)display, NULL, NULL, &overlay_pfs, NULL, NULL);
+
+	while(overlay_pfs[temp_loop]) {
+		if(overlay_pfs[temp_loop] == requested_pixel_format) {
+			return TRUE;
+		}
+		++temp_loop;
+	}
+
+	return FALSE;
+}
+
+static unsigned int get_uv_shift_x (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV422_PACKED_YUY2:
+	case IGD_PF_YUV422_PACKED_UYVY:
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+static unsigned int get_uv_shift_y (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+static unsigned int ovl_check_plb(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	igd_timing_info_t *timing;
+	ovl_chipset_plb_t *ovl_chip;
+	unsigned int min_w, min_h;
+
+	EMGD_TRACE_ENTER;
+
+	if (!display){
+		EMGD_ERROR_EXIT("display is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (!PIPE(display)){
+		EMGD_ERROR_EXIT("PIPE(display) is null");
+		return -IGD_ERROR_INVAL;
+	}
+
+	timing = PIPE(display)->timing;
+
+	/* DCT-PC99TA crashes with dotclock > 300MHz */
+	if(timing->dclk >= 340000){
+		EMGD_ERROR_EXIT("Cannot support dotclock > 340MHz for this SKU");
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* The following parameters are only valid if the overlay is on, so
+	 * return success if the overlay is being turned off. */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Ensure the overlay surface is ok and can be properly displayed.
+	 * This ensures the following is valid:
+	 *    - Ensure x1, x2, y1, y2 are pixel aligned
+	 *    - 2 pixels or greater in width and height
+	 *    - Pixel format is supported by the overlay
+	 *    - Pitch is <= 8KB
+	 *    - Based on the pixel format, the width is supported
+	 *************************************************************************/
+	if (!src_surf){
+		EMGD_ERROR_EXIT("src_surf is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (!src_rect){
+		EMGD_ERROR_EXIT("src_rect is null");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/* Get the minimum size of 1 pixel in width and height for y, u, and v.
+	 */
+	min_w = 1 << get_uv_shift_x(src_surf->pixel_format);
+	min_h = 1 << get_uv_shift_y(src_surf->pixel_format);
+
+	if (((src_rect->x2 - src_rect->x1) < min_w*2) ||
+		((src_rect->y2 - src_rect->y1) < min_h*2)) {
+		EMGD_ERROR_EXIT(
+			"Overlay source width or height is < 2 pixels (%dx%d)\n",
+			src_rect->x2 - src_rect->x1, src_rect->y2 - src_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (FALSE == ovl_check_pf_plb(display, src_surf->pixel_format)) {
+		EMGD_ERROR_EXIT("Overlay source pixel format unsupported (pf:0x%lx)",
+			src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	if (src_surf->pitch > 8192) {
+		EMGD_ERROR_EXIT("Overlay source pitch (%d) > 8KB",
+			src_surf->pitch);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	ovl_chip = ovl_chipset_plb;
+	while(ovl_chip->num_linebuf != OVL_CONFIG_NO_LINE_BUFF){
+		if(((src_surf->pixel_format & IGD_PF_MASK) ==
+				ovl_chip->pixel_format) &&
+			(src_surf->width <= ovl_chip->max_width)) {
+			break;
+		}
+		ovl_chip++;
+	}
+	if (ovl_chip->num_linebuf == OVL_CONFIG_NO_LINE_BUFF) {
+		EMGD_ERROR_EXIT("Overlay source width (%d) > max supported",
+			src_surf->width);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/*************************************************************************
+	 * Ensure the location on the framebuffer is ok and can be properly
+	 * displayed
+	 * This ensures the following is valid:
+	 *    - Greater than 1 pixel width and height
+	 *    - Will be displayed on screen (not panned off)
+	 *************************************************************************/
+	if (!dest_rect){
+		EMGD_ERROR_EXIT("dest_rect is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (((dest_rect->x2 - dest_rect->x1) <= 1) ||
+		((dest_rect->y2 - dest_rect->y1) <= 1)) {
+		EMGD_ERROR_EXIT(
+			"Overlay dest width or height is single pixel (%dx%d)\n",
+			dest_rect->x2 - dest_rect->x1, dest_rect->y2 - dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((dest_rect->x1 >= timing->width) ||
+		(dest_rect->y1 >= timing->height)) {
+		EMGD_ERROR_EXIT(
+			"Overlay dest is panned off the screen (%d,%d)\n",
+			dest_rect->x1, dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+static unsigned int ovl_update_src_plb(igd_display_context_t *display,
+	ovl_reg_image_plb_t *ovl_regs_plb,
+	igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	ovl_chipset_plb_t *ovl_chip;
+	unsigned int      src_Bpp;
+	unsigned int      src_uv_shift_x, src_uv_shift_y;
+	unsigned short    src_w;
+	unsigned short    src_h;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format)/8;
+
+	src_uv_shift_x = get_uv_shift_x(src_surf->pixel_format);
+	src_uv_shift_y = get_uv_shift_y(src_surf->pixel_format);
+
+	src_w = src_rect->x2 - src_rect->x1;
+	src_h = src_rect->y2 - src_rect->y1;
+
+	ovl_regs_plb->yrgb_stride      = (unsigned short)src_surf->pitch;
+	ovl_regs_plb->uv_stride        = (unsigned short)src_surf->u_pitch;
+
+	/* src width */
+	ovl_regs_plb->source_yrgb_width      = src_w;
+	ovl_regs_plb->source_uv_width        = src_w >> src_uv_shift_x;
+
+	/* src width swords - This equation follows the B-Spec */
+	ovl_regs_plb->source_yrgb_width_swords =
+		((((((ovl_regs_plb->buffer0_yrgb_ptr +
+		      (ovl_regs_plb->source_yrgb_width * src_Bpp) +
+		      0x3F) >> 6) -
+		    (ovl_regs_plb->buffer0_yrgb_ptr >> 6)) << 1) - 1) << 2);
+	ovl_regs_plb->source_uv_width_swords   =
+		((((((ovl_regs_plb->buffer0_u_ptr +
+		      (ovl_regs_plb->source_uv_width * src_Bpp) +
+		      0x3F) >> 6) -
+		    (ovl_regs_plb->buffer0_u_ptr >> 6)) << 1) - 1) << 2 );
+
+
+	/* src height */
+	ovl_regs_plb->source_yrgb_height     = src_h;
+	ovl_regs_plb->source_uv_height       = src_h >> src_uv_shift_y;
+
+	/* src pixel format */
+	switch(src_surf->pixel_format){
+	case IGD_PF_YUV422_PACKED_YUY2:
+		ovl_regs_plb->command |= OVL_CMD_YUV_422;
+		break;
+	case IGD_PF_YUV422_PACKED_UYVY:
+		ovl_regs_plb->command |= OVL_CMD_YUV_422 | OVL_CMD_Y_SWAP;
+		break;
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+		ovl_regs_plb->command |= OVL_CMD_YUV_420P;
+		break;
+	case IGD_PF_YUV420_PLANAR_YV12:
+		ovl_regs_plb->command |= OVL_CMD_YUV_420P | OVL_CMD_UV_SWAP;
+		break;
+	case IGD_PF_YUV420_PLANAR_NV12:
+		ovl_regs_plb->command |= OVL_CMD_YUV_NV12;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		ovl_regs_plb->command |= OVL_CMD_YUV_410P;
+		break;
+	case IGD_PF_ARGB32_8888:
+	case IGD_PF_xRGB32_8888:
+		ovl_regs_plb->command |= OVL_CMD_RGB_8888;
+		break;
+	case IGD_PF_RGB16_565:
+		ovl_regs_plb->command |= OVL_CMD_RGB_565;
+		break;
+	case IGD_PF_xRGB16_555:
+	case IGD_PF_ARGB16_1555:
+		ovl_regs_plb->command |= OVL_CMD_RGB_555;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid pixel format: 0x%lx", src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Turn off YUV to RGB conversion if the src is RGB */
+	if (!(src_surf->pixel_format & PF_TYPE_YUV)) {
+		ovl_regs_plb->config |= (1<<4);
+	}
+
+	ovl_chip = ovl_chipset_plb;
+	ovl_regs_plb->config &= ~OVL_CONFIG_LINE_BUFF_MASK;
+	while(ovl_chip->num_linebuf != OVL_CONFIG_NO_LINE_BUFF){
+		if(((src_surf->pixel_format & IGD_PF_MASK) ==
+				ovl_chip->pixel_format) &&
+			(src_w <= ovl_chip->max_width)) {
+			ovl_regs_plb->config |= ovl_chip->num_linebuf;
+			break;
+		}
+		ovl_chip++;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+static unsigned int ovl_update_src_ptr_plb(igd_display_context_t *display,
+	ovl_reg_image_plb_t *ovl_regs_plb,
+        igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	unsigned int      src_Bpp;
+	unsigned int      src_uv_shift_x, src_uv_shift_y;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format)/8;
+	src_uv_shift_x = get_uv_shift_x(src_surf->pixel_format);
+	src_uv_shift_y = get_uv_shift_y(src_surf->pixel_format);
+
+	/* src surface */
+	ovl_regs_plb->buffer0_yrgb_ptr     =
+		ovl_regs_plb->buffer1_yrgb_ptr =
+		src_surf->offset +
+		(src_rect->y1 * src_surf->pitch) + (src_rect->x1 * src_Bpp);
+
+	/*
+	 * The NV12 format has the UV pixels interleaved so the total
+	 * width of the UV portion of the surface is the same as the
+	 * Y width. Thus, don't do any shifting of the UV plane in the
+	 * X direction.
+	 */
+	if (src_surf->pixel_format == IGD_PF_YUV420_PLANAR_NV12) {
+		ovl_regs_plb->buffer0_u_ptr     =
+			ovl_regs_plb->buffer1_u_ptr =
+			src_surf->u_offset +
+			((src_rect->y1>>src_uv_shift_y) * src_surf->u_pitch) +
+			src_rect->x1;
+
+		ovl_regs_plb->buffer0_v_ptr     =
+			ovl_regs_plb->buffer1_v_ptr =
+			src_surf->v_offset +
+			((src_rect->y1>>src_uv_shift_y) * src_surf->v_pitch) +
+			src_rect->x1;
+	} else {
+		ovl_regs_plb->buffer0_u_ptr     =
+			ovl_regs_plb->buffer1_u_ptr =
+			src_surf->u_offset +
+			((src_rect->y1>>src_uv_shift_y) * src_surf->u_pitch) +
+			(src_rect->x1>>src_uv_shift_x);
+
+		ovl_regs_plb->buffer0_v_ptr     =
+			ovl_regs_plb->buffer1_v_ptr =
+			src_surf->v_offset +
+			((src_rect->y1>>src_uv_shift_y) * src_surf->v_pitch) +
+			(src_rect->x1>>src_uv_shift_x);
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+static unsigned int ovl_update_phase_plb(
+	ovl_reg_image_plb_t *ovl_regs_plb,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect)
+{
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Set the Vertical/Horizontal Phase Registers.  Both Field0
+	 * and Field1 are set, although Field1 is only used when
+	 * interleaved.
+	 */
+	switch (src_surf->pixel_format) {
+	case IGD_PF_YUV422_PACKED_YUY2:
+	case IGD_PF_YUV422_PACKED_UYVY:
+		/* YUV 422 */
+		ovl_regs_plb->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_plb->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			ovl_regs_plb->yrgb_vert_phase_field0 = 0;
+			ovl_regs_plb->yrgb_vert_phase_field1 = 0;
+			ovl_regs_plb->uv_vert_phase_field0 = 0;
+			ovl_regs_plb->uv_vert_phase_field1 = 0;
+		} else {
+			ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000; /*.5*/
+			ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000; /*.5*/
+			ovl_regs_plb->uv_vert_phase_field0   = 0x8000; /*.5*/
+			ovl_regs_plb->uv_vert_phase_field1   = 0x8000; /*.5*/
+		}
+
+		/* Horizontal Phase */
+		if (!(src_rect->x1 & 1)) {
+			ovl_regs_plb->yrgb_hphase = 0;
+			ovl_regs_plb->uv_hphase = 0;
+		} else {
+			ovl_regs_plb->init_phase_shift |= Y_HPP_PLUS1;
+			ovl_regs_plb->yrgb_hphase = 0; /*1*/
+			ovl_regs_plb->uv_hphase = 0x8000; /*.5*/
+		}
+		break;
+
+	case IGD_PF_YUV420_PLANAR_I420:
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		/* YUV 420 */
+		ovl_regs_plb->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_plb->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			if (!(src_rect->y1 & 1)) {
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0; /*0*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field0 = 0xc000; /*-.25*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0x4000; /*.25*/
+			} else {
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0;/*0*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x4000; /*.25*/
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field1 = 0xc000; /*-.25*/
+			}
+		} else {
+			if (!(src_rect->y1 & 1)) {
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000; /*.5*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000;/*1.5*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x4000; /*.25*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0xc000; /*.75*/
+			} else {
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000; /*.5*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0xc000; /*.75*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0x4000; /*.25*/
+			}
+		}
+
+		/* Horizontal Phase */
+		if (!(src_rect->x1 & 1)) {
+			ovl_regs_plb->yrgb_hphase = 0;
+			ovl_regs_plb->uv_hphase = 0;
+		} else {
+			ovl_regs_plb->init_phase_shift |= Y_HPP_PLUS1;
+			ovl_regs_plb->yrgb_hphase = 0; /*1*/
+			ovl_regs_plb->uv_hphase = 0x8000; /*.5*/
+		}
+		break;
+
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		/* YUV 410 */
+		ovl_regs_plb->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_plb->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			switch (src_rect->y1 & 3) {
+			default:
+			case 0:
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0; /*0*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field0 = 0xa000; /*-.375*/
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field1 = 0xe000; /*-.125*/
+				break;
+
+			case 1:
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field0 = 0xe000; /*-.125*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0x2000; /*.125*/
+				break;
+
+			case 2:
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x2000; /*.125*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0x6000; /*.375*/
+				break;
+
+			case 3:
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0;/*0*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x6000; /*.375*/
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field1 = 0xa000; /*-.375*/
+				break;
+			}
+		} else {
+			switch (src_rect->y1 & 3) {
+			default:
+			case 0:
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000; /*.5*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000;/*1.5*/
+
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field0 = 0xc000; /*-.25*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0; /*0*/
+				break;
+
+			case 1:
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000; /*1.5*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x0; /*0*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0x4000; /*.25*/
+				break;
+
+			case 2:
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000; /*1.5*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x4000; /*.25*/
+				ovl_regs_plb->uv_vert_phase_field1 = 0x8000; /*.5*/
+				break;
+
+			case 3:
+				ovl_regs_plb->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000; /*.5*/
+
+				ovl_regs_plb->uv_vert_phase_field0 = 0x8000; /*.5*/
+				ovl_regs_plb->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_plb->uv_vert_phase_field1 = 0xc000; /*-.25*/
+				break;
+			}
+		}
+
+		/* Horizontal Phase */
+		switch (src_rect->x1 & 3) {
+		default:
+		case 0:
+			ovl_regs_plb->yrgb_hphase = 0;
+			ovl_regs_plb->init_phase_shift |= UV_HPP_MINUS1;
+			ovl_regs_plb->uv_hphase = 0xa000; /*-.375*/
+			break;
+
+		case 1:
+			ovl_regs_plb->init_phase_shift |= Y_HPP_PLUS1;
+			ovl_regs_plb->yrgb_hphase = 0; /*1*/
+			ovl_regs_plb->init_phase_shift |= UV_HPP_MINUS1;
+			ovl_regs_plb->uv_hphase = 0xe000; /*-.125*/
+			break;
+
+		case 2:
+			ovl_regs_plb->init_phase_shift |= Y_HPP_PLUS2;
+			ovl_regs_plb->yrgb_hphase = 0; /*2*/
+			ovl_regs_plb->uv_hphase = 0x2000; /*.125*/
+			break;
+
+		case 3:
+			ovl_regs_plb->init_phase_shift |= Y_HPP_PLUS2;
+			ovl_regs_plb->yrgb_hphase = 0xffff; /*3*/
+			ovl_regs_plb->uv_hphase = 0x6000; /*.375*/
+			break;
+		}
+		break;
+
+	default:
+		/* RGB format */
+		ovl_regs_plb->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_plb->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			ovl_regs_plb->yrgb_vert_phase_field0 = 0;
+			ovl_regs_plb->yrgb_vert_phase_field1 = 0;
+			ovl_regs_plb->uv_vert_phase_field0 = 0;
+			ovl_regs_plb->uv_vert_phase_field1 = 0;
+		} else {
+			ovl_regs_plb->yrgb_vert_phase_field0 = 0x8000;
+			ovl_regs_plb->yrgb_vert_phase_field1 = 0x8000;
+			ovl_regs_plb->uv_vert_phase_field0   = 0x8000;
+			ovl_regs_plb->uv_vert_phase_field1   = 0x8000;
+		}
+
+		/* Horizontal Phase */
+		ovl_regs_plb->yrgb_hphase = 0;
+		ovl_regs_plb->uv_hphase = 0;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_scale_plb()
+ *
+ * Description: Will update only the scaling registers for the plb core
+ *
+ * Returns:
+ *   N/A
+ *----------------------------------------------------------------------*/
+
+static unsigned int ovl_update_scale_plb(
+	ovl_reg_image_plb_t *ovl_regs_plb,
+	igd_surface_t *src_surf,
+	igd_rect_t   *src_rect,
+	igd_rect_t   *dest_rect,
+	unsigned int flags)
+{
+	unsigned int uv_shift;
+	unsigned int xscale, xscale_int, xscale_fract;
+	unsigned int yscale, yscale_int, yscale_fract;
+	unsigned int xscale_int_uv, xscale_fract_uv;
+	unsigned int yscale_int_uv, yscale_fract_uv;
+
+	EMGD_TRACE_ENTER;
+
+	xscale = ((src_rect->x2 - src_rect->x1)<<12) /
+		(dest_rect->x2 - dest_rect->x1);
+	yscale = ((src_rect->y2 - src_rect->y1)<<12) /
+		(dest_rect->y2 - dest_rect->y1);
+
+	/* In interleaved mode, the y scale is /2 */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		yscale >>= 1;
+	}
+
+	xscale_int = (xscale & 0x3000) >> 12;
+	xscale_fract = xscale & 0xfff;
+	yscale_int = (yscale & 0x7ff000) >> 12;
+	yscale_fract = yscale & 0xfff;
+
+	uv_shift = get_uv_shift_x(src_surf->pixel_format);
+	xscale_int_uv = ((xscale>>uv_shift) & 0x3000) >> 12;
+	xscale_fract_uv = (xscale>>uv_shift) & 0xfff;
+
+	uv_shift = get_uv_shift_y(src_surf->pixel_format);
+	yscale_int_uv = ((yscale>>uv_shift) & 0x7ff000) >> 12;
+	yscale_fract_uv = (yscale>>uv_shift) & 0xfff;
+
+	ovl_regs_plb->yrgb_scale =
+		(yscale_fract  << 20) |   /* Vert  Scale Fraction */
+		(xscale_int    << 16) |   /* Horiz Scale Int */
+		(xscale_fract  << 3);     /* Horiz Scale Fraction */
+
+	ovl_regs_plb->uv_scale =
+		(yscale_fract_uv << 20) | /* UV Vert  Scale Fraction */
+		(xscale_int_uv   << 16) | /* UV Horiz Scale Int */
+		(xscale_fract_uv << 3 );  /* UV Horiz Scale Fraction */
+	ovl_regs_plb->vert_downscale =
+		(yscale_int    << 16) |   /* Vert Scale Factor */
+		yscale_int_uv;            /* UV Vert Scale Factor */
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_video_quality_plb()
+ *
+ * Description:
+ *   This function updates the contrast, brightness, and saturation of
+ *   the overlay using the values specified in overlay_info.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static int ovl_update_video_quality_plb(
+	ovl_reg_image_plb_t *ovl_regs_plb,
+	igd_surface_t       *src_surf,
+	igd_ovl_video_quality_info_t *video_quality)
+{
+	int                          calc_brightness_tmp = 0;
+	int                          calc_brightness     = 0;
+	unsigned int                 calc_contrast_tmp   = 0;
+	unsigned int                 calc_contrast       = 0;
+	unsigned int                 calc_saturation_tmp = 0;
+	unsigned int                 calc_saturation     = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* If the src_surf pixel format is RGB, then brightness, contrast,
+	 * and saturation should all be set to the exact default */
+	if (src_surf->pixel_format & PF_TYPE_RGB) {
+		if (video_quality->brightness != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set brightness to default");
+		}
+		if (video_quality->contrast != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set contrast to default");
+		}
+		if (video_quality->saturation != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set saturation to default");
+		}
+
+		ovl_regs_plb->col_ctl_brt_con = OVL_RGB_COLOR_DEF_CONT_BRGHT;
+		ovl_regs_plb->col_ctl_sat_hue = OVL_RGB_COLOR_DEF_SATN_HUE;
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Brightness
+	 *************************************************************************/
+	if (0x8000 == video_quality->brightness) {
+		calc_brightness = MID_BRIGHTNESS_YUV;
+	} else if (video_quality->brightness < 0x8000) {
+		/*
+		 * we have here a brightness that is less than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = 0x8000 - video_quality->brightness;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = -128 - MID_BRIGHTNESS_YUV;
+		/*
+		 * more range if the midpoint is positive but less range
+		 * if midpoint is negative
+		 */
+
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness < -128) {
+			calc_brightness = -128;
+		}
+		if (calc_brightness > MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	} else {
+		/*
+		 * we have here a brightness that is more than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = video_quality->brightness - 0x8000;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = 127 - MID_BRIGHTNESS_YUV;
+		/*
+		 * less range if the midpoint is positive but more range
+		 * if midpoint is negative
+		 */
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness > 127) {
+			calc_brightness = 127;
+		}
+		if (calc_brightness < MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	}
+
+	ovl_regs_plb->col_ctl_brt_con =
+		(ovl_regs_plb->col_ctl_brt_con & 0xFFFFFF00) |
+		(calc_brightness & 0xFF);
+
+	/*************************************************************************
+	 * Contrast
+	 *************************************************************************/
+	if (0x8000 == video_quality->contrast ){
+		calc_contrast = MID_CONTRAST_YUV;
+	} else if (video_quality->contrast < 0x8000) {
+		/* we have here a contrast that is less than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = MID_CONTRAST_YUV;
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		if (calc_contrast > 0x3F) {
+			calc_contrast = 0x3F;
+		}
+	} else {
+		/* we have here a contrast that is more than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast - 0x8000;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = (0x1FF - MID_CONTRAST_YUV);
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		calc_contrast     += MID_CONTRAST_YUV;
+		if (calc_contrast > 0x1FF) {
+			calc_contrast = 0x1FF;
+		}
+	}
+
+	ovl_regs_plb->col_ctl_brt_con =
+		(ovl_regs_plb->col_ctl_brt_con & 0xF803FFFF) |
+		((calc_contrast & 0x1FF) << 18);
+
+	/*************************************************************************
+	 * Saturation
+	 *************************************************************************/
+	if (video_quality->saturation == 0x8000) {
+		calc_saturation = MID_SATURATION_YUV;
+	} else if (video_quality->saturation < 0x8000) {
+		/* we have here a saturation that is less than the default
+		 * mid point */
+		calc_saturation_tmp = video_quality->saturation;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = MID_SATURATION_YUV;
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		if (calc_saturation > 0x7F) {
+			calc_saturation = 0x7F;
+		}
+	} else {
+		/* we have here a saturation that is more than the default
+		 * mid point*/
+		calc_saturation_tmp = video_quality->saturation - 0x8000;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = (0x3FF - MID_SATURATION_YUV);
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		calc_saturation     += MID_SATURATION_YUV;
+
+		if (calc_saturation > 0x3FF) {
+			calc_saturation = 0x3FF;
+		}
+	}
+
+	ovl_regs_plb->col_ctl_sat_hue =
+		(ovl_regs_plb->col_ctl_sat_hue & 0xFFFFFC00) |
+		(calc_saturation & 0x3FF);
+
+	/*************************************************************************
+	 * Hue
+	 *************************************************************************/
+	/* Hue is always set to the default value.  It is based on the saturation
+	 * value, and having a separate hue is of minimal value. */
+	ovl_regs_plb->col_ctl_sat_hue =
+		(ovl_regs_plb->col_ctl_sat_hue & 0xF800FFFF);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+static void check_gamma(unsigned int *gamma)
+{
+
+	if (*gamma < IGD_OVL_GAMMA_MIN) {
+		EMGD_ERROR("Gamma to small (0x%x in 24i.8f format), "
+			"changing to Min Gamma (0.6)",
+			*gamma);
+		*gamma = IGD_OVL_GAMMA_MIN;
+	}
+	if (*gamma > IGD_OVL_GAMMA_MAX) {
+		EMGD_ERROR("Gamma to large (0x%x in 24i.8f format), "
+			"changing to Max Gamma (6.0)",
+			*gamma);
+		*gamma = IGD_OVL_GAMMA_MAX;
+	}
+
+	return;
+}
+
+
+
+/*-----------------------------------------------------------------------------
+ * Function: ovl_update_gamma_plb()
+ *
+ * Description:
+ *    This function sets the gamma correction values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static int ovl_update_gamma_plb(
+	igd_display_context_t *display,
+	igd_ovl_gamma_info_t * ovl_gamma)
+{
+	const int gamma_reg_input[OVL_TOTAL_GAMMA_REG] = {8, 16, 32, 64, 128, 192};
+	const int gamma_reg_offset[OVL_TOTAL_GAMMA_REG] = {
+		OVL_REG_ADDR_GAMMA0,
+		OVL_REG_ADDR_GAMMA1,
+		OVL_REG_ADDR_GAMMA2,
+		OVL_REG_ADDR_GAMMA3,
+		OVL_REG_ADDR_GAMMA4,
+		OVL_REG_ADDR_GAMMA5
+	};
+	const unsigned int gamma_def[OVL_TOTAL_GAMMA_REG] = {
+		0x00080808,
+		0x00101010,
+		0x00202020,
+		0x00404040,
+		0x00808080,
+		0x00c0c0c0
+	};
+	unsigned int          new_gamma_red_24i_8f, new_gamma_green_24i_8f;
+	unsigned int          new_gamma_blue_24i_8f;
+	unsigned int          gamma_normal_r_24i_8f;
+	unsigned int          gamma_normal_g_24i_8f;
+	unsigned int          gamma_normal_b_24i_8f;
+	unsigned int          gamma_reg, gamma_reg_24i_8f;
+	unsigned int          i;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The gamma values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* If the overlay gamma is disabled, set it to the default */
+	if ((ovl_gamma->flags & IGD_OVL_GAMMA_ENABLE) == IGD_OVL_GAMMA_DISABLE) {
+		for (i = 0; i < OVL_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			EMGD_WRITE32(gamma_def[i], MMIO(display) + gamma_reg_offset[i]);
+		}
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* It is assumed that the input value is a 24-bit number */
+	new_gamma_red_24i_8f   = ovl_gamma->red;
+	new_gamma_green_24i_8f = ovl_gamma->green;
+	new_gamma_blue_24i_8f  = ovl_gamma->blue;
+
+	/* Ensure the gamma values are between MIN and MAX */
+	check_gamma(&new_gamma_red_24i_8f);
+	check_gamma(&new_gamma_green_24i_8f);
+	check_gamma(&new_gamma_blue_24i_8f);
+
+	/*
+	 * Program RGB for each of the 6 gamma registers
+	 */
+
+	/* Since the OS_POW_FIX function can only take an integer base,
+	 * we need to normalize the result by gamma_normal_x
+	 */
+	gamma_normal_r_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_red_24i_8f);
+	gamma_normal_g_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_green_24i_8f);
+	gamma_normal_b_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_blue_24i_8f);
+
+	for( i = 0; i < OVL_TOTAL_GAMMA_REG; i++ )
+	{
+		/* red */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+								(1<<16)/new_gamma_red_24i_8f);
+		gamma_reg        =
+			((255 * gamma_reg_24i_8f) / gamma_normal_r_24i_8f) << 16;
+
+		/* green */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_green_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_g_24i_8f) << 8;
+
+		/* blue */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_blue_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_b_24i_8f);
+
+		/* turn overlay off (TBD) */
+
+		/* program register */
+		EMGD_WRITE32(gamma_reg, MMIO(display) + gamma_reg_offset[i]);
+
+		/* turn overlay on (TBD) */
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_coeff_plb()
+ * Description: Function to calculate the filter coeffcient
+ *              registers for plb
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+static unsigned int ovl_update_coeff_plb(
+	ovl_reg_image_plb_t *ovl_regs_plb,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	unsigned int        flags)
+{
+	unsigned int scale_int, scale_fpint;
+
+	unsigned int dest_h = dest_rect->y2 - dest_rect->y1;
+	unsigned int dest_w = dest_rect->x2 - dest_rect->x1;
+	unsigned int src_h  = src_rect->y2  - src_rect->y1;
+	unsigned int src_w  = src_rect->x2  - src_rect->x1;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The coeff values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* In interleaved mode, the src_h is /2 */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		src_h >>= 1;
+	}
+
+	/* Y Horizontal */
+	scale_int = ((ovl_regs_plb->yrgb_scale) >> 16) & 0x7;
+
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_w << 20) / dest_w) / scale_int;
+	}
+    ovl_update_coeff_regs(5, scale_fpint, 1, 1,
+                          (unsigned short *)ovl_regs_plb->y_horz_coeff_single);
+
+	/* Y Vertical */
+	scale_int = ((ovl_regs_plb->vert_downscale) >> 16) & 0x7ff;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_h << 20) / dest_h) / scale_int;
+	}
+	ovl_update_coeff_regs(3, scale_fpint, 0, 1,
+                          (unsigned short *)ovl_regs_plb->y_vert_coeff_single);
+
+	/* UV Horizontal */
+	scale_int = ((ovl_regs_plb->uv_scale) >> 16) & 0x7;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_w << 20) / dest_w) / scale_int;
+		scale_fpint >>= get_uv_shift_x(src_surf->pixel_format);
+	}
+	ovl_update_coeff_regs(3, scale_fpint , 1, 0,
+                          (unsigned short *)ovl_regs_plb->uv_horz_coeff_single);
+
+	/* UV Vertical */
+	scale_int = (ovl_regs_plb->vert_downscale) & 0x7ff;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_h << 20) / dest_h) / scale_int;
+		scale_fpint >>= get_uv_shift_y(src_surf->pixel_format);
+	}
+	ovl_update_coeff_regs(3, scale_fpint, 0, 0,
+                          (unsigned short *)ovl_regs_plb->uv_vert_coeff_single);
+
+	/* Adjust for 2-line Vertical Buffer */
+	if((ovl_regs_plb->config & OVL_CONFIG_LINE_BUFF_MASK)==
+		OVL_CONFIG_TWO_LINE_BUFF){
+		ovl_update_coeff_regs(2, 0x10, 0, 1,
+			(unsigned short *)ovl_regs_plb->y_vert_coeff_single);
+		ovl_update_coeff_regs(2, 0x10, 0, 0,
+			(unsigned short *)ovl_regs_plb->uv_vert_coeff_single);
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+static unsigned int convert_color_key_to_hw (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	case IGD_PF_ARGB8_INDEXED:
+	default:
+		output = input;
+		break;
+	case IGD_PF_RGB16_565:
+		output =
+			((((input & 0xf800)>>11)<<3)<<16) |
+			((((input & 0x07e0)>>5 )<<2)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	case IGD_PF_ARGB16_1555:
+		output =
+			((((input & 0x7c00)>>10)<<3)<<16) |
+			((((input & 0x03e0)>>5 )<<3)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	}
+
+	return output;
+}
+static unsigned int convert_color_key_to_mask (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	default:
+		output = 0x00000000;
+		break;
+	case IGD_PF_RGB16_565:
+		output = 0x00070307;
+		break;
+	case IGD_PF_ARGB16_1555:
+		output = 0x00070707;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		output = 0x00ffff00;
+		break;
+	}
+
+	return output;
+}
+
+/* Convert RGB to GBR for 32bpp and 16bpp pixel formats.
+ * Do not convert YUV surfaces or RGB indexed pixel formats */
+static unsigned int rgb_to_gbr(
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	if (((pf & IGD_PF_TYPE_MASK) == PF_TYPE_ARGB) &&
+		((pf & IGD_PF_DEPTH_MASK) != PF_DEPTH_8)) {
+		output =
+			((input & 0x00ff0000) >> 16) |
+			((input & 0x0000ff00) << 8)  |
+			((input & 0x000000ff) << 8);
+	} else {
+		output = input;
+	}
+
+	return output;
+}
+
+
+#ifndef OVL_PLB_CACHE_QUICK_SWAP  /* If no OVL_PLB_CACHE_QUICK_SWAP */
+
+static unsigned int ovl_update_regs_plb(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl_reg_image_plb_t *ovl_regs_plb, *ovl_cache_plb ;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	ovl_regs_plb = phys_to_virt(ovl_context->reg_update_phys);
+	ovl_cache_plb = OS_ALLOC(sizeof(ovl_reg_image_plb_t));
+	OS_MEMSET(ovl_cache_plb, 0, sizeof(ovl_reg_image_plb_t));
+
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		/* Turn the overlay Off */
+		ovl_regs_plb->command = 0;
+		/* Always use buf 0 when turning the overlay off. */
+		ovl_context->ovl_buff = 0;
+		OS_FREE(ovl_cache_plb);
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* Force value of y1 to even due hardware expects even number */
+	dest_rect->y1 &= ~1;
+	dest_rect->y2 = (dest_rect->y2 + 1) & ~1;
+	dest_rect->x1 &= ~1;
+	dest_rect->x2 = (dest_rect->x2 + 1) & ~1;
+
+	/*************************************************************************
+	 * Copy the information passed in to the HW overlay structure
+	 *************************************************************************/
+	/* Zero the config and command, since they will be OR'ed in with data
+	 * below */
+	ovl_cache_plb->config = 0;
+	ovl_cache_plb->command = 0;
+
+	/* Set overlay to the proper pipe */
+	if (1 == PIPE(display)->pipe_num) {
+		/* This is pipe B */
+		ovl_cache_plb->config |= 1 << 18;
+	}
+
+	/* Interleaved/progressive and Odd/Even if interleaved */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		ovl_cache_plb->command |= OVL_CMD_FIELD_MODE;
+		/* Need to enable FIELD SYNC OVERLAY FLIP in field mode. */
+		ovl_cache_plb->command |= OVL_CMD_FIELD_SYNC_FLIP;
+		/* HSD# 203821 Mplayer outputs single one buffer including both even and odd fields */
+		if (flags & IGD_OVL_ALTER_FLIP_ODD) {
+			ovl_cache_plb->command |= OVL_CMD_ACT_FLD1;
+			/* HSD# 203821 To display odd field, starts from first odd field. */
+			if (0 == src_rect->y1 & 1) {
+				src_rect->y1 += 1;
+			}
+		} else {
+			ovl_cache_plb->command |= OVL_CMD_ACT_FLD0;
+			/* HSD# 203821 To display even field, starts from first even field. */
+			if (0 != src_rect->y1 & 1) {
+				src_rect->y1 += 1;
+			}
+		}
+	} else {
+		ovl_cache_plb->command |= OVL_CMD_FRAME_MODE;
+	}
+
+	/* Dest rect information */
+	ovl_cache_plb->dest_pos_x_left        = (unsigned short)dest_rect->x1;
+	ovl_cache_plb->dest_pos_y_top         = (unsigned short)dest_rect->y1;
+	ovl_cache_plb->dest_width_x           =
+		(unsigned short)(dest_rect->x2 - dest_rect->x1);
+	ovl_cache_plb->dest_height_y          =
+		(unsigned short)(dest_rect->y2 - dest_rect->y1);
+
+	/* Src rect and surface information */
+
+	ret = ovl_update_src_ptr_plb(display, ovl_cache_plb, src_surf, src_rect);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay updating src pointers failed");
+		return ret;
+	}
+
+	ret = ovl_update_src_plb(display, ovl_cache_plb, src_surf, src_rect);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay updating src failed");
+		return ret;
+	}
+
+	/* Scaling information including Vertical downscaling.
+	 * Scaling should be guaranteed to work, since if the scale is not
+	 * supported, it should have already been blended to a supported scale. */
+	ret = ovl_update_scale_plb(ovl_cache_plb, src_surf, src_rect, dest_rect,
+		flags);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay updating scaling failed");
+		return ret;
+	}
+
+	/* Color control information */
+	ret = ovl_update_video_quality_plb(ovl_cache_plb, src_surf,
+		&ovl_info->video_quality);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay video quality failed");
+		return ret;
+	}
+	ret = ovl_update_gamma_plb(display, &ovl_info->gamma);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay gamma failed");
+		return ret;
+	}
+
+	/* Destination color key */
+	EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Dest Color Key");
+		/* The mask and color key are different for the different
+		 * pixel formats */
+		ovl_cache_plb->dest_ckey_val = convert_color_key_to_hw(
+			PLANE(display)->fb_info->pixel_format,
+			ovl_info->color_key.dest);
+		ovl_cache_plb->dest_ckey_mask = convert_color_key_to_mask(
+			PLANE(display)->fb_info->pixel_format,
+			ovl_info->color_key.dest);
+		ovl_cache_plb->dest_ckey_mask |= 0x80000000;
+	} else {
+		EMGD_DEBUG("Overlay Disable Dest Color Key");
+		ovl_cache_plb->dest_ckey_mask = 0x00000000;
+	}
+
+	/* Source Color key */
+	if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Src Color Key");
+		ovl_cache_plb->source_ckey_high = convert_color_key_to_hw(
+			src_surf->pixel_format,
+			ovl_info->color_key.src_hi);
+		ovl_cache_plb->source_ckey_high = rgb_to_gbr(
+			src_surf->pixel_format,
+			ovl_cache_plb->source_ckey_high);
+
+		ovl_cache_plb->source_ckey_low = convert_color_key_to_hw(
+			src_surf->pixel_format,
+			ovl_info->color_key.src_lo);
+		ovl_cache_plb->source_ckey_low = rgb_to_gbr(
+			src_surf->pixel_format,
+			ovl_cache_plb->source_ckey_low);
+
+		ovl_cache_plb->source_ckey_mask = 0x07000000;
+	} else {
+		EMGD_DEBUG("Overlay Disable Src Color Key");
+		ovl_cache_plb->source_ckey_mask = 0x00000000;
+	}
+
+	/* Coefficients - Must be after Scaling */
+	ret = ovl_update_coeff_plb(ovl_cache_plb, src_surf, src_rect, dest_rect, flags);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay updating coefficient failed");
+		return ret;
+	}
+
+	/* Phase information - Must be after Coefficients */
+	ret = ovl_update_phase_plb(ovl_cache_plb, src_surf, src_rect);
+	if (ret) {
+		OS_FREE(ovl_cache_plb);
+		EMGD_ERROR_EXIT("Overlay updating phase failed");
+		return ret;
+	}
+
+	/* General overlay information.  Turn the overlay on and alternate
+	 * between Buffer 0 and Buffer 1. */
+	ovl_cache_plb->command = (ovl_cache_plb->command & 0xfffffff3) |
+			ovl_context->ovl_buff | 1;
+	ovl_context->ovl_buff ^= OVL_CMD_ACT_BUF1;
+
+	/* Dump out the Overlay Update Registers if debugging */
+	EMGD_VERBOSE(hal.dump_overlay_regs, ovl_dump_regs_plb(ovl_regs_plb));
+
+	OS_MEMCPY(ovl_regs_plb, ovl_cache_plb, sizeof(ovl_reg_image_plb_t));
+	OS_FREE(ovl_cache_plb);
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#else /* Else OVL_PLB_CACHE_QUICK_SWAP */
+
+/* Poulsbo overlay cache structure */
+static ovl_plb_cache_t ovl_cache;
+
+/* Flag to signal the cache is invalid and needs
+ * to be re-initialized */
+static int ovl_cache_needs_init = TRUE;
+
+static unsigned int ovl_update_regs_plb(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl_reg_image_plb_t *ovl_regs_plb,
+		            *ovl_cache_regs;
+	unsigned int         cache_changed;
+	int                  ret;
+
+	EMGD_TRACE_ENTER;
+
+	/* get the pointers to the real regs, and our cached copy of them */
+	ovl_regs_plb = phys_to_virt(ovl_context->reg_update_phys);
+	ovl_cache_regs = &ovl_cache.ovl_regs;
+
+	/* Fast path for turning off overlay. No need for cache */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+
+		/* Turn the overlay Off */
+		ovl_regs_plb->command = 0;
+
+		/* if we were using the cache, turn it off there too */
+		if (!ovl_cache_needs_init) {
+			ovl_cache.ovl_regs.command = 0;
+		}
+
+		/* Reset the cache */
+		ovl_cache_needs_init = TRUE;
+
+		/* Always use buf 0 when turning the overlay off. */
+		ovl_context->ovl_buff = 0;
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* Force value to even due hardware expects even number */
+	dest_rect->y1 &= ~1;
+	dest_rect->y2 = (dest_rect->y2 + 1) & ~1;
+	dest_rect->x1 &= ~1;
+	dest_rect->x2 = (dest_rect->x2 + 1) & ~1;
+
+	/* Init the cache if necessary */
+	if (ovl_cache_needs_init) {
+
+
+		/* Force every cache check to miss */
+		OS_MEMSET(&ovl_cache, 0, sizeof(ovl_plb_cache_t));
+
+		/* We just set our cached flags to 0, which might accidently
+		 * match up with "OFF" for some important incoming flag
+		 * bits, causing us to think we already handled them when
+		 * we didn't.  So set our cached flags to the exact
+		 * opposite of the incoming flags, which will force
+		 * us to test and handle every single bit, regardless
+		 * of whether it is on or off. */
+		ovl_cache.flags = ~flags;
+
+		/* init our cached registers */
+		OS_MEMCPY(ovl_cache_regs,
+			  ovl_regs_plb,
+			  sizeof(ovl_reg_image_plb_t));
+
+		/* initialization complete */
+		ovl_cache_needs_init = FALSE;
+	}
+
+	/* See what has changed in the cache */
+	cache_changed = get_cache_changes_plb (src_surf,
+					   src_rect,
+					   dest_rect,
+					   ovl_info,
+					   flags,
+					   &ovl_cache);
+
+	/* Perhaps the biggest challenge of caching the overlay
+	 * state is what to do with the command and config regs.
+	 * Normally we would clear command and config to 0 here,
+	 * and let the update process set only the bits that are
+	 * needed.  But doing this would invalidate our cache.
+	 * Instead we are relying on the above call to
+	 * get_cache_changes() to clear those bits in command
+	 * and config that will be changing */
+
+	/* Set overlay to the proper pipe */
+	/* it is cheaper to just set this, than to test it and set it. */
+	if (1 == PIPE(display)->pipe_num) {
+		/* This is pipe B */
+		ovl_cache_regs->config |= 1 << 18;
+	} else {
+		ovl_cache_regs->config &= ~(1 << 18);
+	}
+
+	if (cache_changed & IGD_OVL_PLB_UPDATE_FLAGS) {
+
+		/* Interleaved/progressive and Odd/Even if interleaved. */
+		if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+			ovl_cache_regs->command |= OVL_CMD_FIELD_MODE;
+			/* enable FIELD SYNC OVERLAY FLIP in field mode. */
+			ovl_cache_regs->command |= OVL_CMD_FIELD_SYNC_FLIP;
+			/* HSD# 203821 Mplayer outputs single one buffer including both even and odd fields */
+			if (flags & IGD_OVL_ALTER_FLIP_ODD) {
+				ovl_cache_regs->command |= OVL_CMD_ACT_FLD1;
+				/* HSD# 203821 To display odd field, starts from first odd field. */
+				if (0 == (src_rect->y1 & 1)) {
+					src_rect->y1 += 1;
+				}
+			} else {
+				ovl_cache_regs->command |= OVL_CMD_ACT_FLD0;
+				/* HSD# 203821 To display even field, starts from first even field. */
+				if (0 != (src_rect->y1 & 1)) {
+					src_rect->y1 += 1;
+				}	
+			}
+		} else {
+			ovl_cache_regs->command |= OVL_CMD_FRAME_MODE;
+		}
+	}
+
+	/* Has our destination rectangle changed? */
+	if (cache_changed & IGD_OVL_PLB_UPDATE_DEST) {
+		ovl_cache_regs->dest_pos_x_left  =
+			(unsigned short) dest_rect->x1;
+		ovl_cache_regs->dest_pos_y_top   =
+			(unsigned short) dest_rect->y1;
+		ovl_cache_regs->dest_width_x     =
+			(unsigned short) (dest_rect->x2 - dest_rect->x1);
+		ovl_cache_regs->dest_height_y    =
+			(unsigned short) (dest_rect->y2 - dest_rect->y1);
+
+	}
+
+	/* Always update the source pointers every frame */
+	ret = ovl_update_src_ptr_plb(display,
+				 ovl_cache_regs,
+				 src_surf,
+				 src_rect);
+	if (ret) {
+		/* Not good. Invalidate the entire cache and bail. */
+		ovl_cache_needs_init = TRUE;
+		EMGD_ERROR_EXIT("Overlay updating src pointers failed");
+		return ret;
+	}
+
+	/* Did either the Src rect or surface change? */
+	if (cache_changed & (IGD_OVL_PLB_UPDATE_SURF |
+			     IGD_OVL_PLB_UPDATE_SRC  ) ) {
+
+		ret = ovl_update_src_plb(display,
+					 ovl_cache_regs,
+					 src_surf,
+					 src_rect);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay updating src failed");
+			return ret;
+		}
+	}
+
+	/* Scaling information including Vertical downscaling.
+	 * Scaling should be guaranteed to work, since if the scale
+	 * is not supported, it should have already been blended
+	 * to a supported scale. */
+	if ( cache_changed & (IGD_OVL_PLB_UPDATE_SRC  |
+			      IGD_OVL_PLB_UPDATE_SURF |
+			      IGD_OVL_PLB_UPDATE_DEST  |
+			      IGD_OVL_PLB_UPDATE_FLAGS) ) {
+
+		ret = ovl_update_scale_plb(ovl_cache_regs,
+					   src_surf,
+					   src_rect,
+					   dest_rect,
+					   flags);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay updating scaling failed");
+			return ret;
+		}
+	}
+
+	/* Did video quality change? */
+	if (cache_changed & (IGD_OVL_PLB_UPDATE_VQ   |
+			     IGD_OVL_PLB_UPDATE_SURF ) ) {
+		/* Color control information */
+
+		ret = ovl_update_video_quality_plb(ovl_cache_regs, src_surf,
+						   &ovl_info->video_quality);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay video quality failed");
+			return ret;
+		}
+	}
+
+	/* Did gamma change? */
+	if (cache_changed & IGD_OVL_PLB_UPDATE_GAMMA) {
+
+		ret = ovl_update_gamma_plb(display, &ovl_info->gamma);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay gamma failed");
+			return ret;
+		}
+	}
+
+	/* Did color key change? */
+	if (cache_changed & IGD_OVL_PLB_UPDATE_COLORKEY) {
+
+		/* Destination color key */
+		EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+		if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+
+			EMGD_DEBUG("Overlay Enable Dest Color Key");
+			/* The mask and color key are different for the
+			 * different pixel formats */
+			ovl_cache_regs->dest_ckey_val =
+				convert_color_key_to_hw(
+					PLANE(display)->fb_info->pixel_format,
+					ovl_info->color_key.dest);
+			ovl_cache_regs->dest_ckey_mask =
+				convert_color_key_to_mask(
+					PLANE(display)->fb_info->pixel_format,
+					ovl_info->color_key.dest);
+			ovl_cache_regs->dest_ckey_mask |= 0x80000000;
+		} else {
+			EMGD_DEBUG("Overlay Disable Dest Color Key");
+			ovl_cache_regs->dest_ckey_mask = 0x00000000;
+		}
+
+		/* Source Color key */
+		if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+
+			EMGD_DEBUG("Overlay Enable Src Color Key");
+			ovl_cache_regs->source_ckey_high =
+				convert_color_key_to_hw(
+					src_surf->pixel_format,
+					ovl_info->color_key.src_hi);
+			ovl_cache_regs->source_ckey_high =
+				rgb_to_gbr(src_surf->pixel_format,
+					   ovl_cache_regs->source_ckey_high);
+
+			ovl_cache_regs->source_ckey_low =
+				convert_color_key_to_hw(
+					src_surf->pixel_format,
+					ovl_info->color_key.src_lo);
+			ovl_cache_regs->source_ckey_low =
+				rgb_to_gbr(src_surf->pixel_format,
+					   ovl_cache_regs->source_ckey_low);
+
+			ovl_cache_regs->source_ckey_mask = 0x07000000;
+		} else {
+			EMGD_DEBUG("Overlay Disable Src Color Key");
+			ovl_cache_regs->source_ckey_mask = 0x00000000;
+		}
+	} /* end color key changes */
+
+
+	/* Coefficients - Must be after Scaling */
+	if (cache_changed & (IGD_OVL_PLB_UPDATE_SRC   |
+			     IGD_OVL_PLB_UPDATE_SURF  |
+			     IGD_OVL_PLB_UPDATE_DEST  |
+			     IGD_OVL_PLB_UPDATE_FLAGS ) ) {
+
+		    ret = ovl_update_coeff_plb(ovl_cache_regs,
+					       src_surf,
+					       src_rect,
+					       dest_rect,
+					       flags);
+		    if (ret) {
+			    /* Not good. Invalidate entire cache and bail. */
+			    ovl_cache_needs_init = TRUE;
+			    EMGD_ERROR_EXIT("Overlay update coefficient failed");
+			    return ret;
+		    }
+	    }
+
+	    /* Phase information - Must be after Coefficients */
+	    if (cache_changed & (IGD_OVL_PLB_UPDATE_SRC |
+			      IGD_OVL_PLB_UPDATE_SURF ) ) {
+
+		    ret = ovl_update_phase_plb(ovl_cache_regs,
+					       src_surf,
+					       src_rect);
+		    if (ret) {
+			    /* Not good. Invalidate entire cache and bail. */
+			    ovl_cache_needs_init = TRUE;
+			    EMGD_ERROR_EXIT("Overlay updating phase failed");
+			    return ret;
+		    }
+	    }
+
+	    /* General overlay information.  Turn the overlay on and alternate
+	     * between Buffer 0 and Buffer 1. */
+	    ovl_cache_regs->command = (ovl_cache_regs->command & 0xfffffff3) |
+					ovl_context->ovl_buff | 1;
+	    ovl_context->ovl_buff ^= OVL_CMD_ACT_BUF1;
+
+	    /* Dump out the Overlay Update Registers if debugging */
+	    EMGD_VERBOSE(hal.dump_overlay_regs, ovl_dump_regs_plb(ovl_regs_plb));
+
+	    /* Finally, transfer the cached regs to the real regs */
+	    OS_MEMCPY(ovl_regs_plb,
+		      ovl_cache_regs,
+		      sizeof(ovl_reg_image_plb_t));
+
+	    EMGD_TRACE_EXIT;
+	    return IGD_SUCCESS;
+}
+
+#endif /* End if OVL_PLB_CACHE_QUICK_SWAP */
+
+static unsigned int ovl_send_instr_plb(
+	igd_display_context_t     *display,
+	unsigned int      flags)
+{
+	unsigned char * mmio = MMIO(display);
+	unsigned long tmp;
+
+	EMGD_TRACE_ENTER;
+
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_ON) {
+
+		ovl_context->state = OVL_STATE_ON;
+		/*
+		 * If Overlay + FB Blend is requested and the FB is xRGB
+		 * turn on the ARGB format.
+		 */
+		if(ovl_context->fb_blend_ovl) {
+			tmp = EMGD_READ32(mmio +  PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x18000000) {
+				EMGD_WRITE32(tmp | 0x1c000000, mmio +  PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 4);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 4);
+			}
+		}
+
+	} else {
+
+		if (ovl_context->fb_blend_ovl) {
+			tmp = EMGD_READ32(mmio +  PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x1c000000) {
+				tmp = tmp & 0xc3FFFFFF;
+				EMGD_WRITE32(tmp | 0x18000000, mmio +  PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 4);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 4);
+				OS_SLEEP(100);
+			}
+		}
+
+		OS_SLEEP(1);
+
+		/* if overlay is being turned OFF - ensure it's ON first */
+		if (ovl_context->state == OVL_STATE_OFF) {
+			/* Overlay is already off, no need to turn it off again */
+			EMGD_TRACE_EXIT;
+			return IGD_SUCCESS;
+		}
+
+		ovl_context->state = OVL_STATE_OFF;
+	}
+
+	/* Write the address of the memory buffer to the Overlay Update
+	 * Address Register causes the HW to load the new values from the
+	 * memory on the next VBLANK */
+	EMGD_WRITE32((ovl_context->reg_update_phys | 0x1), mmio + 0x30000);
+
+	ovl_context->sync = 0;
+
+	display->context->dispatch.sync(display,
+		IGD_PRIORITY_NORMAL,
+		&ovl_context->sync,
+		IGD_SYNC_NONBLOCK);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+
+static int alter_ovl_plb(igd_display_context_t *display,
+			 igd_surface_t       *src_surf,
+			 igd_rect_t          *src_rect,
+			 igd_rect_t          *dest_rect,
+			 igd_ovl_info_t      *ovl_info,
+			 unsigned int         flags)
+{
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	/* Dump overlay parameters for debugging */
+	/*
+	printk (KERN_ERR " alter_ovl_plb  Entry."
+		"offset=0x%X "
+		"pitch=%d "
+		"width=%d "
+		"height=%d \n"
+		"pixel_format=0x%X "
+		"flags=0x%X "
+		"virt_addr=0x%X "
+		"pvr2d_mem_info=0x%X "
+		"pvr2d_context_h=0x%X "
+		"hPVR2DFlipChain=0x%X \n"
+		"src_x1=%d "
+		"src_x2=%d "
+		"src_y1=%d "
+		"src_y2=%d "
+		"src width=%d "
+		"src height=%d \n"
+		"dest_x1=%d "
+		"dest_x2=%d "
+		"dest_y1=%d "
+		"dest_y2=%d "
+		"dest width=%d "
+		"dest height=%d \n"
+		"color_key.src_lo=0x%X "
+		"color_key.src_hi=0x%X "
+		"color_key.dest=0x%X "
+		"color_key.flags=0x%X "
+		"flags=0x%X "
+		,
+		(unsigned int) src_surf->offset ,
+		(unsigned int) src_surf->pitch ,
+		(unsigned int) src_surf->width ,
+		(unsigned int) src_surf->height ,
+		(unsigned int) src_surf->pixel_format ,
+		(unsigned int) src_surf->flags ,
+		(unsigned int) src_surf->virt_addr ,
+		(unsigned int) src_surf->pvr2d_mem_info ,
+		(unsigned int) src_surf->pvr2d_context_h ,
+		(unsigned int) src_surf->hPVR2DFlipChain ,
+		(unsigned int) src_rect->x1,
+		(unsigned int) src_rect->x2,
+		(unsigned int) src_rect->y1,
+		(unsigned int) src_rect->y2,
+		(unsigned int) (src_rect->x2 - src_rect->x1),
+		(unsigned int) (src_rect->y2 - src_rect->y1),
+		(unsigned int) dest_rect->x1,
+		(unsigned int) dest_rect->x2,
+		(unsigned int) dest_rect->y1,
+		(unsigned int) dest_rect->y2,
+		(unsigned int) (dest_rect->x2 - dest_rect->x1),
+		(unsigned int) (dest_rect->y2 - dest_rect->y1),
+		(unsigned int) ovl_info->color_key.src_lo,
+		(unsigned int) ovl_info->color_key.src_hi,
+		(unsigned int) ovl_info->color_key.dest,
+		(unsigned int) ovl_info->color_key.flags,
+		(unsigned int) flags
+		);
+	*/
+	/* Dump overlay surface contents, for debugging */
+	/*
+	if (flags & IGD_OVL_ALTER_ON)
+	{
+		int i;
+		unsigned char *ptr = src_surf->virt_addr;
+
+		for (i = 0; i<10; ++i) {
+			printk(KERN_ERR
+			       "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X"
+			       "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X"
+			       "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X"
+			       "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X",
+			       ptr[0],ptr[1],ptr[2],ptr[3],ptr[4],
+			       ptr[5],ptr[6],ptr[7],ptr[8],ptr[9],
+			       ptr[10],ptr[11],ptr[12],ptr[13],ptr[14],
+			       ptr[15],ptr[16],ptr[17],ptr[18],ptr[19],
+			       ptr[20],ptr[21],ptr[22],ptr[23],ptr[24],
+			       ptr[25],ptr[26],ptr[27],ptr[28],ptr[29],
+			       ptr[30],ptr[31],ptr[32],ptr[33],ptr[34],
+			       ptr[35],ptr[36],ptr[37],ptr[38],ptr[39]);
+			ptr += src_surf->pitch;
+		}
+	}
+	*/
+
+	/* Check to ensure the overlay can be used given the current mode as
+	 * well as what the IAL is asking for.  If not return an error. */
+	ret = ovl_check_plb(display, src_surf, src_rect, dest_rect, ovl_info,
+			    flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay Check failed");
+		return ret;
+	}
+
+	/* Check if last flip is still pending.
+	 * This is necessary for the following reasons:
+	 *    - If the previous instructions have not been processed, then the
+	 *      ovl_regs_plb is still in use and can not be overwritten.
+	 */
+	if ((FALSE == query_ovl_plb(
+				    (igd_display_h)display,
+				    IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE)) &&
+	    (flags & IGD_OVL_ALTER_ON)) {
+		/* Only return an error if the overlay is on.  If turning it off,
+		 * allow it to continue, since something may have failed and we
+		 * should try our best to turn the overlay off. */
+		EMGD_ERROR_EXIT("Query Overlay failed");
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Update all Overlay Update Registers */
+	ret = ovl_update_regs_plb(display, src_surf, src_rect,
+				  dest_rect, ovl_info,
+				  flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay Update Registers failed");
+		return ret;
+	}
+
+	/* Send the instructions to the command queue */
+	ret = ovl_send_instr_plb(display, flags);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+static int query_ovl_plb(igd_display_h display_h,
+	unsigned int flags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_h;
+	os_alarm_t timeout;
+
+	EMGD_TRACE_ENTER;
+
+	switch (flags) {
+
+	case IGD_OVL_QUERY_IS_HW_SUPPORTED:
+		/* This is the first overlay, so HW overlay is supported */
+		break;
+
+	case IGD_OVL_QUERY_IS_LAST_FLIP_DONE:
+		/* If there no sync to wait on, then the last flip is done, and the
+		 * Register Update has occured, simply return TRUE (Flip done).
+		 */
+		/*if (!(ovl_context->sync)) {
+			return TRUE;
+		}*/
+
+		/* Check to see if the last flip instruction has been executed.  If not
+		 * return FALSE (Flip not done). */
+		/*if(display->context->dispatch.sync(
+			display,
+			IGD_PRIORITY_NORMAL,
+			&ovl_context->sync,
+			IGD_SYNC_NONBLOCK)) {
+			EMGD_DEBUG("Overlay Sync Check - Flip not done");
+			return FALSE;
+		}*/
+
+		/* Check to see if the register update is complete.  If not return
+		 * FALSE (Flip not done). */
+		if(!(EMGD_READ32(MMIO(display) + 0x30008) & 0x80000000)) {
+			EMGD_DEBUG("Overlay Status Check - Register Update not done");
+			return FALSE;
+		}
+
+		/* Now that we know the last flip is done and the register update is
+		 * complete, set the sync to 0 and return TRUE (Flip done). */
+		ovl_context->sync = 0;
+		break;
+
+	case IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE:
+		/* Wait for 200 milliseconds for the last flip to complete.  If not
+		 * done in that time, there is likely a hardware problem so return
+		 * FALSE. */
+		timeout = OS_SET_ALARM(200);
+		do {
+			if (TRUE ==
+				query_ovl_plb(display_h, IGD_OVL_QUERY_IS_LAST_FLIP_DONE)) {
+				EMGD_TRACE_EXIT;
+				return TRUE;
+			}
+		} while (!OS_TEST_ALARM(timeout));
+		EMGD_ERROR_EXIT("Timeout waiting for last flip done");
+		return FALSE;
+		break;
+	case IGD_OVL_QUERY_IS_GAMMA_SUPPORTED:
+		return TRUE;
+		break;
+	case IGD_OVL_QUERY_IS_VIDEO_PARAM_SUPPORTED:
+		return TRUE;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return TRUE;
+}
+
+static int query_max_size_ovl_plb(
+	igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height)
+{
+	ovl_chipset_plb_t *ovl_chip;
+
+	EMGD_TRACE_ENTER;
+
+	ovl_chip = ovl_chipset_plb;
+	*max_width = 0;
+	*max_height = 0;
+	while(ovl_chip->num_linebuf != OVL_CONFIG_NO_LINE_BUFF){
+		if(((pf & IGD_PF_MASK) == ovl_chip->pixel_format) &&
+			(ovl_chip->max_width > *max_width)) {
+			*max_width = ovl_chip->max_width;
+			*max_height = ovl_chip->max_height;
+		}
+		ovl_chip++;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.c b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.c
new file mode 100644
index 0000000..d05c475
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.c
@@ -0,0 +1,321 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_plb_cache.c
+ * $Revision: 1.5 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Support functions for overlay caching.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.overlay
+
+#include "ovl_plb_cache.h"
+
+/*
+ * Caching helper functions.  Implemented in ovl_plb.c, but used by
+ * the ovl2 and micro_ovl modules as well.
+ */
+
+/* Tells if a rectangle has changed */
+int is_changed_rect(
+		    igd_rect_t *old_rect,
+		    igd_rect_t *new_rect)
+{
+	return ((old_rect->x1 != new_rect->x1) ||
+		(old_rect->x2 != new_rect->x2) ||
+		(old_rect->y1 != new_rect->y1) ||
+		(old_rect->y2 != new_rect->y2) );
+}
+
+/* Tells if a surface has changed */
+int is_changed_surf(
+			 igd_surface_t *old_surf,
+			 igd_surface_t *new_surf)
+{
+	return( (old_surf->pitch        !=  new_surf-> pitch)        ||
+		(old_surf->width        !=  new_surf-> width)        ||
+		(old_surf->height       !=  new_surf-> height)       ||
+		(old_surf->pixel_format !=  new_surf-> pixel_format) ||
+		(old_surf->u_pitch      !=  new_surf-> u_pitch)      ||
+		(old_surf->v_pitch      !=  new_surf-> v_pitch)      ||
+		(old_surf->palette_info !=  new_surf-> palette_info) ||
+		(old_surf->flags        !=  new_surf-> flags)        ||
+		(old_surf->logic_ops    !=  new_surf-> logic_ops)    ||
+		(old_surf->render_ops   !=  new_surf-> render_ops)   ||
+		(old_surf->alpha        !=  new_surf-> alpha)        ||
+		(old_surf->diffuse      !=  new_surf-> diffuse)      ||
+		(old_surf->chroma_high  !=  new_surf-> chroma_high)  ||
+		(old_surf->chroma_low   !=  new_surf-> chroma_low)   );
+
+}
+
+/* Tells if the color key has changed */
+ int is_changed_color_key(
+			 igd_ovl_info_t *old_info,
+			 igd_ovl_info_t *new_info)
+{
+	return ( (old_info->color_key.src_lo != new_info->color_key.src_lo)  ||
+		 (old_info->color_key.src_hi != new_info->color_key.src_hi)  ||
+		 (old_info->color_key.dest   != new_info->color_key.dest)    ||
+		 (old_info->color_key.flags  != new_info->color_key.flags)   );
+
+}
+
+/* Tells if the video quality has changed */
+int is_changed_vq(
+			 igd_ovl_info_t *old_info,
+			 igd_ovl_info_t *new_info)
+{
+	return ( (old_info->video_quality.contrast   !=
+		  new_info->video_quality.contrast)        ||
+		 (old_info->video_quality.brightness !=
+		  new_info->video_quality.brightness)      ||
+		 (old_info->video_quality.saturation !=
+		  new_info->video_quality.saturation)       );
+
+}
+
+/* Tells if the gamma has changed */
+int is_changed_gamma(
+			 igd_ovl_info_t *old_info,
+			 igd_ovl_info_t *new_info)
+{
+	return ((old_info->gamma.red   != new_info->gamma.red)   ||
+		(old_info->gamma.green != new_info->gamma.green) ||
+		(old_info->gamma.blue  != new_info->gamma.blue)  ||
+		(old_info->gamma.flags != new_info->gamma.flags) );
+
+}
+
+/* Copies a source rectangle to the cache */
+void copy_src_rect_plb(igd_rect_t *src_rect, povl_plb_cache_t ovl_cache)
+{
+	if (src_rect) {
+		OS_MEMCPY(&ovl_cache->src_rect,
+			  src_rect,
+			  sizeof(igd_rect_t));
+	} else {
+		OS_MEMSET(&ovl_cache->src_rect,
+			  0,
+			  sizeof(igd_rect_t));
+	}
+}
+
+
+/* Copies a dest rectangle to the cache */
+void copy_dest_rect_plb( igd_rect_t *dest_rect, povl_plb_cache_t ovl_cache)
+{
+	if (dest_rect) {
+		OS_MEMCPY(&ovl_cache->dest_rect,
+			  dest_rect,
+			  sizeof(igd_rect_t));
+	} else {
+		OS_MEMSET(&ovl_cache->dest_rect,
+			  0,
+			  sizeof(igd_rect_t));
+	}
+}
+
+
+/* Copies a surface to the cache */
+void copy_surf_plb(igd_surface_t *src_surf, povl_plb_cache_t ovl_cache)
+{
+	if (src_surf) {
+		OS_MEMCPY(&ovl_cache->src_surf,
+			  src_surf,
+			  sizeof(igd_surface_t));
+	} else {
+		OS_MEMSET(&ovl_cache->src_surf,
+			  0,
+			  sizeof(igd_surface_t));
+	}
+}
+
+
+/* Copies the color key to the cache */
+void copy_color_key_plb( igd_ovl_info_t *ovl_info, povl_plb_cache_t ovl_cache)
+{
+	if (ovl_info) {
+		OS_MEMCPY(&ovl_cache->ovl_info.color_key,
+			  &(ovl_info->color_key),
+			  sizeof(igd_ovl_color_key_info_t));
+	} else {
+		OS_MEMSET(&ovl_cache->ovl_info.color_key,
+			  0,
+			  sizeof(igd_ovl_color_key_info_t));
+	}
+}
+
+
+/* Copies the video quality info to the cache */
+void copy_vq_plb(igd_ovl_info_t *ovl_info, povl_plb_cache_t ovl_cache)
+{
+	if (ovl_info) {
+		OS_MEMCPY(&ovl_cache->ovl_info.video_quality,
+			  &(ovl_info->video_quality),
+			  sizeof(igd_ovl_video_quality_info_t));
+	} else {
+		OS_MEMSET(&ovl_cache->ovl_info.video_quality,
+			  0,
+			  sizeof(igd_ovl_video_quality_info_t));
+	}
+}
+
+
+/* Copies the gamma to the cache */
+void copy_gamma_plb(igd_ovl_info_t *ovl_info, povl_plb_cache_t ovl_cache)
+{
+	if (ovl_info) {
+		OS_MEMCPY(&ovl_cache->ovl_info.gamma,
+			  &(ovl_info->gamma),
+			  sizeof(igd_ovl_gamma_info_t));
+	} else {
+		OS_MEMSET(&ovl_cache->ovl_info.gamma,
+			  0,
+			  sizeof(igd_ovl_gamma_info_t));
+	}
+}
+
+
+
+/* Checks to see what, if anything has changed.
+ * Clears bits in the command and config register that are invalid.
+ * Returns a set of flags telling what changed */
+unsigned int get_cache_changes_plb(
+			       igd_surface_t  *src_surf,
+			       igd_rect_t     *src_rect,
+			       igd_rect_t     *dest_rect,
+			       igd_ovl_info_t *ovl_info,
+			       unsigned int    flags,
+			       povl_plb_cache_t ovl_cache)
+{
+	unsigned int cache_changed = 0;
+
+	ovl_cache->ovl2_regs.control = 0;
+
+	/* Have the flags changed? */
+	if (ovl_cache->flags != flags) {
+
+		/* NOTE that we did not check which flags changed.
+		   Currently if any flag changes, we invalidate all
+		   the flags. We could further optimize caching if
+		   we tested for individual flag changes, but I don't
+		   think it will really help much.  Once a movie starts
+		   playing, it pretty much uses the same flags
+		   for its entire duration, unless the user is mucking
+		   about with it as it plays, in which case it's going
+		   to be slow anyway. */
+
+		ovl_cache->flags = flags;
+		cache_changed |= IGD_OVL_PLB_UPDATE_FLAGS;
+
+		/* Invalidating the flags cache will cause the following
+		 * bits to be re-calculated.  Clear them in preparation for
+		 * re-calculation. */
+		ovl_cache->ovl_regs.command &= ~ (OVL_CMD_FRAME_MODE      |
+						 OVL_CMD_FIELD_MODE      |
+						 OVL_CMD_FIELD_SYNC_FLIP |
+						 OVL_CMD_ACT_FLD1        |
+						 OVL_CMD_ACT_FLD0        );
+	}
+
+	/* Do a comparison to source surface */
+	if (is_changed_surf(&ovl_cache->src_surf, src_surf)) {
+		cache_changed |= IGD_OVL_PLB_UPDATE_SURF;
+		copy_surf_plb(src_surf, ovl_cache);
+
+		/* invalidating the source surface will cause the driver
+		 * to recalculate the following bits in command */
+		ovl_cache->ovl_regs.command &= ~( OVL_CMD_YUV_422  |
+						 OVL_CMD_YUV_420P |
+						 OVL_CMD_YUV_410P |
+						 OVL_CMD_YUV_NV12 |
+						 OVL_CMD_Y_SWAP   |
+						 OVL_CMD_UV_SWAP  |
+						 OVL_CMD_RGB_8888 |
+						 OVL_CMD_RGB_565  |
+						 OVL_CMD_RGB_555  );
+
+		/* Invalidating the source surface will cause the driver
+		 * to recalculate the following bits in config */
+		ovl_cache->ovl_regs.config &= ~ ( (1<<4)                    |
+					    OVL_CONFIG_LINE_BUFF_MASK );
+
+	}
+
+	/* Do a comparison to source rectangle */
+	if (is_changed_rect(&ovl_cache->src_rect, src_rect)) {
+		cache_changed |= IGD_OVL_PLB_UPDATE_SRC;
+		copy_src_rect_plb(src_rect, ovl_cache);
+
+		/* invalidating the source rectangle will cause the driver
+		 * to recalculate the following bits in command */
+		ovl_cache->ovl_regs.command &= ~( OVL_CMD_YUV_422  |
+						 OVL_CMD_YUV_420P |
+						 OVL_CMD_YUV_410P |
+						 OVL_CMD_YUV_NV12 |
+						 OVL_CMD_Y_SWAP   |
+						 OVL_CMD_UV_SWAP  |
+						 OVL_CMD_RGB_8888 |
+						 OVL_CMD_RGB_565  |
+						 OVL_CMD_RGB_555  );
+
+		/* Invalidating the source rectangle will cause the driver
+		 * to recalculate the following bits in config */
+		ovl_cache->ovl_regs.config &= ~ ( (1<<4)                    |
+					    OVL_CONFIG_LINE_BUFF_MASK );
+
+
+	}
+
+	/* Has our destination rectangle changed? */
+	if (is_changed_rect(&ovl_cache->dest_rect, dest_rect)) {
+		cache_changed |= IGD_OVL_PLB_UPDATE_DEST;
+		copy_dest_rect_plb(dest_rect, ovl_cache);
+	}
+
+	/* Do a comparison to overlay info color key */
+	if (is_changed_color_key(&ovl_cache->ovl_info, ovl_info)) {
+		cache_changed |= (IGD_OVL_PLB_UPDATE_INFO |
+				  IGD_OVL_PLB_UPDATE_COLORKEY);
+		copy_color_key_plb(ovl_info, ovl_cache);
+	}
+
+	/* Do a comparison to overlay info video quality */
+	if (is_changed_vq(&ovl_cache->ovl_info, ovl_info)) {
+		cache_changed |= ( IGD_OVL_PLB_UPDATE_INFO |
+				   IGD_OVL_PLB_UPDATE_VQ);
+		copy_vq_plb(ovl_info, ovl_cache);
+	}
+
+	/* Do a comparison to overlay info gamma */
+	if (is_changed_gamma(&ovl_cache->ovl_info, ovl_info)) {
+		cache_changed |= ( IGD_OVL_PLB_UPDATE_INFO |
+				   IGD_OVL_PLB_UPDATE_GAMMA);
+		copy_gamma_plb(ovl_info, ovl_cache);
+	}
+
+	return cache_changed;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.h b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.h
new file mode 100644
index 0000000..b8bb2ef
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_plb_cache.h
@@ -0,0 +1,161 @@
+/*
+ *----------------------------------------------------------------------------
+ * Filename: ovl_plb_cache.h
+ * $Revision: 1.5 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for overlay caching. It should be not be
+ *  by any other module besides the overlay module itself.
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _OVL_PLB_CACHE_H
+#define _OVL_PLB_CACHE_H
+
+#include <rb.h>
+#include "ovl_regs_plb.h"
+#include "ovl2_regs_plb.h"
+
+/* Turns the overlay cache on and off. */
+#define OVL_PLB_CACHE_QUICK_SWAP
+
+
+/*
+ * In order to cache the overlay registers we need a structure
+ * that defines and wraps them.  We already have one for the primary
+ * display, called ovl_reg_image_plb_t.
+ * Here we define a structure for the secondary display
+ */
+
+typedef struct _ovl2_reg_plb_cache{
+	unsigned int control;
+	unsigned int start;
+	unsigned int plane_control;
+	unsigned int plane_start;
+
+	unsigned int pitch;
+	unsigned int dest_rect_x1y1;
+	unsigned int dest_rect_x2y2;
+	unsigned int cont_bright;
+	unsigned int satn_hue;
+	unsigned int gamma_regs[OVL2_TOTAL_GAMMA_REG];
+	unsigned int colorkey_hw;
+	unsigned int colorkey_mask;
+	unsigned int ckey_high;
+	unsigned int ckey_low;
+	unsigned int ckey_enable;
+
+} ovl2_reg_plb_cache_t;
+
+#define MMIO_OFFSET_PLB2_PITCH          0x72188
+#define MMIO_OFFSET_PLB2_DEST_RECT_X1Y1 0x7218C
+#define MMIO_OFFSET_PLB2_DEST_RECT_X2Y2 0x72190
+#define MMIO_OFFSET_PLB2_CONT_BRIGHT    0x721D0
+#define MMIO_OFFSET_PLB2_SATN_HUE       0x721D4
+#define MMIO_OFFSET_PLB2_COLORKEY_HW    0x14
+#define MMIO_OFFSET_PLB2_COLORKEY_MASK  0x18
+#define MMIO_OFFSET_PLB2_CKEY_LOW       0x72194
+#define MMIO_OFFSET_PLB2_CKEY_HIGH      0x721a0
+#define MMIO_OFFSET_PLB2_CKEY_ENABLE    0x72198
+#define MMIO_OFFSET_PLB2_CONTROL        0x72180
+#define MMIO_OFFSET_PLB2_START          0x72184
+
+
+/*
+ * This stucture caches the overlay state, so we don't have to
+ * re-program everything for every single frame
+ */
+typedef struct _ovl_plb_cache {
+	igd_surface_t        src_surf;
+	igd_rect_t           src_rect;
+	igd_rect_t           dest_rect;
+	igd_ovl_info_t       ovl_info;
+	unsigned int         flags;
+	ovl_reg_image_plb_t  ovl_regs;
+	ovl2_reg_plb_cache_t ovl2_regs;
+} ovl_plb_cache_t, *povl_plb_cache_t;
+
+
+/* Flags for things that might have changed in the cache */
+#define IGD_OVL_PLB_UPDATE_DEST     (1 << 0)
+#define IGD_OVL_PLB_UPDATE_SRC      (1 << 1)
+#define IGD_OVL_PLB_UPDATE_SURF     (1 << 2)
+#define IGD_OVL_PLB_UPDATE_INFO     (1 << 3)
+#define IGD_OVL_PLB_UPDATE_FLAGS    (1 << 4)
+#define IGD_OVL_PLB_UPDATE_VQ       (1 << 5)
+#define IGD_OVL_PLB_UPDATE_GAMMA    (1 << 6)
+#define IGD_OVL_PLB_UPDATE_COLORKEY (1 << 7)
+
+
+/*
+ * Caching helper functions.  Implemented in ovl_plb.c, but used by
+ * the ovl2 and micro_ovl modules as well.
+ */
+
+/* Tells if a rectangle has changed */
+int is_changed_rect(igd_rect_t *old_rect, igd_rect_t *new_rect);
+
+/* Tells if a surface has changed */
+int is_changed_surf(igd_surface_t *old_surf, igd_surface_t *new_surf);
+
+/* Tells if the color key has changed */
+ int is_changed_color_key(igd_ovl_info_t *old_info, igd_ovl_info_t *new_info) ;
+
+/* Tells if the video quality has changed */
+int is_changed_vq(igd_ovl_info_t *old_info, igd_ovl_info_t *new_info);
+
+/* Tells if the gamma has changed */
+int is_changed_gamma(igd_ovl_info_t *old_info, igd_ovl_info_t *new_info);
+
+/* Copies a source rectangle to the cache */
+void copy_src_rect_plb(igd_rect_t *src_rect, povl_plb_cache_t ovl_cache);
+
+/* Copies a dest rectangle to the cache */
+void copy_dest_rect_plb(igd_rect_t *dest_rect, povl_plb_cache_t ovl_cache);
+
+/* Copies a surface to the cache */
+void copy_surf_plb(igd_surface_t *src_surf, povl_plb_cache_t ovl_cache);
+
+/* Copies the color key to the cache */
+void copy_color_key_plb(igd_ovl_info_t *ovl_info, povl_plb_cache_t ovl_cache);
+
+/* Copies the video quality info to the cache */
+void copy_vq_plb(igd_ovl_info_t *ovl_info, povl_plb_cache_t ovl_cache);
+
+/* Copies the gamma to the cache */
+void copy_gamma_plb(igd_ovl_info_t *ovl_info, povl_plb_cache_t ovl_cache);
+
+/* Checks to see what, if anything has changed.
+ * Clears bits in the command and config register that are invalid.
+ * Returns a set of flags telling what changed */
+unsigned int get_cache_changes_plb(
+			       igd_surface_t  *src_surf,
+			       igd_rect_t     *src_rect,
+			       igd_rect_t     *dest_rect,
+			       igd_ovl_info_t *ovl_info,
+			       unsigned int    flags,
+			       povl_plb_cache_t ovl_cache);
+
+
+
+#endif /* _OVL_PLB_CACHE_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_regs_plb.h b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_regs_plb.h
new file mode 100644
index 0000000..34b24c4
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/plb/ovl_regs_plb.h
@@ -0,0 +1,185 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_regs_plb.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for overlay. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized register dependant information including
+ *  values, structures and addresses specifically for the Napa core
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef OVL_REGS_PLB_H_
+#define OVL_REGS_PLB_H_
+
+/* Overlay Update Register Image Structure.*/
+typedef struct _ovl_reg_image_plb{
+
+	volatile unsigned int			buffer0_yrgb_ptr;
+	volatile unsigned int			buffer1_yrgb_ptr;
+	volatile unsigned int			buffer0_u_ptr;
+	volatile unsigned int			buffer0_v_ptr;
+	volatile unsigned int			buffer1_u_ptr;
+	volatile unsigned int			buffer1_v_ptr;
+	volatile unsigned short			yrgb_stride;
+	volatile unsigned short			uv_stride;
+	volatile unsigned short			yrgb_vert_phase_field0;
+	volatile unsigned short			yrgb_vert_phase_field1;
+	volatile unsigned short			uv_vert_phase_field0;
+	volatile unsigned short			uv_vert_phase_field1;
+	volatile unsigned short			yrgb_hphase;
+	volatile unsigned short			uv_hphase;
+	volatile unsigned int			init_phase_shift;
+	volatile unsigned short			dest_pos_x_left;
+	volatile unsigned short			dest_pos_y_top;
+	volatile unsigned short			dest_width_x;
+	volatile unsigned short			dest_height_y;
+	volatile unsigned short			source_yrgb_width;
+	volatile unsigned short			source_uv_width;
+	volatile unsigned short			source_yrgb_width_swords;
+	volatile unsigned short			source_uv_width_swords;
+	volatile unsigned short			source_yrgb_height;
+	volatile unsigned short			source_uv_height;
+	volatile unsigned int			yrgb_scale;
+	volatile unsigned int			uv_scale;
+	volatile unsigned int			col_ctl_brt_con;
+	volatile unsigned int			col_ctl_sat_hue;
+	volatile unsigned int			dest_ckey_val;
+	volatile unsigned int			dest_ckey_mask;
+	volatile unsigned int			source_ckey_high;
+	volatile unsigned int			source_ckey_low;
+	volatile unsigned int			source_ckey_mask;
+	volatile unsigned int			config;
+	volatile unsigned int			command;
+	volatile unsigned int			reserved1;
+	volatile unsigned short			alpha_pos_x_left;
+	volatile unsigned short			alpha_pos_y_top;
+	volatile unsigned short			alpha_width_x;
+	volatile unsigned short			alpha_height_y;
+	volatile unsigned int			reserved2;
+	volatile unsigned int			reserved3;
+	volatile unsigned int			reserved4;
+	volatile unsigned int			reserved5;
+	volatile unsigned int			reserved6;
+	volatile unsigned int			reserved7;
+	volatile unsigned int			reserved8;
+	volatile unsigned int			reserved9;
+	volatile unsigned int			reserved10;
+	volatile unsigned int			reserved11;
+	volatile unsigned int			reserved11a; /*ovl_fast_horz_downscale;*/
+	volatile unsigned int			vert_downscale;
+	volatile unsigned int			reserved12[86];
+	volatile unsigned short			y_vert_coeff_single[52]; /*offset 0x200 --> 3*17/2 + 1*/
+	volatile unsigned int			reserved13[38];
+	volatile unsigned short			y_horz_coeff_single[86]; /*offset 0x300 --> 5*17/2 + 1*/
+	volatile unsigned int			reserved14[85];
+	volatile unsigned short			uv_vert_coeff_single[52]; /*offset 0x500 --> 3*17/2 + 1*/
+	volatile unsigned int			reserved15[38];
+	volatile unsigned short			uv_horz_coeff_single[52]; /*offset 0x600 --> 3*17/2 + 1*/
+	volatile unsigned int			reserved16[38];
+
+} ovl_reg_image_plb_t;
+
+/* Color Correction */
+#define OVL_YUV_COLOR_DEF_CONT_BRGHT    0x10c00fb
+#define OVL_YUV_COLOR_DEF_SATN_HUE      0x0000091
+#define OVL_RGB_COLOR_DEF_CONT_BRGHT    0x1000000
+#define OVL_RGB_COLOR_DEF_SATN_HUE      0x0000080
+
+/* Overlay Config */
+#define OVL_CONFIG_NO_LINE_BUFF     0xffffffff
+#define OVL_CONFIG_TWO_LINE_BUFF    0x00000000
+#define OVL_CONFIG_THREE_LINE_BUFF  0x00000001
+#define OVL_CONFIG_LINE_BUFF_MASK   0x00000001
+
+/* Overlay Command Definitions */
+/* 422 Swap */
+#define OVL_CMD_UV_SWAP				0x00004000
+#define OVL_CMD_Y_SWAP				0x00008000
+#define OVL_CMD_YUV_SWAP			0x0000C000
+
+/* Source Format */
+#define OVL_CMD_RGB_8888			0x00000400
+#define OVL_CMD_RGB_565				0x00000C00
+#define OVL_CMD_RGB_555				0x00000800
+#define OVL_CMD_YUV_NV12Alt			0x00001C00 /*planar NV12, Alternate?*/
+#define OVL_CMD_YUV_NV12			0x00002C00 /*planar NV12*/
+#define OVL_CMD_YUV_422				0x00002000 /*packed YUV422*/
+#define OVL_CMD_YUV_411				0x00002400 /*packed YUV411*/
+#define OVL_CMD_YUV_420P			0x00003000 /*planar YUV420*/
+#define OVL_CMD_YUV_422P			0x00003400 /*planar YUV422*/
+#define OVL_CMD_YUV_410P			0x00003800 /*planar YUV410*/
+#define OVL_CMD_YUV_444P			0x00003C00 /*planar YUV444*/
+#define OVL_CMD_SRC_FMT_MASK		0x00003C00 /*mask for above*/
+
+/* Flip Type */
+#define OVL_CMD_FRAME_MODE          0x00000000
+#define OVL_CMD_FIELD_MODE			0x00000020
+
+/* Field Sync Flip Enable */
+#define OVL_CMD_FIELD_SYNC_FLIP			0x00000080
+
+/* Buffer and Field */
+#define OVL_CMD_ACT_BUF0			0x00000000
+#define OVL_CMD_ACT_BUF1			0x00000004
+#define OVL_CMD_ACT_FLD0			0x00000000
+#define OVL_CMD_ACT_FLD1			0x00000002
+
+/* Initial phase register */
+#define Y_VPP_FLD0_PLUS1			0x100000
+#define Y_VPP_FLD0_PLUS2			0x200000
+#define Y_VPP_FLD0_MINUS1			0xF00000
+#define Y_VPP_FLD1_PLUS1			0x010000
+#define Y_VPP_FLD1_PLUS2			0x020000
+#define Y_VPP_FLD1_MINUS1			0x0F0000
+#define Y_HPP_PLUS1					0x001000
+#define Y_HPP_PLUS2					0x002000
+#define Y_HPP_MINUS1				0x00F000
+#define UV_VPP_FLD0_PLUS1			0x000100
+#define UV_VPP_FLD0_PLUS2			0x000200
+#define UV_VPP_FLD0_MINUS1			0x000F00
+#define UV_VPP_FLD1_PLUS1			0x000010
+#define UV_VPP_FLD1_PLUS2			0x000020
+#define UV_VPP_FLD1_MINUS1			0x0000F0
+#define UV_HPP_PLUS1				0x000001
+#define UV_HPP_PLUS2				0x000002
+#define UV_HPP_MINUS1				0x00000F
+
+#define OVL_REG_ADDR_GAMMA5						0x30010
+#define OVL_REG_ADDR_GAMMA4						0x30014
+#define OVL_REG_ADDR_GAMMA3						0x30018
+#define OVL_REG_ADDR_GAMMA2						0x3001C
+#define OVL_REG_ADDR_GAMMA1						0x30020
+#define OVL_REG_ADDR_GAMMA0						0x30024
+#define OVL_TOTAL_GAMMA_REG						6
+
+/* following value are needed because hardware seems to display yuv slightly dimmer
+   than RGB when color data is calculated out to be equal */
+#define MID_CONTRAST_YUV					0x43//4a
+#define MID_SATURATION_YUV					0x91//92
+#define MID_BRIGHTNESS_YUV					-5
+
+#endif /* OVL_REGISTER_IMAGE_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/micro_ovl_tnc.c b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/micro_ovl_tnc.c
new file mode 100644
index 0000000..1bcbf29
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/micro_ovl_tnc.c
@@ -0,0 +1,1738 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: micro_ovl_tnc.c
+ * $Revision: 1.18 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains function that actually programs the second
+ *  overlay with the bits to properly configure the overlay
+ *  Also includes functions to execute the second overlay flip
+ *  instruction, and query the overlay flip status.
+ *  Also contains some hardware capabilities querrying functions
+ *  for upper overlay layer to get this chips second overlay
+ *  capabilities
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.overlay
+
+#include <io.h>
+#include <memory.h>
+#include <intelpci.h>
+#include <math_fix.h>
+
+
+#include <igd_mode.h>
+#include <utils.h>
+#include <general.h>
+#include <context.h>
+#include <rb.h>
+#include <mode.h>
+
+#include <tnc/instr.h>
+#include <tnc/regs.h>
+#include <tnc/cmd.h>
+#include "ovl_tnc_cache.h"
+#include "ovl2_tnc.h"
+#include "ovl2_regs_tnc.h"
+#include "../cmn/ovl_dispatch.h"
+#include "../cmn/ovl_virt.h"
+
+extern unsigned long sprite_pixel_formats_tnc[];
+
+extern unsigned int ovl2_check_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+
+int micro_alter_ovl2_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+unsigned int micro_spritec_send_instr_tnc(
+	igd_display_context_t     *display,
+	unsigned long regs,
+	unsigned long value);
+extern ovl_context_t ovl_context[1];
+
+#ifndef CONFIG_MICRO_OVERLAY
+#define	OVL2_CHECK_TNC_RET(ret,a, b, c, d, e,f) ret = ovl2_check_tnc(a ,b ,c ,d ,e ,f)
+#define QUERY_OVL2_TNC_RET(ret,a, b) ret = query_ovl2_tnc(a, b)
+#else
+#define	OVL2_CHECK_TNC_RET(ret,a, b, c, d, e,f) 0
+#define QUERY_OVL2_TNC_RET(ret,a, b) 0
+#endif
+
+ovl_dispatch_t ovl_micro_dispatch_tnc[] = {
+	{
+		NULL,
+		micro_alter_ovl2_tnc,
+		NULL,
+		NULL,
+	},
+};
+
+#ifndef OVL_TNC_CACHE_QUICK_SWAP /* If caching is NOT enabled */
+
+unsigned int micro_spritec_update_src_tnc(igd_display_context_t *display,
+	ovl2_reg_tnc_t *spritec_regs_tnc,
+	igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	unsigned int      src_Bpp;
+	igd_surface_t     blt_surf;
+	igd_rect_t        blt_rect_dest;
+	igd_coord_t       blt_coord_src;
+	igd_chroma_t      chroma;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format)/8;
+
+	/* src surface */
+	spritec_regs_tnc->start = src_surf->offset;
+
+	/* DSPCLINOFF - Display C/Sprite Linear Offset Register */
+	EMGD_WRITE32(
+		     (src_rect->y1 * src_surf->pitch) + (src_rect->x1 * src_Bpp),
+		     MMIO(display) + 0x72184);
+
+	/* DSPCTILEOFF - Display C Tiled Offset Reg */
+	EMGD_WRITE32(0,
+		     MMIO(display) + 0x721a4);
+
+	/* DSPCSTRIDE - Display C/Sprite Stride Register */
+	EMGD_WRITE32(src_surf->pitch,
+		     MMIO(display) + 0x72188);
+
+	/* src pixel format */
+	switch(src_surf->pixel_format){
+	case IGD_PF_YUV422_PACKED_YUY2:
+		spritec_regs_tnc->control |= OVL2_CMD_YUV_422;
+		break;
+	case IGD_PF_YUV422_PACKED_UYVY:
+		spritec_regs_tnc->control |= OVL2_CMD_YUV_422 | OVL2_CMD_UYVY;
+		break;
+	case IGD_PF_ARGB32_8888:
+		spritec_regs_tnc->control |= OVL2_CMD_ARGB_8888;
+		break;
+	case IGD_PF_xRGB32_8888:
+		spritec_regs_tnc->control |= OVL2_CMD_RGB_8888;
+		break;
+	case IGD_PF_RGB16_565:
+		spritec_regs_tnc->control |= OVL2_CMD_RGB_565;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		spritec_regs_tnc->control |= OVL2_CMD_RGB_8;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid pixel format: 0x%lx", src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Turn off YUV to RGB conversion if the src is RGB */
+	if (!(src_surf->pixel_format & PF_TYPE_YUV)) {
+		spritec_regs_tnc->control |= (1<<19);
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+/*----------------------------------------------------------------------
+ * Function: spritec_update_video_quality_tnc()
+ *
+ * Description:
+ *   This function updates the contrast, brightness, and saturation of
+ *   the overlay using the values specified in overlay_info.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static int micro_spritec_update_video_quality_tnc(
+	igd_display_context_t        *display,
+	igd_surface_t                *src_surf,
+	igd_ovl_video_quality_info_t *video_quality)
+{
+	int                          calc_brightness_tmp = 0;
+	int                          calc_brightness     = 0;
+	unsigned int                 calc_contrast_tmp   = 0;
+	unsigned int                 calc_contrast       = 0;
+	unsigned int                 calc_saturation_tmp = 0;
+	unsigned int                 calc_saturation     = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* If the src_surf pixel format is RGB, then brightness, contrast,
+	 * and saturation should all be set to the exact default */
+	if (src_surf->pixel_format & PF_TYPE_RGB) {
+		if (video_quality->brightness != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set brightness to default");
+		}
+		if (video_quality->contrast != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set contrast to default");
+		}
+		if (video_quality->saturation != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set saturation to default");
+		}
+
+		EMGD_WRITE32(OVL2_RGB_COLOR_DEF_CONT_BRGHT,
+			MMIO(display) + 0x721D0);
+		EMGD_WRITE32(OVL2_RGB_COLOR_DEF_SATN_HUE,
+			MMIO(display) + 0x721D4);
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Brightness
+	 *************************************************************************/
+	if (0x8000 == video_quality->brightness) {
+		calc_brightness = MID_BRIGHTNESS_YUV;
+	} else if (video_quality->brightness < 0x8000) {
+		/*
+		 * we have here a brightness that is less than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = 0x8000 - video_quality->brightness;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = -128 - MID_BRIGHTNESS_YUV;
+		/*
+		 * more range if the midpoint is positive but less range
+		 * if midpoint is negative
+		 */
+
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness < -128) {
+			calc_brightness = -128;
+		}
+		if (calc_brightness > MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	} else {
+		/*
+		 * we have here a brightness that is more than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = video_quality->brightness - 0x8000;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = 127 - MID_BRIGHTNESS_YUV;
+		/*
+		 * less range if the midpoint is positive but more range
+		 * if midpoint is negative
+		 */
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness > 127) {
+			calc_brightness = 127;
+		}
+		if (calc_brightness < MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	}
+
+	EMGD_WRITE32((EMGD_READ32(MMIO(display) + 0x721D0) & 0xFFFFFF00) |
+		(calc_brightness & 0xFF),
+		MMIO(display) + 0x721D0);
+
+
+	/*************************************************************************
+	 * Contrast
+	 *************************************************************************/
+	if (0x8000 == video_quality->contrast ){
+		calc_contrast = MID_CONTRAST_YUV;
+	} else if (video_quality->contrast < 0x8000) {
+		/* we have here a contrast that is less than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = MID_CONTRAST_YUV;
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		if (calc_contrast > 0x3F) {
+			calc_contrast = 0x3F;
+		}
+	} else {
+		/* we have here a contrast that is more than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast - 0x8000;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = (0x1FF - MID_CONTRAST_YUV);
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		calc_contrast     += MID_CONTRAST_YUV;
+		if (calc_contrast > 0x1FF) {
+			calc_contrast = 0x1FF;
+		}
+	}
+
+	EMGD_WRITE32((EMGD_READ32(MMIO(display) + 0x721D0) & 0xF803FFFF) |
+		((calc_contrast & 0x1FF) << 18),
+		MMIO(display) + 0x721D0);
+
+	/*************************************************************************
+	 * Saturation
+	 *************************************************************************/
+	if (video_quality->saturation == 0x8000) {
+		calc_saturation = MID_SATURATION_YUV;
+	} else if (video_quality->saturation < 0x8000) {
+		/* we have here a saturation that is less than the default
+		 * mid point */
+		calc_saturation_tmp = video_quality->saturation;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = MID_SATURATION_YUV;
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		if (calc_saturation > 0x7F) {
+			calc_saturation = 0x7F;
+		}
+	} else {
+		/* we have here a saturation that is more than the default
+		 * mid point*/
+		calc_saturation_tmp = video_quality->saturation - 0x8000;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = (0x3FF - MID_SATURATION_YUV);
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		calc_saturation     += MID_SATURATION_YUV;
+
+		if (calc_saturation > 0x3FF) {
+			calc_saturation = 0x3FF;
+		}
+	}
+
+	EMGD_WRITE32((EMGD_READ32(MMIO(display) + 0x721D4) & 0xFFFFFC00) |
+		(calc_saturation & 0x3FF),
+		MMIO(display) + 0x721D4);
+
+
+	/*************************************************************************
+	 * Hue
+	 *************************************************************************/
+	/* Hue is always set to the default value.  It is based on the saturation
+	 * value, and having a separate hue is of minimal value. */
+	EMGD_WRITE32(EMGD_READ32(MMIO(display) + 0x721D4) & 0xF800FFFF,
+		MMIO(display) + 0x721D4);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*-----------------------------------------------------------------------------
+ * Function: spritec_update_gamma_tnc()
+ *
+ * Description:
+ *    This function sets the gamma correction values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static int micro_spritec_update_gamma_tnc(
+	igd_display_context_t *display,
+	ovl2_reg_tnc_t *spritec_regs_tnc,
+	igd_ovl_gamma_info_t * ovl_gamma)
+{
+	const int gamma_reg_input[OVL2_TOTAL_GAMMA_REG] = {8, 16, 32, 64, 128, 192};
+	const int gamma_reg_offset[OVL2_TOTAL_GAMMA_REG] = {
+		OVL2_REG_ADDR_GAMMA0,
+		OVL2_REG_ADDR_GAMMA1,
+		OVL2_REG_ADDR_GAMMA2,
+		OVL2_REG_ADDR_GAMMA3,
+		OVL2_REG_ADDR_GAMMA4,
+		OVL2_REG_ADDR_GAMMA5
+	};
+	const unsigned int gamma_def[OVL2_TOTAL_GAMMA_REG] = {
+		0x00080808,
+		0x00101010,
+		0x00202020,
+		0x00404040,
+		0x00808080,
+		0x00c0c0c0
+	};
+	unsigned int          new_gamma_red_24i_8f, new_gamma_green_24i_8f;
+	unsigned int          new_gamma_blue_24i_8f;
+	unsigned int          gamma_normal_r_24i_8f;
+	unsigned int          gamma_normal_g_24i_8f;
+	unsigned int          gamma_normal_b_24i_8f;
+	unsigned int          gamma_reg, gamma_reg_24i_8f;
+	unsigned int          i;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The gamma values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* If the overlay gamma is disabled or the display is an 8 bit mode
+	 * (second overay can not support gamma in an 8 bit mode),
+	 * set it to the default */
+	if (((ovl_gamma->flags & IGD_OVL_GAMMA_ENABLE) == IGD_OVL_GAMMA_DISABLE) ||
+		(PLANE(display)->fb_info->pixel_format == IGD_PF_ARGB8_INDEXED)) {
+		for (i = 0; i < OVL2_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			EMGD_WRITE32(gamma_def[i], MMIO(display) + gamma_reg_offset[i]);
+		}
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* It is assumed that the input value is a 24-bit number */
+	new_gamma_red_24i_8f   = ovl_gamma->red;
+	new_gamma_green_24i_8f = ovl_gamma->green;
+	new_gamma_blue_24i_8f  = ovl_gamma->blue;
+
+	/*
+	 * Program RGB for each of the 6 gamma registers
+	 */
+
+	/* Since the OS_POW_FIX function can only take an integer base,
+	 * we need to normalize the result by gamma_normal_x
+	 */
+	gamma_normal_r_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_red_24i_8f);
+	gamma_normal_g_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_green_24i_8f);
+	gamma_normal_b_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_blue_24i_8f);
+
+	for( i = 0; i < OVL2_TOTAL_GAMMA_REG; i++ )
+	{
+		/* red */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+								(1<<16)/new_gamma_red_24i_8f);
+		gamma_reg        =
+			((255 * gamma_reg_24i_8f) / gamma_normal_r_24i_8f) << 16;
+
+		/* green */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_green_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_g_24i_8f) << 8;
+
+		/* blue */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_blue_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_b_24i_8f);
+
+		/* turn overlay off (TBD) */
+
+		/* program register */
+		EMGD_WRITE32(gamma_reg, MMIO(display) + gamma_reg_offset[i]);
+
+		/* turn overlay on (TBD) */
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#endif // End if caching is not enabled.
+
+static unsigned int convert_color_key_to_hw (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	case IGD_PF_ARGB8_INDEXED:
+	default:
+		output = input;
+		break;
+	case IGD_PF_RGB16_565:
+		output =
+			((((input & 0xf800)>>11)<<3)<<16) |
+			((((input & 0x07e0)>>5 )<<2)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	}
+
+	return output;
+}
+static unsigned int convert_color_key_to_mask (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	default:
+		output = 0x00ffffff;
+		break;
+	case IGD_PF_RGB16_565:
+		output = 0x00f8fcf8;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		output = 0x000000ff;
+		break;
+	}
+
+	return output;
+}
+
+/* Convert YUV to UVY for YUV pixel formats.
+ * Do not convert RGB pixel formats */
+static unsigned int yuv_to_uvy(
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	if (IGD_PF_TYPE(pf) & PF_TYPE_YUV) {
+		output =
+			((input & 0x00ff0000) >> 16) |
+			((input & 0x0000ff00) << 8)  |
+			((input & 0x000000ff) << 8);
+	} else {
+		output = input;
+	}
+
+	return output;
+}
+
+#ifndef OVL_TNC_CACHE_QUICK_SWAP /* If caching is NOT enabled */
+
+static unsigned int micro_spritec_update_regs_tnc(
+	igd_display_context_t *display,
+	ovl2_reg_tnc_t      *spritec_regs_tnc,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	unsigned int plane_control, orig_plane_control, plane_start;
+	unsigned int pipe_num;
+	unsigned int ckey_low, ckey_high;
+	int ret;
+
+
+	EMGD_TRACE_ENTER;
+
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		/* Turn off the plane control key enable and the second overlay
+		 * control. */
+		plane_control = EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+		orig_plane_control = plane_control;
+
+		if ((plane_control & (3<<22)) != 0) {
+			plane_control &= ~(3<<22);
+		}
+		if((plane_control & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_ARGB_8888) {
+			plane_control &= ~0x04000000;
+		}
+		if(plane_control != orig_plane_control) {
+			EMGD_WRITE32(plane_control,
+				MMIO(display) + PLANE(display)->plane_reg);
+			plane_start =
+				EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+			EMGD_WRITE32(plane_start,
+				MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+		}
+
+		/* Turn the overlay Off.
+		 * Ensure we are using the correct Pipe. */
+		pipe_num = PIPE(display)->pipe_num ?
+			(1<<24)/*Pipe B*/ :
+			(0<<24)/*Pipe A*/;
+		EMGD_WRITE32(pipe_num, MMIO(display) + MMIO_OFFSET_TNC2_CONTROL);
+		EMGD_WRITE32(0, MMIO(display) + MMIO_OFFSET_TNC2_LINEAR_OFF);
+		EMGD_WRITE32(0, MMIO(display) + MMIO_OFFSET_TNC2_SURF_ADDR);
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Copy the information passed in to the HW overlay structure
+	 *************************************************************************/
+	/* Zero the control, since they will be OR'ed in with data
+	 * below */
+	spritec_regs_tnc->control = 0;
+
+	/* Interleaved/progressive and Odd/Even if interleaved */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		EMGD_ERROR("Overlay2 does not support Interleaved");
+	}
+
+	/* Dest rect information */
+	EMGD_WRITE32((dest_rect->y1<<16) | (dest_rect->x1),
+		MMIO(display) + 0x7218C);
+	EMGD_WRITE32(((dest_rect->y2 - dest_rect->y1 - 1)<<16) |
+		(dest_rect->x2 - dest_rect->x1 - 1),
+		MMIO(display) + 0x72190);
+
+	/* Src rect and surface information */
+	ret = micro_spritec_update_src_tnc(display, spritec_regs_tnc, src_surf, src_rect);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay2 updating src failed");
+		return ret;
+	}
+
+	/* Color control information */
+	if (!(flags & IGD_OVL_OSD_ON_SPRITEC)) {
+		ret = micro_spritec_update_video_quality_tnc(display, src_surf,
+			&ovl_info->video_quality);
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 video quality failed");
+			return ret;
+		}
+		ret = micro_spritec_update_gamma_tnc(display, spritec_regs_tnc, &ovl_info->gamma);
+		if (ret) {
+			EMGD_ERROR("Overlay2 gamma failed");
+			return ret;
+		}
+	}
+
+	/* Destination color key */
+	plane_control = EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+	orig_plane_control = plane_control;
+
+	EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Dest Color Key");
+		/* The mask and color key are different for the different
+		 * pixel formats */
+		EMGD_WRITE32(convert_color_key_to_hw(
+			PLANE(display)->fb_info->pixel_format, ovl_info->color_key.dest),
+			MMIO(display) + PLANE(display)->plane_reg +
+			MMIO_OFFSET_TNC2_COLORKEY_HW);
+		EMGD_WRITE32(convert_color_key_to_mask(
+			PLANE(display)->fb_info->pixel_format, ovl_info->color_key.dest),
+			MMIO(display) + PLANE(display)->plane_reg +
+			MMIO_OFFSET_TNC2_COLORKEY_MASK);
+
+		/* Both the plane control key enable and the second overlay
+		 * control order must be enabled to turn on destination color
+		 * key.  Also rewrite the plane_start which is the trigger for
+		 * Plane A/B */
+		plane_control |= (3<<22);
+
+		if (flags & IGD_OVL_OSD_ON_SPRITEC) {
+			/* Sprite C should be below plane and above first overlay */
+			spritec_regs_tnc->control |= 1;
+		} else {
+			/* Sprite C should be on the bottom of the Z order. */
+			spritec_regs_tnc->control |= 4;
+		}
+	} else {
+		EMGD_DEBUG("Overlay Disable Dest Color Key");
+		plane_control &= ~(3<<22);
+	}
+
+	if(ovl_info->color_key.flags & IGD_OVL_DST_BLEND_ENABLE) {
+		/*
+		 * If Overlay + FB Blend is requested and the FB is xRGB
+		 * turn on the ARGB format.
+		 */
+		if((plane_control & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_RGB_8888) {
+			plane_control |= DSPxCNTR_ARGB_8888;
+		}
+
+		/* Dest Blend requires Plane C on the bottom of the Z order */
+		spritec_regs_tnc->control |= 4;
+	} else {
+		if((plane_control & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_ARGB_8888) {
+			plane_control &= ~0x04000000;
+		}
+	}
+
+	if(plane_control != orig_plane_control) {
+		EMGD_WRITE32(plane_control, MMIO(display) + PLANE(display)->plane_reg);
+		plane_start = EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+
+		/* trigger registers must be updated using a command packet */
+		ret = micro_spritec_send_instr_tnc(display, PLANE(display)->plane_reg +
+			0x1c, plane_start);
+
+		/* Note: On a 915GM (maybe other platforms as well), the dest
+		 * color key is not always enabled when the plane A/B and
+		 * plane C are both modified in the same vblank.  So, ensure
+		 * they occur on a different vblank.  This should be fine,
+		 * since this will only occur the first time when enabling
+		 * the dest color key. */
+		display->context->dispatch.wait_vblank((igd_display_h)display);
+
+
+	}
+
+	/* Source Color key */
+	if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Src Color Key");
+
+		ckey_high = convert_color_key_to_hw(src_surf->pixel_format,
+			ovl_info->color_key.src_hi);
+		ckey_high = yuv_to_uvy(src_surf->pixel_format,
+			ckey_high);
+
+		ckey_low = convert_color_key_to_hw(src_surf->pixel_format,
+			ovl_info->color_key.src_lo);
+		ckey_low = yuv_to_uvy(src_surf->pixel_format,
+			ckey_low);
+
+		EMGD_WRITE32(ckey_low,  MMIO(display) + 0x72194);
+		EMGD_WRITE32(ckey_high, MMIO(display) + 0x721a0);
+		EMGD_WRITE32(7,         MMIO(display) + 0x72198);
+		spritec_regs_tnc->control |= (1<<22);
+	} else {
+		EMGD_DEBUG("Overlay Disable Src Color Key");
+		EMGD_WRITE32(0,         MMIO(display) + 0x72198);
+	}
+
+	/* General overlay information.  Turn the second overlay on.
+	 * The trigger register is the start register which causes the
+	 * overlay to update.  The trigger register is written in send_instr */
+	spritec_regs_tnc->control |= (1<<31);
+	spritec_regs_tnc->control |= PIPE(display)->pipe_num ?
+		(1<<24)/*Pipe B*/ :
+		(0<<24)/*Pipe A*/;
+	EMGD_WRITE32(spritec_regs_tnc->control,
+		MMIO(display) + MMIO_OFFSET_TNC2_CONTROL);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#else /* OVL_TNC_CACHE_QUICK_SWAP is enabled. */
+
+/* Atom E6xx overlay cache structure */
+static ovl_tnc_cache_t ovl_cache;
+
+/* Flag to signal the cache is invalid and needs
+ * to be re-initialized */
+static int ovl_cache_needs_init = TRUE;
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_src_ptr_tnc()
+ *
+ * Description:
+ *   This function updates the source offset
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+unsigned int micro_spritec_update_src_ptr_tnc(igd_surface_t *src_surf,
+					  igd_rect_t    *src_rect)
+{
+	unsigned int      src_Bpp;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format) / 8;
+
+	/* src surface */
+	ovl_cache.ovl2_regs.start = src_surf->offset;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_src_tnc()
+ *
+ * Description:
+ *   This function updates the source pitch and pixel format
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+unsigned int micro_spritec_update_src_tnc(
+	igd_display_context_t *display,
+	igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	EMGD_TRACE_ENTER;
+
+	ovl_cache.ovl2_regs.linear_offset =
+		(src_rect->y1 * src_surf->pitch) +
+		(src_rect->x1 * (IGD_PF_BPP(src_surf->pixel_format)/8));
+	ovl_cache.ovl2_regs.pitch = src_surf->pitch;
+	ovl_cache.ovl2_regs.tiled_offset = 0;
+
+	/* src pixel format */
+	switch(src_surf->pixel_format){
+	case IGD_PF_YUV422_PACKED_YUY2:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_YUV_422;
+		break;
+	case IGD_PF_YUV422_PACKED_UYVY:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_YUV_422 | OVL2_CMD_UYVY;
+		break;
+	case IGD_PF_ARGB32_8888:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_ARGB_8888;
+		break;
+	case IGD_PF_xRGB32_8888:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_8888;
+		break;
+	case IGD_PF_RGB16_565:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_565;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		ovl_cache.ovl2_regs.control |= OVL2_CMD_RGB_8;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid pixel format: 0x%lx", src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Turn off YUV to RGB conversion if the src is RGB */
+	if (!(src_surf->pixel_format & PF_TYPE_YUV)) {
+		ovl_cache.ovl2_regs.control |= (1<<19);
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_video_quality_tnc()
+ *
+ * Description:
+ *   This function updates the contrast, brightness, and saturation of
+ *   the overlay using the values specified in overlay_info.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static int micro_spritec_update_video_quality_tnc(
+	igd_display_context_t        *display,
+	igd_surface_t                *src_surf,
+	igd_ovl_video_quality_info_t *video_quality)
+{
+	int                          calc_brightness_tmp = 0;
+	int                          calc_brightness     = 0;
+	unsigned int                 calc_contrast_tmp   = 0;
+	unsigned int                 calc_contrast       = 0;
+	unsigned int                 calc_saturation_tmp = 0;
+	unsigned int                 calc_saturation     = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* If the src_surf pixel format is RGB, then brightness, contrast,
+	 * and saturation should all be set to the exact default */
+	if (src_surf->pixel_format & PF_TYPE_RGB) {
+		if (video_quality->brightness != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set brightness to default");
+		}
+		if (video_quality->contrast != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set contrast to default");
+		}
+		if (video_quality->saturation != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set saturation to default");
+		}
+
+		ovl_cache.ovl2_regs.cont_bright = OVL2_RGB_COLOR_DEF_CONT_BRGHT;
+		ovl_cache.ovl2_regs.satn_hue =  OVL2_RGB_COLOR_DEF_SATN_HUE;
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Brightness
+	 *************************************************************************/
+	if (0x8000 == video_quality->brightness) {
+		calc_brightness = MID_BRIGHTNESS_YUV;
+	} else if (video_quality->brightness < 0x8000) {
+		/*
+		 * we have here a brightness that is less than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = 0x8000 - video_quality->brightness;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = -128 - MID_BRIGHTNESS_YUV;
+		/*
+		 * more range if the midpoint is positive but less range
+		 * if midpoint is negative
+		 */
+
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness < -128) {
+			calc_brightness = -128;
+		}
+		if (calc_brightness > MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	} else {
+		/*
+		 * we have here a brightness that is more than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = video_quality->brightness - 0x8000;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = 127 - MID_BRIGHTNESS_YUV;
+		/*
+		 * less range if the midpoint is positive but more range
+		 * if midpoint is negative
+		 */
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness > 127) {
+			calc_brightness = 127;
+		}
+		if (calc_brightness < MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	}
+
+	ovl_cache.ovl2_regs.cont_bright =
+		(ovl_cache.ovl2_regs.cont_bright & 0xFFFFFF00) |
+		(calc_brightness & 0xFF);
+
+
+	/*************************************************************************
+	 * Contrast
+	 *************************************************************************/
+	if (0x8000 == video_quality->contrast ){
+		calc_contrast = MID_CONTRAST_YUV;
+	} else if (video_quality->contrast < 0x8000) {
+		/* we have here a contrast that is less than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = MID_CONTRAST_YUV;
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		if (calc_contrast > 0x3F) {
+			calc_contrast = 0x3F;
+		}
+	} else {
+		/* we have here a contrast that is more than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast - 0x8000;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = (0x1FF - MID_CONTRAST_YUV);
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		calc_contrast     += MID_CONTRAST_YUV;
+		if (calc_contrast > 0x1FF) {
+			calc_contrast = 0x1FF;
+		}
+	}
+
+	ovl_cache.ovl2_regs.cont_bright =
+		(ovl_cache.ovl2_regs.cont_bright & 0xF803FFFF ) |
+		((calc_contrast & 0x1FF) << 18);
+
+
+	/*************************************************************************
+	 * Saturation
+	 *************************************************************************/
+	if (video_quality->saturation == 0x8000) {
+		calc_saturation = MID_SATURATION_YUV;
+	} else if (video_quality->saturation < 0x8000) {
+		/* we have here a saturation that is less than the default
+		 * mid point */
+		calc_saturation_tmp = video_quality->saturation;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = MID_SATURATION_YUV;
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		if (calc_saturation > 0x7F) {
+			calc_saturation = 0x7F;
+		}
+	} else {
+		/* we have here a saturation that is more than the default
+		 * mid point*/
+		calc_saturation_tmp = video_quality->saturation - 0x8000;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = (0x3FF - MID_SATURATION_YUV);
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		calc_saturation     += MID_SATURATION_YUV;
+
+		if (calc_saturation > 0x3FF) {
+			calc_saturation = 0x3FF;
+		}
+	}
+
+	ovl_cache.ovl2_regs.satn_hue =
+		(ovl_cache.ovl2_regs.satn_hue & 0xFFFFFC00 ) |
+		(calc_saturation & 0x3FF);
+
+	/*************************************************************************
+	 * Hue
+	 *************************************************************************/
+	/* Hue is always set to the default value.  It is based on the saturation
+	 * value, and having a separate hue is of minimal value. */
+	ovl_cache.ovl2_regs.satn_hue =
+		(ovl_cache.ovl2_regs.satn_hue & 0xF800FFFF);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+static const int gamma_reg_input[OVL2_TOTAL_GAMMA_REG] = {8, 16, 32, 64, 128, 192};
+static const int gamma_reg_offset[OVL2_TOTAL_GAMMA_REG] = {
+	OVL2_REG_ADDR_GAMMA0,
+	OVL2_REG_ADDR_GAMMA1,
+	OVL2_REG_ADDR_GAMMA2,
+	OVL2_REG_ADDR_GAMMA3,
+	OVL2_REG_ADDR_GAMMA4,
+	OVL2_REG_ADDR_GAMMA5
+};
+static const unsigned int gamma_def[OVL2_TOTAL_GAMMA_REG] = {
+	0x00080808,
+	0x00101010,
+	0x00202020,
+	0x00404040,
+	0x00808080,
+	0x00c0c0c0
+};
+
+/*-----------------------------------------------------------------------------
+ * Function: spritec_update_gamma_tnc()
+ *
+ * Description:
+ *    This function sets the gamma correction values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static int micro_spritec_update_gamma_tnc(
+	igd_display_context_t *display,
+	igd_ovl_gamma_info_t *ovl_gamma)
+{
+	unsigned int          new_gamma_red_24i_8f, new_gamma_green_24i_8f;
+	unsigned int          new_gamma_blue_24i_8f;
+	unsigned int          gamma_normal_r_24i_8f;
+	unsigned int          gamma_normal_g_24i_8f;
+	unsigned int          gamma_normal_b_24i_8f;
+	unsigned int          gamma_reg, gamma_reg_24i_8f;
+	unsigned int          i;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The gamma values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* If the overlay gamma is disabled or the display is an 8 bit mode
+	 * (second overay can not support gamma in an 8 bit mode),
+	 * set it to the default */
+	if (((ovl_gamma->flags&IGD_OVL_GAMMA_ENABLE)==IGD_OVL_GAMMA_DISABLE) ||
+	     (PLANE(display)->fb_info->pixel_format == IGD_PF_ARGB8_INDEXED)) {
+		for (i = 0; i < OVL2_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			ovl_cache.ovl2_regs.gamma_regs[i] = gamma_def[i];
+		}
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* It is assumed that the input value is a 24-bit number */
+	new_gamma_red_24i_8f   = ovl_gamma->red;
+	new_gamma_green_24i_8f = ovl_gamma->green;
+	new_gamma_blue_24i_8f  = ovl_gamma->blue;
+
+	/*
+	 * Program RGB for each of the 6 gamma registers
+	 */
+
+	/* Since the OS_POW_FIX function can only take an integer base,
+	 * we need to normalize the result by gamma_normal_x
+	 */
+	gamma_normal_r_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_red_24i_8f);
+	gamma_normal_g_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_green_24i_8f);
+	gamma_normal_b_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_blue_24i_8f);
+
+	for( i = 0; i < OVL2_TOTAL_GAMMA_REG; i++ )
+	{
+		/* red */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+					      (1<<16)/new_gamma_red_24i_8f);
+		gamma_reg =
+			((255 * gamma_reg_24i_8f) / gamma_normal_r_24i_8f) << 16;
+
+		/* green */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+					      (1<<16)/new_gamma_green_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_g_24i_8f) << 8;
+
+		/* blue */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+					      (1<<16)/new_gamma_blue_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_b_24i_8f);
+
+		/* program register */
+		ovl_cache.ovl2_regs.gamma_regs[i] = gamma_reg;
+
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_disable_ovl_tnc()
+ *
+ * Description:
+ * Write the registers needed to turn off the overlay.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static unsigned int micro_spritec_disable_ovl_tnc(
+	igd_display_context_t *display	)
+{
+	unsigned int pipe_num;
+	unsigned int plane_control, orig_plane_control, plane_start;
+
+	EMGD_TRACE_ENTER;
+
+	/* Turn off the plane control key enable and the second overlay
+	 * control. */
+	plane_control =
+		EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+	orig_plane_control = plane_control;
+
+	if ((plane_control & (3<<22)) != 0) {
+		plane_control &= ~(3<<22);
+	}
+	if(((plane_control & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_ARGB_8888) && 
+		(!display->context->mod_dispatch.fb_blend_ovl_override)) {
+		/* Preserve the state of the register if FB blend +
+		 * Ovl override is invoked */
+		plane_control &= ~0x04000000;
+	}
+	if(plane_control != orig_plane_control) {
+		EMGD_WRITE32(plane_control,
+			MMIO(display) + PLANE(display)->plane_reg);
+		plane_start = EMGD_READ32(
+			MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+		EMGD_WRITE32(plane_start,
+			MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+	}
+
+	/* Turn the overlay Off.
+	 * Ensure we are using the correct Pipe. */
+	pipe_num = PIPE(display)->pipe_num ?
+		(1<<24)/*Pipe B*/ :
+		(0<<24)/*Pipe A*/;
+	EMGD_WRITE32(pipe_num, MMIO(display) + MMIO_OFFSET_TNC2_CONTROL);
+	EMGD_WRITE32(0, MMIO(display) + MMIO_OFFSET_TNC2_SURF_ADDR);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_clear_cache_tnc()
+ *
+ * Description:
+ *
+ *----------------------------------------------------------------------*/
+
+static void micro_spritec_clear_cache_tnc(
+        igd_display_context_t *display,
+	unsigned int           flags)
+{
+	/* Force every cache check to miss */
+	OS_MEMSET(&ovl_cache, 0, sizeof(ovl_tnc_cache_t));
+
+	/* We just set our cached flags to 0, which might accidently
+	 * match up with "OFF" for some important incoming flag
+	 * bits, causing us to think we already handled them when
+	 * we didn't.  So set our cached flags to the exact
+	 * opposite of the incoming flags, which will force
+	 * us to test and handle every single bit, regardless
+	 * of whether it is on or off. */
+	ovl_cache.flags = ~flags;
+
+	/* init our cached registers */
+	ovl_cache.ovl2_regs.plane_control =
+		EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg);
+	ovl_cache.ovl2_regs.plane_start =
+		EMGD_READ32(MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+
+	/* initialization complete */
+	ovl_cache_needs_init = FALSE;
+
+}
+
+
+/*-----------------------------------------------------------------------------
+ * Function: spritec_update_colorkey_tnc()
+ *
+ * Description:
+ *    This function sets the colorkey values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static void micro_spritec_update_colorkey_tnc(
+	igd_display_context_t *display,
+	igd_surface_t         *src_surf,
+	igd_ovl_info_t        *ovl_info)
+{
+	unsigned int ckey_low, ckey_high;
+
+	/* Destination color key */
+	EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+
+		EMGD_DEBUG("Overlay Enable Dest Color Key");
+		/* The mask and color key are different for the
+		 * different pixel formats */
+		ovl_cache.ovl2_regs.colorkey_hw =
+			convert_color_key_to_hw
+			(PLANE(display)->fb_info->pixel_format,
+			 ovl_info->color_key.dest);
+
+		ovl_cache.ovl2_regs.colorkey_mask =
+			convert_color_key_to_mask
+			(PLANE(display)->fb_info->pixel_format,
+			 ovl_info->color_key.dest);
+		/*
+		 * Both the plane control key enable and the second
+		 * overlay control order must be enabled to turn on
+		 * destination color key.  Also rewrite the
+		 * plane_start which is the trigger for Plane A/B
+		 */
+		ovl_cache.ovl2_regs.plane_control |= (3<<22);
+
+	} else {
+		EMGD_DEBUG("Overlay Disable Dest Color Key");
+		ovl_cache.ovl2_regs.plane_control &= ~(3<<22);
+	}
+
+	if(ovl_info->color_key.flags & IGD_OVL_DST_BLEND_ENABLE) {
+		/*
+		 * If Overlay + FB Blend is requested and the FB is xRGB
+		 * turn on the ARGB format.
+		 */
+		if((ovl_cache.ovl2_regs.plane_control & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_RGB_8888) {
+			ovl_cache.ovl2_regs.plane_control |= DSPxCNTR_ARGB_8888;
+		}
+	} else {
+		if(((ovl_cache.ovl2_regs.plane_control & DSPxCNTR_SRC_FMT_MASK) == DSPxCNTR_ARGB_8888) && 
+			(!display->context->mod_dispatch.fb_blend_ovl_override)){
+			/* Preserve the state of the register if FB blend +
+			 * Ovl override is invoked */
+			ovl_cache.ovl2_regs.plane_control &= ~0x04000000;
+		}
+	}
+
+	/* Source Color key */
+	if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Src Color Key");
+
+		ckey_high = convert_color_key_to_hw
+			(src_surf->pixel_format,
+			 ovl_info->color_key.src_hi);
+		ckey_high = yuv_to_uvy(src_surf->pixel_format,
+				       ckey_high);
+
+		ckey_low = convert_color_key_to_hw
+			(src_surf->pixel_format,
+			 ovl_info->color_key.src_lo);
+		ckey_low = yuv_to_uvy(src_surf->pixel_format,
+				      ckey_low);
+
+		ovl_cache.ovl2_regs.ckey_low = ckey_low;
+		ovl_cache.ovl2_regs.ckey_high = ckey_high;
+		ovl_cache.ovl2_regs.ckey_enable = 7;
+		ovl_cache.ovl2_regs.control |= (1<<22);
+	} else {
+		EMGD_DEBUG("Overlay Disable Src Color Key");
+		ovl_cache.ovl2_regs.ckey_enable = 0;
+	}
+
+}
+
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_write_cache_tnc()
+ *
+ * Description:
+ *
+ *----------------------------------------------------------------------*/
+
+static void micro_spritec_write_cache_tnc(
+        igd_display_context_t *display,
+	ovl2_reg_tnc_t        *spritec_regs_tnc,
+	igd_ovl_info_t        *ovl_info,
+	int                    cache_changed)
+{
+	int i;
+
+	/*
+	 * Now write all the changed registers to the HW
+	 * TODO: Or should we write all the registers, regardless of
+	 * if they have changed?
+	 * TODO: It may be beneficial to turn off overlay while
+	 * updateing the regs?
+	*/
+
+
+	/* Write dest rect information */
+    if (cache_changed & IGD_OVL_TNC_UPDATE_DEST) {
+		EMGD_WRITE32( ovl_cache.ovl2_regs.dest_rect_x1y1,
+			    MMIO(display) + MMIO_OFFSET_TNC2_DEST_RECT_X1Y1);
+		EMGD_WRITE32( ovl_cache.ovl2_regs.dest_rect_w_h,
+			    MMIO(display) + MMIO_OFFSET_TNC2_DEST_RECT_W_H);
+	}
+
+
+	/* Write source information */
+    if (cache_changed & (IGD_OVL_TNC_UPDATE_SURF |
+                             IGD_OVL_TNC_UPDATE_SRC  ) ) {
+		EMGD_WRITE32(ovl_cache.ovl2_regs.linear_offset,
+			   MMIO(display) + MMIO_OFFSET_TNC2_LINEAR_OFF);
+		EMGD_WRITE32(ovl_cache.ovl2_regs.pitch,
+			   MMIO(display) + MMIO_OFFSET_TNC2_PITCH);
+		EMGD_WRITE32(ovl_cache.ovl2_regs.tiled_offset,
+			   MMIO(display) + MMIO_OFFSET_TNC2_DSPC_TILE_OFF);
+	}
+
+
+	/* write the quality information */
+    if (cache_changed & (IGD_OVL_TNC_UPDATE_VQ   |
+                             IGD_OVL_TNC_UPDATE_SURF ) ) {
+		EMGD_WRITE32(ovl_cache.ovl2_regs.cont_bright,
+			MMIO(display) + MMIO_OFFSET_TNC2_CONT_BRIGHT);
+		EMGD_WRITE32(ovl_cache.ovl2_regs.satn_hue,
+			MMIO(display) + MMIO_OFFSET_TNC2_SATN_HUE);
+	}
+
+	/* Write the gamma */
+    if (cache_changed & IGD_OVL_TNC_UPDATE_GAMMA) {
+		for (i = 0; i < OVL2_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			EMGD_WRITE32(ovl_cache.ovl2_regs.gamma_regs[i],
+			   MMIO(display) + gamma_reg_offset[i]);
+		}
+	}
+
+	/* Write the colorkey data */
+    if (cache_changed & IGD_OVL_TNC_UPDATE_COLORKEY) {
+
+		/* Dest color key */
+		if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+
+			/* Write the regs needed to turn it on */
+			EMGD_WRITE32(ovl_cache.ovl2_regs.colorkey_hw,
+				   MMIO(display) +
+				   PLANE(display)->plane_reg +
+				   MMIO_OFFSET_TNC2_COLORKEY_HW);
+
+			EMGD_WRITE32(ovl_cache.ovl2_regs.colorkey_mask,
+				   MMIO(display) +
+				   PLANE(display)->plane_reg +
+				   MMIO_OFFSET_TNC2_COLORKEY_MASK);
+		}
+
+		EMGD_WRITE32(ovl_cache.ovl2_regs.plane_control,
+			   MMIO(display) + PLANE(display)->plane_reg);
+
+		EMGD_WRITE32(ovl_cache.ovl2_regs.plane_start,
+			   MMIO(display) + PLANE(display)->plane_reg + 0x1c);
+
+		/*
+		 * Note: On a 915GM (maybe other platforms as well),
+		 * the dest color key is not always enabled when the
+		 * plane A/B and plane C are both modified in the
+		 * same vblank.  So, ensure they occur on a different
+		 * vblank.  This should be fine, since this will only
+		 * occur the first time when enabling the dest color
+		 * key.
+		 */
+		if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+			display->context->dispatch.
+				wait_vblank((igd_display_h)display);
+		}
+
+		/* Source Color key */
+		if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+			EMGD_WRITE32(ovl_cache.ovl2_regs.ckey_low,
+				   MMIO(display) + MMIO_OFFSET_TNC2_CKEY_LOW);
+			EMGD_WRITE32(ovl_cache.ovl2_regs.ckey_high,
+				   MMIO(display) + MMIO_OFFSET_TNC2_CKEY_HIGH);
+		}
+		EMGD_WRITE32(ovl_cache.ovl2_regs.ckey_enable,
+			   MMIO(display) + MMIO_OFFSET_TNC2_CKEY_ENABLE);
+	}
+
+	/* Write the control register, but not the start register.
+	   The trigger register is the start register
+	   which causes the overlay to update.  The trigger
+	   register is written in send_instr */
+
+	EMGD_WRITE32(ovl_cache.ovl2_regs.control,
+		   MMIO(display) + MMIO_OFFSET_TNC2_CONTROL);
+
+	spritec_regs_tnc->start = ovl_cache.ovl2_regs.start;
+	spritec_regs_tnc->control = ovl_cache.ovl2_regs.control;
+
+}
+
+
+
+
+
+/*----------------------------------------------------------------------
+ * Function: micro_spritec_update_regs_tnc()
+ *
+ * Description:
+ * Examine the incoming overlay parameters, and update the overlay hardware
+ * regs according to what changed.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static unsigned int micro_spritec_update_regs_tnc(
+	igd_display_context_t *display,
+	ovl2_reg_tnc_t      *spritec_regs_tnc,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	// unsigned int ckey_low, ckey_high;
+	// int i,
+	int ret;
+	int cache_changed;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* Fast path for turning off overlay. No need for cache */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		ret = micro_spritec_disable_ovl_tnc(display);
+
+		/* Reset the cache */
+		ovl_cache_needs_init = TRUE;
+
+		EMGD_TRACE_EXIT;
+		return ret;
+	}
+
+	/* Init the cache if needed */
+	if ((ovl_cache_needs_init) || (ovl_context->ovl_display_swapped)){
+		micro_spritec_clear_cache_tnc(display, flags);
+		ovl_context->ovl_display_swapped = 0;
+	}
+
+	/* See what has changed in the cache */
+	cache_changed = get_cache_changes_tnc (
+		src_surf,
+		src_rect,
+		dest_rect,
+		ovl_info,
+		flags,
+		&ovl_cache);
+
+	/*
+	 * Perhaps the biggest challenge of caching the overlay
+	 * state is what to do with the command and config regs.
+	 * Normally we would clear command and config to 0 here,
+	 * and let the update process set only the bits that are
+	 * needed.  But doing this would invalidate our cache.
+	 * Instead we are relying on the above call to
+	 * get_cache_changes() to clear those bits in command
+	 * and config that will be changing
+	 */
+
+
+	/* Normally we would set interleave parameters here,
+	 * but the secondary overlay does not support interleave.*/
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		EMGD_ERROR("Overlay2 does not support Interleaved");
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Has our destination rectangle changed? */
+	if (cache_changed & IGD_OVL_TNC_UPDATE_DEST) {
+		ovl_cache.ovl2_regs.dest_rect_x1y1 =
+			( (dest_rect->y1 << 16) | dest_rect->x1 );
+		ovl_cache.ovl2_regs.dest_rect_w_h =
+			( (dest_rect->y2 - dest_rect->y1 - 1) << 16) |
+			  (dest_rect->x2 - dest_rect->x1 - 1)        ;
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Always update the source pointers every frame. */
+	ret = micro_spritec_update_src_ptr_tnc(src_surf,
+					   src_rect);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay2 updating src failed");
+		EMGD_TRACE_EXIT;
+		return ret;
+	}
+
+
+	/* ----------------------------------------------------------*/
+	/* Did either the Src rect or surface change? */
+    if (cache_changed & (IGD_OVL_TNC_UPDATE_SURF |
+                             IGD_OVL_TNC_UPDATE_SRC  ) ) {
+		ret = micro_spritec_update_src_tnc(display,
+						   src_surf,
+						   src_rect);
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 updating src failed");
+			EMGD_TRACE_EXIT;
+			return ret;
+		}
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Did the quality information change? */
+    if (cache_changed & (IGD_OVL_TNC_UPDATE_VQ   |
+                             IGD_OVL_TNC_UPDATE_SURF ) ) {
+		ret = micro_spritec_update_video_quality_tnc(display,
+			src_surf, &ovl_info->video_quality);
+		if (ret) {
+			EMGD_ERROR_EXIT("Overlay2 video quality failed");
+			EMGD_TRACE_EXIT;
+			return ret;
+		}
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Did the gamma change? */
+    if (cache_changed & IGD_OVL_TNC_UPDATE_GAMMA) {
+		ret = micro_spritec_update_gamma_tnc(display, &ovl_info->gamma);
+		if (ret) {
+			EMGD_ERROR("Overlay2 gamma failed");
+			EMGD_TRACE_EXIT;
+			return ret;
+		}
+
+	}
+
+	/* ----------------------------------------------------------*/
+	/* Did the color key change? */
+    if (cache_changed & IGD_OVL_TNC_UPDATE_COLORKEY) {
+		micro_spritec_update_colorkey_tnc(display,
+						  src_surf,
+						  ovl_info);
+	}
+
+
+	if ((ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) ||
+		(ovl_info->color_key.flags & IGD_OVL_DST_BLEND_ENABLE)) {
+		if (flags & IGD_OVL_OSD_ON_SPRITEC) {
+			ovl_cache.ovl2_regs.control |= 1;
+		} else {
+			/* Sprite C should be on the bottom of the Z order.
+			 * Plane B should be above Sprite C */
+			ovl_cache.ovl2_regs.control |= 4;
+		}
+	}
+
+	/* General overlay information.  Turn the second overlay on. */
+	ovl_cache.ovl2_regs.control |= (1<<31);
+	if (PIPE(display)->pipe_num == 1) {
+		ovl_cache.ovl2_regs.control |= (1<<24);/*Pipe B*/
+	} else {
+		ovl_cache.ovl2_regs.control &= ~(1<<24);/*Pipe A*/
+	}
+
+	/*
+	 * Now write all the changes to the part
+	 */
+	micro_spritec_write_cache_tnc(display,
+				      spritec_regs_tnc,
+				      ovl_info,
+				      cache_changed);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#endif
+
+unsigned int micro_spritec_send_instr_tnc(
+	igd_display_context_t     *display,
+	unsigned long regs,
+	unsigned long value)
+{
+	EMGD_TRACE_ENTER;
+
+	/* Send a load register instruction to write the Plane C sprite address
+	 * which is the trigger register.
+	 * This is an instruction, so it happens after blend, and since it
+	 * is an instruction, we do not have to poll waiting for it. */
+
+	EMGD_WRITE32(value, MMIO(display) + regs);
+
+	ovl_context->sync2 = 0;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+int micro_prepare_ovl2_tnc(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	ovl2_reg_tnc_t      *spritec_regs_tnc,
+	unsigned int         flags)
+{
+	int ret = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Check to ensure the overlay can be used given the current mode as
+	 * well as what the IAL is asking for.  If not return an error. */
+
+	if( (OVL2_CHECK_TNC_RET(ret,display, src_surf, src_rect,
+										dest_rect, ovl_info,flags)) )
+	{
+		if (ret) {
+			printk(KERN_ERR "Overlay2 Check failed\n");
+			return ret;
+		}
+	}
+
+	/* Check if last flip is still pending.
+	 * This is necessary for the following reasons:
+	 *    - If the previous instructions have not been processed, then the
+	 *      spritec_regs_tnc is still in use and can not be overwritten.
+	 */
+	if( (QUERY_OVL2_TNC_RET(ret,(igd_display_h)display,
+								IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE)) )
+	{
+		//printk(KERN_ERR "QEURY_OVL2_TNC_RET failedi\n");
+		if ((FALSE == ret) &&
+			(flags & IGD_OVL_ALTER_ON)) {
+			/* Only return an error if the overlay is on.  If turning it off,
+			* allow it to continue, since something may have failed and we
+			* should try our best to turn the overlay off. */
+			return -IGD_ERROR_HWERROR;
+		}
+	}
+	/* Update all Overlay Update Registers */
+	ret = micro_spritec_update_regs_tnc(display, spritec_regs_tnc,
+		src_surf, src_rect, dest_rect, ovl_info,
+		flags);
+	if (ret) {
+		printk(KERN_ERR "Sprite C update Registers failed");
+		return ret;
+	}
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+int micro_alter_ovl2_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl2_reg_tnc_t spritec_regs_tnc;
+	int ret=0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Initialize structure so compilers don't complain */
+	OS_MEMSET(&spritec_regs_tnc, 0, sizeof(ovl2_reg_tnc_t));
+
+	if (micro_prepare_ovl2_tnc(display, src_surf, src_rect, dest_rect,
+		ovl_info, &spritec_regs_tnc, flags)) {
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Directlt write the register to update 2nd overlay */
+#ifdef OVL_TNC_CACHE_QUICK_SWAP
+	ret = micro_spritec_send_instr_tnc(display,
+		MMIO_OFFSET_TNC2_SURF_ADDR,
+		ovl_cache.ovl2_regs.start);
+#else
+	ret = micro_spritec_send_instr_tnc(display,
+		MMIO_OFFSET_TNC2_SURF_ADDR,
+		spritec_regs_tnc.start);
+#endif
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_regs_tnc.h b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_regs_tnc.h
new file mode 100644
index 0000000..9f2ac10
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_regs_tnc.h
@@ -0,0 +1,76 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl2_regs_tnc.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for napa overlay engine. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized structures and functions internal to
+ *  the napa core's overlay engine
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _OVL2_REGS_TNC_H
+#define _OVL2_REGS_TNC_H
+/* Second Overlay Structure.
+ * These registers are the trigger registers and should be written
+ * last. */
+typedef struct _ovl2_reg_tnc{
+	unsigned int control;
+	unsigned int start;
+} ovl2_reg_tnc_t;
+
+/* Color Correction */
+#define OVL2_RGB_COLOR_DEF_CONT_BRGHT    0x1000000
+#define OVL2_RGB_COLOR_DEF_SATN_HUE      0x0000080
+
+/* Overlay Command Definitions */
+/* 422 Swap */
+#define OVL2_CMD_YVYU				0x00020000
+#define OVL2_CMD_UYVY				0x00010000
+#define OVL2_CMD_VYUY				0x00030000
+
+/* Source Format */
+#define OVL2_CMD_ARGB_8888			0x1C000000
+#define OVL2_CMD_RGB_8888			0x18000000
+#define OVL2_CMD_RGB_565			0x14000000
+#define OVL2_CMD_RGB_555			0x10000000
+#define OVL2_CMD_RGB_8				0x08000000
+#define OVL2_CMD_YUV_422			0x00000000 /*packed YUV422*/
+#define OVL2_CMD_SRC_FMT_MASK		0x3C000000 /*mask for above*/
+
+#define OVL2_REG_ADDR_GAMMA5						0x721E0
+#define OVL2_REG_ADDR_GAMMA4						0x721E4
+#define OVL2_REG_ADDR_GAMMA3						0x721E8
+#define OVL2_REG_ADDR_GAMMA2						0x721EC
+#define OVL2_REG_ADDR_GAMMA1						0x721F0
+#define OVL2_REG_ADDR_GAMMA0						0x721F4
+#define OVL2_TOTAL_GAMMA_REG						6
+
+/* following value are needed because hardware seems to display yuv slightly dimmer
+   than RGB when color data is calculated out to be equal */
+#define MID_CONTRAST_YUV					0x43//4a
+#define MID_SATURATION_YUV					0x91//92
+#define MID_BRIGHTNESS_YUV					-5
+#endif /* _OVL2_REGS_NAP_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.c b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.c
new file mode 100644
index 0000000..1c577c4
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.c
@@ -0,0 +1,460 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl2_tnc.c
+ * $Revision: 1.21 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains function that actually programs the second
+ *  overlay with the bits to properly configure the overlay
+ *  Also includes functions to execute the second overlay flip
+ *  instruction, and query the overlay flip status.
+ *  Also contains some hardware capabilities querrying functions
+ *  for upper overlay layer to get this chips second overlay
+ *  capabilities
+ *-----------------------------------------------------------------------------
+ */
+/* Referenced from Napa. Need change later */
+
+#define MODULE_NAME hal.overlay
+
+#include <io.h>
+#include <memory.h>
+#include <intelpci.h>
+#include <math_fix.h>
+
+
+#include <igd_mode.h>
+
+#include <utils.h>
+#include <general.h>
+#include <context.h>
+#include <rb.h>
+#include <mode.h>
+
+#include <tnc/instr.h>
+#include <tnc/regs.h>
+#include <tnc/cmd.h>
+#include <tnc/context.h>
+
+#include "ovl2_tnc.h"
+#include "ovl2_regs_tnc.h"
+#include "../cmn/ovl_dispatch.h"
+#include "../cmn/ovl_virt.h"
+
+extern unsigned long sprite_pixel_formats_tnc[];
+extern int micro_prepare_ovl2_tnc(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	ovl2_reg_tnc_t      *spritec_regs_tnc,
+	unsigned int         flags);
+
+/*----------------------------------------------------------------------
+ * Function: ovl2_check_pf_tnc()
+ * Parameters: unsigned int requested_pixel_format -
+ *             according to definitions in igd_mode.h
+ *
+ * Description:
+ *
+ * Returns:
+ *   TRUE on Success
+ *   FALSE on The first pixel format that is supported
+ *----------------------------------------------------------------------*/
+static unsigned int ovl2_check_pf_tnc(
+	igd_display_context_t *display,
+	unsigned int requested_pixel_format)
+{
+	unsigned long *spr_pf = sprite_pixel_formats_tnc;
+	int temp_loop = 0;
+
+	while(spr_pf[temp_loop]) {
+		if(spr_pf[temp_loop] == requested_pixel_format) {
+			return TRUE;
+		}
+		++temp_loop;
+	}
+
+	return FALSE;
+}
+
+static unsigned int get_uv_shift_x (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV422_PACKED_YUY2:
+	case IGD_PF_YUV422_PACKED_UYVY:
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+static unsigned int get_uv_shift_y (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+unsigned int ovl2_check_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	igd_timing_info_t *timing;
+	unsigned int min_w, min_h;
+
+	EMGD_TRACE_ENTER;
+
+	if (!display){
+	    EMGD_ERROR_EXIT("display is null");
+	    return -IGD_ERROR_INVAL;
+	}
+	if (!PIPE(display)){
+	    EMGD_ERROR_EXIT("PIPE(display) is null");
+	    return -IGD_ERROR_INVAL;
+	}
+
+	timing = PIPE(display)->timing;
+
+	/* The following parameters are only valid if the overlay is on, so
+	 * return success if the overlay is being turned off. */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	timing = PIPE(display)->timing;
+
+	if(!timing) {
+	    EMGD_ERROR_EXIT("timing is null\n");
+	    return -IGD_ERROR_INVAL;
+	}
+
+	/*************************************************************************
+	 * Ensure the framebuffer dotclock does not exceed the board SKU
+	 * max dotclock
+	 *************************************************************************/
+	/* Make it chipset-specific */
+	/* DCT-PC99TA crashes with dotclock > 300MHz */
+	if(timing->dclk >= 340000){
+		EMGD_ERROR_EXIT("Cannot support dotclock > 340MHz for this SKU");
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/*************************************************************************
+	 * Ensure the overlay surface is ok and can be properly displayed.
+	 * This ensures the following is valid:
+	 *    - Ensure x1, x2, y1, y2 are pixel aligned
+	 *    - 2 pixels or greater in width and height
+	 *    - Pixel format is supported by the overlay
+	 *    - Pitch is <= 16KB
+	 *    - Based on the pixel format, the width is supported
+	 *************************************************************************/
+	if (!src_surf){
+	    EMGD_ERROR_EXIT("src_surf is null");
+	    return -IGD_ERROR_INVAL;
+	}
+	if (!src_rect){
+	    EMGD_ERROR_EXIT("src_rect is null");
+	    return -IGD_ERROR_INVAL;
+	}
+	/* Get the minimum size of 1 pixel in width and height for y, u, and v.
+	 */
+	min_w = 1 << get_uv_shift_x(src_surf->pixel_format);
+	min_h = 1 << get_uv_shift_y(src_surf->pixel_format);
+
+	if (((src_rect->x2 - src_rect->x1) < min_w*2) ||
+		((src_rect->y2 - src_rect->y1) < min_h*2)) {
+		EMGD_ERROR_EXIT(
+			"Overlay2 source width or height is < 2 pixels (%dx%d)\n",
+			src_rect->x2 - src_rect->x1, src_rect->y2 - src_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (FALSE == ovl2_check_pf_tnc(display, src_surf->pixel_format)) {
+		EMGD_ERROR_EXIT("Overlay2 source pixel format unsupported (pf:0x%lx)",
+			src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	if (src_surf->pitch > 16384) {
+		EMGD_ERROR_EXIT("Overlay2 source pitch (%d) > 16KB",
+			src_surf->pitch);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/*************************************************************************
+	 * Ensure the location on the framebuffer is ok and can be properly
+	 * displayed
+	 * This ensures the following is valid:
+	 *    - Greater than 1 pixel width and height
+	 *    - Will be displayed on screen (not panned off)
+	 *************************************************************************/
+	if (!dest_rect){
+	    EMGD_ERROR_EXIT("dest_rect is null");
+	    return -IGD_ERROR_INVAL;
+	}
+	if (((dest_rect->x2 - dest_rect->x1) <= 1) ||
+		((dest_rect->y2 - dest_rect->y1) <= 1)) {
+		EMGD_ERROR_EXIT(
+			"Overlay2 dest width or height is single pixel (%dx%d)\n",
+			dest_rect->x2 - dest_rect->x1, dest_rect->y2 - dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((dest_rect->x1 >= timing->width) ||
+		(dest_rect->y1 >= timing->height)) {
+		EMGD_ERROR_EXIT(
+			"Overlay2 dest is panned off the screen (%d,%d)\n",
+			dest_rect->x1, dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+unsigned int ovl2_send_instr_tnc(
+	igd_display_context_t     *display,
+	ovl2_reg_tnc_t    *spritec_regs_tnc,
+	unsigned int      flags)
+{
+	unsigned char *mmio = MMIO(display);
+	unsigned long tmp;
+	inter_module_dispatch_t *md;
+	platform_context_tnc_t * platform;
+
+
+	EMGD_TRACE_ENTER;
+
+	/* We dont need the CMD_WAIT_OVL2_TNC instruction coz
+	 * our alter_ovl code already querried status
+	 * for last flip completion before getting here. See
+	 * micro_prepare_ovl2_tnc called by alter_ovl2_tnc.
+	 * It calls query overlay before the next flip
+	 */
+
+	/*If Overlay+FB Blend is requested and the FB is xRGB
+	 *turn on the ARGB format. */
+	if(ovl_context->fb_blend_ovl) {
+		if((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_ON) {
+			tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x18000000) {
+				tmp = tmp & 0xc3FFFFFF;
+				EMGD_WRITE32(tmp | 0x1c000000, mmio + PLANE(display)->plane_reg);
+				EMGD_READ32(mmio + PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 0x1c);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 0x1c);
+			}
+		} else {
+			tmp =  EMGD_READ32(mmio + PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x1c000000) {
+				tmp = tmp & 0xc3FFFFFF;
+				EMGD_WRITE32(tmp | 0x18000000, mmio + PLANE(display)->plane_reg);
+				EMGD_READ32(mmio + PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 0x1c);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 0x1c);
+				OS_SLEEP(100);
+			}
+		}
+	}
+
+	/* Send a load register instruction to write the Plane C sprite address
+	 * which is the trigger register.
+	 * This is an instruction, so it happens after blend, and since it
+	 * is an instruction, we do not have to poll waiting for it. */
+	EMGD_WRITE32(spritec_regs_tnc->start, mmio + 0x7219C);
+
+	/* Since the ISR bit 0x100 actually doesnt work,
+	 * we need to setup a trigger for a VBLANK event
+	 * on Pipe-B to guarantee that the Sprite-C had
+	 * actually completed its last flip.
+	 * (ISR bit was tested on Poulsbo D2 by capturing
+	 * timestamps of quick successive alter_overlays..
+	 * checked ISR bit directly after the write to Sprite
+	 * C Address register in process_vqueue handling..
+	 * the ISR bit never changed
+	 */
+
+	md = &display->context->mod_dispatch;
+	platform = (platform_context_tnc_t *)display->context->
+					platform_context;
+	if(md && md->set_flip_pending){
+		OS_PTHREAD_MUTEX_LOCK(&platform->flip_mutex);
+		md->set_flip_pending(MMIO(display), 0x71024);
+		OS_PTHREAD_MUTEX_UNLOCK(&platform->flip_mutex);
+	}
+
+	ovl_context->sync2 = WAIT_FOR_FLIP;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+int alter_ovl2_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl2_reg_tnc_t spritec_regs_tnc;
+	int ret=0;
+
+	EMGD_TRACE_ENTER;
+
+	/* Initialize structure so compilers don't complain */
+	OS_MEMSET(&spritec_regs_tnc, 0, sizeof(ovl2_reg_tnc_t));
+
+	if (micro_prepare_ovl2_tnc(display, src_surf, src_rect, dest_rect,
+		ovl_info, &spritec_regs_tnc, flags)) {
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Send the instructions to the command queue */
+	ret = ovl2_send_instr_tnc(display, &spritec_regs_tnc, flags);
+	EMGD_DEBUG("Sprite C= %s",flags & IGD_OVL_ALTER_ON?"ON":"OFF");
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+int query_ovl2_tnc(igd_display_h display_h,
+	unsigned int flags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_h;
+	inter_module_dispatch_t *md;
+	platform_context_tnc_t * platform;
+	os_alarm_t timeout;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	switch (flags) {
+	case IGD_OVL_QUERY_IS_HW_SUPPORTED:
+		/* This is the second overlay, so HW overlay is not supported */
+		break;
+
+	case IGD_OVL_QUERY_IS_LAST_FLIP_DONE:
+		/* If there no sync to wait on, then the last flip is done, and the
+		 * Register Update has occured, simply return TRUE (Flip done).
+		 */
+		if (!ovl_context->sync2) {
+			EMGD_DEBUG("Overlay already synced");
+			EMGD_TRACE_EXIT;
+			return TRUE;
+		}
+
+		/* According to the PBL B-spec, there doesnt seem to exist any bit
+		 * for Sprite C Flip-Pending status. Testing 0x20AC in code during
+		 * virt queue's REG write shows nothing changed for Bit8. Thus, we
+		 * are using state of the VBLANK ISR bit as ovl2 flip status.
+		 * Assumption is that if were running 2nd overlay, its either clone
+		 * display or VEXT in WinCE. In either case, were not doing full screen
+		 * FB flipping, so this check should be 'statefully' accurate
+		 */
+		md = &display->context->mod_dispatch;
+		platform = (platform_context_tnc_t *)display->context->
+						platform_context;
+		if(md && md->check_flip_pending){
+			ret = OS_PTHREAD_MUTEX_LOCK(&platform->flip_mutex);
+			if(md->check_flip_pending(MMIO(display), 0x71024)){
+				OS_PTHREAD_MUTEX_UNLOCK(&platform->flip_mutex);
+				EMGD_DEBUG("Overlay2 Sync done but Flip not done");
+				return FALSE;
+			}
+			OS_PTHREAD_MUTEX_UNLOCK(&platform->flip_mutex);
+		}
+
+		/* Now that we know the last flip is done and the register update is
+		 * complete, set the sync to 0 and return TRUE (Flip done). */
+		ovl_context->sync2 = FLIP_DONE;
+		break;
+	case IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE:
+		/* Wait for 200 milliseconds for the last flip to complete.  If not
+		 * done in that time, there is likely a hardware problem so return
+		 * FALSE. */
+		timeout = OS_SET_ALARM(200);
+		do {
+			if (TRUE ==
+				query_ovl2_tnc(display_h, IGD_OVL_QUERY_IS_LAST_FLIP_DONE)) {
+				EMGD_TRACE_EXIT;
+				return TRUE;
+			}
+		} while (!OS_TEST_ALARM(timeout));
+		EMGD_ERROR_EXIT("Timeout waiting for last flip done");
+		return FALSE;
+		break;
+	case IGD_OVL_QUERY_IS_GAMMA_SUPPORTED:
+		return TRUE;
+		break;
+	case IGD_OVL_QUERY_IS_VIDEO_PARAM_SUPPORTED:
+		return TRUE;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return TRUE;
+}
+
+
+
+int query_max_size_ovl2_tnc(
+	igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height)
+{
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: Should this be size of FB? */
+	*max_width = 2048;
+	*max_height = 2048;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.h b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.h
new file mode 100644
index 0000000..300e826
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl2_tnc.h
@@ -0,0 +1,55 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl2_tnc.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for napa overlay engine. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized structures and functions internal to
+ *  the napa core's overlay engine
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _OVL2_TNC_H
+#define _OVL2_TNC_H
+extern int blend2_surf_needed_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	unsigned int         flags,
+	igd_surface_t       *blend_surf,
+	igd_rect_t          *blend_rect);
+extern int alter_ovl2_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+extern int query_ovl2_tnc(igd_display_h display_h,
+	unsigned int flags);
+extern int query_max_size_ovl2_tnc(igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height);
+#endif /* _OVL2_NAP_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_regs_tnc.h b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_regs_tnc.h
new file mode 100644
index 0000000..06e85f7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_regs_tnc.h
@@ -0,0 +1,190 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_regs_tnc.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for overlay. It should be not be
+ *  by any other module besides the overlay module itself. It contains the
+ *  neccessary hardware virtualized register dependant information including
+ *  values, structures and addresses specifically for the Napa core
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef OVL_REGS_TNC_H_
+#define OVL_REGS_TNC_H_
+
+/* Overlay Update Register Image Structure.*/
+typedef struct _ovl_reg_image_tnc{
+
+	volatile unsigned int			buffer0_yrgb_loff;
+	volatile unsigned int			buffer1_yrgb_loff;
+	volatile unsigned int			buffer0_u_loff;
+	volatile unsigned int			buffer0_v_loff;
+	volatile unsigned int			buffer1_u_loff;
+	volatile unsigned int			buffer1_v_loff;
+	volatile unsigned short			yrgb_stride;
+	volatile unsigned short			uv_stride;
+	volatile unsigned short			yrgb_vert_phase_field0;
+	volatile unsigned short			yrgb_vert_phase_field1;
+	volatile unsigned short			uv_vert_phase_field0;
+	volatile unsigned short			uv_vert_phase_field1;
+	volatile unsigned short			yrgb_hphase;
+	volatile unsigned short			uv_hphase;
+	volatile unsigned int			init_phase_shift;
+	volatile unsigned short			dest_pos_x_left;
+	volatile unsigned short			dest_pos_y_top;
+	volatile unsigned short			dest_width_x;
+	volatile unsigned short			dest_height_y;
+	volatile unsigned short			source_yrgb_width;
+	volatile unsigned short			source_uv_width;
+	volatile unsigned short			source_yrgb_width_swords;
+	volatile unsigned short			source_uv_width_swords;
+	volatile unsigned short			source_yrgb_height;
+	volatile unsigned short			source_uv_height;
+	volatile unsigned int			yrgb_scale;
+	volatile unsigned int			uv_scale;
+	volatile unsigned int			col_ctl_brt_con;
+	volatile unsigned int			col_ctl_sat_hue;
+	volatile unsigned int			dest_ckey_val;
+	volatile unsigned int			dest_ckey_mask;
+	volatile unsigned int			source_ckey_high;
+	volatile unsigned int			source_ckey_low;
+	volatile unsigned int			source_ckey_mask;
+	volatile unsigned int			config;
+	volatile unsigned int			command;
+	volatile unsigned int			reserved1;
+	volatile unsigned int			buffer0_yrgb_start;
+	volatile unsigned int			buffer1_yrgb_start;
+	volatile unsigned int			buffer0_u_start;
+	volatile unsigned int			buffer0_v_start;
+	volatile unsigned int			buffer1_u_start;
+	volatile unsigned int			buffer1_v_start;
+	volatile unsigned short			buffer0_yrgb_x_toff;
+	volatile unsigned short			buffer0_yrgb_y_toff;
+	volatile unsigned short			buffer1_yrgb_x_toff;
+	volatile unsigned short			buffer1_yrgb_y_toff;
+	volatile unsigned short			buffer0_u_x_toff;
+	volatile unsigned short			buffer0_u_y_toff;
+	volatile unsigned short			buffer0_v_x_toff;
+	volatile unsigned short			buffer0_v_y_toff;
+	volatile unsigned short			buffer1_u_x_toff;
+	volatile unsigned short			buffer1_u_y_toff;
+	volatile unsigned short			buffer1_v_x_toff;
+	volatile unsigned short			buffer1_v_y_toff;
+	volatile unsigned int			reserved11;
+	volatile unsigned int			vert_downscale;
+	volatile unsigned int			reserved12[86];
+	volatile unsigned short			y_vert_coeff_single[52]; /*offset 0x200 --> 3*17/2 + 1*/
+	volatile unsigned int			reserved13[38];
+	volatile unsigned short			y_horz_coeff_single[86]; /*offset 0x300 --> 5*17/2 + 1*/
+	volatile unsigned int			reserved14[85];
+	volatile unsigned short			uv_vert_coeff_single[52]; /*offset 0x500 --> 3*17/2 + 1*/
+	volatile unsigned int			reserved15[38];
+	volatile unsigned short			uv_horz_coeff_single[52]; /*offset 0x600 --> 3*17/2 + 1*/
+	volatile unsigned int			reserved16[38];
+
+} ovl_reg_image_tnc_t;
+
+/* Color Correction */
+#define OVL_YUV_COLOR_DEF_CONT_BRGHT    0x10c00fb
+#define OVL_YUV_COLOR_DEF_SATN_HUE      0x0000091
+#define OVL_RGB_COLOR_DEF_CONT_BRGHT    0x1000000
+#define OVL_RGB_COLOR_DEF_SATN_HUE      0x0000080
+
+
+/* Overlay Config */
+#define OVL_CONFIG_NO_LINE_BUFF     0xffffffff
+#define OVL_CONFIG_TWO_LINE_BUFF    0x00000000
+#define OVL_CONFIG_THREE_LINE_BUFF  0x00000001
+#define OVL_CONFIG_LINE_BUFF_MASK   0x00000001
+
+/* Overlay Command Definitions */
+/* 422 Swap */
+#define OVL_CMD_UV_SWAP				0x00004000
+#define OVL_CMD_Y_SWAP				0x00008000
+#define OVL_CMD_YUV_SWAP			0x0000C000
+
+/* Source Format */
+#define OVL_CMD_RGB_8888			0x00000400
+#define OVL_CMD_RGB_565				0x00000C00
+#define OVL_CMD_RGB_555				0x00000800
+#define OVL_CMD_YUV_NV12Alt			0x00001C00 /*planar NV12, Alternate?*/
+#define OVL_CMD_YUV_NV12			0x00002C00 /*planar NV12*/
+#define OVL_CMD_YUV_422				0x00002000 /*packed YUV422*/
+#define OVL_CMD_YUV_411				0x00002400 /*packed YUV411*/
+#define OVL_CMD_YUV_420P			0x00003000 /*planar YUV420*/
+#define OVL_CMD_YUV_422P			0x00003400 /*planar YUV422*/
+#define OVL_CMD_YUV_410P			0x00003800 /*planar YUV410*/
+#define OVL_CMD_YUV_444P			0x00003C00 /*planar YUV444*/
+#define OVL_CMD_SRC_FMT_MASK		0x00003C00 /*mask for above*/
+
+/* Flip Type */
+#define OVL_CMD_FRAME_MODE          0x00000000
+#define OVL_CMD_FIELD_MODE			0x00000020
+
+/* Field Sync Flip Enable */
+#define OVL_CMD_FIELD_SYNC_FLIP			0x00000080
+
+/* Buffer and Field */
+#define OVL_CMD_ACT_BUF0			0x00000000
+#define OVL_CMD_ACT_BUF1			0x00000004
+#define OVL_CMD_ACT_FLD0			0x00000000
+#define OVL_CMD_ACT_FLD1			0x00000002
+
+/* Initial phase register */
+#define Y_VPP_FLD0_PLUS1			0x100000
+#define Y_VPP_FLD0_PLUS2			0x200000
+#define Y_VPP_FLD0_MINUS1			0xF00000
+#define Y_VPP_FLD1_PLUS1			0x010000
+#define Y_VPP_FLD1_PLUS2			0x020000
+#define Y_VPP_FLD1_MINUS1			0x0F0000
+#define Y_HPP_PLUS1					0x001000
+#define Y_HPP_PLUS2					0x002000
+#define Y_HPP_MINUS1				0x00F000
+#define UV_VPP_FLD0_PLUS1			0x000100
+#define UV_VPP_FLD0_PLUS2			0x000200
+#define UV_VPP_FLD0_MINUS1			0x000F00
+#define UV_VPP_FLD1_PLUS1			0x000010
+#define UV_VPP_FLD1_PLUS2			0x000020
+#define UV_VPP_FLD1_MINUS1			0x0000F0
+#define UV_HPP_PLUS1				0x000001
+#define UV_HPP_PLUS2				0x000002
+#define UV_HPP_MINUS1				0x00000F
+
+#define OVL_REG_ADDR_GAMMA5						0x30010
+#define OVL_REG_ADDR_GAMMA4						0x30014
+#define OVL_REG_ADDR_GAMMA3						0x30018
+#define OVL_REG_ADDR_GAMMA2						0x3001C
+#define OVL_REG_ADDR_GAMMA1						0x30020
+#define OVL_REG_ADDR_GAMMA0						0x30024
+#define OVL_TOTAL_GAMMA_REG						6
+
+/* following value are needed because hardware seems to display yuv slightly dimmer
+   than RGB when color data is calculated out to be equal */
+#define MID_CONTRAST_YUV					0x43//4a
+#define MID_SATURATION_YUV					0x91//92
+#define MID_BRIGHTNESS_YUV					-5
+
+#endif /* OVL_REGISTER_IMAGE_H_ */
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc.c b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc.c
new file mode 100644
index 0000000..116489c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc.c
@@ -0,0 +1,2128 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_tnc.c
+ * $Revision: 1.30 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains function that actually programs the overlay
+ *  register back buffer with the bits to properly configure
+ *  the overlay
+ *  Also includes functions to execute the overlay flip instruction,
+ *  and query the overlay flip status.
+ *  Also contains some hardware capabilities querrying functions
+ *  for upper overlay layer to get this chips overlay capabilities
+ *-----------------------------------------------------------------------------
+ */
+
+#define MODULE_NAME hal.overlay
+
+#include <math_fix.h>
+#include <tnc/cmd.h>
+#include "ovl_tnc_cache.h"
+#include "../cmn/ovl_dispatch.h"
+#include "../cmn/ovl_virt.h"
+#include "../cmn/ovl_coeff.h"
+#include "ovl2_tnc.h"
+
+
+/*-----------------------------------------------------------------------------
+ * Common dispatch functions
+ *---------------------------------------------------------------------------*/
+static int alter_ovl_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags);
+static int query_ovl_tnc(igd_display_h display_h,
+	unsigned int flags);
+static int query_max_size_ovl_tnc(igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height);
+
+
+ovl_dispatch_t ovl_dispatch_tnc[] = {
+	/* Dispatch for the hardware overlay */
+	{
+		NULL, /* blend_surf_needed_tnc, */
+		alter_ovl_tnc,
+		query_ovl_tnc,
+		query_max_size_ovl_tnc,
+	},
+	/* Dispatch for the software overlay */
+	{
+		NULL, /* blend2_surf_needed_tnc, */
+		alter_ovl2_tnc,
+		query_ovl2_tnc,
+		query_max_size_ovl2_tnc,
+	},
+};
+
+
+
+typedef struct _ovl_chipset_tnc {
+	unsigned int	num_linebuf;
+	unsigned int	pixel_format;
+	unsigned int	max_width;
+	unsigned int	max_height;
+} ovl_chipset_tnc_t;
+
+static ovl_chipset_tnc_t ovl_chipset_tnc[] = {
+	{OVL_CONFIG_THREE_LINE_BUFF,
+		(PF_DEPTH_16 | PF_TYPE_YUV_PACKED), 1280, 1080},
+	{OVL_CONFIG_THREE_LINE_BUFF,
+		(PF_DEPTH_8 | PF_TYPE_YUV_PLANAR), 1024, 1080},
+	{OVL_CONFIG_TWO_LINE_BUFF,
+		(PF_DEPTH_16 | PF_TYPE_YUV_PACKED), 1920, 1088},
+	{OVL_CONFIG_TWO_LINE_BUFF,
+		(PF_DEPTH_8 | PF_TYPE_YUV_PLANAR), 1920, 1088},
+	{OVL_CONFIG_NO_LINE_BUFF, 0, 0, 0}
+};
+
+#ifdef DEBUG_BUILD_TYPE
+static void ovl_dump_regs_tnc(
+	ovl_reg_image_tnc_t *ovl_regs_tnc)
+{
+	{
+		int i;
+		unsigned long *ovl_buf = (unsigned long *)ovl_regs_tnc;
+		for (i=0; i<0x10; i++) {
+			EMGD_DEBUG_S("0x%2x: 0x%8lx 0x%8lx 0x%8lx 0x%8lx\n",
+				i*0x10,
+				*(ovl_buf+(i*4)), *(ovl_buf+(i*4+1)),
+				*(ovl_buf+(i*4+2)), *(ovl_buf+(i*4+3)));
+		}
+	}
+
+
+	EMGD_DEBUG_S("************************************************\n");
+	EMGD_DEBUG_S("OVERLAY REGISTER LISTING\n");
+
+	EMGD_DEBUG_S("RGB LOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_yrgb_loff);
+	EMGD_DEBUG_S("RGB LOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_yrgb_loff);
+	EMGD_DEBUG_S("U LOFF 0         =     0x%x  \n",
+		ovl_regs_tnc->buffer0_u_loff);
+	EMGD_DEBUG_S("V LOFF 0         =     0x%x  \n",
+		ovl_regs_tnc->buffer0_v_loff);
+	EMGD_DEBUG_S("U LOFF 1         =     0x%x  \n",
+		ovl_regs_tnc->buffer1_u_loff);
+	EMGD_DEBUG_S("V LOFF 1         =     0x%x  \n",
+		ovl_regs_tnc->buffer1_v_loff);
+
+	EMGD_DEBUG_S("RGB STRIDE       =     0x%x  \n",
+		ovl_regs_tnc->yrgb_stride);
+	EMGD_DEBUG_S("UV STRIDE        =     0x%x  \n",
+		ovl_regs_tnc->uv_stride);
+	EMGD_DEBUG_S("DST POS X        =     %d  \n",
+		ovl_regs_tnc->dest_pos_x_left);
+	EMGD_DEBUG_S("DST POS Y        =     %d  \n",
+		ovl_regs_tnc->dest_pos_y_top);
+	EMGD_DEBUG_S("DST WIDTH        =     %d  \n",
+		ovl_regs_tnc->dest_width_x);
+	EMGD_DEBUG_S("DST HEIGHT       =     %d  \n",
+		ovl_regs_tnc->dest_height_y);
+	EMGD_DEBUG_S("SRC WIDTH        =     %d  \n",
+		ovl_regs_tnc->source_yrgb_width);
+	EMGD_DEBUG_S("SRC SWWIDTH      =     0x%x  \n",
+		ovl_regs_tnc->source_yrgb_width_swords);
+	EMGD_DEBUG_S("SRC HEIGHT       =     %d  \n",
+		ovl_regs_tnc->source_yrgb_height);
+	EMGD_DEBUG_S("UV SRC WIDTH     =     %d  \n",
+		ovl_regs_tnc->source_uv_width);
+	EMGD_DEBUG_S("UV SRC SWWIDTH   =     %d  \n",
+		ovl_regs_tnc->source_uv_width_swords);
+	EMGD_DEBUG_S("UV SRC HEIGHT    =     %d  \n",
+		ovl_regs_tnc->source_uv_height);
+	EMGD_DEBUG_S("RGB SCALE        =     0x%x  \n",
+		ovl_regs_tnc->yrgb_scale);
+	EMGD_DEBUG_S("UV SCALE         =     0x%x  \n",
+		ovl_regs_tnc->uv_scale);
+	EMGD_DEBUG_S("COL CTL BRT CON  =     0x%x  \n",
+		ovl_regs_tnc->col_ctl_brt_con);
+	EMGD_DEBUG_S("COL CTL SAT HUE  =     0x%x  \n",
+		ovl_regs_tnc->col_ctl_sat_hue);
+	EMGD_DEBUG_S("DST COLOR KEY    =     0x%x  \n",
+		ovl_regs_tnc->dest_ckey_val);
+	EMGD_DEBUG_S("DST COLOR KEY MASK =   0x%x  \n",
+		ovl_regs_tnc->dest_ckey_mask);
+	EMGD_DEBUG_S("SRC COLOR KEY HI =     0x%x  \n",
+		ovl_regs_tnc->source_ckey_high);
+	EMGD_DEBUG_S("SRC COLOR KEY LO =     0x%x  \n",
+		ovl_regs_tnc->source_ckey_low);
+	EMGD_DEBUG_S("SRC COLOR KEY MASK =   0x%x  \n",
+		ovl_regs_tnc->source_ckey_mask);
+	EMGD_DEBUG_S("OVL CONFIG       =     0x%x  \n", ovl_regs_tnc->config);
+	EMGD_DEBUG_S("OVL CMD          =     0x%x  \n", ovl_regs_tnc->command);
+	EMGD_DEBUG_S("Y START 0        =     0x%x  \n",
+		ovl_regs_tnc->buffer0_yrgb_start);
+	EMGD_DEBUG_S("Y START 1        =     0x%x  \n",
+		ovl_regs_tnc->buffer1_yrgb_start);
+	EMGD_DEBUG_S("U START 0        =     0x%x  \n",
+		ovl_regs_tnc->buffer0_u_start);
+	EMGD_DEBUG_S("V START 0        =     0x%x  \n",
+		ovl_regs_tnc->buffer0_v_start);
+	EMGD_DEBUG_S("U START 1        =     0x%x  \n",
+		ovl_regs_tnc->buffer1_u_start);
+	EMGD_DEBUG_S("V START 1        =     0x%x  \n",
+		ovl_regs_tnc->buffer1_v_start);
+	EMGD_DEBUG_S("Y X TOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_yrgb_x_toff);
+	EMGD_DEBUG_S("Y Y TOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_yrgb_y_toff);
+	EMGD_DEBUG_S("Y X TOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_yrgb_x_toff);
+	EMGD_DEBUG_S("Y Y TOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_yrgb_y_toff);
+	EMGD_DEBUG_S("U X TOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_u_x_toff);
+	EMGD_DEBUG_S("U Y TOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_u_y_toff);
+	EMGD_DEBUG_S("V X TOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_v_x_toff);
+	EMGD_DEBUG_S("V Y TOFF 0       =     0x%x  \n",
+		ovl_regs_tnc->buffer0_v_y_toff);
+	EMGD_DEBUG_S("U X TOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_u_x_toff);
+	EMGD_DEBUG_S("U Y TOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_u_y_toff);
+	EMGD_DEBUG_S("V X TOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_v_x_toff);
+	EMGD_DEBUG_S("V Y TOFF 1       =     0x%x  \n",
+		ovl_regs_tnc->buffer1_v_y_toff);
+	EMGD_DEBUG_S("FAST_V_DSCALE    =     0x%x  \n",
+		ovl_regs_tnc->vert_downscale);
+	EMGD_DEBUG_S("************************************************\n");
+}
+#endif
+
+/*----------------------------------------------------------------------
+ * Function: ovl_check_pf_tnc()
+ * Parameters: unsigned int requested_pixel_format -
+ *             according to definitions in igd_mode.h
+ *
+ * Description:
+ *
+ * Returns:
+ *   TRUE on Success
+ *   FALSE on The first pixel format that is supported
+ *----------------------------------------------------------------------*/
+static unsigned int ovl_check_pf_tnc(
+	igd_display_context_t *display,
+	unsigned int requested_pixel_format)
+{
+	unsigned long *overlay_pfs;
+	int temp_loop = 0;
+
+	display->context->dispatch.get_pixelformats(
+		(igd_display_h)display, NULL, NULL, &overlay_pfs, NULL, NULL);
+
+	while(overlay_pfs[temp_loop]) {
+		if(overlay_pfs[temp_loop] == requested_pixel_format) {
+			return TRUE;
+		}
+		++temp_loop;
+	}
+
+	return FALSE;
+}
+
+static unsigned int get_uv_shift_x (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV422_PACKED_YUY2:
+	case IGD_PF_YUV422_PACKED_UYVY:
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+static unsigned int get_uv_shift_y (unsigned long pf)
+{
+
+	switch(pf) {
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		return 1;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		return 2;
+		break;
+	default:
+		return 0;
+	}
+
+}
+
+static unsigned int ovl_check_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	igd_timing_info_t *timing;
+	ovl_chipset_tnc_t *ovl_chip;
+	unsigned int min_w, min_h;
+
+	EMGD_TRACE_ENTER;
+
+	if (!display){
+		EMGD_ERROR_EXIT("display is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (!PIPE(display)){
+		EMGD_ERROR_EXIT("PIPE(display) is null");
+		return -IGD_ERROR_INVAL;
+	}
+
+	timing = PIPE(display)->timing;
+
+	if(!timing) {
+		EMGD_ERROR_EXIT("timing is null\n");
+		return -IGD_ERROR_INVAL;
+	}
+
+	/*************************************************************************
+	 * Ensure the framebuffer dotclock does not exceed the board SKU
+	 * max dotclock
+	 *************************************************************************/
+	/* DCT-PC99TA crashes with dotclock > 300MHz */
+	/* FIXME: This is using the dclk from Napa */
+	if(timing->dclk >= 340000){
+		EMGD_ERROR_EXIT("Cannot support dotclock > 340MHz for this SKU");
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* The following parameters are only valid if the overlay is on, so
+	 * return success if the overlay is being turned off. */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Ensure the overlay surface is ok and can be properly displayed.
+	 * This ensures the following is valid:
+	 *    - Ensure x1, x2, y1, y2 are pixel aligned
+	 *    - 2 pixels or greater in width and height
+	 *    - Pixel format is supported by the overlay
+	 *    - Pitch is <= 8KB
+	 *    - Based on the pixel format, the width is supported
+	 *************************************************************************/
+	if (!src_surf){
+		EMGD_ERROR_EXIT("src_surf is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (!src_rect){
+		EMGD_ERROR_EXIT("src_rect is null");
+		return -IGD_ERROR_INVAL;
+	}
+	/* Get the minimum size of 1 pixel in width and height for y, u, and v.
+	 */
+	min_w = 1 << get_uv_shift_x(src_surf->pixel_format);
+	min_h = 1 << get_uv_shift_y(src_surf->pixel_format);
+
+	if (((src_rect->x2 - src_rect->x1) < min_w*2) ||
+		((src_rect->y2 - src_rect->y1) < min_h*2)) {
+		EMGD_ERROR_EXIT(
+			"Overlay source width or height is < 2 pixels (%dx%d)\n",
+			src_rect->x2 - src_rect->x1, src_rect->y2 - src_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if (FALSE == ovl_check_pf_tnc(display, src_surf->pixel_format)) {
+		EMGD_ERROR_EXIT("Overlay source pixel format unsupported (pf:0x%lx)",
+			src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	if (src_surf->pitch > 8192) {
+		EMGD_ERROR_EXIT("Overlay source pitch (%d) > 8KB",
+			src_surf->pitch);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	ovl_chip = ovl_chipset_tnc;
+	while(ovl_chip->num_linebuf != OVL_CONFIG_NO_LINE_BUFF){
+		if(((src_surf->pixel_format & IGD_PF_MASK) ==
+				ovl_chip->pixel_format) &&
+			(src_surf->width <= ovl_chip->max_width)) {
+			break;
+		}
+		ovl_chip++;
+	}
+	if (ovl_chip->num_linebuf == OVL_CONFIG_NO_LINE_BUFF) {
+		EMGD_ERROR_EXIT("Overlay source width (%d) > max supported",
+			src_surf->width);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/*************************************************************************
+	 * Ensure the location on the framebuffer is ok and can be properly
+	 * displayed
+	 * This ensures the following is valid:
+	 *    - Greater than 1 pixel width and height
+	 *    - Will be displayed on screen (not panned off)
+	 *************************************************************************/
+	if (!dest_rect){
+		EMGD_ERROR_EXIT("dest_rect is null");
+		return -IGD_ERROR_INVAL;
+	}
+	if (((dest_rect->x2 - dest_rect->x1) <= 1) ||
+		((dest_rect->y2 - dest_rect->y1) <= 1)) {
+		EMGD_ERROR_EXIT(
+			"Overlay dest width or height is single pixel (%dx%d)\n",
+			dest_rect->x2 - dest_rect->x1, dest_rect->y2 - dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	if ((dest_rect->x1 >= timing->width) ||
+		(dest_rect->y1 >= timing->height)) {
+		EMGD_ERROR_EXIT(
+			"Overlay dest is panned off the screen (%d,%d)\n",
+			dest_rect->x1, dest_rect->y1);
+		return -IGD_ERROR_INVAL;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+static unsigned int ovl_update_src_tnc(igd_display_context_t *display,
+	ovl_reg_image_tnc_t *ovl_regs_tnc,
+	igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	ovl_chipset_tnc_t *ovl_chip;
+	unsigned int      src_Bpp;
+	unsigned int      src_uv_shift_x, src_uv_shift_y;
+	unsigned short    src_w;
+	unsigned short    src_h;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format)/8;
+
+	src_uv_shift_x = get_uv_shift_x(src_surf->pixel_format);
+	src_uv_shift_y = get_uv_shift_y(src_surf->pixel_format);
+
+	src_w = src_rect->x2 - src_rect->x1;
+	src_h = src_rect->y2 - src_rect->y1;
+
+	/* Surface pitch */
+	ovl_regs_tnc->yrgb_stride      = (unsigned short)src_surf->pitch;
+	ovl_regs_tnc->uv_stride        = (unsigned short)src_surf->u_pitch;
+
+
+	/* src width */
+	ovl_regs_tnc->source_yrgb_width      = src_w;
+	ovl_regs_tnc->source_uv_width        = src_w >> src_uv_shift_x;
+
+	/* src width swords - This equation follows the B-Spec */
+	/* Equation in B-spec is cancelling out fixed point round-up when -1.
+	   Explain below:
+	   [[(offset + width + 63)/128 - offset/128] * 2 ] - 1
+	   [(width/128 + 63/128) * 2] - 1
+	   (width/128)*2 + (0.249*2) - 1
+	   width/64
+
+	   Modify the formula witihout -1 to preserve fix point round-up.
+           Equation = width/64 + 0.4999
+	   Also modified 63/128(0x3F) to 31/128(0x1F) to have 0.5 when *2.
+        */
+
+	ovl_regs_tnc->source_yrgb_width_swords =
+		(unsigned short)(((((src_surf->offset +
+							(ovl_regs_tnc->source_yrgb_width * src_Bpp) +
+							0x1F) >> 6) -
+					(src_surf->offset >> 6)) << 1) << 2);
+	ovl_regs_tnc->source_uv_width_swords   =
+		(unsigned short)(((((src_surf->u_offset +
+							(ovl_regs_tnc->source_uv_width * src_Bpp) +
+							0x1F) >> 6) -
+					(src_surf->u_offset >> 6)) << 1) << 2 );
+
+	/* src height */
+	ovl_regs_tnc->source_yrgb_height     = src_h;
+	ovl_regs_tnc->source_uv_height       = src_h >> src_uv_shift_y;
+
+	/* src pixel format */
+	switch(src_surf->pixel_format){
+	case IGD_PF_YUV422_PACKED_YUY2:
+		ovl_regs_tnc->command |= OVL_CMD_YUV_422;
+		break;
+	case IGD_PF_YUV422_PACKED_UYVY:
+		ovl_regs_tnc->command |= OVL_CMD_YUV_422 | OVL_CMD_Y_SWAP;
+		break;
+	case IGD_PF_YUV420_PLANAR_I420: /* same as IYUV */
+		ovl_regs_tnc->command |= OVL_CMD_YUV_420P;
+		break;
+	case IGD_PF_YUV420_PLANAR_YV12:
+		ovl_regs_tnc->command |= OVL_CMD_YUV_420P | OVL_CMD_UV_SWAP;
+		break;
+	case IGD_PF_YUV420_PLANAR_NV12:
+		ovl_regs_tnc->command |= OVL_CMD_YUV_NV12;
+		break;
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		ovl_regs_tnc->command |= OVL_CMD_YUV_410P;
+		break;
+	case IGD_PF_ARGB32_8888:
+	case IGD_PF_xRGB32_8888:
+		ovl_regs_tnc->command |= OVL_CMD_RGB_8888;
+		break;
+	case IGD_PF_RGB16_565:
+		ovl_regs_tnc->command |= OVL_CMD_RGB_565;
+		break;
+	case IGD_PF_xRGB16_555:
+	case IGD_PF_ARGB16_1555:
+		ovl_regs_tnc->command |= OVL_CMD_RGB_555;
+		break;
+	default:
+		EMGD_ERROR_EXIT("Invalid pixel format: 0x%lx", src_surf->pixel_format);
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Turn off YUV to RGB conversion if the src is RGB */
+	if (!(src_surf->pixel_format & PF_TYPE_YUV)) {
+		ovl_regs_tnc->config |= (1<<4);
+	}
+
+	ovl_chip = ovl_chipset_tnc;
+	ovl_regs_tnc->config &= ~OVL_CONFIG_LINE_BUFF_MASK;
+	while(ovl_chip->num_linebuf != OVL_CONFIG_NO_LINE_BUFF){
+		if(((src_surf->pixel_format & IGD_PF_MASK) ==
+				ovl_chip->pixel_format) &&
+			(src_w <= ovl_chip->max_width)) {
+			ovl_regs_tnc->config |= ovl_chip->num_linebuf;
+			break;
+		}
+		ovl_chip++;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+static unsigned int ovl_update_src_ptr_tnc(igd_display_context_t *display,
+	ovl_reg_image_tnc_t *ovl_regs_tnc,
+	igd_surface_t *src_surf,
+	igd_rect_t    *src_rect)
+{
+	unsigned int      src_Bpp;
+	unsigned int      src_uv_shift_x, src_uv_shift_y;
+	unsigned short    src_w;
+	unsigned short    src_h;
+
+	EMGD_TRACE_ENTER;
+
+	/* This is in Bytes per pixel */
+	src_Bpp = IGD_PF_BPP(src_surf->pixel_format)/8;
+
+	src_uv_shift_x = get_uv_shift_x(src_surf->pixel_format);
+	src_uv_shift_y = get_uv_shift_y(src_surf->pixel_format);
+
+	src_w = src_rect->x2 - src_rect->x1;
+	src_h = src_rect->y2 - src_rect->y1;
+
+	/* Surface offset */
+	ovl_regs_tnc->buffer0_yrgb_start =
+		ovl_regs_tnc->buffer1_yrgb_start =
+		src_surf->offset;
+	ovl_regs_tnc->buffer0_u_start =
+		ovl_regs_tnc->buffer1_u_start =
+		src_surf->u_offset;
+	ovl_regs_tnc->buffer0_v_start =
+		ovl_regs_tnc->buffer1_v_start =
+		src_surf->v_offset;
+
+	/* Linear surface information */
+	ovl_regs_tnc->buffer0_yrgb_loff     =
+		ovl_regs_tnc->buffer1_yrgb_loff =
+		(src_rect->y1 * src_surf->pitch) + (src_rect->x1 * src_Bpp);
+
+	/*
+	 * The NV12 format has the UV pixels interleaved so the total
+	 * width of the UV portion of the surface is the same as the
+	 * Y width. Thus, don't do any shifting of the UV plane in the
+	 * X direction.
+	 */
+	if (src_surf->pixel_format == IGD_PF_YUV420_PLANAR_NV12) {
+		ovl_regs_tnc->buffer0_u_loff     =
+			ovl_regs_tnc->buffer1_u_loff =
+			((src_rect->y1>>src_uv_shift_y) *
+			 src_surf->u_pitch) + src_rect->x1;
+
+		ovl_regs_tnc->buffer0_v_loff     =
+			ovl_regs_tnc->buffer1_v_loff =
+			((src_rect->y1>>src_uv_shift_y) *
+			 src_surf->v_pitch) + src_rect->x1;
+	} else {
+		ovl_regs_tnc->buffer0_u_loff     =
+			ovl_regs_tnc->buffer1_u_loff =
+			((src_rect->y1>>src_uv_shift_y) *
+			 src_surf->u_pitch) +
+			(src_rect->x1>>src_uv_shift_x);
+
+		ovl_regs_tnc->buffer0_v_loff     =
+			ovl_regs_tnc->buffer1_v_loff =
+			((src_rect->y1>>src_uv_shift_y) * src_surf->v_pitch) +
+			(src_rect->x1>>src_uv_shift_x);
+	}
+
+
+	/* The B-Spec states that these values are ignored with a linear
+	 * surface.  However on the 965G, these values are not ignored,
+	 * so zero them. */
+	ovl_regs_tnc->buffer0_yrgb_x_toff =
+		ovl_regs_tnc->buffer1_yrgb_x_toff =
+		ovl_regs_tnc->buffer0_yrgb_y_toff =
+		ovl_regs_tnc->buffer1_yrgb_y_toff =
+		ovl_regs_tnc->buffer0_u_x_toff =
+		ovl_regs_tnc->buffer0_v_x_toff =
+		ovl_regs_tnc->buffer1_u_x_toff =
+		ovl_regs_tnc->buffer1_v_x_toff =
+		ovl_regs_tnc->buffer0_u_y_toff =
+		ovl_regs_tnc->buffer0_v_y_toff =
+		ovl_regs_tnc->buffer1_u_y_toff =
+		ovl_regs_tnc->buffer1_v_y_toff =
+		0;
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+static unsigned int ovl_update_phase_tnc(
+	ovl_reg_image_tnc_t *ovl_regs_tnc,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect)
+{
+
+	EMGD_TRACE_ENTER;
+
+	/*
+	 * Set the Vertical/Horizontal Phase Registers.  Both Field0
+	 * and Field1 are set, although Field1 is only used when
+	 * interleaved.
+	 */
+	switch (src_surf->pixel_format) {
+	case IGD_PF_YUV422_PACKED_YUY2:
+	case IGD_PF_YUV422_PACKED_UYVY:
+		/* YUV 422 */
+		ovl_regs_tnc->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_tnc->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			ovl_regs_tnc->yrgb_vert_phase_field0 = 0;
+			ovl_regs_tnc->yrgb_vert_phase_field1 = 0;
+			ovl_regs_tnc->uv_vert_phase_field0 = 0;
+			ovl_regs_tnc->uv_vert_phase_field1 = 0;
+		} else {
+			ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000; /*.5*/
+			ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000; /*.5*/
+			ovl_regs_tnc->uv_vert_phase_field0   = 0x8000; /*.5*/
+			ovl_regs_tnc->uv_vert_phase_field1   = 0x8000; /*.5*/
+		}
+
+		/* Horizontal Phase */
+		if (!(src_rect->x1 & 1)) {
+			ovl_regs_tnc->yrgb_hphase = 0;
+			ovl_regs_tnc->uv_hphase = 0;
+		} else {
+			ovl_regs_tnc->init_phase_shift |= Y_HPP_PLUS1;
+			ovl_regs_tnc->yrgb_hphase = 0; /*1*/
+			ovl_regs_tnc->uv_hphase = 0x8000; /*.5*/
+		}
+		break;
+
+	case IGD_PF_YUV420_PLANAR_I420:
+	case IGD_PF_YUV420_PLANAR_YV12:
+	case IGD_PF_YUV420_PLANAR_NV12:
+		/* YUV 420 */
+		ovl_regs_tnc->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_tnc->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			if (!(src_rect->y1 & 1)) {
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0; /*0*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field0 = 0xc000; /*-.25*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0x4000; /*.25*/
+			} else {
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0;/*0*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x4000; /*.25*/
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field1 = 0xc000; /*-.25*/
+			}
+		} else {
+			if (!(src_rect->y1 & 1)) {
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000; /*.5*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000;/*1.5*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x4000; /*.25*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0xc000; /*.75*/
+			} else {
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000; /*.5*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0xc000; /*.75*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0x4000; /*.25*/
+			}
+		}
+
+		/* Horizontal Phase */
+		if (!(src_rect->x1 & 1)) {
+			ovl_regs_tnc->yrgb_hphase = 0;
+			ovl_regs_tnc->uv_hphase = 0;
+		} else {
+			ovl_regs_tnc->init_phase_shift |= Y_HPP_PLUS1;
+			ovl_regs_tnc->yrgb_hphase = 0; /*1*/
+			ovl_regs_tnc->uv_hphase = 0x8000; /*.5*/
+		}
+		break;
+
+	case IGD_PF_YUV410_PLANAR_YVU9:
+		/* YUV 410 */
+		ovl_regs_tnc->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_tnc->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			switch (src_rect->y1 & 3) {
+			default:
+			case 0:
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0; /*0*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field0 = 0xa000; /*-.375*/
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field1 = 0xe000; /*-.125*/
+				break;
+
+			case 1:
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field0 = 0xe000; /*-.125*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0x2000; /*.125*/
+				break;
+
+			case 2:
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0;/*1*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x2000; /*.125*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0x6000; /*.375*/
+				break;
+
+			case 3:
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0; /*1*/
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0;/*0*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x6000; /*.375*/
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field1 = 0xa000; /*-.375*/
+				break;
+			}
+		} else {
+			switch (src_rect->y1 & 3) {
+			default:
+			case 0:
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000; /*.5*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000;/*1.5*/
+
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD0_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field0 = 0xc000; /*-.25*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0; /*0*/
+				break;
+
+			case 1:
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000; /*1.5*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x0; /*0*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0x4000; /*.25*/
+				break;
+
+			case 2:
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD1_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000; /*1.5*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x4000; /*.25*/
+				ovl_regs_tnc->uv_vert_phase_field1 = 0x8000; /*.5*/
+				break;
+
+			case 3:
+				ovl_regs_tnc->init_phase_shift |= Y_VPP_FLD0_PLUS1;
+				ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000;/*1.5*/
+				ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000; /*.5*/
+
+				ovl_regs_tnc->uv_vert_phase_field0 = 0x8000; /*.5*/
+				ovl_regs_tnc->init_phase_shift |= UV_VPP_FLD1_MINUS1;
+				ovl_regs_tnc->uv_vert_phase_field1 = 0xc000; /*-.25*/
+				break;
+			}
+		}
+
+		/* Horizontal Phase */
+		switch (src_rect->x1 & 3) {
+		default:
+		case 0:
+			ovl_regs_tnc->yrgb_hphase = 0;
+			ovl_regs_tnc->init_phase_shift |= UV_HPP_MINUS1;
+			ovl_regs_tnc->uv_hphase = 0xa000; /*-.375*/
+			break;
+
+		case 1:
+			ovl_regs_tnc->init_phase_shift |= Y_HPP_PLUS1;
+			ovl_regs_tnc->yrgb_hphase = 0; /*1*/
+			ovl_regs_tnc->init_phase_shift |= UV_HPP_MINUS1;
+			ovl_regs_tnc->uv_hphase = 0xe000; /*-.125*/
+			break;
+
+		case 2:
+			ovl_regs_tnc->init_phase_shift |= Y_HPP_PLUS2;
+			ovl_regs_tnc->yrgb_hphase = 0; /*2*/
+			ovl_regs_tnc->uv_hphase = 0x2000; /*.125*/
+			break;
+
+		case 3:
+			ovl_regs_tnc->init_phase_shift |= Y_HPP_PLUS2;
+			ovl_regs_tnc->yrgb_hphase = 0xffff; /*3*/
+			ovl_regs_tnc->uv_hphase = 0x6000; /*.375*/
+			break;
+		}
+		break;
+
+	default:
+		/* RGB format */
+		ovl_regs_tnc->init_phase_shift = 0;
+
+		/* Vertical Phase */
+		if (ovl_regs_tnc->config & OVL_CONFIG_THREE_LINE_BUFF) {
+			ovl_regs_tnc->yrgb_vert_phase_field0 = 0;
+			ovl_regs_tnc->yrgb_vert_phase_field1 = 0;
+			ovl_regs_tnc->uv_vert_phase_field0 = 0;
+			ovl_regs_tnc->uv_vert_phase_field1 = 0;
+		} else {
+			ovl_regs_tnc->yrgb_vert_phase_field0 = 0x8000;
+			ovl_regs_tnc->yrgb_vert_phase_field1 = 0x8000;
+			ovl_regs_tnc->uv_vert_phase_field0   = 0x8000;
+			ovl_regs_tnc->uv_vert_phase_field1   = 0x8000;
+		}
+
+		/* Horizontal Phase */
+		ovl_regs_tnc->yrgb_hphase = 0;
+		ovl_regs_tnc->uv_hphase = 0;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_scale_tnc()
+ *
+ * Description: Will update only the scaling registers for the Atom E6xx core
+ *
+ * Returns:
+ *   N/A
+ *----------------------------------------------------------------------*/
+
+static unsigned int ovl_update_scale_tnc(
+	ovl_reg_image_tnc_t *ovl_regs_tnc,
+	igd_surface_t *src_surf,
+	igd_rect_t   *src_rect,
+	igd_rect_t   *dest_rect,
+	unsigned int flags)
+{
+	unsigned int uv_shift;
+	unsigned int xscale, xscale_int, xscale_fract;
+	unsigned int yscale, yscale_int, yscale_fract;
+	unsigned int xscale_int_uv, xscale_fract_uv;
+	unsigned int yscale_int_uv, yscale_fract_uv;
+
+	EMGD_TRACE_ENTER;
+
+	xscale = ((src_rect->x2 - src_rect->x1)<<12) /
+		(dest_rect->x2 - dest_rect->x1);
+	yscale = ((src_rect->y2 - src_rect->y1)<<12) /
+		(dest_rect->y2 - dest_rect->y1);
+
+	/* In interleaved mode, the y scale is /2 */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		yscale >>= 1;
+	}
+
+	xscale_int = (xscale & 0x3000) >> 12;
+	xscale_fract = xscale & 0xfff;
+	yscale_int = (yscale & 0x7ff000) >> 12;
+	yscale_fract = yscale & 0xfff;
+
+	/* The uv scale is used for both YUV Planar as well as YUV Packed */
+	uv_shift = get_uv_shift_x(src_surf->pixel_format);
+	xscale_int_uv = ((xscale>>uv_shift) & 0x3000) >> 12;
+	xscale_fract_uv = (xscale>>uv_shift) & 0xfff;
+
+	uv_shift = get_uv_shift_y(src_surf->pixel_format);
+	yscale_int_uv = ((yscale>>uv_shift) & 0x7ff000) >> 12;
+	yscale_fract_uv = (yscale>>uv_shift) & 0xfff;
+
+	ovl_regs_tnc->yrgb_scale =
+		(yscale_fract  << 20) |   /* Vert  Scale Fraction */
+		(xscale_int    << 16) |   /* Horiz Scale Int */
+		(xscale_fract  << 3);     /* Horiz Scale Fraction */
+
+	ovl_regs_tnc->uv_scale =
+		(yscale_fract_uv << 20) | /* UV Vert  Scale Fraction */
+		(xscale_int_uv   << 16) | /* UV Horiz Scale Int */
+		(xscale_fract_uv << 3 );  /* UV Horiz Scale Fraction */
+	ovl_regs_tnc->vert_downscale =
+		(yscale_int    << 16) |   /* Vert Scale Factor */
+		yscale_int_uv;            /* UV Vert Scale Factor */
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_video_quality_tnc()
+ *
+ * Description:
+ *   This function updates the contrast, brightness, and saturation of
+ *   the overlay using the values specified in overlay_info.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   0 on Success
+ *----------------------------------------------------------------------*/
+
+static int ovl_update_video_quality_tnc(
+	ovl_reg_image_tnc_t *ovl_regs_tnc,
+	igd_surface_t       *src_surf,
+	igd_ovl_video_quality_info_t *video_quality)
+{
+	int                          calc_brightness_tmp = 0;
+	int                          calc_brightness     = 0;
+	unsigned int                 calc_contrast_tmp   = 0;
+	unsigned int                 calc_contrast       = 0;
+	unsigned int                 calc_saturation_tmp = 0;
+	unsigned int                 calc_saturation     = 0;
+
+	EMGD_TRACE_ENTER;
+
+	/* If the src_surf pixel format is RGB, then brightness, contrast,
+	 * and saturation should all be set to the exact default */
+	if (src_surf->pixel_format & PF_TYPE_RGB) {
+		if (video_quality->brightness != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set brightness to default");
+		}
+		if (video_quality->contrast != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set contrast to default");
+		}
+		if (video_quality->saturation != 0x8000) {
+			EMGD_DEBUG("RGB surfaces must set saturation to default");
+		}
+
+		ovl_regs_tnc->col_ctl_brt_con = OVL_RGB_COLOR_DEF_CONT_BRGHT;
+		ovl_regs_tnc->col_ctl_sat_hue = OVL_RGB_COLOR_DEF_SATN_HUE;
+
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/*************************************************************************
+	 * Brightness
+	 *************************************************************************/
+	if (0x8000 == video_quality->brightness) {
+		calc_brightness = MID_BRIGHTNESS_YUV;
+	} else if (video_quality->brightness < 0x8000) {
+		/*
+		 * we have here a brightness that is less than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = 0x8000 - video_quality->brightness;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = -128 - MID_BRIGHTNESS_YUV;
+		/*
+		 * more range if the midpoint is positive but less range
+		 * if midpoint is negative
+		 */
+
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness < -128) {
+			calc_brightness = -128;
+		}
+		if (calc_brightness > MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	} else {
+		/*
+		 * we have here a brightness that is more than the default
+		 * mid point
+		 */
+		calc_brightness_tmp = video_quality->brightness - 0x8000;
+		calc_brightness_tmp <<= 14;
+		calc_brightness_tmp /= 0x8000;
+		calc_brightness     = 127 - MID_BRIGHTNESS_YUV;
+		/*
+		 * less range if the midpoint is positive but more range
+		 * if midpoint is negative
+		 */
+		calc_brightness *= calc_brightness_tmp;
+		calc_brightness += BIT13;
+		calc_brightness >>= 14;
+
+		if (calc_brightness > 127) {
+			calc_brightness = 127;
+		}
+		if (calc_brightness < MID_BRIGHTNESS_YUV) {
+			calc_brightness = MID_BRIGHTNESS_YUV;
+		}
+	}
+
+	ovl_regs_tnc->col_ctl_brt_con =
+		(ovl_regs_tnc->col_ctl_brt_con & 0xFFFFFF00) |
+		(calc_brightness & 0xFF);
+
+	/*************************************************************************
+	 * Contrast
+	 *************************************************************************/
+	if (0x8000 == video_quality->contrast ){
+		calc_contrast = MID_CONTRAST_YUV;
+	} else if (video_quality->contrast < 0x8000) {
+		/* we have here a contrast that is less than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = MID_CONTRAST_YUV;
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		if (calc_contrast > 0x3F) {
+			calc_contrast = 0x3F;
+		}
+	} else {
+		/* we have here a contrast that is more than the
+		 * default mid point */
+		calc_contrast_tmp = video_quality->contrast - 0x8000;
+		calc_contrast_tmp <<= 12;
+		calc_contrast_tmp /= 0x8000;
+		calc_contrast     = (0x1FF - MID_CONTRAST_YUV);
+		calc_contrast     *= calc_contrast_tmp;
+		calc_contrast     += BIT11;
+		calc_contrast     >>= 12;
+		calc_contrast     += MID_CONTRAST_YUV;
+		if (calc_contrast > 0x1FF) {
+			calc_contrast = 0x1FF;
+		}
+	}
+
+	ovl_regs_tnc->col_ctl_brt_con =
+		(ovl_regs_tnc->col_ctl_brt_con & 0xF803FFFF) |
+		((calc_contrast & 0x1FF) << 18);
+
+	/*************************************************************************
+	 * Saturation
+	 *************************************************************************/
+	if (video_quality->saturation == 0x8000) {
+		calc_saturation = MID_SATURATION_YUV;
+	} else if (video_quality->saturation < 0x8000) {
+		/* we have here a saturation that is less than the default
+		 * mid point */
+		calc_saturation_tmp = video_quality->saturation;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = MID_SATURATION_YUV;
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		if (calc_saturation > 0x7F) {
+			calc_saturation = 0x7F;
+		}
+	} else {
+		/* we have here a saturation that is more than the default
+		 * mid point*/
+		calc_saturation_tmp = video_quality->saturation - 0x8000;
+		calc_saturation_tmp <<= 12;
+		calc_saturation_tmp /= 0x8000;
+		calc_saturation     = (0x3FF - MID_SATURATION_YUV);
+		calc_saturation     *= calc_saturation_tmp;
+		calc_saturation     += BIT11;
+		calc_saturation     >>= 12;
+		calc_saturation     += MID_SATURATION_YUV;
+
+		if (calc_saturation > 0x3FF) {
+			calc_saturation = 0x3FF;
+		}
+	}
+
+	ovl_regs_tnc->col_ctl_sat_hue =
+		(ovl_regs_tnc->col_ctl_sat_hue & 0xFFFFFC00) |
+		(calc_saturation & 0x3FF);
+
+	/*************************************************************************
+	 * Hue
+	 *************************************************************************/
+	/* Hue is always set to the default value.  It is based on the saturation
+	 * value, and having a separate hue is of minimal value. */
+	ovl_regs_tnc->col_ctl_sat_hue =
+		(ovl_regs_tnc->col_ctl_sat_hue & 0xF800FFFF);
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+static void check_gamma(unsigned int *gamma)
+{
+
+	if (*gamma < IGD_OVL_GAMMA_MIN) {
+		EMGD_ERROR("Gamma to small (0x%x in 24i.8f format), "
+			"changing to Min Gamma (0.6)",
+			*gamma);
+		*gamma = IGD_OVL_GAMMA_MIN;
+	}
+	if (*gamma > IGD_OVL_GAMMA_MAX) {
+		EMGD_ERROR("Gamma to large (0x%x in 24i.8f format), "
+			"changing to Max Gamma (6.0)",
+			*gamma);
+		*gamma = IGD_OVL_GAMMA_MAX;
+	}
+
+	return;
+}
+
+/*-----------------------------------------------------------------------------
+ * Function: ovl_update_gamma_tnc()
+ *
+ * Description:
+ *    This function sets the gamma correction values for the overlays.
+ *
+ * Returns:
+ *   != 0 on Error
+ *   IGD_SUCCESS on Success
+ *---------------------------------------------------------------------------*/
+static int ovl_update_gamma_tnc(
+	igd_display_context_t *display,
+	igd_ovl_gamma_info_t * ovl_gamma)
+{
+	const int gamma_reg_input[OVL_TOTAL_GAMMA_REG] = {8, 16, 32, 64, 128, 192};
+	const int gamma_reg_offset[OVL_TOTAL_GAMMA_REG] = {
+		OVL_REG_ADDR_GAMMA0,
+		OVL_REG_ADDR_GAMMA1,
+		OVL_REG_ADDR_GAMMA2,
+		OVL_REG_ADDR_GAMMA3,
+		OVL_REG_ADDR_GAMMA4,
+		OVL_REG_ADDR_GAMMA5
+	};
+	const unsigned int gamma_def[OVL_TOTAL_GAMMA_REG] = {
+		0x00080808,
+		0x00101010,
+		0x00202020,
+		0x00404040,
+		0x00808080,
+		0x00c0c0c0
+	};
+	unsigned int          new_gamma_red_24i_8f, new_gamma_green_24i_8f;
+	unsigned int          new_gamma_blue_24i_8f;
+	unsigned int          gamma_normal_r_24i_8f;
+	unsigned int          gamma_normal_g_24i_8f;
+	unsigned int          gamma_normal_b_24i_8f;
+	unsigned int          gamma_reg, gamma_reg_24i_8f;
+	unsigned int          i;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The gamma values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+	/* If the overlay gamma is disabled, set it to the default */
+	if ((ovl_gamma->flags & IGD_OVL_GAMMA_ENABLE) == IGD_OVL_GAMMA_DISABLE) {
+		for (i = 0; i < OVL_TOTAL_GAMMA_REG; i++) {
+			/* program register */
+			EMGD_WRITE32(gamma_def[i], MMIO(display) + gamma_reg_offset[i]);
+		}
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* It is assumed that the input value is a 24-bit number */
+	new_gamma_red_24i_8f   = ovl_gamma->red;
+	new_gamma_green_24i_8f = ovl_gamma->green;
+	new_gamma_blue_24i_8f  = ovl_gamma->blue;
+
+	/* Ensure the gamma values are between MIN and MAX */
+	check_gamma(&new_gamma_red_24i_8f);
+	check_gamma(&new_gamma_green_24i_8f);
+	check_gamma(&new_gamma_blue_24i_8f);
+
+	/*
+	 * Program RGB for each of the 6 gamma registers
+	 */
+
+	/* Since the OS_POW_FIX function can only take an integer base,
+	 * we need to normalize the result by gamma_normal_x
+	 */
+	gamma_normal_r_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_red_24i_8f);
+	gamma_normal_g_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_green_24i_8f);
+	gamma_normal_b_24i_8f =  OS_POW_FIX(255, (1<<16)/new_gamma_blue_24i_8f);
+
+	for( i = 0; i < OVL_TOTAL_GAMMA_REG; i++ )
+	{
+		/* red */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+								(1<<16)/new_gamma_red_24i_8f);
+		gamma_reg        =
+			((255 * gamma_reg_24i_8f) / gamma_normal_r_24i_8f) << 16;
+
+		/* green */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_green_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_g_24i_8f) << 8;
+
+		/* blue */
+		gamma_reg_24i_8f = OS_POW_FIX(gamma_reg_input[i],
+							(1<<16)/new_gamma_blue_24i_8f);
+		gamma_reg        |=
+			((255 * gamma_reg_24i_8f) / gamma_normal_b_24i_8f);
+
+		/* turn overlay off (TBD) */
+
+		/* program register */
+		EMGD_WRITE32(gamma_reg, MMIO(display) + gamma_reg_offset[i]);
+
+		/* turn overlay on (TBD) */
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+/*----------------------------------------------------------------------
+ * Function: ovl_update_coeff_tnc()
+ * Description: Function to calculate the filter coeffcient
+ *              registers for tnc
+ * Notes in Usage:
+ *
+ *----------------------------------------------------------------------*/
+static unsigned int ovl_update_coeff_tnc(
+	ovl_reg_image_tnc_t	*ovl_regs_tnc,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	unsigned int        flags)
+{
+	unsigned int scale_int, scale_fpint;
+
+	unsigned int dest_h = dest_rect->y2 - dest_rect->y1;
+	unsigned int dest_w = dest_rect->x2 - dest_rect->x1;
+	unsigned int src_h  = src_rect->y2  - src_rect->y1;
+	unsigned int src_w  = src_rect->x2  - src_rect->x1;
+
+	EMGD_TRACE_ENTER;
+
+	/* FIXME: The coeff values are re-written for every alter_ovl call.
+	 * This may cause issues or may be to slow?  If so, store the previous
+	 * values and only re-write when they change. */
+
+        /*
+	ovl_regs_tnc =
+		(ovl_reg_image_tnc_t *)(display->context->device_context.virt_fb_adr +
+			ovl_context->reg_update_offset);
+        ovl_regs_tnc = phys_to_virt(ovl_context->reg_update_phys);
+        */
+
+	/* In interleaved mode, the src_h is /2 */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		src_h >>= 1;
+	}
+
+	/* Y Horizontal */
+	scale_int = ((ovl_regs_tnc->yrgb_scale) >> 16) & 0x7;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_w << 20) / dest_w) / scale_int;
+	}
+	ovl_update_coeff_regs(5, scale_fpint, 1, 1,
+		(unsigned short *)ovl_regs_tnc->y_horz_coeff_single);
+
+	/* Y Vertical */
+	scale_int = ((ovl_regs_tnc->vert_downscale) >> 16) & 0x7ff;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_h << 20) / dest_h) / scale_int;
+	}
+	ovl_update_coeff_regs(3, scale_fpint, 0, 1,
+		(unsigned short *)ovl_regs_tnc->y_vert_coeff_single);
+
+	/* UV Horizontal */
+	scale_int = ((ovl_regs_tnc->uv_scale) >> 16) & 0x7;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_w << 20) / dest_w) / scale_int;
+		scale_fpint >>= get_uv_shift_x(src_surf->pixel_format);
+	}
+	ovl_update_coeff_regs(3, scale_fpint , 1, 0,
+		(unsigned short *)ovl_regs_tnc->uv_horz_coeff_single);
+
+	/* UV Vertical */
+	scale_int = (ovl_regs_tnc->vert_downscale) & 0x7ff;
+	if (!scale_int) {
+		/* upscale - clamp to 1.0 */
+		scale_fpint = 1<<20;
+	} else {
+		scale_fpint = ((src_h << 20) / dest_h) / scale_int;
+		scale_fpint >>= get_uv_shift_y(src_surf->pixel_format);
+	}
+	ovl_update_coeff_regs(3, scale_fpint, 0, 0,
+		(unsigned short *)ovl_regs_tnc->uv_vert_coeff_single);
+
+	/* Adjust for 2-line Vertical Buffer */
+	if((ovl_regs_tnc->config & OVL_CONFIG_LINE_BUFF_MASK)==
+		OVL_CONFIG_TWO_LINE_BUFF){
+		ovl_update_coeff_regs(2, 0x10, 0, 1,
+			(unsigned short *)ovl_regs_tnc->y_vert_coeff_single);
+		ovl_update_coeff_regs(2, 0x10, 0, 0,
+			(unsigned short *)ovl_regs_tnc->uv_vert_coeff_single);
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+static unsigned int convert_color_key_to_hw (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	case IGD_PF_ARGB8_INDEXED:
+	default:
+		output = input;
+		break;
+	case IGD_PF_RGB16_565:
+		output =
+			((((input & 0xf800)>>11)<<3)<<16) |
+			((((input & 0x07e0)>>5 )<<2)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	case IGD_PF_ARGB16_1555:
+		output =
+			((((input & 0x7c00)>>10)<<3)<<16) |
+			((((input & 0x03e0)>>5 )<<3)<<8 ) |
+			((((input & 0x001f)>>0 )<<3)<<0 );
+		break;
+	}
+
+	return output;
+}
+static unsigned int convert_color_key_to_mask (
+	unsigned long pf,
+	unsigned int input)
+{
+	unsigned int output;
+
+	switch (pf) {
+	case IGD_PF_ARGB32:
+	case IGD_PF_xRGB32:
+	default:
+		output = 0x00000000;
+		break;
+	case IGD_PF_RGB16_565:
+		output = 0x00070307;
+		break;
+	case IGD_PF_ARGB16_1555:
+		output = 0x00070707;
+		break;
+	case IGD_PF_ARGB8_INDEXED:
+		output = 0x00ffff00;
+		break;
+	}
+
+	return output;
+}
+
+
+#ifndef OVL_TNC_CACHE_QUICK_SWAP
+
+static unsigned int ovl_update_regs_tnc(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl_reg_image_tnc_t *ovl_regs_tnc, *ovl_cache_tnc;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+        /*
+	ovl_regs_tnc =
+		(ovl_reg_image_tnc_t *)(display->context->device_context.virt_fb_adr +
+			ovl_context->reg_update_offset);
+        */
+	ovl_regs_tnc = phys_to_virt(ovl_context->reg_update_phys);
+	ovl_cache_tnc = OS_ALLOC(sizeof(ovl_reg_image_tnc_t));
+	OS_MEMSET(ovl_cache_tnc, 0, sizeof(ovl_reg_image_tnc_t));
+
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+		/* Turn the overlay Off */
+		ovl_regs_tnc->command = 0;
+		/* Always use buf 0 when turning the overlay off. */
+		ovl_context->ovl_buff = 0;
+		OS_FREE(ovl_cache_tnc);
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* Force value to even due hardware expects even number */
+	dest_rect->y1 &= ~1;
+	dest_rect->y2 = (dest_rect->y2 + 1) & ~1;
+	dest_rect->x1 &= ~1;
+	dest_rect->x2 = (dest_rect->x2 + 1) & ~1;
+
+	/*************************************************************************
+	 * Copy the information passed in to the HW overlay structure
+	 *************************************************************************/
+	/* Zero the config and command, since they will be OR'ed in with data
+	 * below */
+	ovl_cache_tnc->config = 0;
+	ovl_cache_tnc->command = 0;
+
+	/* Set overlay to the proper pipe */
+	if (1 == PIPE(display)->pipe_num) {
+		/* This is pipe B */
+		ovl_cache_tnc->config |= 1 << 18;
+	}
+
+	/* Interleaved/progressive and Odd/Even if interleaved */
+	if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+		ovl_cache_tnc->command |= OVL_CMD_FIELD_MODE;
+		/* Need to enable FIELD SYNC OVERLAY FLIP in field mode. */
+		ovl_cache_tnc->command |= OVL_CMD_FIELD_SYNC_FLIP;
+		/* HSD# 203821 Mplayer outputs single buffer including both even and odd fields. */
+		if (flags & IGD_OVL_ALTER_FLIP_ODD) {
+			ovl_cache_tnc->command |= OVL_CMD_ACT_FLD1;
+			/* HSD# 203821 To display odd field, starts from first odd field. */
+			if (0 == src_rect->y1 & 1) {
+				src_rect->y1 += 1;
+			}
+		} else {
+			ovl_cache_tnc->command |= OVL_CMD_ACT_FLD0;
+			/* HSD# 203821 To display even field, starts from first even field. */
+			if (0 != src_rect->y1 & 1) {
+				src_rect->y1 += 1;
+			}
+		}
+	} else {
+		ovl_cache_tnc->command |= OVL_CMD_FRAME_MODE;
+	}
+
+	/* Dest rect information */
+	ovl_cache_tnc->dest_pos_x_left        = (unsigned short)dest_rect->x1;
+	ovl_cache_tnc->dest_pos_y_top         = (unsigned short)dest_rect->y1;
+	ovl_cache_tnc->dest_width_x           =
+		(unsigned short)(dest_rect->x2 - dest_rect->x1);
+	ovl_cache_tnc->dest_height_y          =
+		(unsigned short)(dest_rect->y2 - dest_rect->y1);
+
+	/* Src surface offset information */
+	ret = ovl_update_src_ptr_tnc(display, ovl_cache_tnc, src_surf, src_rect);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay updating src failed");
+		return ret;
+	}
+
+	/* Src rect and surface information */
+	ret = ovl_update_src_tnc(display, ovl_cache_tnc, src_surf, src_rect);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay updating src failed");
+		return ret;
+	}
+
+	/* Scaling information including Vertical downscaling.
+	 * Scaling should be guaranteed to work, since if the scale is not
+	 * supported, it should have already been blended to a supported scale. */
+	ret = ovl_update_scale_tnc(ovl_cache_tnc, src_surf, src_rect, dest_rect,
+		flags);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay updating scaling failed");
+		return ret;
+	}
+
+	/* Color control information */
+	ret = ovl_update_video_quality_tnc(ovl_cache_tnc, src_surf,
+		&ovl_info->video_quality);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay video quality failed");
+		return ret;
+	}
+	ret = ovl_update_gamma_tnc(display, &ovl_info->gamma);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay gamma failed");
+		return ret;
+	}
+
+	/* Destination color key */
+	EMGD_DEBUG("Color key.flags: 0x%lx", ovl_info->color_key.flags);
+	if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Dest Color Key");
+		/* The mask and color key are different for the different
+		 * pixel formats */
+		ovl_cache_tnc->dest_ckey_val = convert_color_key_to_hw(
+			PLANE(display)->fb_info->pixel_format,
+			ovl_info->color_key.dest);
+		ovl_cache_tnc->dest_ckey_mask = convert_color_key_to_mask(
+			PLANE(display)->fb_info->pixel_format,
+			ovl_info->color_key.dest);
+		ovl_cache_tnc->dest_ckey_mask |= 0x80000000;
+	} else {
+		EMGD_DEBUG("Overlay Disable Dest Color Key");
+		ovl_cache_tnc->dest_ckey_mask = 0x00000000;
+	}
+
+	/* Source Color key */
+	if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+		EMGD_DEBUG("Overlay Enable Src Color Key");
+		ovl_cache_tnc->source_ckey_high = ovl_info->color_key.src_hi;
+		ovl_cache_tnc->source_ckey_low = ovl_info->color_key.src_lo;
+
+		ovl_cache_tnc->source_ckey_mask = 0x07000000;
+	} else {
+		EMGD_DEBUG("Overlay Disable Src Color Key");
+		ovl_cache_tnc->source_ckey_mask = 0x00000000;
+	}
+
+	/* Coefficients - Must be after Scaling */
+//	ret = ovl_update_coeff_tnc(display, src_surf, src_rect, dest_rect, flags);
+	ret = ovl_update_coeff_tnc(ovl_cache_tnc, src_surf, src_rect, dest_rect, flags);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay updating coefficient failed");
+		return ret;
+	}
+
+	/* Phase information - Must be after Coefficients */
+	ret = ovl_update_phase_tnc(ovl_cache_tnc, src_surf, src_rect);
+	if (ret) {
+		OS_FREE(ovl_cache_tnc);
+		EMGD_ERROR_EXIT("Overlay updating phase failed");
+		return ret;
+	}
+
+	/* General overlay information.  Turn the overlay on and alternate
+	 * between Buffer 0 and Buffer 1. */
+	ovl_cache_tnc->command = (ovl_cache_tnc->command & 0xfffffff3) |
+			ovl_context->ovl_buff | 1;
+	ovl_context->ovl_buff ^= OVL_CMD_ACT_BUF1;
+
+	/* Dump out the Overlay Update Registers if debugging */
+	EMGD_VERBOSE(hal.dump_overlay_regs, ovl_dump_regs_tnc(ovl_regs_tnc));
+
+	OS_MEMCPY(ovl_regs_tnc, ovl_cache_tnc, sizeof(ovl_reg_image_tnc_t));
+	OS_FREE(ovl_cache_tnc);
+
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+#else /* new version */
+
+
+/* Atom E6xx cache structure */
+static ovl_tnc_cache_t ovl_cache;
+
+/* Flag to signal the cache is invalid and needs
+ * to be re-initialized */
+static int ovl_cache_needs_init = TRUE;
+
+
+
+
+static unsigned int ovl_update_regs_tnc(
+	igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	ovl_reg_image_tnc_t *ovl_regs_tnc, *ovl_cache_regs;
+	unsigned int cache_changed;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	/* get the pointers to the real regs, and our cached copy of them */
+	ovl_regs_tnc = phys_to_virt(ovl_context->reg_update_phys);
+	ovl_cache_regs = &ovl_cache.ovl_regs;
+
+	/* Fast path for turning off overlay. No need for cache */
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_OFF) {
+
+		/* Turn the overlay Off */
+		ovl_regs_tnc->command = 0;
+
+		/* if we were using the cache, turn it off there too */
+		if (!ovl_cache_needs_init) {
+			ovl_cache.ovl_regs.command = 0;
+		}
+
+		/* Reset the cache */
+		ovl_cache_needs_init = TRUE;
+
+		/* Always use buf 0 when turning the overlay off. */
+		ovl_context->ovl_buff = 0;
+		EMGD_TRACE_EXIT;
+		return IGD_SUCCESS;
+	}
+
+	/* Force value of y1 to even due hardware expects even number */
+	dest_rect->y1 &= ~1;
+	dest_rect->y2 = (dest_rect->y2 + 1) & ~1;
+	dest_rect->x1 &= ~1;
+	dest_rect->x2 = (dest_rect->x2 + 1) & ~1;
+
+	/* Init the cache if necessary */
+	if (ovl_cache_needs_init) {
+		/* Force every cache check to miss */
+		OS_MEMSET(&ovl_cache, 0, sizeof(ovl_tnc_cache_t));
+
+		/* We just set our cached flags to 0, which might accidently
+		 * match up with "OFF" for some important incoming flag
+		 * bits, causing us to think we already handled them when
+		 * we didn't.  So set our cached flags to the exact
+		 * opposite of the incoming flags, which will force
+		 * us to test and handle every single bit, regardless
+		 * of whether it is on or off. */
+		ovl_cache.flags = ~flags;
+
+		/* init our cached registers */
+		OS_MEMCPY(ovl_cache_regs,
+			  ovl_regs_tnc,
+			  sizeof(ovl_reg_image_tnc_t));
+
+		/* initialization complete */
+		ovl_cache_needs_init = FALSE;
+	}
+
+	/* See what has changed in the cache */
+	cache_changed = get_cache_changes_tnc(src_surf,
+		src_rect,
+		dest_rect,
+		ovl_info,
+		flags,
+		&ovl_cache);
+
+	/* Perhaps the biggest challenge of caching the overlay
+	 * state is what to do with the command and config regs.
+	 * Normally we would clear command and config to 0 here,
+	 * and let the update process set only the bits that are
+	 * needed.  But doing this would invalidate our cache.
+	 * Instead we are relying on the above call to
+	 * get_cache_changes() to clear those bits in command
+	 * and config that will be changing */
+
+	/* Set overlay to the proper pipe */
+	/* it is cheaper to just set this, than to test it and set it. */
+	if (1 == PIPE(display)->pipe_num) {
+		/* This is pipe B */
+		ovl_cache_regs->config |= 1 << 18;
+	} else {
+		ovl_cache_regs->config &= ~(1 << 18);
+	}
+
+	if (cache_changed & IGD_OVL_TNC_UPDATE_FLAGS) {
+		/* Interleaved/progressive and Odd/Even if interleaved. */
+		if (flags & IGD_OVL_ALTER_INTERLEAVED) {
+			ovl_cache_regs->command |= OVL_CMD_FIELD_MODE;
+			/* enable FIELD SYNC OVERLAY FLIP in field mode. */
+			ovl_cache_regs->command |= OVL_CMD_FIELD_SYNC_FLIP;
+			/* HSD# 203821 Mplayer outputs single buffer including both even and odd fields. */
+			if (flags & IGD_OVL_ALTER_FLIP_ODD) {
+				ovl_cache_regs->command |= OVL_CMD_ACT_FLD1;
+				/* HSD: 203821 To display odd field, starts from first odd field. */
+				if (0 == (src_rect->y1 & 1)) {
+					src_rect->y1 += 1;
+				}
+			} else {
+				ovl_cache_regs->command |= OVL_CMD_ACT_FLD0;
+				/* HSD: 203821 To display even field, starts from first even field. */
+				if (0 != (src_rect->y1 & 1)) {
+					src_rect->y1 += 1;
+				}
+			}
+		} else {
+			ovl_cache_regs->command |= OVL_CMD_FRAME_MODE;
+		}
+	}
+
+	/* Has our destination rectangle changed? */
+	if (cache_changed & IGD_OVL_TNC_UPDATE_DEST) {
+		ovl_cache_regs->dest_pos_x_left  =
+			(unsigned short) dest_rect->x1;
+		ovl_cache_regs->dest_pos_y_top   =
+			(unsigned short) dest_rect->y1;
+		ovl_cache_regs->dest_width_x     =
+			(unsigned short) (dest_rect->x2 - dest_rect->x1);
+		ovl_cache_regs->dest_height_y    =
+			(unsigned short) (dest_rect->y2 - dest_rect->y1);
+
+	}
+
+	/* Always update the source pointers every frame */
+	ret = ovl_update_src_ptr_tnc(display,
+				     ovl_cache_regs,
+				     src_surf,
+				     src_rect);
+	if (ret) {
+		/* Not good. Invalidate the entire cache and bail. */
+		ovl_cache_needs_init = TRUE;
+		EMGD_ERROR_EXIT("Overlay updating src pointers failed");
+		return ret;
+	}
+
+	/* Did either the Src rect or surface change? */
+	if (cache_changed & (IGD_OVL_TNC_UPDATE_SURF |
+		IGD_OVL_TNC_UPDATE_SRC  ) ) {
+
+		ret = ovl_update_src_tnc(display,
+					 ovl_cache_regs,
+					 src_surf,
+					 src_rect);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay updating src failed");
+			return ret;
+		}
+	}
+
+	/* Scaling information including Vertical downscaling.
+	 * Scaling should be guaranteed to work, since if the scale
+	 * is not supported, it should have already been blended
+	 * to a supported scale. */
+	if ( cache_changed & (IGD_OVL_TNC_UPDATE_SRC |
+		IGD_OVL_TNC_UPDATE_SURF | IGD_OVL_TNC_UPDATE_DEST |
+		IGD_OVL_TNC_UPDATE_FLAGS) ) {
+
+		ret = ovl_update_scale_tnc(ovl_cache_regs,
+					   src_surf,
+					   src_rect,
+					   dest_rect,
+					   flags);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay updating scaling failed");
+			return ret;
+		}
+	}
+
+	/* Did video quality change? */
+	if (cache_changed & (IGD_OVL_TNC_UPDATE_VQ |
+		IGD_OVL_TNC_UPDATE_SURF ) ) {
+
+		/* Color control information */
+		ret = ovl_update_video_quality_tnc(ovl_cache_regs, src_surf,
+			&ovl_info->video_quality);
+
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay video quality failed");
+			return ret;
+		}
+	}
+
+	/* Did gamma change? */
+	if (cache_changed & IGD_OVL_TNC_UPDATE_GAMMA) {
+		ret = ovl_update_gamma_tnc(display, &ovl_info->gamma);
+		if (ret) {
+			/* Not good. Invalidate the entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay gamma failed");
+			return ret;
+		}
+	}
+
+	/* Did color key change? */
+	if (cache_changed & IGD_OVL_TNC_UPDATE_COLORKEY) {
+		/* Destination color key */
+		if (ovl_info->color_key.flags & IGD_OVL_DST_COLOR_KEY_ENABLE) {
+			/* The mask and color key are different for the
+			 * different pixel formats */
+			ovl_cache_regs->dest_ckey_val =
+				convert_color_key_to_hw(
+				    PLANE(display)->fb_info->pixel_format,
+				    ovl_info->color_key.dest);
+			ovl_cache_regs->dest_ckey_mask =
+				convert_color_key_to_mask(
+				    PLANE(display)->fb_info->pixel_format,
+				    ovl_info->color_key.dest);
+			ovl_cache_regs->dest_ckey_mask |= 0x80000000;
+		} else {
+			ovl_cache_regs->dest_ckey_mask = 0x00000000;
+		}
+
+		/* Source Color key */
+		if (ovl_info->color_key.flags & IGD_OVL_SRC_COLOR_KEY_ENABLE) {
+			ovl_cache_regs->source_ckey_high =
+				ovl_info->color_key.src_hi;
+			ovl_cache_regs->source_ckey_low =
+				ovl_info->color_key.src_lo;
+			ovl_cache_regs->source_ckey_mask = 0x07000000;
+		} else {
+			ovl_cache_regs->source_ckey_mask = 0x00000000;
+		}
+	} /* end color key changes */
+
+	/* Coefficients - Must be after Scaling */
+	if (cache_changed & (IGD_OVL_TNC_UPDATE_SRC |
+		IGD_OVL_TNC_UPDATE_SURF | IGD_OVL_TNC_UPDATE_DEST |
+		IGD_OVL_TNC_UPDATE_FLAGS ) ) {
+
+		ret = ovl_update_coeff_tnc(ovl_cache_regs,
+					   src_surf,
+					   src_rect,
+					   dest_rect,
+					   flags);
+		if (ret) {
+			/* Not good. Invalidate entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay update coefficient failed");
+			return ret;
+		}
+	}
+
+	/* Phase information - Must be after Coefficients */
+	if (cache_changed & (IGD_OVL_TNC_UPDATE_SRC |
+		IGD_OVL_TNC_UPDATE_SURF ) ) {
+
+		ret = ovl_update_phase_tnc(ovl_cache_regs,
+					   src_surf,
+					   src_rect);
+		if (ret) {
+			/* Not good. Invalidate entire cache and bail. */
+			ovl_cache_needs_init = TRUE;
+			EMGD_ERROR_EXIT("Overlay updating phase failed");
+			return ret;
+		}
+	}
+
+	/* General overlay information.  Turn the overlay on and alternate
+	 * between Buffer 0 and Buffer 1. */
+	ovl_cache_regs->command = (ovl_cache_regs->command & 0xfffffff3) |
+		ovl_context->ovl_buff | 1;
+	ovl_context->ovl_buff ^= OVL_CMD_ACT_BUF1;
+
+	/* Dump out the Overlay Update Registers if debugging */
+	EMGD_VERBOSE(hal.dump_overlay_regs, ovl_dump_regs_tnc(ovl_regs_tnc));
+
+	/* Finally, transfer the cached regs to the real regs */
+	OS_MEMCPY(ovl_regs_tnc,
+		  ovl_cache_regs,
+		  sizeof(ovl_reg_image_tnc_t));
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+
+}
+#endif
+
+static unsigned int ovl_send_instr_tnc(
+	igd_display_context_t     *display,
+	unsigned int      flags)
+{
+	unsigned char * mmio = MMIO(display);
+	unsigned long tmp;
+
+	EMGD_TRACE_ENTER;
+
+	/* We dont need the CMD_WAIT_OVL_TNC instruction coz
+	 * our alter_ovl code already querried status
+	 * for last flip completion before getting here. See
+	 * alter_ovl_tnc calling query
+	 */
+
+	if ((flags & IGD_OVL_ALTER_ON) == IGD_OVL_ALTER_ON) {
+
+		ovl_context->state = OVL_STATE_ON;
+		/*
+		 * If Overlay + FB Blend is requested and the FB is xRGB
+		 * turn on the ARGB format.
+		 */
+		if(ovl_context->fb_blend_ovl) {
+			tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x18000000) {
+				tmp = tmp & 0xc3FFFFFF;
+				EMGD_WRITE32(tmp | 0x1c000000, mmio + PLANE(display)->plane_reg);
+				EMGD_READ32(mmio + PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 0x1c);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 0x1c);
+			}
+		}
+
+	} else {
+
+		if (ovl_context->fb_blend_ovl) {
+			tmp = EMGD_READ32(mmio +  PLANE(display)->plane_reg);
+			if((tmp & 0x3c000000) == 0x1c000000) {
+				tmp = tmp & 0xc3FFFFFF;
+				EMGD_WRITE32(tmp | 0x18000000, mmio +  PLANE(display)->plane_reg);
+				EMGD_READ32(mmio + PLANE(display)->plane_reg);
+				tmp = EMGD_READ32(mmio + PLANE(display)->plane_reg + 0x1c);
+				EMGD_WRITE32(tmp, mmio + PLANE(display)->plane_reg + 0x1c);
+				OS_SLEEP(100);
+			}
+		}
+
+		OS_SLEEP(1);
+
+		/* if overlay is being turned OFF - ensure it's ON first */
+		if (ovl_context->state == OVL_STATE_OFF) {
+			/* Overlay is already off, no need to turn it off again */
+			EMGD_TRACE_EXIT;
+			return IGD_SUCCESS;
+		}
+
+		ovl_context->state = OVL_STATE_OFF;
+	}
+
+	/* Write the address of the memory buffer to the Overlay Update
+	 * Address Register causes the HW to load the new values from the
+	 * memory on the next VBLANK */
+
+	EMGD_WRITE32((ovl_context->reg_update_offset | 0x01), mmio + 0x30000);
+
+
+	ovl_context->sync = 0;
+
+	/*
+	display->context->dispatch.sync(display,
+		IGD_PRIORITY_NORMAL,
+		&ovl_context->sync,
+		IGD_SYNC_NONBLOCK);
+	*/
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
+
+static int alter_ovl_tnc(igd_display_context_t *display,
+	igd_surface_t       *src_surf,
+	igd_rect_t          *src_rect,
+	igd_rect_t          *dest_rect,
+	igd_ovl_info_t      *ovl_info,
+	unsigned int         flags)
+{
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	/* Check to ensure the overlay can be used given the current mode as
+	 * well as what the IAL is asking for.  If not return an error. */
+	ret = ovl_check_tnc(display, src_surf, src_rect, dest_rect, ovl_info,
+			flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay Check failed");
+		return ret;
+	}
+
+	/* Check if last flip is still pending.
+	 * This is necessary for the following reasons:
+	 *    - If the previous instructions have not been processed, then the
+	 *      ovl_regs_tnc is still in use and can not be overwritten.
+	 */
+	if ((FALSE == query_ovl_tnc(
+			(igd_display_h)display, IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE)) &&
+		(flags & IGD_OVL_ALTER_ON)) {
+		/* Only return an error if the overlay is on.  If turning it off,
+		 * allow it to continue, since something may have failed and we
+		 * should try our best to turn the overlay off. */
+		EMGD_ERROR_EXIT("Query Overlay failed");
+		return -IGD_ERROR_HWERROR;
+	}
+
+	/* Update all Overlay Update Registers */
+	ret = ovl_update_regs_tnc(display, src_surf, src_rect, dest_rect, ovl_info,
+		flags);
+	if (ret) {
+		EMGD_ERROR_EXIT("Overlay Update Registers failed");
+		return ret;
+	}
+
+	/* Send the instructions to the command queue */
+	ret = ovl_send_instr_tnc(display, flags);
+
+	EMGD_TRACE_EXIT;
+	return ret;
+}
+
+
+
+static int query_ovl_tnc(igd_display_h display_h,
+	unsigned int flags)
+{
+	igd_display_context_t *display = (igd_display_context_t *)display_h;
+	os_alarm_t timeout;
+
+	EMGD_TRACE_ENTER;
+
+	switch (flags) {
+
+	case IGD_OVL_QUERY_IS_HW_SUPPORTED:
+		/* This is the first overlay, so HW overlay is supported */
+		break;
+
+	case IGD_OVL_QUERY_IS_LAST_FLIP_DONE:
+
+		/* Check to see if the register update is complete.  If not return
+		 * FALSE (Flip not done). */
+		if(!(EMGD_READ32(MMIO(display) + 0x30008) & 0x80000000)) {
+			/* This is not an error */
+			return FALSE;
+		}
+
+		/* Now that we know the last flip is done and the register update is
+		 * complete, set the sync to 0 and return TRUE (Flip done). */
+		ovl_context->sync = 0;
+		break;
+	case IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE:
+		/* Wait for 200 milliseconds for the last flip to complete.  If not
+		 * done in that time, there is likely a hardware problem so return
+		 * FALSE. */
+		timeout = OS_SET_ALARM(200);
+		do {
+			if (TRUE ==
+				query_ovl_tnc(display_h, IGD_OVL_QUERY_IS_LAST_FLIP_DONE)) {
+				EMGD_TRACE_EXIT;
+				return TRUE;
+			}
+		} while (!OS_TEST_ALARM(timeout));
+		EMGD_ERROR_EXIT("Timeout waiting for last flip done");
+		return FALSE;
+		break;
+	case IGD_OVL_QUERY_IS_GAMMA_SUPPORTED:
+		return TRUE;
+		break;
+	case IGD_OVL_QUERY_IS_VIDEO_PARAM_SUPPORTED:
+		return TRUE;
+		break;
+	}
+
+	EMGD_TRACE_EXIT;
+	return TRUE;
+}
+
+static int query_max_size_ovl_tnc(
+	igd_display_h display_h,
+	unsigned long pf,
+	unsigned int *max_width,
+	unsigned int *max_height)
+{
+	ovl_chipset_tnc_t *ovl_chip;
+
+	EMGD_TRACE_ENTER;
+
+	ovl_chip = ovl_chipset_tnc;
+	*max_width = 0;
+	*max_height = 0;
+	while(ovl_chip->num_linebuf != OVL_CONFIG_NO_LINE_BUFF){
+		if(((pf & IGD_PF_MASK) == ovl_chip->pixel_format) &&
+			(ovl_chip->max_width > *max_width)) {
+			*max_width = ovl_chip->max_width;
+			*max_height = ovl_chip->max_height;
+		}
+		ovl_chip++;
+	}
+
+	EMGD_TRACE_EXIT;
+	return IGD_SUCCESS;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.c b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.c
new file mode 100644
index 0000000..734e0d6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.c
@@ -0,0 +1,246 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: ovl_tnc_cache.c
+ * $Revision: 1.5 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Support functions for overlay caching.
+ *-----------------------------------------------------------------------------
+ */
+#define MODULE_NAME hal.overlay
+
+#include "ovl_tnc_cache.h"
+
+
+/* Copies a source rectangle to the cache */
+void copy_src_rect_tnc(igd_rect_t *src_rect, povl_tnc_cache_t ovl_cache)
+{
+	if (src_rect) {
+		OS_MEMCPY(&ovl_cache->src_rect,
+			  src_rect,
+			  sizeof(igd_rect_t));
+	} else {
+		OS_MEMSET(&ovl_cache->src_rect,
+			  0,
+			  sizeof(igd_rect_t));
+	}
+}
+
+
+/* Copies a dest rectangle to the cache */
+void copy_dest_rect_tnc( igd_rect_t *dest_rect, povl_tnc_cache_t ovl_cache)
+{
+	if (dest_rect) {
+		OS_MEMCPY(&ovl_cache->dest_rect,
+			  dest_rect,
+			  sizeof(igd_rect_t));
+	} else {
+		OS_MEMSET(&ovl_cache->dest_rect,
+			  0,
+			  sizeof(igd_rect_t));
+	}
+}
+
+
+/* Copies a surface to the cache */
+void copy_surf_tnc(igd_surface_t *src_surf, povl_tnc_cache_t ovl_cache)
+{
+	if (src_surf) {
+		OS_MEMCPY(&ovl_cache->src_surf,
+			  src_surf,
+			  sizeof(igd_surface_t));
+	} else {
+		OS_MEMSET(&ovl_cache->src_surf,
+			  0,
+			  sizeof(igd_surface_t));
+	}
+}
+
+
+/* Copies the color key to the cache */
+void copy_color_key_tnc( igd_ovl_info_t *ovl_info, povl_tnc_cache_t ovl_cache)
+{
+	if (ovl_info) {
+		OS_MEMCPY(&ovl_cache->ovl_info.color_key,
+			  &(ovl_info->color_key),
+			  sizeof(igd_ovl_color_key_info_t));
+	} else {
+		OS_MEMSET(&ovl_cache->ovl_info.color_key,
+			  0,
+			  sizeof(igd_ovl_color_key_info_t));
+	}
+}
+
+
+/* Copies the video quality info to the cache */
+void copy_vq_tnc(igd_ovl_info_t *ovl_info, povl_tnc_cache_t ovl_cache)
+{
+	if (ovl_info) {
+		OS_MEMCPY(&ovl_cache->ovl_info.video_quality,
+			  &(ovl_info->video_quality),
+			  sizeof(igd_ovl_video_quality_info_t));
+	} else {
+		OS_MEMSET(&ovl_cache->ovl_info.video_quality,
+			  0,
+			  sizeof(igd_ovl_video_quality_info_t));
+	}
+}
+
+
+/* Copies the gamma to the cache */
+void copy_gamma_tnc(igd_ovl_info_t *ovl_info, povl_tnc_cache_t ovl_cache)
+{
+	if (ovl_info) {
+		OS_MEMCPY(&ovl_cache->ovl_info.gamma,
+			  &(ovl_info->gamma),
+			  sizeof(igd_ovl_gamma_info_t));
+	} else {
+		OS_MEMSET(&ovl_cache->ovl_info.gamma,
+			  0,
+			  sizeof(igd_ovl_gamma_info_t));
+	}
+}
+
+
+
+/* Checks to see what, if anything has changed.
+ * Clears bits in the command and config register that are invalid.
+ * Returns a set of flags telling what changed */
+unsigned int get_cache_changes_tnc(
+	igd_surface_t  *src_surf,
+	igd_rect_t     *src_rect,
+	igd_rect_t     *dest_rect,
+	igd_ovl_info_t *ovl_info,
+	unsigned int    flags,
+	povl_tnc_cache_t ovl_cache)
+{
+	unsigned int cache_changed = 0;
+
+	ovl_cache->ovl2_regs.control = 0;
+
+	/* Have the flags changed? */
+	if (ovl_cache->flags != flags) {
+
+		/* NOTE that we did not check which flags changed.
+		   Currently if any flag changes, we invalidate all
+		   the flags. We could further optimize caching if
+		   we tested for individual flag changes, but I don't
+		   think it will really help much.  Once a movie starts
+		   playing, it pretty much uses the same flags
+		   for its entire duration, unless the user is mucking
+		   about with it as it plays, in which case it's going
+		   to be slow anyway. */
+
+
+		ovl_cache->flags = flags;
+		cache_changed |= IGD_OVL_TNC_UPDATE_FLAGS;
+
+		/* Invalidating the flags cache will cause the following
+		 * bits to be re-calculated.  Clear them in preparation for
+		 * re-calculation. */
+		ovl_cache->ovl_regs.command &= ~ (OVL_CMD_FRAME_MODE      |
+						 OVL_CMD_FIELD_MODE      |
+						 OVL_CMD_FIELD_SYNC_FLIP |
+						 OVL_CMD_ACT_FLD1        |
+						 OVL_CMD_ACT_FLD0        );
+	}
+
+	/* Do a comparison to source surface */
+	if (is_changed_surf(&ovl_cache->src_surf, src_surf)) {
+		cache_changed |= IGD_OVL_TNC_UPDATE_SURF;
+		copy_surf_tnc(src_surf, ovl_cache);
+
+		/* invalidating the source surface will cause the driver
+		 * to recalculate the following bits in command */
+		ovl_cache->ovl_regs.command &= ~( OVL_CMD_YUV_422  |
+						 OVL_CMD_YUV_420P |
+						 OVL_CMD_YUV_410P |
+						 OVL_CMD_YUV_NV12 |
+						 OVL_CMD_Y_SWAP   |
+						 OVL_CMD_UV_SWAP  |
+						 OVL_CMD_RGB_8888 |
+						 OVL_CMD_RGB_565  |
+						 OVL_CMD_RGB_555  );
+
+		/* Invalidating the source surface will cause the driver
+		 * to recalculate the following bits in config */
+		ovl_cache->ovl_regs.config &= ~ ( (1<<4)                    |
+					    OVL_CONFIG_LINE_BUFF_MASK );
+
+	}
+
+	/* Do a comparison to source rectangle */
+	if (is_changed_rect(&ovl_cache->src_rect, src_rect)) {
+		cache_changed |= IGD_OVL_TNC_UPDATE_SRC;
+		copy_src_rect_tnc(src_rect, ovl_cache);
+
+		/* invalidating the source rectangle will cause the driver
+		 * to recalculate the following bits in command */
+		ovl_cache->ovl_regs.command &= ~( OVL_CMD_YUV_422  |
+						 OVL_CMD_YUV_420P |
+						 OVL_CMD_YUV_410P |
+						 OVL_CMD_YUV_NV12 |
+						 OVL_CMD_Y_SWAP   |
+						 OVL_CMD_UV_SWAP  |
+						 OVL_CMD_RGB_8888 |
+						 OVL_CMD_RGB_565  |
+						 OVL_CMD_RGB_555  );
+
+		/* Invalidating the source rectangle will cause the driver
+		 * to recalculate the following bits in config */
+		ovl_cache->ovl_regs.config &= ~ ( (1<<4)                    |
+					    OVL_CONFIG_LINE_BUFF_MASK );
+
+
+	}
+
+	/* Has our destination rectangle changed? */
+	if (is_changed_rect(&ovl_cache->dest_rect, dest_rect)) {
+		cache_changed |= IGD_OVL_TNC_UPDATE_DEST;
+		copy_dest_rect_tnc(dest_rect, ovl_cache);
+	}
+
+	/* Do a comparison to overlay info color key */
+	if (is_changed_color_key(&ovl_cache->ovl_info, ovl_info)) {
+		cache_changed |= (IGD_OVL_TNC_UPDATE_INFO |
+				  IGD_OVL_TNC_UPDATE_COLORKEY);
+		copy_color_key_tnc(ovl_info, ovl_cache);
+	}
+
+	/* Do a comparison to overlay info video quality */
+	if (is_changed_vq(&ovl_cache->ovl_info, ovl_info)) {
+		cache_changed |= ( IGD_OVL_TNC_UPDATE_INFO |
+				   IGD_OVL_TNC_UPDATE_VQ);
+		copy_vq_tnc(ovl_info, ovl_cache);
+	}
+
+	/* Do a comparison to overlay info gamma */
+	if (is_changed_gamma(&ovl_cache->ovl_info, ovl_info)) {
+		cache_changed |= ( IGD_OVL_TNC_UPDATE_INFO |
+				   IGD_OVL_TNC_UPDATE_GAMMA);
+		copy_gamma_tnc(ovl_info, ovl_cache);
+	}
+
+	return cache_changed;
+}
+
diff --git a/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.h b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.h
new file mode 100644
index 0000000..0df413a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/overlay/tnc/ovl_tnc_cache.h
@@ -0,0 +1,162 @@
+/*
+ *----------------------------------------------------------------------------
+ * Filename: ovl_tnc_cache.h
+ * $Revision: 1.5 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the internal header file for overlay caching. It should be not be
+ *  by any other module besides the overlay module itself.
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _OVL_TNC_CACHE_H
+#define _OVL_TNC_CACHE_H
+
+#include <rb.h>
+#include "ovl_regs_tnc.h"
+#include "ovl2_regs_tnc.h"
+
+/* Turns the overlay cache on and off. */
+#define OVL_TNC_CACHE_QUICK_SWAP
+
+
+/*
+ * In order to cache the overlay registers we need a structure
+ * that defines and wraps them.  We already have one for the primary
+ * display, called ovl_reg_image_tnc_t.
+ * Here we define a structure for the secondary display
+ */
+
+typedef struct _ovl2_reg_tnc_cache{
+	unsigned long control;
+	unsigned long start;
+	unsigned long plane_control;
+	unsigned long plane_start;
+	unsigned long linear_offset;
+	unsigned long tiled_offset;
+
+	unsigned long pitch;
+	unsigned long dest_rect_x1y1;
+	unsigned long dest_rect_w_h;
+	unsigned long cont_bright;
+	unsigned long satn_hue;
+	unsigned long gamma_regs[OVL2_TOTAL_GAMMA_REG];
+	unsigned long colorkey_hw;
+	unsigned long colorkey_mask;
+	unsigned long ckey_high;
+	unsigned long ckey_low;
+	unsigned long ckey_enable;
+} ovl2_reg_tnc_cache_t;
+
+#define MMIO_OFFSET_TNC2_CONTROL        0x72180
+#define MMIO_OFFSET_TNC2_LINEAR_OFF     0x72184
+#define MMIO_OFFSET_TNC2_PITCH          0x72188
+#define MMIO_OFFSET_TNC2_DEST_RECT_X1Y1 0x7218C
+#define MMIO_OFFSET_TNC2_DEST_RECT_W_H  0x72190
+#define MMIO_OFFSET_TNC2_CKEY_LOW       0x72194
+#define MMIO_OFFSET_TNC2_CKEY_ENABLE    0x72198
+#define MMIO_OFFSET_TNC2_SURF_ADDR      0x7219C
+#define MMIO_OFFSET_TNC2_CKEY_HIGH      0x721A0
+#define MMIO_OFFSET_TNC2_DSPC_TILE_OFF  0x721A4
+#define MMIO_OFFSET_TNC2_CONT_BRIGHT    0x721D0
+#define MMIO_OFFSET_TNC2_SATN_HUE       0x721D4
+#define MMIO_OFFSET_TNC2_COLORKEY_HW    0x14
+#define MMIO_OFFSET_TNC2_COLORKEY_MASK  0x18
+
+/*
+ * This stucture caches the overlay state, so we don't have to
+ * re-program everything for every single frame
+ */
+typedef struct _ovl_tnc_cache {
+	igd_surface_t        src_surf;
+	igd_rect_t           src_rect;
+	igd_rect_t           dest_rect;
+	igd_ovl_info_t       ovl_info;
+	unsigned long        flags;
+	ovl_reg_image_tnc_t  ovl_regs;
+	ovl2_reg_tnc_cache_t ovl2_regs;
+} ovl_tnc_cache_t, *povl_tnc_cache_t;
+
+
+/* Flags for things that might have changed in the cache */
+#define IGD_OVL_TNC_UPDATE_DEST     (1 << 0)
+#define IGD_OVL_TNC_UPDATE_SRC      (1 << 1)
+#define IGD_OVL_TNC_UPDATE_SURF     (1 << 2)
+#define IGD_OVL_TNC_UPDATE_INFO     (1 << 3)
+#define IGD_OVL_TNC_UPDATE_FLAGS    (1 << 4)
+#define IGD_OVL_TNC_UPDATE_VQ       (1 << 5)
+#define IGD_OVL_TNC_UPDATE_GAMMA    (1 << 6)
+#define IGD_OVL_TNC_UPDATE_COLORKEY (1 << 7)
+
+
+/*
+ * Caching helper functions.  Implemented in ovl_tnc.c, but used by
+ * the ovl2 and micro_ovl modules as well.
+ */
+
+/* Tells if a rectangle has changed */
+int is_changed_rect(igd_rect_t *old_rect, igd_rect_t *new_rect);
+
+/* Tells if a surface has changed */
+int is_changed_surf(igd_surface_t *old_surf, igd_surface_t *new_surf);
+
+/* Tells if the color key has changed */
+int is_changed_color_key(igd_ovl_info_t *old_info, igd_ovl_info_t *new_info);
+
+/* Tells if the video quality has changed */
+int is_changed_vq(igd_ovl_info_t *old_info, igd_ovl_info_t *new_info);
+
+/* Tells if the gamma has changed */
+int is_changed_gamma(igd_ovl_info_t *old_info, igd_ovl_info_t *new_info);
+
+/* Copies a source rectangle to the cache */
+void copy_src_rect_tnc(igd_rect_t *src_rect, povl_tnc_cache_t ovl_cache);
+
+/* Copies a dest rectangle to the cache */
+void copy_dest_rect_tnc( igd_rect_t *dest_rect, povl_tnc_cache_t ovl_cache);
+
+/* Copies a surface to the cache */
+void copy_surf_tnc(igd_surface_t *src_surf, povl_tnc_cache_t ovl_cache);
+
+/* Copies the color key to the cache */
+void copy_color_key_tnc( igd_ovl_info_t *ovl_info, povl_tnc_cache_t ovl_cache);
+
+/* Copies the video quality info to the cache */
+void copy_vq_tnc(igd_ovl_info_t *ovl_info, povl_tnc_cache_t ovl_cache);
+
+/* Copies the gamma to the cache */
+void copy_gamma_tnc(igd_ovl_info_t *ovl_info, povl_tnc_cache_t ovl_cache);
+
+/* Checks to see what, if anything has changed.
+ * Clears bits in the command and config register that are invalid.
+ * Returns a set of flags telling what changed */
+unsigned int get_cache_changes_tnc(
+	igd_surface_t  *src_surf,
+	igd_rect_t     *src_rect,
+	igd_rect_t     *dest_rect,
+	igd_ovl_info_t *ovl_info,
+	unsigned int    flags,
+	povl_tnc_cache_t ovl_cache);
+
+
+
+#endif /* _OVL_TNC_CACHE_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/topaz/topaz.c b/drivers/gpu/drm/emgd/emgd/video/topaz/topaz.c
new file mode 100644
index 0000000..f710ff2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/topaz/topaz.c
@@ -0,0 +1,300 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: topaz.c
+ * $Revision: 1.13 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+#include <sched.h>
+
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+#include <utils.h>
+#include <topaz.h>
+#include "topaz_hdr.h"
+
+#include <plb/regs.h>
+#include <plb/context.h>
+
+void write_mtx_mem_multiple_setup(igd_context_t *context,
+		unsigned long addr)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long bank_size;
+	unsigned long ram_size;
+	unsigned long ram_id;
+	unsigned long reg;
+	unsigned long ctrl = 0;
+
+	reg = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+
+	reg = 0x0a0a0606;
+	bank_size = (reg & 0xf0000) >> 16;
+
+	ram_size = (unsigned long) (1 << (bank_size + 2));
+	ram_id = (addr - MTX_DATA_BASE) / ram_size;
+
+	/* cmd id */
+	ctrl = ((0x18 + ram_id) << 20) & 0x0ff00000;
+	/* Address to read */
+	ctrl |= ((addr >> 2) << 2) & 0x000ffffc;
+	ctrl |= 0x02;
+	/*printk(KERN_INFO "write_mtx_multiple_setup: ctrl=0x%08x, addr=0x%08x", ctrl, addr);*/
+	EMGD_WRITE32(ctrl, mmio + TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL);
+
+	return;
+}
+
+void write_mtx_mem_multiple(igd_context_t *context,
+		unsigned long cmd)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+
+	EMGD_WRITE32(cmd, mmio + TNC_TOPAZ_MTX_RAM_ACCESS_DATA_TRANSFER);
+}
+
+/*
+ * This is the function that actually passes the message to the MTX
+ * firmware. The contents of the message are opaque to this function.
+ *
+ * Currently, the only supported message type is RENDER.
+ */
+int mtx_send_tnc(igd_context_t *context, unsigned long *msg)
+{
+	struct topaz_cmd_header *cur_cmd_header =
+			(struct topaz_cmd_header *) msg;
+	unsigned long cmd_size = cur_cmd_header->size;
+	unsigned long read_index, write_index;
+	const unsigned long *cmd_pointer = (unsigned long *)msg;
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+	int ret = 0;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+
+	write_index = topaz_priv->topaz_cmd_windex;
+	if (write_index + cmd_size + 1 > topaz_priv->topaz_ccb_size)
+	{
+		int free_space = topaz_priv->topaz_ccb_size - write_index;
+
+		EMGD_DEBUG("TOPAZ: wrap CCB write point");
+		if (free_space > 0)
+		{
+			struct topaz_cmd_header pad_cmd;
+
+			pad_cmd.id = MTX_CMDID_NULL;
+			pad_cmd.size = free_space;
+			pad_cmd.seq = 0x7fff & topaz_priv->topaz_cmd_seq++;
+
+			EMGD_DEBUG("TOPAZ: MTX_CMDID_NULL:"
+				" size(%d),seq (0x%04x)\n",
+				pad_cmd.size, pad_cmd.seq);
+
+			TOPAZ_BEGIN_CCB(context);
+			TOPAZ_OUT_CCB(context, pad_cmd.val);
+			TOPAZ_END_CCB(context, 1);
+		}
+		POLL_WB_RINDEX(context, 0);
+		if (ret == 0) {
+			topaz_priv->topaz_cmd_windex = 0;
+		} else {
+			EMGD_ERROR("TOPAZ: poll rindex timeout\n");
+			return ret; /* HW may hang, need reset */
+		}
+		EMGD_DEBUG("TOPAZ: -------wrap CCB was done.\n");
+	}
+
+	read_index = CCB_CTRL_RINDEX(context);/* temperily use CCB CTRL */
+	write_index = topaz_priv->topaz_cmd_windex;
+
+	/*printk(KERN_INFO "TOPAZ: write index(%ld), read index(%ld,WB=%ld)\n",
+		write_index, read_index, WB_CCB_CTRL_RINDEX(context));
+	printk(KERN_INFO "cmd size to kick %d",cmd_size);
+	for(count=0;count<cmd_size;count++){
+		printk(KERN_INFO "value to kick 0x%08x", *(cmd_pointer+count));
+	}*/
+
+	TOPAZ_BEGIN_CCB(context);
+	while (cmd_size > 0) {
+		TOPAZ_OUT_CCB(context, *cmd_pointer++);
+		--cmd_size;
+	}
+	TOPAZ_END_CCB(context, 1);
+
+	/*POLL_WB_RINDEX(context, topaz_priv->topaz_cmd_windex);
+	printk(KERN_INFO "RI after kick =%ld", CCB_CTRL_RINDEX(context));*/
+
+
+#if 0 /* kept for memory callback test */
+	for (i = 0; i < 100; i++) {
+		if (WB_CCB_CTRL_RINDEX(context) == topaz_priv->topaz_cmd_windex)
+			break;
+		else
+			OS_SLEEP(100);
+	}
+	if (WB_CCB_CTRL_RINDEX(context) != topaz_priv->topaz_cmd_windex) {
+		EMGD_ERROR("TOPAZ: poll rindex timeout\n");
+		ret = -IGD_ERROR_HWERROR;
+	}
+#endif
+
+	return ret;
+}
+
+void topaz_sync_tnc(igd_context_t *context)
+{
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+	unsigned long sync_cmd[3];
+	unsigned long *sync_p, temp_ret;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+	sync_p = (unsigned long *)topaz_priv->topaz_sync_addr;
+	topaz_priv->topaz_sync_id++;
+
+	/* insert a SYNC command here */
+	topaz_priv->topaz_sync_cmd_seq = (1 << 15) |
+				topaz_priv->topaz_cmd_seq++;
+	sync_cmd[0] = (MTX_CMDID_SYNC << 1) | (3 << 8) |
+		(topaz_priv->topaz_sync_cmd_seq << 16);
+	sync_cmd[1] = topaz_priv->topaz_sync_offset;
+	sync_cmd[2] = topaz_priv->topaz_sync_id;
+	temp_ret = mtx_send_tnc(context, sync_cmd);
+	if (0 != temp_ret){
+	    EMGD_DEBUG("TOPAZ: sync error: %ld\n", temp_ret);
+	}
+
+	topaz_priv->topaz_frame_skip = CCB_CTRL_FRAMESKIP(context);
+
+#if 0
+	/* debug code to make sure SYNC can be done */
+	{
+		int count = 1000;
+		while (count && *sync_p != topaz_priv->topaz_sync_id) {
+		OS_SLEEP(1000);
+		--count;
+	}
+	if ((count == 0) && (*sync_p != 0x45)) {
+		EMGD_ERROR("TOPAZ: wait sync timeout (0x%08x),"
+			"actual 0x%08x\n",
+			topaz_priv->topaz_sync_id, *sync_p);
+		}
+		}
+#endif
+
+}
+
+
+/*
+ * To process this buffer, find the MTX firmware messages and send each
+ * one to the MTX firmware.
+ */
+
+int process_encode_mtx_messages(igd_context_t *context,
+			unsigned long *mtx_buf,
+			unsigned long size)
+{
+	unsigned long *command = (unsigned long *) mtx_buf;
+	unsigned long cmd_size = size;
+	int ret = 0;
+	struct topaz_cmd_header *cur_cmd_header;
+	unsigned long cur_cmd_size, cur_cmd_id;
+	unsigned long codec;
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+
+	cur_cmd_header = (struct topaz_cmd_header *) command;
+	cur_cmd_size = cur_cmd_header->size;
+	cur_cmd_id = cur_cmd_header->id;
+
+	while (cur_cmd_id != MTX_CMDID_NULL) {
+
+		switch (cur_cmd_id) {
+			case MTX_CMDID_SW_NEW_CODEC:
+				codec = *((unsigned long *) mtx_buf + 1);
+				EMGD_DEBUG("TOPAZ: setup new codec %ld\n", codec);
+				if (topaz_setup_fw(context, codec)) {
+					EMGD_ERROR("TOPAZ: upload FW to HW failed\n");
+					return -IGD_ERROR_INVAL;
+				}
+				topaz_priv->topaz_cur_codec = codec;
+				break;
+			case MTX_CMDID_SW_ENTER_LOWPOWER:
+				EMGD_DEBUG("TOPAZ: enter lowpower.... \n");
+				EMGD_DEBUG("XXX: implement it\n");
+				break;
+			case MTX_CMDID_SW_LEAVE_LOWPOWER:
+				EMGD_DEBUG("TOPAZ: leave lowpower... \n");
+				EMGD_DEBUG("XXX: implement it\n");
+				break;
+			/* ordinary commmand */
+			case MTX_CMDID_START_PIC:
+				/* XXX: specially handle START_PIC hw command */
+				CCB_CTRL_SET_QP(context,
+					*(command + cur_cmd_size - 1));
+				/* strip the QP parameter (it's software arg) */
+				cur_cmd_header->size--;
+			default:
+				cur_cmd_header->seq = 0x7fff &
+					topaz_priv->topaz_cmd_seq++;
+				EMGD_DEBUG("TOPAZ: %ld: size(%ld), seq (0x%04x)\n",
+					cur_cmd_id, cur_cmd_size, cur_cmd_header->seq);
+				ret = mtx_send_tnc(context, command);
+				if (ret) {
+					EMGD_ERROR("TOPAZ: error -- ret(%d)\n", ret);
+					return -IGD_ERROR_INVAL;
+				}
+				break;
+			}
+        /* save frame skip flag for query */
+        /*topaz_priv->topaz_frame_skip = 0; CCB_CTRL_FRAMESKIP(context);*/
+        /* current command done */
+		command += cur_cmd_size;
+		cmd_size -= cur_cmd_size;
+
+		/* Get next command */
+		cur_cmd_header = (struct topaz_cmd_header *) command;
+		cur_cmd_size = cur_cmd_header->size;
+		cur_cmd_id = cur_cmd_header->id;
+	}
+	topaz_sync_tnc(context); 
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/emgd/video/topaz/topaz_hdr.h b/drivers/gpu/drm/emgd/emgd/video/topaz/topaz_hdr.h
new file mode 100644
index 0000000..3545684
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/topaz/topaz_hdr.h
@@ -0,0 +1,128 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: topaz_hdr.h
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef TOPAZ_HDR_H
+#define TOPAZ_HDR_H
+
+#define MTX_CCBCTRL_ROFF		0
+#define MTX_CCBCTRL_COMPLETE		4
+#define MTX_CCBCTRL_CCBSIZE		8
+#define MTX_CCBCTRL_QP			12
+#define MTX_CCBCTRL_FRAMESKIP		20
+#define MTX_CCBCTRL_INITQP		24
+
+#define TOPAZ_CTX(ctx) \
+	((platform_context_tnc_t *)(ctx)->platform_context)->tpz_private_data
+
+
+#define TOPAZ_BEGIN_CCB(ctx) \
+	write_mtx_mem_multiple_setup((ctx), \
+	TOPAZ_CTX((ctx)).topaz_ccb_buffer_addr + \
+	TOPAZ_CTX((ctx)).topaz_cmd_windex * 4)
+
+#define TOPAZ_OUT_CCB(ctx, cmd) \
+	write_mtx_mem_multiple((ctx), (cmd)); \
+	TOPAZ_CTX((ctx)).topaz_cmd_windex++;
+
+#define TOPAZ_END_CCB(ctx, kick_cnt) \
+	EMGD_WRITE32(1,  (ctx)->device_context.virt_mmadr + TNC_TOPAZ_MTX_KICK);
+
+/* macros to get/set CCB control data */
+#define WB_CCB_CTRL_RINDEX(ctx) \
+*((unsigned long *)TOPAZ_CTX(ctx).topaz_ccb_wb)
+
+#define WB_CCB_CTRL_SEQ(ctx) \
+*((unsigned long *)(TOPAZ_CTX(ctx).topaz_ccb_wb + 1))
+
+/* HACK: temporary turn off MMU bypass to POLL, then turn on
+#define POLL_WB_RINDEX(ctx, value)				\
+do {								\
+	int i;							\
+	unsigned char *mmio = ctx->device_context.virt_mmadr;   \
+	EMGD_WRITE32(0x00000000, mmio + TNC_TOPAZ_MMU_CONTROL0);\
+	for (i = 0; i < 10000; i++) {				\
+		if (CCB_CTRL_RINDEX(ctx) == value)	\
+			break;					\
+		else						\
+			OS_SLEEP(100);			\
+	}							\
+	if (CCB_CTRL_RINDEX(ctx) != value) {		\
+		EMGD_ERROR("TOPAZ: poll rindex timeout\n");	\
+		ret = -IGD_ERROR_HWERROR;					\
+	}							\
+	EMGD_WRITE32(0x00000800, mmio + TNC_TOPAZ_MMU_CONTROL0);\
+} while (0)*/
+
+#define POLL_WB_RINDEX(ctx, value)				\
+do {								\
+	int i;							\
+	for (i = 0; i < 10000; i++) {				\
+		if (CCB_CTRL_RINDEX(ctx) == value)	\
+			break;					\
+		else						\
+			OS_SLEEP(100);			\
+	}							\
+	if (CCB_CTRL_RINDEX(ctx) != value) {		\
+		EMGD_ERROR("TOPAZ: poll rindex timeout\n");	\
+		ret = -IGD_ERROR_HWERROR;					\
+	}							\
+} while (0)
+
+#define CCB_CTRL_RINDEX(ctx)			\
+	read_mtx_mem(ctx, 			\
+	TOPAZ_CTX(ctx).topaz_ccb_ctrl_addr + MTX_CCBCTRL_ROFF)
+
+#define CCB_CTRL_SET_QP(ctx, qp)				\
+	write_mtx_mem(ctx,					\
+		TOPAZ_CTX(ctx).topaz_ccb_ctrl_addr + MTX_CCBCTRL_QP, qp)
+
+#define CCB_CTRL_FRAMESKIP(ctx)				   \
+	read_mtx_mem(ctx,				   \
+	    TOPAZ_CTX(ctx).topaz_ccb_ctrl_addr + MTX_CCBCTRL_FRAMESKIP)
+
+#define CCB_CTRL_SEQ(ctx)				\
+	read_mtx_mem(ctx,				\
+	    TOPAZ_CTX(ctx).topaz_ccb_ctrl_addr + MTX_CCBCTRL_COMPLETE)
+
+
+void write_mtx_mem_multiple_setup(igd_context_t *context,
+		unsigned long addr);
+
+void write_mtx_mem_multiple(igd_context_t *context,
+		unsigned long cmd);
+
+unsigned long read_mtx_mem(igd_context_t *, unsigned long );
+
+void write_mtx_mem(igd_context_t *,
+		unsigned long ,
+		unsigned long );
+
+#endif /* TOPAZ_HDR_H */
diff --git a/drivers/gpu/drm/emgd/emgd/video/topaz/topaz_init.c b/drivers/gpu/drm/emgd/emgd/video/topaz/topaz_init.c
new file mode 100644
index 0000000..4401b69
--- /dev/null
+++ b/drivers/gpu/drm/emgd/emgd/video/topaz/topaz_init.c
@@ -0,0 +1,985 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: topaz_init.c
+ * $Revision: 1.23 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include <io.h>
+#include <pci.h>
+#include <memmap.h>
+#include <sched.h>
+
+#include <igd.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+
+#include <context.h>
+#include <intelpci.h>
+#include <general.h>
+#include <utils.h>
+#include <topaz.h>
+
+#include <plb/regs.h>
+#include <plb/context.h>
+
+#include "topaz_hdr.h"
+
+//#include <interrupt.h>
+//#include <os/interrupt.h>
+
+#include <drm/drm.h>
+#include <drm_emgd_private.h>
+
+/* DDK specific DRM device */
+extern struct drm_device *gpDrmDevice;
+
+unsigned char *base_firmware_address;
+
+extern int process_encode_mtx_messages(igd_context_t *context,
+                        unsigned long *mtx_buf,
+                        unsigned long size);
+
+static int reg_ready_tnc(igd_context_t *,
+		unsigned long ,
+		unsigned long ,
+		unsigned long );
+
+static int write_firmware(igd_context_t *,
+		unsigned long ,
+		unsigned long ,
+		unsigned long *,
+		unsigned long );
+
+static int upload_firmware(igd_context_t *, enc_fw_info_t *);
+
+void topaz_write_core_reg(igd_context_t *,
+			unsigned long,
+			unsigned long);
+
+void topaz_read_core_reg(igd_context_t *,
+			unsigned long,
+			unsigned long*);
+
+void get_mtx_control_from_dash(igd_context_t *context);
+
+void release_mtx_control_from_dash(igd_context_t *context);
+
+/* According to UMG code this define is important */
+#define RAM_SIZE (1024 * 24)
+
+/* register default values */
+static unsigned long topaz_def_regs[184][3] = {
+	{MVEA_BASE, 0x00000000, 0x00000000},
+	{MVEA_BASE, 0x00000004, 0x00000400},
+	{MVEA_BASE, 0x00000008, 0x00000000},
+	{MVEA_BASE, 0x0000000C, 0x00000000},
+	{MVEA_BASE, 0x00000010, 0x00000000},
+	{MVEA_BASE, 0x00000014, 0x00000000},
+	{MVEA_BASE, 0x00000018, 0x00000000},
+	{MVEA_BASE, 0x0000001C, 0x00000000},
+	{MVEA_BASE, 0x00000020, 0x00000120},
+	{MVEA_BASE, 0x00000024, 0x00000000},
+	{MVEA_BASE, 0x00000028, 0x00000000},
+	{MVEA_BASE, 0x00000100, 0x00000000},
+	{MVEA_BASE, 0x00000104, 0x00000000},
+	{MVEA_BASE, 0x00000108, 0x00000000},
+	{MVEA_BASE, 0x0000010C, 0x00000000},
+	{MVEA_BASE, 0x0000011C, 0x00000001},
+	{MVEA_BASE, 0x0000012C, 0x00000000},
+	{MVEA_BASE, 0x00000180, 0x00000000},
+	{MVEA_BASE, 0x00000184, 0x00000000},
+	{MVEA_BASE, 0x00000188, 0x00000000},
+	{MVEA_BASE, 0x0000018C, 0x00000000},
+	{MVEA_BASE, 0x00000190, 0x00000000},
+	{MVEA_BASE, 0x00000194, 0x00000000},
+	{MVEA_BASE, 0x00000198, 0x00000000},
+	{MVEA_BASE, 0x0000019C, 0x00000000},
+	{MVEA_BASE, 0x000001A0, 0x00000000},
+	{MVEA_BASE, 0x000001A4, 0x00000000},
+	{MVEA_BASE, 0x000001A8, 0x00000000},
+	{MVEA_BASE, 0x000001AC, 0x00000000},
+	{MVEA_BASE, 0x000001B0, 0x00000000},
+	{MVEA_BASE, 0x000001B4, 0x00000000},
+	{MVEA_BASE, 0x000001B8, 0x00000000},
+	{MVEA_BASE, 0x000001BC, 0x00000000},
+	{MVEA_BASE, 0x000001F8, 0x00000000},
+	{MVEA_BASE, 0x000001FC, 0x00000000},
+	{MVEA_BASE, 0x00000200, 0x00000000},
+	{MVEA_BASE, 0x00000204, 0x00000000},
+	{MVEA_BASE, 0x00000208, 0x00000000},
+	{MVEA_BASE, 0x0000020C, 0x00000000},
+	{MVEA_BASE, 0x00000210, 0x00000000},
+	{MVEA_BASE, 0x00000220, 0x00000001},
+	{MVEA_BASE, 0x00000224, 0x0000001F},
+	{MVEA_BASE, 0x00000228, 0x00000100},
+	{MVEA_BASE, 0x0000022C, 0x00001F00},
+	{MVEA_BASE, 0x00000230, 0x00000101},
+	{MVEA_BASE, 0x00000234, 0x00001F1F},
+	{MVEA_BASE, 0x00000238, 0x00001F01},
+	{MVEA_BASE, 0x0000023C, 0x0000011F},
+	{MVEA_BASE, 0x00000240, 0x00000200},
+	{MVEA_BASE, 0x00000244, 0x00001E00},
+	{MVEA_BASE, 0x00000248, 0x00000002},
+	{MVEA_BASE, 0x0000024C, 0x0000001E},
+	{MVEA_BASE, 0x00000250, 0x00000003},
+	{MVEA_BASE, 0x00000254, 0x0000001D},
+	{MVEA_BASE, 0x00000258, 0x00001F02},
+	{MVEA_BASE, 0x0000025C, 0x00000102},
+	{MVEA_BASE, 0x00000260, 0x0000011E},
+	{MVEA_BASE, 0x00000264, 0x00000000},
+	{MVEA_BASE, 0x00000268, 0x00000000},
+	{MVEA_BASE, 0x0000026C, 0x00000000},
+	{MVEA_BASE, 0x00000270, 0x00000000},
+	{MVEA_BASE, 0x00000274, 0x00000000},
+	{MVEA_BASE, 0x00000278, 0x00000000},
+	{MVEA_BASE, 0x00000280, 0x00008000},
+	{MVEA_BASE, 0x00000284, 0x00000000},
+	{MVEA_BASE, 0x00000288, 0x00000000},
+	{MVEA_BASE, 0x0000028C, 0x00000000},
+	{MVEA_BASE, 0x00000314, 0x00000000},
+	{MVEA_BASE, 0x00000318, 0x00000000},
+	{MVEA_BASE, 0x0000031C, 0x00000000},
+	{MVEA_BASE, 0x00000320, 0x00000000},
+	{MVEA_BASE, 0x00000324, 0x00000000},
+	{MVEA_BASE, 0x00000348, 0x00000000},
+	{MVEA_BASE, 0x00000380, 0x00000000},
+	{MVEA_BASE, 0x00000384, 0x00000000},
+	{MVEA_BASE, 0x00000388, 0x00000000},
+	{MVEA_BASE, 0x0000038C, 0x00000000},
+	{MVEA_BASE, 0x00000390, 0x00000000},
+	{MVEA_BASE, 0x00000394, 0x00000000},
+	{MVEA_BASE, 0x00000398, 0x00000000},
+	{MVEA_BASE, 0x0000039C, 0x00000000},
+	{MVEA_BASE, 0x000003A0, 0x00000000},
+	{MVEA_BASE, 0x000003A4, 0x00000000},
+	{MVEA_BASE, 0x000003A8, 0x00000000},
+	{MVEA_BASE, 0x000003B0, 0x00000000},
+	{MVEA_BASE, 0x000003B4, 0x00000000},
+	{MVEA_BASE, 0x000003B8, 0x00000000},
+	{MVEA_BASE, 0x000003BC, 0x00000000},
+	{MVEA_BASE, 0x000003D4, 0x00000000},
+	{MVEA_BASE, 0x000003D8, 0x00000000},
+	{MVEA_BASE, 0x000003DC, 0x00000000},
+	{MVEA_BASE, 0x000003E0, 0x00000000},
+	{MVEA_BASE, 0x000003E4, 0x00000000},
+	{MVEA_BASE, 0x000003EC, 0x00000000},
+	{MVEA_BASE, 0x000002D0, 0x00000000},
+	{MVEA_BASE, 0x000002D4, 0x00000000},
+	{MVEA_BASE, 0x000002D8, 0x00000000},
+	{MVEA_BASE, 0x000002DC, 0x00000000},
+	{MVEA_BASE, 0x000002E0, 0x00000000},
+	{MVEA_BASE, 0x000002E4, 0x00000000},
+	{MVEA_BASE, 0x000002E8, 0x00000000},
+	{MVEA_BASE, 0x000002EC, 0x00000000},
+	{MVEA_BASE, 0x000002F0, 0x00000000},
+	{MVEA_BASE, 0x000002F4, 0x00000000},
+	{MVEA_BASE, 0x000002F8, 0x00000000},
+	{MVEA_BASE, 0x000002FC, 0x00000000},
+	{MVEA_BASE, 0x00000300, 0x00000000},
+	{MVEA_BASE, 0x00000304, 0x00000000},
+	{MVEA_BASE, 0x00000308, 0x00000000},
+	{MVEA_BASE, 0x0000030C, 0x00000000},
+	{MVEA_BASE, 0x00000290, 0x00000000},
+	{MVEA_BASE, 0x00000294, 0x00000000},
+	{MVEA_BASE, 0x00000298, 0x00000000},
+	{MVEA_BASE, 0x0000029C, 0x00000000},
+	{MVEA_BASE, 0x000002A0, 0x00000000},
+	{MVEA_BASE, 0x000002A4, 0x00000000},
+	{MVEA_BASE, 0x000002A8, 0x00000000},
+	{MVEA_BASE, 0x000002AC, 0x00000000},
+	{MVEA_BASE, 0x000002B0, 0x00000000},
+	{MVEA_BASE, 0x000002B4, 0x00000000},
+	{MVEA_BASE, 0x000002B8, 0x00000000},
+	{MVEA_BASE, 0x000002BC, 0x00000000},
+	{MVEA_BASE, 0x000002C0, 0x00000000},
+	{MVEA_BASE, 0x000002C4, 0x00000000},
+	{MVEA_BASE, 0x000002C8, 0x00000000},
+	{MVEA_BASE, 0x000002CC, 0x00000000},
+	{MVEA_BASE, 0x00000080, 0x00000000},
+	{MVEA_BASE, 0x00000084, 0x80705700},
+	{MVEA_BASE, 0x00000088, 0x00000000},
+	{MVEA_BASE, 0x0000008C, 0x00000000},
+	{MVEA_BASE, 0x00000090, 0x00000000},
+	{MVEA_BASE, 0x00000094, 0x00000000},
+	{MVEA_BASE, 0x00000098, 0x00000000},
+	{MVEA_BASE, 0x0000009C, 0x00000000},
+	{MVEA_BASE, 0x000000A0, 0x00000000},
+	{MVEA_BASE, 0x000000A4, 0x00000000},
+	{MVEA_BASE, 0x000000A8, 0x00000000},
+	{MVEA_BASE, 0x000000AC, 0x00000000},
+	{MVEA_BASE, 0x000000B0, 0x00000000},
+	{MVEA_BASE, 0x000000B4, 0x00000000},
+	{MVEA_BASE, 0x000000B8, 0x00000000},
+	{MVEA_BASE, 0x000000BC, 0x00000000},
+	{MVEA_BASE, 0x000000C0, 0x00000000},
+	{MVEA_BASE, 0x000000C4, 0x00000000},
+	{MVEA_BASE, 0x000000C8, 0x00000000},
+	{MVEA_BASE, 0x000000CC, 0x00000000},
+	{MVEA_BASE, 0x000000D0, 0x00000000},
+	{MVEA_BASE, 0x000000D4, 0x00000000},
+	{MVEA_BASE, 0x000000D8, 0x00000000},
+	{MVEA_BASE, 0x000000DC, 0x00000000},
+	{MVEA_BASE, 0x000000E0, 0x00000000},
+	{MVEA_BASE, 0x000000E4, 0x00000000},
+	{MVEA_BASE, 0x000000E8, 0x00000000},
+	{MVEA_BASE, 0x000000EC, 0x00000000},
+	{MVEA_BASE, 0x000000F0, 0x00000000},
+	{MVEA_BASE, 0x000000F4, 0x00000000},
+	{MVEA_BASE, 0x000000F8, 0x00000000},
+	{MVEA_BASE, 0x000000FC, 0x00000000},
+	{VLC_BASE, 0x00000000, 0x00000000},
+	{VLC_BASE, 0x00000004, 0x00000000},
+	{VLC_BASE, 0x00000008, 0x00000000},
+	{VLC_BASE, 0x0000000C, 0x00000000},
+	{VLC_BASE, 0x00000010, 0x00000000},
+	{VLC_BASE, 0x00000014, 0x00000000},
+	{VLC_BASE, 0x0000001C, 0x00000000},
+	{VLC_BASE, 0x00000020, 0x00000000},
+	{VLC_BASE, 0x00000024, 0x00000000},
+	{VLC_BASE, 0x0000002C, 0x00000000},
+	{VLC_BASE, 0x00000034, 0x00000000},
+	{VLC_BASE, 0x00000038, 0x00000000},
+	{VLC_BASE, 0x0000003C, 0x00000000},
+	{VLC_BASE, 0x00000040, 0x00000000},
+	{VLC_BASE, 0x00000044, 0x00000000},
+	{VLC_BASE, 0x00000048, 0x00000000},
+	{VLC_BASE, 0x0000004C, 0x00000000},
+	{VLC_BASE, 0x00000050, 0x00000000},
+	{VLC_BASE, 0x00000054, 0x00000000},
+	{VLC_BASE, 0x00000058, 0x00000000},
+	{VLC_BASE, 0x0000005C, 0x00000000},
+	{VLC_BASE, 0x00000060, 0x00000000},
+	{VLC_BASE, 0x00000064, 0x00000000},
+	{VLC_BASE, 0x00000068, 0x00000000},
+	{VLC_BASE, 0x0000006C, 0x00000000},
+	{0xffffffff, 0xffffffff, 0xffffffff}
+};
+
+unsigned long ui32H264_MTXTOPAZFWTextSize = 3478;
+unsigned long ui32H264_MTXTOPAZFWDataSize = 4704;
+unsigned long ui32H264_MTXTOPAZFWDataLocation = 0x82883680;
+
+unsigned long ui32H264VBR_MTXTOPAZFWTextSize = 4730;
+unsigned long ui32H264VBR_MTXTOPAZFWDataSize = 3456;
+unsigned long ui32H264VBR_MTXTOPAZFWDataLocation = 0x82884a00;
+
+unsigned long ui32H264CBR_MTXTOPAZFWTextSize = 5084;
+unsigned long ui32H264CBR_MTXTOPAZFWDataSize = 3104;
+unsigned long ui32H264CBR_MTXTOPAZFWDataLocation = 0x82884f80;
+
+unsigned long ui32H263CBR_MTXTOPAZFWTextSize = 4383;
+unsigned long ui32H263CBR_MTXTOPAZFWDataSize = 3808;
+unsigned long ui32H263CBR_MTXTOPAZFWDataLocation = 0x82884480;
+
+unsigned long ui32H263VBR_MTXTOPAZFWTextSize = 4498;
+unsigned long ui32H263VBR_MTXTOPAZFWDataSize = 3680;
+unsigned long ui32H263VBR_MTXTOPAZFWDataLocation = 0x82884680;
+
+unsigned long ui32H263_MTXTOPAZFWTextSize = 3202;
+unsigned long ui32H263_MTXTOPAZFWDataSize = 4976;
+unsigned long ui32H263_MTXTOPAZFWDataLocation = 0x82883240;
+
+unsigned long ui32MPG4CBR_MTXTOPAZFWTextSize = 4403;
+unsigned long ui32MPG4CBR_MTXTOPAZFWDataSize = 3776;
+unsigned long ui32MPG4CBR_MTXTOPAZFWDataLocation = 0x82884500;
+
+unsigned long ui32MPG4VBR_MTXTOPAZFWTextSize = 4519;
+unsigned long ui32MPG4VBR_MTXTOPAZFWDataSize = 3664;
+unsigned long ui32MPG4VBR_MTXTOPAZFWDataLocation = 0x828846c0;
+
+unsigned long ui32MPG4_MTXTOPAZFWTextSize = 3223;
+unsigned long ui32MPG4_MTXTOPAZFWDataSize = 4960;
+unsigned long ui32MPG4_MTXTOPAZFWDataLocation = 0x82883280;
+
+static enc_fw_info_t firmware[10] = {
+	{ 0,0,0,0,0,0},
+	{
+		FW_H264_NO_RC,
+		&ui32H264_MTXTOPAZFWTextSize,
+		&ui32H264_MTXTOPAZFWDataSize,
+		&ui32H264_MTXTOPAZFWDataLocation,
+		NULL, /*aui32H264_MTXTOPAZFWText,*/
+		NULL /*aui32H264_MTXTOPAZFWData*/
+	},
+	{
+		FW_H264_VBR,
+		&ui32H264VBR_MTXTOPAZFWTextSize,
+		&ui32H264VBR_MTXTOPAZFWDataSize,
+		&ui32H264VBR_MTXTOPAZFWDataLocation,
+		NULL, /*aui32H264VBR_MTXTOPAZFWText,*/
+		NULL /*aui32H264VBR_MTXTOPAZFWData */
+	},
+	{
+		FW_H264_CBR,
+		&ui32H264CBR_MTXTOPAZFWTextSize,
+		&ui32H264CBR_MTXTOPAZFWDataSize,
+		&ui32H264CBR_MTXTOPAZFWDataLocation,
+		NULL, /*aui32H264CBR_MTXTOPAZFWText,*/
+		NULL /*aui32H264CBR_MTXTOPAZFWData */
+	},
+	{
+		FW_H263_NO_RC,
+		&ui32H263_MTXTOPAZFWTextSize,
+		&ui32H263_MTXTOPAZFWDataSize,
+		&ui32H263_MTXTOPAZFWDataLocation,
+		NULL, /*aui32H263_MTXTOPAZFWText,*/
+		NULL /*aui32H263_MTXTOPAZFWData*/
+	},
+	{
+		FW_H263_VBR,
+		&ui32H263VBR_MTXTOPAZFWTextSize,
+		&ui32H263VBR_MTXTOPAZFWDataSize,
+		&ui32H263VBR_MTXTOPAZFWDataLocation,
+		NULL, /*aui32H263VBR_MTXTOPAZFWText,*/
+		NULL /*aui32H263VBR_MTXTOPAZFWData*/
+	},
+	{
+		FW_H263_CBR,
+		&ui32H263CBR_MTXTOPAZFWTextSize,
+		&ui32H263CBR_MTXTOPAZFWDataSize,
+		&ui32H263CBR_MTXTOPAZFWDataLocation,
+		NULL, /*aui32H263CBR_MTXTOPAZFWText,*/
+		NULL /*aui32H263CBR_MTXTOPAZFWData*/
+	},
+	{
+		FW_MPEG4_NO_RC,
+		&ui32MPG4_MTXTOPAZFWTextSize,
+		&ui32MPG4_MTXTOPAZFWDataSize,
+		&ui32MPG4_MTXTOPAZFWDataLocation,
+		NULL, /*aui32MPG4_MTXTOPAZFWText,*/
+		NULL /*aui32MPG4_MTXTOPAZFWData*/
+	},
+	{
+		FW_MPEG4_VBR,
+		&ui32MPG4VBR_MTXTOPAZFWTextSize,
+		&ui32MPG4VBR_MTXTOPAZFWDataSize,
+		&ui32MPG4VBR_MTXTOPAZFWDataLocation,
+		NULL, /*aui32MPG4VBR_MTXTOPAZFWText,*/
+		NULL /*aui32MPG4VBR_MTXTOPAZFWData*/
+	},
+	{
+		FW_MPEG4_CBR,
+		&ui32MPG4CBR_MTXTOPAZFWTextSize,
+		&ui32MPG4CBR_MTXTOPAZFWDataSize,
+		&ui32MPG4CBR_MTXTOPAZFWDataLocation,
+		NULL, /*aui32MPG4CBR_MTXTOPAZFWText,*/
+		NULL /*aui32MPG4CBR_MTXTOPAZFWData*/
+	}
+};
+
+
+int topaz_init_tnc(unsigned long wb_offset, void *wb_addr, void *firmware_addr)
+{
+	drm_emgd_priv_t *priv;
+	igd_context_t *context;
+	unsigned char *mmio;
+	unsigned long size;
+	int i;
+	platform_context_tnc_t *platform = NULL;
+	tnc_topaz_priv_t *topaz_priv;
+	unsigned long *km_firm_addr = NULL;
+	unsigned long *firm_offset_values = NULL;
+
+	priv = gpDrmDevice->dev_private;
+	context = priv->context;
+	mmio = context->device_context.virt_mmadr;
+
+	/* Only support Atom E6xx */
+	if ((PCI_DEVICE_ID_VGA_TNC != context->device_context.did)||
+	   (context->device_context.bid == PCI_DEVICE_ID_BRIDGE_TNC_ULP)) {
+		return -1;
+	}
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+
+	topaz_priv->topaz_busy = 0;
+	topaz_priv->topaz_cmd_seq = 0;
+	topaz_priv->topaz_fw_loaded = 0;
+	topaz_priv->topaz_cur_codec = 0;
+	topaz_priv->cur_mtx_data_size = 0;
+
+	size = WRITEBACK_MEM_SIZE;
+
+	GMM_SET_DEBUG_NAME("TOPAZ Writeback Memory");
+
+	topaz_priv->topaz_wb_offset = wb_offset;
+	topaz_priv->topaz_ccb_wb = (unsigned char *)wb_addr;
+
+	/* Sync location will be half of the writeback memory. */
+	topaz_priv->topaz_sync_addr = (unsigned long *)(topaz_priv->topaz_ccb_wb + 2048);
+	topaz_priv->topaz_sync_offset = topaz_priv->topaz_wb_offset + 2048;
+	/*
+	printk(KERN_INFO "Topaz write back memory = %p", topaz_priv->topaz_ccb_wb);
+	printk(KERN_INFO "Topaz write back offset = %lx", topaz_priv->topaz_wb_offset);
+	printk(KERN_INFO "Topaz write back sync memory = %p", topaz_priv->topaz_sync_addr);
+	printk(KERN_INFO "Topaz write back sync offset = %lx", topaz_priv->topaz_sync_offset);
+	*/	
+	
+	*(topaz_priv->topaz_sync_addr) = 0; /*reset sync seq */
+	topaz_priv->topaz_sync_id = 0; /*reset sync id */
+
+	/* firmware part */
+	/* allocate memory for all firmwares */
+	/* to check, is allocate or not */
+	if ( !firmware[1].text && firmware_addr) {
+		base_firmware_address = kmalloc( 512 * 1024, GFP_KERNEL);
+		if ( NULL == base_firmware_address){
+			printk (KERN_INFO "Kernel memory allocation failed\n");
+			printk (KERN_INFO "Kernel firmware is not loaded\n");
+			return 1;
+		}
+		/* copy all firmware to kernel memory */
+		km_firm_addr = (unsigned long *) base_firmware_address;
+		firm_offset_values = km_firm_addr;
+		memcpy(km_firm_addr, firmware_addr, 512*1024);
+		/* printk(KERN_INFO "Topaz km_firm_addr = %p", km_firm_addr); */
+		/* to set firmwares */
+		/* NB! all offsets in bytes */
+		for ( i = 1; i < 10; i++){
+			firmware[i].text = km_firm_addr + (firm_offset_values[2*(i-1) + 0] >> 2) ;
+			firmware[i].data = km_firm_addr + (firm_offset_values[2*(i-1) + 1] >> 2) ;
+		}
+	}
+
+#if 0
+        /* DEBUG ONLY */
+        /* load fw here to make sure firmware can be loaded. */
+        topaz_setup_fw(context, FW_H264_NO_RC); /* just for test */
+
+#endif
+
+	return 0;
+}
+
+int topaz_setup_fw(igd_context_t *context, enum tnc_topaz_encode_fw codec)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long address, ctrl, core_id, core_rev;
+	//unsigned long reg=0;
+	int i = 0;
+	enc_fw_info_t *curr_fw;
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+
+	EMGD_WRITE32(0x00000000, mmio + TNC_TOPAZ_MMU_CONTROL0);
+
+	/* Reset MVEA
+	 * 	MVEA_SPE_SOFT_RESET 	|
+	 * 	MVEA_IPE_SOFT_RESET 	|
+	 * 	MVEA_CMPRS_SOFT_RESET 	|
+	 * 	MVEA_JMCOMP_SOFT_RESET 	|
+	 * 	MVEA_CMC_SOFT_RESET	|
+	 * 	MVEA_DCF_SOFT_RESET
+	 */
+	EMGD_WRITE32(0x0000003f, mmio + TNC_TOPAZ_IMG_MVEA_SRST);
+	EMGD_WRITE32(0x00, mmio + TNC_TOPAZ_IMG_MVEA_SRST);
+
+	/* topaz_set_default_regs */
+	/* Set default value for Video Encode resgister */
+	while (topaz_def_regs[i][0] != 0xffffffff) {
+		EMGD_WRITE32(topaz_def_regs[i][2],
+				mmio + topaz_def_regs[i][0] + topaz_def_regs[i][1]);
+		i++;
+	}
+
+	/* topaz_upload_fw */
+	/* Point to request firmware */
+	curr_fw = &firmware[codec];
+
+	upload_firmware(context, curr_fw);
+
+	/* topaz_write_core_reg */
+	/* Start the firmware thread running */
+	/*topaz_write_core_reg(context, TOPAZ_MTX_PC, PC_START_ADDRESS);*/
+
+	/* topaz_read_core_reg */
+	EMGD_WRITE32(PC_START_ADDRESS, mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_DATA);
+	EMGD_WRITE32(TOPAZ_MTX_PC, mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST);
+	reg_ready_tnc(context, TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST,
+			0x80000000, 0x80000000);
+
+	/* Enable auto clock gate for TOPAZ and MVEA
+	 * 	TOPAZ_VLC_AUTO_CLK_GATE |
+	 * 	TOPAZ_DB_AUTO_CLK_GATE  |
+	 *
+	 * 	MVEA_IPE_AUTO_CLK_GATE    |
+	 * 	MVEA_SPE_AUTO_CLK_GATE    |
+	 * 	MVEA_CMPRS_AUTO_CLK GATE  |
+	 * 	MVEA_JMCOMP_AUTO_CLK_GATE |
+	 */
+	EMGD_WRITE32(0x00000003, mmio + TNC_TOPAZ_IMG_TOPAZ_AUTO_CLK_GATE);
+	EMGD_WRITE32(0x0000000f, mmio + TNC_TOPAZ_MVEA_AUTO_CLOCK_GATING);
+
+	/* Clear the MTX interrupt */
+	EMGD_WRITE32(0x00000002, mmio + TNC_TOPAZ_IMG_TOPAZ_INTCLEAR);
+	/* Enable MTX */
+	EMGD_WRITE32(0x00000001, mmio + TNC_TOPAZ_MTX_ENABLE);
+	/* Once enable MTX fw should generate interrupt to indicate its reday */
+	reg_ready_tnc(context, TNC_TOPAZ_IMG_TOPAZ_INTSTAT,
+			0x00000002, 0x00000002);
+	/* Clear the MTX interrupt */
+	EMGD_WRITE32(0x00000002, mmio + TNC_TOPAZ_IMG_TOPAZ_INTCLEAR);
+
+	/* get ccb buffer addr - file hostutils.c */
+	topaz_priv->topaz_ccb_buffer_addr = read_mtx_mem(
+			context, MTX_DATA_BASE + RAM_SIZE - 4);
+	topaz_priv->topaz_ccb_ctrl_addr = read_mtx_mem( context,
+			MTX_DATA_BASE + RAM_SIZE - 8);
+	topaz_priv->topaz_ccb_size = read_mtx_mem( context,
+			topaz_priv->topaz_ccb_ctrl_addr + MTX_CCB_CTRL_CCB_SIZE);
+	topaz_priv->topaz_cmd_windex = 0;
+
+	/* write back the intial QP value */
+	write_mtx_mem(context,
+			topaz_priv->topaz_ccb_ctrl_addr + MTX_CCB_CTRL_INIT_QP,
+			topaz_priv->stored_initial_qp);
+	write_mtx_mem(context,
+			MTX_DATA_BASE + RAM_SIZE - 12, topaz_priv->topaz_wb_offset);
+
+	/*reg = EMGD_READ32(mmio+TNC_TOPAZ_IMG_TOPAZ_INTSTAT);
+	printk( KERN_INFO "b4 first kick: TOPAZ_INTSTAT= 0x%08x ", reg);*/
+
+	/* this kick is important to update the WB offset */
+	*((unsigned long *)topaz_priv->topaz_ccb_wb) = 0x01020304;
+	EMGD_WRITE32(1, mmio + TNC_TOPAZ_MTX_KICK);
+	OS_SLEEP(1000);
+	/* printk(KERN_INFO "Expected 0x12345678, wb return 0x%08lx",
+			*((unsigned long *)topaz_priv->topaz_ccb_wb));
+	   reg = EMGD_READ32(mmio+TNC_TOPAZ_IMG_TOPAZ_INTSTAT);
+	   printk( KERN_INFO "after first kick: TOPAZ_INTSTAT= 0x%08x ", reg);
+	   reg = EMGD_READ32(mmio+TNC_TOPAZ_MMU_STATUS);
+	   printk( KERN_INFO "sync: TOPAZ_MMU_STATUS= 0x%08x ", reg);*/
+
+	topaz_priv->topaz_busy = 0;
+	topaz_priv->topaz_cmd_seq = 0;
+
+	/* Enable MMU by pass when confguring MMU - TOPAZ_CR_MMU_BYPASS*/
+	EMGD_WRITE32(0x00000800, mmio + TNC_TOPAZ_MMU_CONTROL0);
+
+	/* Topaz MMU will point to the same PD as SGX. */
+	address = EMGD_READ32(mmio + PSB_CR_BIF_DIR_LIST_BASE1);
+	EMGD_WRITE32(address, mmio + TNC_TOPAZ_MMU_DIR_LIST_BASE0);
+	EMGD_WRITE32(0, mmio + TNC_TOPAZ_MMU_TILE0);
+
+	/*
+	 * MMU Page size = 12
+	 * MMU best count = 7
+	 * MMU ADT TTE = 0
+	 * MMU TTE threshold = 12
+	 */
+	EMGD_WRITE32(0xc070000c, mmio + TNC_TOPAZ_MMU_CONTROL1);
+	EMGD_WRITE32(0x00, mmio + TNC_TOPAZ_MMU_CONTROL0);
+
+	/* Set index register, all pointing to directory bank 0, Flush the directory
+	 * cache and disable the MMU bypass */
+	EMGD_WRITE32(0, mmio + TNC_TOPAZ_MMU_BANK_INDEX);
+	ctrl = EMGD_READ32(mmio + TNC_TOPAZ_MMU_CONTROL0) | 0x0C; /* Flush */
+	EMGD_WRITE32(ctrl, mmio + TNC_TOPAZ_MMU_CONTROL0);
+	EMGD_WRITE32(0x00, mmio + TNC_TOPAZ_MMU_CONTROL0);
+
+	core_id  = EMGD_READ32(mmio + TNC_TOPAZ_IMG_TOPAZ_CORE_ID);
+	core_rev = EMGD_READ32(mmio + TNC_TOPAZ_IMG_TOPAZ_CORE_REV);
+
+	printk(KERN_INFO "Topaz Core Id (%lx)", core_id);
+	printk(KERN_INFO "Topaz Core Revision (%lx)", core_rev);
+
+	topaz_flush_tnc(context);
+
+	return 0;
+}
+
+
+static int upload_firmware(igd_context_t *context, enc_fw_info_t *fw)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long start_addr;
+
+	EMGD_DEBUG("Encode Firmware version is %s", fw->fw_version);
+	EMGD_DEBUG("Encode Firmware enum is %d", fw->idx);
+
+	/* # refer HLD document */
+	/* # MTX reset - MTX_SOFT_RESET_MTX_RESET_MASK */
+	EMGD_WRITE32(1, mmio + TNC_TOPAZ_MTX_SOFT_RESET);
+	OS_SLEEP(6000);
+
+	/* topaz_mtx_upload_by_register */
+	EMGD_DEBUG("Writing firmware text to core memory");
+	start_addr = PC_START_ADDRESS - MTX_CODE_BASE;
+	write_firmware(context, start_addr,
+			*(fw->text_size), fw->text, MTX_CORE_CODE_MEM);
+	/* topaz_mtx_upload_by_register */
+	EMGD_DEBUG("Writing firmware data to core memory");
+	start_addr = *(fw->data_offset) - MTX_DATA_BASE;
+	write_firmware(context, start_addr,
+			*(fw->data_size), fw->data, MTX_CORE_DATA_MEM);
+	OS_SLEEP(6000);
+	return 0;
+}
+
+/* topaz_mtx_upload_by_register() */
+static int write_firmware(igd_context_t *context,
+		unsigned long address,
+		unsigned long size,
+		unsigned long *data,
+		unsigned long mtx_mem)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long ram_bank;
+	unsigned long bank_size;
+	unsigned long current_bank;
+	unsigned long ram_id;
+	unsigned long ctrl;
+	unsigned long i;
+
+	get_mtx_control_from_dash(context);
+
+	ram_bank = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+	ram_bank = 0x0a0a0606;
+	bank_size = (ram_bank & 0x000f0000) >> 16;
+	bank_size = (1 << (bank_size + 2));
+
+	/* Loop writing text/code to core memory */
+	current_bank = ~0L;
+
+	for (i = 0; i < size; i++) {
+		/* Wait for MCMSTAT to become be idle 1 */
+		if (reg_ready_tnc(context, TNC_TOPAZ_MTX_RAM_ACCESS_STATUS,
+					0x00000001, 0x00000001) == 0) {
+			ram_id = mtx_mem + (address / bank_size);
+			if (ram_id != current_bank) {
+				/*
+				 * bits 20:27    - ram bank (CODE_BASE | DATA_BASE)
+				 * bits  2:19    - address
+				 * bit   1       - enable auto increment addressing mode
+				 */
+				ctrl = (ram_id << 20) | (((address >> 2) & 0x000ffffc) << 2) |
+					0x02;
+				EMGD_WRITE32(ctrl, mmio + TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL);
+
+				current_bank = ram_id;
+			}
+
+			address +=  4;
+			EMGD_WRITE32(data[i],
+					mmio + TNC_TOPAZ_MTX_RAM_ACCESS_DATA_TRANSFER);
+		} else {
+			/* FIXME: Should we return something here */
+			printk(KERN_ERR "Timeout waiting for MCMSTAT to be idle");
+		}
+	}
+	release_mtx_control_from_dash(context);
+	return 0;
+}
+
+static int reg_ready_tnc(igd_context_t *context,
+		unsigned long reg,
+		unsigned long mask,
+		unsigned long value)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long status;
+	int poll_cnt = 1000;
+
+	while (poll_cnt) {
+		status = EMGD_READ32(mmio + reg);
+		if ((status & mask) == value) {
+			return 0;
+		}
+		poll_cnt--;
+		OS_SLEEP(100);
+	}
+
+	/* Timeout waiting for RAM ACCESS ready */
+	EMGD_DEBUG(KERN_ERR "TIMEOUT: Got 0x%08lx while waiting for 0x%08lx", status, value);
+	return 1;
+}
+
+unsigned long read_mtx_mem(igd_context_t *context, unsigned long addr)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long bank_size;
+	unsigned long ram_size;
+	unsigned long ram_id;
+	unsigned long reg;
+	unsigned long ctrl = 0;
+	unsigned long val = 0;
+
+	reg = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+
+	reg = 0x0a0a0606;
+	bank_size = (reg & 0xf0000) >> 16;
+
+	ram_size = (unsigned long) (1 << (bank_size + 2));
+	ram_id = (addr - MTX_DATA_BASE) / ram_size;
+
+	/* cmd id */
+	ctrl = ((0x18 + ram_id) << 20) & 0x0ff00000;
+	/* Address to read */
+	ctrl |= ((addr >> 2) << 2) & 0x000ffffc;
+	ctrl |= 0x00000001;
+
+	EMGD_WRITE32(ctrl, mmio + TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL);
+	reg_ready_tnc(context, TNC_TOPAZ_MTX_RAM_ACCESS_STATUS, 1, 1);
+	val = EMGD_READ32(mmio + TNC_TOPAZ_MTX_RAM_ACCESS_DATA_TRANSFER);
+
+	return val;
+}
+
+
+void write_mtx_mem(igd_context_t *context,
+		unsigned long addr, unsigned long value)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long bank_size;
+	unsigned long ram_size;
+	unsigned long ram_id;
+	unsigned long reg;
+	unsigned long ctrl = 0;
+
+	reg = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+	/*printk(KERN_INFO "write_mtx_mem: MTX_DEBUG: 0x%08x", reg);*/
+
+	reg = 0x0a0a0606;
+	bank_size = (reg & 0xf0000) >> 16;
+
+	ram_size = (unsigned long) (1 << (bank_size + 2));
+	ram_id = (addr - MTX_DATA_BASE) / ram_size;
+
+	/* cmd id */
+	ctrl = ((0x18 + ram_id) << 20) & 0x0ff00000;
+	/* Address to read */
+	ctrl |= ((addr >> 2) << 2) & 0x000ffffc;
+
+	/*printk(KERN_INFO "write_mtx_mem: ctrl=0x%08x, addr=0x%08x, value=0x%08x", ctrl, addr, value);*/
+
+	EMGD_WRITE32(ctrl, mmio + TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL);
+	EMGD_WRITE32(value, mmio + TNC_TOPAZ_MTX_RAM_ACCESS_DATA_TRANSFER);
+	reg_ready_tnc(context, TNC_TOPAZ_MTX_RAM_ACCESS_STATUS, 1, 1);
+
+	return;
+}
+
+void topaz_write_core_reg(igd_context_t *context,
+		unsigned long reg, unsigned long val)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long tmp;
+
+	get_mtx_control_from_dash(context);
+
+	/* put data into MTX_RW_DATA */
+	EMGD_WRITE32(val, mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_DATA);
+
+	/* request a write */
+	tmp = reg &
+		~TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_DREADY_MASK;
+	EMGD_WRITE32(tmp, mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST);
+
+	/* wait for operation finished */
+	reg_ready_tnc(context,
+		TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST,
+		TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_DREADY_MASK,
+		TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_DREADY_MASK);
+
+	release_mtx_control_from_dash(context);
+}
+
+void topaz_read_core_reg(igd_context_t *context,
+		unsigned long reg, unsigned long *ret_val)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long tmp;
+
+	get_mtx_control_from_dash(context);
+
+	/* request a write */
+	tmp = reg &
+		~TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_DREADY_MASK;
+	EMGD_WRITE32(tmp | TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_RNW_MASK,
+		mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST);
+
+	/* wait for operation finished */
+	reg_ready_tnc(context,
+		(unsigned long )(mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST),
+		0, TNC_TOPAZ_MTX_REGISTER_READ_WRITE_REQUEST_MTX_DREADY_MASK);
+
+	/* read */
+	*ret_val = EMGD_READ32(mmio + TNC_TOPAZ_MTX_REGISTER_READ_WRITE_DATA);
+
+	release_mtx_control_from_dash(context);
+}
+
+void get_mtx_control_from_dash(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	int debug_reg_slave_val;
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+	unsigned long reg;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+
+	reg = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+	reg &= 0xFFFFFFF0;
+	reg |= 0x00000006;
+
+	/*printk(KERN_INFO "get_mtx:debug value = 0x%08x", reg);*/
+
+	/* Get MTX control from dash
+	 * 	TOPAZ_CR_MTX_DBG_IS_SLAVE |
+	 * 	TOPAZ_CR_MTX_DBG_GPIO_OUT
+	 */
+	EMGD_WRITE32(reg, mmio + TNC_TOPAZ_MTX_DEBUG);
+
+	do {
+		debug_reg_slave_val = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+	} while ((debug_reg_slave_val & 0x18) !=0 );
+
+	topaz_priv->topaz_dash_access_ctrl = EMGD_READ32(mmio +
+				TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL);
+}
+
+void release_mtx_control_from_dash(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+	unsigned long reg;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+
+	reg = EMGD_READ32(mmio + TNC_TOPAZ_MTX_DEBUG);
+	reg &= 0xFFFFFFF0;
+	reg |= 0x00000004;
+
+	/*printk(KERN_INFO "release:debug value = 0x%08x", reg);*/
+	/* restore access control */
+	EMGD_WRITE32(topaz_priv->topaz_dash_access_ctrl,
+			mmio + TNC_TOPAZ_MTX_RAM_ACCESS_CONTROL);
+
+	/* release bus
+	 * 	TOPAZ_CR_MTX_DBG_IS_SLAVE
+	 */
+	EMGD_WRITE32(reg, mmio + TNC_TOPAZ_MTX_DEBUG);
+}
+
+int process_video_encode_tnc(igd_context_t *context, unsigned long offset, void *virt_addr, unsigned long *fence_id)
+{
+    unsigned long *mtx_buf;
+	unsigned long size=0;
+	int ret = 0;
+	platform_context_plb_t *platform;
+	tnc_topaz_priv_t *topaz_priv;
+
+	EMGD_TRACE_ENTER;
+
+	platform = (platform_context_plb_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+	mtx_buf = (unsigned long *) virt_addr;
+
+	EMGD_DEBUG("process_video_encode_tnc where buf=%p, offset=%lx\n",
+		mtx_buf, offset);
+
+	platform->topaz_busy = 1;
+	ret = process_encode_mtx_messages(context, mtx_buf, size);
+	if (ret){
+		printk(KERN_INFO "Invalid topaz encode cmd");
+	ret = -EINVAL;
+        }
+
+	*fence_id = topaz_priv->topaz_sync_id;
+	platform->topaz_busy = 0;
+	return ret;
+}
+
+int topaz_flush_tnc(igd_context_t *context)
+{
+	unsigned char *mmio = context->device_context.virt_mmadr;
+	unsigned long topaz_mmu;
+
+	/* Only support Atom E6xx */
+	if ((PCI_DEVICE_ID_VGA_TNC != context->device_context.did)||
+	   (context->device_context.bid == PCI_DEVICE_ID_BRIDGE_TNC_ULP)) {
+		return -1;
+	}
+
+	topaz_mmu = EMGD_READ32(mmio + TNC_TOPAZ_MMU_CONTROL0);
+	topaz_mmu &= ~0x800; /* turn off MMU bypass mode if is on*/
+	topaz_mmu |= 0x0C;      /* MMU_INVALDC + MMU_FLUSH */
+	EMGD_WRITE32(topaz_mmu, mmio + TNC_TOPAZ_MMU_CONTROL0);
+
+	topaz_mmu &= ~0x0C;
+	EMGD_WRITE32(topaz_mmu, mmio + TNC_TOPAZ_MMU_CONTROL0);
+
+#if 0
+	unsigned long reg;
+
+	topaz_mmu = EMGD_READ32(mmio + TNC_TOPAZ_MMU_CONTROL0);
+	printk(KERN_INFO "topaz_flush_tnc: CONTROL0=0x%08x", topaz_mmu);
+	reg = EMGD_READ32(mmio+TNC_TOPAZ_IMG_TOPAZ_INTSTAT);
+	printk( KERN_INFO "topaz_flush_tnc: TOPAZ_INTSTAT= 0x%08x ", reg);
+	reg = EMGD_READ32(mmio+TNC_TOPAZ_MMU_STATUS);
+	printk( KERN_INFO "topaz_flush_tnc: MMU_STATUS= 0x%08x ", reg);
+#endif
+
+	return 0;
+}
+
+int topaz_get_frame_skip(igd_context_t *context, unsigned long *frame_skip)
+{
+	tnc_topaz_priv_t *topaz_priv;
+	platform_context_tnc_t *platform;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+	*frame_skip = topaz_priv->topaz_frame_skip;
+	/* printk(KERN_INFO "call frame skip with return %ld", *frame_skip);*/
+
+	return 0;
+}
+
+int topaz_get_fence_id(igd_context_t *context, unsigned long *fence_id)
+{
+	tnc_topaz_priv_t *topaz_priv;
+	unsigned long *sync_p;
+	platform_context_tnc_t *platform;
+
+	platform = (platform_context_tnc_t *)context->platform_context;
+	topaz_priv = &platform->tpz_private_data;
+	sync_p = (unsigned long *)topaz_priv->topaz_sync_addr;
+
+	*fence_id = *sync_p;
+
+	return 0;
+}
diff --git a/drivers/gpu/drm/emgd/include/emgd_drm.h b/drivers/gpu/drm/emgd/include/emgd_drm.h
new file mode 100644
index 0000000..c2b871a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/emgd_drm.h
@@ -0,0 +1,916 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_drm.h
+ * $Revision: 1.63 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+#ifndef _EMGD_DRM_H_
+#define _EMGD_DRM_H_
+
+#include <drm/drm.h>
+#include <igd.h>
+#include <igd_appcontext.h>
+#include <igd_errno.h>
+#include <igd_init.h>
+#include <igd_mode.h>
+#include <igd_ovl.h>
+#include <igd_pd.h>
+#include <igd_pi.h>
+#include <igd_pwr.h>
+#include <igd_version.h>
+
+enum {
+	CHIP_PSB_8108 = 0,
+	CHIP_PSB_8109 = 1,
+	CHIP_TC_4108 = 2
+};
+
+/* To differentiate different engines for command buffer submission */
+#define PSB_ENGINE_2D 0
+#define PSB_ENGINE_VIDEO 1
+#define PSB_ENGINE_RASTERIZER 2
+#define PSB_ENGINE_TA 3
+#define PSB_ENGINE_HPRAST 4
+#define TNC_ENGINE_ENCODE 5
+#define PSB_ENGINE_COMPOSITOR_MMU 6
+
+
+/* OEM flags for buffer allocation through the PVR2DCreateFlipChain */
+#ifndef PVR2D_CREATE_FLIPCHAIN_SHARED
+#define PVR2D_CREATE_FLIPCHAIN_SHARED		(1UL << 0)
+#define PVR2D_CREATE_FLIPCHAIN_QUERY			(1UL << 1)
+#define PVR2D_CREATE_FLIPCHAIN_OEMOVERLAY	(1UL << 2)
+#define PVR2D_CREATE_FLIPCHAIN_AS_BLITCHAIN	(1UL << 3)
+#endif
+
+#define PVR2D_CREATE_FLIPCHAIN_OEMDISPLAY   (1UL << 4)
+#define PVR2D_CREATE_FLIPCHAIN_OEMGENERAL   (1UL << 5)
+#define PVR2D_CREATE_FLIPCHAIN_OEMFLIPCHAIN (1UL << 6)
+#define PVR2D_CREATE_FLIPCHAIN_CI (1UL << 7)
+#define PVR2D_CREATE_FLIPCHAIN_CI_V4L2_MAP (1UL << 8)
+/* Different command */
+#define CMD_VIDEO_STATE           1
+#define CMD_VIDEO_INITIALIZE      2
+#define CMD_VIDEO_UNINITIALIZE    3
+#define CMD_VIDEO_CLOSE_CONTEXT	  4
+#define CMD_VIDEO_CREATE_CONTEXT  5
+#define CMD_VIDEO_SHUTDOWN        6
+#define CMD_VIDEO_GET_FENCE_ID    7
+#define CMD_VIDOE_GET_FRAME_SKIP  8
+#define CMD_VIDEO_GET_MSVDX_STATUS  	  9
+
+/* Video state */
+#define VIDEO_STATE_FW_LOADED     	0x00000001
+#define VIDEO_STATE_RENDEC_FREED        0x00000002
+
+/* Different command for alter_ovl2 */
+#define CMD_ALTER_OVL2			1
+#define CMD_ALTER_OVL2_OSD		2
+
+typedef enum _kdrm_test_pvrsrv_opcodes {
+	SINGLE_DC_SWAPING        = 0,
+	DIH_DC_DRAW_OPEN_DEV1    = 1,
+	DIH_DC_MODE_CHANGE1_DEV1 = 2,
+	DIH_DC_MODE_CHANGE2_DEV1 = 3,
+	DIH_DC_CLOSE_DEV1        = 4,
+	DIH_DC_DRAW_OPEN_DEV2    = 5,
+	DIH_DC_MODE_CHANGE1_DEV2 = 6,
+	DIH_DC_MODE_CHANGE2_DEV2 = 7,
+	DIH_DC_CLOSE_DEV2        = 8,
+} emgd_drm_test_pvrsrv_opcodes_t;
+
+
+/*
+ * This is where all the data structures used by the Koheo DRM interface are
+ * defined.  These data structures are shared between and are used to pass data
+ * between the user-space & kernel-space code for each ioctl.
+ *
+ * The naming convention is:  emgd_drm_<HAL-procedure-pointer-name>_t
+ */
+
+typedef struct _kdrm_alter_cursor {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	igd_cursor_info_t cursor_info; /* (DOWN) */
+	/**
+	 * A pointer to the cursor data (in user space), which must be copied
+	 * separately from this struct.
+	 */
+	unsigned char *image; /* (DOWN) */
+} emgd_drm_alter_cursor_t;
+
+
+typedef struct _kdrm_alter_cursor_pos {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	igd_cursor_info_t cursor_info; /* (DOWN) */
+} emgd_drm_alter_cursor_pos_t;
+
+
+typedef struct _kdrm_alter_displays {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_info_t primary_pt_info; /* (DOWN) */
+	igd_display_info_t secondary_pt_info; /* (DOWN) */
+	igd_framebuffer_info_t primary_fb_info; /* (DOWN) */
+	igd_framebuffer_info_t secondary_fb_info; /* (DOWN) */
+	unsigned long dc; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+
+	igd_display_h primary; /* (UP) Generated "opaque handle" */
+	igd_display_h secondary; /* (UP) Generated "opaque handle" */
+} emgd_drm_alter_displays_t;
+
+
+typedef struct _kdrm_alter_ovl {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	igd_appcontext_h appcontext_h; /* (DOWN) a handle */
+	/* NOTE: igd_surface_t contains a pointer to an igd_palette_info_t struct
+	 * (member name "palette_info"), which contains a pointer to an "unsigned
+	 * long" "palette".  Normally, each of these values must be copied from
+	 * user to kernel space.  However, an investigation found that the
+	 * igd_palette_info_t struct is not used for either alter_ovl() or
+	 * set_surface() (and igd_get_surface_plb() sets the value to 0,
+	 * a.k.a. NULL).  Thus, we're okay not copying this value.
+	 */
+	igd_surface_t src_surf; /* (DOWN) */
+	igd_rect_t src_rect; /* (DOWN) */
+	igd_rect_t dst_rect; /* (DOWN) */
+	igd_ovl_info_t ovl_info; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_alter_ovl_t;
+
+typedef struct _kdrm_alter_ovl2 {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	igd_surface_t src_surf; /* (DOWN) */
+	igd_rect_t src_rect; /* (DOWN) */
+	igd_rect_t dst_rect; /* (DOWN) */
+	igd_ovl_info_t ovl_info; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+	int cmd; /* (DOWN) */
+} emgd_drm_alter_ovl2_t;
+
+
+typedef struct _kdrm_get_ovl_init_params {
+    ovl_um_context_t *ovl_um_context;
+    int rtn; /* (UP) - return value of HAL procedure */
+} emgd_drm_get_ovl_init_params_t;
+
+
+typedef struct _kdrm_appcontext_alloc {
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	int priority; /* (DOWN) */
+	unsigned int flags; /* (DOWN) */
+	igd_appcontext_h appcontext_h; /* (UP) a handle */
+} emgd_drm_appcontext_alloc_t;
+
+
+typedef struct _kdrm_appcontext_free {
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	int priority; /* (DOWN) */
+	igd_appcontext_h appcontext_h; /* (DOWN) a handle */
+} emgd_drm_appcontext_free_t;
+
+
+typedef struct _kdrm_driver_save_restore {
+	int rtn; /* (UP) - return value of HAL procedure */
+} emgd_drm_driver_save_restore_t;
+
+
+typedef struct _kdrm_enable_port {
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	unsigned long flag; /* (DOWN) */
+	unsigned long test; /* (DOWN) */
+} emgd_drm_enable_port_t;
+
+
+typedef struct _kdrm_get_attrs {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	/** The number of attributes of memory allocated by user-space. */
+	unsigned int allocated_size; /* (DOWN) */
+	/** The number of attributes returned by the kernel. */
+	unsigned long list_size; /* (UP) */
+	/**
+	 * A pointer to the user-space-allocated memory for the kernel to copy the
+	 * returned attribute list into.  allocated_size is how many attributes
+	 * this memory can contain.  list_size is how many attributes the kernel is
+	 * returning (or wants to return).  If list_size > allocated_size, no data
+	 * is copied back to user-space, and the user-space must re-allocate
+	 * list_size amount of memory and try the ioctl again.
+	 */
+	igd_attr_t *attr_list; /* (UP) */
+	int extended; /* (DOWN/UP) - true if there are/to get extended attributes */
+} emgd_drm_get_attrs_t;
+
+#define IGD_GET_DISPLAY_NO_3DD_REINIT 	0x1
+
+typedef struct _kdrm_get_display {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	unsigned short port_number; /* (DOWN) */
+	igd_framebuffer_info_t fb_info; /* (DOWN/UP) Caller alloc's; DRM returns */
+	igd_display_info_t pt_info; /* (DOWN/UP) Caller alloc's; DRM returns */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_get_display_t;
+
+
+typedef struct _kdrm_get_drm_config {
+	int rtn; /* (UP) - return value of HAL procedure */
+	/** Which configuration to use within "xorg.conf" */
+	long config_id; /* (UP) */
+	/** The set of compile-time parameters, specified in "user_config.c". */
+	igd_param_t params; /* (UP) */
+	/** The display config (e.g. 8 for DIH). */
+	int display_config; /* (UP) */
+	/*
+	 * Build configuration (e.g., DDK version used, debug vs release, etc.);
+	 * if these don't match what userspace was built with, the driver may not
+	 * run properly.  (UP)
+	 */
+	igd_build_config_t build_config;
+} emgd_drm_get_drm_config_t;
+
+
+typedef struct _kdrm_get_EDID_block {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	unsigned char block_number; /* (DOWN) */
+	unsigned char edid_block[128]; /* (UP) */
+} emgd_drm_get_EDID_block_t;
+
+
+typedef struct _kdrm_get_EDID_info {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	unsigned char edid_version; /* (UP) */
+	unsigned char edid_revision; /* (UP) */
+	unsigned long edid_size; /* (UP) */
+} emgd_drm_get_EDID_info_t;
+
+
+/*!
+ * This enum is used to state which of the 5 pixel format lists is desired to
+ * be returned in the emgd_drm_get_pixelformats_t struct (which only transfers
+ * one list).
+ */
+typedef enum _kdrm_which_pixelformat {
+	PIXEL_FORMAT_FRAMEBUFFER,
+	PIXEL_FORMAT_CURSOR,
+	PIXEL_FORMAT_OVERLAY,
+	PIXEL_FORMAT_RENDER,
+	PIXEL_FORMAT_TEXTURE
+} emgd_drm_which_pixelformat_t;
+
+/*!
+ * The igd_dispatch_t.get_pixelformats() procedure returns up-to 5 different
+ * lists of pixel formats.  However, for Koheo drivers, only one of these lists
+ * is ever requested/returned.  Thus, only one list needs to be passed between
+ * user/kernel space.  Memory for the list is allocated in user space, and a
+ * pointer to it, along with its size and which format is desired, is passed to
+ * the kernel in this struct.
+ */
+typedef struct _kdrm_get_pixelformats {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	emgd_drm_which_pixelformat_t format; /* (DOWN) */
+	/** The number of pixelformats of memory allocated by user-space. */
+	unsigned int allocated_size; /* (DOWN) */
+	/** The number of pixelformats returned by the kernel. */
+	unsigned int list_size; /* (UP) */
+	unsigned long *format_list; /* (DOWN/UP) */
+} emgd_drm_get_pixelformats_t;
+
+
+typedef struct _kdrm_get_port_info {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	igd_port_info_t port_info; /* (UP) */
+} emgd_drm_get_port_info_t;
+
+
+typedef struct _kdrm_gmm_alloc_region {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long offset; /* (UP) */
+	unsigned long size; /* (DOWN/UP) */
+	unsigned int type; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_gmm_alloc_region_t;
+
+
+typedef struct _kdrm_gmm_alloc_surface {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long offset; /* (UP) */
+	unsigned long pixel_format; /* (DOWN) */
+	unsigned int width; /* (DOWN/UP) */
+	unsigned int height; /* (DOWN/UP) */
+	unsigned int pitch; /* (UP) */
+	unsigned long size; /* (DOWN/UP) */
+	unsigned int type; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_gmm_alloc_surface_t;
+
+
+typedef struct _kdrm_gmm_get_num_surface {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long count; /* (UP) */
+} emgd_drm_gmm_get_num_surface_t;
+
+
+typedef struct _kdrm_gmm_get_surface_list {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long allocated_size; /* (DOWN) */
+	unsigned long list_size; /* (UP) */
+	igd_surface_list_t **surface_list; /* (UP) */
+} emgd_drm_gmm_get_surface_list_t;
+
+
+typedef struct _kdrm_gmm_free {
+	unsigned long offset; /* (DOWN) */
+} emgd_drm_gmm_free_t;
+
+
+typedef struct _kdrm_gmm_flush_cache {
+	int rtn; /* (UP) - return value of HAL procedure */
+} emgd_drm_gmm_flush_cache_t;
+
+typedef struct _kdrm_video_cmd_buf {
+	int rtn;
+	int engine;
+	unsigned long offset;
+	void *kernel_virt_addr;
+	unsigned long fence_id;
+} emgd_drm_video_cmd_buf_t;
+
+typedef struct _kdrm_device_info {
+	unsigned long device_id;
+	unsigned long revision_id;
+	unsigned short bridge_id;
+	unsigned long display_memory_size;
+	unsigned long display_memory_offset;
+} emgd_drm_device_info_t;
+
+typedef struct _kdrm_init_video {
+	int rtn;
+	int cmd;
+	int engine;
+	union {
+		struct {
+			unsigned long base0;
+			unsigned long base1;
+			void *fw_priv;
+			unsigned long fw_size;
+		};
+		struct {
+			unsigned long wb_offset;
+			void *wb_addr;
+			void *firm_addr;
+		};
+		unsigned long status;
+	};
+} emgd_drm_init_video_t;
+
+typedef struct _kdrm_video_get_info {
+	int rtn;
+	int engine;
+	int cmd;
+	int last_frame;
+	unsigned long fence_id;
+	unsigned long frame_skip;
+	unsigned long queue_status; /* (UP) - return 1 if msvdx queue empty, else 0 */
+	unsigned long mtx_msg_status; /* (UP) - return 1 if msvdx current message complete, else 0 */
+} emgd_drm_video_get_info_t;
+
+typedef struct _kdrm_video_flush_tlb {
+	int rtn;
+	int engine;
+} emgd_drm_video_flush_tlb_t;
+
+typedef struct _kdrm_preinit_mmu {
+	int rtn;
+	unsigned long memcontext;
+} emgd_drm_preinit_mmu_t;
+
+typedef struct _kdrm_get_display_info {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_info_t primary_pt_info; /* (UP) */
+	igd_display_info_t secondary_pt_info; /* (UP) */
+	igd_framebuffer_info_t primary_fb_info; /* (UP) */
+	igd_framebuffer_info_t secondary_fb_info; /* (UP) */
+	unsigned long dc; /* (UP) */
+	unsigned long flags; /* (UP) */
+
+	igd_display_h primary; /* (UP) Generated "opaque handle" */
+	igd_display_h secondary; /* (UP) Generated "opaque handle" */
+} emgd_drm_get_display_info_t;
+
+typedef struct _kdrm_pan_display {
+	/* Note: the return value is a long this time: */
+	long rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	unsigned long x_offset; /* (DOWN) */
+	unsigned long y_offset; /* (DOWN) */
+} emgd_drm_pan_display_t;
+
+
+typedef struct _kdrm_power_display {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	unsigned long power_state;  /* (DOWN) */
+} emgd_drm_power_display_t;
+
+
+typedef struct _kdrm_pwr_alter {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned int power_state; /* (DOWN) */
+} emgd_drm_pwr_alter_t;
+
+
+/*!
+ * Memory for the list is allocated in user space, and a pointer to it, along
+ * with its size, is passed to the kernel in this struct.
+ */
+typedef struct _kdrm_query_dc {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long request; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+	/** The number of DCs of memory allocated by user-space. */
+	unsigned int allocated_size; /* (DOWN) */
+	/** The number of DCs returned by the kernel. */
+	unsigned int list_size; /* (UP) */
+	unsigned long *dc_list; /* (UP) */
+} emgd_drm_query_dc_t;
+
+
+typedef struct _kdrm_query_max_size_ovl {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	unsigned long pf; /* (DOWN) */
+	unsigned int max_width; /* (UP) */
+	unsigned int max_height; /* (UP) */
+} emgd_drm_query_max_size_ovl_t;
+
+
+typedef struct _kdrm_query_ovl {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_query_ovl_t;
+
+
+typedef struct _kdrm_query_mode_list {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long dc; /* (DOWN) */
+	unsigned long flags; /* (DOWN) */
+	/** The number of attributes of memory allocated by user-space. */
+	unsigned int allocated_size; /* (DOWN) */
+	/** The number of attributes returned by the kernel. */
+	unsigned int list_size; /* (UP) */
+	igd_display_info_t *mode_list; /* (DOWN/UP) */
+} emgd_drm_query_mode_list_t;
+
+
+typedef struct _kdrm_get_golden_htotal {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_info_t *in_mode; /* (DOWN/UP) */
+	igd_display_info_t *out_mode; /* (DOWN/UP) */
+} emgd_drm_get_golden_htotal_t;
+
+typedef struct _kdrm_set_attrs {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned short port_number; /* (DOWN) */
+	/** The number of attributes sent to the kernel. */
+	unsigned int list_size; /* (DOWN) */
+	/**
+	 * Note: the attribute list is variable-sized data, but an ioctl must use a
+	 * fixed-sized struct.  Thus, this struct contains both a pointer to the
+	 * attribute list (in user space, which must be copied separately from this
+	 * struct) and the number of attibutes (above).
+	 */
+	igd_attr_t *attr_list; /* (DOWN) */
+} emgd_drm_set_attrs_t;
+
+
+typedef struct _kdrm_set_palette_entry {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	unsigned long palette_entry; /* (DOWN) */
+	unsigned long palette_color; /* (DOWN) */
+} emgd_drm_set_palette_entry_t;
+
+
+typedef struct _kdrm_set_surface {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	int priority; /* (DOWN) */
+	igd_buffertype_t type; /* (DOWN) */
+	/* NOTE: igd_surface_t contains a pointer to an igd_palette_info_t struct:
+	 * see the description above, in the typedef emgd_drm_alter_ovl_t.
+	 */
+	igd_surface_t surface; /* (DOWN) */
+	igd_appcontext_h appcontext; /* (DOWN) a handle */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_set_surface_t;
+
+#define PRIMARY_DISPLAY 		0
+#define SECONDARY_DISPLAY 		1
+#define CLONE_PRIMARY 			PRIMARY_DISPLAY
+#define CLONE_SECONDARY 		SECONDARY_DISPLAY
+#define CLONE 				0
+#define DIH   				1
+
+#define DUAL_SCREEN_MAX_DISPLAY 	2
+#define MAX_FFB_SURF_VEXT 		DUAL_SCREEN_MAX_DISPLAY
+#define MAX_FFB_SURF_DIH 		1
+
+typedef struct _kdrm_dihclone_set_surface {
+	int rtn; /* (UP) - return value of HAL procedure */
+	unsigned long dih_clone_display; /* (DOWN) - primary or secondary display to clone */
+	unsigned long mode; /* (DOWN) - dih to clone or back to dih */
+} emgd_drm_dihclone_set_surface_t;
+
+typedef struct _kdrm_control_plane_format {
+	/* Note on modification to the structure to accomodate both 
+	 * Use PRIMARY/SECONDARY to indicate which display
+	 * FB blend + overlay to turn ON/OFF. The relationship between 
+	 * plane, pipe and port is transparent to the user.
+	 * A qualifier (use_plane) is used to decide which model the user wants. 
+	 */
+	int rtn; /* (UP) - return value of HAL procedure */
+	/* 	(DOWN) Turn off transparency by switching to XRGB format = 0
+		Turn on transparency by switching to ARGB format = 1 */
+	int enable; 
+	union {
+		/* 	(DOWN) Plane A = 0
+			Plane B = 1 */
+		int display_plane;
+		/* 	(DOWN) Primary/Secondary display handle */
+		igd_display_h primary_secondary_dsp;
+	};
+	/* If set, KMD will use the plane convention */
+	unsigned int use_plane;
+} emgd_drm_control_plane_format_t;
+
+
+typedef struct _kdrm_set_overlay_display{
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h ovl_display[OVL_MAX_HW]; /* (DOWN) Overlay display handles */
+} emgd_drm_set_overlay_display_t;
+
+
+typedef struct _kdrm_sync {
+	int rtn; /* (UP) - return value of HAL procedure */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	int priority; /* (DOWN) */
+	unsigned long in_sync; /* (DOWN/UP) */
+	unsigned long flags; /* (DOWN) */
+} emgd_drm_sync_t;
+
+
+typedef struct _kdrm_driver_pre_init {
+	int rtn; /* (UP) - return value of HAL procedure */
+	/**
+	 * The set of user-specified parameters, parsed at X server PreInit() time.
+	 */
+	igd_param_t params; /* (DOWN) */
+	/**
+	 * The results of the HAL's igd_get_config_info() procedure.  The
+	 * user-space code allocates memory for this.
+	 */
+	igd_config_info_t config_info; /* (UP) */
+	/* this is passed in by intelPreInit */
+	unsigned long bpp;
+} emgd_drm_driver_pre_init_t;
+
+
+typedef struct _kdrm_driver_get_ports {
+	int rtn; /* (UP) - return value of HAL procedure */
+	/** An array of ports discovered by the device-specific HAL driver. */
+	unsigned long ports[IGD_MAX_PORTS]; /* (UP) */
+} emgd_drm_driver_get_ports_t;
+
+
+typedef struct _kdrm_driver_get_chipset_info {
+	unsigned long device_id;
+	unsigned long revision_id;
+	char name[40];
+	char description[20];
+} emgd_drm_driver_get_chipset_info_t;
+
+
+typedef struct _kdrm_get_page_list {
+	int rtn; /* (UP) - return value of kernel-mode test code() */
+	igd_display_h display_handle; /* (DOWN) - an "opaque handle" */
+	unsigned long offset; /* (DOWN) - surface offset (ID) */
+	unsigned long addr_count; /* (DOWN) - size of address list */
+	unsigned long *addr_list; /* (UP) */
+} emgd_drm_get_page_list_t;
+
+typedef struct _kdrm_start_pvrsrv {
+	int xserver; /* (DOWN) - 1 if called by X server, else 0 */
+	int rtn; /* (UP) - return value of PVRSRVDrmLoad() */
+} emgd_drm_start_pvrsrv_t;
+
+typedef struct _kdrm_test_pvrsrv {
+	emgd_drm_test_pvrsrv_opcodes_t opcode; /* (DOWN) */
+	int rtn; /* (UP) - return value of kernel-mode test code() */
+	char rtn_msg[1024]; /* (UP) */
+} emgd_drm_test_pvrsrv_t;
+
+
+typedef struct _kdrm_query_2d_caps_hwhint {
+	unsigned long caps_val; /* (DOWN) */
+	unsigned long *status; /* (UP) */
+} emgd_drm_query_2d_caps_hwhint_t;
+
+/* For Buffer Class FCB #17711*/
+typedef struct _kdrm_bc_ts {
+	int rtn;
+	int width;
+	int height;
+	int stride;
+	int is_continous;
+	unsigned long dev_id;
+	unsigned long buf_id;
+	unsigned long buf_tag;
+	unsigned long num_buf;	
+	unsigned long pixel_format;
+	unsigned long phyaddr;
+	unsigned long virtaddr;
+} emgd_drm_bc_ts_t;
+
+/*
+ * This is where all the IOCTL's used by the egd DRM interface are
+ * defined.  This information is shared between the user space code and
+ * the kernel module.
+ */
+
+#define BASE DRM_COMMAND_BASE
+
+/*
+ * EMGD-specific ioctls.  These get mapped to the device specific range
+ * between 0x40 and 0x79.
+ *
+ * Client driver must use these values!
+ */
+#define DRM_IGD_ALTER_CURSOR         0x00
+#define DRM_IGD_ALTER_CURSOR_POS     0x01
+#define DRM_IGD_ALTER_DISPLAYS       0x02
+#define DRM_IGD_ALTER_OVL            0x03
+#define DRM_IGD_APPCTX_ALLOC         0x04
+#define DRM_IGD_APPCTX_FREE          0x05
+#define DRM_IGD_DRIVER_SAVE_RESTORE  0x06
+#define DRM_IGD_ENABLE_PORT          0x07
+#define DRM_IGD_GET_ATTRS            0x08
+#define DRM_IGD_GET_DISPLAY          0x09
+#define DRM_IGD_GET_EDID_BLOCK       0x0a
+#define DRM_IGD_GET_EDID_INFO        0x0b
+#define DRM_IGD_GET_PIXELFORMATS     0x0c
+#define DRM_IGD_GET_PORT_INFO        0x0d
+#define DRM_IGD_GMM_ALLOC_REGION     0x0e
+#define DRM_IGD_GMM_ALLOC_SURFACE    0x0f
+#define DRM_IGD_GMM_FREE             0x10
+#define DRM_IGD_GMM_FLUSH_CACHE      0x11
+#define DRM_IGD_GMM_GET_NUM_SURFACE  0x31
+#define DRM_IGD_GMM_GET_SURFACE_LIST 0x32
+#define DRM_IGD_GET_GOLDEN_HTOTAL    0x33
+#define DRM_IGD_CONTROL_PLANE_FORMAT 0x34
+#define DRM_IGD_QUERY_2D_CAPS_HWHINT 0x35
+#define DRM_IGD_DIHCLONE_SET_SURFACE 0x36
+#define DRM_IGD_SET_OVERLAY_DISPLAY  0x37
+#define DRM_IGD_WAIT_VBLANK			 0x40
+
+/*
+ * The EMGD DRM includes the PVR DRM, and as such, includes the following PVR
+ * DRM ioctls.  The numbering must be kept in sync with what is defined in
+ * "emgd_shared.h":
+ */
+#define DRM_IGD_RESERVED_1          DRM_PVR_RESERVED1 /* Reserved */
+#define DRM_IGD_RESERVED_2          DRM_PVR_RESERVED2 /* Reserved */
+#define DRM_IGD_RESERVED_3          DRM_PVR_RESERVED3 /* Reserved */
+#define DRM_IGD_RESERVED_4          DRM_PVR_RESERVED4 /* Reserved */
+#define DRM_IGD_RESERVED_5          DRM_PVR_RESERVED5 /* Reserved */
+#define DRM_IGD_PAN_DISPLAY         0x17
+#define DRM_IGD_POWER_DISPLAY       0x18
+#define DRM_IGD_PWR_ALTER           0x19
+#define DRM_IGD_QUERY_DC            0x1a
+#define DRM_IGD_QUERY_MAX_SIZE_OVL  0x1b
+#define DRM_IGD_QUERY_OVL           0x1c
+#define DRM_IGD_QUERY_MODE_LIST     0x1d
+/*
+ * Another PVR DRM ioctl that must be kept in sync with what is defined in
+ * "emgd_shared.h":
+ */
+#define DRM_IGD_RESERVED_6          DRM_PVR_RESERVED6 /* Reserved */
+#define DRM_IGD_SET_ATTRS           0x1f
+#define DRM_IGD_SET_PALETTE_ENTRY   0x20
+#define DRM_IGD_SET_SURFACE         0x21
+#define DRM_IGD_SYNC                0x22
+#define DRM_IGD_DRIVER_PRE_INIT     0x23
+#define DRM_IGD_DRIVER_GET_PORTS    0x24
+#define DRM_IGD_START_PVRSRV        0x25
+#define DRM_IGD_TEST_PVRSRV         0x26
+/* For Video (MSVDX/TOPAZ) */
+#define DRM_IGD_VIDEO_CMD_BUF       0x27
+#define DRM_IGD_GET_DEVICE_INFO     0x28
+#define DRM_IGD_INIT_VIDEO          0x29
+#define DRM_IGD_VIDEO_GET_INFO      0x2a
+#define DRM_IGD_VIDEO_FLUSH_TLB     0x2b
+/* For X driver */
+#define DRM_IGD_GET_DRM_CONFIG      0x2c
+#define DRM_IGD_GET_PAGE_LIST       0x2d
+#define DRM_IGD_GET_OVL_INIT_PARAMS 0x2e
+#define DRM_IGD_ALTER_OVL2          0x2f
+#define DRM_IGD_GET_CHIPSET_INFO    0x30
+#define DRM_IGD_GET_DISPLAY_INFO    0x38
+#define DRM_IGD_PREINIT_MMU         0x39
+/* For Buffer Class of Texture Stream */
+#define DRM_IGD_BC_TS_INIT			0x40
+#define DRM_IGD_BC_TS_UNINIT		0x41
+#define DRM_IGD_BC_TS_REQUEST_BUFFERS	0x42
+#define DRM_IGD_BC_TS_RELEASE_BUFFERS	0x43
+#define DRM_IGD_BC_TS_SET_BUFFER_INFO	0x44
+#define DRM_IGD_BC_TS_GET_BUFFERS_COUNT	0x45
+#define DRM_IGD_BC_TS_GET_BUFFER_INDEX	0x46
+
+/*
+ * egd IOCTLs.
+ */
+#define DRM_IOCTL_IGD_ALTER_CURSOR     DRM_IOWR(DRM_IGD_ALTER_CURSOR + BASE,\
+		emgd_drm_alter_cursor_t)
+#define DRM_IOCTL_IGD_ALTER_CURSOR_POS DRM_IOWR(DRM_IGD_ALTER_CURSOR_POS +\
+		BASE,\
+		emgd_drm_alter_cursor_pos_t)
+#define DRM_IOCTL_IGD_ALTER_DISPLAYS   DRM_IOWR(DRM_IGD_ALTER_DISPLAYS + BASE,\
+		emgd_drm_alter_displays_t)
+#define DRM_IOCTL_IGD_ALTER_OVL        DRM_IOWR(DRM_IGD_ALTER_OVL + BASE,\
+		emgd_drm_alter_ovl_t)
+#define DRM_IOCTL_IGD_ALTER_OVL2        DRM_IOWR(DRM_IGD_ALTER_OVL2 + BASE,\
+		emgd_drm_alter_ovl2_t)
+#define DRM_IOCTL_IGD_APPCTX_ALLOC     DRM_IOWR(DRM_IGD_APPCTX_ALLOC + BASE,\
+		emgd_drm_appcontext_alloc_t)
+#define DRM_IOCTL_IGD_APPCTX_FREE      DRM_IOWR(DRM_IGD_APPCTX_FREE + BASE,\
+		emgd_drm_appcontext_free_t)
+#define DRM_IOCTL_IGD_DRIVER_SAVE_RESTORE DRM_IOWR(DRM_IGD_DRIVER_SAVE_RESTORE\
+		+ BASE,\
+		emgd_drm_driver_save_restore_t)
+#define DRM_IOCTL_IGD_ENABLE_PORT      DRM_IOWR(DRM_IGD_ENABLE_PORT + BASE,\
+		emgd_drm_enable_port_t)
+#define DRM_IOCTL_IGD_GET_ATTRS        DRM_IOWR(DRM_IGD_GET_ATTRS + BASE,\
+		emgd_drm_get_attrs_t)
+#define DRM_IOCTL_IGD_GET_DISPLAY      DRM_IOWR(DRM_IGD_GET_DISPLAY + BASE,\
+		emgd_drm_get_display_t)
+#define DRM_IOCTL_IGD_GET_DRM_CONFIG   DRM_IOWR(DRM_IGD_GET_DRM_CONFIG + BASE,\
+		emgd_drm_get_drm_config_t)
+#define DRM_IOCTL_IGD_GET_EDID_BLOCK   DRM_IOWR(DRM_IGD_GET_EDID_BLOCK + BASE,\
+		emgd_drm_get_EDID_block_t)
+#define DRM_IOCTL_IGD_GET_EDID_INFO    DRM_IOWR(DRM_IGD_GET_EDID_INFO + BASE,\
+		emgd_drm_get_EDID_info_t)
+#define DRM_IOCTL_IGD_GET_PIXELFORMATS DRM_IOWR(DRM_IGD_GET_PIXELFORMATS +\
+		BASE,\
+		emgd_drm_get_pixelformats_t)
+#define DRM_IOCTL_IGD_GET_PORT_INFO    DRM_IOWR(DRM_IGD_GET_PORT_INFO + BASE,\
+		emgd_drm_get_port_info_t)
+#define DRM_IOCTL_IGD_GMM_ALLOC_REGION DRM_IOWR(DRM_IGD_GMM_ALLOC_REGION +\
+		BASE,\
+		emgd_drm_gmm_alloc_region_t)
+#define DRM_IOCTL_IGD_GMM_ALLOC_SURFACE DRM_IOWR(DRM_IGD_GMM_ALLOC_SURFACE +\
+		BASE,\
+		emgd_drm_gmm_alloc_surface_t)
+#define DRM_IOCTL_IGD_GMM_GET_NUM_SURFACE DRM_IOW(\
+		DRM_IGD_GMM_GET_NUM_SURFACE +\
+		BASE,\
+		emgd_drm_gmm_get_num_surface_t)
+#define DRM_IOCTL_IGD_GMM_GET_SURFACE_LIST DRM_IOW(\
+		DRM_IGD_GMM_GET_SURFACE_LIST +\
+		BASE,\
+		emgd_drm_gmm_get_surface_list_t)
+#define DRM_IOCTL_IGD_GMM_FREE         DRM_IOW(DRM_IGD_GMM_FREE + BASE,\
+		emgd_drm_gmm_free_t)
+#define DRM_IOCTL_IGD_GMM_FLUSH_CACHE  DRM_IOR(DRM_IGD_GMM_FLUSH_CACHE + BASE,\
+		emgd_drm_gmm_flush_cache_t)
+#define DRM_IOCTL_IGD_PAN_DISPLAY      DRM_IOWR(DRM_IGD_PAN_DISPLAY + BASE,\
+		emgd_drm_pan_display_t)
+#define DRM_IOCTL_IGD_POWER_DISPLAY    DRM_IOWR(DRM_IGD_POWER_DISPLAY + BASE,\
+		emgd_drm_power_display_t)
+#define DRM_IOCTL_IGD_PWR_ALTER        DRM_IOWR(DRM_IGD_PWR_ALTER + BASE,\
+		unsigned long)
+#define DRM_IOCTL_IGD_QUERY_DC         DRM_IOWR(DRM_IGD_QUERY_DC + BASE,\
+		emgd_drm_query_dc_t)
+#define DRM_IOCTL_IGD_QUERY_MAX_SIZE_OVL DRM_IOWR(DRM_IGD_QUERY_MAX_SIZE_OVL +\
+		BASE,\
+		emgd_drm_query_max_size_ovl_t)
+#define DRM_IOCTL_IGD_QUERY_OVL        DRM_IOWR(DRM_IGD_QUERY_OVL + BASE,\
+		emgd_drm_query_ovl_t)
+#define DRM_IOCTL_IGD_QUERY_MODE_LIST  DRM_IOWR(DRM_IGD_QUERY_MODE_LIST + BASE,\
+		emgd_drm_query_mode_list_t)
+#define DRM_IOCTL_IGD_GET_GOLDEN_HTOTAL  DRM_IOWR(DRM_IGD_GET_GOLDEN_HTOTAL + BASE,\
+		emgd_drm_get_golden_htotal_t)
+#define DRM_IOCTL_IGD_CONTROL_PLANE_FORMAT DRM_IOWR(DRM_IGD_CONTROL_PLANE_FORMAT + BASE,\
+		emgd_drm_control_plane_format_t)
+#define DRM_IOCTL_IGD_SET_OVERLAY_DISPLAY DRM_IOWR(DRM_IGD_SET_OVERLAY_DISPLAY + BASE,\
+		emgd_drm_set_overlay_display_t)
+#define DRM_IOCTL_IGD_SET_ATTRS        DRM_IOWR(DRM_IGD_SET_ATTRS + BASE,\
+		emgd_drm_set_attrs_t)
+#define DRM_IOCTL_IGD_SET_PALETTE_ENTRY DRM_IOWR(DRM_IGD_SET_PALETTE_ENTRY +\
+		BASE,\
+		emgd_drm_set_palette_entry_t)
+#define DRM_IOCTL_IGD_SET_SURFACE      DRM_IOWR(DRM_IGD_SET_SURFACE + BASE,\
+		emgd_drm_set_surface_t)
+#define DRM_IOCTL_IGD_SYNC             DRM_IOWR(DRM_IGD_SYNC + BASE,\
+		emgd_drm_sync_t)
+#define DRM_IOCTL_IGD_DRIVER_PRE_INIT  DRM_IOWR(DRM_IGD_DRIVER_PRE_INIT + BASE,\
+		emgd_drm_driver_pre_init_t)
+#define DRM_IOCTL_IGD_DRIVER_GET_PORTS DRM_IOWR(DRM_IGD_DRIVER_GET_PORTS + BASE,\
+		emgd_drm_driver_get_ports_t)
+#define DRM_IOCTL_IGD_START_PVRSRV     DRM_IOWR(DRM_IGD_START_PVRSRV + BASE,\
+		emgd_drm_start_pvrsrv_t)
+#define DRM_IOCTL_IGD_TEST_PVRSRV      DRM_IOWR(DRM_IGD_TEST_PVRSRV + BASE,\
+		emgd_drm_test_pvrsrv_t)
+
+#define DRM_IOCTL_IGD_GET_OVL_INIT_PARAMS DRM_IOWR(DRM_IGD_GET_OVL_INIT_PARAMS + BASE,\
+		emgd_drm_get_ovl_init_params_t)
+#define DRM_IOCTL_IGD_GET_CHIPSET_INFO DRM_IOWR(DRM_IGD_GET_CHIPSET_INFO + BASE,\
+		emgd_drm_driver_get_chipset_info_t)
+#define DRM_IOCTL_IGD_QUERY_2D_CAPS_HWHINT DRM_IOWR(DRM_IGD_QUERY_2D_CAPS_HWHINT + BASE,\
+		emgd_drm_query_2d_caps_hwhint_t)
+#define DRM_IOCTL_IGD_GET_DISPLAY_INFO  DRM_IOR(DRM_IGD_GET_DISPLAY_INFO + BASE,\
+		emgd_drm_get_display_info_t)
+
+#define DRM_IOCTL_IGD_DIHCLONE_SET_SURFACE	DRM_IOWR(DRM_IGD_DIHCLONE_SET_SURFACE + BASE,\
+		emgd_drm_dihclone_set_surface_t)
+
+#define DRM_IOCTL_IGD_WAIT_VBLANK			DRM_IOWR(DRM_IGD_WAIT_VBLANK + BASE,\
+		emgd_drm_driver_set_sync_refresh_t)
+
+
+/* From pvr_bridge.h */
+#define DRM_IOCTL_IGD_RESERVED_1       DRM_IOW(DRM_IGD_RESERVED_1 + BASE, \
+		PVRSRV_BRIDGE_PACKAGE)
+#define DRM_IOCTL_IGD_RESERVED_2       DRM_IO(DRM_IGD_RESERVED_2 + BASE)
+#define DRM_IOCTL_IGD_RESERVED_3       DRM_IO(DRM_IGD_RESERVED_3 + BASE)
+#define DRM_IOCTL_IGD_RESERVED_4       DRM_IO(DRM_IGD_RESERVED_4 + BASE)
+#define DRM_IOCTL_IGD_RESERVED_5       DRM_IOWR(DRM_IGD_RESERVED_5 + BASE, \
+		unsigned long)
+#define DRM_IOCTL_IGD_RESERVED_6       DRM_IO(DRM_IGD_RESERVED_6 + BASE)
+
+/* For Video (MXVDX/TOPAZ) */
+#define DRM_IOCTL_IGD_VIDEO_CMD_BUF    DRM_IOR(DRM_IGD_VIDEO_CMD_BUF + BASE,\
+		emgd_drm_video_cmd_buf_t)
+#define DRM_IOCTL_IGD_GET_DEVICE_INFO  DRM_IOR(DRM_IGD_GET_DEVICE_INFO + BASE,\
+		emgd_drm_device_info_t)
+#define DRM_IOCTL_IGD_GET_PAGE_LIST    DRM_IOR(DRM_IGD_GET_PAGE_LIST + BASE,\
+		emgd_drm_get_page_list_t)
+#define DRM_IOCTL_IGD_INIT_VIDEO       DRM_IOR(DRM_IGD_INIT_VIDEO + BASE,\
+		emgd_drm_init_video_t)
+#define DRM_IOCTL_IGD_VIDEO_GET_INFO  DRM_IOR(DRM_IGD_VIDEO_GET_INFO + BASE,\
+		emgd_drm_video_get_info_t)
+#define DRM_IOCTL_IGD_VIDEO_FLUSH_TLB  DRM_IOR(DRM_IGD_VIDEO_FLUSH_TLB + BASE,\
+		emgd_drm_video_flush_tlb_t)
+#define DRM_IOCTL_IGD_PREINIT_MMU  DRM_IOR(DRM_IGD_PREINIT_MMU + BASE,\
+		emgd_drm_preinit_mmu_t)
+#define DRM_IOCTL_IGD_GET_DISPLAY_INFO  DRM_IOR(DRM_IGD_GET_DISPLAY_INFO + BASE,\
+		emgd_drm_get_display_info_t)
+/* For Buffer Class of Texture Stream */
+#define DRM_IOCTL_IGD_BC_TS_INIT DRM_IOR(DRM_IGD_BC_TS_INIT + BASE,\
+		emgd_drm_bc_ts_t)
+#define DRM_IOCTL_IGD_BC_TS_UNINIT DRM_IOR(DRM_IGD_BC_TS_UNINIT + BASE,\
+		emgd_drm_bc_ts_t)
+#define DRM_IOCTL_IGD_BC_TS_REQUEST_BUFFERS DRM_IOR(DRM_IGD_BC_TS_REQUEST_BUFFERS + BASE,\
+		emgd_drm_bc_ts_t)
+#define DRM_IOCTL_IGD_BC_TS_RELEASE_BUFFERS DRM_IOR(DRM_IGD_BC_TS_RELEASE_BUFFERS + BASE,\
+		emgd_drm_bc_ts_t)
+#define DRM_IOCTL_IGD_BC_TS_SET_BUFFER_INFO DRM_IOR(DRM_IGD_BC_TS_SET_BUFFER_INFO + BASE,\
+		emgd_drm_bc_ts_t)
+#define DRM_IOCTL_IGD_BC_TS_GET_BUFFERS_COUNT DRM_IOR(DRM_IGD_BC_TS_GET_BUFFERS_COUNT + BASE,\
+		emgd_drm_bc_ts_t)
+#define DRM_IOCTL_IGD_BC_TS_GET_BUFFER_INDEX DRM_IOR(DRM_IGD_BC_TS_GET_BUFFER_INDEX + BASE,\
+		emgd_drm_bc_ts_t)
+#endif
diff --git a/drivers/gpu/drm/emgd/include/emgd_shared.h b/drivers/gpu/drm/emgd/include/emgd_shared.h
new file mode 100644
index 0000000..fe576ed
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/emgd_shared.h
@@ -0,0 +1,98 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: emgd_shared.h
+ * $Revision: 1.14 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This include file contains information that is shared between the various
+ *  EMGD driver components.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _EMGD_SHARED_H
+#define _EMGD_SHARED_H
+
+/*
+ * Module name is the name of the drm kernel module. This is used by
+ * user space components to open a connection to the module. A typical
+ * call would look like  -- drmOpen(EMGD_MODULE_NAME, NULL);
+ */
+#define EMGD_MODULE_NAME "emgd"
+
+#define EMGD_DRIVER_NAME "emgd"
+
+/*
+ * EMGD-specific numbering of the PVR DRM ioctls.  The EMGD DRM module is in
+ * charge, and includes the PVR DRM code.  As such, the PVR ioctls are included
+ * in with the EMGD ioctls ("emgd_drm.h"), and must be kept in sync.  Both sets
+ * of these ioctls are mapped to the device specific range between 0x40 and
+ * 0x79.
+ *
+ * Client driver must use these values!
+ */
+#define DRM_PVR_RESERVED1	0x12
+#define DRM_PVR_RESERVED2	0x13
+#define DRM_PVR_RESERVED3	0x14
+#define DRM_PVR_RESERVED4	0x15
+#define DRM_PVR_RESERVED5	0x16
+#define DRM_PVR_RESERVED6	0x1E
+
+
+/*
+ * The following typedefs support the ability of non-HAL software to have a
+ * function called when a VBlank interrupt occurs.
+ */
+
+/**
+ * A pointer to a non-HAL-provided function that processes a VBlank interrupt.
+ */
+typedef int (*emgd_process_vblank_interrupt_t)(void *priv);
+
+/**
+ * This structure allows the HAL to track a non-HAL callback (and its
+ *  parameter) to call when a VBlank interrupt occurs for a given port.  An
+ *  opaque pointer to this structure serves as a unique identifier for the
+ *  callback/port combination.
+ */
+typedef struct _emgd_vblank_callback {
+	/** Non-HAL callback function to process a VBlank interrupt. */
+	emgd_process_vblank_interrupt_t callback;
+	/** An opaque pointer to a non-HAL data structure (passed to callback). */
+	void *priv;
+	/** Which HAL port number is associated with this interrupt callback. */
+	unsigned long port_number;
+} emgd_vblank_callback_t;
+
+/**
+ * An opaque pointer to a emgd_vblank_callback_t.  This pointer serves as a
+ * unique identifier for the callback/port combination.
+ */
+typedef void *emgd_vblank_callback_h;
+
+/**
+ * A special value of a emgd_vblank_callback_h, meaning ALL devices/displays.
+ */
+#define ALL_PORT_CALLBACKS	((void *) 1001)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/gart.h b/drivers/gpu/drm/emgd/include/gart.h
new file mode 100644
index 0000000..228ac41
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/gart.h
@@ -0,0 +1,38 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: gart.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains OS independent abstractions for common gart
+ *  operations.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OAL_GART_H
+#define _OAL_GART_H
+
+typedef void * os_gart_allocation_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd.h b/drivers/gpu/drm/emgd/include/igd.h
new file mode 100644
index 0000000..f153eaf
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd.h
@@ -0,0 +1,1625 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd.h
+ * $Revision: 1.22 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains the top level dispatch table definition and includes
+ *  the common header files necessary to interface with the shingle springs
+ *  graphics driver.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_H
+#define _IGD_H
+
+#include <config.h>
+#include <igd_errno.h>
+#include <igd_mode.h>
+#include <igd_appcontext.h>
+#include <igd_render.h>
+#include <igd_2d.h>
+#include <igd_pd.h>
+#include <igd_gmm.h>
+#include <igd_rb.h>
+#include <igd_ovl.h>
+#include <emgd_shared.h>
+
+/*
+ * This is needed so that 16bit ports can use a far pointer on some
+ * of the prototypes.
+ */
+#ifndef FAR
+#define FAR
+#endif
+
+
+
+/*!
+ * @ingroup render_group
+ * @brief Dispatch table for accessing all runtime driver functions.
+ *
+ * This is the dispatch table for the driver. All rendering and driver
+ * manipulation functionality is done by calling functions within this
+ * dispatch table.
+ * This dispatch table will be populated during the igd_module_init()
+ * function call. Upon returning from that call all usable members
+ * will be populated. Any members left as NULL are not supported in the
+ * current HAL configuration and cannot be used.
+ */
+typedef struct _igd_dispatch {
+	/*!
+	 * Save the register state of the graphics engine.
+	 * This function is optional in the HAL. The caller must check that it
+	 * is non-null before calling.
+	 *
+	 * @param driver_handle The driver handle returned from igd_driver_init().
+	 *
+	 * @param flags Any combination of the @ref driver_save_flags
+	 *  flags which control the types of state to be saved. Also used to
+	 *  specify where we save the register data.
+	 *
+	 * @return 0 on Success
+	 * @return <0 on Error
+	 */
+	int (*driver_save)(igd_driver_h driver_handle,
+		const unsigned long flags);
+
+	/*!
+	 * Restore the graphics engine to a previously saved state.
+	 * This function is optional in the HAL. The caller must check that it
+	 * is non-null before calling.
+	 *
+	 * @param driver_handle The driver handle returned from igd_driver_init().
+	 *
+	 * @param flags Used to specify where we are restoring from,
+	 *  @ref driver_save_flags
+	 *
+	 * @return 0 on Success
+	 * @return <0 on Error
+	 */
+	int (*driver_restore)(igd_driver_h driver_handle,
+		const unsigned long flags);
+
+	/*!
+	 * Save all driver registers and then restore all
+	 *   registers from a previously saved set.
+	 * This function is optional in the HAL. The caller must check that it
+	 * is non-null before calling.
+	 *
+	 * @param driver_handle The driver handle returned from igd_driver_init().
+	 * @param flags Flags indicating what registers to save
+	 *
+	 * @return 0 on Success
+	 * @return <0 on Error
+	 */
+	int (*driver_save_restore)(igd_driver_h driver_handle, unsigned long flags);
+
+	/*!
+	 * Gets the value of a runtime driver parameter. These parameters are
+	 * each defined with a unique ID and may be altered at runtime.
+	 *
+	 * @note: There is a wrapper for this function in the dispatch table that
+	 * takes a display instead of a driver handle. This version is for use
+	 * when displays are not yet available.
+	 *
+	 * @return 0 Success
+	 * @return -IGD_INVAL Error
+	 */
+	int (*get_param)(igd_display_h display_handle, unsigned long id,
+		unsigned long *value);
+
+	/*!
+	 * Sets the value of a runtime driver parameter. These parameters are
+	 * each defined with a unique ID and may be altered at runtime.
+	 *
+	 * Note: There is a wrapper for this function in the dispatch table that
+	 * takes a display instead of a driver handle. This version is for use
+	 * when displays are not yet available.
+	 *
+	 * @return 0 Success
+	 * @return  -IGD_INVAL Error
+	 */
+	int (*set_param)(igd_display_h display_handle, unsigned long id,
+		unsigned long value);
+
+	/*!
+	 *  This function returns the list of available pixel formats for the
+	 *  framebuffer and the list of available pixel formats for the cursor.
+	 *
+	 *  Both lists end with NULL.  They are read only and should
+	 *  not be modified.
+	 *
+	 *  @bug To be converted to take a driver handle for IEGD 5.1
+	 *
+	 *  @param display_handle A igd_display_h type returned from a previous
+	 *    display->alter_displays() call.
+	 *
+	 *  fb_list_pfs  - Returns the list of pixel formats for the framebuffer.
+	 *
+	 *  cu_list_pfs - Returns the list of pixel formats for the cursor.
+	 *
+	 *
+	 * Returns:
+	 *  0: Success
+	 *  -IGD_INVAL: Error;
+	 */
+	int (*get_pixelformats)(igd_display_h display_handle,
+		unsigned long **fb_list_pfs, unsigned long **cu_list_pfs,
+		unsigned long **overlay_pfs, unsigned long **render_pfs,
+		unsigned long **texture_pfs);
+
+	/*!
+	 * @brief Return the list of available DCs.
+	 *
+	 * query_dc() returns the live zero terminated Display Configuration list.
+	 *  All usable display configurations are returned from the HAL. The IAL
+	 *  must select one from the list when calling alter_displays().
+	 *
+	 * @param driver_handle The driver handle returned from igd_driver_init()
+	 *
+	 * @param dc_list The returned display configuration(s) list. The IAL
+	 *  should use a dc from the list when calling alter_displays(). The
+	 *  dc_list is zero terminated and live. It should not be altered by the
+	 *  IAL. See @ref dc_defines
+	 *
+	 * @param flags modifies the behavior of the function.
+	 *   See: @ref query_dc_flags
+	 *
+	 * @return 0: Success.
+	 * @return -IGD_INVAL:  Otherwise
+	 */
+	int (*query_dc)(igd_driver_h driver_handle, unsigned long request,
+		unsigned long **dc_list, unsigned long flags);
+
+	/*!
+	 * Returns a live copy of the current mode list
+	 * for the requested display. This mode list will be for the master
+	 * port on the pipe and therefore may be the mode list for a TWIN
+	 * of the display requested.
+	 *
+	 * @param driver_handle The driver handle returned from igd_driver_init()
+	 *
+	 * @param dc The display configuration data to use when determining the
+	 *  available modes. See @ref dc_defines
+	 *
+	 * @param mode_list The returned mode list. This data should be freed by
+	 *   calling free_modes() unless the QUERY_LIVE_MODES flag was passed, in
+	 *   that case the live, in-use, data structure is returned. It should not
+	 *   be modified or freed.
+	 *
+	 * @param flags Flags to modify the operation of the function. Flags
+	 *   may contain any combination of the IGD_QUERY_* flags.
+	 *   See: @ref query_mode_list_flags
+	 *
+	 * @return 0: Success.
+	 * @return -IGD_ERROR_INVAL:  Otherwise
+	 */
+	int (*query_mode_list)(igd_driver_h driver_handle, unsigned long dc,
+		igd_display_info_t **mode_list, unsigned long flags);
+
+	/*!
+	 * Free modes that were returned from a previous call to query mode list.
+	 *
+	 * @param mode_list The mode list to be free. This data was returned
+	 *   from an earlier call to query_modes.=
+	 */
+	void (*free_mode_list)(igd_display_info_t *mode_list);
+
+	/*!
+	 * alter_displays() Modifies the modes associated with one or both display
+	 *  pipes according to the dc provided. The primary and secondary
+	 *  display handles are returned for use when rendering to these displays.
+	 *
+	 *  In extended or DIH modes only one fb_info needs to be provided at a
+	 *  time. In this manner two calls can be used one per framebuffer, as
+	 *  may be required by the OS.
+	 *
+	 *  @param driver_handle - required.  This is returned from a call to
+	 *   igd_init_driver().
+	 *
+	 *  @param primary A pointer to a display handle that will be populated
+	 *   during the call. This handle should be used for all rendering
+	 *   tasks directed to the primary framebuffer and pipe.
+	 *
+	 *  @param primary_pt_info The display timing information to be used for
+	 *    the primary display pipe.
+	 *
+	 *  @param primary_fb_info The framebuffer parameters to be used for the
+	 *   primary display. This data may be larger or smaller than the display
+	 *   timings to allow for centered/panned or scaled modes.
+	 *
+	 *  @param secondary A pointer to a display handle that will be populated
+	 *   during the call. This handle should be used for all rendering
+	 *   tasks directed to the secondary framebuffer or pipe.
+	 *
+	 *  @param secondary_pt_info The display timing information to be used for
+	 *   the secondary display pipe.
+	 *
+	 *  @param secondary_fb_info The framebuffer parameters to be used for the
+	 *   secondary display. This data may be larger or smaller than the display
+	 *   timings to allow for centered/panned or scaled modes.
+	 *
+	 *  @param dc A unique identifer to describe the configuration of the
+	 *   displays to be used. This identifier should be one returned from
+	 *   the _igd_dispatch::query_dc() call. See @ref dc_defines.
+	 *
+	 *  @param flags Bitfield to alter the behavior of the call.
+	 */
+	int (*alter_displays)(igd_driver_h driver_handle,
+		igd_display_h *primary,
+		igd_display_info_t *primary_pt_info,
+		igd_framebuffer_info_t *primary_fb_info,
+		igd_display_h *secondary,
+		igd_display_info_t *secondary_pt_info,
+		igd_framebuffer_info_t *secondary_fb_info,
+		unsigned long dc,
+		unsigned long flags);
+
+	/*!
+	 * igd_configure_display() Modifies the modes associated with one display
+	 *  pipes according to the dc provided.
+	 *
+	 *  @param driver_handle - required.  This is returned from a call to
+	 *   igd_init_driver().
+	 *
+	 *  @param display A pointer to a display handle that will be used
+	 *   during the call. This handle should be used for all rendering
+	 *   tasks directed to the secondary framebuffer or pipe.
+	 *
+	 *  @param pt_info The display timing information to be used for
+	 *    the display pipe.
+	 *
+	 *  @param fb_info The framebuffer parameters to be used for the
+	 *   display. This data may be larger or smaller than the display
+	 *   timings to allow for centered/panned or scaled modes.
+	 *
+	 *  @param dc A unique identifer to describe the configuration of the
+	 *   displays to be used. This identifier should be one returned from
+	 *   the _igd_dispatch::query_dc() call. See @ref dc_defines.
+	 *
+	 *  @param fb_index What is the 0-based framebuffer index
+	 *
+	 *  @param flags Bitfield to alter the behavior of the call.
+	 */
+	int (*igd_configure_display)(
+	    igd_driver_h driver_handle,
+	    igd_display_h *display,
+	    igd_display_info_t *pt_info,
+	    igd_framebuffer_info_t *fb_info,
+	    unsigned long dc,
+	    int fb_index,
+		unsigned long flags);
+
+	/*!
+	 *  pan_display() pans the display on the display device.
+	 *  It takes a @a x_offset, @a y_offset into the frame buffer and
+	 *  sets the display from (x_offset, y_offset) to
+	 *  (x_offset+width, y_offset+height).
+	 *  If x_offset+width, y_offset+height crosses frame buffer
+	 *  width and heigth, then it will return error.
+	 *
+	 * @param display_handle pointer to an IGD_DISPLAY pointer returned
+	 *    from a successful call to dispatch->alter_displays().
+	 *
+	 * @param x_offset these are frame buffer offsets from (0, 0).
+	 * @param y_offset these are frame buffer offsets from (0, 0).
+	 *
+	 * @return 0: The paning was successfull.
+	 * @return -IGD_INVAL:  Otherwise
+	 */
+	long (*pan_display)(igd_display_h display_handle,
+		unsigned long x_offset,
+		unsigned long y_offset);
+
+	/*!
+	 * Alters the current power state for the display. This does not
+	 * change the power state for the graphics hardware device.
+	 *
+	 * @bug Needs to be modified to power all displays on a pipe
+	 *
+	 * @param driver_handle - handle to a driver handle returned from a
+	 *    previous call to igd_driver_init()
+	 *
+	 * @param port_number - specific display (port) to change.
+	 *
+	 * @param power_state - D state to change to.
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 on Error
+	 */
+	int (*power_display)(igd_driver_h driver_handle,
+			unsigned short port_number,
+			unsigned int power_state);
+
+	/*!
+	 *  This function sets one palette entry for the framebuffer when the
+	 *  pixel format for the framebuffer indicates a palette is used.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @param palette_color A 32bit ARGB color
+	 *
+	 * @param palette_entry The palette index to set.
+	 *
+	 * @returns
+	 *  - 0: Success
+	 *  - -IGD_INVAL:  Otherwise
+	 */
+	int (*set_palette_entry)(igd_display_h display_handle,
+		unsigned long palette_entry,
+		unsigned long palette_color);
+	/*!
+	 *  This function gets the requested palette entry from the hardware.
+	 *  The results are undefined when not in a paletted mode or in a
+	 *  mode that uses palette for color correction.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @param palette_color A 32bit ARGB color
+	 *
+	 * @param palette_entry The palette index to return.
+	 *
+	 * @returns 0 on Success
+	 * @returns -IGD_ERROR_INVAL Otherwise
+	 */
+	int (*get_palette_entry)(igd_display_h display_handle,
+		unsigned long palete_entry,
+		unsigned long *palette_color);
+
+	/*!
+	 *  This function sets "count" palette entries starting with "count"
+	 *  offset into the palette_colors array.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 *  @param palette_colors A 32bit ARGB color array
+	 *
+	 *  @param start_index The first palette index to program.
+	 *
+	 *  @param count The number of palette entries to program.
+	 *
+	 * @returns 0 on Success
+	 * @return -IGD_ERROR_INVAL Otherwise
+	 */
+	int (*set_palette_entries)(igd_display_h display_handle,
+		unsigned long *palette_colors, unsigned int start_index,
+		unsigned int count);
+
+
+	/*!
+	 * Gets attributes for a display. SS will allocate the memory required to
+	 * return the *attr_list. This is a live copy of attributes used by both
+	 * IAL and HAL. Don't deallocate this memory. This will be freed by the
+	 * HAL.
+	 *
+	 * @param driver_handle Driver handle returned from a call to
+	 *   igd_driver_init()
+	 *
+	 * @param num_attrs pointer to return the number of attributes
+	 *    returned in attr_list.
+	 *
+	 * @param attr_list pointer to return the attributes.
+	 *
+	 * @returns 0 onSuccess
+	 * @returns -IGD_ERROR_NOATTR No attributes defined for this display
+	 * @returns -IGD_INVAL otherwise
+	 */
+	int (*get_attrs)(igd_driver_h driver_handle,
+			unsigned short port_number,
+			unsigned long *num_attrs,
+			igd_attr_t **attr_list);
+
+	/*!
+	 * set attributes for a display.
+	 *
+	 * @param driver_handle Driver handle returned from a call to
+	 *   igd_driver_init()
+	 *
+	 * @param num_attrs pointer to return the number of attributes
+	 *   returned in attr_list. This is equal to the num_attrs returned by
+	 *   igd_get_attrs().
+	 *
+	 * @param attr_list pointer returned from igd_get_attrs(). Change
+	 *   the attributes to desired values.
+	 *
+	 * @returns 0 on Success
+	 * @returns -IGD_ERROR_NOATTR No attributes defined for this display
+	 * @returns -IGD_INVAL Otherwise
+	 */
+	int (*set_attrs)(igd_driver_h driver_handle,
+			unsigned short port_number,
+			unsigned long num_attrs,
+			igd_attr_t *attr_list);
+
+	/*!
+	 * set flags for a display.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @param port_number of the port to modify.  (use zero to change
+	 *   all ports associated with a display?)
+	 *
+	 * @param flag to set in the port's pt_info before calling program
+	 *   port.
+	 *
+	 * @returns 0 on Success
+	 * @returns -IGD_INVAL Otherwise
+	 */
+	int (*enable_port)(igd_display_h display_handle,
+			unsigned short port_number,
+			unsigned long flag,
+			unsigned long test);
+
+	/*!
+	 * This functions returns the current scanline for the display handle
+	 * provided. The scanline will be accurate when possible, or equal
+	 * to IGD_IN_VBLANK, or IGD_IN_VSYNC during the vblank/vsync periods.
+	 * See @ref get_scanline_defs
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @param scanline An unsigned long pointer which will be populated by
+	 *    the HAL during the call.
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 on Error
+	 */
+	int (*get_scanline)(igd_display_h display_handle, int *scanline);
+
+	/*!
+	 *  This function alters the parameters associated with a cursor.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @param cursor_info An igd_cursor_info_t data structure with the
+	 *   parameters to be applied to the cursor.
+	 *
+	 * @param image A pointer to cursor image data. The image data
+	 *   will only be programmed with the cursor flag has either
+	 *   IGD_CURSOR_LOAD_ARGB_IMAGE or IGD_CURSOR_LOAD_XOR_IMAGE.
+	 *
+	 * @param 0 on Success
+	 * @param <0 on Error
+	 */
+	int (*alter_cursor)(igd_display_h display_handle,
+		igd_cursor_info_t *cursor_info, unsigned char *image);
+
+	/*!
+	 *  This function alters the position parameters associated with a cursor.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @param cursor_info An igd_cursor_info_t data structure with the
+	 *   parameters to be applied to the cursor. Only the x_offset and y_offset
+	 *   parameters will be used.
+	 *
+	 * @param 0 on Success
+	 * @param <0 on Error
+	 */
+	int (*alter_cursor_pos)(igd_display_h display_handle,
+		igd_cursor_info_t *cursor_info);
+
+	/*!
+	 * This function will block until the start of the next vblank/vsync
+	 * period.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 on Error
+	 */
+	int (*wait_vblank)(igd_display_h display_handle);
+
+	/*!
+	 * This function will block until the start of the next vblank/vsync
+	 * period.
+	 *
+	 * @param display_handle Display handle returned from a call to
+	 *   _igd_dispatch::alter_displays()
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 on Error
+	 */
+	int (*wait_vsync)(igd_display_h display_handle);
+
+	int (*query_in_vblank)(igd_display_h display_handle);
+
+	/*!
+	 * This function is to access I2C register values for the specified I2C
+	 * bus. Memory for 'igd_i2c_reg_t->buffer' should be allocated and freed
+	 * by caller.
+	 * This function is valid only for display connected via DIGITAL (DVO)
+	 * ports.
+	 *
+	 * @bug Documentation needs update or Remove
+	 *
+	 *  @param driver IGD driver handle.
+	 *
+	 *  @param i2c_reg pointer to the i2c register structure.
+	 *
+	 *  @param flags If the flags is
+	 *    IGD_I2C_WRITE - then igd_i2c_reg_t->buffer should point
+	 *       to array of 'num_bytes' number of valid I2C registers values.
+	 *    IGD_I2C_READ  - then igd_i2c_reg_t->buffer pointer should have space
+	 *       for 'num_bytes' number of valid I2C registers.
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 on Error
+	 */
+	int (*access_i2c)(igd_display_h display, igd_i2c_reg_t *i2c_reg,
+			unsigned long flags);
+
+	/*!
+	 * This function is to get the EDID information (not the actual block) for
+	 * the display device associated with the 'display_handle'.
+	 *
+	 * @param driver_handle The driver handle returned from igd_driver_init().
+	 * @param port_number display port number connected to display.
+	 * @param edid_version EDID version number.
+	 * @param edid_revision EDID revision number.
+	 * @param edid_size Tells the caller what size of buffer to allocate
+	 *   for the transfer.
+	 *
+	 * @returns 0 on Success
+	 * @returns -IGD_ERROR_EDID Error while reading EDID or no EDID
+	 * @return -IGD_INVAL Other error
+	 */
+	int (*get_EDID_info)(igd_driver_h driver_handle,
+			unsigned short port_number,
+			unsigned char *edid_version,
+			unsigned char *edid_revision,
+			unsigned long *edid_size);
+
+	/*!
+	 * This function is to get the EDID block of the specified size. The size
+	 * is returned from previous call to igd_get_EDID_info().
+	 *
+	 * @note The return value indicates success/failure, blocksize should be
+	 *  set by the API to the actual number of bytes transferred upon return.
+	 * @param driver_handle The driver handle returned from igd_driver_init().
+	 * @param port_number Specific display to query for EDID block data.
+	 * @param edid_ptr Buffer to hold the EDID block data, must be 128bytes.
+	 * @param block_number Tells the API which EDID block to read.
+	 *
+	 *  @returns 0 on Success
+	 *  @returns -IGD_ERROR_EDID Error while reading EDID or no EDID
+	 *  @returns -IGD_INVAL Other error
+	 */
+	int (*get_EDID_block)(igd_driver_h driver_handle,
+		unsigned short port_number,
+		unsigned char FAR *edid_ptr,
+		unsigned char block_number);
+
+	/*!
+	 * This function returns the information about the port/display
+	 *
+	 * @param driver_handle pointer to an IGD_DRIVER_H pointer returned
+	 *    from a successful call to igd_driver_init().
+	 *
+	 * @param port_number Specific port to get information for.
+	 *
+	 * @param port_info Port/display information
+	 *
+	 * @returns 0 on Success
+	 * @returns -IGD_INVAL Otherwise
+	 */
+	int (*get_port_info)(igd_driver_h driver_handle,
+			unsigned short port_number,
+			igd_port_info_t *port_info);
+
+	/*
+	 * Sync is used to insert and check the status of synchronization
+	 * points in the command queue. A sync inserted into the queue and
+	 * then check insures that all rendering commands inserted before the
+	 * sync are now complete.
+	 *
+	 * Sync should be called with the IGD_RENDER_NONBLOCK flag to insert
+	 * a new sync without waiting for completion. When called with
+	 * IGD_RENDER_BLOCK the call will wait for completion but may return
+	 * due to a timeout. The caller should determine if calling again is
+	 * prudent or if some other action should be taken insead of busy
+	 * waiting.
+	 *
+	 * @param display_handle pointer to an IGD_DISPLAY pointer returned
+	 *    from a successful call to dispatch->alter_displays().
+	 *
+	 * @param priority The command queue to use. IGD_PRIORITY_NORMAL is
+	 *    correct for most circumstances.
+	 *
+	 * @param sync The sync identifier that will be populated and returned
+	 *    during the call. To insert a new sync, this should be passed
+	 *    containing 0 (A pointer to a zero). To check the status of an
+	 *    existing sync pass the value returned from a previous call to
+	 *    this function.
+	 *
+	 * @param flags Sync flags.
+	 *
+	 * @returns
+	 *   0: On Success
+	 *   -IGD_ERROR_BUSY: When the sync is not yet complete
+	 */
+	int (*sync)(igd_display_h display_handle, int priority,
+		unsigned long *sync, unsigned long flags);
+	/*
+	 * Idle stalls until the entire engine has been idled.
+	 */
+	int (*idle)(igd_driver_h driver_handle);
+
+	/* igd_appcontext.h */
+	igd_appcontext_h (*appcontext_alloc)(igd_display_h display_handle,
+		int priority, unsigned int flags);
+	void (*appcontext_free)(igd_display_h display_handle,
+		int priority, igd_appcontext_h context_handle);
+
+	/* igd_pwr.h */
+	int (*pwr_alter)(igd_driver_h driver_handle, unsigned int power_state);
+	int (*pwr_query)(igd_driver_h driver_handle, unsigned int power_state);
+
+	/* igd_reset.h */
+	int (*reset_alter)(igd_driver_h driver_handle);
+
+	/* igd_gmm.h */
+
+	/*!
+	 * This function is used by igd client drivers to allocate surfaces from
+	 * graphics memory. A driver must use this function to allocate all
+	 * surfaces, and must in turn free the surfaces with dispatch->gmm_free()
+	 * before exit. Calls to this function are only valid after the gmm_init()
+	 * function has been called by the igd init module.
+	 *
+	 * @param offset The offset into the Gtt memory space that the surface
+	 *   begins. This is an output only. Each element of this array can be
+	 *   added to the base physical or virtual address to obtain a full
+	 *   virtual or physical address. Therefore this parameter should be
+	 *   accessed as offset[x]. The number of offset elements is dependent on
+	 *   the surface type. Normal and Buffer surfaces return 1 element, Mip Map
+	 *   surfaces return NumMips level elements. Cube Map surfaces return
+	 *   NumMips level elements. Volume Map surfaces return NumSlices elements.
+	 *   For Volume Maps: The number of planes decreases by half for each
+	 *   mip level just as the number of X and Y pixels decreases by half.
+	 *   All plane offsets for the first mip are returned first followed
+	 *   by all planes for the second mip and so-forth.
+	 *
+	 * @param pixel_format The pixel format id. See @ref pixel_formats
+	 *
+	 * @param width The width in pixels of the surface. This may be modified
+	 *   to be larger than the requested value.
+	 *
+	 * @param height Height in pixels of the surface. This may be modified to
+	 *   be larger than the requested value.
+	 *
+	 * @param pitch The pitch in bytes of the surface. This value is returned
+	 *   by the driver.
+	 *
+	 * @param size The size of the surface in bytes. This value is returned by
+	 *   the driver. In Planar formats this will be greater than height*pitch
+	 *
+	 * @param type The defined type of the surface to be allocated.
+	 *   See @ref alloc_surface_types
+	 *
+	 * @param flags A bitfied of potential uses for the surface. These
+	 *   potentially impact the requried alignment of the surface offset.
+	 *   See @ref surface_info_flags
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 Error
+	 */
+	int (*gmm_alloc_surface)(unsigned long *offset,
+		unsigned long pixel_format,
+		unsigned int *width,
+		unsigned int *height,
+		unsigned int *pitch,
+		unsigned long *size,
+		unsigned int type,
+		unsigned long *flags);
+
+
+	int (*gmm_map_ci)(unsigned long *gtt_offset,
+			unsigned long ci_param,	/*virtaddr or v4l2_offset*/
+			unsigned long *virt_addr,
+			unsigned int map_method,
+			unsigned long size);
+
+
+  	int (*gmm_unmap_ci)(unsigned long virt_addr);
+
+
+	/*!
+	 * This function maps an existing list of pages into the GTT.
+	 *
+	 * @param pagelist The live list of pages to be mapped.  The GMM
+	 *   should not modify or release this list.
+	 * @param gtt_offset The offset into the Gtt memory space at which the
+	 *   pages begin.  This is an output only.
+	 *
+	 * @param numpages The number of pages to map (i.e., length of pagelist).
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 Error
+	 */
+	int (*gmm_import_pages)(void **pagelist,
+			unsigned long *gtt_offset,
+			unsigned long numpages);
+
+	int (*gmm_get_num_surface)(unsigned long *count);
+	int (*gmm_get_surface_list)(unsigned long allocated_size,
+		unsigned long *list_size,
+		igd_surface_list_t **surface_list);
+
+	/*!
+	 * This function is used by igd client drivers to allocate regions from
+	 * graphics memory. Regions are portions of video memory that do not have
+	 * width and height parameters, only a linear size. A driver must use this
+	 * function to allocate all regions, and must in turn free the regions with
+	 * igd_mm_free() before exit.
+	 * Calls to this function are only valid after the gmm_init() function
+	 * has been called by the igd init module.
+	 *
+	 * @param offset The offset into the Gtt memory space that the region
+	 *   begins. This is an output only. This value can be added to the base
+	 *   physical or virtual address to obtain a full virtual or physical
+	 *   address.
+	 *
+	 * @param size The size of the region, this value may be modified to be
+	 *   larger or smaller than the requested value.
+	 *
+	 * @param type The defined type of the surface to be allocated.
+	 *   See @ref alloc_region_types
+	 *
+	 * @param flags A bitfied of potential uses for the region. These
+	 *   potentially impact the requried alignment of the region offset.
+	 *   See @ref alloc_region_flags
+	 *
+	 * @returns 0 on Success
+	 * @returns <0 on Error
+	 */
+	int (*gmm_alloc_region)(unsigned long *offset,
+		unsigned long *size,
+		unsigned int type,
+		unsigned long flags);
+
+	/*!
+	 * This function is used to find the actual physical address of the
+	 * system memory page given an offset into Gtt memory. This function
+	 * will only be successful if the offset provided was allocated to a
+	 * cursor or overlay register type.
+	 *
+	 * @param offset The offset as provided by the allocation function.
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_virt_to_phys)(unsigned long offset,
+		unsigned long *physical);
+
+	/*!
+	 *  This function should be used to free any regions or surfaces allocated
+	 *  during igd use. Calling with offsets that were not obtained via a
+	 *  prior call to _igd_dispatch::gmm_alloc_surface() or
+	 *  _igd_dispatch::gmm_alloc_region() are invalid and will produce
+	 *  undefined results.
+	 *  Calls to this function are only valid after the igd_module_init()
+	 *  function has been called.
+	 *
+	 * @param offset The offset as provided by the allocation function.
+	 *
+	 * @returns void
+	 */
+	void (*gmm_free)(unsigned long offset);
+
+	/*!
+	 *  This function should be used to release externally-allocated
+	 *  page lists that have been imported into the GMM.  This will
+	 *  simply unmap the pages from the GTT; the pages themselves
+	 *  should subsequently be freed by the external source.
+	 *  Calling with offsets that were not obtained via a
+	 *  prior call to _igd_dispatch::gmm_import_pages() or
+	 *  are invalid and will produce undefined results.
+	 *  Calls to this function are only valid after the igd_module_init()
+	 *  function has been called.
+	 *
+	 * @param offset The offset as provided by the allocation function.
+	 *
+	 * @returns void
+	 */
+	void (*gmm_release_import)(unsigned long offset);
+
+	/*!
+	 * This function returns current memory statistics.
+	 *
+	 * @param memstat An _igd_memstat structure to be populated during the call
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_memstat)(igd_memstat_t *memstat);
+
+	/*!
+	 * Allocates a surface similar to _igd_dispatch::gmm_alloc_surface();
+	 * however, in this case the surface is allocated from a cached pool of
+	 * similar surfaces to improve performance. The surface cache shrinks and
+	 * grows automatically based on usage model. The surface cache should
+	 * be used when many surfaces of similar format and size are allocated
+	 * and freed repeatedly and the highest performance is required. The
+	 * tradeoff is that memory will be consumed by the cache and which
+	 * can lead to the need to flush the cache when non-cached surfaces
+	 * fail due to out-of-memory conditions.
+	 *
+	 * Surface is passed in, and populated during call.
+	 * All inputs are the same as with gmm_alloc_surface.
+	 *
+	 * @param display_handle Display used with this surface
+	 * @param surface Input/Output structure containing surface information
+	 * @param flags used to modify the behavior of the function.
+	 *     See @ref gmm_alloc_cached_flags
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_alloc_cached)(igd_display_h display_handle,
+		igd_surface_t *surface, unsigned int flags);
+
+	/*!
+	 * Free a surface previously allocated with the
+	 * _igd_dispatch::gmm_alloc_cached() dispatch function. When freeing
+	 * a cached surface it is not necessary for rendering to be complete. In
+	 * this manner better performance can be achieved because it is likely
+	 * that the rendering will be complete before the surface could be reused.
+	 * When freeing a cached surface a sync ID obtained from
+	 * _igd_dispatch::sync() after all rendering commands to the surface
+	 * should be provided such that the cache manager can be sure rendering
+	 * is complete before the surface is reused or freed.
+	 *
+	 * @param display_handle Display used with this surface
+	 * @param surface structure containing surface information
+	 * @param sync_id obtained after all rendering to/from this surface
+	 */
+	void (*gmm_free_cached)(igd_display_h display_handle,
+		igd_surface_t *surface,
+		unsigned long sync_id);
+
+	/*!
+	 * Allocates a region similar to _igd_dispatch::gmm_alloc_region();
+	 * however, in this case the region is allocated from a cached pool of
+	 * similar regions to improve performance. The region cache shrinks and
+	 * grows automatically based on usage model. The region cache should
+	 * be used when many regions of similar format and size are allocated
+	 * and freed repeatedly and the highest performance is required. The
+	 * tradeoff is that memory will be consumed by the cache and which
+	 * can lead to the need to flush the cache when non-cached regions
+	 * fail due to out-of-memory conditions.
+	 *
+	 * Region information is passed in, and populated during call.
+	 * All inputs are the same as with gmm_alloc_region.
+	 *
+	 * @param display_handle Display used with this region
+	 *
+	 * @param offset The offset into the Gtt memory space that the region
+	 *   begins. This is an output only. This value can be added to the base
+	 *   physical or virtual address to obtain a full virtual or physical
+	 *   address.
+	 *
+	 * @param size The size of the region, this value may be modified to be
+	 *   larger or smaller than the requested value.
+	 *
+	 * @param type The defined type of the region to be allocated.
+	 *   See @ref alloc_region_types
+	 *
+	 * @param region_flags A bitfied of potential uses for the region. These
+	 *   potentially impact the requried alignment of the region offset.
+	 *   See @ref alloc_region_flags
+	 *
+	 * @param flags used to modify the behavior of the function.
+	 *     See @ref gmm_alloc_cached_flags
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_alloc_cached_region)(igd_display_h display_handle,
+		unsigned long *offset,
+		unsigned long *size,
+		unsigned int type,
+		unsigned int region_flags,
+		unsigned int flags);
+
+	/*!
+	 * Free a region previously allocated with the
+	 * _igd_dispatch::gmm_alloc_cached_region() dispatch function. When freeing
+	 * a cached region it is not necessary for rendering to be complete. In
+	 * this manner better performance can be achieved because it is likely
+	 * that the rendering will be complete before the region could be reused.
+	 * When freeing a cached region a sync ID obtained from
+	 * _igd_dispatch::sync() after all rendering commands to the region
+	 * should be provided such that the cache manager can be sure rendering
+	 * is complete before the region is reused or freed.
+	 *
+	 * @param display_handle Display used with this region
+	 *
+	 * @param offset The offset into the Gtt memory space that the region
+	 *   begins. This is an output only. This value can be added to the base
+	 *   physical or virtual address to obtain a full virtual or physical
+	 *   address.
+	 *
+	 * @param size The size of the region, this value may be modified to be
+	 *   larger or smaller than the requested value.
+	 *
+	 * @param type The defined type of the region to be allocated.
+	 *   See @ref alloc_region_types
+	 *
+	 * @param region_flags A bitfied of potential uses for the region. These
+	 *   potentially impact the requried alignment of the region offset.
+	 *   See @ref alloc_region_flags
+	 *
+	 * @param sync_id_write obtained after all rendering to this region
+	 * @param sync_id_read obtained after all rendering from this region
+	 */
+	void (*gmm_free_cached_region)(igd_display_h display_handle,
+		unsigned long offset,
+		unsigned long size,
+		unsigned int type,
+		unsigned int region_flags,
+		unsigned long sync_id_write,
+		unsigned long sync_id_read);
+
+	/*!
+	 *  Flushes surfaces out of the internal surface cache. During normal
+	 * operation an IAL will not need to call this. Only when an IAL is
+	 * making use of the surface cache for some operations and direct
+	 * gmm operations for others would it be necessary to flush the cache.
+	 *
+	 *
+	 * @returns 0 No surfaces flushed
+	 * @returns >0 Surfaces flushed
+	 * @returns <0 Error
+	 */
+	int (*gmm_flush_cache)(void);
+
+	/*!
+	 * Allocates a heap of the requested size.  The heap allocated is managed
+	 * by the GMM through "gmm_alloc_heapblock", "gmm_free_heapblock", and
+	 * "gmm_free_heap" functions.
+	 *
+	 * @param display_handle Used to access other GMM dispatch functions
+	 *
+	 * @param heap_offset The offset into the Gtt memory space that the heap
+	 *   begins.  This also serves as a "heap ID"
+	 *
+	 * @param heap_size The size of the heap.
+	 *
+	 * @param type Set to zero
+	 *
+	 * @param alignment The alignment of the blocks in this heap
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_alloc_heap)(igd_display_h display_handle,
+		unsigned long *heap_offset,
+		unsigned long *heap_size,
+		unsigned int   type,
+		unsigned long  alignment);
+
+	/*!
+	 * Frees the heap allocated by gmm_alloc_heap
+	 *
+	 * @param heap_offset The offset into the Gtt memory space that the heap
+	 *   begins
+	 */
+	void (*gmm_free_heap)(igd_display_h display_handle,
+				unsigned long heap_offset);
+
+	unsigned long (*gmm_get_pvtheap_size)(void);
+	unsigned long (*gmm_get_cache_mem)(void);
+	/*!
+	 * Allocates a block of the requested size from the heap indicated by
+	 * heap_offset.
+	 *
+	 * @param display_handle Used to access other GMM dispatch functions
+	 *
+	 * @param heap_offset The heap ID
+	 *
+	 * @param block_offset
+	 *
+	 * @param size The size of the block.
+	 *
+	 * @param type Set to zero
+	 *
+	 * @param flags Set to zero
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_alloc_heap_block)(igd_display_h display_handle,
+		unsigned long heap_offset,
+		unsigned long *block_offset,
+		unsigned long *size,
+		unsigned long flags);
+
+	/*!
+	 * Frees a block of previously allocated by gmm_alloc_heap_block
+	 *
+	 * @param display_handle Used to access other GMM dispatch functions
+	 *
+	 * @param block_offset Offset given by gmm_alloc_heap_block
+	 */
+	void (*gmm_free_heap_block)(igd_display_h display_handle,
+		unsigned long heap_offset,
+		unsigned long block_offset,
+		unsigned long sync_id_write,
+		unsigned long sync_id_read);
+
+	/*!
+	 * Gets the heap id/offset from which the block
+	 *
+	 * @param block_offset Offset given by gmm_alloc_heap_block
+	 * @param heap_offset Head ID/Offset associated with the block offset
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_get_heap_from_block)(unsigned long block_offset,
+		unsigned long *heap_offset);
+
+	/*!
+	 * Allocates a reservation of the requested size. A reservation is a
+	 * memory range that is dedicated for the callers use but it not
+	 * populated with usable memory. The caller must make a single call to
+	 * gmm_alloc_surface() or gmm_alloc_region() passing in the reservation
+	 * address to allocate a surface within the reservation. Only a single
+	 * surface or region may be placed in a reservation.
+	 *
+	 * @param offset The offset into the Gtt memory space that the reservation
+	 *   begins. This is an output only. This value can be added to the base
+	 *   physical or virtual address to obtain a full virtual or physical
+	 *   address.
+	 *
+	 * @param size The size of the reservation.
+	 *
+	 * @param flags A bitfied of potential uses for the reservation. These
+	 *   potentially impact the requried alignment of the reservation.
+	 *   See @ref alloc_reservation_flags
+	 */
+	int (*gmm_alloc_reservation)(unsigned long *offset,
+		unsigned long size,
+		unsigned long flags);
+
+	/*!
+	 * Allocates a region similar to _igd_dispatch::gmm_alloc_region();
+	 * however, in this case the region is allocated from a pool of
+	 * persisent regions.  The pool can only grow as additional regions
+	 * are allocated. The pool can be flushed but this should only be
+	 * done when GMM is being shutdown.
+	 *
+	 * Region information is passed in, and populated during call.
+	 * All inputs are the same as with gmm_alloc_region.
+	 *
+	 * @param display_handle Display used with this region
+	 *
+	 * @param offset The offset into the Gtt memory space that the region
+	 *   begins. This is an output only. This value can be added to the base
+	 *   physical or virtual address to obtain a full virtual or physical
+	 *   address.
+	 *
+	 * @param size The size of the region, this value may be modified to be
+	 *   larger or smaller than the requested value.
+	 *
+	 * @param type The defined type of the region to be allocated.
+	 *   See @ref alloc_region_types
+	 *
+	 * @param region_flags A bitfied of potential uses for the region. These
+	 *   potentially impact the requried alignment of the region offset.
+	 *   See @ref alloc_region_flags
+	 *
+	 * @param flags used to modify the behavior of the function.
+	 *     See @ref gmm_alloc_cached_flags
+	 *
+	 * @returns 0 on Success
+	 * @returns  <0 on Error
+	 */
+	int (*gmm_alloc_persistent_region)(igd_display_h display_handle,
+		unsigned long *offset,
+		unsigned long *size,
+		unsigned int type,
+		unsigned int region_flags,
+		unsigned int flags);
+
+	/*!
+	 * Free a region previously allocated with the
+	 * _igd_dispatch::gmm_alloc_persistent_region() dispatch function.
+	 * The region is marked as available and may be reused by the next
+	 * allocation request.  It is the callers responsibilty to make sure
+	 * rendering to the region is complete before freeing it.
+	 *
+	 * @param display_handle Display used with this region
+	 *
+	 * @param offset The offset into the Gtt memory space that the region
+	 *   begins. This is an input only.
+	 */
+	int (*gmm_free_persistent_region)(unsigned long offset);
+
+	/*!
+	 * Flushes regions out of the internal persistent list. During normal
+	 * operation an IAL will not need to call this. Only when an IAL is
+	 * exiting should it flush the list.
+	 *
+	 *
+	 * @returns 0 regions flushed
+	 * @returns <0 Error
+	 */
+	int (*gmm_flush_persistent_regions)(igd_display_h display_handle);
+
+	/*!
+	 * Creates a linear mapping of a video memory region into the
+	 * current process address space.
+	 *
+	 * @param offset Offset used to identifiy the memory region
+	 *
+	 * @returns address to mapping
+	 * @returns NULL Error
+	 */
+	void *(*gmm_map)(unsigned long offset);
+
+	/*!
+	 * Unmaps a linear mapping created by gmm_map.
+	 *
+	 * @param address pointer to the mapping
+	 */
+	void (*gmm_unmap)(void *address);
+
+	/*!
+	 * Export the list of physical pages allocated to a memory
+	 * retion.
+	 *
+	 * @param offset Offset used to identify the memory region
+	 * @param pages  Array of page structures holding the physical page
+	 *               addresses.
+	 * @param page_cnt Number of pages in the page array.
+	 *
+	 * @returns 0 if successful
+	 *          -IGD_ERROR_NOMEM if offset is invalid.
+	 */
+	int (*gmm_get_page_list)(unsigned long offset,
+			unsigned long **pages,
+			unsigned long *page_cnt);
+
+	void (*gmm_dump)(void);
+	void (*gmm_dump_v)(void);
+	char *gmm_debug_desc;
+
+	/* igd_2d.h */
+	int (*setup_clip_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect,
+		igd_appcontext_h appcontext_handle, unsigned int flags);
+	int (*setup_blt)(igd_display_h display_handle, int priority,
+		igd_surface_t *dest_surf, igd_rect_t *dest_rect,
+		unsigned long raster_ops, unsigned long bg_color,
+		unsigned long fg_color, igd_appcontext_h appcontext_handle,
+		unsigned int flags);
+	int (*color_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, unsigned int byte_mask,
+		unsigned int color, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*rgb_color_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, unsigned int byte_mask,
+		unsigned int color, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*pat_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, unsigned int byte_mask,
+		igd_pat_t *pat, igd_chroma_t *chroma, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*mono_pat_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, unsigned int byte_mask,
+		igd_mono_pat_t *pat, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*src_copy_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, igd_surface_t *src,
+		igd_coord_t *src_coord, unsigned int byte_mask, igd_chroma_t *chroma,
+		unsigned int raster_ops, igd_appcontext_h appcontext,
+		unsigned int flags);
+	int (*mono_src_copy_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, igd_mono_src_t *src,
+		unsigned int byte_mask, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*mono_src_copy_immed_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, igd_mono_src_t *src,
+		igd_surface_t *src_surface,	igd_rect_t *src_rect,
+		unsigned int byte_mask, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*full_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, igd_surface_t *src,
+		igd_coord_t *src_coord, unsigned int byte_mask, igd_pat_t *pat,
+		unsigned int raster_ops, igd_appcontext_h appcontext,
+		unsigned int flags);
+	int (*full_mono_src_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, igd_mono_src_t *src,
+		unsigned int byte_mask, igd_pat_t *pat, unsigned int raster_ops,
+		igd_appcontext_h appcontext, unsigned int flags);
+	int (*full_mono_pat_blt)(igd_display_h display_h, int priority,
+		igd_surface_t *dest, igd_rect_t *dest_rect, igd_coord_t *src_coord,
+		igd_surface_t *src,	unsigned int byte_mask, igd_mono_pat_t *pat,
+		unsigned int raster_ops, igd_appcontext_h appcontext,
+		unsigned int flags);
+	int (*full_mono_pat_mono_src_blt)(igd_display_h display_h,
+		int priority, igd_surface_t *dest, igd_rect_t *dest_rect,
+		igd_mono_src_t *src, unsigned int byte_mask, igd_mono_pat_t *pat,
+		unsigned int raster_ops, igd_appcontext_h appcontext,
+		unsigned int flags);
+	int (*text_immed_blt)(igd_display_h display_h,
+		int priority, igd_surface_t *dest_surf,
+		igd_rect_t *dest_rect,
+		unsigned char *glyph_data, unsigned int num_glyph_bytes,
+		igd_appcontext_h appcontext_handle, unsigned long raster_ops,
+		unsigned long bg_color,	unsigned long fg_color,
+		unsigned int flags);
+
+	/* igd_blend.h */
+	/*!
+	 * Blend and stretch and color convert a stack of input surfaces into a
+	 * destination surface.
+	 *
+	 * Blend takes N input surfaces and N corresponding input rectangles
+	 * and output rectangles. This allows for any subset of an input surface
+	 * to be output to any rectangle on the destination surface. The input
+	 * and output rectangles need not have any correlation between inputs.
+	 * Also, the input and output rectangle for any given surface need not
+	 * be the same size or aspect ratio, full up and down scaling is possible.
+	 * The destination surface is provided with a clip rectangle. All
+	 * rendering will be clipped to this rectangle regardless of the
+	 * provided destination rectangles.
+	 * Each input surface has a set of render_ops that will be respected
+	 * during the blend operation.
+	 * The IGD_RENDER_OP_BLEND render operation will allow the surface to
+	 * blend with the surfaces below it in the stack. The bottom surface
+	 * in the stack will then optionally blend with the existing contents
+	 * of the destination surface.
+	 *
+	 * All input surface must be allocated with the IGD_SURFACE_TEXTURE
+	 * flag and all output surfaces must be allocated with the
+	 * IGD_SURFACE_RENDER flag.
+	 *
+	 * When a stretch blit is performed (No blending) the surface should not
+	 * have the IGD_RENDER_OP_BLEND bit set in the render_ops to insure
+	 * that the fastest possible method is used and that no format
+	 * conversion takes place.
+	 *
+	 * Due to the number of possible permutations of this API it is
+	 * necessary to constrain the parameters that can be expected to work.
+	 * Specific hardware implementations may support more, however there is
+	 * no guarentee of functionality beyond those listed here.
+	 *
+	 * Blend will accept 1 or 2 input surfaces.
+	 * Blend will accept a maximum of 1 input surface with a palette.
+	 * Blend will accept a maximum of 1 input surface in planar format.
+	 * All Surfaces can output to ARGB format.
+	 * Only YUV inputs may output to YUV format.
+	 * xRGB formats will output ARGB with Alpha of 1.0 when
+	 *  IGD_RENDER_OP_BLEND is set on the surface. Otherwise the x will
+	 *  be retained from the original source.
+	 * Surfaces that are not pre-multipled cannot have a global alpha.
+	 *
+	 */
+	int (*blend)(igd_display_h display, int priority,
+		igd_appcontext_h appcontext,
+		igd_surface_t *src_surface, igd_rect_t *src_rect,
+		igd_surface_t *mask_surface, igd_rect_t *mask_rect,
+		igd_rect_t *dest_rect, igd_surface_t *dest_surface,
+		igd_rect_t *clip_rect, unsigned long flags);
+
+	/* igd_ovl.h */
+	/*!
+	 * Alter the overlay associated with the given display_h.
+	 *  Only 1 video surface can be associated with a given display_h
+	 *  (i.e. you can not have 2 video surfaces using the same display).
+	 *  This function should be called once for each unique framebuffer.
+	 *  Therefore, single, twin, clone, and vertical extended  modes
+	 *  should call this function once, and the HAL will properly display
+	 *  video using the primary overlay and second overlay if necessary.
+	 *  Whereas extended should call this function twice (once for each
+	 *  display_h) if the video spans multiple displays.
+	 *  In DIH, this function can be called once for each display, since a
+	 *  video surface can not span displays in DIH.
+	 *
+	 * The primary display in clone, the primary display in vertical extended,
+	 *  and the primary display in extended will always use the primary
+	 *  overlay.  The secondary display in clone, the secondary display in
+	 *  vertical extended, and the secondary display in extended will always
+	 *  use the secondary overlay.  The hardware overlay resources (excluding
+	 *  any video memory) will be allocated internal to the HAL during the
+	 *  _igd_dispatch::alter_displays() call.  Video memory surfaces required
+	 *  internal to the HAL, for stretching or pixel format conversions, will
+	 *  be dynamically allocated and freed as necessary.
+	 *
+	 * @param display_h Input display used with this overlay
+	 * @param appcontext_h Input appcontext which may be used for blend.
+	 *     This can be the same appcontext which is used for blend and
+	 *     2d (DD and XAA/EXA).
+	 * @param src_surf Input src surface information
+	 * @param src_rect Input src surface rectangle.
+	 *     This is useful for clone, extended, and vertical extended modes
+	 *     where the entire src_surf may not be displayed.
+	 * @param dest_rect Input dest surface rectangle.  This is relative to the
+	 *     framebuffer not the display (so clone mode works properly).
+	 * @param ovl_info Input overlay information to display.
+	 *     The color key, video quality, and gamma must be valid
+	 *     and correct when the overlay is on.  That means NO passing in NULL
+	 *     values to use previous settings.
+	 * @param flags Input to turn on/off the overlay and set other flags.
+	 *   See: @ref alter_ovl_flags
+	 *
+	 * @returns 0 (IGD_SUCCESS) on Success
+	 * @returns <0 (-igd_errno) on Error.  The overlay will be off, no need
+	 *     for the IAL to call the HAL to turn the overlay off.
+	 *     If -IGD_ERROR_INVAL is returned, something is either to big or
+	 *      to small for the overlay to handle, or it is panned off of the
+	 *      displayed.  This is likely not critical, since it may be stretched
+	 *      or panned back, so the overlay can support it.  The IAL
+	 *      should not return an error to the application, or else the
+	 *      application will likely exit.
+	 *     If -IGD_ERROR_HWERROR is returned, something is outside of what
+	 *      the overlay can support (pitch to large, dot clock to large,
+	 *      invalid pixel format, ring or flip not happening).  In this case,
+	 *      the IAL should return an error to the application, since
+	 *      additional calls will always fail as well.
+	 */
+	int (*alter_ovl)(igd_display_h display_h,
+		igd_appcontext_h     appcontext_h,
+		igd_surface_t       *src_surf,
+		igd_rect_t          *src_rect,
+		igd_rect_t          *dest_rect,
+		igd_ovl_info_t      *ovl_info,
+		unsigned int         flags);
+
+	int (*alter_ovl2)(igd_display_h display_h,
+		igd_surface_t       *src_surf,
+		igd_rect_t          *src_rect,
+		igd_rect_t          *dest_rect,
+		igd_ovl_info_t      *ovl_info,
+		unsigned int         flags);
+
+/*	int (*alter_ovl2_dihclone)(igd_display_h display_h,
+		igd_surface_t       *src_surf,
+		igd_rect_t          *src_rect,
+		igd_rect_t          *dest_rect,
+		igd_ovl_info_t      *ovl_info,
+		unsigned int         flags);*/
+
+
+	/* igd_ovl.h */
+	/*!
+	 *  Retrieve the kernel mode initialization parameters for overlay.
+	 *  This function should be called by user-mode drivers as they are
+	 *  initialized, to retrieve overlay initialization parameters
+	 *  discovered and held by the kernel driver.
+	 *
+	 * @returns 0 (IGD_SUCCESS) on Success
+	 * @returns <0 (-igd_errno) on Error.
+	 */
+	int (*get_ovl_init_params)(igd_driver_h driver_handle,
+				   ovl_um_context_t *ovl_um_context);
+
+	/*!
+	 * Query the overlay to determine if an event is complete or if a given
+	 * capability is present.
+	 *
+	 * @param display_h Display used with this overlay
+	 * @param flags Used to check for which event is complete or which
+	 *     capability is present.
+	 *     See @ref query_ovl_flags
+	 *
+	 * @returns TRUE if event has occured or capability is available
+	 * @returns FALSE if event is pending or capability is not available
+	 */
+	int (*query_ovl)(igd_display_h display_h,
+		unsigned int flags);
+
+	/*!
+	 * Query the overlay for the maximum width and height given the input
+	 * src video pixel format.
+	 *
+	 * @param display_h Display used with this overlay
+	 * @param pf Input src video pixel format
+	 * @param max_width Output maximum overlay width supported (in pixels)
+	 * @param max_height Output maximum overlay height supported (in pixels)
+	 *
+	 * @returns 0 (IGD_SUCCESS) on Success - will return success
+	 *    even if overlay is currently in use.
+	 * @returns <0 (-igd_errno) on Error
+	 */
+	int (*query_max_size_ovl)(igd_display_h display_h,
+		unsigned long pf,
+		unsigned int *max_width,
+		unsigned int *max_height);
+
+	/*!
+	 * Alter the sprite C plane with the associated osd/subpicture data
+	 */
+    int (*alter_ovl_osd)(igd_display_h display_h,
+        igd_appcontext_h     appcontext_h,
+        igd_surface_t *sub_surface,
+        igd_rect_t *sub_src_rect,
+        igd_rect_t *sub_dest_rect,
+        igd_ovl_info_t      *ovl_info,
+        unsigned int         flags);
+
+	int (*alter_ovl2_osd)(igd_display_h display_h,
+		igd_surface_t *sub_surface,
+		igd_rect_t *sub_src_rect,
+		igd_rect_t *sub_dest_rect,
+		igd_ovl_info_t      *ovl_info,
+		unsigned int         flags);
+
+	/* igd_render.h */
+	_igd_get_surface_fn_t get_surface;
+	_igd_set_surface_fn_t set_surface;
+	_igd_query_event_fn_t query_event;
+
+	/* These functions are only to be used by priveledged IALs */
+	_igd_alloc_ring_fn_t alloc_ring;
+	_igd_exec_buffer_fn_t execute_buffer;
+	_igd_rb_reserve_fn_t rb_reserve;
+	_igd_rb_update_fn_t rb_update;
+	_igd_get_sync_slot_fn_t get_sync_slot;
+	_igd_query_buffer_fn_t query_buffer;
+
+	/*!
+	 *  dispatch->get_display() returns the current framebuffer and
+	 *  display information.
+	 *
+	 * @param display_handle required.  The display_handle contains the
+	 *  display information to return.  This parameter was returned from a
+	 *  previous call to dispatch->alter_displays().
+	 *
+	 * @param port_number required. The port number will determine which
+	 *  port's display info data to return.
+	 *
+	 * @param fb_info required and allocated by the caller.  The fb_info
+	 *  struct is returned to the caller describing the current
+	 *  frame buffer.
+	 *
+	 * @param pt_info required and allocated by the caller.  The
+	 *  pt_info struct is returned to caller describing the
+	 *  requested display parameters.
+	 *
+	 * @param flags - Currently not used
+	 *
+	 * @returns 0 The mode was successfully returned for all displays.
+	 * @returns -IGD_INVAL: Was not able to return the display information.
+	 */
+	int (*get_display)(igd_display_h display_handle,
+			unsigned short port_number, igd_framebuffer_info_t *fb_info,
+			igd_display_info_t *pt_info, unsigned long flags);
+
+#ifdef D3D_DPM_ALLOC
+   /* FIXEME: Somehow this D3D_DPM_ALLOC is always invisible to display dll.
+    * To avoid the whole structure corruption, the following delcaration
+    * appended at the very end.
+    */
+   unsigned long* (*gmm_map_sgx)(unsigned long size, unsigned long offset,
+      unsigned long flag);
+#endif
+	int (*get_golden_htotal)(igd_display_info_t *drm_mode_in, igd_display_info_t *drm_mode_out  );
+
+	/*!
+	 *  Registers a VBlank interrupt callback function (and its parameter) to
+	 *  call when a VBlank interrupt occurs for a given port.
+	 *
+	 * @param callback (IN).  A callback (function pointer) to a non-HAL
+	 * function that processes a VBlank interrupt.
+	 *
+	 * @param priv (IN).  An opaque pointer to a non-HAL data structure.
+	 *  This pointer is passed as a parameter of the callback function.
+	 *
+	 * @param port_number (IN).  The EMGD port number to register a VBlank
+	 *  interrupt callback for.
+	 *
+	 * @return A handle that uniquely identifies this callback/port
+	 *  combination, or NULL if a failure.
+	 */
+	emgd_vblank_callback_h (*register_vblank_callback)(
+		emgd_process_vblank_interrupt_t callback,
+		void *priv,
+		unsigned long port_number);
+
+	/*!
+	 *  Unregisters a previously-registered VBlank interrupt callback function
+	 *  for a given port.
+	 *
+	 * @param callback_h (IN).  The handle that uniquely identifies the VBlank
+	 *  interrupt callback to unregister.
+	 */
+	void (*unregister_vblank_callback)(
+		emgd_vblank_callback_h callback_h);
+
+	/*!
+	 *  Enable delivering VBlank interrupts to the callback function for the
+	 *  registered callback/port combination.
+	 *
+	 * @param callback_h (IN).  The handle that uniquely identifies which
+	 *  VBlank interrupt callback/port combination to enable.
+	 *
+	 * @return Zero if successful, non-zero if a failure.
+	 */
+	int (*enable_vblank_callback)(emgd_vblank_callback_h callback_h);
+
+	/*!
+	 *  Disable delivering VBlank interrupts to the callback function for the
+	 *  registered function/port combination.
+	 *
+	 * @param callback_h (IN).  The handle that uniquely identifies which
+	 *  VBlank interrupt callback/port combination to disable.
+	 */
+	void (*disable_vblank_callback)(
+		emgd_vblank_callback_h callback_h);
+
+	/*!
+	 * Shutdown MSVDX before the X exits.
+	 */
+	void (*video_shutdown)(void);
+
+
+	/*!
+	 * Query the hardware on for given 2D Caps.
+	 */ 
+	void (*query_2d_caps_hwhint)(unsigned long caps_val,
+		unsigned long *status);
+
+	int (*dihclone_set_surface)(
+	unsigned long display_number,
+	unsigned long mode);
+
+	/*!
+	 *  Allows a user mode application to change pixel format of a display
+	 *  plane from XRGB to ARGB and vice versa.
+	 *
+	 * @param enable (IN). Whether to turn on transparency or not with the
+	 *  assumption that XRGB turns off transparency and ARGB turns it on.
+	 *  0 = disable
+	 *  1 = enable
+	 * @param display_plane (IN). Which display plane to change pixel_format of
+	 *  0 = Plane A
+	 *  1 = Plane B
+	 */
+	int (*control_plane_format)(int enable, igd_display_h display_handle);
+
+	/*!
+	 * Overlay Plane assignment override.
+	 */ 
+	int (*set_ovl_display)(igd_display_h ovl_displays[]);
+
+/*!
+	 * Get MSVDX status.
+	 */ 
+	int (*msvdx_status)(igd_driver_h driver_handle, unsigned long *queue_status, unsigned long *mtx_msg_status);
+} igd_dispatch_t;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_2d.h b/drivers/gpu/drm/emgd/include/igd_2d.h
new file mode 100644
index 0000000..f7ee253
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_2d.h
@@ -0,0 +1,380 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_2d.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is a header file for the Intel GFX commands.
+ *  This includes commands specific to Intel hardware and structures specific
+ *  to Intel hardware.  All other commands and structures are available
+ *  through GFX.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_2D_H
+#define _IGD_2D_H
+
+/* Needed for igd_display_h and pixel_format. */
+#include <igd_mode.h>
+#include <igd_render.h>
+#include <igd_appcontext.h>
+
+/* byte mask */
+#define IGD_ALPHA_CHANNEL 0x200000
+#define IGD_RGB_CHANNEL   0x100000
+
+/* flags for XY_SETUP_CLIP_BLT */
+#define IGD_CLIP_ENABLED		0x01
+#define IGD_CLIP_DISABLED		0x00
+
+/* flags for igd_mono_src_t->pat_select_transp_mode for functions
+ *  igd_mono_src_copy_blt
+ *  igd_full_mono_src_blt
+ *  igd_full_mono_pat_blt
+ *  igd_full_mono_pat_mono_src_blt
+ */
+#define IGD_SRC_TRANSP_MODE_BACKGROUND	(0x0 << 29)
+#define IGD_SRC_TRANSP_MODE_ENABLED		(0x1 << 29)
+
+/* flags for igd_mono_pat_t->pat_select_transp_mode for functions
+ *  igd_full_mono_pat_blt
+ *  igd_full_mono_pat_mono_src_blt
+*/
+#define IGD_PAT_SOLID                   (0x1 << 31)
+#define IGD_PAT_NO_SOLID                (0x0 << 31)
+
+/* flags for igd_mono_pat_t->pat_select_transp_mode for functions
+ *  igd_mono_pat_blt
+ *  igd_full_mono_pat_blt
+ *  igd_full_mono_pat_mono_src_blt
+ * FIXME: Does this work for igd_full_mono_pat_blt???
+ */
+#define IGD_PAT_TRANSP_MODE_BACKGROUND	(0x0 << 28)
+#define IGD_PAT_TRANSP_MODE_ENABLED		(0x1 << 28)
+
+
+/* chroma transparency range mode */
+#define IGD_NO_TRANSP             0
+#define IGD_SRC_RGB_TRANSP        1
+#define IGD_SRC_RGB_ALPHA_TRANSP  2
+#define IGD_DEST_RGB_TRANSP       3
+#define IGD_DEST_RGB_ALPHA_TRANSP 4
+
+
+typedef struct igd_chroma {
+	unsigned int transp_range_mode;
+	unsigned int color_low;
+	unsigned int color_high;
+} igd_chroma_t;
+
+typedef struct igd_mono_src {
+	unsigned int data_bit_pos;
+	unsigned int mono_src_transp_mode;
+	/*
+	 * FIXME: Maybe. Immediate mode instructions need a virtual
+	 *  address. The rest need an offset. Hence there are two
+	 *  address values in this structure. The type of instructure
+	 *  determines which one is used.  This is a bit confusing for
+	 *  the IAL layer. Ideally, these would be combined into one value.
+	 */
+	unsigned long addr;
+	unsigned char *virt_addr;  /* Used by Immediate mode instructions */
+	unsigned int bgcolor;
+	unsigned int fgcolor;
+	int mono_src_size;
+} igd_mono_src_t;
+
+/* fixed pattern not supported yet */
+typedef struct igd_mono_pat {
+	unsigned int pat_select_transp_mode;
+	unsigned int horiz_seed;
+	unsigned int vert_seed;
+	unsigned int bgcolor;
+	unsigned int fgcolor;
+	unsigned int data0;
+	unsigned int data1;
+} igd_mono_pat_t;
+
+typedef struct igd_pat {
+	unsigned int horiz_seed;
+	unsigned int vert_seed;
+	unsigned long base_addr;
+    unsigned char *virt_addr;
+} igd_pat_t;
+
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->setup_clip_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_rect_t *dest_rect,
+ *    igd_appcontext_h appcontext_handle,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  XY_SETUP_CLIP_BLT: used to change the clip coordinate registers
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->color_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    unsigned int byte_mask,
+ *    unsigned int color,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  color_blt: copy solid_color to dest at specific address
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->rgb_color_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    unsigned int byte_mask,
+ *    unsigned int color,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->pat_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    unsigned int byte_mask,
+ *    igd_pat_t *pat,
+ *    igd_chroma_t *chroma,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  pat_blt() copy a color pattern to the dest surface
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->mono_pat_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    unsigned int byte_mask,
+ *    igd_mono_pat_t *pat,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  mono_pat_blt(): copy a monochrome pattern to the dest surface
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->src_copy_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    igd_surface_t *src,
+ *    igd_coord_t *src_coord,
+ *    unsigned int byte_mask,
+ *    igd_chroma_t *chroma,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  src_copy_blt: copy a source surface to the dest surface
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->mono_src_copy_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    igd_mono_src_t *src,
+ *    unsigned int byte_mask,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  mono_src_copy_blt: color source copy with chroma-keying
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->full_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    igd_surface_t *src,
+ *    igd_coord_t *src_coord,
+ *    unsigned int byte_mask,
+ *    igd_pat_t *pat,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  full_blt: copy with specification of destination, source
+ *  and pattern
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->full_mono_src_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    igd_mono_src_t *src,
+ *    unsigned int byte_mask,
+ *    igd_pat_t *pat,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  full_mono_src_blt: copy with specification of destination, source
+ *  and pattern where source is monochrome
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *   int dispatch->full_mono_pat_blt(igd_display_h display_handle,
+ *     int priority,
+ *     igd_surface_t *dest,
+ *     igd_rect_t *dest_rect,
+ *     igd_coord_t *src_coord,
+ *     igd_surface_t *src,
+ *     unsigned int byte_mask,
+ *     igd_mono_pat_t *pat,
+ *     unsigned int raster_ops,
+ *     igd_appcontext_h appcontext,
+ *     unsigned int flags);
+ *
+ * Description:
+ *  full_mono_pat_blt copy with specification of destination, source
+ *  and pattern where pattern is monochrome
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->full_mono_pat_mono_src_blt(igd_display_h display_handle,
+ *    int priority,
+ *    igd_surface_t *dest,
+ *    igd_rect_t *dest_rect,
+ *    igd_mono_src_t *src,
+ *    unsigned int byte_mask,
+ *    igd_mono_pat_t *pat,
+ *    unsigned int raster_ops,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int flags);
+ *
+ * Description:
+ *  full_mono_pat_mono_src_blt copy with specification of destination, source
+ *  and pattern where patter and source are monochrome
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+#endif /* _IGD_2D_H */
diff --git a/drivers/gpu/drm/emgd/include/igd_appcontext.h b/drivers/gpu/drm/emgd/include/igd_appcontext.h
new file mode 100644
index 0000000..d987937
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_appcontext.h
@@ -0,0 +1,72 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_appcontext.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is an IGD exported header file for the client context Module.
+ *  This module includes tools for maintaining device state including
+ *  hardware context allocation.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_APPCONTEXT_H
+#define _IGD_APPCONTEXT_H
+
+typedef void *igd_appcontext_h;
+
+#define IGD_CONTEXT_STATE_2D     0x00000001
+#define IGD_CONTEXT_STATE_3D     0x00000002
+#define IGD_CONTEXT_STATE_VD     0x00000004
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  igd_appcontext_h dispatch->appcontext_alloc(igd_display_h display_handle,
+ *    int priority,
+ *    unsigned int flags);
+ *
+ * Description:
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  void dispatch->appcontext_free(igd_display_h display_handle,
+ *    int priority,
+ *    igd_appcontext_h context_handle);
+ *
+ * Description:
+ *
+ * Parameters:
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_blend.h b/drivers/gpu/drm/emgd/include/igd_blend.h
new file mode 100644
index 0000000..2ea3eb6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_blend.h
@@ -0,0 +1,73 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_blend.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is a header file for the Intel GFX commands.
+ *  This includes commands specific to Intel hardware and structures specific
+ *  to Intel hardware.  All other commands and structures are available
+ *  through GFX.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_BLEND_H
+#define _IGD_BLEND_H
+
+#include <igd_render.h>
+
+/* These flags must not conflict with the IGD_RENDER flags in igd_render.h */
+#define IGD_FILTER_MASK             0x00000300
+#define IGD_FILTER_NEAREST          0x00000000
+#define IGD_FILTER_BILINEAR         0x00000100
+#define IGD_FILTER_ANISOTROPIC      0x00000200
+#define IGD_FILTER_4X4              0x00000300
+/* Indicates multiple blends will be sent in a row and this is not the
+ * last blend. */
+#define IGD_BLEND_NO_FLUSH          0x00000800
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->blend(igd_display_h display_handle,
+ *    int priority,
+ *    igd_appcontext_h appcontext,
+ *    unsigned int num_src,
+ *    igd_surface_t **src_surface_list,
+ *    igd_rect_t **src_rect_list,
+ *    igd_rect_t **dest_rect_list,
+ *    igd_surface_t *dest_surface,
+ *    igd_rect_t *clip_rect,
+ *    unsigned long flags);
+ *
+ * Description:
+ *
+ * Parameters:
+ *
+ *  flags: Flags are defined in igd_render.h
+ *
+ * Returns:
+ *----------------------------------------------------------------------
+ */
+
+#endif /* _IGD_BLEND_H */
diff --git a/drivers/gpu/drm/emgd/include/igd_debug.h b/drivers/gpu/drm/emgd/include/igd_debug.h
new file mode 100644
index 0000000..455b182
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_debug.h
@@ -0,0 +1,72 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_debug.h
+ * $Revision: 1.10 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains data structures and variables needed to control
+ *  debugging within the IEGD driver.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_DEBUG_H
+#define _IGD_DEBUG_H
+
+typedef struct _igd_debug {
+	struct {
+		/* Per-Module Debug Bits */
+		short cmd      ;
+		short dsp      ;
+		short mode     ;
+		short init     ;
+		short overlay  ;
+		short power    ;
+		short _2d      ;
+		short blend    ;
+		short state    ;
+		short gmm      ;
+		short gart     ;
+		short oal      ;
+		short intr     ;
+		short dpd      ;
+		short video    ;
+		short pvr3dd   ;
+		short buf_class;
+		/* Global Debug Bits */
+		short trace    ;
+		short instr    ;
+		short debug    ;
+		/* Verbose debug bits */
+		short blend_stats        ;
+		short dump_overlay_regs  ;
+		short dump_command_queue ;
+		short dump_gmm_on_fail   ;
+		short dump_shaders       ;
+	} hal;
+} igd_debug_t;
+
+extern igd_debug_t *emgd_debug;
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_errno.h b/drivers/gpu/drm/emgd/include/igd_errno.h
new file mode 100644
index 0000000..ee0c69c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_errno.h
@@ -0,0 +1,60 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_errno.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains igd error definitions.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_ERRNO_H
+#define _IGD_ERRNO_H
+
+/* Error values, must be <64kb for vBIOS */
+#define IGD_ERROR_NOMEM           0x0001
+#define IGD_ERROR_INVAL           0x0002
+#define IGD_ERROR_BUSY            0x0003
+#define IGD_ERROR_PWRDOWN         0x0004
+#define IGD_ERROR_NODEV           0x0005
+#define IGD_ERROR_HWERROR         0x0006
+#define IGD_ERROR_EDID            0x0007
+#define IGD_ERROR_INVAL_CONTEXT   0x0008
+#define IGD_ERROR_INVAL_PARAM     0x0009
+#define IGD_ERROR_IOCTL           0x000A
+#define IGD_ERROR_GENERIC         0x000B
+#define IGD_ERROR_BLT_NOTCOMPLETE 0x000B
+#define IGD_ERROR_NOT_IMPLEMENTED 0x000C
+#define IGD_ERROR_MAPPING_FAILED  0x000D
+
+#define IGD_NO_MEM           IGD_ERROR_NOMEM
+#define IGD_INVAL            IGD_ERROR_INVAL
+
+/* Success value */
+#define IGD_SUCCESS               0
+#define IGD_SUCCESS_DO_QRY_SETMODE    0x1001
+
+#define IGD_DO_QRY_SETMODE        IGD_SUCCESS_DO_QRY_SETMODE
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_gart.h b/drivers/gpu/drm/emgd/include/igd_gart.h
new file mode 100644
index 0000000..568e271f9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_gart.h
@@ -0,0 +1,77 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_gart.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_GART_H_
+#define _IGD_GATR_H_
+
+typedef struct _gtt_info {
+	void* virt_mmadr;
+	void* virt_gttadr;
+	void* virt_gttadr_upper;
+
+	unsigned long new_gtt;
+	unsigned long gtt_phyadr;
+	unsigned long is_virt_aperture;
+	unsigned long reset_gtt_entries;
+	unsigned long fb_phys_addr;
+	unsigned long scratch_phys;
+
+	unsigned long num_gtt_entries;
+	unsigned long gtt_entry_start;
+	unsigned long gtt_entry_end;
+	unsigned long num_contig_allocs;
+	unsigned char **cont_pages_virts;
+	/* FIXME!!! - this cant handle 64-bit Architecture*/
+	unsigned long *cont_pages_phys;
+	unsigned long *cont_pages_sizes;
+#ifdef D3D_DPM_ALLOC
+   void* virt_gttadr_dpm;
+#endif
+}gtt_info_t;
+
+/*
+ * Note: Platforms extend this data structure so the pointer can be used
+ * as either this DI dispatch or cast to the DD dipatch.
+ */
+typedef struct _init_gart_dispatch {
+	int (*get_gtt_ctl)(void* gtt_info);
+	int (*init_gtt_table)(void* gtt_info);
+	int (*flush_gtt_tlb)(void* gtt_info);
+	int (*shutdown_gtt)(void* gtt_info);
+} init_gart_dispatch_t;
+
+void* igd_get_gtt_dispatch(unsigned long dev_id);
+int igd_get_gtt_ctl(void* gtt_info, unsigned long dev_id);
+int igd_init_gtt(void* gtt_info, unsigned long dev_id);
+int igd_flush_gtt(void* gtt_info, unsigned long dev_id);
+int igd_shutdown_gtt(void* gtt_info, unsigned long dev_id);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_gmm.h b/drivers/gpu/drm/emgd/include/igd_gmm.h
new file mode 100644
index 0000000..e28a809
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_gmm.h
@@ -0,0 +1,383 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_gmm.h
+ * $Revision: 1.13 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the public header file for gmm. It should be included by any
+ *  client driver or igd module that requires gmm interaction.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_MM_H
+#define _IGD_MM_H
+
+#include <igd_mode.h>
+#include <igd_render.h>
+
+/*!
+ * @addtogroup gmm_group
+ *
+ * <B>Relavent Dispatch Functions</B>
+ *  - _igd_dispatch::gmm_alloc_surface()
+ *  - _igd_dispatch::gmm_alloc_region()
+ *  - _igd_dispatch::gmm_free()
+ *  - _igd_dispatch::gmm_memstat()
+ *  - _igd_dispatch::gmm_alloc_cached()
+ *  - _igd_dispatch::gmm_free_cached()
+ *  - _igd_dispatch::gmm_flush_cache()
+ *
+ * @{
+ */
+
+/*!
+ * @name Surface Alignment Flags
+ * @anchor surface_info_flags
+ *
+ * Surface Alignment Flags are passed to the _igd_dispatch::gmm_alloc_surface()
+ * function as an indicator for how the surface will be used.
+ *
+ * - IGD_SURFACE_RENDER The surface may be used as a rendering target,
+ *     this must be set for all color buffers.
+ * - IGD_SURFACE_TEXTURE The surface may be used as a texture input.
+ * - IGD_SURFACE_CURSOR Surface may be used as a cursor
+ * - IGD_SURFACE_OVERLAY Surface may be used with the overlay
+ * - IGD_SURFACE_DISPLAY This surface is suitable for use with the display
+ *   engine. This flag must be set for any render target that may later
+ *   be flipped to the front buffer.
+ * - IGD_SURFACE_DEPTH The surface may be used as a depth (Z) buffer.
+ * - IGD_SURFACE_DRI2 The surface is a DRI2 allocation (back buffer,
+ *   fake front buffer, etc.).  It should be allocated on the general
+ *   heap rather than the default '2D' heap.
+ *
+ * - IGD_SURFACE_YMAJOR If the surface is tiled, it is tiled with the
+ *     walk in the YMajor direction. This flag is output only unless the
+ *     allocation type is Fixed TF.
+ * - IGD_SURFACE_XMAJOR If the surface is tiled, it is tiled with the
+ *     walk in the YMajor direction. This flag is output only unless the
+ *     allocation type is Fixed TF.
+ * - IGD_SURFACE_TILED Surface is tiled. This flag is output only unless
+ *     the allocation type is Fixed TF.
+ * - IGD_SURFACE_FENCED Surface is fenced. This flaf is output only unless
+ *     the allocation type is Fixed TF.
+ *
+ * Additionally all FB flags can be populated
+ * See: @ref fb_info_flags
+ *
+ * @{
+ */
+#define IGD_SURFACE_RENDER    0x00000001
+#define IGD_SURFACE_TEXTURE   0x00000002
+#define IGD_SURFACE_CURSOR    0x00000004
+#define IGD_SURFACE_OVERLAY   0x00000008
+#define IGD_SURFACE_DISPLAY   0x00000010
+#define IGD_SURFACE_DEPTH     0x00000020
+#define IGD_SURFACE_VIDEO     0x00000040
+#define IGD_SURFACE_VIDEO_ENCODE     0x00000080
+#define IGD_SURFACE_DRI2      0x00000100
+
+#define IGD_SURFACE_WALK_MASK 0x00001000
+#define IGD_SURFACE_YMAJOR    0x00001000
+#define IGD_SURFACE_XMAJOR    0x00000000
+#define IGD_SURFACE_TILED     0x00002000
+#define IGD_SURFACE_FENCED    0x00004000
+#define IGD_SURFACE_SYS_MEM   0x00008000
+#define IGD_SURFACE_PHYS_PTR  0x00010000
+#define IGD_SURFACE_CI		0x00020000
+#define IGD_SURFACE_CI_V4L2_MAP	0x00040000		/*will be set when map_method=0 without WA*/
+/*
+ * Mipmap flags are only valid on mipmap surfaces.
+ * Planes flags are only valid on volume surfaces.
+ */
+#define IGD_MIPLEVEL_SHIFT    16
+#define IGD_MIPLEVEL_MASK     (0xf<<IGD_MIPLEVEL_SHIFT)
+#define IGD_NUMPLANES_SHIFT   20
+#define IGD_NUMPLANES_MASK    (0x0ff00000UL)
+
+/*
+ * These flags exist in the Framebuffer info data structure also.
+ * They are here to insure that no overlap of flags happens.
+ */
+#define IGD_SURFACE_RESERVED_FLAGS 0xf0000000
+
+/*! @} */
+
+/*!
+ * @name Surface Allocation Types
+ * @anchor alloc_surface_types
+ *
+ * These types are used with the _igd_dispatch::alloc_surface() call to
+ * control the allocation behavior.
+ *
+ *  - IGD_GMM_ALLOC_TYPE_NORMAL Normal surface allocation. GMM will
+ *      determine the best tiling and fencing options and return that
+ *      information in the surface flags.
+ *  - IGD_GMM_ALLOC_TYPE_FIXED_TF Surface allocation will be done using
+ *      the tiled and fenced options from the surface flags. GMM will
+ *      not override these flags but will fail an allocation if the
+ *      tiling options would result in an unusable surface.
+ *  - IGD_GMM_ALLOC_TYPE_RESERVATION The offset provided in the call
+ *      was reserved earlier. The provided offset must be used. This
+ *      may be used with the Fixed bit enabled.
+ * @{
+ */
+#define IGD_GMM_ALLOC_TYPE_NORMAL       0x0000
+#define IGD_GMM_ALLOC_TYPE_FIXED_TF     0x0001
+#define IGD_GMM_ALLOC_TYPE_RESERVATION  0x0002
+
+/*! @} */
+
+/*!
+ * @name Mipmap layout type
+ * @anchor alloc_mipmap
+ *
+ * These constants are used when allocating surfaces to know how many
+ * offsets to expect GMM to return.
+ *
+ * - IGD_GMM_SURFACE_LAYOUT_RIGHT LOD1 be on the right of LOD0
+ * - IGD_GMM_SURFACE_LAYOUT_BELOW LOD1 is below LOD0
+ * - IGD_GMM_SURFACE_LAYOUT_LEGACY Traditional format, used by 
+ *   915 and below
+ * @{
+ */
+
+#define IGD_GMM_SURFACE_LAYOUT_RIGHT    0x0001
+#define IGD_GMM_SURFACE_LAYOUT_BELOW    0x0002
+#define IGD_GMM_SURFACE_LAYOUT_LEGACY   0x0003
+/*! @} */
+
+/*!
+ * @name Surface Allocation Constants
+ * @anchor alloc_surface_constants
+ *
+ * These constants are used when allocating surfaces to know how many
+ * offsets to expect GMM to return.
+ *
+ * - IGD_MAX_MIPLEVELS The maximum number of miplevels returned by GMM.
+ * - IGD_MAX_VOLUME_PLANES The maximum number of planes in a volume map.
+ * - IGD_MAX_VOLUME_SLICES The maximum number of slices in a mipped volume map.
+ *    There are 50% fewer planes with each successive mip level.
+ *    The number of slices can be calculated as,
+ *     (planes*2 - 1) - ((planes>>lod) - 1)
+ * @{
+ */
+#define IGD_MAX_MIPLEVELS 12
+#define IGD_MAX_VOLUME_PLANES 256
+#define IGD_MAX_VOLUME_SLICES 511
+/*! @} */
+
+/*!
+ * @name Region Alignment Flags
+ * @anchor alloc_region_flags
+ *
+ * @{
+ */
+#define IGD_GMM_REGION_ALIGN_MMAP       0x00000001
+#define IGD_GMM_REGION_ALIGN_SIZE       0x00000002 /* Size Aligned */
+#define IGD_GMM_REGION_ALIGN_CONTEXT    0x00000003
+#ifdef D3D_DPM_ALLOC
+#define IGD_GMM_DOUBLE_MAP              0x00000004 /* mapped to SGX/DPM space */
+#endif
+#define IGD_GMM_REGION_ALIGN_64K        0x00000010
+/*! @} */
+
+/*!
+ * @name Region Allocation Types
+ * @anchor alloc_region_types
+ *
+ * @{
+ */
+#define IGD_GMM_REGION_TYPE_DMA         0x00000001
+#define IGD_GMM_REGION_TYPE_RING        0x00000002
+#define IGD_GMM_REGION_TYPE_OVLREG      0x00000003
+#define IGD_GMM_REGION_TYPE_CONTEXT     0x00000004
+#define IGD_GMM_REGION_TYPE_HWSTATUS    0x00000005
+#define IGD_GMM_REGION_TYPE_BPL			0x00000006
+#define IGD_GMM_REGION_TYPE_PERSISTENT	0x00000007
+#define IGD_GMM_REGION_TYPE_OVLREG64	0x00000008
+#define IGD_GMM_REGION_TYPE_RANDEC0		0x00000009
+#define IGD_GMM_REGION_TYPE_RANDEC1		0x0000000A
+/*! @} */
+
+/*!
+ * @name Reservation Flags
+ * @anchor alloc_reservation_flags
+ *
+ * @{
+ */
+#define IGD_GMM_RESERVATION_FENCED       0x00000001
+/*! @} */
+
+
+/*!
+ * @brief Memory stats returned from _igd_dispatch::gmm_memstat()
+ *
+ *  This data structure is used to retrieve the memory usage statistics
+ *  from the HAL with a dispatch->gmm_memstat() function call.
+ */
+typedef struct _igd_memstat {
+	/*! @brief The total memory space available (Bytes) */
+	unsigned long total_avail;
+	/*! @brief Total memory used (Bytes) */
+	unsigned long total_used;
+	/*! @brief Maximum offscreen memory allowed (Bytes) */
+	unsigned long offscreen_max;
+	/*! @brief The amount of offscreen memory currently used (Bytes) */
+	unsigned long offscreen_used;
+	unsigned long offscreen_static_used;
+} igd_memstat_t;
+
+
+
+/*!
+ * @name GMM Alloc Cached Flags
+ * @anchor gmm_alloc_cached_flags
+ *
+ * These flags are used with the _igd_dispatch::gmm_alloc_cached() dispatch
+ * function.
+ *
+ * - IGD_GMM_FLUSH_ALL: Flush out all cached surfaces if needed.
+ * - 0: Flush out no surfaces.
+ *
+ * @{
+ */
+#define IGD_GMM_FLUSH_ALL    0x1
+/*! @} */
+
+typedef unsigned long (*_gmm_get_pvtheap_size)(void);
+typedef unsigned long (*_gmm_gmm_get_cache_mem)(void);
+
+typedef int (*_gmm_alloc_surface_fn_t)(
+	unsigned long *offset,
+	unsigned long pixel_format,
+	unsigned int *width,
+	unsigned int *height,
+	unsigned int *pitch,
+	unsigned long *size,
+	unsigned int type,
+	unsigned long *flags);
+
+typedef int (*_gmm_get_num_surface_fn_t)(
+	unsigned long *count);
+
+typedef int (*_gmm_get_surface_list_fn_t)(
+	unsigned long allocated_size,
+	unsigned long *list_size,
+	igd_surface_list_t **surface_list);
+
+typedef int (*_gmm_alloc_region_fn_t)(
+	unsigned long *offset,
+	unsigned long *size,
+	unsigned int type,
+	unsigned long flags);
+
+typedef int (*_gmm_virt_to_phys_fn_t)(
+	unsigned long offset,
+	unsigned long *physical);
+
+typedef void (*_gmm_free_fn_t)(unsigned long offset);
+
+typedef int (*_gmm_memstat_fn_t)(igd_memstat_t *memstat);
+
+typedef int (*_gmm_alloc_cached_fn_t)(igd_display_h display_handle,
+	igd_surface_t *surface, unsigned int flags);
+
+typedef void (*_gmm_free_cached_fn_t)(igd_display_h display_handle,
+	igd_surface_t *surface,
+	unsigned long sync_id);
+
+typedef int (*_gmm_alloc_cached_region_fn_t)(igd_display_h display_handle,
+	unsigned long *offset, 
+	unsigned long *size,
+	unsigned int type,
+	unsigned int region_flags,
+	unsigned int flags);
+
+typedef void (*_gmm_free_cached_region_fn_t)(igd_display_h display_handle,
+	unsigned long offset, 
+	unsigned long size,
+	unsigned int type,
+	unsigned int region_flags,
+	unsigned long sync_id_write,
+	unsigned long sync_id_read);
+
+typedef int (*_gmm_flush_cache_fn_t)(void);
+
+
+/*
+ * This interface is used in debug driver only. An IAL or HAL component
+ * may point this at a string (char[80]) prior to a surface/region
+ * allocation. A copy of this string will be stored with the allocation
+ * information and will be seen in a GMM dump.
+ */
+#define GMM_DEBUG_DESC_SIZE 80
+extern char gmm_debug_desc[];
+
+#if defined(DEBUG_BUILD_TYPE) && defined(CONFIG_GMM)
+
+/*
+ * This function is only used to copy the debug description name into
+ * the debug string. This way we don't have to worry about a strcpy
+ * being available.
+ * Note: This function is not a real strcpy. It is hardcoded for the
+ * needs of the gmm debug string copy.
+ */
+static __inline void _gmm_strcpy(const char *src)
+{
+	int i;
+
+	for(i=0; i<GMM_DEBUG_DESC_SIZE; i++) {
+		gmm_debug_desc[i] = src[i];
+		if(!gmm_debug_desc[i]) {
+			return;
+		}
+	}
+	gmm_debug_desc[GMM_DEBUG_DESC_SIZE-1] = '\0';
+	return;
+}
+
+static __inline void _gmm_strcpy2(char *dest, const char *src)
+{
+	int i;
+
+	for(i=0; i<GMM_DEBUG_DESC_SIZE; i++) {
+		dest[i] = src[i];
+		if(!dest[i]) {
+			return;
+		}
+	}
+	dest[GMM_DEBUG_DESC_SIZE-1] = '\0';
+	return;
+}
+
+#define GMM_SET_DEBUG_NAME(src) _gmm_strcpy(src)
+#define GMM_SET_DEBUG_NAME2(dest, src) _gmm_strcpy2(dest, src)
+#else
+#define GMM_SET_DEBUG_NAME(src)
+#define GMM_SET_DEBUG_NAME2(dest, src)
+#endif
+
+/*! @} */
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_init.h b/drivers/gpu/drm/emgd/include/igd_init.h
new file mode 100644
index 0000000..47045b6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_init.h
@@ -0,0 +1,895 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_init.h
+ * $Revision: 1.15 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_INIT_H_
+#define _IGD_INIT_H_
+
+#include <igd.h>
+
+
+/*!
+ * @addtogroup init_group
+ *
+ * The init module contains the entry points and data structures
+ * necessary to initialize the graphics driver HAL.
+ * The IAL will call several initialization functions to bring the HAL
+ * to a usable state, after which, the HAL is accessed through a dispatch
+ * table (data structure of function pointers).
+ *
+ * Typical initialization will follow these steps:
+ * - Initialize the HAL by calling igd_driver_init() to
+ *   detect supported chipsets without altering the hardware. If a
+ *   supported device is found the _igd_init_info data structure will be
+ *   populated indicating the chipset name and PCI information for the device.
+ *   A driver handle that should be used during the rest of the
+ *   initialization process will be returned.
+ *
+ * - Call igd_driver_config() to prepare the hardware for use. Until this
+ *   point there has been no alteration of the hardware. After the
+ *   driver has been configured the igd_get_config_info() function can
+ *   be used. Only a subset of the _igd_config_info data will be available
+ *   at this time as the modular HAL components have not been initialized.
+ *
+ * - Call igd_module_init() to initialize the modular components of the
+ *   driver. This function accepts a set of parameters that can alter
+ *   default behavior. A provided dispatch table will be populated with
+ *   the entry points used for hardware operations during the life of
+ *   the driver. igd_get_config_info() may now be used to return full
+ *   configuration details.
+ *
+ * @{
+ */
+
+/*!
+ * @brief Data populated by igd_driver_init().
+ *
+ * The init info is used as input/output to igd_driver_init(). Any non-zero
+ * members (with the exception of the name) will be used to limit the search
+ * for available devices, if all members are zero the first supported
+ * device found on the system will be used. In either case the data returned
+ * will reflect the to-be-controlled device found by the HAL.
+ *
+ * The name is a ASCII string that may be used in human readable output
+ * dialogs.
+ */
+typedef struct _igd_init_info {
+	/*! @brief PCI Vendor ID */
+	unsigned int vendor_id;
+	/*! @brief PCI Device ID */
+	unsigned int device_id;
+	/*! @brief PCI Bus ID */
+	unsigned int bus;
+	/*! @brief PCI Slot ID */
+	unsigned int slot;
+	/*! @brief PCI Function ID */
+	unsigned int func;
+	/*! @brief ASCII chipset name */
+	char *name;
+	/*! @brief ASCII chipset ID */
+	char *chipset;
+	/*! @brief ASCII default port driver list */
+	char *default_pd_list;
+} igd_init_info_t;
+
+/*!
+ * @brief Function to initialize the HAL and detect supported chipset.
+ *
+ * Initialize the driver and determine if the hardware is supported without
+ * altering the hardware state in any way. Init info is populated based on
+ * collected data.
+ *
+ * A driver handle is returned. This handle is an opaque data structure
+ * used with future HAL calls. The contents or meaning of the value are
+ * not known outside the HAL; however, a NULL value should be considered
+ * a failure.
+ *
+ * @param init_info Device details returned from the init process. This
+ *   structure will be populated by the HAL during the call.
+ *
+ * @return igd_driver_h (Non-NULL) on Success
+ * @return NULL on failure
+ */
+igd_driver_h igd_driver_init(igd_init_info_t *init_info);
+
+
+/*!
+ *  Configure the driver. This sets the driver for future operation, it
+ * may alter the hardware state. Must be called after driver init and
+ * before ANY other driver commands.
+ *
+ * @param driver_handle: A driver handle as returned by igd_driver_init()
+ *
+ * @return <0 on Error
+ * @return 0 On Success
+ */
+int igd_driver_config(igd_driver_h driver_handle);
+
+
+
+/*!
+ * @defgroup init_param Initialization Parameters
+ *
+ * Initialization parameters are passed to igd_module_init() to control
+ * configurable behavior of the HAL. Some parameters are global in that
+ * they have an effect on the entire driver. In addition, there are
+ * parameters that are specific to a display. These parameters are provided
+ * in a list with each list entry representing a defined "port number" as
+ * follows shown here:
+ *
+<PRE>
+                                        /|-----------|
+                                       / | Fp Info   |
+                                      /  |-----------|
+        |----------|   /|---------|  /
+        | Params   |  / | Display | /    |-----------|
+        |          | /  | Param 1 |/     | DTD Array |
+        |          |/   |---------|\     |-----------|
+        |----------|\               \
+                     \  |---------|  \   |-----------|
+                      \ | Display |   \  | Attribute |
+                       \| Param N |    \ | Array     |
+                        |---------|     \|-----------|
+</PRE>
+ *
+ * The port numbers are mapped to hardware specific outputs as defined
+ * here:
+ *
+ * 830M and later Port numbers:
+ *   - 1 DVO A port
+ *   - 2 DVO B port
+ *   - 3 DVO C port
+ *   - 4 Internal LVDS port
+ *   - 5 Analog port
+ *
+ *   On 835: If RGBA is used (DVO B & C together), then use DVO B number
+ *   to specify any parameter for it.
+ *
+ * 810/815 Port numbers:
+ *   - 3 DVO port
+ *   - 5 Analog port
+ *
+ * NOTE: The permanence of the Params data structure that is passed from
+ * the IAL to the HAL in igd_module_init API is not guaranteed by the IAL
+ * Therefore, the different HAL modules should use the data out of this
+ * data structure only until igd_module_init and not after that.
+ * @{
+ */
+
+/*!
+ * This parameter, when set, will cause the driver to save the current
+ * register state of the device prior to altering it in any way. This
+ * will allow the state of the device to be reapplied at exit time.
+ */
+#define IGD_DRIVER_SAVE_RESTORE   0x01
+
+/*!
+ * @name Per-Display Present Params Flags
+ * @anchor present_params_flags
+ *
+ * These flags are used to identify which parameters are being provided to
+ * the HAL. If a bit is not set in the _igd_display_params::present_params
+ * variable the parameters will not be read by the HAL and the default
+ * behavior will be used instead.
+ *
+ * - IGD_PARAM_DDC_GPIO Parameter to select non-standard GPIO pair for DDC.
+ * - IGD_PARAM_DDC_SPEED Parameter to select non-standard DDC speed.
+ * - IGD_PARAM_DDC_DAB Parameter to select the Device Address Byte to use for
+ *     DDC communication
+ * - IGD_PARAM_I2C_GPIO Parameter to select non-standard GPIO pair for i2c.
+ * - IGD_PARAM_I2C_SPEED Parameter to select non-standard I2C speed.
+ * - IGD_PARAM_DAB Parameter to select the Device Address Byte to use for I2C
+ *     communication.
+ * - IGD_PARAM_FP_INFO Parameter to provide non-detectable Flat Panel
+ *     configuration data.
+ * - IGD_PARAM_DTD_LIST Parameter to provide a Detailed Timing List for a
+ *     display.
+ * - IGD_PARAM_ATTR_LIST Parameter to provide a set of Port Driver Attributes
+ *     to use during init.
+ *
+ * @{
+ */
+#define IGD_PARAM_DDC_GPIO        0x00000001
+#define IGD_PARAM_DDC_SPEED       0x00000002
+#define IGD_PARAM_DDC_DAB         0x00000004
+#define IGD_PARAM_I2C_GPIO        0x00000008
+#define IGD_PARAM_I2C_SPEED       0x00000010
+#define IGD_PARAM_DAB             0x00000020
+#define IGD_PARAM_FP_INFO         0x00000040
+#define IGD_PARAM_DTD_LIST        0x00000080
+#define IGD_PARAM_ATTR_LIST       0x00000100
+/*! @} */
+
+
+/*!
+ *
+ *
+ *
+ * @note Any changes to the above number assignment will break the driver.
+ *       If change is required, make sure to change the port numbers in
+ *       port tables.
+ *
+ */
+
+/* The method for controlling the flat panel power. The options include:
+ * no support or the Port Driver handles flat panel power. */
+#define IGD_PARAM_FP_PWR_METHOD_NONE 0
+#define IGD_PARAM_FP_PWR_METHOD_PD   1
+
+/*!
+ * @brief Port-specific color correction information
+ *
+ * Initialization parameter passed as part of igd_module_init() to provide
+ * information about an attached display device. One color_correct_info
+ * is provided for each display port.
+ *
+ */
+typedef struct _igd_param_color_correct_info {
+	/*! @brief RED value for GAMMA */
+	unsigned short gamma_r;
+	/*! @brief GREEN value for GAMMA */
+	unsigned short gamma_g;
+	/*! @brief BLUE value for GAMMA */
+	unsigned short gamma_b;
+	/*! @brief RED value for BRIGHTNESS */
+	unsigned short brightness_r;
+	/*! @brief GREEN value for BRIGHTNESS */
+	unsigned short brightness_g;
+	/*! @brief BLUE value for BRIGHTNESS */
+	unsigned short brightness_b;
+	/*! @brief RED value for CONTRAST */
+	unsigned short constrast_r;
+	/*! @brief GREEN value for CONTRAST */
+	unsigned short constrast_g;
+	/*! @brief BLUE value for CONTRAST */
+	unsigned short constrast_b;
+} igd_param_color_correct_info_t;
+
+
+
+/*!
+ * @brief Flat Panel Information
+ *
+ * Initialization parameter passed as part of igd_module_init() to provide
+ * information about an attached Flat Panel (typically LVDS). One fp_info
+ * is provided for each display port, and is only used when the
+ * IGD_PARAM_FP_INFO flag is set in the _igd_display_params::present_params
+ *
+ */
+typedef struct _igd_param_fp_info {
+	/*! @brief Flat panel width */
+	unsigned long fp_width;
+	/*! @brief Flat panel height */
+	unsigned long fp_height;
+	/*! @brief Flat Panel Power Method */
+	unsigned long fp_pwr_method;
+	/*!
+	 * @brief Min time delay in miliseconds between VDD active and clock/data
+	 * active.
+	 */
+	unsigned long fp_pwr_t1;
+	/*!
+	 * @brief Min time delay in miliseconds between clock/data active and
+	 * backlight enable.
+	 */
+	unsigned long fp_pwr_t2;
+	/*!
+	 * @brief Min time delay in miliseconds between Backlight disable and
+	 * clock/data inactive
+	 */
+	unsigned long  fp_pwr_t3;
+	/*!
+	 * @brief Min time delay in miliseconds between clock/data inactive and
+	 * VDD inactive.
+	 */
+	unsigned long  fp_pwr_t4;
+	/*!
+	 * @brief Min time delay in miliseconds between VDD inactive and
+	 * VDD active.
+	 */
+	unsigned long  fp_pwr_t5;
+} igd_param_fp_info_t;
+
+/*!
+ * @brief Per-display init-time list of DTDs.
+ *
+ * In the case of EDID-less display device, this parameter provides
+ * the DTD (Detailed Timing Descriptor) list to the HAL during initialization.
+ */
+typedef struct _igd_param_dtd_list {
+	/*! @brief number of DTDs */
+	unsigned long  num_dtds;
+	/*! @brief DTD list */
+	igd_display_info_t  *dtd;
+} igd_param_dtd_list_t;
+
+/*!
+ * @brief Port driver attribute
+ */
+typedef struct _igd_param_attr {
+	/*! @brief See @ref attr_id_defs for predefined IDs */
+	unsigned long  id;
+	/*! @brief value or index(incase of list type attr) */
+	unsigned long  value;
+} igd_param_attr_t;
+
+/*!
+ * @brief Port driver init-time attribute list
+ *
+ * This data structure is used during HAL initialization. It should be
+ * populated with any port driver attributes that were modified and saved
+ * in a prior use of the HAL.
+ */
+typedef struct _igd_param_attr_list {
+	/*! @brief  number of attributes in the list */
+	unsigned long  num_attrs;
+	/*! @brief IAL allocated attribute list */
+	igd_param_attr_t *attr;
+} igd_param_attr_list_t;
+
+
+
+/*!
+ * @brief Per-Display Init-time configuration parameters.
+ *
+ * These parameters are provided as an array during igd_module_init().
+ * They allow the default behavior of the driver to be altered on a
+ * per-display basis. They are input-only to provide data
+ * about non-standard hardware configurations and persistant driver state.
+ */
+typedef struct _igd_display_params {
+	/*! @brief Port to which these parameters apply. See @ref init_param */
+	unsigned long port_number;
+	/*! @brief parameters present bitfield: See @ref present_params_flags */
+	unsigned long present_params;
+	unsigned long flags;
+	/*!
+	 * @brief Timing sources to use with Edid displays.
+	 * See @ref advanced_edid
+	 */
+	unsigned short edid_avail;
+	/*!
+	 * @brief Timings sources to use with Edid-less displays.
+	 * See @ref advanced_edid
+	 */
+	unsigned short edid_not_avail;
+	unsigned long ddc_gpio;       /* DDC GPIO pin pair number 0..6 */
+	unsigned long ddc_speed;      /* DDC speed in KHz to read EDID */
+	unsigned long ddc_dab;        /* DDC DAB to read EDID from display device */
+	unsigned long i2c_gpio;       /* I2C GPIO pin pair number 0..6 */
+	unsigned long i2c_speed;      /* I2C speed in KHz to read EDID */
+	unsigned long i2c_dab;        /* I2C DAB to communicate with DVO device */
+	igd_param_fp_info_t fp_info;  /* Connected FP Info */
+	/*!
+	 * DTD parameter: Incase of EDID-less display device, this parameter
+	 * provides the DTD (Detailed Timing Descriptor) list
+	 */
+	igd_param_dtd_list_t dtd_list;
+
+	igd_param_attr_list_t attr_list;
+} igd_display_params_t;
+
+/*!
+ * @name Advanced Edid Flags
+ * @anchor advanced_edid
+ *
+ * Flags to use with edid_avail and edid_not_avail:
+ * perport edid_avail/edid_not_avail
+ *
+ * IGD_DISPLAY_USE_STD_TIMINGS:
+ *   If not set: Do not use driver built-in standard timings
+ *   If set:     Use driver built-in standard timings
+ *
+ * IGD_DISPLAY_USE_EDID:  not applicable to edid_not_avail
+ *   If not set: Do not use EDID block
+ *   If set:     Use EDID block and filter modes
+ *
+ * IGD_DISPLAY_USE_USERDTDS:
+ *   If not set: Do not use user-DTDs
+ *   If set:     Use user-DTDs.
+ *
+ * @{
+ */
+#define IGD_DISPLAY_USE_STD_TIMINGS    0x1
+#define IGD_DISPLAY_USE_EDID           0x2
+#define IGD_DISPLAY_USE_USERDTDS       0x4
+/*! @} */
+
+
+/* Flags for igd_display_params_t->flags*/
+#define IGD_DISPLAY_READ_EDID     0x00000001  /* igd_display_params_t.flags */
+
+/*!
+ * @anchor display_flags_def
+ *
+ * These flags may be used in the display_flags member of the
+ * igd_params_t data structure.
+ *
+ */
+#define IGD_DISPLAY_MULTI_DVO     0x00000002  /* igd_param_t.display_flags  */
+#define IGD_DISPLAY_DETECT        0x00000004  /* igd_param_t.display_flags  */
+#define IGD_DISPLAY_FB_BLEND_OVL  0x00000008  /* igd_param_t.display_flags  */
+#define IGD_DISPLAY_BATCH_BLITS   0x00000010
+	/*! @brief enable dynamic blending of display frame buffer with overlay */
+
+
+
+/*!
+ * @brief Init-Time Driver Parameters
+ *
+ * The igd_param_t data structure contains  global init-time static parameters
+ * to impact the behavior of the whole driver. They are provided to the driver
+ * during module initialization to control overall driver behavior.
+ *
+ *
+ *           VERY IMPORTANT!!! CHANGES TO DEFINITION OF THIS STRUCTURE AND
+ *           ITS MEMBER STRUCTURES MUST BE FOLLOWED BY UPDATES TO THE
+ *           FOLLOWING COMPONENTS / FILES:
+ *                       1. ssigd/ial/vbios/usrbld/igd_uinit.h
+ *                       2. ssigd/ial/vbios/src/core/user_config.c
+ *                       3. pcf2iegd tool (gens user_config.c/h for usr-bld)
+ *                       4. CED tool (generates user_config.c/h for usr-bld)
+ *
+ *
+ *
+ * See igd_module_init()
+ */
+typedef struct _igd_param {
+	/*! @brief Maximum pages taken by the driver for offscreen memory */
+	unsigned long        page_request;
+	/*! @brief Maximum pages reserved for the framebuffer. */
+	unsigned long        max_fb_size;
+	/*! @brief Boolean option to preserve initial hardware state */
+	unsigned char        preserve_regs;
+	/*! @ref display_flags_def "Display Flags" */
+	unsigned long        display_flags;
+	/*!
+	 * @brief Port detection order.
+	 *
+	 * HAL will detect the displays in the  order mentioned and also allocate
+	 * in the same order if found.
+	 *
+	 * Example: To detect/allocate in the order on 855:
+	 * -# DVOB PORT
+	 * -# ANALOG PORT
+	 * -# LVDS PORT
+	 * then specify port_number[] = {2, 5, 4, 0};
+	 *
+	 * @note Set port_number[] = {0, 0, 0, 0}; to work in the default order.
+	 *
+	 * @note If a invalid number is specified then it will be skipped.
+	 */
+	unsigned long port_order[IGD_MAX_PORTS];
+
+	/*! Contains the Per-Display init-time parameters. */
+	igd_display_params_t display_params[IGD_MAX_PORTS];
+
+	/*! @brief RGB color that will be used while clearing the framebuffer */
+	unsigned long        display_color;
+
+    unsigned long       quickboot;
+    int                 qb_seamless;
+    unsigned long       qb_video_input;
+    int                 qb_splash;
+
+	/*! Override interrupt support and revert to polling */
+	int                 polling;
+	unsigned long 		ref_freq;
+	int 				tuning_wa;
+	unsigned long 		clip_hw_fix;
+
+	
+	/* Async flip flickering workaround enable */
+	unsigned long		async_flip_wa;
+	
+	/*
+	 * Enable override of following registers when en_reg_override=1.
+	 * Display Arbitration, FIFO Watermark Control, GVD HP_CONTROL,
+	 * Bunit Chickenbits, Bunit Write Flush, Display Chickenbits
+	 */
+	unsigned long		en_reg_override;
+	unsigned long		disp_arb;
+	unsigned long		fifo_watermark1;
+	unsigned long		fifo_watermark2;
+	unsigned long		fifo_watermark3;
+	unsigned long		fifo_watermark4;
+	unsigned long		fifo_watermark5;
+	unsigned long		fifo_watermark6;
+	unsigned long		gvd_hp_control;
+	unsigned long		bunit_chicken_bits;
+	unsigned long		bunit_write_flush;
+	unsigned long		disp_chicken_bits;
+	int					punt_to_3dblit;
+
+} igd_param_t;
+
+typedef struct {
+	unsigned debug : 1;
+	unsigned ddk_version : 16;
+	unsigned emgd_version : 15;
+} igd_build_config_t;
+
+/*! @} */
+
+/*!
+ *
+ *  Initializes individual modules to a runable state. Init time parameters
+ * may be provided to alter the default behavior of the driver.
+ * See @ref init_param
+ *
+ * The dispatch table for all graphics operations is returned. The dispatch
+ * table may return NULL pointers for unsupported functions due to
+ * optional modules. This dispatch table is used to access HAL functionality
+ * throughout the life of the driver.
+ * See @ref _igd_dispatch
+ *
+ * @param driver_handle as returned from igd_driver_init().
+ * @param dsp dispatch table to be populated during the call.
+ * @param params Input parameters to alter default behavior.
+ *   See @ref init_param
+ *
+ * @return 0 Success
+ * @return <0 on Error
+ */
+int igd_module_init(igd_driver_h driver_handle,
+	igd_dispatch_t **dsp,
+	igd_param_t *params);
+
+
+
+/*!
+ * @name Framebuffer Capabilities
+ * @anchor fb_caps
+ *
+ * FB caps are an indication of large feature sets that will or will not
+ * be available based on FB pixel format. This allows an IAL to intelligently
+ * configure itself to the appropriate setup before setting a display
+ * mode.
+ * FB caps are returned from a call to igd_get_config_info().
+ *
+ * - IGD_CAP_BASIC_2D This capability bit indicates that the device is capable
+ *    of performing basic 2d acceleration.
+ * - IGD_CAP_FULL_2D This capability bit indicates that the device is capable
+ *    of performing full 2d acceleration.
+ * - IGD_CAP_BLEND This capability bit indicates that the device is capable of
+ *    performing the blend() function to the framebuffer.
+ *
+ * @{
+ */
+#define IGD_CAP_BASIC_2D  0x01
+#define IGD_CAP_FULL_2D  (0x02 | IGD_CAP_BASIC_2D)
+#define IGD_CAP_BLEND     0x04
+/*! @} */
+
+
+/*!
+ * @brief Device Capabilities based on FB mode
+ *
+ * This structure reports the Framebuffer capabilities on a pixel format
+ * basis. An IAL should check these capabilities before using acceleration
+ * features. This structure is returned as part of the _igd_config_info
+ * information from the igd_get_config_info() call.
+ *
+ * @see pixel_formats
+ */
+typedef struct _igd_fb_caps {
+	/*! @brief The framebuffer pixel format that these caps reference */
+	unsigned long pixel_format;
+	/*! @brief The capability bits as defined by @ref fb_caps */
+	unsigned long caps;
+} igd_fb_caps_t;
+
+/*!
+ * @brief Static HAL configuration data
+ *
+ * Config info contains the static configuration information for the device
+ * that is found during the call to igd_driver_config(). It is obtained
+ * with a call to igd_get_config_info() which may be done before or after
+ * module configuration with limited success.
+ */
+typedef struct _igd_config_info {
+	/*! @brief The Base MMIO physical (Bus) address. */
+	unsigned long mmio_base_phys;
+	/*! @brief The Base MMIO virtual address. */
+	unsigned char *mmio_base_virt;
+	/*! @brief The Base Video Memory physical (Bus) address. */
+	unsigned long gtt_memory_base_phys;
+	/*! @brief The Base Video Memory virtual address. */
+	unsigned char *gtt_memory_base_virt;
+	/*! @brief The Video Memory Size. */
+	unsigned long gtt_memory_size;
+	/*! @brief The number of display planes supported by the device. */
+	unsigned long num_dsp_planes;
+	/*! @brief The number of display pipes supported by the device. */
+	unsigned long num_dsp_pipes;
+	/*! @brief The number of currently active display ports. */
+	unsigned long num_act_dsp_ports;
+	/*! @brief Caps, terminated with PF = 0 */
+	igd_fb_caps_t *fb_caps;
+	/*! @brief The device revision id. */
+	unsigned long revision_id;
+	/*! @brief HW status page offset (priviledged use only) */
+	unsigned long hw_status_offset;
+	/*! @brief The base of stolen memory */
+	unsigned long stolen_memory_base_virt;
+	/* pixel format that matches the bpp that was passed in */
+	unsigned long pixel_format;
+	/* port-specific rotation read from DisplayID */
+	igd_DID_rotation_info_t displayid_rotation[IGD_MAX_PORTS];
+} igd_config_info_t;
+
+
+/*!
+ * @brief Static GTT configuration data
+ *
+ * This holds the configuration of the GTT and the information
+ * required to handle video memory allocation/free.
+ *
+ * FIXME: There is a lot of duplication with information stored
+ * else were (like igd_config_info_t) that needs to be cleaned up.
+ */
+typedef struct _igd_gtt_info_t {
+	unsigned long gatt_start;
+	unsigned long gatt_pages;
+	unsigned long gtt_start;
+	unsigned long gtt_pages;
+	unsigned long gtt_phys_start;
+	unsigned long stolen_mem_base;
+	unsigned long stolen_mem_size;
+	unsigned long stolen_pages;
+	unsigned long pge_ctl;
+	unsigned short gmch_ctl;
+	unsigned long *gtt_mmap;
+	void *vram_virt;
+	unsigned long initialzied;
+	void *scratch_page;
+} igd_gtt_info_t;
+
+
+
+/*!
+ * The igd_get_config_info() function call can be used to get static
+ * configuration information details that the IAL may need. This
+ * function may be called prior to igd_module_init() with limited
+ * success; however, it is recommeneded that it be called after the
+ * modular HAL components have been initialized.
+ *
+ * In addition to static configuration there may be dynamic configuration
+ * details that can be queried at any time after igd_driver_config().
+ * These are defined with unique identifiers and are queried one at a
+ * time with igd_get_param().
+ *
+ *  @param driver_handle as retuned from igd_driver_init()
+ *  @param config_info Populated with hardware information during the call..
+ *
+ *  @return 0 Success
+ *  @return -IGD_INVAL Error
+ */
+int igd_get_config_info(igd_driver_h driver_handle,
+	igd_config_info_t *config_info);
+
+
+
+/*!
+ * @defgroup runtime_param Runtime Parameter Control
+ *
+ * HAL parameters may be queried and set at runtime using the
+ * _igd_dispatch::get_param() and _igd_dispatch::set_param() dispatch functions
+ * (when called after initialization) or the
+ * igd_get_param() and igd_set_param() functions when called during
+ * initialization.
+ * Some implemtations may not support all parameters so IAL's must handle
+ * errors. Additionally, some parameters are hardware specific and of no use to
+ * general purpose IALs. These should only be used by IALs with extensive
+ * hardware knowledge (OpenGL, D3D, etc)
+ *
+ * @{
+ */
+
+/*!
+ * @name Runtime Parameter Defines
+ *
+ * - IGD_PARAM_PANEL_ID Primary Panel ID
+ *    This is a unique Flat Panel Identifier that is obtained from firmware.
+ *    The IAL may use this information to send in Detailed Timing Descriptors
+ *    to igd_module_init(). This facilitates the use of multiple local Flat
+ *    panel configurations without EDID. When available this parameter may
+ *    be queried prior to igd_module_init().
+ * - IGD_PARAM_MEM_RESERVATION Bios memory reservation.
+ *    This is the amount (in bytes?) of memory that the bios has already
+ *    installed in the Gart prior to driver load. It is used when a
+ *    non-standard bios or firmware has pre-allocated an undetectable amount
+ *    of video memory for splash screen use. When available this parameter can
+ *    be queried prior to igd_module_init().
+ * - IGD_PARAM_DEBUG_MASK Debug Printing Mask.
+ *    This parameter is read/write and may be altered at any time after
+ *    igd_driver_init(). It is a bitfield to control different printing
+ *    groups for debug builds.
+ * - IGD_PARAM_PORT_LIST A bitfield used by the vBIOS to override the
+ *    display detect capabilities in the driver.
+ * - IGD_PARAM_HW_CONFIG Hardware configuration Bitfield
+ *    contains one of these dependeing on platform:
+ *    IGD_ALM_HW_CONFIG_BIN or IGD_NAP_HW_CONFIG_BIN both of which indicate
+ *    the presence and availability of the hardware binner. This value should
+ *    be queried and set (removing the bit) by an IAL claiming use of the
+ *    binner.
+ * - IGD_PARAM_INTR_STATUS Unknown FIXME document this
+ *
+ * @{
+ */
+#define IGD_PARAM_PANEL_ID        0x01
+#define IGD_PARAM_MEM_RESERVATION 0x02
+#define IGD_PARAM_DEBUG_MASK 0x03
+#define IGD_PARAM_PORT_LIST  0x04
+#define IGD_PARAM_GFX_FREQ  0x05
+#define IGD_PARAM_SET_LVDS  0x06
+#define IGD_PARAM_HW_CONFIG 0x1000
+#define  IGD_ALM_HW_CONFIG_BIN 0x1
+#define  IGD_NAP_HW_CONFIG_BIN 0x1
+#define IGD_PARAM_INTR_STATUS 0x1001
+
+/*! @} */
+
+/*!
+ * Gets the value of a runtime driver parameter. These parameters are
+ * each defined with a unique ID and may be altered at runtime.
+ *
+ * Note: There is a wrapper for this function in the dispatch table that
+ * takes a display instead of a driver handle. This version is for use
+ * when displays are not yet available.
+ *
+ * @bug Runtime parameter documentation needs updates
+ *
+ * @return 0 Success
+ * @return -IGD_INVAL Error
+ */
+int igd_get_param(igd_driver_h driver_handle,
+	unsigned long id,
+	unsigned long *value);
+
+/*!
+ * Sets the value of a runtime driver parameter. These parameters are
+ * each defined with a unique ID and may be altered at runtime.
+ *
+ * Note: There is a wrapper for this function in the dispatch table that
+ * takes a display instead of a driver handle. This version is for use
+ * when displays are not yet available.
+ *
+ * @return 0 Success
+ * @return  -IGD_INVAL Error
+ */
+int igd_set_param(igd_driver_h driver_handle,
+	unsigned long id,
+	unsigned long value);
+
+/*! @} Runtime Param Group */
+/*! @} Init Group */
+
+
+/*!
+ * @ingroup cleanup
+ * @brief Shuts down the HAL
+ *
+ * This function shuts down the HAL and frees and remaining resources.
+ * It should be called at driver exit to leave the hardware in a safe
+ * configuration.
+ *
+ * @param driver_handle The driver handle returned from igd_driver_init().
+ *
+ * @return void
+ */
+void igd_driver_shutdown(igd_driver_h driver_handle);
+
+/*!
+ * @ingroup cleanup
+ * @brief Shuts down the HAL
+ *
+ * This function shuts down the HAL and frees and remaining resources.
+ * It should be called at driver exit to leave the hardware in a safe
+ * configuration.
+ *
+ * @param driver_handle The driver handle returned from igd_driver_init().
+ *
+ * @return void
+ */
+void igd_driver_shutdown_hal(igd_driver_h driver_handle);
+
+
+/*!
+ * @ingroup cleanup
+ * @brief Query 2D capability of the hardware.
+ *
+ * This function query the hardware whether given capabilities value 
+ * (caps_val) is supported by hardware.
+ *
+ * @param driver_handle The driver handle returned from igd_driver_init().
+ *
+ * @return void
+ */
+void igd_query_2d_caps_hwhint(igd_driver_h driver_handle, 
+			unsigned long caps_val,
+			unsigned long *status);
+
+
+
+/* 2D capabilities to query */
+#define IGD_2D_CAPS_BLT                 0
+  	 
+/* Output of 2D capabilities to query */
+#define IGD_2D_HW_DISABLE                       0
+#define IGD_2D_HW_ENABLE                        1
+#define IGD_2D_CAPS_UNKNOWN                     2
+  	 
+
+
+/*!
+ * @addtogroup power_group
+ * @{
+ */
+
+/*!
+ * @name Driver Save Flags
+ * @anchor driver_save_flags
+ *
+ * Flags for use with dispatch->driver_save()
+ * @{
+ */
+#define IGD_REG_SAVE_VGA       0x00001
+#define IGD_REG_SAVE_DAC       0x00002
+#define IGD_REG_SAVE_MMIO      0x00004
+#define IGD_REG_SAVE_RB        0x00008
+#define IGD_REG_SAVE_VGA_MEM   0x00010
+#define IGD_REG_SAVE_MODE      0x00020
+#define IGD_REG_SAVE_BACKLIGHT 0x00040
+#define IGD_REG_SAVE_3D        0x00080
+#define IGD_REG_SAVE_GTT       0x00100
+#define IGD_REG_SAVE_TYPE_REG  0x10000
+#define IGD_REG_SAVE_TYPE_CON  0x20000
+#define IGD_REG_SAVE_TYPE_MISC 0x40000
+#define IGD_REG_SAVE_TYPE_MASK 0xF0000
+
+#define IGD_REG_SAVE_ALL (IGD_REG_SAVE_VGA | IGD_REG_SAVE_DAC |  \
+		IGD_REG_SAVE_MMIO | IGD_REG_SAVE_RB | IGD_REG_SAVE_VGA_MEM | \
+		IGD_REG_SAVE_MODE | IGD_REG_SAVE_BACKLIGHT | IGD_REG_SAVE_3D | \
+		IGD_REG_SAVE_GTT )
+
+/*!
+ * @note: This macro does not save the mode (i2c) regs. You have to
+ * explicitly ask for that too if you want it.
+ */
+#define IGD_REG_SAVE_STATE (IGD_REG_SAVE_VGA | IGD_REG_SAVE_DAC | \
+		IGD_REG_SAVE_MMIO | IGD_REG_SAVE_RB | IGD_REG_SAVE_VGA_MEM | \
+		IGD_REG_SAVE_GTT )
+/*! @} */
+
+
+/*! @} */
+
+#endif /* _IGD_INIT_H_ */
diff --git a/drivers/gpu/drm/emgd/include/igd_interrupt.h b/drivers/gpu/drm/emgd/include/igd_interrupt.h
new file mode 100644
index 0000000..6886a20
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_interrupt.h
@@ -0,0 +1,300 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_interrupt.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  The is the IGD exported interrupt access functions. It should be included
+ *  by client drivers making use of interrupt functionality of IGD.
+ *  This file Should NOT be included by any IGD modules because in some
+ *  IGD environments the interrupt module will not be available.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_INTERRUPT_H
+#define _IGD_INTERRUPT_H
+
+#include <igd_mode.h>
+
+typedef struct _oal_callbacks {
+
+	void (*spin_lock_irqsave)(void *lock);
+	void (*spin_unlock_irqrestore)(void *lock);
+	void (*spin_lock_isr)(void *lock);
+	void (*spin_unlock_isr)(void *lock);
+	void (*spin_lock)(void *lock);
+	void (*spin_unlock)(void *lock);
+} oal_callbacks_t;
+
+#define CB_FUNC(a, b, c) if(a && a->b) { a->b(c); }
+
+/* Responses returned to the client from drm */
+#define INT_INVALID  -1
+#define INT_NOOP      0
+#define INT_CLEARED   1
+#define INT_HANDLED   2
+#define INT_READ      3
+#define INT_STORED    4
+#define INT_TIMEOUT   5
+
+/* Response from the ISR helper to the ISR routine */
+#define ISR_NEED_DPC  0
+#define ISR_HANDLED   1
+#define ISR_NOT_OURS  2
+
+typedef struct _interrupt_data {
+
+	void *irqmask_lock;
+
+	unsigned char *msvdx_reg;
+	unsigned long sgx_irq_mask;
+	unsigned long sgx_irq_mask2;
+	unsigned long vdc_irq_mask;
+	unsigned long msvdx_irq_mask;
+	int irq_enabled;
+
+	/* condition to wake up on */
+	unsigned int event_present;
+
+	/* interrupts that have already occured */
+	unsigned int out_vdc;
+
+	unsigned int out_sgx;
+	unsigned int out_sgx2;
+
+	unsigned int out_mtx;
+} interrupt_data_t;
+
+
+/*----------------------------------------------------------------------
+ * Typedef: igd_interrupt_handler_t
+ *
+ * Description:
+ *  This function type is used for OS independent interrupt handlers
+ *  that will be called by the hal to handle a requested interrupt.
+ *
+ * Members:
+ *
+ *----------------------------------------------------------------------
+ */
+typedef void (igd_interrupt_handler_t)(unsigned long type,
+	void *call_data,
+	void *user_data);
+
+
+/*----------------------------------------------------------------------
+ * Typedef: igd_interrupt_t
+ *
+ * Description:
+ *  An opaque handle to identify an interrupt to be handled
+ *
+ * Members:
+ *
+ *----------------------------------------------------------------------
+ */
+typedef void * igd_interrupt_t;
+
+#define IGD_INTERRUPT_VBLANK 0x1
+#define IGD_INTERRUPT_SYNC   0x2
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  isr_helper
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in doing all chipset specific
+ *  functionality like register sets when an ISR is fired
+ * Parameters:
+ *	void
+ * Returns:
+ *  int - 0 on the interrupt was handled and we need to queue a DPC for it
+ *      - 1 on the interrupt was handled but wasn't one to do further
+ *          processing on
+ *      - 2 on failure
+ *----------------------------------------------------------------------
+ */
+int isr_helper(void);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  unmask_int_helper
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in unmasking the Interrupt
+ *  registers for a particular chipset so that the Interrupts can start
+ *  firing
+ * Parameters:
+ *	void
+ * Returns:
+ *  int - 0 on SUCCESS
+ *      - 1 on FAILURE
+ *----------------------------------------------------------------------
+ */
+int unmask_int_helper(void);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  mask_int_helper
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in masking the Interrupt
+ *  registers for a particular chipset so that the Interrupts can stop
+ *  firing
+ * Parameters:
+ *	void
+ * Returns:
+ *  int - 0 on SUCCESS
+ *      - 1 on FAILURE
+ *----------------------------------------------------------------------
+ */
+int mask_int_helper(void);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  request_int_info_helper
+ *
+ * Description:
+ *  This function helps the OAL module to request interrupt information
+ *  and status
+ * Parameters:
+ *	void *int_info_data
+ * Returns:
+ *  int -  1 on Interrupt events supported and previous events occured
+ *      -  0 on Interrupt events supported but no previous events occured
+ *      - -1 on FAILURE or Interrupt events type not supported
+ *----------------------------------------------------------------------
+ */
+int request_int_info_helper(void *int_info_data);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  clear_occured_int_helper
+ *
+ * Description:
+ *  This function helps the OAL module to request interrupt information
+ *  and status
+ * Parameters:
+ *	void *int_info_data
+ * Returns:
+ *  void
+ *----------------------------------------------------------------------
+ */
+void clear_occured_int_helper(void *int_info_data);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  mask_int_helper
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in clearing the cached copy
+ *  of Interrupt data
+ * Parameters:
+ *	void
+ * Returns:
+ *	void
+ *----------------------------------------------------------------------
+ */
+void clear_interrupt_cache(void);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  dpc_helper
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in doing all chipset specific
+ *  functionality like register sets when an DPC is fired
+ * Parameters:
+ *  interrupt_data_t **int_data
+ * Returns:
+ *  int - 0 on SUCCESS
+ *      - 1 on FAILURE
+ *----------------------------------------------------------------------
+ */
+int dpc_helper(interrupt_data_t **int_data);
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  interrupt_init
+ *
+ * Description:
+ *  This function initializees the interrupt helper module
+ * Parameters:
+ *  unsigned short did - Device ID for main video device
+ *  oal_callbacks_t *oal_callbacks_table - Callback for
+ *              spin lock related functions
+ * Returns:
+ *  int - 0 on SUCCESS
+ *      - 1 on FAILURE
+ *----------------------------------------------------------------------
+ */
+int interrupt_init(unsigned short did,
+	oal_callbacks_t *oal_callbacks_table);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  pre_init_interrupt
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in doing all chipset specific
+ *  functionality like register sets before an ISR is initialized
+ * Parameters:
+ *  unsigned char *virt_mmadr - The MMIO base address
+ * Returns:
+ *  void
+ *----------------------------------------------------------------------
+ */
+void pre_init_interrupt(unsigned char *virt_mmadr);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  post_init_interrupt
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in doing all chipset specific
+ *  functionality like register sets after an ISR is initialized
+ * Parameters:
+ *  unsigned char *virt_mmadr - The MMIO base address
+ * Returns:
+ *  void
+ *----------------------------------------------------------------------
+ */
+void post_init_interrupt(unsigned char *virt_mmadr);
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  uninstall_interrupt
+ *
+ * Description:
+ *  This function helps the OAL/DRM module in doing all chipset specific
+ *  functionality like register sets for removing Interrupt support
+ * Parameters:
+ *  unsigned char *virt_mmadr - The MMIO base address
+ * Returns:
+ *  void
+ *----------------------------------------------------------------------
+ */
+void uninstall_interrupt(void);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_mode.h b/drivers/gpu/drm/emgd/include/igd_mode.h
new file mode 100644
index 0000000..a3e6da2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_mode.h
@@ -0,0 +1,935 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_mode.h
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_MODE_H_
+#define _IGD_MODE_H_
+
+/*!
+ * @defgroup pixel_formats Pixel Format Definitions
+ * @ingroup display_group
+ *
+ * Pixel Format Definitions used in FB info data structure and
+ * throughout IGD API functions. Pixel formats are comprised
+ * of a Depth and Colorspace component combined (OR'd) with
+ * a unique number. Pixel formats and their components are
+ * defined with the following defines.
+ *
+ * @{
+ */
+
+/*!
+ * This is a just to make use of the Pixel format explicit. This could
+ * be replaced with an ENUM in the future.
+ */
+typedef unsigned long igd_pf_t;
+
+/*!
+ * @name Masks
+ * Masks that may be used to seperate portions of the pixel format
+ * definitions.
+ *
+ * @note Pixel Formats are maintained such that
+ * (pixel_format & IGD_PF_FMT_MASK) >> IGD_PF_FMT_SHIFT
+ * will always provide a unique index that can be used in a
+ * lookup table.
+ * @{
+ */
+#define IGD_PF_MASK                             0x0000FFFF
+#define IGD_PF_TYPE_MASK                        0x0000FF00
+#define IGD_PF_DEPTH_MASK                       0x000000FF
+#define IGD_PF_FMT_MASK                         0x00FF0000
+#define IGD_PF_FMT_SHIFT                        16
+/*! @} */
+
+/*!
+ * @name Depths
+ * Pixel Format depths in Bits per pixel.
+ * @{
+ */
+#define PF_DEPTH_1                              0x00000001
+#define PF_DEPTH_2                              0x00000002
+#define PF_DEPTH_4                              0x00000004
+#define PF_DEPTH_8                              0x00000008
+#define PF_DEPTH_12                             0x0000000c
+#define PF_DEPTH_16                             0x00000010
+#define PF_DEPTH_24                             0x00000018
+#define PF_DEPTH_32                             0x00000020
+#define PF_DEPTH_64                             0x00000040
+/*! @} */
+
+/* Unknown Pixel Format */
+#define IGD_PF_UNKNOWN							0x00000000
+
+/*!
+ * @name Colorspace
+ * Colorspace components of the overall pixel format definition.
+ * Several types may be OR'd together.
+ * To check for equivalent types use  IGD_PF_TYPE(pf) == TYPE
+ *
+ * @{
+ */
+#define PF_TYPE_ALPHA                           0x00000100
+#define PF_TYPE_RGB                             0x00000200
+#define PF_TYPE_YUV                             0x00000400
+#define PF_TYPE_PLANAR                          0x00000800
+#define PF_TYPE_OTHER                           0x00001000
+#define PF_TYPE_RGB_XOR                         0x00002000 /* Cursor */
+#define PF_TYPE_COMP                            0x00004000 /* Compressed */
+
+#define PF_TYPE_ARGB (PF_TYPE_ALPHA | PF_TYPE_RGB)
+#define PF_TYPE_YUV_PACKED (PF_TYPE_YUV)
+#define PF_TYPE_YUV_PLANAR (PF_TYPE_YUV | PF_TYPE_PLANAR)
+
+/*! @} */
+
+/*!
+ * @name 8 Bit RGB Pixel Formats
+ * @note Depth is bits per index (8)
+ * @{
+*/
+#define IGD_PF_ARGB8_INDEXED     (PF_DEPTH_8  | PF_TYPE_ARGB | 0x00010000)
+#define IGD_PF_ARGB4_INDEXED     (PF_DEPTH_4  | PF_TYPE_ARGB | 0x00020000)
+/*! @} */
+
+/*!
+ * @name 16 Bit RGB Pixel Formats
+ * @note Depth is bits per pixel (16)
+ * @{
+ */
+#define IGD_PF_ARGB16_4444       (PF_DEPTH_16 | PF_TYPE_ARGB | 0x00030000)
+#define IGD_PF_ARGB16_1555       (PF_DEPTH_16 | PF_TYPE_ARGB | 0x00040000)
+#define IGD_PF_RGB16_565         (PF_DEPTH_16 | PF_TYPE_RGB  | 0x00050000)
+#define IGD_PF_xRGB16_555        (PF_DEPTH_16 | PF_TYPE_RGB  | 0x00060000)
+/*! @} */
+
+/*!
+ * @name 24 Bit RGB Pixel Formats
+ * @note Depth is bits per pixel (24)
+ * @{
+ */
+#define IGD_PF_RGB24             (PF_DEPTH_24 | PF_TYPE_RGB  | 0x00070000)
+/*! @} */
+
+/*!
+ * @name 32 Bit RGB Pixel Formats
+ * @note Depth is bits per pixel including unused bits (32).
+ * @{
+ */
+#define IGD_PF_xRGB32_8888       (PF_DEPTH_32 | PF_TYPE_RGB  | 0x00080000)
+#define IGD_PF_xRGB32            IGD_PF_xRGB32_8888
+#define IGD_PF_ARGB32            (PF_DEPTH_32 | PF_TYPE_ARGB | 0x00090000)
+#define IGD_PF_ARGB32_8888       IGD_PF_ARGB32
+#define IGD_PF_xBGR32_8888       (PF_DEPTH_32 | PF_TYPE_RGB  | 0x000a0000)
+#define IGD_PF_xBGR32            IGD_PF_xBGR32_8888
+#define IGD_PF_ABGR32            (PF_DEPTH_32 | PF_TYPE_ARGB | 0x000b0000)
+#define IGD_PF_ABGR32_8888       IGD_PF_ABGR32
+/*! @} */
+
+/*!
+ * @name YUV Packed Pixel Formats
+ * @note Depth is Effective bits per pixel
+ *
+ * @{
+ */
+#define IGD_PF_YUV422_PACKED_YUY2 (PF_DEPTH_16| PF_TYPE_YUV_PACKED| 0x000c0000)
+#define IGD_PF_YUV422_PACKED_YVYU (PF_DEPTH_16| PF_TYPE_YUV_PACKED| 0x000d0000)
+#define IGD_PF_YUV422_PACKED_UYVY (PF_DEPTH_16| PF_TYPE_YUV_PACKED| 0x000e0000)
+#define IGD_PF_YUV422_PACKED_VYUY (PF_DEPTH_16| PF_TYPE_YUV_PACKED| 0x000f0000)
+#define IGD_PF_YUV411_PACKED_Y41P (PF_DEPTH_12| PF_TYPE_YUV_PACKED| 0x00100000)
+#define IGD_PF_YUV444_PACKED_AYUV (PF_DEPTH_32| PF_TYPE_YUV_PACKED| 0x00340000)
+/*! @} */
+
+/*!
+ * @name YUV Planar Pixel Formats
+ * @note Depth is Y plane bits per pixel
+ * @{
+ */
+#define IGD_PF_YUV420_PLANAR_I420 (PF_DEPTH_8| PF_TYPE_YUV_PLANAR| 0x00110000)
+#define IGD_PF_YUV420_PLANAR_IYUV IGD_PF_YUV420_PLANAR_I420
+#define IGD_PF_YUV420_PLANAR_YV12 (PF_DEPTH_8| PF_TYPE_YUV_PLANAR| 0x00120000)
+#define IGD_PF_YUV410_PLANAR_YVU9 (PF_DEPTH_8| PF_TYPE_YUV_PLANAR| 0x00130000)
+#define IGD_PF_YUV420_PLANAR_NV12 (PF_DEPTH_8| PF_TYPE_YUV_PLANAR| 0x00140000)
+/*! @} */
+
+/*!
+ * @name Cursor Pixel Formats
+ * @{
+ *
+ * RGB_XOR_2: Palette = {Color 0, Color 1, Transparent, Invert Dest }
+ * RGB_T_2: Palette = {Color 0, Color 1, Transparent, Color 2 }
+ * RGB_2: Palette = {Color 0, Color 1, Color 2, Color 3 }
+ */
+#define IGD_PF_RGB_XOR_2      (PF_DEPTH_2  | PF_TYPE_RGB_XOR | 0x00150000)
+#define IGD_PF_RGB_T_2        (PF_DEPTH_2  | PF_TYPE_RGB     | 0x00160000)
+#define IGD_PF_RGB_2          (PF_DEPTH_2  | PF_TYPE_RGB     | 0x00170000)
+/*! @} */
+
+/*!
+ * @name Bump Pixel Formats
+ * @note Depth is bits per pixel
+ * @{
+ */
+#define IGD_PF_DVDU_88             (PF_DEPTH_16 | PF_TYPE_OTHER | 0x00180000)
+#define IGD_PF_LDVDU_655           (PF_DEPTH_16 | PF_TYPE_OTHER | 0x00190000)
+#define IGD_PF_xLDVDU_8888         (PF_DEPTH_32 | PF_TYPE_OTHER | 0x001a0000)
+/*! @} */
+
+/*!
+ * @name Compressed Pixel Formats
+ * @note Depth is effective bits per pixel
+ * @{
+ */
+#define IGD_PF_DXT1               (PF_DEPTH_4 | PF_TYPE_COMP | 0x001b0000)
+#define IGD_PF_DXT2               (PF_DEPTH_8 | PF_TYPE_COMP | 0x001c0000)
+#define IGD_PF_DXT3               (PF_DEPTH_8 | PF_TYPE_COMP | 0x001d0000)
+#define IGD_PF_DXT4               (PF_DEPTH_8 | PF_TYPE_COMP | 0x001e0000)
+#define IGD_PF_DXT5               (PF_DEPTH_8 | PF_TYPE_COMP | 0x001f0000)
+/*! @} */
+
+/*!
+ * @name Depth Buffer Formats
+ * @note Depth is bits per pixel
+ * @{
+ */
+#define IGD_PF_Z16               (PF_DEPTH_16 | PF_TYPE_OTHER | 0x00200000)
+#define IGD_PF_Z24               (PF_DEPTH_32 | PF_TYPE_OTHER | 0x00210000)
+#define IGD_PF_S8Z24             (PF_DEPTH_32 | PF_TYPE_OTHER | 0x00220000)
+/*! @} */
+
+/*!
+ * @name Other Pixel Formats
+ *
+ * - I8 8bit value replicated to all color channels.
+ * - L8 8bit value replicated to RGB color channels, Alpha is 1.0.
+ * - A8 8bit Alpha with RGB = 0.
+ * - AL88 8bit Alpha, 8bit color replicated to RGB channels.
+ * - AI44 4bit alpha 4bit palette color.
+ * - IA44 4bit alpha 4bit palette color.
+ * @{
+ */
+#define IGD_PF_I8               (PF_DEPTH_8 | PF_TYPE_OTHER | 0x00230000)
+#define IGD_PF_L8               (PF_DEPTH_8 | PF_TYPE_OTHER | 0x00240000)
+#define IGD_PF_A8               (PF_DEPTH_8 | PF_TYPE_ALPHA | 0x00250000)
+#define IGD_PF_AL88             (PF_DEPTH_16 | PF_TYPE_ALPHA | PF_TYPE_OTHER | 0x00260000)
+#define IGD_PF_AI44             (PF_DEPTH_8 | PF_TYPE_OTHER | 0x00270000)
+#define IGD_PF_IA44             (PF_DEPTH_8 | PF_TYPE_OTHER | 0x00280000)
+
+#define IGD_PF_L16              (PF_DEPTH_16 | PF_TYPE_OTHER | 0x00290000)
+#define IGD_PF_ARGB32_2101010   (PF_DEPTH_32 | PF_TYPE_ARGB  | 0x002a0000)
+#define IGD_PF_AWVU32_2101010	(PF_DEPTH_32 | PF_TYPE_OTHER | 0x002b0000)
+#define IGD_PF_QWVU32_8888		(PF_DEPTH_32 | PF_TYPE_OTHER | 0x002c0000)
+#define IGD_PF_GR32_1616        (PF_DEPTH_32 | PF_TYPE_OTHER | 0x002d0000)
+#define IGD_PF_VU32_1616        (PF_DEPTH_32 | PF_TYPE_OTHER | 0x002e0000)
+
+/*!
+ * @name Floating-Point formats
+ *
+ * - R16F Floating-point format 16-bits for the Red Channel
+ * - GR32_1616F 32-bit float format using 16 bits for red and green channel
+ * - R32F IEEE Floating-Point s23e8 32-bits for the Red Channel
+ * - ABGR32_16161616F - This is a 64-bit FP format.
+ * - Z32F 32-bit float 3D depth buffer format.
+ * @{
+ */
+#define IGD_PF_R16F                 (PF_DEPTH_16 | PF_TYPE_OTHER | 0x002f0000)
+#define IGD_PF_GR32_1616F           (PF_DEPTH_32 | PF_TYPE_OTHER | 0x00300000)
+#define IGD_PF_R32F                 (PF_DEPTH_32 | PF_TYPE_OTHER | 0x00310000)
+#define IGD_PF_ABGR64_16161616F     (PF_DEPTH_64 | PF_TYPE_ARGB  | 0x00320000)
+#define IGD_PF_Z32F                 (PF_DEPTH_32 | PF_TYPE_OTHER | 0x00330000)
+/*! @} */
+
+/*!
+ * @name IGD_PF_NEXT array length helper
+ *
+ * This helper should always be set to the next available PF id. In this
+ * manner any array that uses the id as an index can be defined as
+ * unsigned long lookup_table[IGD_PF_NEXT] = {...}; and will then generate
+ * compile warnings if the pixel format list length changes.
+ */
+#define IGD_PF_NEXT 0x35
+/*!
+ * @name Helper Macros
+ * @{
+ */
+
+/*! Gets bits per pixel from pixel format */
+#define IGD_PF_BPP(pf)   ((pf) & IGD_PF_DEPTH_MASK)
+/*! Gets bits per pixel from pixel format */
+#define IGD_PF_DEPTH(pf) IGD_PF_BPP(pf)
+/*! Gets Bytes per pixel from pixel format */
+#define IGD_PF_BYPP(pf)  ((IGD_PF_DEPTH(pf)+0x7)>>3)
+/*! Gets Bytes required for line of pixels */
+#define IGD_PF_PIXEL_BYTES(pf, np)  (((IGD_PF_BPP(pf)*np) +0x7)>>3)
+/*! Gets numeric pf */
+#define IGD_PF_NUM(pf) ((pf>>16) & 0xff)
+/*! Gets pf type */
+#define IGD_PF_TYPE(pf)  (pf & IGD_PF_TYPE_MASK)
+
+/*! @} */
+/*! @} */
+
+
+/*
+ * NOTE: When Adding pixel formats you must add correct definitions to
+ * any pixel format lookup tables. See igd_2d.c
+ */
+
+
+/*!
+ * @addtogroup display_group
+ *
+ * <B>Relavent Dispatch Functions</B>
+ * - _igd_dispatch::query_dc()
+ * - _igd_dispatch::query_mode_list()
+ * - _igd_dispatch::free_mode_list()
+ * - _igd_dispatch::alter_displays()
+ *
+ * @{
+ */
+
+/*!
+ * The opaque display handle, returned from igd_alloc_display().
+ * A display handle is needed for each physical display device.
+ */
+typedef void* igd_display_h;
+
+/*!
+ * The opaque timing_info handle.
+ */
+typedef void* igd_timing_info_h;
+
+/*!
+ * The maximum number of displays available in the display configurations
+ * below.
+ */
+#define IGD_MAX_DISPLAYS 2
+
+/*!
+ * @brief An opaque driver handle.
+ *
+ * This is an opaque driver handle. It is returned during driver init
+ * and remains usable for the life of the driver.
+ */
+typedef void* igd_driver_h;
+
+/*!
+ * @name Display Configuration Definition
+ * @anchor dc_defines
+ *
+ * The display configuration (dc) is a unique 32bit identifier that fully
+ * describes all displays in use and how they are attached to planes and
+ * pipes to form Single, Clone, Twin and Extended display setups.
+ *
+ * The DC is treated as 8 nibbles of information (nibble = 4 bits). Each
+ * nibble position in the 32bit DC corresponds to a specific role as
+ * follows:
+ *
+<PRE>
+    0x12345678
+      ||||||||-- Legacy Display Configuration (Single, Twin, Clone, Ext)
+      |||||||--- Port Number for Primary Pipe Master
+      ||||||---- Port Number for Primary Pipe Twin 1
+      |||||----- Port Number for Primary Pipe Twin 2
+      ||||------ Port Number for Primary Pipe Twin 3
+      |||------- Port Number for Secondary Pipe Master
+      ||-------- Port Number for Secondary Pipe Twin 1
+      |--------- Port Number for Secondary Pipe Twin 2
+</PRE>
+ *
+ * The legacy Display Configuration determines if the display is in Single
+ * Twin, Clone or extended mode using the following defines. When a complex
+ * (>2 displays) setup is defined, the legacy configuration will describe
+ * only a portion of the complete system.
+ *
+ * - IGD_DISPLAY_CONFIG_SINGLE: A single primary display only.
+ * - IGD_DISPLAY_CONFIG_CLONE: Two (or more) displays making use of two
+ *    display pipes. In this configuration two sets of display timings are
+ *    used with a single source data plane.
+ * - IGD_DISPLAY_CONFIG_TWIN: Two (or more) displays using a single display
+ *    pipe. In this configuration a single set of display timings are
+ *    used for multiple displays.
+ * - IGD_DISPLAY_CONFIG_VEXT: Two (or more) displays making use of two
+ *    display pipes and two display planes, however only one contiguous
+ *    Frame buffer has been allocated that spans bothe displays vertically.
+ *    In this configuration two sets of display timings are used and two
+ *    source data planes.
+ * - IGD_DISPLAY_CONFIG_EXTENDED: Two (or more) displays making use of two
+ *    display pipes and two display planes. In this configuration two sets
+ *    of display timings are used and two source data planes.
+ *
+ * @{
+ */
+#define IGD_DISPLAY_CONFIG_SINGLE   0x1
+#define IGD_DISPLAY_CONFIG_CLONE    0x2
+#define IGD_DISPLAY_CONFIG_TWIN     0x4
+#define IGD_DISPLAY_CONFIG_VEXT	    0x5
+#define IGD_DISPLAY_CONFIG_EXTENDED 0x8
+#define IGD_DISPLAY_CONFIG_MASK     0xf
+/*! @} */
+
+/*!
+ * @name IGD_DC_PORT_NUMBER
+ *
+ * Given a display configuration value and an index, return the port
+ * number at that position.
+ */
+#define IGD_DC_PORT_NUMBER(dc, i) (unsigned short) ((dc >> (i * 4)) & 0x0f)
+#define IGD_DC_PRIMARY(dc) (IGD_DC_PORT_NUMBER(dc,1))
+#define IGD_DC_SECONDARY(dc) (IGD_DC_PORT_NUMBER(dc,5))
+
+
+/*!
+ * @name IGD_DC_SINGLE
+ * For a given dc, return true if it is in single display mode
+ */
+#define IGD_DC_SINGLE(dc)   ((dc & 0xf) == IGD_DISPLAY_CONFIG_SINGLE)
+/*!
+ * @name IGD_DC_TWIN
+ * For a given dc, return true if it is in twin display mode
+ */
+#define IGD_DC_TWIN(dc)     ((dc & 0xf) == IGD_DISPLAY_CONFIG_TWIN)
+/*!
+ * @name IGD_DC_CLONE
+ * For a given dc, return true if it is in clone display mode
+ */
+#define IGD_DC_CLONE(dc)    ((dc & 0xf) == IGD_DISPLAY_CONFIG_CLONE)
+/*!
+ * @name IGD_DC_VEXT
+ * For a given dc, return true if it is in vertically extended display mode
+ */
+#define IGD_DC_VEXT(dc) ((dc & 0xf) == IGD_DISPLAY_CONFIG_VEXT)
+/*!
+ * @name IGD_DC_EXTENDED
+ * For a given dc, return true if it is in extended display mode
+ */
+#define IGD_DC_EXTENDED(dc) ((dc & 0xf) == IGD_DISPLAY_CONFIG_EXTENDED)
+
+/*!
+ * @name Query DC Flags
+ * @anchor query_dc_flags
+ *
+ *   - IGD_QUERY_DC_ALL: Query All usable DCs.
+ *   - IGD_QUERY_DC_PREFERRED: Returns only preferred dc's from the list
+ *      of all usable dc's. Twin mode dc's are eliminated if an equivalent
+ *      Clone dc is available. Only the most useful/flexible combination
+ *      of 3 displays is returned rather than all combinations of the 3
+ *      displays.
+ *   - IGD_QUERY_DC_INIT: Returns a pointer to the initial dc to be used.
+ *   - IGD_QUERY_DC_EXISTING: Returns a pointer to the initial dc that
+ *      most closely matches the DC in use by the hardware. This can be
+ *      used by an IAL to "discover" what the vBIOS has arranged and use
+ *      that configuration. (NOT IMPLEMENTED)
+ * @{
+ */
+#define IGD_QUERY_DC_ALL        0x0
+#define IGD_QUERY_DC_PREFERRED  0x1
+#define IGD_QUERY_DC_INIT       0x2
+#define IGD_QUERY_DC_EXISTING   0x3
+/*! @} */
+
+
+/*!
+ * @name DC index Flags
+ * @anchor dc_index_flags
+ *
+ *  - IGD_DC_IDX_PRIMARY_MASTER: index to the primary display pipe master
+ *  - IGD_DC_IDX_PRIMARY_TWIN1: index to the first primary display pipe's twin
+ *  - IGD_DC_IDX_PRIMARY_TWIN2: index to the second primary display pipe's twin
+ *  - IGD_DC_IDX_PRIMARY_TWIN3: index to the third primary display pipe's twin
+ *  - IGD_DC_IDX_SECONDARY_MASTER: index to the secondary display pipe master
+ *  - IGD_DC_IDX_SECONDARY_TWIN1: index to the first secondary display pipe's
+ *     twin
+ *  - IGD_DC_IDX_SECONDARY_TWIN2: index to the second secondary display pipe's
+ *     twin
+ * @{
+ */
+#define IGD_DC_IDX_PRIMARY_MASTER        0x1
+#define IGD_DC_IDX_PRIMARY_TWIN1         0x2
+#define IGD_DC_IDX_PRIMARY_TWIN2         0x3
+#define IGD_DC_IDX_PRIMARY_TWIN3         0x4
+#define IGD_DC_IDX_SECONDARY_MASTER      0x5
+#define IGD_DC_IDX_SECONDARY_TWIN1       0x6
+#define IGD_DC_IDX_SECONDARY_TWIN2       0x7
+/*! @} */
+
+
+/*!
+ * @name Framebuffer Info Flags
+ * @anchor fb_info_flags
+ *
+ * These bits may be set in the flags member of the _igd_framebuffer_info
+ * data structure to alter the HALs behavior when calling
+ * dispatch::alter_displays() All other flag bits must be 0 when calling.
+ *
+ *  - IGD_VBIOS_FB: The framebuffer info is not populated because the mode
+ *     is being set with a VGA/VESA mode number. The HAL should instead
+ *     return the framebuffer information to the caller when the mode
+ *     has been set.
+ *  - IGD_ENABLE_DISPLAY_GAMMA: The framebuffer data will be color corrected
+ *     by using the 3*256 entry palette lookup table. Red, Green, and Blue
+ *     8 bit color values will be looked up and converted to another value
+ *     during display. The pixels are not modified. The caller must also
+ *     set the palette using _igd_dispatch::set_palette_entries()
+ *  - IGD_REUSE_FB: The HAL will use the incoming framebuffer info as the
+ *     framebuffer. The HAL will allocate a framebuffer only if
+ *     frambuffer_info.allocated == 0 and this is the first use of the
+ *     display. If the offset has been changed, the caller is now responsible
+ *     for freeing the previous framebuffer surface, and the HAL now fully
+ *     owns the incoming surface, freeing it on shutdown etc.
+ *  - IGD_MIN_PITCH: The allocation will use the input value
+ *      of pitch as the minimum acceptable pitch. This allows a caller
+ *      to allocate a surface with "extra" room between the width and
+ *      pitch so that the width may later be expanded (use with caution).
+ *
+ * Additionally all surface flags will be populated by the HAL and
+ * returned.
+ * See: @ref surface_info_flags
+ *
+ * @{
+ */
+#define IGD_VBIOS_FB                            0x80000000
+#define IGD_ENABLE_DISPLAY_GAMMA				0x40000000
+#define IGD_REUSE_FB                            0x20000000
+#define IGD_MIN_PITCH                           0x10000000
+#define IGD_FB_FLAGS_MASK                       0xf0000000
+/* @} */
+
+/*
+ * Right now, these flags are only used by igd_alter_displays when calling into
+ * the 3rd party dc code to indicate which flip-chains to
+ * disable.
+ */
+#define IGD_DISPLAY_PRIMARY                     0x1
+#define IGD_DISPLAY_SECONDARY 					0x2
+#define IGD_DISPLAY_ALL                         (IGD_DISPLAY_PRIMARY | IGD_DISPLAY_SECONDARY)
+
+/*!
+ * @brief Framebuffer related mode setting data.
+ *  See _igd_dispatch::alter_displays()
+ *
+ * Data structure that contains details relavent to the framebuffer
+ * configuration. Used with igd_alter_display() to alter the framebuffer
+ * size and format.
+ *
+ * The framebuffer may be larger or smaller than the display timings. When
+ * larger the output will be cropped and can be used in a panning
+ * configuration. When smaller the output will be centered.
+ *
+ */
+typedef struct _igd_framebuffer_info {
+	unsigned int width;           /*!< @brief width of fb */
+	unsigned int height;          /*!< @brief height of fb */
+	/*!
+	 * @brief Output pitch in bytes of the fb
+	 *
+	 * The pitch is the number of bytes from the first pixel of a line to
+	 * the first pixel of the next line. This can, and usually will, be
+	 * larger than width * depth for alignment purposes.
+	 */
+	unsigned int screen_pitch;
+	unsigned long fb_base_offset; /*!< @brief memory location of the fb */
+	/*!< @brief Memory location of the currently visible buffer
+	 *
+	 * This is what is being displayed on the port, which may be another buffer
+	 * (e.g. a PVR services swap-chain buffer) and not the frame buffer.
+	 */
+	unsigned long visible_offset;
+	
+	/* this is the offset that will be restored when swithcing back to dih mode from
+	 * dihclone mode
+	 */
+
+	unsigned long saved_offset;
+	/*!
+	 * @brief pixel format of the fb. See @ref pixel_formats
+	 *
+	 * This member is an input to the _igd_dispatch::alter_displays()
+	 * function and must therefore be set to a pixel format that the
+	 * hardware is capable of displaying as a framebuffer. The list
+	 * of framebuffer pixel formats that are available can be queried
+	 * by calling igd_dispatch::get_pixelformats()
+	 */
+	unsigned long pixel_format;
+	unsigned long flags;          /*!< @brief See: @ref fb_info_flags */
+	unsigned int allocated;       /*!< @brief 1 when allocated, 0 otherwise */
+} igd_framebuffer_info_t, *pigd_framebuffer_info_t;
+
+
+/*!
+ * @name Display Info Flags
+ *
+ * Flags used with Display Info data structure to control boolean information.
+ *
+ * @note These flags are identical to the flags used with the port driver
+ *  SDK. If any of these flags change, the corresponding flag in pd.h must
+ *  also change.
+ *
+ * @{
+ */
+
+/* Enable/Disable Display  */
+#define IGD_DISPLAY_DISABLE       0x00000000
+#define IGD_DISPLAY_ENABLE        0x00000001
+
+#define IGD_SCAN_MASK             0x80000000
+#define IGD_SCAN_PROGRESSIVE      0x00000000
+#define IGD_SCAN_INTERLACE        0x80000000
+
+#define IGD_PIX_LINE_DOUBLE_MASK  0x60000000
+#define IGD_DOUBLE_LINE_AND_PIXEL 0x60000000
+#define IGD_LINE_DOUBLE           0x40000000
+#define IGD_PIXEL_DOUBLE          0x20000000
+#define IGD_MODE_TEXT             0x10000000
+
+#define IGD_VSYNC_HIGH            0x08000000
+#define IGD_HSYNC_HIGH            0x04000000
+#define IGD_BLANK_LOW             0x02000000
+#define IGD_MODE_VESA             0x01000000 /* VGA/VESA mode number is valid*/
+
+#define IGD_MODE_STALL            0x00800000  /* Flag to enable stall signal */
+#define IGD_MODE_SCALE            0x00400000  /* Request NATIVE pipe timings */
+
+#define IGD_ASPECT_16_9           0x00200000  /* 16:9 aspect ratio */
+
+#define IGD_MODE_DTD              0x00080000   /* Read from EDID */
+#define IGD_MODE_DTD_USER         0x00040000   /* User defined timing */
+#define IGD_MODE_DTD_FP_NATIVE    0x00020000   /* Native fp timing */
+#define IGD_MODE_SUPPORTED        0x00010000
+
+#define IGD_MODE_FACTORY          0x00008000   /* Factory supported mode */
+#define IGD_MODE_RB               0x00004000   /* Reduced blanking mode */
+
+/*
+ * Port type definitions to be used in display_info flags.
+ * Also used in port_type field of internal port data structure.
+ */
+#define IGD_PORT_MASK             0x000FF000
+#define IGD_PORT_ANY              0x000FF000
+#define IGD_PORT_ANALOG           0x00001000
+#define IGD_PORT_DIGITAL          0x00002000
+#define IGD_PORT_SDVO             IGD_PORT_DIGITAL
+#define IGD_PORT_LVDS             0x00004000
+#define IGD_PORT_RGBA             0x00008000 /* Enhanced DVO (Sunizona) */
+#define IGD_PORT_TV               0x00010000 /* Integrated TV (Alviso)  */
+#define IGD_PORT_SDVO_ST          0x00020000
+#define IGD_PORT_SDVO_ST_GPIO     0x00040000
+
+
+/* Atom E6xx requires LPC device, define a port type to differentiate
+ * with other devices such as 0:2:0, 0:3:0 */
+#define IGD_PORT_LPC              0x80000000   /* LPC device 0:31:0 */
+
+/*
+ * Standard port definitions
+ */
+#define IGD_PORT_TYPE_TV          0x1
+#define IGD_PORT_TYPE_SDVOB       0x2
+#define IGD_PORT_TYPE_SDVOC       0x3
+#define IGD_PORT_TYPE_LVDS        0x4
+#define IGD_PORT_TYPE_ANALOG      0x5
+
+/*! Max ports
+ *  This is the maximum number of ports currently defined to encompass all
+ *  hardware configurations. This should always match the largest defined
+ *  port number in the port tables. Currently the largest port number is 5
+ *  for the analog port.  If a larger port number is defined, then this
+ *  number must be increased.
+ */
+#define IGD_MAX_PORTS             5
+
+
+/*! @} */
+
+/*!
+ * @brief Display timing information for use with
+ *   _igd_dispatch::alter_displays()
+ */
+typedef struct _igd_display_info {
+	unsigned short width;
+	unsigned short height;
+	unsigned short refresh;
+	unsigned long dclk;              /* in KHz */
+	unsigned short htotal;
+	unsigned short hblank_start;
+	unsigned short hblank_end;
+	unsigned short hsync_start;
+	unsigned short hsync_end;
+	unsigned short vtotal;
+	unsigned short vblank_start;
+	unsigned short vblank_end;
+	unsigned short vsync_start;
+	unsigned short vsync_end;
+	short mode_number;
+	unsigned long flags;
+	unsigned short x_offset;
+	unsigned short y_offset;
+	/* unsigned short port_number; */
+	/* void *pd_private_ptr; */  /* Pointer for use by the PD for any purpose. */
+	void *private_ptr;  /* INTERNAL pointer for use by main driver only */
+	unsigned short reserved_dd;     /* Reserved for device dependant layer */
+	unsigned short reserved_dd_ext; /* Reserved for device dependant layer */
+} igd_display_info_t, *pigd_display_info_t;
+
+
+/*!
+ * @name Query Modes Flags (deprecated)
+ * @deprecated To be removed in IEGD 5.1
+ *
+ * Flags for use in igd_query_display function call
+ *
+ * @{
+ */
+#define IGD_QUERY_ALL_MODES   0x01
+#define IGD_QUERY_PORT_MODES  0x00
+/*! @} */
+
+
+/*!
+ * @name Query Mode List Flags
+ * @anchor query_mode_list_flags
+ *
+ * Flags for use with _igd_dispatch::query_mode_list()
+ *  - IGD_QUERY_PRIMARY_MODES: Query mode list for the primary pipe.
+ *  - IGD_QUERY_SECONDARY_MODES: Query modes list for the secondary pipe.
+ *  - IGD_QUERY_LIVE_MODES: Query the live modes instead of filtering
+ *      and allocing a new list. This may be ORed with other bits.
+ * @{
+ */
+#define IGD_QUERY_PRIMARY_MODES   0x0
+#define IGD_QUERY_SECONDARY_MODES 0x1
+#define IGD_QUERY_LIVE_MODES      0x2
+/*! @} */
+
+
+/*!
+ * @name timing table end of list marker
+ *
+ * This value is used to mark the end of a timing list (igd_display_info_t).
+ * It is stored in the width field.  This must match the value for
+ * PD_TIMING_LIST_END in src/include/pd.h
+ *
+ * @{
+ */
+#define IGD_TIMING_TABLE_END     0xffff
+/*! @} */
+
+/*!
+ * @name Alter Displays Flags
+ * @anchor alter_displays_flags
+ *
+ * These flags are passed to _igd_dispatch::alter_displays() and are
+ * defined as follows:
+ * - IGD_TEST When set, this flag indicates that the mode should only be
+ *    tested. The hardware will not be modified.
+ * - IGD_FORCE_ALTER When set the hardware will be modified even if the
+ *    mode is the same as the current mode.
+ * - IGD_CLEAR_FB Clear the framebuffer to black after the mode set.
+ *
+ * These flags control which ring buffers are allocated for the display.
+ * These ring buffers are used with all rendering functions and designate
+ * the target for the rendering commands.
+ *
+ * - IGD_ALLOC_PRIORITY_NORMAL Command slot used for normal commands
+ * - IGD_ALLOC_PRIORITY_INTERRUPT Command slot with highest priority, often
+ *    used to send commands during interrupt time. This command slot is
+ *    not fully functional.
+ * - IGD_ALLOC_PRIORITY_POWER In theory this command slot could send power
+ *    events with a higher priority than a regular command slot. In
+ *    practice it is unused.
+ * - IGD_ALLOC_PRIORITY_BIN Binner ring buffer. This command slot requires
+ *    knowledge of how the hardware binner works.
+ *
+ * @bug The Priority Flags overlap with other alter_displays flags and
+ *  need to be relocated for IEGD 5.1
+ *
+ * @{
+ */
+#define IGD_TEST                     0x08000000
+#define IGD_FORCE_ALTER              0x04000000
+#define IGD_CLEAR_FB                 0x02000000
+
+#define IGD_ALLOC_PRIORITY_NORMAL    0x10000000
+#define IGD_ALLOC_PRIORITY_INTERRUPT 0x20000000
+#define IGD_ALLOC_PRIORITY_POWER     0x40000000
+#define IGD_ALLOC_PRIORITY_BIN       0x80000000
+/*! @} */
+
+/*!
+ * Display handle list and Display info list should be terminated with
+ * this tag. List may contain nulls, they will be skipped.
+ *
+ * @note This value is -1 so it will work the same on 32 and 64 bit platforms
+ */
+#define IGD_LIST_END -1
+
+
+
+
+/*!
+ * @name Cursor Info Flags
+ * @anchor cursor_info_flags
+ *
+ * Flags for use in the Cursor Info Structure
+ *
+ * @{
+ */
+#define IGD_CURSOR_ON              0x00000001
+#define IGD_CURSOR_OFF             0x00000000
+#define IGD_CURSOR_LOAD_ARGB_IMAGE 0x00000010
+#define IGD_CURSOR_LOAD_XOR_IMAGE  0x00000020
+
+#define IGD_CURSOR_GAMMA           0x00000002
+#define IGD_CURSOR_LOCAL           0x00000004  /* Local Memory */
+#define IGD_CLONE_CURSOR           0x00000001
+/*! @} */
+
+/*!
+ * @brief Cursor Plane information
+ *
+ * This data structure is used as input and output to the
+ * _igd_dispatch::alter_cursor() dispatch function.
+ */
+typedef struct _igd_cursor_info {
+	unsigned long width;
+	unsigned long height;
+	unsigned long pixel_format;
+	/*
+	 * Use the XOR offset for all 2 bit formats, and the ARGB offset for
+	 * 32bit formats. If either offset is 0 it should be assumed that the
+	 * HAL cannot support it.
+	 */
+	unsigned long xor_offset;
+	unsigned long xor_pitch;
+	unsigned long argb_offset;
+	unsigned long argb_pitch;
+	long x_offset;
+	long y_offset;
+	long hot_x;
+	long hot_y;
+	unsigned long palette[4];
+	unsigned long flags;
+	unsigned long rotation;
+	unsigned long render_scale_x;
+	unsigned long render_scale_y;
+	unsigned short off_screen;
+} igd_cursor_info_t;
+
+/*!
+ * @name Get Scanline Defines
+ * @anchor get_scanline_defs
+ *
+ * These defines are returned from the _igd_dispatch::get_scanline()
+ * dispatch function in the place of scanline number when no number exists.
+ *
+ * @{
+ */
+#define IGD_IN_VBLANK -1
+/*! @} */
+
+
+
+
+/*!
+ * @name Acess i2c Flags
+ * @anchor access_i2c_flags
+ *
+ * These flags are used to control the flow of data in the
+ * igd_dispatch::access_i2c() dispatch function.
+ *
+ * @{
+ */
+#define IGD_I2C_READ   0x00000001
+#define IGD_I2C_WRITE  0x00000002
+/*! @} */
+
+/*!
+ * @brief I2C data packet for use with _igd_dispatch::access_i2c()
+ */
+typedef struct _igd_i2c_reg {
+	/*! @brief Serial bus id [0..6] for alm core, and 0 for whitney core */
+	unsigned char bus_id;
+	/*! @brief Data address byte of the device */
+	unsigned char dab;
+	/*! @brief Device register */
+	unsigned char reg;
+	/*! @brief Buffer for register values */
+	unsigned char *buffer;
+	/*! @brief number of bytes to read or write */
+	unsigned char num_bytes;
+	/*! @brief i2c bus speed in KHz */
+	unsigned long i2c_speed;
+} igd_i2c_reg_t;
+
+
+
+typedef struct _igd_port_info {
+	char pd_name[64];
+	unsigned long driver_version;	/* Formatted as pd_version_t */
+	unsigned long port_num;			/* Port Number */
+	unsigned long display_type;		/* Type of display */
+	int connected;					/* 1 - Display connected, 0 - Otherwise */
+	char port_name[8];             /* Default port name (DVOA, sDVO-A, etc.) */
+	unsigned long flags;           /* Attribute flags, currently used for interlace */
+} igd_port_info_t;
+
+
+
+typedef struct _kdrm_driver_set_sync_refresh {
+        igd_display_h   display_handle;
+        unsigned int    start_line;
+        unsigned int    bottom_line;
+} emgd_drm_driver_set_sync_refresh_t;
+
+
+
+/*!
+ * @name Alloc Display Flags (deprecated)
+ *
+ * Flags for use with the igd_alloc_display function call.
+ * - IGD_NEW_ALL Default, request for new plane, pipe, port
+ * - IGD_NEW_PIPE Request for an additional pipe & port, using the same plane
+ *    from previous allocation
+ * - IGD_NEW_PORT Request for an addition port, using the same pipe and plane
+ *    from previous allocation
+ *
+ * @{
+ */
+#define IGD_NEW_MASK           0x00700000
+#define IGD_NEW_ALL            0x00400000
+#define IGD_NEW_PIPE           0x00200000
+#define IGD_NEW_PORT           0x00100000
+/*! @} */
+
+typedef int (*_igd_query_modes_fn_t)(igd_display_h display_handle,
+	igd_display_info_t **mode_list);
+/*! @} */
+
+#endif /* _IGD_MODE_H_ */
diff --git a/drivers/gpu/drm/emgd/include/igd_ovl.h b/drivers/gpu/drm/emgd/include/igd_ovl.h
new file mode 100644
index 0000000..06e826e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_ovl.h
@@ -0,0 +1,326 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_ovl.h
+ * $Revision: 1.17 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the external header file for overlay. It should be included
+ *  by ial and/or other HAL modules that require overlay interaction.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_OVL_H
+#define _IGD_OVL_H
+
+/*!
+ * @defgroup overlay_group Overlay
+ * @ingroup video_group
+ *
+ * The overlay module is responsible for the hardware overlay and the secondary
+ * overlay (sometimes referred to as plane C).
+ *
+ * <B>Relevent Dispatch Fucntions</B>
+ * - _igd_dispatch::alter_ovl()
+ * - _igd_dispatch::query_ovl()
+ * - _igd_dispatch::query_max_size_ovl()
+ * @{
+ */
+
+/*----------------------------------------------------------------------------
+ * Overlay Alter Flags
+ *--------------------------------------------------------------------------*/
+/*!
+ * @name Alter Overlay Flags
+ * @anchor alter_ovl_flags
+ *
+ * Flags for use with _igd_dispatch::alter_ovl()
+ * - IGD_OVL_ALTER_OFF: Turn the overlay off
+ * - IGD_OVL_ALTER_ON: Turn the overlay on
+ *
+ * - IGD_OVL_ALTER_PROGRESSIVE: The overlay is progressive.  Only valid when
+ *     the overlay is on
+ * - IGD_OVL_ALTER_INTERLEAVED: The overlay is interleaved/bobbed.  Only valid
+ *     when the overlay is on
+ *
+ * - IGD_OVL_ALTER_FLIP_EVEN: Flip the even field.  Only valid when the overlay
+ *     is on
+ * - IGD_OVL_ALTER_FLIP_ODD: Flip the odd field.  Only valid when the overlay
+ *     is on
+ * @{
+ */
+#define IGD_OVL_ALTER_OFF 0
+#define IGD_OVL_ALTER_ON  1
+
+#define IGD_OVL_ALTER_PROGRESSIVE   0
+#define IGD_OVL_ALTER_INTERLEAVED   2
+
+#define IGD_OVL_ALTER_FLIP_EVEN     0
+#define IGD_OVL_ALTER_FLIP_ODD      4
+
+#define IGD_FW_VIDEO_OFF	        8
+
+#define IGD_OVL_FORCE_USE_DISP   0x10
+#define IGD_OVL_OSD_ON_SPRITEC   0x20
+#define IGD_OVL_GET_SURFACE_DATA 0x50
+
+/* Note: VEXT: Keep this bitwise! */
+#define IGD_OVL_ALTER_VEXT_PRIMARY			0x100
+#define IGD_OVL_ALTER_VEXT_SECONDARY		0x200
+/*! @} */
+
+/* These are not actually used by any IAL
+#define IGD_OVL_ALTER_MIRROR_NONE  0
+#define IGD_OVL_ALTER_MIRROR_H     0x10
+#define IGD_OVL_ALTER_MIRROR_V     0x20
+#define IGD_OVL_ALTER_MIRROR_HV    (IGD_OVL_FLAGS_MIRROR_H|IGD_OVL_FLAGS_MIRROR_V) */
+
+/*----------------------------------------------------------------------------
+ * Overlay Query Flags
+ *--------------------------------------------------------------------------*/
+/*!
+ * @name Query Overlay Flags
+ * @anchor query_ovl_flags
+ *
+ * Flags for use with _igd_dispatch::query_ovl()
+ * These flags ARE EXCLUSIVE - ONLY ONE AT A TIME can be used during a
+ *    single call to igd_query_ovl
+ *
+ * - IGD_OVL_QUERY_IS_HW_SUPPORTED: Can the hardware support an overlay for
+ *   this display_h?  This will return the same value no matter if the overlay
+ *   is on or off, so the IAL must use some other method to determine if the
+ *   overlay is in use.
+ * - IGD_OVL_QUERY_IS_LAST_FLIP_DONE: Has the last flip completed?
+ * - IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE: Wait until the last flip is complete.
+ *   Returns TRUE if the last flip was successfully completed.  Returns FALSE
+ *   if there was a HW issue where the last flip did not occur.
+ * - IGD_OVL_QUERY_IS_ON: Is the hardware overlay currently on?  This does not
+ *   include the secondary overlay, only the hardware overlay.
+ * - IGD_OVL_QUERY_IS_GAMMA_SUPPORTED: Is the hardware supports GAMMA
+ *   correction?
+ * - IGD_OVL_QUERY_IS_VIDEO_PARAM_SUPPORTED: Is the hardware supports video
+ *   parameters (brightness, contrast, saturation) correction?
+ * @{
+ */
+#define IGD_OVL_QUERY_IS_HW_SUPPORTED                     0x00000001
+#define IGD_OVL_QUERY_IS_LAST_FLIP_DONE                   0x00000002
+#define IGD_OVL_QUERY_WAIT_LAST_FLIP_DONE                 0x00000003
+#define IGD_OVL_QUERY_IS_ON                               0x00000004
+#define IGD_OVL_QUERY_IS_GAMMA_SUPPORTED                  0x00000005
+#define IGD_OVL_QUERY_IS_VIDEO_PARAM_SUPPORTED            0x00000006
+#define IGD_OVL_QUERY_MASK                                0x0000000F
+	/* Ensure no bits conflict with IGD_OVL_FORCE_USE_DISP above */
+/*! @} */
+
+/* This is now in igd_query_max_size_ovl
+ * Get the maximum width/height for the src_surface pixel format
+ * return: max width and height parameters; should always return TRUE.
+#define IGD_OVL_QUERY_MAX_WIDTH_HEIGHT                    0x00000003
+ */
+
+/* This should not be needed.  Since the IAL is always passing in valid
+ * gamma information, the IAL should not query the HAL for the information.
+#define IGD_OVL_QUERY_COLOR_CORRECT_INFO                  0x00000005*/
+
+
+/*----------------------------------------------------------------------------
+ * Overlay Color Key
+ *--------------------------------------------------------------------------*/
+/*!
+ * @name Overlay Color Key Flags
+ * @anchor igd_ovl_color_key_info_flags
+ *
+ * Flags for use with @ref igd_ovl_color_key_info_t
+ * Enables and disables the src and dest color key for the overlay
+ *
+ * IGD_OVL_DST_BLEND_ENABLE enabled blending the contents of the overlay
+ * with the contents of the frambuffer using the alpha from the framebuffer.
+ * (As if the framebuffer was on top of the overlay) This feature requires
+ * that dest colorkey be enabled.
+ *
+ * @{
+ */
+#define IGD_OVL_SRC_COLOR_KEY_DISABLE		0x00000000
+#define IGD_OVL_SRC_COLOR_KEY_ENABLE		0x00000001
+
+#define IGD_OVL_DST_COLOR_KEY_DISABLE		0x00000000
+#define IGD_OVL_DST_COLOR_KEY_ENABLE		0x00000002
+
+#define IGD_OVL_DST_BLEND_ENABLE            0x00000004
+/*! @} */
+
+/*!
+ * @brief Overlay Color Key
+ *
+ * The src_lo, src_hi, and dest color key are in the following pixel format
+ * based on the pixel format of the src and dest surface.
+ * Note: Alpha is always ignored.
+ *
+ * - xRGB32        = x[31:24] R[23:16] G[15:8] B[7:0]
+ * - xRGB16_555    = x[31:15] R[14:10] G[9:5]  B[4:0]
+ * - ARGB8_INDEXED = x[31:8] Index Color[7:0]
+ * - YUV surf      = x[31:24] Y[23:16] U[15:8] V[7:0]
+ *
+ * If the source pixel (overlay) is within the src_lo and src_hi color key
+ * (inclusive) for all color components, then the destination pixel is
+ * displayed.  Otherwise the source pixel is displayed.
+ *
+ * If the source and dest color key are both enabled, what happens???
+ */
+typedef struct _igd_ovl_color_key_info {
+	/*! Low end src color key value */
+	unsigned long        src_lo;
+	/*! High end src color key value */
+	unsigned long        src_hi;
+	/*! Dest color key value.  If the destination pixel matches the
+	 *   dest color key then the souce pixel from the video surface is displayed.
+	 *   Otherwise, the destination pixel is displayed */
+	unsigned long        dest;
+	/*! Enable and disable the src and dest color key.
+	 *   See @ref igd_ovl_color_key_info_flags */
+	unsigned long        flags;
+} igd_ovl_color_key_info_t;
+
+/*----------------------------------------------------------------------------
+ * Overlay Video Quality
+ *--------------------------------------------------------------------------*/
+/*!
+ * @brief Overlay Video Quality
+ *
+ * All have a range between 0x0000 and 0xFFFF with default = 0x8000
+ */
+typedef struct _igd_ovl_video_quality_info{
+	unsigned short		contrast;
+	unsigned short		brightness;
+	unsigned short		saturation;
+} igd_ovl_video_quality_info_t;
+
+
+/*----------------------------------------------------------------------------
+ *  Overlay Gamma
+ *--------------------------------------------------------------------------*/
+/*!
+ * @name Overlay Gamma Flags
+ * @anchor igd_ovl_gamma_info_flags
+ *
+ * Flags for use with @ref igd_ovl_gamma_info_t.
+ * Enables and disables the gamma for the overlay
+ *
+ * @{
+ */
+#define IGD_OVL_GAMMA_DISABLE               0x00000000
+#define IGD_OVL_GAMMA_ENABLE				0x00000001
+/* UM only flag to indicate individual 
+ * plane color correction values should be applied */
+#define IGD_OVL_GAMMA_OVERRIDE               0x00000002
+/*! @} */
+
+/*!
+ * @name Overlay Gamma Min/Max
+ * @anchor igd_ovl_gamma_info_range
+ *
+ * Minimum and maximum gamma values for red, green, and blue with
+ * @ref igd_ovl_gamma_info_t.  These values are in 24i.8f format.
+ * Minimum value is 0.6
+ * Maximum value is 6.0
+ *
+ * @{
+ */
+#define IGD_OVL_GAMMA_DEFAULT   0x100  /* 1.0 */
+#define IGD_OVL_GAMMA_MIN       0x09A  /* 0.6 */
+#define IGD_OVL_GAMMA_MAX       0x600  /* 6.0 */
+/*! @} */
+
+/*!
+ * @brief Overlay Gamma
+ *
+ * Red, green, and blue values must be between min and max values.
+ * This value is in 24i.8f format.
+ * See @ref igd_ovl_gamma_info_range
+ */
+typedef struct _igd_ovl_gamma_info{
+	unsigned int		red;
+	unsigned int		green;
+	unsigned int		blue;
+	/*! Enable and disable the gamma for the overlay.
+	 *   See @ref igd_ovl_gamma_info_flags */
+	unsigned int		flags;
+} igd_ovl_gamma_info_t;
+/*! @} */
+
+/*----------------------------------------------------------------------------
+ * Overlay Info
+ *--------------------------------------------------------------------------*/
+/*!
+ * @brief Overlay Information (includes color key, video quality, and gamma)
+ */
+typedef struct _igd_ovl_info{
+	igd_ovl_color_key_info_t			color_key;
+	igd_ovl_video_quality_info_t		video_quality;
+	igd_ovl_gamma_info_t				gamma;
+} igd_ovl_info_t;
+
+typedef struct _video_surface_data {
+	int flags;
+	void *file;
+} vid_surf_data_t;
+
+#define OVL_PRIMARY   0
+#define OVL_SECONDARY 1
+#define OVL_MAX_HW    2  /* Maximum number of overlays */
+
+/* Define the maximum number of blend surfaces which can be used */
+#define MAX_BLEND_SURF 2
+
+/* User mode overlay context */
+typedef struct _ovl_um_context {
+	/* OVLADD add additional overlay initialization parameters here. */
+
+	int in_dihclone;
+	int vext;
+	int chiptype;
+	void *dispatch; /* can't make this igd_dispatch_t due to circular reference */
+	unsigned long dc;
+	unsigned int  state;
+
+	igd_display_h primary;
+	void *primary_pipe; /* intel_pipe_data_t */
+
+	igd_display_h secondary;
+	void *secondary_pipe; /*intel_pipe_data_t */
+
+	unsigned int blend_surf_num[OVL_MAX_HW];
+	igd_surface_t blend_surf[OVL_MAX_HW][MAX_BLEND_SURF];
+
+	vid_surf_data_t *vid_surf_data_ptr;
+	igd_display_h ovl_display_um[OVL_MAX_HW];
+	igd_ovl_info_t ovl_info_um[OVL_MAX_HW];
+	igd_display_h active_single_ovl;
+} ovl_um_context_t;
+
+
+
+#endif /*_IGD_OVL_H*/
+
+#define WAIT_FOR_FLIP 1
+#define FLIP_DONE 0
diff --git a/drivers/gpu/drm/emgd/include/igd_pd.h b/drivers/gpu/drm/emgd/include/igd_pd.h
new file mode 100644
index 0000000..2963620
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_pd.h
@@ -0,0 +1,544 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_pd.h
+ * $Revision: 1.13 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_PD_H_
+#define _IGD_PD_H_
+
+#define PD_ATTR_LIST_END     0xFFFFFFFF
+
+#define PD_NAME_SIZE      64
+
+#ifndef NULL
+#ifdef __cplusplus
+#define NULL    0
+#else
+#define NULL    ((void *)0)
+#endif
+#endif
+
+#ifndef TRUE
+#define TRUE   1
+#endif
+
+#ifndef FALSE
+#define FALSE  0
+#endif
+
+/*!
+ * @addtogroup display_group
+ *
+ * @{
+ */
+
+/*
+ * Display attributes (igd_attr_t):
+ *    id   - Any associated id. See below list for known attribute ids.
+ *
+ *    type - type of the attribute. (RANGE/LIST/BOOL/BUFFER)
+ *
+ *    name - Name of the attribute. For example 'brightness', 'contrast'
+ *
+ *    flags - any flags for the attribute
+ *
+ *            PD_ATTR_FLAG_SETMODE - IGD will set this flag.
+ *                                    If this flag is set, then changing
+ *                                    the attribute value required set mode
+ *                                    operation.
+ *
+ *                                    Caller (IAL) should check for this flag
+ *                                    after calling igd_set_attrs(). If this
+ *                                    flag is set, then IAL has to do a setmode
+ *                                    operation via igd_alter_display().
+ *
+ *            PD_ATTR_FLAG_VALUE_CHANGED - Caller will set this flag.
+ *                                          If this flag is set, means the
+ *                                          attribute value was changed to
+ *                                          the value mentioned.
+ *
+ *                                          Caller will specify SS that the
+ *                                          value was changed.
+ *
+ *            PD_ATTR_FLAG_DONT_APPLY -    If this flag is set, means the
+ *                                          attribute value will be applied
+ *                                          to new value at the time of next
+ *                                          setmode operation.
+ *
+ *
+ *            PD_ATTR_FLAG_USER_INVISIBLE - If this flag is set, means the
+ *                                          attribute is invisible to end-user
+ *                                          and not available for run-time
+ *                                          changes.
+ *
+ *    If the type is PD_ATTR_TYPE_RANGE, then this structure contains further
+ *    details about this attribute (igd_range_attr_t):
+ *      default_value  - Default value
+ *      current_value  - Current value
+ *      min            - Minimum value for this attribute
+ *      max            - Maximum value for this attribute
+ *      step           - Value changes in increments of 'step'
+ *
+ *    If the type is PD_ATTR_TYPE_LIST, then this structure contains further
+ *    details about this attribute (igd_list_attr_t):
+ *      default_index  - this is the index into above list to point
+ *                              default value for this attribute
+ *
+ *      current_index  - this is the index into above list to point
+ *                              current value for this attribute
+ *
+ *      num_entries    - number values avaliable for this list attribute,
+ *                       for example, for TVFormat attribute, the available
+ *                       values can be NTSC, NTSC_M, PAL, PAL_D etc etc...
+ *
+ *      _pad           - [reserved] Pad bytes to make all attributes
+ *                       structures with same size.
+ *
+ *    If the type is PD_ATTR_TYPE_LIST, then there are num_entries structures
+ *    of igd_list_entry_attr_t will be there (igd_list_entry_attr_t):
+ *      name           - name is overloaded for example for TVFormat attribute
+ *                       the names will be "NTSC", "NTSC_M" etc etc...
+ *
+ *      flags          - same as above flags.
+ *
+ *      value          - Internal value for "NTSC", i.e., the #define value.
+ *
+ *      _pad           - [reserved] Pad bytes to make all attributes
+ *                       structures with same size.
+ *
+ *    If the type is PD_ATTR_TYPE_BOOL, then this structure contains further
+ *    details about this attribute (igd_bool_attr_t):
+ *      default_value  - Default value (TRUE/FALSE)
+ *
+ *      current_value  - Current value (TRUE/FALSE)
+ *
+ *      _pad           - [reserved] Pad bytes to make all attributes
+ *                       structures with same size.
+ *
+ *    If the type is PD_ATTR_TYPE_BUFFER, then there is a buffer pointer
+ *    in the structure to pass larger chunks of data. Originator owns the
+ *    buffer.
+ *      buffer_size    - size of the buffer
+ *
+ *      buffer         - pointer to buffer
+ *
+ */
+#define PD_ATTR_TYPE_RANGE      0x1
+#define PD_ATTR_TYPE_LIST       0x2
+#define PD_ATTR_TYPE_BOOL       0x3
+#define PD_ATTR_TYPE_LIST_ENTRY 0x4
+#define PD_ATTR_TYPE_BUFFER     0x5
+
+/*!
+ * @name Attribute ID definitions
+ * @anchor attr_id_defs
+ *
+ *  These IDs specify what the attribute is and also index into the list of
+ * attributes. See get_attrs() and set_attrs() for more information.
+ *
+ * @{
+ */
+#define PD_ATTR_ID_BRIGHTNESS      0x00
+#define PD_ATTR_ID_CONTRAST        0x01
+#define PD_ATTR_ID_HUE             0x02
+#define PD_ATTR_ID_FLICKER         0x03
+#define PD_ATTR_ID_HPOSITION       0x04
+#define PD_ATTR_ID_VPOSITION       0x05
+#define PD_ATTR_ID_HSCALE          0x06
+#define PD_ATTR_ID_VSCALE          0x07
+#define PD_ATTR_ID_TVFORMAT        0x08  /* See below for list of TV formats */
+#define PD_ATTR_ID_DISPLAY         0x09  /* See below for list of displays */
+#define PD_ATTR_ID_LUMA_FILTER     0x0A
+#define PD_ATTR_ID_CHROMA_FILTER   0x0B
+#define PD_ATTR_ID_TEXT_FILTER     0x0C
+#define PD_ATTR_ID_TVOUT_TYPE      0x0E
+#define PD_ATTR_ID_SATURATION      0x0F
+#define PD_ATTR_ID_PANEL_FIT       0x12  /* Up/Down scale yes/no */
+#define PD_ATTR_ID_SCALING_RATIO   0x13
+#define PD_ATTR_ID_FP_BACKLIGHT_EN 0x14  /* Enable Flat panel backlight      */
+#define PD_ATTR_ID_FP_PWR_T1       0x15  /* VDD active - DVO CLK/DATA active */
+#define PD_ATTR_ID_FP_PWR_T2       0x16  /* DVO active - Backlight enable    */
+#define PD_ATTR_ID_FP_PWR_T3       0x17  /* Backlight disable - DVO inactive */
+#define PD_ATTR_ID_FP_PWR_T4       0x18  /* DVO inactive - VDD inactive      */
+#define PD_ATTR_ID_FP_PWR_T5       0x19  /* VDD inactive - VDD active        */
+#define PD_ATTR_ID_PANEL_DEPTH     0x1A  /* Range, 18/24 panel */
+#define PD_ATTR_ID_2_CHANNEL_PANEL 0x1B  /* Boolean, dual-channel panel ? */
+#define PD_ATTR_ID_GANG_MODE       0x1C  /* Boolean, gang display or not? */
+#define PD_ATTR_ID_GANG_MODE_EVEN_ODD 0x1D  /* Boolean, gang display even/odd */
+#define PD_ATTR_ID_REVERSE_DVO_DATA   0x1E  /* Request reverse DVO data order */
+#define PD_ATTR_ID_SHARPNESS       0x1F
+#define PD_ATTR_ID_HW_CONFIG       0x20
+#define PD_ATTR_ID_HORZ_FILTER     0x21
+#define PD_ATTR_ID_VERT_FILTER     0x22
+#define PD_ATTR_ID_FB_GAMMA        0x23
+#define PD_ATTR_ID_FB_BRIGHTNESS   0x24
+#define PD_ATTR_ID_FB_CONTRAST     0x25
+#define PD_ATTR_ID_EXTENSION       0x26
+#define PD_ATTR_ID_2D_FLICKER      0x27
+#define PD_ATTR_ID_ADAPTIVE_FLICKER     0x28
+#define PD_ATTR_ID_HORZ_OVERSCAN        0x29
+#define PD_ATTR_ID_VERT_OVERSCAN        0x2A
+#define PD_ATTR_ID_SSC                  0x2B
+#define PD_ATTR_ID_DOT_CRAWL            0x2C
+#define PD_ATTR_ID_DITHER               0x2D
+#define PD_ATTR_ID_PANEL_PROTECT_HSYNC  0x2E
+#define PD_ATTR_ID_PANEL_PROTECT_VSYNC  0x2F
+#define PD_ATTR_ID_PANEL_PROTECT_PIXCLK 0x30
+#define PD_ATTR_ID_LVDS_PANEL_TYPE      0x31
+#define PD_ATTR_ID_ANALOG_SRC           0x34
+#define PD_ATTR_ID_SCAN_INFO            0x35
+#define PD_ATTR_ID_PICTURE_ASPECT_RATIO 0x36
+#define PD_ATTR_ID_ACTIVE_FORMAT        0x37
+#define PD_ATTR_ID_GANG_MODE_DVOCLKINV  0x38
+#define PD_ATTR_ID_VGA_2X_IMAGE         0x39
+#define PD_ATTR_ID_TEXT_TUNING          0x3A
+#define PD_ATTR_ID_MAINTAIN_ASPECT_RATIO 0x3B
+#define PD_ATTR_ID_FIXED_TIMING         0x3C
+#define PD_ATTR_ID_COLORIMETRY			0x3D   /* HDMI Attributes */
+#define PD_ATTR_ID_PIXEL_REPLICATION	0x3E
+#define PD_ATTR_ID_INTERRUPT_TYPE       0x3F   /* sdvo interrupt type */
+/* Port Driver alternative name */
+#define PD_ATTR_ID_PORT_NAME	        0x44
+/* Port ddc_reg */
+#define PD_ATTR_ID_PORT_DDC_REG			0x45
+#define PD_ATTR_ID_PWM_INTENSITY		0x46
+#define PD_ATTR_ID_INVERTER_FREQ		0x47
+#define PD_ATTR_ID_BLM_LEGACY_MODE		0x48
+#define PD_ATTR_ID_RB_SWAP_MODE			0x49
+//************ port driver attributes defined for ch7036 port driver **************//
+#define PD_ATTR_ID_HFLIP			0x54
+#define PD_ATTR_ID_VFLIP			0x55
+
+//hdmi audio
+#define PD_ATTR_ID_AUDIO_I2S_FORMAT		0x56
+#define PD_ATTR_ID_AUDIO_I2S_POLARITY           0x57
+#define PD_ATTR_ID_AUDIO_I2S_LENGH		0x58
+
+#define PD_ATTR_ID_AUDIO_TYPE			0x59 //1- I2S, 0: SPDIF
+
+//display mode tables supported by ch7036
+#define PD_ATTR_ID_HDMI_OUT_MODE		0x5A
+#define PD_ATTR_ID_DVI_OUT_MODE			0x5B
+#define PD_ATTR_ID_CRT_OUT_MODE			0x5C
+
+
+#define PD_ATTR_ID_DITHER_BYPASS		0x5D //to bypass Dither block - note: PD_ATTR_ID_DITHER is for dither selection: 18-18, 18-24, etc...
+#define PD_ATTR_ID_EDID_BYPASS			0x5E //1: bypass (use fixed built-in table, 0: read panel EDID)
+
+//1:hdmi, 0: dvi- note: PD_ATTR_ID_HDMI_CHANNEL will be  ‘inactive’ after EDID reading is
+//implemented and selected, e.g. PD_ATTR_ID_EDID_BYPASS =0
+#define PD_ATTR_ID_HDMI_CHANNEL			0x5F
+#define PD_ATTR_ID_PLL_REF_DLY			0x60
+#define PD_ATTR_ID_PLL_REF_FBDLY		0x61
+#define PD_ATTR_ID_ROTATE			0x62
+#define PD_ATTR_ID_HSCALE_CRT			0x63
+#define PD_ATTR_ID_VSCALE_CRT			0x64
+
+#define PD_ATTR_ID_LOAD_FIRMWARE		0x65
+#define PD_ATTR_ID_REFRESH			0x66
+#define PD_ATTR_ID_DWNSCAL_BYPASS		0x57 //1.2.5.pd: new pd attributes to handle the downscalling quality issue
+
+
+//************ end of ch7036 defined port driver attribute ********** //
+
+#define PD_ATTR_ID_NUM_IDS			0x67  /* Always make this last */
+
+/*! @} */
+
+/* Bit fields. */
+#define PD_ATTR_FLAG_SETMODE         0x01
+#define PD_ATTR_FLAG_VALUE_CHANGED   0x02
+#define PD_ATTR_FLAG_DONT_SET_IMM    0x04
+#define PD_ATTR_FLAG_USER_INVISIBLE  0x08   /* Attr invisible to enduser */
+#define PD_ATTR_FLAG_PD_INVISIBLE    0x10   /* Attr invisible to PD */
+#define PD_ATTR_FLAG_DYNAMIC         0x20   /* Attribute queried from sDVO */
+#define PD_ATTR_FLAG_NOTUSED         0x80   /* Attribute not used, this flag
+											 * supercedes all other flags */
+#define PD_ATTR_FLAG_NEED_RESTORE	 0x100  /* Reset occured and register value is in limbo.
+												During this time we need to keep
+												the value of the attr in our table
+												unchanged. */
+#define PD_ATTR_FLAG_GENERAL		 0x1000 /* This flag is used by HAL to querry
+											 * general attributes */
+#define PD_QUERY_GENERAL_ATTR		PD_ATTR_ID_NUM_IDS + 2
+/* TVoutput types PD_ATTR_ID_TVOUT_TYPE */
+#define PD_TVOUT_COMPOSITE    0x01
+#define PD_TVOUT_SVIDEO       0x02
+#define PD_TVOUT_COMPONENT    0x03
+#define PD_TVOUT_CPSTSV       0x04          /* Composite and S-Video */
+#define	PD_TVOUT_SCARTRGB     0x05          /* SCART output */
+#define	PD_TVOUT_VGADAC       0x06          /* VGA Outout */
+
+/* TV Standard Option */
+/* Similar TV formats need to have the same last nibble.
+ * Example for PAL the last nibble is 0x2, for NTSC it is
+ * 0x1 and so forth.
+ */
+#define PD_TV_STD_NTSC_M       0x01    /* USA, 75 IRE Setup */
+#define PD_TV_STD_NTSC_M_J     0x11    /* Japan,  0 IRE Setup */
+#define PD_TV_STD_NTSC_433     0x21
+#define PD_TV_STD_NTSC_N       0x31
+
+#define PD_TV_STD_PAL_B        0x02    /* Australia, U.K., N.Z etc */
+#define PD_TV_STD_PAL_G        0x12
+#define PD_TV_STD_PAL_D        0x22
+#define PD_TV_STD_PAL_H        0x32
+#define PD_TV_STD_PAL_I        0x42
+#define PD_TV_STD_PAL_M        0x52
+#define PD_TV_STD_PAL_N        0x62
+#define PD_TV_STD_PAL_60       0x72
+#define PD_TV_STD_PAL_NC       0x82
+
+#define PD_TV_STD_SECAM_L      0x03    /* France */
+#define PD_TV_STD_SECAM_L1     0x13
+#define PD_TV_STD_SECAM_B      0x23
+#define PD_TV_STD_SECAM_D      0x33
+#define PD_TV_STD_SECAM_G      0x43
+#define PD_TV_STD_SECAM_H      0x53
+#define PD_TV_STD_SECAM_K      0x63
+#define PD_TV_STD_SECAM_K1     0x73
+#define PD_TV_STD_SECAM_60     0x83
+
+#define PD_TV_STD_HDTV_480P    0x04
+#define PD_TV_STD_HDTV_720P    0x14
+#define PD_TV_STD_HDTV_1080I   0x24
+
+/* These HDTV defines are added for Int TV in 915GM */
+#define PD_TV_STD_HDTV_480I_59			0x34  /* 59Hz */
+#define PD_TV_STD_HDTV_480P_59			0x44
+#define PD_TV_STD_HDTV_480P_60			0x54
+
+#define PD_TV_STD_HDTV_576I_50			0x64
+#define PD_TV_STD_HDTV_576P_50			0x74
+
+#define PD_TV_STD_HDTV_720P_50			0x84
+#define PD_TV_STD_HDTV_720P_59			0x94
+#define PD_TV_STD_HDTV_720P_60			0xA4
+
+#define PD_TV_STD_HDTV_1080I_50			0xB4
+#define PD_TV_STD_HDTV_1080I_59			0xC4
+#define PD_TV_STD_HDTV_1080I_60			0xD4
+
+#define PD_TV_STD_HDTV_1080P_60			0xE4
+
+/* Defines for SDVO TV */
+#define PD_HDTV_STD_SMPTE_240M_1080i59  0x101
+#define PD_HDTV_STD_SMPTE_240M_1080i60  0x102
+
+#define PD_HDTV_STD_SMPTE_260M_1080i59  0x103
+#define PD_HDTV_STD_SMPTE_260M_1080i60  0x104
+
+#define PD_HDTV_STD_SMPTE_274M_1080i50  0x105
+#define PD_HDTV_STD_SMPTE_274M_1080i59  0x106
+#define PD_HDTV_STD_SMPTE_274M_1080i60  0x107
+#define PD_HDTV_STD_SMPTE_274M_1080p23  0x108
+#define PD_HDTV_STD_SMPTE_274M_1080p24  0x109
+#define PD_HDTV_STD_SMPTE_274M_1080p25  0x10A
+#define PD_HDTV_STD_SMPTE_274M_1080p29  0x10B
+#define PD_HDTV_STD_SMPTE_274M_1080p30  0x10C
+#define PD_HDTV_STD_SMPTE_274M_1080p50  0x10D
+#define PD_HDTV_STD_SMPTE_274M_1080p59  0x10E
+#define PD_HDTV_STD_SMPTE_274M_1080p60  0x10F
+
+#define PD_HDTV_STD_SMPTE_295M_1080i50  0x110
+#define PD_HDTV_STD_SMPTE_295M_1080p50  0x111
+
+#define PD_HDTV_STD_SMPTE_296M_720p59   0x112
+#define PD_HDTV_STD_SMPTE_296M_720p60   0x113
+#define PD_HDTV_STD_SMPTE_296M_720p50   0x114
+
+#define PD_HDTV_STD_SMPTE_293M_480p59   0x115
+#define PD_HDTV_STD_SMPTE_170M_480i59   0x116
+
+#define PD_HDTV_STD_ITURBT601_576i50    0x117
+#define PD_HDTV_STD_ITURBT601_576p50    0x118
+#define PD_HDTV_STD_EIA_7702A_480i60    0x119
+#define PD_HDTV_STD_EIA_7702A_480p60    0x11A
+
+/* Supported display types */
+/* Port drivers for DVO devices shouldn't use
+ *    PD_DISPLAY_CRT,
+ *    PD_DISPLAY_TVOUT_INT,
+ *        and
+ *    PD_DISPLAY_LVDS_INT. These are for internal on board GMCH displays..
+ *
+ * Only following display types are available for port drivers
+ * for DVO devices:
+ *    PD_DISPLAY_CRT_EXT
+ *    PD_DISPLAY_TVOUT
+ *    PD_DISPLAY_TVFP (i.e., PD_DISPLAY_TVOUT|PD_DISPLAY_FP)
+ *    PD_DISPLAY_LVDS_EXT
+ *    PD_DISPLAY_FP
+ *    PD_DISPLAY_RGBA
+ *    PD_DISPLAY_DRGB
+ *
+ * Also port drivers can make new combinations, for example,
+ * if an DVO encoder supports both LVDS and TVOut displays, it can use
+ *    PD_DISPLAY_LVDS_EXT | PD_DISPLAY_TVOUT
+ */
+#define PD_DISPLAY_CRT         0x00000001    /* GMCH on board CRT */
+#define PD_DISPLAY_TVOUT       0x00000002    /* TVOUT display type */
+#define PD_DISPLAY_FP          0x00000004    /* Flat panel type */
+#define PD_DISPLAY_TVFP        0x00000006    /* Codec supports TV + FP */
+#define PD_DISPLAY_RGBA        0x00000008    /* RGBA display */
+#define PD_DISPLAY_LVDS_EXT    0x00000010    /* External local flat panel */
+#define PD_DISPLAY_LVDS_INT    0x00000020    /* GMCH on board LVDS */
+#define PD_DISPLAY_CRT_EXT     0x00000040    /* External CRT display */
+#define PD_DISPLAY_TVOUT_INT   0x00000080    /* GMCH on board TVout */
+#define PD_DISPLAY_HDMI_INT    0x00000100    /* HDMI on board */
+#define PD_DISPLAY_HDMI_EXT    0x00000200    /* External HDMI */
+#define PD_DISPLAY_DP_INT      0x00000400    /* Display Port on board */
+#define PD_DISPLAY_DRGB        0x00000800    /* SDVO DRGB display */
+/* new display types introduced by LVDS converter chip, CH7036 */
+#define PD_DISPLAY_LVDS_LHDV   0x00001000  /* lvds converter- converts lvds signals to 
+												lvds pass-thru, hdmi, dvi, or vga */
+
+
+#ifdef CONFIG_MICRO
+#define PD_MAKE_ATTR(id, type, name, flags, def, cur, a, b, c) \
+	{id, type, flags, def, cur, a, b, c}
+#else
+#define PD_MAKE_ATTR(id, type, name, flags, def, cur, a, b, c) \
+	{id, type, name, flags, def, cur, a, b, c}
+#endif
+
+typedef struct _igd_DID_rotation_info_t {
+	int rotation;
+	int flip;
+} igd_DID_rotation_info_t;
+
+/* IMP NOTE: All below structures should be with same size.
+ *         igd_attr_t            : General attribute structure
+ *         igd_range_attr_t      : Range type attribute structure
+ *         igd_list_attr_t       : List type attribute
+ *         igd_list_entry_attr_t : Entry for a list
+ *         igd_bool_attr_t       : Boolean type attribute
+ *         igd_extension_attr_t  : Extension type attribute
+ *         igd_buffer_attr_t     : Buffer type attribute
+ */
+typedef struct _igd_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char name[32];
+#endif
+	unsigned long flags;
+	unsigned long default_value;
+	unsigned long current_value;
+	unsigned long _pad0;
+	unsigned long _pad1;
+	unsigned long _pad2;
+}igd_attr_t;
+
+typedef struct _igd_range_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char          name[32];
+#endif
+	unsigned long flags;
+	unsigned long default_value;
+	unsigned long current_value;
+	unsigned long min;
+	unsigned long max;
+	unsigned long step;
+}igd_range_attr_t;
+
+typedef struct _igd_list_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char          name[32];
+#endif
+	unsigned long flags;
+	unsigned long default_index;
+	unsigned long current_index;
+	unsigned long num_entries;
+	unsigned long _pad0;
+	unsigned long _pad1;
+}igd_list_attr_t;
+
+typedef struct _igd_list_entry_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char          name[32];
+#endif
+	unsigned long flags;
+	unsigned long value;
+	unsigned long _pad0;
+	unsigned long _pad1;
+	unsigned long _pad2;
+	unsigned long _pad3;
+}igd_list_entry_attr_t;
+
+typedef struct _igd_bool_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char          name[32];
+#endif
+	unsigned long flags;
+	unsigned long default_value;
+	unsigned long current_value;
+	unsigned long _pad0;
+	unsigned long _pad1;
+	unsigned long _pad2;
+}igd_bool_attr_t;
+
+typedef struct _igd_buffer_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char          name[32];
+#endif
+	unsigned long flags;
+	unsigned long buffer_size;
+	void          *buffer;
+	unsigned long _pad0;
+	unsigned long _pad1;
+	unsigned long _pad2;
+} igd_buffer_attr_t;
+
+typedef struct _igd_extension_attr {
+	unsigned long id;
+	unsigned long type;
+#ifndef CONFIG_MICRO
+	char name[32];
+#endif
+	unsigned long flags;
+	unsigned long default_value;
+	unsigned long current_value;
+	igd_attr_t    *extension;
+} igd_extension_attr_t;
+
+
+/*! @} */
+
+#endif /* _IGD_PD_H_ */
diff --git a/drivers/gpu/drm/emgd/include/igd_pi.h b/drivers/gpu/drm/emgd/include/igd_pi.h
new file mode 100644
index 0000000..bc12a5b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_pi.h
@@ -0,0 +1,130 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_pi.h
+ * $Revision: 1.9 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_PI_H
+#define _IGD_PI_H
+
+/* Typedefinitions */
+typedef int        (*pd_register_p)  (void *handle, void *driver);
+typedef void      *(*pd_malloc_p)(unsigned long size);
+typedef void      *(*pd_memset_p)(void *address, int c, unsigned long size);
+typedef void      *(*pd_memcpy_p)(void *dst, void *src, unsigned long size);
+typedef void       (*pd_free_p)  (void *address);
+typedef void       (*pd_usleep_p)(unsigned long usec);
+typedef void       (*pd_ui_usleep_p)(unsigned long usec);
+typedef char      *(*pd_strcpy_p)(char *dest, char const *src);
+typedef int       *(*pd_printf_p)(const char *format, ...);
+typedef int        (*pd_check_attr_p)(void *curr, void *in);
+typedef void      *(*pd_get_attr_p)(void *attr_list,
+		unsigned long num_attrs, unsigned long attr_id, unsigned long flag);
+typedef void *(*pd_get_igd_debug_p)( void );
+typedef unsigned long *(*pd_get_dropped_debug_messages_p)( void );
+typedef void *(*pd_get_debug_log_mutex_p)( void );
+typedef int        (*pd_filter_timings_p)(void *context, void *inlist,
+		void **olist, void *dvo_info, void *display_info);
+
+
+
+/* Data structures to pass callback functions from child to parent */
+typedef struct _igd_main_cb {
+	unsigned long       version;
+	pd_register_p       pd_register;
+	pd_malloc_p         pd_malloc;
+	pd_memset_p         pd_memset;
+	pd_memcpy_p         pd_memcpy;
+	pd_free_p           pd_free;
+	pd_usleep_p         pd_usleep;
+	pd_ui_usleep_p      pd_ui_usleep;
+	pd_strcpy_p         pd_strcpy;
+	pd_check_attr_p     pd_check_attr;
+	pd_get_attr_p       pd_get_attr;
+	pd_filter_timings_p pd_filter_timings;
+	pd_get_igd_debug_p  pd_get_igd_debug;
+	pd_get_debug_log_mutex_p        pd_get_debug_log_mutex;
+	pd_get_dropped_debug_messages_p pd_get_dropped_debug_messages;
+} igd_main_cb_t;
+
+typedef struct _igd_dpd_cb {
+	unsigned long   version;
+} igd_dpd_cb_t;
+
+
+/* Port driver init and exit function type definitions */
+typedef int (*pd_init_p)  (void *);
+typedef int (*pd_exit_p)  (void);
+
+/* Function to register with main driver. */
+int igd_pd_register(void *handle, void *pd_driver);
+
+/* Function to allocate memory */
+void *igd_pd_malloc(unsigned long size);
+
+/* Function to set the memory */
+void *igd_pd_memset(void *address, int c, unsigned long size);
+
+/* Function to copy block of memory */
+void *igd_pd_memcpy(void *dst, void *src, unsigned long size);
+
+/* Functions to free memory */
+void igd_pd_free(void *ptr);
+
+/* Function to sleep in micro seconds. This can be called with millisecond
+ * ranges. */
+void igd_pd_usleep(unsigned long usec);
+
+/* Function to sleep in micro seconds uniterrupted. This can be called with millisecond
+ * ranges. */
+void igd_pd_ui_usleep(unsigned long usec);
+
+
+/* Function to do a string copy */
+char *igd_pd_strcpy(char *dest, char const *src);
+
+/* Function to check value of an attribute */
+int igd_pd_check_attr(void *curr, void *in);
+
+/* This function searches for the requested attr_id in the attribute list
+ * and returns the pointer.
+ *
+ * In case of LIST attribute, it will return the proper list entry. */
+void *igd_pd_get_attr(void *attr_list, unsigned long num_attrs,
+		unsigned long attr_id, unsigned long flag);
+
+int igd_pd_filter_timings(
+	void *context,
+	void *ilist,
+	void **olist,
+	void *dvo,
+	void *display);
+
+void *igd_pd_get_igd_debug( void );
+
+#endif /* _IGD_PI_H */
diff --git a/drivers/gpu/drm/emgd/include/igd_pwr.h b/drivers/gpu/drm/emgd/include/igd_pwr.h
new file mode 100644
index 0000000..3564031
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_pwr.h
@@ -0,0 +1,65 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_pwr.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+
+
+#ifndef _IGD_PWR_H
+#define _IGD_PWR_H
+
+/*
+ * Device power state defines
+ */
+
+#define IGD_POWERSTATE_D0			0x0
+#define IGD_POWERSTATE_D1			0x1
+#define IGD_POWERSTATE_D2			0x2
+#define IGD_POWERSTATE_D3			0x3
+#define IGD_POWERSTATE_UNDEFINED    0xFFFFFFFF
+
+/*
+ * Graphics Adaptor Power State Defines
+ */
+#define IGD_ADAPTERPOWERSTATE_ON        0x10
+#define IGD_ADAPTERPOWERSTATE_STANDBY   0x11
+#define IGD_ADAPTERPOWERSTATE_SUSPEND   0x12
+#define IGD_ADAPTERPOWERSTATE_OFF       0x13
+#define IGD_ADAPTERPOWERSTATE_HIBERNATE 0x14
+
+
+/*----------------------------------------------------------------------
+ * Function:
+ *  int dispatch->pwr_alter(igd_driver_h driver_handle,
+ *    unsigned int power_state);
+ *
+ * *----------------------------------------------------------------------
+ */
+
+#endif /*_IGD_PWR_H*/
diff --git a/drivers/gpu/drm/emgd/include/igd_rb.h b/drivers/gpu/drm/emgd/include/igd_rb.h
new file mode 100644
index 0000000..3008870
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_rb.h
@@ -0,0 +1,92 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_rb.h
+ * $Revision: 1.7 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Example Usage:
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_RB_H
+#define _IGD_RB_H
+
+#include <igd_mode.h>
+
+/*
+ * Flags for igd_sync()
+ */
+#define IGD_SYNC_BLOCK         0x00000000
+#define IGD_SYNC_NONBLOCK      0x00000001
+/*
+ * No Flush pipe will not issue an MI Flush before the store dword
+ * There is no guarentee that the store dword will not complete before
+ * an earlier command unless the caller is sure that a store dword alone
+ * is sufficient.
+ */
+#define IGD_SYNC_NOFLUSH_PIPE  0x00000010
+/*
+ * Do not flush the render cache as part of this flush. Map Cache will
+ * be invalidated always.
+ */
+#define IGD_SYNC_NOFLUSH_CACHE 0x00000020
+/* Generate a user Interrupt after the sync completes */
+#define IGD_SYNC_INTERRUPT   0x00000040
+/*
+ * Flush the pipe now but delay the cache flush until the
+ * sync is checked.
+ */
+#define IGD_SYNC_DELAY_FLUSH 0x00000080
+/*
+ * Synchronize queue with video decode engine.
+ */
+#define IGD_SYNC_VIDEO       0x00000100
+
+
+/*!
+ * dispatch->get_sync_slot() returns the offset into the status page where
+ * the sync values can be found for a particular display handle.
+ *
+ * This function is to be used by a priviledged IAL only and requires some
+ * knowledge of the underlying behavior of the sync. The intention of this
+ * function is to allow an IAL that already has device knowledge to
+ * check the status of an issued sync call without calling through the HAL.
+ * In some cases (such as OGL) the address space seperation would otherwise
+ * make sync checking through the HAL a cost prohibitive operation.
+ *
+ * @param display_handle pointer to an IGD_DISPLAY pointer returned
+ *    from a successful call to dispatch->allocate_display().
+ *
+ * @param priority One of the IGD_PRIORITY_* defines. This must match
+ *    the define used when issuing the sync. NORMAL priority is the
+ *    only common use case.
+ *
+ * @return 0: Success.
+ * @return -IGD_ERROR_INVAL:  Otherwise
+ */
+typedef int (*_igd_get_sync_slot_fn_t)(igd_display_h display_handle,
+	unsigned int priority,
+	unsigned int *offset);
+
+#endif /* _IGD_RB_H */
diff --git a/drivers/gpu/drm/emgd/include/igd_render.h b/drivers/gpu/drm/emgd/include/igd_render.h
new file mode 100644
index 0000000..c492918
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_render.h
@@ -0,0 +1,532 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_render.h
+ * $Revision: 1.18 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is a header file for the Intel GFX commands.
+ *  This includes commands specific to Intel hardware and structures specific
+ *  to Intel hardware.  All other commands and structures are available
+ *  through GFX.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_RENDER_H
+#define _IGD_RENDER_H
+
+#include <igd_mode.h>
+#include <igd_appcontext.h>
+
+/*
+ * Flags passed to 2d and 3d render functions.
+ * Note: These flags MUST not conflict the the blend flags in igd_blend.h
+ */
+#define IGD_RENDER_BLOCK       0x00000000
+#define IGD_RENDER_NONBLOCK    0x00000001
+/* Render to Framebuffer: Causes a flush afterward */
+#define IGD_RENDER_FRAMEBUFFER 0x00000002
+/* Should this be an immediate blit? */
+#define IGD_RENDER_IMMEDIATE   0x00000004
+/* Render should prevent on-screen tearing */
+#define IGD_RENDER_NOTEAR      0x00000008
+
+/*
+ * Command priority for use with 2d and 3d render functions.
+ */
+#define IGD_PRIORITY_NORMAL    0x0
+#define IGD_PRIORITY_INTERRUPT 0x1
+#define IGD_PRIORITY_POWER     0x2
+#define IGD_PRIORITY_BIN       0x3
+#define IGD_PRIORITY_BB        0x4
+
+
+typedef struct _igd_yuv_coeffs {
+	char ry;
+	char ru;
+	char rv;
+   	char gy;
+	char gu;
+	char gv;
+	char by;
+	char bu;
+	char bv;
+
+	short r_const;
+	short g_const;
+	short b_const;
+
+	unsigned char r_shift;
+	unsigned char g_shift;
+	unsigned char b_shift;
+} igd_yuv_coeffs, *pigd_yuv_coeffs;
+
+typedef struct _igd_palette_info {
+	unsigned long *palette;
+	int palette_id;
+	int palette_type;
+	int size;
+
+	igd_yuv_coeffs yuv_coeffs;
+} igd_palette_info_t, *pigd_palette_info_t;
+
+/*
+ *
+ * 2D blit => igd_coord + igd_surface => igd_rect + igd_surface
+ * 3D blend => (igd_rect + igd_surface + igd_rect)*n => igd_rect + igd_surface
+ *   Blend: N source surfaces with a source rect.
+ *          N destination rects.
+ *          1 Destination surface.
+ *          1 Destination clip rect.
+ */
+
+typedef struct _igd_coord {
+	unsigned int x;
+	unsigned int y;
+} igd_coord_t, *pigd_coord_t;
+
+typedef struct _igd_rect {
+	unsigned int x1;
+	unsigned int y1;
+	unsigned int x2;
+	unsigned int y2;
+} igd_rect_t, *pigd_rect_t;
+
+typedef struct _igd_surface {
+	unsigned long offset;
+	unsigned int pitch;
+	unsigned int width;
+	unsigned int height;
+	unsigned long u_offset;     /* Needed for YUV Planar modes. */
+	unsigned int u_pitch;      /* Needed for YUV Planar modes. */
+	unsigned long v_offset;     /* Needed for YUV Planar modes. */
+	unsigned int v_pitch;      /* Needed for YUV Planar modes. */
+	unsigned long pixel_format;
+	igd_palette_info_t *palette_info;
+	unsigned long flags;        /* GMM Alignment flags (igd_gmm.h) */
+	unsigned int logic_ops;
+	unsigned int render_ops;
+	unsigned int alpha;        /* Global Alpha:
+								*  Surface is modulated by alpha prior
+								*  to blend. Used with IGD_RENDER_OP_ALPHA.
+								*  Used exclusive of diffuse which contains
+								*  a full ARGB value.
+								*/
+	unsigned int diffuse;      /* Diffuse Color:
+								*   Use with IGD_RENDER_OP_DIFFUSE.
+								*  Used with alpha-only surfaces or
+								*  constants color surfaces.
+								*/
+	unsigned long chroma_high; /* Drop pixels between low and high */
+	unsigned long chroma_low;
+	unsigned char *virt_addr;   /* Virtual memory address to be used for
+								 * either Sys Mem to Vid Mem BLT operations
+								 * or for per surface virtual memory.
+								 */
+	void *pvr2d_mem_info;    /* memory information, intended to be used only
+								* by kernel, that allows access to surface memory
+								* from variety of device memory perpectives.
+								*/
+
+	void *pvr2d_context_h;   /* TODO: This needs to be moved to the appcontext and needs to be
+								accessible by blend */
+
+	unsigned long FlipChainID;
+	void * hPVR2DFlipChain;
+
+	/* TODO: remove floating point use from code used by kernel.  who needs this??? */
+	/* We do!! This is how these parameters are passed in to blend.
+	   They are not processed on kernel side. */
+	float proc_amp_const[6];     /* Video ProcAmpControl parameter */
+} igd_surface_t, *pigd_surface_t;
+
+typedef struct _igd_surface_list {
+    unsigned long offset;
+    unsigned long size;
+} igd_surface_list_t, *pigd_surface_list_t;
+
+/*!
+ * @name Surface Render Ops
+ * @anchor render_ops
+ *
+ * These flags apply to a surface and indicate features of the surface
+ * that should be considered during render operations such as
+ * _igd_dispatch::blend()
+ *
+ * - IGD_RENDER_OP_BLEND: This surface should be blended with other
+ *  surfaces during rendering. In calls to _igd_dispatch::blend() this
+ *  surface will blend with those below it in the stack or the destination
+ *  surface (when the surface is the bottom-most in the stack).
+ *   Note: xRGB surfaces used with this render op will convert to ARGB
+ *   surfaces with an alpha of 1.0. Without this render op xRGB surfaces
+ *   will preserve the x value from the source.
+ *
+ * - IGD_RENDER_OP_PREMULT: The surface contains a pre-multipled alpha
+ *  value. This indicates that the per-pixel color values have already been
+ *  multipled by the per-pixel alpha value. Without this bit set it is
+ *  necessary to perform this multiplication as part of a blend operation.
+ *
+ * - IGD_RENDER_OP_ALPHA: The surface contains a global alpha. All pixel
+ *  values are multiplied against the global alpha as part of any blend
+ *  operation. Global alpha is used exclusive of Diffuse color.
+ *
+ * - IGD_RENDER_OP_DIFFUSE: The surface contains a single diffuse color.
+ *  This flag is used with alpha-only surfaces or surfaces of constant
+ *  color. Diffuse is used exclusively of Global Alpha.
+ *
+ * - IGD_RENDER_OP_CHROMA: THe surface contains a chroma-key. Pixel
+ *  values between  chroma_high and chroma_low will be filtered out.
+ *
+ * - IGD_RENDER_OP_COLORKEY: The surface contains a colorkey. This is used
+ *  on mask surfaces to indicate the location of pixels that may be drawn
+ *  on the destination. Only pixels with values between chroma_high and
+ *  chroma_low should "pass through" the mask to the destination. Other pixels
+ *  will be filered out.
+ *
+ * @{
+ */
+
+#define IGD_RENDER_OP_BLEND      0x1
+#define IGD_RENDER_OP_PREMULT    0x2
+#define IGD_RENDER_OP_ALPHA      0x4
+#define IGD_RENDER_OP_DIFFUSE    0x8
+#define IGD_RENDER_OP_CHROMA     0x20
+#define IGD_RENDER_OP_COLORKEY   0x40
+
+/*
+ * Make sure rotate and flip flags are not moved. Blend uses them as
+ * array indexes
+ */
+#define IGD_RENDER_OP_ROT_MASK   0x000300
+#define IGD_RENDER_OP_ROT_0      0x000000
+#define IGD_RENDER_OP_ROT_90     0x000100
+#define IGD_RENDER_OP_ROT_180    0x000200
+#define IGD_RENDER_OP_ROT_270    0x000300
+#define IGD_RENDER_OP_FLIP       0x000400
+#define IGD_RENDER_OP_SKIP_ROT	 0x000800
+#define IGD_RENDER_OP_SKIP_FLIP	 0x008000
+
+/*
+ * flags for post process - Deinterlacing and ProcAmpControl.
+ */
+#define IGD_RENDER_OP_INTERLACED 0x00001000
+#define IGD_RENDER_OP_BOB_EVEN   0x00002000
+#define IGD_RENDER_OP_BOB_ODD    0x00004000
+#define IGD_RENDER_OP_PROCAMP    0x00010000
+
+/*
+ * Region structure. Holds information about non-surface memory
+ * allocations.
+ */
+
+#define IGD_REGION_UNFINISHED 0x00000000
+#define IGD_REGION_READY      0x00000001
+#define IGD_REGION_QUEUED     0x00000002
+#define IGD_REGION_ABANDONED  0x00000004
+
+typedef struct _igd_region {
+	unsigned long offset;
+	char *virt;
+	unsigned long size;
+	unsigned long type;
+	unsigned long flags;
+} igd_region_t;
+/* @} */
+
+typedef struct _igd_dma {
+	unsigned long offset;
+	char *virt;
+	unsigned long size;
+	unsigned long head;
+	unsigned long flags;
+	/* unsigned long phys;
+		- currently not used - maybe needed in future */
+} igd_dma_t;
+
+/*
+ * Functions for setting/getting the surface information for the
+ * Display, Color, Depth, and Aux buffers for a display.
+ */
+typedef enum _igd_buffertype {
+	IGD_BUFFER_DISPLAY = 0,
+	IGD_BUFFER_COLOR,
+	IGD_BUFFER_DEPTH,
+	IGD_BUFFER_SAVE
+} igd_buffertype_t;
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_get_suface_fn_t
+ *
+ * Description:
+ *  This function, available from the top-level igd_dispatch_t as
+ *  get_surface(), allows the client to query the parameters for
+ *  the display, color, depth, and aux buffers for a given display.
+ *
+ * Parameters:
+ *  display_handle - Handle for the requested display.
+ *
+ *  type - One of the igd_buffertype_t enums.
+ *
+ *  surface - An igd_surface_t to return the information in.
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+typedef int (*_igd_get_surface_fn_t)(
+	igd_display_h display_handle,
+	igd_buffertype_t type,
+	igd_surface_t *surface,
+	igd_appcontext_h appcontext);
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_set_suface_fn_t
+ *
+ * Description:
+ *  This function, available from the top-level igd_dispatch_t as
+ *  set_surface(), allows the client to set the parameters for
+ *  the display, color, and depth buffers for a given display.
+ *
+ *  This command will happen asynchronously when possible. It will
+ *  be placed in the command stream and will happen at some point
+ *  after the command is parsed (The next vblank if it is the display
+ *  buffer)
+ *
+ *  When setting the depth buffer the setting will not take place
+ *  until the next color buffer set. Therefore when changing the
+ *  color buffer the depth buffer should always be changed first.
+ *
+ *  If set_surface has been used to change the displayed surface, the
+ *  original framebuffer should be replaced before changing modes.
+ *
+ * Parameters:
+ *  display_handle - Handle for the requested display.
+ *
+ *  priority - The priority for the command.
+ *
+ *  type - One of the igd_buffertype_t enums.
+ *
+ *  flags - Additional bitfield of options for buffer.
+ *
+ *  surface - An igd_surface_t that contains the parameters to apply.
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+#define IGD_BUFFER_ZERO_BIAS 0x1
+#define IGD_BUFFER_FIXED_Z   0x2
+#define IGD_BUFFER_ASYNC     0x4
+/* Issue a Wait for last flip only, do not flip again */
+#define IGD_BUFFER_WAIT      0x8
+/* Do not pan */
+#define IGD_BUFFER_NO_PAN		 0x10
+
+/* ------ WARNING!!!! Read Before Changing ------ */
+/*    Command should always be 32 Bit on all platforms including 64 bit */
+/*    Int is assumed to be 32 bit on all the supported platforms */
+typedef unsigned int igd_command_t;
+
+typedef int (*_igd_set_surface_fn_t)(
+	igd_display_h display_handle,
+	int priority,
+	igd_buffertype_t type,
+	igd_surface_t *surface,
+	igd_appcontext_h appcontext,
+	unsigned long flags);
+
+typedef enum _igd_event {
+	IGD_EVENT_FLIP_PENDING = 1
+} igd_event_t;
+
+typedef int (*_igd_query_event_fn_t)(
+	igd_display_h display_handle,
+	igd_event_t event,
+	unsigned long *status);
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_alloc_ring_fn_t
+ *
+ * Description:
+ *
+ * Parameters:
+ *  display_handle - Handle for the requested display.
+ *
+ *  flags - IGD_ALLOC_PRIORITY_*
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+typedef int (*_igd_alloc_ring_fn_t)(
+	igd_display_h display_handle,
+	unsigned long flags);
+
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_exec_buffer_fn_t
+ *
+ * Description:
+ *  This function allows a privelidged IAL to place instructions directly
+ * into the HAL's command queue. Under normal circumstances an IAL does not
+ * have the knowledge necessary to do this; however, some IALs (D3D, OGL, MC)
+ * will have this knowledge. The contents referenced by the data parameter
+ * are device dependent and may be a data structure or other structured
+ * information.
+ *
+ * Parameters:
+ *  display_handle - Handle for the requested display.
+ *
+ *  priority - The priority for the command.
+ *
+ *  data - Pointer to the device dependent command data or data strucutre.
+ *
+ *  size - Size in Dwords (4bytes).
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+typedef int (*_igd_exec_buffer_fn_t)(
+	igd_display_h display_handle,
+	int priority,
+	igd_appcontext_h appcontext,
+	const void *data,
+	unsigned long size);
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_query_buffer_fn_t
+ *
+ * Description:
+ * This function allows a privelidged IAL to query Topaz/MSVDX.
+ * Under normal circumstances an IAL does not have the knowledge
+ * necessary to do this; however, some IALs (D3D, OGL, MC)
+ * will have this knowledge. The contents referenced by the data parameter
+ * are device dependent and may be a data structure or other structured
+ * information.
+ *
+ * Parameters:
+ *  in_size - in parameter size.
+ *
+ *  in_buffer - input buffer.
+ *
+ *  out_size - out parameter size.
+ *
+ *  out_buffer - output buffer.
+ *
+ *  command - query command.
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+typedef int (*_igd_query_buffer_fn_t) (
+	igd_display_h display_handle,
+	void *in_buffer,
+	void *out_buffer,
+	unsigned long command);
+
+/*
+ * Flags passed to (exclusive) igd_rb_reserve
+ * and igd_rb_update functions.
+ */
+#define IGD_RB_RESERVE_BLOCK     0x00000000
+#define IGD_RB_RESERVE_NONBLOCK  0x00000001
+#define IGD_RB_CACHELINE_PAD     0x00000002
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_rb_reserve_fn_t
+ *
+ * Description:
+ *  This function allows a privelidged IAL to aquire space directly on
+ * the HAL's ring buffer. Under normal circumstances an IAL does not have
+ * the knowledge necessary to do this; however, some IALs (D3D, OGL, MC)
+ * will have this knowledge.
+ *  Any IAL making use of this interface must have full knowledge of all
+ * hardware interactions, padding etc that are needed to safely access
+ * the ring directly.
+ *
+ * Parameters:
+ *  display_handle - Handle for the requested display.
+ *
+ *  priority - The priority for the command.
+ *
+ *  size - Size in Dwords (4bytes).
+ *    Setting size to 0 will re-initialize the ring.
+ *
+ *  block - Blocking or not 1 or 0
+ *
+ *  addr - Virtual address returned from the call. Caller should begin
+ *   writing commands at this address.
+ *
+ *  avail - The amount (in dwords) of space that is available in the
+ *   ring. This is just a hint in case it is helpful to the IAL.
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+typedef int (*_igd_rb_reserve_fn_t)(
+	igd_display_h display_handle,
+	int priority,
+	unsigned long size,
+	unsigned long flags,
+	igd_command_t **addr,
+	unsigned long *avail);
+
+/*----------------------------------------------------------------------
+ * Typedef: _igd_rb_update_fn_t
+ *
+ * Description:
+ *  This function updates the ring pointer after a call to rb_reserve,
+ * and after the command data has been placed in the ring.
+ *
+ * Parameters:
+ *  display_handle - Handle for the requested display. Must be the same
+ *   display used with rb_reserve.
+ *
+ *  priority - The priority for the command. Must be the same priority
+ *   used with rb_reserve.
+ *
+ *  addr - Virtual address after placing the commands into the ring. This
+ *   should be equal to the returned addr + size from rb_reserve.
+ *
+ * Returns:
+ *  0: Success
+ *  < 0: Error
+ *----------------------------------------------------------------------
+ */
+typedef int (*_igd_rb_update_fn_t)(
+	igd_display_h display_handle,
+	int priority,
+	igd_command_t *addr,
+	unsigned long flags);
+
+
+#endif /* _IGD_RENDER_H */
diff --git a/drivers/gpu/drm/emgd/include/igd_reset.h b/drivers/gpu/drm/emgd/include/igd_reset.h
new file mode 100644
index 0000000..6150b6c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_reset.h
@@ -0,0 +1,55 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_reset.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This is the external header file for the reset module. It should be included
+ *  by above IAL for reset functionality on the ssigd HAL layers.
+ *-----------------------------------------------------------------------------
+ */
+
+
+#ifndef _IGD_RESET_H
+#define _IGD_RESET_H
+
+
+/*----------------------------------------------------------------------------
+ * Function:
+ *   int dispatch->reset_alter(igd_driver_h driver_handle);
+ *
+ * Description:
+ *		.
+ * Parameters:
+ *		driver_handle - pointer to main driver context structure.
+ *
+ * Return:
+ *  !0: Pointer to a ring buffer structure upon successful allocation
+ *  0: A ring buffer of specified type can not be allocated, or an
+ *    error occurred during memory allocation for the buffer.
+ *----------------------------------------------------------------------------
+ */
+
+
+#endif /*_IGD_RESET_H*/
diff --git a/drivers/gpu/drm/emgd/include/igd_version.h b/drivers/gpu/drm/emgd/include/igd_version.h
new file mode 100644
index 0000000..a0482a5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_version.h
@@ -0,0 +1,41 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_version.h
+ * $Revision: 1.217 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains igd version number information. The build number
+ *  is updated by the automated build process, DO NOT EDIT MANUALLY.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_VERSION_H
+#define _IGD_VERSION_H
+
+#define IGD_MAJOR_NUM  1
+#define IGD_MINOR_NUM  14
+#define IGD_BUILD_NUM  2443
+
+#define IGD_PCF_VERSION   0x00000400
+#endif
diff --git a/drivers/gpu/drm/emgd/include/igd_vga.h b/drivers/gpu/drm/emgd/include/igd_vga.h
new file mode 100644
index 0000000..1f232f0
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/igd_vga.h
@@ -0,0 +1,65 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: igd_vga.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  Header file for common VGA registers and macros.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _IGD_VGA_H
+#define _IGD_VGA_H
+
+#ifdef CONFIG_MICRO
+#define VB_CODE_SEG __based( __segname("_CODE"))
+#else
+#define VB_CODE_SEG
+#endif
+
+/* The Maximum VGA Mode Number as specified in the IGD Mode Table. */
+#define VGA_MODE_NUM_MAX 0x1C
+
+typedef struct vga_mode_data {
+	unsigned char  cols;
+	unsigned char  rows;                 /* #rows-1 */
+	unsigned char  char_height;
+	unsigned short video_buff_size;
+	unsigned char  sr_regs[4];
+	unsigned char  misc_reg;
+	unsigned char  crtc_regs[25];
+	unsigned char  ar_regs[20];
+	unsigned char  gr_regs[9];
+} vga_mode_data_t;
+
+/* This contains the VGA Mode Table.  A ptr to this exists in the
+ * Environment. */
+extern vga_mode_data_t VB_CODE_SEG vga_mode_data[];
+
+/* This contains a pointer to the VGA Mode_Table.  This is necessary, so
+ * vga_mode.c can use this to program the mode, incase the user has
+ * changed the Environment Table. */
+extern vga_mode_data_t FAR (*vga_mode_data_ptr)[];
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/io.h b/drivers/gpu/drm/emgd/include/io.h
new file mode 100644
index 0000000..9b534df
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/io.h
@@ -0,0 +1,422 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: io.h
+ * $Revision: 1.8 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains Linux user-space implementations for the OAL io.h
+ *  abstractions.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <linux/kernel.h>
+#include <asm/io.h>
+#include <config.h>
+#include <igd_debug.h>
+
+
+#ifndef _OAL_LINUX_KERNEL_IO_H
+#define _OAL_LINUX_KERNEL_IO_H
+
+extern unsigned long _os_read_port8(unsigned long port);
+extern void _os_write_port8(unsigned long port, unsigned char value);
+extern unsigned long _os_read_port16(unsigned long port);
+extern void _os_write_port16(unsigned long port, unsigned short value);
+extern unsigned long _os_read_port32(unsigned long port);
+extern void _os_write_port32(unsigned long port, unsigned long value);
+
+/*
+ * Debug macros rely on the existence of emgd_debug pointer. It is defined
+ * in the HAL. If an IAL wishes to use the print routines and is not
+ * linked with the HAL it will need to provide an emgd_debug pointer.
+ */
+
+#ifndef NULL
+#define NULL ((void *)0)
+#endif
+
+#ifndef FAR
+#define FAR
+#endif
+
+
+/*
+ * All files including io.h should define MODULE_NAME to be a bit
+ * within the emgd_debug_t data structure. If MODULE_NAME is not defined
+ * debug printing will be controlled by the global "debug" bit rather
+ * than a per-module bit.
+ */
+#ifndef MODULE_NAME
+#define MODULE_NAME hal.debug
+#endif
+
+/*
+ * Debug Print Macros use os_get_funcname to get the name of the
+ * function they are in. They cannot rely on __func__ alone because
+ * the function name may be overridden by the caller.
+ *
+ * If the caller wants to override the printed function name they may
+ * call os_set_funcname prior to each call to EMGD_DEBUG/ERROR/TRACE.
+ */
+static const char *_os_override_funcname = NULL;
+
+static __inline int os_set_funcname( const char *name )
+{
+	_os_override_funcname = name;
+	return 1;
+}
+
+static __inline const char *os_get_funcname( const char *name )
+{
+	const char *ret;
+	ret = (_os_override_funcname)?_os_override_funcname:name;
+	_os_override_funcname = NULL;
+	return ret;
+}
+
+
+
+/*
+ * EMGD_ERROR()
+ * Printing with EMGD_ERROR will be compiled in to all debug drivers and
+ * removed from all production drivers. EMGD_ERROR printing is used for
+ * driver errors that should not happen under normal operation. As such,
+ * these messages cannot be disabled at runtime.
+ *
+ * All OAL implementations should result in EMGD_ERROR messsages in the
+ * following format:
+ * <OPTIONAL OS ERROR PREFIX> FUNCTION_NAME Message
+ *
+ * The FUNCTION_NAME must be obtained by calling
+ * os_get_funcname(__func__) to insure that any overridden function names
+ * are respected.
+ */
+#ifndef EMGD_ERROR
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_ERROR(...)							\
+  do {									\
+    printk(KERN_ERR "%s ERROR: ", __FUNCTION__);			\
+    printk(__VA_ARGS__);						\
+    printk("\n");							\
+    /*fflush(stderr);*/							\
+  } while(0);
+#else
+#define EMGD_ERROR(...)  do {} while(0)
+#endif
+#endif
+
+
+/* The CONFIG_USE_INFO_PRIORITY macro is #define'd in "config.h" */
+#ifdef CONFIG_USE_INFO_PRIORITY
+#define EMGD_DEBUG_MSG_PRIORITY KERN_INFO
+#else
+#define EMGD_DEBUG_MSG_PRIORITY KERN_DEBUG
+#endif
+
+/*
+ * EMGD_DEBUG()
+ * Printing with EMGD_DEBUG will be compiled in to all debug drivers and
+ * removed from all production drivers. EMGD_DEBUG printing is used for
+ * debug messages that give information useful for debugging problems.
+ * EMGD_DEBUG messages can be enabled/disabled at compile time and runtime
+ * on a per-module basis.
+ *
+ * Disabling/Enabling module bits in CONFIG_DEBUG_FLAGS will control the
+ *  default printing of debug messages.
+ * Disabling/Enabling the bit in emgd_debug->MODULE_NAME will control
+ *  the runtime printing of debug messages.
+ *
+ * All OAL implementations should result in EMGD_DEBUG messsages in the
+ * following format:
+ * <OPTIONAL OS PREFIX> function_name Message
+ *
+ * The FUNCTION_NAME must be obtained by calling
+ * os_get_funcname(__func__) to insure that any overridden function names
+ * are respected.
+ */
+#ifndef EMGD_DEBUG
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_DEBUG(...) if(emgd_debug &&emgd_debug-> MODULE_NAME)	\
+  do {													\
+    printk(EMGD_DEBUG_MSG_PRIORITY "[EMGD_DEBUG] %s ", __FUNCTION__);	\
+    printk(__VA_ARGS__);								\
+    printk("\n");										\
+    /*fflush(stdout);*/									\
+  } while(0);
+#else
+#define EMGD_DEBUG(...)  do {} while(0)
+#endif
+#endif
+
+/*
+ * EMGD_DEBUG_S()
+ * Printing with EMGD_DEBUG_S will be compiled in to all debug drivers and
+ * removed from all production drivers. EMGD_DEBUG_S printing is used for
+ * debug messages that give information useful for debugging problems in
+ * a shortened form (used by the HAL for multi-line prints).
+ * EMGD_DEBUG_S messages can be enabled/disabled at compile time and runtime
+ * on a per-module basis.
+ *
+ * Disabling/Enabling module bits in CONFIG_DEBUG_FLAGS will control the
+ *  default printing of debug messages.
+ * Disabling/Enabling the bit in emgd_debug->MODULE_NAME will control
+ *  the runtime printing of debug messages.
+ *
+ * All OAL implementations should result in EMGD_DEBUG_S messsages in the
+ * following format:
+ * <OPTIONAL OS PREFIX> Message
+ *
+ * The FUNCTION_NAME must be obtained by calling
+ * os_get_funcname(__func__) to insure that any overridden function names
+ * are respected.
+ */
+#ifndef EMGD_DEBUG_S
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_DEBUG_S(...) if(emgd_debug && emgd_debug-> MODULE_NAME) 	\
+  do {													\
+    printk(EMGD_DEBUG_MSG_PRIORITY __VA_ARGS__);			\
+    printk("\n");										\
+    /* fflush(stdout);*/								\
+  } while(0);
+#else
+#define EMGD_DEBUG_S(...)     do {} while(0)
+#endif
+#endif
+
+/*
+ * EMGD_TRACE_ENTER
+ * Tracing with EMGD_TRACE_ENTER will be compiled in to all debug drivers and
+ * removed from all production drivers. EMGD_TRACE_ENTER will print a fixed
+ * "Enter" message when entering a function.
+ * EMGD_TRACE_ENTER messages can be enabled/disabled at compile time and
+ * runtime on a per-module basis. To Enable tracing in a module, the Global
+ * emgd_debug->trace bit must be enabled as well as the per-module
+ * debug bit.
+ *
+ * Disabling/Enabling module bits in CONFIG_DEBUG_FLAGS will control the
+ *  default printing of debug messages.
+ * Disabling/Enabling the bit in emgd_debug->MODULE_NAME will control
+ *  the runtime printing of debug messages.
+ *
+ * All OAL implementations should result in EMGD_TRACE_ENTER messsages in the
+ * following format:
+ * <OPTIONAL OS PREFIX> function_name ENTER
+ */
+#ifndef EMGD_TRACE_ENTER
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_TRACE_ENTER if(emgd_debug && emgd_debug->hal.trace) EMGD_DEBUG("ENTER")
+#else
+#define EMGD_TRACE_ENTER
+#endif
+#endif
+
+/*
+ * EMGD_TRACE_EXIT
+ * Tracing with EMGD_TRACE_EXIT will be compiled in to all debug drivers and
+ * removed from all production drivers. EMGD_TRACE_EXIT will print a fixed
+ * "Exit" message when exiting a function without error.
+ * EMGD_TRACE_EXIT messages can be enabled/disabled at compile time and
+ * runtime on a per-module basis. To Enable tracing in a module, the Global
+ * emgd_debug->trace bit must be enabled as well as the per-module
+ * debug bit.
+ *
+ * Disabling/Enabling module bits in CONFIG_DEBUG_FLAGS will control the
+ *  default printing of debug messages.
+ * Disabling/Enabling the bit in emgd_debug->MODULE_NAME will control
+ *  the runtime printing of debug messages.
+ *
+ * All OAL implementations should result in EMGD_TRACE_EXIT messsages in the
+ * following format:
+ * <OPTIONAL OS PREFIX> function_name EXIT
+ */
+#ifndef EMGD_TRACE_EXIT
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_TRACE_EXIT if(emgd_debug && emgd_debug->hal.trace) EMGD_DEBUG("EXIT")
+#else
+#define EMGD_TRACE_EXIT
+#endif
+#endif
+
+/*
+ * EMGD_ERROR_EXIT()
+ * Tracing with EMGD_ERROR_EXIT will be compiled in to all debug drivers and
+ * removed from all production drivers. EMGD_ERROR_EXIT will print an error
+ * as well as a fixed "Exit" message when exiting a function without error.
+ * EMGD_ERROR_EXIT messages can be enabled/disabled at compile time and
+ * runtime on a per-module basis. To Enable tracing in a module, the Global
+ * emgd_debug->trace bit must be enabled as well as the per-module
+ * debug bit.
+ *
+ * Note: Only the Tracing message can be disabled, the error message will
+ * still print as with EMGD_ERROR().
+ *
+ * Disabling/Enabling module bits in CONFIG_DEBUG_FLAGS will control the
+ *  default printing of debug messages.
+ * Disabling/Enabling the bit in emgd_debug->MODULE_NAME will control
+ *  the runtime printing of debug messages.
+ *
+ * All OAL implementations should result in EMGD_ERROR_EXIT messsages in the
+ * following format:
+ * <OPTIONAL OS ERROR PREFIX> function_name EXIT Message
+ */
+#ifndef EMGD_ERROR_EXIT
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_ERROR_EXIT													\
+	if(emgd_debug && emgd_debug->hal.trace) EMGD_DEBUG("EXIT With Error..."); \
+	EMGD_ERROR
+#else
+#define EMGD_ERROR_EXIT(...)   do {} while(0)
+#endif
+#endif
+
+
+/*
+ * EMGD_VERBOSE()
+ * Verbose printing that is useful only when debugging specific problems
+ * or contains lots of text should be implemented in a single function and
+ * called from within the EMGD_VERBOSE macro. The default and runtime
+ * control of this output can be altered by changing the value of the
+ * global emgd_debug->verbose.
+ */
+#ifndef EMGD_VERBOSE
+#ifdef DEBUG_BUILD_TYPE
+#define EMGD_VERBOSE(opt, func) if(emgd_debug-> opt) func;
+#else
+#define EMGD_VERBOSE(opt, func)
+#endif
+#endif
+
+/*
+ * EMGD_ASSERT( Statement, Message, Error Code)
+ * EMGD_ASSERT should be used to verify the values of statements that
+ * should always be true. EMGD_ASSERT will generate code only in debug drivers
+ * and therefore should only be used for things that should never be false
+ * in production drivers. For example, testing for null parameters in
+ * an internal interface.
+ *
+ * Usage: EMGD_ASSERT(char_ptr, "Char Pointer is NULL", -IGD_ERROR_INVAL)
+ * Usage: EMGD_ASSERT(char_ptr, "Char Pointer is NULL", )
+ */
+#ifndef EMGD_ASSERT
+#ifdef EMGD_DEBUG
+#define EMGD_ASSERT(a, m, e) if(!(a)) { EMGD_ERROR_EXIT("ASSERT: " m); return e; }
+#else
+#define EMGD_ASSERT(a, m, e)
+#endif
+#endif
+
+/*
+ * This macro _may_ be defined by an OAL port to modify the mmio
+ * base address prior to use. Commonly this is used to make the base
+ * address 0 to remove code for vbios.
+ */
+#ifdef _EMGD_MMIO
+#define EMGD_MMIO(a) _EMGD_MMIO(a)
+#else
+#define EMGD_MMIO(a) a
+#endif
+
+
+
+#define EMGD_READ32(addr) *(volatile unsigned int *)(addr)
+#define EMGD_WRITE32(value, addr) \
+		(*(volatile unsigned long *)(addr) = (value))
+/*	EMGD_DEBUG ("EMGD_WRITE32: 0x%p=0x%lx\n", (addr), (value)); \*/
+
+#define EMGD_READ8(addr) *(volatile unsigned char *)(addr)
+#define EMGD_WRITE8(value, addr) \
+		(*(volatile unsigned char *)(addr) = (value))
+/*	EMGD_DEBUG ("EMGD_WRITE8: 0x%p=0x%x\n", (addr), (value));	\*/
+
+
+/* PORT IO Macros */
+#if 0
+// user mode.  To be removed.
+#define _EMGD_READ_PORT8(port)          _os_read_port8(port)
+#define _EMGD_WRITE_PORT8(port, value)  _os_write_port8(port, value)
+
+#define _EMGD_READ_PORT16(port)         _os_read_port16(port)
+#define _EMGD_WRITE_PORT16(port, value) _os_write_port16(port, value)
+
+#define _EMGD_READ_PORT32(port)         _os_read_port32(port)
+#define _EMGD_WRITE_PORT32(port, value) _os_write_port32(port, value)
+#else
+// Linux kernel mode port I/O
+#define EMGD_READ_PORT8(port)          inb(port)
+#define EMGD_WRITE_PORT8(port, value)  outb(value, port)
+
+#define EMGD_READ_PORT16(port)         inw(port)
+#define EMGD_WRITE_PORT16(port, value) outw(value, port)
+
+#define EMGD_READ_PORT32(port)         inl(port)
+#define EMGD_WRITE_PORT32(port, value) outl(value, port)
+#endif
+
+#ifdef DEBUG_MEM
+extern unsigned long global_virt_mmadr;
+extern unsigned long global_virt_mmadr_offset;
+
+static __inline *(volatile unsigned int *) _EMGD_READ32(unsigned int addr)
+{
+    if ((addr < 0x10100000) && (addr > 0x10000000)) {
+		return *(volatile unsigned int *)(addr+global_virt_mmadr-global_virt_mmadr_offset);
+	} else {
+		return *(volatile unsigned int *)(addr);
+    }
+}
+
+static __inline void _EMGD_WRITE32(unsigned int value, unsigned int addr) \
+{
+	if ((addr < 0x10100000) && (addr > 0x10000000)) {
+		*(volatile unsigned int *)(addr+global_virt_mmadr-global_virt_mmadr_offset) = value;
+	} else {
+		*(volatile unsigned int *)addr = value;
+	}
+	printf("EMGD_WRITE32: *0x%x = 0x%x\n", addr, value);
+}
+
+static __inline *(volatile unsigned char *) _EMGD_READ8(unsigned int addr)
+{
+    if ((addr < 0x10100000) && (addr > 0x10000000)) {
+		return *(volatile unsigned char *)(addr+global_virt_mmadr-global_virt_mmadr_offset);
+	} else {
+		return *(volatile unsigned char *)addr;
+	}
+}
+
+static __inline void _EMGD_WRITE8(unsigned char value, unsigned int addr) \
+{
+    if ((addr < 0x10100000) && (addr > 0x10000000)) {
+		(*(volatile unsigned char *)(addr+global_virt_mmadr-global_virt_mmadr_offset) = value);
+    } else {
+		(*(volatile unsigned char *)(addr) = value);
+    }
+    printf("EMGD_WRITE8: *0x%x = 0x%c\n", addr, value);
+}
+#endif /* DEBUG_MEM */
+
+#define EMGD_PTR_TO_ULONG(ptr) (ptr)
+#define EMGD_ULONG_TO_PTR(ulong) (ulong)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/include/memmap.h b/drivers/gpu/drm/emgd/include/memmap.h
new file mode 100644
index 0000000..a02e943
--- /dev/null
+++ b/drivers/gpu/drm/emgd/include/memmap.h
@@ -0,0 +1,104 @@
+/*
+ *-----------------------------------------------------------------------------
+ * Filename: memmap.h
+ * $Revision: 1.6 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description:
+ *  This file contains OS abstractions for memory mapping of bus addresses.
+ *-----------------------------------------------------------------------------
+ */
+
+#ifndef _OAL_IO_MEMMAP_H
+#define _OAL_IO_MEMMAP_H
+
+void * os_map_io_to_mem_cache(
+        unsigned long base_address,
+        unsigned long size);
+void * os_map_io_to_mem_nocache(
+        unsigned long base_address,
+        unsigned long size);
+void os_unmap_io_from_mem(
+        void * virt_addr,
+        unsigned long size);
+
+
+/*****************************************************************************
+ * Function: os_map_io_to_mem_cache
+ *
+ * Description:
+ *  This function will reserve a range of virtual memory space of "size" and
+ *  map the that virtual address to the hardware "base_address" provided. This
+ *  function call will enable caching of read write transactions to the region.
+ * Parameters:
+ *              IN: base_address -> the base bus/hardware address that will be
+ *                                  used as the target of the memory mapping.
+ *              IN: size-> the size of the virtual memory range requested to
+ *                         be mapped
+ * Return Value:
+ *              NULL for failure OR
+ *              valid virtual address casted as a void *
+ *
+ ****************************************************************************/
+#define OS_MAP_IO_TO_MEM_CACHE(a, b)	os_map_io_to_mem_cache(a, b)
+
+/*****************************************************************************
+ * Function: os_map_io_to_mem_cache
+ *
+ * Description:
+ *  This function will reserve a range of virtual memory space of "size" and
+ *  map the that virtual address to the hardware "base_address" provided. This
+ *  function call will NOT cache any read/write transactions to the region and
+ *  apply the access directly to the hardware bus address that is mapped.
+ * Parameters:
+ *              IN: base_address -> the base bus/hardware address that will be
+ *                                  used as the target of the memory mapping.
+ *              IN: size-> the size of the virtual memory range requested to
+ *                         be mapped
+ * Return Value:
+ *              NULL for failure OR
+ *              valid virtual address casted as a void *
+ *
+ ****************************************************************************/
+#define OS_MAP_IO_TO_MEM_NOCACHE(a, b)	os_map_io_to_mem_nocache(a, b)
+#define OS_MAP_IO_TO_LARGE_MEM_NOCACHE(a, b) os_map_io_to_mem_nocache(a, b)
+
+/*****************************************************************************
+ * Function: os_map_io_to_mem_cache
+ *
+ * Description:
+ *  This function will unmap the range of virtual memory space of "size" that
+ *  was previously mapped with any of the above functions.
+ * Parameters:
+ *              IN: virt_address -> the base bus or hardware address that will
+ *                                  be used as the target of the memory
+ *                                  mapping.
+ *              IN: size-> the size of the virtual memory range as requested in
+ *                         os_map_io*
+ * Return Value:
+ *              none (N/A)
+ *
+ ****************************************************************************/
+#define OS_UNMAP_IO_FROM_MEM(a, b)	os_unmap_io_from_mem(a, b)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/dbgdrvif.h b/drivers/gpu/drm/emgd/pvr/include4/dbgdrvif.h
new file mode 100644
index 0000000..0527e7b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/dbgdrvif.h
@@ -0,0 +1,263 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _DBGDRVIF_
+#define _DBGDRVIF_
+
+
+#include "ioctldef.h"
+
+#define DEBUG_CAPMODE_FRAMED			0x00000001UL
+#define DEBUG_CAPMODE_CONTINUOUS		0x00000002UL
+#define DEBUG_CAPMODE_HOTKEY			0x00000004UL
+
+#define DEBUG_OUTMODE_STANDARDDBG		0x00000001UL
+#define DEBUG_OUTMODE_MONO				0x00000002UL
+#define DEBUG_OUTMODE_STREAMENABLE		0x00000004UL
+#define DEBUG_OUTMODE_ASYNC				0x00000008UL
+#define DEBUG_OUTMODE_SGXVGA            0x00000010UL
+
+#define DEBUG_FLAGS_USE_NONPAGED_MEM	0x00000001UL
+#define DEBUG_FLAGS_NO_BUF_EXPANDSION	0x00000002UL
+#define DEBUG_FLAGS_ENABLESAMPLE		0x00000004UL
+
+#define DEBUG_FLAGS_TEXTSTREAM			0x80000000UL
+
+#define DEBUG_LEVEL_0					0x00000001UL
+#define DEBUG_LEVEL_1					0x00000003UL
+#define DEBUG_LEVEL_2					0x00000007UL
+#define DEBUG_LEVEL_3					0x0000000FUL
+#define DEBUG_LEVEL_4					0x0000001FUL
+#define DEBUG_LEVEL_5					0x0000003FUL
+#define DEBUG_LEVEL_6					0x0000007FUL
+#define DEBUG_LEVEL_7					0x000000FFUL
+#define DEBUG_LEVEL_8					0x000001FFUL
+#define DEBUG_LEVEL_9					0x000003FFUL
+#define DEBUG_LEVEL_10					0x000007FFUL
+#define DEBUG_LEVEL_11					0x00000FFFUL
+
+#define DEBUG_LEVEL_SEL0				0x00000001UL
+#define DEBUG_LEVEL_SEL1				0x00000002UL
+#define DEBUG_LEVEL_SEL2				0x00000004UL
+#define DEBUG_LEVEL_SEL3				0x00000008UL
+#define DEBUG_LEVEL_SEL4				0x00000010UL
+#define DEBUG_LEVEL_SEL5				0x00000020UL
+#define DEBUG_LEVEL_SEL6				0x00000040UL
+#define DEBUG_LEVEL_SEL7				0x00000080UL
+#define DEBUG_LEVEL_SEL8				0x00000100UL
+#define DEBUG_LEVEL_SEL9				0x00000200UL
+#define DEBUG_LEVEL_SEL10				0x00000400UL
+#define DEBUG_LEVEL_SEL11				0x00000800UL
+
+#define DEBUG_SERVICE_IOCTL_BASE		0x800UL
+#define DEBUG_SERVICE_CREATESTREAM		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x01, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_DESTROYSTREAM		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x02, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_GETSTREAM			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x03, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WRITESTRING		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x04, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_READSTRING		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x05, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WRITE				CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x06, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_READ				CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x07, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_SETDEBUGMODE		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x08, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_SETDEBUGOUTMODE	CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x09, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_SETDEBUGLEVEL		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x0A, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_SETFRAME			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x0B, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_GETFRAME			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x0C, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_OVERRIDEMODE		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x0D, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_DEFAULTMODE		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x0E, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_GETSERVICETABLE	CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x0F, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WRITE2			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x10, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WRITESTRINGCM		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x11, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WRITECM			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x12, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_SETMARKER			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x13, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_GETMARKER			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x14, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_ISCAPTUREFRAME	CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x15, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WRITELF			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x16, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_READLF			CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x17, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define DEBUG_SERVICE_WAITFOREVENT		CTL_CODE(FILE_DEVICE_UNKNOWN, DEBUG_SERVICE_IOCTL_BASE + 0x18, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+
+typedef enum _DBG_EVENT_
+{
+	DBG_EVENT_STREAM_DATA = 1
+} DBG_EVENT;
+
+typedef struct _DBG_IN_CREATESTREAM_
+{
+	IMG_UINT32 ui32Pages;
+	IMG_UINT32 ui32CapMode;
+	IMG_UINT32 ui32OutMode;
+	IMG_CHAR *pszName;
+}DBG_IN_CREATESTREAM, *PDBG_IN_CREATESTREAM;
+
+typedef struct _DBG_IN_FINDSTREAM_
+{
+	IMG_BOOL bResetStream;
+	IMG_CHAR *pszName;
+}DBG_IN_FINDSTREAM, *PDBG_IN_FINDSTREAM;
+
+typedef struct _DBG_IN_WRITESTRING_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Level;
+	IMG_CHAR *pszString;
+}DBG_IN_WRITESTRING, *PDBG_IN_WRITESTRING;
+
+typedef struct _DBG_IN_READSTRING_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32StringLen;
+	IMG_CHAR *pszString;
+} DBG_IN_READSTRING, *PDBG_IN_READSTRING;
+
+typedef struct _DBG_IN_SETDEBUGMODE_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Mode;
+	IMG_UINT32 ui32Start;
+	IMG_UINT32 ui32End;
+	IMG_UINT32 ui32SampleRate;
+} DBG_IN_SETDEBUGMODE, *PDBG_IN_SETDEBUGMODE;
+
+typedef struct _DBG_IN_SETDEBUGOUTMODE_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Mode;
+} DBG_IN_SETDEBUGOUTMODE, *PDBG_IN_SETDEBUGOUTMODE;
+
+typedef struct _DBG_IN_SETDEBUGLEVEL_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Level;
+} DBG_IN_SETDEBUGLEVEL, *PDBG_IN_SETDEBUGLEVEL;
+
+typedef struct _DBG_IN_SETFRAME_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Frame;
+} DBG_IN_SETFRAME, *PDBG_IN_SETFRAME;
+
+typedef struct _DBG_IN_WRITE_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Level;
+	IMG_UINT32 ui32TransferSize;
+	IMG_UINT8 *pui8InBuffer;
+} DBG_IN_WRITE, *PDBG_IN_WRITE;
+
+typedef struct _DBG_IN_READ_
+{
+	IMG_VOID *pvStream;
+	IMG_BOOL bReadInitBuffer;
+	IMG_UINT32 ui32OutBufferSize;
+	IMG_UINT8 *pui8OutBuffer;
+} DBG_IN_READ, *PDBG_IN_READ;
+
+typedef struct _DBG_IN_OVERRIDEMODE_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Mode;
+} DBG_IN_OVERRIDEMODE, *PDBG_IN_OVERRIDEMODE;
+
+typedef struct _DBG_IN_ISCAPTUREFRAME_
+{
+	IMG_VOID *pvStream;
+	IMG_BOOL bCheckPreviousFrame;
+} DBG_IN_ISCAPTUREFRAME, *PDBG_IN_ISCAPTUREFRAME;
+
+typedef struct _DBG_IN_SETMARKER_
+{
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Marker;
+} DBG_IN_SETMARKER, *PDBG_IN_SETMARKER;
+
+typedef struct _DBG_IN_WRITE_LF_
+{
+	IMG_UINT32 ui32Flags;
+	IMG_VOID *pvStream;
+	IMG_UINT32 ui32Level;
+	IMG_UINT32 ui32BufferSize;
+	IMG_UINT8 *pui8InBuffer;
+} DBG_IN_WRITE_LF, *PDBG_IN_WRITE_LF;
+
+#define WRITELF_FLAGS_RESETBUF		0x00000001UL
+
+typedef struct _DBG_STREAM_
+{
+	struct _DBG_STREAM_ *psNext;
+	struct _DBG_STREAM_ *psInitStream;
+	IMG_BOOL   bInitPhaseComplete;
+	IMG_UINT32 ui32Flags;
+	IMG_UINT32 ui32Base;
+	IMG_UINT32 ui32Size;
+	IMG_UINT32 ui32RPtr;
+	IMG_UINT32 ui32WPtr;
+	IMG_UINT32 ui32DataWritten;
+	IMG_UINT32 ui32CapMode;
+	IMG_UINT32 ui32OutMode;
+	IMG_UINT32 ui32DebugLevel;
+	IMG_UINT32 ui32DefaultMode;
+	IMG_UINT32 ui32Start;
+	IMG_UINT32 ui32End;
+	IMG_UINT32 ui32Current;
+	IMG_UINT32 ui32Access;
+	IMG_UINT32 ui32SampleRate;
+	IMG_UINT32 ui32Reserved;
+	IMG_UINT32 ui32Timeout;
+	IMG_UINT32 ui32Marker;
+	IMG_CHAR szName[30];
+} DBG_STREAM,*PDBG_STREAM;
+
+typedef struct _DBGKM_SERVICE_TABLE_
+{
+	IMG_UINT32 ui32Size;
+	IMG_VOID * 	(IMG_CALLCONV *pfnCreateStream)			(IMG_CHAR * pszName,IMG_UINT32 ui32CapMode,IMG_UINT32 ui32OutMode,IMG_UINT32 ui32Flags,IMG_UINT32 ui32Pages);
+	IMG_VOID 	(IMG_CALLCONV *pfnDestroyStream)		(PDBG_STREAM psStream);
+	IMG_VOID * 	(IMG_CALLCONV *pfnFindStream) 			(IMG_CHAR * pszName, IMG_BOOL bResetInitBuffer);
+	IMG_UINT32 	(IMG_CALLCONV *pfnWriteString) 			(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level);
+	IMG_UINT32 	(IMG_CALLCONV *pfnReadString)			(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Limit);
+	IMG_UINT32 	(IMG_CALLCONV *pfnWriteBIN)				(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+	IMG_UINT32 	(IMG_CALLCONV *pfnReadBIN)				(PDBG_STREAM psStream,IMG_BOOL bReadInitBuffer, IMG_UINT32 ui32OutBufferSize,IMG_UINT8 *pui8OutBuf);
+	IMG_VOID 	(IMG_CALLCONV *pfnSetCaptureMode)		(PDBG_STREAM psStream,IMG_UINT32 ui32CapMode,IMG_UINT32 ui32Start,IMG_UINT32 ui32Stop,IMG_UINT32 ui32SampleRate);
+	IMG_VOID 	(IMG_CALLCONV *pfnSetOutputMode)		(PDBG_STREAM psStream,IMG_UINT32 ui32OutMode);
+	IMG_VOID 	(IMG_CALLCONV *pfnSetDebugLevel)		(PDBG_STREAM psStream,IMG_UINT32 ui32DebugLevel);
+	IMG_VOID 	(IMG_CALLCONV *pfnSetFrame)				(PDBG_STREAM psStream,IMG_UINT32 ui32Frame);
+	IMG_UINT32 	(IMG_CALLCONV *pfnGetFrame)				(PDBG_STREAM psStream);
+	IMG_VOID 	(IMG_CALLCONV *pfnOverrideMode)			(PDBG_STREAM psStream,IMG_UINT32 ui32Mode);
+	IMG_VOID 	(IMG_CALLCONV *pfnDefaultMode)			(PDBG_STREAM psStream);
+	IMG_UINT32	(IMG_CALLCONV *pfnDBGDrivWrite2)		(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+	IMG_UINT32 	(IMG_CALLCONV *pfnWriteStringCM)		(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level);
+	IMG_UINT32	(IMG_CALLCONV *pfnWriteBINCM)			(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+	IMG_VOID 	(IMG_CALLCONV *pfnSetMarker)			(PDBG_STREAM psStream,IMG_UINT32 ui32Marker);
+	IMG_UINT32 	(IMG_CALLCONV *pfnGetMarker)			(PDBG_STREAM psStream);
+	IMG_VOID 	(IMG_CALLCONV *pfnStartInitPhase)		(PDBG_STREAM psStream);
+	IMG_VOID 	(IMG_CALLCONV *pfnStopInitPhase)		(PDBG_STREAM psStream);
+	IMG_BOOL 	(IMG_CALLCONV *pfnIsCaptureFrame)		(PDBG_STREAM psStream, IMG_BOOL bCheckPreviousFrame);
+	IMG_UINT32 	(IMG_CALLCONV *pfnWriteLF)				(PDBG_STREAM psStream, IMG_UINT8 *pui8InBuf, IMG_UINT32 ui32InBuffSize, IMG_UINT32 ui32Level, IMG_UINT32 ui32Flags);
+	IMG_UINT32 	(IMG_CALLCONV *pfnReadLF)				(PDBG_STREAM psStream, IMG_UINT32 ui32OutBuffSize, IMG_UINT8 *pui8OutBuf);
+	IMG_UINT32 	(IMG_CALLCONV *pfnGetStreamOffset)		(PDBG_STREAM psStream);
+	IMG_VOID	(IMG_CALLCONV *pfnSetStreamOffset)		(PDBG_STREAM psStream, IMG_UINT32 ui32StreamOffset);
+	IMG_BOOL 	(IMG_CALLCONV *pfnIsLastCaptureFrame)	(PDBG_STREAM psStream);
+	IMG_VOID 	(IMG_CALLCONV *pfnWaitForEvent)	(DBG_EVENT eEvent);
+} DBGKM_SERVICE_TABLE, *PDBGKM_SERVICE_TABLE;
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/img_defs.h b/drivers/gpu/drm/emgd/pvr/include4/img_defs.h
new file mode 100644
index 0000000..454a68f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/img_defs.h
@@ -0,0 +1,104 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__IMG_DEFS_H__)
+#define __IMG_DEFS_H__
+
+#include "img_types.h"
+
+typedef		enum	img_tag_TriStateSwitch
+{
+	IMG_ON		=	0x00,
+	IMG_OFF,
+	IMG_IGNORE
+
+} img_TriStateSwitch, * img_pTriStateSwitch;
+
+#define		IMG_SUCCESS				0
+
+#define		IMG_NO_REG				1
+
+#if defined (NO_INLINE_FUNCS)
+	#define	INLINE
+	#define	FORCE_INLINE
+#else
+#if defined (__cplusplus)
+	#define INLINE					inline
+	#define	FORCE_INLINE			inline
+#else
+#if	!defined(INLINE)
+	#define	INLINE					__inline
+#endif
+	#define	FORCE_INLINE			static __inline
+#endif
+#endif
+
+
+#ifndef PVR_UNREFERENCED_PARAMETER
+#define	PVR_UNREFERENCED_PARAMETER(param) (param) = (param)
+#endif
+
+#ifdef __GNUC__
+#define unref__ __attribute__ ((unused))
+#else
+#define unref__
+#endif
+
+#ifndef _TCHAR_DEFINED
+#if defined(UNICODE)
+typedef unsigned short		TCHAR, *PTCHAR, *PTSTR;
+#else
+typedef char				TCHAR, *PTCHAR, *PTSTR;
+#endif
+#define _TCHAR_DEFINED
+#endif
+
+
+			#if defined(__linux__) || defined(__METAG)
+
+				#define IMG_CALLCONV
+				#define IMG_INTERNAL	__attribute__ ((visibility ("hidden")))
+				#define IMG_EXPORT
+				#define IMG_IMPORT
+				#define IMG_RESTRICT	__restrict__
+
+			#else
+					#error("define an OS")
+			#endif
+
+#ifndef IMG_ABORT
+	#define IMG_ABORT()	abort()
+#endif
+
+#ifndef IMG_MALLOC
+	#define IMG_MALLOC(A)		malloc	(A)
+#endif
+
+#ifndef IMG_FREE
+	#define IMG_FREE(A)			free	(A)
+#endif
+
+#define IMG_CONST const
+
+#define IMG_FORMAT_PRINTF(x,y)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/img_types.h b/drivers/gpu/drm/emgd/pvr/include4/img_types.h
new file mode 100644
index 0000000..a7b92e1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/img_types.h
@@ -0,0 +1,124 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __IMG_TYPES_H__
+#define __IMG_TYPES_H__
+
+#if !defined(IMG_ADDRSPACE_CPUVADDR_BITS)
+#define IMG_ADDRSPACE_CPUVADDR_BITS		32
+#endif
+
+#if !defined(IMG_ADDRSPACE_PHYSADDR_BITS)
+#define IMG_ADDRSPACE_PHYSADDR_BITS		32
+#endif
+
+typedef unsigned int	IMG_UINT,	*IMG_PUINT;
+typedef signed int		IMG_INT,	*IMG_PINT;
+
+typedef unsigned char	IMG_UINT8,	*IMG_PUINT8;
+typedef unsigned char	IMG_BYTE,	*IMG_PBYTE;
+typedef signed char		IMG_INT8,	*IMG_PINT8;
+typedef char			IMG_CHAR,	*IMG_PCHAR;
+
+typedef unsigned short	IMG_UINT16,	*IMG_PUINT16;
+typedef signed short	IMG_INT16,	*IMG_PINT16;
+typedef unsigned long	IMG_UINT32,	*IMG_PUINT32;
+typedef signed long		IMG_INT32,	*IMG_PINT32;
+
+#if !defined(IMG_UINT32_MAX)
+	#define IMG_UINT32_MAX 0xFFFFFFFFUL
+#endif
+
+	#if (defined(LINUX) || defined(__METAG))
+#if !defined(USE_CODE)
+		typedef unsigned long long		IMG_UINT64,	*IMG_PUINT64;
+		typedef long long 				IMG_INT64,	*IMG_PINT64;
+#endif
+	#else
+
+		#error("define an OS")
+
+	#endif
+
+#if !(defined(LINUX) && defined (__KERNEL__))
+typedef float			IMG_FLOAT,	*IMG_PFLOAT;
+typedef double			IMG_DOUBLE, *IMG_PDOUBLE;
+#endif
+
+typedef	enum tag_img_bool
+{
+	IMG_FALSE		= 0,
+	IMG_TRUE		= 1,
+	IMG_FORCE_ALIGN = 0x7FFFFFFF
+} IMG_BOOL, *IMG_PBOOL;
+
+typedef void            IMG_VOID,	*IMG_PVOID;
+
+typedef IMG_INT32       IMG_RESULT;
+
+typedef IMG_UINT32      IMG_UINTPTR_T;
+
+typedef IMG_PVOID       IMG_HANDLE;
+
+typedef void**          IMG_HVOID,	* IMG_PHVOID;
+
+typedef IMG_UINT32		IMG_SIZE_T;
+
+#define IMG_NULL        0
+
+
+typedef IMG_PVOID IMG_CPU_VIRTADDR;
+
+typedef struct
+{
+
+	IMG_UINT32 uiAddr;
+#define IMG_CAST_TO_DEVVADDR_UINT(var)		(IMG_UINT32)(var)
+
+} IMG_DEV_VIRTADDR;
+
+typedef struct _IMG_CPU_PHYADDR
+{
+
+	IMG_UINTPTR_T uiAddr;
+} IMG_CPU_PHYADDR;
+
+typedef struct _IMG_DEV_PHYADDR
+{
+#if IMG_ADDRSPACE_PHYSADDR_BITS == 32
+
+	IMG_UINTPTR_T uiAddr;
+#else
+	IMG_UINT32 uiAddr;
+	IMG_UINT32 uiHighAddr;
+#endif
+} IMG_DEV_PHYADDR;
+
+typedef struct _IMG_SYS_PHYADDR
+{
+
+	IMG_UINTPTR_T uiAddr;
+} IMG_SYS_PHYADDR;
+
+#include "img_defs.h"
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/ioctldef.h b/drivers/gpu/drm/emgd/pvr/include4/ioctldef.h
new file mode 100644
index 0000000..d7e494a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/ioctldef.h
@@ -0,0 +1,94 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __IOCTLDEF_H__
+#define __IOCTLDEF_H__
+
+#define MAKEIOCTLINDEX(i)	(((i) >> 2) & 0xFFF)
+
+#ifndef CTL_CODE
+
+#define DEVICE_TYPE ULONG
+
+#define FILE_DEVICE_BEEP                0x00000001
+#define FILE_DEVICE_CD_ROM              0x00000002
+#define FILE_DEVICE_CD_ROM_FILE_SYSTEM  0x00000003
+#define FILE_DEVICE_CONTROLLER          0x00000004
+#define FILE_DEVICE_DATALINK            0x00000005
+#define FILE_DEVICE_DFS                 0x00000006
+#define FILE_DEVICE_DISK                0x00000007
+#define FILE_DEVICE_DISK_FILE_SYSTEM    0x00000008
+#define FILE_DEVICE_FILE_SYSTEM         0x00000009
+#define FILE_DEVICE_INPORT_PORT         0x0000000a
+#define FILE_DEVICE_KEYBOARD            0x0000000b
+#define FILE_DEVICE_MAILSLOT            0x0000000c
+#define FILE_DEVICE_MIDI_IN             0x0000000d
+#define FILE_DEVICE_MIDI_OUT            0x0000000e
+#define FILE_DEVICE_MOUSE               0x0000000f
+#define FILE_DEVICE_MULTI_UNC_PROVIDER  0x00000010
+#define FILE_DEVICE_NAMED_PIPE          0x00000011
+#define FILE_DEVICE_NETWORK             0x00000012
+#define FILE_DEVICE_NETWORK_BROWSER     0x00000013
+#define FILE_DEVICE_NETWORK_FILE_SYSTEM 0x00000014
+#define FILE_DEVICE_NULL                0x00000015
+#define FILE_DEVICE_PARALLEL_PORT       0x00000016
+#define FILE_DEVICE_PHYSICAL_NETCARD    0x00000017
+#define FILE_DEVICE_PRINTER             0x00000018
+#define FILE_DEVICE_SCANNER             0x00000019
+#define FILE_DEVICE_SERIAL_MOUSE_PORT   0x0000001a
+#define FILE_DEVICE_SERIAL_PORT         0x0000001b
+#define FILE_DEVICE_SCREEN              0x0000001c
+#define FILE_DEVICE_SOUND               0x0000001d
+#define FILE_DEVICE_STREAMS             0x0000001e
+#define FILE_DEVICE_TAPE                0x0000001f
+#define FILE_DEVICE_TAPE_FILE_SYSTEM    0x00000020
+#define FILE_DEVICE_TRANSPORT           0x00000021
+#define FILE_DEVICE_UNKNOWN             0x00000022
+#define FILE_DEVICE_VIDEO               0x00000023
+#define FILE_DEVICE_VIRTUAL_DISK        0x00000024
+#define FILE_DEVICE_WAVE_IN             0x00000025
+#define FILE_DEVICE_WAVE_OUT            0x00000026
+#define FILE_DEVICE_8042_PORT           0x00000027
+#define FILE_DEVICE_NETWORK_REDIRECTOR  0x00000028
+#define FILE_DEVICE_BATTERY             0x00000029
+#define FILE_DEVICE_BUS_EXTENDER        0x0000002a
+#define FILE_DEVICE_MODEM               0x0000002b
+#define FILE_DEVICE_VDM                 0x0000002c
+#define FILE_DEVICE_MASS_STORAGE        0x0000002d
+
+#define CTL_CODE( DeviceType, Function, Method, Access ) (                 \
+    ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
+)
+
+#define METHOD_BUFFERED                 0
+#define METHOD_IN_DIRECT                1
+#define METHOD_OUT_DIRECT               2
+#define METHOD_NEITHER                  3
+
+#define FILE_ANY_ACCESS                 0
+#define FILE_READ_ACCESS          ( 0x0001 )
+#define FILE_WRITE_ACCESS         ( 0x0002 )
+
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/pdumpdefs.h b/drivers/gpu/drm/emgd/pvr/include4/pdumpdefs.h
new file mode 100644
index 0000000..6285c6d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/pdumpdefs.h
@@ -0,0 +1,95 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__PDUMPDEFS_H__)
+#define __PDUMPDEFS_H__
+
+typedef enum _PDUMP_PIXEL_FORMAT_
+{
+	PVRSRV_PDUMP_PIXEL_FORMAT_UNSUPPORTED = 0,
+	PVRSRV_PDUMP_PIXEL_FORMAT_RGB8 = 1,
+	PVRSRV_PDUMP_PIXEL_FORMAT_RGB332 = 2,
+	PVRSRV_PDUMP_PIXEL_FORMAT_KRGB555 = 3,
+	PVRSRV_PDUMP_PIXEL_FORMAT_RGB565 = 4,
+	PVRSRV_PDUMP_PIXEL_FORMAT_ARGB4444 = 5,
+	PVRSRV_PDUMP_PIXEL_FORMAT_ARGB1555 = 6,
+	PVRSRV_PDUMP_PIXEL_FORMAT_RGB888 = 7,
+	PVRSRV_PDUMP_PIXEL_FORMAT_ARGB8888 = 8,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV8 = 9,
+	PVRSRV_PDUMP_PIXEL_FORMAT_AYUV4444 = 10,
+	PVRSRV_PDUMP_PIXEL_FORMAT_VY0UY1_8888 = 11,
+	PVRSRV_PDUMP_PIXEL_FORMAT_UY0VY1_8888 = 12,
+	PVRSRV_PDUMP_PIXEL_FORMAT_Y0UY1V_8888 = 13,
+	PVRSRV_PDUMP_PIXEL_FORMAT_Y0VY1U_8888 = 14,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV888 = 15,
+	PVRSRV_PDUMP_PIXEL_FORMAT_UYVY10101010 = 16,
+	PVRSRV_PDUMP_PIXEL_FORMAT_VYAUYA8888 = 17,
+	PVRSRV_PDUMP_PIXEL_FORMAT_AYUV8888 = 18,
+	PVRSRV_PDUMP_PIXEL_FORMAT_AYUV2101010 = 19,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV101010 = 20,
+	PVRSRV_PDUMP_PIXEL_FORMAT_PL12Y8 = 21,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV_IMC2 = 22,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV_YV12 = 23,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV_PL8 = 24,
+	PVRSRV_PDUMP_PIXEL_FORMAT_YUV_PL12 = 25,
+	PVRSRV_PDUMP_PIXEL_FORMAT_422PL12YUV8 = 26,
+	PVRSRV_PDUMP_PIXEL_FORMAT_420PL12YUV8 = 27,
+	PVRSRV_PDUMP_PIXEL_FORMAT_PL12Y10 = 28,
+	PVRSRV_PDUMP_PIXEL_FORMAT_422PL12YUV10 = 29,
+	PVRSRV_PDUMP_PIXEL_FORMAT_420PL12YUV10 = 30,
+	PVRSRV_PDUMP_PIXEL_FORMAT_ABGR8888 = 31,
+	PVRSRV_PDUMP_PIXEL_FORMAT_BGRA8888 = 32,
+	PVRSRV_PDUMP_PIXEL_FORMAT_ARGB8332 = 33,
+	PVRSRV_PDUMP_PIXEL_FORMAT_RGB555 = 34,
+	PVRSRV_PDUMP_PIXEL_FORMAT_F16 = 35,
+	PVRSRV_PDUMP_PIXEL_FORMAT_F32 = 36,
+	PVRSRV_PDUMP_PIXEL_FORMAT_L16 = 37,
+	PVRSRV_PDUMP_PIXEL_FORMAT_L32 = 38,
+
+	PVRSRV_PDUMP_PIXEL_FORMAT_FORCE_I32 = 0x7fffffff
+
+} PDUMP_PIXEL_FORMAT;
+
+typedef enum _PDUMP_MEM_FORMAT_
+{
+	PVRSRV_PDUMP_MEM_FORMAT_STRIDE = 0,
+	PVRSRV_PDUMP_MEM_FORMAT_RESERVED = 1,
+	PVRSRV_PDUMP_MEM_FORMAT_TILED = 8,
+	PVRSRV_PDUMP_MEM_FORMAT_TWIDDLED = 9,
+	PVRSRV_PDUMP_MEM_FORMAT_HYBRID = 10,
+
+	PVRSRV_PDUMP_MEM_FORMAT_FORCE_I32 = 0x7fffffff
+} PDUMP_MEM_FORMAT;
+
+typedef enum _PDUMP_POLL_OPERATOR
+{
+	PDUMP_POLL_OPERATOR_EQUAL = 0,
+	PDUMP_POLL_OPERATOR_LESS = 1,
+	PDUMP_POLL_OPERATOR_LESSEQUAL = 2,
+	PDUMP_POLL_OPERATOR_GREATER = 3,
+	PDUMP_POLL_OPERATOR_GREATEREQUAL = 4,
+	PDUMP_POLL_OPERATOR_NOTEQUAL = 5,
+} PDUMP_POLL_OPERATOR;
+
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/pvr_debug.h b/drivers/gpu/drm/emgd/pvr/include4/pvr_debug.h
new file mode 100644
index 0000000..8459841
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/pvr_debug.h
@@ -0,0 +1,123 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __PVR_DEBUG_H__
+#define __PVR_DEBUG_H__
+
+
+#include "img_types.h"
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#define PVR_MAX_DEBUG_MESSAGE_LEN	(512)
+
+#define DBGPRIV_FATAL		0x01UL
+#define DBGPRIV_ERROR		0x02UL
+#define DBGPRIV_WARNING		0x04UL
+#define DBGPRIV_MESSAGE		0x08UL
+#define DBGPRIV_VERBOSE		0x10UL
+#define DBGPRIV_CALLTRACE	0x20UL
+#define DBGPRIV_ALLOC		0x40UL
+#define DBGPRIV_ALLLEVELS	(DBGPRIV_FATAL | DBGPRIV_ERROR | DBGPRIV_WARNING | DBGPRIV_MESSAGE | DBGPRIV_VERBOSE)
+
+
+
+#define PVR_DBG_FATAL		DBGPRIV_FATAL,__FILE__, __LINE__
+#define PVR_DBG_ERROR		DBGPRIV_ERROR,__FILE__, __LINE__
+#define PVR_DBG_WARNING		DBGPRIV_WARNING,__FILE__, __LINE__
+#define PVR_DBG_MESSAGE		DBGPRIV_MESSAGE,__FILE__, __LINE__
+#define PVR_DBG_VERBOSE		DBGPRIV_VERBOSE,__FILE__, __LINE__
+#define PVR_DBG_CALLTRACE	DBGPRIV_CALLTRACE,__FILE__, __LINE__
+#define PVR_DBG_ALLOC		DBGPRIV_ALLOC,__FILE__, __LINE__
+
+#if !defined(PVRSRV_NEED_PVR_ASSERT) && defined(DEBUG)
+#define PVRSRV_NEED_PVR_ASSERT
+#endif
+
+#if defined(PVRSRV_NEED_PVR_ASSERT) && !defined(PVRSRV_NEED_PVR_DPF)
+#define PVRSRV_NEED_PVR_DPF
+#endif
+
+#if !defined(PVRSRV_NEED_PVR_TRACE) && (defined(DEBUG) || defined(TIMING))
+#define PVRSRV_NEED_PVR_TRACE
+#endif
+
+
+#if defined(PVRSRV_NEED_PVR_ASSERT)
+
+	#define PVR_ASSERT(EXPR) if (!(EXPR)) PVRSRVDebugAssertFail(__FILE__, __LINE__);
+
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVDebugAssertFail(const IMG_CHAR *pszFile,
+													   IMG_UINT32 ui32Line);
+
+			#if defined(PVR_DBG_BREAK_ASSERT_FAIL)
+				#define PVR_DBG_BREAK	PVRSRVDebugAssertFail("PVR_DBG_BREAK", 0)
+			#else
+				#define PVR_DBG_BREAK
+			#endif
+
+#else
+
+	#define PVR_ASSERT(EXPR)
+	#define PVR_DBG_BREAK
+
+#endif
+
+
+#if defined(PVRSRV_NEED_PVR_DPF)
+
+	#define PVR_DPF(X)		PVRSRVDebugPrintf X
+
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVDebugPrintf(IMG_UINT32 ui32DebugLevel,
+												   const IMG_CHAR *pszFileName,
+												   IMG_UINT32 ui32Line,
+												   const IMG_CHAR *pszFormat,
+												   ...);
+
+#else
+
+	#define PVR_DPF(X)
+
+#endif
+
+
+#if defined(PVRSRV_NEED_PVR_TRACE)
+
+	#define PVR_TRACE(X)	PVRSRVTrace X
+
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVTrace(const IMG_CHAR* pszFormat, ... );
+
+#else
+
+	#define PVR_TRACE(X)
+
+#endif
+
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/pvrmodule.h b/drivers/gpu/drm/emgd/pvr/include4/pvrmodule.h
new file mode 100644
index 0000000..8aa4b20
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/pvrmodule.h
@@ -0,0 +1,27 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef	_PVRMODULE_H_
+#define	_PVRMODULE_H_
+MODULE_AUTHOR("Imagination Technologies Ltd. <gpl-support@imgtec.com>");
+MODULE_LICENSE("Dual MIT/GPL");
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/pvrversion.h b/drivers/gpu/drm/emgd/pvr/include4/pvrversion.h
new file mode 100644
index 0000000..25bc19b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/pvrversion.h
@@ -0,0 +1,34 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _PVRVERSION_H_
+#define _PVRVERSION_H_
+
+#define PVRVERSION_MAJ 1
+#define PVRVERSION_MIN 5
+#define PVRVERSION_BRANCH 15
+#define PVRVERSION_BUILD 3226
+#define PVRVERSION_STRING "1.5.15.3226"
+#define PVRVERSION_FILE "eurasiacon.pj"
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/regpaths.h b/drivers/gpu/drm/emgd/pvr/include4/regpaths.h
new file mode 100644
index 0000000..29a5574
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/regpaths.h
@@ -0,0 +1,39 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __REGPATHS_H__
+#define __REGPATHS_H__
+
+#define POWERVR_REG_ROOT 	   			"Drivers\\Display\\PowerVR"
+#define POWERVR_CHIP_KEY				"\\SGX1\\"
+
+#define POWERVR_EURASIA_KEY				"PowerVREurasia\\"
+
+#define POWERVR_SERVICES_KEY			"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\PowerVR\\"
+
+#define PVRSRV_REGISTRY_ROOT			POWERVR_EURASIA_KEY "HWSettings\\PVRSRVKM"
+
+
+#define MAX_REG_STRING_SIZE 128
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/services.h b/drivers/gpu/drm/emgd/pvr/include4/services.h
new file mode 100644
index 0000000..0b8efbb
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/services.h
@@ -0,0 +1,866 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __SERVICES_H__
+#define __SERVICES_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "img_defs.h"
+#include "servicesext.h"
+#include "pdumpdefs.h"
+
+
+#define PVRSRV_4K_PAGE_SIZE		4096UL
+
+#define PVRSRV_MAX_CMD_SIZE		1024
+
+#define PVRSRV_MAX_DEVICES		16
+
+#define EVENTOBJNAME_MAXLENGTH (50)
+
+#define PVRSRV_MEM_READ						(1UL<<0)
+#define PVRSRV_MEM_WRITE					(1UL<<1)
+#define PVRSRV_MEM_CACHE_CONSISTENT			(1UL<<2)
+#define PVRSRV_MEM_NO_SYNCOBJ				(1UL<<3)
+#define PVRSRV_MEM_INTERLEAVED				(1UL<<4)
+#define PVRSRV_MEM_DUMMY					(1UL<<5)
+#define PVRSRV_MEM_EDM_PROTECT				(1UL<<6)
+#define PVRSRV_MEM_ZERO						(1UL<<7)
+#define PVRSRV_MEM_USER_SUPPLIED_DEVVADDR	(1UL<<8)
+#define PVRSRV_MEM_RAM_BACKED_ALLOCATION	(1UL<<9)
+#define PVRSRV_MEM_NO_RESMAN				(1UL<<10)
+#define PVRSRV_MEM_EXPORTED					(1UL<<11)
+
+
+#define PVRSRV_HAP_CACHED					(1UL<<12)
+#define PVRSRV_HAP_UNCACHED					(1UL<<13)
+#define PVRSRV_HAP_WRITECOMBINE				(1UL<<14)
+#define PVRSRV_HAP_CACHETYPE_MASK			(PVRSRV_HAP_CACHED|PVRSRV_HAP_UNCACHED|PVRSRV_HAP_WRITECOMBINE)
+#define PVRSRV_HAP_KERNEL_ONLY				(1UL<<15)
+#define PVRSRV_HAP_SINGLE_PROCESS			(1UL<<16)
+#define PVRSRV_HAP_MULTI_PROCESS			(1UL<<17)
+#define PVRSRV_HAP_FROM_EXISTING_PROCESS	(1UL<<18)
+#define PVRSRV_HAP_NO_CPU_VIRTUAL			(1UL<<19)
+#define PVRSRV_HAP_MAPTYPE_MASK				(PVRSRV_HAP_KERNEL_ONLY \
+                                            |PVRSRV_HAP_SINGLE_PROCESS \
+                                            |PVRSRV_HAP_MULTI_PROCESS \
+                                            |PVRSRV_HAP_FROM_EXISTING_PROCESS \
+                                            |PVRSRV_HAP_NO_CPU_VIRTUAL)
+
+#define PVRSRV_MEM_CACHED					PVRSRV_HAP_CACHED
+#define PVRSRV_MEM_UNCACHED					PVRSRV_HAP_UNCACHED
+#define PVRSRV_MEM_WRITECOMBINE				PVRSRV_HAP_WRITECOMBINE
+
+#define PVRSRV_MEM_BACKINGSTORE_FIELD_SHIFT	(24)
+
+#define PVRSRV_MAP_NOUSERVIRTUAL            (1UL<<27)
+
+#define PVRSRV_NO_CONTEXT_LOSS					0
+#define PVRSRV_SEVERE_LOSS_OF_CONTEXT			1
+#define PVRSRV_PRE_STATE_CHANGE_MASK			0x80
+
+
+#define PVRSRV_DEFAULT_DEV_COOKIE			(1)
+
+
+#define PVRSRV_MISC_INFO_TIMER_PRESENT				(1UL<<0)
+#define PVRSRV_MISC_INFO_CLOCKGATE_PRESENT			(1UL<<1)
+#define PVRSRV_MISC_INFO_MEMSTATS_PRESENT			(1UL<<2)
+#define PVRSRV_MISC_INFO_GLOBALEVENTOBJECT_PRESENT	(1UL<<3)
+#define PVRSRV_MISC_INFO_DDKVERSION_PRESENT			(1UL<<4)
+#define PVRSRV_MISC_INFO_CPUCACHEFLUSH_PRESENT		(1UL<<5)
+
+#define PVRSRV_MISC_INFO_RESET_PRESENT				(1UL<<31)
+
+#define PVRSRV_PDUMP_MAX_FILENAME_SIZE			20
+#define PVRSRV_PDUMP_MAX_COMMENT_SIZE			200
+
+
+#define PVRSRV_CHANGEDEVMEM_ATTRIBS_CACHECOHERENT		0x00000001
+
+#define PVRSRV_MAPEXTMEMORY_FLAGS_ALTERNATEVA			0x00000001
+#define PVRSRV_MAPEXTMEMORY_FLAGS_PHYSCONTIG			0x00000002
+
+#define PVRSRV_MODIFYSYNCOPS_FLAGS_WO_INC			0x00000001
+#define PVRSRV_MODIFYSYNCOPS_FLAGS_RO_INC			0x00000002
+
+typedef enum _PVRSRV_DEVICE_TYPE_
+{
+	PVRSRV_DEVICE_TYPE_UNKNOWN			= 0 ,
+	PVRSRV_DEVICE_TYPE_MBX1				= 1 ,
+	PVRSRV_DEVICE_TYPE_MBX1_LITE		= 2 ,
+
+	PVRSRV_DEVICE_TYPE_M24VA			= 3,
+	PVRSRV_DEVICE_TYPE_MVDA2			= 4,
+	PVRSRV_DEVICE_TYPE_MVED1			= 5,
+	PVRSRV_DEVICE_TYPE_MSVDX			= 6,
+
+	PVRSRV_DEVICE_TYPE_SGX				= 7,
+
+	PVRSRV_DEVICE_TYPE_VGX				= 8,
+
+
+	PVRSRV_DEVICE_TYPE_EXT				= 9,
+
+    PVRSRV_DEVICE_TYPE_LAST             = 9,
+
+	PVRSRV_DEVICE_TYPE_FORCE_I32		= 0x7fffffff
+
+} PVRSRV_DEVICE_TYPE;
+
+#define HEAP_ID( _dev_ , _dev_heap_idx_ )	(  ((_dev_)<<24) | ((_dev_heap_idx_)&((1<<24)-1))  )
+#define HEAP_IDX( _heap_id_ )				( (_heap_id_)&((1<<24) - 1 ) )
+#define HEAP_DEV( _heap_id_ )				( (_heap_id_)>>24 )
+
+#define PVRSRV_UNDEFINED_HEAP_ID			(~0LU)
+
+typedef enum
+{
+	IMG_EGL				= 0x00000001,
+	IMG_OPENGLES1		= 0x00000002,
+	IMG_OPENGLES2		= 0x00000003,
+	IMG_D3DM			= 0x00000004,
+	IMG_SRV_UM			= 0x00000005,
+	IMG_OPENVG			= 0x00000006,
+	IMG_SRVCLIENT		= 0x00000007,
+	IMG_VISTAKMD		= 0x00000008,
+	IMG_VISTA3DNODE		= 0x00000009,
+	IMG_VISTAMVIDEONODE	= 0x0000000A,
+	IMG_VISTAVPBNODE	= 0x0000000B,
+	IMG_OPENGL			= 0x0000000C,
+	IMG_D3D				= 0x0000000D,
+#if defined(SUPPORT_GRAPHICS_HAL)
+	IMG_GRAPHICS_HAL	= 0x0000000E
+#endif
+
+} IMG_MODULE_ID;
+
+
+#define APPHINT_MAX_STRING_SIZE	256
+
+typedef enum
+{
+	IMG_STRING_TYPE		= 1,
+	IMG_FLOAT_TYPE		,
+	IMG_UINT_TYPE		,
+	IMG_INT_TYPE		,
+	IMG_FLAG_TYPE
+}IMG_DATA_TYPE;
+
+
+typedef struct _PVRSRV_DEV_DATA_ *PPVRSRV_DEV_DATA;
+
+typedef struct _PVRSRV_DEVICE_IDENTIFIER_
+{
+	PVRSRV_DEVICE_TYPE		eDeviceType;
+	PVRSRV_DEVICE_CLASS		eDeviceClass;
+	IMG_UINT32				ui32DeviceIndex;
+
+} PVRSRV_DEVICE_IDENTIFIER;
+
+
+typedef struct _PVRSRV_CLIENT_DEV_DATA_
+{
+	IMG_UINT32		ui32NumDevices;
+	PVRSRV_DEVICE_IDENTIFIER asDevID[PVRSRV_MAX_DEVICES];
+	PVRSRV_ERROR	(*apfnDevConnect[PVRSRV_MAX_DEVICES])(PPVRSRV_DEV_DATA);
+	PVRSRV_ERROR	(*apfnDumpTrace[PVRSRV_MAX_DEVICES])(PPVRSRV_DEV_DATA);
+
+} PVRSRV_CLIENT_DEV_DATA;
+
+
+typedef struct _PVRSRV_CONNECTION_
+{
+	IMG_HANDLE hServices;
+	IMG_UINT32 ui32ProcessID;
+	PVRSRV_CLIENT_DEV_DATA	sClientDevData;
+}PVRSRV_CONNECTION;
+
+
+typedef struct _PVRSRV_DEV_DATA_
+{
+	PVRSRV_CONNECTION	sConnection;
+	IMG_HANDLE			hDevCookie;
+
+} PVRSRV_DEV_DATA;
+
+typedef struct _PVRSRV_MEMUPDATE_
+{
+	IMG_UINT32			ui32UpdateAddr;
+	IMG_UINT32			ui32UpdateVal;
+} PVRSRV_MEMUPDATE;
+
+typedef struct _PVRSRV_HWREG_
+{
+	IMG_UINT32			ui32RegAddr;
+	IMG_UINT32			ui32RegVal;
+} PVRSRV_HWREG;
+
+typedef struct _PVRSRV_MEMBLK_
+{
+	IMG_DEV_VIRTADDR	sDevVirtAddr;
+	IMG_HANDLE			hOSMemHandle;
+	IMG_HANDLE			hOSWrapMem;
+	IMG_HANDLE			hBuffer;
+	IMG_HANDLE			hResItem;
+	IMG_SYS_PHYADDR	 	*psIntSysPAddr;
+
+} PVRSRV_MEMBLK;
+
+typedef struct _PVRSRV_KERNEL_MEM_INFO_ *PPVRSRV_KERNEL_MEM_INFO;
+
+typedef struct _PVRSRV_CLIENT_MEM_INFO_
+{
+
+	IMG_PVOID				pvLinAddr;
+
+
+	IMG_PVOID				pvLinAddrKM;
+
+
+	IMG_DEV_VIRTADDR		sDevVAddr;
+
+
+
+
+
+
+	IMG_CPU_PHYADDR			sCpuPAddr;
+
+
+	IMG_UINT32				ui32Flags;
+
+
+
+
+	IMG_UINT32				ui32ClientFlags;
+
+
+	IMG_SIZE_T				ui32AllocSize;
+
+
+
+	struct _PVRSRV_CLIENT_SYNC_INFO_	*psClientSyncInfo;
+
+
+	IMG_HANDLE							hMappingInfo;
+
+
+	IMG_HANDLE							hKernelMemInfo;
+
+
+	IMG_HANDLE							hResItem;
+
+#if defined(SUPPORT_MEMINFO_IDS)
+	#if !defined(USE_CODE)
+
+	IMG_UINT64							ui64Stamp;
+	#else
+	IMG_UINT32							dummy1;
+	IMG_UINT32							dummy2;
+	#endif
+#endif
+
+	IMG_HANDLE							hOSMapInfo;
+
+	struct _PVRSRV_CLIENT_MEM_INFO_		*psNext;
+
+} PVRSRV_CLIENT_MEM_INFO, *PPVRSRV_CLIENT_MEM_INFO;
+
+
+#define PVRSRV_MAX_CLIENT_HEAPS (32)
+typedef struct _PVRSRV_HEAP_INFO_
+{
+	IMG_UINT32			ui32HeapID;
+	IMG_HANDLE 			hDevMemHeap;
+	IMG_DEV_VIRTADDR	sDevVAddrBase;
+	IMG_UINT32			ui32HeapByteSize;
+	IMG_UINT32			ui32Attribs;
+}PVRSRV_HEAP_INFO;
+
+
+
+
+typedef struct _PVRSRV_EVENTOBJECT_
+{
+
+	IMG_CHAR	szName[EVENTOBJNAME_MAXLENGTH];
+
+	IMG_HANDLE	hOSEventKM;
+
+} PVRSRV_EVENTOBJECT;
+
+typedef struct _PVRSRV_MISC_INFO_
+{
+	IMG_UINT32	ui32StateRequest;
+	IMG_UINT32	ui32StatePresent;
+
+
+	IMG_VOID	*pvSOCTimerRegisterKM;
+	IMG_VOID	*pvSOCTimerRegisterUM;
+	IMG_HANDLE	hSOCTimerRegisterOSMemHandle;
+	IMG_HANDLE	hSOCTimerRegisterMappingInfo;
+
+
+	IMG_VOID	*pvSOCClockGateRegs;
+	IMG_UINT32	ui32SOCClockGateRegsSize;
+
+
+	IMG_CHAR	*pszMemoryStr;
+	IMG_UINT32	ui32MemoryStrLen;
+
+
+	PVRSRV_EVENTOBJECT	sGlobalEventObject;
+	IMG_HANDLE			hOSGlobalEvent;
+
+
+	IMG_UINT32	aui32DDKVersion[4];
+
+
+
+	IMG_BOOL	bCPUCacheFlushAll;
+
+	IMG_BOOL	bDeferCPUCacheFlush;
+
+	IMG_PVOID	pvRangeAddrStart;
+
+	IMG_PVOID	pvRangeAddrEnd;
+
+} PVRSRV_MISC_INFO;
+
+
+typedef enum _PVRSRV_CLIENT_EVENT_
+{
+	PVRSRV_CLIENT_EVENT_HWTIMEOUT = 0,
+} PVRSRV_CLIENT_EVENT;
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVClientEvent(IMG_CONST PVRSRV_CLIENT_EVENT eEvent,
+											PVRSRV_DEV_DATA *psDevData,
+											IMG_PVOID pvData);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVConnect(PVRSRV_CONNECTION *psConnection);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDisconnect(PVRSRV_CONNECTION *psConnection);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVEnumerateDevices(IMG_CONST PVRSRV_CONNECTION 			*psConnection,
+													IMG_UINT32 					*puiNumDevices,
+													PVRSRV_DEVICE_IDENTIFIER 	*puiDevIDs);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVAcquireDeviceData(IMG_CONST PVRSRV_CONNECTION 	*psConnection,
+													IMG_UINT32			uiDevIndex,
+													PVRSRV_DEV_DATA		*psDevData,
+													PVRSRV_DEVICE_TYPE	eDeviceType);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetMiscInfo (IMG_CONST PVRSRV_CONNECTION *psConnection, PVRSRV_MISC_INFO *psMiscInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVReleaseMiscInfo (IMG_CONST PVRSRV_CONNECTION *psConnection, PVRSRV_MISC_INFO *psMiscInfo);
+
+#if 1
+IMG_IMPORT
+IMG_UINT32 ReadHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset);
+
+IMG_IMPORT
+IMG_VOID WriteHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset, IMG_UINT32 ui32Value);
+
+IMG_IMPORT IMG_VOID WriteHWRegs(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Count, PVRSRV_HWREG *psHWRegs);
+#endif
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVPollForValue ( const PVRSRV_CONNECTION *psConnection,
+							IMG_HANDLE hOSEvent,
+							volatile IMG_UINT32 *pui32LinMemAddr,
+							IMG_UINT32 ui32Value,
+							IMG_UINT32 ui32Mask,
+							IMG_UINT32 ui32Waitus,
+							IMG_UINT32 ui32Tries);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateDeviceMemContext(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+											IMG_HANDLE *phDevMemContext,
+											IMG_UINT32 *pui32SharedHeapCount,
+											PVRSRV_HEAP_INFO *psHeapInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyDeviceMemContext(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+											IMG_HANDLE 			hDevMemContext);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDeviceMemHeapInfo(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+											IMG_HANDLE hDevMemContext,
+											IMG_UINT32 *pui32SharedHeapCount,
+											PVRSRV_HEAP_INFO *psHeapInfo);
+
+#if defined(PVRSRV_LOG_MEMORY_ALLOCS)
+	#define PVRSRVAllocDeviceMem_log(psDevData, hDevMemHeap, ui32Attribs, ui32Size, ui32Alignment, ppsMemInfo, logStr) \
+		(PVR_TRACE(("PVRSRVAllocDeviceMem(" #psDevData "," #hDevMemHeap "," #ui32Attribs "," #ui32Size "," #ui32Alignment "," #ppsMemInfo ")" \
+			": " logStr " (size = 0x%lx)", ui32Size)), \
+		PVRSRVAllocDeviceMem(psDevData, hDevMemHeap, ui32Attribs, ui32Size, ui32Alignment, ppsMemInfo))
+#else
+	#define PVRSRVAllocDeviceMem_log(psDevData, hDevMemHeap, ui32Attribs, ui32Size, ui32Alignment, ppsMemInfo, logStr) \
+		PVRSRVAllocDeviceMem(psDevData, hDevMemHeap, ui32Attribs, ui32Size, ui32Alignment, ppsMemInfo)
+#endif
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVAllocDeviceMem(IMG_CONST PVRSRV_DEV_DATA	*psDevData,
+									IMG_HANDLE		hDevMemHeap,
+									IMG_UINT32		ui32Attribs,
+									IMG_SIZE_T		ui32Size,
+									IMG_SIZE_T		ui32Alignment,
+									PVRSRV_CLIENT_MEM_INFO	**ppsMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeDeviceMem(IMG_CONST PVRSRV_DEV_DATA	*psDevData,
+								PVRSRV_CLIENT_MEM_INFO		*psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVExportDeviceMem(IMG_CONST PVRSRV_DEV_DATA	*psDevData,
+												PVRSRV_CLIENT_MEM_INFO		*psMemInfo,
+												IMG_HANDLE					*phMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVReserveDeviceVirtualMem(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+											IMG_HANDLE			hDevMemHeap,
+											IMG_DEV_VIRTADDR	*psDevVAddr,
+											IMG_SIZE_T			ui32Size,
+											IMG_SIZE_T			ui32Alignment,
+											PVRSRV_CLIENT_MEM_INFO		**ppsMemInfo);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeDeviceVirtualMem(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+													PVRSRV_CLIENT_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapDeviceMemory (IMG_CONST PVRSRV_DEV_DATA *psDevData,
+									IMG_HANDLE hKernelMemInfo,
+									IMG_HANDLE hDstDevMemHeap,
+									PVRSRV_CLIENT_MEM_INFO **ppsDstMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapDeviceMemory (IMG_CONST PVRSRV_DEV_DATA *psDevData,
+										PVRSRV_CLIENT_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapExtMemory (IMG_CONST PVRSRV_DEV_DATA	*psDevData,
+									PVRSRV_CLIENT_MEM_INFO		*psMemInfo,
+									IMG_SYS_PHYADDR				*psSysPAddr,
+									IMG_UINT32					ui32Flags);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapExtMemory (IMG_CONST PVRSRV_DEV_DATA *psDevData,
+									PVRSRV_CLIENT_MEM_INFO		*psMemInfo,
+									IMG_UINT32					ui32Flags);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVWrapExtMemory2(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+												IMG_HANDLE				hDevMemContext,
+												IMG_SIZE_T 				ui32ByteSize,
+												IMG_SIZE_T				ui32PageOffset,
+												IMG_BOOL				bPhysContig,
+												IMG_SYS_PHYADDR	 		*psSysPAddr,
+												IMG_VOID 				*pvLinAddr,
+												IMG_UINT32				ui32Flags,
+												PVRSRV_CLIENT_MEM_INFO **ppsMemInfo);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVWrapExtMemory(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+												IMG_HANDLE				hDevMemContext,
+												IMG_SIZE_T 				ui32ByteSize,
+												IMG_SIZE_T				ui32PageOffset,
+												IMG_BOOL				bPhysContig,
+												IMG_SYS_PHYADDR	 		*psSysPAddr,
+												IMG_VOID 				*pvLinAddr,
+												PVRSRV_CLIENT_MEM_INFO **ppsMemInfo);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnwrapExtMemory (IMG_CONST PVRSRV_DEV_DATA *psDevData,
+												PVRSRV_CLIENT_MEM_INFO *psMemInfo);
+
+PVRSRV_ERROR PVRSRVChangeDeviceMemoryAttributes(IMG_CONST PVRSRV_DEV_DATA			*psDevData,
+												PVRSRV_CLIENT_MEM_INFO	*psClientMemInfo,
+												IMG_UINT32				ui32Attribs);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapDeviceClassMemory (IMG_CONST PVRSRV_DEV_DATA *psDevData,
+										IMG_HANDLE hDevMemContext,
+										IMG_HANDLE hDeviceClassBuffer,
+										PVRSRV_CLIENT_MEM_INFO **ppsMemInfo);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapDeviceClassMemory (IMG_CONST PVRSRV_DEV_DATA *psDevData,
+										PVRSRV_CLIENT_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapPhysToUserSpace(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+									  IMG_SYS_PHYADDR sSysPhysAddr,
+									  IMG_UINT32 uiSizeInBytes,
+									  IMG_PVOID *ppvUserAddr,
+									  IMG_UINT32 *puiActualSize,
+									  IMG_PVOID *ppvProcess);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapPhysToUserSpace(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+										IMG_PVOID pvUserAddr,
+										IMG_PVOID pvProcess);
+
+typedef enum _PVRSRV_SYNCVAL_MODE_
+{
+	PVRSRV_SYNCVAL_READ				= IMG_TRUE,
+	PVRSRV_SYNCVAL_WRITE			= IMG_FALSE,
+
+} PVRSRV_SYNCVAL_MODE, *PPVRSRV_SYNCVAL_MODE;
+
+typedef IMG_UINT32 PVRSRV_SYNCVAL;
+
+IMG_IMPORT PVRSRV_ERROR PVRSRVWaitForOpsComplete(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode, PVRSRV_SYNCVAL OpRequired);
+
+IMG_IMPORT PVRSRV_ERROR PVRSRVWaitForAllOpsComplete(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode);
+
+IMG_IMPORT IMG_BOOL PVRSRVTestOpsComplete(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode, PVRSRV_SYNCVAL OpRequired);
+
+IMG_IMPORT IMG_BOOL PVRSRVTestAllOpsComplete(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode);
+
+IMG_IMPORT IMG_BOOL PVRSRVTestOpsNotComplete(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode, PVRSRV_SYNCVAL OpRequired);
+
+IMG_IMPORT IMG_BOOL PVRSRVTestAllOpsNotComplete(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode);
+
+IMG_IMPORT PVRSRV_SYNCVAL PVRSRVGetPendingOpSyncVal(PPVRSRV_CLIENT_MEM_INFO psMemInfo,
+	PVRSRV_SYNCVAL_MODE eMode);
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVEnumerateDeviceClass(IMG_CONST PVRSRV_CONNECTION *psConnection,
+													PVRSRV_DEVICE_CLASS DeviceClass,
+													IMG_UINT32 *pui32DevCount,
+													IMG_UINT32 *pui32DevID);
+
+IMG_IMPORT
+IMG_HANDLE IMG_CALLCONV PVRSRVOpenDCDevice(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+											IMG_UINT32 ui32DeviceID);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCloseDCDevice(IMG_CONST PVRSRV_CONNECTION	*psConnection, IMG_HANDLE hDevice);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVEnumDCFormats (IMG_HANDLE hDevice,
+											IMG_UINT32		*pui32Count,
+											DISPLAY_FORMAT	*psFormat);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVEnumDCDims (IMG_HANDLE hDevice,
+										IMG_UINT32 		*pui32Count,
+										DISPLAY_FORMAT	*psFormat,
+										DISPLAY_DIMS	*psDims);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDCSystemBuffer(IMG_HANDLE hDevice,
+										IMG_HANDLE *phBuffer);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDCInfo(IMG_HANDLE hDevice,
+										DISPLAY_INFO* psDisplayInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateDCSwapChain (IMG_HANDLE				hDevice,
+													IMG_UINT32				ui32Flags,
+													DISPLAY_SURF_ATTRIBUTES	*psDstSurfAttrib,
+													DISPLAY_SURF_ATTRIBUTES	*psSrcSurfAttrib,
+													IMG_UINT32				ui32BufferCount,
+													IMG_UINT32				ui32OEMFlags,
+													IMG_UINT32				*pui32SwapChainID,
+													IMG_HANDLE				*phSwapChain);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyDCSwapChain (IMG_HANDLE hDevice,
+											IMG_HANDLE		hSwapChain);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSetDCDstRect (IMG_HANDLE hDevice,
+										IMG_HANDLE	hSwapChain,
+										IMG_RECT	*psDstRect);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSetDCSrcRect (IMG_HANDLE hDevice,
+										IMG_HANDLE	hSwapChain,
+										IMG_RECT	*psSrcRect);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSetDCDstColourKey (IMG_HANDLE hDevice,
+											IMG_HANDLE	hSwapChain,
+											IMG_UINT32	ui32CKColour);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSetDCSrcColourKey (IMG_HANDLE hDevice,
+											IMG_HANDLE	hSwapChain,
+											IMG_UINT32	ui32CKColour);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDCBuffers(IMG_HANDLE hDevice,
+									IMG_HANDLE hSwapChain,
+									IMG_HANDLE *phBuffer);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSwapToDCBuffer (IMG_HANDLE hDevice,
+										IMG_HANDLE hBuffer,
+										IMG_UINT32 ui32ClipRectCount,
+										IMG_RECT *psClipRect,
+										IMG_UINT32 ui32SwapInterval,
+										IMG_HANDLE hPrivateTag);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSwapToDCSystem (IMG_HANDLE hDevice,
+										IMG_HANDLE hSwapChain);
+
+
+IMG_IMPORT
+IMG_HANDLE IMG_CALLCONV PVRSRVOpenBCDevice(IMG_CONST PVRSRV_DEV_DATA *psDevData,
+											IMG_UINT32 ui32DeviceID);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCloseBCDevice(IMG_CONST PVRSRV_CONNECTION *psConnection,
+												IMG_HANDLE hDevice);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetBCBufferInfo(IMG_HANDLE hDevice,
+												BUFFER_INFO	*psBuffer);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetBCBuffer(IMG_HANDLE hDevice,
+												IMG_UINT32 ui32BufferIndex,
+												IMG_HANDLE *phBuffer);
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpInit(IMG_CONST PVRSRV_CONNECTION *psConnection);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpStartInitPhase(IMG_CONST PVRSRV_CONNECTION *psConnection);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpStopInitPhase(IMG_CONST PVRSRV_CONNECTION *psConnection);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpMemPol(IMG_CONST PVRSRV_CONNECTION *psConnection,
+										  PVRSRV_CLIENT_MEM_INFO *psMemInfo,
+										  IMG_UINT32 ui32Offset,
+										  IMG_UINT32 ui32Value,
+										  IMG_UINT32 ui32Mask,
+										  IMG_UINT32 ui32Flags);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpSyncPol(IMG_CONST PVRSRV_CONNECTION *psConnection,
+										  PVRSRV_CLIENT_SYNC_INFO *psClientSyncInfo,
+										  IMG_BOOL bIsRead,
+										  IMG_UINT32 ui32Value,
+										  IMG_UINT32 ui32Mask);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpMem(IMG_CONST PVRSRV_CONNECTION *psConnection,
+									IMG_PVOID pvAltLinAddr,
+									PVRSRV_CLIENT_MEM_INFO *psMemInfo,
+									IMG_UINT32 ui32Offset,
+									IMG_UINT32 ui32Bytes,
+									IMG_UINT32 ui32Flags);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpSync(IMG_CONST PVRSRV_CONNECTION *psConnection,
+										IMG_PVOID pvAltLinAddr,
+										PVRSRV_CLIENT_SYNC_INFO *psClientSyncInfo,
+										IMG_UINT32 ui32Offset,
+										IMG_UINT32 ui32Bytes);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpReg(IMG_CONST PVRSRV_CONNECTION *psConnection,
+											IMG_UINT32 ui32RegAddr,
+											IMG_UINT32 ui32RegValue,
+											IMG_UINT32 ui32Flags);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpRegPolWithFlags(IMG_CONST PVRSRV_CONNECTION *psConnection,
+													 IMG_UINT32 ui32RegAddr,
+													 IMG_UINT32 ui32RegValue,
+													 IMG_UINT32 ui32Mask,
+													 IMG_UINT32 ui32Flags);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpRegPol(IMG_CONST PVRSRV_CONNECTION *psConnection,
+											IMG_UINT32 ui32RegAddr,
+											IMG_UINT32 ui32RegValue,
+											IMG_UINT32 ui32Mask);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpPDReg(IMG_CONST PVRSRV_CONNECTION *psConnection,
+											IMG_UINT32 ui32RegAddr,
+											IMG_UINT32 ui32RegValue);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpPDDevPAddr(IMG_CONST PVRSRV_CONNECTION *psConnection,
+												PVRSRV_CLIENT_MEM_INFO *psMemInfo,
+												IMG_UINT32 ui32Offset,
+												IMG_DEV_PHYADDR sPDDevPAddr);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpMemPages(IMG_CONST PVRSRV_CONNECTION *psConnection,
+												IMG_HANDLE			hKernelMemInfo,
+												IMG_DEV_PHYADDR		*pPages,
+												IMG_UINT32			ui32NumPages,
+												IMG_DEV_VIRTADDR	sDevAddr,
+												IMG_UINT32			ui32Start,
+												IMG_UINT32			ui32Length,
+												IMG_BOOL			bContinuous);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpSetFrame(IMG_CONST PVRSRV_CONNECTION *psConnection,
+											  IMG_UINT32 ui32Frame);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpComment(IMG_CONST PVRSRV_CONNECTION *psConnection,
+											 IMG_CONST IMG_CHAR *pszComment,
+											 IMG_BOOL bContinuous);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpCommentf(IMG_CONST PVRSRV_CONNECTION *psConnection,
+											  IMG_BOOL bContinuous,
+											  IMG_CONST IMG_CHAR *pszFormat, ...);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpCommentWithFlagsf(IMG_CONST PVRSRV_CONNECTION *psConnection,
+													   IMG_UINT32 ui32Flags,
+													   IMG_CONST IMG_CHAR *pszFormat, ...);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpDriverInfo(IMG_CONST PVRSRV_CONNECTION *psConnection,
+								 				IMG_CHAR *pszString,
+												IMG_BOOL bContinuous);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpIsCapturing(IMG_CONST PVRSRV_CONNECTION *psConnection,
+								 				IMG_BOOL *pbIsCapturing);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpBitmap(IMG_CONST PVRSRV_CONNECTION *psConnection,
+								 			IMG_CHAR *pszFileName,
+											IMG_UINT32 ui32FileOffset,
+											IMG_UINT32 ui32Width,
+											IMG_UINT32 ui32Height,
+											IMG_UINT32 ui32StrideInBytes,
+											IMG_DEV_VIRTADDR sDevBaseAddr,
+											IMG_UINT32 ui32Size,
+											PDUMP_PIXEL_FORMAT ePixelFormat,
+											PDUMP_MEM_FORMAT eMemFormat,
+											IMG_UINT32 ui32PDumpFlags);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpRegRead(IMG_CONST PVRSRV_CONNECTION *psConnection,
+								 			IMG_CONST IMG_CHAR *pszFileName,
+											IMG_UINT32 ui32FileOffset,
+											IMG_UINT32 ui32Address,
+											IMG_UINT32 ui32Size,
+											IMG_UINT32 ui32PDumpFlags);
+
+
+IMG_IMPORT
+IMG_BOOL IMG_CALLCONV PVRSRVPDumpIsCapturingTest(IMG_CONST PVRSRV_CONNECTION *psConnection);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVPDumpCycleCountRegRead(IMG_CONST PVRSRV_CONNECTION *psConnection,
+														IMG_UINT32 ui32RegOffset,
+														IMG_BOOL bLastFrame);
+
+IMG_IMPORT IMG_HANDLE	PVRSRVLoadLibrary(const IMG_CHAR *pszLibraryName);
+IMG_IMPORT PVRSRV_ERROR	PVRSRVUnloadLibrary(IMG_HANDLE hExtDrv);
+IMG_IMPORT PVRSRV_ERROR	PVRSRVGetLibFuncAddr(IMG_HANDLE hExtDrv, const IMG_CHAR *pszFunctionName, IMG_VOID **ppvFuncAddr);
+
+IMG_IMPORT IMG_UINT32 PVRSRVClockus (void);
+IMG_IMPORT IMG_VOID PVRSRVWaitus (IMG_UINT32 ui32Timeus);
+IMG_IMPORT IMG_VOID PVRSRVReleaseThreadQuanta (void);
+IMG_IMPORT IMG_UINT32 IMG_CALLCONV PVRSRVGetCurrentProcessID(void);
+IMG_IMPORT IMG_CHAR * IMG_CALLCONV PVRSRVSetLocale(const IMG_CHAR *pszLocale);
+
+
+
+
+
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVCreateAppHintState(IMG_MODULE_ID eModuleID,
+														const IMG_CHAR *pszAppName,
+														IMG_VOID **ppvState);
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVFreeAppHintState(IMG_MODULE_ID eModuleID,
+										 IMG_VOID *pvHintState);
+
+IMG_IMPORT IMG_BOOL IMG_CALLCONV PVRSRVGetAppHint(IMG_VOID			*pvHintState,
+												  const IMG_CHAR	*pszHintName,
+												  IMG_DATA_TYPE		eDataType,
+												  const IMG_VOID	*pvDefault,
+												  IMG_VOID			*pvReturn);
+
+IMG_IMPORT IMG_PVOID IMG_CALLCONV PVRSRVAllocUserModeMem (IMG_SIZE_T ui32Size);
+IMG_IMPORT IMG_PVOID IMG_CALLCONV PVRSRVCallocUserModeMem (IMG_SIZE_T ui32Size);
+IMG_IMPORT IMG_PVOID IMG_CALLCONV PVRSRVReallocUserModeMem (IMG_PVOID pvBase, IMG_SIZE_T uNewSize);
+IMG_IMPORT IMG_VOID  IMG_CALLCONV PVRSRVFreeUserModeMem (IMG_PVOID pvMem);
+IMG_IMPORT IMG_VOID PVRSRVMemCopy(IMG_VOID *pvDst, const IMG_VOID *pvSrc, IMG_SIZE_T ui32Size);
+IMG_IMPORT IMG_VOID PVRSRVMemSet(IMG_VOID *pvDest, IMG_UINT8 ui8Value, IMG_SIZE_T ui32Size);
+
+struct _PVRSRV_MUTEX_OPAQUE_STRUCT_;
+typedef	struct  _PVRSRV_MUTEX_OPAQUE_STRUCT_ *PVRSRV_MUTEX_HANDLE;
+
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateMutex(PVRSRV_MUTEX_HANDLE *phMutex);
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyMutex(PVRSRV_MUTEX_HANDLE hMutex);
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVLockMutex(PVRSRV_MUTEX_HANDLE hMutex);
+IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVUnlockMutex(PVRSRV_MUTEX_HANDLE hMutex);
+
+#if (defined(DEBUG) && defined(__linux__))
+IMG_PVOID PVRSRVAllocUserModeMemTracking(IMG_SIZE_T ui32Size, IMG_CHAR *pszFileName, IMG_UINT32 ui32LineNumber);
+IMG_PVOID PVRSRVCallocUserModeMemTracking(IMG_SIZE_T ui32Size, IMG_CHAR *pszFileName, IMG_UINT32 ui32LineNumber);
+IMG_VOID  PVRSRVFreeUserModeMemTracking(IMG_VOID *pvMem);
+IMG_PVOID PVRSRVReallocUserModeMemTracking(IMG_VOID *pvMem, IMG_SIZE_T ui32NewSize, IMG_CHAR *pszFileName, IMG_UINT32 ui32LineNumber);
+#endif
+
+IMG_IMPORT PVRSRV_ERROR PVRSRVEventObjectWait(const PVRSRV_CONNECTION *psConnection,
+									IMG_HANDLE hOSEvent);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVModifyPendingSyncOps(PVRSRV_CONNECTION *psConnection,
+													  IMG_HANDLE hKernelSyncInfo,
+													  IMG_UINT32 ui32ModifyFlags,
+													  IMG_UINT32 *pui32ReadOpsPending,
+													  IMG_UINT32 *pui32WriteOpsPending);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVModifyCompleteSyncOps(PVRSRV_CONNECTION *psConnection,
+													  IMG_HANDLE hKernelSyncInfo,
+													  IMG_UINT32 ui32ModifyFlags);
+
+
+#define TIME_NOT_PASSED_UINT32(a,b,c)		((a - b) < c)
+
+#if defined (__cplusplus)
+}
+#endif
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/servicesext.h b/drivers/gpu/drm/emgd/pvr/include4/servicesext.h
new file mode 100644
index 0000000..0f3dc1d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/servicesext.h
@@ -0,0 +1,647 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__SERVICESEXT_H__)
+#define __SERVICESEXT_H__
+
+#define PVRSRV_LOCKFLG_READONLY     	(1)
+
+typedef enum _PVRSRV_ERROR_
+{
+	PVRSRV_OK								=  0,
+	PVRSRV_ERROR_GENERIC					=  1,
+	PVRSRV_ERROR_OUT_OF_MEMORY				=  2,
+	PVRSRV_ERROR_TOO_FEW_BUFFERS			=  3,
+	PVRSRV_ERROR_SYMBOL_NOT_FOUND			=  4,
+	PVRSRV_ERROR_OUT_OF_HSPACE				=  5,
+	PVRSRV_ERROR_INVALID_PARAMS				=  6,
+	PVRSRV_ERROR_TILE_MAP_FAILED			=  7,
+	PVRSRV_ERROR_INIT_FAILURE				=  8,
+	PVRSRV_ERROR_CANT_REGISTER_CALLBACK 	=  9,
+	PVRSRV_ERROR_INVALID_DEVICE				= 10,
+	PVRSRV_ERROR_NOT_OWNER					= 11,
+	PVRSRV_ERROR_BAD_MAPPING				= 12,
+	PVRSRV_ERROR_TIMEOUT					= 13,
+	PVRSRV_ERROR_NO_PRIMARY					= 14,
+	PVRSRV_ERROR_FLIP_CHAIN_EXISTS			= 15,
+	PVRSRV_ERROR_CANNOT_ACQUIRE_SYSDATA 	= 16,
+	PVRSRV_ERROR_SCENE_INVALID				= 17,
+	PVRSRV_ERROR_STREAM_ERROR				= 18,
+	PVRSRV_ERROR_INVALID_INTERRUPT      	= 19,
+	PVRSRV_ERROR_FAILED_DEPENDENCIES		= 20,
+	PVRSRV_ERROR_CMD_NOT_PROCESSED			= 21,
+	PVRSRV_ERROR_CMD_TOO_BIG				= 22,
+	PVRSRV_ERROR_DEVICE_REGISTER_FAILED 	= 23,
+	PVRSRV_ERROR_FIFO_SPACE					= 24,
+	PVRSRV_ERROR_TA_RECOVERY				= 25,
+	PVRSRV_ERROR_INDOSORLOWPOWER			= 26,
+	PVRSRV_ERROR_TOOMANYBUFFERS				= 27,
+	PVRSRV_ERROR_NOT_SUPPORTED				= 28,
+	PVRSRV_ERROR_PROCESSING_BLOCKED			= 29,
+
+
+	PVRSRV_ERROR_CANNOT_FLUSH_QUEUE			= 31,
+	PVRSRV_ERROR_CANNOT_GET_QUEUE_SPACE		= 32,
+	PVRSRV_ERROR_CANNOT_GET_RENDERDETAILS	= 33,
+	PVRSRV_ERROR_RETRY						= 34,
+
+	PVRSRV_ERROR_DDK_VERSION_MISMATCH		= 35,
+	PVRSRV_ERROR_BUILD_MISMATCH				= 36,
+	PVRSRV_ERROR_PDUMP_BUF_OVERFLOW,
+
+	PVRSRV_ERROR_FORCE_I32 = 0x7fffffff
+
+} PVRSRV_ERROR;
+
+
+typedef enum _PVRSRV_DEVICE_CLASS_
+{
+	PVRSRV_DEVICE_CLASS_3D				= 0 ,
+	PVRSRV_DEVICE_CLASS_DISPLAY			= 1 ,
+	PVRSRV_DEVICE_CLASS_BUFFER			= 2 ,
+	PVRSRV_DEVICE_CLASS_VIDEO			= 3 ,
+
+	PVRSRV_DEVICE_CLASS_FORCE_I32 		= 0x7fffffff
+
+} PVRSRV_DEVICE_CLASS;
+
+
+
+typedef enum _PVRSRV_SYS_POWER_STATE_
+{
+	PVRSRV_SYS_POWER_STATE_Unspecified		= -1,
+	PVRSRV_SYS_POWER_STATE_D0				= 0,
+	PVRSRV_SYS_POWER_STATE_D1				= 1,
+	PVRSRV_SYS_POWER_STATE_D2				= 2,
+	PVRSRV_SYS_POWER_STATE_D3				= 3,
+	PVRSRV_SYS_POWER_STATE_D4				= 4,
+
+	PVRSRV_SYS_POWER_STATE_FORCE_I32 = 0x7fffffff
+
+} PVRSRV_SYS_POWER_STATE, *PPVRSRV_SYS_POWER_STATE;
+
+
+typedef enum _PVRSRV_DEV_POWER_STATE_
+{
+	PVRSRV_DEV_POWER_STATE_DEFAULT	= -1,
+	PVRSRV_DEV_POWER_STATE_ON		= 0,
+	PVRSRV_DEV_POWER_STATE_IDLE		= 1,
+	PVRSRV_DEV_POWER_STATE_OFF		= 2,
+
+	PVRSRV_DEV_POWER_STATE_FORCE_I32 = 0x7fffffff
+
+} PVRSRV_DEV_POWER_STATE, *PPVRSRV_DEV_POWER_STATE;
+
+
+typedef PVRSRV_ERROR (*PFN_PRE_POWER) (IMG_HANDLE				hDevHandle,
+									   PVRSRV_DEV_POWER_STATE	eNewPowerState,
+									   PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+typedef PVRSRV_ERROR (*PFN_POST_POWER) (IMG_HANDLE				hDevHandle,
+										PVRSRV_DEV_POWER_STATE	eNewPowerState,
+										PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+
+typedef PVRSRV_ERROR (*PFN_PRE_CLOCKSPEED_CHANGE) (IMG_HANDLE				hDevHandle,
+												   IMG_BOOL					bIdleDevice,
+												   PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+typedef PVRSRV_ERROR (*PFN_POST_CLOCKSPEED_CHANGE) (IMG_HANDLE				hDevHandle,
+													IMG_BOOL				bIdleDevice,
+													PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+
+
+typedef enum _PVRSRV_PIXEL_FORMAT_ {
+
+	PVRSRV_PIXEL_FORMAT_UNKNOWN			=  0,
+	PVRSRV_PIXEL_FORMAT_RGB565			=  1,
+	PVRSRV_PIXEL_FORMAT_RGB555			=  2,
+	PVRSRV_PIXEL_FORMAT_RGB888			=  3,
+	PVRSRV_PIXEL_FORMAT_BGR888			=  4,
+	PVRSRV_PIXEL_FORMAT_GREY_SCALE		=  8,
+	PVRSRV_PIXEL_FORMAT_PAL12			= 13,
+	PVRSRV_PIXEL_FORMAT_PAL8			= 14,
+	PVRSRV_PIXEL_FORMAT_PAL4			= 15,
+	PVRSRV_PIXEL_FORMAT_PAL2			= 16,
+	PVRSRV_PIXEL_FORMAT_PAL1			= 17,
+	PVRSRV_PIXEL_FORMAT_ARGB1555		= 18,
+	PVRSRV_PIXEL_FORMAT_ARGB4444		= 19,
+	PVRSRV_PIXEL_FORMAT_ARGB8888		= 20,
+	PVRSRV_PIXEL_FORMAT_ABGR8888		= 21,
+	PVRSRV_PIXEL_FORMAT_YV12			= 22,
+	PVRSRV_PIXEL_FORMAT_I420			= 23,
+    PVRSRV_PIXEL_FORMAT_IMC2            = 25,
+	PVRSRV_PIXEL_FORMAT_XRGB8888        = 26,
+	PVRSRV_PIXEL_FORMAT_XBGR8888,
+	PVRSRV_PIXEL_FORMAT_BGRA8888,
+	PVRSRV_PIXEL_FORMAT_XRGB4444,
+	PVRSRV_PIXEL_FORMAT_ARGB8332,
+	PVRSRV_PIXEL_FORMAT_A2RGB10,
+	PVRSRV_PIXEL_FORMAT_A2BGR10,
+	PVRSRV_PIXEL_FORMAT_P8,
+	PVRSRV_PIXEL_FORMAT_L8,
+	PVRSRV_PIXEL_FORMAT_A8L8,
+	PVRSRV_PIXEL_FORMAT_A4L4,
+	PVRSRV_PIXEL_FORMAT_L16,
+	PVRSRV_PIXEL_FORMAT_L6V5U5,
+	PVRSRV_PIXEL_FORMAT_V8U8,
+	PVRSRV_PIXEL_FORMAT_V16U16,
+	PVRSRV_PIXEL_FORMAT_QWVU8888,
+	PVRSRV_PIXEL_FORMAT_XLVU8888,
+	PVRSRV_PIXEL_FORMAT_QWVU16,
+	PVRSRV_PIXEL_FORMAT_D16,
+	PVRSRV_PIXEL_FORMAT_D24S8,
+	PVRSRV_PIXEL_FORMAT_D24X8,
+
+
+	PVRSRV_PIXEL_FORMAT_ABGR16,
+	PVRSRV_PIXEL_FORMAT_ABGR16F,
+	PVRSRV_PIXEL_FORMAT_ABGR32,
+	PVRSRV_PIXEL_FORMAT_ABGR32F,
+	PVRSRV_PIXEL_FORMAT_B10GR11,
+	PVRSRV_PIXEL_FORMAT_GR88,
+	PVRSRV_PIXEL_FORMAT_BGR32,
+	PVRSRV_PIXEL_FORMAT_GR32,
+	PVRSRV_PIXEL_FORMAT_E5BGR9,
+
+
+	PVRSRV_PIXEL_FORMAT_DUMMY1,
+	PVRSRV_PIXEL_FORMAT_DUMMY2,
+	PVRSRV_PIXEL_FORMAT_DUMMY3,
+	PVRSRV_PIXEL_FORMAT_DUMMY4,
+	PVRSRV_PIXEL_FORMAT_DUMMY5,
+
+
+	PVRSRV_PIXEL_FORMAT_R8G8_B8G8,
+	PVRSRV_PIXEL_FORMAT_G8R8_G8B8,
+
+
+	PVRSRV_PIXEL_FORMAT_NV11,
+	PVRSRV_PIXEL_FORMAT_NV12,
+
+
+	PVRSRV_PIXEL_FORMAT_YUY2,
+	PVRSRV_PIXEL_FORMAT_YUV420,
+	PVRSRV_PIXEL_FORMAT_YUV444,
+	PVRSRV_PIXEL_FORMAT_VUY444,
+	PVRSRV_PIXEL_FORMAT_YUYV,
+	PVRSRV_PIXEL_FORMAT_YVYU,
+	PVRSRV_PIXEL_FORMAT_UYVY,
+	PVRSRV_PIXEL_FORMAT_VYUY,
+
+	PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY,
+	PVRSRV_PIXEL_FORMAT_FOURCC_ORG_YUYV,
+	PVRSRV_PIXEL_FORMAT_FOURCC_ORG_YVYU,
+	PVRSRV_PIXEL_FORMAT_FOURCC_ORG_VYUY,
+	PVRSRV_PIXEL_FORMAT_FOURCC_ORG_AYUV,
+
+
+	PVRSRV_PIXEL_FORMAT_A32B32G32R32,
+	PVRSRV_PIXEL_FORMAT_A32B32G32R32F,
+	PVRSRV_PIXEL_FORMAT_A32B32G32R32_UINT,
+	PVRSRV_PIXEL_FORMAT_A32B32G32R32_SINT,
+
+
+	PVRSRV_PIXEL_FORMAT_B32G32R32,
+	PVRSRV_PIXEL_FORMAT_B32G32R32F,
+	PVRSRV_PIXEL_FORMAT_B32G32R32_UINT,
+	PVRSRV_PIXEL_FORMAT_B32G32R32_SINT,
+
+
+	PVRSRV_PIXEL_FORMAT_G32R32,
+	PVRSRV_PIXEL_FORMAT_G32R32F,
+	PVRSRV_PIXEL_FORMAT_G32R32_UINT,
+	PVRSRV_PIXEL_FORMAT_G32R32_SINT,
+
+
+	PVRSRV_PIXEL_FORMAT_D32F,
+	PVRSRV_PIXEL_FORMAT_R32,
+	PVRSRV_PIXEL_FORMAT_R32F,
+	PVRSRV_PIXEL_FORMAT_R32_UINT,
+	PVRSRV_PIXEL_FORMAT_R32_SINT,
+
+
+	PVRSRV_PIXEL_FORMAT_A16B16G16R16,
+	PVRSRV_PIXEL_FORMAT_A16B16G16R16F,
+	PVRSRV_PIXEL_FORMAT_A16B16G16R16_SINT,
+	PVRSRV_PIXEL_FORMAT_A16B16G16R16_SNORM,
+	PVRSRV_PIXEL_FORMAT_A16B16G16R16_UINT,
+	PVRSRV_PIXEL_FORMAT_A16B16G16R16_UNORM,
+
+
+	PVRSRV_PIXEL_FORMAT_G16R16,
+	PVRSRV_PIXEL_FORMAT_G16R16F,
+	PVRSRV_PIXEL_FORMAT_G16R16_UINT,
+	PVRSRV_PIXEL_FORMAT_G16R16_UNORM,
+	PVRSRV_PIXEL_FORMAT_G16R16_SINT,
+	PVRSRV_PIXEL_FORMAT_G16R16_SNORM,
+
+
+	PVRSRV_PIXEL_FORMAT_R16,
+	PVRSRV_PIXEL_FORMAT_R16F,
+	PVRSRV_PIXEL_FORMAT_R16_UINT,
+	PVRSRV_PIXEL_FORMAT_R16_UNORM,
+	PVRSRV_PIXEL_FORMAT_R16_SINT,
+	PVRSRV_PIXEL_FORMAT_R16_SNORM,
+
+
+	PVRSRV_PIXEL_FORMAT_X8R8G8B8,
+	PVRSRV_PIXEL_FORMAT_X8R8G8B8_UNORM,
+	PVRSRV_PIXEL_FORMAT_X8R8G8B8_UNORM_SRGB,
+
+	PVRSRV_PIXEL_FORMAT_A8R8G8B8,
+	PVRSRV_PIXEL_FORMAT_A8R8G8B8_UNORM,
+	PVRSRV_PIXEL_FORMAT_A8R8G8B8_UNORM_SRGB,
+
+	PVRSRV_PIXEL_FORMAT_A8B8G8R8,
+	PVRSRV_PIXEL_FORMAT_A8B8G8R8_UINT,
+	PVRSRV_PIXEL_FORMAT_A8B8G8R8_UNORM,
+	PVRSRV_PIXEL_FORMAT_A8B8G8R8_UNORM_SRGB,
+	PVRSRV_PIXEL_FORMAT_A8B8G8R8_SINT,
+	PVRSRV_PIXEL_FORMAT_A8B8G8R8_SNORM,
+
+
+	PVRSRV_PIXEL_FORMAT_G8R8,
+	PVRSRV_PIXEL_FORMAT_G8R8_UINT,
+	PVRSRV_PIXEL_FORMAT_G8R8_UNORM,
+	PVRSRV_PIXEL_FORMAT_G8R8_SINT,
+	PVRSRV_PIXEL_FORMAT_G8R8_SNORM,
+
+
+	PVRSRV_PIXEL_FORMAT_A8,
+	PVRSRV_PIXEL_FORMAT_R8,
+	PVRSRV_PIXEL_FORMAT_R8_UINT,
+	PVRSRV_PIXEL_FORMAT_R8_UNORM,
+	PVRSRV_PIXEL_FORMAT_R8_SINT,
+	PVRSRV_PIXEL_FORMAT_R8_SNORM,
+
+
+	PVRSRV_PIXEL_FORMAT_A2B10G10R10,
+	PVRSRV_PIXEL_FORMAT_A2B10G10R10_UNORM,
+	PVRSRV_PIXEL_FORMAT_A2B10G10R10_UINT,
+
+
+	PVRSRV_PIXEL_FORMAT_B10G11R11,
+	PVRSRV_PIXEL_FORMAT_B10G11R11F,
+
+
+	PVRSRV_PIXEL_FORMAT_X24G8R32,
+	PVRSRV_PIXEL_FORMAT_G8R24,
+	PVRSRV_PIXEL_FORMAT_X8R24,
+	PVRSRV_PIXEL_FORMAT_E5B9G9R9,
+	PVRSRV_PIXEL_FORMAT_R1,
+
+	PVRSRV_PIXEL_FORMAT_DUMMY6,
+	PVRSRV_PIXEL_FORMAT_DUMMY7,
+	PVRSRV_PIXEL_FORMAT_DUMMY8,
+	PVRSRV_PIXEL_FORMAT_DUMMY9,
+	PVRSRV_PIXEL_FORMAT_DUMMY10,
+	PVRSRV_PIXEL_FORMAT_DUMMY11,
+	PVRSRV_PIXEL_FORMAT_DUMMY12,
+	PVRSRV_PIXEL_FORMAT_DUMMY13,
+	PVRSRV_PIXEL_FORMAT_DUMMY14,
+	PVRSRV_PIXEL_FORMAT_DUMMY15,
+	PVRSRV_PIXEL_FORMAT_DUMMY16,
+	PVRSRV_PIXEL_FORMAT_DUMMY17,
+	PVRSRV_PIXEL_FORMAT_DUMMY18,
+	PVRSRV_PIXEL_FORMAT_DUMMY19,
+	PVRSRV_PIXEL_FORMAT_DUMMY20,
+
+
+	PVRSRV_PIXEL_FORMAT_L_F16,
+	PVRSRV_PIXEL_FORMAT_L_F16_REP,
+	PVRSRV_PIXEL_FORMAT_L_F16_A_F16,
+	PVRSRV_PIXEL_FORMAT_A_F16,
+	PVRSRV_PIXEL_FORMAT_B16G16R16F,
+
+	PVRSRV_PIXEL_FORMAT_L_F32,
+	PVRSRV_PIXEL_FORMAT_A_F32,
+	PVRSRV_PIXEL_FORMAT_L_F32_A_F32,
+
+
+	PVRSRV_PIXEL_FORMAT_PVRTC2,
+	PVRSRV_PIXEL_FORMAT_PVRTC4,
+	PVRSRV_PIXEL_FORMAT_PVRTCII2,
+	PVRSRV_PIXEL_FORMAT_PVRTCII4,
+	PVRSRV_PIXEL_FORMAT_PVRTCIII,
+	PVRSRV_PIXEL_FORMAT_PVRO8,
+	PVRSRV_PIXEL_FORMAT_PVRO88,
+	PVRSRV_PIXEL_FORMAT_PT1,
+	PVRSRV_PIXEL_FORMAT_PT2,
+	PVRSRV_PIXEL_FORMAT_PT4,
+	PVRSRV_PIXEL_FORMAT_PT8,
+	PVRSRV_PIXEL_FORMAT_PTW,
+	PVRSRV_PIXEL_FORMAT_PTB,
+	PVRSRV_PIXEL_FORMAT_MONO8,
+	PVRSRV_PIXEL_FORMAT_MONO16,
+
+
+	PVRSRV_PIXEL_FORMAT_C0_YUYV,
+	PVRSRV_PIXEL_FORMAT_C0_UYVY,
+	PVRSRV_PIXEL_FORMAT_C0_YVYU,
+	PVRSRV_PIXEL_FORMAT_C0_VYUY,
+	PVRSRV_PIXEL_FORMAT_C1_YUYV,
+	PVRSRV_PIXEL_FORMAT_C1_UYVY,
+	PVRSRV_PIXEL_FORMAT_C1_YVYU,
+	PVRSRV_PIXEL_FORMAT_C1_VYUY,
+
+
+	PVRSRV_PIXEL_FORMAT_C0_YUV420_2P_UV,
+	PVRSRV_PIXEL_FORMAT_C0_YUV420_2P_VU,
+	PVRSRV_PIXEL_FORMAT_C0_YUV420_3P,
+	PVRSRV_PIXEL_FORMAT_C1_YUV420_2P_UV,
+	PVRSRV_PIXEL_FORMAT_C1_YUV420_2P_VU,
+	PVRSRV_PIXEL_FORMAT_C1_YUV420_3P,
+
+	PVRSRV_PIXEL_FORMAT_A2B10G10R10F,
+	PVRSRV_PIXEL_FORMAT_B8G8R8_SINT,
+	PVRSRV_PIXEL_FORMAT_PVRF32SIGNMASK,
+
+	PVRSRV_PIXEL_FORMAT_FORCE_I32 = 0x7fffffff,
+} PVRSRV_PIXEL_FORMAT;
+
+typedef enum _PVRSRV_ALPHA_FORMAT_ {
+	PVRSRV_ALPHA_FORMAT_UNKNOWN		=  0x00000000,
+	PVRSRV_ALPHA_FORMAT_PRE			=  0x00000001,
+	PVRSRV_ALPHA_FORMAT_NONPRE		=  0x00000002,
+	PVRSRV_ALPHA_FORMAT_MASK		=  0x0000000F,
+} PVRSRV_ALPHA_FORMAT;
+
+typedef enum _PVRSRV_COLOURSPACE_FORMAT_ {
+	PVRSRV_COLOURSPACE_FORMAT_UNKNOWN		=  0x00000000,
+	PVRSRV_COLOURSPACE_FORMAT_LINEAR		=  0x00010000,
+	PVRSRV_COLOURSPACE_FORMAT_NONLINEAR		=  0x00020000,
+	PVRSRV_COLOURSPACE_FORMAT_MASK			=  0x000F0000,
+} PVRSRV_COLOURSPACE_FORMAT;
+
+
+typedef enum _PVRSRV_ROTATION_ {
+	PVRSRV_ROTATE_0		=	0,
+	PVRSRV_ROTATE_90	=	1,
+	PVRSRV_ROTATE_180	=	2,
+	PVRSRV_ROTATE_270	=	3,
+	PVRSRV_FLIP_Y
+
+} PVRSRV_ROTATION;
+
+#define PVRSRV_CREATE_SWAPCHAIN_SHARED		(1<<0)
+#define PVRSRV_CREATE_SWAPCHAIN_QUERY		(1<<1)
+#define PVRSRV_CREATE_SWAPCHAIN_OEMOVERLAY	(1<<2)
+
+typedef struct _PVRSRV_SYNC_DATA_
+{
+
+	IMG_UINT32					ui32WriteOpsPending;
+	volatile IMG_UINT32			ui32WriteOpsComplete;
+
+
+	IMG_UINT32					ui32ReadOpsPending;
+	volatile IMG_UINT32			ui32ReadOpsComplete;
+
+
+	IMG_UINT32					ui32LastOpDumpVal;
+	IMG_UINT32					ui32LastReadOpDumpVal;
+
+} PVRSRV_SYNC_DATA;
+
+typedef struct _PVRSRV_CLIENT_SYNC_INFO_
+{
+
+	PVRSRV_SYNC_DATA			*psSyncData;
+
+
+
+
+
+	IMG_DEV_VIRTADDR		sWriteOpsCompleteDevVAddr;
+
+
+	IMG_DEV_VIRTADDR		sReadOpsCompleteDevVAddr;
+
+
+	IMG_HANDLE					hMappingInfo;
+
+
+	IMG_HANDLE					hKernelSyncInfo;
+
+} PVRSRV_CLIENT_SYNC_INFO, *PPVRSRV_CLIENT_SYNC_INFO;
+
+
+typedef struct PVRSRV_RESOURCE_TAG
+{
+	volatile IMG_UINT32 ui32Lock;
+	IMG_UINT32 			ui32ID;
+}PVRSRV_RESOURCE;
+typedef PVRSRV_RESOURCE PVRSRV_RES_HANDLE;
+
+
+typedef IMG_VOID (*PFN_CMD_COMPLETE) (IMG_HANDLE);
+typedef IMG_VOID (**PPFN_CMD_COMPLETE) (IMG_HANDLE);
+
+typedef IMG_BOOL (*PFN_CMD_PROC) (IMG_HANDLE, IMG_UINT32, IMG_VOID*);
+typedef IMG_BOOL (**PPFN_CMD_PROC) (IMG_HANDLE, IMG_UINT32, IMG_VOID*);
+
+
+typedef struct _IMG_RECT_
+{
+	IMG_INT32	x0;
+	IMG_INT32	y0;
+	IMG_INT32	x1;
+	IMG_INT32	y1;
+}IMG_RECT;
+
+typedef struct _IMG_RECT_16_
+{
+	IMG_INT16	x0;
+	IMG_INT16	y0;
+	IMG_INT16	x1;
+	IMG_INT16	y1;
+}IMG_RECT_16;
+
+
+typedef PVRSRV_ERROR (*PFN_GET_BUFFER_ADDR)(IMG_HANDLE,
+											IMG_HANDLE,
+											IMG_SYS_PHYADDR**,
+											IMG_SIZE_T*,
+											IMG_VOID**,
+											IMG_HANDLE*,
+											IMG_BOOL*,
+											IMG_BOOL*);
+
+typedef PVRSRV_ERROR (*PFN_GET_BUFFER_ID_FROM_TAG)(IMG_HANDLE, IMG_UINT32, IMG_HANDLE);
+
+typedef struct DISPLAY_DIMS_TAG
+{
+	IMG_UINT32	ui32ByteStride;
+	IMG_UINT32	ui32Width;
+	IMG_UINT32	ui32Height;
+} DISPLAY_DIMS;
+
+
+typedef struct DISPLAY_FORMAT_TAG
+{
+
+	PVRSRV_PIXEL_FORMAT		pixelformat;
+} DISPLAY_FORMAT;
+
+typedef struct DISPLAY_SURF_ATTRIBUTES_TAG
+{
+
+	PVRSRV_PIXEL_FORMAT		pixelformat;
+
+	DISPLAY_DIMS			sDims;
+	IMG_UINT32			ui32Reseved;
+} DISPLAY_SURF_ATTRIBUTES;
+
+
+typedef struct DISPLAY_MODE_INFO_TAG
+{
+
+	PVRSRV_PIXEL_FORMAT		pixelformat;
+
+	DISPLAY_DIMS			sDims;
+
+	IMG_UINT32				ui32RefreshHZ;
+
+	IMG_UINT32				ui32OEMFlags;
+} DISPLAY_MODE_INFO;
+
+
+
+#define MAX_DISPLAY_NAME_SIZE	(50)
+
+typedef struct DISPLAY_INFO_TAG
+{
+
+	IMG_UINT32 ui32MaxSwapChains;
+
+	IMG_UINT32 ui32MaxSwapChainBuffers;
+
+	IMG_UINT32 ui32MinSwapInterval;
+
+	IMG_UINT32 ui32MaxSwapInterval;
+
+	IMG_UINT32 ui32PhysicalWidthmm;
+	IMG_UINT32 ui32PhysicalHeightmm;
+
+	IMG_CHAR	szDisplayName[MAX_DISPLAY_NAME_SIZE];
+#if defined(SUPPORT_HW_CURSOR)
+
+	IMG_UINT16	ui32CursorWidth;
+	IMG_UINT16	ui32CursorHeight;
+#endif
+} DISPLAY_INFO;
+
+typedef struct ACCESS_INFO_TAG
+{
+	IMG_UINT32		ui32Size;
+	IMG_UINT32  	ui32FBPhysBaseAddress;
+	IMG_UINT32		ui32FBMemAvailable;
+	IMG_UINT32  	ui32SysPhysBaseAddress;
+	IMG_UINT32		ui32SysSize;
+	IMG_UINT32		ui32DevIRQ;
+}ACCESS_INFO;
+
+
+typedef struct PVRSRV_CURSOR_SHAPE_TAG
+{
+	IMG_UINT16			ui16Width;
+	IMG_UINT16			ui16Height;
+	IMG_INT16			i16XHot;
+	IMG_INT16			i16YHot;
+
+
+	IMG_VOID*   		pvMask;
+	IMG_INT16  			i16MaskByteStride;
+
+
+	IMG_VOID*			pvColour;
+	IMG_INT16			i16ColourByteStride;
+	PVRSRV_PIXEL_FORMAT	eColourPixelFormat;
+} PVRSRV_CURSOR_SHAPE;
+
+#define PVRSRV_SET_CURSOR_VISIBILITY	(1<<0)
+#define PVRSRV_SET_CURSOR_POSITION		(1<<1)
+#define PVRSRV_SET_CURSOR_SHAPE			(1<<2)
+#define PVRSRV_SET_CURSOR_ROTATION		(1<<3)
+
+typedef struct PVRSRV_CURSOR_INFO_TAG
+{
+
+	IMG_UINT32 ui32Flags;
+
+
+	IMG_BOOL bVisible;
+
+
+	IMG_INT16 i16XPos;
+	IMG_INT16 i16YPos;
+
+
+	PVRSRV_CURSOR_SHAPE sCursorShape;
+
+
+	IMG_UINT32 ui32Rotation;
+
+} PVRSRV_CURSOR_INFO;
+
+
+typedef struct _PVRSRV_REGISTRY_INFO_
+{
+    IMG_UINT32		ui32DevCookie;
+    IMG_PCHAR		pszKey;
+    IMG_PCHAR		pszValue;
+    IMG_PCHAR		pszBuf;
+    IMG_UINT32		ui32BufSize;
+} PVRSRV_REGISTRY_INFO, *PPVRSRV_REGISTRY_INFO;
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVReadRegistryString (PPVRSRV_REGISTRY_INFO psRegInfo);
+PVRSRV_ERROR IMG_CALLCONV PVRSRVWriteRegistryString (PPVRSRV_REGISTRY_INFO psRegInfo);
+
+
+#define PVRSRV_BC_FLAGS_YUVCSC_CONFORMANT_RANGE	(0 << 0)
+#define PVRSRV_BC_FLAGS_YUVCSC_FULL_RANGE		(1 << 0)
+
+#define PVRSRV_BC_FLAGS_YUVCSC_BT601			(0 << 1)
+#define PVRSRV_BC_FLAGS_YUVCSC_BT709			(1 << 1)
+
+#define MAX_BUFFER_DEVICE_NAME_SIZE	(50)
+
+typedef struct BUFFER_INFO_TAG
+{
+	IMG_UINT32 			ui32BufferCount;
+	IMG_UINT32			ui32BufferDeviceID;
+	PVRSRV_PIXEL_FORMAT	pixelformat;
+	IMG_UINT32			ui32ByteStride;
+	IMG_UINT32			ui32Width;
+	IMG_UINT32			ui32Height;
+	IMG_UINT32			ui32Flags;
+	IMG_CHAR			szDeviceName[MAX_BUFFER_DEVICE_NAME_SIZE];
+} BUFFER_INFO;
+
+typedef enum _OVERLAY_DEINTERLACE_MODE_
+{
+	WEAVE=0x0,
+	BOB_ODD,
+	BOB_EVEN,
+	BOB_EVEN_NONINTERLEAVED
+} OVERLAY_DEINTERLACE_MODE;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/include4/sgx_options.h b/drivers/gpu/drm/emgd/pvr/include4/sgx_options.h
new file mode 100644
index 0000000..75d1286
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/sgx_options.h
@@ -0,0 +1,220 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if defined(DEBUG) || defined (INTERNAL_TEST)
+#define DEBUG_SET_OFFSET	OPTIONS_BIT0
+#define OPTIONS_BIT0		0x1
+#else
+#define OPTIONS_BIT0		0x0
+#endif
+
+#if defined(PDUMP) || defined (INTERNAL_TEST)
+#define PDUMP_SET_OFFSET	OPTIONS_BIT1
+#define OPTIONS_BIT1		(0x1 << 1)
+#else
+#define OPTIONS_BIT1		0x0
+#endif
+
+#if defined(PVRSRV_USSE_EDM_STATUS_DEBUG) || defined (INTERNAL_TEST)
+#define PVRSRV_USSE_EDM_STATUS_DEBUG_SET_OFFSET		OPTIONS_BIT2
+#define OPTIONS_BIT2		(0x1 << 2)
+#else
+#define OPTIONS_BIT2		0x0
+#endif
+
+#if defined(SUPPORT_HW_RECOVERY) || defined (INTERNAL_TEST)
+#define SUPPORT_HW_RECOVERY_SET_OFFSET	OPTIONS_BIT3
+#define OPTIONS_BIT3		(0x1 << 3)
+#else
+#define OPTIONS_BIT3		0x0
+#endif
+
+
+
+#if defined(PVR_SECURE_HANDLES) || defined (INTERNAL_TEST)
+#define PVR_SECURE_HANDLES_SET_OFFSET	OPTIONS_BIT4
+#define OPTIONS_BIT4		(0x1 << 4)
+#else
+#define OPTIONS_BIT4		0x0
+#endif
+
+#if defined(SGX_BYPASS_SYSTEM_CACHE) || defined (INTERNAL_TEST)
+#define SGX_BYPASS_SYSTEM_CACHE_SET_OFFSET	OPTIONS_BIT5
+#define OPTIONS_BIT5		(0x1 << 5)
+#else
+#define OPTIONS_BIT5		0x0
+#endif
+
+#if defined(SGX_DMS_AGE_ENABLE) || defined (INTERNAL_TEST)
+#define SGX_DMS_AGE_ENABLE_SET_OFFSET	OPTIONS_BIT6
+#define OPTIONS_BIT6		(0x1 << 6)
+#else
+#define OPTIONS_BIT6		0x0
+#endif
+
+#if defined(SGX_FAST_DPM_INIT) || defined (INTERNAL_TEST)
+#define SGX_FAST_DPM_INIT_SET_OFFSET	OPTIONS_BIT8
+#define OPTIONS_BIT8		(0x1 << 8)
+#else
+#define OPTIONS_BIT8		0x0
+#endif
+
+#if defined(SGX_FEATURE_DCU) || defined (INTERNAL_TEST)
+#define SGX_FEATURE_DCU_SET_OFFSET	OPTIONS_BIT9
+#define OPTIONS_BIT9		(0x1 << 9)
+#else
+#define OPTIONS_BIT9		0x0
+#endif
+
+#if defined(SGX_FEATURE_MP) || defined (INTERNAL_TEST)
+#define SGX_FEATURE_MP_SET_OFFSET	OPTIONS_BIT10
+#define OPTIONS_BIT10		(0x1 << 10)
+#else
+#define OPTIONS_BIT10		0x0
+#endif
+
+#if defined(SGX_FEATURE_MULTITHREADED_UKERNEL) || defined (INTERNAL_TEST)
+#define SGX_FEATURE_MULTITHREADED_UKERNEL_SET_OFFSET	OPTIONS_BIT11
+#define OPTIONS_BIT11		(0x1 << 11)
+#else
+#define OPTIONS_BIT11		0x0
+#endif
+
+
+
+#if defined(SGX_FEATURE_OVERLAPPED_SPM) || defined (INTERNAL_TEST)
+#define SGX_FEATURE_OVERLAPPED_SPM_SET_OFFSET	OPTIONS_BIT12
+#define OPTIONS_BIT12		(0x1 << 12)
+#else
+#define OPTIONS_BIT12		0x0
+#endif
+
+
+#if defined(SGX_FEATURE_SYSTEM_CACHE) || defined (INTERNAL_TEST)
+#define SGX_FEATURE_SYSTEM_CACHE_SET_OFFSET	OPTIONS_BIT13
+#define OPTIONS_BIT13		(0x1 << 13)
+#else
+#define OPTIONS_BIT13		0x0
+#endif
+
+#if defined(SGX_SUPPORT_HWPROFILING) || defined (INTERNAL_TEST)
+#define SGX_SUPPORT_HWPROFILING_SET_OFFSET	OPTIONS_BIT14
+#define OPTIONS_BIT14		(0x1 << 14)
+#else
+#define OPTIONS_BIT14		0x0
+#endif
+
+
+
+#if defined(SUPPORT_ACTIVE_POWER_MANAGEMENT) || defined (INTERNAL_TEST)
+#define SUPPORT_ACTIVE_POWER_MANAGEMENT_SET_OFFSET	OPTIONS_BIT15
+#define OPTIONS_BIT15		(0x1 << 15)
+#else
+#define OPTIONS_BIT15		0x0
+#endif
+
+#if defined(SUPPORT_DISPLAYCONTROLLER_TILING) || defined (INTERNAL_TEST)
+#define SUPPORT_DISPLAYCONTROLLER_TILING_SET_OFFSET	OPTIONS_BIT16
+#define OPTIONS_BIT16		(0x1 << 16)
+#else
+#define OPTIONS_BIT16		0x0
+#endif
+
+#if defined(SUPPORT_PERCONTEXT_PB) || defined (INTERNAL_TEST)
+#define SUPPORT_PERCONTEXT_PB_SET_OFFSET	OPTIONS_BIT17
+#define OPTIONS_BIT17		(0x1 << 17)
+#else
+#define OPTIONS_BIT17		0x0
+#endif
+
+#if defined(SUPPORT_SGX_HWPERF) || defined (INTERNAL_TEST)
+#define SUPPORT_SGX_HWPERF_SET_OFFSET	OPTIONS_BIT18
+#define OPTIONS_BIT18		(0x1 << 18)
+#else
+#define OPTIONS_BIT18		0x0
+#endif
+
+
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE) || defined (INTERNAL_TEST)
+#define SUPPORT_SGX_MMU_DUMMY_PAGE_SET_OFFSET	OPTIONS_BIT19
+#define OPTIONS_BIT19		(0x1 << 19)
+#else
+#define OPTIONS_BIT19		0x0
+#endif
+
+#if defined(SUPPORT_SGX_PRIORITY_SCHEDULING) || defined (INTERNAL_TEST)
+#define SUPPORT_SGX_PRIORITY_SCHEDULING_SET_OFFSET	OPTIONS_BIT20
+#define OPTIONS_BIT20		(0x1 << 20)
+#else
+#define OPTIONS_BIT20		0x0
+#endif
+
+#if defined(SGX_LOW_LATENCY_SCHEDULING) || defined (INTERNAL_TEST)
+#define SUPPORT_SGX_LOW_LATENCY_SCHEDULING_SET_OFFSET	OPTIONS_BIT21
+#define OPTIONS_BIT21		(0x1 << 21)
+#else
+#define OPTIONS_BIT21		0x0
+#endif
+
+#if defined(USE_SUPPORT_NO_TA3D_OVERLAP) || defined (INTERNAL_TEST)
+#define USE_SUPPORT_NO_TA3D_OVERLAP_SET_OFFSET	OPTIONS_BIT22
+#define OPTIONS_BIT22		(0x1 << 22)
+#else
+#define OPTIONS_BIT22		0x0
+#endif
+
+
+#if defined(SGX_FEATURE_MP) || defined (INTERNAL_TEST)
+#define OPTIONS_HIGHBYTE ((SGX_FEATURE_MP_CORE_COUNT-1) << SGX_FEATURE_MP_CORE_COUNT_SET_OFFSET)
+#define SGX_FEATURE_MP_CORE_COUNT_SET_OFFSET	28UL
+#define SGX_FEATURE_MP_CORE_COUNT_SET_MASK		0xFF
+#else
+#define OPTIONS_HIGHBYTE	0x0
+#endif
+
+
+
+#define SGX_BUILD_OPTIONS	\
+	OPTIONS_BIT0 |\
+	OPTIONS_BIT1 |\
+	OPTIONS_BIT2 |\
+	OPTIONS_BIT3 |\
+	OPTIONS_BIT4 |\
+	OPTIONS_BIT5 |\
+	OPTIONS_BIT6 |\
+	OPTIONS_BIT8 |\
+	OPTIONS_BIT9 |\
+	OPTIONS_BIT10 |\
+	OPTIONS_BIT11 |\
+	OPTIONS_BIT12 |\
+	OPTIONS_BIT13 |\
+	OPTIONS_BIT14 |\
+	OPTIONS_BIT15 |\
+	OPTIONS_BIT16 |\
+	OPTIONS_BIT17 |\
+	OPTIONS_BIT18 |\
+	OPTIONS_BIT19 |\
+	OPTIONS_BIT20 |\
+	OPTIONS_BIT21 |\
+	OPTIONS_HIGHBYTE
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/sgxapi_km.h b/drivers/gpu/drm/emgd/pvr/include4/sgxapi_km.h
new file mode 100644
index 0000000..5302204
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/sgxapi_km.h
@@ -0,0 +1,323 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __SGXAPI_KM_H__
+#define __SGXAPI_KM_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "sgxdefs.h"
+
+#if defined(__linux__) && !defined(USE_CODE)
+	#if defined(__KERNEL__)
+		#include <asm/unistd.h>
+	#else
+		#include <unistd.h>
+	#endif
+#endif
+
+#define SGX_UNDEFINED_HEAP_ID					(~0LU)
+#define SGX_GENERAL_HEAP_ID						0
+#define SGX_TADATA_HEAP_ID						1
+#define SGX_KERNEL_CODE_HEAP_ID					2
+#define SGX_KERNEL_DATA_HEAP_ID					3
+#define SGX_PIXELSHADER_HEAP_ID					4
+#define SGX_VERTEXSHADER_HEAP_ID				5
+#define SGX_PDSPIXEL_CODEDATA_HEAP_ID			6
+#define SGX_PDSVERTEX_CODEDATA_HEAP_ID			7
+#define SGX_SYNCINFO_HEAP_ID					8
+#define SGX_3DPARAMETERS_HEAP_ID				9
+#if defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+#define SGX_GENERAL_MAPPING_HEAP_ID				10
+#endif
+#if defined(SGX_FEATURE_2D_HARDWARE)
+#define SGX_2D_HEAP_ID							11
+#else
+#if defined(FIX_HW_BRN_26915)
+#define SGX_CGBUFFER_HEAP_ID					12
+#endif
+#endif
+#if defined(SUPPORT_SGX_VIDEO_HEAP)
+#define SGX_VIDEO_HEAP_ID						13
+#define SGX_MAX_HEAP_ID							14
+#else
+#define SGX_MAX_HEAP_ID							13
+#endif
+
+#define SGX_MAX_TA_STATUS_VALS	32
+#define SGX_MAX_3D_STATUS_VALS	3
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+#define SGX_MAX_TA_DST_SYNCS			1
+#define SGX_MAX_TA_SRC_SYNCS			1
+#define SGX_MAX_3D_SRC_SYNCS			4
+#else
+#define SGX_MAX_SRC_SYNCS				4
+#endif
+
+#ifdef SUPPORT_SGX_HWPERF
+
+#define	PVRSRV_SGX_HWPERF_NUM_COUNTERS	9
+
+#define PVRSRV_SGX_HWPERF_INVALID					0x1
+
+#define PVRSRV_SGX_HWPERF_TRANSFER					0x2
+#define PVRSRV_SGX_HWPERF_TA						0x3
+#define PVRSRV_SGX_HWPERF_3D						0x4
+#define PVRSRV_SGX_HWPERF_2D						0x5
+
+#define PVRSRV_SGX_HWPERF_MK_EVENT					0x101
+#define PVRSRV_SGX_HWPERF_MK_TA						0x102
+#define PVRSRV_SGX_HWPERF_MK_3D						0x103
+#define PVRSRV_SGX_HWPERF_MK_2D						0x104
+
+#define PVRSRV_SGX_HWPERF_TYPE_STARTEND_BIT			28
+#define PVRSRV_SGX_HWPERF_TYPE_OP_MASK				((1UL << PVRSRV_SGX_HWPERF_TYPE_STARTEND_BIT) - 1)
+#define PVRSRV_SGX_HWPERF_TYPE_OP_START				(0UL << PVRSRV_SGX_HWPERF_TYPE_STARTEND_BIT)
+#define PVRSRV_SGX_HWPERF_TYPE_OP_END				(1Ul << PVRSRV_SGX_HWPERF_TYPE_STARTEND_BIT)
+
+#define PVRSRV_SGX_HWPERF_TYPE_TRANSFER_START		(PVRSRV_SGX_HWPERF_TRANSFER | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_TRANSFER_END			(PVRSRV_SGX_HWPERF_TRANSFER | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+#define PVRSRV_SGX_HWPERF_TYPE_TA_START				(PVRSRV_SGX_HWPERF_TA | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_TA_END				(PVRSRV_SGX_HWPERF_TA | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+#define PVRSRV_SGX_HWPERF_TYPE_3D_START				(PVRSRV_SGX_HWPERF_3D | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_3D_END				(PVRSRV_SGX_HWPERF_3D | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+#define PVRSRV_SGX_HWPERF_TYPE_2D_START				(PVRSRV_SGX_HWPERF_2D | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_2D_END				(PVRSRV_SGX_HWPERF_2D | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+
+#define PVRSRV_SGX_HWPERF_TYPE_MK_EVENT_START		(PVRSRV_SGX_HWPERF_MK_EVENT | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_EVENT_END			(PVRSRV_SGX_HWPERF_MK_EVENT | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_TA_START			(PVRSRV_SGX_HWPERF_MK_TA | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_TA_END			(PVRSRV_SGX_HWPERF_MK_TA | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_3D_START			(PVRSRV_SGX_HWPERF_MK_3D | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_3D_END			(PVRSRV_SGX_HWPERF_MK_3D | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_2D_START			(PVRSRV_SGX_HWPERF_MK_2D | PVRSRV_SGX_HWPERF_TYPE_OP_START)
+#define PVRSRV_SGX_HWPERF_TYPE_MK_2D_END			(PVRSRV_SGX_HWPERF_MK_2D | PVRSRV_SGX_HWPERF_TYPE_OP_END)
+
+#define PVRSRV_SGX_HWPERF_OFF						(0x0)
+#define PVRSRV_SGX_HWPERF_GRAPHICS_ON				(1UL << 0)
+#define PVRSRV_SGX_HWPERF_MK_EXECUTION_ON			(1UL << 1)
+
+
+typedef struct _PVRSRV_SGX_HWPERF_CB_ENTRY_
+{
+	IMG_UINT32	ui32FrameNo;
+	IMG_UINT32	ui32Type;
+	IMG_UINT32	ui32Ordinal;
+	IMG_UINT32	ui32Clocksx16;
+	IMG_UINT32	ui32Counters[PVRSRV_SGX_HWPERF_NUM_COUNTERS];
+} PVRSRV_SGX_HWPERF_CB_ENTRY;
+
+
+typedef struct _PVRSRV_SGX_HWPERF_CBDATA_
+{
+	IMG_UINT32	ui32FrameNo;
+	IMG_UINT32	ui32Type;
+	IMG_UINT32	ui32StartTimeWraps;
+	IMG_UINT32	ui32StartTime;
+	IMG_UINT32	ui32EndTimeWraps;
+	IMG_UINT32	ui32EndTime;
+	IMG_UINT32	ui32ClockSpeed;
+	IMG_UINT32	ui32TimeMax;
+} PVRSRV_SGX_HWPERF_CBDATA;
+
+
+typedef struct _SGX_MISC_INFO_HWPERF_RETRIEVE_CB
+{
+	PVRSRV_SGX_HWPERF_CBDATA*	psHWPerfData;
+	IMG_UINT32					ui32ArraySize;
+	IMG_UINT32					ui32DataCount;
+	IMG_UINT32					ui32Time;
+} SGX_MISC_INFO_HWPERF_RETRIEVE_CB;
+#endif
+
+
+typedef struct _CTL_STATUS_
+{
+	IMG_DEV_VIRTADDR	sStatusDevAddr;
+	IMG_UINT32			ui32StatusValue;
+} CTL_STATUS;
+
+
+typedef enum _SGX_MISC_INFO_REQUEST_
+{
+	SGX_MISC_INFO_REQUEST_CLOCKSPEED = 0,
+	SGX_MISC_INFO_REQUEST_SGXREV,
+	SGX_MISC_INFO_REQUEST_DRIVER_SGXREV,
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+	SGX_MISC_INFO_REQUEST_MEMREAD,
+#endif
+#if defined(SUPPORT_SGX_HWPERF)
+	SGX_MISC_INFO_REQUEST_SET_HWPERF_STATUS,
+	SGX_MISC_INFO_REQUEST_HWPERF_CB_ON,
+	SGX_MISC_INFO_REQUEST_HWPERF_CB_OFF,
+	SGX_MISC_INFO_REQUEST_HWPERF_RETRIEVE_CB,
+#endif
+#if defined(SGX_FEATURE_DATA_BREAKPOINTS)
+	SGX_MISC_INFO_REQUEST_SET_BREAKPOINT,
+#endif
+	SGX_MISC_INFO_DUMP_DEBUG_INFO,
+	SGX_MISC_INFO_PANIC,
+	SGX_MISC_INFO_REQUEST_FORCE_I16 				=  0x7fff
+} SGX_MISC_INFO_REQUEST;
+
+
+typedef struct _PVRSRV_SGX_MISCINFO_FEATURES
+{
+	IMG_UINT32			ui32CoreRev;
+	IMG_UINT32			ui32CoreID;
+	IMG_UINT32			ui32DDKVersion;
+	IMG_UINT32			ui32DDKBuild;
+	IMG_UINT32			ui32CoreIdSW;
+	IMG_UINT32			ui32CoreRevSW;
+	IMG_UINT32			ui32BuildOptions;
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+	IMG_UINT32			ui32DeviceMemValue;
+#endif
+} PVRSRV_SGX_MISCINFO_FEATURES;
+
+
+#if defined(SGX_FEATURE_DATA_BREAKPOINTS)
+typedef struct _SGX_BREAKPOINT_INFO
+{
+
+	IMG_BOOL					bBPEnable;
+
+
+
+	IMG_UINT32					ui32BPIndex;
+
+	IMG_DEV_VIRTADDR			sBPDevVAddr;
+} SGX_BREAKPOINT_INFO;
+#endif
+
+typedef struct _SGX_MISC_INFO_
+{
+	SGX_MISC_INFO_REQUEST	eRequest;
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+	IMG_DEV_VIRTADDR			sDevVAddr;
+	IMG_HANDLE					hDevMemContext;
+#endif
+	union
+	{
+		IMG_UINT32	reserved;
+		PVRSRV_SGX_MISCINFO_FEATURES						sSGXFeatures;
+		IMG_UINT32											ui32SGXClockSpeed;
+#if defined(SGX_FEATURE_DATA_BREAKPOINTS)
+		SGX_BREAKPOINT_INFO									sSGXBreakpointInfo;
+#endif
+#ifdef SUPPORT_SGX_HWPERF
+		IMG_UINT32											ui32NewHWPerfStatus;
+		SGX_MISC_INFO_HWPERF_RETRIEVE_CB					sRetrieveCB;
+#endif
+	} uData;
+} SGX_MISC_INFO;
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+#define PVRSRV_MAX_BLT_SRC_SYNCS		3
+#endif
+
+
+#define SGX_KICKTA_DUMPBITMAP_MAX_NAME_LENGTH		256
+
+typedef struct _SGX_KICKTA_DUMPBITMAP_
+{
+	IMG_DEV_VIRTADDR	sDevBaseAddr;
+	IMG_UINT32			ui32Flags;
+	IMG_UINT32			ui32Width;
+	IMG_UINT32			ui32Height;
+	IMG_UINT32			ui32Stride;
+	IMG_UINT32			ui32PDUMPFormat;
+	IMG_UINT32			ui32BytesPP;
+	IMG_CHAR			pszName[SGX_KICKTA_DUMPBITMAP_MAX_NAME_LENGTH];
+} SGX_KICKTA_DUMPBITMAP, *PSGX_KICKTA_DUMPBITMAP;
+
+#define PVRSRV_SGX_PDUMP_CONTEXT_MAX_BITMAP_ARRAY_SIZE	(16)
+
+typedef struct _PVRSRV_SGX_PDUMP_CONTEXT_
+{
+
+	IMG_UINT32						ui32CacheControl;
+
+} PVRSRV_SGX_PDUMP_CONTEXT;
+
+
+typedef struct _SGX_KICKTA_DUMP_ROFF_
+{
+	IMG_HANDLE			hKernelMemInfo;
+	IMG_UINT32			uiAllocIndex;
+	IMG_UINT32			ui32Offset;
+	IMG_UINT32			ui32Value;
+	IMG_PCHAR			pszName;
+} SGX_KICKTA_DUMP_ROFF, *PSGX_KICKTA_DUMP_ROFF;
+
+typedef struct _SGX_KICKTA_DUMP_BUFFER_
+{
+	IMG_UINT32			ui32SpaceUsed;
+	IMG_UINT32			ui32Start;
+	IMG_UINT32			ui32End;
+	IMG_UINT32			ui32BufferSize;
+	IMG_UINT32			ui32BackEndLength;
+	IMG_UINT32			uiAllocIndex;
+	IMG_HANDLE			hKernelMemInfo;
+	IMG_PVOID			pvLinAddr;
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+	IMG_HANDLE			hCtrlKernelMemInfo;
+	IMG_DEV_VIRTADDR	sCtrlDevVAddr;
+#endif
+	IMG_PCHAR			pszName;
+} SGX_KICKTA_DUMP_BUFFER, *PSGX_KICKTA_DUMP_BUFFER;
+
+#ifdef PDUMP
+typedef struct _SGX_KICKTA_PDUMP_
+{
+
+	PSGX_KICKTA_DUMPBITMAP		psPDumpBitmapArray;
+	IMG_UINT32						ui32PDumpBitmapSize;
+
+
+	PSGX_KICKTA_DUMP_BUFFER	psBufferArray;
+	IMG_UINT32						ui32BufferArraySize;
+
+
+	PSGX_KICKTA_DUMP_ROFF		psROffArray;
+	IMG_UINT32						ui32ROffArraySize;
+} SGX_KICKTA_PDUMP, *PSGX_KICKTA_PDUMP;
+#endif
+
+#if defined(TRANSFER_QUEUE)
+#if defined(SGX_FEATURE_2D_HARDWARE)
+#define SGX_MAX_2D_BLIT_CMD_SIZE 		26
+#define SGX_MAX_2D_SRC_SYNC_OPS			3
+#endif
+#define SGX_MAX_TRANSFER_STATUS_VALS	2
+#define SGX_MAX_TRANSFER_SYNC_OPS	5
+#endif
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/include4/sgxscript.h b/drivers/gpu/drm/emgd/pvr/include4/sgxscript.h
new file mode 100644
index 0000000..c6d080b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/include4/sgxscript.h
@@ -0,0 +1,77 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __SGXSCRIPT_H__
+#define __SGXSCRIPT_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#define	SGX_MAX_INIT_COMMANDS	64
+#define	SGX_MAX_DEINIT_COMMANDS	16
+
+typedef	enum _SGX_INIT_OPERATION
+{
+	SGX_INIT_OP_ILLEGAL = 0,
+	SGX_INIT_OP_WRITE_HW_REG,
+#if defined(PDUMP)
+	SGX_INIT_OP_PDUMP_HW_REG,
+#endif
+	SGX_INIT_OP_HALT
+} SGX_INIT_OPERATION;
+
+typedef union _SGX_INIT_COMMAND
+{
+	SGX_INIT_OPERATION eOp;
+	struct {
+		SGX_INIT_OPERATION eOp;
+		IMG_UINT32 ui32Offset;
+		IMG_UINT32 ui32Value;
+	} sWriteHWReg;
+#if defined(PDUMP)
+	struct {
+		SGX_INIT_OPERATION eOp;
+		IMG_UINT32 ui32Offset;
+		IMG_UINT32 ui32Value;
+	} sPDumpHWReg;
+#endif
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+	struct {
+		SGX_INIT_OPERATION eOp;
+	} sWorkaroundBRN22997;
+#endif
+} SGX_INIT_COMMAND;
+
+typedef struct _SGX_INIT_SCRIPTS_
+{
+	SGX_INIT_COMMAND asInitCommandsPart1[SGX_MAX_INIT_COMMANDS];
+	SGX_INIT_COMMAND asInitCommandsPart2[SGX_MAX_INIT_COMMANDS];
+	SGX_INIT_COMMAND asDeinitCommands[SGX_MAX_DEINIT_COMMANDS];
+} SGX_INIT_SCRIPTS;
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.c b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.c
new file mode 100644
index 0000000..b6fc6d6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.c
@@ -0,0 +1,384 @@
+/***************************************************************************
+ *
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ ******************************************************************************/
+
+#define MODULE_NAME hal.buf_class
+
+#if defined(__linux__)
+#include <linux/string.h>
+#else
+#include <string.h>
+#endif
+#include <linux/version.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <asm/uaccess.h>
+#include <asm/io.h>
+#if 0
+#if defined(LMA)
+#include <linux/pci.h>
+#else
+#include <linux/dma-mapping.h>
+#endif
+#endif
+#include <drm/drmP.h>
+#include <drm/drm.h>
+#include "emgd_drv.h"
+#include "io.h"
+
+#include "pvrmodule.h"
+#include "emgd_bc.h"
+/* 
+** Device Name Space is for device name, steam tag and device state.
+** Format
+** [Name]...[Video ID][Device Status]
+** From Device Name Space tail, 
+** Device Status - one Byte
+** Video ID - four Byte
+** Device Name - variation according to device name set.
+*/
+#define TSBUFFERCLASS_DEVICE_NAME "BC Texture Stream Device"
+
+unsigned int bc_video_id[BUFCLASS_DEVICE_MAX_ID];
+unsigned int bc_video_id_usage[BUFCLASS_DEVICE_MAX_ID];
+
+void *gp_bc_Anchor[BUFCLASS_DEVICE_MAX_ID];
+
+static PFN_BC_GET_PVRJTABLE pfnGetPVRJTable = IMG_NULL;
+
+BC_DEVINFO *GetAnchorPtr (int id){
+    BC_DEVINFO *AnchorPtr = NULL;
+    if (id < BUFCLASS_DEVICE_MAX_ID)
+        AnchorPtr = gp_bc_Anchor[id];
+    return AnchorPtr;
+}
+
+static void SetAnchorPtr(BC_DEVINFO *psDevInfo, int id) {
+    if (id < BUFCLASS_DEVICE_MAX_ID) {
+        gp_bc_Anchor[id] = (void *) psDevInfo;
+    }    
+}
+
+static PVRSRV_ERROR OpenBCDevice (IMG_UINT32 uDeviceID, IMG_HANDLE * phDevice){
+    BC_DEVINFO *psDevInfo = NULL;
+	int i = 0;
+	
+	EMGD_TRACE_ENTER;
+	if (NULL == phDevice) {
+		EMGD_ERROR("Invliad Input parameter");
+		return PVRSRV_ERROR_INVALID_DEVICE;
+	}
+	*phDevice = NULL;
+	
+	EMGD_DEBUG("Try to Open Device ID - %lu", uDeviceID);
+
+	for (i = 0; i < BUFCLASS_DEVICE_MAX_ID; i++) {
+		psDevInfo = GetAnchorPtr(i);
+		if (NULL != psDevInfo && uDeviceID == psDevInfo->Device_ID) {
+			*phDevice = (IMG_HANDLE)psDevInfo;
+			EMGD_DEBUG("psDevInfo - 0x%lx", (unsigned long)psDevInfo);
+			break;
+		}
+	}
+	
+    if (NULL == *phDevice) {
+		EMGD_ERROR("Invliad Device ID - %lu", uDeviceID);
+		return PVRSRV_ERROR_INVALID_DEVICE;
+    }
+
+	EMGD_TRACE_EXIT;
+    return (PVRSRV_OK);
+}
+
+static PVRSRV_ERROR CloseBCDevice(IMG_UINT32 uDeviceID, IMG_HANDLE hDevice){
+    UNREFERENCED_PARAMETER (uDeviceID);
+    UNREFERENCED_PARAMETER (hDevice);
+
+    return (PVRSRV_OK);
+}
+
+static PVRSRV_ERROR GetBCBuffer(IMG_HANDLE hDevice,
+        IMG_UINT32 ui32BufferNumber,
+        PVRSRV_SYNC_DATA * psSyncData, IMG_HANDLE * phBuffer){
+
+    BC_DEVINFO *psDevInfo = NULL;
+	PVRSRV_ERROR ret = PVRSRV_ERROR_INVALID_PARAMS;
+
+	EMGD_TRACE_ENTER;
+
+    if (NULL == hDevice || NULL == phBuffer){
+		return (PVRSRV_ERROR_INVALID_PARAMS);
+    }
+
+    psDevInfo = (BC_DEVINFO *) hDevice;
+
+	EMGD_DEBUG("Device - 0x%lx, Device ID - %lu, Buffer ID - %lu\n", 
+		(unsigned long)psDevInfo, 
+		psDevInfo->Device_ID,
+		ui32BufferNumber);
+	
+    if (ui32BufferNumber < psDevInfo->sBufferInfo.ui32BufferCount) {
+        psDevInfo->psSystemBuffer[ui32BufferNumber].psSyncData = psSyncData;
+        *phBuffer = (IMG_HANDLE) &psDevInfo->psSystemBuffer[ui32BufferNumber];
+		EMGD_DEBUG("Assign System Buffer address - 0x%lx to phBuffer", (unsigned long)(*phBuffer));
+		ret = PVRSRV_OK;
+    } else {
+    	EMGD_ERROR("PVRSRV_ERROR_INVALID_PARAMS\n");
+        ret= PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+	EMGD_TRACE_EXIT;
+
+    return ret;
+}
+
+static PVRSRV_ERROR GetBCInfo(IMG_HANDLE hDevice, BUFFER_INFO *psBCInfo){
+    BC_DEVINFO *psDevInfo = NULL;
+
+	EMGD_TRACE_ENTER;
+
+    if (!hDevice || !psBCInfo){
+        return (PVRSRV_ERROR_INVALID_PARAMS);
+    }
+
+    psDevInfo = (BC_DEVINFO *)hDevice;
+
+	EMGD_DEBUG("psDevInfo - 0x%lx, Device ID - %lu", (unsigned long)psDevInfo, psDevInfo->Device_ID);
+	
+    memcpy((void *)psBCInfo, (void *)&psDevInfo->sBufferInfo, sizeof(BUFFER_INFO));
+
+	EMGD_TRACE_EXIT;
+
+    return (PVRSRV_OK);
+}
+
+static PVRSRV_ERROR
+GetBCBufferAddr (IMG_HANDLE hDevice,
+        IMG_HANDLE hBuffer,
+        IMG_SYS_PHYADDR ** ppsSysAddr,
+        IMG_UINT32 * pui32ByteSize,
+        IMG_VOID ** ppvCpuVAddr,
+        IMG_HANDLE * phOSMapInfo,
+        IMG_BOOL * pbIsContiguous, IMG_BOOL *pbMapped){
+    BC_BUFFER *psBuffer;
+
+	EMGD_TRACE_ENTER;
+
+    if (NULL == hDevice || NULL == hBuffer){
+        return (PVRSRV_ERROR_INVALID_PARAMS);
+    }
+	if (IMG_NULL != pbMapped) {
+		*pbMapped = IMG_TRUE;
+	}	
+    psBuffer = (BC_BUFFER *)hBuffer;
+    
+	EMGD_DEBUG("Buffer 0x%lx", (IMG_UINT32)psBuffer);
+	if (NULL != ppsSysAddr) {
+		*ppsSysAddr = psBuffer->psSysAddr;
+	}
+	if (NULL != pui32ByteSize) {
+		*pui32ByteSize = (IMG_UINT32)psBuffer->ulSize;
+	}
+	if (NULL != ppvCpuVAddr) {
+    	*ppvCpuVAddr = psBuffer->sCPUVAddr;
+	}
+	
+	if (NULL != phOSMapInfo) {
+    	*phOSMapInfo = IMG_NULL;
+	}
+
+    if (NULL != pbIsContiguous) {
+    	*pbIsContiguous = psBuffer->is_conti_addr;
+    }
+	    
+	EMGD_TRACE_EXIT;
+
+    return (PVRSRV_OK);
+}
+
+static PVRSRV_ERROR GetBCBufferIdFromTag(IMG_HANDLE hDevice, IMG_UINT32 tag, IMG_HANDLE idx){
+    BC_DEVINFO *psDevInfo = NULL;
+	IMG_UINT32 i = 0;
+	
+	EMGD_TRACE_ENTER;
+
+    if (NULL == hDevice || NULL == idx){
+        return (PVRSRV_ERROR_INVALID_PARAMS);
+    }
+
+    psDevInfo = (BC_DEVINFO *)hDevice;
+
+	EMGD_DEBUG("hDevice - 0x%lx, Device ID - %lu", (unsigned long)psDevInfo, psDevInfo->Device_ID);
+
+	for (i = 0; i < psDevInfo->sBufferInfo.ui32BufferCount; i++) {
+		if (psDevInfo->psSystemBuffer[i].tag == tag) {
+			EMGD_DEBUG("Found the Buffer: Id - %lu, Tag - %lu", i, tag);
+			*((IMG_UINT32 *) idx) = i;
+			break;	
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+
+    return (PVRSRV_OK);
+}
+
+emgd_error_t bc_ts_init(IMG_UINT32 id) {
+    BC_DEVINFO *psDevInfo = NULL;
+
+	EMGD_TRACE_ENTER;
+
+    psDevInfo = GetAnchorPtr(id);
+
+    if (psDevInfo == NULL){
+    	EMGD_DEBUG("To Allocate Device with Index - %lu", id);
+        psDevInfo = (BC_DEVINFO *)BCAllocKernelMem(sizeof(BC_DEVINFO));
+		EMGD_DEBUG("psDevInfo - %lx", (unsigned long)psDevInfo);
+        if (NULL == psDevInfo){
+            return (EMGD_ERROR_OUT_OF_MEMORY);
+        }
+		memset((void *)psDevInfo, 0, sizeof(BC_DEVINFO));
+
+		psDevInfo->Device_ID = id; /* device index amond device list */
+		psDevInfo->sBufferInfo.ui32BufferDeviceID = id;
+		
+        SetAnchorPtr((void *)psDevInfo, id);
+
+        if (BCOpenPVRServices(&psDevInfo->hPVRServices) != EMGD_OK){
+            return (EMGD_ERROR_INIT_FAILURE);
+        }
+		/* Initialize PVR JTable Function */
+        if (BCGetLibFuncAddr(psDevInfo->hPVRServices, "PVRGetBufferClassJTable",
+                 &pfnGetPVRJTable) != EMGD_OK) {
+            return (EMGD_ERROR_INIT_FAILURE);
+        }
+
+        if (!(*pfnGetPVRJTable)(&psDevInfo->sPVRJTable)){
+            return (EMGD_ERROR_INIT_FAILURE);
+        }
+		if (TSBUFFERCLASS_DEV_NAME_LEN < 0) {
+			EMGD_ERROR("BufferClass Device Name Space is too small!");
+			return EMGD_ERROR_INIT_FAILURE;
+		}
+        strncpy(psDevInfo->sBufferInfo.szDeviceName, TSBUFFERCLASS_DEVICE_NAME, TSBUFFERCLASS_DEV_NAME_LEN);
+		/* Initialize BC JTable */
+        psDevInfo->sBCJTable.ui32TableSize = sizeof (PVRSRV_BC_SRV2BUFFER_KMJTABLE);
+
+        psDevInfo->sBCJTable.pfnOpenBCDevice = (PFN_OPEN_BC_DEVICE)OpenBCDevice;
+        psDevInfo->sBCJTable.pfnCloseBCDevice = (PFN_CLOSE_BC_DEVICE)CloseBCDevice;
+        psDevInfo->sBCJTable.pfnGetBCBuffer = (PFN_GET_BC_BUFFER)GetBCBuffer;
+        psDevInfo->sBCJTable.pfnGetBCInfo = (PFN_GET_BC_INFO)GetBCInfo;
+        psDevInfo->sBCJTable.pfnGetBufferAddr = (PFN_GET_BUFFER_ADDR)GetBCBufferAddr;
+		psDevInfo->sBCJTable.pfnGetBufferIdFromTag = (PFN_GET_BUFFER_ID_FROM_TAG)GetBCBufferIdFromTag;
+
+        if (PVRSRV_OK != psDevInfo->sPVRJTable.pfnPVRSRVRegisterBCDevice(&psDevInfo->sBCJTable,
+                    &(psDevInfo->Device_ID))){
+            return (EMGD_ERROR_DEVICE_REGISTER_FAILED);
+        } else {
+			EMGD_DEBUG("Got Device ID - %lu", psDevInfo->Device_ID);
+		}
+    } else {
+		EMGD_ERROR("Duplicate register Device - %lu", psDevInfo->Device_ID);
+    }
+
+	EMGD_TRACE_EXIT;
+
+    return (EMGD_OK);
+}
+emgd_error_t bc_ts_uninit(IMG_UINT32 id) {
+    BC_DEVINFO *psDevInfo = NULL;
+	int i = 0;
+
+	EMGD_TRACE_ENTER;
+
+    psDevInfo = (BC_DEVINFO *)GetAnchorPtr(id);
+
+    if (psDevInfo == NULL){
+        return (EMGD_ERROR_GENERIC);
+    }
+  	EMGD_DEBUG("To Unregister the Device: ID - %lu, RefCount - %lu, idx - %lu", 
+  		psDevInfo->Device_ID,
+  		psDevInfo->ulRefCount,
+  		psDevInfo->sBufferInfo.ui32BufferDeviceID);
+
+	if (id != psDevInfo->sBufferInfo.ui32BufferDeviceID) {
+		EMGD_ERROR("Index mis-matcheh input idx vs. device idx: %lu vs. %lu", id, psDevInfo->sBufferInfo.ui32BufferDeviceID);
+	}
+	
+	if (0 != psDevInfo->ulRefCount) {
+    	psDevInfo->ulRefCount--;
+	}
+
+    if (1 /*psDevInfo->ulRefCount == 0*/){
+        PVRSRV_BC_BUFFER2SRV_KMJTABLE *psJTable = &psDevInfo->sPVRJTable;
+
+        if (psJTable->pfnPVRSRVRemoveBCDevice(psDevInfo->Device_ID) !=
+                PVRSRV_OK) {
+            return (EMGD_ERROR_GENERIC);
+        }
+
+        if (BCClosePVRServices(psDevInfo->hPVRServices) != EMGD_OK){
+            psDevInfo->hPVRServices = NULL;
+            return (EMGD_ERROR_GENERIC);
+        }
+		if (NULL != psDevInfo->psSystemBuffer) {
+        	for (i = 0; i < BUFCLASS_BUFFER_MAX; i++) {
+				bc_ts_free_bcbuffer(&(psDevInfo->psSystemBuffer[i]));
+        	}
+			BCFreeKernelMem(psDevInfo->psSystemBuffer);
+		}
+        BCFreeKernelMem(psDevInfo);
+
+        SetAnchorPtr(NULL, id);
+        bc_video_id_usage[id] = 0;
+    }
+	EMGD_TRACE_EXIT;
+
+    return (EMGD_OK);
+}
+void bc_ts_free_bcbuffer(BC_BUFFER *bc_buf) {
+	if (NULL == bc_buf) {
+		return;
+	}
+
+	BCFreeKernelMem(bc_buf->psSysAddr);
+	bc_buf->psSysAddr = NULL;
+#if 0 /* Cause Kernel Error */	
+	BCFreeKernelMem(bc_buf->psSyncData);
+	bc_buf->psSyncData = NULL;
+#endif	
+	return;
+}
+
+void *BCAllocKernelMem (unsigned long ulSize){
+    return vmalloc(ulSize); /* kmalloc*/
+}
+
+void BCFreeKernelMem(void *pvMem){
+	if (NULL != pvMem) { /* kfree -> vfree*/
+    	vfree(pvMem);
+	}
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.h b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.h
new file mode 100644
index 0000000..7728631
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc.h
@@ -0,0 +1,173 @@
+/**********************************************************************
+ *
+ * Copyright 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ ******************************************************************************/
+
+#ifndef __EMGD_BC__H__
+#define __EMGD_BC__H__
+
+#include "img_defs.h"
+#include "servicesext.h"
+#include "kernelbuffer.h"
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+    enum BC_memory
+    {
+        BC_MEMORY_MMAP = 1,
+        BC_MEMORY_USERPTR = 2,
+    };
+
+    /* 
+     * the following types are tested for fourcc in struct bc_buf_params_t
+     *   NV12
+     *   UYVY
+     *   RGB565 - not tested yet
+     *   YUYV
+     */
+    typedef struct bc_buf_params
+    {
+        int count;            /*number of buffers, [in/out] */
+        int width;            /*buffer width in pixel, multiple of 8 or 32 */
+        int height;            /*buffer height in pixel */
+        int stride;
+        unsigned int fourcc;    /*buffer pixel format */
+        enum BC_memory type;
+    } bc_buf_params_t;
+
+    extern IMG_IMPORT IMG_BOOL
+        PVRGetBufferClassJTable (PVRSRV_BC_BUFFER2SRV_KMJTABLE * psJTable);
+
+#define BUFCLASS_DEVICE_MAX_ID	6
+#define BUFCLASS_BUFFER_MAX		64
+/* 
+** Device Name Space is for device name, steam tag and device state.
+** Format
+** [Name]...[Video ID][Device Status]
+** From Device Name Space tail, 
+** Device Status - one Byte
+** Video ID - four Byte
+** Device Name - variation according to device name set.
+*/
+#define TSBUFFERCLASS_DEV_NAME_LEN (MAX_BUFFER_DEVICE_NAME_SIZE - sizeof(IMG_UINT32) - sizeof(IMG_CHAR) - 1)
+#define TSBUFFERCLASS_VIDEOID_OFFSET (MAX_BUFFER_DEVICE_NAME_SIZE - sizeof(IMG_UINT32) - sizeof(IMG_CHAR))
+#define TSBUFFERCLASS_DEVSTATUS_OFFSET (MAX_BUFFER_DEVICE_NAME_SIZE - sizeof(IMG_CHAR))
+
+    typedef void *BCE_HANDLE;
+
+    typedef enum tag_bce_bool
+    {
+        BCE_FALSE = 0,
+        BCE_TRUE = 1,
+    } BCE_BOOL, *BCE_PBOOL;
+
+    typedef struct BC_BUFFER_TAG
+    {
+        IMG_UINT32 ulSize;
+        IMG_HANDLE hMemHandle;
+
+        IMG_SYS_PHYADDR *psSysAddr;
+		IMG_UINT32 SysAddr;
+
+        IMG_CPU_VIRTADDR sCPUVAddr;
+        PVRSRV_SYNC_DATA *psSyncData;
+
+        struct BC_BUFFER_TAG *psNext;
+        IMG_UINT32 sBufferHandle;
+        IMG_BOOL is_conti_addr;
+		IMG_UINT32 tag; /* Buffer Tag. -- Surface ID*/
+    } BC_BUFFER;
+
+    typedef struct BC_DEVINFO_TAG
+    {
+        BC_BUFFER *psSystemBuffer;
+        PVRSRV_BC_BUFFER2SRV_KMJTABLE sPVRJTable;
+        PVRSRV_BC_SRV2BUFFER_KMJTABLE sBCJTable;
+        BCE_HANDLE hPVRServices;
+        IMG_UINT32 ulRefCount;
+        BUFFER_INFO sBufferInfo;
+        enum BC_memory buf_type;
+        IMG_UINT32 Device_ID;
+    } BC_DEVINFO;
+
+    typedef enum emgd_bc_error_
+    {
+        EMGD_OK = 0,
+        EMGD_ERROR_GENERIC = 1,
+        EMGD_ERROR_OUT_OF_MEMORY = 2,
+        EMGD_ERROR_TOO_FEW_BUFFERS = 3,
+        EMGD_ERROR_INVALID_PARAMS = 4,
+        EMGD_ERROR_INIT_FAILURE = 5,
+        EMGD_ERROR_CANT_REGISTER_CALLBACK = 6,
+        EMGD_ERROR_INVALID_DEVICE = 7,
+        EMGD_ERROR_DEVICE_REGISTER_FAILED = 8,
+        EMGD_ERROR_NO_PRIMARY = 9
+    } emgd_error_t;
+
+#ifndef UNREFERENCED_PARAMETER
+#define    UNREFERENCED_PARAMETER(param) (param) = (param)
+#endif
+#if 0
+#ifndef NULL
+#define NULL 0
+#endif
+#endif
+emgd_error_t bc_ts_init(IMG_UINT32 id);
+emgd_error_t bc_ts_uninit(IMG_UINT32 id);
+
+emgd_error_t BCOpenPVRServices(BCE_HANDLE * phPVRServices);
+emgd_error_t BCClosePVRServices(BCE_HANDLE hPVRServices);
+
+void *BCAllocKernelMem(unsigned long ulSize);
+void BCFreeKernelMem(void *pvMem);
+#if 0
+    BCE_ERROR BCAllocDiscontigMemory(unsigned long ulSize,
+            BCE_HANDLE unref__ * phMemHandle,
+            IMG_CPU_VIRTADDR * pLinAddr,
+            IMG_SYS_PHYADDR ** ppPhysAddr);
+
+    void BCFreeDiscontigMemory(unsigned long ulSize,
+            BCE_HANDLE unref__ hMemHandle,
+            IMG_CPU_VIRTADDR LinAddr,
+            IMG_SYS_PHYADDR * pPhysAddr);
+
+    IMG_SYS_PHYADDR CpuPAddrToSysPAddrBC(IMG_CPU_PHYADDR cpu_paddr);
+    IMG_CPU_PHYADDR SysPAddrToCpuPAddrBC(IMG_SYS_PHYADDR sys_paddr);
+
+    void *MapPhysAddr(IMG_SYS_PHYADDR sSysAddr, unsigned long ulSize);
+    void UnMapPhysAddr(void *pvAddr, unsigned long ulSize);
+#endif
+emgd_error_t BCGetLibFuncAddr(BCE_HANDLE hExtDrv, char *szFunctionName,
+            PFN_BC_GET_PVRJTABLE * ppfnFuncTable);
+BC_DEVINFO *GetAnchorPtr(int id);
+
+void bc_ts_free_bcbuffer(BC_BUFFER *bc_buf);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc_linux.c b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc_linux.c
new file mode 100644
index 0000000..983b768
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_bufferclass/emgd_bc_linux.c
@@ -0,0 +1,1000 @@
+/****************************************************************************
+ *
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ ******************************************************************************/
+
+#define MODULE_NAME hal.buf_class
+
+#include <linux/version.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <asm/uaccess.h>
+#include <asm/io.h>
+#if 0
+#if defined(LMA)
+#include <linux/pci.h>
+#else
+#include <linux/dma-mapping.h>
+#endif
+#endif
+#include <drm/drmP.h>
+#include <drm/drm.h>
+#include "emgd_drv.h"
+#include "io.h"
+#include "pvrmodule.h"
+#include "emgd_drm.h"
+
+#include "emgd_bc.h"
+#if 0
+#define DEVNAME    "bc_video"
+#define DRVNAME    DEVNAME
+#endif
+#define BC_FOURCC(a,b,c,d) \
+    ((unsigned long) ((a) | (b)<<8 | (c)<<16 | (d)<<24))
+
+#define BC_PIX_FMT_NV12     BC_FOURCC('N', 'V', '1', '2')    /*YUV 4:2:0 */
+#define BC_PIX_FMT_UYVY     BC_FOURCC('U', 'Y', 'V', 'Y')    /*YUV 4:2:2 */
+#define BC_PIX_FMT_YUYV     BC_FOURCC('Y', 'U', 'Y', 'V')    /*YUV 4:2:2 */
+#define BC_PIX_FMT_RGB565   BC_FOURCC('R', 'G', 'B', 'P')    /*RGB 5:6:5 */
+#if 0
+#if defined(BCE_USE_SET_MEMORY)
+#undef BCE_USE_SET_MEMORY
+#endif
+
+#if defined(__i386__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)) && defined(SUPPORT_LINUX_X86_PAT) && defined(SUPPORT_LINUX_X86_WRITECOMBINE)
+#include <asm/cacheflush.h>
+#define    BCE_USE_SET_MEMORY
+#endif
+
+static int width_align;
+#endif
+extern unsigned int bc_video_id[BUFCLASS_DEVICE_MAX_ID];
+extern unsigned int bc_video_id_usage[BUFCLASS_DEVICE_MAX_ID];
+extern void* gp_bc_Anchor[BUFCLASS_DEVICE_MAX_ID];
+
+/* Stream Tag. To be stored on Buffer class name array. */
+#define MAX_STREAM_TAG 0xFFFFFFF0
+unsigned long bc_stream_tag = 0;
+
+typedef enum _BC_DEVICE_STATE {
+	BC_DEV_READY = 0xF0,
+	BC_DEV_NOT_READY,
+} BC_Dev_Status;
+
+#if 0
+MODULE_SUPPORTED_DEVICE (DEVNAME);
+
+int FillBuffer(unsigned int uiBufferIndex);
+
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+static struct class *psPvrClass;
+#endif
+
+static int AssignedMajorNumber;
+
+#define unref__ __attribute__ ((unused))
+
+#if defined(LMA)
+#define PVR_BUFFERCLASS_MEMOFFSET (220 * 1024 * 1024)
+#define PVR_BUFFERCLASS_MEMSIZE      (4 * 1024 * 1024)
+
+unsigned long g_ulMemBase = 0;
+unsigned long g_ulMemCurrent = 0;
+
+#define VENDOR_ID_PVR               0x1010
+#define DEVICE_ID_PVR               0x1CF1
+
+#define PVR_MEM_PCI_BASENUM         2
+#endif
+
+#define file_to_id(file)  (iminor(file->f_path.dentry->d_inode))
+#endif
+/* FCB #17711*/
+/* flag indicates whether BC_Video Module Initialized or not 
+* 0 - Uninitialized, 1 - Initialized.
+*/
+static int flg_bc_ts_init = 0;
+
+/*
+** Function: Set device state
+** state: 0 - enable; 1 - disable
+*/
+void emgd_bc_ts_set_state(BC_DEVINFO *psDevInfo, const IMG_CHAR state);
+
+int emgd_bc_ts_init(void){
+    int i, j;
+#if 0    
+    /*LDM_PCI is defined, while LDM_PLATFORM and LMA are not defined.*/
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+    struct device *psDev;
+#endif
+
+#if defined(LMA)
+    struct pci_dev *psPCIDev;
+    int error;
+#endif
+#endif
+	EMGD_TRACE_ENTER;
+
+	if (0 != flg_bc_ts_init) {
+		EMGD_ERROR("BC already been initialized!");
+		return 0;
+	}
+#if 0	
+    /* video width is 4 byte aligned */
+    width_align = 4;
+#endif
+	memset(bc_video_id, 0, sizeof(bc_video_id));
+	memset(bc_video_id_usage, 0, sizeof(bc_video_id_usage));
+	memset(gp_bc_Anchor, 0, sizeof(gp_bc_Anchor));
+#if 0	
+#if defined(LMA)
+    psPCIDev = pci_get_device (VENDOR_ID_PVR, DEVICE_ID_PVR, NULL);
+    if (psPCIDev == NULL){
+        EMGD_ERROR("pci_get_device failed");
+        goto ExitError;
+    }
+
+    if ((error = pci_enable_device (psPCIDev)) != 0){
+        EMGD_ERROR("pci_enable_device failed (%d)", error);
+        goto ExitError;
+    }
+#endif
+#endif
+#if 0
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+    psPvrClass = class_create(THIS_MODULE, "bc_video");
+    if (IS_ERR (psPvrClass)) {
+        EMGD_ERROR("unable to create class (%ld)",
+                PTR_ERR(psPvrClass));
+        goto ExitUnregister;
+    }
+
+    psDev = device_create (psPvrClass, NULL, MKDEV (AssignedMajorNumber, 0),
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26))
+            NULL,
+#endif
+            DEVNAME);
+    if (IS_ERR (psDev)){
+        EMGD_ERROR("unable to create device (%ld)",
+                PTR_ERR (psDev));
+        goto ExitDestroyClass;
+    }
+#endif
+
+#if defined(LMA)
+    g_ulMemBase =
+        pci_resource_start (psPCIDev,
+                PVR_MEM_PCI_BASENUM) + PVR_BUFFERCLASS_MEMOFFSET;
+#endif
+#endif
+    for (i = 0; i < BUFCLASS_DEVICE_MAX_ID; i++) {
+        bc_video_id[i] = i;
+        bc_video_id_usage[i] = 0;
+        if (bc_ts_init(bc_video_id[i]) != EMGD_OK) {
+            EMGD_ERROR("can't init video bc device %d.", i);
+            for (j = i; j >= 0; j--) {
+                bc_ts_uninit(bc_video_id[j]);
+            }
+            goto ExitUnregister;
+        }
+    }
+#if 0
+#if defined(LMA)
+    pci_disable_device (psPCIDev);
+#endif
+#endif
+	flg_bc_ts_init = 1;
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+#if 0
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+ExitDestroyClass:
+    class_destroy (psPvrClass);
+#endif
+#endif
+ExitUnregister:
+#if 0
+    unregister_chrdev (AssignedMajorNumber, DEVNAME);
+    //ExitDisable:
+#if defined(LMA)
+    pci_disable_device (psPCIDev);
+ExitError:
+#endif
+#endif
+    return -EBUSY;
+}
+
+int emgd_bc_ts_uninit(void){
+    int i = 0;
+#if 0    
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+    device_destroy (psPvrClass, MKDEV (AssignedMajorNumber, 0));
+    class_destroy (psPvrClass);
+#endif
+#endif
+	EMGD_TRACE_ENTER;
+		
+	if (1 != flg_bc_ts_init) {
+		EMGD_ERROR("BC not been initialized yet");
+		return 0;
+	}
+	
+    for (i = 0; i < BUFCLASS_DEVICE_MAX_ID; i++){
+    	if (1 == bc_video_id_usage[i]) {
+        	if (bc_ts_uninit(bc_video_id[i]) != EMGD_OK) {
+            	EMGD_ERROR("can't deinit video device %d.", i);
+            	return -1;
+        	}
+    	}
+    }
+
+	flg_bc_ts_init = 0;
+	
+	EMGD_TRACE_EXIT;
+
+    return 0;
+}
+#if 0
+#define	RANGE_TO_PAGES(range) (((range) + (PAGE_SIZE - 1)) >> PAGE_SHIFT)
+#define	VMALLOC_TO_PAGE_PHYS(vAddr) page_to_phys(vmalloc_to_page(vAddr))
+
+BCE_ERROR BCAllocDiscontigMemory (unsigned long ulSize,
+        BCE_HANDLE unref__ *phMemHandle,
+        IMG_CPU_VIRTADDR *pLinAddr,
+        IMG_SYS_PHYADDR **ppPhysAddr){
+    unsigned long ulPages = RANGE_TO_PAGES (ulSize);
+    IMG_SYS_PHYADDR *pPhysAddr = IMG_NULL;
+    unsigned long ulPage = 0;
+    IMG_CPU_VIRTADDR LinAddr = IMG_NULL;
+
+		EMGD_TRACE_ENTER;
+
+    LinAddr =
+        __vmalloc (ulSize, GFP_KERNEL | __GFP_HIGHMEM,
+                pgprot_noncached (PAGE_KERNEL));
+    if (!LinAddr){
+        return BCE_ERROR_OUT_OF_MEMORY;
+    }
+
+    pPhysAddr = vmalloc (ulPages * sizeof (IMG_SYS_PHYADDR));
+    if (!pPhysAddr) {
+        vfree(LinAddr);
+        return BCE_ERROR_OUT_OF_MEMORY;
+    }
+
+    *pLinAddr = LinAddr;
+
+   for (ulPage = 0; ulPage < ulPages; ulPage++){
+        pPhysAddr[ulPage].uiAddr = VMALLOC_TO_PAGE_PHYS (LinAddr);
+
+        LinAddr += PAGE_SIZE;
+   }
+
+  *ppPhysAddr = pPhysAddr;
+	EMGD_TRACE_EXIT;
+
+  return BCE_OK;
+}
+
+void BCFreeDiscontigMemory (unsigned long ulSize,
+        BCE_HANDLE unref__ hMemHandle,
+        IMG_CPU_VIRTADDR LinAddr, IMG_SYS_PHYADDR * pPhysAddr){
+
+	BCFreeKernelMem(pPhysAddr);
+	
+    if (NULL != LinAddr) {
+    	vfree(LinAddr);
+    }
+}
+
+BCE_ERROR BCAllocContigMemory (unsigned long ulSize,
+        BCE_HANDLE unref__ *phMemHandle,
+        IMG_CPU_VIRTADDR *pLinAddr, IMG_CPU_PHYADDR *pPhysAddr){
+#if defined(LMA)
+    void *pvLinAddr = NULL;
+
+    if (g_ulMemCurrent + ulSize >= PVR_BUFFERCLASS_MEMSIZE){
+        return (BCE_ERROR_OUT_OF_MEMORY);
+    }
+
+    pvLinAddr = ioremap(g_ulMemBase + g_ulMemCurrent, ulSize);
+
+    if (pvLinAddr)
+    {
+        pPhysAddr->uiAddr = g_ulMemBase + g_ulMemCurrent;
+        *pLinAddr = pvLinAddr;
+
+        g_ulMemCurrent += ulSize;
+        return (BCE_OK);
+    }
+    return (BCE_ERROR_OUT_OF_MEMORY);
+#else
+#if defined(BCE_USE_SET_MEMORY)
+    void *pvLinAddr = NULL;
+    unsigned long ulAlignedSize = PAGE_ALIGN(ulSize);
+    int iPages = (int) (ulAlignedSize >> PAGE_SHIFT);
+    int iError;
+
+    pvLinAddr = kmalloc(ulAlignedSize, GFP_KERNEL);
+    BUG_ON (((unsigned long) pvLinAddr) & ~PAGE_MASK);
+
+    iError = set_memory_wc((unsigned long) pvLinAddr, iPages);
+    if (iError != 0){
+        EMGD_ERROR("set_memory_wc failed (%d)", iError);
+        return (BCE_ERROR_OUT_OF_MEMORY);
+    }
+
+    pPhysAddr->uiAddr = virt_to_phys (pvLinAddr);
+    *pLinAddr = pvLinAddr;
+
+    return (BCE_OK);
+#else
+    dma_addr_t dma;
+    void *pvLinAddr = NULL;
+
+    pvLinAddr = dma_alloc_coherent (NULL, ulSize, &dma, GFP_KERNEL);
+    if (NULL == pvLinAddr){
+        return (BCE_ERROR_OUT_OF_MEMORY);
+    }
+
+    pPhysAddr->uiAddr = dma;
+    *pLinAddr = pvLinAddr;
+
+    return (BCE_OK);
+#endif
+#endif
+}
+
+void BCFreeContigMemory (unsigned long ulSize,
+        BCE_HANDLE unref__ hMemHandle,
+        IMG_CPU_VIRTADDR LinAddr, IMG_CPU_PHYADDR PhysAddr){
+#if defined(LMA)
+    g_ulMemCurrent -= ulSize;
+    iounmap(LinAddr);
+#else
+#if defined(BCE_USE_SET_MEMORY)
+    unsigned long ulAlignedSize = PAGE_ALIGN (ulSize);
+    int iError;
+    int iPages = (int) (ulAlignedSize >> PAGE_SHIFT);
+
+    iError = set_memory_wb ((unsigned long) LinAddr, iPages);
+    if (iError != 0) {
+        EMGD_ERROR("set_memory_wb failed (%d)", iError);
+    }
+    BCFreeKernelMem(LinAddr);
+#else
+    dma_free_coherent (NULL, ulSize, LinAddr, (dma_addr_t)PhysAddr.uiAddr);
+#endif
+#endif
+}
+
+IMG_SYS_PHYADDR CpuPAddrToSysPAddrBC (IMG_CPU_PHYADDR cpu_paddr){
+    IMG_SYS_PHYADDR sys_paddr;
+    sys_paddr.uiAddr = cpu_paddr.uiAddr;
+    return sys_paddr;
+}
+
+IMG_CPU_PHYADDR SysPAddrToCpuPAddrBC (IMG_SYS_PHYADDR sys_paddr){
+    IMG_CPU_PHYADDR cpu_paddr;
+    cpu_paddr.uiAddr = sys_paddr.uiAddr;
+    return cpu_paddr;
+}
+#endif
+emgd_error_t BCOpenPVRServices (BCE_HANDLE * phPVRServices){
+    *phPVRServices = 0;
+    return (EMGD_OK);
+}
+
+
+emgd_error_t BCClosePVRServices (BCE_HANDLE unref__ hPVRServices){
+    return (EMGD_OK);
+}
+
+emgd_error_t BCGetLibFuncAddr (BCE_HANDLE unref__ hExtDrv, char *szFunctionName,
+        PFN_BC_GET_PVRJTABLE * ppfnFuncTable) {
+    if (strcmp ("PVRGetBufferClassJTable", szFunctionName) != 0){
+        return (EMGD_ERROR_INVALID_PARAMS);
+    }
+
+    *ppfnFuncTable = PVRGetBufferClassJTable;
+
+    return (EMGD_OK);
+}
+
+int BC_CreateBuffers(BC_DEVINFO *psDevInfo, bc_buf_params_t *p, IMG_BOOL is_conti_addr) {
+    IMG_UINT32 i = 0;
+	IMG_UINT32 stride = 0;
+	IMG_UINT32 size = 0;
+    PVRSRV_PIXEL_FORMAT pixel_fmt = 0;
+	BC_BUFFER *bufnode = NULL;
+	
+	EMGD_TRACE_ENTER;
+
+	if (p->count < 1) {
+  		return -1;
+  	}    
+
+    if (p->width <= 1 || p->height <= 1) {
+        return -1;
+    }
+
+    switch (p->fourcc) {
+        case BC_PIX_FMT_NV12:
+            pixel_fmt = PVRSRV_PIXEL_FORMAT_NV12;
+            break;
+        case BC_PIX_FMT_UYVY:
+            pixel_fmt = PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY;
+            break;
+        case BC_PIX_FMT_RGB565:
+            pixel_fmt = PVRSRV_PIXEL_FORMAT_RGB565;
+            p->stride = p->stride << 1;    /* stride for RGB from user space is uncorrect */
+            break;
+        case BC_PIX_FMT_YUYV:
+            pixel_fmt = PVRSRV_PIXEL_FORMAT_FOURCC_ORG_YUYV;
+            break;
+        default:
+            return -1;
+            break;
+    }
+
+    stride = p->stride;
+
+    if (p->type != BC_MEMORY_MMAP && p->type != BC_MEMORY_USERPTR) {
+        return -1;
+    }      
+
+	if (0 == psDevInfo->sBufferInfo.ui32BufferCount && NULL == psDevInfo->psSystemBuffer) {
+		psDevInfo->psSystemBuffer = (BC_BUFFER *)BCAllocKernelMem(sizeof(BC_BUFFER) * BUFCLASS_BUFFER_MAX);
+		if (NULL != psDevInfo->psSystemBuffer) {
+			memset((void *)psDevInfo->psSystemBuffer, 0, sizeof(BC_BUFFER) * BUFCLASS_BUFFER_MAX);
+		} else {
+			return -1;
+		}
+	} else {
+		if ((psDevInfo->sBufferInfo.ui32BufferCount + p->count) >= BUFCLASS_BUFFER_MAX) {
+			EMGD_ERROR("No avaiable Buffers");
+			return -1;
+		}
+	}
+
+    if (0 != psDevInfo->sBufferInfo.ui32BufferCount) {
+    	if (psDevInfo->sBufferInfo.ui32Width != p->width
+    		|| psDevInfo->sBufferInfo.ui32Height != p->height
+    		|| psDevInfo->sBufferInfo.ui32ByteStride != p->stride
+    		|| psDevInfo->sBufferInfo.pixelformat != pixel_fmt 
+    		|| psDevInfo->buf_type != p->type) {
+    		
+			EMGD_ERROR("Request invalid buffers");
+			return -ENODEV;
+    	}
+    }    
+
+    psDevInfo->buf_type = p->type;
+
+    size = p->height * stride;
+    
+    if (pixel_fmt == PVRSRV_PIXEL_FORMAT_NV12) {
+        size += (stride >> 1) * (p->height >> 1) << 1;
+    }
+    
+	/* Append new nodes*/
+	for (i = 0; i < p->count; i++) {
+		bufnode = &(psDevInfo->psSystemBuffer[psDevInfo->sBufferInfo.ui32BufferCount + i]);
+		EMGD_DEBUG("Buffer idx - %lu", psDevInfo->sBufferInfo.ui32BufferCount + i);
+		bufnode->ulSize = size;
+		EMGD_DEBUG("Buffer size - %lu", size);
+		
+		bufnode->tag = psDevInfo->sBufferInfo.ui32BufferCount + i;
+
+		bufnode->psSyncData = NULL;
+
+		bufnode->is_conti_addr = is_conti_addr;
+		EMGD_DEBUG("Buffer IsCont. - %d", is_conti_addr);
+
+		bufnode->psNext = NULL;
+		if (is_conti_addr){
+        	bufnode->psSysAddr = (IMG_SYS_PHYADDR *)BCAllocKernelMem (sizeof(IMG_SYS_PHYADDR));
+            if (NULL == bufnode->psSysAddr) {
+            	return EMGD_ERROR_OUT_OF_MEMORY;
+            }
+            memset(bufnode->psSysAddr, 0, sizeof(IMG_SYS_PHYADDR));
+        } else {
+            return EMGD_ERROR_INVALID_PARAMS;
+        }
+	} 
+
+	if (0 == psDevInfo->sBufferInfo.ui32BufferCount) {
+    	psDevInfo->sBufferInfo.pixelformat = pixel_fmt;
+    	psDevInfo->sBufferInfo.ui32Width = p->width;
+    	psDevInfo->sBufferInfo.ui32Height = p->height;
+    	psDevInfo->sBufferInfo.ui32ByteStride = stride;
+    	psDevInfo->sBufferInfo.ui32Flags = PVRSRV_BC_FLAGS_YUVCSC_FULL_RANGE |
+        	PVRSRV_BC_FLAGS_YUVCSC_BT601;
+	}
+
+	psDevInfo->sBufferInfo.ui32BufferCount += p->count;
+
+	EMGD_TRACE_EXIT;
+	
+    return 0;
+}
+
+int BC_DestroyBuffers(void *psDevInfo) {
+    BC_DEVINFO *DevInfo = NULL;
+    IMG_UINT32 i = 0;
+    BC_BUFFER *bufnode = NULL;
+
+   	EMGD_TRACE_ENTER;
+
+    if (NULL == psDevInfo) {
+        return -1;
+    }    
+
+	DevInfo = (BC_DEVINFO *)psDevInfo;
+    EMGD_DEBUG("To Free %lu buffers", DevInfo->sBufferInfo.ui32BufferCount);
+   
+	for (i = 0; i < DevInfo->sBufferInfo.ui32BufferCount; i++) {
+		bufnode = &(DevInfo->psSystemBuffer[i]);
+		bc_ts_free_bcbuffer(bufnode);
+	}
+	BCFreeKernelMem(DevInfo->psSystemBuffer);
+	DevInfo->psSystemBuffer = NULL;
+
+	if (0 != DevInfo->ulRefCount) {
+		DevInfo->ulRefCount--;
+	}
+    DevInfo->sBufferInfo.pixelformat = PVRSRV_PIXEL_FORMAT_UNKNOWN;
+    DevInfo->sBufferInfo.ui32Width = 0;
+    DevInfo->sBufferInfo.ui32Height = 0;
+    DevInfo->sBufferInfo.ui32ByteStride = 0;
+    DevInfo->sBufferInfo.ui32Flags = 0;
+    DevInfo->sBufferInfo.ui32BufferCount = 0;
+
+	EMGD_TRACE_EXIT;
+
+    return 0;
+}
+
+/*
+** For Buffer Class of Texture Stream 
+*/
+static __inline int emgd_bc_ts_bridge_init(struct drm_device *drv, void* arg, struct drm_file *file_priv){
+	int err = -EFAULT;
+	int i;
+	emgd_drm_bc_ts_t *psBridge = (emgd_drm_bc_ts_t *) arg;
+	BC_DEVINFO *psDevInfo = NULL;
+
+	EMGD_TRACE_ENTER;
+
+	if (NULL == psBridge) {
+		EMGD_ERROR("Invalid input parameter!\n");
+		return err;
+	}
+	psBridge->rtn = 1;
+	
+	/* search available device */
+	for (i = 0; i < BUFCLASS_DEVICE_MAX_ID; i++) {
+		if (0 == bc_video_id_usage[i]) {
+   			bc_video_id_usage[i] = 1;
+			psDevInfo = (BC_DEVINFO *)GetAnchorPtr(bc_video_id[i]);
+			if (NULL == psDevInfo) {
+				EMGD_ERROR("System Error");
+				return err;
+			}
+			psBridge->dev_id = psDevInfo->sBufferInfo.ui32BufferDeviceID;
+			if (MAX_STREAM_TAG == bc_stream_tag) {
+				bc_stream_tag = 0;
+			} else {	
+				bc_stream_tag++;
+			}	
+
+			*(IMG_UINT32 *)(psDevInfo->sBufferInfo.szDeviceName + TSBUFFERCLASS_VIDEOID_OFFSET) = bc_stream_tag;
+
+			/* Disable device*/
+			emgd_bc_ts_set_state(psDevInfo, 0);
+			
+			EMGD_DEBUG("Grab a Device - 0x%lx , ID %lu, idx - %d\n", 
+				(unsigned long)psDevInfo,
+				psBridge->dev_id, 
+				bc_video_id[i]);
+			psBridge->rtn = 0;
+          	err = 0;
+      		break;
+    	}
+ 	}
+   	  
+ 	if (BUFCLASS_DEVICE_MAX_ID == i) {
+    	EMGD_ERROR("Do you really need to run more than 5 video simulateously.");
+ 	}
+ 	
+	EMGD_TRACE_EXIT;
+
+	return err;
+}
+
+static __inline int emgd_bc_ts_bridge_uninit(struct drm_device *drv, void* arg, struct drm_file *file_priv)
+{
+	int err = -EFAULT;
+	int i;
+	emgd_drm_bc_ts_t *psBridge = (emgd_drm_bc_ts_t *) arg;
+	BC_DEVINFO *psDevInfo = NULL; 
+
+	EMGD_TRACE_ENTER;
+
+	if (NULL == psBridge) {
+		EMGD_ERROR("Invalid input parameter!\n");
+		return err;
+	}
+
+	psDevInfo = (BC_DEVINFO *)GetAnchorPtr(psBridge->dev_id);
+
+	/* To disable buffer class device*/
+	emgd_bc_ts_set_state(psDevInfo, 0);	
+
+	if (EMGD_OK == BC_DestroyBuffers((void *)psDevInfo)) {
+		EMGD_DEBUG("Free Device -  %lu", psBridge->dev_id);
+		bc_video_id_usage[i] = 0,
+		psBridge->rtn = 0;
+		err = 0;
+	} else {
+		EMGD_ERROR("Uninit device with id %lu failure!\n", psBridge->dev_id);
+	}				
+	
+	EMGD_TRACE_EXIT;
+
+	return err;
+}
+
+static __inline int emgd_bc_ts_bridge_request_buffers(struct drm_device *drv, void* arg, struct drm_file *file_priv)
+{
+	int err = -EFAULT;
+	emgd_drm_bc_ts_t *psBridge = (emgd_drm_bc_ts_t *) arg;
+	bc_buf_params_t p;
+	BC_DEVINFO *bc_devinfo = NULL;
+	IMG_BOOL is_continous = IMG_FALSE;
+	
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == psBridge) {
+		EMGD_ERROR("Invalid input parameter!\n");
+		return err;
+	}
+
+	is_continous = psBridge->is_continous ? IMG_TRUE : IMG_FALSE;
+
+	bc_devinfo = (BC_DEVINFO *)GetAnchorPtr(psBridge->dev_id);
+	if (NULL == bc_devinfo 
+		|| (NULL != bc_devinfo && psBridge->dev_id != bc_devinfo->sBufferInfo.ui32BufferDeviceID)) {
+		EMGD_ERROR("input device id is invalid");
+		return err;
+	}
+	if (NULL != bc_devinfo && 1 == bc_video_id_usage[bc_devinfo->sBufferInfo.ui32BufferDeviceID]) {
+   		psBridge->buf_id = bc_devinfo->sBufferInfo.ui32BufferCount;
+   		p.width = psBridge->width;
+   		p.height = psBridge->height;
+   		p.count = psBridge->num_buf;
+   		p.fourcc = psBridge->pixel_format;
+   		p.stride = psBridge->stride;
+   		p.type = BC_MEMORY_MMAP;
+   		if (0 == BC_CreateBuffers(bc_devinfo, &p, is_continous)) {
+			psBridge->rtn = 0;
+			err = 0;
+       	} else {
+			EMGD_ERROR("Request Buffers failure!\n");
+       	}
+ 	}
+
+	EMGD_TRACE_EXIT;
+
+	return err;
+}
+
+static __inline int emgd_bc_ts_bridge_release_buffers(struct drm_device *drv, void* arg, struct drm_file *file_priv)
+{
+	int err = -EFAULT;
+	emgd_drm_bc_ts_t *psBridge = (emgd_drm_bc_ts_t *) arg;
+	BC_DEVINFO *psDevInfo = NULL;
+	
+	EMGD_TRACE_ENTER;
+
+	if (NULL == psBridge) {
+		EMGD_ERROR("Invalid input parameter!\n");
+		return err;
+	}
+
+	psDevInfo = (BC_DEVINFO *)GetAnchorPtr(psBridge->dev_id);
+
+	if (NULL == psDevInfo 
+		|| (NULL != psDevInfo && psBridge->dev_id != psDevInfo->sBufferInfo.ui32BufferDeviceID)) {
+		EMGD_ERROR("input device id is invalid");
+		return err;
+	}
+	
+	if (NULL != psDevInfo && 1 == bc_video_id_usage[psDevInfo->sBufferInfo.ui32BufferDeviceID]) {
+		emgd_bc_ts_set_state(psDevInfo, 0);
+		
+   		if (EMGD_OK == BC_DestroyBuffers((void *)psDevInfo)) {
+			psBridge->rtn = 0;
+			err = EMGD_OK;
+       	} else {
+			EMGD_ERROR("Release Buffers failure!\n");
+		}
+	}
+	EMGD_TRACE_EXIT;
+
+	return EMGD_OK; 
+}
+
+static __inline int emgd_bc_ts_bridge_set_buffer_info(struct drm_device *drv, void* arg, struct drm_file *file_priv)
+{
+	int err = -EFAULT;
+	emgd_drm_bc_ts_t *psBridge = (emgd_drm_bc_ts_t *) arg;
+	BC_DEVINFO *devinfo = IMG_NULL;	
+	BC_BUFFER *bcBuf = NULL;
+	
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == psBridge) {
+		EMGD_ERROR("Invalid input parameter!");
+		return err;
+	}
+	psBridge->rtn = 1;
+
+	EMGD_DEBUG("dev_id %lu, buf_id %lu, buf_tag %lu, phyaddr 0x%lx, virtadd 0x%lx\n", 
+			psBridge->dev_id,
+			psBridge->buf_id,
+			psBridge->buf_tag,
+			psBridge->phyaddr,
+			psBridge->virtaddr);
+
+	devinfo = (BC_DEVINFO *)GetAnchorPtr(psBridge->dev_id);
+
+	if (NULL == devinfo 
+		|| (NULL != devinfo && psBridge->dev_id != devinfo->sBufferInfo.ui32BufferDeviceID)) {
+		EMGD_ERROR("input device id is invalid");
+		return err;
+	}		
+	/*  To Set Buffer Class Device State */
+ 	if (0xFF == psBridge->buf_id && BUFCLASS_BUFFER_MAX < psBridge->buf_id) {
+			/* 0xFF - Indicates Buffer Class Device is ready. 0xF0 - not ready */
+		if (BC_DEV_READY == psBridge->buf_tag && 0 < devinfo->sBufferInfo.ui32BufferCount) {
+			emgd_bc_ts_set_state(devinfo, 1);	
+			EMGD_DEBUG("dev_id %lu Enable to be Ready!\n", psBridge->dev_id); 
+			goto SUCCESS_OK;
+		}
+		if (BC_DEV_NOT_READY == psBridge->buf_tag) {
+			emgd_bc_ts_set_state(devinfo, 0);
+			EMGD_DEBUG("dev_id %lu Disabled!\n", psBridge->dev_id); 
+			goto SUCCESS_OK;
+		}	
+ 	}
+
+   	if (psBridge->buf_id >= devinfo->sBufferInfo.ui32BufferCount) {
+        EMGD_ERROR("Invalid buf_id");
+        return err;
+    }
+
+    bcBuf = &(devinfo->psSystemBuffer[psBridge->buf_id]);
+
+	if (NULL == bcBuf) {
+        EMGD_ERROR("Invalid buffer: buf_id - %lu!", psBridge->buf_id);
+        return err;					
+	}
+	bcBuf->tag = psBridge->buf_tag;
+	
+	bcBuf->sCPUVAddr = (IMG_CPU_VIRTADDR)psBridge->virtaddr;
+	
+	if (IMG_FALSE == bcBuf->is_conti_addr) {
+		EMGD_ERROR("Only support conti. memory!");
+	} else {
+		bcBuf->psSysAddr[0].uiAddr = psBridge->phyaddr;
+	}
+	
+SUCCESS_OK:    
+	psBridge->rtn = 0;
+	
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+static __inline int emgd_bc_ts_bridge_get_buffers_count(struct drm_device *drv, void* arg, struct drm_file *file_priv)
+{
+	int err = -EFAULT;
+	emgd_drm_bc_ts_t *psBridge = (emgd_drm_bc_ts_t *)arg;
+	BC_DEVINFO *psDevInfo = NULL;
+	
+	EMGD_TRACE_ENTER;
+
+	if (NULL == psBridge) {
+		EMGD_ERROR("Invalid input parameter!\n");
+		return err;
+	}
+
+	psBridge->rtn = 1;
+
+	psDevInfo = (BC_DEVINFO *)GetAnchorPtr(psBridge->dev_id);
+
+	if (NULL == psDevInfo 
+		|| (NULL != psDevInfo && psBridge->dev_id != psDevInfo->sBufferInfo.ui32BufferDeviceID)) {
+		EMGD_ERROR("input device id is invalid");
+		return err;
+	}
+	
+	if (NULL != psDevInfo) {
+		psBridge->num_buf = psDevInfo->sBufferInfo.ui32BufferCount;
+		psBridge->rtn = 0;
+		err = 0;
+	}
+	EMGD_TRACE_EXIT;
+
+	return err;
+}
+
+static __inline int emgd_bc_ts_bridge_get_buffer_index(struct drm_device *drv, void* arg, struct drm_file *file_priv)
+{
+  	EMGD_DEBUG("Not supported\n");
+	return 0;
+}
+
+/* For Buffer Class of Texture Stream */
+int emgd_bc_ts_cmd_init(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_init(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+}
+
+int emgd_bc_ts_cmd_uninit(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_uninit(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+}
+int emgd_bc_ts_cmd_request_buffers(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_request_buffers(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+
+}
+int emgd_bc_ts_cmd_release_buffers(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_release_buffers(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+
+}
+int emgd_bc_ts_set_buffer_info(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_set_buffer_info(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+}
+int emgd_bc_ts_get_buffers_count(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_get_buffers_count(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+}
+int emgd_bc_ts_get_buffer_index(struct drm_device *dev, void *arg, struct drm_file *file_priv)
+{
+	emgd_drm_bc_ts_t *drm_data = (emgd_drm_bc_ts_t *)arg;
+
+	EMGD_TRACE_ENTER;
+	
+	if (NULL == dev || NULL == arg || NULL == file_priv) {
+		return 0;
+	}
+
+	drm_data->rtn = emgd_bc_ts_bridge_get_buffer_index(dev, drm_data, file_priv);
+
+	EMGD_DEBUG("drm_data->rtn = %d", drm_data->rtn);
+	EMGD_DEBUG("Returning 0");
+	EMGD_TRACE_EXIT;
+	
+	return 0;
+}
+
+void emgd_bc_ts_set_state(BC_DEVINFO *psDevInfo, const IMG_CHAR state)
+{
+	if (NULL != psDevInfo) {
+		psDevInfo->sBufferInfo.szDeviceName[TSBUFFERCLASS_DEVSTATUS_OFFSET] = state;
+	}
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.c b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.c
new file mode 100644
index 0000000..b7a879a2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.c
@@ -0,0 +1,2814 @@
+/**********************************************************************
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ ******************************************************************************/
+#define MODULE_NAME hal.pvr3dd
+
+#include <linux/spinlock.h>
+
+#include "drm_emgd_private.h"
+
+#include "img_defs.h"
+#include "servicesext.h"
+#include "kerneldisplay.h"
+#include "emgd_dc.h"
+#include "emgd_drm.h"
+
+
+#if !defined(SUPPORT_DRI_DRM)
+#error "SUPPORT_DRI_DRM must be set"
+#endif
+
+
+/* Function to get the PVR services jump table */
+extern IMG_BOOL PVRGetDisplayClassJTable(PVRSRV_DC_DISP2SRV_KMJTABLE *jtable);
+
+#ifdef SUPPORT_FB_EVENTS
+static emgd_error_t enable_event_notification(emgddc_devinfo_t *devinfo);
+static emgd_error_t disable_event_notification(emgddc_devinfo_t *devinfo);
+#endif /* SUPPORT_FB_EVENTS */
+static void flush_flip_queue(emgddc_swapchain_t *swap_chain);
+static PVRSRV_ERROR do_mode_change(igd_context_t *context,
+		emgddc_devinfo_t *devinfo,
+		drm_emgd_priv_t *priv,
+		DISPLAY_SURF_ATTRIBUTES *dst_surf_attrib);
+
+
+/* Special value used to register with the PVR services command queue: */
+#define EMGDDC_COMMAND_COUNT		1
+
+
+/**
+ * This is a pointer to the global emgddc_devinfo_t structure, used in various
+ * parts of this file.
+ */
+static emgddc_devinfo_t *global_devinfo[] = {NULL, NULL};
+
+
+/**
+ * Pairs of equivalent pixel formats, in EMGD and PVR formats:
+ */
+emgddc_pixelformat_translator_t known_pfs[] = {
+	/* 1 Byte-per-pixel [A]RGB Pixel Formats: */
+	{IGD_PF_ARGB8_INDEXED, PVRSRV_PIXEL_FORMAT_PAL8},   /* IMG numbered */
+	{IGD_PF_ARGB4_INDEXED, PVRSRV_PIXEL_FORMAT_PAL4},   /* IMG numbered */
+
+	/* 2 Byte-per-pixel [A]RGB Pixel Formats: */
+	{IGD_PF_ARGB16_4444, PVRSRV_PIXEL_FORMAT_ARGB4444}, /* IMG# & PVR2D-known */
+	{IGD_PF_ARGB16_1555, PVRSRV_PIXEL_FORMAT_ARGB1555}, /* IMG# & PVR2D-known */
+	{IGD_PF_RGB16_565, PVRSRV_PIXEL_FORMAT_RGB565},     /* IMG# & PVR2D-known */
+	{IGD_PF_xRGB16_555, PVRSRV_PIXEL_FORMAT_RGB555},    /* IMG numbered */
+
+	/* 3 Byte-per-pixel [A]RGB Pixel Formats: */
+	{IGD_PF_RGB24, PVRSRV_PIXEL_FORMAT_RGB888},         /* IMG numbered */
+
+	/* 4 Byte-per-pixel [A]RGB Pixel Formats: */
+	{IGD_PF_xRGB32_8888, PVRSRV_PIXEL_FORMAT_XRGB8888}, /* IMG numbered */
+	/* a.k.a. IGD_PF_ARGB32_8888 */
+	{IGD_PF_ARGB32, PVRSRV_PIXEL_FORMAT_ARGB8888},      /* IMG# & PVR2D-known */
+	{IGD_PF_xBGR32_8888, PVRSRV_PIXEL_FORMAT_XBGR8888}, /* IMG numbered */
+	{IGD_PF_ABGR32_8888, PVRSRV_PIXEL_FORMAT_ABGR8888}, /* IMG numbered */
+
+	/* YUV Packed Pixel Formats: */
+	{IGD_PF_YUV422_PACKED_YUY2, PVRSRV_PIXEL_FORMAT_YUY2},
+	{IGD_PF_YUV422_PACKED_YVYU, PVRSRV_PIXEL_FORMAT_YVYU},
+	{IGD_PF_YUV422_PACKED_UYVY, PVRSRV_PIXEL_FORMAT_UYVY},
+	{IGD_PF_YUV422_PACKED_VYUY, PVRSRV_PIXEL_FORMAT_VYUY},
+	/* UNKNOWN to IMG
+	{IGD_PF_YUV411_PACKED_Y41P, PVRSRV_PIXEL_FORMAT_UNKNOWN},
+	*/
+
+	/* YUV Planar Pixel Formats: */
+	/* a.k.a. IGD_PF_YUV420_PLANAR_IYUV */
+	{IGD_PF_YUV420_PLANAR_I420, PVRSRV_PIXEL_FORMAT_I420},/* IMG numbered */
+	{IGD_PF_YUV420_PLANAR_YV12, PVRSRV_PIXEL_FORMAT_YV12},/* IMG numbered */
+	/* UNKNOWN to IMG
+	{IGD_PF_YUV410_PLANAR_YVU9, PVRSRV_PIXEL_FORMAT_UNKNOWN},
+	*/
+	{IGD_PF_YUV420_PLANAR_NV12, PVRSRV_PIXEL_FORMAT_NV12},
+};
+unsigned int num_known_pfs =
+	sizeof(known_pfs) / sizeof(emgddc_pixelformat_translator_t);
+
+
+/**
+ * Translate EMGD-specific pixel formats into PVR-specific pixel formats.
+ *
+ * @param emgd_pf (IN) EMGD-specific pixel format.
+ * @return Translated PVR-specific pixel format.
+ */
+static unsigned long pvr2emgd_pf(PVRSRV_PIXEL_FORMAT pvr_pf)
+{
+	int i;
+	for (i = 0 ; i < num_known_pfs ; i++) {
+		if (known_pfs[i].pvr_pf == pvr_pf) {
+			return known_pfs[i].emgd_pf;
+		}
+	}
+
+	/* If we get to here, we didn't find a known PVR pixel format: */
+	return IGD_PF_UNKNOWN;
+}
+
+
+/**
+ * Translate EMGD-specific pixel formats into PVR-specific pixel formats.
+ *
+ * @param emgd_pf (IN) EMGD-specific pixel format.
+ * @return Translated PVR-specific pixel format.
+ */
+static PVRSRV_PIXEL_FORMAT emgd2pvr_pf(unsigned long emgd_pf)
+{
+	int i;
+	for (i = 0 ; i < num_known_pfs ; i++) {
+		if (known_pfs[i].emgd_pf == emgd_pf) {
+			return known_pfs[i].pvr_pf;
+		}
+	}
+
+	/* If we get to here, we didn't find a known PVR pixel format: */
+	return IGD_PF_UNKNOWN;
+}
+
+
+/**
+ * Determines if the user-space-provided pointer (to a devinfo) is valid.
+ *
+ * @param devinfo (IN) The user-space-provided pointer to a devinfo.
+ */
+static int is_valid_devinfo(emgddc_devinfo_t *devinfo)
+{
+	if ((devinfo == global_devinfo[0]) || (devinfo == global_devinfo[1])) {
+		return 1;
+	} else {
+		return 0;
+	}
+} /* is_valid_devinfo() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnOpenDCDevice()
+ * function.  This function is called when a client wants to use PVR services
+ * with the specified device.
+ *
+ * @param device_id (IN) The device_id associated with this device (i.e.
+ * obtained when emgddc_init() called
+ * PVRSRV_DC_DISP2SRV_KMJTABLE.pfnPVRSRVRegisterDCDevice()).
+ * @param device_h (OUT) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param system_buffer_sync_data (IN) Sync data for this device's system
+ * buffer.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR OpenDCDevice(IMG_UINT32 device_id,
+	IMG_HANDLE *device_h,
+	PVRSRV_SYNC_DATA* system_buffer_sync_data)
+{
+	emgddc_devinfo_t *devinfo;
+
+	EMGD_TRACE_ENTER;
+
+
+	UNREFERENCED_PARAMETER(device_id);
+
+	/* Look up the device (for DIH/Extended mode): */
+	if (device_id == global_devinfo[0]->device_id) {
+		devinfo = global_devinfo[0];
+		EMGD_DEBUG("devinfo = global_devinfo[0] = 0x%p", devinfo);
+	} else if (device_id == global_devinfo[1]->device_id) {
+		devinfo = global_devinfo[1];
+		EMGD_DEBUG("devinfo = global_devinfo[1] = 0x%p", devinfo);
+	} else {
+		printk(KERN_ERR "[EMGD] OpenDCDevice() called with unknown device ID "
+			"%lu\n", device_id);
+		return PVRSRV_ERROR_INVALID_DEVICE;
+	}
+	devinfo->system_buffer.sync_data = system_buffer_sync_data;
+	*device_h = (IMG_HANDLE) devinfo;
+
+
+	EMGD_TRACE_EXIT;
+
+	return PVRSRV_OK;
+} /* OpenDCDevice() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnCloseDCDevice()
+ * function.  This function is called when a client is finished using PVR
+ * services with the specified device.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR CloseDCDevice(IMG_HANDLE device_h)
+{
+	EMGD_TRACE_STUB;
+
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+	if (!is_valid_devinfo((emgddc_devinfo_t *) device_h)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, device_h);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	return PVRSRV_OK;
+} /* CloseDCDevice() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnEnumDCFormats()
+ * function.  This function is called when a client wants to determine the
+ * pixel format currently being used with, and potentially, to determine which
+ * pixel formats can be used with the specified device.  The first entry in the
+ * array is the current pixel format.
+ *
+ * Note: this function must be called twice.  The first time, the formats
+ * parameter is set to NULL, and the client is trying to determine the number
+ * of pixel formats.  The second time, the formats parameter is non-NULL, and
+ * points to enough memory for num_formats-worth of pixel formats.  Note: this
+ * creates a small window of time between calls where a mode change could
+ * occur; the probability is considered so small, as to not be a worry.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param num_formats (OUT) The number of pixel formats for this device.
+ * @param format (IN/OUT) An array of the pixel formats for this device (ignore
+ * if NULL).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR EnumDCFormats(IMG_HANDLE device_h,
+	IMG_UINT32 *num_formats,
+	DISPLAY_FORMAT *format)
+{
+	emgddc_devinfo_t	*devinfo;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !num_formats) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	if (!is_valid_devinfo(devinfo)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, devinfo);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	*num_formats = devinfo->num_formats;
+
+	if (format) {
+		unsigned long i;
+
+		for (i = 0 ; i < devinfo->num_formats ; i++) {
+			format[i] = devinfo->display_format_list[i];
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* EnumDCFormats() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnEnumDCDims() function.
+ * This function is called when a client wants to determine the current
+ * dimensions (similar to an EMGD mode, but just the dimensions) of, and
+ * potentially, to determine the possible dimensions that can be used with this
+ * device.  The first entry in the array is the current dimension.
+ *
+ * Note: this function must be called twice.  The first time, the dims
+ * parameter is set to NULL, and the client is trying to determine the number
+ * of dimensions.  The second time, the dims parameter is non-NULL, and points
+ * to enough memory for num_dims-worth of dimensions.  Note: this
+ * creates a small window of time between calls where a mode change could
+ * occur; the probability is considered so small, as to not be a worry.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param format (IN) A pointer to a pixel format (unused).
+ * @param num_dims (OUT) The number of dimensions for this device.
+ * @param dims (IN/OUT) An array of the dimensions for this device (ignore
+ * if NULL).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR EnumDCDims(IMG_HANDLE device_h,
+	DISPLAY_FORMAT *format,
+	IMG_UINT32 *num_dims,
+	DISPLAY_DIMS *dims)
+{
+	emgddc_devinfo_t	*devinfo;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !format || !num_dims) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	if (!is_valid_devinfo(devinfo)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, devinfo);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	*num_dims = devinfo->num_dims;
+
+	if (dims) {
+		unsigned long i;
+
+		for (i = 0 ; i < devinfo->num_dims ; i++) {
+			dims[i] = devinfo->display_dim_list[i];
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* EnumDCDims() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnGetDCSystemBuffer()
+ * function.  This function returns a handle to the system buffer
+ * (a.k.a. "frame buffer" or "front buffer") of the specified device.  The
+ * handle is an opaque pointer to the buffer.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param buffer_h (OUT) The handle for this buffer (an opaque pointer to
+ * devinfo->system_buffer).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR GetDCSystemBuffer(IMG_HANDLE device_h, IMG_HANDLE *buffer_h)
+{
+	emgddc_devinfo_t	*devinfo;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !buffer_h) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	if (!is_valid_devinfo(devinfo)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, devinfo);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	*buffer_h = (IMG_HANDLE) &devinfo->system_buffer;
+
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* GetDCSystemBuffer() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnGetDCInfo() function.
+ * This function returns a pointer to the DISPLAY_INFO structure associated
+ * with this device, which contains the driver's name, and information about
+ * swap chains that can be created (i.e. all static information).
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param dc_info (OUT) The DISPLAY_INFO structure associated with this device.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR GetDCInfo(IMG_HANDLE device_h, DISPLAY_INFO *dc_info)
+{
+	emgddc_devinfo_t	*devinfo;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !dc_info) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	if (!is_valid_devinfo(devinfo)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, devinfo);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	*dc_info = devinfo->display_info;
+
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* GetDCInfo() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnGetDCBufferAddr()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param buffer_h (IN) The handle of a buffer (an opaque pointer to an
+ * emgddc_buffer_t) to return information of.
+ * @param phys_addr (OUT) A pointer to an array pointer of page addresses of
+ * the buffer.  For many devices, this would be a pointer to the physical
+ * address of a contiguous set of memory associated with the buffer, but since
+ * EMGD doesn't use contiguous memory for a buffer, it returns the array of
+ * addresses.
+ * @param fb_size (OUT) The size (in bytes) of the buffer.
+ * @param virt_addr (OUT) A pointer to the virtual address (in kernel space) of
+ * the buffer.
+ * @param os_map_info_h (OUT) Ununsed by PVR services.
+ * @param is_contiguous (OUT) A pointer to a boolean that is set to IMG_FALSE,
+ * because EMGD uses non-contiguous pages of memory for buffers.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR GetDCBufferAddr(IMG_HANDLE device_h,
+	IMG_HANDLE buffer_h,
+	IMG_SYS_PHYADDR **phys_addr,
+	IMG_UINT32 *fb_size,
+	IMG_VOID **virt_addr,
+	IMG_HANDLE *os_map_info_h,
+	IMG_BOOL *is_contiguous)
+{
+	emgddc_devinfo_t *devinfo;
+	igd_context_t *context;
+	emgddc_buffer_t *system_buffer;
+	unsigned long page_count = 0;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h) {
+		printk(KERN_ERR "[EMGD] %s() Null device handle.\n", __FUNCTION__);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	if (!is_valid_devinfo(devinfo)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, devinfo);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	context = devinfo->priv->context;
+	if (!context || (context->dispatch.gmm_get_page_list == NULL)) {
+		printk(KERN_ERR "[EMGD] %s() HAL not configured.\n", __FUNCTION__);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if (!buffer_h) {
+		printk(KERN_ERR "[EMGD] %s() Null buffer handle.\n", __FUNCTION__);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	system_buffer = (emgddc_buffer_t *) buffer_h;
+
+	if (!phys_addr) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	EMGD_DEBUG("  phys_addr = 0x%p", phys_addr);
+	EMGD_DEBUG("  *phys_addr = 0x%p", (*phys_addr));
+
+	if ((ret = context->dispatch.gmm_get_page_list(system_buffer->offset,
+		(unsigned long **) phys_addr, &page_count)) != 0) {
+		printk(KERN_ERR"Cannot get the page addresses for the buffer at offset "
+			"0x%08lx\n", system_buffer->offset);
+		EMGD_TRACE_EXIT;
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	EMGD_DEBUG("  phys_addr = 0x%p", phys_addr);
+	EMGD_DEBUG("  *phys_addr = 0x%p", (*phys_addr));
+	EMGD_DEBUG("  (*phys_addr)->uiAddr = 0x%lx", (*phys_addr)->uiAddr);
+
+	if (!fb_size) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if (fb_size) {
+		*fb_size = (IMG_UINT32) system_buffer->size;
+		EMGD_DEBUG("  fb_size = 0x%lx", *fb_size);
+	}
+
+	if (virt_addr) {
+		*virt_addr = system_buffer->virt_addr;
+		EMGD_DEBUG("  virt_addr = 0x%p", *virt_addr);
+	}
+
+	/* Note: this value is ignored by the PVR services code: */
+	if (os_map_info_h) {
+		*os_map_info_h = (IMG_HANDLE)system_buffer->offset;
+		EMGD_DEBUG("  os_map_info_h = 0x%p", *os_map_info_h);
+	}
+
+	/*
+	 * Other than cursor, memory allocations are not contiguous pages
+	 */
+	if (is_contiguous) {
+		if(system_buffer->is_contiguous == IMG_TRUE)
+		{	*is_contiguous = IMG_TRUE;
+		
+		}
+		else{
+			*is_contiguous = IMG_FALSE;
+		
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* GetDCBufferAddr() */
+
+
+/**
+ * Determines if the user-space-provided pointers (to a devinfo and swap chain)
+ * are valid.
+ *
+ * @param devinfo (IN) The user-space-provided pointer to a devinfo.
+ * @param swap_chain (IN) The swap chain to add to the list.
+ * @param must_be_flipable (IN) Non-zero if swap_chain must be flip-able.
+ * @return non-zero if valid, zero if not.
+ */
+static int is_valid_swap_chain(emgddc_devinfo_t *devinfo,
+	emgddc_swapchain_t *swap_chain, int must_be_flipable)
+{
+	if (!is_valid_devinfo(devinfo)) {
+		return 0;
+	} else {
+		/* Search both lists for this swap chain: */
+		emgddc_swapchain_t *swap = devinfo->flipable_swapchains;
+		while (swap) {
+			if (swap == swap_chain) {
+				return 1;
+			}
+			swap = swap->next;
+		}
+		if (!must_be_flipable) {
+			swap = devinfo->pixmap_swapchains;
+			while (swap) {
+				if (swap == swap_chain) {
+					return 1;
+				}
+				swap = swap->next;
+			}
+		}
+	}
+	/* We didn't find this swap chain pointer, so it's not valid: */
+	return 0;
+} /* is_valid_swap_chain() */
+
+
+/**
+ * Called by CreateDCSwapChain() to add a new swap chain to one of the lists of
+ * swap chains.
+ *
+ * @param list (IN) A pointer to a list of swap chains.
+ * @param swap_chain (IN) The swap chain to add to the list.
+ */
+static void add_swap_chain_to_list(emgddc_swapchain_t **list,
+	emgddc_swapchain_t *swap_chain)
+{
+	/* It's simplest to add to the front of the list: */
+	if (*list == NULL) {
+		*list = swap_chain;
+	} else {
+		swap_chain->next = *list;
+		*list = swap_chain;
+	}
+} /* add_swap_chain_to_list() */
+
+
+/**
+ * Called by DestroyDCSwapChain() to remove an about-to-be-deleted swap chain
+ * from one of the lists of swap chains.
+ *
+ * @param list (IN) A pointer to a list of swap chains.
+ * @param swap_chain (IN) The swap chain to remove from the list.
+ */
+static void remove_swap_chain_from_list(emgddc_swapchain_t **list,
+	emgddc_swapchain_t *swap_chain)
+{
+	emgddc_swapchain_t *swap, *prev;
+
+	swap = *list;
+	prev = *list;
+	while (swap) {
+		if (swap == swap_chain) {
+			/* Found match */
+			if (swap == *list) {
+				*list = swap->next;
+			} else {
+				prev->next = swap->next;
+			}
+			break;
+		}
+		prev = swap;
+		swap = swap->next;
+	}
+} /* remove_swap_chain_from_list() */
+
+
+/**
+ * Called by either CreateDCSwapChain() or DestroyDCSwapChain() to free all GMM
+ * and kernel space memory memory of the specified swap chain.
+ *
+ * @param swap_chain (IN) The swap chain to free.
+ * @param context (IN) The EMGD context to use to call gmm_free().
+ */
+static void free_swap_chain(emgddc_swapchain_t *swap_chain,
+	igd_context_t *context)
+{
+	emgddc_buffer_t *buffers;
+	int i=0;
+
+	/*
+	 * Free and unmap the buffers.  Must ensure that the HAL is running before
+	 * calling it, and ensure that we don't free/unmap the first buffer if is
+	 * actually the frame buffer.
+	 */
+	if (swap_chain->devinfo->priv->hal_running) {
+		buffers = swap_chain->buffers;
+
+		if(!buffers[i].is_contiguous){		
+			for (i = 0 ; i < swap_chain->buffer_count ; i++) {
+				if (!buffers[i].is_fb) {	
+					if (buffers[i].virt_addr) {
+						context->dispatch.gmm_unmap(buffers[i].virt_addr);
+					}
+					if (buffers[i].offset) {
+						context->dispatch.gmm_free(buffers[i].offset);
+					}
+				}
+			}
+		}
+		else{
+			context->dispatch.gmm_unmap_ci((unsigned long)buffers[0].virt_addr);
+		}
+	}
+
+	if (swap_chain->flip_queue) {
+		OS_FREE(swap_chain->flip_queue);
+	}
+	OS_FREE(swap_chain->buffers);
+	OS_FREE(swap_chain);
+
+	EMGD_TRACE_EXIT;
+} /* free_swap_chain() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnCreateDCSwapChain()
+ * function.  This function was originally designed to create a flip-able swap
+ * chain on the specified device, but has also been augmented to allow the X
+ * driver to create pixmaps or other buffers in GTT memory.  A flip-able swap
+ * chain consists of a front buffer (the "system buffer, a.k.a. the "frame
+ * buffer") and one or more back buffers (therefore, the number of buffers
+ * includes the frame buffer).
+ *
+ * Note: Only full-screen flipping is supported (the hardware is pointed at one
+ * complete buffer at a time).  There is no support for the
+ * SetDC{Src|Dst}Rect() functions to define a smaller region of the display for
+ * the buffers/flipping.
+ *
+ * Note: The DDK documentation says that the mode should be changed if the
+ * dimensions and pixel format do not match the current dimensions and pixel
+ * format.  However, this can only be supported if the X server isn't running
+ * (which is in charge of changing modes when it is running).  If the X server
+ * is running, an error is returned unless the current dimensions and pixel
+ * format are specified.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param flags (IN) Unused.
+ * @param dst_surf_attrib (IN) While not described in the DDK documentation,
+ * this presumably specifies the desired dimensions and pixel format of the
+ * front buffer.
+ * @param src_surf_attrib (IN) Specifies the desired dimensions and pixel
+ * format of the back buffers.
+ * @param buffer_count (IN) Number of buffers required in this swap chain.
+ * @param sync_data (IN) While not described in the DDK documentation,
+ * this is an array of sync data for each buffer.
+ * @param oem_flags (IN) Unused.
+ * @param swap_chain_h (OUT) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @param swap_chain_id (OUT) Unused.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR CreateDCSwapChain(IMG_HANDLE device_h,
+	IMG_UINT32 flags,
+	DISPLAY_SURF_ATTRIBUTES *dst_surf_attrib,
+	DISPLAY_SURF_ATTRIBUTES *src_surf_attrib,
+	IMG_UINT32 buffer_count,
+	PVRSRV_SYNC_DATA **sync_data,
+	IMG_UINT32 oem_flags,
+	IMG_HANDLE *swap_chain_h,
+	IMG_UINT32 *swap_chain_id)
+{
+	emgddc_devinfo_t	*devinfo;
+	emgddc_swapchain_t *swap_chain;
+	emgddc_buffer_t *buffers;
+	IMG_UINT32 i;
+	emgddc_flip_queue_item_t *flip_queue = NULL;
+	unsigned long lock_flags;
+
+	struct drm_device* drm_dev;
+	drm_emgd_priv_t *priv;
+	igd_context_t *context;
+	igd_dispatch_t *dispatch;
+	int flipable;
+
+	IMG_UINT32 ci_offset=0;
+
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p, buffer_count = %lu", device_h, buffer_count);
+	EMGD_DEBUG("flags = 0x%08lx, oem_flags = 0x%08lx", flags, oem_flags);
+	
+	if(flags & PVR2D_CREATE_FLIPCHAIN_CI)
+	{	
+		ci_offset = src_surf_attrib->ui32Reseved;		
+		
+	}
+		
+	/*
+	 * Check the parameters and dependencies:
+	 */
+	if (!device_h || !dst_surf_attrib || !src_surf_attrib || !sync_data ||
+		!swap_chain_h) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	/*
+	 * The oem_flags will determine what type of swapchain this is.  The
+	 * following types are for a non-flip-able swap chain (e.g. for a pixmap):
+	 *
+	 *  PVR2D_CREATE_FLIPCHAIN_OEMDISPLAY - General purpose displayable
+	 *  PVR2D_CREATE_FLIPCHAIN_OEMGENERAL - General purpose non-displayable
+	 *  PVR2D_CREATE_FLIPCHAIN_OEMOVERLAY - Overlay
+	 *
+	 *  Currently, if none of these flags are set, assume this is going
+	 *  to create a set of back buffers, or a "flip-able" swap chain:
+	 *
+	 *  PVR2D_CREATE_FLIPCHAIN_OEMFLIPCHAIN - Flip-able buffers
+	 */
+	/* Is this is an OEM call (I.E. allocating a buffer)? */
+	if ((oem_flags & (PVR2D_CREATE_FLIPCHAIN_OEMDISPLAY |
+				PVR2D_CREATE_FLIPCHAIN_OEMGENERAL |
+				PVR2D_CREATE_FLIPCHAIN_OEMOVERLAY |
+				PVR2D_CREATE_FLIPCHAIN_CI))) {
+		flipable = 0;
+		
+	} else {
+		/*
+		 * If this is suppose to be an actual flip-able swap chain, then
+		 * make sure there are at least 2 buffers.
+		 */
+		if (buffer_count < 2) {
+			return PVRSRV_ERROR_TOO_FEW_BUFFERS;
+		}
+		flipable = 1;
+		
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	if (!is_valid_devinfo(devinfo)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, devinfo);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+	drm_dev = devinfo->drm_device;
+	priv = drm_dev->dev_private;
+	context = priv->context;
+	dispatch = &(context->dispatch);
+
+
+	if (buffer_count > devinfo->display_info.ui32MaxSwapChainBuffers) {
+		return PVRSRV_ERROR_TOOMANYBUFFERS;
+	}
+
+	/* Ensure the source & destination attributes match each other: */
+	if ((dst_surf_attrib->pixelformat != src_surf_attrib->pixelformat) ||
+		(dst_surf_attrib->sDims.ui32ByteStride !=
+		src_surf_attrib->sDims.ui32ByteStride) ||
+		(dst_surf_attrib->sDims.ui32Width != src_surf_attrib->sDims.ui32Width)||
+		(dst_surf_attrib->sDims.ui32Height !=
+		src_surf_attrib->sDims.ui32Height)) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if (flipable) {
+		PVRSRV_ERROR err =
+			do_mode_change(context, devinfo, priv, dst_surf_attrib);
+		if (err != PVRSRV_OK) {
+			EMGD_DEBUG("Exiting early because of an error in do_mode_change()");
+			EMGD_TRACE_EXIT;
+			return err;
+		}
+	}
+
+	/*
+	 * Allocate data structures:
+	 */
+	swap_chain = (emgddc_swapchain_t *) OS_ALLOC(sizeof(emgddc_swapchain_t));
+	if (!swap_chain) {
+		EMGD_ERROR_EXIT("Can not allocate memory for a swap chain");
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OS_MEMSET(swap_chain, 0, sizeof(emgddc_swapchain_t));
+
+	buffers = (emgddc_buffer_t *) OS_ALLOC(sizeof(emgddc_buffer_t) *
+		buffer_count);
+	if (!buffers) {
+		OS_FREE(swap_chain);
+		EMGD_ERROR_EXIT("Can not allocate memory for swap chain buffers");
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OS_MEMSET(buffers, 0, sizeof(emgddc_buffer_t) * buffer_count);
+
+	if (flipable) {
+		flip_queue = (emgddc_flip_queue_item_t *)
+			OS_ALLOC(sizeof(emgddc_flip_queue_item_t) * buffer_count);
+		if (!flip_queue) {
+			OS_FREE(buffers);
+			OS_FREE(swap_chain);
+			EMGD_ERROR_EXIT("Can not allocate memory for flip queue");
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+		OS_MEMSET(flip_queue, 0,
+			sizeof(emgddc_flip_queue_item_t) * buffer_count);
+	}
+
+	/*
+	 * Initialize data structures:
+	 */
+	swap_chain->devinfo = devinfo;
+	swap_chain->valid = EMGD_TRUE;
+	swap_chain->buffer_count = (unsigned long) buffer_count;
+	swap_chain->buffers = buffers;
+	swap_chain->flags = flags;
+	swap_chain->next = NULL;
+	if (flipable) {
+		swap_chain->flip_queue = flip_queue;
+		swap_chain->insert_index = 0;
+		swap_chain->remove_index = 0;
+	}
+	swap_chain->pvr_jtable = &devinfo->pvr_jtable;
+
+	/* Link the buffers of the swap chain: */
+	for (i = 0 ; i < buffer_count-1 ; i++) {
+		buffers[i].next = &buffers[i+1];
+	}
+	buffers[i].next = &buffers[0];
+
+	i = 0;
+	if (flipable) {
+		/* The first buffer is the frame buffer (a.k.a. the front buffer). */
+		buffers[i].priv = priv;
+		buffers[i].offset = devinfo->system_buffer.offset;
+		buffers[i].pixel_format = devinfo->system_buffer.pixel_format;
+		buffers[i].width = devinfo->system_buffer.width;
+		buffers[i].height = devinfo->system_buffer.height;
+		buffers[i].pitch = devinfo->system_buffer.pitch;
+		buffers[i].size = devinfo->system_buffer.size;
+		buffers[i].virt_addr = devinfo->system_buffer.virt_addr;
+		buffers[i].sync_data = sync_data[0];
+		buffers[i].is_fb = 1;
+		swap_chain->flags |= PVR2D_CREATE_FLIPCHAIN_OEMFLIPCHAIN;
+		i++;
+	}
+
+	/*
+	 * Allocate memory for the buffers
+	 */
+	for (; i < buffer_count ; i++) {
+		unsigned long offset;
+		unsigned long virt_addr;
+		unsigned int width = 0;
+		unsigned int height = 0;
+		unsigned int pitch = 0;
+		unsigned long size = 0;
+		unsigned long pf;
+		unsigned long flags = IGD_SURFACE_RENDER;
+		int ret;
+		unsigned int map_method=1;	/*1: gtt map by va driver*/
+
+		if (!(oem_flags & PVR2D_CREATE_FLIPCHAIN_OEMGENERAL)) {
+			flags |= IGD_SURFACE_DISPLAY;
+		}
+
+		buffers[i].priv = priv;
+
+		/*
+		 * What should be used for the surface attributes, the source
+		 * surface attributes or the destination surface attributes?
+		 * Can we assume that source is the surface requested?
+		 */
+		pf = pvr2emgd_pf(dst_surf_attrib->pixelformat);
+		width = dst_surf_attrib->sDims.ui32Width;
+		height = dst_surf_attrib->sDims.ui32Height;
+		pitch = dst_surf_attrib->sDims.ui32ByteStride;
+		flags |= IGD_MIN_PITCH;
+		
+		if(oem_flags & PVR2D_CREATE_FLIPCHAIN_CI){
+			if(oem_flags & PVR2D_CREATE_FLIPCHAIN_CI_V4L2_MAP)
+				map_method = 0;		
+
+
+			size=height*pitch;
+			ret = dispatch->gmm_map_ci(&offset,
+				ci_offset,	
+				&virt_addr,		
+				map_method,
+				size);
+			
+						
+			if (0 != ret) {
+				free_swap_chain(swap_chain, context);
+				EMGD_ERROR_EXIT("gmm_alloc_surface() failed (%d)", ret);
+				return PVRSRV_ERROR_OUT_OF_MEMORY;
+			}
+			buffers[i].is_contiguous = IMG_TRUE;
+
+		}
+		else{
+			
+			ret = dispatch->gmm_alloc_surface(&offset,
+				pf,
+				&width, &height,
+				&pitch, &size,
+				IGD_GMM_ALLOC_TYPE_NORMAL, &flags);
+			if (0 != ret) {
+				free_swap_chain(swap_chain, context);
+				EMGD_ERROR_EXIT("gmm_alloc_surface() failed (%d)", ret);
+				return PVRSRV_ERROR_OUT_OF_MEMORY;
+			}
+			buffers[i].is_contiguous = IMG_FALSE;
+		}
+
+		dst_surf_attrib->sDims.ui32ByteStride = pitch;
+		src_surf_attrib->sDims.ui32ByteStride = pitch;
+
+		buffers[i].pixel_format = pf;
+		buffers[i].width = width;
+		buffers[i].height = height;
+		buffers[i].pitch = pitch;
+		buffers[i].size = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
+		
+		buffers[i].offset = offset;		
+		
+		if(oem_flags & PVR2D_CREATE_FLIPCHAIN_CI){			
+			
+				buffers[i].virt_addr = (IMG_CPU_VIRTADDR)virt_addr;			
+		}
+		else
+			buffers[i].virt_addr = dispatch->gmm_map(offset);
+		buffers[i].sync_data = sync_data[i];
+		buffers[i].is_fb = 0;
+	} /* for */
+
+
+	if (flipable) {
+		/* Initialize what's needed for flip-able swap chains: */
+		int must_enable;
+
+		for (i = 0 ; i < buffer_count ; i++) {
+			flip_queue[i].valid = EMGD_FALSE;
+			flip_queue[i].flipped = EMGD_FALSE;
+			flip_queue[i].cmd_completed = EMGD_FALSE;
+		}
+
+		spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+
+		must_enable = (devinfo->flipable_swapchains == NULL) ? 1 : 0;
+
+		/* Add this swap chain to the list of flip-able swap chains: */
+		add_swap_chain_to_list(&devinfo->flipable_swapchains, swap_chain);
+
+		/* Unlock here (before enabling interrupts), to prevent deadlock: */
+		spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+		if (!devinfo->flush_commands) {
+			if (must_enable) {
+				/* Enable interrupts for vblanks: */
+				if (!devinfo->interrupt_h || dispatch->
+					enable_vblank_callback(devinfo->interrupt_h)) {
+					/* For some reason (rare), interrupts weren't enabled: */
+					EMGD_ERROR_EXIT("Can not enable VBlank interrupts!  "
+									"Therefore, cannot do buffer flipping!");
+					/* Properly clean up: */
+					remove_swap_chain_from_list(&devinfo->flipable_swapchains,
+												swap_chain);
+					free_swap_chain(swap_chain, context);
+					return PVRSRV_ERROR_BAD_MAPPING;
+				} else {
+					devinfo->flipping_disabled = EMGD_FALSE;
+				}
+			}
+		}
+
+#ifdef SUPPORT_FB_EVENTS
+		if (must_enable) {
+			/* Enable fb events: */
+			if (enable_event_notification(devinfo)!= EMGD_OK) {
+				EMGD_ERROR_EXIT("Can not enable framebuffer event "
+					"notification");
+				/* Properly clean up: */
+				if (devinfo->interrupt_h) {
+					dispatch->disable_vblank_callback(
+						devinfo->interrupt_h);
+				}
+				spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+				remove_swap_chain_from_list(&devinfo->flipable_swapchains,
+					swap_chain);
+				spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+				free_swap_chain(swap_chain, context);
+				return PVRSRV_ERROR_BAD_MAPPING;
+			}
+		}
+#endif /* SUPPORT_FB_EVENTS */
+	} else {
+		/* Add this swap chain to the list of pixmap swap chains: */
+		add_swap_chain_to_list(&devinfo->pixmap_swapchains, swap_chain);
+	}
+
+
+	*swap_chain_id = ++devinfo->swap_chain_id_counter;
+	*swap_chain_h = (IMG_HANDLE) swap_chain;
+	EMGD_DEBUG("swap_chain_h = 0x%p, *swap_chain_id = %lu",
+		swap_chain_h, *swap_chain_id);
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* CreateDCSwapChain() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnDestroyDCSwapChain()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR DestroyDCSwapChain(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h)
+{
+	emgddc_devinfo_t	*devinfo;
+	drm_emgd_priv_t *priv;
+	igd_context_t *context;
+	igd_dispatch_t *dispatch;
+	emgddc_swapchain_t *swap_chain;
+	unsigned long lock_flags;
+#ifdef SUPPORT_FB_EVENTS
+	emgd_error_t error;
+#endif /* SUPPORT_FB_EVENTS */
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !swap_chain_h) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	priv = devinfo->priv;
+	context = priv->context;
+	dispatch = &(context->dispatch);
+	swap_chain = (emgddc_swapchain_t *) swap_chain_h;
+	if (!is_valid_swap_chain(devinfo, swap_chain, 0)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p) and/or "
+			"swap chain handle (0x%p)\n",__FUNCTION__, devinfo, swap_chain);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	/* Remove swap chain from the appropriate list: */
+	if (swap_chain->flags & PVR2D_CREATE_FLIPCHAIN_OEMFLIPCHAIN) {
+		/* De-initialize what's needed for flip-able swap chains: */
+		int must_disable;
+		spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+		must_disable =
+			((devinfo->flipable_swapchains == swap_chain) &&
+			 (devinfo->flipable_swapchains->next == NULL)) ? 1 : 0;
+
+		/* Remove this swap chain from the list of flip-able swap chains: */
+		remove_swap_chain_from_list(&devinfo->flipable_swapchains, swap_chain);
+
+		spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+#ifdef SUPPORT_FB_EVENTS
+		if (must_disable) {
+			/* Disable fb events: */
+			error = disable_event_notification(devinfo);
+			if (error != EMGD_OK) {
+				EMGD_ERROR("Could not disable framebuffer event notification");
+			}
+		}
+#endif /* SUPPORT_FB_EVENTS */
+
+		/* Disable interrupts for vblanks: */
+		if (must_disable) {
+			if (devinfo->interrupt_h) {
+				dispatch->disable_vblank_callback(devinfo->interrupt_h);
+			}
+		}
+
+		/* Flush any pending flips: */
+		flush_flip_queue(swap_chain);
+
+		/* Flip back to the system buffer: */
+		emgddc_flip(swap_chain, &devinfo->system_buffer);
+	} else {
+		/* Remove this swap chain from the list of pixmap swap chains: */
+		remove_swap_chain_from_list(&devinfo->pixmap_swapchains, swap_chain);
+	}
+
+	/* Free all GMM and kernel space memory for this swap chain: */
+	free_swap_chain(swap_chain, context);
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* DestroyDCSwapChain() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSetDCDstRect()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @param rect (IN) Unused.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR SetDCDstRect(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h,
+	IMG_RECT *rect)
+{
+	UNREFERENCED_PARAMETER(device_h);
+	UNREFERENCED_PARAMETER(swap_chain_h);
+	UNREFERENCED_PARAMETER(rect);
+
+	EMGD_TRACE_STUB;
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSetDCSrcRect()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @param rect (IN) Unused.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR SetDCSrcRect(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h,
+	IMG_RECT *rect)
+{
+	UNREFERENCED_PARAMETER(device_h);
+	UNREFERENCED_PARAMETER(swap_chain_h);
+	UNREFERENCED_PARAMETER(rect);
+
+	EMGD_TRACE_STUB;
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSetDCDstColourKey()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @param color (IN) Unused.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR SetDCDstColourKey(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h,
+	IMG_UINT32 color)
+{
+	UNREFERENCED_PARAMETER(device_h);
+	UNREFERENCED_PARAMETER(swap_chain_h);
+	UNREFERENCED_PARAMETER(color);
+
+	EMGD_TRACE_STUB;
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSetDCSrcColourKey()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @param color (IN) Unused.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR SetDCSrcColourKey(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h,
+	IMG_UINT32 color)
+{
+	UNREFERENCED_PARAMETER(device_h);
+	UNREFERENCED_PARAMETER(swap_chain_h);
+	UNREFERENCED_PARAMETER(color);
+
+	EMGD_TRACE_STUB;
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnGetDCBuffers()
+ * function.
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @param buffer_count (OUT) The number of buffers in the specified swap chain.
+ * @param buffer_h (IN/OUT) An array of buffer handles (an opaque pointer to a
+ * emgddc_buffer_t) in the specified swap chain (memory must be allocated by
+ * the caller).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR GetDCBuffers(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h,
+	IMG_UINT32 *buffer_count,
+	IMG_HANDLE *buffer_h)
+{
+	emgddc_devinfo_t *devinfo;
+	emgddc_swapchain_t *swap_chain;
+	unsigned long i;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !swap_chain_h || !buffer_count || !buffer_h) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	swap_chain = (emgddc_swapchain_t *) swap_chain_h;
+	if (!is_valid_swap_chain(devinfo, swap_chain, 0)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p) and/or "
+			"swap chain handle (0x%p)\n",__FUNCTION__, devinfo, swap_chain);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	*buffer_count = (IMG_UINT32) swap_chain->buffer_count;
+
+	for (i = 0 ; i < swap_chain->buffer_count ; i++) {
+		buffer_h[i] = (IMG_HANDLE) &swap_chain->buffers[i];
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* GetDCBuffers() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSwapToDCBuffer()
+ * function.  This function is supposed to cause a flip to the specified
+ * buffer.  However, it is no longer called by PVR services!
+ *
+ * NOTE: As can be seen, this function was never completely implemented
+ * (i.e. in the DDK used to create this version).  This is because the PVR code
+ * doesn't call this function.  Instead, it calls emgddc_process_flip().
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param buffer_h (IN) The handle for this buffer (an opaque pointer to a
+ * emgddc_buffer_t).
+ * @param swap_interval (IN) Unused.
+ * @param private_tag_h (IN) Unused.
+ * @param clip_rect_count (IN) Largely unused.
+ * @param clip_rect (IN) Unused.
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR SwapToDCBuffer(IMG_HANDLE device_h,
+	IMG_HANDLE buffer_h,
+	IMG_UINT32 swap_interval,
+	IMG_HANDLE private_tag_h,
+	IMG_UINT32 clip_rect_count,
+	IMG_RECT *clip_rect)
+{
+	/* This function is never called by PVR services, and so it is stubbed: */
+	UNREFERENCED_PARAMETER(device_h);
+	UNREFERENCED_PARAMETER(buffer_h);
+	UNREFERENCED_PARAMETER(swap_interval);
+	UNREFERENCED_PARAMETER(private_tag_h);
+	UNREFERENCED_PARAMETER(clip_rect_count);
+	UNREFERENCED_PARAMETER(clip_rect);
+
+	EMGD_TRACE_STUB;
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+
+} /* SwapToDCBuffer() */
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSwapToDCSystem()
+ * function.  This function causes a flip to the "system buffer" (a.k.a. frame
+ * buffer).
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param swap_chain_h (IN) The handle for this swap chain (an opaque pointer
+ * to a emgddc_swapchain_t).
+ * @return PVRSRV_OK or a PVRSRV_ERROR enum value.
+ */
+static PVRSRV_ERROR SwapToDCSystem(IMG_HANDLE device_h,
+	IMG_HANDLE swap_chain_h)
+{
+	emgddc_devinfo_t   *devinfo;
+	emgddc_swapchain_t *swap_chain;
+	unsigned long      lock_flags;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!device_h || !swap_chain_h) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	devinfo = (emgddc_devinfo_t *) device_h;
+	swap_chain = (emgddc_swapchain_t *) swap_chain_h;
+	if (!is_valid_swap_chain(devinfo, swap_chain, 1)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p) and/or "
+			"swap chain handle (0x%p)\n",__FUNCTION__, devinfo, swap_chain);
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+	/* This must be a flip-able swap chain, or we can't cause a flip for it: */
+	if (!(swap_chain->flags & PVR2D_CREATE_FLIPCHAIN_OEMFLIPCHAIN)) {
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+
+	flush_flip_queue(swap_chain);
+	emgddc_flip(swap_chain, &devinfo->system_buffer);
+
+	spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+
+	EMGD_TRACE_EXIT;
+	return PVRSRV_OK;
+
+} /* SwapToDCSystem() */
+
+
+/**
+ * Drains the circular queue of buffers to flip for a specified swap chain.
+ * The queue is processed in order.  For every item in the queue, processing is
+ * as follows:
+ *
+ * - An item that has been "completed" (i.e. PVR services has been told that
+ *   the flip occured), but hasn't reached the end of its swap interval, is
+ *   immediately ended and cleared.
+ *
+ * - An item that has been flipped, but hasn't been "completed" with PVR
+ *   services, is "completed," ended, and cleared.
+ *
+ * - An item that has been queued, but hasn't been flipped, is immediately
+ *   flipped (i.e. given the Poulsbo hardware, this really means that the
+ *   hardware is told to flip them at the start of the next vertical blanking
+ *   period), "completed," ended, and cleared.
+ *
+ * Note: This function assumes that the calling function has already obtained
+ * the spin lock.
+ *
+ * @param swap_chain (IN) The swap chain to flush the queue for.
+ */
+static void flush_flip_queue(emgddc_swapchain_t *swap_chain)
+{
+	emgddc_flip_queue_item_t *flip_item;
+	unsigned long max_index;
+	unsigned long i;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Get the first item to drain in the circular queue: */
+	flip_item = &swap_chain->flip_queue[swap_chain->remove_index];
+	max_index = swap_chain->buffer_count - 1;
+
+	for (i = 0 ; i < swap_chain->buffer_count ; i++) {
+		if (flip_item->valid == EMGD_FALSE) {
+			continue;
+		}
+
+		EMGD_DEBUG("Flushing buffer offset=0x%lx", flip_item->buffer->offset);
+
+		if ((swap_chain->devinfo->flipping_disabled == EMGD_FALSE) &&
+			(flip_item->flipped == EMGD_FALSE) &&
+			(swap_chain->valid == EMGD_TRUE)) {
+			EMGD_DEBUG("Flipping to buffer offset=0x%lx",
+				flip_item->buffer->offset);
+			emgddc_flip(swap_chain, flip_item->buffer);
+		}
+
+		if (flip_item->cmd_completed == EMGD_FALSE) {
+			PVRSRV_DC_DISP2SRV_KMJTABLE	*pvr_jtable = swap_chain->pvr_jtable;
+
+			EMGD_DEBUG("Calling pfnPVRSRVCmdComplete() for buffer offset=0x%lx",
+				flip_item->buffer->offset);
+			pvr_jtable->pfnPVRSRVCmdComplete(flip_item->cmd_complete, IMG_TRUE);
+		}
+
+		/* We're done with this item in the queue.  Prepare for processing the
+		 * next item:
+		 */
+		flip_item->flipped = EMGD_FALSE;
+		flip_item->cmd_completed = EMGD_FALSE;
+		flip_item->valid = EMGD_FALSE;
+
+		/* Point to the next item in the circular queue: */
+		swap_chain->remove_index++;
+		if (swap_chain->remove_index > max_index) {
+			swap_chain->remove_index = 0;
+		}
+
+		/* Get the next item in the circular queue: */
+		flip_item = &swap_chain->flip_queue[swap_chain->remove_index];
+	}
+
+	/* Reset the circular queue to the start: */
+	swap_chain->insert_index = 0;
+	swap_chain->remove_index = 0;
+
+
+	EMGD_TRACE_EXIT;
+
+} /* flush_flip_queue() */
+
+
+static void set_flush_state_internal_nolock(emgddc_devinfo_t* devinfo,
+	emgd_bool flush_state)
+{
+	emgddc_swapchain_t *swap_chain = devinfo->flipable_swapchains;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (flush_state) {
+		if (devinfo->set_flush_state_ref_count == 0) {
+			/* Don't actually disable interrupts.  Just set a flag so that
+			 * buffer flips won't be queued, then flush the circular buffer of
+			 * all pending flips, for all swap chains:
+			 */
+			devinfo->flush_commands = EMGD_TRUE;
+			while (swap_chain != NULL) {
+				flush_flip_queue(swap_chain);
+				swap_chain = swap_chain->next;
+			}
+		}
+		devinfo->set_flush_state_ref_count++;
+	} else {
+		if (devinfo->set_flush_state_ref_count != 0) {
+			devinfo->set_flush_state_ref_count--;
+			if (devinfo->set_flush_state_ref_count == 0) {
+				/* Don't actually enable interrupts.  Just set a flag so that
+				 * buffer flips will be queued:
+				 */
+				devinfo->flush_commands = EMGD_FALSE;
+			}
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+}
+
+
+static void set_flush_state_external(emgddc_devinfo_t* devinfo,
+	emgd_bool flush_state)
+{
+	unsigned long lock_flags;
+
+	EMGD_TRACE_ENTER;
+
+
+	spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+
+	if (devinfo->flush_commands != flush_state) {
+		devinfo->flush_commands = flush_state;
+		set_flush_state_internal_nolock(devinfo, flush_state);
+	}
+
+	spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+
+	EMGD_TRACE_EXIT;
+}
+
+
+/**
+ * Implementation of the PVRSRV_DC_SRV2DISP_KMJTABLE.pfnSetDCState() function.
+ * This function is supposed to set some state flags in the driver, affecting
+ * interrupt-driven buffer flips.
+ *
+ * Note: The following function used to never be called, but now is called when
+ * X11 is rotated 90 degrees (it is called as a result of an interrupt handler
+ * dealing with an SGX hardware reset).  As such, the code that this calls
+ * cannot enable/disable interrupts (which may cause an interrupt handler to be
+ * registered/unregistered).
+ *
+ * Note: The code that this calls seems overly complicated.  If we never enable
+ * SUPPORT_FB_EVENTS, it can be simplified (e.g. no use of
+ * set_flush_state_ref_count).
+ *
+ * @param device_h (IN) The handle for this device (an opaque pointer to
+ * devinfo).
+ * @param state (IN) Either DC_STATE_FLUSH_COMMANDS or
+ * DC_STATE_NO_FLUSH_COMMANDS.
+ */
+static IMG_VOID SetDCState(IMG_HANDLE device_h, IMG_UINT32 state)
+{
+	emgddc_devinfo_t *devinfo = (emgddc_devinfo_t *) device_h;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("device_h = 0x%p", device_h);
+
+
+	if (!is_valid_devinfo((emgddc_devinfo_t *) device_h)) {
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p)\n",
+			__FUNCTION__, device_h);
+		return;
+	}
+
+	switch (state) {
+	case DC_STATE_FLUSH_COMMANDS:
+		set_flush_state_external(devinfo, EMGD_TRUE);
+		break;
+	case DC_STATE_NO_FLUSH_COMMANDS:
+		set_flush_state_external(devinfo, EMGD_FALSE);
+		break;
+	default:
+		break;
+	}
+
+
+	EMGD_TRACE_EXIT;
+} /* SetDCState() */
+
+
+#ifdef SUPPORT_FB_EVENTS
+/* NOTE -- The following code is probably not correct.  It was inherited from
+ * an early IMG/UMG DDK, and has never been used.  If we ever decide to use
+ * this code, we should look at the latest DDK and UMG code, to glean what may
+ * be really needed.  At a minimum, the following code needs to affect all swap
+ * chains.
+ */
+
+static int emgddc_fb_events(struct notifier_block *notif,
+	unsigned long event, void *data)
+{
+	emgddc_devinfo_t *devinfo;
+	emgddc_swapchain_t *swap_chain;
+	struct fb_event *fb_event = (struct fb_event *) data;
+	emgd_bool blanked;
+	unsigned long lock_flags;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (event != FB_EVENT_BLANK) {
+		return 0;
+	}
+
+	/* Look up the device (for DIH/Extended mode): */
+	if (notif == &(global_devinfo[0]->lin_notif_block)) {
+		devinfo = global_devinfo[0];
+	} else if (notif == &(global_devinfo[1]->lin_notif_block)) {
+		devinfo = global_devinfo[1];
+	} else {
+		printk(KERN_ERR "[EMGD] emgddc_fb_events() cannot find its device\n");
+		return -ENODEV;
+	}
+	swap_chain = devinfo->flipable_swapchains;
+
+	blanked = (*(IMG_INT *) fb_event->data != 0) ? EMGD_TRUE : EMGD_FALSE;
+
+	if (blanked != swap_chain->blanked) {
+		swap_chain->blanked = blanked;
+
+		spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+
+		if (blanked) {
+			set_flush_state_internal_nolock(devinfo, EMGD_TRUE);
+		} else {
+			set_flush_state_internal_nolock(devinfo, EMGD_FALSE);
+		}
+
+		spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+
+
+static emgd_error_t enable_event_notification(emgddc_devinfo_t *devinfo)
+{
+	int res;
+	emgddc_swapchain_t *swap_chain = devinfo->flipable_swapchains;
+
+	EMGD_TRACE_ENTER;
+
+
+	memset(&devinfo->lin_notif_block, 0, sizeof(devinfo->lin_notif_block));
+
+	devinfo->lin_notif_block.notifier_call = emgddc_fb_events;
+	swap_chain->blanked = EMGD_FALSE;
+	res = fb_register_client(&devinfo->lin_notif_block);
+	if (res != 0) {
+		EMGD_ERROR_EXIT("fb_register_client() failed (%d)", res);
+		return EMGD_ERROR_GENERIC;
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return EMGD_OK;
+}
+
+
+static emgd_error_t disable_event_notification(emgddc_devinfo_t *devinfo)
+{
+	int res;
+
+
+	EMGD_TRACE_ENTER;
+
+
+	res = fb_unregister_client(&devinfo->lin_notif_block);
+	if (res != 0) {
+		EMGD_ERROR_EXIT("fb_unregister_client() failed (%d)", res);
+		return EMGD_ERROR_GENERIC;
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return EMGD_OK;
+}
+#endif /* SUPPORT_FB_EVENTS */
+
+
+/**
+ * This function does per-vblank processing of the circular queue of buffers to
+ * flip.  It is called for each flip-able swap chain, during a VBlank
+ * interrupt.  The first item in the queue is processed, and if it is cleared
+ * (see below), the next item is processed, etc.  Items are processed as
+ * follows:
+ *
+ * - An item that hasn't been flipped, is flipped.  In this case, processing
+ *   stops.  It is assumed that the swap interval for this item is at least
+ *   one, and therefore, another vblank is needed for this flip.
+ *
+ * - An item that has been flipped, but hasn't been "completed" (i.e. PVR
+ *   services has been told that the flip occured), is "completed" and has its
+ *   swap interval decremented by 1.  If the swap interval is now 0, the item
+ *   is cleared, allowing processing of the next item to start.
+ *
+ * - An item that has been "completed," but hasn't reached the end of its swap
+ *   interval, has its swap interval decremented by 1.  If the swap interval is
+ *   now 0, the item is cleared, allowing processing of the next item to start.
+ *
+ * @param swap_chain (IN) The swap chain to process the queue for.
+ */
+emgd_bool emgddc_process_flip_queue_for_vblank(emgddc_swapchain_t *swap_chain)
+{
+	PVRSRV_DC_DISP2SRV_KMJTABLE	*pvr_jtable = swap_chain->pvr_jtable;
+	IMG_BOOL status = IMG_TRUE;
+	emgddc_flip_queue_item_t *flip_item;
+	unsigned long max_index;
+
+	EMGD_TRACE_ENTER;
+
+
+	if (swap_chain->devinfo->flush_commands) {
+		EMGD_TRACE_EXIT;
+		return status;
+	}
+
+	/* Get the first item to process in the circular queue: */
+	flip_item = &swap_chain->flip_queue[swap_chain->remove_index];
+	max_index = swap_chain->buffer_count - 1;
+
+	while (flip_item->valid) {
+		if (flip_item->flipped) {
+			if (!flip_item->cmd_completed) {
+				EMGD_DEBUG("Calling pfnPVRSRVCmdComplete() for buffer "
+					"offset=0x%lx", flip_item->buffer->offset);
+				pvr_jtable->pfnPVRSRVCmdComplete(flip_item->cmd_complete,
+					IMG_TRUE);
+				flip_item->cmd_completed = EMGD_TRUE;
+			}
+
+			flip_item->swap_interval--;
+			EMGD_DEBUG("Swap interval is %lu for buffer offset=0x%lx",
+				flip_item->swap_interval, flip_item->buffer->offset);
+
+			if (flip_item->swap_interval == 0) {
+				/* We're done with this item in the queue.  Prepare for
+				 * processing the next item:
+				 */
+				flip_item->cmd_completed = EMGD_FALSE;
+				flip_item->flipped = EMGD_FALSE;
+				flip_item->valid = EMGD_FALSE;
+
+				/* Point to the next item in the circular queue: */
+				swap_chain->remove_index++;
+				if (swap_chain->remove_index > max_index) {
+					swap_chain->remove_index = 0;
+				}
+			} else {
+				/* Wait for more vblanks before doing more queue processing: */
+				break;
+			}
+		} else {
+			EMGD_DEBUG("Flipping to buffer offset=0x%lx",
+				flip_item->buffer->offset);
+			emgddc_flip(swap_chain, flip_item->buffer);
+			flip_item->flipped = EMGD_TRUE;
+			/* Wait for more vblanks before doing more queue processing: */
+			break;
+		}
+
+		/* Get the next item in the circular queue: */
+		flip_item = &swap_chain->flip_queue[swap_chain->remove_index];
+	}
+
+
+	EMGD_TRACE_EXIT;
+
+	return status;
+
+} /* emgddc_process_flip_queue_for_vblank() */
+
+
+/**
+ * This is called by a HAL-implemented, Linux interrupt handler.  It is called
+ * when a VBlank interrupt occurs.  All device-specific functionality was
+ * implemented by the HAL, and only 3DD-specific functionality needs to be
+ * provided by this function.
+ *
+ * @param pdevinfo (IN) Pointer to the devinfo that had a VBlank interrupt.
+ * @return Non-zero for success, zeron for failure.
+ */
+static int emgddc_process_vblank(void* pdevinfo)
+{
+	emgddc_devinfo_t *devinfo = (emgddc_devinfo_t *) pdevinfo;
+	emgddc_swapchain_t *swap_chain;
+	unsigned long lock_flags;
+
+	EMGD_TRACE_ENTER;
+
+
+	if ((devinfo != global_devinfo[0]) && (devinfo != global_devinfo[1])) {
+		return 0;
+	}
+
+	spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+
+	swap_chain = devinfo->flipable_swapchains;
+	while (swap_chain != NULL) {
+		(void) emgddc_process_flip_queue_for_vblank(swap_chain);
+		swap_chain = swap_chain->next;
+	}
+
+	spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+	return 1;
+
+} /* emgddc_process_vblank() */
+
+
+/**
+ * Called by PVR services to flip a buffer.  When interrupts are supported, the
+ * flip may be queued (in a circular buffer) to happen later.  When interrupts
+ * are not supported, the flip always happens immediately
+ *
+ * @param cmd_cookie_h (IN) An opaque pointer to a PVR service data structure
+ *   that must be handed back when the flip is "completed" (i.e. PVR services
+ *   is told that the flip occured).
+ * @param data_size (IN) Size of the flip command and all clipping rectangles
+ * (which isn't supported).  This is only used for consistency-checking.
+ * @param data (IN) A pointer to information about what to flip.
+ */
+static IMG_BOOL emgddc_process_flip(IMG_HANDLE cmd_cookie_h,
+	IMG_UINT32 data_size,
+	IMG_VOID *data)
+{
+	DISPLAYCLASS_FLIP_COMMAND *flip_cmd;
+	emgddc_devinfo_t *devinfo;
+	emgddc_buffer_t *buffers;
+	emgddc_swapchain_t *swap_chain;
+	unsigned long max_index;
+	emgddc_flip_queue_item_t* flip_item;
+	unsigned long lock_flags;
+	int must_flip = 0;
+	int must_complete = 0;
+	igd_context_t *context;
+
+	EMGD_TRACE_ENTER;
+
+
+	/*
+	 * Unpack the flip command and look for errors:
+	 */
+
+	if (!cmd_cookie_h || !data) {
+		EMGD_ERROR_EXIT("NULL parameter(s)");
+		return IMG_FALSE;
+	}
+
+	flip_cmd = (DISPLAYCLASS_FLIP_COMMAND *) data;
+
+	/* Note: the data_size actually accounts for both the flip command and
+	 * all of the clipping rectangles.  As such, the only error is if
+	 * data_size is smaller than the sizeof the flip command:
+	 */
+	if (flip_cmd == IMG_NULL ||
+		(sizeof(DISPLAYCLASS_FLIP_COMMAND) > data_size)) {
+		EMGD_ERROR_EXIT("Invalid flip_cmd (0x%p)", flip_cmd);
+		return IMG_FALSE;
+	}
+
+	devinfo = (emgddc_devinfo_t *) flip_cmd->hExtDevice;
+	buffers = (emgddc_buffer_t *) flip_cmd->hExtBuffer;
+	swap_chain = (emgddc_swapchain_t *) flip_cmd->hExtSwapChain;
+
+	if (!is_valid_swap_chain(devinfo, swap_chain, 1)) {
+		/* Note: Hardware video decode creates pixmap swap chains,
+		 * and when they are being destroyed (at
+		 * the end of video playback), something tries to flip these
+		 * non-flipable swap chains.  The only way to avoid a hang is to
+		 * "complete" the flip command.
+		 */
+		printk(KERN_ERR "[EMGD] %s() given invalid device handle (0x%p) and/or "
+			"swap chain handle (0x%p)\n",__FUNCTION__, devinfo, swap_chain);
+		swap_chain->pvr_jtable->pfnPVRSRVCmdComplete(cmd_cookie_h, IMG_TRUE);
+		EMGD_TRACE_EXIT;
+		return IMG_TRUE;
+	}
+
+	spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+
+	if ((devinfo->flipping_disabled == EMGD_TRUE) ||
+		(swap_chain->valid != EMGD_TRUE)) {
+		/* We won't flip, but must tell PVR services that the flip occured: */
+		EMGD_DEBUG("Something (e.g. a mode change) has invalidated\n"
+			"this swap chain.  As such buffer flips are not allowed.\n"
+			"If a mode change caused this problem, this swap chain\n"
+			"needs to be destroyed, and a new one created.");
+		swap_chain->pvr_jtable->pfnPVRSRVCmdComplete(cmd_cookie_h, IMG_TRUE);
+		spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+		EMGD_TRACE_EXIT;
+		return IMG_TRUE;
+	}
+
+	context = devinfo->priv->context;
+	if (context->device_context.power_state != IGD_POWERSTATE_D0) {
+		/* If device is in a suspended state, but PVR services asks the driver
+		 * to perform a buffer flip, basically ignore it except for telling PVR
+		 * services that we did the flip:
+		 */
+		EMGD_DEBUG("Device in suspended state--completing command");
+		swap_chain->pvr_jtable->pfnPVRSRVCmdComplete(cmd_cookie_h, IMG_TRUE);
+		spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+		EMGD_TRACE_EXIT;
+		return IMG_TRUE;
+	}
+
+	/* Get the first item to add to the circular queue: */
+	flip_item = &swap_chain->flip_queue[swap_chain->insert_index];
+	max_index = swap_chain->buffer_count - 1;
+
+
+	/*
+	 * Decide what needs to be done:
+	 */
+	if ((flip_cmd->ui32SwapInterval == 0) ||
+		(devinfo->flush_commands == EMGD_TRUE)) {
+		/* Perform and complete the flip now: */
+		must_flip = 1;
+		must_complete = 1;
+	} else {
+		/* PVR services only calls emgddc_process_flip() when the 3DD completes
+		 * the previous flip.  Thus, the circular flip_item queue should never
+		 * overflow.  However, just in case, check whether the flip_item
+		 * already contains a valid/queued flip (i.e. we've overflowed the
+		 * circular queue).
+		 */
+		if (flip_item->valid == EMGD_FALSE) {
+			if (swap_chain->insert_index == swap_chain->remove_index) {
+			/* Perform the flip now, but queue it for completion: */
+				must_flip = 1;
+			}
+			/* else - queue the flip for later: */
+		} else {
+			/* Just in case we overflow the circular queue, generate an error */
+			swap_chain->pvr_jtable->pfnPVRSRVCmdComplete(cmd_cookie_h,IMG_TRUE);
+			spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+			EMGD_ERROR_EXIT("Overflowed the circular flip_item queue");
+			return IMG_FALSE;
+		}
+	}
+
+
+	/*
+	 * Do what needs to be done:
+	 */
+	if (must_flip) {
+		/* Perform the flip now: */
+		EMGD_DEBUG("Flipping to buffer offset=0x%lx", buffers->offset);
+		emgddc_flip(swap_chain, buffers);
+	}
+	if (must_complete) {
+		/* Tell the PVR services that the flip occured: */
+		EMGD_DEBUG("Calling pfnPVRSRVCmdComplete() for buffer offset=0x%lx",
+			buffers->offset);
+		swap_chain->pvr_jtable->pfnPVRSRVCmdComplete(cmd_cookie_h,IMG_TRUE);
+	} else {
+		/* Queue the flip for later completion: */
+		EMGD_DEBUG("Queueing buffer offset=0x%lx", buffers->offset);
+		if (must_flip) {
+			flip_item->flipped = EMGD_TRUE;
+		}
+		flip_item->cmd_complete = cmd_cookie_h;
+		flip_item->swap_interval = (unsigned long) flip_cmd->ui32SwapInterval;
+		flip_item->valid = EMGD_TRUE;
+		flip_item->buffer = buffers;
+
+		swap_chain->insert_index++;
+		if (swap_chain->insert_index > max_index) {
+			swap_chain->insert_index = 0;
+		}
+	}
+
+	spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+	EMGD_TRACE_EXIT;
+	return IMG_TRUE;
+} /* emgddc_process_flip() */
+
+
+/**
+ * For a given devinfo, unmap's the frame buffer, and frees the devinfo and
+ * it's surfaces.
+ *
+ * @param dev (IN) The drm_device for this driver connection.
+ */
+void emgddc_free_a_devinfo(emgddc_devinfo_t *devinfo)
+{
+	igd_context_t *context = devinfo->priv->context;
+
+	EMGD_TRACE_ENTER;
+
+	if (devinfo->priv->hal_running) {
+		/* Un-register (if needed) the interrupt connection with the HAL: */
+		if (devinfo->interrupt_h) {
+			context->dispatch.unregister_vblank_callback(devinfo->interrupt_h);
+			devinfo->interrupt_h = NULL;
+		}
+
+		if (devinfo->system_buffer.virt_addr) {
+			context->dispatch.gmm_unmap(devinfo->system_buffer.virt_addr);
+		}
+	}
+	OS_FREE(devinfo);
+
+	EMGD_TRACE_EXIT;
+} /* emgddc_free_a_devinfo() */
+
+
+/**
+ * Frees all devinfo structures and their surfaces.  This is called during
+ * de-init time, or when init fails.
+ */
+void emgddc_free_all_devinfos(void)
+{
+	emgddc_devinfo_t *devinfo;
+
+	EMGD_TRACE_ENTER;
+
+	/* Free the primary display's structures: */
+	devinfo = global_devinfo[0];
+	emgddc_free_a_devinfo(devinfo);
+	global_devinfo[0] = NULL;
+
+	/* Free the secondary display's structures, if applicable: */
+	if (NULL == (devinfo = global_devinfo[1])) {
+		emgddc_free_a_devinfo(devinfo);
+		global_devinfo[1] = NULL;
+	}
+
+	EMGD_TRACE_EXIT;
+} /* emgddc_free_all_devinfos() */
+
+
+/**
+ * Initialize the "static" (i.e. doesn't vary with alter_display) portion of
+ * a devinfo structure.
+ *
+ * @param dev (IN) The drm_device for this driver connection.
+ * @param devinfo (IN/OUT) The devinfo to initialize.
+ * @param port_number (IN) Which devinfo (0 for primary, 1 for secondary/DIH).
+ * @return emgd_error_t value (e.g. EMGD_OK, EMGD_ERROR_OUT_OF_MEMORY).
+ */
+static emgd_error_t emgddc_init_devinfo(struct drm_device *dev,
+	emgddc_devinfo_t *devinfo, int which_devinfo)
+{
+	drm_emgd_priv_t *priv = dev->dev_private;
+	PVRSRV_DC_DISP2SRV_KMJTABLE	*pvr_jtable;
+	PFN_CMD_PROC cmd_proc_list[EMGDDC_COMMAND_COUNT];
+	IMG_UINT32 sync_count_list[EMGDDC_COMMAND_COUNT][2];
+
+	EMGD_TRACE_ENTER;
+
+
+	/*
+	 * Initialize the static/display-independent devinfo values:
+	 */
+	devinfo->which_devinfo = which_devinfo;
+	devinfo->priv = priv;
+	devinfo->drm_device = dev;
+	devinfo->flipable_swapchains = NULL;
+	devinfo->swap_chain_id_counter = 0;
+	devinfo->display_info.ui32MaxSwapChainBuffers = 5;
+	devinfo->display_info.ui32MaxSwapChains = 1024 * 1024;
+	/* Note: change from zero if we support interrupts and see the need: */
+	devinfo->display_info.ui32MaxSwapInterval = 2;
+	devinfo->display_info.ui32MinSwapInterval = 0;
+	EMGD_DEBUG("Maximum number of swap chains: %lu",
+		devinfo->display_info.ui32MaxSwapChains);
+	EMGD_DEBUG("Maximum number of swap chain buffers: %lu",
+		devinfo->display_info.ui32MaxSwapChainBuffers);
+	strncpy(devinfo->display_info.szDisplayName, DISPLAY_DEVICE_NAME,
+		MAX_DISPLAY_NAME_SIZE);
+	devinfo->flush_commands = EMGD_FALSE;
+	spin_lock_init(&devinfo->swap_chain_lock);
+
+
+	/*
+	 * Get the PVR services jump table, which this 3rd-party display driver can
+	 * use to call PVR services:
+	 */
+	if (!PVRGetDisplayClassJTable(&devinfo->pvr_jtable)) {
+		EMGD_ERROR_EXIT("Can not get PVR services jump table");
+		return EMGD_ERROR_INIT_FAILURE;
+	}
+	pvr_jtable = &devinfo->pvr_jtable;
+
+
+	/*
+	 * Setup the jump table that PVR services uses to call this 3rd-party
+	 * display driver:
+	 */
+	devinfo->dc_jtable.ui32TableSize = sizeof(PVRSRV_DC_SRV2DISP_KMJTABLE);
+	devinfo->dc_jtable.pfnOpenDCDevice = OpenDCDevice;
+	devinfo->dc_jtable.pfnCloseDCDevice = CloseDCDevice;
+	devinfo->dc_jtable.pfnEnumDCFormats = EnumDCFormats;
+	devinfo->dc_jtable.pfnEnumDCDims = EnumDCDims;
+	devinfo->dc_jtable.pfnGetDCSystemBuffer = GetDCSystemBuffer;
+	devinfo->dc_jtable.pfnGetDCInfo = GetDCInfo;
+	devinfo->dc_jtable.pfnGetBufferAddr = GetDCBufferAddr;
+	devinfo->dc_jtable.pfnCreateDCSwapChain = CreateDCSwapChain;
+	devinfo->dc_jtable.pfnDestroyDCSwapChain = DestroyDCSwapChain;
+	devinfo->dc_jtable.pfnSetDCDstRect = SetDCDstRect;
+	devinfo->dc_jtable.pfnSetDCSrcRect = SetDCSrcRect;
+	devinfo->dc_jtable.pfnSetDCDstColourKey = SetDCDstColourKey;
+	devinfo->dc_jtable.pfnSetDCSrcColourKey = SetDCSrcColourKey;
+	devinfo->dc_jtable.pfnGetDCBuffers = GetDCBuffers;
+	devinfo->dc_jtable.pfnSwapToDCBuffer = SwapToDCBuffer;
+	devinfo->dc_jtable.pfnSwapToDCSystem = SwapToDCSystem;
+	devinfo->dc_jtable.pfnSetDCState = SetDCState;
+
+
+	/*
+	 * Register this device with PVR services:
+	 */
+	if (pvr_jtable->pfnPVRSRVRegisterDCDevice(&devinfo->dc_jtable,
+		&devinfo->device_id ) != PVRSRV_OK) {
+		EMGD_ERROR_EXIT("Device registration failed");
+		return EMGD_ERROR_DEVICE_REGISTER_FAILED;
+	}
+
+	EMGD_DEBUG("Device ID: %d", (int)devinfo->device_id);
+
+
+	/*
+	 * Tell PVR services about the function to process swap-chain buffer
+	 * flips:
+	 */
+	cmd_proc_list[DC_FLIP_COMMAND] = emgddc_process_flip;
+
+	/* FIXME:  Not sure what these are for: */
+	sync_count_list[DC_FLIP_COMMAND][0] = 0; 
+	sync_count_list[DC_FLIP_COMMAND][1] = 2; 
+
+	if (pvr_jtable->pfnPVRSRVRegisterCmdProcList(devinfo->device_id,
+		&cmd_proc_list[0],
+		sync_count_list,
+		EMGDDC_COMMAND_COUNT) != PVRSRV_OK) {
+		EMGD_ERROR_EXIT("Can't register callback\n");
+		return EMGD_ERROR_CANT_REGISTER_CALLBACK;
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return EMGD_OK;
+
+} /* emgddc_init_devinfo() */
+
+
+/** 
+ * Initializes the display/device-specific values of the devinfo structure for
+ * a specified display (primary or secondary).  This function is called during
+ * initialization time, and whenever the EMGD driver does a potential mode
+ * change, via alter_displays().
+ *
+ * @param devinfo (IN/OUT) The devinfo to initialize for the display.
+ * @param display (IN) The specified display (primary or secondary).
+ * @param port_number (IN) The port number of the specified display.
+ * @return emgd_error_t value (e.g. EMGD_OK, EMGD_ERROR_OUT_OF_MEMORY).
+ */
+static emgd_error_t init_display(emgddc_devinfo_t *devinfo,
+	igd_display_h display,
+	unsigned short port_number)
+{
+	drm_emgd_priv_t *priv = devinfo->priv;
+	igd_context_t *context = priv->context;
+	unsigned long dc = priv->dc;
+	igd_framebuffer_info_t fb_info;
+	DISPLAY_FORMAT *display_format_list;
+	igd_display_info_t pt_info;
+	unsigned long *fb_list_pfs;
+	DISPLAY_DIMS *display_dim_list;
+	igd_display_info_t *mode_list = NULL;
+	igd_display_info_t *mode;
+	int mode_flags = IGD_QUERY_LIVE_MODES;
+	emgddc_buffer_t *buffer = &(devinfo->system_buffer);
+	int i = 1, j, ret;
+
+	EMGD_TRACE_ENTER;
+
+	EMGD_DEBUG("Parameters:");
+	EMGD_DEBUG(" devinfo=0x%p", devinfo);
+	EMGD_DEBUG(" devinfo->which_devinfo=%d", devinfo->which_devinfo);
+	EMGD_DEBUG(" display=0x%p", display);
+	EMGD_DEBUG(" port_number=%u", port_number);
+
+	/* Clear the following lists, in case we are re-initializing: */
+	OS_MEMSET(devinfo->display_format_list, 0,
+			sizeof(devinfo->display_format_list));
+	OS_MEMSET(devinfo->display_dim_list, 0,
+			sizeof(devinfo->display_dim_list));
+	display_format_list = devinfo->display_format_list;
+	display_dim_list = devinfo->display_dim_list;
+
+
+	/* Call get_display() to get some of the following info: */
+	ret = context->dispatch.get_display(display, port_number,
+		&fb_info, &pt_info, 0);
+	if (0 != ret) {
+		EMGD_ERROR_EXIT("get_display() returned %d", ret);
+		return EMGD_ERROR_GENERIC;
+	}
+
+
+	/*
+	 * Obtain the following addresses:
+	 *
+	 * - buffer->offset = Framebuffer GTT address
+	 * - virt_addr = gmm_map(framebuffer offset);
+	 */
+	buffer->offset = fb_info.fb_base_offset;
+	if(NULL == buffer->virt_addr){
+		buffer->virt_addr = context->dispatch.gmm_map(fb_info.fb_base_offset);
+	}
+	EMGD_DEBUG("buffer->virt_addr = 0x%p", buffer->virt_addr);
+	/* This is the offset of the allocated framebuffer (e.g. the
+	 * 1024x768 surface of gmm-managed memory):
+	 */
+	EMGD_DEBUG("fb_info->fb_base_offset = 0x%lx", fb_info.fb_base_offset);
+
+
+	/* Register (if we haven't) with the HAL to be able to use interrupts: */
+	if (!devinfo->interrupt_h) {
+		devinfo->interrupt_h =
+			context->dispatch.register_vblank_callback(emgddc_process_vblank,
+				devinfo, port_number);
+		if (!devinfo->interrupt_h) {
+			/* This should not happen, but just in case, provide an error: */
+			printk(KERN_ERR "Cannot establish the ability to perform VBlank "
+				"interrupts for port number %u.", port_number);
+		}
+	}
+
+
+	/*
+	 * Obtain and translate pixel formats:
+	 */
+	/* Note: We always care about the current pixel format: */
+	devinfo->emgd_pf = fb_info.pixel_format;
+	devinfo->pvr_pf = emgd2pvr_pf(fb_info.pixel_format);
+	display_format_list[0].pixelformat = emgd2pvr_pf(fb_info.pixel_format);
+	EMGD_DEBUG("FB's native EMGD pixel format = 0x%08lx", devinfo->emgd_pf);
+	EMGD_DEBUG("FB's PVR pixel format = %u",display_format_list[0].pixelformat);
+	if (!priv->xserver_running) {
+		/* In addition, provide an entire list of pixel formats: */
+		ret = context->dispatch.get_pixelformats(display, &fb_list_pfs, NULL,
+			NULL, NULL, NULL);
+		if (0 != ret) {
+			EMGD_ERROR_EXIT("get_pixelformats() returned %d", ret);
+			return EMGD_ERROR_GENERIC;
+		}
+		while (*fb_list_pfs) {
+			if ((devinfo->pvr_pf != emgd2pvr_pf(*fb_list_pfs)) &&
+				(PVRSRV_PIXEL_FORMAT_UNKNOWN != emgd2pvr_pf(*fb_list_pfs))) {
+				display_format_list[i].pixelformat = emgd2pvr_pf(*fb_list_pfs);
+				EMGD_DEBUG("  Add'l (%d) PVR pixel format = "
+					"%u", i, emgd2pvr_pf(*fb_list_pfs));
+			}
+			fb_list_pfs++;
+			if (++i >= EMGDDC_MAXFORMATS) {
+				/* Don't write past the end of the array */
+				EMGD_ERROR("Reached end of display_format_list!  Consider "
+						"increasing EMGDDC_MAXFORMATS.");
+				break;
+			}
+		}
+	}
+	devinfo->num_formats = i;
+	EMGD_DEBUG("Total number of translated pixel formats = %d", i);
+
+
+	/*
+	 * Obtain the possible dimensions, from the EMGD modes:
+	 */
+	/* Note: we always care about the current dimensions: */
+	devinfo->width = fb_info.width;
+	devinfo->height = fb_info.height;
+	devinfo->byte_stride = fb_info.screen_pitch;
+	EMGD_DEBUG("FB's width = %ld, height = %ld, stride = %ld",
+		devinfo->width, devinfo->height, devinfo->byte_stride);
+	display_dim_list[0].ui32Width = fb_info.width;
+	display_dim_list[0].ui32Height = fb_info.height;
+	display_dim_list[0].ui32ByteStride = fb_info.screen_pitch;
+	i = 1;
+	if (!priv->xserver_running) {
+		/* In addition, provide an entire list of dimensions: */
+		ret = context->dispatch.query_mode_list((igd_driver_h) context, dc,
+			&mode_list, mode_flags);
+		if (0 != ret) {
+			EMGD_ERROR_EXIT("query_mode_list() returned %d", ret);
+			return EMGD_ERROR_GENERIC;
+		}
+		mode = mode_list;
+		while (mode && (mode->width != IGD_TIMING_TABLE_END)) {
+			int seen = 0;
+			for (j = i - 1 ; j >= 0 ; j--) {
+				if ((display_dim_list[j].ui32Width == mode->width) &&
+					(display_dim_list[j].ui32Height == mode->height)) {
+					seen = 1;
+				}
+			}
+			if (!seen) {
+				display_dim_list[i].ui32Width = mode->width;
+				display_dim_list[i].ui32Height = mode->height;
+				display_dim_list[i].ui32ByteStride =
+					(mode->width * IGD_PF_BPP(devinfo->emgd_pf) + 7) >>3;
+				EMGD_DEBUG("  Add'l width = %d, height = %d, stride = %ld",
+					mode->width, mode->height,
+					display_dim_list[i].ui32ByteStride);
+				if (++i >= EMGDDC_MAXDIMS) {
+					/* Don't write past the end of the array */
+					EMGD_ERROR("Reached end of display_dim_list!  Consider "
+							"increasing EMGDDC_MAXDIMS.");
+					break;
+				}
+			}
+			mode++;
+		}
+	}
+	devinfo->num_dims = i;
+	EMGD_DEBUG("Total number of dimensions = %d", i);
+
+
+	/*
+	 * Obtain the size of the frame buffer, which will also be the size of any
+	 * swap chain buffers:
+	 */
+	devinfo->fb_size = devinfo->height * devinfo->byte_stride;
+	EMGD_DEBUG("Frame buffer size = %lu = %luMB = 0x%lx", devinfo->fb_size,
+		devinfo->fb_size / (1024 * 1024), devinfo->fb_size);
+
+
+	/*
+	 * Initialize the igd_surface_t structure for the frame buffer, in order
+	 * allow the buffer flipping code to flip back to the frame buffer:
+	 */
+	buffer->priv = priv;
+	buffer->offset = fb_info.fb_base_offset;
+	buffer->pitch = devinfo->byte_stride;
+	buffer->width = devinfo->width;
+	buffer->height = devinfo->height;
+	buffer->pixel_format = fb_info.pixel_format;
+	buffer->size = devinfo->fb_size;
+
+
+	EMGD_TRACE_EXIT;
+	return EMGD_OK;
+
+} /* init_display() */
+
+/** 
+ * Loops through the avaiable displays, invalidating the  associated flip-chains
+ * This function is called from igd_alter_displays so as to resolve any race 
+ * conditions that may occur due to performing a flip during a mode-set.
+ *
+ * @param display     (IN) The display whose flipchains are to be invalidated.
+ */
+static int emgddc_invalidate_flip_chains(int display) {
+
+	emgddc_devinfo_t * devinfo;
+	emgddc_swapchain_t *swap_chain;
+	unsigned long lock_flags;
+	igd_surface_t surf;
+	igd_display_h display_handle;
+	igd_context_t *context;
+	int i;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters:");
+	EMGD_DEBUG("display=0x%1x",display);
+
+	for (i = 0; i < MAX_DISPLAYS; i++) {
+		if (display & (1 << i)) {
+			devinfo = global_devinfo[i];
+
+			if (devinfo == NULL) {
+				EMGD_DEBUG("Skipping NULL display at index %d", i);
+				continue;
+			}
+
+
+			/* Mode changes invalidate flip-able swap chains.  We can't destroy
+			 * them behind the back of PVR services, but we can and should
+			 * ignore all pending and future buffer flips for existing swap
+			 * chains.
+			 * Note: new swap chains will be valid, and be able to perform flips.
+			 */
+			/* Obtain the lock, to hold-off future interrupt handling for a bit */
+			spin_lock_irqsave(&devinfo->swap_chain_lock, lock_flags);
+			swap_chain = devinfo->flipable_swapchains;
+			while (swap_chain != NULL) {
+				swap_chain->valid = EMGD_FALSE;
+
+				flush_flip_queue(swap_chain);
+				swap_chain = swap_chain->next;
+			}
+			/* Now that we've invalidated pending flips, release the lock */
+			spin_unlock_irqrestore(&devinfo->swap_chain_lock, lock_flags);
+
+			/* Reset the frame-buffer information to point to the system
+			 * buffer */
+			context = devinfo->priv->context;
+			display_handle = (devinfo->which_devinfo == 1)?
+								devinfo->priv->secondary:devinfo->priv->primary;
+
+			surf.offset = devinfo->system_buffer.offset;
+			surf.pitch = devinfo->system_buffer.pitch;
+			surf.width = devinfo->system_buffer.width;
+			surf.height = devinfo->system_buffer.height;
+			surf.pixel_format = devinfo->system_buffer.pixel_format;
+			surf.flags = IGD_SURFACE_RENDER | IGD_SURFACE_DISPLAY;
+
+			ret = context->dispatch.set_surface(display_handle,
+					IGD_PRIORITY_NORMAL, IGD_BUFFER_DISPLAY, &surf, NULL, 0);
+
+			if (ret) {
+				EMGD_ERROR("set_surface() returned %d for display at index %d",
+						ret, i);
+			}
+		}
+	}
+
+	EMGD_TRACE_EXIT;
+
+	return 0;
+}
+
+
+/**
+ * [Re-]Initializes the 3DD's display/device-specific values for both devinfo
+ * structures.  This function is called during initializatio time, and whenever
+ * the EMGD driver does a potential mode change, via alter_displays().
+ *
+ * @param dev (IN) The drm_device associated with this driver.
+ * @return emgd_error_t value (e.g. EMGD_OK, EMGD_ERROR_OUT_OF_MEMORY).
+ */
+static int emgddc_reinit_3dd(struct drm_device *dev)
+{
+	emgddc_devinfo_t *devinfo;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+	if (0 == priv->dc) {
+		EMGD_DEBUG("not ready to re-init, because no dc has been set");
+		if (!priv->hal_running) {
+			/* Ensure that devinfo->interrupt_h is NULL: */
+			if (NULL != (devinfo = global_devinfo[0])) {
+				devinfo->interrupt_h = NULL;
+			}
+			if (NULL != (devinfo = global_devinfo[1])) {
+				devinfo->interrupt_h = NULL;
+			}
+		}
+		return EMGD_OK;
+	}
+
+	EMGD_DEBUG("The DC is 0x%08lx", priv->dc);
+	EMGD_DEBUG("IGD_DC_CLONE(priv->dc) is %d", IGD_DC_CLONE(priv->dc));
+	EMGD_DEBUG("IGD_DC_EXTENDED(priv->dc) is %d", IGD_DC_EXTENDED(priv->dc));
+
+	/* Always initialize the primary devinfo: */
+	ret = init_display(global_devinfo[0], priv->primary,
+		priv->primary_port_number);
+	if (ret != EMGD_OK) {
+		return ret;
+	}
+
+	/* Initialize the secondary devinfo if we're in DIH/extended mode: */
+	if (IGD_DC_EXTENDED(priv->dc)) {
+		EMGD_DEBUG("Detected that we're in DIH/EXTENDED mode");
+
+		/* Allocate the devinfo if it hasn't already been allocated: */
+		if (NULL == (devinfo = global_devinfo[1])) {
+			EMGD_DEBUG("Allocating devinfo structure for DIH/EXTENDED mode");
+			devinfo = (emgddc_devinfo_t *) OS_ALLOC(sizeof(emgddc_devinfo_t));
+			if (!devinfo) {
+				EMGD_ERROR_EXIT("Can not allocate emgddc_devinfo_t structure");
+				return EMGD_ERROR_OUT_OF_MEMORY;
+			}
+			OS_MEMSET(devinfo, 0, sizeof(emgddc_devinfo_t));
+
+			/* Perform static/display-independent initialization: */
+			ret = emgddc_init_devinfo(dev, devinfo, 1);
+			if (ret != EMGD_OK) {
+				EMGD_ERROR_EXIT("CAN NOT support DIH/EXTENDED mode!");
+				emgddc_free_a_devinfo(devinfo);
+				return ret;
+			}
+		}
+
+		/* Perform dynamic/display-dependent initialization: */
+		ret = init_display(devinfo, priv->secondary,
+			priv->secondary_port_number);
+		if (ret != EMGD_OK) {
+			EMGD_ERROR_EXIT("CAN NOT support DIH/EXTENDED mode!");
+			emgddc_free_a_devinfo(devinfo);
+			global_devinfo[1] = NULL;
+			return ret;
+		}
+
+		/* Remember the devinfo, for other functions that aren't passed it: */
+		global_devinfo[1] = devinfo;
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return EMGD_OK;
+
+} /* emgddc_reinit_3dd() */
+
+
+/**
+ * Master initialization function.  This is called when the EMGD DRM module
+ * tells PVR services to start, which calls this function.
+ *
+ * @param dev (IN) The drm_device for this driver connection.
+ * @return emgd_error_t value (e.g. EMGD_OK, EMGD_ERROR_OUT_OF_MEMORY).
+ */
+emgd_error_t emgddc_init(struct drm_device *dev)
+{
+	emgddc_devinfo_t *devinfo;
+	drm_emgd_priv_t *priv = dev->dev_private;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+
+
+	/* Exit early if trying to initialize again: */
+	devinfo = global_devinfo[0];
+	if (devinfo != NULL) {
+		EMGD_TRACE_EXIT;
+		return EMGD_OK;
+	}
+
+	/* Always allocate the primary display's devinfo structure: */
+	devinfo = (emgddc_devinfo_t *) OS_ALLOC(sizeof(emgddc_devinfo_t));
+	if (!devinfo) {
+		EMGD_ERROR_EXIT("Can not allocate emgddc_devinfo_t structure");
+		return EMGD_ERROR_OUT_OF_MEMORY;
+	}
+	OS_MEMSET(devinfo, 0, sizeof(emgddc_devinfo_t));
+	global_devinfo[0] = devinfo;
+
+	/* Perform static/display-independent initialization: */
+	ret = emgddc_init_devinfo(dev, devinfo, 0);
+	if (ret != EMGD_OK) {
+		emgddc_free_all_devinfos();
+		return ret;
+	}
+
+	/* Perform dynamic/display-dependent initialization (if we're in
+	 * DIH/Extended mode, the secondary devinfo will also be allocated and
+	 * initialized):
+	 */
+	priv->reinit_3dd = emgddc_reinit_3dd;
+
+	/* Used inside igd_alter_displays */
+	priv->invalidate_flip_chains = emgddc_invalidate_flip_chains;
+
+	ret = emgddc_reinit_3dd(dev);
+	if (ret != EMGD_OK) {
+		emgddc_free_all_devinfos();
+		return ret;
+	}
+
+	/* Remember the devinfo, for other functions that aren't passed it: */
+	global_devinfo[0] = devinfo;
+
+
+	EMGD_TRACE_EXIT;
+	return EMGD_OK;
+
+} /* emgddc_init() */
+
+
+/**
+ * Master de-initialization function.  This is called when the EMGD DRM module
+ * is being unloaded (it tells PVR services to exit, which calls this
+ * function).
+ *
+ * @return emgd_error_t value (e.g. EMGD_OK, EMGD_ERROR_OUT_OF_MEMORY).
+ */
+emgd_error_t emgddc_deinit(void)
+{
+	emgddc_devinfo_t *devinfo;
+	int i;
+	emgd_error_t ret = EMGD_OK;
+
+	EMGD_TRACE_ENTER;
+
+
+	for (i = 0 ; i < 2 ; i++) {
+		devinfo = global_devinfo[i];
+		if (devinfo == NULL) {
+			continue;
+		}
+
+
+		/* Unhook and unregister from PVR services: */
+		if (devinfo->pvr_jtable.pfnPVRSRVRemoveCmdProcList(devinfo->device_id,
+			EMGDDC_COMMAND_COUNT) != PVRSRV_OK) {
+			ret = EMGD_ERROR_GENERIC;
+		}
+		if (devinfo->pvr_jtable.pfnPVRSRVRemoveDCDevice(devinfo->device_id) !=
+			PVRSRV_OK) {
+			ret = EMGD_ERROR_GENERIC;
+		}
+
+
+		/* Delete any/all swap chains now: */
+		/* Note: If we ever support interrupts, there may be a race condition
+		 * of pending flips.  This was placed here, after the PVR services
+		 * thinks the driver has gone away, so that no flips should come by
+		 * this time.
+		 */
+		while (devinfo->flipable_swapchains) {
+			if (PVRSRV_OK != DestroyDCSwapChain(devinfo,
+				devinfo->flipable_swapchains)) {
+				ret = EMGD_ERROR_GENERIC;
+			}
+		}
+		while (devinfo->pixmap_swapchains) {
+			if (PVRSRV_OK != DestroyDCSwapChain(devinfo,
+				devinfo->pixmap_swapchains)) {
+				ret = EMGD_ERROR_GENERIC;
+			}
+		}
+
+
+		emgddc_free_a_devinfo(devinfo);
+		global_devinfo[i] = NULL;
+	}
+
+
+	EMGD_TRACE_EXIT;
+	return ret;
+
+} /* emgddc_deinit() */
+
+
+/*
+ * Potentially perform a mode change.
+ *
+ * If the X server is running, PVR services (including this function) can't
+ * do mode changes.  To avoid having to switch modes, ensure that the
+ * current pixel format and dimension is specified:
+ *
+ * If the X server is NOT running, PVR services (including this function)
+ * can do mode changes.  In this case, if the pixel format and/or
+ * dimensions don't match the current mode, perform a mode change (as long
+ * as valid values are provided):
+ */
+static PVRSRV_ERROR do_mode_change(igd_context_t *context,
+		emgddc_devinfo_t *devinfo,
+		drm_emgd_priv_t *priv,
+		DISPLAY_SURF_ATTRIBUTES *dst_surf_attrib)
+{
+	struct drm_device* drm_dev;
+	int err = PVRSRV_OK;
+
+	drm_dev = devinfo->drm_device;
+
+	if (priv->xserver_running) {
+		if ((dst_surf_attrib->pixelformat != devinfo->pvr_pf) ||
+			(dst_surf_attrib->sDims.ui32ByteStride != devinfo->byte_stride) ||
+			(dst_surf_attrib->sDims.ui32Width != devinfo->width) ||
+			(dst_surf_attrib->sDims.ui32Height != devinfo->height)) {
+			return PVRSRV_ERROR_INVALID_PARAMS;
+		}
+	} else if ((dst_surf_attrib->pixelformat != devinfo->pvr_pf) ||
+			(dst_surf_attrib->sDims.ui32ByteStride != devinfo->byte_stride)||
+			(dst_surf_attrib->sDims.ui32Width != devinfo->width) ||
+			(dst_surf_attrib->sDims.ui32Height != devinfo->height)) {
+		unsigned long emgd_pf = pvr2emgd_pf(dst_surf_attrib->pixelformat);
+		igd_display_info_t *mode_list = NULL;
+		igd_display_info_t *mode = NULL;
+		igd_display_info_t *desired_mode = NULL;
+		int mode_flags = IGD_QUERY_LIVE_MODES;
+		unsigned long byte_stride;
+		igd_framebuffer_info_t primary_fb_info;
+		igd_framebuffer_info_t secondary_fb_info;
+		igd_display_h primary;
+		igd_display_h secondary;
+
+		printk("[EMGD] A mode change is requested.  The following new values\n"
+			"[EMGD] will be checked, and if good, take effect:\n");
+		printk("[EMGD]   pixel format = %u (PVR) = 0x%08lx (EMGD)\n",
+			dst_surf_attrib->pixelformat, emgd_pf);
+		printk("[EMGD]   width = %lu, height = %lu\n",
+			dst_surf_attrib->sDims.ui32Width,
+			dst_surf_attrib->sDims.ui32Height);
+		printk("[EMGD]   stride = %lu\n",dst_surf_attrib->sDims.ui32ByteStride);
+
+
+		/* Check the pixel format: */
+		if (IGD_PF_UNKNOWN == emgd_pf) {
+			printk(KERN_ERR "Unknown pixel format %u\n",
+				dst_surf_attrib->pixelformat);
+			return PVRSRV_ERROR_INVALID_PARAMS;
+		}
+
+
+		/* Check the width, height, and stride: */
+		EMGD_DEBUG("Calling query_mode_list()");
+		err = context->dispatch.query_mode_list(context, priv->dc,
+			&mode_list, mode_flags);
+		if (err) {
+			printk(KERN_ERR "The query_mode_list() function returned %d.", err);
+			return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+		}
+		EMGD_DEBUG("Comparing the mode list with the desired width, height, "
+				"and stride...");
+		mode = mode_list;
+		while (mode && (mode->width != IGD_TIMING_TABLE_END)) {
+			byte_stride =  IGD_PF_PIXEL_BYTES(emgd_pf, mode->width);
+			EMGD_DEBUG(" ... Found a mode with width=%d, height=%d, "
+					"refresh=%d;", mode->width, mode->height, mode->refresh);
+			if ((mode->width == dst_surf_attrib->sDims.ui32Width) &&
+				(mode->height == dst_surf_attrib->sDims.ui32Height) &&
+				(byte_stride == dst_surf_attrib->sDims.ui32ByteStride)) {
+				EMGD_DEBUG("     ... This mode is a match!");
+				desired_mode = mode;
+				break;
+			}
+			mode++;
+		}
+		if (NULL == desired_mode) {
+			printk(KERN_ERR "No mode matching the desired width (%lu), height "
+				"(%lu), and stride (%lu) was found.",
+				dst_surf_attrib->sDims.ui32Width,
+				dst_surf_attrib->sDims.ui32Height,
+				dst_surf_attrib->sDims.ui32ByteStride);
+			return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+		} else {
+			/* Must set this in order to get the timings setup: */
+			desired_mode->flags |= IGD_DISPLAY_ENABLE;
+		}
+
+
+		/* Make the mode change by calling alter_displays(): */
+		primary_fb_info.width = desired_mode->width;
+		primary_fb_info.height = desired_mode->height;
+		primary_fb_info.pixel_format = emgd_pf;
+		primary_fb_info.flags = 0;
+		primary_fb_info.allocated = 0;
+		memcpy(&secondary_fb_info, &primary_fb_info,
+				sizeof(igd_framebuffer_info_t));
+
+		EMGD_DEBUG("Calling alter_displays()");
+		err = context->dispatch.alter_displays(context,
+			&primary, desired_mode, &primary_fb_info,
+			&secondary, desired_mode, &secondary_fb_info, priv->dc, 0);
+		if (err) {
+			printk(KERN_ERR "The alter_displays() function returned %d.", err);
+			return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+		}
+
+
+		/* Update the private copy, like emgd_alter_displays() would do: */
+		priv->primary = primary;
+		priv->secondary = secondary;
+		priv->primary_port_number = (priv->dc & 0xf0) >> 4;
+		priv->secondary_port_number = (priv->dc & 0xf00000) >> 20;
+
+
+		/* Re-initialize the display values: */
+		err = priv->reinit_3dd(drm_dev);
+		if (err != EMGD_OK) {
+			printk(KERN_ERR "The reinit_3dd() function returned %d.", err);
+			return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+		}
+	} /* end of mode-change code */
+
+	return err;
+} /* do_mode_change() */
diff --git a/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.h b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.h
new file mode 100644
index 0000000..8ea6d33
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc.h
@@ -0,0 +1,342 @@
+/**********************************************************************
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef EMGD_DC_H
+#define EMGD_DC_H
+
+#include <drm/drmP.h>
+#include "memory.h"
+#include "io.h"
+#include "emgd_shared.h"
+
+
+#define EMGDDC_MAXFORMATS 20
+#define EMGDDC_MAXDIMS 20
+
+
+#define DISPLAY_DEVICE_NAME "Intel EMGD Display Driver"
+#define	DRVNAME	"emgd_dc"
+#define	DEVNAME	DRVNAME
+#define	DRIVER_PREFIX DRVNAME
+
+
+#define	EMGD_TRACE_STUB \
+	EMGD_DEBUG("Inside the stubbed %s() function", __FUNCTION__)
+
+
+typedef struct _emgddc_devinfo  emgddc_devinfo_t;
+
+
+typedef enum tag_emgd_bool
+{
+	EMGD_FALSE = 0,
+	EMGD_TRUE  = 1,
+} emgd_bool;
+
+
+/**
+ * A pair of equivalent pixel formats, one in the EMGD format, one in the PVR
+ * format.  This structure is used to translate between the two formats.
+ */
+typedef struct _emgddc_pixelformat_translator
+{
+	/** An EMGD-formatted pixel format. */
+	unsigned long emgd_pf;
+
+	/** A PVR-formatted pixel format. */
+	PVRSRV_PIXEL_FORMAT pvr_pf;
+}  emgddc_pixelformat_translator_t;
+
+
+/** Information about a given swap chain buffer. */
+typedef struct _emgddc_buffer
+{
+	/** A copy of the drm_emgd_private associated with this buffer. */
+	drm_emgd_priv_t *priv;
+
+	/** The GMM offset of this buffer's memory. */
+	unsigned long offset;
+
+	/** This buffer's EMGD-formatted pixel format. */
+	unsigned long pixel_format;
+
+	/** This buffer's width. */
+	unsigned long width;
+
+	/** This buffer's height. */
+	unsigned long height;
+
+	/**
+	 * This buffer's stride (i.e. number of bytes between the same point on
+	 * successive scanlines).
+	 */
+	unsigned long pitch;
+
+	/** This buffer's size. */
+	unsigned long size;
+
+	/** Is this buffer the framebuffer (a.k.a. system buffer). */
+	int is_fb;
+
+	/**
+	 * The memory-mapped virtual address of the buffer (obtained from
+	 * gmm_map())
+	 */
+	IMG_CPU_VIRTADDR virt_addr;
+
+	/** The PVR sync data for this buffer */
+	PVRSRV_SYNC_DATA *sync_data;
+
+	unsigned long is_contiguous;
+
+	/**
+	 * The PVR next buffer in the swap chain (the last buffer points to the
+	 * first buffer
+	 */
+	struct _emgddc_buffer *next;
+} emgddc_buffer_t;
+
+
+/** Information for queueing a flip for a given swap chain buffer. */
+typedef struct _emgddc_flip_queue_item
+{
+	/**
+	 * A handle to a PVR services data structure that must be passed to
+	 * PVRSRVCmdComplete(), in order to "complete" a flip (i.e. when PVR
+	 * services knows that the 3DD has caused the flip to occur, and as such,
+	 * when its queue-processing code can call emgddc_process_flip() to
+	 * start/queue another flip).  "Completing" a flip does not mean that the
+	 * flip has reached the end of its swap_interval and been "cleared."
+	 */
+	IMG_HANDLE cmd_complete;
+
+	/**
+	 * The number of VBlanks after a flip occurs, before the next flip can
+	 * start.
+	 */
+	unsigned long swap_interval;
+
+	/** Whether the corresponding buffer has a flip in progress. */
+	emgd_bool valid;
+
+	/**
+	 * Whether the corresponding buffer completed a flip.  This can only be
+	 * EMGD_TRUE if valid is also EMGD_TRUE.
+	 */
+	emgd_bool flipped;
+
+	/**
+	 * Whether the corresponding buffer told PVR services that it completed a
+	 * flip.  This can only be EMGD_TRUE if flipped is also EMGD_TRUE.
+	 */
+	emgd_bool cmd_completed;
+
+	/** Which buffer is associated with this flip_item. */
+	emgddc_buffer_t *buffer;
+
+} emgddc_flip_queue_item_t;
+
+
+/** Information about a given swap chain. */
+typedef struct _emgddc_swapchain
+{
+	/**
+	 * Pointer to the parent devinfo, so that emgddc_flip() can lookup which
+	 * device (and therefore, which igd_display_t) to use.
+	 */
+	emgddc_devinfo_t *devinfo;
+
+	/** Whether this swap chain is valid for buffer flipping. */
+	emgd_bool valid;
+
+	/** Number of buffers in this swap chain. */
+	unsigned long buffer_count;
+
+	/** An array of the buffers in this swap chain. */
+	emgddc_buffer_t *buffers;
+
+	/** An array of the structures that keep track of queued flips.  Each flip
+	 * item corresponds to the identically-numbered buffer in this swap chain.
+	 */
+	emgddc_flip_queue_item_t	*flip_queue;
+
+	/** Index into both the buffers and flip_queue arrays.  Used when queueing
+	 * (or inserting) flips.  Is incremented until it gets too large, and then
+	 * is zero'd-out.
+	 */
+	unsigned long insert_index;
+
+	/** Index into both the buffers and flip_queue arrays.  Used when flushing
+	 * (or removing from) the flip queue.  Is incremented until it gets too
+	 * large, and then is zero'd-out.
+	 */
+	unsigned long remove_index;
+
+#ifdef SUPPORT_FB_EVENTS
+	emgd_bool blanked;
+#endif /* SUPPORT_FB_EVENTS */
+
+	/** Type of swap chain */
+	unsigned long flags;
+
+	/** A copy of emgddc_devinfo_t.pvr_jtable. */
+	PVRSRV_DC_DISP2SRV_KMJTABLE	*pvr_jtable;
+
+	/** Next swap chain in the list */
+	struct _emgddc_swapchain *next;
+} emgddc_swapchain_t;
+
+
+/** Information about the device. */
+struct _emgddc_devinfo
+{
+	/*
+	 * The following values are display-dependent, and are dynamically
+	 * re-initialized after an EMGD alter_displays() call:
+	 */
+
+	/** The size (in bytes) of the framebuffer and future swap chain buffers. */
+	unsigned long fb_size;
+
+	/** The width (in pixels) of the framebuffer. */
+	unsigned long width;
+
+	/** The height (in pixels) of the framebuffer. */
+	unsigned long height;
+
+	/** The stride (in bytes) from one row of the framebuffer to another. */
+	unsigned long byte_stride;
+
+	/* The number of dimensions for the primary display: */
+	unsigned long num_dims;
+
+	/* The supported dimensions for the primary display: */
+	DISPLAY_DIMS display_dim_list[EMGDDC_MAXDIMS];
+
+	/** The one EMGD-formatted pixel format (before translation) being used. */
+	unsigned long emgd_pf;
+
+	/** The one PVR-formatted pixel format (after translation) being used. */
+	PVRSRV_PIXEL_FORMAT pvr_pf;
+
+	/** The number of pixel formats for the primary display. */
+	unsigned long num_formats;
+
+	/** The supported PVR pixel formats for the primary display. */
+	DISPLAY_FORMAT display_format_list[EMGDDC_MAXFORMATS];
+
+	/** The frame buffer (a.k.a. "system buffer" or "front buffer"). */
+	emgddc_buffer_t system_buffer;
+
+
+	/*
+	 * The following values are display-independent, are statically
+	 * initialized at start-up time, and some may be altered during run time:
+	 */
+
+	/** 0 for the primary devinfo, 1 for the secondary/DIH mode devinfo */
+	int which_devinfo;
+
+	/** A copy of the drm_emgd_private associated with this devinfo. */
+	drm_emgd_priv_t *priv;
+
+	/** Private copy of the drm_device associated with this devinfo. */
+	struct drm_device *drm_device;
+
+	/**
+	 * The first of potentially multiple swap chains that support full-screen
+	 * buffer flipping.
+	 */
+	emgddc_swapchain_t *flipable_swapchains;
+
+	/** The first of potentially many swap chains that support pixmaps. */
+	emgddc_swapchain_t *pixmap_swapchains;
+
+	/** Counter to provide unique IDs to each swap chain. */
+	unsigned long swap_chain_id_counter;
+
+	/** Static information about swap chains, plus the driver's string name. */
+	DISPLAY_INFO display_info;
+
+	/** HAL handle associated with this devinfo for dealing with interrupts. */
+	emgd_vblank_callback_h interrupt_h;
+
+	/** Handle very rare case of not being able to [re-]enable interrupts. */
+	emgd_bool flipping_disabled;
+
+	/** If EMGD_TRUE, don't queue buffer flips, but flip/complete right away. */
+	emgd_bool flush_commands;
+
+	/**
+	 * A counter so that the state of flush_commands must be set to EMGD_TRUE
+	 * and EMGD_FALSE an equal number of times before the state actually
+	 * changes.
+	 */
+	unsigned long set_flush_state_ref_count;
+
+	/**
+	 * A spinlock to prevent contention between regular code and
+	 * interrupt-handling code.
+	 */
+	spinlock_t swap_chain_lock;
+
+#ifdef SUPPORT_FB_EVENTS
+	struct notifier_block lin_notif_block;
+#endif /* SUPPORT_FB_EVENTS */
+
+	/** PVR services functions. */
+	PVRSRV_DC_DISP2SRV_KMJTABLE	pvr_jtable;
+
+	/** Functions we provide to PVR services. */
+	PVRSRV_DC_SRV2DISP_KMJTABLE	dc_jtable;
+
+	/** This device's numeric ID, obtained when registering with PVR services */
+	unsigned long device_id;
+
+};
+
+
+typedef enum _emgd_error
+{
+	EMGD_OK                             =  0,
+	EMGD_ERROR_GENERIC                  =  1,
+	EMGD_ERROR_OUT_OF_MEMORY            =  2,
+	EMGD_ERROR_TOO_FEW_BUFFERS          =  3,
+	EMGD_ERROR_INVALID_PARAMS           =  4,
+	EMGD_ERROR_INIT_FAILURE             =  5,
+	EMGD_ERROR_CANT_REGISTER_CALLBACK   =  6,
+	EMGD_ERROR_INVALID_DEVICE           =  7,
+	EMGD_ERROR_DEVICE_REGISTER_FAILED   =  8
+} emgd_error_t;
+
+
+#ifndef UNREFERENCED_PARAMETER
+#define	UNREFERENCED_PARAMETER(param) (param) = (param)
+#endif
+
+
+emgd_error_t emgddc_init(struct drm_device * dev);
+emgd_error_t emgddc_deinit(void);
+
+void emgddc_flip(emgddc_swapchain_t *swap_chain, emgddc_buffer_t *buffer);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc_linux.c b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc_linux.c
new file mode 100644
index 0000000..c8d8cdf
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/3rdparty/emgd_displayclass/emgd_dc_linux.c
@@ -0,0 +1,153 @@
+/**********************************************************************
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ ******************************************************************************/
+#define MODULE_NAME hal.pvr3dd
+
+#include "drm_emgd_private.h"
+
+#include "img_defs.h"
+#include "servicesext.h"
+#include "kerneldisplay.h"
+#include "emgd_dc.h"
+
+
+#if !defined(SUPPORT_DRI_DRM)
+#error "SUPPORT_DRI_DRM must be set"
+#endif
+
+
+/* The following macros exist to help the PVR services code name/find and call
+ * the Init and Cleanup functions below.  The DISPLAY_CONTROLLER macro must be
+ * set in the build, so that the PVR service code can use the same magic to
+ * discover the name of the Init and Cleanup functions for this driver.
+ */
+#if !defined(DISPLAY_CONTROLLER)
+#define DISPLAY_CONTROLLER emgd_dc
+#endif
+#define MAKENAME_HELPER(x, y) x ## y
+#define	MAKENAME2(x, y) MAKENAME_HELPER(x, y)
+#define	MAKENAME(x) MAKENAME2(DISPLAY_CONTROLLER, x)
+
+
+/* The following tells GCC to not warn about ununsed functions: */
+#define unref__ __attribute__ ((unused))
+
+
+/**
+ * Function that initializes and starts this 3rd-party display driver.  This is
+ * called by the PVR services when it starts.
+ *
+ * @param dev (IN) The drm_device associated with this driver.
+ */
+int MAKENAME(_Init)(struct drm_device unref__ *dev)
+{
+	EMGD_TRACE_ENTER;
+
+	if (emgddc_init(dev) != EMGD_OK) {
+		EMGD_ERROR_EXIT(DRVNAME " init failed (" DISPLAY_DEVICE_NAME ")\n");
+		return -ENODEV;
+	}
+
+	EMGD_TRACE_EXIT;
+	return 0;
+}
+
+
+/**
+ * Function that shuts-down and de-initializes this 3rd-party display driver.
+ * This is called by the PVR services when it ends.
+ *
+ * @param dev (IN) The drm_device associated with this driver.
+ */
+void MAKENAME(_Cleanup)(struct drm_device unref__ *dev)
+{
+	EMGD_TRACE_ENTER;
+
+	if (emgddc_deinit() != EMGD_OK) {
+		EMGD_ERROR_EXIT(DRVNAME " de-init failed (" DISPLAY_DEVICE_NAME ")\n");
+	}
+
+	EMGD_TRACE_EXIT;
+}
+
+
+/**
+ * Set the display to the surface specified by buffer.
+ *
+ * @param swap_chain (IN) Pointer to the swap chain associated with buffer.
+ * @param buffer (IN) Pointer to the buffer to display.
+ */
+void emgddc_flip(emgddc_swapchain_t *swap_chain, emgddc_buffer_t *buffer)
+{
+	drm_emgd_priv_t *priv = buffer->priv;
+	igd_context_t *context = priv->context;
+	igd_surface_t surf;
+	int ret;
+
+	EMGD_TRACE_ENTER;
+	EMGD_DEBUG("Parameters: swap_chain=0x%p", swap_chain);
+	EMGD_DEBUG("  buffer=0x%p, *buffer->offset=0x%08lx",
+		buffer, buffer->offset);
+
+	if (EMGD_FALSE == swap_chain->valid) {
+		EMGD_DEBUG("Not flipping--swap chain invalidated by a mode change.");
+		EMGD_TRACE_EXIT;
+		return;
+	}
+
+	surf.offset = buffer->offset;
+	surf.pitch = buffer->pitch;
+	surf.width = buffer->width;
+	surf.height = buffer->height;
+	surf.pixel_format = buffer->pixel_format;
+	surf.flags = IGD_SURFACE_RENDER | IGD_SURFACE_DISPLAY;
+
+	/* Flip the primary surface.  Select a different EMGD display depending on
+	 * the DC & devinfo:
+	 */
+	if (IGD_DC_EXTENDED(priv->dc) &&
+		(swap_chain->devinfo->which_devinfo == 1))  {
+		ret = context->dispatch.set_surface(priv->secondary,
+			IGD_PRIORITY_NORMAL, IGD_BUFFER_DISPLAY, &surf, NULL, 0);
+		if (ret != 0) {
+			printk(KERN_ERR "%s: set_surface() returned %d", __FUNCTION__, ret);
+		}
+	} else {
+		ret = context->dispatch.set_surface(priv->primary,
+			IGD_PRIORITY_NORMAL, IGD_BUFFER_DISPLAY, &surf, NULL, 0);
+		if (ret != 0) {
+			printk(KERN_ERR "%s: set_surface() returned %d", __FUNCTION__, ret);
+		}
+	}
+
+	/* Flip the secondary surface: */
+	if (IGD_DC_CLONE(priv->dc)) {
+		/* If in clone mode, flip the other pipe too: */
+		ret = context->dispatch.set_surface(priv->secondary,
+			IGD_PRIORITY_NORMAL, IGD_BUFFER_DISPLAY, &surf, NULL, 0);
+		if (ret != 0) {
+			printk(KERN_ERR "%s: set_surface() returned %d", __FUNCTION__, ret);
+		}
+	}
+
+
+	EMGD_TRACE_EXIT;
+} /* emgddc_flip() */
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/env/linux/pvr_drm_shared.h b/drivers/gpu/drm/emgd/pvr/services4/include/env/linux/pvr_drm_shared.h
new file mode 100644
index 0000000..7f0f445
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/env/linux/pvr_drm_shared.h
@@ -0,0 +1,59 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__PVR_DRM_SHARED_H__)
+#define __PVR_DRM_SHARED_H__
+
+#if defined(SUPPORT_DRI_DRM)
+
+#if defined(SUPPORT_DRI_DRM_EXT)
+#define PVR_DRM_SRVKM_CMD	DRM_PVR_RESERVED1
+#define	PVR_DRM_DISP_CMD	DRM_PVR_RESERVED2
+#define	PVR_DRM_BC_CMD		DRM_PVR_RESERVED3
+#define PVR_DRM_IS_MASTER_CMD	DRM_PVR_RESERVED4
+#define PVR_DRM_UNPRIV_CMD	DRM_PVR_RESERVED5
+#define PVR_DRM_DBGDRV_CMD	DRM_PVR_RESERVED6
+#else
+#define PVR_DRM_SRVKM_CMD	0
+#define	PVR_DRM_DISP_CMD	1
+#define	PVR_DRM_BC_CMD		2
+#define PVR_DRM_IS_MASTER_CMD	3
+#define PVR_DRM_UNPRIV_CMD	4
+#define PVR_DRM_DBGDRV_CMD	5
+#endif
+
+#define	PVR_DRM_UNPRIV_INIT_SUCCESFUL	0
+#define	PVR_DRM_UNPRIV_BUSID_TYPE	1
+#define	PVR_DRM_UNPRIV_BUSID_FIELD	2
+
+#define	PVR_DRM_BUS_TYPE_PCI		0
+
+#define	PVR_DRM_PCI_DOMAIN		0
+#define	PVR_DRM_PCI_BUS			1
+#define	PVR_DRM_PCI_DEV			2
+#define	PVR_DRM_PCI_FUNC		3
+
+#endif
+
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/kernelbuffer.h b/drivers/gpu/drm/emgd/pvr/services4/include/kernelbuffer.h
new file mode 100644
index 0000000..4bf59b1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/kernelbuffer.h
@@ -0,0 +1,57 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__KERNELBUFFER_H__)
+#define __KERNELBUFFER_H__
+
+typedef PVRSRV_ERROR (*PFN_OPEN_BC_DEVICE)(IMG_UINT32, IMG_HANDLE*);
+typedef PVRSRV_ERROR (*PFN_CLOSE_BC_DEVICE)(IMG_UINT32, IMG_HANDLE);
+typedef PVRSRV_ERROR (*PFN_GET_BC_INFO)(IMG_HANDLE, BUFFER_INFO*);
+typedef PVRSRV_ERROR (*PFN_GET_BC_BUFFER)(IMG_HANDLE, IMG_UINT32, PVRSRV_SYNC_DATA*, IMG_HANDLE*);
+
+typedef struct PVRSRV_BC_SRV2BUFFER_KMJTABLE_TAG
+{
+	IMG_UINT32							ui32TableSize;
+	PFN_OPEN_BC_DEVICE					pfnOpenBCDevice;
+	PFN_CLOSE_BC_DEVICE					pfnCloseBCDevice;
+	PFN_GET_BC_INFO						pfnGetBCInfo;
+	PFN_GET_BC_BUFFER					pfnGetBCBuffer;
+	PFN_GET_BUFFER_ADDR					pfnGetBufferAddr;
+	PFN_GET_BUFFER_ID_FROM_TAG			pfnGetBufferIdFromTag;
+
+} PVRSRV_BC_SRV2BUFFER_KMJTABLE;
+
+
+typedef PVRSRV_ERROR (*PFN_BC_REGISTER_BUFFER_DEV)(PVRSRV_BC_SRV2BUFFER_KMJTABLE*, IMG_UINT32*);
+typedef PVRSRV_ERROR (*PFN_BC_REMOVE_BUFFER_DEV)(IMG_UINT32);
+
+typedef struct PVRSRV_BC_BUFFER2SRV_KMJTABLE_TAG
+{
+	IMG_UINT32							ui32TableSize;
+	PFN_BC_REGISTER_BUFFER_DEV			pfnPVRSRVRegisterBCDevice;
+	PFN_BC_REMOVE_BUFFER_DEV			pfnPVRSRVRemoveBCDevice;
+
+} PVRSRV_BC_BUFFER2SRV_KMJTABLE, *PPVRSRV_BC_BUFFER2SRV_KMJTABLE;
+
+typedef IMG_BOOL (*PFN_BC_GET_PVRJTABLE) (PPVRSRV_BC_BUFFER2SRV_KMJTABLE);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/kerneldisplay.h b/drivers/gpu/drm/emgd/pvr/services4/include/kerneldisplay.h
new file mode 100644
index 0000000..b14c131
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/kerneldisplay.h
@@ -0,0 +1,152 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__KERNELDISPLAY_H__)
+#define __KERNELDISPLAY_H__
+
+typedef PVRSRV_ERROR (*PFN_OPEN_DC_DEVICE)(IMG_UINT32, IMG_HANDLE*, PVRSRV_SYNC_DATA*);
+typedef PVRSRV_ERROR (*PFN_CLOSE_DC_DEVICE)(IMG_HANDLE);
+typedef PVRSRV_ERROR (*PFN_ENUM_DC_FORMATS)(IMG_HANDLE, IMG_UINT32*, DISPLAY_FORMAT*);
+typedef PVRSRV_ERROR (*PFN_ENUM_DC_DIMS)(IMG_HANDLE,
+										 DISPLAY_FORMAT*,
+										 IMG_UINT32*,
+										 DISPLAY_DIMS*);
+typedef PVRSRV_ERROR (*PFN_GET_DC_SYSTEMBUFFER)(IMG_HANDLE, IMG_HANDLE*);
+typedef PVRSRV_ERROR (*PFN_GET_DC_INFO)(IMG_HANDLE, DISPLAY_INFO*);
+typedef PVRSRV_ERROR (*PFN_CREATE_DC_SWAPCHAIN)(IMG_HANDLE,
+												IMG_UINT32,
+												DISPLAY_SURF_ATTRIBUTES*,
+												DISPLAY_SURF_ATTRIBUTES*,
+												IMG_UINT32,
+												PVRSRV_SYNC_DATA**,
+												IMG_UINT32,
+												IMG_HANDLE*,
+												IMG_UINT32*);
+typedef PVRSRV_ERROR (*PFN_DESTROY_DC_SWAPCHAIN)(IMG_HANDLE,
+												 IMG_HANDLE);
+typedef PVRSRV_ERROR (*PFN_SET_DC_DSTRECT)(IMG_HANDLE, IMG_HANDLE, IMG_RECT*);
+typedef PVRSRV_ERROR (*PFN_SET_DC_SRCRECT)(IMG_HANDLE, IMG_HANDLE, IMG_RECT*);
+typedef PVRSRV_ERROR (*PFN_SET_DC_DSTCK)(IMG_HANDLE, IMG_HANDLE, IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_SET_DC_SRCCK)(IMG_HANDLE, IMG_HANDLE, IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_GET_DC_BUFFERS)(IMG_HANDLE,
+										   IMG_HANDLE,
+										   IMG_UINT32*,
+										   IMG_HANDLE*);
+typedef PVRSRV_ERROR (*PFN_SWAP_TO_DC_BUFFER)(IMG_HANDLE,
+											  IMG_HANDLE,
+											  IMG_UINT32,
+											  IMG_HANDLE,
+											  IMG_UINT32,
+											  IMG_RECT*);
+typedef PVRSRV_ERROR (*PFN_SWAP_TO_DC_SYSTEM)(IMG_HANDLE, IMG_HANDLE);
+typedef IMG_VOID (*PFN_QUERY_SWAP_COMMAND_ID)(IMG_HANDLE, IMG_HANDLE, IMG_HANDLE, IMG_HANDLE, IMG_UINT16*, IMG_BOOL*);
+typedef IMG_VOID (*PFN_SET_DC_STATE)(IMG_HANDLE, IMG_UINT32);
+
+typedef struct PVRSRV_DC_SRV2DISP_KMJTABLE_TAG
+{
+	IMG_UINT32						ui32TableSize;
+	PFN_OPEN_DC_DEVICE				pfnOpenDCDevice;
+	PFN_CLOSE_DC_DEVICE				pfnCloseDCDevice;
+	PFN_ENUM_DC_FORMATS				pfnEnumDCFormats;
+	PFN_ENUM_DC_DIMS				pfnEnumDCDims;
+	PFN_GET_DC_SYSTEMBUFFER			pfnGetDCSystemBuffer;
+	PFN_GET_DC_INFO					pfnGetDCInfo;
+	PFN_GET_BUFFER_ADDR				pfnGetBufferAddr;
+	PFN_CREATE_DC_SWAPCHAIN			pfnCreateDCSwapChain;
+	PFN_DESTROY_DC_SWAPCHAIN		pfnDestroyDCSwapChain;
+	PFN_SET_DC_DSTRECT				pfnSetDCDstRect;
+	PFN_SET_DC_SRCRECT				pfnSetDCSrcRect;
+	PFN_SET_DC_DSTCK				pfnSetDCDstColourKey;
+	PFN_SET_DC_SRCCK				pfnSetDCSrcColourKey;
+	PFN_GET_DC_BUFFERS				pfnGetDCBuffers;
+	PFN_SWAP_TO_DC_BUFFER			pfnSwapToDCBuffer;
+	PFN_SWAP_TO_DC_SYSTEM			pfnSwapToDCSystem;
+	PFN_SET_DC_STATE				pfnSetDCState;
+	PFN_QUERY_SWAP_COMMAND_ID		pfnQuerySwapCommandID;
+
+} PVRSRV_DC_SRV2DISP_KMJTABLE;
+
+typedef IMG_BOOL (*PFN_ISR_HANDLER)(IMG_VOID*);
+
+typedef PVRSRV_ERROR (*PFN_DC_REGISTER_DISPLAY_DEV)(PVRSRV_DC_SRV2DISP_KMJTABLE*, IMG_UINT32*);
+typedef PVRSRV_ERROR (*PFN_DC_REMOVE_DISPLAY_DEV)(IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_DC_OEM_FUNCTION)(IMG_UINT32, IMG_VOID*, IMG_UINT32, IMG_VOID*, IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_DC_REGISTER_COMMANDPROCLIST)(IMG_UINT32, PPFN_CMD_PROC,IMG_UINT32[][2], IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_DC_REMOVE_COMMANDPROCLIST)(IMG_UINT32, IMG_UINT32);
+typedef IMG_VOID (*PFN_DC_CMD_COMPLETE)(IMG_HANDLE, IMG_BOOL);
+typedef PVRSRV_ERROR (*PFN_DC_REGISTER_SYS_ISR)(PFN_ISR_HANDLER, IMG_VOID*, IMG_UINT32, IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_DC_REGISTER_POWER)(IMG_UINT32, PFN_PRE_POWER, PFN_POST_POWER,
+											  PFN_PRE_CLOCKSPEED_CHANGE, PFN_POST_CLOCKSPEED_CHANGE,
+											  IMG_HANDLE, PVRSRV_DEV_POWER_STATE, PVRSRV_DEV_POWER_STATE);
+
+typedef struct PVRSRV_DC_DISP2SRV_KMJTABLE_TAG
+{
+	IMG_UINT32						ui32TableSize;
+	PFN_DC_REGISTER_DISPLAY_DEV		pfnPVRSRVRegisterDCDevice;
+	PFN_DC_REMOVE_DISPLAY_DEV		pfnPVRSRVRemoveDCDevice;
+	PFN_DC_OEM_FUNCTION				pfnPVRSRVOEMFunction;
+	PFN_DC_REGISTER_COMMANDPROCLIST	pfnPVRSRVRegisterCmdProcList;
+	PFN_DC_REMOVE_COMMANDPROCLIST	pfnPVRSRVRemoveCmdProcList;
+	PFN_DC_CMD_COMPLETE				pfnPVRSRVCmdComplete;
+	PFN_DC_REGISTER_SYS_ISR			pfnPVRSRVRegisterSystemISRHandler;
+	PFN_DC_REGISTER_POWER			pfnPVRSRVRegisterPowerDevice;
+	PFN_DC_CMD_COMPLETE				pfnPVRSRVFreeCmdCompletePacket;
+} PVRSRV_DC_DISP2SRV_KMJTABLE, *PPVRSRV_DC_DISP2SRV_KMJTABLE;
+
+
+typedef struct DISPLAYCLASS_FLIP_COMMAND_TAG
+{
+
+	IMG_HANDLE hExtDevice;
+
+
+	IMG_HANDLE hExtSwapChain;
+
+
+	IMG_HANDLE hExtBuffer;
+
+
+	IMG_HANDLE hPrivateTag;
+
+
+	IMG_UINT32 ui32ClipRectCount;
+
+
+	IMG_RECT *psClipRect;
+
+
+	IMG_UINT32	ui32SwapInterval;
+
+} DISPLAYCLASS_FLIP_COMMAND;
+
+#define DC_FLIP_COMMAND		0
+
+#define DC_STATE_NO_FLUSH_COMMANDS		0
+#define DC_STATE_FLUSH_COMMANDS			1
+
+
+typedef IMG_BOOL (*PFN_DC_GET_PVRJTABLE)(PPVRSRV_DC_DISP2SRV_KMJTABLE);
+
+
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge.h b/drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge.h
new file mode 100644
index 0000000..9e6004ee
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge.h
@@ -0,0 +1,1380 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __PVR_BRIDGE_H__
+#define __PVR_BRIDGE_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "servicesint.h"
+
+#ifdef __linux__
+
+		#include <linux/ioctl.h>
+
+    #define PVRSRV_IOC_GID      'g'
+    #define PVRSRV_IO(INDEX)    _IO(PVRSRV_IOC_GID, INDEX, PVRSRV_BRIDGE_PACKAGE)
+    #define PVRSRV_IOW(INDEX)   _IOW(PVRSRV_IOC_GID, INDEX, PVRSRV_BRIDGE_PACKAGE)
+    #define PVRSRV_IOR(INDEX)   _IOR(PVRSRV_IOC_GID, INDEX, PVRSRV_BRIDGE_PACKAGE)
+    #define PVRSRV_IOWR(INDEX)  _IOWR(PVRSRV_IOC_GID, INDEX, PVRSRV_BRIDGE_PACKAGE)
+
+#else
+
+			#error Unknown platform: Cannot define ioctls
+
+	#define PVRSRV_IO(INDEX)    (PVRSRV_IOC_GID + INDEX)
+	#define PVRSRV_IOW(INDEX)   (PVRSRV_IOC_GID + INDEX)
+	#define PVRSRV_IOR(INDEX)   (PVRSRV_IOC_GID + INDEX)
+	#define PVRSRV_IOWR(INDEX)  (PVRSRV_IOC_GID + INDEX)
+
+	#define PVRSRV_BRIDGE_BASE                  PVRSRV_IOC_GID
+#endif
+
+
+#define PVRSRV_BRIDGE_CORE_CMD_FIRST			0UL
+#define PVRSRV_BRIDGE_ENUM_DEVICES				PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_ACQUIRE_DEVICEINFO		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_RELEASE_DEVICEINFO		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_CREATE_DEVMEMCONTEXT		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+3)
+#define PVRSRV_BRIDGE_DESTROY_DEVMEMCONTEXT		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+4)
+#define PVRSRV_BRIDGE_GET_DEVMEM_HEAPINFO		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+5)
+#define PVRSRV_BRIDGE_ALLOC_DEVICEMEM			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+6)
+#define PVRSRV_BRIDGE_FREE_DEVICEMEM			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+7)
+#define PVRSRV_BRIDGE_GETFREE_DEVICEMEM			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+8)
+#define PVRSRV_BRIDGE_CREATE_COMMANDQUEUE		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+9)
+#define PVRSRV_BRIDGE_DESTROY_COMMANDQUEUE		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+10)
+#define	PVRSRV_BRIDGE_MHANDLE_TO_MMAP_DATA           PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+11)
+#define PVRSRV_BRIDGE_CONNECT_SERVICES			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+12)
+#define PVRSRV_BRIDGE_DISCONNECT_SERVICES		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+13)
+#define PVRSRV_BRIDGE_WRAP_DEVICE_MEM			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+14)
+#define PVRSRV_BRIDGE_GET_DEVICEMEMINFO			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+15)
+#define PVRSRV_BRIDGE_RESERVE_DEV_VIRTMEM		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+16)
+#define PVRSRV_BRIDGE_FREE_DEV_VIRTMEM			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+17)
+#define PVRSRV_BRIDGE_MAP_EXT_MEMORY			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+18)
+#define PVRSRV_BRIDGE_UNMAP_EXT_MEMORY			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+19)
+#define PVRSRV_BRIDGE_MAP_DEV_MEMORY			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+20)
+#define PVRSRV_BRIDGE_UNMAP_DEV_MEMORY			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+21)
+#define PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY	PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+22)
+#define PVRSRV_BRIDGE_UNMAP_DEVICECLASS_MEMORY	PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+23)
+#define PVRSRV_BRIDGE_MAP_MEM_INFO_TO_USER		PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+24)
+#define PVRSRV_BRIDGE_UNMAP_MEM_INFO_FROM_USER	PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+25)
+#define PVRSRV_BRIDGE_EXPORT_DEVICEMEM			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+26)
+#define PVRSRV_BRIDGE_RELEASE_MMAP_DATA			PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST+27)
+#define PVRSRV_BRIDGE_CORE_CMD_LAST				(PVRSRV_BRIDGE_CORE_CMD_FIRST+27)
+
+#define PVRSRV_BRIDGE_SIM_CMD_FIRST				(PVRSRV_BRIDGE_CORE_CMD_LAST+1)
+#define PVRSRV_BRIDGE_PROCESS_SIMISR_EVENT		PVRSRV_IOWR(PVRSRV_BRIDGE_SIM_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_REGISTER_SIM_PROCESS		PVRSRV_IOWR(PVRSRV_BRIDGE_SIM_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_UNREGISTER_SIM_PROCESS	PVRSRV_IOWR(PVRSRV_BRIDGE_SIM_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_SIM_CMD_LAST				(PVRSRV_BRIDGE_SIM_CMD_FIRST+2)
+
+#define PVRSRV_BRIDGE_MAPPING_CMD_FIRST			(PVRSRV_BRIDGE_SIM_CMD_LAST+1)
+#define PVRSRV_BRIDGE_MAPPHYSTOUSERSPACE		PVRSRV_IOWR(PVRSRV_BRIDGE_MAPPING_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_UNMAPPHYSTOUSERSPACE		PVRSRV_IOWR(PVRSRV_BRIDGE_MAPPING_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_GETPHYSTOUSERSPACEMAP		PVRSRV_IOWR(PVRSRV_BRIDGE_MAPPING_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_MAPPING_CMD_LAST			(PVRSRV_BRIDGE_MAPPING_CMD_FIRST+2)
+
+#define PVRSRV_BRIDGE_STATS_CMD_FIRST			(PVRSRV_BRIDGE_MAPPING_CMD_LAST+1)
+#define	PVRSRV_BRIDGE_GET_FB_STATS				PVRSRV_IOWR(PVRSRV_BRIDGE_STATS_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_STATS_CMD_LAST			(PVRSRV_BRIDGE_STATS_CMD_FIRST+0)
+
+#define PVRSRV_BRIDGE_MISC_CMD_FIRST			(PVRSRV_BRIDGE_STATS_CMD_LAST+1)
+#define PVRSRV_BRIDGE_GET_MISC_INFO				PVRSRV_IOWR(PVRSRV_BRIDGE_MISC_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_RELEASE_MISC_INFO			PVRSRV_IOWR(PVRSRV_BRIDGE_MISC_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_MISC_CMD_LAST				(PVRSRV_BRIDGE_MISC_CMD_FIRST+1)
+
+#define PVRSRV_BRIDGE_OVERLAY_CMD_FIRST			(PVRSRV_BRIDGE_MISC_CMD_LAST+1)
+#if defined (SUPPORT_OVERLAY_ROTATE_BLIT)
+#define PVRSRV_BRIDGE_INIT_3D_OVL_BLT_RES		PVRSRV_IOWR(PVRSRV_BRIDGE_OVERLAY_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_DEINIT_3D_OVL_BLT_RES		PVRSRV_IOWR(PVRSRV_BRIDGE_OVERLAY_CMD_FIRST+1)
+#endif
+#define PVRSRV_BRIDGE_OVERLAY_CMD_LAST			(PVRSRV_BRIDGE_OVERLAY_CMD_FIRST+1)
+
+#if defined(PDUMP)
+#define PVRSRV_BRIDGE_PDUMP_CMD_FIRST			(PVRSRV_BRIDGE_OVERLAY_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_PDUMP_INIT			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_PDUMP_MEMPOL			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_PDUMP_DUMPMEM			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_PDUMP_REG				PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+3)
+#define PVRSRV_BRIDGE_PDUMP_REGPOL			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+4)
+#define PVRSRV_BRIDGE_PDUMP_COMMENT			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+5)
+#define PVRSRV_BRIDGE_PDUMP_SETFRAME			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+6)
+#define PVRSRV_BRIDGE_PDUMP_ISCAPTURING			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+7)
+#define PVRSRV_BRIDGE_PDUMP_DUMPBITMAP			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+8)
+#define PVRSRV_BRIDGE_PDUMP_DUMPREADREG			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+9)
+#define PVRSRV_BRIDGE_PDUMP_SYNCPOL			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+10)
+#define PVRSRV_BRIDGE_PDUMP_DUMPSYNC			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+11)
+#define PVRSRV_BRIDGE_PDUMP_MEMPAGES			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+12)
+#define PVRSRV_BRIDGE_PDUMP_DRIVERINFO			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+13)
+#define PVRSRV_BRIDGE_PDUMP_PDREG			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+14)
+#define PVRSRV_BRIDGE_PDUMP_DUMPPDDEVPADDR		PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+15)
+#define PVRSRV_BRIDGE_PDUMP_CYCLE_COUNT_REG_READ	PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+16)
+#define PVRSRV_BRIDGE_PDUMP_STARTINITPHASE			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+17)
+#define PVRSRV_BRIDGE_PDUMP_STOPINITPHASE			PVRSRV_IOWR(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+18)
+#define PVRSRV_BRIDGE_PDUMP_CMD_LAST			(PVRSRV_BRIDGE_PDUMP_CMD_FIRST+18)
+#else
+#define PVRSRV_BRIDGE_PDUMP_CMD_LAST			PVRSRV_BRIDGE_OVERLAY_CMD_LAST
+#endif
+
+#define PVRSRV_BRIDGE_OEM_CMD_FIRST				(PVRSRV_BRIDGE_PDUMP_CMD_LAST+1)
+#define PVRSRV_BRIDGE_GET_OEMJTABLE				PVRSRV_IOWR(PVRSRV_BRIDGE_OEM_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_OEM_CMD_LAST				(PVRSRV_BRIDGE_OEM_CMD_FIRST+0)
+
+#define PVRSRV_BRIDGE_DEVCLASS_CMD_FIRST		(PVRSRV_BRIDGE_OEM_CMD_LAST+1)
+#define PVRSRV_BRIDGE_ENUM_CLASS				PVRSRV_IOWR(PVRSRV_BRIDGE_DEVCLASS_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_DEVCLASS_CMD_LAST			(PVRSRV_BRIDGE_DEVCLASS_CMD_FIRST+0)
+
+#define PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST		(PVRSRV_BRIDGE_DEVCLASS_CMD_LAST+1)
+#define PVRSRV_BRIDGE_OPEN_DISPCLASS_DEVICE		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_CLOSE_DISPCLASS_DEVICE	PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_ENUM_DISPCLASS_FORMATS	PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_ENUM_DISPCLASS_DIMS		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+3)
+#define PVRSRV_BRIDGE_GET_DISPCLASS_SYSBUFFER	PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+4)
+#define PVRSRV_BRIDGE_GET_DISPCLASS_INFO		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+5)
+#define PVRSRV_BRIDGE_CREATE_DISPCLASS_SWAPCHAIN		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+6)
+#define PVRSRV_BRIDGE_DESTROY_DISPCLASS_SWAPCHAIN		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+7)
+#define PVRSRV_BRIDGE_SET_DISPCLASS_DSTRECT		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+8)
+#define PVRSRV_BRIDGE_SET_DISPCLASS_SRCRECT		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+9)
+#define PVRSRV_BRIDGE_SET_DISPCLASS_DSTCOLOURKEY		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+10)
+#define PVRSRV_BRIDGE_SET_DISPCLASS_SRCCOLOURKEY		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+11)
+#define PVRSRV_BRIDGE_GET_DISPCLASS_BUFFERS		PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+12)
+#define PVRSRV_BRIDGE_SWAP_DISPCLASS_TO_BUFFER	PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+13)
+#define PVRSRV_BRIDGE_SWAP_DISPCLASS_TO_SYSTEM	PVRSRV_IOWR(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+14)
+#define PVRSRV_BRIDGE_DISPCLASS_CMD_LAST		(PVRSRV_BRIDGE_DISPCLASS_CMD_FIRST+14)
+
+
+#define PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST		(PVRSRV_BRIDGE_DISPCLASS_CMD_LAST+1)
+#define PVRSRV_BRIDGE_OPEN_BUFFERCLASS_DEVICE	PVRSRV_IOWR(PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_CLOSE_BUFFERCLASS_DEVICE	PVRSRV_IOWR(PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_GET_BUFFERCLASS_INFO		PVRSRV_IOWR(PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_GET_BUFFERCLASS_BUFFER	PVRSRV_IOWR(PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST+3)
+#define PVRSRV_BRIDGE_GET_BUFFERCLASS_BUFFER_ID_FROM_TAG PVRSRV_IOWR(PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST+4)
+#define PVRSRV_BRIDGE_BUFCLASS_CMD_LAST			(PVRSRV_BRIDGE_BUFCLASS_CMD_FIRST+4)
+
+#define PVRSRV_BRIDGE_WRAP_CMD_FIRST			(PVRSRV_BRIDGE_BUFCLASS_CMD_LAST+1)
+#define PVRSRV_BRIDGE_WRAP_EXT_MEMORY			PVRSRV_IOWR(PVRSRV_BRIDGE_WRAP_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_UNWRAP_EXT_MEMORY			PVRSRV_IOWR(PVRSRV_BRIDGE_WRAP_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_WRAP_CMD_LAST				(PVRSRV_BRIDGE_WRAP_CMD_FIRST+1)
+
+#define PVRSRV_BRIDGE_SHAREDMEM_CMD_FIRST		(PVRSRV_BRIDGE_WRAP_CMD_LAST+1)
+#define PVRSRV_BRIDGE_ALLOC_SHARED_SYS_MEM		PVRSRV_IOWR(PVRSRV_BRIDGE_SHAREDMEM_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_FREE_SHARED_SYS_MEM		PVRSRV_IOWR(PVRSRV_BRIDGE_SHAREDMEM_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_MAP_MEMINFO_MEM			PVRSRV_IOWR(PVRSRV_BRIDGE_SHAREDMEM_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_UNMAP_MEMINFO_MEM			PVRSRV_IOWR(PVRSRV_BRIDGE_SHAREDMEM_CMD_FIRST+3)
+#define PVRSRV_BRIDGE_SHAREDMEM_CMD_LAST		(PVRSRV_BRIDGE_SHAREDMEM_CMD_FIRST+3)
+
+#define PVRSRV_BRIDGE_SERVICES4_TMP_CMD_FIRST	(PVRSRV_BRIDGE_SHAREDMEM_CMD_LAST+1)
+#define PVRSRV_BRIDGE_GETMMU_PD_DEVPADDR        PVRSRV_IOWR(PVRSRV_BRIDGE_SERVICES4_TMP_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_SERVICES4_TMP_CMD_LAST	(PVRSRV_BRIDGE_SERVICES4_TMP_CMD_FIRST+0)
+
+#define PVRSRV_BRIDGE_INITSRV_CMD_FIRST			(PVRSRV_BRIDGE_SERVICES4_TMP_CMD_LAST+1)
+#define PVRSRV_BRIDGE_INITSRV_CONNECT			PVRSRV_IOWR(PVRSRV_BRIDGE_INITSRV_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_INITSRV_DISCONNECT		PVRSRV_IOWR(PVRSRV_BRIDGE_INITSRV_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_INITSRV_CMD_LAST			(PVRSRV_BRIDGE_INITSRV_CMD_FIRST+1)
+
+#define PVRSRV_BRIDGE_EVENT_OBJECT_CMD_FIRST	(PVRSRV_BRIDGE_INITSRV_CMD_LAST+1)
+#define PVRSRV_BRIDGE_EVENT_OBJECT_WAIT			PVRSRV_IOWR(PVRSRV_BRIDGE_EVENT_OBJECT_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_EVENT_OBJECT_OPEN			PVRSRV_IOWR(PVRSRV_BRIDGE_EVENT_OBJECT_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_EVENT_OBJECT_CLOSE		PVRSRV_IOWR(PVRSRV_BRIDGE_EVENT_OBJECT_CMD_FIRST+2)
+#define PVRSRV_BRIDGE_EVENT_OBJECT_CMD_LAST		(PVRSRV_BRIDGE_EVENT_OBJECT_CMD_FIRST+2)
+
+#define PVRSRV_BRIDGE_SYNC_OPS_CMD_FIRST		(PVRSRV_BRIDGE_EVENT_OBJECT_CMD_LAST+1)
+#define PVRSRV_BRIDGE_MODIFY_PENDING_SYNC_OPS	PVRSRV_IOWR(PVRSRV_BRIDGE_SYNC_OPS_CMD_FIRST+0)
+#define PVRSRV_BRIDGE_MODIFY_COMPLETE_SYNC_OPS	PVRSRV_IOWR(PVRSRV_BRIDGE_SYNC_OPS_CMD_FIRST+1)
+#define PVRSRV_BRIDGE_SYNC_OPS_CMD_LAST			(PVRSRV_BRIDGE_SYNC_OPS_CMD_FIRST+1)
+
+#define PVRSRV_BRIDGE_LAST_NON_DEVICE_CMD		(PVRSRV_BRIDGE_SYNC_OPS_CMD_LAST+1)
+
+
+#define PVRSRV_KERNEL_MODE_CLIENT				1
+
+typedef struct PVRSRV_BRIDGE_RETURN_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_VOID *pvData;
+
+}PVRSRV_BRIDGE_RETURN;
+
+
+typedef struct PVRSRV_BRIDGE_PACKAGE_TAG
+{
+	IMG_UINT32				ui32BridgeID;
+	IMG_UINT32				ui32Size;
+	IMG_VOID				*pvParamIn;
+	IMG_UINT32				ui32InBufferSize;
+	IMG_VOID				*pvParamOut;
+	IMG_UINT32				ui32OutBufferSize;
+
+	IMG_HANDLE				hKernelServices;
+}PVRSRV_BRIDGE_PACKAGE;
+
+
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_ACQUIRE_DEVICEINFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_UINT32			uiDevIndex;
+	PVRSRV_DEVICE_TYPE	eDeviceType;
+
+} PVRSRV_BRIDGE_IN_ACQUIRE_DEVICEINFO;
+
+
+typedef struct PVRSRV_BRIDGE_IN_ENUMCLASS_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	PVRSRV_DEVICE_CLASS sDeviceClass;
+} PVRSRV_BRIDGE_IN_ENUMCLASS;
+
+
+typedef struct PVRSRV_BRIDGE_IN_CLOSE_DISPCLASS_DEVICE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+} PVRSRV_BRIDGE_IN_CLOSE_DISPCLASS_DEVICE;
+
+
+typedef struct PVRSRV_BRIDGE_IN_ENUM_DISPCLASS_FORMATS_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+} PVRSRV_BRIDGE_IN_ENUM_DISPCLASS_FORMATS;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_DISPCLASS_SYSBUFFER_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+} PVRSRV_BRIDGE_IN_GET_DISPCLASS_SYSBUFFER;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_DISPCLASS_INFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+} PVRSRV_BRIDGE_IN_GET_DISPCLASS_INFO;
+
+
+typedef struct PVRSRV_BRIDGE_IN_CLOSE_BUFFERCLASS_DEVICE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+} PVRSRV_BRIDGE_IN_CLOSE_BUFFERCLASS_DEVICE;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_INFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+} PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_INFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_RELEASE_DEVICEINFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+
+} PVRSRV_BRIDGE_IN_RELEASE_DEVICEINFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_FREE_CLASSDEVICEINFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	PVRSRV_DEVICE_CLASS DeviceClass;
+	IMG_VOID*			pvDevInfo;
+
+}PVRSRV_BRIDGE_IN_FREE_CLASSDEVICEINFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_DEVMEM_HEAPINFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_HANDLE 			hDevMemContext;
+
+}PVRSRV_BRIDGE_IN_GET_DEVMEM_HEAPINFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_CREATE_DEVMEMCONTEXT_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+
+}PVRSRV_BRIDGE_IN_CREATE_DEVMEMCONTEXT;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_DESTROY_DEVMEMCONTEXT_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE 			hDevCookie;
+	IMG_HANDLE 			hDevMemContext;
+
+}PVRSRV_BRIDGE_IN_DESTROY_DEVMEMCONTEXT;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_HANDLE			hDevMemHeap;
+	IMG_UINT32			ui32Attribs;
+	IMG_SIZE_T			ui32Size;
+	IMG_SIZE_T			ui32Alignment;
+
+}PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM;
+
+
+typedef struct PVRSRV_BRIDGE_IN_MAPMEMINFOTOUSER_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+
+}PVRSRV_BRIDGE_IN_MAPMEMINFOTOUSER;
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNMAPMEMINFOFROMUSER_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	IMG_PVOID				 pvLinAddr;
+	IMG_HANDLE				 hMappingInfo;
+
+}PVRSRV_BRIDGE_IN_UNMAPMEMINFOFROMUSER;
+
+
+typedef struct PVRSRV_BRIDGE_IN_FREEDEVICEMEM_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+
+}PVRSRV_BRIDGE_IN_FREEDEVICEMEM;
+
+
+typedef struct PVRSRV_BRIDGE_IN_EXPORTDEVICEMEM_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+
+}PVRSRV_BRIDGE_IN_EXPORTDEVICEMEM;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GETFREEDEVICEMEM_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_UINT32			ui32Flags;
+
+} PVRSRV_BRIDGE_IN_GETFREEDEVICEMEM;
+
+
+typedef struct PVRSRV_BRIDGE_IN_CREATECOMMANDQUEUE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_SIZE_T			ui32QueueSize;
+
+}PVRSRV_BRIDGE_IN_CREATECOMMANDQUEUE;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_DESTROYCOMMANDQUEUE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	PVRSRV_QUEUE_INFO	*psQueueInfo;
+
+}PVRSRV_BRIDGE_IN_DESTROYCOMMANDQUEUE;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_MHANDLE_TO_MMAP_DATA_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hMHandle;
+} PVRSRV_BRIDGE_IN_MHANDLE_TO_MMAP_DATA;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_RELEASE_MMAP_DATA_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hMHandle;
+} PVRSRV_BRIDGE_IN_RELEASE_MMAP_DATA;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_RESERVE_DEV_VIRTMEM_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevMemHeap;
+	IMG_DEV_VIRTADDR	*psDevVAddr;
+	IMG_SIZE_T			ui32Size;
+	IMG_SIZE_T			ui32Alignment;
+
+}PVRSRV_BRIDGE_IN_RESERVE_DEV_VIRTMEM;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_CONNECT_SERVICES_TAG
+{
+	PVRSRV_ERROR 			eError;
+	IMG_HANDLE		hKernelServices;
+}PVRSRV_BRIDGE_OUT_CONNECT_SERVICES;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_RESERVE_DEV_VIRTMEM_TAG
+{
+	PVRSRV_ERROR 			eError;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sClientSyncInfo;
+
+}PVRSRV_BRIDGE_OUT_RESERVE_DEV_VIRTMEM;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_FREE_DEV_VIRTMEM_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sClientSyncInfo;
+
+}PVRSRV_BRIDGE_IN_FREE_DEV_VIRTMEM;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hKernelMemInfo;
+	IMG_HANDLE				hDstDevMemHeap;
+
+}PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY_TAG
+{
+	PVRSRV_ERROR			eError;
+	PVRSRV_KERNEL_MEM_INFO	*psDstKernelMemInfo;
+	PVRSRV_KERNEL_SYNC_INFO	*psDstKernelSyncInfo;
+	PVRSRV_CLIENT_MEM_INFO	sDstClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sDstClientSyncInfo;
+
+}PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNMAP_DEV_MEMORY_TAG
+{
+	IMG_UINT32					ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO		*psKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO		sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO		sClientSyncInfo;
+
+}PVRSRV_BRIDGE_IN_UNMAP_DEV_MEMORY;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_MAP_EXT_MEMORY_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	IMG_SYS_PHYADDR			*psSysPAddr;
+	IMG_UINT32				ui32Flags;
+
+}PVRSRV_BRIDGE_IN_MAP_EXT_MEMORY;
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNMAP_EXT_MEMORY_TAG
+{
+	IMG_UINT32					ui32BridgeFlags;
+	PVRSRV_CLIENT_MEM_INFO		sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO		sClientSyncInfo;
+	IMG_UINT32					ui32Flags;
+
+}PVRSRV_BRIDGE_IN_UNMAP_EXT_MEMORY;
+
+
+typedef struct PVRSRV_BRIDGE_IN_MAP_DEVICECLASS_MEMORY_TAG
+{
+	IMG_UINT32					ui32BridgeFlags;
+	IMG_HANDLE		hDeviceClassBuffer;
+	IMG_HANDLE		hDevMemContext;
+
+}PVRSRV_BRIDGE_IN_MAP_DEVICECLASS_MEMORY;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY_TAG
+{
+	PVRSRV_ERROR			eError;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO sClientSyncInfo;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	IMG_HANDLE				hMappingInfo;
+
+}PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNMAP_DEVICECLASS_MEMORY_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sClientSyncInfo;
+
+}PVRSRV_BRIDGE_IN_UNMAP_DEVICECLASS_MEMORY;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_MEMPOL_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	IMG_UINT32 ui32Offset;
+	IMG_UINT32 ui32Value;
+	IMG_UINT32 ui32Mask;
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_MEMPOL;
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_SYNCPOL_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	IMG_BOOL bIsRead;
+	IMG_UINT32 ui32Value;
+	IMG_UINT32 ui32Mask;
+
+}PVRSRV_BRIDGE_IN_PDUMP_SYNCPOL;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_DUMPMEM_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_PVOID pvLinAddr;
+	IMG_PVOID pvAltLinAddr;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	IMG_UINT32 ui32Offset;
+	IMG_UINT32 ui32Bytes;
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_DUMPMEM;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_DUMPSYNC_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_PVOID pvAltLinAddr;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	IMG_UINT32 ui32Offset;
+	IMG_UINT32 ui32Bytes;
+
+}PVRSRV_BRIDGE_IN_PDUMP_DUMPSYNC;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_DUMPREG_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	PVRSRV_HWREG sHWReg;
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_DUMPREG;
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_REGPOL_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	PVRSRV_HWREG sHWReg;
+	IMG_UINT32 ui32Mask;
+	IMG_UINT32 ui32Flags;
+}PVRSRV_BRIDGE_IN_PDUMP_REGPOL;
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_DUMPPDREG_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	PVRSRV_HWREG sHWReg;
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_DUMPPDREG;
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_MEMPAGES_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hKernelMemInfo;
+	IMG_DEV_PHYADDR		*pPages;
+	IMG_UINT32			ui32NumPages;
+	IMG_DEV_VIRTADDR	sDevAddr;
+	IMG_UINT32			ui32Start;
+	IMG_UINT32			ui32Length;
+	IMG_BOOL			bContinuous;
+
+}PVRSRV_BRIDGE_IN_PDUMP_MEMPAGES;
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_COMMENT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_CHAR szComment[PVRSRV_PDUMP_MAX_COMMENT_SIZE];
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_COMMENT;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_SETFRAME_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_UINT32 ui32Frame;
+
+}PVRSRV_BRIDGE_IN_PDUMP_SETFRAME;
+
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_BITMAP_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_CHAR szFileName[PVRSRV_PDUMP_MAX_FILENAME_SIZE];
+	IMG_UINT32 ui32FileOffset;
+	IMG_UINT32 ui32Width;
+	IMG_UINT32 ui32Height;
+	IMG_UINT32 ui32StrideInBytes;
+	IMG_DEV_VIRTADDR sDevBaseAddr;
+	IMG_UINT32 ui32Size;
+	PDUMP_PIXEL_FORMAT ePixelFormat;
+	PDUMP_MEM_FORMAT eMemFormat;
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_BITMAP;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_READREG_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_CHAR szFileName[PVRSRV_PDUMP_MAX_FILENAME_SIZE];
+	IMG_UINT32 ui32FileOffset;
+	IMG_UINT32 ui32Address;
+	IMG_UINT32 ui32Size;
+	IMG_UINT32 ui32Flags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_READREG;
+
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_DRIVERINFO_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_CHAR szString[PVRSRV_PDUMP_MAX_COMMENT_SIZE];
+	IMG_BOOL bContinuous;
+
+}PVRSRV_BRIDGE_IN_PDUMP_DRIVERINFO;
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_DUMPPDDEVPADDR_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hKernelMemInfo;
+	IMG_UINT32 ui32Offset;
+	IMG_DEV_PHYADDR sPDDevPAddr;
+}PVRSRV_BRIDGE_IN_PDUMP_DUMPPDDEVPADDR;
+
+
+typedef struct PVRSRV_BRIDGE_PDUM_IN_CYCLE_COUNT_REG_READ_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_UINT32 ui32RegOffset;
+	IMG_BOOL bLastFrame;
+}PVRSRV_BRIDGE_IN_PDUMP_CYCLE_COUNT_REG_READ;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_ENUMDEVICE_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_UINT32 ui32NumDevices;
+	PVRSRV_DEVICE_IDENTIFIER asDeviceIdentifier[PVRSRV_MAX_DEVICES];
+
+}PVRSRV_BRIDGE_OUT_ENUMDEVICE;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_ACQUIRE_DEVICEINFO_TAG
+{
+
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hDevCookie;
+
+} PVRSRV_BRIDGE_OUT_ACQUIRE_DEVICEINFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_ENUMCLASS_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_UINT32 ui32NumDevices;
+	IMG_UINT32 ui32DevID[PVRSRV_MAX_DEVICES];
+
+}PVRSRV_BRIDGE_OUT_ENUMCLASS;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_OPEN_DISPCLASS_DEVICE_TAG
+{
+	IMG_UINT32		ui32BridgeFlags;
+	IMG_UINT32		ui32DeviceID;
+	IMG_HANDLE		hDevCookie;
+
+}PVRSRV_BRIDGE_IN_OPEN_DISPCLASS_DEVICE;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_OPEN_DISPCLASS_DEVICE_TAG
+{
+	PVRSRV_ERROR	eError;
+	IMG_HANDLE		hDeviceKM;
+
+}PVRSRV_BRIDGE_OUT_OPEN_DISPCLASS_DEVICE;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_WRAP_EXT_MEMORY_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE              hDevCookie;
+	IMG_HANDLE				hDevMemContext;
+	IMG_VOID				*pvLinAddr;
+	IMG_SIZE_T              ui32ByteSize;
+	IMG_SIZE_T              ui32PageOffset;
+	IMG_BOOL                bPhysContig;
+	IMG_UINT32				ui32NumPageTableEntries;
+	IMG_SYS_PHYADDR         *psSysPAddr;
+	IMG_UINT32				ui32Flags;
+
+}PVRSRV_BRIDGE_IN_WRAP_EXT_MEMORY;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_WRAP_EXT_MEMORY_TAG
+{
+	PVRSRV_ERROR	eError;
+	PVRSRV_CLIENT_MEM_INFO  sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sClientSyncInfo;
+
+}PVRSRV_BRIDGE_OUT_WRAP_EXT_MEMORY;
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNWRAP_EXT_MEMORY_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO sClientSyncInfo;
+
+}PVRSRV_BRIDGE_IN_UNWRAP_EXT_MEMORY;
+
+
+#define PVRSRV_MAX_DC_DISPLAY_FORMATS			10
+#define PVRSRV_MAX_DC_DISPLAY_DIMENSIONS		10
+#define PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS			4
+#define PVRSRV_MAX_DC_CLIP_RECTS				32
+
+
+typedef struct PVRSRV_BRIDGE_OUT_ENUM_DISPCLASS_FORMATS_TAG
+{
+	PVRSRV_ERROR	eError;
+	IMG_UINT32		ui32Count;
+	DISPLAY_FORMAT	asFormat[PVRSRV_MAX_DC_DISPLAY_FORMATS];
+
+}PVRSRV_BRIDGE_OUT_ENUM_DISPCLASS_FORMATS;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_ENUM_DISPCLASS_DIMS_TAG
+{
+	IMG_UINT32		ui32BridgeFlags;
+	IMG_HANDLE		hDeviceKM;
+	DISPLAY_FORMAT	sFormat;
+
+}PVRSRV_BRIDGE_IN_ENUM_DISPCLASS_DIMS;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_ENUM_DISPCLASS_DIMS_TAG
+{
+	PVRSRV_ERROR	eError;
+	IMG_UINT32		ui32Count;
+	DISPLAY_DIMS	asDim[PVRSRV_MAX_DC_DISPLAY_DIMENSIONS];
+
+}PVRSRV_BRIDGE_OUT_ENUM_DISPCLASS_DIMS;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_DISPCLASS_INFO_TAG
+{
+	PVRSRV_ERROR	eError;
+	DISPLAY_INFO	sDisplayInfo;
+
+}PVRSRV_BRIDGE_OUT_GET_DISPCLASS_INFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_DISPCLASS_SYSBUFFER_TAG
+{
+	PVRSRV_ERROR	eError;
+	IMG_HANDLE		hBuffer;
+
+}PVRSRV_BRIDGE_OUT_GET_DISPCLASS_SYSBUFFER;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_CREATE_DISPCLASS_SWAPCHAIN_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDeviceKM;
+	IMG_UINT32				ui32Flags;
+	DISPLAY_SURF_ATTRIBUTES	sDstSurfAttrib;
+	DISPLAY_SURF_ATTRIBUTES	sSrcSurfAttrib;
+	IMG_UINT32				ui32BufferCount;
+	IMG_UINT32				ui32OEMFlags;
+	IMG_UINT32				ui32SwapChainID;
+
+} PVRSRV_BRIDGE_IN_CREATE_DISPCLASS_SWAPCHAIN;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_CREATE_DISPCLASS_SWAPCHAIN_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hSwapChain;
+	IMG_UINT32			ui32SwapChainID;
+
+} PVRSRV_BRIDGE_OUT_CREATE_DISPCLASS_SWAPCHAIN;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_DESTROY_DISPCLASS_SWAPCHAIN_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_HANDLE			hSwapChain;
+
+} PVRSRV_BRIDGE_IN_DESTROY_DISPCLASS_SWAPCHAIN;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_SET_DISPCLASS_RECT_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_HANDLE			hSwapChain;
+	IMG_RECT			sRect;
+
+} PVRSRV_BRIDGE_IN_SET_DISPCLASS_RECT;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_SET_DISPCLASS_COLOURKEY_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_HANDLE			hSwapChain;
+	IMG_UINT32			ui32CKColour;
+
+} PVRSRV_BRIDGE_IN_SET_DISPCLASS_COLOURKEY;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_DISPCLASS_BUFFERS_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_HANDLE			hSwapChain;
+
+} PVRSRV_BRIDGE_IN_GET_DISPCLASS_BUFFERS;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_DISPCLASS_BUFFERS_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_UINT32			ui32BufferCount;
+	IMG_HANDLE			ahBuffer[PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS];
+
+} PVRSRV_BRIDGE_OUT_GET_DISPCLASS_BUFFERS;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_SWAP_DISPCLASS_TO_BUFFER_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_HANDLE			hBuffer;
+	IMG_UINT32			ui32SwapInterval;
+	IMG_HANDLE			hPrivateTag;
+	IMG_UINT32			ui32ClipRectCount;
+	IMG_RECT			sClipRect[PVRSRV_MAX_DC_CLIP_RECTS];
+
+} PVRSRV_BRIDGE_IN_SWAP_DISPCLASS_TO_BUFFER;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_SWAP_DISPCLASS_TO_SYSTEM_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_HANDLE			hSwapChain;
+
+} PVRSRV_BRIDGE_IN_SWAP_DISPCLASS_TO_SYSTEM;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_OPEN_BUFFERCLASS_DEVICE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_UINT32			ui32DeviceID;
+	IMG_HANDLE			hDevCookie;
+
+} PVRSRV_BRIDGE_IN_OPEN_BUFFERCLASS_DEVICE;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_OPEN_BUFFERCLASS_DEVICE_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hDeviceKM;
+
+} PVRSRV_BRIDGE_OUT_OPEN_BUFFERCLASS_DEVICE;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_INFO_TAG
+{
+	PVRSRV_ERROR		eError;
+	BUFFER_INFO			sBufferInfo;
+
+} PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_INFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_BUFFER_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDeviceKM;
+	IMG_UINT32			ui32BufferIndex;
+
+} PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_BUFFER;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_BUFFER_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hBuffer;
+
+} PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_BUFFER;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_DEVMEM_HEAPINFO_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_UINT32			ui32ClientHeapCount;
+	PVRSRV_HEAP_INFO	sHeapInfo[PVRSRV_MAX_CLIENT_HEAPS];
+
+} PVRSRV_BRIDGE_OUT_GET_DEVMEM_HEAPINFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_CREATE_DEVMEMCONTEXT_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hDevMemContext;
+	IMG_UINT32			ui32ClientHeapCount;
+	PVRSRV_HEAP_INFO	sHeapInfo[PVRSRV_MAX_CLIENT_HEAPS];
+
+} PVRSRV_BRIDGE_OUT_CREATE_DEVMEMCONTEXT;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_CREATE_DEVMEMHEAP_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hDevMemHeap;
+
+} PVRSRV_BRIDGE_OUT_CREATE_DEVMEMHEAP;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_ALLOCDEVICEMEM_TAG
+{
+	PVRSRV_ERROR			eError;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sClientSyncInfo;
+
+} PVRSRV_BRIDGE_OUT_ALLOCDEVICEMEM;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM_TAG
+{
+	PVRSRV_ERROR			eError;
+	IMG_HANDLE				hMemInfo;
+#if defined(SUPPORT_MEMINFO_IDS)
+	IMG_UINT64				ui64Stamp;
+#endif
+
+} PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_MAPMEMINFOTOUSER_TAG
+{
+	PVRSRV_ERROR			eError;
+	IMG_PVOID				pvLinAddr;
+	IMG_HANDLE				hMappingInfo;
+
+}PVRSRV_BRIDGE_OUT_MAPMEMINFOTOUSER;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GETFREEDEVICEMEM_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_SIZE_T ui32Total;
+	IMG_SIZE_T ui32Free;
+	IMG_SIZE_T ui32LargestBlock;
+
+} PVRSRV_BRIDGE_OUT_GETFREEDEVICEMEM;
+
+
+#include "pvrmmap.h"
+typedef struct PVRSRV_BRIDGE_OUT_MHANDLE_TO_MMAP_DATA_TAG
+{
+    PVRSRV_ERROR		eError;
+
+
+     IMG_UINT32			ui32MMapOffset;
+
+
+    IMG_UINT32			ui32ByteOffset;
+
+
+    IMG_UINT32 			ui32RealByteSize;
+
+
+    IMG_UINT32			ui32UserVAddr;
+
+} PVRSRV_BRIDGE_OUT_MHANDLE_TO_MMAP_DATA;
+
+typedef struct PVRSRV_BRIDGE_OUT_RELEASE_MMAP_DATA_TAG
+{
+    PVRSRV_ERROR		eError;
+
+
+    IMG_BOOL			bMUnmap;
+
+
+    IMG_UINT32			ui32UserVAddr;
+
+
+    IMG_UINT32			ui32RealByteSize;
+} PVRSRV_BRIDGE_OUT_RELEASE_MMAP_DATA;
+
+typedef struct PVRSRV_BRIDGE_IN_GET_MISC_INFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	PVRSRV_MISC_INFO	sMiscInfo;
+
+}PVRSRV_BRIDGE_IN_GET_MISC_INFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GET_MISC_INFO_TAG
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_MISC_INFO	sMiscInfo;
+
+}PVRSRV_BRIDGE_OUT_GET_MISC_INFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_RELEASE_MISC_INFO_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	PVRSRV_MISC_INFO	sMiscInfo;
+
+}PVRSRV_BRIDGE_IN_RELEASE_MISC_INFO;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_RELEASE_MISC_INFO_TAG
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_MISC_INFO	sMiscInfo;
+
+}PVRSRV_BRIDGE_OUT_RELEASE_MISC_INFO;
+
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_PDUMP_ISCAPTURING_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_BOOL bIsCapturing;
+
+} PVRSRV_BRIDGE_OUT_PDUMP_ISCAPTURING;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GET_FB_STATS_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_SIZE_T ui32Total;
+	IMG_SIZE_T ui32Available;
+
+} PVRSRV_BRIDGE_IN_GET_FB_STATS;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_MAPPHYSTOUSERSPACE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_SYS_PHYADDR		sSysPhysAddr;
+	IMG_UINT32			uiSizeInBytes;
+
+} PVRSRV_BRIDGE_IN_MAPPHYSTOUSERSPACE;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_MAPPHYSTOUSERSPACE_TAG
+{
+	IMG_PVOID			pvUserAddr;
+	IMG_UINT32			uiActualSize;
+	IMG_PVOID			pvProcess;
+
+} PVRSRV_BRIDGE_OUT_MAPPHYSTOUSERSPACE;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNMAPPHYSTOUSERSPACE_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_PVOID			pvUserAddr;
+	IMG_PVOID			pvProcess;
+
+} PVRSRV_BRIDGE_IN_UNMAPPHYSTOUSERSPACE;
+
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GETPHYSTOUSERSPACEMAP_TAG
+{
+	IMG_PVOID			*ppvTbl;
+	IMG_UINT32			uiTblSize;
+
+} PVRSRV_BRIDGE_OUT_GETPHYSTOUSERSPACEMAP;
+
+
+
+typedef struct PVRSRV_BRIDGE_IN_REGISTER_SIM_PROCESS_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_PVOID			pvProcess;
+
+} PVRSRV_BRIDGE_IN_REGISTER_SIM_PROCESS;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_REGISTER_SIM_PROCESS_TAG
+{
+	IMG_SYS_PHYADDR		sRegsPhysBase;
+	IMG_VOID			*pvRegsBase;
+	IMG_PVOID			pvProcess;
+	IMG_UINT32			ulNoOfEntries;
+	IMG_PVOID			pvTblLinAddr;
+
+} PVRSRV_BRIDGE_OUT_REGISTER_SIM_PROCESS;
+
+
+typedef struct PVRSRV_BRIDGE_IN_UNREGISTER_SIM_PROCESS_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_PVOID			pvProcess;
+	IMG_VOID			*pvRegsBase;
+
+} PVRSRV_BRIDGE_IN_UNREGISTER_SIM_PROCESS;
+
+typedef struct PVRSRV_BRIDGE_IN_PROCESS_SIMISR_EVENT_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_UINT32			ui32StatusAndMask;
+	PVRSRV_ERROR 		eError;
+
+} PVRSRV_BRIDGE_IN_PROCESS_SIMISR_EVENT;
+
+typedef struct PVRSRV_BRIDGE_IN_INITSRV_DISCONNECT_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_BOOL			bInitSuccesful;
+} PVRSRV_BRIDGE_IN_INITSRV_DISCONNECT;
+
+
+typedef struct PVRSRV_BRIDGE_IN_ALLOC_SHARED_SYS_MEM_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+    IMG_UINT32 ui32Flags;
+    IMG_SIZE_T ui32Size;
+}PVRSRV_BRIDGE_IN_ALLOC_SHARED_SYS_MEM;
+
+typedef struct PVRSRV_BRIDGE_OUT_ALLOC_SHARED_SYS_MEM_TAG
+{
+	PVRSRV_ERROR			eError;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+}PVRSRV_BRIDGE_OUT_ALLOC_SHARED_SYS_MEM;
+
+typedef struct PVRSRV_BRIDGE_IN_FREE_SHARED_SYS_MEM_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	sClientMemInfo;
+}PVRSRV_BRIDGE_IN_FREE_SHARED_SYS_MEM;
+
+typedef struct PVRSRV_BRIDGE_OUT_FREE_SHARED_SYS_MEM_TAG
+{
+	PVRSRV_ERROR eError;
+}PVRSRV_BRIDGE_OUT_FREE_SHARED_SYS_MEM;
+
+typedef struct PVRSRV_BRIDGE_IN_MAP_MEMINFO_MEM_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hKernelMemInfo;
+}PVRSRV_BRIDGE_IN_MAP_MEMINFO_MEM;
+
+typedef struct PVRSRV_BRIDGE_OUT_MAP_MEMINFO_MEM_TAG
+{
+	PVRSRV_CLIENT_MEM_INFO  sClientMemInfo;
+	PVRSRV_CLIENT_SYNC_INFO	sClientSyncInfo;
+	PVRSRV_KERNEL_MEM_INFO  *psKernelMemInfo;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	PVRSRV_ERROR eError;
+}PVRSRV_BRIDGE_OUT_MAP_MEMINFO_MEM;
+
+typedef struct PVRSRV_BRIDGE_IN_UNMAP_MEMINFO_MEM_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	PVRSRV_CLIENT_MEM_INFO sClientMemInfo;
+}PVRSRV_BRIDGE_IN_UNMAP_MEMINFO_MEM;
+
+typedef struct PVRSRV_BRIDGE_OUT_UNMAP_MEMINFO_MEM_TAG
+{
+	PVRSRV_ERROR eError;
+}PVRSRV_BRIDGE_OUT_UNMAP_MEMINFO_MEM;
+
+typedef struct PVRSRV_BRIDGE_IN_GETMMU_PD_DEVPADDR_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+    IMG_HANDLE hDevMemContext;
+}PVRSRV_BRIDGE_IN_GETMMU_PD_DEVPADDR;
+
+typedef struct PVRSRV_BRIDGE_OUT_GETMMU_PD_DEVPADDR_TAG
+{
+    IMG_DEV_PHYADDR sPDDevPAddr;
+	PVRSRV_ERROR eError;
+}PVRSRV_BRIDGE_OUT_GETMMU_PD_DEVPADDR;
+
+typedef struct PVRSRV_BRIDGE_IN_EVENT_OBJECT_WAI_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE	hOSEventKM;
+} PVRSRV_BRIDGE_IN_EVENT_OBJECT_WAIT;
+
+typedef struct PVRSRV_BRIDGE_IN_EVENT_OBJECT_OPEN_TAG
+{
+	PVRSRV_EVENTOBJECT sEventObject;
+} PVRSRV_BRIDGE_IN_EVENT_OBJECT_OPEN;
+
+typedef struct	PVRSRV_BRIDGE_OUT_EVENT_OBJECT_OPEN_TAG
+{
+	IMG_HANDLE hOSEvent;
+	PVRSRV_ERROR eError;
+} PVRSRV_BRIDGE_OUT_EVENT_OBJECT_OPEN;
+
+typedef struct PVRSRV_BRIDGE_IN_EVENT_OBJECT_CLOSE_TAG
+{
+	PVRSRV_EVENTOBJECT sEventObject;
+	IMG_HANDLE hOSEventKM;
+} PVRSRV_BRIDGE_IN_EVENT_OBJECT_CLOSE;
+
+typedef struct PVRSRV_BRIDGE_IN_MODIFY_PENDING_SYNC_OPS_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hKernelSyncInfo;
+	IMG_UINT32 ui32ModifyFlags;
+
+} PVRSRV_BRIDGE_IN_MODIFY_PENDING_SYNC_OPS;
+
+typedef struct PVRSRV_BRIDGE_IN_MODIFY_COMPLETE_SYNC_OPS_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hKernelSyncInfo;
+	IMG_UINT32 ui32ModifyFlags;
+
+} PVRSRV_BRIDGE_IN_MODIFY_COMPLETE_SYNC_OPS;
+
+typedef struct PVRSRV_BRIDGE_OUT_MODIFY_PENDING_SYNC_OPS_TAG
+{
+	PVRSRV_ERROR eError;
+
+
+	IMG_UINT32 ui32ReadOpsPending;
+	IMG_UINT32 ui32WriteOpsPending;
+
+} PVRSRV_BRIDGE_OUT_MODIFY_PENDING_SYNC_OPS;
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge_km.h b/drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge_km.h
new file mode 100644
index 0000000..063ffb5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/pvr_bridge_km.h
@@ -0,0 +1,292 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __PVR_BRIDGE_KM_H_
+#define __PVR_BRIDGE_KM_H_
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "pvr_bridge.h"
+#include "perproc.h"
+
+#if defined(__linux__)
+PVRSRV_ERROR LinuxBridgeInit(IMG_VOID);
+IMG_VOID LinuxBridgeDeInit(IMG_VOID);
+#endif
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVEnumerateDevicesKM(IMG_UINT32 *pui32NumDevices,
+												   PVRSRV_DEVICE_IDENTIFIER *psDevIdList);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVAcquireDeviceDataKM(IMG_UINT32			uiDevIndex,
+													PVRSRV_DEVICE_TYPE	eDeviceType,
+													IMG_HANDLE			*phDevCookie);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateCommandQueueKM(IMG_SIZE_T ui32QueueSize,
+													 PVRSRV_QUEUE_INFO **ppsQueueInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyCommandQueueKM(PVRSRV_QUEUE_INFO *psQueueInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDeviceMemHeapsKM(IMG_HANDLE hDevCookie,
+													PVRSRV_HEAP_INFO *psHeapInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateDeviceMemContextKM(IMG_HANDLE					hDevCookie,
+														 PVRSRV_PER_PROCESS_DATA	*psPerProc,
+														 IMG_HANDLE					*phDevMemContext,
+														 IMG_UINT32					*pui32ClientHeapCount,
+														 PVRSRV_HEAP_INFO			*psHeapInfo,
+														 IMG_BOOL					*pbCreated,
+														 IMG_BOOL					*pbShared);
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyDeviceMemContextKM(IMG_HANDLE hDevCookie,
+														  IMG_HANDLE hDevMemContext,
+														  IMG_BOOL *pbDestroyed);
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDeviceMemHeapInfoKM(IMG_HANDLE				hDevCookie,
+															IMG_HANDLE			hDevMemContext,
+															IMG_UINT32			*pui32ClientHeapCount,
+															PVRSRV_HEAP_INFO	*psHeapInfo,
+															IMG_BOOL 			*pbShared
+					);
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV _PVRSRVAllocDeviceMemKM(IMG_HANDLE					hDevCookie,
+												 PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												 IMG_HANDLE					hDevMemHeap,
+												 IMG_UINT32					ui32Flags,
+												 IMG_SIZE_T					ui32Size,
+												 IMG_SIZE_T					ui32Alignment,
+												 PVRSRV_KERNEL_MEM_INFO		**ppsMemInfo);
+
+
+#if defined(PVRSRV_LOG_MEMORY_ALLOCS)
+	#define PVRSRVAllocDeviceMemKM(devCookie, perProc, devMemHeap, flags, size, alignment, memInfo, logStr) \
+		(PVR_TRACE(("PVRSRVAllocDeviceMemKM(" #devCookie ", " #perProc ", " #devMemHeap ", " #flags ", " #size \
+			", " #alignment "," #memInfo "): " logStr " (size = 0x%;x)", size)),\
+			_PVRSRVAllocDeviceMemKM(devCookie, perProc, devMemHeap, flags, size, alignment, memInfo))
+#else
+	#define PVRSRVAllocDeviceMemKM(devCookie, perProc, devMemHeap, flags, size, alignment, memInfo, logStr) \
+			_PVRSRVAllocDeviceMemKM(devCookie, perProc, devMemHeap, flags, size, alignment, memInfo)
+#endif
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeDeviceMemKM(IMG_HANDLE			hDevCookie,
+												PVRSRV_KERNEL_MEM_INFO	*psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDissociateDeviceMemKM(IMG_HANDLE			hDevCookie,
+												PVRSRV_KERNEL_MEM_INFO	*psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVReserveDeviceVirtualMemKM(IMG_HANDLE		hDevMemHeap,
+														 IMG_DEV_VIRTADDR	*psDevVAddr,
+														 IMG_SIZE_T			ui32Size,
+														 IMG_SIZE_T			ui32Alignment,
+														 PVRSRV_KERNEL_MEM_INFO	**ppsMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeDeviceVirtualMemKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapDeviceMemoryKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												  PVRSRV_KERNEL_MEM_INFO	*psSrcMemInfo,
+												  IMG_HANDLE				hDstDevMemHeap,
+												  PVRSRV_KERNEL_MEM_INFO	**ppsDstMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapDeviceMemoryKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVWrapExtMemoryKM(IMG_HANDLE				hDevCookie,
+												PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												IMG_HANDLE				hDevMemContext,
+												IMG_SIZE_T 				ui32ByteSize,
+												IMG_SIZE_T				ui32PageOffset,
+												IMG_BOOL				bPhysContig,
+												IMG_SYS_PHYADDR	 		*psSysAddr,
+												IMG_VOID 				*pvLinAddr,
+												IMG_UINT32				ui32Flags,
+												PVRSRV_KERNEL_MEM_INFO **ppsMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnwrapExtMemoryKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVEnumerateDCKM(PVRSRV_DEVICE_CLASS DeviceClass,
+								 IMG_UINT32 *pui32DevCount,
+								 IMG_UINT32 *pui32DevID );
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVOpenDCDeviceKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+								  IMG_UINT32				ui32DeviceID,
+								  IMG_HANDLE 				hDevCookie,
+								  IMG_HANDLE 				*phDeviceKM);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVCloseDCDeviceKM(IMG_HANDLE hDeviceKM, IMG_BOOL bResManCallback);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVEnumDCFormatsKM(IMG_HANDLE hDeviceKM,
+								   IMG_UINT32 *pui32Count,
+								   DISPLAY_FORMAT *psFormat);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVEnumDCDimsKM(IMG_HANDLE hDeviceKM,
+								DISPLAY_FORMAT *psFormat,
+								IMG_UINT32 *pui32Count,
+								DISPLAY_DIMS *psDim);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVGetDCSystemBufferKM(IMG_HANDLE hDeviceKM,
+									   IMG_HANDLE *phBuffer);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVGetDCInfoKM(IMG_HANDLE hDeviceKM,
+							   DISPLAY_INFO *psDisplayInfo);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVCreateDCSwapChainKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+									   IMG_HANDLE				hDeviceKM,
+									   IMG_UINT32				ui32Flags,
+									   DISPLAY_SURF_ATTRIBUTES	*psDstSurfAttrib,
+									   DISPLAY_SURF_ATTRIBUTES	*psSrcSurfAttrib,
+									   IMG_UINT32				ui32BufferCount,
+									   IMG_UINT32				ui32OEMFlags,
+									   IMG_HANDLE				*phSwapChain,
+									   IMG_UINT32				*pui32SwapChainID);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVDestroyDCSwapChainKM(IMG_HANDLE	hSwapChain);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetDCDstRectKM(IMG_HANDLE	hDeviceKM,
+								  IMG_HANDLE	hSwapChain,
+								  IMG_RECT	*psRect);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetDCSrcRectKM(IMG_HANDLE	hDeviceKM,
+								  IMG_HANDLE	hSwapChain,
+								  IMG_RECT	*psRect);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetDCDstColourKeyKM(IMG_HANDLE	hDeviceKM,
+									   IMG_HANDLE	hSwapChain,
+									   IMG_UINT32	ui32CKColour);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetDCSrcColourKeyKM(IMG_HANDLE	hDeviceKM,
+									IMG_HANDLE		hSwapChain,
+									IMG_UINT32		ui32CKColour);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVGetDCBuffersKM(IMG_HANDLE	hDeviceKM,
+								  IMG_HANDLE	hSwapChain,
+								  IMG_UINT32	*pui32BufferCount,
+								  IMG_HANDLE	*phBuffer);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSwapToDCBufferKM(IMG_HANDLE	hDeviceKM,
+									IMG_HANDLE	hBuffer,
+									IMG_UINT32	ui32SwapInterval,
+									IMG_HANDLE	hPrivateTag,
+									IMG_UINT32	ui32ClipRectCount,
+									IMG_RECT	*psClipRect);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSwapToDCSystemKM(IMG_HANDLE	hDeviceKM,
+									IMG_HANDLE	hSwapChain);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVOpenBCDeviceKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+								  IMG_UINT32				ui32DeviceID,
+								  IMG_HANDLE				hDevCookie,
+								  IMG_HANDLE				*phDeviceKM);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVCloseBCDeviceKM(IMG_HANDLE hDeviceKM, IMG_BOOL bResManCallback);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVGetBCInfoKM(IMG_HANDLE	hDeviceKM,
+							   BUFFER_INFO	*psBufferInfo);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVGetBCBufferKM(IMG_HANDLE	hDeviceKM,
+								 IMG_UINT32	ui32BufferIndex,
+								 IMG_HANDLE	*phBuffer);
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetBCBufferIdFromTagKM(IMG_HANDLE hDeviceKM,
+								  IMG_UINT32 ui32BufferIndex,
+								  IMG_HANDLE pidx);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapDeviceClassMemoryKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+													   IMG_HANDLE				hDevMemContext,
+													   IMG_HANDLE				hDeviceClassBuffer,
+													   PVRSRV_KERNEL_MEM_INFO	**ppsMemInfo,
+													   IMG_HANDLE				*phOSMapInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapDeviceClassMemoryKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetFreeDeviceMemKM(IMG_UINT32 ui32Flags,
+												   IMG_SIZE_T *pui32Total,
+												   IMG_SIZE_T *pui32Free,
+												   IMG_SIZE_T *pui32LargestBlock);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVAllocSyncInfoKM(IMG_HANDLE					hDevCookie,
+												IMG_HANDLE					hDevMemContext,
+												PVRSRV_KERNEL_SYNC_INFO	**ppsKernelSyncInfo);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeSyncInfoKM(PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetMiscInfoKM(PVRSRV_MISC_INFO *psMiscInfo);
+
+PVRSRV_ERROR PVRSRVGetFBStatsKM(IMG_SIZE_T	*pui32Total,
+								IMG_SIZE_T	*pui32Available);
+
+IMG_IMPORT PVRSRV_ERROR
+PVRSRVAllocSharedSysMemoryKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+							 IMG_UINT32 				ui32Flags,
+							 IMG_SIZE_T 				ui32Size,
+							 PVRSRV_KERNEL_MEM_INFO		**ppsKernelMemInfo);
+
+IMG_IMPORT PVRSRV_ERROR
+PVRSRVFreeSharedSysMemoryKM(PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo);
+
+IMG_IMPORT PVRSRV_ERROR
+PVRSRVDissociateMemFromResmanKM(PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo);
+
+IMG_IMPORT PVRSRV_ERROR
+PVRSRVGetPageListKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo, struct page ***pvPageList,
+	unsigned long *numpages,
+	unsigned long *offset);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/pvrmmap.h b/drivers/gpu/drm/emgd/pvr/services4/include/pvrmmap.h
new file mode 100644
index 0000000..54f1121
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/pvrmmap.h
@@ -0,0 +1,32 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __PVRMMAP_H__
+#define __PVRMMAP_H__
+
+PVRSRV_ERROR PVRPMapKMem(IMG_HANDLE hModule, IMG_VOID **ppvLinAddr, IMG_VOID *pvLinAddrKM, IMG_HANDLE *phMappingInfo, IMG_HANDLE hMHandle);
+
+
+IMG_BOOL PVRUnMapKMem(IMG_HANDLE hModule, IMG_HANDLE hMappingInfo, IMG_HANDLE hMHandle);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/pvrsrv_errors.h b/drivers/gpu/drm/emgd/pvr/services4/include/pvrsrv_errors.h
new file mode 100755
index 0000000..7c8ed47
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/pvrsrv_errors.h
@@ -0,0 +1,189 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__PVRSRV_ERRORS_H__)
+#define __PVRSRV_ERRORS_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+	switch (eError)
+	{
+		case PVRSRV_OK:									return "No Errors";
+		case PVRSRV_ERROR_GENERIC:						return "Generic error - fix";
+		case PVRSRV_ERROR_OUT_OF_MEMORY:				return "PVRSRV_ERROR_OUT_OF_MEMORY - Unable to allocate required memory";
+		case PVRSRV_ERROR_TOO_FEW_BUFFERS:				return "PVRSRV_ERROR_TOO_FEW_BUFFERS";
+		case PVRSRV_ERROR_INVALID_PARAMS:				return "PVRSRV_ERROR_INVALID_PARAMS";
+		case PVRSRV_ERROR_INIT_FAILURE:					return "PVRSRV_ERROR_INIT_FAILURE";
+		case PVRSRV_ERROR_CANT_REGISTER_CALLBACK:		return "PVRSRV_ERROR_CANT_REGISTER_CALLBACK";
+		case PVRSRV_ERROR_INVALID_DEVICE:				return "PVRSRV_ERROR_INVALID_DEVICE";
+		case PVRSRV_ERROR_NOT_OWNER:					return "PVRSRV_ERROR_NOT_OWNER";
+		case PVRSRV_ERROR_BAD_MAPPING:					return "PVRSRV_ERROR_BAD_MAPPING";
+		case PVRSRV_ERROR_TIMEOUT:						return "PVRSRV_ERROR_TIMEOUT";
+		case PVRSRV_ERROR_FLIP_CHAIN_EXISTS:			return "PVRSRV_ERROR_FLIP_CHAIN_EXISTS";
+		case PVRSRV_ERROR_SCENE_INVALID:				return "PVRSRV_ERROR_SCENE_INVALID";
+		case PVRSRV_ERROR_STREAM_ERROR:					return "PVRSRV_ERROR_STREAM_ERROR";
+		case PVRSRV_ERROR_FAILED_DEPENDENCIES:			return "PVRSRV_ERROR_FAILED_DEPENDENCIES";
+		case PVRSRV_ERROR_CMD_NOT_PROCESSED:			return "PVRSRV_ERROR_CMD_NOT_PROCESSED";
+		case PVRSRV_ERROR_CMD_TOO_BIG:					return "PVRSRV_ERROR_CMD_TOO_BIG";
+		case PVRSRV_ERROR_DEVICE_REGISTER_FAILED:		return "PVRSRV_ERROR_DEVICE_REGISTER_FAILED";
+		case PVRSRV_ERROR_TOOMANYBUFFERS:				return "PVRSRV_ERROR_TOOMANYBUFFERS";
+		case PVRSRV_ERROR_NOT_SUPPORTED:				return "PVRSRV_ERROR_NOT_SUPPORTED - fix";
+		case PVRSRV_ERROR_PROCESSING_BLOCKED:			return "PVRSRV_ERROR_PROCESSING_BLOCKED";
+
+		case PVRSRV_ERROR_CANNOT_FLUSH_QUEUE:			return "PVRSRV_ERROR_CANNOT_FLUSH_QUEUE";
+		case PVRSRV_ERROR_CANNOT_GET_QUEUE_SPACE:		return "PVRSRV_ERROR_CANNOT_GET_QUEUE_SPACE";
+		case PVRSRV_ERROR_CANNOT_GET_RENDERDETAILS:		return "PVRSRV_ERROR_CANNOT_GET_RENDERDETAILS";
+		case PVRSRV_ERROR_RETRY:						return "PVRSRV_ERROR_RETRY";
+
+		case PVRSRV_ERROR_DDK_VERSION_MISMATCH:			return "PVRSRV_ERROR_DDK_VERSION_MISMATCH";
+		case PVRSRV_ERROR_BUILD_MISMATCH:				return "PVRSRV_ERROR_BUILD_MISMATCH";
+
+		case PVRSRV_ERROR_INVALID_FLAGS:				return "PVRSRV_ERROR_INVALID_FLAGS";
+		case PVRSRV_ERROR_FAILED_TO_REGISTER_PROCESS:	return "PVRSRV_ERROR_FAILED_TO_REGISTER_PROCESS";
+
+		case PVRSRV_ERROR_UNABLE_TO_LOAD_LIBRARY:		return "PVRSRV_ERROR_UNABLE_TO_LOAD_LIBRARY";
+		case PVRSRV_ERROR_UNABLE_GET_FUNC_ADDR:			return "PVRSRV_ERROR_UNABLE_GET_FUNC_ADDR";
+		case PVRSRV_ERROR_UNLOAD_LIBRARY_FAILED:		return "PVRSRV_ERROR_UNLOAD_LIBRARY_FAILED";
+
+		case PVRSRV_ERROR_BRIDGE_CALL_FAILED:			return "PVRSRV_ERROR_BRIDGE_CALL_FAILED";
+
+		case PVRSRV_ERROR_MMU_CONTEXT_NOT_FOUND:		return "PVRSRV_ERROR_MMU_CONTEXT_NOT_FOUND";
+		case PVRSRV_ERROR_BUFFER_DEVICE_NOT_FOUND:		return "PVRSRV_ERROR_BUFFER_DEVICE_NOT_FOUND";
+		case PVRSRV_ERROR_BUFFER_DEVICE_ALREADY_PRESENT:return "PVRSRV_ERROR_BUFFER_DEVICE_ALREADY_PRESENT";
+
+		case PVRSRV_ERROR_PCI_DEVICE_NOT_FOUND:			return "PVRSRV_ERROR_PCI_DEVICE_NOT_FOUND";
+		case PVRSRV_ERROR_PCI_CALL_FAILED:				return "PVRSRV_ERROR_PCI_CALL_FAILED";
+
+		case PVRSRV_ERROR_FAILED_TO_ALLOC_USER_MEM:		return "PVRSRV_ERROR_FAILED_TO_ALLOC_USER_MEM";
+		case PVRSRV_ERROR_FAILED_TO_ALLOC_VP_MEMORY:	return "PVRSRV_ERROR_FAILED_TO_ALLOC_VP_MEMORY";
+		case PVRSRV_ERROR_FAILED_TO_MAP_SHARED_PBDESC:	return "PVRSRV_ERROR_FAILED_TO_MAP_SHARED_PBDESC";
+		case PVRSRV_ERROR_FAILED_TO_GET_PHYS_ADDR:		return "PVRSRV_ERROR_FAILED_TO_GET_PHYS_ADDR";
+
+		case PVRSRV_ERROR_FAILED_TO_ALLOC_VIRT_MEMORY:	return "PVRSRV_ERROR_FAILED_TO_ALLOC_VIRT_MEMORY";
+		case PVRSRV_ERROR_FAILED_TO_COPY_VIRT_MEMORY:	return "PVRSRV_ERROR_FAILED_TO_COPY_VIRT_MEMORY";
+
+		case PVRSRV_ERROR_FAILED_TO_ALLOC_PAGES:		return "PVRSRV_ERROR_FAILED_TO_ALLOC_PAGES";
+		case PVRSRV_ERROR_FAILED_TO_FREE_PAGES:			return "PVRSRV_ERROR_FAILED_TO_FREE_PAGES";
+		case PVRSRV_ERROR_FAILED_TO_COPY_PAGES:			return "PVRSRV_ERROR_FAILED_TO_COPY_PAGES";
+		case PVRSRV_ERROR_UNABLE_TO_LOCK_PAGES:			return "PVRSRV_ERROR_UNABLE_TO_LOCK_PAGES";
+		case PVRSRV_ERROR_UNABLE_TO_UNLOCK_PAGES:		return "PVRSRV_ERROR_UNABLE_TO_UNLOCK_PAGES";
+		case PVRSRV_ERROR_STILL_MAPPED:					return "PVRSRV_ERROR_STILL_MAPPED";
+		case PVRSRV_ERROR_MAPPING_NOT_FOUND:			return "PVRSRV_ERROR_MAPPING_NOT_FOUND";
+		case PVRSRV_ERROR_PHYS_ADDRESS_EXCEEDS_32BIT:	return "PVRSRV_ERROR_PHYS_ADDRESS_EXCEEDS_32BIT";
+
+		case PVRSRV_ERROR_INVALID_SEGMENT_BLOCK:		return "PVRSRV_ERROR_INVALID_SEGMENT_BLOCK";
+		case PVRSRV_ERROR_INVALID_SGXDEVDATA:			return "PVRSRV_ERROR_INVALID_SGXDEVDATA";
+		case PVRSRV_ERROR_INVALID_DEVINFO:				return "PVRSRV_ERROR_INVALID_DEVINFO";
+		case PVRSRV_ERROR_INVALID_MEMINFO:				return "PVRSRV_ERROR_INVALID_MEMINFO";
+		case PVRSRV_ERROR_UNKNOWN_IOCTL:				return "PVRSRV_ERROR_UNKNOWN_IOCTL";
+		case PVRSRV_ERROR_INVALID_KERNELINFO:			return "PVRSRV_ERROR_INVALID_KERNELINFO";
+		case PVRSRV_ERROR_UNKNOWN_POWER_STATE:			return "PVRSRV_ERROR_UNKNOWN_POWER_STATE";
+		case PVRSRV_ERROR_INVALID_HANDLE_TYPE:			return "PVRSRV_ERROR_INVALID_HANDLE_TYPE";
+		case PVRSRV_ERROR_INVALID_WRAP_TYPE:			return "PVRSRV_ERROR_INVALID_WRAP_TYPE";
+		case PVRSRV_ERROR_INVALID_PHYS_ADDR:			return "PVRSRV_ERROR_INVALID_PHYS_ADDR";
+		case PVRSRV_ERROR_INVALID_HEAPINFO:				return "PVRSRV_ERROR_INVALID_HEAPINFO";
+		case PVRSRV_ERROR_FAILED_TO_RETRIEVE_HEAPINFO:	return "PVRSRV_ERROR_FAILED_TO_RETRIEVE_HEAPINFO";
+		case PVRSRV_ERROR_INVALID_MAP_REQUEST:			return "PVRSRV_ERROR_INVALID_MAP_REQUEST";
+		case PVRSRV_ERROR_INVALID_UNMAP_REQUEST:		return "PVRSRV_ERROR_INVALID_UNMAP_REQUEST";
+
+		case PVRSRV_ERROR_EXCEEDED_HW_LIMITS:			return "PVRSRV_ERROR_EXCEEDED_HW_LIMITS";
+		case PVRSRV_ERROR_NO_STAGING_BUFFER_ALLOCATED:	return "PVRSRV_ERROR_NO_STAGING_BUFFER_ALLOCATED";
+
+		case PVRSRV_ERROR_UNABLE_TO_CREATE_PERPROC_AREA:return "PVRSRV_ERROR_UNABLE_TO_CREATE_PERPROC_AREA";
+		case PVRSRV_ERROR_UNABLE_TO_CREATE_EVENT:		return "PVRSRV_ERROR_UNABLE_TO_CREATE_EVENT";
+		case PVRSRV_ERROR_UNABLE_TO_ENABLE_EVENT:		return "PVRSRV_ERROR_UNABLE_TO_ENABLE_EVENT";
+		case PVRSRV_ERROR_UNABLE_TO_REGISTER_EVENT:		return "PVRSRV_ERROR_UNABLE_TO_REGISTER_EVENT";
+		case PVRSRV_ERROR_UNABLE_TO_DESTROY_EVENT:		return "PVRSRV_ERROR_UNABLE_TO_DESTROY_EVENT";
+		case PVRSRV_ERROR_UNABLE_TO_CREATE_THREAD:		return "PVRSRV_ERROR_UNABLE_TO_CREATE_THREAD";
+		case PVRSRV_ERROR_UNABLE_TO_CLOSE_THREAD:		return "PVRSRV_ERROR_UNABLE_TO_CLOSE_THREAD";
+		case PVRSRV_ERROR_THREAD_READ_ERROR:			return "PVRSRV_ERROR_THREAD_READ_ERROR";
+		case PVRSRV_ERROR_UNABLE_TO_REGISTER_ISR_HANDLER:return "PVRSRV_ERROR_UNABLE_TO_REGISTER_ISR_HANDLER";
+		case PVRSRV_ERROR_UNABLE_TO_INSTALL_ISR:		return "PVRSRV_ERROR_UNABLE_TO_INSTALL_ISR";
+		case PVRSRV_ERROR_UNABLE_TO_UNINSTALL_ISR:		return "PVRSRV_ERROR_UNABLE_TO_UNINSTALL_ISR";
+		case PVRSRV_ERROR_ISR_ALREADY_INSTALLED:		return "PVRSRV_ERROR_ISR_ALREADY_INSTALLED";
+		case PVRSRV_ERROR_ISR_NOT_INSTALLED:			return "PVRSRV_ERROR_ISR_NOT_INSTALLED";
+		case PVRSRV_ERROR_UNABLE_TO_INITIALISE_INTERRUPT:return "PVRSRV_ERROR_UNABLE_TO_INITIALISE_INTERRUPT";
+		case PVRSRV_ERROR_UNABLE_TO_RETRIEVE_INFO:		return "PVRSRV_ERROR_UNABLE_TO_RETRIEVE_INFO";
+		case PVRSRV_ERROR_UNABLE_TO_DO_BACKWARDS_BLIT:	return "PVRSRV_ERROR_UNABLE_TO_DO_BACKWARDS_BLIT";
+		case PVRSRV_ERROR_UNABLE_TO_CLOSE_SERVICES :	return "PVRSRV_ERROR_UNABLE_TO_CLOSE_SERVICES";
+
+		case PVRSRV_ERROR_UNABLE_TO_LOCK_RESOURCE:		return "PVRSRV_ERROR_UNABLE_TO_LOCK_RESOURCE";
+		case PVRSRV_ERROR_INVALID_LOCK_ID:				return "PVRSRV_ERROR_INVALID_LOCK_ID";
+		case PVRSRV_ERROR_RESOURCE_NOT_LOCKED:			return "PVRSRV_ERROR_RESOURCE_NOT_LOCKED";
+
+		case PVRSRV_ERROR_FLIP_FAILED:					return "PVRSRV_ERROR_FLIP_FAILED";
+		case PVRSRV_ERROR_UNBLANK_DISPLAY_FAILED:		return "PVRSRV_ERROR_UNBLANK_DISPLAY_FAILED";
+
+		case PVRSRV_ERROR_TIMEOUT_POLLING_FOR_VALUE:	return "PVRSRV_ERROR_TIMEOUT_POLLING_FOR_VALUE";
+
+		case PVRSRV_ERROR_CREATE_RENDER_CONTEXT_FAILED:	return "PVRSRV_ERROR_CREATE_RENDER_CONTEXT_FAILED";
+		case PVRSRV_ERROR_UNKNOWN_PRIMARY_FRAG:			return "PVRSRV_ERROR_UNKNOWN_PRIMARY_FRAG";
+		case PVRSRV_ERROR_UNEXPECTED_SECONDARY_FRAG:	return "PVRSRV_ERROR_UNEXPECTED_SECONDARY_FRAG";
+		case PVRSRV_ERROR_UNEXPECTED_PRIMARY_FRAG:		return "PVRSRV_ERROR_UNEXPECTED_PRIMARY_FRAG";
+
+		case PVRSRV_ERROR_UNABLE_TO_INSERT_FENCE_ID:	return "PVRSRV_ERROR_UNABLE_TO_INSERT_FENCE_ID";
+
+		case PVRSRV_ERROR_PDUMP_NOT_AVAILABLE:			return "PVRSRV_ERROR_PDUMP_NOT_AVAILABLE";
+		case PVRSRV_ERROR_PDUMP_BUFFER_FULL:			return "PVRSRV_ERROR_PDUMP_BUFFER_FULL";
+		case PVRSRV_ERROR_PDUMP_BUF_OVERFLOW:			return "PVRSRV_ERROR_PDUMP_BUF_OVERFLOW";
+		case PVRSRV_ERROR_PDUMP_NOT_ACTIVE:				return "PVRSRV_ERROR_PDUMP_NOT_ACTIVE";
+		case PVRSRV_ERROR_INCOMPLETE_LINE_OVERLAPS_PAGES:return "PVRSRV_ERROR_INCOMPLETE_LINE_OVERLAPS_PAGES";
+
+		case PVRSRV_ERROR_MUTEX_DESTROY_FAILED:			return "PVRSRV_ERROR_MUTEX_DESTROY_FAILED";
+		case PVRSRV_ERROR_MUTEX_INTERRUPTIBLE_ERROR:	return "PVRSRV_ERROR_MUTEX_INTERRUPTIBLE_ERROR";
+
+		case PVRSRV_ERROR_INSUFFICIENT_SCRIPT_SPACE:	return "PVRSRV_ERROR_INSUFFICIENT_SCRIPT_SPACE";
+
+		case PVRSRV_ERROR_PROCESS_NOT_INITIALISED:		return "PVRSRV_ERROR_PROCESS_NOT_INITIALISED";
+		case PVRSRV_ERROR_PROCESS_NOT_FOUND:			return "PVRSRV_ERROR_PROCESS_NOT_FOUND";
+		case PVRSRV_ERROR_SRV_CONNECT_FAILED:			return "PVRSRV_ERROR_SRV_CONNECT_FAILED";
+		case PVRSRV_ERROR_SRV_DISCONNECT_FAILED:		return "PVRSRV_ERROR_SRV_DISCONNECT_FAILED";
+		case PVRSRV_ERROR_DEINT_PHASE_FAILED:			return "PVRSRV_ERROR_DEINT_PHASE_FAILED";
+		case PVRSRV_ERROR_INIT2_PHASE_FAILED:			return "PVRSRV_ERROR_INIT2_PHASE_FAILED";
+
+		case PVRSRV_ERROR_NO_DC_DEVICES_FOUND:			return "PVRSRV_ERROR_NO_DC_DEVICES_FOUND";
+		case PVRSRV_ERROR_UNABLE_TO_OPEN_DC_DEVICE:		return "PVRSRV_ERROR_UNABLE_TO_OPEN_DC_DEVICE";
+		case PVRSRV_ERROR_NO_DEVICEDATA_FOUND:			return "PVRSRV_ERROR_NO_DEVICEDATA_FOUND";
+
+		case PVRSRV_ERROR_UNKNOWN_PIXEL_FORMAT:			return "PVRSRV_ERROR_UNKNOWN_PIXEL_FORMAT";
+
+		case PVRSRV_ERROR_UNABLE_TO_INIT_TASK:			return "PVRSRV_ERROR_UNABLE_TO_INIT_TASK";
+		case PVRSRV_ERROR_UNABLE_TO_SCHEDULE_TASK:		return "PVRSRV_ERROR_UNABLE_TO_SCHEDULE_TASK";
+		case PVRSRV_ERROR_UNABLE_TO_KILL_TASK:			return "PVRSRV_ERROR_UNABLE_TO_KILL_TASK";
+
+		case PVRSRV_ERROR_UNABLE_TO_ENABLE_TIMER:		return "PVRSRV_ERROR_UNABLE_TO_ENABLE_TIMER";
+		case PVRSRV_ERROR_UNABLE_TO_DISABLE_TIMER:		return "PVRSRV_ERROR_UNABLE_TO_DISABLE_TIMER";
+		case PVRSRV_ERROR_UNABLE_TO_REMOVE_TIMER:		return "PVRSRV_ERROR_UNABLE_TO_REMOVE_TIMER";
+
+		case PVRSRV_ERROR_FORCE_I32:					return "PVRSRV_ERROR_FORCE_I32";
+
+		default:
+			return "Unknown PVRSRV error number";
+	}
+
+#if defined (__cplusplus)
+}
+#endif
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/servicesint.h b/drivers/gpu/drm/emgd/pvr/services4/include/servicesint.h
new file mode 100644
index 0000000..4ef3939
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/servicesint.h
@@ -0,0 +1,272 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__SERVICESINT_H__)
+#define __SERVICESINT_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "services.h"
+#include "sysinfo.h"
+
+#define HWREC_DEFAULT_TIMEOUT	(500)
+
+#define DRIVERNAME_MAXLENGTH	(100)
+
+typedef enum _PVRSRV_MEMTYPE_
+{
+	PVRSRV_MEMTYPE_UNKNOWN		= 0,
+	PVRSRV_MEMTYPE_DEVICE		= 1,
+	PVRSRV_MEMTYPE_DEVICECLASS	= 2,
+	PVRSRV_MEMTYPE_WRAPPED		= 3,
+	PVRSRV_MEMTYPE_MAPPED		= 4,
+}
+PVRSRV_MEMTYPE;
+
+typedef struct _PVRSRV_KERNEL_MEM_INFO_
+{
+
+	IMG_PVOID				pvLinAddrKM;
+
+
+	IMG_DEV_VIRTADDR		sDevVAddr;
+
+
+	IMG_UINT32				ui32Flags;
+
+
+	IMG_SIZE_T				ui32AllocSize;
+
+
+	PVRSRV_MEMBLK			sMemBlk;
+
+
+	IMG_PVOID				pvSysBackupBuffer;
+
+
+	IMG_UINT32				ui32RefCount;
+
+
+	IMG_BOOL				bPendingFree;
+
+
+	#if defined(SUPPORT_MEMINFO_IDS)
+	#if !defined(USE_CODE)
+
+	IMG_UINT64				ui64Stamp;
+	#else
+	IMG_UINT32				dummy1;
+	IMG_UINT32				dummy2;
+	#endif
+	#endif
+
+
+	struct _PVRSRV_KERNEL_SYNC_INFO_	*psKernelSyncInfo;
+
+	PVRSRV_MEMTYPE			memType;
+
+} PVRSRV_KERNEL_MEM_INFO;
+
+
+typedef struct _PVRSRV_KERNEL_SYNC_INFO_
+{
+
+	PVRSRV_SYNC_DATA		*psSyncData;
+
+
+	IMG_DEV_VIRTADDR		sWriteOpsCompleteDevVAddr;
+
+
+	IMG_DEV_VIRTADDR		sReadOpsCompleteDevVAddr;
+
+
+	PVRSRV_KERNEL_MEM_INFO	*psSyncDataMemInfoKM;
+
+
+	IMG_HANDLE				hResItem;
+
+
+
+        IMG_UINT32              ui32RefCount;
+
+} PVRSRV_KERNEL_SYNC_INFO;
+
+typedef struct _PVRSRV_DEVICE_SYNC_OBJECT_
+{
+
+	IMG_UINT32			ui32ReadOpsPendingVal;
+	IMG_DEV_VIRTADDR	sReadOpsCompleteDevVAddr;
+	IMG_UINT32			ui32WriteOpsPendingVal;
+	IMG_DEV_VIRTADDR	sWriteOpsCompleteDevVAddr;
+} PVRSRV_DEVICE_SYNC_OBJECT;
+
+typedef struct _PVRSRV_SYNC_OBJECT
+{
+	PVRSRV_KERNEL_SYNC_INFO *psKernelSyncInfoKM;
+	IMG_UINT32				ui32WriteOpsPending;
+	IMG_UINT32				ui32ReadOpsPending;
+
+}PVRSRV_SYNC_OBJECT, *PPVRSRV_SYNC_OBJECT;
+
+typedef struct _PVRSRV_COMMAND
+{
+	IMG_SIZE_T			ui32CmdSize;
+	IMG_UINT32			ui32DevIndex;
+	IMG_UINT32			CommandType;
+	IMG_UINT32			ui32DstSyncCount;
+	IMG_UINT32			ui32SrcSyncCount;
+	PVRSRV_SYNC_OBJECT	*psDstSync;
+	PVRSRV_SYNC_OBJECT	*psSrcSync;
+	IMG_SIZE_T			ui32DataSize;
+	IMG_UINT32			ui32ProcessID;
+	IMG_VOID			*pvData;
+}PVRSRV_COMMAND, *PPVRSRV_COMMAND;
+
+
+typedef struct _PVRSRV_QUEUE_INFO_
+{
+	IMG_VOID			*pvLinQueueKM;
+	IMG_VOID			*pvLinQueueUM;
+	volatile IMG_SIZE_T	ui32ReadOffset;
+	volatile IMG_SIZE_T	ui32WriteOffset;
+	IMG_UINT32			*pui32KickerAddrKM;
+	IMG_UINT32			*pui32KickerAddrUM;
+	IMG_SIZE_T			ui32QueueSize;
+
+	IMG_UINT32			ui32ProcessID;
+
+	IMG_HANDLE			hMemBlock[2];
+
+	struct _PVRSRV_QUEUE_INFO_ *psNextKM;
+}PVRSRV_QUEUE_INFO;
+
+typedef PVRSRV_ERROR (*PFN_INSERT_CMD) (PVRSRV_QUEUE_INFO*,
+										PVRSRV_COMMAND**,
+										IMG_UINT32,
+										IMG_UINT16,
+										IMG_UINT32,
+										PVRSRV_KERNEL_SYNC_INFO*[],
+										IMG_UINT32,
+										PVRSRV_KERNEL_SYNC_INFO*[],
+										IMG_UINT32);
+typedef PVRSRV_ERROR (*PFN_SUBMIT_CMD) (PVRSRV_QUEUE_INFO*, PVRSRV_COMMAND*, IMG_BOOL);
+
+
+typedef struct PVRSRV_DEVICECLASS_BUFFER_TAG
+{
+	PFN_GET_BUFFER_ADDR		pfnGetBufferAddr;
+	IMG_HANDLE				hDevMemContext;
+	IMG_HANDLE				hExtDevice;
+	IMG_HANDLE				hExtBuffer;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+
+} PVRSRV_DEVICECLASS_BUFFER;
+
+
+typedef struct PVRSRV_CLIENT_DEVICECLASS_INFO_TAG
+{
+	IMG_HANDLE hDeviceKM;
+	IMG_HANDLE	hServices;
+} PVRSRV_CLIENT_DEVICECLASS_INFO;
+
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVGetWriteOpsPending)
+#endif
+static INLINE
+IMG_UINT32 PVRSRVGetWriteOpsPending(PVRSRV_KERNEL_SYNC_INFO *psSyncInfo, IMG_BOOL bIsReadOp)
+{
+	IMG_UINT32 ui32WriteOpsPending;
+
+	if(bIsReadOp)
+	{
+		ui32WriteOpsPending = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+	else
+	{
+
+
+
+		ui32WriteOpsPending = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+	}
+
+	return ui32WriteOpsPending;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVGetReadOpsPending)
+#endif
+static INLINE
+IMG_UINT32 PVRSRVGetReadOpsPending(PVRSRV_KERNEL_SYNC_INFO *psSyncInfo, IMG_BOOL bIsReadOp)
+{
+	IMG_UINT32 ui32ReadOpsPending;
+
+	if(bIsReadOp)
+	{
+		ui32ReadOpsPending = psSyncInfo->psSyncData->ui32ReadOpsPending++;
+	}
+	else
+	{
+		ui32ReadOpsPending = psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+
+	return ui32ReadOpsPending;
+}
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVQueueCommand(IMG_HANDLE hQueueInfo,
+								PVRSRV_COMMAND *psCommand);
+
+
+
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV
+PVRSRVGetMMUContextPDDevPAddr(const PVRSRV_CONNECTION *psConnection,
+                              IMG_HANDLE hDevMemContext,
+                              IMG_DEV_PHYADDR *sPDDevPAddr);
+
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV
+PVRSRVAllocSharedSysMem(const PVRSRV_CONNECTION *psConnection,
+						IMG_UINT32 ui32Flags,
+						IMG_SIZE_T ui32Size,
+						PVRSRV_CLIENT_MEM_INFO **ppsClientMemInfo);
+
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV
+PVRSRVFreeSharedSysMem(const PVRSRV_CONNECTION *psConnection,
+					   PVRSRV_CLIENT_MEM_INFO *psClientMemInfo);
+
+IMG_IMPORT PVRSRV_ERROR
+PVRSRVUnrefSharedSysMem(const PVRSRV_CONNECTION *psConnection,
+                        PVRSRV_CLIENT_MEM_INFO *psClientMemInfo);
+
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV
+PVRSRVMapMemInfoMem(const PVRSRV_CONNECTION *psConnection,
+                    IMG_HANDLE hKernelMemInfo,
+                    PVRSRV_CLIENT_MEM_INFO **ppsClientMemInfo);
+
+
+#if defined (__cplusplus)
+}
+#endif
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/sgx_bridge.h b/drivers/gpu/drm/emgd/pvr/services4/include/sgx_bridge.h
new file mode 100644
index 0000000..61a147c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/sgx_bridge.h
@@ -0,0 +1,473 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__SGX_BRIDGE_H__)
+#define __SGX_BRIDGE_H__
+
+#include "sgxapi_km.h"
+#include "sgxinfo.h"
+#include "pvr_bridge.h"
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+
+#define PVRSRV_BRIDGE_SGX_CMD_BASE (PVRSRV_BRIDGE_LAST_NON_DEVICE_CMD+1)
+#define PVRSRV_BRIDGE_SGX_GETCLIENTINFO			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+0)
+#define PVRSRV_BRIDGE_SGX_RELEASECLIENTINFO		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+1)
+#define PVRSRV_BRIDGE_SGX_GETINTERNALDEVINFO	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+2)
+#define PVRSRV_BRIDGE_SGX_DOKICK				PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+3)
+#define PVRSRV_BRIDGE_SGX_GETPHYSPAGEADDR		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+4)
+#define PVRSRV_BRIDGE_SGX_READREGISTRYDWORD		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+5)
+
+#define PVRSRV_BRIDGE_SGX_2DQUERYBLTSCOMPLETE   PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+9)
+
+#define PVRSRV_BRIDGE_SGX_GETMMUPDADDR  		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+10)
+
+#if defined(TRANSFER_QUEUE)
+#define PVRSRV_BRIDGE_SGX_SUBMITTRANSFER			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+13)
+#endif
+#define PVRSRV_BRIDGE_SGX_GETMISCINFO				PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+14)
+#define PVRSRV_BRIDGE_SGXINFO_FOR_SRVINIT			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+15)
+#define PVRSRV_BRIDGE_SGX_DEVINITPART2				PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+16)
+
+#define PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+17)
+#define PVRSRV_BRIDGE_SGX_UNREFSHAREDPBDESC			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+18)
+#define PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+19)
+#define PVRSRV_BRIDGE_SGX_REGISTER_HW_RENDER_CONTEXT	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+20)
+#define PVRSRV_BRIDGE_SGX_FLUSH_HW_RENDER_TARGET	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+21)
+#define PVRSRV_BRIDGE_SGX_UNREGISTER_HW_RENDER_CONTEXT	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+22)
+#if defined(SGX_FEATURE_2D_HARDWARE)
+#define PVRSRV_BRIDGE_SGX_SUBMIT2D					PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+23)
+#define PVRSRV_BRIDGE_SGX_REGISTER_HW_2D_CONTEXT	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+24)
+#define PVRSRV_BRIDGE_SGX_UNREGISTER_HW_2D_CONTEXT	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+25)
+#endif
+#define PVRSRV_BRIDGE_SGX_REGISTER_HW_TRANSFER_CONTEXT	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+26)
+#define PVRSRV_BRIDGE_SGX_UNREGISTER_HW_TRANSFER_CONTEXT	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+27)
+
+#define PVRSRV_BRIDGE_SGX_SCHEDULE_PROCESS_QUEUES		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+28)
+
+#if defined(SUPPORT_SGX_HWPERF)
+#define PVRSRV_BRIDGE_SGX_READ_DIFF_COUNTERS		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+29)
+#define PVRSRV_BRIDGE_SGX_READ_HWPERF_CB			PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+30)
+#endif
+
+#if defined(PDUMP)
+#define PVRSRV_BRIDGE_SGX_PDUMP_BUFFER_ARRAY		PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+31)
+#define PVRSRV_BRIDGE_SGX_PDUMP_3D_SIGNATURE_REGISTERS	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+32)
+#define PVRSRV_BRIDGE_SGX_PDUMP_COUNTER_REGISTERS	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+33)
+#define PVRSRV_BRIDGE_SGX_PDUMP_TA_SIGNATURE_REGISTERS	PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+34)
+#define PVRSRV_BRIDGE_SGX_PDUMP_HWPERFCB				PVRSRV_IOWR(PVRSRV_BRIDGE_SGX_CMD_BASE+35)
+#endif
+
+
+
+#define PVRSRV_BRIDGE_LAST_SGX_CMD (PVRSRV_BRIDGE_SGX_CMD_BASE+35)
+
+
+typedef struct PVRSRV_BRIDGE_IN_GETPHYSPAGEADDR
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevMemHeap;
+	IMG_DEV_VIRTADDR sDevVAddr;
+}PVRSRV_BRIDGE_IN_GETPHYSPAGEADDR;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GETPHYSPAGEADDR
+{
+	PVRSRV_ERROR		eError;
+	IMG_DEV_PHYADDR		DevPAddr;
+	IMG_CPU_PHYADDR		CpuPAddr;
+}PVRSRV_BRIDGE_OUT_GETPHYSPAGEADDR;
+
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_GETMMU_PDADDR_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	IMG_HANDLE				hDevMemContext;
+}PVRSRV_BRIDGE_IN_SGX_GETMMU_PDADDR;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_SGX_GETMMU_PDADDR_TAG
+{
+	IMG_DEV_PHYADDR 		sPDDevPAddr;
+	PVRSRV_ERROR			eError;
+}PVRSRV_BRIDGE_OUT_SGX_GETMMU_PDADDR;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GETCLIENTINFO_TAG
+{
+	IMG_UINT32					ui32BridgeFlags;
+	IMG_HANDLE					hDevCookie;
+}PVRSRV_BRIDGE_IN_GETCLIENTINFO;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GETINTERNALDEVINFO_TAG
+{
+	SGX_INTERNAL_DEVINFO	sSGXInternalDevInfo;
+	PVRSRV_ERROR				eError;
+}PVRSRV_BRIDGE_OUT_GETINTERNALDEVINFO;
+
+
+typedef struct PVRSRV_BRIDGE_IN_GETINTERNALDEVINFO_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+}PVRSRV_BRIDGE_IN_GETINTERNALDEVINFO;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_GETCLIENTINFO_TAG
+{
+	SGX_CLIENT_INFO  	sClientInfo;
+	PVRSRV_ERROR			eError;
+}PVRSRV_BRIDGE_OUT_GETCLIENTINFO;
+
+
+typedef struct PVRSRV_BRIDGE_IN_RELEASECLIENTINFO_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	SGX_CLIENT_INFO  	sClientInfo;
+}PVRSRV_BRIDGE_IN_RELEASECLIENTINFO;
+
+
+typedef struct PVRSRV_BRIDGE_IN_ISPBREAKPOLL_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+}PVRSRV_BRIDGE_IN_ISPBREAKPOLL;
+
+
+typedef struct PVRSRV_BRIDGE_IN_DOKICK_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	SGX_CCB_KICK			sCCBKick;
+}PVRSRV_BRIDGE_IN_DOKICK;
+
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_SCHEDULE_PROCESS_QUEUES_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+}PVRSRV_BRIDGE_IN_SGX_SCHEDULE_PROCESS_QUEUES;
+
+
+#if defined(TRANSFER_QUEUE)
+
+typedef struct PVRSRV_BRIDGE_IN_SUBMITTRANSFER_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	PVRSRV_TRANSFER_SGX_KICK 			sKick;
+}PVRSRV_BRIDGE_IN_SUBMITTRANSFER;
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+
+typedef struct PVRSRV_BRIDGE_IN_SUBMIT2D_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	PVRSRV_2D_SGX_KICK 				sKick;
+} PVRSRV_BRIDGE_IN_SUBMIT2D;
+#endif
+#endif
+
+
+typedef struct PVRSRV_BRIDGE_IN_READREGDWORD_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+    IMG_PCHAR				pszKey;
+    IMG_PCHAR				pszValue;
+}PVRSRV_BRIDGE_IN_READREGDWORD;
+
+
+typedef struct PVRSRV_BRIDGE_OUT_READREGDWORD_TAG
+{
+	PVRSRV_ERROR	eError;
+	IMG_UINT32		ui32Data;
+}PVRSRV_BRIDGE_OUT_READREGDWORD;
+
+
+typedef struct PVRSRV_BRIDGE_IN_SGXGETMISCINFO_TAG
+{
+	IMG_UINT32		ui32BridgeFlags;
+	IMG_HANDLE		hDevCookie;
+	SGX_MISC_INFO	*psMiscInfo;
+}PVRSRV_BRIDGE_IN_SGXGETMISCINFO;
+
+typedef struct PVRSRV_BRIDGE_IN_SGXINFO_FOR_SRVINIT_TAG
+{
+	IMG_UINT32		ui32BridgeFlags;
+	IMG_HANDLE		hDevCookie;
+}PVRSRV_BRIDGE_IN_SGXINFO_FOR_SRVINIT;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGXINFO_FOR_SRVINIT_TAG
+{
+	PVRSRV_ERROR			eError;
+	SGX_BRIDGE_INFO_FOR_SRVINIT	sInitInfo;
+}PVRSRV_BRIDGE_OUT_SGXINFO_FOR_SRVINIT;
+
+typedef struct PVRSRV_BRIDGE_IN_SGXDEVINITPART2_TAG
+{
+	IMG_UINT32		ui32BridgeFlags;
+	IMG_HANDLE		hDevCookie;
+	SGX_BRIDGE_INIT_INFO	sInitInfo;
+}PVRSRV_BRIDGE_IN_SGXDEVINITPART2;
+
+
+typedef struct PVRSRV_BRIDGE_IN_2DQUERYBLTSCOMPLETE_TAG
+{
+	IMG_UINT32				ui32BridgeFlags;
+	IMG_HANDLE				hDevCookie;
+	IMG_HANDLE				hKernSyncInfo;
+	IMG_BOOL				bWaitForComplete;
+}PVRSRV_BRIDGE_IN_2DQUERYBLTSCOMPLETE;
+
+
+#define PVRSRV_BRIDGE_SGX_SHAREDPBDESC_MAX_SUBMEMINFOS 10
+
+typedef struct PVRSRV_BRIDGE_IN_SGXFINDSHAREDPBDESC_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_BOOL bLockOnFailure;
+	IMG_UINT32 ui32TotalPBSize;
+}PVRSRV_BRIDGE_IN_SGXFINDSHAREDPBDESC;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGXFINDSHAREDPBDESC_TAG
+{
+	IMG_HANDLE hKernelMemInfo;
+	IMG_HANDLE hSharedPBDesc;
+	IMG_HANDLE hSharedPBDescKernelMemInfoHandle;
+	IMG_HANDLE hHWPBDescKernelMemInfoHandle;
+	IMG_HANDLE hBlockKernelMemInfoHandle;
+	IMG_HANDLE hHWBlockKernelMemInfoHandle;
+	IMG_HANDLE ahSharedPBDescSubKernelMemInfoHandles[PVRSRV_BRIDGE_SGX_SHAREDPBDESC_MAX_SUBMEMINFOS];
+	IMG_UINT32 ui32SharedPBDescSubKernelMemInfoHandlesCount;
+	PVRSRV_ERROR eError;
+}PVRSRV_BRIDGE_OUT_SGXFINDSHAREDPBDESC;
+
+typedef struct PVRSRV_BRIDGE_IN_SGXUNREFSHAREDPBDESC_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hSharedPBDesc;
+}PVRSRV_BRIDGE_IN_SGXUNREFSHAREDPBDESC;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGXUNREFSHAREDPBDESC_TAG
+{
+	PVRSRV_ERROR eError;
+}PVRSRV_BRIDGE_OUT_SGXUNREFSHAREDPBDESC;
+
+
+typedef struct PVRSRV_BRIDGE_IN_SGXADDSHAREDPBDESC_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_HANDLE hSharedPBDescKernelMemInfo;
+	IMG_HANDLE hHWPBDescKernelMemInfo;
+	IMG_HANDLE hBlockKernelMemInfo;
+	IMG_HANDLE hHWBlockKernelMemInfo;
+	IMG_UINT32 ui32TotalPBSize;
+	IMG_HANDLE *phKernelMemInfoHandles;
+	IMG_UINT32 ui32KernelMemInfoHandlesCount;
+}PVRSRV_BRIDGE_IN_SGXADDSHAREDPBDESC;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGXADDSHAREDPBDESC_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hSharedPBDesc;
+}PVRSRV_BRIDGE_OUT_SGXADDSHAREDPBDESC;
+
+
+#ifdef	PDUMP
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_BUFFER_ARRAY_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	SGX_KICKTA_DUMP_BUFFER *psBufferArray;
+	IMG_UINT32 ui32BufferArrayLength;
+	IMG_BOOL bDumpPolls;
+} PVRSRV_BRIDGE_IN_PDUMP_BUFFER_ARRAY;
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_3D_SIGNATURE_REGISTERS_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_UINT32 ui32DumpFrameNum;
+	IMG_BOOL bLastFrame;
+	IMG_UINT32 *pui32Registers;
+	IMG_UINT32 ui32NumRegisters;
+}PVRSRV_BRIDGE_IN_PDUMP_3D_SIGNATURE_REGISTERS;
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMPCOUNTER_REGISTERS_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_UINT32 ui32DumpFrameNum;
+	IMG_BOOL bLastFrame;
+	IMG_UINT32 *pui32Registers;
+	IMG_UINT32 ui32NumRegisters;
+}PVRSRV_BRIDGE_IN_PDUMP_COUNTER_REGISTERS;
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_TA_SIGNATURE_REGISTERS_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_UINT32 ui32DumpFrameNum;
+	IMG_UINT32 ui32TAKickCount;
+	IMG_BOOL bLastFrame;
+	IMG_UINT32 *pui32Registers;
+	IMG_UINT32 ui32NumRegisters;
+}PVRSRV_BRIDGE_IN_PDUMP_TA_SIGNATURE_REGISTERS;
+
+typedef struct PVRSRV_BRIDGE_IN_PDUMP_HWPERFCB_TAG
+{
+	IMG_UINT32			ui32BridgeFlags;
+	IMG_HANDLE			hDevCookie;
+	IMG_CHAR			szFileName[PVRSRV_PDUMP_MAX_FILENAME_SIZE];
+	IMG_UINT32			ui32FileOffset;
+	IMG_UINT32			ui32PDumpFlags;
+
+}PVRSRV_BRIDGE_IN_PDUMP_HWPERFCB;
+
+#endif
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_RENDER_CONTEXT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_DEV_VIRTADDR sHWRenderContextDevVAddr;
+}PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_RENDER_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_RENDER_CONTEXT_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hHWRenderContext;
+}PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_RENDER_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_RENDER_CONTEXT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_HANDLE hHWRenderContext;
+}PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_RENDER_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_TRANSFER_CONTEXT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_DEV_VIRTADDR sHWTransferContextDevVAddr;
+}PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_TRANSFER_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_TRANSFER_CONTEXT_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hHWTransferContext;
+}PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_TRANSFER_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_TRANSFER_CONTEXT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_HANDLE hHWTransferContext;
+}PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_TRANSFER_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_FLUSH_HW_RENDER_TARGET_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_DEV_VIRTADDR sHWRTDataSetDevVAddr;
+}PVRSRV_BRIDGE_IN_SGX_FLUSH_HW_RENDER_TARGET;
+
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+typedef struct PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_2D_CONTEXT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_DEV_VIRTADDR sHW2DContextDevVAddr;
+}PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_2D_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_2D_CONTEXT_TAG
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hHW2DContext;
+}PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_2D_CONTEXT;
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_2D_CONTEXT_TAG
+{
+	IMG_UINT32 ui32BridgeFlags;
+	IMG_HANDLE hDevCookie;
+	IMG_HANDLE hHW2DContext;
+}PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_2D_CONTEXT;
+
+#define	SGX2D_MAX_BLT_CMD_SIZ		256
+#endif
+
+
+typedef struct PVRSRV_BRIDGE_IN_SGX_READ_DIFF_COUNTERS_TAG
+{
+	IMG_UINT32		ui32BridgeFlags;
+	IMG_HANDLE		hDevCookie;
+	IMG_UINT32		ui32Reg;
+	IMG_BOOL		bNew;
+	IMG_UINT32		ui32New;
+	IMG_UINT32		ui32NewReset;
+	IMG_UINT32		ui32CountersReg;
+	IMG_UINT32		ui32Reg2;
+} PVRSRV_BRIDGE_IN_SGX_READ_DIFF_COUNTERS;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGX_READ_DIFF_COUNTERS_TAG
+{
+	PVRSRV_ERROR				eError;
+	IMG_UINT32					ui32Old;
+	IMG_BOOL					bActive;
+	PVRSRV_SGXDEV_DIFF_INFO		sDiffs;
+} PVRSRV_BRIDGE_OUT_SGX_READ_DIFF_COUNTERS;
+
+
+#if defined(SUPPORT_SGX_HWPERF)
+typedef struct PVRSRV_BRIDGE_IN_SGX_READ_HWPERF_CB_TAG
+{
+	IMG_UINT32					ui32BridgeFlags;
+	IMG_HANDLE					hDevCookie;
+	IMG_UINT32					ui32ArraySize;
+	PVRSRV_SGX_HWPERF_CB_ENTRY	*psHWPerfCBData;
+} PVRSRV_BRIDGE_IN_SGX_READ_HWPERF_CB;
+
+typedef struct PVRSRV_BRIDGE_OUT_SGX_READ_HWPERF_CB_TAG
+{
+	PVRSRV_ERROR		eError;
+	IMG_UINT32			ui32DataCount;
+	IMG_UINT32			ui32ClockSpeed;
+	IMG_UINT32			ui32HostTimeStamp;
+} PVRSRV_BRIDGE_OUT_SGX_READ_HWPERF_CB;
+#endif
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/sgx_mkif_km.h b/drivers/gpu/drm/emgd/pvr/services4/include/sgx_mkif_km.h
new file mode 100644
index 0000000..7a59274
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/sgx_mkif_km.h
@@ -0,0 +1,339 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__SGX_MKIF_KM_H__)
+#define __SGX_MKIF_KM_H__
+
+#include "img_types.h"
+#include "servicesint.h"
+#include "sgxapi_km.h"
+
+
+#if defined(SGX_FEATURE_MP)
+	#define SGX_REG_BANK_SHIFT 			(12)
+	#define SGX_REG_BANK_SIZE 			(0x4000)
+	#if defined(SGX541)
+		#define SGX_REG_BANK_BASE_INDEX		(1)
+		#define	SGX_REG_BANK_MASTER_INDEX	(SGX_REG_BANK_BASE_INDEX + SGX_FEATURE_MP_CORE_COUNT)
+	#else
+		#define SGX_REG_BANK_BASE_INDEX		(2)
+		#define	SGX_REG_BANK_MASTER_INDEX	(1)
+	#endif
+	#define SGX_MP_CORE_SELECT(x,i) 	(x + ((i + SGX_REG_BANK_BASE_INDEX) * SGX_REG_BANK_SIZE))
+	#define SGX_MP_MASTER_SELECT(x) 	(x + (SGX_REG_BANK_MASTER_INDEX * SGX_REG_BANK_SIZE))
+#else
+	#define SGX_MP_CORE_SELECT(x,i) 	(x)
+#endif
+
+
+typedef struct _SGXMKIF_COMMAND_
+{
+	IMG_UINT32				ui32ServiceAddress;
+	IMG_UINT32				ui32CacheControl;
+	IMG_UINT32				ui32Data[2];
+} SGXMKIF_COMMAND;
+
+
+typedef struct _PVRSRV_SGX_KERNEL_CCB_
+{
+	SGXMKIF_COMMAND		asCommands[256];
+} PVRSRV_SGX_KERNEL_CCB;
+
+
+typedef struct _PVRSRV_SGX_CCB_CTL_
+{
+	IMG_UINT32				ui32WriteOffset;
+	IMG_UINT32				ui32ReadOffset;
+} PVRSRV_SGX_CCB_CTL;
+
+
+typedef struct _SGXMKIF_HOST_CTL_
+{
+#if defined(PVRSRV_USSE_EDM_BREAKPOINTS)
+	IMG_UINT32				ui32BreakpointDisable;
+	IMG_UINT32				ui32Continue;
+#endif
+
+	volatile IMG_UINT32		ui32InitStatus;
+	volatile IMG_UINT32		ui32PowerStatus;
+	volatile IMG_UINT32		ui32CleanupStatus;
+#if defined(FIX_HW_BRN_28889)
+	volatile IMG_UINT32		ui32InvalStatus;
+#endif
+#if defined(SUPPORT_HW_RECOVERY)
+	IMG_UINT32				ui32uKernelDetectedLockups;
+	IMG_UINT32				ui32HostDetectedLockups;
+	IMG_UINT32				ui32HWRecoverySampleRate;
+#endif
+	IMG_UINT32				ui32uKernelTimerClock;
+	IMG_UINT32				ui32ActivePowManSampleRate;
+	IMG_UINT32				ui32InterruptFlags;
+	IMG_UINT32				ui32InterruptClearFlags;
+
+
+	IMG_UINT32				ui32NumActivePowerEvents;
+
+#if defined(SUPPORT_SGX_HWPERF)
+	IMG_UINT32			ui32HWPerfFlags;
+#endif
+
+
+	IMG_UINT32			ui32TimeWraps;
+} SGXMKIF_HOST_CTL;
+
+#define	SGXMKIF_CMDTA_CTRLFLAGS_READY			0x00000001
+typedef struct _SGXMKIF_CMDTA_SHARED_
+{
+	IMG_UINT32			ui32CtrlFlags;
+
+	IMG_UINT32			ui32NumTAStatusVals;
+	IMG_UINT32			ui32Num3DStatusVals;
+
+
+	IMG_UINT32			ui32TATQSyncWriteOpsPendingVal;
+	IMG_DEV_VIRTADDR	sTATQSyncWriteOpsCompleteDevVAddr;
+	IMG_UINT32			ui32TATQSyncReadOpsPendingVal;
+	IMG_DEV_VIRTADDR	sTATQSyncReadOpsCompleteDevVAddr;
+
+
+	IMG_UINT32			ui323DTQSyncWriteOpsPendingVal;
+	IMG_DEV_VIRTADDR	s3DTQSyncWriteOpsCompleteDevVAddr;
+	IMG_UINT32			ui323DTQSyncReadOpsPendingVal;
+	IMG_DEV_VIRTADDR	s3DTQSyncReadOpsCompleteDevVAddr;
+
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+
+	IMG_UINT32					ui32NumTASrcSyncs;
+	PVRSRV_DEVICE_SYNC_OBJECT	asTASrcSyncs[SGX_MAX_TA_SRC_SYNCS];
+	IMG_UINT32					ui32NumTADstSyncs;
+	PVRSRV_DEVICE_SYNC_OBJECT	asTADstSyncs[SGX_MAX_TA_DST_SYNCS];
+	IMG_UINT32					ui32Num3DSrcSyncs;
+	PVRSRV_DEVICE_SYNC_OBJECT	as3DSrcSyncs[SGX_MAX_3D_SRC_SYNCS];
+#else
+
+	IMG_UINT32			ui32NumSrcSyncs;
+	PVRSRV_DEVICE_SYNC_OBJECT	asSrcSyncs[SGX_MAX_SRC_SYNCS];
+#endif
+
+
+	PVRSRV_DEVICE_SYNC_OBJECT	sTA3DDependency;
+
+	CTL_STATUS			sCtlTAStatusInfo[SGX_MAX_TA_STATUS_VALS];
+	CTL_STATUS			sCtl3DStatusInfo[SGX_MAX_3D_STATUS_VALS];
+
+} SGXMKIF_CMDTA_SHARED;
+
+#define SGXTQ_MAX_STATUS						SGX_MAX_TRANSFER_STATUS_VALS + 2
+
+#define SGXMKIF_TQFLAGS_NOSYNCUPDATE			0x00000001
+#define SGXMKIF_TQFLAGS_KEEPPENDING				0x00000002
+#define SGXMKIF_TQFLAGS_TATQ_SYNC				0x00000004
+#define SGXMKIF_TQFLAGS_3DTQ_SYNC				0x00000008
+#if defined(SGX_FEATURE_FAST_RENDER_CONTEXT_SWITCH)
+#define SGXMKIF_TQFLAGS_CTXSWITCH				0x00000010
+#endif
+#define SGXMKIF_TQFLAGS_DUMMYTRANSFER			0x00000020
+
+typedef struct _SGXMKIF_TRANSFERCMD_SHARED_
+{
+
+
+	IMG_UINT32		ui32SrcReadOpPendingVal;
+	IMG_DEV_VIRTADDR	sSrcReadOpsCompleteDevAddr;
+
+	IMG_UINT32		ui32SrcWriteOpPendingVal;
+	IMG_DEV_VIRTADDR	sSrcWriteOpsCompleteDevAddr;
+
+
+
+	IMG_UINT32		ui32DstReadOpPendingVal;
+	IMG_DEV_VIRTADDR	sDstReadOpsCompleteDevAddr;
+
+	IMG_UINT32		ui32DstWriteOpPendingVal;
+	IMG_DEV_VIRTADDR	sDstWriteOpsCompleteDevAddr;
+
+
+	IMG_UINT32		ui32TASyncWriteOpsPendingVal;
+	IMG_DEV_VIRTADDR	sTASyncWriteOpsCompleteDevVAddr;
+	IMG_UINT32		ui32TASyncReadOpsPendingVal;
+	IMG_DEV_VIRTADDR	sTASyncReadOpsCompleteDevVAddr;
+
+
+	IMG_UINT32		ui323DSyncWriteOpsPendingVal;
+	IMG_DEV_VIRTADDR	s3DSyncWriteOpsCompleteDevVAddr;
+	IMG_UINT32		ui323DSyncReadOpsPendingVal;
+	IMG_DEV_VIRTADDR	s3DSyncReadOpsCompleteDevVAddr;
+
+	IMG_UINT32 		ui32NumStatusVals;
+	CTL_STATUS  	sCtlStatusInfo[SGXTQ_MAX_STATUS];
+} SGXMKIF_TRANSFERCMD_SHARED, *PSGXMKIF_TRANSFERCMD_SHARED;
+
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+typedef struct _SGXMKIF_2DCMD_SHARED_ {
+
+	IMG_UINT32			ui32NumSrcSync;
+	PVRSRV_DEVICE_SYNC_OBJECT	sSrcSyncData[SGX_MAX_2D_SRC_SYNC_OPS];
+
+
+	PVRSRV_DEVICE_SYNC_OBJECT	sDstSyncData;
+
+
+	PVRSRV_DEVICE_SYNC_OBJECT	sTASyncData;
+
+
+	PVRSRV_DEVICE_SYNC_OBJECT	s3DSyncData;
+} SGXMKIF_2DCMD_SHARED, *PSGXMKIF_2DCMD_SHARED;
+#endif
+
+
+typedef struct _SGXMKIF_HWDEVICE_SYNC_LIST_
+{
+	IMG_DEV_VIRTADDR	sAccessDevAddr;
+	IMG_UINT32			ui32NumSyncObjects;
+
+	PVRSRV_DEVICE_SYNC_OBJECT	asSyncData[1];
+} SGXMKIF_HWDEVICE_SYNC_LIST, *PSGXMKIF_HWDEVICE_SYNC_LIST;
+
+
+#define PVRSRV_USSE_EDM_INIT_COMPLETE			(1UL << 0)
+
+#define PVRSRV_USSE_EDM_POWMAN_IDLE_COMPLETE				(1UL << 2)
+#define PVRSRV_USSE_EDM_POWMAN_POWEROFF_COMPLETE			(1UL << 3)
+#define PVRSRV_USSE_EDM_POWMAN_POWEROFF_RESTART_IMMEDIATE	(1UL << 4)
+#define PVRSRV_USSE_EDM_POWMAN_NO_WORK						(1UL << 5)
+
+#define PVRSRV_USSE_EDM_INTERRUPT_HWR			(1UL << 0)
+#define PVRSRV_USSE_EDM_INTERRUPT_ACTIVE_POWER	(1UL << 1)
+
+#define PVRSRV_USSE_EDM_CLEANUPCMD_COMPLETE 	(1UL << 0)
+
+#if defined(FIX_HW_BRN_28889)
+#define PVRSRV_USSE_EDM_BIF_INVAL_COMPLETE 		(1UL << 0)1UL
+#endif
+
+#define PVRSRV_USSE_MISCINFO_READY		0x1UL
+#define PVRSRV_USSE_MISCINFO_GET_STRUCT_SIZES	0x2UL
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+#define PVRSRV_USSE_MISCINFO_MEMREAD			0x4UL
+
+#if !defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+#define PVRSRV_USSE_MISCINFO_MEMREAD_FAIL		0x1UL << 31
+#endif
+#endif
+
+
+#define	PVRSRV_CLEANUPCMD_RT		0x1
+#define	PVRSRV_CLEANUPCMD_RC		0x2
+#define	PVRSRV_CLEANUPCMD_TC		0x3
+#define	PVRSRV_CLEANUPCMD_2DC		0x4
+#define	PVRSRV_CLEANUPCMD_PB		0x5
+
+#define PVRSRV_POWERCMD_POWEROFF	0x1
+#define PVRSRV_POWERCMD_IDLE		0x2
+#define PVRSRV_POWERCMD_RESUME		0x3
+
+
+#if defined(SGX_FEATURE_BIF_NUM_DIRLISTS)
+#define SGX_BIF_DIR_LIST_INDEX_EDM	(SGX_FEATURE_BIF_NUM_DIRLISTS - 1)
+#else
+#define SGX_BIF_DIR_LIST_INDEX_EDM	(0)
+#endif
+
+#define	SGXMKIF_CC_INVAL_BIF_PT	0x1
+#define	SGXMKIF_CC_INVAL_BIF_PD	0x2
+#define SGXMKIF_CC_INVAL_BIF_SL	0x4
+#define SGXMKIF_CC_INVAL_DATA	0x8
+
+
+
+typedef struct _SGX_MISCINFO_STRUCT_SIZES_
+{
+#if defined (SGX_FEATURE_2D_HARDWARE)
+	IMG_UINT32	ui32Sizeof_2DCMD;
+	IMG_UINT32	ui32Sizeof_2DCMD_SHARED;
+#endif
+	IMG_UINT32	ui32Sizeof_CMDTA;
+	IMG_UINT32	ui32Sizeof_CMDTA_SHARED;
+	IMG_UINT32	ui32Sizeof_TRANSFERCMD;
+	IMG_UINT32	ui32Sizeof_TRANSFERCMD_SHARED;
+	IMG_UINT32	ui32Sizeof_3DREGISTERS;
+	IMG_UINT32	ui32Sizeof_HWPBDESC;
+	IMG_UINT32	ui32Sizeof_HWRENDERCONTEXT;
+	IMG_UINT32	ui32Sizeof_HWRENDERDETAILS;
+	IMG_UINT32	ui32Sizeof_HWRTDATA;
+	IMG_UINT32	ui32Sizeof_HWRTDATASET;
+	IMG_UINT32	ui32Sizeof_HWTRANSFERCONTEXT;
+	IMG_UINT32	ui32Sizeof_HOST_CTL;
+	IMG_UINT32	ui32Sizeof_COMMAND;
+} SGX_MISCINFO_STRUCT_SIZES;
+
+
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+typedef struct _PVRSRV_SGX_MISCINFO_MEMREAD
+{
+	IMG_DEV_VIRTADDR	sDevVAddr;
+	IMG_DEV_PHYADDR		sPDDevPAddr;
+} PVRSRV_SGX_MISCINFO_MEMREAD;
+#endif
+
+typedef struct _PVRSRV_SGX_MISCINFO_INFO
+{
+	IMG_UINT32						ui32MiscInfoFlags;
+	PVRSRV_SGX_MISCINFO_FEATURES	sSGXFeatures;
+	SGX_MISCINFO_STRUCT_SIZES		sSGXStructSizes;
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+	PVRSRV_SGX_MISCINFO_MEMREAD		sSGXMemReadData;
+#endif
+} PVRSRV_SGX_MISCINFO_INFO;
+
+#ifdef PVRSRV_USSE_EDM_STATUS_DEBUG
+#define SGXMK_TRACE_BUFFER_SIZE 512
+#endif
+
+#define SGXMKIF_HWPERF_CB_SIZE					0x100
+
+#if defined(SUPPORT_SGX_HWPERF)
+typedef struct _SGXMKIF_HWPERF_CB_ENTRY_
+{
+	IMG_UINT32	ui32FrameNo;
+	IMG_UINT32	ui32Type;
+	IMG_UINT32	ui32Ordinal;
+	IMG_UINT32	ui32TimeWraps;
+	IMG_UINT32	ui32Time;
+	IMG_UINT32	ui32Counters[PVRSRV_SGX_HWPERF_NUM_COUNTERS];
+} SGXMKIF_HWPERF_CB_ENTRY;
+
+typedef struct _SGXMKIF_HWPERF_CB_
+{
+	IMG_UINT32				ui32Woff;
+	IMG_UINT32				ui32Roff;
+	IMG_UINT32				ui32OrdinalGRAPHICS;
+	IMG_UINT32				ui32OrdinalMK_EXECUTION;
+	SGXMKIF_HWPERF_CB_ENTRY psHWPerfCBData[SGXMKIF_HWPERF_CB_SIZE];
+} SGXMKIF_HWPERF_CB;
+#endif
+
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/include/sgxinfo.h b/drivers/gpu/drm/emgd/pvr/services4/include/sgxinfo.h
new file mode 100644
index 0000000..2e85b61
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/include/sgxinfo.h
@@ -0,0 +1,288 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined (__SGXINFO_H__)
+#define __SGXINFO_H__
+
+#include "sgxscript.h"
+#include "servicesint.h"
+#include "services.h"
+#include "sgxapi_km.h"
+#include "sgx_mkif_km.h"
+
+
+#define SGX_MAX_DEV_DATA			24
+#define	SGX_MAX_INIT_MEM_HANDLES	16
+
+
+typedef struct _SGX_BRIDGE_INFO_FOR_SRVINIT
+{
+	IMG_DEV_PHYADDR sPDDevPAddr;
+	PVRSRV_HEAP_INFO asHeapInfo[PVRSRV_MAX_CLIENT_HEAPS];
+} SGX_BRIDGE_INFO_FOR_SRVINIT;
+
+
+typedef enum _SGXMKIF_CMD_TYPE_
+{
+	SGXMKIF_CMD_TA				= 0,
+	SGXMKIF_CMD_TRANSFER		= 1,
+	SGXMKIF_CMD_2D				= 2,
+	SGXMKIF_CMD_POWER			= 3,
+	SGXMKIF_CMD_CLEANUP			= 4,
+	SGXMKIF_CMD_GETMISCINFO		= 5,
+	SGXMKIF_CMD_PROCESS_QUEUES	= 6,
+	SGXMKIF_CMD_MAX				= 7,
+
+	SGXMKIF_CMD_FORCE_I32   	= -1,
+
+} SGXMKIF_CMD_TYPE;
+
+
+typedef struct _SGX_BRIDGE_INIT_INFO_
+{
+	IMG_HANDLE	hKernelCCBMemInfo;
+	IMG_HANDLE	hKernelCCBCtlMemInfo;
+	IMG_HANDLE	hKernelCCBEventKickerMemInfo;
+	IMG_HANDLE	hKernelSGXHostCtlMemInfo;
+	IMG_HANDLE	hKernelSGXTA3DCtlMemInfo;
+	IMG_HANDLE	hKernelSGXMiscMemInfo;
+
+	IMG_UINT32	aui32HostKickAddr[SGXMKIF_CMD_MAX];
+
+	SGX_INIT_SCRIPTS sScripts;
+
+	IMG_UINT32	ui32ClientBuildOptions;
+	SGX_MISCINFO_STRUCT_SIZES	sSGXStructSizes;
+
+#if defined(SGX_SUPPORT_HWPROFILING)
+	IMG_HANDLE	hKernelHWProfilingMemInfo;
+#endif
+#if defined(SUPPORT_SGX_HWPERF)
+	IMG_HANDLE	hKernelHWPerfCBMemInfo;
+#endif
+#if defined(PVRSRV_USSE_EDM_STATUS_DEBUG)
+	IMG_HANDLE	hKernelEDMStatusBufferMemInfo;
+#endif
+#if defined(SGX_FEATURE_OVERLAPPED_SPM)
+	IMG_HANDLE hKernelTmpRgnHeaderMemInfo;
+#endif
+#if defined(SGX_FEATURE_SPM_MODE_0)
+	IMG_HANDLE hKernelTmpDPMStateMemInfo;
+#endif
+
+	IMG_UINT32 ui32EDMTaskReg0;
+	IMG_UINT32 ui32EDMTaskReg1;
+
+	IMG_UINT32 ui32ClkGateStatusReg;
+	IMG_UINT32 ui32ClkGateStatusMask;
+#if defined(SGX_FEATURE_MP)
+	IMG_UINT32 ui32MasterClkGateStatusReg;
+	IMG_UINT32 ui32MasterClkGateStatusMask;
+#endif
+
+	IMG_UINT32 ui32CacheControl;
+
+	IMG_UINT32	asInitDevData[SGX_MAX_DEV_DATA];
+	IMG_HANDLE	asInitMemHandles[SGX_MAX_INIT_MEM_HANDLES];
+
+} SGX_BRIDGE_INIT_INFO;
+
+
+typedef struct _SGX_DEVICE_SYNC_LIST_
+{
+	PSGXMKIF_HWDEVICE_SYNC_LIST	psHWDeviceSyncList;
+
+	IMG_HANDLE				hKernelHWSyncListMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	*psHWDeviceSyncListClientMemInfo;
+	PVRSRV_CLIENT_MEM_INFO	*psAccessResourceClientMemInfo;
+
+	volatile IMG_UINT32		*pui32Lock;
+
+	struct _SGX_DEVICE_SYNC_LIST_	*psNext;
+
+
+	IMG_UINT32			ui32NumSyncObjects;
+	IMG_HANDLE			ahSyncHandles[1];
+} SGX_DEVICE_SYNC_LIST, *PSGX_DEVICE_SYNC_LIST;
+
+
+typedef struct _SGX_INTERNEL_STATUS_UPDATE_
+{
+	CTL_STATUS				sCtlStatus;
+	IMG_HANDLE				hKernelMemInfo;
+
+	IMG_UINT32				ui32LastStatusUpdateDumpVal;
+} SGX_INTERNEL_STATUS_UPDATE;
+
+
+typedef struct _SGX_CCB_KICK_
+{
+	SGXMKIF_COMMAND		sCommand;
+	IMG_HANDLE			hCCBKernelMemInfo;
+
+	IMG_UINT32	ui32NumDstSyncObjects;
+	IMG_HANDLE	hKernelHWSyncListMemInfo;
+
+
+	IMG_HANDLE	*pahDstSyncHandles;
+
+	IMG_UINT32	ui32NumTAStatusVals;
+	IMG_UINT32	ui32Num3DStatusVals;
+
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+	SGX_INTERNEL_STATUS_UPDATE	asTAStatusUpdate[SGX_MAX_TA_STATUS_VALS];
+	SGX_INTERNEL_STATUS_UPDATE	as3DStatusUpdate[SGX_MAX_3D_STATUS_VALS];
+#else
+	IMG_HANDLE	ahTAStatusSyncInfo[SGX_MAX_TA_STATUS_VALS];
+	IMG_HANDLE	ah3DStatusSyncInfo[SGX_MAX_3D_STATUS_VALS];
+#endif
+
+	IMG_BOOL	bFirstKickOrResume;
+#if (defined(NO_HARDWARE) || defined(PDUMP))
+	IMG_BOOL	bTerminateOrAbort;
+#endif
+#if defined(SUPPORT_SGX_HWPERF)
+	IMG_BOOL			bKickRender;
+#endif
+
+
+	IMG_UINT32	ui32CCBOffset;
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+
+	IMG_UINT32	ui32NumTASrcSyncs;
+	IMG_HANDLE	ahTASrcKernelSyncInfo[SGX_MAX_TA_SRC_SYNCS];
+	IMG_UINT32	ui32NumTADstSyncs;
+	IMG_HANDLE	ahTADstKernelSyncInfo[SGX_MAX_TA_DST_SYNCS];
+	IMG_UINT32	ui32Num3DSrcSyncs;
+	IMG_HANDLE	ah3DSrcKernelSyncInfo[SGX_MAX_3D_SRC_SYNCS];
+#else
+
+	IMG_UINT32	ui32NumSrcSyncs;
+	IMG_HANDLE	ahSrcKernelSyncInfo[SGX_MAX_SRC_SYNCS];
+#endif
+
+
+	IMG_BOOL	bTADependency;
+	IMG_HANDLE	hTA3DSyncInfo;
+
+	IMG_HANDLE	hTASyncInfo;
+	IMG_HANDLE	h3DSyncInfo;
+#if defined(PDUMP)
+	IMG_UINT32	ui32CCBDumpWOff;
+#endif
+#if defined(NO_HARDWARE)
+	IMG_UINT32	ui32WriteOpsPendingVal;
+#endif
+} SGX_CCB_KICK;
+
+
+#define SGX_KERNEL_USE_CODE_BASE_INDEX		15
+
+
+typedef struct _SGX_CLIENT_INFO_
+{
+	IMG_UINT32					ui32ProcessID;
+	IMG_VOID					*pvProcess;
+	PVRSRV_MISC_INFO			sMiscInfo;
+
+	IMG_UINT32					asDevData[SGX_MAX_DEV_DATA];
+
+} SGX_CLIENT_INFO;
+
+typedef struct _SGX_INTERNAL_DEVINFO_
+{
+	IMG_UINT32			ui32Flags;
+	IMG_HANDLE			hHostCtlKernelMemInfoHandle;
+	IMG_BOOL			bForcePTOff;
+} SGX_INTERNAL_DEVINFO;
+
+
+#if defined(TRANSFER_QUEUE)
+typedef struct _PVRSRV_TRANSFER_SGX_KICK_
+{
+	IMG_HANDLE		hCCBMemInfo;
+	IMG_UINT32		ui32SharedCmdCCBOffset;
+
+	IMG_DEV_VIRTADDR 	sHWTransferContextDevVAddr;
+
+	IMG_HANDLE		hTASyncInfo;
+	IMG_HANDLE		h3DSyncInfo;
+
+	IMG_UINT32		ui32NumSrcSync;
+	IMG_HANDLE		ahSrcSyncInfo[SGX_MAX_TRANSFER_SYNC_OPS];
+
+	IMG_UINT32		ui32NumDstSync;
+	IMG_HANDLE		ahDstSyncInfo[SGX_MAX_TRANSFER_SYNC_OPS];
+
+	IMG_UINT32		ui32Flags;
+
+	IMG_UINT32		ui32PDumpFlags;
+#if defined(PDUMP)
+	IMG_UINT32		ui32CCBDumpWOff;
+#endif
+
+	IMG_HANDLE		display;
+	IMG_UINT32		headline;
+	IMG_UINT32		footline;
+} PVRSRV_TRANSFER_SGX_KICK, *PPVRSRV_TRANSFER_SGX_KICK;
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+typedef struct _PVRSRV_2D_SGX_KICK_
+{
+	IMG_HANDLE		hCCBMemInfo;
+	IMG_UINT32		ui32SharedCmdCCBOffset;
+
+	IMG_DEV_VIRTADDR 	sHW2DContextDevVAddr;
+
+	IMG_UINT32		ui32NumSrcSync;
+	IMG_HANDLE		ahSrcSyncInfo[SGX_MAX_2D_SRC_SYNC_OPS];
+
+
+	IMG_HANDLE 		hDstSyncInfo;
+
+
+	IMG_HANDLE		hTASyncInfo;
+
+
+	IMG_HANDLE		h3DSyncInfo;
+
+	IMG_UINT32		ui32PDumpFlags;
+#if defined(PDUMP)
+	IMG_UINT32		ui32CCBDumpWOff;
+#endif
+} PVRSRV_2D_SGX_KICK, *PPVRSRV_2D_SGX_KICK;
+#endif
+#endif
+
+#define PVRSRV_SGX_DIFF_NUM_COUNTERS	9
+
+typedef struct _PVRSRV_SGXDEV_DIFF_INFO_
+{
+	IMG_UINT32	aui32Counters[PVRSRV_SGX_DIFF_NUM_COUNTERS];
+	IMG_UINT32	ui32Time[3];
+	IMG_UINT32	ui32Marker[2];
+} PVRSRV_SGXDEV_DIFF_INFO, *PPVRSRV_SGXDEV_DIFF_INFO;
+
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.c
new file mode 100644
index 0000000..efabffe
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.c
@@ -0,0 +1,3447 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+
+
+#include <stddef.h>
+
+#include "img_defs.h"
+#include "services.h"
+#include "pvr_bridge_km.h"
+#include "pvr_debug.h"
+#include "ra.h"
+#include "pvr_bridge.h"
+#if defined(SUPPORT_SGX)
+#include "sgx_bridge.h"
+#endif
+#if defined(SUPPORT_VGX)
+#include "vgx_bridge.h"
+#endif
+#if defined(SUPPORT_MSVDX)
+#include "msvdx_bridge.h"
+#endif
+#include "perproc.h"
+#include "device.h"
+#include "buffer_manager.h"
+
+#include "pdump_km.h"
+#include "sysconfig.h"
+
+#include "bridged_pvr_bridge.h"
+#if defined(SUPPORT_SGX)
+#include "bridged_sgx_bridge.h"
+#endif
+#if defined(SUPPORT_VGX)
+#include "bridged_vgx_bridge.h"
+#endif
+#if defined(SUPPORT_MSVDX)
+#include "bridged_msvdx_bridge.h"
+#endif
+
+#include "env_data.h"
+
+#if defined (__linux__)
+#include "mmap.h"
+#endif
+
+#include "srvkm.h"
+
+PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY g_BridgeDispatchTable[BRIDGE_DISPATCH_TABLE_ENTRY_COUNT];
+
+#if defined(DEBUG_BRIDGE_KM)
+PVRSRV_BRIDGE_GLOBAL_STATS g_BridgeGlobalStats;
+#endif
+
+#if defined(PVR_SECURE_HANDLES)
+static IMG_BOOL abSharedDeviceMemHeap[PVRSRV_MAX_CLIENT_HEAPS];
+static IMG_BOOL *pbSharedDeviceMemHeap = abSharedDeviceMemHeap;
+#else
+static IMG_BOOL *pbSharedDeviceMemHeap = (IMG_BOOL*)IMG_NULL;
+#endif
+
+
+#if defined(DEBUG_BRIDGE_KM)
+PVRSRV_ERROR
+CopyFromUserWrapper(PVRSRV_PER_PROCESS_DATA *pProcData,
+					IMG_UINT32 ui32BridgeID,
+					IMG_VOID *pvDest,
+					IMG_VOID *pvSrc,
+					IMG_UINT32 ui32Size)
+{
+	g_BridgeDispatchTable[ui32BridgeID].ui32CopyFromUserTotalBytes+=ui32Size;
+	g_BridgeGlobalStats.ui32TotalCopyFromUserBytes+=ui32Size;
+	return OSCopyFromUser(pProcData, pvDest, pvSrc, ui32Size);
+}
+PVRSRV_ERROR
+CopyToUserWrapper(PVRSRV_PER_PROCESS_DATA *pProcData,
+				  IMG_UINT32 ui32BridgeID,
+				  IMG_VOID *pvDest,
+				  IMG_VOID *pvSrc,
+				  IMG_UINT32 ui32Size)
+{
+	g_BridgeDispatchTable[ui32BridgeID].ui32CopyToUserTotalBytes+=ui32Size;
+	g_BridgeGlobalStats.ui32TotalCopyToUserBytes+=ui32Size;
+	return OSCopyToUser(pProcData, pvDest, pvSrc, ui32Size);
+}
+#endif
+
+
+static IMG_INT
+PVRSRVEnumerateDevicesBW(IMG_UINT32 ui32BridgeID,
+						 IMG_VOID *psBridgeIn,
+						 PVRSRV_BRIDGE_OUT_ENUMDEVICE *psEnumDeviceOUT,
+						 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ENUM_DEVICES);
+
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+
+	psEnumDeviceOUT->eError =
+		PVRSRVEnumerateDevicesKM(&psEnumDeviceOUT->ui32NumDevices,
+								 psEnumDeviceOUT->asDeviceIdentifier);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVAcquireDeviceDataBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_ACQUIRE_DEVICEINFO *psAcquireDevInfoIN,
+						  PVRSRV_BRIDGE_OUT_ACQUIRE_DEVICEINFO *psAcquireDevInfoOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ACQUIRE_DEVICEINFO);
+
+	psAcquireDevInfoOUT->eError =
+		PVRSRVAcquireDeviceDataKM(psAcquireDevInfoIN->uiDevIndex,
+								  psAcquireDevInfoIN->eDeviceType,
+								  &hDevCookieInt);
+	if(psAcquireDevInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+		psAcquireDevInfoOUT->eError =
+		PVRSRVAllocHandle(psPerProc->psHandleBase,
+						  &psAcquireDevInfoOUT->hDevCookie,
+						  hDevCookieInt,
+						  PVRSRV_HANDLE_TYPE_DEV_NODE,
+						  PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVCreateDeviceMemContextBW(IMG_UINT32 ui32BridgeID,
+							   PVRSRV_BRIDGE_IN_CREATE_DEVMEMCONTEXT *psCreateDevMemContextIN,
+							   PVRSRV_BRIDGE_OUT_CREATE_DEVMEMCONTEXT *psCreateDevMemContextOUT,
+							   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDevMemContextInt;
+	IMG_UINT32 i;
+	IMG_BOOL bCreated;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_CREATE_DEVMEMCONTEXT);
+
+
+	NEW_HANDLE_BATCH_OR_ERROR(psCreateDevMemContextOUT->eError, psPerProc, PVRSRV_MAX_CLIENT_HEAPS + 1);
+
+	psCreateDevMemContextOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psCreateDevMemContextIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psCreateDevMemContextOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psCreateDevMemContextOUT->eError =
+		PVRSRVCreateDeviceMemContextKM(hDevCookieInt,
+									   psPerProc,
+									   &hDevMemContextInt,
+									   &psCreateDevMemContextOUT->ui32ClientHeapCount,
+									   &psCreateDevMemContextOUT->sHeapInfo[0],
+									   &bCreated,
+									   pbSharedDeviceMemHeap);
+
+	if(psCreateDevMemContextOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	if(bCreated)
+	{
+		PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+						  &psCreateDevMemContextOUT->hDevMemContext,
+						  hDevMemContextInt,
+						  PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT,
+						  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+	}
+	else
+	{
+		psCreateDevMemContextOUT->eError =
+			PVRSRVFindHandle(psPerProc->psHandleBase,
+							 &psCreateDevMemContextOUT->hDevMemContext,
+							 hDevMemContextInt,
+							 PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+		if(psCreateDevMemContextOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	for(i = 0; i < psCreateDevMemContextOUT->ui32ClientHeapCount; i++)
+	{
+		IMG_HANDLE hDevMemHeapExt;
+
+#if defined(PVR_SECURE_HANDLES)
+		if(abSharedDeviceMemHeap[i])
+#endif
+		{
+
+			PVRSRVAllocHandleNR(psPerProc->psHandleBase, &hDevMemHeapExt,
+							  psCreateDevMemContextOUT->sHeapInfo[i].hDevMemHeap,
+							  PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP,
+							  PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+		}
+#if defined(PVR_SECURE_HANDLES)
+		else
+		{
+
+			if(bCreated)
+			{
+				PVRSRVAllocSubHandleNR(psPerProc->psHandleBase, &hDevMemHeapExt,
+									 psCreateDevMemContextOUT->sHeapInfo[i].hDevMemHeap,
+									 PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP,
+									 PVRSRV_HANDLE_ALLOC_FLAG_NONE,
+									 psCreateDevMemContextOUT->hDevMemContext);
+			}
+			else
+			{
+				psCreateDevMemContextOUT->eError =
+					PVRSRVFindHandle(psPerProc->psHandleBase, &hDevMemHeapExt,
+									 psCreateDevMemContextOUT->sHeapInfo[i].hDevMemHeap,
+									 PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP);
+				if(psCreateDevMemContextOUT->eError != PVRSRV_OK)
+				{
+					return 0;
+				}
+			}
+		}
+#endif
+		psCreateDevMemContextOUT->sHeapInfo[i].hDevMemHeap = hDevMemHeapExt;
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psCreateDevMemContextOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVDestroyDeviceMemContextBW(IMG_UINT32 ui32BridgeID,
+								PVRSRV_BRIDGE_IN_DESTROY_DEVMEMCONTEXT *psDestroyDevMemContextIN,
+								PVRSRV_BRIDGE_RETURN *psRetOUT,
+								PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDevMemContextInt;
+	IMG_BOOL bDestroyed;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_DESTROY_DEVMEMCONTEXT);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psDestroyDevMemContextIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevMemContextInt,
+						   psDestroyDevMemContextIN->hDevMemContext,
+						   PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVDestroyDeviceMemContextKM(hDevCookieInt, hDevMemContextInt, &bDestroyed);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	if(bDestroyed)
+	{
+		psRetOUT->eError =
+			PVRSRVReleaseHandle(psPerProc->psHandleBase,
+								psDestroyDevMemContextIN->hDevMemContext,
+								PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+	}
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVGetDeviceMemHeapInfoBW(IMG_UINT32 ui32BridgeID,
+							   PVRSRV_BRIDGE_IN_GET_DEVMEM_HEAPINFO *psGetDevMemHeapInfoIN,
+							   PVRSRV_BRIDGE_OUT_GET_DEVMEM_HEAPINFO *psGetDevMemHeapInfoOUT,
+							   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDevMemContextInt;
+	IMG_UINT32 i;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_DEVMEM_HEAPINFO);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psGetDevMemHeapInfoOUT->eError, psPerProc, PVRSRV_MAX_CLIENT_HEAPS);
+
+	psGetDevMemHeapInfoOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psGetDevMemHeapInfoIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psGetDevMemHeapInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetDevMemHeapInfoOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevMemContextInt,
+						   psGetDevMemHeapInfoIN->hDevMemContext,
+						   PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+
+	if(psGetDevMemHeapInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetDevMemHeapInfoOUT->eError =
+		PVRSRVGetDeviceMemHeapInfoKM(hDevCookieInt,
+									   hDevMemContextInt,
+									   &psGetDevMemHeapInfoOUT->ui32ClientHeapCount,
+									   &psGetDevMemHeapInfoOUT->sHeapInfo[0],
+									   pbSharedDeviceMemHeap);
+
+	if(psGetDevMemHeapInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	for(i = 0; i < psGetDevMemHeapInfoOUT->ui32ClientHeapCount; i++)
+	{
+		IMG_HANDLE hDevMemHeapExt;
+
+#if defined(PVR_SECURE_HANDLES)
+		if(abSharedDeviceMemHeap[i])
+#endif
+		{
+
+			PVRSRVAllocHandleNR(psPerProc->psHandleBase, &hDevMemHeapExt,
+							  psGetDevMemHeapInfoOUT->sHeapInfo[i].hDevMemHeap,
+							  PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP,
+							  PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+		}
+#if defined(PVR_SECURE_HANDLES)
+		else
+		{
+
+			psGetDevMemHeapInfoOUT->eError =
+				PVRSRVFindHandle(psPerProc->psHandleBase, &hDevMemHeapExt,
+								 psGetDevMemHeapInfoOUT->sHeapInfo[i].hDevMemHeap,
+								 PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP);
+			if(psGetDevMemHeapInfoOUT->eError != PVRSRV_OK)
+			{
+				return 0;
+			}
+		}
+#endif
+		psGetDevMemHeapInfoOUT->sHeapInfo[i].hDevMemHeap = hDevMemHeapExt;
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psGetDevMemHeapInfoOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+#if defined(OS_PVRSRV_ALLOC_DEVICE_MEM_BW)
+IMG_INT
+PVRSRVAllocDeviceMemBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM *psAllocDeviceMemIN,
+					   PVRSRV_BRIDGE_OUT_ALLOCDEVICEMEM *psAllocDeviceMemOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc);
+#else
+static IMG_INT
+PVRSRVAllocDeviceMemBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM *psAllocDeviceMemIN,
+					   PVRSRV_BRIDGE_OUT_ALLOCDEVICEMEM *psAllocDeviceMemOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo;
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDevMemHeapInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ALLOC_DEVICEMEM);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psAllocDeviceMemOUT->eError, psPerProc, 2);
+
+	psAllocDeviceMemOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psAllocDeviceMemIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psAllocDeviceMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psAllocDeviceMemOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevMemHeapInt,
+						   psAllocDeviceMemIN->hDevMemHeap,
+						   PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP);
+
+	if(psAllocDeviceMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psAllocDeviceMemOUT->eError =
+		PVRSRVAllocDeviceMemKM(hDevCookieInt,
+							   psPerProc,
+							   hDevMemHeapInt,
+							   psAllocDeviceMemIN->ui32Attribs,
+							   psAllocDeviceMemIN->ui32Size,
+							   psAllocDeviceMemIN->ui32Alignment,
+							   &psMemInfo,
+							   "" );
+
+	if(psAllocDeviceMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	OSMemSet(&psAllocDeviceMemOUT->sClientMemInfo,
+			 0,
+			 sizeof(psAllocDeviceMemOUT->sClientMemInfo));
+
+	psAllocDeviceMemOUT->sClientMemInfo.pvLinAddrKM =
+			psMemInfo->pvLinAddrKM;
+
+#if defined (__linux__)
+	psAllocDeviceMemOUT->sClientMemInfo.pvLinAddr = 0;
+#else
+	psAllocDeviceMemOUT->sClientMemInfo.pvLinAddr = psMemInfo->pvLinAddrKM;
+#endif
+	psAllocDeviceMemOUT->sClientMemInfo.sDevVAddr = psMemInfo->sDevVAddr;
+	psAllocDeviceMemOUT->sClientMemInfo.ui32Flags = psMemInfo->ui32Flags;
+	psAllocDeviceMemOUT->sClientMemInfo.ui32AllocSize = psMemInfo->ui32AllocSize;
+	psAllocDeviceMemOUT->sClientMemInfo.hMappingInfo = psMemInfo->sMemBlk.hOSMemHandle;
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psAllocDeviceMemOUT->sClientMemInfo.hKernelMemInfo,
+					  psMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+	if(psAllocDeviceMemIN->ui32Attribs & PVRSRV_MEM_NO_SYNCOBJ)
+	{
+
+		OSMemSet(&psAllocDeviceMemOUT->sClientSyncInfo,
+				 0,
+				 sizeof (PVRSRV_CLIENT_SYNC_INFO));
+		psAllocDeviceMemOUT->sClientMemInfo.psClientSyncInfo = IMG_NULL;
+		psAllocDeviceMemOUT->psKernelSyncInfo = IMG_NULL;
+	}
+	else
+	{
+
+		psAllocDeviceMemOUT->psKernelSyncInfo = psMemInfo->psKernelSyncInfo;
+
+		psAllocDeviceMemOUT->sClientSyncInfo.psSyncData =
+			psMemInfo->psKernelSyncInfo->psSyncData;
+		psAllocDeviceMemOUT->sClientSyncInfo.sWriteOpsCompleteDevVAddr =
+			psMemInfo->psKernelSyncInfo->sWriteOpsCompleteDevVAddr;
+		psAllocDeviceMemOUT->sClientSyncInfo.sReadOpsCompleteDevVAddr =
+			psMemInfo->psKernelSyncInfo->sReadOpsCompleteDevVAddr;
+
+		psAllocDeviceMemOUT->sClientSyncInfo.hMappingInfo =
+			psMemInfo->psKernelSyncInfo->psSyncDataMemInfoKM->sMemBlk.hOSMemHandle;
+
+		PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+							 &psAllocDeviceMemOUT->sClientSyncInfo.hKernelSyncInfo,
+							 psMemInfo->psKernelSyncInfo,
+							 PVRSRV_HANDLE_TYPE_SYNC_INFO,
+							 PVRSRV_HANDLE_ALLOC_FLAG_NONE,
+							 psAllocDeviceMemOUT->sClientMemInfo.hKernelMemInfo);
+
+		psAllocDeviceMemOUT->sClientMemInfo.psClientSyncInfo =
+			&psAllocDeviceMemOUT->sClientSyncInfo;
+
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psAllocDeviceMemOUT->eError, psPerProc);
+
+	return 0;
+}
+
+#endif
+
+static IMG_INT
+PVRSRVFreeDeviceMemBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_FREEDEVICEMEM *psFreeDeviceMemIN,
+					  PVRSRV_BRIDGE_RETURN *psRetOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_VOID *pvKernelMemInfo;
+
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_FREE_DEVICEMEM);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psFreeDeviceMemIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvKernelMemInfo,
+						   psFreeDeviceMemIN->psKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVFreeDeviceMemKM(hDevCookieInt, pvKernelMemInfo);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psFreeDeviceMemIN->psKernelMemInfo,
+							PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVExportDeviceMemBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_EXPORTDEVICEMEM *psExportDeviceMemIN,
+					  PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM *psExportDeviceMemOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_EXPORT_DEVICEMEM);
+
+
+	psExportDeviceMemOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psExportDeviceMemIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psExportDeviceMemOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVExportDeviceMemBW: can't find devcookie"));
+		return 0;
+	}
+
+
+	psExportDeviceMemOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, (IMG_PVOID *)&psKernelMemInfo,
+						   psExportDeviceMemIN->psKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	if(psExportDeviceMemOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVExportDeviceMemBW: can't find kernel meminfo"));
+		return 0;
+	}
+
+
+	psExportDeviceMemOUT->eError =
+		PVRSRVFindHandle(KERNEL_HANDLE_BASE,
+							 &psExportDeviceMemOUT->hMemInfo,
+							 psKernelMemInfo,
+							 PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psExportDeviceMemOUT->eError == PVRSRV_OK)
+	{
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVExportDeviceMemBW: allocation is already exported"));
+		return 0;
+	}
+
+
+	psExportDeviceMemOUT->eError = PVRSRVAllocHandle(KERNEL_HANDLE_BASE,
+													&psExportDeviceMemOUT->hMemInfo,
+													psKernelMemInfo,
+													PVRSRV_HANDLE_TYPE_MEM_INFO,
+													PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+	if (psExportDeviceMemOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVExportDeviceMemBW: failed to allocate handle from global handle list"));
+		return 0;
+	}
+
+
+	psKernelMemInfo->ui32Flags |= PVRSRV_MEM_EXPORTED;
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVMapDeviceMemoryBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY *psMapDevMemIN,
+							 PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY *psMapDevMemOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO	*psSrcKernelMemInfo = IMG_NULL;
+	PVRSRV_KERNEL_MEM_INFO	*psDstKernelMemInfo = IMG_NULL;
+	IMG_HANDLE				hDstDevMemHeap = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_MAP_DEV_MEMORY);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psMapDevMemOUT->eError, psPerProc, 2);
+
+
+	psMapDevMemOUT->eError = PVRSRVLookupHandle(KERNEL_HANDLE_BASE,
+												(IMG_VOID**)&psSrcKernelMemInfo,
+												psMapDevMemIN->hKernelMemInfo,
+												PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psMapDevMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	psMapDevMemOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+												&hDstDevMemHeap,
+												psMapDevMemIN->hDstDevMemHeap,
+												PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP);
+	if(psMapDevMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	psMapDevMemOUT->eError = PVRSRVMapDeviceMemoryKM(psPerProc,
+												  psSrcKernelMemInfo,
+												  hDstDevMemHeap,
+												  &psDstKernelMemInfo);
+	if(psMapDevMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	OSMemSet(&psMapDevMemOUT->sDstClientMemInfo,
+			 0,
+			 sizeof(psMapDevMemOUT->sDstClientMemInfo));
+	OSMemSet(&psMapDevMemOUT->sDstClientSyncInfo,
+			 0,
+			 sizeof(psMapDevMemOUT->sDstClientSyncInfo));
+
+	psMapDevMemOUT->sDstClientMemInfo.pvLinAddrKM =
+			psDstKernelMemInfo->pvLinAddrKM;
+
+	psMapDevMemOUT->sDstClientMemInfo.pvLinAddr = 0;
+	psMapDevMemOUT->sDstClientMemInfo.sDevVAddr = psDstKernelMemInfo->sDevVAddr;
+	psMapDevMemOUT->sDstClientMemInfo.ui32Flags = psDstKernelMemInfo->ui32Flags;
+	psMapDevMemOUT->sDstClientMemInfo.ui32AllocSize = psDstKernelMemInfo->ui32AllocSize;
+	psMapDevMemOUT->sDstClientMemInfo.hMappingInfo = psDstKernelMemInfo->sMemBlk.hOSMemHandle;
+
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psMapDevMemOUT->sDstClientMemInfo.hKernelMemInfo,
+					  psDstKernelMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+	psMapDevMemOUT->sDstClientSyncInfo.hKernelSyncInfo = IMG_NULL;
+	psMapDevMemOUT->psDstKernelSyncInfo = IMG_NULL;
+
+
+	if(psDstKernelMemInfo->psKernelSyncInfo)
+	{
+		psMapDevMemOUT->psDstKernelSyncInfo = psDstKernelMemInfo->psKernelSyncInfo;
+
+		psMapDevMemOUT->sDstClientSyncInfo.psSyncData =
+			psDstKernelMemInfo->psKernelSyncInfo->psSyncData;
+		psMapDevMemOUT->sDstClientSyncInfo.sWriteOpsCompleteDevVAddr =
+			psDstKernelMemInfo->psKernelSyncInfo->sWriteOpsCompleteDevVAddr;
+		psMapDevMemOUT->sDstClientSyncInfo.sReadOpsCompleteDevVAddr =
+			psDstKernelMemInfo->psKernelSyncInfo->sReadOpsCompleteDevVAddr;
+
+		psMapDevMemOUT->sDstClientSyncInfo.hMappingInfo =
+			psDstKernelMemInfo->psKernelSyncInfo->psSyncDataMemInfoKM->sMemBlk.hOSMemHandle;
+
+		psMapDevMemOUT->sDstClientMemInfo.psClientSyncInfo = &psMapDevMemOUT->sDstClientSyncInfo;
+
+		PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psMapDevMemOUT->sDstClientSyncInfo.hKernelSyncInfo,
+					  psDstKernelMemInfo->psKernelSyncInfo,
+					  PVRSRV_HANDLE_TYPE_SYNC_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+					  psMapDevMemOUT->sDstClientMemInfo.hKernelMemInfo);
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psMapDevMemOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVUnmapDeviceMemoryBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_UNMAP_DEV_MEMORY *psUnmapDevMemIN,
+							 PVRSRV_BRIDGE_RETURN *psRetOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO	*psKernelMemInfo = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_UNMAP_DEV_MEMORY);
+
+	psRetOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+											(IMG_VOID**)&psKernelMemInfo,
+											psUnmapDevMemIN->psKernelMemInfo,
+											PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVUnmapDeviceMemoryKM(psKernelMemInfo);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psUnmapDevMemIN->psKernelMemInfo,
+							PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	return 0;
+}
+
+
+
+static IMG_INT
+PVRSRVMapDeviceClassMemoryBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_MAP_DEVICECLASS_MEMORY *psMapDevClassMemIN,
+							 PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY *psMapDevClassMemOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo;
+	IMG_HANDLE hOSMapInfo;
+	IMG_HANDLE hDeviceClassBufferInt;
+	IMG_HANDLE hDevMemContextInt;
+	PVRSRV_HANDLE_TYPE eHandleType;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psMapDevClassMemOUT->eError, psPerProc, 2);
+
+
+	psMapDevClassMemOUT->eError =
+		PVRSRVLookupHandleAnyType(psPerProc->psHandleBase, &hDeviceClassBufferInt,
+								  &eHandleType,
+								  psMapDevClassMemIN->hDeviceClassBuffer);
+
+	if(psMapDevClassMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	psMapDevClassMemOUT->eError =
+	PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevMemContextInt,
+				   psMapDevClassMemIN->hDevMemContext,
+				   PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+
+	if(psMapDevClassMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	switch(eHandleType)
+	{
+#if defined(PVR_SECURE_HANDLES)
+		case PVRSRV_HANDLE_TYPE_DISP_BUFFER:
+		case PVRSRV_HANDLE_TYPE_BUF_BUFFER:
+#else
+		case PVRSRV_HANDLE_TYPE_NONE:
+#endif
+			break;
+		default:
+			psMapDevClassMemOUT->eError = PVRSRV_ERROR_GENERIC;
+			return 0;
+	}
+
+	psMapDevClassMemOUT->eError =
+		PVRSRVMapDeviceClassMemoryKM(psPerProc,
+									 hDevMemContextInt,
+									 hDeviceClassBufferInt,
+									 &psMemInfo,
+									 &hOSMapInfo);
+	if(psMapDevClassMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	OSMemSet(&psMapDevClassMemOUT->sClientMemInfo,
+			 0,
+			 sizeof(psMapDevClassMemOUT->sClientMemInfo));
+	OSMemSet(&psMapDevClassMemOUT->sClientSyncInfo,
+			 0,
+			 sizeof(psMapDevClassMemOUT->sClientSyncInfo));
+
+	psMapDevClassMemOUT->sClientMemInfo.pvLinAddrKM =
+			psMemInfo->pvLinAddrKM;
+
+	psMapDevClassMemOUT->sClientMemInfo.pvLinAddr = 0;
+	psMapDevClassMemOUT->sClientMemInfo.sDevVAddr = psMemInfo->sDevVAddr;
+	psMapDevClassMemOUT->sClientMemInfo.ui32Flags = psMemInfo->ui32Flags;
+	psMapDevClassMemOUT->sClientMemInfo.ui32AllocSize = psMemInfo->ui32AllocSize;
+	psMapDevClassMemOUT->sClientMemInfo.hMappingInfo = psMemInfo->sMemBlk.hOSMemHandle;
+	psMapDevClassMemOUT->sClientMemInfo.hOSMapInfo = hOSMapInfo;
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psMapDevClassMemOUT->sClientMemInfo.hKernelMemInfo,
+					  psMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE,
+					  psMapDevClassMemIN->hDeviceClassBuffer);
+
+	psMapDevClassMemOUT->sClientSyncInfo.hKernelSyncInfo = IMG_NULL;
+	psMapDevClassMemOUT->psKernelSyncInfo = IMG_NULL;
+
+
+	if(psMemInfo->psKernelSyncInfo)
+	{
+		psMapDevClassMemOUT->psKernelSyncInfo = psMemInfo->psKernelSyncInfo;
+
+		psMapDevClassMemOUT->sClientSyncInfo.psSyncData =
+			psMemInfo->psKernelSyncInfo->psSyncData;
+		psMapDevClassMemOUT->sClientSyncInfo.sWriteOpsCompleteDevVAddr =
+			psMemInfo->psKernelSyncInfo->sWriteOpsCompleteDevVAddr;
+		psMapDevClassMemOUT->sClientSyncInfo.sReadOpsCompleteDevVAddr =
+			psMemInfo->psKernelSyncInfo->sReadOpsCompleteDevVAddr;
+
+		psMapDevClassMemOUT->sClientSyncInfo.hMappingInfo =
+			psMemInfo->psKernelSyncInfo->psSyncDataMemInfoKM->sMemBlk.hOSMemHandle;
+
+		psMapDevClassMemOUT->sClientMemInfo.psClientSyncInfo = &psMapDevClassMemOUT->sClientSyncInfo;
+
+		PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+						  &psMapDevClassMemOUT->sClientSyncInfo.hKernelSyncInfo,
+						  psMemInfo->psKernelSyncInfo,
+						  PVRSRV_HANDLE_TYPE_SYNC_INFO,
+						  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+						  psMapDevClassMemOUT->sClientMemInfo.hKernelMemInfo);
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psMapDevClassMemOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVUnmapDeviceClassMemoryBW(IMG_UINT32 ui32BridgeID,
+							   PVRSRV_BRIDGE_IN_UNMAP_DEVICECLASS_MEMORY *psUnmapDevClassMemIN,
+							   PVRSRV_BRIDGE_RETURN *psRetOUT,
+							   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvKernelMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_UNMAP_DEVICECLASS_MEMORY);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvKernelMemInfo,
+						   psUnmapDevClassMemIN->psKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVUnmapDeviceClassMemoryKM(pvKernelMemInfo);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psUnmapDevClassMemIN->psKernelMemInfo,
+							PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	return 0;
+}
+
+
+#if defined(OS_PVRSRV_WRAP_EXT_MEM_BW)
+IMG_INT
+PVRSRVWrapExtMemoryBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_WRAP_EXT_MEMORY *psWrapExtMemIN,
+					  PVRSRV_BRIDGE_OUT_WRAP_EXT_MEMORY *psWrapExtMemOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc);
+#else
+static IMG_INT
+PVRSRVWrapExtMemoryBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_WRAP_EXT_MEMORY *psWrapExtMemIN,
+					  PVRSRV_BRIDGE_OUT_WRAP_EXT_MEMORY *psWrapExtMemOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDevMemContextInt;
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo;
+	IMG_UINT32 ui32PageTableSize = 0;
+	IMG_SYS_PHYADDR *psSysPAddr = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_WRAP_EXT_MEMORY);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psWrapExtMemOUT->eError, psPerProc, 2);
+
+
+	psWrapExtMemOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psWrapExtMemIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psWrapExtMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	psWrapExtMemOUT->eError =
+	PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevMemContextInt,
+				   psWrapExtMemIN->hDevMemContext,
+				   PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+
+	if(psWrapExtMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	if(psWrapExtMemIN->ui32NumPageTableEntries)
+	{
+		ui32PageTableSize = psWrapExtMemIN->ui32NumPageTableEntries
+						* sizeof(IMG_SYS_PHYADDR);
+
+		ASSIGN_AND_EXIT_ON_ERROR(psWrapExtMemOUT->eError,
+				  OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32PageTableSize,
+				  (IMG_VOID **)&psSysPAddr, 0,
+				  "Page Table"));
+
+		if(CopyFromUserWrapper(psPerProc,
+							   ui32BridgeID,
+							   psSysPAddr,
+							   psWrapExtMemIN->psSysPAddr,
+							   ui32PageTableSize) != PVRSRV_OK)
+		{
+			OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, 	ui32PageTableSize, (IMG_VOID *)psSysPAddr, 0);
+
+			return -EFAULT;
+		}
+	}
+
+	psWrapExtMemOUT->eError =
+		PVRSRVWrapExtMemoryKM(hDevCookieInt,
+							  psPerProc,
+							  hDevMemContextInt,
+							  psWrapExtMemIN->ui32ByteSize,
+							  psWrapExtMemIN->ui32PageOffset,
+							  psWrapExtMemIN->bPhysContig,
+							  psSysPAddr,
+							  psWrapExtMemIN->pvLinAddr,
+							  psWrapExtMemIN->ui32Flags,
+							  &psMemInfo);
+	if(psWrapExtMemIN->ui32NumPageTableEntries)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  ui32PageTableSize,
+			  (IMG_VOID *)psSysPAddr, 0);
+
+	}
+	if(psWrapExtMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psWrapExtMemOUT->sClientMemInfo.pvLinAddrKM =
+			psMemInfo->pvLinAddrKM;
+
+
+	psWrapExtMemOUT->sClientMemInfo.pvLinAddr = 0;
+	psWrapExtMemOUT->sClientMemInfo.sDevVAddr = psMemInfo->sDevVAddr;
+	psWrapExtMemOUT->sClientMemInfo.ui32Flags = psMemInfo->ui32Flags;
+	psWrapExtMemOUT->sClientMemInfo.ui32AllocSize = psMemInfo->ui32AllocSize;
+	psWrapExtMemOUT->sClientMemInfo.hMappingInfo = psMemInfo->sMemBlk.hOSMemHandle;
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psWrapExtMemOUT->sClientMemInfo.hKernelMemInfo,
+					  psMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+
+	psWrapExtMemOUT->sClientSyncInfo.psSyncData =
+		psMemInfo->psKernelSyncInfo->psSyncData;
+	psWrapExtMemOUT->sClientSyncInfo.sWriteOpsCompleteDevVAddr =
+		psMemInfo->psKernelSyncInfo->sWriteOpsCompleteDevVAddr;
+	psWrapExtMemOUT->sClientSyncInfo.sReadOpsCompleteDevVAddr =
+		psMemInfo->psKernelSyncInfo->sReadOpsCompleteDevVAddr;
+
+	psWrapExtMemOUT->sClientSyncInfo.hMappingInfo =
+		psMemInfo->psKernelSyncInfo->psSyncDataMemInfoKM->sMemBlk.hOSMemHandle;
+
+	psWrapExtMemOUT->sClientMemInfo.psClientSyncInfo = &psWrapExtMemOUT->sClientSyncInfo;
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psWrapExtMemOUT->sClientSyncInfo.hKernelSyncInfo,
+					  (IMG_HANDLE)psMemInfo->psKernelSyncInfo,
+					  PVRSRV_HANDLE_TYPE_SYNC_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE,
+					  psWrapExtMemOUT->sClientMemInfo.hKernelMemInfo);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psWrapExtMemOUT->eError, psPerProc);
+
+	return 0;
+}
+#endif
+
+static IMG_INT
+PVRSRVUnwrapExtMemoryBW(IMG_UINT32 ui32BridgeID,
+						PVRSRV_BRIDGE_IN_UNWRAP_EXT_MEMORY *psUnwrapExtMemIN,
+						PVRSRV_BRIDGE_RETURN *psRetOUT,
+						PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_UNWRAP_EXT_MEMORY);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvMemInfo,
+						   psUnwrapExtMemIN->hKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVUnwrapExtMemoryKM((PVRSRV_KERNEL_MEM_INFO *)pvMemInfo);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+						   psUnwrapExtMemIN->hKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVGetFreeDeviceMemBW(IMG_UINT32 ui32BridgeID,
+						 PVRSRV_BRIDGE_IN_GETFREEDEVICEMEM *psGetFreeDeviceMemIN,
+						 PVRSRV_BRIDGE_OUT_GETFREEDEVICEMEM *psGetFreeDeviceMemOUT,
+						 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GETFREE_DEVICEMEM);
+
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psGetFreeDeviceMemOUT->eError =
+		PVRSRVGetFreeDeviceMemKM(psGetFreeDeviceMemIN->ui32Flags,
+								 &psGetFreeDeviceMemOUT->ui32Total,
+								 &psGetFreeDeviceMemOUT->ui32Free,
+								 &psGetFreeDeviceMemOUT->ui32LargestBlock);
+
+	return 0;
+}
+
+static IMG_INT
+PVRMMapOSMemHandleToMMapDataBW(IMG_UINT32 ui32BridgeID,
+								  PVRSRV_BRIDGE_IN_MHANDLE_TO_MMAP_DATA *psMMapDataIN,
+								  PVRSRV_BRIDGE_OUT_MHANDLE_TO_MMAP_DATA *psMMapDataOUT,
+								  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_MHANDLE_TO_MMAP_DATA);
+
+#if defined (__linux__)
+	psMMapDataOUT->eError =
+		PVRMMapOSMemHandleToMMapData(psPerProc,
+										psMMapDataIN->hMHandle,
+										&psMMapDataOUT->ui32MMapOffset,
+										&psMMapDataOUT->ui32ByteOffset,
+										&psMMapDataOUT->ui32RealByteSize,
+										&psMMapDataOUT->ui32UserVAddr);
+#else
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	PVR_UNREFERENCED_PARAMETER(psMMapDataIN);
+
+	psMMapDataOUT->eError = PVRSRV_ERROR_NOT_SUPPORTED;
+#endif
+	return 0;
+}
+
+
+static IMG_INT
+PVRMMapReleaseMMapDataBW(IMG_UINT32 ui32BridgeID,
+								  PVRSRV_BRIDGE_IN_RELEASE_MMAP_DATA *psMMapDataIN,
+								  PVRSRV_BRIDGE_OUT_RELEASE_MMAP_DATA *psMMapDataOUT,
+								  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_RELEASE_MMAP_DATA);
+
+#if defined (__linux__)
+	psMMapDataOUT->eError =
+		PVRMMapReleaseMMapData(psPerProc,
+										psMMapDataIN->hMHandle,
+										&psMMapDataOUT->bMUnmap,
+										&psMMapDataOUT->ui32RealByteSize,
+										&psMMapDataOUT->ui32UserVAddr);
+#else
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	PVR_UNREFERENCED_PARAMETER(psMMapDataIN);
+
+	psMMapDataOUT->eError = PVRSRV_ERROR_NOT_SUPPORTED;
+#endif
+	return 0;
+}
+
+
+#ifdef PDUMP
+static IMG_INT
+PDumpIsCaptureFrameBW(IMG_UINT32 ui32BridgeID,
+					  IMG_VOID *psBridgeIn,
+					  PVRSRV_BRIDGE_OUT_PDUMP_ISCAPTURING *psPDumpIsCapturingOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_ISCAPTURING);
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psPDumpIsCapturingOUT->bIsCapturing = PDumpIsCaptureFrameKM();
+	psPDumpIsCapturingOUT->eError = PVRSRV_OK;
+
+	return 0;
+}
+
+static IMG_INT
+PDumpCommentBW(IMG_UINT32 ui32BridgeID,
+			   PVRSRV_BRIDGE_IN_PDUMP_COMMENT *psPDumpCommentIN,
+			   PVRSRV_BRIDGE_RETURN *psRetOUT,
+			   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_COMMENT);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError = PDumpCommentKM(&psPDumpCommentIN->szComment[0],
+									  psPDumpCommentIN->ui32Flags);
+	return 0;
+}
+
+static IMG_INT
+PDumpSetFrameBW(IMG_UINT32 ui32BridgeID,
+				PVRSRV_BRIDGE_IN_PDUMP_SETFRAME *psPDumpSetFrameIN,
+				PVRSRV_BRIDGE_RETURN *psRetOUT,
+				PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_SETFRAME);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError = PDumpSetFrameKM(psPDumpSetFrameIN->ui32Frame);
+
+	return 0;
+}
+
+static IMG_INT
+PDumpRegWithFlagsBW(IMG_UINT32 ui32BridgeID,
+					PVRSRV_BRIDGE_IN_PDUMP_DUMPREG *psPDumpRegDumpIN,
+					PVRSRV_BRIDGE_RETURN *psRetOUT,
+					PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_REG);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError =
+		PDumpRegWithFlagsKM(psPDumpRegDumpIN->sHWReg.ui32RegAddr,
+							psPDumpRegDumpIN->sHWReg.ui32RegVal,
+							psPDumpRegDumpIN->ui32Flags);
+
+	return 0;
+}
+
+static IMG_INT
+PDumpRegPolBW(IMG_UINT32 ui32BridgeID,
+			  PVRSRV_BRIDGE_IN_PDUMP_REGPOL *psPDumpRegPolIN,
+			  PVRSRV_BRIDGE_RETURN *psRetOUT,
+			  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_REGPOL);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError =
+		PDumpRegPolWithFlagsKM(psPDumpRegPolIN->sHWReg.ui32RegAddr,
+							   psPDumpRegPolIN->sHWReg.ui32RegVal,
+							   psPDumpRegPolIN->ui32Mask,
+							   psPDumpRegPolIN->ui32Flags);
+
+	return 0;
+}
+
+static IMG_INT
+PDumpMemPolBW(IMG_UINT32 ui32BridgeID,
+			  PVRSRV_BRIDGE_IN_PDUMP_MEMPOL *psPDumpMemPolIN,
+			  PVRSRV_BRIDGE_RETURN *psRetOUT,
+			  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_MEMPOL);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvMemInfo,
+						   psPDumpMemPolIN->psKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PDumpMemPolKM(((PVRSRV_KERNEL_MEM_INFO *)pvMemInfo),
+					  psPDumpMemPolIN->ui32Offset,
+					  psPDumpMemPolIN->ui32Value,
+					  psPDumpMemPolIN->ui32Mask,
+					  PDUMP_POLL_OPERATOR_EQUAL,
+					  psPDumpMemPolIN->ui32Flags,
+					  MAKEUNIQUETAG(pvMemInfo));
+
+	return 0;
+}
+
+static IMG_INT
+PDumpMemBW(IMG_UINT32 ui32BridgeID,
+		   PVRSRV_BRIDGE_IN_PDUMP_DUMPMEM *psPDumpMemDumpIN,
+		   PVRSRV_BRIDGE_RETURN *psRetOUT,
+		   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_DUMPMEM);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvMemInfo,
+						   psPDumpMemDumpIN->psKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PDumpMemUM(psPerProc,
+				   psPDumpMemDumpIN->pvAltLinAddr,
+				   psPDumpMemDumpIN->pvLinAddr,
+				   pvMemInfo,
+				   psPDumpMemDumpIN->ui32Offset,
+				   psPDumpMemDumpIN->ui32Bytes,
+				   psPDumpMemDumpIN->ui32Flags,
+				   MAKEUNIQUETAG(pvMemInfo));
+
+	return 0;
+}
+
+static IMG_INT
+PDumpBitmapBW(IMG_UINT32 ui32BridgeID,
+			  PVRSRV_BRIDGE_IN_PDUMP_BITMAP *psPDumpBitmapIN,
+			  PVRSRV_BRIDGE_RETURN *psRetOUT,
+			  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	PVR_UNREFERENCED_PARAMETER(ui32BridgeID);
+
+	psRetOUT->eError =
+		PDumpBitmapKM(&psPDumpBitmapIN->szFileName[0],
+					  psPDumpBitmapIN->ui32FileOffset,
+					  psPDumpBitmapIN->ui32Width,
+					  psPDumpBitmapIN->ui32Height,
+					  psPDumpBitmapIN->ui32StrideInBytes,
+					  psPDumpBitmapIN->sDevBaseAddr,
+					  psPDumpBitmapIN->ui32Size,
+					  psPDumpBitmapIN->ePixelFormat,
+					  psPDumpBitmapIN->eMemFormat,
+					  psPDumpBitmapIN->ui32Flags);
+
+	return 0;
+}
+
+static IMG_INT
+PDumpReadRegBW(IMG_UINT32 ui32BridgeID,
+			   PVRSRV_BRIDGE_IN_PDUMP_READREG *psPDumpReadRegIN,
+			   PVRSRV_BRIDGE_RETURN *psRetOUT,
+			   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_DUMPREADREG);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError =
+		PDumpReadRegKM(&psPDumpReadRegIN->szFileName[0],
+					   psPDumpReadRegIN->ui32FileOffset,
+					   psPDumpReadRegIN->ui32Address,
+					   psPDumpReadRegIN->ui32Size,
+					   psPDumpReadRegIN->ui32Flags);
+
+	return 0;
+}
+
+static IMG_INT
+PDumpDriverInfoBW(IMG_UINT32 ui32BridgeID,
+				  PVRSRV_BRIDGE_IN_PDUMP_DRIVERINFO *psPDumpDriverInfoIN,
+				  PVRSRV_BRIDGE_RETURN *psRetOUT,
+				  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 ui32PDumpFlags;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_DRIVERINFO);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	ui32PDumpFlags = 0;
+	if(psPDumpDriverInfoIN->bContinuous)
+	{
+		ui32PDumpFlags |= PDUMP_FLAGS_CONTINUOUS;
+	}
+	psRetOUT->eError =
+		PDumpDriverInfoKM(&psPDumpDriverInfoIN->szString[0],
+						  ui32PDumpFlags);
+
+	return 0;
+}
+
+static IMG_INT
+PDumpSyncDumpBW(IMG_UINT32 ui32BridgeID,
+				PVRSRV_BRIDGE_IN_PDUMP_DUMPSYNC *psPDumpSyncDumpIN,
+				PVRSRV_BRIDGE_RETURN *psRetOUT,
+				PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 ui32Bytes = psPDumpSyncDumpIN->ui32Bytes;
+	IMG_VOID *pvSyncInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_DUMPSYNC);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvSyncInfo,
+						   psPDumpSyncDumpIN->psKernelSyncInfo,
+						   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PDumpMemUM(psPerProc,
+				   psPDumpSyncDumpIN->pvAltLinAddr,
+				   IMG_NULL,
+				   ((PVRSRV_KERNEL_SYNC_INFO *)pvSyncInfo)->psSyncDataMemInfoKM,
+				   psPDumpSyncDumpIN->ui32Offset,
+				   ui32Bytes,
+				   0,
+				   MAKEUNIQUETAG(((PVRSRV_KERNEL_SYNC_INFO *)pvSyncInfo)->psSyncDataMemInfoKM));
+
+	return 0;
+}
+
+static IMG_INT
+PDumpSyncPolBW(IMG_UINT32 ui32BridgeID,
+			   PVRSRV_BRIDGE_IN_PDUMP_SYNCPOL *psPDumpSyncPolIN,
+			   PVRSRV_BRIDGE_RETURN *psRetOUT,
+			   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 ui32Offset;
+	IMG_VOID *pvSyncInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_SYNCPOL);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvSyncInfo,
+						   psPDumpSyncPolIN->psKernelSyncInfo,
+						   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	if(psPDumpSyncPolIN->bIsRead)
+	{
+		ui32Offset = offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete);
+	}
+	else
+	{
+		ui32Offset = offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete);
+	}
+
+	psRetOUT->eError =
+		PDumpMemPolKM(((PVRSRV_KERNEL_SYNC_INFO *)pvSyncInfo)->psSyncDataMemInfoKM,
+					  ui32Offset,
+					  psPDumpSyncPolIN->ui32Value,
+					  psPDumpSyncPolIN->ui32Mask,
+					  PDUMP_POLL_OPERATOR_EQUAL,
+					  0,
+					  MAKEUNIQUETAG(((PVRSRV_KERNEL_SYNC_INFO *)pvSyncInfo)->psSyncDataMemInfoKM));
+
+	return 0;
+}
+
+static IMG_INT
+PDumpPDRegBW(IMG_UINT32 ui32BridgeID,
+			 PVRSRV_BRIDGE_IN_PDUMP_DUMPPDREG *psPDumpPDRegDumpIN,
+			 PVRSRV_BRIDGE_RETURN *psRetOUT,
+			 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_PDREG);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	PDumpPDReg(psPDumpPDRegDumpIN->sHWReg.ui32RegAddr,
+			   psPDumpPDRegDumpIN->sHWReg.ui32RegVal,
+			   PDUMP_PD_UNIQUETAG);
+
+	psRetOUT->eError = PVRSRV_OK;
+	return 0;
+}
+
+static IMG_INT
+PDumpCycleCountRegReadBW(IMG_UINT32 ui32BridgeID,
+						 PVRSRV_BRIDGE_IN_PDUMP_CYCLE_COUNT_REG_READ *psPDumpCycleCountRegReadIN,
+						 PVRSRV_BRIDGE_RETURN *psRetOUT,
+						 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_CYCLE_COUNT_REG_READ);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	PDumpCycleCountRegRead(psPDumpCycleCountRegReadIN->ui32RegOffset,
+						   psPDumpCycleCountRegReadIN->bLastFrame);
+
+	psRetOUT->eError = PVRSRV_OK;
+
+	return 0;
+}
+
+static IMG_INT
+PDumpPDDevPAddrBW(IMG_UINT32 ui32BridgeID,
+				  PVRSRV_BRIDGE_IN_PDUMP_DUMPPDDEVPADDR *psPDumpPDDevPAddrIN,
+				  PVRSRV_BRIDGE_RETURN *psRetOUT,
+				  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_DUMPPDDEVPADDR);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvMemInfo,
+						   psPDumpPDDevPAddrIN->hKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PDumpPDDevPAddrKM((PVRSRV_KERNEL_MEM_INFO *)pvMemInfo,
+						  psPDumpPDDevPAddrIN->ui32Offset,
+						  psPDumpPDDevPAddrIN->sPDDevPAddr,
+						  MAKEUNIQUETAG(pvMemInfo),
+						  PDUMP_PD_UNIQUETAG);
+	return 0;
+}
+
+static IMG_INT
+PDumpStartInitPhaseBW(IMG_UINT32 ui32BridgeID,
+					  IMG_VOID *psBridgeIn,
+					  PVRSRV_BRIDGE_RETURN *psRetOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_STARTINITPHASE);
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError = PDumpStartInitPhaseKM();
+
+	return 0;
+}
+
+static IMG_INT
+PDumpStopInitPhaseBW(IMG_UINT32 ui32BridgeID,
+					  IMG_VOID *psBridgeIn,
+					  PVRSRV_BRIDGE_RETURN *psRetOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_PDUMP_STOPINITPHASE);
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	psRetOUT->eError = PDumpStopInitPhaseKM();
+
+	return 0;
+}
+
+#endif
+
+
+static IMG_INT
+PVRSRVGetMiscInfoBW(IMG_UINT32 ui32BridgeID,
+					PVRSRV_BRIDGE_IN_GET_MISC_INFO *psGetMiscInfoIN,
+					PVRSRV_BRIDGE_OUT_GET_MISC_INFO *psGetMiscInfoOUT,
+					PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_ERROR eError;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_MISC_INFO);
+
+	OSMemCopy(&psGetMiscInfoOUT->sMiscInfo,
+	          &psGetMiscInfoIN->sMiscInfo,
+	          sizeof(PVRSRV_MISC_INFO));
+
+	if (((psGetMiscInfoIN->sMiscInfo.ui32StateRequest & PVRSRV_MISC_INFO_MEMSTATS_PRESENT) != 0) &&
+	    ((psGetMiscInfoIN->sMiscInfo.ui32StateRequest & PVRSRV_MISC_INFO_DDKVERSION_PRESENT) != 0))
+	{
+
+		psGetMiscInfoOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+
+	if (((psGetMiscInfoIN->sMiscInfo.ui32StateRequest & PVRSRV_MISC_INFO_MEMSTATS_PRESENT) != 0) ||
+	    ((psGetMiscInfoIN->sMiscInfo.ui32StateRequest & PVRSRV_MISC_INFO_DDKVERSION_PRESENT) != 0))
+	{
+
+		ASSIGN_AND_EXIT_ON_ERROR(psGetMiscInfoOUT->eError,
+				    OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+		                    psGetMiscInfoOUT->sMiscInfo.ui32MemoryStrLen,
+		                    (IMG_VOID **)&psGetMiscInfoOUT->sMiscInfo.pszMemoryStr, 0,
+							"Output string buffer"));
+
+		psGetMiscInfoOUT->eError = PVRSRVGetMiscInfoKM(&psGetMiscInfoOUT->sMiscInfo);
+
+
+		eError = CopyToUserWrapper(psPerProc, ui32BridgeID,
+		                           psGetMiscInfoIN->sMiscInfo.pszMemoryStr,
+		                           psGetMiscInfoOUT->sMiscInfo.pszMemoryStr,
+		                           psGetMiscInfoOUT->sMiscInfo.ui32MemoryStrLen);
+
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+		          psGetMiscInfoOUT->sMiscInfo.ui32MemoryStrLen,
+		         (IMG_VOID *)psGetMiscInfoOUT->sMiscInfo.pszMemoryStr, 0);
+		psGetMiscInfoOUT->sMiscInfo.pszMemoryStr = IMG_NULL;
+
+
+		psGetMiscInfoOUT->sMiscInfo.pszMemoryStr = psGetMiscInfoIN->sMiscInfo.pszMemoryStr;
+
+		if(eError != PVRSRV_OK)
+		{
+
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVGetMiscInfoBW Error copy to user"));
+			return -EFAULT;
+		}
+	}
+	else
+	{
+		psGetMiscInfoOUT->eError = PVRSRVGetMiscInfoKM(&psGetMiscInfoOUT->sMiscInfo);
+	}
+
+
+	if (psGetMiscInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	if (psGetMiscInfoIN->sMiscInfo.ui32StateRequest & PVRSRV_MISC_INFO_GLOBALEVENTOBJECT_PRESENT)
+	{
+		psGetMiscInfoOUT->eError = PVRSRVAllocHandle(psPerProc->psHandleBase,
+													&psGetMiscInfoOUT->sMiscInfo.sGlobalEventObject.hOSEventKM,
+													psGetMiscInfoOUT->sMiscInfo.sGlobalEventObject.hOSEventKM,
+													PVRSRV_HANDLE_TYPE_SHARED_EVENT_OBJECT,
+													PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+
+			if (psGetMiscInfoOUT->eError != PVRSRV_OK)
+			{
+				return 0;
+			}
+	}
+
+	if (psGetMiscInfoOUT->sMiscInfo.hSOCTimerRegisterOSMemHandle)
+	{
+
+		psGetMiscInfoOUT->eError = PVRSRVAllocHandle(psPerProc->psHandleBase,
+						  &psGetMiscInfoOUT->sMiscInfo.hSOCTimerRegisterOSMemHandle,
+						  psGetMiscInfoOUT->sMiscInfo.hSOCTimerRegisterOSMemHandle,
+						  PVRSRV_HANDLE_TYPE_SOC_TIMER,
+						  PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+
+		if (psGetMiscInfoOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVConnectBW(IMG_UINT32 ui32BridgeID,
+				IMG_VOID *psBridgeIn,
+				PVRSRV_BRIDGE_OUT_CONNECT_SERVICES *psConnectServicesOUT,
+				PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_CONNECT_SERVICES);
+
+	psConnectServicesOUT->hKernelServices = psPerProc->hPerProcData;
+	psConnectServicesOUT->eError = PVRSRV_OK;
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVDisconnectBW(IMG_UINT32 ui32BridgeID,
+				   IMG_VOID *psBridgeIn,
+				   PVRSRV_BRIDGE_RETURN *psRetOUT,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_DISCONNECT_SERVICES);
+
+
+	psRetOUT->eError = PVRSRV_OK;
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVEnumerateDCBW(IMG_UINT32 ui32BridgeID,
+					PVRSRV_BRIDGE_IN_ENUMCLASS *psEnumDispClassIN,
+					PVRSRV_BRIDGE_OUT_ENUMCLASS *psEnumDispClassOUT,
+					PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ENUM_CLASS);
+
+	psEnumDispClassOUT->eError =
+		PVRSRVEnumerateDCKM(psEnumDispClassIN->sDeviceClass,
+							&psEnumDispClassOUT->ui32NumDevices,
+							&psEnumDispClassOUT->ui32DevID[0]);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVOpenDCDeviceBW(IMG_UINT32 ui32BridgeID,
+					 PVRSRV_BRIDGE_IN_OPEN_DISPCLASS_DEVICE *psOpenDispClassDeviceIN,
+					 PVRSRV_BRIDGE_OUT_OPEN_DISPCLASS_DEVICE *psOpenDispClassDeviceOUT,
+					 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDispClassInfoInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_OPEN_DISPCLASS_DEVICE);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psOpenDispClassDeviceOUT->eError, psPerProc, 1);
+
+	psOpenDispClassDeviceOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psOpenDispClassDeviceIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psOpenDispClassDeviceOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psOpenDispClassDeviceOUT->eError =
+		PVRSRVOpenDCDeviceKM(psPerProc,
+							 psOpenDispClassDeviceIN->ui32DeviceID,
+							 hDevCookieInt,
+							 &hDispClassInfoInt);
+
+	if(psOpenDispClassDeviceOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psOpenDispClassDeviceOUT->hDeviceKM,
+					  hDispClassInfoInt,
+					  PVRSRV_HANDLE_TYPE_DISP_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+	COMMIT_HANDLE_BATCH_OR_ERROR(psOpenDispClassDeviceOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVCloseDCDeviceBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_CLOSE_DISPCLASS_DEVICE *psCloseDispClassDeviceIN,
+					  PVRSRV_BRIDGE_RETURN *psRetOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfoInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_CLOSE_DISPCLASS_DEVICE);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfoInt,
+						   psCloseDispClassDeviceIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVCloseDCDeviceKM(pvDispClassInfoInt, IMG_FALSE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psCloseDispClassDeviceIN->hDeviceKM,
+							PVRSRV_HANDLE_TYPE_DISP_INFO);
+	return 0;
+}
+
+static IMG_INT
+PVRSRVEnumDCFormatsBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_ENUM_DISPCLASS_FORMATS *psEnumDispClassFormatsIN,
+					  PVRSRV_BRIDGE_OUT_ENUM_DISPCLASS_FORMATS *psEnumDispClassFormatsOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfoInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ENUM_DISPCLASS_FORMATS);
+
+	psEnumDispClassFormatsOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfoInt,
+						   psEnumDispClassFormatsIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psEnumDispClassFormatsOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psEnumDispClassFormatsOUT->eError =
+		PVRSRVEnumDCFormatsKM(pvDispClassInfoInt,
+							  &psEnumDispClassFormatsOUT->ui32Count,
+							  psEnumDispClassFormatsOUT->asFormat);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVEnumDCDimsBW(IMG_UINT32 ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_ENUM_DISPCLASS_DIMS *psEnumDispClassDimsIN,
+				   PVRSRV_BRIDGE_OUT_ENUM_DISPCLASS_DIMS *psEnumDispClassDimsOUT,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfoInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ENUM_DISPCLASS_DIMS);
+
+	psEnumDispClassDimsOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfoInt,
+						   psEnumDispClassDimsIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+
+	if(psEnumDispClassDimsOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psEnumDispClassDimsOUT->eError =
+		PVRSRVEnumDCDimsKM(pvDispClassInfoInt,
+						   &psEnumDispClassDimsIN->sFormat,
+						   &psEnumDispClassDimsOUT->ui32Count,
+						   psEnumDispClassDimsOUT->asDim);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVGetDCSystemBufferBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_GET_DISPCLASS_SYSBUFFER *psGetDispClassSysBufferIN,
+						  PVRSRV_BRIDGE_OUT_GET_DISPCLASS_SYSBUFFER *psGetDispClassSysBufferOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hBufferInt;
+	IMG_VOID *pvDispClassInfoInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_DISPCLASS_SYSBUFFER);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psGetDispClassSysBufferOUT->eError, psPerProc, 1);
+
+	psGetDispClassSysBufferOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfoInt,
+						   psGetDispClassSysBufferIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psGetDispClassSysBufferOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetDispClassSysBufferOUT->eError =
+		PVRSRVGetDCSystemBufferKM(pvDispClassInfoInt,
+								  &hBufferInt);
+
+	if(psGetDispClassSysBufferOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+						 &psGetDispClassSysBufferOUT->hBuffer,
+						 hBufferInt,
+						 PVRSRV_HANDLE_TYPE_DISP_BUFFER,
+						 (PVRSRV_HANDLE_ALLOC_FLAG)(PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE | PVRSRV_HANDLE_ALLOC_FLAG_SHARED),
+						 psGetDispClassSysBufferIN->hDeviceKM);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psGetDispClassSysBufferOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVGetDCInfoBW(IMG_UINT32 ui32BridgeID,
+				  PVRSRV_BRIDGE_IN_GET_DISPCLASS_INFO *psGetDispClassInfoIN,
+				  PVRSRV_BRIDGE_OUT_GET_DISPCLASS_INFO *psGetDispClassInfoOUT,
+				  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_DISPCLASS_INFO);
+
+	psGetDispClassInfoOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psGetDispClassInfoIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psGetDispClassInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetDispClassInfoOUT->eError =
+		PVRSRVGetDCInfoKM(pvDispClassInfo,
+						  &psGetDispClassInfoOUT->sDisplayInfo);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVCreateDCSwapChainBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_CREATE_DISPCLASS_SWAPCHAIN *psCreateDispClassSwapChainIN,
+						  PVRSRV_BRIDGE_OUT_CREATE_DISPCLASS_SWAPCHAIN *psCreateDispClassSwapChainOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_HANDLE hSwapChainInt;
+	IMG_UINT32	ui32SwapChainID;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_CREATE_DISPCLASS_SWAPCHAIN);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psCreateDispClassSwapChainOUT->eError, psPerProc, 1);
+
+	psCreateDispClassSwapChainOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psCreateDispClassSwapChainIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+
+	if(psCreateDispClassSwapChainOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	ui32SwapChainID = psCreateDispClassSwapChainIN->ui32SwapChainID;
+
+	psCreateDispClassSwapChainOUT->eError =
+		PVRSRVCreateDCSwapChainKM(psPerProc, pvDispClassInfo,
+								  psCreateDispClassSwapChainIN->ui32Flags,
+								  &psCreateDispClassSwapChainIN->sDstSurfAttrib,
+								  &psCreateDispClassSwapChainIN->sSrcSurfAttrib,
+								  psCreateDispClassSwapChainIN->ui32BufferCount,
+								  psCreateDispClassSwapChainIN->ui32OEMFlags,
+								  &hSwapChainInt,
+								  &ui32SwapChainID);
+
+	if(psCreateDispClassSwapChainOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	psCreateDispClassSwapChainOUT->ui32SwapChainID = ui32SwapChainID;
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psCreateDispClassSwapChainOUT->hSwapChain,
+					  hSwapChainInt,
+					  PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE,
+					  psCreateDispClassSwapChainIN->hDeviceKM);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psCreateDispClassSwapChainOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVDestroyDCSwapChainBW(IMG_UINT32 ui32BridgeID,
+						   PVRSRV_BRIDGE_IN_DESTROY_DISPCLASS_SWAPCHAIN *psDestroyDispClassSwapChainIN,
+						   PVRSRV_BRIDGE_RETURN *psRetOUT,
+						   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvSwapChain;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_DESTROY_DISPCLASS_SWAPCHAIN);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvSwapChain,
+						   psDestroyDispClassSwapChainIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVDestroyDCSwapChainKM(pvSwapChain);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psDestroyDispClassSwapChainIN->hSwapChain,
+							PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVSetDCDstRectBW(IMG_UINT32 ui32BridgeID,
+					 PVRSRV_BRIDGE_IN_SET_DISPCLASS_RECT *psSetDispClassDstRectIN,
+					 PVRSRV_BRIDGE_RETURN *psRetOUT,
+					 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChain;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SET_DISPCLASS_DSTRECT);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psSetDispClassDstRectIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvSwapChain,
+						   psSetDispClassDstRectIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVSetDCDstRectKM(pvDispClassInfo,
+							 pvSwapChain,
+							 &psSetDispClassDstRectIN->sRect);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVSetDCSrcRectBW(IMG_UINT32 ui32BridgeID,
+					 PVRSRV_BRIDGE_IN_SET_DISPCLASS_RECT *psSetDispClassSrcRectIN,
+					 PVRSRV_BRIDGE_RETURN *psRetOUT,
+					 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChain;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SET_DISPCLASS_SRCRECT);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psSetDispClassSrcRectIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvSwapChain,
+						   psSetDispClassSrcRectIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVSetDCSrcRectKM(pvDispClassInfo,
+							 pvSwapChain,
+							 &psSetDispClassSrcRectIN->sRect);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVSetDCDstColourKeyBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_SET_DISPCLASS_COLOURKEY *psSetDispClassColKeyIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChain;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SET_DISPCLASS_DSTCOLOURKEY);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psSetDispClassColKeyIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvSwapChain,
+						   psSetDispClassColKeyIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVSetDCDstColourKeyKM(pvDispClassInfo,
+								  pvSwapChain,
+								  psSetDispClassColKeyIN->ui32CKColour);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVSetDCSrcColourKeyBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_SET_DISPCLASS_COLOURKEY *psSetDispClassColKeyIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChain;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SET_DISPCLASS_SRCCOLOURKEY);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psSetDispClassColKeyIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvSwapChain,
+						   psSetDispClassColKeyIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVSetDCSrcColourKeyKM(pvDispClassInfo,
+								  pvSwapChain,
+								  psSetDispClassColKeyIN->ui32CKColour);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVGetDCBuffersBW(IMG_UINT32 ui32BridgeID,
+					 PVRSRV_BRIDGE_IN_GET_DISPCLASS_BUFFERS *psGetDispClassBuffersIN,
+					 PVRSRV_BRIDGE_OUT_GET_DISPCLASS_BUFFERS *psGetDispClassBuffersOUT,
+					 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChain;
+	IMG_UINT32 i;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_DISPCLASS_BUFFERS);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psGetDispClassBuffersOUT->eError, psPerProc, PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS);
+
+	psGetDispClassBuffersOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psGetDispClassBuffersIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psGetDispClassBuffersOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetDispClassBuffersOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvSwapChain,
+						   psGetDispClassBuffersIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN);
+	if(psGetDispClassBuffersOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetDispClassBuffersOUT->eError =
+		PVRSRVGetDCBuffersKM(pvDispClassInfo,
+							 pvSwapChain,
+							 &psGetDispClassBuffersOUT->ui32BufferCount,
+							 psGetDispClassBuffersOUT->ahBuffer);
+	if (psGetDispClassBuffersOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	PVR_ASSERT(psGetDispClassBuffersOUT->ui32BufferCount <= PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS);
+
+	for(i = 0; i < psGetDispClassBuffersOUT->ui32BufferCount; i++)
+	{
+		IMG_HANDLE hBufferExt;
+
+
+		PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+							 &hBufferExt,
+							 psGetDispClassBuffersOUT->ahBuffer[i],
+							 PVRSRV_HANDLE_TYPE_DISP_BUFFER,
+							 (PVRSRV_HANDLE_ALLOC_FLAG)(PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE | PVRSRV_HANDLE_ALLOC_FLAG_SHARED),
+							 psGetDispClassBuffersIN->hSwapChain);
+
+		psGetDispClassBuffersOUT->ahBuffer[i] = hBufferExt;
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psGetDispClassBuffersOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVSwapToDCBufferBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_SWAP_DISPCLASS_TO_BUFFER *psSwapDispClassBufferIN,
+					   PVRSRV_BRIDGE_RETURN *psRetOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChainBuf;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SWAP_DISPCLASS_TO_BUFFER);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psSwapDispClassBufferIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupSubHandle(psPerProc->psHandleBase,
+						   &pvSwapChainBuf,
+						   psSwapDispClassBufferIN->hBuffer,
+						   PVRSRV_HANDLE_TYPE_DISP_BUFFER,
+						   psSwapDispClassBufferIN->hDeviceKM);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVSwapToDCBufferKM(pvDispClassInfo,
+							   pvSwapChainBuf,
+							   psSwapDispClassBufferIN->ui32SwapInterval,
+							   psSwapDispClassBufferIN->hPrivateTag,
+							   psSwapDispClassBufferIN->ui32ClipRectCount,
+							   psSwapDispClassBufferIN->sClipRect);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVSwapToDCSystemBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_SWAP_DISPCLASS_TO_SYSTEM *psSwapDispClassSystemIN,
+					   PVRSRV_BRIDGE_RETURN *psRetOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvDispClassInfo;
+	IMG_VOID *pvSwapChain;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SWAP_DISPCLASS_TO_SYSTEM);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvDispClassInfo,
+						   psSwapDispClassSystemIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_DISP_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupSubHandle(psPerProc->psHandleBase,
+						   &pvSwapChain,
+						   psSwapDispClassSystemIN->hSwapChain,
+						   PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN,
+						   psSwapDispClassSystemIN->hDeviceKM);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+	psRetOUT->eError =
+		PVRSRVSwapToDCSystemKM(pvDispClassInfo,
+							   pvSwapChain);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVOpenBCDeviceBW(IMG_UINT32 ui32BridgeID,
+					 PVRSRV_BRIDGE_IN_OPEN_BUFFERCLASS_DEVICE *psOpenBufferClassDeviceIN,
+					 PVRSRV_BRIDGE_OUT_OPEN_BUFFERCLASS_DEVICE *psOpenBufferClassDeviceOUT,
+					 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hBufClassInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_OPEN_BUFFERCLASS_DEVICE);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psOpenBufferClassDeviceOUT->eError, psPerProc, 1);
+
+	psOpenBufferClassDeviceOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psOpenBufferClassDeviceIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psOpenBufferClassDeviceOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psOpenBufferClassDeviceOUT->eError =
+		PVRSRVOpenBCDeviceKM(psPerProc,
+							 psOpenBufferClassDeviceIN->ui32DeviceID,
+							 hDevCookieInt,
+							 &hBufClassInfo);
+	if(psOpenBufferClassDeviceOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psOpenBufferClassDeviceOUT->hDeviceKM,
+					  hBufClassInfo,
+					  PVRSRV_HANDLE_TYPE_BUF_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psOpenBufferClassDeviceOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVCloseBCDeviceBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_CLOSE_BUFFERCLASS_DEVICE *psCloseBufferClassDeviceIN,
+					  PVRSRV_BRIDGE_RETURN *psRetOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvBufClassInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_CLOSE_BUFFERCLASS_DEVICE);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvBufClassInfo,
+						   psCloseBufferClassDeviceIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_BUF_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVCloseBCDeviceKM(pvBufClassInfo, IMG_FALSE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+										   psCloseBufferClassDeviceIN->hDeviceKM,
+										   PVRSRV_HANDLE_TYPE_BUF_INFO);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVGetBCInfoBW(IMG_UINT32 ui32BridgeID,
+				  PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_INFO *psGetBufferClassInfoIN,
+				  PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_INFO *psGetBufferClassInfoOUT,
+				  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvBufClassInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_BUFFERCLASS_INFO);
+
+	psGetBufferClassInfoOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvBufClassInfo,
+						   psGetBufferClassInfoIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_BUF_INFO);
+	if(psGetBufferClassInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetBufferClassInfoOUT->eError =
+		PVRSRVGetBCInfoKM(pvBufClassInfo,
+						  &psGetBufferClassInfoOUT->sBufferInfo);
+	return 0;
+}
+
+static IMG_INT
+PVRSRVGetBCBufferBW(IMG_UINT32 ui32BridgeID,
+					PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_BUFFER *psGetBufferClassBufferIN,
+					PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_BUFFER *psGetBufferClassBufferOUT,
+					PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvBufClassInfo;
+	IMG_HANDLE hBufferInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_BUFFERCLASS_BUFFER);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psGetBufferClassBufferOUT->eError, psPerProc, 1);
+
+	psGetBufferClassBufferOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvBufClassInfo,
+						   psGetBufferClassBufferIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_BUF_INFO);
+	if(psGetBufferClassBufferOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetBufferClassBufferOUT->eError =
+		PVRSRVGetBCBufferKM(pvBufClassInfo,
+							psGetBufferClassBufferIN->ui32BufferIndex,
+							&hBufferInt);
+
+	if(psGetBufferClassBufferOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+						 &psGetBufferClassBufferOUT->hBuffer,
+						 hBufferInt,
+						 PVRSRV_HANDLE_TYPE_BUF_BUFFER,
+						 (PVRSRV_HANDLE_ALLOC_FLAG)(PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE |  PVRSRV_HANDLE_ALLOC_FLAG_SHARED),
+						 psGetBufferClassBufferIN->hDeviceKM);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psGetBufferClassBufferOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+static IMG_INT 
+PVRSRVGetBCBufferIdFromTagBW(IMG_UINT32 ui32BridgeID,
+					PVRSRV_BRIDGE_IN_GET_BUFFERCLASS_BUFFER *psGetBufferClassBufferIN,
+					PVRSRV_BRIDGE_OUT_GET_BUFFERCLASS_BUFFER *psGetBufferClassBufferOUT,
+					PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_VOID *pvBufClassInfo = IMG_NULL;
+	IMG_HANDLE pidx = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GET_BUFFERCLASS_BUFFER_ID_FROM_TAG);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psGetBufferClassBufferOUT->eError, psPerProc, 1);
+
+	psGetBufferClassBufferOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &pvBufClassInfo,
+						   psGetBufferClassBufferIN->hDeviceKM,
+						   PVRSRV_HANDLE_TYPE_BUF_INFO);
+	if(psGetBufferClassBufferOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetBufferClassBufferOUT->eError =
+		PVRSRVGetBCBufferIdFromTagKM(pvBufClassInfo,
+							psGetBufferClassBufferIN->ui32BufferIndex,
+							&pidx);
+
+	if(psGetBufferClassBufferOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetBufferClassBufferOUT->hBuffer = pidx;
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVAllocSharedSysMemoryBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_ALLOC_SHARED_SYS_MEM *psAllocSharedSysMemIN,
+							 PVRSRV_BRIDGE_OUT_ALLOC_SHARED_SYS_MEM *psAllocSharedSysMemOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_ALLOC_SHARED_SYS_MEM);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psAllocSharedSysMemOUT->eError, psPerProc, 1);
+
+	psAllocSharedSysMemOUT->eError =
+		PVRSRVAllocSharedSysMemoryKM(psPerProc,
+									 psAllocSharedSysMemIN->ui32Flags,
+									 psAllocSharedSysMemIN->ui32Size,
+									 &psKernelMemInfo);
+	if(psAllocSharedSysMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	OSMemSet(&psAllocSharedSysMemOUT->sClientMemInfo,
+			 0,
+			 sizeof(psAllocSharedSysMemOUT->sClientMemInfo));
+
+	psAllocSharedSysMemOUT->sClientMemInfo.pvLinAddrKM =
+			psKernelMemInfo->pvLinAddrKM;
+
+	psAllocSharedSysMemOUT->sClientMemInfo.pvLinAddr = 0;
+	psAllocSharedSysMemOUT->sClientMemInfo.ui32Flags =
+		psKernelMemInfo->ui32Flags;
+	psAllocSharedSysMemOUT->sClientMemInfo.ui32AllocSize =
+		psKernelMemInfo->ui32AllocSize;
+	psAllocSharedSysMemOUT->sClientMemInfo.hMappingInfo = psKernelMemInfo->sMemBlk.hOSMemHandle;
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psAllocSharedSysMemOUT->sClientMemInfo.hKernelMemInfo,
+					  psKernelMemInfo,
+					  PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psAllocSharedSysMemOUT->eError, psPerProc);
+
+	return 0;
+}
+
+static IMG_INT
+PVRSRVFreeSharedSysMemoryBW(IMG_UINT32 ui32BridgeID,
+							PVRSRV_BRIDGE_IN_FREE_SHARED_SYS_MEM *psFreeSharedSysMemIN,
+							PVRSRV_BRIDGE_OUT_FREE_SHARED_SYS_MEM *psFreeSharedSysMemOUT,
+							PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_FREE_SHARED_SYS_MEM);
+
+	psFreeSharedSysMemOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   (IMG_VOID **)&psKernelMemInfo,
+						   psFreeSharedSysMemIN->psKernelMemInfo,
+																   PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO);
+
+	if(psFreeSharedSysMemOUT->eError != PVRSRV_OK)
+		return 0;
+
+	psFreeSharedSysMemOUT->eError =
+		PVRSRVFreeSharedSysMemoryKM(psKernelMemInfo);
+	if(psFreeSharedSysMemOUT->eError != PVRSRV_OK)
+		return 0;
+
+	psFreeSharedSysMemOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psFreeSharedSysMemIN->psKernelMemInfo,
+							PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO);
+	return 0;
+}
+
+static IMG_INT
+PVRSRVMapMemInfoMemBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_MAP_MEMINFO_MEM *psMapMemInfoMemIN,
+					  PVRSRV_BRIDGE_OUT_MAP_MEMINFO_MEM *psMapMemInfoMemOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
+	PVRSRV_HANDLE_TYPE eHandleType;
+	IMG_HANDLE	hParent;
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_MAP_MEMINFO_MEM);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psMapMemInfoMemOUT->eError, psPerProc, 2);
+
+	psMapMemInfoMemOUT->eError =
+		PVRSRVLookupHandleAnyType(psPerProc->psHandleBase,
+						   (IMG_VOID **)&psKernelMemInfo,
+						   &eHandleType,
+						   psMapMemInfoMemIN->hKernelMemInfo);
+	if(psMapMemInfoMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	switch (eHandleType)
+	{
+#if defined(PVR_SECURE_HANDLES)
+		case PVRSRV_HANDLE_TYPE_MEM_INFO:
+		case PVRSRV_HANDLE_TYPE_MEM_INFO_REF:
+		case PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO:
+#else
+		case PVRSRV_HANDLE_TYPE_NONE:
+#endif
+			break;
+		default:
+			psMapMemInfoMemOUT->eError = PVRSRV_ERROR_GENERIC;
+			return 0;
+	}
+
+
+	psMapMemInfoMemOUT->eError =
+		PVRSRVGetParentHandle(psPerProc->psHandleBase,
+					&hParent,
+					psMapMemInfoMemIN->hKernelMemInfo,
+					eHandleType);
+	if (psMapMemInfoMemOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+	if (hParent == IMG_NULL)
+	{
+		hParent = psMapMemInfoMemIN->hKernelMemInfo;
+	}
+
+	OSMemSet(&psMapMemInfoMemOUT->sClientMemInfo,
+			 0,
+			 sizeof(psMapMemInfoMemOUT->sClientMemInfo));
+
+	psMapMemInfoMemOUT->sClientMemInfo.pvLinAddrKM =
+			psKernelMemInfo->pvLinAddrKM;
+
+	psMapMemInfoMemOUT->sClientMemInfo.pvLinAddr = 0;
+	psMapMemInfoMemOUT->sClientMemInfo.sDevVAddr =
+		psKernelMemInfo->sDevVAddr;
+	psMapMemInfoMemOUT->sClientMemInfo.ui32Flags =
+		psKernelMemInfo->ui32Flags;
+	psMapMemInfoMemOUT->sClientMemInfo.ui32AllocSize =
+		psKernelMemInfo->ui32AllocSize;
+	psMapMemInfoMemOUT->sClientMemInfo.hMappingInfo = psKernelMemInfo->sMemBlk.hOSMemHandle;
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psMapMemInfoMemOUT->sClientMemInfo.hKernelMemInfo,
+					  psKernelMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+					  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+					  hParent);
+
+	if(psKernelMemInfo->ui32Flags & PVRSRV_MEM_NO_SYNCOBJ)
+	{
+
+		OSMemSet(&psMapMemInfoMemOUT->sClientSyncInfo,
+				 0,
+				 sizeof (PVRSRV_CLIENT_SYNC_INFO));
+		psMapMemInfoMemOUT->psKernelSyncInfo = IMG_NULL;
+	}
+	else
+	{
+
+		psMapMemInfoMemOUT->sClientSyncInfo.psSyncData =
+			psKernelMemInfo->psKernelSyncInfo->psSyncData;
+		psMapMemInfoMemOUT->sClientSyncInfo.sWriteOpsCompleteDevVAddr =
+			psKernelMemInfo->psKernelSyncInfo->sWriteOpsCompleteDevVAddr;
+		psMapMemInfoMemOUT->sClientSyncInfo.sReadOpsCompleteDevVAddr =
+			psKernelMemInfo->psKernelSyncInfo->sReadOpsCompleteDevVAddr;
+
+		psMapMemInfoMemOUT->sClientSyncInfo.hMappingInfo =
+			psKernelMemInfo->psKernelSyncInfo->psSyncDataMemInfoKM->sMemBlk.hOSMemHandle;
+
+		psMapMemInfoMemOUT->sClientMemInfo.psClientSyncInfo = &psMapMemInfoMemOUT->sClientSyncInfo;
+
+		PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+							 &psMapMemInfoMemOUT->sClientSyncInfo.hKernelSyncInfo,
+							 psKernelMemInfo->psKernelSyncInfo,
+							 PVRSRV_HANDLE_TYPE_SYNC_INFO,
+							 PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+							 psMapMemInfoMemOUT->sClientMemInfo.hKernelMemInfo);
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psMapMemInfoMemOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+
+static IMG_INT
+MMU_GetPDDevPAddrBW(IMG_UINT32 ui32BridgeID,
+					PVRSRV_BRIDGE_IN_GETMMU_PD_DEVPADDR *psGetMmuPDDevPAddrIN,
+					PVRSRV_BRIDGE_OUT_GETMMU_PD_DEVPADDR *psGetMmuPDDevPAddrOUT,
+					PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevMemContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_GETMMU_PD_DEVPADDR);
+
+	psGetMmuPDDevPAddrOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevMemContextInt,
+						   psGetMmuPDDevPAddrIN->hDevMemContext,
+						   PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+	if(psGetMmuPDDevPAddrOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetMmuPDDevPAddrOUT->sPDDevPAddr =
+		BM_GetDeviceNode(hDevMemContextInt)->pfnMMUGetPDDevPAddr(BM_GetMMUContextFromMemContext(hDevMemContextInt));
+	if(psGetMmuPDDevPAddrOUT->sPDDevPAddr.uiAddr)
+	{
+		psGetMmuPDDevPAddrOUT->eError = PVRSRV_OK;
+	}
+	else
+	{
+		psGetMmuPDDevPAddrOUT->eError = PVRSRV_ERROR_GENERIC;
+	}
+	return 0;
+}
+
+
+
+IMG_INT
+DummyBW(IMG_UINT32 ui32BridgeID,
+		IMG_VOID *psBridgeIn,
+		IMG_VOID *psBridgeOut,
+		PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+#if !defined(DEBUG)
+	PVR_UNREFERENCED_PARAMETER(ui32BridgeID);
+#endif
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+	PVR_UNREFERENCED_PARAMETER(psBridgeOut);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+
+#if defined(DEBUG_BRIDGE_KM)
+	PVR_DPF((PVR_DBG_ERROR, "%s: BRIDGE ERROR: BridgeID %lu (%s) mapped to "
+			 "Dummy Wrapper (probably not what you want!)",
+			 __FUNCTION__, ui32BridgeID, g_BridgeDispatchTable[ui32BridgeID].pszIOCName));
+#else
+	PVR_DPF((PVR_DBG_ERROR, "%s: BRIDGE ERROR: BridgeID %lu mapped to "
+			 "Dummy Wrapper (probably not what you want!)",
+			 __FUNCTION__, ui32BridgeID));
+#endif
+	return -ENOTTY;
+}
+
+
+IMG_VOID
+_SetDispatchTableEntry(IMG_UINT32 ui32Index,
+					   const IMG_CHAR *pszIOCName,
+					   BridgeWrapperFunction pfFunction,
+					   const IMG_CHAR *pszFunctionName)
+{
+	static IMG_UINT32 ui32PrevIndex = ~0UL;
+#if !defined(DEBUG)
+	PVR_UNREFERENCED_PARAMETER(pszIOCName);
+#endif
+#if !defined(DEBUG_BRIDGE_KM_DISPATCH_TABLE) && !defined(DEBUG_BRIDGE_KM)
+	PVR_UNREFERENCED_PARAMETER(pszFunctionName);
+#endif
+
+#if defined(DEBUG_BRIDGE_KM_DISPATCH_TABLE)
+
+	PVR_DPF((PVR_DBG_WARNING, "%s: %d %s %s", __FUNCTION__, ui32Index, pszIOCName, pszFunctionName));
+#endif
+
+
+	if(g_BridgeDispatchTable[ui32Index].pfFunction)
+	{
+#if defined(DEBUG_BRIDGE_KM)
+		PVR_DPF((PVR_DBG_ERROR,
+				 "%s: BUG!: Adding dispatch table entry for %s clobbers an existing entry for %s",
+				 __FUNCTION__, pszIOCName, g_BridgeDispatchTable[ui32Index].pszIOCName));
+#else
+		PVR_DPF((PVR_DBG_ERROR,
+				 "%s: BUG!: Adding dispatch table entry for %s clobbers an existing entry (index=%lu)",
+				 __FUNCTION__, pszIOCName, ui32Index));
+#endif
+		PVR_DPF((PVR_DBG_ERROR, "NOTE: Enabling DEBUG_BRIDGE_KM_DISPATCH_TABLE may help debug this issue.",
+				__FUNCTION__));
+	}
+
+
+	if((ui32PrevIndex != ~0UL) &&
+	   ((ui32Index >= ui32PrevIndex + DISPATCH_TABLE_GAP_THRESHOLD) ||
+		(ui32Index <= ui32PrevIndex)))
+	{
+#if defined(DEBUG_BRIDGE_KM)
+		PVR_DPF((PVR_DBG_WARNING,
+				 "%s: There is a gap in the dispatch table between indices %lu (%s) and %lu (%s)",
+				 __FUNCTION__, ui32PrevIndex, g_BridgeDispatchTable[ui32PrevIndex].pszIOCName,
+				 ui32Index, pszIOCName));
+#else
+		PVR_DPF((PVR_DBG_WARNING,
+				 "%s: There is a gap in the dispatch table between indices %u and %u (%s)",
+				 __FUNCTION__, (IMG_UINT)ui32PrevIndex, (IMG_UINT)ui32Index, pszIOCName));
+#endif
+		PVR_DPF((PVR_DBG_ERROR, "NOTE: Enabling DEBUG_BRIDGE_KM_DISPATCH_TABLE may help debug this issue.",
+				__FUNCTION__));
+	}
+
+	g_BridgeDispatchTable[ui32Index].pfFunction = pfFunction;
+#if defined(DEBUG_BRIDGE_KM)
+	g_BridgeDispatchTable[ui32Index].pszIOCName = pszIOCName;
+	g_BridgeDispatchTable[ui32Index].pszFunctionName = pszFunctionName;
+	g_BridgeDispatchTable[ui32Index].ui32CallCount = 0;
+	g_BridgeDispatchTable[ui32Index].ui32CopyFromUserTotalBytes = 0;
+#endif
+
+	ui32PrevIndex = ui32Index;
+}
+
+static IMG_INT
+PVRSRVInitSrvConnectBW(IMG_UINT32 ui32BridgeID,
+					   IMG_VOID *psBridgeIn,
+					   PVRSRV_BRIDGE_RETURN *psRetOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_INITSRV_CONNECT);
+	PVR_UNREFERENCED_PARAMETER(psBridgeIn);
+
+	if(PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_RUNNING) || PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_RAN))
+	{
+		psRetOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+#if defined (__linux__)
+	PVRSRVSetInitServerState(PVRSRV_INIT_SERVER_RUNNING, IMG_TRUE);
+#endif
+	psPerProc->bInitProcess = IMG_TRUE;
+
+	psRetOUT->eError = PVRSRV_OK;
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVInitSrvDisconnectBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_INITSRV_DISCONNECT *psInitSrvDisconnectIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_INITSRV_DISCONNECT);
+
+	if(!psPerProc->bInitProcess)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+	psPerProc->bInitProcess = IMG_FALSE;
+
+	PVRSRVSetInitServerState(PVRSRV_INIT_SERVER_RUNNING, IMG_FALSE);
+	PVRSRVSetInitServerState(PVRSRV_INIT_SERVER_RAN, IMG_TRUE);
+
+	psRetOUT->eError = PVRSRVFinaliseSystem(psInitSrvDisconnectIN->bInitSuccesful);
+
+	PVRSRVSetInitServerState( PVRSRV_INIT_SERVER_SUCCESSFUL,
+				(((psRetOUT->eError == PVRSRV_OK) && (psInitSrvDisconnectIN->bInitSuccesful)))
+				? IMG_TRUE : IMG_FALSE);
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVEventObjectWaitBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_EVENT_OBJECT_WAIT *psEventObjectWaitIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hOSEventKM;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_EVENT_OBJECT_WAIT);
+
+	psRetOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hOSEventKM,
+						   psEventObjectWaitIN->hOSEventKM,
+						   PVRSRV_HANDLE_TYPE_EVENT_OBJECT_CONNECT);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = OSEventObjectWait(hOSEventKM);
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVEventObjectOpenBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_EVENT_OBJECT_OPEN *psEventObjectOpenIN,
+						  PVRSRV_BRIDGE_OUT_EVENT_OBJECT_OPEN *psEventObjectOpenOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_EVENT_OBJECT_OPEN);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psEventObjectOpenOUT->eError, psPerProc, 1);
+
+	psEventObjectOpenOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &psEventObjectOpenIN->sEventObject.hOSEventKM,
+						   psEventObjectOpenIN->sEventObject.hOSEventKM,
+						   PVRSRV_HANDLE_TYPE_SHARED_EVENT_OBJECT);
+
+	if(psEventObjectOpenOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psEventObjectOpenOUT->eError = OSEventObjectOpen(&psEventObjectOpenIN->sEventObject, &psEventObjectOpenOUT->hOSEvent);
+
+	if(psEventObjectOpenOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psEventObjectOpenOUT->hOSEvent,
+					  psEventObjectOpenOUT->hOSEvent,
+					  PVRSRV_HANDLE_TYPE_EVENT_OBJECT_CONNECT,
+					  PVRSRV_HANDLE_ALLOC_FLAG_MULTI);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psEventObjectOpenOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVEventObjectCloseBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_EVENT_OBJECT_CLOSE *psEventObjectCloseIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hOSEventKM;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_EVENT_OBJECT_CLOSE);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &psEventObjectCloseIN->sEventObject.hOSEventKM,
+						   psEventObjectCloseIN->sEventObject.hOSEventKM,
+						   PVRSRV_HANDLE_TYPE_SHARED_EVENT_OBJECT);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &hOSEventKM,
+						   psEventObjectCloseIN->hOSEventKM,
+						   PVRSRV_HANDLE_TYPE_EVENT_OBJECT_CONNECT);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = OSEventObjectClose(&psEventObjectCloseIN->sEventObject, hOSEventKM);
+
+	return 0;
+}
+
+
+typedef struct _MODIFY_SYNC_OP_INFO
+{
+	PVRSRV_KERNEL_SYNC_INFO *psKernelSyncInfo;
+	IMG_UINT32 	ui32ModifyFlags;
+	IMG_UINT32	ui32ReadOpsPendingSnapShot;
+	IMG_UINT32	ui32WriteOpsPendingSnapShot;
+} MODIFY_SYNC_OP_INFO;
+
+
+static PVRSRV_ERROR ModifyCompleteSyncOpsCallBack(IMG_PVOID		pvParam,
+													IMG_UINT32	ui32Param)
+{
+	MODIFY_SYNC_OP_INFO		*psModSyncOpInfo;
+	PVRSRV_KERNEL_SYNC_INFO *psKernelSyncInfo;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	if (!pvParam)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "ModifyCompleteSyncOpsCallBack: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psModSyncOpInfo = (MODIFY_SYNC_OP_INFO*)pvParam;
+	psKernelSyncInfo = psModSyncOpInfo->psKernelSyncInfo;
+
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		if((psModSyncOpInfo->ui32WriteOpsPendingSnapShot == psKernelSyncInfo->psSyncData->ui32WriteOpsComplete)
+		&& (psModSyncOpInfo->ui32ReadOpsPendingSnapShot == psKernelSyncInfo->psSyncData->ui32ReadOpsComplete))
+		{
+			goto OpFlushedComplete;
+		}
+		PVR_DPF((PVR_DBG_ERROR, "ModifyCompleteSyncOpsCallBack: waiting for old Ops to flush"));
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+	PVR_DPF((PVR_DBG_ERROR, "ModifyCompleteSyncOpsCallBack: waiting for old Ops to flush timed out"));
+
+	return PVRSRV_ERROR_TIMEOUT;
+
+OpFlushedComplete:
+
+
+	if(psModSyncOpInfo->ui32ModifyFlags & PVRSRV_MODIFYSYNCOPS_FLAGS_WO_INC)
+	{
+		psKernelSyncInfo->psSyncData->ui32WriteOpsComplete++;
+	}
+
+
+	if(psModSyncOpInfo->ui32ModifyFlags & PVRSRV_MODIFYSYNCOPS_FLAGS_RO_INC)
+	{
+		psKernelSyncInfo->psSyncData->ui32ReadOpsComplete++;
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, 	sizeof(MODIFY_SYNC_OP_INFO), (IMG_VOID *)psModSyncOpInfo, 0);
+
+
+
+	PVRSRVCommandCompleteCallbacks();
+
+	return PVRSRV_OK;
+}
+
+
+static IMG_INT
+PVRSRVModifyPendingSyncOpsBW(IMG_UINT32									ui32BridgeID,
+						      PVRSRV_BRIDGE_IN_MODIFY_PENDING_SYNC_OPS	*psModifySyncOpsIN,
+							  PVRSRV_BRIDGE_OUT_MODIFY_PENDING_SYNC_OPS	*psModifySyncOpsOUT,
+							  PVRSRV_PER_PROCESS_DATA					*psPerProc)
+{
+	IMG_HANDLE				hKernelSyncInfo;
+	PVRSRV_KERNEL_SYNC_INFO *psKernelSyncInfo;
+	MODIFY_SYNC_OP_INFO		*psModSyncOpInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_MODIFY_PENDING_SYNC_OPS);
+
+	psModifySyncOpsOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+													&hKernelSyncInfo,
+													psModifySyncOpsIN->hKernelSyncInfo,
+													PVRSRV_HANDLE_TYPE_SYNC_INFO);
+	if (psModifySyncOpsOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVModifyPendingSyncOpsBW: PVRSRVLookupHandle failed"));
+		return 0;
+	}
+
+	psKernelSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)hKernelSyncInfo;
+
+	if(psKernelSyncInfo->hResItem != IMG_NULL)
+	{
+
+		psModifySyncOpsOUT->eError = PVRSRV_ERROR_RETRY;
+		return 0;
+	}
+
+	ASSIGN_AND_EXIT_ON_ERROR(psModifySyncOpsOUT->eError,
+			  OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  sizeof(MODIFY_SYNC_OP_INFO),
+			  (IMG_VOID **)&psModSyncOpInfo, 0,
+			  "ModSyncOpInfo (MODIFY_SYNC_OP_INFO)"));
+
+
+	psModSyncOpInfo->psKernelSyncInfo = psKernelSyncInfo;
+	psModSyncOpInfo->ui32ModifyFlags = psModifySyncOpsIN->ui32ModifyFlags;
+	psModSyncOpInfo->ui32ReadOpsPendingSnapShot = psKernelSyncInfo->psSyncData->ui32ReadOpsPending;
+	psModSyncOpInfo->ui32WriteOpsPendingSnapShot = psKernelSyncInfo->psSyncData->ui32WriteOpsPending;
+
+
+
+	psModifySyncOpsOUT->ui32ReadOpsPending = psKernelSyncInfo->psSyncData->ui32ReadOpsPending;
+	psModifySyncOpsOUT->ui32WriteOpsPending = psKernelSyncInfo->psSyncData->ui32WriteOpsPending;
+
+	if(psModifySyncOpsIN->ui32ModifyFlags & PVRSRV_MODIFYSYNCOPS_FLAGS_WO_INC)
+	{
+		psKernelSyncInfo->psSyncData->ui32WriteOpsPending++;
+	}
+
+	if(psModifySyncOpsIN->ui32ModifyFlags & PVRSRV_MODIFYSYNCOPS_FLAGS_RO_INC)
+	{
+		psKernelSyncInfo->psSyncData->ui32ReadOpsPending++;
+	}
+
+	psKernelSyncInfo->hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													RESMAN_TYPE_MODIFY_SYNC_OPS,
+													psModSyncOpInfo,
+													0,
+													ModifyCompleteSyncOpsCallBack);
+	return 0;
+}
+
+
+static IMG_INT
+PVRSRVModifyCompleteSyncOpsBW(IMG_UINT32							ui32BridgeID,
+				      PVRSRV_BRIDGE_IN_MODIFY_COMPLETE_SYNC_OPS		*psModifySyncOpsIN,
+					  PVRSRV_BRIDGE_RETURN							*psModifySyncOpsOUT,
+					  PVRSRV_PER_PROCESS_DATA						*psPerProc)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_KERNEL_SYNC_INFO *psKernelSyncInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_MODIFY_COMPLETE_SYNC_OPS);
+
+	psModifySyncOpsOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+													(IMG_VOID**)&psKernelSyncInfo,
+													psModifySyncOpsIN->hKernelSyncInfo,
+													PVRSRV_HANDLE_TYPE_SYNC_INFO);
+	if (psModifySyncOpsOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVModifyCompleteSyncOpsBW: PVRSRVLookupHandle failed"));
+		return 0;
+	}
+
+	if(psKernelSyncInfo->hResItem == IMG_NULL)
+	{
+
+		psModifySyncOpsOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+
+
+
+
+
+
+
+
+
+
+	eError = ResManFreeResByPtr(psKernelSyncInfo->hResItem);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVModifyCompleteSyncOpsBW: ResManFreeResByPtr failed"));
+		return 0;
+	}
+
+	psKernelSyncInfo->hResItem = IMG_NULL;
+
+	return 0;
+}
+
+
+PVRSRV_ERROR
+CommonBridgeInit(IMG_VOID)
+{
+	IMG_UINT32 i;
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ENUM_DEVICES, PVRSRVEnumerateDevicesBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ACQUIRE_DEVICEINFO, PVRSRVAcquireDeviceDataBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_RELEASE_DEVICEINFO, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_CREATE_DEVMEMCONTEXT, PVRSRVCreateDeviceMemContextBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_DESTROY_DEVMEMCONTEXT, PVRSRVDestroyDeviceMemContextBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_DEVMEM_HEAPINFO, PVRSRVGetDeviceMemHeapInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ALLOC_DEVICEMEM, PVRSRVAllocDeviceMemBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_FREE_DEVICEMEM, PVRSRVFreeDeviceMemBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GETFREE_DEVICEMEM, PVRSRVGetFreeDeviceMemBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_CREATE_COMMANDQUEUE, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_DESTROY_COMMANDQUEUE, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MHANDLE_TO_MMAP_DATA, PVRMMapOSMemHandleToMMapDataBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_CONNECT_SERVICES, PVRSRVConnectBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_DISCONNECT_SERVICES, PVRSRVDisconnectBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_WRAP_DEVICE_MEM, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_DEVICEMEMINFO, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_RESERVE_DEV_VIRTMEM	, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_FREE_DEV_VIRTMEM, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MAP_EXT_MEMORY, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNMAP_EXT_MEMORY, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MAP_DEV_MEMORY, PVRSRVMapDeviceMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNMAP_DEV_MEMORY, PVRSRVUnmapDeviceMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY, PVRSRVMapDeviceClassMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNMAP_DEVICECLASS_MEMORY, PVRSRVUnmapDeviceClassMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MAP_MEM_INFO_TO_USER, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNMAP_MEM_INFO_FROM_USER, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_EXPORT_DEVICEMEM, PVRSRVExportDeviceMemBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_RELEASE_MMAP_DATA, PVRMMapReleaseMMapDataBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PROCESS_SIMISR_EVENT, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_REGISTER_SIM_PROCESS, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNREGISTER_SIM_PROCESS, DummyBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MAPPHYSTOUSERSPACE, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNMAPPHYSTOUSERSPACE, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GETPHYSTOUSERSPACEMAP, DummyBW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_FB_STATS, DummyBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_MISC_INFO, PVRSRVGetMiscInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_RELEASE_MISC_INFO, DummyBW);
+
+
+#if defined (SUPPORT_OVERLAY_ROTATE_BLIT)
+	SetDispatchTableEntry(PVRSRV_BRIDGE_INIT_3D_OVL_BLT_RES, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_DEINIT_3D_OVL_BLT_RES, DummyBW);
+#endif
+
+
+
+#if defined(PDUMP)
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_INIT, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_MEMPOL, PDumpMemPolBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_DUMPMEM, PDumpMemBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_REG, PDumpRegWithFlagsBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_REGPOL, PDumpRegPolBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_COMMENT, PDumpCommentBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_SETFRAME, PDumpSetFrameBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_ISCAPTURING, PDumpIsCaptureFrameBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_DUMPBITMAP, PDumpBitmapBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_DUMPREADREG, PDumpReadRegBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_SYNCPOL, PDumpSyncPolBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_DUMPSYNC, PDumpSyncDumpBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_DRIVERINFO, PDumpDriverInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_PDREG, PDumpPDRegBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_DUMPPDDEVPADDR, PDumpPDDevPAddrBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_CYCLE_COUNT_REG_READ, PDumpCycleCountRegReadBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_STARTINITPHASE, PDumpStartInitPhaseBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_PDUMP_STOPINITPHASE, PDumpStopInitPhaseBW);
+#endif
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_OEMJTABLE, DummyBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ENUM_CLASS, PVRSRVEnumerateDCBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_OPEN_DISPCLASS_DEVICE, PVRSRVOpenDCDeviceBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_CLOSE_DISPCLASS_DEVICE, PVRSRVCloseDCDeviceBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ENUM_DISPCLASS_FORMATS, PVRSRVEnumDCFormatsBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ENUM_DISPCLASS_DIMS, PVRSRVEnumDCDimsBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_DISPCLASS_SYSBUFFER, PVRSRVGetDCSystemBufferBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_DISPCLASS_INFO, PVRSRVGetDCInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_CREATE_DISPCLASS_SWAPCHAIN, PVRSRVCreateDCSwapChainBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_DESTROY_DISPCLASS_SWAPCHAIN, PVRSRVDestroyDCSwapChainBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SET_DISPCLASS_DSTRECT, PVRSRVSetDCDstRectBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SET_DISPCLASS_SRCRECT, PVRSRVSetDCSrcRectBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SET_DISPCLASS_DSTCOLOURKEY, PVRSRVSetDCDstColourKeyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SET_DISPCLASS_SRCCOLOURKEY, PVRSRVSetDCSrcColourKeyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_DISPCLASS_BUFFERS, PVRSRVGetDCBuffersBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SWAP_DISPCLASS_TO_BUFFER, PVRSRVSwapToDCBufferBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SWAP_DISPCLASS_TO_SYSTEM, PVRSRVSwapToDCSystemBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_OPEN_BUFFERCLASS_DEVICE, PVRSRVOpenBCDeviceBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_CLOSE_BUFFERCLASS_DEVICE, PVRSRVCloseBCDeviceBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_BUFFERCLASS_INFO, PVRSRVGetBCInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_BUFFERCLASS_BUFFER, PVRSRVGetBCBufferBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GET_BUFFERCLASS_BUFFER_ID_FROM_TAG, PVRSRVGetBCBufferIdFromTagBW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_WRAP_EXT_MEMORY, PVRSRVWrapExtMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_UNWRAP_EXT_MEMORY, PVRSRVUnwrapExtMemoryBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_ALLOC_SHARED_SYS_MEM, PVRSRVAllocSharedSysMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_FREE_SHARED_SYS_MEM, PVRSRVFreeSharedSysMemoryBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MAP_MEMINFO_MEM, PVRSRVMapMemInfoMemBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_GETMMU_PD_DEVPADDR, MMU_GetPDDevPAddrBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_INITSRV_CONNECT,	PVRSRVInitSrvConnectBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_INITSRV_DISCONNECT, PVRSRVInitSrvDisconnectBW);
+
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_EVENT_OBJECT_WAIT,	PVRSRVEventObjectWaitBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_EVENT_OBJECT_OPEN,	PVRSRVEventObjectOpenBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_EVENT_OBJECT_CLOSE, PVRSRVEventObjectCloseBW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MODIFY_PENDING_SYNC_OPS, PVRSRVModifyPendingSyncOpsBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_MODIFY_COMPLETE_SYNC_OPS, PVRSRVModifyCompleteSyncOpsBW);
+
+#if defined (SUPPORT_SGX)
+	SetSGXDispatchTableEntry();
+#endif
+#if defined (SUPPORT_VGX)
+	SetVGXDispatchTableEntry();
+#endif
+#if defined (SUPPORT_MSVDX)
+	SetMSVDXDispatchTableEntry();
+#endif
+
+
+
+
+	for(i=0;i<BRIDGE_DISPATCH_TABLE_ENTRY_COUNT;i++)
+	{
+		if(!g_BridgeDispatchTable[i].pfFunction)
+		{
+			g_BridgeDispatchTable[i].pfFunction = DummyBW;
+#if defined(DEBUG_BRIDGE_KM)
+			g_BridgeDispatchTable[i].pszIOCName = "_PVRSRV_BRIDGE_DUMMY";
+			g_BridgeDispatchTable[i].pszFunctionName = "DummyBW";
+			g_BridgeDispatchTable[i].ui32CallCount = 0;
+			g_BridgeDispatchTable[i].ui32CopyFromUserTotalBytes = 0;
+			g_BridgeDispatchTable[i].ui32CopyToUserTotalBytes = 0;
+#endif
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_INT BridgedDispatchKM(PVRSRV_PER_PROCESS_DATA * psPerProc,
+					  PVRSRV_BRIDGE_PACKAGE   * psBridgePackageKM)
+{
+
+	IMG_VOID   * psBridgeIn;
+	IMG_VOID   * psBridgeOut;
+	BridgeWrapperFunction pfBridgeHandler;
+	IMG_UINT32   ui32BridgeID = psBridgePackageKM->ui32BridgeID;
+	IMG_INT      err          = -EFAULT;
+
+#if defined(DEBUG_TRACE_BRIDGE_KM)
+	PVR_DPF((PVR_DBG_ERROR, "%s: %s",
+			 __FUNCTION__,
+			 g_BridgeDispatchTable[ui32BridgeID].pszIOCName));
+#endif
+
+#if defined(DEBUG_BRIDGE_KM)
+	g_BridgeDispatchTable[ui32BridgeID].ui32CallCount++;
+	g_BridgeGlobalStats.ui32IOCTLCount++;
+#endif
+
+	if(!psPerProc->bInitProcess)
+	{
+		if(PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_RAN))
+		{
+			if(!PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL))
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Initialisation failed.  Driver unusable.",
+						 __FUNCTION__));
+				goto return_fault;
+			}
+		}
+		else
+		{
+			if(PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_RUNNING))
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Initialisation is in progress",
+						 __FUNCTION__));
+				goto return_fault;
+			}
+			else
+			{
+
+				switch(ui32BridgeID)
+				{
+					case PVRSRV_GET_BRIDGE_ID(PVRSRV_BRIDGE_CONNECT_SERVICES):
+					case PVRSRV_GET_BRIDGE_ID(PVRSRV_BRIDGE_DISCONNECT_SERVICES):
+					case PVRSRV_GET_BRIDGE_ID(PVRSRV_BRIDGE_INITSRV_CONNECT):
+					case PVRSRV_GET_BRIDGE_ID(PVRSRV_BRIDGE_INITSRV_DISCONNECT):
+						break;
+					default:
+						PVR_DPF((PVR_DBG_ERROR, "%s: Driver initialisation not completed yet.",
+								 __FUNCTION__));
+						goto return_fault;
+				}
+			}
+		}
+	}
+
+
+
+#if defined(__linux__)
+	{
+
+		SYS_DATA *psSysData;
+
+		SysAcquireData(&psSysData);
+
+
+		psBridgeIn = ((ENV_DATA *)psSysData->pvEnvSpecificData)->pvBridgeData;
+		psBridgeOut = (IMG_PVOID)((IMG_PBYTE)psBridgeIn + PVRSRV_MAX_BRIDGE_IN_SIZE);
+
+		if(psBridgePackageKM->ui32InBufferSize > 0)
+		{
+			if(!OSAccessOK(PVR_VERIFY_READ,
+							psBridgePackageKM->pvParamIn,
+							psBridgePackageKM->ui32InBufferSize))
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Invalid pvParamIn pointer", __FUNCTION__));
+			}
+
+			if(CopyFromUserWrapper(psPerProc,
+					               ui32BridgeID,
+								   psBridgeIn,
+								   psBridgePackageKM->pvParamIn,
+								   psBridgePackageKM->ui32InBufferSize)
+			  != PVRSRV_OK)
+			{
+				goto return_fault;
+			}
+		}
+	}
+#else
+	psBridgeIn  = psBridgePackageKM->pvParamIn;
+	psBridgeOut = psBridgePackageKM->pvParamOut;
+#endif
+
+	if(ui32BridgeID >= (BRIDGE_DISPATCH_TABLE_ENTRY_COUNT))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: ui32BridgeID = %d is out if range!",
+				 __FUNCTION__, ui32BridgeID));
+		goto return_fault;
+	}
+	pfBridgeHandler =
+		(BridgeWrapperFunction)g_BridgeDispatchTable[ui32BridgeID].pfFunction;
+	err = pfBridgeHandler(ui32BridgeID,
+						  psBridgeIn,
+						  psBridgeOut,
+						  psPerProc);
+	if(err < 0)
+	{
+		goto return_fault;
+	}
+
+
+#if defined(__linux__)
+
+	if(CopyToUserWrapper(psPerProc,
+						 ui32BridgeID,
+						 psBridgePackageKM->pvParamOut,
+						 psBridgeOut,
+						 psBridgePackageKM->ui32OutBufferSize)
+	   != PVRSRV_OK)
+	{
+		goto return_fault;
+	}
+#endif
+
+	err = 0;
+return_fault:
+	ReleaseHandleBatch(psPerProc);
+	return err;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.h
new file mode 100644
index 0000000..9d03a24
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_pvr_bridge.h
@@ -0,0 +1,227 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __BRIDGED_PVR_BRIDGE_H__
+#define __BRIDGED_PVR_BRIDGE_H__
+
+#include "pvr_bridge.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#if defined(__linux__)
+#define PVRSRV_GET_BRIDGE_ID(X)	_IOC_NR(X)
+#else
+#define PVRSRV_GET_BRIDGE_ID(X)	(X - PVRSRV_IOWR(PVRSRV_BRIDGE_CORE_CMD_FIRST))
+#endif
+
+#ifndef ENOMEM
+#define ENOMEM	12
+#endif
+#ifndef EFAULT
+#define EFAULT	14
+#endif
+#ifndef ENOTTY
+#define ENOTTY	25
+#endif
+
+#if defined(DEBUG_BRIDGE_KM)
+PVRSRV_ERROR
+CopyFromUserWrapper(PVRSRV_PER_PROCESS_DATA *pProcData,
+					IMG_UINT32 ui32BridgeID,
+					IMG_VOID *pvDest,
+					IMG_VOID *pvSrc,
+					IMG_UINT32 ui32Size);
+PVRSRV_ERROR
+CopyToUserWrapper(PVRSRV_PER_PROCESS_DATA *pProcData,
+				  IMG_UINT32 ui32BridgeID,
+				  IMG_VOID *pvDest,
+				  IMG_VOID *pvSrc,
+				  IMG_UINT32 ui32Size);
+#else
+#define CopyFromUserWrapper(pProcData, ui32BridgeID, pvDest, pvSrc, ui32Size) \
+	OSCopyFromUser(pProcData, pvDest, pvSrc, ui32Size)
+#define CopyToUserWrapper(pProcData, ui32BridgeID, pvDest, pvSrc, ui32Size) \
+	OSCopyToUser(pProcData, pvDest, pvSrc, ui32Size)
+#endif
+
+
+#define ASSIGN_AND_RETURN_ON_ERROR(error, src, res)		\
+	do							\
+	{							\
+		(error) = (src);				\
+		if ((error) != PVRSRV_OK) 			\
+		{						\
+			return (res);				\
+		}						\
+	} while (error != PVRSRV_OK)
+
+#define ASSIGN_AND_EXIT_ON_ERROR(error, src)		\
+	ASSIGN_AND_RETURN_ON_ERROR(error, src, 0)
+
+#if defined (PVR_SECURE_HANDLES)
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(NewHandleBatch)
+#endif
+static INLINE PVRSRV_ERROR
+NewHandleBatch(PVRSRV_PER_PROCESS_DATA *psPerProc,
+					IMG_UINT32 ui32BatchSize)
+{
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(!psPerProc->bHandlesBatched);
+
+	eError = PVRSRVNewHandleBatch(psPerProc->psHandleBase, ui32BatchSize);
+
+	if (eError == PVRSRV_OK)
+	{
+		psPerProc->bHandlesBatched = IMG_TRUE;
+	}
+
+	return eError;
+}
+
+#define NEW_HANDLE_BATCH_OR_ERROR(error, psPerProc, ui32BatchSize)	\
+	ASSIGN_AND_EXIT_ON_ERROR(error, NewHandleBatch(psPerProc, ui32BatchSize))
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(CommitHandleBatch)
+#endif
+static INLINE PVRSRV_ERROR
+CommitHandleBatch(PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVR_ASSERT(psPerProc->bHandlesBatched);
+
+	psPerProc->bHandlesBatched = IMG_FALSE;
+
+	return PVRSRVCommitHandleBatch(psPerProc->psHandleBase);
+}
+
+
+#define COMMIT_HANDLE_BATCH_OR_ERROR(error, psPerProc) 			\
+	ASSIGN_AND_EXIT_ON_ERROR(error, CommitHandleBatch(psPerProc))
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(ReleaseHandleBatch)
+#endif
+static INLINE IMG_VOID
+ReleaseHandleBatch(PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	if (psPerProc->bHandlesBatched)
+	{
+		psPerProc->bHandlesBatched = IMG_FALSE;
+
+		PVRSRVReleaseHandleBatch(psPerProc->psHandleBase);
+	}
+}
+#else
+#define NEW_HANDLE_BATCH_OR_ERROR(error, psPerProc, ui32BatchSize)
+#define COMMIT_HANDLE_BATCH_OR_ERROR(error, psPerProc)
+#define ReleaseHandleBatch(psPerProc)
+#endif
+
+IMG_INT
+DummyBW(IMG_UINT32 ui32BridgeID,
+		IMG_VOID *psBridgeIn,
+		IMG_VOID *psBridgeOut,
+		PVRSRV_PER_PROCESS_DATA *psPerProc);
+
+typedef IMG_INT (*BridgeWrapperFunction)(IMG_UINT32 ui32BridgeID,
+									 IMG_VOID *psBridgeIn,
+									 IMG_VOID *psBridgeOut,
+									 PVRSRV_PER_PROCESS_DATA *psPerProc);
+
+typedef struct _PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY
+{
+	BridgeWrapperFunction pfFunction;
+#if defined(DEBUG_BRIDGE_KM)
+	const IMG_CHAR *pszIOCName;
+	const IMG_CHAR *pszFunctionName;
+	IMG_UINT32 ui32CallCount;
+	IMG_UINT32 ui32CopyFromUserTotalBytes;
+	IMG_UINT32 ui32CopyToUserTotalBytes;
+#endif
+}PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY;
+
+#if defined(SUPPORT_VGX) || defined(SUPPORT_MSVDX)
+	#if defined(SUPPORT_VGX)
+		#define BRIDGE_DISPATCH_TABLE_ENTRY_COUNT (PVRSRV_BRIDGE_LAST_VGX_CMD+1)
+		#define PVRSRV_BRIDGE_LAST_DEVICE_CMD	   PVRSRV_BRIDGE_LAST_VGX_CMD
+	#else
+		#define BRIDGE_DISPATCH_TABLE_ENTRY_COUNT (PVRSRV_BRIDGE_LAST_MSVDX_CMD+1)
+		#define PVRSRV_BRIDGE_LAST_DEVICE_CMD	   PVRSRV_BRIDGE_LAST_MSVDX_CMD
+	#endif
+#else
+	#if defined(SUPPORT_SGX)
+		#define BRIDGE_DISPATCH_TABLE_ENTRY_COUNT (PVRSRV_BRIDGE_LAST_SGX_CMD+1)
+		#define PVRSRV_BRIDGE_LAST_DEVICE_CMD	   PVRSRV_BRIDGE_LAST_SGX_CMD
+	#else
+		#define BRIDGE_DISPATCH_TABLE_ENTRY_COUNT (PVRSRV_BRIDGE_LAST_NON_DEVICE_CMD+1)
+		#define PVRSRV_BRIDGE_LAST_DEVICE_CMD	   PVRSRV_BRIDGE_LAST_NON_DEVICE_CMD
+	#endif
+#endif
+
+extern PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY g_BridgeDispatchTable[BRIDGE_DISPATCH_TABLE_ENTRY_COUNT];
+
+IMG_VOID
+_SetDispatchTableEntry(IMG_UINT32 ui32Index,
+					   const IMG_CHAR *pszIOCName,
+					   BridgeWrapperFunction pfFunction,
+					   const IMG_CHAR *pszFunctionName);
+
+
+#define SetDispatchTableEntry(ui32Index, pfFunction) \
+	_SetDispatchTableEntry(PVRSRV_GET_BRIDGE_ID(ui32Index), #ui32Index, (BridgeWrapperFunction)pfFunction, #pfFunction)
+
+#define DISPATCH_TABLE_GAP_THRESHOLD 5
+
+#if defined(DEBUG)
+#define PVRSRV_BRIDGE_ASSERT_CMD(X, Y) PVR_ASSERT(X == PVRSRV_GET_BRIDGE_ID(Y))
+#else
+#define PVRSRV_BRIDGE_ASSERT_CMD(X, Y) PVR_UNREFERENCED_PARAMETER(X)
+#endif
+
+
+#if defined(DEBUG_BRIDGE_KM)
+typedef struct _PVRSRV_BRIDGE_GLOBAL_STATS
+{
+	IMG_UINT32 ui32IOCTLCount;
+	IMG_UINT32 ui32TotalCopyFromUserBytes;
+	IMG_UINT32 ui32TotalCopyToUserBytes;
+}PVRSRV_BRIDGE_GLOBAL_STATS;
+
+extern PVRSRV_BRIDGE_GLOBAL_STATS g_BridgeGlobalStats;
+#endif
+
+
+PVRSRV_ERROR CommonBridgeInit(IMG_VOID);
+
+IMG_INT BridgedDispatchKM(PVRSRV_PER_PROCESS_DATA * psPerProc,
+					  PVRSRV_BRIDGE_PACKAGE   * psBridgePackageKM);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.c
new file mode 100644
index 0000000..b82231a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.c
@@ -0,0 +1,81 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "img_defs.h"
+#include "servicesint.h"
+#include "bridged_support.h"
+
+
+PVRSRV_ERROR
+PVRSRVLookupOSMemHandle(PVRSRV_HANDLE_BASE *psHandleBase, IMG_HANDLE *phOSMemHandle, IMG_HANDLE hMHandle)
+{
+	IMG_HANDLE hMHandleInt;
+	PVRSRV_HANDLE_TYPE eHandleType;
+	PVRSRV_ERROR eError;
+
+
+	eError = PVRSRVLookupHandleAnyType(psHandleBase, &hMHandleInt,
+							  &eHandleType,
+							  hMHandle);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	switch(eHandleType)
+	{
+#if defined(PVR_SECURE_HANDLES)
+		case PVRSRV_HANDLE_TYPE_MEM_INFO:
+		case PVRSRV_HANDLE_TYPE_MEM_INFO_REF:
+		case PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO:
+		{
+			PVRSRV_KERNEL_MEM_INFO *psMemInfo = (PVRSRV_KERNEL_MEM_INFO *)hMHandleInt;
+
+			*phOSMemHandle = psMemInfo->sMemBlk.hOSMemHandle;
+
+			break;
+		}
+		case PVRSRV_HANDLE_TYPE_SYNC_INFO:
+		{
+			PVRSRV_KERNEL_SYNC_INFO *psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)hMHandleInt;
+			PVRSRV_KERNEL_MEM_INFO *psMemInfo = psSyncInfo->psSyncDataMemInfoKM;
+
+			*phOSMemHandle = psMemInfo->sMemBlk.hOSMemHandle;
+
+			break;
+		}
+		case  PVRSRV_HANDLE_TYPE_SOC_TIMER:
+		{
+			*phOSMemHandle = (IMG_VOID *)hMHandleInt;
+			break;
+		}
+#else
+		case  PVRSRV_HANDLE_TYPE_NONE:
+			*phOSMemHandle = (IMG_VOID *)hMHandleInt;
+			break;
+#endif
+		default:
+			return PVRSRV_ERROR_BAD_MAPPING;
+	}
+
+	return PVRSRV_OK;
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.h
new file mode 100644
index 0000000..b5c2d15
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/bridged_support.h
@@ -0,0 +1,39 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __BRIDGED_SUPPORT_H__
+#define __BRIDGED_SUPPORT_H__
+
+#include "handle.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+PVRSRV_ERROR PVRSRVLookupOSMemHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phOSMemHandle, IMG_HANDLE hMHandle);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.c
new file mode 100644
index 0000000..de4e697
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.c
@@ -0,0 +1,2510 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+
+
+#include <stddef.h>
+
+#include "img_defs.h"
+
+#if defined(SUPPORT_SGX)
+
+#include "services.h"
+#include "pvr_debug.h"
+#include "pvr_bridge.h"
+#include "sgx_bridge.h"
+#include "perproc.h"
+#include "power.h"
+#include "pvr_bridge_km.h"
+#include "sgx_bridge_km.h"
+
+#if defined(SUPPORT_MSVDX)
+	#include "msvdx_bridge.h"
+#endif
+
+#include "bridged_pvr_bridge.h"
+#include "bridged_sgx_bridge.h"
+#include "sgxutils.h"
+#include "pdump_km.h"
+
+static IMG_INT
+SGXGetClientInfoBW(IMG_UINT32 ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_GETCLIENTINFO *psGetClientInfoIN,
+				   PVRSRV_BRIDGE_OUT_GETCLIENTINFO *psGetClientInfoOUT,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_GETCLIENTINFO);
+
+	psGetClientInfoOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psGetClientInfoIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psGetClientInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psGetClientInfoOUT->eError =
+		SGXGetClientInfoKM(hDevCookieInt,
+						   &psGetClientInfoOUT->sClientInfo);
+	return 0;
+}
+
+static IMG_INT
+SGXReleaseClientInfoBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_RELEASECLIENTINFO *psReleaseClientInfoIN,
+					   PVRSRV_BRIDGE_RETURN *psRetOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+	IMG_HANDLE hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_RELEASECLIENTINFO);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psReleaseClientInfoIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psDevInfo = (PVRSRV_SGXDEV_INFO *)((PVRSRV_DEVICE_NODE *)hDevCookieInt)->pvDevice;
+
+	PVR_ASSERT(psDevInfo->ui32ClientRefCount > 0);
+
+	psDevInfo->ui32ClientRefCount--;
+
+	psRetOUT->eError = PVRSRV_OK;
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXGetInternalDevInfoBW(IMG_UINT32 ui32BridgeID,
+						PVRSRV_BRIDGE_IN_GETINTERNALDEVINFO *psSGXGetInternalDevInfoIN,
+						PVRSRV_BRIDGE_OUT_GETINTERNALDEVINFO *psSGXGetInternalDevInfoOUT,
+						PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_GETINTERNALDEVINFO);
+
+	psSGXGetInternalDevInfoOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psSGXGetInternalDevInfoIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psSGXGetInternalDevInfoOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psSGXGetInternalDevInfoOUT->eError =
+		SGXGetInternalDevInfoKM(hDevCookieInt,
+								&psSGXGetInternalDevInfoOUT->sSGXInternalDevInfo);
+
+
+	psSGXGetInternalDevInfoOUT->eError =
+		PVRSRVAllocHandle(psPerProc->psHandleBase,
+						  &psSGXGetInternalDevInfoOUT->sSGXInternalDevInfo.hHostCtlKernelMemInfoHandle,
+						  psSGXGetInternalDevInfoOUT->sSGXInternalDevInfo.hHostCtlKernelMemInfoHandle,
+						  PVRSRV_HANDLE_TYPE_MEM_INFO,
+						  PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXDoKickBW(IMG_UINT32 ui32BridgeID,
+			PVRSRV_BRIDGE_IN_DOKICK *psDoKickIN,
+			PVRSRV_BRIDGE_RETURN *psRetOUT,
+			PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_UINT32 i;
+	IMG_INT ret = 0;
+	IMG_UINT32 ui32NumDstSyncs;
+	IMG_HANDLE *phKernelSyncInfoHandles = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_DOKICK);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psDoKickIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &psDoKickIN->sCCBKick.hCCBKernelMemInfo,
+						   psDoKickIN->sCCBKick.hCCBKernelMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	if(psDoKickIN->sCCBKick.hTA3DSyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.hTA3DSyncInfo,
+							   psDoKickIN->sCCBKick.hTA3DSyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if(psDoKickIN->sCCBKick.hTASyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.hTASyncInfo,
+							   psDoKickIN->sCCBKick.hTASyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if(psDoKickIN->sCCBKick.h3DSyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.h3DSyncInfo,
+							   psDoKickIN->sCCBKick.h3DSyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+
+	if (psDoKickIN->sCCBKick.ui32NumTASrcSyncs > SGX_MAX_TA_SRC_SYNCS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+
+	for(i=0; i<psDoKickIN->sCCBKick.ui32NumTASrcSyncs; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.ahTASrcKernelSyncInfo[i],
+							   psDoKickIN->sCCBKick.ahTASrcKernelSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psDoKickIN->sCCBKick.ui32NumTADstSyncs > SGX_MAX_TA_DST_SYNCS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+
+	for(i=0; i<psDoKickIN->sCCBKick.ui32NumTADstSyncs; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.ahTADstKernelSyncInfo[i],
+							   psDoKickIN->sCCBKick.ahTADstKernelSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psDoKickIN->sCCBKick.ui32Num3DSrcSyncs > SGX_MAX_3D_SRC_SYNCS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+
+	for(i=0; i<psDoKickIN->sCCBKick.ui32Num3DSrcSyncs; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.ah3DSrcKernelSyncInfo[i],
+							   psDoKickIN->sCCBKick.ah3DSrcKernelSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+#else
+
+	if (psDoKickIN->sCCBKick.ui32NumSrcSyncs > SGX_MAX_SRC_SYNCS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+	for(i=0; i<psDoKickIN->sCCBKick.ui32NumSrcSyncs; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.ahSrcKernelSyncInfo[i],
+							   psDoKickIN->sCCBKick.ahSrcKernelSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+#endif
+
+	if (psDoKickIN->sCCBKick.ui32NumTAStatusVals > SGX_MAX_TA_STATUS_VALS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+	for (i = 0; i < psDoKickIN->sCCBKick.ui32NumTAStatusVals; i++)
+	{
+		psRetOUT->eError =
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.asTAStatusUpdate[i].hKernelMemInfo,
+							   psDoKickIN->sCCBKick.asTAStatusUpdate[i].hKernelMemInfo,
+							   PVRSRV_HANDLE_TYPE_MEM_INFO);
+#else
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.ahTAStatusSyncInfo[i],
+							   psDoKickIN->sCCBKick.ahTAStatusSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+#endif
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psDoKickIN->sCCBKick.ui32Num3DStatusVals > SGX_MAX_3D_STATUS_VALS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+	for(i = 0; i < psDoKickIN->sCCBKick.ui32Num3DStatusVals; i++)
+	{
+		psRetOUT->eError =
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.as3DStatusUpdate[i].hKernelMemInfo,
+							   psDoKickIN->sCCBKick.as3DStatusUpdate[i].hKernelMemInfo,
+							   PVRSRV_HANDLE_TYPE_MEM_INFO);
+#else
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psDoKickIN->sCCBKick.ah3DStatusSyncInfo[i],
+							   psDoKickIN->sCCBKick.ah3DStatusSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+#endif
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	ui32NumDstSyncs = psDoKickIN->sCCBKick.ui32NumDstSyncObjects;
+
+	if(ui32NumDstSyncs > 0)
+	{
+		if(!OSAccessOK(PVR_VERIFY_READ,
+						psDoKickIN->sCCBKick.pahDstSyncHandles,
+						ui32NumDstSyncs * sizeof(IMG_HANDLE)))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "%s: SGXDoKickBW:"
+					" Invalid pasDstSyncHandles pointer", __FUNCTION__));
+			return -EFAULT;
+		}
+
+		psRetOUT->eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+										ui32NumDstSyncs * sizeof(IMG_HANDLE),
+										(IMG_VOID **)&phKernelSyncInfoHandles,
+										0,
+										"Array of Synchronization Info Handles");
+		if (psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+
+		if(CopyFromUserWrapper(psPerProc,
+							ui32BridgeID,
+							phKernelSyncInfoHandles,
+							psDoKickIN->sCCBKick.pahDstSyncHandles,
+							ui32NumDstSyncs * sizeof(IMG_HANDLE)) != PVRSRV_OK)
+		{
+			ret = -EFAULT;
+			goto PVRSRV_BRIDGE_SGX_DOKICK_RETURN_RESULT;
+		}
+
+
+		psDoKickIN->sCCBKick.pahDstSyncHandles = phKernelSyncInfoHandles;
+
+		for( i = 0; i < ui32NumDstSyncs; i++)
+		{
+			psRetOUT->eError =
+				PVRSRVLookupHandle(psPerProc->psHandleBase,
+									&psDoKickIN->sCCBKick.pahDstSyncHandles[i],
+									psDoKickIN->sCCBKick.pahDstSyncHandles[i],
+									PVRSRV_HANDLE_TYPE_SYNC_INFO);
+
+			if(psRetOUT->eError != PVRSRV_OK)
+			{
+				goto PVRSRV_BRIDGE_SGX_DOKICK_RETURN_RESULT;
+			}
+
+		}
+
+		psRetOUT->eError =
+					PVRSRVLookupHandle(psPerProc->psHandleBase,
+									   &psDoKickIN->sCCBKick.hKernelHWSyncListMemInfo,
+									   psDoKickIN->sCCBKick.hKernelHWSyncListMemInfo,
+									   PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			goto PVRSRV_BRIDGE_SGX_DOKICK_RETURN_RESULT;
+		}
+	}
+
+	psRetOUT->eError =
+		SGXDoKickKM(hDevCookieInt,
+					&psDoKickIN->sCCBKick);
+
+PVRSRV_BRIDGE_SGX_DOKICK_RETURN_RESULT:
+
+	if(phKernelSyncInfoHandles)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32NumDstSyncs * sizeof(IMG_HANDLE),
+				  (IMG_VOID *)phKernelSyncInfoHandles,
+				  0);
+
+	}
+	return ret;
+}
+
+
+static IMG_INT
+SGXScheduleProcessQueuesBW(IMG_UINT32 ui32BridgeID,
+			PVRSRV_BRIDGE_IN_SGX_SCHEDULE_PROCESS_QUEUES *psScheduleProcQIN,
+			PVRSRV_BRIDGE_RETURN *psRetOUT,
+			PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_SCHEDULE_PROCESS_QUEUES);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psScheduleProcQIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = SGXScheduleProcessQueuesKM(hDevCookieInt);
+
+	return 0;
+}
+
+
+#if defined(TRANSFER_QUEUE)
+static IMG_INT
+SGXSubmitTransferBW(IMG_UINT32 ui32BridgeID,
+			PVRSRV_BRIDGE_IN_SUBMITTRANSFER *psSubmitTransferIN,
+			PVRSRV_BRIDGE_RETURN *psRetOUT,
+			PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_TRANSFER_SGX_KICK *psKick;
+	IMG_UINT32 i;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_SUBMITTRANSFER);
+	PVR_UNREFERENCED_PARAMETER(ui32BridgeID);
+
+	psKick = &psSubmitTransferIN->sKick;
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSubmitTransferIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &psKick->hCCBMemInfo,
+						   psKick->hCCBMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	if (psKick->hTASyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->hTASyncInfo,
+							   psKick->hTASyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psKick->h3DSyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->h3DSyncInfo,
+							   psKick->h3DSyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psKick->ui32NumSrcSync > SGX_MAX_TRANSFER_SYNC_OPS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+	for (i = 0; i < psKick->ui32NumSrcSync; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->ahSrcSyncInfo[i],
+							   psKick->ahSrcSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psKick->ui32NumDstSync > SGX_MAX_TRANSFER_SYNC_OPS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+	for (i = 0; i < psKick->ui32NumDstSync; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->ahDstSyncInfo[i],
+							   psKick->ahDstSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	psRetOUT->eError = SGXSubmitTransferKM(hDevCookieInt, psKick);
+
+	return 0;
+}
+
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+static IMG_INT
+SGXSubmit2DBW(IMG_UINT32 ui32BridgeID,
+			PVRSRV_BRIDGE_IN_SUBMIT2D *psSubmit2DIN,
+			PVRSRV_BRIDGE_RETURN *psRetOUT,
+			PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_2D_SGX_KICK *psKick;
+	IMG_UINT32 i;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_SUBMIT2D);
+	PVR_UNREFERENCED_PARAMETER(ui32BridgeID);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSubmit2DIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psKick = &psSubmit2DIN->sKick;
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &psKick->hCCBMemInfo,
+						   psKick->hCCBMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	if (psKick->hTASyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->hTASyncInfo,
+							   psKick->hTASyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psKick->h3DSyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->h3DSyncInfo,
+							   psKick->h3DSyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psKick->ui32NumSrcSync > SGX_MAX_2D_SRC_SYNC_OPS)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_INVALID_PARAMS;
+		return 0;
+	}
+	for (i = 0; i < psKick->ui32NumSrcSync; i++)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->ahSrcSyncInfo[i],
+							   psKick->ahSrcSyncInfo[i],
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	if (psKick->hDstSyncInfo != IMG_NULL)
+	{
+		psRetOUT->eError =
+			PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &psKick->hDstSyncInfo,
+							   psKick->hDstSyncInfo,
+							   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+	psRetOUT->eError =
+		SGXSubmit2DKM(hDevCookieInt, psKick);
+
+	return 0;
+}
+#endif
+#endif
+
+
+static IMG_INT
+SGXGetMiscInfoBW(IMG_UINT32 ui32BridgeID,
+				 PVRSRV_BRIDGE_IN_SGXGETMISCINFO *psSGXGetMiscInfoIN,
+				 PVRSRV_BRIDGE_RETURN *psRetOUT,
+				 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hDevMemContextInt = 0;
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+	SGX_MISC_INFO        sMiscInfo;
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID,
+							PVRSRV_BRIDGE_SGX_GETMISCINFO);
+
+	psRetOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+							&hDevCookieInt,
+							psSGXGetMiscInfoIN->hDevCookie,
+							PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+
+	if (psSGXGetMiscInfoIN->psMiscInfo->eRequest == SGX_MISC_INFO_REQUEST_MEMREAD)
+	{
+		psRetOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+								&hDevMemContextInt,
+								psSGXGetMiscInfoIN->psMiscInfo->hDevMemContext,
+								PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT);
+
+		if(psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+#endif
+
+	psDeviceNode = hDevCookieInt;
+	PVR_ASSERT(psDeviceNode != IMG_NULL);
+	if (psDeviceNode == IMG_NULL)
+	{
+		return -EFAULT;
+	}
+
+	psDevInfo = psDeviceNode->pvDevice;
+
+
+	psRetOUT->eError = CopyFromUserWrapper(psPerProc,
+			                               ui32BridgeID,
+			                               &sMiscInfo,
+			                               psSGXGetMiscInfoIN->psMiscInfo,
+			                               sizeof(SGX_MISC_INFO));
+	if (psRetOUT->eError != PVRSRV_OK)
+	{
+		return -EFAULT;
+	}
+
+#ifdef SUPPORT_SGX_HWPERF
+	if (sMiscInfo.eRequest == SGX_MISC_INFO_REQUEST_HWPERF_RETRIEVE_CB)
+	{
+
+		IMG_VOID           * pAllocated;
+		IMG_HANDLE           hAllocatedHandle;
+		IMG_VOID           * psTmpUserData;
+		IMG_UINT32           allocatedSize;
+
+		allocatedSize = (IMG_UINT32)(sMiscInfo.uData.sRetrieveCB.ui32ArraySize * sizeof(PVRSRV_SGX_HWPERF_CBDATA));
+
+		ASSIGN_AND_EXIT_ON_ERROR(psRetOUT->eError,
+		                    OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+		                    allocatedSize,
+		                    &pAllocated,
+		                    &hAllocatedHandle,
+							"Array of Hardware Performance Circular Buffer Data"));
+
+
+		psTmpUserData = sMiscInfo.uData.sRetrieveCB.psHWPerfData;
+		sMiscInfo.uData.sRetrieveCB.psHWPerfData = pAllocated;
+
+		psRetOUT->eError = SGXGetMiscInfoKM(psDevInfo, &sMiscInfo, psDeviceNode, 0);
+		if (psRetOUT->eError != PVRSRV_OK)
+		{
+			OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+					  allocatedSize,
+					  pAllocated,
+					  hAllocatedHandle);
+
+			return 0;
+		}
+
+
+		psRetOUT->eError = CopyToUserWrapper(psPerProc,
+					                         ui32BridgeID,
+					                         psTmpUserData,
+					                         sMiscInfo.uData.sRetrieveCB.psHWPerfData,
+					                         allocatedSize);
+
+		sMiscInfo.uData.sRetrieveCB.psHWPerfData = psTmpUserData;
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  allocatedSize,
+				  pAllocated,
+			      hAllocatedHandle);
+
+		if (psRetOUT->eError != PVRSRV_OK)
+		{
+			return -EFAULT;
+		}
+	}
+	else
+#endif
+	{
+		psRetOUT->eError = SGXGetMiscInfoKM(psDevInfo, &sMiscInfo, psDeviceNode, hDevMemContextInt);
+
+		if (psRetOUT->eError != PVRSRV_OK)
+		{
+			return 0;
+		}
+	}
+
+
+	psRetOUT->eError = CopyToUserWrapper(psPerProc,
+		                             ui32BridgeID,
+		                             psSGXGetMiscInfoIN->psMiscInfo,
+		                             &sMiscInfo,
+		                             sizeof(SGX_MISC_INFO));
+	if (psRetOUT->eError != PVRSRV_OK)
+	{
+		return -EFAULT;
+	}
+	return 0;
+}
+
+
+#if defined(SUPPORT_SGX_HWPERF)
+static IMG_INT
+SGXReadDiffCountersBW(IMG_UINT32									ui32BridgeID,
+						PVRSRV_BRIDGE_IN_SGX_READ_DIFF_COUNTERS		*psSGXReadDiffCountersIN,
+						PVRSRV_BRIDGE_OUT_SGX_READ_DIFF_COUNTERS	*psSGXReadDiffCountersOUT,
+						PVRSRV_PER_PROCESS_DATA						*psPerProc)
+{
+	IMG_HANDLE			hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_READ_DIFF_COUNTERS);
+
+	psSGXReadDiffCountersOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+							&hDevCookieInt,
+							psSGXReadDiffCountersIN->hDevCookie,
+							PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psSGXReadDiffCountersOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psSGXReadDiffCountersOUT->eError = SGXReadDiffCountersKM(hDevCookieInt,
+							psSGXReadDiffCountersIN->ui32Reg,
+							&psSGXReadDiffCountersOUT->ui32Old,
+							psSGXReadDiffCountersIN->bNew,
+							psSGXReadDiffCountersIN->ui32New,
+							psSGXReadDiffCountersIN->ui32NewReset,
+							psSGXReadDiffCountersIN->ui32CountersReg,
+							psSGXReadDiffCountersIN->ui32Reg2,
+							&psSGXReadDiffCountersOUT->bActive,
+							&psSGXReadDiffCountersOUT->sDiffs);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXReadHWPerfCBBW(IMG_UINT32							ui32BridgeID,
+				  PVRSRV_BRIDGE_IN_SGX_READ_HWPERF_CB	*psSGXReadHWPerfCBIN,
+				  PVRSRV_BRIDGE_OUT_SGX_READ_HWPERF_CB	*psSGXReadHWPerfCBOUT,
+				  PVRSRV_PER_PROCESS_DATA				*psPerProc)
+{
+	IMG_HANDLE					hDevCookieInt;
+	PVRSRV_SGX_HWPERF_CB_ENTRY	*psAllocated;
+	IMG_HANDLE					hAllocatedHandle;
+	IMG_UINT32					ui32AllocatedSize;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_READ_HWPERF_CB);
+
+	psSGXReadHWPerfCBOUT->eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+							&hDevCookieInt,
+							psSGXReadHWPerfCBIN->hDevCookie,
+							PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psSGXReadHWPerfCBOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	ui32AllocatedSize = psSGXReadHWPerfCBIN->ui32ArraySize *
+							sizeof(psSGXReadHWPerfCBIN->psHWPerfCBData[0]);
+	ASSIGN_AND_EXIT_ON_ERROR(psSGXReadHWPerfCBOUT->eError,
+	                    OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+	                    ui32AllocatedSize,
+	                    (IMG_VOID **)&psAllocated,
+	                    &hAllocatedHandle,
+						"Array of Hardware Performance Circular Buffer Data"));
+
+	psSGXReadHWPerfCBOUT->eError = SGXReadHWPerfCBKM(hDevCookieInt,
+													 psSGXReadHWPerfCBIN->ui32ArraySize,
+													 psAllocated,
+													 &psSGXReadHWPerfCBOUT->ui32DataCount,
+													 &psSGXReadHWPerfCBOUT->ui32ClockSpeed,
+													 &psSGXReadHWPerfCBOUT->ui32HostTimeStamp);
+	if (psSGXReadHWPerfCBOUT->eError == PVRSRV_OK)
+	{
+		psSGXReadHWPerfCBOUT->eError = CopyToUserWrapper(psPerProc,
+		                                                 ui32BridgeID,
+		                                                 psSGXReadHWPerfCBIN->psHWPerfCBData,
+		                                                 psAllocated,
+		                                                 ui32AllocatedSize);
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  ui32AllocatedSize,
+			  psAllocated,
+			  hAllocatedHandle);
+
+
+	return 0;
+}
+#endif
+
+
+static IMG_INT
+SGXDevInitPart2BW(IMG_UINT32 ui32BridgeID,
+				  PVRSRV_BRIDGE_IN_SGXDEVINITPART2 *psSGXDevInitPart2IN,
+				  PVRSRV_BRIDGE_RETURN *psRetOUT,
+				  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_ERROR eError;
+	IMG_BOOL bDissociateFailed = IMG_FALSE;
+	IMG_BOOL bLookupFailed = IMG_FALSE;
+	IMG_BOOL bReleaseFailed = IMG_FALSE;
+	IMG_HANDLE hDummy;
+	IMG_UINT32 i;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_DEVINITPART2);
+
+	if(!psPerProc->bInitProcess)
+	{
+		psRetOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSGXDevInitPart2IN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+
+
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelCCBMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelCCBCtlMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelCCBEventKickerMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelSGXHostCtlMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelSGXTA3DCtlMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelSGXMiscMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+#if defined(SGX_SUPPORT_HWPROFILING)
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelHWProfilingMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(SUPPORT_SGX_HWPERF)
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelHWPerfCBMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(PVRSRV_USSE_EDM_STATUS_DEBUG)
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelEDMStatusBufferMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(SGX_FEATURE_SPM_MODE_0)
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDummy,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelTmpDPMStateMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+	for (i = 0; i < SGX_MAX_INIT_MEM_HANDLES; i++)
+	{
+		IMG_HANDLE hHandle = psSGXDevInitPart2IN->sInitInfo.asInitMemHandles[i];
+
+		if (hHandle == IMG_NULL)
+		{
+			continue;
+		}
+
+		eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+							   &hDummy,
+							   hHandle,
+							   PVRSRV_HANDLE_TYPE_MEM_INFO);
+		bLookupFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+	}
+
+	if (bLookupFailed)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "DevInitSGXPart2BW: A handle lookup failed"));
+		psRetOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelCCBMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelCCBMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelCCBCtlMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelCCBCtlMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelCCBEventKickerMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelCCBEventKickerMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelSGXHostCtlMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelSGXHostCtlMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelSGXTA3DCtlMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelSGXTA3DCtlMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelSGXMiscMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelSGXMiscMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+
+	#if defined(SGX_SUPPORT_HWPROFILING)
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelHWProfilingMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelHWProfilingMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(SUPPORT_SGX_HWPERF)
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelHWPerfCBMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelHWPerfCBMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(PVRSRV_USSE_EDM_STATUS_DEBUG)
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelEDMStatusBufferMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelEDMStatusBufferMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(SGX_FEATURE_SPM_MODE_0)
+	eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+						   &psSGXDevInitPart2IN->sInitInfo.hKernelTmpDPMStateMemInfo,
+						   psSGXDevInitPart2IN->sInitInfo.hKernelTmpDPMStateMemInfo,
+						   PVRSRV_HANDLE_TYPE_MEM_INFO);
+	bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+
+	for (i = 0; i < SGX_MAX_INIT_MEM_HANDLES; i++)
+	{
+		IMG_HANDLE *phHandle = &psSGXDevInitPart2IN->sInitInfo.asInitMemHandles[i];
+
+		if (*phHandle == IMG_NULL)
+			continue;
+
+		eError = PVRSRVLookupAndReleaseHandle(psPerProc->psHandleBase,
+							   phHandle,
+							   *phHandle,
+							   PVRSRV_HANDLE_TYPE_MEM_INFO);
+		bReleaseFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+	}
+
+	if (bReleaseFailed)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "DevInitSGXPart2BW: A handle release failed"));
+		psRetOUT->eError = PVRSRV_ERROR_GENERIC;
+
+		PVR_DBG_BREAK;
+		return 0;
+	}
+
+
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelCCBMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelCCBCtlMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelCCBEventKickerMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelSGXHostCtlMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelSGXTA3DCtlMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelSGXMiscMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+
+
+#if defined(SGX_SUPPORT_HWPROFILING)
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelHWProfilingMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(SUPPORT_SGX_HWPERF)
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelHWPerfCBMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(PVRSRV_USSE_EDM_STATUS_DEBUG)
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelEDMStatusBufferMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+#if defined(SGX_FEATURE_SPM_MODE_0)
+	eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelTmpDPMStateMemInfo);
+	bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+#endif
+
+	for (i = 0; i < SGX_MAX_INIT_MEM_HANDLES; i++)
+	{
+		IMG_HANDLE hHandle = psSGXDevInitPart2IN->sInitInfo.asInitMemHandles[i];
+
+		if (hHandle == IMG_NULL)
+			continue;
+
+		eError = PVRSRVDissociateDeviceMemKM(hDevCookieInt, hHandle);
+		bDissociateFailed |= (IMG_BOOL)(eError != PVRSRV_OK);
+	}
+
+
+
+
+	if(bDissociateFailed)
+	{
+		PVRSRVFreeDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelCCBMemInfo);
+		PVRSRVFreeDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelCCBCtlMemInfo);
+		PVRSRVFreeDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelSGXHostCtlMemInfo);
+		PVRSRVFreeDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelSGXTA3DCtlMemInfo);
+		PVRSRVFreeDeviceMemKM(hDevCookieInt, psSGXDevInitPart2IN->sInitInfo.hKernelSGXMiscMemInfo);
+
+		for (i = 0; i < SGX_MAX_INIT_MEM_HANDLES; i++)
+		{
+			IMG_HANDLE hHandle = psSGXDevInitPart2IN->sInitInfo.asInitMemHandles[i];
+
+			if (hHandle == IMG_NULL)
+				continue;
+
+			PVRSRVFreeDeviceMemKM(hDevCookieInt, (PVRSRV_KERNEL_MEM_INFO *)hHandle);
+
+		}
+
+		PVR_DPF((PVR_DBG_ERROR, "DevInitSGXPart2BW: A dissociate failed"));
+
+		psRetOUT->eError = PVRSRV_ERROR_GENERIC;
+
+
+		PVR_DBG_BREAK;
+		return 0;
+	}
+
+	psRetOUT->eError =
+		DevInitSGXPart2KM(psPerProc,
+						  hDevCookieInt,
+						  &psSGXDevInitPart2IN->sInitInfo);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXRegisterHWRenderContextBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_RENDER_CONTEXT *psSGXRegHWRenderContextIN,
+							 PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_RENDER_CONTEXT *psSGXRegHWRenderContextOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hHWRenderContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_REGISTER_HW_RENDER_CONTEXT);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psSGXRegHWRenderContextOUT->eError, psPerProc, 1);
+
+	psSGXRegHWRenderContextOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSGXRegHWRenderContextIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psSGXRegHWRenderContextOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	hHWRenderContextInt =
+		SGXRegisterHWRenderContextKM(hDevCookieInt,
+									 &psSGXRegHWRenderContextIN->sHWRenderContextDevVAddr,
+									 psPerProc);
+
+	if (hHWRenderContextInt == IMG_NULL)
+	{
+		psSGXRegHWRenderContextOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psSGXRegHWRenderContextOUT->hHWRenderContext,
+					  hHWRenderContextInt,
+					  PVRSRV_HANDLE_TYPE_SGX_HW_RENDER_CONTEXT,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psSGXRegHWRenderContextOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXUnregisterHWRenderContextBW(IMG_UINT32 ui32BridgeID,
+							   PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_RENDER_CONTEXT *psSGXUnregHWRenderContextIN,
+							   PVRSRV_BRIDGE_RETURN *psRetOUT,
+							   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hHWRenderContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_UNREGISTER_HW_RENDER_CONTEXT);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hHWRenderContextInt,
+						   psSGXUnregHWRenderContextIN->hHWRenderContext,
+						   PVRSRV_HANDLE_TYPE_SGX_HW_RENDER_CONTEXT);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = SGXUnregisterHWRenderContextKM(hHWRenderContextInt);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psSGXUnregHWRenderContextIN->hHWRenderContext,
+							PVRSRV_HANDLE_TYPE_SGX_HW_RENDER_CONTEXT);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXRegisterHWTransferContextBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_TRANSFER_CONTEXT *psSGXRegHWTransferContextIN,
+							 PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_TRANSFER_CONTEXT *psSGXRegHWTransferContextOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hHWTransferContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_REGISTER_HW_TRANSFER_CONTEXT);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psSGXRegHWTransferContextOUT->eError, psPerProc, 1);
+
+	psSGXRegHWTransferContextOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSGXRegHWTransferContextIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psSGXRegHWTransferContextOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	hHWTransferContextInt =
+		SGXRegisterHWTransferContextKM(hDevCookieInt,
+									   &psSGXRegHWTransferContextIN->sHWTransferContextDevVAddr,
+									   psPerProc);
+
+	if (hHWTransferContextInt == IMG_NULL)
+	{
+		psSGXRegHWTransferContextOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psSGXRegHWTransferContextOUT->hHWTransferContext,
+					  hHWTransferContextInt,
+					  PVRSRV_HANDLE_TYPE_SGX_HW_TRANSFER_CONTEXT,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psSGXRegHWTransferContextOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXUnregisterHWTransferContextBW(IMG_UINT32 ui32BridgeID,
+							   PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_TRANSFER_CONTEXT *psSGXUnregHWTransferContextIN,
+							   PVRSRV_BRIDGE_RETURN *psRetOUT,
+							   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hHWTransferContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_UNREGISTER_HW_TRANSFER_CONTEXT);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hHWTransferContextInt,
+						   psSGXUnregHWTransferContextIN->hHWTransferContext,
+						   PVRSRV_HANDLE_TYPE_SGX_HW_TRANSFER_CONTEXT);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = SGXUnregisterHWTransferContextKM(hHWTransferContextInt);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psSGXUnregHWTransferContextIN->hHWTransferContext,
+							PVRSRV_HANDLE_TYPE_SGX_HW_TRANSFER_CONTEXT);
+
+	return 0;
+}
+
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+static IMG_INT
+SGXRegisterHW2DContextBW(IMG_UINT32 ui32BridgeID,
+							 PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_2D_CONTEXT *psSGXRegHW2DContextIN,
+							 PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_2D_CONTEXT *psSGXRegHW2DContextOUT,
+							 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_HANDLE hHW2DContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_REGISTER_HW_2D_CONTEXT);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psSGXRegHW2DContextOUT->eError, psPerProc, 1);
+
+	psSGXRegHW2DContextOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSGXRegHW2DContextIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psSGXRegHW2DContextOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	hHW2DContextInt =
+		SGXRegisterHW2DContextKM(hDevCookieInt,
+								 &psSGXRegHW2DContextIN->sHW2DContextDevVAddr,
+								 psPerProc);
+
+	if (hHW2DContextInt == IMG_NULL)
+	{
+		psSGXRegHW2DContextOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psSGXRegHW2DContextOUT->hHW2DContext,
+					  hHW2DContextInt,
+					  PVRSRV_HANDLE_TYPE_SGX_HW_2D_CONTEXT,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psSGXRegHW2DContextOUT->eError, psPerProc);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXUnregisterHW2DContextBW(IMG_UINT32 ui32BridgeID,
+							   PVRSRV_BRIDGE_IN_SGX_UNREGISTER_HW_2D_CONTEXT *psSGXUnregHW2DContextIN,
+							   PVRSRV_BRIDGE_RETURN *psRetOUT,
+							   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hHW2DContextInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_UNREGISTER_HW_2D_CONTEXT);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hHW2DContextInt,
+						   psSGXUnregHW2DContextIN->hHW2DContext,
+						   PVRSRV_HANDLE_TYPE_SGX_HW_2D_CONTEXT);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError = SGXUnregisterHW2DContextKM(hHW2DContextInt);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+							psSGXUnregHW2DContextIN->hHW2DContext,
+							PVRSRV_HANDLE_TYPE_SGX_HW_2D_CONTEXT);
+
+	return 0;
+}
+#endif
+
+static IMG_INT
+SGXFlushHWRenderTargetBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_SGX_FLUSH_HW_RENDER_TARGET *psSGXFlushHWRenderTargetIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_FLUSH_HW_RENDER_TARGET);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSGXFlushHWRenderTargetIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	SGXFlushHWRenderTargetKM(hDevCookieInt, psSGXFlushHWRenderTargetIN->sHWRTDataSetDevVAddr);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGX2DQueryBlitsCompleteBW(IMG_UINT32 ui32BridgeID,
+						  PVRSRV_BRIDGE_IN_2DQUERYBLTSCOMPLETE *ps2DQueryBltsCompleteIN,
+						  PVRSRV_BRIDGE_RETURN *psRetOUT,
+						  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_VOID *pvSyncInfo;
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_2DQUERYBLTSCOMPLETE);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   ps2DQueryBltsCompleteIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &pvSyncInfo,
+						   ps2DQueryBltsCompleteIN->hKernSyncInfo,
+						   PVRSRV_HANDLE_TYPE_SYNC_INFO);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psDevInfo = (PVRSRV_SGXDEV_INFO *)((PVRSRV_DEVICE_NODE *)hDevCookieInt)->pvDevice;
+
+	psRetOUT->eError =
+		SGX2DQueryBlitsCompleteKM(psDevInfo,
+								  (PVRSRV_KERNEL_SYNC_INFO *)pvSyncInfo,
+								  ps2DQueryBltsCompleteIN->bWaitForComplete);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXFindSharedPBDescBW(IMG_UINT32 ui32BridgeID,
+					  PVRSRV_BRIDGE_IN_SGXFINDSHAREDPBDESC *psSGXFindSharedPBDescIN,
+					  PVRSRV_BRIDGE_OUT_SGXFINDSHAREDPBDESC *psSGXFindSharedPBDescOUT,
+					  PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_KERNEL_MEM_INFO *psSharedPBDescKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO *psHWPBDescKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO *psBlockKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO *psHWBlockKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO **ppsSharedPBDescSubKernelMemInfos = IMG_NULL;
+	IMG_UINT32 ui32SharedPBDescSubKernelMemInfosCount = 0;
+	IMG_UINT32 i;
+	IMG_HANDLE hSharedPBDesc = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psSGXFindSharedPBDescOUT->eError, psPerProc, PVRSRV_BRIDGE_SGX_SHAREDPBDESC_MAX_SUBMEMINFOS + 4);
+
+	psSGXFindSharedPBDescOUT->hSharedPBDesc = IMG_NULL;
+
+	psSGXFindSharedPBDescOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hDevCookieInt,
+						   psSGXFindSharedPBDescIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psSGXFindSharedPBDescOUT->eError != PVRSRV_OK)
+		goto PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC_EXIT;
+
+	psSGXFindSharedPBDescOUT->eError =
+		SGXFindSharedPBDescKM(psPerProc, hDevCookieInt,
+							  psSGXFindSharedPBDescIN->bLockOnFailure,
+							  psSGXFindSharedPBDescIN->ui32TotalPBSize,
+							  &hSharedPBDesc,
+							  &psSharedPBDescKernelMemInfo,
+							  &psHWPBDescKernelMemInfo,
+							  &psBlockKernelMemInfo,
+							  &psHWBlockKernelMemInfo,
+							  &ppsSharedPBDescSubKernelMemInfos,
+							  &ui32SharedPBDescSubKernelMemInfosCount);
+	if(psSGXFindSharedPBDescOUT->eError != PVRSRV_OK)
+		goto PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC_EXIT;
+
+	PVR_ASSERT(ui32SharedPBDescSubKernelMemInfosCount
+			   <= PVRSRV_BRIDGE_SGX_SHAREDPBDESC_MAX_SUBMEMINFOS);
+
+	psSGXFindSharedPBDescOUT->ui32SharedPBDescSubKernelMemInfoHandlesCount =
+		ui32SharedPBDescSubKernelMemInfosCount;
+
+	if(hSharedPBDesc == IMG_NULL)
+	{
+		psSGXFindSharedPBDescOUT->hSharedPBDescKernelMemInfoHandle = 0;
+
+		goto PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC_EXIT;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+					  &psSGXFindSharedPBDescOUT->hSharedPBDesc,
+					  hSharedPBDesc,
+					  PVRSRV_HANDLE_TYPE_SHARED_PB_DESC,
+					  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psSGXFindSharedPBDescOUT->hSharedPBDescKernelMemInfoHandle,
+					  psSharedPBDescKernelMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+					  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+					  psSGXFindSharedPBDescOUT->hSharedPBDesc);
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+					  &psSGXFindSharedPBDescOUT->hHWPBDescKernelMemInfoHandle,
+					  psHWPBDescKernelMemInfo,
+					  PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+					  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+					  psSGXFindSharedPBDescOUT->hSharedPBDesc);
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+				  &psSGXFindSharedPBDescOUT->hBlockKernelMemInfoHandle,
+				  psBlockKernelMemInfo,
+				  PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+				  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+				  psSGXFindSharedPBDescOUT->hSharedPBDesc);
+
+	PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+				  &psSGXFindSharedPBDescOUT->hHWBlockKernelMemInfoHandle,
+				  psHWBlockKernelMemInfo,
+				  PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+				  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+				  psSGXFindSharedPBDescOUT->hSharedPBDesc);
+
+
+	for(i=0; i<ui32SharedPBDescSubKernelMemInfosCount; i++)
+	{
+		PVRSRV_BRIDGE_OUT_SGXFINDSHAREDPBDESC *psSGXFindSharedPBDescOut =
+			psSGXFindSharedPBDescOUT;
+
+			PVRSRVAllocSubHandleNR(psPerProc->psHandleBase,
+							  &psSGXFindSharedPBDescOut->ahSharedPBDescSubKernelMemInfoHandles[i],
+							  ppsSharedPBDescSubKernelMemInfos[i],
+							  PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+							  PVRSRV_HANDLE_ALLOC_FLAG_MULTI,
+							  psSGXFindSharedPBDescOUT->hSharedPBDescKernelMemInfoHandle);
+	}
+
+PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC_EXIT:
+	if (ppsSharedPBDescSubKernelMemInfos != IMG_NULL)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_KERNEL_MEM_INFO *) * ui32SharedPBDescSubKernelMemInfosCount,
+				  ppsSharedPBDescSubKernelMemInfos,
+				  IMG_NULL);
+	}
+
+	if(psSGXFindSharedPBDescOUT->eError != PVRSRV_OK)
+	{
+		if(hSharedPBDesc != IMG_NULL)
+		{
+			SGXUnrefSharedPBDescKM(hSharedPBDesc);
+		}
+	}
+	else
+	{
+		COMMIT_HANDLE_BATCH_OR_ERROR(psSGXFindSharedPBDescOUT->eError, psPerProc);
+	}
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXUnrefSharedPBDescBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_SGXUNREFSHAREDPBDESC *psSGXUnrefSharedPBDescIN,
+					   PVRSRV_BRIDGE_OUT_SGXUNREFSHAREDPBDESC *psSGXUnrefSharedPBDescOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hSharedPBDesc;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_UNREFSHAREDPBDESC);
+
+	psSGXUnrefSharedPBDescOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase,
+						   &hSharedPBDesc,
+						   psSGXUnrefSharedPBDescIN->hSharedPBDesc,
+						   PVRSRV_HANDLE_TYPE_SHARED_PB_DESC);
+	if(psSGXUnrefSharedPBDescOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psSGXUnrefSharedPBDescOUT->eError =
+		SGXUnrefSharedPBDescKM(hSharedPBDesc);
+
+	if(psSGXUnrefSharedPBDescOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psSGXUnrefSharedPBDescOUT->eError =
+		PVRSRVReleaseHandle(psPerProc->psHandleBase,
+						   psSGXUnrefSharedPBDescIN->hSharedPBDesc,
+						   PVRSRV_HANDLE_TYPE_SHARED_PB_DESC);
+
+	return 0;
+}
+
+
+static IMG_INT
+SGXAddSharedPBDescBW(IMG_UINT32 ui32BridgeID,
+					 PVRSRV_BRIDGE_IN_SGXADDSHAREDPBDESC *psSGXAddSharedPBDescIN,
+					 PVRSRV_BRIDGE_OUT_SGXADDSHAREDPBDESC *psSGXAddSharedPBDescOUT,
+					 PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	PVRSRV_KERNEL_MEM_INFO *psSharedPBDescKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO *psHWPBDescKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO *psBlockKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO *psHWBlockKernelMemInfo;
+	IMG_UINT32 ui32KernelMemInfoHandlesCount =
+		psSGXAddSharedPBDescIN->ui32KernelMemInfoHandlesCount;
+	IMG_INT ret = 0;
+	IMG_HANDLE *phKernelMemInfoHandles = IMG_NULL;
+	PVRSRV_KERNEL_MEM_INFO **ppsKernelMemInfos = IMG_NULL;
+	IMG_UINT32 i;
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hSharedPBDesc = IMG_NULL;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psSGXAddSharedPBDescOUT->eError, psPerProc, 1);
+
+	psSGXAddSharedPBDescOUT->hSharedPBDesc = IMG_NULL;
+
+	PVR_ASSERT(ui32KernelMemInfoHandlesCount
+			   <= PVRSRV_BRIDGE_SGX_SHAREDPBDESC_MAX_SUBMEMINFOS);
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+								&hDevCookieInt,
+								psSGXAddSharedPBDescIN->hDevCookie,
+								PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+								(IMG_VOID **)&psSharedPBDescKernelMemInfo,
+								psSGXAddSharedPBDescIN->hSharedPBDescKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO);
+	if(eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+								(IMG_VOID **)&psHWPBDescKernelMemInfo,
+								psSGXAddSharedPBDescIN->hHWPBDescKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+								(IMG_VOID **)&psBlockKernelMemInfo,
+								psSGXAddSharedPBDescIN->hBlockKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO);
+	if(eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+								(IMG_VOID **)&psHWBlockKernelMemInfo,
+								psSGXAddSharedPBDescIN->hHWBlockKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_MEM_INFO);
+	if(eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+
+	if(!OSAccessOK(PVR_VERIFY_READ,
+				   psSGXAddSharedPBDescIN->phKernelMemInfoHandles,
+				   ui32KernelMemInfoHandlesCount * sizeof(IMG_HANDLE)))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC:"
+				 " Invalid phKernelMemInfos pointer", __FUNCTION__));
+		ret = -EFAULT;
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32KernelMemInfoHandlesCount * sizeof(IMG_HANDLE),
+				  (IMG_VOID **)&phKernelMemInfoHandles,
+				  0,
+				  "Array of Handles");
+	if (eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	if(CopyFromUserWrapper(psPerProc,
+			               ui32BridgeID,
+			               phKernelMemInfoHandles,
+						   psSGXAddSharedPBDescIN->phKernelMemInfoHandles,
+						   ui32KernelMemInfoHandlesCount * sizeof(IMG_HANDLE))
+	   != PVRSRV_OK)
+	{
+		ret = -EFAULT;
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32KernelMemInfoHandlesCount * sizeof(PVRSRV_KERNEL_MEM_INFO *),
+				  (IMG_VOID **)&ppsKernelMemInfos,
+				  0,
+				  "Array of pointers to Kernel Memory Info");
+	if (eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	for(i=0; i<ui32KernelMemInfoHandlesCount; i++)
+	{
+		eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+									(IMG_VOID **)&ppsKernelMemInfos[i],
+									phKernelMemInfoHandles[i],
+									PVRSRV_HANDLE_TYPE_MEM_INFO);
+		if(eError != PVRSRV_OK)
+		{
+			goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+		}
+	}
+
+
+
+	eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+								psSGXAddSharedPBDescIN->hSharedPBDescKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO);
+	PVR_ASSERT(eError == PVRSRV_OK);
+
+
+	eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+								psSGXAddSharedPBDescIN->hHWPBDescKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_MEM_INFO);
+	PVR_ASSERT(eError == PVRSRV_OK);
+
+
+	eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+								psSGXAddSharedPBDescIN->hBlockKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO);
+	PVR_ASSERT(eError == PVRSRV_OK);
+
+
+	eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+								psSGXAddSharedPBDescIN->hHWBlockKernelMemInfo,
+								PVRSRV_HANDLE_TYPE_MEM_INFO);
+	PVR_ASSERT(eError == PVRSRV_OK);
+
+	for(i=0; i<ui32KernelMemInfoHandlesCount; i++)
+	{
+
+		eError = PVRSRVReleaseHandle(psPerProc->psHandleBase,
+									phKernelMemInfoHandles[i],
+									PVRSRV_HANDLE_TYPE_MEM_INFO);
+		PVR_ASSERT(eError == PVRSRV_OK);
+	}
+
+	eError = SGXAddSharedPBDescKM(psPerProc, hDevCookieInt,
+								  psSharedPBDescKernelMemInfo,
+								  psHWPBDescKernelMemInfo,
+								  psBlockKernelMemInfo,
+								  psHWBlockKernelMemInfo,
+								  psSGXAddSharedPBDescIN->ui32TotalPBSize,
+								  &hSharedPBDesc,
+								  ppsKernelMemInfos,
+								  ui32KernelMemInfoHandlesCount);
+
+
+	if (eError != PVRSRV_OK)
+	{
+		goto PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT;
+	}
+
+	PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+				  &psSGXAddSharedPBDescOUT->hSharedPBDesc,
+				  hSharedPBDesc,
+				  PVRSRV_HANDLE_TYPE_SHARED_PB_DESC,
+				  PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+
+PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC_RETURN_RESULT:
+
+	if(phKernelMemInfoHandles)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  psSGXAddSharedPBDescIN->ui32KernelMemInfoHandlesCount * sizeof(IMG_HANDLE),
+				  (IMG_VOID *)phKernelMemInfoHandles,
+				  0);
+	}
+	if(ppsKernelMemInfos)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  psSGXAddSharedPBDescIN->ui32KernelMemInfoHandlesCount * sizeof(PVRSRV_KERNEL_MEM_INFO *),
+				  (IMG_VOID *)ppsKernelMemInfos,
+				  0);
+	}
+
+	if(ret == 0 && eError == PVRSRV_OK)
+	{
+		COMMIT_HANDLE_BATCH_OR_ERROR(psSGXAddSharedPBDescOUT->eError, psPerProc);
+	}
+
+	psSGXAddSharedPBDescOUT->eError = eError;
+
+	return ret;
+}
+
+static IMG_INT
+SGXGetInfoForSrvinitBW(IMG_UINT32 ui32BridgeID,
+					   PVRSRV_BRIDGE_IN_SGXINFO_FOR_SRVINIT *psSGXInfoForSrvinitIN,
+					   PVRSRV_BRIDGE_OUT_SGXINFO_FOR_SRVINIT *psSGXInfoForSrvinitOUT,
+					   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_HANDLE hDevCookieInt;
+	IMG_UINT32 i;
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGXINFO_FOR_SRVINIT);
+
+	NEW_HANDLE_BATCH_OR_ERROR(psSGXInfoForSrvinitOUT->eError, psPerProc, PVRSRV_MAX_CLIENT_HEAPS);
+
+	if(!psPerProc->bInitProcess)
+	{
+		psSGXInfoForSrvinitOUT->eError = PVRSRV_ERROR_GENERIC;
+		return 0;
+	}
+
+	psSGXInfoForSrvinitOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psSGXInfoForSrvinitIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+
+	if(psSGXInfoForSrvinitOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psSGXInfoForSrvinitOUT->eError =
+		SGXGetInfoForSrvinitKM(hDevCookieInt,
+							   &psSGXInfoForSrvinitOUT->sInitInfo);
+
+	if(psSGXInfoForSrvinitOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	for(i = 0; i < PVRSRV_MAX_CLIENT_HEAPS; i++)
+	{
+		PVRSRV_HEAP_INFO *psHeapInfo;
+
+		psHeapInfo = &psSGXInfoForSrvinitOUT->sInitInfo.asHeapInfo[i];
+
+		if (psHeapInfo->ui32HeapID != (IMG_UINT32)SGX_UNDEFINED_HEAP_ID)
+		{
+			IMG_HANDLE hDevMemHeapExt;
+
+			if (psHeapInfo->hDevMemHeap != IMG_NULL)
+			{
+
+				PVRSRVAllocHandleNR(psPerProc->psHandleBase,
+								  &hDevMemHeapExt,
+								  psHeapInfo->hDevMemHeap,
+								  PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP,
+								  PVRSRV_HANDLE_ALLOC_FLAG_SHARED);
+				psHeapInfo->hDevMemHeap = hDevMemHeapExt;
+			}
+		}
+	}
+
+	COMMIT_HANDLE_BATCH_OR_ERROR(psSGXInfoForSrvinitOUT->eError, psPerProc);
+
+	return 0;
+}
+
+#if defined(PDUMP)
+static IMG_VOID
+DumpBufferArray(PVRSRV_PER_PROCESS_DATA *psPerProc,
+				PSGX_KICKTA_DUMP_BUFFER	psBufferArray,
+				IMG_UINT32						ui32BufferArrayLength,
+				IMG_BOOL						bDumpPolls)
+{
+	IMG_UINT32	i;
+
+	for (i=0; i<ui32BufferArrayLength; i++)
+	{
+		PSGX_KICKTA_DUMP_BUFFER	psBuffer;
+		PVRSRV_KERNEL_MEM_INFO 	*psCtrlMemInfoKM;
+		IMG_CHAR * pszName;
+		IMG_HANDLE hUniqueTag;
+		IMG_UINT32	ui32Offset;
+
+		psBuffer = &psBufferArray[i];
+		pszName = psBuffer->pszName;
+		if (!pszName)
+		{
+			pszName = "Nameless buffer";
+		}
+
+		hUniqueTag = MAKEUNIQUETAG((PVRSRV_KERNEL_MEM_INFO *)psBuffer->hKernelMemInfo);
+
+	#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+		psCtrlMemInfoKM	= ((PVRSRV_KERNEL_MEM_INFO *)psBuffer->hCtrlKernelMemInfo);
+		ui32Offset =  psBuffer->sCtrlDevVAddr.uiAddr - psCtrlMemInfoKM->sDevVAddr.uiAddr;
+	#else
+		psCtrlMemInfoKM = ((PVRSRV_KERNEL_MEM_INFO *)psBuffer->hKernelMemInfo)->psKernelSyncInfo->psSyncDataMemInfoKM;
+		ui32Offset = offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete);
+	#endif
+
+		if (psBuffer->ui32Start <= psBuffer->ui32End)
+		{
+			if (bDumpPolls)
+			{
+				PDUMPCOMMENTWITHFLAGS(0, "Wait for %s space\r\n", pszName);
+				PDUMPCBP(psCtrlMemInfoKM,
+						 ui32Offset,
+						 psBuffer->ui32Start,
+						 psBuffer->ui32SpaceUsed,
+						 psBuffer->ui32BufferSize,
+						 0,
+						 MAKEUNIQUETAG(psCtrlMemInfoKM));
+			}
+
+			PDUMPCOMMENTWITHFLAGS(0, "%s\r\n", pszName);
+			PDUMPMEMUM(psPerProc,
+					 IMG_NULL,
+					 psBuffer->pvLinAddr,
+					 (PVRSRV_KERNEL_MEM_INFO*)psBuffer->hKernelMemInfo,
+					 psBuffer->ui32Start,
+					 psBuffer->ui32End - psBuffer->ui32Start,
+					 0,
+					 hUniqueTag);
+		}
+		else
+		{
+
+
+			if (bDumpPolls)
+			{
+				PDUMPCOMMENTWITHFLAGS(0, "Wait for %s space\r\n", pszName);
+				PDUMPCBP(psCtrlMemInfoKM,
+						 ui32Offset,
+						 psBuffer->ui32Start,
+						 psBuffer->ui32BackEndLength,
+						 psBuffer->ui32BufferSize,
+						 0,
+						 MAKEUNIQUETAG(psCtrlMemInfoKM));
+			}
+			PDUMPCOMMENTWITHFLAGS(0, "%s (part 1)\r\n", pszName);
+			PDUMPMEMUM(psPerProc,
+					 IMG_NULL,
+					 psBuffer->pvLinAddr,
+					 (PVRSRV_KERNEL_MEM_INFO*)psBuffer->hKernelMemInfo,
+					 psBuffer->ui32Start,
+					 psBuffer->ui32BackEndLength,
+					 0,
+					 hUniqueTag);
+
+			if (bDumpPolls)
+			{
+				PDUMPMEMPOL(psCtrlMemInfoKM,
+							ui32Offset,
+							0,
+							0xFFFFFFFF,
+							PDUMP_POLL_OPERATOR_NOTEQUAL,
+							0,
+							MAKEUNIQUETAG(psCtrlMemInfoKM));
+
+				PDUMPCOMMENTWITHFLAGS(0, "Wait for %s space\r\n", pszName);
+				PDUMPCBP(psCtrlMemInfoKM,
+						 ui32Offset,
+						 0,
+						 psBuffer->ui32End,
+						 psBuffer->ui32BufferSize,
+						 0,
+						 MAKEUNIQUETAG(psCtrlMemInfoKM));
+			}
+			PDUMPCOMMENTWITHFLAGS(0, "%s (part 2)\r\n", pszName);
+			PDUMPMEMUM(psPerProc,
+					 IMG_NULL,
+					 psBuffer->pvLinAddr,
+					 (PVRSRV_KERNEL_MEM_INFO*)psBuffer->hKernelMemInfo,
+					 0,
+					 psBuffer->ui32End,
+					 0,
+					 hUniqueTag);
+		}
+	}
+}
+static IMG_INT
+SGXPDumpBufferArrayBW(IMG_UINT32 ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_PDUMP_BUFFER_ARRAY *psPDumpBufferArrayIN,
+				   IMG_VOID *psBridgeOut,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 i;
+	SGX_KICKTA_DUMP_BUFFER *psKickTADumpBuffer;
+	IMG_UINT32 ui32BufferArrayLength =
+		psPDumpBufferArrayIN->ui32BufferArrayLength;
+	IMG_UINT32 ui32BufferArraySize =
+		ui32BufferArrayLength * sizeof(SGX_KICKTA_DUMP_BUFFER);
+	PVRSRV_ERROR eError = PVRSRV_ERROR_GENERIC;
+
+	PVR_UNREFERENCED_PARAMETER(psBridgeOut);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_PDUMP_BUFFER_ARRAY);
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32BufferArraySize,
+				  (IMG_PVOID *)&psKickTADumpBuffer, 0,
+				  "Array of Kick Tile Accelerator Dump Buffer") != PVRSRV_OK)
+	{
+		return -ENOMEM;
+	}
+
+	if(CopyFromUserWrapper(psPerProc,
+			               ui32BridgeID,
+						   psKickTADumpBuffer,
+						   psPDumpBufferArrayIN->psBufferArray,
+						   ui32BufferArraySize) != PVRSRV_OK)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, ui32BufferArraySize, psKickTADumpBuffer, 0);
+
+		return -EFAULT;
+	}
+
+	for(i = 0; i < ui32BufferArrayLength; i++)
+	{
+		IMG_VOID *pvMemInfo;
+
+		eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+									&pvMemInfo,
+									psKickTADumpBuffer[i].hKernelMemInfo,
+									PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRV_BRIDGE_SGX_PDUMP_BUFFER_ARRAY: "
+					 "PVRSRVLookupHandle failed (%d)", eError));
+			break;
+		}
+		psKickTADumpBuffer[i].hKernelMemInfo = pvMemInfo;
+
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+		eError = PVRSRVLookupHandle(psPerProc->psHandleBase,
+									&pvMemInfo,
+									psKickTADumpBuffer[i].hCtrlKernelMemInfo,
+									PVRSRV_HANDLE_TYPE_MEM_INFO);
+
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRV_BRIDGE_SGX_PDUMP_BUFFER_ARRAY: "
+					 "PVRSRVLookupHandle failed (%d)", eError));
+			break;
+		}
+		psKickTADumpBuffer[i].hCtrlKernelMemInfo = pvMemInfo;
+#endif
+	}
+
+	if(eError == PVRSRV_OK)
+	{
+		DumpBufferArray(psPerProc,
+						psKickTADumpBuffer,
+						ui32BufferArrayLength,
+						psPDumpBufferArrayIN->bDumpPolls);
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, ui32BufferArraySize, psKickTADumpBuffer, 0);
+
+
+	return 0;
+}
+
+static IMG_INT
+SGXPDump3DSignatureRegistersBW(IMG_UINT32 ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_PDUMP_3D_SIGNATURE_REGISTERS *psPDump3DSignatureRegistersIN,
+				   PVRSRV_BRIDGE_RETURN *psRetOUT,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 ui32RegisterArraySize =  psPDump3DSignatureRegistersIN->ui32NumRegisters * sizeof(IMG_UINT32);
+	IMG_UINT32 *pui32Registers = IMG_NULL;
+#if defined(SGX_FEATURE_MP)	&& defined(FIX_HW_BRN_27270)
+	PVRSRV_SGXDEV_INFO	*psDevInfo = IMG_NULL;
+	IMG_HANDLE 	hDevCookieInt;
+	IMG_UINT32	ui32RegVal = 0;
+#endif
+	IMG_INT ret = -EFAULT;
+
+	PVR_UNREFERENCED_PARAMETER(psRetOUT);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_PDUMP_3D_SIGNATURE_REGISTERS);
+
+	if (ui32RegisterArraySize == 0)
+	{
+		goto ExitNoError;
+	}
+
+#if defined(SGX_FEATURE_MP)	&& defined(FIX_HW_BRN_27270)
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psPDump3DSignatureRegistersIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpTASignatureRegistersBW: hDevCookie lookup failed"));
+		goto Exit;
+	}
+
+	psDevInfo = ((PVRSRV_DEVICE_NODE *)hDevCookieInt)->pvDevice;
+
+
+	ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_CORE);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_CORE, (SGX_FEATURE_MP_CORE_COUNT - 1) << EUR_CR_MASTER_CORE_ENABLE_SHIFT);
+#if defined(PDUMP)
+	PDUMPREGWITHFLAGS(EUR_CR_MASTER_CORE, (SGX_FEATURE_MP_CORE_COUNT - 1) << EUR_CR_MASTER_CORE_ENABLE_SHIFT,
+						psPDump3DSignatureRegistersIN->bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0);
+#endif
+#endif
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32RegisterArraySize,
+				  (IMG_PVOID *)&pui32Registers, 0,
+				  "Array of Registers") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDump3DSignatureRegistersBW: OSAllocMem failed"));
+		goto Exit;
+	}
+
+	if(CopyFromUserWrapper(psPerProc,
+			        	ui32BridgeID,
+					pui32Registers,
+					psPDump3DSignatureRegistersIN->pui32Registers,
+					ui32RegisterArraySize) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDump3DSignatureRegistersBW: CopyFromUserWrapper failed"));
+		goto Exit;
+	}
+
+	PDump3DSignatureRegisters(psPDump3DSignatureRegistersIN->ui32DumpFrameNum,
+					psPDump3DSignatureRegistersIN->bLastFrame,
+					pui32Registers,
+					psPDump3DSignatureRegistersIN->ui32NumRegisters);
+
+ExitNoError:
+	psRetOUT->eError = PVRSRV_OK;
+	ret = 0;
+Exit:
+	if (pui32Registers != IMG_NULL)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, ui32RegisterArraySize, pui32Registers, 0);
+	}
+
+#if defined(SGX_FEATURE_MP)	&& defined(FIX_HW_BRN_27270)
+	if (psDevInfo != IMG_NULL)
+	{
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_CORE, ui32RegVal);
+#if defined(PDUMP)
+		PDUMPREGWITHFLAGS(EUR_CR_MASTER_CORE, ui32RegVal,
+							psPDump3DSignatureRegistersIN->bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0);
+#endif
+	}
+#endif
+
+	return ret;
+}
+
+static IMG_INT
+SGXPDumpCounterRegistersBW(IMG_UINT32 ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_PDUMP_COUNTER_REGISTERS *psPDumpCounterRegistersIN,
+				   IMG_VOID *psBridgeOut,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 ui32RegisterArraySize =  psPDumpCounterRegistersIN->ui32NumRegisters * sizeof(IMG_UINT32);
+	IMG_UINT32 *pui32Registers = IMG_NULL;
+	IMG_INT ret = -EFAULT;
+
+	PVR_UNREFERENCED_PARAMETER(psBridgeOut);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_PDUMP_COUNTER_REGISTERS);
+
+	if (ui32RegisterArraySize == 0)
+	{
+		goto ExitNoError;
+	}
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32RegisterArraySize,
+				  (IMG_PVOID *)&pui32Registers, 0,
+				  "Array of Registers") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpCounterRegistersBW: OSAllocMem failed"));
+		ret = -ENOMEM;
+		goto Exit;
+	}
+
+	if(CopyFromUserWrapper(psPerProc,
+			        	ui32BridgeID,
+					pui32Registers,
+					psPDumpCounterRegistersIN->pui32Registers,
+					ui32RegisterArraySize) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpCounterRegistersBW: CopyFromUserWrapper failed"));
+		goto Exit;
+	}
+
+	PDumpCounterRegisters(psPDumpCounterRegistersIN->ui32DumpFrameNum,
+					psPDumpCounterRegistersIN->bLastFrame,
+					pui32Registers,
+					psPDumpCounterRegistersIN->ui32NumRegisters);
+
+ExitNoError:
+	ret = 0;
+Exit:
+	if (pui32Registers != IMG_NULL)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, ui32RegisterArraySize, pui32Registers, 0);
+	}
+
+	return ret;
+}
+
+static IMG_INT
+SGXPDumpTASignatureRegistersBW(IMG_UINT32 ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_PDUMP_TA_SIGNATURE_REGISTERS *psPDumpTASignatureRegistersIN,
+				   PVRSRV_BRIDGE_RETURN *psRetOUT,
+				   PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	IMG_UINT32 ui32RegisterArraySize =  psPDumpTASignatureRegistersIN->ui32NumRegisters * sizeof(IMG_UINT32);
+	IMG_UINT32 *pui32Registers = IMG_NULL;
+#if defined(SGX_FEATURE_MP)	&& defined(FIX_HW_BRN_27270)
+	PVRSRV_SGXDEV_INFO	*psDevInfo = IMG_NULL;
+	IMG_HANDLE hDevCookieInt;
+	IMG_UINT32	ui32RegVal = 0;
+#endif
+	IMG_INT ret = -EFAULT;
+
+	PVR_UNREFERENCED_PARAMETER(psRetOUT);
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_PDUMP_TA_SIGNATURE_REGISTERS);
+
+	if (ui32RegisterArraySize == 0)
+	{
+		goto ExitNoError;
+	}
+
+#if defined(SGX_FEATURE_MP)	&& defined(FIX_HW_BRN_27270)
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psPDumpTASignatureRegistersIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpTASignatureRegistersBW: hDevCookie lookup failed"));
+		goto Exit;
+	}
+
+	psDevInfo = ((PVRSRV_DEVICE_NODE *)hDevCookieInt)->pvDevice;
+
+
+	ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_CORE);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_CORE, (SGX_FEATURE_MP_CORE_COUNT - 1) << EUR_CR_MASTER_CORE_ENABLE_SHIFT);
+#if defined(PDUMP)
+	PDUMPREGWITHFLAGS(EUR_CR_MASTER_CORE, (SGX_FEATURE_MP_CORE_COUNT - 1) << EUR_CR_MASTER_CORE_ENABLE_SHIFT,
+						psPDumpTASignatureRegistersIN->bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0);
+#endif
+#endif
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  ui32RegisterArraySize,
+				  (IMG_PVOID *)&pui32Registers, 0,
+				  "Array of Registers") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpTASignatureRegistersBW: OSAllocMem failed"));
+		ret = -ENOMEM;
+		goto Exit;
+	}
+
+	if(CopyFromUserWrapper(psPerProc,
+			        	ui32BridgeID,
+					pui32Registers,
+					psPDumpTASignatureRegistersIN->pui32Registers,
+					ui32RegisterArraySize) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpTASignatureRegistersBW: CopyFromUserWrapper failed"));
+		goto Exit;
+	}
+
+	PDumpTASignatureRegisters(psPDumpTASignatureRegistersIN->ui32DumpFrameNum,
+					psPDumpTASignatureRegistersIN->ui32TAKickCount,
+					psPDumpTASignatureRegistersIN->bLastFrame,
+					pui32Registers,
+					psPDumpTASignatureRegistersIN->ui32NumRegisters);
+
+ExitNoError:
+	psRetOUT->eError = PVRSRV_OK;
+	ret = 0;
+Exit:
+	if (pui32Registers != IMG_NULL)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, ui32RegisterArraySize, pui32Registers, 0);
+	}
+
+#if defined(SGX_FEATURE_MP)	&& defined(FIX_HW_BRN_27270)
+	if (psDevInfo != IMG_NULL)
+	{
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_CORE, ui32RegVal);
+#if defined(PDUMP)
+		PDUMPREGWITHFLAGS(EUR_CR_MASTER_CORE, ui32RegVal,
+							psPDumpTASignatureRegistersIN->bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0);
+#endif
+	}
+#endif
+
+	return ret;
+}
+static IMG_INT
+SGXPDumpHWPerfCBBW(IMG_UINT32						ui32BridgeID,
+				   PVRSRV_BRIDGE_IN_PDUMP_HWPERFCB	*psPDumpHWPerfCBIN,
+				   PVRSRV_BRIDGE_RETURN 			*psRetOUT,
+				   PVRSRV_PER_PROCESS_DATA 			*psPerProc)
+{
+#if defined(SUPPORT_SGX_HWPERF)
+#if defined(__linux__)
+	PVRSRV_SGXDEV_INFO	*psDevInfo;
+	IMG_HANDLE			hDevCookieInt;
+
+	PVRSRV_BRIDGE_ASSERT_CMD(ui32BridgeID, PVRSRV_BRIDGE_SGX_PDUMP_HWPERFCB);
+
+	psRetOUT->eError =
+		PVRSRVLookupHandle(psPerProc->psHandleBase, &hDevCookieInt,
+						   psPDumpHWPerfCBIN->hDevCookie,
+						   PVRSRV_HANDLE_TYPE_DEV_NODE);
+	if(psRetOUT->eError != PVRSRV_OK)
+	{
+		return 0;
+	}
+
+	psDevInfo = ((PVRSRV_DEVICE_NODE *)hDevCookieInt)->pvDevice;
+
+	PDumpHWPerfCBKM(&psPDumpHWPerfCBIN->szFileName[0],
+					psPDumpHWPerfCBIN->ui32FileOffset,
+					psDevInfo->psKernelHWPerfCBMemInfo->sDevVAddr,
+					psDevInfo->psKernelHWPerfCBMemInfo->ui32AllocSize,
+					psPDumpHWPerfCBIN->ui32PDumpFlags);
+
+	return 0;
+#else
+	PVR_UNREFERENCED_PARAMETER(ui32BridgeID);
+	PVR_UNREFERENCED_PARAMETER(psPDumpHWPerfCBIN);
+	PVR_UNREFERENCED_PARAMETER(psRetOUT);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	return 0;
+#endif
+#else
+	PVR_UNREFERENCED_PARAMETER(ui32BridgeID);
+	PVR_UNREFERENCED_PARAMETER(psPDumpHWPerfCBIN);
+	PVR_UNREFERENCED_PARAMETER(psRetOUT);
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	return -EFAULT;
+#endif
+}
+
+#endif
+
+
+IMG_VOID SetSGXDispatchTableEntry(IMG_VOID)
+{
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_GETCLIENTINFO, SGXGetClientInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_RELEASECLIENTINFO, SGXReleaseClientInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_GETINTERNALDEVINFO, SGXGetInternalDevInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_DOKICK, SGXDoKickBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_GETPHYSPAGEADDR, DummyBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_READREGISTRYDWORD, DummyBW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_2DQUERYBLTSCOMPLETE, SGX2DQueryBlitsCompleteBW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_GETMMUPDADDR, DummyBW);
+
+#if defined(TRANSFER_QUEUE)
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_SUBMITTRANSFER, SGXSubmitTransferBW);
+#endif
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_GETMISCINFO, SGXGetMiscInfoBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGXINFO_FOR_SRVINIT	, SGXGetInfoForSrvinitBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_DEVINITPART2, SGXDevInitPart2BW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_FINDSHAREDPBDESC, SGXFindSharedPBDescBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_UNREFSHAREDPBDESC, SGXUnrefSharedPBDescBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_ADDSHAREDPBDESC, SGXAddSharedPBDescBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_REGISTER_HW_RENDER_CONTEXT, SGXRegisterHWRenderContextBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_FLUSH_HW_RENDER_TARGET, SGXFlushHWRenderTargetBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_UNREGISTER_HW_RENDER_CONTEXT, SGXUnregisterHWRenderContextBW);
+#if defined(SGX_FEATURE_2D_HARDWARE)
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_SUBMIT2D, SGXSubmit2DBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_REGISTER_HW_2D_CONTEXT, SGXRegisterHW2DContextBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_UNREGISTER_HW_2D_CONTEXT, SGXUnregisterHW2DContextBW);
+#endif
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_REGISTER_HW_TRANSFER_CONTEXT, SGXRegisterHWTransferContextBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_UNREGISTER_HW_TRANSFER_CONTEXT, SGXUnregisterHWTransferContextBW);
+
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_SCHEDULE_PROCESS_QUEUES, SGXScheduleProcessQueuesBW);
+
+#if defined(SUPPORT_SGX_HWPERF)
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_READ_DIFF_COUNTERS, SGXReadDiffCountersBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_READ_HWPERF_CB, SGXReadHWPerfCBBW);
+#endif
+
+#if defined(PDUMP)
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_PDUMP_BUFFER_ARRAY, SGXPDumpBufferArrayBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_PDUMP_3D_SIGNATURE_REGISTERS, SGXPDump3DSignatureRegistersBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_PDUMP_COUNTER_REGISTERS, SGXPDumpCounterRegistersBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_PDUMP_TA_SIGNATURE_REGISTERS, SGXPDumpTASignatureRegistersBW);
+	SetDispatchTableEntry(PVRSRV_BRIDGE_SGX_PDUMP_HWPERFCB, SGXPDumpHWPerfCBBW);
+#endif
+}
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.h
new file mode 100644
index 0000000..ba2a3c8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/bridged/sgx/bridged_sgx_bridge.h
@@ -0,0 +1,38 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __BRIDGED_SGX_BRIDGE_H__
+#define __BRIDGED_SGX_BRIDGE_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+
+IMG_VOID SetSGXDispatchTableEntry(IMG_VOID);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/buffer_manager.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/buffer_manager.c
new file mode 100644
index 0000000..ecb5d03
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/buffer_manager.c
@@ -0,0 +1,2069 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+
+#include "sysconfig.h"
+#include "hash.h"
+#include "ra.h"
+#include "pdump_km.h"
+
+#define MIN(a,b)       (a > b ? b : a)
+
+
+#include "lists.h"
+
+DECLARE_LIST_ANY_VA(BM_HEAP);
+DECLARE_LIST_ANY_2(BM_HEAP, PVRSRV_ERROR, PVRSRV_OK);
+DECLARE_LIST_ANY_VA_2(BM_HEAP, PVRSRV_ERROR, PVRSRV_OK);
+DECLARE_LIST_FOR_EACH_VA(BM_HEAP);
+DECLARE_LIST_INSERT(BM_HEAP);
+DECLARE_LIST_REMOVE(BM_HEAP);
+
+DECLARE_LIST_FOR_EACH(BM_CONTEXT);
+DECLARE_LIST_ANY_VA(BM_CONTEXT);
+DECLARE_LIST_ANY_VA_2(BM_CONTEXT, IMG_HANDLE, IMG_NULL);
+DECLARE_LIST_INSERT(BM_CONTEXT);
+DECLARE_LIST_REMOVE(BM_CONTEXT);
+
+
+static IMG_BOOL
+ZeroBuf(BM_BUF *pBuf, BM_MAPPING *pMapping, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags);
+static IMG_VOID
+BM_FreeMemory (IMG_VOID *pH, IMG_UINTPTR_T base, BM_MAPPING *psMapping);
+static IMG_BOOL
+BM_ImportMemory(IMG_VOID *pH, IMG_SIZE_T uSize,
+					IMG_SIZE_T *pActualSize, BM_MAPPING **ppsMapping,
+					IMG_UINT32 uFlags, IMG_UINTPTR_T *pBase);
+
+static IMG_BOOL
+DevMemoryAlloc (BM_CONTEXT *pBMContext,
+				BM_MAPPING *pMapping,
+				IMG_SIZE_T *pActualSize,
+				IMG_UINT32 uFlags,
+				IMG_UINT32 dev_vaddr_alignment,
+				IMG_DEV_VIRTADDR *pDevVAddr);
+static IMG_VOID
+DevMemoryFree (BM_MAPPING *pMapping);
+
+static IMG_BOOL
+AllocMemory (BM_CONTEXT				*pBMContext,
+				BM_HEAP				*psBMHeap,
+				IMG_DEV_VIRTADDR	*psDevVAddr,
+				IMG_SIZE_T			uSize,
+				IMG_UINT32			uFlags,
+				IMG_UINT32			uDevVAddrAlignment,
+				BM_BUF				*pBuf)
+{
+	BM_MAPPING			*pMapping;
+	IMG_UINTPTR_T		uOffset;
+	RA_ARENA			*pArena = IMG_NULL;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "AllocMemory (pBMContext=%08X, uSize=0x%x, uFlags=0x%x, align=0x%x, pBuf=%08X)",
+			  pBMContext, uSize, uFlags, uDevVAddrAlignment, pBuf));
+
+
+
+
+	if(uFlags & PVRSRV_MEM_RAM_BACKED_ALLOCATION)
+	{
+		if(uFlags & PVRSRV_MEM_USER_SUPPLIED_DEVVADDR)
+		{
+
+			PVR_DPF ((PVR_DBG_ERROR, "AllocMemory: combination of DevVAddr management and RAM backing mode unsupported"));
+			return IMG_FALSE;
+		}
+
+
+
+
+		if(psBMHeap->ui32Attribs
+		   &	(PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG
+		   |PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG))
+		{
+
+			pArena = psBMHeap->pImportArena;
+		}
+		else
+		{
+			PVR_DPF ((PVR_DBG_ERROR, "AllocMemory: backing store type doesn't match heap"));
+			return IMG_FALSE;
+		}
+
+
+		if (!RA_Alloc(pArena,
+					  uSize,
+					  IMG_NULL,
+					  (IMG_VOID*) &pMapping,
+					  uFlags,
+					  uDevVAddrAlignment,
+					  0,
+					  (IMG_UINTPTR_T *)&(pBuf->DevVAddr.uiAddr)))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "AllocMemory: RA_Alloc(0x%x) FAILED", uSize));
+			return IMG_FALSE;
+		}
+
+		uOffset = pBuf->DevVAddr.uiAddr - pMapping->DevVAddr.uiAddr;
+		if(pMapping->CpuVAddr)
+		{
+			pBuf->CpuVAddr = (IMG_VOID*) ((IMG_UINTPTR_T)pMapping->CpuVAddr + uOffset);
+		}
+		else
+		{
+			pBuf->CpuVAddr = IMG_NULL;
+		}
+
+		if(uSize == pMapping->uSize)
+		{
+			pBuf->hOSMemHandle = pMapping->hOSMemHandle;
+		}
+		else
+		{
+			if(OSGetSubMemHandle(pMapping->hOSMemHandle,
+								 uOffset,
+								 uSize,
+								 psBMHeap->ui32Attribs,
+								 &pBuf->hOSMemHandle)!=PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "AllocMemory: OSGetSubMemHandle FAILED"));
+				return IMG_FALSE;
+			}
+		}
+
+
+		pBuf->CpuPAddr.uiAddr = pMapping->CpuPAddr.uiAddr + uOffset;
+
+		if(uFlags & PVRSRV_MEM_ZERO)
+		{
+			if(!ZeroBuf(pBuf, pMapping, uSize, psBMHeap->ui32Attribs | uFlags))
+			{
+				return IMG_FALSE;
+			}
+		}
+	}
+	else
+	{
+		if(uFlags & PVRSRV_MEM_USER_SUPPLIED_DEVVADDR)
+		{
+
+			PVR_ASSERT(psDevVAddr != IMG_NULL);
+
+			if (psDevVAddr == IMG_NULL)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "AllocMemory: invalid parameter - psDevVAddr"));
+				return IMG_FALSE;
+			}
+
+
+			pBMContext->psDeviceNode->pfnMMUAlloc (psBMHeap->pMMUHeap,
+													uSize,
+													IMG_NULL,
+													PVRSRV_MEM_USER_SUPPLIED_DEVVADDR,
+													uDevVAddrAlignment,
+													psDevVAddr);
+
+
+			pBuf->DevVAddr = *psDevVAddr;
+		}
+		else
+		{
+
+
+
+			pBMContext->psDeviceNode->pfnMMUAlloc (psBMHeap->pMMUHeap,
+													uSize,
+													IMG_NULL,
+													0,
+													uDevVAddrAlignment,
+													&pBuf->DevVAddr);
+		}
+
+
+		if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+							sizeof (struct _BM_MAPPING_),
+							(IMG_PVOID *)&pMapping, IMG_NULL,
+							"Buffer Manager Mapping") != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "AllocMemory: OSAllocMem(0x%x) FAILED"));
+			return IMG_FALSE;
+		}
+
+
+		pBuf->CpuVAddr = IMG_NULL;
+		pBuf->hOSMemHandle = 0;
+		pBuf->CpuPAddr.uiAddr = 0;
+
+
+		pMapping->CpuVAddr = IMG_NULL;
+		pMapping->CpuPAddr.uiAddr = 0;
+		pMapping->DevVAddr = pBuf->DevVAddr;
+		pMapping->psSysAddr = IMG_NULL;
+		pMapping->uSize = uSize;
+		pMapping->hOSMemHandle = 0;
+	}
+
+
+	pMapping->pArena = pArena;
+
+
+	pMapping->pBMHeap = psBMHeap;
+	pBuf->pMapping = pMapping;
+
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+				"AllocMemory: pMapping=%08X: DevV=%08X CpuV=%08X CpuP=%08X uSize=0x%x",
+				pMapping,
+				pMapping->DevVAddr.uiAddr,
+				pMapping->CpuVAddr,
+				pMapping->CpuPAddr.uiAddr,
+				pMapping->uSize));
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+				"AllocMemory: pBuf=%08X: DevV=%08X CpuV=%08X CpuP=%08X uSize=0x%x",
+				pBuf,
+				pBuf->DevVAddr.uiAddr,
+				pBuf->CpuVAddr,
+				pBuf->CpuPAddr.uiAddr,
+				uSize));
+
+
+	PVR_ASSERT(((pBuf->DevVAddr.uiAddr) & (uDevVAddrAlignment - 1)) == 0);
+
+	return IMG_TRUE;
+}
+
+
+static IMG_BOOL
+WrapMemory (BM_HEAP *psBMHeap,
+			IMG_SIZE_T uSize,
+			IMG_SIZE_T ui32BaseOffset,
+			IMG_BOOL bPhysContig,
+			IMG_SYS_PHYADDR *psAddr,
+			IMG_VOID *pvCPUVAddr,
+			IMG_UINT32 uFlags,
+			BM_BUF *pBuf)
+{
+	IMG_DEV_VIRTADDR DevVAddr = {0};
+	BM_MAPPING *pMapping;
+	IMG_BOOL bResult;
+	IMG_SIZE_T const ui32PageSize = HOST_PAGESIZE();
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "WrapMemory(psBMHeap=%08X, size=0x%x, offset=0x%x, bPhysContig=0x%x, pvCPUVAddr = 0x%x, flags=0x%x, pBuf=%08X)",
+			  psBMHeap, uSize, ui32BaseOffset, bPhysContig, pvCPUVAddr, uFlags, pBuf));
+
+	PVR_ASSERT((psAddr->uiAddr & (ui32PageSize - 1)) == 0);
+
+	PVR_ASSERT(((IMG_UINTPTR_T)pvCPUVAddr & (ui32PageSize - 1)) == 0);
+
+	uSize += ui32BaseOffset;
+	uSize = HOST_PAGEALIGN (uSize);
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(*pMapping),
+						(IMG_PVOID *)&pMapping, IMG_NULL,
+						"Mocked-up mapping") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "WrapMemory: OSAllocMem(0x%x) FAILED",sizeof(*pMapping)));
+		return IMG_FALSE;
+	}
+
+	OSMemSet(pMapping, 0, sizeof (*pMapping));
+
+	pMapping->uSize = uSize;
+	pMapping->pBMHeap = psBMHeap;
+
+	if(pvCPUVAddr)
+	{
+		pMapping->CpuVAddr = pvCPUVAddr;
+
+		if (bPhysContig)
+		{
+			pMapping->eCpuMemoryOrigin = hm_wrapped_virtaddr;
+			pMapping->CpuPAddr = SysSysPAddrToCpuPAddr(psAddr[0]);
+
+			if(OSRegisterMem(pMapping->CpuPAddr,
+							pMapping->CpuVAddr,
+							pMapping->uSize,
+							uFlags,
+							&pMapping->hOSMemHandle) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,	"WrapMemory: OSRegisterMem Phys=0x%08X, CpuVAddr = 0x%08X, Size=%d) failed",
+					pMapping->CpuPAddr, pMapping->CpuVAddr, pMapping->uSize));
+				goto fail_cleanup;
+			}
+		}
+		else
+		{
+			pMapping->eCpuMemoryOrigin = hm_wrapped_scatter_virtaddr;
+			pMapping->psSysAddr = psAddr;
+
+			if(OSRegisterDiscontigMem(pMapping->psSysAddr,
+							pMapping->CpuVAddr,
+							pMapping->uSize,
+							uFlags,
+							&pMapping->hOSMemHandle) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,	"WrapMemory: OSRegisterDiscontigMem CpuVAddr = 0x%08X, Size=%d) failed",
+					pMapping->CpuVAddr, pMapping->uSize));
+				goto fail_cleanup;
+			}
+		}
+	}
+	else
+	{
+		if (bPhysContig)
+		{
+			pMapping->eCpuMemoryOrigin = hm_wrapped;
+			pMapping->CpuPAddr = SysSysPAddrToCpuPAddr(psAddr[0]);
+
+			if(OSReservePhys(pMapping->CpuPAddr,
+							 pMapping->uSize,
+							 uFlags,
+							 &pMapping->CpuVAddr,
+							 &pMapping->hOSMemHandle) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,	"WrapMemory: OSReservePhys Phys=0x%08X, Size=%d) failed",
+					pMapping->CpuPAddr, pMapping->uSize));
+				goto fail_cleanup;
+			}
+		}
+		else
+		{
+			pMapping->eCpuMemoryOrigin = hm_wrapped_scatter;
+			pMapping->psSysAddr = psAddr;
+
+			if(OSReserveDiscontigPhys(pMapping->psSysAddr,
+							 pMapping->uSize,
+							 uFlags,
+							 &pMapping->CpuVAddr,
+							 &pMapping->hOSMemHandle) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,	"WrapMemory: OSReserveDiscontigPhys Size=%d) failed",
+					pMapping->uSize));
+				goto fail_cleanup;
+			}
+		}
+	}
+
+
+	bResult = DevMemoryAlloc(psBMHeap->pBMContext,
+							 pMapping,
+							 IMG_NULL,
+							 uFlags | PVRSRV_MEM_READ | PVRSRV_MEM_WRITE,
+							 IMG_CAST_TO_DEVVADDR_UINT(ui32PageSize),
+							 &DevVAddr);
+	if (!bResult)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+				"WrapMemory: DevMemoryAlloc(0x%x) failed",
+				pMapping->uSize));
+		goto fail_cleanup;
+	}
+
+
+	pBuf->CpuPAddr.uiAddr = pMapping->CpuPAddr.uiAddr + ui32BaseOffset;
+	if(!ui32BaseOffset)
+	{
+		pBuf->hOSMemHandle = pMapping->hOSMemHandle;
+	}
+	else
+	{
+		if(OSGetSubMemHandle(pMapping->hOSMemHandle,
+							 ui32BaseOffset,
+							 (pMapping->uSize-ui32BaseOffset),
+							 uFlags,
+							 &pBuf->hOSMemHandle)!=PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "WrapMemory: OSGetSubMemHandle failed"));
+			goto fail_cleanup;
+		}
+	}
+	if(pMapping->CpuVAddr)
+	{
+		pBuf->CpuVAddr = (IMG_VOID*) ((IMG_UINTPTR_T)pMapping->CpuVAddr + ui32BaseOffset);
+	}
+	pBuf->DevVAddr.uiAddr = pMapping->DevVAddr.uiAddr + IMG_CAST_TO_DEVVADDR_UINT(ui32BaseOffset);
+
+	if(uFlags & PVRSRV_MEM_ZERO)
+	{
+		if(!ZeroBuf(pBuf, pMapping, uSize, uFlags))
+		{
+			return IMG_FALSE;
+		}
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "DevVaddr.uiAddr=%08X", DevVAddr.uiAddr));
+	PVR_DPF ((PVR_DBG_MESSAGE,
+				"WrapMemory: pMapping=%08X: DevV=%08X CpuV=%08X CpuP=%08X uSize=0x%x",
+				pMapping, pMapping->DevVAddr.uiAddr,
+				pMapping->CpuVAddr, pMapping->CpuPAddr.uiAddr, pMapping->uSize));
+	PVR_DPF ((PVR_DBG_MESSAGE,
+				"WrapMemory: pBuf=%08X: DevV=%08X CpuV=%08X CpuP=%08X uSize=0x%x",
+				pBuf, pBuf->DevVAddr.uiAddr,
+				pBuf->CpuVAddr, pBuf->CpuPAddr.uiAddr, uSize));
+
+	pBuf->pMapping = pMapping;
+	return IMG_TRUE;
+
+fail_cleanup:
+	if(ui32BaseOffset && pBuf->hOSMemHandle)
+	{
+		OSReleaseSubMemHandle(pBuf->hOSMemHandle, uFlags);
+	}
+
+	if(pMapping && (pMapping->CpuVAddr || pMapping->hOSMemHandle))
+	{
+		switch(pMapping->eCpuMemoryOrigin)
+		{
+			case hm_wrapped:
+				OSUnReservePhys(pMapping->CpuVAddr, pMapping->uSize, uFlags, pMapping->hOSMemHandle);
+				break;
+			case hm_wrapped_virtaddr:
+				OSUnRegisterMem(pMapping->CpuVAddr, pMapping->uSize, uFlags, pMapping->hOSMemHandle);
+				break;
+			case hm_wrapped_scatter:
+				OSUnReserveDiscontigPhys(pMapping->CpuVAddr, pMapping->uSize, uFlags, pMapping->hOSMemHandle);
+				break;
+			case hm_wrapped_scatter_virtaddr:
+				OSUnRegisterDiscontigMem(pMapping->CpuVAddr, pMapping->uSize, uFlags, pMapping->hOSMemHandle);
+				break;
+			default:
+				break;
+		}
+
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_MAPPING), pMapping, IMG_NULL);
+
+
+	return IMG_FALSE;
+}
+
+
+static IMG_BOOL
+ZeroBuf(BM_BUF *pBuf, BM_MAPPING *pMapping, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags)
+{
+	IMG_VOID *pvCpuVAddr;
+
+	if(pBuf->CpuVAddr)
+	{
+		OSMemSet(pBuf->CpuVAddr, 0, ui32Bytes);
+	}
+	else if(pMapping->eCpuMemoryOrigin == hm_contiguous
+			|| pMapping->eCpuMemoryOrigin == hm_wrapped)
+	{
+		pvCpuVAddr = OSMapPhysToLin(pBuf->CpuPAddr,
+									ui32Bytes,
+									PVRSRV_HAP_KERNEL_ONLY
+									| (ui32Flags & PVRSRV_HAP_CACHETYPE_MASK),
+									IMG_NULL);
+		if(!pvCpuVAddr)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "ZeroBuf: OSMapPhysToLin for contiguous buffer failed"));
+			return IMG_FALSE;
+		}
+		OSMemSet(pvCpuVAddr, 0, ui32Bytes);
+		OSUnMapPhysToLin(pvCpuVAddr,
+						 ui32Bytes,
+						 PVRSRV_HAP_KERNEL_ONLY
+						 | (ui32Flags & PVRSRV_HAP_CACHETYPE_MASK),
+						 IMG_NULL);
+	}
+	else
+	{
+		IMG_SIZE_T ui32BytesRemaining = ui32Bytes;
+		IMG_SIZE_T ui32CurrentOffset = 0;
+		IMG_CPU_PHYADDR CpuPAddr;
+
+
+		PVR_ASSERT(pBuf->hOSMemHandle);
+
+		while(ui32BytesRemaining > 0)
+		{
+			IMG_SIZE_T ui32BlockBytes = MIN(ui32BytesRemaining, HOST_PAGESIZE());
+			CpuPAddr = OSMemHandleToCpuPAddr(pBuf->hOSMemHandle, ui32CurrentOffset);
+
+			if(CpuPAddr.uiAddr & (HOST_PAGESIZE() -1))
+			{
+				ui32BlockBytes =
+					MIN(ui32BytesRemaining, HOST_PAGEALIGN(CpuPAddr.uiAddr) - CpuPAddr.uiAddr);
+			}
+
+			pvCpuVAddr = OSMapPhysToLin(CpuPAddr,
+										ui32BlockBytes,
+										PVRSRV_HAP_KERNEL_ONLY
+										| (ui32Flags & PVRSRV_HAP_CACHETYPE_MASK),
+										IMG_NULL);
+			if(!pvCpuVAddr)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "ZeroBuf: OSMapPhysToLin while zeroing non-contiguous memory FAILED"));
+				return IMG_FALSE;
+			}
+			OSMemSet(pvCpuVAddr, 0, ui32BlockBytes);
+			OSUnMapPhysToLin(pvCpuVAddr,
+							 ui32BlockBytes,
+							 PVRSRV_HAP_KERNEL_ONLY
+							 | (ui32Flags & PVRSRV_HAP_CACHETYPE_MASK),
+							 IMG_NULL);
+
+			ui32BytesRemaining -= ui32BlockBytes;
+			ui32CurrentOffset += ui32BlockBytes;
+		}
+	}
+
+	return IMG_TRUE;
+}
+
+static IMG_VOID
+FreeBuf (BM_BUF *pBuf, IMG_UINT32 ui32Flags, IMG_BOOL bFromAllocator)
+{
+	BM_MAPPING *pMapping;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			"FreeBuf: pBuf=%08X: DevVAddr=%08X CpuVAddr=%08X CpuPAddr=%08X",
+			pBuf, pBuf->DevVAddr.uiAddr, pBuf->CpuVAddr, pBuf->CpuPAddr.uiAddr));
+
+
+	pMapping = pBuf->pMapping;
+
+	if(ui32Flags & PVRSRV_MEM_USER_SUPPLIED_DEVVADDR)
+	{
+		if ((pBuf->ui32ExportCount == 0) && (pBuf->ui32RefCount == 0))
+		{
+
+			if(ui32Flags & PVRSRV_MEM_RAM_BACKED_ALLOCATION)
+			{
+
+				PVR_DPF ((PVR_DBG_ERROR, "FreeBuf: combination of DevVAddr management and RAM backing mode unsupported"));
+			}
+			else
+			{
+
+				OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_MAPPING), pMapping, IMG_NULL);
+				pBuf->pMapping = IMG_NULL;
+			}
+		}
+	}
+	else
+	{
+
+		if(pBuf->hOSMemHandle != pMapping->hOSMemHandle)
+		{
+			if ((pBuf->ui32ExportCount == 0) && (pBuf->ui32RefCount == 0))
+			{
+
+				OSReleaseSubMemHandle(pBuf->hOSMemHandle, ui32Flags);
+			}
+		}
+		if(ui32Flags & PVRSRV_MEM_RAM_BACKED_ALLOCATION)
+		{
+
+			if ((pBuf->ui32ExportCount == 0) && (pBuf->ui32RefCount == 0))
+			{
+
+
+
+				PVR_ASSERT(pBuf->ui32ExportCount == 0);
+				RA_Free (pBuf->pMapping->pArena, pBuf->DevVAddr.uiAddr, IMG_FALSE);
+			}
+		}
+		else
+		{
+			if ((pBuf->ui32ExportCount == 0) && (pBuf->ui32RefCount == 0))
+			{
+				switch (pMapping->eCpuMemoryOrigin)
+				{
+					case hm_wrapped:
+						OSUnReservePhys(pMapping->CpuVAddr, pMapping->uSize, ui32Flags, pMapping->hOSMemHandle);
+						break;
+					case hm_wrapped_virtaddr:
+						OSUnRegisterMem(pMapping->CpuVAddr, pMapping->uSize, ui32Flags, pMapping->hOSMemHandle);
+						break;
+					case hm_wrapped_scatter:
+						OSUnReserveDiscontigPhys(pMapping->CpuVAddr, pMapping->uSize, ui32Flags, pMapping->hOSMemHandle);
+						break;
+					case hm_wrapped_scatter_virtaddr:
+						OSUnRegisterDiscontigMem(pMapping->CpuVAddr, pMapping->uSize, ui32Flags, pMapping->hOSMemHandle);
+						break;
+					default:
+						break;
+				}
+			}
+
+			if (bFromAllocator)
+				DevMemoryFree (pMapping);
+
+			if ((pBuf->ui32ExportCount == 0) && (pBuf->ui32RefCount == 0))
+			{
+
+				OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_MAPPING), pMapping, IMG_NULL);
+				pBuf->pMapping = IMG_NULL;
+			}
+		}
+
+		if ((pBuf->ui32ExportCount == 0) && (pBuf->ui32RefCount == 0))
+		{
+			OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_BUF), pBuf, IMG_NULL);
+
+		}
+	}
+}
+
+PVRSRV_ERROR BM_DestroyContext_AnyCb(BM_HEAP *psBMHeap)
+{
+	if(psBMHeap->ui32Attribs
+	& 	(PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG
+		|PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG))
+	{
+		if (psBMHeap->pImportArena)
+		{
+			IMG_BOOL bTestDelete = RA_TestDelete(psBMHeap->pImportArena);
+			if (!bTestDelete)
+			{
+				PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyContext_AnyCb: RA_TestDelete failed"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+		}
+	}
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR
+BM_DestroyContext(IMG_HANDLE	hBMContext,
+				  IMG_BOOL		*pbDestroyed)
+{
+	PVRSRV_ERROR eError;
+	BM_CONTEXT *pBMContext = (BM_CONTEXT*)hBMContext;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "BM_DestroyContext"));
+
+	if (pbDestroyed != IMG_NULL)
+	{
+		*pbDestroyed = IMG_FALSE;
+	}
+
+
+
+	if (pBMContext == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyContext: Invalid handle"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	pBMContext->ui32RefCount--;
+
+	if (pBMContext->ui32RefCount > 0)
+	{
+
+		return PVRSRV_OK;
+	}
+
+
+
+
+	eError = List_BM_HEAP_PVRSRV_ERROR_Any(pBMContext->psBMHeap, BM_DestroyContext_AnyCb);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyContext: List_BM_HEAP_PVRSRV_ERROR_Any failed"));
+#if 0
+
+
+
+
+		PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyContext: Cleaning up with ResManFreeSpecial"));
+		if(ResManFreeSpecial() != PVRSRV_OK)
+		{
+			PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyContext: ResManFreeSpecial failed %d",eError));
+		}
+
+#endif
+		return eError;
+	}
+	else
+	{
+
+		eError = ResManFreeResByPtr(pBMContext->hResItem);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyContext: ResManFreeResByPtr failed %d",eError));
+			return eError;
+		}
+
+
+		if (pbDestroyed != IMG_NULL)
+		{
+			*pbDestroyed = IMG_TRUE;
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR BM_DestroyContextCallBack_AnyVaCb(BM_HEAP *psBMHeap, va_list va)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	psDeviceNode = va_arg(va, PVRSRV_DEVICE_NODE*);
+
+
+	if(psBMHeap->ui32Attribs
+	& 	(PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG
+		|PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG))
+	{
+		if (psBMHeap->pImportArena)
+		{
+			RA_Delete (psBMHeap->pImportArena);
+		}
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_DestroyContext: backing store type unsupported"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	psDeviceNode->pfnMMUDelete(psBMHeap->pMMUHeap);
+
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_HEAP), psBMHeap, IMG_NULL);
+
+
+	return PVRSRV_OK;
+}
+
+
+static PVRSRV_ERROR BM_DestroyContextCallBack(IMG_PVOID		pvParam,
+											  IMG_UINT32	ui32Param)
+{
+	BM_CONTEXT *pBMContext = pvParam;
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+
+
+	psDeviceNode = pBMContext->psDeviceNode;
+
+
+
+	if(List_BM_HEAP_PVRSRV_ERROR_Any_va(pBMContext->psBMHeap,
+										BM_DestroyContextCallBack_AnyVaCb,
+										psDeviceNode) != PVRSRV_OK)
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	if (pBMContext->psMMUContext)
+	{
+		psDeviceNode->pfnMMUFinalise(pBMContext->psMMUContext);
+	}
+
+
+
+	if (pBMContext->pBufferHash)
+	{
+		HASH_Delete(pBMContext->pBufferHash);
+	}
+
+	if (pBMContext == psDeviceNode->sDevMemoryInfo.pBMKernelContext)
+	{
+
+		psDeviceNode->sDevMemoryInfo.pBMKernelContext = IMG_NULL;
+	}
+	else
+	{
+
+		List_BM_CONTEXT_Remove(pBMContext);
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_CONTEXT), pBMContext, IMG_NULL);
+
+
+	return PVRSRV_OK;
+}
+
+
+IMG_HANDLE BM_CreateContext_IncRefCount_AnyVaCb(BM_CONTEXT *pBMContext, va_list va)
+{
+	PRESMAN_CONTEXT	hResManContext;
+	hResManContext = va_arg(va, PRESMAN_CONTEXT);
+	if(ResManFindResourceByPtr(hResManContext, pBMContext->hResItem) == PVRSRV_OK)
+	{
+
+		pBMContext->ui32RefCount++;
+		return pBMContext;
+	}
+	return IMG_NULL;
+}
+
+IMG_VOID BM_CreateContext_InsertHeap_ForEachVaCb(BM_HEAP *psBMHeap, va_list va)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	BM_CONTEXT *pBMContext;
+	psDeviceNode = va_arg(va, PVRSRV_DEVICE_NODE*);
+	pBMContext = va_arg(va, BM_CONTEXT*);
+	switch(psBMHeap->sDevArena.DevMemHeapType)
+	{
+		case DEVICE_MEMORY_HEAP_SHARED:
+		case DEVICE_MEMORY_HEAP_SHARED_EXPORTED:
+		{
+
+			psDeviceNode->pfnMMUInsertHeap(pBMContext->psMMUContext, psBMHeap->pMMUHeap);
+			break;
+		}
+	}
+}
+
+IMG_HANDLE
+BM_CreateContext(PVRSRV_DEVICE_NODE			*psDeviceNode,
+				 IMG_DEV_PHYADDR			*psPDDevPAddr,
+				 PVRSRV_PER_PROCESS_DATA	*psPerProc,
+				 IMG_BOOL					*pbCreated)
+{
+	BM_CONTEXT			*pBMContext;
+	DEVICE_MEMORY_INFO	*psDevMemoryInfo;
+	IMG_BOOL			bKernelContext;
+	PRESMAN_CONTEXT		hResManContext;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "BM_CreateContext"));
+
+	if (psPerProc == IMG_NULL)
+	{
+		bKernelContext = IMG_TRUE;
+		hResManContext = psDeviceNode->hResManContext;
+	}
+	else
+	{
+		bKernelContext = IMG_FALSE;
+		hResManContext = psPerProc->hResManContext;
+	}
+
+	if (pbCreated != IMG_NULL)
+	{
+		*pbCreated = IMG_FALSE;
+	}
+
+
+	psDevMemoryInfo = &psDeviceNode->sDevMemoryInfo;
+
+	if (bKernelContext == IMG_FALSE)
+	{
+		IMG_HANDLE res = (IMG_HANDLE) List_BM_CONTEXT_Any_va(psDevMemoryInfo->pBMContext,
+															BM_CreateContext_IncRefCount_AnyVaCb,
+															hResManContext);
+		if (res)
+		{
+			return res;
+		}
+	}
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof (struct _BM_CONTEXT_),
+					 (IMG_PVOID *)&pBMContext, IMG_NULL,
+					 "Buffer Manager Context") != PVRSRV_OK)
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_CreateContext: Alloc failed"));
+		return IMG_NULL;
+	}
+	OSMemSet(pBMContext, 0, sizeof (BM_CONTEXT));
+
+
+	pBMContext->psDeviceNode = psDeviceNode;
+
+
+
+	pBMContext->pBufferHash = HASH_Create(32);
+	if (pBMContext->pBufferHash==IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_CreateContext: HASH_Create failed"));
+		goto cleanup;
+	}
+
+	if(psDeviceNode->pfnMMUInitialise(psDeviceNode,
+										&pBMContext->psMMUContext,
+										psPDDevPAddr) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_CreateContext: MMUInitialise failed"));
+		goto cleanup;
+	}
+
+	if(bKernelContext)
+	{
+
+		PVR_ASSERT(psDevMemoryInfo->pBMKernelContext == IMG_NULL);
+		psDevMemoryInfo->pBMKernelContext = pBMContext;
+	}
+	else
+	{
+
+
+
+
+
+		PVR_ASSERT(psDevMemoryInfo->pBMKernelContext);
+
+		if (psDevMemoryInfo->pBMKernelContext == IMG_NULL)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "BM_CreateContext: psDevMemoryInfo->pBMKernelContext invalid"));
+			goto cleanup;
+		}
+
+		PVR_ASSERT(psDevMemoryInfo->pBMKernelContext->psBMHeap);
+
+
+
+
+
+		pBMContext->psBMSharedHeap = psDevMemoryInfo->pBMKernelContext->psBMHeap;
+
+
+
+
+		List_BM_HEAP_ForEach_va(pBMContext->psBMSharedHeap,
+								BM_CreateContext_InsertHeap_ForEachVaCb,
+								psDeviceNode,
+								pBMContext);
+
+
+		List_BM_CONTEXT_Insert(&psDevMemoryInfo->pBMContext, pBMContext);
+	}
+
+
+	pBMContext->ui32RefCount++;
+
+
+	pBMContext->hResItem = ResManRegisterRes(hResManContext,
+											RESMAN_TYPE_DEVICEMEM_CONTEXT,
+											pBMContext,
+											0,
+											BM_DestroyContextCallBack);
+	if (pBMContext->hResItem == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_CreateContext: ResManRegisterRes failed"));
+		goto cleanup;
+	}
+
+	if (pbCreated != IMG_NULL)
+	{
+		*pbCreated = IMG_TRUE;
+	}
+	return (IMG_HANDLE)pBMContext;
+
+cleanup:
+	(IMG_VOID)BM_DestroyContextCallBack(pBMContext, 0);
+
+	return IMG_NULL;
+}
+
+
+IMG_VOID *BM_CreateHeap_AnyVaCb(BM_HEAP *psBMHeap, va_list va)
+{
+	DEVICE_MEMORY_HEAP_INFO *psDevMemHeapInfo;
+	psDevMemHeapInfo = va_arg(va, DEVICE_MEMORY_HEAP_INFO*);
+	if (psBMHeap->sDevArena.ui32HeapID ==  psDevMemHeapInfo->ui32HeapID)
+	{
+
+		return psBMHeap;
+	}
+	else
+	{
+		return IMG_NULL;
+	}
+}
+
+IMG_HANDLE
+BM_CreateHeap (IMG_HANDLE hBMContext,
+			   DEVICE_MEMORY_HEAP_INFO *psDevMemHeapInfo)
+{
+	BM_CONTEXT *pBMContext = (BM_CONTEXT*)hBMContext;
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	BM_HEAP *psBMHeap;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "BM_CreateHeap"));
+
+	if(!pBMContext)
+	{
+		return IMG_NULL;
+	}
+
+	psDeviceNode = pBMContext->psDeviceNode;
+
+
+
+
+
+
+	if(pBMContext->ui32RefCount > 0)
+	{
+		psBMHeap = (BM_HEAP*)List_BM_HEAP_Any_va(pBMContext->psBMHeap,
+												 BM_CreateHeap_AnyVaCb,
+												 psDevMemHeapInfo);
+
+		if (psBMHeap)
+		{
+			return psBMHeap;
+		}
+	}
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof (BM_HEAP),
+						(IMG_PVOID *)&psBMHeap, IMG_NULL,
+						"Buffer Manager Heap") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_CreateHeap: Alloc failed"));
+		return IMG_NULL;
+	}
+
+	OSMemSet (psBMHeap, 0, sizeof (BM_HEAP));
+
+	psBMHeap->sDevArena.ui32HeapID = psDevMemHeapInfo->ui32HeapID;
+	psBMHeap->sDevArena.pszName = psDevMemHeapInfo->pszName;
+	psBMHeap->sDevArena.BaseDevVAddr = psDevMemHeapInfo->sDevVAddrBase;
+	psBMHeap->sDevArena.ui32Size = psDevMemHeapInfo->ui32HeapSize;
+	psBMHeap->sDevArena.DevMemHeapType = psDevMemHeapInfo->DevMemHeapType;
+	psBMHeap->sDevArena.ui32DataPageSize = psDevMemHeapInfo->ui32DataPageSize;
+	psBMHeap->sDevArena.psDeviceMemoryHeapInfo = psDevMemHeapInfo;
+	psBMHeap->ui32Attribs = psDevMemHeapInfo->ui32Attribs;
+
+
+	psBMHeap->pBMContext = pBMContext;
+
+	psBMHeap->pMMUHeap = psDeviceNode->pfnMMUCreate (pBMContext->psMMUContext,
+													&psBMHeap->sDevArena,
+													&psBMHeap->pVMArena);
+	if (!psBMHeap->pMMUHeap)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_CreateHeap: MMUCreate failed"));
+		goto ErrorExit;
+	}
+
+
+	psBMHeap->pImportArena = RA_Create (psDevMemHeapInfo->pszBSName,
+										0, 0, IMG_NULL,
+										psBMHeap->sDevArena.ui32DataPageSize,
+										BM_ImportMemory,
+										BM_FreeMemory,
+										IMG_NULL,
+										psBMHeap);
+	if(psBMHeap->pImportArena == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_CreateHeap: RA_Create failed"));
+		goto ErrorExit;
+	}
+
+	if(psBMHeap->ui32Attribs & PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG)
+	{
+
+
+
+
+		psBMHeap->pLocalDevMemArena = psDevMemHeapInfo->psLocalDevMemArena;
+		if(psBMHeap->pLocalDevMemArena == IMG_NULL)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "BM_CreateHeap: LocalDevMemArena null"));
+			goto ErrorExit;
+		}
+	}
+
+
+	List_BM_HEAP_Insert(&pBMContext->psBMHeap, psBMHeap);
+
+	return (IMG_HANDLE)psBMHeap;
+
+
+ErrorExit:
+
+
+	if (psBMHeap->pMMUHeap != IMG_NULL)
+	{
+		psDeviceNode->pfnMMUDelete (psBMHeap->pMMUHeap);
+		psDeviceNode->pfnMMUFinalise (pBMContext->psMMUContext);
+	}
+
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_HEAP), psBMHeap, IMG_NULL);
+
+
+	return IMG_NULL;
+}
+
+IMG_VOID
+BM_DestroyHeap (IMG_HANDLE hDevMemHeap)
+{
+	BM_HEAP* psBMHeap = (BM_HEAP*)hDevMemHeap;
+	PVRSRV_DEVICE_NODE *psDeviceNode = psBMHeap->pBMContext->psDeviceNode;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "BM_DestroyHeap"));
+
+	if(psBMHeap)
+	{
+
+		if(psBMHeap->ui32Attribs
+		&	(PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG
+			|PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG))
+		{
+			if (psBMHeap->pImportArena)
+			{
+				RA_Delete (psBMHeap->pImportArena);
+			}
+		}
+		else
+		{
+			PVR_DPF((PVR_DBG_ERROR, "BM_DestroyHeap: backing store type unsupported"));
+			return;
+		}
+
+
+		psDeviceNode->pfnMMUDelete (psBMHeap->pMMUHeap);
+
+
+		List_BM_HEAP_Remove(psBMHeap);
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_HEAP), psBMHeap, IMG_NULL);
+
+	}
+	else
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_DestroyHeap: invalid heap handle"));
+	}
+}
+
+
+IMG_BOOL
+BM_Reinitialise (PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+
+	PVR_DPF((PVR_DBG_MESSAGE, "BM_Reinitialise"));
+	PVR_UNREFERENCED_PARAMETER(psDeviceNode);
+
+
+	return IMG_TRUE;
+}
+
+IMG_BOOL
+BM_Alloc (  IMG_HANDLE			hDevMemHeap,
+			IMG_DEV_VIRTADDR	*psDevVAddr,
+			IMG_SIZE_T			uSize,
+			IMG_UINT32			*pui32Flags,
+			IMG_UINT32			uDevVAddrAlignment,
+			BM_HANDLE			*phBuf)
+{
+	BM_BUF *pBuf;
+	BM_CONTEXT *pBMContext;
+	BM_HEAP *psBMHeap;
+	SYS_DATA *psSysData;
+	IMG_UINT32 uFlags;
+
+	if (pui32Flags == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_Alloc: invalid parameter"));
+		PVR_DBG_BREAK;
+		return IMG_FALSE;
+	}
+
+	uFlags = *pui32Flags;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+		  "BM_Alloc (uSize=0x%x, uFlags=0x%x, uDevVAddrAlignment=0x%x)",
+			uSize, uFlags, uDevVAddrAlignment));
+
+	SysAcquireData(&psSysData);
+
+	psBMHeap = (BM_HEAP*)hDevMemHeap;
+	pBMContext = psBMHeap->pBMContext;
+
+	if(uDevVAddrAlignment == 0)
+	{
+		uDevVAddrAlignment = 1;
+	}
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				   sizeof (BM_BUF),
+				   (IMG_PVOID *)&pBuf, IMG_NULL,
+				   "Buffer Manager buffer") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_Alloc: BM_Buf alloc FAILED"));
+		return IMG_FALSE;
+	}
+	OSMemSet(pBuf, 0, sizeof (BM_BUF));
+
+
+	if (AllocMemory(pBMContext,
+					psBMHeap,
+					psDevVAddr,
+					uSize,
+					uFlags,
+					uDevVAddrAlignment,
+					pBuf) != IMG_TRUE)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof (BM_BUF), pBuf, IMG_NULL);
+
+		PVR_DPF((PVR_DBG_ERROR, "BM_Alloc: AllocMemory FAILED"));
+		return IMG_FALSE;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+		  "BM_Alloc (uSize=0x%x, uFlags=0x%x)=%08X",
+		  uSize, uFlags, pBuf));
+
+
+	pBuf->ui32RefCount = 1;
+	*phBuf = (BM_HANDLE)pBuf;
+	*pui32Flags = uFlags | psBMHeap->ui32Attribs;
+
+
+	if(uFlags & PVRSRV_HAP_CACHETYPE_MASK)
+	{
+		*pui32Flags &= ~PVRSRV_HAP_CACHETYPE_MASK;
+		*pui32Flags |= (uFlags & PVRSRV_HAP_CACHETYPE_MASK);
+	}
+
+	return IMG_TRUE;
+}
+
+
+
+#if defined(PVR_LMA)
+static IMG_BOOL
+ValidSysPAddrArrayForDev(PVRSRV_DEVICE_NODE *psDeviceNode, IMG_SYS_PHYADDR *psSysPAddr, IMG_UINT32 ui32PageCount, IMG_SIZE_T ui32PageSize)
+{
+	IMG_UINT32 i;
+
+	for (i = 0; i < ui32PageCount; i++)
+	{
+		IMG_SYS_PHYADDR sStartSysPAddr = psSysPAddr[i];
+		IMG_SYS_PHYADDR sEndSysPAddr;
+
+		if (!SysVerifySysPAddrToDevPAddr(psDeviceNode->sDevId.eDeviceType, sStartSysPAddr))
+		{
+			return IMG_FALSE;
+		}
+
+		sEndSysPAddr.uiAddr = sStartSysPAddr.uiAddr + ui32PageSize;
+
+		if (!SysVerifySysPAddrToDevPAddr(psDeviceNode->sDevId.eDeviceType, sEndSysPAddr))
+		{
+			return IMG_FALSE;
+		}
+	}
+
+	return IMG_TRUE;
+}
+
+static IMG_BOOL
+ValidSysPAddrRangeForDev(PVRSRV_DEVICE_NODE *psDeviceNode, IMG_SYS_PHYADDR sStartSysPAddr, IMG_SIZE_T ui32Range)
+{
+	IMG_SYS_PHYADDR sEndSysPAddr;
+
+	if (!SysVerifySysPAddrToDevPAddr(psDeviceNode->sDevId.eDeviceType, sStartSysPAddr))
+	{
+		return IMG_FALSE;
+	}
+
+	sEndSysPAddr.uiAddr = sStartSysPAddr.uiAddr + ui32Range;
+
+	if (!SysVerifySysPAddrToDevPAddr(psDeviceNode->sDevId.eDeviceType, sEndSysPAddr))
+	{
+		return IMG_FALSE;
+	}
+
+	return IMG_TRUE;
+}
+
+#define	WRAP_MAPPING_SIZE(ui32ByteSize, ui32PageOffset) HOST_PAGEALIGN((ui32ByteSize) + (ui32PageOffset))
+
+#define	WRAP_PAGE_COUNT(ui32ByteSize, ui32PageOffset, ui32HostPageSize)	(WRAP_MAPPING_SIZE(ui32ByteSize, ui32PageOffset) / (ui32HostPageSize))
+
+#endif
+
+
+IMG_BOOL
+BM_Wrap (	IMG_HANDLE hDevMemHeap,
+			IMG_SIZE_T ui32Size,
+			IMG_SIZE_T ui32Offset,
+			IMG_BOOL bPhysContig,
+			IMG_SYS_PHYADDR *psSysAddr,
+			IMG_VOID *pvCPUVAddr,
+			IMG_UINT32 *pui32Flags,
+			BM_HANDLE *phBuf)
+{
+	BM_BUF *pBuf;
+	BM_CONTEXT *psBMContext;
+	BM_HEAP *psBMHeap;
+	SYS_DATA *psSysData;
+	IMG_SYS_PHYADDR sHashAddress;
+	IMG_UINT32 uFlags;
+
+	psBMHeap = (BM_HEAP*)hDevMemHeap;
+	psBMContext = psBMHeap->pBMContext;
+
+	uFlags = psBMHeap->ui32Attribs & (PVRSRV_HAP_CACHETYPE_MASK | PVRSRV_HAP_MAPTYPE_MASK);
+
+	if ((pui32Flags != IMG_NULL) && ((*pui32Flags & PVRSRV_HAP_CACHETYPE_MASK) != 0))
+	{
+		uFlags &= ~PVRSRV_HAP_CACHETYPE_MASK;
+		uFlags |= *pui32Flags & PVRSRV_HAP_CACHETYPE_MASK;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+		  "BM_Wrap (uSize=0x%x, uOffset=0x%x, bPhysContig=0x%x, pvCPUVAddr=0x%x, uFlags=0x%x)",
+			ui32Size, ui32Offset, bPhysContig, pvCPUVAddr, uFlags));
+
+	SysAcquireData(&psSysData);
+
+#if defined(PVR_LMA)
+	if (bPhysContig)
+	{
+		if (!ValidSysPAddrRangeForDev(psBMContext->psDeviceNode, *psSysAddr, WRAP_MAPPING_SIZE(ui32Size, ui32Offset)))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "BM_Wrap: System address range invalid for device"));
+			return IMG_FALSE;
+		}
+	}
+	else
+	{
+		IMG_SIZE_T ui32HostPageSize = HOST_PAGESIZE();
+
+		if (!ValidSysPAddrArrayForDev(psBMContext->psDeviceNode, psSysAddr, WRAP_PAGE_COUNT(ui32Size, ui32Offset, ui32HostPageSize), ui32HostPageSize))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "BM_Wrap: Array of system addresses invalid for device"));
+			return IMG_FALSE;
+		}
+	}
+#endif
+
+	sHashAddress = psSysAddr[0];
+
+
+	sHashAddress.uiAddr += ui32Offset;
+
+
+	pBuf = (BM_BUF *)HASH_Retrieve(psBMContext->pBufferHash, (IMG_UINTPTR_T) sHashAddress.uiAddr);
+
+	if(pBuf)
+	{
+		IMG_SIZE_T ui32MappingSize = HOST_PAGEALIGN (ui32Size + ui32Offset);
+
+
+		if(pBuf->pMapping->uSize == ui32MappingSize && (pBuf->pMapping->eCpuMemoryOrigin == hm_wrapped ||
+														pBuf->pMapping->eCpuMemoryOrigin == hm_wrapped_virtaddr))
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,
+					"BM_Wrap (Matched previous Wrap! uSize=0x%x, uOffset=0x%x, SysAddr=%08X)",
+					ui32Size, ui32Offset, sHashAddress.uiAddr));
+
+			pBuf->ui32RefCount++;
+			*phBuf = (BM_HANDLE)pBuf;
+			if(pui32Flags)
+				*pui32Flags = uFlags;
+
+			return IMG_TRUE;
+		}
+	}
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof (BM_BUF),
+						(IMG_PVOID *)&pBuf, IMG_NULL,
+						"Buffer Manager buffer") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_Wrap: BM_Buf alloc FAILED"));
+		return IMG_FALSE;
+	}
+	OSMemSet(pBuf, 0, sizeof (BM_BUF));
+
+
+	if (WrapMemory (psBMHeap, ui32Size, ui32Offset, bPhysContig, psSysAddr, pvCPUVAddr, uFlags, pBuf) != IMG_TRUE)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_Wrap: WrapMemory FAILED"));
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof (BM_BUF), pBuf, IMG_NULL);
+
+		return IMG_FALSE;
+	}
+
+
+	if(pBuf->pMapping->eCpuMemoryOrigin == hm_wrapped || pBuf->pMapping->eCpuMemoryOrigin == hm_wrapped_virtaddr)
+	{
+
+		PVR_ASSERT(SysSysPAddrToCpuPAddr(sHashAddress).uiAddr == pBuf->CpuPAddr.uiAddr);
+
+		if (!HASH_Insert (psBMContext->pBufferHash, (IMG_UINTPTR_T) sHashAddress.uiAddr, (IMG_UINTPTR_T)pBuf))
+		{
+			FreeBuf (pBuf, uFlags, IMG_TRUE);
+			PVR_DPF((PVR_DBG_ERROR, "BM_Wrap: HASH_Insert FAILED"));
+			return IMG_FALSE;
+		}
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			"BM_Wrap (uSize=0x%x, uFlags=0x%x)=%08X(devVAddr=%08X)",
+			ui32Size, uFlags, pBuf, pBuf->DevVAddr.uiAddr));
+
+
+	pBuf->ui32RefCount = 1;
+	*phBuf = (BM_HANDLE)pBuf;
+	if(pui32Flags)
+	{
+
+		*pui32Flags = (uFlags & ~PVRSRV_HAP_MAPTYPE_MASK) | PVRSRV_HAP_MULTI_PROCESS;
+	}
+
+	return IMG_TRUE;
+}
+
+
+IMG_VOID BM_Export (BM_HANDLE hBuf)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+
+	pBuf->ui32ExportCount++;
+}
+
+IMG_VOID BM_FreeExport (BM_HANDLE hBuf, IMG_UINT32 ui32Flags)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+
+	pBuf->ui32ExportCount--;
+	FreeBuf (pBuf, ui32Flags, IMG_FALSE);
+}
+
+IMG_VOID
+BM_Free (BM_HANDLE hBuf,
+		IMG_UINT32 ui32Flags)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+	SYS_DATA *psSysData;
+	IMG_SYS_PHYADDR sHashAddr;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "BM_Free (h=%08X)", hBuf));
+	PVR_ASSERT (pBuf!=IMG_NULL);
+
+	if (pBuf == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_Free: invalid parameter"));
+		return;
+	}
+
+	SysAcquireData(&psSysData);
+
+	pBuf->ui32RefCount--;
+
+	if(pBuf->ui32RefCount == 0)
+	{
+		if(pBuf->pMapping->eCpuMemoryOrigin == hm_wrapped || pBuf->pMapping->eCpuMemoryOrigin == hm_wrapped_virtaddr)
+		{
+			sHashAddr = SysCpuPAddrToSysPAddr(pBuf->CpuPAddr);
+
+			HASH_Remove (pBuf->pMapping->pBMHeap->pBMContext->pBufferHash,	(IMG_UINTPTR_T)sHashAddr.uiAddr);
+		}
+		FreeBuf (pBuf, ui32Flags, IMG_TRUE);
+	}
+}
+
+
+IMG_CPU_VIRTADDR
+BM_HandleToCpuVaddr (BM_HANDLE hBuf)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+
+	PVR_ASSERT (pBuf != IMG_NULL);
+	if (pBuf == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_HandleToCpuVaddr: invalid parameter"));
+		return IMG_NULL;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+				"BM_HandleToCpuVaddr(h=%08X)=%08X",
+				hBuf, pBuf->CpuVAddr));
+	return pBuf->CpuVAddr;
+}
+
+
+IMG_DEV_VIRTADDR
+BM_HandleToDevVaddr (BM_HANDLE hBuf)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+
+	PVR_ASSERT (pBuf != IMG_NULL);
+	if (pBuf == IMG_NULL)
+	{
+		IMG_DEV_VIRTADDR	DevVAddr = {0};
+		PVR_DPF((PVR_DBG_ERROR, "BM_HandleToDevVaddr: invalid parameter"));
+		return DevVAddr;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "BM_HandleToDevVaddr(h=%08X)=%08X", hBuf, pBuf->DevVAddr));
+	return pBuf->DevVAddr;
+}
+
+
+IMG_SYS_PHYADDR
+BM_HandleToSysPaddr (BM_HANDLE hBuf)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+
+	PVR_ASSERT (pBuf != IMG_NULL);
+
+	if (pBuf == IMG_NULL)
+	{
+		IMG_SYS_PHYADDR	PhysAddr = {0};
+		PVR_DPF((PVR_DBG_ERROR, "BM_HandleToSysPaddr: invalid parameter"));
+		return PhysAddr;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "BM_HandleToSysPaddr(h=%08X)=%08X", hBuf, pBuf->CpuPAddr.uiAddr));
+	return SysCpuPAddrToSysPAddr (pBuf->CpuPAddr);
+}
+
+IMG_HANDLE
+BM_HandleToOSMemHandle(BM_HANDLE hBuf)
+{
+	BM_BUF *pBuf = (BM_BUF *)hBuf;
+
+	PVR_ASSERT (pBuf != IMG_NULL);
+
+	if (pBuf == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_HandleToOSMemHandle: invalid parameter"));
+		return IMG_NULL;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+				"BM_HandleToOSMemHandle(h=%08X)=%08X",
+				hBuf, pBuf->hOSMemHandle));
+	return pBuf->hOSMemHandle;
+}
+
+IMG_BOOL
+BM_ContiguousStatistics (IMG_UINT32 uFlags,
+						 IMG_UINT32 *pTotalBytes,
+						 IMG_UINT32 *pAvailableBytes)
+{
+	if (pAvailableBytes || pTotalBytes || uFlags);
+	return IMG_FALSE;
+}
+
+
+static IMG_BOOL
+DevMemoryAlloc (BM_CONTEXT *pBMContext,
+				BM_MAPPING *pMapping,
+				IMG_SIZE_T *pActualSize,
+				IMG_UINT32 uFlags,
+				IMG_UINT32 dev_vaddr_alignment,
+				IMG_DEV_VIRTADDR *pDevVAddr)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+#ifdef PDUMP
+	IMG_UINT32 ui32PDumpSize = pMapping->uSize;
+#endif
+
+	psDeviceNode = pBMContext->psDeviceNode;
+
+	if(uFlags & PVRSRV_MEM_INTERLEAVED)
+	{
+
+		pMapping->uSize *= 2;
+	}
+
+#ifdef PDUMP
+	if(uFlags & PVRSRV_MEM_DUMMY)
+	{
+
+		ui32PDumpSize = pMapping->pBMHeap->sDevArena.ui32DataPageSize;
+	}
+#endif
+
+
+	if (!psDeviceNode->pfnMMUAlloc (pMapping->pBMHeap->pMMUHeap,
+									pMapping->uSize,
+									pActualSize,
+									0,
+									dev_vaddr_alignment,
+									&(pMapping->DevVAddr)))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "DevMemoryAlloc ERROR MMU_Alloc"));
+		return IMG_FALSE;
+	}
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+	EnableHostAccess(pBMContext->psMMUContext);
+#endif
+
+
+
+	PDUMPMALLOCPAGES(psDeviceNode->sDevId.eDeviceType, pMapping->DevVAddr.uiAddr, pMapping->CpuVAddr, pMapping->hOSMemHandle, ui32PDumpSize, pMapping->pBMHeap->sDevArena.ui32DataPageSize, (IMG_HANDLE)pMapping);
+
+	switch (pMapping->eCpuMemoryOrigin)
+	{
+		case hm_wrapped:
+		case hm_wrapped_virtaddr:
+		case hm_contiguous:
+		{
+			psDeviceNode->pfnMMUMapPages (	pMapping->pBMHeap->pMMUHeap,
+							pMapping->DevVAddr,
+							SysCpuPAddrToSysPAddr (pMapping->CpuPAddr),
+							pMapping->uSize,
+							uFlags,
+							(IMG_HANDLE)pMapping);
+
+			*pDevVAddr = pMapping->DevVAddr;
+			break;
+		}
+		case hm_env:
+		{
+			psDeviceNode->pfnMMUMapShadow (	pMapping->pBMHeap->pMMUHeap,
+							pMapping->DevVAddr,
+							pMapping->uSize,
+							pMapping->CpuVAddr,
+							pMapping->hOSMemHandle,
+							pDevVAddr,
+							uFlags,
+							(IMG_HANDLE)pMapping);
+			break;
+		}
+		case hm_wrapped_scatter:
+		case hm_wrapped_scatter_virtaddr:
+		{
+			psDeviceNode->pfnMMUMapScatter (pMapping->pBMHeap->pMMUHeap,
+							pMapping->DevVAddr,
+							pMapping->psSysAddr,
+							pMapping->uSize,
+							uFlags,
+							(IMG_HANDLE)pMapping);
+
+			*pDevVAddr = pMapping->DevVAddr;
+			break;
+		}
+		default:
+			PVR_DPF((PVR_DBG_ERROR,
+				"Illegal value %d for pMapping->eCpuMemoryOrigin",
+				pMapping->eCpuMemoryOrigin));
+			return IMG_FALSE;
+	}
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+	DisableHostAccess(pBMContext->psMMUContext);
+#endif
+
+	return IMG_TRUE;
+}
+
+static IMG_VOID
+DevMemoryFree (BM_MAPPING *pMapping)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+#ifdef PDUMP
+	IMG_UINT32 ui32PSize;
+#endif
+
+#ifdef PDUMP
+
+	if(pMapping->ui32Flags & PVRSRV_MEM_DUMMY)
+	{
+
+		ui32PSize = pMapping->pBMHeap->sDevArena.ui32DataPageSize;
+	}
+	else
+	{
+		ui32PSize = pMapping->uSize;
+	}
+
+	PDUMPFREEPAGES(pMapping->pBMHeap,
+                    pMapping->DevVAddr,
+                    ui32PSize,
+                    pMapping->pBMHeap->sDevArena.ui32DataPageSize,
+                    (IMG_HANDLE)pMapping,
+                    (pMapping->ui32Flags & PVRSRV_MEM_INTERLEAVED) ? IMG_TRUE : IMG_FALSE);
+#endif
+
+	psDeviceNode = pMapping->pBMHeap->pBMContext->psDeviceNode;
+
+	psDeviceNode->pfnMMUFree (pMapping->pBMHeap->pMMUHeap, pMapping->DevVAddr, IMG_CAST_TO_DEVVADDR_UINT(pMapping->uSize));
+}
+
+static IMG_BOOL
+BM_ImportMemory (IMG_VOID *pH,
+			  IMG_SIZE_T uRequestSize,
+			  IMG_SIZE_T *pActualSize,
+			  BM_MAPPING **ppsMapping,
+			  IMG_UINT32 uFlags,
+			  IMG_UINTPTR_T *pBase)
+{
+	BM_MAPPING *pMapping;
+	BM_HEAP *pBMHeap = pH;
+	BM_CONTEXT *pBMContext = pBMHeap->pBMContext;
+	IMG_BOOL bResult;
+	IMG_SIZE_T uSize;
+	IMG_SIZE_T uPSize;
+	IMG_UINT32 uDevVAddrAlignment = 0;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "BM_ImportMemory (pBMContext=%08X, uRequestSize=0x%x, uFlags=0x%x, uAlign=0x%x)",
+			  pBMContext, uRequestSize, uFlags, uDevVAddrAlignment));
+
+	PVR_ASSERT (ppsMapping != IMG_NULL);
+	PVR_ASSERT (pBMContext != IMG_NULL);
+
+	if (ppsMapping == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_ImportMemory: invalid parameter"));
+		goto fail_exit;
+	}
+
+	uSize = HOST_PAGEALIGN (uRequestSize);
+	PVR_ASSERT (uSize >= uRequestSize);
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof (BM_MAPPING),
+						(IMG_PVOID *)&pMapping, IMG_NULL,
+						"Buffer Manager Mapping") != PVRSRV_OK)
+	{
+		PVR_DPF ((PVR_DBG_ERROR, "BM_ImportMemory: failed BM_MAPPING alloc"));
+		goto fail_exit;
+	}
+
+	pMapping->hOSMemHandle = 0;
+	pMapping->CpuVAddr = 0;
+	pMapping->DevVAddr.uiAddr = 0;
+	pMapping->CpuPAddr.uiAddr = 0;
+	pMapping->uSize = uSize;
+	pMapping->pBMHeap = pBMHeap;
+	pMapping->ui32Flags = uFlags;
+
+
+	if (pActualSize)
+	{
+		*pActualSize = uSize;
+	}
+
+
+	if(pMapping->ui32Flags & PVRSRV_MEM_DUMMY)
+	{
+		uPSize = pBMHeap->sDevArena.ui32DataPageSize;
+	}
+	else
+	{
+		uPSize = pMapping->uSize;
+	}
+
+
+
+	if(pBMHeap->ui32Attribs & PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG)
+	{
+		IMG_UINT32 ui32Attribs = pBMHeap->ui32Attribs;
+
+
+		if (pMapping->ui32Flags & PVRSRV_HAP_CACHETYPE_MASK)
+		{
+			ui32Attribs &= ~PVRSRV_HAP_CACHETYPE_MASK;
+			ui32Attribs |= (pMapping->ui32Flags & PVRSRV_HAP_CACHETYPE_MASK);
+		}
+
+
+		if (OSAllocPages(ui32Attribs,
+						 uPSize,
+						 pBMHeap->sDevArena.ui32DataPageSize,
+						 (IMG_VOID **)&pMapping->CpuVAddr,
+						 &pMapping->hOSMemHandle) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,
+					"BM_ImportMemory: OSAllocPages(0x%x) failed",
+					uPSize));
+			goto fail_mapping_alloc;
+		}
+
+
+		pMapping->eCpuMemoryOrigin = hm_env;
+	}
+	else if(pBMHeap->ui32Attribs & PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG)
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+		IMG_UINT32 ui32Attribs = pBMHeap->ui32Attribs;
+
+
+		if (pMapping->ui32Flags & PVRSRV_HAP_CACHETYPE_MASK)
+		{
+			ui32Attribs &= ~PVRSRV_HAP_CACHETYPE_MASK;
+			ui32Attribs |= (pMapping->ui32Flags & PVRSRV_HAP_CACHETYPE_MASK);
+		}
+
+
+		PVR_ASSERT(pBMHeap->pLocalDevMemArena != IMG_NULL);
+
+		if (!RA_Alloc (pBMHeap->pLocalDevMemArena,
+					   uPSize,
+					   IMG_NULL,
+					   IMG_NULL,
+					   0,
+					   pBMHeap->sDevArena.ui32DataPageSize,
+					   0,
+					   (IMG_UINTPTR_T *)&sSysPAddr.uiAddr))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "BM_ImportMemory: RA_Alloc(0x%x) FAILED", uPSize));
+			goto fail_mapping_alloc;
+		}
+
+
+		pMapping->CpuPAddr = SysSysPAddrToCpuPAddr(sSysPAddr);
+		if(OSReservePhys(pMapping->CpuPAddr,
+						 uPSize,
+						 ui32Attribs,
+						 &pMapping->CpuVAddr,
+						 &pMapping->hOSMemHandle) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,	"BM_ImportMemory: OSReservePhys failed"));
+			goto fail_dev_mem_alloc;
+		}
+
+
+		pMapping->eCpuMemoryOrigin = hm_contiguous;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR,	"BM_ImportMemory: Invalid backing store type"));
+		goto fail_mapping_alloc;
+	}
+
+
+	bResult = DevMemoryAlloc (pBMContext,
+								pMapping,
+								IMG_NULL,
+								uFlags,
+								uDevVAddrAlignment,
+								&pMapping->DevVAddr);
+	if (!bResult)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+				"BM_ImportMemory: DevMemoryAlloc(0x%x) failed",
+				pMapping->uSize));
+		goto fail_dev_mem_alloc;
+	}
+
+
+
+	PVR_ASSERT (uDevVAddrAlignment>1?(pMapping->DevVAddr.uiAddr%uDevVAddrAlignment)==0:1);
+
+	*pBase = pMapping->DevVAddr.uiAddr;
+	*ppsMapping = pMapping;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "BM_ImportMemory: IMG_TRUE"));
+	return IMG_TRUE;
+
+fail_dev_mem_alloc:
+	if (pMapping && (pMapping->CpuVAddr || pMapping->hOSMemHandle))
+	{
+
+		if(pMapping->ui32Flags & PVRSRV_MEM_INTERLEAVED)
+		{
+			pMapping->uSize /= 2;
+		}
+
+		if(pMapping->ui32Flags & PVRSRV_MEM_DUMMY)
+		{
+			uPSize = pBMHeap->sDevArena.ui32DataPageSize;
+		}
+		else
+		{
+			uPSize = pMapping->uSize;
+		}
+
+		if(pBMHeap->ui32Attribs & PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG)
+		{
+			OSFreePages(pBMHeap->ui32Attribs,
+						  uPSize,
+						  (IMG_VOID *)pMapping->CpuVAddr,
+						  pMapping->hOSMemHandle);
+		}
+		else
+		{
+			IMG_SYS_PHYADDR sSysPAddr;
+
+			if(pMapping->CpuVAddr)
+			{
+				OSUnReservePhys(pMapping->CpuVAddr,
+								uPSize,
+								pBMHeap->ui32Attribs,
+								pMapping->hOSMemHandle);
+			}
+			sSysPAddr = SysCpuPAddrToSysPAddr(pMapping->CpuPAddr);
+			RA_Free (pBMHeap->pLocalDevMemArena, sSysPAddr.uiAddr, IMG_FALSE);
+		}
+	}
+fail_mapping_alloc:
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_MAPPING), pMapping, IMG_NULL);
+
+fail_exit:
+	return IMG_FALSE;
+}
+
+
+static IMG_VOID
+BM_FreeMemory (IMG_VOID *h, IMG_UINTPTR_T _base, BM_MAPPING *psMapping)
+{
+	BM_HEAP *pBMHeap = h;
+	IMG_SIZE_T uPSize;
+
+	PVR_UNREFERENCED_PARAMETER (_base);
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "BM_FreeMemory (h=%08X, base=0x%x, psMapping=0x%x)", h, _base, psMapping));
+
+	PVR_ASSERT (psMapping != IMG_NULL);
+
+	if (psMapping == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "BM_FreeMemory: invalid parameter"));
+		return;
+	}
+
+	DevMemoryFree (psMapping);
+
+
+	if((psMapping->ui32Flags & PVRSRV_MEM_INTERLEAVED) != 0)
+	{
+		psMapping->uSize /= 2;
+	}
+
+	if(psMapping->ui32Flags & PVRSRV_MEM_DUMMY)
+	{
+		uPSize = psMapping->pBMHeap->sDevArena.ui32DataPageSize;
+	}
+	else
+	{
+		uPSize = psMapping->uSize;
+	}
+
+	if(pBMHeap->ui32Attribs & PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG)
+	{
+		OSFreePages(pBMHeap->ui32Attribs,
+						uPSize,
+						(IMG_VOID *) psMapping->CpuVAddr,
+						psMapping->hOSMemHandle);
+	}
+	else if(pBMHeap->ui32Attribs & PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG)
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+
+		OSUnReservePhys(psMapping->CpuVAddr, uPSize, pBMHeap->ui32Attribs, psMapping->hOSMemHandle);
+
+		sSysPAddr = SysCpuPAddrToSysPAddr(psMapping->CpuPAddr);
+
+		RA_Free (pBMHeap->pLocalDevMemArena, sSysPAddr.uiAddr, IMG_FALSE);
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR,	"BM_FreeMemory: Invalid backing store type"));
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BM_MAPPING), psMapping, IMG_NULL);
+
+
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"..BM_FreeMemory (h=%08X, base=0x%x, psMapping=0x%x)",
+			h, _base, psMapping));
+}
+
+IMG_VOID BM_GetPhysPageAddr(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+								IMG_DEV_VIRTADDR sDevVPageAddr,
+								IMG_DEV_PHYADDR *psDevPAddr)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "BM_GetPhysPageAddr"));
+
+	PVR_ASSERT (psMemInfo && psDevPAddr)
+
+
+	PVR_ASSERT((sDevVPageAddr.uiAddr & 0xFFF) == 0);
+
+	psDeviceNode = ((BM_BUF*)psMemInfo->sMemBlk.hBuffer)->pMapping->pBMHeap->pBMContext->psDeviceNode;
+
+	*psDevPAddr = psDeviceNode->pfnMMUGetPhysPageAddr(((BM_BUF*)psMemInfo->sMemBlk.hBuffer)->pMapping->pBMHeap->pMMUHeap,
+												sDevVPageAddr);
+}
+
+
+PVRSRV_ERROR BM_GetHeapInfo(IMG_HANDLE hDevMemHeap, PVRSRV_HEAP_INFO *psHeapInfo)
+{
+	BM_HEAP *psBMHeap = (BM_HEAP *)hDevMemHeap;
+
+	PVR_DPF((PVR_DBG_VERBOSE, "BM_GetHeapInfo"));
+
+	psHeapInfo->hDevMemHeap = hDevMemHeap;
+	psHeapInfo->sDevVAddrBase = psBMHeap->sDevArena.BaseDevVAddr;
+	psHeapInfo->ui32HeapByteSize = psBMHeap->sDevArena.ui32Size;
+	psHeapInfo->ui32Attribs = psBMHeap->ui32Attribs;
+
+	return PVRSRV_OK;
+}
+
+
+MMU_CONTEXT* BM_GetMMUContext(IMG_HANDLE hDevMemHeap)
+{
+	BM_HEAP *pBMHeap = (BM_HEAP*)hDevMemHeap;
+
+	PVR_DPF((PVR_DBG_VERBOSE, "BM_GetMMUContext"));
+
+	return pBMHeap->pBMContext->psMMUContext;
+}
+
+MMU_CONTEXT* BM_GetMMUContextFromMemContext(IMG_HANDLE hDevMemContext)
+{
+	BM_CONTEXT *pBMContext = (BM_CONTEXT*)hDevMemContext;
+
+	PVR_DPF ((PVR_DBG_VERBOSE, "BM_GetMMUContextFromMemContext"));
+
+	return pBMContext->psMMUContext;
+}
+
+IMG_HANDLE BM_GetMMUHeap(IMG_HANDLE hDevMemHeap)
+{
+	PVR_DPF((PVR_DBG_VERBOSE, "BM_GetMMUHeap"));
+
+	return (IMG_HANDLE)((BM_HEAP*)hDevMemHeap)->pMMUHeap;
+}
+
+
+PVRSRV_DEVICE_NODE* BM_GetDeviceNode(IMG_HANDLE hDevMemContext)
+{
+	PVR_DPF((PVR_DBG_VERBOSE, "BM_GetDeviceNode"));
+
+	return ((BM_CONTEXT*)hDevMemContext)->psDeviceNode;
+}
+
+
+IMG_HANDLE BM_GetMappingHandle(PVRSRV_KERNEL_MEM_INFO *psMemInfo)
+{
+	PVR_DPF((PVR_DBG_VERBOSE, "BM_GetMappingHandle"));
+
+	return ((BM_BUF*)psMemInfo->sMemBlk.hBuffer)->pMapping->hOSMemHandle;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/deviceclass.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/deviceclass.c
new file mode 100644
index 0000000..2867a08
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/deviceclass.c
@@ -0,0 +1,2060 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "kernelbuffer.h"
+#include "pvr_bridge_km.h"
+
+#include "lists.h"
+#include "emgd_drm.h"
+
+DECLARE_LIST_ANY_VA(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_FOR_EACH_VA(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_INSERT(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_REMOVE(PVRSRV_DEVICE_NODE);
+
+IMG_VOID* MatchDeviceKM_AnyVaCb(PVRSRV_DEVICE_NODE* psDeviceNode, va_list va);
+
+PVRSRV_ERROR AllocateDeviceID(SYS_DATA *psSysData, IMG_UINT32 *pui32DevID);
+PVRSRV_ERROR FreeDeviceID(SYS_DATA *psSysData, IMG_UINT32 ui32DevID);
+
+#if defined(SUPPORT_MISR_IN_THREAD)
+void OSVSyncMISR(IMG_HANDLE, IMG_BOOL);
+#endif
+
+#if defined(SUPPORT_CUSTOM_SWAP_OPERATIONS)
+IMG_VOID PVRSRVFreeCommandCompletePacketKM(IMG_HANDLE	hCmdCookie,
+										   IMG_BOOL		bScheduleMISR);
+#endif
+typedef struct PVRSRV_DC_SRV2DISP_KMJTABLE_TAG *PPVRSRV_DC_SRV2DISP_KMJTABLE;
+
+typedef struct PVRSRV_DC_BUFFER_TAG
+{
+
+	PVRSRV_DEVICECLASS_BUFFER sDeviceClassBuffer;
+
+	struct PVRSRV_DISPLAYCLASS_INFO_TAG *psDCInfo;
+	struct PVRSRV_DC_SWAPCHAIN_TAG *psSwapChain;
+} PVRSRV_DC_BUFFER;
+
+typedef struct PVRSRV_DC_SWAPCHAIN_TAG
+{
+	IMG_HANDLE							hExtSwapChain;
+	IMG_UINT32							ui32SwapChainID;
+	IMG_UINT32							ui32Flags;
+	IMG_UINT32							ui32RefCount;
+	PVRSRV_QUEUE_INFO					*psQueue;
+	PVRSRV_DC_BUFFER					asBuffer[PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS];
+	IMG_UINT32							ui32BufferCount;
+	PVRSRV_DC_BUFFER					*psLastFlipBuffer;
+	struct PVRSRV_DC_SWAPCHAIN_TAG		*psNext;
+	struct PVRSRV_DISPLAYCLASS_INFO_TAG *psDCInfo;
+} PVRSRV_DC_SWAPCHAIN;
+
+typedef struct PVRSRV_DC_SWAPCHAIN_REF_TAG
+{
+	struct PVRSRV_DC_SWAPCHAIN_TAG		*psSwapChain;
+	IMG_HANDLE							hResItem;
+} PVRSRV_DC_SWAPCHAIN_REF;
+
+
+typedef struct PVRSRV_DISPLAYCLASS_INFO_TAG
+{
+	IMG_UINT32 							ui32RefCount;
+	IMG_UINT32							ui32DeviceID;
+	IMG_HANDLE							hExtDevice;
+	PPVRSRV_DC_SRV2DISP_KMJTABLE		psFuncTable;
+	IMG_HANDLE							hDevMemContext;
+	PVRSRV_DC_BUFFER 					sSystemBuffer;
+	struct PVRSRV_DC_SWAPCHAIN_TAG		*psDCSwapChainShared;
+} PVRSRV_DISPLAYCLASS_INFO;
+
+
+typedef struct PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO_TAG
+{
+	PVRSRV_DISPLAYCLASS_INFO			*psDCInfo;
+	PRESMAN_ITEM						hResItem;
+} PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO;
+
+
+typedef struct PVRSRV_BC_SRV2BUFFER_KMJTABLE_TAG *PPVRSRV_BC_SRV2BUFFER_KMJTABLE;
+
+typedef struct PVRSRV_BC_BUFFER_TAG
+{
+
+	PVRSRV_DEVICECLASS_BUFFER sDeviceClassBuffer;
+
+	struct PVRSRV_BUFFERCLASS_INFO_TAG *psBCInfo;
+} PVRSRV_BC_BUFFER;
+
+
+typedef struct PVRSRV_BUFFERCLASS_INFO_TAG
+{
+	IMG_UINT32 							ui32RefCount;
+	IMG_UINT32							ui32DeviceID;
+	IMG_HANDLE							hExtDevice;
+	PPVRSRV_BC_SRV2BUFFER_KMJTABLE		psFuncTable;
+	IMG_HANDLE							hDevMemContext;
+
+	IMG_UINT32							ui32BufferCount;
+	PVRSRV_BC_BUFFER 					*psBuffer;
+
+} PVRSRV_BUFFERCLASS_INFO;
+
+
+typedef struct PVRSRV_BUFFERCLASS_PERCONTEXT_INFO_TAG
+{
+	PVRSRV_BUFFERCLASS_INFO				*psBCInfo;
+	IMG_HANDLE							hResItem;
+} PVRSRV_BUFFERCLASS_PERCONTEXT_INFO;
+
+
+static PVRSRV_DISPLAYCLASS_INFO* DCDeviceHandleToDCInfo (IMG_HANDLE hDeviceKM)
+{
+	PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *psDCPerContextInfo;
+
+	psDCPerContextInfo = (PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *)hDeviceKM;
+
+	return psDCPerContextInfo->psDCInfo;
+}
+
+
+static PVRSRV_BUFFERCLASS_INFO* BCDeviceHandleToBCInfo (IMG_HANDLE hDeviceKM)
+{
+	PVRSRV_BUFFERCLASS_PERCONTEXT_INFO *psBCPerContextInfo;
+
+	psBCPerContextInfo = (PVRSRV_BUFFERCLASS_PERCONTEXT_INFO *)hDeviceKM;
+
+	return psBCPerContextInfo->psBCInfo;
+}
+
+IMG_VOID PVRSRVEnumerateDCKM_ForEachVaCb(PVRSRV_DEVICE_NODE *psDeviceNode, va_list va)
+{
+	IMG_UINT *pui32DevCount;
+	IMG_UINT32 **ppui32DevID;
+	PVRSRV_DEVICE_CLASS peDeviceClass;
+
+	pui32DevCount = va_arg(va, IMG_UINT*);
+	ppui32DevID = va_arg(va, IMG_UINT32**);
+	peDeviceClass = va_arg(va, PVRSRV_DEVICE_CLASS);
+
+	if	((psDeviceNode->sDevId.eDeviceClass == peDeviceClass)
+	&&	(psDeviceNode->sDevId.eDeviceType == PVRSRV_DEVICE_TYPE_EXT))
+	{
+		(*pui32DevCount)++;
+		if(*ppui32DevID)
+		{
+			*(*ppui32DevID)++ = psDeviceNode->sDevId.ui32DeviceIndex;
+		}
+	}
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVEnumerateDCKM (PVRSRV_DEVICE_CLASS DeviceClass,
+								  IMG_UINT32 *pui32DevCount,
+								  IMG_UINT32 *pui32DevID )
+{
+
+	IMG_UINT			ui32DevCount = 0;
+	SYS_DATA 			*psSysData;
+	IMG_UINT32			pui32Temp[PVRSRV_MAX_DEVICES];
+	int					i;
+
+	SysAcquireData(&psSysData);
+
+
+	List_PVRSRV_DEVICE_NODE_ForEach_va(psSysData->psDeviceNodeList,
+										PVRSRVEnumerateDCKM_ForEachVaCb,
+										&ui32DevCount,
+										&pui32DevID,
+										DeviceClass);
+
+	if(pui32DevCount)
+	{
+		*pui32DevCount = ui32DevCount;
+	}
+	else if(pui32DevID == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVEnumerateDCKM: Invalid parameters"));
+		return (PVRSRV_ERROR_INVALID_PARAMS);
+	}
+
+
+
+	/* Note: The above macro returns the device IDs in the opposite order that
+	 * they were registered in, which messes up EMGD's DIH (dual independant
+	 * head) code.  To fix that, we need to reverse the order of the device
+	 * IDs:
+	 */
+	/* 1st) make a temporary copy of the array, in the correct order: */
+	for (i=0; i<ui32DevCount; i++) {
+		pui32Temp[i] = *(--pui32DevID);
+	}
+	/* 2nd) make a final version of the array, in the correct order: */
+	for (i=0; i<ui32DevCount; i++) {
+		pui32DevID[i] = pui32Temp[i];
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PVRSRVRegisterDCDeviceKM (PVRSRV_DC_SRV2DISP_KMJTABLE *psFuncTable,
+									   IMG_UINT32 *pui32DeviceID)
+{
+	PVRSRV_DISPLAYCLASS_INFO 	*psDCInfo = IMG_NULL;
+	PVRSRV_DEVICE_NODE			*psDeviceNode;
+	SYS_DATA					*psSysData;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+	SysAcquireData(&psSysData);
+
+
+
+
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(*psDCInfo),
+					 (IMG_VOID **)&psDCInfo, IMG_NULL,
+					 "Display Class Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterDCDeviceKM: Failed psDCInfo alloc"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OSMemSet (psDCInfo, 0, sizeof(*psDCInfo));
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_DC_SRV2DISP_KMJTABLE),
+					 (IMG_VOID **)&psDCInfo->psFuncTable, IMG_NULL,
+					 "Function table for SRVKM->DISPLAY") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterDCDeviceKM: Failed psFuncTable alloc"));
+		goto ErrorExit;
+	}
+	OSMemSet (psDCInfo->psFuncTable, 0, sizeof(PVRSRV_DC_SRV2DISP_KMJTABLE));
+
+
+	*psDCInfo->psFuncTable = *psFuncTable;
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_DEVICE_NODE),
+					 (IMG_VOID **)&psDeviceNode, IMG_NULL,
+					 "Device Node") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterDCDeviceKM: Failed psDeviceNode alloc"));
+		goto ErrorExit;
+	}
+	OSMemSet (psDeviceNode, 0, sizeof(PVRSRV_DEVICE_NODE));
+
+	psDeviceNode->pvDevice = (IMG_VOID*)psDCInfo;
+	psDeviceNode->ui32pvDeviceSize = sizeof(*psDCInfo);
+	psDeviceNode->ui32RefCount = 1;
+	psDeviceNode->sDevId.eDeviceType = PVRSRV_DEVICE_TYPE_EXT;
+	psDeviceNode->sDevId.eDeviceClass = PVRSRV_DEVICE_CLASS_DISPLAY;
+	psDeviceNode->psSysData = psSysData;
+
+
+	if (AllocateDeviceID(psSysData, &psDeviceNode->sDevId.ui32DeviceIndex) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterBCDeviceKM: Failed to allocate Device ID"));
+		goto ErrorExit;
+	}
+	psDCInfo->ui32DeviceID = psDeviceNode->sDevId.ui32DeviceIndex;
+	if (pui32DeviceID)
+	{
+		*pui32DeviceID = psDeviceNode->sDevId.ui32DeviceIndex;
+	}
+
+
+	SysRegisterExternalDevice(psDeviceNode);
+
+
+	List_PVRSRV_DEVICE_NODE_Insert(&psSysData->psDeviceNodeList, psDeviceNode);
+
+	return PVRSRV_OK;
+
+ErrorExit:
+
+	if(psDCInfo->psFuncTable)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DC_SRV2DISP_KMJTABLE), psDCInfo->psFuncTable, IMG_NULL);
+		psDCInfo->psFuncTable = IMG_NULL;
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DISPLAYCLASS_INFO), psDCInfo, IMG_NULL);
+
+
+	return PVRSRV_ERROR_OUT_OF_MEMORY;
+}
+
+PVRSRV_ERROR PVRSRVRemoveDCDeviceKM(IMG_UINT32 ui32DevIndex)
+{
+	SYS_DATA					*psSysData;
+	PVRSRV_DEVICE_NODE			*psDeviceNode;
+	PVRSRV_DISPLAYCLASS_INFO	*psDCInfo;
+
+	SysAcquireData(&psSysData);
+
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)
+		List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+									   MatchDeviceKM_AnyVaCb,
+									   ui32DevIndex,
+									   IMG_FALSE,
+									   PVRSRV_DEVICE_CLASS_DISPLAY);
+	if (!psDeviceNode)
+	{
+
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRemoveDCDeviceKM: requested device %d not present", ui32DevIndex));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	psDCInfo = (PVRSRV_DISPLAYCLASS_INFO*)psDeviceNode->pvDevice;
+
+
+
+
+	if(psDCInfo->ui32RefCount == 0)
+	{
+
+
+		List_PVRSRV_DEVICE_NODE_Remove(psDeviceNode);
+
+
+		SysRemoveExternalDevice(psDeviceNode);
+
+
+
+
+		PVR_ASSERT(psDCInfo->ui32RefCount == 0);
+		(IMG_VOID)FreeDeviceID(psSysData, ui32DevIndex);
+		(IMG_VOID)OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DC_SRV2DISP_KMJTABLE), psDCInfo->psFuncTable, IMG_NULL);
+		psDCInfo->psFuncTable = IMG_NULL;
+		(IMG_VOID)OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DISPLAYCLASS_INFO), psDCInfo, IMG_NULL);
+
+		(IMG_VOID)OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DEVICE_NODE), psDeviceNode, IMG_NULL);
+
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRemoveDCDeviceKM: failed as %d Services DC API connections are still open", psDCInfo->ui32RefCount));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PVRSRVRegisterBCDeviceKM (PVRSRV_BC_SRV2BUFFER_KMJTABLE *psFuncTable,
+									   IMG_UINT32	*pui32DeviceID)
+{
+	PVRSRV_BUFFERCLASS_INFO	*psBCInfo = IMG_NULL;
+	PVRSRV_DEVICE_NODE		*psDeviceNode;
+	SYS_DATA				*psSysData;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+	SysAcquireData(&psSysData);
+
+
+
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(*psBCInfo),
+					 (IMG_VOID **)&psBCInfo, IMG_NULL,
+					 "Buffer Class Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterBCDeviceKM: Failed psBCInfo alloc"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OSMemSet (psBCInfo, 0, sizeof(*psBCInfo));
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_BC_SRV2BUFFER_KMJTABLE),
+					 (IMG_VOID **)&psBCInfo->psFuncTable, IMG_NULL,
+					 "Function table for SRVKM->BUFFER") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterBCDeviceKM: Failed psFuncTable alloc"));
+		goto ErrorExit;
+	}
+	OSMemSet (psBCInfo->psFuncTable, 0, sizeof(PVRSRV_BC_SRV2BUFFER_KMJTABLE));
+
+
+	*psBCInfo->psFuncTable = *psFuncTable;
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_DEVICE_NODE),
+					 (IMG_VOID **)&psDeviceNode, IMG_NULL,
+					 "Device Node") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterBCDeviceKM: Failed psDeviceNode alloc"));
+		goto ErrorExit;
+	}
+	OSMemSet (psDeviceNode, 0, sizeof(PVRSRV_DEVICE_NODE));
+
+	psDeviceNode->pvDevice = (IMG_VOID*)psBCInfo;
+	psDeviceNode->ui32pvDeviceSize = sizeof(*psBCInfo);
+	psDeviceNode->ui32RefCount = 1;
+	psDeviceNode->sDevId.eDeviceType = PVRSRV_DEVICE_TYPE_EXT;
+	psDeviceNode->sDevId.eDeviceClass = PVRSRV_DEVICE_CLASS_BUFFER;
+	psDeviceNode->psSysData = psSysData;
+
+
+	if (AllocateDeviceID(psSysData, &psDeviceNode->sDevId.ui32DeviceIndex) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterBCDeviceKM: Failed to allocate Device ID"));
+		goto ErrorExit;
+	}
+	psBCInfo->ui32DeviceID = psDeviceNode->sDevId.ui32DeviceIndex;
+	if (pui32DeviceID)
+	{
+		*pui32DeviceID = psDeviceNode->sDevId.ui32DeviceIndex;
+	}
+
+
+	List_PVRSRV_DEVICE_NODE_Insert(&psSysData->psDeviceNodeList, psDeviceNode);
+
+	return PVRSRV_OK;
+
+ErrorExit:
+
+	if(psBCInfo->psFuncTable)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PPVRSRV_BC_SRV2BUFFER_KMJTABLE), psBCInfo->psFuncTable, IMG_NULL);
+		psBCInfo->psFuncTable = IMG_NULL;
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_BUFFERCLASS_INFO), psBCInfo, IMG_NULL);
+
+
+	return PVRSRV_ERROR_OUT_OF_MEMORY;
+}
+
+
+PVRSRV_ERROR PVRSRVRemoveBCDeviceKM(IMG_UINT32 ui32DevIndex)
+{
+	SYS_DATA					*psSysData;
+	PVRSRV_DEVICE_NODE			*psDevNode;
+	PVRSRV_BUFFERCLASS_INFO		*psBCInfo;
+
+	SysAcquireData(&psSysData);
+
+
+	psDevNode = (PVRSRV_DEVICE_NODE*)
+		List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+									   MatchDeviceKM_AnyVaCb,
+									   ui32DevIndex,
+									   IMG_FALSE,
+									   PVRSRV_DEVICE_CLASS_BUFFER);
+
+	if (!psDevNode)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRemoveBCDeviceKM: requested device %d not present", ui32DevIndex));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+
+	psBCInfo = (PVRSRV_BUFFERCLASS_INFO*)psDevNode->pvDevice;
+
+
+
+
+	if(psBCInfo->ui32RefCount == 0)
+	{
+
+
+		List_PVRSRV_DEVICE_NODE_Remove(psDevNode);
+
+
+
+
+		(IMG_VOID)FreeDeviceID(psSysData, ui32DevIndex);
+
+
+		(IMG_VOID)OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_BC_SRV2BUFFER_KMJTABLE), psBCInfo->psFuncTable, IMG_NULL);
+		psBCInfo->psFuncTable = IMG_NULL;
+		(IMG_VOID)OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_BUFFERCLASS_INFO), psBCInfo, IMG_NULL);
+
+		(IMG_VOID)OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DEVICE_NODE), psDevNode, IMG_NULL);
+
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRemoveBCDeviceKM: failed as %d Services BC API connections are still open", psBCInfo->ui32RefCount));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVCloseDCDeviceKM (IMG_HANDLE	hDeviceKM,
+									IMG_BOOL	bResManCallback)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *psDCPerContextInfo;
+
+	PVR_UNREFERENCED_PARAMETER(bResManCallback);
+
+	psDCPerContextInfo = (PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *)hDeviceKM;
+
+
+	eError = ResManFreeResByPtr(psDCPerContextInfo->hResItem);
+
+	return eError;
+}
+
+
+static PVRSRV_ERROR CloseDCDeviceCallBack(IMG_PVOID		pvParam,
+										  IMG_UINT32	ui32Param)
+{
+	PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *psDCPerContextInfo;
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	psDCPerContextInfo = (PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *)pvParam;
+	psDCInfo = psDCPerContextInfo->psDCInfo;
+
+	psDCInfo->ui32RefCount--;
+	if(psDCInfo->ui32RefCount == 0)
+	{
+
+		psDCInfo->psFuncTable->pfnCloseDCDevice(psDCInfo->hExtDevice);
+
+		if (--psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount == 0)
+		{
+			PVRSRVFreeSyncInfoKM(psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo);
+		}
+
+		psDCInfo->hDevMemContext = IMG_NULL;
+		psDCInfo->hExtDevice = IMG_NULL;
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO), psDCPerContextInfo, IMG_NULL);
+
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVOpenDCDeviceKM (PVRSRV_PER_PROCESS_DATA	*psPerProc,
+								   IMG_UINT32				ui32DeviceID,
+								   IMG_HANDLE				hDevCookie,
+								   IMG_HANDLE				*phDeviceKM)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DISPLAYCLASS_PERCONTEXT_INFO *psDCPerContextInfo;
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+	SYS_DATA			*psSysData;
+	PVRSRV_ERROR eError;
+
+	if(!phDeviceKM || !hDevCookie)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenDCDeviceKM: Invalid params"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	SysAcquireData(&psSysData);
+
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)
+			List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+										   MatchDeviceKM_AnyVaCb,
+										   ui32DeviceID,
+										   IMG_FALSE,
+										   PVRSRV_DEVICE_CLASS_DISPLAY);
+	if (!psDeviceNode)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenDCDeviceKM: no devnode matching index %d", ui32DeviceID));
+		return PVRSRV_ERROR_GENERIC;
+	}
+	psDCInfo = (PVRSRV_DISPLAYCLASS_INFO*)psDeviceNode->pvDevice;
+
+
+
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(*psDCPerContextInfo),
+				  (IMG_VOID **)&psDCPerContextInfo, IMG_NULL,
+				  "Display Class per Context Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenDCDeviceKM: Failed psDCPerContextInfo alloc"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OSMemSet(psDCPerContextInfo, 0, sizeof(*psDCPerContextInfo));
+
+	if(psDCInfo->ui32RefCount++ == 0)
+	{
+
+		psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevCookie;
+
+
+		psDCInfo->hDevMemContext = (IMG_HANDLE)psDeviceNode->sDevMemoryInfo.pBMKernelContext;
+
+
+		eError = PVRSRVAllocSyncInfoKM(IMG_NULL,
+									(IMG_HANDLE)psDeviceNode->sDevMemoryInfo.pBMKernelContext,
+									&psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenDCDeviceKM: Failed sync info alloc"));
+			psDCInfo->ui32RefCount--;
+			return eError;
+		}
+
+
+		eError = psDCInfo->psFuncTable->pfnOpenDCDevice(ui32DeviceID,
+	&psDCInfo->hExtDevice,
+								(PVRSRV_SYNC_DATA*)psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo->psSyncDataMemInfoKM->pvLinAddrKM);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenDCDeviceKM: Failed to open external DC device"));
+			psDCInfo->ui32RefCount--;
+			PVRSRVFreeSyncInfoKM(psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo);
+			return eError;
+		}
+
+		psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount++;
+	}
+
+	psDCPerContextInfo->psDCInfo = psDCInfo;
+	psDCPerContextInfo->hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													 RESMAN_TYPE_DISPLAYCLASS_DEVICE,
+													 psDCPerContextInfo,
+													 0,
+													 CloseDCDeviceCallBack);
+
+
+	*phDeviceKM = (IMG_HANDLE)psDCPerContextInfo;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVEnumDCFormatsKM (IMG_HANDLE hDeviceKM,
+									IMG_UINT32 *pui32Count,
+									DISPLAY_FORMAT *psFormat)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+
+	/* Note: It is acceptable for psFormat to be NULL.  In fact, this is the
+	 * desired way in which to find out the number of pixel formats, so that
+	 * memory can be allocated before a second call to this function, in order
+	 * to get the pixel formats.
+	 */
+	/*	if(!hDeviceKM || !pui32Count || !psFormat)*/
+	if(!hDeviceKM || !pui32Count)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVEnumDCFormatsKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+
+
+	return psDCInfo->psFuncTable->pfnEnumDCFormats(psDCInfo->hExtDevice, pui32Count, psFormat);
+}
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVEnumDCDimsKM (IMG_HANDLE hDeviceKM,
+								 DISPLAY_FORMAT *psFormat,
+								 IMG_UINT32 *pui32Count,
+								 DISPLAY_DIMS *psDim)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+
+	if(!hDeviceKM || !pui32Count || !psFormat)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVEnumDCDimsKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+
+
+	return psDCInfo->psFuncTable->pfnEnumDCDims(psDCInfo->hExtDevice, psFormat, pui32Count, psDim);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetDCSystemBufferKM (IMG_HANDLE hDeviceKM,
+										IMG_HANDLE *phBuffer)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	IMG_HANDLE hExtBuffer;
+
+	if(!hDeviceKM || !phBuffer)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetDCSystemBufferKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+
+
+	eError = psDCInfo->psFuncTable->pfnGetDCSystemBuffer(psDCInfo->hExtDevice, &hExtBuffer);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetDCSystemBufferKM: Failed to get valid buffer handle from external driver"));
+		return eError;
+	}
+
+
+	psDCInfo->sSystemBuffer.sDeviceClassBuffer.pfnGetBufferAddr = psDCInfo->psFuncTable->pfnGetBufferAddr;
+	psDCInfo->sSystemBuffer.sDeviceClassBuffer.hDevMemContext = psDCInfo->hDevMemContext;
+	psDCInfo->sSystemBuffer.sDeviceClassBuffer.hExtDevice = psDCInfo->hExtDevice;
+	psDCInfo->sSystemBuffer.sDeviceClassBuffer.hExtBuffer = hExtBuffer;
+
+	psDCInfo->sSystemBuffer.psDCInfo = psDCInfo;
+
+
+	*phBuffer = (IMG_HANDLE)&(psDCInfo->sSystemBuffer);
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetDCInfoKM (IMG_HANDLE hDeviceKM,
+								DISPLAY_INFO *psDisplayInfo)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_ERROR eError;
+
+	if(!hDeviceKM || !psDisplayInfo)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetDCInfoKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+
+
+	eError = psDCInfo->psFuncTable->pfnGetDCInfo(psDCInfo->hExtDevice, psDisplayInfo);
+	if (eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	if (psDisplayInfo->ui32MaxSwapChainBuffers > PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS)
+	{
+		psDisplayInfo->ui32MaxSwapChainBuffers = PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVDestroyDCSwapChainKM(IMG_HANDLE hSwapChainRef)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_DC_SWAPCHAIN_REF *psSwapChainRef;
+
+	if(!hSwapChainRef)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDestroyDCSwapChainKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psSwapChainRef = hSwapChainRef;
+
+	eError = ResManFreeResByPtr(psSwapChainRef->hResItem);
+
+	return eError;
+}
+
+
+static PVRSRV_ERROR DestroyDCSwapChain(PVRSRV_DC_SWAPCHAIN *psSwapChain)
+{
+	PVRSRV_ERROR				eError;
+	PVRSRV_DISPLAYCLASS_INFO	*psDCInfo = psSwapChain->psDCInfo;
+	IMG_UINT32 i;
+	int timeout = 30;
+
+
+
+	if( psDCInfo->psDCSwapChainShared )
+	{
+		if( psDCInfo->psDCSwapChainShared == psSwapChain )
+		{
+			psDCInfo->psDCSwapChainShared = psSwapChain->psNext;
+		}
+		else
+		{
+			PVRSRV_DC_SWAPCHAIN *psCurrentSwapChain;
+			psCurrentSwapChain = psDCInfo->psDCSwapChainShared;
+			while( psCurrentSwapChain->psNext )
+			{
+				if( psCurrentSwapChain->psNext != psSwapChain )
+				{
+					psCurrentSwapChain = psCurrentSwapChain->psNext;
+					continue;
+				}
+				psCurrentSwapChain->psNext = psSwapChain->psNext;
+				break;
+			}
+		}
+	}
+
+	if (psSwapChain->psQueue)
+	{
+		do
+		{
+			eError = PVRSRVDestroyCommandQueueKM(psSwapChain->psQueue);
+		} while (eError != PVRSRV_OK && (timeout-- > 0));
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"DestroyDCSwapChainCallBack: Failed to destroy command queue"));
+		}
+	}
+
+	eError = psDCInfo->psFuncTable->pfnDestroyDCSwapChain(psDCInfo->hExtDevice,
+															psSwapChain->hExtSwapChain);
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DestroyDCSwapChainCallBack: Failed to destroy DC swap chain"));
+		return eError;
+	}
+
+
+	for(i=0; i<psSwapChain->ui32BufferCount; i++)
+	{
+		if(psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo)
+		{
+			if (--psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount == 0)
+			{
+				PVRSRVFreeSyncInfoKM(psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo);
+			}
+		}
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DC_SWAPCHAIN), psSwapChain, IMG_NULL);
+
+
+	return eError;
+}
+
+
+static PVRSRV_ERROR DestroyDCSwapChainRefCallBack(IMG_PVOID pvParam, IMG_UINT32 ui32Param)
+{
+	PVRSRV_DC_SWAPCHAIN_REF *psSwapChainRef = (PVRSRV_DC_SWAPCHAIN_REF *) pvParam;
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	if(--psSwapChainRef->psSwapChain->ui32RefCount == 0)
+	{
+		eError = DestroyDCSwapChain(psSwapChainRef->psSwapChain);
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DC_SWAPCHAIN_REF), psSwapChainRef, IMG_NULL);
+	return eError;
+}
+
+static PVRSRV_DC_SWAPCHAIN* PVRSRVFindSharedDCSwapChainKM(PVRSRV_DISPLAYCLASS_INFO *psDCInfo,
+														 IMG_UINT32 ui32SwapChainID)
+{
+	PVRSRV_DC_SWAPCHAIN *psCurrentSwapChain;
+
+	for(psCurrentSwapChain = psDCInfo->psDCSwapChainShared;
+		psCurrentSwapChain;
+		psCurrentSwapChain = psCurrentSwapChain->psNext)
+	{
+		if(psCurrentSwapChain->ui32SwapChainID == ui32SwapChainID)
+			return psCurrentSwapChain;
+	}
+	return IMG_NULL;
+}
+
+static PVRSRV_ERROR PVRSRVCreateDCSwapChainRefKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												 PVRSRV_DC_SWAPCHAIN 		*psSwapChain,
+												 PVRSRV_DC_SWAPCHAIN_REF 	**ppsSwapChainRef)
+{
+	PVRSRV_DC_SWAPCHAIN_REF *psSwapChainRef = IMG_NULL;
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_DC_SWAPCHAIN_REF),
+					 (IMG_VOID **)&psSwapChainRef, IMG_NULL,
+					 "Display Class Swapchain Reference") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainRefKM: Failed psSwapChainRef alloc"));
+		return  PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OSMemSet (psSwapChainRef, 0, sizeof(PVRSRV_DC_SWAPCHAIN_REF));
+
+
+	psSwapChain->ui32RefCount++;
+
+
+	psSwapChainRef->psSwapChain = psSwapChain;
+	psSwapChainRef->hResItem = ResManRegisterRes(psPerProc->hResManContext,
+												  RESMAN_TYPE_DISPLAYCLASS_SWAPCHAIN_REF,
+												  psSwapChainRef,
+												  0,
+												  &DestroyDCSwapChainRefCallBack);
+	*ppsSwapChainRef = psSwapChainRef;
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVCreateDCSwapChainKM (PVRSRV_PER_PROCESS_DATA	*psPerProc,
+										IMG_HANDLE				hDeviceKM,
+										IMG_UINT32				ui32Flags,
+										DISPLAY_SURF_ATTRIBUTES	*psDstSurfAttrib,
+										DISPLAY_SURF_ATTRIBUTES *psSrcSurfAttrib,
+										IMG_UINT32				ui32BufferCount,
+										IMG_UINT32				ui32OEMFlags,
+										IMG_HANDLE				*phSwapChainRef,
+										IMG_UINT32				*pui32SwapChainID)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain = IMG_NULL;
+	PVRSRV_DC_SWAPCHAIN_REF *psSwapChainRef = IMG_NULL;
+	PVRSRV_SYNC_DATA *apsSyncData[PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS];
+	PVRSRV_QUEUE_INFO *psQueue = IMG_NULL;
+	PVRSRV_ERROR eError;
+	IMG_UINT32 i;
+	DISPLAY_INFO sDisplayInfo;
+	int timeout = 30;
+
+
+	if(!hDeviceKM
+	|| !psDstSurfAttrib
+	|| !psSrcSurfAttrib
+	|| !phSwapChainRef
+	|| !pui32SwapChainID)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if (ui32BufferCount > PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Too many buffers"));
+		return PVRSRV_ERROR_TOOMANYBUFFERS;
+	}
+
+#if 0 /* Removing limiation  to allow 1 buffer allocations */
+	if (ui32BufferCount < 2)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Too few buffers"));
+		return PVRSRV_ERROR_TOO_FEW_BUFFERS;
+	}
+#endif
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+
+	if( ui32Flags & PVRSRV_CREATE_SWAPCHAIN_QUERY )
+	{
+
+		psSwapChain = PVRSRVFindSharedDCSwapChainKM(psDCInfo, *pui32SwapChainID );
+		if( psSwapChain  )
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,"PVRSRVCreateDCSwapChainKM: found query"));
+
+			eError = PVRSRVCreateDCSwapChainRefKM(psPerProc,
+												  psSwapChain,
+												  &psSwapChainRef);
+			if( eError != PVRSRV_OK )
+			{
+				PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Couldn't create swap chain reference"));
+				return eError;
+			}
+
+			*phSwapChainRef = (IMG_HANDLE)psSwapChainRef;
+			return PVRSRV_OK;
+		}
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: No shared SwapChain found for query"));
+		return PVRSRV_ERROR_FLIP_CHAIN_EXISTS;
+	}
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_DC_SWAPCHAIN),
+					 (IMG_VOID **)&psSwapChain, IMG_NULL,
+					 "Display Class Swapchain") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Failed psSwapChain alloc"));
+		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+		goto ErrorExit;
+	}
+
+	OSMemSet (psSwapChain, 0, sizeof(PVRSRV_DC_SWAPCHAIN));
+
+	if (ui32OEMFlags & (PVR2D_CREATE_FLIPCHAIN_OEMDISPLAY |
+				PVR2D_CREATE_FLIPCHAIN_OEMGENERAL |
+				PVR2D_CREATE_FLIPCHAIN_OEMOVERLAY))
+	{
+		psQueue = NULL;
+	}
+	else
+	{
+		do
+		{
+			eError = PVRSRVCreateCommandQueueKM(1024, &psQueue);
+		} while (eError != PVRSRV_OK && (timeout-- > 0));
+
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Failed to create CmdQueue"));
+			goto ErrorExit;
+		}
+	}
+
+	psSwapChain->psQueue = psQueue;
+
+	for(i=0; i<ui32BufferCount; i++)
+	{
+		eError = PVRSRVAllocSyncInfoKM(IMG_NULL,
+										psDCInfo->hDevMemContext,
+										&psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Failed to alloc syninfo for psSwapChain"));
+			goto ErrorExit;
+		}
+
+		psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount++;
+
+
+		psSwapChain->asBuffer[i].sDeviceClassBuffer.pfnGetBufferAddr = psDCInfo->psFuncTable->pfnGetBufferAddr;
+		psSwapChain->asBuffer[i].sDeviceClassBuffer.hDevMemContext = psDCInfo->hDevMemContext;
+		psSwapChain->asBuffer[i].sDeviceClassBuffer.hExtDevice = psDCInfo->hExtDevice;
+
+
+		psSwapChain->asBuffer[i].psDCInfo = psDCInfo;
+		psSwapChain->asBuffer[i].psSwapChain = psSwapChain;
+
+
+		apsSyncData[i] = (PVRSRV_SYNC_DATA*)psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->psSyncDataMemInfoKM->pvLinAddrKM;
+	}
+
+	psSwapChain->ui32BufferCount = ui32BufferCount;
+	psSwapChain->psDCInfo = psDCInfo;
+
+	eError = psDCInfo->psFuncTable->pfnGetDCInfo(psDCInfo->hExtDevice, &sDisplayInfo);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Failed to get DC info"));
+		return eError;
+	}
+
+
+	eError =  psDCInfo->psFuncTable->pfnCreateDCSwapChain(psDCInfo->hExtDevice,
+														ui32Flags,
+														psDstSurfAttrib,
+														psSrcSurfAttrib,
+														ui32BufferCount,
+														apsSyncData,
+														ui32OEMFlags,
+														&psSwapChain->hExtSwapChain,
+														&psSwapChain->ui32SwapChainID);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Failed to create 3rd party SwapChain"));
+		goto ErrorExit;
+	}
+
+
+	eError = PVRSRVCreateDCSwapChainRefKM(psPerProc,
+										  psSwapChain,
+										  &psSwapChainRef);
+	if( eError != PVRSRV_OK )
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDCSwapChainKM: Couldn't create swap chain reference"));
+		goto ErrorExit;
+	}
+
+	psSwapChain->ui32RefCount = 1;
+	psSwapChain->ui32Flags = ui32Flags;
+
+
+	if( ui32Flags & PVRSRV_CREATE_SWAPCHAIN_SHARED )
+	{
+		if(! psDCInfo->psDCSwapChainShared )
+		{
+			psDCInfo->psDCSwapChainShared = psSwapChain;
+		}
+		else
+		{
+			PVRSRV_DC_SWAPCHAIN *psOldHead = psDCInfo->psDCSwapChainShared;
+			psDCInfo->psDCSwapChainShared = psSwapChain;
+			psSwapChain->psNext = psOldHead;
+		}
+	}
+
+
+	*pui32SwapChainID = psSwapChain->ui32SwapChainID;
+
+
+	*phSwapChainRef= (IMG_HANDLE)psSwapChainRef;
+
+	return eError;
+
+ErrorExit:
+
+	for(i=0; i<ui32BufferCount; i++)
+	{
+		if(psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo)
+		{
+			if (--psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount == 0)
+			{
+				PVRSRVFreeSyncInfoKM(psSwapChain->asBuffer[i].sDeviceClassBuffer.psKernelSyncInfo);
+			}
+		}
+	}
+
+	if(psQueue)
+	{
+		PVRSRVDestroyCommandQueueKM(psQueue);
+	}
+
+	if(psSwapChain)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_DC_SWAPCHAIN), psSwapChain, IMG_NULL);
+
+	}
+
+	return eError;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetDCDstRectKM(IMG_HANDLE	hDeviceKM,
+								  IMG_HANDLE	hSwapChainRef,
+								  IMG_RECT		*psRect)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain;
+
+	if(!hDeviceKM || !hSwapChainRef)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSetDCDstRectKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+	psSwapChain = ((PVRSRV_DC_SWAPCHAIN_REF*)hSwapChainRef)->psSwapChain;
+
+	return psDCInfo->psFuncTable->pfnSetDCDstRect(psDCInfo->hExtDevice,
+													psSwapChain->hExtSwapChain,
+													psRect);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetDCSrcRectKM(IMG_HANDLE	hDeviceKM,
+								  IMG_HANDLE	hSwapChainRef,
+								  IMG_RECT		*psRect)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain;
+
+	if(!hDeviceKM || !hSwapChainRef)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSetDCSrcRectKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+	psSwapChain = ((PVRSRV_DC_SWAPCHAIN_REF*)hSwapChainRef)->psSwapChain;
+
+	return psDCInfo->psFuncTable->pfnSetDCSrcRect(psDCInfo->hExtDevice,
+													psSwapChain->hExtSwapChain,
+													psRect);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetDCDstColourKeyKM(IMG_HANDLE	hDeviceKM,
+									   IMG_HANDLE	hSwapChainRef,
+									   IMG_UINT32	ui32CKColour)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain;
+
+	if(!hDeviceKM || !hSwapChainRef)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSetDCDstColourKeyKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+	psSwapChain = ((PVRSRV_DC_SWAPCHAIN_REF*)hSwapChainRef)->psSwapChain;
+
+	return psDCInfo->psFuncTable->pfnSetDCDstColourKey(psDCInfo->hExtDevice,
+														psSwapChain->hExtSwapChain,
+														ui32CKColour);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetDCSrcColourKeyKM(IMG_HANDLE	hDeviceKM,
+									   IMG_HANDLE	hSwapChainRef,
+									   IMG_UINT32	ui32CKColour)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain;
+
+	if(!hDeviceKM || !hSwapChainRef)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSetDCSrcColourKeyKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+	psSwapChain = ((PVRSRV_DC_SWAPCHAIN_REF*)hSwapChainRef)->psSwapChain;
+
+	return psDCInfo->psFuncTable->pfnSetDCSrcColourKey(psDCInfo->hExtDevice,
+														psSwapChain->hExtSwapChain,
+														ui32CKColour);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetDCBuffersKM(IMG_HANDLE	hDeviceKM,
+								  IMG_HANDLE	hSwapChainRef,
+								  IMG_UINT32	*pui32BufferCount,
+								  IMG_HANDLE	*phBuffer)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain;
+	IMG_HANDLE ahExtBuffer[PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS];
+	PVRSRV_ERROR eError;
+	IMG_UINT32 i;
+
+	if(!hDeviceKM || !hSwapChainRef || !phBuffer)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetDCBuffersKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+	psSwapChain = ((PVRSRV_DC_SWAPCHAIN_REF*)hSwapChainRef)->psSwapChain;
+
+
+	eError = psDCInfo->psFuncTable->pfnGetDCBuffers(psDCInfo->hExtDevice,
+													psSwapChain->hExtSwapChain,
+													pui32BufferCount,
+													ahExtBuffer);
+
+	PVR_ASSERT(*pui32BufferCount <= PVRSRV_MAX_DC_SWAPCHAIN_BUFFERS);
+
+
+
+
+	for(i=0; i<*pui32BufferCount; i++)
+	{
+		psSwapChain->asBuffer[i].sDeviceClassBuffer.hExtBuffer = ahExtBuffer[i];
+		phBuffer[i] = (IMG_HANDLE)&psSwapChain->asBuffer[i];
+	}
+
+	return eError;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSwapToDCBufferKM(IMG_HANDLE	hDeviceKM,
+									IMG_HANDLE	hBuffer,
+									IMG_UINT32	ui32SwapInterval,
+									IMG_HANDLE	hPrivateTag,
+									IMG_UINT32	ui32ClipRectCount,
+									IMG_RECT	*psClipRect)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_BUFFER *psBuffer;
+	PVRSRV_QUEUE_INFO *psQueue;
+	DISPLAYCLASS_FLIP_COMMAND *psFlipCmd;
+	IMG_UINT32 i;
+	IMG_BOOL bAddReferenceToLast = IMG_TRUE;
+	IMG_UINT16 ui16SwapCommandID = DC_FLIP_COMMAND;
+	IMG_UINT32 ui32NumSrcSyncs = 1;
+	PVRSRV_KERNEL_SYNC_INFO *apsSrcSync[2];
+	PVRSRV_COMMAND *psCommand;
+
+	if(!hDeviceKM || !hBuffer || !psClipRect)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCBufferKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+#if defined(SUPPORT_LMA)
+	eError = PVRSRVPowerLock(KERNEL_ID, IMG_FALSE);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+#endif
+
+	psBuffer = (PVRSRV_DC_BUFFER*)hBuffer;
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+
+#if defined(SUPPORT_CUSTOM_SWAP_OPERATIONS)
+
+	if(psDCInfo->psFuncTable->pfnQuerySwapCommandID != IMG_NULL)
+	{
+		psDCInfo->psFuncTable->pfnQuerySwapCommandID(psDCInfo->hExtDevice,
+													 psBuffer->psSwapChain->hExtSwapChain,
+													 psBuffer->sDeviceClassBuffer.hExtBuffer,
+													 hPrivateTag,
+													 &ui16SwapCommandID,
+													 &bAddReferenceToLast);
+
+	}
+
+#endif
+
+	psQueue = psBuffer->psSwapChain->psQueue;
+
+	if (!psQueue)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCBufferKM: Non-flippable swap chain"));
+		goto Exit;
+	}
+
+	apsSrcSync[0] = psBuffer->sDeviceClassBuffer.psKernelSyncInfo;
+
+
+
+	if(bAddReferenceToLast && psBuffer->psSwapChain->psLastFlipBuffer &&
+		psBuffer != psBuffer->psSwapChain->psLastFlipBuffer)
+	{
+		apsSrcSync[1] = psBuffer->psSwapChain->psLastFlipBuffer->sDeviceClassBuffer.psKernelSyncInfo;
+
+
+
+		ui32NumSrcSyncs++;
+	}
+
+
+	eError = PVRSRVInsertCommandKM (psQueue,
+									&psCommand,
+									psDCInfo->ui32DeviceID,
+									ui16SwapCommandID,
+									0,
+									IMG_NULL,
+									ui32NumSrcSyncs,
+									apsSrcSync,
+									sizeof(DISPLAYCLASS_FLIP_COMMAND) + (sizeof(IMG_RECT) * ui32ClipRectCount));
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCBufferKM: Failed to get space in queue"));
+		goto Exit;
+	}
+
+
+	psFlipCmd = (DISPLAYCLASS_FLIP_COMMAND*)psCommand->pvData;
+
+
+	psFlipCmd->hExtDevice = psDCInfo->hExtDevice;
+
+
+	psFlipCmd->hExtSwapChain = psBuffer->psSwapChain->hExtSwapChain;
+
+
+	psFlipCmd->hExtBuffer = psBuffer->sDeviceClassBuffer.hExtBuffer;
+
+
+	psFlipCmd->hPrivateTag = hPrivateTag;
+
+
+	psFlipCmd->ui32ClipRectCount = ui32ClipRectCount;
+
+	psFlipCmd->psClipRect = (IMG_RECT*)((IMG_UINT8*)psFlipCmd + sizeof(DISPLAYCLASS_FLIP_COMMAND));
+
+	for(i=0; i<ui32ClipRectCount; i++)
+	{
+		psFlipCmd->psClipRect[i] = psClipRect[i];
+	}
+
+
+	psFlipCmd->ui32SwapInterval = ui32SwapInterval;
+
+
+	eError = PVRSRVSubmitCommandKM (psQueue, psCommand);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCBufferKM: Failed to submit command"));
+		goto Exit;
+	}
+
+
+
+
+
+
+
+
+
+
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		if(PVRSRVProcessQueues(KERNEL_ID, IMG_FALSE) != PVRSRV_ERROR_PROCESSING_BLOCKED)
+		{
+			goto ProcessedQueues;
+		}
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+	PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCBufferKM: Failed to process queues"));
+
+	eError = PVRSRV_ERROR_GENERIC;
+	goto Exit;
+
+ProcessedQueues:
+
+	psBuffer->psSwapChain->psLastFlipBuffer = psBuffer;
+
+Exit:
+
+	if(eError == PVRSRV_ERROR_CANNOT_GET_QUEUE_SPACE)
+	{
+		eError = PVRSRV_ERROR_RETRY;
+	}
+
+#if defined(SUPPORT_LMA)
+	PVRSRVPowerUnlock(KERNEL_ID);
+#endif
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSwapToDCSystemKM(IMG_HANDLE	hDeviceKM,
+									IMG_HANDLE	hSwapChainRef)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_QUEUE_INFO *psQueue;
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	PVRSRV_DC_SWAPCHAIN *psSwapChain;
+	PVRSRV_DC_SWAPCHAIN_REF *psSwapChainRef;
+	DISPLAYCLASS_FLIP_COMMAND *psFlipCmd;
+	IMG_UINT32 ui32NumSrcSyncs = 1;
+	PVRSRV_KERNEL_SYNC_INFO *apsSrcSync[2];
+	PVRSRV_COMMAND *psCommand;
+	IMG_BOOL bAddReferenceToLast = IMG_TRUE;
+	IMG_UINT16 ui16SwapCommandID = DC_FLIP_COMMAND;
+
+	if(!hDeviceKM || !hSwapChainRef)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCSystemKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+#if defined(SUPPORT_LMA)
+	eError = PVRSRVPowerLock(KERNEL_ID, IMG_FALSE);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+#endif
+
+	psDCInfo = DCDeviceHandleToDCInfo(hDeviceKM);
+	psSwapChainRef = (PVRSRV_DC_SWAPCHAIN_REF*)hSwapChainRef;
+	psSwapChain = psSwapChainRef->psSwapChain;
+
+
+	psQueue = psSwapChain->psQueue;
+
+	if (!psQueue)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCSystemKM: Non-flippable swap chain"));
+		goto Exit;
+	}
+
+#if defined(SUPPORT_CUSTOM_SWAP_OPERATIONS)
+
+	if(psDCInfo->psFuncTable->pfnQuerySwapCommandID != IMG_NULL)
+	{
+		psDCInfo->psFuncTable->pfnQuerySwapCommandID(psDCInfo->hExtDevice,
+													 psSwapChain->hExtSwapChain,
+													 psDCInfo->sSystemBuffer.sDeviceClassBuffer.hExtBuffer,
+													 0,
+													 &ui16SwapCommandID,
+													 &bAddReferenceToLast);
+
+	}
+
+#endif
+
+
+	apsSrcSync[0] = psDCInfo->sSystemBuffer.sDeviceClassBuffer.psKernelSyncInfo;
+
+
+
+	if(bAddReferenceToLast && psSwapChain->psLastFlipBuffer)
+	{
+
+		if (apsSrcSync[0] != psSwapChain->psLastFlipBuffer->sDeviceClassBuffer.psKernelSyncInfo)
+		{
+			apsSrcSync[1] = psSwapChain->psLastFlipBuffer->sDeviceClassBuffer.psKernelSyncInfo;
+
+
+
+			ui32NumSrcSyncs++;
+		}
+	}
+
+
+	eError = PVRSRVInsertCommandKM (psQueue,
+									&psCommand,
+									psDCInfo->ui32DeviceID,
+									ui16SwapCommandID,
+									0,
+									IMG_NULL,
+									ui32NumSrcSyncs,
+									apsSrcSync,
+									sizeof(DISPLAYCLASS_FLIP_COMMAND));
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCSystemKM: Failed to get space in queue"));
+		goto Exit;
+	}
+
+
+	psFlipCmd = (DISPLAYCLASS_FLIP_COMMAND*)psCommand->pvData;
+
+
+	psFlipCmd->hExtDevice = psDCInfo->hExtDevice;
+
+
+	psFlipCmd->hExtSwapChain = psSwapChain->hExtSwapChain;
+
+
+	psFlipCmd->hExtBuffer = psDCInfo->sSystemBuffer.sDeviceClassBuffer.hExtBuffer;
+
+
+	psFlipCmd->hPrivateTag = IMG_NULL;
+
+
+	psFlipCmd->ui32ClipRectCount = 0;
+
+	psFlipCmd->ui32SwapInterval = 1;
+
+
+	eError = PVRSRVSubmitCommandKM (psQueue, psCommand);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCSystemKM: Failed to submit command"));
+		goto Exit;
+	}
+
+
+
+
+
+
+
+
+
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		if(PVRSRVProcessQueues(KERNEL_ID, IMG_FALSE) != PVRSRV_ERROR_PROCESSING_BLOCKED)
+		{
+			goto ProcessedQueues;
+		}
+
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+	PVR_DPF((PVR_DBG_ERROR,"PVRSRVSwapToDCSystemKM: Failed to process queues"));
+	eError = PVRSRV_ERROR_GENERIC;
+	goto Exit;
+
+ProcessedQueues:
+
+	psSwapChain->psLastFlipBuffer = &psDCInfo->sSystemBuffer;
+
+	eError = PVRSRV_OK;
+
+Exit:
+
+	if(eError == PVRSRV_ERROR_CANNOT_GET_QUEUE_SPACE)
+	{
+		eError = PVRSRV_ERROR_RETRY;
+	}
+
+#if defined(SUPPORT_LMA)
+	PVRSRVPowerUnlock(KERNEL_ID);
+#endif
+	return eError;
+}
+
+
+PVRSRV_ERROR PVRSRVRegisterSystemISRHandler (PFN_ISR_HANDLER	pfnISRHandler,
+											 IMG_VOID			*pvISRHandlerData,
+											 IMG_UINT32			ui32ISRSourceMask,
+											 IMG_UINT32			ui32DeviceID)
+{
+	SYS_DATA 			*psSysData;
+	PVRSRV_DEVICE_NODE	*psDevNode;
+
+	PVR_UNREFERENCED_PARAMETER(ui32ISRSourceMask);
+
+	SysAcquireData(&psSysData);
+
+
+	psDevNode = (PVRSRV_DEVICE_NODE*)
+				List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+												MatchDeviceKM_AnyVaCb,
+												ui32DeviceID,
+												IMG_TRUE);
+
+	if (psDevNode == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterSystemISRHandler: Failed to get psDevNode"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	psDevNode->pvISRData = (IMG_VOID*) pvISRHandlerData;
+
+
+	psDevNode->pfnDeviceISR	= pfnISRHandler;
+
+	return PVRSRV_OK;
+}
+
+IMG_VOID PVRSRVSetDCState_ForEachVaCb(PVRSRV_DEVICE_NODE *psDeviceNode, va_list va)
+{
+	PVRSRV_DISPLAYCLASS_INFO *psDCInfo;
+	IMG_UINT32 ui32State;
+	ui32State = va_arg(va, IMG_UINT32);
+
+	if (psDeviceNode->sDevId.eDeviceClass == PVRSRV_DEVICE_CLASS_DISPLAY)
+	{
+		psDCInfo = (PVRSRV_DISPLAYCLASS_INFO *)psDeviceNode->pvDevice;
+		if (psDCInfo->psFuncTable->pfnSetDCState && psDCInfo->hExtDevice)
+		{
+			psDCInfo->psFuncTable->pfnSetDCState(psDCInfo->hExtDevice, ui32State);
+		}
+	}
+}
+
+
+IMG_VOID IMG_CALLCONV PVRSRVSetDCState(IMG_UINT32 ui32State)
+{
+	SYS_DATA					*psSysData;
+
+	SysAcquireData(&psSysData);
+
+	List_PVRSRV_DEVICE_NODE_ForEach_va(psSysData->psDeviceNodeList,
+										PVRSRVSetDCState_ForEachVaCb,
+										ui32State);
+}
+
+
+IMG_EXPORT
+IMG_BOOL PVRGetDisplayClassJTable(PVRSRV_DC_DISP2SRV_KMJTABLE *psJTable)
+{
+	psJTable->ui32TableSize = sizeof(PVRSRV_DC_DISP2SRV_KMJTABLE);
+	psJTable->pfnPVRSRVRegisterDCDevice = PVRSRVRegisterDCDeviceKM;
+	psJTable->pfnPVRSRVRemoveDCDevice = PVRSRVRemoveDCDeviceKM;
+	psJTable->pfnPVRSRVOEMFunction = SysOEMFunction;
+	psJTable->pfnPVRSRVRegisterCmdProcList = PVRSRVRegisterCmdProcListKM;
+	psJTable->pfnPVRSRVRemoveCmdProcList = PVRSRVRemoveCmdProcListKM;
+#if defined(SUPPORT_MISR_IN_THREAD)
+        psJTable->pfnPVRSRVCmdComplete = OSVSyncMISR;
+#else
+        psJTable->pfnPVRSRVCmdComplete = PVRSRVCommandCompleteKM;
+#endif
+	psJTable->pfnPVRSRVRegisterSystemISRHandler = PVRSRVRegisterSystemISRHandler;
+	psJTable->pfnPVRSRVRegisterPowerDevice = PVRSRVRegisterPowerDevice;
+#if defined(SUPPORT_CUSTOM_SWAP_OPERATIONS)
+	psJTable->pfnPVRSRVFreeCmdCompletePacket = &PVRSRVFreeCommandCompletePacketKM;
+#endif
+
+	return IMG_TRUE;
+}
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVCloseBCDeviceKM (IMG_HANDLE	hDeviceKM,
+									IMG_BOOL	bResManCallback)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_BUFFERCLASS_PERCONTEXT_INFO *psBCPerContextInfo;
+
+	PVR_UNREFERENCED_PARAMETER(bResManCallback);
+
+	psBCPerContextInfo = (PVRSRV_BUFFERCLASS_PERCONTEXT_INFO *)hDeviceKM;
+
+
+	eError = ResManFreeResByPtr(psBCPerContextInfo->hResItem);
+
+	return eError;
+}
+
+
+static PVRSRV_ERROR CloseBCDeviceCallBack(IMG_PVOID		pvParam,
+										  IMG_UINT32	ui32Param)
+{
+	PVRSRV_BUFFERCLASS_PERCONTEXT_INFO *psBCPerContextInfo;
+	PVRSRV_BUFFERCLASS_INFO *psBCInfo;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	psBCPerContextInfo = (PVRSRV_BUFFERCLASS_PERCONTEXT_INFO *)pvParam;
+	psBCInfo = psBCPerContextInfo->psBCInfo;
+
+	psBCInfo->ui32RefCount--;
+	if(psBCInfo->ui32RefCount == 0)
+	{
+		IMG_UINT32 i;
+
+
+		psBCInfo->psFuncTable->pfnCloseBCDevice(psBCInfo->ui32DeviceID, psBCInfo->hExtDevice);
+
+
+		for(i=0; i<psBCInfo->ui32BufferCount; i++)
+		{
+			if(psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo)
+			{
+				if (--psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount == 0)
+				{
+					PVRSRVFreeSyncInfoKM(psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo);
+				}
+			}
+		}
+
+
+		if(psBCInfo->psBuffer)
+		{
+			OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_BC_BUFFER), psBCInfo->psBuffer, IMG_NULL);
+			psBCInfo->psBuffer = IMG_NULL;
+		}
+	}
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_BUFFERCLASS_PERCONTEXT_INFO), psBCPerContextInfo, IMG_NULL);
+
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVOpenBCDeviceKM (PVRSRV_PER_PROCESS_DATA	*psPerProc,
+								   IMG_UINT32				ui32DeviceID,
+								   IMG_HANDLE				hDevCookie,
+								   IMG_HANDLE				*phDeviceKM)
+{
+	PVRSRV_BUFFERCLASS_INFO	*psBCInfo;
+	PVRSRV_BUFFERCLASS_PERCONTEXT_INFO	*psBCPerContextInfo;
+	PVRSRV_DEVICE_NODE		*psDeviceNode;
+	SYS_DATA 				*psSysData;
+	IMG_UINT32 				i;
+	PVRSRV_ERROR			eError;
+
+	if(!phDeviceKM || !hDevCookie)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: Invalid params"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	SysAcquireData(&psSysData);
+
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)
+			List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+										   MatchDeviceKM_AnyVaCb,
+										   ui32DeviceID,
+										   IMG_FALSE,
+										   PVRSRV_DEVICE_CLASS_BUFFER);
+	if (!psDeviceNode)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: No devnode matching index %d", ui32DeviceID));
+		return PVRSRV_ERROR_GENERIC;
+	}
+	psBCInfo = (PVRSRV_BUFFERCLASS_INFO*)psDeviceNode->pvDevice;
+
+
+
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(*psBCPerContextInfo),
+				  (IMG_VOID **)&psBCPerContextInfo, IMG_NULL,
+				  "Buffer Class per Context Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: Failed psBCPerContextInfo alloc"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+	OSMemSet(psBCPerContextInfo, 0, sizeof(*psBCPerContextInfo));
+
+	if(psBCInfo->ui32RefCount++ == 0)
+	{
+		BUFFER_INFO sBufferInfo;
+
+		psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevCookie;
+
+
+		psBCInfo->hDevMemContext = (IMG_HANDLE)psDeviceNode->sDevMemoryInfo.pBMKernelContext;
+
+
+		eError = psBCInfo->psFuncTable->pfnOpenBCDevice(ui32DeviceID, &psBCInfo->hExtDevice);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: Failed to open external BC device"));
+			return eError;
+		}
+
+
+		eError = psBCInfo->psFuncTable->pfnGetBCInfo(psBCInfo->hExtDevice, &sBufferInfo);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM : Failed to get BC Info"));
+			return eError;
+		}
+
+
+		psBCInfo->ui32BufferCount = sBufferInfo.ui32BufferCount;
+
+
+
+		eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+							  sizeof(PVRSRV_BC_BUFFER) * sBufferInfo.ui32BufferCount,
+							  (IMG_VOID **)&psBCInfo->psBuffer,
+						 	  IMG_NULL,
+							  "Array of Buffer Class Buffer");
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: Failed to allocate BC buffers"));
+			return eError;
+		}
+		OSMemSet (psBCInfo->psBuffer,
+					0,
+					sizeof(PVRSRV_BC_BUFFER) * sBufferInfo.ui32BufferCount);
+
+		for(i=0; i<psBCInfo->ui32BufferCount; i++)
+		{
+
+			eError = PVRSRVAllocSyncInfoKM(IMG_NULL,
+										psBCInfo->hDevMemContext,
+										&psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo);
+			if(eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: Failed sync info alloc"));
+				goto ErrorExit;
+			}
+
+			psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount++;
+
+
+
+
+			eError = psBCInfo->psFuncTable->pfnGetBCBuffer(psBCInfo->hExtDevice,
+															i,
+															psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->psSyncData,
+															&psBCInfo->psBuffer[i].sDeviceClassBuffer.hExtBuffer);
+			if(eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"PVRSRVOpenBCDeviceKM: Failed to get BC buffers"));
+				goto ErrorExit;
+			}
+
+
+			psBCInfo->psBuffer[i].sDeviceClassBuffer.pfnGetBufferAddr = psBCInfo->psFuncTable->pfnGetBufferAddr;
+			psBCInfo->psBuffer[i].sDeviceClassBuffer.hDevMemContext = psBCInfo->hDevMemContext;
+			psBCInfo->psBuffer[i].sDeviceClassBuffer.hExtDevice = psBCInfo->hExtDevice;
+		}
+	}
+
+	psBCPerContextInfo->psBCInfo = psBCInfo;
+	psBCPerContextInfo->hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													 RESMAN_TYPE_BUFFERCLASS_DEVICE,
+													 psBCPerContextInfo,
+													 0,
+													 CloseBCDeviceCallBack);
+
+
+	*phDeviceKM = (IMG_HANDLE)psBCPerContextInfo;
+
+	return PVRSRV_OK;
+
+ErrorExit:
+
+
+	for(i=0; i<psBCInfo->ui32BufferCount; i++)
+	{
+		if(psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo)
+		{
+			if (--psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo->ui32RefCount == 0)
+			{
+				PVRSRVFreeSyncInfoKM(psBCInfo->psBuffer[i].sDeviceClassBuffer.psKernelSyncInfo);
+			}
+		}
+	}
+
+
+	if(psBCInfo->psBuffer)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_BC_BUFFER), psBCInfo->psBuffer, IMG_NULL);
+		psBCInfo->psBuffer = IMG_NULL;
+	}
+
+	return eError;
+}
+
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetBCInfoKM (IMG_HANDLE hDeviceKM,
+								BUFFER_INFO *psBufferInfo)
+{
+	PVRSRV_BUFFERCLASS_INFO *psBCInfo;
+	PVRSRV_ERROR 			eError;
+
+	if(!hDeviceKM || !psBufferInfo)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetBCInfoKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psBCInfo = BCDeviceHandleToBCInfo(hDeviceKM);
+
+	eError = psBCInfo->psFuncTable->pfnGetBCInfo(psBCInfo->hExtDevice, psBufferInfo);
+
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetBCInfoKM : Failed to get BC Info"));
+		return eError;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetBCBufferKM (IMG_HANDLE hDeviceKM,
+								  IMG_UINT32 ui32BufferIndex,
+								  IMG_HANDLE *phBuffer)
+{
+	PVRSRV_BUFFERCLASS_INFO *psBCInfo;
+
+	if(!hDeviceKM || !phBuffer)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetBCBufferKM: Invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psBCInfo = BCDeviceHandleToBCInfo(hDeviceKM);
+
+	if(ui32BufferIndex < psBCInfo->ui32BufferCount)
+	{
+		*phBuffer = (IMG_HANDLE)&psBCInfo->psBuffer[ui32BufferIndex];
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetBCBufferKM: Buffer index %d out of range (%d)", ui32BufferIndex,psBCInfo->ui32BufferCount));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVGetBCBufferIdFromTagKM(IMG_HANDLE hDeviceKM,
+								  IMG_UINT32 ui32BufferIndex,
+								  IMG_HANDLE pidx)
+{
+	PVRSRV_BUFFERCLASS_INFO *psBCInfo;
+	PVRSRV_ERROR 			eError = PVRSRV_ERROR_INVALID_PARAMS;
+
+	if(NULL == hDeviceKM)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"%s: Invalid parameters", __FUNCTION__));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+	psBCInfo = BCDeviceHandleToBCInfo(hDeviceKM);
+
+	if (NULL != psBCInfo->psFuncTable->pfnGetBufferIdFromTag) {
+		eError = psBCInfo->psFuncTable->pfnGetBufferIdFromTag(psBCInfo->hExtDevice,
+				ui32BufferIndex,
+				pidx);
+		if(eError != PVRSRV_OK) {
+			PVR_DPF((PVR_DBG_ERROR,"%s : Failed to get BC Buffer Index", __FUNCTION__));
+			return PVRSRV_ERROR_GENERIC;
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+IMG_BOOL PVRGetBufferClassJTable(PVRSRV_BC_BUFFER2SRV_KMJTABLE *psJTable)
+{
+	psJTable->ui32TableSize = sizeof(PVRSRV_BC_BUFFER2SRV_KMJTABLE);
+
+	psJTable->pfnPVRSRVRegisterBCDevice = PVRSRVRegisterBCDeviceKM;
+	psJTable->pfnPVRSRVRemoveBCDevice = PVRSRVRemoveBCDeviceKM;
+
+	return IMG_TRUE;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/devicemem.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/devicemem.c
new file mode 100644
index 0000000..1cf0c3c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/devicemem.c
@@ -0,0 +1,1546 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <stddef.h>
+
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "pdump_km.h"
+#include "pvr_bridge_km.h"
+#include "mm.h"
+
+static PVRSRV_ERROR AllocDeviceMem(IMG_HANDLE		hDevCookie,
+									IMG_HANDLE		hDevMemHeap,
+									IMG_UINT32		ui32Flags,
+									IMG_SIZE_T		ui32Size,
+									IMG_SIZE_T		ui32Alignment,
+									PVRSRV_KERNEL_MEM_INFO	**ppsMemInfo);
+
+typedef struct _RESMAN_MAP_DEVICE_MEM_DATA_
+{
+
+	PVRSRV_KERNEL_MEM_INFO	*psMemInfo;
+
+	PVRSRV_KERNEL_MEM_INFO	*psSrcMemInfo;
+} RESMAN_MAP_DEVICE_MEM_DATA;
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDeviceMemHeapsKM(IMG_HANDLE hDevCookie,
+													PVRSRV_HEAP_INFO *psHeapInfo)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	IMG_UINT32 ui32HeapCount;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+	IMG_UINT32 i;
+
+	if (hDevCookie == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVGetDeviceMemHeapsKM: hDevCookie invalid"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevCookie;
+
+
+	ui32HeapCount = psDeviceNode->sDevMemoryInfo.ui32HeapCount;
+	psDeviceMemoryHeap = psDeviceNode->sDevMemoryInfo.psDeviceMemoryHeap;
+
+
+	PVR_ASSERT(ui32HeapCount <= PVRSRV_MAX_CLIENT_HEAPS);
+
+
+	for(i=0; i<ui32HeapCount; i++)
+	{
+
+		psHeapInfo[i].ui32HeapID = psDeviceMemoryHeap[i].ui32HeapID;
+		psHeapInfo[i].hDevMemHeap = psDeviceMemoryHeap[i].hDevMemHeap;
+		psHeapInfo[i].sDevVAddrBase = psDeviceMemoryHeap[i].sDevVAddrBase;
+		psHeapInfo[i].ui32HeapByteSize = psDeviceMemoryHeap[i].ui32HeapSize;
+		psHeapInfo[i].ui32Attribs = psDeviceMemoryHeap[i].ui32Attribs;
+	}
+
+	for(; i < PVRSRV_MAX_CLIENT_HEAPS; i++)
+	{
+		OSMemSet(psHeapInfo + i, 0, sizeof(*psHeapInfo));
+		psHeapInfo[i].ui32HeapID = (IMG_UINT32)PVRSRV_UNDEFINED_HEAP_ID;
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateDeviceMemContextKM(IMG_HANDLE					hDevCookie,
+														 PVRSRV_PER_PROCESS_DATA	*psPerProc,
+														 IMG_HANDLE 				*phDevMemContext,
+														 IMG_UINT32 				*pui32ClientHeapCount,
+														 PVRSRV_HEAP_INFO			*psHeapInfo,
+														 IMG_BOOL					*pbCreated,
+														 IMG_BOOL 					*pbShared)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	IMG_UINT32 ui32HeapCount, ui32ClientHeapCount=0;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+	IMG_HANDLE hDevMemContext;
+	IMG_HANDLE hDevMemHeap;
+	IMG_DEV_PHYADDR sPDDevPAddr;
+	IMG_UINT32 i;
+
+#if !defined(PVR_SECURE_HANDLES)
+	PVR_UNREFERENCED_PARAMETER(pbShared);
+#endif
+
+	if (hDevCookie == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVCreateDeviceMemContextKM: hDevCookie invalid"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevCookie;
+
+
+
+	ui32HeapCount = psDeviceNode->sDevMemoryInfo.ui32HeapCount;
+	psDeviceMemoryHeap = psDeviceNode->sDevMemoryInfo.psDeviceMemoryHeap;
+
+
+
+	PVR_ASSERT(ui32HeapCount <= PVRSRV_MAX_CLIENT_HEAPS);
+
+
+
+	hDevMemContext = BM_CreateContext(psDeviceNode,
+									  &sPDDevPAddr,
+									  psPerProc,
+									  pbCreated);
+	if (hDevMemContext == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateDeviceMemContextKM: Failed BM_CreateContext"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+
+	for(i=0; i<ui32HeapCount; i++)
+	{
+		switch(psDeviceMemoryHeap[i].DevMemHeapType)
+		{
+			case DEVICE_MEMORY_HEAP_SHARED_EXPORTED:
+			{
+
+				psHeapInfo[ui32ClientHeapCount].ui32HeapID = psDeviceMemoryHeap[i].ui32HeapID;
+				psHeapInfo[ui32ClientHeapCount].hDevMemHeap = psDeviceMemoryHeap[i].hDevMemHeap;
+				psHeapInfo[ui32ClientHeapCount].sDevVAddrBase = psDeviceMemoryHeap[i].sDevVAddrBase;
+				psHeapInfo[ui32ClientHeapCount].ui32HeapByteSize = psDeviceMemoryHeap[i].ui32HeapSize;
+				psHeapInfo[ui32ClientHeapCount].ui32Attribs = psDeviceMemoryHeap[i].ui32Attribs;
+#if defined(PVR_SECURE_HANDLES)
+				pbShared[ui32ClientHeapCount] = IMG_TRUE;
+#endif
+				ui32ClientHeapCount++;
+				break;
+			}
+			case DEVICE_MEMORY_HEAP_PERCONTEXT:
+			{
+				hDevMemHeap = BM_CreateHeap(hDevMemContext,
+											&psDeviceMemoryHeap[i]);
+
+
+				psHeapInfo[ui32ClientHeapCount].ui32HeapID = psDeviceMemoryHeap[i].ui32HeapID;
+				psHeapInfo[ui32ClientHeapCount].hDevMemHeap = hDevMemHeap;
+				psHeapInfo[ui32ClientHeapCount].sDevVAddrBase = psDeviceMemoryHeap[i].sDevVAddrBase;
+				psHeapInfo[ui32ClientHeapCount].ui32HeapByteSize = psDeviceMemoryHeap[i].ui32HeapSize;
+				psHeapInfo[ui32ClientHeapCount].ui32Attribs = psDeviceMemoryHeap[i].ui32Attribs;
+#if defined(PVR_SECURE_HANDLES)
+				pbShared[ui32ClientHeapCount] = IMG_FALSE;
+#endif
+
+				ui32ClientHeapCount++;
+				break;
+			}
+		}
+	}
+
+
+	*pui32ClientHeapCount = ui32ClientHeapCount;
+	*phDevMemContext = hDevMemContext;
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyDeviceMemContextKM(IMG_HANDLE hDevCookie,
+														  IMG_HANDLE hDevMemContext,
+														  IMG_BOOL *pbDestroyed)
+{
+	PVR_UNREFERENCED_PARAMETER(hDevCookie);
+
+	return BM_DestroyContext(hDevMemContext, pbDestroyed);
+}
+
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetDeviceMemHeapInfoKM(IMG_HANDLE					hDevCookie,
+														 IMG_HANDLE 				hDevMemContext,
+														 IMG_UINT32 				*pui32ClientHeapCount,
+														 PVRSRV_HEAP_INFO			*psHeapInfo,
+														 IMG_BOOL 					*pbShared)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	IMG_UINT32 ui32HeapCount, ui32ClientHeapCount=0;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+	IMG_HANDLE hDevMemHeap;
+	IMG_UINT32 i;
+
+#if !defined(PVR_SECURE_HANDLES)
+	PVR_UNREFERENCED_PARAMETER(pbShared);
+#endif
+
+	if (hDevCookie == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVGetDeviceMemHeapInfoKM: hDevCookie invalid"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevCookie;
+
+
+
+	ui32HeapCount = psDeviceNode->sDevMemoryInfo.ui32HeapCount;
+	psDeviceMemoryHeap = psDeviceNode->sDevMemoryInfo.psDeviceMemoryHeap;
+
+
+
+	PVR_ASSERT(ui32HeapCount <= PVRSRV_MAX_CLIENT_HEAPS);
+
+
+	for(i=0; i<ui32HeapCount; i++)
+	{
+		switch(psDeviceMemoryHeap[i].DevMemHeapType)
+		{
+			case DEVICE_MEMORY_HEAP_SHARED_EXPORTED:
+			{
+
+				psHeapInfo[ui32ClientHeapCount].ui32HeapID = psDeviceMemoryHeap[i].ui32HeapID;
+				psHeapInfo[ui32ClientHeapCount].hDevMemHeap = psDeviceMemoryHeap[i].hDevMemHeap;
+				psHeapInfo[ui32ClientHeapCount].sDevVAddrBase = psDeviceMemoryHeap[i].sDevVAddrBase;
+				psHeapInfo[ui32ClientHeapCount].ui32HeapByteSize = psDeviceMemoryHeap[i].ui32HeapSize;
+				psHeapInfo[ui32ClientHeapCount].ui32Attribs = psDeviceMemoryHeap[i].ui32Attribs;
+#if defined(PVR_SECURE_HANDLES)
+				pbShared[ui32ClientHeapCount] = IMG_TRUE;
+#endif
+				ui32ClientHeapCount++;
+				break;
+			}
+			case DEVICE_MEMORY_HEAP_PERCONTEXT:
+			{
+				hDevMemHeap = BM_CreateHeap(hDevMemContext,
+											&psDeviceMemoryHeap[i]);
+
+
+				psHeapInfo[ui32ClientHeapCount].ui32HeapID = psDeviceMemoryHeap[i].ui32HeapID;
+				psHeapInfo[ui32ClientHeapCount].hDevMemHeap = hDevMemHeap;
+				psHeapInfo[ui32ClientHeapCount].sDevVAddrBase = psDeviceMemoryHeap[i].sDevVAddrBase;
+				psHeapInfo[ui32ClientHeapCount].ui32HeapByteSize = psDeviceMemoryHeap[i].ui32HeapSize;
+				psHeapInfo[ui32ClientHeapCount].ui32Attribs = psDeviceMemoryHeap[i].ui32Attribs;
+#if defined(PVR_SECURE_HANDLES)
+				pbShared[ui32ClientHeapCount] = IMG_FALSE;
+#endif
+
+				ui32ClientHeapCount++;
+				break;
+			}
+		}
+	}
+
+
+	*pui32ClientHeapCount = ui32ClientHeapCount;
+
+	return PVRSRV_OK;
+}
+
+
+static PVRSRV_ERROR AllocDeviceMem(IMG_HANDLE		hDevCookie,
+									IMG_HANDLE		hDevMemHeap,
+									IMG_UINT32		ui32Flags,
+									IMG_SIZE_T		ui32Size,
+									IMG_SIZE_T		ui32Alignment,
+									PVRSRV_KERNEL_MEM_INFO	**ppsMemInfo)
+{
+	PVRSRV_KERNEL_MEM_INFO	*psMemInfo;
+	BM_HANDLE 		hBuffer;
+
+	PVRSRV_MEMBLK	*psMemBlock;
+	IMG_BOOL		bBMError;
+
+	PVR_UNREFERENCED_PARAMETER(hDevCookie);
+
+	*ppsMemInfo = IMG_NULL;
+
+	if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+					sizeof(PVRSRV_KERNEL_MEM_INFO),
+					(IMG_VOID **)&psMemInfo, IMG_NULL,
+					"Kernel Memory Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"AllocDeviceMem: Failed to alloc memory for block"));
+		return (PVRSRV_ERROR_OUT_OF_MEMORY);
+	}
+
+	OSMemSet(psMemInfo, 0, sizeof(*psMemInfo));
+
+	psMemBlock = &(psMemInfo->sMemBlk);
+
+
+	psMemInfo->ui32Flags = ui32Flags | PVRSRV_MEM_RAM_BACKED_ALLOCATION;
+
+	bBMError = BM_Alloc (hDevMemHeap,
+							IMG_NULL,
+							ui32Size,
+							&psMemInfo->ui32Flags,
+							IMG_CAST_TO_DEVVADDR_UINT(ui32Alignment),
+							&hBuffer);
+
+	if (!bBMError)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"AllocDeviceMem: BM_Alloc Failed"));
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_MEM_INFO), psMemInfo, IMG_NULL);
+
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+
+	psMemBlock->sDevVirtAddr = BM_HandleToDevVaddr(hBuffer);
+	psMemBlock->hOSMemHandle = BM_HandleToOSMemHandle(hBuffer);
+
+
+	psMemBlock->hBuffer = (IMG_HANDLE)hBuffer;
+
+
+
+	psMemInfo->pvLinAddrKM = BM_HandleToCpuVaddr(hBuffer);
+
+	psMemInfo->sDevVAddr = psMemBlock->sDevVirtAddr;
+
+	psMemInfo->ui32AllocSize = ui32Size;
+
+
+	psMemInfo->pvSysBackupBuffer = IMG_NULL;
+
+
+	*ppsMemInfo = psMemInfo;
+
+
+	return (PVRSRV_OK);
+}
+
+static PVRSRV_ERROR FreeDeviceMem2(PVRSRV_KERNEL_MEM_INFO *psMemInfo, IMG_BOOL bFromAllocator)
+{
+	BM_HANDLE		hBuffer;
+
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	hBuffer = psMemInfo->sMemBlk.hBuffer;
+
+
+	if (bFromAllocator)
+		BM_Free(hBuffer, psMemInfo->ui32Flags);
+	else
+		BM_FreeExport(hBuffer, psMemInfo->ui32Flags);
+
+
+	if ((psMemInfo->pvSysBackupBuffer) && bFromAllocator)
+	{
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, psMemInfo->ui32AllocSize, psMemInfo->pvSysBackupBuffer, IMG_NULL);
+		psMemInfo->pvSysBackupBuffer = IMG_NULL;
+	}
+
+	if (psMemInfo->ui32RefCount == 0)
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_MEM_INFO), psMemInfo, IMG_NULL);
+
+
+	return(PVRSRV_OK);
+}
+
+static PVRSRV_ERROR FreeDeviceMem(PVRSRV_KERNEL_MEM_INFO *psMemInfo)
+{
+	BM_HANDLE		hBuffer;
+
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	hBuffer = psMemInfo->sMemBlk.hBuffer;
+
+
+	BM_Free(hBuffer, psMemInfo->ui32Flags);
+
+	if(psMemInfo->pvSysBackupBuffer)
+	{
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, psMemInfo->ui32AllocSize, psMemInfo->pvSysBackupBuffer, IMG_NULL);
+		psMemInfo->pvSysBackupBuffer = IMG_NULL;
+	}
+
+	OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_MEM_INFO), psMemInfo, IMG_NULL);
+
+
+	return(PVRSRV_OK);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVAllocSyncInfoKM(IMG_HANDLE					hDevCookie,
+												IMG_HANDLE					hDevMemContext,
+												PVRSRV_KERNEL_SYNC_INFO		**ppsKernelSyncInfo)
+{
+	IMG_HANDLE hSyncDevMemHeap;
+	DEVICE_MEMORY_INFO *psDevMemoryInfo;
+	BM_CONTEXT *pBMContext;
+	PVRSRV_ERROR eError;
+	PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo;
+	PVRSRV_SYNC_DATA *psSyncData;
+
+	eError = OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+						sizeof(PVRSRV_KERNEL_SYNC_INFO),
+						(IMG_VOID **)&psKernelSyncInfo, IMG_NULL,
+						"Kernel Synchronization Info");
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVAllocSyncInfoKM: Failed to alloc memory"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	psKernelSyncInfo->ui32RefCount = 0;
+
+
+	pBMContext = (BM_CONTEXT*)hDevMemContext;
+	psDevMemoryInfo = &pBMContext->psDeviceNode->sDevMemoryInfo;
+
+
+	hSyncDevMemHeap = psDevMemoryInfo->psDeviceMemoryHeap[psDevMemoryInfo->ui32SyncHeapID].hDevMemHeap;
+
+
+
+
+	eError = AllocDeviceMem(hDevCookie,
+							hSyncDevMemHeap,
+							PVRSRV_MEM_CACHE_CONSISTENT,
+							sizeof(PVRSRV_SYNC_DATA),
+							sizeof(IMG_UINT32),
+							&psKernelSyncInfo->psSyncDataMemInfoKM);
+
+	if (eError != PVRSRV_OK)
+	{
+
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVAllocSyncInfoKM: Failed to alloc memory"));
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_SYNC_INFO), psKernelSyncInfo, IMG_NULL);
+
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+
+	psKernelSyncInfo->psSyncData = psKernelSyncInfo->psSyncDataMemInfoKM->pvLinAddrKM;
+	psSyncData = psKernelSyncInfo->psSyncData;
+
+	psSyncData->ui32WriteOpsPending = 0;
+	psSyncData->ui32WriteOpsComplete = 0;
+	psSyncData->ui32ReadOpsPending = 0;
+	psSyncData->ui32ReadOpsComplete = 0;
+	psSyncData->ui32LastOpDumpVal = 0;
+	psSyncData->ui32LastReadOpDumpVal = 0;
+
+#if defined(PDUMP)
+	PDUMPMEM(psKernelSyncInfo->psSyncDataMemInfoKM->pvLinAddrKM,
+			psKernelSyncInfo->psSyncDataMemInfoKM,
+			0,
+			psKernelSyncInfo->psSyncDataMemInfoKM->ui32AllocSize,
+			PDUMP_FLAGS_CONTINUOUS,
+			MAKEUNIQUETAG(psKernelSyncInfo->psSyncDataMemInfoKM));
+#endif
+
+	psKernelSyncInfo->sWriteOpsCompleteDevVAddr.uiAddr = psKernelSyncInfo->psSyncDataMemInfoKM->sDevVAddr.uiAddr + offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete);
+	psKernelSyncInfo->sReadOpsCompleteDevVAddr.uiAddr = psKernelSyncInfo->psSyncDataMemInfoKM->sDevVAddr.uiAddr + offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete);
+
+
+	psKernelSyncInfo->psSyncDataMemInfoKM->psKernelSyncInfo = IMG_NULL;
+
+
+	*ppsKernelSyncInfo = psKernelSyncInfo;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeSyncInfoKM(PVRSRV_KERNEL_SYNC_INFO	*psKernelSyncInfo)
+{
+	PVRSRV_ERROR eError;
+
+	if (psKernelSyncInfo->ui32RefCount != 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "oops: sync info ref count not zero at destruction"));
+
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	eError = FreeDeviceMem(psKernelSyncInfo->psSyncDataMemInfoKM);
+	(IMG_VOID)OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_SYNC_INFO), psKernelSyncInfo, IMG_NULL);
+
+
+	return eError;
+}
+
+static IMG_VOID freeWrapped(PVRSRV_KERNEL_MEM_INFO *psMemInfo)
+{
+	IMG_HANDLE hOSWrapMem = psMemInfo->sMemBlk.hOSWrapMem;
+
+
+	if(psMemInfo->sMemBlk.psIntSysPAddr)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(IMG_SYS_PHYADDR), psMemInfo->sMemBlk.psIntSysPAddr, IMG_NULL);
+		psMemInfo->sMemBlk.psIntSysPAddr = IMG_NULL;
+	}
+
+	if(hOSWrapMem)
+	{
+		OSReleasePhysPageAddr(hOSWrapMem);
+	}
+}
+
+static PVRSRV_ERROR FreeMemCallBackCommon(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+										  IMG_UINT32	ui32Param,
+										  IMG_BOOL		bFromAllocator)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+
+	psMemInfo->ui32RefCount--;
+
+
+	if((psMemInfo->ui32Flags & PVRSRV_MEM_EXPORTED) && (bFromAllocator == IMG_TRUE))
+	{
+		IMG_HANDLE hMemInfo = IMG_NULL;
+
+
+		eError = PVRSRVFindHandle(KERNEL_HANDLE_BASE,
+								 &hMemInfo,
+								 psMemInfo,
+								 PVRSRV_HANDLE_TYPE_MEM_INFO);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreeMemCallBackCommon: can't find exported meminfo in the global handle list"));
+			return eError;
+		}
+
+
+		eError = PVRSRVReleaseHandle(KERNEL_HANDLE_BASE,
+									hMemInfo,
+									PVRSRV_HANDLE_TYPE_MEM_INFO);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreeMemCallBackCommon: PVRSRVReleaseHandle failed for exported meminfo"));
+			return eError;
+		}
+	}
+
+
+	if (psMemInfo->ui32RefCount == 0)
+	{
+		switch(psMemInfo->memType)
+		{
+
+			case PVRSRV_MEMTYPE_WRAPPED:
+				freeWrapped(psMemInfo);
+			case PVRSRV_MEMTYPE_DEVICE:
+				if (psMemInfo->psKernelSyncInfo)
+				{
+					psMemInfo->psKernelSyncInfo->ui32RefCount--;
+
+					if (psMemInfo->psKernelSyncInfo->ui32RefCount == 0)
+					{
+						eError = PVRSRVFreeSyncInfoKM(psMemInfo->psKernelSyncInfo);
+					}
+				}
+			case PVRSRV_MEMTYPE_DEVICECLASS:
+				break;
+			default:
+				PVR_DPF((PVR_DBG_ERROR, "FreeMemCallBackCommon: Unknown memType"));
+				return PVRSRV_ERROR_GENERIC;
+		}
+	}
+
+
+	return FreeDeviceMem2(psMemInfo, bFromAllocator);
+}
+
+static PVRSRV_ERROR FreeDeviceMemCallBack(IMG_PVOID pvParam,
+										  IMG_UINT32 ui32Param)
+{
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo = (PVRSRV_KERNEL_MEM_INFO *)pvParam;
+
+	return FreeMemCallBackCommon(psMemInfo, ui32Param, IMG_TRUE);
+}
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFreeDeviceMemKM(IMG_HANDLE				hDevCookie,
+												PVRSRV_KERNEL_MEM_INFO	*psMemInfo)
+{
+	PVRSRV_ERROR eError;
+
+	PVR_UNREFERENCED_PARAMETER(hDevCookie);
+
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if (psMemInfo->sMemBlk.hResItem != IMG_NULL)
+	{
+		eError = ResManFreeResByPtr(psMemInfo->sMemBlk.hResItem);
+	}
+	else
+	{
+
+		eError = FreeDeviceMemCallBack(psMemInfo, 0);
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV _PVRSRVAllocDeviceMemKM(IMG_HANDLE					hDevCookie,
+												 PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												 IMG_HANDLE					hDevMemHeap,
+												 IMG_UINT32					ui32Flags,
+												 IMG_SIZE_T					ui32Size,
+												 IMG_SIZE_T					ui32Alignment,
+												 PVRSRV_KERNEL_MEM_INFO		**ppsMemInfo)
+{
+	PVRSRV_KERNEL_MEM_INFO	*psMemInfo;
+	PVRSRV_ERROR 			eError;
+	BM_HEAP					*psBMHeap;
+	IMG_HANDLE				hDevMemContext;
+
+	if (!hDevMemHeap ||
+		(ui32Size == 0))
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	if (ui32Flags & PVRSRV_HAP_CACHETYPE_MASK)
+	{
+		if (((ui32Size % HOST_PAGESIZE()) != 0) ||
+			((ui32Alignment % HOST_PAGESIZE()) != 0))
+		{
+			return PVRSRV_ERROR_INVALID_PARAMS;
+		}
+	}
+
+	eError = AllocDeviceMem(hDevCookie,
+							hDevMemHeap,
+							ui32Flags,
+							ui32Size,
+							ui32Alignment,
+							&psMemInfo);
+
+	if (eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	if (ui32Flags & PVRSRV_MEM_NO_SYNCOBJ)
+	{
+		psMemInfo->psKernelSyncInfo = IMG_NULL;
+	}
+	else
+	{
+
+
+
+		psBMHeap = (BM_HEAP*)hDevMemHeap;
+		hDevMemContext = (IMG_HANDLE)psBMHeap->pBMContext;
+		eError = PVRSRVAllocSyncInfoKM(hDevCookie,
+									   hDevMemContext,
+									   &psMemInfo->psKernelSyncInfo);
+		if(eError != PVRSRV_OK)
+		{
+			goto free_mainalloc;
+		}
+		psMemInfo->psKernelSyncInfo->ui32RefCount++;
+	}
+
+
+	*ppsMemInfo = psMemInfo;
+
+	if (ui32Flags & PVRSRV_MEM_NO_RESMAN)
+	{
+		psMemInfo->sMemBlk.hResItem = IMG_NULL;
+	}
+	else
+	{
+
+		psMemInfo->sMemBlk.hResItem = ResManRegisterRes(psPerProc->hResManContext,
+														RESMAN_TYPE_DEVICEMEM_ALLOCATION,
+														psMemInfo,
+														0,
+														FreeDeviceMemCallBack);
+		if (psMemInfo->sMemBlk.hResItem == IMG_NULL)
+		{
+
+			eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+			goto free_mainalloc;
+		}
+	}
+
+
+	psMemInfo->ui32RefCount++;
+
+	psMemInfo->memType = PVRSRV_MEMTYPE_DEVICE;
+
+
+	return (PVRSRV_OK);
+
+free_mainalloc:
+	FreeDeviceMem(psMemInfo);
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDissociateDeviceMemKM(IMG_HANDLE              hDevCookie,
+													  PVRSRV_KERNEL_MEM_INFO *psMemInfo)
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_DEVICE_NODE	*psDeviceNode = hDevCookie;
+
+	PVR_UNREFERENCED_PARAMETER(hDevCookie);
+
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	eError = ResManDissociateRes(psMemInfo->sMemBlk.hResItem, psDeviceNode->hResManContext);
+
+	PVR_ASSERT(eError == PVRSRV_OK);
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetFreeDeviceMemKM(IMG_UINT32 ui32Flags,
+												   IMG_SIZE_T *pui32Total,
+												   IMG_SIZE_T *pui32Free,
+												   IMG_SIZE_T *pui32LargestBlock)
+{
+
+
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+	PVR_UNREFERENCED_PARAMETER(pui32Total);
+	PVR_UNREFERENCED_PARAMETER(pui32Free);
+	PVR_UNREFERENCED_PARAMETER(pui32LargestBlock);
+
+	return PVRSRV_OK;
+}
+
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnwrapExtMemoryKM (PVRSRV_KERNEL_MEM_INFO	*psMemInfo)
+{
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	return ResManFreeResByPtr(psMemInfo->sMemBlk.hResItem);
+}
+
+
+static PVRSRV_ERROR UnwrapExtMemoryCallBack(IMG_PVOID	pvParam,
+											IMG_UINT32	ui32Param)
+{
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo = pvParam;
+
+	return FreeMemCallBackCommon(psMemInfo, ui32Param, IMG_TRUE);
+}
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVWrapExtMemoryKM(IMG_HANDLE				hDevCookie,
+												PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												IMG_HANDLE				hDevMemContext,
+												IMG_SIZE_T 				ui32ByteSize,
+												IMG_SIZE_T				ui32PageOffset,
+												IMG_BOOL				bPhysContig,
+												IMG_SYS_PHYADDR	 		*psExtSysPAddr,
+												IMG_VOID 				*pvLinAddr,
+												IMG_UINT32				ui32Flags,
+												PVRSRV_KERNEL_MEM_INFO	**ppsMemInfo)
+{
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo = IMG_NULL;
+	DEVICE_MEMORY_INFO  *psDevMemoryInfo;
+	IMG_SIZE_T			ui32HostPageSize = HOST_PAGESIZE();
+	IMG_HANDLE			hDevMemHeap = IMG_NULL;
+	PVRSRV_DEVICE_NODE* psDeviceNode;
+	BM_HANDLE 			hBuffer;
+	PVRSRV_MEMBLK		*psMemBlock;
+	IMG_BOOL			bBMError;
+	BM_HEAP				*psBMHeap;
+	PVRSRV_ERROR		eError;
+	IMG_VOID 			*pvPageAlignedCPUVAddr;
+	IMG_SYS_PHYADDR	 	*psIntSysPAddr = IMG_NULL;
+	IMG_HANDLE			hOSWrapMem = IMG_NULL;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+	IMG_SIZE_T		ui32PageCount = 0;
+	IMG_UINT32		i;
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)hDevCookie;
+	PVR_ASSERT(psDeviceNode != IMG_NULL);
+
+	if (psDeviceNode == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVWrapExtMemoryKM: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if(pvLinAddr)
+	{
+
+		ui32PageOffset = (IMG_UINTPTR_T)pvLinAddr & (ui32HostPageSize - 1);
+
+
+		ui32PageCount = HOST_PAGEALIGN(ui32ByteSize + ui32PageOffset) / ui32HostPageSize;
+		pvPageAlignedCPUVAddr = (IMG_VOID *)((IMG_UINTPTR_T)pvLinAddr - ui32PageOffset);
+
+
+		if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						ui32PageCount * sizeof(IMG_SYS_PHYADDR),
+						(IMG_VOID **)&psIntSysPAddr, IMG_NULL,
+						"Array of Page Addresses") != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVWrapExtMemoryKM: Failed to alloc memory for block"));
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+
+		eError = OSAcquirePhysPageAddr(pvPageAlignedCPUVAddr,
+										ui32PageCount * ui32HostPageSize,
+										psIntSysPAddr,
+										&hOSWrapMem,
+										(ui32Flags != 0) ? IMG_TRUE : IMG_FALSE);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVWrapExtMemoryKM: Failed to alloc memory for block"));
+			eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+			goto ErrorExitPhase1;
+		}
+
+
+		psExtSysPAddr = psIntSysPAddr;
+
+
+
+		bPhysContig = IMG_FALSE;
+	}
+	else
+	{
+
+	}
+
+
+	psDevMemoryInfo = &((BM_CONTEXT*)hDevMemContext)->psDeviceNode->sDevMemoryInfo;
+	psDeviceMemoryHeap = psDevMemoryInfo->psDeviceMemoryHeap;
+	for(i=0; i<PVRSRV_MAX_CLIENT_HEAPS; i++)
+	{
+		if(HEAP_IDX(psDeviceMemoryHeap[i].ui32HeapID) == psDevMemoryInfo->ui32MappingHeapID)
+		{
+			if(psDeviceMemoryHeap[i].DevMemHeapType == DEVICE_MEMORY_HEAP_PERCONTEXT)
+			{
+
+				hDevMemHeap = BM_CreateHeap(hDevMemContext, &psDeviceMemoryHeap[i]);
+			}
+			else
+			{
+				hDevMemHeap = psDevMemoryInfo->psDeviceMemoryHeap[i].hDevMemHeap;
+			}
+			break;
+		}
+	}
+
+	if(hDevMemHeap == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVWrapExtMemoryKM: unable to find mapping heap"));
+		eError = PVRSRV_ERROR_GENERIC;
+		goto ErrorExitPhase2;
+	}
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					sizeof(PVRSRV_KERNEL_MEM_INFO),
+					(IMG_VOID **)&psMemInfo, IMG_NULL,
+					"Kernel Memory Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVWrapExtMemoryKM: Failed to alloc memory for block"));
+		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+		goto ErrorExitPhase2;
+	}
+
+	OSMemSet(psMemInfo, 0, sizeof(*psMemInfo));
+	psMemInfo->ui32Flags = ui32Flags;
+
+	psMemBlock = &(psMemInfo->sMemBlk);
+
+	bBMError = BM_Wrap(hDevMemHeap,
+					   ui32ByteSize,
+					   ui32PageOffset,
+					   bPhysContig,
+					   psExtSysPAddr,
+					   IMG_NULL,
+					   &psMemInfo->ui32Flags,
+					   &hBuffer);
+	if (!bBMError)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVWrapExtMemoryKM: BM_Wrap Failed"));
+		eError = PVRSRV_ERROR_BAD_MAPPING;
+		goto ErrorExitPhase3;
+	}
+
+
+	psMemBlock->sDevVirtAddr = BM_HandleToDevVaddr(hBuffer);
+	psMemBlock->hOSMemHandle = BM_HandleToOSMemHandle(hBuffer);
+	psMemBlock->hOSWrapMem = hOSWrapMem;
+	psMemBlock->psIntSysPAddr = psIntSysPAddr;
+
+
+	psMemBlock->hBuffer = (IMG_HANDLE)hBuffer;
+
+
+	psMemInfo->pvLinAddrKM = BM_HandleToCpuVaddr(hBuffer);
+	psMemInfo->sDevVAddr = psMemBlock->sDevVirtAddr;
+	psMemInfo->ui32AllocSize = ui32ByteSize;
+
+
+
+	psMemInfo->pvSysBackupBuffer = IMG_NULL;
+
+
+
+
+	psBMHeap = (BM_HEAP*)hDevMemHeap;
+	hDevMemContext = (IMG_HANDLE)psBMHeap->pBMContext;
+	eError = PVRSRVAllocSyncInfoKM(hDevCookie,
+									hDevMemContext,
+									&psMemInfo->psKernelSyncInfo);
+	if(eError != PVRSRV_OK)
+	{
+		goto ErrorExitPhase4;
+	}
+
+	psMemInfo->psKernelSyncInfo->ui32RefCount++;
+
+	psMemInfo->memType = PVRSRV_MEMTYPE_WRAPPED;
+
+
+	psMemInfo->ui32RefCount++;
+
+
+	psMemInfo->sMemBlk.hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													RESMAN_TYPE_DEVICEMEM_WRAP,
+													psMemInfo,
+													0,
+													UnwrapExtMemoryCallBack);
+
+
+	*ppsMemInfo = psMemInfo;
+
+	return PVRSRV_OK;
+
+
+
+ErrorExitPhase4:
+	if(psMemInfo)
+	{
+		FreeDeviceMem(psMemInfo);
+
+
+
+		psMemInfo = IMG_NULL;
+	}
+
+ErrorExitPhase3:
+	if(psMemInfo)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_KERNEL_MEM_INFO), psMemInfo, IMG_NULL);
+
+	}
+
+ErrorExitPhase2:
+	if(psIntSysPAddr)
+	{
+		OSReleasePhysPageAddr(hOSWrapMem);
+	}
+
+ErrorExitPhase1:
+	if(psIntSysPAddr)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, ui32PageCount * sizeof(IMG_SYS_PHYADDR), psIntSysPAddr, IMG_NULL);
+
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapDeviceMemoryKM (PVRSRV_KERNEL_MEM_INFO *psMemInfo)
+{
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	return ResManFreeResByPtr(psMemInfo->sMemBlk.hResItem);
+}
+
+
+static PVRSRV_ERROR UnmapDeviceMemoryCallBack(IMG_PVOID pvParam,
+											  IMG_UINT32 ui32Param)
+{
+	PVRSRV_ERROR				eError;
+	RESMAN_MAP_DEVICE_MEM_DATA	*psMapData = pvParam;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	if(psMapData->psMemInfo->sMemBlk.psIntSysPAddr)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(IMG_SYS_PHYADDR), psMapData->psMemInfo->sMemBlk.psIntSysPAddr, IMG_NULL);
+		psMapData->psMemInfo->sMemBlk.psIntSysPAddr = IMG_NULL;
+	}
+
+	psMapData->psMemInfo->psKernelSyncInfo->ui32RefCount--;
+	if (psMapData->psMemInfo->psKernelSyncInfo->ui32RefCount == 0)
+	{
+		eError = PVRSRVFreeSyncInfoKM(psMapData->psMemInfo->psKernelSyncInfo);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"UnmapDeviceMemoryCallBack: Failed to free sync info"));
+			return eError;
+		}
+	}
+
+	eError = FreeDeviceMem(psMapData->psMemInfo);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"UnmapDeviceMemoryCallBack: Failed to free DST meminfo"));
+		return eError;
+	}
+
+
+	eError = FreeMemCallBackCommon(psMapData->psSrcMemInfo, 0, IMG_FALSE);
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(RESMAN_MAP_DEVICE_MEM_DATA), psMapData, IMG_NULL);
+
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapDeviceMemoryKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+												  PVRSRV_KERNEL_MEM_INFO	*psSrcMemInfo,
+												  IMG_HANDLE				hDstDevMemHeap,
+												  PVRSRV_KERNEL_MEM_INFO	**ppsDstMemInfo)
+{
+	PVRSRV_ERROR				eError;
+	IMG_UINT32					i;
+	IMG_SIZE_T					ui32PageCount, ui32PageOffset;
+	IMG_SIZE_T					ui32HostPageSize = HOST_PAGESIZE();
+	IMG_SYS_PHYADDR				*psSysPAddr = IMG_NULL;
+	IMG_DEV_PHYADDR				sDevPAddr;
+	BM_BUF						*psBuf;
+	IMG_DEV_VIRTADDR			sDevVAddr;
+	PVRSRV_KERNEL_MEM_INFO		*psMemInfo = IMG_NULL;
+	BM_HANDLE 					hBuffer;
+	PVRSRV_MEMBLK				*psMemBlock;
+	IMG_BOOL					bBMError;
+	PVRSRV_DEVICE_NODE			*psDeviceNode;
+	IMG_VOID 					*pvPageAlignedCPUVAddr;
+	RESMAN_MAP_DEVICE_MEM_DATA	*psMapData = IMG_NULL;
+
+
+	if(!psSrcMemInfo || !hDstDevMemHeap || !ppsDstMemInfo)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceMemoryKM: invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	*ppsDstMemInfo = IMG_NULL;
+
+	ui32PageOffset = psSrcMemInfo->sDevVAddr.uiAddr & (ui32HostPageSize - 1);
+	ui32PageCount = HOST_PAGEALIGN(psSrcMemInfo->ui32AllocSize + ui32PageOffset) / ui32HostPageSize;
+	pvPageAlignedCPUVAddr = (IMG_VOID *)(psSrcMemInfo->sDevVAddr.uiAddr - ui32PageOffset);
+
+
+
+
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					ui32PageCount*sizeof(IMG_SYS_PHYADDR),
+					(IMG_VOID **)&psSysPAddr, IMG_NULL,
+					"Array of Page Addresses") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceMemoryKM: Failed to alloc memory for block"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	psBuf = psSrcMemInfo->sMemBlk.hBuffer;
+
+
+	psDeviceNode = psBuf->pMapping->pBMHeap->pBMContext->psDeviceNode;
+
+
+	sDevVAddr.uiAddr = psSrcMemInfo->sDevVAddr.uiAddr - IMG_CAST_TO_DEVVADDR_UINT(ui32PageOffset);
+	for(i=0; i<ui32PageCount; i++)
+	{
+		BM_GetPhysPageAddr(psSrcMemInfo, sDevVAddr, &sDevPAddr);
+
+
+		psSysPAddr[i] = SysDevPAddrToSysPAddr (psDeviceNode->sDevId.eDeviceType, sDevPAddr);
+
+
+		sDevVAddr.uiAddr += IMG_CAST_TO_DEVVADDR_UINT(ui32HostPageSize);
+	}
+
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					sizeof(RESMAN_MAP_DEVICE_MEM_DATA),
+					(IMG_VOID **)&psMapData, IMG_NULL,
+					"Resource Manager Map Data") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceMemoryKM: Failed to alloc resman map data"));
+		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+		goto ErrorExit;
+	}
+
+
+	if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+					sizeof(PVRSRV_KERNEL_MEM_INFO),
+					(IMG_VOID **)&psMemInfo, IMG_NULL,
+					"Kernel Memory Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceMemoryKM: Failed to alloc memory for block"));
+		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+		goto ErrorExit;
+	}
+
+	OSMemSet(psMemInfo, 0, sizeof(*psMemInfo));
+	psMemInfo->ui32Flags = psSrcMemInfo->ui32Flags;
+
+	psMemBlock = &(psMemInfo->sMemBlk);
+
+	bBMError = BM_Wrap(hDstDevMemHeap,
+					   psSrcMemInfo->ui32AllocSize,
+					   ui32PageOffset,
+					   IMG_FALSE,
+					   psSysPAddr,
+					   pvPageAlignedCPUVAddr,
+					   &psMemInfo->ui32Flags,
+					   &hBuffer);
+
+	if (!bBMError)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceMemoryKM: BM_Wrap Failed"));
+		eError = PVRSRV_ERROR_BAD_MAPPING;
+		goto ErrorExit;
+	}
+
+
+	psMemBlock->sDevVirtAddr = BM_HandleToDevVaddr(hBuffer);
+	psMemBlock->hOSMemHandle = BM_HandleToOSMemHandle(hBuffer);
+
+
+	psMemBlock->hBuffer = (IMG_HANDLE)hBuffer;
+
+
+	psMemBlock->psIntSysPAddr = psSysPAddr;
+
+
+	psMemInfo->pvLinAddrKM = psSrcMemInfo->pvLinAddrKM;
+
+
+	psMemInfo->sDevVAddr = psMemBlock->sDevVirtAddr;
+	psMemInfo->ui32AllocSize = psSrcMemInfo->ui32AllocSize;
+	psMemInfo->psKernelSyncInfo = psSrcMemInfo->psKernelSyncInfo;
+
+
+	psMemInfo->psKernelSyncInfo->ui32RefCount++;
+
+
+
+	psMemInfo->pvSysBackupBuffer = IMG_NULL;
+
+
+	psMemInfo->ui32RefCount++;
+
+
+	psSrcMemInfo->ui32RefCount++;
+
+
+	BM_Export(psSrcMemInfo->sMemBlk.hBuffer);
+
+	psMemInfo->memType = PVRSRV_MEMTYPE_MAPPED;
+
+
+	psMapData->psMemInfo = psMemInfo;
+	psMapData->psSrcMemInfo = psSrcMemInfo;
+
+
+	psMemInfo->sMemBlk.hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													RESMAN_TYPE_DEVICEMEM_MAPPING,
+													psMapData,
+													0,
+													UnmapDeviceMemoryCallBack);
+
+	*ppsDstMemInfo = psMemInfo;
+
+	return PVRSRV_OK;
+
+
+
+ErrorExit:
+
+	if(psSysPAddr)
+	{
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(IMG_SYS_PHYADDR), psSysPAddr, IMG_NULL);
+
+	}
+
+	if(psMemInfo)
+	{
+
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_MEM_INFO), psMemInfo, IMG_NULL);
+
+	}
+
+	if(psMapData)
+	{
+
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(RESMAN_MAP_DEVICE_MEM_DATA), psMapData, IMG_NULL);
+
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVUnmapDeviceClassMemoryKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo)
+{
+	if (!psMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	return ResManFreeResByPtr(psMemInfo->sMemBlk.hResItem);
+}
+
+
+static PVRSRV_ERROR UnmapDeviceClassMemoryCallBack(IMG_PVOID	pvParam,
+												   IMG_UINT32	ui32Param)
+{
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo = pvParam;
+
+	return FreeMemCallBackCommon(psMemInfo, ui32Param, IMG_TRUE);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVMapDeviceClassMemoryKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+													   IMG_HANDLE				hDevMemContext,
+													   IMG_HANDLE				hDeviceClassBuffer,
+													   PVRSRV_KERNEL_MEM_INFO	**ppsMemInfo,
+													   IMG_HANDLE				*phOSMapInfo)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_KERNEL_MEM_INFO *psMemInfo;
+	PVRSRV_DEVICECLASS_BUFFER *psDeviceClassBuffer;
+	IMG_SYS_PHYADDR *psSysPAddr;
+	IMG_VOID *pvCPUVAddr, *pvPageAlignedCPUVAddr;
+	IMG_BOOL bPhysContig;
+	BM_CONTEXT *psBMContext;
+	DEVICE_MEMORY_INFO *psDevMemoryInfo;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+	IMG_HANDLE hDevMemHeap = IMG_NULL;
+	IMG_SIZE_T ui32ByteSize;
+	IMG_SIZE_T ui32Offset;
+	IMG_SIZE_T ui32PageSize = HOST_PAGESIZE();
+	BM_HANDLE		hBuffer;
+	PVRSRV_MEMBLK	*psMemBlock;
+	IMG_BOOL		bBMError;
+	IMG_UINT32 i;
+	IMG_BOOL bMapped = IMG_FALSE;
+	
+	if(!hDeviceClassBuffer || !ppsMemInfo || !phOSMapInfo || !hDevMemContext)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceClassMemoryKM: invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psDeviceClassBuffer = (PVRSRV_DEVICECLASS_BUFFER*)hDeviceClassBuffer;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+	eError = psDeviceClassBuffer->pfnGetBufferAddr(psDeviceClassBuffer->hExtDevice,
+												   psDeviceClassBuffer->hExtBuffer,
+												   &psSysPAddr,
+												   &ui32ByteSize,
+												   &pvCPUVAddr,
+												   phOSMapInfo,
+												   &bPhysContig,
+												   &bMapped);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceClassMemoryKM: unable to get buffer address"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	psBMContext = (BM_CONTEXT*)psDeviceClassBuffer->hDevMemContext;
+	psDevMemoryInfo = &psBMContext->psDeviceNode->sDevMemoryInfo;
+	psDeviceMemoryHeap = psDevMemoryInfo->psDeviceMemoryHeap;
+	for(i=0; i<PVRSRV_MAX_CLIENT_HEAPS; i++)
+	{
+		if(HEAP_IDX(psDeviceMemoryHeap[i].ui32HeapID) == psDevMemoryInfo->ui32MappingHeapID)
+		{
+			if(psDeviceMemoryHeap[i].DevMemHeapType == DEVICE_MEMORY_HEAP_PERCONTEXT)
+			{
+
+				hDevMemHeap = BM_CreateHeap(hDevMemContext, &psDeviceMemoryHeap[i]);
+			}
+			else
+			{
+				hDevMemHeap = psDevMemoryInfo->psDeviceMemoryHeap[i].hDevMemHeap;
+			}
+			break;
+		}
+	}
+
+	if(hDevMemHeap == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceClassMemoryKM: unable to find mapping heap"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	ui32Offset = ((IMG_UINTPTR_T)pvCPUVAddr) & (ui32PageSize - 1);
+	pvPageAlignedCPUVAddr = (IMG_VOID *)((IMG_UINTPTR_T)pvCPUVAddr - ui32Offset);
+
+	if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+					sizeof(PVRSRV_KERNEL_MEM_INFO),
+					(IMG_VOID **)&psMemInfo, IMG_NULL,
+					"Kernel Memory Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceClassMemoryKM: Failed to alloc memory for block"));
+		return (PVRSRV_ERROR_OUT_OF_MEMORY);
+	}
+
+	OSMemSet(psMemInfo, 0, sizeof(*psMemInfo));
+
+	psMemBlock = &(psMemInfo->sMemBlk);
+
+	bBMError = BM_Wrap(hDevMemHeap,
+					   ui32ByteSize,
+					   ui32Offset,
+					   bPhysContig,
+					   psSysPAddr,
+					   pvPageAlignedCPUVAddr,
+					   &psMemInfo->ui32Flags,
+					   &hBuffer);
+
+	if (!bBMError)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceClassMemoryKM: BM_Wrap Failed"));
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(PVRSRV_KERNEL_MEM_INFO), psMemInfo, IMG_NULL);
+
+		return PVRSRV_ERROR_BAD_MAPPING;
+	}
+
+
+	psMemBlock->sDevVirtAddr = BM_HandleToDevVaddr(hBuffer);
+	psMemBlock->hOSMemHandle = BM_HandleToOSMemHandle(hBuffer);
+
+
+	psMemBlock->hBuffer = (IMG_HANDLE)hBuffer;
+
+
+
+	psMemInfo->pvLinAddrKM = BM_HandleToCpuVaddr(hBuffer);
+	/* For Buffer Class of Texture Stream. Memory has been already mapped. */
+	if (IMG_FALSE == bMapped) { 
+		psMemInfo->sDevVAddr = psMemBlock->sDevVirtAddr;
+	} else {
+		psMemInfo->sDevVAddr.uiAddr = (IMG_UINT32)(psSysPAddr->uiAddr);
+	}
+	
+	psMemInfo->ui32AllocSize = ui32ByteSize;
+	psMemInfo->psKernelSyncInfo = psDeviceClassBuffer->psKernelSyncInfo;
+
+
+
+	psMemInfo->pvSysBackupBuffer = IMG_NULL;
+
+
+	psMemInfo->sMemBlk.hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													RESMAN_TYPE_DEVICECLASSMEM_MAPPING,
+													psMemInfo,
+													0,
+													UnmapDeviceClassMemoryCallBack);
+
+	psMemInfo->ui32RefCount++;
+
+	psMemInfo->memType = PVRSRV_MEMTYPE_DEVICECLASS;
+
+
+	*ppsMemInfo = psMemInfo;
+
+	return PVRSRV_OK;
+}
+
+
+/*
+ * PVRSRVGetPageListKM()
+ *
+ * This is an EMGD addition to PVR services.  Given a PowerVR meminfo,
+ * returns a list of pages for that allocation.  This can then be used
+ * by EMGD code for things like mapping into the GTT if the surface
+ * is going to be displayed.
+ *
+ * Note that the page list returned is the live page list and should
+ * not be modified or freed by the caller.
+ */
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetPageListKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+	struct page ***pvPageList,
+	unsigned long *numpages,
+	unsigned long *offset)
+{
+	LinuxMemArea *ma;
+	unsigned long skippages, total_offset;
+
+	/* Sanity check the parameters */
+	if (!psMemInfo || !pvPageList || !numpages) {
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVMapDeviceMemoryKM: invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	ma = (LinuxMemArea*)psMemInfo->sMemBlk.hOSMemHandle;
+
+	/* # of pages isn't stored; needs to be calculated from number of bytes */
+	*numpages = (ma->ui32ByteSize + 4095) / 4096;
+
+	/*
+	 * What type of memarea is this?  We can handle ALLOC_PAGES, or SUB_ALLOC
+	 * areas whose earliest ancestor is an ALLOC_PAGES.
+	 */
+	switch (ma->eAreaType) {
+	case LINUX_MEM_AREA_ALLOC_PAGES:
+		*pvPageList = ma->uData.sPageList.pvPageList;
+		*offset = 0;
+		break;
+	case LINUX_MEM_AREA_SUB_ALLOC:
+		/*
+		 * This allocation may be a subarea of a larger allocation.  We'll need to
+		 * figure out the details of the parent allocation first so that we can
+		 * calculate our subset of the page list and appropriate offset into the
+		 * first page.
+		 */
+		total_offset = 0;
+		while (ma->eAreaType == LINUX_MEM_AREA_SUB_ALLOC) {
+			total_offset += ma->uData.sSubAlloc.ui32ByteOffset;
+			ma = ma->uData.sSubAlloc.psParentLinuxMemArea;
+		}
+
+		/*
+		 * We should now have the original ALLOC_PAGES memarea.  Make sure
+		 * it's actually the type we expect.
+		 */
+		if (ma->eAreaType != LINUX_MEM_AREA_ALLOC_PAGES) {
+			PVR_DPF((PVR_DBG_ERROR,
+				"PVRSRVGetPageListKM: meminfo for suballocation did not "
+				"originate from a a page-based allocation (type=%d)",
+				ma->eAreaType));
+			*numpages = 0;
+			return PVRSRV_ERROR_GENERIC;
+		}
+
+		/*
+		 * After taking all nested suballocations into account, figure out
+		 * where in the page list the allocation we care about really starts.
+		 */
+		skippages = total_offset / 4096;
+		*offset = total_offset % 4096;
+		*pvPageList = &(ma->uData.sPageList.pvPageList)[skippages];
+
+		break;
+
+	default:
+		PVR_DPF((PVR_DBG_ERROR,
+			"PVRSRVGetPageListKM: meminfo not a page-based allocation or sub-allocation (type=%d)",
+			ma->eAreaType));
+		*numpages = 0;
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/handle.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/handle.c
new file mode 100644
index 0000000..bfbd6b3
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/handle.c
@@ -0,0 +1,1545 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifdef	PVR_SECURE_HANDLES
+#include <stddef.h>
+
+#include "services_headers.h"
+#include "handle.h"
+
+#ifdef	DEBUG
+#define	HANDLE_BLOCK_SIZE	1
+#else
+#define	HANDLE_BLOCK_SIZE	256
+#endif
+
+#define	HANDLE_HASH_TAB_INIT_SIZE	32
+
+#define	DEFAULT_MAX_INDEX_PLUS_ONE	0xfffffffful
+#define	DEFAULT_MAX_HANDLE		DEFAULT_MAX_INDEX_PLUS_ONE
+
+#define	INDEX_IS_VALID(psBase, i) ((i) < (psBase)->ui32TotalHandCount)
+
+#define	INDEX_TO_HANDLE(psBase, idx) ((IMG_HANDLE)((idx) + 1))
+#define	HANDLE_TO_INDEX(psBase, hand) ((IMG_UINT32)(hand) - 1)
+
+#define INDEX_TO_HANDLE_PTR(psBase, i) (((psBase)->psHandleArray) + (i))
+#define	HANDLE_TO_HANDLE_PTR(psBase, h) (INDEX_TO_HANDLE_PTR(psBase, HANDLE_TO_INDEX(psBase, h)))
+
+#define	HANDLE_PTR_TO_INDEX(psBase, psHandle) (IMG_UINT32)((psHandle) - ((psBase)->psHandleArray))
+#define	HANDLE_PTR_TO_HANDLE(psBase, psHandle) \
+	INDEX_TO_HANDLE(psBase, HANDLE_PTR_TO_INDEX(psBase, psHandle))
+
+#define	ROUND_UP_TO_MULTIPLE(a, b) ((((a) + (b) - 1) / (b)) * (b))
+
+#define	HANDLES_BATCHED(psBase) ((psBase)->ui32HandBatchSize != 0)
+
+#define	SET_FLAG(v, f) ((IMG_VOID)((v) |= (f)))
+#define	CLEAR_FLAG(v, f) ((IMG_VOID)((v) &= ~(f)))
+#define	TEST_FLAG(v, f) ((IMG_BOOL)(((v) & (f)) != 0))
+
+#define	TEST_ALLOC_FLAG(psHandle, f) TEST_FLAG((psHandle)->eFlag, f)
+
+#define	SET_INTERNAL_FLAG(psHandle, f) SET_FLAG((psHandle)->eInternalFlag, f)
+#define	CLEAR_INTERNAL_FLAG(psHandle, f) CLEAR_FLAG((psHandle)->eInternalFlag, f)
+#define	TEST_INTERNAL_FLAG(psHandle, f) TEST_FLAG((psHandle)->eInternalFlag, f)
+
+#define	BATCHED_HANDLE(psHandle) TEST_INTERNAL_FLAG(psHandle, INTERNAL_HANDLE_FLAG_BATCHED)
+
+#define	SET_BATCHED_HANDLE(psHandle) SET_INTERNAL_FLAG(psHandle, INTERNAL_HANDLE_FLAG_BATCHED)
+
+#define	SET_UNBATCHED_HANDLE(psHandle) CLEAR_INTERNAL_FLAG(psHandle, INTERNAL_HANDLE_FLAG_BATCHED)
+
+#define	BATCHED_HANDLE_PARTIALLY_FREE(psHandle) TEST_INTERNAL_FLAG(psHandle, INTERNAL_HANDLE_FLAG_BATCHED_PARTIALLY_FREE)
+
+#define SET_BATCHED_HANDLE_PARTIALLY_FREE(psHandle) SET_INTERNAL_FLAG(psHandle, INTERNAL_HANDLE_FLAG_BATCHED_PARTIALLY_FREE)
+
+#define	HANDLE_STRUCT_IS_FREE(psHandle) ((psHandle)->eType == PVRSRV_HANDLE_TYPE_NONE && (psHandle)->eInternalFlag == INTERNAL_HANDLE_FLAG_NONE)
+
+#ifdef	MIN
+#undef MIN
+#endif
+
+#define	MIN(x, y) (((x) < (y)) ? (x) : (y))
+
+struct sHandleList
+{
+	IMG_UINT32 ui32Prev;
+	IMG_UINT32 ui32Next;
+	IMG_HANDLE hParent;
+};
+
+enum ePVRSRVInternalHandleFlag
+{
+	INTERNAL_HANDLE_FLAG_NONE = 0x00,
+	INTERNAL_HANDLE_FLAG_BATCHED = 0x01,
+	INTERNAL_HANDLE_FLAG_BATCHED_PARTIALLY_FREE = 0x02,
+};
+
+struct sHandle
+{
+
+	PVRSRV_HANDLE_TYPE eType;
+
+
+	IMG_VOID *pvData;
+
+
+	IMG_UINT32 ui32NextIndexPlusOne;
+
+
+	enum ePVRSRVInternalHandleFlag eInternalFlag;
+
+
+	PVRSRV_HANDLE_ALLOC_FLAG eFlag;
+
+
+	IMG_UINT32 ui32Index;
+
+
+	struct sHandleList sChildren;
+
+
+	struct sHandleList sSiblings;
+};
+
+struct _PVRSRV_HANDLE_BASE_
+{
+
+	IMG_HANDLE hBaseBlockAlloc;
+
+
+	IMG_HANDLE hHandBlockAlloc;
+
+
+	struct sHandle *psHandleArray;
+
+
+	HASH_TABLE *psHashTab;
+
+
+	IMG_UINT32 ui32FreeHandCount;
+
+
+	IMG_UINT32 ui32FirstFreeIndex;
+
+
+	IMG_UINT32 ui32MaxIndexPlusOne;
+
+
+	IMG_UINT32 ui32TotalHandCount;
+
+
+	IMG_UINT32 ui32LastFreeIndexPlusOne;
+
+
+	IMG_UINT32 ui32HandBatchSize;
+
+
+	IMG_UINT32 ui32TotalHandCountPreBatch;
+
+
+	IMG_UINT32 ui32FirstBatchIndexPlusOne;
+
+
+	IMG_UINT32 ui32BatchHandAllocFailures;
+
+
+	IMG_BOOL bPurgingEnabled;
+};
+
+enum eHandKey {
+	HAND_KEY_DATA = 0,
+	HAND_KEY_TYPE,
+	HAND_KEY_PARENT,
+	HAND_KEY_LEN
+};
+
+PVRSRV_HANDLE_BASE *gpsKernelHandleBase = IMG_NULL;
+
+typedef IMG_UINTPTR_T HAND_KEY[HAND_KEY_LEN];
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(HandleListInit)
+#endif
+static INLINE
+IMG_VOID HandleListInit(IMG_UINT32 ui32Index, struct sHandleList *psList, IMG_HANDLE hParent)
+{
+	psList->ui32Next = ui32Index;
+	psList->ui32Prev = ui32Index;
+	psList->hParent = hParent;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(InitParentList)
+#endif
+static INLINE
+IMG_VOID InitParentList(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psHandle)
+{
+	IMG_UINT32 ui32Parent = HANDLE_PTR_TO_INDEX(psBase, psHandle);
+
+	HandleListInit(ui32Parent, &psHandle->sChildren, INDEX_TO_HANDLE(psBase, ui32Parent));
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(InitChildEntry)
+#endif
+static INLINE
+IMG_VOID InitChildEntry(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psHandle)
+{
+	HandleListInit(HANDLE_PTR_TO_INDEX(psBase, psHandle), &psHandle->sSiblings, IMG_NULL);
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(HandleListIsEmpty)
+#endif
+static INLINE
+IMG_BOOL HandleListIsEmpty(IMG_UINT32 ui32Index, struct sHandleList *psList)
+{
+	IMG_BOOL bIsEmpty;
+
+	bIsEmpty = (IMG_BOOL)(psList->ui32Next == ui32Index);
+
+#ifdef	DEBUG
+	{
+		IMG_BOOL bIsEmpty2;
+
+		bIsEmpty2 = (IMG_BOOL)(psList->ui32Prev == ui32Index);
+		PVR_ASSERT(bIsEmpty == bIsEmpty2);
+	}
+#endif
+
+	return bIsEmpty;
+}
+
+#ifdef DEBUG
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(NoChildren)
+#endif
+static INLINE
+IMG_BOOL NoChildren(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psHandle)
+{
+	PVR_ASSERT(psHandle->sChildren.hParent == HANDLE_PTR_TO_HANDLE(psBase, psHandle));
+
+	return HandleListIsEmpty(HANDLE_PTR_TO_INDEX(psBase, psHandle), &psHandle->sChildren);
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(NoParent)
+#endif
+static INLINE
+IMG_BOOL NoParent(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psHandle)
+{
+	if (HandleListIsEmpty(HANDLE_PTR_TO_INDEX(psBase, psHandle), &psHandle->sSiblings))
+	{
+		PVR_ASSERT(psHandle->sSiblings.hParent == IMG_NULL);
+
+		return IMG_TRUE;
+	}
+	else
+	{
+		PVR_ASSERT(psHandle->sSiblings.hParent != IMG_NULL);
+	}
+	return IMG_FALSE;
+}
+#endif
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(ParentHandle)
+#endif
+static INLINE
+IMG_HANDLE ParentHandle(struct sHandle *psHandle)
+{
+	return psHandle->sSiblings.hParent;
+}
+
+#define	LIST_PTR_FROM_INDEX_AND_OFFSET(psBase, i, p, po, eo) \
+		((struct sHandleList *)((IMG_CHAR *)(INDEX_TO_HANDLE_PTR(psBase, i)) + (((i) == (p)) ? (po) : (eo))))
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(HandleListInsertBefore)
+#endif
+static INLINE
+IMG_VOID HandleListInsertBefore(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32InsIndex, struct sHandleList *psIns, IMG_SIZE_T uiParentOffset, IMG_UINT32 ui32EntryIndex, struct sHandleList *psEntry, IMG_SIZE_T uiEntryOffset, IMG_UINT32 ui32ParentIndex)
+{
+
+	struct sHandleList *psPrevIns = LIST_PTR_FROM_INDEX_AND_OFFSET(psBase, psIns->ui32Prev, ui32ParentIndex, uiParentOffset, uiEntryOffset);
+
+	PVR_ASSERT(psEntry->hParent == IMG_NULL);
+	PVR_ASSERT(ui32InsIndex == psPrevIns->ui32Next);
+	PVR_ASSERT(LIST_PTR_FROM_INDEX_AND_OFFSET(psBase, ui32ParentIndex, ui32ParentIndex, uiParentOffset, uiParentOffset)->hParent == INDEX_TO_HANDLE(psBase, ui32ParentIndex));
+
+	psEntry->ui32Prev = psIns->ui32Prev;
+	psIns->ui32Prev = ui32EntryIndex;
+	psEntry->ui32Next = ui32InsIndex;
+	psPrevIns->ui32Next = ui32EntryIndex;
+
+	psEntry->hParent = INDEX_TO_HANDLE(psBase, ui32ParentIndex);
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(AdoptChild)
+#endif
+static INLINE
+IMG_VOID AdoptChild(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psParent, struct sHandle *psChild)
+{
+	IMG_UINT32 ui32Parent = HANDLE_TO_INDEX(psBase, psParent->sChildren.hParent);
+
+	PVR_ASSERT(ui32Parent == HANDLE_PTR_TO_INDEX(psBase, psParent));
+
+	HandleListInsertBefore(psBase, ui32Parent, &psParent->sChildren, offsetof(struct sHandle, sChildren), HANDLE_PTR_TO_INDEX(psBase, psChild), &psChild->sSiblings, offsetof(struct sHandle, sSiblings), ui32Parent);
+
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(HandleListRemove)
+#endif
+static INLINE
+IMG_VOID HandleListRemove(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32EntryIndex, struct sHandleList *psEntry, IMG_SIZE_T uiEntryOffset, IMG_SIZE_T uiParentOffset)
+{
+	if (!HandleListIsEmpty(ui32EntryIndex, psEntry))
+	{
+
+		struct sHandleList *psPrev = LIST_PTR_FROM_INDEX_AND_OFFSET(psBase, psEntry->ui32Prev, HANDLE_TO_INDEX(psBase, psEntry->hParent), uiParentOffset, uiEntryOffset);
+		struct sHandleList *psNext = LIST_PTR_FROM_INDEX_AND_OFFSET(psBase, psEntry->ui32Next, HANDLE_TO_INDEX(psBase, psEntry->hParent), uiParentOffset, uiEntryOffset);
+
+
+		PVR_ASSERT(psEntry->hParent != IMG_NULL);
+
+		psPrev->ui32Next = psEntry->ui32Next;
+		psNext->ui32Prev = psEntry->ui32Prev;
+
+		HandleListInit(ui32EntryIndex, psEntry, IMG_NULL);
+	}
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(UnlinkFromParent)
+#endif
+static INLINE
+IMG_VOID UnlinkFromParent(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psHandle)
+{
+	HandleListRemove(psBase, HANDLE_PTR_TO_INDEX(psBase, psHandle), &psHandle->sSiblings, offsetof(struct sHandle, sSiblings), offsetof(struct sHandle, sChildren));
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(HandleListIterate)
+#endif
+static INLINE
+PVRSRV_ERROR HandleListIterate(PVRSRV_HANDLE_BASE *psBase, struct sHandleList *psHead, IMG_SIZE_T uiParentOffset, IMG_SIZE_T uiEntryOffset, PVRSRV_ERROR (*pfnIterFunc)(PVRSRV_HANDLE_BASE *, struct sHandle *))
+{
+	IMG_UINT32 ui32Index;
+	IMG_UINT32 ui32Parent = HANDLE_TO_INDEX(psBase, psHead->hParent);
+
+	PVR_ASSERT(psHead->hParent != IMG_NULL);
+
+
+	for(ui32Index = psHead->ui32Next; ui32Index != ui32Parent; )
+	{
+		struct sHandle *psHandle = INDEX_TO_HANDLE_PTR(psBase, ui32Index);
+
+		struct sHandleList *psEntry = LIST_PTR_FROM_INDEX_AND_OFFSET(psBase, ui32Index, ui32Parent, uiParentOffset, uiEntryOffset);
+		PVRSRV_ERROR eError;
+
+		PVR_ASSERT(psEntry->hParent == psHead->hParent);
+
+		ui32Index = psEntry->ui32Next;
+
+		eError = (*pfnIterFunc)(psBase, psHandle);
+		if (eError != PVRSRV_OK)
+		{
+			return eError;
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(IterateOverChildren)
+#endif
+static INLINE
+PVRSRV_ERROR IterateOverChildren(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psParent, PVRSRV_ERROR (*pfnIterFunc)(PVRSRV_HANDLE_BASE *, struct sHandle *))
+{
+	 return HandleListIterate(psBase, &psParent->sChildren, offsetof(struct sHandle, sChildren), offsetof(struct sHandle, sSiblings), pfnIterFunc);
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(GetHandleStructure)
+#endif
+static INLINE
+PVRSRV_ERROR GetHandleStructure(PVRSRV_HANDLE_BASE *psBase, struct sHandle **ppsHandle, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	IMG_UINT32 ui32Index = HANDLE_TO_INDEX(psBase, hHandle);
+	struct sHandle *psHandle;
+
+
+	if (!INDEX_IS_VALID(psBase, ui32Index))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "GetHandleStructure: Handle index out of range (%u >= %u)", ui32Index, psBase->ui32TotalHandCount));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	psHandle =  INDEX_TO_HANDLE_PTR(psBase, ui32Index);
+	if (psHandle->eType == PVRSRV_HANDLE_TYPE_NONE)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "GetHandleStructure: Handle not allocated (index: %u)", ui32Index));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	if (eType != PVRSRV_HANDLE_TYPE_NONE && eType != psHandle->eType)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "GetHandleStructure: Handle type mismatch (%d != %d)", eType, psHandle->eType));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	*ppsHandle = psHandle;
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(ParentIfPrivate)
+#endif
+static INLINE
+IMG_HANDLE ParentIfPrivate(struct sHandle *psHandle)
+{
+	return TEST_ALLOC_FLAG(psHandle, PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE) ?
+			ParentHandle(psHandle) : IMG_NULL;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(InitKey)
+#endif
+static INLINE
+IMG_VOID InitKey(HAND_KEY aKey, PVRSRV_HANDLE_BASE *psBase, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, IMG_HANDLE hParent)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	aKey[HAND_KEY_DATA] = (IMG_UINTPTR_T)pvData;
+	aKey[HAND_KEY_TYPE] = (IMG_UINTPTR_T)eType;
+	aKey[HAND_KEY_PARENT] = (IMG_UINTPTR_T)hParent;
+}
+
+static PVRSRV_ERROR FreeHandleArray(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	if (psBase->psHandleArray != IMG_NULL)
+	{
+		eError = OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			psBase->ui32TotalHandCount * sizeof(struct sHandle),
+			psBase->psHandleArray,
+			psBase->hHandBlockAlloc);
+
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreeHandleArray: Error freeing memory (%d)", eError));
+		}
+		else
+		{
+			psBase->psHandleArray = IMG_NULL;
+		}
+	}
+
+	return eError;
+}
+
+static PVRSRV_ERROR FreeHandle(PVRSRV_HANDLE_BASE *psBase, struct sHandle *psHandle)
+{
+	HAND_KEY aKey;
+	IMG_UINT32 ui32Index = HANDLE_PTR_TO_INDEX(psBase, psHandle);
+	PVRSRV_ERROR eError;
+
+
+	InitKey(aKey, psBase, psHandle->pvData, psHandle->eType, ParentIfPrivate(psHandle));
+
+	if (!TEST_ALLOC_FLAG(psHandle, PVRSRV_HANDLE_ALLOC_FLAG_MULTI) && !BATCHED_HANDLE_PARTIALLY_FREE(psHandle))
+	{
+		IMG_HANDLE hHandle;
+		hHandle = (IMG_HANDLE) HASH_Remove_Extended(psBase->psHashTab, aKey);
+
+		PVR_ASSERT(hHandle != IMG_NULL);
+		PVR_ASSERT(hHandle == INDEX_TO_HANDLE(psBase, ui32Index));
+		PVR_UNREFERENCED_PARAMETER(hHandle);
+	}
+
+
+	UnlinkFromParent(psBase, psHandle);
+
+
+	eError = IterateOverChildren(psBase, psHandle, FreeHandle);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreeHandle: Error whilst freeing subhandles (%d)", eError));
+		return eError;
+	}
+
+
+	psHandle->eType = PVRSRV_HANDLE_TYPE_NONE;
+
+	if (BATCHED_HANDLE(psHandle) && !BATCHED_HANDLE_PARTIALLY_FREE(psHandle))
+	{
+
+		SET_BATCHED_HANDLE_PARTIALLY_FREE(psHandle);
+
+		return PVRSRV_OK;
+	}
+
+
+	if (!psBase->bPurgingEnabled)
+	{
+		if (psBase->ui32FreeHandCount == 0)
+		{
+			PVR_ASSERT(psBase->ui32FirstFreeIndex == 0);
+			PVR_ASSERT(psBase->ui32LastFreeIndexPlusOne == 0);
+
+			psBase->ui32FirstFreeIndex =  ui32Index;
+		}
+		else
+		{
+
+			PVR_ASSERT(psBase->ui32LastFreeIndexPlusOne != 0);
+			PVR_ASSERT(INDEX_TO_HANDLE_PTR(psBase, psBase->ui32LastFreeIndexPlusOne - 1)->ui32NextIndexPlusOne == 0);
+			INDEX_TO_HANDLE_PTR(psBase, psBase->ui32LastFreeIndexPlusOne - 1)->ui32NextIndexPlusOne =  ui32Index + 1;
+		}
+
+		PVR_ASSERT(psHandle->ui32NextIndexPlusOne == 0);
+
+
+		psBase->ui32LastFreeIndexPlusOne = ui32Index + 1;
+	}
+
+	psBase->ui32FreeHandCount++;
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR FreeAllHandles(PVRSRV_HANDLE_BASE *psBase)
+{
+	IMG_UINT32 i;
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	if (psBase->ui32FreeHandCount == psBase->ui32TotalHandCount)
+	{
+		return eError;
+	}
+
+	for (i = 0; i < psBase->ui32TotalHandCount; i++)
+	{
+		struct sHandle *psHandle;
+
+		psHandle = INDEX_TO_HANDLE_PTR(psBase, i);
+
+		if (psHandle->eType != PVRSRV_HANDLE_TYPE_NONE)
+		{
+			eError = FreeHandle(psBase, psHandle);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "FreeAllHandles: FreeHandle failed (%d)", eError));
+				break;
+			}
+
+
+			if (psBase->ui32FreeHandCount == psBase->ui32TotalHandCount)
+			{
+				break;
+			}
+		}
+	}
+
+	return eError;
+}
+
+static PVRSRV_ERROR FreeHandleBase(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVRSRV_ERROR eError;
+
+	if (HANDLES_BATCHED(psBase))
+	{
+		PVR_DPF((PVR_DBG_WARNING, "FreeHandleBase: Uncommitted/Unreleased handle batch"));
+		PVRSRVReleaseHandleBatch(psBase);
+	}
+
+
+	eError = FreeAllHandles(psBase);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreeHandleBase: Couldn't free handles (%d)", eError));
+		return eError;
+	}
+
+
+	eError = FreeHandleArray(psBase);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreeHandleBase: Couldn't free handle array (%d)", eError));
+		return eError;
+	}
+
+	if (psBase->psHashTab != IMG_NULL)
+	{
+
+		HASH_Delete(psBase->psHashTab);
+	}
+
+	eError = OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+		sizeof(*psBase),
+		psBase,
+		psBase->hBaseBlockAlloc);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreeHandleBase: Couldn't free handle base (%d)", eError));
+		return eError;
+	}
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(FindHandle)
+#endif
+static INLINE
+IMG_HANDLE FindHandle(PVRSRV_HANDLE_BASE *psBase, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, IMG_HANDLE hParent)
+{
+	HAND_KEY aKey;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	InitKey(aKey, psBase, pvData, eType, hParent);
+
+	return (IMG_HANDLE) HASH_Retrieve_Extended(psBase->psHashTab, aKey);
+}
+
+static PVRSRV_ERROR ReallocMem(IMG_PVOID *ppvMem, IMG_HANDLE *phBlockAlloc, IMG_UINT32 ui32NewSize, IMG_UINT32 ui32OldSize)
+{
+	IMG_VOID *pvOldMem = *ppvMem;
+	IMG_HANDLE hOldBlockAlloc = *phBlockAlloc;
+	IMG_UINT32 ui32CopySize = MIN(ui32NewSize, ui32OldSize);
+	IMG_VOID *pvNewMem = IMG_NULL;
+	IMG_HANDLE hNewBlockAlloc = IMG_NULL;
+	PVRSRV_ERROR eError;
+
+	if (ui32NewSize == ui32OldSize)
+	{
+		return (PVRSRV_OK);
+	}
+
+	if (ui32NewSize != 0)
+	{
+
+		eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+			ui32NewSize,
+			&pvNewMem,
+			&hNewBlockAlloc,
+			"Memory Area");
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "ReallocMem: Couldn't allocate new memory area (%d)", eError));
+			return eError;
+		}
+	}
+
+	if (ui32CopySize != 0)
+	{
+
+		OSMemCopy(pvNewMem, pvOldMem, ui32CopySize);
+	}
+
+	if (ui32OldSize != 0)
+	{
+
+		eError = OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				ui32OldSize,
+				pvOldMem,
+				hOldBlockAlloc);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "ReallocMem: Couldn't free old memory area (%d)", eError));
+		}
+	}
+
+	*ppvMem = pvNewMem;
+	*phBlockAlloc = hNewBlockAlloc;
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(ReallocHandleArray)
+#endif
+static INLINE
+PVRSRV_ERROR ReallocHandleArray(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32NewCount, IMG_UINT32 ui32OldCount)
+{
+	return ReallocMem((IMG_PVOID *)&psBase->psHandleArray,
+				&psBase->hHandBlockAlloc,
+				ui32NewCount * sizeof(struct sHandle),
+				ui32OldCount * sizeof(struct sHandle));
+}
+
+static PVRSRV_ERROR IncreaseHandleArraySize(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32Delta)
+{
+	PVRSRV_ERROR eError;
+	struct sHandle *psHandle;
+	IMG_UINT32 ui32DeltaAdjusted = ROUND_UP_TO_MULTIPLE(ui32Delta, HANDLE_BLOCK_SIZE);
+	IMG_UINT32 ui32NewTotalHandCount = psBase->ui32TotalHandCount + ui32DeltaAdjusted;
+;
+
+	PVR_ASSERT(ui32Delta != 0);
+
+
+	if (ui32NewTotalHandCount > psBase->ui32MaxIndexPlusOne || ui32NewTotalHandCount <= psBase->ui32TotalHandCount)
+	{
+		ui32NewTotalHandCount = psBase->ui32MaxIndexPlusOne;
+
+		ui32DeltaAdjusted = ui32NewTotalHandCount - psBase->ui32TotalHandCount;
+
+		if (ui32DeltaAdjusted < ui32Delta)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "IncreaseHandleArraySize: Maximum handle limit reached (%d)", psBase->ui32MaxIndexPlusOne));
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+	}
+
+	PVR_ASSERT(ui32DeltaAdjusted >= ui32Delta);
+
+
+	eError = ReallocHandleArray(psBase, ui32NewTotalHandCount, psBase->ui32TotalHandCount);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "IncreaseHandleArraySize: ReallocHandleArray failed (%d)", eError));
+		return eError;
+	}
+
+
+	for(psHandle = psBase->psHandleArray + psBase->ui32TotalHandCount;
+		psHandle < psBase->psHandleArray + ui32NewTotalHandCount;
+		psHandle++)
+	{
+		psHandle->eType = PVRSRV_HANDLE_TYPE_NONE;
+		psHandle->eInternalFlag = INTERNAL_HANDLE_FLAG_NONE;
+		psHandle->ui32NextIndexPlusOne  = 0;
+	}
+
+
+	psBase->ui32FreeHandCount += ui32DeltaAdjusted;
+
+	if (psBase->ui32FirstFreeIndex == 0)
+	{
+		PVR_ASSERT(psBase->ui32LastFreeIndexPlusOne == 0);
+
+		psBase->ui32FirstFreeIndex = psBase->ui32TotalHandCount;
+	}
+	else
+	{
+		if (!psBase->bPurgingEnabled)
+		{
+			PVR_ASSERT(psBase->ui32LastFreeIndexPlusOne != 0)
+			PVR_ASSERT(INDEX_TO_HANDLE_PTR(psBase, psBase->ui32LastFreeIndexPlusOne - 1)->ui32NextIndexPlusOne == 0);
+
+			INDEX_TO_HANDLE_PTR(psBase, psBase->ui32LastFreeIndexPlusOne - 1)->ui32NextIndexPlusOne = psBase->ui32TotalHandCount + 1;
+		}
+	}
+
+	if (!psBase->bPurgingEnabled)
+	{
+		psBase->ui32LastFreeIndexPlusOne = ui32NewTotalHandCount;
+	}
+
+	psBase->ui32TotalHandCount = ui32NewTotalHandCount;
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR EnsureFreeHandles(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32Free)
+{
+	PVRSRV_ERROR eError;
+
+	if (ui32Free > psBase->ui32FreeHandCount)
+	{
+		IMG_UINT32 ui32FreeHandDelta = ui32Free - psBase->ui32FreeHandCount;
+		eError = IncreaseHandleArraySize(psBase, ui32FreeHandDelta);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "EnsureFreeHandles: Couldn't allocate %u handles to ensure %u free handles (IncreaseHandleArraySize failed with error %d)", ui32FreeHandDelta, ui32Free, eError));
+
+			return eError;
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR AllocHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag, IMG_HANDLE hParent)
+{
+	IMG_UINT32 ui32NewIndex;
+	struct sHandle *psNewHandle = IMG_NULL;
+	IMG_HANDLE hHandle;
+	HAND_KEY aKey;
+	PVRSRV_ERROR eError;
+
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	PVR_ASSERT(psBase->psHashTab != IMG_NULL);
+
+	if (!TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_MULTI))
+	{
+
+		PVR_ASSERT(FindHandle(psBase, pvData, eType, hParent) == IMG_NULL);
+	}
+
+	if (psBase->ui32FreeHandCount == 0 && HANDLES_BATCHED(psBase))
+	{
+		 PVR_DPF((PVR_DBG_WARNING, "AllocHandle: Handle batch size (%u) was too small, allocating additional space", psBase->ui32HandBatchSize));
+	}
+
+
+	eError = EnsureFreeHandles(psBase, 1);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "AllocHandle: EnsureFreeHandles failed (%d)", eError));
+		return eError;
+	}
+	PVR_ASSERT(psBase->ui32FreeHandCount != 0)
+
+	if (!psBase->bPurgingEnabled)
+	{
+
+		ui32NewIndex = psBase->ui32FirstFreeIndex;
+
+
+		psNewHandle = INDEX_TO_HANDLE_PTR(psBase, ui32NewIndex);
+	}
+	else
+	{
+
+		for(ui32NewIndex = psBase->ui32FirstFreeIndex; ui32NewIndex < psBase->ui32TotalHandCount; ui32NewIndex++)
+		{
+			psNewHandle = INDEX_TO_HANDLE_PTR(psBase, ui32NewIndex);
+			if (HANDLE_STRUCT_IS_FREE(psNewHandle))
+			{
+				break;
+			}
+
+		}
+		psBase->ui32FirstFreeIndex = 0;
+		PVR_ASSERT(ui32NewIndex < psBase->ui32TotalHandCount);
+	}
+	PVR_ASSERT(psNewHandle != IMG_NULL);
+
+
+	hHandle = INDEX_TO_HANDLE(psBase, ui32NewIndex);
+
+
+	if (!TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_MULTI))
+	{
+
+		InitKey(aKey, psBase, pvData, eType, hParent);
+
+
+		if (!HASH_Insert_Extended(psBase->psHashTab, aKey, (IMG_UINTPTR_T)hHandle))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "AllocHandle: Couldn't add handle to hash table"));
+
+			return PVRSRV_ERROR_GENERIC;
+		}
+	}
+
+	psBase->ui32FreeHandCount--;
+
+
+	if (!psBase->bPurgingEnabled)
+	{
+
+		if (psBase->ui32FreeHandCount == 0)
+		{
+			PVR_ASSERT(psBase->ui32FirstFreeIndex == ui32NewIndex);
+			PVR_ASSERT(psBase->ui32LastFreeIndexPlusOne == (ui32NewIndex + 1));
+
+			psBase->ui32LastFreeIndexPlusOne = 0;
+			psBase->ui32FirstFreeIndex = 0;
+		}
+		else
+		{
+
+			psBase->ui32FirstFreeIndex = (psNewHandle->ui32NextIndexPlusOne == 0) ?
+				ui32NewIndex + 1 :
+				psNewHandle->ui32NextIndexPlusOne - 1;
+		}
+	}
+
+
+	psNewHandle->eType = eType;
+	psNewHandle->pvData = pvData;
+	psNewHandle->eInternalFlag = INTERNAL_HANDLE_FLAG_NONE;
+	psNewHandle->eFlag = eFlag;
+	psNewHandle->ui32Index = ui32NewIndex;
+
+	InitParentList(psBase, psNewHandle);
+#if defined(DEBUG)
+	PVR_ASSERT(NoChildren(psBase, psNewHandle));
+#endif
+
+	InitChildEntry(psBase, psNewHandle);
+#if defined(DEBUG)
+	PVR_ASSERT(NoParent(psBase, psNewHandle));
+#endif
+
+	if (HANDLES_BATCHED(psBase))
+	{
+
+		psNewHandle->ui32NextIndexPlusOne = psBase->ui32FirstBatchIndexPlusOne;
+
+		psBase->ui32FirstBatchIndexPlusOne = ui32NewIndex + 1;
+
+
+		SET_BATCHED_HANDLE(psNewHandle);
+	}
+	else
+	{
+		psNewHandle->ui32NextIndexPlusOne = 0;
+	}
+
+
+	*phHandle = hHandle;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVAllocHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag)
+{
+	IMG_HANDLE hHandle;
+	PVRSRV_ERROR eError;
+
+	*phHandle = IMG_NULL;
+
+	if (HANDLES_BATCHED(psBase))
+	{
+
+		psBase->ui32BatchHandAllocFailures++;
+	}
+
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	if (!TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_MULTI))
+	{
+
+		hHandle = FindHandle(psBase, pvData, eType, IMG_NULL);
+		if (hHandle != IMG_NULL)
+		{
+			struct sHandle *psHandle;
+
+			eError = GetHandleStructure(psBase, &psHandle, hHandle, eType);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocHandle: Lookup of existing handle failed"));
+				return eError;
+			}
+
+
+			if (TEST_FLAG(psHandle->eFlag & eFlag, PVRSRV_HANDLE_ALLOC_FLAG_SHARED))
+			{
+				*phHandle = hHandle;
+				eError = PVRSRV_OK;
+				goto exit_ok;
+			}
+			return PVRSRV_ERROR_GENERIC;
+		}
+	}
+
+	eError = AllocHandle(psBase, phHandle, pvData, eType, eFlag, IMG_NULL);
+
+exit_ok:
+	if (HANDLES_BATCHED(psBase) && (eError == PVRSRV_OK))
+	{
+		psBase->ui32BatchHandAllocFailures--;
+	}
+
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVAllocSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag, IMG_HANDLE hParent)
+{
+	struct sHandle *psPHand;
+	struct sHandle *psCHand;
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hParentKey;
+	IMG_HANDLE hHandle;
+
+	*phHandle = IMG_NULL;
+
+	if (HANDLES_BATCHED(psBase))
+	{
+
+		psBase->ui32BatchHandAllocFailures++;
+	}
+
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	hParentKey = TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE) ?
+			hParent : IMG_NULL;
+
+
+	eError = GetHandleStructure(psBase, &psPHand, hParent, PVRSRV_HANDLE_TYPE_NONE);
+	if (eError != PVRSRV_OK)
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	if (!TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_MULTI))
+	{
+
+		hHandle = FindHandle(psBase, pvData, eType, hParentKey);
+		if (hHandle != IMG_NULL)
+		{
+			struct sHandle *psCHandle;
+			PVRSRV_ERROR eErr;
+
+			eErr = GetHandleStructure(psBase, &psCHandle, hHandle, eType);
+			if (eErr != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Lookup of existing handle failed"));
+				return eErr;
+			}
+
+			PVR_ASSERT(hParentKey != IMG_NULL && ParentHandle(HANDLE_TO_HANDLE_PTR(psBase, hHandle)) == hParent);
+
+
+			if (TEST_FLAG(psCHandle->eFlag & eFlag, PVRSRV_HANDLE_ALLOC_FLAG_SHARED) && ParentHandle(HANDLE_TO_HANDLE_PTR(psBase, hHandle)) == hParent)
+			{
+				*phHandle = hHandle;
+				goto exit_ok;
+			}
+			return PVRSRV_ERROR_GENERIC;
+		}
+	}
+
+	eError = AllocHandle(psBase, &hHandle, pvData, eType, eFlag, hParentKey);
+	if (eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+
+	psPHand = HANDLE_TO_HANDLE_PTR(psBase, hParent);
+
+	psCHand = HANDLE_TO_HANDLE_PTR(psBase, hHandle);
+
+	AdoptChild(psBase, psPHand, psCHand);
+
+	*phHandle = hHandle;
+
+exit_ok:
+	if (HANDLES_BATCHED(psBase))
+	{
+		psBase->ui32BatchHandAllocFailures--;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVFindHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType)
+{
+	IMG_HANDLE hHandle;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+
+	hHandle = (IMG_HANDLE) FindHandle(psBase, pvData, eType, IMG_NULL);
+	if (hHandle == IMG_NULL)
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	*phHandle = hHandle;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVLookupHandleAnyType(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, PVRSRV_HANDLE_TYPE *peType, IMG_HANDLE hHandle)
+{
+	struct sHandle *psHandle;
+	PVRSRV_ERROR eError;
+
+	eError = GetHandleStructure(psBase, &psHandle, hHandle, PVRSRV_HANDLE_TYPE_NONE);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVLookupHandleAnyType: Error looking up handle (%d)", eError));
+		return eError;
+	}
+
+	*ppvData = psHandle->pvData;
+	*peType = psHandle->eType;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVLookupHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	struct sHandle *psHandle;
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	eError = GetHandleStructure(psBase, &psHandle, hHandle, eType);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVLookupHandle: Error looking up handle (%d)", eError));
+		return eError;
+	}
+
+	*ppvData = psHandle->pvData;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVLookupSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType, IMG_HANDLE hAncestor)
+{
+	struct sHandle *psPHand;
+	struct sHandle *psCHand;
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	eError = GetHandleStructure(psBase, &psCHand, hHandle, eType);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVLookupSubHandle: Error looking up subhandle (%d)", eError));
+		return eError;
+	}
+
+
+	for (psPHand = psCHand; ParentHandle(psPHand) != hAncestor; )
+	{
+		eError = GetHandleStructure(psBase, &psPHand, ParentHandle(psPHand), PVRSRV_HANDLE_TYPE_NONE);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVLookupSubHandle: Subhandle doesn't belong to given ancestor"));
+			return PVRSRV_ERROR_GENERIC;
+		}
+	}
+
+	*ppvData = psCHand->pvData;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVGetParentHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *phParent, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	struct sHandle *psHandle;
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	eError = GetHandleStructure(psBase, &psHandle, hHandle, eType);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVGetParentHandle: Error looking up subhandle (%d)", eError));
+		return eError;
+	}
+
+	*phParent = ParentHandle(psHandle);
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVLookupAndReleaseHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	struct sHandle *psHandle;
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	eError = GetHandleStructure(psBase, &psHandle, hHandle, eType);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVLookupAndReleaseHandle: Error looking up handle (%d)", eError));
+		return eError;
+	}
+
+	*ppvData = psHandle->pvData;
+
+	eError = FreeHandle(psBase, psHandle);
+
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVReleaseHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	struct sHandle *psHandle;
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE);
+
+	eError = GetHandleStructure(psBase, &psHandle, hHandle, eType);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVReleaseHandle: Error looking up handle (%d)", eError));
+		return eError;
+	}
+
+	eError = FreeHandle(psBase, psHandle);
+
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVNewHandleBatch(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32BatchSize)
+{
+	PVRSRV_ERROR eError;
+
+	if (HANDLES_BATCHED(psBase))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVNewHandleBatch: There is a handle batch already in use (size %u)", psBase->ui32HandBatchSize));
+		return  PVRSRV_ERROR_GENERIC;
+	}
+
+	if (ui32BatchSize == 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVNewHandleBatch: Invalid batch size (%u)", ui32BatchSize));
+		return  PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	eError = EnsureFreeHandles(psBase, ui32BatchSize);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVNewHandleBatch: EnsureFreeHandles failed (error %d)", eError));
+		return eError;
+	}
+
+	psBase->ui32HandBatchSize = ui32BatchSize;
+
+
+	psBase->ui32TotalHandCountPreBatch = psBase->ui32TotalHandCount;
+
+	PVR_ASSERT(psBase->ui32BatchHandAllocFailures == 0);
+
+	PVR_ASSERT(psBase->ui32FirstBatchIndexPlusOne == 0);
+
+	PVR_ASSERT(HANDLES_BATCHED(psBase));
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR PVRSRVHandleBatchCommitOrRelease(PVRSRV_HANDLE_BASE *psBase, IMG_BOOL bCommit)
+{
+
+	IMG_UINT32 ui32IndexPlusOne;
+	IMG_BOOL bCommitBatch = bCommit;
+
+	if (!HANDLES_BATCHED(psBase))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVHandleBatchCommitOrRelease: There is no handle batch"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+
+	}
+
+	if (psBase->ui32BatchHandAllocFailures != 0)
+	{
+		if (bCommit)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVHandleBatchCommitOrRelease: Attempting to commit batch with handle allocation failures."));
+		}
+		bCommitBatch = IMG_FALSE;
+	}
+
+	PVR_ASSERT(psBase->ui32BatchHandAllocFailures == 0 || !bCommit);
+
+	ui32IndexPlusOne = psBase->ui32FirstBatchIndexPlusOne;
+	while(ui32IndexPlusOne != 0)
+	{
+		struct sHandle *psHandle = INDEX_TO_HANDLE_PTR(psBase, ui32IndexPlusOne - 1);
+		IMG_UINT32 ui32NextIndexPlusOne = psHandle->ui32NextIndexPlusOne;
+		PVR_ASSERT(BATCHED_HANDLE(psHandle));
+
+		psHandle->ui32NextIndexPlusOne = 0;
+
+		if (!bCommitBatch || BATCHED_HANDLE_PARTIALLY_FREE(psHandle))
+		{
+			PVRSRV_ERROR eError;
+
+
+			if (!BATCHED_HANDLE_PARTIALLY_FREE(psHandle))
+			{
+				SET_UNBATCHED_HANDLE(psHandle);
+			}
+
+			eError = FreeHandle(psBase, psHandle);
+			if (eError != PVRSRV_OK)
+			{
+				 PVR_DPF((PVR_DBG_ERROR, "PVRSRVHandleBatchCommitOrRelease: Error freeing handle (%d)", eError));
+			}
+			PVR_ASSERT(eError == PVRSRV_OK);
+		}
+		else
+		{
+			SET_UNBATCHED_HANDLE(psHandle);
+		}
+
+		ui32IndexPlusOne = ui32NextIndexPlusOne;
+	}
+
+#ifdef DEBUG
+	if (psBase->ui32TotalHandCountPreBatch != psBase->ui32TotalHandCount)
+	{
+		IMG_UINT32 ui32Delta = psBase->ui32TotalHandCount - psBase->ui32TotalHandCountPreBatch;
+
+		PVR_ASSERT(psBase->ui32TotalHandCount > psBase->ui32TotalHandCountPreBatch);
+
+		PVR_DPF((PVR_DBG_WARNING, "PVRSRVHandleBatchCommitOrRelease: The batch size was too small.  Batch size was %u, but needs to be %u", psBase->ui32HandBatchSize,  psBase->ui32HandBatchSize + ui32Delta));
+
+	}
+#endif
+
+	psBase->ui32HandBatchSize = 0;
+	psBase->ui32FirstBatchIndexPlusOne = 0;
+	psBase->ui32TotalHandCountPreBatch = 0;
+	psBase->ui32BatchHandAllocFailures = 0;
+
+	if (psBase->ui32BatchHandAllocFailures != 0 && bCommit)
+	{
+		PVR_ASSERT(!bCommitBatch);
+
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVCommitHandleBatch(PVRSRV_HANDLE_BASE *psBase)
+{
+	return PVRSRVHandleBatchCommitOrRelease(psBase, IMG_TRUE);
+}
+
+IMG_VOID PVRSRVReleaseHandleBatch(PVRSRV_HANDLE_BASE *psBase)
+{
+	(IMG_VOID) PVRSRVHandleBatchCommitOrRelease(psBase, IMG_FALSE);
+}
+
+PVRSRV_ERROR PVRSRVSetMaxHandle(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32MaxHandle)
+{
+	if (HANDLES_BATCHED(psBase))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVSetMaxHandle: Limit cannot be set whilst in batch mode"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	if (ui32MaxHandle == 0 || ui32MaxHandle >= DEFAULT_MAX_HANDLE)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVSetMaxHandle: Limit must be between %u and %u, inclusive", 0, DEFAULT_MAX_HANDLE));
+
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	if (psBase->ui32TotalHandCount != 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVSetMaxHandle: Limit cannot be set becuase handles have already been allocated"));
+
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psBase->ui32MaxIndexPlusOne = ui32MaxHandle;
+
+	return PVRSRV_OK;
+}
+
+IMG_UINT32 PVRSRVGetMaxHandle(PVRSRV_HANDLE_BASE *psBase)
+{
+	return psBase->ui32MaxIndexPlusOne;
+}
+
+PVRSRV_ERROR PVRSRVEnableHandlePurging(PVRSRV_HANDLE_BASE *psBase)
+{
+	if (psBase->bPurgingEnabled)
+	{
+		PVR_DPF((PVR_DBG_WARNING, "PVRSRVEnableHandlePurging: Purging already enabled"));
+		return PVRSRV_OK;
+	}
+
+
+	if (psBase->ui32TotalHandCount != 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVEnableHandlePurging: Handles have already been allocated"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psBase->bPurgingEnabled = IMG_TRUE;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVPurgeHandles(PVRSRV_HANDLE_BASE *psBase)
+{
+	IMG_UINT32 ui32Handle;
+	IMG_UINT32 ui32NewHandCount;
+
+	if (!psBase->bPurgingEnabled)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVPurgeHandles: Purging not enabled for this handle base"));
+		return PVRSRV_ERROR_NOT_SUPPORTED;
+	}
+
+	if (HANDLES_BATCHED(psBase))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVPurgeHandles: Purging not allowed whilst in batch mode"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	for (ui32Handle = psBase->ui32TotalHandCount; ui32Handle != 0; ui32Handle--)
+	{
+		struct sHandle *psHandle = HANDLE_TO_HANDLE_PTR(psBase, ui32Handle);
+		if (!HANDLE_STRUCT_IS_FREE(psHandle))
+		{
+			break;
+		}
+	}
+
+	ui32NewHandCount = ROUND_UP_TO_MULTIPLE(ui32Handle, HANDLE_BLOCK_SIZE);
+
+
+	if (ui32NewHandCount >= ui32Handle && ui32NewHandCount <= (psBase->ui32TotalHandCount/2))
+	{
+		IMG_UINT32 ui32Delta = psBase->ui32TotalHandCount - ui32NewHandCount;
+		PVRSRV_ERROR eError;
+
+
+
+		eError = ReallocHandleArray(psBase, ui32NewHandCount, psBase->ui32TotalHandCount);
+		if (eError != PVRSRV_OK)
+		{
+			return eError;
+		}
+
+
+		psBase->ui32TotalHandCount = ui32NewHandCount;
+		psBase->ui32FreeHandCount -= ui32Delta;
+		psBase->ui32FirstFreeIndex = 0;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVAllocHandleBase(PVRSRV_HANDLE_BASE **ppsBase)
+{
+	PVRSRV_HANDLE_BASE *psBase;
+	IMG_HANDLE hBlockAlloc;
+	PVRSRV_ERROR eError;
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+		sizeof(*psBase),
+		(IMG_PVOID *)&psBase,
+		&hBlockAlloc,
+		"Handle Base");
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocHandleBase: Couldn't allocate handle base (%d)", eError));
+		return eError;
+	}
+	OSMemSet(psBase, 0, sizeof(*psBase));
+
+
+	psBase->psHashTab = HASH_Create_Extended(HANDLE_HASH_TAB_INIT_SIZE, sizeof(HAND_KEY), HASH_Func_Default, HASH_Key_Comp_Default);
+	if (psBase->psHashTab == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocHandleBase: Couldn't create data pointer hash table\n"));
+		goto failure;
+	}
+
+	psBase->hBaseBlockAlloc = hBlockAlloc;
+
+	psBase->ui32MaxIndexPlusOne = DEFAULT_MAX_INDEX_PLUS_ONE;
+
+	*ppsBase = psBase;
+
+	return PVRSRV_OK;
+failure:
+	(IMG_VOID)PVRSRVFreeHandleBase(psBase);
+	return PVRSRV_ERROR_GENERIC;
+}
+
+PVRSRV_ERROR PVRSRVFreeHandleBase(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(psBase != gpsKernelHandleBase);
+
+	eError = FreeHandleBase(psBase);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVFreeHandleBase: FreeHandleBase failed (%d)", eError));
+	}
+
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVHandleInit(IMG_VOID)
+{
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(gpsKernelHandleBase == IMG_NULL);
+
+	eError = PVRSRVAllocHandleBase(&gpsKernelHandleBase);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVHandleInit: PVRSRVAllocHandleBase failed (%d)", eError));
+		goto error;
+	}
+
+	eError = PVRSRVEnableHandlePurging(gpsKernelHandleBase);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVHandleInit: PVRSRVEnableHandlePurging failed (%d)", eError));
+		goto error;
+	}
+
+	return PVRSRV_OK;
+error:
+	(IMG_VOID) PVRSRVHandleDeInit();
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVHandleDeInit(IMG_VOID)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	if (gpsKernelHandleBase != IMG_NULL)
+	{
+		eError = FreeHandleBase(gpsKernelHandleBase);
+		if (eError == PVRSRV_OK)
+		{
+			gpsKernelHandleBase = IMG_NULL;
+		}
+		else
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVHandleDeInit: FreeHandleBase failed (%d)", eError));
+		}
+	}
+
+	return eError;
+}
+#else
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/hash.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/hash.c
new file mode 100644
index 0000000..981702b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/hash.c
@@ -0,0 +1,459 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "pvr_debug.h"
+#include "img_defs.h"
+#include "services.h"
+#include "servicesint.h"
+#include "hash.h"
+#include "osfunc.h"
+
+#define PRIVATE_MAX(a,b) ((a)>(b)?(a):(b))
+
+#define	KEY_TO_INDEX(pHash, key, uSize) \
+	((pHash)->pfnHashFunc((pHash)->uKeySize, key, uSize) % uSize)
+
+#define	KEY_COMPARE(pHash, pKey1, pKey2) \
+	((pHash)->pfnKeyComp((pHash)->uKeySize, pKey1, pKey2))
+
+struct _BUCKET_
+{
+
+	struct _BUCKET_ *pNext;
+
+
+	IMG_UINTPTR_T v;
+
+
+	IMG_UINTPTR_T k[];
+};
+typedef struct _BUCKET_ BUCKET;
+
+struct _HASH_TABLE_
+{
+
+	BUCKET **ppBucketTable;
+
+
+	IMG_UINT32 uSize;
+
+
+	IMG_UINT32 uCount;
+
+
+	IMG_UINT32 uMinimumSize;
+
+
+	IMG_UINT32 uKeySize;
+
+
+	HASH_FUNC *pfnHashFunc;
+
+
+	HASH_KEY_COMP *pfnKeyComp;
+};
+
+IMG_UINT32
+HASH_Func_Default (IMG_SIZE_T uKeySize, IMG_VOID *pKey, IMG_UINT32 uHashTabLen)
+{
+	IMG_UINTPTR_T *p = (IMG_UINTPTR_T *)pKey;
+	IMG_UINT32 uKeyLen = uKeySize / sizeof(IMG_UINTPTR_T);
+	IMG_UINT32 ui;
+	IMG_UINT32 uHashKey = 0;
+
+	PVR_UNREFERENCED_PARAMETER(uHashTabLen);
+
+	PVR_ASSERT((uKeySize % sizeof(IMG_UINTPTR_T)) == 0);
+
+	for (ui = 0; ui < uKeyLen; ui++)
+	{
+		IMG_UINT32 uHashPart = (IMG_UINT32)*p++;
+
+		uHashPart += (uHashPart << 12);
+		uHashPart ^= (uHashPart >> 22);
+		uHashPart += (uHashPart << 4);
+		uHashPart ^= (uHashPart >> 9);
+		uHashPart += (uHashPart << 10);
+		uHashPart ^= (uHashPart >> 2);
+		uHashPart += (uHashPart << 7);
+		uHashPart ^= (uHashPart >> 12);
+
+		uHashKey += uHashPart;
+	}
+
+	return uHashKey;
+}
+
+IMG_BOOL
+HASH_Key_Comp_Default (IMG_SIZE_T uKeySize, IMG_VOID *pKey1, IMG_VOID *pKey2)
+{
+	IMG_UINTPTR_T *p1 = (IMG_UINTPTR_T *)pKey1;
+	IMG_UINTPTR_T *p2 = (IMG_UINTPTR_T *)pKey2;
+	IMG_UINT32 uKeyLen = uKeySize / sizeof(IMG_UINTPTR_T);
+	IMG_UINT32 ui;
+
+	PVR_ASSERT((uKeySize % sizeof(IMG_UINTPTR_T)) == 0);
+
+	for (ui = 0; ui < uKeyLen; ui++)
+	{
+		if (*p1++ != *p2++)
+			return IMG_FALSE;
+	}
+
+	return IMG_TRUE;
+}
+
+static PVRSRV_ERROR
+_ChainInsert (HASH_TABLE *pHash, BUCKET *pBucket, BUCKET **ppBucketTable, IMG_UINT32 uSize)
+{
+	IMG_UINT32 uIndex;
+
+	PVR_ASSERT (pBucket != IMG_NULL);
+	PVR_ASSERT (ppBucketTable != IMG_NULL);
+	PVR_ASSERT (uSize != 0);
+
+	if ((pBucket == IMG_NULL) || (ppBucketTable == IMG_NULL) || (uSize == 0))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "_ChainInsert: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	uIndex = KEY_TO_INDEX(pHash, pBucket->k, uSize);
+	pBucket->pNext = ppBucketTable[uIndex];
+	ppBucketTable[uIndex] = pBucket;
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR
+_Rehash (HASH_TABLE *pHash,
+	 BUCKET **ppOldTable, IMG_UINT32 uOldSize,
+         BUCKET **ppNewTable, IMG_UINT32 uNewSize)
+{
+	IMG_UINT32 uIndex;
+	for (uIndex=0; uIndex< uOldSize; uIndex++)
+    {
+		BUCKET *pBucket;
+		pBucket = ppOldTable[uIndex];
+		while (pBucket != IMG_NULL)
+		{
+			BUCKET *pNextBucket = pBucket->pNext;
+			if (_ChainInsert (pHash, pBucket, ppNewTable, uNewSize) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "_Rehash: call to _ChainInsert failed"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+			pBucket = pNextBucket;
+		}
+    }
+	return PVRSRV_OK;
+}
+
+static IMG_BOOL
+_Resize (HASH_TABLE *pHash, IMG_UINT32 uNewSize)
+{
+	if (uNewSize != pHash->uSize)
+    {
+		BUCKET **ppNewTable;
+        IMG_UINT32 uIndex;
+
+		PVR_DPF ((PVR_DBG_MESSAGE,
+                  "HASH_Resize: oldsize=0x%x  newsize=0x%x  count=0x%x",
+				pHash->uSize, uNewSize, pHash->uCount));
+
+		OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+                      sizeof (BUCKET *) * uNewSize,
+                      (IMG_PVOID*)&ppNewTable, IMG_NULL,
+					  "Hash Table Buckets");
+		if (ppNewTable == IMG_NULL)
+            return IMG_FALSE;
+
+        for (uIndex=0; uIndex<uNewSize; uIndex++)
+            ppNewTable[uIndex] = IMG_NULL;
+
+        if (_Rehash (pHash, pHash->ppBucketTable, pHash->uSize, ppNewTable, uNewSize) != PVRSRV_OK)
+		{
+			return IMG_FALSE;
+		}
+
+        OSFreeMem (PVRSRV_PAGEABLE_SELECT, sizeof(BUCKET *)*pHash->uSize, pHash->ppBucketTable, IMG_NULL);
+
+        pHash->ppBucketTable = ppNewTable;
+        pHash->uSize = uNewSize;
+    }
+    return IMG_TRUE;
+}
+
+
+HASH_TABLE * HASH_Create_Extended (IMG_UINT32 uInitialLen, IMG_SIZE_T uKeySize, HASH_FUNC *pfnHashFunc, HASH_KEY_COMP *pfnKeyComp)
+{
+	HASH_TABLE *pHash;
+	IMG_UINT32 uIndex;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Create_Extended: InitialSize=0x%x", uInitialLen));
+
+	if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+					sizeof(HASH_TABLE),
+					(IMG_VOID **)&pHash, IMG_NULL,
+					"Hash Table") != PVRSRV_OK)
+	{
+		return IMG_NULL;
+	}
+
+	pHash->uCount = 0;
+	pHash->uSize = uInitialLen;
+	pHash->uMinimumSize = uInitialLen;
+	pHash->uKeySize = uKeySize;
+	pHash->pfnHashFunc = pfnHashFunc;
+	pHash->pfnKeyComp = pfnKeyComp;
+
+	OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+                  sizeof (BUCKET *) * pHash->uSize,
+                  (IMG_PVOID*)&pHash->ppBucketTable, IMG_NULL,
+				  "Hash Table Buckets");
+
+	if (pHash->ppBucketTable == IMG_NULL)
+    {
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(HASH_TABLE), pHash, IMG_NULL);
+
+		return IMG_NULL;
+    }
+
+	for (uIndex=0; uIndex<pHash->uSize; uIndex++)
+		pHash->ppBucketTable[uIndex] = IMG_NULL;
+	return pHash;
+}
+
+HASH_TABLE * HASH_Create (IMG_UINT32 uInitialLen)
+{
+	return HASH_Create_Extended(uInitialLen, sizeof(IMG_UINTPTR_T),
+		&HASH_Func_Default, &HASH_Key_Comp_Default);
+}
+
+IMG_VOID
+HASH_Delete (HASH_TABLE *pHash)
+{
+	if (pHash != IMG_NULL)
+    {
+		PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Delete"));
+
+		PVR_ASSERT (pHash->uCount==0);
+		if(pHash->uCount != 0)
+		{
+			PVR_DPF ((PVR_DBG_ERROR, "HASH_Delete: leak detected in hash table!"));
+			PVR_DPF ((PVR_DBG_ERROR, "Likely Cause: client drivers not freeing alocations before destroying devmemcontext"));
+		}
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(BUCKET *)*pHash->uSize, pHash->ppBucketTable, IMG_NULL);
+		pHash->ppBucketTable = IMG_NULL;
+		OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(HASH_TABLE), pHash, IMG_NULL);
+
+    }
+}
+
+IMG_BOOL
+HASH_Insert_Extended (HASH_TABLE *pHash, IMG_VOID *pKey, IMG_UINTPTR_T v)
+{
+	BUCKET *pBucket;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+              "HASH_Insert_Extended: Hash=%08X, pKey=%08X, v=0x%x", pHash, pKey, v));
+
+	PVR_ASSERT (pHash != IMG_NULL);
+
+	if (pHash == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "HASH_Insert_Extended: invalid parameter"));
+		return IMG_FALSE;
+	}
+
+	if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
+					sizeof(BUCKET) + pHash->uKeySize,
+					(IMG_VOID **)&pBucket, IMG_NULL,
+					"Hash Table entry") != PVRSRV_OK)
+	{
+		return IMG_FALSE;
+	}
+
+	pBucket->v = v;
+
+	OSMemCopy(pBucket->k, pKey, pHash->uKeySize);
+	if (_ChainInsert (pHash, pBucket, pHash->ppBucketTable, pHash->uSize) != PVRSRV_OK)
+	{
+		return IMG_FALSE;
+	}
+
+	pHash->uCount++;
+
+
+	if (pHash->uCount << 1 > pHash->uSize)
+    {
+
+
+        _Resize (pHash, pHash->uSize << 1);
+    }
+
+
+	return IMG_TRUE;
+}
+
+IMG_BOOL
+HASH_Insert (HASH_TABLE *pHash, IMG_UINTPTR_T k, IMG_UINTPTR_T v)
+{
+	PVR_DPF ((PVR_DBG_MESSAGE,
+              "HASH_Insert: Hash=%08X, k=0x%x, v=0x%x", pHash, k, v));
+
+	return HASH_Insert_Extended(pHash, &k, v);
+}
+
+IMG_UINTPTR_T
+HASH_Remove_Extended(HASH_TABLE *pHash, IMG_VOID *pKey)
+{
+	BUCKET **ppBucket;
+	IMG_UINT32 uIndex;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Remove_Extended: Hash=%08X, pKey=%08X", pHash, pKey));
+
+	PVR_ASSERT (pHash != IMG_NULL);
+
+	if (pHash == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "HASH_Remove_Extended: Null hash table"));
+		return 0;
+	}
+
+	uIndex = KEY_TO_INDEX(pHash, pKey, pHash->uSize);
+
+	for (ppBucket = &(pHash->ppBucketTable[uIndex]); *ppBucket != IMG_NULL; ppBucket = &((*ppBucket)->pNext))
+	{
+
+		if (KEY_COMPARE(pHash, (*ppBucket)->k, pKey))
+		{
+			BUCKET *pBucket = *ppBucket;
+			IMG_UINTPTR_T v = pBucket->v;
+			(*ppBucket) = pBucket->pNext;
+
+			OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(BUCKET) + pHash->uKeySize, pBucket, IMG_NULL);
+
+
+			pHash->uCount--;
+
+
+			if (pHash->uSize > (pHash->uCount << 2) &&
+                pHash->uSize > pHash->uMinimumSize)
+            {
+
+
+				_Resize (pHash,
+                         PRIVATE_MAX (pHash->uSize >> 1,
+                                      pHash->uMinimumSize));
+            }
+
+			PVR_DPF ((PVR_DBG_MESSAGE,
+                      "HASH_Remove_Extended: Hash=%08X, pKey=%08X = 0x%x",
+                      pHash, pKey, v));
+			return v;
+		}
+	}
+	PVR_DPF ((PVR_DBG_MESSAGE,
+              "HASH_Remove_Extended: Hash=%08X, pKey=%08X = 0x0 !!!!", pHash, pKey));
+	return 0;
+}
+
+IMG_UINTPTR_T
+HASH_Remove (HASH_TABLE *pHash, IMG_UINTPTR_T k)
+{
+	PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Remove: Hash=%08X, k=0x%x", pHash, k));
+
+	return HASH_Remove_Extended(pHash, &k);
+}
+
+IMG_UINTPTR_T
+HASH_Retrieve_Extended (HASH_TABLE *pHash, IMG_VOID *pKey)
+{
+	BUCKET **ppBucket;
+	IMG_UINT32 uIndex;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Retrieve_Extended: Hash=%08X, pKey=%08X", pHash,pKey));
+
+	PVR_ASSERT (pHash != IMG_NULL);
+
+	if (pHash == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "HASH_Retrieve_Extended: Null hash table"));
+		return 0;
+	}
+
+	uIndex = KEY_TO_INDEX(pHash, pKey, pHash->uSize);
+
+	for (ppBucket = &(pHash->ppBucketTable[uIndex]); *ppBucket != IMG_NULL; ppBucket = &((*ppBucket)->pNext))
+	{
+
+		if (KEY_COMPARE(pHash, (*ppBucket)->k, pKey))
+		{
+			BUCKET *pBucket = *ppBucket;
+			IMG_UINTPTR_T v = pBucket->v;
+
+			PVR_DPF ((PVR_DBG_MESSAGE,
+                      "HASH_Retrieve: Hash=%08X, pKey=%08X = 0x%x",
+                      pHash, pKey, v));
+			return v;
+		}
+	}
+	PVR_DPF ((PVR_DBG_MESSAGE,
+              "HASH_Retrieve: Hash=%08X, pKey=%08X = 0x0 !!!!", pHash, pKey));
+	return 0;
+}
+
+IMG_UINTPTR_T
+HASH_Retrieve (HASH_TABLE *pHash, IMG_UINTPTR_T k)
+{
+	PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Retrieve: Hash=%08X, k=0x%x", pHash,k));
+	return HASH_Retrieve_Extended(pHash, &k);
+}
+
+#ifdef HASH_TRACE
+IMG_VOID
+HASH_Dump (HASH_TABLE *pHash)
+{
+	IMG_UINT32 uIndex;
+	IMG_UINT32 uMaxLength=0;
+	IMG_UINT32 uEmptyCount=0;
+
+	PVR_ASSERT (pHash != IMG_NULL);
+	for (uIndex=0; uIndex<pHash->uSize; uIndex++)
+	{
+		BUCKET *pBucket;
+		IMG_UINT32 uLength = 0;
+		if (pHash->ppBucketTable[uIndex] == IMG_NULL)
+			uEmptyCount++;
+		for (pBucket=pHash->ppBucketTable[uIndex];
+		pBucket != IMG_NULL;
+		pBucket = pBucket->pNext)
+				uLength++;
+		uMaxLength = PRIVATE_MAX (uMaxLength, uLength);
+	}
+
+	PVR_TRACE(("hash table: uMinimumSize=%d  size=%d  count=%d",
+			pHash->uMinimumSize, pHash->uSize, pHash->uCount));
+	PVR_TRACE(("  empty=%d  max=%d", uEmptyCount, uMaxLength));
+}
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/lists.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/lists.c
new file mode 100644
index 0000000..1d7602a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/lists.c
@@ -0,0 +1,95 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "lists.h"
+#include "services_headers.h"
+
+IMPLEMENT_LIST_ANY_VA(BM_HEAP)
+IMPLEMENT_LIST_ANY_2(BM_HEAP, PVRSRV_ERROR, PVRSRV_OK)
+IMPLEMENT_LIST_ANY_VA_2(BM_HEAP, PVRSRV_ERROR, PVRSRV_OK)
+IMPLEMENT_LIST_FOR_EACH_VA(BM_HEAP)
+IMPLEMENT_LIST_REMOVE(BM_HEAP)
+IMPLEMENT_LIST_INSERT(BM_HEAP)
+
+IMPLEMENT_LIST_ANY_VA(BM_CONTEXT)
+IMPLEMENT_LIST_ANY_VA_2(BM_CONTEXT, IMG_HANDLE, IMG_NULL)
+IMPLEMENT_LIST_ANY_VA_2(BM_CONTEXT, PVRSRV_ERROR, PVRSRV_OK)
+IMPLEMENT_LIST_FOR_EACH(BM_CONTEXT)
+IMPLEMENT_LIST_REMOVE(BM_CONTEXT)
+IMPLEMENT_LIST_INSERT(BM_CONTEXT)
+
+IMPLEMENT_LIST_ANY_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK)
+IMPLEMENT_LIST_ANY_VA(PVRSRV_DEVICE_NODE)
+IMPLEMENT_LIST_ANY_VA_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK)
+IMPLEMENT_LIST_FOR_EACH(PVRSRV_DEVICE_NODE)
+IMPLEMENT_LIST_FOR_EACH_VA(PVRSRV_DEVICE_NODE)
+IMPLEMENT_LIST_INSERT(PVRSRV_DEVICE_NODE)
+IMPLEMENT_LIST_REMOVE(PVRSRV_DEVICE_NODE)
+
+IMPLEMENT_LIST_ANY_VA(PVRSRV_POWER_DEV)
+IMPLEMENT_LIST_ANY_VA_2(PVRSRV_POWER_DEV, PVRSRV_ERROR, PVRSRV_OK)
+IMPLEMENT_LIST_INSERT(PVRSRV_POWER_DEV)
+IMPLEMENT_LIST_REMOVE(PVRSRV_POWER_DEV)
+
+
+IMG_VOID* MatchDeviceKM_AnyVaCb(PVRSRV_DEVICE_NODE* psDeviceNode, va_list va)
+{
+	IMG_UINT32 ui32DevIndex;
+	IMG_BOOL bIgnoreClass;
+	PVRSRV_DEVICE_CLASS eDevClass;
+
+	ui32DevIndex = va_arg(va, IMG_UINT32);
+	bIgnoreClass = va_arg(va, IMG_BOOL);
+	if (!bIgnoreClass)
+	{
+		eDevClass = va_arg(va, PVRSRV_DEVICE_CLASS);
+	}
+	else
+	{
+
+
+		eDevClass = PVRSRV_DEVICE_CLASS_FORCE_I32;
+	}
+
+	if ((bIgnoreClass || psDeviceNode->sDevId.eDeviceClass == eDevClass) &&
+		psDeviceNode->sDevId.ui32DeviceIndex == ui32DevIndex)
+	{
+		return psDeviceNode;
+	}
+	return IMG_NULL;
+}
+
+IMG_VOID* MatchPowerDeviceIndex_AnyVaCb(PVRSRV_POWER_DEV *psPowerDev, va_list va)
+{
+	IMG_UINT32 ui32DeviceIndex;
+
+	ui32DeviceIndex = va_arg(va, IMG_UINT32);
+
+	if (psPowerDev->ui32DeviceIndex == ui32DeviceIndex)
+	{
+		return psPowerDev;
+	}
+	else
+	{
+		return IMG_NULL;
+	}
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem.c
new file mode 100644
index 0000000..0dd99db
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem.c
@@ -0,0 +1,147 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "pvr_bridge_km.h"
+
+
+static PVRSRV_ERROR
+FreeSharedSysMemCallBack(IMG_PVOID	pvParam,
+						 IMG_UINT32	ui32Param)
+{
+	PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo = pvParam;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	OSFreePages(psKernelMemInfo->ui32Flags,
+				psKernelMemInfo->ui32AllocSize,
+				psKernelMemInfo->pvLinAddrKM,
+				psKernelMemInfo->sMemBlk.hOSMemHandle);
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  sizeof(PVRSRV_KERNEL_MEM_INFO),
+			  psKernelMemInfo,
+			  IMG_NULL);
+
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT PVRSRV_ERROR
+PVRSRVAllocSharedSysMemoryKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+							 IMG_UINT32					ui32Flags,
+							 IMG_SIZE_T 				ui32Size,
+							 PVRSRV_KERNEL_MEM_INFO 	**ppsKernelMemInfo)
+{
+	PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_KERNEL_MEM_INFO),
+				  (IMG_VOID **)&psKernelMemInfo, IMG_NULL,
+				  "Kernel Memory Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVAllocSharedSysMemoryKM: Failed to alloc memory for meminfo"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	OSMemSet(psKernelMemInfo, 0, sizeof(*psKernelMemInfo));
+
+	ui32Flags &= ~PVRSRV_HAP_MAPTYPE_MASK;
+	ui32Flags |= PVRSRV_HAP_MULTI_PROCESS;
+	psKernelMemInfo->ui32Flags = ui32Flags;
+	psKernelMemInfo->ui32AllocSize = ui32Size;
+
+	if(OSAllocPages(psKernelMemInfo->ui32Flags,
+					psKernelMemInfo->ui32AllocSize,
+					HOST_PAGESIZE(),
+					&psKernelMemInfo->pvLinAddrKM,
+					&psKernelMemInfo->sMemBlk.hOSMemHandle)
+		!= PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSharedSysMemoryKM: Failed to alloc memory for block"));
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_KERNEL_MEM_INFO),
+				  psKernelMemInfo,
+				  0);
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+
+	psKernelMemInfo->sMemBlk.hResItem =
+				ResManRegisterRes(psPerProc->hResManContext,
+								  RESMAN_TYPE_SHARED_MEM_INFO,
+								  psKernelMemInfo,
+								  0,
+								  FreeSharedSysMemCallBack);
+
+	*ppsKernelMemInfo = psKernelMemInfo;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT PVRSRV_ERROR
+PVRSRVFreeSharedSysMemoryKM(PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo)
+{
+	PVRSRV_ERROR eError;
+
+	if(psKernelMemInfo->sMemBlk.hResItem)
+	{
+		eError = ResManFreeResByPtr(psKernelMemInfo->sMemBlk.hResItem);
+	}
+	else
+	{
+		eError = FreeSharedSysMemCallBack(psKernelMemInfo, 0);
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT PVRSRV_ERROR
+PVRSRVDissociateMemFromResmanKM(PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	if(!psKernelMemInfo)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	if(psKernelMemInfo->sMemBlk.hResItem)
+	{
+		eError = ResManDissociateRes(psKernelMemInfo->sMemBlk.hResItem, IMG_NULL);
+
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVDissociateMemFromResmanKM: ResManDissociateRes failed"));
+			PVR_DBG_BREAK;
+			return eError;
+		}
+
+		psKernelMemInfo->sMemBlk.hResItem = IMG_NULL;
+	}
+
+	return eError;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem_debug.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem_debug.c
new file mode 100644
index 0000000..e9ae968
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/mem_debug.c
@@ -0,0 +1,246 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef MEM_DEBUG_C
+#define MEM_DEBUG_C
+
+#if defined(PVRSRV_DEBUG_OS_MEMORY)
+
+#include "img_types.h"
+#include "services_headers.h"
+
+#if defined (__cplusplus)
+extern "C"
+{
+#endif
+
+#define STOP_ON_ERROR 0
+
+
+
+
+
+
+
+
+
+	IMG_BOOL MemCheck(const IMG_PVOID pvAddr, const IMG_UINT8 ui8Pattern, IMG_SIZE_T uSize)
+	{
+		IMG_UINT8 *pui8Addr;
+		for (pui8Addr = (IMG_UINT8*)pvAddr; uSize > 0; uSize--, pui8Addr++)
+		{
+			if (*pui8Addr != ui8Pattern)
+			{
+				return IMG_FALSE;
+			}
+		}
+		return IMG_TRUE;
+	}
+
+
+
+	IMG_VOID OSCheckMemDebug(IMG_PVOID pvCpuVAddr, IMG_SIZE_T uSize, const IMG_CHAR *pszFileName, const IMG_UINT32 uLine)
+	{
+		OSMEM_DEBUG_INFO const *psInfo = (OSMEM_DEBUG_INFO *)((IMG_UINT32)pvCpuVAddr - TEST_BUFFER_PADDING_STATUS);
+
+
+		if (pvCpuVAddr == IMG_NULL)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "Pointer 0x%X : null pointer"
+					 " - referenced %s:%d - allocated %s:%d",
+					 pvCpuVAddr,
+					 pszFileName, uLine,
+					 psInfo->sFileName, psInfo->uLineNo));
+			while (STOP_ON_ERROR);
+		}
+
+
+		if (((IMG_UINT32)pvCpuVAddr&3) != 0)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "Pointer 0x%X : invalid alignment"
+					 " - referenced %s:%d - allocated %s:%d",
+					 pvCpuVAddr,
+					 pszFileName, uLine,
+					 psInfo->sFileName, psInfo->uLineNo));
+			while (STOP_ON_ERROR);
+		}
+
+
+		if (!MemCheck((IMG_PVOID)psInfo->sGuardRegionBefore, 0xB1, sizeof(psInfo->sGuardRegionBefore)))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "Pointer 0x%X : guard region before overwritten"
+					 " - referenced %s:%d - allocated %s:%d",
+					 pvCpuVAddr,
+					 pszFileName, uLine,
+					 psInfo->sFileName, psInfo->uLineNo));
+			while (STOP_ON_ERROR);
+		}
+
+
+		if (uSize != psInfo->uSize)
+		{
+			PVR_DPF((PVR_DBG_WARNING, "Pointer 0x%X : supplied size was different to stored size (0x%X != 0x%X)"
+					 " - referenced %s:%d - allocated %s:%d",
+					 pvCpuVAddr, uSize, psInfo->uSize,
+					 pszFileName, uLine,
+					 psInfo->sFileName, psInfo->uLineNo));
+			while (STOP_ON_ERROR);
+		}
+
+
+		if ((0x01234567 ^ psInfo->uSizeParityCheck) != psInfo->uSize)
+		{
+			PVR_DPF((PVR_DBG_WARNING, "Pointer 0x%X : stored size parity error (0x%X != 0x%X)"
+					 " - referenced %s:%d - allocated %s:%d",
+					 pvCpuVAddr, psInfo->uSize, 0x01234567 ^ psInfo->uSizeParityCheck,
+					 pszFileName, uLine,
+					 psInfo->sFileName, psInfo->uLineNo));
+			while (STOP_ON_ERROR);
+		}
+		else
+		{
+
+			uSize = psInfo->uSize;
+		}
+
+
+		if (uSize)
+		{
+			if (!MemCheck((IMG_VOID*)((IMG_UINT32)pvCpuVAddr + uSize), 0xB2, TEST_BUFFER_PADDING_AFTER))
+			{
+				PVR_DPF((PVR_DBG_ERROR, "Pointer 0x%X : guard region after overwritten"
+						 " - referenced from %s:%d - allocated from %s:%d",
+						 pvCpuVAddr,
+						 pszFileName, uLine,
+						 psInfo->sFileName, psInfo->uLineNo));
+			}
+		}
+
+
+		if (psInfo->eValid != isAllocated)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "Pointer 0x%X : not allocated (freed? %d)"
+					 " - referenced %s:%d - freed %s:%d",
+					 pvCpuVAddr, psInfo->eValid == isFree,
+					 pszFileName, uLine,
+					 psInfo->sFileName, psInfo->uLineNo));
+			while (STOP_ON_ERROR);
+		}
+	}
+
+	IMG_VOID debug_strcpy(IMG_CHAR *pDest, const IMG_CHAR *pSrc)
+	{
+		IMG_SIZE_T i = 0;
+
+		for (; i < 128; i++)
+		{
+			*pDest = *pSrc;
+			if (*pSrc == '\0') break;
+			pDest++;
+			pSrc++;
+		}
+	}
+
+	PVRSRV_ERROR OSAllocMem_Debug_Wrapper(IMG_UINT32 ui32Flags,
+										  IMG_UINT32 ui32Size,
+										  IMG_PVOID *ppvCpuVAddr,
+										  IMG_HANDLE *phBlockAlloc,
+										  IMG_CHAR *pszFilename,
+										  IMG_UINT32 ui32Line)
+	{
+		OSMEM_DEBUG_INFO *psInfo;
+
+		PVRSRV_ERROR eError;
+
+		eError = OSAllocMem_Debug_Linux_Memory_Allocations(ui32Flags,
+				 ui32Size + TEST_BUFFER_PADDING,
+				 ppvCpuVAddr,
+				 phBlockAlloc,
+				 pszFilename,
+				 ui32Line);
+
+		if (eError != PVRSRV_OK)
+		{
+			return eError;
+		}
+
+
+		OSMemSet((IMG_CHAR *)(*ppvCpuVAddr) + TEST_BUFFER_PADDING_STATUS, 0xBB, ui32Size);
+		OSMemSet((IMG_CHAR *)(*ppvCpuVAddr) + ui32Size + TEST_BUFFER_PADDING_STATUS, 0xB2, TEST_BUFFER_PADDING_AFTER);
+
+
+		psInfo = (OSMEM_DEBUG_INFO *)(*ppvCpuVAddr);
+
+		OSMemSet(psInfo->sGuardRegionBefore, 0xB1, sizeof(psInfo->sGuardRegionBefore));
+		debug_strcpy(psInfo->sFileName, pszFilename);
+		psInfo->uLineNo = ui32Line;
+		psInfo->eValid = isAllocated;
+		psInfo->uSize = ui32Size;
+		psInfo->uSizeParityCheck = 0x01234567 ^ ui32Size;
+
+
+		*ppvCpuVAddr = (IMG_PVOID) ((IMG_UINT32)*ppvCpuVAddr)+TEST_BUFFER_PADDING_STATUS;
+
+#ifdef PVRSRV_LOG_MEMORY_ALLOCS
+
+		PVR_TRACE(("Allocated pointer (after debug info): 0x%X from %s:%d", *ppvCpuVAddr, pszFilename, ui32Line));
+#endif
+
+		return PVRSRV_OK;
+	}
+
+	PVRSRV_ERROR OSFreeMem_Debug_Wrapper(IMG_UINT32 ui32Flags,
+										 IMG_UINT32 ui32Size,
+										 IMG_PVOID pvCpuVAddr,
+										 IMG_HANDLE hBlockAlloc,
+										 IMG_CHAR *pszFilename,
+										 IMG_UINT32 ui32Line)
+	{
+		OSMEM_DEBUG_INFO *psInfo;
+
+
+		OSCheckMemDebug(pvCpuVAddr, ui32Size, pszFilename, ui32Line);
+
+
+		OSMemSet(pvCpuVAddr, 0xBF, ui32Size + TEST_BUFFER_PADDING_AFTER);
+
+
+		psInfo = (OSMEM_DEBUG_INFO *)((IMG_UINT32) pvCpuVAddr - TEST_BUFFER_PADDING_STATUS);
+
+
+		psInfo->uSize = 0;
+		psInfo->uSizeParityCheck = 0;
+		psInfo->eValid = isFree;
+		psInfo->uLineNo = ui32Line;
+		debug_strcpy(psInfo->sFileName, pszFilename);
+
+		return OSFreeMem_Debug_Linux_Memory_Allocations(ui32Flags, ui32Size + TEST_BUFFER_PADDING, psInfo, hBlockAlloc, pszFilename, ui32Line);
+	}
+
+#if defined (__cplusplus)
+
+}
+#endif
+
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/metrics.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/metrics.c
new file mode 100644
index 0000000..c1df322
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/metrics.c
@@ -0,0 +1,156 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "metrics.h"
+
+#if defined(SUPPORT_VGX)
+#include "vgxapi_km.h"
+#endif
+
+#if defined(SUPPORT_SGX)
+#include "sgxapi_km.h"
+#endif
+
+#if defined(DEBUG) || defined(TIMING)
+
+static volatile IMG_UINT32 *pui32TimerRegister = 0;
+
+#define PVRSRV_TIMER_TOTAL_IN_TICKS(X)	asTimers[X].ui32Total
+#define PVRSRV_TIMER_TOTAL_IN_MS(X)		((1000*asTimers[X].ui32Total)/ui32TicksPerMS)
+#define PVRSRV_TIMER_COUNT(X)			asTimers[X].ui32Count
+
+
+Temporal_Data asTimers[PVRSRV_NUM_TIMERS];
+
+
+IMG_UINT32 PVRSRVTimeNow(IMG_VOID)
+{
+	if (!pui32TimerRegister)
+	{
+		static IMG_BOOL bFirstTime = IMG_TRUE;
+
+		if (bFirstTime)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVTimeNow: No timer register set up"));
+
+			bFirstTime = IMG_FALSE;
+		}
+
+		return 0;
+	}
+
+#if defined(__sh__)
+
+	return (0xffffffff-*pui32TimerRegister);
+
+#else
+
+	return 0;
+
+#endif
+}
+
+
+static IMG_UINT32 PVRSRVGetCPUFreq(IMG_VOID)
+{
+	IMG_UINT32 ui32Time1, ui32Time2;
+
+	ui32Time1 = PVRSRVTimeNow();
+
+	OSWaitus(1000000);
+
+	ui32Time2 = PVRSRVTimeNow();
+
+	PVR_DPF((PVR_DBG_WARNING, "PVRSRVGetCPUFreq: timer frequency = %d Hz", ui32Time2 - ui32Time1));
+
+	return (ui32Time2 - ui32Time1);
+}
+
+
+IMG_VOID PVRSRVSetupMetricTimers(IMG_VOID *pvDevInfo)
+{
+	IMG_UINT32 ui32Loop;
+
+	PVR_UNREFERENCED_PARAMETER(pvDevInfo);
+
+	for(ui32Loop=0; ui32Loop < (PVRSRV_NUM_TIMERS); ui32Loop++)
+	{
+		asTimers[ui32Loop].ui32Total = 0;
+		asTimers[ui32Loop].ui32Count = 0;
+	}
+
+
+	#if defined(__sh__)
+
+
+
+
+
+		*TCR_2 = TIMER_DIVISOR;
+
+
+		*TCOR_2 = *TCNT_2 = (IMG_UINT)0xffffffff;
+
+
+		*TST_REG |= (IMG_UINT8)0x04;
+
+		pui32TimerRegister = (IMG_UINT32 *)TCNT_2;
+
+	#else
+
+		pui32TimerRegister = 0;
+
+	#endif
+
+}
+
+
+IMG_VOID PVRSRVOutputMetricTotals(IMG_VOID)
+{
+	IMG_UINT32 ui32TicksPerMS, ui32Loop;
+
+	ui32TicksPerMS = PVRSRVGetCPUFreq();
+
+	if (!ui32TicksPerMS)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVOutputMetricTotals: Failed to get CPU Freq"));
+		return;
+	}
+
+	for(ui32Loop=0; ui32Loop < (PVRSRV_NUM_TIMERS); ui32Loop++)
+	{
+		if (asTimers[ui32Loop].ui32Count & 0x80000000L)
+		{
+			PVR_DPF((PVR_DBG_WARNING,"PVRSRVOutputMetricTotals: Timer %u is still ON", ui32Loop));
+		}
+	}
+#if 0
+
+	PVR_DPF((PVR_DBG_ERROR," Timer(%u): Total = %u",PVRSRV_TIMER_EXAMPLE_1, PVRSRV_TIMER_TOTAL_IN_TICKS(PVRSRV_TIMER_EXAMPLE_1)));
+	PVR_DPF((PVR_DBG_ERROR," Timer(%u): Time = %ums",PVRSRV_TIMER_EXAMPLE_1, PVRSRV_TIMER_TOTAL_IN_MS(PVRSRV_TIMER_EXAMPLE_1)));
+	PVR_DPF((PVR_DBG_ERROR," Timer(%u): Count = %u",PVRSRV_TIMER_EXAMPLE_1, PVRSRV_TIMER_COUNT(PVRSRV_TIMER_EXAMPLE_1)));
+#endif
+}
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pdump_common.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pdump_common.c
new file mode 100644
index 0000000..518a583
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pdump_common.c
@@ -0,0 +1,1723 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if defined(PDUMP)
+
+#include <stdarg.h>
+
+#include "services_headers.h"
+#if defined(SUPPORT_SGX)
+#include "sgxdefs.h"
+#include "sgxmmu.h"
+#endif
+#include "pdump_km.h"
+
+#if !defined(PDUMP_TEMP_BUFFER_SIZE)
+#define PDUMP_TEMP_BUFFER_SIZE (64 * 1024L)
+#endif
+
+#if 1
+#define PDUMP_DBG(a)   PDumpOSDebugPrintf a
+#else
+#define PDUMP_DBG(a)
+#endif
+
+#define PDUMP_DATAMASTER_PIXEL		(1)
+#define PDUMP_DATAMASTER_EDM		(3)
+
+#define	MIN(x, y) (((x) < (y)) ? (x) : (y))
+#define	PTR_PLUS(t, p, x) ((t *)(((IMG_CHAR *)(p)) + (x)))
+#define	VPTR_PLUS(p, x) PTR_PLUS(IMG_VOID, p, x)
+#define	VPTR_INC(p, x) (p = VPTR_PLUS(p, x))
+#define MAX_PDUMP_MMU_CONTEXTS	(32)
+static IMG_VOID *gpvTempBuffer = IMG_NULL;
+static IMG_HANDLE ghTempBufferBlockAlloc;
+static IMG_UINT16 gui16MMUContextUsage = 0;
+
+
+
+static IMG_VOID *GetTempBuffer(IMG_VOID)
+{
+
+	if (gpvTempBuffer == IMG_NULL)
+	{
+		PVRSRV_ERROR eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					  PDUMP_TEMP_BUFFER_SIZE,
+					  &gpvTempBuffer,
+					  &ghTempBufferBlockAlloc,
+					  "PDUMP Temporary Buffer");
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "GetTempBuffer: OSAllocMem failed: %d", eError));
+		}
+	}
+
+	return gpvTempBuffer;
+}
+
+static IMG_VOID FreeTempBuffer(IMG_VOID)
+{
+
+	if (gpvTempBuffer != IMG_NULL)
+	{
+		PVRSRV_ERROR eError = OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+					  PDUMP_TEMP_BUFFER_SIZE,
+					  gpvTempBuffer,
+					  ghTempBufferBlockAlloc);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreeTempBuffer: OSFreeMem failed: %d", eError));
+		}
+		else
+		{
+			gpvTempBuffer = IMG_NULL;
+		}
+	}
+}
+
+IMG_VOID PDumpInitCommon(IMG_VOID)
+{
+
+	(IMG_VOID) GetTempBuffer();
+
+
+	PDumpInit();
+}
+
+IMG_VOID PDumpDeInitCommon(IMG_VOID)
+{
+
+	FreeTempBuffer();
+
+
+	PDumpDeInit();
+}
+
+#if defined(SGX_SUPPORT_COMMON_PDUMP)
+
+IMG_BOOL PDumpIsSuspended(IMG_VOID)
+{
+	return PDumpOSIsSuspended();
+}
+
+PVRSRV_ERROR PDumpRegWithFlagsKM(IMG_UINT32 ui32Reg, IMG_UINT32 ui32Data, IMG_UINT32 ui32Flags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING()
+	PDUMP_DBG(("PDumpRegWithFlagsKM"));
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW :SGXREG:0x%8.8lX 0x%8.8lX\r\n", ui32Reg, ui32Data);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpRegKM(IMG_UINT32 ui32Reg,IMG_UINT32 ui32Data)
+{
+	return PDumpRegWithFlagsKM(ui32Reg, ui32Data, PDUMP_FLAGS_CONTINUOUS);
+}
+
+PVRSRV_ERROR PDumpRegPolWithFlagsKM(IMG_UINT32 ui32RegAddr, IMG_UINT32 ui32RegValue, IMG_UINT32 ui32Mask, IMG_UINT32 ui32Flags)
+{
+
+	#define POLL_DELAY			1000UL
+	#define POLL_COUNT_LONG		(2000000000UL / POLL_DELAY)
+	#define POLL_COUNT_SHORT	(1000000UL / POLL_DELAY)
+
+	PVRSRV_ERROR eErr;
+	IMG_UINT32	ui32PollCount;
+
+	PDUMP_GET_SCRIPT_STRING();
+	PDUMP_DBG(("PDumpRegPolWithFlagsKM"));
+
+	if (((ui32RegAddr == EUR_CR_EVENT_STATUS) &&
+		(ui32RegValue & ui32Mask & EUR_CR_EVENT_STATUS_TA_FINISHED_MASK) != 0) ||
+	    ((ui32RegAddr == EUR_CR_EVENT_STATUS) &&
+		(ui32RegValue & ui32Mask & EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_MASK) != 0) ||
+	    ((ui32RegAddr == EUR_CR_EVENT_STATUS) &&
+		(ui32RegValue & ui32Mask & EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_MASK) != 0))
+	{
+		ui32PollCount = POLL_COUNT_LONG;
+	}
+	else
+	{
+		ui32PollCount = POLL_COUNT_SHORT;
+	}
+
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "POL :SGXREG:0x%8.8lX 0x%8.8lX 0x%8.8lX %d %lu %d\r\n",
+			ui32RegAddr, ui32RegValue, ui32Mask, 0, ui32PollCount, POLL_DELAY);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PDumpRegPolKM(IMG_UINT32 ui32RegAddr, IMG_UINT32 ui32RegValue, IMG_UINT32 ui32Mask)
+{
+	return PDumpRegPolWithFlagsKM(ui32RegAddr, ui32RegValue, ui32Mask, PDUMP_FLAGS_CONTINUOUS);
+}
+
+PVRSRV_ERROR PDumpMallocPages (PVRSRV_DEVICE_TYPE eDeviceType,
+                           IMG_UINT32         ui32DevVAddr,
+                           IMG_CPU_VIRTADDR   pvLinAddr,
+                           IMG_HANDLE         hOSMemHandle,
+                           IMG_UINT32         ui32NumBytes,
+                           IMG_UINT32         ui32PageSize,
+                           IMG_HANDLE         hUniqueTag)
+{
+	PVRSRV_ERROR eErr;
+	IMG_PUINT8		pui8LinAddr;
+    IMG_UINT32      ui32Offset;
+	IMG_UINT32		ui32NumPages;
+	IMG_DEV_PHYADDR	sDevPAddr;
+	IMG_UINT32		ui32Page;
+
+	PDUMP_GET_SCRIPT_STRING();
+
+#if defined(LINUX)
+	PVR_ASSERT(hOSMemHandle);
+#else
+
+	PVR_UNREFERENCED_PARAMETER(hOSMemHandle);
+	PVR_ASSERT(((IMG_UINT32) pvLinAddr & (SGX_MMU_PAGE_MASK)) == 0);
+#endif
+
+	PVR_ASSERT(((IMG_UINT32) ui32DevVAddr & (SGX_MMU_PAGE_MASK)) == 0);
+	PVR_ASSERT(((IMG_UINT32) ui32NumBytes & (SGX_MMU_PAGE_MASK)) == 0);
+
+
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "-- MALLOC :SGXMEM:VA_%8.8lX 0x%8.8lX %lu\r\n",
+			ui32DevVAddr, ui32NumBytes, ui32PageSize);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+
+
+	pui8LinAddr = (IMG_PUINT8) pvLinAddr;
+	ui32Offset = 0;
+	ui32NumPages = ui32NumBytes / ui32PageSize;
+	while (ui32NumPages)
+	{
+		ui32NumPages--;
+
+
+		PDumpOSCPUVAddrToDevPAddr(eDeviceType,
+				hOSMemHandle,
+				ui32Offset,
+				pui8LinAddr,
+				ui32PageSize,
+				&sDevPAddr);
+		ui32Page = sDevPAddr.uiAddr / ui32PageSize;
+
+		pui8LinAddr	+= ui32PageSize;
+		ui32Offset += ui32PageSize;
+
+		eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "MALLOC :SGXMEM:PA_%8.8lX%8.8lX %lu %lu 0x%8.8lX\r\n",
+												(IMG_UINT32) hUniqueTag,
+												ui32Page * ui32PageSize,
+												ui32PageSize,
+												ui32PageSize,
+												ui32Page * ui32PageSize);
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpMallocPageTable (PVRSRV_DEVICE_TYPE eDeviceType,
+                               IMG_CPU_VIRTADDR   pvLinAddr,
+								IMG_UINT32        ui32PTSize,
+                               IMG_HANDLE         hUniqueTag)
+{
+	PVRSRV_ERROR eErr;
+	IMG_DEV_PHYADDR	sDevPAddr;
+	IMG_UINT32		ui32Page;
+
+	PDUMP_GET_SCRIPT_STRING();
+
+	PVR_ASSERT(((IMG_UINT32) pvLinAddr & (ui32PTSize - 1)) == 0);
+
+
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "-- MALLOC :SGXMEM:PAGE_TABLE 0x%8.8lX %lu\r\n", ui32PTSize, SGX_MMU_PAGE_SIZE);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+	{
+
+
+		PDumpOSCPUVAddrToDevPAddr(eDeviceType,
+				IMG_NULL,
+				0,
+				(IMG_PUINT8) pvLinAddr,
+				SGX_MMU_PAGE_SIZE,
+				&sDevPAddr);
+		ui32Page = sDevPAddr.uiAddr >> SGX_MMU_PAGE_SHIFT;
+
+		eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "MALLOC :SGXMEM:PA_%8.8lX%8.8lX 0x%lX %lu 0x%8.8lX\r\n",
+												(IMG_UINT32) hUniqueTag,
+												ui32Page * SGX_MMU_PAGE_SIZE,
+												SGX_MMU_PAGE_SIZE,
+												SGX_MMU_PAGE_SIZE,
+												ui32Page * SGX_MMU_PAGE_SIZE);
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpFreePages	(BM_HEAP 			*psBMHeap,
+                         IMG_DEV_VIRTADDR  sDevVAddr,
+                         IMG_UINT32        ui32NumBytes,
+                         IMG_UINT32        ui32PageSize,
+                         IMG_HANDLE        hUniqueTag,
+						 IMG_BOOL		   bInterleaved)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32 ui32NumPages, ui32PageCounter;
+	IMG_DEV_PHYADDR	sDevPAddr;
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+
+	PDUMP_GET_SCRIPT_STRING();
+
+	PVR_ASSERT(((IMG_UINT32) sDevVAddr.uiAddr & (ui32PageSize - 1)) == 0);
+	PVR_ASSERT(((IMG_UINT32) ui32NumBytes & (ui32PageSize - 1)) == 0);
+
+
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "-- FREE :SGXMEM:VA_%8.8lX\r\n", sDevVAddr.uiAddr);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+
+
+	ui32NumPages = ui32NumBytes / ui32PageSize;
+	psDeviceNode = psBMHeap->pBMContext->psDeviceNode;
+	for (ui32PageCounter = 0; ui32PageCounter < ui32NumPages; ui32PageCounter++)
+	{
+		if (!bInterleaved || (ui32PageCounter % 2) == 0)
+		{
+			sDevPAddr = psDeviceNode->pfnMMUGetPhysPageAddr(psBMHeap->pMMUHeap, sDevVAddr);
+			{
+				eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "FREE :SGXMEM:PA_%8.8lX%8.8lX\r\n", (IMG_UINT32) hUniqueTag, sDevPAddr.uiAddr);
+				if(eErr != PVRSRV_OK)
+				{
+					return eErr;
+				}
+				PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+			}
+		}
+		else
+		{
+
+		}
+
+		sDevVAddr.uiAddr += ui32PageSize;
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpFreePageTable	(PVRSRV_DEVICE_TYPE eDeviceType,
+							 IMG_CPU_VIRTADDR   pvLinAddr,
+							 IMG_UINT32         ui32PTSize,
+							 IMG_HANDLE         hUniqueTag)
+{
+	PVRSRV_ERROR eErr;
+	IMG_DEV_PHYADDR	sDevPAddr;
+	IMG_UINT32		ui32Page;
+
+	PDUMP_GET_SCRIPT_STRING();
+
+	PVR_UNREFERENCED_PARAMETER(ui32PTSize);
+
+
+	PVR_ASSERT(((IMG_UINT32) pvLinAddr & (ui32PTSize-1UL)) == 0);
+
+
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "-- FREE :SGXMEM:PAGE_TABLE\r\n");
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+
+
+
+
+
+
+
+
+
+
+
+	{
+		PDumpOSCPUVAddrToDevPAddr(eDeviceType,
+				IMG_NULL,
+				0,
+				(IMG_PUINT8) pvLinAddr,
+				SGX_MMU_PAGE_SIZE,
+				&sDevPAddr);
+		ui32Page = sDevPAddr.uiAddr >> SGX_MMU_PAGE_SHIFT;
+
+		{
+			eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "FREE :SGXMEM:PA_%8.8lX%8.8lX\r\n", (IMG_UINT32) hUniqueTag, ui32Page * SGX_MMU_PAGE_SIZE);
+			if(eErr != PVRSRV_OK)
+			{
+				return eErr;
+			}
+			PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+		}
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpPDRegWithFlags(IMG_UINT32 ui32Reg,
+							 IMG_UINT32 ui32Data,
+							 IMG_UINT32	ui32Flags,
+							 IMG_HANDLE hUniqueTag)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING()
+
+
+
+#if defined(SGX_FEATURE_36BIT_MMU)
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen,
+			 "WRW :SGXMEM:$1 :SGXMEM:PA_%8.8lX%8.8lX:0x0\r\n",
+			 (IMG_UINT32)hUniqueTag,
+			 (ui32Data & SGX_MMU_PDE_ADDR_MASK) << SGX_MMU_PDE_ADDR_ALIGNSHIFT);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "SHR :SGXMEM:$1 :SGXMEM:$1 0x4\r\n");
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen,
+			 "WRW :SGXREG:0x%8.8lX: SGXMEM:$1\r\n",
+			 ui32Reg);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+#else
+	eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+				"WRW :SGXREG:0x%8.8lX :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX\r\n",
+				ui32Reg,
+				(IMG_UINT32) hUniqueTag,
+				(ui32Data & SGX_MMU_PDE_ADDR_MASK) << SGX_MMU_PDE_ADDR_ALIGNSHIFT,
+				ui32Data & ~SGX_MMU_PDE_ADDR_MASK);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+#endif
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpPDReg	(IMG_UINT32 ui32Reg,
+					 IMG_UINT32 ui32Data,
+					 IMG_HANDLE hUniqueTag)
+{
+	return PDumpPDRegWithFlags(ui32Reg, ui32Data, PDUMP_FLAGS_CONTINUOUS, hUniqueTag);
+}
+
+PVRSRV_ERROR PDumpMemPolKM(PVRSRV_KERNEL_MEM_INFO		*psMemInfo,
+						   IMG_UINT32			ui32Offset,
+						   IMG_UINT32			ui32Value,
+						   IMG_UINT32			ui32Mask,
+						   PDUMP_POLL_OPERATOR	eOperator,
+						   IMG_UINT32			ui32Flags,
+						   IMG_HANDLE			hUniqueTag)
+{
+	#define MEMPOLL_DELAY		(1000)
+	#define MEMPOLL_COUNT		(2000000000 / MEMPOLL_DELAY)
+
+	PVRSRV_ERROR eErr;
+	IMG_UINT32			ui32PageOffset;
+	IMG_UINT8			*pui8LinAddr;
+	IMG_DEV_PHYADDR		sDevPAddr;
+	IMG_DEV_VIRTADDR	sDevVPageAddr;
+	PDUMP_GET_SCRIPT_STRING();
+
+
+	PVR_ASSERT((ui32Offset + sizeof(IMG_UINT32)) <= psMemInfo->ui32AllocSize);
+
+
+
+	eErr = PDumpOSBufprintf(hScript,
+			 ui32MaxLen,
+			 "-- POL :SGXMEM:VA_%8.8lX 0x%8.8lX 0x%8.8lX %d %d %d\r\n",
+			 psMemInfo->sDevVAddr.uiAddr + ui32Offset,
+			 ui32Value,
+			 ui32Mask,
+			 eOperator,
+			 MEMPOLL_COUNT,
+			 MEMPOLL_DELAY);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+
+
+	pui8LinAddr = psMemInfo->pvLinAddrKM;
+
+
+	pui8LinAddr += ui32Offset;
+
+
+
+
+	PDumpOSCPUVAddrToPhysPages(psMemInfo->sMemBlk.hOSMemHandle,
+			ui32Offset,
+			pui8LinAddr,
+			&ui32PageOffset);
+
+
+	sDevVPageAddr.uiAddr = psMemInfo->sDevVAddr.uiAddr + ui32Offset - ui32PageOffset;
+
+	PVR_ASSERT((sDevVPageAddr.uiAddr & 0xFFF) == 0);
+
+
+	BM_GetPhysPageAddr(psMemInfo, sDevVPageAddr, &sDevPAddr);
+
+
+	sDevPAddr.uiAddr += ui32PageOffset;
+
+	eErr = PDumpOSBufprintf(hScript,
+			 ui32MaxLen,
+			 "POL :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX 0x%8.8lX %d %d %d\r\n",
+			 (IMG_UINT32) hUniqueTag,
+			 sDevPAddr.uiAddr & ~(SGX_MMU_PAGE_MASK),
+			 sDevPAddr.uiAddr & (SGX_MMU_PAGE_MASK),
+			 ui32Value,
+			 ui32Mask,
+			 eOperator,
+			 MEMPOLL_COUNT,
+			 MEMPOLL_DELAY);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpMemKM(IMG_PVOID pvAltLinAddr,
+						PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+						IMG_UINT32 ui32Offset,
+						IMG_UINT32 ui32Bytes,
+						IMG_UINT32 ui32Flags,
+						IMG_HANDLE hUniqueTag)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32 ui32NumPages;
+	IMG_UINT32 ui32PageByteOffset;
+	IMG_UINT32 ui32BlockBytes;
+	IMG_UINT8* pui8LinAddr;
+	IMG_UINT8* pui8DataLinAddr = IMG_NULL;
+	IMG_DEV_VIRTADDR sDevVPageAddr;
+	IMG_DEV_VIRTADDR sDevVAddr;
+	IMG_DEV_PHYADDR sDevPAddr;
+	IMG_UINT32 ui32ParamOutPos;
+
+	PDUMP_GET_SCRIPT_AND_FILE_STRING();
+
+
+	PVR_ASSERT((ui32Offset + ui32Bytes) <= psMemInfo->ui32AllocSize);
+
+	if (!PDumpOSJTInitialised())
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	if (ui32Bytes == 0 || PDumpOSIsSuspended())
+	{
+		return PVRSRV_OK;
+	}
+
+
+	if(pvAltLinAddr)
+	{
+		pui8DataLinAddr = pvAltLinAddr;
+	}
+	else if(psMemInfo->pvLinAddrKM)
+	{
+		pui8DataLinAddr = (IMG_UINT8 *)psMemInfo->pvLinAddrKM + ui32Offset;
+	}
+	pui8LinAddr = (IMG_UINT8 *)psMemInfo->pvLinAddrKM;
+	sDevVAddr = psMemInfo->sDevVAddr;
+
+
+	sDevVAddr.uiAddr += ui32Offset;
+	pui8LinAddr += ui32Offset;
+
+	PVR_ASSERT(pui8DataLinAddr);
+
+	PDumpOSCheckForSplitting(PDumpOSGetStream(PDUMP_STREAM_PARAM2), ui32Bytes, ui32Flags);
+
+	ui32ParamOutPos = PDumpOSGetStreamOffset(PDUMP_STREAM_PARAM2);
+
+
+
+	if(!PDumpOSWriteString(PDumpOSGetStream(PDUMP_STREAM_PARAM2),
+						pui8DataLinAddr,
+						ui32Bytes,
+						ui32Flags))
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	if (PDumpOSGetParamFileNum() == 0)
+	{
+		eErr = PDumpOSSprintf(pszFileName, ui32MaxLenFileName, "%%0%%.prm");
+	}
+	else
+	{
+		eErr = PDumpOSSprintf(pszFileName, ui32MaxLenFileName, "%%0%%%lu.prm", PDumpOSGetParamFileNum());
+	}
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+
+
+	eErr = PDumpOSBufprintf(hScript,
+			 ui32MaxLenScript,
+			 "-- LDB :SGXMEM:VA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX 0x%8.8lX %s\r\n",
+			 (IMG_UINT32)hUniqueTag,
+			 psMemInfo->sDevVAddr.uiAddr,
+			 ui32Offset,
+			 ui32Bytes,
+			 ui32ParamOutPos,
+			 pszFileName);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+
+
+
+
+	PDumpOSCPUVAddrToPhysPages(psMemInfo->sMemBlk.hOSMemHandle,
+			ui32Offset,
+			pui8LinAddr,
+			&ui32PageByteOffset);
+	ui32NumPages = (ui32PageByteOffset + ui32Bytes + HOST_PAGESIZE() - 1) / HOST_PAGESIZE();
+
+	while(ui32NumPages)
+	{
+#if 0
+		IMG_UINT32 ui32BlockBytes = MIN(ui32BytesRemaining, PAGE_SIZE);
+		CpuPAddr = OSMemHandleToCpuPAddr(psMemInfo->sMemBlk.hOSMemHandle,
+						 ui32CurrentOffset);
+#endif
+		ui32NumPages--;
+
+
+		sDevVPageAddr.uiAddr = sDevVAddr.uiAddr - ui32PageByteOffset;
+
+		PVR_ASSERT((sDevVPageAddr.uiAddr & 0xFFF) == 0);
+
+
+		BM_GetPhysPageAddr(psMemInfo, sDevVPageAddr, &sDevPAddr);
+
+
+		sDevPAddr.uiAddr += ui32PageByteOffset;
+#if 0
+		if(ui32PageByteOffset)
+		{
+		    ui32BlockBytes =
+			MIN(ui32BytesRemaining, PAGE_ALIGN(CpuPAddr.uiAddr) - CpuPAddr.uiAddr);
+
+		    ui32PageByteOffset = 0;
+		}
+#endif
+
+		if (ui32PageByteOffset + ui32Bytes > HOST_PAGESIZE())
+		{
+
+			ui32BlockBytes = HOST_PAGESIZE() - ui32PageByteOffset;
+		}
+		else
+		{
+
+			ui32BlockBytes = ui32Bytes;
+		}
+
+		eErr = PDumpOSBufprintf(hScript,
+					 ui32MaxLenScript,
+					 "LDB :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX 0x%8.8lX %s\r\n",
+					 (IMG_UINT32) hUniqueTag,
+					 sDevPAddr.uiAddr & ~(SGX_MMU_PAGE_MASK),
+					 sDevPAddr.uiAddr & (SGX_MMU_PAGE_MASK),
+					 ui32BlockBytes,
+					 ui32ParamOutPos,
+					 pszFileName);
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, ui32Flags);
+
+
+
+
+		ui32PageByteOffset = 0;
+
+		ui32Bytes -= ui32BlockBytes;
+
+		sDevVAddr.uiAddr += ui32BlockBytes;
+
+		pui8LinAddr += ui32BlockBytes;
+
+		ui32ParamOutPos += ui32BlockBytes;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpMem2KM(PVRSRV_DEVICE_TYPE eDeviceType,
+						 IMG_CPU_VIRTADDR pvLinAddr,
+						 IMG_UINT32 ui32Bytes,
+						 IMG_UINT32 ui32Flags,
+						 IMG_BOOL bInitialisePages,
+						 IMG_HANDLE hUniqueTag1,
+						 IMG_HANDLE hUniqueTag2)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32 ui32NumPages;
+	IMG_UINT32 ui32PageOffset;
+	IMG_UINT32 ui32BlockBytes;
+	IMG_UINT8* pui8LinAddr;
+	IMG_DEV_PHYADDR sDevPAddr;
+	IMG_CPU_PHYADDR sCpuPAddr;
+	IMG_UINT32 ui32Offset;
+	IMG_UINT32 ui32ParamOutPos;
+
+	PDUMP_GET_SCRIPT_AND_FILE_STRING();
+
+	if (!pvLinAddr || !PDumpOSJTInitialised())
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	if (PDumpOSIsSuspended())
+	{
+		return PVRSRV_OK;
+	}
+
+	PDumpOSCheckForSplitting(PDumpOSGetStream(PDUMP_STREAM_PARAM2), ui32Bytes, ui32Flags);
+
+	ui32ParamOutPos = PDumpOSGetStreamOffset(PDUMP_STREAM_PARAM2);
+
+	if (bInitialisePages)
+	{
+
+
+
+		if (!PDumpOSWriteString(PDumpOSGetStream(PDUMP_STREAM_PARAM2),
+							pvLinAddr,
+							ui32Bytes,
+							PDUMP_FLAGS_CONTINUOUS))
+		{
+			return PVRSRV_ERROR_GENERIC;
+		}
+
+		if (PDumpOSGetParamFileNum() == 0)
+		{
+			eErr = PDumpOSSprintf(pszFileName, ui32MaxLenFileName, "%%0%%.prm");
+		}
+		else
+		{
+			eErr = PDumpOSSprintf(pszFileName, ui32MaxLenFileName, "%%0%%%lu.prm", PDumpOSGetParamFileNum());
+		}
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+	}
+
+
+
+
+	ui32PageOffset	= (IMG_UINT32) pvLinAddr & (HOST_PAGESIZE() - 1);
+	ui32NumPages	= (ui32PageOffset + ui32Bytes + HOST_PAGESIZE() - 1) / HOST_PAGESIZE();
+	pui8LinAddr		= (IMG_UINT8*) pvLinAddr;
+
+	while (ui32NumPages)
+	{
+		ui32NumPages--;
+		sCpuPAddr = OSMapLinToCPUPhys(pui8LinAddr);
+		sDevPAddr = SysCpuPAddrToDevPAddr(eDeviceType, sCpuPAddr);
+
+
+		if (ui32PageOffset + ui32Bytes > HOST_PAGESIZE())
+		{
+
+			ui32BlockBytes = HOST_PAGESIZE() - ui32PageOffset;
+		}
+		else
+		{
+
+			ui32BlockBytes = ui32Bytes;
+		}
+
+
+
+		if (bInitialisePages)
+		{
+			eErr = PDumpOSBufprintf(hScript,
+					 ui32MaxLenScript,
+					 "LDB :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX 0x%8.8lX %s\r\n",
+					 (IMG_UINT32) hUniqueTag1,
+					 sDevPAddr.uiAddr & ~(SGX_MMU_PAGE_MASK),
+					 sDevPAddr.uiAddr & (SGX_MMU_PAGE_MASK),
+					 ui32BlockBytes,
+					 ui32ParamOutPos,
+					 pszFileName);
+			if(eErr != PVRSRV_OK)
+			{
+				return eErr;
+			}
+			PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+		}
+		else
+		{
+			for (ui32Offset = 0; ui32Offset < ui32BlockBytes; ui32Offset += sizeof(IMG_UINT32))
+			{
+				IMG_UINT32 ui32PTE = *((IMG_UINT32 *) (pui8LinAddr + ui32Offset));
+
+				if ((ui32PTE & SGX_MMU_PDE_ADDR_MASK) != 0)
+				{
+#if defined(SGX_FEATURE_36BIT_MMU)
+					eErr = PDumpOSBufprintf(hScript,
+							ui32MaxLenScript,
+							 "WRW :SGXMEM:$1 :SGXMEM:PA_%8.8lX%8.8lX:0x0\r\n",
+							 (IMG_UINT32)hUniqueTag2,
+							 (ui32PTE & SGX_MMU_PDE_ADDR_MASK) << SGX_MMU_PTE_ADDR_ALIGNSHIFT);
+					if(eErr != PVRSRV_OK)
+					{
+						return eErr;
+					}
+					PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+					eErr = PDumpOSBufprintf(hScript, ui32MaxLenScript, "SHR :SGXMEM:$1 :SGXMEM:$1 0x4\r\n");
+					if(eErr != PVRSRV_OK)
+					{
+						return eErr;
+					}
+					PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+					eErr = PDumpOSBufprintf(hScript, ui32MaxLenScript, "OR :SGXMEM:$1 :SGXMEM:$1 0x%8.8lX\r\n", ui32PTE & ~SGX_MMU_PDE_ADDR_MASK);
+					if(eErr != PVRSRV_OK)
+					{
+						return eErr;
+					}
+					PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+					eErr = PDumpOSBufprintf(hScript,
+							ui32MaxLenScript,
+							 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX :SGXMEM:$1\r\n",
+							 (IMG_UINT32)hUniqueTag1,
+							 (sDevPAddr.uiAddr + ui32Offset) & ~(SGX_MMU_PAGE_MASK),
+							 (sDevPAddr.uiAddr + ui32Offset) & (SGX_MMU_PAGE_MASK));
+					if(eErr != PVRSRV_OK)
+					{
+						return eErr;
+					}
+					PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+#else
+					eErr = PDumpOSBufprintf(hScript,
+							ui32MaxLenScript,
+							 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX\r\n",
+							 (IMG_UINT32) hUniqueTag1,
+							 (sDevPAddr.uiAddr + ui32Offset) & ~(SGX_MMU_PAGE_MASK),
+							 (sDevPAddr.uiAddr + ui32Offset) & (SGX_MMU_PAGE_MASK),
+							 (IMG_UINT32) hUniqueTag2,
+							 (ui32PTE & SGX_MMU_PDE_ADDR_MASK) << SGX_MMU_PTE_ADDR_ALIGNSHIFT,
+							 ui32PTE & ~SGX_MMU_PDE_ADDR_MASK);
+					if(eErr != PVRSRV_OK)
+					{
+						return eErr;
+					}
+#endif
+				}
+				else
+				{
+					PVR_ASSERT((ui32PTE & SGX_MMU_PTE_VALID) == 0UL);
+					eErr = PDumpOSBufprintf(hScript,
+							ui32MaxLenScript,
+							 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX%8.8lX\r\n",
+							 (IMG_UINT32) hUniqueTag1,
+							 (sDevPAddr.uiAddr + ui32Offset) & ~(SGX_MMU_PAGE_MASK),
+							 (sDevPAddr.uiAddr + ui32Offset) & (SGX_MMU_PAGE_MASK),
+							 (ui32PTE << SGX_MMU_PTE_ADDR_ALIGNSHIFT),
+							 (IMG_UINT32) hUniqueTag2);
+					if(eErr != PVRSRV_OK)
+					{
+						return eErr;
+					}
+				}
+				PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+			}
+		}
+
+
+
+
+		ui32PageOffset = 0;
+
+		ui32Bytes -= ui32BlockBytes;
+
+		pui8LinAddr += ui32BlockBytes;
+
+		ui32ParamOutPos += ui32BlockBytes;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpPDDevPAddrKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+							   IMG_UINT32 ui32Offset,
+							   IMG_DEV_PHYADDR sPDDevPAddr,
+							   IMG_HANDLE hUniqueTag1,
+							   IMG_HANDLE hUniqueTag2)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32 ui32PageByteOffset;
+	IMG_DEV_VIRTADDR sDevVAddr;
+	IMG_DEV_VIRTADDR sDevVPageAddr;
+	IMG_DEV_PHYADDR sDevPAddr;
+
+	PDUMP_GET_SCRIPT_STRING();
+
+	if(!PDumpOSWriteString(PDumpOSGetStream(PDUMP_STREAM_PARAM2),
+						(IMG_UINT8 *)&sPDDevPAddr,
+						sizeof(IMG_DEV_PHYADDR),
+						PDUMP_FLAGS_CONTINUOUS))
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	sDevVAddr = psMemInfo->sDevVAddr;
+	ui32PageByteOffset = sDevVAddr.uiAddr & (SGX_MMU_PAGE_MASK);
+
+	sDevVPageAddr.uiAddr = sDevVAddr.uiAddr - ui32PageByteOffset;
+	PVR_ASSERT((sDevVPageAddr.uiAddr & 0xFFF) == 0);
+
+	BM_GetPhysPageAddr(psMemInfo, sDevVPageAddr, &sDevPAddr);
+	sDevPAddr.uiAddr += ui32PageByteOffset + ui32Offset;
+
+	if ((sPDDevPAddr.uiAddr & SGX_MMU_PDE_ADDR_MASK) != 0UL)
+	{
+#if defined(SGX_FEATURE_36BIT_MMU)
+		eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+				 "WRW :SGXMEM:$1 :SGXMEM:PA_%8.8lX%8.8lX:0x0\r\n",
+				 (IMG_UINT32)hUniqueTag2,
+				 sPDDevPAddr.uiAddr);
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+		eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "AND  :SGXMEM:$2 :SGXMEM:$1 0xFFFFFFFF\r\n");
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+		eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+				 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX :SGXMEM:$2\r\n",
+				 (IMG_UINT32)hUniqueTag1,
+				 (sDevPAddr.uiAddr) & ~(SGX_MMU_PAGE_MASK),
+				 (sDevPAddr.uiAddr) & (SGX_MMU_PAGE_MASK));
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+		eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "SHR :SGXMEM:$2 :SGXMEM:$1 0x20\r\n");
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+		eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+				 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX :SGXMEM:$2\r\n",
+				 (IMG_UINT32)hUniqueTag1,
+				 (sDevPAddr.uiAddr + 4) & ~(SGX_MMU_PAGE_MASK),
+				 (sDevPAddr.uiAddr + 4) & (SGX_MMU_PAGE_MASK));
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+		PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+#else
+		eErr = PDumpOSBufprintf(hScript,
+				 ui32MaxLen,
+				 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX\r\n",
+				 (IMG_UINT32) hUniqueTag1,
+				 sDevPAddr.uiAddr & ~(SGX_MMU_PAGE_MASK),
+				 sDevPAddr.uiAddr & (SGX_MMU_PAGE_MASK),
+				 (IMG_UINT32) hUniqueTag2,
+				 sPDDevPAddr.uiAddr & SGX_MMU_PDE_ADDR_MASK,
+				 sPDDevPAddr.uiAddr & ~SGX_MMU_PDE_ADDR_MASK);
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+#endif
+	}
+	else
+	{
+		PVR_ASSERT(!(sDevPAddr.uiAddr & SGX_MMU_PTE_VALID));
+		eErr = PDumpOSBufprintf(hScript,
+				 ui32MaxLen,
+				 "WRW :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX\r\n",
+				 (IMG_UINT32) hUniqueTag1,
+				 sDevPAddr.uiAddr & ~(SGX_MMU_PAGE_MASK),
+				 sDevPAddr.uiAddr & (SGX_MMU_PAGE_MASK),
+				 sPDDevPAddr.uiAddr);
+		if(eErr != PVRSRV_OK)
+		{
+			return eErr;
+		}
+	}
+	PDumpOSWriteString2(hScript, PDUMP_FLAGS_CONTINUOUS);
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpCommentKM(IMG_CHAR *pszComment, IMG_UINT32 ui32Flags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_MSG_STRING();
+	PDUMP_DBG(("PDumpCommentKM"));
+
+
+	if (!PDumpOSWriteString2("-- ", ui32Flags))
+	{
+		if(ui32Flags & PDUMP_FLAGS_CONTINUOUS)
+		{
+			return PVRSRV_ERROR_GENERIC;
+		}
+		else
+		{
+			return PVRSRV_ERROR_CMD_NOT_PROCESSED;
+		}
+	}
+
+
+	eErr = PDumpOSBufprintf(hMsg, ui32MaxLen, "%s", pszComment);
+	if( (eErr != PVRSRV_OK) &&
+		(eErr != PVRSRV_ERROR_PDUMP_BUF_OVERFLOW))
+	{
+		return eErr;
+	}
+
+
+	PDumpOSVerifyLineEnding(hMsg, ui32MaxLen);
+	PDumpOSWriteString2(hMsg, ui32Flags);
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpCommentWithFlags(IMG_UINT32 ui32Flags, IMG_CHAR * pszFormat, ...)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_va_list ap;
+	PDUMP_GET_MSG_STRING();
+
+
+	PDUMP_va_start(ap, pszFormat);
+	eErr = PDumpOSVSprintf(hMsg, ui32MaxLen, pszFormat, ap);
+	PDUMP_va_end(ap);
+
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	return PDumpCommentKM(hMsg, ui32Flags);
+}
+
+PVRSRV_ERROR PDumpComment(IMG_CHAR *pszFormat, ...)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_va_list ap;
+	PDUMP_GET_MSG_STRING();
+
+
+	PDUMP_va_start(ap, pszFormat);
+	eErr = PDumpOSVSprintf(hMsg, ui32MaxLen, pszFormat, ap);
+	PDUMP_va_end(ap);
+
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	return PDumpCommentKM(hMsg, PDUMP_FLAGS_CONTINUOUS);
+}
+
+PVRSRV_ERROR PDumpDriverInfoKM(IMG_CHAR *pszString, IMG_UINT32 ui32Flags)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32	ui32MsgLen;
+	PDUMP_GET_MSG_STRING();
+
+
+	eErr = PDumpOSBufprintf(hMsg, ui32MaxLen, "%s", pszString);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+
+	PDumpOSVerifyLineEnding(hMsg, ui32MaxLen);
+	ui32MsgLen = PDumpOSBuflen(hMsg, ui32MaxLen);
+
+	if	(!PDumpOSWriteString(PDumpOSGetStream(PDUMP_STREAM_DRIVERINFO),
+						  (IMG_UINT8 *)hMsg,
+						  ui32MsgLen,
+						  ui32Flags))
+	{
+		if	(ui32Flags & PDUMP_FLAGS_CONTINUOUS)
+		{
+			return PVRSRV_ERROR_GENERIC;
+		}
+		else
+		{
+			return PVRSRV_ERROR_CMD_NOT_PROCESSED;
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpBitmapKM(	IMG_CHAR *pszFileName,
+							IMG_UINT32 ui32FileOffset,
+							IMG_UINT32 ui32Width,
+							IMG_UINT32 ui32Height,
+							IMG_UINT32 ui32StrideInBytes,
+							IMG_DEV_VIRTADDR sDevBaseAddr,
+							IMG_UINT32 ui32Size,
+							PDUMP_PIXEL_FORMAT ePixelFormat,
+							PDUMP_MEM_FORMAT eMemFormat,
+							IMG_UINT32 ui32PDumpFlags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+	PDumpCommentWithFlags(ui32PDumpFlags, "\r\n-- Dump bitmap of render\r\n");
+
+#if defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+
+	eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+				"SII %s %s.bin :SGXMEM:v%x:0x%08lX 0x%08lX 0x%08lX 0x%08X 0x%08lX 0x%08lX 0x%08lX 0x%08X\r\n",
+				pszFileName,
+				pszFileName,
+				PDUMP_DATAMASTER_PIXEL,
+				sDevBaseAddr.uiAddr,
+				ui32Size,
+				ui32FileOffset,
+				ePixelFormat,
+				ui32Width,
+				ui32Height,
+				ui32StrideInBytes,
+				eMemFormat);
+#else
+	eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+				"SII %s %s.bin :SGXMEM:v:0x%08lX 0x%08lX 0x%08lX 0x%08X 0x%08lX 0x%08lX 0x%08lX 0x%08X\r\n",
+				pszFileName,
+				pszFileName,
+				sDevBaseAddr.uiAddr,
+				ui32Size,
+				ui32FileOffset,
+				ePixelFormat,
+				ui32Width,
+				ui32Height,
+				ui32StrideInBytes,
+				eMemFormat);
+#endif
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpOSWriteString2( hScript, ui32PDumpFlags);
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpReadRegKM		(	IMG_CHAR *pszFileName,
+									IMG_UINT32 ui32FileOffset,
+									IMG_UINT32 ui32Address,
+									IMG_UINT32 ui32Size,
+									IMG_UINT32 ui32PDumpFlags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+
+	PVR_UNREFERENCED_PARAMETER(ui32Size);
+
+	eErr = PDumpOSBufprintf(hScript,
+			ui32MaxLen,
+			"SAB :SGXREG:0x%08lX 0x%08lX %s\r\n",
+			ui32Address,
+			ui32FileOffset,
+			pszFileName);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpOSWriteString2( hScript, ui32PDumpFlags);
+
+	return PVRSRV_OK;
+}
+
+IMG_BOOL PDumpTestNextFrame(IMG_UINT32 ui32CurrentFrame)
+{
+	IMG_BOOL	bFrameDumped;
+
+
+
+	(IMG_VOID) PDumpSetFrameKM(ui32CurrentFrame + 1);
+	bFrameDumped = PDumpIsCaptureFrameKM();
+	(IMG_VOID) PDumpSetFrameKM(ui32CurrentFrame);
+
+	return bFrameDumped;
+}
+
+static PVRSRV_ERROR PDumpSignatureRegister	(IMG_CHAR	*pszFileName,
+									 IMG_UINT32		ui32Address,
+									 IMG_UINT32		ui32Size,
+									 IMG_UINT32		*pui32FileOffset,
+									 IMG_UINT32		ui32Flags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+
+	eErr = PDumpOSBufprintf(hScript,
+			ui32MaxLen,
+			"SAB :SGXREG:0x%08X 0x%08X %s\r\n",
+			ui32Address,
+			*pui32FileOffset,
+			pszFileName);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpOSWriteString2(hScript, ui32Flags);
+	*pui32FileOffset += ui32Size;
+	return PVRSRV_OK;
+}
+
+static IMG_VOID PDumpRegisterRange(IMG_CHAR *pszFileName,
+		IMG_UINT32 *pui32Registers,
+		IMG_UINT32  ui32NumRegisters,
+		IMG_UINT32 *pui32FileOffset,
+		IMG_UINT32	ui32Size,
+		IMG_UINT32	ui32Flags)
+{
+	IMG_UINT32 i;
+	for (i = 0; i < ui32NumRegisters; i++)
+	{
+		PDumpSignatureRegister(pszFileName, pui32Registers[i], ui32Size, pui32FileOffset, ui32Flags);
+	}
+}
+
+PVRSRV_ERROR PDump3DSignatureRegisters(IMG_UINT32 ui32DumpFrameNum,
+			IMG_BOOL bLastFrame,
+			IMG_UINT32 *pui32Registers,
+			IMG_UINT32 ui32NumRegisters)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32	ui32FileOffset, ui32Flags;
+
+	PDUMP_GET_FILE_STRING();
+
+	ui32Flags = bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0;
+	ui32FileOffset = 0;
+
+	PDumpCommentWithFlags(ui32Flags, "\r\n-- Dump 3D signature registers\r\n");
+	eErr = PDumpOSSprintf(pszFileName, ui32MaxLen, "out%lu_3d.sig", ui32DumpFrameNum);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpRegisterRange(pszFileName, pui32Registers, ui32NumRegisters, &ui32FileOffset, sizeof(IMG_UINT32), ui32Flags);
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpTASignatureRegisters	(IMG_UINT32 ui32DumpFrameNum,
+			 IMG_UINT32	ui32TAKickCount,
+			 IMG_BOOL	bLastFrame,
+			 IMG_UINT32 *pui32Registers,
+			 IMG_UINT32 ui32NumRegisters)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32	ui32FileOffset, ui32Flags;
+
+	PDUMP_GET_FILE_STRING();
+
+	ui32Flags = bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0;
+	ui32FileOffset = ui32TAKickCount * ui32NumRegisters * sizeof(IMG_UINT32);
+
+	PDumpCommentWithFlags(ui32Flags, "\r\n-- Dump TA signature registers\r\n");
+	eErr = PDumpOSSprintf(pszFileName, ui32MaxLen, "out%lu_ta.sig", ui32DumpFrameNum);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpRegisterRange(pszFileName, pui32Registers, ui32NumRegisters, &ui32FileOffset, sizeof(IMG_UINT32), ui32Flags);
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpCounterRegisters (IMG_UINT32 ui32DumpFrameNum,
+								IMG_BOOL	bLastFrame,
+								IMG_UINT32 *pui32Registers,
+								IMG_UINT32 ui32NumRegisters)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32	ui32FileOffset, ui32Flags;
+
+	PDUMP_GET_FILE_STRING();
+
+	ui32Flags = bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0UL;
+	ui32FileOffset = 0UL;
+
+	PDumpCommentWithFlags(ui32Flags, "\r\n-- Dump counter registers\r\n");
+	eErr = PDumpOSSprintf(pszFileName, ui32MaxLen, "out%lu.perf", ui32DumpFrameNum);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpRegisterRange(pszFileName, pui32Registers, ui32NumRegisters, &ui32FileOffset, sizeof(IMG_UINT32), ui32Flags);
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpRegRead(const IMG_UINT32 ui32RegOffset, IMG_UINT32 ui32Flags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "RDW :SGXREG:0x%lX\r\n", ui32RegOffset);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpCycleCountRegRead(const IMG_UINT32 ui32RegOffset, IMG_BOOL bLastFrame)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "RDW :SGXREG:0x%lX\r\n", ui32RegOffset);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, bLastFrame ? PDUMP_FLAGS_LASTFRAME : 0);
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpHWPerfCBKM (IMG_CHAR			*pszFileName,
+						  IMG_UINT32		ui32FileOffset,
+						  IMG_DEV_VIRTADDR	sDevBaseAddr,
+						  IMG_UINT32 		ui32Size,
+						  IMG_UINT32 		ui32PDumpFlags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+	PDumpCommentWithFlags(ui32PDumpFlags, "\r\n-- Dump Hardware Performance Circular Buffer\r\n");
+
+	eErr = PDumpOSBufprintf(hScript,
+				ui32MaxLen,
+#if defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+				"SAB :SGXMEM:v%x:0x%08lX 0x%08lX 0x%08lX %s.bin\r\n",
+				PDUMP_DATAMASTER_EDM,
+#else
+				"SAB :SGXMEM:v:0x%08lX 0x%08lX 0x%08lX %s.bin\r\n",
+#endif
+				sDevBaseAddr.uiAddr,
+				ui32Size,
+				ui32FileOffset,
+				pszFileName);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+
+	PDumpOSWriteString2(hScript, ui32PDumpFlags);
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PDumpCBP(PPVRSRV_KERNEL_MEM_INFO		psROffMemInfo,
+			  IMG_UINT32					ui32ROffOffset,
+			  IMG_UINT32					ui32WPosVal,
+			  IMG_UINT32					ui32PacketSize,
+			  IMG_UINT32					ui32BufferSize,
+			  IMG_UINT32					ui32Flags,
+			  IMG_HANDLE					hUniqueTag)
+{
+	PVRSRV_ERROR eErr;
+	IMG_UINT32			ui32PageOffset;
+	IMG_UINT8			*pui8LinAddr;
+	IMG_DEV_VIRTADDR	sDevVAddr;
+	IMG_DEV_PHYADDR		sDevPAddr;
+	IMG_DEV_VIRTADDR 	sDevVPageAddr;
+
+
+	PDUMP_GET_SCRIPT_STRING();
+
+
+	PVR_ASSERT((ui32ROffOffset + sizeof(IMG_UINT32)) <= psROffMemInfo->ui32AllocSize);
+
+	pui8LinAddr = psROffMemInfo->pvLinAddrKM;
+	sDevVAddr = psROffMemInfo->sDevVAddr;
+
+
+	pui8LinAddr += ui32ROffOffset;
+	sDevVAddr.uiAddr += ui32ROffOffset;
+
+
+
+
+
+
+	PDumpOSCPUVAddrToPhysPages(psROffMemInfo->sMemBlk.hOSMemHandle,
+			ui32ROffOffset,
+			pui8LinAddr,
+			&ui32PageOffset);
+
+
+	sDevVPageAddr.uiAddr = sDevVAddr.uiAddr - ui32PageOffset;
+
+	PVR_ASSERT((sDevVPageAddr.uiAddr & 0xFFF) == 0);
+
+
+	BM_GetPhysPageAddr(psROffMemInfo, sDevVPageAddr, &sDevPAddr);
+
+
+	sDevPAddr.uiAddr += ui32PageOffset;
+
+	eErr = PDumpOSBufprintf(hScript,
+			 ui32MaxLen,
+			 "CBP :SGXMEM:PA_%8.8lX%8.8lX:0x%8.8lX 0x%8.8lX 0x%8.8lX 0x%8.8lX\r\n",
+			 (IMG_UINT32) hUniqueTag,
+			 sDevPAddr.uiAddr & ~(SGX_MMU_PAGE_MASK),
+			 sDevPAddr.uiAddr & (SGX_MMU_PAGE_MASK),
+			 ui32WPosVal,
+			 ui32PacketSize,
+			 ui32BufferSize);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PDumpIDLWithFlags(IMG_UINT32 ui32Clocks, IMG_UINT32 ui32Flags)
+{
+	PVRSRV_ERROR eErr;
+	PDUMP_GET_SCRIPT_STRING();
+	PDUMP_DBG(("PDumpIDLWithFlags"));
+
+	eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "IDL %lu\r\n", ui32Clocks);
+	if(eErr != PVRSRV_OK)
+	{
+		return eErr;
+	}
+	PDumpOSWriteString2(hScript, ui32Flags);
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PDumpIDL(IMG_UINT32 ui32Clocks)
+{
+	return PDumpIDLWithFlags(ui32Clocks, PDUMP_FLAGS_CONTINUOUS);
+}
+#endif
+
+
+PVRSRV_ERROR PDumpMemUM(PVRSRV_PER_PROCESS_DATA *psPerProc,
+						IMG_PVOID pvAltLinAddrUM,
+						IMG_PVOID pvLinAddrUM,
+						PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+						IMG_UINT32 ui32Offset,
+						IMG_UINT32 ui32Bytes,
+						IMG_UINT32 ui32Flags,
+						IMG_HANDLE hUniqueTag)
+{
+	IMG_VOID *pvAddrUM;
+	IMG_VOID *pvAddrKM;
+	IMG_UINT32 ui32BytesDumped;
+	IMG_UINT32 ui32CurrentOffset;
+
+	if (psMemInfo->pvLinAddrKM != IMG_NULL && pvAltLinAddrUM == IMG_NULL)
+	{
+
+		return PDumpMemKM(IMG_NULL,
+					   psMemInfo,
+					   ui32Offset,
+					   ui32Bytes,
+					   ui32Flags,
+					   hUniqueTag);
+	}
+
+	pvAddrUM = (pvAltLinAddrUM != IMG_NULL) ? pvAltLinAddrUM : ((pvLinAddrUM != IMG_NULL) ? VPTR_PLUS(pvLinAddrUM, ui32Offset) : IMG_NULL);
+
+	pvAddrKM = GetTempBuffer();
+
+
+	PVR_ASSERT(pvAddrUM != IMG_NULL && pvAddrKM != IMG_NULL);
+	if (pvAddrUM == IMG_NULL || pvAddrKM == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpMemUM: Nothing to dump"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	if (ui32Bytes > PDUMP_TEMP_BUFFER_SIZE)
+	{
+		PDumpCommentWithFlags(ui32Flags, "Dumping 0x%8.8lx bytes of memory, in blocks of 0x%8.8lx bytes", ui32Bytes, (IMG_UINT32)PDUMP_TEMP_BUFFER_SIZE);
+	}
+
+	ui32CurrentOffset = ui32Offset;
+	for (ui32BytesDumped = 0; ui32BytesDumped < ui32Bytes;)
+	{
+		PVRSRV_ERROR eError;
+		IMG_UINT32 ui32BytesToDump = MIN(PDUMP_TEMP_BUFFER_SIZE, ui32Bytes - ui32BytesDumped);
+
+		eError = OSCopyFromUser(psPerProc,
+					   pvAddrKM,
+					   pvAddrUM,
+					   ui32BytesToDump);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PDumpMemUM: OSCopyFromUser failed (%d), eError"));
+			return PVRSRV_ERROR_GENERIC;
+		}
+
+		eError = PDumpMemKM(pvAddrKM,
+					   psMemInfo,
+					   ui32CurrentOffset,
+					   ui32BytesToDump,
+					   ui32Flags,
+					   hUniqueTag);
+
+		if (eError != PVRSRV_OK)
+		{
+
+			if (ui32BytesDumped != 0)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "PDumpMemUM: PDumpMemKM failed (%d)", eError));
+			}
+/*			FIXME: This assert causes kernel to crash when using PDUMP */
+/*			PVR_ASSERT(ui32BytesDumped == 0); */
+			return eError;
+		}
+
+		VPTR_INC(pvAddrUM, ui32BytesToDump);
+		ui32CurrentOffset += ui32BytesToDump;
+		ui32BytesDumped += ui32BytesToDump;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+static PVRSRV_ERROR _PdumpAllocMMUContext(IMG_UINT32 *pui32MMUContextID)
+{
+	IMG_UINT32 i;
+
+
+	for(i=0; i<MAX_PDUMP_MMU_CONTEXTS; i++)
+	{
+		if((gui16MMUContextUsage & (1U << i)) == 0)
+		{
+
+			gui16MMUContextUsage |= 1U << i;
+			*pui32MMUContextID = i;
+			return PVRSRV_OK;
+		}
+	}
+
+	PVR_DPF((PVR_DBG_ERROR, "_PdumpAllocMMUContext: no free MMU context ids"));
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+
+static PVRSRV_ERROR _PdumpFreeMMUContext(IMG_UINT32 ui32MMUContextID)
+{
+	if(ui32MMUContextID < MAX_PDUMP_MMU_CONTEXTS)
+	{
+
+		gui16MMUContextUsage &= ~(1U << ui32MMUContextID);
+		return PVRSRV_OK;
+	}
+
+	PVR_DPF((PVR_DBG_ERROR, "_PdumpFreeMMUContext: MMU context ids invalid"));
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+
+PVRSRV_ERROR PDumpSetMMUContext(PVRSRV_DEVICE_TYPE eDeviceType,
+								IMG_CHAR *pszMemSpace,
+								IMG_UINT32 *pui32MMUContextID,
+								IMG_UINT32 ui32MMUType,
+								IMG_HANDLE hUniqueTag1,
+								IMG_VOID *pvPDCPUAddr)
+{
+	IMG_UINT8 *pui8LinAddr = (IMG_UINT8 *)pvPDCPUAddr;
+	IMG_CPU_PHYADDR sCpuPAddr;
+	IMG_DEV_PHYADDR sDevPAddr;
+	IMG_UINT32 ui32MMUContextID;
+	PVRSRV_ERROR eError;
+
+	eError = _PdumpAllocMMUContext(&ui32MMUContextID);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpSetMMUContext: _PdumpAllocMMUContext failed: %d", eError));
+		return eError;
+	}
+
+
+	sCpuPAddr = OSMapLinToCPUPhys(pui8LinAddr);
+	sDevPAddr = SysCpuPAddrToDevPAddr(eDeviceType, sCpuPAddr);
+
+	sDevPAddr.uiAddr &= ~((PVRSRV_4K_PAGE_SIZE) -1);
+
+	PDumpComment("Set MMU Context\r\n");
+
+	PDumpComment("MMU :%s:v%d %d :%s:PA_%8.8lX%8.8lX\r\n",
+						pszMemSpace,
+						ui32MMUContextID,
+						ui32MMUType,
+						pszMemSpace,
+						hUniqueTag1,
+						sDevPAddr.uiAddr);
+
+
+	*pui32MMUContextID = ui32MMUContextID;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PDumpClearMMUContext(PVRSRV_DEVICE_TYPE eDeviceType,
+								IMG_CHAR *pszMemSpace,
+								IMG_UINT32 ui32MMUContextID,
+								IMG_UINT32 ui32MMUType)
+{
+	PVRSRV_ERROR eError;
+
+	PVR_UNREFERENCED_PARAMETER(eDeviceType);
+
+
+	PDumpComment("Clear MMU Context for memory space %s\r\n", pszMemSpace);
+
+	PDumpComment("MMU :%s:v%d %d\r\n",
+						pszMemSpace,
+						ui32MMUContextID,
+						ui32MMUType);
+
+	eError = _PdumpFreeMMUContext(ui32MMUContextID);
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PDumpClearMMUContext: _PdumpFreeMMUContext failed: %d", eError));
+		return eError;
+	}
+
+	return PVRSRV_OK;
+}
+
+#else
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/perproc.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/perproc.c
new file mode 100644
index 0000000..64e8faf
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/perproc.c
@@ -0,0 +1,279 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "resman.h"
+#include "handle.h"
+#include "perproc.h"
+#include "osperproc.h"
+
+#define	HASH_TAB_INIT_SIZE 32
+
+static HASH_TABLE *psHashTab = IMG_NULL;
+
+static PVRSRV_ERROR FreePerProcessData(PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_ERROR eError;
+	IMG_UINTPTR_T uiPerProc;
+
+	PVR_ASSERT(psPerProc != IMG_NULL);
+
+	if (psPerProc == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	uiPerProc = HASH_Remove(psHashTab, (IMG_UINTPTR_T)psPerProc->ui32PID);
+	if (uiPerProc == 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't find process in per-process data hash table"));
+
+		PVR_ASSERT(psPerProc->ui32PID == 0);
+	}
+	else
+	{
+		PVR_ASSERT((PVRSRV_PER_PROCESS_DATA *)uiPerProc == psPerProc);
+		PVR_ASSERT(((PVRSRV_PER_PROCESS_DATA *)uiPerProc)->ui32PID == psPerProc->ui32PID);
+	}
+
+
+	if (psPerProc->psHandleBase != IMG_NULL)
+	{
+		eError = PVRSRVFreeHandleBase(psPerProc->psHandleBase);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't free handle base for process (%d)", eError));
+			return eError;
+		}
+	}
+
+
+	if (psPerProc->hPerProcData != IMG_NULL)
+	{
+		eError = PVRSRVReleaseHandle(KERNEL_HANDLE_BASE, psPerProc->hPerProcData, PVRSRV_HANDLE_TYPE_PERPROC_DATA);
+
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't release per-process data handle (%d)", eError));
+			return eError;
+		}
+	}
+
+
+	eError = OSPerProcessPrivateDataDeInit(psPerProc->hOsPrivateData);
+	if (eError != PVRSRV_OK)
+	{
+		 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: OSPerProcessPrivateDataDeInit failed (%d)", eError));
+		return eError;
+	}
+
+	eError = OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+		sizeof(*psPerProc),
+		psPerProc,
+		psPerProc->hBlockAlloc);
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't free per-process data (%d)", eError));
+		return eError;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_PER_PROCESS_DATA *PVRSRVPerProcessData(IMG_UINT32 ui32PID)
+{
+	PVRSRV_PER_PROCESS_DATA *psPerProc;
+
+	PVR_ASSERT(psHashTab != IMG_NULL);
+
+
+	psPerProc = (PVRSRV_PER_PROCESS_DATA *)HASH_Retrieve(psHashTab, (IMG_UINTPTR_T)ui32PID);
+	return psPerProc;
+}
+
+
+PVRSRV_ERROR PVRSRVPerProcessDataConnect(IMG_UINT32	ui32PID)
+{
+	PVRSRV_PER_PROCESS_DATA *psPerProc;
+	IMG_HANDLE hBlockAlloc;
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	PVR_ASSERT(psHashTab != IMG_NULL);
+
+
+	psPerProc = (PVRSRV_PER_PROCESS_DATA *)HASH_Retrieve(psHashTab, (IMG_UINTPTR_T)ui32PID);
+
+	if (psPerProc == IMG_NULL)
+	{
+
+		eError = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+							sizeof(*psPerProc),
+							(IMG_PVOID *)&psPerProc,
+							&hBlockAlloc,
+							"Per Process Data");
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't allocate per-process data (%d)", eError));
+			return eError;
+		}
+		OSMemSet(psPerProc, 0, sizeof(*psPerProc));
+		psPerProc->hBlockAlloc = hBlockAlloc;
+
+		if (!HASH_Insert(psHashTab, (IMG_UINTPTR_T)ui32PID, (IMG_UINTPTR_T)psPerProc))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't insert per-process data into hash table"));
+			eError = PVRSRV_ERROR_GENERIC;
+			goto failure;
+		}
+
+		psPerProc->ui32PID = ui32PID;
+		psPerProc->ui32RefCount = 0;
+
+
+		eError = OSPerProcessPrivateDataInit(&psPerProc->hOsPrivateData);
+		if (eError != PVRSRV_OK)
+		{
+			 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: OSPerProcessPrivateDataInit failed (%d)", eError));
+			goto failure;
+		}
+
+
+		eError = PVRSRVAllocHandle(KERNEL_HANDLE_BASE,
+								   &psPerProc->hPerProcData,
+								   psPerProc,
+								   PVRSRV_HANDLE_TYPE_PERPROC_DATA,
+								   PVRSRV_HANDLE_ALLOC_FLAG_NONE);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't allocate handle for per-process data (%d)", eError));
+			goto failure;
+		}
+
+
+		eError = PVRSRVAllocHandleBase(&psPerProc->psHandleBase);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't allocate handle base for process (%d)", eError));
+			goto failure;
+		}
+
+
+		eError = OSPerProcessSetHandleOptions(psPerProc->psHandleBase);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't set handle options (%d)", eError));
+			goto failure;
+		}
+
+
+		eError = PVRSRVResManConnect(psPerProc, &psPerProc->hResManContext);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't register with the resource manager"));
+			goto failure;
+		}
+	}
+
+	psPerProc->ui32RefCount++;
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"PVRSRVPerProcessDataConnect: Process 0x%x has ref-count %d",
+			ui32PID, psPerProc->ui32RefCount));
+
+	return eError;
+
+failure:
+	(IMG_VOID)FreePerProcessData(psPerProc);
+	return eError;
+}
+
+
+IMG_VOID PVRSRVPerProcessDataDisconnect(IMG_UINT32	ui32PID)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_PER_PROCESS_DATA *psPerProc;
+
+	PVR_ASSERT(psHashTab != IMG_NULL);
+
+	psPerProc = (PVRSRV_PER_PROCESS_DATA *)HASH_Retrieve(psHashTab, (IMG_UINTPTR_T)ui32PID);
+	if (psPerProc == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataDealloc: Couldn't locate per-process data for PID %u", ui32PID));
+	}
+	else
+	{
+		psPerProc->ui32RefCount--;
+		if (psPerProc->ui32RefCount == 0)
+		{
+			PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVPerProcessDataDisconnect: "
+					"Last close from process 0x%x received", ui32PID));
+
+
+			PVRSRVResManDisconnect(psPerProc->hResManContext, IMG_FALSE);
+
+
+			eError = FreePerProcessData(psPerProc);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataDisconnect: Error freeing per-process data"));
+			}
+		}
+	}
+
+	eError = PVRSRVPurgeHandles(KERNEL_HANDLE_BASE);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataDisconnect: Purge of global handle pool failed (%d)", eError));
+	}
+}
+
+
+PVRSRV_ERROR PVRSRVPerProcessDataInit(IMG_VOID)
+{
+	PVR_ASSERT(psHashTab == IMG_NULL);
+
+
+	psHashTab = HASH_Create(HASH_TAB_INIT_SIZE);
+	if (psHashTab == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataInit: Couldn't create per-process data hash table"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PVRSRVPerProcessDataDeInit(IMG_VOID)
+{
+
+	if (psHashTab != IMG_NULL)
+	{
+
+		HASH_Delete(psHashTab);
+		psHashTab = IMG_NULL;
+	}
+
+	return PVRSRV_OK;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/power.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/power.c
new file mode 100644
index 0000000..b5b8585
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/power.c
@@ -0,0 +1,743 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "pdump_km.h"
+
+#include "lists.h"
+
+DECLARE_LIST_ANY_VA(PVRSRV_POWER_DEV);
+DECLARE_LIST_ANY_VA_2(PVRSRV_POWER_DEV, PVRSRV_ERROR, PVRSRV_OK);
+DECLARE_LIST_INSERT(PVRSRV_POWER_DEV);
+DECLARE_LIST_REMOVE(PVRSRV_POWER_DEV);
+
+IMG_VOID* MatchPowerDeviceIndex_AnyVaCb(PVRSRV_POWER_DEV *psPowerDev, va_list va);
+
+
+static IMG_BOOL gbInitServerRunning = IMG_FALSE;
+static IMG_BOOL gbInitServerRan = IMG_FALSE;
+static IMG_BOOL gbInitSuccessful = IMG_FALSE;
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetInitServerState(PVRSRV_INIT_SERVER_STATE eInitServerState, IMG_BOOL bState)
+{
+
+	switch(eInitServerState)
+	{
+		case PVRSRV_INIT_SERVER_RUNNING:
+			gbInitServerRunning	= bState;
+			break;
+		case PVRSRV_INIT_SERVER_RAN:
+			gbInitServerRan	= bState;
+			break;
+		case PVRSRV_INIT_SERVER_SUCCESSFUL:
+			gbInitSuccessful = bState;
+			break;
+		default:
+			PVR_DPF((PVR_DBG_ERROR,
+				"PVRSRVSetInitServerState : Unknown state %lx", eInitServerState));
+			return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+IMG_BOOL PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_STATE eInitServerState)
+{
+	IMG_BOOL	bReturnVal;
+
+	switch(eInitServerState)
+	{
+		case PVRSRV_INIT_SERVER_RUNNING:
+			bReturnVal = gbInitServerRunning;
+			break;
+		case PVRSRV_INIT_SERVER_RAN:
+			bReturnVal = gbInitServerRan;
+			break;
+		case PVRSRV_INIT_SERVER_SUCCESSFUL:
+			bReturnVal = gbInitSuccessful;
+			break;
+		default:
+			PVR_DPF((PVR_DBG_ERROR,
+				"PVRSRVGetInitServerState : Unknown state %lx", eInitServerState));
+			bReturnVal = IMG_FALSE;
+	}
+
+	return bReturnVal;
+}
+
+static IMG_BOOL _IsSystemStatePowered(PVRSRV_SYS_POWER_STATE eSystemPowerState)
+{
+	return (IMG_BOOL)(eSystemPowerState < PVRSRV_SYS_POWER_STATE_D2);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVPowerLock(IMG_UINT32	ui32CallerID,
+							 IMG_BOOL	bSystemPowerEvent)
+{
+	PVRSRV_ERROR	eError;
+	SYS_DATA		*psSysData;
+	IMG_UINT32		ui32Timeout = 1000000;
+
+#if defined(SUPPORT_LMA)
+
+	ui32Timeout *= 60;
+#endif
+
+	SysAcquireData(&psSysData);
+
+#if defined(SYS_CUSTOM_POWERLOCK_WRAP)
+	eError = SysPowerLockWrap(psSysData);
+	if (eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+#endif
+	do
+	{
+		eError = OSLockResource(&psSysData->sPowerStateChangeResource,
+								ui32CallerID);
+		if (eError == PVRSRV_OK)
+		{
+			break;
+		}
+		else if (ui32CallerID == ISR_ID)
+		{
+
+
+			eError = PVRSRV_ERROR_RETRY;
+			break;
+		}
+
+		OSWaitus(1);
+		ui32Timeout--;
+	} while (ui32Timeout > 0);
+
+#if defined(SYS_CUSTOM_POWERLOCK_WRAP)
+	if (eError != PVRSRV_OK)
+	{
+		SysPowerLockUnwrap(psSysData);
+	}
+#endif
+	if ((eError == PVRSRV_OK) &&
+		!bSystemPowerEvent &&
+		!_IsSystemStatePowered(psSysData->eCurrentPowerState))
+	{
+
+		PVRSRVPowerUnlock(ui32CallerID);
+		eError = PVRSRV_ERROR_RETRY;
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+IMG_VOID PVRSRVPowerUnlock(IMG_UINT32	ui32CallerID)
+{
+	OSUnlockResource(&gpsSysData->sPowerStateChangeResource, ui32CallerID);
+#if defined(SYS_CUSTOM_POWERLOCK_WRAP)
+	SysPowerLockUnwrap(gpsSysData);
+#endif
+}
+
+
+PVRSRV_ERROR PVRSRVDevicePrePowerStateKM_AnyVaCb(PVRSRV_POWER_DEV *psPowerDevice, va_list va)
+{
+	PVRSRV_DEV_POWER_STATE	eNewDevicePowerState;
+	PVRSRV_ERROR			eError;
+
+
+	IMG_BOOL				bAllDevices;
+	IMG_UINT32				ui32DeviceIndex;
+	PVRSRV_DEV_POWER_STATE	eNewPowerState;
+
+
+	bAllDevices = va_arg(va, IMG_BOOL);
+	ui32DeviceIndex = va_arg(va, IMG_UINT32);
+	eNewPowerState = va_arg(va, PVRSRV_DEV_POWER_STATE);
+
+	if (bAllDevices || (ui32DeviceIndex == psPowerDevice->ui32DeviceIndex))
+	{
+		eNewDevicePowerState = (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT) ?
+							psPowerDevice->eDefaultPowerState : eNewPowerState;
+
+		if (psPowerDevice->eCurrentPowerState != eNewDevicePowerState)
+		{
+			if (psPowerDevice->pfnPrePower != IMG_NULL)
+			{
+
+				eError = psPowerDevice->pfnPrePower(psPowerDevice->hDevCookie,
+													eNewDevicePowerState,
+													psPowerDevice->eCurrentPowerState);
+				if (eError != PVRSRV_OK)
+				{
+					return eError;
+				}
+			}
+
+
+			eError = SysDevicePrePowerState(psPowerDevice->ui32DeviceIndex,
+											eNewDevicePowerState,
+											psPowerDevice->eCurrentPowerState);
+			if (eError != PVRSRV_OK)
+			{
+				return eError;
+			}
+		}
+	}
+
+	return  PVRSRV_OK;
+}
+
+static
+PVRSRV_ERROR PVRSRVDevicePrePowerStateKM(IMG_BOOL				bAllDevices,
+										 IMG_UINT32				ui32DeviceIndex,
+										 PVRSRV_DEV_POWER_STATE	eNewPowerState)
+{
+	PVRSRV_ERROR		eError;
+	SYS_DATA			*psSysData;
+
+	SysAcquireData(&psSysData);
+
+
+	eError = List_PVRSRV_POWER_DEV_PVRSRV_ERROR_Any_va(psSysData->psPowerDeviceList,
+														PVRSRVDevicePrePowerStateKM_AnyVaCb,
+														bAllDevices,
+														ui32DeviceIndex,
+														eNewPowerState);
+
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVDevicePostPowerStateKM_AnyVaCb(PVRSRV_POWER_DEV *psPowerDevice, va_list va)
+{
+	PVRSRV_DEV_POWER_STATE	eNewDevicePowerState;
+	PVRSRV_ERROR			eError;
+
+
+	IMG_BOOL				bAllDevices;
+	IMG_UINT32				ui32DeviceIndex;
+	PVRSRV_DEV_POWER_STATE	eNewPowerState;
+
+
+	bAllDevices = va_arg(va, IMG_BOOL);
+	ui32DeviceIndex = va_arg(va, IMG_UINT32);
+	eNewPowerState = va_arg(va, PVRSRV_DEV_POWER_STATE);
+
+	if (bAllDevices || (ui32DeviceIndex == psPowerDevice->ui32DeviceIndex))
+	{
+		eNewDevicePowerState = (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT) ?
+								psPowerDevice->eDefaultPowerState : eNewPowerState;
+
+		if (psPowerDevice->eCurrentPowerState != eNewDevicePowerState)
+		{
+
+			eError = SysDevicePostPowerState(psPowerDevice->ui32DeviceIndex,
+											 eNewDevicePowerState,
+											 psPowerDevice->eCurrentPowerState);
+			if (eError != PVRSRV_OK)
+			{
+				return eError;
+			}
+
+			if (psPowerDevice->pfnPostPower != IMG_NULL)
+			{
+
+				eError = psPowerDevice->pfnPostPower(psPowerDevice->hDevCookie,
+													 eNewDevicePowerState,
+													 psPowerDevice->eCurrentPowerState);
+				if (eError != PVRSRV_OK)
+				{
+					return eError;
+				}
+			}
+
+			psPowerDevice->eCurrentPowerState = eNewDevicePowerState;
+		}
+	}
+	return PVRSRV_OK;
+}
+
+static
+PVRSRV_ERROR PVRSRVDevicePostPowerStateKM(IMG_BOOL					bAllDevices,
+										  IMG_UINT32				ui32DeviceIndex,
+										  PVRSRV_DEV_POWER_STATE	eNewPowerState)
+{
+	PVRSRV_ERROR		eError;
+	SYS_DATA			*psSysData;
+
+	SysAcquireData(&psSysData);
+
+
+	eError = List_PVRSRV_POWER_DEV_PVRSRV_ERROR_Any_va(psSysData->psPowerDeviceList,
+														PVRSRVDevicePostPowerStateKM_AnyVaCb,
+														bAllDevices,
+														ui32DeviceIndex,
+														eNewPowerState);
+
+	return eError;
+}
+
+
+PVRSRV_ERROR PVRSRVSetDevicePowerStateCoreKM(IMG_UINT32				ui32DeviceIndex,
+                                             PVRSRV_DEV_POWER_STATE	eNewPowerState)
+{
+	PVRSRV_ERROR	eError;
+	eError = PVRSRVDevicePrePowerStateKM(IMG_FALSE, ui32DeviceIndex, eNewPowerState);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	eError = PVRSRVDevicePostPowerStateKM(IMG_FALSE, ui32DeviceIndex, eNewPowerState);
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetDevicePowerStateKM(IMG_UINT32				ui32DeviceIndex,
+										 PVRSRV_DEV_POWER_STATE	eNewPowerState,
+										 IMG_UINT32				ui32CallerID,
+										 IMG_BOOL				bRetainMutex)
+{
+	PVRSRV_ERROR	eError;
+	SYS_DATA		*psSysData;
+
+	SysAcquireData(&psSysData);
+
+	eError = PVRSRVPowerLock(ui32CallerID, IMG_FALSE);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	#if defined(PDUMP)
+	if (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT)
+	{
+
+
+
+
+		eError = PVRSRVDevicePrePowerStateKM(IMG_FALSE, ui32DeviceIndex, PVRSRV_DEV_POWER_STATE_ON);
+		if(eError != PVRSRV_OK)
+		{
+			goto Exit;
+		}
+
+		eError = PVRSRVDevicePostPowerStateKM(IMG_FALSE, ui32DeviceIndex, PVRSRV_DEV_POWER_STATE_ON);
+
+		if (eError != PVRSRV_OK)
+		{
+			goto Exit;
+		}
+
+		PDUMPSUSPEND();
+	}
+	#endif
+
+	eError = PVRSRVDevicePrePowerStateKM(IMG_FALSE, ui32DeviceIndex, eNewPowerState);
+	if(eError != PVRSRV_OK)
+	{
+		if (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT)
+		{
+			PDUMPRESUME();
+		}
+		goto Exit;
+	}
+
+	eError = PVRSRVDevicePostPowerStateKM(IMG_FALSE, ui32DeviceIndex, eNewPowerState);
+
+	if (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT)
+	{
+		PDUMPRESUME();
+	}
+
+Exit:
+
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+				"PVRSRVSetDevicePowerStateKM : Transition to %d FAILED 0x%x", eNewPowerState, eError));
+	}
+
+	if (!bRetainMutex || (eError != PVRSRV_OK))
+	{
+		PVRSRVPowerUnlock(ui32CallerID);
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSystemPrePowerStateKM(PVRSRV_SYS_POWER_STATE eNewSysPowerState)
+{
+	PVRSRV_ERROR			eError;
+	SYS_DATA				*psSysData;
+	PVRSRV_DEV_POWER_STATE	eNewDevicePowerState;
+
+	SysAcquireData(&psSysData);
+
+
+	eError = PVRSRVPowerLock(KERNEL_ID, IMG_TRUE);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	if (_IsSystemStatePowered(eNewSysPowerState) !=
+		_IsSystemStatePowered(psSysData->eCurrentPowerState))
+	{
+		if (_IsSystemStatePowered(eNewSysPowerState))
+		{
+
+			eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_DEFAULT;
+		}
+		else
+		{
+			eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_OFF;
+		}
+
+
+		eError = PVRSRVDevicePrePowerStateKM(IMG_TRUE, 0, eNewDevicePowerState);
+		if (eError != PVRSRV_OK)
+		{
+			goto ErrorExit;
+		}
+	}
+
+	if (eNewSysPowerState != psSysData->eCurrentPowerState)
+	{
+
+		eError = SysSystemPrePowerState(eNewSysPowerState);
+		if (eError != PVRSRV_OK)
+		{
+			goto ErrorExit;
+		}
+	}
+
+	return eError;
+
+ErrorExit:
+
+	PVR_DPF((PVR_DBG_ERROR,
+			"PVRSRVSystemPrePowerStateKM: Transition from %d to %d FAILED 0x%x",
+			psSysData->eCurrentPowerState, eNewSysPowerState, eError));
+
+
+	psSysData->eFailedPowerState = eNewSysPowerState;
+
+	PVRSRVPowerUnlock(KERNEL_ID);
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSystemPostPowerStateKM(PVRSRV_SYS_POWER_STATE eNewSysPowerState)
+{
+	PVRSRV_ERROR			eError = PVRSRV_OK;
+	SYS_DATA				*psSysData;
+	PVRSRV_DEV_POWER_STATE	eNewDevicePowerState;
+
+	SysAcquireData(&psSysData);
+
+	if (eNewSysPowerState != psSysData->eCurrentPowerState)
+	{
+
+		eError = SysSystemPostPowerState(eNewSysPowerState);
+		if (eError != PVRSRV_OK)
+		{
+			goto Exit;
+		}
+	}
+
+	if (_IsSystemStatePowered(eNewSysPowerState) !=
+		_IsSystemStatePowered(psSysData->eCurrentPowerState))
+	{
+		if (_IsSystemStatePowered(eNewSysPowerState))
+		{
+
+			eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_DEFAULT;
+		}
+		else
+		{
+			eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_OFF;
+		}
+
+
+		eError = PVRSRVDevicePostPowerStateKM(IMG_TRUE, 0, eNewDevicePowerState);
+		if (eError != PVRSRV_OK)
+		{
+			goto Exit;
+		}
+	}
+
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"PVRSRVSystemPostPowerStateKM: System Power Transition from %d to %d OK",
+			psSysData->eCurrentPowerState, eNewSysPowerState));
+
+	psSysData->eCurrentPowerState = eNewSysPowerState;
+
+Exit:
+
+	PVRSRVPowerUnlock(KERNEL_ID);
+
+	if (_IsSystemStatePowered(eNewSysPowerState) &&
+			PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL))
+	{
+
+
+
+		PVRSRVCommandCompleteCallbacks();
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE eNewSysPowerState)
+{
+	PVRSRV_ERROR	eError;
+	SYS_DATA		*psSysData;
+
+	SysAcquireData(&psSysData);
+
+	eError = PVRSRVSystemPrePowerStateKM(eNewSysPowerState);
+	if(eError != PVRSRV_OK)
+	{
+		goto ErrorExit;
+	}
+
+	eError = PVRSRVSystemPostPowerStateKM(eNewSysPowerState);
+	if(eError != PVRSRV_OK)
+	{
+		goto ErrorExit;
+	}
+
+
+	psSysData->eFailedPowerState = PVRSRV_SYS_POWER_STATE_Unspecified;
+
+	return PVRSRV_OK;
+
+ErrorExit:
+
+	PVR_DPF((PVR_DBG_ERROR,
+			"PVRSRVSetPowerStateKM: Transition from %d to %d FAILED 0x%x",
+			psSysData->eCurrentPowerState, eNewSysPowerState, eError));
+
+
+	psSysData->eFailedPowerState = eNewSysPowerState;
+
+	return eError;
+}
+
+
+PVRSRV_ERROR PVRSRVRegisterPowerDevice(IMG_UINT32					ui32DeviceIndex,
+									   PFN_PRE_POWER				pfnPrePower,
+									   PFN_POST_POWER				pfnPostPower,
+									   PFN_PRE_CLOCKSPEED_CHANGE	pfnPreClockSpeedChange,
+									   PFN_POST_CLOCKSPEED_CHANGE	pfnPostClockSpeedChange,
+									   IMG_HANDLE					hDevCookie,
+									   PVRSRV_DEV_POWER_STATE		eCurrentPowerState,
+									   PVRSRV_DEV_POWER_STATE		eDefaultPowerState)
+{
+	PVRSRV_ERROR		eError;
+	SYS_DATA			*psSysData;
+	PVRSRV_POWER_DEV	*psPowerDevice;
+
+	if (pfnPrePower == IMG_NULL &&
+		pfnPostPower == IMG_NULL)
+	{
+		return PVRSRVRemovePowerDevice(ui32DeviceIndex);
+	}
+
+	SysAcquireData(&psSysData);
+
+	eError = OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+						 sizeof(PVRSRV_POWER_DEV),
+						 (IMG_VOID **)&psPowerDevice, IMG_NULL,
+						 "Power Device");
+	if(eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterPowerDevice: Failed to alloc PVRSRV_POWER_DEV"));
+		return eError;
+	}
+
+
+	psPowerDevice->pfnPrePower = pfnPrePower;
+	psPowerDevice->pfnPostPower = pfnPostPower;
+	psPowerDevice->pfnPreClockSpeedChange = pfnPreClockSpeedChange;
+	psPowerDevice->pfnPostClockSpeedChange = pfnPostClockSpeedChange;
+	psPowerDevice->hDevCookie = hDevCookie;
+	psPowerDevice->ui32DeviceIndex = ui32DeviceIndex;
+	psPowerDevice->eCurrentPowerState = eCurrentPowerState;
+	psPowerDevice->eDefaultPowerState = eDefaultPowerState;
+
+
+	List_PVRSRV_POWER_DEV_Insert(&(psSysData->psPowerDeviceList), psPowerDevice);
+
+	return (PVRSRV_OK);
+}
+
+
+PVRSRV_ERROR PVRSRVRemovePowerDevice (IMG_UINT32 ui32DeviceIndex)
+{
+	SYS_DATA			*psSysData;
+	PVRSRV_POWER_DEV	*psPowerDev;
+
+	SysAcquireData(&psSysData);
+
+
+	psPowerDev = (PVRSRV_POWER_DEV*)
+					List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
+												 MatchPowerDeviceIndex_AnyVaCb,
+												 ui32DeviceIndex);
+
+	if (psPowerDev)
+	{
+		List_PVRSRV_POWER_DEV_Remove(psPowerDev);
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_POWER_DEV), psPowerDev, IMG_NULL);
+
+	}
+
+	return (PVRSRV_OK);
+}
+
+
+IMG_EXPORT
+IMG_BOOL PVRSRVIsDevicePowered(IMG_UINT32 ui32DeviceIndex)
+{
+	SYS_DATA			*psSysData;
+	PVRSRV_POWER_DEV	*psPowerDevice;
+
+	SysAcquireData(&psSysData);
+
+	if (OSIsResourceLocked(&psSysData->sPowerStateChangeResource, KERNEL_ID) ||
+		OSIsResourceLocked(&psSysData->sPowerStateChangeResource, ISR_ID))
+	{
+		return IMG_FALSE;
+	}
+
+	psPowerDevice = (PVRSRV_POWER_DEV*)
+					List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
+												 MatchPowerDeviceIndex_AnyVaCb,
+												 ui32DeviceIndex);
+	return (psPowerDevice && (psPowerDevice->eCurrentPowerState == PVRSRV_DEV_POWER_STATE_ON))
+			? IMG_TRUE : IMG_FALSE;
+}
+
+
+PVRSRV_ERROR PVRSRVDevicePreClockSpeedChange(IMG_UINT32	ui32DeviceIndex,
+											 IMG_BOOL	bIdleDevice,
+											 IMG_VOID	*pvInfo)
+{
+	PVRSRV_ERROR		eError = PVRSRV_OK;
+	SYS_DATA			*psSysData;
+	PVRSRV_POWER_DEV	*psPowerDevice;
+
+	PVR_UNREFERENCED_PARAMETER(pvInfo);
+
+	SysAcquireData(&psSysData);
+
+	if (bIdleDevice)
+	{
+
+		eError = PVRSRVPowerLock(KERNEL_ID, IMG_FALSE);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,	"PVRSRVDevicePreClockSpeedChange : failed to acquire lock, error:0x%lx", eError));
+			return eError;
+		}
+	}
+
+
+	psPowerDevice = (PVRSRV_POWER_DEV*)
+					List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
+												 MatchPowerDeviceIndex_AnyVaCb,
+												 ui32DeviceIndex);
+
+	if (psPowerDevice && psPowerDevice->pfnPostClockSpeedChange)
+	{
+			eError = psPowerDevice->pfnPreClockSpeedChange(psPowerDevice->hDevCookie,
+														   bIdleDevice,
+														   psPowerDevice->eCurrentPowerState);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,
+						"PVRSRVDevicePreClockSpeedChange : Device %lu failed, error:0x%lx",
+						ui32DeviceIndex, eError));
+			}
+	}
+
+	if (bIdleDevice && eError != PVRSRV_OK)
+	{
+		PVRSRVPowerUnlock(KERNEL_ID);
+	}
+
+	return eError;
+}
+
+
+IMG_VOID PVRSRVDevicePostClockSpeedChange(IMG_UINT32	ui32DeviceIndex,
+										  IMG_BOOL		bIdleDevice,
+										  IMG_VOID		*pvInfo)
+{
+	PVRSRV_ERROR		eError;
+	SYS_DATA			*psSysData;
+	PVRSRV_POWER_DEV	*psPowerDevice;
+
+	PVR_UNREFERENCED_PARAMETER(pvInfo);
+
+	SysAcquireData(&psSysData);
+
+
+	psPowerDevice = (PVRSRV_POWER_DEV*)
+					List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
+												 MatchPowerDeviceIndex_AnyVaCb,
+												 ui32DeviceIndex);
+
+	if (psPowerDevice && psPowerDevice->pfnPostClockSpeedChange)
+	{
+		eError = psPowerDevice->pfnPostClockSpeedChange(psPowerDevice->hDevCookie,
+														bIdleDevice,
+														psPowerDevice->eCurrentPowerState);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,
+					"PVRSRVDevicePostClockSpeedChange : Device %lu failed, error:0x%lx",
+					ui32DeviceIndex, eError));
+		}
+	}
+
+
+	if (bIdleDevice)
+	{
+
+		PVRSRVPowerUnlock(KERNEL_ID);
+	}
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pvrsrv.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pvrsrv.c
new file mode 100644
index 0000000..82cc4dd
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/pvrsrv.c
@@ -0,0 +1,1194 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "handle.h"
+#include "perproc.h"
+#include "pdump_km.h"
+#include "ra.h"
+
+#include "pvrversion.h"
+
+#include "lists.h"
+
+
+DECLARE_LIST_ANY_VA_2(BM_CONTEXT, PVRSRV_ERROR, PVRSRV_OK);
+
+DECLARE_LIST_FOR_EACH_VA(BM_HEAP);
+
+DECLARE_LIST_ANY_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK);
+DECLARE_LIST_ANY_VA(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_ANY_VA_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK);
+DECLARE_LIST_FOR_EACH_VA(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_FOR_EACH(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_INSERT(PVRSRV_DEVICE_NODE);
+DECLARE_LIST_REMOVE(PVRSRV_DEVICE_NODE);
+
+IMG_VOID* MatchDeviceKM_AnyVaCb(PVRSRV_DEVICE_NODE* psDeviceNode, va_list va);
+
+
+PVRSRV_ERROR AllocateDeviceID(SYS_DATA *psSysData, IMG_UINT32 *pui32DevID)
+{
+	SYS_DEVICE_ID* psDeviceWalker;
+	SYS_DEVICE_ID* psDeviceEnd;
+
+	psDeviceWalker = &psSysData->sDeviceID[0];
+	psDeviceEnd = psDeviceWalker + psSysData->ui32NumDevices;
+
+
+	while (psDeviceWalker < psDeviceEnd)
+	{
+		if (!psDeviceWalker->bInUse)
+		{
+			psDeviceWalker->bInUse = IMG_TRUE;
+			*pui32DevID = psDeviceWalker->uiID;
+			return PVRSRV_OK;
+		}
+		psDeviceWalker++;
+	}
+
+	PVR_DPF((PVR_DBG_ERROR,"AllocateDeviceID: No free and valid device IDs "
+			 "available!\nPerhaps need to increase SYS_DEVICE_COUNT (in "
+			 "\"sysinfo.h\")."));
+
+
+	PVR_ASSERT(psDeviceWalker < psDeviceEnd);
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+
+PVRSRV_ERROR FreeDeviceID(SYS_DATA *psSysData, IMG_UINT32 ui32DevID)
+{
+	SYS_DEVICE_ID* psDeviceWalker;
+	SYS_DEVICE_ID* psDeviceEnd;
+
+	psDeviceWalker = &psSysData->sDeviceID[0];
+	psDeviceEnd = psDeviceWalker + psSysData->ui32NumDevices;
+
+
+	while (psDeviceWalker < psDeviceEnd)
+	{
+
+		if	(
+				(psDeviceWalker->uiID == ui32DevID) &&
+				(psDeviceWalker->bInUse)
+			)
+		{
+			psDeviceWalker->bInUse = IMG_FALSE;
+			return PVRSRV_OK;
+		}
+		psDeviceWalker++;
+	}
+
+	PVR_DPF((PVR_DBG_ERROR,"FreeDeviceID: no matching dev ID that is in use!"));
+
+
+	PVR_ASSERT(psDeviceWalker < psDeviceEnd);
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+
+#ifndef ReadHWReg
+IMG_EXPORT
+IMG_UINT32 ReadHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset)
+{
+	return *(volatile IMG_UINT32*)((IMG_UINTPTR_T)pvLinRegBaseAddr+ui32Offset);
+}
+#endif
+
+
+#ifndef WriteHWReg
+IMG_EXPORT
+IMG_VOID WriteHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset, IMG_UINT32 ui32Value)
+{
+	PVR_DPF((PVR_DBG_MESSAGE,"WriteHWReg Base:%x, Offset: %x, Value %x",pvLinRegBaseAddr,ui32Offset,ui32Value));
+
+	*(IMG_UINT32*)((IMG_UINTPTR_T)pvLinRegBaseAddr+ui32Offset) = ui32Value;
+}
+#endif
+
+
+#ifndef WriteHWRegs
+IMG_EXPORT
+IMG_VOID WriteHWRegs(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Count, PVRSRV_HWREG *psHWRegs)
+{
+	while (ui32Count)
+	{
+		WriteHWReg (pvLinRegBaseAddr, psHWRegs->ui32RegAddr, psHWRegs->ui32RegVal);
+		psHWRegs++;
+		ui32Count--;
+	}
+}
+#endif
+
+IMG_VOID PVRSRVEnumerateDevicesKM_ForEachVaCb(PVRSRV_DEVICE_NODE *psDeviceNode, va_list va)
+{
+	IMG_UINT *pui32DevCount;
+	PVRSRV_DEVICE_IDENTIFIER **ppsDevIdList;
+
+	pui32DevCount = va_arg(va, IMG_UINT*);
+	ppsDevIdList = va_arg(va, PVRSRV_DEVICE_IDENTIFIER**);
+
+	if (psDeviceNode->sDevId.eDeviceType != PVRSRV_DEVICE_TYPE_EXT)
+	{
+		*(*ppsDevIdList) = psDeviceNode->sDevId;
+		(*ppsDevIdList)++;
+		(*pui32DevCount)++;
+	}
+}
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVEnumerateDevicesKM(IMG_UINT32 *pui32NumDevices,
+											 	   PVRSRV_DEVICE_IDENTIFIER *psDevIdList)
+{
+	SYS_DATA			*psSysData;
+	IMG_UINT32 			i;
+
+	if (!pui32NumDevices || !psDevIdList)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVEnumerateDevicesKM: Invalid params"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	SysAcquireData(&psSysData);
+
+
+
+	for (i=0; i<PVRSRV_MAX_DEVICES; i++)
+	{
+		psDevIdList[i].eDeviceType = PVRSRV_DEVICE_TYPE_UNKNOWN;
+	}
+
+
+	*pui32NumDevices = 0;
+
+
+
+
+
+	List_PVRSRV_DEVICE_NODE_ForEach_va(psSysData->psDeviceNodeList,
+									   PVRSRVEnumerateDevicesKM_ForEachVaCb,
+									   pui32NumDevices,
+									   &psDevIdList);
+
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVInit(PSYS_DATA psSysData)
+{
+	PVRSRV_ERROR	eError;
+
+
+	eError = ResManInit();
+	if (eError != PVRSRV_OK)
+	{
+		goto Error;
+	}
+
+	eError = PVRSRVPerProcessDataInit();
+	if(eError != PVRSRV_OK)
+	{
+		goto Error;
+	}
+
+
+	eError = PVRSRVHandleInit();
+	if(eError != PVRSRV_OK)
+	{
+		goto Error;
+	}
+
+
+	eError = OSCreateResource(&psSysData->sPowerStateChangeResource);
+	if (eError != PVRSRV_OK)
+	{
+		goto Error;
+	}
+
+
+	psSysData->eCurrentPowerState = PVRSRV_SYS_POWER_STATE_D0;
+	psSysData->eFailedPowerState = PVRSRV_SYS_POWER_STATE_Unspecified;
+
+
+	if(OSAllocMem( PVRSRV_PAGEABLE_SELECT,
+					 sizeof(PVRSRV_EVENTOBJECT) ,
+					 (IMG_VOID **)&psSysData->psGlobalEventObject, 0,
+					 "Event Object") != PVRSRV_OK)
+	{
+
+		goto Error;
+	}
+
+	if(OSEventObjectCreate("PVRSRV_GLOBAL_EVENTOBJECT", psSysData->psGlobalEventObject) != PVRSRV_OK)
+	{
+		goto Error;
+	}
+
+	return eError;
+
+Error:
+	PVRSRVDeInit(psSysData);
+	return eError;
+}
+
+
+
+IMG_VOID IMG_CALLCONV PVRSRVDeInit(PSYS_DATA psSysData)
+{
+	PVRSRV_ERROR	eError;
+
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+
+	if (psSysData == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeInit: PVRSRVHandleDeInit failed - invalid param"));
+		return;
+	}
+
+
+	if(psSysData->psGlobalEventObject)
+	{
+		OSEventObjectDestroy(psSysData->psGlobalEventObject);
+		OSFreeMem( PVRSRV_PAGEABLE_SELECT,
+						 sizeof(PVRSRV_EVENTOBJECT),
+						 psSysData->psGlobalEventObject,
+						 0);
+		psSysData->psGlobalEventObject = IMG_NULL;
+	}
+
+	eError = PVRSRVHandleDeInit();
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeInit: PVRSRVHandleDeInit failed"));
+	}
+
+	eError = PVRSRVPerProcessDataDeInit();
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeInit: PVRSRVPerProcessDataDeInit failed"));
+	}
+
+	ResManDeInit();
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVRegisterDevice(PSYS_DATA psSysData,
+											  PVRSRV_ERROR (*pfnRegisterDevice)(PVRSRV_DEVICE_NODE*),
+											  IMG_UINT32 ui32SOCInterruptBit,
+			 								  IMG_UINT32 *pui32DeviceIndex)
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+
+
+	if(OSAllocMem( PVRSRV_OS_NON_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_DEVICE_NODE),
+					 (IMG_VOID **)&psDeviceNode, IMG_NULL,
+					 "Device Node") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterDevice : Failed to alloc memory for psDeviceNode"));
+		return (PVRSRV_ERROR_OUT_OF_MEMORY);
+	}
+	OSMemSet (psDeviceNode, 0, sizeof(PVRSRV_DEVICE_NODE));
+
+	eError = pfnRegisterDevice(psDeviceNode);
+	if (eError != PVRSRV_OK)
+	{
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					sizeof(PVRSRV_DEVICE_NODE), psDeviceNode, IMG_NULL);
+
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterDevice : Failed to register device"));
+		return (PVRSRV_ERROR_DEVICE_REGISTER_FAILED);
+	}
+
+
+
+
+
+
+	psDeviceNode->ui32RefCount = 1;
+	psDeviceNode->psSysData = psSysData;
+	psDeviceNode->ui32SOCInterruptBit = ui32SOCInterruptBit;
+
+
+	AllocateDeviceID(psSysData, &psDeviceNode->sDevId.ui32DeviceIndex);
+
+
+	List_PVRSRV_DEVICE_NODE_Insert(&psSysData->psDeviceNodeList, psDeviceNode);
+
+
+	*pui32DeviceIndex = psDeviceNode->sDevId.ui32DeviceIndex;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVInitialiseDevice (IMG_UINT32 ui32DevIndex)
+{
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+	SYS_DATA			*psSysData;
+	PVRSRV_ERROR		eError;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVInitialiseDevice"));
+
+	SysAcquireData(&psSysData);
+
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)
+					 List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+													MatchDeviceKM_AnyVaCb,
+													ui32DevIndex,
+													IMG_TRUE);
+	if(!psDeviceNode)
+	{
+
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVInitialiseDevice: requested device is not present"));
+		return PVRSRV_ERROR_INIT_FAILURE;
+	}
+	PVR_ASSERT (psDeviceNode->ui32RefCount > 0);
+
+
+
+	eError = PVRSRVResManConnect(IMG_NULL, &psDeviceNode->hResManContext);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVInitialiseDevice: Failed PVRSRVResManConnect call"));
+		return eError;
+	}
+
+
+	if(psDeviceNode->pfnInitDevice != IMG_NULL)
+	{
+		eError = psDeviceNode->pfnInitDevice(psDeviceNode);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVInitialiseDevice: Failed InitDevice call"));
+			return eError;
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PVRSRVFinaliseSystem_SetPowerState_AnyCb(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	PVRSRV_ERROR eError;
+	eError = PVRSRVSetDevicePowerStateKM(psDeviceNode->sDevId.ui32DeviceIndex,
+										 PVRSRV_DEV_POWER_STATE_DEFAULT,
+										 KERNEL_ID, IMG_FALSE);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVFinaliseSystem: Failed PVRSRVSetDevicePowerStateKM call (device index: %d)", psDeviceNode->sDevId.ui32DeviceIndex));
+	}
+	return eError;
+}
+
+PVRSRV_ERROR PVRSRVFinaliseSystem_CompatCheck_AnyCb(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	PVRSRV_ERROR eError;
+	eError = PVRSRVDevInitCompatCheck(psDeviceNode);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVFinaliseSystem: Failed PVRSRVDevInitCompatCheck call (device index: %d)", psDeviceNode->sDevId.ui32DeviceIndex));
+	}
+	return eError;
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFinaliseSystem(IMG_BOOL bInitSuccessful)
+{
+	SYS_DATA		*psSysData;
+	PVRSRV_ERROR		eError;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVFinaliseSystem"));
+
+	SysAcquireData(&psSysData);
+
+	if (bInitSuccessful)
+	{
+		eError = SysFinalise();
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVFinaliseSystem: SysFinalise failed (%d)", eError));
+			return eError;
+		}
+
+
+		eError = List_PVRSRV_DEVICE_NODE_PVRSRV_ERROR_Any(psSysData->psDeviceNodeList,
+														PVRSRVFinaliseSystem_SetPowerState_AnyCb);
+		if (eError != PVRSRV_OK)
+		{
+			return eError;
+		}
+
+
+		eError = List_PVRSRV_DEVICE_NODE_PVRSRV_ERROR_Any(psSysData->psDeviceNodeList,
+													PVRSRVFinaliseSystem_CompatCheck_AnyCb);
+		if (eError != PVRSRV_OK)
+		{
+			return eError;
+		}
+	}
+
+
+
+
+
+
+
+
+#if !defined(SUPPORT_PDUMP_DELAYED_INITPHASE_TERMINATION)
+	PDUMPENDINITPHASE();
+#endif
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PVRSRVDevInitCompatCheck(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+
+	if (psDeviceNode->pfnInitDeviceCompatCheck)
+		return psDeviceNode->pfnInitDeviceCompatCheck(psDeviceNode);
+	else
+		return PVRSRV_OK;
+}
+
+IMG_VOID * PVRSRVAcquireDeviceDataKM_Match_AnyVaCb(PVRSRV_DEVICE_NODE *psDeviceNode, va_list va)
+{
+	PVRSRV_DEVICE_TYPE eDeviceType;
+	IMG_UINT32 ui32DevIndex;
+
+	eDeviceType = va_arg(va, PVRSRV_DEVICE_TYPE);
+	ui32DevIndex = va_arg(va, IMG_UINT32);
+
+	if ((eDeviceType != PVRSRV_DEVICE_TYPE_UNKNOWN &&
+		psDeviceNode->sDevId.eDeviceType == eDeviceType) ||
+		(eDeviceType == PVRSRV_DEVICE_TYPE_UNKNOWN &&
+		 psDeviceNode->sDevId.ui32DeviceIndex == ui32DevIndex))
+	{
+		return psDeviceNode;
+	}
+	else
+	{
+		return IMG_NULL;
+	}
+}
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVAcquireDeviceDataKM (IMG_UINT32			ui32DevIndex,
+													 PVRSRV_DEVICE_TYPE	eDeviceType,
+													 IMG_HANDLE			*phDevCookie)
+{
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+	SYS_DATA			*psSysData;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVAcquireDeviceDataKM"));
+
+	SysAcquireData(&psSysData);
+
+
+	psDeviceNode = List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+												PVRSRVAcquireDeviceDataKM_Match_AnyVaCb,
+												eDeviceType,
+												ui32DevIndex);
+
+
+	if (!psDeviceNode)
+	{
+
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVAcquireDeviceDataKM: requested device is not present"));
+		return PVRSRV_ERROR_INIT_FAILURE;
+	}
+
+	PVR_ASSERT (psDeviceNode->ui32RefCount > 0);
+
+
+	if (phDevCookie)
+	{
+		*phDevCookie = (IMG_HANDLE)psDeviceNode;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDeinitialiseDevice(IMG_UINT32 ui32DevIndex)
+{
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+	SYS_DATA			*psSysData;
+	PVRSRV_ERROR		eError;
+
+	SysAcquireData(&psSysData);
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)
+					 List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+													MatchDeviceKM_AnyVaCb,
+													ui32DevIndex,
+													IMG_TRUE);
+
+	if (!psDeviceNode)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeinitialiseDevice: requested device %d is not present", ui32DevIndex));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+
+	eError = PVRSRVSetDevicePowerStateKM(ui32DevIndex,
+										 PVRSRV_DEV_POWER_STATE_OFF,
+										 KERNEL_ID,
+										 IMG_FALSE);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeinitialiseDevice: Failed PVRSRVSetDevicePowerStateKM call"));
+		return eError;
+	}
+
+
+
+	eError = ResManFreeResByCriteria(psDeviceNode->hResManContext,
+									 RESMAN_CRITERIA_RESTYPE,
+									 RESMAN_TYPE_DEVICEMEM_ALLOCATION,
+									 IMG_NULL, 0);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeinitialiseDevice: Failed ResManFreeResByCriteria call"));
+		return eError;
+	}
+
+
+
+	if(psDeviceNode->pfnDeInitDevice != IMG_NULL)
+	{
+		eError = psDeviceNode->pfnDeInitDevice(psDeviceNode);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVDeinitialiseDevice: Failed DeInitDevice call"));
+			return eError;
+		}
+	}
+
+
+
+	PVRSRVResManDisconnect(psDeviceNode->hResManContext, IMG_TRUE);
+	psDeviceNode->hResManContext = IMG_NULL;
+
+
+	List_PVRSRV_DEVICE_NODE_Remove(psDeviceNode);
+
+
+	(IMG_VOID)FreeDeviceID(psSysData, ui32DevIndex);
+	OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				sizeof(PVRSRV_DEVICE_NODE), psDeviceNode, IMG_NULL);
+
+
+	return (PVRSRV_OK);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PollForValueKM (volatile IMG_UINT32* pui32LinMemAddr,
+										  IMG_UINT32 ui32Value,
+										  IMG_UINT32 ui32Mask,
+										  IMG_UINT32 ui32Waitus,
+										  IMG_UINT32 ui32Tries)
+{
+	{
+		IMG_UINT32	uiMaxTime = ui32Tries * ui32Waitus;
+
+		LOOP_UNTIL_TIMEOUT(uiMaxTime)
+		{
+			if((*pui32LinMemAddr & ui32Mask) == ui32Value)
+			{
+				return PVRSRV_OK;
+			}
+			OSWaitus(ui32Waitus);
+		} END_LOOP_UNTIL_TIMEOUT();
+	}
+
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+
+#if defined (USING_ISR_INTERRUPTS)
+
+extern IMG_UINT32 gui32EventStatusServicesByISR;
+
+PVRSRV_ERROR PollForInterruptKM (IMG_UINT32 ui32Value,
+								 IMG_UINT32 ui32Mask,
+								 IMG_UINT32 ui32Waitus,
+								 IMG_UINT32 ui32Tries)
+{
+	IMG_UINT32	uiMaxTime;
+
+	uiMaxTime = ui32Tries * ui32Waitus;
+
+
+	LOOP_UNTIL_TIMEOUT(uiMaxTime)
+	{
+		if ((gui32EventStatusServicesByISR & ui32Mask) == ui32Value)
+		{
+			gui32EventStatusServicesByISR = 0;
+			return PVRSRV_OK;
+		}
+		OSWaitus(ui32Waitus);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+	return PVRSRV_ERROR_GENERIC;
+}
+#endif
+
+IMG_VOID PVRSRVGetMiscInfoKM_RA_GetStats_ForEachVaCb(BM_HEAP *psBMHeap, va_list va)
+{
+	IMG_CHAR **ppszStr;
+	IMG_UINT32 *pui32StrLen;
+
+	ppszStr = va_arg(va, IMG_CHAR**);
+	pui32StrLen = va_arg(va, IMG_UINT32*);
+
+	if(psBMHeap->pImportArena)
+	{
+		RA_GetStats(psBMHeap->pImportArena,
+					ppszStr,
+					pui32StrLen);
+	}
+
+	if(psBMHeap->pVMArena)
+	{
+		RA_GetStats(psBMHeap->pVMArena,
+					ppszStr,
+					pui32StrLen);
+	}
+}
+
+PVRSRV_ERROR PVRSRVGetMiscInfoKM_BMContext_AnyVaCb(BM_CONTEXT *psBMContext, va_list va)
+{
+
+	IMG_UINT32 *pui32StrLen;
+	IMG_INT32 *pi32Count;
+	IMG_CHAR **ppszStr;
+
+	pui32StrLen = va_arg(va, IMG_UINT32*);
+	pi32Count = va_arg(va, IMG_INT32*);
+	ppszStr = va_arg(va, IMG_CHAR**);
+
+	CHECK_SPACE(*pui32StrLen);
+	*pi32Count = OSSNPrintf(*ppszStr, 100, "\nApplication Context (hDevMemContext) 0x%08X:\n",
+							(IMG_HANDLE)psBMContext);
+	UPDATE_SPACE(*ppszStr, *pi32Count, *pui32StrLen);
+
+	List_BM_HEAP_ForEach_va(psBMContext->psBMHeap,
+							PVRSRVGetMiscInfoKM_RA_GetStats_ForEachVaCb,
+							ppszStr,
+							pui32StrLen);
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR PVRSRVGetMiscInfoKM_Device_AnyVaCb(PVRSRV_DEVICE_NODE *psDeviceNode, va_list va)
+{
+	IMG_UINT32 *pui32StrLen;
+	IMG_INT32 *pi32Count;
+	IMG_CHAR **ppszStr;
+
+	pui32StrLen = va_arg(va, IMG_UINT32*);
+	pi32Count = va_arg(va, IMG_INT32*);
+	ppszStr = va_arg(va, IMG_CHAR**);
+
+	CHECK_SPACE(*pui32StrLen);
+	*pi32Count = OSSNPrintf(*ppszStr, 100, "\n\nDevice Type %d:\n", psDeviceNode->sDevId.eDeviceType);
+	UPDATE_SPACE(*ppszStr, *pi32Count, *pui32StrLen);
+
+
+	if(psDeviceNode->sDevMemoryInfo.pBMKernelContext)
+	{
+		CHECK_SPACE(*pui32StrLen);
+		*pi32Count = OSSNPrintf(*ppszStr, 100, "\nKernel Context:\n");
+		UPDATE_SPACE(*ppszStr, *pi32Count, *pui32StrLen);
+
+
+		List_BM_HEAP_ForEach_va(psDeviceNode->sDevMemoryInfo.pBMKernelContext->psBMHeap,
+								PVRSRVGetMiscInfoKM_RA_GetStats_ForEachVaCb,
+								ppszStr,
+								pui32StrLen);
+	}
+
+
+	return List_BM_CONTEXT_PVRSRV_ERROR_Any_va(psDeviceNode->sDevMemoryInfo.pBMContext,
+												PVRSRVGetMiscInfoKM_BMContext_AnyVaCb,
+							 					pui32StrLen,
+												pi32Count,
+												ppszStr);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetMiscInfoKM(PVRSRV_MISC_INFO *psMiscInfo)
+{
+	SYS_DATA *psSysData;
+
+	if(!psMiscInfo)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetMiscInfoKM: invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	psMiscInfo->ui32StatePresent = 0;
+
+
+	if(psMiscInfo->ui32StateRequest & ~( PVRSRV_MISC_INFO_TIMER_PRESENT
+										|PVRSRV_MISC_INFO_CLOCKGATE_PRESENT
+										|PVRSRV_MISC_INFO_MEMSTATS_PRESENT
+										|PVRSRV_MISC_INFO_GLOBALEVENTOBJECT_PRESENT
+										|PVRSRV_MISC_INFO_DDKVERSION_PRESENT
+										|PVRSRV_MISC_INFO_CPUCACHEFLUSH_PRESENT
+										|PVRSRV_MISC_INFO_RESET_PRESENT))
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVGetMiscInfoKM: invalid state request flags"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	SysAcquireData(&psSysData);
+
+
+	if(((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_TIMER_PRESENT) != 0UL) &&
+		(psSysData->pvSOCTimerRegisterKM != IMG_NULL))
+	{
+		psMiscInfo->ui32StatePresent |= PVRSRV_MISC_INFO_TIMER_PRESENT;
+		psMiscInfo->pvSOCTimerRegisterKM = psSysData->pvSOCTimerRegisterKM;
+		psMiscInfo->hSOCTimerRegisterOSMemHandle = psSysData->hSOCTimerRegisterOSMemHandle;
+	}
+	else
+	{
+		psMiscInfo->pvSOCTimerRegisterKM = IMG_NULL;
+		psMiscInfo->hSOCTimerRegisterOSMemHandle = IMG_NULL;
+	}
+
+
+	if(((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_CLOCKGATE_PRESENT) != 0UL) &&
+		(psSysData->pvSOCClockGateRegsBase != IMG_NULL))
+	{
+		psMiscInfo->ui32StatePresent |= PVRSRV_MISC_INFO_CLOCKGATE_PRESENT;
+		psMiscInfo->pvSOCClockGateRegs = psSysData->pvSOCClockGateRegsBase;
+		psMiscInfo->ui32SOCClockGateRegsSize = psSysData->ui32SOCClockGateRegsSize;
+	}
+
+
+	if(((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_MEMSTATS_PRESENT) != 0UL) &&
+		(psMiscInfo->pszMemoryStr != IMG_NULL))
+	{
+		RA_ARENA			**ppArena;
+		IMG_CHAR			*pszStr;
+		IMG_UINT32			ui32StrLen;
+		IMG_INT32			i32Count;
+
+		pszStr = psMiscInfo->pszMemoryStr;
+		ui32StrLen = psMiscInfo->ui32MemoryStrLen;
+
+		psMiscInfo->ui32StatePresent |= PVRSRV_MISC_INFO_MEMSTATS_PRESENT;
+
+
+		ppArena = &psSysData->apsLocalDevMemArena[0];
+		while(*ppArena)
+		{
+			CHECK_SPACE(ui32StrLen);
+			i32Count = OSSNPrintf(pszStr, 100, "\nLocal Backing Store:\n");
+			UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+			RA_GetStats(*ppArena,
+							&pszStr,
+							&ui32StrLen);
+
+			ppArena++;
+		}
+
+
+
+		List_PVRSRV_DEVICE_NODE_PVRSRV_ERROR_Any_va(psSysData->psDeviceNodeList,
+													PVRSRVGetMiscInfoKM_Device_AnyVaCb,
+													&ui32StrLen,
+													&i32Count,
+													&pszStr);
+
+
+		i32Count = OSSNPrintf(pszStr, 100, "\n\0");
+		UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+	}
+
+	if(((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_GLOBALEVENTOBJECT_PRESENT) != 0UL) &&
+		(psSysData->psGlobalEventObject != IMG_NULL))
+	{
+		psMiscInfo->ui32StatePresent |= PVRSRV_MISC_INFO_GLOBALEVENTOBJECT_PRESENT;
+		psMiscInfo->sGlobalEventObject = *psSysData->psGlobalEventObject;
+	}
+
+
+
+	if (((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_DDKVERSION_PRESENT) != 0UL)
+		&& ((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_MEMSTATS_PRESENT) == 0UL)
+		&& (psMiscInfo->pszMemoryStr != IMG_NULL))
+	{
+		IMG_CHAR	*pszStr;
+		IMG_UINT32	ui32StrLen;
+		IMG_UINT32 	ui32LenStrPerNum = 12;
+		IMG_INT32	i32Count;
+		IMG_INT i;
+		psMiscInfo->ui32StatePresent |= PVRSRV_MISC_INFO_DDKVERSION_PRESENT;
+
+
+		psMiscInfo->aui32DDKVersion[0] = PVRVERSION_MAJ;
+		psMiscInfo->aui32DDKVersion[1] = PVRVERSION_MIN;
+		psMiscInfo->aui32DDKVersion[2] = PVRVERSION_BRANCH;
+		psMiscInfo->aui32DDKVersion[3] = PVRVERSION_BUILD;
+
+		pszStr = psMiscInfo->pszMemoryStr;
+		ui32StrLen = psMiscInfo->ui32MemoryStrLen;
+
+		for (i=0; i<4; i++)
+		{
+			if (ui32StrLen < ui32LenStrPerNum)
+			{
+				return PVRSRV_ERROR_INVALID_PARAMS;
+			}
+
+			i32Count = OSSNPrintf(pszStr, ui32LenStrPerNum, "%ld", psMiscInfo->aui32DDKVersion[i]);
+			UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+			if (i != 3)
+			{
+				i32Count = OSSNPrintf(pszStr, 2, ".");
+				UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+			}
+		}
+	}
+
+#if defined(SUPPORT_CPU_CACHED_BUFFERS)
+	if((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_CPUCACHEFLUSH_PRESENT) != 0UL)
+	{
+		if(psMiscInfo->bDeferCPUCacheFlush)
+		{
+
+			if(!psMiscInfo->bCPUCacheFlushAll)
+			{
+
+
+
+				PVR_DPF((PVR_DBG_MESSAGE,"PVRSRVGetMiscInfoKM: don't support deferred range flushes"));
+				PVR_DPF((PVR_DBG_MESSAGE,"                     using deferred flush all instead"));
+			}
+
+			psSysData->bFlushAll = IMG_TRUE;
+		}
+		else
+		{
+
+			if(psMiscInfo->bCPUCacheFlushAll)
+			{
+
+				OSFlushCPUCacheKM();
+
+				psSysData->bFlushAll = IMG_FALSE;
+			}
+			else
+			{
+
+				OSFlushCPUCacheRangeKM(psMiscInfo->pvRangeAddrStart, psMiscInfo->pvRangeAddrEnd);
+			}
+		}
+	}
+#endif
+
+#if defined(PVRSRV_RESET_ON_HWTIMEOUT)
+	if((psMiscInfo->ui32StateRequest & PVRSRV_MISC_INFO_RESET_PRESENT) != 0UL)
+	{
+		PVR_LOG(("User requested OS reset"));
+		OSPanic();
+	}
+#endif
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetFBStatsKM(IMG_UINT32		*pui32Total,
+											 IMG_UINT32		*pui32Available)
+{
+	IMG_UINT32 ui32Total = 0, i = 0;
+	IMG_UINT32 ui32Available = 0;
+
+	*pui32Total		= 0;
+	*pui32Available = 0;
+
+
+	while(BM_ContiguousStatistics(i, &ui32Total, &ui32Available) == IMG_TRUE)
+	{
+		*pui32Total		+= ui32Total;
+		*pui32Available += ui32Available;
+
+		i++;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_BOOL IMG_CALLCONV PVRSRVDeviceLISR(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	SYS_DATA			*psSysData;
+	IMG_BOOL			bStatus = IMG_FALSE;
+	IMG_UINT32			ui32InterruptSource;
+
+	if(!psDeviceNode)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVDeviceLISR: Invalid params\n"));
+		goto out;
+	}
+	psSysData = psDeviceNode->psSysData;
+
+
+	ui32InterruptSource = SysGetInterruptSource(psSysData, psDeviceNode);
+	if(ui32InterruptSource & psDeviceNode->ui32SOCInterruptBit)
+	{
+		if(psDeviceNode->pfnDeviceISR != IMG_NULL)
+		{
+			bStatus = (*psDeviceNode->pfnDeviceISR)(psDeviceNode->pvISRData);
+		}
+
+		SysClearInterrupts(psSysData, psDeviceNode->ui32SOCInterruptBit);
+	}
+
+out:
+	return bStatus;
+}
+
+IMG_VOID PVRSRVSystemLISR_ForEachVaCb(PVRSRV_DEVICE_NODE *psDeviceNode, va_list va)
+{
+
+	IMG_BOOL *pbStatus;
+	IMG_UINT32 *pui32InterruptSource;
+	IMG_UINT32 *pui32ClearInterrupts;
+
+	pbStatus = va_arg(va, IMG_BOOL*);
+	pui32InterruptSource = va_arg(va, IMG_UINT32*);
+	pui32ClearInterrupts = va_arg(va, IMG_UINT32*);
+
+
+	if(psDeviceNode->pfnDeviceISR != IMG_NULL)
+	{
+		if(*pui32InterruptSource & psDeviceNode->ui32SOCInterruptBit)
+		{
+			if((*psDeviceNode->pfnDeviceISR)(psDeviceNode->pvISRData))
+			{
+
+				*pbStatus = IMG_TRUE;
+			}
+
+			*pui32ClearInterrupts |= psDeviceNode->ui32SOCInterruptBit;
+		}
+	}
+}
+
+IMG_BOOL IMG_CALLCONV PVRSRVSystemLISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA			*psSysData = pvSysData;
+	IMG_BOOL			bStatus = IMG_FALSE;
+	IMG_UINT32			ui32InterruptSource;
+	IMG_UINT32			ui32ClearInterrupts = 0;
+	if(!psSysData)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVSystemLISR: Invalid params\n"));
+	}
+	else
+	{
+
+		ui32InterruptSource = SysGetInterruptSource(psSysData, IMG_NULL);
+
+
+		if(ui32InterruptSource)
+		{
+
+			List_PVRSRV_DEVICE_NODE_ForEach_va(psSysData->psDeviceNodeList,
+												PVRSRVSystemLISR_ForEachVaCb,
+												&bStatus,
+												&ui32InterruptSource,
+												&ui32ClearInterrupts);
+
+			SysClearInterrupts(psSysData, ui32ClearInterrupts);
+		}
+	}
+	return bStatus;
+}
+
+
+IMG_VOID PVRSRVMISR_ForEachCb(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	if(psDeviceNode->pfnDeviceMISR != IMG_NULL)
+	{
+		(*psDeviceNode->pfnDeviceMISR)(psDeviceNode->pvISRData);
+	}
+}
+
+IMG_VOID IMG_CALLCONV PVRSRVMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA			*psSysData = pvSysData;
+	if(!psSysData)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVMISR: Invalid params\n"));
+		return;
+	}
+
+
+	List_PVRSRV_DEVICE_NODE_ForEach(psSysData->psDeviceNodeList,
+									PVRSRVMISR_ForEachCb);
+
+
+	if (PVRSRVProcessQueues(ISR_ID, IMG_FALSE) == PVRSRV_ERROR_PROCESSING_BLOCKED)
+	{
+		PVRSRVProcessQueues(ISR_ID, IMG_FALSE);
+	}
+
+
+	if (psSysData->psGlobalEventObject)
+	{
+		IMG_HANDLE hOSEventKM = psSysData->psGlobalEventObject->hOSEventKM;
+		if(hOSEventKM)
+		{
+			OSEventObjectSignal(hOSEventKM);
+		}
+	}
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVProcessConnect(IMG_UINT32	ui32PID)
+{
+	return PVRSRVPerProcessDataConnect(ui32PID);
+}
+
+
+IMG_EXPORT
+IMG_VOID IMG_CALLCONV PVRSRVProcessDisconnect(IMG_UINT32	ui32PID)
+{
+	PVRSRVPerProcessDataDisconnect(ui32PID);
+}
+
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSaveRestoreLiveSegments(IMG_HANDLE hArena, IMG_PBYTE pbyBuffer,
+														IMG_SIZE_T *puiBufSize, IMG_BOOL bSave)
+{
+	IMG_SIZE_T         uiBytesSaved = 0;
+	IMG_PVOID          pvLocalMemCPUVAddr;
+	RA_SEGMENT_DETAILS sSegDetails;
+
+	if (hArena == IMG_NULL)
+	{
+		return (PVRSRV_ERROR_INVALID_PARAMS);
+	}
+
+	sSegDetails.uiSize = 0;
+	sSegDetails.sCpuPhyAddr.uiAddr = 0;
+	sSegDetails.hSegment = 0;
+
+
+	while (RA_GetNextLiveSegment(hArena, &sSegDetails))
+	{
+		if (pbyBuffer == IMG_NULL)
+		{
+
+			uiBytesSaved += sizeof(sSegDetails.uiSize) + sSegDetails.uiSize;
+		}
+		else
+		{
+			if ((uiBytesSaved + sizeof(sSegDetails.uiSize) + sSegDetails.uiSize) > *puiBufSize)
+			{
+				return (PVRSRV_ERROR_OUT_OF_MEMORY);
+			}
+
+			PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVSaveRestoreLiveSegments: Base %08x size %08x", sSegDetails.sCpuPhyAddr.uiAddr, sSegDetails.uiSize));
+
+
+			pvLocalMemCPUVAddr = OSMapPhysToLin(sSegDetails.sCpuPhyAddr,
+									sSegDetails.uiSize,
+									PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+									IMG_NULL);
+			if (pvLocalMemCPUVAddr == IMG_NULL)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "PVRSRVSaveRestoreLiveSegments: Failed to map local memory to host"));
+				return (PVRSRV_ERROR_OUT_OF_MEMORY);
+			}
+
+			if (bSave)
+			{
+
+				OSMemCopy(pbyBuffer, &sSegDetails.uiSize, sizeof(sSegDetails.uiSize));
+				pbyBuffer += sizeof(sSegDetails.uiSize);
+
+				OSMemCopy(pbyBuffer, pvLocalMemCPUVAddr, sSegDetails.uiSize);
+				pbyBuffer += sSegDetails.uiSize;
+			}
+			else
+			{
+				IMG_UINT32 uiSize;
+
+				OSMemCopy(&uiSize, pbyBuffer, sizeof(sSegDetails.uiSize));
+
+				if (uiSize != sSegDetails.uiSize)
+				{
+					PVR_DPF((PVR_DBG_ERROR, "PVRSRVSaveRestoreLiveSegments: Segment size error"));
+				}
+				else
+				{
+					pbyBuffer += sizeof(sSegDetails.uiSize);
+
+					OSMemCopy(pvLocalMemCPUVAddr, pbyBuffer, sSegDetails.uiSize);
+					pbyBuffer += sSegDetails.uiSize;
+				}
+			}
+
+
+			uiBytesSaved += sizeof(sSegDetails.uiSize) + sSegDetails.uiSize;
+
+			OSUnMapPhysToLin(pvLocalMemCPUVAddr,
+		                     sSegDetails.uiSize,
+		                     PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+		                     IMG_NULL);
+		}
+	}
+
+	if (pbyBuffer == IMG_NULL)
+	{
+		*puiBufSize = uiBytesSaved;
+	}
+
+	return (PVRSRV_OK);
+}
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/queue.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/queue.c
new file mode 100644
index 0000000..d16ab6d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/queue.c
@@ -0,0 +1,1161 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+
+#include "lists.h"
+
+DECLARE_LIST_FOR_EACH(PVRSRV_DEVICE_NODE);
+
+#if defined(__linux__) && defined(__KERNEL__)
+
+#include "proc.h"
+
+static IMG_INT
+QueuePrintCommands (PVRSRV_QUEUE_INFO * psQueue, IMG_CHAR * buffer, size_t size)
+{
+	off_t off = 0;
+	IMG_INT cmds = 0;
+	IMG_SIZE_T ui32ReadOffset  = psQueue->ui32ReadOffset;
+	IMG_SIZE_T ui32WriteOffset = psQueue->ui32WriteOffset;
+	PVRSRV_COMMAND * psCmd;
+
+	while (ui32ReadOffset != ui32WriteOffset)
+	{
+		psCmd= (PVRSRV_COMMAND *)((IMG_UINTPTR_T)psQueue->pvLinQueueKM + ui32ReadOffset);
+
+		off = printAppend(buffer, size, off, "%p %p  %5lu  %6lu  %3lu  %5lu   %2lu   %2lu    %3lu  \n",
+							psQueue,
+					 		psCmd,
+					 		psCmd->ui32ProcessID,
+							psCmd->CommandType,
+							psCmd->ui32CmdSize,
+							psCmd->ui32DevIndex,
+							psCmd->ui32DstSyncCount,
+							psCmd->ui32SrcSyncCount,
+							psCmd->ui32DataSize);
+
+		ui32ReadOffset += psCmd->ui32CmdSize;
+		ui32ReadOffset &= psQueue->ui32QueueSize - 1;
+		cmds++;
+	}
+	if (cmds == 0)
+		off = printAppend(buffer, size, off, "%p <empty>\n", psQueue);
+	return off;
+}
+
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+void ProcSeqShowQueue(struct seq_file *sfile,void* el)
+{
+	PVRSRV_QUEUE_INFO * psQueue = (PVRSRV_QUEUE_INFO*)el;
+	IMG_INT cmds = 0;
+	IMG_SIZE_T ui32ReadOffset;
+	IMG_SIZE_T ui32WriteOffset;
+	PVRSRV_COMMAND * psCmd;
+
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+		seq_printf( sfile,
+					"Command Queues\n"
+					"Queue    CmdPtr      Pid Command Size DevInd  DSC  SSC  #Data ...\n");
+		return;
+	}
+
+	ui32ReadOffset = psQueue->ui32ReadOffset;
+	ui32WriteOffset = psQueue->ui32WriteOffset;
+
+	while (ui32ReadOffset != ui32WriteOffset)
+	{
+		psCmd= (PVRSRV_COMMAND *)((IMG_UINTPTR_T)psQueue->pvLinQueueKM + ui32ReadOffset);
+
+		seq_printf(sfile, "%p %p  %5lu  %6lu  %3lu  %5lu   %2lu   %2lu    %3lu  \n",
+							psQueue,
+					 		psCmd,
+					 		psCmd->ui32ProcessID,
+							psCmd->CommandType,
+							psCmd->ui32CmdSize,
+							psCmd->ui32DevIndex,
+							psCmd->ui32DstSyncCount,
+							psCmd->ui32SrcSyncCount,
+							psCmd->ui32DataSize);
+
+		ui32ReadOffset += psCmd->ui32CmdSize;
+		ui32ReadOffset &= psQueue->ui32QueueSize - 1;
+		cmds++;
+	}
+
+	if (cmds == 0)
+		seq_printf(sfile, "%p <empty>\n", psQueue);
+}
+
+void* ProcSeqOff2ElementQueue(struct seq_file * sfile, loff_t off)
+{
+	PVRSRV_QUEUE_INFO * psQueue;
+	SYS_DATA * psSysData;
+
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+
+	SysAcquireData(&psSysData);
+
+	for (psQueue = psSysData->psQueueList; (((--off) > 0) && (psQueue != IMG_NULL)); psQueue = psQueue->psNextKM);
+	return psQueue;
+}
+
+#endif
+
+off_t
+QueuePrintQueues (IMG_CHAR * buffer, size_t size, off_t off)
+{
+	SYS_DATA * psSysData;
+	PVRSRV_QUEUE_INFO * psQueue;
+
+	SysAcquireData(&psSysData);
+
+	 if (!off)
+		  return printAppend (buffer, size, 0,
+								"Command Queues\n"
+								"Queue    CmdPtr      Pid Command Size DevInd  DSC  SSC  #Data ...\n");
+
+
+
+	for (psQueue = psSysData->psQueueList; (((--off) > 0) && (psQueue != IMG_NULL)); psQueue = psQueue->psNextKM)
+		;
+
+	return psQueue ? QueuePrintCommands (psQueue, buffer, size) : END_OF_FILE;
+}
+#endif
+
+#define GET_SPACE_IN_CMDQ(psQueue)										\
+	(((psQueue->ui32ReadOffset - psQueue->ui32WriteOffset)				\
+	+ (psQueue->ui32QueueSize - 1)) & (psQueue->ui32QueueSize - 1))
+
+#define UPDATE_QUEUE_WOFF(psQueue, ui32Size)							\
+	psQueue->ui32WriteOffset = (psQueue->ui32WriteOffset + ui32Size)	\
+	& (psQueue->ui32QueueSize - 1);
+
+#define SYNCOPS_STALE(ui32OpsComplete, ui32OpsPending)					\
+	(ui32OpsComplete >= ui32OpsPending)
+
+
+DECLARE_LIST_FOR_EACH(PVRSRV_DEVICE_NODE);
+
+static IMG_VOID QueueDumpCmdComplete(COMMAND_COMPLETE_DATA *psCmdCompleteData,
+									 IMG_UINT32				i,
+									 IMG_BOOL				bIsSrc)
+{
+	PVRSRV_SYNC_OBJECT	*psSyncObject;
+
+	psSyncObject = bIsSrc ? psCmdCompleteData->psSrcSync : psCmdCompleteData->psDstSync;
+
+	if (psCmdCompleteData->bInUse)
+	{
+		PVR_LOG(("\t%s %lu: ROC DevVAddr:0x%lX ROP:0x%lx ROC:0x%lx, WOC DevVAddr:0x%lX WOP:0x%lx WOC:0x%lx",
+				bIsSrc ? "SRC" : "DEST", i,
+				psSyncObject[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psSyncObject[i].psKernelSyncInfoKM->psSyncData->ui32ReadOpsPending,
+				psSyncObject[i].psKernelSyncInfoKM->psSyncData->ui32ReadOpsComplete,
+				psSyncObject[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psSyncObject[i].psKernelSyncInfoKM->psSyncData->ui32WriteOpsPending,
+				psSyncObject[i].psKernelSyncInfoKM->psSyncData->ui32WriteOpsComplete));
+	}
+	else
+	{
+		PVR_LOG(("\t%s %lu: (Not in use)", bIsSrc ? "SRC" : "DEST", i));
+	}
+}
+
+
+static IMG_VOID QueueDumpDebugInfo_ForEachCb(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	if (psDeviceNode->sDevId.eDeviceClass == PVRSRV_DEVICE_CLASS_DISPLAY)
+	{
+		IMG_UINT32				i;
+		SYS_DATA				*psSysData;
+		COMMAND_COMPLETE_DATA	**ppsCmdCompleteData;
+		COMMAND_COMPLETE_DATA	*psCmdCompleteData;
+
+		SysAcquireData(&psSysData);
+
+		ppsCmdCompleteData = psSysData->ppsCmdCompleteData[psDeviceNode->sDevId.ui32DeviceIndex];
+
+		if (ppsCmdCompleteData != IMG_NULL)
+		{
+			psCmdCompleteData = ppsCmdCompleteData[DC_FLIP_COMMAND];
+
+			PVR_LOG(("Command Complete Data for display device %lu:", psDeviceNode->sDevId.ui32DeviceIndex));
+
+			for (i = 0; i < psCmdCompleteData->ui32SrcSyncCount; i++)
+			{
+				QueueDumpCmdComplete(psCmdCompleteData, i, IMG_TRUE);
+			}
+
+			for (i = 0; i < psCmdCompleteData->ui32DstSyncCount; i++)
+			{
+				QueueDumpCmdComplete(psCmdCompleteData, i, IMG_FALSE);
+			}
+		}
+		else
+		{
+			PVR_LOG(("There is no Command Complete Data for display device %u", psDeviceNode->sDevId.ui32DeviceIndex));
+		}
+	}
+}
+
+
+IMG_VOID QueueDumpDebugInfo(IMG_VOID)
+{
+	SYS_DATA	*psSysData;
+	SysAcquireData(&psSysData);
+	List_PVRSRV_DEVICE_NODE_ForEach(psSysData->psDeviceNodeList, QueueDumpDebugInfo_ForEachCb);
+}
+
+
+IMG_SIZE_T NearestPower2(IMG_SIZE_T ui32Value)
+{
+	IMG_SIZE_T ui32Temp, ui32Result = 1;
+
+	if(!ui32Value)
+		return 0;
+
+	ui32Temp = ui32Value - 1;
+	while(ui32Temp)
+	{
+		ui32Result <<= 1;
+		ui32Temp >>= 1;
+	}
+
+	return ui32Result;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateCommandQueueKM(IMG_SIZE_T ui32QueueSize,
+													 PVRSRV_QUEUE_INFO **ppsQueueInfo)
+{
+	PVRSRV_QUEUE_INFO	*psQueueInfo;
+	IMG_SIZE_T			ui32Power2QueueSize = NearestPower2(ui32QueueSize);
+	SYS_DATA			*psSysData;
+	PVRSRV_ERROR		eError;
+	IMG_HANDLE			hMemBlock;
+
+	SysAcquireData(&psSysData);
+
+
+	if(OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_QUEUE_INFO),
+					 (IMG_VOID **)&psQueueInfo, &hMemBlock,
+					 "Queue Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateCommandQueueKM: Failed to alloc queue struct"));
+		goto ErrorExit;
+	}
+	OSMemSet(psQueueInfo, 0, sizeof(PVRSRV_QUEUE_INFO));
+
+	psQueueInfo->hMemBlock[0] = hMemBlock;
+	psQueueInfo->ui32ProcessID = OSGetCurrentProcessIDKM();
+
+
+	if(OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					 ui32Power2QueueSize + PVRSRV_MAX_CMD_SIZE,
+					 &psQueueInfo->pvLinQueueKM, &hMemBlock,
+					 "Command Queue") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateCommandQueueKM: Failed to alloc queue buffer"));
+		goto ErrorExit;
+	}
+
+	psQueueInfo->hMemBlock[1] = hMemBlock;
+	psQueueInfo->pvLinQueueUM = psQueueInfo->pvLinQueueKM;
+
+
+	PVR_ASSERT(psQueueInfo->ui32ReadOffset == 0);
+	PVR_ASSERT(psQueueInfo->ui32WriteOffset == 0);
+
+	psQueueInfo->ui32QueueSize = ui32Power2QueueSize;
+
+
+	if (psSysData->psQueueList == IMG_NULL)
+	{
+		eError = OSCreateResource(&psSysData->sQProcessResource);
+		if (eError != PVRSRV_OK)
+		{
+			goto ErrorExit;
+		}
+	}
+
+
+	if (OSLockResource(&psSysData->sQProcessResource,
+							KERNEL_ID) != PVRSRV_OK)
+	{
+		goto ErrorExit;
+	}
+
+	psQueueInfo->psNextKM = psSysData->psQueueList;
+	psSysData->psQueueList = psQueueInfo;
+
+	if (OSUnlockResource(&psSysData->sQProcessResource, KERNEL_ID) != PVRSRV_OK)
+	{
+		goto ErrorExit;
+	}
+
+	*ppsQueueInfo = psQueueInfo;
+
+	return PVRSRV_OK;
+
+ErrorExit:
+
+	if(psQueueInfo)
+	{
+		if(psQueueInfo->pvLinQueueKM)
+		{
+			OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+						psQueueInfo->ui32QueueSize,
+						psQueueInfo->pvLinQueueKM,
+						psQueueInfo->hMemBlock[1]);
+			psQueueInfo->pvLinQueueKM = IMG_NULL;
+		}
+
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					sizeof(PVRSRV_QUEUE_INFO),
+					psQueueInfo,
+					psQueueInfo->hMemBlock[0]);
+
+	}
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyCommandQueueKM(PVRSRV_QUEUE_INFO *psQueueInfo)
+{
+	PVRSRV_QUEUE_INFO	*psQueue;
+	SYS_DATA			*psSysData;
+	PVRSRV_ERROR		eError;
+	IMG_BOOL			bTimeout = IMG_TRUE;
+
+	SysAcquireData(&psSysData);
+
+	psQueue = psSysData->psQueueList;
+
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		if(psQueueInfo->ui32ReadOffset == psQueueInfo->ui32WriteOffset)
+		{
+			bTimeout = IMG_FALSE;
+			break;
+		}
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+	if (bTimeout)
+	{
+
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVDestroyCommandQueueKM : Failed to empty queue"));
+		eError = PVRSRV_ERROR_CANNOT_FLUSH_QUEUE;
+		goto ErrorExit;
+	}
+
+
+	eError = OSLockResource(&psSysData->sQProcessResource,
+								KERNEL_ID);
+	if (eError != PVRSRV_OK)
+	{
+		goto ErrorExit;
+	}
+
+	if(psQueue == psQueueInfo)
+	{
+		psSysData->psQueueList = psQueueInfo->psNextKM;
+
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					NearestPower2(psQueueInfo->ui32QueueSize) + PVRSRV_MAX_CMD_SIZE,
+					psQueueInfo->pvLinQueueKM,
+					psQueueInfo->hMemBlock[1]);
+		psQueueInfo->pvLinQueueKM = IMG_NULL;
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					sizeof(PVRSRV_QUEUE_INFO),
+					psQueueInfo,
+					psQueueInfo->hMemBlock[0]);
+
+		psQueueInfo = IMG_NULL;
+	}
+	else
+	{
+		while(psQueue)
+		{
+			if(psQueue->psNextKM == psQueueInfo)
+			{
+				psQueue->psNextKM = psQueueInfo->psNextKM;
+
+				OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+							psQueueInfo->ui32QueueSize,
+							psQueueInfo->pvLinQueueKM,
+							psQueueInfo->hMemBlock[1]);
+				psQueueInfo->pvLinQueueKM = IMG_NULL;
+				OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+							sizeof(PVRSRV_QUEUE_INFO),
+							psQueueInfo,
+							psQueueInfo->hMemBlock[0]);
+
+				psQueueInfo = IMG_NULL;
+				break;
+			}
+			psQueue = psQueue->psNextKM;
+		}
+
+		if(!psQueue)
+		{
+			eError = OSUnlockResource(&psSysData->sQProcessResource, KERNEL_ID);
+			if (eError != PVRSRV_OK)
+			{
+				goto ErrorExit;
+			}
+			eError = PVRSRV_ERROR_INVALID_PARAMS;
+			goto ErrorExit;
+		}
+	}
+
+
+	eError = OSUnlockResource(&psSysData->sQProcessResource, KERNEL_ID);
+	if (eError != PVRSRV_OK)
+	{
+		goto ErrorExit;
+	}
+
+
+	if (psSysData->psQueueList == IMG_NULL)
+	{
+		eError = OSDestroyResource(&psSysData->sQProcessResource);
+		if (eError != PVRSRV_OK)
+		{
+			goto ErrorExit;
+		}
+	}
+
+ErrorExit:
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetQueueSpaceKM(PVRSRV_QUEUE_INFO *psQueue,
+												IMG_SIZE_T ui32ParamSize,
+												IMG_VOID **ppvSpace)
+{
+	IMG_BOOL bTimeout = IMG_TRUE;
+
+
+	ui32ParamSize =  (ui32ParamSize+3) & 0xFFFFFFFC;
+
+	if (ui32ParamSize > PVRSRV_MAX_CMD_SIZE)
+	{
+		PVR_DPF((PVR_DBG_WARNING,"PVRSRVGetQueueSpace: max command size is %d bytes", PVRSRV_MAX_CMD_SIZE));
+		return PVRSRV_ERROR_CMD_TOO_BIG;
+	}
+
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		if (GET_SPACE_IN_CMDQ(psQueue) > ui32ParamSize)
+		{
+			bTimeout = IMG_FALSE;
+			break;
+		}
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+	if (bTimeout == IMG_TRUE)
+	{
+		*ppvSpace = IMG_NULL;
+
+		return PVRSRV_ERROR_CANNOT_GET_QUEUE_SPACE;
+	}
+	else
+	{
+		*ppvSpace = (IMG_VOID *)((IMG_UINTPTR_T)psQueue->pvLinQueueUM + psQueue->ui32WriteOffset);
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVInsertCommandKM(PVRSRV_QUEUE_INFO	*psQueue,
+												PVRSRV_COMMAND		**ppsCommand,
+												IMG_UINT32			ui32DevIndex,
+												IMG_UINT16			CommandType,
+												IMG_UINT32			ui32DstSyncCount,
+												PVRSRV_KERNEL_SYNC_INFO	*apsDstSync[],
+												IMG_UINT32			ui32SrcSyncCount,
+												PVRSRV_KERNEL_SYNC_INFO	*apsSrcSync[],
+												IMG_SIZE_T			ui32DataByteSize )
+{
+	PVRSRV_ERROR 	eError;
+	PVRSRV_COMMAND	*psCommand;
+	IMG_SIZE_T		ui32CommandSize;
+	IMG_UINT32		i;
+
+
+	ui32DataByteSize = (ui32DataByteSize + 3UL) & ~3UL;
+
+
+	ui32CommandSize = sizeof(PVRSRV_COMMAND)
+					+ ((ui32DstSyncCount + ui32SrcSyncCount) * sizeof(PVRSRV_SYNC_OBJECT))
+					+ ui32DataByteSize;
+
+
+	eError = PVRSRVGetQueueSpaceKM (psQueue, ui32CommandSize, (IMG_VOID**)&psCommand);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	psCommand->ui32ProcessID	= OSGetCurrentProcessIDKM();
+
+
+	psCommand->ui32CmdSize		= ui32CommandSize;
+	psCommand->ui32DevIndex 	= ui32DevIndex;
+	psCommand->CommandType 		= CommandType;
+	psCommand->ui32DstSyncCount	= ui32DstSyncCount;
+	psCommand->ui32SrcSyncCount	= ui32SrcSyncCount;
+
+
+	psCommand->psDstSync		= (PVRSRV_SYNC_OBJECT*)(((IMG_UINTPTR_T)psCommand) + sizeof(PVRSRV_COMMAND));
+
+
+	psCommand->psSrcSync		= (PVRSRV_SYNC_OBJECT*)(((IMG_UINTPTR_T)psCommand->psDstSync)
+								+ (ui32DstSyncCount * sizeof(PVRSRV_SYNC_OBJECT)));
+
+	psCommand->pvData			= (PVRSRV_SYNC_OBJECT*)(((IMG_UINTPTR_T)psCommand->psSrcSync)
+								+ (ui32SrcSyncCount * sizeof(PVRSRV_SYNC_OBJECT)));
+	psCommand->ui32DataSize		= ui32DataByteSize;
+
+
+	for (i=0; i<ui32DstSyncCount; i++)
+	{
+		psCommand->psDstSync[i].psKernelSyncInfoKM = apsDstSync[i];
+		psCommand->psDstSync[i].ui32WriteOpsPending = PVRSRVGetWriteOpsPending(apsDstSync[i], IMG_FALSE);
+		psCommand->psDstSync[i].ui32ReadOpsPending = PVRSRVGetReadOpsPending(apsDstSync[i], IMG_FALSE);
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVInsertCommandKM: Dst %lu RO-VA:0x%lx WO-VA:0x%lx ROP:0x%lx WOP:0x%lx",
+				i, psCommand->psDstSync[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psCommand->psDstSync[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psCommand->psDstSync[i].ui32ReadOpsPending,
+				psCommand->psDstSync[i].ui32WriteOpsPending));
+	}
+
+
+	for (i=0; i<ui32SrcSyncCount; i++)
+	{
+		psCommand->psSrcSync[i].psKernelSyncInfoKM = apsSrcSync[i];
+		psCommand->psSrcSync[i].ui32WriteOpsPending = PVRSRVGetWriteOpsPending(apsSrcSync[i], IMG_TRUE);
+		psCommand->psSrcSync[i].ui32ReadOpsPending = PVRSRVGetReadOpsPending(apsSrcSync[i], IMG_TRUE);
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVInsertCommandKM: Src %lu RO-VA:0x%lx WO-VA:0x%lx ROP:0x%lx WOP:0x%lx",
+				i, psCommand->psSrcSync[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psCommand->psSrcSync[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psCommand->psSrcSync[i].ui32ReadOpsPending,
+				psCommand->psSrcSync[i].ui32WriteOpsPending));
+	}
+
+
+	*ppsCommand = psCommand;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSubmitCommandKM(PVRSRV_QUEUE_INFO *psQueue,
+												PVRSRV_COMMAND *psCommand)
+{
+
+
+
+	if (psCommand->ui32DstSyncCount > 0)
+	{
+		psCommand->psDstSync = (PVRSRV_SYNC_OBJECT*)(((IMG_UINTPTR_T)psQueue->pvLinQueueKM)
+									+ psQueue->ui32WriteOffset + sizeof(PVRSRV_COMMAND));
+	}
+
+	if (psCommand->ui32SrcSyncCount > 0)
+	{
+		psCommand->psSrcSync = (PVRSRV_SYNC_OBJECT*)(((IMG_UINTPTR_T)psQueue->pvLinQueueKM)
+									+ psQueue->ui32WriteOffset + sizeof(PVRSRV_COMMAND)
+									+ (psCommand->ui32DstSyncCount * sizeof(PVRSRV_SYNC_OBJECT)));
+	}
+
+	psCommand->pvData = (PVRSRV_SYNC_OBJECT*)(((IMG_UINTPTR_T)psQueue->pvLinQueueKM)
+									+ psQueue->ui32WriteOffset + sizeof(PVRSRV_COMMAND)
+									+ (psCommand->ui32DstSyncCount * sizeof(PVRSRV_SYNC_OBJECT))
+									+ (psCommand->ui32SrcSyncCount * sizeof(PVRSRV_SYNC_OBJECT)));
+
+
+	UPDATE_QUEUE_WOFF(psQueue, psCommand->ui32CmdSize);
+
+	return PVRSRV_OK;
+}
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVProcessCommand(SYS_DATA			*psSysData,
+								  PVRSRV_COMMAND	*psCommand,
+								  IMG_BOOL			bFlush)
+{
+	PVRSRV_SYNC_OBJECT		*psWalkerObj;
+	PVRSRV_SYNC_OBJECT		*psEndObj;
+	IMG_UINT32				i;
+	COMMAND_COMPLETE_DATA	*psCmdCompleteData;
+	PVRSRV_ERROR			eError = PVRSRV_OK;
+	IMG_UINT32				ui32WriteOpsComplete;
+	IMG_UINT32				ui32ReadOpsComplete;
+
+
+	psWalkerObj = psCommand->psDstSync;
+	psEndObj = psWalkerObj + psCommand->ui32DstSyncCount;
+	while (psWalkerObj < psEndObj)
+	{
+		PVRSRV_SYNC_DATA *psSyncData = psWalkerObj->psKernelSyncInfoKM->psSyncData;
+
+		ui32WriteOpsComplete = psSyncData->ui32WriteOpsComplete;
+		ui32ReadOpsComplete = psSyncData->ui32ReadOpsComplete;
+
+		if ((ui32WriteOpsComplete != psWalkerObj->ui32WriteOpsPending)
+		||	(ui32ReadOpsComplete != psWalkerObj->ui32ReadOpsPending))
+		{
+			if (!bFlush ||
+				!SYNCOPS_STALE(ui32WriteOpsComplete, psWalkerObj->ui32WriteOpsPending) ||
+				!SYNCOPS_STALE(ui32ReadOpsComplete, psWalkerObj->ui32ReadOpsPending))
+			{
+				return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+			}
+		}
+
+		psWalkerObj++;
+	}
+
+
+	psWalkerObj = psCommand->psSrcSync;
+	psEndObj = psWalkerObj + psCommand->ui32SrcSyncCount;
+	while (psWalkerObj < psEndObj)
+	{
+		PVRSRV_SYNC_DATA *psSyncData = psWalkerObj->psKernelSyncInfoKM->psSyncData;
+
+		ui32ReadOpsComplete = psSyncData->ui32ReadOpsComplete;
+		ui32WriteOpsComplete = psSyncData->ui32WriteOpsComplete;
+
+		if ((ui32WriteOpsComplete != psWalkerObj->ui32WriteOpsPending)
+		|| (ui32ReadOpsComplete != psWalkerObj->ui32ReadOpsPending))
+		{
+			if (!bFlush &&
+				SYNCOPS_STALE(ui32WriteOpsComplete, psWalkerObj->ui32WriteOpsPending) &&
+				SYNCOPS_STALE(ui32ReadOpsComplete, psWalkerObj->ui32ReadOpsPending))
+			{
+				PVR_DPF((PVR_DBG_WARNING,
+						"PVRSRVProcessCommand: Stale syncops psSyncData:0x%x ui32WriteOpsComplete:0x%x ui32WriteOpsPending:0x%x",
+						psSyncData, ui32WriteOpsComplete, psWalkerObj->ui32WriteOpsPending));
+			}
+
+			if (!bFlush ||
+				!SYNCOPS_STALE(ui32WriteOpsComplete, psWalkerObj->ui32WriteOpsPending) ||
+				!SYNCOPS_STALE(ui32ReadOpsComplete, psWalkerObj->ui32ReadOpsPending))
+			{
+				return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+			}
+		}
+		psWalkerObj++;
+	}
+
+
+	if (psCommand->ui32DevIndex >= SYS_DEVICE_COUNT)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+					"PVRSRVProcessCommand: invalid DeviceType 0x%x",
+					psCommand->ui32DevIndex));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	psCmdCompleteData = psSysData->ppsCmdCompleteData[psCommand->ui32DevIndex][psCommand->CommandType];
+	if (psCmdCompleteData->bInUse)
+	{
+
+		return PVRSRV_ERROR_FAILED_DEPENDENCIES;
+	}
+
+
+	psCmdCompleteData->bInUse = IMG_TRUE;
+
+
+	psCmdCompleteData->ui32DstSyncCount = psCommand->ui32DstSyncCount;
+	for (i=0; i<psCommand->ui32DstSyncCount; i++)
+	{
+		psCmdCompleteData->psDstSync[i] = psCommand->psDstSync[i];
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVProcessCommand: Dst %lu RO-VA:0x%lx WO-VA:0x%lx ROP:0x%lx WOP:0x%lx",
+				i, psCmdCompleteData->psDstSync[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psDstSync[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psDstSync[i].ui32ReadOpsPending,
+				psCmdCompleteData->psDstSync[i].ui32WriteOpsPending));
+	}
+
+
+	psCmdCompleteData->ui32SrcSyncCount = psCommand->ui32SrcSyncCount;
+	for (i=0; i<psCommand->ui32SrcSyncCount; i++)
+	{
+		psCmdCompleteData->psSrcSync[i] = psCommand->psSrcSync[i];
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVProcessCommand: Src %lu RO-VA:0x%lx WO-VA:0x%lx ROP:0x%lx WOP:0x%lx",
+				i, psCmdCompleteData->psSrcSync[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psSrcSync[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psSrcSync[i].ui32ReadOpsPending,
+				psCmdCompleteData->psSrcSync[i].ui32WriteOpsPending));
+	}
+
+
+
+
+
+
+
+
+
+
+
+	if (psSysData->ppfnCmdProcList[psCommand->ui32DevIndex][psCommand->CommandType]((IMG_HANDLE)psCmdCompleteData,
+																				psCommand->ui32DataSize,
+																				psCommand->pvData) == IMG_FALSE)
+	{
+
+
+
+		psCmdCompleteData->bInUse = IMG_FALSE;
+		eError = PVRSRV_ERROR_CMD_NOT_PROCESSED;
+	}
+
+	return eError;
+}
+
+
+IMG_VOID PVRSRVProcessQueues_ForEachCb(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	if (psDeviceNode->bReProcessDeviceCommandComplete &&
+		psDeviceNode->pfnDeviceCommandComplete != IMG_NULL)
+	{
+		(*psDeviceNode->pfnDeviceCommandComplete)(psDeviceNode);
+	}
+}
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVProcessQueues(IMG_UINT32	ui32CallerID,
+								 IMG_BOOL	bFlush)
+{
+	PVRSRV_QUEUE_INFO 	*psQueue;
+	SYS_DATA			*psSysData;
+	PVRSRV_COMMAND 		*psCommand;
+	PVRSRV_ERROR		eError;
+
+	SysAcquireData(&psSysData);
+
+
+	psSysData->bReProcessQueues = IMG_FALSE;
+
+
+	eError = OSLockResource(&psSysData->sQProcessResource,
+							ui32CallerID);
+	if(eError != PVRSRV_OK)
+	{
+
+		psSysData->bReProcessQueues = IMG_TRUE;
+
+
+		if(ui32CallerID == ISR_ID)
+		{
+			if (bFlush)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"PVRSRVProcessQueues: Couldn't acquire queue processing lock for FLUSH"));
+			}
+			else
+			{
+				PVR_DPF((PVR_DBG_MESSAGE,"PVRSRVProcessQueues: Couldn't acquire queue processing lock"));
+			}
+		}
+		else
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,"PVRSRVProcessQueues: Queue processing lock-acquire failed when called from the Services driver."));
+			PVR_DPF((PVR_DBG_MESSAGE,"                     This is due to MISR queue processing being interrupted by the Services driver."));
+		}
+
+		return PVRSRV_OK;
+	}
+
+	psQueue = psSysData->psQueueList;
+
+	if(!psQueue)
+	{
+		PVR_DPF((PVR_DBG_MESSAGE,"No Queues installed - cannot process commands"));
+	}
+
+	if (bFlush)
+	{
+		PVRSRVSetDCState(DC_STATE_FLUSH_COMMANDS);
+	}
+
+	while (psQueue)
+	{
+		while (psQueue->ui32ReadOffset != psQueue->ui32WriteOffset)
+		{
+			psCommand = (PVRSRV_COMMAND*)((IMG_UINTPTR_T)psQueue->pvLinQueueKM + psQueue->ui32ReadOffset);
+
+			if (PVRSRVProcessCommand(psSysData, psCommand, bFlush) == PVRSRV_OK)
+			{
+
+				UPDATE_QUEUE_ROFF(psQueue, psCommand->ui32CmdSize)
+
+				if (bFlush)
+				{
+					continue;
+				}
+			}
+
+			break;
+		}
+		psQueue = psQueue->psNextKM;
+	}
+
+	if (bFlush)
+	{
+		PVRSRVSetDCState(DC_STATE_NO_FLUSH_COMMANDS);
+	}
+
+
+	List_PVRSRV_DEVICE_NODE_ForEach(psSysData->psDeviceNodeList,
+									PVRSRVProcessQueues_ForEachCb);
+
+
+
+	OSUnlockResource(&psSysData->sQProcessResource, ui32CallerID);
+
+
+	if(psSysData->bReProcessQueues)
+	{
+		return PVRSRV_ERROR_PROCESSING_BLOCKED;
+	}
+
+	return PVRSRV_OK;
+}
+
+#if defined(SUPPORT_CUSTOM_SWAP_OPERATIONS)
+IMG_INTERNAL
+IMG_VOID PVRSRVFreeCommandCompletePacketKM(IMG_HANDLE	hCmdCookie,
+										   IMG_BOOL		bScheduleMISR)
+{
+	COMMAND_COMPLETE_DATA	*psCmdCompleteData = (COMMAND_COMPLETE_DATA *)hCmdCookie;
+	SYS_DATA				*psSysData;
+
+	SysAcquireData(&psSysData);
+
+
+	psCmdCompleteData->bInUse = IMG_FALSE;
+
+
+	PVRSRVCommandCompleteCallbacks();
+
+#if defined(SYS_USING_INTERRUPTS)
+	if(bScheduleMISR)
+	{
+		OSScheduleMISR(psSysData);
+	}
+#else
+	PVR_UNREFERENCED_PARAMETER(bScheduleMISR);
+#endif
+}
+
+#endif
+IMG_EXPORT
+IMG_VOID PVRSRVCommandCompleteKM(IMG_HANDLE	hCmdCookie,
+								 IMG_BOOL	bScheduleMISR)
+{
+	IMG_UINT32				i;
+	COMMAND_COMPLETE_DATA	*psCmdCompleteData = (COMMAND_COMPLETE_DATA *)hCmdCookie;
+	SYS_DATA				*psSysData;
+
+	SysAcquireData(&psSysData);
+
+
+	for (i=0; i<psCmdCompleteData->ui32DstSyncCount; i++)
+	{
+		psCmdCompleteData->psDstSync[i].psKernelSyncInfoKM->psSyncData->ui32WriteOpsComplete++;
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVCommandCompleteKM: Dst %lu RO-VA:0x%lx WO-VA:0x%lx ROP:0x%lx WOP:0x%lx",
+				i, psCmdCompleteData->psDstSync[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psDstSync[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psDstSync[i].ui32ReadOpsPending,
+				psCmdCompleteData->psDstSync[i].ui32WriteOpsPending));
+	}
+
+
+	for (i=0; i<psCmdCompleteData->ui32SrcSyncCount; i++)
+	{
+		psCmdCompleteData->psSrcSync[i].psKernelSyncInfoKM->psSyncData->ui32ReadOpsComplete++;
+
+		PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVCommandCompleteKM: Src %lu RO-VA:0x%lx WO-VA:0x%lx ROP:0x%lx WOP:0x%lx",
+				i, psCmdCompleteData->psSrcSync[i].psKernelSyncInfoKM->sReadOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psSrcSync[i].psKernelSyncInfoKM->sWriteOpsCompleteDevVAddr.uiAddr,
+				psCmdCompleteData->psSrcSync[i].ui32ReadOpsPending,
+				psCmdCompleteData->psSrcSync[i].ui32WriteOpsPending));
+	}
+
+
+	psCmdCompleteData->bInUse = IMG_FALSE;
+
+
+	PVRSRVCommandCompleteCallbacks();
+
+#if defined(SYS_USING_INTERRUPTS)
+	if(bScheduleMISR)
+	{
+		OSScheduleMISR(psSysData);
+	}
+#else
+	PVR_UNREFERENCED_PARAMETER(bScheduleMISR);
+#endif
+}
+
+
+IMG_VOID PVRSRVCommandCompleteCallbacks_ForEachCb(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	if(psDeviceNode->pfnDeviceCommandComplete != IMG_NULL)
+	{
+
+		(*psDeviceNode->pfnDeviceCommandComplete)(psDeviceNode);
+	}
+}
+
+IMG_VOID PVRSRVCommandCompleteCallbacks(IMG_VOID)
+{
+	SYS_DATA				*psSysData;
+	SysAcquireData(&psSysData);
+
+
+	List_PVRSRV_DEVICE_NODE_ForEach(psSysData->psDeviceNodeList,
+									PVRSRVCommandCompleteCallbacks_ForEachCb);
+}
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVRegisterCmdProcListKM(IMG_UINT32		ui32DevIndex,
+										 PFN_CMD_PROC	*ppfnCmdProcList,
+										 IMG_UINT32		ui32MaxSyncsPerCmd[][2],
+										 IMG_UINT32		ui32CmdCount)
+{
+	SYS_DATA				*psSysData;
+	PVRSRV_ERROR			eError;
+	IMG_UINT32				i;
+	IMG_SIZE_T				ui32AllocSize;
+	PFN_CMD_PROC			*ppfnCmdProc;
+	COMMAND_COMPLETE_DATA	*psCmdCompleteData;
+
+
+	if(ui32DevIndex >= SYS_DEVICE_COUNT)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+					"PVRSRVRegisterCmdProcListKM: invalid DeviceType 0x%x",
+					ui32DevIndex));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	SysAcquireData(&psSysData);
+
+
+	eError = OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 ui32CmdCount * sizeof(PFN_CMD_PROC),
+					 (IMG_VOID **)&psSysData->ppfnCmdProcList[ui32DevIndex], IMG_NULL,
+					 "Internal Queue Info structure");
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterCmdProcListKM: Failed to alloc queue"));
+		return eError;
+	}
+
+
+	ppfnCmdProc = psSysData->ppfnCmdProcList[ui32DevIndex];
+
+
+	for (i=0; i<ui32CmdCount; i++)
+	{
+		ppfnCmdProc[i] = ppfnCmdProcList[i];
+	}
+
+
+	ui32AllocSize = ui32CmdCount * sizeof(COMMAND_COMPLETE_DATA*);
+	eError = OSAllocMem( PVRSRV_OS_NON_PAGEABLE_HEAP,
+					 ui32AllocSize,
+					 (IMG_VOID **)&psSysData->ppsCmdCompleteData[ui32DevIndex], IMG_NULL,
+					 "Array of Pointers for Command Store");
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterCmdProcListKM: Failed to alloc CC data"));
+		goto ErrorExit;
+	}
+
+	for (i=0; i<ui32CmdCount; i++)
+	{
+
+
+		ui32AllocSize = sizeof(COMMAND_COMPLETE_DATA)
+					  + ((ui32MaxSyncsPerCmd[i][0]
+					  +	ui32MaxSyncsPerCmd[i][1])
+					  * sizeof(PVRSRV_SYNC_OBJECT));
+
+		eError = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+							ui32AllocSize,
+							(IMG_VOID **)&psSysData->ppsCmdCompleteData[ui32DevIndex][i],
+							IMG_NULL,
+							"Command Complete Data");
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterCmdProcListKM: Failed to alloc cmd %d",i));
+			goto ErrorExit;
+		}
+
+
+		OSMemSet(psSysData->ppsCmdCompleteData[ui32DevIndex][i], 0x00, ui32AllocSize);
+
+		psCmdCompleteData = psSysData->ppsCmdCompleteData[ui32DevIndex][i];
+
+
+		psCmdCompleteData->psDstSync = (PVRSRV_SYNC_OBJECT*)
+										(((IMG_UINTPTR_T)psCmdCompleteData)
+										+ sizeof(COMMAND_COMPLETE_DATA));
+		psCmdCompleteData->psSrcSync = (PVRSRV_SYNC_OBJECT*)
+										(((IMG_UINTPTR_T)psCmdCompleteData->psDstSync)
+										+ (sizeof(PVRSRV_SYNC_OBJECT) * ui32MaxSyncsPerCmd[i][0]));
+
+		psCmdCompleteData->ui32AllocSize = ui32AllocSize;
+	}
+
+	return PVRSRV_OK;
+
+ErrorExit:
+
+
+
+	if(psSysData->ppsCmdCompleteData[ui32DevIndex] != IMG_NULL)
+	{
+		for (i=0; i<ui32CmdCount; i++)
+		{
+			if (psSysData->ppsCmdCompleteData[ui32DevIndex][i] != IMG_NULL)
+			{
+				ui32AllocSize = sizeof(COMMAND_COMPLETE_DATA)
+							  + ((ui32MaxSyncsPerCmd[i][0]
+							  +	ui32MaxSyncsPerCmd[i][1])
+							  * sizeof(PVRSRV_SYNC_OBJECT));
+				OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP, ui32AllocSize, psSysData->ppsCmdCompleteData[ui32DevIndex][i], IMG_NULL);
+				psSysData->ppsCmdCompleteData[ui32DevIndex][i] = IMG_NULL;
+			}
+		}
+		ui32AllocSize = ui32CmdCount * sizeof(COMMAND_COMPLETE_DATA*);
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP, ui32AllocSize, psSysData->ppsCmdCompleteData[ui32DevIndex], IMG_NULL);
+		psSysData->ppsCmdCompleteData[ui32DevIndex] = IMG_NULL;
+	}
+
+	if(psSysData->ppfnCmdProcList[ui32DevIndex] != IMG_NULL)
+	{
+		ui32AllocSize = ui32CmdCount * sizeof(PFN_CMD_PROC);
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP, ui32AllocSize, psSysData->ppfnCmdProcList[ui32DevIndex], IMG_NULL);
+		psSysData->ppfnCmdProcList[ui32DevIndex] = IMG_NULL;
+	}
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR PVRSRVRemoveCmdProcListKM(IMG_UINT32 ui32DevIndex,
+									   IMG_UINT32 ui32CmdCount)
+{
+	SYS_DATA		*psSysData;
+	IMG_UINT32		i;
+
+
+	if(ui32DevIndex >= SYS_DEVICE_COUNT)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+					"PVRSRVRemoveCmdProcListKM: invalid DeviceType 0x%x",
+					ui32DevIndex));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	SysAcquireData(&psSysData);
+
+	if(psSysData->ppsCmdCompleteData[ui32DevIndex] == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PVRSRVRemoveCmdProcListKM: Invalid command array"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+	else
+	{
+		for(i=0; i<ui32CmdCount; i++)
+		{
+
+			if(psSysData->ppsCmdCompleteData[ui32DevIndex][i] != IMG_NULL)
+			{
+				OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+						 psSysData->ppsCmdCompleteData[ui32DevIndex][i]->ui32AllocSize,
+						 psSysData->ppsCmdCompleteData[ui32DevIndex][i],
+						 IMG_NULL);
+				psSysData->ppsCmdCompleteData[ui32DevIndex][i] = IMG_NULL;
+			}
+		}
+
+
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				 ui32CmdCount * sizeof(COMMAND_COMPLETE_DATA*),
+				 psSysData->ppsCmdCompleteData[ui32DevIndex],
+				 IMG_NULL);
+		psSysData->ppsCmdCompleteData[ui32DevIndex] = IMG_NULL;
+	}
+
+
+	if(psSysData->ppfnCmdProcList[ui32DevIndex] != IMG_NULL)
+	{
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				 ui32CmdCount * sizeof(PFN_CMD_PROC),
+				 psSysData->ppfnCmdProcList[ui32DevIndex],
+				 IMG_NULL);
+		psSysData->ppfnCmdProcList[ui32DevIndex] = IMG_NULL;
+	}
+
+	return PVRSRV_OK;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/ra.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/ra.c
new file mode 100644
index 0000000..c2f7fb5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/ra.c
@@ -0,0 +1,1867 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "hash.h"
+#include "ra.h"
+#include "buffer_manager.h"
+#include "osfunc.h"
+
+#ifdef __linux__
+#include <linux/kernel.h>
+#include "proc.h"
+#endif
+
+#ifdef USE_BM_FREESPACE_CHECK
+#include <stdio.h>
+#endif
+
+#define MINIMUM_HASH_SIZE (64)
+
+#if defined(VALIDATE_ARENA_TEST)
+
+typedef enum RESOURCE_DESCRIPTOR_TAG {
+
+	RESOURCE_SPAN_LIVE				= 10,
+	RESOURCE_SPAN_FREE,
+	IMPORTED_RESOURCE_SPAN_START,
+	IMPORTED_RESOURCE_SPAN_LIVE,
+	IMPORTED_RESOURCE_SPAN_FREE,
+	IMPORTED_RESOURCE_SPAN_END,
+
+} RESOURCE_DESCRIPTOR;
+
+typedef enum RESOURCE_TYPE_TAG {
+
+	IMPORTED_RESOURCE_TYPE		= 20,
+	NON_IMPORTED_RESOURCE_TYPE
+
+} RESOURCE_TYPE;
+
+
+static IMG_UINT32 ui32BoundaryTagID = 0;
+
+IMG_UINT32 ValidateArena(RA_ARENA *pArena);
+#endif
+
+struct _BT_
+{
+	enum bt_type
+	{
+		btt_span,
+		btt_free,
+		btt_live
+	} type;
+
+
+	IMG_UINTPTR_T base;
+	IMG_SIZE_T uSize;
+
+
+	struct _BT_ *pNextSegment;
+	struct _BT_ *pPrevSegment;
+
+	struct _BT_ *pNextFree;
+	struct _BT_ *pPrevFree;
+
+	BM_MAPPING *psMapping;
+
+#if defined(VALIDATE_ARENA_TEST)
+	RESOURCE_DESCRIPTOR eResourceSpan;
+	RESOURCE_TYPE		eResourceType;
+
+
+	IMG_UINT32			ui32BoundaryTagID;
+#endif
+
+};
+typedef struct _BT_ BT;
+
+
+struct _RA_ARENA_
+{
+
+	IMG_CHAR *name;
+
+
+	IMG_SIZE_T uQuantum;
+
+
+	IMG_BOOL (*pImportAlloc)(IMG_VOID *,
+							 IMG_SIZE_T uSize,
+							 IMG_SIZE_T *pActualSize,
+							 BM_MAPPING **ppsMapping,
+							 IMG_UINT32 uFlags,
+							 IMG_UINTPTR_T *pBase);
+	IMG_VOID (*pImportFree) (IMG_VOID *,
+						 IMG_UINTPTR_T,
+						 BM_MAPPING *psMapping);
+	IMG_VOID (*pBackingStoreFree) (IMG_VOID *, IMG_SIZE_T, IMG_SIZE_T, IMG_HANDLE);
+
+
+	IMG_VOID *pImportHandle;
+
+
+#define FREE_TABLE_LIMIT 32
+
+
+	BT *aHeadFree [FREE_TABLE_LIMIT];
+
+
+	BT *pHeadSegment;
+	BT *pTailSegment;
+
+
+	HASH_TABLE *pSegmentHash;
+
+#ifdef RA_STATS
+	RA_STATISTICS sStatistics;
+#endif
+
+#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+#define PROC_NAME_SIZE		32
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+	struct proc_dir_entry* pProcInfo;
+	struct proc_dir_entry* pProcSegs;
+#else
+	IMG_CHAR szProcInfoName[PROC_NAME_SIZE];
+	IMG_CHAR szProcSegsName[PROC_NAME_SIZE];
+#endif
+
+	IMG_BOOL bInitProcEntry;
+#endif
+};
+#if defined(ENABLE_RA_DUMP)
+IMG_VOID RA_Dump (RA_ARENA *pArena);
+#endif
+
+#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void RA_ProcSeqShowInfo(struct seq_file *sfile, void* el);
+static void* RA_ProcSeqOff2ElementInfo(struct seq_file * sfile, loff_t off);
+
+static void RA_ProcSeqShowRegs(struct seq_file *sfile, void* el);
+static void* RA_ProcSeqOff2ElementRegs(struct seq_file * sfile, loff_t off);
+
+#else
+static IMG_INT
+RA_DumpSegs(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data);
+static IMG_INT
+RA_DumpInfo(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data);
+#endif
+
+#endif
+
+#ifdef USE_BM_FREESPACE_CHECK
+IMG_VOID CheckBMFreespace(IMG_VOID);
+#endif
+
+#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+static IMG_CHAR *ReplaceSpaces(IMG_CHAR * const pS)
+{
+	IMG_CHAR *pT;
+
+	for(pT = pS; *pT != 0; pT++)
+	{
+		if (*pT == ' ' || *pT == '\t')
+		{
+			*pT = '_';
+		}
+	}
+
+	return pS;
+}
+#endif
+
+static IMG_BOOL
+_RequestAllocFail (IMG_VOID *_h,
+				  IMG_SIZE_T _uSize,
+				  IMG_SIZE_T *_pActualSize,
+				  BM_MAPPING **_ppsMapping,
+				  IMG_UINT32 _uFlags,
+				  IMG_UINTPTR_T *_pBase)
+{
+	PVR_UNREFERENCED_PARAMETER (_h);
+	PVR_UNREFERENCED_PARAMETER (_uSize);
+	PVR_UNREFERENCED_PARAMETER (_pActualSize);
+	PVR_UNREFERENCED_PARAMETER (_ppsMapping);
+	PVR_UNREFERENCED_PARAMETER (_uFlags);
+	PVR_UNREFERENCED_PARAMETER (_pBase);
+
+	return IMG_FALSE;
+}
+
+static IMG_UINT32
+pvr_log2 (IMG_SIZE_T n)
+{
+	IMG_UINT32 l = 0;
+	n>>=1;
+	while (n>0)
+	{
+		n>>=1;
+		l++;
+	}
+	return l;
+}
+
+static PVRSRV_ERROR
+_SegmentListInsertAfter (RA_ARENA *pArena,
+						 BT *pInsertionPoint,
+						 BT *pBT)
+{
+	PVR_ASSERT (pArena != IMG_NULL);
+	PVR_ASSERT (pInsertionPoint != IMG_NULL);
+
+	if ((pInsertionPoint == IMG_NULL) || (pArena == IMG_NULL))
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_SegmentListInsertAfter: invalid parameters"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	pBT->pNextSegment = pInsertionPoint->pNextSegment;
+	pBT->pPrevSegment = pInsertionPoint;
+	if (pInsertionPoint->pNextSegment == IMG_NULL)
+		pArena->pTailSegment = pBT;
+	else
+		pInsertionPoint->pNextSegment->pPrevSegment = pBT;
+	pInsertionPoint->pNextSegment = pBT;
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR
+_SegmentListInsert (RA_ARENA *pArena, BT *pBT)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+
+	if (pArena->pHeadSegment == IMG_NULL)
+	{
+		pArena->pHeadSegment = pArena->pTailSegment = pBT;
+		pBT->pNextSegment = pBT->pPrevSegment = IMG_NULL;
+	}
+	else
+	{
+		BT *pBTScan;
+
+		if (pBT->base < pArena->pHeadSegment->base)
+		{
+
+			pBT->pNextSegment = pArena->pHeadSegment;
+			pArena->pHeadSegment->pPrevSegment = pBT;
+			pArena->pHeadSegment = pBT;
+			pBT->pPrevSegment = IMG_NULL;
+		}
+		else
+		{
+
+
+
+
+			pBTScan = pArena->pHeadSegment;
+
+			while ((pBTScan->pNextSegment != IMG_NULL)  && (pBT->base >= pBTScan->pNextSegment->base))
+			{
+				pBTScan = pBTScan->pNextSegment;
+			}
+
+			eError = _SegmentListInsertAfter (pArena, pBTScan, pBT);
+			if (eError != PVRSRV_OK)
+			{
+				return eError;
+			}
+		}
+	}
+	return eError;
+}
+
+static IMG_VOID
+_SegmentListRemove (RA_ARENA *pArena, BT *pBT)
+{
+	if (pBT->pPrevSegment == IMG_NULL)
+		pArena->pHeadSegment = pBT->pNextSegment;
+	else
+		pBT->pPrevSegment->pNextSegment = pBT->pNextSegment;
+
+	if (pBT->pNextSegment == IMG_NULL)
+		pArena->pTailSegment = pBT->pPrevSegment;
+	else
+		pBT->pNextSegment->pPrevSegment = pBT->pPrevSegment;
+}
+
+static BT *
+_SegmentSplit (RA_ARENA *pArena, BT *pBT, IMG_SIZE_T uSize)
+{
+	BT *pNeighbour;
+
+	PVR_ASSERT (pArena != IMG_NULL);
+
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_SegmentSplit: invalid parameter - pArena"));
+		return IMG_NULL;
+	}
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					sizeof(BT),
+					(IMG_VOID **)&pNeighbour, IMG_NULL,
+					"Boundary Tag") != PVRSRV_OK)
+	{
+		return IMG_NULL;
+	}
+
+	OSMemSet(pNeighbour, 0, sizeof(BT));
+
+#if defined(VALIDATE_ARENA_TEST)
+	pNeighbour->ui32BoundaryTagID = ++ui32BoundaryTagID;
+#endif
+
+	pNeighbour->pPrevSegment = pBT;
+	pNeighbour->pNextSegment = pBT->pNextSegment;
+	if (pBT->pNextSegment == IMG_NULL)
+		pArena->pTailSegment = pNeighbour;
+	else
+		pBT->pNextSegment->pPrevSegment = pNeighbour;
+	pBT->pNextSegment = pNeighbour;
+
+	pNeighbour->type = btt_free;
+	pNeighbour->uSize = pBT->uSize - uSize;
+	pNeighbour->base = pBT->base + uSize;
+	pNeighbour->psMapping = pBT->psMapping;
+	pBT->uSize = uSize;
+
+#if defined(VALIDATE_ARENA_TEST)
+	if (pNeighbour->pPrevSegment->eResourceType == IMPORTED_RESOURCE_TYPE)
+	{
+		pNeighbour->eResourceType = IMPORTED_RESOURCE_TYPE;
+		pNeighbour->eResourceSpan = IMPORTED_RESOURCE_SPAN_FREE;
+	}
+	else if (pNeighbour->pPrevSegment->eResourceType == NON_IMPORTED_RESOURCE_TYPE)
+	{
+		pNeighbour->eResourceType = NON_IMPORTED_RESOURCE_TYPE;
+		pNeighbour->eResourceSpan = RESOURCE_SPAN_FREE;
+	}
+	else
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_SegmentSplit: pNeighbour->pPrevSegment->eResourceType unrecognized"));
+		PVR_DBG_BREAK;
+	}
+#endif
+
+	return pNeighbour;
+}
+
+static IMG_VOID
+_FreeListInsert (RA_ARENA *pArena, BT *pBT)
+{
+	IMG_UINT32 uIndex;
+	uIndex = pvr_log2 (pBT->uSize);
+	pBT->type = btt_free;
+	pBT->pNextFree = pArena->aHeadFree [uIndex];
+	pBT->pPrevFree = IMG_NULL;
+	if (pArena->aHeadFree[uIndex] != IMG_NULL)
+		pArena->aHeadFree[uIndex]->pPrevFree = pBT;
+	pArena->aHeadFree [uIndex] = pBT;
+}
+
+static IMG_VOID
+_FreeListRemove (RA_ARENA *pArena, BT *pBT)
+{
+	IMG_UINT32 uIndex;
+	uIndex = pvr_log2 (pBT->uSize);
+	if (pBT->pNextFree != IMG_NULL)
+		pBT->pNextFree->pPrevFree = pBT->pPrevFree;
+	if (pBT->pPrevFree == IMG_NULL)
+		pArena->aHeadFree[uIndex] = pBT->pNextFree;
+	else
+		pBT->pPrevFree->pNextFree = pBT->pNextFree;
+}
+
+static BT *
+_BuildSpanMarker (IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+{
+	BT *pBT;
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					sizeof(BT),
+					(IMG_VOID **)&pBT, IMG_NULL,
+					"Boundary Tag") != PVRSRV_OK)
+	{
+		return IMG_NULL;
+	}
+
+	OSMemSet(pBT, 0, sizeof(BT));
+
+#if defined(VALIDATE_ARENA_TEST)
+	pBT->ui32BoundaryTagID = ++ui32BoundaryTagID;
+#endif
+
+	pBT->type = btt_span;
+	pBT->base = base;
+	pBT->uSize = uSize;
+	pBT->psMapping = IMG_NULL;
+
+	return pBT;
+}
+
+static BT *
+_BuildBT (IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+{
+	BT *pBT;
+
+	if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					sizeof(BT),
+					(IMG_VOID **)&pBT, IMG_NULL,
+					"Boundary Tag") != PVRSRV_OK)
+	{
+		return IMG_NULL;
+	}
+
+	OSMemSet(pBT, 0, sizeof(BT));
+
+#if defined(VALIDATE_ARENA_TEST)
+	pBT->ui32BoundaryTagID = ++ui32BoundaryTagID;
+#endif
+
+	pBT->type = btt_free;
+	pBT->base = base;
+	pBT->uSize = uSize;
+
+	return pBT;
+}
+
+static BT *
+_InsertResource (RA_ARENA *pArena, IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+{
+	BT *pBT;
+	PVR_ASSERT (pArena!=IMG_NULL);
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_InsertResource: invalid parameter - pArena"));
+		return IMG_NULL;
+	}
+
+	pBT = _BuildBT (base, uSize);
+	if (pBT != IMG_NULL)
+	{
+
+#if defined(VALIDATE_ARENA_TEST)
+		pBT->eResourceSpan = RESOURCE_SPAN_FREE;
+		pBT->eResourceType = NON_IMPORTED_RESOURCE_TYPE;
+#endif
+
+		if (_SegmentListInsert (pArena, pBT) != PVRSRV_OK)
+		{
+			PVR_DPF ((PVR_DBG_ERROR,"_InsertResource: call to _SegmentListInsert failed"));
+			return IMG_NULL;
+		}
+		_FreeListInsert (pArena, pBT);
+#ifdef RA_STATS
+		pArena->sStatistics.uTotalResourceCount+=uSize;
+		pArena->sStatistics.uFreeResourceCount+=uSize;
+		pArena->sStatistics.uSpanCount++;
+#endif
+	}
+	return pBT;
+}
+
+static BT *
+_InsertResourceSpan (RA_ARENA *pArena, IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+{
+	PVRSRV_ERROR eError;
+	BT *pSpanStart;
+	BT *pSpanEnd;
+	BT *pBT;
+
+	PVR_ASSERT (pArena != IMG_NULL);
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_InsertResourceSpan: invalid parameter - pArena"));
+		return IMG_NULL;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_InsertResourceSpan: arena='%s', base=0x%x, size=0x%x",
+			  pArena->name, base, uSize));
+
+	pSpanStart = _BuildSpanMarker (base, uSize);
+	if (pSpanStart == IMG_NULL)
+	{
+		goto fail_start;
+	}
+
+#if defined(VALIDATE_ARENA_TEST)
+	pSpanStart->eResourceSpan = IMPORTED_RESOURCE_SPAN_START;
+	pSpanStart->eResourceType = IMPORTED_RESOURCE_TYPE;
+#endif
+
+	pSpanEnd = _BuildSpanMarker (base + uSize, 0);
+	if (pSpanEnd == IMG_NULL)
+	{
+		goto fail_end;
+	}
+
+#if defined(VALIDATE_ARENA_TEST)
+	pSpanEnd->eResourceSpan = IMPORTED_RESOURCE_SPAN_END;
+	pSpanEnd->eResourceType = IMPORTED_RESOURCE_TYPE;
+#endif
+
+	pBT = _BuildBT (base, uSize);
+	if (pBT == IMG_NULL)
+	{
+		goto fail_bt;
+	}
+
+#if defined(VALIDATE_ARENA_TEST)
+	pBT->eResourceSpan = IMPORTED_RESOURCE_SPAN_FREE;
+	pBT->eResourceType = IMPORTED_RESOURCE_TYPE;
+#endif
+
+	eError = _SegmentListInsert (pArena, pSpanStart);
+	if (eError != PVRSRV_OK)
+	{
+		goto fail_SegListInsert;
+	}
+
+	eError = _SegmentListInsertAfter (pArena, pSpanStart, pBT);
+	if (eError != PVRSRV_OK)
+	{
+		goto fail_SegListInsert;
+	}
+
+	_FreeListInsert (pArena, pBT);
+
+	eError = _SegmentListInsertAfter (pArena, pBT, pSpanEnd);
+	if (eError != PVRSRV_OK)
+	{
+		goto fail_SegListInsert;
+	}
+
+#ifdef RA_STATS
+	pArena->sStatistics.uTotalResourceCount+=uSize;
+#endif
+	return pBT;
+
+  fail_SegListInsert:
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pBT, IMG_NULL);
+
+  fail_bt:
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pSpanEnd, IMG_NULL);
+
+  fail_end:
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pSpanStart, IMG_NULL);
+
+  fail_start:
+	return IMG_NULL;
+}
+
+static IMG_VOID
+_FreeBT (RA_ARENA *pArena, BT *pBT, IMG_BOOL bFreeBackingStore)
+{
+	BT *pNeighbour;
+	IMG_UINTPTR_T uOrigBase;
+	IMG_SIZE_T uOrigSize;
+
+	PVR_ASSERT (pArena!=IMG_NULL);
+	PVR_ASSERT (pBT!=IMG_NULL);
+
+	if ((pArena == IMG_NULL) || (pBT == IMG_NULL))
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_FreeBT: invalid parameter"));
+		return;
+	}
+
+#ifdef RA_STATS
+	pArena->sStatistics.uLiveSegmentCount--;
+	pArena->sStatistics.uFreeSegmentCount++;
+	pArena->sStatistics.uFreeResourceCount+=pBT->uSize;
+#endif
+
+	uOrigBase = pBT->base;
+	uOrigSize = pBT->uSize;
+
+
+	pNeighbour = pBT->pPrevSegment;
+	if (pNeighbour!=IMG_NULL
+		&& pNeighbour->type == btt_free
+		&& pNeighbour->base + pNeighbour->uSize == pBT->base)
+	{
+		_FreeListRemove (pArena, pNeighbour);
+		_SegmentListRemove (pArena, pNeighbour);
+		pBT->base = pNeighbour->base;
+		pBT->uSize += pNeighbour->uSize;
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pNeighbour, IMG_NULL);
+
+#ifdef RA_STATS
+		pArena->sStatistics.uFreeSegmentCount--;
+#endif
+	}
+
+
+	pNeighbour = pBT->pNextSegment;
+	if (pNeighbour!=IMG_NULL
+		&& pNeighbour->type == btt_free
+		&& pBT->base + pBT->uSize == pNeighbour->base)
+	{
+		_FreeListRemove (pArena, pNeighbour);
+		_SegmentListRemove (pArena, pNeighbour);
+		pBT->uSize += pNeighbour->uSize;
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pNeighbour, IMG_NULL);
+
+#ifdef RA_STATS
+		pArena->sStatistics.uFreeSegmentCount--;
+#endif
+	}
+
+
+	if (pArena->pBackingStoreFree != IMG_NULL && bFreeBackingStore)
+	{
+		IMG_UINTPTR_T	uRoundedStart, uRoundedEnd;
+
+
+		uRoundedStart = (uOrigBase / pArena->uQuantum) * pArena->uQuantum;
+
+		if (uRoundedStart < pBT->base)
+		{
+			uRoundedStart += pArena->uQuantum;
+		}
+
+
+		uRoundedEnd = ((uOrigBase + uOrigSize + pArena->uQuantum - 1) / pArena->uQuantum) * pArena->uQuantum;
+
+		if (uRoundedEnd > (pBT->base + pBT->uSize))
+		{
+			uRoundedEnd -= pArena->uQuantum;
+		}
+
+		if (uRoundedStart < uRoundedEnd)
+		{
+			pArena->pBackingStoreFree(pArena->pImportHandle, uRoundedStart, uRoundedEnd, (IMG_HANDLE)0);
+		}
+	}
+
+	if (pBT->pNextSegment!=IMG_NULL && pBT->pNextSegment->type == btt_span
+		&& pBT->pPrevSegment!=IMG_NULL && pBT->pPrevSegment->type == btt_span)
+	{
+		BT *next = pBT->pNextSegment;
+		BT *prev = pBT->pPrevSegment;
+		_SegmentListRemove (pArena, next);
+		_SegmentListRemove (pArena, prev);
+		_SegmentListRemove (pArena, pBT);
+		pArena->pImportFree (pArena->pImportHandle, pBT->base, pBT->psMapping);
+#ifdef RA_STATS
+		pArena->sStatistics.uSpanCount--;
+		pArena->sStatistics.uExportCount++;
+		pArena->sStatistics.uFreeSegmentCount--;
+		pArena->sStatistics.uFreeResourceCount-=pBT->uSize;
+		pArena->sStatistics.uTotalResourceCount-=pBT->uSize;
+#endif
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), next, IMG_NULL);
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), prev, IMG_NULL);
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pBT, IMG_NULL);
+
+	}
+	else
+		_FreeListInsert (pArena, pBT);
+}
+
+
+static IMG_BOOL
+_AttemptAllocAligned (RA_ARENA *pArena,
+					  IMG_SIZE_T uSize,
+					  BM_MAPPING **ppsMapping,
+					  IMG_UINT32 uFlags,
+					  IMG_UINT32 uAlignment,
+					  IMG_UINT32 uAlignmentOffset,
+					  IMG_UINTPTR_T *base)
+{
+	IMG_UINT32 uIndex;
+	PVR_ASSERT (pArena!=IMG_NULL);
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"_AttemptAllocAligned: invalid parameter - pArena"));
+		return IMG_FALSE;
+	}
+
+	if (uAlignment>1)
+		uAlignmentOffset %= uAlignment;
+
+
+
+	uIndex = pvr_log2 (uSize);
+
+#if 0
+
+	if (1u<<uIndex < uSize)
+		uIndex++;
+#endif
+
+	while (uIndex < FREE_TABLE_LIMIT && pArena->aHeadFree[uIndex]==IMG_NULL)
+		uIndex++;
+
+	while (uIndex < FREE_TABLE_LIMIT)
+	{
+		if (pArena->aHeadFree[uIndex]!=IMG_NULL)
+		{
+
+			BT *pBT;
+
+			pBT = pArena->aHeadFree [uIndex];
+			while (pBT!=IMG_NULL)
+			{
+				IMG_UINTPTR_T aligned_base;
+
+				if (uAlignment>1)
+					aligned_base = (pBT->base + uAlignmentOffset + uAlignment - 1) / uAlignment * uAlignment - uAlignmentOffset;
+				else
+					aligned_base = pBT->base;
+				PVR_DPF ((PVR_DBG_MESSAGE,
+						  "RA_AttemptAllocAligned: pBT-base=0x%x "
+						  "pBT-size=0x%x alignedbase=0x%x size=0x%x",
+						pBT->base, pBT->uSize, aligned_base, uSize));
+
+				if (pBT->base + pBT->uSize >= aligned_base + uSize)
+				{
+					if(!pBT->psMapping || pBT->psMapping->ui32Flags == uFlags)
+					{
+						_FreeListRemove (pArena, pBT);
+
+						PVR_ASSERT (pBT->type == btt_free);
+
+#ifdef RA_STATS
+						pArena->sStatistics.uLiveSegmentCount++;
+						pArena->sStatistics.uFreeSegmentCount--;
+						pArena->sStatistics.uFreeResourceCount-=pBT->uSize;
+#endif
+
+
+						if (aligned_base > pBT->base)
+						{
+							BT *pNeighbour;
+
+							pNeighbour = _SegmentSplit (pArena, pBT, aligned_base-pBT->base);
+
+							if (pNeighbour==IMG_NULL)
+							{
+								PVR_DPF ((PVR_DBG_ERROR,"_AttemptAllocAligned: Front split failed"));
+
+								_FreeListInsert (pArena, pBT);
+								return IMG_FALSE;
+							}
+
+							_FreeListInsert (pArena, pBT);
+	#ifdef RA_STATS
+							pArena->sStatistics.uFreeSegmentCount++;
+							pArena->sStatistics.uFreeResourceCount+=pBT->uSize;
+	#endif
+							pBT = pNeighbour;
+						}
+
+
+						if (pBT->uSize > uSize)
+						{
+							BT *pNeighbour;
+							pNeighbour = _SegmentSplit (pArena, pBT, uSize);
+
+							if (pNeighbour==IMG_NULL)
+							{
+								PVR_DPF ((PVR_DBG_ERROR,"_AttemptAllocAligned: Back split failed"));
+
+								_FreeListInsert (pArena, pBT);
+								return IMG_FALSE;
+							}
+
+							_FreeListInsert (pArena, pNeighbour);
+	#ifdef RA_STATS
+							pArena->sStatistics.uFreeSegmentCount++;
+							pArena->sStatistics.uFreeResourceCount+=pNeighbour->uSize;
+	#endif
+						}
+
+						pBT->type = btt_live;
+
+#if defined(VALIDATE_ARENA_TEST)
+						if (pBT->eResourceType == IMPORTED_RESOURCE_TYPE)
+						{
+							pBT->eResourceSpan = IMPORTED_RESOURCE_SPAN_LIVE;
+						}
+						else if (pBT->eResourceType == NON_IMPORTED_RESOURCE_TYPE)
+						{
+							pBT->eResourceSpan = RESOURCE_SPAN_LIVE;
+						}
+						else
+						{
+							PVR_DPF ((PVR_DBG_ERROR,"_AttemptAllocAligned ERROR: pBT->eResourceType unrecognized"));
+							PVR_DBG_BREAK;
+						}
+#endif
+						if (!HASH_Insert (pArena->pSegmentHash, pBT->base, (IMG_UINTPTR_T) pBT))
+						{
+							_FreeBT (pArena, pBT, IMG_FALSE);
+							return IMG_FALSE;
+						}
+
+						if (ppsMapping!=IMG_NULL)
+							*ppsMapping = pBT->psMapping;
+
+						*base = pBT->base;
+
+						return IMG_TRUE;
+					}
+					else
+					{
+						PVR_DPF ((PVR_DBG_MESSAGE,
+								"AttemptAllocAligned: mismatch in flags. Import has %x, request was %x", pBT->psMapping->ui32Flags, uFlags));
+
+					}
+				}
+				pBT = pBT->pNextFree;
+			}
+
+		}
+		uIndex++;
+	}
+
+	return IMG_FALSE;
+}
+
+
+
+RA_ARENA *
+RA_Create (IMG_CHAR *name,
+		   IMG_UINTPTR_T base,
+		   IMG_SIZE_T uSize,
+		   BM_MAPPING *psMapping,
+		   IMG_SIZE_T uQuantum,
+		   IMG_BOOL (*imp_alloc)(IMG_VOID *, IMG_SIZE_T uSize, IMG_SIZE_T *pActualSize,
+		                     BM_MAPPING **ppsMapping, IMG_UINT32 _flags, IMG_UINTPTR_T *pBase),
+		   IMG_VOID (*imp_free) (IMG_VOID *, IMG_UINTPTR_T, BM_MAPPING *),
+		   IMG_VOID (*backingstore_free) (IMG_VOID*, IMG_SIZE_T, IMG_SIZE_T, IMG_HANDLE),
+		   IMG_VOID *pImportHandle)
+{
+	RA_ARENA *pArena;
+	BT *pBT;
+	IMG_INT i;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_Create: name='%s', base=0x%x, uSize=0x%x, alloc=0x%x, free=0x%x",
+			  name, base, uSize, imp_alloc, imp_free));
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof (*pArena),
+					 (IMG_VOID **)&pArena, IMG_NULL,
+					 "Resource Arena") != PVRSRV_OK)
+	{
+		goto arena_fail;
+	}
+
+	pArena->name = name;
+	pArena->pImportAlloc = (imp_alloc!=IMG_NULL) ? imp_alloc : _RequestAllocFail;
+	pArena->pImportFree = imp_free;
+	pArena->pBackingStoreFree = backingstore_free;
+	pArena->pImportHandle = pImportHandle;
+	for (i=0; i<FREE_TABLE_LIMIT; i++)
+		pArena->aHeadFree[i] = IMG_NULL;
+	pArena->pHeadSegment = IMG_NULL;
+	pArena->pTailSegment = IMG_NULL;
+	pArena->uQuantum = uQuantum;
+
+#ifdef RA_STATS
+	pArena->sStatistics.uSpanCount = 0;
+	pArena->sStatistics.uLiveSegmentCount = 0;
+	pArena->sStatistics.uFreeSegmentCount = 0;
+	pArena->sStatistics.uFreeResourceCount = 0;
+	pArena->sStatistics.uTotalResourceCount = 0;
+	pArena->sStatistics.uCumulativeAllocs = 0;
+	pArena->sStatistics.uCumulativeFrees = 0;
+	pArena->sStatistics.uImportCount = 0;
+	pArena->sStatistics.uExportCount = 0;
+#endif
+
+#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+	if(strcmp(pArena->name,"") != 0)
+	{
+
+#ifndef PVR_PROC_USE_SEQ_FILE
+		IMG_INT ret;
+		IMG_INT (*pfnCreateProcEntry)(const IMG_CHAR *, read_proc_t, write_proc_t, IMG_VOID *);
+
+		pArena->bInitProcEntry = !PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL);
+
+
+		pfnCreateProcEntry = pArena->bInitProcEntry ? CreateProcEntry : CreatePerProcessProcEntry;
+
+		ret = snprintf(pArena->szProcInfoName, sizeof(pArena->szProcInfoName), "ra_info_%s", pArena->name);
+		if (ret > 0 && ret < sizeof(pArena->szProcInfoName))
+		{
+			(IMG_VOID) pfnCreateProcEntry(ReplaceSpaces(pArena->szProcInfoName), RA_DumpInfo, 0, pArena);
+		}
+		else
+		{
+			pArena->szProcInfoName[0] = 0;
+			PVR_DPF((PVR_DBG_ERROR, "RA_Create: couldn't create ra_info proc entry for arena %s", pArena->name));
+		}
+
+		ret = snprintf(pArena->szProcSegsName, sizeof(pArena->szProcSegsName), "ra_segs_%s", pArena->name);
+		if (ret > 0 && ret < sizeof(pArena->szProcSegsName))
+		{
+			(IMG_VOID) pfnCreateProcEntry(ReplaceSpaces(pArena->szProcSegsName), RA_DumpSegs, 0, pArena);
+		}
+		else
+		{
+			pArena->szProcSegsName[0] = 0;
+			PVR_DPF((PVR_DBG_ERROR, "RA_Create: couldn't create ra_segs proc entry for arena %s", pArena->name));
+		}
+#else
+
+		IMG_INT ret;
+		IMG_CHAR szProcInfoName[PROC_NAME_SIZE];
+		IMG_CHAR szProcSegsName[PROC_NAME_SIZE];
+		struct proc_dir_entry* (*pfnCreateProcEntrySeq)(const IMG_CHAR *,
+										 IMG_VOID*,
+										 pvr_next_proc_seq_t,
+										 pvr_show_proc_seq_t,
+										 pvr_off2element_proc_seq_t,
+										 pvr_startstop_proc_seq_t,
+										 write_proc_t);
+
+		pArena->bInitProcEntry = !PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL);
+
+
+		pfnCreateProcEntrySeq = pArena->bInitProcEntry ? CreateProcEntrySeq : CreatePerProcessProcEntrySeq;
+
+		ret = snprintf(szProcInfoName, sizeof(szProcInfoName), "ra_info_%s", pArena->name);
+		if (ret > 0 && ret < sizeof(szProcInfoName))
+		{
+			pArena->pProcInfo =  pfnCreateProcEntrySeq(ReplaceSpaces(szProcInfoName), pArena, NULL,
+											 RA_ProcSeqShowInfo, RA_ProcSeqOff2ElementInfo, NULL, NULL);
+		}
+		else
+		{
+			pArena->pProcInfo = 0;
+			PVR_DPF((PVR_DBG_ERROR, "RA_Create: couldn't create ra_info proc entry for arena %s", pArena->name));
+		}
+
+		ret = snprintf(szProcSegsName, sizeof(szProcSegsName), "ra_segs_%s", pArena->name);
+		if (ret > 0 && ret < sizeof(szProcInfoName))
+		{
+			pArena->pProcSegs = pfnCreateProcEntrySeq(ReplaceSpaces(szProcSegsName), pArena, NULL,
+											 RA_ProcSeqShowRegs, RA_ProcSeqOff2ElementRegs, NULL, NULL);
+		}
+		else
+		{
+			pArena->pProcSegs = 0;
+			PVR_DPF((PVR_DBG_ERROR, "RA_Create: couldn't create ra_segs proc entry for arena %s", pArena->name));
+		}
+
+#endif
+
+	}
+#endif
+
+	pArena->pSegmentHash = HASH_Create (MINIMUM_HASH_SIZE);
+	if (pArena->pSegmentHash==IMG_NULL)
+	{
+		goto hash_fail;
+	}
+	if (uSize>0)
+	{
+		uSize = (uSize + uQuantum - 1) / uQuantum * uQuantum;
+		pBT = _InsertResource (pArena, base, uSize);
+		if (pBT == IMG_NULL)
+		{
+			goto insert_fail;
+		}
+		pBT->psMapping = psMapping;
+
+	}
+	return pArena;
+
+insert_fail:
+	HASH_Delete (pArena->pSegmentHash);
+hash_fail:
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(RA_ARENA), pArena, IMG_NULL);
+
+arena_fail:
+	return IMG_NULL;
+}
+
+IMG_VOID
+RA_Delete (RA_ARENA *pArena)
+{
+	IMG_UINT32 uIndex;
+
+	PVR_ASSERT(pArena != IMG_NULL);
+
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"RA_Delete: invalid parameter - pArena"));
+		return;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_Delete: name='%s'", pArena->name));
+
+	for (uIndex=0; uIndex<FREE_TABLE_LIMIT; uIndex++)
+		pArena->aHeadFree[uIndex] = IMG_NULL;
+
+	while (pArena->pHeadSegment != IMG_NULL)
+	{
+		BT *pBT = pArena->pHeadSegment;
+
+		if (pBT->type != btt_free)
+		{
+			PVR_DPF ((PVR_DBG_ERROR,"RA_Delete: allocations still exist in the arena that is being destroyed"));
+			PVR_DPF ((PVR_DBG_ERROR,"Likely Cause: client drivers not freeing alocations before destroying devmemcontext"));
+			PVR_DPF ((PVR_DBG_ERROR,"RA_Delete: base = 0x%x size=0x%x", pBT->base, pBT->uSize));
+		}
+
+		_SegmentListRemove (pArena, pBT);
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pBT, IMG_NULL);
+
+#ifdef RA_STATS
+		pArena->sStatistics.uSpanCount--;
+#endif
+	}
+#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+	{
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+		IMG_VOID (*pfnRemoveProcEntrySeq)(struct proc_dir_entry*);
+
+		pfnRemoveProcEntrySeq = pArena->bInitProcEntry ? RemoveProcEntrySeq : RemovePerProcessProcEntrySeq;
+
+		if (pArena->pProcInfo != 0)
+		{
+			pfnRemoveProcEntrySeq( pArena->pProcInfo );
+		}
+
+		if (pArena->pProcSegs != 0)
+		{
+			pfnRemoveProcEntrySeq( pArena->pProcSegs );
+		}
+
+#else
+		IMG_VOID (*pfnRemoveProcEntry)(const IMG_CHAR *);
+
+		pfnRemoveProcEntry = pArena->bInitProcEntry ? RemoveProcEntry : RemovePerProcessProcEntry;
+
+		if (pArena->szProcInfoName[0] != 0)
+		{
+			pfnRemoveProcEntry(pArena->szProcInfoName);
+		}
+
+		if (pArena->szProcSegsName[0] != 0)
+		{
+			pfnRemoveProcEntry(pArena->szProcSegsName);
+		}
+
+#endif
+	}
+#endif
+	HASH_Delete (pArena->pSegmentHash);
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(RA_ARENA), pArena, IMG_NULL);
+
+}
+
+IMG_BOOL
+RA_TestDelete (RA_ARENA *pArena)
+{
+	PVR_ASSERT(pArena != IMG_NULL);
+
+	if (pArena != IMG_NULL)
+	{
+		while (pArena->pHeadSegment != IMG_NULL)
+		{
+			BT *pBT = pArena->pHeadSegment;
+			if (pBT->type != btt_free)
+			{
+				PVR_DPF ((PVR_DBG_ERROR,"RA_TestDelete: detected resource leak!"));
+				PVR_DPF ((PVR_DBG_ERROR,"RA_TestDelete: base = 0x%x size=0x%x", pBT->base, pBT->uSize));
+				return IMG_FALSE;
+			}
+		}
+	}
+
+	return IMG_TRUE;
+}
+
+IMG_BOOL
+RA_Add (RA_ARENA *pArena, IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+{
+	PVR_ASSERT (pArena != IMG_NULL);
+
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"RA_Add: invalid parameter - pArena"));
+		return IMG_FALSE;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_Add: name='%s', base=0x%x, size=0x%x", pArena->name, base, uSize));
+
+	uSize = (uSize + pArena->uQuantum - 1) / pArena->uQuantum * pArena->uQuantum;
+	return ((IMG_BOOL)(_InsertResource (pArena, base, uSize) != IMG_NULL));
+}
+
+IMG_BOOL
+RA_Alloc (RA_ARENA *pArena,
+		  IMG_SIZE_T uRequestSize,
+		  IMG_SIZE_T *pActualSize,
+		  BM_MAPPING **ppsMapping,
+		  IMG_UINT32 uFlags,
+		  IMG_UINT32 uAlignment,
+		  IMG_UINT32 uAlignmentOffset,
+		  IMG_UINTPTR_T *base)
+{
+	IMG_BOOL bResult;
+	IMG_SIZE_T uSize = uRequestSize;
+
+	PVR_ASSERT (pArena!=IMG_NULL);
+
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"RA_Alloc: invalid parameter - pArena"));
+		return IMG_FALSE;
+	}
+
+#if defined(VALIDATE_ARENA_TEST)
+	ValidateArena(pArena);
+#endif
+
+#ifdef USE_BM_FREESPACE_CHECK
+	CheckBMFreespace();
+#endif
+
+	if (pActualSize != IMG_NULL)
+	{
+		*pActualSize = uSize;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_Alloc: arena='%s', size=0x%x(0x%x), alignment=0x%x, offset=0x%x",
+		   pArena->name, uSize, uRequestSize, uAlignment, uAlignmentOffset));
+
+
+
+	bResult = _AttemptAllocAligned (pArena, uSize, ppsMapping, uFlags,
+									uAlignment, uAlignmentOffset, base);
+	if (!bResult)
+	{
+		BM_MAPPING *psImportMapping;
+		IMG_UINTPTR_T import_base;
+		IMG_SIZE_T uImportSize = uSize;
+
+
+
+
+		if (uAlignment > pArena->uQuantum)
+		{
+			uImportSize += (uAlignment - 1);
+		}
+
+
+		uImportSize = ((uImportSize + pArena->uQuantum - 1)/pArena->uQuantum)*pArena->uQuantum;
+
+		bResult =
+			pArena->pImportAlloc (pArena->pImportHandle, uImportSize, &uImportSize,
+								 &psImportMapping, uFlags, &import_base);
+		if (bResult)
+		{
+			BT *pBT;
+			pBT = _InsertResourceSpan (pArena, import_base, uImportSize);
+
+			if (pBT == IMG_NULL)
+			{
+
+				pArena->pImportFree(pArena->pImportHandle, import_base,
+									psImportMapping);
+				PVR_DPF ((PVR_DBG_MESSAGE,
+						  "RA_Alloc: name='%s', size=0x%x failed!",
+						  pArena->name, uSize));
+
+				return IMG_FALSE;
+			}
+			pBT->psMapping = psImportMapping;
+#ifdef RA_STATS
+			pArena->sStatistics.uFreeSegmentCount++;
+			pArena->sStatistics.uFreeResourceCount += uImportSize;
+			pArena->sStatistics.uImportCount++;
+			pArena->sStatistics.uSpanCount++;
+#endif
+			bResult = _AttemptAllocAligned(pArena, uSize, ppsMapping, uFlags,
+										   uAlignment, uAlignmentOffset,
+										   base);
+			if (!bResult)
+			{
+				PVR_DPF ((PVR_DBG_MESSAGE,
+						  "RA_Alloc: name='%s' uAlignment failed!",
+						  pArena->name));
+			}
+		}
+	}
+#ifdef RA_STATS
+	if (bResult)
+		pArena->sStatistics.uCumulativeAllocs++;
+#endif
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_Alloc: name='%s', size=0x%x, *base=0x%x = %d",
+			  pArena->name, uSize, *base, bResult));
+
+
+
+#if defined(VALIDATE_ARENA_TEST)
+	ValidateArena(pArena);
+#endif
+
+	return bResult;
+}
+
+
+#if defined(VALIDATE_ARENA_TEST)
+
+IMG_UINT32 ValidateArena(RA_ARENA *pArena)
+{
+	BT* pSegment;
+	RESOURCE_DESCRIPTOR eNextSpan;
+
+	pSegment = pArena->pHeadSegment;
+
+	if (pSegment == IMG_NULL)
+	{
+		return 0;
+	}
+
+	if (pSegment->eResourceType == IMPORTED_RESOURCE_TYPE)
+	{
+		PVR_ASSERT(pSegment->eResourceSpan == IMPORTED_RESOURCE_SPAN_START);
+
+		while (pSegment->pNextSegment)
+		{
+			eNextSpan = pSegment->pNextSegment->eResourceSpan;
+
+			switch (pSegment->eResourceSpan)
+			{
+				case IMPORTED_RESOURCE_SPAN_LIVE:
+
+					if (!((eNextSpan == IMPORTED_RESOURCE_SPAN_LIVE) ||
+						  (eNextSpan == IMPORTED_RESOURCE_SPAN_FREE) ||
+						  (eNextSpan == IMPORTED_RESOURCE_SPAN_END)))
+					{
+
+						PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+						PVR_DBG_BREAK;
+					}
+				break;
+
+				case IMPORTED_RESOURCE_SPAN_FREE:
+
+					if (!((eNextSpan == IMPORTED_RESOURCE_SPAN_LIVE) ||
+						  (eNextSpan == IMPORTED_RESOURCE_SPAN_END)))
+					{
+
+						PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+						PVR_DBG_BREAK;
+					}
+				break;
+
+				case IMPORTED_RESOURCE_SPAN_END:
+
+					if ((eNextSpan == IMPORTED_RESOURCE_SPAN_LIVE) ||
+						(eNextSpan == IMPORTED_RESOURCE_SPAN_FREE) ||
+						(eNextSpan == IMPORTED_RESOURCE_SPAN_END))
+					{
+
+						PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+						PVR_DBG_BREAK;
+					}
+				break;
+
+
+				case IMPORTED_RESOURCE_SPAN_START:
+
+					if (!((eNextSpan == IMPORTED_RESOURCE_SPAN_LIVE) ||
+						  (eNextSpan == IMPORTED_RESOURCE_SPAN_FREE)))
+					{
+
+						PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+						PVR_DBG_BREAK;
+					}
+				break;
+
+				default:
+					PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+					PVR_DBG_BREAK;
+				break;
+			}
+			pSegment = pSegment->pNextSegment;
+		}
+	}
+	else if (pSegment->eResourceType == NON_IMPORTED_RESOURCE_TYPE)
+	{
+		PVR_ASSERT((pSegment->eResourceSpan == RESOURCE_SPAN_FREE) || (pSegment->eResourceSpan == RESOURCE_SPAN_LIVE));
+
+		while (pSegment->pNextSegment)
+		{
+			eNextSpan = pSegment->pNextSegment->eResourceSpan;
+
+			switch (pSegment->eResourceSpan)
+			{
+				case RESOURCE_SPAN_LIVE:
+
+					if (!((eNextSpan == RESOURCE_SPAN_FREE) ||
+						  (eNextSpan == RESOURCE_SPAN_LIVE)))
+					{
+
+						PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+						PVR_DBG_BREAK;
+					}
+				break;
+
+				case RESOURCE_SPAN_FREE:
+
+					if (!((eNextSpan == RESOURCE_SPAN_FREE) ||
+						  (eNextSpan == RESOURCE_SPAN_LIVE)))
+					{
+
+						PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+						PVR_DBG_BREAK;
+					}
+				break;
+
+				default:
+					PVR_DPF((PVR_DBG_ERROR, "ValidateArena ERROR: adjacent boundary tags %d (base=0x%x) and %d (base=0x%x) are incompatible (arena: %s)",
+								pSegment->ui32BoundaryTagID, pSegment->base, pSegment->pNextSegment->ui32BoundaryTagID, pSegment->pNextSegment->base, pArena->name));
+
+					PVR_DBG_BREAK;
+				break;
+			}
+			pSegment = pSegment->pNextSegment;
+		}
+
+	}
+	else
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"ValidateArena ERROR: pSegment->eResourceType unrecognized"));
+
+		PVR_DBG_BREAK;
+	}
+
+	return 0;
+}
+
+#endif
+
+
+IMG_VOID
+RA_Free (RA_ARENA *pArena, IMG_UINTPTR_T base, IMG_BOOL bFreeBackingStore)
+{
+	BT *pBT;
+
+	PVR_ASSERT (pArena != IMG_NULL);
+
+	if (pArena == IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_ERROR,"RA_Free: invalid parameter - pArena"));
+		return;
+	}
+
+#ifdef USE_BM_FREESPACE_CHECK
+	CheckBMFreespace();
+#endif
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			  "RA_Free: name='%s', base=0x%x", pArena->name, base));
+
+	pBT = (BT *) HASH_Remove (pArena->pSegmentHash, base);
+	PVR_ASSERT (pBT != IMG_NULL);
+
+	if (pBT)
+	{
+		PVR_ASSERT (pBT->base == base);
+
+#ifdef RA_STATS
+		pArena->sStatistics.uCumulativeFrees++;
+#endif
+
+#ifdef USE_BM_FREESPACE_CHECK
+{
+	IMG_BYTE* p;
+	IMG_BYTE* endp;
+
+	p = (IMG_BYTE*)pBT->base + SysGetDevicePhysOffset();
+	endp = (IMG_BYTE*)((IMG_UINT32)(p + pBT->uSize));
+	while ((IMG_UINT32)p & 3)
+	{
+		*p++ = 0xAA;
+	}
+	while (p < (IMG_BYTE*)((IMG_UINT32)endp & 0xfffffffc))
+	{
+		*(IMG_UINT32*)p = 0xAAAAAAAA;
+		p += sizeof(IMG_UINT32);
+	}
+	while (p < endp)
+	{
+		*p++ = 0xAA;
+	}
+	PVR_DPF((PVR_DBG_MESSAGE,"BM_FREESPACE_CHECK: RA_Free Cleared %08X to %08X (size=0x%x)",(IMG_BYTE*)pBT->base + SysGetDevicePhysOffset(),endp-1,pBT->uSize));
+}
+#endif
+		_FreeBT (pArena, pBT, bFreeBackingStore);
+	}
+}
+
+
+IMG_BOOL RA_GetNextLiveSegment(IMG_HANDLE hArena, RA_SEGMENT_DETAILS *psSegDetails)
+{
+	BT        *pBT;
+
+	if (psSegDetails->hSegment)
+	{
+		pBT = (BT *)psSegDetails->hSegment;
+	}
+	else
+	{
+		RA_ARENA *pArena = (RA_ARENA *)hArena;
+
+		pBT = pArena->pHeadSegment;
+	}
+
+	while (pBT != IMG_NULL)
+	{
+		if (pBT->type == btt_live)
+		{
+			psSegDetails->uiSize = pBT->uSize;
+			psSegDetails->sCpuPhyAddr.uiAddr = pBT->base;
+			psSegDetails->hSegment = (IMG_HANDLE)pBT->pNextSegment;
+
+			return IMG_TRUE;
+		}
+
+		pBT = pBT->pNextSegment;
+	}
+
+	psSegDetails->uiSize = 0;
+	psSegDetails->sCpuPhyAddr.uiAddr = 0;
+	psSegDetails->hSegment = (IMG_HANDLE)-1;
+
+	return IMG_FALSE;
+}
+
+
+#ifdef USE_BM_FREESPACE_CHECK
+RA_ARENA* pJFSavedArena = IMG_NULL;
+
+IMG_VOID CheckBMFreespace(IMG_VOID)
+{
+	BT *pBT;
+	IMG_BYTE* p;
+	IMG_BYTE* endp;
+
+	if (pJFSavedArena != IMG_NULL)
+	{
+		for (pBT=pJFSavedArena->pHeadSegment; pBT!=IMG_NULL; pBT=pBT->pNextSegment)
+		{
+			if (pBT->type == btt_free)
+			{
+				p = (IMG_BYTE*)pBT->base + SysGetDevicePhysOffset();
+				endp = (IMG_BYTE*)((IMG_UINT32)(p + pBT->uSize) & 0xfffffffc);
+
+				while ((IMG_UINT32)p & 3)
+				{
+					if (*p++ != 0xAA)
+					{
+						fprintf(stderr,"BM_FREESPACE_CHECK: Blank space at %08X has changed to 0x%x\n",p,*(IMG_UINT32*)p);
+						for (;;);
+						break;
+					}
+				}
+				while (p < endp)
+				{
+					if (*(IMG_UINT32*)p != 0xAAAAAAAA)
+					{
+						fprintf(stderr,"BM_FREESPACE_CHECK: Blank space at %08X has changed to 0x%x\n",p,*(IMG_UINT32*)p);
+						for (;;);
+						break;
+					}
+					p += 4;
+				}
+			}
+		}
+	}
+}
+#endif
+
+
+#if (defined(CONFIG_PROC_FS) && defined(DEBUG)) || defined (RA_STATS)
+static IMG_CHAR *
+_BTType (IMG_INT eType)
+{
+	switch (eType)
+	{
+	case btt_span: return "span";
+	case btt_free: return "free";
+	case btt_live: return "live";
+	}
+	return "junk";
+}
+#endif
+
+#if defined(ENABLE_RA_DUMP)
+IMG_VOID
+RA_Dump (RA_ARENA *pArena)
+{
+	BT *pBT;
+	PVR_ASSERT (pArena != IMG_NULL);
+	PVR_DPF ((PVR_DBG_MESSAGE,"Arena '%s':", pArena->name));
+	PVR_DPF ((PVR_DBG_MESSAGE,"  alloc=%08X free=%08X handle=%08X quantum=%d",
+			 pArena->pImportAlloc, pArena->pImportFree, pArena->pImportHandle,
+			 pArena->uQuantum));
+	PVR_DPF ((PVR_DBG_MESSAGE,"  segment Chain:"));
+	if (pArena->pHeadSegment != IMG_NULL &&
+	    pArena->pHeadSegment->pPrevSegment != IMG_NULL)
+		PVR_DPF ((PVR_DBG_MESSAGE,"  error: head boundary tag has invalid pPrevSegment"));
+	if (pArena->pTailSegment != IMG_NULL &&
+	    pArena->pTailSegment->pNextSegment != IMG_NULL)
+		PVR_DPF ((PVR_DBG_MESSAGE,"  error: tail boundary tag has invalid pNextSegment"));
+
+	for (pBT=pArena->pHeadSegment; pBT!=IMG_NULL; pBT=pBT->pNextSegment)
+	{
+		PVR_DPF ((PVR_DBG_MESSAGE,"\tbase=0x%x size=0x%x type=%s ref=%08X",
+				 (IMG_UINT32) pBT->base, pBT->uSize, _BTType (pBT->type),
+				 pBT->pRef));
+	}
+
+#ifdef HASH_TRACE
+	HASH_Dump (pArena->pSegmentHash);
+#endif
+}
+#endif
+
+
+#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void RA_ProcSeqShowInfo(struct seq_file *sfile, void* el)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)sfile->private;
+	RA_ARENA *pArena = (RA_ARENA *)handlers->data;
+	IMG_INT off = (IMG_INT)el;
+
+	switch (off)
+	{
+	case 1:
+		seq_printf(sfile, "quantum\t\t\t%lu\n", pArena->uQuantum);
+		break;
+	case 2:
+		seq_printf(sfile, "import_handle\t\t%08X\n", (IMG_UINT)pArena->pImportHandle);
+		break;
+#ifdef RA_STATS
+	case 3:
+		seq_printf(sfile,"span count\t\t%lu\n", pArena->sStatistics.uSpanCount);
+		break;
+	case 4:
+		seq_printf(sfile, "live segment count\t%lu\n", pArena->sStatistics.uLiveSegmentCount);
+		break;
+	case 5:
+		seq_printf(sfile, "free segment count\t%lu\n", pArena->sStatistics.uFreeSegmentCount);
+		break;
+	case 6:
+		seq_printf(sfile, "free resource count\t%lu (0x%x)\n",
+							pArena->sStatistics.uFreeResourceCount,
+							(IMG_UINT)pArena->sStatistics.uFreeResourceCount);
+		break;
+	case 7:
+		seq_printf(sfile, "total allocs\t\t%lu\n", pArena->sStatistics.uCumulativeAllocs);
+		break;
+	case 8:
+		seq_printf(sfile, "total frees\t\t%lu\n", pArena->sStatistics.uCumulativeFrees);
+		break;
+	case 9:
+		seq_printf(sfile, "import count\t\t%lu\n", pArena->sStatistics.uImportCount);
+		break;
+	case 10:
+		seq_printf(sfile, "export count\t\t%lu\n", pArena->sStatistics.uExportCount);
+		break;
+#endif
+	}
+
+}
+
+static void* RA_ProcSeqOff2ElementInfo(struct seq_file * sfile, loff_t off)
+{
+#ifdef RA_STATS
+	if(off <= 9)
+#else
+	if(off <= 1)
+#endif
+		return (void*)(IMG_INT)(off+1);
+	return 0;
+}
+
+static void RA_ProcSeqShowRegs(struct seq_file *sfile, void* el)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)sfile->private;
+	RA_ARENA *pArena = (RA_ARENA *)handlers->data;
+	BT *pBT = (BT*)el;
+
+	if (el == PVR_PROC_SEQ_START_TOKEN)
+	{
+		seq_printf(sfile, "Arena \"%s\"\nBase         Size Type Ref\n", pArena->name);
+		return;
+	}
+
+	if (pBT)
+	{
+		seq_printf(sfile, "%08x %8x %4s %08x\n",
+				   (IMG_UINT)pBT->base, (IMG_UINT)pBT->uSize, _BTType (pBT->type),
+			       (IMG_UINT)pBT->psMapping);
+	}
+}
+
+static void* RA_ProcSeqOff2ElementRegs(struct seq_file * sfile, loff_t off)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)sfile->private;
+	RA_ARENA *pArena = (RA_ARENA *)handlers->data;
+	BT *pBT = 0;
+
+	if(off == 0)
+		return PVR_PROC_SEQ_START_TOKEN;
+
+	for (pBT=pArena->pHeadSegment; --off && pBT; pBT=pBT->pNextSegment);
+
+	return (void*)pBT;
+}
+
+
+
+#else
+static IMG_INT
+RA_DumpSegs(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data)
+{
+	BT *pBT = 0;
+	IMG_INT len = 0;
+	RA_ARENA *pArena = (RA_ARENA *)data;
+
+	if (count < 80)
+	{
+		*start = (IMG_CHAR *)0;
+		return (0);
+	}
+	*eof = 0;
+	*start = (IMG_CHAR *)1;
+	if (off == 0)
+	{
+		return printAppend(page, count, 0, "Arena \"%s\"\nBase         Size Type Ref\n", pArena->name);
+	}
+	for (pBT=pArena->pHeadSegment; --off && pBT; pBT=pBT->pNextSegment)
+		;
+	if (pBT)
+	{
+		len = printAppend(page, count, 0, "%08x %8x %4s %08x\n",
+				 			(IMG_UINT)pBT->base, (IMG_UINT)pBT->uSize, _BTType (pBT->type),
+				 			(IMG_UINT)pBT->psMapping);
+	}
+	else
+	{
+		*eof = 1;
+	}
+	return (len);
+}
+
+static IMG_INT
+RA_DumpInfo(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data)
+{
+	IMG_INT len = 0;
+	RA_ARENA *pArena = (RA_ARENA *)data;
+
+	if (count < 80)
+	{
+		*start = (IMG_CHAR *)0;
+		return (0);
+	}
+	*eof = 0;
+	switch (off)
+	{
+	case 0:
+		len = printAppend(page, count, 0, "quantum\t\t\t%lu\n", pArena->uQuantum);
+		break;
+	case 1:
+		len = printAppend(page, count, 0, "import_handle\t\t%08X\n", (IMG_UINT)pArena->pImportHandle);
+		break;
+#ifdef RA_STATS
+	case 2:
+		len = printAppend(page, count, 0, "span count\t\t%lu\n", pArena->sStatistics.uSpanCount);
+		break;
+	case 3:
+		len = printAppend(page, count, 0, "live segment count\t%lu\n", pArena->sStatistics.uLiveSegmentCount);
+		break;
+	case 4:
+		len = printAppend(page, count, 0, "free segment count\t%lu\n", pArena->sStatistics.uFreeSegmentCount);
+		break;
+	case 5:
+		len = printAppend(page, count, 0, "free resource count\t%lu (0x%x)\n",
+							pArena->sStatistics.uFreeResourceCount,
+							(IMG_UINT)pArena->sStatistics.uFreeResourceCount);
+		break;
+	case 6:
+		len = printAppend(page, count, 0, "total allocs\t\t%lu\n", pArena->sStatistics.uCumulativeAllocs);
+		break;
+	case 7:
+		len = printAppend(page, count, 0, "total frees\t\t%lu\n", pArena->sStatistics.uCumulativeFrees);
+		break;
+	case 8:
+		len = printAppend(page, count, 0, "import count\t\t%lu\n", pArena->sStatistics.uImportCount);
+		break;
+	case 9:
+		len = printAppend(page, count, 0, "export count\t\t%lu\n", pArena->sStatistics.uExportCount);
+		break;
+#endif
+
+	default:
+		*eof = 1;
+	}
+	*start = (IMG_CHAR *)1;
+	return (len);
+}
+#endif
+#endif
+
+
+#ifdef RA_STATS
+PVRSRV_ERROR RA_GetStats(RA_ARENA *pArena,
+							IMG_CHAR **ppszStr,
+							IMG_UINT32 *pui32StrLen)
+{
+	IMG_CHAR 	*pszStr = *ppszStr;
+	IMG_UINT32 	ui32StrLen = *pui32StrLen;
+	IMG_INT32	i32Count;
+	BT 			*pBT;
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "\nArena '%s':\n", pArena->name);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "  allocCB=%08X freeCB=%08X handle=%08X quantum=%d\n",
+							 pArena->pImportAlloc,
+							 pArena->pImportFree,
+							 pArena->pImportHandle,
+							 pArena->uQuantum);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "span count\t\t%lu\n", pArena->sStatistics.uSpanCount);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "live segment count\t%lu\n", pArena->sStatistics.uLiveSegmentCount);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "free segment count\t%lu\n", pArena->sStatistics.uFreeSegmentCount);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "free resource count\t%lu (0x%x)\n",
+							pArena->sStatistics.uFreeResourceCount,
+							(IMG_UINT)pArena->sStatistics.uFreeResourceCount);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "total allocs\t\t%lu\n", pArena->sStatistics.uCumulativeAllocs);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "total frees\t\t%lu\n", pArena->sStatistics.uCumulativeFrees);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "import count\t\t%lu\n", pArena->sStatistics.uImportCount);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "export count\t\t%lu\n", pArena->sStatistics.uExportCount);
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	CHECK_SPACE(ui32StrLen);
+	i32Count = OSSNPrintf(pszStr, 100, "  segment Chain:\n");
+	UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+
+	if (pArena->pHeadSegment != IMG_NULL &&
+	    pArena->pHeadSegment->pPrevSegment != IMG_NULL)
+	{
+		CHECK_SPACE(ui32StrLen);
+		i32Count = OSSNPrintf(pszStr, 100, "  error: head boundary tag has invalid pPrevSegment\n");
+		UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+	}
+
+	if (pArena->pTailSegment != IMG_NULL &&
+	    pArena->pTailSegment->pNextSegment != IMG_NULL)
+	{
+		CHECK_SPACE(ui32StrLen);
+		i32Count = OSSNPrintf(pszStr, 100, "  error: tail boundary tag has invalid pNextSegment\n");
+		UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+	}
+
+	for (pBT=pArena->pHeadSegment; pBT!=IMG_NULL; pBT=pBT->pNextSegment)
+	{
+		CHECK_SPACE(ui32StrLen);
+		i32Count = OSSNPrintf(pszStr, 100, "\tbase=0x%x size=0x%x type=%s ref=%08X\n",
+											 (IMG_UINT32) pBT->base,
+											 pBT->uSize,
+											 _BTType(pBT->type),
+											 pBT->psMapping);
+		UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
+	}
+
+	*ppszStr = pszStr;
+	*pui32StrLen = ui32StrLen;
+
+	return PVRSRV_OK;
+}
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/resman.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/resman.c
new file mode 100644
index 0000000..14b16fe
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/common/resman.c
@@ -0,0 +1,704 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "resman.h"
+
+#ifdef __linux__
+
+#include <linux/version.h>
+#include <linux/sched.h>
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
+#include <linux/hardirq.h>
+#else
+#include <asm/hardirq.h>
+#endif
+
+static DEFINE_MUTEX(lock);
+
+#define ACQUIRE_SYNC_OBJ  do {							\
+		if (in_interrupt()) { 							\
+			printk ("ISR cannot take RESMAN mutex\n"); 	\
+			BUG(); 										\
+		} 												\
+		else mutex_lock(&lock); 						\
+} while (0)
+#define RELEASE_SYNC_OBJ mutex_unlock(&lock)
+
+#else
+
+#define ACQUIRE_SYNC_OBJ
+#define RELEASE_SYNC_OBJ
+
+#endif
+
+#define RESMAN_SIGNATURE 0x12345678
+
+typedef struct _RESMAN_ITEM_
+{
+#ifdef DEBUG
+	IMG_UINT32				ui32Signature;
+#endif
+	struct _RESMAN_ITEM_	**ppsThis;
+	struct _RESMAN_ITEM_	*psNext;
+
+	IMG_UINT32				ui32Flags;
+	IMG_UINT32				ui32ResType;
+
+	IMG_PVOID				pvParam;
+	IMG_UINT32				ui32Param;
+
+	RESMAN_FREE_FN			pfnFreeResource;
+} RESMAN_ITEM;
+
+
+typedef struct _RESMAN_CONTEXT_
+{
+#ifdef DEBUG
+	IMG_UINT32					ui32Signature;
+#endif
+	struct	_RESMAN_CONTEXT_	**ppsThis;
+	struct	_RESMAN_CONTEXT_	*psNext;
+
+	PVRSRV_PER_PROCESS_DATA		*psPerProc;
+
+	RESMAN_ITEM					*psResItemList;
+
+} RESMAN_CONTEXT;
+
+
+typedef struct
+{
+	RESMAN_CONTEXT	*psContextList;
+
+} RESMAN_LIST, *PRESMAN_LIST;
+
+
+PRESMAN_LIST	gpsResList = IMG_NULL;
+
+#include "lists.h"
+
+static IMPLEMENT_LIST_ANY_VA(RESMAN_ITEM)
+static IMPLEMENT_LIST_ANY_VA_2(RESMAN_ITEM, IMG_BOOL, IMG_FALSE)
+static IMPLEMENT_LIST_INSERT(RESMAN_ITEM)
+static IMPLEMENT_LIST_REMOVE(RESMAN_ITEM)
+
+static IMPLEMENT_LIST_REMOVE(RESMAN_CONTEXT)
+static IMPLEMENT_LIST_INSERT(RESMAN_CONTEXT)
+
+
+#define PRINT_RESLIST(x, y, z)
+
+static PVRSRV_ERROR FreeResourceByPtr(RESMAN_ITEM *psItem, IMG_BOOL bExecuteCallback);
+
+static PVRSRV_ERROR FreeResourceByCriteria(PRESMAN_CONTEXT	psContext,
+										   IMG_UINT32		ui32SearchCriteria,
+										   IMG_UINT32		ui32ResType,
+										   IMG_PVOID		pvParam,
+										   IMG_UINT32		ui32Param,
+										   IMG_BOOL			bExecuteCallback);
+
+
+#ifdef DEBUG
+	static IMG_VOID ValidateResList(PRESMAN_LIST psResList);
+	#define VALIDATERESLIST() ValidateResList(gpsResList)
+#else
+	#define VALIDATERESLIST()
+#endif
+
+
+
+
+
+
+PVRSRV_ERROR ResManInit(IMG_VOID)
+{
+	if (gpsResList == IMG_NULL)
+	{
+
+		if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(*gpsResList),
+						(IMG_VOID **)&gpsResList, IMG_NULL,
+						"Resource Manager List") != PVRSRV_OK)
+		{
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+
+
+		gpsResList->psContextList = IMG_NULL;
+
+
+		VALIDATERESLIST();
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_VOID ResManDeInit(IMG_VOID)
+{
+	if (gpsResList != IMG_NULL)
+	{
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(*gpsResList), gpsResList, IMG_NULL);
+		gpsResList = IMG_NULL;
+	}
+}
+
+
+PVRSRV_ERROR PVRSRVResManConnect(IMG_HANDLE			hPerProc,
+								 PRESMAN_CONTEXT	*phResManContext)
+{
+	PVRSRV_ERROR	eError;
+	PRESMAN_CONTEXT	psResManContext;
+
+
+	ACQUIRE_SYNC_OBJ;
+
+
+	VALIDATERESLIST();
+
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(*psResManContext),
+						(IMG_VOID **)&psResManContext, IMG_NULL,
+						"Resource Manager Context");
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRSRVResManConnect: ERROR allocating new RESMAN context struct"));
+
+
+		VALIDATERESLIST();
+
+
+		RELEASE_SYNC_OBJ;
+
+		return eError;
+	}
+
+#ifdef DEBUG
+	psResManContext->ui32Signature = RESMAN_SIGNATURE;
+#endif
+	psResManContext->psResItemList	= IMG_NULL;
+	psResManContext->psPerProc = hPerProc;
+
+
+	List_RESMAN_CONTEXT_Insert(&gpsResList->psContextList, psResManContext);
+
+
+	VALIDATERESLIST();
+
+
+	RELEASE_SYNC_OBJ;
+
+	*phResManContext = psResManContext;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_VOID PVRSRVResManDisconnect(PRESMAN_CONTEXT psResManContext,
+								IMG_BOOL		bKernelContext)
+{
+
+	ACQUIRE_SYNC_OBJ;
+
+
+	VALIDATERESLIST();
+
+
+	PRINT_RESLIST(gpsResList, psResManContext, IMG_TRUE);
+
+
+
+	if (!bKernelContext)
+	{
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_OS_USERMODE_MAPPING, 0, 0, IMG_TRUE);
+
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_EVENT_OBJECT, 0, 0, IMG_TRUE);
+
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_MODIFY_SYNC_OPS, 0, 0, IMG_TRUE);
+
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_HW_RENDER_CONTEXT, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_HW_TRANSFER_CONTEXT, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_HW_2D_CONTEXT, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_TRANSFER_CONTEXT, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_SHARED_PB_DESC_CREATE_LOCK, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_SHARED_PB_DESC, 0, 0, IMG_TRUE);
+
+
+
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DISPLAYCLASS_SWAPCHAIN_REF, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DISPLAYCLASS_DEVICE, 0, 0, IMG_TRUE);
+
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_BUFFERCLASS_DEVICE, 0, 0, IMG_TRUE);
+
+
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DEVICECLASSMEM_MAPPING, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DEVICEMEM_WRAP, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DEVICEMEM_MAPPING, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_KERNEL_DEVICEMEM_ALLOCATION, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DEVICEMEM_ALLOCATION, 0, 0, IMG_TRUE);
+		FreeResourceByCriteria(psResManContext, RESMAN_CRITERIA_RESTYPE, RESMAN_TYPE_DEVICEMEM_CONTEXT, 0, 0, IMG_TRUE);
+	}
+
+
+	PVR_ASSERT(psResManContext->psResItemList == IMG_NULL);
+
+
+	List_RESMAN_CONTEXT_Remove(psResManContext);
+
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(RESMAN_CONTEXT), psResManContext, IMG_NULL);
+
+
+
+
+	VALIDATERESLIST();
+
+
+	PRINT_RESLIST(gpsResList, psResManContext, IMG_FALSE);
+
+
+	RELEASE_SYNC_OBJ;
+}
+
+
+PRESMAN_ITEM ResManRegisterRes(PRESMAN_CONTEXT	psResManContext,
+							   IMG_UINT32		ui32ResType,
+							   IMG_PVOID		pvParam,
+							   IMG_UINT32		ui32Param,
+							   RESMAN_FREE_FN	pfnFreeResource)
+{
+	PRESMAN_ITEM	psNewResItem;
+
+	PVR_ASSERT(psResManContext != IMG_NULL);
+	PVR_ASSERT(ui32ResType != 0);
+
+	if (psResManContext == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "ResManRegisterRes: invalid parameter - psResManContext"));
+		return (PRESMAN_ITEM) IMG_NULL;
+	}
+
+
+	ACQUIRE_SYNC_OBJ;
+
+
+	VALIDATERESLIST();
+
+	PVR_DPF((PVR_DBG_MESSAGE, "ResManRegisterRes: register resource "
+			"Context 0x%x, ResType 0x%x, pvParam 0x%x, ui32Param 0x%x, "
+			"FreeFunc %08X",
+			psResManContext, ui32ResType, (IMG_UINT32)pvParam,
+			ui32Param, pfnFreeResource));
+
+
+	if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				   sizeof(RESMAN_ITEM), (IMG_VOID **)&psNewResItem,
+				   IMG_NULL,
+				   "Resource Manager Item") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "ResManRegisterRes: "
+				"ERROR allocating new resource item"));
+
+
+		RELEASE_SYNC_OBJ;
+
+		return((PRESMAN_ITEM)IMG_NULL);
+	}
+
+
+#ifdef DEBUG
+	psNewResItem->ui32Signature		= RESMAN_SIGNATURE;
+#endif
+	psNewResItem->ui32ResType		= ui32ResType;
+	psNewResItem->pvParam			= pvParam;
+	psNewResItem->ui32Param			= ui32Param;
+	psNewResItem->pfnFreeResource	= pfnFreeResource;
+	psNewResItem->ui32Flags		    = 0;
+
+
+	List_RESMAN_ITEM_Insert(&psResManContext->psResItemList, psNewResItem);
+
+
+	VALIDATERESLIST();
+
+
+	RELEASE_SYNC_OBJ;
+
+	return(psNewResItem);
+}
+
+PVRSRV_ERROR ResManFreeResByPtr(RESMAN_ITEM	*psResItem)
+{
+	PVRSRV_ERROR eError;
+
+	PVR_ASSERT(psResItem != IMG_NULL);
+
+	if (psResItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_MESSAGE, "ResManFreeResByPtr: NULL ptr - nothing to do"));
+		return PVRSRV_OK;
+	}
+
+	PVR_DPF((PVR_DBG_MESSAGE, "ResManFreeResByPtr: freeing resource at %08X", psResItem));
+
+
+	ACQUIRE_SYNC_OBJ;
+
+
+	VALIDATERESLIST();
+
+
+	eError = FreeResourceByPtr(psResItem, IMG_TRUE);
+
+
+	VALIDATERESLIST();
+
+
+	RELEASE_SYNC_OBJ;
+
+	return(eError);
+}
+
+
+PVRSRV_ERROR ResManFreeResByCriteria(PRESMAN_CONTEXT	psResManContext,
+									 IMG_UINT32			ui32SearchCriteria,
+									 IMG_UINT32			ui32ResType,
+									 IMG_PVOID			pvParam,
+									 IMG_UINT32			ui32Param)
+{
+	PVRSRV_ERROR	eError;
+
+	PVR_ASSERT(psResManContext != IMG_NULL);
+
+
+	ACQUIRE_SYNC_OBJ;
+
+
+	VALIDATERESLIST();
+
+	PVR_DPF((PVR_DBG_MESSAGE, "ResManFreeResByCriteria: "
+			"Context 0x%x, Criteria 0x%x, Type 0x%x, Addr 0x%x, Param 0x%x",
+			psResManContext, ui32SearchCriteria, ui32ResType,
+			(IMG_UINT32)pvParam, ui32Param));
+
+
+	eError = FreeResourceByCriteria(psResManContext, ui32SearchCriteria,
+									ui32ResType, pvParam, ui32Param,
+									IMG_TRUE);
+
+
+	VALIDATERESLIST();
+
+
+	RELEASE_SYNC_OBJ;
+
+	return eError;
+}
+
+
+PVRSRV_ERROR ResManDissociateRes(RESMAN_ITEM		*psResItem,
+							 PRESMAN_CONTEXT	psNewResManContext)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	PVR_ASSERT(psResItem != IMG_NULL);
+
+	if (psResItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "ResManDissociateRes: invalid parameter - psResItem"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+#ifdef DEBUG
+	PVR_ASSERT(psResItem->ui32Signature == RESMAN_SIGNATURE);
+#endif
+
+	if (psNewResManContext != IMG_NULL)
+	{
+
+		List_RESMAN_ITEM_Remove(psResItem);
+
+
+		List_RESMAN_ITEM_Insert(&psNewResManContext->psResItemList, psResItem);
+
+	}
+	else
+	{
+		eError = FreeResourceByPtr(psResItem, IMG_FALSE);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "ResManDissociateRes: failed to free resource by pointer"));
+			return eError;
+		}
+	}
+
+	return eError;
+}
+
+IMG_BOOL ResManFindResourceByPtr_AnyVaCb(RESMAN_ITEM *psCurItem, va_list va)
+{
+	RESMAN_ITEM		*psItem;
+
+	psItem = va_arg(va, RESMAN_ITEM*);
+
+	return (IMG_BOOL)(psCurItem == psItem);
+}
+
+
+IMG_INTERNAL PVRSRV_ERROR ResManFindResourceByPtr(PRESMAN_CONTEXT	psResManContext,
+												  RESMAN_ITEM		*psItem)
+{
+	PVRSRV_ERROR	eResult;
+
+	PVR_ASSERT(psResManContext != IMG_NULL);
+	PVR_ASSERT(psItem != IMG_NULL);
+
+	if ((psItem == IMG_NULL) || (psResManContext == IMG_NULL))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "ResManFindResourceByPtr: invalid parameter"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+#ifdef DEBUG
+	PVR_ASSERT(psItem->ui32Signature == RESMAN_SIGNATURE);
+#endif
+
+
+	ACQUIRE_SYNC_OBJ;
+
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"FindResourceByPtr: psItem=%08X, psItem->psNext=%08X",
+			psItem, psItem->psNext));
+
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"FindResourceByPtr: Resource Ctx 0x%x, Type 0x%x, Addr 0x%x, "
+			"Param 0x%x, FnCall %08X, Flags 0x%x",
+			psResManContext,
+			psItem->ui32ResType, (IMG_UINT32)psItem->pvParam, psItem->ui32Param,
+			psItem->pfnFreeResource, psItem->ui32Flags));
+
+
+	if(List_RESMAN_ITEM_IMG_BOOL_Any_va(psResManContext->psResItemList,
+										ResManFindResourceByPtr_AnyVaCb,
+										psItem))
+	{
+		eResult = PVRSRV_OK;
+	}
+	else
+	{
+		eResult = PVRSRV_ERROR_NOT_OWNER;
+	}
+
+
+	RELEASE_SYNC_OBJ;
+
+	return eResult;
+}
+
+static PVRSRV_ERROR FreeResourceByPtr(RESMAN_ITEM	*psItem,
+									  IMG_BOOL		bExecuteCallback)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	PVR_ASSERT(psItem != IMG_NULL);
+
+	if (psItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreeResourceByPtr: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+#ifdef DEBUG
+	PVR_ASSERT(psItem->ui32Signature == RESMAN_SIGNATURE);
+#endif
+
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"FreeResourceByPtr: psItem=%08X, psItem->psNext=%08X",
+			psItem, psItem->psNext));
+
+	PVR_DPF((PVR_DBG_MESSAGE,
+			"FreeResourceByPtr: Type 0x%x, Addr 0x%x, "
+			"Param 0x%x, FnCall %08X, Flags 0x%x",
+			psItem->ui32ResType, (IMG_UINT32)psItem->pvParam, psItem->ui32Param,
+			psItem->pfnFreeResource, psItem->ui32Flags));
+
+
+	List_RESMAN_ITEM_Remove(psItem);
+
+
+
+	RELEASE_SYNC_OBJ;
+
+
+	if (bExecuteCallback)
+	{
+		eError = psItem->pfnFreeResource(psItem->pvParam, psItem->ui32Param);
+	 	if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "FreeResourceByPtr: ERROR calling FreeResource function"));
+		}
+	}
+
+
+	ACQUIRE_SYNC_OBJ;
+
+
+	if(OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(RESMAN_ITEM), psItem, IMG_NULL) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "FreeResourceByPtr: ERROR freeing resource list item memory"));
+		eError = PVRSRV_ERROR_GENERIC;
+	}
+
+	return(eError);
+}
+
+IMG_VOID* FreeResourceByCriteria_AnyVaCb(RESMAN_ITEM *psCurItem, va_list va)
+{
+	IMG_UINT32 ui32SearchCriteria;
+	IMG_UINT32 ui32ResType;
+	IMG_PVOID pvParam;
+	IMG_UINT32 ui32Param;
+
+	ui32SearchCriteria = va_arg(va, IMG_UINT32);
+	ui32ResType = va_arg(va, IMG_UINT32);
+	pvParam = va_arg(va, IMG_PVOID);
+	ui32Param = va_arg(va, IMG_UINT32);
+
+
+	if(
+
+		(((ui32SearchCriteria & RESMAN_CRITERIA_RESTYPE) == 0UL) ||
+		(psCurItem->ui32ResType == ui32ResType))
+	&&
+
+		(((ui32SearchCriteria & RESMAN_CRITERIA_PVOID_PARAM) == 0UL) ||
+			 (psCurItem->pvParam == pvParam))
+	&&
+
+		(((ui32SearchCriteria & RESMAN_CRITERIA_UI32_PARAM) == 0UL) ||
+			 (psCurItem->ui32Param == ui32Param))
+		)
+	{
+		return psCurItem;
+	}
+	else
+	{
+		return IMG_NULL;
+	}
+}
+
+static PVRSRV_ERROR FreeResourceByCriteria(PRESMAN_CONTEXT	psResManContext,
+										   IMG_UINT32		ui32SearchCriteria,
+										   IMG_UINT32		ui32ResType,
+										   IMG_PVOID		pvParam,
+										   IMG_UINT32		ui32Param,
+										   IMG_BOOL			bExecuteCallback)
+{
+	PRESMAN_ITEM	psCurItem;
+	PVRSRV_ERROR	eError = PVRSRV_OK;
+
+
+
+	while((psCurItem = (PRESMAN_ITEM)
+				List_RESMAN_ITEM_Any_va(psResManContext->psResItemList,
+										FreeResourceByCriteria_AnyVaCb,
+										ui32SearchCriteria,
+										ui32ResType,
+						 				pvParam,
+						 				ui32Param)) != IMG_NULL
+		  	&& eError == PVRSRV_OK)
+	{
+		eError = FreeResourceByPtr(psCurItem, bExecuteCallback);
+	}
+
+	return eError;
+}
+
+
+#ifdef DEBUG
+static IMG_VOID ValidateResList(PRESMAN_LIST psResList)
+{
+	PRESMAN_ITEM	psCurItem, *ppsThisItem;
+	PRESMAN_CONTEXT	psCurContext, *ppsThisContext;
+
+
+	if (psResList == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_MESSAGE, "ValidateResList: resman not initialised yet"));
+		return;
+	}
+
+	psCurContext = psResList->psContextList;
+	ppsThisContext = &psResList->psContextList;
+
+
+	while(psCurContext != IMG_NULL)
+	{
+
+		PVR_ASSERT(psCurContext->ui32Signature == RESMAN_SIGNATURE);
+		if (psCurContext->ppsThis != ppsThisContext)
+		{
+			PVR_DPF((PVR_DBG_WARNING,
+					"psCC=%08X psCC->ppsThis=%08X psCC->psNext=%08X ppsTC=%08X",
+					psCurContext, psCurContext->ppsThis,
+					psCurContext->psNext, ppsThisContext));
+			PVR_ASSERT(psCurContext->ppsThis == ppsThisContext);
+		}
+
+
+		psCurItem = psCurContext->psResItemList;
+		ppsThisItem = &psCurContext->psResItemList;
+		while(psCurItem != IMG_NULL)
+		{
+
+			PVR_ASSERT(psCurItem->ui32Signature == RESMAN_SIGNATURE);
+			if (psCurItem->ppsThis != ppsThisItem)
+			{
+				PVR_DPF((PVR_DBG_WARNING,
+						"psCurItem=%08X psCurItem->ppsThis=%08X psCurItem->psNext=%08X ppsThisItem=%08X",
+						psCurItem, psCurItem->ppsThis, psCurItem->psNext, ppsThisItem));
+				PVR_ASSERT(psCurItem->ppsThis == ppsThisItem);
+			}
+
+
+			ppsThisItem = &psCurItem->psNext;
+			psCurItem = psCurItem->psNext;
+		}
+
+
+		ppsThisContext = &psCurContext->psNext;
+		psCurContext = psCurContext->psNext;
+	}
+}
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.c
new file mode 100644
index 0000000..ad24b6f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.c
@@ -0,0 +1,2772 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "sgxdefs.h"
+#include "sgxmmu.h"
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "hash.h"
+#include "ra.h"
+#include "pdump_km.h"
+#include "sgxapi_km.h"
+#include "sgxinfo.h"
+#include "sgxinfokm.h"
+#include "mmu.h"
+#include "sgxconfig.h"
+
+#define UINT32_MAX_VALUE	0xFFFFFFFFUL
+
+#define SGX_MAX_PD_ENTRIES	(1<<(SGX_FEATURE_ADDRESS_SPACE_SIZE - SGX_MMU_PT_SHIFT - SGX_MMU_PAGE_SHIFT))
+
+typedef struct _MMU_PT_INFO_
+{
+
+	IMG_VOID *hPTPageOSMemHandle;
+	IMG_CPU_VIRTADDR PTPageCpuVAddr;
+	IMG_UINT32 ui32ValidPTECount;
+} MMU_PT_INFO;
+
+struct _MMU_CONTEXT_
+{
+
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+
+
+	IMG_CPU_VIRTADDR pvPDCpuVAddr;
+	IMG_DEV_PHYADDR sPDDevPAddr;
+
+	IMG_VOID *hPDOSMemHandle;
+
+
+	MMU_PT_INFO *apsPTInfoList[SGX_MAX_PD_ENTRIES];
+
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+
+#if defined(PDUMP)
+	IMG_UINT32 ui32PDumpMMUContextID;
+#endif
+
+	struct _MMU_CONTEXT_ *psNext;
+};
+
+struct _MMU_HEAP_
+{
+
+	MMU_CONTEXT			*psMMUContext;
+
+
+
+
+	IMG_UINT32			ui32PDBaseIndex;
+
+	IMG_UINT32			ui32PageTableCount;
+
+	IMG_UINT32			ui32PTETotal;
+
+	IMG_UINT32			ui32PDEPageSizeCtrl;
+
+
+
+
+	IMG_UINT32			ui32DataPageSize;
+
+	IMG_UINT32			ui32DataPageBitWidth;
+
+	IMG_UINT32			ui32DataPageMask;
+
+
+
+
+	IMG_UINT32			ui32PTShift;
+
+	IMG_UINT32			ui32PTBitWidth;
+
+	IMG_UINT32			ui32PTMask;
+
+	IMG_UINT32			ui32PTSize;
+
+	IMG_UINT32			ui32PTECount;
+
+
+
+
+	IMG_UINT32			ui32PDShift;
+
+	IMG_UINT32			ui32PDBitWidth;
+
+	IMG_UINT32			ui32PDMask;
+
+
+
+	RA_ARENA *psVMArena;
+	DEV_ARENA_DESCRIPTOR *psDevArena;
+};
+
+
+
+#if defined (SUPPORT_SGX_MMU_DUMMY_PAGE)
+#define DUMMY_DATA_PAGE_SIGNATURE	0xDEADBEEF
+#endif
+
+#if defined(PDUMP)
+static IMG_VOID
+MMU_PDumpPageTables	(MMU_HEAP *pMMUHeap,
+					 IMG_DEV_VIRTADDR DevVAddr,
+					 IMG_SIZE_T uSize,
+					 IMG_BOOL bForUnmap,
+					 IMG_HANDLE hUniqueTag);
+#endif
+
+#define PAGE_TEST					0
+#if PAGE_TEST
+static IMG_VOID PageTest(IMG_VOID* pMem, IMG_DEV_PHYADDR sDevPAddr);
+#endif
+
+#define PT_DEBUG 0
+#if PT_DEBUG
+static IMG_VOID DumpPT(MMU_PT_INFO *psPTInfoList)
+{
+	IMG_UINT32 *p = (IMG_UINT32*)psPTInfoList->PTPageCpuVAddr;
+	IMG_UINT32 i;
+
+
+	for(i = 0; i < 1024; i += 8)
+	{
+		PVR_DPF((PVR_DBG_WARNING,
+				 "%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx\n",
+				 p[i + 0], p[i + 1], p[i + 2], p[i + 3],
+				 p[i + 4], p[i + 5], p[i + 6], p[i + 7]));
+	}
+}
+
+static IMG_VOID CheckPT(MMU_PT_INFO *psPTInfoList)
+{
+	IMG_UINT32 *p = (IMG_UINT32*) psPTInfoList->PTPageCpuVAddr;
+	IMG_UINT32 i, ui32Count = 0;
+
+
+	for(i = 0; i < 1024; i++)
+		if(p[i] & SGX_MMU_PTE_VALID)
+			ui32Count++;
+
+	if(psPTInfoList->ui32ValidPTECount != ui32Count)
+	{
+		PVR_DPF((PVR_DBG_WARNING, "ui32ValidPTECount: %lu ui32Count: %lu\n",
+				 psPTInfoList->ui32ValidPTECount, ui32Count));
+		DumpPT(psPTInfoList);
+		BUG();
+	}
+}
+#else
+static INLINE IMG_VOID DumpPT(MMU_PT_INFO *psPTInfoList)
+{
+	PVR_UNREFERENCED_PARAMETER(psPTInfoList);
+}
+
+static INLINE IMG_VOID CheckPT(MMU_PT_INFO *psPTInfoList)
+{
+	PVR_UNREFERENCED_PARAMETER(psPTInfoList);
+}
+#endif
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+IMG_VOID
+EnableHostAccess (MMU_CONTEXT *psMMUContext)
+{
+	IMG_UINT32 ui32RegVal;
+	IMG_VOID *pvRegsBaseKM = psMMUContext->psDevInfo->pvRegsBaseKM;
+
+
+
+
+	ui32RegVal = OSReadHWReg(pvRegsBaseKM, EUR_CR_BIF_CTRL);
+
+	OSWriteHWReg(pvRegsBaseKM,
+				EUR_CR_BIF_CTRL,
+				ui32RegVal | EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_MASK);
+
+	PDUMPREG(EUR_CR_BIF_CTRL, EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_MASK);
+}
+
+IMG_VOID
+DisableHostAccess (MMU_CONTEXT *psMMUContext)
+{
+	IMG_UINT32 ui32RegVal;
+	IMG_VOID *pvRegsBaseKM = psMMUContext->psDevInfo->pvRegsBaseKM;
+
+
+
+
+
+	OSWriteHWReg(pvRegsBaseKM,
+				EUR_CR_BIF_CTRL,
+				ui32RegVal & ~EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_MASK);
+
+	PDUMPREG(EUR_CR_BIF_CTRL, 0);
+}
+#endif
+
+
+IMG_VOID MMU_InvalidateSystemLevelCache(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	#if defined(SGX_FEATURE_MP)
+	psDevInfo->ui32CacheControl |= SGXMKIF_CC_INVAL_BIF_SL;
+	#else
+
+	PVR_UNREFERENCED_PARAMETER(psDevInfo);
+	#endif
+}
+
+
+IMG_VOID MMU_InvalidateDirectoryCache(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	psDevInfo->ui32CacheControl |= SGXMKIF_CC_INVAL_BIF_PD;
+	#if defined(SGX_FEATURE_SYSTEM_CACHE)
+	MMU_InvalidateSystemLevelCache(psDevInfo);
+	#endif
+}
+
+
+IMG_VOID MMU_InvalidatePageTableCache(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	psDevInfo->ui32CacheControl |= SGXMKIF_CC_INVAL_BIF_PT;
+	#if defined(SGX_FEATURE_SYSTEM_CACHE)
+	MMU_InvalidateSystemLevelCache(psDevInfo);
+	#endif
+}
+
+
+static IMG_BOOL
+_AllocPageTableMemory (MMU_HEAP *pMMUHeap,
+						MMU_PT_INFO *psPTInfoList,
+						IMG_DEV_PHYADDR	*psDevPAddr)
+{
+	IMG_DEV_PHYADDR	sDevPAddr;
+	IMG_CPU_PHYADDR sCpuPAddr;
+
+
+
+
+	if(pMMUHeap->psDevArena->psDeviceMemoryHeapInfo->psLocalDevMemArena == IMG_NULL)
+	{
+
+		if (OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						   pMMUHeap->ui32PTSize,
+						   SGX_MMU_PAGE_SIZE,
+						   (IMG_VOID **)&psPTInfoList->PTPageCpuVAddr,
+						   &psPTInfoList->hPTPageOSMemHandle) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "_AllocPageTableMemory: ERROR call to OSAllocPages failed"));
+			return IMG_FALSE;
+		}
+
+
+		if(psPTInfoList->PTPageCpuVAddr)
+		{
+			sCpuPAddr = OSMapLinToCPUPhys(psPTInfoList->PTPageCpuVAddr);
+		}
+		else
+		{
+
+			sCpuPAddr = OSMemHandleToCpuPAddr(psPTInfoList->hPTPageOSMemHandle, 0);
+		}
+
+		sDevPAddr = SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+	}
+	else
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+
+
+
+
+
+		if(RA_Alloc(pMMUHeap->psDevArena->psDeviceMemoryHeapInfo->psLocalDevMemArena,
+					SGX_MMU_PAGE_SIZE,
+					IMG_NULL,
+					IMG_NULL,
+					0,
+					SGX_MMU_PAGE_SIZE,
+					0,
+					&(sSysPAddr.uiAddr))!= IMG_TRUE)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "_AllocPageTableMemory: ERROR call to RA_Alloc failed"));
+			return IMG_FALSE;
+		}
+
+
+		sCpuPAddr = SysSysPAddrToCpuPAddr(sSysPAddr);
+
+		psPTInfoList->PTPageCpuVAddr = OSMapPhysToLin(sCpuPAddr,
+													SGX_MMU_PAGE_SIZE,
+													PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+													&psPTInfoList->hPTPageOSMemHandle);
+		if(!psPTInfoList->PTPageCpuVAddr)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "_AllocPageTableMemory: ERROR failed to map page tables"));
+			return IMG_FALSE;
+		}
+
+
+		sDevPAddr = SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+		#if PAGE_TEST
+		PageTest(psPTInfoList->PTPageCpuVAddr, sDevPAddr);
+		#endif
+	}
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+	{
+		IMG_UINT32 *pui32Tmp;
+		IMG_UINT32 i;
+
+		pui32Tmp = (IMG_UINT32*)psPTInfoList->PTPageCpuVAddr;
+
+		for(i=0; i<pMMUHeap->ui32PTECount; i++)
+		{
+			pui32Tmp[i] = (pMMUHeap->psMMUContext->psDevInfo->sDummyDataDevPAddr.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+						| SGX_MMU_PTE_VALID;
+		}
+	}
+#else
+
+	OSMemSet(psPTInfoList->PTPageCpuVAddr, 0, pMMUHeap->ui32PTSize);
+#endif
+
+
+	PDUMPMALLOCPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psPTInfoList->PTPageCpuVAddr, pMMUHeap->ui32PTSize, PDUMP_PT_UNIQUETAG);
+
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, psPTInfoList->PTPageCpuVAddr, pMMUHeap->ui32PTSize, 0, IMG_TRUE, PDUMP_PT_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+
+
+	*psDevPAddr = sDevPAddr;
+
+	return IMG_TRUE;
+}
+
+
+static IMG_VOID
+_FreePageTableMemory (MMU_HEAP *pMMUHeap, MMU_PT_INFO *psPTInfoList)
+{
+
+
+
+
+	if(pMMUHeap->psDevArena->psDeviceMemoryHeapInfo->psLocalDevMemArena == IMG_NULL)
+	{
+
+		OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+					  pMMUHeap->ui32PTSize,
+					  psPTInfoList->PTPageCpuVAddr,
+					  psPTInfoList->hPTPageOSMemHandle);
+	}
+	else
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+		IMG_CPU_PHYADDR sCpuPAddr;
+
+
+		sCpuPAddr = OSMapLinToCPUPhys(psPTInfoList->PTPageCpuVAddr);
+		sSysPAddr = SysCpuPAddrToSysPAddr (sCpuPAddr);
+
+
+
+		OSUnMapPhysToLin(psPTInfoList->PTPageCpuVAddr,
+                         SGX_MMU_PAGE_SIZE,
+                         PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+                         psPTInfoList->hPTPageOSMemHandle);
+
+
+
+
+		RA_Free (pMMUHeap->psDevArena->psDeviceMemoryHeapInfo->psLocalDevMemArena, sSysPAddr.uiAddr, IMG_FALSE);
+	}
+}
+
+
+
+static IMG_VOID
+_DeferredFreePageTable (MMU_HEAP *pMMUHeap, IMG_UINT32 ui32PTIndex, IMG_BOOL bOSFreePT)
+{
+	IMG_UINT32 *pui32PDEntry;
+	IMG_UINT32 i;
+	IMG_UINT32 ui32PDIndex;
+	SYS_DATA *psSysData;
+	MMU_PT_INFO **ppsPTInfoList;
+
+	SysAcquireData(&psSysData);
+
+
+	ui32PDIndex = pMMUHeap->psDevArena->BaseDevVAddr.uiAddr >> pMMUHeap->ui32PDShift;
+
+
+	ppsPTInfoList = &pMMUHeap->psMMUContext->apsPTInfoList[ui32PDIndex];
+
+	{
+#if PT_DEBUG
+		if(ppsPTInfoList[ui32PTIndex] && ppsPTInfoList[ui32PTIndex]->ui32ValidPTECount > 0)
+		{
+			DumpPT(ppsPTInfoList[ui32PTIndex]);
+
+		}
+#endif
+
+
+		PVR_ASSERT(ppsPTInfoList[ui32PTIndex] == IMG_NULL || ppsPTInfoList[ui32PTIndex]->ui32ValidPTECount == 0);
+	}
+
+
+	PDUMPCOMMENT("Free page table (page count == %08X)", pMMUHeap->ui32PageTableCount);
+	if(ppsPTInfoList[ui32PTIndex] && ppsPTInfoList[ui32PTIndex]->PTPageCpuVAddr)
+	{
+		PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, ppsPTInfoList[ui32PTIndex]->PTPageCpuVAddr, pMMUHeap->ui32PTSize, PDUMP_PT_UNIQUETAG);
+	}
+
+	switch(pMMUHeap->psDevArena->DevMemHeapType)
+	{
+		case DEVICE_MEMORY_HEAP_SHARED :
+		case DEVICE_MEMORY_HEAP_SHARED_EXPORTED :
+		{
+
+			MMU_CONTEXT *psMMUContext = (MMU_CONTEXT*)pMMUHeap->psMMUContext->psDevInfo->pvMMUContextList;
+
+			while(psMMUContext)
+			{
+
+				pui32PDEntry = (IMG_UINT32*)psMMUContext->pvPDCpuVAddr;
+				pui32PDEntry += ui32PDIndex;
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+				pui32PDEntry[ui32PTIndex] = (psMMUContext->psDevInfo->sDummyPTDevPAddr.uiAddr
+											>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+											| SGX_MMU_PDE_PAGE_SIZE_4K
+											| SGX_MMU_PDE_VALID;
+#else
+
+				if(bOSFreePT)
+				{
+					pui32PDEntry[ui32PTIndex] = 0;
+				}
+#endif
+
+
+				PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, (IMG_VOID*)&pui32PDEntry[ui32PTIndex], sizeof(IMG_UINT32), 0, IMG_FALSE, PDUMP_PT_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+
+
+				psMMUContext = psMMUContext->psNext;
+			}
+			break;
+		}
+		case DEVICE_MEMORY_HEAP_PERCONTEXT :
+		case DEVICE_MEMORY_HEAP_KERNEL :
+		{
+
+			pui32PDEntry = (IMG_UINT32*)pMMUHeap->psMMUContext->pvPDCpuVAddr;
+			pui32PDEntry += ui32PDIndex;
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+			pui32PDEntry[ui32PTIndex] = (pMMUHeap->psMMUContext->psDevInfo->sDummyPTDevPAddr.uiAddr
+										>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+										| SGX_MMU_PDE_PAGE_SIZE_4K
+										| SGX_MMU_PDE_VALID;
+#else
+
+			if(bOSFreePT)
+			{
+				pui32PDEntry[ui32PTIndex] = 0;
+			}
+#endif
+
+
+			PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, (IMG_VOID*)&pui32PDEntry[ui32PTIndex], sizeof(IMG_UINT32), 0, IMG_FALSE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+			break;
+		}
+		default:
+		{
+			PVR_DPF((PVR_DBG_ERROR, "_DeferredFreePagetable: ERROR invalid heap type"));
+			return;
+		}
+	}
+
+
+	if(ppsPTInfoList[ui32PTIndex] != IMG_NULL)
+	{
+		if(ppsPTInfoList[ui32PTIndex]->PTPageCpuVAddr != IMG_NULL)
+		{
+			IMG_PUINT32 pui32Tmp;
+
+			pui32Tmp = (IMG_UINT32*)ppsPTInfoList[ui32PTIndex]->PTPageCpuVAddr;
+
+
+			for(i=0;
+				(i<pMMUHeap->ui32PTETotal) && (i<pMMUHeap->ui32PTECount);
+				 i++)
+			{
+				pui32Tmp[i] = 0;
+			}
+
+
+
+			if(bOSFreePT)
+			{
+				_FreePageTableMemory(pMMUHeap, ppsPTInfoList[ui32PTIndex]);
+			}
+
+
+
+
+			pMMUHeap->ui32PTETotal -= i;
+		}
+		else
+		{
+
+			pMMUHeap->ui32PTETotal -= pMMUHeap->ui32PTECount;
+		}
+
+		if(bOSFreePT)
+		{
+
+			OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(MMU_PT_INFO),
+						ppsPTInfoList[ui32PTIndex],
+						IMG_NULL);
+			ppsPTInfoList[ui32PTIndex] = IMG_NULL;
+		}
+	}
+	else
+	{
+
+		pMMUHeap->ui32PTETotal -= pMMUHeap->ui32PTECount;
+	}
+
+	PDUMPCOMMENT("Finished free page table (page count == %08X)", pMMUHeap->ui32PageTableCount);
+}
+
+static IMG_VOID
+_DeferredFreePageTables (MMU_HEAP *pMMUHeap)
+{
+	IMG_UINT32 i;
+
+	for(i=0; i<pMMUHeap->ui32PageTableCount; i++)
+	{
+		_DeferredFreePageTable(pMMUHeap, i, IMG_TRUE);
+	}
+	MMU_InvalidateDirectoryCache(pMMUHeap->psMMUContext->psDevInfo);
+}
+
+
+static IMG_BOOL
+_DeferredAllocPagetables(MMU_HEAP *pMMUHeap, IMG_DEV_VIRTADDR DevVAddr, IMG_UINT32 ui32Size)
+{
+	IMG_UINT32 ui32PageTableCount;
+	IMG_UINT32 ui32PDIndex;
+	IMG_UINT32 i;
+	IMG_UINT32 *pui32PDEntry;
+	MMU_PT_INFO **ppsPTInfoList;
+	SYS_DATA *psSysData;
+	IMG_DEV_VIRTADDR sHighDevVAddr;
+
+
+#if SGX_FEATURE_ADDRESS_SPACE_SIZE < 32
+	PVR_ASSERT(DevVAddr.uiAddr < (1<<SGX_FEATURE_ADDRESS_SPACE_SIZE));
+#endif
+
+
+	SysAcquireData(&psSysData);
+
+
+	ui32PDIndex = DevVAddr.uiAddr >> pMMUHeap->ui32PDShift;
+
+
+
+	if((UINT32_MAX_VALUE - DevVAddr.uiAddr)
+		< (ui32Size + pMMUHeap->ui32DataPageMask + pMMUHeap->ui32PTMask))
+	{
+
+		sHighDevVAddr.uiAddr = UINT32_MAX_VALUE;
+	}
+	else
+	{
+		sHighDevVAddr.uiAddr = DevVAddr.uiAddr
+								+ ui32Size
+								+ pMMUHeap->ui32DataPageMask
+								+ pMMUHeap->ui32PTMask;
+	}
+
+	ui32PageTableCount = sHighDevVAddr.uiAddr >> pMMUHeap->ui32PDShift;
+
+	ui32PageTableCount -= ui32PDIndex;
+
+
+	pui32PDEntry = (IMG_UINT32*)pMMUHeap->psMMUContext->pvPDCpuVAddr;
+	pui32PDEntry += ui32PDIndex;
+
+
+	ppsPTInfoList = &pMMUHeap->psMMUContext->apsPTInfoList[ui32PDIndex];
+
+	PDUMPCOMMENT("Alloc page table (page count == %08X)", ui32PageTableCount);
+	PDUMPCOMMENT("Page directory mods (page count == %08X)", ui32PageTableCount);
+
+
+	for(i=0; i<ui32PageTableCount; i++)
+	{
+		if(ppsPTInfoList[i] == IMG_NULL)
+		{
+			OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						 sizeof (MMU_PT_INFO),
+						 (IMG_VOID **)&ppsPTInfoList[i], IMG_NULL,
+						 "MMU Page Table Info");
+			if (ppsPTInfoList[i] == IMG_NULL)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "_DeferredAllocPagetables: ERROR call to OSAllocMem failed"));
+				return IMG_FALSE;
+			}
+			OSMemSet (ppsPTInfoList[i], 0, sizeof(MMU_PT_INFO));
+		}
+
+		if(ppsPTInfoList[i]->hPTPageOSMemHandle == IMG_NULL
+		&& ppsPTInfoList[i]->PTPageCpuVAddr == IMG_NULL)
+		{
+			IMG_DEV_PHYADDR	sDevPAddr;
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+			IMG_UINT32 *pui32Tmp;
+			IMG_UINT32 j;
+#else
+
+			PVR_ASSERT(pui32PDEntry[i] == 0);
+#endif
+
+			if(_AllocPageTableMemory (pMMUHeap, ppsPTInfoList[i], &sDevPAddr) != IMG_TRUE)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "_DeferredAllocPagetables: ERROR call to _AllocPageTableMemory failed"));
+				return IMG_FALSE;
+			}
+
+			switch(pMMUHeap->psDevArena->DevMemHeapType)
+			{
+				case DEVICE_MEMORY_HEAP_SHARED :
+				case DEVICE_MEMORY_HEAP_SHARED_EXPORTED :
+				{
+
+					MMU_CONTEXT *psMMUContext = (MMU_CONTEXT*)pMMUHeap->psMMUContext->psDevInfo->pvMMUContextList;
+
+					while(psMMUContext)
+					{
+
+						pui32PDEntry = (IMG_UINT32*)psMMUContext->pvPDCpuVAddr;
+						pui32PDEntry += ui32PDIndex;
+
+
+						pui32PDEntry[i] = (sDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+										| pMMUHeap->ui32PDEPageSizeCtrl
+										| SGX_MMU_PDE_VALID;
+
+
+						PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, (IMG_VOID*)&pui32PDEntry[i], sizeof(IMG_UINT32), 0, IMG_FALSE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+
+
+						psMMUContext = psMMUContext->psNext;
+					}
+					break;
+				}
+				case DEVICE_MEMORY_HEAP_PERCONTEXT :
+				case DEVICE_MEMORY_HEAP_KERNEL :
+				{
+
+					pui32PDEntry[i] = (sDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+									| pMMUHeap->ui32PDEPageSizeCtrl
+									| SGX_MMU_PDE_VALID;
+
+
+					PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, (IMG_VOID*)&pui32PDEntry[i], sizeof(IMG_UINT32), 0, IMG_FALSE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+					break;
+				}
+				default:
+				{
+					PVR_DPF((PVR_DBG_ERROR, "_DeferredAllocPagetables: ERROR invalid heap type"));
+					return IMG_FALSE;
+				}
+			}
+
+#if !defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+
+
+
+
+			MMU_InvalidateDirectoryCache(pMMUHeap->psMMUContext->psDevInfo);
+#endif
+		}
+		else
+		{
+
+			PVR_ASSERT(pui32PDEntry[i] != 0);
+		}
+	}
+
+	#if defined(SGX_FEATURE_SYSTEM_CACHE)
+	MMU_InvalidateSystemLevelCache(pMMUHeap->psMMUContext->psDevInfo);
+	#endif
+
+	return IMG_TRUE;
+}
+
+
+PVRSRV_ERROR
+MMU_Initialise (PVRSRV_DEVICE_NODE *psDeviceNode, MMU_CONTEXT **ppsMMUContext, IMG_DEV_PHYADDR *psPDDevPAddr)
+{
+	IMG_UINT32 *pui32Tmp;
+	IMG_UINT32 i;
+	IMG_CPU_VIRTADDR pvPDCpuVAddr;
+	IMG_DEV_PHYADDR sPDDevPAddr;
+	IMG_CPU_PHYADDR sCpuPAddr;
+	MMU_CONTEXT *psMMUContext;
+	IMG_HANDLE hPDOSMemHandle;
+	SYS_DATA *psSysData;
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "MMU_Initialise"));
+
+	SysAcquireData(&psSysData);
+
+	OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				 sizeof (MMU_CONTEXT),
+				 (IMG_VOID **)&psMMUContext, IMG_NULL,
+				 "MMU Context");
+	if (psMMUContext == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to OSAllocMem failed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+	OSMemSet (psMMUContext, 0, sizeof(MMU_CONTEXT));
+
+
+	psDevInfo = (PVRSRV_SGXDEV_INFO*)psDeviceNode->pvDevice;
+	psMMUContext->psDevInfo = psDevInfo;
+
+
+	psMMUContext->psDeviceNode = psDeviceNode;
+
+
+	if(psDeviceNode->psLocalDevMemArena == IMG_NULL)
+	{
+		if (OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+							SGX_MMU_PAGE_SIZE,
+							SGX_MMU_PAGE_SIZE,
+							&pvPDCpuVAddr,
+							&hPDOSMemHandle) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to OSAllocPages failed"));
+			return PVRSRV_ERROR_GENERIC;
+		}
+
+		if(pvPDCpuVAddr)
+		{
+			sCpuPAddr = OSMapLinToCPUPhys(pvPDCpuVAddr);
+		}
+		else
+		{
+
+			sCpuPAddr = OSMemHandleToCpuPAddr(hPDOSMemHandle, 0);
+		}
+		sPDDevPAddr = SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+		#if PAGE_TEST
+		PageTest(pvPDCpuVAddr, sPDDevPAddr);
+		#endif
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+		if(!psDevInfo->pvMMUContextList)
+		{
+
+			if (OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+								SGX_MMU_PAGE_SIZE,
+								SGX_MMU_PAGE_SIZE,
+								&psDevInfo->pvDummyPTPageCpuVAddr,
+								&psDevInfo->hDummyPTPageOSMemHandle) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to OSAllocPages failed"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+
+			if(psDevInfo->pvDummyPTPageCpuVAddr)
+			{
+				sCpuPAddr = OSMapLinToCPUPhys(psDevInfo->pvDummyPTPageCpuVAddr);
+			}
+			else
+			{
+
+				sCpuPAddr = OSMemHandleToCpuPAddr(psDevInfo->hDummyPTPageOSMemHandle, 0);
+			}
+			psDevInfo->sDummyPTDevPAddr = SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+
+			if (OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+								SGX_MMU_PAGE_SIZE,
+								SGX_MMU_PAGE_SIZE,
+								&psDevInfo->pvDummyDataPageCpuVAddr,
+								&psDevInfo->hDummyDataPageOSMemHandle) != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to OSAllocPages failed"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+
+			if(psDevInfo->pvDummyDataPageCpuVAddr)
+			{
+				sCpuPAddr = OSMapLinToCPUPhys(psDevInfo->pvDummyDataPageCpuVAddr);
+			}
+			else
+			{
+				sCpuPAddr = OSMemHandleToCpuPAddr(psDevInfo->hDummyDataPageOSMemHandle, 0);
+			}
+			psDevInfo->sDummyDataDevPAddr = SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+		}
+#endif
+	}
+	else
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+
+
+		if(RA_Alloc(psDeviceNode->psLocalDevMemArena,
+					SGX_MMU_PAGE_SIZE,
+					IMG_NULL,
+					IMG_NULL,
+					0,
+					SGX_MMU_PAGE_SIZE,
+					0,
+					&(sSysPAddr.uiAddr))!= IMG_TRUE)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to RA_Alloc failed"));
+			return PVRSRV_ERROR_GENERIC;
+		}
+
+
+		sCpuPAddr = SysSysPAddrToCpuPAddr(sSysPAddr);
+		sPDDevPAddr = SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sSysPAddr);
+		pvPDCpuVAddr = OSMapPhysToLin(sCpuPAddr,
+										SGX_MMU_PAGE_SIZE,
+										PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+										&hPDOSMemHandle);
+		if(!pvPDCpuVAddr)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR failed to map page tables"));
+			return PVRSRV_ERROR_GENERIC;
+		}
+
+		#if PAGE_TEST
+		PageTest(pvPDCpuVAddr, sPDDevPAddr);
+		#endif
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+		if(!psDevInfo->pvMMUContextList)
+		{
+
+			if(RA_Alloc(psDeviceNode->psLocalDevMemArena,
+						SGX_MMU_PAGE_SIZE,
+						IMG_NULL,
+						IMG_NULL,
+						0,
+						SGX_MMU_PAGE_SIZE,
+						0,
+						&(sSysPAddr.uiAddr))!= IMG_TRUE)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to RA_Alloc failed"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+
+
+			sCpuPAddr = SysSysPAddrToCpuPAddr(sSysPAddr);
+			psDevInfo->sDummyPTDevPAddr = SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sSysPAddr);
+			psDevInfo->pvDummyPTPageCpuVAddr = OSMapPhysToLin(sCpuPAddr,
+																SGX_MMU_PAGE_SIZE,
+																PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+																&psDevInfo->hDummyPTPageOSMemHandle);
+			if(!psDevInfo->pvDummyPTPageCpuVAddr)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR failed to map page tables"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+
+
+			if(RA_Alloc(psDeviceNode->psLocalDevMemArena,
+						SGX_MMU_PAGE_SIZE,
+						IMG_NULL,
+						IMG_NULL,
+						0,
+						SGX_MMU_PAGE_SIZE,
+						0,
+						&(sSysPAddr.uiAddr))!= IMG_TRUE)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to RA_Alloc failed"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+
+
+			sCpuPAddr = SysSysPAddrToCpuPAddr(sSysPAddr);
+			psDevInfo->sDummyDataDevPAddr = SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sSysPAddr);
+			psDevInfo->pvDummyDataPageCpuVAddr = OSMapPhysToLin(sCpuPAddr,
+																SGX_MMU_PAGE_SIZE,
+																PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+																&psDevInfo->hDummyDataPageOSMemHandle);
+			if(!psDevInfo->pvDummyDataPageCpuVAddr)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR failed to map page tables"));
+				return PVRSRV_ERROR_GENERIC;
+			}
+		}
+#endif
+	}
+
+
+	PDUMPCOMMENT("Alloc page directory");
+#ifdef SUPPORT_SGX_MMU_BYPASS
+	EnableHostAccess(psMMUContext);
+#endif
+
+	PDUMPMALLOCPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, pvPDCpuVAddr, SGX_MMU_PAGE_SIZE, PDUMP_PD_UNIQUETAG);
+
+	if (pvPDCpuVAddr)
+	{
+		pui32Tmp = (IMG_UINT32 *)pvPDCpuVAddr;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: pvPDCpuVAddr invalid"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+	for(i=0; i<SGX_MMU_PD_SIZE; i++)
+	{
+		pui32Tmp[i] = (psDevInfo->sDummyPTDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+					| SGX_MMU_PDE_PAGE_SIZE_4K
+					| SGX_MMU_PDE_VALID;
+	}
+
+	if(!psDevInfo->pvMMUContextList)
+	{
+
+
+
+		pui32Tmp = (IMG_UINT32 *)psDevInfo->pvDummyPTPageCpuVAddr;
+		for(i=0; i<SGX_MMU_PT_SIZE; i++)
+		{
+			pui32Tmp[i] = (psDevInfo->sDummyDataDevPAddr.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+						| SGX_MMU_PTE_VALID;
+		}
+
+		PDUMPCOMMENT("Dummy Page table contents");
+		PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pvDummyPTPageCpuVAddr, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+
+
+
+		pui32Tmp = (IMG_UINT32 *)psDevInfo->pvDummyDataPageCpuVAddr;
+		for(i=0; i<(SGX_MMU_PAGE_SIZE/4); i++)
+		{
+			pui32Tmp[i] = DUMMY_DATA_PAGE_SIGNATURE;
+		}
+
+		PDUMPCOMMENT("Dummy Data Page contents");
+		PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pvDummyDataPageCpuVAddr, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+	}
+#else
+
+	for(i=0; i<SGX_MMU_PD_SIZE; i++)
+	{
+
+		pui32Tmp[i] = 0;
+	}
+#endif
+
+
+	PDUMPCOMMENT("Page directory contents");
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pvPDCpuVAddr, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+
+
+#if defined(PDUMP)
+	if(PDumpSetMMUContext(PVRSRV_DEVICE_TYPE_SGX,
+							"SGXMEM",
+							&psMMUContext->ui32PDumpMMUContextID,
+							2,
+							PDUMP_PT_UNIQUETAG,
+							pvPDCpuVAddr) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Initialise: ERROR call to PDumpSetMMUContext failed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+#endif
+
+
+	psMMUContext->pvPDCpuVAddr = pvPDCpuVAddr;
+	psMMUContext->sPDDevPAddr = sPDDevPAddr;
+	psMMUContext->hPDOSMemHandle = hPDOSMemHandle;
+
+
+	*ppsMMUContext = psMMUContext;
+
+
+	*psPDDevPAddr = sPDDevPAddr;
+
+
+	psMMUContext->psNext = (MMU_CONTEXT*)psDevInfo->pvMMUContextList;
+	psDevInfo->pvMMUContextList = (IMG_VOID*)psMMUContext;
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+	DisableHostAccess(psMMUContext);
+#endif
+
+	return PVRSRV_OK;
+}
+
+IMG_VOID
+MMU_Finalise (MMU_CONTEXT *psMMUContext)
+{
+	IMG_UINT32 *pui32Tmp, i;
+	SYS_DATA *psSysData;
+	MMU_CONTEXT **ppsMMUContext;
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+	PVRSRV_SGXDEV_INFO *psDevInfo = (PVRSRV_SGXDEV_INFO*)psMMUContext->psDevInfo;
+	MMU_CONTEXT *psMMUContextList = (MMU_CONTEXT*)psDevInfo->pvMMUContextList;
+#endif
+
+	SysAcquireData(&psSysData);
+
+
+	PDUMPCLEARMMUCONTEXT(PVRSRV_DEVICE_TYPE_SGX, "SGXMEM", psMMUContext->ui32PDumpMMUContextID, 2);
+
+
+	PDUMPCOMMENT("Free page directory");
+	PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psMMUContext->pvPDCpuVAddr, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+	PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pvDummyPTPageCpuVAddr, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+	PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pvDummyDataPageCpuVAddr, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+#endif
+
+	pui32Tmp = (IMG_UINT32 *)psMMUContext->pvPDCpuVAddr;
+
+
+	for(i=0; i<SGX_MMU_PD_SIZE; i++)
+	{
+
+		pui32Tmp[i] = 0;
+	}
+
+
+
+
+
+	if(psMMUContext->psDeviceNode->psLocalDevMemArena == IMG_NULL)
+	{
+		OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						SGX_MMU_PAGE_SIZE,
+						psMMUContext->pvPDCpuVAddr,
+						psMMUContext->hPDOSMemHandle);
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+		if(!psMMUContextList->psNext)
+		{
+			OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+							SGX_MMU_PAGE_SIZE,
+							psDevInfo->pvDummyPTPageCpuVAddr,
+							psDevInfo->hDummyPTPageOSMemHandle);
+			OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+							SGX_MMU_PAGE_SIZE,
+							psDevInfo->pvDummyDataPageCpuVAddr,
+							psDevInfo->hDummyDataPageOSMemHandle);
+		}
+#endif
+	}
+	else
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+		IMG_CPU_PHYADDR sCpuPAddr;
+
+
+		sCpuPAddr = OSMapLinToCPUPhys(psMMUContext->pvPDCpuVAddr);
+		sSysPAddr = SysCpuPAddrToSysPAddr(sCpuPAddr);
+
+
+		OSUnMapPhysToLin(psMMUContext->pvPDCpuVAddr,
+							SGX_MMU_PAGE_SIZE,
+                            PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+							psMMUContext->hPDOSMemHandle);
+
+		RA_Free (psMMUContext->psDeviceNode->psLocalDevMemArena, sSysPAddr.uiAddr, IMG_FALSE);
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+		if(!psMMUContextList->psNext)
+		{
+
+			sCpuPAddr = OSMapLinToCPUPhys(psDevInfo->pvDummyPTPageCpuVAddr);
+			sSysPAddr = SysCpuPAddrToSysPAddr(sCpuPAddr);
+
+
+			OSUnMapPhysToLin(psDevInfo->pvDummyPTPageCpuVAddr,
+								SGX_MMU_PAGE_SIZE,
+                                PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+								psDevInfo->hDummyPTPageOSMemHandle);
+
+			RA_Free (psMMUContext->psDeviceNode->psLocalDevMemArena, sSysPAddr.uiAddr, IMG_FALSE);
+
+
+			sCpuPAddr = OSMapLinToCPUPhys(psDevInfo->pvDummyDataPageCpuVAddr);
+			sSysPAddr = SysCpuPAddrToSysPAddr(sCpuPAddr);
+
+
+			OSUnMapPhysToLin(psDevInfo->pvDummyDataPageCpuVAddr,
+								SGX_MMU_PAGE_SIZE,
+                                PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+								psDevInfo->hDummyDataPageOSMemHandle);
+
+			RA_Free (psMMUContext->psDeviceNode->psLocalDevMemArena, sSysPAddr.uiAddr, IMG_FALSE);
+		}
+#endif
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE, "MMU_Finalise"));
+
+
+	ppsMMUContext = (MMU_CONTEXT**)&psMMUContext->psDevInfo->pvMMUContextList;
+	while(*ppsMMUContext)
+	{
+		if(*ppsMMUContext == psMMUContext)
+		{
+
+			*ppsMMUContext = psMMUContext->psNext;
+			break;
+		}
+
+
+		ppsMMUContext = &((*ppsMMUContext)->psNext);
+	}
+
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(MMU_CONTEXT), psMMUContext, IMG_NULL);
+
+}
+
+
+IMG_VOID
+MMU_InsertHeap(MMU_CONTEXT *psMMUContext, MMU_HEAP *psMMUHeap)
+{
+	IMG_UINT32 *pui32PDCpuVAddr = (IMG_UINT32 *) psMMUContext->pvPDCpuVAddr;
+	IMG_UINT32 *pui32KernelPDCpuVAddr = (IMG_UINT32 *) psMMUHeap->psMMUContext->pvPDCpuVAddr;
+	IMG_UINT32 ui32PDEntry;
+#if !defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+	IMG_BOOL bInvalidateDirectoryCache = IMG_FALSE;
+#endif
+
+
+	pui32PDCpuVAddr += psMMUHeap->psDevArena->BaseDevVAddr.uiAddr >> psMMUHeap->ui32PDShift;
+	pui32KernelPDCpuVAddr += psMMUHeap->psDevArena->BaseDevVAddr.uiAddr >> psMMUHeap->ui32PDShift;
+
+
+
+
+	PDUMPCOMMENT("Page directory shared heap range copy");
+#ifdef SUPPORT_SGX_MMU_BYPASS
+	EnableHostAccess(psMMUContext);
+#endif
+
+	for (ui32PDEntry = 0; ui32PDEntry < psMMUHeap->ui32PageTableCount; ui32PDEntry++)
+	{
+#if !defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+		PVR_ASSERT(pui32PDCpuVAddr[ui32PDEntry] == 0);
+#endif
+
+
+		pui32PDCpuVAddr[ui32PDEntry] = pui32KernelPDCpuVAddr[ui32PDEntry];
+		if (pui32PDCpuVAddr[ui32PDEntry])
+		{
+			PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, (IMG_VOID *) &pui32PDCpuVAddr[ui32PDEntry], sizeof(IMG_UINT32), 0, IMG_FALSE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+
+#if !defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+			bInvalidateDirectoryCache = IMG_TRUE;
+#endif
+		}
+	}
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+	DisableHostAccess(psMMUContext);
+#endif
+
+#if !defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+	if (bInvalidateDirectoryCache)
+	{
+
+
+
+
+		MMU_InvalidateDirectoryCache(psMMUContext->psDevInfo);
+	}
+#endif
+}
+
+
+static IMG_VOID
+MMU_UnmapPagesAndFreePTs (MMU_HEAP *psMMUHeap,
+						  IMG_DEV_VIRTADDR sDevVAddr,
+						  IMG_UINT32 ui32PageCount,
+						  IMG_HANDLE hUniqueTag)
+{
+	IMG_DEV_VIRTADDR	sTmpDevVAddr;
+	IMG_UINT32			i;
+	IMG_UINT32			ui32PDIndex;
+	IMG_UINT32			ui32PTIndex;
+	IMG_UINT32			*pui32Tmp;
+	IMG_BOOL			bInvalidateDirectoryCache = IMG_FALSE;
+
+#if !defined (PDUMP)
+	PVR_UNREFERENCED_PARAMETER(hUniqueTag);
+#endif
+
+	sTmpDevVAddr = sDevVAddr;
+
+	for(i=0; i<ui32PageCount; i++)
+	{
+		MMU_PT_INFO **ppsPTInfoList;
+
+
+		ui32PDIndex = sTmpDevVAddr.uiAddr >> psMMUHeap->ui32PDShift;
+
+
+		ppsPTInfoList = &psMMUHeap->psMMUContext->apsPTInfoList[ui32PDIndex];
+
+		{
+
+			ui32PTIndex = (sTmpDevVAddr.uiAddr & psMMUHeap->ui32PTMask) >> psMMUHeap->ui32PTShift;
+
+
+			if (!ppsPTInfoList[0])
+			{
+				PVR_DPF((PVR_DBG_MESSAGE, "MMU_UnmapPagesAndFreePTs: Invalid PT for alloc at VAddr:0x%08lX (VaddrIni:0x%08lX AllocPage:%u) PDIdx:%u PTIdx:%u",sTmpDevVAddr.uiAddr, sDevVAddr.uiAddr,i, ui32PDIndex, ui32PTIndex ));
+
+
+				sTmpDevVAddr.uiAddr += psMMUHeap->ui32DataPageSize;
+
+
+				continue;
+			}
+
+
+			pui32Tmp = (IMG_UINT32*)ppsPTInfoList[0]->PTPageCpuVAddr;
+
+
+			if (!pui32Tmp)
+			{
+				continue;
+			}
+
+			CheckPT(ppsPTInfoList[0]);
+
+
+			if (pui32Tmp[ui32PTIndex] & SGX_MMU_PTE_VALID)
+			{
+				ppsPTInfoList[0]->ui32ValidPTECount--;
+			}
+			else
+			{
+				PVR_DPF((PVR_DBG_MESSAGE, "MMU_UnmapPagesAndFreePTs: Page is already invalid for alloc at VAddr:0x%08lX (VAddrIni:0x%08lX AllocPage:%u) PDIdx:%u PTIdx:%u",sTmpDevVAddr.uiAddr, sDevVAddr.uiAddr,i, ui32PDIndex, ui32PTIndex ));
+			}
+
+
+			PVR_ASSERT((IMG_INT32)ppsPTInfoList[0]->ui32ValidPTECount >= 0);
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+			pui32Tmp[ui32PTIndex] = (psMMUHeap->psMMUContext->psDevInfo->sDummyDataDevPAddr.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+									| SGX_MMU_PTE_VALID;
+#else
+
+			pui32Tmp[ui32PTIndex] = 0;
+#endif
+
+			CheckPT(ppsPTInfoList[0]);
+		}
+
+
+
+		if (ppsPTInfoList[0] && ppsPTInfoList[0]->ui32ValidPTECount == 0)
+		{
+			_DeferredFreePageTable(psMMUHeap, ui32PDIndex - psMMUHeap->ui32PDBaseIndex, IMG_TRUE);
+			bInvalidateDirectoryCache = IMG_TRUE;
+		}
+
+
+		sTmpDevVAddr.uiAddr += psMMUHeap->ui32DataPageSize;
+	}
+
+	if(bInvalidateDirectoryCache)
+	{
+		MMU_InvalidateDirectoryCache(psMMUHeap->psMMUContext->psDevInfo);
+	}
+	else
+	{
+		MMU_InvalidatePageTableCache(psMMUHeap->psMMUContext->psDevInfo);
+	}
+
+#if defined(PDUMP)
+	MMU_PDumpPageTables(psMMUHeap,
+						sDevVAddr,
+						psMMUHeap->ui32DataPageSize * ui32PageCount,
+						IMG_TRUE,
+						hUniqueTag);
+#endif
+}
+
+
+IMG_VOID MMU_FreePageTables(IMG_PVOID pvMMUHeap,
+                            IMG_SIZE_T ui32Start,
+                            IMG_SIZE_T ui32End,
+                            IMG_HANDLE hUniqueTag)
+{
+	MMU_HEAP *pMMUHeap = (MMU_HEAP*)pvMMUHeap;
+	IMG_DEV_VIRTADDR Start;
+
+	Start.uiAddr = ui32Start;
+
+	MMU_UnmapPagesAndFreePTs(pMMUHeap, Start, (ui32End - ui32Start) >> pMMUHeap->ui32PTShift, hUniqueTag);
+}
+
+MMU_HEAP *
+MMU_Create (MMU_CONTEXT *psMMUContext,
+			DEV_ARENA_DESCRIPTOR *psDevArena,
+			RA_ARENA **ppsVMArena)
+{
+	MMU_HEAP *pMMUHeap;
+	IMG_UINT32 ui32ScaleSize;
+
+	PVR_ASSERT (psDevArena != IMG_NULL);
+
+	if (psDevArena == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Create: invalid parameter"));
+		return IMG_NULL;
+	}
+
+	OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+				 sizeof (MMU_HEAP),
+				 (IMG_VOID **)&pMMUHeap, IMG_NULL,
+				 "MMU Heap");
+	if (pMMUHeap == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Create: ERROR call to OSAllocMem failed"));
+		return IMG_NULL;
+	}
+
+	pMMUHeap->psMMUContext = psMMUContext;
+	pMMUHeap->psDevArena = psDevArena;
+
+
+
+
+	switch(pMMUHeap->psDevArena->ui32DataPageSize)
+	{
+		case 0x1000:
+			ui32ScaleSize = 0;
+			pMMUHeap->ui32PDEPageSizeCtrl = SGX_MMU_PDE_PAGE_SIZE_4K;
+			break;
+#if defined(SGX_FEATURE_VARIABLE_MMU_PAGE_SIZE)
+		case 0x4000:
+			ui32ScaleSize = 2;
+			pMMUHeap->ui32PDEPageSizeCtrl = SGX_MMU_PDE_PAGE_SIZE_16K;
+			break;
+		case 0x10000:
+			ui32ScaleSize = 4;
+			pMMUHeap->ui32PDEPageSizeCtrl = SGX_MMU_PDE_PAGE_SIZE_64K;
+			break;
+		case 0x40000:
+			ui32ScaleSize = 6;
+			pMMUHeap->ui32PDEPageSizeCtrl = SGX_MMU_PDE_PAGE_SIZE_256K;
+			break;
+		case 0x100000:
+			ui32ScaleSize = 8;
+			pMMUHeap->ui32PDEPageSizeCtrl = SGX_MMU_PDE_PAGE_SIZE_1M;
+			break;
+		case 0x400000:
+			ui32ScaleSize = 10;
+			pMMUHeap->ui32PDEPageSizeCtrl = SGX_MMU_PDE_PAGE_SIZE_4M;
+			break;
+#endif
+		default:
+			PVR_DPF((PVR_DBG_ERROR, "MMU_Create: invalid data page size"));
+			goto ErrorFreeHeap;
+	}
+
+
+	pMMUHeap->ui32DataPageSize = psDevArena->ui32DataPageSize;
+	pMMUHeap->ui32DataPageBitWidth = SGX_MMU_PAGE_SHIFT + ui32ScaleSize;
+	pMMUHeap->ui32DataPageMask = pMMUHeap->ui32DataPageSize - 1;
+
+	pMMUHeap->ui32PTShift = pMMUHeap->ui32DataPageBitWidth;
+	pMMUHeap->ui32PTBitWidth = SGX_MMU_PT_SHIFT - ui32ScaleSize;
+	pMMUHeap->ui32PTMask = SGX_MMU_PT_MASK & (SGX_MMU_PT_MASK<<ui32ScaleSize);
+	pMMUHeap->ui32PTSize = (1UL<<pMMUHeap->ui32PTBitWidth) * sizeof(IMG_UINT32);
+
+	if(pMMUHeap->ui32PTSize < 4 * sizeof(IMG_UINT32))
+	{
+		pMMUHeap->ui32PTSize = 4 * sizeof(IMG_UINT32);
+	}
+	pMMUHeap->ui32PTECount = pMMUHeap->ui32PTSize >> 2;
+
+
+	pMMUHeap->ui32PDShift = pMMUHeap->ui32PTBitWidth + pMMUHeap->ui32PTShift;
+	pMMUHeap->ui32PDBitWidth = SGX_FEATURE_ADDRESS_SPACE_SIZE - pMMUHeap->ui32PTBitWidth - pMMUHeap->ui32DataPageBitWidth;
+	pMMUHeap->ui32PDMask = SGX_MMU_PD_MASK & (SGX_MMU_PD_MASK>>(32-SGX_FEATURE_ADDRESS_SPACE_SIZE));
+
+
+
+
+
+	if(psDevArena->BaseDevVAddr.uiAddr > (pMMUHeap->ui32DataPageMask | pMMUHeap->ui32PTMask))
+	{
+
+
+
+		PVR_ASSERT ((psDevArena->BaseDevVAddr.uiAddr
+						& (pMMUHeap->ui32DataPageMask
+							| pMMUHeap->ui32PTMask)) == 0);
+	}
+
+
+	pMMUHeap->ui32PTETotal = pMMUHeap->psDevArena->ui32Size >> pMMUHeap->ui32PTShift;
+
+
+	pMMUHeap->ui32PDBaseIndex = (pMMUHeap->psDevArena->BaseDevVAddr.uiAddr & pMMUHeap->ui32PDMask) >> pMMUHeap->ui32PDShift;
+
+
+
+
+	pMMUHeap->ui32PageTableCount = (pMMUHeap->ui32PTETotal + pMMUHeap->ui32PTECount - 1)
+										>> pMMUHeap->ui32PTBitWidth;
+
+
+	pMMUHeap->psVMArena = RA_Create(psDevArena->pszName,
+									psDevArena->BaseDevVAddr.uiAddr,
+									psDevArena->ui32Size,
+									IMG_NULL,
+									pMMUHeap->ui32DataPageSize,
+									IMG_NULL,
+									IMG_NULL,
+									MMU_FreePageTables,
+									pMMUHeap);
+
+	if (pMMUHeap->psVMArena == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Create: ERROR call to RA_Create failed"));
+		goto ErrorFreePagetables;
+	}
+
+#if 0
+
+	if(psDevArena->ui32HeapID == SGX_TILED_HEAP_ID)
+	{
+		IMG_UINT32 ui32RegVal;
+		IMG_UINT32 ui32XTileStride;
+
+
+
+
+
+
+		ui32XTileStride	= 2;
+
+		ui32RegVal = (EUR_CR_BIF_TILE0_MIN_ADDRESS_MASK
+						& ((psDevArena->BaseDevVAddr.uiAddr>>20)
+						<< EUR_CR_BIF_TILE0_MIN_ADDRESS_SHIFT))
+					|(EUR_CR_BIF_TILE0_MAX_ADDRESS_MASK
+						& (((psDevArena->BaseDevVAddr.uiAddr+psDevArena->ui32Size)>>20)
+						<< EUR_CR_BIF_TILE0_MAX_ADDRESS_SHIFT))
+					|(EUR_CR_BIF_TILE0_CFG_MASK
+						& (((ui32XTileStride<<1)|8) << EUR_CR_BIF_TILE0_CFG_SHIFT));
+		PDUMPREG(EUR_CR_BIF_TILE0, ui32RegVal);
+	}
+#endif
+
+
+
+	*ppsVMArena = pMMUHeap->psVMArena;
+
+	return pMMUHeap;
+
+
+ErrorFreePagetables:
+	_DeferredFreePageTables (pMMUHeap);
+
+ErrorFreeHeap:
+	OSFreeMem (PVRSRV_OS_PAGEABLE_HEAP, sizeof(MMU_HEAP), pMMUHeap, IMG_NULL);
+
+
+	return IMG_NULL;
+}
+
+IMG_VOID
+MMU_Delete (MMU_HEAP *pMMUHeap)
+{
+	if (pMMUHeap != IMG_NULL)
+	{
+		PVR_DPF ((PVR_DBG_MESSAGE, "MMU_Delete"));
+
+		if(pMMUHeap->psVMArena)
+		{
+			RA_Delete (pMMUHeap->psVMArena);
+		}
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+		EnableHostAccess(pMMUHeap->psMMUContext);
+#endif
+		_DeferredFreePageTables (pMMUHeap);
+#ifdef SUPPORT_SGX_MMU_BYPASS
+		DisableHostAccess(pMMUHeap->psMMUContext);
+#endif
+
+		OSFreeMem (PVRSRV_OS_PAGEABLE_HEAP, sizeof(MMU_HEAP), pMMUHeap, IMG_NULL);
+
+	}
+}
+
+IMG_BOOL
+MMU_Alloc (MMU_HEAP *pMMUHeap,
+		   IMG_SIZE_T uSize,
+		   IMG_SIZE_T *pActualSize,
+		   IMG_UINT32 uFlags,
+		   IMG_UINT32 uDevVAddrAlignment,
+		   IMG_DEV_VIRTADDR *psDevVAddr)
+{
+	IMG_BOOL bStatus;
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+		"MMU_Alloc: uSize=0x%x, flags=0x%x, align=0x%x",
+		uSize, uFlags, uDevVAddrAlignment));
+
+
+
+	if((uFlags & PVRSRV_MEM_USER_SUPPLIED_DEVVADDR) == 0)
+	{
+		IMG_UINTPTR_T uiAddr;
+
+		bStatus = RA_Alloc (pMMUHeap->psVMArena,
+							uSize,
+							pActualSize,
+							IMG_NULL,
+							0,
+							uDevVAddrAlignment,
+							0,
+							&uiAddr);
+		if(!bStatus)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"MMU_Alloc: RA_Alloc of VMArena failed"));
+			return bStatus;
+		}
+
+		psDevVAddr->uiAddr = IMG_CAST_TO_DEVVADDR_UINT(uiAddr);
+	}
+
+	#ifdef SUPPORT_SGX_MMU_BYPASS
+	EnableHostAccess(pMMUHeap->psMMUContext);
+	#endif
+
+
+	bStatus = _DeferredAllocPagetables(pMMUHeap, *psDevVAddr, uSize);
+
+	#ifdef SUPPORT_SGX_MMU_BYPASS
+	DisableHostAccess(pMMUHeap->psMMUContext);
+	#endif
+
+	if (!bStatus)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"MMU_Alloc: _DeferredAllocPagetables failed"));
+		if((uFlags & PVRSRV_MEM_USER_SUPPLIED_DEVVADDR) == 0)
+		{
+
+			RA_Free (pMMUHeap->psVMArena, psDevVAddr->uiAddr, IMG_FALSE);
+		}
+	}
+
+	return bStatus;
+}
+
+IMG_VOID
+MMU_Free (MMU_HEAP *pMMUHeap, IMG_DEV_VIRTADDR DevVAddr, IMG_UINT32 ui32Size)
+{
+	PVR_ASSERT (pMMUHeap != IMG_NULL);
+
+	if (pMMUHeap == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_Free: invalid parameter"));
+		return;
+	}
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+		"MMU_Free: mmu=%08X, dev_vaddr=%08X", pMMUHeap, DevVAddr.uiAddr));
+
+	if((DevVAddr.uiAddr >= pMMUHeap->psDevArena->BaseDevVAddr.uiAddr) &&
+		(DevVAddr.uiAddr + ui32Size <= pMMUHeap->psDevArena->BaseDevVAddr.uiAddr + pMMUHeap->psDevArena->ui32Size))
+	{
+		RA_Free (pMMUHeap->psVMArena, DevVAddr.uiAddr, IMG_TRUE);
+		return;
+	}
+
+	PVR_DPF((PVR_DBG_ERROR,"MMU_Free: Couldn't find DevVAddr %08X in a DevArena",DevVAddr.uiAddr));
+}
+
+IMG_VOID
+MMU_Enable (MMU_HEAP *pMMUHeap)
+{
+	PVR_UNREFERENCED_PARAMETER(pMMUHeap);
+
+}
+
+IMG_VOID
+MMU_Disable (MMU_HEAP *pMMUHeap)
+{
+	PVR_UNREFERENCED_PARAMETER(pMMUHeap);
+
+}
+
+#if defined(PDUMP)
+static IMG_VOID
+MMU_PDumpPageTables	(MMU_HEAP *pMMUHeap,
+					 IMG_DEV_VIRTADDR DevVAddr,
+					 IMG_SIZE_T uSize,
+					 IMG_BOOL bForUnmap,
+					 IMG_HANDLE hUniqueTag)
+{
+	IMG_UINT32	ui32NumPTEntries;
+	IMG_UINT32	ui32PTIndex;
+	IMG_UINT32	*pui32PTEntry;
+
+	MMU_PT_INFO **ppsPTInfoList;
+	IMG_UINT32 ui32PDIndex;
+	IMG_UINT32 ui32PTDumpCount;
+
+
+	ui32NumPTEntries = (uSize + pMMUHeap->ui32DataPageMask) >> pMMUHeap->ui32PTShift;
+
+
+	ui32PDIndex = DevVAddr.uiAddr >> pMMUHeap->ui32PDShift;
+
+
+	ppsPTInfoList = &pMMUHeap->psMMUContext->apsPTInfoList[ui32PDIndex];
+
+
+	ui32PTIndex = (DevVAddr.uiAddr & pMMUHeap->ui32PTMask) >> pMMUHeap->ui32PTShift;
+
+
+	PDUMPCOMMENT("Page table mods (num entries == %08X) %s", ui32NumPTEntries, bForUnmap ? "(for unmap)" : "");
+
+
+	while(ui32NumPTEntries > 0)
+	{
+		MMU_PT_INFO* psPTInfo = *ppsPTInfoList++;
+
+		if(ui32NumPTEntries <= pMMUHeap->ui32PTECount - ui32PTIndex)
+		{
+			ui32PTDumpCount = ui32NumPTEntries;
+		}
+		else
+		{
+			ui32PTDumpCount = pMMUHeap->ui32PTECount - ui32PTIndex;
+		}
+
+		if (psPTInfo)
+		{
+			pui32PTEntry = (IMG_UINT32*)psPTInfo->PTPageCpuVAddr;
+			PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, (IMG_VOID *) &pui32PTEntry[ui32PTIndex], ui32PTDumpCount * sizeof(IMG_UINT32), 0, IMG_FALSE, PDUMP_PT_UNIQUETAG, hUniqueTag);
+		}
+
+
+		ui32NumPTEntries -= ui32PTDumpCount;
+
+
+		ui32PTIndex = 0;
+	}
+
+	PDUMPCOMMENT("Finished page table mods %s", bForUnmap ? "(for unmap)" : "");
+}
+#endif
+
+
+static IMG_VOID
+MMU_MapPage (MMU_HEAP *pMMUHeap,
+			 IMG_DEV_VIRTADDR DevVAddr,
+			 IMG_DEV_PHYADDR DevPAddr,
+			 IMG_UINT32 ui32MemFlags)
+{
+	IMG_UINT32 ui32Index;
+	IMG_UINT32 *pui32Tmp;
+	IMG_UINT32 ui32MMUFlags = 0;
+	MMU_PT_INFO **ppsPTInfoList;
+
+
+	PVR_ASSERT((DevPAddr.uiAddr & pMMUHeap->ui32DataPageMask) == 0);
+
+
+
+	if(((PVRSRV_MEM_READ|PVRSRV_MEM_WRITE) & ui32MemFlags) == (PVRSRV_MEM_READ|PVRSRV_MEM_WRITE))
+	{
+
+		ui32MMUFlags = 0;
+	}
+	else if(PVRSRV_MEM_READ & ui32MemFlags)
+	{
+
+		ui32MMUFlags |= SGX_MMU_PTE_READONLY;
+	}
+	else if(PVRSRV_MEM_WRITE & ui32MemFlags)
+	{
+
+		ui32MMUFlags |= SGX_MMU_PTE_WRITEONLY;
+	}
+
+
+	if(PVRSRV_MEM_CACHE_CONSISTENT & ui32MemFlags)
+	{
+		ui32MMUFlags |= SGX_MMU_PTE_CACHECONSISTENT;
+	}
+
+#if !defined(FIX_HW_BRN_25503)
+
+	if(PVRSRV_MEM_EDM_PROTECT & ui32MemFlags)
+	{
+		ui32MMUFlags |= SGX_MMU_PTE_EDMPROTECT;
+	}
+#endif
+
+
+
+
+
+	ui32Index = DevVAddr.uiAddr >> pMMUHeap->ui32PDShift;
+
+
+	ppsPTInfoList = &pMMUHeap->psMMUContext->apsPTInfoList[ui32Index];
+
+	CheckPT(ppsPTInfoList[0]);
+
+
+	ui32Index = (DevVAddr.uiAddr & pMMUHeap->ui32PTMask) >> pMMUHeap->ui32PTShift;
+
+
+	pui32Tmp = (IMG_UINT32*)ppsPTInfoList[0]->PTPageCpuVAddr;
+
+#if !defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+	if (pui32Tmp[ui32Index] & SGX_MMU_PTE_VALID)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "MMU_MapPage: Page is already valid for alloc at VAddr:0x%08lX PDIdx:%u PTIdx:%u",
+								DevVAddr.uiAddr,
+								DevVAddr.uiAddr >> pMMUHeap->ui32PDShift,
+								ui32Index ));
+		PVR_DPF((PVR_DBG_ERROR, "MMU_MapPage: Page table entry value: 0x%08lX", pui32Tmp[ui32Index]));
+		PVR_DPF((PVR_DBG_ERROR, "MMU_MapPage: Physical page to map: 0x%08lX", DevPAddr.uiAddr));
+	}
+
+	PVR_ASSERT((pui32Tmp[ui32Index] & SGX_MMU_PTE_VALID) == 0);
+#endif
+
+
+	ppsPTInfoList[0]->ui32ValidPTECount++;
+
+
+	pui32Tmp[ui32Index] = ((DevPAddr.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+						& ((~pMMUHeap->ui32DataPageMask)>>SGX_MMU_PTE_ADDR_ALIGNSHIFT))
+						| SGX_MMU_PTE_VALID
+						| ui32MMUFlags;
+
+	CheckPT(ppsPTInfoList[0]);
+}
+
+
+IMG_VOID
+MMU_MapScatter (MMU_HEAP *pMMUHeap,
+				IMG_DEV_VIRTADDR DevVAddr,
+				IMG_SYS_PHYADDR *psSysAddr,
+				IMG_SIZE_T uSize,
+				IMG_UINT32 ui32MemFlags,
+				IMG_HANDLE hUniqueTag)
+{
+#if defined(PDUMP)
+	IMG_DEV_VIRTADDR MapBaseDevVAddr;
+#endif
+	IMG_UINT32 uCount, i;
+	IMG_DEV_PHYADDR DevPAddr;
+
+	PVR_ASSERT (pMMUHeap != IMG_NULL);
+
+#if defined(PDUMP)
+	MapBaseDevVAddr = DevVAddr;
+#else
+	PVR_UNREFERENCED_PARAMETER(hUniqueTag);
+#endif
+
+	for (i=0, uCount=0; uCount<uSize; i++, uCount+=pMMUHeap->ui32DataPageSize)
+	{
+		IMG_SYS_PHYADDR sSysAddr;
+
+		sSysAddr = psSysAddr[i];
+
+
+
+		PVR_ASSERT((sSysAddr.uiAddr & pMMUHeap->ui32DataPageMask) == 0);
+
+		DevPAddr = SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sSysAddr);
+
+		MMU_MapPage (pMMUHeap, DevVAddr, DevPAddr, ui32MemFlags);
+		DevVAddr.uiAddr += pMMUHeap->ui32DataPageSize;
+
+		PVR_DPF ((PVR_DBG_MESSAGE,
+				 "MMU_MapScatter: devVAddr=%08X, SysAddr=%08X, size=0x%x/0x%x",
+				  DevVAddr.uiAddr, sSysAddr.uiAddr, uCount, uSize));
+	}
+
+#if defined(PDUMP)
+	MMU_PDumpPageTables (pMMUHeap, MapBaseDevVAddr, uSize, IMG_FALSE, hUniqueTag);
+#endif
+}
+
+IMG_VOID
+MMU_MapPages (MMU_HEAP *pMMUHeap,
+			  IMG_DEV_VIRTADDR DevVAddr,
+			  IMG_SYS_PHYADDR SysPAddr,
+			  IMG_SIZE_T uSize,
+			  IMG_UINT32 ui32MemFlags,
+			  IMG_HANDLE hUniqueTag)
+{
+	IMG_DEV_PHYADDR DevPAddr;
+#if defined(PDUMP)
+	IMG_DEV_VIRTADDR MapBaseDevVAddr;
+#endif
+	IMG_UINT32 uCount;
+	IMG_UINT32 ui32VAdvance;
+	IMG_UINT32 ui32PAdvance;
+
+	PVR_ASSERT (pMMUHeap != IMG_NULL);
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+		  "MMU_MapPages: mmu=%08X, devVAddr=%08X, SysPAddr=%08X, size=0x%x",
+		  pMMUHeap, DevVAddr.uiAddr, SysPAddr.uiAddr, uSize));
+
+
+	ui32VAdvance = pMMUHeap->ui32DataPageSize;
+	ui32PAdvance = pMMUHeap->ui32DataPageSize;
+
+#if defined(PDUMP)
+	MapBaseDevVAddr = DevVAddr;
+#else
+	PVR_UNREFERENCED_PARAMETER(hUniqueTag);
+#endif
+
+	DevPAddr = SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, SysPAddr);
+
+
+	PVR_ASSERT((DevPAddr.uiAddr & pMMUHeap->ui32DataPageMask) == 0);
+
+#if defined(FIX_HW_BRN_23281)
+	if(ui32MemFlags & PVRSRV_MEM_INTERLEAVED)
+	{
+		ui32VAdvance *= 2;
+	}
+#endif
+
+
+
+
+	if(ui32MemFlags & PVRSRV_MEM_DUMMY)
+	{
+		ui32PAdvance = 0;
+	}
+
+	for (uCount=0; uCount<uSize; uCount+=ui32VAdvance)
+	{
+		MMU_MapPage (pMMUHeap, DevVAddr, DevPAddr, ui32MemFlags);
+		DevVAddr.uiAddr += ui32VAdvance;
+		DevPAddr.uiAddr += ui32PAdvance;
+	}
+
+#if defined(PDUMP)
+	MMU_PDumpPageTables (pMMUHeap, MapBaseDevVAddr, uSize, IMG_FALSE, hUniqueTag);
+#endif
+}
+
+IMG_VOID
+MMU_MapShadow (MMU_HEAP          *pMMUHeap,
+			   IMG_DEV_VIRTADDR   MapBaseDevVAddr,
+			   IMG_SIZE_T         uByteSize,
+			   IMG_CPU_VIRTADDR   CpuVAddr,
+			   IMG_HANDLE         hOSMemHandle,
+			   IMG_DEV_VIRTADDR  *pDevVAddr,
+			   IMG_UINT32         ui32MemFlags,
+			   IMG_HANDLE         hUniqueTag)
+{
+	IMG_UINT32			i;
+	IMG_UINT32			uOffset = 0;
+	IMG_DEV_VIRTADDR	MapDevVAddr;
+	IMG_UINT32			ui32VAdvance;
+	IMG_UINT32			ui32PAdvance;
+
+#if !defined (PDUMP)
+	PVR_UNREFERENCED_PARAMETER(hUniqueTag);
+#endif
+
+	PVR_DPF ((PVR_DBG_MESSAGE,
+			"MMU_MapShadow: %08X, 0x%x, %08X",
+			MapBaseDevVAddr.uiAddr,
+			uByteSize,
+			CpuVAddr));
+
+
+	ui32VAdvance = pMMUHeap->ui32DataPageSize;
+	ui32PAdvance = pMMUHeap->ui32DataPageSize;
+
+
+	PVR_ASSERT(((IMG_UINT32)CpuVAddr & (SGX_MMU_PAGE_SIZE - 1)) == 0);
+	PVR_ASSERT(((IMG_UINT32)uByteSize & pMMUHeap->ui32DataPageMask) == 0);
+	pDevVAddr->uiAddr = MapBaseDevVAddr.uiAddr;
+
+#if defined(FIX_HW_BRN_23281)
+	if(ui32MemFlags & PVRSRV_MEM_INTERLEAVED)
+	{
+		ui32VAdvance *= 2;
+	}
+#endif
+
+
+
+
+	if(ui32MemFlags & PVRSRV_MEM_DUMMY)
+	{
+		ui32PAdvance = 0;
+	}
+
+
+	MapDevVAddr = MapBaseDevVAddr;
+	for (i=0; i<uByteSize; i+=ui32VAdvance)
+	{
+		IMG_CPU_PHYADDR CpuPAddr;
+		IMG_DEV_PHYADDR DevPAddr;
+
+		if(CpuVAddr)
+		{
+			CpuPAddr = OSMapLinToCPUPhys ((IMG_VOID *)((IMG_UINT32)CpuVAddr + uOffset));
+		}
+		else
+		{
+			CpuPAddr = OSMemHandleToCpuPAddr(hOSMemHandle, uOffset);
+		}
+		DevPAddr = SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE_SGX, CpuPAddr);
+
+
+		PVR_ASSERT((DevPAddr.uiAddr & pMMUHeap->ui32DataPageMask) == 0);
+
+		PVR_DPF ((PVR_DBG_MESSAGE,
+				"0x%x: CpuVAddr=%08X, CpuPAddr=%08X, DevVAddr=%08X, DevPAddr=%08X",
+				uOffset,
+				(IMG_UINTPTR_T)CpuVAddr + uOffset,
+				CpuPAddr.uiAddr,
+				MapDevVAddr.uiAddr,
+				DevPAddr.uiAddr));
+
+		MMU_MapPage (pMMUHeap, MapDevVAddr, DevPAddr, ui32MemFlags);
+
+
+		MapDevVAddr.uiAddr += ui32VAdvance;
+		uOffset += ui32PAdvance;
+	}
+
+#if defined(PDUMP)
+	MMU_PDumpPageTables (pMMUHeap, MapBaseDevVAddr, uByteSize, IMG_FALSE, hUniqueTag);
+#endif
+}
+
+
+IMG_VOID
+MMU_UnmapPages (MMU_HEAP *psMMUHeap,
+				IMG_DEV_VIRTADDR sDevVAddr,
+				IMG_UINT32 ui32PageCount,
+				IMG_HANDLE hUniqueTag)
+{
+	IMG_UINT32			uPageSize = psMMUHeap->ui32DataPageSize;
+	IMG_DEV_VIRTADDR	sTmpDevVAddr;
+	IMG_UINT32			i;
+	IMG_UINT32			ui32PDIndex;
+	IMG_UINT32			ui32PTIndex;
+	IMG_UINT32			*pui32Tmp;
+
+#if !defined (PDUMP)
+	PVR_UNREFERENCED_PARAMETER(hUniqueTag);
+#endif
+
+
+	sTmpDevVAddr = sDevVAddr;
+
+	for(i=0; i<ui32PageCount; i++)
+	{
+		MMU_PT_INFO **ppsPTInfoList;
+
+
+		ui32PDIndex = sTmpDevVAddr.uiAddr >> psMMUHeap->ui32PDShift;
+
+
+		ppsPTInfoList = &psMMUHeap->psMMUContext->apsPTInfoList[ui32PDIndex];
+
+
+		ui32PTIndex = (sTmpDevVAddr.uiAddr & psMMUHeap->ui32PTMask) >> psMMUHeap->ui32PTShift;
+
+
+		if (!ppsPTInfoList[0])
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_UnmapPages: ERROR Invalid PT for alloc at VAddr:0x%08lX (VaddrIni:0x%08lX AllocPage:%u) PDIdx:%u PTIdx:%u",
+									sTmpDevVAddr.uiAddr,
+									sDevVAddr.uiAddr,
+									i,
+									ui32PDIndex,
+									ui32PTIndex));
+
+
+			sTmpDevVAddr.uiAddr += uPageSize;
+
+
+			continue;
+		}
+
+		CheckPT(ppsPTInfoList[0]);
+
+
+		pui32Tmp = (IMG_UINT32*)ppsPTInfoList[0]->PTPageCpuVAddr;
+
+
+		if (pui32Tmp[ui32PTIndex] & SGX_MMU_PTE_VALID)
+		{
+			ppsPTInfoList[0]->ui32ValidPTECount--;
+		}
+		else
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_UnmapPages: Page is already invalid for alloc at VAddr:0x%08lX (VAddrIni:0x%08lX AllocPage:%u) PDIdx:%u PTIdx:%u",
+									sTmpDevVAddr.uiAddr,
+									sDevVAddr.uiAddr,
+									i,
+									ui32PDIndex,
+									ui32PTIndex));
+			PVR_DPF((PVR_DBG_ERROR, "MMU_UnmapPages: Page table entry value: 0x%08lX", pui32Tmp[ui32PTIndex]));
+		}
+
+
+		PVR_ASSERT((IMG_INT32)ppsPTInfoList[0]->ui32ValidPTECount >= 0);
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+		pui32Tmp[ui32PTIndex] = (psMMUHeap->psMMUContext->psDevInfo->sDummyDataDevPAddr.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+								| SGX_MMU_PTE_VALID;
+#else
+
+		pui32Tmp[ui32PTIndex] = 0;
+#endif
+
+		CheckPT(ppsPTInfoList[0]);
+
+
+		sTmpDevVAddr.uiAddr += uPageSize;
+	}
+
+	MMU_InvalidatePageTableCache(psMMUHeap->psMMUContext->psDevInfo);
+
+#if defined(PDUMP)
+	MMU_PDumpPageTables (psMMUHeap, sDevVAddr, uPageSize*ui32PageCount, IMG_TRUE, hUniqueTag);
+#endif
+}
+
+
+IMG_DEV_PHYADDR
+MMU_GetPhysPageAddr(MMU_HEAP *pMMUHeap, IMG_DEV_VIRTADDR sDevVPageAddr)
+{
+	IMG_UINT32 *pui32PageTable;
+	IMG_UINT32 ui32Index;
+	IMG_DEV_PHYADDR sDevPAddr;
+	MMU_PT_INFO **ppsPTInfoList;
+
+
+	ui32Index = sDevVPageAddr.uiAddr >> pMMUHeap->ui32PDShift;
+
+
+	ppsPTInfoList = &pMMUHeap->psMMUContext->apsPTInfoList[ui32Index];
+	if (!ppsPTInfoList[0])
+	{
+		PVR_DPF((PVR_DBG_ERROR,"MMU_GetPhysPageAddr: Not mapped in at 0x%08x", sDevVPageAddr.uiAddr));
+		sDevPAddr.uiAddr = 0;
+		return sDevPAddr;
+	}
+
+
+	ui32Index = (sDevVPageAddr.uiAddr & pMMUHeap->ui32PTMask) >> pMMUHeap->ui32PTShift;
+
+
+	pui32PageTable = (IMG_UINT32*)ppsPTInfoList[0]->PTPageCpuVAddr;
+
+
+	sDevPAddr.uiAddr = pui32PageTable[ui32Index];
+
+
+	sDevPAddr.uiAddr &= ~(pMMUHeap->ui32DataPageMask>>SGX_MMU_PTE_ADDR_ALIGNSHIFT);
+
+
+	sDevPAddr.uiAddr <<= SGX_MMU_PTE_ADDR_ALIGNSHIFT;
+
+	return sDevPAddr;
+}
+
+
+IMG_DEV_PHYADDR MMU_GetPDDevPAddr(MMU_CONTEXT *pMMUContext)
+{
+	return (pMMUContext->sPDDevPAddr);
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR SGXGetPhysPageAddrKM (IMG_HANDLE hDevMemHeap,
+								   IMG_DEV_VIRTADDR sDevVAddr,
+								   IMG_DEV_PHYADDR *pDevPAddr,
+								   IMG_CPU_PHYADDR *pCpuPAddr)
+{
+	MMU_HEAP *pMMUHeap;
+	IMG_DEV_PHYADDR DevPAddr;
+
+
+
+	pMMUHeap = (MMU_HEAP*)BM_GetMMUHeap(hDevMemHeap);
+
+	DevPAddr = MMU_GetPhysPageAddr(pMMUHeap, sDevVAddr);
+	pCpuPAddr->uiAddr = DevPAddr.uiAddr;
+	pDevPAddr->uiAddr = DevPAddr.uiAddr;
+
+	return (pDevPAddr->uiAddr != 0) ? PVRSRV_OK : PVRSRV_ERROR_INVALID_PARAMS;
+}
+
+
+PVRSRV_ERROR SGXGetMMUPDAddrKM(IMG_HANDLE		hDevCookie,
+								IMG_HANDLE 		hDevMemContext,
+								IMG_DEV_PHYADDR *psPDDevPAddr)
+{
+	if (!hDevCookie || !hDevMemContext || !psPDDevPAddr)
+	{
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	*psPDDevPAddr = ((BM_CONTEXT*)hDevMemContext)->psMMUContext->sPDDevPAddr;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR MMU_BIFResetPDAlloc(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	PVRSRV_ERROR eError;
+	SYS_DATA *psSysData;
+	RA_ARENA *psLocalDevMemArena;
+	IMG_HANDLE hOSMemHandle = IMG_NULL;
+	IMG_BYTE *pui8MemBlock = IMG_NULL;
+	IMG_SYS_PHYADDR sMemBlockSysPAddr;
+	IMG_CPU_PHYADDR sMemBlockCpuPAddr;
+
+	SysAcquireData(&psSysData);
+
+	psLocalDevMemArena = psSysData->apsLocalDevMemArena[0];
+
+
+	if(psLocalDevMemArena == IMG_NULL)
+	{
+
+		eError = OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						      3 * SGX_MMU_PAGE_SIZE,
+						      SGX_MMU_PAGE_SIZE,
+						      (IMG_VOID **)&pui8MemBlock,
+						      &hOSMemHandle);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_BIFResetPDAlloc: ERROR call to OSAllocPages failed"));
+			return eError;
+		}
+
+
+		if(pui8MemBlock)
+		{
+			sMemBlockCpuPAddr = OSMapLinToCPUPhys(pui8MemBlock);
+		}
+		else
+		{
+
+			sMemBlockCpuPAddr = OSMemHandleToCpuPAddr(hOSMemHandle, 0);
+		}
+	}
+	else
+	{
+
+
+		if(RA_Alloc(psLocalDevMemArena,
+					3 * SGX_MMU_PAGE_SIZE,
+					IMG_NULL,
+					IMG_NULL,
+					0,
+					SGX_MMU_PAGE_SIZE,
+					0,
+					&(sMemBlockSysPAddr.uiAddr)) != IMG_TRUE)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_BIFResetPDAlloc: ERROR call to RA_Alloc failed"));
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+
+
+		sMemBlockCpuPAddr = SysSysPAddrToCpuPAddr(sMemBlockSysPAddr);
+		pui8MemBlock = OSMapPhysToLin(sMemBlockCpuPAddr,
+									  SGX_MMU_PAGE_SIZE * 3,
+									  PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+									  &hOSMemHandle);
+		if(!pui8MemBlock)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_BIFResetPDAlloc: ERROR failed to map page tables"));
+			return PVRSRV_ERROR_BAD_MAPPING;
+		}
+	}
+
+	psDevInfo->hBIFResetPDOSMemHandle = hOSMemHandle;
+	psDevInfo->sBIFResetPDDevPAddr = SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sMemBlockCpuPAddr);
+	psDevInfo->sBIFResetPTDevPAddr.uiAddr = psDevInfo->sBIFResetPDDevPAddr.uiAddr + SGX_MMU_PAGE_SIZE;
+	psDevInfo->sBIFResetPageDevPAddr.uiAddr = psDevInfo->sBIFResetPTDevPAddr.uiAddr + SGX_MMU_PAGE_SIZE;
+
+
+	psDevInfo->pui32BIFResetPD = (IMG_UINT32 *)pui8MemBlock;
+	psDevInfo->pui32BIFResetPT = (IMG_UINT32 *)(pui8MemBlock + SGX_MMU_PAGE_SIZE);
+
+
+	OSMemSet(psDevInfo->pui32BIFResetPD, 0, SGX_MMU_PAGE_SIZE);
+	OSMemSet(psDevInfo->pui32BIFResetPT, 0, SGX_MMU_PAGE_SIZE);
+
+	OSMemSet(pui8MemBlock + (2 * SGX_MMU_PAGE_SIZE), 0xDB, SGX_MMU_PAGE_SIZE);
+
+	return PVRSRV_OK;
+}
+
+IMG_VOID MMU_BIFResetPDFree(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	SYS_DATA *psSysData;
+	RA_ARENA *psLocalDevMemArena;
+	IMG_SYS_PHYADDR sPDSysPAddr;
+
+	SysAcquireData(&psSysData);
+
+	psLocalDevMemArena = psSysData->apsLocalDevMemArena[0];
+
+
+	if(psLocalDevMemArena == IMG_NULL)
+	{
+		OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+					3 * SGX_MMU_PAGE_SIZE,
+					psDevInfo->pui32BIFResetPD,
+					psDevInfo->hBIFResetPDOSMemHandle);
+	}
+	else
+	{
+		OSUnMapPhysToLin(psDevInfo->pui32BIFResetPD,
+                         3 * SGX_MMU_PAGE_SIZE,
+                         PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+                         psDevInfo->hBIFResetPDOSMemHandle);
+
+		sPDSysPAddr = SysDevPAddrToSysPAddr(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->sBIFResetPDDevPAddr);
+		RA_Free(psLocalDevMemArena, sPDSysPAddr.uiAddr, IMG_FALSE);
+	}
+}
+
+
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+PVRSRV_ERROR WorkaroundBRN22997Alloc(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	PVRSRV_ERROR eError;
+	SYS_DATA *psSysData;
+	RA_ARENA *psLocalDevMemArena;
+	IMG_HANDLE hPTPageOSMemHandle = IMG_NULL;
+	IMG_HANDLE hPDPageOSMemHandle = IMG_NULL;
+	IMG_UINT32 *pui32PD = IMG_NULL;
+	IMG_UINT32 *pui32PT = IMG_NULL;
+	IMG_CPU_PHYADDR sCpuPAddr;
+	IMG_DEV_PHYADDR sPTDevPAddr;
+	IMG_DEV_PHYADDR sPDDevPAddr;
+
+	SysAcquireData(&psSysData);
+
+	psLocalDevMemArena = psSysData->apsLocalDevMemArena[0];
+
+
+	if(psLocalDevMemArena == IMG_NULL)
+	{
+
+		eError = OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						   SGX_MMU_PAGE_SIZE,
+						   SGX_MMU_PAGE_SIZE,
+						   (IMG_VOID **)&pui32PT,
+						   &hPTPageOSMemHandle);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "WorkaroundBRN22997: ERROR call to OSAllocPages failed"));
+			return eError;
+		}
+
+		eError = OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						   SGX_MMU_PAGE_SIZE,
+						   SGX_MMU_PAGE_SIZE,
+						   (IMG_VOID **)&pui32PD,
+						   &hPDPageOSMemHandle);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "WorkaroundBRN22997: ERROR call to OSAllocPages failed"));
+			return eError;
+		}
+
+
+		if(pui32PT)
+        {
+            sCpuPAddr = OSMapLinToCPUPhys(pui32PT);
+        }
+        else
+        {
+
+            sCpuPAddr = OSMemHandleToCpuPAddr(hPTPageOSMemHandle, 0);
+        }
+		sPTDevPAddr = SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+		if(pui32PD)
+        {
+            sCpuPAddr = OSMapLinToCPUPhys(pui32PD);
+        }
+        else
+        {
+
+            sCpuPAddr = OSMemHandleToCpuPAddr(hPDPageOSMemHandle, 0);
+        }
+		sPDDevPAddr = SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+	}
+	else
+	{
+
+
+		if(RA_Alloc(psLocalDevMemArena,
+					SGX_MMU_PAGE_SIZE * 2,
+					IMG_NULL,
+					IMG_NULL,
+					0,
+					SGX_MMU_PAGE_SIZE,
+					0,
+					&(psDevInfo->sBRN22997SysPAddr.uiAddr))!= IMG_TRUE)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "WorkaroundBRN22997: ERROR call to RA_Alloc failed"));
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+
+
+		sCpuPAddr = SysSysPAddrToCpuPAddr(psDevInfo->sBRN22997SysPAddr);
+		pui32PT = OSMapPhysToLin(sCpuPAddr,
+								SGX_MMU_PAGE_SIZE * 2,
+                                PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+								&hPTPageOSMemHandle);
+		if(!pui32PT)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "WorkaroundBRN22997: ERROR failed to map page tables"));
+			return PVRSRV_ERROR_BAD_MAPPING;
+		}
+
+
+		sPTDevPAddr = SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+		pui32PD = pui32PT + 1024;
+		sPDDevPAddr.uiAddr = sPTDevPAddr.uiAddr + 4096;
+	}
+
+	OSMemSet(pui32PD, 0, SGX_MMU_PAGE_SIZE);
+	OSMemSet(pui32PT, 0, SGX_MMU_PAGE_SIZE);
+
+
+	PDUMPMALLOCPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, pui32PD, SGX_MMU_PAGE_SIZE, PDUMP_PD_UNIQUETAG);
+	PDUMPMALLOCPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, pui32PT, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PD, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PT, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PT_UNIQUETAG, PDUMP_PD_UNIQUETAG);
+
+	psDevInfo->hBRN22997PTPageOSMemHandle = hPTPageOSMemHandle;
+	psDevInfo->hBRN22997PDPageOSMemHandle = hPDPageOSMemHandle;
+	psDevInfo->sBRN22997PTDevPAddr = sPTDevPAddr;
+	psDevInfo->sBRN22997PDDevPAddr = sPDDevPAddr;
+	psDevInfo->pui32BRN22997PD = pui32PD;
+	psDevInfo->pui32BRN22997PT = pui32PT;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_VOID WorkaroundBRN22997ReadHostPort(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	IMG_UINT32 *pui32PD = psDevInfo->pui32BRN22997PD;
+	IMG_UINT32 *pui32PT = psDevInfo->pui32BRN22997PT;
+	IMG_UINT32 ui32PDIndex;
+	IMG_UINT32 ui32PTIndex;
+	IMG_DEV_VIRTADDR sDevVAddr;
+	volatile IMG_UINT32 *pui32HostPort;
+	IMG_UINT32 ui32BIFCtrl;
+
+
+
+
+	pui32HostPort = (volatile IMG_UINT32*)(((IMG_UINT8*)psDevInfo->pvHostPortBaseKM) + SYS_SGX_HOSTPORT_BRN23030_OFFSET);
+
+
+	sDevVAddr.uiAddr = SYS_SGX_HOSTPORT_BASE_DEVVADDR + SYS_SGX_HOSTPORT_BRN23030_OFFSET;
+
+	ui32PDIndex = (sDevVAddr.uiAddr & SGX_MMU_PD_MASK) >> (SGX_MMU_PAGE_SHIFT + SGX_MMU_PT_SHIFT);
+	ui32PTIndex = (sDevVAddr.uiAddr & SGX_MMU_PT_MASK) >> SGX_MMU_PAGE_SHIFT;
+
+
+	pui32PD[ui32PDIndex] = (psDevInfo->sBRN22997PTDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+							| SGX_MMU_PDE_VALID;
+
+	pui32PT[ui32PTIndex] = (psDevInfo->sBRN22997PTDevPAddr.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+							| SGX_MMU_PTE_VALID;
+
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PD, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PT, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PT_UNIQUETAG, PDUMP_PD_UNIQUETAG);
+
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_DIR_LIST_BASE0,
+				 psDevInfo->sBRN22997PDDevPAddr.uiAddr);
+	PDUMPPDREG(EUR_CR_BIF_DIR_LIST_BASE0, psDevInfo->sBRN22997PDDevPAddr.uiAddr, PDUMP_PD_UNIQUETAG);
+
+
+	ui32BIFCtrl = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32BIFCtrl | EUR_CR_BIF_CTRL_INVALDC_MASK);
+	PDUMPREG(EUR_CR_BIF_CTRL, ui32BIFCtrl | EUR_CR_BIF_CTRL_INVALDC_MASK);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32BIFCtrl);
+	PDUMPREG(EUR_CR_BIF_CTRL, ui32BIFCtrl);
+
+
+	if (pui32HostPort)
+	{
+
+		IMG_UINT32 ui32Tmp;
+		ui32Tmp = *pui32HostPort;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR,"Host Port not present for BRN22997 workaround"));
+	}
+
+
+
+
+
+
+
+	PDUMPCOMMENT("RDW :SGXMEM:v4:%08lX\r\n", sDevVAddr.uiAddr);
+
+    PDUMPCOMMENT("SAB :SGXMEM:v4:%08lX 4 0 hostport.bin", sDevVAddr.uiAddr);
+
+
+	pui32PD[ui32PDIndex] = 0;
+	pui32PT[ui32PTIndex] = 0;
+
+
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PD, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PT, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PT_UNIQUETAG, PDUMP_PD_UNIQUETAG);
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32BIFCtrl | EUR_CR_BIF_CTRL_INVALDC_MASK);
+	PDUMPREG(EUR_CR_BIF_CTRL, ui32BIFCtrl | EUR_CR_BIF_CTRL_INVALDC_MASK);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32BIFCtrl);
+	PDUMPREG(EUR_CR_BIF_CTRL, ui32BIFCtrl);
+}
+
+
+IMG_VOID WorkaroundBRN22997Free(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	SYS_DATA *psSysData;
+	RA_ARENA *psLocalDevMemArena;
+
+	SysAcquireData(&psSysData);
+
+	psLocalDevMemArena = psSysData->apsLocalDevMemArena[0];
+
+	PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pui32BRN22997PD, SGX_MMU_PAGE_SIZE, PDUMP_PD_UNIQUETAG);
+	PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pui32BRN22997PT, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+
+
+	if(psLocalDevMemArena == IMG_NULL)
+	{
+		if (psDevInfo->pui32BRN22997PD != IMG_NULL)
+		{
+			OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						  SGX_MMU_PAGE_SIZE,
+						  psDevInfo->pui32BRN22997PD,
+						  psDevInfo->hBRN22997PDPageOSMemHandle);
+		}
+
+		if (psDevInfo->pui32BRN22997PT != IMG_NULL)
+		{
+			OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						  SGX_MMU_PAGE_SIZE,
+						  psDevInfo->pui32BRN22997PT,
+						  psDevInfo->hBRN22997PTPageOSMemHandle);
+		}
+	}
+	else
+	{
+		if (psDevInfo->pui32BRN22997PT != IMG_NULL)
+		{
+			OSUnMapPhysToLin(psDevInfo->pui32BRN22997PT,
+				 SGX_MMU_PAGE_SIZE * 2,
+				 PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+				 psDevInfo->hBRN22997PTPageOSMemHandle);
+
+
+			RA_Free(psLocalDevMemArena, psDevInfo->sBRN22997SysPAddr.uiAddr, IMG_FALSE);
+		}
+	}
+}
+#endif
+
+
+#if defined(SUPPORT_EXTERNAL_SYSTEM_CACHE)
+PVRSRV_ERROR MMU_MapExtSystemCacheRegs(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	PVRSRV_ERROR eError;
+	SYS_DATA *psSysData;
+	RA_ARENA *psLocalDevMemArena;
+	IMG_HANDLE hPTPageOSMemHandle = IMG_NULL;
+	IMG_UINT32 *pui32PD;
+	IMG_UINT32 *pui32PT = IMG_NULL;
+	IMG_CPU_PHYADDR sCpuPAddr;
+	IMG_DEV_PHYADDR sPTDevPAddr;
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+	IMG_UINT32 ui32PDIndex;
+	IMG_UINT32 ui32PTIndex;
+
+	psDevInfo = (PVRSRV_SGXDEV_INFO*)psDeviceNode->pvDevice;
+	pui32PD = (IMG_UINT32*)psDeviceNode->sDevMemoryInfo.pBMKernelContext->psMMUContext->pvPDCpuVAddr;
+
+	SysAcquireData(&psSysData);
+
+	psLocalDevMemArena = psSysData->apsLocalDevMemArena[0];
+
+
+	if(psLocalDevMemArena == IMG_NULL)
+	{
+
+		eError = OSAllocPages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						   SGX_MMU_PAGE_SIZE,
+						   SGX_MMU_PAGE_SIZE,
+						   (IMG_VOID **)&pui32PT,
+						   &hPTPageOSMemHandle);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_MapExtSystemCacheRegs: ERROR call to OSAllocPages failed"));
+			return eError;
+		}
+
+
+		if(pui32PT)
+        {
+            sCpuPAddr = OSMapLinToCPUPhys(pui32PT);
+        }
+        else
+        {
+
+            sCpuPAddr = OSMemHandleToCpuPAddr(hPTPageOSMemHandle, 0);
+        }
+		sPTDevPAddr = SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+	}
+	else
+	{
+		IMG_SYS_PHYADDR sSysPAddr;
+
+
+		if(RA_Alloc(psLocalDevMemArena,
+					SGX_MMU_PAGE_SIZE,
+					IMG_NULL,
+					IMG_NULL,
+					0,
+					SGX_MMU_PAGE_SIZE,
+					0,
+					&(sSysPAddr.uiAddr))!= IMG_TRUE)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_MapExtSystemCacheRegs: ERROR call to RA_Alloc failed"));
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+
+
+		sCpuPAddr = SysSysPAddrToCpuPAddr(sSysPAddr);
+		pui32PT = OSMapPhysToLin(sCpuPAddr,
+								SGX_MMU_PAGE_SIZE,
+                                PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+								&hPTPageOSMemHandle);
+		if(!pui32PT)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "MMU_MapExtSystemCacheRegs: ERROR failed to map page tables"));
+			return PVRSRV_ERROR_BAD_MAPPING;
+		}
+
+
+		sPTDevPAddr = SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, sCpuPAddr);
+
+
+		psDevInfo->sExtSystemCacheRegsPTSysPAddr = sSysPAddr;
+	}
+
+	OSMemSet(pui32PT, 0, SGX_MMU_PAGE_SIZE);
+
+	ui32PDIndex = (SGX_EXT_SYSTEM_CACHE_REGS_DEVVADDR_BASE & SGX_MMU_PD_MASK) >> (SGX_MMU_PAGE_SHIFT + SGX_MMU_PT_SHIFT);
+	ui32PTIndex = (SGX_EXT_SYSTEM_CACHE_REGS_DEVVADDR_BASE & SGX_MMU_PT_MASK) >> SGX_MMU_PAGE_SHIFT;
+
+
+	pui32PD[ui32PDIndex] = (sPTDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+							| SGX_MMU_PDE_VALID;
+
+	pui32PT[ui32PTIndex] = (psDevInfo->sExtSysCacheRegsDevPBase.uiAddr>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+							| SGX_MMU_PTE_VALID;
+
+
+	PDUMPMALLOCPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, pui32PT, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PD, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PT, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PT_UNIQUETAG, PDUMP_PD_UNIQUETAG);
+
+
+	psDevInfo->pui32ExtSystemCacheRegsPT = pui32PT;
+	psDevInfo->hExtSystemCacheRegsPTPageOSMemHandle = hPTPageOSMemHandle;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR MMU_UnmapExtSystemCacheRegs(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	SYS_DATA *psSysData;
+	RA_ARENA *psLocalDevMemArena;
+	PVRSRV_SGXDEV_INFO *psDevInfo;
+	IMG_UINT32 ui32PDIndex;
+	IMG_UINT32 *pui32PD;
+
+	psDevInfo = (PVRSRV_SGXDEV_INFO*)psDeviceNode->pvDevice;
+	pui32PD = (IMG_UINT32*)psDeviceNode->sDevMemoryInfo.pBMKernelContext->psMMUContext->pvPDCpuVAddr;
+
+	SysAcquireData(&psSysData);
+
+	psLocalDevMemArena = psSysData->apsLocalDevMemArena[0];
+
+
+	ui32PDIndex = (SGX_EXT_SYSTEM_CACHE_REGS_DEVVADDR_BASE & SGX_MMU_PD_MASK) >> (SGX_MMU_PAGE_SHIFT + SGX_MMU_PT_SHIFT);
+	pui32PD[ui32PDIndex] = 0;
+
+	PDUMPMEM2(PVRSRV_DEVICE_TYPE_SGX, pui32PD, SGX_MMU_PAGE_SIZE, 0, IMG_TRUE, PDUMP_PD_UNIQUETAG, PDUMP_PT_UNIQUETAG);
+	PDUMPFREEPAGETABLE(PVRSRV_DEVICE_TYPE_SGX, psDevInfo->pui32ExtSystemCacheRegsPT, SGX_MMU_PAGE_SIZE, PDUMP_PT_UNIQUETAG);
+
+
+	if(psLocalDevMemArena == IMG_NULL)
+	{
+		if (psDevInfo->pui32ExtSystemCacheRegsPT != IMG_NULL)
+		{
+			OSFreePages(PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_KERNEL_ONLY,
+						  SGX_MMU_PAGE_SIZE,
+						  psDevInfo->pui32ExtSystemCacheRegsPT,
+						  psDevInfo->hExtSystemCacheRegsPTPageOSMemHandle);
+		}
+	}
+	else
+	{
+		if (psDevInfo->pui32ExtSystemCacheRegsPT != IMG_NULL)
+		{
+			OSUnMapPhysToLin(psDevInfo->pui32ExtSystemCacheRegsPT,
+				 SGX_MMU_PAGE_SIZE,
+				 PVRSRV_HAP_WRITECOMBINE|PVRSRV_HAP_KERNEL_ONLY,
+				 psDevInfo->hExtSystemCacheRegsPTPageOSMemHandle);
+
+			RA_Free(psLocalDevMemArena, psDevInfo->sExtSystemCacheRegsPTSysPAddr.uiAddr, IMG_FALSE);
+		}
+	}
+
+	return PVRSRV_OK;
+}
+#endif
+
+
+#if PAGE_TEST
+static IMG_VOID PageTest(IMG_VOID* pMem, IMG_DEV_PHYADDR sDevPAddr)
+{
+	volatile IMG_UINT32 ui32WriteData;
+	volatile IMG_UINT32 ui32ReadData;
+	volatile IMG_UINT32 *pMem32 = (volatile IMG_UINT32 *)pMem;
+	IMG_INT n;
+	IMG_BOOL bOK=IMG_TRUE;
+
+	ui32WriteData = 0xffffffff;
+
+	for (n=0; n<1024; n++)
+	{
+		pMem32[n] = ui32WriteData;
+		ui32ReadData = pMem32[n];
+
+		if (ui32WriteData != ui32ReadData)
+		{
+
+			PVR_DPF ((PVR_DBG_ERROR, "Error - memory page test failed at device phys address 0x%08X", sDevPAddr.uiAddr + (n<<2) ));
+			PVR_DBG_BREAK;
+			bOK = IMG_FALSE;
+		}
+	}
+
+	ui32WriteData = 0;
+
+	for (n=0; n<1024; n++)
+	{
+		pMem32[n] = ui32WriteData;
+		ui32ReadData = pMem32[n];
+
+		if (ui32WriteData != ui32ReadData)
+		{
+
+			PVR_DPF ((PVR_DBG_ERROR, "Error - memory page test failed at device phys address 0x%08X", sDevPAddr.uiAddr + (n<<2) ));
+			PVR_DBG_BREAK;
+			bOK = IMG_FALSE;
+		}
+	}
+
+	if (bOK)
+	{
+		PVR_DPF ((PVR_DBG_VERBOSE, "MMU Page 0x%08X is OK", sDevPAddr.uiAddr));
+	}
+	else
+	{
+		PVR_DPF ((PVR_DBG_VERBOSE, "MMU Page 0x%08X *** FAILED ***", sDevPAddr.uiAddr));
+	}
+}
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.h
new file mode 100644
index 0000000..287d4f3
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/mmu.h
@@ -0,0 +1,135 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _MMU_H_
+#define _MMU_H_
+
+#include "sgxinfokm.h"
+
+PVRSRV_ERROR
+MMU_Initialise (PVRSRV_DEVICE_NODE *psDeviceNode, MMU_CONTEXT **ppsMMUContext, IMG_DEV_PHYADDR *psPDDevPAddr);
+
+IMG_VOID
+MMU_Finalise (MMU_CONTEXT *psMMUContext);
+
+
+IMG_VOID
+MMU_InsertHeap(MMU_CONTEXT *psMMUContext, MMU_HEAP *psMMUHeap);
+
+MMU_HEAP *
+MMU_Create (MMU_CONTEXT *psMMUContext,
+			DEV_ARENA_DESCRIPTOR *psDevArena,
+			RA_ARENA **ppsVMArena);
+
+IMG_VOID
+MMU_Delete (MMU_HEAP *pMMU);
+
+IMG_BOOL
+MMU_Alloc (MMU_HEAP *pMMU,
+           IMG_SIZE_T uSize,
+           IMG_SIZE_T *pActualSize,
+           IMG_UINT32 uFlags,
+		   IMG_UINT32 uDevVAddrAlignment,
+           IMG_DEV_VIRTADDR *pDevVAddr);
+
+IMG_VOID
+MMU_Free (MMU_HEAP *pMMU,
+          IMG_DEV_VIRTADDR DevVAddr,
+		  IMG_UINT32 ui32Size);
+
+IMG_VOID
+MMU_Enable (MMU_HEAP *pMMU);
+
+IMG_VOID
+MMU_Disable (MMU_HEAP *pMMU);
+
+IMG_VOID
+MMU_MapPages (MMU_HEAP *pMMU,
+			  IMG_DEV_VIRTADDR devVAddr,
+			  IMG_SYS_PHYADDR SysPAddr,
+			  IMG_SIZE_T uSize,
+			  IMG_UINT32 ui32MemFlags,
+			  IMG_HANDLE hUniqueTag);
+
+IMG_VOID
+MMU_MapShadow (MMU_HEAP          * pMMU,
+               IMG_DEV_VIRTADDR    MapBaseDevVAddr,
+               IMG_SIZE_T          uSize,
+               IMG_CPU_VIRTADDR    CpuVAddr,
+               IMG_HANDLE          hOSMemHandle,
+               IMG_DEV_VIRTADDR  * pDevVAddr,
+               IMG_UINT32          ui32MemFlags,
+               IMG_HANDLE          hUniqueTag);
+
+IMG_VOID
+MMU_UnmapPages (MMU_HEAP *pMMU,
+             IMG_DEV_VIRTADDR dev_vaddr,
+             IMG_UINT32 ui32PageCount,
+             IMG_HANDLE hUniqueTag);
+
+IMG_VOID
+MMU_MapScatter (MMU_HEAP *pMMU,
+				IMG_DEV_VIRTADDR DevVAddr,
+				IMG_SYS_PHYADDR *psSysAddr,
+				IMG_SIZE_T uSize,
+				IMG_UINT32 ui32MemFlags,
+				IMG_HANDLE hUniqueTag);
+
+
+IMG_DEV_PHYADDR
+MMU_GetPhysPageAddr(MMU_HEAP *pMMUHeap, IMG_DEV_VIRTADDR sDevVPageAddr);
+
+
+IMG_DEV_PHYADDR
+MMU_GetPDDevPAddr(MMU_CONTEXT *pMMUContext);
+
+
+#ifdef SUPPORT_SGX_MMU_BYPASS
+IMG_VOID
+EnableHostAccess (MMU_CONTEXT *psMMUContext);
+
+
+IMG_VOID
+DisableHostAccess (MMU_CONTEXT *psMMUContext);
+#endif
+
+IMG_VOID MMU_InvalidateDirectoryCache(PVRSRV_SGXDEV_INFO *psDevInfo);
+
+PVRSRV_ERROR MMU_BIFResetPDAlloc(PVRSRV_SGXDEV_INFO *psDevInfo);
+
+IMG_VOID MMU_BIFResetPDFree(PVRSRV_SGXDEV_INFO *psDevInfo);
+
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+PVRSRV_ERROR WorkaroundBRN22997Alloc(PVRSRV_SGXDEV_INFO *psDevInfo);
+
+IMG_VOID WorkaroundBRN22997ReadHostPort(PVRSRV_SGXDEV_INFO *psDevInfo);
+
+IMG_VOID WorkaroundBRN22997Free(PVRSRV_SGXDEV_INFO *psDevInfo);
+#endif
+
+#if defined(SUPPORT_EXTERNAL_SYSTEM_CACHE)
+PVRSRV_ERROR MMU_MapExtSystemCacheRegs(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+PVRSRV_ERROR MMU_UnmapExtSystemCacheRegs(PVRSRV_DEVICE_NODE *psDeviceNode);
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/pb.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/pb.c
new file mode 100644
index 0000000..93f356d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/pb.c
@@ -0,0 +1,454 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <stddef.h>
+
+#include "services_headers.h"
+#include "sgxapi_km.h"
+#include "sgxinfo.h"
+#include "sgxinfokm.h"
+#include "pvr_bridge_km.h"
+#include "pdump_km.h"
+#include "sgxutils.h"
+
+#ifndef __linux__
+#pragma message("TODO: Review use of OS_PAGEABLE vs OS_NON_PAGEABLE")
+#endif
+
+#include "lists.h"
+
+static IMPLEMENT_LIST_INSERT(PVRSRV_STUB_PBDESC)
+static IMPLEMENT_LIST_REMOVE(PVRSRV_STUB_PBDESC)
+
+static PRESMAN_ITEM psResItemCreateSharedPB = IMG_NULL;
+static PVRSRV_PER_PROCESS_DATA *psPerProcCreateSharedPB = IMG_NULL;
+
+static PVRSRV_ERROR SGXCleanupSharedPBDescCallback(IMG_PVOID pvParam, IMG_UINT32 ui32Param);
+static PVRSRV_ERROR SGXCleanupSharedPBDescCreateLockCallback(IMG_PVOID pvParam, IMG_UINT32 ui32Param);
+
+IMG_EXPORT PVRSRV_ERROR
+SGXFindSharedPBDescKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+					  IMG_HANDLE 				hDevCookie,
+					  IMG_BOOL 				bLockOnFailure,
+					  IMG_UINT32 				ui32TotalPBSize,
+					  IMG_HANDLE 				*phSharedPBDesc,
+					  PVRSRV_KERNEL_MEM_INFO 	**ppsSharedPBDescKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO 	**ppsHWPBDescKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO 	**ppsBlockKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO 	**ppsHWBlockKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO 	***pppsSharedPBDescSubKernelMemInfos,
+					  IMG_UINT32				*ui32SharedPBDescSubKernelMemInfosCount)
+{
+	PVRSRV_STUB_PBDESC *psStubPBDesc;
+	PVRSRV_KERNEL_MEM_INFO **ppsSharedPBDescSubKernelMemInfos=IMG_NULL;
+	PVRSRV_SGXDEV_INFO *psSGXDevInfo;
+	PVRSRV_ERROR eError;
+
+	psSGXDevInfo = ((PVRSRV_DEVICE_NODE *)hDevCookie)->pvDevice;
+
+	psStubPBDesc = psSGXDevInfo->psStubPBDescListKM;
+	if (psStubPBDesc != IMG_NULL)
+	{
+		IMG_UINT32 i;
+		PRESMAN_ITEM psResItem;
+
+		if(psStubPBDesc->ui32TotalPBSize != ui32TotalPBSize)
+		{
+			PVR_DPF((PVR_DBG_WARNING,
+					"SGXFindSharedPBDescKM: Shared PB requested with different size (0x%x) from existing shared PB (0x%x) - requested size ignored",
+					ui32TotalPBSize, psStubPBDesc->ui32TotalPBSize));
+		}
+
+		if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+					  sizeof(PVRSRV_KERNEL_MEM_INFO *)
+						* psStubPBDesc->ui32SubKernelMemInfosCount,
+					  (IMG_VOID **)&ppsSharedPBDescSubKernelMemInfos,
+					  IMG_NULL,
+					  "Array of Kernel Memory Info") != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "SGXFindSharedPBDescKM: OSAllocMem failed"));
+
+			eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+			goto ExitNotFound;
+		}
+
+		psResItem = ResManRegisterRes(psPerProc->hResManContext,
+									  RESMAN_TYPE_SHARED_PB_DESC,
+									  psStubPBDesc,
+									  0,
+									  &SGXCleanupSharedPBDescCallback);
+
+		if (psResItem == IMG_NULL)
+		{
+			OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					  sizeof(PVRSRV_KERNEL_MEM_INFO *) * psStubPBDesc->ui32SubKernelMemInfosCount,
+					  ppsSharedPBDescSubKernelMemInfos,
+					  0);
+
+
+			PVR_DPF((PVR_DBG_ERROR, "SGXFindSharedPBDescKM: ResManRegisterRes failed"));
+
+			eError = PVRSRV_ERROR_GENERIC;
+			goto ExitNotFound;
+		}
+
+		*ppsSharedPBDescKernelMemInfo = psStubPBDesc->psSharedPBDescKernelMemInfo;
+		*ppsHWPBDescKernelMemInfo = psStubPBDesc->psHWPBDescKernelMemInfo;
+		*ppsBlockKernelMemInfo = psStubPBDesc->psBlockKernelMemInfo;
+		*ppsHWBlockKernelMemInfo = psStubPBDesc->psHWBlockKernelMemInfo;
+
+		*ui32SharedPBDescSubKernelMemInfosCount =
+			psStubPBDesc->ui32SubKernelMemInfosCount;
+
+		*pppsSharedPBDescSubKernelMemInfos = ppsSharedPBDescSubKernelMemInfos;
+
+		for(i=0; i<psStubPBDesc->ui32SubKernelMemInfosCount; i++)
+		{
+			ppsSharedPBDescSubKernelMemInfos[i] =
+				psStubPBDesc->ppsSubKernelMemInfos[i];
+		}
+
+		psStubPBDesc->ui32RefCount++;
+		*phSharedPBDesc = (IMG_HANDLE)psResItem;
+		return PVRSRV_OK;
+	}
+
+	eError = PVRSRV_OK;
+	if (bLockOnFailure)
+	{
+		if (psResItemCreateSharedPB == IMG_NULL)
+		{
+			psResItemCreateSharedPB = ResManRegisterRes(psPerProc->hResManContext,
+				  RESMAN_TYPE_SHARED_PB_DESC_CREATE_LOCK,
+				  psPerProc,
+				  0,
+				  &SGXCleanupSharedPBDescCreateLockCallback);
+
+			if (psResItemCreateSharedPB == IMG_NULL)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "SGXFindSharedPBDescKM: ResManRegisterRes failed"));
+
+				eError = PVRSRV_ERROR_GENERIC;
+				goto ExitNotFound;
+			}
+			PVR_ASSERT(psPerProcCreateSharedPB == IMG_NULL);
+			psPerProcCreateSharedPB = psPerProc;
+		}
+		else
+		{
+			 eError = PVRSRV_ERROR_PROCESSING_BLOCKED;
+		}
+	}
+ExitNotFound:
+	*phSharedPBDesc = IMG_NULL;
+
+	return eError;
+}
+
+
+static PVRSRV_ERROR
+SGXCleanupSharedPBDescKM(PVRSRV_STUB_PBDESC *psStubPBDescIn)
+{
+
+	IMG_UINT32 i;
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE*)psStubPBDescIn->hDevCookie;
+
+
+
+
+	psStubPBDescIn->ui32RefCount--;
+	if (psStubPBDescIn->ui32RefCount == 0)
+	{
+		List_PVRSRV_STUB_PBDESC_Remove(psStubPBDescIn);
+		for(i=0 ; i<psStubPBDescIn->ui32SubKernelMemInfosCount; i++)
+		{
+
+			PVRSRVFreeDeviceMemKM(psStubPBDescIn->hDevCookie,
+								  psStubPBDescIn->ppsSubKernelMemInfos[i]);
+		}
+
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_KERNEL_MEM_INFO *) * psStubPBDescIn->ui32SubKernelMemInfosCount,
+				  psStubPBDescIn->ppsSubKernelMemInfos,
+				  0);
+		psStubPBDescIn->ppsSubKernelMemInfos = IMG_NULL;
+
+		PVRSRVFreeSharedSysMemoryKM(psStubPBDescIn->psBlockKernelMemInfo);
+
+		PVRSRVFreeDeviceMemKM(psStubPBDescIn->hDevCookie, psStubPBDescIn->psHWBlockKernelMemInfo);
+
+		PVRSRVFreeDeviceMemKM(psStubPBDescIn->hDevCookie, psStubPBDescIn->psHWPBDescKernelMemInfo);
+
+		PVRSRVFreeSharedSysMemoryKM(psStubPBDescIn->psSharedPBDescKernelMemInfo);
+
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_STUB_PBDESC),
+				  psStubPBDescIn,
+				  0);
+
+
+
+		SGXCleanupRequest(psDeviceNode,
+						  IMG_NULL,
+						  PVRSRV_CLEANUPCMD_PB);
+	}
+	return PVRSRV_OK;
+
+}
+
+static PVRSRV_ERROR SGXCleanupSharedPBDescCallback(IMG_PVOID pvParam, IMG_UINT32 ui32Param)
+{
+	PVRSRV_STUB_PBDESC *psStubPBDesc = (PVRSRV_STUB_PBDESC *)pvParam;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	return SGXCleanupSharedPBDescKM(psStubPBDesc);
+}
+
+static PVRSRV_ERROR SGXCleanupSharedPBDescCreateLockCallback(IMG_PVOID pvParam, IMG_UINT32 ui32Param)
+{
+#ifdef DEBUG
+	PVRSRV_PER_PROCESS_DATA *psPerProc = (PVRSRV_PER_PROCESS_DATA *)pvParam;
+	PVR_ASSERT(psPerProc == psPerProcCreateSharedPB);
+#else
+	PVR_UNREFERENCED_PARAMETER(pvParam);
+#endif
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	psPerProcCreateSharedPB = IMG_NULL;
+	psResItemCreateSharedPB = IMG_NULL;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_EXPORT PVRSRV_ERROR
+SGXUnrefSharedPBDescKM(IMG_HANDLE hSharedPBDesc)
+{
+	PVR_ASSERT(hSharedPBDesc != IMG_NULL);
+
+	return ResManFreeResByPtr(hSharedPBDesc);
+}
+
+
+IMG_EXPORT PVRSRV_ERROR
+SGXAddSharedPBDescKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+					 IMG_HANDLE					hDevCookie,
+					 PVRSRV_KERNEL_MEM_INFO		*psSharedPBDescKernelMemInfo,
+					 PVRSRV_KERNEL_MEM_INFO		*psHWPBDescKernelMemInfo,
+					 PVRSRV_KERNEL_MEM_INFO		*psBlockKernelMemInfo,
+					 PVRSRV_KERNEL_MEM_INFO		*psHWBlockKernelMemInfo,
+					 IMG_UINT32					ui32TotalPBSize,
+					 IMG_HANDLE					*phSharedPBDesc,
+					 PVRSRV_KERNEL_MEM_INFO		**ppsSharedPBDescSubKernelMemInfos,
+					 IMG_UINT32					ui32SharedPBDescSubKernelMemInfosCount)
+{
+	PVRSRV_STUB_PBDESC *psStubPBDesc=IMG_NULL;
+	PVRSRV_ERROR eRet = PVRSRV_ERROR_GENERIC;
+	IMG_UINT32 i;
+	PVRSRV_SGXDEV_INFO *psSGXDevInfo;
+	PRESMAN_ITEM psResItem;
+
+
+	if (psPerProcCreateSharedPB != psPerProc)
+	{
+		goto NoAdd;
+	}
+	else
+	{
+		PVR_ASSERT(psResItemCreateSharedPB != IMG_NULL);
+
+		ResManFreeResByPtr(psResItemCreateSharedPB);
+
+		PVR_ASSERT(psResItemCreateSharedPB == IMG_NULL);
+		PVR_ASSERT(psPerProcCreateSharedPB == IMG_NULL);
+	}
+
+	psSGXDevInfo = (PVRSRV_SGXDEV_INFO *)((PVRSRV_DEVICE_NODE *)hDevCookie)->pvDevice;
+
+	psStubPBDesc = psSGXDevInfo->psStubPBDescListKM;
+	if (psStubPBDesc != IMG_NULL)
+	{
+		if(psStubPBDesc->ui32TotalPBSize != ui32TotalPBSize)
+		{
+			PVR_DPF((PVR_DBG_WARNING,
+					"SGXAddSharedPBDescKM: Shared PB requested with different size (0x%x) from existing shared PB (0x%x) - requested size ignored",
+					ui32TotalPBSize, psStubPBDesc->ui32TotalPBSize));
+
+		}
+
+
+		psResItem = ResManRegisterRes(psPerProc->hResManContext,
+									  RESMAN_TYPE_SHARED_PB_DESC,
+									  psStubPBDesc,
+									  0,
+									  &SGXCleanupSharedPBDescCallback);
+		if (psResItem == IMG_NULL)
+		{
+			PVR_DPF((PVR_DBG_ERROR,
+				"SGXAddSharedPBDescKM: "
+				"Failed to register existing shared "
+				"PBDesc with the resource manager"));
+			goto NoAddKeepPB;
+		}
+
+
+		psStubPBDesc->ui32RefCount++;
+
+		*phSharedPBDesc = (IMG_HANDLE)psResItem;
+		eRet = PVRSRV_OK;
+		goto NoAddKeepPB;
+	}
+
+	if(OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_STUB_PBDESC),
+				  (IMG_VOID **)&psStubPBDesc,
+				  0,
+				  "Stub Parameter Buffer Description") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXAddSharedPBDescKM: Failed to alloc "
+					"StubPBDesc"));
+		eRet = PVRSRV_ERROR_OUT_OF_MEMORY;
+		goto NoAdd;
+	}
+
+
+	psStubPBDesc->ppsSubKernelMemInfos = IMG_NULL;
+
+	if(OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_KERNEL_MEM_INFO *)
+				  * ui32SharedPBDescSubKernelMemInfosCount,
+				  (IMG_VOID **)&psStubPBDesc->ppsSubKernelMemInfos,
+				  0,
+				  "Array of Kernel Memory Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXAddSharedPBDescKM: "
+				 "Failed to alloc "
+				 "StubPBDesc->ppsSubKernelMemInfos"));
+		eRet = PVRSRV_ERROR_OUT_OF_MEMORY;
+		goto NoAdd;
+	}
+
+	if(PVRSRVDissociateMemFromResmanKM(psSharedPBDescKernelMemInfo)
+	   != PVRSRV_OK)
+	{
+		goto NoAdd;
+	}
+
+	if(PVRSRVDissociateMemFromResmanKM(psHWPBDescKernelMemInfo)
+	   != PVRSRV_OK)
+	{
+		goto NoAdd;
+	}
+
+	if(PVRSRVDissociateMemFromResmanKM(psBlockKernelMemInfo)
+	   != PVRSRV_OK)
+	{
+		goto NoAdd;
+	}
+
+	if(PVRSRVDissociateMemFromResmanKM(psHWBlockKernelMemInfo)
+	   != PVRSRV_OK)
+	{
+		goto NoAdd;
+	}
+
+	psStubPBDesc->ui32RefCount = 1;
+	psStubPBDesc->ui32TotalPBSize = ui32TotalPBSize;
+	psStubPBDesc->psSharedPBDescKernelMemInfo = psSharedPBDescKernelMemInfo;
+	psStubPBDesc->psHWPBDescKernelMemInfo = psHWPBDescKernelMemInfo;
+	psStubPBDesc->psBlockKernelMemInfo = psBlockKernelMemInfo;
+	psStubPBDesc->psHWBlockKernelMemInfo = psHWBlockKernelMemInfo;
+
+	psStubPBDesc->ui32SubKernelMemInfosCount =
+		ui32SharedPBDescSubKernelMemInfosCount;
+	for(i=0; i<ui32SharedPBDescSubKernelMemInfosCount; i++)
+	{
+		psStubPBDesc->ppsSubKernelMemInfos[i] = ppsSharedPBDescSubKernelMemInfos[i];
+		if(PVRSRVDissociateMemFromResmanKM(ppsSharedPBDescSubKernelMemInfos[i])
+		   != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "SGXAddSharedPBDescKM: "
+					 "Failed to dissociate shared PBDesc "
+					 "from process"));
+			goto NoAdd;
+		}
+	}
+
+	psResItem = ResManRegisterRes(psPerProc->hResManContext,
+								  RESMAN_TYPE_SHARED_PB_DESC,
+								  psStubPBDesc,
+								  0,
+								  &SGXCleanupSharedPBDescCallback);
+	if (psResItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXAddSharedPBDescKM: "
+					 "Failed to register shared PBDesc "
+					 " with the resource manager"));
+		goto NoAdd;
+	}
+	psStubPBDesc->hDevCookie = hDevCookie;
+
+
+	List_PVRSRV_STUB_PBDESC_Insert(&(psSGXDevInfo->psStubPBDescListKM),
+									psStubPBDesc);
+
+	*phSharedPBDesc = (IMG_HANDLE)psResItem;
+
+	return PVRSRV_OK;
+
+NoAdd:
+	if(psStubPBDesc)
+	{
+		if(psStubPBDesc->ppsSubKernelMemInfos)
+		{
+			OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+					  sizeof(PVRSRV_KERNEL_MEM_INFO *) * ui32SharedPBDescSubKernelMemInfosCount,
+					  psStubPBDesc->ppsSubKernelMemInfos,
+					  0);
+			psStubPBDesc->ppsSubKernelMemInfos = IMG_NULL;
+		}
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				  sizeof(PVRSRV_STUB_PBDESC),
+				  psStubPBDesc,
+				  0);
+
+	}
+
+NoAddKeepPB:
+	for (i = 0; i < ui32SharedPBDescSubKernelMemInfosCount; i++)
+	{
+		PVRSRVFreeDeviceMemKM(hDevCookie, ppsSharedPBDescSubKernelMemInfos[i]);
+	}
+
+	PVRSRVFreeSharedSysMemoryKM(psSharedPBDescKernelMemInfo);
+	PVRSRVFreeDeviceMemKM(hDevCookie, psHWPBDescKernelMemInfo);
+
+	PVRSRVFreeSharedSysMemoryKM(psBlockKernelMemInfo);
+	PVRSRVFreeDeviceMemKM(hDevCookie, psHWBlockKernelMemInfo);
+
+	return eRet;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgx_bridge_km.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgx_bridge_km.h
new file mode 100644
index 0000000..bfc726f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgx_bridge_km.h
@@ -0,0 +1,143 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__SGX_BRIDGE_KM_H__)
+#define __SGX_BRIDGE_KM_H__
+
+#include "sgxapi_km.h"
+#include "sgxinfo.h"
+#include "sgxinfokm.h"
+#include "sgx_bridge.h"
+#include "pvr_bridge.h"
+#include "perproc.h"
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+IMG_IMPORT
+PVRSRV_ERROR SGXSubmitTransferKM(IMG_HANDLE hDevHandle, PVRSRV_TRANSFER_SGX_KICK *psKick);
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+IMG_IMPORT
+PVRSRV_ERROR SGXSubmit2DKM(IMG_HANDLE hDevHandle, PVRSRV_2D_SGX_KICK *psKick);
+#endif
+
+IMG_IMPORT
+PVRSRV_ERROR SGXDoKickKM(IMG_HANDLE hDevHandle,
+						 SGX_CCB_KICK *psCCBKick);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXGetPhysPageAddrKM(IMG_HANDLE hDevMemHeap,
+								  IMG_DEV_VIRTADDR sDevVAddr,
+								  IMG_DEV_PHYADDR *pDevPAddr,
+								  IMG_CPU_PHYADDR *pCpuPAddr);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV SGXGetMMUPDAddrKM(IMG_HANDLE		hDevCookie,
+											IMG_HANDLE		hDevMemContext,
+											IMG_DEV_PHYADDR	*psPDDevPAddr);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXGetClientInfoKM(IMG_HANDLE				hDevCookie,
+								SGX_CLIENT_INFO*	psClientInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXGetMiscInfoKM(PVRSRV_SGXDEV_INFO	*psDevInfo,
+							  SGX_MISC_INFO			*psMiscInfo,
+							  PVRSRV_DEVICE_NODE 	*psDeviceNode,
+							  IMG_HANDLE 			 hDevMemContext);
+
+#if defined(SUPPORT_SGX_HWPERF)
+IMG_IMPORT
+PVRSRV_ERROR SGXReadDiffCountersKM(IMG_HANDLE				hDevHandle,
+								   IMG_UINT32				ui32Reg,
+								   IMG_UINT32				*pui32Old,
+								   IMG_BOOL					bNew,
+								   IMG_UINT32				ui32New,
+								   IMG_UINT32				ui32NewReset,
+								   IMG_UINT32				ui32CountersReg,
+								   IMG_UINT32				ui32Reg2,
+								   IMG_BOOL					*pbActive,
+								   PVRSRV_SGXDEV_DIFF_INFO	*psDiffs);
+IMG_IMPORT
+PVRSRV_ERROR SGXReadHWPerfCBKM(IMG_HANDLE					hDevHandle,
+							   IMG_UINT32					ui32ArraySize,
+							   PVRSRV_SGX_HWPERF_CB_ENTRY	*psHWPerfCBData,
+							   IMG_UINT32					*pui32DataCount,
+							   IMG_UINT32					*pui32ClockSpeed,
+							   IMG_UINT32					*pui32HostTimeStamp);
+#endif
+
+IMG_IMPORT
+PVRSRV_ERROR SGX2DQueryBlitsCompleteKM(PVRSRV_SGXDEV_INFO		*psDevInfo,
+									   PVRSRV_KERNEL_SYNC_INFO	*psSyncInfo,
+									   IMG_BOOL bWaitForComplete);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXGetInfoForSrvinitKM(IMG_HANDLE hDevHandle,
+									SGX_BRIDGE_INFO_FOR_SRVINIT *psInitInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR DevInitSGXPart2KM(PVRSRV_PER_PROCESS_DATA *psPerProc,
+							   IMG_HANDLE hDevHandle,
+							   SGX_BRIDGE_INIT_INFO *psInitInfo);
+
+IMG_IMPORT PVRSRV_ERROR
+SGXFindSharedPBDescKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+					  IMG_HANDLE				hDevCookie,
+					  IMG_BOOL				bLockOnFailure,
+					  IMG_UINT32				ui32TotalPBSize,
+					  IMG_HANDLE				*phSharedPBDesc,
+					  PVRSRV_KERNEL_MEM_INFO	**ppsSharedPBDescKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO	**ppsHWPBDescKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO	**ppsBlockKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO	**ppsHWBlockKernelMemInfo,
+					  PVRSRV_KERNEL_MEM_INFO	***pppsSharedPBDescSubKernelMemInfos,
+					  IMG_UINT32				*ui32SharedPBDescSubKernelMemInfosCount);
+
+IMG_IMPORT PVRSRV_ERROR
+SGXUnrefSharedPBDescKM(IMG_HANDLE hSharedPBDesc);
+
+IMG_IMPORT PVRSRV_ERROR
+SGXAddSharedPBDescKM(PVRSRV_PER_PROCESS_DATA	*psPerProc,
+					 IMG_HANDLE 				hDevCookie,
+					 PVRSRV_KERNEL_MEM_INFO		*psSharedPBDescKernelMemInfo,
+					 PVRSRV_KERNEL_MEM_INFO		*psHWPBDescKernelMemInfo,
+					 PVRSRV_KERNEL_MEM_INFO		*psBlockKernelMemInfo,
+					 PVRSRV_KERNEL_MEM_INFO		*psHWBlockKernelMemInfo,
+					 IMG_UINT32					ui32TotalPBSize,
+					 IMG_HANDLE					*phSharedPBDesc,
+					 PVRSRV_KERNEL_MEM_INFO		**psSharedPBDescSubKernelMemInfos,
+					 IMG_UINT32					ui32SharedPBDescSubKernelMemInfosCount);
+
+
+IMG_IMPORT PVRSRV_ERROR
+SGXGetInternalDevInfoKM(IMG_HANDLE hDevCookie,
+						SGX_INTERNAL_DEVINFO *psSGXInternalDevInfo);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxconfig.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxconfig.h
new file mode 100644
index 0000000..58415d8
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxconfig.h
@@ -0,0 +1,161 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __SGXCONFIG_H__
+#define __SGXCONFIG_H__
+
+#include "sgxdefs.h"
+
+#define DEV_DEVICE_TYPE			PVRSRV_DEVICE_TYPE_SGX
+#define DEV_DEVICE_CLASS		PVRSRV_DEVICE_CLASS_3D
+
+#define DEV_MAJOR_VERSION		1
+#define DEV_MINOR_VERSION		0
+
+#if SGX_FEATURE_ADDRESS_SPACE_SIZE == 32
+#if 0 /* ORIGIAL_SGX_MAP */
+	#if defined(SGX_FEATURE_2D_HARDWARE)
+	#define SGX_2D_HEAP_BASE					 0x00100000
+	#define SGX_2D_HEAP_SIZE					(0x08000000-0x00100000-0x00001000)
+	#else
+		#if defined(FIX_HW_BRN_26915)
+		#define SGX_CGBUFFER_HEAP_BASE					 0x00100000
+		#define SGX_CGBUFFER_HEAP_SIZE					(0x08000000-0x00100000-0x00001000)
+		#endif
+	#endif
+
+	#if defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+	#define SGX_GENERAL_MAPPING_HEAP_BASE		 0x08000000
+	#define SGX_GENERAL_MAPPING_HEAP_SIZE		(0x08000000-0x00001000)
+	#endif
+
+	#define SGX_GENERAL_HEAP_BASE				 0x10000000
+	#define SGX_GENERAL_HEAP_SIZE				(0xC8000000-0x00001000)
+#else /* EMGD Mapping */
+	/* Leave 0 - 0x10000000 (256MB) untouched */
+	#define SGX_2D_HEAP_BASE					 0x10000000
+	#define SGX_2D_HEAP_SIZE					(0x08000000-0x00001000)
+
+#if defined(SUPPORT_SGX_VIDEO_HEAP)
+
+	#if defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+	#define SGX_GENERAL_MAPPING_HEAP_BASE		 0x18000000
+	#define SGX_GENERAL_MAPPING_HEAP_SIZE		(0x30000000-0x00001000)
+	#endif
+
+	#define SGX_VIDEO_HEAP_BASE					 0x48000000
+	#define SGX_VIDEO_HEAP_SIZE					(0x18000000-0x00001000)
+
+	#define SGX_GENERAL_HEAP_BASE				 0x60000000
+	#define SGX_GENERAL_HEAP_SIZE				(0x78000000-0x00001000)
+
+#else
+
+	#if defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+	#define SGX_GENERAL_MAPPING_HEAP_BASE		 0x18000000
+	#define SGX_GENERAL_MAPPING_HEAP_SIZE		(0x08000000-0x00001000)
+	#endif
+
+	#define SGX_GENERAL_HEAP_BASE				 0x20000000
+	#define SGX_GENERAL_HEAP_SIZE				(0xB8000000-0x00001000)
+
+#endif
+
+#endif
+	#define SGX_3DPARAMETERS_HEAP_BASE			 0xD8000000
+	#define SGX_3DPARAMETERS_HEAP_SIZE			(0x10000000-0x00001000)
+
+	#define SGX_TADATA_HEAP_BASE				 0xE8000000
+	#define SGX_TADATA_HEAP_SIZE				(0x0D000000-0x00001000)
+
+	#define SGX_SYNCINFO_HEAP_BASE				 0xF5000000
+	#define SGX_SYNCINFO_HEAP_SIZE				(0x01000000-0x00001000)
+
+	#define SGX_PDSPIXEL_CODEDATA_HEAP_BASE		 0xF6000000
+	#define SGX_PDSPIXEL_CODEDATA_HEAP_SIZE		(0x02000000-0x00001000)
+
+	#define SGX_KERNEL_CODE_HEAP_BASE			 0xF8000000
+	#define SGX_KERNEL_CODE_HEAP_SIZE			(0x00080000-0x00001000)
+
+	#define SGX_PDSVERTEX_CODEDATA_HEAP_BASE	 0xF8400000
+	#define SGX_PDSVERTEX_CODEDATA_HEAP_SIZE	(0x01C00000-0x00001000)
+
+	#define SGX_KERNEL_DATA_HEAP_BASE		 	 0xFA000000
+	#define SGX_KERNEL_DATA_HEAP_SIZE			(0x05000000-0x00001000)
+
+	#define SGX_PIXELSHADER_HEAP_BASE			 0xFF000000
+	#define SGX_PIXELSHADER_HEAP_SIZE			(0x00500000-0x00001000)
+
+	#define SGX_VERTEXSHADER_HEAP_BASE			 0xFF800000
+	#define SGX_VERTEXSHADER_HEAP_SIZE			(0x00200000-0x00001000)
+
+
+	#define SGX_CORE_IDENTIFIED
+#endif
+
+#if SGX_FEATURE_ADDRESS_SPACE_SIZE == 28
+	#if defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+	#define SGX_GENERAL_MAPPING_HEAP_BASE		 0x00001000
+	#define SGX_GENERAL_MAPPING_HEAP_SIZE		(0x01800000-0x00001000-0x00001000)
+	#endif
+
+	#define SGX_GENERAL_HEAP_BASE				 0x01800000
+	#define SGX_GENERAL_HEAP_SIZE				(0x07000000-0x00001000)
+
+	#define SGX_3DPARAMETERS_HEAP_BASE			 0x08800000
+	#define SGX_3DPARAMETERS_HEAP_SIZE			(0x04000000-0x00001000)
+
+	#define SGX_TADATA_HEAP_BASE				 0x0C800000
+	#define SGX_TADATA_HEAP_SIZE				(0x01000000-0x00001000)
+
+	#define SGX_SYNCINFO_HEAP_BASE				 0x0D800000
+	#define SGX_SYNCINFO_HEAP_SIZE				(0x00400000-0x00001000)
+
+	#define SGX_PDSPIXEL_CODEDATA_HEAP_BASE		 0x0DC00000
+	#define SGX_PDSPIXEL_CODEDATA_HEAP_SIZE		(0x00800000-0x00001000)
+
+	#define SGX_KERNEL_CODE_HEAP_BASE			 0x0E400000
+	#define SGX_KERNEL_CODE_HEAP_SIZE			(0x00080000-0x00001000)
+
+	#define SGX_PDSVERTEX_CODEDATA_HEAP_BASE	 0x0E800000
+	#define SGX_PDSVERTEX_CODEDATA_HEAP_SIZE	(0x00800000-0x00001000)
+
+	#define SGX_KERNEL_DATA_HEAP_BASE			 0x0F000000
+	#define SGX_KERNEL_DATA_HEAP_SIZE			(0x00400000-0x00001000)
+
+	#define SGX_PIXELSHADER_HEAP_BASE			 0x0F400000
+	#define SGX_PIXELSHADER_HEAP_SIZE			(0x00500000-0x00001000)
+
+	#define SGX_VERTEXSHADER_HEAP_BASE			 0x0FC00000
+	#define SGX_VERTEXSHADER_HEAP_SIZE			(0x00200000-0x00001000)
+
+
+	#define SGX_CORE_IDENTIFIED
+
+#endif
+
+#if !defined(SGX_CORE_IDENTIFIED)
+	#error "sgxconfig.h: ERROR: unspecified SGX Core version"
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinfokm.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinfokm.h
new file mode 100644
index 0000000..b4fd589
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinfokm.h
@@ -0,0 +1,348 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __SGXINFOKM_H__
+#define __SGXINFOKM_H__
+
+#include "sgxdefs.h"
+#include "device.h"
+#include "power.h"
+#include "sysconfig.h"
+#include "sgxscript.h"
+#include "sgxinfo.h"
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#define		SGX_HOSTPORT_PRESENT			0x00000001UL
+
+
+typedef struct _PVRSRV_STUB_PBDESC_ PVRSRV_STUB_PBDESC;
+
+
+typedef struct _PVRSRV_SGX_CCB_INFO_ *PPVRSRV_SGX_CCB_INFO;
+
+typedef struct _PVRSRV_SGXDEV_INFO_
+{
+	PVRSRV_DEVICE_TYPE		eDeviceType;
+	PVRSRV_DEVICE_CLASS		eDeviceClass;
+
+	IMG_UINT8				ui8VersionMajor;
+	IMG_UINT8				ui8VersionMinor;
+	IMG_UINT32				ui32CoreConfig;
+	IMG_UINT32				ui32CoreFlags;
+
+
+	IMG_PVOID				pvRegsBaseKM;
+
+#if defined(SGX_FEATURE_HOST_PORT)
+
+	IMG_PVOID				pvHostPortBaseKM;
+
+	IMG_UINT32				ui32HPSize;
+
+	IMG_SYS_PHYADDR			sHPSysPAddr;
+#endif
+
+
+	IMG_HANDLE				hRegMapping;
+
+
+	IMG_SYS_PHYADDR			sRegsPhysBase;
+
+	IMG_UINT32				ui32RegSize;
+
+#if defined(SUPPORT_EXTERNAL_SYSTEM_CACHE)
+
+	IMG_UINT32				ui32ExtSysCacheRegsSize;
+
+	IMG_DEV_PHYADDR			sExtSysCacheRegsDevPBase;
+
+	IMG_UINT32				*pui32ExtSystemCacheRegsPT;
+
+	IMG_HANDLE				hExtSystemCacheRegsPTPageOSMemHandle;
+
+	IMG_SYS_PHYADDR			sExtSystemCacheRegsPTSysPAddr;
+#endif
+
+
+	IMG_UINT32				ui32CoreClockSpeed;
+	IMG_UINT32				ui32uKernelTimerClock;
+
+	PVRSRV_STUB_PBDESC		*psStubPBDescListKM;
+
+
+
+	IMG_DEV_PHYADDR			sKernelPDDevPAddr;
+
+	IMG_VOID				*pvDeviceMemoryHeap;
+	PPVRSRV_KERNEL_MEM_INFO	psKernelCCBMemInfo;
+	PVRSRV_SGX_KERNEL_CCB	*psKernelCCB;
+	PPVRSRV_SGX_CCB_INFO	psKernelCCBInfo;
+	PPVRSRV_KERNEL_MEM_INFO	psKernelCCBCtlMemInfo;
+	PVRSRV_SGX_CCB_CTL		*psKernelCCBCtl;
+	PPVRSRV_KERNEL_MEM_INFO psKernelCCBEventKickerMemInfo;
+	IMG_UINT32				*pui32KernelCCBEventKicker;
+#if defined(PDUMP)
+	IMG_UINT32				ui32KernelCCBEventKickerDumpVal;
+#endif
+	PVRSRV_KERNEL_MEM_INFO	*psKernelSGXMiscMemInfo;
+	IMG_UINT32				aui32HostKickAddr[SGXMKIF_CMD_MAX];
+#if defined(SGX_SUPPORT_HWPROFILING)
+	PPVRSRV_KERNEL_MEM_INFO psKernelHWProfilingMemInfo;
+#endif
+	IMG_UINT32				ui32KickTACounter;
+	IMG_UINT32				ui32KickTARenderCounter;
+#if defined(SUPPORT_SGX_HWPERF)
+	PPVRSRV_KERNEL_MEM_INFO		psKernelHWPerfCBMemInfo;
+	IMG_UINT32					ui32HWGroupRequested;
+	IMG_UINT32					ui32HWReset;
+#endif
+#ifdef PVRSRV_USSE_EDM_STATUS_DEBUG
+	PPVRSRV_KERNEL_MEM_INFO	psKernelEDMStatusBufferMemInfo;
+#endif
+#if defined(SGX_FEATURE_OVERLAPPED_SPM)
+	PPVRSRV_KERNEL_MEM_INFO	psKernelTmpRgnHeaderMemInfo;
+#endif
+#if defined(SGX_FEATURE_SPM_MODE_0)
+	PPVRSRV_KERNEL_MEM_INFO	psKernelTmpDPMStateMemInfo;
+#endif
+
+
+	IMG_UINT32				ui32ClientRefCount;
+
+
+	IMG_UINT32				ui32CacheControl;
+
+
+	IMG_UINT32				ui32ClientBuildOptions;
+
+
+	SGX_MISCINFO_STRUCT_SIZES	sSGXStructSizes;
+
+
+
+
+	IMG_VOID				*pvMMUContextList;
+
+
+	IMG_BOOL				bForcePTOff;
+
+	IMG_UINT32				ui32EDMTaskReg0;
+	IMG_UINT32				ui32EDMTaskReg1;
+
+	IMG_UINT32				ui32ClkGateStatusReg;
+	IMG_UINT32				ui32ClkGateStatusMask;
+#if defined(SGX_FEATURE_MP)
+	IMG_UINT32				ui32MasterClkGateStatusReg;
+	IMG_UINT32				ui32MasterClkGateStatusMask;
+#endif
+	SGX_INIT_SCRIPTS		sScripts;
+
+
+	IMG_HANDLE 				hBIFResetPDOSMemHandle;
+	IMG_DEV_PHYADDR 		sBIFResetPDDevPAddr;
+	IMG_DEV_PHYADDR 		sBIFResetPTDevPAddr;
+	IMG_DEV_PHYADDR 		sBIFResetPageDevPAddr;
+	IMG_UINT32				*pui32BIFResetPD;
+	IMG_UINT32				*pui32BIFResetPT;
+
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+
+	IMG_HANDLE				hBRN22997PTPageOSMemHandle;
+	IMG_HANDLE				hBRN22997PDPageOSMemHandle;
+	IMG_DEV_PHYADDR 		sBRN22997PTDevPAddr;
+	IMG_DEV_PHYADDR 		sBRN22997PDDevPAddr;
+	IMG_UINT32				*pui32BRN22997PT;
+	IMG_UINT32				*pui32BRN22997PD;
+	IMG_SYS_PHYADDR 		sBRN22997SysPAddr;
+#endif
+
+#if defined(SUPPORT_HW_RECOVERY)
+
+	IMG_HANDLE				hTimer;
+
+	IMG_UINT32				ui32TimeStamp;
+#endif
+
+
+	IMG_UINT32				ui32NumResets;
+
+
+	PVRSRV_KERNEL_MEM_INFO			*psKernelSGXHostCtlMemInfo;
+	SGXMKIF_HOST_CTL				*psSGXHostCtl;
+
+
+	PVRSRV_KERNEL_MEM_INFO			*psKernelSGXTA3DCtlMemInfo;
+
+	IMG_UINT32				ui32Flags;
+
+	#if defined(PDUMP)
+	PVRSRV_SGX_PDUMP_CONTEXT	sPDContext;
+	#endif
+
+#if defined(SUPPORT_SGX_MMU_DUMMY_PAGE)
+
+	IMG_VOID				*pvDummyPTPageCpuVAddr;
+	IMG_DEV_PHYADDR			sDummyPTDevPAddr;
+	IMG_HANDLE				hDummyPTPageOSMemHandle;
+	IMG_VOID				*pvDummyDataPageCpuVAddr;
+	IMG_DEV_PHYADDR 		sDummyDataDevPAddr;
+	IMG_HANDLE				hDummyDataPageOSMemHandle;
+#endif
+
+	IMG_UINT32				asSGXDevData[SGX_MAX_DEV_DATA];
+
+} PVRSRV_SGXDEV_INFO;
+
+
+typedef struct _SGX_TIMING_INFORMATION_
+{
+	IMG_UINT32			ui32CoreClockSpeed;
+	IMG_UINT32			ui32HWRecoveryFreq;
+	IMG_BOOL			bEnableActivePM;
+	IMG_UINT32			ui32ActivePowManLatencyms;
+	IMG_UINT32			ui32uKernelFreq;
+} SGX_TIMING_INFORMATION;
+
+typedef struct _SGX_DEVICE_MAP_
+{
+	IMG_UINT32				ui32Flags;
+
+
+	IMG_SYS_PHYADDR			sRegsSysPBase;
+	IMG_CPU_PHYADDR			sRegsCpuPBase;
+	IMG_CPU_VIRTADDR		pvRegsCpuVBase;
+	IMG_UINT32				ui32RegsSize;
+
+#if defined(SGX_FEATURE_HOST_PORT)
+	IMG_SYS_PHYADDR			sHPSysPBase;
+	IMG_CPU_PHYADDR			sHPCpuPBase;
+	IMG_UINT32				ui32HPSize;
+#endif
+
+
+	IMG_SYS_PHYADDR			sLocalMemSysPBase;
+	IMG_DEV_PHYADDR			sLocalMemDevPBase;
+	IMG_CPU_PHYADDR			sLocalMemCpuPBase;
+	IMG_UINT32				ui32LocalMemSize;
+
+#if defined(SUPPORT_EXTERNAL_SYSTEM_CACHE)
+	IMG_UINT32				ui32ExtSysCacheRegsSize;
+	IMG_DEV_PHYADDR			sExtSysCacheRegsDevPBase;
+#endif
+
+
+	IMG_UINT32				ui32IRQ;
+
+#if !defined(SGX_DYNAMIC_TIMING_INFO)
+
+	SGX_TIMING_INFORMATION	sTimingInfo;
+#endif
+} SGX_DEVICE_MAP;
+
+
+struct _PVRSRV_STUB_PBDESC_
+{
+	IMG_UINT32		ui32RefCount;
+	IMG_UINT32		ui32TotalPBSize;
+	PVRSRV_KERNEL_MEM_INFO  *psSharedPBDescKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO  *psHWPBDescKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO	**ppsSubKernelMemInfos;
+	IMG_UINT32		ui32SubKernelMemInfosCount;
+	IMG_HANDLE		hDevCookie;
+	PVRSRV_KERNEL_MEM_INFO  *psBlockKernelMemInfo;
+	PVRSRV_KERNEL_MEM_INFO  *psHWBlockKernelMemInfo;
+	PVRSRV_STUB_PBDESC	*psNext;
+	PVRSRV_STUB_PBDESC	**ppsThis;
+};
+
+typedef struct _PVRSRV_SGX_CCB_INFO_
+{
+	PVRSRV_KERNEL_MEM_INFO	*psCCBMemInfo;
+	PVRSRV_KERNEL_MEM_INFO	*psCCBCtlMemInfo;
+	SGXMKIF_COMMAND		*psCommands;
+	IMG_UINT32				*pui32WriteOffset;
+	volatile IMG_UINT32		*pui32ReadOffset;
+#if defined(PDUMP)
+	IMG_UINT32				ui32CCBDumpWOff;
+#endif
+} PVRSRV_SGX_CCB_INFO;
+
+PVRSRV_ERROR SGXRegisterDevice (PVRSRV_DEVICE_NODE *psDeviceNode);
+
+IMG_VOID SGXOSTimer(IMG_VOID *pvData);
+
+IMG_VOID SGXReset(PVRSRV_SGXDEV_INFO	*psDevInfo,
+				  IMG_UINT32			 ui32PDUMPFlags);
+
+PVRSRV_ERROR SGXInitialise(PVRSRV_SGXDEV_INFO	*psDevInfo);
+PVRSRV_ERROR SGXDeinitialise(IMG_HANDLE hDevCookie);
+
+PVRSRV_ERROR SGXPrePowerState(IMG_HANDLE				hDevHandle,
+							  PVRSRV_DEV_POWER_STATE	eNewPowerState,
+							  PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+
+PVRSRV_ERROR SGXPostPowerState(IMG_HANDLE				hDevHandle,
+							   PVRSRV_DEV_POWER_STATE	eNewPowerState,
+							   PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+
+PVRSRV_ERROR SGXPreClockSpeedChange(IMG_HANDLE				hDevHandle,
+									IMG_BOOL				bIdleDevice,
+									PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+
+PVRSRV_ERROR SGXPostClockSpeedChange(IMG_HANDLE				hDevHandle,
+									 IMG_BOOL				bIdleDevice,
+									 PVRSRV_DEV_POWER_STATE	eCurrentPowerState);
+
+IMG_VOID SGXPanic(PVRSRV_DEVICE_NODE	*psDeviceNode);
+
+PVRSRV_ERROR SGXDevInitCompatCheck(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+#if defined(SGX_DYNAMIC_TIMING_INFO)
+IMG_VOID SysGetSGXTimingInformation(SGX_TIMING_INFORMATION *psSGXTimingInfo);
+#endif
+
+#if defined(NO_HARDWARE)
+static INLINE IMG_VOID NoHardwareGenerateEvent(PVRSRV_SGXDEV_INFO		*psDevInfo,
+												IMG_UINT32 ui32StatusRegister,
+												IMG_UINT32 ui32StatusValue,
+												IMG_UINT32 ui32StatusMask)
+{
+	IMG_UINT32 ui32RegVal;
+
+	ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, ui32StatusRegister);
+
+	ui32RegVal &= ~ui32StatusMask;
+	ui32RegVal |= (ui32StatusValue & ui32StatusMask);
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, ui32StatusRegister, ui32RegVal);
+}
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinit.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinit.c
new file mode 100644
index 0000000..45c16dc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxinit.c
@@ -0,0 +1,2267 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <stddef.h>
+
+#include "sgxdefs.h"
+#include "sgxmmu.h"
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "sgxapi_km.h"
+#include "sgxinfo.h"
+#include "sgx_mkif_km.h"
+#include "sgxconfig.h"
+#include "sysconfig.h"
+#include "pvr_bridge_km.h"
+
+#include "sgx_bridge_km.h"
+
+#include "pdump_km.h"
+#include "ra.h"
+#include "mmu.h"
+#include "handle.h"
+#include "perproc.h"
+
+#include "sgxutils.h"
+#include "pvrversion.h"
+#include "sgx_options.h"
+
+#include "lists.h"
+#include "srvkm.h"
+
+DECLARE_LIST_ANY_VA(PVRSRV_POWER_DEV);
+
+#if defined(SUPPORT_SGX_HWPERF)
+IMG_VOID* MatchPowerDeviceIndex_AnyVaCb(PVRSRV_POWER_DEV *psPowerDev, va_list va);
+#endif
+
+#define VAR(x) #x
+
+#define CHECK_SIZE(NAME) \
+{	\
+	if (psSGXStructSizes->ui32Sizeof_##NAME != psDevInfo->sSGXStructSizes.ui32Sizeof_##NAME) \
+	{	\
+		PVR_DPF((PVR_DBG_ERROR, "SGXDevInitCompatCheck: Size check failed for SGXMKIF_%s (client) = %d bytes, (ukernel) = %d bytes\n", \
+			VAR(NAME), \
+			psDevInfo->sSGXStructSizes.ui32Sizeof_##NAME, \
+			psSGXStructSizes->ui32Sizeof_##NAME )); \
+		bStructSizesFailed = IMG_TRUE; \
+	}	\
+}
+
+#if defined (SYS_USING_INTERRUPTS)
+IMG_BOOL SGX_ISRHandler(IMG_VOID *pvData);
+#endif
+
+IMG_UINT32 gui32EventStatusServicesByISR = 0;
+
+
+static
+PVRSRV_ERROR SGXGetMiscInfoUkernel(PVRSRV_SGXDEV_INFO	*psDevInfo,
+								   PVRSRV_DEVICE_NODE 	*psDeviceNode);
+
+
+static IMG_VOID SGXCommandComplete(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+#if defined(OS_SUPPORTS_IN_LISR)
+	if (OSInLISR(psDeviceNode->psSysData))
+	{
+
+		psDeviceNode->bReProcessDeviceCommandComplete = IMG_TRUE;
+	}
+	else
+	{
+		SGXScheduleProcessQueuesKM(psDeviceNode);
+	}
+#else
+	SGXScheduleProcessQueuesKM(psDeviceNode);
+#endif
+}
+
+static IMG_UINT32 DeinitDevInfo(PVRSRV_SGXDEV_INFO *psDevInfo)
+{
+	if (psDevInfo->psKernelCCBInfo != IMG_NULL)
+	{
+
+
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_SGX_CCB_INFO), psDevInfo->psKernelCCBInfo, IMG_NULL);
+	}
+
+	return PVRSRV_OK;
+}
+
+static PVRSRV_ERROR InitDevInfo(PVRSRV_PER_PROCESS_DATA *psPerProc,
+								PVRSRV_DEVICE_NODE *psDeviceNode,
+								SGX_BRIDGE_INIT_INFO *psInitInfo)
+{
+	PVRSRV_SGXDEV_INFO *psDevInfo = (PVRSRV_SGXDEV_INFO *)psDeviceNode->pvDevice;
+	PVRSRV_ERROR		eError;
+
+	PVRSRV_SGX_CCB_INFO	*psKernelCCBInfo = IMG_NULL;
+
+	PVR_UNREFERENCED_PARAMETER(psPerProc);
+	psDevInfo->sScripts = psInitInfo->sScripts;
+
+	psDevInfo->psKernelCCBMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelCCBMemInfo;
+	psDevInfo->psKernelCCB = (PVRSRV_SGX_KERNEL_CCB *) psDevInfo->psKernelCCBMemInfo->pvLinAddrKM;
+
+	psDevInfo->psKernelCCBCtlMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelCCBCtlMemInfo;
+	psDevInfo->psKernelCCBCtl = (PVRSRV_SGX_CCB_CTL *) psDevInfo->psKernelCCBCtlMemInfo->pvLinAddrKM;
+
+	psDevInfo->psKernelCCBEventKickerMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelCCBEventKickerMemInfo;
+	psDevInfo->pui32KernelCCBEventKicker = (IMG_UINT32 *)psDevInfo->psKernelCCBEventKickerMemInfo->pvLinAddrKM;
+
+	psDevInfo->psKernelSGXHostCtlMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelSGXHostCtlMemInfo;
+	psDevInfo->psSGXHostCtl = (SGXMKIF_HOST_CTL *)psDevInfo->psKernelSGXHostCtlMemInfo->pvLinAddrKM;
+
+	psDevInfo->psKernelSGXTA3DCtlMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelSGXTA3DCtlMemInfo;
+
+	psDevInfo->psKernelSGXMiscMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelSGXMiscMemInfo;
+
+#if defined(SGX_SUPPORT_HWPROFILING)
+	psDevInfo->psKernelHWProfilingMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelHWProfilingMemInfo;
+#endif
+#if defined(SUPPORT_SGX_HWPERF)
+	psDevInfo->psKernelHWPerfCBMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelHWPerfCBMemInfo;
+#endif
+#ifdef PVRSRV_USSE_EDM_STATUS_DEBUG
+	psDevInfo->psKernelEDMStatusBufferMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelEDMStatusBufferMemInfo;
+#endif
+#if defined(SGX_FEATURE_OVERLAPPED_SPM)
+	psDevInfo->psKernelTmpRgnHeaderMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelTmpRgnHeaderMemInfo;
+#endif
+#if defined(SGX_FEATURE_SPM_MODE_0)
+	psDevInfo->psKernelTmpDPMStateMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psInitInfo->hKernelTmpDPMStateMemInfo;
+#endif
+
+	psDevInfo->ui32ClientBuildOptions = psInitInfo->ui32ClientBuildOptions;
+
+
+	psDevInfo->sSGXStructSizes = psInitInfo->sSGXStructSizes;
+
+
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(PVRSRV_SGX_CCB_INFO),
+						(IMG_VOID **)&psKernelCCBInfo, 0,
+						"SGX Circular Command Buffer Info");
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"InitDevInfo: Failed to alloc memory"));
+		goto failed_allockernelccb;
+	}
+
+
+	OSMemSet(psKernelCCBInfo, 0, sizeof(PVRSRV_SGX_CCB_INFO));
+	psKernelCCBInfo->psCCBMemInfo		= psDevInfo->psKernelCCBMemInfo;
+	psKernelCCBInfo->psCCBCtlMemInfo	= psDevInfo->psKernelCCBCtlMemInfo;
+	psKernelCCBInfo->psCommands			= psDevInfo->psKernelCCB->asCommands;
+	psKernelCCBInfo->pui32WriteOffset	= &psDevInfo->psKernelCCBCtl->ui32WriteOffset;
+	psKernelCCBInfo->pui32ReadOffset	= &psDevInfo->psKernelCCBCtl->ui32ReadOffset;
+	psDevInfo->psKernelCCBInfo = psKernelCCBInfo;
+
+
+
+	OSMemCopy(psDevInfo->aui32HostKickAddr, psInitInfo->aui32HostKickAddr,
+			  SGXMKIF_CMD_MAX * sizeof(psDevInfo->aui32HostKickAddr[0]));
+
+	psDevInfo->bForcePTOff = IMG_FALSE;
+
+	psDevInfo->ui32CacheControl = psInitInfo->ui32CacheControl;
+
+	psDevInfo->ui32EDMTaskReg0 = psInitInfo->ui32EDMTaskReg0;
+	psDevInfo->ui32EDMTaskReg1 = psInitInfo->ui32EDMTaskReg1;
+	psDevInfo->ui32ClkGateStatusReg = psInitInfo->ui32ClkGateStatusReg;
+	psDevInfo->ui32ClkGateStatusMask = psInitInfo->ui32ClkGateStatusMask;
+#if defined(SGX_FEATURE_MP)
+	psDevInfo->ui32MasterClkGateStatusReg = psInitInfo->ui32MasterClkGateStatusReg;
+	psDevInfo->ui32MasterClkGateStatusMask = psInitInfo->ui32MasterClkGateStatusMask;
+#endif
+
+
+
+	OSMemCopy(&psDevInfo->asSGXDevData,  &psInitInfo->asInitDevData, sizeof(psDevInfo->asSGXDevData));
+
+	return PVRSRV_OK;
+
+failed_allockernelccb:
+	DeinitDevInfo(psDevInfo);
+
+	return eError;
+}
+
+
+
+
+static PVRSRV_ERROR SGXRunScript(PVRSRV_SGXDEV_INFO *psDevInfo, SGX_INIT_COMMAND *psScript, IMG_UINT32 ui32NumInitCommands)
+{
+	IMG_UINT32 ui32PC;
+	SGX_INIT_COMMAND *psComm;
+
+	for (ui32PC = 0, psComm = psScript;
+		ui32PC < ui32NumInitCommands;
+		ui32PC++, psComm++)
+	{
+		switch (psComm->eOp)
+		{
+			case SGX_INIT_OP_WRITE_HW_REG:
+			{
+				OSWriteHWReg(psDevInfo->pvRegsBaseKM, psComm->sWriteHWReg.ui32Offset, psComm->sWriteHWReg.ui32Value);
+				PDUMPREG(psComm->sWriteHWReg.ui32Offset, psComm->sWriteHWReg.ui32Value);
+				break;
+			}
+#if defined(PDUMP)
+			case SGX_INIT_OP_PDUMP_HW_REG:
+			{
+				PDUMPREG(psComm->sPDumpHWReg.ui32Offset, psComm->sPDumpHWReg.ui32Value);
+				break;
+			}
+#endif
+			case SGX_INIT_OP_HALT:
+			{
+				return PVRSRV_OK;
+			}
+			case SGX_INIT_OP_ILLEGAL:
+
+			default:
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SGXRunScript: PC %d: Illegal command: %d", ui32PC, psComm->eOp));
+				return PVRSRV_ERROR_GENERIC;
+			}
+		}
+
+	}
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+PVRSRV_ERROR SGXInitialise(PVRSRV_SGXDEV_INFO	*psDevInfo)
+{
+	PVRSRV_ERROR			eError;
+	PVRSRV_KERNEL_MEM_INFO	*psSGXHostCtlMemInfo = psDevInfo->psKernelSGXHostCtlMemInfo;
+	SGXMKIF_HOST_CTL		*psSGXHostCtl = psSGXHostCtlMemInfo->pvLinAddrKM;
+#if defined(PDUMP)
+	static IMG_BOOL			bFirstTime = IMG_TRUE;
+#endif
+
+
+
+	PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS, "SGX initialisation script part 1\n");
+	eError = SGXRunScript(psDevInfo, psDevInfo->sScripts.asInitCommandsPart1, SGX_MAX_INIT_COMMANDS);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXInitialise: SGXRunScript (part 1) failed (%d)", eError));
+		return (PVRSRV_ERROR_GENERIC);
+	}
+	PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS, "End of SGX initialisation script part 1\n");
+
+
+	SGXReset(psDevInfo, PDUMP_FLAGS_CONTINUOUS);
+
+#if defined(EUR_CR_POWER)
+#if defined(SGX531)
+
+
+
+
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_POWER, 1);
+	PDUMPREG(EUR_CR_POWER, 1);
+#else
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_POWER, 0);
+	PDUMPREG(EUR_CR_POWER, 0);
+#endif
+#endif
+
+
+	*psDevInfo->pui32KernelCCBEventKicker = 0;
+#if defined(PDUMP)
+	if (bFirstTime)
+	{
+		psDevInfo->ui32KernelCCBEventKickerDumpVal = 0;
+		PDUMPMEM(&psDevInfo->ui32KernelCCBEventKickerDumpVal,
+				 psDevInfo->psKernelCCBEventKickerMemInfo, 0,
+				 sizeof(*psDevInfo->pui32KernelCCBEventKicker), PDUMP_FLAGS_CONTINUOUS,
+				 MAKEUNIQUETAG(psDevInfo->psKernelCCBEventKickerMemInfo));
+	}
+#endif
+
+
+
+	PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS, "SGX initialisation script part 2\n");
+	eError = SGXRunScript(psDevInfo, psDevInfo->sScripts.asInitCommandsPart2, SGX_MAX_INIT_COMMANDS);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXInitialise: SGXRunScript (part 2) failed (%d)", eError));
+		return (PVRSRV_ERROR_GENERIC);
+	}
+	PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS, "End of SGX initialisation script part 2\n");
+
+
+	psSGXHostCtl->ui32InitStatus = 0;
+#if defined(PDUMP)
+	PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS,
+						  "Reset the SGX microkernel initialisation status\n");
+	PDUMPMEM(IMG_NULL, psSGXHostCtlMemInfo,
+			 offsetof(SGXMKIF_HOST_CTL, ui32InitStatus),
+			 sizeof(IMG_UINT32), PDUMP_FLAGS_CONTINUOUS,
+			 MAKEUNIQUETAG(psSGXHostCtlMemInfo));
+#endif
+
+#if defined(SGX_FEATURE_MULTI_EVENT_KICK)
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM,
+				 SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK2, 0),
+				 EUR_CR_EVENT_KICK2_NOW_MASK);
+#else
+	*psDevInfo->pui32KernelCCBEventKicker = (*psDevInfo->pui32KernelCCBEventKicker + 1) & 0xFF;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM,
+				 SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK, 0),
+				 EUR_CR_EVENT_KICK_NOW_MASK);
+#endif
+
+#if defined(PDUMP)
+
+
+
+
+
+
+	if (bFirstTime)
+	{
+#if defined(SGX_FEATURE_MULTI_EVENT_KICK)
+		PDUMPREG(SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK2, 0), EUR_CR_EVENT_KICK2_NOW_MASK);
+#else
+		psDevInfo->ui32KernelCCBEventKickerDumpVal = 1;
+		PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS,
+							  "First increment of the SGX event kicker value\n");
+		PDUMPMEM(&psDevInfo->ui32KernelCCBEventKickerDumpVal,
+				 psDevInfo->psKernelCCBEventKickerMemInfo,
+				 0,
+				 sizeof(IMG_UINT32),
+				 PDUMP_FLAGS_CONTINUOUS,
+				 MAKEUNIQUETAG(psDevInfo->psKernelCCBEventKickerMemInfo));
+		PDUMPREG(SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK, 0), EUR_CR_EVENT_KICK_NOW_MASK);
+#endif
+		bFirstTime = IMG_FALSE;
+	}
+#endif
+
+#if !defined(NO_HARDWARE)
+
+
+	if (PollForValueKM(&psSGXHostCtl->ui32InitStatus,
+					   PVRSRV_USSE_EDM_INIT_COMPLETE,
+					   PVRSRV_USSE_EDM_INIT_COMPLETE,
+					   MAX_HW_TIME_US/WAIT_TRY_COUNT,
+					   WAIT_TRY_COUNT) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXInitialise: Wait for uKernel initialisation failed"));
+		PVR_DBG_BREAK;
+		return PVRSRV_ERROR_RETRY;
+	}
+#endif
+
+#if defined(PDUMP)
+	PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS,
+						  "Wait for the SGX microkernel initialisation to complete");
+	PDUMPMEMPOL(psSGXHostCtlMemInfo,
+				offsetof(SGXMKIF_HOST_CTL, ui32InitStatus),
+				PVRSRV_USSE_EDM_INIT_COMPLETE,
+				PVRSRV_USSE_EDM_INIT_COMPLETE,
+				PDUMP_POLL_OPERATOR_EQUAL,
+				PDUMP_FLAGS_CONTINUOUS,
+				MAKEUNIQUETAG(psSGXHostCtlMemInfo));
+#endif
+
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+
+
+
+	WorkaroundBRN22997ReadHostPort(psDevInfo);
+#endif
+
+	PVR_ASSERT(psDevInfo->psKernelCCBCtl->ui32ReadOffset == psDevInfo->psKernelCCBCtl->ui32WriteOffset);
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR SGXDeinitialise(IMG_HANDLE hDevCookie)
+
+{
+	PVRSRV_SGXDEV_INFO	*psDevInfo = (PVRSRV_SGXDEV_INFO *) hDevCookie;
+	PVRSRV_ERROR		eError;
+
+
+	if (psDevInfo->pvRegsBaseKM == IMG_NULL)
+	{
+		return PVRSRV_OK;
+	}
+
+	eError = SGXRunScript(psDevInfo, psDevInfo->sScripts.asDeinitCommands, SGX_MAX_DEINIT_COMMANDS);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXDeinitialise: SGXRunScript failed (%d)", eError));
+		return (PVRSRV_ERROR_GENERIC);
+	}
+
+	return PVRSRV_OK;
+}
+
+
+static PVRSRV_ERROR DevInitSGXPart1 (IMG_VOID *pvDeviceNode)
+{
+	PVRSRV_SGXDEV_INFO	*psDevInfo;
+	IMG_HANDLE		hKernelDevMemContext;
+	IMG_DEV_PHYADDR		sPDDevPAddr;
+	IMG_UINT32		i;
+	PVRSRV_DEVICE_NODE  *psDeviceNode = (PVRSRV_DEVICE_NODE *)pvDeviceNode;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap = psDeviceNode->sDevMemoryInfo.psDeviceMemoryHeap;
+	PVRSRV_ERROR		eError;
+
+	PDUMPCOMMENT("SGX Initialisation Part 1");
+
+
+	PDUMPCOMMENT("SGX Core Version Information: %s", SGX_CORE_FRIENDLY_NAME);
+#ifdef SGX_CORE_REV
+	PDUMPCOMMENT("SGX Core Revision Information: %d", SGX_CORE_REV);
+#else
+	PDUMPCOMMENT("SGX Core Revision Information: head rtl");
+#endif
+
+	#if defined(SGX_FEATURE_SYSTEM_CACHE)
+	PDUMPCOMMENT("SGX System Level Cache is present\r\n");
+	#if defined(SGX_BYPASS_SYSTEM_CACHE)
+	PDUMPCOMMENT("SGX System Level Cache is bypassed\r\n");
+	#endif
+	#endif
+
+
+	if(OSAllocMem( PVRSRV_OS_NON_PAGEABLE_HEAP,
+					 sizeof(PVRSRV_SGXDEV_INFO),
+					 (IMG_VOID **)&psDevInfo, IMG_NULL,
+					 "SGX Device Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart1 : Failed to alloc memory for DevInfo"));
+		return (PVRSRV_ERROR_OUT_OF_MEMORY);
+	}
+	OSMemSet (psDevInfo, 0, sizeof(PVRSRV_SGXDEV_INFO));
+
+
+	psDevInfo->eDeviceType 		= DEV_DEVICE_TYPE;
+	psDevInfo->eDeviceClass 	= DEV_DEVICE_CLASS;
+
+
+	psDeviceNode->pvDevice = (IMG_PVOID)psDevInfo;
+
+
+	psDevInfo->pvDeviceMemoryHeap = (IMG_VOID*)psDeviceMemoryHeap;
+
+
+	hKernelDevMemContext = BM_CreateContext(psDeviceNode,
+											&sPDDevPAddr,
+											IMG_NULL,
+											IMG_NULL);
+	if (hKernelDevMemContext == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart1: Failed BM_CreateContext"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	psDevInfo->sKernelPDDevPAddr = sPDDevPAddr;
+
+
+	for(i=0; i<psDeviceNode->sDevMemoryInfo.ui32HeapCount; i++)
+	{
+		IMG_HANDLE hDevMemHeap;
+
+		switch(psDeviceMemoryHeap[i].DevMemHeapType)
+		{
+			case DEVICE_MEMORY_HEAP_KERNEL:
+			case DEVICE_MEMORY_HEAP_SHARED:
+			case DEVICE_MEMORY_HEAP_SHARED_EXPORTED:
+			{
+				hDevMemHeap = BM_CreateHeap (hKernelDevMemContext,
+												&psDeviceMemoryHeap[i]);
+
+
+
+				psDeviceMemoryHeap[i].hDevMemHeap = hDevMemHeap;
+				break;
+			}
+		}
+	}
+
+	eError = MMU_BIFResetPDAlloc(psDevInfo);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevInitSGX : Failed to alloc memory for BIF reset"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGXGetInfoForSrvinitKM(IMG_HANDLE hDevHandle, SGX_BRIDGE_INFO_FOR_SRVINIT *psInitInfo)
+{
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+	PVRSRV_SGXDEV_INFO	*psDevInfo;
+	PVRSRV_ERROR		eError;
+
+	PDUMPCOMMENT("SGXGetInfoForSrvinit");
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevHandle;
+	psDevInfo = (PVRSRV_SGXDEV_INFO *)psDeviceNode->pvDevice;
+
+	psInitInfo->sPDDevPAddr = psDevInfo->sKernelPDDevPAddr;
+
+	eError = PVRSRVGetDeviceMemHeapsKM(hDevHandle, &psInitInfo->asHeapInfo[0]);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXGetInfoForSrvinit: PVRSRVGetDeviceMemHeapsKM failed (%d)", eError));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return eError;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR DevInitSGXPart2KM (PVRSRV_PER_PROCESS_DATA *psPerProc,
+                                IMG_HANDLE hDevHandle,
+                                SGX_BRIDGE_INIT_INFO *psInitInfo)
+{
+	PVRSRV_DEVICE_NODE		*psDeviceNode;
+	PVRSRV_SGXDEV_INFO		*psDevInfo;
+	PVRSRV_ERROR			eError;
+	SGX_DEVICE_MAP			*psSGXDeviceMap;
+	PVRSRV_DEV_POWER_STATE	eDefaultPowerState;
+
+	PDUMPCOMMENT("SGX Initialisation Part 2");
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevHandle;
+	psDevInfo = (PVRSRV_SGXDEV_INFO *)psDeviceNode->pvDevice;
+
+
+
+	eError = InitDevInfo(psPerProc, psDeviceNode, psInitInfo);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart2KM: Failed to load EDM program"));
+		goto failed_init_dev_info;
+	}
+
+
+	eError = SysGetDeviceMemoryMap(PVRSRV_DEVICE_TYPE_SGX,
+									(IMG_VOID**)&psSGXDeviceMap);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart2KM: Failed to get device memory map!"));
+		return PVRSRV_ERROR_INIT_FAILURE;
+	}
+
+
+	if (psSGXDeviceMap->pvRegsCpuVBase)
+	{
+		psDevInfo->pvRegsBaseKM = psSGXDeviceMap->pvRegsCpuVBase;
+	}
+	else
+	{
+
+		psDevInfo->pvRegsBaseKM = OSMapPhysToLin(psSGXDeviceMap->sRegsCpuPBase,
+											   psSGXDeviceMap->ui32RegsSize,
+											   PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+											   IMG_NULL);
+		if (!psDevInfo->pvRegsBaseKM)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart2KM: Failed to map in regs\n"));
+			return PVRSRV_ERROR_BAD_MAPPING;
+		}
+	}
+	psDevInfo->ui32RegSize = psSGXDeviceMap->ui32RegsSize;
+	psDevInfo->sRegsPhysBase = psSGXDeviceMap->sRegsSysPBase;
+
+
+#if defined(SGX_FEATURE_HOST_PORT)
+	if (psSGXDeviceMap->ui32Flags & SGX_HOSTPORT_PRESENT)
+	{
+
+		psDevInfo->pvHostPortBaseKM = OSMapPhysToLin(psSGXDeviceMap->sHPCpuPBase,
+									  	           psSGXDeviceMap->ui32HPSize,
+									  	           PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+									  	           IMG_NULL);
+		if (!psDevInfo->pvHostPortBaseKM)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart2KM: Failed to map in host port\n"));
+			return PVRSRV_ERROR_BAD_MAPPING;
+		}
+		psDevInfo->ui32HPSize = psSGXDeviceMap->ui32HPSize;
+		psDevInfo->sHPSysPAddr = psSGXDeviceMap->sHPSysPBase;
+	}
+#endif
+
+#if defined (SYS_USING_INTERRUPTS)
+
+
+	psDeviceNode->pvISRData = psDeviceNode;
+
+	PVR_ASSERT(psDeviceNode->pfnDeviceISR == SGX_ISRHandler);
+
+#endif
+
+
+	psDevInfo->psSGXHostCtl->ui32PowerStatus |= PVRSRV_USSE_EDM_POWMAN_NO_WORK;
+	eDefaultPowerState = PVRSRV_DEV_POWER_STATE_OFF;
+
+	eError = PVRSRVRegisterPowerDevice (psDeviceNode->sDevId.ui32DeviceIndex,
+										SGXPrePowerState, SGXPostPowerState,
+										SGXPreClockSpeedChange, SGXPostClockSpeedChange,
+										(IMG_HANDLE)psDeviceNode,
+										PVRSRV_DEV_POWER_STATE_OFF,
+										eDefaultPowerState);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevInitSGXPart2KM: failed to register device with power manager"));
+		return eError;
+	}
+
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+	eError = WorkaroundBRN22997Alloc(psDevInfo);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXInitialise : Failed to alloc memory for BRN22997 workaround"));
+		return eError;
+	}
+#endif
+
+#if defined(SUPPORT_EXTERNAL_SYSTEM_CACHE)
+
+	psDevInfo->ui32ExtSysCacheRegsSize = psSGXDeviceMap->ui32ExtSysCacheRegsSize;
+	psDevInfo->sExtSysCacheRegsDevPBase = psSGXDeviceMap->sExtSysCacheRegsDevPBase;
+	eError = MMU_MapExtSystemCacheRegs(psDeviceNode);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXInitialise : Failed to map external system cache registers"));
+		return eError;
+	}
+#endif
+
+
+
+	OSMemSet(psDevInfo->psKernelCCB, 0, sizeof(PVRSRV_SGX_KERNEL_CCB));
+	OSMemSet(psDevInfo->psKernelCCBCtl, 0, sizeof(PVRSRV_SGX_CCB_CTL));
+	OSMemSet(psDevInfo->pui32KernelCCBEventKicker, 0, sizeof(*psDevInfo->pui32KernelCCBEventKicker));
+	PDUMPCOMMENT("Initialise Kernel CCB");
+	PDUMPMEM(IMG_NULL, psDevInfo->psKernelCCBMemInfo, 0, sizeof(PVRSRV_SGX_KERNEL_CCB), PDUMP_FLAGS_CONTINUOUS, MAKEUNIQUETAG(psDevInfo->psKernelCCBMemInfo));
+	PDUMPCOMMENT("Initialise Kernel CCB Control");
+	PDUMPMEM(IMG_NULL, psDevInfo->psKernelCCBCtlMemInfo, 0, sizeof(PVRSRV_SGX_CCB_CTL), PDUMP_FLAGS_CONTINUOUS, MAKEUNIQUETAG(psDevInfo->psKernelCCBCtlMemInfo));
+	PDUMPCOMMENT("Initialise Kernel CCB Event Kicker");
+	PDUMPMEM(IMG_NULL, psDevInfo->psKernelCCBEventKickerMemInfo, 0, sizeof(*psDevInfo->pui32KernelCCBEventKicker), PDUMP_FLAGS_CONTINUOUS, MAKEUNIQUETAG(psDevInfo->psKernelCCBEventKickerMemInfo));
+
+	return PVRSRV_OK;
+
+failed_init_dev_info:
+	return eError;
+}
+
+static PVRSRV_ERROR DevDeInitSGX (IMG_VOID *pvDeviceNode)
+{
+	PVRSRV_DEVICE_NODE			*psDeviceNode = (PVRSRV_DEVICE_NODE *)pvDeviceNode;
+	PVRSRV_SGXDEV_INFO			*psDevInfo = (PVRSRV_SGXDEV_INFO*)psDeviceNode->pvDevice;
+	PVRSRV_ERROR				eError;
+	IMG_UINT32					ui32Heap;
+	DEVICE_MEMORY_HEAP_INFO		*psDeviceMemoryHeap;
+	SGX_DEVICE_MAP				*psSGXDeviceMap;
+
+	if (!psDevInfo)
+	{
+
+		PVR_DPF((PVR_DBG_ERROR,"DevDeInitSGX: Null DevInfo"));
+		return PVRSRV_OK;
+	}
+
+#if defined(SUPPORT_HW_RECOVERY)
+	if (psDevInfo->hTimer)
+	{
+		eError = OSRemoveTimer(psDevInfo->hTimer);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"DevDeInitSGX: Failed to remove timer"));
+			return 	eError;
+		}
+		psDevInfo->hTimer = IMG_NULL;
+	}
+#endif
+
+#if defined(SUPPORT_EXTERNAL_SYSTEM_CACHE)
+
+	eError = MMU_UnmapExtSystemCacheRegs(psDeviceNode);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevDeInitSGX: Failed to unmap ext system cache registers"));
+		return eError;
+	}
+#endif
+
+#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030) && defined(SGX_FEATURE_HOST_PORT)
+	WorkaroundBRN22997Free(psDevInfo);
+#endif
+
+	MMU_BIFResetPDFree(psDevInfo);
+
+
+
+
+	DeinitDevInfo(psDevInfo);
+
+
+	psDeviceMemoryHeap = (DEVICE_MEMORY_HEAP_INFO *)psDevInfo->pvDeviceMemoryHeap;
+	for(ui32Heap=0; ui32Heap<psDeviceNode->sDevMemoryInfo.ui32HeapCount; ui32Heap++)
+	{
+		switch(psDeviceMemoryHeap[ui32Heap].DevMemHeapType)
+		{
+			case DEVICE_MEMORY_HEAP_KERNEL:
+			case DEVICE_MEMORY_HEAP_SHARED:
+			case DEVICE_MEMORY_HEAP_SHARED_EXPORTED:
+			{
+				if (psDeviceMemoryHeap[ui32Heap].hDevMemHeap != IMG_NULL)
+				{
+					BM_DestroyHeap(psDeviceMemoryHeap[ui32Heap].hDevMemHeap);
+				}
+				break;
+			}
+		}
+	}
+
+
+	eError = BM_DestroyContext(psDeviceNode->sDevMemoryInfo.pBMKernelContext, IMG_NULL);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevDeInitSGX : Failed to destroy kernel context"));
+		return eError;
+	}
+
+
+	eError = PVRSRVRemovePowerDevice (((PVRSRV_DEVICE_NODE*)pvDeviceNode)->sDevId.ui32DeviceIndex);
+	if (eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	eError = SysGetDeviceMemoryMap(PVRSRV_DEVICE_TYPE_SGX,
+									(IMG_VOID**)&psSGXDeviceMap);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DevDeInitSGX: Failed to get device memory map!"));
+		return eError;
+	}
+
+
+	if (!psSGXDeviceMap->pvRegsCpuVBase)
+	{
+
+		if (psDevInfo->pvRegsBaseKM != IMG_NULL)
+		{
+			OSUnMapPhysToLin(psDevInfo->pvRegsBaseKM,
+							 psDevInfo->ui32RegSize,
+							 PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+							 IMG_NULL);
+		}
+	}
+
+#if defined(SGX_FEATURE_HOST_PORT)
+	if (psSGXDeviceMap->ui32Flags & SGX_HOSTPORT_PRESENT)
+	{
+
+		if (psDevInfo->pvHostPortBaseKM != IMG_NULL)
+		{
+			OSUnMapPhysToLin(psDevInfo->pvHostPortBaseKM,
+						   psDevInfo->ui32HPSize,
+						   PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+						   IMG_NULL);
+		}
+	}
+#endif
+
+
+
+	OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				sizeof(PVRSRV_SGXDEV_INFO),
+				psDevInfo,
+				0);
+
+	psDeviceNode->pvDevice = IMG_NULL;
+
+	if (psDeviceMemoryHeap != IMG_NULL)
+	{
+
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				sizeof(DEVICE_MEMORY_HEAP_INFO) * SGX_MAX_HEAP_ID,
+				psDeviceMemoryHeap,
+				0);
+	}
+
+	return PVRSRV_OK;
+}
+
+
+IMG_VOID SGXDumpDebugInfo (PVRSRV_DEVICE_NODE *psDeviceNode,
+						   IMG_BOOL			  bDumpSGXRegs)
+{
+	IMG_UINT			ui32RegVal;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+
+	if (bDumpSGXRegs)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGX Register Base Address (Linear):   0x%08X", psDevInfo->pvRegsBaseKM));
+		PVR_DPF((PVR_DBG_ERROR,"SGX Register Base Address (Physical): 0x%08X", psDevInfo->sRegsPhysBase));
+
+
+
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_STATUS);
+		if (ui32RegVal & (EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_MASK | EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_MASK))
+		{
+			PVR_LOG(("DPM out of memory!!"));
+		}
+		PVR_LOG(("EUR_CR_EVENT_STATUS:     %x", ui32RegVal));
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_STATUS2);
+		PVR_LOG(("EUR_CR_EVENT_STATUS2:    %x", ui32RegVal));
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL);
+		PVR_LOG(("EUR_CR_BIF_CTRL:         %x", ui32RegVal));
+
+		#if defined(EUR_CR_BIF_BANK0)
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_BANK0);
+		PVR_LOG(("EUR_CR_BIF_BANK0:        %x", ui32RegVal));
+		#endif
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_INT_STAT);
+		PVR_LOG(("EUR_CR_BIF_INT_STAT:     %x", ui32RegVal));
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_FAULT);
+		PVR_LOG(("EUR_CR_BIF_FAULT:        %x", ui32RegVal));
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_MEM_REQ_STAT);
+		PVR_LOG(("EUR_CR_BIF_MEM_REQ_STAT: %x", ui32RegVal));
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_CLKGATECTL);
+		PVR_LOG(("EUR_CR_CLKGATECTL:       %x", ui32RegVal));
+
+		#if defined(EUR_CR_PDS_PC_BASE)
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_PDS_PC_BASE);
+		PVR_LOG(("EUR_CR_PDS_PC_BASE:      %x", ui32RegVal));
+		#endif
+
+
+	}
+
+	#if defined(PVRSRV_USSE_EDM_STATUS_DEBUG)
+	{
+		IMG_UINT32	*pui32MKTraceBuffer = psDevInfo->psKernelEDMStatusBufferMemInfo->pvLinAddrKM;
+		IMG_UINT32	ui32LastStatusCode, ui32WriteOffset;
+
+		ui32LastStatusCode = *pui32MKTraceBuffer;
+		pui32MKTraceBuffer++;
+		ui32WriteOffset = *pui32MKTraceBuffer;
+		pui32MKTraceBuffer++;
+
+		PVR_LOG(("Last SGX microkernel status code: 0x%x", ui32LastStatusCode));
+
+		#if defined(PVRSRV_DUMP_MK_TRACE)
+
+
+		{
+			IMG_UINT32	ui32LoopCounter;
+
+			for (ui32LoopCounter = 0;
+				 ui32LoopCounter < SGXMK_TRACE_BUFFER_SIZE;
+				 ui32LoopCounter++)
+			{
+				IMG_UINT32	*pui32BufPtr;
+				pui32BufPtr = pui32MKTraceBuffer +
+								(((ui32WriteOffset + ui32LoopCounter) % SGXMK_TRACE_BUFFER_SIZE) * 4);
+				PVR_LOG(("(MKT%u) %08X %08X %08X %08X", ui32LoopCounter,
+						 pui32BufPtr[2], pui32BufPtr[3], pui32BufPtr[1], pui32BufPtr[0]));
+			}
+		}
+		#endif
+	}
+	#endif
+
+	{
+
+
+		IMG_UINT32	*pui32HostCtlBuffer = (IMG_UINT32 *)psDevInfo->psSGXHostCtl;
+		IMG_UINT32	ui32LoopCounter;
+
+		PVR_LOG(("SGX Host control:"));
+
+		for (ui32LoopCounter = 0;
+			 ui32LoopCounter < sizeof(*psDevInfo->psSGXHostCtl) / sizeof(*pui32HostCtlBuffer);
+			 ui32LoopCounter += 4)
+		{
+			PVR_LOG(("\t0x%X: 0x%08X 0x%08X 0x%08X 0x%08X", ui32LoopCounter * sizeof(*pui32HostCtlBuffer),
+					pui32HostCtlBuffer[ui32LoopCounter + 0], pui32HostCtlBuffer[ui32LoopCounter + 1],
+					pui32HostCtlBuffer[ui32LoopCounter + 2], pui32HostCtlBuffer[ui32LoopCounter + 3]));
+		}
+	}
+
+	{
+
+
+		IMG_UINT32	*pui32TA3DCtlBuffer = psDevInfo->psKernelSGXTA3DCtlMemInfo->pvLinAddrKM;
+		IMG_UINT32	ui32LoopCounter;
+
+		PVR_LOG(("SGX TA/3D control:"));
+
+		for (ui32LoopCounter = 0;
+			 ui32LoopCounter < psDevInfo->psKernelSGXTA3DCtlMemInfo->ui32AllocSize / sizeof(*pui32TA3DCtlBuffer);
+			 ui32LoopCounter += 4)
+		{
+			PVR_LOG(("\t0x%X: 0x%08X 0x%08X 0x%08X 0x%08X", ui32LoopCounter * sizeof(*pui32TA3DCtlBuffer),
+					pui32TA3DCtlBuffer[ui32LoopCounter + 0], pui32TA3DCtlBuffer[ui32LoopCounter + 1],
+					pui32TA3DCtlBuffer[ui32LoopCounter + 2], pui32TA3DCtlBuffer[ui32LoopCounter + 3]));
+		}
+	}
+
+	QueueDumpDebugInfo();
+}
+
+
+#if defined(SYS_USING_INTERRUPTS) || defined(SUPPORT_HW_RECOVERY)
+static
+IMG_VOID HWRecoveryResetSGX (PVRSRV_DEVICE_NODE *psDeviceNode,
+							 IMG_UINT32 		ui32Component,
+							 IMG_UINT32			ui32CallerID)
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = (PVRSRV_SGXDEV_INFO*)psDeviceNode->pvDevice;
+	SGXMKIF_HOST_CTL	*psSGXHostCtl = (SGXMKIF_HOST_CTL *)psDevInfo->psSGXHostCtl;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Component);
+
+
+
+	eError = PVRSRVPowerLock(ui32CallerID, IMG_FALSE);
+	if(eError != PVRSRV_OK)
+	{
+
+
+
+		PVR_DPF((PVR_DBG_WARNING,"HWRecoveryResetSGX: Power transition in progress"));
+		return;
+	}
+
+	psSGXHostCtl->ui32InterruptClearFlags |= PVRSRV_USSE_EDM_INTERRUPT_HWR;
+
+	PVR_LOG(("HWRecoveryResetSGX: SGX Hardware Recovery triggered"));
+
+	SGXDumpDebugInfo(psDeviceNode, IMG_TRUE);
+
+
+	PDUMPSUSPEND();
+
+
+	eError = SGXInitialise(psDevInfo);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"HWRecoveryResetSGX: SGXInitialise failed (%d)", eError));
+	}
+
+
+	PDUMPRESUME();
+
+	PVRSRVPowerUnlock(ui32CallerID);
+
+
+	SGXScheduleProcessQueuesKM(psDeviceNode);
+
+
+
+	PVRSRVProcessQueues(ui32CallerID, IMG_TRUE);
+}
+#endif
+
+
+#if defined(SUPPORT_HW_RECOVERY)
+IMG_VOID SGXOSTimer(IMG_VOID *pvData)
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode = pvData;
+	PVRSRV_SGXDEV_INFO *psDevInfo = psDeviceNode->pvDevice;
+	static IMG_UINT32	ui32EDMTasks = 0;
+	static IMG_UINT32	ui32LockupCounter = 0;
+	static IMG_UINT32	ui32NumResets = 0;
+	IMG_UINT32		ui32CurrentEDMTasks;
+	IMG_BOOL		bLockup = IMG_FALSE;
+	IMG_BOOL		bPoweredDown;
+
+
+	psDevInfo->ui32TimeStamp++;
+
+#if defined(NO_HARDWARE)
+	bPoweredDown = IMG_TRUE;
+#else
+	bPoweredDown = SGXIsDevicePowered(psDeviceNode) ? IMG_FALSE : IMG_TRUE;
+#endif
+
+
+
+	if (bPoweredDown)
+	{
+		ui32LockupCounter = 0;
+	}
+	else
+	{
+
+		ui32CurrentEDMTasks = OSReadHWReg(psDevInfo->pvRegsBaseKM, psDevInfo->ui32EDMTaskReg0);
+		if (psDevInfo->ui32EDMTaskReg1 != 0)
+		{
+			ui32CurrentEDMTasks ^= OSReadHWReg(psDevInfo->pvRegsBaseKM, psDevInfo->ui32EDMTaskReg1);
+		}
+		if ((ui32CurrentEDMTasks == ui32EDMTasks) &&
+			(psDevInfo->ui32NumResets == ui32NumResets))
+		{
+			ui32LockupCounter++;
+			if (ui32LockupCounter == 3)
+			{
+				ui32LockupCounter = 0;
+				PVR_DPF((PVR_DBG_ERROR, "SGXOSTimer() detected SGX lockup (0x%x tasks)", ui32EDMTasks));
+
+				bLockup = IMG_TRUE;
+			}
+		}
+		else
+		{
+			ui32LockupCounter = 0;
+			ui32EDMTasks = ui32CurrentEDMTasks;
+			ui32NumResets = psDevInfo->ui32NumResets;
+		}
+	}
+
+	if (bLockup)
+	{
+		SGXMKIF_HOST_CTL	*psSGXHostCtl = (SGXMKIF_HOST_CTL *)psDevInfo->psSGXHostCtl;
+
+
+		psSGXHostCtl->ui32HostDetectedLockups ++;
+
+		PVR_LOG(("HWRecoveryResetSGX: From Kernel"));
+
+		HWRecoveryResetSGX(psDeviceNode, 0, KERNEL_ID);
+	}
+}
+#endif
+
+
+#if defined(SYS_USING_INTERRUPTS)
+
+IMG_BOOL SGX_ISRHandler (IMG_VOID *pvData)
+{
+	IMG_BOOL bInterruptProcessed = IMG_FALSE;
+
+
+
+	{
+		IMG_UINT32 ui32EventStatus, ui32EventEnable;
+		IMG_UINT32 ui32EventClear = 0;
+		PVRSRV_DEVICE_NODE *psDeviceNode;
+		PVRSRV_SGXDEV_INFO *psDevInfo;
+
+
+		if(pvData == IMG_NULL)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "SGX_ISRHandler: Invalid params\n"));
+			return bInterruptProcessed;
+		}
+
+		psDeviceNode = (PVRSRV_DEVICE_NODE *)pvData;
+		psDevInfo = (PVRSRV_SGXDEV_INFO *)psDeviceNode->pvDevice;
+
+		ui32EventStatus = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_STATUS);
+		ui32EventEnable = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_HOST_ENABLE);
+
+
+
+		gui32EventStatusServicesByISR = ui32EventStatus;
+
+
+		ui32EventStatus &= ui32EventEnable;
+
+		if (ui32EventStatus & EUR_CR_EVENT_STATUS_SW_EVENT_MASK)
+		{
+			ui32EventClear |= EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_MASK;
+		}
+
+		if (ui32EventClear)
+		{
+			bInterruptProcessed = IMG_TRUE;
+
+
+			ui32EventClear |= EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_MASK;
+
+
+			OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_HOST_CLEAR, ui32EventClear);
+		}
+	}
+
+	return bInterruptProcessed;
+}
+
+
+IMG_VOID SGX_MISRHandler (IMG_VOID *pvData)
+{
+	PVRSRV_DEVICE_NODE	*psDeviceNode = (PVRSRV_DEVICE_NODE *)pvData;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = (PVRSRV_SGXDEV_INFO*)psDeviceNode->pvDevice;
+	SGXMKIF_HOST_CTL	*psSGXHostCtl = (SGXMKIF_HOST_CTL *)psDevInfo->psSGXHostCtl;
+
+	if (((psSGXHostCtl->ui32InterruptFlags & PVRSRV_USSE_EDM_INTERRUPT_HWR) != 0UL) &&
+		((psSGXHostCtl->ui32InterruptClearFlags & PVRSRV_USSE_EDM_INTERRUPT_HWR) == 0UL))
+	{
+		PVR_LOG(("HWRecoveryResetSGX: From ISR"));
+		HWRecoveryResetSGX(psDeviceNode, 0, ISR_ID);
+	}
+
+#if defined(OS_SUPPORTS_IN_LISR)
+	if (psDeviceNode->bReProcessDeviceCommandComplete)
+	{
+		SGXScheduleProcessQueuesKM(psDeviceNode);
+	}
+#endif
+
+	SGXTestActivePowerEvent(psDeviceNode, ISR_ID);
+}
+#endif
+
+
+PVRSRV_ERROR SGXRegisterDevice (PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	DEVICE_MEMORY_INFO *psDevMemoryInfo;
+	DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+
+
+	psDeviceNode->sDevId.eDeviceType	= DEV_DEVICE_TYPE;
+	psDeviceNode->sDevId.eDeviceClass	= DEV_DEVICE_CLASS;
+
+	psDeviceNode->pfnInitDevice		= DevInitSGXPart1;
+	psDeviceNode->pfnDeInitDevice		= DevDeInitSGX;
+
+	psDeviceNode->pfnInitDeviceCompatCheck	= SGXDevInitCompatCheck;
+
+
+
+	psDeviceNode->pfnMMUInitialise = MMU_Initialise;
+	psDeviceNode->pfnMMUFinalise = MMU_Finalise;
+	psDeviceNode->pfnMMUInsertHeap = MMU_InsertHeap;
+	psDeviceNode->pfnMMUCreate = MMU_Create;
+	psDeviceNode->pfnMMUDelete = MMU_Delete;
+	psDeviceNode->pfnMMUAlloc = MMU_Alloc;
+	psDeviceNode->pfnMMUFree = MMU_Free;
+	psDeviceNode->pfnMMUMapPages = MMU_MapPages;
+	psDeviceNode->pfnMMUMapShadow = MMU_MapShadow;
+	psDeviceNode->pfnMMUUnmapPages = MMU_UnmapPages;
+	psDeviceNode->pfnMMUMapScatter = MMU_MapScatter;
+	psDeviceNode->pfnMMUGetPhysPageAddr = MMU_GetPhysPageAddr;
+	psDeviceNode->pfnMMUGetPDDevPAddr = MMU_GetPDDevPAddr;
+
+#if defined (SYS_USING_INTERRUPTS)
+
+
+	psDeviceNode->pfnDeviceISR = SGX_ISRHandler;
+	psDeviceNode->pfnDeviceMISR = SGX_MISRHandler;
+#endif
+
+
+
+	psDeviceNode->pfnDeviceCommandComplete = SGXCommandComplete;
+
+
+
+	psDevMemoryInfo = &psDeviceNode->sDevMemoryInfo;
+
+	psDevMemoryInfo->ui32AddressSpaceSizeLog2 = SGX_FEATURE_ADDRESS_SPACE_SIZE;
+
+
+	psDevMemoryInfo->ui32Flags = 0;
+
+
+	if(OSAllocMem( PVRSRV_OS_PAGEABLE_HEAP,
+					 sizeof(DEVICE_MEMORY_HEAP_INFO) * SGX_MAX_HEAP_ID,
+					 (IMG_VOID **)&psDevMemoryInfo->psDeviceMemoryHeap, 0,
+					 "Array of Device Memory Heap Info") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXRegisterDevice : Failed to alloc memory for DEVICE_MEMORY_HEAP_INFO"));
+		return (PVRSRV_ERROR_OUT_OF_MEMORY);
+	}
+	OSMemSet(psDevMemoryInfo->psDeviceMemoryHeap, 0, sizeof(DEVICE_MEMORY_HEAP_INFO) * SGX_MAX_HEAP_ID);
+
+	psDeviceMemoryHeap = psDevMemoryInfo->psDeviceMemoryHeap;
+
+
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_GENERAL_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_GENERAL_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_GENERAL_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+														| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+														| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "General";
+	psDeviceMemoryHeap->pszBSName = "General BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+#if !defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+
+	psDevMemoryInfo->ui32MappingHeapID = (IMG_UINT32)(psDeviceMemoryHeap - psDevMemoryInfo->psDeviceMemoryHeap);
+#endif
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_TADATA_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_TADATA_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_TADATA_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+														| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+														| PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->pszName = "TA Data";
+	psDeviceMemoryHeap->pszBSName = "TA Data BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_KERNEL_CODE_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_KERNEL_CODE_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_KERNEL_CODE_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+															| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+															| PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->pszName = "Kernel Code";
+	psDeviceMemoryHeap->pszBSName = "Kernel Code BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_KERNEL_DATA_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_KERNEL_DATA_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_KERNEL_DATA_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+																| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+																| PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->pszName = "KernelData";
+	psDeviceMemoryHeap->pszBSName = "KernelData BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_PIXELSHADER_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_PIXELSHADER_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_PIXELSHADER_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+																| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+																| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "PixelShaderUSSE";
+	psDeviceMemoryHeap->pszBSName = "PixelShaderUSSE BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_VERTEXSHADER_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_VERTEXSHADER_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_VERTEXSHADER_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+																| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+																| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "VertexShaderUSSE";
+	psDeviceMemoryHeap->pszBSName = "VertexShaderUSSE BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_PDSPIXEL_CODEDATA_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_PDSPIXEL_CODEDATA_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_PDSPIXEL_CODEDATA_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+																| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+																| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "PDSPixelCodeData";
+	psDeviceMemoryHeap->pszBSName = "PDSPixelCodeData BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_PDSVERTEX_CODEDATA_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_PDSVERTEX_CODEDATA_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_PDSVERTEX_CODEDATA_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+																| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+																| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "PDSVertexCodeData";
+	psDeviceMemoryHeap->pszBSName = "PDSVertexCodeData BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_SYNCINFO_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_SYNCINFO_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_SYNCINFO_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+														| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+														| PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->pszName = "CacheCoherent";
+	psDeviceMemoryHeap->pszBSName = "CacheCoherent BS";
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+
+	psDevMemoryInfo->ui32SyncHeapID = (IMG_UINT32)(psDeviceMemoryHeap - psDevMemoryInfo->psDeviceMemoryHeap);
+	psDeviceMemoryHeap++;
+
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_3DPARAMETERS_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_3DPARAMETERS_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_3DPARAMETERS_HEAP_SIZE;
+	psDeviceMemoryHeap->pszName = "3DParameters";
+	psDeviceMemoryHeap->pszBSName = "3DParameters BS";
+#if defined(SUPPORT_PERCONTEXT_PB)
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+															| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+															| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+#else
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+													| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+													| PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+#endif
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+
+
+#if defined(SUPPORT_SGX_GENERAL_MAPPING_HEAP)
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_GENERAL_MAPPING_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_GENERAL_MAPPING_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_GENERAL_MAPPING_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->pszName = "GeneralMapping";
+	psDeviceMemoryHeap->pszBSName = "GeneralMapping BS";
+	#if defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS) && defined(FIX_HW_BRN_23410)
+
+
+
+
+
+
+
+		psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+#else
+		psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+#endif
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+
+	psDevMemoryInfo->ui32MappingHeapID = (IMG_UINT32)(psDeviceMemoryHeap - psDevMemoryInfo->psDeviceMemoryHeap);
+	psDeviceMemoryHeap++;
+#endif
+
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_2D_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_2D_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_2D_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+														| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+														| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "2D";
+	psDeviceMemoryHeap->pszBSName = "2D BS";
+
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+#endif
+
+
+#if defined(FIX_HW_BRN_26915)
+
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_CGBUFFER_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_CGBUFFER_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_CGBUFFER_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+														| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+														| PVRSRV_HAP_SINGLE_PROCESS;
+	psDeviceMemoryHeap->pszName = "CGBuffer";
+	psDeviceMemoryHeap->pszBSName = "CGBuffer BS";
+
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_PERCONTEXT;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+#endif
+
+#if defined(SUPPORT_SGX_VIDEO_HEAP)
+
+	psDeviceMemoryHeap->ui32HeapID = HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_VIDEO_HEAP_ID);
+	psDeviceMemoryHeap->sDevVAddrBase.uiAddr = SGX_VIDEO_HEAP_BASE;
+	psDeviceMemoryHeap->ui32HeapSize = SGX_VIDEO_HEAP_SIZE;
+	psDeviceMemoryHeap->ui32Attribs = PVRSRV_HAP_WRITECOMBINE
+										| PVRSRV_MEM_RAM_BACKED_ALLOCATION
+										| PVRSRV_HAP_MULTI_PROCESS;
+	psDeviceMemoryHeap->pszName = "Video";
+	psDeviceMemoryHeap->pszBSName = "Video BS";
+
+	psDeviceMemoryHeap->DevMemHeapType = DEVICE_MEMORY_HEAP_SHARED_EXPORTED;
+
+	psDeviceMemoryHeap->ui32DataPageSize = SGX_MMU_PAGE_SIZE;
+	psDeviceMemoryHeap++;
+#endif
+
+
+	psDevMemoryInfo->ui32HeapCount = (IMG_UINT32)(psDeviceMemoryHeap - psDevMemoryInfo->psDeviceMemoryHeap);
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGXGetClientInfoKM(IMG_HANDLE					hDevCookie,
+								SGX_CLIENT_INFO*		psClientInfo)
+{
+	PVRSRV_SGXDEV_INFO *psDevInfo = (PVRSRV_SGXDEV_INFO *)((PVRSRV_DEVICE_NODE *)hDevCookie)->pvDevice;
+
+
+
+	psDevInfo->ui32ClientRefCount++;
+
+#if defined(PDUMP)
+
+	psDevInfo->psKernelCCBInfo->ui32CCBDumpWOff = 0;
+#endif
+
+
+	psClientInfo->ui32ProcessID = OSGetCurrentProcessIDKM();
+
+
+
+	OSMemCopy(&psClientInfo->asDevData, &psDevInfo->asSGXDevData, sizeof(psClientInfo->asDevData));
+
+
+	return PVRSRV_OK;
+}
+
+
+IMG_VOID SGXPanic(PVRSRV_DEVICE_NODE	*psDeviceNode)
+{
+	PVR_LOG(("SGX panic"));
+	SGXDumpDebugInfo(psDeviceNode, IMG_FALSE);
+	OSPanic();
+}
+
+
+PVRSRV_ERROR SGXDevInitCompatCheck(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	PVRSRV_ERROR	eError;
+	PVRSRV_SGXDEV_INFO 				*psDevInfo;
+	IMG_UINT32 			ui32BuildOptions, ui32BuildOptionsMismatch;
+#if !defined(NO_HARDWARE)
+	PPVRSRV_KERNEL_MEM_INFO			psMemInfo;
+	PVRSRV_SGX_MISCINFO_INFO		*psSGXMiscInfoInt;
+	PVRSRV_SGX_MISCINFO_FEATURES	*psSGXFeatures;
+	SGX_MISCINFO_STRUCT_SIZES		*psSGXStructSizes;
+	IMG_BOOL						bStructSizesFailed;
+
+
+	IMG_BOOL	bCheckCoreRev;
+	const IMG_UINT32	aui32CoreRevExceptions[] =
+		{
+			0x10100, 0x10101
+		};
+	const IMG_UINT32	ui32NumCoreExceptions = sizeof(aui32CoreRevExceptions) / (2*sizeof(IMG_UINT32));
+	IMG_UINT	i;
+#endif
+
+
+	if(psDeviceNode->sDevId.eDeviceType != PVRSRV_DEVICE_TYPE_SGX)
+	{
+		PVR_LOG(("(FAIL) SGXInit: Device not of type SGX"));
+		eError = PVRSRV_ERROR_INVALID_PARAMS;
+		goto chk_exit;
+	}
+
+	psDevInfo = psDeviceNode->pvDevice;
+
+
+
+	ui32BuildOptions = (SGX_BUILD_OPTIONS);
+#if defined(DEBUG) || defined (INTERNAL_TEST)
+	/* Workaround: During development, the DEBUG bit can get out of sync, so
+	 * ignore that bit:
+	 */
+	if (0x1 & ui32BuildOptions) {
+		psDevInfo->ui32ClientBuildOptions =
+			psDevInfo->ui32ClientBuildOptions | 0x1;
+	} else {
+		psDevInfo->ui32ClientBuildOptions =
+			psDevInfo->ui32ClientBuildOptions & 0xfffffffe;
+	}
+#endif
+	if (ui32BuildOptions != psDevInfo->ui32ClientBuildOptions)
+	{
+		ui32BuildOptionsMismatch = ui32BuildOptions ^ psDevInfo->ui32ClientBuildOptions;
+		if ( (psDevInfo->ui32ClientBuildOptions & ui32BuildOptionsMismatch) != 0)
+		{
+			PVR_LOG(("(FAIL) SGXInit: Mismatch in client-side and KM driver build options; "
+				"extra options present in client-side driver: (0x%lx). Please check sgx_options.h",
+				psDevInfo->ui32ClientBuildOptions & ui32BuildOptionsMismatch ));
+		}
+
+		if ( (ui32BuildOptions & ui32BuildOptionsMismatch) != 0)
+		{
+			PVR_LOG(("(FAIL) SGXInit: Mismatch in client-side and KM driver build options; "
+				"extra options present in KM: (0x%lx). Please check sgx_options.h",
+				ui32BuildOptions & ui32BuildOptionsMismatch ));
+		}
+		eError = PVRSRV_ERROR_BUILD_MISMATCH;
+		goto chk_exit;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_MESSAGE, "SGXInit: Client-side and KM driver build options match. [ OK ]"));
+	}
+
+#if !defined (NO_HARDWARE)
+	psMemInfo = psDevInfo->psKernelSGXMiscMemInfo;
+
+
+	psSGXMiscInfoInt = psMemInfo->pvLinAddrKM;
+	psSGXMiscInfoInt->ui32MiscInfoFlags = 0;
+	psSGXMiscInfoInt->ui32MiscInfoFlags |= PVRSRV_USSE_MISCINFO_GET_STRUCT_SIZES;
+	eError = SGXGetMiscInfoUkernel(psDevInfo, psDeviceNode);
+
+
+	if(eError != PVRSRV_OK)
+	{
+		PVR_LOG(("(FAIL) SGXInit: Unable to validate device DDK version"));
+		goto chk_exit;
+	}
+	psSGXFeatures = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->sSGXFeatures;
+	if( (psSGXFeatures->ui32DDKVersion !=
+		((PVRVERSION_MAJ << 16) |
+		 (PVRVERSION_MIN << 8) |
+		  PVRVERSION_BRANCH) ) ||
+		(psSGXFeatures->ui32DDKBuild != PVRVERSION_BUILD) )
+	{
+		PVR_LOG(("(FAIL) SGXInit: Incompatible driver DDK revision (%ld)/device DDK revision (%ld).",
+				PVRVERSION_BUILD, psSGXFeatures->ui32DDKBuild));
+		eError = PVRSRV_ERROR_DDK_VERSION_MISMATCH;
+		PVR_DBG_BREAK;
+		goto chk_exit;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_MESSAGE, "SGXInit: driver DDK (%ld) and device DDK (%ld) match. [ OK ]",
+				PVRVERSION_BUILD, psSGXFeatures->ui32DDKBuild));
+	}
+
+
+	if (psSGXFeatures->ui32CoreRevSW == 0)
+	{
+
+
+		PVR_LOG(("SGXInit: HW core rev (%lx) check skipped.",
+				psSGXFeatures->ui32CoreRev));
+	}
+	else
+	{
+
+		bCheckCoreRev = IMG_TRUE;
+		for(i=0; i<ui32NumCoreExceptions; i+=2)
+		{
+			if( (psSGXFeatures->ui32CoreRev==aui32CoreRevExceptions[i]) &&
+				(psSGXFeatures->ui32CoreRevSW==aui32CoreRevExceptions[i+1])	)
+			{
+				PVR_LOG(("SGXInit: HW core rev (%lx), SW core rev (%lx) check skipped.",
+						psSGXFeatures->ui32CoreRev,
+						psSGXFeatures->ui32CoreRevSW));
+				bCheckCoreRev = IMG_FALSE;
+			}
+		}
+
+		if (bCheckCoreRev)
+		{
+			if (psSGXFeatures->ui32CoreRev != psSGXFeatures->ui32CoreRevSW)
+			{
+				PVR_LOG(("(FAIL) SGXInit: Incompatible HW core rev (%lx) and SW core rev (%lx).",
+						psSGXFeatures->ui32CoreRev, psSGXFeatures->ui32CoreRevSW));
+						eError = PVRSRV_ERROR_BUILD_MISMATCH;
+						goto chk_exit;
+			}
+			else
+			{
+				PVR_DPF((PVR_DBG_MESSAGE, "SGXInit: HW core rev (%lx) and SW core rev (%lx) match. [ OK ]",
+						psSGXFeatures->ui32CoreRev, psSGXFeatures->ui32CoreRevSW));
+			}
+		}
+	}
+
+
+	psSGXStructSizes = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->sSGXStructSizes;
+
+	bStructSizesFailed = IMG_FALSE;
+
+	CHECK_SIZE(HOST_CTL);
+	CHECK_SIZE(COMMAND);
+#if defined(SGX_FEATURE_2D_HARDWARE)
+	CHECK_SIZE(2DCMD);
+	CHECK_SIZE(2DCMD_SHARED);
+#endif
+	CHECK_SIZE(CMDTA);
+	CHECK_SIZE(CMDTA_SHARED);
+	CHECK_SIZE(TRANSFERCMD);
+	CHECK_SIZE(TRANSFERCMD_SHARED);
+
+	CHECK_SIZE(3DREGISTERS);
+	CHECK_SIZE(HWPBDESC);
+	CHECK_SIZE(HWRENDERCONTEXT);
+	CHECK_SIZE(HWRENDERDETAILS);
+	CHECK_SIZE(HWRTDATA);
+	CHECK_SIZE(HWRTDATASET);
+	CHECK_SIZE(HWTRANSFERCONTEXT);
+
+	if (bStructSizesFailed == IMG_TRUE)
+	{
+		PVR_LOG(("(FAIL) SGXInit: Mismatch in SGXMKIF structure sizes."));
+		eError = PVRSRV_ERROR_BUILD_MISMATCH;
+		goto chk_exit;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_MESSAGE, "SGXInit: SGXMKIF structure sizes match. [ OK ]"));
+	}
+
+
+#if defined(DEBUG) || defined (INTERNAL_TEST)
+	/* Workaround: During development, the DEBUG bit can get out of sync, so
+	 * ignore that bit:
+	 */
+	if (0x1 & (SGX_BUILD_OPTIONS)) {
+		ui32BuildOptions = psSGXFeatures->ui32BuildOptions | 0x1;
+	} else {
+		ui32BuildOptions = psSGXFeatures->ui32BuildOptions & 0xfffffffe;
+	}
+#else
+	ui32BuildOptions = psSGXFeatures->ui32BuildOptions;
+#endif
+	if (ui32BuildOptions != (SGX_BUILD_OPTIONS))
+	{
+		ui32BuildOptionsMismatch = ui32BuildOptions ^ (SGX_BUILD_OPTIONS);
+		if ( ((SGX_BUILD_OPTIONS) & ui32BuildOptionsMismatch) != 0)
+		{
+			PVR_LOG(("(FAIL) SGXInit: Mismatch in driver and microkernel build options; "
+				"extra options present in driver: (0x%lx). Please check sgx_options.h",
+				(SGX_BUILD_OPTIONS) & ui32BuildOptionsMismatch ));
+		}
+
+		if ( (ui32BuildOptions & ui32BuildOptionsMismatch) != 0)
+		{
+			PVR_LOG(("(FAIL) SGXInit: Mismatch in driver and microkernel build options; "
+				"extra options present in microkernel: (0x%lx). Please check sgx_options.h",
+				ui32BuildOptions & ui32BuildOptionsMismatch ));
+		}
+		eError = PVRSRV_ERROR_BUILD_MISMATCH;
+		goto chk_exit;
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_MESSAGE, "SGXInit: Driver and microkernel build options match. [ OK ]"));
+	}
+#endif
+
+	eError = PVRSRV_OK;
+chk_exit:
+#if defined(IGNORE_SGX_INIT_COMPATIBILITY_CHECK)
+	return PVRSRV_OK;
+#else
+	return eError;
+#endif
+}
+
+static
+PVRSRV_ERROR SGXGetMiscInfoUkernel(PVRSRV_SGXDEV_INFO	*psDevInfo,
+								   PVRSRV_DEVICE_NODE 	*psDeviceNode)
+{
+	PVRSRV_ERROR		eError;
+	SGXMKIF_COMMAND		sCommandData;
+	PVRSRV_SGX_MISCINFO_INFO			*psSGXMiscInfoInt;
+	PVRSRV_SGX_MISCINFO_FEATURES		*psSGXFeatures;
+	SGX_MISCINFO_STRUCT_SIZES			*psSGXStructSizes;
+
+	PPVRSRV_KERNEL_MEM_INFO	psMemInfo = psDevInfo->psKernelSGXMiscMemInfo;
+
+	if (! psMemInfo->pvLinAddrKM)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXGetMiscInfoUkernel: Invalid address."));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+	psSGXMiscInfoInt = psMemInfo->pvLinAddrKM;
+	psSGXFeatures = &psSGXMiscInfoInt->sSGXFeatures;
+	psSGXStructSizes = &psSGXMiscInfoInt->sSGXStructSizes;
+
+	psSGXMiscInfoInt->ui32MiscInfoFlags &= ~PVRSRV_USSE_MISCINFO_READY;
+
+
+	OSMemSet(psSGXFeatures, 0, sizeof(*psSGXFeatures));
+	OSMemSet(psSGXStructSizes, 0, sizeof(*psSGXStructSizes));
+
+
+	sCommandData.ui32Data[1] = psMemInfo->sDevVAddr.uiAddr;
+
+	eError = SGXScheduleCCBCommandKM(psDeviceNode,
+									 SGXMKIF_CMD_GETMISCINFO,
+									 &sCommandData,
+									 KERNEL_ID,
+									 0);
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXGetMiscInfoUkernel: SGXScheduleCCBCommandKM failed."));
+		return eError;
+	}
+
+
+#if !defined(NO_HARDWARE)
+	{
+		IMG_BOOL bExit;
+
+		bExit = IMG_FALSE;
+		LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+		{
+			if ((psSGXMiscInfoInt->ui32MiscInfoFlags & PVRSRV_USSE_MISCINFO_READY) != 0)
+			{
+				bExit = IMG_TRUE;
+				break;
+			}
+		} END_LOOP_UNTIL_TIMEOUT();
+
+
+		if (!bExit)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "SGXGetMiscInfoUkernel: Timeout occurred waiting for misc info."));
+			return PVRSRV_ERROR_TIMEOUT;
+		}
+	}
+#endif
+
+	return PVRSRV_OK;
+}
+
+
+
+IMG_EXPORT
+PVRSRV_ERROR SGXGetMiscInfoKM(PVRSRV_SGXDEV_INFO	*psDevInfo,
+							  SGX_MISC_INFO			*psMiscInfo,
+							  PVRSRV_DEVICE_NODE 	*psDeviceNode,
+							  IMG_HANDLE 			 hDevMemContext)
+{
+	PPVRSRV_KERNEL_MEM_INFO	psMemInfo = psDevInfo->psKernelSGXMiscMemInfo;
+	IMG_UINT32	*pui32MiscInfoFlags = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->ui32MiscInfoFlags;
+
+
+	*pui32MiscInfoFlags = 0;
+
+#if !defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+	PVR_UNREFERENCED_PARAMETER(hDevMemContext);
+#endif
+
+	switch(psMiscInfo->eRequest)
+	{
+#if defined(SGX_FEATURE_DATA_BREAKPOINTS)
+		case SGX_MISC_INFO_REQUEST_SET_BREAKPOINT:
+		{
+			IMG_UINT32 ui32RegOffset;
+			IMG_UINT32 ui32RegVal;
+			IMG_UINT32 ui32BaseRegOffset;
+			IMG_UINT32 ui32BaseRegVal;
+			IMG_UINT32 ui32MaskRegOffset;
+			IMG_UINT32 ui32MaskRegVal;
+
+			switch(psMiscInfo->uData.sSGXBreakpointInfo.ui32BPIndex)
+			{
+				case 0:
+					ui32RegOffset = EUR_CR_BREAKPOINT0;
+					ui32BaseRegOffset = EUR_CR_BREAKPOINT0_BASE;
+					ui32MaskRegOffset = EUR_CR_BREAKPOINT0_MASK;
+					break;
+				case 1:
+					ui32RegOffset = EUR_CR_BREAKPOINT1;
+					ui32BaseRegOffset = EUR_CR_BREAKPOINT1_BASE;
+					ui32MaskRegOffset = EUR_CR_BREAKPOINT1_MASK;
+					break;
+				case 2:
+					ui32RegOffset = EUR_CR_BREAKPOINT2;
+					ui32BaseRegOffset = EUR_CR_BREAKPOINT2_BASE;
+					ui32MaskRegOffset = EUR_CR_BREAKPOINT2_MASK;
+					break;
+				case 3:
+					ui32RegOffset = EUR_CR_BREAKPOINT3;
+					ui32BaseRegOffset = EUR_CR_BREAKPOINT3_BASE;
+					ui32MaskRegOffset = EUR_CR_BREAKPOINT3_MASK;
+					break;
+				default:
+					PVR_DPF((PVR_DBG_ERROR,"SGXGetMiscInfoKM: SGX_MISC_INFO_REQUEST_SET_BREAKPOINT invalid BP idx %d", psMiscInfo->uData.sSGXBreakpointInfo.ui32BPIndex));
+					return PVRSRV_ERROR_INVALID_PARAMS;
+			}
+
+
+			if(psMiscInfo->uData.sSGXBreakpointInfo.bBPEnable)
+			{
+
+				IMG_DEV_VIRTADDR sBPDevVAddr = psMiscInfo->uData.sSGXBreakpointInfo.sBPDevVAddr;
+
+
+				ui32MaskRegVal = EUR_CR_BREAKPOINT0_MASK_REGION_MASK | EUR_CR_BREAKPOINT0_MASK_DM_MASK;
+
+
+				ui32BaseRegVal = sBPDevVAddr.uiAddr & EUR_CR_BREAKPOINT0_BASE_ADDRESS_MASK;
+
+
+				ui32RegVal =	EUR_CR_BREAKPOINT0_CTRL_WENABLE_MASK
+							|	EUR_CR_BREAKPOINT0_CTRL_WENABLE_MASK
+							|	EUR_CR_BREAKPOINT0_CTRL_TRAPENABLE_MASK;
+			}
+			else
+			{
+
+				ui32RegVal = ui32BaseRegVal = ui32MaskRegVal = 0;
+			}
+
+
+
+
+
+
+
+
+
+
+			return PVRSRV_OK;
+		}
+#endif
+
+		case SGX_MISC_INFO_REQUEST_CLOCKSPEED:
+		{
+			psMiscInfo->uData.ui32SGXClockSpeed = psDevInfo->ui32CoreClockSpeed;
+			return PVRSRV_OK;
+		}
+
+		case SGX_MISC_INFO_REQUEST_SGXREV:
+		{
+			PVRSRV_ERROR eError;
+			PVRSRV_SGX_MISCINFO_FEATURES		*psSGXFeatures;
+
+			eError = SGXGetMiscInfoUkernel(psDevInfo, psDeviceNode);
+			if(eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "An error occurred in SGXGetMiscInfoUkernel: %d\n",
+						eError));
+				return eError;
+			}
+			psSGXFeatures = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->sSGXFeatures;
+
+
+			psMiscInfo->uData.sSGXFeatures = *psSGXFeatures;
+
+
+			PVR_DPF((PVR_DBG_MESSAGE, "SGXGetMiscInfoKM: Core 0x%lx, sw ID 0x%lx, sw Rev 0x%lx\n",
+					psSGXFeatures->ui32CoreRev,
+					psSGXFeatures->ui32CoreIdSW,
+					psSGXFeatures->ui32CoreRevSW));
+			PVR_DPF((PVR_DBG_MESSAGE, "SGXGetMiscInfoKM: DDK version 0x%lx, DDK build 0x%lx\n",
+					psSGXFeatures->ui32DDKVersion,
+					psSGXFeatures->ui32DDKBuild));
+
+
+			return PVRSRV_OK;
+		}
+
+		case SGX_MISC_INFO_REQUEST_DRIVER_SGXREV:
+		{
+			PVRSRV_SGX_MISCINFO_FEATURES		*psSGXFeatures;
+
+			psSGXFeatures = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->sSGXFeatures;
+
+
+			OSMemSet(psMemInfo->pvLinAddrKM, 0,
+					sizeof(PVRSRV_SGX_MISCINFO_INFO));
+
+			psSGXFeatures->ui32DDKVersion =
+				(PVRVERSION_MAJ << 16) |
+				(PVRVERSION_MIN << 8) |
+				PVRVERSION_BRANCH;
+			psSGXFeatures->ui32DDKBuild = PVRVERSION_BUILD;
+
+
+			psSGXFeatures->ui32BuildOptions = (SGX_BUILD_OPTIONS);
+
+
+			psMiscInfo->uData.sSGXFeatures = *psSGXFeatures;
+			return PVRSRV_OK;
+		}
+
+#if defined(SUPPORT_SGX_EDM_MEMORY_DEBUG)
+		case SGX_MISC_INFO_REQUEST_MEMREAD:
+		{
+			PVRSRV_ERROR eError;
+			PPVRSRV_KERNEL_MEM_INFO	psMemInfo = psDevInfo->psKernelSGXMiscMemInfo;
+			PVRSRV_SGX_MISCINFO_FEATURES		*psSGXFeatures;
+			PVRSRV_SGX_MISCINFO_MEMREAD			*psSGXMemReadData;
+
+			psSGXMemReadData = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->sSGXMemReadData;
+
+
+			*pui32MiscInfoFlags |= PVRSRV_USSE_MISCINFO_MEMREAD;
+
+
+			if(psMiscInfo->hDevMemContext != IMG_NULL)
+			{
+				SGXGetMMUPDAddrKM( (IMG_HANDLE)psDeviceNode, hDevMemContext, &psSGXMemReadData->sPDDevPAddr);
+			}
+			else
+			{
+				return PVRSRV_ERROR_INVALID_PARAMS;
+			}
+
+
+			if(psMiscInfo->sDevVAddr.uiAddr != 0)
+			{
+				psSGXMemReadData->sDevVAddr = psMiscInfo->sDevVAddr;
+			}
+			else
+			{
+				return PVRSRV_ERROR_INVALID_PARAMS;
+			}
+
+
+			eError = SGXGetMiscInfoUkernel(psDevInfo, psDeviceNode);
+			if(eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "An error occurred in SGXGetMiscInfoUkernel: %d\n",
+						eError));
+				return eError;
+			}
+			psSGXFeatures = &((PVRSRV_SGX_MISCINFO_INFO*)(psMemInfo->pvLinAddrKM))->sSGXFeatures;
+
+#if !defined SGX_FEATURE_MULTIPLE_MEM_CONTEXTS
+			if(*pui32MiscInfoFlags & PVRSRV_USSE_MISCINFO_MEMREAD_FAIL)
+			{
+				return PVRSRV_ERROR_GENERIC;
+			}
+#endif
+
+			psMiscInfo->uData.sSGXFeatures = *psSGXFeatures;
+			return PVRSRV_OK;
+		}
+#endif
+
+#ifdef SUPPORT_SGX_HWPERF
+		case SGX_MISC_INFO_REQUEST_SET_HWPERF_STATUS:
+		{
+			SGXMKIF_HWPERF_CB *psHWPerfCB = psDevInfo->psKernelHWPerfCBMemInfo->pvLinAddrKM;
+			IMG_UINT ui32MatchingFlags;
+
+
+			if ((psMiscInfo->uData.ui32NewHWPerfStatus & ~(PVRSRV_SGX_HWPERF_GRAPHICS_ON | PVRSRV_SGX_HWPERF_MK_EXECUTION_ON)) != 0)
+			{
+				return PVRSRV_ERROR_INVALID_PARAMS;
+			}
+
+
+			ui32MatchingFlags = psMiscInfo->uData.ui32NewHWPerfStatus & psDevInfo->psSGXHostCtl->ui32HWPerfFlags;
+			if((ui32MatchingFlags & PVRSRV_SGX_HWPERF_GRAPHICS_ON) == 0UL)
+			{
+				psHWPerfCB->ui32OrdinalGRAPHICS = 0xffffffff;
+			}
+			if((ui32MatchingFlags & PVRSRV_SGX_HWPERF_MK_EXECUTION_ON) == 0UL)
+			{
+				psHWPerfCB->ui32OrdinalMK_EXECUTION = 0xffffffffUL;
+			}
+
+
+			psDevInfo->psSGXHostCtl->ui32HWPerfFlags = psMiscInfo->uData.ui32NewHWPerfStatus;
+			#if defined(PDUMP)
+			PDUMPCOMMENTWITHFLAGS(PDUMP_FLAGS_CONTINUOUS, "SGX ukernel HWPerf status %lu\n",
+								  psDevInfo->psSGXHostCtl->ui32HWPerfFlags);
+			PDUMPMEM(IMG_NULL, psDevInfo->psKernelSGXHostCtlMemInfo,
+					 offsetof(SGXMKIF_HOST_CTL, ui32HWPerfFlags),
+					 sizeof(psDevInfo->psSGXHostCtl->ui32HWPerfFlags), PDUMP_FLAGS_CONTINUOUS,
+					 MAKEUNIQUETAG(psDevInfo->psKernelSGXHostCtlMemInfo));
+			#endif
+
+			return PVRSRV_OK;
+		}
+		case SGX_MISC_INFO_REQUEST_HWPERF_CB_ON:
+		{
+
+			SGXMKIF_HWPERF_CB *psHWPerfCB = psDevInfo->psKernelHWPerfCBMemInfo->pvLinAddrKM;
+			psHWPerfCB->ui32OrdinalGRAPHICS = 0xffffffffUL;
+
+			psDevInfo->psSGXHostCtl->ui32HWPerfFlags |= PVRSRV_SGX_HWPERF_GRAPHICS_ON;
+			return PVRSRV_OK;
+		}
+		case SGX_MISC_INFO_REQUEST_HWPERF_CB_OFF:
+		{
+
+			psDevInfo->psSGXHostCtl->ui32HWPerfFlags = 0;
+			return PVRSRV_OK;
+		}
+		case SGX_MISC_INFO_REQUEST_HWPERF_RETRIEVE_CB:
+		{
+
+			SGX_MISC_INFO_HWPERF_RETRIEVE_CB *psRetrieve = &psMiscInfo->uData.sRetrieveCB;
+			SGXMKIF_HWPERF_CB *psHWPerfCB = psDevInfo->psKernelHWPerfCBMemInfo->pvLinAddrKM;
+			IMG_UINT i;
+
+			for (i = 0; psHWPerfCB->ui32Woff != psHWPerfCB->ui32Roff && i < psRetrieve->ui32ArraySize; i++)
+			{
+				SGXMKIF_HWPERF_CB_ENTRY *psData = &psHWPerfCB->psHWPerfCBData[psHWPerfCB->ui32Roff];
+
+
+
+				psRetrieve->psHWPerfData[i].ui32FrameNo = psData->ui32FrameNo;
+				psRetrieve->psHWPerfData[i].ui32Type = (psData->ui32Type & PVRSRV_SGX_HWPERF_TYPE_OP_MASK);
+				psRetrieve->psHWPerfData[i].ui32StartTime = psData->ui32Time;
+				psRetrieve->psHWPerfData[i].ui32StartTimeWraps = psData->ui32TimeWraps;
+				psRetrieve->psHWPerfData[i].ui32EndTime = psData->ui32Time;
+				psRetrieve->psHWPerfData[i].ui32EndTimeWraps = psData->ui32TimeWraps;
+				psRetrieve->psHWPerfData[i].ui32ClockSpeed = psDevInfo->ui32CoreClockSpeed;
+				psRetrieve->psHWPerfData[i].ui32TimeMax = psDevInfo->ui32uKernelTimerClock;
+				psHWPerfCB->ui32Roff = (psHWPerfCB->ui32Roff + 1) & (SGXMKIF_HWPERF_CB_SIZE - 1);
+			}
+			psRetrieve->ui32DataCount = i;
+			psRetrieve->ui32Time = OSClockus();
+			return PVRSRV_OK;
+		}
+#endif
+		case SGX_MISC_INFO_DUMP_DEBUG_INFO:
+		{
+			PVR_LOG(("User requested SGX debug info"));
+
+
+			SGXDumpDebugInfo(psDeviceNode, IMG_FALSE);
+
+			return PVRSRV_OK;
+		}
+
+		case SGX_MISC_INFO_PANIC:
+		{
+			PVR_LOG(("User requested SGX panic"));
+
+			SGXPanic(psDeviceNode);
+
+			return PVRSRV_OK;
+		}
+
+		default:
+		{
+
+			return PVRSRV_ERROR_INVALID_PARAMS;
+		}
+	}
+}
+
+#if defined(SUPPORT_SGX_HWPERF)
+IMG_EXPORT
+PVRSRV_ERROR SGXReadDiffCountersKM(IMG_HANDLE				hDevHandle,
+								   IMG_UINT32				ui32Reg,
+								   IMG_UINT32				*pui32Old,
+								   IMG_BOOL					bNew,
+								   IMG_UINT32				ui32New,
+								   IMG_UINT32				ui32NewReset,
+								   IMG_UINT32				ui32CountersReg,
+								   IMG_UINT32				ui32Reg2,
+								   IMG_BOOL					*pbActive,
+								   PVRSRV_SGXDEV_DIFF_INFO	*psDiffs)
+{
+	PVRSRV_ERROR    	eError;
+	SYS_DATA			*psSysData;
+	PVRSRV_POWER_DEV	*psPowerDevice;
+	IMG_BOOL			bPowered = IMG_FALSE;
+	PVRSRV_DEVICE_NODE	*psDeviceNode = hDevHandle;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+
+
+	if(bNew)
+	{
+		psDevInfo->ui32HWGroupRequested = ui32New;
+	}
+	psDevInfo->ui32HWReset |= ui32NewReset;
+
+
+	eError = PVRSRVPowerLock(KERNEL_ID, IMG_FALSE);
+	if (eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+
+	SysAcquireData(&psSysData);
+
+
+	psPowerDevice = (PVRSRV_POWER_DEV*)
+				List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
+											MatchPowerDeviceIndex_AnyVaCb,
+											psDeviceNode->sDevId.ui32DeviceIndex);
+
+	if (psPowerDevice)
+	{
+		bPowered = (IMG_BOOL)(psPowerDevice->eCurrentPowerState == PVRSRV_DEV_POWER_STATE_ON);
+	}
+
+
+
+	*pbActive = bPowered;
+
+
+
+	{
+		IMG_UINT32 ui32rval = 0;
+
+
+		if(bPowered)
+		{
+			IMG_UINT32 i;
+
+
+			*pui32Old = OSReadHWReg(psDevInfo->pvRegsBaseKM, ui32Reg);
+
+			for (i = 0; i < PVRSRV_SGX_DIFF_NUM_COUNTERS; ++i)
+			{
+				psDiffs->aui32Counters[i] = OSReadHWReg(psDevInfo->pvRegsBaseKM, ui32CountersReg + (i * 4));
+			}
+
+			if(ui32Reg2)
+			{
+				ui32rval = OSReadHWReg(psDevInfo->pvRegsBaseKM, ui32Reg2);
+			}
+
+
+
+			if (psDevInfo->ui32HWGroupRequested != *pui32Old)
+			{
+
+				if(psDevInfo->ui32HWReset != 0)
+				{
+					OSWriteHWReg(psDevInfo->pvRegsBaseKM, ui32Reg, psDevInfo->ui32HWGroupRequested | psDevInfo->ui32HWReset);
+					psDevInfo->ui32HWReset = 0;
+				}
+
+				OSWriteHWReg(psDevInfo->pvRegsBaseKM, ui32Reg, psDevInfo->ui32HWGroupRequested);
+			}
+		}
+
+		psDiffs->ui32Time[0] = OSClockus();
+		psDiffs->ui32Time[1] = psDevInfo->psSGXHostCtl->ui32TimeWraps;
+		psDiffs->ui32Time[2] = ui32rval;
+
+		psDiffs->ui32Marker[0] = psDevInfo->ui32KickTACounter;
+		psDiffs->ui32Marker[1] = psDevInfo->ui32KickTARenderCounter;
+	}
+
+
+	PVRSRVPowerUnlock(KERNEL_ID);
+
+	SGXTestActivePowerEvent(psDeviceNode, KERNEL_ID);
+
+	return eError;
+}
+
+
+IMG_EXPORT
+PVRSRV_ERROR SGXReadHWPerfCBKM(IMG_HANDLE					hDevHandle,
+							   IMG_UINT32					ui32ArraySize,
+							   PVRSRV_SGX_HWPERF_CB_ENTRY	*psClientHWPerfEntry,
+							   IMG_UINT32					*pui32DataCount,
+							   IMG_UINT32					*pui32ClockSpeed,
+							   IMG_UINT32					*pui32HostTimeStamp)
+{
+	PVRSRV_ERROR    	eError = PVRSRV_OK;
+	PVRSRV_DEVICE_NODE	*psDeviceNode = hDevHandle;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+	SGXMKIF_HWPERF_CB	*psHWPerfCB = psDevInfo->psKernelHWPerfCBMemInfo->pvLinAddrKM;
+	IMG_UINT			i;
+
+	for (i = 0;
+		 psHWPerfCB->ui32Woff != psHWPerfCB->ui32Roff && i < ui32ArraySize;
+		 i++)
+	{
+		SGXMKIF_HWPERF_CB_ENTRY *psMKPerfEntry = &psHWPerfCB->psHWPerfCBData[psHWPerfCB->ui32Roff];
+
+		psClientHWPerfEntry[i].ui32FrameNo = psMKPerfEntry->ui32FrameNo;
+		psClientHWPerfEntry[i].ui32Type = psMKPerfEntry->ui32Type;
+		psClientHWPerfEntry[i].ui32Ordinal	= psMKPerfEntry->ui32Ordinal;
+		psClientHWPerfEntry[i].ui32Clocksx16 = SGXConvertTimeStamp(psDevInfo,
+													psMKPerfEntry->ui32TimeWraps,
+													psMKPerfEntry->ui32Time);
+		OSMemCopy(&psClientHWPerfEntry[i].ui32Counters[0],
+				  &psMKPerfEntry->ui32Counters[0],
+				  sizeof(psMKPerfEntry->ui32Counters));
+
+		psHWPerfCB->ui32Roff = (psHWPerfCB->ui32Roff + 1) & (SGXMKIF_HWPERF_CB_SIZE - 1);
+	}
+
+	*pui32DataCount = i;
+	*pui32ClockSpeed = psDevInfo->ui32CoreClockSpeed;
+	*pui32HostTimeStamp = OSClockus();
+
+	return eError;
+}
+#else
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxkick.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxkick.c
new file mode 100644
index 0000000..575ca69
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxkick.c
@@ -0,0 +1,740 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <stddef.h>
+#include "services_headers.h"
+#include "sgxinfo.h"
+#include "sgxinfokm.h"
+#if defined (PDUMP)
+#include "sgxapi_km.h"
+#include "pdump_km.h"
+#endif
+#include "sgx_bridge_km.h"
+#include "osfunc.h"
+#include "pvr_debug.h"
+#include "sgxutils.h"
+
+IMG_EXPORT
+PVRSRV_ERROR SGXDoKickKM(IMG_HANDLE hDevHandle, SGX_CCB_KICK *psCCBKick)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_KERNEL_SYNC_INFO	*psSyncInfo;
+	PVRSRV_KERNEL_MEM_INFO	*psCCBMemInfo = (PVRSRV_KERNEL_MEM_INFO *) psCCBKick->hCCBKernelMemInfo;
+	SGXMKIF_CMDTA_SHARED *psTACmd;
+	IMG_UINT32 i;
+#if defined(SUPPORT_SGX_HWPERF)
+	PVRSRV_DEVICE_NODE      *psDeviceNode;
+	PVRSRV_SGXDEV_INFO      *psDevInfo;
+
+	psDeviceNode = (PVRSRV_DEVICE_NODE *)hDevHandle;
+	psDevInfo = (PVRSRV_SGXDEV_INFO *)psDeviceNode->pvDevice;
+#endif
+
+#if defined(SUPPORT_SGX_HWPERF)
+	if (psCCBKick->bKickRender)
+	{
+		++psDevInfo->ui32KickTARenderCounter;
+	}
+	++psDevInfo->ui32KickTACounter;
+#endif
+
+	if (!CCB_OFFSET_IS_VALID(SGXMKIF_CMDTA_SHARED, psCCBMemInfo, psCCBKick, ui32CCBOffset))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXDoKickKM: Invalid CCB offset"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	psTACmd = CCB_DATA_FROM_OFFSET(SGXMKIF_CMDTA_SHARED, psCCBMemInfo, psCCBKick, ui32CCBOffset);
+
+
+	if (psCCBKick->hTA3DSyncInfo)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->hTA3DSyncInfo;
+		psTACmd->sTA3DDependency.sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+
+		psTACmd->sTA3DDependency.ui32WriteOpsPendingVal   = psSyncInfo->psSyncData->ui32WriteOpsPending;
+
+		if (psCCBKick->bTADependency)
+		{
+			psSyncInfo->psSyncData->ui32WriteOpsPending++;
+		}
+	}
+
+	if (psCCBKick->hTASyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->hTASyncInfo;
+
+		psTACmd->sTATQSyncReadOpsCompleteDevVAddr  = psSyncInfo->sReadOpsCompleteDevVAddr;
+		psTACmd->sTATQSyncWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+
+		psTACmd->ui32TATQSyncReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending++;
+		psTACmd->ui32TATQSyncWriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+
+	if (psCCBKick->h3DSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->h3DSyncInfo;
+
+		psTACmd->s3DTQSyncReadOpsCompleteDevVAddr  = psSyncInfo->sReadOpsCompleteDevVAddr;
+		psTACmd->s3DTQSyncWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+
+		psTACmd->ui323DTQSyncReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending++;
+		psTACmd->ui323DTQSyncWriteOpsPendingVal  = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+
+	psTACmd->ui32NumTAStatusVals = psCCBKick->ui32NumTAStatusVals;
+	if (psCCBKick->ui32NumTAStatusVals != 0)
+	{
+
+		for (i = 0; i < psCCBKick->ui32NumTAStatusVals; i++)
+		{
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			psTACmd->sCtlTAStatusInfo[i] = psCCBKick->asTAStatusUpdate[i].sCtlStatus;
+#else
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->ahTAStatusSyncInfo[i];
+			psTACmd->sCtlTAStatusInfo[i].sStatusDevAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+			psTACmd->sCtlTAStatusInfo[i].ui32StatusValue = psSyncInfo->psSyncData->ui32ReadOpsPending;
+#endif
+		}
+	}
+
+	psTACmd->ui32Num3DStatusVals = psCCBKick->ui32Num3DStatusVals;
+	if (psCCBKick->ui32Num3DStatusVals != 0)
+	{
+
+		for (i = 0; i < psCCBKick->ui32Num3DStatusVals; i++)
+		{
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			psTACmd->sCtl3DStatusInfo[i] = psCCBKick->as3DStatusUpdate[i].sCtlStatus;
+#else
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->ah3DStatusSyncInfo[i];
+			psTACmd->sCtl3DStatusInfo[i].sStatusDevAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+			psTACmd->sCtl3DStatusInfo[i].ui32StatusValue = psSyncInfo->psSyncData->ui32ReadOpsPending;
+#endif
+		}
+	}
+
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+
+	psTACmd->ui32NumTASrcSyncs = psCCBKick->ui32NumTASrcSyncs;
+	for (i=0; i<psCCBKick->ui32NumTASrcSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTASrcKernelSyncInfo[i];
+
+		psTACmd->asTASrcSyncs[i].sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		psTACmd->asTASrcSyncs[i].sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+
+		psTACmd->asTASrcSyncs[i].ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending++;
+
+		psTACmd->asTASrcSyncs[i].ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+
+	psTACmd->ui32NumTADstSyncs = psCCBKick->ui32NumTADstSyncs;
+	for (i=0; i<psCCBKick->ui32NumTADstSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTADstKernelSyncInfo[i];
+
+		psTACmd->asTADstSyncs[i].sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		psTACmd->asTADstSyncs[i].sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+
+		psTACmd->asTADstSyncs[i].ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		psTACmd->asTADstSyncs[i].ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+	}
+
+	psTACmd->ui32Num3DSrcSyncs = psCCBKick->ui32Num3DSrcSyncs;
+	for (i=0; i<psCCBKick->ui32Num3DSrcSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ah3DSrcKernelSyncInfo[i];
+
+		psTACmd->as3DSrcSyncs[i].sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		psTACmd->as3DSrcSyncs[i].sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+
+		psTACmd->as3DSrcSyncs[i].ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending++;
+
+		psTACmd->as3DSrcSyncs[i].ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+#else
+
+	psTACmd->ui32NumSrcSyncs = psCCBKick->ui32NumSrcSyncs;
+	for (i=0; i<psCCBKick->ui32NumSrcSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahSrcKernelSyncInfo[i];
+
+		psTACmd->asSrcSyncs[i].sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		psTACmd->asSrcSyncs[i].sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+
+		psTACmd->asSrcSyncs[i].ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending++;
+
+		psTACmd->asSrcSyncs[i].ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+#endif
+
+	if (psCCBKick->bFirstKickOrResume && psCCBKick->ui32NumDstSyncObjects > 0)
+	{
+		PVRSRV_KERNEL_MEM_INFO	*psHWDstSyncListMemInfo =
+								(PVRSRV_KERNEL_MEM_INFO *)psCCBKick->hKernelHWSyncListMemInfo;
+		SGXMKIF_HWDEVICE_SYNC_LIST *psHWDeviceSyncList = psHWDstSyncListMemInfo->pvLinAddrKM;
+		IMG_UINT32	ui32NumDstSyncs = psCCBKick->ui32NumDstSyncObjects;
+
+		PVR_ASSERT(((PVRSRV_KERNEL_MEM_INFO *)psCCBKick->hKernelHWSyncListMemInfo)->ui32AllocSize >= (sizeof(SGXMKIF_HWDEVICE_SYNC_LIST) +
+								(sizeof(PVRSRV_DEVICE_SYNC_OBJECT) * ui32NumDstSyncs)));
+
+		psHWDeviceSyncList->ui32NumSyncObjects = ui32NumDstSyncs;
+#if defined(PDUMP)
+		if (PDumpIsCaptureFrameKM())
+		{
+			PDUMPCOMMENT("HWDeviceSyncList for TACmd\r\n");
+			PDUMPMEM(IMG_NULL,
+					 psHWDstSyncListMemInfo,
+					 0,
+					 sizeof(SGXMKIF_HWDEVICE_SYNC_LIST),
+					 0,
+					 MAKEUNIQUETAG(psHWDstSyncListMemInfo));
+		}
+#endif
+
+		for (i=0; i<ui32NumDstSyncs; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->pahDstSyncHandles[i];
+
+			if (psSyncInfo)
+			{
+				psHWDeviceSyncList->asSyncData[i].sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+				psHWDeviceSyncList->asSyncData[i].sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+				psHWDeviceSyncList->asSyncData[i].ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+				psHWDeviceSyncList->asSyncData[i].ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+
+	#if defined(PDUMP)
+				if (PDumpIsCaptureFrameKM())
+				{
+					IMG_UINT32 ui32ModifiedValue;
+					IMG_UINT32 ui32SyncOffset = offsetof(SGXMKIF_HWDEVICE_SYNC_LIST, asSyncData)
+												+ (i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT));
+					IMG_UINT32 ui32WOpsOffset = ui32SyncOffset
+												+ offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32WriteOpsPendingVal);
+					IMG_UINT32 ui32ROpsOffset = ui32SyncOffset
+												+ offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32ReadOpsPendingVal);
+
+					PDUMPCOMMENT("HWDeviceSyncObject for RT: %i\r\n", i);
+
+					PDUMPMEM(IMG_NULL,
+							 psHWDstSyncListMemInfo,
+							 ui32SyncOffset,
+							 sizeof(PVRSRV_DEVICE_SYNC_OBJECT),
+							 0,
+							 MAKEUNIQUETAG(psHWDstSyncListMemInfo));
+
+					if ((psSyncInfo->psSyncData->ui32LastOpDumpVal == 0) &&
+						(psSyncInfo->psSyncData->ui32LastReadOpDumpVal == 0))
+					{
+
+						PDUMPCOMMENT("Init RT ROpsComplete\r\n", i);
+						PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+							psSyncInfo->psSyncDataMemInfoKM,
+							offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete),
+							sizeof(psSyncInfo->psSyncData->ui32ReadOpsComplete),
+							0,
+							MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+
+						PDUMPCOMMENT("Init RT WOpsComplete\r\n");
+							PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+								psSyncInfo->psSyncDataMemInfoKM,
+								offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete),
+								sizeof(psSyncInfo->psSyncData->ui32WriteOpsComplete),
+								0,
+								MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+					}
+
+					psSyncInfo->psSyncData->ui32LastOpDumpVal++;
+
+					ui32ModifiedValue = psSyncInfo->psSyncData->ui32LastOpDumpVal - 1;
+
+					PDUMPCOMMENT("Modify RT %d WOpPendingVal in HWDevSyncList\r\n", i);
+
+					PDUMPMEM(&ui32ModifiedValue,
+						psHWDstSyncListMemInfo,
+						ui32WOpsOffset,
+						sizeof(IMG_UINT32),
+						0,
+						MAKEUNIQUETAG(psHWDstSyncListMemInfo));
+
+					ui32ModifiedValue = 0;
+					PDUMPCOMMENT("Modify RT %d ROpsPendingVal in HWDevSyncList\r\n", i);
+
+					PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+						 psHWDstSyncListMemInfo,
+						 ui32ROpsOffset,
+						 sizeof(IMG_UINT32),
+						 0,
+						MAKEUNIQUETAG(psHWDstSyncListMemInfo));
+				}
+	#endif
+			}
+			else
+			{
+				psHWDeviceSyncList->asSyncData[i].sWriteOpsCompleteDevVAddr.uiAddr = 0;
+				psHWDeviceSyncList->asSyncData[i].sReadOpsCompleteDevVAddr.uiAddr = 0;
+
+				psHWDeviceSyncList->asSyncData[i].ui32ReadOpsPendingVal = 0;
+				psHWDeviceSyncList->asSyncData[i].ui32WriteOpsPendingVal = 0;
+			}
+		}
+	}
+
+
+
+
+	psTACmd->ui32CtrlFlags |= SGXMKIF_CMDTA_CTRLFLAGS_READY;
+
+#if defined(PDUMP)
+	if (PDumpIsCaptureFrameKM())
+	{
+		PDUMPCOMMENT("Shared part of TA command\r\n");
+
+		PDUMPMEM(psTACmd,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff,
+				 sizeof(SGXMKIF_CMDTA_SHARED),
+				 0,
+				 MAKEUNIQUETAG(psCCBMemInfo));
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+		for (i=0; i<psCCBKick->ui32NumTASrcSyncs; i++)
+		{
+			IMG_UINT32 	ui32ModifiedValue;
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTASrcKernelSyncInfo[i];
+
+			if ((psSyncInfo->psSyncData->ui32LastOpDumpVal == 0) &&
+				(psSyncInfo->psSyncData->ui32LastReadOpDumpVal == 0))
+			{
+
+				PDUMPCOMMENT("Init RT TA-SRC ROpsComplete\r\n", i);
+				PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psSyncInfo->psSyncDataMemInfoKM,
+					offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete),
+					sizeof(psSyncInfo->psSyncData->ui32ReadOpsComplete),
+					0,
+					MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+
+				PDUMPCOMMENT("Init RT TA-SRC WOpsComplete\r\n");
+					PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+						psSyncInfo->psSyncDataMemInfoKM,
+						offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete),
+						sizeof(psSyncInfo->psSyncData->ui32WriteOpsComplete),
+						0,
+						MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+			}
+
+			psSyncInfo->psSyncData->ui32LastReadOpDumpVal++;
+
+			ui32ModifiedValue = psSyncInfo->psSyncData->ui32LastReadOpDumpVal - 1;
+
+			PDUMPCOMMENT("Modify TA SrcSync %d ROpsPendingVal\r\n", i);
+
+			PDUMPMEM(&ui32ModifiedValue,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, asTASrcSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32ReadOpsPendingVal),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Modify TA SrcSync %d WOpPendingVal\r\n", i);
+
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+				psCCBMemInfo,
+				psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, asTASrcSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32WriteOpsPendingVal),
+				sizeof(IMG_UINT32),
+				0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+		}
+
+		for (i=0; i<psCCBKick->ui32NumTADstSyncs; i++)
+		{
+			IMG_UINT32 	ui32ModifiedValue;
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTADstKernelSyncInfo[i];
+
+			if ((psSyncInfo->psSyncData->ui32LastOpDumpVal == 0) &&
+				(psSyncInfo->psSyncData->ui32LastReadOpDumpVal == 0))
+			{
+
+				PDUMPCOMMENT("Init RT TA-DST ROpsComplete\r\n", i);
+				PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psSyncInfo->psSyncDataMemInfoKM,
+					offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete),
+					sizeof(psSyncInfo->psSyncData->ui32ReadOpsComplete),
+					0,
+					MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+
+				PDUMPCOMMENT("Init RT TA-DST WOpsComplete\r\n");
+					PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+						psSyncInfo->psSyncDataMemInfoKM,
+						offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete),
+						sizeof(psSyncInfo->psSyncData->ui32WriteOpsComplete),
+						0,
+						MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+			}
+
+			psSyncInfo->psSyncData->ui32LastOpDumpVal++;
+
+			ui32ModifiedValue = psSyncInfo->psSyncData->ui32LastOpDumpVal - 1;
+
+			PDUMPCOMMENT("Modify TA DstSync %d WOpPendingVal\r\n", i);
+
+			PDUMPMEM(&ui32ModifiedValue,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, asTADstSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32WriteOpsPendingVal),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Modify TA DstSync %d ROpsPendingVal\r\n", i);
+
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+				psCCBMemInfo,
+				psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, asTADstSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32ReadOpsPendingVal),
+				sizeof(IMG_UINT32),
+				0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+		}
+
+		for (i=0; i<psCCBKick->ui32Num3DSrcSyncs; i++)
+		{
+			IMG_UINT32 	ui32ModifiedValue;
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ah3DSrcKernelSyncInfo[i];
+
+			if ((psSyncInfo->psSyncData->ui32LastOpDumpVal == 0) &&
+				(psSyncInfo->psSyncData->ui32LastReadOpDumpVal == 0))
+			{
+
+				PDUMPCOMMENT("Init RT 3D-SRC ROpsComplete\r\n", i);
+				PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psSyncInfo->psSyncDataMemInfoKM,
+					offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete),
+					sizeof(psSyncInfo->psSyncData->ui32ReadOpsComplete),
+					0,
+					MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+
+				PDUMPCOMMENT("Init RT 3D-SRC WOpsComplete\r\n");
+					PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+						psSyncInfo->psSyncDataMemInfoKM,
+						offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete),
+						sizeof(psSyncInfo->psSyncData->ui32WriteOpsComplete),
+						0,
+						MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+			}
+
+			psSyncInfo->psSyncData->ui32LastReadOpDumpVal++;
+
+			ui32ModifiedValue = psSyncInfo->psSyncData->ui32LastReadOpDumpVal - 1;
+
+			PDUMPCOMMENT("Modify 3D SrcSync %d ROpsPendingVal\r\n", i);
+
+			PDUMPMEM(&ui32ModifiedValue,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, as3DSrcSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32ReadOpsPendingVal),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Modify 3D SrcSync %d WOpPendingVal\r\n", i);
+
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+				psCCBMemInfo,
+				psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, as3DSrcSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32WriteOpsPendingVal),
+				sizeof(IMG_UINT32),
+				0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+		}
+#else
+		for (i=0; i<psCCBKick->ui32NumSrcSyncs; i++)
+		{
+			IMG_UINT32 	ui32ModifiedValue;
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahSrcKernelSyncInfo[i];
+
+			if ((psSyncInfo->psSyncData->ui32LastOpDumpVal == 0) &&
+				(psSyncInfo->psSyncData->ui32LastReadOpDumpVal == 0))
+			{
+
+				PDUMPCOMMENT("Init RT ROpsComplete\r\n", i);
+				PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psSyncInfo->psSyncDataMemInfoKM,
+					offsetof(PVRSRV_SYNC_DATA, ui32ReadOpsComplete),
+					sizeof(psSyncInfo->psSyncData->ui32ReadOpsComplete),
+					0,
+					MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+
+				PDUMPCOMMENT("Init RT WOpsComplete\r\n");
+					PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+						psSyncInfo->psSyncDataMemInfoKM,
+						offsetof(PVRSRV_SYNC_DATA, ui32WriteOpsComplete),
+						sizeof(psSyncInfo->psSyncData->ui32WriteOpsComplete),
+						0,
+						MAKEUNIQUETAG(psSyncInfo->psSyncDataMemInfoKM));
+			}
+
+			psSyncInfo->psSyncData->ui32LastReadOpDumpVal++;
+
+			ui32ModifiedValue = psSyncInfo->psSyncData->ui32LastReadOpDumpVal - 1;
+
+			PDUMPCOMMENT("Modify SrcSync %d ROpsPendingVal\r\n", i);
+
+			PDUMPMEM(&ui32ModifiedValue,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, asSrcSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32ReadOpsPendingVal),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Modify SrcSync %d WOpPendingVal\r\n", i);
+
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+				psCCBMemInfo,
+				psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, asSrcSyncs) +
+					(i * sizeof(PVRSRV_DEVICE_SYNC_OBJECT)) + offsetof(PVRSRV_DEVICE_SYNC_OBJECT, ui32WriteOpsPendingVal),
+				sizeof(IMG_UINT32),
+				0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+		}
+#endif
+
+		for (i = 0; i < psCCBKick->ui32NumTAStatusVals; i++)
+		{
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			PDUMPCOMMENT("Modify TA status value in TA cmd\r\n");
+			PDUMPMEM(&psCCBKick->asTAStatusUpdate[i].ui32LastStatusUpdateDumpVal,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, sCtlTAStatusInfo[i].ui32StatusValue),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+#else
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->ahTAStatusSyncInfo[i];
+			PDUMPCOMMENT("Modify TA status value in TA cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, sCtlTAStatusInfo[i].ui32StatusValue),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+#endif
+		}
+
+		for (i = 0; i < psCCBKick->ui32Num3DStatusVals; i++)
+		{
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			PDUMPCOMMENT("Modify 3D status value in TA cmd\r\n");
+			PDUMPMEM(&psCCBKick->as3DStatusUpdate[i].ui32LastStatusUpdateDumpVal,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, sCtl3DStatusInfo[i].ui32StatusValue),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+#else
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->ah3DStatusSyncInfo[i];
+			PDUMPCOMMENT("Modify 3D status value in TA cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+				 psCCBMemInfo,
+				 psCCBKick->ui32CCBDumpWOff + offsetof(SGXMKIF_CMDTA_SHARED, sCtl3DStatusInfo[i].ui32StatusValue),
+				 sizeof(IMG_UINT32),
+				 0,
+				MAKEUNIQUETAG(psCCBMemInfo));
+#endif
+		}
+	}
+#endif
+
+	eError = SGXScheduleCCBCommandKM(hDevHandle, SGXMKIF_CMD_TA, &psCCBKick->sCommand, KERNEL_ID, 0);
+	if (eError == PVRSRV_ERROR_RETRY)
+	{
+		if (psCCBKick->bFirstKickOrResume && psCCBKick->ui32NumDstSyncObjects > 0)
+		{
+			for (i=0; i < psCCBKick->ui32NumDstSyncObjects; i++)
+			{
+
+				psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->pahDstSyncHandles[i];
+
+				if (psSyncInfo)
+				{
+					psSyncInfo->psSyncData->ui32WriteOpsPending--;
+#if defined(PDUMP)
+					if (PDumpIsCaptureFrameKM())
+					{
+						psSyncInfo->psSyncData->ui32LastOpDumpVal--;
+					}
+#endif
+				}
+			}
+		}
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+		for (i=0; i<psCCBKick->ui32NumTASrcSyncs; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTASrcKernelSyncInfo[i];
+			psSyncInfo->psSyncData->ui32ReadOpsPending--;
+		}
+		for (i=0; i<psCCBKick->ui32NumTADstSyncs; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTADstKernelSyncInfo[i];
+			psSyncInfo->psSyncData->ui32WriteOpsPending--;
+		}
+		for (i=0; i<psCCBKick->ui32Num3DSrcSyncs; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ah3DSrcKernelSyncInfo[i];
+			psSyncInfo->psSyncData->ui32ReadOpsPending--;
+		}
+#else
+		for (i=0; i<psCCBKick->ui32NumSrcSyncs; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahSrcKernelSyncInfo[i];
+			psSyncInfo->psSyncData->ui32ReadOpsPending--;
+		}
+#endif
+
+		return eError;
+	}
+	else if (PVRSRV_OK != eError)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXDoKickKM: SGXScheduleCCBCommandKM failed."));
+		return eError;
+	}
+
+
+#if defined(NO_HARDWARE)
+
+
+
+	if (psCCBKick->hTA3DSyncInfo)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->hTA3DSyncInfo;
+
+		if (psCCBKick->bTADependency)
+		{
+			psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+		}
+	}
+
+	if (psCCBKick->hTASyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->hTASyncInfo;
+
+		psSyncInfo->psSyncData->ui32ReadOpsComplete =  psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+
+	if (psCCBKick->h3DSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->h3DSyncInfo;
+
+		psSyncInfo->psSyncData->ui32ReadOpsComplete =  psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+
+
+	for (i = 0; i < psCCBKick->ui32NumTAStatusVals; i++)
+	{
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+		PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo = (PVRSRV_KERNEL_MEM_INFO*)psCCBKick->asTAStatusUpdate[i].hKernelMemInfo;
+
+		*(IMG_UINT32*)((IMG_UINTPTR_T)psKernelMemInfo->pvLinAddrKM
+						+ (psTACmd->sCtlTAStatusInfo[i].sStatusDevAddr.uiAddr
+						- psKernelMemInfo->sDevVAddr.uiAddr)) = psTACmd->sCtlTAStatusInfo[i].ui32StatusValue;
+#else
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->ahTAStatusSyncInfo[i];
+		psSyncInfo->psSyncData->ui32ReadOpsComplete = psTACmd->sCtlTAStatusInfo[i].ui32StatusValue;
+#endif
+	}
+
+#if defined(SUPPORT_SGX_GENERALISED_SYNCOBJECTS)
+
+	for (i=0; i<psCCBKick->ui32NumTASrcSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTASrcKernelSyncInfo[i];
+		psSyncInfo->psSyncData->ui32ReadOpsComplete =  psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+	for (i=0; i<psCCBKick->ui32NumTADstSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahTADstKernelSyncInfo[i];
+		psSyncInfo->psSyncData->ui32WriteOpsComplete =  psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+	for (i=0; i<psCCBKick->ui32Num3DSrcSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ah3DSrcKernelSyncInfo[i];
+		psSyncInfo->psSyncData->ui32ReadOpsComplete =  psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+#else
+
+	for (i=0; i<psCCBKick->ui32NumSrcSyncs; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *) psCCBKick->ahSrcKernelSyncInfo[i];
+		psSyncInfo->psSyncData->ui32ReadOpsComplete =  psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+#endif
+
+	if (psCCBKick->bTerminateOrAbort)
+	{
+		if (psCCBKick->ui32NumDstSyncObjects > 0)
+		{
+			PVRSRV_KERNEL_MEM_INFO	*psHWDstSyncListMemInfo =
+								(PVRSRV_KERNEL_MEM_INFO *)psCCBKick->hKernelHWSyncListMemInfo;
+			SGXMKIF_HWDEVICE_SYNC_LIST *psHWDeviceSyncList = psHWDstSyncListMemInfo->pvLinAddrKM;
+
+			for (i=0; i<psCCBKick->ui32NumDstSyncObjects; i++)
+			{
+				psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->pahDstSyncHandles[i];
+				if (psSyncInfo)
+					psSyncInfo->psSyncData->ui32WriteOpsComplete = psHWDeviceSyncList->asSyncData[i].ui32WriteOpsPendingVal+1;
+			}
+		}
+
+
+		for (i = 0; i < psCCBKick->ui32Num3DStatusVals; i++)
+		{
+#if defined(SUPPORT_SGX_NEW_STATUS_VALS)
+			PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo = (PVRSRV_KERNEL_MEM_INFO*)psCCBKick->as3DStatusUpdate[i].hKernelMemInfo;
+
+			*(IMG_UINT32*)((IMG_UINTPTR_T)psKernelMemInfo->pvLinAddrKM
+							+ (psTACmd->sCtl3DStatusInfo[i].sStatusDevAddr.uiAddr
+							- psKernelMemInfo->sDevVAddr.uiAddr)) = psTACmd->sCtl3DStatusInfo[i].ui32StatusValue;
+#else
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psCCBKick->ah3DStatusSyncInfo[i];
+			psSyncInfo->psSyncData->ui32ReadOpsComplete = psTACmd->sCtl3DStatusInfo[i].ui32StatusValue;
+#endif
+		}
+	}
+#endif
+
+	return eError;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxpower.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxpower.c
new file mode 100644
index 0000000..af4d52a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxpower.c
@@ -0,0 +1,465 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <stddef.h>
+
+#include "sgxdefs.h"
+#include "services_headers.h"
+#include "sgxapi_km.h"
+#include "sgx_mkif_km.h"
+#include "sgxutils.h"
+#include "pdump_km.h"
+
+
+#if defined(SUPPORT_HW_RECOVERY)
+static PVRSRV_ERROR SGXAddTimer(PVRSRV_DEVICE_NODE		*psDeviceNode,
+								SGX_TIMING_INFORMATION	*psSGXTimingInfo,
+								IMG_HANDLE				*phTimer)
+{
+
+
+
+	*phTimer = OSAddTimer(SGXOSTimer, psDeviceNode,
+						  1000 * 50 / psSGXTimingInfo->ui32uKernelFreq);
+	if(*phTimer == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXAddTimer : Failed to register timer callback function"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	return PVRSRV_OK;
+}
+#endif
+
+
+static PVRSRV_ERROR SGXUpdateTimingInfo(PVRSRV_DEVICE_NODE	*psDeviceNode)
+{
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+#if defined(SGX_DYNAMIC_TIMING_INFO)
+	SGX_TIMING_INFORMATION	sSGXTimingInfo = {0};
+#else
+	SGX_DEVICE_MAP		*psSGXDeviceMap;
+#endif
+	IMG_UINT32		ui32ActivePowManSampleRate;
+	SGX_TIMING_INFORMATION	*psSGXTimingInfo;
+
+
+#if defined(SGX_DYNAMIC_TIMING_INFO)
+	psSGXTimingInfo = &sSGXTimingInfo;
+	SysGetSGXTimingInformation(psSGXTimingInfo);
+#else
+	SysGetDeviceMemoryMap(PVRSRV_DEVICE_TYPE_SGX,
+						  (IMG_VOID**)&psSGXDeviceMap);
+	psSGXTimingInfo = &psSGXDeviceMap->sTimingInfo;
+#endif
+
+#if defined(SUPPORT_HW_RECOVERY)
+	{
+		PVRSRV_ERROR			eError;
+		IMG_UINT32	ui32OlduKernelFreq;
+
+		if (psDevInfo->hTimer != IMG_NULL)
+		{
+			ui32OlduKernelFreq = psDevInfo->ui32CoreClockSpeed / psDevInfo->ui32uKernelTimerClock;
+			if (ui32OlduKernelFreq != psSGXTimingInfo->ui32uKernelFreq)
+			{
+
+
+				IMG_HANDLE hNewTimer;
+
+				eError = SGXAddTimer(psDeviceNode, psSGXTimingInfo, &hNewTimer);
+				if (eError == PVRSRV_OK)
+				{
+					eError = OSRemoveTimer(psDevInfo->hTimer);
+					if (eError != PVRSRV_OK)
+					{
+						PVR_DPF((PVR_DBG_ERROR,"SGXUpdateTimingInfo: Failed to remove timer"));
+					}
+					psDevInfo->hTimer = hNewTimer;
+				}
+				else
+				{
+
+				}
+			}
+		}
+		else
+		{
+			eError = SGXAddTimer(psDeviceNode, psSGXTimingInfo, &psDevInfo->hTimer);
+			if (eError != PVRSRV_OK)
+			{
+				return eError;
+			}
+		}
+
+		psDevInfo->psSGXHostCtl->ui32HWRecoverySampleRate =
+			psSGXTimingInfo->ui32uKernelFreq / psSGXTimingInfo->ui32HWRecoveryFreq;
+	}
+#endif
+
+
+	psDevInfo->ui32CoreClockSpeed = psSGXTimingInfo->ui32CoreClockSpeed;
+	psDevInfo->ui32uKernelTimerClock = psSGXTimingInfo->ui32CoreClockSpeed / psSGXTimingInfo->ui32uKernelFreq;
+
+
+	psDevInfo->psSGXHostCtl->ui32uKernelTimerClock = psDevInfo->ui32uKernelTimerClock;
+#if defined(PDUMP)
+	PDUMPCOMMENT("Host Control - Microkernel clock");
+	PDUMPMEM(IMG_NULL, psDevInfo->psKernelSGXHostCtlMemInfo,
+			 offsetof(SGXMKIF_HOST_CTL, ui32uKernelTimerClock),
+			 sizeof(IMG_UINT32), PDUMP_FLAGS_CONTINUOUS,
+			 MAKEUNIQUETAG(psDevInfo->psKernelSGXHostCtlMemInfo));
+#endif
+
+	if (psSGXTimingInfo->bEnableActivePM)
+	{
+		ui32ActivePowManSampleRate =
+			psSGXTimingInfo->ui32uKernelFreq * psSGXTimingInfo->ui32ActivePowManLatencyms / 1000;
+
+
+
+
+
+
+
+
+		ui32ActivePowManSampleRate += 1;
+	}
+	else
+	{
+		ui32ActivePowManSampleRate = 0;
+	}
+
+	psDevInfo->psSGXHostCtl->ui32ActivePowManSampleRate = ui32ActivePowManSampleRate;
+#if defined(PDUMP)
+	PDUMPMEM(IMG_NULL, psDevInfo->psKernelSGXHostCtlMemInfo,
+			 offsetof(SGXMKIF_HOST_CTL, ui32ActivePowManSampleRate),
+			 sizeof(IMG_UINT32), PDUMP_FLAGS_CONTINUOUS,
+			 MAKEUNIQUETAG(psDevInfo->psKernelSGXHostCtlMemInfo));
+#endif
+
+	return PVRSRV_OK;
+}
+
+
+static IMG_VOID SGXStartTimer(PVRSRV_SGXDEV_INFO	*psDevInfo)
+{
+	#if defined(SUPPORT_HW_RECOVERY)
+	PVRSRV_ERROR	eError;
+
+	eError = OSEnableTimer(psDevInfo->hTimer);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXStartTimer : Failed to enable host timer"));
+	}
+	#else
+	PVR_UNREFERENCED_PARAMETER(psDevInfo);
+	#endif
+}
+
+
+static IMG_VOID SGXPollForClockGating (PVRSRV_SGXDEV_INFO	*psDevInfo,
+									   IMG_UINT32			ui32Register,
+									   IMG_UINT32			ui32RegisterValue,
+									   IMG_CHAR				*pszComment)
+{
+	PVR_UNREFERENCED_PARAMETER(psDevInfo);
+	PVR_UNREFERENCED_PARAMETER(ui32Register);
+	PVR_UNREFERENCED_PARAMETER(ui32RegisterValue);
+	PVR_UNREFERENCED_PARAMETER(pszComment);
+
+	#if !defined(NO_HARDWARE)
+	PVR_ASSERT(psDevInfo != IMG_NULL);
+
+
+	if (PollForValueKM((IMG_UINT32 *)psDevInfo->pvRegsBaseKM + (ui32Register >> 2),
+						0,
+						ui32RegisterValue,
+						MAX_HW_TIME_US/WAIT_TRY_COUNT,
+						WAIT_TRY_COUNT) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXPrePowerState: %s failed.", pszComment));
+	}
+	#endif
+
+	PDUMPCOMMENT(pszComment);
+	PDUMPREGPOL(ui32Register, 0, ui32RegisterValue);
+}
+
+
+PVRSRV_ERROR SGXPrePowerState (IMG_HANDLE				hDevHandle,
+							   PVRSRV_DEV_POWER_STATE	eNewPowerState,
+							   PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	if ((eNewPowerState != eCurrentPowerState) &&
+		(eNewPowerState != PVRSRV_DEV_POWER_STATE_ON))
+	{
+		PVRSRV_ERROR		eError;
+		PVRSRV_DEVICE_NODE	*psDeviceNode = hDevHandle;
+		PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+		IMG_UINT32			ui32PowerCmd, ui32CompleteStatus;
+		SGXMKIF_COMMAND		sCommand = {0};
+		IMG_UINT32			ui32Core;
+
+		#if defined(SUPPORT_HW_RECOVERY)
+
+		eError = OSDisableTimer(psDevInfo->hTimer);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXPrePowerState: Failed to disable timer"));
+			return eError;
+		}
+		#endif
+
+		if (eNewPowerState == PVRSRV_DEV_POWER_STATE_OFF)
+		{
+
+			ui32PowerCmd = PVRSRV_POWERCMD_POWEROFF;
+			ui32CompleteStatus = PVRSRV_USSE_EDM_POWMAN_POWEROFF_COMPLETE;
+			PDUMPCOMMENT("SGX power off request");
+		}
+		else
+		{
+
+			ui32PowerCmd = PVRSRV_POWERCMD_IDLE;
+			ui32CompleteStatus = PVRSRV_USSE_EDM_POWMAN_IDLE_COMPLETE;
+			PDUMPCOMMENT("SGX idle request");
+		}
+
+		sCommand.ui32Data[1] = ui32PowerCmd;
+
+		eError = SGXScheduleCCBCommand(psDevInfo, SGXMKIF_CMD_POWER, &sCommand, KERNEL_ID, 0);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXPrePowerState: Failed to submit power down command"));
+			return eError;
+		}
+
+
+		#if !defined(NO_HARDWARE)
+		if (PollForValueKM(&psDevInfo->psSGXHostCtl->ui32PowerStatus,
+							ui32CompleteStatus,
+							ui32CompleteStatus,
+							MAX_HW_TIME_US/WAIT_TRY_COUNT,
+							WAIT_TRY_COUNT) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXPrePowerState: Wait for SGX ukernel power transition failed."));
+			PVR_DBG_BREAK;
+		}
+		#endif
+
+		#if defined(PDUMP)
+		PDUMPCOMMENT("TA/3D CCB Control - Wait for power event on uKernel.");
+		PDUMPMEMPOL(psDevInfo->psKernelSGXHostCtlMemInfo,
+					offsetof(SGXMKIF_HOST_CTL, ui32PowerStatus),
+					ui32CompleteStatus,
+					ui32CompleteStatus,
+					PDUMP_POLL_OPERATOR_EQUAL,
+					0,
+					MAKEUNIQUETAG(psDevInfo->psKernelSGXHostCtlMemInfo));
+		#endif
+
+		for (ui32Core = 0; ui32Core < SGX_FEATURE_MP_CORE_COUNT; ui32Core++)
+		{
+
+			SGXPollForClockGating(psDevInfo,
+								  SGX_MP_CORE_SELECT(psDevInfo->ui32ClkGateStatusReg, ui32Core),
+								  psDevInfo->ui32ClkGateStatusMask,
+								  "Wait for SGX clock gating");
+		}
+
+		#if defined(SGX_FEATURE_MP)
+
+		SGXPollForClockGating(psDevInfo,
+							  psDevInfo->ui32MasterClkGateStatusReg,
+							  psDevInfo->ui32MasterClkGateStatusMask,
+							  "Wait for SGX master clock gating");
+		#endif
+
+		if (eNewPowerState == PVRSRV_DEV_POWER_STATE_OFF)
+		{
+
+			eError = SGXDeinitialise(psDevInfo);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SGXPrePowerState: SGXDeinitialise failed: %lu", eError));
+				return eError;
+			}
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR SGXPostPowerState (IMG_HANDLE				hDevHandle,
+								PVRSRV_DEV_POWER_STATE	eNewPowerState,
+								PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	if ((eNewPowerState != eCurrentPowerState) &&
+		(eCurrentPowerState != PVRSRV_DEV_POWER_STATE_ON))
+	{
+		PVRSRV_ERROR		eError;
+		PVRSRV_DEVICE_NODE	*psDeviceNode = hDevHandle;
+		PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+		SGXMKIF_HOST_CTL *psSGXHostCtl = psDevInfo->psSGXHostCtl;
+
+
+		psSGXHostCtl->ui32PowerStatus = 0;
+		#if defined(PDUMP)
+		PDUMPCOMMENT("TA/3D CCB Control - Reset power status");
+		PDUMPMEM(IMG_NULL, psDevInfo->psKernelSGXHostCtlMemInfo,
+				 offsetof(SGXMKIF_HOST_CTL, ui32PowerStatus),
+				 sizeof(IMG_UINT32), PDUMP_FLAGS_CONTINUOUS,
+				 MAKEUNIQUETAG(psDevInfo->psKernelSGXHostCtlMemInfo));
+		#endif
+
+		if (eCurrentPowerState == PVRSRV_DEV_POWER_STATE_OFF)
+		{
+
+
+
+
+
+			eError = SGXUpdateTimingInfo(psDeviceNode);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SGXPostPowerState: SGXUpdateTimingInfo failed"));
+				return eError;
+			}
+
+
+
+			eError = SGXInitialise(psDevInfo);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SGXPostPowerState: SGXInitialise failed"));
+				return eError;
+			}
+		}
+		else
+		{
+
+
+			SGXMKIF_COMMAND		sCommand = {0};
+
+			sCommand.ui32Data[1] = PVRSRV_POWERCMD_RESUME;
+			eError = SGXScheduleCCBCommand(psDevInfo, SGXMKIF_CMD_POWER, &sCommand, ISR_ID, 0);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SGXPostPowerState failed to schedule CCB command: %lu", eError));
+				return PVRSRV_ERROR_GENERIC;
+			}
+		}
+
+		SGXStartTimer(psDevInfo);
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR SGXPreClockSpeedChange (IMG_HANDLE				hDevHandle,
+									 IMG_BOOL				bIdleDevice,
+									 PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_DEVICE_NODE	*psDeviceNode = hDevHandle;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+
+	PVR_UNREFERENCED_PARAMETER(psDevInfo);
+
+	if (eCurrentPowerState == PVRSRV_DEV_POWER_STATE_ON)
+	{
+		if (bIdleDevice)
+		{
+
+			PDUMPSUSPEND();
+
+			eError = SGXPrePowerState(hDevHandle, PVRSRV_DEV_POWER_STATE_IDLE,
+									  PVRSRV_DEV_POWER_STATE_ON);
+
+			if (eError != PVRSRV_OK)
+			{
+				PDUMPRESUME();
+				return eError;
+			}
+		}
+	}
+
+	PVR_DPF((PVR_DBG_MESSAGE,"SGXPreClockSpeedChange: SGX clock speed was %luHz",
+			psDevInfo->ui32CoreClockSpeed));
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR SGXPostClockSpeedChange (IMG_HANDLE				hDevHandle,
+									  IMG_BOOL					bIdleDevice,
+									  PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	PVRSRV_DEVICE_NODE	*psDeviceNode = hDevHandle;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+	IMG_UINT32			ui32OldClockSpeed = psDevInfo->ui32CoreClockSpeed;
+
+	PVR_UNREFERENCED_PARAMETER(ui32OldClockSpeed);
+
+	if (eCurrentPowerState == PVRSRV_DEV_POWER_STATE_ON)
+	{
+		PVRSRV_ERROR eError;
+
+
+
+		eError = SGXUpdateTimingInfo(psDeviceNode);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXPostPowerState: SGXUpdateTimingInfo failed"));
+			return eError;
+		}
+
+		if (bIdleDevice)
+		{
+
+			eError = SGXPostPowerState(hDevHandle, PVRSRV_DEV_POWER_STATE_ON,
+									   PVRSRV_DEV_POWER_STATE_IDLE);
+
+			PDUMPRESUME();
+
+			if (eError != PVRSRV_OK)
+			{
+				return eError;
+			}
+		}
+		else
+		{
+			SGXStartTimer(psDevInfo);
+		}
+
+	}
+
+	PVR_DPF((PVR_DBG_MESSAGE,"SGXPostClockSpeedChange: SGX clock speed changed from %luHz to %luHz",
+			ui32OldClockSpeed, psDevInfo->ui32CoreClockSpeed));
+
+	return PVRSRV_OK;
+}
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxreset.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxreset.c
new file mode 100644
index 0000000..a746b88
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxreset.c
@@ -0,0 +1,485 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "sgxdefs.h"
+#include "sgxmmu.h"
+#include "services_headers.h"
+#include "sgxinfokm.h"
+#include "sgxconfig.h"
+
+#include "pdump_km.h"
+
+
+static IMG_VOID SGXResetSoftReset(PVRSRV_SGXDEV_INFO	*psDevInfo,
+								  IMG_BOOL				bResetBIF,
+								  IMG_UINT32			ui32PDUMPFlags,
+								  IMG_BOOL				bPDump)
+{
+	IMG_UINT32 ui32SoftResetRegVal;
+
+#if defined(SGX_FEATURE_MP)
+	ui32SoftResetRegVal =
+					EUR_CR_MASTER_SOFT_RESET_IPF_RESET_MASK |
+					EUR_CR_MASTER_SOFT_RESET_DPM_RESET_MASK  |
+					EUR_CR_MASTER_SOFT_RESET_VDM_RESET_MASK;
+
+#if defined(SGX_FEATURE_SYSTEM_CACHE)
+	ui32SoftResetRegVal |= EUR_CR_MASTER_SOFT_RESET_SLC_RESET_MASK;
+#endif
+
+	if (bResetBIF)
+	{
+		ui32SoftResetRegVal |= EUR_CR_MASTER_SOFT_RESET_BIF_RESET_MASK;
+	}
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_SOFT_RESET, ui32SoftResetRegVal);
+	if (bPDump)
+	{
+		PDUMPREGWITHFLAGS(EUR_CR_MASTER_SOFT_RESET, ui32SoftResetRegVal, ui32PDUMPFlags);
+	}
+#endif
+
+	ui32SoftResetRegVal =
+
+					EUR_CR_SOFT_RESET_DPM_RESET_MASK |
+					EUR_CR_SOFT_RESET_TA_RESET_MASK  |
+					EUR_CR_SOFT_RESET_USE_RESET_MASK |
+					EUR_CR_SOFT_RESET_ISP_RESET_MASK |
+					EUR_CR_SOFT_RESET_TSP_RESET_MASK;
+
+#ifdef EUR_CR_SOFT_RESET_TWOD_RESET_MASK
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_TWOD_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_TE_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_TE_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_MTE_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_MTE_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_ISP2_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_ISP2_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_PDS_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_PDS_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_PBE_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_PBE_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_CACHEL2_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_CACHEL2_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_TCU_L2_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_TCU_L2_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_UCACHEL2_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_UCACHEL2_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_MADD_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_MADD_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_ITR_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_ITR_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_TEX_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_TEX_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_IDXFIFO_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_IDXFIFO_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_VDM_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_VDM_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_DCU_L2_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_DCU_L2_RESET_MASK;
+#endif
+#if defined(EUR_CR_SOFT_RESET_DCU_L0L1_RESET_MASK)
+	ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_DCU_L0L1_RESET_MASK;
+#endif
+
+#if !defined(PDUMP)
+	PVR_UNREFERENCED_PARAMETER(ui32PDUMPFlags);
+#endif
+
+	if (bResetBIF)
+	{
+		ui32SoftResetRegVal |= EUR_CR_SOFT_RESET_BIF_RESET_MASK;
+	}
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_SOFT_RESET, ui32SoftResetRegVal);
+	if (bPDump)
+	{
+		PDUMPREGWITHFLAGS(EUR_CR_SOFT_RESET, ui32SoftResetRegVal, ui32PDUMPFlags);
+	}
+}
+
+
+static IMG_VOID SGXResetSleep(PVRSRV_SGXDEV_INFO	*psDevInfo,
+							  IMG_UINT32			ui32PDUMPFlags,
+							  IMG_BOOL				bPDump)
+{
+#if !defined(PDUMP)
+	PVR_UNREFERENCED_PARAMETER(ui32PDUMPFlags);
+#endif
+
+
+	OSWaitus(1000 * 1000000 / psDevInfo->ui32CoreClockSpeed);
+	if (bPDump)
+	{
+		PDUMPIDLWITHFLAGS(30, ui32PDUMPFlags);
+#if defined(PDUMP)
+		PDumpRegRead(EUR_CR_SOFT_RESET, ui32PDUMPFlags);
+#endif
+	}
+
+
+
+}
+
+
+static IMG_VOID SGXResetInvalDC(PVRSRV_SGXDEV_INFO	*psDevInfo,
+							    IMG_UINT32			ui32PDUMPFlags,
+								IMG_BOOL			bPDump)
+{
+	IMG_UINT32 ui32RegVal;
+
+
+#if defined(EUR_CR_BIF_CTRL_INVAL)
+	ui32RegVal = EUR_CR_BIF_CTRL_INVAL_ALL_MASK;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL_INVAL, ui32RegVal);
+	if (bPDump)
+	{
+		PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL_INVAL, ui32RegVal, ui32PDUMPFlags);
+	}
+#else
+	ui32RegVal = EUR_CR_BIF_CTRL_INVALDC_MASK;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32RegVal);
+	if (bPDump)
+	{
+		PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+	}
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, bPDump);
+
+	ui32RegVal = 0;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32RegVal);
+	if (bPDump)
+	{
+		PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+	}
+#endif
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, bPDump);
+
+#if !defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+	{
+
+
+
+		if (PollForValueKM((IMG_UINT32 *)((IMG_UINT8*)psDevInfo->pvRegsBaseKM + EUR_CR_BIF_MEM_REQ_STAT),
+							0,
+							EUR_CR_BIF_MEM_REQ_STAT_READS_MASK,
+							MAX_HW_TIME_US/WAIT_TRY_COUNT,
+							WAIT_TRY_COUNT) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"Wait for DC invalidate failed."));
+			PVR_DBG_BREAK;
+		}
+
+		if (bPDump)
+		{
+			PDUMPREGPOLWITHFLAGS(EUR_CR_BIF_MEM_REQ_STAT, 0, EUR_CR_BIF_MEM_REQ_STAT_READS_MASK, ui32PDUMPFlags);
+		}
+	}
+#endif
+}
+
+
+IMG_VOID SGXReset(PVRSRV_SGXDEV_INFO	*psDevInfo,
+				  IMG_UINT32			 ui32PDUMPFlags)
+{
+	IMG_UINT32 ui32RegVal;
+#if defined(EUR_CR_BIF_INT_STAT_FAULT_REQ_MASK)
+	const IMG_UINT32 ui32BifFaultMask = EUR_CR_BIF_INT_STAT_FAULT_REQ_MASK;
+#else
+	const IMG_UINT32 ui32BifFaultMask = EUR_CR_BIF_INT_STAT_FAULT_MASK;
+#endif
+
+#ifndef PDUMP
+	PVR_UNREFERENCED_PARAMETER(ui32PDUMPFlags);
+#endif
+
+	psDevInfo->ui32NumResets++;
+
+	PDUMPCOMMENTWITHFLAGS(ui32PDUMPFlags, "Start of SGX reset sequence\r\n");
+
+#if defined(FIX_HW_BRN_23944)
+
+	ui32RegVal = EUR_CR_BIF_CTRL_PAUSE_MASK;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+
+	ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_INT_STAT);
+	if (ui32RegVal & ui32BifFaultMask)
+	{
+
+		ui32RegVal = EUR_CR_BIF_CTRL_PAUSE_MASK | EUR_CR_BIF_CTRL_CLEAR_FAULT_MASK;
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32RegVal);
+		PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+
+		SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+
+		ui32RegVal = EUR_CR_BIF_CTRL_PAUSE_MASK;
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32RegVal);
+		PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+
+		SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+	}
+#endif
+
+
+	SGXResetSoftReset(psDevInfo, IMG_TRUE, ui32PDUMPFlags, IMG_TRUE);
+
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+
+
+
+#if defined(SGX_FEATURE_36BIT_MMU)
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_36BIT_ADDRESSING, EUR_CR_BIF_36BIT_ADDRESSING_ENABLE_MASK);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_36BIT_ADDRESSING, EUR_CR_BIF_36BIT_ADDRESSING_ENABLE_MASK, ui32PDUMPFlags);
+#endif
+
+	ui32RegVal = 0;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_CTRL, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+#if defined(SGX_FEATURE_MP)
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_BIF_CTRL, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_MASTER_BIF_CTRL, ui32RegVal, ui32PDUMPFlags);
+#endif
+#if defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_BANK_SET, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_BANK_SET, ui32RegVal, ui32PDUMPFlags);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_BANK0, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_BANK0, ui32RegVal, ui32PDUMPFlags);
+#endif
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_DIR_LIST_BASE0, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_DIR_LIST_BASE0, ui32RegVal, ui32PDUMPFlags);
+
+#if defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+	{
+		IMG_UINT32	ui32DirList, ui32DirListReg;
+
+		for (ui32DirList = 1;
+			 ui32DirList < SGX_FEATURE_BIF_NUM_DIRLISTS;
+			 ui32DirList++)
+		{
+			ui32DirListReg = EUR_CR_BIF_DIR_LIST_BASE1 + 4 * (ui32DirList - 1);
+			OSWriteHWReg(psDevInfo->pvRegsBaseKM, ui32DirListReg, ui32RegVal);
+			PDUMPREGWITHFLAGS(ui32DirListReg, ui32RegVal, ui32PDUMPFlags);
+		}
+	}
+#endif
+
+#if defined(EUR_CR_BIF_MEM_ARB_CONFIG)
+
+
+	ui32RegVal	= (12UL << EUR_CR_BIF_MEM_ARB_CONFIG_PAGE_SIZE_SHIFT) |
+				  (7UL << EUR_CR_BIF_MEM_ARB_CONFIG_BEST_CNT_SHIFT) |
+				  (12UL << EUR_CR_BIF_MEM_ARB_CONFIG_TTE_THRESH_SHIFT);
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_MEM_ARB_CONFIG, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_MEM_ARB_CONFIG, ui32RegVal, ui32PDUMPFlags);
+#endif
+
+#if defined(SGX_FEATURE_SYSTEM_CACHE)
+#if defined(SGX_FEATURE_MP)
+	#if defined(SGX_BYPASS_SYSTEM_CACHE)
+		#error SGX_BYPASS_SYSTEM_CACHE not supported
+	#else
+		ui32RegVal = EUR_CR_MASTER_SLC_CTRL_USSE_INVAL_REQ0_MASK |
+						(0xC << EUR_CR_MASTER_SLC_CTRL_ARB_PAGE_SIZE_SHIFT);
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_SLC_CTRL, ui32RegVal);
+		PDUMPREG(EUR_CR_MASTER_SLC_CTRL, ui32RegVal);
+
+		ui32RegVal = EUR_CR_MASTER_SLC_CTRL_BYPASS_BYP_CC_MASK;
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_SLC_CTRL_BYPASS, ui32RegVal);
+		PDUMPREG(EUR_CR_MASTER_SLC_CTRL_BYPASS, ui32RegVal);
+	#endif
+#else
+	#if defined(SGX_BYPASS_SYSTEM_CACHE)
+
+		ui32RegVal = EUR_CR_MNE_CR_CTRL_BYPASS_ALL_MASK;
+	#else
+		#if defined(FIX_HW_BRN_26620)
+			ui32RegVal = 0;
+		#else
+
+			ui32RegVal = EUR_CR_MNE_CR_CTRL_BYP_CC_MASK;
+		#endif
+	#endif
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MNE_CR_CTRL, ui32RegVal);
+	PDUMPREG(EUR_CR_MNE_CR_CTRL, ui32RegVal);
+#endif
+#endif
+
+
+
+
+
+
+	ui32RegVal = psDevInfo->sBIFResetPDDevPAddr.uiAddr;
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_DIR_LIST_BASE0, ui32RegVal);
+
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_FALSE);
+
+
+	SGXResetSoftReset(psDevInfo, IMG_FALSE, ui32PDUMPFlags, IMG_TRUE);
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_FALSE);
+
+	SGXResetInvalDC(psDevInfo, ui32PDUMPFlags, IMG_FALSE);
+
+
+
+	for (;;)
+	{
+		IMG_UINT32 ui32BifIntStat = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_INT_STAT);
+		IMG_DEV_VIRTADDR sBifFault;
+		IMG_UINT32 ui32PDIndex, ui32PTIndex;
+
+		if ((ui32BifIntStat & ui32BifFaultMask) == 0)
+		{
+			break;
+		}
+
+
+
+
+		sBifFault.uiAddr = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_FAULT);
+		PVR_DPF((PVR_DBG_WARNING, "SGXReset: Page fault 0x%x/0x%x", ui32BifIntStat, sBifFault.uiAddr));
+		ui32PDIndex = sBifFault.uiAddr >> (SGX_MMU_PAGE_SHIFT + SGX_MMU_PT_SHIFT);
+		ui32PTIndex = (sBifFault.uiAddr & SGX_MMU_PT_MASK) >> SGX_MMU_PAGE_SHIFT;
+
+
+		SGXResetSoftReset(psDevInfo, IMG_TRUE, ui32PDUMPFlags, IMG_FALSE);
+
+
+		psDevInfo->pui32BIFResetPD[ui32PDIndex] = (psDevInfo->sBIFResetPTDevPAddr.uiAddr
+												>>SGX_MMU_PDE_ADDR_ALIGNSHIFT)
+												| SGX_MMU_PDE_PAGE_SIZE_4K
+												| SGX_MMU_PDE_VALID;
+		psDevInfo->pui32BIFResetPT[ui32PTIndex] = (psDevInfo->sBIFResetPageDevPAddr.uiAddr
+												>>SGX_MMU_PTE_ADDR_ALIGNSHIFT)
+												| SGX_MMU_PTE_VALID;
+
+
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_STATUS);
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_HOST_CLEAR, ui32RegVal);
+		ui32RegVal = OSReadHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_STATUS2);
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_EVENT_HOST_CLEAR2, ui32RegVal);
+
+		SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_FALSE);
+
+
+		SGXResetSoftReset(psDevInfo, IMG_FALSE, ui32PDUMPFlags, IMG_FALSE);
+		SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_FALSE);
+
+
+		SGXResetInvalDC(psDevInfo, ui32PDUMPFlags, IMG_FALSE);
+
+
+		psDevInfo->pui32BIFResetPD[ui32PDIndex] = 0;
+		psDevInfo->pui32BIFResetPT[ui32PTIndex] = 0;
+	}
+
+
+
+
+	#if defined(SGX_FEATURE_MULTIPLE_MEM_CONTEXTS)
+
+	ui32RegVal = (SGX_BIF_DIR_LIST_INDEX_EDM << EUR_CR_BIF_BANK0_INDEX_EDM_SHIFT);
+
+	#if defined(SGX_FEATURE_2D_HARDWARE)
+
+	ui32RegVal |= (SGX_BIF_DIR_LIST_INDEX_EDM << EUR_CR_BIF_BANK0_INDEX_2D_SHIFT);
+	#endif
+
+	#if defined(FIX_HW_BRN_23410)
+
+	ui32RegVal |= (SGX_BIF_DIR_LIST_INDEX_EDM << EUR_CR_BIF_BANK0_INDEX_TA_SHIFT);
+	#endif
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_BANK0, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_BANK0, ui32RegVal, ui32PDUMPFlags);
+	#endif
+
+	{
+		IMG_UINT32	ui32EDMDirListReg;
+
+
+		#if (SGX_BIF_DIR_LIST_INDEX_EDM == 0)
+		ui32EDMDirListReg = EUR_CR_BIF_DIR_LIST_BASE0;
+		#else
+
+		ui32EDMDirListReg = EUR_CR_BIF_DIR_LIST_BASE1 + 4 * (SGX_BIF_DIR_LIST_INDEX_EDM - 1);
+		#endif
+
+#if defined(FIX_HW_BRN_28011)
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_DIR_LIST_BASE0, psDevInfo->sKernelPDDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT);
+		PDUMPPDREGWITHFLAGS(EUR_CR_BIF_DIR_LIST_BASE0, psDevInfo->sKernelPDDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT, ui32PDUMPFlags, PDUMP_PD_UNIQUETAG);
+#endif
+
+		OSWriteHWReg(psDevInfo->pvRegsBaseKM, ui32EDMDirListReg, psDevInfo->sKernelPDDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT);
+		PDUMPPDREGWITHFLAGS(ui32EDMDirListReg, psDevInfo->sKernelPDDevPAddr.uiAddr>>SGX_MMU_PDE_ADDR_ALIGNSHIFT, ui32PDUMPFlags, PDUMP_PD_UNIQUETAG);
+	}
+
+#ifdef SGX_FEATURE_2D_HARDWARE
+
+	#if ((SGX_2D_HEAP_BASE & ~EUR_CR_BIF_TWOD_REQ_BASE_ADDR_MASK) != 0)
+		#error "SGXReset: SGX_2D_HEAP_BASE doesn't match EUR_CR_BIF_TWOD_REQ_BASE_ADDR_MASK alignment"
+	#endif
+
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_BIF_TWOD_REQ_BASE, SGX_2D_HEAP_BASE);
+	PDUMPREGWITHFLAGS(EUR_CR_BIF_TWOD_REQ_BASE, SGX_2D_HEAP_BASE, ui32PDUMPFlags);
+#endif
+
+
+	SGXResetInvalDC(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+
+	PVR_DPF((PVR_DBG_MESSAGE,"Soft Reset of SGX"));
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+
+
+	ui32RegVal = 0;
+#if defined(SGX_FEATURE_MP)
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_MASTER_SOFT_RESET, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_MASTER_SOFT_RESET, ui32RegVal, ui32PDUMPFlags);
+#endif
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM, EUR_CR_SOFT_RESET, ui32RegVal);
+	PDUMPREGWITHFLAGS(EUR_CR_SOFT_RESET, ui32RegVal, ui32PDUMPFlags);
+
+
+	SGXResetSleep(psDevInfo, ui32PDUMPFlags, IMG_TRUE);
+
+	PDUMPCOMMENTWITHFLAGS(ui32PDUMPFlags, "End of SGX reset sequence\r\n");
+}
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxtransfer.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxtransfer.c
new file mode 100644
index 0000000..9899f92
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxtransfer.c
@@ -0,0 +1,549 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if defined(TRANSFER_QUEUE)
+
+#include <stddef.h>
+
+#include "sgxdefs.h"
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "sgxinfo.h"
+#include "sysconfig.h"
+#include "regpaths.h"
+#include "pdump_km.h"
+#include "mmu.h"
+#include "pvr_bridge.h"
+#include "sgx_bridge_km.h"
+#include "sgxinfokm.h"
+#include "osfunc.h"
+#include "pvr_debug.h"
+#include "sgxutils.h"
+
+int do_wait_vblank(void *display, int headline, int footline);
+
+IMG_EXPORT PVRSRV_ERROR SGXSubmitTransferKM(IMG_HANDLE hDevHandle, PVRSRV_TRANSFER_SGX_KICK *psKick)
+{
+	PVRSRV_KERNEL_MEM_INFO  *psCCBMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psKick->hCCBMemInfo;
+	SGXMKIF_COMMAND sCommand = {0};
+	SGXMKIF_TRANSFERCMD_SHARED *psSharedTransferCmd;
+	PVRSRV_KERNEL_SYNC_INFO *psSyncInfo;
+	PVRSRV_ERROR eError;
+
+
+	if (!CCB_OFFSET_IS_VALID(SGXMKIF_TRANSFERCMD_SHARED, psCCBMemInfo, psKick, ui32SharedCmdCCBOffset))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXSubmitTransferKM: Invalid CCB offset"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	psSharedTransferCmd =  CCB_DATA_FROM_OFFSET(SGXMKIF_TRANSFERCMD_SHARED, psCCBMemInfo, psKick, ui32SharedCmdCCBOffset);
+
+	if (psKick->hTASyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hTASyncInfo;
+
+		psSharedTransferCmd->ui32TASyncWriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+		psSharedTransferCmd->ui32TASyncReadOpsPendingVal  = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		psSharedTransferCmd->sTASyncWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		psSharedTransferCmd->sTASyncReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+	}
+	else
+	{
+		psSharedTransferCmd->sTASyncWriteOpsCompleteDevVAddr.uiAddr = 0;
+		psSharedTransferCmd->sTASyncReadOpsCompleteDevVAddr.uiAddr = 0;
+	}
+
+	if (psKick->h3DSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->h3DSyncInfo;
+
+		psSharedTransferCmd->ui323DSyncWriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+		psSharedTransferCmd->ui323DSyncReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		psSharedTransferCmd->s3DSyncWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		psSharedTransferCmd->s3DSyncReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+	}
+	else
+	{
+		psSharedTransferCmd->s3DSyncWriteOpsCompleteDevVAddr.uiAddr = 0;
+		psSharedTransferCmd->s3DSyncReadOpsCompleteDevVAddr.uiAddr = 0;
+	}
+
+	if ((psKick->ui32Flags & SGXMKIF_TQFLAGS_KEEPPENDING) == 0UL)
+	{
+		if (psKick->ui32NumSrcSync > 0)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[0];
+
+			psSharedTransferCmd->ui32SrcWriteOpPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+			psSharedTransferCmd->ui32SrcReadOpPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+			psSharedTransferCmd->sSrcWriteOpsCompleteDevAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+			psSharedTransferCmd->sSrcReadOpsCompleteDevAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+		}
+
+		if (psKick->ui32NumDstSync > 0)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahDstSyncInfo[0];
+
+			psSharedTransferCmd->ui32DstWriteOpPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+			psSharedTransferCmd->ui32DstReadOpPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+			psSharedTransferCmd->sDstWriteOpsCompleteDevAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+			psSharedTransferCmd->sDstReadOpsCompleteDevAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+
+		}
+
+
+		if (psKick->ui32NumSrcSync > 0)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[0];
+			psSyncInfo->psSyncData->ui32ReadOpsPending++;
+		}
+		if (psKick->ui32NumDstSync > 0)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahDstSyncInfo[0];
+			psSyncInfo->psSyncData->ui32WriteOpsPending++;
+		}
+	}
+
+
+	if (psKick->ui32NumDstSync > 1 || psKick->ui32NumSrcSync  > 1)
+	{
+		PVR_DPF((PVR_DBG_ERROR,
+					"Transfer command doesn't support more than 1 sync object per src/dst\ndst: %d, src: %d",
+					psKick->ui32NumDstSync, psKick->ui32NumSrcSync));
+	}
+
+#if defined(PDUMP)
+	if (PDumpIsCaptureFrameKM()
+	|| ((psKick->ui32PDumpFlags & PDUMP_FLAGS_CONTINUOUS) != 0))
+	{
+		PDUMPCOMMENT("Shared part of transfer command\r\n");
+		PDUMPMEM(psSharedTransferCmd,
+				psCCBMemInfo,
+				psKick->ui32CCBDumpWOff,
+				sizeof(SGXMKIF_TRANSFERCMD_SHARED),
+				psKick->ui32PDumpFlags,
+				MAKEUNIQUETAG(psCCBMemInfo));
+
+		if((psKick->ui32NumSrcSync > 0) && ((psKick->ui32Flags & SGXMKIF_TQFLAGS_KEEPPENDING) == 0UL))
+		{
+			psSyncInfo = psKick->ahSrcSyncInfo[0];
+
+			PDUMPCOMMENT("Hack src surface write op in transfer cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_TRANSFERCMD_SHARED, ui32SrcWriteOpPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Hack src surface read op in transfer cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_TRANSFERCMD_SHARED, ui32SrcReadOpPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastReadOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+
+		}
+		if((psKick->ui32NumDstSync > 0) && ((psKick->ui32Flags & SGXMKIF_TQFLAGS_KEEPPENDING) == 0UL))
+		{
+			psSyncInfo = psKick->ahDstSyncInfo[0];
+
+			PDUMPCOMMENT("Hack dest surface write op in transfer cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_TRANSFERCMD_SHARED, ui32DstWriteOpPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Hack dest surface read op in transfer cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_TRANSFERCMD_SHARED, ui32DstReadOpPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastReadOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+
+		}
+
+
+		if((psKick->ui32NumSrcSync > 0) && ((psKick->ui32Flags & SGXMKIF_TQFLAGS_KEEPPENDING)== 0UL))
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[0];
+			psSyncInfo->psSyncData->ui32LastReadOpDumpVal++;
+		}
+
+		if((psKick->ui32NumDstSync > 0) && ((psKick->ui32Flags & SGXMKIF_TQFLAGS_KEEPPENDING) == 0UL))
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahDstSyncInfo[0];
+			psSyncInfo->psSyncData->ui32LastOpDumpVal++;
+		}
+	}
+#endif
+
+	sCommand.ui32Data[1] = psKick->sHWTransferContextDevVAddr.uiAddr;
+
+	if (psKick->display)
+		do_wait_vblank(psKick->display, psKick->headline, psKick->footline);
+	eError = SGXScheduleCCBCommandKM(hDevHandle, SGXMKIF_CMD_TRANSFER, &sCommand, KERNEL_ID, psKick->ui32PDumpFlags);
+
+	if (eError == PVRSRV_ERROR_RETRY)
+	{
+
+		if ((psKick->ui32Flags & SGXMKIF_TQFLAGS_KEEPPENDING) == 0UL)
+		{
+			if (psKick->ui32NumSrcSync > 0)
+			{
+				psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[0];
+				psSyncInfo->psSyncData->ui32ReadOpsPending--;
+			}
+			if (psKick->ui32NumDstSync > 0)
+			{
+				psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahDstSyncInfo[0];
+				psSyncInfo->psSyncData->ui32WriteOpsPending--;
+			}
+#if defined(PDUMP)
+			if (PDumpIsCaptureFrameKM()
+			|| ((psKick->ui32PDumpFlags & PDUMP_FLAGS_CONTINUOUS) != 0))
+			{
+				if (psKick->ui32NumSrcSync > 0)
+				{
+					psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[0];
+					psSyncInfo->psSyncData->ui32LastReadOpDumpVal--;
+				}
+				if (psKick->ui32NumDstSync > 0)
+				{
+					psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahDstSyncInfo[0];
+					psSyncInfo->psSyncData->ui32LastOpDumpVal--;
+				}
+			}
+#endif
+		}
+
+
+		if (psKick->hTASyncInfo != IMG_NULL)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hTASyncInfo;
+			psSyncInfo->psSyncData->ui32WriteOpsPending--;
+		}
+
+
+		if (psKick->h3DSyncInfo != IMG_NULL)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->h3DSyncInfo;
+			psSyncInfo->psSyncData->ui32WriteOpsPending--;
+		}
+	}
+
+	else if (PVRSRV_OK != eError)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXSubmitTransferKM: SGXScheduleCCBCommandKM failed."));
+		return eError;
+	}
+
+
+#if defined(NO_HARDWARE)
+	if ((psKick->ui32Flags & SGXMKIF_TQFLAGS_NOSYNCUPDATE) == 0)
+	{
+		IMG_UINT32 i;
+
+
+		for(i = 0; i < psKick->ui32NumSrcSync; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[i];
+			psSyncInfo->psSyncData->ui32ReadOpsComplete = psSyncInfo->psSyncData->ui32ReadOpsPending;
+		}
+
+		for(i = 0; i < psKick->ui32NumDstSync; i++)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahDstSyncInfo[i];
+			psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+
+		}
+
+		if (psKick->hTASyncInfo != IMG_NULL)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hTASyncInfo;
+
+			psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+		}
+
+		if (psKick->h3DSyncInfo != IMG_NULL)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->h3DSyncInfo;
+
+			psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+		}
+	}
+#endif
+
+	return eError;
+}
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+IMG_EXPORT PVRSRV_ERROR SGXSubmit2DKM(IMG_HANDLE hDevHandle, PVRSRV_2D_SGX_KICK *psKick)
+
+{
+	PVRSRV_KERNEL_MEM_INFO  *psCCBMemInfo = (PVRSRV_KERNEL_MEM_INFO *)psKick->hCCBMemInfo;
+	SGXMKIF_COMMAND sCommand = {0};
+	SGXMKIF_2DCMD_SHARED *ps2DCmd;
+	PVRSRV_KERNEL_SYNC_INFO *psSyncInfo;
+	PVRSRV_ERROR eError;
+	IMG_UINT32 i;
+
+	if (!CCB_OFFSET_IS_VALID(SGXMKIF_2DCMD_SHARED, psCCBMemInfo, psKick, ui32SharedCmdCCBOffset))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXSubmit2DKM: Invalid CCB offset"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+
+	ps2DCmd =  CCB_DATA_FROM_OFFSET(SGXMKIF_2DCMD_SHARED, psCCBMemInfo, psKick, ui32SharedCmdCCBOffset);
+
+	OSMemSet(ps2DCmd, 0, sizeof(*ps2DCmd));
+
+
+	if (psKick->hTASyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hTASyncInfo;
+
+		ps2DCmd->sTASyncData.ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+		ps2DCmd->sTASyncData.ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		ps2DCmd->sTASyncData.sWriteOpsCompleteDevVAddr 	= psSyncInfo->sWriteOpsCompleteDevVAddr;
+		ps2DCmd->sTASyncData.sReadOpsCompleteDevVAddr 	= psSyncInfo->sReadOpsCompleteDevVAddr;
+	}
+
+
+	if (psKick->h3DSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->h3DSyncInfo;
+
+		ps2DCmd->s3DSyncData.ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending++;
+		ps2DCmd->s3DSyncData.ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		ps2DCmd->s3DSyncData.sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		ps2DCmd->s3DSyncData.sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+	}
+
+
+	ps2DCmd->ui32NumSrcSync = psKick->ui32NumSrcSync;
+	for (i = 0; i < psKick->ui32NumSrcSync; i++)
+	{
+		psSyncInfo = psKick->ahSrcSyncInfo[i];
+
+		ps2DCmd->sSrcSyncData[i].ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+		ps2DCmd->sSrcSyncData[i].ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		ps2DCmd->sSrcSyncData[i].sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		ps2DCmd->sSrcSyncData[i].sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+	}
+
+	if (psKick->hDstSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = psKick->hDstSyncInfo;
+
+		ps2DCmd->sDstSyncData.ui32WriteOpsPendingVal = psSyncInfo->psSyncData->ui32WriteOpsPending;
+		ps2DCmd->sDstSyncData.ui32ReadOpsPendingVal = psSyncInfo->psSyncData->ui32ReadOpsPending;
+
+		ps2DCmd->sDstSyncData.sWriteOpsCompleteDevVAddr = psSyncInfo->sWriteOpsCompleteDevVAddr;
+		ps2DCmd->sDstSyncData.sReadOpsCompleteDevVAddr = psSyncInfo->sReadOpsCompleteDevVAddr;
+	}
+
+
+	for (i = 0; i < psKick->ui32NumSrcSync; i++)
+	{
+		psSyncInfo = psKick->ahSrcSyncInfo[i];
+		psSyncInfo->psSyncData->ui32ReadOpsPending++;
+	}
+
+	if (psKick->hDstSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = psKick->hDstSyncInfo;
+		psSyncInfo->psSyncData->ui32WriteOpsPending++;
+	}
+
+#if defined(PDUMP)
+	if (PDumpIsCaptureFrameKM()
+	|| ((psKick->ui32PDumpFlags & PDUMP_FLAGS_CONTINUOUS) != 0))
+	{
+
+		PDUMPCOMMENT("Shared part of 2D command\r\n");
+		PDUMPMEM(ps2DCmd,
+				psCCBMemInfo,
+				psKick->ui32CCBDumpWOff,
+				sizeof(SGXMKIF_2DCMD_SHARED),
+				psKick->ui32PDumpFlags,
+				MAKEUNIQUETAG(psCCBMemInfo));
+
+		for (i = 0; i < psKick->ui32NumSrcSync; i++)
+		{
+			psSyncInfo = psKick->ahSrcSyncInfo[i];
+
+			PDUMPCOMMENT("Hack src surface write op in 2D cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_2DCMD_SHARED, sSrcSyncData[i].ui32WriteOpsPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Hack src surface read op in 2D cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_2DCMD_SHARED, sSrcSyncData[i].ui32ReadOpsPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastReadOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+		}
+
+		if (psKick->hDstSyncInfo != IMG_NULL)
+		{
+			psSyncInfo = psKick->hDstSyncInfo;
+
+			PDUMPCOMMENT("Hack dest surface write op in 2D cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_2DCMD_SHARED, sDstSyncData.ui32WriteOpsPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+
+			PDUMPCOMMENT("Hack dest surface read op in 2D cmd\r\n");
+			PDUMPMEM(&psSyncInfo->psSyncData->ui32LastReadOpDumpVal,
+					psCCBMemInfo,
+					psKick->ui32CCBDumpWOff + offsetof(SGXMKIF_2DCMD_SHARED, sDstSyncData.ui32ReadOpsPendingVal),
+					sizeof(psSyncInfo->psSyncData->ui32LastReadOpDumpVal),
+					psKick->ui32PDumpFlags,
+					MAKEUNIQUETAG(psCCBMemInfo));
+		}
+
+
+		for (i = 0; i < psKick->ui32NumSrcSync; i++)
+		{
+			psSyncInfo = psKick->ahSrcSyncInfo[i];
+			psSyncInfo->psSyncData->ui32LastReadOpDumpVal++;
+		}
+
+		if (psKick->hDstSyncInfo != IMG_NULL)
+		{
+			psSyncInfo = psKick->hDstSyncInfo;
+			psSyncInfo->psSyncData->ui32LastOpDumpVal++;
+		}
+	}
+#endif
+
+	sCommand.ui32Data[1] = psKick->sHW2DContextDevVAddr.uiAddr;
+
+	eError = SGXScheduleCCBCommandKM(hDevHandle, SGXMKIF_CMD_2D, &sCommand, KERNEL_ID, psKick->ui32PDumpFlags);
+
+	if (eError == PVRSRV_ERROR_RETRY)
+	{
+
+
+#if defined(PDUMP)
+		if (PDumpIsCaptureFrameKM())
+		{
+			for (i = 0; i < psKick->ui32NumSrcSync; i++)
+			{
+				psSyncInfo = psKick->ahSrcSyncInfo[i];
+				psSyncInfo->psSyncData->ui32LastReadOpDumpVal--;
+			}
+
+			if (psKick->hDstSyncInfo != IMG_NULL)
+			{
+				psSyncInfo = psKick->hDstSyncInfo;
+				psSyncInfo->psSyncData->ui32LastOpDumpVal--;
+			}
+		}
+#endif
+
+		for (i = 0; i < psKick->ui32NumSrcSync; i++)
+		{
+			psSyncInfo = psKick->ahSrcSyncInfo[i];
+			psSyncInfo->psSyncData->ui32ReadOpsPending--;
+		}
+
+		if (psKick->hDstSyncInfo != IMG_NULL)
+		{
+			psSyncInfo = psKick->hDstSyncInfo;
+			psSyncInfo->psSyncData->ui32WriteOpsPending--;
+		}
+
+
+		if (psKick->hTASyncInfo != IMG_NULL)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hTASyncInfo;
+
+			psSyncInfo->psSyncData->ui32WriteOpsPending--;
+		}
+
+
+		if (psKick->h3DSyncInfo != IMG_NULL)
+		{
+			psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->h3DSyncInfo;
+
+			psSyncInfo->psSyncData->ui32WriteOpsPending--;
+		}
+	}
+
+
+
+
+#if defined(NO_HARDWARE)
+
+	for(i = 0; i < psKick->ui32NumSrcSync; i++)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->ahSrcSyncInfo[i];
+		psSyncInfo->psSyncData->ui32ReadOpsComplete = psSyncInfo->psSyncData->ui32ReadOpsPending;
+	}
+
+	psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hDstSyncInfo;
+	psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+
+	if (psKick->hTASyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->hTASyncInfo;
+
+		psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+
+	if (psKick->h3DSyncInfo != IMG_NULL)
+	{
+		psSyncInfo = (PVRSRV_KERNEL_SYNC_INFO *)psKick->h3DSyncInfo;
+
+		psSyncInfo->psSyncData->ui32WriteOpsComplete = psSyncInfo->psSyncData->ui32WriteOpsPending;
+	}
+#endif
+
+	return eError;
+}
+#endif
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.c
new file mode 100644
index 0000000..8ffa3fa
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.c
@@ -0,0 +1,1054 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <stddef.h>
+
+#include "sgxdefs.h"
+#include "services_headers.h"
+#include "buffer_manager.h"
+#include "sgxapi_km.h"
+#include "sgxinfo.h"
+#include "sgx_mkif_km.h"
+#include "sysconfig.h"
+#include "pdump_km.h"
+#include "mmu.h"
+#include "pvr_bridge_km.h"
+#include "osfunc.h"
+#include "pvr_debug.h"
+#include "sgxutils.h"
+
+#ifdef __linux__
+#include <linux/tty.h>
+#else
+#include <stdio.h>
+#endif
+
+
+#if defined(SYS_CUSTOM_POWERDOWN)
+PVRSRV_ERROR SysPowerDownMISR(PVRSRV_DEVICE_NODE	* psDeviceNode, IMG_UINT32 ui32CallerID);
+#endif
+
+
+
+IMG_VOID SGXPostActivePowerEvent(PVRSRV_DEVICE_NODE	* psDeviceNode,
+                                 IMG_UINT32           ui32CallerID)
+{
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+	SGXMKIF_HOST_CTL	*psSGXHostCtl = psDevInfo->psSGXHostCtl;
+
+
+	psSGXHostCtl->ui32NumActivePowerEvents++;
+
+	if ((psSGXHostCtl->ui32PowerStatus & PVRSRV_USSE_EDM_POWMAN_POWEROFF_RESTART_IMMEDIATE) != 0)
+	{
+
+
+
+		if (ui32CallerID == ISR_ID)
+		{
+			psDeviceNode->bReProcessDeviceCommandComplete = IMG_TRUE;
+		}
+		else
+		{
+			SGXScheduleProcessQueuesKM(psDeviceNode);
+		}
+	}
+}
+
+
+IMG_VOID SGXTestActivePowerEvent (PVRSRV_DEVICE_NODE	*psDeviceNode,
+								  IMG_UINT32			ui32CallerID)
+{
+	PVRSRV_ERROR		eError = PVRSRV_OK;
+	PVRSRV_SGXDEV_INFO	*psDevInfo = psDeviceNode->pvDevice;
+	SGXMKIF_HOST_CTL	*psSGXHostCtl = psDevInfo->psSGXHostCtl;
+
+	if (((psSGXHostCtl->ui32InterruptFlags & PVRSRV_USSE_EDM_INTERRUPT_ACTIVE_POWER) != 0) &&
+		((psSGXHostCtl->ui32InterruptClearFlags & PVRSRV_USSE_EDM_INTERRUPT_ACTIVE_POWER) == 0))
+	{
+
+		psSGXHostCtl->ui32InterruptClearFlags |= PVRSRV_USSE_EDM_INTERRUPT_ACTIVE_POWER;
+
+
+		PDUMPSUSPEND();
+
+#if defined(SYS_CUSTOM_POWERDOWN)
+
+
+
+		eError = SysPowerDownMISR(psDeviceNode, ui32CallerID);
+#else
+		eError = PVRSRVSetDevicePowerStateKM(psDeviceNode->sDevId.ui32DeviceIndex,
+											 PVRSRV_DEV_POWER_STATE_OFF,
+											 ui32CallerID, IMG_FALSE);
+		if (eError == PVRSRV_OK)
+		{
+			SGXPostActivePowerEvent(psDeviceNode, ui32CallerID);
+		}
+#endif
+		if (eError == PVRSRV_ERROR_RETRY)
+		{
+
+
+			psSGXHostCtl->ui32InterruptClearFlags &= ~PVRSRV_USSE_EDM_INTERRUPT_ACTIVE_POWER;
+			eError = PVRSRV_OK;
+		}
+
+
+		PDUMPRESUME();
+	}
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXTestActivePowerEvent error:%lu", eError));
+	}
+}
+
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(SGXAcquireKernelCCBSlot)
+#endif
+static INLINE SGXMKIF_COMMAND * SGXAcquireKernelCCBSlot(PVRSRV_SGX_CCB_INFO *psCCB)
+{
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		if(((*psCCB->pui32WriteOffset + 1) & 255) != *psCCB->pui32ReadOffset)
+		{
+			return &psCCB->psCommands[*psCCB->pui32WriteOffset];
+		}
+
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+
+	return IMG_NULL;
+}
+
+PVRSRV_ERROR SGXScheduleCCBCommand(PVRSRV_SGXDEV_INFO 	*psDevInfo,
+								   SGXMKIF_CMD_TYPE		eCmdType,
+								   SGXMKIF_COMMAND		*psCommandData,
+								   IMG_UINT32			ui32CallerID,
+								   IMG_UINT32			ui32PDumpFlags)
+{
+	PVRSRV_SGX_CCB_INFO *psKernelCCB;
+	PVRSRV_ERROR eError = PVRSRV_OK;
+	SGXMKIF_COMMAND *psSGXCommand;
+#if defined(PDUMP)
+	IMG_VOID *pvDumpCommand;
+	IMG_BOOL bPDumpIsSuspended = PDumpIsSuspended();
+#else
+	PVR_UNREFERENCED_PARAMETER(ui32CallerID);
+	PVR_UNREFERENCED_PARAMETER(ui32PDumpFlags);
+#endif
+
+#if defined(FIX_HW_BRN_28889)
+
+
+
+
+	if ( (eCmdType != SGXMKIF_CMD_PROCESS_QUEUES) &&
+		 ((psDevInfo->ui32CacheControl & SGXMKIF_CC_INVAL_DATA) != 0) &&
+		 ((psDevInfo->ui32CacheControl & (SGXMKIF_CC_INVAL_BIF_PT | SGXMKIF_CC_INVAL_BIF_PD)) != 0))
+	{
+	#if defined(PDUMP)
+		PVRSRV_KERNEL_MEM_INFO	*psSGXHostCtlMemInfo = psDevInfo->psKernelSGXHostCtlMemInfo;
+	#endif
+		SGXMKIF_HOST_CTL	*psSGXHostCtl = psDevInfo->psSGXHostCtl;
+		SGXMKIF_COMMAND		sCacheCommand = {0};
+
+		eError = SGXScheduleCCBCommand(psDevInfo,
+									   SGXMKIF_CMD_PROCESS_QUEUES,
+									   &sCacheCommand,
+									   ui32CallerID,
+									   ui32PDumpFlags);
+		if (eError != PVRSRV_OK)
+		{
+			goto Exit;
+		}
+
+
+		#if !defined(NO_HARDWARE)
+		if(PollForValueKM(&psSGXHostCtl->ui32InvalStatus,
+						  PVRSRV_USSE_EDM_BIF_INVAL_COMPLETE,
+						  PVRSRV_USSE_EDM_BIF_INVAL_COMPLETE,
+						  2 * MAX_HW_TIME_US/WAIT_TRY_COUNT,
+						  WAIT_TRY_COUNT) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXScheduleCCBCommand: Wait for uKernel to Invalidate BIF cache failed"));
+			PVR_DBG_BREAK;
+		}
+		#endif
+
+		psSGXHostCtl->ui32InvalStatus &= ~(PVRSRV_USSE_EDM_BIF_INVAL_COMPLETE);
+
+		#if defined(PDUMP)
+		if ((ui32CallerID != ISR_ID) && (bPDumpIsSuspended == IMG_FALSE))
+		{
+
+			PDUMPCOMMENTWITHFLAGS(0, "Host Control - Poll for BIF cache invalidate request to complete");
+			PDUMPMEMPOL(psSGXHostCtlMemInfo,
+						offsetof(SGXMKIF_HOST_CTL, ui32InvalStatus),
+						PVRSRV_USSE_EDM_BIF_INVAL_COMPLETE,
+						PVRSRV_USSE_EDM_BIF_INVAL_COMPLETE,
+						PDUMP_POLL_OPERATOR_EQUAL,
+						0,
+						MAKEUNIQUETAG(psSGXHostCtlMemInfo));
+			PDUMPMEM(IMG_NULL, psSGXHostCtlMemInfo, offsetof(SGXMKIF_HOST_CTL, ui32CleanupStatus), sizeof(IMG_UINT32), 0, MAKEUNIQUETAG(psSGXHostCtlMemInfo));
+		}
+		#endif
+	}
+#endif
+
+	psKernelCCB = psDevInfo->psKernelCCBInfo;
+
+	psSGXCommand = SGXAcquireKernelCCBSlot(psKernelCCB);
+
+
+	if(!psSGXCommand)
+	{
+		eError = PVRSRV_ERROR_TIMEOUT;
+		goto Exit;
+	}
+
+
+	psCommandData->ui32CacheControl = psDevInfo->ui32CacheControl;
+
+#if defined(PDUMP)
+
+	psDevInfo->sPDContext.ui32CacheControl |= psDevInfo->ui32CacheControl;
+#endif
+
+
+	psDevInfo->ui32CacheControl = 0;
+
+
+	*psSGXCommand = *psCommandData;
+
+	if (eCmdType >= SGXMKIF_CMD_MAX)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXScheduleCCBCommandKM: Unknown command type: %d", eCmdType)) ;
+		eError = PVRSRV_ERROR_GENERIC;
+		goto Exit;
+	}
+
+#if defined(SUPPORT_CPU_CACHED_BUFFERS)
+	{
+		SYS_DATA *psSysData;
+
+		SysAcquireData(&psSysData);
+
+		if (psSysData->bFlushAll)
+		{
+			OSFlushCPUCacheKM();
+
+			psSysData->bFlushAll = IMG_FALSE;
+		}
+	}
+#endif
+
+	psSGXCommand->ui32ServiceAddress = psDevInfo->aui32HostKickAddr[eCmdType];
+
+#if defined(PDUMP)
+	if ((ui32CallerID != ISR_ID) && (bPDumpIsSuspended == IMG_FALSE))
+	{
+
+		PDUMPCOMMENTWITHFLAGS(ui32PDumpFlags, "Poll for space in the Kernel CCB\r\n");
+		PDUMPMEMPOL(psKernelCCB->psCCBCtlMemInfo,
+					offsetof(PVRSRV_SGX_CCB_CTL, ui32ReadOffset),
+					(psKernelCCB->ui32CCBDumpWOff + 1) & 0xff,
+					0xff,
+					PDUMP_POLL_OPERATOR_NOTEQUAL,
+					ui32PDumpFlags,
+					MAKEUNIQUETAG(psKernelCCB->psCCBCtlMemInfo));
+
+		PDUMPCOMMENTWITHFLAGS(ui32PDumpFlags, "Kernel CCB command\r\n");
+		pvDumpCommand = (IMG_VOID *)((IMG_UINT8 *)psKernelCCB->psCCBMemInfo->pvLinAddrKM + (*psKernelCCB->pui32WriteOffset * sizeof(SGXMKIF_COMMAND)));
+
+		PDUMPMEM(pvDumpCommand,
+					psKernelCCB->psCCBMemInfo,
+					psKernelCCB->ui32CCBDumpWOff * sizeof(SGXMKIF_COMMAND),
+					sizeof(SGXMKIF_COMMAND),
+					ui32PDumpFlags,
+					MAKEUNIQUETAG(psKernelCCB->psCCBMemInfo));
+
+
+		PDUMPMEM(&psDevInfo->sPDContext.ui32CacheControl,
+					psKernelCCB->psCCBMemInfo,
+					psKernelCCB->ui32CCBDumpWOff * sizeof(SGXMKIF_COMMAND) +
+					offsetof(SGXMKIF_COMMAND, ui32CacheControl),
+					sizeof(IMG_UINT32),
+					ui32PDumpFlags,
+					MAKEUNIQUETAG(psKernelCCB->psCCBMemInfo));
+
+		if (PDumpIsCaptureFrameKM()
+		|| ((ui32PDumpFlags & PDUMP_FLAGS_CONTINUOUS) != 0))
+		{
+
+			psDevInfo->sPDContext.ui32CacheControl = 0;
+		}
+	}
+#endif
+
+#if defined(FIX_HW_BRN_26620) && defined(SGX_FEATURE_SYSTEM_CACHE) && !defined(SGX_BYPASS_SYSTEM_CACHE)
+
+	eError = PollForValueKM (psKernelCCB->pui32ReadOffset,
+								*psKernelCCB->pui32WriteOffset,
+								0xFF,
+								MAX_HW_TIME_US/WAIT_TRY_COUNT,
+								WAIT_TRY_COUNT);
+	if (eError != PVRSRV_OK)
+	{
+		eError = PVRSRV_ERROR_TIMEOUT;
+		goto Exit;
+	}
+#endif
+
+
+
+	*psKernelCCB->pui32WriteOffset = (*psKernelCCB->pui32WriteOffset + 1) & 255;
+
+#if defined(PDUMP)
+	if ((ui32CallerID != ISR_ID) && (bPDumpIsSuspended == IMG_FALSE))
+	{
+	#if defined(FIX_HW_BRN_26620) && defined(SGX_FEATURE_SYSTEM_CACHE) && !defined(SGX_BYPASS_SYSTEM_CACHE)
+		PDUMPCOMMENTWITHFLAGS(ui32PDumpFlags, "Poll for previous Kernel CCB CMD to be read\r\n");
+		PDUMPMEMPOL(psKernelCCB->psCCBCtlMemInfo,
+					offsetof(PVRSRV_SGX_CCB_CTL, ui32ReadOffset),
+					(psKernelCCB->ui32CCBDumpWOff),
+					0xFF,
+					PDUMP_POLL_OPERATOR_EQUAL,
+					ui32PDumpFlags,
+					MAKEUNIQUETAG(psKernelCCB->psCCBCtlMemInfo));
+	#endif
+
+		if (PDumpIsCaptureFrameKM()
+		|| ((ui32PDumpFlags & PDUMP_FLAGS_CONTINUOUS) != 0))
+		{
+			psKernelCCB->ui32CCBDumpWOff = (psKernelCCB->ui32CCBDumpWOff + 1) & 0xFF;
+			psDevInfo->ui32KernelCCBEventKickerDumpVal = (psDevInfo->ui32KernelCCBEventKickerDumpVal + 1) & 0xFF;
+		}
+
+		PDUMPCOMMENTWITHFLAGS(ui32PDumpFlags, "Kernel CCB write offset\r\n");
+		PDUMPMEM(&psKernelCCB->ui32CCBDumpWOff,
+				 psKernelCCB->psCCBCtlMemInfo,
+				 offsetof(PVRSRV_SGX_CCB_CTL, ui32WriteOffset),
+				 sizeof(IMG_UINT32),
+				 ui32PDumpFlags,
+				 MAKEUNIQUETAG(psKernelCCB->psCCBCtlMemInfo));
+		PDUMPCOMMENTWITHFLAGS(ui32PDumpFlags, "Kernel CCB event kicker\r\n");
+		PDUMPMEM(&psDevInfo->ui32KernelCCBEventKickerDumpVal,
+				 psDevInfo->psKernelCCBEventKickerMemInfo,
+				 0,
+				 sizeof(IMG_UINT32),
+				 ui32PDumpFlags,
+				 MAKEUNIQUETAG(psDevInfo->psKernelCCBEventKickerMemInfo));
+		PDUMPCOMMENTWITHFLAGS(ui32PDumpFlags, "Kick the SGX microkernel\r\n");
+	#if defined(FIX_HW_BRN_26620) && defined(SGX_FEATURE_SYSTEM_CACHE) && !defined(SGX_BYPASS_SYSTEM_CACHE)
+		PDUMPREGWITHFLAGS(SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK2, 0), EUR_CR_EVENT_KICK2_NOW_MASK, ui32PDumpFlags);
+	#else
+		PDUMPREGWITHFLAGS(SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK, 0), EUR_CR_EVENT_KICK_NOW_MASK, ui32PDumpFlags);
+	#endif
+	}
+#endif
+
+	*psDevInfo->pui32KernelCCBEventKicker = (*psDevInfo->pui32KernelCCBEventKicker + 1) & 0xFF;
+#if defined(FIX_HW_BRN_26620) && defined(SGX_FEATURE_SYSTEM_CACHE) && !defined(SGX_BYPASS_SYSTEM_CACHE)
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM,
+				SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK2, 0),
+				EUR_CR_EVENT_KICK2_NOW_MASK);
+#else
+	OSWriteHWReg(psDevInfo->pvRegsBaseKM,
+				SGX_MP_CORE_SELECT(EUR_CR_EVENT_KICK, 0),
+				EUR_CR_EVENT_KICK_NOW_MASK);
+#endif
+
+#if defined(NO_HARDWARE)
+
+	*psKernelCCB->pui32ReadOffset = (*psKernelCCB->pui32ReadOffset + 1) & 255;
+#endif
+
+Exit:
+	return eError;
+}
+
+
+PVRSRV_ERROR SGXScheduleCCBCommandKM(PVRSRV_DEVICE_NODE		*psDeviceNode,
+									 SGXMKIF_CMD_TYPE		eCmdType,
+									 SGXMKIF_COMMAND		*psCommandData,
+									 IMG_UINT32				ui32CallerID,
+									 IMG_UINT32				ui32PDumpFlags)
+{
+	PVRSRV_ERROR		eError;
+	PVRSRV_SGXDEV_INFO 	*psDevInfo = psDeviceNode->pvDevice;
+
+
+	PDUMPSUSPEND();
+
+
+	eError = PVRSRVSetDevicePowerStateKM(psDeviceNode->sDevId.ui32DeviceIndex,
+										 PVRSRV_DEV_POWER_STATE_ON,
+										 ui32CallerID,
+										 IMG_TRUE);
+
+	PDUMPRESUME();
+
+	if (eError == PVRSRV_OK)
+	{
+		psDeviceNode->bReProcessDeviceCommandComplete = IMG_FALSE;
+	}
+	else
+	{
+		if (eError == PVRSRV_ERROR_RETRY)
+		{
+			if (ui32CallerID == ISR_ID)
+			{
+
+
+
+				psDeviceNode->bReProcessDeviceCommandComplete = IMG_TRUE;
+				eError = PVRSRV_OK;
+			}
+			else
+			{
+
+
+			}
+		}
+		else
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXScheduleCCBCommandKM failed to acquire lock - "
+					 "ui32CallerID:%ld eError:%lu", ui32CallerID, eError));
+		}
+
+		return eError;
+	}
+
+	eError = SGXScheduleCCBCommand(psDevInfo, eCmdType, psCommandData, ui32CallerID, ui32PDumpFlags);
+
+	PVRSRVPowerUnlock(ui32CallerID);
+
+	if (ui32CallerID != ISR_ID)
+	{
+
+
+
+		SGXTestActivePowerEvent(psDeviceNode, ui32CallerID);
+	}
+
+	return eError;
+}
+
+
+#define print_error(function, variable)\
+{\
+	printk(KERN_ERR "ERROR!\nERROR!\nERROR!\n");\
+	printk(KERN_ERR "ERROR: %s() about to dereference a NULL pointer!\n", function);\
+	printk(KERN_ERR "  %s is NULL\n", variable);\
+	printk(KERN_ERR "  The client-side program needs to call SrvInit()\n");\
+	printk(KERN_ERR "  Working-around error by exiting the function early\n");\
+}
+
+
+PVRSRV_ERROR SGXScheduleProcessQueuesKM(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	/* Note: If the client-side code doesn't call SrvInit(), the normal
+	 * implementation of this function will chase a NULL pointer.  The
+	 * workaround is to use an extra set of curly braces, and then to check the
+	 * pointers normally used during the variable initialization code.  if a
+	 * problem is found, exit early and provide a helpful error message in the
+	 * kernel log file.
+	 */
+	if (NULL == psDeviceNode) {
+		print_error(__FUNCTION__, "psDeviceNode");
+		return PVRSRV_OK;
+	} else {
+		PVRSRV_SGXDEV_INFO 	*psDevInfo = psDeviceNode->pvDevice;
+		if (NULL == psDevInfo) {
+			print_error(__FUNCTION__, "psDevInfo");
+			return PVRSRV_OK;
+		}
+		if (NULL == psDevInfo->psKernelSGXHostCtlMemInfo) {
+			print_error(__FUNCTION__, "psDevInfo->psKernelSGXHostCtlMemInfo");
+			return PVRSRV_OK;
+		}
+		if (NULL == psDevInfo->psKernelSGXHostCtlMemInfo->pvLinAddrKM) {
+			print_error(__FUNCTION__,
+				"psDevInfo->psKernelSGXHostCtlMemInfo->pvLinAddrKM");
+			return PVRSRV_OK;
+		}
+	}
+	/* Below starts the extra set of curly-braces, and the original function: */
+{
+	PVRSRV_ERROR 		eError;
+	PVRSRV_SGXDEV_INFO 	*psDevInfo = psDeviceNode->pvDevice;
+	SGXMKIF_HOST_CTL	*psHostCtl = psDevInfo->psKernelSGXHostCtlMemInfo->pvLinAddrKM;
+	IMG_UINT32		ui32PowerStatus;
+	SGXMKIF_COMMAND		sCommand = {0};
+
+	ui32PowerStatus = psHostCtl->ui32PowerStatus;
+	if ((ui32PowerStatus & PVRSRV_USSE_EDM_POWMAN_NO_WORK) != 0)
+	{
+
+		return PVRSRV_OK;
+	}
+
+	eError = SGXScheduleCCBCommandKM(psDeviceNode, SGXMKIF_CMD_PROCESS_QUEUES, &sCommand, ISR_ID, 0);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SGXScheduleProcessQueuesKM failed to schedule CCB command: %lu", eError));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+}
+
+
+IMG_BOOL SGXIsDevicePowered(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	return PVRSRVIsDevicePowered(psDeviceNode->sDevId.ui32DeviceIndex);
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGXGetInternalDevInfoKM(IMG_HANDLE hDevCookie,
+									SGX_INTERNAL_DEVINFO *psSGXInternalDevInfo)
+{
+	PVRSRV_SGXDEV_INFO *psDevInfo = (PVRSRV_SGXDEV_INFO *)((PVRSRV_DEVICE_NODE *)hDevCookie)->pvDevice;
+
+	psSGXInternalDevInfo->ui32Flags = psDevInfo->ui32Flags;
+	psSGXInternalDevInfo->bForcePTOff = (IMG_BOOL)psDevInfo->bForcePTOff;
+
+
+	psSGXInternalDevInfo->hHostCtlKernelMemInfoHandle =
+		(IMG_HANDLE)psDevInfo->psKernelSGXHostCtlMemInfo;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_VOID SGXCleanupRequest(PVRSRV_DEVICE_NODE	*psDeviceNode,
+						   IMG_DEV_VIRTADDR		*psHWDataDevVAddr,
+						   IMG_UINT32			ui32CleanupType)
+{
+	/* Note: If the client-side code doesn't call SrvInit(), the normal
+	 * implementation of this function will chase a NULL pointer.  The
+	 * workaround is to use an extra set of curly braces, and then to check the
+	 * pointers normally used during the variable initialization code.  if a
+	 * problem is found, exit early and provide a helpful error message in the
+	 * kernel log file.
+	 */
+	if (NULL == psDeviceNode) {
+		print_error(__FUNCTION__, "psDeviceNode");
+		return;
+	} else {
+		PVRSRV_SGXDEV_INFO 	*psSGXDevInfo = psDeviceNode->pvDevice;
+		if (NULL == psSGXDevInfo) {
+			print_error(__FUNCTION__, "psSGXDevInfo");
+			return;
+		}
+		if (NULL == psSGXDevInfo->psKernelSGXHostCtlMemInfo) {
+			print_error(__FUNCTION__,"psSGXDevInfo->psKernelSGXHostCtlMemInfo");
+			return;
+		} else {
+			PVRSRV_KERNEL_MEM_INFO	*psSGXHostCtlMemInfo =
+				psSGXDevInfo->psKernelSGXHostCtlMemInfo;
+			if (NULL == psSGXHostCtlMemInfo->pvLinAddrKM) {
+				print_error(__FUNCTION__, "psSGXHostCtlMemInfo->pvLinAddrKM");
+				return;
+			}
+		}
+	}
+	/* Below starts the extra set of curly-braces, and the original function: */
+{
+	PVRSRV_ERROR			eError;
+	PVRSRV_SGXDEV_INFO		*psSGXDevInfo = psDeviceNode->pvDevice;
+	PVRSRV_KERNEL_MEM_INFO	*psSGXHostCtlMemInfo = psSGXDevInfo->psKernelSGXHostCtlMemInfo;
+	SGXMKIF_HOST_CTL		*psSGXHostCtl = psSGXHostCtlMemInfo->pvLinAddrKM;
+
+	if ((psSGXHostCtl->ui32PowerStatus & PVRSRV_USSE_EDM_POWMAN_NO_WORK) != 0)
+	{
+
+	}
+	else
+	{
+		SGXMKIF_COMMAND		sCommand = {0};
+
+		PDUMPCOMMENTWITHFLAGS(0, "Request ukernel resouce clean-up");
+		sCommand.ui32Data[0] = ui32CleanupType;
+		sCommand.ui32Data[1] = (psHWDataDevVAddr == IMG_NULL) ? 0 : psHWDataDevVAddr->uiAddr;
+
+		eError = SGXScheduleCCBCommandKM(psDeviceNode, SGXMKIF_CMD_CLEANUP, &sCommand, KERNEL_ID, 0);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXCleanupRequest: Failed to submit clean-up command"));
+			PVR_DBG_BREAK;
+		}
+
+
+		#if !defined(NO_HARDWARE)
+		if(PollForValueKM(&psSGXHostCtl->ui32CleanupStatus,
+						  PVRSRV_USSE_EDM_CLEANUPCMD_COMPLETE,
+						  PVRSRV_USSE_EDM_CLEANUPCMD_COMPLETE,
+						  MAX_HW_TIME_US/WAIT_TRY_COUNT,
+						  WAIT_TRY_COUNT) != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SGXCleanupRequest: Wait for uKernel to clean up failed"));
+			PVR_DBG_BREAK;
+		}
+		#endif
+
+		#if defined(PDUMP)
+
+		PDUMPCOMMENTWITHFLAGS(0, "Host Control - Poll for clean-up request to complete");
+		PDUMPMEMPOL(psSGXHostCtlMemInfo,
+					offsetof(SGXMKIF_HOST_CTL, ui32CleanupStatus),
+					PVRSRV_USSE_EDM_CLEANUPCMD_COMPLETE,
+					PVRSRV_USSE_EDM_CLEANUPCMD_COMPLETE,
+					PDUMP_POLL_OPERATOR_EQUAL,
+					0,
+					MAKEUNIQUETAG(psSGXHostCtlMemInfo));
+		#endif
+
+		psSGXHostCtl->ui32CleanupStatus &= ~(PVRSRV_USSE_EDM_CLEANUPCMD_COMPLETE);
+		PDUMPMEM(IMG_NULL, psSGXHostCtlMemInfo, offsetof(SGXMKIF_HOST_CTL, ui32CleanupStatus), sizeof(IMG_UINT32), 0, MAKEUNIQUETAG(psSGXHostCtlMemInfo));
+
+	#if defined(SGX_FEATURE_SYSTEM_CACHE)
+		psSGXDevInfo->ui32CacheControl |= (SGXMKIF_CC_INVAL_BIF_SL | SGXMKIF_CC_INVAL_DATA);
+	#else
+		psSGXDevInfo->ui32CacheControl |= SGXMKIF_CC_INVAL_DATA;
+	#endif
+	}
+}
+}
+
+
+typedef struct _SGX_HW_RENDER_CONTEXT_CLEANUP_
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	IMG_DEV_VIRTADDR sHWRenderContextDevVAddr;
+	IMG_HANDLE hBlockAlloc;
+	PRESMAN_ITEM psResItem;
+} SGX_HW_RENDER_CONTEXT_CLEANUP;
+
+
+static PVRSRV_ERROR SGXCleanupHWRenderContextCallback(IMG_PVOID		pvParam,
+													  IMG_UINT32	ui32Param)
+{
+	SGX_HW_RENDER_CONTEXT_CLEANUP *psCleanup = pvParam;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	SGXCleanupRequest(psCleanup->psDeviceNode,
+					  &psCleanup->sHWRenderContextDevVAddr,
+					  PVRSRV_CLEANUPCMD_RC);
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  sizeof(SGX_HW_RENDER_CONTEXT_CLEANUP),
+			  psCleanup,
+			  psCleanup->hBlockAlloc);
+
+
+	return PVRSRV_OK;
+}
+
+typedef struct _SGX_HW_TRANSFER_CONTEXT_CLEANUP_
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	IMG_DEV_VIRTADDR sHWTransferContextDevVAddr;
+	IMG_HANDLE hBlockAlloc;
+	PRESMAN_ITEM psResItem;
+} SGX_HW_TRANSFER_CONTEXT_CLEANUP;
+
+
+static PVRSRV_ERROR SGXCleanupHWTransferContextCallback(IMG_PVOID	pvParam,
+														IMG_UINT32	ui32Param)
+{
+	SGX_HW_TRANSFER_CONTEXT_CLEANUP *psCleanup = (SGX_HW_TRANSFER_CONTEXT_CLEANUP *)pvParam;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	SGXCleanupRequest(psCleanup->psDeviceNode,
+					  &psCleanup->sHWTransferContextDevVAddr,
+					  PVRSRV_CLEANUPCMD_TC);
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  sizeof(SGX_HW_TRANSFER_CONTEXT_CLEANUP),
+			  psCleanup,
+			  psCleanup->hBlockAlloc);
+
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+IMG_HANDLE SGXRegisterHWRenderContextKM(IMG_HANDLE				psDeviceNode,
+										IMG_DEV_VIRTADDR		*psHWRenderContextDevVAddr,
+										PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hBlockAlloc;
+	SGX_HW_RENDER_CONTEXT_CLEANUP *psCleanup;
+	PRESMAN_ITEM psResItem;
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(SGX_HW_RENDER_CONTEXT_CLEANUP),
+						(IMG_VOID **)&psCleanup,
+						&hBlockAlloc,
+						"SGX Hardware Render Context Cleanup");
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXRegisterHWRenderContextKM: Couldn't allocate memory for SGX_HW_RENDER_CONTEXT_CLEANUP structure"));
+		return IMG_NULL;
+	}
+
+	psCleanup->hBlockAlloc = hBlockAlloc;
+	psCleanup->psDeviceNode = psDeviceNode;
+	psCleanup->sHWRenderContextDevVAddr = *psHWRenderContextDevVAddr;
+
+	psResItem = ResManRegisterRes(psPerProc->hResManContext,
+								  RESMAN_TYPE_HW_RENDER_CONTEXT,
+								  (IMG_VOID *)psCleanup,
+								  0,
+								  &SGXCleanupHWRenderContextCallback);
+
+	if (psResItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXRegisterHWRenderContextKM: ResManRegisterRes failed"));
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(SGX_HW_RENDER_CONTEXT_CLEANUP),
+				  psCleanup,
+				  psCleanup->hBlockAlloc);
+
+
+		return IMG_NULL;
+	}
+
+	psCleanup->psResItem = psResItem;
+
+	return (IMG_HANDLE)psCleanup;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGXUnregisterHWRenderContextKM(IMG_HANDLE hHWRenderContext)
+{
+	PVRSRV_ERROR eError;
+	SGX_HW_RENDER_CONTEXT_CLEANUP *psCleanup;
+
+	PVR_ASSERT(hHWRenderContext != IMG_NULL);
+
+	psCleanup = (SGX_HW_RENDER_CONTEXT_CLEANUP *)hHWRenderContext;
+
+	if (psCleanup == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXUnregisterHWRenderContextKM: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	eError = ResManFreeResByPtr(psCleanup->psResItem);
+
+	return eError;
+}
+
+
+IMG_EXPORT
+IMG_HANDLE SGXRegisterHWTransferContextKM(IMG_HANDLE				psDeviceNode,
+										  IMG_DEV_VIRTADDR			*psHWTransferContextDevVAddr,
+										  PVRSRV_PER_PROCESS_DATA	*psPerProc)
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hBlockAlloc;
+	SGX_HW_TRANSFER_CONTEXT_CLEANUP *psCleanup;
+	PRESMAN_ITEM psResItem;
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(SGX_HW_TRANSFER_CONTEXT_CLEANUP),
+						(IMG_VOID **)&psCleanup,
+						&hBlockAlloc,
+						"SGX Hardware Transfer Context Cleanup");
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXRegisterHWTransferContextKM: Couldn't allocate memory for SGX_HW_TRANSFER_CONTEXT_CLEANUP structure"));
+		return IMG_NULL;
+	}
+
+	psCleanup->hBlockAlloc = hBlockAlloc;
+	psCleanup->psDeviceNode = psDeviceNode;
+	psCleanup->sHWTransferContextDevVAddr = *psHWTransferContextDevVAddr;
+
+	psResItem = ResManRegisterRes(psPerProc->hResManContext,
+								  RESMAN_TYPE_HW_TRANSFER_CONTEXT,
+								  psCleanup,
+								  0,
+								  &SGXCleanupHWTransferContextCallback);
+
+	if (psResItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXRegisterHWTransferContextKM: ResManRegisterRes failed"));
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(SGX_HW_TRANSFER_CONTEXT_CLEANUP),
+				  psCleanup,
+				  psCleanup->hBlockAlloc);
+
+
+		return IMG_NULL;
+	}
+
+	psCleanup->psResItem = psResItem;
+
+	return (IMG_HANDLE)psCleanup;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGXUnregisterHWTransferContextKM(IMG_HANDLE hHWTransferContext)
+{
+	PVRSRV_ERROR eError;
+	SGX_HW_TRANSFER_CONTEXT_CLEANUP *psCleanup;
+
+	PVR_ASSERT(hHWTransferContext != IMG_NULL);
+
+	psCleanup = (SGX_HW_TRANSFER_CONTEXT_CLEANUP *)hHWTransferContext;
+
+	if (psCleanup == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXUnregisterHWTransferContextKM: invalid parameter"));
+		return PVRSRV_ERROR_INVALID_PARAMS;
+	}
+
+	eError = ResManFreeResByPtr(psCleanup->psResItem);
+
+	return eError;
+}
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+typedef struct _SGX_HW_2D_CONTEXT_CLEANUP_
+{
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+	IMG_DEV_VIRTADDR sHW2DContextDevVAddr;
+	IMG_HANDLE hBlockAlloc;
+	PRESMAN_ITEM psResItem;
+} SGX_HW_2D_CONTEXT_CLEANUP;
+
+static PVRSRV_ERROR SGXCleanupHW2DContextCallback(IMG_PVOID pvParam, IMG_UINT32 ui32Param)
+{
+	SGX_HW_2D_CONTEXT_CLEANUP *psCleanup = (SGX_HW_2D_CONTEXT_CLEANUP *)pvParam;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	SGXCleanupRequest(psCleanup->psDeviceNode,
+					  &psCleanup->sHW2DContextDevVAddr,
+					  PVRSRV_CLEANUPCMD_2DC);
+
+	OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+			  sizeof(SGX_HW_2D_CONTEXT_CLEANUP),
+			  psCleanup,
+			  psCleanup->hBlockAlloc);
+
+
+	return PVRSRV_OK;
+}
+
+IMG_EXPORT
+IMG_HANDLE SGXRegisterHW2DContextKM(IMG_HANDLE				psDeviceNode,
+									IMG_DEV_VIRTADDR		*psHW2DContextDevVAddr,
+									PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hBlockAlloc;
+	SGX_HW_2D_CONTEXT_CLEANUP *psCleanup;
+	PRESMAN_ITEM psResItem;
+
+	eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+						sizeof(SGX_HW_2D_CONTEXT_CLEANUP),
+						(IMG_VOID **)&psCleanup,
+						&hBlockAlloc,
+						"SGX Hardware 2D Context Cleanup");
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXRegisterHW2DContextKM: Couldn't allocate memory for SGX_HW_2D_CONTEXT_CLEANUP structure"));
+		return IMG_NULL;
+	}
+
+	psCleanup->hBlockAlloc = hBlockAlloc;
+	psCleanup->psDeviceNode = psDeviceNode;
+	psCleanup->sHW2DContextDevVAddr = *psHW2DContextDevVAddr;
+
+	psResItem = ResManRegisterRes(psPerProc->hResManContext,
+								  RESMAN_TYPE_HW_2D_CONTEXT,
+								  psCleanup,
+								  0,
+								  &SGXCleanupHW2DContextCallback);
+
+	if (psResItem == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "SGXRegisterHW2DContextKM: ResManRegisterRes failed"));
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+				  sizeof(SGX_HW_2D_CONTEXT_CLEANUP),
+				  psCleanup,
+				  psCleanup->hBlockAlloc);
+
+
+		return IMG_NULL;
+	}
+
+	psCleanup->psResItem = psResItem;
+
+	return (IMG_HANDLE)psCleanup;
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGXUnregisterHW2DContextKM(IMG_HANDLE hHW2DContext)
+{
+	PVRSRV_ERROR eError;
+	SGX_HW_2D_CONTEXT_CLEANUP *psCleanup;
+
+	PVR_ASSERT(hHW2DContext != IMG_NULL);
+
+	if (hHW2DContext == IMG_NULL)
+	{
+		return (PVRSRV_ERROR_INVALID_PARAMS);
+	}
+
+	psCleanup = (SGX_HW_2D_CONTEXT_CLEANUP *)hHW2DContext;
+
+	eError = ResManFreeResByPtr(psCleanup->psResItem);
+
+	return eError;
+}
+#endif
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(SGX2DQuerySyncOpsComplete)
+#endif
+static INLINE
+IMG_BOOL SGX2DQuerySyncOpsComplete(PVRSRV_KERNEL_SYNC_INFO	*psSyncInfo,
+								   IMG_UINT32				ui32ReadOpsPending,
+								   IMG_UINT32				ui32WriteOpsPending)
+{
+	PVRSRV_SYNC_DATA *psSyncData = psSyncInfo->psSyncData;
+
+	return (IMG_BOOL)(
+					  (psSyncData->ui32ReadOpsComplete >= ui32ReadOpsPending) &&
+					  (psSyncData->ui32WriteOpsComplete >= ui32WriteOpsPending)
+					 );
+}
+
+IMG_EXPORT
+PVRSRV_ERROR SGX2DQueryBlitsCompleteKM(PVRSRV_SGXDEV_INFO	*psDevInfo,
+									   PVRSRV_KERNEL_SYNC_INFO *psSyncInfo,
+									   IMG_BOOL bWaitForComplete)
+{
+	IMG_UINT32	ui32ReadOpsPending, ui32WriteOpsPending;
+
+	PVR_UNREFERENCED_PARAMETER(psDevInfo);
+
+	PVR_DPF((PVR_DBG_CALLTRACE, "SGX2DQueryBlitsCompleteKM: Start"));
+
+	ui32ReadOpsPending = psSyncInfo->psSyncData->ui32ReadOpsPending;
+	ui32WriteOpsPending = psSyncInfo->psSyncData->ui32WriteOpsPending;
+
+	if(SGX2DQuerySyncOpsComplete(psSyncInfo, ui32ReadOpsPending, ui32WriteOpsPending))
+	{
+
+		PVR_DPF((PVR_DBG_CALLTRACE, "SGX2DQueryBlitsCompleteKM: No wait. Blits complete."));
+		return PVRSRV_OK;
+	}
+
+
+	if (!bWaitForComplete)
+	{
+
+		PVR_DPF((PVR_DBG_CALLTRACE, "SGX2DQueryBlitsCompleteKM: No wait. Ops pending."));
+		return PVRSRV_ERROR_CMD_NOT_PROCESSED;
+	}
+
+
+	PVR_DPF((PVR_DBG_MESSAGE, "SGX2DQueryBlitsCompleteKM: Ops pending. Start polling."));
+
+	LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
+	{
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+
+		if(SGX2DQuerySyncOpsComplete(psSyncInfo, ui32ReadOpsPending, ui32WriteOpsPending))
+		{
+
+			PVR_DPF((PVR_DBG_CALLTRACE, "SGX2DQueryBlitsCompleteKM: Wait over.  Blits complete."));
+			return PVRSRV_OK;
+		}
+
+		OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
+	} END_LOOP_UNTIL_TIMEOUT();
+
+
+	PVR_DPF((PVR_DBG_ERROR,"SGX2DQueryBlitsCompleteKM: Timed out. Ops pending."));
+
+#if defined(DEBUG)
+	{
+		PVRSRV_SYNC_DATA *psSyncData = psSyncInfo->psSyncData;
+
+		PVR_TRACE(("SGX2DQueryBlitsCompleteKM: Syncinfo: %p, Syncdata: %p", psSyncInfo, psSyncData));
+
+		PVR_TRACE(("SGX2DQueryBlitsCompleteKM: Read ops complete: %d, Read ops pending: %d", psSyncData->ui32ReadOpsComplete, psSyncData->ui32ReadOpsPending));
+		PVR_TRACE(("SGX2DQueryBlitsCompleteKM: Write ops complete: %d, Write ops pending: %d", psSyncData->ui32WriteOpsComplete, psSyncData->ui32WriteOpsPending));
+
+	}
+#endif
+
+	return PVRSRV_ERROR_TIMEOUT;
+}
+
+
+IMG_EXPORT
+IMG_VOID SGXFlushHWRenderTargetKM(IMG_HANDLE psDeviceNode, IMG_DEV_VIRTADDR sHWRTDataSetDevVAddr)
+{
+	PVR_ASSERT(sHWRTDataSetDevVAddr.uiAddr != IMG_NULL);
+
+	SGXCleanupRequest(psDeviceNode,
+					  &sHWRTDataSetDevVAddr,
+					  PVRSRV_CLEANUPCMD_RT);
+}
+
+
+IMG_UINT32 SGXConvertTimeStamp(PVRSRV_SGXDEV_INFO	*psDevInfo,
+							   IMG_UINT32			ui32TimeWraps,
+							   IMG_UINT32			ui32Time)
+{
+#if defined(EUR_CR_TIMER)
+	PVR_UNREFERENCED_PARAMETER(psDevInfo);
+	PVR_UNREFERENCED_PARAMETER(ui32TimeWraps);
+	return ui32Time;
+#else
+	IMG_UINT64	ui64Clocks;
+	IMG_UINT32	ui32Clocksx16;
+
+	ui64Clocks = ((IMG_UINT64)ui32TimeWraps * psDevInfo->ui32uKernelTimerClock) +
+					(psDevInfo->ui32uKernelTimerClock - (ui32Time & EUR_CR_EVENT_TIMER_VALUE_MASK));
+	ui32Clocksx16 = (IMG_UINT32)(ui64Clocks / 16);
+
+	return ui32Clocksx16;
+#endif
+}
+
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.h
new file mode 100644
index 0000000..ead8d56
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/devices/sgx/sgxutils.h
@@ -0,0 +1,95 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "perproc.h"
+#include "sgxinfokm.h"
+
+#define CCB_OFFSET_IS_VALID(type, psCCBMemInfo, psCCBKick, offset) \
+	((sizeof(type) <= (psCCBMemInfo)->ui32AllocSize) && \
+	((psCCBKick)->offset <= (psCCBMemInfo)->ui32AllocSize - sizeof(type)))
+
+#define	CCB_DATA_FROM_OFFSET(type, psCCBMemInfo, psCCBKick, offset) \
+	((type *)(((IMG_CHAR *)(psCCBMemInfo)->pvLinAddrKM) + \
+		(psCCBKick)->offset))
+
+
+IMG_IMPORT
+IMG_VOID SGXTestActivePowerEvent(PVRSRV_DEVICE_NODE	*psDeviceNode,
+								 IMG_UINT32			ui32CallerID);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXScheduleCCBCommand(PVRSRV_SGXDEV_INFO 	*psDevInfo,
+								   SGXMKIF_CMD_TYPE		eCommandType,
+								   SGXMKIF_COMMAND		*psCommandData,
+								   IMG_UINT32			ui32CallerID,
+								   IMG_UINT32			ui32PDumpFlags);
+IMG_IMPORT
+PVRSRV_ERROR SGXScheduleCCBCommandKM(PVRSRV_DEVICE_NODE		*psDeviceNode,
+									 SGXMKIF_CMD_TYPE		eCommandType,
+									 SGXMKIF_COMMAND		*psCommandData,
+									 IMG_UINT32				ui32CallerID,
+									 IMG_UINT32				ui32PDumpFlags);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXScheduleProcessQueuesKM(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+IMG_IMPORT
+IMG_BOOL SGXIsDevicePowered(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+IMG_IMPORT
+IMG_HANDLE SGXRegisterHWRenderContextKM(IMG_HANDLE				psDeviceNode,
+										IMG_DEV_VIRTADDR		*psHWRenderContextDevVAddr,
+										PVRSRV_PER_PROCESS_DATA *psPerProc);
+
+IMG_IMPORT
+IMG_HANDLE SGXRegisterHWTransferContextKM(IMG_HANDLE				psDeviceNode,
+										  IMG_DEV_VIRTADDR			*psHWTransferContextDevVAddr,
+										  PVRSRV_PER_PROCESS_DATA	*psPerProc);
+
+IMG_IMPORT
+IMG_VOID SGXFlushHWRenderTargetKM(IMG_HANDLE psSGXDevInfo, IMG_DEV_VIRTADDR psHWRTDataSetDevVAddr);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXUnregisterHWRenderContextKM(IMG_HANDLE hHWRenderContext);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXUnregisterHWTransferContextKM(IMG_HANDLE hHWTransferContext);
+
+#if defined(SGX_FEATURE_2D_HARDWARE)
+IMG_IMPORT
+IMG_HANDLE SGXRegisterHW2DContextKM(IMG_HANDLE				psDeviceNode,
+									IMG_DEV_VIRTADDR		*psHW2DContextDevVAddr,
+									PVRSRV_PER_PROCESS_DATA *psPerProc);
+
+IMG_IMPORT
+PVRSRV_ERROR SGXUnregisterHW2DContextKM(IMG_HANDLE hHW2DContext);
+#endif
+
+IMG_UINT32 SGXConvertTimeStamp(PVRSRV_SGXDEV_INFO	*psDevInfo,
+							   IMG_UINT32			ui32TimeWraps,
+							   IMG_UINT32			ui32Time);
+
+IMG_VOID SGXCleanupRequest(PVRSRV_DEVICE_NODE	*psDeviceNode,
+							IMG_DEV_VIRTADDR	*psHWDataDevVAddr,
+							IMG_UINT32			ui32CleanupType);
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_data.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_data.h
new file mode 100644
index 0000000..a16a2fa
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_data.h
@@ -0,0 +1,62 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _ENV_DATA_
+#define _ENV_DATA_
+
+#include <linux/interrupt.h>
+#include <linux/pci.h>
+
+#if defined(PVR_LINUX_MISR_USING_WORKQUEUE) || defined(PVR_LINUX_MISR_USING_PRIVATE_WORKQUEUE)
+#include <linux/workqueue.h>
+#endif
+
+#define PVRSRV_MAX_BRIDGE_IN_SIZE	0x1000
+#define PVRSRV_MAX_BRIDGE_OUT_SIZE	0x1000
+
+typedef	struct _PVR_PCI_DEV_TAG
+{
+	struct pci_dev		*psPCIDev;
+	HOST_PCI_INIT_FLAGS	ePCIFlags;
+	IMG_BOOL abPCIResourceInUse[DEVICE_COUNT_RESOURCE];
+} PVR_PCI_DEV;
+
+typedef struct _ENV_DATA_TAG
+{
+	IMG_VOID		*pvBridgeData;
+	struct pm_dev		*psPowerDevice;
+	IMG_BOOL		bLISRInstalled;
+	IMG_BOOL		bMISRInstalled;
+	IMG_UINT32		ui32IRQ;
+	IMG_VOID		*pvISRCookie;
+#if defined(PVR_LINUX_MISR_USING_PRIVATE_WORKQUEUE)
+	struct workqueue_struct	*psWorkQueue;
+#endif
+#if defined(PVR_LINUX_MISR_USING_WORKQUEUE) || defined(PVR_LINUX_MISR_USING_PRIVATE_WORKQUEUE)
+	struct work_struct	sMISRWork;
+	IMG_VOID		*pvMISRData;
+#else
+	struct tasklet_struct	sMISRTasklet;
+#endif
+} ENV_DATA;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_perproc.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_perproc.h
new file mode 100644
index 0000000..c9fbad9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/env_perproc.h
@@ -0,0 +1,52 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __ENV_PERPROC_H__
+#define __ENV_PERPROC_H__
+
+#include <linux/list.h>
+#include <linux/proc_fs.h>
+
+#include "services.h"
+#include "handle.h"
+
+typedef struct _PVRSRV_ENV_PER_PROCESS_DATA_
+{
+	IMG_HANDLE hBlockAlloc;
+	struct proc_dir_entry *psProcDir;
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+	struct list_head sDRMAuthListHead;
+#endif
+} PVRSRV_ENV_PER_PROCESS_DATA;
+
+IMG_VOID RemovePerProcessProcDir(PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc);
+
+PVRSRV_ERROR LinuxMMapPerProcessConnect(PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc);
+
+IMG_VOID LinuxMMapPerProcessDisconnect(PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc);
+
+PVRSRV_ERROR LinuxMMapPerProcessHandleOptions(PVRSRV_HANDLE_BASE *psHandleBase);
+
+IMG_HANDLE LinuxTerminatingProcessPrivateData(IMG_VOID);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.c
new file mode 100644
index 0000000..85493c1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.c
@@ -0,0 +1,267 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/version.h>
+#include <asm/io.h>
+#include <asm/page.h>
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
+#include <asm/system.h>
+#endif
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+#include <linux/delay.h>
+#include <linux/pci.h>
+
+#include <linux/string.h>
+#include <linux/sched.h>
+#include <linux/interrupt.h>
+#include <asm/hardirq.h>
+#include <linux/timer.h>
+#include <linux/capability.h>
+#include <linux/sched.h>
+#include <asm/uaccess.h>
+
+#include "img_types.h"
+#include "services_headers.h"
+#include "mm.h"
+#include "pvrmmap.h"
+#include "mmap.h"
+#include "env_data.h"
+#include "proc.h"
+#include "mutex.h"
+#include "lock.h"
+
+typedef struct PVRSRV_LINUX_EVENT_OBJECT_LIST_TAG
+{
+   rwlock_t 			   sLock;
+   struct list_head        sList;
+
+} PVRSRV_LINUX_EVENT_OBJECT_LIST;
+
+
+typedef struct PVRSRV_LINUX_EVENT_OBJECT_TAG
+{
+	atomic_t	sTimeStamp;
+	IMG_UINT32  ui32TimeStampPrevious;
+#if defined(DEBUG)
+	IMG_UINT	ui32Stats;
+#endif
+    wait_queue_head_t sWait;
+	struct list_head        sList;
+	IMG_HANDLE				hResItem;
+	PVRSRV_LINUX_EVENT_OBJECT_LIST *psLinuxEventObjectList;
+} PVRSRV_LINUX_EVENT_OBJECT;
+
+PVRSRV_ERROR LinuxEventObjectListCreate(IMG_HANDLE *phEventObjectList)
+{
+	PVRSRV_LINUX_EVENT_OBJECT_LIST *psEvenObjectList;
+
+	if(OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP, sizeof(PVRSRV_LINUX_EVENT_OBJECT_LIST),
+		(IMG_VOID **)&psEvenObjectList, IMG_NULL,
+		"Linux Event Object List") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "LinuxEventObjectCreate: failed to allocate memory for event list"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+    INIT_LIST_HEAD(&psEvenObjectList->sList);
+
+	rwlock_init(&psEvenObjectList->sLock);
+
+	*phEventObjectList = (IMG_HANDLE *) psEvenObjectList;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR LinuxEventObjectListDestroy(IMG_HANDLE hEventObjectList)
+{
+
+	PVRSRV_LINUX_EVENT_OBJECT_LIST *psEvenObjectList = (PVRSRV_LINUX_EVENT_OBJECT_LIST *) hEventObjectList ;
+
+	if(psEvenObjectList)
+	{
+		if (!list_empty(&psEvenObjectList->sList))
+		{
+			 PVR_DPF((PVR_DBG_ERROR, "LinuxEventObjectListDestroy: Event List is not empty"));
+			 return PVRSRV_ERROR_GENERIC;
+		}
+		OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP, sizeof(PVRSRV_LINUX_EVENT_OBJECT_LIST), psEvenObjectList, IMG_NULL);
+
+	}
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR LinuxEventObjectDelete(IMG_HANDLE hOSEventObjectList, IMG_HANDLE hOSEventObject)
+{
+	if(hOSEventObjectList)
+	{
+		if(hOSEventObject)
+		{
+			PVRSRV_LINUX_EVENT_OBJECT *psLinuxEventObject = (PVRSRV_LINUX_EVENT_OBJECT *)hOSEventObject;
+#if defined(DEBUG)
+			PVR_DPF((PVR_DBG_MESSAGE, "LinuxEventObjectListDelete: Event object waits: %lu", psLinuxEventObject->ui32Stats));
+#endif
+			if(ResManFreeResByPtr(psLinuxEventObject->hResItem) != PVRSRV_OK)
+			{
+				return PVRSRV_ERROR_GENERIC;
+			}
+
+			return PVRSRV_OK;
+		}
+	}
+	return PVRSRV_ERROR_GENERIC;
+
+}
+
+static PVRSRV_ERROR LinuxEventObjectDeleteCallback(IMG_PVOID pvParam, IMG_UINT32 ui32Param)
+{
+	PVRSRV_LINUX_EVENT_OBJECT *psLinuxEventObject = pvParam;
+	PVRSRV_LINUX_EVENT_OBJECT_LIST *psLinuxEventObjectList = psLinuxEventObject->psLinuxEventObjectList;
+
+	PVR_UNREFERENCED_PARAMETER(ui32Param);
+
+	write_lock_bh(&psLinuxEventObjectList->sLock);
+	list_del(&psLinuxEventObject->sList);
+	write_unlock_bh(&psLinuxEventObjectList->sLock);
+
+#if defined(DEBUG)
+	PVR_DPF((PVR_DBG_MESSAGE, "LinuxEventObjectDeleteCallback: Event object waits: %lu", psLinuxEventObject->ui32Stats));
+#endif
+
+	OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP, sizeof(PVRSRV_LINUX_EVENT_OBJECT), psLinuxEventObject, IMG_NULL);
+
+
+	return PVRSRV_OK;
+}
+PVRSRV_ERROR LinuxEventObjectAdd(IMG_HANDLE hOSEventObjectList, IMG_HANDLE *phOSEventObject)
+ {
+	PVRSRV_LINUX_EVENT_OBJECT *psLinuxEventObject;
+	PVRSRV_LINUX_EVENT_OBJECT_LIST *psLinuxEventObjectList = (PVRSRV_LINUX_EVENT_OBJECT_LIST*)hOSEventObjectList;
+	IMG_UINT32 ui32PID = OSGetCurrentProcessIDKM();
+	PVRSRV_PER_PROCESS_DATA *psPerProc;
+
+	psPerProc = PVRSRVPerProcessData(ui32PID);
+	if (psPerProc == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "LinuxEventObjectAdd: Couldn't find per-process data"));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+
+	if(OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP, sizeof(PVRSRV_LINUX_EVENT_OBJECT),
+		(IMG_VOID **)&psLinuxEventObject, IMG_NULL,
+		"Linux Event Object") != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "LinuxEventObjectAdd: failed to allocate memory "));
+		return PVRSRV_ERROR_OUT_OF_MEMORY;
+	}
+
+	INIT_LIST_HEAD(&psLinuxEventObject->sList);
+
+	atomic_set(&psLinuxEventObject->sTimeStamp, 0);
+	psLinuxEventObject->ui32TimeStampPrevious = 0;
+
+#if defined(DEBUG)
+	psLinuxEventObject->ui32Stats = 0;
+#endif
+    init_waitqueue_head(&psLinuxEventObject->sWait);
+
+	psLinuxEventObject->psLinuxEventObjectList = psLinuxEventObjectList;
+
+	psLinuxEventObject->hResItem = ResManRegisterRes(psPerProc->hResManContext,
+													 RESMAN_TYPE_EVENT_OBJECT,
+													 psLinuxEventObject,
+													 0,
+													 &LinuxEventObjectDeleteCallback);
+
+	write_lock_bh(&psLinuxEventObjectList->sLock);
+	list_add(&psLinuxEventObject->sList, &psLinuxEventObjectList->sList);
+    write_unlock_bh(&psLinuxEventObjectList->sLock);
+
+	*phOSEventObject = psLinuxEventObject;
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR LinuxEventObjectSignal(IMG_HANDLE hOSEventObjectList)
+{
+	PVRSRV_LINUX_EVENT_OBJECT *psLinuxEventObject;
+	PVRSRV_LINUX_EVENT_OBJECT_LIST *psLinuxEventObjectList = (PVRSRV_LINUX_EVENT_OBJECT_LIST*)hOSEventObjectList;
+	struct list_head *psListEntry, *psListEntryTemp, *psList;
+	psList = &psLinuxEventObjectList->sList;
+
+	read_lock(&psLinuxEventObjectList->sLock);
+	list_for_each_safe(psListEntry, psListEntryTemp, psList)
+	{
+
+		psLinuxEventObject = (PVRSRV_LINUX_EVENT_OBJECT *)list_entry(psListEntry, PVRSRV_LINUX_EVENT_OBJECT, sList);
+
+		atomic_inc(&psLinuxEventObject->sTimeStamp);
+	 	wake_up_interruptible(&psLinuxEventObject->sWait);
+	}
+	read_unlock(&psLinuxEventObjectList->sLock);
+
+	return 	PVRSRV_OK;
+
+}
+
+PVRSRV_ERROR LinuxEventObjectWait(IMG_HANDLE hOSEventObject, IMG_UINT32 ui32MSTimeout)
+{
+	IMG_UINT32 ui32TimeStamp;
+	DEFINE_WAIT(sWait);
+
+	PVRSRV_LINUX_EVENT_OBJECT *psLinuxEventObject = (PVRSRV_LINUX_EVENT_OBJECT *) hOSEventObject;
+
+	IMG_UINT32 ui32TimeOutJiffies = msecs_to_jiffies(ui32MSTimeout);
+
+	do
+	{
+		prepare_to_wait(&psLinuxEventObject->sWait, &sWait, TASK_INTERRUPTIBLE);
+		ui32TimeStamp = atomic_read(&psLinuxEventObject->sTimeStamp);
+
+		if(psLinuxEventObject->ui32TimeStampPrevious != ui32TimeStamp)
+		{
+			break;
+		}
+
+		mutex_unlock(&gPVRSRVLock);
+
+		ui32TimeOutJiffies = (IMG_UINT32)schedule_timeout((IMG_INT32)ui32TimeOutJiffies);
+
+		mutex_lock(&gPVRSRVLock);
+#if defined(DEBUG)
+		psLinuxEventObject->ui32Stats++;
+#endif
+
+
+	} while (ui32TimeOutJiffies);
+
+	finish_wait(&psLinuxEventObject->sWait, &sWait);
+
+	psLinuxEventObject->ui32TimeStampPrevious = ui32TimeStamp;
+
+	return ui32TimeOutJiffies ? PVRSRV_OK : PVRSRV_ERROR_TIMEOUT;
+
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.h
new file mode 100644
index 0000000..908014e
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/event.h
@@ -0,0 +1,28 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+PVRSRV_ERROR LinuxEventObjectListCreate(IMG_HANDLE *phEventObjectList);
+PVRSRV_ERROR LinuxEventObjectListDestroy(IMG_HANDLE hEventObjectList);
+PVRSRV_ERROR LinuxEventObjectAdd(IMG_HANDLE hOSEventObjectList, IMG_HANDLE *phOSEventObject);
+PVRSRV_ERROR LinuxEventObjectDelete(IMG_HANDLE hOSEventObjectList, IMG_HANDLE hOSEventObject);
+PVRSRV_ERROR LinuxEventObjectSignal(IMG_HANDLE hOSEventObjectList);
+PVRSRV_ERROR LinuxEventObjectWait(IMG_HANDLE hOSEventObject, IMG_UINT32 ui32MSTimeout);
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/kbuild/Makefile b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/kbuild/Makefile
new file mode 100644
index 0000000..887689f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/kbuild/Makefile
@@ -0,0 +1,151 @@
+#
+# Copyright (c) Imagination Technologies Ltd.
+#
+# 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 shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE. 
+# 
+#
+
+MODULE		?= pvrsrvkm
+
+MODULE_CFLAGS = 
+
+ifeq ($(PVR_KBUILD_IN_KERNEL),1)
+# PVRKROOT is the relative path to the PVR sources.
+# DRVKROOT should point to the non-PVR sources, if any.
+PVRKROOT ?= .
+KBUILDROOT 	= $(PVRKROOT)/services4
+else
+KBUILDROOT 	= ../../../..
+endif
+
+clean-dirs :=
+
+INCLUDES = 	-I$(EURASIAROOT)/include4 \
+			-I$(EURASIAROOT)/services4/include \
+			-I$(EURASIAROOT)/services4/srvkm/env/linux \
+			-I$(EURASIAROOT)/services4/srvkm/include \
+			-I$(EURASIAROOT)/services4/srvkm/bridged \
+			-I$(EURASIAROOT)/services4/system/include \
+			-I$(EURASIAROOT)/services4/system/plb \
+			-I$(EURASIAROOT)/services4/system/tnc 
+
+
+SOURCES	      = $(KBUILDROOT)/srvkm/env/linux/osfunc.c \
+				$(KBUILDROOT)/srvkm/env/linux/mutils.c \
+				$(KBUILDROOT)/srvkm/env/linux/mmap.c \
+				$(KBUILDROOT)/srvkm/env/linux/module.c \
+				$(KBUILDROOT)/srvkm/env/linux/pdump.c \
+				$(KBUILDROOT)/srvkm/env/linux/proc.c \
+				$(KBUILDROOT)/srvkm/env/linux/pvr_bridge_k.c \
+				$(KBUILDROOT)/srvkm/env/linux/pvr_debug.c \
+				$(KBUILDROOT)/srvkm/env/linux/mm.c \
+				$(KBUILDROOT)/srvkm/env/linux/mutex.c \
+				$(KBUILDROOT)/srvkm/env/linux/event.c \
+				$(KBUILDROOT)/srvkm/env/linux/osperproc.c
+
+SOURCES	     += $(KBUILDROOT)/srvkm/common/buffer_manager.c \
+				$(KBUILDROOT)/srvkm/common/devicemem.c \
+				$(KBUILDROOT)/srvkm/common/deviceclass.c \
+				$(KBUILDROOT)/srvkm/common/handle.c \
+				$(KBUILDROOT)/srvkm/common/hash.c \
+				$(KBUILDROOT)/srvkm/common/metrics.c \
+				$(KBUILDROOT)/srvkm/common/pvrsrv.c \
+				$(KBUILDROOT)/srvkm/common/queue.c \
+				$(KBUILDROOT)/srvkm/common/ra.c \
+				$(KBUILDROOT)/srvkm/common/resman.c \
+				$(KBUILDROOT)/srvkm/common/power.c \
+				$(KBUILDROOT)/srvkm/common/mem.c \
+				$(KBUILDROOT)/srvkm/common/pdump_common.c \
+				$(KBUILDROOT)/srvkm/bridged/bridged_support.c \
+				$(KBUILDROOT)/srvkm/bridged/bridged_pvr_bridge.c \
+				$(KBUILDROOT)/srvkm/common/perproc.c \
+				$(KBUILDROOT)/../services4/system/plb/sysconfig.c \
+				$(KBUILDROOT)/../services4/system/tnc/sysconfig.c \
+				$(KBUILDROOT)/../services4/system/common/sysconfig.c \
+				$(KBUILDROOT)/../services4/system/common/sysutils.c \
+				$(KBUILDROOT)/srvkm/common/lists.c \
+				$(KBUILDROOT)/srvkm/common/mem_debug.c
+
+INCLUDES += -I$(EURASIAROOT)/services4/srvkm/hwdefs 
+
+SYM_VERS_DEPS = $(EURASIAROOT)/tools/intern/debug/dbgdriv/linux
+
+
+#
+# Device dependent
+#
+
+ifeq ($(SUPPORT_SGX), 1)
+
+INCLUDES 	+=	-I$(EURASIAROOT)/services4/srvkm/bridged/sgx \
+				-I$(EURASIAROOT)/services4/srvkm/devices/sgx
+
+SOURCES		+= 	$(KBUILDROOT)/srvkm/bridged/sgx/bridged_sgx_bridge.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/sgxinit.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/sgxpower.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/sgxreset.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/sgxutils.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/sgxkick.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/sgxtransfer.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/mmu.c \
+				$(KBUILDROOT)/srvkm/devices/sgx/pb.c
+
+else  
+
+ifeq ($(SUPPORT_VGX), 1)
+
+INCLUDES 	+=	-I$(EURASIAROOT)/services4/srvkm/bridged/vgx \
+				-I$(EURASIAROOT)/services4/srvkm/devices/vgx
+
+#FIXME: We need to cerate a subset of vgxdefs.h from $(EURASIAROOT)/hwdefs 
+# and put it into srvkm/hwdefs for GPL compliance
+INCLUDES 	+=	-I$(EURASIAROOT)/hwdefs
+
+SOURCES		+= 	$(KBUILDROOT)/srvkm/bridged/vgx/bridged_vgx_bridge.c \
+				$(KBUILDROOT)/srvkm/devices/vgx/vgxinit.c \
+				$(KBUILDROOT)/srvkm/devices/vgx/vgxreset.c \
+				$(KBUILDROOT)/srvkm/devices/vgx/mmu.c \
+				$(KBUILDROOT)/srvkm/devices/vgx/pb.c \
+				$(KBUILDROOT)/srvkm/env/linux/pdump_vgx.c
+endif
+
+endif # SUPPORT_SGX
+
+ifeq ($(SUPPORT_DRI_DRM),1)
+	SOURCES += $(KBUILDROOT)/srvkm/env/linux/pvr_drm.c
+	INCLUDES += -I$(KERNELDIR)/include/drm \
+		-I$(EURASIAROOT)/services4/include/env/linux
+
+ifneq ("$(DISPLAY_CONTROLLER_DIR)","")
+include $(EURASIAROOT)/services4/$(DISPLAY_CONTROLLER_DIR)/makefile.linux.common
+endif
+
+ifeq ($(PDUMP),1)
+include $(EURASIAROOT)/tools/intern/debug/dbgdriv/linux/makefile.linux.common
+endif
+
+endif
+
+ifeq ($(PVR_MODULE_TEST),1)
+INCLUDES += -I$(EURASIAROOT)/moduletests/include
+SOURCES	 += $(KBUILDROOT)/../moduletests/services_test/kern_test.c
+SYS_CFLAGS += -DMODULE_TEST
+endif
+
+include $(EURASIAROOT)/eurasiacon/build/linux/kbuild/Makefile.kbuild_subdir_common
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/linkage.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/linkage.h
new file mode 100644
index 0000000..a124f78
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/linkage.h
@@ -0,0 +1,57 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __LINKAGE_H__
+#define __LINKAGE_H__
+
+#if !defined(SUPPORT_DRI_DRM)
+IMG_INT32 PVRSRV_BridgeDispatchKM(struct file *file, IMG_UINT cmd, IMG_UINT32 arg);
+#endif
+
+IMG_VOID PVRDPFInit(IMG_VOID);
+PVRSRV_ERROR PVROSFuncInit(IMG_VOID);
+IMG_VOID PVROSFuncDeInit(IMG_VOID);
+
+#ifdef DEBUG
+IMG_INT PVRDebugProcSetLevel(struct file *file, const IMG_CHAR *buffer, IMG_UINT32 count, IMG_VOID *data);
+IMG_VOID PVRDebugSetLevel(IMG_UINT32 uDebugLevel);
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+void ProcSeqShowDebugLevel(struct seq_file *sfile,void* el);
+#else
+IMG_INT PVRDebugProcGetLevel(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data);
+#endif
+
+#ifdef PVR_MANUAL_POWER_CONTROL
+IMG_INT PVRProcSetPowerLevel(struct file *file, const IMG_CHAR *buffer, IMG_UINT32 count, IMG_VOID *data);
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+void ProcSeqShowPowerLevel(struct seq_file *sfile,void* el);
+#else
+IMG_INT PVRProcGetPowerLevel(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data);
+#endif
+
+
+#endif
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/lock.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/lock.h
new file mode 100644
index 0000000..4a18e01
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/lock.h
@@ -0,0 +1,28 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __LOCK_H__
+#define __LOCK_H__
+
+extern struct mutex gPVRSRVLock;
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.c
new file mode 100644
index 0000000..8844f0a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.c
@@ -0,0 +1,2377 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/version.h>
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <asm/io.h>
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
+#include <linux/wrapper.h>
+#endif
+#include <linux/slab.h>
+#include <linux/highmem.h>
+#include <linux/sched.h>
+
+#include "img_defs.h"
+#include "services.h"
+#include "servicesint.h"
+#include "sysconfig.h"
+#include "mutils.h"
+#include "mm.h"
+#include "pvrmmap.h"
+#include "mmap.h"
+#include "osfunc.h"
+#include "pvr_debug.h"
+#include "proc.h"
+#include "mutex.h"
+#include "lock.h"
+
+#if defined(DEBUG_LINUX_MEM_AREAS) || defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+	#include "lists.h"
+#endif
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+typedef enum {
+    DEBUG_MEM_ALLOC_TYPE_KMALLOC,
+    DEBUG_MEM_ALLOC_TYPE_VMALLOC,
+    DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES,
+    DEBUG_MEM_ALLOC_TYPE_IOREMAP,
+    DEBUG_MEM_ALLOC_TYPE_IO,
+    DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE,
+    DEBUG_MEM_ALLOC_TYPE_COUNT
+}DEBUG_MEM_ALLOC_TYPE;
+
+typedef struct _DEBUG_MEM_ALLOC_REC
+{
+    DEBUG_MEM_ALLOC_TYPE    eAllocType;
+	IMG_VOID				*pvKey;
+    IMG_VOID                *pvCpuVAddr;
+    IMG_UINT32              ulCpuPAddr;
+    IMG_VOID                *pvPrivateData;
+	IMG_UINT32				ui32Bytes;
+	pid_t					pid;
+    IMG_CHAR                *pszFileName;
+    IMG_UINT32              ui32Line;
+
+    struct _DEBUG_MEM_ALLOC_REC   *psNext;
+	struct _DEBUG_MEM_ALLOC_REC   **ppsThis;
+}DEBUG_MEM_ALLOC_REC;
+
+static IMPLEMENT_LIST_ANY_VA_2(DEBUG_MEM_ALLOC_REC, IMG_BOOL, IMG_FALSE)
+static IMPLEMENT_LIST_ANY_VA(DEBUG_MEM_ALLOC_REC)
+static IMPLEMENT_LIST_FOR_EACH(DEBUG_MEM_ALLOC_REC)
+static IMPLEMENT_LIST_INSERT(DEBUG_MEM_ALLOC_REC)
+static IMPLEMENT_LIST_REMOVE(DEBUG_MEM_ALLOC_REC)
+
+
+static DEBUG_MEM_ALLOC_REC *g_MemoryRecords;
+
+static IMG_UINT32 g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_COUNT];
+static IMG_UINT32 g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_COUNT];
+
+static IMG_UINT32 g_SysRAMWaterMark;
+static IMG_UINT32 g_SysRAMHighWaterMark;
+
+static IMG_UINT32 g_IOMemWaterMark;
+static IMG_UINT32 g_IOMemHighWaterMark;
+
+static IMG_VOID DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE eAllocType,
+                                       IMG_VOID *pvKey,
+                                       IMG_VOID *pvCpuVAddr,
+                                       IMG_UINT32 ulCpuPAddr,
+                                       IMG_VOID *pvPrivateData,
+                                       IMG_UINT32 ui32Bytes,
+                                       IMG_CHAR *pszFileName,
+                                       IMG_UINT32 ui32Line);
+
+static IMG_VOID DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE eAllocType, IMG_VOID *pvKey, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+
+static IMG_CHAR *DebugMemAllocRecordTypeToString(DEBUG_MEM_ALLOC_TYPE eAllocType);
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+static struct proc_dir_entry *g_SeqFileMemoryRecords =0;
+static void* ProcSeqNextMemoryRecords(struct seq_file *sfile,void* el,loff_t off);
+static void ProcSeqShowMemoryRecords(struct seq_file *sfile,void* el);
+static void* ProcSeqOff2ElementMemoryRecords(struct seq_file * sfile, loff_t off);
+
+#else
+static off_t printMemoryRecords(IMG_CHAR * buffer, size_t size, off_t off);
+#endif
+
+#endif
+
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+typedef struct _DEBUG_LINUX_MEM_AREA_REC
+{
+	LinuxMemArea                *psLinuxMemArea;
+    IMG_UINT32                  ui32Flags;
+	pid_t					    pid;
+
+	struct _DEBUG_LINUX_MEM_AREA_REC  *psNext;
+	struct _DEBUG_LINUX_MEM_AREA_REC  **ppsThis;
+}DEBUG_LINUX_MEM_AREA_REC;
+
+
+static IMPLEMENT_LIST_ANY_VA(DEBUG_LINUX_MEM_AREA_REC)
+static IMPLEMENT_LIST_FOR_EACH(DEBUG_LINUX_MEM_AREA_REC)
+static IMPLEMENT_LIST_INSERT(DEBUG_LINUX_MEM_AREA_REC)
+static IMPLEMENT_LIST_REMOVE(DEBUG_LINUX_MEM_AREA_REC)
+
+
+
+#if defined(DEBUG_LINUX_MEM_AREAS) || defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+static struct mutex g_sDebugMutex;
+#endif
+
+static DEBUG_LINUX_MEM_AREA_REC *g_LinuxMemAreaRecords;
+static IMG_UINT32 g_LinuxMemAreaCount;
+static IMG_UINT32 g_LinuxMemAreaWaterMark;
+static IMG_UINT32 g_LinuxMemAreaHighWaterMark;
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+static struct proc_dir_entry *g_SeqFileMemArea=0;
+
+static void* ProcSeqNextMemArea(struct seq_file *sfile,void* el,loff_t off);
+static void ProcSeqShowMemArea(struct seq_file *sfile,void* el);
+static void* ProcSeqOff2ElementMemArea(struct seq_file *sfile, loff_t off);
+
+#else
+static off_t printLinuxMemAreaRecords(IMG_CHAR * buffer, size_t size, off_t off);
+#endif
+
+#endif
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+#if (defined(DEBUG_LINUX_MEM_AREAS) || defined(DEBUG_LINUX_MEMORY_ALLOCATIONS))
+static void ProcSeqStartstopDebugMutex(struct seq_file *sfile,IMG_BOOL start);
+#endif
+#endif
+
+static LinuxKMemCache *psLinuxMemAreaCache;
+
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
+static IMG_VOID ReservePages(IMG_VOID *pvAddress, IMG_UINT32 ui32Length);
+static IMG_VOID UnreservePages(IMG_VOID *pvAddress, IMG_UINT32 ui32Length);
+#endif
+
+static LinuxMemArea *LinuxMemAreaStructAlloc(IMG_VOID);
+static IMG_VOID LinuxMemAreaStructFree(LinuxMemArea *psLinuxMemArea);
+#if defined(DEBUG_LINUX_MEM_AREAS)
+static IMG_VOID DebugLinuxMemAreaRecordAdd(LinuxMemArea *psLinuxMemArea, IMG_UINT32 ui32Flags);
+static DEBUG_LINUX_MEM_AREA_REC *DebugLinuxMemAreaRecordFind(LinuxMemArea *psLinuxMemArea);
+static IMG_VOID DebugLinuxMemAreaRecordRemove(LinuxMemArea *psLinuxMemArea);
+#endif
+
+PVRSRV_ERROR
+LinuxMMInit(IMG_VOID)
+{
+#if defined(DEBUG_LINUX_MEM_AREAS) || defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+	mutex_init(&g_sDebugMutex);
+#endif
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    {
+        IMG_INT iStatus;
+#ifdef PVR_PROC_USE_SEQ_FILE
+		g_SeqFileMemArea = CreateProcReadEntrySeq(
+									"mem_areas",
+									NULL,
+									ProcSeqNextMemArea,
+									ProcSeqShowMemArea,
+									ProcSeqOff2ElementMemArea,
+									ProcSeqStartstopDebugMutex
+								   );
+		iStatus = !g_SeqFileMemArea ? -1 : 0;
+#else
+   iStatus = CreateProcReadEntry("mem_areas", printLinuxMemAreaRecords);
+#endif
+        if(iStatus!=0)
+        {
+            return PVRSRV_ERROR_OUT_OF_MEMORY;
+        }
+    }
+#endif
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    {
+        IMG_INT iStatus;
+#ifdef PVR_PROC_USE_SEQ_FILE
+		g_SeqFileMemoryRecords =CreateProcReadEntrySeq(
+									"meminfo",
+									NULL,
+									ProcSeqNextMemoryRecords,
+									ProcSeqShowMemoryRecords,
+									ProcSeqOff2ElementMemoryRecords,
+									ProcSeqStartstopDebugMutex
+								   );
+
+		iStatus = !g_SeqFileMemoryRecords ? -1 : 0;
+#else
+        iStatus = CreateProcReadEntry("meminfo", printMemoryRecords);
+#endif
+        if(iStatus!=0)
+        {
+            return PVRSRV_ERROR_OUT_OF_MEMORY;
+        }
+    }
+#endif
+
+    psLinuxMemAreaCache = KMemCacheCreateWrapper("img-mm", sizeof(LinuxMemArea), 0, 0);
+    if(!psLinuxMemAreaCache)
+    {
+        PVR_DPF((PVR_DBG_ERROR,"%s: failed to allocate kmem_cache", __FUNCTION__));
+        return PVRSRV_ERROR_OUT_OF_MEMORY;
+    }
+
+    return PVRSRV_OK;
+}
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+IMG_VOID LinuxMMCleanup_MemAreas_ForEachCb(DEBUG_LINUX_MEM_AREA_REC *psCurrentRecord)
+{
+	LinuxMemArea *psLinuxMemArea;
+
+	psLinuxMemArea = psCurrentRecord->psLinuxMemArea;
+	PVR_DPF((PVR_DBG_ERROR, "%s: BUG!: Cleaning up Linux memory area (%p), type=%s, size=%ld bytes",
+				__FUNCTION__,
+				psCurrentRecord->psLinuxMemArea,
+				LinuxMemAreaTypeToString(psCurrentRecord->psLinuxMemArea->eAreaType),
+				psCurrentRecord->psLinuxMemArea->ui32ByteSize));
+
+	LinuxMemAreaDeepFree(psLinuxMemArea);
+}
+#endif
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+IMG_VOID LinuxMMCleanup_MemRecords_ForEachVa(DEBUG_MEM_ALLOC_REC *psCurrentRecord)
+
+{
+
+	PVR_DPF((PVR_DBG_ERROR, "%s: BUG!: Cleaning up memory: "
+							"type=%s "
+							"CpuVAddr=%p "
+							"CpuPAddr=0x%08lx, "
+							"allocated @ file=%s,line=%d",
+			__FUNCTION__,
+			DebugMemAllocRecordTypeToString(psCurrentRecord->eAllocType),
+			psCurrentRecord->pvCpuVAddr,
+			psCurrentRecord->ulCpuPAddr,
+			psCurrentRecord->pszFileName,
+			psCurrentRecord->ui32Line));
+	switch(psCurrentRecord->eAllocType)
+	{
+		case DEBUG_MEM_ALLOC_TYPE_KMALLOC:
+			KFreeWrapper(psCurrentRecord->pvCpuVAddr);
+			break;
+		case DEBUG_MEM_ALLOC_TYPE_IOREMAP:
+			IOUnmapWrapper(psCurrentRecord->pvCpuVAddr);
+			break;
+		case DEBUG_MEM_ALLOC_TYPE_IO:
+
+			DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_IO, psCurrentRecord->pvKey, __FILE__, __LINE__);
+			break;
+		case DEBUG_MEM_ALLOC_TYPE_VMALLOC:
+			VFreeWrapper(psCurrentRecord->pvCpuVAddr);
+			break;
+		case DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES:
+
+			DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES, psCurrentRecord->pvKey, __FILE__, __LINE__);
+			break;
+		case DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE:
+			KMemCacheFreeWrapper(psCurrentRecord->pvPrivateData, psCurrentRecord->pvCpuVAddr);
+			break;
+		default:
+			PVR_ASSERT(0);
+	}
+}
+#endif
+
+
+IMG_VOID
+LinuxMMCleanup(IMG_VOID)
+{
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    {
+        if(g_LinuxMemAreaCount)
+        {
+            PVR_DPF((PVR_DBG_ERROR, "%s: BUG!: There are %d LinuxMemArea allocation unfreed (%ld bytes)",
+                    __FUNCTION__, g_LinuxMemAreaCount, g_LinuxMemAreaWaterMark));
+        }
+
+		List_DEBUG_LINUX_MEM_AREA_REC_ForEach(g_LinuxMemAreaRecords,
+											LinuxMMCleanup_MemAreas_ForEachCb);
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+		RemoveProcEntrySeq( g_SeqFileMemArea );
+#else
+        RemoveProcEntry("mem_areas");
+#endif
+    }
+#endif
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    {
+
+
+		List_DEBUG_MEM_ALLOC_REC_ForEach(g_MemoryRecords,
+										LinuxMMCleanup_MemRecords_ForEachVa);
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+		RemoveProcEntrySeq( g_SeqFileMemoryRecords );
+#else
+        RemoveProcEntry("meminfo");
+#endif
+
+    }
+#endif
+
+    if(psLinuxMemAreaCache)
+    {
+        KMemCacheDestroyWrapper(psLinuxMemAreaCache);
+        psLinuxMemAreaCache=NULL;
+    }
+}
+
+
+IMG_VOID *
+_KMallocWrapper(IMG_UINT32 ui32ByteSize, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line)
+{
+    IMG_VOID *pvRet;
+    pvRet = kmalloc(ui32ByteSize, GFP_KERNEL);
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    if(pvRet)
+    {
+        DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE_KMALLOC,
+                               pvRet,
+                               pvRet,
+                               0,
+                               NULL,
+                               ui32ByteSize,
+                               pszFileName,
+                               ui32Line
+                               );
+    }
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+    return pvRet;
+}
+
+
+IMG_VOID
+_KFreeWrapper(IMG_VOID *pvCpuVAddr, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line)
+{
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_KMALLOC, pvCpuVAddr, pszFileName,  ui32Line);
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+    kfree(pvCpuVAddr);
+}
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+static IMG_VOID
+DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE eAllocType,
+                       IMG_VOID *pvKey,
+                       IMG_VOID *pvCpuVAddr,
+                       IMG_UINT32 ulCpuPAddr,
+                       IMG_VOID *pvPrivateData,
+                       IMG_UINT32 ui32Bytes,
+                       IMG_CHAR *pszFileName,
+                       IMG_UINT32 ui32Line)
+{
+    DEBUG_MEM_ALLOC_REC *psRecord;
+
+    mutex_lock(&g_sDebugMutex);
+
+    psRecord = kmalloc(sizeof(DEBUG_MEM_ALLOC_REC), GFP_KERNEL);
+
+    psRecord->eAllocType = eAllocType;
+    psRecord->pvKey = pvKey;
+    psRecord->pvCpuVAddr = pvCpuVAddr;
+    psRecord->ulCpuPAddr = ulCpuPAddr;
+    psRecord->pvPrivateData = pvPrivateData;
+    psRecord->pid = current->pid;
+    psRecord->ui32Bytes = ui32Bytes;
+    psRecord->pszFileName = pszFileName;
+    psRecord->ui32Line = ui32Line;
+
+	List_DEBUG_MEM_ALLOC_REC_Insert(&g_MemoryRecords, psRecord);
+
+    g_WaterMarkData[eAllocType] += ui32Bytes;
+    if(g_WaterMarkData[eAllocType] > g_HighWaterMarkData[eAllocType])
+    {
+        g_HighWaterMarkData[eAllocType] = g_WaterMarkData[eAllocType];
+    }
+
+    if(eAllocType == DEBUG_MEM_ALLOC_TYPE_KMALLOC
+       || eAllocType == DEBUG_MEM_ALLOC_TYPE_VMALLOC
+       || eAllocType == DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES
+       || eAllocType == DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE)
+    {
+        g_SysRAMWaterMark += ui32Bytes;
+        if(g_SysRAMWaterMark > g_SysRAMHighWaterMark)
+        {
+            g_SysRAMHighWaterMark = g_SysRAMWaterMark;
+        }
+    }
+    else if(eAllocType == DEBUG_MEM_ALLOC_TYPE_IOREMAP
+            || eAllocType == DEBUG_MEM_ALLOC_TYPE_IO)
+    {
+        g_IOMemWaterMark += ui32Bytes;
+        if(g_IOMemWaterMark > g_IOMemHighWaterMark)
+        {
+            g_IOMemHighWaterMark = g_IOMemWaterMark;
+        }
+    }
+
+    mutex_unlock(&g_sDebugMutex);
+}
+
+
+IMG_BOOL DebugMemAllocRecordRemove_AnyVaCb(DEBUG_MEM_ALLOC_REC *psCurrentRecord, va_list va)
+{
+	DEBUG_MEM_ALLOC_TYPE eAllocType;
+	IMG_VOID *pvKey;
+
+	eAllocType = va_arg(va, DEBUG_MEM_ALLOC_TYPE);
+	pvKey = va_arg(va, IMG_VOID*);
+
+	if(psCurrentRecord->eAllocType == eAllocType
+		&& psCurrentRecord->pvKey == pvKey)
+	{
+		eAllocType = psCurrentRecord->eAllocType;
+		g_WaterMarkData[eAllocType] -= psCurrentRecord->ui32Bytes;
+
+		if(eAllocType == DEBUG_MEM_ALLOC_TYPE_KMALLOC
+		   || eAllocType == DEBUG_MEM_ALLOC_TYPE_VMALLOC
+		   || eAllocType == DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES
+		   || eAllocType == DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE)
+		{
+			g_SysRAMWaterMark -= psCurrentRecord->ui32Bytes;
+		}
+		else if(eAllocType == DEBUG_MEM_ALLOC_TYPE_IOREMAP
+				|| eAllocType == DEBUG_MEM_ALLOC_TYPE_IO)
+		{
+			g_IOMemWaterMark -= psCurrentRecord->ui32Bytes;
+		}
+
+		List_DEBUG_MEM_ALLOC_REC_Remove(psCurrentRecord);
+		kfree(psCurrentRecord);
+
+		return IMG_TRUE;
+	}
+	else
+	{
+		return IMG_FALSE;
+	}
+}
+
+
+static IMG_VOID
+DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE eAllocType, IMG_VOID *pvKey, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line)
+{
+    mutex_lock(&g_sDebugMutex);
+
+
+	if(!List_DEBUG_MEM_ALLOC_REC_IMG_BOOL_Any_va(g_MemoryRecords,
+												DebugMemAllocRecordRemove_AnyVaCb,
+												eAllocType,
+												pvKey))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: couldn't find an entry for type=%s with pvKey=%p (called from %s, line %d\n",
+		__FUNCTION__, DebugMemAllocRecordTypeToString(eAllocType), pvKey,
+		pszFileName, ui32Line));
+	}
+
+    mutex_unlock(&g_sDebugMutex);
+}
+
+
+static IMG_CHAR *
+DebugMemAllocRecordTypeToString(DEBUG_MEM_ALLOC_TYPE eAllocType)
+{
+    IMG_CHAR *apszDebugMemoryRecordTypes[] = {
+        "KMALLOC",
+        "VMALLOC",
+        "ALLOC_PAGES",
+        "IOREMAP",
+        "IO",
+        "KMEM_CACHE_ALLOC"
+    };
+    return apszDebugMemoryRecordTypes[eAllocType];
+}
+#endif
+
+
+
+IMG_VOID *
+_VMallocWrapper(IMG_UINT32 ui32Bytes,
+                IMG_UINT32 ui32AllocFlags,
+                IMG_CHAR *pszFileName,
+                IMG_UINT32 ui32Line)
+{
+    pgprot_t PGProtFlags;
+    IMG_VOID *pvRet;
+
+    switch(ui32AllocFlags & PVRSRV_HAP_CACHETYPE_MASK)
+    {
+        case PVRSRV_HAP_CACHED:
+            PGProtFlags = PAGE_KERNEL;
+            break;
+        case PVRSRV_HAP_WRITECOMBINE:
+            PGProtFlags = PGPROT_WC(PAGE_KERNEL);
+            break;
+        case PVRSRV_HAP_UNCACHED:
+            PGProtFlags = PGPROT_UC(PAGE_KERNEL);
+            break;
+        default:
+            PVR_DPF((PVR_DBG_ERROR,
+                     "VMAllocWrapper: unknown mapping flags=0x%08lx",
+                     ui32AllocFlags));
+            dump_stack();
+            return NULL;
+    }
+
+
+    pvRet = __vmalloc(ui32Bytes, GFP_KERNEL | __GFP_HIGHMEM, PGProtFlags);
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    if(pvRet)
+    {
+        DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE_VMALLOC,
+                               pvRet,
+                               pvRet,
+                               0,
+                               NULL,
+                               PAGE_ALIGN(ui32Bytes),
+                               pszFileName,
+                               ui32Line
+                               );
+    }
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+
+    return pvRet;
+}
+
+
+IMG_VOID
+_VFreeWrapper(IMG_VOID *pvCpuVAddr, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line)
+{
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_VMALLOC, pvCpuVAddr, pszFileName, ui32Line);
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+    vfree(pvCpuVAddr);
+}
+
+
+LinuxMemArea *
+NewVMallocLinuxMemArea(IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AreaFlags)
+{
+    LinuxMemArea *psLinuxMemArea;
+    IMG_VOID *pvCpuVAddr;
+
+    psLinuxMemArea = LinuxMemAreaStructAlloc();
+    if(!psLinuxMemArea)
+    {
+        goto failed;
+    }
+
+    pvCpuVAddr = VMallocWrapper(ui32Bytes, ui32AreaFlags);
+    if(!pvCpuVAddr)
+    {
+        goto failed;
+    }
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
+
+    ReservePages(pvCpuVAddr, ui32Bytes);
+#endif
+
+    psLinuxMemArea->eAreaType = LINUX_MEM_AREA_VMALLOC;
+    psLinuxMemArea->uData.sVmalloc.pvVmallocAddress = pvCpuVAddr;
+    psLinuxMemArea->ui32ByteSize = ui32Bytes;
+    psLinuxMemArea->ui32AreaFlags = ui32AreaFlags;
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+    INIT_LIST_HEAD(&psLinuxMemArea->sMMapOffsetStructList);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordAdd(psLinuxMemArea, ui32AreaFlags);
+#endif
+
+    return psLinuxMemArea;
+
+failed:
+    PVR_DPF((PVR_DBG_ERROR, "%s: failed!", __FUNCTION__));
+    if(psLinuxMemArea)
+        LinuxMemAreaStructFree(psLinuxMemArea);
+    return NULL;
+}
+
+
+IMG_VOID
+FreeVMallocLinuxMemArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVR_ASSERT(psLinuxMemArea);
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_VMALLOC);
+    PVR_ASSERT(psLinuxMemArea->uData.sVmalloc.pvVmallocAddress);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordRemove(psLinuxMemArea);
+#endif
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
+	UnreservePages(psLinuxMemArea->uData.sVmalloc.pvVmallocAddress,
+                    psLinuxMemArea->ui32ByteSize);
+#endif
+
+    PVR_DPF((PVR_DBG_MESSAGE,"%s: pvCpuVAddr: %p",
+             __FUNCTION__, psLinuxMemArea->uData.sVmalloc.pvVmallocAddress));
+    VFreeWrapper(psLinuxMemArea->uData.sVmalloc.pvVmallocAddress);
+
+    LinuxMemAreaStructFree(psLinuxMemArea);
+}
+
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
+static IMG_VOID
+ReservePages(IMG_VOID *pvAddress, IMG_UINT32 ui32Length)
+{
+	IMG_VOID *pvPage;
+	IMG_VOID *pvEnd = pvAddress + ui32Length;
+
+	for(pvPage = pvAddress; pvPage < pvEnd;  pvPage += PAGE_SIZE)
+	{
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
+		SetPageReserved(vmalloc_to_page(pvPage));
+#else
+		mem_map_reserve(vmalloc_to_page(pvPage));
+#endif
+	}
+}
+
+
+static IMG_VOID
+UnreservePages(IMG_VOID *pvAddress, IMG_UINT32 ui32Length)
+{
+	IMG_VOID *pvPage;
+	IMG_VOID *pvEnd = pvAddress + ui32Length;
+
+	for(pvPage = pvAddress; pvPage < pvEnd;  pvPage += PAGE_SIZE)
+	{
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
+		ClearPageReserved(vmalloc_to_page(pvPage));
+#else
+		mem_map_unreserve(vmalloc_to_page(pvPage));
+#endif
+	}
+}
+#endif
+
+
+IMG_VOID *
+_IORemapWrapper(IMG_CPU_PHYADDR BasePAddr,
+               IMG_UINT32 ui32Bytes,
+               IMG_UINT32 ui32MappingFlags,
+               IMG_CHAR *pszFileName,
+               IMG_UINT32 ui32Line)
+{
+    IMG_VOID *pvIORemapCookie;
+
+    switch(ui32MappingFlags & PVRSRV_HAP_CACHETYPE_MASK)
+    {
+        case PVRSRV_HAP_CACHED:
+	    pvIORemapCookie = (IMG_VOID *)IOREMAP(BasePAddr.uiAddr, ui32Bytes);
+            break;
+        case PVRSRV_HAP_WRITECOMBINE:
+	    pvIORemapCookie = (IMG_VOID *)IOREMAP_WC(BasePAddr.uiAddr, ui32Bytes);
+            break;
+        case PVRSRV_HAP_UNCACHED:
+            pvIORemapCookie = (IMG_VOID *)IOREMAP_UC(BasePAddr.uiAddr, ui32Bytes);
+            break;
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "IORemapWrapper: unknown mapping flags"));
+            return NULL;
+    }
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    if(pvIORemapCookie)
+    {
+        DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE_IOREMAP,
+                               pvIORemapCookie,
+                               pvIORemapCookie,
+                               BasePAddr.uiAddr,
+                               NULL,
+                               ui32Bytes,
+                               pszFileName,
+                               ui32Line
+                               );
+    }
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+
+    return pvIORemapCookie;
+}
+
+
+IMG_VOID
+_IOUnmapWrapper(IMG_VOID *pvIORemapCookie, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line)
+{
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_IOREMAP, pvIORemapCookie, pszFileName, ui32Line);
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+    iounmap(pvIORemapCookie);
+}
+
+
+LinuxMemArea *
+NewIORemapLinuxMemArea(IMG_CPU_PHYADDR BasePAddr,
+                       IMG_UINT32 ui32Bytes,
+                       IMG_UINT32 ui32AreaFlags)
+{
+    LinuxMemArea *psLinuxMemArea;
+    IMG_VOID *pvIORemapCookie;
+
+    psLinuxMemArea = LinuxMemAreaStructAlloc();
+    if(!psLinuxMemArea)
+    {
+        return NULL;
+    }
+
+    pvIORemapCookie = IORemapWrapper(BasePAddr, ui32Bytes, ui32AreaFlags);
+    if(!pvIORemapCookie)
+    {
+        LinuxMemAreaStructFree(psLinuxMemArea);
+        return NULL;
+    }
+
+    psLinuxMemArea->eAreaType = LINUX_MEM_AREA_IOREMAP;
+    psLinuxMemArea->uData.sIORemap.pvIORemapCookie = pvIORemapCookie;
+    psLinuxMemArea->uData.sIORemap.CPUPhysAddr = BasePAddr;
+    psLinuxMemArea->ui32ByteSize = ui32Bytes;
+    psLinuxMemArea->ui32AreaFlags = ui32AreaFlags;
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+    INIT_LIST_HEAD(&psLinuxMemArea->sMMapOffsetStructList);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordAdd(psLinuxMemArea, ui32AreaFlags);
+#endif
+
+    return psLinuxMemArea;
+}
+
+
+IMG_VOID
+FreeIORemapLinuxMemArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_IOREMAP);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordRemove(psLinuxMemArea);
+#endif
+
+    IOUnmapWrapper(psLinuxMemArea->uData.sIORemap.pvIORemapCookie);
+
+    LinuxMemAreaStructFree(psLinuxMemArea);
+}
+
+
+static IMG_BOOL
+TreatExternalPagesAsContiguous(IMG_SYS_PHYADDR *psSysPhysAddr, IMG_UINT32 ui32Bytes, IMG_BOOL bPhysContig)
+{
+	IMG_UINT32 ui32;
+	IMG_UINT32 ui32AddrChk;
+	IMG_UINT32 ui32NumPages = RANGE_TO_PAGES(ui32Bytes);
+
+
+	for (ui32 = 0, ui32AddrChk = psSysPhysAddr[0].uiAddr;
+		ui32 < ui32NumPages;
+		ui32++, ui32AddrChk = (bPhysContig) ? (ui32AddrChk + PAGE_SIZE) : psSysPhysAddr[ui32].uiAddr)
+	{
+		if (!pfn_valid(PHYS_TO_PFN(ui32AddrChk)))
+		{
+			break;
+		}
+	}
+	if (ui32 == ui32NumPages)
+	{
+		return IMG_FALSE;
+	}
+
+	if (!bPhysContig)
+	{
+		for (ui32 = 0, ui32AddrChk = psSysPhysAddr[0].uiAddr;
+			ui32 < ui32NumPages;
+			ui32++, ui32AddrChk += PAGE_SIZE)
+		{
+			if (psSysPhysAddr[ui32].uiAddr != ui32AddrChk)
+			{
+				return IMG_FALSE;
+			}
+		}
+	}
+
+	return IMG_TRUE;
+}
+
+LinuxMemArea *NewExternalKVLinuxMemArea(IMG_SYS_PHYADDR *pBasePAddr, IMG_VOID *pvCPUVAddr, IMG_UINT32 ui32Bytes, IMG_BOOL bPhysContig, IMG_UINT32 ui32AreaFlags)
+{
+    LinuxMemArea *psLinuxMemArea;
+
+    psLinuxMemArea = LinuxMemAreaStructAlloc();
+    if(!psLinuxMemArea)
+    {
+        return NULL;
+    }
+
+    psLinuxMemArea->eAreaType = LINUX_MEM_AREA_EXTERNAL_KV;
+    psLinuxMemArea->uData.sExternalKV.pvExternalKV = pvCPUVAddr;
+    psLinuxMemArea->uData.sExternalKV.bPhysContig = (IMG_BOOL)(bPhysContig || TreatExternalPagesAsContiguous(pBasePAddr, ui32Bytes, bPhysContig));
+
+    if (psLinuxMemArea->uData.sExternalKV.bPhysContig)
+    {
+	psLinuxMemArea->uData.sExternalKV.uPhysAddr.SysPhysAddr = *pBasePAddr;
+    }
+    else
+    {
+	psLinuxMemArea->uData.sExternalKV.uPhysAddr.pSysPhysAddr = pBasePAddr;
+    }
+    psLinuxMemArea->ui32ByteSize = ui32Bytes;
+    psLinuxMemArea->ui32AreaFlags = ui32AreaFlags;
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+    INIT_LIST_HEAD(&psLinuxMemArea->sMMapOffsetStructList);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordAdd(psLinuxMemArea, ui32AreaFlags);
+#endif
+
+    return psLinuxMemArea;
+}
+
+
+IMG_VOID
+FreeExternalKVLinuxMemArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_EXTERNAL_KV);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordRemove(psLinuxMemArea);
+#endif
+
+    LinuxMemAreaStructFree(psLinuxMemArea);
+}
+
+
+LinuxMemArea *
+NewIOLinuxMemArea(IMG_CPU_PHYADDR BasePAddr,
+                  IMG_UINT32 ui32Bytes,
+                  IMG_UINT32 ui32AreaFlags)
+{
+    LinuxMemArea *psLinuxMemArea = LinuxMemAreaStructAlloc();
+    if(!psLinuxMemArea)
+    {
+        return NULL;
+    }
+
+
+    psLinuxMemArea->eAreaType = LINUX_MEM_AREA_IO;
+    psLinuxMemArea->uData.sIO.CPUPhysAddr.uiAddr = BasePAddr.uiAddr;
+    psLinuxMemArea->ui32ByteSize = ui32Bytes;
+    psLinuxMemArea->ui32AreaFlags = ui32AreaFlags;
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+    INIT_LIST_HEAD(&psLinuxMemArea->sMMapOffsetStructList);
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE_IO,
+                           (IMG_VOID *)BasePAddr.uiAddr,
+                           0,
+                           BasePAddr.uiAddr,
+                           NULL,
+                           ui32Bytes,
+                           "unknown",
+                           0
+                           );
+#endif
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordAdd(psLinuxMemArea, ui32AreaFlags);
+#endif
+
+    return psLinuxMemArea;
+}
+
+
+IMG_VOID
+FreeIOLinuxMemArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_IO);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordRemove(psLinuxMemArea);
+#endif
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_IO,
+                              (IMG_VOID *)psLinuxMemArea->uData.sIO.CPUPhysAddr.uiAddr, __FILE__, __LINE__);
+#endif
+
+
+
+    LinuxMemAreaStructFree(psLinuxMemArea);
+}
+
+
+LinuxMemArea *
+NewAllocPagesLinuxMemArea(IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AreaFlags)
+{
+    LinuxMemArea *psLinuxMemArea;
+    IMG_UINT32 ui32PageCount;
+    struct page **pvPageList;
+    IMG_HANDLE hBlockPageList;
+    IMG_INT32 i;
+    PVRSRV_ERROR eError;
+
+    psLinuxMemArea = LinuxMemAreaStructAlloc();
+    if(!psLinuxMemArea)
+    {
+        goto failed_area_alloc;
+    }
+
+    ui32PageCount = RANGE_TO_PAGES(ui32Bytes);
+    eError = OSAllocMem(0, sizeof(*pvPageList) * ui32PageCount, (IMG_VOID **)&pvPageList, &hBlockPageList,
+							"Array of pages");
+    if(eError != PVRSRV_OK)
+    {
+        goto failed_page_list_alloc;
+    }
+
+    for(i=0; i<(IMG_INT32)ui32PageCount; i++)
+    {
+        pvPageList[i] = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, 0);
+        if(!pvPageList[i])
+        {
+            goto failed_alloc_pages;
+        }
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
+
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
+	SetPageReserved(pvPageList[i]);
+#else
+        mem_map_reserve(pvPageList[i]);
+#endif
+#endif
+
+    }
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES,
+                           pvPageList,
+                           0,
+                           0,
+                           NULL,
+                           PAGE_ALIGN(ui32Bytes),
+                           "unknown",
+                           0
+                           );
+#endif
+
+    psLinuxMemArea->eAreaType = LINUX_MEM_AREA_ALLOC_PAGES;
+    psLinuxMemArea->uData.sPageList.pvPageList = pvPageList;
+    psLinuxMemArea->uData.sPageList.hBlockPageList = hBlockPageList;
+    psLinuxMemArea->ui32ByteSize = ui32Bytes;
+    psLinuxMemArea->ui32AreaFlags = ui32AreaFlags;
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+    INIT_LIST_HEAD(&psLinuxMemArea->sMMapOffsetStructList);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordAdd(psLinuxMemArea, ui32AreaFlags);
+#endif
+
+    return psLinuxMemArea;
+
+failed_alloc_pages:
+    for(i--; i >= 0; i--)
+    {
+        __free_pages(pvPageList[i], 0);
+    }
+    (IMG_VOID) OSFreeMem(0, sizeof(*pvPageList) * ui32PageCount, pvPageList, hBlockPageList);
+	psLinuxMemArea->uData.sPageList.pvPageList = IMG_NULL;
+failed_page_list_alloc:
+    LinuxMemAreaStructFree(psLinuxMemArea);
+failed_area_alloc:
+    PVR_DPF((PVR_DBG_ERROR, "%s: failed", __FUNCTION__));
+
+    return NULL;
+}
+
+
+IMG_VOID
+FreeAllocPagesLinuxMemArea(LinuxMemArea *psLinuxMemArea)
+{
+    IMG_UINT32 ui32PageCount;
+    struct page **pvPageList;
+    IMG_HANDLE hBlockPageList;
+    IMG_INT32 i;
+
+    PVR_ASSERT(psLinuxMemArea);
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_ALLOC_PAGES);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordRemove(psLinuxMemArea);
+#endif
+
+    ui32PageCount = RANGE_TO_PAGES(psLinuxMemArea->ui32ByteSize);
+    pvPageList = psLinuxMemArea->uData.sPageList.pvPageList;
+    hBlockPageList = psLinuxMemArea->uData.sPageList.hBlockPageList;
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES, pvPageList, __FILE__, __LINE__);
+#endif
+
+    for(i=0;i<(IMG_INT32)ui32PageCount;i++)
+    {
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
+        ClearPageReserved(pvPageList[i]);
+#else
+        mem_map_reserve(pvPageList[i]);
+#endif
+#endif
+        __free_pages(pvPageList[i], 0);
+    }
+
+    (IMG_VOID) OSFreeMem(0, sizeof(*pvPageList) * ui32PageCount, pvPageList, hBlockPageList);
+	psLinuxMemArea->uData.sPageList.pvPageList = IMG_NULL;
+
+    LinuxMemAreaStructFree(psLinuxMemArea);
+}
+
+
+struct page*
+LinuxMemAreaOffsetToPage(LinuxMemArea *psLinuxMemArea,
+                         IMG_UINT32 ui32ByteOffset)
+{
+    IMG_UINT32 ui32PageIndex;
+    IMG_CHAR *pui8Addr;
+
+    switch(psLinuxMemArea->eAreaType)
+    {
+        case LINUX_MEM_AREA_ALLOC_PAGES:
+            ui32PageIndex = PHYS_TO_PFN(ui32ByteOffset);
+            return psLinuxMemArea->uData.sPageList.pvPageList[ui32PageIndex];
+
+        case LINUX_MEM_AREA_VMALLOC:
+            pui8Addr = psLinuxMemArea->uData.sVmalloc.pvVmallocAddress;
+            pui8Addr += ui32ByteOffset;
+            return vmalloc_to_page(pui8Addr);
+
+        case LINUX_MEM_AREA_SUB_ALLOC:
+
+            return LinuxMemAreaOffsetToPage(psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea,
+                                            psLinuxMemArea->uData.sSubAlloc.ui32ByteOffset
+                                             + ui32ByteOffset);
+        default:
+            PVR_DPF((PVR_DBG_ERROR,
+                    "%s: Unsupported request for struct page from LinuxMemArea with type=%s",
+                    __FUNCTION__, LinuxMemAreaTypeToString(psLinuxMemArea->eAreaType)));
+            return NULL;
+    }
+}
+
+
+LinuxKMemCache *
+KMemCacheCreateWrapper(IMG_CHAR *pszName,
+                       size_t Size,
+                       size_t Align,
+                       IMG_UINT32 ui32Flags)
+{
+#if defined(DEBUG_LINUX_SLAB_ALLOCATIONS)
+    ui32Flags |= SLAB_POISON|SLAB_RED_ZONE;
+#endif
+    return kmem_cache_create(pszName, Size, Align, ui32Flags, NULL
+#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22))
+				, NULL
+#endif
+			    );
+}
+
+
+IMG_VOID
+KMemCacheDestroyWrapper(LinuxKMemCache *psCache)
+{
+    kmem_cache_destroy(psCache);
+}
+
+
+IMG_VOID *
+_KMemCacheAllocWrapper(LinuxKMemCache *psCache,
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14))
+                      gfp_t Flags,
+#else
+                      IMG_INT Flags,
+#endif
+                      IMG_CHAR *pszFileName,
+                      IMG_UINT32 ui32Line)
+{
+    IMG_VOID *pvRet;
+
+    pvRet = kmem_cache_alloc(psCache, Flags);
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordAdd(DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE,
+                           pvRet,
+                           pvRet,
+                           0,
+                           psCache,
+                           kmem_cache_size(psCache),
+                           pszFileName,
+                           ui32Line
+                           );
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+
+    return pvRet;
+}
+
+
+IMG_VOID
+_KMemCacheFreeWrapper(LinuxKMemCache *psCache, IMG_VOID *pvObject, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line)
+{
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    DebugMemAllocRecordRemove(DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE, pvObject, pszFileName, ui32Line);
+#else
+    PVR_UNREFERENCED_PARAMETER(pszFileName);
+    PVR_UNREFERENCED_PARAMETER(ui32Line);
+#endif
+
+    kmem_cache_free(psCache, pvObject);
+}
+
+
+const IMG_CHAR *
+KMemCacheNameWrapper(LinuxKMemCache *psCache)
+{
+    PVR_UNREFERENCED_PARAMETER(psCache);
+
+
+    return "";
+}
+
+
+LinuxMemArea *
+NewSubLinuxMemArea(LinuxMemArea *psParentLinuxMemArea,
+                   IMG_UINT32 ui32ByteOffset,
+                   IMG_UINT32 ui32Bytes)
+{
+    LinuxMemArea *psLinuxMemArea;
+
+    PVR_ASSERT((ui32ByteOffset+ui32Bytes) <= psParentLinuxMemArea->ui32ByteSize);
+
+    psLinuxMemArea = LinuxMemAreaStructAlloc();
+    if(!psLinuxMemArea)
+    {
+        return NULL;
+    }
+
+    psLinuxMemArea->eAreaType = LINUX_MEM_AREA_SUB_ALLOC;
+    psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea = psParentLinuxMemArea;
+    psLinuxMemArea->uData.sSubAlloc.ui32ByteOffset = ui32ByteOffset;
+    psLinuxMemArea->ui32ByteSize = ui32Bytes;
+    psLinuxMemArea->ui32AreaFlags = psParentLinuxMemArea->ui32AreaFlags;
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+    INIT_LIST_HEAD(&psLinuxMemArea->sMMapOffsetStructList);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    {
+        DEBUG_LINUX_MEM_AREA_REC *psParentRecord;
+        psParentRecord = DebugLinuxMemAreaRecordFind(psParentLinuxMemArea);
+        DebugLinuxMemAreaRecordAdd(psLinuxMemArea, psParentRecord->ui32Flags);
+    }
+#endif
+
+    return psLinuxMemArea;
+}
+
+
+IMG_VOID
+FreeSubLinuxMemArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_SUB_ALLOC);
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+    DebugLinuxMemAreaRecordRemove(psLinuxMemArea);
+#endif
+
+
+
+    LinuxMemAreaStructFree(psLinuxMemArea);
+}
+
+
+static LinuxMemArea *
+LinuxMemAreaStructAlloc(IMG_VOID)
+{
+#if 0
+    LinuxMemArea *psLinuxMemArea;
+    psLinuxMemArea = kmem_cache_alloc(psLinuxMemAreaCache, GFP_KERNEL);
+    printk(KERN_ERR "%s: psLinuxMemArea=%p\n", __FUNCTION__, psLinuxMemArea);
+    dump_stack();
+    return psLinuxMemArea;
+#else
+    return KMemCacheAllocWrapper(psLinuxMemAreaCache, GFP_KERNEL);
+#endif
+}
+
+
+static IMG_VOID
+LinuxMemAreaStructFree(LinuxMemArea *psLinuxMemArea)
+{
+    KMemCacheFreeWrapper(psLinuxMemAreaCache, psLinuxMemArea);
+
+
+}
+
+
+IMG_VOID
+LinuxMemAreaDeepFree(LinuxMemArea *psLinuxMemArea)
+{
+    switch(psLinuxMemArea->eAreaType)
+    {
+        case LINUX_MEM_AREA_VMALLOC:
+            FreeVMallocLinuxMemArea(psLinuxMemArea);
+            break;
+        case LINUX_MEM_AREA_ALLOC_PAGES:
+            FreeAllocPagesLinuxMemArea(psLinuxMemArea);
+            break;
+        case LINUX_MEM_AREA_IOREMAP:
+            FreeIORemapLinuxMemArea(psLinuxMemArea);
+            break;
+	case LINUX_MEM_AREA_EXTERNAL_KV:
+	    FreeExternalKVLinuxMemArea(psLinuxMemArea);
+	    break;
+        case LINUX_MEM_AREA_IO:
+            FreeIOLinuxMemArea(psLinuxMemArea);
+            break;
+        case LINUX_MEM_AREA_SUB_ALLOC:
+            FreeSubLinuxMemArea(psLinuxMemArea);
+            break;
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "%s: Unknown are type (%d)\n",
+                     __FUNCTION__, psLinuxMemArea->eAreaType));
+            break;
+    }
+}
+
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+static IMG_VOID
+DebugLinuxMemAreaRecordAdd(LinuxMemArea *psLinuxMemArea, IMG_UINT32 ui32Flags)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psNewRecord;
+    const IMG_CHAR *pi8FlagsString;
+
+    mutex_lock(&g_sDebugMutex);
+
+    if(psLinuxMemArea->eAreaType != LINUX_MEM_AREA_SUB_ALLOC)
+    {
+        g_LinuxMemAreaWaterMark += psLinuxMemArea->ui32ByteSize;
+        if(g_LinuxMemAreaWaterMark > g_LinuxMemAreaHighWaterMark)
+        {
+            g_LinuxMemAreaHighWaterMark = g_LinuxMemAreaWaterMark;
+        }
+    }
+    g_LinuxMemAreaCount++;
+
+
+    psNewRecord = kmalloc(sizeof(DEBUG_LINUX_MEM_AREA_REC), GFP_KERNEL);
+    if(psNewRecord)
+    {
+
+        psNewRecord->psLinuxMemArea = psLinuxMemArea;
+        psNewRecord->ui32Flags = ui32Flags;
+        psNewRecord->pid = current->pid;
+
+		List_DEBUG_LINUX_MEM_AREA_REC_Insert(&g_LinuxMemAreaRecords, psNewRecord);
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+                 "%s: failed to allocate linux memory area record.",
+                 __FUNCTION__));
+    }
+
+
+    pi8FlagsString = HAPFlagsToString(ui32Flags);
+    if(strstr(pi8FlagsString, "UNKNOWN"))
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+                 "%s: Unexpected flags (0x%08lx) associated with psLinuxMemArea @ 0x%08lx",
+                 __FUNCTION__,
+                 ui32Flags,
+                 psLinuxMemArea));
+
+    }
+
+    mutex_unlock(&g_sDebugMutex);
+}
+
+
+
+IMG_VOID* MatchLinuxMemArea_AnyVaCb(DEBUG_LINUX_MEM_AREA_REC *psCurrentRecord,
+												va_list va)
+{
+	LinuxMemArea *psLinuxMemArea;
+
+	psLinuxMemArea = va_arg(va, LinuxMemArea*);
+	if(psCurrentRecord->psLinuxMemArea == psLinuxMemArea)
+	{
+		return psCurrentRecord;
+	}
+	else
+	{
+		return IMG_NULL;
+	}
+}
+
+
+static DEBUG_LINUX_MEM_AREA_REC *
+DebugLinuxMemAreaRecordFind(LinuxMemArea *psLinuxMemArea)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psCurrentRecord;
+
+    mutex_lock(&g_sDebugMutex);
+	psCurrentRecord = List_DEBUG_LINUX_MEM_AREA_REC_Any_va(g_LinuxMemAreaRecords,
+														MatchLinuxMemArea_AnyVaCb,
+														psLinuxMemArea);
+
+    mutex_unlock(&g_sDebugMutex);
+
+    return psCurrentRecord;
+}
+
+
+static IMG_VOID
+DebugLinuxMemAreaRecordRemove(LinuxMemArea *psLinuxMemArea)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psCurrentRecord;
+
+    mutex_lock(&g_sDebugMutex);
+
+    if(psLinuxMemArea->eAreaType != LINUX_MEM_AREA_SUB_ALLOC)
+    {
+        g_LinuxMemAreaWaterMark -= psLinuxMemArea->ui32ByteSize;
+    }
+    g_LinuxMemAreaCount--;
+
+
+	psCurrentRecord = List_DEBUG_LINUX_MEM_AREA_REC_Any_va(g_LinuxMemAreaRecords,
+														MatchLinuxMemArea_AnyVaCb,
+														psLinuxMemArea);
+	if(psCurrentRecord)
+	{
+
+		List_DEBUG_LINUX_MEM_AREA_REC_Remove(psCurrentRecord);
+		kfree(psCurrentRecord);
+	}
+	else
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: couldn't find an entry for psLinuxMemArea=%p\n",
+	     __FUNCTION__, psLinuxMemArea));
+	}
+
+    mutex_unlock(&g_sDebugMutex);
+}
+#endif
+
+
+IMG_VOID *
+LinuxMemAreaToCpuVAddr(LinuxMemArea *psLinuxMemArea)
+{
+    switch(psLinuxMemArea->eAreaType)
+    {
+        case LINUX_MEM_AREA_VMALLOC:
+            return psLinuxMemArea->uData.sVmalloc.pvVmallocAddress;
+        case LINUX_MEM_AREA_IOREMAP:
+            return psLinuxMemArea->uData.sIORemap.pvIORemapCookie;
+	case LINUX_MEM_AREA_EXTERNAL_KV:
+	    return psLinuxMemArea->uData.sExternalKV.pvExternalKV;
+        case LINUX_MEM_AREA_SUB_ALLOC:
+        {
+            IMG_CHAR *pAddr =
+                LinuxMemAreaToCpuVAddr(psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea);
+            if(!pAddr)
+            {
+                return NULL;
+            }
+            return pAddr + psLinuxMemArea->uData.sSubAlloc.ui32ByteOffset;
+        }
+        default:
+            return NULL;
+    }
+}
+
+
+IMG_CPU_PHYADDR
+LinuxMemAreaToCpuPAddr(LinuxMemArea *psLinuxMemArea, IMG_UINT32 ui32ByteOffset)
+{
+    IMG_CPU_PHYADDR CpuPAddr;
+
+    CpuPAddr.uiAddr = 0;
+
+    switch(psLinuxMemArea->eAreaType)
+    {
+        case LINUX_MEM_AREA_IOREMAP:
+        {
+            CpuPAddr = psLinuxMemArea->uData.sIORemap.CPUPhysAddr;
+            CpuPAddr.uiAddr += ui32ByteOffset;
+            break;
+        }
+	case LINUX_MEM_AREA_EXTERNAL_KV:
+	{
+	    if (psLinuxMemArea->uData.sExternalKV.bPhysContig)
+	    {
+		CpuPAddr = SysSysPAddrToCpuPAddr(psLinuxMemArea->uData.sExternalKV.uPhysAddr.SysPhysAddr);
+		CpuPAddr.uiAddr += ui32ByteOffset;
+	    }
+	    else
+	    {
+		IMG_UINT32 ui32PageIndex = PHYS_TO_PFN(ui32ByteOffset);
+		IMG_SYS_PHYADDR SysPAddr = psLinuxMemArea->uData.sExternalKV.uPhysAddr.pSysPhysAddr[ui32PageIndex];
+
+		CpuPAddr = SysSysPAddrToCpuPAddr(SysPAddr);
+                CpuPAddr.uiAddr += ADDR_TO_PAGE_OFFSET(ui32ByteOffset);
+		if (0 == CpuPAddr.uiAddr) {
+			/* FIXME: REPLACE THIS WITH A DIFFERENT FIX SOMEDAY.
+			 * has only seen this code path be triggered when
+			 * DestroyOffsetStruct() calls this as a parameter for a debug
+			 * statement.  The times its been seen has been when the user-mode
+			 * program (i.e. X server) crashed when doing mode changes, or was
+			 * killed after an unhealthy mode change, and the kernel was trying
+			 * to clean up all of the VMA's associated with that client.  For
+			 * now, don't leave the address as 0 (which causes an Oops), but
+			 * work-around this by setting the CpuPAddr.uiAddr to 7.  As long
+			 * as 7 is used for a debug statement, it's no big deal.  If 7 were
+			 * used as a real address, an Oops would be caused anyway.
+			 */
+			printk(KERN_ERR "ERROR DESCRIPTION TO FOLLOW:\n");
+			printk(KERN_ERR "  LinuxMemAreaToCpuPAddr() failed to look up a "
+				"physical\n  address of a memory mapped area (i.e. got an "
+				"address\n  of 0x00000000).  If this occured after increasing "
+				"the\n  resolution of a display, it probably indicates that "
+				"the\n  client stepped past the end of the old frame buffer's\n"
+				"  memory, and needed to re-initialize its connection with\n"
+				"  PVR services in order to map the new frame buffer address."
+				"\n\n");
+			CpuPAddr.uiAddr = 7;
+		}
+	    }
+            break;
+	}
+        case LINUX_MEM_AREA_IO:
+        {
+            CpuPAddr = psLinuxMemArea->uData.sIO.CPUPhysAddr;
+            CpuPAddr.uiAddr += ui32ByteOffset;
+            break;
+        }
+        case LINUX_MEM_AREA_VMALLOC:
+        {
+            IMG_CHAR *pCpuVAddr;
+            pCpuVAddr =
+                (IMG_CHAR *)psLinuxMemArea->uData.sVmalloc.pvVmallocAddress;
+            pCpuVAddr += ui32ByteOffset;
+            CpuPAddr.uiAddr = VMallocToPhys(pCpuVAddr);
+            break;
+        }
+        case LINUX_MEM_AREA_ALLOC_PAGES:
+        {
+            struct page *page;
+            IMG_UINT32 ui32PageIndex = PHYS_TO_PFN(ui32ByteOffset);
+            page = psLinuxMemArea->uData.sPageList.pvPageList[ui32PageIndex];
+            CpuPAddr.uiAddr = page_to_phys(page);
+            CpuPAddr.uiAddr += ADDR_TO_PAGE_OFFSET(ui32ByteOffset);
+            break;
+        }
+        case LINUX_MEM_AREA_SUB_ALLOC:
+        {
+            CpuPAddr =
+                OSMemHandleToCpuPAddr(psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea,
+                                      psLinuxMemArea->uData.sSubAlloc.ui32ByteOffset
+                                        + ui32ByteOffset);
+            break;
+        }
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "%s: Unknown LinuxMemArea type (%d)\n",
+                     __FUNCTION__, psLinuxMemArea->eAreaType));
+            break;
+   }
+
+    PVR_ASSERT(CpuPAddr.uiAddr);
+    return CpuPAddr;
+}
+
+
+IMG_BOOL
+LinuxMemAreaPhysIsContig(LinuxMemArea *psLinuxMemArea)
+{
+    switch(psLinuxMemArea->eAreaType)
+    {
+        case LINUX_MEM_AREA_IOREMAP:
+        case LINUX_MEM_AREA_IO:
+            return IMG_TRUE;
+
+	case LINUX_MEM_AREA_EXTERNAL_KV:
+	    return psLinuxMemArea->uData.sExternalKV.bPhysContig;
+
+        case LINUX_MEM_AREA_VMALLOC:
+        case LINUX_MEM_AREA_ALLOC_PAGES:
+	    return IMG_FALSE;
+
+        case LINUX_MEM_AREA_SUB_ALLOC:
+
+	    return LinuxMemAreaPhysIsContig(psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea);
+
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "%s: Unknown LinuxMemArea type (%d)\n",
+                     __FUNCTION__, psLinuxMemArea->eAreaType));
+	    break;
+    }
+    return IMG_FALSE;
+}
+
+
+const IMG_CHAR *
+LinuxMemAreaTypeToString(LINUX_MEM_AREA_TYPE eMemAreaType)
+{
+
+    switch(eMemAreaType)
+    {
+        case LINUX_MEM_AREA_IOREMAP:
+            return "LINUX_MEM_AREA_IOREMAP";
+	case LINUX_MEM_AREA_EXTERNAL_KV:
+	    return "LINUX_MEM_AREA_EXTERNAL_KV";
+        case LINUX_MEM_AREA_IO:
+            return "LINUX_MEM_AREA_IO";
+        case LINUX_MEM_AREA_VMALLOC:
+            return "LINUX_MEM_AREA_VMALLOC";
+        case LINUX_MEM_AREA_SUB_ALLOC:
+            return "LINUX_MEM_AREA_SUB_ALLOC";
+        case LINUX_MEM_AREA_ALLOC_PAGES:
+            return "LINUX_MEM_AREA_ALLOC_PAGES";
+        default:
+            PVR_ASSERT(0);
+    }
+
+    return "";
+}
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+#if defined(DEBUG_LINUX_MEM_AREAS) || defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+static void ProcSeqStartstopDebugMutex(struct seq_file *sfile, IMG_BOOL start)
+{
+	if(start)
+	{
+	    mutex_lock(&g_sDebugMutex);
+	}
+	else
+	{
+	    mutex_unlock(&g_sDebugMutex);
+	}
+}
+#endif
+#endif
+
+#if defined(DEBUG_LINUX_MEM_AREAS)
+
+IMG_VOID* DecOffMemAreaRec_AnyVaCb(DEBUG_LINUX_MEM_AREA_REC *psNode, va_list va)
+{
+	off_t *pOff = va_arg(va, off_t*);
+	if (--(*pOff))
+	{
+		return IMG_NULL;
+	}
+	else
+	{
+		return psNode;
+	}
+}
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void* ProcSeqNextMemArea(struct seq_file *sfile,void* el,loff_t off)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psRecord;
+	psRecord = (DEBUG_LINUX_MEM_AREA_REC*)
+				List_DEBUG_LINUX_MEM_AREA_REC_Any_va(g_LinuxMemAreaRecords,
+													DecOffMemAreaRec_AnyVaCb,
+													&off);
+	return (void*)psRecord;
+}
+
+static void* ProcSeqOff2ElementMemArea(struct seq_file * sfile, loff_t off)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psRecord;
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+	psRecord = (DEBUG_LINUX_MEM_AREA_REC*)
+				List_DEBUG_LINUX_MEM_AREA_REC_Any_va(g_LinuxMemAreaRecords,
+													DecOffMemAreaRec_AnyVaCb,
+													&off);
+	return (void*)psRecord;
+}
+
+
+static void ProcSeqShowMemArea(struct seq_file *sfile,void* el)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psRecord = (DEBUG_LINUX_MEM_AREA_REC*)el;
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+        seq_printf( sfile,
+			  "Number of Linux Memory Areas: %lu\n"
+                          "At the current water mark these areas correspond to %lu bytes (excluding SUB areas)\n"
+                          "At the highest water mark these areas corresponded to %lu bytes (excluding SUB areas)\n"
+                          "\nDetails for all Linux Memory Areas:\n"
+                          "%s %-24s %s %s %-8s %-5s %s\n",
+                          g_LinuxMemAreaCount,
+                          g_LinuxMemAreaWaterMark,
+                          g_LinuxMemAreaHighWaterMark,
+                          "psLinuxMemArea",
+                          "LinuxMemType",
+                          "CpuVAddr",
+                          "CpuPAddr",
+                          "Bytes",
+                          "Pid",
+                          "Flags"
+                         );
+#else
+        seq_printf( sfile,
+                          "<mem_areas_header>\n"
+                          "\t<count>%lu</count>\n"
+                          "\t<watermark key=\"mar0\" description=\"current\" bytes=\"%lu\"/>\n"
+                          "\t<watermark key=\"mar1\" description=\"high\" bytes=\"%lu\"/>\n"
+                          "</mem_areas_header>\n",
+                          g_LinuxMemAreaCount,
+                          g_LinuxMemAreaWaterMark,
+                          g_LinuxMemAreaHighWaterMark
+                         );
+#endif
+		return;
+	}
+
+        seq_printf( sfile,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+                       "%8p       %-24s %8p %08lx %-8ld %-5u %08lx=(%s)\n",
+#else
+                       "<linux_mem_area>\n"
+                       "\t<pointer>%8p</pointer>\n"
+                       "\t<type>%s</type>\n"
+                       "\t<cpu_virtual>%8p</cpu_virtual>\n"
+                       "\t<cpu_physical>%08lx</cpu_physical>\n"
+                       "\t<bytes>%ld</bytes>\n"
+                       "\t<pid>%u</pid>\n"
+                       "\t<flags>%08lx</flags>\n"
+                       "\t<flags_string>%s</flags_string>\n"
+                       "</linux_mem_area>\n",
+#endif
+                       psRecord->psLinuxMemArea,
+                       LinuxMemAreaTypeToString(psRecord->psLinuxMemArea->eAreaType),
+                       LinuxMemAreaToCpuVAddr(psRecord->psLinuxMemArea),
+                       LinuxMemAreaToCpuPAddr(psRecord->psLinuxMemArea,0).uiAddr,
+                       psRecord->psLinuxMemArea->ui32ByteSize,
+                       psRecord->pid,
+                       psRecord->ui32Flags,
+                       HAPFlagsToString(psRecord->ui32Flags)
+                      );
+
+}
+
+#else
+
+static off_t
+printLinuxMemAreaRecords(IMG_CHAR * buffer, size_t count, off_t off)
+{
+    DEBUG_LINUX_MEM_AREA_REC *psRecord;
+    off_t Ret;
+
+    mutex_lock(&g_sDebugMutex);
+
+    if(!off)
+    {
+        if(count < 500)
+        {
+            Ret = 0;
+            goto unlock_and_return;
+        }
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+        Ret = printAppend(buffer, count, 0,
+                          "Number of Linux Memory Areas: %lu\n"
+                          "At the current water mark these areas correspond to %lu bytes (excluding SUB areas)\n"
+                          "At the highest water mark these areas corresponded to %lu bytes (excluding SUB areas)\n"
+                          "\nDetails for all Linux Memory Areas:\n"
+                          "%s %-24s %s %s %-8s %-5s %s\n",
+                          g_LinuxMemAreaCount,
+                          g_LinuxMemAreaWaterMark,
+                          g_LinuxMemAreaHighWaterMark,
+                          "psLinuxMemArea",
+                          "LinuxMemType",
+                          "CpuVAddr",
+                          "CpuPAddr",
+                          "Bytes",
+                          "Pid",
+                          "Flags"
+                         );
+#else
+        Ret = printAppend(buffer, count, 0,
+                          "<mem_areas_header>\n"
+                          "\t<count>%lu</count>\n"
+                          "\t<watermark key=\"mar0\" description=\"current\" bytes=\"%lu\"/>\n"
+                          "\t<watermark key=\"mar1\" description=\"high\" bytes=\"%lu\"/>\n"
+                          "</mem_areas_header>\n",
+                          g_LinuxMemAreaCount,
+                          g_LinuxMemAreaWaterMark,
+                          g_LinuxMemAreaHighWaterMark
+                         );
+#endif
+        goto unlock_and_return;
+    }
+
+	psRecord = (DEBUG_LINUX_MEM_AREA_REC*)
+				List_DEBUG_LINUX_MEM_AREA_REC_Any_va(g_LinuxMemAreaRecords,
+													DecOffMemAreaRec_AnyVaCb,
+													&off);
+
+    if(!psRecord)
+    {
+        Ret = END_OF_FILE;
+        goto unlock_and_return;
+    }
+
+    if(count < 500)
+    {
+        Ret = 0;
+        goto unlock_and_return;
+    }
+
+    Ret =  printAppend(buffer, count, 0,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+                       "%8p       %-24s %8p %08lx %-8ld %-5u %08lx=(%s)\n",
+#else
+                       "<linux_mem_area>\n"
+                       "\t<pointer>%8p</pointer>\n"
+                       "\t<type>%s</type>\n"
+                       "\t<cpu_virtual>%8p</cpu_virtual>\n"
+                       "\t<cpu_physical>%08lx</cpu_physical>\n"
+                       "\t<bytes>%ld</bytes>\n"
+                       "\t<pid>%u</pid>\n"
+                       "\t<flags>%08lx</flags>\n"
+                       "\t<flags_string>%s</flags_string>\n"
+                       "</linux_mem_area>\n",
+#endif
+                       psRecord->psLinuxMemArea,
+                       LinuxMemAreaTypeToString(psRecord->psLinuxMemArea->eAreaType),
+                       LinuxMemAreaToCpuVAddr(psRecord->psLinuxMemArea),
+                       LinuxMemAreaToCpuPAddr(psRecord->psLinuxMemArea,0).uiAddr,
+                       psRecord->psLinuxMemArea->ui32ByteSize,
+                       psRecord->pid,
+                       psRecord->ui32Flags,
+                       HAPFlagsToString(psRecord->ui32Flags)
+                      );
+
+unlock_and_return:
+    mutex_unlock(&g_sDebugMutex);
+    return Ret;
+}
+#endif
+
+#endif
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+
+IMG_VOID* DecOffMemAllocRec_AnyVaCb(DEBUG_MEM_ALLOC_REC *psNode, va_list va)
+{
+	off_t *pOff = va_arg(va, off_t*);
+	if (--(*pOff))
+	{
+		return IMG_NULL;
+	}
+	else
+	{
+		return psNode;
+	}
+}
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void* ProcSeqNextMemoryRecords(struct seq_file *sfile,void* el,loff_t off)
+{
+    DEBUG_MEM_ALLOC_REC *psRecord;
+	psRecord = (DEBUG_MEM_ALLOC_REC*)
+		List_DEBUG_MEM_ALLOC_REC_Any_va(g_MemoryRecords,
+										DecOffMemAllocRec_AnyVaCb,
+										&off);
+#if defined(DEBUG_LINUX_XML_PROC_FILES)
+	if(!psRecord)
+	{
+		seq_printf( sfile, "</meminfo>\n");
+	}
+#endif
+
+	return (void*)psRecord;
+}
+
+static void* ProcSeqOff2ElementMemoryRecords(struct seq_file *sfile, loff_t off)
+{
+    DEBUG_MEM_ALLOC_REC *psRecord;
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+	psRecord = (DEBUG_MEM_ALLOC_REC*)
+		List_DEBUG_MEM_ALLOC_REC_Any_va(g_MemoryRecords,
+										DecOffMemAllocRec_AnyVaCb,
+										&off);
+
+#if defined(DEBUG_LINUX_XML_PROC_FILES)
+	if(!psRecord)
+	{
+		seq_printf( sfile, "</meminfo>\n");
+	}
+#endif
+
+	return (void*)psRecord;
+}
+
+static void ProcSeqShowMemoryRecords(struct seq_file *sfile,void* el)
+{
+    DEBUG_MEM_ALLOC_REC *psRecord = (DEBUG_MEM_ALLOC_REC*)el;
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via kmalloc",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via kmalloc",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via vmalloc",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via vmalloc",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via alloc_pages",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via alloc_pages",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via ioremap",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via ioremap",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes reserved for \"IO\" memory areas",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated for \"IO\" memory areas",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via kmem_cache_alloc",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via kmem_cache_alloc",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+        seq_printf( sfile, "\n");
+
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "The Current Water Mark for memory allocated from system RAM",
+                           g_SysRAMWaterMark);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "The Highest Water Mark for memory allocated from system RAM",
+                           g_SysRAMHighWaterMark);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "The Current Water Mark for memory allocated from IO memory",
+                           g_IOMemWaterMark);
+        seq_printf( sfile, "%-60s: %ld bytes\n",
+                           "The Highest Water Mark for memory allocated from IO memory",
+                           g_IOMemHighWaterMark);
+
+        seq_printf( sfile, "\n");
+
+		seq_printf( sfile, "Details for all known allocations:\n"
+                           "%-16s %-8s %-8s %-10s %-5s %-10s %s\n",
+                           "Type",
+                           "CpuVAddr",
+                           "CpuPAddr",
+                           "Bytes",
+                           "PID",
+                           "PrivateData",
+                           "Filename:Line");
+
+#else
+
+
+		seq_printf( sfile, "<meminfo>\n<meminfo_header>\n");
+		seq_printf( sfile,
+                           "<watermark key=\"mr0\" description=\"kmalloc_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr1\" description=\"kmalloc_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr2\" description=\"vmalloc_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr3\" description=\"vmalloc_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr4\" description=\"alloc_pages_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr5\" description=\"alloc_pages_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr6\" description=\"ioremap_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr7\" description=\"ioremap_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr8\" description=\"io_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr9\" description=\"io_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr10\" description=\"kmem_cache_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+		seq_printf( sfile,
+                           "<watermark key=\"mr11\" description=\"kmem_cache_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+		seq_printf( sfile,"\n" );
+
+		seq_printf( sfile,
+                           "<watermark key=\"mr14\" description=\"system_ram_current\" bytes=\"%ld\"/>\n",
+                           g_SysRAMWaterMark);
+		seq_printf( sfile,
+                           "<watermark key=\"mr15\" description=\"system_ram_high\" bytes=\"%ld\"/>\n",
+                           g_SysRAMHighWaterMark);
+		seq_printf( sfile,
+                           "<watermark key=\"mr16\" description=\"system_io_current\" bytes=\"%ld\"/>\n",
+                           g_IOMemWaterMark);
+		seq_printf( sfile,
+                           "<watermark key=\"mr17\" description=\"system_io_high\" bytes=\"%ld\"/>\n",
+                           g_IOMemHighWaterMark);
+
+		seq_printf( sfile, "</meminfo_header>\n");
+
+#endif
+		return;
+	}
+
+    if(psRecord->eAllocType != DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE)
+    {
+		seq_printf( sfile,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+                           "%-16s %-8p %08lx %-10ld %-5d %-10s %s:%ld\n",
+#else
+                           "<allocation>\n"
+                           "\t<type>%s</type>\n"
+                           "\t<cpu_virtual>%-8p</cpu_virtual>\n"
+                           "\t<cpu_physical>%08lx</cpu_physical>\n"
+                           "\t<bytes>%ld</bytes>\n"
+                           "\t<pid>%d</pid>\n"
+                           "\t<private>%s</private>\n"
+                           "\t<filename>%s</filename>\n"
+                           "\t<line>%ld</line>\n"
+                           "</allocation>\n",
+#endif
+                           DebugMemAllocRecordTypeToString(psRecord->eAllocType),
+                           psRecord->pvCpuVAddr,
+                           psRecord->ulCpuPAddr,
+                           psRecord->ui32Bytes,
+                           psRecord->pid,
+                           "NULL",
+                           psRecord->pszFileName,
+                           psRecord->ui32Line);
+    }
+    else
+    {
+		seq_printf( sfile,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+                           "%-16s %-8p %08lx %-10ld %-5d %-10s %s:%ld\n",
+#else
+                           "<allocation>\n"
+                           "\t<type>%s</type>\n"
+                           "\t<cpu_virtual>%-8p</cpu_virtual>\n"
+                           "\t<cpu_physical>%08lx</cpu_physical>\n"
+                           "\t<bytes>%ld</bytes>\n"
+                           "\t<pid>%d</pid>\n"
+                           "\t<private>%s</private>\n"
+                           "\t<filename>%s</filename>\n"
+                           "\t<line>%ld</line>\n"
+                           "</allocation>\n",
+#endif
+                           DebugMemAllocRecordTypeToString(psRecord->eAllocType),
+                           psRecord->pvCpuVAddr,
+                           psRecord->ulCpuPAddr,
+                           psRecord->ui32Bytes,
+                           psRecord->pid,
+                           KMemCacheNameWrapper(psRecord->pvPrivateData),
+                           psRecord->pszFileName,
+                           psRecord->ui32Line);
+    }
+}
+
+
+
+#else
+
+static off_t
+printMemoryRecords(IMG_CHAR * buffer, size_t count, off_t off)
+{
+    DEBUG_MEM_ALLOC_REC *psRecord;
+    off_t Ret;
+
+    mutex_lock(&g_sDebugMutex);
+
+    if(!off)
+    {
+        if(count < 1000)
+        {
+            Ret = 0;
+            goto unlock_and_return;
+        }
+
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+
+        Ret =  printAppend(buffer, count, 0, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via kmalloc",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via kmalloc",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via vmalloc",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via vmalloc",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via alloc_pages",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via alloc_pages",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via ioremap",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via ioremap",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes reserved for \"IO\" memory areas",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated for \"IO\" memory areas",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Current Water Mark of bytes allocated via kmem_cache_alloc",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "Highest Water Mark of bytes allocated via kmem_cache_alloc",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+        Ret =  printAppend(buffer, count, Ret, "\n");
+
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "The Current Water Mark for memory allocated from system RAM",
+                           g_SysRAMWaterMark);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "The Highest Water Mark for memory allocated from system RAM",
+                           g_SysRAMHighWaterMark);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "The Current Water Mark for memory allocated from IO memory",
+                           g_IOMemWaterMark);
+        Ret =  printAppend(buffer, count, Ret, "%-60s: %ld bytes\n",
+                           "The Highest Water Mark for memory allocated from IO memory",
+                           g_IOMemHighWaterMark);
+
+        Ret =  printAppend(buffer, count, Ret, "\n");
+
+        Ret =  printAppend(buffer, count, Ret, "Details for all known allocations:\n"
+                           "%-16s %-8s %-8s %-10s %-5s %-10s %s\n",
+                           "Type",
+                           "CpuVAddr",
+                           "CpuPAddr",
+                           "Bytes",
+                           "PID",
+                           "PrivateData",
+                           "Filename:Line");
+
+#else
+
+
+        Ret =  printAppend(buffer, count, 0, "<meminfo>\n<meminfo_header>\n");
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr0\" description=\"kmalloc_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr1\" description=\"kmalloc_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMALLOC]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr2\" description=\"vmalloc_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr3\" description=\"vmalloc_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_VMALLOC]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr4\" description=\"alloc_pages_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr5\" description=\"alloc_pages_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_ALLOC_PAGES]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr6\" description=\"ioremap_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr7\" description=\"ioremap_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IOREMAP]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr8\" description=\"io_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr9\" description=\"io_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_IO]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr10\" description=\"kmem_cache_current\" bytes=\"%ld\"/>\n",
+                           g_WaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr11\" description=\"kmem_cache_high\" bytes=\"%ld\"/>\n",
+                           g_HighWaterMarkData[DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE]);
+        Ret =  printAppend(buffer, count, Ret, "\n");
+
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr14\" description=\"system_ram_current\" bytes=\"%ld\"/>\n",
+                           g_SysRAMWaterMark);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr15\" description=\"system_ram_high\" bytes=\"%ld\"/>\n",
+                           g_SysRAMHighWaterMark);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr16\" description=\"system_io_current\" bytes=\"%ld\"/>\n",
+                           g_IOMemWaterMark);
+        Ret =  printAppend(buffer, count, Ret,
+                           "<watermark key=\"mr17\" description=\"system_io_high\" bytes=\"%ld\"/>\n",
+                           g_IOMemHighWaterMark);
+
+        Ret =  printAppend(buffer, count, Ret, "</meminfo_header>\n");
+
+#endif
+
+        goto unlock_and_return;
+    }
+
+    if(count < 1000)
+    {
+        Ret = 0;
+        goto unlock_and_return;
+    }
+
+	psRecord = (DEBUG_MEM_ALLOC_REC*)
+		List_DEBUG_MEM_ALLOC_REC_Any_va(g_MemoryRecords,
+										DecOffMemAllocRec_AnyVaCb,
+										&off);
+    if(!psRecord)
+    {
+#if defined(DEBUG_LINUX_XML_PROC_FILES)
+		if(off == 0)
+		{
+			Ret =  printAppend(buffer, count, 0, "</meminfo>\n");
+			goto unlock_and_return;
+		}
+#endif
+        Ret = END_OF_FILE;
+        goto unlock_and_return;
+    }
+
+    if(psRecord->eAllocType != DEBUG_MEM_ALLOC_TYPE_KMEM_CACHE)
+    {
+        Ret =  printAppend(buffer, count, 0,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+                           "%-16s %-8p %08lx %-10ld %-5d %-10s %s:%ld\n",
+#else
+                           "<allocation>\n"
+                           "\t<type>%s</type>\n"
+                           "\t<cpu_virtual>%-8p</cpu_virtual>\n"
+                           "\t<cpu_physical>%08lx</cpu_physical>\n"
+                           "\t<bytes>%ld</bytes>\n"
+                           "\t<pid>%d</pid>\n"
+                           "\t<private>%s</private>\n"
+                           "\t<filename>%s</filename>\n"
+                           "\t<line>%ld</line>\n"
+                           "</allocation>\n",
+#endif
+                           DebugMemAllocRecordTypeToString(psRecord->eAllocType),
+                           psRecord->pvCpuVAddr,
+                           psRecord->ulCpuPAddr,
+                           psRecord->ui32Bytes,
+                           psRecord->pid,
+                           "NULL",
+                           psRecord->pszFileName,
+                           psRecord->ui32Line);
+    }
+    else
+    {
+        Ret =  printAppend(buffer, count, 0,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+                           "%-16s %-8p %08lx %-10ld %-5d %-10s %s:%ld\n",
+#else
+                           "<allocation>\n"
+                           "\t<type>%s</type>\n"
+                           "\t<cpu_virtual>%-8p</cpu_virtual>\n"
+                           "\t<cpu_physical>%08lx</cpu_physical>\n"
+                           "\t<bytes>%ld</bytes>\n"
+                           "\t<pid>%d</pid>\n"
+                           "\t<private>%s</private>\n"
+                           "\t<filename>%s</filename>\n"
+                           "\t<line>%ld</line>\n"
+                           "</allocation>\n",
+#endif
+                           DebugMemAllocRecordTypeToString(psRecord->eAllocType),
+                           psRecord->pvCpuVAddr,
+                           psRecord->ulCpuPAddr,
+                           psRecord->ui32Bytes,
+                           psRecord->pid,
+                           KMemCacheNameWrapper(psRecord->pvPrivateData),
+                           psRecord->pszFileName,
+                           psRecord->ui32Line);
+    }
+
+unlock_and_return:
+    mutex_unlock(&g_sDebugMutex);
+    return Ret;
+}
+#endif
+#endif
+
+
+#if defined(DEBUG_LINUX_MEM_AREAS) || defined(DEBUG_LINUX_MMAP_AREAS)
+const IMG_CHAR *
+HAPFlagsToString(IMG_UINT32 ui32Flags)
+{
+    static IMG_CHAR szFlags[50];
+    IMG_INT32 i32Pos = 0;
+    IMG_UINT32 ui32CacheTypeIndex, ui32MapTypeIndex;
+    IMG_CHAR *apszCacheTypes[] = {
+        "UNCACHED",
+        "CACHED",
+        "WRITECOMBINE",
+        "UNKNOWN"
+    };
+    IMG_CHAR *apszMapType[] = {
+        "KERNEL_ONLY",
+        "SINGLE_PROCESS",
+        "MULTI_PROCESS",
+        "FROM_EXISTING_PROCESS",
+        "NO_CPU_VIRTUAL",
+        "UNKNOWN"
+    };
+
+
+    if(ui32Flags & PVRSRV_HAP_UNCACHED){
+        ui32CacheTypeIndex=0;
+    }else if(ui32Flags & PVRSRV_HAP_CACHED){
+        ui32CacheTypeIndex=1;
+    }else if(ui32Flags & PVRSRV_HAP_WRITECOMBINE){
+        ui32CacheTypeIndex=2;
+    }else{
+        ui32CacheTypeIndex=3;
+        PVR_DPF((PVR_DBG_ERROR, "%s: unknown cache type (%u)",
+                 __FUNCTION__, (ui32Flags & PVRSRV_HAP_CACHETYPE_MASK)));
+    }
+
+
+    if(ui32Flags & PVRSRV_HAP_KERNEL_ONLY){
+        ui32MapTypeIndex = 0;
+    }else if(ui32Flags & PVRSRV_HAP_SINGLE_PROCESS){
+        ui32MapTypeIndex = 1;
+    }else if(ui32Flags & PVRSRV_HAP_MULTI_PROCESS){
+        ui32MapTypeIndex = 2;
+    }else if(ui32Flags & PVRSRV_HAP_FROM_EXISTING_PROCESS){
+        ui32MapTypeIndex = 3;
+    }else if(ui32Flags & PVRSRV_HAP_NO_CPU_VIRTUAL){
+        ui32MapTypeIndex = 4;
+    }else{
+        ui32MapTypeIndex = 5;
+        PVR_DPF((PVR_DBG_ERROR, "%s: unknown map type (%u)",
+                 __FUNCTION__, (ui32Flags & PVRSRV_HAP_MAPTYPE_MASK)));
+    }
+
+    i32Pos = sprintf(szFlags, "%s|", apszCacheTypes[ui32CacheTypeIndex]);
+    if (i32Pos <= 0)
+    {
+	PVR_DPF((PVR_DBG_ERROR, "%s: sprintf for cache type %u failed (%d)",
+		__FUNCTION__, ui32CacheTypeIndex, i32Pos));
+	szFlags[0] = 0;
+    }
+    else
+    {
+        sprintf(szFlags + i32Pos, "%s", apszMapType[ui32MapTypeIndex]);
+    }
+
+    return szFlags;
+}
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.h
new file mode 100644
index 0000000..78f3d14
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mm.h
@@ -0,0 +1,323 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __IMG_LINUX_MM_H__
+#define __IMG_LINUX_MM_H__
+
+#include <linux/version.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include <linux/list.h>
+
+#include <asm/io.h>
+
+#define	PHYS_TO_PFN(phys) ((phys) >> PAGE_SHIFT)
+#define PFN_TO_PHYS(pfn) ((pfn) << PAGE_SHIFT)
+
+#define RANGE_TO_PAGES(range) (((range) + (PAGE_SIZE - 1)) >> PAGE_SHIFT)
+
+#define	ADDR_TO_PAGE_OFFSET(addr) (((unsigned long)(addr)) & (PAGE_SIZE - 1))
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
+#define	REMAP_PFN_RANGE(vma, addr, pfn, size, prot) remap_pfn_range(vma, addr, pfn, size, prot)
+#else
+#define	REMAP_PFN_RANGE(vma, addr, pfn, size, prot) remap_page_range(vma, addr, PFN_TO_PHYS(pfn), size, prot)
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
+#define	IO_REMAP_PFN_RANGE(vma, addr, pfn, size, prot) io_remap_pfn_range(vma, addr, pfn, size, prot)
+#else
+#define	IO_REMAP_PFN_RANGE(vma, addr, pfn, size, prot) io_remap_page_range(vma, addr, PFN_TO_PHYS(pfn), size, prot)
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
+#define	VM_INSERT_PAGE(vma, addr, page) vm_insert_page(vma, addr, page)
+#else
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
+#define VM_INSERT_PAGE(vma, addr, page) remap_pfn_range(vma, addr, page_to_pfn(page), PAGE_SIZE, vma->vm_page_prot);
+#else
+#define VM_INSERT_PAGE(vma, addr, page) remap_page_range(vma, addr, page_to_phys(page), PAGE_SIZE, vma->vm_page_prot);
+#endif
+#endif
+
+static inline IMG_UINT32 VMallocToPhys(IMG_VOID *pCpuVAddr)
+{
+	return (page_to_phys(vmalloc_to_page(pCpuVAddr)) + ADDR_TO_PAGE_OFFSET(pCpuVAddr));
+
+}
+
+typedef enum {
+    LINUX_MEM_AREA_IOREMAP,
+	LINUX_MEM_AREA_EXTERNAL_KV,
+    LINUX_MEM_AREA_IO,
+    LINUX_MEM_AREA_VMALLOC,
+    LINUX_MEM_AREA_ALLOC_PAGES,
+    LINUX_MEM_AREA_SUB_ALLOC,
+    LINUX_MEM_AREA_TYPE_COUNT
+}LINUX_MEM_AREA_TYPE;
+
+typedef struct _LinuxMemArea LinuxMemArea;
+
+
+struct _LinuxMemArea {
+    LINUX_MEM_AREA_TYPE eAreaType;
+    union _uData
+    {
+        struct _sIORemap
+        {
+
+            IMG_CPU_PHYADDR CPUPhysAddr;
+            IMG_VOID *pvIORemapCookie;
+        }sIORemap;
+        struct _sExternalKV
+        {
+
+	    IMG_BOOL bPhysContig;
+	    union {
+
+		    IMG_SYS_PHYADDR SysPhysAddr;
+		    IMG_SYS_PHYADDR *pSysPhysAddr;
+	    } uPhysAddr;
+            IMG_VOID *pvExternalKV;
+        }sExternalKV;
+        struct _sIO
+        {
+
+            IMG_CPU_PHYADDR CPUPhysAddr;
+        }sIO;
+        struct _sVmalloc
+        {
+
+            IMG_VOID *pvVmallocAddress;
+        }sVmalloc;
+        struct _sPageList
+        {
+
+            struct page **pvPageList;
+	    IMG_HANDLE hBlockPageList;
+        }sPageList;
+        struct _sSubAlloc
+        {
+
+            LinuxMemArea *psParentLinuxMemArea;
+            IMG_UINT32 ui32ByteOffset;
+        }sSubAlloc;
+    }uData;
+
+    IMG_UINT32 ui32ByteSize;
+
+    IMG_UINT32 ui32AreaFlags;
+
+    IMG_BOOL bMMapRegistered;
+
+
+    struct list_head	sMMapItem;
+
+
+    struct list_head	sMMapOffsetStructList;
+};
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
+typedef kmem_cache_t LinuxKMemCache;
+#else
+typedef struct kmem_cache LinuxKMemCache;
+#endif
+
+
+PVRSRV_ERROR LinuxMMInit(IMG_VOID);
+
+
+IMG_VOID LinuxMMCleanup(IMG_VOID);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define KMallocWrapper(ui32ByteSize) _KMallocWrapper(ui32ByteSize, __FILE__, __LINE__)
+#else
+#define KMallocWrapper(ui32ByteSize) _KMallocWrapper(ui32ByteSize, NULL, 0)
+#endif
+IMG_VOID *_KMallocWrapper(IMG_UINT32 ui32ByteSize, IMG_CHAR *szFileName, IMG_UINT32 ui32Line);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define KFreeWrapper(pvCpuVAddr) _KFreeWrapper(pvCpuVAddr, __FILE__, __LINE__)
+#else
+#define KFreeWrapper(pvCpuVAddr) _KFreeWrapper(pvCpuVAddr, NULL, 0)
+#endif
+IMG_VOID _KFreeWrapper(IMG_VOID *pvCpuVAddr, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define VMallocWrapper(ui32Bytes, ui32AllocFlags) _VMallocWrapper(ui32Bytes, ui32AllocFlags, __FILE__, __LINE__)
+#else
+#define VMallocWrapper(ui32Bytes, ui32AllocFlags) _VMallocWrapper(ui32Bytes, ui32AllocFlags, NULL, 0)
+#endif
+IMG_VOID *_VMallocWrapper(IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AllocFlags, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define VFreeWrapper(pvCpuVAddr) _VFreeWrapper(pvCpuVAddr, __FILE__, __LINE__)
+#else
+#define VFreeWrapper(pvCpuVAddr) _VFreeWrapper(pvCpuVAddr, NULL, 0)
+#endif
+IMG_VOID _VFreeWrapper(IMG_VOID *pvCpuVAddr, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+
+
+LinuxMemArea *NewVMallocLinuxMemArea(IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AreaFlags);
+
+
+IMG_VOID FreeVMallocLinuxMemArea(LinuxMemArea *psLinuxMemArea);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags) \
+    _IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags, __FILE__, __LINE__)
+#else
+#define IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags) \
+    _IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags, NULL, 0)
+#endif
+IMG_VOID *_IORemapWrapper(IMG_CPU_PHYADDR BasePAddr,
+                          IMG_UINT32 ui32Bytes,
+                          IMG_UINT32 ui32MappingFlags,
+                          IMG_CHAR *pszFileName,
+                          IMG_UINT32 ui32Line);
+
+
+LinuxMemArea *NewIORemapLinuxMemArea(IMG_CPU_PHYADDR BasePAddr, IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AreaFlags);
+
+
+IMG_VOID FreeIORemapLinuxMemArea(LinuxMemArea *psLinuxMemArea);
+
+LinuxMemArea *NewExternalKVLinuxMemArea(IMG_SYS_PHYADDR *pBasePAddr, IMG_VOID *pvCPUVAddr, IMG_UINT32 ui32Bytes, IMG_BOOL bPhysContig, IMG_UINT32 ui32AreaFlags);
+
+
+IMG_VOID FreeExternalKVLinuxMemArea(LinuxMemArea *psLinuxMemArea);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define IOUnmapWrapper(pvIORemapCookie) \
+    _IOUnmapWrapper(pvIORemapCookie, __FILE__, __LINE__)
+#else
+#define IOUnmapWrapper(pvIORemapCookie) \
+    _IOUnmapWrapper(pvIORemapCookie, NULL, 0)
+#endif
+IMG_VOID _IOUnmapWrapper(IMG_VOID *pvIORemapCookie, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+
+
+struct page *LinuxMemAreaOffsetToPage(LinuxMemArea *psLinuxMemArea, IMG_UINT32 ui32ByteOffset);
+
+
+LinuxKMemCache *KMemCacheCreateWrapper(IMG_CHAR *pszName, size_t Size, size_t Align, IMG_UINT32 ui32Flags);
+
+
+IMG_VOID KMemCacheDestroyWrapper(LinuxKMemCache *psCache);
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define KMemCacheAllocWrapper(psCache, Flags) _KMemCacheAllocWrapper(psCache, Flags, __FILE__, __LINE__)
+#else
+#define KMemCacheAllocWrapper(psCache, Flags) _KMemCacheAllocWrapper(psCache, Flags, NULL, 0)
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14))
+IMG_VOID *_KMemCacheAllocWrapper(LinuxKMemCache *psCache, gfp_t Flags, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+#else
+IMG_VOID *_KMemCacheAllocWrapper(LinuxKMemCache *psCache, int Flags, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+#endif
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+#define KMemCacheFreeWrapper(psCache, pvObject) _KMemCacheFreeWrapper(psCache, pvObject, __FILE__, __LINE__)
+#else
+#define KMemCacheFreeWrapper(psCache, pvObject) _KMemCacheFreeWrapper(psCache, pvObject, NULL, 0)
+#endif
+IMG_VOID _KMemCacheFreeWrapper(LinuxKMemCache *psCache, IMG_VOID *pvObject, IMG_CHAR *pszFileName, IMG_UINT32 ui32Line);
+
+
+const IMG_CHAR *KMemCacheNameWrapper(LinuxKMemCache *psCache);
+
+
+LinuxMemArea *NewIOLinuxMemArea(IMG_CPU_PHYADDR BasePAddr, IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AreaFlags);
+
+
+IMG_VOID FreeIOLinuxMemArea(LinuxMemArea *psLinuxMemArea);
+
+
+LinuxMemArea *NewAllocPagesLinuxMemArea(IMG_UINT32 ui32Bytes, IMG_UINT32 ui32AreaFlags);
+
+
+IMG_VOID FreeAllocPagesLinuxMemArea(LinuxMemArea *psLinuxMemArea);
+
+
+LinuxMemArea *NewSubLinuxMemArea(LinuxMemArea *psParentLinuxMemArea,
+                                 IMG_UINT32 ui32ByteOffset,
+                                 IMG_UINT32 ui32Bytes);
+
+
+IMG_VOID LinuxMemAreaDeepFree(LinuxMemArea *psLinuxMemArea);
+
+
+#if defined(LINUX_MEM_AREAS_DEBUG)
+IMG_VOID LinuxMemAreaRegister(LinuxMemArea *psLinuxMemArea);
+#else
+#define LinuxMemAreaRegister(X)
+#endif
+
+
+IMG_VOID *LinuxMemAreaToCpuVAddr(LinuxMemArea *psLinuxMemArea);
+
+
+IMG_CPU_PHYADDR LinuxMemAreaToCpuPAddr(LinuxMemArea *psLinuxMemArea, IMG_UINT32 ui32ByteOffset);
+
+
+#define	 LinuxMemAreaToCpuPFN(psLinuxMemArea, ui32ByteOffset) PHYS_TO_PFN(LinuxMemAreaToCpuPAddr(psLinuxMemArea, ui32ByteOffset).uiAddr)
+
+IMG_BOOL LinuxMemAreaPhysIsContig(LinuxMemArea *psLinuxMemArea);
+
+static inline LinuxMemArea *
+LinuxMemAreaRoot(LinuxMemArea *psLinuxMemArea)
+{
+    if(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_SUB_ALLOC)
+    {
+        return psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea;
+    }
+    else
+    {
+        return psLinuxMemArea;
+    }
+}
+
+
+static inline LINUX_MEM_AREA_TYPE
+LinuxMemAreaRootType(LinuxMemArea *psLinuxMemArea)
+{
+    return LinuxMemAreaRoot(psLinuxMemArea)->eAreaType;
+}
+
+
+const IMG_CHAR *LinuxMemAreaTypeToString(LINUX_MEM_AREA_TYPE eMemAreaType);
+
+
+#if defined(DEBUG) || defined(DEBUG_LINUX_MEM_AREAS)
+const IMG_CHAR *HAPFlagsToString(IMG_UINT32 ui32Flags);
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.c
new file mode 100644
index 0000000..a273689
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.c
@@ -0,0 +1,1149 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/version.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
+#include <linux/wrapper.h>
+#endif
+#include <linux/slab.h>
+#include <asm/io.h>
+#include <asm/page.h>
+#include <asm/shmparam.h>
+#include <asm/pgtable.h>
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
+#include <linux/sched.h>
+#include <asm/current.h>
+#endif
+#if defined(SUPPORT_DRI_DRM)
+#include <drm/drmP.h>
+#endif
+
+#include "img_defs.h"
+#include "services.h"
+#include "servicesint.h"
+#include "pvrmmap.h"
+#include "mutils.h"
+#include "mmap.h"
+#include "mm.h"
+#include "pvr_debug.h"
+#include "osfunc.h"
+#include "proc.h"
+#include "mutex.h"
+#include "handle.h"
+#include "perproc.h"
+#include "env_perproc.h"
+#include "bridged_support.h"
+#if defined(SUPPORT_DRI_DRM)
+#include "pvr_drm.h"
+#endif
+
+#if !defined(PVR_SECURE_HANDLES)
+#error "The mmap code requires PVR_SECURE_HANDLES"
+#endif
+
+static struct mutex g_sMMapMutex;
+
+static LinuxKMemCache *g_psMemmapCache = NULL;
+static LIST_HEAD(g_sMMapAreaList);
+static LIST_HEAD(g_sMMapOffsetStructList);
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+static IMG_UINT32 g_ui32RegisteredAreas = 0;
+static IMG_UINT32 g_ui32TotalByteSize = 0;
+#endif
+
+
+#if defined(PVR_PROC_USE_SEQ_FILE) && defined(DEBUG_LINUX_MMAP_AREAS)
+static struct proc_dir_entry *g_ProcMMap;
+#endif
+
+#define	FIRST_PHYSICAL_PFN	0
+#define	LAST_PHYSICAL_PFN	0x7fffffffUL
+#define	FIRST_SPECIAL_PFN	(LAST_PHYSICAL_PFN + 1)
+#define	LAST_SPECIAL_PFN	0xffffffffUL
+
+#define	MAX_MMAP_HANDLE		0x7fffffffUL
+
+static inline IMG_BOOL
+PFNIsPhysical(IMG_UINT32 pfn)
+{
+
+	return ((pfn >= FIRST_PHYSICAL_PFN) && (pfn <= LAST_PHYSICAL_PFN)) ? IMG_TRUE : IMG_FALSE;
+}
+
+static inline IMG_BOOL
+PFNIsSpecial(IMG_UINT32 pfn)
+{
+
+	return ((pfn >= FIRST_SPECIAL_PFN) && (pfn <= LAST_SPECIAL_PFN)) ? IMG_TRUE : IMG_FALSE;
+}
+
+static inline IMG_HANDLE
+MMapOffsetToHandle(IMG_UINT32 pfn)
+{
+	if (PFNIsPhysical(pfn))
+	{
+		PVR_ASSERT(PFNIsPhysical(pfn));
+		return IMG_NULL;
+	}
+
+	return (IMG_HANDLE)(pfn - FIRST_SPECIAL_PFN);
+}
+
+static inline IMG_UINT32
+HandleToMMapOffset(IMG_HANDLE hHandle)
+{
+	IMG_UINT32 ulHandle = (IMG_UINT32)hHandle;
+
+	if (PFNIsSpecial(ulHandle))
+	{
+		PVR_ASSERT(PFNIsSpecial(ulHandle));
+		return 0;
+	}
+
+	return ulHandle + FIRST_SPECIAL_PFN;
+}
+
+static inline IMG_BOOL
+LinuxMemAreaUsesPhysicalMap(LinuxMemArea *psLinuxMemArea)
+{
+    return LinuxMemAreaPhysIsContig(psLinuxMemArea);
+}
+
+static inline IMG_UINT32
+GetCurrentThreadID(IMG_VOID)
+{
+
+	return (IMG_UINT32)current->pid;
+}
+
+static PKV_OFFSET_STRUCT
+CreateOffsetStruct(LinuxMemArea *psLinuxMemArea, IMG_UINT32 ui32Offset, IMG_UINT32 ui32RealByteSize)
+{
+    PKV_OFFSET_STRUCT psOffsetStruct;
+#if defined(DEBUG) || defined(DEBUG_LINUX_MMAP_AREAS)
+    const IMG_CHAR *pszName = LinuxMemAreaTypeToString(LinuxMemAreaRootType(psLinuxMemArea));
+#endif
+
+#if defined(DEBUG) || defined(DEBUG_LINUX_MMAP_AREAS)
+    PVR_DPF((PVR_DBG_MESSAGE,
+             "%s(%s, psLinuxMemArea: 0x%p, ui32AllocFlags: 0x%8lx)",
+             __FUNCTION__, pszName, psLinuxMemArea, psLinuxMemArea->ui32AreaFlags));
+#endif
+
+    PVR_ASSERT(psLinuxMemArea->eAreaType != LINUX_MEM_AREA_SUB_ALLOC || LinuxMemAreaRoot(psLinuxMemArea)->eAreaType != LINUX_MEM_AREA_SUB_ALLOC);
+
+    PVR_ASSERT(psLinuxMemArea->bMMapRegistered);
+
+    psOffsetStruct = KMemCacheAllocWrapper(g_psMemmapCache, GFP_KERNEL);
+    if(psOffsetStruct == IMG_NULL)
+    {
+        PVR_DPF((PVR_DBG_ERROR,"PVRMMapRegisterArea: Couldn't alloc another mapping record from cache"));
+        return IMG_NULL;
+    }
+
+    psOffsetStruct->ui32MMapOffset = ui32Offset;
+
+    psOffsetStruct->psLinuxMemArea = psLinuxMemArea;
+
+    psOffsetStruct->ui32Mapped = 0;
+
+    psOffsetStruct->ui32RealByteSize = ui32RealByteSize;
+
+
+    psOffsetStruct->ui32TID = GetCurrentThreadID();
+
+    psOffsetStruct->ui32PID = OSGetCurrentProcessIDKM();
+
+    psOffsetStruct->bOnMMapList = IMG_FALSE;
+
+    psOffsetStruct->ui32RefCount = 0;
+
+    psOffsetStruct->ui32UserVAddr = 0;
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+
+    psOffsetStruct->pszName = pszName;
+#endif
+
+    list_add_tail(&psOffsetStruct->sAreaItem, &psLinuxMemArea->sMMapOffsetStructList);
+
+    return psOffsetStruct;
+}
+
+
+static IMG_VOID
+DestroyOffsetStruct(PKV_OFFSET_STRUCT psOffsetStruct)
+{
+    list_del(&psOffsetStruct->sAreaItem);
+
+    if (psOffsetStruct->bOnMMapList)
+    {
+        list_del(&psOffsetStruct->sMMapItem);
+    }
+
+    PVR_DPF((PVR_DBG_MESSAGE, "%s: Table entry: "
+             "psLinuxMemArea=0x%08lX, CpuPAddr=0x%08lX", __FUNCTION__,
+             psOffsetStruct->psLinuxMemArea,
+             LinuxMemAreaToCpuPAddr(psOffsetStruct->psLinuxMemArea, 0)));
+
+    KMemCacheFreeWrapper(g_psMemmapCache, psOffsetStruct);
+}
+
+
+static inline IMG_VOID
+DetermineUsersSizeAndByteOffset(LinuxMemArea *psLinuxMemArea,
+                               IMG_UINT32 *pui32RealByteSize,
+                               IMG_UINT32 *pui32ByteOffset)
+{
+    IMG_UINT32 ui32PageAlignmentOffset;
+    IMG_CPU_PHYADDR CpuPAddr;
+
+    CpuPAddr = LinuxMemAreaToCpuPAddr(psLinuxMemArea, 0);
+    ui32PageAlignmentOffset = ADDR_TO_PAGE_OFFSET(CpuPAddr.uiAddr);
+
+    *pui32ByteOffset = ui32PageAlignmentOffset;
+
+    *pui32RealByteSize = PAGE_ALIGN(psLinuxMemArea->ui32ByteSize + ui32PageAlignmentOffset);
+}
+
+
+PVRSRV_ERROR
+PVRMMapOSMemHandleToMMapData(PVRSRV_PER_PROCESS_DATA *psPerProc,
+				IMG_HANDLE hMHandle,
+                                IMG_UINT32 *pui32MMapOffset,
+                                IMG_UINT32 *pui32ByteOffset,
+                                IMG_UINT32 *pui32RealByteSize,
+				IMG_UINT32 *pui32UserVAddr)
+{
+    LinuxMemArea *psLinuxMemArea;
+    PKV_OFFSET_STRUCT psOffsetStruct;
+    IMG_HANDLE hOSMemHandle;
+    PVRSRV_ERROR eError;
+
+    mutex_lock(&g_sMMapMutex);
+
+    PVR_ASSERT(PVRSRVGetMaxHandle(psPerProc->psHandleBase) <= MAX_MMAP_HANDLE);
+
+    eError = PVRSRVLookupOSMemHandle(psPerProc->psHandleBase, &hOSMemHandle, hMHandle);
+    if (eError != PVRSRV_OK)
+    {
+	PVR_DPF((PVR_DBG_ERROR, "%s: Lookup of handle 0x%lx failed", __FUNCTION__, hMHandle));
+
+	goto exit_unlock;
+    }
+
+    psLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+
+    DetermineUsersSizeAndByteOffset(psLinuxMemArea,
+                                   pui32RealByteSize,
+                                   pui32ByteOffset);
+
+
+    list_for_each_entry(psOffsetStruct, &psLinuxMemArea->sMMapOffsetStructList, sAreaItem)
+    {
+        if (psPerProc->ui32PID == psOffsetStruct->ui32PID)
+        {
+
+	   PVR_ASSERT(*pui32RealByteSize == psOffsetStruct->ui32RealByteSize);
+
+	   *pui32MMapOffset = psOffsetStruct->ui32MMapOffset;
+	   *pui32UserVAddr = psOffsetStruct->ui32UserVAddr;
+	   psOffsetStruct->ui32RefCount++;
+
+	   eError = PVRSRV_OK;
+	   goto exit_unlock;
+        }
+    }
+
+
+    *pui32UserVAddr = 0;
+
+    if (LinuxMemAreaUsesPhysicalMap(psLinuxMemArea))
+    {
+        *pui32MMapOffset = LinuxMemAreaToCpuPFN(psLinuxMemArea, 0);
+        PVR_ASSERT(PFNIsPhysical(*pui32MMapOffset));
+    }
+    else
+    {
+        *pui32MMapOffset = HandleToMMapOffset(hMHandle);
+        PVR_ASSERT(PFNIsSpecial(*pui32MMapOffset));
+    }
+
+    psOffsetStruct = CreateOffsetStruct(psLinuxMemArea, *pui32MMapOffset, *pui32RealByteSize);
+    if (psOffsetStruct == IMG_NULL)
+    {
+        eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+	goto exit_unlock;
+    }
+
+
+    list_add_tail(&psOffsetStruct->sMMapItem, &g_sMMapOffsetStructList);
+
+    psOffsetStruct->bOnMMapList = IMG_TRUE;
+
+    psOffsetStruct->ui32RefCount++;
+
+    eError = PVRSRV_OK;
+
+exit_unlock:
+    mutex_unlock(&g_sMMapMutex);
+
+    return eError;
+}
+
+
+PVRSRV_ERROR
+PVRMMapReleaseMMapData(PVRSRV_PER_PROCESS_DATA *psPerProc,
+				IMG_HANDLE hMHandle,
+				IMG_BOOL *pbMUnmap,
+				IMG_UINT32 *pui32RealByteSize,
+                                IMG_UINT32 *pui32UserVAddr)
+{
+    LinuxMemArea *psLinuxMemArea;
+    PKV_OFFSET_STRUCT psOffsetStruct;
+    IMG_HANDLE hOSMemHandle;
+    PVRSRV_ERROR eError;
+    IMG_UINT32 ui32PID = OSGetCurrentProcessIDKM();
+
+    mutex_lock(&g_sMMapMutex);
+
+    PVR_ASSERT(PVRSRVGetMaxHandle(psPerProc->psHandleBase) <= MAX_MMAP_HANDLE);
+
+    eError = PVRSRVLookupOSMemHandle(psPerProc->psHandleBase, &hOSMemHandle, hMHandle);
+    if (eError != PVRSRV_OK)
+    {
+	PVR_DPF((PVR_DBG_ERROR, "%s: Lookup of handle 0x%lx failed", __FUNCTION__, hMHandle));
+
+	goto exit_unlock;
+    }
+
+    psLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+
+
+    list_for_each_entry(psOffsetStruct, &psLinuxMemArea->sMMapOffsetStructList, sAreaItem)
+    {
+        if (psOffsetStruct->ui32PID == ui32PID)
+        {
+	    if (psOffsetStruct->ui32RefCount == 0)
+	    {
+		PVR_DPF((PVR_DBG_ERROR, "%s: Attempt to release mmap data with zero reference count for offset struct 0x%p, memory area 0x%p", __FUNCTION__, psOffsetStruct, psLinuxMemArea));
+		eError = PVRSRV_ERROR_GENERIC;
+		goto exit_unlock;
+	    }
+
+	    psOffsetStruct->ui32RefCount--;
+
+	    *pbMUnmap = (IMG_BOOL)((psOffsetStruct->ui32RefCount == 0) && (psOffsetStruct->ui32UserVAddr != 0));
+
+	    *pui32UserVAddr = (*pbMUnmap) ? psOffsetStruct->ui32UserVAddr : 0;
+	    *pui32RealByteSize = (*pbMUnmap) ? psOffsetStruct->ui32RealByteSize : 0;
+
+	    eError = PVRSRV_OK;
+	    goto exit_unlock;
+        }
+    }
+
+
+    PVR_DPF((PVR_DBG_ERROR, "%s: Mapping data not found for handle 0x%lx (memory area 0x%p)", __FUNCTION__, hMHandle, psLinuxMemArea));
+
+    eError =  PVRSRV_ERROR_GENERIC;
+
+exit_unlock:
+    mutex_unlock(&g_sMMapMutex);
+
+    return eError;
+}
+
+static inline PKV_OFFSET_STRUCT
+FindOffsetStructByOffset(IMG_UINT32 ui32Offset, IMG_UINT32 ui32RealByteSize)
+{
+    PKV_OFFSET_STRUCT psOffsetStruct;
+    IMG_UINT32 ui32TID = GetCurrentThreadID();
+    IMG_UINT32 ui32PID = OSGetCurrentProcessIDKM();
+
+    list_for_each_entry(psOffsetStruct, &g_sMMapOffsetStructList, sMMapItem)
+    {
+        if (ui32Offset == psOffsetStruct->ui32MMapOffset && ui32RealByteSize == psOffsetStruct->ui32RealByteSize && psOffsetStruct->ui32PID == ui32PID)
+        {
+
+	    if (!PFNIsPhysical(ui32Offset) || psOffsetStruct->ui32TID == ui32TID)
+	    {
+	        return psOffsetStruct;
+	    }
+        }
+    }
+
+    return IMG_NULL;
+}
+
+
+static IMG_BOOL
+DoMapToUser(LinuxMemArea *psLinuxMemArea,
+            struct vm_area_struct* ps_vma,
+            IMG_UINT32 ui32ByteOffset)
+{
+    IMG_UINT32 ui32ByteSize;
+
+    if (psLinuxMemArea->eAreaType == LINUX_MEM_AREA_SUB_ALLOC)
+    {
+        return DoMapToUser(LinuxMemAreaRoot(psLinuxMemArea),
+                    ps_vma,
+                    psLinuxMemArea->uData.sSubAlloc.ui32ByteOffset + ui32ByteOffset);
+    }
+
+
+    ui32ByteSize = ps_vma->vm_end - ps_vma->vm_start;
+    PVR_ASSERT(ADDR_TO_PAGE_OFFSET(ui32ByteSize) == 0);
+
+#if defined (__sparc__)
+
+#error "SPARC not supported"
+#endif
+
+    if (PFNIsPhysical(ps_vma->vm_pgoff))
+    {
+	IMG_INT result;
+
+	PVR_ASSERT(LinuxMemAreaPhysIsContig(psLinuxMemArea));
+	PVR_ASSERT(LinuxMemAreaToCpuPFN(psLinuxMemArea, ui32ByteOffset) == ps_vma->vm_pgoff);
+
+
+	result = IO_REMAP_PFN_RANGE(ps_vma, ps_vma->vm_start, ps_vma->vm_pgoff, ui32ByteSize, ps_vma->vm_page_prot);
+
+        if(result == 0)
+        {
+            return IMG_TRUE;
+        }
+
+        PVR_DPF((PVR_DBG_MESSAGE, "%s: Failed to map contiguous physical address range (%d), trying non-contiguous path", __FUNCTION__, result));
+    }
+
+    {
+
+        IMG_UINT32 ulVMAPos;
+	IMG_UINT32 ui32ByteEnd = ui32ByteOffset + ui32ByteSize;
+	IMG_UINT32 ui32PA;
+
+
+	for(ui32PA = ui32ByteOffset; ui32PA < ui32ByteEnd; ui32PA += PAGE_SIZE)
+	{
+	    IMG_UINT32 pfn =  LinuxMemAreaToCpuPFN(psLinuxMemArea, ui32PA);
+
+	    if (!pfn_valid(pfn))
+	    {
+                PVR_DPF((PVR_DBG_ERROR,"%s: Error - PFN invalid: 0x%lx", __FUNCTION__, pfn));
+                return IMG_FALSE;
+	    }
+	}
+
+
+        ulVMAPos = ps_vma->vm_start;
+	for(ui32PA = ui32ByteOffset; ui32PA < ui32ByteEnd; ui32PA += PAGE_SIZE)
+	{
+	    IMG_UINT32 pfn;
+	    struct page *psPage;
+	    IMG_INT result;
+
+	    pfn =  LinuxMemAreaToCpuPFN(psLinuxMemArea, ui32PA);
+	    PVR_ASSERT(pfn_valid(pfn));
+
+	    psPage = pfn_to_page(pfn);
+
+	    result = VM_INSERT_PAGE(ps_vma,  ulVMAPos, psPage);
+            if(result != 0)
+            {
+                PVR_DPF((PVR_DBG_ERROR,"%s: Error - VM_INSERT_PAGE failed (%d)", __FUNCTION__, result));
+                return IMG_FALSE;
+            }
+            ulVMAPos += PAGE_SIZE;
+        }
+    }
+
+    return IMG_TRUE;
+}
+
+
+static IMG_VOID
+MMapVOpenNoLock(struct vm_area_struct* ps_vma)
+{
+    PKV_OFFSET_STRUCT psOffsetStruct = (PKV_OFFSET_STRUCT)ps_vma->vm_private_data;
+    PVR_ASSERT(psOffsetStruct != IMG_NULL)
+    psOffsetStruct->ui32Mapped++;
+    PVR_ASSERT(!psOffsetStruct->bOnMMapList);
+
+    if (psOffsetStruct->ui32Mapped > 1)
+    {
+	PVR_DPF((PVR_DBG_WARNING, "%s: Offset structure 0x%p is being shared across processes (psOffsetStruct->ui32Mapped: %lu)", __FUNCTION__, psOffsetStruct, psOffsetStruct->ui32Mapped));
+        PVR_ASSERT((ps_vma->vm_flags & VM_DONTCOPY) == 0);
+    }
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+
+    PVR_DPF((PVR_DBG_MESSAGE,
+             "%s: psLinuxMemArea 0x%p, KVAddress 0x%p MMapOffset %ld, ui32Mapped %d",
+             __FUNCTION__,
+             psOffsetStruct->psLinuxMemArea,
+             LinuxMemAreaToCpuVAddr(psOffsetStruct->psLinuxMemArea),
+             psOffsetStruct->ui32MMapOffset,
+             psOffsetStruct->ui32Mapped));
+#endif
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
+    MOD_INC_USE_COUNT;
+#endif
+}
+
+
+static void
+MMapVOpen(struct vm_area_struct* ps_vma)
+{
+    mutex_lock(&g_sMMapMutex);
+
+    MMapVOpenNoLock(ps_vma);
+
+    mutex_unlock(&g_sMMapMutex);
+}
+
+
+static IMG_VOID
+MMapVCloseNoLock(struct vm_area_struct* ps_vma)
+{
+    PKV_OFFSET_STRUCT psOffsetStruct = (PKV_OFFSET_STRUCT)ps_vma->vm_private_data;
+    PVR_ASSERT(psOffsetStruct != IMG_NULL)
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+    PVR_DPF((PVR_DBG_MESSAGE,
+             "%s: psLinuxMemArea 0x%p, CpuVAddr 0x%p ui32MMapOffset %ld, ui32Mapped %d",
+             __FUNCTION__,
+             psOffsetStruct->psLinuxMemArea,
+             LinuxMemAreaToCpuVAddr(psOffsetStruct->psLinuxMemArea),
+             psOffsetStruct->ui32MMapOffset,
+             psOffsetStruct->ui32Mapped));
+#endif
+
+    PVR_ASSERT(!psOffsetStruct->bOnMMapList);
+    psOffsetStruct->ui32Mapped--;
+    if (psOffsetStruct->ui32Mapped == 0)
+    {
+	if (psOffsetStruct->ui32RefCount != 0)
+	{
+	        PVR_DPF((PVR_DBG_MESSAGE, "%s: psOffsetStruct 0x%p has non-zero reference count (ui32RefCount = %lu). User mode address of start of mapping: 0x%lx", __FUNCTION__, psOffsetStruct, psOffsetStruct->ui32RefCount, psOffsetStruct->ui32UserVAddr));
+	}
+
+	DestroyOffsetStruct(psOffsetStruct);
+    }
+
+    ps_vma->vm_private_data = NULL;
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
+    MOD_DEC_USE_COUNT;
+#endif
+}
+
+static void
+MMapVClose(struct vm_area_struct* ps_vma)
+{
+    mutex_lock(&g_sMMapMutex);
+
+    MMapVCloseNoLock(ps_vma);
+
+    mutex_unlock(&g_sMMapMutex);
+}
+
+
+static struct vm_operations_struct MMapIOOps =
+{
+	.open=MMapVOpen,
+	.close=MMapVClose
+};
+
+
+int
+PVRMMap(struct file* pFile, struct vm_area_struct* ps_vma)
+{
+    IMG_UINT32 ui32ByteSize;
+    PKV_OFFSET_STRUCT psOffsetStruct;
+    int iRetVal = 0;
+
+    PVR_UNREFERENCED_PARAMETER(pFile);
+
+    mutex_lock(&g_sMMapMutex);
+
+    ui32ByteSize = ps_vma->vm_end - ps_vma->vm_start;
+
+
+    PVR_DPF((PVR_DBG_MESSAGE, "%s: Received mmap(2) request with ui32MMapOffset 0x%08lx,"
+                              " and ui32ByteSize %ld(0x%08lx)",
+            __FUNCTION__,
+            ps_vma->vm_pgoff,
+            ui32ByteSize, ui32ByteSize));
+
+    psOffsetStruct = FindOffsetStructByOffset(ps_vma->vm_pgoff, ui32ByteSize);
+    if (psOffsetStruct == IMG_NULL)
+    {
+#if defined(SUPPORT_DRI_DRM)
+        mutex_unlock(&g_sMMapMutex);
+
+
+        return drm_mmap(pFile, ps_vma);
+#else
+        PVR_UNREFERENCED_PARAMETER(pFile);
+
+#if 0 /* FIXME: crash when call to print debug messages */
+        PVR_DPF((PVR_DBG_ERROR,
+             "%s: Attempted to mmap unregistered area at vm_pgoff %ld",
+             __FUNCTION__, ps_vma->vm_pgoff));
+#endif
+        iRetVal = -EINVAL;
+#endif
+        goto unlock_and_return;
+    }
+    list_del(&psOffsetStruct->sMMapItem);
+    psOffsetStruct->bOnMMapList = IMG_FALSE;
+
+
+    if (((ps_vma->vm_flags & VM_WRITE) != 0) &&
+        ((ps_vma->vm_flags & VM_SHARED) == 0))
+    {
+#if 0 /* FIXME: crash when call to print debug messages */
+        PVR_DPF((PVR_DBG_ERROR, "%s: Cannot mmap non-shareable writable areas", __FUNCTION__));
+#endif
+        iRetVal = -EINVAL;
+        goto unlock_and_return;
+    }
+
+
+#if 0 /* FIXME: crash when call to print debug messages */
+    PVR_DPF((PVR_DBG_MESSAGE, "%s: Mapped psLinuxMemArea 0x%p\n",
+         __FUNCTION__, psOffsetStruct->psLinuxMemArea));
+#endif
+    ps_vma->vm_flags |= VM_RESERVED;
+    ps_vma->vm_flags |= VM_IO;
+
+
+    ps_vma->vm_flags |= VM_DONTEXPAND;
+
+
+    ps_vma->vm_flags |= VM_DONTCOPY;
+
+    ps_vma->vm_private_data = (void *)psOffsetStruct;
+
+    switch(psOffsetStruct->psLinuxMemArea->ui32AreaFlags & PVRSRV_HAP_CACHETYPE_MASK)
+    {
+        case PVRSRV_HAP_CACHED:
+
+            break;
+        case PVRSRV_HAP_WRITECOMBINE:
+	    ps_vma->vm_page_prot = PGPROT_WC(ps_vma->vm_page_prot);
+            break;
+        case PVRSRV_HAP_UNCACHED:
+            ps_vma->vm_page_prot = PGPROT_UC(ps_vma->vm_page_prot);
+            break;
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "%s: unknown cache type", __FUNCTION__));
+	    iRetVal = -EINVAL;
+	    goto unlock_and_return;
+    }
+
+
+    ps_vma->vm_ops = &MMapIOOps;
+
+    if(!DoMapToUser(psOffsetStruct->psLinuxMemArea, ps_vma, 0))
+    {
+        iRetVal = -EAGAIN;
+        goto unlock_and_return;
+    }
+
+    PVR_ASSERT(psOffsetStruct->ui32UserVAddr == 0)
+
+    psOffsetStruct->ui32UserVAddr = ps_vma->vm_start;
+
+
+    MMapVOpenNoLock(ps_vma);
+
+#if 0 /* FIXME: crash when call to print debug messages */
+    PVR_DPF((PVR_DBG_MESSAGE, "%s: Mapped area at offset 0x%08lx\n",
+             __FUNCTION__, ps_vma->vm_pgoff));
+#endif
+
+unlock_and_return:
+    if (iRetVal != 0 && psOffsetStruct != IMG_NULL)
+    {
+	DestroyOffsetStruct(psOffsetStruct);
+    }
+
+    mutex_unlock(&g_sMMapMutex);
+
+    return iRetVal;
+}
+
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void ProcSeqStartstopMMapRegistations(struct seq_file *sfile,IMG_BOOL start)
+{
+	if(start)
+	{
+	    mutex_lock(&g_sMMapMutex);
+	}
+	else
+	{
+	    mutex_unlock(&g_sMMapMutex);
+	}
+}
+
+
+static void* ProcSeqOff2ElementMMapRegistrations(struct seq_file *sfile, loff_t off)
+{
+    LinuxMemArea *psLinuxMemArea;
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+    list_for_each_entry(psLinuxMemArea, &g_sMMapAreaList, sMMapItem)
+    {
+        PKV_OFFSET_STRUCT psOffsetStruct;
+
+	 	list_for_each_entry(psOffsetStruct, &psLinuxMemArea->sMMapOffsetStructList, sAreaItem)
+        {
+	    	off--;
+	    	if (off == 0)
+	    	{
+				PVR_ASSERT(psOffsetStruct->psLinuxMemArea == psLinuxMemArea);
+				return (void*)psOffsetStruct;
+		    }
+        }
+    }
+	return (void*)0;
+}
+
+static void* ProcSeqNextMMapRegistrations(struct seq_file *sfile,void* el,loff_t off)
+{
+	return ProcSeqOff2ElementMMapRegistrations(sfile,off);
+}
+
+
+static void ProcSeqShowMMapRegistrations(struct seq_file *sfile,void* el)
+{
+	KV_OFFSET_STRUCT *psOffsetStruct = (KV_OFFSET_STRUCT*)el;
+    LinuxMemArea *psLinuxMemArea;
+	IMG_UINT32 ui32RealByteSize;
+	IMG_UINT32 ui32ByteOffset;
+
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+        seq_printf( sfile,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+						  "Allocations registered for mmap: %lu\n"
+                          "In total these areas correspond to %lu bytes\n"
+                          "psLinuxMemArea "
+						  "UserVAddr "
+						  "KernelVAddr "
+						  "CpuPAddr "
+                          "MMapOffset "
+                          "ByteLength "
+                          "LinuxMemType             "
+						  "Pid   Name     Flags\n",
+#else
+                          "<mmap_header>\n"
+                          "\t<count>%lu</count>\n"
+                          "\t<bytes>%lu</bytes>\n"
+                          "</mmap_header>\n",
+#endif
+						  g_ui32RegisteredAreas,
+                          g_ui32TotalByteSize
+                          );
+		return;
+	}
+
+	psLinuxMemArea = psOffsetStruct->psLinuxMemArea;
+
+	DetermineUsersSizeAndByteOffset(psLinuxMemArea,
+									&ui32RealByteSize,
+									&ui32ByteOffset);
+
+	seq_printf( sfile,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+						"%-8p       %08lx %-8p %08lx %08lx   %-8ld   %-24s %-5lu %-8s %08lx(%s)\n",
+#else
+                        "<mmap_record>\n"
+						"\t<pointer>%-8p</pointer>\n"
+                        "\t<user_virtual>%-8lx</user_virtual>\n"
+                        "\t<kernel_virtual>%-8p</kernel_virtual>\n"
+                        "\t<cpu_physical>%08lx</cpu_physical>\n"
+                        "\t<mmap_offset>%08lx</mmap_offset>\n"
+                        "\t<bytes>%-8ld</bytes>\n"
+                        "\t<linux_mem_area_type>%-24s</linux_mem_area_type>\n"
+                        "\t<pid>%-5lu</pid>\n"
+                        "\t<name>%-8s</name>\n"
+                        "\t<flags>%08lx</flags>\n"
+                        "\t<flags_string>%s</flags_string>\n"
+                        "</mmap_record>\n",
+#endif
+                        psLinuxMemArea,
+						psOffsetStruct->ui32UserVAddr + ui32ByteOffset,
+						LinuxMemAreaToCpuVAddr(psLinuxMemArea),
+                        LinuxMemAreaToCpuPAddr(psLinuxMemArea,0).uiAddr,
+						psOffsetStruct->ui32MMapOffset,
+						psLinuxMemArea->ui32ByteSize,
+                        LinuxMemAreaTypeToString(psLinuxMemArea->eAreaType),
+						psOffsetStruct->ui32PID,
+						psOffsetStruct->pszName,
+						psLinuxMemArea->ui32AreaFlags,
+                        HAPFlagsToString(psLinuxMemArea->ui32AreaFlags));
+}
+
+#else
+
+static off_t
+PrintMMapRegistrations(IMG_CHAR *buffer, size_t size, off_t off)
+{
+    LinuxMemArea *psLinuxMemArea;
+    off_t Ret;
+
+    mutex_lock(&g_sMMapMutex);
+
+    if(!off)
+    {
+		Ret = printAppend(buffer, size, 0,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+						  "Allocations registered for mmap: %lu\n"
+                          "In total these areas correspond to %lu bytes\n"
+                          "psLinuxMemArea "
+						  "UserVAddr "
+						  "KernelVAddr "
+						  "CpuPAddr "
+                          "MMapOffset "
+                          "ByteLength "
+                          "LinuxMemType             "
+						  "Pid   Name     Flags\n",
+#else
+                          "<mmap_header>\n"
+                          "\t<count>%lu</count>\n"
+                          "\t<bytes>%lu</bytes>\n"
+                          "</mmap_header>\n",
+#endif
+						  g_ui32RegisteredAreas,
+                          g_ui32TotalByteSize
+                          );
+
+        goto unlock_and_return;
+    }
+
+    if (size < 135)
+    {
+		Ret = 0;
+        goto unlock_and_return;
+    }
+
+    PVR_ASSERT(off != 0);
+    list_for_each_entry(psLinuxMemArea, &g_sMMapAreaList, sMMapItem)
+    {
+        PKV_OFFSET_STRUCT psOffsetStruct;
+
+	list_for_each_entry(psOffsetStruct, &psLinuxMemArea->sMMapOffsetStructList, sAreaItem)
+        {
+	    off--;
+	    if (off == 0)
+	    {
+		IMG_UINT32 ui32RealByteSize;
+		IMG_UINT32 ui32ByteOffset;
+
+		PVR_ASSERT(psOffsetStruct->psLinuxMemArea == psLinuxMemArea);
+
+		DetermineUsersSizeAndByteOffset(psLinuxMemArea,
+                                   &ui32RealByteSize,
+                                   &ui32ByteOffset);
+
+                Ret =  printAppend (buffer, size, 0,
+#if !defined(DEBUG_LINUX_XML_PROC_FILES)
+						"%-8p       %08lx %-8p %08lx %08lx   %-8ld   %-24s %-5lu %-8s %08lx(%s)\n",
+#else
+                        "<mmap_record>\n"
+						"\t<pointer>%-8p</pointer>\n"
+                        "\t<user_virtual>%-8lx</user_virtual>\n"
+                        "\t<kernel_virtual>%-8p</kernel_virtual>\n"
+                        "\t<cpu_physical>%08lx</cpu_physical>\n"
+                        "\t<mmap_offset>%08lx</mmap_offset>\n"
+                        "\t<bytes>%-8ld</bytes>\n"
+                        "\t<linux_mem_area_type>%-24s</linux_mem_area_type>\n"
+                        "\t<pid>%-5lu</pid>\n"
+                        "\t<name>%-8s</name>\n"
+                        "\t<flags>%08lx</flags>\n"
+                        "\t<flags_string>%s</flags_string>\n"
+                        "</mmap_record>\n",
+#endif
+                        psLinuxMemArea,
+			psOffsetStruct->ui32UserVAddr + ui32ByteOffset,
+						LinuxMemAreaToCpuVAddr(psLinuxMemArea),
+                        LinuxMemAreaToCpuPAddr(psLinuxMemArea,0).uiAddr,
+						psOffsetStruct->ui32MMapOffset,
+						psLinuxMemArea->ui32ByteSize,
+                        LinuxMemAreaTypeToString(psLinuxMemArea->eAreaType),
+						psOffsetStruct->ui32PID,
+						psOffsetStruct->pszName,
+						psLinuxMemArea->ui32AreaFlags,
+                        HAPFlagsToString(psLinuxMemArea->ui32AreaFlags));
+		goto unlock_and_return;
+	    }
+        }
+    }
+    Ret = END_OF_FILE;
+
+unlock_and_return:
+    mutex_unlock(&g_sMMapMutex);
+    return Ret;
+}
+#endif
+#endif
+
+
+PVRSRV_ERROR
+PVRMMapRegisterArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVRSRV_ERROR eError;
+#if defined(DEBUG) || defined(DEBUG_LINUX_MMAP_AREAS)
+    const IMG_CHAR *pszName = LinuxMemAreaTypeToString(LinuxMemAreaRootType(psLinuxMemArea));
+#endif
+
+    mutex_lock(&g_sMMapMutex);
+
+#if defined(DEBUG) || defined(DEBUG_LINUX_MMAP_AREAS)
+    PVR_DPF((PVR_DBG_MESSAGE,
+             "%s(%s, psLinuxMemArea 0x%p, ui32AllocFlags 0x%8lx)",
+             __FUNCTION__, pszName, psLinuxMemArea,  psLinuxMemArea->ui32AreaFlags));
+#endif
+
+    PVR_ASSERT(psLinuxMemArea->eAreaType != LINUX_MEM_AREA_SUB_ALLOC || LinuxMemAreaRoot(psLinuxMemArea)->eAreaType != LINUX_MEM_AREA_SUB_ALLOC);
+
+
+    if(psLinuxMemArea->bMMapRegistered)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "%s: psLinuxMemArea 0x%p is already registered",
+                __FUNCTION__, psLinuxMemArea));
+        eError = PVRSRV_ERROR_INVALID_PARAMS;
+	goto exit_unlock;
+    }
+
+    list_add_tail(&psLinuxMemArea->sMMapItem, &g_sMMapAreaList);
+
+    psLinuxMemArea->bMMapRegistered = IMG_TRUE;
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+    g_ui32RegisteredAreas++;
+
+    if (psLinuxMemArea->eAreaType != LINUX_MEM_AREA_SUB_ALLOC)
+    {
+        g_ui32TotalByteSize += psLinuxMemArea->ui32ByteSize;
+    }
+#endif
+
+    eError = PVRSRV_OK;
+
+exit_unlock:
+    mutex_unlock(&g_sMMapMutex);
+
+    return eError;
+}
+
+
+PVRSRV_ERROR
+PVRMMapRemoveRegisteredArea(LinuxMemArea *psLinuxMemArea)
+{
+    PVRSRV_ERROR eError;
+    PKV_OFFSET_STRUCT psOffsetStruct, psTmpOffsetStruct;
+
+    mutex_lock(&g_sMMapMutex);
+
+    PVR_ASSERT(psLinuxMemArea->bMMapRegistered);
+
+    list_for_each_entry_safe(psOffsetStruct, psTmpOffsetStruct, &psLinuxMemArea->sMMapOffsetStructList, sAreaItem)
+    {
+	if (psOffsetStruct->ui32Mapped != 0)
+	{
+	     PVR_DPF((PVR_DBG_ERROR, "%s: psOffsetStruct 0x%p for memory area 0x0x%p is still mapped; psOffsetStruct->ui32Mapped %lu",  __FUNCTION__, psOffsetStruct, psLinuxMemArea, psOffsetStruct->ui32Mapped));
+		eError = PVRSRV_ERROR_GENERIC;
+		goto exit_unlock;
+	}
+	else
+	{
+
+	     PVR_DPF((PVR_DBG_WARNING, "%s: psOffsetStruct 0x%p was never mapped",  __FUNCTION__, psOffsetStruct));
+	}
+
+	PVR_ASSERT((psOffsetStruct->ui32Mapped == 0) && psOffsetStruct->bOnMMapList);
+
+	DestroyOffsetStruct(psOffsetStruct);
+    }
+
+    list_del(&psLinuxMemArea->sMMapItem);
+
+    psLinuxMemArea->bMMapRegistered = IMG_FALSE;
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+    g_ui32RegisteredAreas--;
+    if (psLinuxMemArea->eAreaType != LINUX_MEM_AREA_SUB_ALLOC)
+    {
+        g_ui32TotalByteSize -= psLinuxMemArea->ui32ByteSize;
+    }
+#endif
+
+    eError = PVRSRV_OK;
+
+exit_unlock:
+    mutex_unlock(&g_sMMapMutex);
+    return eError;
+}
+
+
+PVRSRV_ERROR
+LinuxMMapPerProcessConnect(PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc)
+{
+    PVR_UNREFERENCED_PARAMETER(psEnvPerProc);
+
+    return PVRSRV_OK;
+}
+
+IMG_VOID
+LinuxMMapPerProcessDisconnect(PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc)
+{
+    PKV_OFFSET_STRUCT psOffsetStruct, psTmpOffsetStruct;
+    IMG_BOOL bWarn = IMG_FALSE;
+    IMG_UINT32 ui32PID = OSGetCurrentProcessIDKM();
+
+    PVR_UNREFERENCED_PARAMETER(psEnvPerProc);
+
+    mutex_lock(&g_sMMapMutex);
+
+    list_for_each_entry_safe(psOffsetStruct, psTmpOffsetStruct, &g_sMMapOffsetStructList, sMMapItem)
+    {
+	if (psOffsetStruct->ui32PID == ui32PID)
+	{
+	    if (!bWarn)
+	    {
+		PVR_DPF((PVR_DBG_WARNING, "%s: process has unmapped offset structures. Removing them", __FUNCTION__));
+		bWarn = IMG_TRUE;
+	    }
+	    PVR_ASSERT(psOffsetStruct->ui32Mapped == 0);
+	    PVR_ASSERT(psOffsetStruct->bOnMMapList);
+
+	    DestroyOffsetStruct(psOffsetStruct);
+	}
+    }
+
+    mutex_unlock(&g_sMMapMutex);
+}
+
+
+PVRSRV_ERROR LinuxMMapPerProcessHandleOptions(PVRSRV_HANDLE_BASE *psHandleBase)
+{
+    PVRSRV_ERROR eError;
+
+    eError = PVRSRVSetMaxHandle(psHandleBase, MAX_MMAP_HANDLE);
+    if (eError != PVRSRV_OK)
+    {
+	PVR_DPF((PVR_DBG_ERROR,"%s: failed to set handle limit (%d)", __FUNCTION__, eError));
+	return eError;
+    }
+
+    return eError;
+}
+
+
+IMG_VOID
+PVRMMapInit(IMG_VOID)
+{
+    mutex_init(&g_sMMapMutex);
+
+    g_psMemmapCache = KMemCacheCreateWrapper("img-mmap", sizeof(KV_OFFSET_STRUCT), 0, 0);
+    if (!g_psMemmapCache)
+    {
+        PVR_DPF((PVR_DBG_ERROR,"%s: failed to allocate kmem_cache", __FUNCTION__));
+	goto error;
+    }
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+#ifdef PVR_PROC_USE_SEQ_FILE
+	g_ProcMMap = CreateProcReadEntrySeq("mmap", NULL,
+						  ProcSeqNextMMapRegistrations,
+						  ProcSeqShowMMapRegistrations,
+						  ProcSeqOff2ElementMMapRegistrations,
+						  ProcSeqStartstopMMapRegistations
+						 );
+#else
+    CreateProcReadEntry("mmap", PrintMMapRegistrations);
+#endif
+#endif
+    return;
+
+error:
+    PVRMMapCleanup();
+    return;
+}
+
+
+IMG_VOID
+PVRMMapCleanup(IMG_VOID)
+{
+    PVRSRV_ERROR eError;
+
+    if (!list_empty(&g_sMMapAreaList))
+    {
+	LinuxMemArea *psLinuxMemArea, *psTmpMemArea;
+
+	PVR_DPF((PVR_DBG_ERROR, "%s: Memory areas are still registered with MMap", __FUNCTION__));
+
+	PVR_TRACE(("%s: Unregistering memory areas", __FUNCTION__));
+	list_for_each_entry_safe(psLinuxMemArea, psTmpMemArea, &g_sMMapAreaList, sMMapItem)
+	{
+		eError = PVRMMapRemoveRegisteredArea(psLinuxMemArea);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "%s: PVRMMapRemoveRegisteredArea failed (%d)", __FUNCTION__, eError));
+		}
+		PVR_ASSERT(eError == PVRSRV_OK);
+
+		LinuxMemAreaDeepFree(psLinuxMemArea);
+	}
+    }
+    PVR_ASSERT(list_empty((&g_sMMapAreaList)));
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+#ifdef PVR_PROC_USE_SEQ_FILE
+    RemoveProcEntrySeq(g_ProcMMap);
+#else
+    RemoveProcEntry("mmap");
+#endif
+#endif
+
+    if(g_psMemmapCache)
+    {
+        KMemCacheDestroyWrapper(g_psMemmapCache);
+        g_psMemmapCache = NULL;
+    }
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.h
new file mode 100644
index 0000000..afc7c94
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mmap.h
@@ -0,0 +1,103 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__MMAP_H__)
+#define __MMAP_H__
+
+#include <linux/mm.h>
+#include <linux/list.h>
+
+#include "perproc.h"
+#include "mm.h"
+
+typedef struct KV_OFFSET_STRUCT_TAG
+{
+
+    IMG_UINT32			ui32Mapped;
+
+
+    IMG_UINT32                  ui32MMapOffset;
+
+    IMG_UINT32			ui32RealByteSize;
+
+
+    LinuxMemArea                *psLinuxMemArea;
+
+
+    IMG_UINT32			ui32TID;
+
+
+    IMG_UINT32			ui32PID;
+
+
+    IMG_BOOL			bOnMMapList;
+
+
+    IMG_UINT32			ui32RefCount;
+
+
+    IMG_UINT32			ui32UserVAddr;
+
+
+#if defined(DEBUG_LINUX_MMAP_AREAS)
+    const IMG_CHAR		*pszName;
+#endif
+
+
+   struct list_head		sMMapItem;
+
+
+   struct list_head		sAreaItem;
+}KV_OFFSET_STRUCT, *PKV_OFFSET_STRUCT;
+
+
+
+IMG_VOID PVRMMapInit(IMG_VOID);
+
+
+IMG_VOID PVRMMapCleanup(IMG_VOID);
+
+
+PVRSRV_ERROR PVRMMapRegisterArea(LinuxMemArea *psLinuxMemArea);
+
+
+PVRSRV_ERROR PVRMMapRemoveRegisteredArea(LinuxMemArea *psLinuxMemArea);
+
+
+PVRSRV_ERROR PVRMMapOSMemHandleToMMapData(PVRSRV_PER_PROCESS_DATA *psPerProc,
+					     IMG_HANDLE hMHandle,
+                                             IMG_UINT32 *pui32MMapOffset,
+                                             IMG_UINT32 *pui32ByteOffset,
+                                             IMG_UINT32 *pui32RealByteSize,						     IMG_UINT32 *pui32UserVAddr);
+
+PVRSRV_ERROR
+PVRMMapReleaseMMapData(PVRSRV_PER_PROCESS_DATA *psPerProc,
+				IMG_HANDLE hMHandle,
+				IMG_BOOL *pbMUnmap,
+				IMG_UINT32 *pui32RealByteSize,
+                                IMG_UINT32 *pui32UserVAddr);
+
+int PVRMMap(struct file* pFile, struct vm_area_struct* ps_vma);
+
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/module.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/module.c
new file mode 100644
index 0000000..0fd336b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/module.c
@@ -0,0 +1,756 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(SUPPORT_DRI_DRM)
+
+	#if defined(LDM_PLATFORM)
+		#define	PVR_LDM_PLATFORM_MODULE
+		#define	PVR_LDM_MODULE
+	#else
+		#if defined(LDM_PCI)
+			#define PVR_LDM_PCI_MODULE
+			#define	PVR_LDM_MODULE
+		#endif
+	#endif
+#endif
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/fs.h>
+#include <linux/proc_fs.h>
+
+#if defined(SUPPORT_DRI_DRM)
+#include <drm/drmP.h>
+#endif
+
+#if defined(PVR_LDM_PLATFORM_MODULE)
+#include <linux/platform_device.h>
+#endif
+
+#if defined(PVR_LDM_PCI_MODULE)
+#include <linux/pci.h>
+#endif
+
+#if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
+#include <asm/uaccess.h>
+#endif
+
+#include "img_defs.h"
+#include "services.h"
+#include "kerneldisplay.h"
+#include "kernelbuffer.h"
+#include "sysconfig.h"
+#include "sysplb.h"
+#include "systnc.h"
+#include "pvrmmap.h"
+#include "mutils.h"
+#include "mm.h"
+#include "mmap.h"
+#include "mutex.h"
+#include "pvr_debug.h"
+#include "srvkm.h"
+#include "perproc.h"
+#include "handle.h"
+#include "pvr_bridge_km.h"
+#include "proc.h"
+#include "pvrmodule.h"
+#include "private_data.h"
+#include "lock.h"
+#include "linkage.h"
+
+#if defined(SUPPORT_DRI_DRM)
+#include "pvr_drm.h"
+#endif
+#define DRVNAME		"pvrsrvkm"
+#define DEVNAME		"pvrsrvkm"
+
+#if defined(SUPPORT_DRI_DRM)
+#define PRIVATE_DATA(pFile) ((pFile)->driver_priv)
+#else
+#define PRIVATE_DATA(pFile) ((pFile)->private_data)
+#endif
+
+MODULE_SUPPORTED_DEVICE(DEVNAME);
+#ifdef DEBUG
+static IMG_INT debug = DBGPRIV_WARNING;
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
+#include <linux/moduleparam.h>
+module_param(debug, int, 0);
+#else
+MODULE_PARM(debug, "i");
+MODULE_PARM_DESC(debug, "Sets the level of debug output (default=0x4)");
+#endif
+#endif
+
+
+extern IMG_BOOL PVRGetDisplayClassJTable(PVRSRV_DC_DISP2SRV_KMJTABLE *psJTable);
+extern IMG_BOOL PVRGetBufferClassJTable(PVRSRV_BC_BUFFER2SRV_KMJTABLE *psJTable);
+
+EXPORT_SYMBOL(PVRGetDisplayClassJTable);
+EXPORT_SYMBOL(PVRGetBufferClassJTable);
+
+
+#if defined(PVR_LDM_MODULE)
+static struct class *psPvrClass;
+#endif
+
+#if !defined(SUPPORT_DRI_DRM)
+static IMG_INT AssignedMajorNumber;
+
+static IMG_INT PVRSRVOpen(struct inode* pInode, struct file* pFile);
+static IMG_INT PVRSRVRelease(struct inode* pInode, struct file* pFile);
+
+static struct file_operations pvrsrv_fops = {
+	.owner=THIS_MODULE,
+	.unlocked_ioctl=PVRSRV_BridgeDispatchKM,
+	.open=PVRSRVOpen,
+	.release=PVRSRVRelease,
+	.mmap=PVRMMap,
+};
+#endif
+
+struct mutex gPVRSRVLock;
+
+IMG_UINT32 gui32ReleasePID;
+
+#if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
+static IMG_UINT32 gPVRPowerLevel;
+#endif
+
+#if defined(PVR_LDM_MODULE)
+
+#if defined(PVR_LDM_PLATFORM_MODULE)
+#define	LDM_DEV	struct platform_device
+#define	LDM_DRV	struct platform_driver
+#endif
+
+#if defined(PVR_LDM_PCI_MODULE)
+#define	LDM_DEV	struct pci_dev
+#define	LDM_DRV	struct pci_driver
+#endif
+
+#if defined(PVR_LDM_PLATFORM_MODULE)
+static IMG_INT PVRSRVDriverRemove(LDM_DEV *device);
+static IMG_INT PVRSRVDriverProbe(LDM_DEV *device);
+#endif
+#if defined(PVR_LDM_PCI_MODULE)
+static IMG_VOID PVRSRVDriverRemove(LDM_DEV *device);
+static IMG_INT PVRSRVDriverProbe(LDM_DEV *device, const struct pci_device_id *id);
+#endif
+static IMG_INT PVRSRVDriverSuspend(LDM_DEV *device, pm_message_t state);
+static IMG_VOID PVRSRVDriverShutdown(LDM_DEV *device);
+static IMG_INT PVRSRVDriverResume(LDM_DEV *device);
+
+#if defined(PVR_LDM_PCI_MODULE)
+/* all supported platform ids */
+struct pci_device_id powervr_id_table[] __devinitdata = {
+	{ PCI_DEVICE(SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV_DEVICE_ID_PLB) },
+	{ PCI_DEVICE(SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV_DEVICE_ID_TNC) },
+	{ 0 }
+};
+
+MODULE_DEVICE_TABLE(pci, powervr_id_table);
+#endif
+
+static LDM_DRV powervr_driver = {
+#if defined(PVR_LDM_PLATFORM_MODULE)
+	.driver = {
+		.name		= DRVNAME,
+	},
+#endif
+#if defined(PVR_LDM_PCI_MODULE)
+	.name		= DRVNAME,
+	.id_table = powervr_id_table,
+#endif
+	.probe		= PVRSRVDriverProbe,
+#if defined(PVR_LDM_PLATFORM_MODULE)
+	.remove		= PVRSRVDriverRemove,
+#endif
+#if defined(PVR_LDM_PCI_MODULE)
+	.remove		= __devexit_p(PVRSRVDriverRemove),
+#endif
+	.suspend	= PVRSRVDriverSuspend,
+	.resume		= PVRSRVDriverResume,
+	.shutdown	= PVRSRVDriverShutdown,
+};
+
+LDM_DEV *gpsPVRLDMDev;
+
+#if defined(MODULE) && defined(PVR_LDM_PLATFORM_MODULE)
+
+static IMG_VOID PVRSRVDeviceRelease(struct device *pDevice)
+{
+	PVR_UNREFERENCED_PARAMETER(pDevice);
+}
+
+static struct platform_device powervr_device = {
+	.name			= DEVNAME,
+	.id				= -1,
+	.dev 			= {
+		.release	= PVRSRVDeviceRelease
+	}
+};
+
+#endif
+
+#if defined(PVR_LDM_PLATFORM_MODULE)
+static IMG_INT PVRSRVDriverProbe(LDM_DEV *pDevice)
+#endif
+#if defined(PVR_LDM_PCI_MODULE)
+static IMG_INT __devinit PVRSRVDriverProbe(LDM_DEV *pDevice, const struct pci_device_id *id)
+#endif
+{
+	SYS_DATA *psSysData;
+
+	PVR_TRACE(("PVRSRVDriverProbe(pDevice=%p)", pDevice));
+
+#if 0
+
+	if (PerDeviceSysInitialise((IMG_PVOID)pDevice) != PVRSRV_OK)
+	{
+		return -EINVAL;
+	}
+#endif
+
+	if (SysAcquireData(&psSysData) != PVRSRV_OK)
+	{
+		gpsPVRLDMDev = pDevice;
+
+		if (SysInitialise() != PVRSRV_OK)
+		{
+			return -ENODEV;
+		}
+	}
+
+	return 0;
+}
+
+
+#if defined (PVR_LDM_PLATFORM_MODULE)
+static IMG_INT PVRSRVDriverRemove(LDM_DEV *pDevice)
+#endif
+#if defined(PVR_LDM_PCI_MODULE)
+static IMG_VOID __devexit PVRSRVDriverRemove(LDM_DEV *pDevice)
+#endif
+{
+	SYS_DATA *psSysData;
+
+	PVR_TRACE(("PVRSRVDriverRemove(pDevice=%p)", pDevice));
+
+	if (SysAcquireData(&psSysData) == PVRSRV_OK)
+	{
+#if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
+		if (gPVRPowerLevel != 0)
+		{
+			if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) == PVRSRV_OK)
+			{
+				gPVRPowerLevel = 0;
+			}
+		}
+#endif
+		SysDeinitialise(psSysData);
+
+		gpsPVRLDMDev = IMG_NULL;
+	}
+
+#if 0
+	if (PerDeviceSysDeInitialise((IMG_PVOID)pDevice) != PVRSRV_OK)
+	{
+		return -EINVAL;
+	}
+#endif
+
+#if defined (PVR_LDM_PLATFORM_MODULE)
+	return 0;
+#endif
+#if defined (PVR_LDM_PCI_MODULE)
+	return;
+#endif
+}
+
+
+static IMG_VOID PVRSRVDriverShutdown(LDM_DEV *pDevice)
+{
+	PVR_TRACE(("PVRSRVDriverShutdown(pDevice=%p)", pDevice));
+
+	(IMG_VOID) PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D3);
+}
+
+#endif
+
+
+#if defined(PVR_LDM_MODULE) || defined(SUPPORT_DRI_DRM)
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT PVRSRVDriverSuspend(struct drm_device *pDevice, pm_message_t state)
+#else
+static IMG_INT PVRSRVDriverSuspend(LDM_DEV *pDevice, pm_message_t state)
+#endif
+{
+#if !(defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL) && !defined(SUPPORT_DRI_DRM))
+	PVR_TRACE(( "PVRSRVDriverSuspend(pDevice=%p)", pDevice));
+
+	if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D3) != PVRSRV_OK)
+	{
+		return -EINVAL;
+	}
+#endif
+	return 0;
+}
+
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT PVRSRVDriverResume(struct drm_device *pDevice)
+#else
+static IMG_INT PVRSRVDriverResume(LDM_DEV *pDevice)
+#endif
+{
+#if !(defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL) && !defined(SUPPORT_DRI_DRM))
+	PVR_TRACE(("PVRSRVDriverResume(pDevice=%p)", pDevice));
+
+	if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) != PVRSRV_OK)
+	{
+		return -EINVAL;
+	}
+#endif
+	return 0;
+}
+#endif
+
+
+#if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL) && !defined(SUPPORT_DRI_DRM)
+IMG_INT PVRProcSetPowerLevel(struct file *file, const IMG_CHAR *buffer, IMG_UINT32 count, IMG_VOID *data)
+{
+	IMG_CHAR data_buffer[2];
+	IMG_UINT32 PVRPowerLevel;
+
+	if (count != sizeof(data_buffer))
+	{
+		return -EINVAL;
+	}
+	else
+	{
+		if (copy_from_user(data_buffer, buffer, count))
+			return -EINVAL;
+		if (data_buffer[count - 1] != '\n')
+			return -EINVAL;
+		PVRPowerLevel = data_buffer[0] - '0';
+		if (PVRPowerLevel != gPVRPowerLevel)
+		{
+			if (PVRPowerLevel != 0)
+			{
+				if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D3) != PVRSRV_OK)
+				{
+					return -EINVAL;
+				}
+			}
+			else
+			{
+				if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) != PVRSRV_OK)
+				{
+					return -EINVAL;
+				}
+			}
+
+			gPVRPowerLevel = PVRPowerLevel;
+		}
+	}
+	return (count);
+}
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+void ProcSeqShowPowerLevel(struct seq_file *sfile,void* el)
+{
+	seq_printf(sfile, "%lu\n", gPVRPowerLevel);
+}
+
+#else
+IMG_INT PVRProcGetPowerLevel(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data)
+{
+	if (off == 0) {
+		*start = (IMG_CHAR *)1;
+		return printAppend(page, count, 0, "%lu\n", gPVRPowerLevel);
+	}
+	*eof = 1;
+	return 0;
+}
+#endif
+
+#endif
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT PVRSRVOpen(struct drm_device unref__ *dev, struct drm_file *pFile)
+#else
+static IMG_INT PVRSRVOpen(struct inode unref__ * pInode, struct file *pFile)
+#endif
+{
+	PVRSRV_FILE_PRIVATE_DATA *psPrivateData;
+	IMG_HANDLE hBlockAlloc;
+	IMG_INT iRet = -ENOMEM;
+	PVRSRV_ERROR eError;
+	IMG_UINT32 ui32PID;
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+	PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc;
+#endif
+
+#if defined(SUPPORT_DRI_DRM)
+	PVR_UNREFERENCED_PARAMETER(dev);
+#else
+	PVR_UNREFERENCED_PARAMETER(pInode);
+#endif
+
+	mutex_lock(&gPVRSRVLock);
+
+	ui32PID = OSGetCurrentProcessIDKM();
+
+	if (PVRSRVProcessConnect(ui32PID) != PVRSRV_OK)
+		goto err_unlock;
+
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+	psEnvPerProc = PVRSRVPerProcessPrivateData(ui32PID);
+	if (psEnvPerProc == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: No per-process private data", __FUNCTION__));
+		goto err_unlock;
+	}
+#endif
+
+	eError = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+						sizeof(PVRSRV_FILE_PRIVATE_DATA),
+						(IMG_PVOID *)&psPrivateData,
+						&hBlockAlloc,
+						"File Private Data");
+
+	if(eError != PVRSRV_OK)
+		goto err_unlock;
+
+#if defined(PVR_SECURE_FD_EXPORT)
+	psPrivateData->hKernelMemInfo = NULL;
+#endif
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+	psPrivateData->psDRMFile = pFile;
+
+	list_add_tail(&psPrivateData->sDRMAuthListItem, &psEnvPerProc->sDRMAuthListHead);
+#endif
+	psPrivateData->ui32OpenPID = ui32PID;
+	psPrivateData->hBlockAlloc = hBlockAlloc;
+	PRIVATE_DATA(pFile) = psPrivateData;
+	iRet = 0;
+err_unlock:
+	mutex_unlock(&gPVRSRVLock);
+	return iRet;
+}
+
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT PVRSRVRelease(struct drm_device unref__ *dev, struct drm_file *pFile)
+#else
+static IMG_INT PVRSRVRelease(struct inode unref__ * pInode, struct file *pFile)
+#endif
+{
+	PVRSRV_FILE_PRIVATE_DATA *psPrivateData;
+
+#if defined(SUPPORT_DRI_DRM)
+	PVR_UNREFERENCED_PARAMETER(dev);
+#else
+	PVR_UNREFERENCED_PARAMETER(pInode);
+#endif
+
+	mutex_lock(&gPVRSRVLock);
+
+	psPrivateData = PRIVATE_DATA(pFile);
+
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+	list_del(&psPrivateData->sDRMAuthListItem);
+#endif
+
+
+	gui32ReleasePID = psPrivateData->ui32OpenPID;
+	PVRSRVProcessDisconnect(psPrivateData->ui32OpenPID);
+	gui32ReleasePID = 0;
+
+	OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+			  sizeof(PVRSRV_FILE_PRIVATE_DATA),
+			  psPrivateData, psPrivateData->hBlockAlloc);
+
+	PRIVATE_DATA(pFile) = NULL;
+
+	mutex_unlock(&gPVRSRVLock);
+	return 0;
+}
+
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT PVRCore_Init(IMG_VOID)
+#else
+static IMG_INT __init PVRCore_Init(IMG_VOID)
+#endif
+{
+	IMG_INT error;
+#if !defined(PVR_LDM_MODULE)
+	PVRSRV_ERROR eError;
+#else
+	struct device *psDev;
+#endif
+
+	PVRDPFInit();
+	PVR_TRACE(("PVRCore_Init"));
+
+	mutex_init(&gPVRSRVLock);
+
+#ifdef DEBUG
+	PVRDebugSetLevel(debug);
+#endif
+
+	if (CreateProcEntries ())
+	{
+		error = -ENOMEM;
+		return error;
+	}
+
+	if (PVROSFuncInit() != PVRSRV_OK)
+	{
+		error = -ENOMEM;
+		goto init_failed;
+	}
+
+	PVRLinuxMUtilsInit();
+
+	if(LinuxMMInit() != PVRSRV_OK)
+	{
+		error = -ENOMEM;
+		goto init_failed;
+	}
+
+	LinuxBridgeInit();
+
+	PVRMMapInit();
+
+#if defined(PVR_LDM_MODULE)
+
+#if defined(PVR_LDM_PLATFORM_MODULE)
+	if ((error = platform_driver_register(&powervr_driver)) != 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register platform driver (%d)", error));
+
+		goto init_failed;
+	}
+
+#if defined(MODULE)
+	if ((error = platform_device_register(&powervr_device)) != 0)
+	{
+		platform_driver_unregister(&powervr_driver);
+
+		PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register platform device (%d)", error));
+
+		goto init_failed;
+	}
+#endif
+#endif
+
+#if defined(PVR_LDM_PCI_MODULE)
+	if ((error = pci_register_driver(&powervr_driver)) != 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to register PCI driver (%d)", error));
+
+		goto init_failed;
+	}
+#endif
+
+#else
+
+	if ((eError = SysInitialise()) != PVRSRV_OK)
+	{
+		error = -ENODEV;
+#if defined(TCF_REV) && (TCF_REV == 110)
+		if(eError == PVRSRV_ERROR_NOT_SUPPORTED)
+		{
+			printk("\nAtlas wrapper (FPGA image) version mismatch");
+			error = -ENODEV;
+		}
+#endif
+		goto init_failed;
+	}
+#endif
+
+#if !defined(SUPPORT_DRI_DRM)
+	AssignedMajorNumber = register_chrdev(0, DEVNAME, &pvrsrv_fops);
+
+	if (AssignedMajorNumber <= 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to get major number"));
+
+		error = -EBUSY;
+		goto sys_deinit;
+	}
+
+	PVR_TRACE(("PVRCore_Init: major device %d", AssignedMajorNumber));
+#endif
+
+#if defined(PVR_LDM_MODULE)
+
+	psPvrClass = class_create(THIS_MODULE, "pvr");
+
+	if (IS_ERR(psPvrClass))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to create class (%ld)", PTR_ERR(psPvrClass)));
+		error = -EBUSY;
+		goto unregister_device;
+	}
+
+	psDev = device_create(psPvrClass, NULL, MKDEV(AssignedMajorNumber, 0),
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26))
+				  NULL,
+#endif
+				  DEVNAME);
+	if (IS_ERR(psDev))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "PVRCore_Init: unable to create device (%ld)", PTR_ERR(psDev)));
+		error = -EBUSY;
+		goto destroy_class;
+	}
+#endif
+
+	return 0;
+
+#if defined(PVR_LDM_MODULE)
+destroy_class:
+	class_destroy(psPvrClass);
+unregister_device:
+	unregister_chrdev((IMG_UINT)AssignedMajorNumber, DRVNAME);
+#endif
+#if !defined(SUPPORT_DRI_DRM)
+sys_deinit:
+#endif
+#if defined(PVR_LDM_MODULE)
+#if defined(PVR_LDM_PCI_MODULE)
+	pci_unregister_driver(&powervr_driver);
+#endif
+
+#if defined (PVR_LDM_PLATFORM_MODULE)
+#if defined (MODULE)
+	platform_device_unregister(&powervr_device);
+#endif
+	platform_driver_unregister(&powervr_driver);
+#endif
+
+#else
+
+	{
+		SYS_DATA *psSysData;
+
+		SysAcquireData(&psSysData);
+		if (psSysData != IMG_NULL)
+		{
+			SysDeinitialise(psSysData);
+		}
+	}
+#endif
+init_failed:
+	PVRMMapCleanup();
+	LinuxMMCleanup();
+	LinuxBridgeDeInit();
+	PVROSFuncDeInit();
+	RemoveProcEntries();
+
+	return error;
+
+}
+
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_VOID PVRCore_Cleanup(IMG_VOID)
+#else
+static IMG_VOID __exit PVRCore_Cleanup(IMG_VOID)
+#endif
+{
+	SYS_DATA *psSysData;
+
+	PVR_TRACE(("PVRCore_Cleanup"));
+
+	SysAcquireData(&psSysData);
+
+#if defined(PVR_LDM_MODULE)
+	device_destroy(psPvrClass, MKDEV(AssignedMajorNumber, 0));
+	class_destroy(psPvrClass);
+#endif
+
+#if !defined(SUPPORT_DRI_DRM)
+#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22))
+	if (
+#endif
+		unregister_chrdev((IMG_UINT)AssignedMajorNumber, DRVNAME)
+#if !(LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22))
+								;
+#else
+								)
+	{
+		PVR_DPF((PVR_DBG_ERROR," can't unregister device major %d", AssignedMajorNumber));
+	}
+#endif
+#endif
+
+#if defined(PVR_LDM_MODULE)
+
+#if defined(PVR_LDM_PCI_MODULE)
+	pci_unregister_driver(&powervr_driver);
+#endif
+
+#if defined (PVR_LDM_PLATFORM_MODULE)
+#if defined (MODULE)
+	platform_device_unregister(&powervr_device);
+#endif
+	platform_driver_unregister(&powervr_driver);
+#endif
+
+#else
+#if defined(DEBUG) && defined(PVR_MANUAL_POWER_CONTROL)
+	if (gPVRPowerLevel != 0)
+	{
+		if (PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE_D0) == PVRSRV_OK)
+		{
+			gPVRPowerLevel = 0;
+		}
+	}
+#endif
+
+	SysDeinitialise(psSysData);
+#endif
+
+	PVRMMapCleanup();
+
+	LinuxMMCleanup();
+
+	LinuxBridgeDeInit();
+
+	PVROSFuncDeInit();
+
+	RemoveProcEntries();
+
+	PVR_TRACE(("PVRCore_Cleanup: unloading"));
+}
+
+#if !defined(SUPPORT_DRI_DRM)
+module_init(PVRCore_Init);
+module_exit(PVRCore_Cleanup);
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.c
new file mode 100644
index 0000000..652955a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.c
@@ -0,0 +1,28 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+/*
+ * PVR's mutex wrapping code has been removed and we're just calling
+ * mutex_lock, mutex_unlock, etc. directly now.  Most of the wrappers
+ * here were unused, and the couple that were used simply confused
+ * the compiler about potential recursive locking.
+ */
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.h
new file mode 100644
index 0000000..894c1c9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutex.h
@@ -0,0 +1,37 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __INCLUDED_LINUX_MUTEX_H_
+#define __INCLUDED_LINUX_MUTEX_H_
+
+#include <linux/version.h>
+
+#include <linux/mutex.h>
+
+/*
+ * PVR's mutex wrapping code has been removed and we're just calling
+ * mutex_lock, mutex_unlock, etc. directly now.  Most of the wrappers
+ * here were unused, and the couple that were used simply confused
+ * the compiler about potential recursive locking.
+ */
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.c
new file mode 100644
index 0000000..ce010bf
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.c
@@ -0,0 +1,126 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/version.h>
+
+#include <linux/spinlock.h>
+#include <linux/mm.h>
+#include <asm/page.h>
+#include <asm/pgtable.h>
+
+#include "img_defs.h"
+#include "pvr_debug.h"
+#include "mutils.h"
+
+#if defined(SUPPORT_LINUX_X86_PAT)
+#define	PAT_LINUX_X86_WC	1
+
+#define	PAT_X86_ENTRY_BITS	8
+
+#define	PAT_X86_BIT_PWT		1U
+#define	PAT_X86_BIT_PCD		2U
+#define	PAT_X86_BIT_PAT		4U
+#define	PAT_X86_BIT_MASK	(PAT_X86_BIT_PAT | PAT_X86_BIT_PCD | PAT_X86_BIT_PWT)
+
+static IMG_BOOL g_write_combining_available = IMG_FALSE;
+
+#define	PROT_TO_PAT_INDEX(v, B) ((v & _PAGE_ ## B) ? PAT_X86_BIT_ ## B : 0)
+
+static inline IMG_UINT
+pvr_pat_index(pgprotval_t prot_val)
+{
+	IMG_UINT ret = 0;
+	pgprotval_t val = prot_val & _PAGE_CACHE_MASK;
+
+	ret |= PROT_TO_PAT_INDEX(val, PAT);
+	ret |= PROT_TO_PAT_INDEX(val, PCD);
+	ret |= PROT_TO_PAT_INDEX(val, PWT);
+
+	return ret;
+}
+
+static inline IMG_UINT
+pvr_pat_entry(u64 pat, IMG_UINT index)
+{
+	return (IMG_UINT)(pat >> (index * PAT_X86_ENTRY_BITS)) & PAT_X86_BIT_MASK;
+}
+
+static IMG_VOID
+PVRLinuxX86PATProbe(IMG_VOID)
+{
+
+	if (cpu_has_pat)
+	{
+		u64 pat;
+		IMG_UINT pat_index;
+		IMG_UINT pat_entry;
+
+		PVR_TRACE(("%s: PAT available", __FUNCTION__));
+
+		rdmsrl(MSR_IA32_CR_PAT, pat);
+		PVR_TRACE(("%s: Top 32 bits of PAT: 0x%.8x", __FUNCTION__, (IMG_UINT)(pat >> 32)));
+		PVR_TRACE(("%s: Bottom 32 bits of PAT: 0x%.8x", __FUNCTION__, (IMG_UINT)(pat)));
+
+		pat_index = pvr_pat_index(_PAGE_CACHE_WC);
+		PVR_TRACE(("%s: PAT index for write combining: %u", __FUNCTION__, pat_index));
+
+		pat_entry = pvr_pat_entry(pat, pat_index);
+		PVR_TRACE(("%s: PAT entry for write combining: 0x%.2x (should be 0x%.2x)", __FUNCTION__, pat_entry, PAT_LINUX_X86_WC));
+
+#if defined(SUPPORT_LINUX_X86_WRITECOMBINE)
+		g_write_combining_available = (IMG_BOOL)(pat_entry == PAT_LINUX_X86_WC);
+#endif
+	}
+#if defined(DEBUG)
+#if defined(SUPPORT_LINUX_X86_WRITECOMBINE)
+	if (g_write_combining_available)
+	{
+		PVR_TRACE(("%s: Write combining available via PAT", __FUNCTION__));
+	}
+	else
+	{
+		PVR_TRACE(("%s: Write combining not available", __FUNCTION__));
+	}
+#else
+	PVR_TRACE(("%s: Write combining disabled in driver build", __FUNCTION__));
+#endif
+#endif
+}
+
+pgprot_t
+pvr_pgprot_writecombine(pgprot_t prot)
+{
+
+
+    return (g_write_combining_available) ?
+		__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_MASK) | _PAGE_CACHE_WC) : pgprot_noncached(prot);
+}
+#endif
+
+IMG_VOID
+PVRLinuxMUtilsInit(IMG_VOID)
+{
+#if defined(SUPPORT_LINUX_X86_PAT)
+	PVRLinuxX86PATProbe();
+#endif
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.h
new file mode 100644
index 0000000..625041b
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/mutils.h
@@ -0,0 +1,93 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __IMG_LINUX_MUTILS_H__
+#define __IMG_LINUX_MUTILS_H__
+
+#include <linux/version.h>
+
+#if !(defined(__i386__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)))
+#if defined(SUPPORT_LINUX_X86_PAT)
+#undef SUPPORT_LINUX_X86_PAT
+#endif
+#endif
+
+#if defined(SUPPORT_LINUX_X86_PAT)
+	pgprot_t pvr_pgprot_writecombine(pgprot_t prot);
+	#define	PGPROT_WC(pv)	pvr_pgprot_writecombine(pv)
+#else
+	#if defined(__arm__) || defined(__sh__)
+		#define	PGPROT_WC(pv)	pgprot_writecombine(pv)
+	#else
+		#if defined(__i386__)
+			#define	PGPROT_WC(pv)	pgprot_noncached(pv)
+		#else
+			#define PGPROT_WC(pv)	pgprot_noncached(pv)
+			#error  Unsupported architecture!
+		#endif
+	#endif
+#endif
+
+#define	PGPROT_UC(pv)	pgprot_noncached(pv)
+
+#if defined(__i386__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
+	#define	IOREMAP(pa, bytes)	ioremap_cache(pa, bytes)
+#else
+	#if defined(__arm__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
+		#define	IOREMAP(pa, bytes)	ioremap_cached(pa, bytes)
+	#else
+		#define IOREMAP(pa, bytes)	ioremap(pa, bytes)
+	#endif
+#endif
+
+#if defined(SUPPORT_LINUX_X86_PAT)
+	#if defined(SUPPORT_LINUX_X86_WRITECOMBINE)
+		#define IOREMAP_WC(pa, bytes) ioremap_wc(pa, bytes)
+	#else
+		#define IOREMAP_WC(pa, bytes) ioremap_nocache(pa, bytes)
+	#endif
+#else
+	#if defined(__arm__)
+		#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
+			#define IOREMAP_WC(pa, bytes) ioremap_wc(pa, bytes)
+		#else
+			#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
+				#define	IOREMAP_WC(pa, bytes)	ioremap_nocache(pa, bytes)
+			#else
+				#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17))
+					#define	IOREMAP_WC(pa, bytes)	__ioremap(pa, bytes, L_PTE_BUFFERABLE)
+				#else
+					#define IOREMAP_WC(pa, bytes)	__ioremap(pa, bytes, , L_PTE_BUFFERABLE, 1)
+				#endif
+			#endif
+		#endif
+	#else
+		#define IOREMAP_WC(pa, bytes)	ioremap_nocache(pa, bytes)
+	#endif
+#endif
+
+#define	IOREMAP_UC(pa, bytes)	ioremap_nocache(pa, bytes)
+
+IMG_VOID PVRLinuxMUtilsInit(IMG_VOID);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osfunc.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osfunc.c
new file mode 100644
index 0000000..60b8a69
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osfunc.c
@@ -0,0 +1,2529 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/version.h>
+#include <asm/io.h>
+#include <asm/page.h>
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
+#include <asm/system.h>
+#endif
+#if defined(SUPPORT_CPU_CACHED_BUFFERS)
+#include <asm/cacheflush.h>
+#endif
+#include <linux/mm.h>
+#include <linux/pagemap.h>
+#include <linux/hugetlb.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+#include <linux/delay.h>
+#include <linux/pci.h>
+
+#include <linux/string.h>
+#include <linux/sched.h>
+#include <linux/interrupt.h>
+#include <asm/hardirq.h>
+#include <linux/timer.h>
+#include <linux/capability.h>
+#include <asm/uaccess.h>
+#include <linux/spinlock.h>
+#if defined(PVR_LINUX_MISR_USING_WORKQUEUE) || \
+	defined(PVR_LINUX_MISR_USING_PRIVATE_WORKQUEUE) || \
+	defined(PVR_LINUX_TIMERS_USING_WORKQUEUES) || \
+	defined(PVR_LINUX_USING_WORKQUEUES)
+#include <linux/workqueue.h>
+#endif
+
+#include "img_types.h"
+#include "services_headers.h"
+#include "mm.h"
+#include "pvrmmap.h"
+#include "mmap.h"
+#include "env_data.h"
+#include "proc.h"
+#include "mutex.h"
+#include "event.h"
+#include "linkage.h"
+
+#define EVENT_OBJECT_TIMEOUT_MS		(100)
+
+#if defined(SUPPORT_CPU_CACHED_BUFFERS) || \
+	defined(SUPPORT_CACHEFLUSH_ON_ALLOC)
+
+#if defined(__i386__)
+static void per_cpu_cache_flush(void *arg)
+{
+    PVR_UNREFERENCED_PARAMETER(arg);
+    wbinvd();
+}
+#endif
+
+#if !defined(SUPPORT_CPU_CACHED_BUFFERS)
+static
+#endif
+IMG_VOID OSFlushCPUCacheKM(IMG_VOID)
+{
+#if defined(__arm__)
+    flush_cache_all();
+#elif defined(__i386__)
+
+    on_each_cpu(per_cpu_cache_flush, NULL, 1);
+#else
+#error "Implement full CPU cache flush for this CPU!"
+#endif
+}
+
+#endif
+#if defined(SUPPORT_CPU_CACHED_BUFFERS)
+
+IMG_VOID OSFlushCPUCacheRangeKM(IMG_VOID *pvRangeAddrStart,
+								IMG_VOID *pvRangeAddrEnd)
+{
+	PVR_UNREFERENCED_PARAMETER(pvRangeAddrStart);
+	PVR_UNREFERENCED_PARAMETER(pvRangeAddrEnd);
+
+
+	OSFlushCPUCacheKM();
+}
+
+#endif
+
+#define HOST_ALLOC_MEM_USING_KMALLOC ((IMG_HANDLE)0)
+#define HOST_ALLOC_MEM_USING_VMALLOC ((IMG_HANDLE)1)
+
+#if !defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+PVRSRV_ERROR OSAllocMem_Impl(IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size, IMG_PVOID *ppvCpuVAddr, IMG_HANDLE *phBlockAlloc)
+#else
+PVRSRV_ERROR OSAllocMem_Impl(IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size, IMG_PVOID *ppvCpuVAddr, IMG_HANDLE *phBlockAlloc, IMG_CHAR *pszFilename, IMG_UINT32 ui32Line)
+#endif
+{
+    PVR_UNREFERENCED_PARAMETER(ui32Flags);
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    *ppvCpuVAddr = _KMallocWrapper(ui32Size, pszFilename, ui32Line);
+#else
+    *ppvCpuVAddr = KMallocWrapper(ui32Size);
+#endif
+    if(*ppvCpuVAddr)
+    {
+    if (phBlockAlloc)
+    {
+
+        *phBlockAlloc = HOST_ALLOC_MEM_USING_KMALLOC;
+    }
+    }
+    else
+    {
+    if (!phBlockAlloc)
+    {
+        return PVRSRV_ERROR_OUT_OF_MEMORY;
+    }
+
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    *ppvCpuVAddr = _VMallocWrapper(ui32Size, PVRSRV_HAP_CACHED, pszFilename, ui32Line);
+#else
+    *ppvCpuVAddr = VMallocWrapper(ui32Size, PVRSRV_HAP_CACHED);
+#endif
+    if (!*ppvCpuVAddr)
+    {
+         return PVRSRV_ERROR_OUT_OF_MEMORY;
+    }
+
+
+    *phBlockAlloc = HOST_ALLOC_MEM_USING_VMALLOC;
+    }
+
+    return PVRSRV_OK;
+}
+
+
+#if !defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+PVRSRV_ERROR OSFreeMem_Impl(IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size, IMG_PVOID pvCpuVAddr, IMG_HANDLE hBlockAlloc)
+#else
+PVRSRV_ERROR OSFreeMem_Impl(IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size, IMG_PVOID pvCpuVAddr, IMG_HANDLE hBlockAlloc, IMG_CHAR *pszFilename, IMG_UINT32 ui32Line)
+#endif
+{
+    PVR_UNREFERENCED_PARAMETER(ui32Flags);
+    PVR_UNREFERENCED_PARAMETER(ui32Size);
+
+    if (hBlockAlloc == HOST_ALLOC_MEM_USING_VMALLOC)
+    {
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    _VFreeWrapper(pvCpuVAddr, pszFilename, ui32Line);
+#else
+    VFreeWrapper(pvCpuVAddr);
+#endif
+    }
+    else
+    {
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    _KFreeWrapper(pvCpuVAddr, pszFilename, ui32Line);
+#else
+        KFreeWrapper(pvCpuVAddr);
+#endif
+    }
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR
+OSAllocPages_Impl(IMG_UINT32 ui32AllocFlags,
+                IMG_UINT32 ui32Size,
+                IMG_UINT32 ui32PageSize,
+                IMG_VOID **ppvCpuVAddr,
+                IMG_HANDLE *phOSMemHandle)
+{
+    LinuxMemArea *psLinuxMemArea;
+
+    PVR_UNREFERENCED_PARAMETER(ui32PageSize);
+
+#if 0
+
+    if(ui32AllocFlags & PVRSRV_HAP_SINGLE_PROCESS)
+    {
+        ui32AllocFlags &= ~PVRSRV_HAP_SINGLE_PROCESS;
+        ui32AllocFlags |= PVRSRV_HAP_MULTI_PROCESS;
+    }
+#endif
+
+    switch(ui32AllocFlags & PVRSRV_HAP_MAPTYPE_MASK)
+    {
+        case PVRSRV_HAP_KERNEL_ONLY:
+        {
+            psLinuxMemArea = NewVMallocLinuxMemArea(ui32Size, ui32AllocFlags);
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_OUT_OF_MEMORY;
+            }
+            break;
+        }
+        case PVRSRV_HAP_SINGLE_PROCESS:
+        {
+
+
+            psLinuxMemArea = NewAllocPagesLinuxMemArea(ui32Size, ui32AllocFlags);
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_OUT_OF_MEMORY;
+            }
+            PVRMMapRegisterArea(psLinuxMemArea);
+            break;
+        }
+
+        case PVRSRV_HAP_MULTI_PROCESS:
+        {
+
+#if defined(VIVT_CACHE) || defined(__sh__)
+
+            ui32AllocFlags &= ~PVRSRV_HAP_CACHED;
+#endif
+            psLinuxMemArea = NewVMallocLinuxMemArea(ui32Size, ui32AllocFlags);
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_OUT_OF_MEMORY;
+            }
+            PVRMMapRegisterArea(psLinuxMemArea);
+            break;
+        }
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "OSAllocPages: invalid flags 0x%x\n", ui32AllocFlags));
+            *ppvCpuVAddr = NULL;
+            *phOSMemHandle = (IMG_HANDLE)0;
+            return PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+#if defined(SUPPORT_CACHEFLUSH_ON_ALLOC)
+
+    if(ui32AllocFlags & (PVRSRV_HAP_WRITECOMBINE | PVRSRV_HAP_UNCACHED))
+    {
+        OSFlushCPUCacheKM();
+    }
+#endif
+
+    *ppvCpuVAddr = LinuxMemAreaToCpuVAddr(psLinuxMemArea);
+    *phOSMemHandle = psLinuxMemArea;
+
+    LinuxMemAreaRegister(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR
+OSFreePages(IMG_UINT32 ui32AllocFlags, IMG_UINT32 ui32Bytes, IMG_VOID *pvCpuVAddr, IMG_HANDLE hOSMemHandle)
+{
+    LinuxMemArea *psLinuxMemArea;
+    PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+    PVR_UNREFERENCED_PARAMETER(pvCpuVAddr);
+
+    psLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+
+    switch(ui32AllocFlags & PVRSRV_HAP_MAPTYPE_MASK)
+    {
+        case PVRSRV_HAP_KERNEL_ONLY:
+            break;
+        case PVRSRV_HAP_SINGLE_PROCESS:
+        case PVRSRV_HAP_MULTI_PROCESS:
+            if(PVRMMapRemoveRegisteredArea(psLinuxMemArea) != PVRSRV_OK)
+            {
+                PVR_DPF((PVR_DBG_ERROR,
+                         "OSFreePages(ui32AllocFlags=0x%08X, ui32Bytes=%ld, "
+                                        "pvCpuVAddr=%p, hOSMemHandle=%p) FAILED!",
+                         ui32AllocFlags, ui32Bytes, pvCpuVAddr, hOSMemHandle));
+                return PVRSRV_ERROR_GENERIC;
+            }
+            break;
+        default:
+            PVR_DPF((PVR_DBG_ERROR,"%s: invalid flags 0x%x\n",
+                    __FUNCTION__, ui32AllocFlags));
+            return PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+    LinuxMemAreaDeepFree(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR
+OSGetSubMemHandle(IMG_HANDLE hOSMemHandle,
+                  IMG_UINT32 ui32ByteOffset,
+                  IMG_UINT32 ui32Bytes,
+                  IMG_UINT32 ui32Flags,
+                  IMG_HANDLE *phOSMemHandleRet)
+{
+    LinuxMemArea *psParentLinuxMemArea, *psLinuxMemArea;
+    PVRSRV_ERROR eError;
+
+    psParentLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+
+    psLinuxMemArea = NewSubLinuxMemArea(psParentLinuxMemArea, ui32ByteOffset, ui32Bytes);
+    if(!psLinuxMemArea)
+    {
+        *phOSMemHandleRet = NULL;
+        return PVRSRV_ERROR_OUT_OF_MEMORY;
+    }
+    *phOSMemHandleRet = psLinuxMemArea;
+
+
+    if(ui32Flags & PVRSRV_HAP_KERNEL_ONLY)
+    {
+        return PVRSRV_OK;
+    }
+
+    eError = PVRMMapRegisterArea(psLinuxMemArea);
+    if(eError != PVRSRV_OK)
+     {
+        goto failed_register_area;
+    }
+
+    return PVRSRV_OK;
+
+failed_register_area:
+    *phOSMemHandleRet = NULL;
+    LinuxMemAreaDeepFree(psLinuxMemArea);
+    return eError;
+}
+
+PVRSRV_ERROR
+OSReleaseSubMemHandle(IMG_VOID *hOSMemHandle, IMG_UINT32 ui32Flags)
+{
+    LinuxMemArea *psLinuxMemArea;
+    PVRSRV_ERROR eError;
+
+    psLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+    PVR_ASSERT(psLinuxMemArea->eAreaType == LINUX_MEM_AREA_SUB_ALLOC);
+
+    if((ui32Flags & PVRSRV_HAP_KERNEL_ONLY) == 0)
+    {
+        eError = PVRMMapRemoveRegisteredArea(psLinuxMemArea);
+        if(eError != PVRSRV_OK)
+        {
+            return eError;
+        }
+    }
+    LinuxMemAreaDeepFree(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+
+IMG_CPU_PHYADDR
+OSMemHandleToCpuPAddr(IMG_VOID *hOSMemHandle, IMG_UINT32 ui32ByteOffset)
+{
+    PVR_ASSERT(hOSMemHandle);
+
+    return LinuxMemAreaToCpuPAddr(hOSMemHandle, ui32ByteOffset);
+}
+
+
+
+IMG_VOID OSMemCopy(IMG_VOID *pvDst, IMG_VOID *pvSrc, IMG_UINT32 ui32Size)
+{
+#if defined(USE_UNOPTIMISED_MEMCPY)
+    IMG_UINT8 *Src,*Dst;
+    IMG_INT i;
+
+    Src=(IMG_UINT8 *)pvSrc;
+    Dst=(IMG_UINT8 *)pvDst;
+    for(i=0;i<ui32Size;i++)
+    {
+        Dst[i]=Src[i];
+    }
+#else
+    memcpy(pvDst, pvSrc, ui32Size);
+#endif
+}
+
+
+IMG_VOID OSMemSet(IMG_VOID *pvDest, IMG_UINT8 ui8Value, IMG_UINT32 ui32Size)
+{
+#if defined(USE_UNOPTIMISED_MEMSET)
+    IMG_UINT8 *Buff;
+    IMG_INT i;
+
+    Buff=(IMG_UINT8 *)pvDest;
+    for(i=0;i<ui32Size;i++)
+    {
+        Buff[i]=ui8Value;
+    }
+#else
+    memset(pvDest, (IMG_INT) ui8Value, (size_t) ui32Size);
+#endif
+}
+
+
+IMG_CHAR *OSStringCopy(IMG_CHAR *pszDest, const IMG_CHAR *pszSrc)
+{
+    return (strcpy(pszDest, pszSrc));
+}
+
+IMG_INT32 OSSNPrintf(IMG_CHAR *pStr, IMG_UINT32 ui32Size, const IMG_CHAR *pszFormat, ...)
+{
+    va_list argList;
+    IMG_INT32 iCount;
+
+    va_start(argList, pszFormat);
+    iCount = vsnprintf(pStr, (size_t)ui32Size, pszFormat, argList);
+    va_end(argList);
+
+    return iCount;
+}
+
+IMG_VOID OSBreakResourceLock (PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID)
+{
+    volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)&psResource->ui32Lock;
+
+    if(*pui32Access)
+    {
+        if(psResource->ui32ID == ui32ID)
+        {
+            psResource->ui32ID = 0;
+            *pui32Access = 0;
+        }
+        else
+        {
+            PVR_DPF((PVR_DBG_MESSAGE,"OSBreakResourceLock: Resource is not locked for this process."));
+        }
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_MESSAGE,"OSBreakResourceLock: Resource is not locked"));
+    }
+}
+
+
+PVRSRV_ERROR OSCreateResource(PVRSRV_RESOURCE *psResource)
+{
+    psResource->ui32ID = 0;
+    psResource->ui32Lock = 0;
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSDestroyResource (PVRSRV_RESOURCE *psResource)
+{
+    OSBreakResourceLock (psResource, psResource->ui32ID);
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSInitEnvData(IMG_PVOID *ppvEnvSpecificData)
+{
+    ENV_DATA		*psEnvData;
+
+
+    if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(ENV_DATA), (IMG_VOID **)&psEnvData, IMG_NULL,
+        "Environment Data") != PVRSRV_OK)
+    {
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, PVRSRV_MAX_BRIDGE_IN_SIZE + PVRSRV_MAX_BRIDGE_OUT_SIZE,
+                    &psEnvData->pvBridgeData, IMG_NULL,
+                    "Bridge Data") != PVRSRV_OK)
+    {
+        OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(ENV_DATA), psEnvData, IMG_NULL);
+
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+
+
+    psEnvData->bMISRInstalled = IMG_FALSE;
+    psEnvData->bLISRInstalled = IMG_FALSE;
+
+
+    *ppvEnvSpecificData = psEnvData;
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSDeInitEnvData(IMG_PVOID pvEnvSpecificData)
+{
+    ENV_DATA		*psEnvData = (ENV_DATA*)pvEnvSpecificData;
+
+    PVR_ASSERT(!psEnvData->bMISRInstalled);
+    PVR_ASSERT(!psEnvData->bLISRInstalled);
+
+    OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, PVRSRV_MAX_BRIDGE_IN_SIZE + PVRSRV_MAX_BRIDGE_OUT_SIZE, psEnvData->pvBridgeData, IMG_NULL);
+    psEnvData->pvBridgeData = IMG_NULL;
+
+    OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(ENV_DATA), pvEnvSpecificData, IMG_NULL);
+
+
+    return PVRSRV_OK;
+}
+
+
+
+IMG_VOID OSReleaseThreadQuanta(IMG_VOID)
+{
+    schedule();
+}
+
+
+
+IMG_UINT32 OSClockus(IMG_VOID)
+{
+    IMG_UINT32 time, j = jiffies;
+
+    time = j * (1000000 / HZ);
+
+    return time;
+}
+
+
+
+IMG_VOID OSWaitus(IMG_UINT32 ui32Timeus)
+{
+    udelay(ui32Timeus);
+}
+
+
+IMG_UINT32 OSGetCurrentProcessIDKM(IMG_VOID)
+{
+    if (in_interrupt())
+    {
+        return KERNEL_ID;
+    }
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
+    return (IMG_UINT32)current->pgrp;
+#else
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
+    return (IMG_UINT32)task_tgid_nr(current);
+#else
+    return (IMG_UINT32)current->tgid;
+#endif
+#endif
+}
+
+
+IMG_UINT32 OSGetPageSize(IMG_VOID)
+{
+#if defined(__sh__)
+    IMG_UINT32 ui32ReturnValue = PAGE_SIZE;
+
+    return (ui32ReturnValue);
+#else
+    return PAGE_SIZE;
+#endif
+}
+
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
+static irqreturn_t DeviceISRWrapper(int irq, void *dev_id
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
+        , struct pt_regs *regs
+#endif
+        )
+{
+    PVRSRV_DEVICE_NODE *psDeviceNode;
+    IMG_BOOL bStatus = IMG_FALSE;
+
+    PVR_UNREFERENCED_PARAMETER(irq);
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
+    PVR_UNREFERENCED_PARAMETER(regs);
+#endif
+    psDeviceNode = (PVRSRV_DEVICE_NODE*)dev_id;
+    if(!psDeviceNode)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "DeviceISRWrapper: invalid params\n"));
+        goto out;
+    }
+
+    bStatus = PVRSRVDeviceLISR(psDeviceNode);
+
+    if (bStatus)
+    {
+		OSScheduleMISR((IMG_VOID *)psDeviceNode->psSysData);
+    }
+
+out:
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
+    return bStatus ? IRQ_HANDLED : IRQ_NONE;
+#endif
+}
+
+
+
+static irqreturn_t SystemISRWrapper(int irq, void *dev_id
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
+        , struct pt_regs *regs
+#endif
+        )
+{
+    SYS_DATA *psSysData;
+    IMG_BOOL bStatus = IMG_FALSE;
+
+    PVR_UNREFERENCED_PARAMETER(irq);
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
+    PVR_UNREFERENCED_PARAMETER(regs);
+#endif
+    psSysData = (SYS_DATA *)dev_id;
+    if(!psSysData)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "SystemISRWrapper: invalid params\n"));
+        goto out;
+    }
+
+    bStatus = PVRSRVSystemLISR(psSysData);
+
+    if (bStatus)
+    {
+        OSScheduleMISR((IMG_VOID *)psSysData);
+    }
+
+out:
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
+    return bStatus ? IRQ_HANDLED : IRQ_NONE;
+#endif
+}
+PVRSRV_ERROR OSInstallDeviceLISR(IMG_VOID *pvSysData,
+                                    IMG_UINT32 ui32Irq,
+                                    IMG_CHAR *pszISRName,
+                                    IMG_VOID *pvDeviceNode)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+    if (psEnvData->bLISRInstalled)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSInstallDeviceLISR: An ISR has already been installed: IRQ %d cookie %x", psEnvData->ui32IRQ, psEnvData->pvISRCookie));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    PVR_TRACE(("Installing device LISR %s on IRQ %d with cookie %x", pszISRName, ui32Irq, pvDeviceNode));
+
+    if(request_irq(ui32Irq, DeviceISRWrapper,
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22))
+        SA_SHIRQ
+#else
+        IRQF_SHARED
+#endif
+        , pszISRName, pvDeviceNode))
+    {
+        PVR_DPF((PVR_DBG_ERROR,"OSInstallDeviceLISR: Couldn't install device LISR on IRQ %d", ui32Irq));
+
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    psEnvData->ui32IRQ = ui32Irq;
+    psEnvData->pvISRCookie = pvDeviceNode;
+    psEnvData->bLISRInstalled = IMG_TRUE;
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSUninstallDeviceLISR(IMG_VOID *pvSysData)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+    if (!psEnvData->bLISRInstalled)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSUninstallDeviceLISR: No LISR has been installed"));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    PVR_TRACE(("Uninstalling device LISR on IRQ %d with cookie %x", psEnvData->ui32IRQ,  psEnvData->pvISRCookie));
+
+    free_irq(psEnvData->ui32IRQ, psEnvData->pvISRCookie);
+
+    psEnvData->bLISRInstalled = IMG_FALSE;
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSInstallSystemLISR(IMG_VOID *pvSysData, IMG_UINT32 ui32Irq)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+    if (psEnvData->bLISRInstalled)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSInstallSystemLISR: An LISR has already been installed: IRQ %d cookie %x", psEnvData->ui32IRQ, psEnvData->pvISRCookie));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    PVR_TRACE(("Installing system LISR on IRQ %d with cookie %x", ui32Irq, pvSysData));
+
+    if(request_irq(ui32Irq, SystemISRWrapper,
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22))
+        SA_SHIRQ
+#else
+        IRQF_SHARED
+#endif
+        , "PowerVR", pvSysData))
+    {
+        PVR_DPF((PVR_DBG_ERROR,"OSInstallSystemLISR: Couldn't install system LISR on IRQ %d", ui32Irq));
+
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    psEnvData->ui32IRQ = ui32Irq;
+    psEnvData->pvISRCookie = pvSysData;
+    psEnvData->bLISRInstalled = IMG_TRUE;
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSUninstallSystemLISR(IMG_VOID *pvSysData)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+    if (!psEnvData->bLISRInstalled)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSUninstallSystemLISR: No LISR has been installed"));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    PVR_TRACE(("Uninstalling system LISR on IRQ %d with cookie %x", psEnvData->ui32IRQ, psEnvData->pvISRCookie));
+
+    free_irq(psEnvData->ui32IRQ, psEnvData->pvISRCookie);
+
+    psEnvData->bLISRInstalled = IMG_FALSE;
+
+    return PVRSRV_OK;
+}
+
+#if defined(PVR_LINUX_MISR_USING_PRIVATE_WORKQUEUE)
+static void MISRWrapper(
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
+			void *data
+#else
+			struct work_struct *data
+#endif
+)
+{
+	ENV_DATA *psEnvData = container_of(data, ENV_DATA, sMISRWork);
+	SYS_DATA *psSysData  = (SYS_DATA *)psEnvData->pvMISRData;
+
+	PVRSRVMISR(psSysData);
+}
+
+
+PVRSRV_ERROR OSInstallMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+	ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+	if (psEnvData->bMISRInstalled)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "OSInstallMISR: An MISR has already been installed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	PVR_TRACE(("Installing MISR with cookie %p", pvSysData));
+
+	psEnvData->psWorkQueue = create_singlethread_workqueue("pvr_workqueue");
+
+	if (psEnvData->psWorkQueue == IMG_NULL)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "OSInstallMISR: create_singlethreaded_workqueue failed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	INIT_WORK(&psEnvData->sMISRWork, MISRWrapper
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
+		, (void *)&psEnvData->sMISRWork
+#endif
+				);
+
+	psEnvData->pvMISRData = pvSysData;
+	psEnvData->bMISRInstalled = IMG_TRUE;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSUninstallMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+	ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+	if (!psEnvData->bMISRInstalled)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "OSUninstallMISR: No MISR has been installed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	PVR_TRACE(("Uninstalling MISR"));
+
+	destroy_workqueue(psEnvData->psWorkQueue);
+
+	psEnvData->bMISRInstalled = IMG_FALSE;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSScheduleMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+	ENV_DATA *psEnvData = (ENV_DATA*)psSysData->pvEnvSpecificData;
+
+	if (psEnvData->bMISRInstalled)
+	{
+		queue_work(psEnvData->psWorkQueue, &psEnvData->sMISRWork);
+	}
+
+	return PVRSRV_OK;
+}
+#else
+#if defined(PVR_LINUX_MISR_USING_WORKQUEUE)
+static void MISRWrapper(
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
+			void *data
+#else
+			struct work_struct *data
+#endif
+)
+{
+	ENV_DATA *psEnvData = container_of(data, ENV_DATA, sMISRWork);
+	SYS_DATA *psSysData  = (SYS_DATA *)psEnvData->pvMISRData;
+
+	PVRSRVMISR(psSysData);
+}
+
+
+PVRSRV_ERROR OSInstallMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+	ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+	if (psEnvData->bMISRInstalled)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "OSInstallMISR: An MISR has already been installed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	PVR_TRACE(("Installing MISR with cookie %x", pvSysData));
+
+	INIT_WORK(&psEnvData->sMISRWork, MISRWrapper
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
+		, (void *)&psEnvData->sMISRWork
+#endif
+				);
+
+	psEnvData->pvMISRData = pvSysData;
+	psEnvData->bMISRInstalled = IMG_TRUE;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSUninstallMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+	ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+	if (!psEnvData->bMISRInstalled)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "OSUninstallMISR: No MISR has been installed"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	PVR_TRACE(("Uninstalling MISR"));
+
+	flush_scheduled_work();
+
+	psEnvData->bMISRInstalled = IMG_FALSE;
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSScheduleMISR(IMG_VOID *pvSysData)
+{
+	SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+	ENV_DATA *psEnvData = (ENV_DATA*)psSysData->pvEnvSpecificData;
+
+	if (psEnvData->bMISRInstalled)
+	{
+		schedule_work(&psEnvData->sMISRWork);
+	}
+
+	return PVRSRV_OK;
+}
+
+#else
+
+
+static void MISRWrapper(unsigned long data)
+{
+    SYS_DATA *psSysData;
+
+    psSysData = (SYS_DATA *)data;
+
+    PVRSRVMISR(psSysData);
+}
+
+
+PVRSRV_ERROR OSInstallMISR(IMG_VOID *pvSysData)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+    if (psEnvData->bMISRInstalled)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSInstallMISR: An MISR has already been installed"));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    PVR_TRACE(("Installing MISR with cookie %x", pvSysData));
+
+    tasklet_init(&psEnvData->sMISRTasklet, MISRWrapper, (unsigned long)pvSysData);
+
+    psEnvData->bMISRInstalled = IMG_TRUE;
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSUninstallMISR(IMG_VOID *pvSysData)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA *)psSysData->pvEnvSpecificData;
+
+    if (!psEnvData->bMISRInstalled)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSUninstallMISR: No MISR has been installed"));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    PVR_TRACE(("Uninstalling MISR"));
+
+    tasklet_kill(&psEnvData->sMISRTasklet);
+
+    psEnvData->bMISRInstalled = IMG_FALSE;
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSScheduleMISR(IMG_VOID *pvSysData)
+{
+    SYS_DATA *psSysData = (SYS_DATA*)pvSysData;
+    ENV_DATA *psEnvData = (ENV_DATA*)psSysData->pvEnvSpecificData;
+
+    if (psEnvData->bMISRInstalled)
+    {
+        tasklet_schedule(&psEnvData->sMISRTasklet);
+    }
+
+    return PVRSRV_OK;
+}
+
+#endif
+#endif
+
+#endif
+
+IMG_VOID OSPanic(IMG_VOID)
+{
+	BUG();
+}
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
+#define	OS_TAS(p)	xchg((p), 1)
+#else
+#define	OS_TAS(p)	tas(p)
+#endif
+PVRSRV_ERROR OSLockResource ( PVRSRV_RESOURCE 	*psResource,
+                                IMG_UINT32 			ui32ID)
+
+{
+    PVRSRV_ERROR eError = PVRSRV_OK;
+
+    if(!OS_TAS(&psResource->ui32Lock))
+        psResource->ui32ID = ui32ID;
+    else
+        eError = PVRSRV_ERROR_GENERIC;
+
+    return eError;
+}
+
+
+PVRSRV_ERROR OSUnlockResource (PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID)
+{
+    volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)&psResource->ui32Lock;
+    PVRSRV_ERROR eError = PVRSRV_OK;
+
+    if(*pui32Access)
+    {
+        if(psResource->ui32ID == ui32ID)
+        {
+            psResource->ui32ID = 0;
+            *pui32Access = 0;
+        }
+        else
+        {
+            PVR_DPF((PVR_DBG_ERROR,"OSUnlockResource: Resource %p is not locked with expected value.", psResource));
+            PVR_DPF((PVR_DBG_MESSAGE,"Should be %x is actually %x", ui32ID, psResource->ui32ID));
+            eError = PVRSRV_ERROR_GENERIC;
+        }
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR,"OSUnlockResource: Resource %p is not locked", psResource));
+        eError = PVRSRV_ERROR_GENERIC;
+    }
+
+    return eError;
+}
+
+
+IMG_BOOL OSIsResourceLocked (PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID)
+{
+    volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)&psResource->ui32Lock;
+
+    return 	(*(volatile IMG_UINT32 *)pui32Access == 1) && (psResource->ui32ID == ui32ID)
+            ?	IMG_TRUE
+            :	IMG_FALSE;
+}
+
+
+IMG_CPU_PHYADDR OSMapLinToCPUPhys(IMG_VOID *pvLinAddr)
+{
+    IMG_CPU_PHYADDR CpuPAddr;
+
+    CpuPAddr.uiAddr = (IMG_UINTPTR_T)VMallocToPhys(pvLinAddr);
+
+    return CpuPAddr;
+}
+
+
+IMG_VOID *
+OSMapPhysToLin(IMG_CPU_PHYADDR BasePAddr,
+               IMG_UINT32 ui32Bytes,
+               IMG_UINT32 ui32MappingFlags,
+               IMG_HANDLE *phOSMemHandle)
+{
+    if(phOSMemHandle)
+    {
+        *phOSMemHandle = (IMG_HANDLE)0;
+    }
+
+    if(ui32MappingFlags & PVRSRV_HAP_KERNEL_ONLY)
+    {
+        IMG_VOID *pvIORemapCookie;
+        pvIORemapCookie = IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags);
+        if(pvIORemapCookie == IMG_NULL)
+        {
+            return NULL;
+        }
+        return pvIORemapCookie;
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+                 "OSMapPhysToLin should only be used with PVRSRV_HAP_KERNEL_ONLY "
+                 " (Use OSReservePhys otherwise)"));
+        return NULL;
+    }
+}
+
+IMG_BOOL
+OSUnMapPhysToLin(IMG_VOID *pvLinAddr, IMG_UINT32 ui32Bytes, IMG_UINT32 ui32MappingFlags, IMG_HANDLE hPageAlloc)
+{
+    PVR_TRACE(("%s: unmapping %d bytes from 0x%08x", __FUNCTION__, ui32Bytes, pvLinAddr));
+
+    PVR_UNREFERENCED_PARAMETER(hPageAlloc);
+    PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+
+    if(ui32MappingFlags & PVRSRV_HAP_KERNEL_ONLY)
+    {
+        IOUnmapWrapper(pvLinAddr);
+        return IMG_TRUE;
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+                     "OSUnMapPhysToLin should only be used with PVRSRV_HAP_KERNEL_ONLY "
+                     " (Use OSUnReservePhys otherwise)"));
+        return IMG_FALSE;
+    }
+}
+
+static PVRSRV_ERROR
+RegisterExternalMem(IMG_SYS_PHYADDR *pBasePAddr,
+          IMG_VOID *pvCPUVAddr,
+              IMG_UINT32 ui32Bytes,
+          IMG_BOOL bPhysContig,
+              IMG_UINT32 ui32MappingFlags,
+              IMG_HANDLE *phOSMemHandle)
+{
+    LinuxMemArea *psLinuxMemArea;
+
+    switch(ui32MappingFlags & PVRSRV_HAP_MAPTYPE_MASK)
+    {
+        case PVRSRV_HAP_KERNEL_ONLY:
+        {
+        psLinuxMemArea = NewExternalKVLinuxMemArea(pBasePAddr, pvCPUVAddr, ui32Bytes, bPhysContig, ui32MappingFlags);
+
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_GENERIC;
+            }
+            break;
+        }
+        case PVRSRV_HAP_SINGLE_PROCESS:
+        {
+        psLinuxMemArea = NewExternalKVLinuxMemArea(pBasePAddr, pvCPUVAddr, ui32Bytes, bPhysContig, ui32MappingFlags);
+
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_GENERIC;
+            }
+            PVRMMapRegisterArea(psLinuxMemArea);
+            break;
+        }
+        case PVRSRV_HAP_MULTI_PROCESS:
+        {
+
+#if defined(VIVT_CACHE) || defined(__sh__)
+
+            ui32MappingFlags &= ~PVRSRV_HAP_CACHED;
+#endif
+        psLinuxMemArea = NewExternalKVLinuxMemArea(pBasePAddr, pvCPUVAddr, ui32Bytes, bPhysContig, ui32MappingFlags);
+
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_GENERIC;
+            }
+            PVRMMapRegisterArea(psLinuxMemArea);
+            break;
+        }
+        default:
+            PVR_DPF((PVR_DBG_ERROR,"OSRegisterMem : invalid flags 0x%x\n", ui32MappingFlags));
+            *phOSMemHandle = (IMG_HANDLE)0;
+            return PVRSRV_ERROR_GENERIC;
+    }
+
+    *phOSMemHandle = (IMG_HANDLE)psLinuxMemArea;
+
+    LinuxMemAreaRegister(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR
+OSRegisterMem(IMG_CPU_PHYADDR BasePAddr,
+              IMG_VOID *pvCPUVAddr,
+              IMG_UINT32 ui32Bytes,
+              IMG_UINT32 ui32MappingFlags,
+              IMG_HANDLE *phOSMemHandle)
+{
+    IMG_SYS_PHYADDR SysPAddr = SysCpuPAddrToSysPAddr(BasePAddr);
+
+    return RegisterExternalMem(&SysPAddr, pvCPUVAddr, ui32Bytes, IMG_TRUE, ui32MappingFlags, phOSMemHandle);
+}
+
+
+PVRSRV_ERROR OSRegisterDiscontigMem(IMG_SYS_PHYADDR *pBasePAddr, IMG_VOID *pvCPUVAddr, IMG_UINT32 ui32Bytes, IMG_UINT32 ui32MappingFlags, IMG_HANDLE *phOSMemHandle)
+{
+    return RegisterExternalMem(pBasePAddr, pvCPUVAddr, ui32Bytes, IMG_FALSE, ui32MappingFlags, phOSMemHandle);
+}
+
+
+PVRSRV_ERROR
+OSUnRegisterMem (IMG_VOID *pvCpuVAddr,
+                IMG_UINT32 ui32Bytes,
+                IMG_UINT32 ui32MappingFlags,
+                IMG_HANDLE hOSMemHandle)
+{
+    LinuxMemArea *psLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+
+    PVR_UNREFERENCED_PARAMETER(pvCpuVAddr);
+    PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+
+    switch(ui32MappingFlags & PVRSRV_HAP_MAPTYPE_MASK)
+    {
+        case PVRSRV_HAP_KERNEL_ONLY:
+            break;
+        case PVRSRV_HAP_SINGLE_PROCESS:
+        case PVRSRV_HAP_MULTI_PROCESS:
+        {
+            if(PVRMMapRemoveRegisteredArea(psLinuxMemArea) != PVRSRV_OK)
+            {
+                 PVR_DPF((PVR_DBG_ERROR, "%s(%p, %d, 0x%08X, %p) FAILED!",
+                          __FUNCTION__, pvCpuVAddr, ui32Bytes,
+                          ui32MappingFlags, hOSMemHandle));
+                return PVRSRV_ERROR_GENERIC;
+            }
+            break;
+        }
+        default:
+        {
+            PVR_DPF((PVR_DBG_ERROR, "OSUnRegisterMem : invalid flags 0x%x", ui32MappingFlags));
+            return PVRSRV_ERROR_INVALID_PARAMS;
+        }
+    }
+
+    LinuxMemAreaDeepFree(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSUnRegisterDiscontigMem(IMG_VOID *pvCpuVAddr, IMG_UINT32 ui32Bytes, IMG_UINT32 ui32Flags, IMG_HANDLE hOSMemHandle)
+{
+    return OSUnRegisterMem(pvCpuVAddr, ui32Bytes, ui32Flags, hOSMemHandle);
+}
+
+PVRSRV_ERROR
+OSReservePhys(IMG_CPU_PHYADDR BasePAddr,
+              IMG_UINT32 ui32Bytes,
+              IMG_UINT32 ui32MappingFlags,
+              IMG_VOID **ppvCpuVAddr,
+              IMG_HANDLE *phOSMemHandle)
+{
+    LinuxMemArea *psLinuxMemArea;
+
+#if 0
+
+    if(ui32MappingFlags & PVRSRV_HAP_SINGLE_PROCESS)
+    {
+        ui32MappingFlags &= ~PVRSRV_HAP_SINGLE_PROCESS;
+        ui32MappingFlags |= PVRSRV_HAP_MULTI_PROCESS;
+    }
+#endif
+
+    switch(ui32MappingFlags & PVRSRV_HAP_MAPTYPE_MASK)
+    {
+        case PVRSRV_HAP_KERNEL_ONLY:
+        {
+
+            psLinuxMemArea = NewIORemapLinuxMemArea(BasePAddr, ui32Bytes, ui32MappingFlags);
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_GENERIC;
+            }
+            break;
+        }
+        case PVRSRV_HAP_SINGLE_PROCESS:
+        {
+
+            psLinuxMemArea = NewIOLinuxMemArea(BasePAddr, ui32Bytes, ui32MappingFlags);
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_GENERIC;
+            }
+            PVRMMapRegisterArea(psLinuxMemArea);
+            break;
+        }
+        case PVRSRV_HAP_MULTI_PROCESS:
+        {
+
+#if defined(VIVT_CACHE) || defined(__sh__)
+
+            ui32MappingFlags &= ~PVRSRV_HAP_CACHED;
+#endif
+            psLinuxMemArea = NewIORemapLinuxMemArea(BasePAddr, ui32Bytes, ui32MappingFlags);
+            if(!psLinuxMemArea)
+            {
+                return PVRSRV_ERROR_GENERIC;
+            }
+            PVRMMapRegisterArea(psLinuxMemArea);
+            break;
+        }
+        default:
+            PVR_DPF((PVR_DBG_ERROR,"OSMapPhysToLin : invalid flags 0x%x\n", ui32MappingFlags));
+            *ppvCpuVAddr = NULL;
+            *phOSMemHandle = (IMG_HANDLE)0;
+            return PVRSRV_ERROR_GENERIC;
+    }
+
+    *phOSMemHandle = (IMG_HANDLE)psLinuxMemArea;
+    *ppvCpuVAddr = LinuxMemAreaToCpuVAddr(psLinuxMemArea);
+
+    LinuxMemAreaRegister(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR
+OSUnReservePhys(IMG_VOID *pvCpuVAddr,
+                IMG_UINT32 ui32Bytes,
+                IMG_UINT32 ui32MappingFlags,
+                IMG_HANDLE hOSMemHandle)
+{
+    LinuxMemArea *psLinuxMemArea;
+
+    PVR_UNREFERENCED_PARAMETER(pvCpuVAddr);
+   PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+
+    psLinuxMemArea = (LinuxMemArea *)hOSMemHandle;
+
+    switch(ui32MappingFlags & PVRSRV_HAP_MAPTYPE_MASK)
+    {
+        case PVRSRV_HAP_KERNEL_ONLY:
+            break;
+        case PVRSRV_HAP_SINGLE_PROCESS:
+        case PVRSRV_HAP_MULTI_PROCESS:
+        {
+            if(PVRMMapRemoveRegisteredArea(psLinuxMemArea) != PVRSRV_OK)
+            {
+                 PVR_DPF((PVR_DBG_ERROR, "%s(%p, %d, 0x%08X, %p) FAILED!",
+                          __FUNCTION__, pvCpuVAddr, ui32Bytes,
+                          ui32MappingFlags, hOSMemHandle));
+                return PVRSRV_ERROR_GENERIC;
+            }
+            break;
+        }
+        default:
+        {
+            PVR_DPF((PVR_DBG_ERROR, "OSUnMapPhysToLin : invalid flags 0x%x", ui32MappingFlags));
+            return PVRSRV_ERROR_INVALID_PARAMS;
+        }
+    }
+
+    LinuxMemAreaDeepFree(psLinuxMemArea);
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSBaseAllocContigMemory(IMG_UINT32 ui32Size, IMG_CPU_VIRTADDR *pvLinAddr, IMG_CPU_PHYADDR *psPhysAddr)
+{
+#if !defined(NO_HARDWARE)
+    PVR_UNREFERENCED_PARAMETER(ui32Size);
+    PVR_UNREFERENCED_PARAMETER(pvLinAddr);
+    PVR_UNREFERENCED_PARAMETER(psPhysAddr);
+    PVR_DPF((PVR_DBG_ERROR, "%s: Not available", __FUNCTION__));
+
+    return PVRSRV_ERROR_OUT_OF_MEMORY;
+#else
+    IMG_VOID *pvKernLinAddr;
+
+#if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+    pvKernLinAddr = _KMallocWrapper(ui32Size, __FILE__, __LINE__);
+#else
+    pvKernLinAddr = KMallocWrapper(ui32Size);
+#endif
+    if (!pvKernLinAddr)
+    {
+    return PVRSRV_ERROR_OUT_OF_MEMORY;
+    }
+
+    *pvLinAddr = pvKernLinAddr;
+
+    psPhysAddr->uiAddr = virt_to_phys(pvKernLinAddr);
+
+    return PVRSRV_OK;
+#endif
+}
+
+
+PVRSRV_ERROR OSBaseFreeContigMemory(IMG_UINT32 ui32Size, IMG_CPU_VIRTADDR pvLinAddr, IMG_CPU_PHYADDR psPhysAddr)
+{
+#if !defined(NO_HARDWARE)
+    PVR_UNREFERENCED_PARAMETER(ui32Size);
+    PVR_UNREFERENCED_PARAMETER(pvLinAddr);
+    PVR_UNREFERENCED_PARAMETER(psPhysAddr.uiAddr);
+
+    PVR_DPF((PVR_DBG_WARNING, "%s: Not available", __FUNCTION__));
+#else
+    PVR_UNREFERENCED_PARAMETER(ui32Size);
+    PVR_UNREFERENCED_PARAMETER(psPhysAddr.uiAddr);
+
+    KFreeWrapper(pvLinAddr);
+#endif
+    return PVRSRV_OK;
+}
+
+IMG_UINT32 OSReadHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset)
+{
+#if !defined(NO_HARDWARE)
+    return (IMG_UINT32) readl((IMG_PBYTE)pvLinRegBaseAddr+ui32Offset);
+#else
+    return *(IMG_UINT32 *)((IMG_PBYTE)pvLinRegBaseAddr+ui32Offset);
+#endif
+}
+
+IMG_VOID OSWriteHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset, IMG_UINT32 ui32Value)
+{
+#if !defined(NO_HARDWARE)
+    writel(ui32Value, (IMG_PBYTE)pvLinRegBaseAddr+ui32Offset);
+#else
+    *(IMG_UINT32 *)((IMG_PBYTE)pvLinRegBaseAddr+ui32Offset) = ui32Value;
+#endif
+}
+
+#if defined(CONFIG_PCI) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14))
+
+PVRSRV_PCI_DEV_HANDLE OSPCISetDev(IMG_VOID *pvPCICookie, HOST_PCI_INIT_FLAGS eFlags)
+{
+    int err;
+    IMG_UINT32 i;
+    PVR_PCI_DEV *psPVRPCI;
+
+    PVR_TRACE(("OSPCISetDev"));
+
+    if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(*psPVRPCI), (IMG_VOID **)&psPVRPCI, IMG_NULL,
+        "PCI Device") != PVRSRV_OK)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCISetDev: Couldn't allocate PVR PCI structure"));
+        return IMG_NULL;
+    }
+
+    psPVRPCI->psPCIDev = (struct pci_dev *)pvPCICookie;
+    psPVRPCI->ePCIFlags = eFlags;
+
+    err = pci_enable_device(psPVRPCI->psPCIDev);
+    if (err != 0)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCISetDev: Couldn't enable device (%d)", err));
+        return IMG_NULL;
+    }
+
+    if (psPVRPCI->ePCIFlags & HOST_PCI_INIT_FLAG_BUS_MASTER)
+    {
+        pci_set_master(psPVRPCI->psPCIDev);
+    }
+
+    if (psPVRPCI->ePCIFlags & HOST_PCI_INIT_FLAG_MSI)
+    {
+#if defined(CONFIG_PCI_MSI)
+        err = pci_enable_msi(psPVRPCI->psPCIDev);
+        if (err != 0)
+        {
+            PVR_DPF((PVR_DBG_WARNING, "OSPCISetDev: Couldn't enable MSI (%d)", err));
+            psPVRPCI->ePCIFlags &= ~HOST_PCI_INIT_FLAG_MSI;
+        }
+#else
+        PVR_DPF((PVR_DBG_WARNING, "OSPCISetDev: MSI support not enabled in the kernel"));
+#endif
+    }
+
+
+    for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
+    {
+        psPVRPCI->abPCIResourceInUse[i] = IMG_FALSE;
+    }
+
+    return (PVRSRV_PCI_DEV_HANDLE)psPVRPCI;
+}
+
+PVRSRV_PCI_DEV_HANDLE OSPCIAcquireDev(IMG_UINT16 ui16VendorID, IMG_UINT16 ui16DeviceID, HOST_PCI_INIT_FLAGS eFlags)
+{
+    struct pci_dev *psPCIDev;
+
+    psPCIDev = pci_get_device(ui16VendorID, ui16DeviceID, NULL);
+    if (psPCIDev == NULL)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCIAcquireDev: Couldn't acquire device"));
+        return IMG_NULL;
+    }
+
+    return OSPCISetDev((IMG_VOID *)psPCIDev, eFlags);
+}
+
+PVRSRV_ERROR OSPCIIRQ(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 *pui32IRQ)
+{
+    PVR_PCI_DEV *psPVRPCI = (PVR_PCI_DEV *)hPVRPCI;
+
+    *pui32IRQ = psPVRPCI->psPCIDev->irq;
+
+    return PVRSRV_OK;
+}
+
+enum HOST_PCI_ADDR_RANGE_FUNC
+{
+    HOST_PCI_ADDR_RANGE_FUNC_LEN,
+    HOST_PCI_ADDR_RANGE_FUNC_START,
+    HOST_PCI_ADDR_RANGE_FUNC_END,
+    HOST_PCI_ADDR_RANGE_FUNC_REQUEST,
+    HOST_PCI_ADDR_RANGE_FUNC_RELEASE
+};
+
+static IMG_UINT32 OSPCIAddrRangeFunc(enum HOST_PCI_ADDR_RANGE_FUNC eFunc,
+                                     PVRSRV_PCI_DEV_HANDLE hPVRPCI,
+                                     IMG_UINT32 ui32Index)
+{
+    PVR_PCI_DEV *psPVRPCI = (PVR_PCI_DEV *)hPVRPCI;
+
+    if (ui32Index >= DEVICE_COUNT_RESOURCE)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCIAddrRangeFunc: Index out of range"));
+        return 0;
+
+    }
+
+    switch (eFunc)
+    {
+        case HOST_PCI_ADDR_RANGE_FUNC_LEN:
+            return pci_resource_len(psPVRPCI->psPCIDev, ui32Index);
+        case HOST_PCI_ADDR_RANGE_FUNC_START:
+            return pci_resource_start(psPVRPCI->psPCIDev, ui32Index);
+        case HOST_PCI_ADDR_RANGE_FUNC_END:
+            return pci_resource_end(psPVRPCI->psPCIDev, ui32Index);
+        case HOST_PCI_ADDR_RANGE_FUNC_REQUEST:
+        {
+            int err;
+
+            err = pci_request_region(psPVRPCI->psPCIDev, (IMG_INT)ui32Index, "PowerVR");
+            if (err != 0)
+            {
+                PVR_DPF((PVR_DBG_ERROR, "OSPCIAddrRangeFunc: pci_request_region_failed (%d)", err));
+                return 0;
+            }
+            psPVRPCI->abPCIResourceInUse[ui32Index] = IMG_TRUE;
+            return 1;
+        }
+        case HOST_PCI_ADDR_RANGE_FUNC_RELEASE:
+            if (psPVRPCI->abPCIResourceInUse[ui32Index])
+            {
+                pci_release_region(psPVRPCI->psPCIDev, (IMG_INT)ui32Index);
+                psPVRPCI->abPCIResourceInUse[ui32Index] = IMG_FALSE;
+            }
+            return 1;
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "OSPCIAddrRangeFunc: Unknown function"));
+            break;
+    }
+
+    return 0;
+}
+
+IMG_UINT32 OSPCIAddrRangeLen(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index)
+{
+    return OSPCIAddrRangeFunc(HOST_PCI_ADDR_RANGE_FUNC_LEN, hPVRPCI, ui32Index);
+}
+
+IMG_UINT32 OSPCIAddrRangeStart(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index)
+{
+    return OSPCIAddrRangeFunc(HOST_PCI_ADDR_RANGE_FUNC_START, hPVRPCI, ui32Index);
+}
+
+IMG_UINT32 OSPCIAddrRangeEnd(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index)
+{
+    return OSPCIAddrRangeFunc(HOST_PCI_ADDR_RANGE_FUNC_END, hPVRPCI, ui32Index);
+}
+
+PVRSRV_ERROR OSPCIRequestAddrRange(PVRSRV_PCI_DEV_HANDLE hPVRPCI,
+                                   IMG_UINT32 ui32Index)
+{
+    return OSPCIAddrRangeFunc(HOST_PCI_ADDR_RANGE_FUNC_REQUEST, hPVRPCI, ui32Index) == 0 ? PVRSRV_ERROR_GENERIC : PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSPCIReleaseAddrRange(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index)
+{
+    return OSPCIAddrRangeFunc(HOST_PCI_ADDR_RANGE_FUNC_RELEASE, hPVRPCI, ui32Index) == 0 ? PVRSRV_ERROR_GENERIC : PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSPCIReleaseDev(PVRSRV_PCI_DEV_HANDLE hPVRPCI)
+{
+    PVR_PCI_DEV *psPVRPCI = (PVR_PCI_DEV *)hPVRPCI;
+    int i;
+
+    PVR_TRACE(("OSPCIReleaseDev"));
+
+
+    for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
+    {
+        if (psPVRPCI->abPCIResourceInUse[i])
+        {
+            PVR_TRACE(("OSPCIReleaseDev: Releasing Address range %d", i));
+            pci_release_region(psPVRPCI->psPCIDev, i);
+            psPVRPCI->abPCIResourceInUse[i] = IMG_FALSE;
+        }
+    }
+
+#if defined(CONFIG_PCI_MSI)
+    if (psPVRPCI->ePCIFlags & HOST_PCI_INIT_FLAG_MSI)
+    {
+        pci_disable_msi(psPVRPCI->psPCIDev);
+    }
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29))
+    if (psPVRPCI->ePCIFlags & HOST_PCI_INIT_FLAG_BUS_MASTER)
+    {
+        pci_clear_master(psPVRPCI->psPCIDev);
+    }
+#endif
+    pci_disable_device(psPVRPCI->psPCIDev);
+
+    OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(*psPVRPCI), (IMG_VOID *)psPVRPCI, IMG_NULL);
+
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSPCISuspendDev(PVRSRV_PCI_DEV_HANDLE hPVRPCI)
+{
+    PVR_PCI_DEV *psPVRPCI = (PVR_PCI_DEV *)hPVRPCI;
+    int err;
+
+    PVR_TRACE(("OSPCISuspendDev"));
+
+    err = pci_save_state(psPVRPCI->psPCIDev);
+    if (err != 0)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCISuspendDev: pci_save_state_failed (%d)", err));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    pci_disable_device(psPVRPCI->psPCIDev);
+
+    err = pci_set_power_state(psPVRPCI->psPCIDev, pci_choose_state(psPVRPCI->psPCIDev, PMSG_SUSPEND));
+    switch(err)
+    {
+        case 0:
+            break;
+        case -EIO:
+            PVR_DPF((PVR_DBG_WARNING, "OSPCISuspendDev: device doesn't support PCI PM"));
+            break;
+        case -EINVAL:
+            PVR_DPF((PVR_DBG_ERROR, "OSPCISuspendDev: can't enter requested power state"));
+            break;
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "OSPCISuspendDev: pci_set_power_state failed (%d)", err));
+            break;
+    }
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSPCIResumeDev(PVRSRV_PCI_DEV_HANDLE hPVRPCI)
+{
+    PVR_PCI_DEV *psPVRPCI = (PVR_PCI_DEV *)hPVRPCI;
+    int err;
+
+    PVR_TRACE(("OSPCIResumeDev"));
+
+    err = pci_set_power_state(psPVRPCI->psPCIDev, pci_choose_state(psPVRPCI->psPCIDev, PMSG_ON));
+    switch(err)
+    {
+        case 0:
+            break;
+        case -EIO:
+            PVR_DPF((PVR_DBG_WARNING, "OSPCIResumeDev: device doesn't support PCI PM"));
+            break;
+        case -EINVAL:
+            PVR_DPF((PVR_DBG_ERROR, "OSPCIResumeDev: can't enter requested power state"));
+            return PVRSRV_ERROR_GENERIC;
+        default:
+            PVR_DPF((PVR_DBG_ERROR, "OSPCIResumeDev: pci_set_power_state failed (%d)", err));
+            return PVRSRV_ERROR_GENERIC;
+    }
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38))
+	pci_restore_state(psPVRPCI->psPCIDev);
+#else
+    err = pci_restore_state(psPVRPCI->psPCIDev);
+    if (err != 0)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCIResumeDev: pci_restore_state failed (%d)", err));
+        return PVRSRV_ERROR_GENERIC;
+    }
+#endif
+
+    err = pci_enable_device(psPVRPCI->psPCIDev);
+    if (err != 0)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSPCIResumeDev: Couldn't enable device (%d)", err));
+        return PVRSRV_ERROR_GENERIC;
+    }
+
+    if (psPVRPCI->ePCIFlags & HOST_PCI_INIT_FLAG_BUS_MASTER)
+        pci_set_master(psPVRPCI->psPCIDev);
+
+    return PVRSRV_OK;
+}
+
+#endif
+
+#define	OS_MAX_TIMERS	8
+
+typedef struct TIMER_CALLBACK_DATA_TAG
+{
+    IMG_BOOL			bInUse;
+    PFN_TIMER_FUNC		pfnTimerFunc;
+    IMG_VOID 			*pvData;
+    struct timer_list	sTimer;
+    IMG_UINT32			ui32Delay;
+    IMG_BOOL			bActive;
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+	struct work_struct	sWork;
+#endif
+}TIMER_CALLBACK_DATA;
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+static struct workqueue_struct	*psTimerWorkQueue;
+#endif
+
+static TIMER_CALLBACK_DATA sTimers[OS_MAX_TIMERS];
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+DEFINE_MUTEX(sTimerStructLock);
+#else
+DEFINE_SPINLOCK(sTimerStructLock);
+#endif
+
+static void OSTimerCallbackBody(TIMER_CALLBACK_DATA *psTimerCBData)
+{
+    if (!psTimerCBData->bActive)
+        return;
+
+
+    psTimerCBData->pfnTimerFunc(psTimerCBData->pvData);
+
+
+    mod_timer(&psTimerCBData->sTimer, psTimerCBData->ui32Delay + jiffies);
+}
+
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+static void OSTimerWorkQueueCallBack(struct work_struct *psWork)
+{
+    TIMER_CALLBACK_DATA *psTimerCBData = container_of(psWork, TIMER_CALLBACK_DATA, sWork);
+
+    OSTimerCallbackBody(psTimerCBData);
+}
+#endif
+
+static IMG_VOID OSTimerCallbackWrapper(IMG_UINT32 ui32Data)
+{
+    TIMER_CALLBACK_DATA	*psTimerCBData = (TIMER_CALLBACK_DATA*)ui32Data;
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+    int res;
+
+    res = queue_work(psTimerWorkQueue, &psTimerCBData->sWork);
+    if (res == 0)
+    {
+        PVR_DPF((PVR_DBG_WARNING, "OSTimerCallbackWrapper: work already queued"));
+    }
+#else
+    OSTimerCallbackBody(psTimerCBData);
+#endif
+}
+
+
+IMG_HANDLE OSAddTimer(PFN_TIMER_FUNC pfnTimerFunc, IMG_VOID *pvData, IMG_UINT32 ui32MsTimeout)
+{
+    TIMER_CALLBACK_DATA	*psTimerCBData;
+    IMG_UINT32		ui32i;
+#if !defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+    unsigned long		ulLockFlags;
+#endif
+
+
+    if(!pfnTimerFunc)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSAddTimer: passed invalid callback"));
+        return IMG_NULL;
+    }
+
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+    mutex_lock(&sTimerStructLock);
+#else
+    spin_lock_irqsave(&sTimerStructLock, ulLockFlags);
+#endif
+    for (ui32i = 0; ui32i < OS_MAX_TIMERS; ui32i++)
+    {
+        psTimerCBData = &sTimers[ui32i];
+        if (!psTimerCBData->bInUse)
+        {
+            psTimerCBData->bInUse = IMG_TRUE;
+            break;
+        }
+    }
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+    mutex_unlock(&sTimerStructLock);
+#else
+    spin_unlock_irqrestore(&sTimerStructLock, ulLockFlags);
+#endif
+    if (ui32i >= OS_MAX_TIMERS)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSAddTimer: all timers are in use"));
+        return IMG_NULL;
+    }
+
+    psTimerCBData->pfnTimerFunc = pfnTimerFunc;
+    psTimerCBData->pvData = pvData;
+    psTimerCBData->bActive = IMG_FALSE;
+
+
+
+
+    psTimerCBData->ui32Delay = ((HZ * ui32MsTimeout) < 1000)
+                                ?	1
+                                :	((HZ * ui32MsTimeout) / 1000);
+
+    init_timer(&psTimerCBData->sTimer);
+
+
+    psTimerCBData->sTimer.function = (IMG_VOID *)OSTimerCallbackWrapper;
+    psTimerCBData->sTimer.data = (IMG_UINT32)psTimerCBData;
+    psTimerCBData->sTimer.expires = psTimerCBData->ui32Delay + jiffies;
+
+    return (IMG_HANDLE)(ui32i + 1);
+}
+
+
+static inline TIMER_CALLBACK_DATA *GetTimerStructure(IMG_HANDLE hTimer)
+{
+    IMG_UINT32 ui32i = ((IMG_UINT32)hTimer) - 1;
+
+    PVR_ASSERT(ui32i < OS_MAX_TIMERS);
+
+    return &sTimers[ui32i];
+}
+
+PVRSRV_ERROR OSRemoveTimer (IMG_HANDLE hTimer)
+{
+    TIMER_CALLBACK_DATA *psTimerCBData = GetTimerStructure(hTimer);
+
+    PVR_ASSERT(psTimerCBData->bInUse);
+    PVR_ASSERT(!psTimerCBData->bActive);
+
+
+    psTimerCBData->bInUse = IMG_FALSE;
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSEnableTimer (IMG_HANDLE hTimer)
+{
+    TIMER_CALLBACK_DATA *psTimerCBData = GetTimerStructure(hTimer);
+
+    PVR_ASSERT(psTimerCBData->bInUse);
+    PVR_ASSERT(!psTimerCBData->bActive);
+
+
+    psTimerCBData->bActive = IMG_TRUE;
+
+
+    psTimerCBData->sTimer.expires = psTimerCBData->ui32Delay + jiffies;
+
+
+    add_timer(&psTimerCBData->sTimer);
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSDisableTimer (IMG_HANDLE hTimer)
+{
+    TIMER_CALLBACK_DATA *psTimerCBData = GetTimerStructure(hTimer);
+
+    PVR_ASSERT(psTimerCBData->bInUse);
+    PVR_ASSERT(psTimerCBData->bActive);
+
+
+    psTimerCBData->bActive = IMG_FALSE;
+	smp_mb();
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+	flush_workqueue(psTimerWorkQueue);
+#endif
+
+
+    del_timer_sync(&psTimerCBData->sTimer);
+
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+
+	flush_workqueue(psTimerWorkQueue);
+#endif
+
+    return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR OSEventObjectCreate(const IMG_CHAR *pszName, PVRSRV_EVENTOBJECT *psEventObject)
+{
+
+    PVRSRV_ERROR eError = PVRSRV_OK;
+
+    if(psEventObject)
+    {
+        if(pszName)
+        {
+
+            strncpy(psEventObject->szName, pszName, EVENTOBJNAME_MAXLENGTH);
+        }
+        else
+        {
+
+            static IMG_UINT16 ui16NameIndex = 0;
+            snprintf(psEventObject->szName, EVENTOBJNAME_MAXLENGTH, "PVRSRV_EVENTOBJECT_%d", ui16NameIndex++);
+        }
+
+        if(LinuxEventObjectListCreate(&psEventObject->hOSEventKM) != PVRSRV_OK)
+        {
+             eError = PVRSRV_ERROR_OUT_OF_MEMORY;
+        }
+
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSEventObjectCreate: psEventObject is not a valid pointer"));
+        eError = PVRSRV_ERROR_GENERIC;
+    }
+
+    return eError;
+
+}
+
+
+PVRSRV_ERROR OSEventObjectDestroy(PVRSRV_EVENTOBJECT *psEventObject)
+{
+    PVRSRV_ERROR eError = PVRSRV_OK;
+
+    if(psEventObject)
+    {
+        if(psEventObject->hOSEventKM)
+        {
+            LinuxEventObjectListDestroy(psEventObject->hOSEventKM);
+        }
+        else
+        {
+            PVR_DPF((PVR_DBG_ERROR, "OSEventObjectDestroy: hOSEventKM is not a valid pointer"));
+            eError = PVRSRV_ERROR_INVALID_PARAMS;
+        }
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSEventObjectDestroy: psEventObject is not a valid pointer"));
+        eError = PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+    return eError;
+}
+
+PVRSRV_ERROR OSEventObjectWait(IMG_HANDLE hOSEventKM)
+{
+    PVRSRV_ERROR eError;
+
+    if(hOSEventKM)
+    {
+        eError = LinuxEventObjectWait(hOSEventKM, EVENT_OBJECT_TIMEOUT_MS);
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSEventObjectWait: hOSEventKM is not a valid handle"));
+        eError = PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+    return eError;
+}
+
+PVRSRV_ERROR OSEventObjectOpen(PVRSRV_EVENTOBJECT *psEventObject,
+                                            IMG_HANDLE *phOSEvent)
+{
+    PVRSRV_ERROR eError = PVRSRV_OK;
+
+    if(psEventObject)
+    {
+        if(LinuxEventObjectAdd(psEventObject->hOSEventKM, phOSEvent) != PVRSRV_OK)
+        {
+            PVR_DPF((PVR_DBG_ERROR, "LinuxEventObjectAdd: failed"));
+            eError = PVRSRV_ERROR_INVALID_PARAMS;
+        }
+
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSEventObjectCreate: psEventObject is not a valid pointer"));
+        eError = PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+    return eError;
+}
+
+PVRSRV_ERROR OSEventObjectClose(PVRSRV_EVENTOBJECT *psEventObject,
+                                            IMG_HANDLE hOSEventKM)
+{
+    PVRSRV_ERROR eError = PVRSRV_OK;
+
+    if(psEventObject)
+    {
+        if(LinuxEventObjectDelete(psEventObject->hOSEventKM, hOSEventKM) != PVRSRV_OK)
+        {
+            PVR_DPF((PVR_DBG_ERROR, "LinuxEventObjectDelete: failed"));
+            eError = PVRSRV_ERROR_INVALID_PARAMS;
+        }
+
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSEventObjectDestroy: psEventObject is not a valid pointer"));
+        eError = PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+    return eError;
+
+}
+
+PVRSRV_ERROR OSEventObjectSignal(IMG_HANDLE hOSEventKM)
+{
+    PVRSRV_ERROR eError;
+
+    if(hOSEventKM)
+    {
+        eError = LinuxEventObjectSignal(hOSEventKM);
+    }
+    else
+    {
+        PVR_DPF((PVR_DBG_ERROR, "OSEventObjectSignal: hOSEventKM is not a valid handle"));
+        eError = PVRSRV_ERROR_INVALID_PARAMS;
+    }
+
+    return eError;
+}
+
+IMG_BOOL OSProcHasPrivSrvInit(IMG_VOID)
+{
+    return (capable(CAP_SYS_MODULE) != 0) ? IMG_TRUE : IMG_FALSE;
+}
+
+PVRSRV_ERROR OSCopyToUser(IMG_PVOID pvProcess,
+                          IMG_VOID *pvDest,
+                          IMG_VOID *pvSrc,
+                          IMG_UINT32 ui32Bytes)
+{
+    PVR_UNREFERENCED_PARAMETER(pvProcess);
+
+    if(copy_to_user(pvDest, pvSrc, ui32Bytes)==0)
+        return PVRSRV_OK;
+    else
+        return PVRSRV_ERROR_GENERIC;
+}
+
+PVRSRV_ERROR OSCopyFromUser( IMG_PVOID pvProcess,
+                             IMG_VOID *pvDest,
+                             IMG_VOID *pvSrc,
+                             IMG_UINT32 ui32Bytes)
+{
+    PVR_UNREFERENCED_PARAMETER(pvProcess);
+
+    if(copy_from_user(pvDest, pvSrc, ui32Bytes)==0)
+        return PVRSRV_OK;
+    else
+        return PVRSRV_ERROR_GENERIC;
+}
+
+IMG_BOOL OSAccessOK(IMG_VERIFY_TEST eVerification, IMG_VOID *pvUserPtr, IMG_UINT32 ui32Bytes)
+{
+    IMG_INT linuxType;
+
+    if (eVerification == PVR_VERIFY_READ)
+    {
+        linuxType = VERIFY_READ;
+    }
+    else
+    {
+        PVR_ASSERT(eVerification == PVR_VERIFY_WRITE);
+        linuxType = VERIFY_WRITE;
+    }
+
+    return access_ok(linuxType, pvUserPtr, ui32Bytes);
+}
+
+typedef enum _eWrapMemType_
+{
+    WRAP_TYPE_CLEANUP,
+    WRAP_TYPE_GET_USER_PAGES,
+    WRAP_TYPE_FIND_VMA_PAGES,
+    WRAP_TYPE_FIND_VMA_PFN
+} eWrapMemType;
+
+typedef struct _sWrapMemInfo_
+{
+    eWrapMemType eType;
+    IMG_INT iNumPages;
+    struct page **ppsPages;
+    IMG_SYS_PHYADDR *psPhysAddr;
+    IMG_INT iPageOffset;
+    IMG_INT iContiguous;
+#if defined(DEBUG)
+    IMG_UINT32 ulStartAddr;
+    IMG_UINT32 ulBeyondEndAddr;
+    struct vm_area_struct *psVMArea;
+#endif
+	IMG_BOOL bWrapWorkaround;
+} sWrapMemInfo;
+
+static IMG_VOID CheckPagesContiguous(sWrapMemInfo *psInfo)
+{
+    IMG_INT i;
+    IMG_UINT32 ui32AddrChk;
+
+    BUG_ON(psInfo == IMG_NULL);
+
+    psInfo->iContiguous = 1;
+
+    for (i = 0, ui32AddrChk = psInfo->psPhysAddr[0].uiAddr;
+        i < psInfo->iNumPages;
+        i++, ui32AddrChk += PAGE_SIZE)
+    {
+        if (psInfo->psPhysAddr[i].uiAddr != ui32AddrChk)
+        {
+            psInfo->iContiguous = 0;
+            break;
+        }
+    }
+}
+
+static struct page *CPUVAddrToPage(struct vm_area_struct *psVMArea, IMG_UINT32 ulCPUVAddr)
+{
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10))
+    pgd_t *psPGD;
+    pud_t *psPUD;
+    pmd_t *psPMD;
+    pte_t *psPTE;
+    struct mm_struct *psMM = psVMArea->vm_mm;
+    IMG_UINT32 ulPFN;
+    spinlock_t *psPTLock;
+    struct page *psPage;
+
+    psPGD = pgd_offset(psMM, ulCPUVAddr);
+    if (pgd_none(*psPGD) || pgd_bad(*psPGD))
+        return NULL;
+
+    psPUD = pud_offset(psPGD, ulCPUVAddr);
+    if (pud_none(*psPUD) || pud_bad(*psPUD))
+        return NULL;
+
+    psPMD = pmd_offset(psPUD, ulCPUVAddr);
+    if (pmd_none(*psPMD) || pmd_bad(*psPMD))
+        return NULL;
+
+    psPage = NULL;
+
+    psPTE = (pte_t *)pte_offset_map_lock(psMM, psPMD, ulCPUVAddr, &psPTLock);
+    if ((pte_none(*psPTE) != 0) || (pte_present(*psPTE) == 0) || (pte_write(*psPTE) == 0))
+        goto exit_unlock;
+
+    ulPFN = pte_pfn(*psPTE);
+    if (!pfn_valid(ulPFN))
+        goto exit_unlock;
+
+    psPage = pfn_to_page(ulPFN);
+
+    get_page(psPage);
+
+exit_unlock:
+    pte_unmap_unlock(psPTE, psPTLock);
+
+    return psPage;
+#else
+    return NULL;
+#endif
+}
+PVRSRV_ERROR OSReleasePhysPageAddr(IMG_HANDLE hOSWrapMem)
+{
+    sWrapMemInfo *psInfo = (sWrapMemInfo *)hOSWrapMem;
+    IMG_INT i;
+
+    BUG_ON(psInfo == IMG_NULL);
+
+    switch (psInfo->eType)
+    {
+        case WRAP_TYPE_CLEANUP:
+            break;
+        case WRAP_TYPE_FIND_VMA_PFN:
+            break;
+        case WRAP_TYPE_GET_USER_PAGES:
+        {
+            for (i = 0; i < psInfo->iNumPages; i++)
+            {
+                struct page *psPage = psInfo->ppsPages[i];
+
+
+                if (!PageReserved(psPage))
+                {
+                    SetPageDirty(psPage);
+                }
+                page_cache_release(psPage);
+            }
+            break;
+        }
+        case WRAP_TYPE_FIND_VMA_PAGES:
+        {
+            for (i = 0; i < psInfo->iNumPages; i++)
+            {
+                if(psInfo->bWrapWorkaround)
+                    put_page(psInfo->ppsPages[i]);
+                else
+                    put_page_testzero(psInfo->ppsPages[i]);
+            }
+            break;
+        }
+        default:
+        {
+            PVR_DPF((PVR_DBG_ERROR,
+                "OSReleasePhysPageAddr: Unknown wrap type (%d)", psInfo->eType));
+            return PVRSRV_ERROR_GENERIC;
+        }
+    }
+
+    if (psInfo->ppsPages != IMG_NULL)
+    {
+        kfree(psInfo->ppsPages);
+    }
+
+    if (psInfo->psPhysAddr != IMG_NULL)
+    {
+        kfree(psInfo->psPhysAddr);
+    }
+
+    kfree(psInfo);
+
+    return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSAcquirePhysPageAddr(IMG_VOID* pvCPUVAddr,
+                                    IMG_UINT32 ui32Bytes,
+                                    IMG_SYS_PHYADDR *psSysPAddr,
+                                    IMG_HANDLE *phOSWrapMem,
+                                    IMG_BOOL bWrapWorkaround)
+{
+    IMG_UINT32 ulStartAddrOrig = (IMG_UINT32) pvCPUVAddr;
+    IMG_UINT32 ulAddrRangeOrig = (IMG_UINT32) ui32Bytes;
+    IMG_UINT32 ulBeyondEndAddrOrig = ulStartAddrOrig + ulAddrRangeOrig;
+    IMG_UINT32 ulStartAddr;
+    IMG_UINT32 ulAddrRange;
+    IMG_UINT32 ulBeyondEndAddr;
+    IMG_UINT32 ulAddr;
+    IMG_INT iNumPagesMapped;
+    IMG_INT i;
+    struct vm_area_struct *psVMArea;
+    sWrapMemInfo *psInfo;
+
+
+    ulStartAddr = ulStartAddrOrig & PAGE_MASK;
+    ulBeyondEndAddr = PAGE_ALIGN(ulBeyondEndAddrOrig);
+    ulAddrRange = ulBeyondEndAddr - ulStartAddr;
+
+
+    psInfo = kmalloc(sizeof(*psInfo), GFP_KERNEL);
+    if (psInfo == NULL)
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: Couldn't allocate information structure"));
+        return PVRSRV_ERROR_OUT_OF_MEMORY;
+    }
+    memset(psInfo, 0, sizeof(*psInfo));
+    psInfo->bWrapWorkaround = bWrapWorkaround;
+
+#if defined(DEBUG)
+    psInfo->ulStartAddr = ulStartAddrOrig;
+    psInfo->ulBeyondEndAddr = ulBeyondEndAddrOrig;
+#endif
+
+    psInfo->iNumPages = (IMG_INT)(ulAddrRange >> PAGE_SHIFT);
+    psInfo->iPageOffset = (IMG_INT)(ulStartAddrOrig & ~PAGE_MASK);
+
+
+    psInfo->psPhysAddr = kmalloc((size_t)psInfo->iNumPages * sizeof(*psInfo->psPhysAddr), GFP_KERNEL);
+    if (psInfo->psPhysAddr == NULL)
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: Couldn't allocate page array"));
+        goto error_free;
+    }
+
+
+    psInfo->ppsPages = kmalloc((size_t)psInfo->iNumPages * sizeof(*psInfo->ppsPages),  GFP_KERNEL);
+    if (psInfo->ppsPages == NULL)
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: Couldn't allocate page array"));
+        goto error_free;
+    }
+
+
+    down_read(&current->mm->mmap_sem);
+    iNumPagesMapped = get_user_pages(current, current->mm, ulStartAddr, psInfo->iNumPages, 1, 0, psInfo->ppsPages, NULL);
+    up_read(&current->mm->mmap_sem);
+
+    if (iNumPagesMapped >= 0)
+    {
+
+        if (iNumPagesMapped != psInfo->iNumPages)
+        {
+            PVR_TRACE(("OSAcquirePhysPageAddr: Couldn't map all the pages needed (wanted: %d, got %d)", psInfo->iNumPages, iNumPagesMapped));
+
+
+            for (i = 0; i < iNumPagesMapped; i++)
+            {
+                page_cache_release(psInfo->ppsPages[i]);
+
+            }
+            goto error_free;
+        }
+
+
+        for (i = 0; i < psInfo->iNumPages; i++)
+        {
+            IMG_CPU_PHYADDR CPUPhysAddr;
+
+            CPUPhysAddr.uiAddr = page_to_pfn(psInfo->ppsPages[i]) << PAGE_SHIFT;
+            psInfo->psPhysAddr[i] = SysCpuPAddrToSysPAddr(CPUPhysAddr);
+            psSysPAddr[i] = psInfo->psPhysAddr[i];
+
+        }
+
+        psInfo->eType = WRAP_TYPE_GET_USER_PAGES;
+
+        goto exit_check;
+    }
+
+    PVR_DPF((PVR_DBG_MESSAGE, "OSAcquirePhysPageAddr: get_user_pages failed (%d), trying something else", iNumPagesMapped));
+
+
+    down_read(&current->mm->mmap_sem);
+
+    psVMArea = find_vma(current->mm, ulStartAddrOrig);
+    if (psVMArea == NULL)
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: Couldn't find memory region containing start address %lx", ulStartAddrOrig));
+
+        goto error_release_mmap_sem;
+    }
+#if defined(DEBUG)
+    psInfo->psVMArea = psVMArea;
+#endif
+
+
+    if (ulStartAddrOrig < psVMArea->vm_start)
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: Start address %lx is outside of the region returned by find_vma", ulStartAddrOrig));
+        goto error_release_mmap_sem;
+    }
+
+
+    if (ulBeyondEndAddrOrig > psVMArea->vm_end)
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: End address %lx is outside of the region returned by find_vma", ulBeyondEndAddrOrig));
+        goto error_release_mmap_sem;
+    }
+
+
+    if ((psVMArea->vm_flags & (VM_IO | VM_RESERVED)) != (VM_IO | VM_RESERVED))
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: Memory region does not represent memory mapped I/O (VMA flags: 0x%lx)", psVMArea->vm_flags));
+        goto error_release_mmap_sem;
+    }
+
+
+    if ((psVMArea->vm_flags & (VM_READ | VM_WRITE)) != (VM_READ | VM_WRITE))
+    {
+        PVR_DPF((PVR_DBG_ERROR,
+            "OSAcquirePhysPageAddr: No read/write access to memory region (VMA flags: 0x%lx)", psVMArea->vm_flags));
+        goto error_release_mmap_sem;
+    }
+
+
+    for (ulAddr = ulStartAddrOrig, i = 0; ulAddr < ulBeyondEndAddrOrig; ulAddr += PAGE_SIZE, i++)
+    {
+        struct page *psPage;
+
+        BUG_ON(i >= psInfo->iNumPages);
+
+        psPage = CPUVAddrToPage(psVMArea, ulAddr);
+        if (psPage == NULL)
+        {
+            IMG_INT j;
+
+        PVR_TRACE(("OSAcquirePhysPageAddr: Couldn't lookup page structure for address 0x%lx, trying something else", ulAddr));
+
+
+            for (j = 0; j < i; j++)
+            {
+                if(psInfo->bWrapWorkaround)
+                    put_page(psInfo->ppsPages[j]);
+                else
+                    put_page_testzero(psInfo->ppsPages[j]);
+            }
+            break;
+        }
+
+        psInfo->ppsPages[i] = psPage;
+    }
+
+    BUG_ON(i > psInfo->iNumPages);
+    if (i == psInfo->iNumPages)
+    {
+
+        for (i = 0; i < psInfo->iNumPages; i++)
+        {
+            struct page *psPage = psInfo->ppsPages[i];
+            IMG_CPU_PHYADDR CPUPhysAddr;
+
+
+            CPUPhysAddr.uiAddr = page_to_pfn(psPage) << PAGE_SHIFT;
+
+            psInfo->psPhysAddr[i] = SysCpuPAddrToSysPAddr(CPUPhysAddr);
+            psSysPAddr[i] = psInfo->psPhysAddr[i];
+        }
+
+        psInfo->eType = WRAP_TYPE_FIND_VMA_PAGES;
+    }
+    else
+    {
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10)) && defined(PVR_SECURE_HANDLES)
+
+
+
+        if ((psVMArea->vm_flags & VM_PFNMAP) == 0)
+        {
+        PVR_DPF((PVR_DBG_WARNING,
+            "OSAcquirePhysPageAddr: Region isn't a raw PFN mapping.  Giving up."));
+            goto error_release_mmap_sem;
+        }
+
+        for (ulAddr = ulStartAddrOrig, i = 0; ulAddr < ulBeyondEndAddrOrig; ulAddr += PAGE_SIZE, i++)
+        {
+            IMG_CPU_PHYADDR CPUPhysAddr;
+
+            CPUPhysAddr.uiAddr = ((ulAddr - psVMArea->vm_start) + (psVMArea->vm_pgoff << PAGE_SHIFT)) & PAGE_MASK;
+
+            psInfo->psPhysAddr[i] = SysCpuPAddrToSysPAddr(CPUPhysAddr);
+            psSysPAddr[i] = psInfo->psPhysAddr[i];
+        }
+        BUG_ON(i != psInfo->iNumPages);
+
+        psInfo->eType = WRAP_TYPE_FIND_VMA_PFN;
+
+
+        PVR_DPF((PVR_DBG_WARNING,
+            "OSAcquirePhysPageAddr: Region can't be locked down"));
+#else
+        PVR_DPF((PVR_DBG_WARNING,
+            "OSAcquirePhysPageAddr: Raw PFN mappings not supported.  Giving up."));
+        goto error_release_mmap_sem;
+#endif
+    }
+
+    up_read(&current->mm->mmap_sem);
+
+exit_check:
+    CheckPagesContiguous(psInfo);
+
+
+
+    *phOSWrapMem = (IMG_HANDLE)psInfo;
+
+    return PVRSRV_OK;
+
+error_release_mmap_sem:
+    up_read(&current->mm->mmap_sem);
+error_free:
+    psInfo->eType = WRAP_TYPE_CLEANUP;
+    OSReleasePhysPageAddr((IMG_HANDLE)psInfo);
+    return PVRSRV_ERROR_GENERIC;
+}
+
+PVRSRV_ERROR PVROSFuncInit(IMG_VOID)
+{
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+    {
+        IMG_UINT32 ui32i;
+
+        psTimerWorkQueue = create_workqueue("pvr_timer");
+        if (psTimerWorkQueue == NULL)
+        {
+	    PVR_DPF((PVR_DBG_ERROR, "%s: couldn't create timer workqueue", __FUNCTION__));
+	    return PVRSRV_ERROR_GENERIC;
+
+        }
+
+        for (ui32i = 0; ui32i < OS_MAX_TIMERS; ui32i++)
+        {
+            TIMER_CALLBACK_DATA *psTimerCBData = &sTimers[ui32i];
+
+	    INIT_WORK(&psTimerCBData->sWork, OSTimerWorkQueueCallBack);
+        }
+    }
+#endif
+    return PVRSRV_OK;
+}
+
+IMG_VOID PVROSFuncDeInit(IMG_VOID)
+{
+#if defined(PVR_LINUX_TIMERS_USING_WORKQUEUES)
+    if (psTimerWorkQueue != NULL)
+    {
+	destroy_workqueue(psTimerWorkQueue);
+    }
+#endif
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osperproc.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osperproc.c
new file mode 100644
index 0000000..6126ffd
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/osperproc.c
@@ -0,0 +1,109 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "osperproc.h"
+
+#include "env_perproc.h"
+#include "proc.h"
+
+extern IMG_UINT32 gui32ReleasePID;
+
+PVRSRV_ERROR OSPerProcessPrivateDataInit(IMG_HANDLE *phOsPrivateData)
+{
+	PVRSRV_ERROR eError;
+	IMG_HANDLE hBlockAlloc;
+	PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc;
+
+	eError = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				sizeof(PVRSRV_ENV_PER_PROCESS_DATA),
+				phOsPrivateData,
+				&hBlockAlloc,
+				"Environment per Process Data");
+
+	if (eError != PVRSRV_OK)
+	{
+		*phOsPrivateData = IMG_NULL;
+
+		PVR_DPF((PVR_DBG_ERROR, "%s: OSAllocMem failed (%d)", __FUNCTION__, eError));
+		return eError;
+	}
+
+	psEnvPerProc = (PVRSRV_ENV_PER_PROCESS_DATA *)*phOsPrivateData;
+	OSMemSet(psEnvPerProc, 0, sizeof(*psEnvPerProc));
+
+	psEnvPerProc->hBlockAlloc = hBlockAlloc;
+
+
+	LinuxMMapPerProcessConnect(psEnvPerProc);
+
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+
+	INIT_LIST_HEAD(&psEnvPerProc->sDRMAuthListHead);
+#endif
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSPerProcessPrivateDataDeInit(IMG_HANDLE hOsPrivateData)
+{
+	PVRSRV_ERROR eError;
+	PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc;
+
+	if (hOsPrivateData == IMG_NULL)
+	{
+		return PVRSRV_OK;
+	}
+
+	psEnvPerProc = (PVRSRV_ENV_PER_PROCESS_DATA *)hOsPrivateData;
+
+
+	LinuxMMapPerProcessDisconnect(psEnvPerProc);
+
+
+	RemovePerProcessProcDir(psEnvPerProc);
+
+	eError = OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
+				sizeof(PVRSRV_ENV_PER_PROCESS_DATA),
+				hOsPrivateData,
+				psEnvPerProc->hBlockAlloc);
+
+
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: OSFreeMem failed (%d)", __FUNCTION__, eError));
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR OSPerProcessSetHandleOptions(PVRSRV_HANDLE_BASE *psHandleBase)
+{
+	return LinuxMMapPerProcessHandleOptions(psHandleBase);
+}
+
+IMG_HANDLE LinuxTerminatingProcessPrivateData(IMG_VOID)
+{
+	if(!gui32ReleasePID)
+		return NULL;
+	return PVRSRVPerProcessPrivateData(gui32ReleasePID);
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pdump.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pdump.c
new file mode 100644
index 0000000..70a8892
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pdump.c
@@ -0,0 +1,658 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if defined (SUPPORT_SGX)
+#if defined (PDUMP)
+
+#include <asm/atomic.h>
+#include <stdarg.h>
+#include "sgxdefs.h"
+#include "services_headers.h"
+
+#include "pvrversion.h"
+#include "pvr_debug.h"
+
+#include "dbgdrvif.h"
+#include "sgxmmu.h"
+#include "mm.h"
+#include "pdump_km.h"
+
+#include <linux/tty.h>
+
+static IMG_BOOL PDumpWriteString2		(IMG_CHAR * pszString, IMG_UINT32 ui32Flags);
+static IMG_BOOL PDumpWriteILock			(PDBG_STREAM psStream, IMG_UINT8 *pui8Data, IMG_UINT32 ui32Count, IMG_UINT32 ui32Flags);
+static IMG_VOID DbgSetFrame				(PDBG_STREAM psStream, IMG_UINT32 ui32Frame);
+static IMG_UINT32 DbgGetFrame			(PDBG_STREAM psStream);
+static IMG_VOID DbgSetMarker			(PDBG_STREAM psStream, IMG_UINT32 ui32Marker);
+static IMG_UINT32 DbgWrite				(PDBG_STREAM psStream, IMG_UINT8 *pui8Data, IMG_UINT32 ui32BCount, IMG_UINT32 ui32Flags);
+
+#define PDUMP_DATAMASTER_PIXEL		(1)
+#define PDUMP_DATAMASTER_EDM		(3)
+
+#define MIN(a,b)       (a > b ? b : a)
+
+#define MAX_FILE_SIZE	0x40000000
+
+static atomic_t gsPDumpSuspended = ATOMIC_INIT(0);
+
+static PDBGKM_SERVICE_TABLE gpfnDbgDrv = IMG_NULL;
+
+
+
+IMG_CHAR *pszStreamName[PDUMP_NUM_STREAMS] = {	"ParamStream2",
+												"ScriptStream2",
+												"DriverInfoStream"};
+typedef struct PDBG_PDUMP_STATE_TAG
+{
+	PDBG_STREAM psStream[PDUMP_NUM_STREAMS];
+	IMG_UINT32 ui32ParamFileNum;
+
+	IMG_CHAR *pszMsg;
+	IMG_CHAR *pszScript;
+	IMG_CHAR *pszFile;
+
+} PDBG_PDUMP_STATE;
+
+static PDBG_PDUMP_STATE gsDBGPdumpState = {{IMG_NULL}, 0, IMG_NULL, IMG_NULL, IMG_NULL};
+
+#define SZ_MSG_SIZE_MAX			PVRSRV_PDUMP_MAX_COMMENT_SIZE-1
+#define SZ_SCRIPT_SIZE_MAX		PVRSRV_PDUMP_MAX_COMMENT_SIZE-1
+#define SZ_FILENAME_SIZE_MAX	PVRSRV_PDUMP_MAX_COMMENT_SIZE-1
+
+
+
+
+IMG_VOID DBGDrvGetServiceTable(IMG_VOID **fn_table);
+
+static inline IMG_BOOL PDumpSuspended(IMG_VOID)
+{
+	return atomic_read(&gsPDumpSuspended) != 0;
+}
+
+PVRSRV_ERROR PDumpOSGetScriptString(IMG_HANDLE *phScript,
+									IMG_UINT32 *pui32MaxLen)
+{
+	*phScript = (IMG_HANDLE)gsDBGPdumpState.pszScript;
+	*pui32MaxLen = SZ_SCRIPT_SIZE_MAX;
+	if ((!*phScript) || PDumpSuspended())
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpOSGetMessageString(IMG_HANDLE *phMsg,
+									 IMG_UINT32 *pui32MaxLen)
+{
+	*phMsg = (IMG_HANDLE)gsDBGPdumpState.pszMsg;
+	*pui32MaxLen = SZ_MSG_SIZE_MAX;
+	if ((!*phMsg) || PDumpSuspended())
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpOSGetFilenameString(IMG_CHAR **ppszFile,
+									 IMG_UINT32 *pui32MaxLen)
+{
+	*ppszFile = gsDBGPdumpState.pszFile;
+	*pui32MaxLen = SZ_FILENAME_SIZE_MAX;
+	if ((!*ppszFile) || PDumpSuspended())
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+	return PVRSRV_OK;
+}
+
+IMG_BOOL PDumpOSWriteString2(IMG_HANDLE hScript, IMG_UINT32 ui32Flags)
+{
+	return PDumpWriteString2(hScript, ui32Flags);
+}
+
+PVRSRV_ERROR PDumpOSBufprintf(IMG_HANDLE hBuf, IMG_UINT32 ui32ScriptSizeMax, IMG_CHAR* pszFormat, ...)
+{
+	IMG_CHAR* pszBuf = hBuf;
+	IMG_UINT32 n;
+	va_list	vaArgs;
+
+	va_start(vaArgs, pszFormat);
+
+	n = vsnprintf(pszBuf, ui32ScriptSizeMax, pszFormat, vaArgs);
+
+	va_end(vaArgs);
+
+	if (n>=ui32ScriptSizeMax || n==-1)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "Buffer overflow detected, pdump output may be incomplete."));
+
+		return PVRSRV_ERROR_PDUMP_BUF_OVERFLOW;
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpOSVSprintf(IMG_CHAR *pszComment, IMG_UINT32 ui32ScriptSizeMax, IMG_CHAR* pszFormat, PDUMP_va_list vaArgs)
+{
+	IMG_UINT32 n;
+
+	n = vsnprintf(pszComment, ui32ScriptSizeMax, pszFormat, vaArgs);
+
+	if (n>=ui32ScriptSizeMax || n==-1)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "Buffer overflow detected, pdump output may be incomplete."));
+
+		return PVRSRV_ERROR_PDUMP_BUF_OVERFLOW;
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_VOID PDumpOSDebugPrintf(IMG_CHAR* pszFormat, ...)
+{
+
+}
+
+PVRSRV_ERROR PDumpOSSprintf(IMG_CHAR *pszComment, IMG_UINT32 ui32ScriptSizeMax, IMG_CHAR *pszFormat, ...)
+{
+	IMG_UINT32 n;
+	va_list	vaArgs;
+
+	va_start(vaArgs, pszFormat);
+
+	n = vsnprintf(pszComment, ui32ScriptSizeMax, pszFormat, vaArgs);
+
+	va_end(vaArgs);
+
+	if (n>=ui32ScriptSizeMax || n==-1)
+	{
+		PVR_DPF((PVR_DBG_ERROR, "Buffer overflow detected, pdump output may be incomplete."));
+
+		return PVRSRV_ERROR_PDUMP_BUF_OVERFLOW;
+	}
+
+	return PVRSRV_OK;
+}
+
+IMG_UINT32 PDumpOSBuflen(IMG_HANDLE hBuffer, IMG_UINT32 ui32BufferSizeMax)
+{
+	IMG_CHAR* pszBuf = hBuffer;
+	IMG_UINT32 ui32Count = 0;
+
+	while ((pszBuf[ui32Count]!=0) && (ui32Count<ui32BufferSizeMax) )
+	{
+		ui32Count++;
+	}
+	return(ui32Count);
+}
+
+IMG_VOID PDumpOSVerifyLineEnding(IMG_HANDLE hBuffer, IMG_UINT32 ui32BufferSizeMax)
+{
+	IMG_UINT32 ui32Count = 0;
+	IMG_CHAR* pszBuf = hBuffer;
+
+
+	ui32Count = PDumpOSBuflen(hBuffer, ui32BufferSizeMax);
+
+
+	if ((ui32Count >= 1) && (pszBuf[ui32Count-1] != '\n') && (ui32Count<ui32BufferSizeMax))
+	{
+		pszBuf[ui32Count] = '\n';
+		ui32Count++;
+		pszBuf[ui32Count] = '\0';
+	}
+	if ((ui32Count >= 2) && (pszBuf[ui32Count-2] != '\r') && (ui32Count<ui32BufferSizeMax))
+	{
+		pszBuf[ui32Count-1] = '\r';
+		pszBuf[ui32Count] = '\n';
+		ui32Count++;
+		pszBuf[ui32Count] = '\0';
+	}
+}
+
+IMG_HANDLE PDumpOSGetStream(IMG_UINT32 ePDumpStream)
+{
+	return (IMG_HANDLE)gsDBGPdumpState.psStream[ePDumpStream];
+}
+
+IMG_UINT32 PDumpOSGetStreamOffset(IMG_UINT32 ePDumpStream)
+{
+	PDBG_STREAM psStream = gsDBGPdumpState.psStream[ePDumpStream];
+	return gpfnDbgDrv->pfnGetStreamOffset(psStream);
+}
+
+IMG_UINT32 PDumpOSGetParamFileNum(IMG_VOID)
+{
+	return gsDBGPdumpState.ui32ParamFileNum;
+}
+
+IMG_BOOL PDumpOSWriteString(IMG_HANDLE hStream,
+		IMG_UINT8 *psui8Data,
+		IMG_UINT32 ui32Size,
+		IMG_UINT32 ui32Flags)
+{
+	PDBG_STREAM psStream = (PDBG_STREAM)hStream;
+	return PDumpWriteILock(psStream,
+					psui8Data,
+					ui32Size,
+					ui32Flags);
+}
+
+IMG_VOID PDumpOSCheckForSplitting(IMG_HANDLE hStream, IMG_UINT32 ui32Size, IMG_UINT32 ui32Flags)
+{
+
+	PVR_UNREFERENCED_PARAMETER(hStream);
+	PVR_UNREFERENCED_PARAMETER(ui32Size);
+	PVR_UNREFERENCED_PARAMETER(ui32Size);
+}
+
+IMG_BOOL PDumpOSJTInitialised(IMG_VOID)
+{
+	if(gpfnDbgDrv)
+	{
+		return IMG_TRUE;
+	}
+	return IMG_FALSE;
+}
+
+inline IMG_BOOL PDumpOSIsSuspended(IMG_VOID)
+{
+	return atomic_read(&gsPDumpSuspended) != 0;
+}
+
+IMG_VOID PDumpOSCPUVAddrToDevPAddr(PVRSRV_DEVICE_TYPE eDeviceType,
+        IMG_HANDLE hOSMemHandle,
+		IMG_UINT32 ui32Offset,
+		IMG_UINT8 *pui8LinAddr,
+		IMG_UINT32 ui32PageSize,
+		IMG_DEV_PHYADDR *psDevPAddr)
+{
+	if(hOSMemHandle)
+	{
+
+		IMG_CPU_PHYADDR	sCpuPAddr;
+
+		PVR_UNREFERENCED_PARAMETER(pui8LinAddr);
+
+		sCpuPAddr = OSMemHandleToCpuPAddr(hOSMemHandle, ui32Offset);
+		PVR_ASSERT((sCpuPAddr.uiAddr & (ui32PageSize - 1)) == 0);
+
+
+		*psDevPAddr = SysCpuPAddrToDevPAddr(eDeviceType, sCpuPAddr);
+	}
+	else
+	{
+		IMG_CPU_PHYADDR	sCpuPAddr;
+
+		PVR_UNREFERENCED_PARAMETER(ui32Offset);
+
+		sCpuPAddr = OSMapLinToCPUPhys(pui8LinAddr);
+		*psDevPAddr = SysCpuPAddrToDevPAddr(eDeviceType, sCpuPAddr);
+	}
+}
+
+IMG_VOID PDumpOSCPUVAddrToPhysPages(IMG_HANDLE hOSMemHandle,
+		IMG_UINT32 ui32Offset,
+		IMG_PUINT8 pui8LinAddr,
+		IMG_UINT32 *pui32PageOffset)
+{
+	if(hOSMemHandle)
+	{
+
+		IMG_CPU_PHYADDR     sCpuPAddr;
+
+		PVR_UNREFERENCED_PARAMETER(pui8LinAddr);
+
+		sCpuPAddr = OSMemHandleToCpuPAddr(hOSMemHandle, ui32Offset);
+	    *pui32PageOffset = sCpuPAddr.uiAddr & (HOST_PAGESIZE() -1);
+	}
+	else
+	{
+		PVR_UNREFERENCED_PARAMETER(hOSMemHandle);
+		PVR_UNREFERENCED_PARAMETER(ui32Offset);
+
+		*pui32PageOffset = (IMG_UINT32)pui8LinAddr & (HOST_PAGESIZE() - 1);
+	}
+}
+
+
+
+IMG_VOID PDumpInit(IMG_VOID)
+{
+	IMG_UINT32 i;
+
+
+	if (!gpfnDbgDrv)
+	{
+		DBGDrvGetServiceTable((IMG_VOID **)&gpfnDbgDrv);
+
+
+
+
+		if (gpfnDbgDrv == IMG_NULL)
+		{
+			return;
+		}
+
+		if(!gsDBGPdumpState.pszFile)
+		{
+			if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_FILENAME_SIZE_MAX, (IMG_PVOID *)&gsDBGPdumpState.pszFile, 0,
+				"Filename string") != PVRSRV_OK)
+			{
+				goto init_failed;
+			}
+		}
+
+		if(!gsDBGPdumpState.pszMsg)
+		{
+			if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_MSG_SIZE_MAX, (IMG_PVOID *)&gsDBGPdumpState.pszMsg, 0,
+				"Message string") != PVRSRV_OK)
+			{
+				goto init_failed;
+			}
+		}
+
+		if(!gsDBGPdumpState.pszScript)
+		{
+			if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_SCRIPT_SIZE_MAX, (IMG_PVOID *)&gsDBGPdumpState.pszScript, 0,
+				"Script string") != PVRSRV_OK)
+			{
+				goto init_failed;
+			}
+		}
+
+		for(i=0; i < PDUMP_NUM_STREAMS; i++)
+		{
+			gsDBGPdumpState.psStream[i] = gpfnDbgDrv->pfnCreateStream(pszStreamName[i],
+														DEBUG_CAPMODE_FRAMED,
+														DEBUG_OUTMODE_STREAMENABLE,
+														0,
+														10);
+
+			gpfnDbgDrv->pfnSetCaptureMode(gsDBGPdumpState.psStream[i],DEBUG_CAPMODE_FRAMED,0xFFFFFFFF, 0xFFFFFFFF, 1);
+			gpfnDbgDrv->pfnSetFrame(gsDBGPdumpState.psStream[i],0);
+		}
+
+		PDUMPCOMMENT("Driver Product Name: %s", VS_PRODUCT_NAME);
+		PDUMPCOMMENT("Driver Product Version: %s (%s)", PVRVERSION_STRING, PVRVERSION_FILE);
+		PDUMPCOMMENT("Start of Init Phase");
+	}
+
+	return;
+
+init_failed:
+
+	if(gsDBGPdumpState.pszFile)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_FILENAME_SIZE_MAX, (IMG_PVOID) gsDBGPdumpState.pszFile, 0);
+		gsDBGPdumpState.pszFile = IMG_NULL;
+	}
+
+	if(gsDBGPdumpState.pszScript)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_SCRIPT_SIZE_MAX, (IMG_PVOID) gsDBGPdumpState.pszScript, 0);
+		gsDBGPdumpState.pszScript = IMG_NULL;
+	}
+
+	if(gsDBGPdumpState.pszMsg)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_MSG_SIZE_MAX, (IMG_PVOID) gsDBGPdumpState.pszMsg, 0);
+		gsDBGPdumpState.pszMsg = IMG_NULL;
+	}
+
+	gpfnDbgDrv = IMG_NULL;
+}
+
+
+IMG_VOID PDumpDeInit(IMG_VOID)
+{
+	IMG_UINT32 i;
+
+	for(i=0; i < PDUMP_NUM_STREAMS; i++)
+	{
+		gpfnDbgDrv->pfnDestroyStream(gsDBGPdumpState.psStream[i]);
+	}
+
+	if(gsDBGPdumpState.pszFile)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_FILENAME_SIZE_MAX, (IMG_PVOID) gsDBGPdumpState.pszFile, 0);
+		gsDBGPdumpState.pszFile = IMG_NULL;
+	}
+
+	if(gsDBGPdumpState.pszScript)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_SCRIPT_SIZE_MAX, (IMG_PVOID) gsDBGPdumpState.pszScript, 0);
+		gsDBGPdumpState.pszScript = IMG_NULL;
+	}
+
+	if(gsDBGPdumpState.pszMsg)
+	{
+		OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, SZ_MSG_SIZE_MAX, (IMG_PVOID) gsDBGPdumpState.pszMsg, 0);
+		gsDBGPdumpState.pszMsg = IMG_NULL;
+	}
+
+	gpfnDbgDrv = IMG_NULL;
+}
+
+PVRSRV_ERROR PDumpStartInitPhaseKM(IMG_VOID)
+{
+	IMG_UINT32 i;
+
+	if (gpfnDbgDrv)
+	{
+		PDUMPCOMMENT("Start Init Phase");
+		for(i=0; i < PDUMP_NUM_STREAMS; i++)
+		{
+			gpfnDbgDrv->pfnStartInitPhase(gsDBGPdumpState.psStream[i]);
+		}
+	}
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpStopInitPhaseKM(IMG_VOID)
+{
+	IMG_UINT32 i;
+
+	if (gpfnDbgDrv)
+	{
+		PDUMPCOMMENT("Stop Init Phase");
+
+		for(i=0; i < PDUMP_NUM_STREAMS; i++)
+		{
+			gpfnDbgDrv->pfnStopInitPhase(gsDBGPdumpState.psStream[i]);
+		}
+	}
+	return PVRSRV_OK;
+}
+
+IMG_BOOL PDumpIsLastCaptureFrameKM(IMG_VOID)
+{
+	return gpfnDbgDrv->pfnIsLastCaptureFrame(gsDBGPdumpState.psStream[PDUMP_STREAM_SCRIPT2]);
+}
+
+
+IMG_BOOL PDumpIsCaptureFrameKM(IMG_VOID)
+{
+	if (PDumpSuspended())
+	{
+		return IMG_FALSE;
+	}
+	return gpfnDbgDrv->pfnIsCaptureFrame(gsDBGPdumpState.psStream[PDUMP_STREAM_SCRIPT2], IMG_FALSE);
+}
+
+PVRSRV_ERROR PDumpSetFrameKM(IMG_UINT32 ui32Frame)
+{
+	IMG_UINT32	ui32Stream;
+
+	for	(ui32Stream = 0; ui32Stream < PDUMP_NUM_STREAMS; ui32Stream++)
+	{
+		if	(gsDBGPdumpState.psStream[ui32Stream])
+		{
+			DbgSetFrame(gsDBGPdumpState.psStream[ui32Stream], ui32Frame);
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+PVRSRV_ERROR PDumpGetFrameKM(IMG_PUINT32 pui32Frame)
+{
+	*pui32Frame = DbgGetFrame(gsDBGPdumpState.psStream[PDUMP_STREAM_SCRIPT2]);
+
+	return PVRSRV_OK;
+}
+
+
+
+static IMG_BOOL PDumpWriteString2(IMG_CHAR * pszString, IMG_UINT32 ui32Flags)
+{
+	return PDumpWriteILock(gsDBGPdumpState.psStream[PDUMP_STREAM_SCRIPT2], (IMG_UINT8 *) pszString, strlen(pszString), ui32Flags);
+}
+
+
+static IMG_BOOL PDumpWriteILock(PDBG_STREAM psStream, IMG_UINT8 *pui8Data, IMG_UINT32 ui32Count, IMG_UINT32 ui32Flags)
+{
+	IMG_UINT32 ui32Written = 0;
+	IMG_UINT32 ui32Off = 0;
+
+	if ((psStream == IMG_NULL) || PDumpSuspended() || ((ui32Flags & PDUMP_FLAGS_NEVER) != 0))
+	{
+		return IMG_TRUE;
+	}
+
+
+
+
+	if (psStream == gsDBGPdumpState.psStream[PDUMP_STREAM_PARAM2])
+	{
+		IMG_UINT32 ui32ParamOutPos = gpfnDbgDrv->pfnGetStreamOffset(gsDBGPdumpState.psStream[PDUMP_STREAM_PARAM2]);
+
+		if (ui32ParamOutPos + ui32Count > MAX_FILE_SIZE)
+		{
+			if ((gsDBGPdumpState.psStream[PDUMP_STREAM_SCRIPT2] && PDumpWriteString2("\r\n-- Splitting pdump output file\r\n\r\n", ui32Flags)))
+			{
+				DbgSetMarker(gsDBGPdumpState.psStream[PDUMP_STREAM_PARAM2], ui32ParamOutPos);
+				gsDBGPdumpState.ui32ParamFileNum++;
+			}
+		}
+	}
+
+
+	while (((IMG_UINT32) ui32Count > 0) && (ui32Written != 0xFFFFFFFF))
+	{
+		ui32Written = DbgWrite(psStream, &pui8Data[ui32Off], ui32Count, ui32Flags);
+
+
+
+
+		if (ui32Written == 0)
+		{
+			OSReleaseThreadQuanta();
+		}
+
+		if (ui32Written != 0xFFFFFFFF)
+		{
+			ui32Off += ui32Written;
+			ui32Count -= ui32Written;
+		}
+	}
+
+	if (ui32Written == 0xFFFFFFFF)
+	{
+		return IMG_FALSE;
+	}
+
+	return IMG_TRUE;
+}
+
+static IMG_VOID DbgSetFrame(PDBG_STREAM psStream, IMG_UINT32 ui32Frame)
+{
+	gpfnDbgDrv->pfnSetFrame(psStream, ui32Frame);
+}
+
+
+static IMG_UINT32 DbgGetFrame(PDBG_STREAM psStream)
+{
+	return gpfnDbgDrv->pfnGetFrame(psStream);
+}
+
+static IMG_VOID DbgSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker)
+{
+	gpfnDbgDrv->pfnSetMarker(psStream, ui32Marker);
+}
+
+static IMG_UINT32 DbgWrite(PDBG_STREAM psStream, IMG_UINT8 *pui8Data, IMG_UINT32 ui32BCount, IMG_UINT32 ui32Flags)
+{
+	IMG_UINT32	ui32BytesWritten;
+
+	if ((ui32Flags & PDUMP_FLAGS_CONTINUOUS) != 0)
+	{
+
+
+		if (((psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED) != 0) &&
+		    (psStream->ui32Start == 0xFFFFFFFFUL) &&
+		    (psStream->ui32End == 0xFFFFFFFFUL) &&
+		     psStream->bInitPhaseComplete)
+		{
+			ui32BytesWritten = ui32BCount;
+		}
+		else
+		{
+			ui32BytesWritten = gpfnDbgDrv->pfnDBGDrivWrite2(psStream, pui8Data, ui32BCount, 1);
+		}
+	}
+	else
+	{
+		if (ui32Flags & PDUMP_FLAGS_LASTFRAME)
+		{
+			IMG_UINT32	ui32DbgFlags;
+
+			ui32DbgFlags = 0;
+			if (ui32Flags & PDUMP_FLAGS_RESETLFBUFFER)
+			{
+				ui32DbgFlags |= WRITELF_FLAGS_RESETBUF;
+			}
+
+			ui32BytesWritten = gpfnDbgDrv->pfnWriteLF(psStream, pui8Data, ui32BCount, 1, ui32DbgFlags);
+		}
+		else
+		{
+			ui32BytesWritten = gpfnDbgDrv->pfnWriteBINCM(psStream, pui8Data, ui32BCount, 1);
+		}
+	}
+
+	return ui32BytesWritten;
+}
+
+
+IMG_VOID PDumpSuspendKM(IMG_VOID)
+{
+	atomic_inc(&gsPDumpSuspended);
+}
+
+IMG_VOID PDumpResumeKM(IMG_VOID)
+{
+	atomic_dec(&gsPDumpSuspended);
+}
+
+#endif
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/private_data.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/private_data.h
new file mode 100644
index 0000000..10a0201
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/private_data.h
@@ -0,0 +1,63 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __INCLUDED_PRIVATE_DATA_H_
+#define __INCLUDED_PRIVATE_DATA_H_
+
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+#include <linux/list.h>
+#include <drm/drmP.h>
+#endif
+
+typedef struct
+{
+
+	IMG_UINT32 ui32OpenPID;
+
+#if defined(PVR_SECURE_FD_EXPORT)
+
+	IMG_HANDLE hKernelMemInfo;
+#endif
+
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+
+	struct list_head sDRMAuthListItem;
+
+	struct drm_file *psDRMFile;
+#endif
+
+#if defined(SUPPORT_MEMINFO_IDS)
+
+	IMG_UINT64 ui64Stamp;
+#endif
+
+
+	IMG_HANDLE hBlockAlloc;
+
+#if defined(SUPPORT_DRI_DRM_EXT)
+	IMG_PVOID pPriv;
+#endif
+}
+PVRSRV_FILE_PRIVATE_DATA;
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.c
new file mode 100644
index 0000000..50245a1
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.c
@@ -0,0 +1,962 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/fs.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+
+#include "services_headers.h"
+
+#include "queue.h"
+#include "resman.h"
+#include "pvrmmap.h"
+#include "pvr_debug.h"
+#include "pvrversion.h"
+#include "proc.h"
+#include "perproc.h"
+#include "env_perproc.h"
+#include "linkage.h"
+
+#include "lists.h"
+DECLARE_LIST_ANY_VA(PVRSRV_DEVICE_NODE);
+
+
+static struct proc_dir_entry * dir;
+
+#ifndef PVR_PROC_USE_SEQ_FILE
+static off_t procDumpSysNodes(IMG_CHAR *buf, size_t size, off_t off);
+static off_t procDumpVersion(IMG_CHAR *buf, size_t size, off_t off);
+#endif
+
+
+static const IMG_CHAR PVRProcDirRoot[] = "pvr";
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+#define PVR_PROC_SEQ_START_TOKEN (void*)1
+static IMG_INT pvr_proc_open(struct inode *inode,struct file *file);
+static void *pvr_proc_seq_start (struct seq_file *m, loff_t *pos);
+static void pvr_proc_seq_stop (struct seq_file *m, void *v);
+static void *pvr_proc_seq_next (struct seq_file *m, void *v, loff_t *pos);
+static int pvr_proc_seq_show (struct seq_file *m, void *v);
+static ssize_t pvr_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
+
+static struct file_operations pvr_proc_operations =
+{
+	.open		= pvr_proc_open,
+	.read		= seq_read,
+	.write		= pvr_proc_write,
+	.llseek		= seq_lseek,
+	.release	= seq_release,
+};
+
+static struct seq_operations pvr_proc_seq_operations =
+{
+	.start =	pvr_proc_seq_start,
+	.next =		pvr_proc_seq_next,
+	.stop =		pvr_proc_seq_stop,
+	.show =		pvr_proc_seq_show,
+};
+
+static struct proc_dir_entry* g_pProcQueue;
+static struct proc_dir_entry* g_pProcVersion;
+static struct proc_dir_entry* g_pProcSysNodes;
+
+#ifdef DEBUG
+static struct proc_dir_entry* g_pProcDebugLevel;
+#endif
+
+#ifdef PVR_MANUAL_POWER_CONTROL
+static struct proc_dir_entry* g_pProcPowerLevel;
+#endif
+
+
+static void ProcSeqShowVersion(struct seq_file *sfile,void* el);
+
+static void ProcSeqShowSysNodes(struct seq_file *sfile,void* el);
+static void* ProcSeqOff2ElementSysNodes(struct seq_file * sfile, loff_t off);
+
+#endif
+
+off_t printAppend(IMG_CHAR * buffer, size_t size, off_t off, const IMG_CHAR * format, ...)
+{
+    IMG_INT n;
+    size_t space = size - (size_t)off;
+    va_list ap;
+
+    PVR_ASSERT(space >= 0);
+
+    va_start (ap, format);
+
+    n = vsnprintf (buffer+off, space, format, ap);
+
+    va_end (ap);
+
+    if (n >= (IMG_INT)space || n < 0)
+    {
+
+        buffer[size - 1] = 0;
+        return (off_t)(size - 1);
+    }
+    else
+    {
+        return (off + (off_t)n);
+    }
+}
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+void* ProcSeq1ElementOff2Element(struct seq_file *sfile, loff_t off)
+{
+
+	if(!off)
+		return (void*)2;
+	return NULL;
+}
+
+
+void* ProcSeq1ElementHeaderOff2Element(struct seq_file *sfile, loff_t off)
+{
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+
+	if(off == 1)
+		return (void*)2;
+
+	return NULL;
+}
+
+
+static IMG_INT pvr_proc_open(struct inode *inode,struct file *file)
+{
+	IMG_INT ret = seq_open(file, &pvr_proc_seq_operations);
+
+	struct seq_file *seq = (struct seq_file*)file->private_data;
+	struct proc_dir_entry* pvr_proc_entry = PDE(inode);
+
+
+	seq->private = pvr_proc_entry->data;
+	return ret;
+}
+
+static ssize_t pvr_proc_write(struct file *file, const char __user *buffer,
+		size_t count, loff_t *ppos)
+{
+	struct inode *inode = file->f_path.dentry->d_inode;
+	struct proc_dir_entry * dp;
+
+	dp = PDE(inode);
+
+	if (!dp->write_proc)
+		return -EIO;
+
+	return dp->write_proc(file, buffer, count, dp->data);
+}
+
+
+static void *pvr_proc_seq_start (struct seq_file *proc_seq_file, loff_t *pos)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)proc_seq_file->private;
+	if(handlers->startstop != NULL)
+		handlers->startstop(proc_seq_file, IMG_TRUE);
+	return handlers->off2element(proc_seq_file, *pos);
+}
+
+static void pvr_proc_seq_stop (struct seq_file *proc_seq_file, void *v)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)proc_seq_file->private;
+	if(handlers->startstop != NULL)
+		handlers->startstop(proc_seq_file, IMG_FALSE);
+}
+
+static void *pvr_proc_seq_next (struct seq_file *proc_seq_file, void *v, loff_t *pos)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)proc_seq_file->private;
+	(*pos)++;
+	if( handlers->next != NULL)
+		return handlers->next( proc_seq_file, v, *pos );
+	return handlers->off2element(proc_seq_file, *pos);
+}
+
+static int pvr_proc_seq_show (struct seq_file *proc_seq_file, void *v)
+{
+	PVR_PROC_SEQ_HANDLERS *handlers = (PVR_PROC_SEQ_HANDLERS*)proc_seq_file->private;
+	handlers->show( proc_seq_file,v );
+    return 0;
+}
+
+
+
+static struct proc_dir_entry* CreateProcEntryInDirSeq(
+									   struct proc_dir_entry *pdir,
+									   const IMG_CHAR * name,
+								   IMG_VOID* data,
+									   pvr_next_proc_seq_t next_handler,
+									   pvr_show_proc_seq_t show_handler,
+									   pvr_off2element_proc_seq_t off2element_handler,
+									   pvr_startstop_proc_seq_t startstop_handler,
+									   write_proc_t whandler
+									   )
+{
+
+    struct proc_dir_entry * file;
+	mode_t mode;
+
+    if (!dir)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcEntryInDirSeq: cannot make proc entry /proc/%s/%s: no parent", PVRProcDirRoot, name));
+        return NULL;
+    }
+
+	mode = S_IFREG;
+
+    if (show_handler)
+    {
+		mode |= S_IRUGO;
+    }
+
+    if (whandler)
+    {
+		mode |= S_IWUSR;
+    }
+
+	file=create_proc_entry(name, mode, pdir);
+
+    if (file)
+    {
+		PVR_PROC_SEQ_HANDLERS *seq_handlers;
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
+        file->owner = THIS_MODULE;
+#endif
+
+		file->proc_fops = &pvr_proc_operations;
+		file->write_proc = whandler;
+
+
+		file->data =  kmalloc(sizeof(PVR_PROC_SEQ_HANDLERS), GFP_KERNEL);
+		if(file->data)
+		{
+			seq_handlers = (PVR_PROC_SEQ_HANDLERS*)file->data;
+			seq_handlers->next = next_handler;
+			seq_handlers->show = show_handler;
+			seq_handlers->off2element = off2element_handler;
+			seq_handlers->startstop = startstop_handler;
+			seq_handlers->data = data;
+
+	return file;
+		}
+    }
+
+    PVR_DPF((PVR_DBG_ERROR, "CreateProcEntryInDirSeq: cannot make proc entry /proc/%s/%s: no memory", PVRProcDirRoot, name));
+    return 0;
+}
+
+
+struct proc_dir_entry* CreateProcReadEntrySeq (
+								const IMG_CHAR * name,
+								IMG_VOID* data,
+								pvr_next_proc_seq_t next_handler,
+								pvr_show_proc_seq_t show_handler,
+								pvr_off2element_proc_seq_t off2element_handler,
+								pvr_startstop_proc_seq_t startstop_handler
+							   )
+{
+	return CreateProcEntrySeq(name,
+							  data,
+							  next_handler,
+							  show_handler,
+							  off2element_handler,
+							  startstop_handler,
+							  NULL);
+}
+
+struct proc_dir_entry* CreateProcEntrySeq (
+											const IMG_CHAR * name,
+											IMG_VOID* data,
+											pvr_next_proc_seq_t next_handler,
+											pvr_show_proc_seq_t show_handler,
+											pvr_off2element_proc_seq_t off2element_handler,
+											pvr_startstop_proc_seq_t startstop_handler,
+											write_proc_t whandler
+										  )
+{
+	return CreateProcEntryInDirSeq(
+								   dir,
+								   name,
+								   data,
+								   next_handler,
+								   show_handler,
+								   off2element_handler,
+								   startstop_handler,
+								   NULL
+								  );
+}
+
+
+
+struct proc_dir_entry* CreatePerProcessProcEntrySeq (
+									  const IMG_CHAR * name,
+								  IMG_VOID* data,
+									  pvr_next_proc_seq_t next_handler,
+									  pvr_show_proc_seq_t show_handler,
+									  pvr_off2element_proc_seq_t off2element_handler,
+									  pvr_startstop_proc_seq_t startstop_handler,
+									  write_proc_t whandler
+									 )
+{
+    PVRSRV_ENV_PER_PROCESS_DATA *psPerProc;
+    IMG_UINT32 ui32PID;
+
+    if (!dir)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntrySeq: /proc/%s doesn't exist", PVRProcDirRoot));
+        return NULL;
+    }
+
+    ui32PID = OSGetCurrentProcessIDKM();
+
+    psPerProc = PVRSRVPerProcessPrivateData(ui32PID);
+    if (!psPerProc)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntrySeq: no per process data"));
+
+        return NULL;
+    }
+
+    if (!psPerProc->psProcDir)
+    {
+        IMG_CHAR dirname[16];
+        IMG_INT ret;
+
+        ret = snprintf(dirname, sizeof(dirname), "%lu", ui32PID);
+
+		if (ret <=0 || ret >= (IMG_INT)sizeof(dirname))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: couldn't generate per process proc directory name \"%u\"", ui32PID));
+			return NULL;
+		}
+		else
+		{
+			psPerProc->psProcDir = proc_mkdir(dirname, dir);
+			if (!psPerProc->psProcDir)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: couldn't create per process proc directory /proc/%s/%u",
+						PVRProcDirRoot, ui32PID));
+				return NULL;
+			}
+		}
+    }
+
+    return CreateProcEntryInDirSeq(psPerProc->psProcDir, name, data, next_handler,
+								   show_handler,off2element_handler,startstop_handler,whandler);
+}
+
+
+IMG_VOID RemoveProcEntrySeq( struct proc_dir_entry* proc_entry )
+{
+    if (dir)
+    {
+		void* data = proc_entry->data ;
+        PVR_DPF((PVR_DBG_MESSAGE, "Removing /proc/%s/%s", PVRProcDirRoot, proc_entry->name));
+
+        remove_proc_entry(proc_entry->name, dir);
+		if( data)
+			kfree( data );
+
+    }
+}
+
+IMG_VOID RemovePerProcessProcEntrySeq(struct proc_dir_entry* proc_entry)
+{
+    PVRSRV_ENV_PER_PROCESS_DATA *psPerProc;
+
+    psPerProc = LinuxTerminatingProcessPrivateData();
+    if (!psPerProc)
+    {
+        psPerProc = PVRSRVFindPerProcessPrivateData();
+        if (!psPerProc)
+        {
+            PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: can't "
+                                    "remove %s, no per process data", proc_entry->name));
+            return;
+        }
+    }
+
+    if (psPerProc->psProcDir)
+    {
+		void* data = proc_entry->data ;
+        PVR_DPF((PVR_DBG_MESSAGE, "Removing proc entry %s from %s", proc_entry->name, psPerProc->psProcDir->name));
+
+        remove_proc_entry(proc_entry->name, psPerProc->psProcDir);
+		if(data)
+			kfree( data );
+    }
+}
+
+#endif
+
+static IMG_INT pvr_read_proc(IMG_CHAR *page, IMG_CHAR **start, off_t off,
+                         IMG_INT count, IMG_INT *eof, IMG_VOID *data)
+{
+    pvr_read_proc_t *pprn = (pvr_read_proc_t *)data;
+
+    off_t len = pprn (page, (size_t)count, off);
+
+    if (len == END_OF_FILE)
+    {
+        len  = 0;
+        *eof = 1;
+    }
+    else if (!len)
+    {
+        *start = (IMG_CHAR *) 0;
+    }
+    else
+    {
+        *start = (IMG_CHAR *) 1;
+    }
+
+    return len;
+}
+
+
+static IMG_INT CreateProcEntryInDir(struct proc_dir_entry *pdir, const IMG_CHAR * name, read_proc_t rhandler, write_proc_t whandler, IMG_VOID *data)
+{
+    struct proc_dir_entry * file;
+    mode_t mode;
+
+    if (!pdir)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcEntryInDir: parent directory doesn't exist"));
+
+        return -ENOMEM;
+    }
+
+    mode = S_IFREG;
+
+    if (rhandler)
+    {
+	mode |= S_IRUGO;
+    }
+
+    if (whandler)
+    {
+	mode |= S_IWUSR;
+    }
+
+    file = create_proc_entry(name, mode, pdir);
+
+    if (file)
+    {
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
+        file->owner = THIS_MODULE;
+#endif
+		file->read_proc = rhandler;
+		file->write_proc = whandler;
+		file->data = data;
+
+		PVR_DPF((PVR_DBG_MESSAGE, "Created proc entry %s in %s", name, pdir->name));
+
+        return 0;
+    }
+
+    PVR_DPF((PVR_DBG_ERROR, "CreateProcEntry: cannot create proc entry %s in %s", name, pdir->name));
+
+    return -ENOMEM;
+}
+
+
+IMG_INT CreateProcEntry(const IMG_CHAR * name, read_proc_t rhandler, write_proc_t whandler, IMG_VOID *data)
+{
+    return CreateProcEntryInDir(dir, name, rhandler, whandler, data);
+}
+
+
+IMG_INT CreatePerProcessProcEntry(const IMG_CHAR * name, read_proc_t rhandler, write_proc_t whandler, IMG_VOID *data)
+{
+    PVRSRV_ENV_PER_PROCESS_DATA *psPerProc;
+    IMG_UINT32 ui32PID;
+
+    if (!dir)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: /proc/%s doesn't exist", PVRProcDirRoot));
+
+        return -ENOMEM;
+    }
+
+    ui32PID = OSGetCurrentProcessIDKM();
+
+    psPerProc = PVRSRVPerProcessPrivateData(ui32PID);
+    if (!psPerProc)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: no per process data"));
+
+        return -ENOMEM;
+    }
+
+    if (!psPerProc->psProcDir)
+    {
+        IMG_CHAR dirname[16];
+        IMG_INT ret;
+
+        ret = snprintf(dirname, sizeof(dirname), "%lu", ui32PID);
+
+		if (ret <=0 || ret >= (IMG_INT)sizeof(dirname))
+		{
+			PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: couldn't generate per process proc directory name \"%u\"", ui32PID));
+
+					return -ENOMEM;
+		}
+		else
+		{
+			psPerProc->psProcDir = proc_mkdir(dirname, dir);
+			if (!psPerProc->psProcDir)
+			{
+			PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: couldn't create per process proc directory /proc/%s/%u", PVRProcDirRoot, ui32PID));
+
+			return -ENOMEM;
+			}
+		}
+    }
+
+    return CreateProcEntryInDir(psPerProc->psProcDir, name, rhandler, whandler, data);
+}
+
+
+IMG_INT CreateProcReadEntry(const IMG_CHAR * name, pvr_read_proc_t handler)
+{
+    struct proc_dir_entry * file;
+
+    if (!dir)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcReadEntry: cannot make proc entry /proc/%s/%s: no parent", PVRProcDirRoot, name));
+
+        return -ENOMEM;
+    }
+
+    file = create_proc_read_entry (name, S_IFREG | S_IRUGO, dir, pvr_read_proc, (IMG_VOID *)handler);
+
+    if (file)
+    {
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
+        file->owner = THIS_MODULE;
+#endif
+        return 0;
+    }
+
+    PVR_DPF((PVR_DBG_ERROR, "CreateProcReadEntry: cannot make proc entry /proc/%s/%s: no memory", PVRProcDirRoot, name));
+
+    return -ENOMEM;
+}
+
+
+IMG_INT CreateProcEntries(IMG_VOID)
+{
+    dir = proc_mkdir (PVRProcDirRoot, NULL);
+
+    if (!dir)
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcEntries: cannot make /proc/%s directory", PVRProcDirRoot));
+
+        return -ENOMEM;
+    }
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+	g_pProcQueue = CreateProcReadEntrySeq("queue", NULL, NULL, ProcSeqShowQueue, ProcSeqOff2ElementQueue, NULL);
+	g_pProcVersion = CreateProcReadEntrySeq("version", NULL, NULL, ProcSeqShowVersion, ProcSeq1ElementHeaderOff2Element, NULL);
+	g_pProcSysNodes = CreateProcReadEntrySeq("nodes", NULL, NULL, ProcSeqShowSysNodes, ProcSeqOff2ElementSysNodes, NULL);
+
+	if(!g_pProcQueue || !g_pProcVersion || !g_pProcSysNodes)
+#else
+    if (CreateProcReadEntry("queue", QueuePrintQueues) ||
+		CreateProcReadEntry("version", procDumpVersion) ||
+		CreateProcReadEntry("nodes", procDumpSysNodes))
+#endif
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcEntries: couldn't make /proc/%s files", PVRProcDirRoot));
+
+        return -ENOMEM;
+    }
+
+
+#ifdef DEBUG
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+	g_pProcDebugLevel = CreateProcEntrySeq("debug_level", NULL, NULL,
+											ProcSeqShowDebugLevel,
+											ProcSeq1ElementOff2Element, NULL,
+										    PVRDebugProcSetLevel);
+	if(!g_pProcDebugLevel)
+#else
+	if (CreateProcEntry ("debug_level", PVRDebugProcGetLevel, PVRDebugProcSetLevel, 0))
+#endif
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcEntries: couldn't make /proc/%s/debug_level", PVRProcDirRoot));
+
+        return -ENOMEM;
+    }
+
+#ifdef PVR_MANUAL_POWER_CONTROL
+#ifdef PVR_PROC_USE_SEQ_FILE
+	g_pProcPowerLevel = CreateProcEntrySeq("power_control", NULL, NULL,
+											ProcSeqShowPowerLevel,
+											ProcSeq1ElementOff2Element, NULL,
+										    PVRProcSetPowerLevel);
+	if(!g_pProcPowerLevel)
+#else
+	if (CreateProcEntry("power_control", PVRProcGetPowerLevel, PVRProcSetPowerLevel, 0))
+#endif
+    {
+        PVR_DPF((PVR_DBG_ERROR, "CreateProcEntries: couldn't make /proc/%s/power_control", PVRProcDirRoot));
+
+        return -ENOMEM;
+    }
+#endif
+#endif
+
+    return 0;
+}
+
+
+IMG_VOID RemoveProcEntry(const IMG_CHAR * name)
+{
+    if (dir)
+    {
+        remove_proc_entry(name, dir);
+        PVR_DPF((PVR_DBG_MESSAGE, "Removing /proc/%s/%s", PVRProcDirRoot, name));
+    }
+}
+
+
+IMG_VOID RemovePerProcessProcEntry(const IMG_CHAR *name)
+{
+    PVRSRV_ENV_PER_PROCESS_DATA *psPerProc;
+
+    psPerProc = LinuxTerminatingProcessPrivateData();
+    if (!psPerProc)
+    {
+        psPerProc = PVRSRVFindPerProcessPrivateData();
+        if (!psPerProc)
+        {
+            PVR_DPF((PVR_DBG_ERROR, "CreatePerProcessProcEntries: can't "
+                                    "remove %s, no per process data", name));
+            return;
+        }
+    }
+
+    if (psPerProc->psProcDir)
+    {
+        remove_proc_entry(name, psPerProc->psProcDir);
+
+        PVR_DPF((PVR_DBG_MESSAGE, "Removing proc entry %s from %s", name, psPerProc->psProcDir->name));
+    }
+}
+
+
+IMG_VOID RemovePerProcessProcDir(PVRSRV_ENV_PER_PROCESS_DATA *psPerProc)
+{
+    if (psPerProc->psProcDir)
+    {
+        while (psPerProc->psProcDir->subdir)
+        {
+            PVR_DPF((PVR_DBG_WARNING, "Belatedly removing /proc/%s/%s/%s", PVRProcDirRoot, psPerProc->psProcDir->name, psPerProc->psProcDir->subdir->name));
+
+            RemoveProcEntry(psPerProc->psProcDir->subdir->name);
+        }
+        RemoveProcEntry(psPerProc->psProcDir->name);
+    }
+}
+
+IMG_VOID RemoveProcEntries(IMG_VOID)
+{
+#ifdef DEBUG
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+    RemoveProcEntrySeq( g_pProcDebugLevel );
+#else
+    RemoveProcEntry("debug_level");
+#endif
+
+#ifdef PVR_MANUAL_POWER_CONTROL
+#ifdef PVR_PROC_USE_SEQ_FILE
+    RemoveProcEntrySeq( g_pProcPowerLevel );
+#else
+    RemoveProcEntry("power_control");
+#endif
+#endif
+
+#endif
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+    RemoveProcEntrySeq(g_pProcQueue);
+    RemoveProcEntrySeq(g_pProcVersion);
+	RemoveProcEntrySeq(g_pProcSysNodes);
+#else
+    RemoveProcEntry("queue");
+    RemoveProcEntry("version");
+    RemoveProcEntry("nodes");
+#endif
+
+    while (dir->subdir)
+    {
+	PVR_DPF((PVR_DBG_WARNING, "Belatedly removing /proc/%s/%s", PVRProcDirRoot, dir->subdir->name));
+
+	RemoveProcEntry(dir->subdir->name);
+    }
+
+    remove_proc_entry(PVRProcDirRoot, NULL);
+}
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void ProcSeqShowVersion(struct seq_file *sfile,void* el)
+{
+	SYS_DATA * psSysData;
+	IMG_CHAR *pszSystemVersionString = "None";
+
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+		seq_printf( sfile,
+						"Version %s (%s) %s\n",
+						PVRVERSION_STRING,
+						PVR_BUILD_TYPE, PVR_BUILD_DIR);
+		return;
+	}
+
+    SysAcquireData(&psSysData);
+
+    if(psSysData->pszVersionString)
+	{
+	pszSystemVersionString = psSysData->pszVersionString;
+    }
+
+	seq_printf( sfile, "System Version String: %s\n", pszSystemVersionString);
+}
+
+#else
+
+static off_t procDumpVersion(IMG_CHAR *buf, size_t size, off_t off)
+{
+    SYS_DATA *psSysData;
+
+    if (off == 0)
+    {
+	return printAppend(buf, size, 0,
+						"Version %s (%s) %s\n",
+						PVRVERSION_STRING,
+						PVR_BUILD_TYPE, PVR_BUILD_DIR);
+    }
+
+    SysAcquireData(&psSysData)
+
+    if (off == 1)
+    {
+        IMG_CHAR *pszSystemVersionString = "None";
+
+        if(psSysData->pszVersionString)
+        {
+            pszSystemVersionString = psSysData->pszVersionString;
+        }
+
+        if(strlen(pszSystemVersionString)
+            + strlen("System Version String: \n")
+            + 1 > size)
+        {
+            return 0;
+        }
+        return printAppend(buf, size, 0,
+                            "System Version String: %s\n",
+                            pszSystemVersionString);
+    }
+
+    return END_OF_FILE;
+}
+
+#endif
+
+
+static const IMG_CHAR *deviceTypeToString(PVRSRV_DEVICE_TYPE deviceType)
+{
+    switch (deviceType)
+    {
+        default:
+        {
+            static IMG_CHAR text[10];
+
+            sprintf(text, "?%x", (IMG_UINT)deviceType);
+
+            return text;
+        }
+    }
+}
+
+
+static const IMG_CHAR *deviceClassToString(PVRSRV_DEVICE_CLASS deviceClass)
+{
+    switch (deviceClass)
+    {
+	case PVRSRV_DEVICE_CLASS_3D:
+	{
+	    return "3D";
+	}
+	case PVRSRV_DEVICE_CLASS_DISPLAY:
+	{
+	    return "display";
+	}
+	case PVRSRV_DEVICE_CLASS_BUFFER:
+	{
+	    return "buffer";
+	}
+	default:
+	{
+	    static IMG_CHAR text[10];
+
+	    sprintf(text, "?%x", (IMG_UINT)deviceClass);
+	    return text;
+	}
+    }
+}
+
+IMG_VOID* DecOffPsDev_AnyVaCb(PVRSRV_DEVICE_NODE *psNode, va_list va)
+{
+	off_t *pOff = va_arg(va, off_t*);
+	if (--(*pOff))
+	{
+		return IMG_NULL;
+	}
+	else
+	{
+		return psNode;
+	}
+}
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void ProcSeqShowSysNodes(struct seq_file *sfile,void* el)
+{
+	SYS_DATA * psSysData;
+    PVRSRV_DEVICE_NODE	*psDevNode = (PVRSRV_DEVICE_NODE*)el;
+
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+		seq_printf( sfile,
+						"Registered nodes\n"
+						"Addr     Type     Class    Index Ref pvDev     Size Res\n");
+		return;
+	}
+
+    SysAcquireData(&psSysData);
+
+	seq_printf( sfile,
+				  "%p %-8s %-8s %4d  %2lu  %p  %3lu  %p\n",
+				  psDevNode,
+				  deviceTypeToString(psDevNode->sDevId.eDeviceType),
+				  deviceClassToString(psDevNode->sDevId.eDeviceClass),
+				  psDevNode->sDevId.eDeviceClass,
+				  psDevNode->ui32RefCount,
+				  psDevNode->pvDevice,
+				  psDevNode->ui32pvDeviceSize,
+				  psDevNode->hResManContext);
+
+}
+
+static void* ProcSeqOff2ElementSysNodes(struct seq_file * sfile, loff_t off)
+{
+    SYS_DATA *psSysData;
+    PVRSRV_DEVICE_NODE	*psDevNode;
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+    SysAcquireData(&psSysData);
+
+
+	psDevNode = (PVRSRV_DEVICE_NODE*)
+				List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+													DecOffPsDev_AnyVaCb,
+													&off);
+
+
+	return (void*)psDevNode;
+}
+
+#else
+
+static
+off_t procDumpSysNodes(IMG_CHAR *buf, size_t size, off_t off)
+{
+    SYS_DATA 			*psSysData;
+    PVRSRV_DEVICE_NODE	*psDevNode;
+    off_t				len;
+
+
+    if (size < 80)
+    {
+		return 0;
+    }
+
+    if (off == 0)
+    {
+		return printAppend(buf, size, 0,
+						"Registered nodes\n"
+						"Addr     Type     Class    Index Ref pvDev     Size Res\n");
+    }
+
+    SysAcquireData(&psSysData);
+
+
+	psDevNode = (PVRSRV_DEVICE_NODE*)
+				List_PVRSRV_DEVICE_NODE_Any_va(psSysData->psDeviceNodeList,
+													DecOffPsDev_AnyVaCb,
+													&off);
+
+    if (!psDevNode)
+    {
+		return END_OF_FILE;
+    }
+
+    len = printAppend(buf, size, 0,
+				  "%p %-8s %-8s %4d  %2lu  %p  %3lu  %p\n",
+				  psDevNode,
+				  deviceTypeToString(psDevNode->sDevId.eDeviceType),
+				  deviceClassToString(psDevNode->sDevId.eDeviceClass),
+				  psDevNode->sDevId.eDeviceClass,
+				  psDevNode->ui32RefCount,
+				  psDevNode->pvDevice,
+				  psDevNode->ui32pvDeviceSize,
+				  psDevNode->hResManContext);
+    return (len);
+}
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.h
new file mode 100644
index 0000000..7aa5424
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/proc.h
@@ -0,0 +1,111 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __SERVICES_PROC_H__
+#define __SERVICES_PROC_H__
+
+#include <asm/system.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+
+#define END_OF_FILE (off_t) -1
+
+typedef off_t (pvr_read_proc_t)(IMG_CHAR *, size_t, off_t);
+
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+#define PVR_PROC_SEQ_START_TOKEN (void*)1
+typedef void* (pvr_next_proc_seq_t)(struct seq_file *,void*,loff_t);
+typedef void* (pvr_off2element_proc_seq_t)(struct seq_file *, loff_t);
+typedef void (pvr_show_proc_seq_t)(struct seq_file *,void*);
+typedef void (pvr_startstop_proc_seq_t)(struct seq_file *, IMG_BOOL start);
+
+typedef struct _PVR_PROC_SEQ_HANDLERS_ {
+	pvr_next_proc_seq_t *next;
+	pvr_show_proc_seq_t *show;
+	pvr_off2element_proc_seq_t *off2element;
+	pvr_startstop_proc_seq_t *startstop;
+	IMG_VOID *data;
+} PVR_PROC_SEQ_HANDLERS;
+
+
+void* ProcSeq1ElementOff2Element(struct seq_file *sfile, loff_t off);
+
+void* ProcSeq1ElementHeaderOff2Element(struct seq_file *sfile, loff_t off);
+
+
+#endif
+
+off_t printAppend(IMG_CHAR * buffer, size_t size, off_t off, const IMG_CHAR * format, ...)
+	__attribute__((format(printf, 4, 5)));
+
+IMG_INT CreateProcEntries(IMG_VOID);
+
+IMG_INT CreateProcReadEntry (const IMG_CHAR * name, pvr_read_proc_t handler);
+
+IMG_INT CreateProcEntry(const IMG_CHAR * name, read_proc_t rhandler, write_proc_t whandler, IMG_VOID *data);
+
+IMG_INT CreatePerProcessProcEntry(const IMG_CHAR * name, read_proc_t rhandler, write_proc_t whandler, IMG_VOID *data);
+
+IMG_VOID RemoveProcEntry(const IMG_CHAR * name);
+
+IMG_VOID RemovePerProcessProcEntry(const IMG_CHAR * name);
+
+IMG_VOID RemoveProcEntries(IMG_VOID);
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+struct proc_dir_entry* CreateProcReadEntrySeq (
+								const IMG_CHAR* name,
+								IMG_VOID* data,
+								pvr_next_proc_seq_t next_handler,
+								pvr_show_proc_seq_t show_handler,
+								pvr_off2element_proc_seq_t off2element_handler,
+								pvr_startstop_proc_seq_t startstop_handler
+							   );
+
+struct proc_dir_entry* CreateProcEntrySeq (
+								const IMG_CHAR* name,
+								IMG_VOID* data,
+								pvr_next_proc_seq_t next_handler,
+								pvr_show_proc_seq_t show_handler,
+								pvr_off2element_proc_seq_t off2element_handler,
+								pvr_startstop_proc_seq_t startstop_handler,
+								write_proc_t whandler
+							   );
+
+struct proc_dir_entry* CreatePerProcessProcEntrySeq (
+								const IMG_CHAR* name,
+								IMG_VOID* data,
+								pvr_next_proc_seq_t next_handler,
+								pvr_show_proc_seq_t show_handler,
+								pvr_off2element_proc_seq_t off2element_handler,
+								pvr_startstop_proc_seq_t startstop_handler,
+								write_proc_t whandler
+							   );
+
+
+IMG_VOID RemoveProcEntrySeq(struct proc_dir_entry* proc_entry);
+IMG_VOID RemovePerProcessProcEntrySeq(struct proc_dir_entry* proc_entry);
+
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_bridge_k.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_bridge_k.c
new file mode 100644
index 0000000..8342891
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_bridge_k.c
@@ -0,0 +1,647 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "img_defs.h"
+#include "services.h"
+#include "pvr_bridge.h"
+#include "perproc.h"
+#include "mutex.h"
+#include "sysconfig.h"
+#include "pvr_debug.h"
+#include "proc.h"
+#include "private_data.h"
+#include "linkage.h"
+#include "pvr_bridge_km.h"
+
+#if defined(SUPPORT_DRI_DRM)
+#include <drm/drmP.h>
+#include "pvr_drm.h"
+#if defined(PVR_SECURE_DRM_AUTH_EXPORT)
+#include "env_perproc.h"
+#endif
+#endif
+
+#if defined(SUPPORT_VGX)
+#include "vgx_bridge.h"
+#endif
+
+#if defined(SUPPORT_SGX)
+#include "sgx_bridge.h"
+#endif
+
+#include "bridged_pvr_bridge.h"
+
+#ifdef MODULE_TEST
+#include "pvr_test_bridge.h"
+#include "kern_test.h"
+#endif
+
+
+#if defined(SUPPORT_DRI_DRM)
+#define	PRIVATE_DATA(pFile) ((pFile)->driver_priv)
+#else
+#define	PRIVATE_DATA(pFile) ((pFile)->private_data)
+#endif
+
+#if defined(DEBUG_BRIDGE_KM)
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+static struct proc_dir_entry *g_ProcBridgeStats =0;
+static void* ProcSeqNextBridgeStats(struct seq_file *sfile,void* el,loff_t off);
+static void ProcSeqShowBridgeStats(struct seq_file *sfile,void* el);
+static void* ProcSeqOff2ElementBridgeStats(struct seq_file * sfile, loff_t off);
+static void ProcSeqStartstopBridgeStats(struct seq_file *sfile,IMG_BOOL start);
+
+#else
+static off_t printLinuxBridgeStats(IMG_CHAR * buffer, size_t size, off_t off);
+#endif
+
+#endif
+
+extern struct mutex gPVRSRVLock;
+
+#if defined(SUPPORT_MEMINFO_IDS)
+static IMG_UINT64 ui64Stamp;
+#endif
+
+PVRSRV_ERROR
+LinuxBridgeInit(IMG_VOID)
+{
+#if defined(DEBUG_BRIDGE_KM)
+	{
+		IMG_INT iStatus;
+#ifdef PVR_PROC_USE_SEQ_FILE
+		g_ProcBridgeStats = CreateProcReadEntrySeq(
+												  "bridge_stats",
+												  NULL,
+												  ProcSeqNextBridgeStats,
+												  ProcSeqShowBridgeStats,
+												  ProcSeqOff2ElementBridgeStats,
+												  ProcSeqStartstopBridgeStats
+						  						 );
+		iStatus = !g_ProcBridgeStats ? -1 : 0;
+#else
+		iStatus = CreateProcReadEntry("bridge_stats", printLinuxBridgeStats);
+#endif
+
+		if(iStatus!=0)
+		{
+			return PVRSRV_ERROR_OUT_OF_MEMORY;
+		}
+	}
+#endif
+	return CommonBridgeInit();
+}
+
+IMG_VOID
+LinuxBridgeDeInit(IMG_VOID)
+{
+#if defined(DEBUG_BRIDGE_KM)
+#ifdef PVR_PROC_USE_SEQ_FILE
+    RemoveProcEntrySeq(g_ProcBridgeStats);
+#else
+	RemoveProcEntry("bridge_stats");
+#endif
+#endif
+}
+
+#if defined(DEBUG_BRIDGE_KM)
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+
+static void ProcSeqStartstopBridgeStats(struct seq_file *sfile,IMG_BOOL start)
+{
+	if(start)
+	{
+		mutex_lock(&gPVRSRVLock);
+	}
+	else
+	{
+		mutex_unlock(&gPVRSRVLock);
+	}
+}
+
+
+static void* ProcSeqOff2ElementBridgeStats(struct seq_file *sfile, loff_t off)
+{
+	if(!off)
+	{
+		return PVR_PROC_SEQ_START_TOKEN;
+	}
+
+	if(off > BRIDGE_DISPATCH_TABLE_ENTRY_COUNT)
+	{
+		return (void*)0;
+	}
+
+
+	return (void*)&g_BridgeDispatchTable[off-1];
+}
+
+static void* ProcSeqNextBridgeStats(struct seq_file *sfile,void* el,loff_t off)
+{
+	return ProcSeqOff2ElementBridgeStats(sfile,off);
+}
+
+
+static void ProcSeqShowBridgeStats(struct seq_file *sfile,void* el)
+{
+	PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *psEntry = (	PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY*)el;
+
+	if(el == PVR_PROC_SEQ_START_TOKEN)
+	{
+		seq_printf(sfile,
+						  "Total ioctl call count = %lu\n"
+						  "Total number of bytes copied via copy_from_user = %lu\n"
+						  "Total number of bytes copied via copy_to_user = %lu\n"
+						  "Total number of bytes copied via copy_*_user = %lu\n\n"
+						  "%-45s | %-40s | %10s | %20s | %10s\n",
+						  g_BridgeGlobalStats.ui32IOCTLCount,
+						  g_BridgeGlobalStats.ui32TotalCopyFromUserBytes,
+						  g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
+						  g_BridgeGlobalStats.ui32TotalCopyFromUserBytes+g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
+						  "Bridge Name",
+						  "Wrapper Function",
+						  "Call Count",
+						  "copy_from_user Bytes",
+						  "copy_to_user Bytes"
+						 );
+		return;
+	}
+
+	seq_printf(sfile,
+				   "%-45s   %-40s   %-10lu   %-20lu   %-10lu\n",
+				   psEntry->pszIOCName,
+				   psEntry->pszFunctionName,
+				   psEntry->ui32CallCount,
+				   psEntry->ui32CopyFromUserTotalBytes,
+				   psEntry->ui32CopyToUserTotalBytes);
+}
+
+#else
+
+static off_t
+printLinuxBridgeStats(IMG_CHAR * buffer, size_t count, off_t off)
+{
+	PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *psEntry;
+	off_t Ret;
+
+	mutex_lock(&gPVRSRVLock);
+
+	if(!off)
+	{
+		if(count < 500)
+		{
+			Ret = 0;
+			goto unlock_and_return;
+		}
+		Ret = printAppend(buffer, count, 0,
+						  "Total ioctl call count = %lu\n"
+						  "Total number of bytes copied via copy_from_user = %lu\n"
+						  "Total number of bytes copied via copy_to_user = %lu\n"
+						  "Total number of bytes copied via copy_*_user = %lu\n\n"
+						  "%-45s | %-40s | %10s | %20s | %10s\n",
+						  g_BridgeGlobalStats.ui32IOCTLCount,
+						  g_BridgeGlobalStats.ui32TotalCopyFromUserBytes,
+						  g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
+						  g_BridgeGlobalStats.ui32TotalCopyFromUserBytes+g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
+						  "Bridge Name",
+						  "Wrapper Function",
+						  "Call Count",
+						  "copy_from_user Bytes",
+						  "copy_to_user Bytes"
+						 );
+		goto unlock_and_return;
+	}
+
+	if(off > BRIDGE_DISPATCH_TABLE_ENTRY_COUNT)
+	{
+		Ret = END_OF_FILE;
+		goto unlock_and_return;
+	}
+
+	if(count < 300)
+	{
+		Ret = 0;
+		goto unlock_and_return;
+	}
+
+	psEntry = &g_BridgeDispatchTable[off-1];
+	Ret =  printAppend(buffer, count, 0,
+					   "%-45s   %-40s   %-10lu   %-20lu   %-10lu\n",
+					   psEntry->pszIOCName,
+					   psEntry->pszFunctionName,
+					   psEntry->ui32CallCount,
+					   psEntry->ui32CopyFromUserTotalBytes,
+					   psEntry->ui32CopyToUserTotalBytes);
+
+unlock_and_return:
+	mutex_unlock(&gPVRSRVLock);
+	return Ret;
+}
+#endif
+#endif
+
+
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT
+PVRSRV_BridgeDispatchKM(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile)
+#else
+IMG_INT32
+PVRSRV_BridgeDispatchKM(struct file *pFile, IMG_UINT unref__ ioctlCmd, IMG_UINT32 arg)
+#endif
+{
+	IMG_UINT32 cmd;
+#if !defined(SUPPORT_DRI_DRM)
+	PVRSRV_BRIDGE_PACKAGE *psBridgePackageUM = (PVRSRV_BRIDGE_PACKAGE *)arg;
+	PVRSRV_BRIDGE_PACKAGE sBridgePackageKM;
+#endif
+	PVRSRV_BRIDGE_PACKAGE *psBridgePackageKM;
+	IMG_UINT32 ui32PID = OSGetCurrentProcessIDKM();
+	PVRSRV_PER_PROCESS_DATA *psPerProc;
+	IMG_INT err = -EFAULT;
+
+	mutex_lock(&gPVRSRVLock);
+
+#if defined(SUPPORT_DRI_DRM)
+	PVR_UNREFERENCED_PARAMETER(dev);
+
+	psBridgePackageKM = (PVRSRV_BRIDGE_PACKAGE *)arg;
+	PVR_ASSERT(psBridgePackageKM != IMG_NULL);
+#else
+	PVR_UNREFERENCED_PARAMETER(ioctlCmd);
+
+	psBridgePackageKM = &sBridgePackageKM;
+
+	if(!OSAccessOK(PVR_VERIFY_WRITE,
+				   psBridgePackageUM,
+				   sizeof(PVRSRV_BRIDGE_PACKAGE)))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: Received invalid pointer to function arguments",
+				 __FUNCTION__));
+
+		goto unlock_and_return;
+	}
+
+
+	if(OSCopyFromUser(IMG_NULL,
+					  psBridgePackageKM,
+					  psBridgePackageUM,
+					  sizeof(PVRSRV_BRIDGE_PACKAGE))
+	  != PVRSRV_OK)
+	{
+		goto unlock_and_return;
+	}
+#endif
+
+	cmd = psBridgePackageKM->ui32BridgeID;
+
+#if defined(MODULE_TEST)
+	switch (cmd)
+	{
+		case PVRSRV_BRIDGE_SERVICES_TEST_MEM1:
+			{
+				PVRSRV_ERROR eError = MemTest1();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+		case PVRSRV_BRIDGE_SERVICES_TEST_MEM2:
+			{
+				PVRSRV_ERROR eError = MemTest2();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_RESOURCE:
+			{
+				PVRSRV_ERROR eError = ResourceTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_EVENTOBJECT:
+			{
+				PVRSRV_ERROR eError = EventObjectTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_MEMMAPPING:
+			{
+				PVRSRV_ERROR eError = MemMappingTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_PROCESSID:
+			{
+				PVRSRV_ERROR eError = ProcessIDTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_CLOCKUSWAITUS:
+			{
+				PVRSRV_ERROR eError = ClockusWaitusTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_TIMER:
+			{
+				PVRSRV_ERROR eError = TimerTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_PRIVSRV:
+			{
+				PVRSRV_ERROR eError = PrivSrvTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+		case PVRSRV_BRIDGE_SERVICES_TEST_COPYDATA:
+		{
+			IMG_UINT32               ui32PID;
+			PVRSRV_PER_PROCESS_DATA *psPerProc;
+			PVRSRV_ERROR eError;
+
+			ui32PID = OSGetCurrentProcessIDKM();
+
+			PVRSRVTrace("PVRSRV_BRIDGE_SERVICES_TEST_COPYDATA %d", ui32PID);
+
+			psPerProc = PVRSRVPerProcessData(ui32PID);
+
+			eError = CopyDataTest(psBridgePackageKM->pvParamIn, psBridgePackageKM->pvParamOut, psPerProc);
+
+			*(PVRSRV_ERROR*)psBridgePackageKM->pvParamOut = eError;
+			err = 0;
+			goto unlock_and_return;
+		}
+
+
+		case PVRSRV_BRIDGE_SERVICES_TEST_POWERMGMT:
+			{
+				PVRSRV_ERROR eError = PowerMgmtTest();
+				if (psBridgePackageKM->ui32OutBufferSize == sizeof(PVRSRV_BRIDGE_RETURN))
+				{
+					PVRSRV_BRIDGE_RETURN* pReturn = (PVRSRV_BRIDGE_RETURN*)psBridgePackageKM->pvParamOut ;
+					pReturn->eError = eError;
+				}
+			}
+			err = 0;
+			goto unlock_and_return;
+
+	}
+#endif
+
+	if(cmd != PVRSRV_BRIDGE_CONNECT_SERVICES)
+	{
+		PVRSRV_ERROR eError;
+
+		eError = PVRSRVLookupHandle(KERNEL_HANDLE_BASE,
+									(IMG_PVOID *)&psPerProc,
+									psBridgePackageKM->hKernelServices,
+									PVRSRV_HANDLE_TYPE_PERPROC_DATA);
+		if(eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "%s: Invalid kernel services handle (%d)",
+					 __FUNCTION__, eError));
+			goto unlock_and_return;
+		}
+
+		if(psPerProc->ui32PID != ui32PID)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "%s: Process %d tried to access data "
+					 "belonging to process %d", __FUNCTION__, ui32PID,
+					 psPerProc->ui32PID));
+			goto unlock_and_return;
+		}
+	}
+	else
+	{
+
+		psPerProc = PVRSRVPerProcessData(ui32PID);
+		if(psPerProc == IMG_NULL)
+		{
+			PVR_DPF((PVR_DBG_ERROR, "PVRSRV_BridgeDispatchKM: "
+					 "Couldn't create per-process data area"));
+			goto unlock_and_return;
+		}
+	}
+
+	psBridgePackageKM->ui32BridgeID = PVRSRV_GET_BRIDGE_ID(psBridgePackageKM->ui32BridgeID);
+
+#if defined(PVR_SECURE_FD_EXPORT)
+	switch(cmd)
+	{
+		case PVRSRV_BRIDGE_EXPORT_DEVICEMEM:
+		{
+			PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
+
+			if(psPrivateData->hKernelMemInfo)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Can only export one MemInfo "
+						 "per file descriptor", __FUNCTION__));
+				err = -EINVAL;
+				goto unlock_and_return;
+			}
+			break;
+		}
+
+		case PVRSRV_BRIDGE_MAP_DEV_MEMORY:
+		{
+			PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY *psMapDevMemIN =
+				(PVRSRV_BRIDGE_IN_MAP_DEV_MEMORY *)psBridgePackageKM->pvParamIn;
+			PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
+
+			if(!psPrivateData->hKernelMemInfo)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: File descriptor has no "
+						 "associated MemInfo handle", __FUNCTION__));
+				err = -EINVAL;
+				goto unlock_and_return;
+			}
+
+			psMapDevMemIN->hKernelMemInfo = psPrivateData->hKernelMemInfo;
+			break;
+		}
+
+		default:
+		{
+			PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
+
+			if(psPrivateData->hKernelMemInfo)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Import/Export handle tried "
+						 "to use privileged service", __FUNCTION__));
+				goto unlock_and_return;
+			}
+			break;
+		}
+	}
+#endif
+#if defined(SUPPORT_DRI_DRM) && defined(PVR_SECURE_DRM_AUTH_EXPORT)
+	switch(cmd)
+	{
+		case PVRSRV_BRIDGE_MAP_DEV_MEMORY:
+		case PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY:
+		{
+			PVRSRV_FILE_PRIVATE_DATA *psPrivateData;
+			int authenticated = pFile->authenticated;
+			PVRSRV_ENV_PER_PROCESS_DATA *psEnvPerProc;
+
+			if (authenticated)
+			{
+				break;
+			}
+
+
+			psEnvPerProc = (PVRSRV_ENV_PER_PROCESS_DATA *)PVRSRVProcessPrivateData(psPerProc);
+			if (psEnvPerProc == IMG_NULL)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Process private data not allocated", __FUNCTION__));
+				err = -EFAULT;
+				goto unlock_and_return;
+			}
+
+			list_for_each_entry(psPrivateData, &psEnvPerProc->sDRMAuthListHead, sDRMAuthListItem)
+			{
+				struct drm_file *psDRMFile = psPrivateData->psDRMFile;
+
+				if (pFile->master == psDRMFile->master)
+				{
+					authenticated |= psDRMFile->authenticated;
+					if (authenticated)
+					{
+						break;
+					}
+				}
+			}
+
+			if (!authenticated)
+			{
+				PVR_DPF((PVR_DBG_ERROR, "%s: Not authenticated for mapping device or device class memory", __FUNCTION__));
+				err = -EPERM;
+				goto unlock_and_return;
+			}
+			break;
+		}
+		default:
+			break;
+	}
+#endif
+
+	err = BridgedDispatchKM(psPerProc, psBridgePackageKM);
+	if(err != PVRSRV_OK)
+		goto unlock_and_return;
+
+	switch(cmd)
+	{
+#if defined(PVR_SECURE_FD_EXPORT)
+		case PVRSRV_BRIDGE_EXPORT_DEVICEMEM:
+		{
+			PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM *psExportDeviceMemOUT =
+				(PVRSRV_BRIDGE_OUT_EXPORTDEVICEMEM *)psBridgePackageKM->pvParamOut;
+			PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
+
+			psPrivateData->hKernelMemInfo = psExportDeviceMemOUT->hMemInfo;
+#if defined(SUPPORT_MEMINFO_IDS)
+			psExportDeviceMemOUT->ui64Stamp = psPrivateData->ui64Stamp = ++ui64Stamp;
+#endif
+			break;
+		}
+#endif
+
+#if defined(SUPPORT_MEMINFO_IDS)
+		case PVRSRV_BRIDGE_MAP_DEV_MEMORY:
+		{
+			PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY *psMapDeviceMemoryOUT =
+				(PVRSRV_BRIDGE_OUT_MAP_DEV_MEMORY *)psBridgePackageKM->pvParamOut;
+			PVRSRV_FILE_PRIVATE_DATA *psPrivateData = PRIVATE_DATA(pFile);
+			psMapDeviceMemoryOUT->sDstClientMemInfo.ui64Stamp =	psPrivateData->ui64Stamp;
+			break;
+		}
+
+		case PVRSRV_BRIDGE_MAP_DEVICECLASS_MEMORY:
+		{
+			PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY *psDeviceClassMemoryOUT =
+				(PVRSRV_BRIDGE_OUT_MAP_DEVICECLASS_MEMORY *)psBridgePackageKM->pvParamOut;
+			psDeviceClassMemoryOUT->sClientMemInfo.ui64Stamp = ++ui64Stamp;
+			break;
+		}
+#endif
+
+		default:
+			break;
+	}
+
+unlock_and_return:
+	mutex_unlock(&gPVRSRVLock);
+	return err;
+}
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_debug.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_debug.c
new file mode 100644
index 0000000..56d1046
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_debug.c
@@ -0,0 +1,418 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+
+#include <asm/io.h>
+#include <asm/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/hardirq.h>
+#include <linux/module.h>
+#include <linux/spinlock.h>
+#include <linux/tty.h>
+#include <stdarg.h>
+#include "img_types.h"
+#include "servicesext.h"
+#include "pvr_debug.h"
+#include "proc.h"
+#include "mutex.h"
+#include "linkage.h"
+
+#if defined(PVRSRV_NEED_PVR_DPF)
+
+#define PVR_MAX_FILEPATH_LEN 256
+
+static IMG_UINT32 gPVRDebugLevel = DBGPRIV_WARNING;
+
+#endif
+
+#define	PVR_MAX_MSG_LEN PVR_MAX_DEBUG_MESSAGE_LEN
+
+static IMG_CHAR gszBufferNonIRQ[PVR_MAX_MSG_LEN + 1];
+
+static IMG_CHAR gszBufferIRQ[PVR_MAX_MSG_LEN + 1];
+
+static struct mutex gsDebugMutexNonIRQ;
+
+DEFINE_SPINLOCK(gsDebugLockIRQ);
+
+#define	USE_SPIN_LOCK (in_interrupt() || !preemptible())
+
+static inline void GetBufferLock(unsigned long *pulLockFlags)
+{
+	if (USE_SPIN_LOCK)
+	{
+		spin_lock_irqsave(&gsDebugLockIRQ, *pulLockFlags);
+	}
+	else
+	{
+		mutex_lock(&gsDebugMutexNonIRQ);
+	}
+}
+
+static inline void ReleaseBufferLock(unsigned long ulLockFlags)
+{
+	if (USE_SPIN_LOCK)
+	{
+		spin_unlock_irqrestore(&gsDebugLockIRQ, ulLockFlags);
+	}
+	else
+	{
+		mutex_unlock(&gsDebugMutexNonIRQ);
+	}
+}
+
+static inline void SelectBuffer(IMG_CHAR **ppszBuf, IMG_UINT32 *pui32BufSiz)
+{
+	if (USE_SPIN_LOCK)
+	{
+		*ppszBuf = gszBufferIRQ;
+		*pui32BufSiz = sizeof(gszBufferIRQ);
+	}
+	else
+	{
+		*ppszBuf = gszBufferNonIRQ;
+		*pui32BufSiz = sizeof(gszBufferNonIRQ);
+	}
+}
+
+static IMG_BOOL VBAppend(IMG_CHAR *pszBuf, IMG_UINT32 ui32BufSiz, const IMG_CHAR* pszFormat, va_list VArgs)
+{
+	IMG_UINT32 ui32Used;
+	IMG_UINT32 ui32Space;
+	IMG_INT32 i32Len;
+
+	ui32Used = strlen(pszBuf);
+	BUG_ON(ui32Used >= ui32BufSiz);
+	ui32Space = ui32BufSiz - ui32Used;
+
+	i32Len = vsnprintf(&pszBuf[ui32Used], ui32Space, pszFormat, VArgs);
+	pszBuf[ui32BufSiz - 1] = 0;
+
+
+	return (i32Len < 0 || i32Len >= ui32Space);
+}
+
+IMG_VOID PVRDPFInit(IMG_VOID)
+{
+    mutex_init(&gsDebugMutexNonIRQ);
+}
+
+IMG_VOID PVRSRVReleasePrintf(const IMG_CHAR *pszFormat, ...)
+{
+	va_list vaArgs;
+	unsigned long ulLockFlags = 0;
+	IMG_CHAR *pszBuf;
+	IMG_UINT32 ui32BufSiz;
+
+	SelectBuffer(&pszBuf, &ui32BufSiz);
+
+	va_start(vaArgs, pszFormat);
+
+	GetBufferLock(&ulLockFlags);
+	strncpy (pszBuf, "PVR_K: ", (ui32BufSiz -1));
+
+	if (VBAppend(pszBuf, ui32BufSiz, pszFormat, vaArgs))
+	{
+		printk(KERN_INFO "PVR_K:(Message Truncated): %s\n", pszBuf);
+	}
+	else
+	{
+		printk(KERN_INFO "%s\n", pszBuf);
+	}
+
+	ReleaseBufferLock(ulLockFlags);
+	va_end(vaArgs);
+
+}
+
+#if defined(PVRSRV_NEED_PVR_ASSERT)
+
+IMG_VOID PVRSRVDebugAssertFail(const IMG_CHAR* pszFile, IMG_UINT32 uLine)
+{
+	PVRSRVDebugPrintf(DBGPRIV_FATAL, pszFile, uLine, "Debug assertion failed!");
+	BUG();
+}
+
+#endif
+
+#if defined(PVRSRV_NEED_PVR_TRACE)
+
+IMG_VOID PVRSRVTrace(const IMG_CHAR* pszFormat, ...)
+{
+	va_list VArgs;
+	unsigned long ulLockFlags = 0;
+	IMG_CHAR *pszBuf;
+	IMG_UINT32 ui32BufSiz;
+
+	SelectBuffer(&pszBuf, &ui32BufSiz);
+
+	va_start(VArgs, pszFormat);
+
+	GetBufferLock(&ulLockFlags);
+
+	strncpy(pszBuf, "PVR: ", (ui32BufSiz -1));
+
+	if (VBAppend(pszBuf, ui32BufSiz, pszFormat, VArgs))
+	{
+		printk(KERN_INFO "PVR_K:(Message Truncated): %s\n", pszBuf);
+	}
+	else
+	{
+		printk(KERN_INFO "%s\n", pszBuf);
+	}
+
+	ReleaseBufferLock(ulLockFlags);
+
+	va_end(VArgs);
+}
+
+#endif
+
+#if defined(PVRSRV_NEED_PVR_DPF)
+
+static IMG_BOOL BAppend(IMG_CHAR *pszBuf, IMG_UINT32 ui32BufSiz, const IMG_CHAR *pszFormat, ...)
+{
+		va_list VArgs;
+		IMG_BOOL bTrunc;
+
+		va_start (VArgs, pszFormat);
+
+		bTrunc = VBAppend(pszBuf, ui32BufSiz, pszFormat, VArgs);
+
+		va_end (VArgs);
+
+		return bTrunc;
+}
+
+IMG_VOID PVRSRVDebugPrintf	(
+						IMG_UINT32	ui32DebugLevel,
+						const IMG_CHAR*	pszFullFileName,
+						IMG_UINT32	ui32Line,
+						const IMG_CHAR*	pszFormat,
+						...
+					)
+{
+	IMG_BOOL bTrace, bDebug;
+	const IMG_CHAR *pszFileName = pszFullFileName;
+	IMG_CHAR *pszLeafName;
+
+	bTrace = gPVRDebugLevel & ui32DebugLevel & DBGPRIV_CALLTRACE;
+	bDebug = ((gPVRDebugLevel & DBGPRIV_ALLLEVELS) >= ui32DebugLevel);
+
+	if (bTrace || bDebug)
+	{
+		va_list vaArgs;
+		unsigned long ulLockFlags = 0;
+		IMG_CHAR *pszBuf;
+		IMG_UINT32 ui32BufSiz;
+
+		SelectBuffer(&pszBuf, &ui32BufSiz);
+
+		va_start(vaArgs, pszFormat);
+
+		GetBufferLock(&ulLockFlags);
+
+
+		if (bDebug)
+		{
+			switch(ui32DebugLevel)
+			{
+				case DBGPRIV_FATAL:
+				{
+					strncpy (pszBuf, "PVR_K:(Fatal): ", (ui32BufSiz -1));
+					break;
+				}
+				case DBGPRIV_ERROR:
+				{
+					strncpy (pszBuf, "PVR_K:(Error): ", (ui32BufSiz -1));
+					break;
+				}
+				case DBGPRIV_WARNING:
+				{
+					strncpy (pszBuf, "PVR_K:(Warning): ", (ui32BufSiz -1));
+					break;
+				}
+				case DBGPRIV_MESSAGE:
+				{
+					strncpy (pszBuf, "PVR_K:(Message): ", (ui32BufSiz -1));
+					break;
+				}
+				case DBGPRIV_VERBOSE:
+				{
+					strncpy (pszBuf, "PVR_K:(Verbose): ", (ui32BufSiz -1));
+					break;
+				}
+				default:
+				{
+					strncpy (pszBuf, "PVR_K:(Unknown message level)", (ui32BufSiz -1));
+					break;
+				}
+			}
+		}
+		else
+		{
+			strncpy (pszBuf, "PVR_K: ", (ui32BufSiz -1));
+		}
+
+		if (VBAppend(pszBuf, ui32BufSiz, pszFormat, vaArgs))
+		{
+			printk(KERN_INFO "PVR_K:(Message Truncated): %s\n", pszBuf);
+		}
+		else
+		{
+
+			if (!bTrace)
+			{
+#ifdef DEBUG_LOG_PATH_TRUNCATE
+
+				static IMG_CHAR szFileNameRewrite[PVR_MAX_FILEPATH_LEN];
+
+				IMG_CHAR* pszTruncIter;
+				IMG_CHAR* pszTruncBackInter;
+
+
+				pszFileName = pszFullFileName + strlen(DEBUG_LOG_PATH_TRUNCATE)+1;
+
+
+				strncpy(szFileNameRewrite, pszFileName,PVR_MAX_FILEPATH_LEN);
+
+				if(strlen(szFileNameRewrite) == PVR_MAX_FILEPATH_LEN-1) {
+					IMG_CHAR szTruncateMassage[] = "FILENAME TRUNCATED";
+					strcpy(szFileNameRewrite + (PVR_MAX_FILEPATH_LEN - 1 - strlen(szTruncateMassage)), szTruncateMassage);
+				}
+
+				pszTruncIter = szFileNameRewrite;
+				while(*pszTruncIter++ != 0)
+				{
+					IMG_CHAR* pszNextStartPoint;
+
+					if(
+					   !( ( *pszTruncIter == '/' && (pszTruncIter-4 >= szFileNameRewrite) ) &&
+						 ( *(pszTruncIter-1) == '.') &&
+						 ( *(pszTruncIter-2) == '.') &&
+						 ( *(pszTruncIter-3) == '/') )
+					   ) continue;
+
+
+					pszTruncBackInter = pszTruncIter - 3;
+					while(*(--pszTruncBackInter) != '/')
+					{
+						if(pszTruncBackInter <= szFileNameRewrite) break;
+					}
+					pszNextStartPoint = pszTruncBackInter;
+
+
+					while(*pszTruncIter != 0)
+					{
+						*pszTruncBackInter++ = *pszTruncIter++;
+					}
+					*pszTruncBackInter = 0;
+
+
+					pszTruncIter = pszNextStartPoint;
+				}
+
+				pszFileName = szFileNameRewrite;
+
+				if(*pszFileName == '/') pszFileName++;
+#endif
+
+#if !defined(__sh__)
+				pszLeafName = (IMG_CHAR *)strrchr (pszFileName, '\\');
+
+				if (pszLeafName)
+				{
+					pszFileName = pszLeafName;
+		       	}
+#endif
+
+				if (BAppend(pszBuf, ui32BufSiz, " [%lu, %s]", ui32Line, pszFileName))
+				{
+					printk(KERN_INFO "PVR_K:(Message Truncated): %s\n", pszBuf);
+				}
+				else
+				{
+					printk(KERN_INFO "%s\n", pszBuf);
+				}
+			}
+			else
+			{
+				printk(KERN_INFO "%s\n", pszBuf);
+			}
+		}
+
+		ReleaseBufferLock(ulLockFlags);
+
+		va_end (vaArgs);
+	}
+}
+
+#endif
+
+#if defined(DEBUG)
+
+IMG_VOID PVRDebugSetLevel(IMG_UINT32 uDebugLevel)
+{
+	printk(KERN_INFO "PVR: Setting Debug Level = 0x%x\n",(IMG_UINT)uDebugLevel);
+
+	gPVRDebugLevel = uDebugLevel;
+}
+
+IMG_INT PVRDebugProcSetLevel(struct file *file, const IMG_CHAR *buffer, IMG_UINT32 count, IMG_VOID *data)
+{
+#define	_PROC_SET_BUFFER_SZ		2
+	IMG_CHAR data_buffer[_PROC_SET_BUFFER_SZ];
+
+	if (count != _PROC_SET_BUFFER_SZ)
+	{
+		return -EINVAL;
+	}
+	else
+	{
+		if (copy_from_user(data_buffer, buffer, count))
+			return -EINVAL;
+		if (data_buffer[count - 1] != '\n')
+			return -EINVAL;
+		PVRDebugSetLevel(data_buffer[0] - '0');
+	}
+	return (count);
+}
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+void ProcSeqShowDebugLevel(struct seq_file *sfile,void* el)
+{
+	seq_printf(sfile, "%lu\n", gPVRDebugLevel);
+}
+
+#else
+IMG_INT PVRDebugProcGetLevel(IMG_CHAR *page, IMG_CHAR **start, off_t off, IMG_INT count, IMG_INT *eof, IMG_VOID *data)
+{
+	if (off == 0) {
+		*start = (IMG_CHAR *)1;
+		return printAppend(page, count, 0, "%lu\n", gPVRDebugLevel);
+	}
+	*eof = 1;
+	return 0;
+}
+#endif
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.c b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.c
new file mode 100644
index 0000000..f31f989
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.c
@@ -0,0 +1,301 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if defined(SUPPORT_DRI_DRM)
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/fs.h>
+#include <linux/proc_fs.h>
+#include <asm/ioctl.h>
+#include <drm/drmP.h>
+#include <drm/drm.h>
+
+#include "img_defs.h"
+#include "services.h"
+#include "kerneldisplay.h"
+#include "kernelbuffer.h"
+#include "sysconfig.h"
+#include "pvrmmap.h"
+#include "mm.h"
+#include "mmap.h"
+#include "mutex.h"
+#include "pvr_debug.h"
+#include "srvkm.h"
+#include "perproc.h"
+#include "handle.h"
+#include "pvr_bridge_km.h"
+#include "pvr_bridge.h"
+#include "proc.h"
+#include "pvrversion.h"
+#include "linkage.h"
+#include "pvr_drm_shared.h"
+#include "pvr_drm.h"
+
+#define	MAKENAME_HELPER(x, y) x ## y
+#define	MAKENAME(x, y) MAKENAME_HELPER(x, y)
+
+#define PVR_DRM_NAME	"pvrsrvkm"
+#define PVR_DRM_DESC	"Imagination Technologies PVR DRM"
+
+#define PVR_PCI_IDS \
+	{SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, \
+	{0, 0, 0}
+
+struct pci_dev *gpsPVRLDMDev;
+struct drm_device *gpsPVRDRMDev;
+
+#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,24))
+#error "Linux kernel version 2.6.25 or later required for PVR DRM support"
+#endif
+
+#define PVR_DRM_FILE struct drm_file *
+
+#if defined(DISPLAY_CONTROLLER)
+extern int MAKENAME(DISPLAY_CONTROLLER, _Init)(struct drm_device *);
+extern void MAKENAME(DISPLAY_CONTROLLER, _Cleanup)(struct drm_device *);
+#endif
+
+
+#if !defined(SUPPORT_DRI_DRM_EXT)
+static struct pci_device_id asPciIdList[] = {
+	PVR_PCI_IDS
+};
+#endif
+
+IMG_INT PVRSRVDrmLoad(struct drm_device *dev, unsigned long flags)
+{
+	IMG_INT iRes;
+
+	PVR_TRACE(("PVRSRVDrmLoad"));
+
+	gpsPVRDRMDev = dev;
+	gpsPVRLDMDev = dev->pdev;
+
+#if defined(PDUMP)
+	iRes = dbgdrv_init();
+	if (iRes != 0)
+	{
+		return iRes;
+	}
+#endif
+
+	iRes = PVRCore_Init();
+	if (iRes != 0)
+	{
+		goto exit_dbgdrv_cleanup;
+	}
+
+#if defined(DISPLAY_CONTROLLER)
+	iRes = MAKENAME(DISPLAY_CONTROLLER, _Init)(dev);
+	if (iRes != 0)
+	{
+		goto exit_pvrcore_cleanup;
+	}
+#endif
+	return 0;
+
+#if defined(DISPLAY_CONTROLLER)
+exit_pvrcore_cleanup:
+	PVRCore_Cleanup();
+#endif
+exit_dbgdrv_cleanup:
+#if defined(PDUMP)
+	dbgdrv_cleanup();
+#endif
+	return iRes;
+}
+
+IMG_INT PVRSRVDrmUnload(struct drm_device *dev)
+{
+	PVR_TRACE(("PVRSRVDrmUnload"));
+
+#if defined(DISPLAY_CONTROLLER)
+	MAKENAME(DISPLAY_CONTROLLER, _Cleanup)(dev);
+#endif
+
+	PVRCore_Cleanup();
+
+#if defined(PDUMP)
+	dbgdrv_cleanup();
+#endif
+
+	return 0;
+}
+
+IMG_INT PVRSRVDrmOpen(struct drm_device *dev, struct drm_file *file)
+{
+	return PVRSRVOpen(dev, file);
+}
+
+IMG_VOID PVRSRVDrmPostClose(struct drm_device *dev, struct drm_file *file)
+{
+	PVRSRVRelease(dev, file);
+}
+
+DRI_DRM_STATIC IMG_INT
+PVRDRMIsMaster(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile)
+{
+	return 0;
+}
+
+#if defined(SUPPORT_DRI_DRM_EXT)
+IMG_INT
+PVRDRM_Dummy_ioctl(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile)
+{
+	return 0;
+}
+#endif
+
+static IMG_INT
+PVRDRMPCIBusIDField(struct drm_device *dev, IMG_UINT32 *pui32Field, IMG_UINT32 ui32FieldType)
+{
+	struct pci_dev *psPCIDev = (struct pci_dev *)dev->pdev;
+
+	switch (ui32FieldType)
+	{
+		case PVR_DRM_PCI_DOMAIN:
+			*pui32Field = pci_domain_nr(psPCIDev->bus);
+			break;
+
+		case PVR_DRM_PCI_BUS:
+			*pui32Field = psPCIDev->bus->number;
+			break;
+
+		case PVR_DRM_PCI_DEV:
+			*pui32Field = PCI_SLOT(psPCIDev->devfn);
+			break;
+
+		case PVR_DRM_PCI_FUNC:
+			*pui32Field = PCI_FUNC(psPCIDev->devfn);
+			break;
+
+		default:
+			return -EFAULT;
+	}
+
+	return 0;
+}
+
+DRI_DRM_STATIC IMG_INT
+PVRDRMUnprivCmd(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile)
+{
+	IMG_UINT32 *pui32Args = (IMG_UINT32 *)arg;
+	IMG_UINT32 ui32Cmd = pui32Args[0];
+	IMG_UINT32 ui32Arg1 = pui32Args[1];
+	IMG_UINT32 *pui32OutArg = (IMG_UINT32 *)arg;
+
+	switch (ui32Cmd)
+	{
+		case PVR_DRM_UNPRIV_INIT_SUCCESFUL:
+			*pui32OutArg = PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL) ? 1 : 0;
+			break;
+
+		case PVR_DRM_UNPRIV_BUSID_TYPE:
+			*pui32OutArg = PVR_DRM_BUS_TYPE_PCI;
+			break;
+
+		case PVR_DRM_UNPRIV_BUSID_FIELD:
+			return PVRDRMPCIBusIDField(dev, pui32OutArg, ui32Arg1);
+
+		default:
+			return -EFAULT;
+	}
+
+	return 0;
+}
+
+#if !defined(SUPPORT_DRI_DRM_EXT)
+struct drm_ioctl_desc sPVRDrmIoctls[] = {
+	DRM_IOCTL_DEF(PVR_DRM_SRVKM_IOCTL, PVRSRV_BridgeDispatchKM, 0),
+	DRM_IOCTL_DEF(PVR_DRM_IS_MASTER_IOCTL, PVRDRMIsMaster, DRM_MASTER),
+	DRM_IOCTL_DEF(PVR_DRM_UNPRIV_IOCTL, PVRDRMUnprivCmd, 0),
+#if defined(PDUMP)
+	DRM_IOCTL_DEF(PVR_DRM_DBGDRV_IOCTL, dbgdrv_ioctl, 0),
+#endif
+};
+
+static IMG_INT pvr_max_ioctl = DRM_ARRAY_SIZE(sPVRDrmIoctls);
+
+static struct drm_driver sPVRDrmDriver =
+{
+	.driver_features = 0,
+	.dev_priv_size = 0,
+	.load = PVRSRVDrmLoad,
+	.unload = PVRSRVDrmUnload,
+	.open = PVRSRVDrmOpen,
+	.postclose = PVRSRVDrmPostClose,
+	.suspend = PVRSRVDriverSuspend,
+	.resume = PVRSRVDriverResume,
+	.get_map_ofs = drm_core_get_map_ofs,
+	.get_reg_ofs = drm_core_get_reg_ofs,
+	.ioctls = sPVRDrmIoctls,
+	.fops =
+	{
+		.owner = THIS_MODULE,
+		.open = drm_open,
+		.release = drm_release,
+		.ioctl = drm_ioctl,
+		.mmap = PVRMMap,
+		.poll = drm_poll,
+		.fasync = drm_fasync,
+	},
+	.pci_driver =
+	{
+		.name = PVR_DRM_NAME,
+		.id_table = asPciIdList,
+	},
+
+	.name = PVR_DRM_NAME,
+	.desc = PVR_DRM_DESC,
+	.date = PVR_BUILD_DATE,
+	.major = PVRVERSION_MAJ,
+	.minor = PVRVERSION_MIN,
+	.patchlevel = PVRVERSION_BUILD,
+};
+
+static IMG_INT __init PVRSRVDrmInit(IMG_VOID)
+{
+	IMG_INT iRes;
+	sPVRDrmDriver.num_ioctls = pvr_max_ioctl;
+
+
+	PVRDPFInit();
+
+	iRes = drm_init(&sPVRDrmDriver);
+
+	return iRes;
+}
+
+static IMG_VOID __exit PVRSRVDrmExit(IMG_VOID)
+{
+	drm_exit(&sPVRDrmDriver);
+}
+
+module_init(PVRSRVDrmInit);
+module_exit(PVRSRVDrmExit);
+#endif
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.h
new file mode 100644
index 0000000..9f5e931
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/env/linux/pvr_drm.h
@@ -0,0 +1,65 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__PVR_DRM_H__)
+#define __PVR_DRM_H__
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT PVRCore_Init(IMG_VOID);
+IMG_VOID PVRCore_Cleanup(IMG_VOID);
+IMG_INT PVRSRVOpen(struct drm_device *dev, struct drm_file *pFile);
+IMG_INT PVRSRVRelease(struct drm_device *dev, struct drm_file *pFile);
+IMG_INT PVRSRVDriverSuspend(struct drm_device *pDevice, pm_message_t state);
+IMG_INT PVRSRVDriverResume(struct drm_device *pDevice);
+
+IMG_INT PVRSRV_BridgeDispatchKM(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile);
+
+#if defined(SUPPORT_DRI_DRM_EXT)
+#define	DRI_DRM_STATIC
+IMG_INT PVRSRVDrmLoad(struct drm_device *dev, unsigned long flags);
+IMG_INT PVRSRVDrmUnload(struct drm_device *dev);
+IMG_VOID PVRSRVDrmPostClose(struct drm_device *dev, struct drm_file *file);
+IMG_INT PVRDRMIsMaster(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile);
+IMG_INT PVRDRMUnprivCmd(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile);
+IMG_INT PVRDRM_Dummy_ioctl(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile);
+#else
+#define	DRI_DRM_STATIC	static
+#endif
+
+#if defined(PDUMP)
+int dbgdrv_init(void);
+void dbgdrv_cleanup(void);
+IMG_INT dbgdrv_ioctl(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile);
+#endif
+
+#if !defined(SUPPORT_DRI_DRM_EXT)
+#define	PVR_DRM_SRVKM_IOCTL	_IO(0, PVR_DRM_SRVKM_CMD)
+#define	PVR_DRM_IS_MASTER_IOCTL _IO(0, PVR_DRM_IS_MASTER_CMD)
+#define	PVR_DRM_UNPRIV_IOCTL	_IO(0, PVR_DRM_UNPRIV_CMD)
+#define	PVR_DRM_DBGDRV_IOCTL	_IO(0, PVR_DRM_DBGDRV_CMD)
+#endif
+
+#endif
+
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx535defs.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx535defs.h
new file mode 100644
index 0000000..4e1c97f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx535defs.h
@@ -0,0 +1,633 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SGX535DEFS_KM_H_
+#define _SGX535DEFS_KM_H_
+
+#define EUR_CR_CLKGATECTL                   0x0000
+#define EUR_CR_CLKGATECTL_2D_CLKG_MASK      0x00000003UL
+#define EUR_CR_CLKGATECTL_2D_CLKG_SHIFT     0
+#define EUR_CR_CLKGATECTL_ISP_CLKG_MASK     0x00000030UL
+#define EUR_CR_CLKGATECTL_ISP_CLKG_SHIFT    4
+#define EUR_CR_CLKGATECTL_TSP_CLKG_MASK     0x00000300UL
+#define EUR_CR_CLKGATECTL_TSP_CLKG_SHIFT    8
+#define EUR_CR_CLKGATECTL_TA_CLKG_MASK      0x00003000UL
+#define EUR_CR_CLKGATECTL_TA_CLKG_SHIFT     12
+#define EUR_CR_CLKGATECTL_DPM_CLKG_MASK     0x00030000UL
+#define EUR_CR_CLKGATECTL_DPM_CLKG_SHIFT    16
+#define EUR_CR_CLKGATECTL_USE_CLKG_MASK     0x00300000UL
+#define EUR_CR_CLKGATECTL_USE_CLKG_SHIFT    20
+#define EUR_CR_CLKGATECTL_AUTO_MAN_REG_MASK 0x01000000UL
+#define EUR_CR_CLKGATECTL_AUTO_MAN_REG_SHIFT 24
+#define EUR_CR_CLKGATESTATUS                0x0004
+#define EUR_CR_CLKGATESTATUS_2D_CLKS_MASK   0x00000001UL
+#define EUR_CR_CLKGATESTATUS_2D_CLKS_SHIFT  0
+#define EUR_CR_CLKGATESTATUS_ISP_CLKS_MASK  0x00000010UL
+#define EUR_CR_CLKGATESTATUS_ISP_CLKS_SHIFT 4
+#define EUR_CR_CLKGATESTATUS_TSP_CLKS_MASK  0x00000100UL
+#define EUR_CR_CLKGATESTATUS_TSP_CLKS_SHIFT 8
+#define EUR_CR_CLKGATESTATUS_TA_CLKS_MASK   0x00001000UL
+#define EUR_CR_CLKGATESTATUS_TA_CLKS_SHIFT  12
+#define EUR_CR_CLKGATESTATUS_DPM_CLKS_MASK  0x00010000UL
+#define EUR_CR_CLKGATESTATUS_DPM_CLKS_SHIFT 16
+#define EUR_CR_CLKGATESTATUS_USE_CLKS_MASK  0x00100000UL
+#define EUR_CR_CLKGATESTATUS_USE_CLKS_SHIFT 20
+#define EUR_CR_CLKGATECTLOVR                0x0008
+#define EUR_CR_CLKGATECTLOVR_2D_CLKO_MASK   0x00000003UL
+#define EUR_CR_CLKGATECTLOVR_2D_CLKO_SHIFT  0
+#define EUR_CR_CLKGATECTLOVR_ISP_CLKO_MASK  0x00000030UL
+#define EUR_CR_CLKGATECTLOVR_ISP_CLKO_SHIFT 4
+#define EUR_CR_CLKGATECTLOVR_TSP_CLKO_MASK  0x00000300UL
+#define EUR_CR_CLKGATECTLOVR_TSP_CLKO_SHIFT 8
+#define EUR_CR_CLKGATECTLOVR_TA_CLKO_MASK   0x00003000UL
+#define EUR_CR_CLKGATECTLOVR_TA_CLKO_SHIFT  12
+#define EUR_CR_CLKGATECTLOVR_DPM_CLKO_MASK  0x00030000UL
+#define EUR_CR_CLKGATECTLOVR_DPM_CLKO_SHIFT 16
+#define EUR_CR_CLKGATECTLOVR_USE_CLKO_MASK  0x00300000UL
+#define EUR_CR_CLKGATECTLOVR_USE_CLKO_SHIFT 20
+#define EUR_CR_CORE_ID                      0x0010
+#define EUR_CR_CORE_ID_CONFIG_MASK          0x0000FFFFUL
+#define EUR_CR_CORE_ID_CONFIG_SHIFT         0
+#define EUR_CR_CORE_ID_ID_MASK              0xFFFF0000UL
+#define EUR_CR_CORE_ID_ID_SHIFT             16
+#define EUR_CR_CORE_REVISION                0x0014
+#define EUR_CR_CORE_REVISION_MAINTENANCE_MASK 0x000000FFUL
+#define EUR_CR_CORE_REVISION_MAINTENANCE_SHIFT 0
+#define EUR_CR_CORE_REVISION_MINOR_MASK     0x0000FF00UL
+#define EUR_CR_CORE_REVISION_MINOR_SHIFT    8
+#define EUR_CR_CORE_REVISION_MAJOR_MASK     0x00FF0000UL
+#define EUR_CR_CORE_REVISION_MAJOR_SHIFT    16
+#define EUR_CR_CORE_REVISION_DESIGNER_MASK  0xFF000000UL
+#define EUR_CR_CORE_REVISION_DESIGNER_SHIFT 24
+#define EUR_CR_DESIGNER_REV_FIELD1          0x0018
+#define EUR_CR_DESIGNER_REV_FIELD1_DESIGNER_REV_FIELD1_MASK 0xFFFFFFFFUL
+#define EUR_CR_DESIGNER_REV_FIELD1_DESIGNER_REV_FIELD1_SHIFT 0
+#define EUR_CR_DESIGNER_REV_FIELD2          0x001C
+#define EUR_CR_DESIGNER_REV_FIELD2_DESIGNER_REV_FIELD2_MASK 0xFFFFFFFFUL
+#define EUR_CR_DESIGNER_REV_FIELD2_DESIGNER_REV_FIELD2_SHIFT 0
+#define EUR_CR_SOFT_RESET                   0x0080
+#define EUR_CR_SOFT_RESET_BIF_RESET_MASK    0x00000001UL
+#define EUR_CR_SOFT_RESET_BIF_RESET_SHIFT   0
+#define EUR_CR_SOFT_RESET_TWOD_RESET_MASK   0x00000002UL
+#define EUR_CR_SOFT_RESET_TWOD_RESET_SHIFT  1
+#define EUR_CR_SOFT_RESET_DPM_RESET_MASK    0x00000004UL
+#define EUR_CR_SOFT_RESET_DPM_RESET_SHIFT   2
+#define EUR_CR_SOFT_RESET_TA_RESET_MASK     0x00000008UL
+#define EUR_CR_SOFT_RESET_TA_RESET_SHIFT    3
+#define EUR_CR_SOFT_RESET_USE_RESET_MASK    0x00000010UL
+#define EUR_CR_SOFT_RESET_USE_RESET_SHIFT   4
+#define EUR_CR_SOFT_RESET_ISP_RESET_MASK    0x00000020UL
+#define EUR_CR_SOFT_RESET_ISP_RESET_SHIFT   5
+#define EUR_CR_SOFT_RESET_TSP_RESET_MASK    0x00000040UL
+#define EUR_CR_SOFT_RESET_TSP_RESET_SHIFT   6
+#define EUR_CR_EVENT_HOST_ENABLE2           0x0110
+#define EUR_CR_EVENT_HOST_ENABLE2_BIF_REQUESTER_FAULT_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_ENABLE2_BIF_REQUESTER_FAULT_SHIFT 4
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_DHOST_FREE_LOAD_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_DHOST_FREE_LOAD_SHIFT 3
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_HOST_FREE_LOAD_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_HOST_FREE_LOAD_SHIFT 2
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_HOST_CLEAR2            0x0114
+#define EUR_CR_EVENT_HOST_CLEAR2_BIF_REQUESTER_FAULT_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_CLEAR2_BIF_REQUESTER_FAULT_SHIFT 4
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_DHOST_FREE_LOAD_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_DHOST_FREE_LOAD_SHIFT 3
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_HOST_FREE_LOAD_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_HOST_FREE_LOAD_SHIFT 2
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_STATUS2                0x0118
+#define EUR_CR_EVENT_STATUS2_BIF_REQUESTER_FAULT_MASK 0x00000010UL
+#define EUR_CR_EVENT_STATUS2_BIF_REQUESTER_FAULT_SHIFT 4
+#define EUR_CR_EVENT_STATUS2_DPM_DHOST_FREE_LOAD_MASK 0x00000008UL
+#define EUR_CR_EVENT_STATUS2_DPM_DHOST_FREE_LOAD_SHIFT 3
+#define EUR_CR_EVENT_STATUS2_DPM_HOST_FREE_LOAD_MASK 0x00000004UL
+#define EUR_CR_EVENT_STATUS2_DPM_HOST_FREE_LOAD_SHIFT 2
+#define EUR_CR_EVENT_STATUS2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_STATUS2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_STATUS2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_STATUS2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_STATUS                 0x012CUL
+#define EUR_CR_EVENT_STATUS_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_STATUS_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_STATUS_TIMER_MASK      0x20000000UL
+#define EUR_CR_EVENT_STATUS_TIMER_SHIFT     29
+#define EUR_CR_EVENT_STATUS_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_STATUS_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_STATUS_TWOD_COMPLETE_MASK 0x08000000UL
+#define EUR_CR_EVENT_STATUS_TWOD_COMPLETE_SHIFT 27
+#define EUR_CR_EVENT_STATUS_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_STATUS_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_STATUS_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_STATUS_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_STATUS_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_STATUS_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_STATUS_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_STATUS_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_STATUS_OTPM_LOADED_MASK 0x00200000UL
+#define EUR_CR_EVENT_STATUS_OTPM_LOADED_SHIFT 21
+#define EUR_CR_EVENT_STATUS_OTPM_INV_MASK   0x00100000UL
+#define EUR_CR_EVENT_STATUS_OTPM_INV_SHIFT  20
+#define EUR_CR_EVENT_STATUS_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_STATUS_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_STATUS_ISP_HALT_MASK   0x00020000UL
+#define EUR_CR_EVENT_STATUS_ISP_HALT_SHIFT  17
+#define EUR_CR_EVENT_STATUS_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_STATUS_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_STATUS_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_STATUS_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_STATUS_SW_EVENT_MASK   0x00004000UL
+#define EUR_CR_EVENT_STATUS_SW_EVENT_SHIFT  14
+#define EUR_CR_EVENT_STATUS_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_STATUS_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_STATUS_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_STATUS_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_STATUS_TPC_CLEAR_MASK  0x00000800UL
+#define EUR_CR_EVENT_STATUS_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_STATUS_TPC_FLUSH_MASK  0x00000400UL
+#define EUR_CR_EVENT_STATUS_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_STATUS_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_STATUS_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_STATUS_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_STATUS_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_STATUS_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_EVENT_HOST_ENABLE            0x0130
+#define EUR_CR_EVENT_HOST_ENABLE_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_HOST_ENABLE_TIMER_MASK 0x20000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TIMER_SHIFT 29
+#define EUR_CR_EVENT_HOST_ENABLE_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_HOST_ENABLE_TWOD_COMPLETE_MASK 0x08000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TWOD_COMPLETE_SHIFT 27
+#define EUR_CR_EVENT_HOST_ENABLE_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_LOADED_MASK 0x00200000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_LOADED_SHIFT 21
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_INV_MASK 0x00100000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_INV_SHIFT 20
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_HOST_ENABLE_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_HOST_ENABLE_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_HALT_MASK 0x00020000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_HALT_SHIFT 17
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_HOST_ENABLE_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_HOST_ENABLE_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_HOST_ENABLE_SW_EVENT_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_ENABLE_SW_EVENT_SHIFT 14
+#define EUR_CR_EVENT_HOST_ENABLE_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_HOST_ENABLE_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_CLEAR_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_FLUSH_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_EVENT_HOST_CLEAR             0x0134
+#define EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_HOST_CLEAR_TIMER_MASK  0x20000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TIMER_SHIFT 29
+#define EUR_CR_EVENT_HOST_CLEAR_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_HOST_CLEAR_TWOD_COMPLETE_MASK 0x08000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TWOD_COMPLETE_SHIFT 27
+#define EUR_CR_EVENT_HOST_CLEAR_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_LOADED_MASK 0x00200000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_LOADED_SHIFT 21
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_INV_MASK 0x00100000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_INV_SHIFT 20
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_HOST_CLEAR_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_HOST_CLEAR_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_HALT_MASK 0x00020000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_HALT_SHIFT 17
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_HOST_CLEAR_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_HOST_CLEAR_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_SHIFT 14
+#define EUR_CR_EVENT_HOST_CLEAR_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_HOST_CLEAR_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_CLEAR_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_FLUSH_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_PDS                          0x0ABC
+#define EUR_CR_PDS_DOUT_TIMEOUT_DISABLE_MASK 0x00000040UL
+#define EUR_CR_PDS_DOUT_TIMEOUT_DISABLE_SHIFT 6
+#define EUR_CR_PDS_EXEC_BASE                0x0AB8
+#define EUR_CR_PDS_EXEC_BASE_ADDR_MASK      0xFFF00000UL
+#define EUR_CR_PDS_EXEC_BASE_ADDR_SHIFT     20
+#define EUR_CR_EVENT_KICKER                 0x0AC4
+#define EUR_CR_EVENT_KICKER_ADDRESS_MASK    0xFFFFFFF0UL
+#define EUR_CR_EVENT_KICKER_ADDRESS_SHIFT   4
+#define EUR_CR_EVENT_KICK                   0x0AC8
+#define EUR_CR_EVENT_KICK_NOW_MASK          0x00000001UL
+#define EUR_CR_EVENT_KICK_NOW_SHIFT         0
+#define EUR_CR_EVENT_TIMER                  0x0ACC
+#define EUR_CR_EVENT_TIMER_ENABLE_MASK      0x01000000UL
+#define EUR_CR_EVENT_TIMER_ENABLE_SHIFT     24
+#define EUR_CR_EVENT_TIMER_VALUE_MASK       0x00FFFFFFUL
+#define EUR_CR_EVENT_TIMER_VALUE_SHIFT      0
+#define EUR_CR_PDS_INV0                     0x0AD0
+#define EUR_CR_PDS_INV0_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV0_DSC_SHIFT           0
+#define EUR_CR_PDS_INV1                     0x0AD4
+#define EUR_CR_PDS_INV1_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV1_DSC_SHIFT           0
+#define EUR_CR_PDS_INV2                     0x0AD8
+#define EUR_CR_PDS_INV2_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV2_DSC_SHIFT           0
+#define EUR_CR_PDS_INV3                     0x0ADC
+#define EUR_CR_PDS_INV3_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV3_DSC_SHIFT           0
+#define EUR_CR_PDS_INV_CSC                  0x0AE0
+#define EUR_CR_PDS_INV_CSC_KICK_MASK        0x00000001UL
+#define EUR_CR_PDS_INV_CSC_KICK_SHIFT       0
+#define EUR_CR_PDS_PC_BASE                  0x0B2C
+#define EUR_CR_PDS_PC_BASE_ADDRESS_MASK     0x3FFFFFFFUL
+#define EUR_CR_PDS_PC_BASE_ADDRESS_SHIFT    0
+#define EUR_CR_BIF_CTRL                     0x0C00
+#define EUR_CR_BIF_CTRL_NOREORDER_MASK      0x00000001UL
+#define EUR_CR_BIF_CTRL_NOREORDER_SHIFT     0
+#define EUR_CR_BIF_CTRL_PAUSE_MASK          0x00000002UL
+#define EUR_CR_BIF_CTRL_PAUSE_SHIFT         1
+#define EUR_CR_BIF_CTRL_FLUSH_MASK          0x00000004UL
+#define EUR_CR_BIF_CTRL_FLUSH_SHIFT         2
+#define EUR_CR_BIF_CTRL_INVALDC_MASK        0x00000008UL
+#define EUR_CR_BIF_CTRL_INVALDC_SHIFT       3
+#define EUR_CR_BIF_CTRL_CLEAR_FAULT_MASK    0x00000010UL
+#define EUR_CR_BIF_CTRL_CLEAR_FAULT_SHIFT   4
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_CACHE_MASK 0x00000100UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_CACHE_SHIFT 8
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_VDM_MASK 0x00000200UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_VDM_SHIFT 9
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TE_MASK  0x00000400UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TE_SHIFT 10
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TWOD_MASK 0x00000800UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TWOD_SHIFT 11
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_PBE_MASK 0x00001000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_PBE_SHIFT 12
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TSPP_MASK 0x00002000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TSPP_SHIFT 13
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_ISP_MASK 0x00004000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_ISP_SHIFT 14
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_USE_MASK 0x00008000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_USE_SHIFT 15
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_MASK 0x00010000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_SHIFT 16
+#define EUR_CR_BIF_INT_STAT                 0x0C04
+#define EUR_CR_BIF_INT_STAT_FAULT_MASK      0x00003FFFUL
+#define EUR_CR_BIF_INT_STAT_FAULT_SHIFT     0
+#define EUR_CR_BIF_INT_STAT_PF_N_RW_MASK    0x00004000UL
+#define EUR_CR_BIF_INT_STAT_PF_N_RW_SHIFT   14
+#define EUR_CR_BIF_FAULT                    0x0C08
+#define EUR_CR_BIF_FAULT_ADDR_MASK          0xFFFFF000UL
+#define EUR_CR_BIF_FAULT_ADDR_SHIFT         12
+#define EUR_CR_BIF_TILE0                    0x0C0C
+#define EUR_CR_BIF_TILE0_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE0_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE0_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE0_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE0_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE0_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE1                    0x0C10
+#define EUR_CR_BIF_TILE1_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE1_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE1_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE1_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE1_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE1_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE2                    0x0C14
+#define EUR_CR_BIF_TILE2_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE2_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE2_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE2_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE2_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE2_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE3                    0x0C18
+#define EUR_CR_BIF_TILE3_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE3_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE3_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE3_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE3_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE3_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE4                    0x0C1C
+#define EUR_CR_BIF_TILE4_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE4_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE4_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE4_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE4_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE4_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE5                    0x0C20
+#define EUR_CR_BIF_TILE5_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE5_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE5_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE5_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE5_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE5_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE6                    0x0C24
+#define EUR_CR_BIF_TILE6_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE6_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE6_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE6_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE6_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE6_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE7                    0x0C28
+#define EUR_CR_BIF_TILE7_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE7_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE7_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE7_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE7_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE7_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE8                    0x0C2C
+#define EUR_CR_BIF_TILE8_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE8_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE8_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE8_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE8_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE8_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE9                    0x0C30
+#define EUR_CR_BIF_TILE9_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE9_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE9_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE9_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE9_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE9_CFG_SHIFT          24
+#define EUR_CR_BIF_DIR_LIST_BASE1           0x0C38
+#define EUR_CR_BIF_DIR_LIST_BASE1_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE1_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE2           0x0C3C
+#define EUR_CR_BIF_DIR_LIST_BASE2_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE2_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE3           0x0C40
+#define EUR_CR_BIF_DIR_LIST_BASE3_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE3_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE4           0x0C44
+#define EUR_CR_BIF_DIR_LIST_BASE4_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE4_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE5           0x0C48
+#define EUR_CR_BIF_DIR_LIST_BASE5_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE5_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE6           0x0C4C
+#define EUR_CR_BIF_DIR_LIST_BASE6_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE6_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE7           0x0C50
+#define EUR_CR_BIF_DIR_LIST_BASE7_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE7_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE8           0x0C54
+#define EUR_CR_BIF_DIR_LIST_BASE8_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE8_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE9           0x0C58
+#define EUR_CR_BIF_DIR_LIST_BASE9_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE9_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE10          0x0C5C
+#define EUR_CR_BIF_DIR_LIST_BASE10_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE10_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE11          0x0C60
+#define EUR_CR_BIF_DIR_LIST_BASE11_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE11_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE12          0x0C64
+#define EUR_CR_BIF_DIR_LIST_BASE12_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE12_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE13          0x0C68
+#define EUR_CR_BIF_DIR_LIST_BASE13_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE13_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE14          0x0C6C
+#define EUR_CR_BIF_DIR_LIST_BASE14_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE14_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE15          0x0C70
+#define EUR_CR_BIF_DIR_LIST_BASE15_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE15_ADDR_SHIFT 12
+#define EUR_CR_BIF_BANK_SET                 0x0C74
+#define EUR_CR_BIF_BANK_SET_SELECT_MASK     0x000003FFUL
+#define EUR_CR_BIF_BANK_SET_SELECT_SHIFT    0
+#define EUR_CR_BIF_BANK0                    0x0C78
+#define EUR_CR_BIF_BANK0_INDEX_EDM_MASK     0x0000000FUL
+#define EUR_CR_BIF_BANK0_INDEX_EDM_SHIFT    0
+#define EUR_CR_BIF_BANK0_INDEX_TA_MASK      0x000000F0UL
+#define EUR_CR_BIF_BANK0_INDEX_TA_SHIFT     4
+#define EUR_CR_BIF_BANK0_INDEX_HOST_MASK    0x00000F00UL
+#define EUR_CR_BIF_BANK0_INDEX_HOST_SHIFT   8
+#define EUR_CR_BIF_BANK0_INDEX_3D_MASK      0x0000F000UL
+#define EUR_CR_BIF_BANK0_INDEX_3D_SHIFT     12
+#define EUR_CR_BIF_BANK0_INDEX_2D_MASK      0x000F0000UL
+#define EUR_CR_BIF_BANK0_INDEX_2D_SHIFT     16
+#define EUR_CR_BIF_BANK1                    0x0C7C
+#define EUR_CR_BIF_BANK1_INDEX_EDM_MASK     0x0000000FUL
+#define EUR_CR_BIF_BANK1_INDEX_EDM_SHIFT    0
+#define EUR_CR_BIF_BANK1_INDEX_TA_MASK      0x000000F0UL
+#define EUR_CR_BIF_BANK1_INDEX_TA_SHIFT     4
+#define EUR_CR_BIF_BANK1_INDEX_HOST_MASK    0x00000F00UL
+#define EUR_CR_BIF_BANK1_INDEX_HOST_SHIFT   8
+#define EUR_CR_BIF_BANK1_INDEX_3D_MASK      0x0000F000UL
+#define EUR_CR_BIF_BANK1_INDEX_3D_SHIFT     12
+#define EUR_CR_BIF_BANK1_INDEX_2D_MASK      0x000F0000UL
+#define EUR_CR_BIF_BANK1_INDEX_2D_SHIFT     16
+#define EUR_CR_BIF_ADT_TTE                  0x0C80
+#define EUR_CR_BIF_ADT_TTE_VALUE_MASK       0x000000FFUL
+#define EUR_CR_BIF_ADT_TTE_VALUE_SHIFT      0
+#define EUR_CR_BIF_DIR_LIST_BASE0           0x0C84
+#define EUR_CR_BIF_DIR_LIST_BASE0_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE0_ADDR_SHIFT 12
+#define EUR_CR_BIF_TWOD_REQ_BASE            0x0C88
+#define EUR_CR_BIF_TWOD_REQ_BASE_ADDR_MASK  0xFFF00000UL
+#define EUR_CR_BIF_TWOD_REQ_BASE_ADDR_SHIFT 20
+#define EUR_CR_BIF_TA_REQ_BASE              0x0C90
+#define EUR_CR_BIF_TA_REQ_BASE_ADDR_MASK    0xFFF00000UL
+#define EUR_CR_BIF_TA_REQ_BASE_ADDR_SHIFT   20
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1      0x0C94
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_MMU_MASK 0x00000007UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_MMU_SHIFT 0
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_CACHE_MASK 0x00000038UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_CACHE_SHIFT 3
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_VDM_MASK 0x000001C0UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_VDM_SHIFT 6
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_TE_MASK 0x00000E00UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_TE_SHIFT 9
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_TWOD_MASK 0x00007000UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_TWOD_SHIFT 12
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_PBE_MASK 0x00038000UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_1_PBE_SHIFT 15
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2      0x0C98
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_HOST_MASK 0x00000007UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_HOST_SHIFT 0
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_USE_MASK 0x00000038UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_USE_SHIFT 3
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_ISP_MASK 0x000001C0UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_ISP_SHIFT 6
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_TSPP_MASK 0x00000E00UL
+#define EUR_CR_BIF_MEM_ARB_FLOWRATES_2_TSPP_SHIFT 9
+#define EUR_CR_BIF_MEM_ARB_CONFIG           0x0CA0
+#define EUR_CR_BIF_MEM_ARB_CONFIG_PAGE_SIZE_MASK 0x0000000FUL
+#define EUR_CR_BIF_MEM_ARB_CONFIG_PAGE_SIZE_SHIFT 0
+#define EUR_CR_BIF_MEM_ARB_CONFIG_BEST_CNT_MASK 0x00000FF0UL
+#define EUR_CR_BIF_MEM_ARB_CONFIG_BEST_CNT_SHIFT 4
+#define EUR_CR_BIF_MEM_ARB_CONFIG_TTE_THRESH_MASK 0x00FFF000UL
+#define EUR_CR_BIF_MEM_ARB_CONFIG_TTE_THRESH_SHIFT 12
+#define EUR_CR_BIF_MEM_REQ_STAT             0x0CA8
+#define EUR_CR_BIF_MEM_REQ_STAT_READS_MASK  0x000000FFUL
+#define EUR_CR_BIF_MEM_REQ_STAT_READS_SHIFT 0
+#define EUR_CR_BIF_3D_REQ_BASE              0x0CAC
+#define EUR_CR_BIF_3D_REQ_BASE_ADDR_MASK    0xFFF00000UL
+#define EUR_CR_BIF_3D_REQ_BASE_ADDR_SHIFT   20
+#define EUR_CR_BIF_ZLS_REQ_BASE             0x0CB0
+#define EUR_CR_BIF_ZLS_REQ_BASE_ADDR_MASK   0xFFF00000UL
+#define EUR_CR_BIF_ZLS_REQ_BASE_ADDR_SHIFT  20
+#define EUR_CR_BIF_BANK_STATUS              0x0CB4
+#define EUR_CR_BIF_BANK_STATUS_3D_CURRENT_BANK_MASK 0x00000001UL
+#define EUR_CR_BIF_BANK_STATUS_3D_CURRENT_BANK_SHIFT 0
+#define EUR_CR_BIF_BANK_STATUS_TA_CURRENT_BANK_MASK 0x00000002UL
+#define EUR_CR_BIF_BANK_STATUS_TA_CURRENT_BANK_SHIFT 1
+#define EUR_CR_2D_BLIT_STATUS               0x0E04
+#define EUR_CR_2D_BLIT_STATUS_COMPLETE_MASK 0x00FFFFFFUL
+#define EUR_CR_2D_BLIT_STATUS_COMPLETE_SHIFT 0
+#define EUR_CR_2D_BLIT_STATUS_BUSY_MASK     0x01000000UL
+#define EUR_CR_2D_BLIT_STATUS_BUSY_SHIFT    24
+#define EUR_CR_2D_VIRTUAL_FIFO_0            0x0E10
+#define EUR_CR_2D_VIRTUAL_FIFO_0_ENABLE_MASK 0x00000001UL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_ENABLE_SHIFT 0
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_MASK 0x0000000EUL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_SHIFT 1
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_DIV_MASK 0x00000FF0UL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_DIV_SHIFT 4
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_MUL_MASK 0x0000F000UL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_MUL_SHIFT 12
+#define EUR_CR_2D_VIRTUAL_FIFO_1            0x0E14
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_ACC_MASK 0x00000FFFUL
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_ACC_SHIFT 0
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MAX_ACC_MASK 0x00FFF000UL
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MAX_ACC_SHIFT 12
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_METRIC_MASK 0xFF000000UL
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_METRIC_SHIFT 24
+#define EUR_CR_2D_SOCIF                     0x0E18
+#define EUR_CR_2D_SOCIF_FREESPACE_MASK      0x000000FFUL
+#define EUR_CR_2D_SOCIF_FREESPACE_SHIFT     0
+#define EUR_CR_2D_ALPHA						0x0E1C
+#define EUR_CR_2D_ALPHA_COMPONENT_ONE_MASK  0x0000FF00UL
+#define EUR_CR_2D_ALPHA_COMPONENT_ONE_SHIFT 8
+#define EUR_CR_2D_ALPHA_COMPONENT_ZERO_MASK 0x000000FFUL
+#define EUR_CR_2D_ALPHA_COMPONENT_ZERO_SHIFT 0
+#define EUR_CR_USE_CODE_BASE(X)     (0x0A0C + (4 * (X)))
+#define EUR_CR_USE_CODE_BASE_ADDR_MASK      0x01FFFFFFUL
+#define EUR_CR_USE_CODE_BASE_ADDR_SHIFT     0
+#define EUR_CR_USE_CODE_BASE_DM_MASK        0x06000000UL
+#define EUR_CR_USE_CODE_BASE_DM_SHIFT       25
+#define EUR_CR_USE_CODE_BASE_SIZE_UINT32 16
+#define EUR_CR_USE_CODE_BASE_NUM_ENTRIES 16
+
+#define EUR_CR_MNE_CR_CTRL						0x0D00
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_MASK			0x00008000UL
+#define EUR_CR_MNE_CR_CTRL_INVAL				0x0D20
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx540defs.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx540defs.h
new file mode 100755
index 0000000..92a19f9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx540defs.h
@@ -0,0 +1,586 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SGX540DEFS_KM_H_
+#define _SGX540DEFS_KM_H_
+
+#define EUR_CR_CLKGATECTL                   0x0000
+#define EUR_CR_CLKGATECTL_ISP_CLKG_MASK     0x00000003UL
+#define EUR_CR_CLKGATECTL_ISP_CLKG_SHIFT    0
+#define EUR_CR_CLKGATECTL_ISP2_CLKG_MASK    0x0000000CUL
+#define EUR_CR_CLKGATECTL_ISP2_CLKG_SHIFT   2
+#define EUR_CR_CLKGATECTL_TSP_CLKG_MASK     0x00000030UL
+#define EUR_CR_CLKGATECTL_TSP_CLKG_SHIFT    4
+#define EUR_CR_CLKGATECTL_TE_CLKG_MASK      0x000000C0UL
+#define EUR_CR_CLKGATECTL_TE_CLKG_SHIFT     6
+#define EUR_CR_CLKGATECTL_MTE_CLKG_MASK     0x00000300UL
+#define EUR_CR_CLKGATECTL_MTE_CLKG_SHIFT    8
+#define EUR_CR_CLKGATECTL_DPM_CLKG_MASK     0x00000C00UL
+#define EUR_CR_CLKGATECTL_DPM_CLKG_SHIFT    10
+#define EUR_CR_CLKGATECTL_VDM_CLKG_MASK     0x00003000UL
+#define EUR_CR_CLKGATECTL_VDM_CLKG_SHIFT    12
+#define EUR_CR_CLKGATECTL_PDS_CLKG_MASK     0x0000C000UL
+#define EUR_CR_CLKGATECTL_PDS_CLKG_SHIFT    14
+#define EUR_CR_CLKGATECTL_IDXFIFO_CLKG_MASK 0x00030000UL
+#define EUR_CR_CLKGATECTL_IDXFIFO_CLKG_SHIFT 16
+#define EUR_CR_CLKGATECTL_TA_CLKG_MASK      0x000C0000UL
+#define EUR_CR_CLKGATECTL_TA_CLKG_SHIFT     18
+#define EUR_CR_CLKGATECTL_AUTO_MAN_REG_MASK 0x01000000UL
+#define EUR_CR_CLKGATECTL_AUTO_MAN_REG_SHIFT 24
+#define EUR_CR_CLKGATECTL_SYSTEM_CLKG_MASK  0x10000000UL
+#define EUR_CR_CLKGATECTL_SYSTEM_CLKG_SHIFT 28
+#define EUR_CR_CLKGATECTL2                  0x0004
+#define EUR_CR_CLKGATECTL2_PBE_CLKG_MASK    0x00000003UL
+#define EUR_CR_CLKGATECTL2_PBE_CLKG_SHIFT   0
+#define EUR_CR_CLKGATECTL2_CACHEL2_CLKG_MASK 0x0000000CUL
+#define EUR_CR_CLKGATECTL2_CACHEL2_CLKG_SHIFT 2
+#define EUR_CR_CLKGATECTL2_UCACHEL2_CLKG_MASK 0x00000030UL
+#define EUR_CR_CLKGATECTL2_UCACHEL2_CLKG_SHIFT 4
+#define EUR_CR_CLKGATECTL2_USE0_CLKG_MASK   0x000000C0UL
+#define EUR_CR_CLKGATECTL2_USE0_CLKG_SHIFT  6
+#define EUR_CR_CLKGATECTL2_ITR0_CLKG_MASK   0x00000300UL
+#define EUR_CR_CLKGATECTL2_ITR0_CLKG_SHIFT  8
+#define EUR_CR_CLKGATECTL2_TEX0_CLKG_MASK   0x00000C00UL
+#define EUR_CR_CLKGATECTL2_TEX0_CLKG_SHIFT  10
+#define EUR_CR_CLKGATECTL2_MADD0_CLKG_MASK  0x00003000UL
+#define EUR_CR_CLKGATECTL2_MADD0_CLKG_SHIFT 12
+#define EUR_CR_CLKGATECTL2_USE1_CLKG_MASK   0x0000C000UL
+#define EUR_CR_CLKGATECTL2_USE1_CLKG_SHIFT  14
+#define EUR_CR_CLKGATECTL2_ITR1_CLKG_MASK   0x00030000UL
+#define EUR_CR_CLKGATECTL2_ITR1_CLKG_SHIFT  16
+#define EUR_CR_CLKGATECTL2_TEX1_CLKG_MASK   0x000C0000UL
+#define EUR_CR_CLKGATECTL2_TEX1_CLKG_SHIFT  18
+#define EUR_CR_CLKGATECTL2_MADD1_CLKG_MASK  0x00300000UL
+#define EUR_CR_CLKGATECTL2_MADD1_CLKG_SHIFT 20
+#define EUR_CR_CLKGATESTATUS                0x0008
+#define EUR_CR_CLKGATESTATUS_ISP_CLKS_MASK  0x00000001UL
+#define EUR_CR_CLKGATESTATUS_ISP_CLKS_SHIFT 0
+#define EUR_CR_CLKGATESTATUS_ISP2_CLKS_MASK 0x00000002UL
+#define EUR_CR_CLKGATESTATUS_ISP2_CLKS_SHIFT 1
+#define EUR_CR_CLKGATESTATUS_TSP_CLKS_MASK  0x00000004UL
+#define EUR_CR_CLKGATESTATUS_TSP_CLKS_SHIFT 2
+#define EUR_CR_CLKGATESTATUS_TE_CLKS_MASK   0x00000008UL
+#define EUR_CR_CLKGATESTATUS_TE_CLKS_SHIFT  3
+#define EUR_CR_CLKGATESTATUS_MTE_CLKS_MASK  0x00000010UL
+#define EUR_CR_CLKGATESTATUS_MTE_CLKS_SHIFT 4
+#define EUR_CR_CLKGATESTATUS_DPM_CLKS_MASK  0x00000020UL
+#define EUR_CR_CLKGATESTATUS_DPM_CLKS_SHIFT 5
+#define EUR_CR_CLKGATESTATUS_VDM_CLKS_MASK  0x00000040UL
+#define EUR_CR_CLKGATESTATUS_VDM_CLKS_SHIFT 6
+#define EUR_CR_CLKGATESTATUS_PDS_CLKS_MASK  0x00000080UL
+#define EUR_CR_CLKGATESTATUS_PDS_CLKS_SHIFT 7
+#define EUR_CR_CLKGATESTATUS_PBE_CLKS_MASK  0x00000100UL
+#define EUR_CR_CLKGATESTATUS_PBE_CLKS_SHIFT 8
+#define EUR_CR_CLKGATESTATUS_CACHEL2_CLKS_MASK 0x00000200UL
+#define EUR_CR_CLKGATESTATUS_CACHEL2_CLKS_SHIFT 9
+#define EUR_CR_CLKGATESTATUS_UCACHEL2_CLKS_MASK 0x00000400UL
+#define EUR_CR_CLKGATESTATUS_UCACHEL2_CLKS_SHIFT 10
+#define EUR_CR_CLKGATESTATUS_USE0_CLKS_MASK 0x00000800UL
+#define EUR_CR_CLKGATESTATUS_USE0_CLKS_SHIFT 11
+#define EUR_CR_CLKGATESTATUS_ITR0_CLKS_MASK 0x00001000UL
+#define EUR_CR_CLKGATESTATUS_ITR0_CLKS_SHIFT 12
+#define EUR_CR_CLKGATESTATUS_TEX0_CLKS_MASK 0x00002000UL
+#define EUR_CR_CLKGATESTATUS_TEX0_CLKS_SHIFT 13
+#define EUR_CR_CLKGATESTATUS_MADD0_CLKS_MASK 0x00004000UL
+#define EUR_CR_CLKGATESTATUS_MADD0_CLKS_SHIFT 14
+#define EUR_CR_CLKGATESTATUS_USE1_CLKS_MASK 0x00008000UL
+#define EUR_CR_CLKGATESTATUS_USE1_CLKS_SHIFT 15
+#define EUR_CR_CLKGATESTATUS_ITR1_CLKS_MASK 0x00010000UL
+#define EUR_CR_CLKGATESTATUS_ITR1_CLKS_SHIFT 16
+#define EUR_CR_CLKGATESTATUS_TEX1_CLKS_MASK 0x00020000UL
+#define EUR_CR_CLKGATESTATUS_TEX1_CLKS_SHIFT 17
+#define EUR_CR_CLKGATESTATUS_MADD1_CLKS_MASK 0x00040000UL
+#define EUR_CR_CLKGATESTATUS_MADD1_CLKS_SHIFT 18
+#define EUR_CR_CLKGATESTATUS_IDXFIFO_CLKS_MASK 0x00080000UL
+#define EUR_CR_CLKGATESTATUS_IDXFIFO_CLKS_SHIFT 19
+#define EUR_CR_CLKGATESTATUS_TA_CLKS_MASK   0x00100000UL
+#define EUR_CR_CLKGATESTATUS_TA_CLKS_SHIFT  20
+#define EUR_CR_CLKGATECTLOVR                0x000C
+#define EUR_CR_CLKGATECTLOVR_ISP_CLKO_MASK  0x00000003UL
+#define EUR_CR_CLKGATECTLOVR_ISP_CLKO_SHIFT 0
+#define EUR_CR_CLKGATECTLOVR_ISP2_CLKO_MASK 0x0000000CUL
+#define EUR_CR_CLKGATECTLOVR_ISP2_CLKO_SHIFT 2
+#define EUR_CR_CLKGATECTLOVR_TSP_CLKO_MASK  0x00000030UL
+#define EUR_CR_CLKGATECTLOVR_TSP_CLKO_SHIFT 4
+#define EUR_CR_CLKGATECTLOVR_TE_CLKO_MASK   0x000000C0UL
+#define EUR_CR_CLKGATECTLOVR_TE_CLKO_SHIFT  6
+#define EUR_CR_CLKGATECTLOVR_MTE_CLKO_MASK  0x00000300UL
+#define EUR_CR_CLKGATECTLOVR_MTE_CLKO_SHIFT 8
+#define EUR_CR_CLKGATECTLOVR_DPM_CLKO_MASK  0x00000C00UL
+#define EUR_CR_CLKGATECTLOVR_DPM_CLKO_SHIFT 10
+#define EUR_CR_CLKGATECTLOVR_VDM_CLKO_MASK  0x00003000UL
+#define EUR_CR_CLKGATECTLOVR_VDM_CLKO_SHIFT 12
+#define EUR_CR_CLKGATECTLOVR_PDS_CLKO_MASK  0x0000C000UL
+#define EUR_CR_CLKGATECTLOVR_PDS_CLKO_SHIFT 14
+#define EUR_CR_CLKGATECTLOVR_IDXFIFO_CLKO_MASK 0x00030000UL
+#define EUR_CR_CLKGATECTLOVR_IDXFIFO_CLKO_SHIFT 16
+#define EUR_CR_CLKGATECTLOVR_TA_CLKO_MASK   0x000C0000UL
+#define EUR_CR_CLKGATECTLOVR_TA_CLKO_SHIFT  18
+#define EUR_CR_POWER                        0x001C
+#define EUR_CR_POWER_PIPE_DISABLE_MASK      0x00000001UL
+#define EUR_CR_POWER_PIPE_DISABLE_SHIFT     0
+#define EUR_CR_CORE_ID                      0x0020
+#define EUR_CR_CORE_ID_CONFIG_MASK          0x0000FFFFUL
+#define EUR_CR_CORE_ID_CONFIG_SHIFT         0
+#define EUR_CR_CORE_ID_ID_MASK              0xFFFF0000UL
+#define EUR_CR_CORE_ID_ID_SHIFT             16
+#define EUR_CR_CORE_REVISION                0x0024
+#define EUR_CR_CORE_REVISION_MAINTENANCE_MASK 0x000000FFUL
+#define EUR_CR_CORE_REVISION_MAINTENANCE_SHIFT 0
+#define EUR_CR_CORE_REVISION_MINOR_MASK     0x0000FF00UL
+#define EUR_CR_CORE_REVISION_MINOR_SHIFT    8
+#define EUR_CR_CORE_REVISION_MAJOR_MASK     0x00FF0000UL
+#define EUR_CR_CORE_REVISION_MAJOR_SHIFT    16
+#define EUR_CR_CORE_REVISION_DESIGNER_MASK  0xFF000000UL
+#define EUR_CR_CORE_REVISION_DESIGNER_SHIFT 24
+#define EUR_CR_DESIGNER_REV_FIELD1          0x0028
+#define EUR_CR_DESIGNER_REV_FIELD1_DESIGNER_REV_FIELD1_MASK 0xFFFFFFFFUL
+#define EUR_CR_DESIGNER_REV_FIELD1_DESIGNER_REV_FIELD1_SHIFT 0
+#define EUR_CR_DESIGNER_REV_FIELD2          0x002C
+#define EUR_CR_DESIGNER_REV_FIELD2_DESIGNER_REV_FIELD2_MASK 0xFFFFFFFFUL
+#define EUR_CR_DESIGNER_REV_FIELD2_DESIGNER_REV_FIELD2_SHIFT 0
+#define EUR_CR_SOFT_RESET                   0x0080
+#define EUR_CR_SOFT_RESET_BIF_RESET_MASK    0x00000001UL
+#define EUR_CR_SOFT_RESET_BIF_RESET_SHIFT   0
+#define EUR_CR_SOFT_RESET_VDM_RESET_MASK    0x00000002UL
+#define EUR_CR_SOFT_RESET_VDM_RESET_SHIFT   1
+#define EUR_CR_SOFT_RESET_DPM_RESET_MASK    0x00000004UL
+#define EUR_CR_SOFT_RESET_DPM_RESET_SHIFT   2
+#define EUR_CR_SOFT_RESET_TE_RESET_MASK     0x00000008UL
+#define EUR_CR_SOFT_RESET_TE_RESET_SHIFT    3
+#define EUR_CR_SOFT_RESET_MTE_RESET_MASK    0x00000010UL
+#define EUR_CR_SOFT_RESET_MTE_RESET_SHIFT   4
+#define EUR_CR_SOFT_RESET_ISP_RESET_MASK    0x00000020UL
+#define EUR_CR_SOFT_RESET_ISP_RESET_SHIFT   5
+#define EUR_CR_SOFT_RESET_ISP2_RESET_MASK   0x00000040UL
+#define EUR_CR_SOFT_RESET_ISP2_RESET_SHIFT  6
+#define EUR_CR_SOFT_RESET_TSP_RESET_MASK    0x00000080UL
+#define EUR_CR_SOFT_RESET_TSP_RESET_SHIFT   7
+#define EUR_CR_SOFT_RESET_PDS_RESET_MASK    0x00000100UL
+#define EUR_CR_SOFT_RESET_PDS_RESET_SHIFT   8
+#define EUR_CR_SOFT_RESET_PBE_RESET_MASK    0x00000200UL
+#define EUR_CR_SOFT_RESET_PBE_RESET_SHIFT   9
+#define EUR_CR_SOFT_RESET_CACHEL2_RESET_MASK 0x00000400UL
+#define EUR_CR_SOFT_RESET_CACHEL2_RESET_SHIFT 10
+#define EUR_CR_SOFT_RESET_UCACHEL2_RESET_MASK 0x00000800UL
+#define EUR_CR_SOFT_RESET_UCACHEL2_RESET_SHIFT 11
+#define EUR_CR_SOFT_RESET_MADD_RESET_MASK   0x00001000UL
+#define EUR_CR_SOFT_RESET_MADD_RESET_SHIFT  12
+#define EUR_CR_SOFT_RESET_ITR_RESET_MASK    0x00002000UL
+#define EUR_CR_SOFT_RESET_ITR_RESET_SHIFT   13
+#define EUR_CR_SOFT_RESET_TEX_RESET_MASK    0x00004000UL
+#define EUR_CR_SOFT_RESET_TEX_RESET_SHIFT   14
+#define EUR_CR_SOFT_RESET_USE_RESET_MASK    0x00008000UL
+#define EUR_CR_SOFT_RESET_USE_RESET_SHIFT   15
+#define EUR_CR_SOFT_RESET_IDXFIFO_RESET_MASK 0x00010000UL
+#define EUR_CR_SOFT_RESET_IDXFIFO_RESET_SHIFT 16
+#define EUR_CR_SOFT_RESET_TA_RESET_MASK     0x00020000UL
+#define EUR_CR_SOFT_RESET_TA_RESET_SHIFT    17
+#define EUR_CR_EVENT_HOST_ENABLE2           0x0110
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_TA_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_TA_SHIFT 4
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_3D_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_3D_SHIFT 3
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_DL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_DL_SHIFT 2
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_HOST_CLEAR2            0x0114
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_TA_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_TA_SHIFT 4
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_3D_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_3D_SHIFT 3
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_DL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_DL_SHIFT 2
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_STATUS2                0x0118
+#define EUR_CR_EVENT_STATUS2_TRIG_TA_MASK   0x00000010UL
+#define EUR_CR_EVENT_STATUS2_TRIG_TA_SHIFT  4
+#define EUR_CR_EVENT_STATUS2_TRIG_3D_MASK   0x00000008UL
+#define EUR_CR_EVENT_STATUS2_TRIG_3D_SHIFT  3
+#define EUR_CR_EVENT_STATUS2_TRIG_DL_MASK   0x00000004UL
+#define EUR_CR_EVENT_STATUS2_TRIG_DL_SHIFT  2
+#define EUR_CR_EVENT_STATUS2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_STATUS2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_STATUS2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_STATUS2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_STATUS                 0x012CUL
+#define EUR_CR_EVENT_STATUS_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_STATUS_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_STATUS_TIMER_MASK      0x20000000UL
+#define EUR_CR_EVENT_STATUS_TIMER_SHIFT     29
+#define EUR_CR_EVENT_STATUS_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_STATUS_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_STATUS_TWOD_COMPLETE_MASK 0x08000000UL
+#define EUR_CR_EVENT_STATUS_TWOD_COMPLETE_SHIFT 27
+#define EUR_CR_EVENT_STATUS_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_STATUS_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_STATUS_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_STATUS_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_STATUS_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_STATUS_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_STATUS_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_STATUS_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_STATUS_OTPM_LOADED_MASK 0x00200000UL
+#define EUR_CR_EVENT_STATUS_OTPM_LOADED_SHIFT 21
+#define EUR_CR_EVENT_STATUS_OTPM_INV_MASK   0x00100000UL
+#define EUR_CR_EVENT_STATUS_OTPM_INV_SHIFT  20
+#define EUR_CR_EVENT_STATUS_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_STATUS_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_STATUS_ISP_HALT_MASK   0x00020000UL
+#define EUR_CR_EVENT_STATUS_ISP_HALT_SHIFT  17
+#define EUR_CR_EVENT_STATUS_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_STATUS_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_STATUS_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_STATUS_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_STATUS_SW_EVENT_MASK   0x00004000UL
+#define EUR_CR_EVENT_STATUS_SW_EVENT_SHIFT  14
+#define EUR_CR_EVENT_STATUS_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_STATUS_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_STATUS_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_STATUS_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_STATUS_TPC_CLEAR_MASK  0x00000800UL
+#define EUR_CR_EVENT_STATUS_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_STATUS_TPC_FLUSH_MASK  0x00000400UL
+#define EUR_CR_EVENT_STATUS_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_STATUS_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_STATUS_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_STATUS_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_STATUS_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_STATUS_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_EVENT_HOST_ENABLE            0x0130
+#define EUR_CR_EVENT_HOST_ENABLE_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_HOST_ENABLE_TIMER_MASK 0x20000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TIMER_SHIFT 29
+#define EUR_CR_EVENT_HOST_ENABLE_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_HOST_ENABLE_TWOD_COMPLETE_MASK 0x08000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TWOD_COMPLETE_SHIFT 27
+#define EUR_CR_EVENT_HOST_ENABLE_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_LOADED_MASK 0x00200000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_LOADED_SHIFT 21
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_INV_MASK 0x00100000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_INV_SHIFT 20
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_HOST_ENABLE_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_HOST_ENABLE_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_HALT_MASK 0x00020000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_HALT_SHIFT 17
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_HOST_ENABLE_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_HOST_ENABLE_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_HOST_ENABLE_SW_EVENT_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_ENABLE_SW_EVENT_SHIFT 14
+#define EUR_CR_EVENT_HOST_ENABLE_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_HOST_ENABLE_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_CLEAR_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_FLUSH_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_EVENT_HOST_CLEAR             0x0134
+#define EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_HOST_CLEAR_TIMER_MASK  0x20000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TIMER_SHIFT 29
+#define EUR_CR_EVENT_HOST_CLEAR_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_HOST_CLEAR_TWOD_COMPLETE_MASK 0x08000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TWOD_COMPLETE_SHIFT 27
+#define EUR_CR_EVENT_HOST_CLEAR_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_LOADED_MASK 0x00200000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_LOADED_SHIFT 21
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_INV_MASK 0x00100000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_INV_SHIFT 20
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_HOST_CLEAR_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_HOST_CLEAR_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_HALT_MASK 0x00020000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_HALT_SHIFT 17
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_HOST_CLEAR_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_HOST_CLEAR_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_SHIFT 14
+#define EUR_CR_EVENT_HOST_CLEAR_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_HOST_CLEAR_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_CLEAR_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_FLUSH_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_TIMER                        0x0144
+#define EUR_CR_TIMER_VALUE_MASK             0xFFFFFFFFUL
+#define EUR_CR_TIMER_VALUE_SHIFT            0
+#define EUR_CR_EVENT_KICK1                  0x0AB0
+#define EUR_CR_EVENT_KICK1_NOW_MASK         0x000000FFUL
+#define EUR_CR_EVENT_KICK1_NOW_SHIFT        0
+#define EUR_CR_PDS_EXEC_BASE                0x0AB8
+#define EUR_CR_PDS_EXEC_BASE_ADDR_MASK      0x0FF00000UL
+#define EUR_CR_PDS_EXEC_BASE_ADDR_SHIFT     20
+#define EUR_CR_EVENT_KICK2                  0x0AC0
+#define EUR_CR_EVENT_KICK2_NOW_MASK         0x00000001UL
+#define EUR_CR_EVENT_KICK2_NOW_SHIFT        0
+#define EUR_CR_EVENT_KICKER                 0x0AC4
+#define EUR_CR_EVENT_KICKER_ADDRESS_MASK    0x0FFFFFF0UL
+#define EUR_CR_EVENT_KICKER_ADDRESS_SHIFT   4
+#define EUR_CR_EVENT_KICK                   0x0AC8
+#define EUR_CR_EVENT_KICK_NOW_MASK          0x00000001UL
+#define EUR_CR_EVENT_KICK_NOW_SHIFT         0
+#define EUR_CR_EVENT_TIMER                  0x0ACC
+#define EUR_CR_EVENT_TIMER_ENABLE_MASK      0x01000000UL
+#define EUR_CR_EVENT_TIMER_ENABLE_SHIFT     24
+#define EUR_CR_EVENT_TIMER_VALUE_MASK       0x00FFFFFFUL
+#define EUR_CR_EVENT_TIMER_VALUE_SHIFT      0
+#define EUR_CR_PDS_INV0                     0x0AD0
+#define EUR_CR_PDS_INV0_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV0_DSC_SHIFT           0
+#define EUR_CR_PDS_INV1                     0x0AD4
+#define EUR_CR_PDS_INV1_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV1_DSC_SHIFT           0
+#define EUR_CR_EVENT_KICK3                  0x0AD8
+#define EUR_CR_EVENT_KICK3_NOW_MASK         0x00000001UL
+#define EUR_CR_EVENT_KICK3_NOW_SHIFT        0
+#define EUR_CR_PDS_INV3                     0x0ADC
+#define EUR_CR_PDS_INV3_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV3_DSC_SHIFT           0
+#define EUR_CR_PDS_INV_CSC                  0x0AE0
+#define EUR_CR_PDS_INV_CSC_KICK_MASK        0x00000001UL
+#define EUR_CR_PDS_INV_CSC_KICK_SHIFT       0
+#define EUR_CR_PDS_PC_BASE                  0x0B2C
+#define EUR_CR_PDS_PC_BASE_ADDRESS_MASK     0x00FFFFFFUL
+#define EUR_CR_PDS_PC_BASE_ADDRESS_SHIFT    0
+#define EUR_CR_BIF_CTRL                     0x0C00
+#define EUR_CR_BIF_CTRL_NOREORDER_MASK      0x00000001UL
+#define EUR_CR_BIF_CTRL_NOREORDER_SHIFT     0
+#define EUR_CR_BIF_CTRL_PAUSE_MASK          0x00000002UL
+#define EUR_CR_BIF_CTRL_PAUSE_SHIFT         1
+#define EUR_CR_BIF_CTRL_FLUSH_MASK          0x00000004UL
+#define EUR_CR_BIF_CTRL_FLUSH_SHIFT         2
+#define EUR_CR_BIF_CTRL_INVALDC_MASK        0x00000008UL
+#define EUR_CR_BIF_CTRL_INVALDC_SHIFT       3
+#define EUR_CR_BIF_CTRL_CLEAR_FAULT_MASK    0x00000010UL
+#define EUR_CR_BIF_CTRL_CLEAR_FAULT_SHIFT   4
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_CACHE_MASK 0x00000100UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_CACHE_SHIFT 8
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_VDM_MASK 0x00000200UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_VDM_SHIFT 9
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TE_MASK  0x00000400UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TE_SHIFT 10
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_PBE_MASK 0x00001000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_PBE_SHIFT 12
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TSPP_MASK 0x00002000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TSPP_SHIFT 13
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_ISP_MASK 0x00004000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_ISP_SHIFT 14
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_USE_MASK 0x00008000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_USE_SHIFT 15
+#define EUR_CR_BIF_INT_STAT                 0x0C04
+#define EUR_CR_BIF_INT_STAT_FAULT_MASK      0x00003FFFUL
+#define EUR_CR_BIF_INT_STAT_FAULT_SHIFT     0
+#define EUR_CR_BIF_INT_STAT_PF_N_RW_MASK    0x00004000UL
+#define EUR_CR_BIF_INT_STAT_PF_N_RW_SHIFT   14
+#define EUR_CR_BIF_INT_STAT_FLUSH_COMPLETE_MASK 0x00008000UL
+#define EUR_CR_BIF_INT_STAT_FLUSH_COMPLETE_SHIFT 15
+#define EUR_CR_BIF_FAULT                    0x0C08
+#define EUR_CR_BIF_FAULT_SB_MASK            0x000001F0UL
+#define EUR_CR_BIF_FAULT_SB_SHIFT           4
+#define EUR_CR_BIF_FAULT_ADDR_MASK          0x0FFFF000UL
+#define EUR_CR_BIF_FAULT_ADDR_SHIFT         12
+#define EUR_CR_BIF_DIR_LIST_BASE0           0x0C84
+#define EUR_CR_BIF_DIR_LIST_BASE0_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE0_ADDR_SHIFT 12
+#define EUR_CR_BIF_TA_REQ_BASE              0x0C90
+#define EUR_CR_BIF_TA_REQ_BASE_ADDR_MASK    0x0FF00000UL
+#define EUR_CR_BIF_TA_REQ_BASE_ADDR_SHIFT   20
+#define EUR_CR_BIF_MEM_REQ_STAT             0x0CA8
+#define EUR_CR_BIF_MEM_REQ_STAT_READS_MASK  0x000000FFUL
+#define EUR_CR_BIF_MEM_REQ_STAT_READS_SHIFT 0
+#define EUR_CR_BIF_3D_REQ_BASE              0x0CAC
+#define EUR_CR_BIF_3D_REQ_BASE_ADDR_MASK    0x0FF00000UL
+#define EUR_CR_BIF_3D_REQ_BASE_ADDR_SHIFT   20
+#define EUR_CR_BIF_ZLS_REQ_BASE             0x0CB0
+#define EUR_CR_BIF_ZLS_REQ_BASE_ADDR_MASK   0x0FF00000UL
+#define EUR_CR_BIF_ZLS_REQ_BASE_ADDR_SHIFT  20
+#define EUR_CR_2D_BLIT_STATUS               0x0E04
+#define EUR_CR_2D_BLIT_STATUS_COMPLETE_MASK 0x00FFFFFFUL
+#define EUR_CR_2D_BLIT_STATUS_COMPLETE_SHIFT 0
+#define EUR_CR_2D_BLIT_STATUS_BUSY_MASK     0x01000000UL
+#define EUR_CR_2D_BLIT_STATUS_BUSY_SHIFT    24
+#define EUR_CR_2D_VIRTUAL_FIFO_0            0x0E10
+#define EUR_CR_2D_VIRTUAL_FIFO_0_ENABLE_MASK 0x00000001UL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_ENABLE_SHIFT 0
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_MASK 0x0000000EUL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_SHIFT 1
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_DIV_MASK 0x00000FF0UL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_DIV_SHIFT 4
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_MUL_MASK 0x0000F000UL
+#define EUR_CR_2D_VIRTUAL_FIFO_0_FLOWRATE_MUL_SHIFT 12
+#define EUR_CR_2D_VIRTUAL_FIFO_1            0x0E14
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_ACC_MASK 0x00000FFFUL
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_ACC_SHIFT 0
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MAX_ACC_MASK 0x00FFF000UL
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MAX_ACC_SHIFT 12
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_METRIC_MASK 0xFF000000UL
+#define EUR_CR_2D_VIRTUAL_FIFO_1_MIN_METRIC_SHIFT 24
+#define EUR_CR_USE_CODE_BASE(X)     (0x0A0C + (4 * (X)))
+#define EUR_CR_USE_CODE_BASE_ADDR_MASK      0x00FFFFFFUL
+#define EUR_CR_USE_CODE_BASE_ADDR_SHIFT     0
+#define EUR_CR_USE_CODE_BASE_DM_MASK        0x03000000UL
+#define EUR_CR_USE_CODE_BASE_DM_SHIFT       24
+#define EUR_CR_USE_CODE_BASE_SIZE_UINT32 16
+#define EUR_CR_USE_CODE_BASE_NUM_ENTRIES 16
+#define EUR_CR_MNE_CR_CTRL						0x0D00
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_N_MASK		0x00010000UL
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_N_SHIFT		16
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_MASK			0x00008000UL
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_SHIFT			15
+#define EUR_CR_MNE_CR_CTRL_USE_INVAL_ADDR_MASK	0x00007800UL
+#define EUR_CR_MNE_CR_CTRL_USE_INVAL_ADDR_SHIFT	11
+#define EUR_CR_MNE_CR_CTRL_BYPASS_ALL_MASK		0x00000400UL
+#define EUR_CR_MNE_CR_CTRL_BYPASS_ALL_SHIFT		10
+#define EUR_CR_MNE_CR_CTRL_BYPASS_MASK			0x000003E0UL
+#define EUR_CR_MNE_CR_CTRL_BYPASS_SHIFT			5
+#define EUR_CR_MNE_CR_CTRL_PAUSE_MASK			0x00000010UL
+#define EUR_CR_MNE_CR_CTRL_PAUSE_SHIFT			4
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_MASK		0x0000000EUL
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT		1
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_PDS_MASK (1UL<<EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT+2)
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_USEC_MASK (1UL<<EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT+1)
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_CACHE_MASK (1UL<<EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT)
+#define EUR_CR_MNE_CR_CTRL_INVAL_ALL_MASK		0x00000001UL
+#define EUR_CR_MNE_CR_CTRL_INVAL_ALL_SHIFT		0
+#define EUR_CR_MNE_CR_USE_INVAL					0x0D04
+#define EUR_CR_MNE_CR_USE_INVAL_ADDR_MASK		0xFFFFFFFFUL
+#define EUR_CR_MNE_CR_USE_INVAL_ADDR_SHIFT		0
+#define EUR_CR_MNE_CR_STAT					0x0D08
+#define EUR_CR_MNE_CR_STAT_PAUSED_MASK		0x00000400UL
+#define EUR_CR_MNE_CR_STAT_PAUSED_SHIFT		10
+#define EUR_CR_MNE_CR_STAT_READS_MASK		0x000003FFUL
+#define EUR_CR_MNE_CR_STAT_READS_SHIFT		0
+#define EUR_CR_MNE_CR_STAT_STATS			0x0D0C
+#define EUR_CR_MNE_CR_STAT_STATS_RST_MASK	0x000FFFF0UL
+#define EUR_CR_MNE_CR_STAT_STATS_RST_SHIFT	4
+#define EUR_CR_MNE_CR_STAT_STATS_SEL_MASK	0x0000000FUL
+#define EUR_CR_MNE_CR_STAT_STATS_SEL_SHIFT	0
+#define EUR_CR_MNE_CR_STAT_STATS_OUT				0x0D10
+#define EUR_CR_MNE_CR_STAT_STATS_OUT_VALUE_MASK		0xFFFFFFFFUL
+#define EUR_CR_MNE_CR_STAT_STATS_OUT_VALUE_SHIFT	0
+#define EUR_CR_MNE_CR_EVENT_STATUS				0x0D14
+#define EUR_CR_MNE_CR_EVENT_STATUS_INVAL_MASK	0x00000001UL
+#define EUR_CR_MNE_CR_EVENT_STATUS_INVAL_SHIFT	0
+#define EUR_CR_MNE_CR_EVENT_CLEAR				0x0D18
+#define EUR_CR_MNE_CR_EVENT_CLEAR_INVAL_MASK	0x00000001UL
+#define EUR_CR_MNE_CR_EVENT_CLEAR_INVAL_SHIFT	0
+#define EUR_CR_MNE_CR_CTRL_INVAL				0x0D20
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx545defs.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx545defs.h
new file mode 100755
index 0000000..cad51f5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgx545defs.h
@@ -0,0 +1,828 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SGX545DEFS_KM_H_
+#define _SGX545DEFS_KM_H_
+
+#define EUR_CR_CLKGATECTL                   0x0000
+#define EUR_CR_CLKGATECTL_ISP_CLKG_MASK     0x00000003UL
+#define EUR_CR_CLKGATECTL_ISP_CLKG_SHIFT    0
+#define EUR_CR_CLKGATECTL_ISP2_CLKG_MASK    0x0000000CUL
+#define EUR_CR_CLKGATECTL_ISP2_CLKG_SHIFT   2
+#define EUR_CR_CLKGATECTL_TSP_CLKG_MASK     0x00000030UL
+#define EUR_CR_CLKGATECTL_TSP_CLKG_SHIFT    4
+#define EUR_CR_CLKGATECTL_TE_CLKG_MASK      0x000000C0UL
+#define EUR_CR_CLKGATECTL_TE_CLKG_SHIFT     6
+#define EUR_CR_CLKGATECTL_MTE_CLKG_MASK     0x00000300UL
+#define EUR_CR_CLKGATECTL_MTE_CLKG_SHIFT    8
+#define EUR_CR_CLKGATECTL_DPM_CLKG_MASK     0x00000C00UL
+#define EUR_CR_CLKGATECTL_DPM_CLKG_SHIFT    10
+#define EUR_CR_CLKGATECTL_VDM_CLKG_MASK     0x00003000UL
+#define EUR_CR_CLKGATECTL_VDM_CLKG_SHIFT    12
+#define EUR_CR_CLKGATECTL_PDS0_CLKG_MASK    0x0000C000UL
+#define EUR_CR_CLKGATECTL_PDS0_CLKG_SHIFT   14
+#define EUR_CR_CLKGATECTL_AUTO_MAN_REG_MASK 0x01000000UL
+#define EUR_CR_CLKGATECTL_AUTO_MAN_REG_SHIFT 24
+#define EUR_CR_CLKGATECTL2                  0x0004
+#define EUR_CR_CLKGATECTL2_PBE_CLKG_MASK    0x00000003UL
+#define EUR_CR_CLKGATECTL2_PBE_CLKG_SHIFT   0
+#define EUR_CR_CLKGATECTL2_CACHEL2_CLKG_MASK 0x0000000CUL
+#define EUR_CR_CLKGATECTL2_CACHEL2_CLKG_SHIFT 2
+#define EUR_CR_CLKGATECTL2_UCACHEL2_CLKG_MASK 0x00000030UL
+#define EUR_CR_CLKGATECTL2_UCACHEL2_CLKG_SHIFT 4
+#define EUR_CR_CLKGATECTL2_USE0_CLKG_MASK   0x000000C0UL
+#define EUR_CR_CLKGATECTL2_USE0_CLKG_SHIFT  6
+#define EUR_CR_CLKGATECTL2_ITR0_CLKG_MASK   0x00000300UL
+#define EUR_CR_CLKGATECTL2_ITR0_CLKG_SHIFT  8
+#define EUR_CR_CLKGATECTL2_TEX0_CLKG_MASK   0x00000C00UL
+#define EUR_CR_CLKGATECTL2_TEX0_CLKG_SHIFT  10
+#define EUR_CR_CLKGATECTL2_MADD0_CLKG_MASK  0x00003000UL
+#define EUR_CR_CLKGATECTL2_MADD0_CLKG_SHIFT 12
+#define EUR_CR_CLKGATECTL2_USE1_CLKG_MASK   0x0000C000UL
+#define EUR_CR_CLKGATECTL2_USE1_CLKG_SHIFT  14
+#define EUR_CR_CLKGATECTL2_ITR1_CLKG_MASK   0x00030000UL
+#define EUR_CR_CLKGATECTL2_ITR1_CLKG_SHIFT  16
+#define EUR_CR_CLKGATECTL2_TEX1_CLKG_MASK   0x000C0000UL
+#define EUR_CR_CLKGATECTL2_TEX1_CLKG_SHIFT  18
+#define EUR_CR_CLKGATECTL2_MADD1_CLKG_MASK  0x00300000UL
+#define EUR_CR_CLKGATECTL2_MADD1_CLKG_SHIFT 20
+#define EUR_CR_CLKGATECTL2_PDS1_CLKG_MASK   0x00C00000UL
+#define EUR_CR_CLKGATECTL2_PDS1_CLKG_SHIFT  22
+#define EUR_CR_CLKGATESTATUS                0x0008
+#define EUR_CR_CLKGATESTATUS_ISP_CLKS_MASK  0x00000001UL
+#define EUR_CR_CLKGATESTATUS_ISP_CLKS_SHIFT 0
+#define EUR_CR_CLKGATESTATUS_ISP2_CLKS_MASK 0x00000002UL
+#define EUR_CR_CLKGATESTATUS_ISP2_CLKS_SHIFT 1
+#define EUR_CR_CLKGATESTATUS_TSP_CLKS_MASK  0x00000004UL
+#define EUR_CR_CLKGATESTATUS_TSP_CLKS_SHIFT 2
+#define EUR_CR_CLKGATESTATUS_TE_CLKS_MASK   0x00000008UL
+#define EUR_CR_CLKGATESTATUS_TE_CLKS_SHIFT  3
+#define EUR_CR_CLKGATESTATUS_MTE_CLKS_MASK  0x00000010UL
+#define EUR_CR_CLKGATESTATUS_MTE_CLKS_SHIFT 4
+#define EUR_CR_CLKGATESTATUS_DPM_CLKS_MASK  0x00000020UL
+#define EUR_CR_CLKGATESTATUS_DPM_CLKS_SHIFT 5
+#define EUR_CR_CLKGATESTATUS_VDM_CLKS_MASK  0x00000040UL
+#define EUR_CR_CLKGATESTATUS_VDM_CLKS_SHIFT 6
+#define EUR_CR_CLKGATESTATUS_PDS0_CLKS_MASK 0x00000080UL
+#define EUR_CR_CLKGATESTATUS_PDS0_CLKS_SHIFT 7
+#define EUR_CR_CLKGATESTATUS_PBE_CLKS_MASK  0x00000100UL
+#define EUR_CR_CLKGATESTATUS_PBE_CLKS_SHIFT 8
+#define EUR_CR_CLKGATESTATUS_CACHEL2_CLKS_MASK 0x00000200UL
+#define EUR_CR_CLKGATESTATUS_CACHEL2_CLKS_SHIFT 9
+#define EUR_CR_CLKGATESTATUS_UCACHEL2_CLKS_MASK 0x00000400UL
+#define EUR_CR_CLKGATESTATUS_UCACHEL2_CLKS_SHIFT 10
+#define EUR_CR_CLKGATESTATUS_USE0_CLKS_MASK 0x00000800UL
+#define EUR_CR_CLKGATESTATUS_USE0_CLKS_SHIFT 11
+#define EUR_CR_CLKGATESTATUS_ITR0_CLKS_MASK 0x00001000UL
+#define EUR_CR_CLKGATESTATUS_ITR0_CLKS_SHIFT 12
+#define EUR_CR_CLKGATESTATUS_TEX0_CLKS_MASK 0x00002000UL
+#define EUR_CR_CLKGATESTATUS_TEX0_CLKS_SHIFT 13
+#define EUR_CR_CLKGATESTATUS_MADD0_CLKS_MASK 0x00004000UL
+#define EUR_CR_CLKGATESTATUS_MADD0_CLKS_SHIFT 14
+#define EUR_CR_CLKGATESTATUS_USE1_CLKS_MASK 0x00008000UL
+#define EUR_CR_CLKGATESTATUS_USE1_CLKS_SHIFT 15
+#define EUR_CR_CLKGATESTATUS_ITR1_CLKS_MASK 0x00010000UL
+#define EUR_CR_CLKGATESTATUS_ITR1_CLKS_SHIFT 16
+#define EUR_CR_CLKGATESTATUS_TEX1_CLKS_MASK 0x00020000UL
+#define EUR_CR_CLKGATESTATUS_TEX1_CLKS_SHIFT 17
+#define EUR_CR_CLKGATESTATUS_MADD1_CLKS_MASK 0x00040000UL
+#define EUR_CR_CLKGATESTATUS_MADD1_CLKS_SHIFT 18
+#define EUR_CR_CLKGATESTATUS_PDS1_CLKS_MASK 0x00080000UL
+#define EUR_CR_CLKGATESTATUS_PDS1_CLKS_SHIFT 19
+#define EUR_CR_CLKGATECTLOVR                0x000C
+#define EUR_CR_CLKGATECTLOVR_ISP_CLKO_MASK  0x00000003UL
+#define EUR_CR_CLKGATECTLOVR_ISP_CLKO_SHIFT 0
+#define EUR_CR_CLKGATECTLOVR_ISP2_CLKO_MASK 0x0000000CUL
+#define EUR_CR_CLKGATECTLOVR_ISP2_CLKO_SHIFT 2
+#define EUR_CR_CLKGATECTLOVR_TSP_CLKO_MASK  0x00000030UL
+#define EUR_CR_CLKGATECTLOVR_TSP_CLKO_SHIFT 4
+#define EUR_CR_CLKGATECTLOVR_TE_CLKO_MASK   0x000000C0UL
+#define EUR_CR_CLKGATECTLOVR_TE_CLKO_SHIFT  6
+#define EUR_CR_CLKGATECTLOVR_MTE_CLKO_MASK  0x00000300UL
+#define EUR_CR_CLKGATECTLOVR_MTE_CLKO_SHIFT 8
+#define EUR_CR_CLKGATECTLOVR_DPM_CLKO_MASK  0x00000C00UL
+#define EUR_CR_CLKGATECTLOVR_DPM_CLKO_SHIFT 10
+#define EUR_CR_CLKGATECTLOVR_VDM_CLKO_MASK  0x00003000UL
+#define EUR_CR_CLKGATECTLOVR_VDM_CLKO_SHIFT 12
+#define EUR_CR_CLKGATECTLOVR_PDS0_CLKO_MASK 0x0000C000UL
+#define EUR_CR_CLKGATECTLOVR_PDS0_CLKO_SHIFT 14
+#define EUR_CR_CORE_ID                      0x001C
+#define EUR_CR_CORE_ID_CONFIG_MASK          0x0000FFFFUL
+#define EUR_CR_CORE_ID_CONFIG_SHIFT         0
+#define EUR_CR_CORE_ID_ID_MASK              0xFFFF0000UL
+#define EUR_CR_CORE_ID_ID_SHIFT             16
+#define EUR_CR_CORE_REVISION                0x0020
+#define EUR_CR_CORE_REVISION_MAINTENANCE_MASK 0x000000FFUL
+#define EUR_CR_CORE_REVISION_MAINTENANCE_SHIFT 0
+#define EUR_CR_CORE_REVISION_MINOR_MASK     0x0000FF00UL
+#define EUR_CR_CORE_REVISION_MINOR_SHIFT    8
+#define EUR_CR_CORE_REVISION_MAJOR_MASK     0x00FF0000UL
+#define EUR_CR_CORE_REVISION_MAJOR_SHIFT    16
+#define EUR_CR_CORE_REVISION_DESIGNER_MASK  0xFF000000UL
+#define EUR_CR_CORE_REVISION_DESIGNER_SHIFT 24
+#define EUR_CR_DESIGNER_REV_FIELD1          0x0024
+#define EUR_CR_DESIGNER_REV_FIELD1_DESIGNER_REV_FIELD1_MASK 0xFFFFFFFFUL
+#define EUR_CR_DESIGNER_REV_FIELD1_DESIGNER_REV_FIELD1_SHIFT 0
+#define EUR_CR_DESIGNER_REV_FIELD2          0x002C
+#define EUR_CR_DESIGNER_REV_FIELD2_DESIGNER_REV_FIELD2_MASK 0xFFFFFFFFUL
+#define EUR_CR_DESIGNER_REV_FIELD2_DESIGNER_REV_FIELD2_SHIFT 0
+#define EUR_CR_SOFT_RESET                   0x0080
+#define EUR_CR_SOFT_RESET_BIF_RESET_MASK    0x00000001UL
+#define EUR_CR_SOFT_RESET_BIF_RESET_SHIFT   0
+#define EUR_CR_SOFT_RESET_DPM_RESET_MASK    0x00000002UL
+#define EUR_CR_SOFT_RESET_DPM_RESET_SHIFT   1
+#define EUR_CR_SOFT_RESET_TA_RESET_MASK     0x00000004UL
+#define EUR_CR_SOFT_RESET_TA_RESET_SHIFT    2
+#define EUR_CR_SOFT_RESET_USE_RESET_MASK    0x00000008UL
+#define EUR_CR_SOFT_RESET_USE_RESET_SHIFT   3
+#define EUR_CR_SOFT_RESET_ISP_RESET_MASK    0x00000010UL
+#define EUR_CR_SOFT_RESET_ISP_RESET_SHIFT   4
+#define EUR_CR_SOFT_RESET_TSP_RESET_MASK    0x00000020UL
+#define EUR_CR_SOFT_RESET_TSP_RESET_SHIFT   5
+#define EUR_CR_EVENT_HOST_ENABLE2           0x0110
+#define EUR_CR_EVENT_HOST_ENABLE2_VDM_CONTEXT_LOAD_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_ENABLE2_VDM_CONTEXT_LOAD_SHIFT 14
+#define EUR_CR_EVENT_HOST_ENABLE2_VDM_TASK_KICKED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_ENABLE2_VDM_TASK_KICKED_SHIFT 13
+#define EUR_CR_EVENT_HOST_ENABLE2_OTPM_MEM_CLEARED_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_ENABLE2_OTPM_MEM_CLEARED_SHIFT 12
+#define EUR_CR_EVENT_HOST_ENABLE2_OTPM_FLUSHED_INV_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_ENABLE2_OTPM_FLUSHED_INV_SHIFT 11
+#define EUR_CR_EVENT_HOST_ENABLE2_DCU_INVALCOMPLETE_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DCU_INVALCOMPLETE_SHIFT 10
+#define EUR_CR_EVENT_HOST_ENABLE2_GSG_FLUSHED_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_ENABLE2_GSG_FLUSHED_SHIFT 9
+#define EUR_CR_EVENT_HOST_ENABLE2_GSG_LOADED_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_ENABLE2_GSG_LOADED_SHIFT 8
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_TA_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_TA_SHIFT 7
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_3D_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_3D_SHIFT 6
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_DL_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_ENABLE2_TRIG_DL_SHIFT 5
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_DHOST_FREE_LOAD_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_DHOST_FREE_LOAD_SHIFT 3
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_HOST_FREE_LOAD_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_HOST_FREE_LOAD_SHIFT 2
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_ENABLE2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_HOST_CLEAR2            0x0114
+#define EUR_CR_EVENT_HOST_CLEAR2_VDM_CONTEXT_LOAD_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_CLEAR2_VDM_CONTEXT_LOAD_SHIFT 14
+#define EUR_CR_EVENT_HOST_CLEAR2_VDM_TASK_KICKED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_CLEAR2_VDM_TASK_KICKED_SHIFT 13
+#define EUR_CR_EVENT_HOST_CLEAR2_OTPM_MEM_CLEARED_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_CLEAR2_OTPM_MEM_CLEARED_SHIFT 12
+#define EUR_CR_EVENT_HOST_CLEAR2_OTPM_FLUSHED_INV_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_CLEAR2_OTPM_FLUSHED_INV_SHIFT 11
+#define EUR_CR_EVENT_HOST_CLEAR2_DCU_INVALCOMPLETE_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DCU_INVALCOMPLETE_SHIFT 10
+#define EUR_CR_EVENT_HOST_CLEAR2_GSG_FLUSHED_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_CLEAR2_GSG_FLUSHED_SHIFT 9
+#define EUR_CR_EVENT_HOST_CLEAR2_GSG_LOADED_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_CLEAR2_GSG_LOADED_SHIFT 8
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_TA_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_TA_SHIFT 7
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_3D_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_3D_SHIFT 6
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_DL_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_CLEAR2_TRIG_DL_SHIFT 5
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_DHOST_FREE_LOAD_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_DHOST_FREE_LOAD_SHIFT 3
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_HOST_FREE_LOAD_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_HOST_FREE_LOAD_SHIFT 2
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_CLEAR2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_STATUS2                0x0118
+#define EUR_CR_EVENT_STATUS2_VDM_CONTEXT_LOAD_MASK 0x00004000UL
+#define EUR_CR_EVENT_STATUS2_VDM_CONTEXT_LOAD_SHIFT 14
+#define EUR_CR_EVENT_STATUS2_VDM_TASK_KICKED_MASK 0x00002000UL
+#define EUR_CR_EVENT_STATUS2_VDM_TASK_KICKED_SHIFT 13
+#define EUR_CR_EVENT_STATUS2_OTPM_MEM_CLEARED_MASK 0x00001000UL
+#define EUR_CR_EVENT_STATUS2_OTPM_MEM_CLEARED_SHIFT 12
+#define EUR_CR_EVENT_STATUS2_OTPM_FLUSHED_INV_MASK 0x00000800UL
+#define EUR_CR_EVENT_STATUS2_OTPM_FLUSHED_INV_SHIFT 11
+#define EUR_CR_EVENT_STATUS2_DCU_INVALCOMPLETE_MASK 0x00000400UL
+#define EUR_CR_EVENT_STATUS2_DCU_INVALCOMPLETE_SHIFT 10
+#define EUR_CR_EVENT_STATUS2_GSG_FLUSHED_MASK 0x00000200UL
+#define EUR_CR_EVENT_STATUS2_GSG_FLUSHED_SHIFT 9
+#define EUR_CR_EVENT_STATUS2_GSG_LOADED_MASK 0x00000100UL
+#define EUR_CR_EVENT_STATUS2_GSG_LOADED_SHIFT 8
+#define EUR_CR_EVENT_STATUS2_TRIG_TA_MASK   0x00000080UL
+#define EUR_CR_EVENT_STATUS2_TRIG_TA_SHIFT  7
+#define EUR_CR_EVENT_STATUS2_TRIG_3D_MASK   0x00000040UL
+#define EUR_CR_EVENT_STATUS2_TRIG_3D_SHIFT  6
+#define EUR_CR_EVENT_STATUS2_TRIG_DL_MASK   0x00000020UL
+#define EUR_CR_EVENT_STATUS2_TRIG_DL_SHIFT  5
+#define EUR_CR_EVENT_STATUS2_DPM_DHOST_FREE_LOAD_MASK 0x00000008UL
+#define EUR_CR_EVENT_STATUS2_DPM_DHOST_FREE_LOAD_SHIFT 3
+#define EUR_CR_EVENT_STATUS2_DPM_HOST_FREE_LOAD_MASK 0x00000004UL
+#define EUR_CR_EVENT_STATUS2_DPM_HOST_FREE_LOAD_SHIFT 2
+#define EUR_CR_EVENT_STATUS2_DPM_3D_FREE_LOAD_MASK 0x00000002UL
+#define EUR_CR_EVENT_STATUS2_DPM_3D_FREE_LOAD_SHIFT 1
+#define EUR_CR_EVENT_STATUS2_DPM_TA_FREE_LOAD_MASK 0x00000001UL
+#define EUR_CR_EVENT_STATUS2_DPM_TA_FREE_LOAD_SHIFT 0
+#define EUR_CR_EVENT_STATUS                 0x012CUL
+#define EUR_CR_EVENT_STATUS_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_STATUS_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_STATUS_TIMER_MASK      0x20000000UL
+#define EUR_CR_EVENT_STATUS_TIMER_SHIFT     29
+#define EUR_CR_EVENT_STATUS_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_STATUS_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_STATUS_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_STATUS_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_STATUS_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_STATUS_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_STATUS_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_STATUS_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_STATUS_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_STATUS_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_STATUS_ISP2_ZLS_CSW_FINISHED_MASK 0x00200000UL
+#define EUR_CR_EVENT_STATUS_ISP2_ZLS_CSW_FINISHED_SHIFT 21
+#define EUR_CR_EVENT_STATUS_OTPM_INV_MASK   0x00100000UL
+#define EUR_CR_EVENT_STATUS_OTPM_INV_SHIFT  20
+#define EUR_CR_EVENT_STATUS_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_STATUS_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_STATUS_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_STATUS_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_STATUS_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_STATUS_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_STATUS_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_STATUS_SW_EVENT_MASK   0x00004000UL
+#define EUR_CR_EVENT_STATUS_SW_EVENT_SHIFT  14
+#define EUR_CR_EVENT_STATUS_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_STATUS_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_STATUS_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_STATUS_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_STATUS_TPC_CLEAR_MASK  0x00000800UL
+#define EUR_CR_EVENT_STATUS_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_STATUS_TPC_FLUSH_MASK  0x00000400UL
+#define EUR_CR_EVENT_STATUS_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_STATUS_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_STATUS_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_STATUS_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_STATUS_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_STATUS_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_STATUS_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_STATUS_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_STATUS_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_STATUS_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_EVENT_HOST_ENABLE            0x0130
+#define EUR_CR_EVENT_HOST_ENABLE_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_HOST_ENABLE_TIMER_MASK 0x20000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TIMER_SHIFT 29
+#define EUR_CR_EVENT_HOST_ENABLE_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_HOST_ENABLE_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_HOST_ENABLE_ISP2_ZLS_CSW_FINISHED_MASK 0x00200000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP2_ZLS_CSW_FINISHED_SHIFT 21
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_INV_MASK 0x00100000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_INV_SHIFT 20
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_HOST_ENABLE_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_HOST_ENABLE_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_HOST_ENABLE_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_HOST_ENABLE_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_HOST_ENABLE_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_HOST_ENABLE_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_HOST_ENABLE_SW_EVENT_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_ENABLE_SW_EVENT_SHIFT 14
+#define EUR_CR_EVENT_HOST_ENABLE_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_HOST_ENABLE_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_ENABLE_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_CLEAR_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_FLUSH_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_ENABLE_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_ENABLE_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_EVENT_HOST_CLEAR             0x0134
+#define EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_MASK 0x80000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_MASTER_INTERRUPT_SHIFT 31
+#define EUR_CR_EVENT_HOST_CLEAR_TIMER_MASK  0x20000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TIMER_SHIFT 29
+#define EUR_CR_EVENT_HOST_CLEAR_TA_DPM_FAULT_MASK 0x10000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_DPM_FAULT_SHIFT 28
+#define EUR_CR_EVENT_HOST_CLEAR_MADD_CACHE_INVALCOMPLETE_MASK 0x04000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_MADD_CACHE_INVALCOMPLETE_SHIFT 26
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_ZLS_MASK 0x02000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_ZLS_SHIFT 25
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_TA_MEM_FREE_MASK 0x01000000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_TA_MEM_FREE_SHIFT 24
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_END_TILE_MASK 0x00800000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_END_TILE_SHIFT 23
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_INITEND_MASK 0x00400000UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_INITEND_SHIFT 22
+#define EUR_CR_EVENT_HOST_CLEAR_ISP2_ZLS_CSW_FINISHED_MASK 0x00200000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP2_ZLS_CSW_FINISHED_SHIFT 21
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_INV_MASK 0x00100000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_INV_SHIFT 20
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_FLUSHED_MASK 0x00080000UL
+#define EUR_CR_EVENT_HOST_CLEAR_OTPM_FLUSHED_SHIFT 19
+#define EUR_CR_EVENT_HOST_CLEAR_PIXELBE_END_RENDER_MASK 0x00040000UL
+#define EUR_CR_EVENT_HOST_CLEAR_PIXELBE_END_RENDER_SHIFT 18
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_VISIBILITY_FAIL_MASK 0x00010000UL
+#define EUR_CR_EVENT_HOST_CLEAR_ISP_VISIBILITY_FAIL_SHIFT 16
+#define EUR_CR_EVENT_HOST_CLEAR_BREAKPOINT_MASK 0x00008000UL
+#define EUR_CR_EVENT_HOST_CLEAR_BREAKPOINT_SHIFT 15
+#define EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_MASK 0x00004000UL
+#define EUR_CR_EVENT_HOST_CLEAR_SW_EVENT_SHIFT 14
+#define EUR_CR_EVENT_HOST_CLEAR_TA_FINISHED_MASK 0x00002000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_FINISHED_SHIFT 13
+#define EUR_CR_EVENT_HOST_CLEAR_TA_TERMINATE_MASK 0x00001000UL
+#define EUR_CR_EVENT_HOST_CLEAR_TA_TERMINATE_SHIFT 12
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_CLEAR_MASK 0x00000800UL
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_CLEAR_SHIFT 11
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_FLUSH_MASK 0x00000400UL
+#define EUR_CR_EVENT_HOST_CLEAR_TPC_FLUSH_SHIFT 10
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_CLEAR_MASK 0x00000200UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_CLEAR_SHIFT 9
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_LOAD_MASK 0x00000100UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_LOAD_SHIFT 8
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_STORE_MASK 0x00000080UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_CONTROL_STORE_SHIFT 7
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_CLEAR_MASK 0x00000040UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_CLEAR_SHIFT 6
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_LOAD_MASK 0x00000020UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_LOAD_SHIFT 5
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_STORE_MASK 0x00000010UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_STATE_STORE_SHIFT 4
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_REACHED_MEM_THRESH_MASK 0x00000008UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_REACHED_MEM_THRESH_SHIFT 3
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_GBL_MASK 0x00000004UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_GBL_SHIFT 2
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_MT_MASK 0x00000002UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_OUT_OF_MEMORY_MT_SHIFT 1
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_3D_MEM_FREE_MASK 0x00000001UL
+#define EUR_CR_EVENT_HOST_CLEAR_DPM_3D_MEM_FREE_SHIFT 0
+#define EUR_CR_PDS_EXEC_BASE                0x0AB8
+#define EUR_CR_PDS_EXEC_BASE_ADDR_MASK      0xFFF00000UL
+#define EUR_CR_PDS_EXEC_BASE_ADDR_SHIFT     20
+#define EUR_CR_EVENT_KICKER                 0x0AC4
+#define EUR_CR_EVENT_KICKER_ADDRESS_MASK    0xFFFFFFF0UL
+#define EUR_CR_EVENT_KICKER_ADDRESS_SHIFT   4
+#define EUR_CR_EVENT_KICK                   0x0AC8
+#define EUR_CR_EVENT_KICK_NOW_MASK          0x00000001UL
+#define EUR_CR_EVENT_KICK_NOW_SHIFT         0
+#define EUR_CR_EVENT_TIMER                  0x0ACC
+#define EUR_CR_EVENT_TIMER_ENABLE_MASK      0x01000000UL
+#define EUR_CR_EVENT_TIMER_ENABLE_SHIFT     24
+#define EUR_CR_EVENT_TIMER_VALUE_MASK       0x00FFFFFFUL
+#define EUR_CR_EVENT_TIMER_VALUE_SHIFT      0
+#define EUR_CR_PDS_INV0                     0x0AD0
+#define EUR_CR_PDS_INV0_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV0_DSC_SHIFT           0
+#define EUR_CR_PDS_INV1                     0x0AD4
+#define EUR_CR_PDS_INV1_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV1_DSC_SHIFT           0
+#define EUR_CR_PDS_INV3                     0x0AD8
+#define EUR_CR_PDS_INV3_DSC_MASK            0x00000001UL
+#define EUR_CR_PDS_INV3_DSC_SHIFT           0
+#define EUR_CR_PDS_INV_CSC                  0x0AE0
+#define EUR_CR_PDS_INV_CSC_KICK_MASK        0x00000001UL
+#define EUR_CR_PDS_INV_CSC_KICK_SHIFT       0
+#define EUR_CR_EVENT_KICK1                  0x0AE4
+#define EUR_CR_EVENT_KICK1_NOW_MASK         0x000000FFUL
+#define EUR_CR_EVENT_KICK1_NOW_SHIFT        0
+#define EUR_CR_EVENT_KICK2                  0x0AE8
+#define EUR_CR_EVENT_KICK2_NOW_MASK         0x00000001UL
+#define EUR_CR_EVENT_KICK2_NOW_SHIFT        0
+#define EUR_CR_EVENT_KICK3                  0x0AEC
+#define EUR_CR_EVENT_KICK3_NOW_MASK         0x00000001UL
+#define EUR_CR_EVENT_KICK3_NOW_SHIFT        0
+#define EUR_CR_BIF_CTRL                     0x0C00
+#define EUR_CR_BIF_CTRL_NOREORDER_MASK      0x00000001UL
+#define EUR_CR_BIF_CTRL_NOREORDER_SHIFT     0
+#define EUR_CR_BIF_CTRL_PAUSE_MASK          0x00000002UL
+#define EUR_CR_BIF_CTRL_PAUSE_SHIFT         1
+#define EUR_CR_BIF_CTRL_FLUSH_MASK          0x00000004UL
+#define EUR_CR_BIF_CTRL_FLUSH_SHIFT         2
+#define EUR_CR_BIF_CTRL_INVALDC_MASK        0x00000008UL
+#define EUR_CR_BIF_CTRL_INVALDC_SHIFT       3
+#define EUR_CR_BIF_CTRL_CLEAR_FAULT_MASK    0x00000010UL
+#define EUR_CR_BIF_CTRL_CLEAR_FAULT_SHIFT   4
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_CACHE_MASK 0x00000100UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_CACHE_SHIFT 8
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_VDM_MASK 0x00000200UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_VDM_SHIFT 9
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TE_MASK  0x00000400UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TE_SHIFT 10
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_PBE_MASK 0x00000800UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_PBE_SHIFT 11
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TSPP_MASK 0x00001000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_TSPP_SHIFT 12
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_ISP_MASK 0x00002000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_ISP_SHIFT 13
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_USE_MASK 0x00004000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_USE_SHIFT 14
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_MASK 0x00008000UL
+#define EUR_CR_BIF_CTRL_MMU_BYPASS_HOST_SHIFT 15
+#define EUR_CR_BIF_INT_STAT                 0x0C04
+#define EUR_CR_BIF_INT_STAT_FAULT_REQ_MASK  0x0000FFFFUL
+#define EUR_CR_BIF_INT_STAT_FAULT_REQ_SHIFT 0
+#define EUR_CR_BIF_INT_STAT_FAULT_TYPE_MASK 0x00070000UL
+#define EUR_CR_BIF_INT_STAT_FAULT_TYPE_SHIFT 16
+#define EUR_CR_BIF_INT_STAT_FLUSH_COMPLETE_MASK 0x00080000UL
+#define EUR_CR_BIF_INT_STAT_FLUSH_COMPLETE_SHIFT 19
+#define EUR_CR_BIF_FAULT                    0x0C08
+#define EUR_CR_BIF_FAULT_CID_MASK           0x0000000FUL
+#define EUR_CR_BIF_FAULT_CID_SHIFT          0
+#define EUR_CR_BIF_FAULT_SB_MASK            0x000001F0UL
+#define EUR_CR_BIF_FAULT_SB_SHIFT           4
+#define EUR_CR_BIF_FAULT_ADDR_MASK          0xFFFFF000UL
+#define EUR_CR_BIF_FAULT_ADDR_SHIFT         12
+
+#if defined(SGX_FEATURE_BIF_WIDE_TILING_AND_4K_ADDRESS)
+#define EUR_CR_BIF_TILE0                    0x0C0C
+#define EUR_CR_BIF_TILE0_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE0_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE0_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE0_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE1                    0x0C10
+#define EUR_CR_BIF_TILE1_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE1_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE1_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE1_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE2                    0x0C14
+#define EUR_CR_BIF_TILE2_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE2_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE2_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE2_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE3                    0x0C18
+#define EUR_CR_BIF_TILE3_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE3_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE3_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE3_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE4                    0x0C1C
+#define EUR_CR_BIF_TILE4_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE4_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE4_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE4_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE5                    0x0C20
+#define EUR_CR_BIF_TILE5_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE5_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE5_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE5_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE6                    0x0C24
+#define EUR_CR_BIF_TILE6_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE6_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE6_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE6_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE7                    0x0C28
+#define EUR_CR_BIF_TILE7_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE7_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE7_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE7_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE8                    0x0C2C
+#define EUR_CR_BIF_TILE8_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE8_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE8_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE8_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE9                    0x0C30
+#define EUR_CR_BIF_TILE9_MAX_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE9_MAX_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE9_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE9_CFG_SHIFT          24
+
+#else
+
+#define EUR_CR_BIF_TILE0                    0x0C0C
+#define EUR_CR_BIF_TILE0_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE0_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE0_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE0_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE0_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE0_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE1                    0x0C10
+#define EUR_CR_BIF_TILE1_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE1_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE1_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE1_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE1_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE1_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE2                    0x0C14
+#define EUR_CR_BIF_TILE2_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE2_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE2_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE2_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE2_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE2_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE3                    0x0C18
+#define EUR_CR_BIF_TILE3_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE3_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE3_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE3_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE3_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE3_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE4                    0x0C1C
+#define EUR_CR_BIF_TILE4_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE4_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE4_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE4_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE4_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE4_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE5                    0x0C20
+#define EUR_CR_BIF_TILE5_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE5_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE5_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE5_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE5_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE5_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE6                    0x0C24
+#define EUR_CR_BIF_TILE6_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE6_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE6_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE6_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE6_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE6_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE7                    0x0C28
+#define EUR_CR_BIF_TILE7_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE7_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE7_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE7_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE7_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE7_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE8                    0x0C2C
+#define EUR_CR_BIF_TILE8_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE8_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE8_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE8_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE8_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE8_CFG_SHIFT          24
+#define EUR_CR_BIF_TILE9                    0x0C30
+#define EUR_CR_BIF_TILE9_MIN_ADDRESS_MASK   0x00000FFFUL
+#define EUR_CR_BIF_TILE9_MIN_ADDRESS_SHIFT  0
+#define EUR_CR_BIF_TILE9_MAX_ADDRESS_MASK   0x00FFF000UL
+#define EUR_CR_BIF_TILE9_MAX_ADDRESS_SHIFT  12
+#define EUR_CR_BIF_TILE9_CFG_MASK           0x0F000000UL
+#define EUR_CR_BIF_TILE9_CFG_SHIFT          24
+#endif
+
+#define EUR_CR_BIF_DIR_LIST_BASE1           0x0C38
+#define EUR_CR_BIF_DIR_LIST_BASE1_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE1_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE2           0x0C3C
+#define EUR_CR_BIF_DIR_LIST_BASE2_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE2_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE3           0x0C40
+#define EUR_CR_BIF_DIR_LIST_BASE3_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE3_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE4           0x0C44
+#define EUR_CR_BIF_DIR_LIST_BASE4_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE4_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE5           0x0C48
+#define EUR_CR_BIF_DIR_LIST_BASE5_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE5_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE6           0x0C4C
+#define EUR_CR_BIF_DIR_LIST_BASE6_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE6_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE7           0x0C50
+#define EUR_CR_BIF_DIR_LIST_BASE7_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE7_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE8           0x0C54
+#define EUR_CR_BIF_DIR_LIST_BASE8_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE8_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE9           0x0C58
+#define EUR_CR_BIF_DIR_LIST_BASE9_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE9_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE10          0x0C5C
+#define EUR_CR_BIF_DIR_LIST_BASE10_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE10_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE11          0x0C60
+#define EUR_CR_BIF_DIR_LIST_BASE11_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE11_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE12          0x0C64
+#define EUR_CR_BIF_DIR_LIST_BASE12_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE12_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE13          0x0C68
+#define EUR_CR_BIF_DIR_LIST_BASE13_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE13_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE14          0x0C6C
+#define EUR_CR_BIF_DIR_LIST_BASE14_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE14_ADDR_SHIFT 12
+#define EUR_CR_BIF_DIR_LIST_BASE15          0x0C70
+#define EUR_CR_BIF_DIR_LIST_BASE15_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE15_ADDR_SHIFT 12
+#define EUR_CR_BIF_BANK_SET                 0x0C74
+#define EUR_CR_BIF_BANK_SET_SELECT_2D_MASK  0x00000001UL
+#define EUR_CR_BIF_BANK_SET_SELECT_2D_SHIFT 0
+#define EUR_CR_BIF_BANK_SET_SELECT_3D_MASK  0x0000000CUL
+#define EUR_CR_BIF_BANK_SET_SELECT_3D_SHIFT 2
+#define EUR_CR_BIF_BANK_SET_SELECT_HOST_MASK 0x00000010UL
+#define EUR_CR_BIF_BANK_SET_SELECT_HOST_SHIFT 4
+#define EUR_CR_BIF_BANK_SET_SELECT_TA_MASK  0x000000C0UL
+#define EUR_CR_BIF_BANK_SET_SELECT_TA_SHIFT 6
+#define EUR_CR_BIF_BANK_SET_SELECT_EDM_MASK 0x00000100UL
+#define EUR_CR_BIF_BANK_SET_SELECT_EDM_SHIFT 8
+#define EUR_CR_BIF_BANK_SET_SELECT_DPM_LSS_MASK 0x00000200UL
+#define EUR_CR_BIF_BANK_SET_SELECT_DPM_LSS_SHIFT 9
+#define EUR_CR_BIF_BANK0                    0x0C78
+#define EUR_CR_BIF_BANK0_INDEX_EDM_MASK     0x0000000FUL
+#define EUR_CR_BIF_BANK0_INDEX_EDM_SHIFT    0
+#define EUR_CR_BIF_BANK0_INDEX_TA_MASK      0x000000F0UL
+#define EUR_CR_BIF_BANK0_INDEX_TA_SHIFT     4
+#define EUR_CR_BIF_BANK0_INDEX_HOST_MASK    0x00000F00UL
+#define EUR_CR_BIF_BANK0_INDEX_HOST_SHIFT   8
+#define EUR_CR_BIF_BANK0_INDEX_3D_MASK      0x0000F000UL
+#define EUR_CR_BIF_BANK0_INDEX_3D_SHIFT     12
+#define EUR_CR_BIF_BANK0_INDEX_2D_MASK      0x000F0000UL
+#define EUR_CR_BIF_BANK0_INDEX_2D_SHIFT     16
+#define EUR_CR_BIF_BANK1                    0x0C7C
+#define EUR_CR_BIF_BANK1_INDEX_EDM_MASK     0x0000000FUL
+#define EUR_CR_BIF_BANK1_INDEX_EDM_SHIFT    0
+#define EUR_CR_BIF_BANK1_INDEX_TA_MASK      0x000000F0UL
+#define EUR_CR_BIF_BANK1_INDEX_TA_SHIFT     4
+#define EUR_CR_BIF_BANK1_INDEX_HOST_MASK    0x00000F00UL
+#define EUR_CR_BIF_BANK1_INDEX_HOST_SHIFT   8
+#define EUR_CR_BIF_BANK1_INDEX_3D_MASK      0x0000F000UL
+#define EUR_CR_BIF_BANK1_INDEX_3D_SHIFT     12
+#define EUR_CR_BIF_BANK1_INDEX_2D_MASK      0x000F0000UL
+#define EUR_CR_BIF_BANK1_INDEX_2D_SHIFT     16
+#define EUR_CR_BIF_DIR_LIST_BASE0           0x0C84
+#define EUR_CR_BIF_DIR_LIST_BASE0_ADDR_MASK 0xFFFFF000UL
+#define EUR_CR_BIF_DIR_LIST_BASE0_ADDR_SHIFT 12
+#define EUR_CR_BIF_TA_REQ_BASE              0x0C90
+#define EUR_CR_BIF_TA_REQ_BASE_ADDR_MASK    0xFFF00000UL
+#define EUR_CR_BIF_TA_REQ_BASE_ADDR_SHIFT   20
+#define EUR_CR_BIF_MEM_REQ_STAT             0x0CA8
+#define EUR_CR_BIF_MEM_REQ_STAT_READS_MASK  0x000000FFUL
+#define EUR_CR_BIF_MEM_REQ_STAT_READS_SHIFT 0
+#define EUR_CR_BIF_3D_REQ_BASE              0x0CAC
+#define EUR_CR_BIF_3D_REQ_BASE_ADDR_MASK    0xFFF00000UL
+#define EUR_CR_BIF_3D_REQ_BASE_ADDR_SHIFT   20
+#define EUR_CR_BIF_ZLS_REQ_BASE             0x0CB0
+#define EUR_CR_BIF_ZLS_REQ_BASE_ADDR_MASK   0xFFF00000UL
+#define EUR_CR_BIF_ZLS_REQ_BASE_ADDR_SHIFT  20
+#define EUR_CR_BIF_BANK_STATUS              0x0CB4
+#define EUR_CR_BIF_BANK_STATUS_3D_CURRENT_BANK_MASK 0x00000001UL
+#define EUR_CR_BIF_BANK_STATUS_3D_CURRENT_BANK_SHIFT 0
+#define EUR_CR_BIF_BANK_STATUS_TA_CURRENT_BANK_MASK 0x00000002UL
+#define EUR_CR_BIF_BANK_STATUS_TA_CURRENT_BANK_SHIFT 1
+
+#define EUR_CR_BIF_36BIT_ADDRESSING         0x0CCC
+#define EUR_CR_BIF_36BIT_ADDRESSING_ENABLE_MASK 0x00000001UL
+#define EUR_CR_BIF_36BIT_ADDRESSING_ENABLE_SHIFT 0
+
+#if defined(SGX_FEATURE_BIF_WIDE_TILING_AND_4K_ADDRESS)
+#define EUR_CR_BIF_TILE0_MIN                0x0CD0
+#define EUR_CR_BIF_TILE0_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE0_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE1_MIN                0x0CD4
+#define EUR_CR_BIF_TILE1_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE1_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE2_MIN                0x0CD8
+#define EUR_CR_BIF_TILE2_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE2_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE3_MIN                0x0CDC
+#define EUR_CR_BIF_TILE3_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE3_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE4_MIN                0x0CE0
+#define EUR_CR_BIF_TILE4_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE4_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE5_MIN                0x0CE4
+#define EUR_CR_BIF_TILE5_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE5_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE6_MIN                0x0CE8
+#define EUR_CR_BIF_TILE6_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE6_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE7_MIN                0x0CEC
+#define EUR_CR_BIF_TILE7_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE7_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE8_MIN                0x0CF0
+#define EUR_CR_BIF_TILE8_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE8_MIN_ADDRESS_SHIFT  0
+
+#define EUR_CR_BIF_TILE9_MIN                0x0CF4
+#define EUR_CR_BIF_TILE9_MIN_ADDRESS_MASK   0x000FFFFFUL
+#define EUR_CR_BIF_TILE9_MIN_ADDRESS_SHIFT  0
+#endif
+
+#define EUR_CR_USE_CODE_BASE(X)     (0x0A0C + (4 * (X)))
+#define EUR_CR_USE_CODE_BASE_ADDR_MASK      0x01FFFFFFUL
+#define EUR_CR_USE_CODE_BASE_ADDR_SHIFT     0
+#define EUR_CR_USE_CODE_BASE_DM_MASK        0x06000000UL
+#define EUR_CR_USE_CODE_BASE_DM_SHIFT       25
+#define EUR_CR_USE_CODE_BASE_SIZE_UINT32 16
+#define EUR_CR_USE_CODE_BASE_NUM_ENTRIES 16
+#define EUR_CR_MNE_CR_CTRL						0x0D00
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_N_MASK		0x00010000UL
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_N_SHIFT		16
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_MASK			0x00008000UL
+#define EUR_CR_MNE_CR_CTRL_BYP_CC_SHIFT			15
+#define EUR_CR_MNE_CR_CTRL_USE_INVAL_ADDR_MASK	0x00007800UL
+#define EUR_CR_MNE_CR_CTRL_USE_INVAL_ADDR_SHIFT	11
+#define EUR_CR_MNE_CR_CTRL_BYPASS_ALL_MASK		0x00000400UL
+#define EUR_CR_MNE_CR_CTRL_BYPASS_ALL_SHIFT		10
+#define EUR_CR_MNE_CR_CTRL_BYPASS_MASK			0x000003E0UL
+#define EUR_CR_MNE_CR_CTRL_BYPASS_SHIFT			5
+#define EUR_CR_MNE_CR_CTRL_PAUSE_MASK			0x00000010UL
+#define EUR_CR_MNE_CR_CTRL_PAUSE_SHIFT			4
+
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_MASK		0x0000000EUL
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT		1
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_PDS_MASK 	(1UL<<EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT+2)
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_USEC_MASK (1UL<<EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT+1)
+#define EUR_CR_MNE_CR_CTRL_INVAL_PREQ_CACHE_MASK (1UL<<EUR_CR_MNE_CR_CTRL_INVAL_PREQ_SHIFT)
+#define EUR_CR_MNE_CR_CTRL_INVAL_ALL_MASK		0x00000001UL
+#define EUR_CR_MNE_CR_CTRL_INVAL_ALL_SHIFT		0
+#define EUR_CR_MNE_CR_USE_INVAL					0x0D04
+#define EUR_CR_MNE_CR_USE_INVAL_ADDR_MASK		0xFFFFFFFFUL
+#define EUR_CR_MNE_CR_USE_INVAL_ADDR_SHIFT		0
+#define EUR_CR_MNE_CR_STAT					0x0D08
+#define EUR_CR_MNE_CR_STAT_PAUSED_MASK		0x00000400UL
+#define EUR_CR_MNE_CR_STAT_PAUSED_SHIFT		10
+#define EUR_CR_MNE_CR_STAT_READS_MASK		0x000003FFUL
+#define EUR_CR_MNE_CR_STAT_READS_SHIFT		0
+#define EUR_CR_MNE_CR_STAT_STATS			0x0D0C
+#define EUR_CR_MNE_CR_STAT_STATS_RST_MASK	0x000FFFF0UL
+#define EUR_CR_MNE_CR_STAT_STATS_RST_SHIFT	4
+#define EUR_CR_MNE_CR_STAT_STATS_SEL_MASK	0x0000000FUL
+#define EUR_CR_MNE_CR_STAT_STATS_SEL_SHIFT	0
+#define EUR_CR_MNE_CR_STAT_STATS_OUT				0x0D10
+#define EUR_CR_MNE_CR_STAT_STATS_OUT_VALUE_MASK		0xFFFFFFFFUL
+#define EUR_CR_MNE_CR_STAT_STATS_OUT_VALUE_SHIFT	0
+#define EUR_CR_MNE_CR_EVENT_STATUS				0x0D14
+#define EUR_CR_MNE_CR_EVENT_STATUS_INVAL_MASK	0x00000001UL
+#define EUR_CR_MNE_CR_EVENT_STATUS_INVAL_SHIFT	0
+#define EUR_CR_MNE_CR_EVENT_CLEAR				0x0D18
+#define EUR_CR_MNE_CR_EVENT_CLEAR_INVAL_MASK	0x00000001UL
+#define EUR_CR_MNE_CR_EVENT_CLEAR_INVAL_SHIFT	0
+#define EUR_CR_MNE_CR_CTRL_INVAL				0x0D20
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxdefs.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxdefs.h
new file mode 100644
index 0000000..c496765
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxdefs.h
@@ -0,0 +1,78 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SGXDEFS_H_
+#define	_SGXDEFS_H_
+
+#include "sgxerrata.h"
+#include "sgxfeaturedefs.h"
+
+#if defined(SGX520)
+#include "sgx520defs.h"
+#else
+#if defined(SGX530)
+#include "sgx530defs.h"
+#else
+#if defined(SGX535)
+#include "sgx535defs.h"
+#else
+#if defined(SGX535_V1_1)
+#include "sgx535defs.h"
+#else
+#if defined(SGX540)
+#include "sgx540defs.h"
+#else
+#if defined(SGX541)
+#include "sgx541defs.h"
+#else
+#if defined(SGX543)
+#include "sgx543defs.h"
+#else
+#if defined(SGX545)
+#include "sgx545defs.h"
+#else
+#if defined(SGX531)
+#include "sgx531defs.h"
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+
+#if defined(SGX_FEATURE_MP)
+#if defined(SGX541)
+#if SGX_CORE_REV == 100
+#include "sgx541_100mpdefs.h"
+#else
+#include "sgx541mpdefs.h"
+#endif
+#else
+#include "sgxmpdefs.h"
+#endif
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxerrata.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxerrata.h
new file mode 100644
index 0000000..dee5b43
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxerrata.h
@@ -0,0 +1,310 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SGXERRATA_KM_H_
+#define _SGXERRATA_KM_H_
+
+#if defined(SGX520) && !defined(SGX_CORE_DEFINED)
+
+	#define SGX_CORE_REV_HEAD	0
+	#if defined(USE_SGX_CORE_REV_HEAD)
+
+		#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+	#endif
+
+	#if SGX_CORE_REV == 100
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+	#else
+		#error "sgxerrata.h: SGX520 Core Revision unspecified"
+	#endif
+	#endif
+
+	#define SGX_CORE_DEFINED
+#endif
+
+#if defined(SGX530) && !defined(SGX_CORE_DEFINED)
+
+	#define SGX_CORE_REV_HEAD	0
+	#if defined(USE_SGX_CORE_REV_HEAD)
+
+		#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+	#endif
+
+	#if SGX_CORE_REV == 103
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == 110
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == 111
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == 120
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == 121
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == 125
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_28889
+	#else
+	#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+	#else
+		#error "sgxerrata.h: SGX530 Core Revision unspecified"
+	#endif
+	#endif
+	#endif
+	#endif
+	#endif
+#endif
+        #endif
+
+	#define SGX_CORE_DEFINED
+#endif
+
+#if defined(SGX531) && !defined(SGX_CORE_DEFINED)
+
+	#define SGX_CORE_REV_HEAD	0
+	#if defined(USE_SGX_CORE_REV_HEAD)
+
+		#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+	#endif
+
+	#if SGX_CORE_REV == 101
+		#define FIX_HW_BRN_26620
+		#define FIX_HW_BRN_28011
+	#else
+	#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+	#else
+		#error "sgxerrata.h: SGX531 Core Revision unspecified"
+	#endif
+	#endif
+
+	#define SGX_CORE_DEFINED
+#endif
+
+#if (defined(SGX535) || defined(SGX535_V1_1)) && !defined(SGX_CORE_DEFINED)
+
+	#define SGX_CORE_REV_HEAD	0
+	#if defined(USE_SGX_CORE_REV_HEAD)
+
+		#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+	#endif
+
+	#if SGX_CORE_REV == 111
+		#define FIX_HW_BRN_23281
+		#define FIX_HW_BRN_23410
+		#define FIX_HW_BRN_22693
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_22997
+		#define FIX_HW_BRN_23030
+	#else
+	#if SGX_CORE_REV == 1111
+		#define FIX_HW_BRN_23281
+		#define FIX_HW_BRN_23410
+		#define FIX_HW_BRN_22693
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_22997
+		#define FIX_HW_BRN_23030
+	#else
+	#if SGX_CORE_REV == 112
+		#define FIX_HW_BRN_23281
+		#define FIX_HW_BRN_23410
+		#define FIX_HW_BRN_22693
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_22997
+		#define FIX_HW_BRN_23030
+	#else
+	#if SGX_CORE_REV == 113
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_23281
+		#define FIX_HW_BRN_23944
+		#define FIX_HW_BRN_23410
+	#else
+	#if SGX_CORE_REV == 121
+		#define FIX_HW_BRN_22934
+		#define FIX_HW_BRN_23944
+		#define FIX_HW_BRN_23410
+	#else
+	#if SGX_CORE_REV == 126
+		#define FIX_HW_BRN_22934
+	#else
+	#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+	#else
+		#error "sgxerrata.h: SGX535 Core Revision unspecified"
+
+	#endif
+	#endif
+	#endif
+	#endif
+	#endif
+	#endif
+	#endif
+
+	#define SGX_CORE_DEFINED
+#endif
+
+#if defined(SGX540) && !defined(SGX_CORE_DEFINED)
+
+	#define SGX_CORE_REV_HEAD	0
+	#if defined(USE_SGX_CORE_REV_HEAD)
+
+		#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+	#endif
+
+	#if SGX_CORE_REV == 101
+		#define FIX_HW_BRN_25499
+		#define FIX_HW_BRN_25503
+		#define FIX_HW_BRN_26620
+		#define FIX_HW_BRN_28011
+	#else
+	#if SGX_CORE_REV == 110
+		#define FIX_HW_BRN_25503
+		#define FIX_HW_BRN_26620
+		#define FIX_HW_BRN_28011
+	#else
+	#if SGX_CORE_REV == 120
+		#define FIX_HW_BRN_26620
+		#define FIX_HW_BRN_28011
+	#else
+	#if SGX_CORE_REV == 121
+		#define FIX_HW_BRN_28011
+	#else
+	#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+	#else
+		#error "sgxerrata.h: SGX540 Core Revision unspecified"
+	#endif
+	#endif
+	#endif
+	#endif
+	#endif
+
+	#define SGX_CORE_DEFINED
+#endif
+
+#if defined(SGX541) && !defined(SGX_CORE_DEFINED)
+	#if defined(SGX_FEATURE_MP)
+
+		#define SGX_CORE_REV_HEAD	0
+		#if defined(USE_SGX_CORE_REV_HEAD)
+
+			#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+		#endif
+
+		#if SGX_CORE_REV == 100
+			#define FIX_HW_BRN_27270
+			#define FIX_HW_BRN_28011
+			#define FIX_HW_BRN_27510
+
+		#else
+		#if SGX_CORE_REV == 101
+
+		#else
+		#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+		#else
+			#error "sgxerrata.h: SGX541 Core Revision unspecified"
+		#endif
+		#endif
+		#endif
+
+		#define SGX_CORE_DEFINED
+	#else
+		#error "sgxerrata.h: SGX541 only supports MP configs (SGX_FEATURE_MP)"
+	#endif
+#endif
+
+#if defined(SGX543) && !defined(SGX_CORE_DEFINED)
+	#if defined(SGX_FEATURE_MP)
+
+		#define SGX_CORE_REV_HEAD	0
+		#if defined(USE_SGX_CORE_REV_HEAD)
+
+			#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+		#endif
+
+		#if SGX_CORE_REV == 100
+
+		#else
+		#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+		#else
+			#error "sgxerrata.h: SGX543 Core Revision unspecified"
+		#endif
+		#endif
+
+		#define SGX_CORE_DEFINED
+	#else
+		#error "sgxerrata.h: SGX543 only supports MP configs (SGX_FEATURE_MP)"
+	#endif
+#endif
+
+#if defined(SGX545) && !defined(SGX_CORE_DEFINED)
+
+	#define SGX_CORE_REV_HEAD	0
+	#if defined(USE_SGX_CORE_REV_HEAD)
+
+		#define SGX_CORE_REV	SGX_CORE_REV_HEAD
+	#endif
+
+	#if SGX_CORE_REV == 100
+		#define FIX_HW_BRN_26620
+		#define FIX_HW_BRN_27266
+		#define FIX_HW_BRN_27456
+	#else
+	#if SGX_CORE_REV == 109
+
+	#else
+	#if SGX_CORE_REV == SGX_CORE_REV_HEAD
+
+	#else
+		#error "sgxerrata.h: SGX545 Core Revision unspecified"
+	#endif
+	#endif
+	#endif
+
+	#define SGX_CORE_DEFINED
+#endif
+
+#if !defined(SGX_CORE_DEFINED)
+#if defined (__GNUC__)
+	#warning "sgxerrata.h: SGX Core Version unspecified"
+#else
+	#pragma message("sgxerrata.h: SGX Core Version unspecified")
+#endif
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxfeaturedefs.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxfeaturedefs.h
new file mode 100644
index 0000000..0672ad6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxfeaturedefs.h
@@ -0,0 +1,159 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if defined(SGX520)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX520"
+	#define SGX_CORE_ID										SGX_CORE_ID_520
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(28)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+#else
+#if defined(SGX530)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX530"
+	#define SGX_CORE_ID										SGX_CORE_ID_530
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(28)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+#else
+#if defined(SGX535)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX535"
+	#define SGX_CORE_ID										SGX_CORE_ID_535
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(32)
+	#define SGX_FEATURE_MULTIPLE_MEM_CONTEXTS
+	#define SGX_FEATURE_BIF_NUM_DIRLISTS					(16)
+	#define SGX_FEATURE_2D_HARDWARE
+	#define SGX_FEATURE_AUTOCLOCKGATING
+	#define SUPPORT_SGX_GENERAL_MAPPING_HEAP
+	#define SUPPORT_SGX_VIDEO_HEAP
+#else
+#if defined(SGX540)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX540"
+	#define SGX_CORE_ID										SGX_CORE_ID_540
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(28)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+	#define SGX_FEATURE_MULTI_EVENT_KICK
+#else
+#if defined(SGX541)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX541"
+	#define SGX_CORE_ID										SGX_CORE_ID_541
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(32)
+	#define SGX_FEATURE_MULTIPLE_MEM_CONTEXTS
+	#define SGX_FEATURE_BIF_NUM_DIRLISTS					(8)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+    #define SGX_FEATURE_SPM_MODE_0
+	#define SGX_FEATURE_MULTI_EVENT_KICK
+#else
+#if defined(SGX543)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX543"
+	#define SGX_CORE_ID										SGX_CORE_ID_543
+	#define SGX_FEATURE_USE_NO_INSTRUCTION_PAIRING
+	#define SGX_FEATURE_USE_UNLIMITED_PHASES
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(32)
+	#define SGX_FEATURE_MULTIPLE_MEM_CONTEXTS
+	#define SGX_FEATURE_BIF_NUM_DIRLISTS					(8)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+	#define SGX_FEATURE_MONOLITHIC_UKERNEL
+	#define SGX_FEATURE_MULTI_EVENT_KICK
+	#define SGX_FEATURE_DATA_BREAKPOINTS
+#else
+#if defined(SGX531)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX531"
+	#define SGX_CORE_ID										SGX_CORE_ID_531
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(28)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+	#define SGX_FEATURE_MULTI_EVENT_KICK
+#else
+#if defined(SGX545)
+	#define SGX_CORE_FRIENDLY_NAME							"SGX545"
+	#define SGX_CORE_ID										SGX_CORE_ID_545
+	#define SGX_FEATURE_ADDRESS_SPACE_SIZE					(32)
+	#define SGX_FEATURE_AUTOCLOCKGATING
+	#define SGX_FEATURE_USE_NO_INSTRUCTION_PAIRING
+	#define SGX_FEATURE_USE_UNLIMITED_PHASES
+	#define SGX_FEATURE_VOLUME_TEXTURES
+	#define SGX_FEATURE_HOST_ALLOC_FROM_DPM
+	#define SGX_FEATURE_MULTIPLE_MEM_CONTEXTS
+	#define SGX_FEATURE_BIF_NUM_DIRLISTS				(16)
+	#define SGX_FEATURE_NUM_USE_PIPES					(4)
+	#define	SGX_FEATURE_TEXTURESTRIDE_EXTENSION
+	#define SGX_FEATURE_PDS_DATA_INTERLEAVE_2DWORDS
+	#define SGX_FEATURE_MONOLITHIC_UKERNEL
+	#define SGX_FEATURE_ZLS_EXTERNALZ
+	#define SGX_FEATURE_VDM_CONTEXT_SWITCH_REV_2
+	#define SGX_FEATURE_ISP_CONTEXT_SWITCH_REV_2
+	#define SGX_FEATURE_NUM_PDS_PIPES					(2)
+	#define SGX_FEATURE_NATIVE_BACKWARD_BLIT
+	#define SGX_FEATURE_MAX_TA_RENDER_TARGETS				(512)
+	#define SGX_FEATURE_SPM_MODE_0
+	#define SGX_FEATURE_SECONDARY_REQUIRES_USE_KICK
+	#define SGX_FEATURE_DCU
+
+
+	#define SGX_FEATURE_BIF_WIDE_TILING_AND_4K_ADDRESS
+	#define SGX_FEATURE_MULTI_EVENT_KICK
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+
+#if defined(FIX_HW_BRN_22693)
+#undef SGX_FEATURE_AUTOCLOCKGATING
+#endif
+
+#if defined(FIX_HW_BRN_27266)
+#undef SGX_FEATURE_36BIT_MMU
+#endif
+
+#if defined(FIX_HW_BRN_27456)
+#undef SGX_FEATURE_BIF_WIDE_TILING_AND_4K_ADDRESS
+#endif
+
+#if defined(FIX_HW_BRN_22934)	\
+	|| defined(FIX_HW_BRN_25499)
+#undef SGX_FEATURE_MULTI_EVENT_KICK
+#endif
+
+#if defined(SGX_FEATURE_SYSTEM_CACHE)
+	#if defined(SGX_FEATURE_36BIT_MMU)
+		#error SGX_FEATURE_SYSTEM_CACHE is incompatible with SGX_FEATURE_36BIT_MMU
+	#endif
+	#if defined(FIX_HW_BRN_26620) && !defined(SGX_FEATURE_MULTI_EVENT_KICK)
+		#define SGX_BYPASS_SYSTEM_CACHE
+	#endif
+#endif
+
+#if defined(SGX_FEATURE_MP)
+#if !defined(SGX_FEATURE_MP_CORE_COUNT)
+#error SGX_FEATURE_MP_CORE_COUNT must be defined when SGX_FEATURE_MP is defined
+#endif
+#else
+#define SGX_FEATURE_MP_CORE_COUNT	(1)
+#endif
+
+#if defined(SUPPORT_SGX_LOW_LATENCY_SCHEDULING) && !defined(SUPPORT_SGX_PRIORITY_SCHEDULING)
+#define SUPPORT_SGX_PRIORITY_SCHEDULING
+#endif
+
+#include "img_types.h"
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxmmu.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxmmu.h
new file mode 100644
index 0000000..c6b4c0d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/hwdefs/sgxmmu.h
@@ -0,0 +1,75 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__SGXMMU_KM_H__)
+#define __SGXMMU_KM_H__
+
+#define SGX_MMU_PAGE_SHIFT				(12)
+#define SGX_MMU_PAGE_SIZE				(1UL<<SGX_MMU_PAGE_SHIFT)
+#define SGX_MMU_PAGE_MASK				(SGX_MMU_PAGE_SIZE - 1UL)
+
+#define SGX_MMU_PD_SHIFT				(10)
+#define SGX_MMU_PD_SIZE					(1UL<<SGX_MMU_PD_SHIFT)
+#define SGX_MMU_PD_MASK					(0xFFC00000UL)
+
+#if defined(SGX_FEATURE_36BIT_MMU)
+	#define SGX_MMU_PDE_ADDR_MASK			(0xFFFFFF00UL)
+	#define SGX_MMU_PDE_ADDR_ALIGNSHIFT		(4)
+#else
+	#define SGX_MMU_PDE_ADDR_MASK			(0xFFFFF000UL)
+	#define SGX_MMU_PDE_ADDR_ALIGNSHIFT		(0)
+#endif
+#define SGX_MMU_PDE_VALID				(0x00000001UL)
+#define SGX_MMU_PDE_PAGE_SIZE_4K		(0x00000000UL)
+#if defined(SGX_FEATURE_VARIABLE_MMU_PAGE_SIZE)
+	#define SGX_MMU_PDE_PAGE_SIZE_16K		(0x00000002UL)
+	#define SGX_MMU_PDE_PAGE_SIZE_64K		(0x00000004UL)
+	#define SGX_MMU_PDE_PAGE_SIZE_256K		(0x00000006UL)
+	#define SGX_MMU_PDE_PAGE_SIZE_1M		(0x00000008UL)
+	#define SGX_MMU_PDE_PAGE_SIZE_4M		(0x0000000AUL)
+	#define SGX_MMU_PDE_PAGE_SIZE_MASK		(0x0000000EUL)
+#else
+	#define SGX_MMU_PDE_WRITEONLY			(0x00000002UL)
+	#define SGX_MMU_PDE_READONLY			(0x00000004UL)
+	#define SGX_MMU_PDE_CACHECONSISTENT		(0x00000008UL)
+	#define SGX_MMU_PDE_EDMPROTECT			(0x00000010UL)
+#endif
+
+#define SGX_MMU_PT_SHIFT				(10)
+#define SGX_MMU_PT_SIZE					(1UL<<SGX_MMU_PT_SHIFT)
+#define SGX_MMU_PT_MASK					(0x003FF000UL)
+
+#if defined(SGX_FEATURE_36BIT_MMU)
+	#define SGX_MMU_PTE_ADDR_MASK			(0xFFFFFF00UL)
+	#define SGX_MMU_PTE_ADDR_ALIGNSHIFT		(4)
+#else
+	#define SGX_MMU_PTE_ADDR_MASK			(0xFFFFF000UL)
+	#define SGX_MMU_PTE_ADDR_ALIGNSHIFT		(0)
+#endif
+#define SGX_MMU_PTE_VALID				(0x00000001UL)
+#define SGX_MMU_PTE_WRITEONLY			(0x00000002UL)
+#define SGX_MMU_PTE_READONLY			(0x00000004UL)
+#define SGX_MMU_PTE_CACHECONSISTENT		(0x00000008UL)
+#define SGX_MMU_PTE_EDMPROTECT			(0x00000010UL)
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/buffer_manager.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/buffer_manager.h
new file mode 100644
index 0000000..c792292
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/buffer_manager.h
@@ -0,0 +1,214 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _BUFFER_MANAGER_H_
+#define _BUFFER_MANAGER_H_
+
+#include "img_types.h"
+#include "ra.h"
+#include "perproc.h"
+
+#if defined(__cplusplus)
+extern "C"{
+#endif
+
+typedef struct _BM_HEAP_ BM_HEAP;
+
+struct _BM_MAPPING_
+{
+	enum
+	{
+		hm_wrapped = 1,
+		hm_wrapped_scatter,
+		hm_wrapped_virtaddr,
+		hm_wrapped_scatter_virtaddr,
+		hm_env,
+		hm_contiguous
+	} eCpuMemoryOrigin;
+
+	BM_HEAP				*pBMHeap;
+	RA_ARENA			*pArena;
+
+	IMG_CPU_VIRTADDR	CpuVAddr;
+	IMG_CPU_PHYADDR		CpuPAddr;
+	IMG_DEV_VIRTADDR	DevVAddr;
+	IMG_SYS_PHYADDR		*psSysAddr;
+	IMG_SIZE_T			uSize;
+    IMG_HANDLE          hOSMemHandle;
+	IMG_UINT32			ui32Flags;
+};
+
+typedef struct _BM_BUF_
+{
+	IMG_CPU_VIRTADDR	*CpuVAddr;
+    IMG_VOID            *hOSMemHandle;
+	IMG_CPU_PHYADDR		CpuPAddr;
+	IMG_DEV_VIRTADDR	DevVAddr;
+
+	BM_MAPPING			*pMapping;
+	IMG_UINT32			ui32RefCount;
+	IMG_UINT32			ui32ExportCount;
+} BM_BUF;
+
+struct _BM_HEAP_
+{
+	IMG_UINT32				ui32Attribs;
+	BM_CONTEXT				*pBMContext;
+	RA_ARENA				*pImportArena;
+	RA_ARENA				*pLocalDevMemArena;
+	RA_ARENA				*pVMArena;
+	DEV_ARENA_DESCRIPTOR	sDevArena;
+	MMU_HEAP				*pMMUHeap;
+
+	struct _BM_HEAP_ 		*psNext;
+	struct _BM_HEAP_ 		**ppsThis;
+};
+
+struct _BM_CONTEXT_
+{
+	MMU_CONTEXT	*psMMUContext;
+
+
+	 BM_HEAP *psBMHeap;
+
+
+	 BM_HEAP *psBMSharedHeap;
+
+	PVRSRV_DEVICE_NODE *psDeviceNode;
+
+
+	HASH_TABLE *pBufferHash;
+
+
+	IMG_HANDLE hResItem;
+
+	IMG_UINT32 ui32RefCount;
+
+
+
+	struct _BM_CONTEXT_ *psNext;
+	struct _BM_CONTEXT_ **ppsThis;
+};
+
+
+
+typedef IMG_VOID *BM_HANDLE;
+
+#define BP_POOL_MASK         0x7
+
+#define BP_CONTIGUOUS			(1 << 3)
+#define BP_PARAMBUFFER			(1 << 4)
+
+#define BM_MAX_DEVMEM_ARENAS  2
+
+IMG_HANDLE
+BM_CreateContext(PVRSRV_DEVICE_NODE			*psDeviceNode,
+				 IMG_DEV_PHYADDR			*psPDDevPAddr,
+				 PVRSRV_PER_PROCESS_DATA	*psPerProc,
+				 IMG_BOOL					*pbCreated);
+
+
+PVRSRV_ERROR
+BM_DestroyContext (IMG_HANDLE hBMContext,
+					IMG_BOOL *pbCreated);
+
+
+IMG_HANDLE
+BM_CreateHeap (IMG_HANDLE hBMContext,
+				DEVICE_MEMORY_HEAP_INFO *psDevMemHeapInfo);
+
+IMG_VOID
+BM_DestroyHeap (IMG_HANDLE hDevMemHeap);
+
+
+IMG_BOOL
+BM_Reinitialise (PVRSRV_DEVICE_NODE *psDeviceNode);
+
+IMG_BOOL
+BM_Alloc (IMG_HANDLE			hDevMemHeap,
+			IMG_DEV_VIRTADDR	*psDevVAddr,
+			IMG_SIZE_T			uSize,
+			IMG_UINT32			*pui32Flags,
+			IMG_UINT32			uDevVAddrAlignment,
+			BM_HANDLE			*phBuf);
+
+IMG_BOOL
+BM_Wrap (	IMG_HANDLE hDevMemHeap,
+		    IMG_SIZE_T ui32Size,
+			IMG_SIZE_T ui32Offset,
+			IMG_BOOL bPhysContig,
+			IMG_SYS_PHYADDR *psSysAddr,
+			IMG_VOID *pvCPUVAddr,
+			IMG_UINT32 *pui32Flags,
+			BM_HANDLE *phBuf);
+
+IMG_VOID
+BM_Free (BM_HANDLE hBuf,
+		IMG_UINT32 ui32Flags);
+
+
+IMG_CPU_VIRTADDR
+BM_HandleToCpuVaddr (BM_HANDLE hBuf);
+
+IMG_DEV_VIRTADDR
+BM_HandleToDevVaddr (BM_HANDLE hBuf);
+
+IMG_SYS_PHYADDR
+BM_HandleToSysPaddr (BM_HANDLE hBuf);
+
+IMG_HANDLE
+BM_HandleToOSMemHandle (BM_HANDLE hBuf);
+
+IMG_BOOL
+BM_ContiguousStatistics (IMG_UINT32 uFlags,
+                         IMG_UINT32 *pTotalBytes,
+                         IMG_UINT32 *pAvailableBytes);
+
+
+IMG_VOID BM_GetPhysPageAddr(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+								IMG_DEV_VIRTADDR sDevVPageAddr,
+								IMG_DEV_PHYADDR *psDevPAddr);
+
+PVRSRV_ERROR BM_GetHeapInfo(IMG_HANDLE hDevMemHeap,
+							PVRSRV_HEAP_INFO *psHeapInfo);
+
+MMU_CONTEXT* BM_GetMMUContext(IMG_HANDLE hDevMemHeap);
+
+MMU_CONTEXT* BM_GetMMUContextFromMemContext(IMG_HANDLE hDevMemContext);
+
+IMG_HANDLE BM_GetMMUHeap(IMG_HANDLE hDevMemHeap);
+
+PVRSRV_DEVICE_NODE* BM_GetDeviceNode(IMG_HANDLE hDevMemContext);
+
+
+IMG_HANDLE BM_GetMappingHandle(PVRSRV_KERNEL_MEM_INFO *psMemInfo);
+
+IMG_VOID BM_Export(BM_HANDLE hBuf);
+
+IMG_VOID BM_FreeExport(BM_HANDLE hBuf, IMG_UINT32 ui32Flags);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/device.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/device.h
new file mode 100644
index 0000000..c7fa61c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/device.h
@@ -0,0 +1,274 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __DEVICE_H__
+#define __DEVICE_H__
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#include "ra.h"
+#include "resman.h"
+
+typedef struct _BM_CONTEXT_ BM_CONTEXT;
+
+typedef struct _MMU_HEAP_ MMU_HEAP;
+typedef struct _MMU_CONTEXT_ MMU_CONTEXT;
+
+#define PVRSRV_BACKINGSTORE_SYSMEM_CONTIG		(1<<(PVRSRV_MEM_BACKINGSTORE_FIELD_SHIFT+0))
+#define PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG	(1<<(PVRSRV_MEM_BACKINGSTORE_FIELD_SHIFT+1))
+#define PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG		(1<<(PVRSRV_MEM_BACKINGSTORE_FIELD_SHIFT+2))
+#define PVRSRV_BACKINGSTORE_LOCALMEM_NONCONTIG	(1<<(PVRSRV_MEM_BACKINGSTORE_FIELD_SHIFT+3))
+
+typedef IMG_UINT32 DEVICE_MEMORY_HEAP_TYPE;
+#define DEVICE_MEMORY_HEAP_PERCONTEXT		0
+#define DEVICE_MEMORY_HEAP_KERNEL			1
+#define DEVICE_MEMORY_HEAP_SHARED			2
+#define DEVICE_MEMORY_HEAP_SHARED_EXPORTED	3
+
+#define PVRSRV_DEVICE_NODE_FLAGS_PORT80DISPLAY	1
+#define PVRSRV_DEVICE_NODE_FLAGS_MMU_OPT_INV	2
+
+typedef struct _DEVICE_MEMORY_HEAP_INFO_
+{
+
+	IMG_UINT32				ui32HeapID;
+
+
+	IMG_CHAR				*pszName;
+
+
+	IMG_CHAR				*pszBSName;
+
+
+	IMG_DEV_VIRTADDR		sDevVAddrBase;
+
+
+	IMG_UINT32				ui32HeapSize;
+
+
+	IMG_UINT32				ui32Attribs;
+
+
+	DEVICE_MEMORY_HEAP_TYPE	DevMemHeapType;
+
+
+	IMG_HANDLE				hDevMemHeap;
+
+
+	RA_ARENA				*psLocalDevMemArena;
+
+
+	IMG_UINT32				ui32DataPageSize;
+
+} DEVICE_MEMORY_HEAP_INFO;
+
+typedef struct _DEVICE_MEMORY_INFO_
+{
+
+	IMG_UINT32				ui32AddressSpaceSizeLog2;
+
+
+
+
+	IMG_UINT32				ui32Flags;
+
+
+	IMG_UINT32				ui32HeapCount;
+
+
+	IMG_UINT32				ui32SyncHeapID;
+
+
+	IMG_UINT32				ui32MappingHeapID;
+
+
+	DEVICE_MEMORY_HEAP_INFO	*psDeviceMemoryHeap;
+
+
+    BM_CONTEXT				*pBMKernelContext;
+
+
+    BM_CONTEXT				*pBMContext;
+
+} DEVICE_MEMORY_INFO;
+
+
+typedef struct DEV_ARENA_DESCRIPTOR_TAG
+{
+	IMG_UINT32				ui32HeapID;
+
+	IMG_CHAR				*pszName;
+
+	IMG_DEV_VIRTADDR		BaseDevVAddr;
+
+	IMG_UINT32 				ui32Size;
+
+	DEVICE_MEMORY_HEAP_TYPE	DevMemHeapType;
+
+
+	IMG_UINT32				ui32DataPageSize;
+
+	DEVICE_MEMORY_HEAP_INFO	*psDeviceMemoryHeapInfo;
+
+} DEV_ARENA_DESCRIPTOR;
+
+typedef struct _SYS_DATA_TAG_ *PSYS_DATA;
+
+typedef struct _PVRSRV_DEVICE_NODE_
+{
+	PVRSRV_DEVICE_IDENTIFIER	sDevId;
+	IMG_UINT32					ui32RefCount;
+
+
+
+
+	PVRSRV_ERROR			(*pfnInitDevice) (IMG_VOID*);
+
+	PVRSRV_ERROR			(*pfnDeInitDevice) (IMG_VOID*);
+
+
+	PVRSRV_ERROR			(*pfnInitDeviceCompatCheck) (struct _PVRSRV_DEVICE_NODE_*);
+
+
+	PVRSRV_ERROR			(*pfnMMUInitialise)(struct _PVRSRV_DEVICE_NODE_*, MMU_CONTEXT**, IMG_DEV_PHYADDR*);
+	IMG_VOID				(*pfnMMUFinalise)(MMU_CONTEXT*);
+	IMG_VOID				(*pfnMMUInsertHeap)(MMU_CONTEXT*, MMU_HEAP*);
+	MMU_HEAP*				(*pfnMMUCreate)(MMU_CONTEXT*,DEV_ARENA_DESCRIPTOR*,RA_ARENA**);
+	IMG_VOID				(*pfnMMUDelete)(MMU_HEAP*);
+	IMG_BOOL				(*pfnMMUAlloc)(MMU_HEAP*pMMU,
+										   IMG_SIZE_T uSize,
+										   IMG_SIZE_T *pActualSize,
+										   IMG_UINT32 uFlags,
+										   IMG_UINT32 uDevVAddrAlignment,
+										   IMG_DEV_VIRTADDR *pDevVAddr);
+	IMG_VOID				(*pfnMMUFree)(MMU_HEAP*,IMG_DEV_VIRTADDR,IMG_UINT32);
+	IMG_VOID 				(*pfnMMUEnable)(MMU_HEAP*);
+	IMG_VOID				(*pfnMMUDisable)(MMU_HEAP*);
+	IMG_VOID				(*pfnMMUMapPages)(MMU_HEAP *pMMU,
+											  IMG_DEV_VIRTADDR devVAddr,
+											  IMG_SYS_PHYADDR SysPAddr,
+											  IMG_SIZE_T uSize,
+											  IMG_UINT32 ui32MemFlags,
+											  IMG_HANDLE hUniqueTag);
+	IMG_VOID				(*pfnMMUMapShadow)(MMU_HEAP            *pMMU,
+											   IMG_DEV_VIRTADDR    MapBaseDevVAddr,
+											   IMG_SIZE_T          uSize,
+											   IMG_CPU_VIRTADDR    CpuVAddr,
+											   IMG_HANDLE          hOSMemHandle,
+											   IMG_DEV_VIRTADDR    *pDevVAddr,
+											   IMG_UINT32 ui32MemFlags,
+											   IMG_HANDLE hUniqueTag);
+	IMG_VOID				(*pfnMMUUnmapPages)(MMU_HEAP *pMMU,
+												IMG_DEV_VIRTADDR dev_vaddr,
+												IMG_UINT32 ui32PageCount,
+												IMG_HANDLE hUniqueTag);
+
+	IMG_VOID				(*pfnMMUMapScatter)(MMU_HEAP *pMMU,
+												IMG_DEV_VIRTADDR DevVAddr,
+												IMG_SYS_PHYADDR *psSysAddr,
+												IMG_SIZE_T uSize,
+												IMG_UINT32 ui32MemFlags,
+												IMG_HANDLE hUniqueTag);
+
+	IMG_DEV_PHYADDR			(*pfnMMUGetPhysPageAddr)(MMU_HEAP *pMMUHeap, IMG_DEV_VIRTADDR sDevVPageAddr);
+	IMG_DEV_PHYADDR			(*pfnMMUGetPDDevPAddr)(MMU_CONTEXT *pMMUContext);
+
+
+	IMG_BOOL				(*pfnDeviceISR)(IMG_VOID*);
+
+	IMG_VOID				*pvISRData;
+
+	IMG_UINT32 				ui32SOCInterruptBit;
+
+	IMG_VOID				(*pfnDeviceMISR)(IMG_VOID*);
+
+
+	IMG_VOID				(*pfnDeviceCommandComplete)(struct _PVRSRV_DEVICE_NODE_ *psDeviceNode);
+
+	IMG_BOOL				bReProcessDeviceCommandComplete;
+
+
+	DEVICE_MEMORY_INFO		sDevMemoryInfo;
+
+
+	IMG_VOID				*pvDevice;
+	IMG_UINT32				ui32pvDeviceSize;
+
+
+	PRESMAN_CONTEXT			hResManContext;
+
+
+	PSYS_DATA				psSysData;
+
+
+	RA_ARENA				*psLocalDevMemArena;
+
+	IMG_UINT32				ui32Flags;
+
+	struct _PVRSRV_DEVICE_NODE_	*psNext;
+	struct _PVRSRV_DEVICE_NODE_	**ppsThis;
+} PVRSRV_DEVICE_NODE;
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVRegisterDevice(PSYS_DATA psSysData,
+											  PVRSRV_ERROR (*pfnRegisterDevice)(PVRSRV_DEVICE_NODE*),
+											  IMG_UINT32 ui32SOCInterruptBit,
+			 								  IMG_UINT32 *pui32DeviceIndex );
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVInitialiseDevice(IMG_UINT32 ui32DevIndex);
+PVRSRV_ERROR IMG_CALLCONV PVRSRVFinaliseSystem(IMG_BOOL bInitSuccesful);
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDevInitCompatCheck(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDeinitialiseDevice(IMG_UINT32 ui32DevIndex);
+
+#if !defined(USE_CODE)
+
+IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV PollForValueKM(volatile IMG_UINT32* pui32LinMemAddr,
+												   IMG_UINT32 ui32Value,
+												   IMG_UINT32 ui32Mask,
+												   IMG_UINT32 ui32Waitus,
+												   IMG_UINT32 ui32Tries);
+
+#endif
+
+
+#if defined (USING_ISR_INTERRUPTS)
+PVRSRV_ERROR IMG_CALLCONV PollForInterruptKM(IMG_UINT32 ui32Value,
+								IMG_UINT32 ui32Mask,
+								IMG_UINT32 ui32Waitus,
+								IMG_UINT32 ui32Tries);
+#endif
+
+PVRSRV_ERROR IMG_CALLCONV PVRSRVInit(PSYS_DATA psSysData);
+IMG_VOID IMG_CALLCONV PVRSRVDeInit(PSYS_DATA psSysData);
+IMG_BOOL IMG_CALLCONV PVRSRVDeviceLISR(PVRSRV_DEVICE_NODE *psDeviceNode);
+IMG_BOOL IMG_CALLCONV PVRSRVSystemLISR(IMG_VOID *pvSysData);
+IMG_VOID IMG_CALLCONV PVRSRVMISR(IMG_VOID *pvSysData);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/handle.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/handle.h
new file mode 100644
index 0000000..ffa952f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/handle.h
@@ -0,0 +1,378 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __HANDLE_H__
+#define __HANDLE_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "img_types.h"
+#include "hash.h"
+#include "resman.h"
+
+typedef enum
+{
+	PVRSRV_HANDLE_TYPE_NONE = 0,
+	PVRSRV_HANDLE_TYPE_PERPROC_DATA,
+	PVRSRV_HANDLE_TYPE_DEV_NODE,
+	PVRSRV_HANDLE_TYPE_DEV_MEM_CONTEXT,
+	PVRSRV_HANDLE_TYPE_DEV_MEM_HEAP,
+	PVRSRV_HANDLE_TYPE_MEM_INFO,
+	PVRSRV_HANDLE_TYPE_SYNC_INFO,
+	PVRSRV_HANDLE_TYPE_DISP_INFO,
+	PVRSRV_HANDLE_TYPE_DISP_SWAP_CHAIN,
+	PVRSRV_HANDLE_TYPE_BUF_INFO,
+	PVRSRV_HANDLE_TYPE_DISP_BUFFER,
+	PVRSRV_HANDLE_TYPE_BUF_BUFFER,
+	PVRSRV_HANDLE_TYPE_SGX_HW_RENDER_CONTEXT,
+	PVRSRV_HANDLE_TYPE_SGX_HW_TRANSFER_CONTEXT,
+	PVRSRV_HANDLE_TYPE_SGX_HW_2D_CONTEXT,
+	PVRSRV_HANDLE_TYPE_SHARED_PB_DESC,
+	PVRSRV_HANDLE_TYPE_MEM_INFO_REF,
+	PVRSRV_HANDLE_TYPE_SHARED_SYS_MEM_INFO,
+	PVRSRV_HANDLE_TYPE_SHARED_EVENT_OBJECT,
+	PVRSRV_HANDLE_TYPE_EVENT_OBJECT_CONNECT,
+	PVRSRV_HANDLE_TYPE_MMAP_INFO,
+	PVRSRV_HANDLE_TYPE_SOC_TIMER
+} PVRSRV_HANDLE_TYPE;
+
+typedef enum
+{
+
+	PVRSRV_HANDLE_ALLOC_FLAG_NONE = 		0,
+
+	PVRSRV_HANDLE_ALLOC_FLAG_SHARED = 		0x01,
+
+	PVRSRV_HANDLE_ALLOC_FLAG_MULTI = 		0x02,
+
+	PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE = 		0x04
+} PVRSRV_HANDLE_ALLOC_FLAG;
+
+struct _PVRSRV_HANDLE_BASE_;
+typedef struct _PVRSRV_HANDLE_BASE_ PVRSRV_HANDLE_BASE;
+
+#ifdef	PVR_SECURE_HANDLES
+extern PVRSRV_HANDLE_BASE *gpsKernelHandleBase;
+
+#define	KERNEL_HANDLE_BASE (gpsKernelHandleBase)
+
+PVRSRV_ERROR PVRSRVAllocHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag);
+
+PVRSRV_ERROR PVRSRVAllocSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag, IMG_HANDLE hParent);
+
+PVRSRV_ERROR PVRSRVFindHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType);
+
+PVRSRV_ERROR PVRSRVLookupHandleAnyType(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, PVRSRV_HANDLE_TYPE *peType, IMG_HANDLE hHandle);
+
+PVRSRV_ERROR PVRSRVLookupHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType);
+
+PVRSRV_ERROR PVRSRVLookupSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType, IMG_HANDLE hAncestor);
+
+PVRSRV_ERROR PVRSRVGetParentHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *phParent, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType);
+
+PVRSRV_ERROR PVRSRVLookupAndReleaseHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType);
+
+PVRSRV_ERROR PVRSRVReleaseHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType);
+
+PVRSRV_ERROR PVRSRVNewHandleBatch(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32BatchSize);
+
+PVRSRV_ERROR PVRSRVCommitHandleBatch(PVRSRV_HANDLE_BASE *psBase);
+
+IMG_VOID PVRSRVReleaseHandleBatch(PVRSRV_HANDLE_BASE *psBase);
+
+PVRSRV_ERROR PVRSRVSetMaxHandle(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32MaxHandle);
+
+IMG_UINT32 PVRSRVGetMaxHandle(PVRSRV_HANDLE_BASE *psBase);
+
+PVRSRV_ERROR PVRSRVEnableHandlePurging(PVRSRV_HANDLE_BASE *psBase);
+
+PVRSRV_ERROR PVRSRVPurgeHandles(PVRSRV_HANDLE_BASE *psBase);
+
+PVRSRV_ERROR PVRSRVAllocHandleBase(PVRSRV_HANDLE_BASE **ppsBase);
+
+PVRSRV_ERROR PVRSRVFreeHandleBase(PVRSRV_HANDLE_BASE *psBase);
+
+PVRSRV_ERROR PVRSRVHandleInit(IMG_VOID);
+
+PVRSRV_ERROR PVRSRVHandleDeInit(IMG_VOID);
+
+#else
+
+#define KERNEL_HANDLE_BASE IMG_NULL
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVAllocHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVAllocHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag)
+{
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(eFlag);
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	*phHandle = pvData;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVAllocSubHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVAllocSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag, IMG_HANDLE hParent)
+{
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(eFlag);
+	PVR_UNREFERENCED_PARAMETER(hParent);
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	*phHandle = pvData;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVFindHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVFindHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType)
+{
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	*phHandle = pvData;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVLookupHandleAnyType)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVLookupHandleAnyType(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, PVRSRV_HANDLE_TYPE *peType, IMG_HANDLE hHandle)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	*peType = PVRSRV_HANDLE_TYPE_NONE;
+
+	*ppvData = hHandle;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVLookupHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVLookupHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+	PVR_UNREFERENCED_PARAMETER(eType);
+
+	*ppvData = hHandle;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVLookupSubHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVLookupSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType, IMG_HANDLE hAncestor)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(hAncestor);
+
+	*ppvData = hHandle;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVGetParentHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVGetParentHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *phParent, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(hHandle);
+
+	*phParent = IMG_NULL;
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVLookupAndReleaseHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVLookupAndReleaseHandle(PVRSRV_HANDLE_BASE *psBase, IMG_PVOID *ppvData, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	*ppvData = hHandle;
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVReleaseHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVReleaseHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE hHandle, PVRSRV_HANDLE_TYPE eType)
+{
+	PVR_UNREFERENCED_PARAMETER(hHandle);
+	PVR_UNREFERENCED_PARAMETER(eType);
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVNewHandleBatch)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVNewHandleBatch(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32BatchSize)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+	PVR_UNREFERENCED_PARAMETER(ui32BatchSize);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVCommitHandleBatch)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVCommitHandleBatch(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVReleaseHandleBatch)
+#endif
+static INLINE
+IMG_VOID PVRSRVReleaseHandleBatch(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVSetMaxHandle)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVSetMaxHandle(PVRSRV_HANDLE_BASE *psBase, IMG_UINT32 ui32MaxHandle)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+	PVR_UNREFERENCED_PARAMETER(ui32MaxHandle);
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVGetMaxHandle)
+#endif
+static INLINE
+IMG_UINT32 PVRSRVGetMaxHandle(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	return 0;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVEnableHandlePurging)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVEnableHandlePurging(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVPurgeHandles)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVPurgeHandles(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVAllocHandleBase)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVAllocHandleBase(PVRSRV_HANDLE_BASE **ppsBase)
+{
+	*ppsBase = IMG_NULL;
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVFreeHandleBase)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVFreeHandleBase(PVRSRV_HANDLE_BASE *psBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psBase);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVHandleInit)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVHandleInit(IMG_VOID)
+{
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVHandleDeInit)
+#endif
+static INLINE
+PVRSRV_ERROR PVRSRVHandleDeInit(IMG_VOID)
+{
+	return PVRSRV_OK;
+}
+
+#endif
+
+#define PVRSRVAllocHandleNR(psBase, phHandle, pvData, eType, eFlag) \
+	(IMG_VOID)PVRSRVAllocHandle(psBase, phHandle, pvData, eType, eFlag)
+
+#define PVRSRVAllocSubHandleNR(psBase, phHandle, pvData, eType, eFlag, hParent) \
+	(IMG_VOID)PVRSRVAllocSubHandle(psBase, phHandle, pvData, eType, eFlag, hParent)
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/hash.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/hash.h
new file mode 100644
index 0000000..26c8e44
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/hash.h
@@ -0,0 +1,69 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _HASH_H_
+#define _HASH_H_
+
+#include "img_types.h"
+#include "osfunc.h"
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+typedef IMG_UINT32 HASH_FUNC(IMG_SIZE_T uKeySize, IMG_VOID *pKey, IMG_UINT32 uHashTabLen);
+typedef IMG_BOOL HASH_KEY_COMP(IMG_SIZE_T uKeySize, IMG_VOID *pKey1, IMG_VOID *pKey2);
+
+typedef struct _HASH_TABLE_ HASH_TABLE;
+
+IMG_UINT32 HASH_Func_Default (IMG_SIZE_T uKeySize, IMG_VOID *pKey, IMG_UINT32 uHashTabLen);
+
+IMG_BOOL HASH_Key_Comp_Default (IMG_SIZE_T uKeySize, IMG_VOID *pKey1, IMG_VOID *pKey2);
+
+HASH_TABLE * HASH_Create_Extended (IMG_UINT32 uInitialLen, IMG_SIZE_T uKeySize, HASH_FUNC *pfnHashFunc, HASH_KEY_COMP *pfnKeyComp);
+
+HASH_TABLE * HASH_Create (IMG_UINT32 uInitialLen);
+
+IMG_VOID HASH_Delete (HASH_TABLE *pHash);
+
+IMG_BOOL HASH_Insert_Extended (HASH_TABLE *pHash, IMG_VOID *pKey, IMG_UINTPTR_T v);
+
+IMG_BOOL HASH_Insert (HASH_TABLE *pHash, IMG_UINTPTR_T k, IMG_UINTPTR_T v);
+
+IMG_UINTPTR_T HASH_Remove_Extended(HASH_TABLE *pHash, IMG_VOID *pKey);
+
+IMG_UINTPTR_T HASH_Remove (HASH_TABLE *pHash, IMG_UINTPTR_T k);
+
+IMG_UINTPTR_T HASH_Retrieve_Extended (HASH_TABLE *pHash, IMG_VOID *pKey);
+
+IMG_UINTPTR_T HASH_Retrieve (HASH_TABLE *pHash, IMG_UINTPTR_T k);
+
+#ifdef HASH_TRACE
+IMG_VOID HASH_Dump (HASH_TABLE *pHash);
+#endif
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/lists.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/lists.h
new file mode 100644
index 0000000..70ef61d
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/lists.h
@@ -0,0 +1,172 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __LISTS_UTILS__
+#define __LISTS_UTILS__
+
+#include <stdarg.h>
+#include "img_types.h"
+
+#define DECLARE_LIST_FOR_EACH(TYPE) \
+IMG_VOID List_##TYPE##_ForEach(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))
+
+#define IMPLEMENT_LIST_FOR_EACH(TYPE) \
+IMG_VOID List_##TYPE##_ForEach(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))\
+{\
+	while(psHead)\
+	{\
+		pfnCallBack(psHead);\
+		psHead = psHead->psNext;\
+	}\
+}
+
+
+#define DECLARE_LIST_FOR_EACH_VA(TYPE) \
+IMG_VOID List_##TYPE##_ForEach_va(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode, va_list va), ...)
+
+#define IMPLEMENT_LIST_FOR_EACH_VA(TYPE) \
+IMG_VOID List_##TYPE##_ForEach_va(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode, va_list va), ...) \
+{\
+	va_list ap;\
+	while(psHead)\
+	{\
+		va_start(ap, pfnCallBack);\
+		pfnCallBack(psHead, ap);\
+		psHead = psHead->psNext;\
+		va_end(ap);\
+	}\
+}
+
+
+#define DECLARE_LIST_ANY(TYPE) \
+IMG_VOID* List_##TYPE##_Any(TYPE *psHead, IMG_VOID* (*pfnCallBack)(TYPE* psNode))
+
+#define IMPLEMENT_LIST_ANY(TYPE) \
+IMG_VOID* List_##TYPE##_Any(TYPE *psHead, IMG_VOID* (*pfnCallBack)(TYPE* psNode))\
+{ \
+	IMG_VOID *pResult;\
+	TYPE *psNextNode;\
+	pResult = IMG_NULL;\
+	psNextNode = psHead;\
+	while(psHead && !pResult)\
+	{\
+		psNextNode = psNextNode->psNext;\
+		pResult = pfnCallBack(psHead);\
+		psHead = psNextNode;\
+	}\
+	return pResult;\
+}
+
+
+#define DECLARE_LIST_ANY_VA(TYPE) \
+IMG_VOID* List_##TYPE##_Any_va(TYPE *psHead, IMG_VOID*(*pfnCallBack)(TYPE* psNode, va_list va), ...)
+
+#define IMPLEMENT_LIST_ANY_VA(TYPE) \
+IMG_VOID* List_##TYPE##_Any_va(TYPE *psHead, IMG_VOID*(*pfnCallBack)(TYPE* psNode, va_list va), ...)\
+{\
+	va_list ap;\
+	TYPE *psNextNode;\
+	IMG_VOID* pResult = IMG_NULL;\
+	while(psHead && !pResult)\
+	{\
+		psNextNode = psHead->psNext;\
+		va_start(ap, pfnCallBack);\
+		pResult = pfnCallBack(psHead, ap);\
+		va_end(ap);\
+		psHead = psNextNode;\
+	}\
+	return pResult;\
+}
+
+#define DECLARE_LIST_ANY_2(TYPE, RTYPE, CONTINUE) \
+RTYPE List_##TYPE##_##RTYPE##_Any(TYPE *psHead, RTYPE (*pfnCallBack)(TYPE* psNode))
+
+#define IMPLEMENT_LIST_ANY_2(TYPE, RTYPE, CONTINUE) \
+RTYPE List_##TYPE##_##RTYPE##_Any(TYPE *psHead, RTYPE (*pfnCallBack)(TYPE* psNode))\
+{ \
+	RTYPE result;\
+	TYPE *psNextNode;\
+	result = CONTINUE;\
+	psNextNode = psHead;\
+	while(psHead && result == CONTINUE)\
+	{\
+		psNextNode = psNextNode->psNext;\
+		result = pfnCallBack(psHead);\
+		psHead = psNextNode;\
+	}\
+	return result;\
+}
+
+
+#define DECLARE_LIST_ANY_VA_2(TYPE, RTYPE, CONTINUE) \
+RTYPE List_##TYPE##_##RTYPE##_Any_va(TYPE *psHead, RTYPE(*pfnCallBack)(TYPE* psNode, va_list va), ...)
+
+#define IMPLEMENT_LIST_ANY_VA_2(TYPE, RTYPE, CONTINUE) \
+RTYPE List_##TYPE##_##RTYPE##_Any_va(TYPE *psHead, RTYPE(*pfnCallBack)(TYPE* psNode, va_list va), ...)\
+{\
+	va_list ap;\
+	TYPE *psNextNode;\
+	RTYPE result = CONTINUE;\
+	while(psHead && result == CONTINUE)\
+	{\
+		psNextNode = psHead->psNext;\
+		va_start(ap, pfnCallBack);\
+		result = pfnCallBack(psHead, ap);\
+		va_end(ap);\
+		psHead = psNextNode;\
+	}\
+	return result;\
+}
+
+
+#define DECLARE_LIST_REMOVE(TYPE) \
+IMG_VOID List_##TYPE##_Remove(TYPE *psNode)
+
+#define IMPLEMENT_LIST_REMOVE(TYPE) \
+IMG_VOID List_##TYPE##_Remove(TYPE *psNode)\
+{\
+	(*psNode->ppsThis)=psNode->psNext;\
+	if(psNode->psNext)\
+	{\
+		psNode->psNext->ppsThis = psNode->ppsThis;\
+	}\
+}
+
+#define DECLARE_LIST_INSERT(TYPE) \
+IMG_VOID List_##TYPE##_Insert(TYPE **ppsHead, TYPE *psNewNode)
+
+#define IMPLEMENT_LIST_INSERT(TYPE) \
+IMG_VOID List_##TYPE##_Insert(TYPE **ppsHead, TYPE *psNewNode)\
+{\
+	psNewNode->ppsThis = ppsHead;\
+	psNewNode->psNext = *ppsHead;\
+	*ppsHead = psNewNode;\
+	if(psNewNode->psNext)\
+	{\
+		psNewNode->psNext->ppsThis = &(psNewNode->psNext);\
+	}\
+}
+
+
+#define IS_LAST_ELEMENT(x) ((x)->psNext == IMG_NULL)
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/metrics.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/metrics.h
new file mode 100644
index 0000000..1ea12de
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/metrics.h
@@ -0,0 +1,126 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _METRICS_
+#define _METRICS_
+
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+
+#if defined(DEBUG) || defined(TIMING)
+
+
+typedef struct
+{
+	IMG_UINT32 ui32Start;
+	IMG_UINT32 ui32Stop;
+	IMG_UINT32 ui32Total;
+	IMG_UINT32 ui32Count;
+} Temporal_Data;
+
+extern Temporal_Data asTimers[];
+
+extern IMG_UINT32 PVRSRVTimeNow(IMG_VOID);
+extern IMG_VOID   PVRSRVSetupMetricTimers(IMG_VOID *pvDevInfo);
+extern IMG_VOID   PVRSRVOutputMetricTotals(IMG_VOID);
+
+
+#define PVRSRV_TIMER_DUMMY				0
+
+#define PVRSRV_TIMER_EXAMPLE_1			1
+#define PVRSRV_TIMER_EXAMPLE_2			2
+
+
+#define PVRSRV_NUM_TIMERS		(PVRSRV_TIMER_EXAMPLE_2 + 1)
+
+#define PVRSRV_TIME_START(X)	{ \
+									asTimers[X].ui32Count += 1; \
+									asTimers[X].ui32Count |= 0x80000000L; \
+									asTimers[X].ui32Start = PVRSRVTimeNow(); \
+									asTimers[X].ui32Stop  = 0; \
+								}
+
+#define PVRSRV_TIME_SUSPEND(X)	{ \
+									asTimers[X].ui32Stop += PVRSRVTimeNow() - asTimers[X].ui32Start; \
+								}
+
+#define PVRSRV_TIME_RESUME(X)	{ \
+									asTimers[X].ui32Start = PVRSRVTimeNow(); \
+								}
+
+#define PVRSRV_TIME_STOP(X)		{ \
+									asTimers[X].ui32Stop  += PVRSRVTimeNow() - asTimers[X].ui32Start; \
+									asTimers[X].ui32Total += asTimers[X].ui32Stop; \
+									asTimers[X].ui32Count &= 0x7FFFFFFFL; \
+								}
+
+#define PVRSRV_TIME_RESET(X)	{ \
+									asTimers[X].ui32Start = 0; \
+									asTimers[X].ui32Stop  = 0; \
+									asTimers[X].ui32Total = 0; \
+									asTimers[X].ui32Count = 0; \
+								}
+
+
+#if defined(__sh__)
+
+#define TST_REG   ((volatile IMG_UINT8 *) (psDevInfo->pvSOCRegsBaseKM))
+
+#define TCOR_2    ((volatile IMG_UINT *)  (psDevInfo->pvSOCRegsBaseKM+28))
+#define TCNT_2    ((volatile IMG_UINT *)  (psDevInfo->pvSOCRegsBaseKM+32))
+#define TCR_2     ((volatile IMG_UINT16 *)(psDevInfo->pvSOCRegsBaseKM+36))
+
+#define TIMER_DIVISOR  4
+
+#endif
+
+
+
+
+
+#else
+
+
+
+#define PVRSRV_TIME_START(X)
+#define PVRSRV_TIME_SUSPEND(X)
+#define PVRSRV_TIME_RESUME(X)
+#define PVRSRV_TIME_STOP(X)
+#define PVRSRV_TIME_RESET(X)
+
+#define PVRSRVSetupMetricTimers(X)
+#define PVRSRVOutputMetricTotals()
+
+
+
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osfunc.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osfunc.h
new file mode 100644
index 0000000..7a0b064
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osfunc.h
@@ -0,0 +1,483 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifdef DEBUG_RELEASE_BUILD
+#pragma optimize( "", off )
+#define DEBUG		1
+#endif
+
+#ifndef __OSFUNC_H__
+#define __OSFUNC_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#if defined(__linux__) && defined(__KERNEL__)
+#include <linux/hardirq.h>
+#include <linux/string.h>
+#endif
+
+
+
+	#define PVRSRV_PAGEABLE_SELECT		PVRSRV_OS_PAGEABLE_HEAP
+
+#define KERNEL_ID			0xffffffffL
+#define POWER_MANAGER_ID	0xfffffffeL
+#define ISR_ID				0xfffffffdL
+#define TIMER_ID			0xfffffffcL
+
+
+#define HOST_PAGESIZE			OSGetPageSize
+#define HOST_PAGEMASK			(~(HOST_PAGESIZE()-1))
+#define HOST_PAGEALIGN(addr)	(((addr)+HOST_PAGESIZE()-1)&HOST_PAGEMASK)
+
+#define PVRSRV_OS_HEAP_MASK			0xf
+#define PVRSRV_OS_PAGEABLE_HEAP		0x1
+#define PVRSRV_OS_NON_PAGEABLE_HEAP	0x2
+
+
+IMG_UINT32 OSClockus(IMG_VOID);
+IMG_SIZE_T OSGetPageSize(IMG_VOID);
+PVRSRV_ERROR OSInstallDeviceLISR(IMG_VOID *pvSysData,
+								 IMG_UINT32 ui32Irq,
+								 IMG_CHAR *pszISRName,
+								 IMG_VOID *pvDeviceNode);
+PVRSRV_ERROR OSUninstallDeviceLISR(IMG_VOID *pvSysData);
+PVRSRV_ERROR OSInstallSystemLISR(IMG_VOID *pvSysData, IMG_UINT32 ui32Irq);
+PVRSRV_ERROR OSUninstallSystemLISR(IMG_VOID *pvSysData);
+PVRSRV_ERROR OSInstallMISR(IMG_VOID *pvSysData);
+PVRSRV_ERROR OSUninstallMISR(IMG_VOID *pvSysData);
+IMG_CPU_PHYADDR OSMapLinToCPUPhys(IMG_VOID* pvLinAddr);
+IMG_VOID OSMemCopy(IMG_VOID *pvDst, IMG_VOID *pvSrc, IMG_SIZE_T ui32Size);
+IMG_VOID *OSMapPhysToLin(IMG_CPU_PHYADDR BasePAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_HANDLE *phOSMemHandle);
+IMG_BOOL OSUnMapPhysToLin(IMG_VOID *pvLinAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_HANDLE hOSMemHandle);
+
+PVRSRV_ERROR OSReservePhys(IMG_CPU_PHYADDR BasePAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_VOID **ppvCpuVAddr, IMG_HANDLE *phOSMemHandle);
+PVRSRV_ERROR OSUnReservePhys(IMG_VOID *pvCpuVAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_HANDLE hOSMemHandle);
+
+#if defined(SUPPORT_CPU_CACHED_BUFFERS)
+IMG_VOID OSFlushCPUCacheKM(IMG_VOID);
+IMG_VOID OSFlushCPUCacheRangeKM(IMG_VOID *pvRangeAddrStart,
+						 	IMG_VOID *pvRangeAddrEnd);
+#endif
+
+#if defined(__linux__)
+PVRSRV_ERROR OSRegisterDiscontigMem(IMG_SYS_PHYADDR *pBasePAddr,
+									IMG_VOID *pvCpuVAddr,
+									IMG_SIZE_T ui32Bytes,
+									IMG_UINT32 ui32Flags,
+									IMG_HANDLE *phOSMemHandle);
+PVRSRV_ERROR OSUnRegisterDiscontigMem(IMG_VOID *pvCpuVAddr,
+									IMG_SIZE_T ui32Bytes,
+									IMG_UINT32 ui32Flags,
+									IMG_HANDLE hOSMemHandle);
+#else
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSRegisterDiscontigMem)
+#endif
+static INLINE PVRSRV_ERROR OSRegisterDiscontigMem(IMG_SYS_PHYADDR *pBasePAddr,
+													IMG_VOID *pvCpuVAddr,
+													IMG_SIZE_T ui32Bytes,
+													IMG_UINT32 ui32Flags,
+													IMG_HANDLE *phOSMemHandle)
+{
+	PVR_UNREFERENCED_PARAMETER(pBasePAddr);
+	PVR_UNREFERENCED_PARAMETER(pvCpuVAddr);
+	PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+	PVR_UNREFERENCED_PARAMETER(phOSMemHandle);
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSUnRegisterDiscontigMem)
+#endif
+static INLINE PVRSRV_ERROR OSUnRegisterDiscontigMem(IMG_VOID *pvCpuVAddr,
+													IMG_SIZE_T ui32Bytes,
+													IMG_UINT32 ui32Flags,
+													IMG_HANDLE hOSMemHandle)
+{
+	PVR_UNREFERENCED_PARAMETER(pvCpuVAddr);
+	PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+	PVR_UNREFERENCED_PARAMETER(hOSMemHandle);
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+#endif
+
+
+#if defined(__linux__)
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSReserveDiscontigPhys)
+#endif
+static INLINE PVRSRV_ERROR OSReserveDiscontigPhys(IMG_SYS_PHYADDR *pBasePAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_VOID **ppvCpuVAddr, IMG_HANDLE *phOSMemHandle)
+{
+#if defined(__linux__)
+	*ppvCpuVAddr = IMG_NULL;
+	return OSRegisterDiscontigMem(pBasePAddr, *ppvCpuVAddr, ui32Bytes, ui32Flags, phOSMemHandle);
+#else
+	extern IMG_CPU_PHYADDR SysSysPAddrToCpuPAddr(IMG_SYS_PHYADDR SysPAddr);
+
+
+	return OSReservePhys(SysSysPAddrToCpuPAddr(pBasePAddr[0]), ui32Bytes, ui32Flags, ppvCpuVAddr, phOSMemHandle);
+#endif
+}
+
+static INLINE PVRSRV_ERROR OSUnReserveDiscontigPhys(IMG_VOID *pvCpuVAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_HANDLE hOSMemHandle)
+{
+#if defined(__linux__)
+	OSUnRegisterDiscontigMem(pvCpuVAddr, ui32Bytes, ui32Flags, hOSMemHandle);
+#endif
+
+	return PVRSRV_OK;
+}
+#else
+
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSReserveDiscontigPhys)
+#endif
+static INLINE PVRSRV_ERROR OSReserveDiscontigPhys(IMG_SYS_PHYADDR *pBasePAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_VOID **ppvCpuVAddr, IMG_HANDLE *phOSMemHandle)
+{
+	PVR_UNREFERENCED_PARAMETER(pBasePAddr);
+	PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+	PVR_UNREFERENCED_PARAMETER(ppvCpuVAddr);
+	PVR_UNREFERENCED_PARAMETER(phOSMemHandle);
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSUnReserveDiscontigPhys)
+#endif
+static INLINE PVRSRV_ERROR OSUnReserveDiscontigPhys(IMG_VOID *pvCpuVAddr, IMG_SIZE_T ui32Bytes, IMG_UINT32 ui32Flags, IMG_HANDLE hOSMemHandle)
+{
+	PVR_UNREFERENCED_PARAMETER(pvCpuVAddr);
+	PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+	PVR_UNREFERENCED_PARAMETER(hOSMemHandle);
+
+	return PVRSRV_ERROR_NOT_SUPPORTED;
+}
+#endif
+
+PVRSRV_ERROR OSRegisterMem(IMG_CPU_PHYADDR BasePAddr,
+							IMG_VOID *pvCpuVAddr,
+							IMG_SIZE_T ui32Bytes,
+							IMG_UINT32 ui32Flags,
+							IMG_HANDLE *phOSMemHandle);
+PVRSRV_ERROR OSUnRegisterMem(IMG_VOID *pvCpuVAddr,
+							IMG_SIZE_T ui32Bytes,
+							IMG_UINT32 ui32Flags,
+							IMG_HANDLE hOSMemHandle);
+
+
+
+#if defined(__linux__)
+PVRSRV_ERROR OSGetSubMemHandle(IMG_HANDLE hOSMemHandle,
+							   IMG_UINTPTR_T ui32ByteOffset,
+							   IMG_SIZE_T ui32Bytes,
+							   IMG_UINT32 ui32Flags,
+							   IMG_HANDLE *phOSMemHandleRet);
+PVRSRV_ERROR OSReleaseSubMemHandle(IMG_HANDLE hOSMemHandle, IMG_UINT32 ui32Flags);
+#else
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSGetSubMemHandle)
+#endif
+static INLINE PVRSRV_ERROR OSGetSubMemHandle(IMG_HANDLE hOSMemHandle,
+											 IMG_UINTPTR_T ui32ByteOffset,
+											 IMG_SIZE_T ui32Bytes,
+											 IMG_UINT32 ui32Flags,
+											 IMG_HANDLE *phOSMemHandleRet)
+{
+	PVR_UNREFERENCED_PARAMETER(ui32ByteOffset);
+	PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+
+	*phOSMemHandleRet = hOSMemHandle;
+	return PVRSRV_OK;
+}
+
+static INLINE PVRSRV_ERROR OSReleaseSubMemHandle(IMG_HANDLE hOSMemHandle, IMG_UINT32 ui32Flags)
+{
+	PVR_UNREFERENCED_PARAMETER(hOSMemHandle);
+	PVR_UNREFERENCED_PARAMETER(ui32Flags);
+	return PVRSRV_OK;
+}
+#endif
+
+IMG_UINT32 OSGetCurrentProcessIDKM(IMG_VOID);
+IMG_UINT32 OSGetCurrentThreadID( IMG_VOID );
+IMG_VOID OSMemSet(IMG_VOID *pvDest, IMG_UINT8 ui8Value, IMG_SIZE_T ui32Size);
+
+PVRSRV_ERROR OSAllocPages_Impl(IMG_UINT32 ui32Flags, IMG_SIZE_T ui32Size, IMG_UINT32 ui32PageSize, IMG_PVOID *ppvLinAddr, IMG_HANDLE *phPageAlloc);
+PVRSRV_ERROR OSFreePages(IMG_UINT32 ui32Flags, IMG_SIZE_T ui32Size, IMG_PVOID pvLinAddr, IMG_HANDLE hPageAlloc);
+
+
+#ifdef PVRSRV_LOG_MEMORY_ALLOCS
+	#define OSAllocMem(flags, size, linAddr, blockAlloc, logStr) \
+		(PVR_TRACE(("OSAllocMem(" #flags ", " #size ", " #linAddr ", " #blockAlloc "): " logStr " (size = 0x%lx)", size)), \
+			OSAllocMem_Debug_Wrapper(flags, size, linAddr, blockAlloc, __FILE__, __LINE__))
+
+	#define OSAllocPages(flags, size, pageSize, linAddr, pageAlloc) \
+		(PVR_TRACE(("OSAllocPages(" #flags ", " #size ", " #pageSize ", " #linAddr ", " #pageAlloc "): (size = 0x%lx)", size)), \
+			OSAllocPages_Impl(flags, size, pageSize, linAddr, pageAlloc))
+
+	#define OSFreeMem(flags, size, linAddr, blockAlloc) \
+		(PVR_TRACE(("OSFreeMem(" #flags ", " #size ", " #linAddr ", " #blockAlloc "): (pointer = 0x%X)", linAddr)), \
+			OSFreeMem_Debug_Wrapper(flags, size, linAddr, blockAlloc, __FILE__, __LINE__))
+#else
+	#define OSAllocMem(flags, size, linAddr, blockAlloc, logString) \
+		OSAllocMem_Debug_Wrapper(flags, size, linAddr, blockAlloc, __FILE__, __LINE__)
+
+	#define OSAllocPages OSAllocPages_Impl
+
+	#define OSFreeMem(flags, size, linAddr, blockAlloc) \
+			OSFreeMem_Debug_Wrapper(flags, size, linAddr, blockAlloc, __FILE__, __LINE__)
+#endif
+
+#ifdef PVRSRV_DEBUG_OS_MEMORY
+
+	PVRSRV_ERROR OSAllocMem_Debug_Wrapper(IMG_UINT32 ui32Flags,
+										IMG_UINT32 ui32Size,
+										IMG_PVOID *ppvCpuVAddr,
+										IMG_HANDLE *phBlockAlloc,
+										IMG_CHAR *pszFilename,
+										IMG_UINT32 ui32Line);
+
+	PVRSRV_ERROR OSFreeMem_Debug_Wrapper(IMG_UINT32 ui32Flags,
+									 IMG_UINT32 ui32Size,
+									 IMG_PVOID pvCpuVAddr,
+									 IMG_HANDLE hBlockAlloc,
+									 IMG_CHAR *pszFilename,
+									 IMG_UINT32 ui32Line);
+
+
+	typedef struct
+	{
+		IMG_UINT8 sGuardRegionBefore[8];
+		IMG_CHAR sFileName[128];
+		IMG_UINT32 uLineNo;
+		IMG_SIZE_T uSize;
+		IMG_SIZE_T uSizeParityCheck;
+		enum valid_tag
+		{	isFree = 0x277260FF,
+			isAllocated = 0x260511AA
+		} eValid;
+	} OSMEM_DEBUG_INFO;
+
+	#define TEST_BUFFER_PADDING_STATUS (sizeof(OSMEM_DEBUG_INFO))
+	#define TEST_BUFFER_PADDING_AFTER  (8)
+	#define TEST_BUFFER_PADDING (TEST_BUFFER_PADDING_STATUS + TEST_BUFFER_PADDING_AFTER)
+#else
+	#define OSAllocMem_Debug_Wrapper OSAllocMem_Debug_Linux_Memory_Allocations
+	#define OSFreeMem_Debug_Wrapper OSFreeMem_Debug_Linux_Memory_Allocations
+#endif
+
+#if defined(__linux__) && defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
+	PVRSRV_ERROR OSAllocMem_Impl(IMG_UINT32 ui32Flags, IMG_SIZE_T ui32Size, IMG_PVOID *ppvLinAddr, IMG_HANDLE *phBlockAlloc, IMG_CHAR *pszFilename, IMG_UINT32 ui32Line);
+	PVRSRV_ERROR OSFreeMem_Impl(IMG_UINT32 ui32Flags, IMG_SIZE_T ui32Size, IMG_PVOID pvLinAddr, IMG_HANDLE hBlockAlloc, IMG_CHAR *pszFilename, IMG_UINT32 ui32Line);
+
+	#define OSAllocMem_Debug_Linux_Memory_Allocations OSAllocMem_Impl
+	#define OSFreeMem_Debug_Linux_Memory_Allocations OSFreeMem_Impl
+#else
+	PVRSRV_ERROR OSAllocMem_Impl(IMG_UINT32 ui32Flags, IMG_SIZE_T ui32Size, IMG_PVOID *ppvLinAddr, IMG_HANDLE *phBlockAlloc);
+	PVRSRV_ERROR OSFreeMem_Impl(IMG_UINT32 ui32Flags, IMG_SIZE_T ui32Size, IMG_PVOID pvLinAddr, IMG_HANDLE hBlockAlloc);
+
+	#define OSAllocMem_Debug_Linux_Memory_Allocations(flags, size, addr, blockAlloc, file, line) \
+		OSAllocMem_Impl(flags, size, addr, blockAlloc)
+	#define OSFreeMem_Debug_Linux_Memory_Allocations(flags, size, addr, blockAlloc, file, line) \
+		OSFreeMem_Impl(flags, size, addr, blockAlloc)
+#endif
+
+
+
+#if defined(__linux__)
+IMG_CPU_PHYADDR OSMemHandleToCpuPAddr(IMG_VOID *hOSMemHandle, IMG_SIZE_T ui32ByteOffset);
+#else
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSMemHandleToCpuPAddr)
+#endif
+static INLINE IMG_CPU_PHYADDR OSMemHandleToCpuPAddr(IMG_HANDLE hOSMemHandle, IMG_SIZE_T ui32ByteOffset)
+{
+	IMG_CPU_PHYADDR sCpuPAddr;
+	PVR_UNREFERENCED_PARAMETER(hOSMemHandle);
+	PVR_UNREFERENCED_PARAMETER(ui32ByteOffset);
+	sCpuPAddr.uiAddr = 0;
+	return sCpuPAddr;
+}
+#endif
+PVRSRV_ERROR OSInitEnvData(IMG_PVOID *ppvEnvSpecificData);
+PVRSRV_ERROR OSDeInitEnvData(IMG_PVOID pvEnvSpecificData);
+IMG_CHAR* OSStringCopy(IMG_CHAR *pszDest, const IMG_CHAR *pszSrc);
+IMG_INT32 OSSNPrintf(IMG_CHAR *pStr, IMG_SIZE_T ui32Size, const IMG_CHAR *pszFormat, ...);
+#define OSStringLength(pszString) strlen(pszString)
+
+PVRSRV_ERROR OSEventObjectCreate(const IMG_CHAR *pszName,
+								 PVRSRV_EVENTOBJECT *psEventObject);
+PVRSRV_ERROR OSEventObjectDestroy(PVRSRV_EVENTOBJECT *psEventObject);
+PVRSRV_ERROR OSEventObjectSignal(IMG_HANDLE hOSEventKM);
+PVRSRV_ERROR OSEventObjectWait(IMG_HANDLE hOSEventKM);
+PVRSRV_ERROR OSEventObjectOpen(PVRSRV_EVENTOBJECT *psEventObject,
+											IMG_HANDLE *phOSEvent);
+PVRSRV_ERROR OSEventObjectClose(PVRSRV_EVENTOBJECT *psEventObject,
+											IMG_HANDLE hOSEventKM);
+
+
+PVRSRV_ERROR OSBaseAllocContigMemory(IMG_SIZE_T ui32Size, IMG_CPU_VIRTADDR *pLinAddr, IMG_CPU_PHYADDR *pPhysAddr);
+PVRSRV_ERROR OSBaseFreeContigMemory(IMG_SIZE_T ui32Size, IMG_CPU_VIRTADDR LinAddr, IMG_CPU_PHYADDR PhysAddr);
+
+IMG_PVOID MapUserFromKernel(IMG_PVOID pvLinAddrKM,IMG_SIZE_T ui32Size,IMG_HANDLE *phMemBlock);
+IMG_PVOID OSMapHWRegsIntoUserSpace(IMG_HANDLE hDevCookie, IMG_SYS_PHYADDR sRegAddr, IMG_UINT32 ulSize, IMG_PVOID *ppvProcess);
+IMG_VOID  OSUnmapHWRegsFromUserSpace(IMG_HANDLE hDevCookie, IMG_PVOID pvUserAddr, IMG_PVOID pvProcess);
+
+IMG_VOID  UnmapUserFromKernel(IMG_PVOID pvLinAddrUM, IMG_SIZE_T ui32Size, IMG_HANDLE hMemBlock);
+
+PVRSRV_ERROR OSMapPhysToUserSpace(IMG_HANDLE hDevCookie,
+								  IMG_SYS_PHYADDR sCPUPhysAddr,
+								  IMG_SIZE_T uiSizeInBytes,
+								  IMG_UINT32 ui32CacheFlags,
+								  IMG_PVOID *ppvUserAddr,
+								  IMG_SIZE_T *puiActualSize,
+								  IMG_HANDLE hMappingHandle);
+
+PVRSRV_ERROR OSUnmapPhysToUserSpace(IMG_HANDLE hDevCookie,
+									IMG_PVOID pvUserAddr,
+									IMG_PVOID pvProcess);
+
+PVRSRV_ERROR OSLockResource(PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID);
+PVRSRV_ERROR OSUnlockResource(PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID);
+IMG_BOOL OSIsResourceLocked(PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID);
+PVRSRV_ERROR OSCreateResource(PVRSRV_RESOURCE *psResource);
+PVRSRV_ERROR OSDestroyResource(PVRSRV_RESOURCE *psResource);
+IMG_VOID OSBreakResourceLock(PVRSRV_RESOURCE *psResource, IMG_UINT32 ui32ID);
+IMG_VOID OSWaitus(IMG_UINT32 ui32Timeus);
+IMG_VOID OSReleaseThreadQuanta(IMG_VOID);
+IMG_UINT32 OSPCIReadDword(IMG_UINT32 ui32Bus, IMG_UINT32 ui32Dev, IMG_UINT32 ui32Func, IMG_UINT32 ui32Reg);
+IMG_VOID OSPCIWriteDword(IMG_UINT32 ui32Bus, IMG_UINT32 ui32Dev, IMG_UINT32 ui32Func, IMG_UINT32 ui32Reg, IMG_UINT32 ui32Value);
+
+#ifndef OSReadHWReg
+IMG_UINT32 OSReadHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset);
+#endif
+#ifndef OSWriteHWReg
+IMG_VOID OSWriteHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset, IMG_UINT32 ui32Value);
+#endif
+
+typedef IMG_VOID (*PFN_TIMER_FUNC)(IMG_VOID*);
+IMG_HANDLE OSAddTimer(PFN_TIMER_FUNC pfnTimerFunc, IMG_VOID *pvData, IMG_UINT32 ui32MsTimeout);
+PVRSRV_ERROR OSRemoveTimer (IMG_HANDLE hTimer);
+PVRSRV_ERROR OSEnableTimer (IMG_HANDLE hTimer);
+PVRSRV_ERROR OSDisableTimer (IMG_HANDLE hTimer);
+
+PVRSRV_ERROR OSGetSysMemSize(IMG_SIZE_T *pui32Bytes);
+
+typedef enum _HOST_PCI_INIT_FLAGS_
+{
+	HOST_PCI_INIT_FLAG_BUS_MASTER	= 0x00000001,
+	HOST_PCI_INIT_FLAG_MSI		= 0x00000002,
+	HOST_PCI_INIT_FLAG_FORCE_I32 	= 0x7fffffff
+} HOST_PCI_INIT_FLAGS;
+
+struct _PVRSRV_PCI_DEV_OPAQUE_STRUCT_;
+typedef struct _PVRSRV_PCI_DEV_OPAQUE_STRUCT_ *PVRSRV_PCI_DEV_HANDLE;
+
+PVRSRV_PCI_DEV_HANDLE OSPCIAcquireDev(IMG_UINT16 ui16VendorID, IMG_UINT16 ui16DeviceID, HOST_PCI_INIT_FLAGS eFlags);
+PVRSRV_PCI_DEV_HANDLE OSPCISetDev(IMG_VOID *pvPCICookie, HOST_PCI_INIT_FLAGS eFlags);
+PVRSRV_ERROR OSPCIReleaseDev(PVRSRV_PCI_DEV_HANDLE hPVRPCI);
+PVRSRV_ERROR OSPCIIRQ(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 *pui32IRQ);
+IMG_UINT32 OSPCIAddrRangeLen(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index);
+IMG_UINT32 OSPCIAddrRangeStart(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index);
+IMG_UINT32 OSPCIAddrRangeEnd(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index);
+PVRSRV_ERROR OSPCIRequestAddrRange(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index);
+PVRSRV_ERROR OSPCIReleaseAddrRange(PVRSRV_PCI_DEV_HANDLE hPVRPCI, IMG_UINT32 ui32Index);
+PVRSRV_ERROR OSPCISuspendDev(PVRSRV_PCI_DEV_HANDLE hPVRPCI);
+PVRSRV_ERROR OSPCIResumeDev(PVRSRV_PCI_DEV_HANDLE hPVRPCI);
+
+PVRSRV_ERROR OSScheduleMISR(IMG_VOID *pvSysData);
+
+IMG_VOID OSPanic(IMG_VOID);
+
+IMG_BOOL OSProcHasPrivSrvInit(IMG_VOID);
+
+typedef enum _img_verify_test
+{
+	PVR_VERIFY_WRITE = 0,
+	PVR_VERIFY_READ
+} IMG_VERIFY_TEST;
+
+IMG_BOOL OSAccessOK(IMG_VERIFY_TEST eVerification, IMG_VOID *pvUserPtr, IMG_SIZE_T ui32Bytes);
+
+PVRSRV_ERROR OSCopyToUser(IMG_PVOID pvProcess, IMG_VOID *pvDest, IMG_VOID *pvSrc, IMG_SIZE_T ui32Bytes);
+PVRSRV_ERROR OSCopyFromUser(IMG_PVOID pvProcess, IMG_VOID *pvDest, IMG_VOID *pvSrc, IMG_SIZE_T ui32Bytes);
+
+#if defined(__linux__)
+PVRSRV_ERROR OSAcquirePhysPageAddr(IMG_VOID* pvCPUVAddr,
+									IMG_SIZE_T ui32Bytes,
+									IMG_SYS_PHYADDR *psSysPAddr,
+									IMG_HANDLE *phOSWrapMem,
+									IMG_BOOL bWrapWorkaround);
+PVRSRV_ERROR OSReleasePhysPageAddr(IMG_HANDLE hOSWrapMem);
+#else
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSAcquirePhysPageAddr)
+#endif
+static INLINE PVRSRV_ERROR OSAcquirePhysPageAddr(IMG_VOID* pvCPUVAddr,
+												IMG_SIZE_T ui32Bytes,
+												IMG_SYS_PHYADDR *psSysPAddr,
+												IMG_HANDLE *phOSWrapMem,
+												IMG_BOOL bWrapWorkaround)
+{
+	PVR_UNREFERENCED_PARAMETER(pvCPUVAddr);
+	PVR_UNREFERENCED_PARAMETER(ui32Bytes);
+	PVR_UNREFERENCED_PARAMETER(psSysPAddr);
+	PVR_UNREFERENCED_PARAMETER(phOSWrapMem);
+	PVR_UNREFERENCED_PARAMETER(bWrapWorkaround);
+	return PVRSRV_OK;
+}
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSReleasePhysPageAddr)
+#endif
+static INLINE PVRSRV_ERROR OSReleasePhysPageAddr(IMG_HANDLE hOSWrapMem)
+{
+	PVR_UNREFERENCED_PARAMETER(hOSWrapMem);
+	return PVRSRV_OK;
+}
+#endif
+
+#if defined(__linux__) && defined(__KERNEL__)
+#define	OS_SUPPORTS_IN_LISR
+static inline IMG_BOOL OSInLISR(IMG_VOID unref__ *pvSysData)
+{
+	return in_irq();
+}
+#endif
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osperproc.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osperproc.h
new file mode 100644
index 0000000..a23b983
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/osperproc.h
@@ -0,0 +1,72 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __OSPERPROC_H__
+#define __OSPERPROC_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#if defined(__linux__)
+PVRSRV_ERROR OSPerProcessPrivateDataInit(IMG_HANDLE *phOsPrivateData);
+PVRSRV_ERROR OSPerProcessPrivateDataDeInit(IMG_HANDLE hOsPrivateData);
+
+PVRSRV_ERROR OSPerProcessSetHandleOptions(PVRSRV_HANDLE_BASE *psHandleBase);
+#else
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSPerProcessPrivateDataInit)
+#endif
+static INLINE PVRSRV_ERROR OSPerProcessPrivateDataInit(IMG_HANDLE *phOsPrivateData)
+{
+	PVR_UNREFERENCED_PARAMETER(phOsPrivateData);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSPerProcessPrivateDataDeInit)
+#endif
+static INLINE PVRSRV_ERROR OSPerProcessPrivateDataDeInit(IMG_HANDLE hOsPrivateData)
+{
+	PVR_UNREFERENCED_PARAMETER(hOsPrivateData);
+
+	return PVRSRV_OK;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(OSPerProcessSetHandleOptions)
+#endif
+static INLINE PVRSRV_ERROR OSPerProcessSetHandleOptions(PVRSRV_HANDLE_BASE *psHandleBase)
+{
+	PVR_UNREFERENCED_PARAMETER(psHandleBase);
+
+	return PVRSRV_OK;
+}
+#endif
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_km.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_km.h
new file mode 100644
index 0000000..31f7aa2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_km.h
@@ -0,0 +1,448 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _PDUMP_KM_H_
+#define _PDUMP_KM_H_
+
+#if defined(LINUX)
+#define COMMON_PDUMP_OS_SUPPORT
+#endif
+
+#if defined(COMMON_PDUMP_OS_SUPPORT)
+#if defined(SUPPORT_SGX) || defined(SUPPORT_MSVDX)
+#define SGX_SUPPORT_COMMON_PDUMP
+#if defined(SGX_SUPPORT_COMMON_PDUMP)
+#include <pdump_osfunc.h>
+#endif
+
+#endif
+#endif
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define PDUMP_FLAGS_NEVER			0x08000000UL
+#define PDUMP_FLAGS_TOOUT2MEM		0x10000000UL
+#define PDUMP_FLAGS_LASTFRAME		0x20000000UL
+#define PDUMP_FLAGS_RESETLFBUFFER	0x40000000UL
+#define PDUMP_FLAGS_CONTINUOUS		0x80000000UL
+
+#define PDUMP_PD_UNIQUETAG			(IMG_HANDLE)0
+#define PDUMP_PT_UNIQUETAG			(IMG_HANDLE)0
+
+#define PDUMP_STREAM_PARAM2			0
+#define PDUMP_STREAM_SCRIPT2		1
+#define PDUMP_STREAM_DRIVERINFO		2
+#define PDUMP_NUM_STREAMS			3
+
+
+#ifndef PDUMP
+#define MAKEUNIQUETAG(hMemInfo)	(0)
+#endif
+
+#ifdef PDUMP
+
+#define MAKEUNIQUETAG(hMemInfo)	(((BM_BUF *)(((PVRSRV_KERNEL_MEM_INFO *)hMemInfo)->sMemBlk.hBuffer))->pMapping)
+
+	IMG_IMPORT PVRSRV_ERROR PDumpMemPolKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+										  IMG_UINT32			ui32Offset,
+										  IMG_UINT32			ui32Value,
+										  IMG_UINT32			ui32Mask,
+										  PDUMP_POLL_OPERATOR	eOperator,
+										  IMG_UINT32			ui32Flags,
+										  IMG_HANDLE			hUniqueTag);
+
+	IMG_IMPORT PVRSRV_ERROR PDumpMemUM(PVRSRV_PER_PROCESS_DATA *psProcData,
+									   IMG_PVOID			pvAltLinAddr,
+									   IMG_PVOID			pvLinAddr,
+									   PVRSRV_KERNEL_MEM_INFO	*psMemInfo,
+									   IMG_UINT32			ui32Offset,
+									   IMG_UINT32			ui32Bytes,
+									   IMG_UINT32			ui32Flags,
+									   IMG_HANDLE			hUniqueTag);
+
+	IMG_IMPORT PVRSRV_ERROR PDumpMemKM(IMG_PVOID			pvAltLinAddr,
+									   PVRSRV_KERNEL_MEM_INFO	*psMemInfo,
+									   IMG_UINT32			ui32Offset,
+									   IMG_UINT32			ui32Bytes,
+									   IMG_UINT32			ui32Flags,
+									   IMG_HANDLE			hUniqueTag);
+	PVRSRV_ERROR PDumpMemPagesKM(PVRSRV_DEVICE_TYPE	eDeviceType,
+								 IMG_DEV_PHYADDR		*pPages,
+								 IMG_UINT32			ui32NumPages,
+								 IMG_DEV_VIRTADDR	sDevAddr,
+								 IMG_UINT32			ui32Start,
+								 IMG_UINT32			ui32Length,
+								 IMG_UINT32			ui32Flags,
+								 IMG_HANDLE			hUniqueTag);
+
+	PVRSRV_ERROR PDumpMem2KM(PVRSRV_DEVICE_TYPE	eDeviceType,
+							 IMG_CPU_VIRTADDR	pvLinAddr,
+							 IMG_UINT32			ui32Bytes,
+							 IMG_UINT32			ui32Flags,
+							 IMG_BOOL			bInitialisePages,
+							 IMG_HANDLE			hUniqueTag1,
+							 IMG_HANDLE			hUniqueTag2);
+	IMG_VOID PDumpInitCommon(IMG_VOID);
+	IMG_VOID PDumpDeInitCommon(IMG_VOID);
+	IMG_VOID PDumpInit(IMG_VOID);
+	IMG_VOID PDumpDeInit(IMG_VOID);
+	PVRSRV_ERROR PDumpStartInitPhaseKM(IMG_VOID);
+	PVRSRV_ERROR PDumpStopInitPhaseKM(IMG_VOID);
+	IMG_IMPORT PVRSRV_ERROR PDumpSetFrameKM(IMG_UINT32 ui32Frame);
+	IMG_IMPORT PVRSRV_ERROR PDumpCommentKM(IMG_CHAR *pszComment, IMG_UINT32 ui32Flags);
+	IMG_IMPORT PVRSRV_ERROR PDumpDriverInfoKM(IMG_CHAR *pszString, IMG_UINT32 ui32Flags);
+
+	PVRSRV_ERROR PDumpRegWithFlagsKM(IMG_UINT32 ui32RegAddr,
+									 IMG_UINT32 ui32RegValue,
+									 IMG_UINT32 ui32Flags);
+	PVRSRV_ERROR PDumpRegPolWithFlagsKM(IMG_UINT32 ui32RegAddr,
+										IMG_UINT32 ui32RegValue,
+										IMG_UINT32 ui32Mask,
+										IMG_UINT32 ui32Flags);
+	PVRSRV_ERROR PDumpRegPolKM(IMG_UINT32 ui32RegAddr,
+							   IMG_UINT32 ui32RegValue,
+							   IMG_UINT32 ui32Mask);
+
+	IMG_IMPORT PVRSRV_ERROR PDumpBitmapKM(IMG_CHAR *pszFileName,
+										  IMG_UINT32 ui32FileOffset,
+										  IMG_UINT32 ui32Width,
+										  IMG_UINT32 ui32Height,
+										  IMG_UINT32 ui32StrideInBytes,
+										  IMG_DEV_VIRTADDR sDevBaseAddr,
+										  IMG_UINT32 ui32Size,
+										  PDUMP_PIXEL_FORMAT ePixelFormat,
+										  PDUMP_MEM_FORMAT eMemFormat,
+										  IMG_UINT32 ui32PDumpFlags);
+	IMG_IMPORT PVRSRV_ERROR PDumpReadRegKM(IMG_CHAR *pszFileName,
+										   IMG_UINT32 ui32FileOffset,
+										   IMG_UINT32 ui32Address,
+										   IMG_UINT32 ui32Size,
+										   IMG_UINT32 ui32PDumpFlags);
+
+	IMG_BOOL PDumpIsSuspended(IMG_VOID);
+
+#if defined(SGX_SUPPORT_COMMON_PDUMP) || !defined(SUPPORT_VGX)
+
+	PVRSRV_ERROR PDumpRegKM(IMG_UINT32		dwReg,
+							IMG_UINT32		dwData);
+	PVRSRV_ERROR PDumpComment(IMG_CHAR* pszFormat, ...);
+	PVRSRV_ERROR PDumpCommentWithFlags(IMG_UINT32	ui32Flags,
+									   IMG_CHAR*	pszFormat,
+									   ...);
+
+	PVRSRV_ERROR PDumpPDReg(IMG_UINT32	ui32Reg,
+							IMG_UINT32	ui32dwData,
+							IMG_HANDLE	hUniqueTag);
+	PVRSRV_ERROR PDumpPDRegWithFlags(IMG_UINT32		ui32Reg,
+									 IMG_UINT32		ui32Data,
+									 IMG_UINT32		ui32Flags,
+									 IMG_HANDLE		hUniqueTag);
+#else
+	IMG_VOID PDumpRegKM(IMG_UINT32		dwReg,
+							IMG_UINT32		dwData);
+	IMG_VOID PDumpComment(IMG_CHAR* pszFormat, ...);
+	IMG_VOID PDumpCommentWithFlags(IMG_UINT32	ui32Flags,
+									   IMG_CHAR*	pszFormat,
+									   ...);
+
+
+	IMG_VOID PDumpPDReg(IMG_UINT32	ui32Reg,
+							IMG_UINT32	ui32dwData,
+							IMG_HANDLE	hUniqueTag);
+	IMG_VOID PDumpPDRegWithFlags(IMG_UINT32		ui32Reg,
+									 IMG_UINT32		ui32Data,
+									 IMG_UINT32		ui32Flags,
+									 IMG_HANDLE		hUniqueTag);
+#endif
+
+	IMG_VOID PDumpMsvdxRegRead(const IMG_CHAR* const	pRegRegion,
+							   const IMG_UINT32		dwRegOffset);
+
+	IMG_VOID PDumpMsvdxRegWrite(const IMG_CHAR* const	pRegRegion,
+								const IMG_UINT32		dwRegOffset,
+								const IMG_UINT32		dwData);
+
+	PVRSRV_ERROR PDumpMsvdxRegPol(const IMG_CHAR* const	pRegRegion,
+								  const IMG_UINT32		ui32Offset,
+								  const IMG_UINT32		ui32CheckFuncIdExt,
+								  const IMG_UINT32		ui32RequValue,
+								  const IMG_UINT32		ui32Enable,
+								  const IMG_UINT32		ui32PollCount,
+								  const IMG_UINT32		ui32TimeOut);
+
+	PVRSRV_ERROR  PDumpMsvdxWriteRef(const IMG_CHAR* const	pRegRegion,
+									 const IMG_UINT32		ui32VLROffset,
+									 const IMG_UINT32		ui32Physical );
+
+	IMG_BOOL PDumpIsLastCaptureFrameKM(IMG_VOID);
+	IMG_IMPORT IMG_BOOL PDumpIsCaptureFrameKM(IMG_VOID);
+
+	IMG_VOID PDumpMallocPagesPhys(PVRSRV_DEVICE_TYPE	eDeviceType,
+								  IMG_UINT32			ui32DevVAddr,
+								  IMG_PUINT32			pui32PhysPages,
+								  IMG_UINT32			ui32NumPages,
+								  IMG_HANDLE			hUniqueTag);
+	PVRSRV_ERROR PDumpSetMMUContext(PVRSRV_DEVICE_TYPE eDeviceType,
+									IMG_CHAR *pszMemSpace,
+									IMG_UINT32 *pui32MMUContextID,
+									IMG_UINT32 ui32MMUType,
+									IMG_HANDLE hUniqueTag1,
+									IMG_VOID *pvPDCPUAddr);
+	PVRSRV_ERROR PDumpClearMMUContext(PVRSRV_DEVICE_TYPE eDeviceType,
+									IMG_CHAR *pszMemSpace,
+									IMG_UINT32 ui32MMUContextID,
+									IMG_UINT32 ui32MMUType);
+
+	PVRSRV_ERROR PDumpPDDevPAddrKM(PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+								   IMG_UINT32 ui32Offset,
+								   IMG_DEV_PHYADDR sPDDevPAddr,
+								   IMG_HANDLE hUniqueTag1,
+								   IMG_HANDLE hUniqueTag2);
+
+	IMG_BOOL PDumpTestNextFrame(IMG_UINT32 ui32CurrentFrame);
+
+
+#if defined (COMMON_PDUMP_OS_SUPPORT) && !defined(SUPPORT_VGX)
+
+	PVRSRV_ERROR PDumpTASignatureRegisters(IMG_UINT32	ui32DumpFrameNum,
+								   IMG_UINT32	ui32TAKickCount,
+								   IMG_BOOL		bLastFrame,
+								   IMG_UINT32 *pui32Registers,
+								   IMG_UINT32 ui32NumRegisters);
+
+	PVRSRV_ERROR PDump3DSignatureRegisters(IMG_UINT32 ui32DumpFrameNum,
+															IMG_BOOL bLastFrame,
+															IMG_UINT32 *pui32Registers,
+															IMG_UINT32 ui32NumRegisters);
+
+	PVRSRV_ERROR PDumpCounterRegisters(IMG_UINT32 ui32DumpFrameNum,
+					IMG_BOOL		bLastFrame,
+					IMG_UINT32 *pui32Registers,
+					IMG_UINT32 ui32NumRegisters);
+
+	PVRSRV_ERROR PDumpRegRead(const IMG_UINT32 dwRegOffset, IMG_UINT32	ui32Flags);
+
+	PVRSRV_ERROR PDumpCycleCountRegRead(const IMG_UINT32 dwRegOffset, IMG_BOOL bLastFrame);
+
+	PVRSRV_ERROR PDumpIDLWithFlags(IMG_UINT32 ui32Clocks, IMG_UINT32 ui32Flags);
+	PVRSRV_ERROR PDumpIDL(IMG_UINT32 ui32Clocks);
+
+	PVRSRV_ERROR PDumpMallocPages(PVRSRV_DEVICE_TYPE	eDeviceType,
+							  IMG_UINT32			ui32DevVAddr,
+							  IMG_CPU_VIRTADDR		pvLinAddr,
+							  IMG_HANDLE			hOSMemHandle,
+							  IMG_UINT32			ui32NumBytes,
+							  IMG_UINT32			ui32PageSize,
+							  IMG_HANDLE			hUniqueTag);
+	PVRSRV_ERROR PDumpMallocPageTable(PVRSRV_DEVICE_TYPE	eDeviceType,
+								  IMG_CPU_VIRTADDR		pvLinAddr,
+								  IMG_UINT32			ui32NumBytes,
+								  IMG_HANDLE			hUniqueTag);
+	PVRSRV_ERROR PDumpFreePages(struct _BM_HEAP_	*psBMHeap,
+							IMG_DEV_VIRTADDR	sDevVAddr,
+							IMG_UINT32			ui32NumBytes,
+							IMG_UINT32			ui32PageSize,
+							IMG_HANDLE      	hUniqueTag,
+							IMG_BOOL			bInterleaved);
+	PVRSRV_ERROR PDumpFreePageTable(PVRSRV_DEVICE_TYPE	eDeviceType,
+								IMG_CPU_VIRTADDR	pvLinAddr,
+								IMG_UINT32			ui32NumBytes,
+								IMG_HANDLE			hUniqueTag);
+
+	IMG_IMPORT PVRSRV_ERROR PDumpHWPerfCBKM(IMG_CHAR			*pszFileName,
+										IMG_UINT32			ui32FileOffset,
+										IMG_DEV_VIRTADDR	sDevBaseAddr,
+										IMG_UINT32 			ui32Size,
+										IMG_UINT32 			ui32PDumpFlags);
+
+	PVRSRV_ERROR PDumpCBP(PPVRSRV_KERNEL_MEM_INFO	psROffMemInfo,
+				  IMG_UINT32				ui32ROffOffset,
+				  IMG_UINT32				ui32WPosVal,
+				  IMG_UINT32				ui32PacketSize,
+				  IMG_UINT32				ui32BufferSize,
+				  IMG_UINT32				ui32Flags,
+				  IMG_HANDLE				hUniqueTag);
+
+#else
+	IMG_VOID PDumpTASignatureRegisters(IMG_UINT32	ui32DumpFrameNum,
+			   IMG_UINT32	ui32TAKickCount,
+			   IMG_BOOL		bLastFrame,
+			   IMG_UINT32 *pui32Registers,
+			   IMG_UINT32 ui32NumRegisters);
+	IMG_VOID PDump3DSignatureRegisters(IMG_UINT32 ui32DumpFrameNum,
+			IMG_BOOL bLastFrame,
+			IMG_UINT32 *pui32Registers,
+			IMG_UINT32 ui32NumRegisters);
+	IMG_VOID PDumpCounterRegisters(IMG_UINT32 ui32DumpFrameNum,
+			IMG_BOOL		bLastFrame,
+			IMG_UINT32 *pui32Registers,
+			IMG_UINT32 ui32NumRegisters);
+
+	IMG_VOID PDumpRegRead(const IMG_UINT32 dwRegOffset, IMG_UINT32	ui32Flags);
+	IMG_VOID PDumpCycleCountRegRead(const IMG_UINT32 dwRegOffset, IMG_BOOL bLastFrame);
+
+	IMG_VOID PDumpIDLWithFlags(IMG_UINT32 ui32Clocks, IMG_UINT32 ui32Flags);
+	IMG_VOID PDumpIDL(IMG_UINT32 ui32Clocks);
+
+
+	IMG_VOID PDumpMallocPages(PVRSRV_DEVICE_TYPE	eDeviceType,
+							  IMG_UINT32			ui32DevVAddr,
+							  IMG_CPU_VIRTADDR		pvLinAddr,
+							  IMG_HANDLE			hOSMemHandle,
+							  IMG_UINT32			ui32NumBytes,
+							  IMG_UINT32			ui32PageSize,
+							  IMG_HANDLE			hUniqueTag);
+	IMG_VOID PDumpMallocPageTable(PVRSRV_DEVICE_TYPE	eDeviceType,
+								  IMG_CPU_VIRTADDR		pvLinAddr,
+								  IMG_UINT32			ui32NumBytes,
+								  IMG_HANDLE			hUniqueTag);
+	IMG_VOID PDumpFreePages(struct _BM_HEAP_	*psBMHeap,
+							IMG_DEV_VIRTADDR	sDevVAddr,
+							IMG_UINT32			ui32NumBytes,
+							IMG_UINT32			ui32PageSize,
+							IMG_HANDLE      	hUniqueTag,
+							IMG_BOOL			bInterleaved);
+	IMG_VOID PDumpFreePageTable(PVRSRV_DEVICE_TYPE	eDeviceType,
+								IMG_CPU_VIRTADDR	pvLinAddr,
+								IMG_UINT32			ui32NumBytes,
+								IMG_HANDLE			hUniqueTag);
+
+	IMG_IMPORT IMG_VOID PDumpHWPerfCBKM(IMG_CHAR			*pszFileName,
+										IMG_UINT32			ui32FileOffset,
+										IMG_DEV_VIRTADDR	sDevBaseAddr,
+										IMG_UINT32 			ui32Size,
+										IMG_UINT32 			ui32PDumpFlags);
+
+	IMG_VOID PDumpCBP(PPVRSRV_KERNEL_MEM_INFO	psROffMemInfo,
+				  IMG_UINT32				ui32ROffOffset,
+				  IMG_UINT32				ui32WPosVal,
+				  IMG_UINT32				ui32PacketSize,
+				  IMG_UINT32				ui32BufferSize,
+				  IMG_UINT32				ui32Flags,
+				  IMG_HANDLE				hUniqueTag);
+
+#endif
+
+	IMG_VOID PDumpVGXMemToFile(IMG_CHAR *pszFileName,
+							   IMG_UINT32 ui32FileOffset,
+							   PVRSRV_KERNEL_MEM_INFO *psMemInfo,
+							   IMG_UINT32 uiAddr,
+							   IMG_UINT32 ui32Size,
+							   IMG_UINT32 ui32PDumpFlags,
+							   IMG_HANDLE hUniqueTag);
+
+	IMG_VOID PDumpSuspendKM(IMG_VOID);
+	IMG_VOID PDumpResumeKM(IMG_VOID);
+
+	#define PDUMPMEMPOL				PDumpMemPolKM
+	#define PDUMPMEM				PDumpMemKM
+	#define PDUMPMEM2				PDumpMem2KM
+	#define PDUMPMEMUM				PDumpMemUM
+	#define PDUMPINIT				PDumpInitCommon
+	#define PDUMPDEINIT				PDumpDeInitCommon
+	#define PDUMPISLASTFRAME		PDumpIsLastCaptureFrameKM
+	#define PDUMPTESTFRAME			PDumpIsCaptureFrameKM
+	#define PDUMPTESTNEXTFRAME		PDumpTestNextFrame
+	#define PDUMPREGWITHFLAGS		PDumpRegWithFlagsKM
+	#define PDUMPREG				PDumpRegKM
+	#define PDUMPCOMMENT			PDumpComment
+	#define PDUMPCOMMENTWITHFLAGS	PDumpCommentWithFlags
+	#define PDUMPREGPOL				PDumpRegPolKM
+	#define PDUMPREGPOLWITHFLAGS	PDumpRegPolWithFlagsKM
+	#define PDUMPMALLOCPAGES		PDumpMallocPages
+	#define PDUMPMALLOCPAGETABLE	PDumpMallocPageTable
+	#define PDUMPSETMMUCONTEXT		PDumpSetMMUContext
+	#define PDUMPCLEARMMUCONTEXT	PDumpClearMMUContext
+	#define PDUMPFREEPAGES			PDumpFreePages
+	#define PDUMPFREEPAGETABLE		PDumpFreePageTable
+	#define PDUMPPDREG				PDumpPDReg
+	#define PDUMPPDREGWITHFLAGS		PDumpPDRegWithFlags
+	#define PDUMPCBP				PDumpCBP
+	#define PDUMPMALLOCPAGESPHYS	PDumpMallocPagesPhys
+	#define PDUMPENDINITPHASE		PDumpStopInitPhaseKM
+	#define PDUMPMSVDXREGWRITE		PDumpMsvdxRegWrite
+	#define PDUMPMSVDXREGREAD		PDumpMsvdxRegRead
+	#define PDUMPMSVDXPOL			PDumpMsvdxRegPol
+	#define PDUMPMSVDXWRITEREF		PDumpMsvdxWriteRef
+	#define PDUMPBITMAPKM			PDumpBitmapKM
+	#define PDUMPDRIVERINFO			PDumpDriverInfoKM
+	#define PDUMPIDLWITHFLAGS		PDumpIDLWithFlags
+	#define PDUMPIDL				PDumpIDL
+	#define PDUMPSUSPEND			PDumpSuspendKM
+	#define PDUMPRESUME				PDumpResumeKM
+
+#else
+		#if ((defined(LINUX) || defined(GCC_IA32)) || defined(GCC_ARM))
+			#define PDUMPMEMPOL(args...)
+			#define PDUMPMEM(args...)
+			#define PDUMPMEM2(args...)
+			#define PDUMPMEMUM(args...)
+			#define PDUMPINIT(args...)
+			#define PDUMPDEINIT(args...)
+			#define PDUMPISLASTFRAME(args...)
+			#define PDUMPTESTFRAME(args...)
+			#define PDUMPTESTNEXTFRAME(args...)
+			#define PDUMPREGWITHFLAGS(args...)
+			#define PDUMPREG(args...)
+			#define PDUMPCOMMENT(args...)
+			#define PDUMPREGPOL(args...)
+			#define PDUMPREGPOLWITHFLAGS(args...)
+			#define PDUMPMALLOCPAGES(args...)
+			#define PDUMPMALLOCPAGETABLE(args...)
+			#define PDUMPSETMMUCONTEXT(args...)
+			#define PDUMPCLEARMMUCONTEXT(args...)
+			#define PDUMPFREEPAGES(args...)
+			#define PDUMPFREEPAGETABLE(args...)
+			#define PDUMPPDREG(args...)
+			#define PDUMPPDREGWITHFLAGS(args...)
+			#define PDUMPSYNC(args...)
+			#define PDUMPCOPYTOMEM(args...)
+			#define PDUMPWRITE(args...)
+			#define PDUMPCBP(args...)
+			#define PDUMPCOMMENTWITHFLAGS(args...)
+			#define PDUMPMALLOCPAGESPHYS(args...)
+			#define PDUMPENDINITPHASE(args...)
+			#define PDUMPMSVDXREG(args...)
+			#define PDUMPMSVDXREGWRITE(args...)
+			#define PDUMPMSVDXREGREAD(args...)
+			#define PDUMPMSVDXPOLEQ(args...)
+			#define PDUMPMSVDXPOL(args...)
+			#define PDUMPBITMAPKM(args...)
+			#define PDUMPDRIVERINFO(args...)
+			#define PDUMPIDLWITHFLAGS(args...)
+			#define PDUMPIDL(args...)
+			#define PDUMPSUSPEND(args...)
+			#define PDUMPRESUME(args...)
+			#define PDUMPMSVDXWRITEREF(args...)
+		#else
+			#error Compiler not specified
+		#endif
+#endif
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_osfunc.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_osfunc.h
new file mode 100644
index 0000000..eb2197f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/pdump_osfunc.h
@@ -0,0 +1,133 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __PDUMP_OSFUNC_H__
+#define __PDUMP_OSFUNC_H__
+
+#include <stdarg.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+
+#define MAX_PDUMP_STRING_LENGTH (256)
+#define PDUMP_GET_SCRIPT_STRING()				\
+	IMG_HANDLE hScript;							\
+	IMG_UINT32	ui32MaxLen;						\
+	PVRSRV_ERROR eError;						\
+	eError = PDumpOSGetScriptString(&hScript, &ui32MaxLen);\
+	if(eError != PVRSRV_OK) return eError;
+
+#define PDUMP_GET_MSG_STRING()					\
+	IMG_HANDLE hMsg;							\
+	IMG_UINT32	ui32MaxLen;						\
+	PVRSRV_ERROR eError;						\
+	eError = PDumpOSGetMessageString(&hMsg, &ui32MaxLen);\
+	if(eError != PVRSRV_OK) return eError;
+
+#define PDUMP_GET_FILE_STRING()				\
+	IMG_CHAR *pszFileName;					\
+	IMG_UINT32	ui32MaxLen;					\
+	PVRSRV_ERROR eError;					\
+	eError = PDumpOSGetFilenameString(&pszFileName, &ui32MaxLen);\
+	if(eError != PVRSRV_OK) return eError;
+
+#define PDUMP_GET_SCRIPT_AND_FILE_STRING()		\
+	IMG_HANDLE hScript;							\
+	IMG_CHAR *pszFileName;						\
+	IMG_UINT32	ui32MaxLenScript;				\
+	IMG_UINT32	ui32MaxLenFileName;				\
+	PVRSRV_ERROR eError;						\
+	eError = PDumpOSGetScriptString(&hScript, &ui32MaxLenScript);\
+	if(eError != PVRSRV_OK) return eError;		\
+	eError = PDumpOSGetFilenameString(&pszFileName, &ui32MaxLenFileName);\
+	if(eError != PVRSRV_OK) return eError;
+
+
+
+	PVRSRV_ERROR PDumpOSGetScriptString(IMG_HANDLE *phScript, IMG_UINT32 *pui32MaxLen);
+
+
+	PVRSRV_ERROR PDumpOSGetMessageString(IMG_HANDLE *phMsg, IMG_UINT32 *pui32MaxLen);
+
+
+	PVRSRV_ERROR PDumpOSGetFilenameString(IMG_CHAR **ppszFile, IMG_UINT32 *pui32MaxLen);
+
+
+
+
+#define PDUMP_va_list	va_list
+#define PDUMP_va_start	va_start
+#define PDUMP_va_end	va_end
+
+
+
+IMG_HANDLE PDumpOSGetStream(IMG_UINT32 ePDumpStream);
+
+IMG_UINT32 PDumpOSGetStreamOffset(IMG_UINT32 ePDumpStream);
+
+IMG_UINT32 PDumpOSGetParamFileNum(IMG_VOID);
+
+IMG_VOID PDumpOSCheckForSplitting(IMG_HANDLE hStream, IMG_UINT32 ui32Size, IMG_UINT32 ui32Flags);
+
+IMG_BOOL PDumpOSIsSuspended(IMG_VOID);
+
+IMG_BOOL PDumpOSJTInitialised(IMG_VOID);
+
+IMG_BOOL PDumpOSWriteString(IMG_HANDLE hDbgStream,
+		IMG_UINT8 *psui8Data,
+		IMG_UINT32 ui32Size,
+		IMG_UINT32 ui32Flags);
+
+IMG_BOOL PDumpOSWriteString2(IMG_HANDLE	hScript, IMG_UINT32 ui32Flags);
+
+PVRSRV_ERROR PDumpOSBufprintf(IMG_HANDLE hBuf, IMG_UINT32 ui32ScriptSizeMax, IMG_CHAR* pszFormat, ...);
+
+IMG_VOID PDumpOSDebugPrintf(IMG_CHAR* pszFormat, ...);
+
+PVRSRV_ERROR PDumpOSSprintf(IMG_CHAR *pszComment, IMG_UINT32 ui32ScriptSizeMax, IMG_CHAR *pszFormat, ...);
+
+PVRSRV_ERROR PDumpOSVSprintf(IMG_CHAR *pszMsg, IMG_UINT32 ui32ScriptSizeMax, IMG_CHAR* pszFormat, PDUMP_va_list vaArgs);
+
+IMG_UINT32 PDumpOSBuflen(IMG_HANDLE hBuffer, IMG_UINT32 ui32BufferSizeMax);
+
+IMG_VOID PDumpOSVerifyLineEnding(IMG_HANDLE hBuffer, IMG_UINT32 ui32BufferSizeMax);
+
+IMG_VOID PDumpOSCPUVAddrToDevPAddr(PVRSRV_DEVICE_TYPE eDeviceType,
+        IMG_HANDLE hOSMemHandle,
+		IMG_UINT32 ui32Offset,
+		IMG_UINT8 *pui8LinAddr,
+		IMG_UINT32 ui32PageSize,
+		IMG_DEV_PHYADDR *psDevPAddr);
+
+IMG_VOID PDumpOSCPUVAddrToPhysPages(IMG_HANDLE hOSMemHandle,
+		IMG_UINT32 ui32Offset,
+		IMG_PUINT8 pui8LinAddr,
+		IMG_UINT32 *pui32PageOffset);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/perproc.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/perproc.h
new file mode 100644
index 0000000..11c1608
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/perproc.h
@@ -0,0 +1,106 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __PERPROC_H__
+#define __PERPROC_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#include "img_types.h"
+#include "resman.h"
+
+#include "handle.h"
+
+typedef struct _PVRSRV_PER_PROCESS_DATA_
+{
+	IMG_UINT32		ui32PID;
+	IMG_HANDLE		hBlockAlloc;
+	PRESMAN_CONTEXT 	hResManContext;
+	IMG_HANDLE		hPerProcData;
+	PVRSRV_HANDLE_BASE 	*psHandleBase;
+#if defined (PVR_SECURE_HANDLES)
+
+	IMG_BOOL		bHandlesBatched;
+#endif
+	IMG_UINT32		ui32RefCount;
+
+
+	IMG_BOOL		bInitProcess;
+
+
+	IMG_HANDLE		hOsPrivateData;
+} PVRSRV_PER_PROCESS_DATA;
+
+PVRSRV_PER_PROCESS_DATA *PVRSRVPerProcessData(IMG_UINT32 ui32PID);
+
+PVRSRV_ERROR PVRSRVPerProcessDataConnect(IMG_UINT32	ui32PID);
+IMG_VOID PVRSRVPerProcessDataDisconnect(IMG_UINT32	ui32PID);
+
+PVRSRV_ERROR PVRSRVPerProcessDataInit(IMG_VOID);
+PVRSRV_ERROR PVRSRVPerProcessDataDeInit(IMG_VOID);
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVFindPerProcessData)
+#endif
+static INLINE
+PVRSRV_PER_PROCESS_DATA *PVRSRVFindPerProcessData(IMG_VOID)
+{
+	return PVRSRVPerProcessData(OSGetCurrentProcessIDKM());
+}
+
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVProcessPrivateData)
+#endif
+static INLINE
+IMG_HANDLE PVRSRVProcessPrivateData(PVRSRV_PER_PROCESS_DATA *psPerProc)
+{
+	return (psPerProc != IMG_NULL) ? psPerProc->hOsPrivateData : IMG_NULL;
+}
+
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVPerProcessPrivateData)
+#endif
+static INLINE
+IMG_HANDLE PVRSRVPerProcessPrivateData(IMG_UINT32 ui32PID)
+{
+	return PVRSRVProcessPrivateData(PVRSRVPerProcessData(ui32PID));
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(PVRSRVFindPerProcessPrivateData)
+#endif
+static INLINE
+IMG_HANDLE PVRSRVFindPerProcessPrivateData(IMG_VOID)
+{
+	return PVRSRVProcessPrivateData(PVRSRVFindPerProcessData());
+}
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/power.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/power.h
new file mode 100644
index 0000000..1e47736
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/power.h
@@ -0,0 +1,116 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef POWER_H
+#define POWER_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+
+
+typedef struct _PVRSRV_POWER_DEV_TAG_
+{
+	PFN_PRE_POWER					pfnPrePower;
+	PFN_POST_POWER					pfnPostPower;
+	PFN_PRE_CLOCKSPEED_CHANGE		pfnPreClockSpeedChange;
+	PFN_POST_CLOCKSPEED_CHANGE		pfnPostClockSpeedChange;
+	IMG_HANDLE						hDevCookie;
+	IMG_UINT32						ui32DeviceIndex;
+	PVRSRV_DEV_POWER_STATE 			eDefaultPowerState;
+	PVRSRV_DEV_POWER_STATE 			eCurrentPowerState;
+	struct _PVRSRV_POWER_DEV_TAG_	*psNext;
+	struct _PVRSRV_POWER_DEV_TAG_	**ppsThis;
+
+} PVRSRV_POWER_DEV;
+
+typedef enum _PVRSRV_INIT_SERVER_STATE_
+{
+	PVRSRV_INIT_SERVER_Unspecified		= -1,
+	PVRSRV_INIT_SERVER_RUNNING			= 0,
+	PVRSRV_INIT_SERVER_RAN				= 1,
+	PVRSRV_INIT_SERVER_SUCCESSFUL		= 2,
+	PVRSRV_INIT_SERVER_NUM				= 3,
+	PVRSRV_INIT_SERVER_FORCE_I32 = 0x7fffffff
+
+} PVRSRV_INIT_SERVER_STATE, *PPVRSRV_INIT_SERVER_STATE;
+
+IMG_IMPORT
+IMG_BOOL PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_STATE	eInitServerState);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetInitServerState(PVRSRV_INIT_SERVER_STATE	eInitServerState, IMG_BOOL bState);
+
+
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVPowerLock(IMG_UINT32	ui32CallerID,
+							 IMG_BOOL	bSystemPowerEvent);
+IMG_IMPORT
+IMG_VOID PVRSRVPowerUnlock(IMG_UINT32	ui32CallerID);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetDevicePowerStateKM(IMG_UINT32				ui32DeviceIndex,
+										 PVRSRV_DEV_POWER_STATE	eNewPowerState,
+										 IMG_UINT32				ui32CallerID,
+										 IMG_BOOL				bRetainMutex);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSystemPrePowerStateKM(PVRSRV_SYS_POWER_STATE eNewPowerState);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSystemPostPowerStateKM(PVRSRV_SYS_POWER_STATE eNewPowerState);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVSetPowerStateKM (PVRSRV_SYS_POWER_STATE ePVRState);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVRegisterPowerDevice(IMG_UINT32					ui32DeviceIndex,
+									   PFN_PRE_POWER				pfnPrePower,
+									   PFN_POST_POWER				pfnPostPower,
+									   PFN_PRE_CLOCKSPEED_CHANGE	pfnPreClockSpeedChange,
+									   PFN_POST_CLOCKSPEED_CHANGE	pfnPostClockSpeedChange,
+									   IMG_HANDLE					hDevCookie,
+									   PVRSRV_DEV_POWER_STATE		eCurrentPowerState,
+									   PVRSRV_DEV_POWER_STATE		eDefaultPowerState);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVRemovePowerDevice (IMG_UINT32 ui32DeviceIndex);
+
+IMG_IMPORT
+IMG_BOOL PVRSRVIsDevicePowered(IMG_UINT32 ui32DeviceIndex);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVDevicePreClockSpeedChange(IMG_UINT32	ui32DeviceIndex,
+											 IMG_BOOL	bIdleDevice,
+											 IMG_VOID	*pvInfo);
+
+IMG_IMPORT
+IMG_VOID PVRSRVDevicePostClockSpeedChange(IMG_UINT32	ui32DeviceIndex,
+										  IMG_BOOL		bIdleDevice,
+										  IMG_VOID		*pvInfo);
+
+#if defined (__cplusplus)
+}
+#endif
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/queue.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/queue.h
new file mode 100644
index 0000000..f3e5df6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/queue.h
@@ -0,0 +1,115 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef QUEUE_H
+#define QUEUE_H
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define UPDATE_QUEUE_ROFF(psQueue, ui32Size)						\
+	psQueue->ui32ReadOffset = (psQueue->ui32ReadOffset + ui32Size)	\
+	& (psQueue->ui32QueueSize - 1);
+
+ typedef struct _COMMAND_COMPLETE_DATA_
+ {
+	IMG_BOOL			bInUse;
+
+	IMG_UINT32			ui32DstSyncCount;
+	IMG_UINT32			ui32SrcSyncCount;
+	PVRSRV_SYNC_OBJECT	*psDstSync;
+	PVRSRV_SYNC_OBJECT	*psSrcSync;
+	IMG_UINT32			ui32AllocSize;
+ }COMMAND_COMPLETE_DATA, *PCOMMAND_COMPLETE_DATA;
+
+#if !defined(USE_CODE)
+IMG_VOID QueueDumpDebugInfo(IMG_VOID);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVProcessQueues (IMG_UINT32	ui32CallerID,
+								  IMG_BOOL		bFlush);
+
+#if defined(__linux__) && defined(__KERNEL__)
+#include <linux/types.h>
+#include <linux/seq_file.h>
+off_t
+QueuePrintQueues (IMG_CHAR * buffer, size_t size, off_t off);
+
+#ifdef PVR_PROC_USE_SEQ_FILE
+void* ProcSeqOff2ElementQueue(struct seq_file * sfile, loff_t off);
+void ProcSeqShowQueue(struct seq_file *sfile,void* el);
+#endif
+
+#endif
+
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVCreateCommandQueueKM(IMG_SIZE_T ui32QueueSize,
+													 PVRSRV_QUEUE_INFO **ppsQueueInfo);
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVDestroyCommandQueueKM(PVRSRV_QUEUE_INFO *psQueueInfo);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVInsertCommandKM(PVRSRV_QUEUE_INFO	*psQueue,
+												PVRSRV_COMMAND		**ppsCommand,
+												IMG_UINT32			ui32DevIndex,
+												IMG_UINT16			CommandType,
+												IMG_UINT32			ui32DstSyncCount,
+												PVRSRV_KERNEL_SYNC_INFO	*apsDstSync[],
+												IMG_UINT32			ui32SrcSyncCount,
+												PVRSRV_KERNEL_SYNC_INFO	*apsSrcSync[],
+												IMG_SIZE_T			ui32DataByteSize );
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVGetQueueSpaceKM(PVRSRV_QUEUE_INFO *psQueue,
+												IMG_SIZE_T ui32ParamSize,
+												IMG_VOID **ppvSpace);
+
+IMG_IMPORT
+PVRSRV_ERROR IMG_CALLCONV PVRSRVSubmitCommandKM(PVRSRV_QUEUE_INFO *psQueue,
+												PVRSRV_COMMAND *psCommand);
+
+IMG_IMPORT
+IMG_VOID PVRSRVCommandCompleteKM(IMG_HANDLE hCmdCookie, IMG_BOOL bScheduleMISR);
+
+IMG_VOID PVRSRVCommandCompleteCallbacks(IMG_VOID);
+
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVRegisterCmdProcListKM(IMG_UINT32		ui32DevIndex,
+										 PFN_CMD_PROC	*ppfnCmdProcList,
+										 IMG_UINT32		ui32MaxSyncsPerCmd[][2],
+										 IMG_UINT32		ui32CmdCount);
+IMG_IMPORT
+PVRSRV_ERROR PVRSRVRemoveCmdProcListKM(IMG_UINT32	ui32DevIndex,
+									   IMG_UINT32	ui32CmdCount);
+
+#endif
+
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/ra.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/ra.h
new file mode 100644
index 0000000..d537601
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/ra.h
@@ -0,0 +1,151 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _RA_H_
+#define _RA_H_
+
+#include "img_types.h"
+#include "hash.h"
+#include "osfunc.h"
+
+typedef struct _RA_ARENA_ RA_ARENA;
+typedef struct _BM_MAPPING_ BM_MAPPING;
+
+
+
+#define RA_STATS
+
+
+struct _RA_STATISTICS_
+{
+
+    IMG_SIZE_T uSpanCount;
+
+
+    IMG_SIZE_T uLiveSegmentCount;
+
+
+    IMG_SIZE_T uFreeSegmentCount;
+
+
+    IMG_SIZE_T uTotalResourceCount;
+
+
+    IMG_SIZE_T uFreeResourceCount;
+
+
+    IMG_SIZE_T uCumulativeAllocs;
+
+
+    IMG_SIZE_T uCumulativeFrees;
+
+
+    IMG_SIZE_T uImportCount;
+
+
+    IMG_SIZE_T uExportCount;
+};
+typedef struct _RA_STATISTICS_ RA_STATISTICS;
+
+struct _RA_SEGMENT_DETAILS_
+{
+	IMG_SIZE_T      uiSize;
+	IMG_CPU_PHYADDR sCpuPhyAddr;
+	IMG_HANDLE      hSegment;
+};
+typedef struct _RA_SEGMENT_DETAILS_ RA_SEGMENT_DETAILS;
+
+RA_ARENA *
+RA_Create (IMG_CHAR *name,
+           IMG_UINTPTR_T base,
+           IMG_SIZE_T uSize,
+           BM_MAPPING *psMapping,
+           IMG_SIZE_T uQuantum,
+           IMG_BOOL (*imp_alloc)(IMG_VOID *_h,
+                                IMG_SIZE_T uSize,
+                                IMG_SIZE_T *pActualSize,
+                                BM_MAPPING **ppsMapping,
+                                IMG_UINT32 uFlags,
+                                IMG_UINTPTR_T *pBase),
+           IMG_VOID (*imp_free) (IMG_VOID *,
+                                IMG_UINTPTR_T,
+                                BM_MAPPING *),
+           IMG_VOID (*backingstore_free) (IMG_VOID *,
+                                          IMG_SIZE_T,
+                                          IMG_SIZE_T,
+                                          IMG_HANDLE),
+           IMG_VOID *import_handle);
+
+IMG_VOID
+RA_Delete (RA_ARENA *pArena);
+
+IMG_BOOL
+RA_TestDelete (RA_ARENA *pArena);
+
+IMG_BOOL
+RA_Add (RA_ARENA *pArena, IMG_UINTPTR_T base, IMG_SIZE_T uSize);
+
+IMG_BOOL
+RA_Alloc (RA_ARENA *pArena,
+          IMG_SIZE_T uSize,
+          IMG_SIZE_T *pActualSize,
+          BM_MAPPING **ppsMapping,
+          IMG_UINT32 uFlags,
+          IMG_UINT32 uAlignment,
+		  IMG_UINT32 uAlignmentOffset,
+          IMG_UINTPTR_T *pBase);
+
+IMG_VOID
+RA_Free (RA_ARENA *pArena, IMG_UINTPTR_T base, IMG_BOOL bFreeBackingStore);
+
+
+#ifdef RA_STATS
+
+#define CHECK_SPACE(total)					\
+{											\
+	if(total<100) 							\
+		return PVRSRV_ERROR_INVALID_PARAMS;	\
+}
+
+#define UPDATE_SPACE(str, count, total)		\
+{											\
+	if(count == -1)					 		\
+		return PVRSRV_ERROR_INVALID_PARAMS;	\
+	else									\
+	{										\
+		str += count;						\
+		total -= count;						\
+	}										\
+}
+
+
+IMG_BOOL RA_GetNextLiveSegment(IMG_HANDLE hArena, RA_SEGMENT_DETAILS *psSegDetails);
+
+
+PVRSRV_ERROR RA_GetStats(RA_ARENA *pArena,
+							IMG_CHAR **ppszStr,
+							IMG_UINT32 *pui32StrLen);
+
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/resman.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/resman.h
new file mode 100644
index 0000000..a2c6d02
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/resman.h
@@ -0,0 +1,109 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef __RESMAN_H__
+#define __RESMAN_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+enum {
+
+	RESMAN_TYPE_SHARED_PB_DESC = 1,
+	RESMAN_TYPE_SHARED_PB_DESC_CREATE_LOCK,
+	RESMAN_TYPE_HW_RENDER_CONTEXT,
+	RESMAN_TYPE_HW_TRANSFER_CONTEXT,
+	RESMAN_TYPE_HW_2D_CONTEXT,
+	RESMAN_TYPE_TRANSFER_CONTEXT,
+
+
+
+
+
+	RESMAN_TYPE_DISPLAYCLASS_SWAPCHAIN_REF,
+	RESMAN_TYPE_DISPLAYCLASS_DEVICE,
+
+
+	RESMAN_TYPE_BUFFERCLASS_DEVICE,
+
+
+	RESMAN_TYPE_OS_USERMODE_MAPPING,
+
+
+	RESMAN_TYPE_DEVICEMEM_CONTEXT,
+	RESMAN_TYPE_DEVICECLASSMEM_MAPPING,
+	RESMAN_TYPE_DEVICEMEM_MAPPING,
+	RESMAN_TYPE_DEVICEMEM_WRAP,
+	RESMAN_TYPE_DEVICEMEM_ALLOCATION,
+	RESMAN_TYPE_EVENT_OBJECT,
+    RESMAN_TYPE_SHARED_MEM_INFO,
+    RESMAN_TYPE_MODIFY_SYNC_OPS,
+
+
+	RESMAN_TYPE_KERNEL_DEVICEMEM_ALLOCATION
+};
+
+#define RESMAN_CRITERIA_ALL				0x00000000
+#define RESMAN_CRITERIA_RESTYPE			0x00000001
+#define RESMAN_CRITERIA_PVOID_PARAM		0x00000002
+#define RESMAN_CRITERIA_UI32_PARAM		0x00000004
+
+typedef PVRSRV_ERROR (*RESMAN_FREE_FN)(IMG_PVOID pvParam, IMG_UINT32 ui32Param);
+
+typedef struct _RESMAN_ITEM_ *PRESMAN_ITEM;
+typedef struct _RESMAN_CONTEXT_ *PRESMAN_CONTEXT;
+
+PVRSRV_ERROR ResManInit(IMG_VOID);
+IMG_VOID ResManDeInit(IMG_VOID);
+
+PRESMAN_ITEM ResManRegisterRes(PRESMAN_CONTEXT	hResManContext,
+							   IMG_UINT32		ui32ResType,
+							   IMG_PVOID		pvParam,
+							   IMG_UINT32		ui32Param,
+							   RESMAN_FREE_FN	pfnFreeResource);
+
+PVRSRV_ERROR ResManFreeResByPtr(PRESMAN_ITEM	psResItem);
+
+PVRSRV_ERROR ResManFreeResByCriteria(PRESMAN_CONTEXT	hResManContext,
+									 IMG_UINT32			ui32SearchCriteria,
+									 IMG_UINT32			ui32ResType,
+									 IMG_PVOID			pvParam,
+									 IMG_UINT32			ui32Param);
+
+PVRSRV_ERROR ResManDissociateRes(PRESMAN_ITEM		psResItem,
+							 PRESMAN_CONTEXT	psNewResManContext);
+
+PVRSRV_ERROR ResManFindResourceByPtr(PRESMAN_CONTEXT	hResManContext,
+									 PRESMAN_ITEM		psItem);
+
+PVRSRV_ERROR PVRSRVResManConnect(IMG_HANDLE			hPerProc,
+								 PRESMAN_CONTEXT	*phResManContext);
+IMG_VOID PVRSRVResManDisconnect(PRESMAN_CONTEXT hResManContext,
+								IMG_BOOL		bKernelContext);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/services_headers.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/services_headers.h
new file mode 100644
index 0000000..4f6c2a2
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/services_headers.h
@@ -0,0 +1,45 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef SERVICES_HEADERS_H
+#define SERVICES_HEADERS_H
+
+#ifdef DEBUG_RELEASE_BUILD
+#pragma optimize( "", off )
+#define DEBUG		1
+#endif
+
+#include "img_defs.h"
+#include "services.h"
+#include "servicesint.h"
+#include "power.h"
+#include "resman.h"
+#include "queue.h"
+#include "srvkm.h"
+#include "kerneldisplay.h"
+#include "sysconfig.h"
+#include "pvr_debug.h"
+#include "metrics.h"
+#include "osfunc.h"
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/srvkm.h b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/srvkm.h
new file mode 100644
index 0000000..5506992
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/srvkm/include/srvkm.h
@@ -0,0 +1,65 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef SRVKM_H
+#define SRVKM_H
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+
+	#ifdef PVR_DISABLE_LOGGING
+	#define PVR_LOG(X)
+	#else
+	#define PVR_LOG(X)			PVRSRVReleasePrintf X
+	#endif
+
+	IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVReleasePrintf(const IMG_CHAR *pszFormat,
+										...);
+
+	IMG_IMPORT PVRSRV_ERROR IMG_CALLCONV PVRSRVProcessConnect(IMG_UINT32	ui32PID);
+	IMG_IMPORT IMG_VOID IMG_CALLCONV PVRSRVProcessDisconnect(IMG_UINT32	ui32PID);
+
+	IMG_VOID IMG_CALLCONV PVRSRVSetDCState(IMG_UINT32 ui32State);
+
+	PVRSRV_ERROR IMG_CALLCONV PVRSRVSaveRestoreLiveSegments(IMG_HANDLE hArena, IMG_PBYTE pbyBuffer, IMG_SIZE_T *puiBufSize, IMG_BOOL bSave);
+
+#if defined (__cplusplus)
+}
+#endif
+
+#define LOOP_UNTIL_TIMEOUT(TIMEOUT) \
+{\
+	IMG_UINT32 uiOffset, uiStart, uiCurrent, uiNotLastLoop;								\
+	for(uiOffset = 0, uiStart = OSClockus(), uiCurrent = uiStart + 1, uiNotLastLoop = 1;\
+		((uiCurrent - uiStart + uiOffset) < TIMEOUT) || uiNotLastLoop--;				\
+		uiCurrent = OSClockus(),														\
+		uiOffset = uiCurrent < uiStart ? IMG_UINT32_MAX - uiStart : uiOffset,			\
+		uiStart = uiCurrent < uiStart ? 0 : uiStart)
+
+#define END_LOOP_UNTIL_TIMEOUT() \
+}
+
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/common/sysconfig.c b/drivers/gpu/drm/emgd/pvr/services4/system/common/sysconfig.c
new file mode 100644
index 0000000..6146806
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/common/sysconfig.c
@@ -0,0 +1,1367 @@
+/* -*- syscommon-c -*-
+ *-----------------------------------------------------------------------------
+ * Filename: syscommon.c
+ * $Revision: 1.11 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description: platform detection, and sharing of correct platform interface.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include <linux/pci.h>
+#include "sgxdefs.h"
+#include "services_headers.h"
+#include "kerneldisplay.h"
+#include "oemfuncs.h"
+#include "sgxinfo.h"
+#include "sgxinfokm.h"
+#ifdef SUPPORT_MSVDX
+#include "msvdx_defs.h"
+#include "msvdxapi.h"
+#include "msvdx_infokm.h"
+#include "osfunc.h"
+#endif
+#include "pdump_km.h"
+#include "syslocal.h"
+#include "sysconfig.h"
+#include "pvr_debug.h"
+#include "msvdx_pvr.h"
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Pointer to "System Data" used by all platform blocks, exported
+* in "syscommon.h" header file.
+*/
+/* --------------------------------------------------------------------------*/
+SYS_DATA *gpsSysData = (SYS_DATA*)IMG_NULL;
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Pointer to found platform interface. It is IMG_NULL when no
+* correct platform can be found.
+*/
+/* --------------------------------------------------------------------------*/
+SYS_PLATFORM_INTERFACE *gpsSysPlatformInterface = (SYS_PLATFORM_INTERFACE*)IMG_NULL;
+
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Poulsbo platform interface.
+*/
+/* --------------------------------------------------------------------------*/
+extern SYS_PLATFORM_INTERFACE gpsSysPlatformInterfacePlb;
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Atom E6xx platform interface.
+*/
+/* --------------------------------------------------------------------------*/
+extern SYS_PLATFORM_INTERFACE gpsSysPlatformInterfaceTnc;
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Table to available platforms interfaces.
+* The last element must be set to IMG_NULL.
+*
+* @Param &gpsSysPlatformInterfacePlb Poulsbo interface
+* @Param &gpsSysPlatformInterfaceTnc Atom E6xx interface
+*/
+/* --------------------------------------------------------------------------*/
+SYS_PLATFORM_INTERFACE* gpsSysPlatformInterfacesTab[] = {	&gpsSysPlatformInterfacePlb,
+								&gpsSysPlatformInterfaceTnc,
+								(SYS_PLATFORM_INTERFACE*)IMG_NULL};
+
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Function to detect platform and set "gpsSysPlatformInterface" to
+* one member of "gpsSysPlatformInterfacesTab".
+*
+* @Param IMG_VOID
+*
+* @Returns
+*/
+/* --------------------------------------------------------------------------*/
+IMG_VOID SysPlatformDetect(IMG_VOID)
+{
+	SYS_PLATFORM_INTERFACE** psSysPlatformInterfacesTab = gpsSysPlatformInterfacesTab;
+
+	while(*psSysPlatformInterfacesTab != IMG_NULL) {
+
+		if(pci_get_device(SYS_SGX_DEV_VENDOR_ID, (unsigned short)((*psSysPlatformInterfacesTab)->uiSgxDevDeviceID), NULL)) {
+			gpsSysPlatformInterface = *psSysPlatformInterfacesTab;
+			PVR_DPF((PVR_DBG_MESSAGE,"Platform detected: %s", gpsSysPlatformInterface->sProductName));
+			return;
+		}
+		PVR_DPF((PVR_DBG_MESSAGE,"Platform search: %s", (*psSysPlatformInterfacesTab)->sProductName));
+		psSysPlatformInterfacesTab++;
+	}
+	PVR_DPF((PVR_DBG_ERROR,"Platform detected Failed"));
+}
+
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis  Interface for Atom E6xx device
+*/
+/* ----------------------------------------------------------------------------*/
+
+static SYS_DATA  gsSysData;
+
+static SYS_SPECIFIC_DATA gsSysSpecificData;
+
+static IMG_UINT32		gui32SGXDeviceID;
+static SGX_DEVICE_MAP	gsSGXDeviceMap;
+static IMG_UINT32		gui32MSVDXDeviceID;
+
+#ifdef SUPPORT_MSVDX
+static IMG_UINT32		gui32MSVDXDeviceID;
+static MSVDX_DEVICE_MAP	gsMSVDXDeviceMap;
+#endif
+
+
+#if defined(NO_HARDWARE)
+static IMG_CPU_VIRTADDR gsSGXRegsCPUVAddr;
+#ifdef SUPPORT_MSVDX
+static IMG_CPU_VIRTADDR gsMSVDXRegsCPUVAddr;
+#endif
+#endif
+
+#if !defined(NO_HARDWARE)
+static IMG_CPU_VIRTADDR gsPoulsboRegsCPUVaddr;
+
+#if defined(MAP_UNUSED_MAPPINGS)
+static IMG_CPU_VIRTADDR gsPoulsboDisplayRegsCPUVaddr;
+#endif
+
+#endif
+
+#ifdef	LDM_PCI
+extern struct pci_dev *gpsPVRLDMDev;
+#endif
+
+IMG_UINT32 PVRSRV_BridgeDispatchKM( IMG_UINT32  Ioctl,
+									IMG_BYTE   *pInBuf,
+									IMG_UINT32  InBufLen,
+									IMG_BYTE   *pOutBuf,
+									IMG_UINT32  OutBufLen,
+									IMG_UINT32 *pdwBytesTransferred);
+
+#ifdef __linux__
+#define	ADDR_RANGE_INDEX	(MMADR_INDEX - 4)
+#if defined(SGX_FEATURE_HOST_PORT)
+#define	HP_ADDR_RANGE_INDEX	(GMADR_INDEX - 4)
+#endif
+static PVRSRV_ERROR PCIInitDev(SYS_DATA *psSysData)
+{
+	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;
+
+#ifdef	LDM_PCI
+	psSysSpecData->hSGXPCI = OSPCISetDev((IMG_VOID *)psSysSpecData->psPCIDev, HOST_PCI_INIT_FLAG_BUS_MASTER);
+#else
+	psSysSpecData->hSGXPCI = OSPCIAcquireDev(SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV_DEVICE_ID, HOST_PCI_INIT_FLAG_BUS_MASTER);
+#endif
+	if (!psSysSpecData->hSGXPCI)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PCIInitDev: Failed to acquire PCI device"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	 SYS_SPECIFIC_DATA_SET(psSysSpecData, SYS_SPECIFIC_DATA_PCI_ACQUIRE_DEV);
+
+	PVR_TRACE(("PCI memory region: %x to %x", OSPCIAddrRangeStart(psSysSpecData->hSGXPCI, ADDR_RANGE_INDEX), OSPCIAddrRangeEnd(psSysSpecData->hSGXPCI, ADDR_RANGE_INDEX)));
+#if defined(SGX_FEATURE_HOST_PORT)
+	PVR_TRACE(("Host Port region: %x to %x", OSPCIAddrRangeStart(psSysSpecData->hSGXPCI, HP_ADDR_RANGE_INDEX), OSPCIAddrRangeEnd(psSysSpecData->hSGXPCI, HP_ADDR_RANGE_INDEX)));
+#endif
+
+	if (OSPCIAddrRangeLen(psSysSpecData->hSGXPCI, ADDR_RANGE_INDEX) < MAX_OFFSET)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PCIInitDev: Device memory region isn't big enough"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+
+	if (OSPCIRequestAddrRange(psSysSpecData->hSGXPCI, ADDR_RANGE_INDEX) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PCIInitDev: Device memory region not available"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+	 SYS_SPECIFIC_DATA_SET(psSysSpecData, SYS_SPECIFIC_DATA_PCI_REQUEST_SGX_ADDR_RANGE);
+
+
+#if defined(SGX_FEATURE_HOST_PORT)
+	if (OSPCIRequestAddrRange(psSysSpecData->hSGXPCI, HP_ADDR_RANGE_INDEX) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"PCIInitDev: Host Port region not available"));
+		return PVRSRV_ERROR_GENERIC;
+	}
+	 SYS_SPECIFIC_DATA_SET(psSysSpecData, SYS_SPECIFIC_DATA_PCI_REQUEST_HOST_PORT_RANGE);
+#endif
+	return PVRSRV_OK;
+}
+
+static IMG_VOID PCIDeInitDev(SYS_DATA *psSysData)
+{
+	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;
+
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_PCI_REQUEST_SGX_ADDR_RANGE))
+	{
+		OSPCIReleaseAddrRange(psSysSpecData->hSGXPCI, ADDR_RANGE_INDEX);
+	}
+
+#if defined(SGX_FEATURE_HOST_PORT)
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_PCI_REQUEST_HOST_PORT_RANGE))
+	{
+		OSPCIReleaseAddrRange(psSysSpecData->hSGXPCI, HP_ADDR_RANGE_INDEX);
+	}
+#endif
+
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_PCI_ACQUIRE_DEV))
+	{
+		OSPCIReleaseDev(psSysSpecData->hSGXPCI);
+	}
+}
+#else
+static PVRSRV_ERROR FindPCIDevice(IMG_UINT16 ui16VenID, IMG_UINT16 ui16DevID, PCICONFIG_SPACE *psPCISpace)
+{
+	IMG_UINT32 ui32BusNum;
+	IMG_UINT32 ui32DevNum;
+	IMG_UINT32 ui32VenDevID;
+
+
+	for (ui32BusNum=0; ui32BusNum < 255; ui32BusNum++)
+	{
+
+		for (ui32DevNum=0; ui32DevNum < 32; ui32DevNum++)
+		{
+
+			ui32VenDevID=OSPCIReadDword(ui32BusNum, ui32DevNum, 0, 0);
+
+
+			if (ui32VenDevID == (IMG_UINT32)((ui16DevID<<16)+ui16VenID))
+			{
+				IMG_UINT32 ui32Idx;
+
+
+				OSPCIWriteDword(ui32BusNum, ui32DevNum, 0, 4, OSPCIReadDword(ui32BusNum, ui32DevNum, 0, 4) | 0x02);
+
+
+				for (ui32Idx=0; ui32Idx < 64; ui32Idx++)
+				{
+					psPCISpace->u.aui32PCISpace[ui32Idx] = OSPCIReadDword(ui32BusNum, ui32DevNum, 0, ui32Idx*4);
+
+					if (ui32Idx < 16)
+					{
+						PVR_DPF((PVR_DBG_VERBOSE,"%08X\n",psPCISpace->u.aui32PCISpace[ui32Idx]));
+					}
+				}
+				return PVRSRV_OK;
+			}
+
+		}
+
+	}
+
+	PVR_DPF((PVR_DBG_ERROR,"Couldn't find PCI device"));
+
+	return PVRSRV_ERROR_GENERIC;
+}
+#endif
+
+static PVRSRV_ERROR SysLocateDevices(SYS_DATA *psSysData)
+{
+#if !defined(NO_HARDWARE) || defined(__linux__)
+	IMG_UINT32 ui32BaseAddr;
+	IMG_UINT32 ui32IRQ;
+#endif
+#if defined(SGX_FEATURE_HOST_PORT)
+	IMG_UINT32 ui32HostPortAddr = 0UL;
+#endif
+#if defined(NO_HARDWARE)
+	IMG_CPU_PHYADDR sCpuPAddr;
+#endif
+#if defined(NO_HARDWARE) || defined(__linux__)
+	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;
+#endif
+#if (!defined(__linux__) || defined(NO_HARDWARE))
+	PVRSRV_ERROR eError;
+#endif
+
+#ifdef __linux__
+	ui32BaseAddr = OSPCIAddrRangeStart(psSysSpecData->hSGXPCI, ADDR_RANGE_INDEX);
+#if defined(SGX_FEATURE_HOST_PORT)
+	ui32HostPortAddr = OSPCIAddrRangeStart(psSysSpecData->hSGXPCI, HP_ADDR_RANGE_INDEX);
+#endif
+	if (OSPCIIRQ(psSysSpecData->hSGXPCI, &ui32IRQ) != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysLocateDevices: Couldn't get IRQ"));
+		return PVRSRV_ERROR_INVALID_DEVICE;
+	}
+	PVR_TRACE(("IRQ: %d", ui32IRQ));
+#else
+	PVRSRV_ERROR eError;
+	PCICONFIG_SPACE	sPCISpace;
+
+	eError = FindPCIDevice(VENDOR_ID, DEVICE_ID, &sPCISpace);
+	if (eError == PVRSRV_OK)
+	{
+		ui32BaseAddr = sPCISpace.u.aui32PCISpace[MMADR_INDEX];
+#if defined(SGX_FEATURE_HOST_PORT)
+		ui32HostPortAddr = sPCISpace.u.aui32PCISpace[GMADR_INDEX];
+#endif
+	}
+	else
+	{
+		return PVRSRV_ERROR_INVALID_DEVICE;
+	}
+
+	ui32IRQ = (IMG_UINT32)sPCISpace.u.aui8PCISpace[0x3C];
+#endif
+
+
+	gsSGXDeviceMap.ui32Flags = 0x0;
+
+
+#if defined(NO_HARDWARE)
+
+	gsSGXDeviceMap.ui32IRQ = 0;
+#else
+	gsSGXDeviceMap.ui32IRQ = ui32IRQ;
+#endif
+
+#if defined(NO_HARDWARE)
+
+	eError = OSBaseAllocContigMemory(SGX_REG_SIZE,
+										&gsSGXRegsCPUVAddr,
+										&sCpuPAddr);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+	SYS_SPECIFIC_DATA_SET(psSysSpecData, SYS_SPECIFIC_DATA_ALLOC_DUMMY_SGX_REGS);
+
+	gsSGXDeviceMap.sRegsCpuPBase = sCpuPAddr;
+
+	OSMemSet(gsSGXRegsCPUVAddr, 0, SGX_REG_SIZE);
+
+#if defined(__linux__)
+
+	gsSGXDeviceMap.pvRegsCpuVBase = gsSGXRegsCPUVAddr;
+#else
+
+	gsSGXDeviceMap.pvRegsCpuVBase = IMG_NULL;
+#endif
+
+#else
+	gsSGXDeviceMap.sRegsSysPBase.uiAddr = ui32BaseAddr + SGX_REGS_OFFSET;
+#endif
+
+
+	gsSGXDeviceMap.sRegsCpuPBase = SysSysPAddrToCpuPAddr(gsSGXDeviceMap.sRegsSysPBase);
+	gsSGXDeviceMap.ui32RegsSize = SGX_REG_SIZE;
+
+#if defined(SGX_FEATURE_HOST_PORT)
+
+	gsSGXDeviceMap.ui32Flags = SGX_HOSTPORT_PRESENT;
+	gsSGXDeviceMap.sHPSysPBase.uiAddr = ui32HostPortAddr;
+	gsSGXDeviceMap.sHPCpuPBase = SysSysPAddrToCpuPAddr(gsSGXDeviceMap.sHPSysPBase);
+	gsSGXDeviceMap.ui32HPSize = SYS_SGX_HP_SIZE;
+#endif
+
+
+
+
+	gsSGXDeviceMap.sLocalMemSysPBase.uiAddr = 0;
+	gsSGXDeviceMap.sLocalMemDevPBase.uiAddr = 0;
+	gsSGXDeviceMap.sLocalMemCpuPBase.uiAddr = 0;
+	gsSGXDeviceMap.ui32LocalMemSize = 0;
+
+#if !defined(NO_HARDWARE)
+
+	{
+		IMG_SYS_PHYADDR sPoulsboRegsCpuPBase;
+
+		sPoulsboRegsCpuPBase.uiAddr = ui32BaseAddr + REGS_OFFSET;
+		gsPoulsboRegsCPUVaddr = OSMapPhysToLin(SysSysPAddrToCpuPAddr(sPoulsboRegsCpuPBase),
+												 REG_SIZE,
+												 PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+												 IMG_NULL);
+
+#if defined(MAP_UNUSED_MAPPINGS)
+		sPoulsboRegsCpuPBase.uiAddr = ui32BaseAddr + DISPLAY_REGS_OFFSET;
+		gsPoulsboDisplayRegsCPUVaddr = OSMapPhysToLin(SysSysPAddrToCpuPAddr(sPoulsboRegsCpuPBase),
+												 DISPLAY_REG_SIZE,
+												 PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+												 IMG_NULL);
+#endif
+	}
+#endif
+
+#ifdef SUPPORT_MSVDX
+
+
+#if defined(NO_HARDWARE)
+
+	eError = OSBaseAllocContigMemory(MSVDX_REG_SIZE,
+										&gsMSVDXRegsCPUVAddr,
+										&sCpuPAddr);
+	if(eError != PVRSRV_OK)
+	{
+		return eError;
+	}
+	SYS_SPECIFIC_DATA_SET(psSysSpecData, SYS_SPECIFIC_DATA_ALLOC_DUMMY_MSVDX_REGS);
+	gsMSVDXDeviceMap.sRegsCpuPBase = sCpuPAddr;
+
+	OSMemSet(gsMSVDXRegsCPUVAddr, 0, MSVDX_REG_SIZE);
+
+#if defined(__linux__)
+
+	gsMSVDXDeviceMap.pvRegsCpuVBase = gsMSVDXRegsCPUVAddr;
+#else
+
+	gsMSVDXDeviceMap.pvRegsCpuVBase = IMG_NULL;
+#endif
+#else
+	gsMSVDXDeviceMap.sRegsCpuPBase.uiAddr = ui32BaseAddr + MSVDX_REGS_OFFSET;
+#endif
+	gsMSVDXDeviceMap.sRegsSysPBase		  = SysCpuPAddrToSysPAddr(gsMSVDXDeviceMap.sRegsCpuPBase);
+	gsMSVDXDeviceMap.ui32RegsSize		  = MSVDX_REG_SIZE;
+
+
+
+
+
+	gsMSVDXDeviceMap.sLocalMemSysPBase.uiAddr = 0;
+	gsMSVDXDeviceMap.sLocalMemDevPBase.uiAddr = 0;
+	gsMSVDXDeviceMap.sLocalMemCpuPBase.uiAddr = 0;
+	gsMSVDXDeviceMap.ui32LocalMemSize		  = 0;
+
+
+
+	gsMSVDXDeviceMap.ui32IRQ = ui32IRQ;
+
+#endif
+
+
+
+	return PVRSRV_OK;
+}
+
+#ifdef SUPPORT_MSVDX_FPGA
+static PVRSRV_ERROR FindPCIDevice(IMG_UINT16 ui16VenID, IMG_UINT16 ui16DevID, PCICONFIG_SPACE *psPCISpace)
+{
+	IMG_UINT32  ui32BusNum;
+	IMG_UINT32  ui32DevNum;
+	IMG_UINT32  ui32VenDevID;
+	IMG_UINT32	ui32BarIndex;
+
+
+	for (ui32BusNum=0; ui32BusNum < 255; ui32BusNum++)
+	{
+
+		for (ui32DevNum=0; ui32DevNum < 32; ui32DevNum++)
+		{
+
+			ui32VenDevID=OSPCIReadDword(ui32BusNum, ui32DevNum, 0, 0);
+
+
+			if (ui32VenDevID == (IMG_UINT32)((ui16DevID<<16)+ui16VenID))
+			{
+				IMG_UINT32 ui32Idx;
+
+
+				OSPCIWriteDword(ui32BusNum, ui32DevNum, 0, 4, OSPCIReadDword(ui32BusNum, ui32DevNum, 0, 4) | 0x02);
+
+				psPCISpace->ui32BusNum  = ui32BusNum;
+				psPCISpace->ui32DevNum  = ui32DevNum;
+				psPCISpace->ui32FuncNum = 0;
+
+
+				for (ui32Idx=0; ui32Idx < 64; ui32Idx++)
+				{
+					psPCISpace->u.aui32PCISpace[ui32Idx] = OSPCIReadDword(ui32BusNum, ui32DevNum, 0, ui32Idx*4);
+
+					if (ui32Idx < 16)
+					{
+						PVR_DPF((PVR_DBG_VERBOSE,"%08X\n",psPCISpace->u.aui32PCISpace[ui32Idx]));
+					}
+				}
+
+				for (ui32BarIndex = 0; ui32BarIndex < 6; ui32BarIndex++)
+				{
+					GetPCIMemSpaceSize (ui32BusNum, ui32DevNum, ui32BarIndex, &psPCISpace->aui32PCIMemSpaceSize[ui32BarIndex]);
+				}
+				return PVRSRV_OK;
+			}
+
+		}
+
+	}
+
+	PVR_DPF((PVR_DBG_ERROR,"Couldn't find PCI device"));
+
+	return PVRSRV_ERROR_GENERIC;
+}
+
+static IMG_UINT32 GetPCIMemSpaceSize (IMG_UINT32 ui32BusNum, IMG_UINT32 ui32DevNum, IMG_UINT32 ui32BarIndex, IMG_UINT32* pui32PCIMemSpaceSize)
+{
+
+	IMG_UINT32	ui32AddressRange;
+	IMG_UINT32	ui32BarSave;
+
+	ui32BarSave = OSPCIReadDword (ui32BusNum, ui32DevNum, 0, ((4 + ui32BarIndex) * sizeof (IMG_UINT32)));
+
+	OSPCIWriteDword (ui32BusNum, ui32DevNum, 0, ((4 + ui32BarIndex) * sizeof (IMG_UINT32)), 0xFFFFFFFF);
+
+	ui32AddressRange = OSPCIReadDword (ui32BusNum, ui32DevNum, 0, ((4 + ui32BarIndex) * sizeof (IMG_UINT32)));
+
+	OSPCIWriteDword (ui32BusNum, ui32DevNum, 0, ((4 + ui32BarIndex) * sizeof (IMG_UINT32)), ui32BarSave);
+
+	*pui32PCIMemSpaceSize = (~(ui32AddressRange & 0xFFFFFFF0)) + 1;
+	return PVRSRV_OK;
+}
+#endif
+
+
+#ifdef __linux__
+#define VERSION_STR_MAX_LEN_TEMPLATE "SGX revision = 000.000.000"
+static PVRSRV_ERROR SysCreateVersionString(SYS_DATA *psSysData)
+{
+    IMG_UINT32 ui32SGXRevision = 0;
+    IMG_UINT32 ui32MaxStrLen;
+    PVRSRV_ERROR eError;
+    IMG_INT32 i32Count;
+    IMG_CHAR *pszVersionString;
+
+#if !defined(NO_HARDWARE)
+
+    {
+	IMG_VOID *pvSGXRegs;
+
+	pvSGXRegs = OSMapPhysToLin(gsSGXDeviceMap.sRegsCpuPBase,
+											 gsSGXDeviceMap.ui32RegsSize,
+											 PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+											 IMG_NULL);
+
+	if (pvSGXRegs != IMG_NULL)
+	{
+            ui32SGXRevision = OSReadHWReg(pvSGXRegs, EUR_CR_CORE_REVISION);
+
+	     OSUnMapPhysToLin(pvSGXRegs,
+		   									 	gsSGXDeviceMap.ui32RegsSize,
+											 	PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+												IMG_NULL);
+	}
+	else
+	{
+	     PVR_DPF((PVR_DBG_ERROR,"SysCreateVersionString: Couldn't map SGX registers"));
+	}
+    }
+#endif
+
+    ui32MaxStrLen = OSStringLength(VERSION_STR_MAX_LEN_TEMPLATE);
+    eError = OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+                          ui32MaxStrLen + 1,
+                          (IMG_PVOID *)&pszVersionString,
+                          IMG_NULL,
+						  "Version String");
+    if(eError != PVRSRV_OK)
+    {
+		return PVRSRV_ERROR_GENERIC;
+    }
+
+    i32Count = OSSNPrintf(pszVersionString, ui32MaxStrLen + 1,
+                           "SGX revision = %u.%u.%u",
+                           (IMG_UINT)((ui32SGXRevision & EUR_CR_CORE_REVISION_MAJOR_MASK)
+                            >> EUR_CR_CORE_REVISION_MAJOR_SHIFT),
+                           (IMG_UINT)((ui32SGXRevision & EUR_CR_CORE_REVISION_MINOR_MASK)
+                            >> EUR_CR_CORE_REVISION_MINOR_SHIFT),
+                           (IMG_UINT)((ui32SGXRevision & EUR_CR_CORE_REVISION_MAINTENANCE_MASK)
+                            >> EUR_CR_CORE_REVISION_MAINTENANCE_SHIFT)
+                           );
+    if(i32Count == -1)
+    {
+        ui32MaxStrLen = OSStringLength(VERSION_STR_MAX_LEN_TEMPLATE);
+        OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+                    ui32MaxStrLen + 1,
+                    pszVersionString,
+                    IMG_NULL);
+
+		return PVRSRV_ERROR_GENERIC;
+    }
+
+    psSysData->pszVersionString = pszVersionString;
+
+    return PVRSRV_OK;
+}
+
+static IMG_VOID SysFreeVersionString(SYS_DATA *psSysData)
+{
+    if(psSysData->pszVersionString)
+    {
+        IMG_UINT32 ui32MaxStrLen;
+        ui32MaxStrLen = OSStringLength(VERSION_STR_MAX_LEN_TEMPLATE);
+        OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
+                    ui32MaxStrLen+1,
+                    psSysData->pszVersionString,
+                    IMG_NULL);
+		psSysData->pszVersionString = NULL;
+    }
+}
+#endif
+PVRSRV_ERROR SysInitialise(IMG_VOID)
+{
+	IMG_UINT32			i;
+	PVRSRV_ERROR 		eError;
+	PVRSRV_DEVICE_NODE	*psDeviceNode;
+	SGX_TIMING_INFORMATION*	psTimingInfo;
+
+	PVR_DPF((PVR_DBG_MESSAGE,"SysInitialise"));
+
+	SysPlatformDetect();
+
+	gpsSysData = &gsSysData;
+	OSMemSet(gpsSysData, 0, sizeof(SYS_DATA));
+
+	gpsSysData->pvSysSpecificData = &gsSysSpecificData;
+	gsSysSpecificData.ui32SysSpecificData = 0;
+#ifdef	LDM_PCI
+
+	PVR_ASSERT(gpsPVRLDMDev != IMG_NULL);
+	gsSysSpecificData.psPCIDev = gpsPVRLDMDev;
+#endif
+
+	eError = OSInitEnvData(&gpsSysData->pvEnvSpecificData);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to setup env structure"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+
+
+	psTimingInfo = &gsSGXDeviceMap.sTimingInfo;
+	psTimingInfo->ui32CoreClockSpeed = SYS_SGX_CLOCK_SPEED;
+	psTimingInfo->ui32HWRecoveryFreq = SYS_SGX_HWRECOVERY_TIMEOUT_FREQ;
+#if defined(SUPPORT_ACTIVE_POWER_MANAGEMENT)
+	psTimingInfo->bEnableActivePM = IMG_TRUE;
+#else
+	psTimingInfo->bEnableActivePM = IMG_FALSE;
+#endif
+	psTimingInfo->ui32ActivePowManLatencyms = SYS_SGX_ACTIVE_POWER_LATENCY_MS;
+	psTimingInfo->ui32uKernelFreq = SYS_SGX_PDS_TIMER_FREQ;
+
+#ifdef __linux__
+	eError = PCIInitDev(gpsSysData);
+	if (eError != PVRSRV_OK)
+	{
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+#endif
+
+	gpsSysData->ui32NumDevices = SYS_DEVICE_COUNT;
+
+
+	for(i=0; i<SYS_DEVICE_COUNT; i++)
+	{
+		gpsSysData->sDeviceID[i].uiID = i;
+		gpsSysData->sDeviceID[i].bInUse = IMG_FALSE;
+	}
+
+	gpsSysData->psDeviceNodeList = IMG_NULL;
+	gpsSysData->psQueueList = IMG_NULL;
+
+	eError = SysInitialiseCommon(gpsSysData);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed in SysInitialiseCommon"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+
+
+	eError = SysLocateDevices(gpsSysData);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to locate devices"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+
+
+	eError = PVRSRVRegisterDevice(gpsSysData, SGXRegisterDevice,
+								  DEVICE_SGX_INTERRUPT, &gui32SGXDeviceID);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to register device!"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+
+//#ifdef SUPPORT_MSVDX
+	eError = PVRSRVRegisterDevice(gpsSysData, MSVDXRegisterDevice,
+								  DEVICE_MSVDX_INTERRUPT, &gui32MSVDXDeviceID);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to register device!"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+//#endif
+
+
+	psDeviceNode = gpsSysData->psDeviceNodeList;
+
+	while(psDeviceNode)
+	{
+
+		switch(psDeviceNode->sDevId.eDeviceType)
+		{
+			case PVRSRV_DEVICE_TYPE_SGX:
+			{
+				DEVICE_MEMORY_INFO *psDevMemoryInfo;
+				DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+
+
+				psDeviceNode->psLocalDevMemArena = IMG_NULL;
+
+
+				psDevMemoryInfo = &psDeviceNode->sDevMemoryInfo;
+				psDeviceMemoryHeap = psDevMemoryInfo->psDeviceMemoryHeap;
+
+
+				for(i=0; i<psDevMemoryInfo->ui32HeapCount; i++)
+				{
+					psDeviceMemoryHeap[i].ui32Attribs |= PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG;
+#ifdef OEM_CUSTOMISE
+
+#endif
+				}
+
+				break;
+			}
+
+//#ifdef SUPPORT_MSVDX
+			case PVRSRV_DEVICE_TYPE_MSVDX:
+			{
+				DEVICE_MEMORY_INFO *psDevMemoryInfo;
+				DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;
+
+
+				psDeviceNode->psLocalDevMemArena = IMG_NULL;
+
+
+				psDevMemoryInfo = &psDeviceNode->sDevMemoryInfo;
+				psDeviceMemoryHeap = psDevMemoryInfo->psDeviceMemoryHeap;
+
+
+				for(i=0; i<psDevMemoryInfo->ui32HeapCount; i++)
+				{
+					psDeviceMemoryHeap[i].ui32Attribs |= PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG;
+#ifdef OEM_CUSTOMISE
+
+#endif
+				}
+				break;
+			}
+//#endif
+			default:
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to find SGX device node!"));
+				return PVRSRV_ERROR_INIT_FAILURE;
+			}
+		}
+
+
+		psDeviceNode = psDeviceNode->psNext;
+	}
+
+	PDUMPINIT();
+	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_PDUMP_INIT);
+
+
+	eError = PVRSRVInitialiseDevice (gui32SGXDeviceID);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to initialise device!"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_SGX_INITIALISED);
+
+//#ifdef SUPPORT_MSVDX
+	eError = PVRSRVInitialiseDevice (gui32MSVDXDeviceID);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to initialise device!"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_MSVDX_INITIALISED);
+//#endif
+
+	return PVRSRV_OK;
+}
+
+
+static IMG_VOID SysEnableInterrupts(SYS_DATA *psSysData)
+{
+#if !defined(NO_HARDWARE)
+	IMG_UINT32 ui32RegData;
+	IMG_UINT32 ui32Mask;
+
+	ui32Mask = THALIA_MASK | MSVDX_MASK;
+
+	ui32RegData = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_IDENTITY_REG);
+	OSWriteHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_IDENTITY_REG, ui32RegData | ui32Mask);
+
+
+	ui32RegData = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_MASK_REG);
+	OSWriteHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_MASK_REG, ui32RegData & (~ui32Mask));
+
+
+	ui32RegData = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_ENABLE_REG);
+	OSWriteHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_ENABLE_REG, ui32RegData | ui32Mask);
+
+	PVR_TRACE(("SysEnableInterrupts: Interrupts enabled"));
+#endif
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+}
+
+static IMG_VOID SysDisableInterrupts(SYS_DATA *psSysData)
+{
+#if !defined(NO_HARDWARE)
+	IMG_UINT32 ui32RegData;
+	IMG_UINT32 ui32Mask;
+
+
+	ui32Mask = THALIA_MASK | MSVDX_MASK;
+
+	ui32RegData = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_ENABLE_REG);
+	OSWriteHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_ENABLE_REG, ui32RegData & (~ui32Mask));
+
+
+	ui32RegData = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_MASK_REG);
+	OSWriteHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_MASK_REG, ui32RegData | ui32Mask);
+
+	PVR_TRACE(("SysDisableInterrupts: Interrupts disabled"));
+#endif
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+}
+
+IMG_VOID SysReEnableInterrupts(IMG_VOID)
+{
+	SysEnableInterrupts(gpsSysData);
+}
+
+PVRSRV_ERROR SysFinalise(IMG_VOID)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+#if defined(SYS_USING_INTERRUPTS)
+	eError = OSInstallMISR(gpsSysData);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysFinalise: OSInstallMISR failed"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_MISR_INSTALLED);
+
+	eError = OSInstallSystemLISR(gpsSysData, gsSGXDeviceMap.ui32IRQ);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysFinalise: OSInstallSystemLISR failed"));
+		SysDeinitialise(gpsSysData);
+		gpsSysData = IMG_NULL;
+		return eError;
+	}
+	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_LISR_INSTALLED);
+#endif
+
+	SysEnableInterrupts(gpsSysData);
+	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_IRQ_ENABLED);
+
+#ifdef	__linux__
+
+	eError = SysCreateVersionString(gpsSysData);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysInitialise: Failed to create a system version string"));
+	}
+#endif
+
+	return eError;
+}
+
+PVRSRV_ERROR SysDeinitialise (SYS_DATA *psSysData)
+{
+	PVRSRV_ERROR eError;
+
+	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;
+
+	if (SYS_SPECIFIC_DATA_TEST(&gsSysSpecificData, SYS_SPECIFIC_DATA_IRQ_ENABLED))
+	{
+		SysDisableInterrupts(psSysData);
+	}
+
+#if defined(SYS_USING_INTERRUPTS)
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_LISR_INSTALLED))
+	{
+		eError = OSUninstallSystemLISR(psSysData);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SysDeinitialise: OSUninstallSystemLISR failed"));
+			return eError;
+		}
+	}
+
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_MISR_INSTALLED))
+	{
+		eError = OSUninstallMISR(psSysData);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SysDeinitialise: OSUninstallMISR failed"));
+			return eError;
+		}
+	}
+#endif
+/* Commenting this out to clean up allocation made in SysInitialise */
+/*#if defined(SUPPORT_MSVDX)*/
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_MSVDX_INITIALISED))
+	{
+
+		eError = PVRSRVDeinitialiseDevice(gui32MSVDXDeviceID);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SysDeinitialise: failed to de-init the device"));
+			return eError;
+		}
+	}
+/*#endif*/
+
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_SGX_INITIALISED))
+	{
+
+		eError = PVRSRVDeinitialiseDevice(gui32SGXDeviceID);
+		if (eError != PVRSRV_OK)
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SysDeinitialise: failed to de-init the device"));
+			return eError;
+		}
+	}
+
+#ifdef __linux__
+	SysFreeVersionString(psSysData);
+
+	PCIDeInitDev(psSysData);
+#endif
+
+	eError = OSDeInitEnvData(psSysData->pvEnvSpecificData);
+	if (eError != PVRSRV_OK)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"SysDeinitialise: failed to de-init env structure"));
+		return eError;
+	}
+
+	SysDeinitialiseCommon(gpsSysData);
+
+#if defined(NO_HARDWARE)
+#ifdef SUPPORT_MSVDX
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_ALLOC_DUMMY_MSVDX_REGS))
+	{
+		OSBaseFreeContigMemory(MSVDX_REG_SIZE, gsMSVDXRegsCPUVAddr, gsMSVDXDeviceMap.sRegsCpuPBase);
+	}
+#endif
+
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_ALLOC_DUMMY_SGX_REGS))
+	{
+		OSBaseFreeContigMemory(SGX_REG_SIZE, gsSGXRegsCPUVAddr, gsSGXDeviceMap.sRegsCpuPBase);
+	}
+#endif
+
+#if !defined(NO_HARDWARE)
+
+	OSUnMapPhysToLin(gsPoulsboRegsCPUVaddr,
+											 REG_SIZE,
+											 PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+											 IMG_NULL);
+
+#if defined(MAP_UNUSED_MAPPINGS)
+	OSUnMapPhysToLin(gsPoulsboDisplayRegsCPUVaddr,
+											 DISPLAY_REG_SIZE,
+											 PVRSRV_HAP_KERNEL_ONLY|PVRSRV_HAP_UNCACHED,
+											 IMG_NULL);
+#endif
+
+#endif
+	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_PDUMP_INIT))
+	{
+		PDUMPDEINIT();
+	}
+
+	gpsSysData = IMG_NULL;
+
+	return PVRSRV_OK;
+}
+
+
+IMG_UINT32 SysGetInterruptSource(SYS_DATA* psSysData,
+								 PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+#if !defined(NO_HARDWARE)
+	IMG_UINT32 ui32Devices = 0;
+	IMG_UINT32 ui32Data, ui32DIMMask;
+
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+	PVR_UNREFERENCED_PARAMETER(psDeviceNode);
+
+
+	ui32Data = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_IDENTITY_REG);
+
+	if (ui32Data & THALIA_MASK)
+	{
+		ui32Devices |= DEVICE_SGX_INTERRUPT;
+	}
+
+	if (ui32Data & MSVDX_MASK)
+	{
+		ui32Devices |= DEVICE_MSVDX_INTERRUPT;
+	}
+
+
+	ui32DIMMask = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_ENABLE_REG);
+	ui32DIMMask &= ~(THALIA_MASK | MSVDX_MASK);
+
+
+	if (ui32Data & ui32DIMMask)
+	{
+		ui32Devices |= DEVICE_DISP_INTERRUPT;
+	}
+
+	return (ui32Devices);
+#else
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+	PVR_UNREFERENCED_PARAMETER(psDeviceNode);
+
+	return 0;
+#endif
+}
+
+IMG_VOID SysClearInterrupts(SYS_DATA* psSysData, IMG_UINT32 ui32ClearBits)
+{
+#if !defined(NO_HARDWARE)
+	IMG_UINT32 ui32Data;
+	IMG_UINT32 ui32Mask = 0;
+
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+
+	ui32Data = OSReadHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_STATUS_REG);
+
+	if ((ui32ClearBits & DEVICE_SGX_INTERRUPT) &&
+		((ui32Data & THALIA_MASK) == 0))
+	{
+		ui32Mask |= THALIA_MASK;
+	}
+
+	if ((ui32ClearBits & DEVICE_MSVDX_INTERRUPT) &&
+		((ui32Data & MSVDX_MASK) == 0))
+	{
+		ui32Mask |= MSVDX_MASK;
+	}
+
+	if (ui32Mask)
+	{
+		OSWriteHWReg(gsPoulsboRegsCPUVaddr, INTERRUPT_IDENTITY_REG, ui32Mask);
+	}
+#else
+	PVR_UNREFERENCED_PARAMETER(psSysData);
+	PVR_UNREFERENCED_PARAMETER(ui32ClearBits);
+#endif
+}
+
+
+
+PVRSRV_ERROR SysGetDeviceMemoryMap(PVRSRV_DEVICE_TYPE eDeviceType,
+									IMG_VOID **ppvDeviceMap)
+{
+
+	switch(eDeviceType)
+	{
+		case PVRSRV_DEVICE_TYPE_SGX:
+		{
+
+			*ppvDeviceMap = (IMG_VOID*)&gsSGXDeviceMap;
+			break;
+		}
+#ifdef SUPPORT_MSVDX
+		case PVRSRV_DEVICE_TYPE_MSVDX:
+		{
+
+			*ppvDeviceMap = (IMG_VOID*)&gsMSVDXDeviceMap;
+			break;
+		}
+#endif
+		default:
+		{
+			PVR_DPF((PVR_DBG_ERROR,"SysGetDeviceMemoryMap: unsupported device type"));
+		}
+	}
+	return PVRSRV_OK;
+}
+
+
+IMG_DEV_PHYADDR SysCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE eDeviceType,
+										IMG_CPU_PHYADDR CpuPAddr)
+{
+	IMG_DEV_PHYADDR DevPAddr;
+
+	PVR_UNREFERENCED_PARAMETER(eDeviceType);
+
+
+	DevPAddr.uiAddr = CpuPAddr.uiAddr;
+
+	return DevPAddr;
+}
+
+
+IMG_CPU_PHYADDR SysSysPAddrToCpuPAddr (IMG_SYS_PHYADDR sys_paddr)
+{
+	IMG_CPU_PHYADDR cpu_paddr;
+
+
+	cpu_paddr.uiAddr = sys_paddr.uiAddr;
+	return cpu_paddr;
+}
+
+IMG_SYS_PHYADDR SysCpuPAddrToSysPAddr (IMG_CPU_PHYADDR cpu_paddr)
+{
+	IMG_SYS_PHYADDR sys_paddr;
+
+
+	sys_paddr.uiAddr = cpu_paddr.uiAddr;
+	return sys_paddr;
+}
+
+
+IMG_DEV_PHYADDR SysSysPAddrToDevPAddr (PVRSRV_DEVICE_TYPE eDeviceType, IMG_SYS_PHYADDR SysPAddr)
+{
+    IMG_DEV_PHYADDR DevPAddr;
+
+	PVR_UNREFERENCED_PARAMETER(eDeviceType);
+
+
+    DevPAddr.uiAddr = SysPAddr.uiAddr;
+
+    return DevPAddr;
+}
+
+
+IMG_SYS_PHYADDR SysDevPAddrToSysPAddr (PVRSRV_DEVICE_TYPE eDeviceType, IMG_DEV_PHYADDR DevPAddr)
+{
+    IMG_SYS_PHYADDR SysPAddr;
+
+	PVR_UNREFERENCED_PARAMETER(eDeviceType);
+
+
+    SysPAddr.uiAddr = DevPAddr.uiAddr;
+
+    return SysPAddr;
+}
+
+
+IMG_VOID SysRegisterExternalDevice(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	PVR_UNREFERENCED_PARAMETER(psDeviceNode);
+}
+
+
+IMG_VOID SysRemoveExternalDevice(PVRSRV_DEVICE_NODE *psDeviceNode)
+{
+	PVR_UNREFERENCED_PARAMETER(psDeviceNode);
+}
+
+PVRSRV_ERROR SysOEMFunction (	IMG_UINT32	ui32ID,
+								IMG_VOID	*pvIn,
+								IMG_UINT32  ulInSize,
+								IMG_VOID	*pvOut,
+								IMG_UINT32	ulOutSize)
+{
+	if (ulInSize || pvIn);
+
+	if ((ui32ID == OEM_GET_EXT_FUNCS) &&
+		(ulOutSize == sizeof(PVRSRV_DC_OEM_JTABLE)))
+	{
+		PVRSRV_DC_OEM_JTABLE *psOEMJTable = (PVRSRV_DC_OEM_JTABLE*)pvOut;
+		psOEMJTable->pfnOEMBridgeDispatch = &PVRSRV_BridgeDispatchKM;
+#ifdef	__linux__
+		psOEMJTable->pfnOEMReadRegistryString  = IMG_NULL;
+		psOEMJTable->pfnOEMWriteRegistryString = IMG_NULL;
+#else
+		psOEMJTable->pfnOEMReadRegistryString  = IMG_NULL;
+		psOEMJTable->pfnOEMWriteRegistryString = IMG_NULL;
+#endif
+		return PVRSRV_OK;
+	}
+
+	return PVRSRV_ERROR_INVALID_PARAMS;
+}
+
+
+
+PVRSRV_ERROR SysSystemPrePowerState(PVRSRV_SYS_POWER_STATE eNewPowerState)
+{
+	PVRSRV_ERROR eError= PVRSRV_OK;
+
+	if (eNewPowerState != gpsSysData->eCurrentPowerState)
+	{
+		if ((eNewPowerState == PVRSRV_SYS_POWER_STATE_D3) &&
+			(gpsSysData->eCurrentPowerState < PVRSRV_SYS_POWER_STATE_D3))
+		{
+
+			if (SYS_SPECIFIC_DATA_TEST(&gsSysSpecificData, SYS_SPECIFIC_DATA_IRQ_ENABLED))
+			{
+				SysDisableInterrupts(gpsSysData);
+
+				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_PM_IRQ_DISABLE);
+				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData, SYS_SPECIFIC_DATA_IRQ_ENABLED);
+			}
+
+#if defined(SYS_USING_INTERRUPTS)
+			if (SYS_SPECIFIC_DATA_TEST(&gsSysSpecificData, SYS_SPECIFIC_DATA_LISR_INSTALLED))
+			{
+				eError = OSUninstallSystemLISR(gpsSysData);
+				if (eError != PVRSRV_OK)
+				{
+					PVR_DPF((PVR_DBG_ERROR,"SysSystemPrePowerState: OSUninstallSystemLISR failed (%d)", eError));
+				}
+				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR);
+				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData, SYS_SPECIFIC_DATA_LISR_INSTALLED);
+			}
+#endif
+
+
+#ifdef	__linux__
+			eError = OSPCISuspendDev(gsSysSpecificData.hSGXPCI);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SysSystemPrePowerState: OSPCISuspendDev failed (%d)", eError));
+			}
+#endif
+		}
+	}
+
+	return eError;
+}
+
+PVRSRV_ERROR SysSystemPostPowerState(PVRSRV_SYS_POWER_STATE eNewPowerState)
+{
+	PVRSRV_ERROR eError = PVRSRV_OK;
+
+	if (eNewPowerState != gpsSysData->eCurrentPowerState)
+	{
+		if ((gpsSysData->eCurrentPowerState == PVRSRV_SYS_POWER_STATE_D3) &&
+			(eNewPowerState < PVRSRV_SYS_POWER_STATE_D3))
+		{
+#ifdef	__linux__
+			eError = OSPCIResumeDev(gsSysSpecificData.hSGXPCI);
+			if (eError != PVRSRV_OK)
+			{
+				PVR_DPF((PVR_DBG_ERROR,"SysSystemPostPowerState: OSPCIResumeDev failed (%d)", eError));
+			}
+#endif
+
+
+#if defined(SYS_USING_INTERRUPTS)
+			if (SYS_SPECIFIC_DATA_TEST(&gsSysSpecificData, SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR))
+			{
+				eError = OSInstallSystemLISR(gpsSysData, gsSGXDeviceMap.ui32IRQ);
+				if (eError != PVRSRV_OK)
+				{
+					PVR_DPF((PVR_DBG_ERROR,"SysSystemPostPowerState: OSInstallSystemLISR failed to install ISR (%d)", eError));
+				}
+				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_LISR_INSTALLED);
+				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData, SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR);
+			}
+#endif
+
+			if (SYS_SPECIFIC_DATA_TEST(&gsSysSpecificData, SYS_SPECIFIC_DATA_PM_IRQ_DISABLE))
+			{
+				SysEnableInterrupts(gpsSysData);
+
+				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_IRQ_ENABLED);
+				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData, SYS_SPECIFIC_DATA_PM_IRQ_DISABLE);
+			}
+		}
+	}
+	return eError;
+}
+
+
+PVRSRV_ERROR SysDevicePrePowerState(IMG_UINT32				ui32DeviceIndex,
+									PVRSRV_DEV_POWER_STATE	eNewPowerState,
+									PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	if (ui32DeviceIndex == gui32SGXDeviceID)
+	{
+		if ((eNewPowerState != eCurrentPowerState) &&
+			(eNewPowerState == PVRSRV_DEV_POWER_STATE_OFF))
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,"SysDevicePrePowerState: Remove SGX power"));
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+
+PVRSRV_ERROR SysDevicePostPowerState(IMG_UINT32				ui32DeviceIndex,
+									 PVRSRV_DEV_POWER_STATE	eNewPowerState,
+									 PVRSRV_DEV_POWER_STATE	eCurrentPowerState)
+{
+	if (ui32DeviceIndex == gui32SGXDeviceID)
+	{
+		if ((eNewPowerState != eCurrentPowerState) &&
+			(eCurrentPowerState == PVRSRV_DEV_POWER_STATE_OFF))
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,"SysDevicePostPowerState: Restore SGX power"));
+		}
+	}
+
+	return PVRSRV_OK;
+}
+
+
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/common/sysutils.c b/drivers/gpu/drm/emgd/pvr/services4/system/common/sysutils.c
new file mode 100644
index 0000000..0e3e7a9
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/common/sysutils.c
@@ -0,0 +1,26 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include "services_headers.h"
+#include "sysinfo.h"
+#include "syslocal.h"
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/include/oemfuncs.h b/drivers/gpu/drm/emgd/pvr/services4/system/include/oemfuncs.h
new file mode 100644
index 0000000..343e85f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/include/oemfuncs.h
@@ -0,0 +1,68 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__OEMFUNCS_H__)
+#define __OEMFUNCS_H__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+#define OEM_EXCHANGE_POWER_STATE	(1<<0)
+#define OEM_DEVICE_MEMORY_POWER		(1<<1)
+#define OEM_DISPLAY_POWER			(1<<2)
+#define OEM_GET_EXT_FUNCS			(1<<3)
+
+typedef struct OEM_ACCESS_INFO_TAG
+{
+	IMG_UINT32		ui32Size;
+	IMG_UINT32  	ui32FBPhysBaseAddress;
+	IMG_UINT32		ui32FBMemAvailable;
+	IMG_UINT32  	ui32SysPhysBaseAddress;
+	IMG_UINT32		ui32SysSize;
+	IMG_UINT32		ui32DevIRQ;
+} OEM_ACCESS_INFO, *POEM_ACCESS_INFO;
+
+typedef IMG_UINT32   (*PFN_SRV_BRIDGEDISPATCH)( IMG_UINT32  Ioctl,
+												IMG_BYTE   *pInBuf,
+												IMG_UINT32  InBufLen,
+											    IMG_BYTE   *pOutBuf,
+												IMG_UINT32  OutBufLen,
+												IMG_UINT32 *pdwBytesTransferred);
+
+
+typedef PVRSRV_ERROR (*PFN_SRV_READREGSTRING)(PPVRSRV_REGISTRY_INFO psRegInfo);
+
+
+typedef struct PVRSRV_DC_OEM_JTABLE_TAG
+{
+	PFN_SRV_BRIDGEDISPATCH			pfnOEMBridgeDispatch;
+	PFN_SRV_READREGSTRING			pfnOEMReadRegistryString;
+	PFN_SRV_READREGSTRING			pfnOEMWriteRegistryString;
+
+} PVRSRV_DC_OEM_JTABLE;
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/include/sys_pvr_drm_shared.h b/drivers/gpu/drm/emgd/pvr/services4/system/include/sys_pvr_drm_shared.h
new file mode 100644
index 0000000..8ae67dc
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/include/sys_pvr_drm_shared.h
@@ -0,0 +1,34 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__SYS_PVR_DRM_SHARED_H__)
+#define __SYS_PVR_DRM_SHARED_H__
+
+#define	DRM_PSB_GTT_MAP     0x0F
+#define	DRM_PSB_GTT_UNMAP   0x10
+
+/* Note: the ioctl numbers used to be here, but are now defined in the common
+ * header, "emgd_shared.h", to avoid getting out-of-sync:
+ */
+#include "emgd_shared.h"
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/include/syscommon.h b/drivers/gpu/drm/emgd/pvr/services4/system/include/syscommon.h
new file mode 100644
index 0000000..d316141
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/include/syscommon.h
@@ -0,0 +1,27 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+#ifndef _SYSCOMMON_H
+#define _SYSCOMMON_H
+
+#include "sysconfig.h"
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/include/sysconfig.h b/drivers/gpu/drm/emgd/pvr/services4/system/include/sysconfig.h
new file mode 100644
index 0000000..56abae7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/include/sysconfig.h
@@ -0,0 +1,326 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SYSCONFIG_H
+#define _SYSCONFIG_H
+
+#include "sysinfo.h"
+#include "servicesint.h"
+#include "queue.h"
+#include "power.h"
+#include "resman.h"
+#include "ra.h"
+#include "device.h"
+#include "buffer_manager.h"
+
+
+#if defined(NO_HARDWARE) && defined(__linux__) && defined(__KERNEL__)
+#include <asm/io.h>
+#endif
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+typedef struct _SYS_DEVICE_ID_TAG
+{
+	IMG_UINT32	uiID;
+	IMG_BOOL	bInUse;
+
+} SYS_DEVICE_ID;
+
+
+#define SYS_MAX_LOCAL_DEVMEM_ARENAS	4
+
+typedef struct _SYS_DATA_TAG_
+{
+    IMG_UINT32				ui32NumDevices;
+	SYS_DEVICE_ID			sDeviceID[SYS_DEVICE_COUNT];
+    PVRSRV_DEVICE_NODE		*psDeviceNodeList;
+    PVRSRV_POWER_DEV		*psPowerDeviceList;
+	PVRSRV_RESOURCE			sPowerStateChangeResource;
+	PVRSRV_SYS_POWER_STATE	eCurrentPowerState;
+	PVRSRV_SYS_POWER_STATE	eFailedPowerState;
+	IMG_UINT32		 		ui32CurrentOSPowerState;
+    PVRSRV_QUEUE_INFO		*psQueueList;
+	PVRSRV_KERNEL_SYNC_INFO *psSharedSyncInfoList;
+    IMG_PVOID				pvEnvSpecificData;
+    IMG_PVOID				pvSysSpecificData;
+	PVRSRV_RESOURCE			sQProcessResource;
+	IMG_VOID				*pvSOCRegsBase;
+    IMG_HANDLE				hSOCTimerRegisterOSMemHandle;
+	IMG_UINT32				*pvSOCTimerRegisterKM;
+	IMG_VOID				*pvSOCClockGateRegsBase;
+	IMG_UINT32				ui32SOCClockGateRegsSize;
+	PFN_CMD_PROC			*ppfnCmdProcList[SYS_DEVICE_COUNT];
+
+
+	PCOMMAND_COMPLETE_DATA	*ppsCmdCompleteData[SYS_DEVICE_COUNT];
+
+	IMG_BOOL				bReProcessQueues;
+
+	RA_ARENA				*apsLocalDevMemArena[SYS_MAX_LOCAL_DEVMEM_ARENAS];
+
+    IMG_CHAR				*pszVersionString;
+	PVRSRV_EVENTOBJECT		*psGlobalEventObject;
+
+	IMG_BOOL				bFlushAll;
+
+} SYS_DATA;
+
+
+
+PVRSRV_ERROR SysInitialise(IMG_VOID);
+PVRSRV_ERROR SysFinalise(IMG_VOID);
+
+IMG_VOID SysReEnableInterrupts(IMG_VOID);
+
+PVRSRV_ERROR SysDeinitialise(SYS_DATA *psSysData);
+PVRSRV_ERROR SysGetDeviceMemoryMap(PVRSRV_DEVICE_TYPE eDeviceType,
+									IMG_VOID **ppvDeviceMap);
+
+IMG_VOID SysRegisterExternalDevice(PVRSRV_DEVICE_NODE *psDeviceNode);
+IMG_VOID SysRemoveExternalDevice(PVRSRV_DEVICE_NODE *psDeviceNode);
+
+IMG_UINT32 SysGetInterruptSource(SYS_DATA			*psSysData,
+								 PVRSRV_DEVICE_NODE *psDeviceNode);
+
+IMG_VOID SysClearInterrupts(SYS_DATA* psSysData, IMG_UINT32 ui32ClearBits);
+
+PVRSRV_ERROR SysResetDevice(IMG_UINT32 ui32DeviceIndex);
+
+PVRSRV_ERROR SysSystemPrePowerState(PVRSRV_SYS_POWER_STATE eNewPowerState);
+PVRSRV_ERROR SysSystemPostPowerState(PVRSRV_SYS_POWER_STATE eNewPowerState);
+PVRSRV_ERROR SysDevicePrePowerState(IMG_UINT32 ui32DeviceIndex,
+									PVRSRV_DEV_POWER_STATE eNewPowerState,
+									PVRSRV_DEV_POWER_STATE eCurrentPowerState);
+PVRSRV_ERROR SysDevicePostPowerState(IMG_UINT32 ui32DeviceIndex,
+									 PVRSRV_DEV_POWER_STATE eNewPowerState,
+									 PVRSRV_DEV_POWER_STATE eCurrentPowerState);
+
+#if defined(SYS_CUSTOM_POWERLOCK_WRAP)
+PVRSRV_ERROR SysPowerLockWrap(SYS_DATA *psSysData);
+IMG_VOID SysPowerLockUnwrap(SYS_DATA *psSysData);
+#endif
+PVRSRV_ERROR SysOEMFunction(		IMG_UINT32 ui32ID,
+						IMG_VOID *pvIn,
+						IMG_UINT32 ulInSize,
+						IMG_VOID *pvOut,
+						IMG_UINT32 ulOutSize);
+IMG_DEV_PHYADDR SysCpuPAddrToDevPAddr(	PVRSRV_DEVICE_TYPE eDeviceType,
+						IMG_CPU_PHYADDR cpu_paddr);
+IMG_DEV_PHYADDR SysSysPAddrToDevPAddr(	PVRSRV_DEVICE_TYPE eDeviceType,
+						IMG_SYS_PHYADDR SysPAddr);
+IMG_SYS_PHYADDR SysDevPAddrToSysPAddr(	PVRSRV_DEVICE_TYPE eDeviceType,
+						IMG_DEV_PHYADDR SysPAddr);
+IMG_CPU_PHYADDR SysSysPAddrToCpuPAddr(	IMG_SYS_PHYADDR SysPAddr);
+IMG_SYS_PHYADDR SysCpuPAddrToSysPAddr(	IMG_CPU_PHYADDR cpu_paddr);
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis  platform interface definition
+*/
+/* ----------------------------------------------------------------------------*/
+typedef struct _SYS_PLATFORM_INTERFACE {
+	char* 		sProductName;
+	IMG_UINT32	uiSgxDevDeviceID;
+	IMG_UINT32	uiSgxRegsOffset;
+	IMG_UINT32	uiMsvdxRegsOffset;
+	IMG_UINT32	uiSysSgxClockSpeed;
+	IMG_UINT32	uiSysSgxActivePowerLatencyMs;
+} SYS_PLATFORM_INTERFACE;
+
+/* #define MAP_UNUSED_MAPPINGS */
+
+#if defined(MAP_UNUSED_MAPPINGS)
+#define SGX_FEATURE_HOST_PORT
+#endif
+
+extern SYS_PLATFORM_INTERFACE *gpsSysPlatformInterface;
+
+#define VS_PRODUCT_NAME  	((gpsSysPlatformInterface) ? (gpsSysPlatformInterface->sProductName) : "Unknown Product")
+#define SYS_SGX_DEV_DEVICE_ID  	((gpsSysPlatformInterface) ? (gpsSysPlatformInterface->uiSgxDevDeviceID) : 0x0)
+#define SGX_REGS_OFFSET  	((gpsSysPlatformInterface) ? (gpsSysPlatformInterface->uiSgxRegsOffset) : 0x0)
+#ifdef SUPPORT_MSVDX
+#define MSVDX_REGS_OFFSET  	((gpsSysPlatformInterface) ? (gpsSysPlatformInterface->uiMsvdxRegsOffset) : 0x0)
+#endif
+#define SYS_SGX_CLOCK_SPEED 	((gpsSysPlatformInterface) ? (gpsSysPlatformInterface->uiSysSgxClockSpeed) : 0)
+#define SYS_SGX_ACTIVE_POWER_LATENCY_MS ((gpsSysPlatformInterface) ? (gpsSysPlatformInterface->uiSysSgxActivePowerLatencyMs) : 0)
+
+#define SYS_SGX_DEV_VENDOR_ID           0x8086
+
+#define SYS_SGX_HWRECOVERY_TIMEOUT_FREQ         (100)
+#define SYS_SGX_PDS_TIMER_FREQ                  (1000)
+
+
+#define REGS_OFFSET	0x00000
+#define REG_SIZE	0x2100
+
+#define SGX_REG_SIZE 		0x4000
+
+#ifdef SUPPORT_MSVDX
+#define	MAX_OFFSET	(MSVDX_REGS_OFFSET + MSVDX_REG_SIZE)
+#else
+#define	MAX_OFFSET	(SGX_REGS_OFFSET + SGX_REG_SIZE)
+#endif
+
+#define MMADR_INDEX			4
+
+#define DEVICE_SGX_INTERRUPT		(1UL<<0)
+#define DEVICE_MSVDX_INTERRUPT		(1UL<<1)
+#define DEVICE_DISP_INTERRUPT		(1UL<<2)
+
+#define INTERRUPT_ENABLE_REG		0x20A0
+#define INTERRUPT_IDENTITY_REG		0x20A4
+#define INTERRUPT_MASK_REG			0x20A8
+#define INTERRUPT_STATUS_REG		0x20AC
+
+#define DISP_MASK					(1UL<<17)
+#define THALIA_MASK					(1UL<<18)
+#define MSVDX_MASK					(1UL<<19)
+#define VSYNC_PIPEA_VBLANK_MASK		(1UL<<7)
+#define VSYNC_PIPEA_EVENT_MASK		(1UL<<6)
+#define VSYNC_PIPEB_VBLANK_MASK		(1UL<<5)
+#define VSYNC_PIPEB_EVENT_MASK		(1UL<<4)
+
+#define DISPLAY_REGS_OFFSET			0x70000
+#define DISPLAY_REG_SIZE			0x2000
+
+#define DISPLAY_A_CONFIG			0x00008UL
+#define DISPLAY_A_STATUS_SELECT		0x00024UL
+#define DISPLAY_B_CONFIG			0x01008UL
+#define DISPLAY_B_STATUS_SELECT		0x01024UL
+
+#define DISPLAY_PIPE_ENABLE			(1UL<<31)
+#define DISPLAY_VSYNC_STS_EN		(1UL<<25)
+#define DISPLAY_VSYNC_STS			(1UL<<9)
+
+#if defined(SGX_FEATURE_HOST_PORT)
+	#define SYS_SGX_HP_SIZE		0x8000000
+
+	#define SYS_SGX_HOSTPORT_BASE_DEVVADDR 0xD0000000
+	#if defined(FIX_HW_BRN_22997) && defined(FIX_HW_BRN_23030)
+		#define SYS_SGX_HOSTPORT_BRN23030_OFFSET 0x7C00000
+	#endif
+#endif
+
+
+typedef struct
+{
+	union
+	{
+#if !defined(VISTA)
+		IMG_UINT8	aui8PCISpace[256];
+		IMG_UINT16	aui16PCISpace[128];
+		IMG_UINT32	aui32PCISpace[64];
+#endif
+		struct
+		{
+			IMG_UINT16	ui16VenID;
+			IMG_UINT16	ui16DevID;
+			IMG_UINT16	ui16PCICmd;
+			IMG_UINT16	ui16PCIStatus;
+		}s;
+	}u;
+} PCICONFIG_SPACE, *PPCICONFIG_SPACE;
+
+
+extern SYS_DATA* gpsSysData;
+
+PVRSRV_ERROR SysResetDevice(IMG_UINT32 ui32DeviceIndex);
+
+#if defined(SYS_CUSTOM_POWERLOCK_WRAP)
+PVRSRV_ERROR SysPowerLockWrap(SYS_DATA *psSysData);
+IMG_VOID SysPowerLockUnwrap(SYS_DATA *psSysData);
+#endif
+
+
+#if defined(PVR_LMA)
+IMG_BOOL SysVerifyCpuPAddrToDevPAddr (PVRSRV_DEVICE_TYPE eDeviceType, IMG_CPU_PHYADDR CpuPAddr);
+IMG_BOOL SysVerifySysPAddrToDevPAddr (PVRSRV_DEVICE_TYPE eDeviceType, IMG_SYS_PHYADDR SysPAddr);
+#endif
+
+extern SYS_DATA* gpsSysData;
+
+#if !defined(USE_CODE)
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(SysAcquireData)
+#endif
+static INLINE PVRSRV_ERROR SysAcquireData(SYS_DATA **ppsSysData)
+{
+
+	*ppsSysData = gpsSysData;
+
+	if (!gpsSysData)
+	{
+		return PVRSRV_ERROR_GENERIC;
+	}
+
+	return PVRSRV_OK;
+}
+
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(SysInitialiseCommon)
+#endif
+static INLINE PVRSRV_ERROR SysInitialiseCommon(SYS_DATA *psSysData)
+{
+	PVRSRV_ERROR	eError;
+	eError = PVRSRVInit(psSysData);
+
+	return eError;
+}
+
+#ifdef INLINE_IS_PRAGMA
+#pragma inline(SysDeinitialiseCommon)
+#endif
+static INLINE IMG_VOID SysDeinitialiseCommon(SYS_DATA *psSysData)
+{
+	PVRSRVDeInit(psSysData);
+
+	OSDestroyResource(&psSysData->sPowerStateChangeResource);
+}
+#endif
+
+
+#if !(defined(NO_HARDWARE) && defined(__linux__) && defined(__KERNEL__))
+#define	SysReadHWReg(p, o) OSReadHWReg(p, o)
+#define SysWriteHWReg(p, o, v) OSWriteHWReg(p, o, v)
+#else
+static inline IMG_UINT32 SysReadHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset)
+{
+	return (IMG_UINT32) readl(pvLinRegBaseAddr + ui32Offset);
+}
+
+static inline IMG_VOID SysWriteHWReg(IMG_PVOID pvLinRegBaseAddr, IMG_UINT32 ui32Offset, IMG_UINT32 ui32Value)
+{
+	writel(ui32Value, pvLinRegBaseAddr + ui32Offset);
+}
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/include/sysinfo.h b/drivers/gpu/drm/emgd/pvr/services4/system/include/sysinfo.h
new file mode 100644
index 0000000..95724d0
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/include/sysinfo.h
@@ -0,0 +1,42 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__SYSINFO_H__)
+#define __SYSINFO_H__
+
+#define MAX_HW_TIME_US				(500000)
+#define WAIT_TRY_COUNT				(10000)
+
+typedef enum _SYS_DEVICE_TYPE_
+{
+	SYS_DEVICE_SGX						= 0,
+
+	SYS_DEVICE_FORCE_I16 				= 0x7fff
+
+} SYS_DEVICE_TYPE;
+#if 0
+#define SYS_DEVICE_COUNT 5
+#endif
+
+#define SYS_DEVICE_COUNT 11
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/include/syslocal.h b/drivers/gpu/drm/emgd/pvr/services4/system/include/syslocal.h
new file mode 100644
index 0000000..59d80da
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/include/syslocal.h
@@ -0,0 +1,80 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#if !defined(__SYSLOCAL_H__)
+#define __SYSLOCAL_H__
+
+#define SYS_SPECIFIC_DATA_PCI_ACQUIRE_DEV		0x00000001UL
+#define SYS_SPECIFIC_DATA_PCI_REQUEST_SGX_ADDR_RANGE	0x00000002UL
+#define SYS_SPECIFIC_DATA_PCI_REQUEST_HOST_PORT_RANGE	0x00000004UL
+#if defined(NO_HARDWARE)
+#define SYS_SPECIFIC_DATA_ALLOC_DUMMY_SGX_REGS		0x00000008UL
+#if defined(SUPPORT_MSVDX)
+#define	SYS_SPECIFIC_DATA_ALLOC_DUMMY_MSVDX_REGS	0x00000020UL
+#endif
+#endif
+#define	SYS_SPECIFIC_DATA_SGX_INITIALISED		0x00000040UL
+//#if defined(SUPPORT_MSVDX)
+#define	SYS_SPECIFIC_DATA_MSVDX_INITIALISED		0x00000080UL
+//#endif
+#define	SYS_SPECIFIC_DATA_MISR_INSTALLED		0x00000100UL
+#define	SYS_SPECIFIC_DATA_LISR_INSTALLED		0x00000200UL
+#define	SYS_SPECIFIC_DATA_PDUMP_INIT			0x00000400UL
+#define	SYS_SPECIFIC_DATA_IRQ_ENABLED			0x00000800UL
+
+#define	SYS_SPECIFIC_DATA_PM_UNMAP_SGX_REGS		0x00001000UL
+#define	SYS_SPECIFIC_DATA_PM_UNMAP_SGX_HP		0x00004000UL
+#define	SYS_SPECIFIC_DATA_PM_UNMAP_MSVDX_REGS		0x00008000UL
+#define	SYS_SPECIFIC_DATA_PM_IRQ_DISABLE		0x00010000UL
+#define	SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR		0x00020000UL
+
+#define	SYS_SPECIFIC_DATA_SET(psSysSpecData, flag) ((IMG_VOID)((psSysSpecData)->ui32SysSpecificData |= (flag)))
+
+#define	SYS_SPECIFIC_DATA_CLEAR(psSysSpecData, flag) ((IMG_VOID)((psSysSpecData)->ui32SysSpecificData &= ~(flag)))
+
+#define	SYS_SPECIFIC_DATA_TEST(psSysSpecData, flag) (((psSysSpecData)->ui32SysSpecificData & (flag)) != 0)
+
+
+typedef struct _SYS_SPECIFIC_DATA_TAG_
+{
+
+	IMG_UINT32 ui32SysSpecificData;
+#ifdef	__linux__
+	PVRSRV_PCI_DEV_HANDLE hSGXPCI;
+#endif
+#ifdef	LDM_PCI
+	struct pci_dev *psPCIDev;
+#endif
+#ifdef	SUPPORT_DRI_DRM_EXT
+
+	IMG_UINT32 msi_addr;
+	IMG_UINT32 msi_data;
+
+	IMG_UINT32 saveBSM;
+	IMG_UINT32 saveVBT;
+#endif
+} SYS_SPECIFIC_DATA;
+
+
+#endif
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/plb/sysconfig.c b/drivers/gpu/drm/emgd/pvr/services4/system/plb/sysconfig.c
new file mode 100644
index 0000000..ec0222c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/plb/sysconfig.c
@@ -0,0 +1,48 @@
+/* -*- syscommon-c -*-
+ *-----------------------------------------------------------------------------
+ * Filename: syscommon.c
+ * $Revision: 1.4 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description: platform detection, and sharing of correct platform interface.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include "sysconfig.h"
+#include "sysplb.h"
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Poulsbo platform interface.
+*/
+/* --------------------------------------------------------------------------*/
+SYS_PLATFORM_INTERFACE gpsSysPlatformInterfacePlb = {
+        VS_PRODUCT_NAME_PLB,
+        SYS_SGX_DEV_DEVICE_ID_PLB,
+	SGX_REGS_OFFSET_PLB,
+	MSVDX_REGS_OFFSET_PLB,
+	SYS_SGX_CLOCK_SPEED_PLB,
+	SYS_SGX_ACTIVE_POWER_LATENCY_MS_PLB
+};
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/plb/sysplb.h b/drivers/gpu/drm/emgd/pvr/services4/system/plb/sysplb.h
new file mode 100644
index 0000000..0f7deff
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/plb/sysplb.h
@@ -0,0 +1,33 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SYSPLB_H
+#define _SYSPLB_H
+
+#define SYS_SGX_DEV_DEVICE_ID_PLB	0x8108
+#define VS_PRODUCT_NAME_PLB     	"SGX Poulsbo"
+#define SGX_REGS_OFFSET_PLB     	0x40000
+#define MSVDX_REGS_OFFSET_PLB   	0x50000
+#define SYS_SGX_CLOCK_SPEED_PLB                     (200000000)
+#define SYS_SGX_ACTIVE_POWER_LATENCY_MS_PLB         (1)
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/tnc/sysconfig.c b/drivers/gpu/drm/emgd/pvr/services4/system/tnc/sysconfig.c
new file mode 100644
index 0000000..09e7997
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/tnc/sysconfig.c
@@ -0,0 +1,48 @@
+/* -*- syscommon-c -*-
+ *-----------------------------------------------------------------------------
+ * Filename: syscommon.c
+ * $Revision: 1.4 $
+ *-----------------------------------------------------------------------------
+ * Copyright (c) 2002-2010, Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *-----------------------------------------------------------------------------
+ * Description: platform detection, and sharing of correct platform interface.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#include "sysconfig.h"
+#include "systnc.h"
+
+/* --------------------------------------------------------------------------*/
+/**
+* @Synopsis Poulsbo platform interface.
+*/
+/* --------------------------------------------------------------------------*/
+SYS_PLATFORM_INTERFACE gpsSysPlatformInterfaceTnc = {
+        VS_PRODUCT_NAME_TNC,
+        SYS_SGX_DEV_DEVICE_ID_TNC,
+	SGX_REGS_OFFSET_TNC,
+	MSVDX_REGS_OFFSET_TNC,
+	SYS_SGX_CLOCK_SPEED_TNC,
+	SYS_SGX_ACTIVE_POWER_LATENCY_MS_TNC
+};
+
diff --git a/drivers/gpu/drm/emgd/pvr/services4/system/tnc/systnc.h b/drivers/gpu/drm/emgd/pvr/services4/system/tnc/systnc.h
new file mode 100644
index 0000000..5793535
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/services4/system/tnc/systnc.h
@@ -0,0 +1,33 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _SYSTNC_H
+#define _SYSTNC_H
+
+#define SYS_SGX_DEV_DEVICE_ID_TNC	0x4108
+#define VS_PRODUCT_NAME_TNC     	"SGX Atom E6xx"
+#define SGX_REGS_OFFSET_TNC     	0x80000
+#define MSVDX_REGS_OFFSET_TNC   	0x90000
+#define SYS_SGX_CLOCK_SPEED_TNC                     (400000000)
+#define SYS_SGX_ACTIVE_POWER_LATENCY_MS_TNC         (50)
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/client/linuxsrv.h b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/client/linuxsrv.h
new file mode 100644
index 0000000..aa43db5
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/client/linuxsrv.h
@@ -0,0 +1,44 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _LINUXSRV_H__
+#define _LINUXSRV_H__
+
+typedef struct tagIOCTL_PACKAGE
+{
+	IMG_UINT32 ui32Cmd;
+	IMG_UINT32 ui32Size;
+	IMG_VOID 	*pInBuffer;
+	IMG_UINT32  ui32InBufferSize;
+	IMG_VOID    *pOutBuffer;
+	IMG_UINT32  ui32OutBufferSize;
+} IOCTL_PACKAGE;
+
+IMG_UINT32 DeviceIoControl(IMG_UINT32 hDevice,
+						IMG_UINT32 ui32ControlCode,
+						IMG_VOID *pInBuffer,
+						IMG_UINT32 ui32InBufferSize,
+						IMG_VOID *pOutBuffer,
+						IMG_UINT32 ui32OutBufferSize,
+						IMG_UINT32 *pui32BytesReturned);
+
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.c b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.c
new file mode 100644
index 0000000..d317957
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.c
@@ -0,0 +1,2072 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+
+#ifdef LINUX
+#include <linux/string.h>
+#endif
+
+#include "img_types.h"
+#include "pvr_debug.h"
+#include "dbgdrvif.h"
+#include "dbgdriv.h"
+#include "hotkey.h"
+#include "hostfunc.h"
+
+
+
+
+#define LAST_FRAME_BUF_SIZE	1024
+
+typedef struct _DBG_LASTFRAME_BUFFER_
+{
+	PDBG_STREAM	psStream;
+	IMG_UINT8 ui8Buffer[LAST_FRAME_BUF_SIZE];
+	IMG_UINT32		ui32BufLen;
+	struct _DBG_LASTFRAME_BUFFER_	*psNext;
+} *PDBG_LASTFRAME_BUFFER;
+
+
+static PDBG_STREAM	g_psStreamList = 0;
+static PDBG_LASTFRAME_BUFFER	g_psLFBufferList;
+
+static IMG_UINT32		g_ui32LOff = 0;
+static IMG_UINT32		g_ui32Line = 0;
+static IMG_UINT32		g_ui32MonoLines = 25;
+
+static IMG_BOOL			g_bHotkeyMiddump = IMG_FALSE;
+static IMG_UINT32		g_ui32HotkeyMiddumpStart = 0xffffffff;
+static IMG_UINT32		g_ui32HotkeyMiddumpEnd = 0xffffffff;
+
+IMG_VOID *				g_pvAPIMutex=IMG_NULL;
+
+extern IMG_UINT32		g_ui32HotKeyFrame;
+extern IMG_BOOL			g_bHotKeyPressed;
+extern IMG_BOOL			g_bHotKeyRegistered;
+
+IMG_BOOL				gbDumpThisFrame = IMG_FALSE;
+
+
+IMG_UINT32 SpaceInStream(PDBG_STREAM psStream);
+IMG_BOOL ExpandStreamBuffer(PDBG_STREAM psStream, IMG_UINT32 ui32NewSize);
+PDBG_LASTFRAME_BUFFER FindLFBuf(PDBG_STREAM psStream);
+
+DBGKM_SERVICE_TABLE g_sDBGKMServices =
+{
+	sizeof (DBGKM_SERVICE_TABLE),
+	ExtDBGDrivCreateStream,
+	ExtDBGDrivDestroyStream,
+	ExtDBGDrivFindStream,
+	ExtDBGDrivWriteString,
+	ExtDBGDrivReadString,
+	ExtDBGDrivWrite,
+	ExtDBGDrivRead,
+	ExtDBGDrivSetCaptureMode,
+	ExtDBGDrivSetOutputMode,
+	ExtDBGDrivSetDebugLevel,
+	ExtDBGDrivSetFrame,
+	ExtDBGDrivGetFrame,
+	ExtDBGDrivOverrideMode,
+	ExtDBGDrivDefaultMode,
+	ExtDBGDrivWrite2,
+	ExtDBGDrivWriteStringCM,
+	ExtDBGDrivWriteCM,
+	ExtDBGDrivSetMarker,
+	ExtDBGDrivGetMarker,
+	ExtDBGDrivStartInitPhase,
+	ExtDBGDrivStopInitPhase,
+	ExtDBGDrivIsCaptureFrame,
+	ExtDBGDrivWriteLF,
+	ExtDBGDrivReadLF,
+	ExtDBGDrivGetStreamOffset,
+	ExtDBGDrivSetStreamOffset,
+	ExtDBGDrivIsLastCaptureFrame,
+	ExtDBGDrivWaitForEvent
+};
+
+
+
+
+
+IMG_VOID * IMG_CALLCONV ExtDBGDrivCreateStream(IMG_CHAR *	pszName, IMG_UINT32 ui32CapMode, IMG_UINT32	ui32OutMode, IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size)
+{
+	IMG_VOID *	pvRet;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	pvRet=DBGDrivCreateStream(pszName, ui32CapMode, ui32OutMode, ui32Flags, ui32Size);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return pvRet;
+}
+
+void IMG_CALLCONV ExtDBGDrivDestroyStream(PDBG_STREAM psStream)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivDestroyStream(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+IMG_VOID * IMG_CALLCONV ExtDBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream)
+{
+	IMG_VOID *	pvRet;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	pvRet=DBGDrivFindStream(pszName, bResetStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return pvRet;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivWriteString(psStream, pszString, ui32Level);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivReadString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Limit)
+{
+	IMG_UINT32 ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivReadString(psStream, pszString, ui32Limit);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWrite(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivWrite(psStream, pui8InBuf, ui32InBuffSize, ui32Level);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivRead(PDBG_STREAM psStream, IMG_BOOL bReadInitBuffer, IMG_UINT32 ui32OutBuffSize,IMG_UINT8 * pui8OutBuf)
+{
+	IMG_UINT32 ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivRead(psStream, bReadInitBuffer, ui32OutBuffSize, pui8OutBuf);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+void IMG_CALLCONV ExtDBGDrivSetCaptureMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode,IMG_UINT32 ui32Start,IMG_UINT32 ui32End,IMG_UINT32 ui32SampleRate)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivSetCaptureMode(psStream, ui32Mode, ui32Start, ui32End, ui32SampleRate);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+void IMG_CALLCONV ExtDBGDrivSetOutputMode(PDBG_STREAM psStream,IMG_UINT32 ui32OutMode)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivSetOutputMode(psStream, ui32OutMode);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+void IMG_CALLCONV ExtDBGDrivSetDebugLevel(PDBG_STREAM psStream,IMG_UINT32 ui32DebugLevel)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivSetDebugLevel(psStream, ui32DebugLevel);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+void IMG_CALLCONV ExtDBGDrivSetFrame(PDBG_STREAM psStream,IMG_UINT32 ui32Frame)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivSetFrame(psStream, ui32Frame);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetFrame(PDBG_STREAM psStream)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivGetFrame(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_BOOL IMG_CALLCONV ExtDBGDrivIsLastCaptureFrame(PDBG_STREAM psStream)
+{
+	IMG_BOOL	bRet;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	bRet = DBGDrivIsLastCaptureFrame(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return bRet;
+}
+
+IMG_BOOL IMG_CALLCONV ExtDBGDrivIsCaptureFrame(PDBG_STREAM psStream, IMG_BOOL bCheckPreviousFrame)
+{
+	IMG_BOOL	bRet;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	bRet = DBGDrivIsCaptureFrame(psStream, bCheckPreviousFrame);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return bRet;
+}
+
+void IMG_CALLCONV ExtDBGDrivOverrideMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivOverrideMode(psStream, ui32Mode);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+void IMG_CALLCONV ExtDBGDrivDefaultMode(PDBG_STREAM psStream)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivDefaultMode(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWrite2(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivWrite2(psStream, pui8InBuf, ui32InBuffSize, ui32Level);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteStringCM(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivWriteStringCM(psStream, pszString, ui32Level);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteCM(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret=DBGDrivWriteCM(psStream, pui8InBuf, ui32InBuffSize, ui32Level);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+void IMG_CALLCONV ExtDBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivSetMarker(psStream, ui32Marker);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetMarker(PDBG_STREAM psStream)
+{
+	IMG_UINT32	ui32Marker;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Marker = DBGDrivGetMarker(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Marker;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteLF(PDBG_STREAM psStream, IMG_UINT8 * pui8InBuf, IMG_UINT32 ui32InBuffSize, IMG_UINT32 ui32Level, IMG_UINT32 ui32Flags)
+{
+	IMG_UINT32	ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret = DBGDrivWriteLF(psStream, pui8InBuf, ui32InBuffSize, ui32Level, ui32Flags);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivReadLF(PDBG_STREAM psStream, IMG_UINT32 ui32OutBuffSize, IMG_UINT8 * pui8OutBuf)
+{
+	IMG_UINT32 ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret = DBGDrivReadLF(psStream, ui32OutBuffSize, pui8OutBuf);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+
+IMG_VOID IMG_CALLCONV ExtDBGDrivStartInitPhase(PDBG_STREAM psStream)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivStartInitPhase(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+IMG_VOID IMG_CALLCONV ExtDBGDrivStopInitPhase(PDBG_STREAM psStream)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivStopInitPhase(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return;
+}
+
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetStreamOffset(PDBG_STREAM psStream)
+{
+	IMG_UINT32 ui32Ret;
+
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	ui32Ret = DBGDrivGetStreamOffset(psStream);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+
+	return ui32Ret;
+}
+
+IMG_VOID IMG_CALLCONV ExtDBGDrivSetStreamOffset(PDBG_STREAM psStream, IMG_UINT32 ui32StreamOffset)
+{
+
+	HostAquireMutex(g_pvAPIMutex);
+
+	DBGDrivSetStreamOffset(psStream, ui32StreamOffset);
+
+
+	HostReleaseMutex(g_pvAPIMutex);
+}
+
+IMG_VOID IMG_CALLCONV ExtDBGDrivWaitForEvent(DBG_EVENT eEvent)
+{
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+	DBGDrivWaitForEvent(eEvent);
+#else
+	PVR_UNREFERENCED_PARAMETER(eEvent);
+#endif
+}
+
+IMG_UINT32 AtoI(IMG_CHAR *szIn)
+{
+	IMG_INT		iLen = 0;
+	IMG_UINT32	ui32Value = 0;
+	IMG_UINT32	ui32Digit=1;
+	IMG_UINT32	ui32Base=10;
+	IMG_INT		iPos;
+	IMG_CHAR	bc;
+
+
+	while (szIn[iLen] > 0)
+	{
+		iLen ++;
+	}
+
+
+	if (iLen == 0)
+	{
+		return (0);
+	}
+
+
+	iPos=0;
+	while (szIn[iPos] == '0')
+	{
+		iPos++;
+	}
+	if (szIn[iPos] == '\0')
+	{
+		return 0;
+	}
+	if (szIn[iPos] == 'x' || szIn[iPos] == 'X')
+	{
+		ui32Base=16;
+		szIn[iPos]='0';
+	}
+
+
+	for (iPos = iLen - 1; iPos >= 0; iPos --)
+	{
+		bc = szIn[iPos];
+
+		if ( (bc >= 'a') && (bc <= 'f') && ui32Base == 16)
+		{
+			bc -= 'a' - 0xa;
+		}
+		else
+		if ( (bc >= 'A') && (bc <= 'F') && ui32Base == 16)
+		{
+			bc -= 'A' - 0xa;
+		}
+		else
+		if ((bc >= '0') && (bc <= '9'))
+		{
+			bc -= '0';
+		}
+		else
+			return (0);
+
+		ui32Value += (IMG_UINT32)bc  * ui32Digit;
+
+		ui32Digit = ui32Digit * ui32Base;
+	}
+	return (ui32Value);
+}
+
+
+IMG_BOOL StreamValid(PDBG_STREAM psStream)
+{
+	PDBG_STREAM	psThis;
+
+	psThis = g_psStreamList;
+
+	while (psThis)
+	{
+		if (psStream && (psThis == psStream))
+		{
+			return(IMG_TRUE);
+		}
+		else
+		{
+			psThis = psThis->psNext;
+		}
+	}
+
+	return(IMG_FALSE);
+}
+
+
+void Write(PDBG_STREAM psStream,IMG_UINT8 * pui8Data,IMG_UINT32 ui32InBuffSize)
+{
+
+
+	if ((psStream->ui32WPtr + ui32InBuffSize) > psStream->ui32Size)
+	{
+		IMG_UINT32 ui32B1 = psStream->ui32Size - psStream->ui32WPtr;
+		IMG_UINT32 ui32B2 = ui32InBuffSize - ui32B1;
+
+
+		HostMemCopy((IMG_VOID *)(psStream->ui32Base + psStream->ui32WPtr),
+				(IMG_VOID *) pui8Data,
+				ui32B1);
+
+
+		HostMemCopy((IMG_VOID *)psStream->ui32Base,
+				(IMG_VOID *)((IMG_UINT32) pui8Data + ui32B1),
+				ui32B2);
+
+
+		psStream->ui32WPtr = ui32B2;
+	}
+	else
+	{
+		HostMemCopy((IMG_VOID *)(psStream->ui32Base + psStream->ui32WPtr),
+				(IMG_VOID *) pui8Data,
+				ui32InBuffSize);
+
+		psStream->ui32WPtr += ui32InBuffSize;
+
+		if (psStream->ui32WPtr == psStream->ui32Size)
+		{
+			psStream->ui32WPtr = 0;
+		}
+	}
+	psStream->ui32DataWritten += ui32InBuffSize;
+}
+
+
+void MonoOut(IMG_CHAR * pszString,IMG_BOOL bNewLine)
+{
+	IMG_UINT32 	i;
+	IMG_CHAR *	pScreen;
+
+	pScreen = (IMG_CHAR *) DBGDRIV_MONOBASE;
+
+	pScreen += g_ui32Line * 160;
+
+
+
+	i=0;
+	do
+	{
+		pScreen[g_ui32LOff + (i*2)] = pszString[i];
+		pScreen[g_ui32LOff + (i*2)+1] = 127;
+		i++;
+	}
+	while ((pszString[i] != 0) && (i < 4096));
+
+	g_ui32LOff += i * 2;
+
+	if (bNewLine)
+	{
+		g_ui32LOff = 0;
+		g_ui32Line++;
+	}
+
+
+
+	if (g_ui32Line == g_ui32MonoLines)
+	{
+		g_ui32Line = g_ui32MonoLines - 1;
+
+		HostMemCopy((IMG_VOID *)DBGDRIV_MONOBASE,(IMG_VOID *)(DBGDRIV_MONOBASE + 160),160 * (g_ui32MonoLines - 1));
+
+		HostMemSet((IMG_VOID *)(DBGDRIV_MONOBASE + (160 * (g_ui32MonoLines - 1))),0,160);
+	}
+}
+
+
+
+void AppendName(IMG_CHAR * pszOut,IMG_CHAR * pszBase,IMG_CHAR * pszName)
+{
+	IMG_UINT32 i;
+	IMG_UINT32 ui32Off;
+
+	i = 0;
+
+	while (pszBase[i] != 0)
+	{
+		pszOut[i] = pszBase[i];
+		i++;
+	}
+
+	ui32Off = i;
+	i = 0;
+
+	while (pszName[i] != 0)
+	{
+		pszOut[ui32Off+i] = pszName[i];
+		i++;
+	}
+
+	pszOut[ui32Off+i] = pszName[i];
+}
+
+
+IMG_VOID * IMG_CALLCONV DBGDrivCreateStream(IMG_CHAR *		pszName,
+								   IMG_UINT32 	ui32CapMode,
+								   IMG_UINT32 	ui32OutMode,
+								   IMG_UINT32	ui32Flags,
+								   IMG_UINT32 	ui32Size)
+{
+	PDBG_STREAM	psStream;
+	PDBG_STREAM	psInitStream;
+	PDBG_LASTFRAME_BUFFER	psLFBuffer;
+	IMG_UINT32		ui32Off;
+	IMG_VOID *		pvBase;
+
+
+
+
+	psStream = (PDBG_STREAM) DBGDrivFindStream(pszName, IMG_FALSE);
+
+	if (psStream)
+	{
+		return ((IMG_VOID *) psStream);
+	}
+
+
+
+	psStream = HostNonPageablePageAlloc(1);
+	psInitStream = HostNonPageablePageAlloc(1);
+	psLFBuffer = HostNonPageablePageAlloc(1);
+	if	(
+			(!psStream) ||
+			(!psInitStream) ||
+			(!psLFBuffer)
+		)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc control structs\n\r"));
+		return((IMG_VOID *) 0);
+	}
+
+
+	if ((ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
+	{
+		pvBase = HostNonPageablePageAlloc(ui32Size);
+	}
+	else
+	{
+		pvBase = HostPageablePageAlloc(ui32Size);
+	}
+
+	if (!pvBase)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc Stream buffer\n\r"));
+		HostNonPageablePageFree(psStream);
+		return((IMG_VOID *) 0);
+	}
+
+
+
+	psStream->psNext = 0;
+	psStream->ui32Flags = ui32Flags;
+	psStream->ui32Base = (IMG_UINT32)pvBase;
+	psStream->ui32Size = ui32Size * 4096UL;
+	psStream->ui32RPtr = 0;
+	psStream->ui32WPtr = 0;
+	psStream->ui32DataWritten = 0;
+	psStream->ui32CapMode = ui32CapMode;
+	psStream->ui32OutMode = ui32OutMode;
+	psStream->ui32DebugLevel = DEBUG_LEVEL_0;
+	psStream->ui32DefaultMode = ui32CapMode;
+	psStream->ui32Start = 0;
+	psStream->ui32End = 0;
+	psStream->ui32Current = 0;
+	psStream->ui32SampleRate = 1;
+	psStream->ui32Access = 0;
+	psStream->ui32Timeout = 0;
+	psStream->ui32Marker = 0;
+	psStream->bInitPhaseComplete = IMG_FALSE;
+
+
+	if ((ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
+	{
+		pvBase = HostNonPageablePageAlloc(ui32Size);
+	}
+	else
+	{
+		pvBase = HostPageablePageAlloc(ui32Size);
+	}
+
+	if (!pvBase)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc InitStream buffer\n\r"));
+
+		if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
+		{
+			HostNonPageablePageFree((IMG_VOID *)psStream->ui32Base);
+		}
+		else
+		{
+			HostPageablePageFree((IMG_VOID *)psStream->ui32Base);
+		}
+		HostNonPageablePageFree(psStream);
+		return((IMG_VOID *) 0);
+	}
+
+	psInitStream->psNext = 0;
+	psInitStream->ui32Flags = ui32Flags;
+	psInitStream->ui32Base = (IMG_UINT32)pvBase;
+	psInitStream->ui32Size = ui32Size * 4096UL;
+	psInitStream->ui32RPtr = 0;
+	psInitStream->ui32WPtr = 0;
+	psInitStream->ui32DataWritten = 0;
+	psInitStream->ui32CapMode = ui32CapMode;
+	psInitStream->ui32OutMode = ui32OutMode;
+	psInitStream->ui32DebugLevel = DEBUG_LEVEL_0;
+	psInitStream->ui32DefaultMode = ui32CapMode;
+	psInitStream->ui32Start = 0;
+	psInitStream->ui32End = 0;
+	psInitStream->ui32Current = 0;
+	psInitStream->ui32SampleRate = 1;
+	psInitStream->ui32Access = 0;
+	psInitStream->ui32Timeout = 0;
+	psInitStream->ui32Marker = 0;
+	psInitStream->bInitPhaseComplete = IMG_FALSE;
+
+	psStream->psInitStream = psInitStream;
+
+
+	psLFBuffer->psStream = psStream;
+	psLFBuffer->ui32BufLen = 0UL;
+
+	g_bHotkeyMiddump = IMG_FALSE;
+	g_ui32HotkeyMiddumpStart = 0xffffffffUL;
+	g_ui32HotkeyMiddumpEnd = 0xffffffffUL;
+
+
+
+	ui32Off = 0;
+
+	do
+	{
+		psStream->szName[ui32Off] = pszName[ui32Off];
+
+		ui32Off++;
+	}
+	while ((pszName[ui32Off] != 0) && (ui32Off < (4096UL - sizeof(DBG_STREAM))));
+
+	psStream->szName[ui32Off] = pszName[ui32Off];
+
+
+
+	psStream->psNext = g_psStreamList;
+	g_psStreamList = psStream;
+
+	psLFBuffer->psNext = g_psLFBufferList;
+	g_psLFBufferList = psLFBuffer;
+
+
+	return((IMG_VOID *) psStream);
+}
+
+void IMG_CALLCONV DBGDrivDestroyStream(PDBG_STREAM psStream)
+{
+	PDBG_STREAM	psStreamThis;
+	PDBG_STREAM	psStreamPrev;
+	PDBG_LASTFRAME_BUFFER	psLFBuffer;
+	PDBG_LASTFRAME_BUFFER	psLFThis;
+	PDBG_LASTFRAME_BUFFER	psLFPrev;
+
+	PVR_DPF((PVR_DBG_MESSAGE, "DBGDriv: Destroying stream %s\r\n", psStream->szName ));
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psLFBuffer = FindLFBuf(psStream);
+
+
+
+	psStreamThis = g_psStreamList;
+	psStreamPrev = 0;
+
+	while (psStreamThis)
+	{
+		if (psStreamThis == psStream)
+		{
+			if (psStreamPrev)
+			{
+				psStreamPrev->psNext = psStreamThis->psNext;
+			}
+			else
+			{
+				g_psStreamList = psStreamThis->psNext;
+			}
+
+			psStreamThis = 0;
+		}
+		else
+		{
+			psStreamPrev = psStreamThis;
+			psStreamThis = psStreamThis->psNext;
+		}
+	}
+
+	psLFThis = g_psLFBufferList;
+	psLFPrev = 0;
+
+	while (psLFThis)
+	{
+		if (psLFThis == psLFBuffer)
+		{
+			if (psLFPrev)
+			{
+				psLFPrev->psNext = psLFThis->psNext;
+			}
+			else
+			{
+				g_psLFBufferList = psLFThis->psNext;
+			}
+
+			psLFThis = 0;
+		}
+		else
+		{
+			psLFPrev = psLFThis;
+			psLFThis = psLFThis->psNext;
+		}
+	}
+
+
+	if (psStream->ui32CapMode & DEBUG_CAPMODE_HOTKEY)
+	{
+		DeactivateHotKeys();
+	}
+
+
+
+	if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
+	{
+		HostNonPageablePageFree((IMG_VOID *)psStream->ui32Base);
+		HostNonPageablePageFree((IMG_VOID *)psStream->psInitStream->ui32Base);
+	}
+	else
+	{
+		HostPageablePageFree((IMG_VOID *)psStream->ui32Base);
+		HostPageablePageFree((IMG_VOID *)psStream->psInitStream->ui32Base);
+	}
+
+	HostNonPageablePageFree(psStream->psInitStream);
+	HostNonPageablePageFree(psStream);
+	HostNonPageablePageFree(psLFBuffer);
+
+	if (g_psStreamList == 0)
+	{
+		PVR_DPF((PVR_DBG_MESSAGE,"DBGDriv: Stream list now empty" ));
+	}
+
+	return;
+}
+
+IMG_VOID * IMG_CALLCONV DBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream)
+{
+	PDBG_STREAM	psStream;
+	PDBG_STREAM	psThis;
+	IMG_UINT32	ui32Off;
+	IMG_BOOL	bAreSame;
+
+	psStream = 0;
+
+
+
+	for (psThis = g_psStreamList; psThis != IMG_NULL; psThis = psThis->psNext)
+	{
+		bAreSame = IMG_TRUE;
+		ui32Off = 0;
+
+		if (strlen(psThis->szName) == strlen(pszName))
+		{
+			while ((psThis->szName[ui32Off] != 0) && (pszName[ui32Off] != 0) && (ui32Off < 128) && bAreSame)
+			{
+				if (psThis->szName[ui32Off] != pszName[ui32Off])
+				{
+					bAreSame = IMG_FALSE;
+				}
+
+				ui32Off++;
+			}
+		}
+		else
+		{
+			bAreSame = IMG_FALSE;
+		}
+
+		if (bAreSame)
+		{
+			psStream = psThis;
+			break;
+		}
+	}
+
+	if(bResetStream && psStream)
+	{
+		static IMG_CHAR szComment[] = "-- Init phase terminated\r\n";
+		psStream->psInitStream->ui32RPtr = 0;
+		psStream->ui32RPtr = 0;
+		psStream->ui32WPtr = 0;
+		psStream->ui32DataWritten = psStream->psInitStream->ui32DataWritten;
+		if (psStream->bInitPhaseComplete == IMG_FALSE)
+		{
+			if (psStream->ui32Flags & DEBUG_FLAGS_TEXTSTREAM)
+			{
+				DBGDrivWrite2(psStream, (IMG_UINT8 *)szComment, sizeof(szComment) - 1, 0x01);
+			}
+			psStream->bInitPhaseComplete = IMG_TRUE;
+		}
+	}
+
+	return((IMG_VOID *) psStream);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteStringCM(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if (psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED)
+	{
+		if	((psStream->ui32Flags & DEBUG_FLAGS_ENABLESAMPLE) == 0)
+		{
+			return(0);
+		}
+	}
+	else
+	{
+		if (psStream->ui32CapMode == DEBUG_CAPMODE_HOTKEY)
+		{
+			if ((psStream->ui32Current != g_ui32HotKeyFrame) || (g_bHotKeyPressed == IMG_FALSE))
+			{
+				return(0);
+			}
+		}
+	}
+
+	return(DBGDrivWriteString(psStream,pszString,ui32Level));
+
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Len;
+	IMG_UINT32	ui32Space;
+	IMG_UINT32	ui32WPtr;
+	IMG_UINT8 *	pui8Buffer;
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if ((psStream->ui32DebugLevel & ui32Level) == 0)
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+
+	if ((psStream->ui32OutMode & DEBUG_OUTMODE_ASYNC) == 0)
+	{
+		if (psStream->ui32OutMode & DEBUG_OUTMODE_STANDARDDBG)
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,"%s: %s\r\n",psStream->szName, pszString));
+		}
+
+
+
+		if (psStream->ui32OutMode & DEBUG_OUTMODE_MONO)
+		{
+			MonoOut(psStream->szName,IMG_FALSE);
+			MonoOut(": ",IMG_FALSE);
+			MonoOut(pszString,IMG_TRUE);
+		}
+	}
+
+
+
+	if	(
+			!(
+				((psStream->ui32OutMode & DEBUG_OUTMODE_STREAMENABLE) != 0) ||
+				((psStream->ui32OutMode & DEBUG_OUTMODE_ASYNC) != 0)
+			)
+		)
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	ui32Space=SpaceInStream(psStream);
+
+	if(ui32Space > 0)
+	{
+		ui32Space--;
+	}
+
+	ui32Len		= 0;
+	ui32WPtr	= psStream->ui32WPtr;
+	pui8Buffer	= (IMG_UINT8 *) psStream->ui32Base;
+
+	while((pszString[ui32Len] != 0) && (ui32Len < ui32Space))
+	{
+		pui8Buffer[ui32WPtr] = (IMG_UINT8)pszString[ui32Len];
+		ui32Len++;
+		ui32WPtr++;
+		if (ui32WPtr == psStream->ui32Size)
+		{
+			ui32WPtr = 0;
+		}
+	}
+
+	if (ui32Len < ui32Space)
+	{
+
+		pui8Buffer[ui32WPtr] = (IMG_UINT8)pszString[ui32Len];
+		ui32Len++;
+		ui32WPtr++;
+		if (ui32WPtr == psStream->ui32Size)
+		{
+			ui32WPtr = 0;
+		}
+
+
+		psStream->ui32WPtr = ui32WPtr;
+		psStream->ui32DataWritten+= ui32Len;
+	} else
+	{
+		ui32Len = 0;
+	}
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+	if (ui32Len)
+	{
+		HostSignalEvent(DBG_EVENT_STREAM_DATA);
+	}
+#endif
+
+	return(ui32Len);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivReadString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Limit)
+{
+	IMG_UINT32				ui32OutLen;
+	IMG_UINT32				ui32Len;
+	IMG_UINT32				ui32Offset;
+	IMG_UINT8				*pui8Buff;
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0);
+	}
+
+
+
+	pui8Buff = (IMG_UINT8 *) psStream->ui32Base;
+	ui32Offset = psStream->ui32RPtr;
+
+	if (psStream->ui32RPtr == psStream->ui32WPtr)
+	{
+		return(0);
+	}
+
+
+
+	ui32Len = 0;
+	while((pui8Buff[ui32Offset] != 0) && (ui32Offset != psStream->ui32WPtr))
+	{
+		ui32Offset++;
+		ui32Len++;
+
+
+
+		if (ui32Offset == psStream->ui32Size)
+		{
+			ui32Offset = 0;
+		}
+	}
+
+	ui32OutLen = ui32Len + 1;
+
+
+
+	if (ui32Len > ui32Limit)
+	{
+		return(0);
+	}
+
+
+
+	ui32Offset = psStream->ui32RPtr;
+	ui32Len = 0;
+
+	while ((pui8Buff[ui32Offset] != 0) && (ui32Len < ui32Limit))
+	{
+		pszString[ui32Len] = (IMG_CHAR)pui8Buff[ui32Offset];
+		ui32Offset++;
+		ui32Len++;
+
+
+
+		if (ui32Offset == psStream->ui32Size)
+		{
+			ui32Offset = 0;
+		}
+	}
+
+	pszString[ui32Len] = (IMG_CHAR)pui8Buff[ui32Offset];
+
+	psStream->ui32RPtr = ui32Offset + 1;
+
+	if (psStream->ui32RPtr == psStream->ui32Size)
+	{
+		psStream->ui32RPtr = 0;
+	}
+
+	return(ui32OutLen);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivWrite(PDBG_STREAM psMainStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32				ui32Space;
+	DBG_STREAM *psStream;
+
+
+
+	if (!StreamValid(psMainStream))
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if ((psMainStream->ui32DebugLevel & ui32Level) == 0)
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if (psMainStream->ui32CapMode & DEBUG_CAPMODE_FRAMED)
+	{
+		if	((psMainStream->ui32Flags & DEBUG_FLAGS_ENABLESAMPLE) == 0)
+		{
+			return(0xFFFFFFFFUL);
+		}
+	}
+	else if (psMainStream->ui32CapMode == DEBUG_CAPMODE_HOTKEY)
+	{
+		if ((psMainStream->ui32Current != g_ui32HotKeyFrame) || (g_bHotKeyPressed == IMG_FALSE))
+			return(0xFFFFFFFFUL);
+	}
+
+	if(psMainStream->bInitPhaseComplete)
+	{
+		psStream = psMainStream;
+	}
+	else
+	{
+		psStream = psMainStream->psInitStream;
+	}
+
+
+
+	ui32Space=SpaceInStream(psStream);
+
+
+
+	if ((psStream->ui32OutMode & DEBUG_OUTMODE_STREAMENABLE) == 0)
+	{
+		return(0);
+	}
+
+	if (ui32Space < 8)
+	{
+		return(0);
+	}
+
+
+
+	if (ui32Space <= (ui32InBuffSize + 4))
+	{
+		ui32InBuffSize = ui32Space - 8;
+	}
+
+
+
+	Write(psStream,(IMG_UINT8 *) &ui32InBuffSize,4);
+	Write(psStream,pui8InBuf,ui32InBuffSize);
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+	if (ui32InBuffSize)
+	{
+		HostSignalEvent(DBG_EVENT_STREAM_DATA);
+	}
+#endif
+	return(ui32InBuffSize);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteCM(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if (psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED)
+	{
+		if	((psStream->ui32Flags & DEBUG_FLAGS_ENABLESAMPLE) == 0)
+		{
+			return(0xFFFFFFFFUL);
+		}
+	}
+	else
+	{
+		if (psStream->ui32CapMode == DEBUG_CAPMODE_HOTKEY)
+		{
+			if ((psStream->ui32Current != g_ui32HotKeyFrame) || (g_bHotKeyPressed == IMG_FALSE))
+			{
+				return(0xFFFFFFFFUL);
+			}
+		}
+	}
+
+	return(DBGDrivWrite2(psStream,pui8InBuf,ui32InBuffSize,ui32Level));
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivWrite2(PDBG_STREAM psMainStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level)
+{
+	IMG_UINT32	ui32Space;
+	DBG_STREAM	*psStream;
+
+
+
+	if (!StreamValid(psMainStream))
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if ((psMainStream->ui32DebugLevel & ui32Level) == 0)
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+	if(psMainStream->bInitPhaseComplete)
+	{
+		psStream = psMainStream;
+	}
+	else
+	{
+		psStream = psMainStream->psInitStream;
+	}
+
+
+
+	ui32Space=SpaceInStream(psStream);
+
+
+
+	if ((psStream->ui32OutMode & DEBUG_OUTMODE_STREAMENABLE) == 0)
+	{
+		return(0);
+	}
+
+
+
+	if (psStream->ui32Flags & DEBUG_FLAGS_NO_BUF_EXPANDSION)
+	{
+
+
+
+		if (ui32Space < 32)
+		{
+			return(0);
+		}
+	}
+	else
+	{
+		if ((ui32Space < 32) || (ui32Space <= (ui32InBuffSize + 4)))
+		{
+			IMG_UINT32	ui32NewBufSize;
+
+
+
+			ui32NewBufSize = 2 * psStream->ui32Size;
+
+			if (ui32InBuffSize > psStream->ui32Size)
+			{
+				ui32NewBufSize += ui32InBuffSize;
+			}
+
+
+
+			if (!ExpandStreamBuffer(psStream,ui32NewBufSize))
+			{
+				if (ui32Space < 32)
+				{
+					return(0);
+				}
+			}
+
+
+
+			ui32Space = SpaceInStream(psStream);
+		}
+	}
+
+
+
+	if (ui32Space <= (ui32InBuffSize + 4))
+	{
+		ui32InBuffSize = ui32Space - 4;
+	}
+
+
+
+	Write(psStream,pui8InBuf,ui32InBuffSize);
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+	if (ui32InBuffSize)
+	{
+		HostSignalEvent(DBG_EVENT_STREAM_DATA);
+	}
+#endif
+	return(ui32InBuffSize);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivRead(PDBG_STREAM psMainStream, IMG_BOOL bReadInitBuffer, IMG_UINT32 ui32OutBuffSize,IMG_UINT8 * pui8OutBuf)
+{
+	IMG_UINT32 ui32Data;
+	DBG_STREAM *psStream;
+
+
+
+	if (!StreamValid(psMainStream))
+	{
+		return(0);
+	}
+
+	if(bReadInitBuffer)
+	{
+		psStream = psMainStream->psInitStream;
+	}
+	else
+	{
+		psStream = psMainStream;
+	}
+
+	if (psStream->ui32RPtr == psStream->ui32WPtr)
+	{
+		return(0);
+	}
+
+
+
+	if (psStream->ui32RPtr <= psStream->ui32WPtr)
+	{
+		ui32Data = psStream->ui32WPtr - psStream->ui32RPtr;
+	}
+	else
+	{
+		ui32Data = psStream->ui32WPtr + (psStream->ui32Size - psStream->ui32RPtr);
+	}
+
+
+
+	if (ui32Data > ui32OutBuffSize)
+	{
+		ui32Data = ui32OutBuffSize;
+	}
+
+
+
+	if ((psStream->ui32RPtr + ui32Data) > psStream->ui32Size)
+	{
+		IMG_UINT32 ui32B1 = psStream->ui32Size - psStream->ui32RPtr;
+		IMG_UINT32 ui32B2 = ui32Data - ui32B1;
+
+
+		HostMemCopy((IMG_VOID *) pui8OutBuf,
+				(IMG_VOID *)(psStream->ui32Base + psStream->ui32RPtr),
+				ui32B1);
+
+
+		HostMemCopy((IMG_VOID *)((IMG_UINT32) pui8OutBuf + ui32B1),
+				(IMG_VOID *)psStream->ui32Base,
+				ui32B2);
+
+
+		psStream->ui32RPtr = ui32B2;
+	}
+	else
+	{
+		HostMemCopy((IMG_VOID *) pui8OutBuf,
+				(IMG_VOID *)(psStream->ui32Base + psStream->ui32RPtr),
+				ui32Data);
+
+
+		psStream->ui32RPtr += ui32Data;
+
+
+		if (psStream->ui32RPtr == psStream->ui32Size)
+		{
+			psStream->ui32RPtr = 0;
+		}
+	}
+
+	return(ui32Data);
+}
+
+void IMG_CALLCONV DBGDrivSetCaptureMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode,IMG_UINT32 ui32Start,IMG_UINT32 ui32End,IMG_UINT32 ui32SampleRate)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32CapMode = ui32Mode;
+	psStream->ui32DefaultMode = ui32Mode;
+	psStream->ui32Start = ui32Start;
+	psStream->ui32End = ui32End;
+	psStream->ui32SampleRate = ui32SampleRate;
+
+
+
+	if (psStream->ui32CapMode & DEBUG_CAPMODE_HOTKEY)
+	{
+		ActivateHotKeys(psStream);
+	}
+}
+
+void IMG_CALLCONV DBGDrivSetOutputMode(PDBG_STREAM psStream,IMG_UINT32 ui32OutMode)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32OutMode = ui32OutMode;
+}
+
+void IMG_CALLCONV DBGDrivSetDebugLevel(PDBG_STREAM psStream,IMG_UINT32 ui32DebugLevel)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32DebugLevel = ui32DebugLevel;
+}
+
+void IMG_CALLCONV DBGDrivSetFrame(PDBG_STREAM psStream,IMG_UINT32 ui32Frame)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32Current = ui32Frame;
+
+	if ((ui32Frame >= psStream->ui32Start) &&
+		(ui32Frame <= psStream->ui32End) &&
+		(((ui32Frame - psStream->ui32Start) % psStream->ui32SampleRate) == 0))
+	{
+		psStream->ui32Flags |= DEBUG_FLAGS_ENABLESAMPLE;
+	}
+	else
+	{
+		psStream->ui32Flags &= ~DEBUG_FLAGS_ENABLESAMPLE;
+	}
+
+	if (g_bHotkeyMiddump)
+	{
+		if ((ui32Frame >= g_ui32HotkeyMiddumpStart) &&
+			(ui32Frame <= g_ui32HotkeyMiddumpEnd) &&
+			(((ui32Frame - g_ui32HotkeyMiddumpStart) % psStream->ui32SampleRate) == 0))
+		{
+			psStream->ui32Flags |= DEBUG_FLAGS_ENABLESAMPLE;
+		}
+		else
+		{
+			psStream->ui32Flags &= ~DEBUG_FLAGS_ENABLESAMPLE;
+			if (psStream->ui32Current > g_ui32HotkeyMiddumpEnd)
+			{
+				g_bHotkeyMiddump = IMG_FALSE;
+			}
+		}
+	}
+
+
+	if (g_bHotKeyRegistered)
+	{
+		g_bHotKeyRegistered = IMG_FALSE;
+
+		PVR_DPF((PVR_DBG_MESSAGE,"Hotkey pressed (%08x)!\n",psStream));
+
+		if (!g_bHotKeyPressed)
+		{
+
+
+			g_ui32HotKeyFrame = psStream->ui32Current + 2;
+
+
+
+			g_bHotKeyPressed = IMG_TRUE;
+		}
+
+
+
+		if (((psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED) != 0) &&
+			((psStream->ui32CapMode & DEBUG_CAPMODE_HOTKEY) != 0))
+		{
+			if (!g_bHotkeyMiddump)
+			{
+
+				g_ui32HotkeyMiddumpStart = g_ui32HotKeyFrame + 1;
+				g_ui32HotkeyMiddumpEnd = 0xffffffff;
+				g_bHotkeyMiddump = IMG_TRUE;
+				PVR_DPF((PVR_DBG_MESSAGE,"Sampling every %d frame(s)\n", psStream->ui32SampleRate));
+			}
+			else
+			{
+
+				g_ui32HotkeyMiddumpEnd = g_ui32HotKeyFrame;
+				PVR_DPF((PVR_DBG_MESSAGE,"Turning off sampling\n"));
+			}
+		}
+
+	}
+
+
+
+	if (psStream->ui32Current > g_ui32HotKeyFrame)
+	{
+		g_bHotKeyPressed = IMG_FALSE;
+	}
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivGetFrame(PDBG_STREAM psStream)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0);
+	}
+
+	return(psStream->ui32Current);
+}
+
+IMG_BOOL IMG_CALLCONV DBGDrivIsLastCaptureFrame(PDBG_STREAM psStream)
+{
+	IMG_UINT32	ui32NextFrame;
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return IMG_FALSE;
+	}
+
+	if (psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED)
+	{
+		ui32NextFrame = psStream->ui32Current + psStream->ui32SampleRate;
+		if (ui32NextFrame > psStream->ui32End)
+		{
+			return IMG_TRUE;
+		}
+	}
+	return IMG_FALSE;
+}
+
+IMG_BOOL IMG_CALLCONV DBGDrivIsCaptureFrame(PDBG_STREAM psStream, IMG_BOOL bCheckPreviousFrame)
+{
+	IMG_UINT32 ui32FrameShift = bCheckPreviousFrame ? 1UL : 0UL;
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return IMG_FALSE;
+	}
+
+	if (psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED)
+	{
+
+		if (g_bHotkeyMiddump)
+		{
+			if ((psStream->ui32Current >= (g_ui32HotkeyMiddumpStart - ui32FrameShift)) &&
+				(psStream->ui32Current <= (g_ui32HotkeyMiddumpEnd - ui32FrameShift)) &&
+				((((psStream->ui32Current + ui32FrameShift) - g_ui32HotkeyMiddumpStart) % psStream->ui32SampleRate) == 0))
+			{
+				return IMG_TRUE;
+			}
+		}
+		else
+		{
+			if ((psStream->ui32Current >= (psStream->ui32Start - ui32FrameShift)) &&
+				(psStream->ui32Current <= (psStream->ui32End - ui32FrameShift)) &&
+				((((psStream->ui32Current + ui32FrameShift) - psStream->ui32Start) % psStream->ui32SampleRate) == 0))
+			{
+				return IMG_TRUE;
+			}
+		}
+	}
+	else if (psStream->ui32CapMode == DEBUG_CAPMODE_HOTKEY)
+	{
+		if ((psStream->ui32Current == (g_ui32HotKeyFrame-ui32FrameShift)) && (g_bHotKeyPressed))
+		{
+			return IMG_TRUE;
+		}
+	}
+	return IMG_FALSE;
+}
+
+void IMG_CALLCONV DBGDrivOverrideMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32CapMode = ui32Mode;
+}
+
+void IMG_CALLCONV DBGDrivDefaultMode(PDBG_STREAM psStream)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32CapMode = psStream->ui32DefaultMode;
+}
+
+void IMG_CALLCONV DBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return;
+	}
+
+	psStream->ui32Marker = ui32Marker;
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivGetMarker(PDBG_STREAM psStream)
+{
+
+
+	if (!StreamValid(psStream))
+	{
+		return 0;
+	}
+
+	return psStream->ui32Marker;
+}
+
+
+IMG_UINT32 IMG_CALLCONV DBGDrivGetStreamOffset(PDBG_STREAM psMainStream)
+{
+	PDBG_STREAM psStream;
+
+
+
+	if (!StreamValid(psMainStream))
+	{
+		return 0;
+	}
+
+	if(psMainStream->bInitPhaseComplete)
+	{
+		psStream = psMainStream;
+	}
+	else
+	{
+		psStream = psMainStream->psInitStream;
+	}
+
+	return psStream->ui32DataWritten;
+}
+
+IMG_VOID IMG_CALLCONV DBGDrivSetStreamOffset(PDBG_STREAM psMainStream, IMG_UINT32 ui32StreamOffset)
+{
+	PDBG_STREAM psStream;
+
+
+
+	if (!StreamValid(psMainStream))
+	{
+		return;
+	}
+
+	if(psMainStream->bInitPhaseComplete)
+	{
+		psStream = psMainStream;
+	}
+	else
+	{
+		psStream = psMainStream->psInitStream;
+	}
+
+	psStream->ui32DataWritten = ui32StreamOffset;
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivGetServiceTable(void)
+{
+	return((IMG_UINT32) &g_sDBGKMServices);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteLF(PDBG_STREAM psStream, IMG_UINT8 * pui8InBuf, IMG_UINT32 ui32InBuffSize, IMG_UINT32 ui32Level, IMG_UINT32 ui32Flags)
+{
+	PDBG_LASTFRAME_BUFFER	psLFBuffer;
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if ((psStream->ui32DebugLevel & ui32Level) == 0)
+	{
+		return(0xFFFFFFFFUL);
+	}
+
+
+
+	if ((psStream->ui32CapMode & DEBUG_CAPMODE_FRAMED) != 0)
+	{
+		if	((psStream->ui32Flags & DEBUG_FLAGS_ENABLESAMPLE) == 0)
+		{
+			return(0xFFFFFFFFUL);
+		}
+	}
+	else if (psStream->ui32CapMode == DEBUG_CAPMODE_HOTKEY)
+	{
+		if ((psStream->ui32Current != g_ui32HotKeyFrame) || (g_bHotKeyPressed == IMG_FALSE))
+			return(0xFFFFFFFFUL);
+	}
+
+	psLFBuffer = FindLFBuf(psStream);
+
+	if (ui32Flags & WRITELF_FLAGS_RESETBUF)
+	{
+
+
+		ui32InBuffSize = (ui32InBuffSize > LAST_FRAME_BUF_SIZE) ? LAST_FRAME_BUF_SIZE : ui32InBuffSize;
+		HostMemCopy((IMG_VOID *)psLFBuffer->ui8Buffer, (IMG_VOID *)pui8InBuf, ui32InBuffSize);
+		psLFBuffer->ui32BufLen = ui32InBuffSize;
+	}
+	else
+	{
+
+
+		ui32InBuffSize = ((psLFBuffer->ui32BufLen + ui32InBuffSize) > LAST_FRAME_BUF_SIZE) ? (LAST_FRAME_BUF_SIZE - psLFBuffer->ui32BufLen) : ui32InBuffSize;
+		HostMemCopy((IMG_VOID *)(&psLFBuffer->ui8Buffer[psLFBuffer->ui32BufLen]), (IMG_VOID *)pui8InBuf, ui32InBuffSize);
+		psLFBuffer->ui32BufLen += ui32InBuffSize;
+	}
+
+	return(ui32InBuffSize);
+}
+
+IMG_UINT32 IMG_CALLCONV DBGDrivReadLF(PDBG_STREAM psStream, IMG_UINT32 ui32OutBuffSize, IMG_UINT8 * pui8OutBuf)
+{
+	PDBG_LASTFRAME_BUFFER	psLFBuffer;
+	IMG_UINT32	ui32Data;
+
+
+
+	if (!StreamValid(psStream))
+	{
+		return(0);
+	}
+
+	psLFBuffer = FindLFBuf(psStream);
+
+
+
+	ui32Data = (ui32OutBuffSize < psLFBuffer->ui32BufLen) ? ui32OutBuffSize : psLFBuffer->ui32BufLen;
+
+
+
+	HostMemCopy((IMG_VOID *)pui8OutBuf, (IMG_VOID *)psLFBuffer->ui8Buffer, ui32Data);
+
+	return ui32Data;
+}
+
+IMG_VOID IMG_CALLCONV DBGDrivStartInitPhase(PDBG_STREAM psStream)
+{
+	psStream->bInitPhaseComplete = IMG_FALSE;
+}
+
+IMG_VOID IMG_CALLCONV DBGDrivStopInitPhase(PDBG_STREAM psStream)
+{
+	psStream->bInitPhaseComplete = IMG_TRUE;
+}
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+IMG_VOID IMG_CALLCONV DBGDrivWaitForEvent(DBG_EVENT eEvent)
+{
+	HostWaitForEvent(eEvent);
+}
+#endif
+
+IMG_BOOL ExpandStreamBuffer(PDBG_STREAM psStream, IMG_UINT32 ui32NewSize)
+{
+	IMG_VOID *	pvNewBuf;
+	IMG_UINT32	ui32NewSizeInPages;
+	IMG_UINT32	ui32NewWOffset;
+	IMG_UINT32	ui32SpaceInOldBuf;
+
+
+
+	if (psStream->ui32Size >= ui32NewSize)
+	{
+		return IMG_FALSE;
+	}
+
+
+
+	ui32SpaceInOldBuf = SpaceInStream(psStream);
+
+
+
+	ui32NewSizeInPages = ((ui32NewSize + 0xfffUL) & ~0xfffUL) / 4096UL;
+
+	if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
+	{
+		pvNewBuf = HostNonPageablePageAlloc(ui32NewSizeInPages);
+	}
+	else
+	{
+		pvNewBuf = HostPageablePageAlloc(ui32NewSizeInPages);
+	}
+
+	if (pvNewBuf == IMG_NULL)
+	{
+		return IMG_FALSE;
+	}
+
+
+
+
+	if (psStream->ui32RPtr <= psStream->ui32WPtr)
+	{
+
+
+		HostMemCopy((IMG_VOID *)pvNewBuf, (IMG_VOID *)(psStream->ui32Base + psStream->ui32RPtr), psStream->ui32WPtr - psStream->ui32RPtr);
+	}
+	else
+	{
+		IMG_UINT32	ui32FirstCopySize;
+
+
+
+		ui32FirstCopySize = psStream->ui32Size - psStream->ui32RPtr;
+
+		HostMemCopy((IMG_VOID *)pvNewBuf, (IMG_VOID *)(psStream->ui32Base + psStream->ui32RPtr), ui32FirstCopySize);
+
+
+
+		HostMemCopy((IMG_VOID *)((IMG_UINT32)pvNewBuf + ui32FirstCopySize), (IMG_VOID *)psStream->ui32Base, psStream->ui32WPtr);
+	}
+
+
+
+	ui32NewWOffset = psStream->ui32Size - ui32SpaceInOldBuf;
+
+
+
+	if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
+	{
+		HostNonPageablePageFree((IMG_VOID *)psStream->ui32Base);
+	}
+	else
+	{
+		HostPageablePageFree((IMG_VOID *)psStream->ui32Base);
+	}
+
+
+
+	psStream->ui32Base = (IMG_UINT32)pvNewBuf;
+	psStream->ui32RPtr = 0;
+	psStream->ui32WPtr = ui32NewWOffset;
+	psStream->ui32Size = ui32NewSizeInPages * 4096;
+
+	return IMG_TRUE;
+}
+
+IMG_UINT32 SpaceInStream(PDBG_STREAM psStream)
+{
+	IMG_UINT32	ui32Space;
+
+	if (psStream->ui32RPtr > psStream->ui32WPtr)
+	{
+		ui32Space = psStream->ui32RPtr - psStream->ui32WPtr;
+	}
+	else
+	{
+		ui32Space = psStream->ui32RPtr + (psStream->ui32Size - psStream->ui32WPtr);
+	}
+
+	return ui32Space;
+}
+
+
+void DestroyAllStreams(void)
+{
+	while (g_psStreamList != IMG_NULL)
+	{
+		DBGDrivDestroyStream(g_psStreamList);
+	}
+	return;
+}
+
+PDBG_LASTFRAME_BUFFER FindLFBuf(PDBG_STREAM psStream)
+{
+	PDBG_LASTFRAME_BUFFER	psLFBuffer;
+
+	psLFBuffer = g_psLFBufferList;
+
+	while (psLFBuffer)
+	{
+		if (psLFBuffer->psStream == psStream)
+		{
+			break;
+		}
+
+		psLFBuffer = psLFBuffer->psNext;
+	}
+
+	return psLFBuffer;
+}
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.h b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.h
new file mode 100644
index 0000000..167e0f6
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/dbgdriv.h
@@ -0,0 +1,112 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _DBGDRIV_
+#define _DBGDRIV_
+
+#define BUFFER_SIZE 64*PAGESIZE
+
+#define DBGDRIV_VERSION 	0x100
+#define MAX_PROCESSES 		2
+#define BLOCK_USED			0x01
+#define BLOCK_LOCKED		0x02
+#define DBGDRIV_MONOBASE	0x000B0000
+
+
+extern IMG_VOID *	g_pvAPIMutex;
+
+IMG_VOID * IMG_CALLCONV DBGDrivCreateStream(IMG_CHAR *		pszName,
+								   IMG_UINT32 	ui32CapMode,
+								   IMG_UINT32 	ui32OutMode,
+								   IMG_UINT32	ui32Flags,
+								   IMG_UINT32 	ui32Pages);
+IMG_VOID   IMG_CALLCONV DBGDrivDestroyStream(PDBG_STREAM psStream);
+IMG_VOID * IMG_CALLCONV DBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream);
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV DBGDrivReadString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Limit);
+IMG_UINT32 IMG_CALLCONV DBGDrivWrite(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV DBGDrivWrite2(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV DBGDrivRead(PDBG_STREAM psStream, IMG_BOOL bReadInitBuffer, IMG_UINT32 ui32OutBufferSize,IMG_UINT8 *pui8OutBuf);
+IMG_VOID   IMG_CALLCONV DBGDrivSetCaptureMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode,IMG_UINT32 ui32Start,IMG_UINT32 ui32Stop,IMG_UINT32 ui32SampleRate);
+IMG_VOID   IMG_CALLCONV DBGDrivSetOutputMode(PDBG_STREAM psStream,IMG_UINT32 ui32OutMode);
+IMG_VOID   IMG_CALLCONV DBGDrivSetDebugLevel(PDBG_STREAM psStream,IMG_UINT32 ui32DebugLevel);
+IMG_VOID   IMG_CALLCONV DBGDrivSetFrame(PDBG_STREAM psStream,IMG_UINT32 ui32Frame);
+IMG_UINT32 IMG_CALLCONV DBGDrivGetFrame(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV DBGDrivOverrideMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode);
+IMG_VOID   IMG_CALLCONV DBGDrivDefaultMode(PDBG_STREAM psStream);
+IMG_UINT32 IMG_CALLCONV DBGDrivGetServiceTable(IMG_VOID);
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteStringCM(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteCM(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+IMG_VOID   IMG_CALLCONV DBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker);
+IMG_UINT32 IMG_CALLCONV DBGDrivGetMarker(PDBG_STREAM psStream);
+IMG_BOOL   IMG_CALLCONV DBGDrivIsLastCaptureFrame(PDBG_STREAM psStream);
+IMG_BOOL   IMG_CALLCONV DBGDrivIsCaptureFrame(PDBG_STREAM psStream, IMG_BOOL bCheckPreviousFrame);
+IMG_UINT32 IMG_CALLCONV DBGDrivWriteLF(PDBG_STREAM psStream, IMG_UINT8 *pui8InBuf, IMG_UINT32 ui32InBuffSize, IMG_UINT32 ui32Level, IMG_UINT32 ui32Flags);
+IMG_UINT32 IMG_CALLCONV DBGDrivReadLF(PDBG_STREAM psStream, IMG_UINT32 ui32OutBuffSize, IMG_UINT8 *pui8OutBuf);
+IMG_VOID   IMG_CALLCONV DBGDrivStartInitPhase(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV DBGDrivStopInitPhase(PDBG_STREAM psStream);
+IMG_UINT32 IMG_CALLCONV DBGDrivGetStreamOffset(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV DBGDrivSetStreamOffset(PDBG_STREAM psStream, IMG_UINT32 ui32StreamOffset);
+IMG_VOID   IMG_CALLCONV DBGDrivWaitForEvent(DBG_EVENT eEvent);
+
+IMG_VOID DestroyAllStreams(IMG_VOID);
+
+IMG_UINT32 AtoI(IMG_CHAR *szIn);
+
+IMG_VOID HostMemSet(IMG_VOID *pvDest,IMG_UINT8 ui8Value,IMG_UINT32 ui32Size);
+IMG_VOID HostMemCopy(IMG_VOID *pvDest,IMG_VOID *pvSrc,IMG_UINT32 ui32Size);
+IMG_BOOL StreamValid(PDBG_STREAM psStream);
+IMG_VOID Write(PDBG_STREAM psStream,IMG_UINT8 *pui8Data,IMG_UINT32 ui32InBuffSize);
+IMG_VOID MonoOut(IMG_CHAR * pszString,IMG_BOOL bNewLine);
+
+
+IMG_VOID * IMG_CALLCONV ExtDBGDrivCreateStream(IMG_CHAR *	pszName, IMG_UINT32 ui32CapMode, IMG_UINT32	ui32OutMode, IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivDestroyStream(PDBG_STREAM psStream);
+IMG_VOID * IMG_CALLCONV ExtDBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivReadString(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Limit);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWrite(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivRead(PDBG_STREAM psStream, IMG_BOOL bReadInitBuffer, IMG_UINT32 ui32OutBuffSize,IMG_UINT8 *pui8OutBuf);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivSetCaptureMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode,IMG_UINT32 ui32Start,IMG_UINT32 ui32End,IMG_UINT32 ui32SampleRate);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivSetOutputMode(PDBG_STREAM psStream,IMG_UINT32 ui32OutMode);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivSetDebugLevel(PDBG_STREAM psStream,IMG_UINT32 ui32DebugLevel);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivSetFrame(PDBG_STREAM psStream,IMG_UINT32 ui32Frame);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetFrame(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivOverrideMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivDefaultMode(PDBG_STREAM psStream);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWrite2(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteStringCM(PDBG_STREAM psStream,IMG_CHAR * pszString,IMG_UINT32 ui32Level);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteCM(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize,IMG_UINT32 ui32Level);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetMarker(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivStartInitPhase(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivStopInitPhase(PDBG_STREAM psStream);
+IMG_BOOL   IMG_CALLCONV ExtDBGDrivIsLastCaptureFrame(PDBG_STREAM psStream);
+IMG_BOOL   IMG_CALLCONV ExtDBGDrivIsCaptureFrame(PDBG_STREAM psStream, IMG_BOOL bCheckPreviousFrame);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivWriteLF(PDBG_STREAM psStream, IMG_UINT8 *pui8InBuf, IMG_UINT32 ui32InBuffSize, IMG_UINT32 ui32Level, IMG_UINT32 ui32Flags);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivReadLF(PDBG_STREAM psStream, IMG_UINT32 ui32OutBuffSize, IMG_UINT8 *pui8OutBuf);
+IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetStreamOffset(PDBG_STREAM psStream);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivSetStreamOffset(PDBG_STREAM psStream, IMG_UINT32 ui32StreamOffset);
+IMG_VOID   IMG_CALLCONV ExtDBGDrivWaitForEvent(DBG_EVENT eEvent);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hostfunc.h b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hostfunc.h
new file mode 100644
index 0000000..558d6fa
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hostfunc.h
@@ -0,0 +1,54 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _HOSTFUNC_
+#define _HOSTFUNC_
+
+#define HOST_PAGESIZE			(4096)
+#define DBG_MEMORY_INITIALIZER	(0xe2)
+
+IMG_UINT32 HostReadRegistryDWORDFromString(IMG_CHAR *pcKey, IMG_CHAR *pcValueName, IMG_UINT32 *pui32Data);
+
+IMG_VOID * HostPageablePageAlloc(IMG_UINT32 ui32Pages);
+IMG_VOID HostPageablePageFree(IMG_VOID * pvBase);
+IMG_VOID * HostNonPageablePageAlloc(IMG_UINT32 ui32Pages);
+IMG_VOID HostNonPageablePageFree(IMG_VOID * pvBase);
+
+IMG_VOID * HostMapKrnBufIntoUser(IMG_VOID * pvKrnAddr, IMG_UINT32 ui32Size, IMG_VOID * *ppvMdl);
+IMG_VOID HostUnMapKrnBufFromUser(IMG_VOID * pvUserAddr, IMG_VOID * pvMdl, IMG_VOID * pvProcess);
+
+IMG_VOID HostCreateRegDeclStreams(IMG_VOID);
+
+IMG_VOID * HostCreateMutex(IMG_VOID);
+IMG_VOID HostAquireMutex(IMG_VOID * pvMutex);
+IMG_VOID HostReleaseMutex(IMG_VOID * pvMutex);
+IMG_VOID HostDestroyMutex(IMG_VOID * pvMutex);
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+IMG_INT32 HostCreateEventObjects(IMG_VOID);
+IMG_VOID HostWaitForEvent(DBG_EVENT eEvent);
+IMG_VOID HostSignalEvent(DBG_EVENT eEvent);
+IMG_VOID HostDestroyEventObjects(IMG_VOID);
+#endif
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.c b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.c
new file mode 100644
index 0000000..7ea2b5a
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.c
@@ -0,0 +1,131 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+
+#if !defined(LINUX)
+#include <ntddk.h>
+#include <windef.h>
+#endif
+
+#include "img_types.h"
+#include "pvr_debug.h"
+#include "dbgdrvif.h"
+#include "dbgdriv.h"
+#include "hotkey.h"
+#include "hostfunc.h"
+
+
+
+
+
+IMG_UINT32	g_ui32HotKeyFrame = 0xFFFFFFFF;
+IMG_BOOL	g_bHotKeyPressed = IMG_FALSE;
+IMG_BOOL	g_bHotKeyRegistered = IMG_FALSE;
+
+PRIVATEHOTKEYDATA    g_PrivateHotKeyData;
+
+
+IMG_VOID ReadInHotKeys(IMG_VOID)
+{
+	g_PrivateHotKeyData.ui32ScanCode = 0x58;
+	g_PrivateHotKeyData.ui32ShiftState = 0x0;
+
+
+
+#if 0
+	if (_RegOpenKey(HKEY_LOCAL_MACHINE,pszRegPath,&hKey) == ERROR_SUCCESS)
+	{
+
+
+		QueryReg(hKey,"ui32ScanCode",&g_PrivateHotKeyData.ui32ScanCode);
+		QueryReg(hKey,"ui32ShiftState",&g_PrivateHotKeyData.ui32ShiftState);
+	}
+#else
+	HostReadRegistryDWORDFromString("DEBUG\\Streams", "ui32ScanCode"  , &g_PrivateHotKeyData.ui32ScanCode);
+	HostReadRegistryDWORDFromString("DEBUG\\Streams", "ui32ShiftState", &g_PrivateHotKeyData.ui32ShiftState);
+#endif
+}
+
+IMG_VOID RegisterKeyPressed(IMG_UINT32 dwui32ScanCode, PHOTKEYINFO pInfo)
+{
+	PDBG_STREAM	psStream;
+
+	PVR_UNREFERENCED_PARAMETER(pInfo);
+
+	if (dwui32ScanCode == g_PrivateHotKeyData.ui32ScanCode)
+	{
+		PVR_DPF((PVR_DBG_MESSAGE,"PDUMP Hotkey pressed !\n"));
+
+		psStream = (PDBG_STREAM) g_PrivateHotKeyData.sHotKeyInfo.pvStream;
+
+		if (!g_bHotKeyPressed)
+		{
+
+
+			g_ui32HotKeyFrame = psStream->ui32Current + 2;
+
+
+
+			g_bHotKeyPressed = IMG_TRUE;
+		}
+	}
+}
+
+IMG_VOID ActivateHotKeys(PDBG_STREAM psStream)
+{
+
+
+	ReadInHotKeys();
+
+
+
+	if (!g_PrivateHotKeyData.sHotKeyInfo.hHotKey)
+	{
+		if (g_PrivateHotKeyData.ui32ScanCode != 0)
+		{
+			PVR_DPF((PVR_DBG_MESSAGE,"Activate HotKey for PDUMP.\n"));
+
+
+
+			g_PrivateHotKeyData.sHotKeyInfo.pvStream = psStream;
+
+			DefineHotKey(g_PrivateHotKeyData.ui32ScanCode, g_PrivateHotKeyData.ui32ShiftState, &g_PrivateHotKeyData.sHotKeyInfo);
+		}
+		else
+		{
+			g_PrivateHotKeyData.sHotKeyInfo.hHotKey = 0;
+		}
+	}
+}
+
+IMG_VOID DeactivateHotKeys(IMG_VOID)
+{
+	if (g_PrivateHotKeyData.sHotKeyInfo.hHotKey != 0)
+	{
+		PVR_DPF((PVR_DBG_MESSAGE,"Deactivate HotKey.\n"));
+
+		RemoveHotKey(g_PrivateHotKeyData.sHotKeyInfo.hHotKey);
+		g_PrivateHotKeyData.sHotKeyInfo.hHotKey = 0;
+	}
+}
+
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.h b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.h
new file mode 100644
index 0000000..5f95d22
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/hotkey.h
@@ -0,0 +1,56 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _HOTKEY_
+#define _HOTKEY_
+
+
+typedef struct _hotkeyinfo
+{
+	IMG_UINT8 ui8ScanCode;
+	IMG_UINT8 ui8Type;
+	IMG_UINT8 ui8Flag;
+	IMG_UINT8 ui8Filler1;
+	IMG_UINT32 ui32ShiftState;
+	IMG_UINT32 ui32HotKeyProc;
+	IMG_VOID *pvStream;
+	IMG_UINT32 hHotKey;
+} HOTKEYINFO, *PHOTKEYINFO;
+
+typedef struct _privatehotkeydata
+{
+	IMG_UINT32		ui32ScanCode;
+	IMG_UINT32		ui32ShiftState;
+	HOTKEYINFO	sHotKeyInfo;
+} PRIVATEHOTKEYDATA, *PPRIVATEHOTKEYDATA;
+
+
+IMG_VOID ReadInHotKeys (IMG_VOID);
+IMG_VOID ActivateHotKeys(PDBG_STREAM psStream);
+IMG_VOID DeactivateHotKeys(IMG_VOID);
+
+IMG_VOID RemoveHotKey (IMG_UINT32 hHotKey);
+IMG_VOID DefineHotKey (IMG_UINT32 ui32ScanCode, IMG_UINT32 ui32ShiftState, PHOTKEYINFO psInfo);
+IMG_VOID RegisterKeyPressed (IMG_UINT32 ui32ScanCode, PHOTKEYINFO psInfo);
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.c b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.c
new file mode 100644
index 0000000..6b130d7
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.c
@@ -0,0 +1,367 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+
+
+#ifdef LINUX
+#include <asm/uaccess.h>
+#endif
+
+#include "img_types.h"
+#include "dbgdrvif.h"
+#include "dbgdriv.h"
+#include "hotkey.h"
+
+
+IMG_UINT32 DBGDIOCDrivCreateStream(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_CREATESTREAM psIn;
+	IMG_VOID * *ppvOut;
+	#ifdef LINUX
+	static IMG_CHAR name[32];
+	#endif
+
+	psIn = (PDBG_IN_CREATESTREAM) pvInBuffer;
+	ppvOut = (IMG_VOID * *) pvOutBuffer;
+
+	#ifdef LINUX
+
+	if(copy_from_user(name, psIn->pszName, 32) != 0)
+	{
+		return IMG_FALSE;
+	}
+
+	*ppvOut = ExtDBGDrivCreateStream(name, psIn->ui32CapMode, psIn->ui32OutMode, 0, psIn->ui32Pages);
+
+	#else
+	*ppvOut = ExtDBGDrivCreateStream(psIn->pszName, psIn->ui32CapMode, psIn->ui32OutMode, DEBUG_FLAGS_NO_BUF_EXPANDSION, psIn->ui32Pages);
+	#endif
+
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivDestroyStream(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *		pStream;
+	PDBG_STREAM	psStream;
+
+	pStream = (IMG_UINT32 *) pvInBuffer;
+	psStream = (PDBG_STREAM) *pStream;
+
+	PVR_UNREFERENCED_PARAMETER(	pvOutBuffer);
+
+	ExtDBGDrivDestroyStream(psStream);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivGetStream(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_FINDSTREAM psParams;
+	IMG_UINT32 *	pui32Stream;
+
+	psParams		= (PDBG_IN_FINDSTREAM)pvInBuffer;
+	pui32Stream	= (IMG_UINT32 *)pvOutBuffer;
+
+	*pui32Stream = (IMG_UINT32)ExtDBGDrivFindStream(psParams->pszName, psParams->bResetStream);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWriteString(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_WRITESTRING psParams;
+	IMG_UINT32 *				pui32OutLen;
+
+	psParams = (PDBG_IN_WRITESTRING) pvInBuffer;
+	pui32OutLen = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32OutLen = ExtDBGDrivWriteString((PDBG_STREAM) psParams->pvStream,psParams->pszString,psParams->ui32Level);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWriteStringCM(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_WRITESTRING psParams;
+	IMG_UINT32 *				pui32OutLen;
+
+	psParams = (PDBG_IN_WRITESTRING) pvInBuffer;
+	pui32OutLen = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32OutLen = ExtDBGDrivWriteStringCM((PDBG_STREAM) psParams->pvStream,psParams->pszString,psParams->ui32Level);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivReadString(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *				pui32OutLen;
+	PDBG_IN_READSTRING	psParams;
+
+	psParams = (PDBG_IN_READSTRING) pvInBuffer;
+	pui32OutLen = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32OutLen = ExtDBGDrivReadString(psParams->pvStream,psParams->pszString,psParams->ui32StringLen);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWrite(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *				pui32BytesCopied;
+	PDBG_IN_WRITE		psInParams;
+
+	psInParams = (PDBG_IN_WRITE) pvInBuffer;
+	pui32BytesCopied = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32BytesCopied = ExtDBGDrivWrite((PDBG_STREAM) psInParams->pvStream,psInParams->pui8InBuffer,psInParams->ui32TransferSize,psInParams->ui32Level);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWrite2(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *				pui32BytesCopied;
+	PDBG_IN_WRITE		psInParams;
+
+	psInParams = (PDBG_IN_WRITE) pvInBuffer;
+	pui32BytesCopied = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32BytesCopied = ExtDBGDrivWrite2((PDBG_STREAM) psInParams->pvStream,psInParams->pui8InBuffer,psInParams->ui32TransferSize,psInParams->ui32Level);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWriteCM(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *				pui32BytesCopied;
+	PDBG_IN_WRITE		psInParams;
+
+	psInParams = (PDBG_IN_WRITE) pvInBuffer;
+	pui32BytesCopied = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32BytesCopied = ExtDBGDrivWriteCM((PDBG_STREAM) psInParams->pvStream,psInParams->pui8InBuffer,psInParams->ui32TransferSize,psInParams->ui32Level);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivRead(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *				pui32BytesCopied;
+	PDBG_IN_READ		psInParams;
+
+	psInParams = (PDBG_IN_READ) pvInBuffer;
+	pui32BytesCopied = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32BytesCopied = ExtDBGDrivRead((PDBG_STREAM) psInParams->pvStream,psInParams->bReadInitBuffer, psInParams->ui32OutBufferSize,psInParams->pui8OutBuffer);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivSetCaptureMode(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_SETDEBUGMODE 	psParams;
+
+	psParams = (PDBG_IN_SETDEBUGMODE) pvInBuffer;
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivSetCaptureMode((PDBG_STREAM) psParams->pvStream,
+						  psParams->ui32Mode,
+						  psParams->ui32Start,
+						  psParams->ui32End,
+						  psParams->ui32SampleRate);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivSetOutMode(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_SETDEBUGOUTMODE psParams;
+
+	psParams = (PDBG_IN_SETDEBUGOUTMODE) pvInBuffer;
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivSetOutputMode((PDBG_STREAM) psParams->pvStream,psParams->ui32Mode);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivSetDebugLevel(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_SETDEBUGLEVEL psParams;
+
+	psParams = (PDBG_IN_SETDEBUGLEVEL) pvInBuffer;
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivSetDebugLevel((PDBG_STREAM) psParams->pvStream,psParams->ui32Level);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivSetFrame(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_SETFRAME	psParams;
+
+	psParams = (PDBG_IN_SETFRAME) pvInBuffer;
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivSetFrame((PDBG_STREAM) psParams->pvStream,psParams->ui32Frame);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivGetFrame(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *		pStream;
+	PDBG_STREAM	psStream;
+	IMG_UINT32 *		pui32Current;
+
+	pStream = (IMG_UINT32 *) pvInBuffer;
+	psStream = (PDBG_STREAM) *pStream;
+	pui32Current = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32Current = ExtDBGDrivGetFrame(psStream);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivIsCaptureFrame(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_ISCAPTUREFRAME psParams;
+	IMG_UINT32 *		pui32Current;
+
+	psParams = (PDBG_IN_ISCAPTUREFRAME) pvInBuffer;
+	pui32Current = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32Current = ExtDBGDrivIsCaptureFrame((PDBG_STREAM) psParams->pvStream, psParams->bCheckPreviousFrame);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivOverrideMode(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_OVERRIDEMODE	psParams;
+
+	psParams = (PDBG_IN_OVERRIDEMODE) pvInBuffer;
+	PVR_UNREFERENCED_PARAMETER(	pvOutBuffer);
+
+	ExtDBGDrivOverrideMode((PDBG_STREAM) psParams->pvStream,psParams->ui32Mode);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivDefaultMode(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *		pStream;
+	PDBG_STREAM	psStream;
+
+	pStream = (IMG_UINT32 *) pvInBuffer;
+	psStream = (PDBG_STREAM) *pStream;
+
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivDefaultMode(psStream);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivSetMarker(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_SETMARKER	psParams;
+
+	psParams = (PDBG_IN_SETMARKER) pvInBuffer;
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivSetMarker((PDBG_STREAM) psParams->pvStream, psParams->ui32Marker);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivGetMarker(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *		pStream;
+	PDBG_STREAM	psStream;
+	IMG_UINT32 *		pui32Current;
+
+	pStream = (IMG_UINT32 *) pvInBuffer;
+	psStream = (PDBG_STREAM) *pStream;
+	pui32Current = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32Current = ExtDBGDrivGetMarker(psStream);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivGetServiceTable(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *	pui32Out;
+
+	PVR_UNREFERENCED_PARAMETER(pvInBuffer);
+	pui32Out = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32Out = DBGDrivGetServiceTable();
+
+    return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWriteLF(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	PDBG_IN_WRITE_LF	psInParams;
+	IMG_UINT32 *				pui32BytesCopied;
+
+	psInParams = (PDBG_IN_WRITE_LF) pvInBuffer;
+	pui32BytesCopied = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32BytesCopied = ExtDBGDrivWriteLF(psInParams->pvStream,
+										psInParams->pui8InBuffer,
+										psInParams->ui32BufferSize,
+										psInParams->ui32Level,
+										psInParams->ui32Flags);
+
+	return IMG_TRUE;
+}
+
+IMG_UINT32 DBGDIOCDrivReadLF(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	IMG_UINT32 *				pui32BytesCopied;
+	PDBG_IN_READ		psInParams;
+
+	psInParams = (PDBG_IN_READ) pvInBuffer;
+	pui32BytesCopied = (IMG_UINT32 *) pvOutBuffer;
+
+	*pui32BytesCopied = ExtDBGDrivReadLF((PDBG_STREAM) psInParams->pvStream,psInParams->ui32OutBufferSize,psInParams->pui8OutBuffer);
+
+	return(IMG_TRUE);
+}
+
+IMG_UINT32 DBGDIOCDrivWaitForEvent(IMG_VOID * pvInBuffer, IMG_VOID * pvOutBuffer)
+{
+	DBG_EVENT eEvent = (DBG_EVENT)(*(IMG_UINT32 *)pvInBuffer);
+
+	PVR_UNREFERENCED_PARAMETER(pvOutBuffer);
+
+	ExtDBGDrivWaitForEvent(eEvent);
+
+	return(IMG_TRUE);
+}
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.h b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.h
new file mode 100644
index 0000000..882408c
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/common/ioctl.h
@@ -0,0 +1,83 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef _IOCTL_
+#define _IOCTL_
+
+
+IMG_UINT32 DBGDIOCDrivCreateStream(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivDestroyStream(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivGetStream(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWriteString(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivReadString(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWrite(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWrite2(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivRead(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivSetCaptureMode(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivSetOutMode(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivSetDebugLevel(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivSetFrame(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivGetFrame(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivOverrideMode(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivDefaultMode(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivGetServiceTable(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWriteStringCM(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWriteCM(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivSetMarker(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivGetMarker(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivIsCaptureFrame(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWriteLF(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivReadLF(IMG_VOID *, IMG_VOID *);
+IMG_UINT32 DBGDIOCDrivWaitForEvent(IMG_VOID*, IMG_VOID *);
+
+IMG_UINT32 (*g_DBGDrivProc[])(IMG_VOID *, IMG_VOID *) =
+{
+	DBGDIOCDrivCreateStream,
+	DBGDIOCDrivDestroyStream,
+	DBGDIOCDrivGetStream,
+	DBGDIOCDrivWriteString,
+	DBGDIOCDrivReadString,
+	DBGDIOCDrivWrite,
+	DBGDIOCDrivRead,
+	DBGDIOCDrivSetCaptureMode,
+	DBGDIOCDrivSetOutMode,
+	DBGDIOCDrivSetDebugLevel,
+	DBGDIOCDrivSetFrame,
+	DBGDIOCDrivGetFrame,
+	DBGDIOCDrivOverrideMode,
+	DBGDIOCDrivDefaultMode,
+	DBGDIOCDrivGetServiceTable,
+	DBGDIOCDrivWrite2,
+	DBGDIOCDrivWriteStringCM,
+	DBGDIOCDrivWriteCM,
+	DBGDIOCDrivSetMarker,
+	DBGDIOCDrivGetMarker,
+	DBGDIOCDrivIsCaptureFrame,
+	DBGDIOCDrivWriteLF,
+	DBGDIOCDrivReadLF,
+	DBGDIOCDrivWaitForEvent
+};
+
+#define MAX_DBGVXD_W32_API (sizeof(g_DBGDrivProc)/sizeof(IMG_UINT32))
+
+#endif
+
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/hostfunc.c b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/hostfunc.c
new file mode 100644
index 0000000..29c3427
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/hostfunc.c
@@ -0,0 +1,300 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/version.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/string.h>
+#include <asm/page.h>
+#include <linux/vmalloc.h>
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
+#include <linux/mutex.h>
+#else
+#include <asm/semaphore.h>
+#endif
+#include <linux/hardirq.h>
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+#include <linux/sched.h>
+#include <linux/wait.h>
+#include <linux/jiffies.h>
+#include <linux/delay.h>
+#endif
+
+#include <linux/slab.h>
+
+#include "img_types.h"
+#include "pvr_debug.h"
+
+#include "dbgdrvif.h"
+#include "dbgdriv/common/hostfunc.h"
+
+#if !defined(SUPPORT_DRI_DRM)
+IMG_UINT32	gPVRDebugLevel = DBGPRIV_WARNING;
+
+#define PVR_STRING_TERMINATOR		'\0'
+#define PVR_IS_FILE_SEPARATOR(character) ( ((character) == '\\') || ((character) == '/') )
+
+void PVRSRVDebugPrintf	(
+						IMG_UINT32	ui32DebugLevel,
+						const IMG_CHAR*	pszFileName,
+						IMG_UINT32	ui32Line,
+						const IMG_CHAR*	pszFormat,
+						...
+					)
+{
+	IMG_BOOL bTrace, bDebug;
+#if !defined(__sh__)
+	IMG_CHAR *pszLeafName;
+
+	pszLeafName = (char *)strrchr (pszFileName, '\\');
+
+	if (pszLeafName)
+	{
+		pszFileName = pszLeafName;
+	}
+#endif
+
+	bTrace = gPVRDebugLevel & ui32DebugLevel & DBGPRIV_CALLTRACE;
+	bDebug = ((gPVRDebugLevel & DBGPRIV_ALLLEVELS) >= ui32DebugLevel);
+
+	if (bTrace || bDebug)
+	{
+		va_list vaArgs;
+		static char szBuffer[256];
+
+		va_start (vaArgs, pszFormat);
+
+
+		if (bDebug)
+		{
+			switch(ui32DebugLevel)
+			{
+				case DBGPRIV_FATAL:
+				{
+					strcpy (szBuffer, "PVR_K:(Fatal): ");
+					break;
+				}
+				case DBGPRIV_ERROR:
+				{
+					strcpy (szBuffer, "PVR_K:(Error): ");
+					break;
+				}
+				case DBGPRIV_WARNING:
+				{
+					strcpy (szBuffer, "PVR_K:(Warning): ");
+					break;
+				}
+				case DBGPRIV_MESSAGE:
+				{
+					strcpy (szBuffer, "PVR_K:(Message): ");
+					break;
+				}
+				case DBGPRIV_VERBOSE:
+				{
+					strcpy (szBuffer, "PVR_K:(Verbose): ");
+					break;
+				}
+				default:
+				{
+					strcpy (szBuffer, "PVR_K:(Unknown message level)");
+					break;
+				}
+			}
+		}
+		else
+		{
+			strcpy (szBuffer, "PVR_K: ");
+		}
+
+		vsprintf (&szBuffer[strlen(szBuffer)], pszFormat, vaArgs);
+
+
+
+		if (!bTrace)
+		{
+			sprintf (&szBuffer[strlen(szBuffer)], " [%d, %s]", (int)ui32Line, pszFileName);
+		}
+
+		printk(KERN_INFO "%s\r\n", szBuffer);
+
+		va_end (vaArgs);
+	}
+}
+#endif
+
+IMG_VOID HostMemSet(IMG_VOID *pvDest, IMG_UINT8 ui8Value, IMG_UINT32 ui32Size)
+{
+	memset(pvDest, (int) ui8Value, (size_t) ui32Size);
+}
+
+IMG_VOID HostMemCopy(IMG_VOID *pvDst, IMG_VOID *pvSrc, IMG_UINT32 ui32Size)
+{
+#if defined(USE_UNOPTIMISED_MEMCPY)
+    unsigned char *src,*dst;
+    int i;
+
+    src=(unsigned char *)pvSrc;
+    dst=(unsigned char *)pvDst;
+    for(i=0;i<ui32Size;i++)
+    {
+        dst[i]=src[i];
+    }
+#else
+    memcpy(pvDst, pvSrc, ui32Size);
+#endif
+}
+
+IMG_UINT32 HostReadRegistryDWORDFromString(char *pcKey, char *pcValueName, IMG_UINT32 *pui32Data)
+{
+
+	return 0;
+}
+
+IMG_VOID * HostPageablePageAlloc(IMG_UINT32 ui32Pages)
+{
+    return (void*)vmalloc(ui32Pages * PAGE_SIZE);
+}
+
+IMG_VOID HostPageablePageFree(IMG_VOID * pvBase)
+{
+    vfree(pvBase);
+}
+
+IMG_VOID * HostNonPageablePageAlloc(IMG_UINT32 ui32Pages)
+{
+    return (void*)vmalloc(ui32Pages * PAGE_SIZE);
+}
+
+IMG_VOID HostNonPageablePageFree(IMG_VOID * pvBase)
+{
+    vfree(pvBase);
+}
+
+IMG_VOID * HostMapKrnBufIntoUser(IMG_VOID * pvKrnAddr, IMG_UINT32 ui32Size, IMG_VOID **ppvMdl)
+{
+
+	return IMG_NULL;
+}
+
+IMG_VOID HostUnMapKrnBufFromUser(IMG_VOID * pvUserAddr, IMG_VOID * pvMdl, IMG_VOID * pvProcess)
+{
+
+}
+
+IMG_VOID HostCreateRegDeclStreams(IMG_VOID)
+{
+
+}
+
+IMG_VOID * HostCreateMutex(IMG_VOID)
+{
+	struct semaphore *psSem;
+
+	psSem = kmalloc(sizeof(*psSem), GFP_KERNEL);
+	if (psSem)
+	{
+		init_MUTEX(psSem);
+	}
+
+	return psSem;
+}
+
+IMG_VOID HostAquireMutex(IMG_VOID * pvMutex)
+{
+	BUG_ON(in_interrupt());
+
+#if defined(PVR_DEBUG_DBGDRV_DETECT_HOST_MUTEX_COLLISIONS)
+	if (down_trylock((struct semaphore *)pvMutex))
+	{
+		printk(KERN_INFO "HostAquireMutex: Waiting for mutex\n");
+		down((struct semaphore *)pvMutex);
+	}
+#else
+	down((struct semaphore *)pvMutex);
+#endif
+}
+
+IMG_VOID HostReleaseMutex(IMG_VOID * pvMutex)
+{
+	up((struct semaphore *)pvMutex);
+}
+
+IMG_VOID HostDestroyMutex(IMG_VOID * pvMutex)
+{
+	if (pvMutex)
+	{
+		kfree(pvMutex);
+	}
+}
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+
+#define	EVENT_WAIT_TIMEOUT_MS	500
+#define	EVENT_WAIT_TIMEOUT_JIFFIES	(EVENT_WAIT_TIMEOUT_MS * HZ / 1000)
+
+static int iStreamData;
+static wait_queue_head_t sStreamDataEvent;
+
+IMG_INT32 HostCreateEventObjects(IMG_VOID)
+{
+	init_waitqueue_head(&sStreamDataEvent);
+
+	return 0;
+}
+
+IMG_VOID HostWaitForEvent(DBG_EVENT eEvent)
+{
+	switch(eEvent)
+	{
+		case DBG_EVENT_STREAM_DATA:
+
+			wait_event_interruptible_timeout(sStreamDataEvent, iStreamData != 0, EVENT_WAIT_TIMEOUT_JIFFIES);
+			iStreamData = 0;
+			break;
+		default:
+
+			msleep_interruptible(EVENT_WAIT_TIMEOUT_MS);
+			break;
+	}
+}
+
+IMG_VOID HostSignalEvent(DBG_EVENT eEvent)
+{
+	switch(eEvent)
+	{
+		case DBG_EVENT_STREAM_DATA:
+			iStreamData = 1;
+			wake_up_interruptible(&sStreamDataEvent);
+			break;
+		default:
+			break;
+	}
+}
+
+IMG_VOID HostDestroyEventObjects(IMG_VOID)
+{
+}
+#endif
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/kbuild/Makefile b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/kbuild/Makefile
new file mode 100644
index 0000000..44c1357
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/kbuild/Makefile
@@ -0,0 +1,33 @@
+#
+# Copyright (c) Imagination Technologies Ltd.
+#
+# 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 shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE. 
+# 
+#
+#
+
+include $(EURASIAROOT)/eurasiacon/build/linux/kbuild/Makefile.kbuild_subdir_common
+
+MODULE		= dbgdrv
+
+INCLUDES =
+
+SOURCES	=
+				
+include $(EURASIAROOT)/tools/intern/debug/dbgdriv/linux/makefile.linux.common
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/main.c b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/main.c
new file mode 100644
index 0000000..08691c0
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/main.c
@@ -0,0 +1,294 @@
+/**********************************************************************
+ Copyright (c) Imagination Technologies Ltd.
+
+ 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 shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ ******************************************************************************/
+
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/kdev_t.h>
+#include <linux/pci.h>
+#include <linux/list.h>
+#include <linux/init.h>
+#include <linux/vmalloc.h>
+#include <linux/version.h>
+
+#if defined(LDM_PLATFORM) && !defined(SUPPORT_DRI_DRM)
+#include <linux/platform_device.h>
+#endif
+
+#if defined(LDM_PCI) && !defined(SUPPORT_DRI_DRM)
+#include <linux/pci.h>
+#endif
+
+#include <asm/uaccess.h>
+
+#if defined(SUPPORT_DRI_DRM)
+#include "drmP.h"
+#include "drm.h"
+#endif
+
+#include "img_types.h"
+#include "client/linuxsrv.h"
+#include "dbgdriv/common/ioctl.h"
+#include "dbgdrvif.h"
+#include "dbgdriv/common/dbgdriv.h"
+#include "dbgdriv/common/hostfunc.h"
+#include "pvr_debug.h"
+#include "pvrmodule.h"
+
+#if defined(SUPPORT_DRI_DRM)
+
+#include "pvr_drm_shared.h"
+#include "pvr_drm.h"
+
+#else
+
+#define DRVNAME "dbgdrv"
+MODULE_SUPPORTED_DEVICE(DRVNAME);
+
+#if (defined(LDM_PLATFORM) || defined(LDM_PCI)) && !defined(SUPPORT_DRI_DRM)
+static struct class *psDbgDrvClass;
+#endif
+
+static int AssignedMajorNumber = 0;
+
+long dbgdrv_ioctl(struct file *, unsigned int, unsigned long);
+
+static int dbgdrv_open(struct inode unref__ * pInode, struct file unref__ * pFile)
+{
+	return 0;
+}
+
+static int dbgdrv_release(struct inode unref__ * pInode, struct file unref__ * pFile)
+{
+	return 0;
+}
+
+static int dbgdrv_mmap(struct file* pFile, struct vm_area_struct* ps_vma)
+{
+	return 0;
+}
+
+static struct file_operations dbgdrv_fops = {
+	.owner          = THIS_MODULE,
+	.unlocked_ioctl = dbgdrv_ioctl,
+	.open           = dbgdrv_open,
+	.release        = dbgdrv_release,
+	.mmap           = dbgdrv_mmap,
+};
+
+#endif
+
+void DBGDrvGetServiceTable(void **fn_table)
+{
+	extern DBGKM_SERVICE_TABLE g_sDBGKMServices;
+
+	*fn_table = &g_sDBGKMServices;
+}
+
+#if defined(SUPPORT_DRI_DRM)
+void dbgdrv_cleanup(void)
+#else
+void cleanup_module(void)
+#endif
+{
+#if !defined(SUPPORT_DRI_DRM)
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+	device_destroy(psDbgDrvClass, MKDEV(AssignedMajorNumber, 0));
+	class_destroy(psDbgDrvClass);
+#endif
+	unregister_chrdev(AssignedMajorNumber, DRVNAME);
+#endif
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+	HostDestroyEventObjects();
+#endif
+	HostDestroyMutex(g_pvAPIMutex);
+	return;
+}
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT dbgdrv_init(void)
+#else
+int init_module(void)
+#endif
+{
+#if (defined(LDM_PLATFORM) || defined(LDM_PCI)) && !defined(SUPPORT_DRI_DRM)
+	struct device *psDev;
+#endif
+
+#if !defined(SUPPORT_DRI_DRM)
+	int err = -EBUSY;
+#endif
+
+
+	if ((g_pvAPIMutex=HostCreateMutex()) == IMG_NULL)
+	{
+		return -ENOMEM;
+	}
+
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+
+	(void) HostCreateEventObjects();
+#endif
+
+#if !defined(SUPPORT_DRI_DRM)
+	AssignedMajorNumber =
+	register_chrdev(AssignedMajorNumber, DRVNAME, &dbgdrv_fops);
+
+	if (AssignedMajorNumber <= 0)
+	{
+		PVR_DPF((PVR_DBG_ERROR," unable to get major\n"));
+		goto ErrDestroyEventObjects;
+	}
+
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+
+	psDbgDrvClass = class_create(THIS_MODULE, DRVNAME);
+	if (IS_ERR(psDbgDrvClass))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: unable to create class (%ld)",
+				 __func__, PTR_ERR(psDbgDrvClass)));
+		goto ErrUnregisterCharDev;
+	}
+
+	psDev = device_create(psDbgDrvClass, NULL, MKDEV(AssignedMajorNumber, 0),
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26))
+						  NULL,
+#endif
+						  DRVNAME);
+	if (IS_ERR(psDev))
+	{
+		PVR_DPF((PVR_DBG_ERROR, "%s: unable to create device (%ld)",
+								__func__, PTR_ERR(psDev)));
+		goto ErrDestroyClass;
+	}
+#endif
+#endif
+
+	return 0;
+
+#if !defined(SUPPORT_DRI_DRM)
+ErrDestroyEventObjects:
+#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
+	HostDestroyEventObjects();
+#endif
+#if defined(LDM_PLATFORM) || defined(LDM_PCI)
+ErrUnregisterCharDev:
+	unregister_chrdev(AssignedMajorNumber, DRVNAME);
+ErrDestroyClass:
+	class_destroy(psDbgDrvClass);
+#endif
+	return err;
+#endif
+}
+
+#if defined(SUPPORT_DRI_DRM)
+IMG_INT dbgdrv_ioctl(struct drm_device *dev, IMG_VOID *arg, struct drm_file *pFile)
+#else
+long dbgdrv_ioctl(struct file *file, unsigned int ioctlCmd, unsigned long arg)
+#endif
+{
+	IOCTL_PACKAGE *pIP = (IOCTL_PACKAGE *) arg;
+	char *buffer, *in, *out;
+	unsigned int cmd;
+
+	if((pIP->ui32InBufferSize > (PAGE_SIZE >> 1) ) || (pIP->ui32OutBufferSize > (PAGE_SIZE >> 1)))
+	{
+		PVR_DPF((PVR_DBG_ERROR,"Sizes of the buffers are too large, cannot do ioctl\n"));
+		return -1;
+	}
+
+	buffer = (char *) HostPageablePageAlloc(1);
+	if(!buffer)
+	{
+		PVR_DPF((PVR_DBG_ERROR,"Failed to allocate buffer, cannot do ioctl\n"));
+		return -EFAULT;
+	}
+
+	in = buffer;
+	out = buffer + (PAGE_SIZE >>1);
+
+	if(copy_from_user(in, pIP->pInBuffer, pIP->ui32InBufferSize) != 0)
+	{
+		goto init_failed;
+	}
+
+	cmd = ((pIP->ui32Cmd >> 2) & 0xFFF) - 0x801;
+
+	if(pIP->ui32Cmd == DEBUG_SERVICE_READ)
+	{
+		IMG_CHAR *ui8Tmp;
+		IMG_UINT32 *pui32BytesCopied = (IMG_UINT32 *)out;
+		DBG_IN_READ *psReadInParams = (DBG_IN_READ *)in;
+
+		ui8Tmp = vmalloc(psReadInParams->ui32OutBufferSize);
+
+		if(!ui8Tmp)
+		{
+			goto init_failed;
+		}
+
+		*pui32BytesCopied = ExtDBGDrivRead((DBG_STREAM *)psReadInParams->pvStream,
+										   psReadInParams->bReadInitBuffer,
+										   psReadInParams->ui32OutBufferSize,
+										   ui8Tmp);
+
+		if(copy_to_user(psReadInParams->pui8OutBuffer,
+						ui8Tmp,
+						*pui32BytesCopied) != 0)
+		{
+			vfree(ui8Tmp);
+			goto init_failed;
+		}
+
+		vfree(ui8Tmp);
+	}
+	else
+	{
+		(g_DBGDrivProc[cmd])(in, out);
+	}
+
+	if(copy_to_user(pIP->pOutBuffer, out, pIP->ui32OutBufferSize) != 0)
+	{
+		goto init_failed;
+	}
+
+	HostPageablePageFree((IMG_VOID *)buffer);
+	return 0;
+
+init_failed:
+	HostPageablePageFree((IMG_VOID *)buffer);
+	return -EFAULT;
+}
+
+
+void RemoveHotKey(unsigned hHotKey)
+{
+
+}
+
+void DefineHotKey(unsigned ScanCode, unsigned ShiftState, void *pInfo)
+{
+
+}
+
+EXPORT_SYMBOL(DBGDrvGetServiceTable);
diff --git a/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/makefile.linux.common b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/makefile.linux.common
new file mode 100644
index 0000000..f9a1c7f
--- /dev/null
+++ b/drivers/gpu/drm/emgd/pvr/tools/intern/debug/dbgdriv/linux/makefile.linux.common
@@ -0,0 +1,38 @@
+#
+# Copyright (c) Imagination Technologies Ltd.
+#
+# 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 shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE. 
+# 
+#
+#
+
+ifeq ($(SUPPORT_DRI_DRM),1)
+DBGDRV_SOURCES_ROOT = $(KBUILDROOT)/../tools/intern/debug/dbgdriv
+else
+DBGDRV_SOURCES_ROOT = ../..
+endif
+
+INCLUDES += 	-I$(EURASIAROOT)/include4 \
+			-I$(EURASIAROOT)/tools/intern/debug
+
+SOURCES	+=	$(DBGDRV_SOURCES_ROOT)/linux/main.c \
+				$(DBGDRV_SOURCES_ROOT)/common/dbgdriv.c \
+				$(DBGDRV_SOURCES_ROOT)/common/ioctl.c \
+				$(DBGDRV_SOURCES_ROOT)/linux/hostfunc.c \
+				$(DBGDRV_SOURCES_ROOT)/common/hotkey.c
-- 
1.7.5.4