summaryrefslogtreecommitdiff
path: root/src/savage_vbe.c
blob: d8a08332dc935e8f6bec2bffe03cdba364dce6cf (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

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "savage_driver.h"
#include "savage_vbe.h"

#define iabs(a)	((int)(a)>0?(a):(-(a)))

#if X_BYTE_ORDER == X_LITTLE_ENDIAN
#define B_O16(x)  (x) 
#define B_O32(x)  (x)
#else
#define B_O16(x)  ((((x) & 0xff) << 8) | (((x) & 0xff) >> 8))
#define B_O32(x)  ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) \
                  | (((x) & 0xff0000) >> 8) | (((x) & 0xff000000) >> 24))
#endif
#define L_ADD(x)  (B_O32(x) & 0xffff) + ((B_O32(x) >> 12) & 0xffff00)

static int SavageGetDevice( SavagePtr psav );
/*static int SavageGetTVType( SavagePtr psav );*/
void SavageSetVESAModeCrtc1( SavagePtr psav, int n, int Refresh );
void SavageSetVESAModeCrtc2( SavagePtr psav, int n, int Refresh );

static void
SavageClearVM86Regs( xf86Int10InfoPtr pInt )
{
    pInt->ax = 0;
    pInt->bx = 0;
    pInt->cx = 0;
    pInt->dx = 0;
    pInt->si = 0;
    pInt->di = 0;
    pInt->es = 0xc000;
    pInt->num = 0x10;
}

void
SavageSetTextMode( SavagePtr psav )
{
    /* Retrieve current output status via BIOS calls */
    psav->iDevInfo = SavageGetDevice(psav);

    /* TODO: should the settings also be corrected for the DuoView flag? */

    /* Restore display device to original (primary) state if active output
       device status has changed since last mode switch AWAY from text. */
    if( (psav->iDevInfo & ACTIVE_DEVICES) !=
	(psav->iDevInfoPrim & ACTIVE_DEVICES) ) {
	SavageClearVM86Regs( psav->pVbe->pInt10 );
	psav->pVbe->pInt10->ax = 0x4f14;
	psav->pVbe->pInt10->bx = 0x0003;
	psav->pVbe->pInt10->cx = psav->iDevInfoPrim;
	xf86ExecX86int10( psav->pVbe->pInt10 );
    }

    /* switch to text mode */
    SavageClearVM86Regs( psav->pVbe->pInt10 );
    psav->pVbe->pInt10->ax = 0x83;
    xf86ExecX86int10( psav->pVbe->pInt10 );
}

void
SavageSetVESAModeCrtc1(SavagePtr psav, int n, int refresh)
{
    unsigned char byte;

    xf86Msg(X_INFO,"SavageSetVESAModeCrtc1:mode=0x%x,refresh=%dHZ\n",n,refresh);

    SavageClearVM86Regs(psav->pVbe->pInt10);
    
    /* set active displays. */
    psav->pVbe->pInt10->ax = S3_EXTBIOS_INFO;
    psav->pVbe->pInt10->bx = S3_SET_ACTIVE_DISP;
    if (psav->TvOn)
    	psav->pVbe->pInt10->cx = 0x87; /* lcd, tv, crt, duoview */
    else
    	psav->pVbe->pInt10->cx = 0x83; /* lcd, crt, duoview */
    xf86ExecX86int10(psav->pVbe->pInt10);
    
    SavageClearVM86Regs(psav->pVbe->pInt10);
    
    /* Establish the refresh rate for this mode. */
    psav->pVbe->pInt10->ax = S3_EXTBIOS_INFO;
    psav->pVbe->pInt10->bx = S3_SET_REFRESH;
    psav->pVbe->pInt10->cx = n & 0x1ff;
    psav->pVbe->pInt10->di = refresh & 0xffff;
    xf86ExecX86int10(psav->pVbe->pInt10);

    /* SR01:turn off screen */
    OUTREG8 (SEQ_ADDRESS_REG,0x01);
    byte = INREG8(SEQ_DATA_REG) | 0x20;
    OUTREG8(SEQ_DATA_REG,byte);
    
    psav->pVbe->pInt10->ax = BIOS_SET_VBE_MODE;
    psav->pVbe->pInt10->bx = n;
    xf86ExecX86int10(psav->pVbe->pInt10);
    
}

void
SavageSetVESAModeCrtc2( SavagePtr psav, int n, int refresh )
{

    xf86Msg(X_INFO,"SavageSetVESAModeCrtc2:mode=0x%x,refresh=%dHZ\n",n,refresh);

    SavageClearVM86Regs(psav->pVbe->pInt10);

    UnLockExtRegs();
    
    psav->pVbe->pInt10->ax = S3_EXTBIOS_INFO;
    psav->pVbe->pInt10->bx = S3_ALT_SET_ACTIVE_DISP;
    if (psav->TvOn)
    	psav->pVbe->pInt10->cx = 0x87; /* lcd, tv, crt, duoview */
    else
    	psav->pVbe->pInt10->cx = 0x83; /* lcd, crt, duoview */
    psav->pVbe->pInt10->dx = n & 0x1ff;
    psav->pVbe->pInt10->di = refresh & 0xffff;
    xf86ExecX86int10(psav->pVbe->pInt10);

}

void
SavageSetVESAMode( SavagePtr psav, int n, int Refresh )
{
    int iDevInfo;
    int iTVInfo;
    static Bool bFirstRun = TRUE;

    if (psav->IsSecondary) {
        SavageSetVESAModeCrtc2(psav, n, Refresh);
	return;
    }
    if (psav->IsPrimary) {
        SavageSetVESAModeCrtc1(psav, n, Refresh);
	return;
    }

    /* Get current display device status. */
    iDevInfo = SavageGetDevice(psav);
    iTVInfo = SavageGetTV(psav);

    if( bFirstRun == TRUE ) {
	bFirstRun = FALSE;
	/* run for the first time - store settings as current and original */
	psav->iDevInfoPrim = iDevInfo;
	psav->iDevInfo = iDevInfo;
	/* add features requested by the config to the current settings */
	if( psav->TvOn ) {
	    psav->iDevInfo |= TV_ACTIVE; /* TvOn is NOT TvOnly! */
	}
	if( psav->CrtOnly ) {
	    psav->iDevInfo = CRT_ACTIVE; /* cancels other settings */
	}
	psav->PAL = ((iTVInfo & BIOS_TV_PAL) != 0);
    } else { /* subsequent run */
	/* if the current settings are identical to the original, we likely
	   come from text mode - prefer the stored ones */
	/* but take the current ones if settings are different as they might
	   have been modified on-line (outside this driver) */
	if (psav->iDevInfoPrim != iDevInfo) {
	    psav->iDevInfo = iDevInfo;
	}
	/* update the configuration flags to reflect current status */
	psav->TvOn = ((psav->iDevInfo & TV_ACTIVE) != 0);
	/* only set CRT flag if no other output is active */
	psav->CrtOnly = (((psav->iDevInfo & ACTIVE_DEVICES) &
			  ((unsigned char) ~CRT_ACTIVE)) == 0);
    }

    /* fully adjust the DuoView flag */
    psav->iDevInfo = SavageCorrectDuoViewFlag(psav->iDevInfo, TRUE, TRUE);

    /* Establish the refresh rate for this mode. */

    SavageClearVM86Regs( psav->pVbe->pInt10 );
    psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extensions */
    psav->pVbe->pInt10->bx = 0x0001;	/* Set default refresh rate */
    psav->pVbe->pInt10->cx = n & 0x3fff;
    psav->pVbe->pInt10->di = Refresh & 0xffff;

    xf86ExecX86int10( psav->pVbe->pInt10 );

    /* Set TV type if TV is on. */
    if( psav->TvOn ) {
	/* make this conditional to avoid unnecessary writes */
	if (((psav->PAL) && (iTVInfo & BIOS_TV_NTSC))
	    || ((!(psav->PAL)) && (iTVInfo & BIOS_TV_PAL))) {
	    SavageClearVM86Regs( psav->pVbe->pInt10 );
	    psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extensions */
	    psav->pVbe->pInt10->bx = 0x0007;	/* TV extensions */
	    psav->pVbe->pInt10->cx = psav->PAL ? 0x08 : 0x04;
	    psav->pVbe->pInt10->dx = 0x0c;
	    xf86ExecX86int10( psav->pVbe->pInt10 );
	}
    }

    /* Manipulate output device set. */
    if( psav->iDevInfo != iDevInfo ) {
	SavageClearVM86Regs( psav->pVbe->pInt10 );
	psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extensions */
	psav->pVbe->pInt10->bx = 0x0003;	/* set active devices */
	psav->pVbe->pInt10->cx = psav->iDevInfo;
	xf86ExecX86int10( psav->pVbe->pInt10 );

	/* Re-fetch actual device set. */
	psav->iDevInfo = SavageGetDevice( psav );
	iDevInfo = psav->iDevInfo;
	psav->TvOn = ((iDevInfo & TV_ACTIVE) != 0);
	/* set CRT flag if no other output is active */
	psav->CrtOnly = ((iDevInfo & ((unsigned char) ~CRT_ACTIVE)) == 0);
    }

    /* Now, make this mode current. */

    if( xf86LoaderCheckSymbol( "VBESetVBEMode" ) )
    {
	if( !VBESetVBEMode( psav->pVbe, n, NULL ) )
	{
	    ErrorF("Set video mode failed\n");
	}
    }
}

void
SavageSetPanelEnabled( SavagePtr psav, Bool active )
{
    int iDevInfo;
    Bool bDoChange = 0;

    if( !psav->PanelX )
	return; /* no panel */

    iDevInfo = SavageGetDevice( psav );
    /*
     * This function (currently) gets called ONLY by the DPMS mode switching function.
     * Therefore, we should NOT change anything in the following cases:
     * - if the TV output is active, as we will corrupt the screen
     * - TODO: check conditions for CRT / dual modes
     * Anyway, we will make our settings consistent here and only write out something
     * if we need a change.
     */
    if ((iDevInfo & TV_ACTIVE) == 0) {
	/* continue, no TV will disturb us */
	if( active ) {
	    /* LCD should be on afterwards */
	    if ((iDevInfo & LCD_ACTIVE) == 0) {
		iDevInfo |= LCD_ACTIVE;
		/* we need a change */
		bDoChange = 1;
	    }
	} else {
	    /* LCD should be off afterwards */
	    if ((iDevInfo & LCD_ACTIVE) != 0) {
		iDevInfo &= ~LCD_ACTIVE;
		/* change requested */
		bDoChange = 1;
	    }
	}
    }

    /* now check if we really need to do anything */
    if (bDoChange != 0) {
	/* update the active device configuration */
	SavageClearVM86Regs( psav->pVbe->pInt10 );
	psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extensions */
	psav->pVbe->pInt10->bx = 0x0003;	/* set active devices */
	psav->pVbe->pInt10->cx = iDevInfo;
	xf86ExecX86int10( psav->pVbe->pInt10 );
    }
}

/* Function to get supported device list. */

static int SavageGetDevice( SavagePtr psav )
{
    SavageClearVM86Regs( psav->pVbe->pInt10 );
    psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extensions */
    psav->pVbe->pInt10->bx = 0x0103;	/* get active devices */

    xf86ExecX86int10( psav->pVbe->pInt10 );

    return (psav->pVbe->pInt10->cx) & (DUO_ON | ACTIVE_DEVICES);
}

/* Function to get TV format. */

int SavageGetTV( SavagePtr psav )
{
    SavageClearVM86Regs( psav->pVbe->pInt10 );
    psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extensions */
    psav->pVbe->pInt10->bx = 0x0107;	/* get TV mode */

    xf86ExecX86int10( psav->pVbe->pInt10 );

    return ((psav->pVbe->pInt10->cx) & 0xc); /* TV mode bits are bits 2 and 3 */
}

/* function for online update of current devices and TV format */
void SavageUpdateDevices(ScrnInfoPtr pScrn)
{
    SavagePtr psav;

    psav = SAVPTR(pScrn);
    psav->iDevInfo = SavageGetDevice(psav);
    psav->PAL = ((SavageGetTV(psav) & BIOS_TV_PAL) != 0);
    psav->TvOn = ((psav->iDevInfo & TV_ACTIVE) != 0);
    psav->CrtOnly = ((psav->iDevInfo & (unsigned char) ~CRT_ACTIVE) == 0);
}

/* Function to correct the DuoView flag in the case of a single display */

int SavageCorrectDuoViewFlag(int iDevInfo, Bool allowEnabling,
			     Bool allowDisabling)
{
    /* TODO: limit this function to be used only if no multi view mode is
     * active ("real" DuoView) */

    /*
     * This function can be used to correctly set the DuoView flag in case of
     * a single X display. It operates directly on an "iDevInfo" bit field, so
     * the caller has the choice which one should be checked.
     *
     * The DuoView flag is only necessary as soon as the TV output is involved.
     * Logic derived from the behaviour of the hardware features of HP XE3:
     * - if LCD or CRT or both are active and TV is NOT active, DuoView NEVER
     *   needs to be set
     * - if TV is the only active output, DuoView also does NOT need to be set
     * - if TV is active together with LCD or CRT or both, DuoView MUST be set
     *   to allow all outputs to work
     *
     * The two Bool flags allow the caller to set which operations should be
     * done on the bit field.
     * To use the full logic mentioned above, set both flags to TRUE.
     */

    Bool setDuoView = FALSE;

    /* decision logic */
    if ((iDevInfo & TV_ACTIVE) && 
	((iDevInfo & CRT_ACTIVE) || (iDevInfo & LCD_ACTIVE)))
	    setDuoView = TRUE;

    /* action logic */
    if (setDuoView && allowEnabling)
	    iDevInfo |= DUO_ON;
    if (!setDuoView && allowDisabling)
	    iDevInfo &= ~DUO_ON;

    return iDevInfo;
}

void
SavageFreeBIOSModeTable( SavagePtr psav, SavageModeTablePtr* ppTable )
{
    int i;
    SavageModeEntryPtr pMode = (*ppTable)->Modes;

    for( i = (*ppTable)->NumModes; i--; )
    {
	if( pMode->RefreshRate )
	{
	    free( pMode->RefreshRate );
	    pMode->RefreshRate = NULL;
	}
	pMode++;
    }

    free( *ppTable );
}


SavageModeTablePtr
SavageGetBIOSModeTable( SavagePtr psav, int iDepth )
{
    VbeInfoBlock *vbe;
    int nModes;
    SavageModeTablePtr pTable;

    if( !psav->pVbe )
	return 0;

    if (!(vbe = VBEGetVBEInfo(psav->pVbe)))
	return 0;

    nModes = SavageGetBIOSModes( psav, vbe, iDepth, NULL );

    pTable = (SavageModeTablePtr) 
	calloc( 1, sizeof(SavageModeTableRec) + 
		    (nModes-1) * sizeof(SavageModeEntry) );
    if( pTable ) {
	pTable->NumModes = nModes;
	SavageGetBIOSModes( psav, vbe, iDepth, pTable->Modes );
    }

    VBEFreeVBEInfo(vbe);

    return pTable;
}


unsigned short
SavageGetBIOSModes( 
    SavagePtr psav,
    VbeInfoBlock *vbe,
    int iDepth,
    SavageModeEntryPtr s3vModeTable )
{
    unsigned short iModeCount = 0;
    unsigned short int *mode_list;
    pointer vbeLinear = NULL;
    int vbeReal;
    struct vbe_mode_info_block * vmib;

    vbeLinear = xf86Int10AllocPages( psav->pVbe->pInt10, 1, &vbeReal );
    if( !vbeLinear )
    {
	ErrorF( "Cannot allocate scratch page in real mode memory." );
	return 0;
    }
    vmib = (struct vbe_mode_info_block *) vbeLinear;
    
    for (mode_list = vbe->VideoModePtr; *mode_list != 0xffff; mode_list++) {

	/*
	 * This is a HACK to work around what I believe is a BUG in the
	 * Toshiba Satellite BIOSes in 08/2000 and 09/2000.  The BIOS
	 * table for 1024x600 says it has six refresh rates, when in fact
	 * it only has 3.  When I ask for rate #4, the BIOS goes into an 
	 * infinite loop until the user interrupts it, usually by pressing
	 * Ctrl-Alt-F1.  For now, we'll just punt everything with a VESA
	 * number greater than or equal to 0200.
	 *
	 * This also prevents some strange and unusual results seen with
	 * the later ProSavage/PM133 BIOSes directly from S3/VIA.
	 */
	if( *mode_list >= 0x0200 )
	    continue;

	SavageClearVM86Regs( psav->pVbe->pInt10 );

	psav->pVbe->pInt10->ax = 0x4f01;
	psav->pVbe->pInt10->cx = *mode_list;
	psav->pVbe->pInt10->es = SEG_ADDR(vbeReal);
	psav->pVbe->pInt10->di = SEG_OFF(vbeReal);
	psav->pVbe->pInt10->num = 0x10;

	xf86ExecX86int10( psav->pVbe->pInt10 );

	if( 
	   (vmib->bits_per_pixel == iDepth) &&
	   (
	      (vmib->memory_model == VBE_MODEL_256) ||
	      (vmib->memory_model == VBE_MODEL_PACKED) ||
	      (vmib->memory_model == VBE_MODEL_RGB)
	   )
	)
	{
	    /* This mode is a match. */

	    iModeCount++;

	    /* If we're supposed to fetch information, do it now. */

	    if( s3vModeTable )
	    {
	        int iRefresh = 0;

		s3vModeTable->Width = vmib->x_resolution;
		s3vModeTable->Height = vmib->y_resolution;
		s3vModeTable->VesaMode = *mode_list;
		
		/* Query the refresh rates at this mode. */

		psav->pVbe->pInt10->cx = *mode_list;
		psav->pVbe->pInt10->dx = 0;

		do
		{
		    if( (iRefresh % 8) == 0 )
		    {
			if( s3vModeTable->RefreshRate )
			{
			    s3vModeTable->RefreshRate = (unsigned char *)
				realloc( 
				    s3vModeTable->RefreshRate,
				    (iRefresh+8) * sizeof(unsigned char)
				);
			}
			else
			{
			    s3vModeTable->RefreshRate = (unsigned char *)
				calloc( 
				    sizeof(unsigned char),
				    (iRefresh+8)
				);
			}
		    }

		    psav->pVbe->pInt10->ax = 0x4f14;	/* S3 extended functions */
		    psav->pVbe->pInt10->bx = 0x0201;	/* query refresh rates */
		    psav->pVbe->pInt10->num = 0x10;
		    xf86ExecX86int10( psav->pVbe->pInt10 );

		    s3vModeTable->RefreshRate[iRefresh++] = psav->pVbe->pInt10->di;
		}
		while( psav->pVbe->pInt10->dx );

		s3vModeTable->RefreshCount = iRefresh;

	    	s3vModeTable++;
	    }
	}
    }

    xf86Int10FreePages( psav->pVbe->pInt10, vbeLinear, 1 );

    return iModeCount;
}

ModeStatus SavageMatchBiosMode(ScrnInfoPtr pScrn,int width,int height,int refresh,
                              unsigned int *vesaMode,unsigned int *newRefresh)
{
    SavageModeEntryPtr pmt;
    Bool found = FALSE;
    SavagePtr psav = SAVPTR(pScrn);    
    int i,j;
    unsigned int chosenVesaMode = 0;
    unsigned int chosenRefresh = 0;
    
    /* Scan through our BIOS list to locate the closest valid mode. */
    
    /*
     * If we ever break 4GHz clocks on video boards, we'll need to
     * change this.
     * refresh = (mode->Clock * 1000) / (mode->HTotal * mode->VTotal);
     * now we use VRefresh directly,instead of by calculating from dot clock
     */

    for( i = 0, pmt = psav->ModeTable->Modes; 
	i < psav->ModeTable->NumModes;
	i++, pmt++ )
    {
	if( (pmt->Width == width) && 
	    (pmt->Height == height) )
	{
	    int jDelta = 99;
	    int jBest = 0;

	    /* We have an acceptable mode.  Find a refresh rate. */
	    chosenVesaMode = pmt->VesaMode;
            if (vesaMode)
                *vesaMode = chosenVesaMode;
	    for( j = 0; j < pmt->RefreshCount; j++ )
	    {
		if( pmt->RefreshRate[j] == refresh )
		{
		    /* Exact match. */
		    jBest = j;
		    break;
		}
		else if( iabs(pmt->RefreshRate[j] - refresh) < jDelta )
		{
		    jDelta = iabs(pmt->RefreshRate[j] - refresh);
		    jBest = j;
		}
	    }
	    chosenRefresh = pmt->RefreshRate[jBest];
            if (newRefresh)
                *newRefresh = chosenRefresh;
            found = TRUE;
	    break;
	}
    }

    if( found ) {
	/* Success: we found a match in the BIOS. */
	xf86DrvMsg(pScrn->scrnIndex, X_PROBED, 
		  "Chose mode %x at %dHz.\n", chosenVesaMode, chosenRefresh );
        return MODE_OK;
    } else {
	xf86DrvMsg(pScrn->scrnIndex, X_PROBED, 
		  "No suitable BIOS mode found for %dx%d %dHz.\n",
		  width, height, refresh);
        return MODE_NOMODE;
    }
}