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
|
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/mga/mga_dri.c,v 1.28 2003/02/08 21:26:58 dawes Exp $ */
/*
* Copyright 2000 VA Linux Systems Inc., Fremont, California.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Keith Whitwell <keith@tungstengraphics.com>
* Gareth Hughes <gareth@valinux.com>
*/
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "driver.h"
#include "drm.h"
#include "memops.h"
#include "mga_reg.h"
#include "mga.h"
#include "mga_macros.h"
#include "mga_dri.h"
/* Quiescence, locking
*/
#define MGA_TIMEOUT 2048
static void MGAWaitForIdleDMA( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
drm_lock_t lock;
int ret;
int i = 0;
memset( &lock, 0, sizeof(lock) );
for (;;) {
do {
/* first ask for quiescent and flush */
lock.flags = DRM_LOCK_QUIESCENT | DRM_LOCK_FLUSH;
do {
ret = drmCommandWrite( ctx->drmFD, DRM_MGA_FLUSH,
&lock, sizeof( lock ) );
} while ( ret == -EBUSY && i++ < DRM_MGA_IDLE_RETRY );
/* if it's still busy just try quiescent */
if ( ret == -EBUSY ) {
lock.flags = DRM_LOCK_QUIESCENT;
do {
ret = drmCommandWrite( ctx->drmFD, DRM_MGA_FLUSH,
&lock, sizeof( lock ) );
} while ( ret == -EBUSY && i++ < DRM_MGA_IDLE_RETRY );
}
} while ( ( ret == -EBUSY ) && ( i++ < MGA_TIMEOUT ) );
if ( ret == 0 )
return;
fprintf( stderr,
"[dri] Idle timed out, resetting engine...\n" );
drmCommandNone( ctx->drmFD, DRM_MGA_RESET );
}
}
static unsigned int mylog2( unsigned int n )
{
unsigned int log2 = 1;
while ( n > 1 ) n >>= 1, log2++;
return log2;
}
static int MGADRIAgpInit(struct DRIDriverContextRec *ctx, MGAPtr pMga)
{
unsigned long mode;
unsigned int vendor, device;
int ret, count, i;
if(pMga->agpSize < 12)pMga->agpSize = 12;
if(pMga->agpSize > 64)pMga->agpSize = 64; /* cap */
/* FIXME: Make these configurable...
*/
pMga->agp.size = pMga->agpSize * 1024 * 1024;
pMga->warp.offset = 0;
pMga->warp.size = MGA_WARP_UCODE_SIZE;
pMga->primary.offset = (pMga->warp.offset +
pMga->warp.size);
pMga->primary.size = 1024 * 1024;
pMga->buffers.offset = (pMga->primary.offset +
pMga->primary.size);
pMga->buffers.size = MGA_NUM_BUFFERS * MGA_BUFFER_SIZE;
pMga->agpTextures.offset = (pMga->buffers.offset +
pMga->buffers.size);
pMga->agpTextures.size = pMga->agp.size -
pMga->agpTextures.offset;
if ( drmAgpAcquire( ctx->drmFD ) < 0 ) {
fprintf( stderr, "[agp] AGP not available\n" );
return 0;
}
mode = drmAgpGetMode( ctx->drmFD ); /* Default mode */
vendor = drmAgpVendorId( ctx->drmFD );
device = drmAgpDeviceId( ctx->drmFD );
mode &= ~MGA_AGP_MODE_MASK;
switch ( pMga->agpMode ) {
case 4:
mode |= MGA_AGP_4X_MODE;
case 2:
mode |= MGA_AGP_2X_MODE;
case 1:
default:
mode |= MGA_AGP_1X_MODE;
}
#if 0
fprintf( stderr,
"[agp] Mode 0x%08lx [AGP 0x%04x/0x%04x; Card 0x%04x/0x%04x]\n",
mode, vendor, device,
ctx->pciVendor,
ctx->pciChipType );
#endif
if ( drmAgpEnable( ctx->drmFD, mode ) < 0 ) {
fprintf( stderr, "[agp] AGP not enabled\n" );
drmAgpRelease( ctx->drmFD );
return 0;
}
if ( pMga->Chipset == PCI_CHIP_MGAG200 ) {
switch ( pMga->agpMode ) {
case 2:
fprintf( stderr,
"[drm] Enabling AGP 2x PLL encoding\n" );
OUTREG( MGAREG_AGP_PLL, MGA_AGP2XPLL_ENABLE );
break;
case 1:
default:
fprintf( stderr,
"[drm] Disabling AGP 2x PLL encoding\n" );
OUTREG( MGAREG_AGP_PLL, MGA_AGP2XPLL_DISABLE );
pMga->agpMode = 1;
break;
}
}
ret = drmAgpAlloc( ctx->drmFD, pMga->agp.size,
0, NULL, &pMga->agp.handle );
if ( ret < 0 ) {
fprintf( stderr, "[agp] Out of memory (%d)\n", ret );
drmAgpRelease( ctx->drmFD );
return 0;
}
fprintf( stderr,
"[agp] %d kB allocated with handle 0x%08x\n",
pMga->agp.size/1024, (unsigned int)pMga->agp.handle );
if ( drmAgpBind( ctx->drmFD, pMga->agp.handle, 0 ) < 0 ) {
fprintf( stderr, "[agp] Could not bind memory\n" );
drmAgpFree( ctx->drmFD, pMga->agp.handle );
drmAgpRelease( ctx->drmFD );
return 0;
}
/* WARP microcode space
*/
if ( drmAddMap( ctx->drmFD,
pMga->warp.offset,
pMga->warp.size,
DRM_AGP, DRM_READ_ONLY,
&pMga->warp.handle ) < 0 ) {
fprintf( stderr,
"[agp] Could not add WARP microcode mapping\n" );
return 0;
}
fprintf( stderr,
"[agp] WARP microcode handle = 0x%08x\n",
pMga->warp.handle );
if ( drmMap( ctx->drmFD,
pMga->warp.handle,
pMga->warp.size,
&pMga->warp.map ) < 0 ) {
fprintf( stderr,
"[agp] Could not map WARP microcode\n" );
return 0;
}
fprintf( stderr,
"[agp] WARP microcode mapped at 0x%08lx\n",
(unsigned long)pMga->warp.map );
/* Primary DMA space
*/
if ( drmAddMap( ctx->drmFD,
pMga->primary.offset,
pMga->primary.size,
DRM_AGP, DRM_READ_ONLY,
&pMga->primary.handle ) < 0 ) {
fprintf( stderr,
"[agp] Could not add primary DMA mapping\n" );
return 0;
}
fprintf( stderr,
"[agp] Primary DMA handle = 0x%08x\n",
pMga->primary.handle );
if ( drmMap( ctx->drmFD,
pMga->primary.handle,
pMga->primary.size,
&pMga->primary.map ) < 0 ) {
fprintf( stderr,
"[agp] Could not map primary DMA\n" );
return 0;
}
fprintf( stderr,
"[agp] Primary DMA mapped at 0x%08lx\n",
(unsigned long)pMga->primary.map );
/* DMA buffers
*/
if ( drmAddMap( ctx->drmFD,
pMga->buffers.offset,
pMga->buffers.size,
DRM_AGP, 0,
&pMga->buffers.handle ) < 0 ) {
fprintf( stderr,
"[agp] Could not add DMA buffers mapping\n" );
return 0;
}
fprintf( stderr,
"[agp] DMA buffers handle = 0x%08x\n",
pMga->buffers.handle );
if ( drmMap( ctx->drmFD,
pMga->buffers.handle,
pMga->buffers.size,
&pMga->buffers.map ) < 0 ) {
fprintf( stderr,
"[agp] Could not map DMA buffers\n" );
return 0;
}
fprintf( stderr,
"[agp] DMA buffers mapped at 0x%08lx\n",
(unsigned long)pMga->buffers.map );
count = drmAddBufs( ctx->drmFD,
MGA_NUM_BUFFERS, MGA_BUFFER_SIZE,
DRM_AGP_BUFFER, pMga->buffers.offset );
if ( count <= 0 ) {
fprintf( stderr,
"[drm] failure adding %d %d byte DMA buffers\n",
MGA_NUM_BUFFERS, MGA_BUFFER_SIZE );
return 0;
}
fprintf( stderr,
"[drm] Added %d %d byte DMA buffers\n",
count, MGA_BUFFER_SIZE );
i = mylog2(pMga->agpTextures.size / MGA_NR_TEX_REGIONS);
if(i < MGA_LOG_MIN_TEX_REGION_SIZE)
i = MGA_LOG_MIN_TEX_REGION_SIZE;
pMga->agpTextures.size = (pMga->agpTextures.size >> i) << i;
if ( drmAddMap( ctx->drmFD,
pMga->agpTextures.offset,
pMga->agpTextures.size,
DRM_AGP, 0,
&pMga->agpTextures.handle ) < 0 ) {
fprintf( stderr,
"[agp] Could not add agpTexture mapping\n" );
return 0;
}
/* should i map it ? */
fprintf( stderr,
"[agp] agpTexture handle = 0x%08x\n",
pMga->agpTextures.handle );
fprintf( stderr,
"[agp] agpTexture size: %d kb\n", pMga->agpTextures.size/1024 );
return 1;
}
static int MGADRIMapInit( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
pMga->registers.size = MGAIOMAPSIZE;
if ( drmAddMap( ctx->drmFD,
(drm_handle_t)pMga->IOAddress,
pMga->registers.size,
DRM_REGISTERS, DRM_READ_ONLY,
&pMga->registers.handle ) < 0 ) {
fprintf( stderr,
"[drm] Could not add MMIO registers mapping\n" );
return 0;
}
fprintf( stderr,
"[drm] Registers handle = 0x%08lx\n",
pMga->registers.handle );
pMga->status.size = SAREA_MAX;
if ( drmAddMap( ctx->drmFD, 0, pMga->status.size,
DRM_SHM, DRM_READ_ONLY | DRM_LOCKED | DRM_KERNEL,
&pMga->status.handle ) < 0 ) {
fprintf( stderr,
"[drm] Could not add status page mapping\n" );
return 0;
}
fprintf( stderr,
"[drm] Status handle = 0x%08x\n",
pMga->status.handle );
if ( drmMap( ctx->drmFD,
pMga->status.handle,
pMga->status.size,
&pMga->status.map ) < 0 ) {
fprintf( stderr,
"[agp] Could not map status page\n" );
return 0;
}
fprintf( stderr,
"[agp] Status page mapped at 0x%08lx\n",
(unsigned long)pMga->status.map );
return 1;
}
static int MGADRIKernelInit( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
drm_mga_init_t init;
int ret;
memset( &init, 0, sizeof(init) );
init.func = MGA_INIT_DMA;
init.sarea_priv_offset = sizeof(drm_sarea_t);
switch ( pMga->Chipset ) {
case PCI_CHIP_MGAG550:
case PCI_CHIP_MGAG400:
init.chipset = MGA_CARD_TYPE_G400;
break;
case PCI_CHIP_MGAG200:
case PCI_CHIP_MGAG200_PCI:
init.chipset = MGA_CARD_TYPE_G200;
break;
default:
return 0;
}
init.sgram = 0; /* FIXME !pMga->HasSDRAM; */
switch (ctx->bpp)
{
case 16:
init.maccess = MGA_MACCESS_PW16;
break;
case 32:
init.maccess = MGA_MACCESS_PW32;
break;
default:
fprintf( stderr, "[mga] invalid bpp (%d)\n", ctx->bpp );
return 0;
}
init.fb_cpp = ctx->bpp / 8;
init.front_offset = pMga->frontOffset;
init.front_pitch = pMga->frontPitch / init.fb_cpp;
init.back_offset = pMga->backOffset;
init.back_pitch = pMga->backPitch / init.fb_cpp;
init.depth_cpp = ctx->bpp / 8;
init.depth_offset = pMga->depthOffset;
init.depth_pitch = pMga->depthPitch / init.depth_cpp;
init.texture_offset[0] = pMga->textureOffset;
init.texture_size[0] = pMga->textureSize;
init.fb_offset = ctx->shared.hFrameBuffer;
init.mmio_offset = pMga->registers.handle;
init.status_offset = pMga->status.handle;
init.warp_offset = pMga->warp.handle;
init.primary_offset = pMga->primary.handle;
init.buffers_offset = pMga->buffers.handle;
init.texture_offset[1] = pMga->agpTextures.handle;
init.texture_size[1] = pMga->agpTextures.size;
ret = drmCommandWrite( ctx->drmFD, DRM_MGA_INIT, &init, sizeof(init));
if ( ret < 0 ) {
fprintf( stderr,
"[drm] Failed to initialize DMA! (%d)\n", ret );
return 0;
}
return 1;
}
static void MGADRIIrqInit(struct DRIDriverContextRec *ctx, MGAPtr pMga)
{
if (!pMga->irq)
{
pMga->irq = drmGetInterruptFromBusID(ctx->drmFD,
ctx->pciBus,
ctx->pciDevice,
ctx->pciFunc);
fprintf(stderr, "[drm] got IRQ %d\n", pMga->irq);
if((drmCtlInstHandler(ctx->drmFD, pMga->irq)) != 0)
{
fprintf(stderr,
"[drm] failure adding irq handler, "
"there is a device already using that irq\n"
"[drm] falling back to irq-free operation\n");
pMga->irq = 0;
}
else
{
pMga->reg_ien = INREG( MGAREG_IEN );
}
}
if (pMga->irq)
fprintf(stderr,
"[drm] dma control initialized, using IRQ %d\n",
pMga->irq);
}
static int MGADRIBuffersInit( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
pMga->drmBuffers = drmMapBufs( ctx->drmFD );
if ( !pMga->drmBuffers )
{
fprintf( stderr,
"[drm] Failed to map DMA buffers list\n" );
return 0;
}
fprintf( stderr,
"[drm] Mapped %d DMA buffers\n",
pMga->drmBuffers->count );
return 1;
}
static int MGAMemoryInit( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
int width_bytes = ctx->shared.virtualWidth * ctx->cpp;
int bufferSize = ((ctx->shared.virtualHeight * width_bytes
+ MGA_BUFFER_ALIGN)
& ~MGA_BUFFER_ALIGN);
int depthSize = ((((ctx->shared.virtualHeight+15) & ~15) * width_bytes
+ MGA_BUFFER_ALIGN)
& ~MGA_BUFFER_ALIGN);
int l;
pMga->frontOffset = 0;
pMga->frontPitch = ctx->shared.virtualWidth * ctx->cpp;
fprintf(stderr,
"Using %d MB AGP aperture\n", pMga->agpSize);
fprintf(stderr,
"Using %d MB for vertex/indirect buffers\n", pMga->buffers.size>>20);
fprintf(stderr,
"Using %d MB for AGP textures\n", pMga->agpTextures.size>>20);
/* Front, back and depth buffers - everything else texture??
*/
pMga->textureSize = ctx->shared.fbSize - 2 * bufferSize - depthSize;
if (pMga->textureSize < 0)
return 0;
l = mylog2( pMga->textureSize / MGA_NR_TEX_REGIONS );
if ( l < MGA_LOG_MIN_TEX_REGION_SIZE )
l = MGA_LOG_MIN_TEX_REGION_SIZE;
/* Round the texture size up to the nearest whole number of
* texture regions. Again, be greedy about this, don't
* round down.
*/
pMga->logTextureGranularity = l;
pMga->textureSize = (pMga->textureSize >> l) << l;
/* Set a minimum usable local texture heap size. This will fit
* two 256x256x32bpp textures.
*/
if (pMga->textureSize < 512 * 1024) {
pMga->textureOffset = 0;
pMga->textureSize = 0;
}
/* Reserve space for textures */
pMga->textureOffset = ((ctx->shared.fbSize - pMga->textureSize +
MGA_BUFFER_ALIGN) &
~MGA_BUFFER_ALIGN);
/* Reserve space for the shared depth
* buffer.
*/
pMga->depthOffset = ((pMga->textureOffset - depthSize +
MGA_BUFFER_ALIGN) &
~MGA_BUFFER_ALIGN);
pMga->depthPitch = ctx->shared.virtualWidth * ctx->cpp;
pMga->backOffset = ((pMga->depthOffset - bufferSize +
MGA_BUFFER_ALIGN) &
~MGA_BUFFER_ALIGN);
pMga->backPitch = ctx->shared.virtualWidth * ctx->cpp;
fprintf(stderr,
"Will use back buffer at offset 0x%x\n",
pMga->backOffset);
fprintf(stderr,
"Will use depth buffer at offset 0x%x\n",
pMga->depthOffset);
fprintf(stderr,
"Will use %d kb for textures at offset 0x%x\n",
pMga->textureSize/1024, pMga->textureOffset);
return 1;
}
static int MGACheckDRMVersion( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
drmVersionPtr version;
/* Check the MGA DRM version */
version = drmGetVersion(ctx->drmFD);
if ( version ) {
if ( version->version_major != 3 ||
version->version_minor < 0 ) {
/* incompatible drm version */
fprintf( stderr,
"[dri] MGADRIScreenInit failed because of a version mismatch.\n"
"[dri] mga.o kernel module version is %d.%d.%d but version 3.0.x is needed.\n"
"[dri] Disabling DRI.\n",
version->version_major,
version->version_minor,
version->version_patchlevel );
drmFreeVersion( version );
return 0;
}
drmFreeVersion( version );
}
return 1;
}
static void print_client_msg( MGADRIPtr pMGADRI )
{
fprintf( stderr, "chipset: %d\n", pMGADRI->chipset );
fprintf( stderr, "width: %d\n", pMGADRI->width );
fprintf( stderr, "height: %d\n", pMGADRI->height );
fprintf( stderr, "mem: %d\n", pMGADRI->mem );
fprintf( stderr, "cpp: %d\n", pMGADRI->cpp );
fprintf( stderr, "agpMode: %d\n", pMGADRI->agpMode );
fprintf( stderr, "frontOffset: %d\n", pMGADRI->frontOffset );
fprintf( stderr, "frontPitch: %d\n", pMGADRI->frontPitch );
fprintf( stderr, "backOffset: %d\n", pMGADRI->backOffset );
fprintf( stderr, "backPitch: %d\n", pMGADRI->backPitch );
fprintf( stderr, "depthOffset: %d\n", pMGADRI->depthOffset );
fprintf( stderr, "depthPitch: %d\n", pMGADRI->depthPitch );
fprintf( stderr, "textureOffset: %d\n", pMGADRI->textureOffset );
fprintf( stderr, "textureSize: %d\n", pMGADRI->textureSize );
fprintf( stderr, "logTextureGranularity: %d\n", pMGADRI->logTextureGranularity );
fprintf( stderr, "logAgpTextureGranularity: %d\n", pMGADRI->logAgpTextureGranularity );
fprintf( stderr, "agpTextureHandle: %u\n", (unsigned int)pMGADRI->agpTextureOffset );
fprintf( stderr, "agpTextureSize: %u\n", (unsigned int)pMGADRI->agpTextureSize );
#if 0
pMGADRI->registers.handle = pMga->registers.handle;
pMGADRI->registers.size = pMga->registers.size;
pMGADRI->status.handle = pMga->status.handle;
pMGADRI->status.size = pMga->status.size;
pMGADRI->primary.handle = pMga->primary.handle;
pMGADRI->primary.size = pMga->primary.size;
pMGADRI->buffers.handle = pMga->buffers.handle;
pMGADRI->buffers.size = pMga->buffers.size;
pMGADRI->sarea_priv_offset = sizeof(drm_sarea_t);
#endif
}
static int MGAScreenInit( struct DRIDriverContextRec *ctx, MGAPtr pMga )
{
int i;
int err;
MGADRIPtr pMGADRI;
usleep(100);
/*assert(!ctx->IsClient);*/
{
int width_bytes = (ctx->shared.virtualWidth * ctx->cpp);
int maxy = ctx->shared.fbSize / width_bytes;
if (maxy <= ctx->shared.virtualHeight * 3) {
fprintf(stderr,
"Static buffer allocation failed -- "
"need at least %d kB video memory (have %d kB)\n",
(ctx->shared.virtualWidth * ctx->shared.virtualHeight *
ctx->cpp * 3 + 1023) / 1024,
ctx->shared.fbSize / 1024);
return 0;
}
}
switch(pMga->Chipset) {
case PCI_CHIP_MGAG550:
case PCI_CHIP_MGAG400:
case PCI_CHIP_MGAG200:
#if 0
case PCI_CHIP_MGAG200_PCI:
#endif
break;
default:
fprintf(stderr, "[drm] Direct rendering only supported with G200/G400/G550 AGP\n");
return 0;
}
fprintf( stderr,
"[drm] bpp: %d depth: %d\n",
ctx->bpp, ctx->bpp /* FIXME: depth */ );
if ( (ctx->bpp / 8) != 2 &&
(ctx->bpp / 8) != 4 ) {
fprintf( stderr,
"[dri] Direct rendering only supported in 16 and 32 bpp modes\n" );
return 0;
}
ctx->shared.SAREASize = SAREA_MAX;
/* Note that drmOpen will try to load the kernel module, if needed. */
ctx->drmFD = drmOpen("mga", NULL );
if (ctx->drmFD < 0) {
fprintf(stderr, "[drm] drmOpen failed\n");
return 0;
}
if ((err = drmSetBusid(ctx->drmFD, ctx->pciBusID)) < 0) {
fprintf(stderr, "[drm] drmSetBusid failed (%d, %s), %s\n",
ctx->drmFD, ctx->pciBusID, strerror(-err));
return 0;
}
if (drmAddMap( ctx->drmFD,
0,
ctx->shared.SAREASize,
DRM_SHM,
DRM_CONTAINS_LOCK,
&ctx->shared.hSAREA) < 0)
{
fprintf(stderr, "[drm] drmAddMap failed\n");
return 0;
}
fprintf(stderr, "[drm] added %d byte SAREA at 0x%08lx\n",
ctx->shared.SAREASize, ctx->shared.hSAREA);
if (drmMap( ctx->drmFD,
ctx->shared.hSAREA,
ctx->shared.SAREASize,
(drmAddressPtr)(&ctx->pSAREA)) < 0)
{
fprintf(stderr, "[drm] drmMap failed\n");
return 0;
}
memset(ctx->pSAREA, 0, ctx->shared.SAREASize);
fprintf(stderr, "[drm] mapped SAREA 0x%08lx to %p, size %d\n",
ctx->shared.hSAREA, ctx->pSAREA, ctx->shared.SAREASize);
/* Need to AddMap the framebuffer and mmio regions here:
*/
if (drmAddMap( ctx->drmFD,
(drm_handle_t)ctx->FBStart,
ctx->FBSize,
DRM_FRAME_BUFFER,
0,
&ctx->shared.hFrameBuffer) < 0)
{
fprintf(stderr, "[drm] drmAddMap framebuffer failed\n");
return 0;
}
fprintf(stderr, "[drm] framebuffer handle = 0x%08lx\n",
ctx->shared.hFrameBuffer);
#if 0 /* will be done in MGADRIMapInit */
if (drmAddMap(ctx->drmFD,
ctx->FixedInfo.mmio_start,
ctx->FixedInfo.mmio_len,
DRM_REGISTERS,
DRM_READ_ONLY,
&pMga->registers.handle) < 0) {
fprintf(stderr, "[drm] drmAddMap mmio failed\n");
return 0;
}
fprintf(stderr,
"[drm] register handle = 0x%08lx\n", pMga->registers.handle);
#endif
/* Check the mga DRM version */
if (!MGACheckDRMVersion(ctx, pMga)) {
return 0;
}
if ( !MGADRIAgpInit( ctx, pMga ) ) {
return 0;
}
if ( !MGADRIMapInit( ctx, pMga ) ) {
return 0;
}
/* Memory manager setup */
if (!MGAMemoryInit(ctx, pMga)) {
return 0;
}
/* Create a 'server' context so we can grab the lock for
* initialization ioctls.
*/
if ((err = drmCreateContext(ctx->drmFD, &ctx->serverContext)) != 0) {
fprintf(stderr, "%s: drmCreateContext failed %d\n", __FUNCTION__, err);
return 0;
}
DRM_LOCK(ctx->drmFD, ctx->pSAREA, ctx->serverContext, 0);
/* Initialize the kernel data structures */
if (!MGADRIKernelInit(ctx, pMga)) {
fprintf(stderr, "MGADRIKernelInit failed\n");
DRM_UNLOCK(ctx->drmFD, ctx->pSAREA, ctx->serverContext);
return 0;
}
/* Initialize the vertex buffers list */
if (!MGADRIBuffersInit(ctx, pMga)) {
fprintf(stderr, "MGADRIBuffersInit failed\n");
DRM_UNLOCK(ctx->drmFD, ctx->pSAREA, ctx->serverContext);
return 0;
}
/* Initialize IRQ */
MGADRIIrqInit(ctx, pMga);
/* Initialize the SAREA private data structure */
{
drm_mga_sarea_t *pSAREAPriv;
pSAREAPriv = (drm_mga_sarea_t *)(((char*)ctx->pSAREA) +
sizeof(drm_sarea_t));
memset(pSAREAPriv, 0, sizeof(*pSAREAPriv));
}
/* Quick hack to clear the front & back buffers. Could also use
* the clear ioctl to do this, but would need to setup hw state
* first.
*/
drimemsetio((char *)ctx->FBAddress + pMga->frontOffset,
0,
pMga->frontPitch * ctx->shared.virtualHeight );
drimemsetio((char *)ctx->FBAddress + pMga->backOffset,
0,
pMga->backPitch * ctx->shared.virtualHeight );
/* Can release the lock now */
/* DRM_UNLOCK(ctx->drmFD, ctx->pSAREA, ctx->serverContext);*/
/* This is the struct passed to radeon_dri.so for its initialization */
ctx->driverClientMsg = malloc(sizeof(MGADRIRec));
ctx->driverClientMsgSize = sizeof(MGADRIRec);
pMGADRI = (MGADRIPtr)ctx->driverClientMsg;
switch(pMga->Chipset) {
case PCI_CHIP_MGAG550:
case PCI_CHIP_MGAG400:
pMGADRI->chipset = MGA_CARD_TYPE_G400;
break;
case PCI_CHIP_MGAG200:
case PCI_CHIP_MGAG200_PCI:
pMGADRI->chipset = MGA_CARD_TYPE_G200;
break;
default:
return 0;
}
pMGADRI->width = ctx->shared.virtualWidth;
pMGADRI->height = ctx->shared.virtualHeight;
pMGADRI->mem = ctx->shared.fbSize;
pMGADRI->cpp = ctx->bpp / 8;
pMGADRI->agpMode = pMga->agpMode;
pMGADRI->frontOffset = pMga->frontOffset;
pMGADRI->frontPitch = pMga->frontPitch;
pMGADRI->backOffset = pMga->backOffset;
pMGADRI->backPitch = pMga->backPitch;
pMGADRI->depthOffset = pMga->depthOffset;
pMGADRI->depthPitch = pMga->depthPitch;
pMGADRI->textureOffset = pMga->textureOffset;
pMGADRI->textureSize = pMga->textureSize;
pMGADRI->logTextureGranularity = pMga->logTextureGranularity;
i = mylog2( pMga->agpTextures.size / MGA_NR_TEX_REGIONS );
if ( i < MGA_LOG_MIN_TEX_REGION_SIZE )
i = MGA_LOG_MIN_TEX_REGION_SIZE;
pMGADRI->logAgpTextureGranularity = i;
pMGADRI->agpTextureOffset = (unsigned int)pMga->agpTextures.handle;
pMGADRI->agpTextureSize = (unsigned int)pMga->agpTextures.size;
pMGADRI->registers.handle = pMga->registers.handle;
pMGADRI->registers.size = pMga->registers.size;
pMGADRI->status.handle = pMga->status.handle;
pMGADRI->status.size = pMga->status.size;
pMGADRI->primary.handle = pMga->primary.handle;
pMGADRI->primary.size = pMga->primary.size;
pMGADRI->buffers.handle = pMga->buffers.handle;
pMGADRI->buffers.size = pMga->buffers.size;
pMGADRI->sarea_priv_offset = sizeof(drm_sarea_t);
print_client_msg( pMGADRI );
return 1;
}
/**
* \brief Validate the fbdev mode.
*
* \param ctx display handle.
*
* \return one on success, or zero on failure.
*
* Saves some registers and returns 1.
*
* \sa mgaValidateMode().
*/
static int mgaValidateMode( const DRIDriverContext *ctx )
{
return 1;
}
/**
* \brief Examine mode returned by fbdev.
*
* \param ctx display handle.
*
* \return one on success, or zero on failure.
*
* Restores registers that fbdev has clobbered and returns 1.
*
* \sa mgaValidateMode().
*/
static int mgaPostValidateMode( const DRIDriverContext *ctx )
{
return 1;
}
/**
* \brief Initialize the framebuffer device mode
*
* \param ctx display handle.
*
* \return one on success, or zero on failure.
*
* Fills in \p info with some default values and some information from \p ctx
* and then calls MGAScreenInit() for the screen initialization.
*
* Before exiting clears the framebuffer memomry accessing it directly.
*/
static int mgaInitFBDev( struct DRIDriverContextRec *ctx )
{
MGAPtr pMga = calloc(1, sizeof(*pMga));
{
int dummy = ctx->shared.virtualWidth;
switch (ctx->bpp / 8) {
case 1: dummy = (ctx->shared.virtualWidth + 127) & ~127; break;
case 2: dummy = (ctx->shared.virtualWidth + 31) & ~31; break;
case 3:
case 4: dummy = (ctx->shared.virtualWidth + 15) & ~15; break;
}
ctx->shared.virtualWidth = dummy;
}
ctx->driverPrivate = (void *)pMga;
pMga->agpMode = MGA_DEFAULT_AGP_MODE;
pMga->agpSize = MGA_DEFAULT_AGP_SIZE;
pMga->Chipset = ctx->chipset;
pMga->IOAddress = ctx->MMIOStart;
pMga->IOBase = ctx->MMIOAddress;
pMga->frontPitch = ctx->shared.virtualWidth * ctx->cpp;
if (!MGAScreenInit( ctx, pMga ))
return 0;
return 1;
}
/**
* \brief The screen is being closed, so clean up any state and free any
* resources used by the DRI.
*
* \param ctx display handle.
*
* Unmaps the SAREA, closes the DRM device file descriptor and frees the driver
* private data.
*/
static void mgaHaltFBDev( struct DRIDriverContextRec *ctx )
{
drmUnmap( ctx->pSAREA, ctx->shared.SAREASize );
drmClose(ctx->drmFD);
if (ctx->driverPrivate) {
free(ctx->driverPrivate);
ctx->driverPrivate = NULL;
}
}
static int mgaEngineShutdown( const DRIDriverContext *ctx )
{
fprintf(stderr, "%s() is not yet implemented!\n", __FUNCTION__);
return 1;
}
static int mgaEngineRestore( const DRIDriverContext *ctx )
{
fprintf(stderr, "%s() is not yet implemented!\n", __FUNCTION__);
return 1;
}
/**
* \brief Exported driver interface for Mini GLX.
*
* \sa DRIDriverRec.
*/
struct DRIDriverRec __driDriver = {
mgaValidateMode,
mgaPostValidateMode,
mgaInitFBDev,
mgaHaltFBDev,
mgaEngineShutdown,
mgaEngineRestore,
0
};
#if 0
void MGADRICloseScreen( ScreenPtr pScreen )
{
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
MGAPtr pMga = MGAPTR(pScrn);
MGADRIServerPrivatePtr pMga = pMga->DRIServerInfo;
drmMGAInit init;
if ( pMga->drmBuffers ) {
drmUnmapBufs( pMga->drmBuffers );
pMga->drmBuffers = NULL;
}
if (pMga->irq) {
drmCtlUninstHandler(ctx->drmFD);
pMga->irq = 0;
}
/* Cleanup DMA */
memset( &init, 0, sizeof(drmMGAInit) );
init.func = MGA_CLEANUP_DMA;
drmCommandWrite( ctx->drmFD, DRM_MGA_INIT, &init, sizeof(drmMGAInit) );
if ( pMga->status.map ) {
drmUnmap( pMga->status.map, pMga->status.size );
pMga->status.map = NULL;
}
if ( pMga->buffers.map ) {
drmUnmap( pMga->buffers.map, pMga->buffers.size );
pMga->buffers.map = NULL;
}
if ( pMga->primary.map ) {
drmUnmap( pMga->primary.map, pMga->primary.size );
pMga->primary.map = NULL;
}
if ( pMga->warp.map ) {
drmUnmap( pMga->warp.map, pMga->warp.size );
pMga->warp.map = NULL;
}
if ( pMga->agpTextures.map ) {
drmUnmap( pMga->agpTextures.map, pMga->agpTextures.size );
pMga->agpTextures.map = NULL;
}
if ( pMga->agp.handle ) {
drmAgpUnbind( ctx->drmFD, pMga->agp.handle );
drmAgpFree( ctx->drmFD, pMga->agp.handle );
pMga->agp.handle = 0;
drmAgpRelease( ctx->drmFD );
}
DRICloseScreen( pScreen );
if ( pMga->pDRIInfo ) {
if ( pMga->pDRIpMga->devPrivate ) {
xfree( pMga->pDRIpMga->devPrivate );
pMga->pDRIpMga->devPrivate = 0;
}
DRIDestroyInfoRec( pMga->pDRIInfo );
pMga->pDRIInfo = 0;
}
if ( pMga->DRIServerInfo ) {
xfree( pMga->DRIServerInfo );
pMga->DRIServerInfo = 0;
}
if ( pMga->pVisualConfigs ) {
xfree( pMga->pVisualConfigs );
}
if ( pMga->pVisualConfigsPriv ) {
xfree( pMga->pVisualConfigsPriv );
}
}
#endif
|