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