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