Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / drivers / video / sis / sis_accel.c
1 /*
2  * SiS 300/540/630[S]/730[S],
3  * SiS 315[E|PRO]/550/[M]650/651/[M]661[F|M]X/740/[M]741[GX]/330/[M]760[GX],
4  * XGI V3XT/V5/V8, Z7
5  * frame buffer driver for Linux kernels >= 2.4.14 and >=2.6.3
6  *
7  * 2D acceleration part
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the named License,
12  * or any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
22  *
23  * Based on the XFree86/X.org driver which is
24  *     Copyright (C) 2001-2005 by Thomas Winischhofer, Vienna, Austria
25  *
26  * Author: Thomas Winischhofer <thomas@winischhofer.net>
27  *                      (see http://www.winischhofer.net/
28  *                      for more information and updates)
29  */
30
31 #include <linux/config.h>
32 #include <linux/version.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/fb.h>
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
37 #include <linux/console.h>
38 #endif
39 #include <linux/ioport.h>
40 #include <linux/types.h>
41
42 #include <asm/io.h>
43
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
45 #include <video/fbcon.h>
46 #include <video/fbcon-cfb8.h>
47 #include <video/fbcon-cfb16.h>
48 #include <video/fbcon-cfb24.h>
49 #include <video/fbcon-cfb32.h>
50 #endif
51
52 #include "sis.h"
53 #include "sis_accel.h"
54
55 static const u8 sisALUConv[] =
56 {
57     0x00,       /* dest = 0;            0,      GXclear,        0 */
58     0x88,       /* dest &= src;         DSa,    GXand,          0x1 */
59     0x44,       /* dest = src & ~dest;  SDna,   GXandReverse,   0x2 */
60     0xCC,       /* dest = src;          S,      GXcopy,         0x3 */
61     0x22,       /* dest &= ~src;        DSna,   GXandInverted,  0x4 */
62     0xAA,       /* dest = dest;         D,      GXnoop,         0x5 */
63     0x66,       /* dest = ^src;         DSx,    GXxor,          0x6 */
64     0xEE,       /* dest |= src;         DSo,    GXor,           0x7 */
65     0x11,       /* dest = ~src & ~dest; DSon,   GXnor,          0x8 */
66     0x99,       /* dest ^= ~src ;       DSxn,   GXequiv,        0x9 */
67     0x55,       /* dest = ~dest;        Dn,     GXInvert,       0xA */
68     0xDD,       /* dest = src|~dest ;   SDno,   GXorReverse,    0xB */
69     0x33,       /* dest = ~src;         Sn,     GXcopyInverted, 0xC */
70     0xBB,       /* dest |= ~src;        DSno,   GXorInverted,   0xD */
71     0x77,       /* dest = ~src|~dest;   DSan,   GXnand,         0xE */
72     0xFF,       /* dest = 0xFF;         1,      GXset,          0xF */
73 };
74 /* same ROP but with Pattern as Source */
75 static const u8 sisPatALUConv[] =
76 {
77     0x00,       /* dest = 0;            0,      GXclear,        0 */
78     0xA0,       /* dest &= src;         DPa,    GXand,          0x1 */
79     0x50,       /* dest = src & ~dest;  PDna,   GXandReverse,   0x2 */
80     0xF0,       /* dest = src;          P,      GXcopy,         0x3 */
81     0x0A,       /* dest &= ~src;        DPna,   GXandInverted,  0x4 */
82     0xAA,       /* dest = dest;         D,      GXnoop,         0x5 */
83     0x5A,       /* dest = ^src;         DPx,    GXxor,          0x6 */
84     0xFA,       /* dest |= src;         DPo,    GXor,           0x7 */
85     0x05,       /* dest = ~src & ~dest; DPon,   GXnor,          0x8 */
86     0xA5,       /* dest ^= ~src ;       DPxn,   GXequiv,        0x9 */
87     0x55,       /* dest = ~dest;        Dn,     GXInvert,       0xA */
88     0xF5,       /* dest = src|~dest ;   PDno,   GXorReverse,    0xB */
89     0x0F,       /* dest = ~src;         Pn,     GXcopyInverted, 0xC */
90     0xAF,       /* dest |= ~src;        DPno,   GXorInverted,   0xD */
91     0x5F,       /* dest = ~src|~dest;   DPan,   GXnand,         0xE */
92     0xFF,       /* dest = 0xFF;         1,      GXset,          0xF */
93 };
94
95 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,34)
96 static const int myrops[] = {
97         3, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
98 };
99 #endif
100
101 /* 300 series ----------------------------------------------------- */
102 #ifdef CONFIG_FB_SIS_300
103 static void
104 SiS300Sync(struct sis_video_info *ivideo)
105 {
106         SiS300Idle
107 }
108
109 static void
110 SiS300SetupForScreenToScreenCopy(struct sis_video_info *ivideo, int xdir, int ydir,
111                                  int rop, int trans_color)
112 {
113         SiS300SetupDSTColorDepth(ivideo->DstColor);
114         SiS300SetupSRCPitch(ivideo->video_linelength)
115         SiS300SetupDSTRect(ivideo->video_linelength, 0xffff)
116
117         if(trans_color != -1) {
118                 SiS300SetupROP(0x0A)
119                 SiS300SetupSRCTrans(trans_color)
120                 SiS300SetupCMDFlag(TRANSPARENT_BITBLT)
121         } else {
122                 SiS300SetupROP(sisALUConv[rop])
123         }
124         if(xdir > 0) {
125                 SiS300SetupCMDFlag(X_INC)
126         }
127         if(ydir > 0) {
128                 SiS300SetupCMDFlag(Y_INC)
129         }
130 }
131
132 static void
133 SiS300SubsequentScreenToScreenCopy(struct sis_video_info *ivideo, int src_x,
134                                    int src_y, int dst_x, int dst_y, int width, int height)
135 {
136         u32 srcbase = 0, dstbase = 0;
137
138         if(src_y >= 2048) {
139                 srcbase = ivideo->video_linelength * src_y;
140                 src_y = 0;
141         }
142         if(dst_y >= 2048) {
143                 dstbase = ivideo->video_linelength * dst_y;
144                 dst_y = 0;
145         }
146
147         SiS300SetupSRCBase(srcbase);
148         SiS300SetupDSTBase(dstbase);
149
150         if(!(ivideo->CommandReg & X_INC))  {
151                 src_x += width-1;
152                 dst_x += width-1;
153         }
154         if(!(ivideo->CommandReg & Y_INC))  {
155                 src_y += height-1;
156                 dst_y += height-1;
157         }
158         SiS300SetupRect(width, height)
159         SiS300SetupSRCXY(src_x, src_y)
160         SiS300SetupDSTXY(dst_x, dst_y)
161         SiS300DoCMD
162 }
163
164 static void
165 SiS300SetupForSolidFill(struct sis_video_info *ivideo, u32 color, int rop)
166 {
167         SiS300SetupPATFG(color)
168         SiS300SetupDSTRect(ivideo->video_linelength, 0xffff)
169         SiS300SetupDSTColorDepth(ivideo->DstColor);
170         SiS300SetupROP(sisPatALUConv[rop])
171         SiS300SetupCMDFlag(PATFG)
172 }
173
174 static void
175 SiS300SubsequentSolidFillRect(struct sis_video_info *ivideo, int x, int y, int w, int h)
176 {
177         u32 dstbase = 0;
178
179         if(y >= 2048) {
180                 dstbase = ivideo->video_linelength * y;
181                 y = 0;
182         }
183         SiS300SetupDSTBase(dstbase)
184         SiS300SetupDSTXY(x,y)
185         SiS300SetupRect(w,h)
186         SiS300SetupCMDFlag(X_INC | Y_INC | BITBLT)
187         SiS300DoCMD
188 }
189 #endif
190
191 /* 315/330/340 series ---------------------------------------------- */
192
193 #ifdef CONFIG_FB_SIS_315
194 static void
195 SiS310Sync(struct sis_video_info *ivideo)
196 {
197         SiS310Idle
198 }
199
200 static void
201 SiS310SetupForScreenToScreenCopy(struct sis_video_info *ivideo, int rop, int trans_color)
202 {
203         SiS310SetupDSTColorDepth(ivideo->DstColor);
204         SiS310SetupSRCPitch(ivideo->video_linelength)
205         SiS310SetupDSTRect(ivideo->video_linelength, 0x0fff)
206         if(trans_color != -1) {
207                 SiS310SetupROP(0x0A)
208                 SiS310SetupSRCTrans(trans_color)
209                 SiS310SetupCMDFlag(TRANSPARENT_BITBLT)
210         } else {
211                 SiS310SetupROP(sisALUConv[rop])
212                 /* Set command - not needed, both 0 */
213                 /* SiSSetupCMDFlag(BITBLT | SRCVIDEO) */
214         }
215         SiS310SetupCMDFlag(ivideo->SiS310_AccelDepth)
216         /* The chip is smart enough to know the direction */
217 }
218
219 static void
220 SiS310SubsequentScreenToScreenCopy(struct sis_video_info *ivideo, int src_x, int src_y,
221                          int dst_x, int dst_y, int width, int height)
222 {
223         u32 srcbase = 0, dstbase = 0;
224         int mymin = min(src_y, dst_y);
225         int mymax = max(src_y, dst_y);
226
227         /* Although the chip knows the direction to use
228          * if the source and destination areas overlap,
229          * that logic fails if we fiddle with the bitmap
230          * addresses. Therefore, we check if the source
231          * and destination blitting areas overlap and
232          * adapt the bitmap addresses synchronously
233          * if the coordinates exceed the valid range.
234          * The the areas do not overlap, we do our
235          * normal check.
236          */
237         if((mymax - mymin) < height) {
238                 if((src_y >= 2048) || (dst_y >= 2048)) {
239                         srcbase = ivideo->video_linelength * mymin;
240                         dstbase = ivideo->video_linelength * mymin;
241                         src_y -= mymin;
242                         dst_y -= mymin;
243                 }
244         } else {
245                 if(src_y >= 2048) {
246                         srcbase = ivideo->video_linelength * src_y;
247                         src_y = 0;
248                 }
249                 if(dst_y >= 2048) {
250                         dstbase = ivideo->video_linelength * dst_y;
251                         dst_y = 0;
252                 }
253         }
254
255         srcbase += ivideo->video_offset;
256         dstbase += ivideo->video_offset;
257
258         SiS310SetupSRCBase(srcbase);
259         SiS310SetupDSTBase(dstbase);
260         SiS310SetupRect(width, height)
261         SiS310SetupSRCXY(src_x, src_y)
262         SiS310SetupDSTXY(dst_x, dst_y)
263         SiS310DoCMD
264 }
265
266 static void
267 SiS310SetupForSolidFill(struct sis_video_info *ivideo, u32 color, int rop)
268 {
269         SiS310SetupPATFG(color)
270         SiS310SetupDSTRect(ivideo->video_linelength, 0x0fff)
271         SiS310SetupDSTColorDepth(ivideo->DstColor);
272         SiS310SetupROP(sisPatALUConv[rop])
273         SiS310SetupCMDFlag(PATFG | ivideo->SiS310_AccelDepth)
274 }
275
276 static void
277 SiS310SubsequentSolidFillRect(struct sis_video_info *ivideo, int x, int y, int w, int h)
278 {
279         u32 dstbase = 0;
280
281         if(y >= 2048) {
282                 dstbase = ivideo->video_linelength * y;
283                 y = 0;
284         }
285         dstbase += ivideo->video_offset;
286         SiS310SetupDSTBase(dstbase)
287         SiS310SetupDSTXY(x,y)
288         SiS310SetupRect(w,h)
289         SiS310SetupCMDFlag(BITBLT)
290         SiS310DoCMD
291 }
292 #endif
293
294 /* --------------------------------------------------------------------- */
295
296 /* The exported routines */
297
298 int sisfb_initaccel(struct sis_video_info *ivideo)
299 {
300 #ifdef SISFB_USE_SPINLOCKS
301         spin_lock_init(&ivideo->lockaccel);
302 #endif
303         return 0;
304 }
305
306 void sisfb_syncaccel(struct sis_video_info *ivideo)
307 {
308         if(ivideo->sisvga_engine == SIS_300_VGA) {
309 #ifdef CONFIG_FB_SIS_300
310                 SiS300Sync(ivideo);
311 #endif
312         } else {
313 #ifdef CONFIG_FB_SIS_315
314                 SiS310Sync(ivideo);
315 #endif
316         }
317 }
318
319 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)  /* --------------- 2.5 --------------- */
320
321 int fbcon_sis_sync(struct fb_info *info)
322 {
323         struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
324         CRITFLAGS
325
326         if((!ivideo->accel) || (!ivideo->engineok))
327                 return 0;
328
329         CRITBEGIN
330         sisfb_syncaccel(ivideo);
331         CRITEND
332
333         return 0;
334 }
335
336 void fbcon_sis_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
337 {
338         struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
339         u32 col = 0;
340         u32 vxres = info->var.xres_virtual;
341         u32 vyres = info->var.yres_virtual;
342         int width, height;
343         CRITFLAGS
344
345         if(info->state != FBINFO_STATE_RUNNING)
346                 return;
347
348         if((!ivideo->accel) || (!ivideo->engineok)) {
349                 cfb_fillrect(info, rect);
350                 return;
351         }
352
353         if(!rect->width || !rect->height || rect->dx >= vxres || rect->dy >= vyres)
354                 return;
355
356         /* Clipping */
357         width = ((rect->dx + rect->width) > vxres) ? (vxres - rect->dx) : rect->width;
358         height = ((rect->dy + rect->height) > vyres) ? (vyres - rect->dy) : rect->height;
359
360         switch(info->var.bits_per_pixel) {
361         case 8:  col = rect->color;
362                  break;
363         case 16:
364         case 32: col = ((u32 *)(info->pseudo_palette))[rect->color];
365                  break;
366         }
367
368         if(ivideo->sisvga_engine == SIS_300_VGA) {
369 #ifdef CONFIG_FB_SIS_300
370                 CRITBEGIN
371                 SiS300SetupForSolidFill(ivideo, col, myrops[rect->rop]);
372                 SiS300SubsequentSolidFillRect(ivideo, rect->dx, rect->dy, width, height);
373                 CRITEND
374 #endif
375         } else {
376 #ifdef CONFIG_FB_SIS_315
377                 CRITBEGIN
378                 SiS310SetupForSolidFill(ivideo, col, myrops[rect->rop]);
379                 SiS310SubsequentSolidFillRect(ivideo, rect->dx, rect->dy, width, height);
380                 CRITEND
381 #endif
382         }
383
384         sisfb_syncaccel(ivideo);
385 }
386
387 void fbcon_sis_copyarea(struct fb_info *info, const struct fb_copyarea *area)
388 {
389         struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
390         u32 vxres = info->var.xres_virtual;
391         u32 vyres = info->var.yres_virtual;
392         int width = area->width;
393         int height = area->height;
394         CRITFLAGS
395
396         if(info->state != FBINFO_STATE_RUNNING)
397                 return;
398
399         if((!ivideo->accel) || (!ivideo->engineok)) {
400                 cfb_copyarea(info, area);
401                 return;
402         }
403
404         if(!width || !height ||
405            area->sx >= vxres || area->sy >= vyres ||
406            area->dx >= vxres || area->dy >= vyres)
407                 return;
408
409         /* Clipping */
410         if((area->sx + width) > vxres) width = vxres - area->sx;
411         if((area->dx + width) > vxres) width = vxres - area->dx;
412         if((area->sy + height) > vyres) height = vyres - area->sy;
413         if((area->dy + height) > vyres) height = vyres - area->dy;
414
415         if(ivideo->sisvga_engine == SIS_300_VGA) {
416 #ifdef CONFIG_FB_SIS_300
417                 int xdir, ydir;
418
419                 if(area->sx < area->dx) xdir = 0;
420                 else                    xdir = 1;
421                 if(area->sy < area->dy) ydir = 0;
422                 else                    ydir = 1;
423
424                 CRITBEGIN
425                 SiS300SetupForScreenToScreenCopy(ivideo, xdir, ydir, 3, -1);
426                 SiS300SubsequentScreenToScreenCopy(ivideo, area->sx, area->sy,
427                                         area->dx, area->dy, width, height);
428                 CRITEND
429 #endif
430         } else {
431 #ifdef CONFIG_FB_SIS_315
432                 CRITBEGIN
433                 SiS310SetupForScreenToScreenCopy(ivideo, 3, -1);
434                 SiS310SubsequentScreenToScreenCopy(ivideo, area->sx, area->sy,
435                                         area->dx, area->dy, width, height);
436                 CRITEND
437 #endif
438         }
439
440         sisfb_syncaccel(ivideo);
441 }
442
443 #endif
444
445 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)  /* -------------- 2.4 --------------- */
446
447 #include "sisfb_accel_2_4.h"
448
449 #endif /* KERNEL VERSION */
450
451