Merge branch 'x86-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / drivers / video / matrox / matroxfb_accel.c
1 /*
2  *
3  * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
4  *
5  * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
6  *
7  * Version: 1.65 2002/08/14
8  *
9  * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
10  *
11  * Contributors: "menion?" <menion@mindless.com>
12  *                     Betatesting, fixes, ideas
13  *
14  *               "Kurt Garloff" <garloff@suse.de>
15  *                     Betatesting, fixes, ideas, videomodes, videomodes timmings
16  *
17  *               "Tom Rini" <trini@kernel.crashing.org>
18  *                     MTRR stuff, PPC cleanups, betatesting, fixes, ideas
19  *
20  *               "Bibek Sahu" <scorpio@dodds.net>
21  *                     Access device through readb|w|l and write b|w|l
22  *                     Extensive debugging stuff
23  *
24  *               "Daniel Haun" <haund@usa.net>
25  *                     Testing, hardware cursor fixes
26  *
27  *               "Scott Wood" <sawst46+@pitt.edu>
28  *                     Fixes
29  *
30  *               "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
31  *                     Betatesting
32  *
33  *               "Kelly French" <targon@hazmat.com>
34  *               "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
35  *                     Betatesting, bug reporting
36  *
37  *               "Pablo Bianucci" <pbian@pccp.com.ar>
38  *                     Fixes, ideas, betatesting
39  *
40  *               "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
41  *                     Fixes, enhandcements, ideas, betatesting
42  *
43  *               "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
44  *                     PPC betatesting, PPC support, backward compatibility
45  *
46  *               "Paul Womar" <Paul@pwomar.demon.co.uk>
47  *               "Owen Waller" <O.Waller@ee.qub.ac.uk>
48  *                     PPC betatesting
49  *
50  *               "Thomas Pornin" <pornin@bolet.ens.fr>
51  *                     Alpha betatesting
52  *
53  *               "Pieter van Leuven" <pvl@iae.nl>
54  *               "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
55  *                     G100 testing
56  *
57  *               "H. Peter Arvin" <hpa@transmeta.com>
58  *                     Ideas
59  *
60  *               "Cort Dougan" <cort@cs.nmt.edu>
61  *                     CHRP fixes and PReP cleanup
62  *
63  *               "Mark Vojkovich" <mvojkovi@ucsd.edu>
64  *                     G400 support
65  *
66  * (following author is not in any relation with this code, but his code
67  *  is included in this driver)
68  *
69  * Based on framebuffer driver for VBE 2.0 compliant graphic boards
70  *     (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
71  *
72  * (following author is not in any relation with this code, but his ideas
73  *  were used when writing this driver)
74  *
75  *               FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
76  *
77  */
78
79 #include "matroxfb_accel.h"
80 #include "matroxfb_DAC1064.h"
81 #include "matroxfb_Ti3026.h"
82 #include "matroxfb_misc.h"
83
84 #define curr_ydstorg(x) ACCESS_FBINFO2(x, curr.ydstorg.pixels)
85
86 #define mga_ydstlen(y,l) mga_outl(M_YDSTLEN | M_EXEC, ((y) << 16) | (l))
87
88 static inline void matrox_cfb4_pal(u_int32_t* pal) {
89         unsigned int i;
90         
91         for (i = 0; i < 16; i++) {
92                 pal[i] = i * 0x11111111U;
93         }
94 }
95
96 static inline void matrox_cfb8_pal(u_int32_t* pal) {
97         unsigned int i;
98         
99         for (i = 0; i < 16; i++) {
100                 pal[i] = i * 0x01010101U;
101         }
102 }
103
104 static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area);
105 static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
106 static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image);
107 static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
108 static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area);
109
110 void matrox_cfbX_init(WPMINFO2) {
111         u_int32_t maccess;
112         u_int32_t mpitch;
113         u_int32_t mopmode;
114         int accel;
115
116         DBG(__func__)
117
118         mpitch = ACCESS_FBINFO(fbcon).var.xres_virtual;
119
120         ACCESS_FBINFO(fbops).fb_copyarea = cfb_copyarea;
121         ACCESS_FBINFO(fbops).fb_fillrect = cfb_fillrect;
122         ACCESS_FBINFO(fbops).fb_imageblit = cfb_imageblit;
123         ACCESS_FBINFO(fbops).fb_cursor = NULL;
124
125         accel = (ACCESS_FBINFO(fbcon).var.accel_flags & FB_ACCELF_TEXT) == FB_ACCELF_TEXT;
126
127         switch (ACCESS_FBINFO(fbcon).var.bits_per_pixel) {
128                 case 4:         maccess = 0x00000000;   /* accelerate as 8bpp video */
129                                 mpitch = (mpitch >> 1) | 0x8000; /* disable linearization */
130                                 mopmode = M_OPMODE_4BPP;
131                                 matrox_cfb4_pal(ACCESS_FBINFO(cmap));
132                                 if (accel && !(mpitch & 1)) {
133                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_cfb4_copyarea;
134                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_cfb4_fillrect;
135                                 }
136                                 break;
137                 case 8:         maccess = 0x00000000;
138                                 mopmode = M_OPMODE_8BPP;
139                                 matrox_cfb8_pal(ACCESS_FBINFO(cmap));
140                                 if (accel) {
141                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
142                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
143                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
144                                 }
145                                 break;
146                 case 16:        if (ACCESS_FBINFO(fbcon).var.green.length == 5)
147                                         maccess = 0xC0000001;
148                                 else
149                                         maccess = 0x40000001;
150                                 mopmode = M_OPMODE_16BPP;
151                                 if (accel) {
152                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
153                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
154                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
155                                 }
156                                 break;
157                 case 24:        maccess = 0x00000003;
158                                 mopmode = M_OPMODE_24BPP;
159                                 if (accel) {
160                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
161                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
162                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
163                                 }
164                                 break;
165                 case 32:        maccess = 0x00000002;
166                                 mopmode = M_OPMODE_32BPP;
167                                 if (accel) {
168                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
169                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
170                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
171                                 }
172                                 break;
173                 default:        maccess = 0x00000000;
174                                 mopmode = 0x00000000;
175                                 break;  /* turn off acceleration!!! */
176         }
177         mga_fifo(8);
178         mga_outl(M_PITCH, mpitch);
179         mga_outl(M_YDSTORG, curr_ydstorg(MINFO));
180         if (ACCESS_FBINFO(capable.plnwt))
181                 mga_outl(M_PLNWT, -1);
182         if (ACCESS_FBINFO(capable.srcorg)) {
183                 mga_outl(M_SRCORG, 0);
184                 mga_outl(M_DSTORG, 0);
185         }
186         mga_outl(M_OPMODE, mopmode);
187         mga_outl(M_CXBNDRY, 0xFFFF0000);
188         mga_outl(M_YTOP, 0);
189         mga_outl(M_YBOT, 0x01FFFFFF);
190         mga_outl(M_MACCESS, maccess);
191         ACCESS_FBINFO(accel.m_dwg_rect) = M_DWG_TRAP | M_DWG_SOLID | M_DWG_ARZERO | M_DWG_SGNZERO | M_DWG_SHIFTZERO;
192         if (isMilleniumII(MINFO)) ACCESS_FBINFO(accel.m_dwg_rect) |= M_DWG_TRANSC;
193         ACCESS_FBINFO(accel.m_opmode) = mopmode;
194 }
195
196 EXPORT_SYMBOL(matrox_cfbX_init);
197
198 static void matrox_accel_bmove(WPMINFO int vxres, int sy, int sx, int dy, int dx, int height, int width) {
199         int start, end;
200         CRITFLAGS
201
202         DBG(__func__)
203
204         CRITBEGIN
205
206         if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
207                 mga_fifo(2);
208                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
209                          M_DWG_BFCOL | M_DWG_REPLACE);
210                 mga_outl(M_AR5, vxres);
211                 width--;
212                 start = sy*vxres+sx+curr_ydstorg(MINFO);
213                 end = start+width;
214         } else {
215                 mga_fifo(3);
216                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
217                 mga_outl(M_SGN, 5);
218                 mga_outl(M_AR5, -vxres);
219                 width--;
220                 end = (sy+height-1)*vxres+sx+curr_ydstorg(MINFO);
221                 start = end+width;
222                 dy += height-1;
223         }
224         mga_fifo(4);
225         mga_outl(M_AR0, end);
226         mga_outl(M_AR3, start);
227         mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
228         mga_ydstlen(dy, height);
229         WaitTillIdle();
230
231         CRITEND
232 }
233
234 static void matrox_accel_bmove_lin(WPMINFO int vxres, int sy, int sx, int dy, int dx, int height, int width) {
235         int start, end;
236         CRITFLAGS
237
238         DBG(__func__)
239
240         CRITBEGIN
241
242         if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
243                 mga_fifo(2);
244                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
245                         M_DWG_BFCOL | M_DWG_REPLACE);
246                 mga_outl(M_AR5, vxres);
247                 width--;
248                 start = sy*vxres+sx+curr_ydstorg(MINFO);
249                 end = start+width;
250         } else {
251                 mga_fifo(3);
252                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
253                 mga_outl(M_SGN, 5);
254                 mga_outl(M_AR5, -vxres);
255                 width--;
256                 end = (sy+height-1)*vxres+sx+curr_ydstorg(MINFO);
257                 start = end+width;
258                 dy += height-1;
259         }
260         mga_fifo(5);
261         mga_outl(M_AR0, end);
262         mga_outl(M_AR3, start);
263         mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
264         mga_outl(M_YDST, dy*vxres >> 5);
265         mga_outl(M_LEN | M_EXEC, height);
266         WaitTillIdle();
267
268         CRITEND
269 }
270
271 static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
272         MINFO_FROM_INFO(info);
273
274         if ((area->sx | area->dx | area->width) & 1)
275                 cfb_copyarea(info, area);
276         else
277                 matrox_accel_bmove_lin(PMINFO ACCESS_FBINFO(fbcon.var.xres_virtual) >> 1, area->sy, area->sx >> 1, area->dy, area->dx >> 1, area->height, area->width >> 1);
278 }
279
280 static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
281         MINFO_FROM_INFO(info);
282
283         matrox_accel_bmove(PMINFO ACCESS_FBINFO(fbcon.var.xres_virtual), area->sy, area->sx, area->dy, area->dx, area->height, area->width);
284 }
285
286 static void matroxfb_accel_clear(WPMINFO u_int32_t color, int sy, int sx, int height,
287                 int width) {
288         CRITFLAGS
289
290         DBG(__func__)
291
292         CRITBEGIN
293
294         mga_fifo(5);
295         mga_outl(M_DWGCTL, ACCESS_FBINFO(accel.m_dwg_rect) | M_DWG_REPLACE);
296         mga_outl(M_FCOL, color);
297         mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
298         mga_ydstlen(sy, height);
299         WaitTillIdle();
300
301         CRITEND
302 }
303
304 static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
305         MINFO_FROM_INFO(info);
306
307         switch (rect->rop) {
308                 case ROP_COPY:
309                         matroxfb_accel_clear(PMINFO ((u_int32_t*)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
310                         break;
311         }
312 }
313
314 static void matroxfb_cfb4_clear(WPMINFO u_int32_t bgx, int sy, int sx, int height, int width) {
315         int whattodo;
316         CRITFLAGS
317
318         DBG(__func__)
319
320         CRITBEGIN
321
322         whattodo = 0;
323         if (sx & 1) {
324                 sx ++;
325                 if (!width) return;
326                 width --;
327                 whattodo = 1;
328         }
329         if (width & 1) {
330                 whattodo |= 2;
331         }
332         width >>= 1;
333         sx >>= 1;
334         if (width) {
335                 mga_fifo(5);
336                 mga_outl(M_DWGCTL, ACCESS_FBINFO(accel.m_dwg_rect) | M_DWG_REPLACE2);
337                 mga_outl(M_FCOL, bgx);
338                 mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
339                 mga_outl(M_YDST, sy * ACCESS_FBINFO(fbcon).var.xres_virtual >> 6);
340                 mga_outl(M_LEN | M_EXEC, height);
341                 WaitTillIdle();
342         }
343         if (whattodo) {
344                 u_int32_t step = ACCESS_FBINFO(fbcon).var.xres_virtual >> 1;
345                 vaddr_t vbase = ACCESS_FBINFO(video.vbase);
346                 if (whattodo & 1) {
347                         unsigned int uaddr = sy * step + sx - 1;
348                         u_int32_t loop;
349                         u_int8_t bgx2 = bgx & 0xF0;
350                         for (loop = height; loop > 0; loop --) {
351                                 mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0x0F) | bgx2);
352                                 uaddr += step;
353                         }
354                 }
355                 if (whattodo & 2) {
356                         unsigned int uaddr = sy * step + sx + width;
357                         u_int32_t loop;
358                         u_int8_t bgx2 = bgx & 0x0F;
359                         for (loop = height; loop > 0; loop --) {
360                                 mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0xF0) | bgx2);
361                                 uaddr += step;
362                         }
363                 }
364         }
365
366         CRITEND
367 }
368
369 static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
370         MINFO_FROM_INFO(info);
371
372         switch (rect->rop) {
373                 case ROP_COPY:
374                         matroxfb_cfb4_clear(PMINFO ((u_int32_t*)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
375                         break;
376         }
377 }
378
379 static void matroxfb_1bpp_imageblit(WPMINFO u_int32_t fgx, u_int32_t bgx,
380                 const u_int8_t* chardata, int width, int height, int yy, int xx) {
381         u_int32_t step;
382         u_int32_t ydstlen;
383         u_int32_t xlen;
384         u_int32_t ar0;
385         u_int32_t charcell;
386         u_int32_t fxbndry;
387         vaddr_t mmio;
388         int easy;
389         CRITFLAGS
390
391         DBG_HEAVY(__func__);
392
393         step = (width + 7) >> 3;
394         charcell = height * step;
395         xlen = (charcell + 3) & ~3;
396         ydstlen = (yy << 16) | height;
397         if (width == step << 3) {
398                 ar0 = height * width - 1;
399                 easy = 1;
400         } else {
401                 ar0 = width - 1;
402                 easy = 0;
403         }
404
405         CRITBEGIN
406
407         mga_fifo(3);
408         if (easy)
409                 mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_LINEAR | M_DWG_REPLACE);
410         else
411                 mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_REPLACE);
412         mga_outl(M_FCOL, fgx);
413         mga_outl(M_BCOL, bgx);
414         fxbndry = ((xx + width - 1) << 16) | xx;
415         mmio = ACCESS_FBINFO(mmio.vbase);
416
417         mga_fifo(6);
418         mga_writel(mmio, M_FXBNDRY, fxbndry);
419         mga_writel(mmio, M_AR0, ar0);
420         mga_writel(mmio, M_AR3, 0);
421         if (easy) {
422                 mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
423                 mga_memcpy_toio(mmio, chardata, xlen);
424         } else {
425                 mga_writel(mmio, M_AR5, 0);
426                 mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
427                 if ((step & 3) == 0) {
428                         /* Great. Source has 32bit aligned lines, so we can feed them
429                            directly to the accelerator. */
430                         mga_memcpy_toio(mmio, chardata, charcell);
431                 } else if (step == 1) {
432                         /* Special case for 1..8bit widths */
433                         while (height--) {
434 #if defined(__BIG_ENDIAN)
435                                 fb_writel((*chardata) << 24, mmio.vaddr);
436 #else
437                                 fb_writel(*chardata, mmio.vaddr);
438 #endif
439                                 chardata++;
440                         }
441                 } else if (step == 2) {
442                         /* Special case for 9..15bit widths */
443                         while (height--) {
444 #if defined(__BIG_ENDIAN)
445                                 fb_writel((*(u_int16_t*)chardata) << 16, mmio.vaddr);
446 #else
447                                 fb_writel(*(u_int16_t*)chardata, mmio.vaddr);
448 #endif
449                                 chardata += 2;
450                         }
451                 } else {
452                         /* Tell... well, why bother... */
453                         while (height--) {
454                                 size_t i;
455                                 
456                                 for (i = 0; i < step; i += 4) {
457                                         /* Hope that there are at least three readable bytes beyond the end of bitmap */
458                                         fb_writel(get_unaligned((u_int32_t*)(chardata + i)),mmio.vaddr);
459                                 }
460                                 chardata += step;
461                         }
462                 }
463         }
464         WaitTillIdle();
465         CRITEND
466 }
467
468
469 static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image) {
470         MINFO_FROM_INFO(info);
471
472         DBG_HEAVY(__func__);
473
474         if (image->depth == 1) {
475                 u_int32_t fgx, bgx;
476
477                 fgx = ((u_int32_t*)info->pseudo_palette)[image->fg_color];
478                 bgx = ((u_int32_t*)info->pseudo_palette)[image->bg_color];
479                 matroxfb_1bpp_imageblit(PMINFO fgx, bgx, image->data, image->width, image->height, image->dy, image->dx);
480         } else {
481                 /* Danger! image->depth is useless: logo painting code always
482                    passes framebuffer color depth here, although logo data are
483                    always 8bpp and info->pseudo_palette is changed to contain
484                    logo palette to be used (but only for true/direct-color... sic...).
485                    So do it completely in software... */
486                 cfb_imageblit(info, image);
487         }
488 }
489
490 MODULE_LICENSE("GPL");