summaryrefslogtreecommitdiff
path: root/hw/kdrive/via/viadraw.c
blob: 6f09314639565e4313dd266ad19e29662ac204bc (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
/*
 * Copyright © 2004 Ralph Thomas
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Ralph Thomas not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Ralph Thomas makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * RALPH THOMAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL RALPH THOMAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
/*
** VIA CLE266 driver
** Copyright 2004 (C) Ralph Thomas <ralpht@gmail.com>
**
** http://www.viatech.com.tw/
**
** This code is for accelerated drawing of solids and accelerated
** copies. Note that there is currently no software FIFO implemented,
** and no documentation on any hardware FIFO.
*/

#include "via.h"
#include "viadraw.h"
#include "via_regs.h"
#include <sched.h>

/*
** A global to contain card information between calls into this file.
** XXX: This is totally brain-damaged. Why don't I get the information
**      I want in viaSolid/viaCopy/viaDoneXXX?
*/
static ViaCardInfo* card;

/*
** Translation table from GC raster operation values into ROP3 values
** that the VIA chip understands.
**
** viaPatternRop is used by viaPrepareSolid.
** viaCopyRop is used by viaPrepareCopy.
*/
CARD8 viaPatternRop[16] = {
	/* GXclear      */	0x00,	/* ROP_0	0 */
	/* GXand        */	0xA0,	/* ROP_DPa	src AND dst */
	/* GXandReverse */	0x50,	/* ROP_PDna	src AND NOT dst */
	/* GXcopy       */	0xF0,	/* ROP_P	src */
	/* GXandInverted*/	0x0A,	/* ROP_DPna	NOT src AND dst */
	/* GXnoop       */	0xAA,	/* ROP_D	dst */
	/* GXxor        */	0x5A,	/* ROP_DPx	src XOR dst */
	/* GXor         */	0xFA,	/* ROP_DPo	src OR dst */
	/* GXnor        */	0x05,	/* ROP_DPon	NOT src AND NOT dst */
	/* GXequiv      */	0xA5,	/* ROP_PDxn	NOT src XOR dst */
	/* GXinvert     */	0x55,	/* ROP_Dn	NOT dst */
	/* GXorReverse  */	0xF5,	/* ROP_PDno	src OR NOT dst */
	/* GXcopyInverted*/	0x0F, 	/* ROP_Pn	NOT src */
	/* GXorInverted */	0xAF,	/* ROP_DPno	NOT src OR dst */
	/* GXnand       */	0x5F,	/* ROP_DPan	NOT src OR NOT dst */
	/* GXset        */	0xFF,	/* ROP_1	1 */
};

CARD8 viaCopyRop[16] = {
	/* GXclear	*/	0x00,	/* ROP_0	0 */
	/* GXand	*/	0x88,	/* ROP_DSa	src AND dst */
	/* GXandReverse	*/	0x44,	/* ROP_SDna	src AND NOT dst */
	/* GXcopy	*/	0xCC,	/* ROP_S	src */
	/* GXandInverted*/	0x22,	/* ROP_DSna	NOT src AND dst */
	/* GXnoop	*/	0xAA,	/* ROP_D	dst */
	/* GXxor	*/	0x66,	/* ROP_DSx	src XOR dst */
	/* GXor		*/	0xEE,	/* ROP_DSo	src OR dst */
	/* GXnor	*/	0x11,	/* ROP_DSon	NOT src AND NOT dst */
	/* GXequiv	*/	0x99,	/* ROP_DSxn	NOT src XOR dst */
	/* GXinvert	*/	0x55,	/* ROP_Dn	NOT dst */
	/* GXorReverse	*/	0xDD,	/* ROP_SDno	src OR NOT dst */
	/* GXcopyInverted*/	0x33,	/* ROP_Sn	NOT src */
	/* GXorInverted	*/	0xBB,	/* ROP_DSno	NOT src OR dst */
	/* GXnand	*/	0x77,	/* ROP_DSan	NOT src OR NOT dst */
	/* GXset	*/	0xFF,	/* ROP_1	1 */
};

/*
** void viaWaitIdle( ViaCardInfo* viac )
**
** Description:
**	Block up the CPU while waiting for the last command sent to
**	the chip to complete. As an experiment I'm going to try to
**	yield my process to others instead of just tight looping.
**
** Arguments:
**	viac	VIA-driver specific chip information
**
** Return:
**	None.
*/
void
viaWaitIdle( ViaCardInfo* viac ) {
	while( INREG32( VIA_REG_STATUS ) & VIA_BUSY )
		sched_yield();
}

/*
** Bool viaPrepareSolid( PixmapPtr pPixmap, int alu, Pixel planemask, Pixel fg )
**
** Description:
**	Decide if the specified solid fill operation can be accelerated or not,
**	and if the fill can be accelerated, prepare the hardware for doing it.
**
** Arguments:
**	pPixmap		Pixmap to draw solid into.
**	alu		Raster operation to draw using, these are the same
**			values which get set by XSetFunction(3X). See the
**			Xlib PM p. 140 for a list of raster operations as
**			well as descriptions.
**	planemask	This is the GC plane mask. We only copy bits which
**			match the plane mask.
**	fg		The foreground pixel of the GC, the pixel to draw in.
**
** Return:
**	TRUE	This operation can be accelerated, call viaSolid to actually
**		have it drawn.
**	FALSE	This operation cannot be accelerated, fall back to software.
**
** See Also:
**	viaSolid - the function which actually draws the solid.
*/
static Bool
viaPrepareSolid( PixmapPtr pPixmap, int alu, Pixel planeMask, Pixel fg ) {
	ScreenPtr pScreen = pPixmap->drawable.pScreen;
	KdScreenPriv( pScreen );
	ViaCardInfo* viac = pScreenPriv->card->driver;

	/*
	** We don't accelerate when the plane mask is not everything.
	*/
	if( ~planeMask & FbFullMask( pPixmap->drawable.depth ) ) return FALSE;

	/*
	** Compose the command, then store the composed command and color
	** in the viac structure so that when viaSolid gets called we can
	** write them out.
	*/
	viac->savedCommand = VIA_GEC_BLT | VIA_GEC_FIXCOLOR_PAT |
			     (viaPatternRop[alu]  << 24); 
	viac->savedFgColor = fg;

	/*
	** Store pointer to chip information, due to brain-damaged KAA.
	*/
	card = viac;

	return TRUE;
}

/*
** void viaSolid( int x1, int y1, int x2, int y2 )
**
** Description:
**	Perform a solid fill, using the data that was stored by viaPrepareSolid.
**
** Arguments:
**	x1	x-coordinate of fill origin
**	y1	y-coordinate of fill origin
**	x2	x-coordinate of fill end point
**	y2	y-coordinate of fill end point
**
** Return:
**	None.
**
** See Also:
**	viaPrepareSolid - the function that composes the GE command and saves
**			  the color for us.
*/
static void
viaSolid( int x1, int y1, int x2, int y2 ) {
	ViaCardInfo* viac = card;
	int w = x2 - x1; int h = y2 - y1;

	if( !viac ) return;
	if( !w || !h ) return;

	/*
	** Wait for the card to finish the current draw.
	*/
	viaWaitIdle( viac );

	/*
	** Do the draw.
	*/
	OUTREG32( VIA_REG_DSTPOS, ((y1 << 16) | x1) );
	OUTREG32( VIA_REG_DIMENSION, (((h - 1) << 16) | (w - 1)) );
	OUTREG32( VIA_REG_FGCOLOR, viac->savedFgColor );
	OUTREG32( VIA_REG_GECMD, viac->savedCommand );
}

/*
** void viaDoneSolid
**
** Description:
**	Finish up drawing of the solid.
**
** Arguments:
**	None.
**
** Return:
**	None.
*/
static void
viaDoneSolid() {
	;
}

/*
** Bool viaPrepareCopy( PixmapPtr pSrcPixmap, PixmapPtr pDestPixmap, int dx,
**			int dy, int alu, Pixel planeMask )
**
** Description:
**	Set up the VIA chip for a BitBlt.
**
** Arguments:
**	pSrcPixmap	the source pixmap to copy from
**	pDestPixmap	the destination pixmap to copy to
**	dx		direction of copy in x
**	dy		direction of copy in y
**	alu		Raster operation to draw using, these are the same
**			values which get set by XSetFunction(3X). See the
**			Xlib PM p. 140 for a list of raster operations as
**			well as descriptions.
**	planeMask	This is the GC plane mask. We only copy bits which
**			match the plane mask.
**
** Return:
**	TRUE	the requested copy operation can be accelerated using hardware,
**		call viaCopy next.
**	FALSE	the requested copy operation cannot be accelerated using
**		hardware - fallback to software.
**
** See Also:
**	viaCopy	- the function which does the actual copy.
*/
static Bool
viaPrepareCopy( PixmapPtr pSrcPixmap, PixmapPtr pDestPixmap, int dx, int dy,
		int alu, Pixel planeMask ) {
	ScreenPtr pScreen = pDestPixmap->drawable.pScreen;
	KdScreenPriv( pScreen );
	ViaCardInfo* viac = pScreenPriv->card->driver;

	/*
	** Don't accelerate when the plane mask is set.
	*/
	if( ~planeMask & FbFullMask( pDestPixmap->drawable.depth ) ) return FALSE;

	viac->savedCommand = VIA_GEC_BLT | (viaCopyRop[alu] << 24);

	if( dx < 0 ) viac->savedCommand |= VIA_GEC_DECX;
	if( dy < 0 ) viac->savedCommand |= VIA_GEC_DECY;

	/*
	** Store pointer to chip structure, due to brain-damaged KAA.
	*/
	card = viac;

	return TRUE;
}

/*
** void viaCopy( int srcX, int srcY, int dstX, int dstY, int w, int h )
**
** Description:
**	Perform a BitBlt from one screen area to another.
**
** Arguments:
**	srcX	source x-coordinate
**	srcY	source y-coordinate
**	dstX	destination x-coordinate
**	dstY	destination y-coordinate
**	w	width of area to copy (pixels)
**	h	height of area to copy (pixels)
**
** Return:
**	None.
**
** See Also:
**	viaPrepareCopy	- the function which sets up for the copy.
*/
static void
viaCopy( int srcX, int srcY, int dstX, int dstY, int w, int h ) {
	ViaCardInfo* viac = card;

	if( !viac ) return;
	if( !w | !h ) return;

	/*
	** XXX: Check these two "if"s out.
	*/
	if( viac->savedCommand & VIA_GEC_DECX ) {
		srcX += ( w - 1 );
		dstX += ( w - 1 );
	}

	if( viac->savedCommand & VIA_GEC_DECY ) {
		srcY += ( h - 1 );
		dstY += ( h - 1 );
	}

	OUTREG32( VIA_REG_SRCPOS, ((srcY << 16) | srcX) );
	OUTREG32( VIA_REG_DSTPOS, ((dstY << 16) | dstX) );
	OUTREG32( VIA_REG_DIMENSION, (((h - 1) << 16) | (w - 1)) );
	OUTREG32( VIA_REG_GECMD, viac->savedCommand );
}

/*
** void viaDoneCopy()
**
** Description:
**	Finish up the copy.
**
** Arguments:
**	None.
**
** Return:
**	None.
*/
static void
viaDoneCopy() {
	;
}

/*
** viaKaa structure
**
** Description:
**	Structure to contain function pointers to accelerated KAA operations
**	in this driver.
*/
KaaScreenInfoRec viaKaa = {
	viaPrepareSolid,
	viaSolid,
	viaDoneSolid,

	viaPrepareCopy,
	viaCopy,
	viaDoneCopy,
};

/*
** Bool viaDrawInit( ScreenPtr pScreen )
**
** Description:
**	Initialize the 2D acceleration hardware and register the KAA
**	acceleration layer with the VIA acceleration functions (above).
**
** Arguments:
**	pScreen		Pointer to screen structure for the screen we're
**			enabling acceleration on.
**
** Return:
**	TRUE	initialization and setup of KAA acceleration was successful.
**	FALSE	initialization and setup of KAA acceleration failed.
*/
Bool
viaDrawInit( ScreenPtr pScreen ) {
	KdScreenPriv( pScreen );
	ViaCardInfo* viac = pScreenPriv->card->driver;
	CARD32 geMode = 0;

	if( !viac ) return FALSE;
	DebugF( "viac->mapBase = 0x%x\n", viac->mapBase );

	/*
	** We reset the 2D engine to a known state by setting all of it's
	** registers to zero.
	*/
	OUTREG32( VIA_REG_GEMODE, 0x0 );
	OUTREG32( VIA_REG_SRCPOS, 0x0 );
	OUTREG32( VIA_REG_DSTPOS, 0x0 );
	OUTREG32( VIA_REG_DIMENSION, 0x0 );
	OUTREG32( VIA_REG_PATADDR, 0x0 );
	OUTREG32( VIA_REG_FGCOLOR, 0x0 );
	OUTREG32( VIA_REG_BGCOLOR, 0x0 );
	OUTREG32( VIA_REG_CLIPTL, 0x0 );
	OUTREG32( VIA_REG_CLIPBR, 0x0 );
	OUTREG32( VIA_REG_OFFSET, 0x0 );
	OUTREG32( VIA_REG_KEYCONTROL, 0x0 );
	OUTREG32( VIA_REG_SRCBASE, 0x0 );
	OUTREG32( VIA_REG_DSTBASE, 0x0 );
	OUTREG32( VIA_REG_PITCH, 0x0 );
	OUTREG32( VIA_REG_MONOPAT0, 0x0 );
	OUTREG32( VIA_REG_MONOPAT1, 0x0 );

	/*
	** Set the GE mode up.
	** XXX: What happens in 24bpp mode?
	*/
	switch( pScreenPriv->screen->fb[0].bitsPerPixel ) {
	  case 16:
		geMode = VIA_GEM_16bpp;
		break;
	  case 32:
		geMode = VIA_GEM_32bpp;
		break;
	  default:
		geMode = VIA_GEM_8bpp;
		break;
	}

	OUTREG32( VIA_REG_GEMODE, geMode );

	/*
	** Set the source and destination base addresses, and set pitch.
	*/
	OUTREG32( VIA_REG_SRCBASE, 0x0 );
	OUTREG32( VIA_REG_DSTBASE, 0x0 );
	OUTREG32( VIA_REG_PITCH, VIA_PITCH_ENABLE |
	       ((pScreen->width * pScreenPriv->screen->fb[0].bitsPerPixel >> 3) >> 3) |
               (((pScreen->width * pScreenPriv->screen->fb[0].bitsPerPixel >> 3) >> 3) << 16));

	DebugF( "Initialized 2D engine!\n" );

	return kaaDrawInit( pScreen, &viaKaa );
}

/*
** void viaDrawEnable( ScreenPtr pScreen )
**
** Description:
**	Enable accelerated drawing on the specified screen.
**
** Arguments:
**	pScreen		Pointer to screen structure for the screen we're
**			enabling acceleration on.
**
** Return:
**	None.
*/
void
viaDrawEnable( ScreenPtr pScreen ) {
	KdMarkSync( pScreen );
}

/*
** void viaDrawDisable( ScreenPtr pScreen )
**
** Description:
**	Disable accelerated drawing to the specified screen.
**
** Arguments:
**	pScreen		Pointer to screen structure for the screen we're
**			disabling acceleration on.
**
** Return:
**	None
*/
void
viaDrawDisable( ScreenPtr pScreen ) {
}

/*
** void viaDrawFini( ScreenPtr pScreen )
**
** Description:
**	Shutdown accelerated drawing and free associated strucures and
**	resources.
**
** Arguments:
**	pScreen		Pointer to screen structure for the screen we're
**			disabling acceleration on.
**
** Return:
**	None.
*/
void
viaDrawFini( ScreenPtr pScreen ) {
}

/*
** void viaDrawSync( ScreenPtr pScreen )
**
** Description:
**	Block until the graphics chip has finished all outstanding drawing
**	operations and the framebuffer contents is static.
**
** Arguments:
**	pScreen		Pointer to screen strucutre for the screen we're
**			waiting for drawing to end on.
**
** Return:
**	None.
*/
void
viaDrawSync( ScreenPtr pScreen ) {
	KdScreenPriv( pScreen );
	ViaCardInfo* viac = pScreenPriv->card->driver;

	viaWaitIdle( viac );
}