aboutsummaryrefslogtreecommitdiffstats
path: root/vxd_svga.c
blob: 550db463f9898d5ae037e03e017018a13eba9d17 (plain) (blame)
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
/*****************************************************************************

Copyright (c) 2024 Jaroslav Hensl <emulator@emulace.cz>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*****************************************************************************/

/* 32 bit RING-0 code for SVGA-II 3D acceleration */
#define SVGA

#include <limits.h>

#include "winhack.h"
#include "vmm.h"
#include "vxd.h"
#include "vpicd.h"
#include "vxd_lib.h"

#include "svga_all.h"

#include "3d_accel.h"

#include "code32.h"

#include "vxd_svga.h"

#include "vxd_strings.h"

#include "svga_ver.h"

#include "vxd_color.h"

/*
 * consts
 */
#define SVGA3D_MAX_MOBS (SVGA3D_MAX_CONTEXT_IDS + SVGA3D_MAX_CONTEXT_IDS + SVGA3D_MAX_SURFACE_IDS)

#define SVGA_MIN_VRAM 8

#define SVGA_TIMEOUT 0x1000000UL

BOOL CB_queue_check(SVGACBHeader *tracked);

/*
 * globals
 */
extern FBHDA_t *hda;
extern ULONG hda_sem;
static SVGA_DB_t *svga_db = NULL;

extern LONG fb_lock_cnt;

       BOOL gb_support = FALSE;
       BOOL cb_support = FALSE;
       BOOL cb_context0 = FALSE;

/* for GPU9 is FIFO more stable (VMWARE) or faster (VBOX) */
static DWORD prefer_fifo = 1;

static BOOL SVGA_is_valid = FALSE;

BOOL surface_dirty = FALSE;

static DWORD fence_next_id = 1;
void *cmdbuf = NULL;
void *ctlbuf = NULL;

DWORD async_mobs = 1;
DWORD hw_cursor  = 0;

/* vxd_mouse.vxd */
BOOL mouse_get_rect(DWORD *ptr_left, DWORD *ptr_top,
	DWORD *ptr_right, DWORD *ptr_bottom);


//DWORD present_fence = 0;
/*
 * strings
 */
static char SVGA_conf_path[] = "Software\\VMWSVGA";
static char SVGA_conf_hw_cursor[]  = "HWCursor";
/*	^ recovered */
static char SVGA_conf_vram_limit[] = "VRAMLimit";
static char SVGA_conf_rgb565bug[]  = "RGB565bug";
static char SVGA_conf_cb[]         = "CommandBuffers";
static char SVGA_conf_pref_fifo[]  = "PreferFIFO";
static char SVGA_conf_hw_version[] = "HWVersion";
static char SVGA_vxd_name[]        = "vmwsmini.vxd";

static char SVGA_conf_disable_multisample[] = "NoMultisample";
static char SVGA_conf_async_mobs[] = "AsyncMOBs";

typedef struct _svga_saved_state_t
{
	BOOL enabled;
	DWORD width;
	DWORD height;
	DWORD bpp;
} svga_saved_state_t;

static svga_saved_state_t svga_saved_state = {FALSE};

/**
 * Notify virtual HW that is some work to do
 **/
void SVGA_Sync()
{
	SVGA_WriteReg(SVGA_REG_SYNC, 1);
}

/* map physical addresses to system linear space */
void SVGA_MapIO()
{
	if(SVGA_IsSVGA3())
	{
		gSVGA.rmmio_linear = _MapPhysToLinear(gSVGA.fifoPhy, gSVGA.fifoSize, 0);
		gSVGA.rmmio = (uint32 FARP*)gSVGA.rmmio_linear;
	}
}

static char db_mutexname[] = "svga_db_mux";

static void SVGA_DB_alloc()
{
	DWORD size;
	BYTE *mem = NULL;
	
	DWORD max_regions = SVGA3D_MAX_MOBS; ///SVGA_ReadReg(SVGA_REG_GMR_MAX_IDS);
	
	DWORD regions_map_size = ((max_regions + 31) >> 3) & 0xFFFFFFFCUL;
	DWORD contexts_map_size = ((SVGA3D_MAX_CONTEXT_IDS + 31) >> 3) & 0xFFFFFFFCUL;
	DWORD surfaces_map_size = ((SVGA3D_MAX_SURFACE_IDS + 31) >> 3) & 0xFFFFFFFCUL;
	
	size = max_regions * sizeof(SVGA_DB_region_t) +
	  SVGA3D_MAX_CONTEXT_IDS * sizeof(SVGA_DB_context_t) +
	  SVGA3D_MAX_SURFACE_IDS * sizeof(SVGA_DB_surface_t) +
	  sizeof(SVGA_DB_t) +
	  regions_map_size + contexts_map_size + surfaces_map_size;
	  
	svga_db = (SVGA_DB_t*)_PageAllocate(RoundToPages(size), PG_VM, ThisVM, 0, 0x0, 0x100000, NULL, PAGEFIXED);
	if(svga_db)
	{
		memset(svga_db, 0, size);
		
		mem = (BYTE*)(svga_db+1);
		
		svga_db->regions = (SVGA_DB_region_t*)mem;
		svga_db->regions_cnt = max_regions;
		mem += svga_db->regions_cnt * sizeof(SVGA_DB_region_t);
		
		svga_db->contexts = (SVGA_DB_context_t*)mem;
		svga_db->contexts_cnt =  SVGA3D_MAX_CONTEXT_IDS;
		mem += svga_db->contexts_cnt * sizeof(SVGA_DB_context_t);
		
		svga_db->surfaces = (SVGA_DB_surface_t*)mem;
		svga_db->surfaces_cnt = SVGA3D_MAX_SURFACE_IDS;
		mem += svga_db->surfaces_cnt * sizeof(SVGA_DB_surface_t);
		
		svga_db->regions_map = (DWORD*)mem;
		mem += regions_map_size;
		
		svga_db->contexts_map = (DWORD*)mem;
		mem += contexts_map_size;
		
		svga_db->surfaces_map = (DWORD*)mem;
	  mem += surfaces_map_size;
		
		memcpy(svga_db->mutexname, &(db_mutexname[0]), sizeof(db_mutexname));
		
		memset(svga_db->regions_map,  0xFF, regions_map_size);
		memset(svga_db->contexts_map, 0xFF, contexts_map_size);
		memset(svga_db->surfaces_map, 0xFF, surfaces_map_size);
			
		svga_db->stat_regions_usage = 0;
	}
}

void SVGA_region_usage_reset()
{
	svga_db->stat_regions_usage = 0;
}

SVGA_DB_surface_t *SVGA_GetSurfaceInfo(DWORD sid)
{
	if(sid > 0 && sid < SVGA3D_MAX_SURFACE_IDS)	
		return &(svga_db->surfaces[sid-1]);
	
	return NULL;
}

SVGA_DB_t *SVGA_DB_setup()
{
	return svga_db;
}

DWORD SVGA_fence_passed()
{
	if(SVGA_IsSVGA3())
	{
		return SVGA_ReadReg(SVGA_REG_FENCE);
	}
	
	return gSVGA.fifoMem[SVGA_FIFO_FENCE];
}

DWORD SVGA_fence_get()
{
	if(fence_next_id == 0)
	{
		/* fence overflow drain all FIFO */  
		SVGA_Flush();
		
		fence_next_id = 1;
		dbg_printf(dbg_fence_overflow);
	}
	
	return fence_next_id++;
}

void SVGA_fence_query(DWORD FBPTR ptr_fence_passed, DWORD FBPTR ptr_fence_last)
{
	if(ptr_fence_passed)
	{
		*ptr_fence_passed = SVGA_fence_passed();
	}
	
	if(ptr_fence_last)
	{
		*ptr_fence_last = fence_next_id-1;
	
		if(*ptr_fence_last == 0)
		{
			*ptr_fence_last = ULONG_MAX;
		}
	}
}

BOOL SVGA_fence_is_passed(DWORD fence_id)
{
	DWORD last_pased;
	DWORD last_fence;
	
	SVGA_fence_query(&last_pased, &last_fence);
	if(fence_id > last_fence)
	{
		return TRUE;
	}
		
	if(fence_id <= last_pased)
	{
		return TRUE;
	}
	
	return FALSE;
}

#ifndef DBGPRINT
void SVGA_fence_wait(DWORD fence_id)
#else
void SVGA_fence_wait_dbg(DWORD fence_id, int line)
#endif
{
//	dbg_printf(dbg_fence_wait, fence_id, line);
	
	for(;;)
	{
		if(SVGA_fence_is_passed(fence_id))
		{
			break;
		}
		
#if 1
		if(cb_support && cb_context0)
		{
			if(CB_queue_check(NULL))
			{
				/* waiting for fence but command queue is empty */
				SVGA_Flush();
				return;
			}
		}
#endif
		SVGA_Sync();
	}
}

void *SVGA_cmd_ptr(DWORD *buf, DWORD *pOffset, DWORD cmd, DWORD cmdsize)
{
	DWORD pp = (*pOffset)/sizeof(DWORD);
	buf[pp] = cmd;
	*pOffset = (*pOffset) + sizeof(DWORD) + cmdsize;
	
	return (void*)(buf + pp + 1);
}

void *SVGA_cmd3d_ptr(DWORD *buf, DWORD *pOffset, DWORD cmd, DWORD cmdsize)
{
	DWORD pp = (*pOffset)/sizeof(DWORD);
	buf[pp] = cmd;
	buf[pp+1] = cmdsize;
	*pOffset = (*pOffset) + 2*sizeof(DWORD) + cmdsize;
	
	return (void*)(buf + pp + 2);
}

#if 0
void SVGA_IRQ_proc()
{
	dbg_printf(dbg_irq);
}

void __declspec(naked) SVGA_IRQ_entry()
{
	_asm
	{
		pushad
		call SVGA_IRQ_proc
		popad
	}
	
	VxDJmp(VPICD, Set_Int_Request);
}
#endif

BOOL SVGA_vxdcmd(DWORD cmd)
{
	switch(cmd)
	{
		case SVGA_CMD_INVALIDATE_FB:
			return TRUE;
	}
	
	return FALSE;
}

static DWORD fb_pm16 = 0;
//static DWORD st_pm16 = 0;
//static DWORD st_address = 0;
static DWORD st_surface_mb = 0;
static DWORD disable_multisample = 0;

static void SVGA_write_driver_id()
{
	if(gSVGA.deviceVersionId >= SVGA_ID_2)
	{
		uint32 caps2 = SVGA_ReadReg(SVGA_REG_CAP2);
		
		if((caps2 & SVGA_CAP2_DX2) != 0)
		{
			SVGA_WriteReg(SVGA_REG_GUEST_DRIVER_ID, SVGA_REG_GUEST_DRIVER_ID_LINUX);

			SVGA_WriteReg(SVGA_REG_GUEST_DRIVER_VERSION1,
				LINUX_VERSION_MAJOR << 24 |
				LINUX_VERSION_PATCHLEVEL << 16 |
				LINUX_VERSION_SUBLEVEL);

			SVGA_WriteReg(SVGA_REG_GUEST_DRIVER_VERSION2,
			  VMWGFX_DRIVER_MAJOR << 24 |
			  VMWGFX_DRIVER_MINOR << 16 |
			  VMWGFX_DRIVER_PATCHLEVEL);

			SVGA_WriteReg(SVGA_REG_GUEST_DRIVER_VERSION3, 0);

			SVGA_WriteReg(SVGA_REG_GUEST_DRIVER_ID, SVGA_REG_GUEST_DRIVER_ID_SUBMIT);
		}
	}
}

/**
 * Init SVGA-II hardware
 * return TRUE on success
 * return FALSE on failure (unsupported HW version or invalid HW)
 **/
BOOL SVGA_init_hw()
{
	/* defaults */
	DWORD conf_vram_limit = 0;
	DWORD conf_rgb565bug = 1;
	DWORD conf_cb = 1;
	DWORD conf_hw_version = SVGA_VERSION_2;
#if 0
	uint8 irq = 0;
#endif

	int rc;
	
	/* configs in registry */
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_vram_limit, &conf_vram_limit);
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_rgb565bug,  &conf_rgb565bug);
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_cb,         &conf_cb); 
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_hw_version, &conf_hw_version);
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_pref_fifo,  &prefer_fifo);
 	
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_disable_multisample, &disable_multisample);
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_async_mobs, &async_mobs);
 	RegReadConf(HKEY_LOCAL_MACHINE, SVGA_conf_path, SVGA_conf_hw_cursor,  &hw_cursor);
 	
 	if(async_mobs < 1)
 		async_mobs = 1;
 	
 	if(async_mobs >= SVGA_CB_MAX_QUEUED_PER_CONTEXT)
 		async_mobs = SVGA_CB_MAX_QUEUED_PER_CONTEXT-1;
 	
 	if(!FBHDA_init_hw())
 	{
 		return FALSE;
 	}
 	
 	dbg_printf(dbg_test, 0);
 	
 	rc = SVGA_Init(FALSE, conf_hw_version);
 	dbg_printf(dbg_SVGA_Init, rc);
 	
	if(rc == 0)
	{
		/* default flags */
		gSVGA.userFlags = 0;
		
		if(conf_rgb565bug)
		{
			gSVGA.userFlags |= SVGA_USER_FLAGS_RGB565_BROKEN;
		}
		
		if(conf_vram_limit > SVGA_MIN_VRAM)
		{
			if(gSVGA.vramSize > conf_vram_limit*(1024*1024))
			{
				gSVGA.vramSize = conf_vram_limit*(1024*1024);
			}
		}
		
		dbg_printf(dbg_Device_Init_proc_succ);
		
		dbg_printf(dbg_mapping_map, gSVGA.fbPhy, gSVGA.fifoPhy);
		
		/* map phys FB to linear */
		gSVGA.fbLinear = _MapPhysToLinear(gSVGA.fbPhy, gSVGA.vramSize, 0);
		
		/* map phys FIFO to linear */
		if(!SVGA_IsSVGA3())
			gSVGA.fifoLinear = _MapPhysToLinear(gSVGA.fifoPhy, gSVGA.fifoSize, 0);
		
		/* system linear addres == PM32 address */
		gSVGA.fbMem = (uint8 *)gSVGA.fbLinear;
		if(!SVGA_IsSVGA3())
			gSVGA.fifoMem = (uint32 *)gSVGA.fifoLinear;
		
		dbg_printf(dbg_mapping);
		dbg_printf(dbg_mapping_map, gSVGA.fbPhy, gSVGA.fbMem);
		dbg_printf(dbg_mapping_map, gSVGA.fifoPhy, gSVGA.fifoMem);
		//dbg_printf(dbg_mapping_map, gSVGA.fifo.bouncePhy, gSVGA.fifo.bounceMem);
		dbg_printf(dbg_siz, sizeof(gSVGA), sizeof(uint8 FARP *));
		
		SVGA_write_driver_id();

#if 0
		irq = SVGA_Install_IRQ();
		if(irq > 0)
		{
			VPICD_IRQ_Descriptor vid;
			memset(&vid, 0, sizeof(vid));
			vid.IRQ_Number      = irq;
			vid.Options         = VPICD_OPT_CAN_SHARE;
			vid.Hw_Int_Proc     = (DWORD)SVGA_IRQ_entry;
			vid.IRET_Time_Out   = 500;
			
			/* JH: OK this is NOT way how catch IRQ, because all PCI video IRQ
			       are catched by fat vdd driver.
			 */
			if(VPICD_Virtualize_IRQ(&vid))
			{
				dbg_printf(dbg_irq_install, irq);
			}
			else
			{
				dbg_printf(dbg_irq_install_fail, irq);
			}
			
			SVGA_WriteReg(SVGA_REG_IRQMASK, SVGA_IRQFLAG_COMMAND_BUFFER);
		}
		else
		{
			dbg_printf(dbg_no_irq);
		}
#endif
		
		/* enable FIFO */
		SVGA_Enable();
		
		/* allocate GB tables, if supported */
		if(SVGA_ReadReg(SVGA_REG_CAPABILITIES) & SVGA_CAP_GBOBJECTS)
		{
			SVGA_OTable_alloc(st_surface_mb > 0);
			gb_support = TRUE;
			dbg_printf(dbg_gb_on);
		}
		
		/* enable command buffers if supported and enabled */
		if(SVGA_ReadReg(SVGA_REG_CAPABILITIES) & (SVGA_CAP_COMMAND_BUFFERS | SVGA_CAP_CMD_BUFFERS_2))
		{
			if(prefer_fifo && !gb_support)
			{
				conf_cb = FALSE;
			}
			
			if(conf_cb)
			{
				cb_support = TRUE;
				dbg_printf(dbg_cb_on);
			}
		}
		
		set_fragmantation_limit();
		
		SVGA_DB_alloc();
		
		/* allocate buffer for enable and disable CB */
		ctlbuf = SVGA_CMB_alloc_size(64);
		
		/* allocate CB for this driver */
		cmdbuf = SVGA_CMB_alloc();
		
		/* special set for faster MOB define */
		mob_cb_alloc();
	
		/* vGPU10 */
		if(gb_support)
		{
			SVGA_CB_start();
			
			if(cb_support) /* SVGA_CB_start may fail */
			{
				SVGA_OTable_load();
			}
		}
		
		/* allocate 16bit selector for FB */
		fb_pm16 = map_pm16(1, gSVGA.fbLinear, gSVGA.vramSize);
		
		/* fill address in FBHDA */
		hda->vram_pm32 = (void*)gSVGA.fbLinear;
		hda->vram_size = gSVGA.vramSize;
		hda->vram_pm16 = fb_pm16;
		
 		memcpy(hda->vxdname, SVGA_vxd_name, sizeof(SVGA_vxd_name));
		
		hda->flags |= FB_ACCEL_VMSVGA;
		
		if(cb_support && gb_support)
		{
			hda->flags |= FB_ACCEL_VMSVGA10;
		}
			
		cache_init();
				
		SVGA_is_valid = TRUE;
		
		if(_GetFreePageCount(NULL) >= 0x38000UL) /* 896MB / 4096 */
		{
			if(!gb_support) /* mob cache is done by RING-3 DLL */
			{
				cache_enable(TRUE);
			}
		}
		
		/* switch back to VGA mode, SVGA mode will be request by 16 bit driver later */
		SVGA_HW_disable();
		
		return TRUE;
	}
	
	return FALSE;
}

/* Check if screen acceleration is available */
static BOOL SVGA_hasAccelScreen()
{
	if(SVGA_HasFIFOCap(SVGA_FIFO_CAP_SCREEN_OBJECT | SVGA_FIFO_CAP_SCREEN_OBJECT_2))
	{
		return TRUE;
	}
	
	return FALSE;
}

/* from svga3d.c, we need only this function from whole file */
BOOL SVGA3D_Init(void)
{
	SVGA3dHardwareVersion hwVersion;

	if(!(gSVGA.capabilities & SVGA_CAP_EXTENDED_FIFO))
	{
		return FALSE;
	}
	
	if((gSVGA.capabilities & SVGA_CAP_GBOBJECTS))
	{
		if(!SVGA_GetDevCap(SVGA3D_DEVCAP_DXCONTEXT))
		{
			/* don't allow 3D on vGPU without DX context */
			return FALSE;
		}
		hwVersion = SVGA3D_HWVERSION_WS8_B1;
	}
	else if(SVGA_HasFIFOCap(SVGA_FIFO_CAP_3D_HWVERSION_REVISED))
	{
		hwVersion = gSVGA.fifoMem[SVGA_FIFO_3D_HWVERSION_REVISED];
	}
	else
	{
		if(gSVGA.fifoMem[SVGA_FIFO_MIN] <= sizeof(uint32) * SVGA_FIFO_GUEST_3D_HWVERSION)
		{
			return FALSE;
		}
		hwVersion = gSVGA.fifoMem[SVGA_FIFO_3D_HWVERSION];
	}

	/*
	 * Check the host's version, make sure we're binary compatible.
	 */
	if(hwVersion == 0)
	{
		//dbg_printf("3D disabled by host.");
		return FALSE;
	}
	if (hwVersion < SVGA3D_HWVERSION_WS65_B1)
	{
		//dbg_printf("Host SVGA3D protocol is too old, not binary compatible.");
		return FALSE;
	}
	
	return TRUE;
}

DWORD SVGA_pitch(DWORD width, DWORD bpp)
{
	DWORD bp = (bpp + 7) / 8;
	return (bp * width + 15) & 0xFFFFFFF0UL;
}

static DWORD SVGA_DT_stride(DWORD w, DWORD h)
{
	DWORD stride = SVGA_pitch(w, 32) * h;
	
	return (stride + 65535) & 0xFFFF0000UL;
}

/**
 *
 * @return: screen offset to VRAM
 **/
static void SVGA_defineScreen(DWORD w, DWORD h, DWORD bpp, DWORD offset)
{
  SVGAFifoCmdDefineScreen *screen;
  DWORD cmdoff = 0;
  
  wait_for_cmdbuf();
  
  screen = SVGA_cmd_ptr(cmdbuf, &cmdoff, SVGA_CMD_DEFINE_SCREEN, sizeof(SVGAFifoCmdDefineScreen));

	memset(screen, 0, sizeof(SVGAFifoCmdDefineScreen));
	screen->screen.structSize = sizeof(SVGAScreenObject);
	screen->screen.id = 0;
	screen->screen.flags = SVGA_SCREEN_MUST_BE_SET | SVGA_SCREEN_IS_PRIMARY;
	screen->screen.size.width = w;
	screen->screen.size.height = h;
	screen->screen.root.x = 0;
	screen->screen.root.y = 0;
	screen->screen.cloneCount = 0;

	screen->screen.backingStore.pitch = SVGA_pitch(w, bpp);
	screen->screen.backingStore.ptr.offset = 0; /* vmware, always 0 for primary surface  */
	screen->screen.backingStore.ptr.gmrId = SVGA_GMR_FRAMEBUFFER; /* must be framebuffer */
	
	submit_cmdbuf(cmdoff, SVGA_CB_SYNC|SVGA_CB_FORCE_FIFO, 0);
}

static void SVGA_FillGMRFB(SVGAFifoCmdDefineGMRFB *fbgmr, 
	DWORD offset, DWORD pitch, DWORD bpp)
{
	fbgmr->ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
	fbgmr->ptr.offset = offset;
	fbgmr->bytesPerLine = pitch;
	fbgmr->format.colorDepth = bpp;
	if(bpp >= 24)
	{
		fbgmr->format.bitsPerPixel = 32;
		fbgmr->format.colorDepth   = 24;
		fbgmr->format.reserved     = 0;
	}
	else
	{
		fbgmr->format.bitsPerPixel = 16;
		fbgmr->format.colorDepth   = 16;
		fbgmr->format.reserved     = 0;
	}
}

static void SVGA_DefineGMRFB()
{
	SVGAFifoCmdDefineGMRFB *gmrfb;
	DWORD cmd_offset = 0;

	wait_for_cmdbuf();
	  	
	gmrfb = SVGA_cmd_ptr(cmdbuf, &cmd_offset, SVGA_CMD_DEFINE_GMRFB, sizeof(SVGAFifoCmdDefineGMRFB));
	SVGA_FillGMRFB(gmrfb, hda->surface, hda->pitch, hda->bpp);
	submit_cmdbuf(cmd_offset, 0, 0);
	
	dbg_printf("SVGA_DefineGMRFB: %ld\n", hda->surface);
}

/**
 * Test if display mode is supported
 **/
BOOL SVGA_validmode(DWORD w, DWORD h, DWORD bpp)
{
	switch(bpp)
	{
		case 8:
		case 16:
		case 32:
			break;
		default:
			return FALSE;
	}
	
	if(w <= SVGA_ReadReg(SVGA_REG_MAX_WIDTH))
	{
		if(h <= SVGA_ReadReg(SVGA_REG_MAX_HEIGHT))
		{
			DWORD size = SVGA_pitch(w, bpp) * h;
			if(size < hda->vram_size)
			{
				if(bpp != 32 && size > SVGA_FB_MAX_TRACEABLE_SIZE)
				{
					return FALSE;
				}
				
				return TRUE;
			}
		}
	}
	
	return FALSE;
}

static void SVGA_setmode_phy(DWORD w, DWORD h, DWORD bpp)
{
	//SVGA_OTable_unload(); // unload otables
	
	/* Make sure, that we drain full FIFO */
	SVGA_Sync();
	SVGA_Flush_CB(); 
	
#if 0
	/* delete old screen at its objects */
	if(SVGA_hasAccelScreen())
	{
		if(st_used)
		{
			st_destroyScreen();
			SVGA_Sync();
			SVGA_Flush_CB(); 
		}
	}
	/* JH: OK, we should normally clean after ourselves,
	       but VirtualBox GUI can crash if there are
	       no surface to draw. Anyway, when we touch legacy
	       screen registers all screen objects will be
	       reset.
	 */
#endif

	/* stop command buffer context 0 */
	SVGA_CB_stop();
  
   /* setup by legacy registry */
  if(SVGA_hasAccelScreen())
  {
		SVGA_SetModeLegacy(w, h, 32);
	}
	else
	{
		SVGA_SetModeLegacy(w, h, bpp);
	}
	
	/* VMware, vGPU10: OK, when screen has change, whoale GPU is reset including FIFO */
	SVGA_Enable();
	
	SVGA_Flush(); /* make sure, that is really set */

	/* setting screen by fifo, this method is required in VB 6.1 */
	if(SVGA_hasAccelScreen())
	{
		SVGA_defineScreen(w, h, bpp, 0);
			
		/* reenable fifo */
		SVGA_Enable();
		
		SVGA_Flush();
		
		/* TODO, not do this on vGPU10 */
	}
	SVGA_Sync();
	
	/* start command buffer context 0 */
	SVGA_CB_start();
	
	SVGA_Sync();
	SVGA_Flush_CB();
}

/**
 * Set display mode
 *
 **/
BOOL SVGA_setmode(DWORD w, DWORD h, DWORD bpp)
{
	BOOL has3D;
	if(!SVGA_validmode(w, h, bpp))
	{
		return FALSE;
	}
	
	svga_saved_state.width = w;
	svga_saved_state.height = h;
	svga_saved_state.bpp = bpp;
	svga_saved_state.enabled = TRUE;

	/* when on overlay silently eat change mode requests */
	if(hda->overlay > 0)
	{
		return TRUE;
	}
	
	/* free chached regions */
	/*
	SVGA_flushcache();
	 ^JH: no alloc/free operations here (don't know why,
				but when VXD si called from 16 bits, these
				operations are bit unstable)
	*/
	
	mouse_invalidate();
	FBHDA_access_begin(0);
	
	SVGA_setmode_phy(w, h, bpp);
		
	has3D = SVGA3D_Init();
	
	hda->flags &= ~((DWORD)FB_SUPPORT_FLIPING);
	
	hda->flags &= ~((DWORD)FB_ACCEL_VMSVGA10_ST);

	hda->vram_pm32 = (void*)gSVGA.fbLinear;
	hda->vram_size = gSVGA.vramSize;
	hda->vram_pm16 = fb_pm16;

	hda->width   = w;//SVGA_ReadReg(SVGA_REG_WIDTH);
	hda->height  = h;//SVGA_ReadReg(SVGA_REG_HEIGHT);
	
	if(SVGA_hasAccelScreen())
	{
		if(bpp >= 8)
		{
			DWORD offset = SVGA_DT_stride(hda->width, hda->height);
			
			hda->bpp     = bpp;
			hda->pitch   = SVGA_pitch(hda->width, bpp);
			hda->system_surface = offset;
			hda->surface = offset;
		}
		else
		{
			hda->bpp     = SVGA_ReadReg(SVGA_REG_BITS_PER_PIXEL);
			hda->pitch   = SVGA_ReadReg(SVGA_REG_BYTES_PER_LINE);
			hda->surface = 0;
			hda->system_surface = 0;
		}
	}
	else
	{
		hda->bpp     = SVGA_ReadReg(SVGA_REG_BITS_PER_PIXEL);
		hda->pitch   = SVGA_ReadReg(SVGA_REG_BYTES_PER_LINE);
		hda->surface = 0;
		hda->system_surface = 0;
	}
	hda->stride  = hda->height * hda->pitch;
	
	if(has3D && SVGA_GetDevCap(SVGA3D_DEVCAP_3D) > 0)
	{
		hda->flags |= FB_ACCEL_VMSVGA3D;
	}
	else
	{
		hda->flags &= ~((DWORD)FB_ACCEL_VMSVGA3D);
	}
	
	if(hda->system_surface > 0)
	{
		hda->flags |= FB_SUPPORT_FLIPING;
		SVGA_DefineGMRFB();
	}
	
	mouse_invalidate();
	FBHDA_access_end(0);

  return TRUE;
}

/*
 * Fix SVGA caps which are known as bad:
 *  - VirtualBox returning A4R4G4B4 instead of R5G6B5,
 *    try to guess it's value from X8R8G8B8.
 *
 */
DWORD SVGA_FixDevCap(DWORD cap_id, DWORD cap_val)
{
	switch(cap_id)
	{
		case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
		{
			if(gSVGA.userFlags & SVGA_USER_FLAGS_RGB565_BROKEN)
			{
				DWORD xrgb8888 = SVGA_GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
				xrgb8888 &= ~SVGA3DFORMAT_OP_SRGBWRITE; /* nvidia */
				return xrgb8888;
			}
			else
			{
				if(cap_val & SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET) /* VirtualBox BUG */
				{
					DWORD xrgb8888 = SVGA_GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
					xrgb8888 &= ~SVGA3DFORMAT_OP_SRGBWRITE; /* nvidia */
					return xrgb8888;
				}
				
				if((cap_val & SVGA3DFORMAT_OP_3DACCELERATION) == 0) /* VirtualBox BUG, older drivers */
				{
					DWORD xrgb8888 = SVGA_GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
					xrgb8888 &= ~SVGA3DFORMAT_OP_SRGBWRITE; /* nvidia */
					if(xrgb8888 & SVGA3DFORMAT_OP_3DACCELERATION)
					{
						return xrgb8888;
					}
				}
			}
			break;
		}
		case SVGA3D_DEVCAP_MULTISAMPLE_2X:
		case SVGA3D_DEVCAP_MULTISAMPLE_4X:
		case SVGA3D_DEVCAP_MULTISAMPLE_8X:
			if(disable_multisample)
			{
				return 0;
			}
			break;
		
	}
	
	return cap_val;
}

/**
 * Read HW cap, supported old way (GPU gen9):
 *   struct SVGA_FIFO_3D_CAPS in FIFO,
 * and new way (GPU gen10):
 *   SVGA_REG_DEV_CAP HW register
 *
 **/
DWORD SVGA_GetDevCap(DWORD search_id)
{
	if (gSVGA.capabilities & SVGA_CAP_GBOBJECTS)
	{
		/* new way to read device CAPS */
		SVGA_WriteReg(SVGA_REG_DEV_CAP, search_id);
		return SVGA_FixDevCap(search_id, SVGA_ReadReg(SVGA_REG_DEV_CAP));
	}
	else
	{
		SVGA3dCapsRecord *pCaps = (SVGA3dCapsRecord *)&(gSVGA.fifoMem[SVGA_FIFO_3D_CAPS]);  
		while(pCaps->header.length != 0)
		{
		  if(pCaps->header.type == SVGA3DCAPS_RECORD_DEVCAPS)
		  {
		   	DWORD datalen = (pCaps->header.length - 2)/2;
	    	SVGA3dCapPair *pData = (SVGA3dCapPair *)(&pCaps->data);
	    	DWORD i;
	    	
		 		for(i = 0; i < datalen; i++)
		 		{
		  		DWORD id  = pData[i][0];
		  		DWORD val = pData[i][1];
		  			
		  		if(id == search_id)
					{
						return SVGA_FixDevCap(search_id, val);
					}
				}
			}
			pCaps = (SVGA3dCapsRecord *)((DWORD *)pCaps + pCaps->header.length);
		}
	}
	
	return 0;
}

DWORD SVGA_query(DWORD type, DWORD index)
{
	switch(type)
	{
		case SVGA_QUERY_REGS:
			if(index >= 256) break;			
			return SVGA_ReadReg(index);
		case SVGA_QUERY_FIFO:
			if(index >= 1024) break;
			return gSVGA.fifoMem[index];
		case SVGA_QUERY_CAPS:
			if(index >= 512) break;
			return SVGA_GetDevCap(index);
	}
	
	return ~0x0;
}

void SVGA_query_vector(DWORD type, DWORD index_start, DWORD count, DWORD *out)
{
	DWORD i;
	for(i = 0; i < count; i++)
	{
		out[i] = SVGA_query(type, index_start+i);
	}
}

void SVGA_HW_enable()
{
	if(hda->width > 0 && hda->height > 0)
	{
		SVGA_WriteReg(SVGA_REG_ENABLE, TRUE);
		
		SVGA_CB_start();
		
		svga_saved_state.enabled = TRUE;
	}
}

void SVGA_HW_disable()
{
	dbg_printf(dbg_disable);
	
	SVGA_CB_stop();
	
	SVGA_Disable();
	
	svga_saved_state.enabled = FALSE;
}

BOOL SVGA_valid()
{
	return SVGA_is_valid;
}

BOOL FBHDA_swap(DWORD offset)
{
	BOOL rc = FALSE;

	if(hda->overlay > 0)
	{
		/* on overlay emulate behaviour */
		if(offset >= hda->system_surface && hda->bpp > 8)
		{
			hda->surface = offset;
			return TRUE;
		}
		return FALSE;
	}
	
	if(offset >= hda->system_surface) /* DON'T touch surface 0 */
	{
	 	FBHDA_access_begin(0);
	 	if(hda->bpp > 8)
	 	{
	 		SVGA_DefineGMRFB();
	  	hda->surface = offset;
	  	rc = TRUE;
		}
	  FBHDA_access_end(0);
	}

	return rc;
}

static DWORD rect_left;
static DWORD rect_top;
static DWORD rect_right;
static DWORD rect_bottom;

static inline void update_rect(DWORD left, DWORD top, DWORD right, DWORD bottom)
{
	if(left < rect_left)
		rect_left = left;

	if(top < rect_top)
		rect_top = top;

	if(right > rect_right)
	{
		rect_right = right;
		if(rect_right > hda->width)
		{
			rect_right = hda->width;
		}
	}

	if(bottom > rect_bottom)
	{
		rect_bottom = bottom;
		if(rect_bottom > hda->height)
		{
			rect_bottom = hda->height;
		}
	}
}

static inline void check_dirty()
{
	if(surface_dirty)
	{
		switch(hda->bpp)
		{
			case 32:
			{
			 	SVGAFifoCmdBlitScreenToGMRFB *gmrblit;
			 	DWORD cmd_offset = 0;
		
				wait_for_cmdbuf();
						
				gmrblit = SVGA_cmd_ptr(cmdbuf, &cmd_offset, SVGA_CMD_BLIT_SCREEN_TO_GMRFB, sizeof(SVGAFifoCmdBlitScreenToGMRFB));
	
				gmrblit->destOrigin.x    = 0;
				gmrblit->destOrigin.y    = 0;
				gmrblit->srcRect.left    = 0;
				gmrblit->srcRect.top     = 0;
				gmrblit->srcRect.right   = hda->width;
				gmrblit->srcRect.bottom  = hda->height;
				gmrblit->srcScreenId = 0;
				  	
				submit_cmdbuf(cmd_offset, SVGA_CB_UPDATE, 0);
				break;
			}
			case 16:
			{
				readback16(
					hda->vram_pm32, SVGA_pitch(hda->width, 32),
					((BYTE*)hda->vram_pm32)+hda->surface, hda->pitch,
					0, 0, hda->width, hda->height
				);
				break;
			}
		} // switch
		
		surface_dirty = FALSE;
	}
}

void FBHDA_access_rect(DWORD left, DWORD top, DWORD right, DWORD bottom)
{
	if(hda->overlay > 0)
	{
		return;
	}
	
	Wait_Semaphore(hda_sem, 0);
	
/*	dbg_printf("FBHDA_access_rect(%ld, %ld, %ld, %ld)\n",
		left, top, right, bottom
	);*/
	
	if(left > hda->width)
		return;
	
	if(top > hda->height)
		return;
	
	if(right > hda->width)
		right = hda->width;
	
	if(bottom > hda->height)
		bottom = hda->height;

	if(fb_lock_cnt++ == 0)
	{
		SVGA_CMB_wait_update();
		check_dirty();
		
		rect_left   = left;
		rect_top    = top;
		rect_right  = right;
		rect_bottom = bottom;

		mouse_erase();

		if(mouse_get_rect(&left, &top, &right, &bottom))
		{
			update_rect(left, top, right, bottom);
		}
	}
	else
	{
		update_rect(left, top, right, bottom);
	}

	Signal_Semaphore(hda_sem);
}

void FBHDA_access_begin(DWORD flags)
{
	if(hda->overlay > 0)
	{
		return;
	}
	
	if(flags & (FBHDA_ACCESS_RAW_BUFFERING | FBHDA_ACCESS_MOUSE_MOVE))
	{
		Wait_Semaphore(hda_sem, 0);
		check_dirty();
		
//		dbg_printf("FBHDA_access_begin(%ld)\n", flags);
		
		if(fb_lock_cnt++ == 0)
		{
			SVGA_CMB_wait_update();
			mouse_erase();
			
			if(!mouse_get_rect(&rect_left, &rect_top, &rect_right, &rect_bottom))
			{
				rect_left   = 0;
				rect_top    = 0;
				rect_right  = 0;
				rect_bottom = 0;
			}
		}
		else
		{
			DWORD l, t, r, b;
			
			if(mouse_get_rect(&l, &t, &r, &b))
			{
				update_rect(l, t, r, b);
			}
		}
		
		Signal_Semaphore(hda_sem);
	}
	else
	{
		FBHDA_access_rect(0, 0, hda->width, hda->height);
	}
}

void FBHDA_access_end(DWORD flags)
{
	if(hda->overlay > 0)
	{
		return;
	}

	Wait_Semaphore(hda_sem, 0);

	if(flags & FBHDA_ACCESS_SURFACE_DIRTY)
	{
		surface_dirty = TRUE;
	}

	if(flags & FBHDA_ACCESS_MOUSE_MOVE)
	{
		DWORD l, t, r, b;
		
		if(mouse_get_rect(&l, &t, &r, &b))
		{
			update_rect(l, t, r, b);
		}
	}

	fb_lock_cnt--;
	if(fb_lock_cnt < 0) fb_lock_cnt = 0;
	
	if(fb_lock_cnt == 0)
	{
		DWORD w, h;
		BOOL need_refresh = ((hda->bpp == 32) && (hda->system_surface == 0));
		
		w = rect_right - rect_left;
		h = rect_bottom - rect_top;
		
/*		dbg_printf("FBHDA_access_end(%ld %ld %ld %ld)\n", 
			rect_left, rect_top, rect_right, rect_bottom);*/

		if(w > 0 && h > 0)
		{
			check_dirty();
			mouse_blit();
			if(hda->surface > 0)
			{
				switch(hda->bpp)
				{
					case 32:
					{
				  	SVGAFifoCmdBlitGMRFBToScreen *gmrblit;
				  	DWORD cmd_offset = 0;
		
						wait_for_cmdbuf();
						
				  	gmrblit = SVGA_cmd_ptr(cmdbuf, &cmd_offset, SVGA_CMD_BLIT_GMRFB_TO_SCREEN, sizeof(SVGAFifoCmdBlitGMRFBToScreen));
	
				  	gmrblit->srcOrigin.x      = rect_left;
				  	gmrblit->srcOrigin.y      = rect_top;
				  	gmrblit->destRect.left    = rect_left;
				  	gmrblit->destRect.top     = rect_top;
				  	gmrblit->destRect.right   = rect_right;
				  	gmrblit->destRect.bottom  = rect_bottom;
	
				  	gmrblit->destScreenId = 0;
	
						submit_cmdbuf(cmd_offset, SVGA_CB_UPDATE, 0);
						break;
					}
					case 16:
						blit16(
							((BYTE*)hda->vram_pm32)+hda->surface, hda->pitch,
							hda->vram_pm32,  SVGA_pitch(hda->width, 32),
							rect_left, rect_top,
							rect_right - rect_left, rect_bottom - rect_top
						);
						need_refresh = TRUE;
						break;
					case 8:
						blit8(
							((BYTE*)hda->vram_pm32)+hda->surface, hda->pitch,
							hda->vram_pm32,  SVGA_pitch(hda->width, 32),
							rect_left, rect_top,
							rect_right - rect_left, rect_bottom - rect_top
						);
						need_refresh = TRUE;
						break;
				} // switch
			}
	
		  if(need_refresh)
		  {
		  	SVGAFifoCmdUpdate *cmd_update;
		  	DWORD cmd_offset = 0;
	
		  	wait_for_cmdbuf();
	
		  	cmd_update = SVGA_cmd_ptr(cmdbuf, &cmd_offset, SVGA_CMD_UPDATE, sizeof(SVGAFifoCmdUpdate));
		  	cmd_update->x = rect_left;
		  	cmd_update->y = rect_top;
		  	cmd_update->width  = rect_right - rect_left;
		  	cmd_update->height = rect_bottom - rect_top;
	
				submit_cmdbuf(cmd_offset, SVGA_CB_UPDATE, 0);
		  }
		}
		else
		{
			mouse_blit(); /* in this case is mouse unvisible, but we need still switch visibility state */
		} // w == 0 && h == 0
	} // fb_lock_cnt == 0
	
	Signal_Semaphore(hda_sem);
}

void FBHDA_palette_set(unsigned char index, DWORD rgb)
{
	if(hda->system_surface > 0)
	{
		palette_emulation[index] = rgb;
	}
	else
	{
		UINT sIndex = SVGA_PALETTE_BASE + index*3;
		
  	SVGA_WriteReg(sIndex+0, (rgb >> 16) & 0xFF);
  	SVGA_WriteReg(sIndex+1, (rgb >>  8) & 0xFF);
  	SVGA_WriteReg(sIndex+2,  rgb        & 0xFF);
  }
  hda->palette_update++;
}

DWORD FBHDA_palette_get(unsigned char index)
{
	if(hda->system_surface > 0)
	{
		return palette_emulation[index];
	}
	else
	{
		UINT sIndex = SVGA_PALETTE_BASE + index*3;
	
		return ((SVGA_ReadReg(sIndex+0) & 0xFF) << 16) |
			((SVGA_ReadReg(sIndex+1) & 0xFF) << 8) |
		 	(SVGA_ReadReg(sIndex+2) & 0xFF);
	}
}

static DWORD ov_width = 0;
static DWORD ov_height = 0;
static DWORD ov_pitch = 0;
static DWORD ov_bpp = 0;

DWORD FBHDA_overlay_setup(DWORD overlay, DWORD width, DWORD height, DWORD bpp)
{
	dbg_printf("FBHDA_overlay_setup: %ld\n", overlay);

	if(overlay >= FBHA_OVERLAYS_MAX)
		return 0;

	if(overlay == 0)
	{
		/* restore */
		SVGA_setmode_phy(svga_saved_state.width, svga_saved_state.height, svga_saved_state.bpp);
		SVGA_DefineGMRFB();
		
		hda->overlay = 0;

		if(!svga_saved_state.enabled)
		{
			SVGA_Disable();
		}
		else
		{
			/* redraw framebuffer */
			FBHDA_access_begin(0);
			FBHDA_access_end(0);
		}

		return hda->pitch;
	}
	else
	{
		if(bpp == 32)
		{
			SVGAFifoCmdDefineGMRFB *gmrfb;
			DWORD cmd_offset = 0;

			DWORD pitch  = SVGA_pitch(width, bpp);
			DWORD offset = ((BYTE*)hda->overlays[overlay].ptr) - ((BYTE*)hda->vram_pm32);
			DWORD stride = pitch*height;

			if(hda->overlays[overlay].size > stride)
			{
				hda->overlay = overlay;
				SVGA_setmode_phy(width, height, bpp);

				wait_for_cmdbuf();
				gmrfb = SVGA_cmd_ptr(cmdbuf, &cmd_offset, SVGA_CMD_DEFINE_GMRFB, sizeof(SVGAFifoCmdDefineGMRFB));
				SVGA_FillGMRFB(gmrfb, offset, pitch, bpp);
				submit_cmdbuf(cmd_offset, SVGA_CB_SYNC, 0);

				ov_width  = width;
				ov_height = height;
				ov_pitch  = pitch;
				ov_bpp    = bpp;

				/* clear screen */
				FBHDA_overlay_lock(0, 0, width, height);
				memset(hda->overlays[overlay].ptr, 0, stride);
				FBHDA_overlay_unlock(0);

				return pitch;
			}
		} // bpp == 32
	} // overlay > 0
	
	return 0;
}

static int overlay_lock_cnt = 0;
static DWORD ov_rect_left   = 0;
static DWORD ov_rect_top    = 0;
static DWORD ov_rect_right  = 0;
static DWORD ov_rect_bottom = 0;

void FBHDA_overlay_lock(DWORD left, DWORD top, DWORD right, DWORD bottom)
{	
	if(hda->overlay == 0) return;
	
	if(overlay_lock_cnt++ == 0)
	{
		ov_rect_left   = left;
		ov_rect_top    = top;
		ov_rect_right  = right;
		ov_rect_bottom = bottom;
	}
	else
	{
		if(left < ov_rect_left) ov_rect_left = left;

		if(top < ov_rect_top) ov_rect_top = top;

		if(right > ov_rect_right) ov_rect_right = right;

		if(bottom > ov_rect_bottom) ov_rect_bottom = bottom;

	}
	
	if(ov_rect_right > ov_width) ov_rect_right = ov_width;

	if(ov_rect_bottom > ov_height) ov_rect_right = ov_height;
}

void  FBHDA_overlay_unlock(DWORD flags)
{
	if(hda->overlay == 0) return;
	
	if(--overlay_lock_cnt <= 0)
	{
		if(ov_rect_left < ov_rect_right && ov_rect_top < ov_rect_bottom)
		{
			SVGAFifoCmdBlitGMRFBToScreen *gmrblit;
			DWORD cmd_offset = 0;
			
			wait_for_cmdbuf();
			gmrblit = SVGA_cmd_ptr(cmdbuf, &cmd_offset, SVGA_CMD_BLIT_GMRFB_TO_SCREEN, sizeof(SVGAFifoCmdBlitGMRFBToScreen));

	  	gmrblit->srcOrigin.x      = ov_rect_left;
	  	gmrblit->srcOrigin.y      = ov_rect_top;
	  	gmrblit->destRect.left    = ov_rect_left;
	  	gmrblit->destRect.top     = ov_rect_top;
	  	gmrblit->destRect.right   = ov_rect_right;
	  	gmrblit->destRect.bottom  = ov_rect_bottom;
	
	  	gmrblit->destScreenId = 0;
				  	
			submit_cmdbuf(cmd_offset, SVGA_CB_UPDATE, 0);
		}

		if(overlay_lock_cnt < 0)
		{
			overlay_lock_cnt = 0;
		}
	}
}