summaryrefslogtreecommitdiff
path: root/src/via_cursor.c
blob: 58f77df7a9539b3a0eb489b087d1596882861a08 (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
/*
 * Copyright 2004-2009 Luc Verhaegen.
 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
 * Copyright 2001-2003 S3 Graphics, Inc. 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, sub license,
 * and/or sell copies of the Software, and to 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*************************************************************************
 *
 *  File:       via_cursor.c
 *  Content:    Hardware cursor support for VIA/S3G UniChrome
 *
 ************************************************************************/

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

#include "via_driver.h"
#include "via_id.h"
#include "via_crtc.h"

#include "xf86Cursor.h"

#ifndef _XF86_ANSIC_H
#include <string.h>
#endif

#include "via_2dregs.h"

/*
 *
 */
struct ViaCursor {
    int scrnIndex;

    xf86CursorInfoPtr Info;
    int Address;
    int Size;

    /* for save/restore */
    unsigned char *Image;
    CARD32 FG;
    CARD32 BG;
    CARD32 MC;
};

/*
 *
 */
static void
ViaCursorShow(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    CARD32 dwCursorMode;

    VIAFUNC(pVia);

    dwCursorMode = VIAGETREG(VIA_REG_CURSOR_MODE);

    /* Turn on Hardware Cursor */
    VIASETREG(VIA_REG_CURSOR_MODE, dwCursorMode | 0x01);
}

/*
 *
 */
static void
ViaCursorHide(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    CARD32 dwCursorMode;

    VIAFUNC(pVia);

    dwCursorMode = VIAGETREG(VIA_REG_CURSOR_MODE);

    /* Turn cursor off. */
    VIASETREG(VIA_REG_CURSOR_MODE, dwCursorMode & 0xFFFFFFFE);
}

/*
 *
 */
static void
ViaCursorLoadImage(ScrnInfoPtr pScrn, unsigned char *src)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaCursor *Cursor = pVia->Cursor;
    CARD32 dwCursorMode;

    VIAFUNC(pVia);

    VIAAccelSync(pScrn);

    dwCursorMode = VIAGETREG(VIA_REG_CURSOR_MODE);

    /* Turn cursor off. */
    VIASETREG(VIA_REG_CURSOR_MODE, dwCursorMode & 0xFFFFFFFE);

    /* Upload the cursor image to the frame buffer. */
    memcpy(pVia->FBBase + Cursor->Address, src, Cursor->Size);

    /* Restore cursor status */
    VIASETREG(VIA_REG_CURSOR_MODE, dwCursorMode);
}

/*
 *
 */
static void
ViaCursorSetPosition(ScrnInfoPtr pScrn, int x, int y)
{
    VIAPtr pVia = VIAPTR(pScrn);
    CARD8 xoff, yoff;
    CARD32 dwCursorMode;

    /* Handle CRTC Scaling */
    if (pVia->VisibleWidth != pVia->ScaledWidth)
	x = (x * pVia->ScaledWidth) / pVia->VisibleWidth;
    if (pVia->VisibleHeight != pVia->ScaledHeight)
	y = (y * pVia->ScaledHeight) / pVia->VisibleHeight;

    if (x < 0) {
	xoff = ((-x) & 0xFE);
	x = 0;
    } else
	xoff = 0;

    if (y < 0) {
	yoff = ((-y) & 0xFE);
	y = 0;
    } else
	yoff = 0;

    /* Hide cursor before set cursor position in order to avoid ghost cursor
     * image when directly set cursor position. It should be a HW bug but
     * we can use patch by SW. */
    dwCursorMode = VIAGETREG(VIA_REG_CURSOR_MODE);

    /* Turn cursor off. */
    VIASETREG(VIA_REG_CURSOR_MODE, dwCursorMode & 0xFFFFFFFE);

    VIASETREG(VIA_REG_CURSOR_ORG, ((xoff << 16) | (yoff & 0x003f)));
    VIASETREG(VIA_REG_CURSOR_POS, ((x << 16) | (y & 0x07ff)));

    /* Restore cursor status */
    VIASETREG(VIA_REG_CURSOR_MODE, dwCursorMode);
}

/*
 *
 */
static void
ViaCursorSetColors(ScrnInfoPtr pScrn, int bg, int fg)
{
    VIAPtr pVia = VIAPTR(pScrn);

    VIASETREG(VIA_REG_CURSOR_FG, fg);
    VIASETREG(VIA_REG_CURSOR_BG, bg);
}

/*
 * Grabs memory from the tail end of the FB.
 */
Bool
VIACursorFBGrab(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    CARD32 Offset, Size;

    pVia->FBCursorStart = -1;
    pVia->FBCursorSize = 0;

    switch(pVia->Chipset) {
    case VT3122:
    case VT7205:
    case VT3108:
    case VT3344:
	break;
    default:
	xf86DrvMsg(pVia->scrnIndex, X_INFO, "%s: %s is not supported.\n",
		   __func__, pScrn->chipset);
	return FALSE;
    }

    /* cursor is a 2bit plane, so 64*64*2 / 8 = 1024.
       hw is also quite picky about alignment. */
    Size = 1024;
    Offset = (pVia->FBFreeStart + pVia->FBFreeSize - Size) & ~(Size - 1);
    if (Offset < pVia->FBFreeStart) {
	Size = 256;
	Offset = (pVia->FBFreeStart + pVia->FBFreeSize - Size) & ~(Size - 1);
	if (Offset < pVia->FBFreeStart) {
	    xf86DrvMsg(pVia->scrnIndex, X_INFO, "%s: No free FB available for"
		       " hardware cursor.\n", __func__);
	    return FALSE;
	}
    }

    pVia->FBCursorStart = Offset;
    pVia->FBCursorSize = Size;
    pVia->FBFreeSize = Offset - pVia->FBFreeStart;

    ViaDebug(pVia->scrnIndex, "%s: Hardware Cursor memory at 0x%08X (%03X)\n",
	     __func__, pVia->FBCursorStart, pVia->FBCursorSize);
    return TRUE;
}


/*
 *
 */
struct ViaCursor *
VIACursorInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
{
    VIAPtr pVia = VIAPTR(pScrn);
    xf86CursorInfoPtr infoPtr;
    struct ViaCursor *Cursor;
    CARD32 Mode = 0;

    VIAFUNC(pVia);

    switch(pVia->Chipset) {
    case VT3122:
    case VT7205:
    case VT3108:
    case VT3344:
	break;
    default:
	xf86DrvMsg(pVia->scrnIndex, X_INFO, "%s: %s is not supported.\n",
		   __func__, pScrn->chipset);
	return NULL;
    }

    if (pVia->Crtc[1]->Active && !pVia->Crtc[0]->Active) {
	if ((pVia->Chipset == VT3122) || (pVia->Chipset == VT7205)) {
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Hardware cursor is "
                       "not supported on the Secondary CRTC.\n");
	    return NULL;
	} else
	    Mode |= 0x80000000; /* CRTC2 */
    }

    if (!pVia->FBCursorSize) {
	xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "%s: No framebuffer reserved for"
		   " Hardware cursor.\n", __func__);
	return NULL;
    }

    infoPtr = xf86CreateCursorInfoRec();
    if (!infoPtr)
	return NULL;

    infoPtr->Flags = HARDWARE_CURSOR_AND_SOURCE_WITH_MASK |
		     HARDWARE_CURSOR_TRUECOLOR_AT_8BPP |
		     HARDWARE_CURSOR_INVERT_MASK |
		     HARDWARE_CURSOR_BIT_ORDER_MSBFIRST ;

    infoPtr->SetCursorColors = ViaCursorSetColors;
    infoPtr->SetCursorPosition = ViaCursorSetPosition;
    infoPtr->LoadCursorImage = ViaCursorLoadImage;
    infoPtr->HideCursor = ViaCursorHide;
    infoPtr->ShowCursor = ViaCursorShow;
    infoPtr->UseHWCursor = NULL;

    Cursor = xnfcalloc(1, sizeof(struct ViaCursor));
    Cursor->scrnIndex = pVia->scrnIndex;

    Cursor->Address = pVia->FBCursorStart;
    Cursor->Size = pVia->FBCursorSize;
    Mode |= Cursor->Address;

    if (Cursor->Size == 1024) {
	xf86DrvMsg(pVia->scrnIndex, X_INFO, "Using 64x64 HW Cursor.\n");

	infoPtr->MaxWidth = 64;
	infoPtr->MaxHeight = 64;
	infoPtr->Flags |= HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64;
    } else {
	xf86DrvMsg(pVia->scrnIndex, X_INFO, "Using 32x32 HW Cursor.\n");

	infoPtr->MaxWidth = 32;
	infoPtr->MaxHeight = 32;
	infoPtr->Flags |= HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_32;

	Mode |= 0x00000002;
    }

    if (!xf86InitCursor(pScreen, infoPtr)) {
	xf86DestroyCursorInfoRec(infoPtr);
	xfree(Cursor);
	return NULL;
    }

    Cursor->Info = infoPtr;

    VIASETREG(VIA_REG_CURSOR_MODE, Mode);

    return Cursor;
}

/*
 *
 */
void
VIACursorDestroy(VIAPtr pVia)
{
    struct ViaCursor *Cursor = pVia->Cursor;

    if (!Cursor)
	return;

    VIAFUNC(pVia);

    xf86DestroyCursorInfoRec(Cursor->Info);
    if (Cursor->Image)
	xfree(Cursor->Image);

    xfree(Cursor);
    pVia->Cursor = NULL;
}

/*
 *
 */
void
VIACursorStore(VIAPtr pVia)
{
    struct ViaCursor *Cursor = pVia->Cursor;

    if (!Cursor)
	return;

    VIAFUNC(pVia);

    if (Cursor->Image) {
	xf86DrvMsg(pVia->scrnIndex, X_WARNING, "%s: stale image left.\n", __func__);
	xfree(Cursor->Image);
    }

    Cursor->Image = xcalloc(1, Cursor->Size);
    memcpy(Cursor->Image, pVia->FBBase + Cursor->Address, Cursor->Size);

    Cursor->FG = (CARD32)VIAGETREG(VIA_REG_CURSOR_FG);
    Cursor->BG = (CARD32)VIAGETREG(VIA_REG_CURSOR_BG);
    Cursor->MC = (CARD32)VIAGETREG(VIA_REG_CURSOR_MODE);
}

/*
 *
 */
void
VIACursorRestore(VIAPtr pVia)
{
    struct ViaCursor *Cursor = pVia->Cursor;

    if (!Cursor)
	return;

    VIAFUNC(pVia);

    if (Cursor->Image) {
	memcpy(pVia->FBBase + Cursor->Address, Cursor->Image, Cursor->Size);

	VIASETREG(VIA_REG_CURSOR_FG, Cursor->FG);
	VIASETREG(VIA_REG_CURSOR_BG, Cursor->BG);
	VIASETREG(VIA_REG_CURSOR_MODE, Cursor->MC);

	xfree(Cursor->Image);
	Cursor->Image = NULL;
    } else
	xf86DrvMsg(pVia->scrnIndex, X_ERROR, "%s: No cursor image stored.\n", __func__);
}

/*
 *
 */
void
VIACursorHide(VIAPtr pVia)
{
    struct ViaCursor *Cursor = pVia->Cursor;

    if (!Cursor)
	return;

    VIAFUNC(pVia);

    Cursor->Info->HideCursor(xf86Screens[Cursor->scrnIndex]);
}